Skip to content

feat(subscriptions): add AI-prompt subscription fields + activity logging#59629

Open
vdekrijger wants to merge 6 commits into
subscriptions-ai-summary-credit-gatefrom
ai-model
Open

feat(subscriptions): add AI-prompt subscription fields + activity logging#59629
vdekrijger wants to merge 6 commits into
subscriptions-ai-summary-credit-gatefrom
ai-model

Conversation

@vdekrijger
Copy link
Copy Markdown
Contributor

@vdekrijger vdekrijger commented May 22, 2026

Problem

Subscriptions currently only support insights and dashboards as their target resource. To support AI-powered subscription reports (where a user provides a prompt rather than linking to an existing insight or dashboard), the model needs a way to represent and classify this new resource type.

Additionally, subscription saves were not being recorded in the activity log, making it impossible to audit changes to subscription configuration.

Changes

  • Adds a prompt field to Subscription for AI-prompt subscriptions
  • Classifies a subscription as insight, dashboard, or ai_prompt via a derived resource_type property rather than a stored column. Because it is computed on read from the existing relation/prompt:
    • there is no migration backfill and no column default to keep in sync
    • it can be omitted from activity-log change diffs — it is not a field, so prompt/relation edits are tracked but resource_type never shows up as its own spurious change
    • the ResourceType choices are retained for the resource-type lookups needed by the follow-up PRs
  • url and resource_info now dispatch on the derived resource_type
  • Adds a display_name property that resolves a human-readable name from the subscription's resource info, falling back gracefully through title → prompt snippet → "AI report"
  • Wires Subscription into the activity logging system via ModelActivityMixin and a model_activity_signal receiver, so creates and updates are recorded in the activity log
  • Excludes next_delivery_date from both signal emission (via signal_exclusions) and change diffs (via diff_exclusions), so scheduler-only saves never produce spurious activity log entries
  • Adds resource_type (derived) and prompt_length to get_analytics_metadata()
  • Registers a new feature flag constant SUBSCRIPTION_AI_PROMPT_FEATURE_FLAG_KEY = "ai-subscriptions"

How did you test this code?

New test file test_subscription_activity_log.py covers:

  • Activity log entries created on subscription create (parameterized across insight / dashboard / AI) and update
  • Correct display_name resolution across all resource types and fallback cases
  • Prompt field change recording, including null transitions
  • Soft-delete recording
  • Exclusion of next_delivery_date from diffs even in mixed saves
  • Scheduler-only saves (both with and without update_fields) producing no new log entries

Existing test_subscription_model.py extended with parameterized tests verifying resource_type is correctly derived from the relation.

Publish to changelog?

No

@github-actions
Copy link
Copy Markdown
Contributor

github-actions Bot commented May 22, 2026

🎭 Playwright didn't run on this PR — your changes touch code that could affect E2E behavior, but Playwright is opt-in via label now to keep CI cost down.

Add the run-playwright label if you want an E2E sweep before merging — CI will pick it up automatically.

Most PRs don't need this. Real regressions still get caught on master and fix-forward.

@github-actions
Copy link
Copy Markdown
Contributor

github-actions Bot commented May 22, 2026

Migration SQL Changes

Hey 👋, we've detected some migrations on this PR. Here's the SQL output for each migration, make sure they make sense:

posthog/migrations/1177_subscription_ai_fields.py

BEGIN;
--
-- Add field prompt to subscription
--
ALTER TABLE "posthog_subscription" ADD COLUMN "prompt" text NULL;
COMMIT;

Last updated: 2026-05-27 11:33 UTC (429d4c6)

@github-actions
Copy link
Copy Markdown
Contributor

github-actions Bot commented May 22, 2026

🔍 Migration Risk Analysis

We've analyzed your migrations for potential risks.

Summary: 1 Safe | 0 Needs Review | 0 Blocked

✅ Safe

Brief or no lock, backwards compatible

posthog.1177_subscription_ai_fields
  └─ #1 ✅ AddField
     Adding nullable field requires brief lock
     model: subscription, field: prompt

📚 How to Deploy These Changes Safely

AddField:

This operation acquires a brief lock but doesn't rewrite the table.

Deployment uses lock timeouts with automatic retries, so lock contention will cause retries rather than connection pile-up.

Last updated: 2026-05-27 11:34 UTC (429d4c6)

Comment thread posthog/migrations/1177_subscription_ai_fields.py Outdated
Comment thread posthog/models/subscription.py Outdated
Comment thread posthog/models/subscription.py Outdated
Comment thread posthog/models/subscription.py Outdated
Comment thread posthog/models/subscription.py Outdated
Comment thread posthog/migrations/1177_subscription_ai_fields.py Outdated
Comment thread posthog/models/subscription.py Outdated
- rename content_type -> resource_type (avoids clash with django contenttypes)
- drop ai_config (YAGNI; not part of MVP)
- move activity logging onto ModelActivityMixin + a model_activity_signal
  receiver (matches AlertConfiguration), so changes are auto-diffed for the
  audit trail and the activity_log import is module-level
- exclude next_delivery_date from both the signal and the change diff so
  scheduler bumps are not logged
- fix AI resource kind ("AI" not "AI report") so email subjects read correctly
- extract ai_display_name + AI_PROMPT_DISPLAY_MAX_LEN to remove duplication
- add activity-logging tests (create/update/scheduler/soft-delete/transitions)
@vdekrijger vdekrijger changed the title feat(subscriptions): add AI-prompt content_type, prompt, ai_config fields feat(subscriptions): add AI-prompt subscription fields + activity logging May 27, 2026
Comment thread posthog/models/subscription.py
Comment thread posthog/models/subscription.py Outdated
Comment thread posthog/models/subscription.py Outdated
Comment thread posthog/models/subscription.py Outdated
Comment thread posthog/models/subscription.py Outdated
Address review feedback on PR #59629:
- Log activity for all subscription changes, not just AI (records are cheap
  and useful for debugging/undo); drop the AI_PROMPT and created_by guards
- Generalise ai_display_name into a display_name property that resolves the
  name for insight/dashboard/AI subscriptions via resource_info
- Remove redundant inline comments flagged in review

Infra was already in place (Subscription is a registered activity scope,
next_delivery_date excluded from both signal and diff, target_value masked).
Comment thread posthog/models/subscription.py Outdated
Comment thread posthog/models/test/test_subscription_activity_log.py Outdated
@vdekrijger vdekrijger marked this pull request as ready for review May 27, 2026 10:33
@greptile-apps
Copy link
Copy Markdown
Contributor

greptile-apps Bot commented May 27, 2026

Prompt To Fix All With AI
Fix the following 1 code review issue. Work through them one at a time, proposing concise fixes.

---

### Issue 1 of 1
posthog/models/test/test_subscription_activity_log.py:37-82
**AI and non-AI creation tests can be unified**

`test_creating_ai_subscription_logs_activity` and `test_non_ai_subscription_logs_activity` test the same assertion pattern (count, activity, item_id, name) for three symmetric cases. The AI case can be added as a third entry in the `@parameterized.expand` list so all three resource-type creation scenarios live in one parameterised test, following the team's preference for parameterised tests over one-off test methods for symmetric cases.

Reviews (1): Last reviewed commit: "chore(subscriptions): drop explanatory c..." | Re-trigger Greptile

Comment thread posthog/models/test/test_subscription_activity_log.py
@github-actions
Copy link
Copy Markdown
Contributor

ClickHouse migration SQL per cloud environment

No ClickHouse migrations changed in this PR.

@vdekrijger vdekrijger added the stamphog Request AI review from stamphog label May 27, 2026 — with Graphite App
Copy link
Copy Markdown

@stamphog stamphog Bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Gates denied this PR: it touches billing-related code (subscriptions) and is classified as requiring human review. This needs a human reviewer to approve.

@stamphog stamphog Bot removed the stamphog Request AI review from stamphog label May 27, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant