:hedgehog: PostHog is the leading platform for building self-driving products. Our developer tools – AI observability, analytics, session replay, flags, experiments, error tracking, logs, and more – capture all the context agents need to diagnose problems, uncover opportunities, and ship fixes. Steer it all from Slack, web, desktop, or the MCP.
Python ab-testingai-analyticsanalyticscdpdata-warehouseexperimentsfeature-flagsjavascriptproduct-analyticspythonreactsession-replaysurveystypescriptweb-analytics
fix(loops): accept event.action shorthand and show trigger events (#73094)
Charles Vien66f495c1cada
diff --git a/products/tasks/backend/presentation/serializers_loops.py b/products/tasks/backend/presentation/serializers_loops.pyindex a1fb8431f041..89608e00e054 100644--- a/products/tasks/backend/presentation/serializers_loops.py+++ b/products/tasks/backend/presentation/serializers_loops.py@@ -192,16 +192,46 @@ def _validate_github_trigger_config(config: dict, team_id: int) -> dict:{"repository": "Repository is not accessible via the selected GitHub integration."})- events = config.get("events")- if not isinstance(events, list) or not events:+ events_raw = config.get("events")+ if not isinstance(events_raw, list) or not events_raw:raise serializers.ValidationError({"events": "At least one event is required."})+ # Agents reach for GitHub Actions shorthand like `issues.opened`; fold it into the bare+ # webhook event plus an `actions` filter instead of rejecting the trigger.+ events: list = []+ shorthand_actions: list[str] = []+ has_bare_event = False+ for item in events_raw:+ event = item+ if isinstance(item, str) and "." in item:+ base, action = item.split(".", 1)+ if base in loops_facade.ALLOWED_GITHUB_TRIGGER_EVENTS and action:+ event = base+ if action not in shorthand_actions:+ shorthand_actions.append(action)+ else:+ has_bare_event = True+ if event not in events:+ events.append(event)invalid_events = sorted(set(events) - set(loops_facade.ALLOWED_GITHUB_TRIGGER_EVENTS))if invalid_events:raise serializers.ValidationError({"events": (f"Unsupported event(s): {invalid_events}. "- f"Allowed: {list(loops_facade.ALLOWED_GITHUB_TRIGGER_EVENTS)}."+ f"Allowed: {list(loops_facade.ALLOWED_GITHUB_TRIGGER_EVENTS)}, "+ "optionally with an action suffix like 'issues.opened'."+ )+ }+ )+ # The folded `actions` filter applies to every event in the trigger, so shorthand spanning+ # several events (or mixed with bare events) would make unrequested event/action pairs fire.+ if shorthand_actions and (has_bare_event or len(events) > 1):+ raise serializers.ValidationError(+ {+ "events": (+ "`event.action` shorthand supports a single event per trigger because the "+ "folded `actions` filter applies to every event. Use one trigger per event, "+ "or bare events with `filters.actions`.")})@@ -224,6 +254,12 @@ def _validate_github_trigger_config(config: dict, team_id: int) -> dict:raise serializers.ValidationError({"filters": f"Filter '{key}' must be a string or a list of strings."})filters[key] = values+ if shorthand_actions:+ explicit_actions = filters.get("actions", [])+ filters["actions"] = explicit_actions + [+ action for action in shorthand_actions if action not in explicit_actions+ ]+return {"github_integration_id": github_integration_id,"repository": normalized_repository,@@ -256,7 +292,11 @@ class LoopTriggerWriteSerializer(serializers.Serializer):help_text=("Trigger configuration, shape validated per `type`: schedule takes ""`{cron_expression, timezone}` or `{run_at}` for a one-time run; github takes "- "`{github_integration_id, repository, events, filters}`; api takes no config."+ "`{github_integration_id, repository, events, filters}` where `events` is one or more of "+ f"{', '.join(f'`{event}`' for event in loops_facade.ALLOWED_GITHUB_TRIGGER_EVENTS)} "+ "(`event.action` shorthand like `issues.opened` is folded into an `actions` filter, one "+ "event per trigger) and "+ "`filters` takes `{actions, branches, labels}`; api takes no config."),)diff --git a/products/tasks/backend/tests/test_loops_api.py b/products/tasks/backend/tests/test_loops_api.pyindex 08513224ab9f..1c0ea53c5dd9 100644--- a/products/tasks/backend/tests/test_loops_api.py+++ b/products/tasks/backend/tests/test_loops_api.py@@ -630,6 +630,77 @@ class LoopScheduleTriggerValidationAPITest(LoopsAPITestCase):self.assertEqual(response.status_code, expected_status, response.content)+class LoopGithubTriggerValidationAPITest(LoopsAPITestCase):+ def setUp(self) -> None:+ super().setUp()+ self.integration = Integration.objects.create(team=self.team, kind="github", integration_id="1", config={})+ mock_github = self._start_patch("products.tasks.backend.facade.loops.GitHubIntegration")+ mock_github.return_value.list_all_cached_repositories.return_value = [{"full_name": "acme/repo"}]++ def _github_trigger(self, events: list, filters: dict | None = None) -> dict:+ config: dict = {"github_integration_id": self.integration.id, "repository": "acme/repo", "events": events}+ if filters is not None:+ config["filters"] = filters+ return {"type": "github", "config": config}++ @parameterized.expand(+ [+ ("bare_event", ["issues"], None, ["issues"], {}),+ ("bare_event_with_action_filter", ["issues"], {"actions": ["opened"]}, ["issues"], {"actions": ["opened"]}),+ ("singular_action_alias", ["issues"], {"action": "opened"}, ["issues"], {"actions": ["opened"]}),+ ("dotted_shorthand", ["issues.opened"], None, ["issues"], {"actions": ["opened"]}),+ (+ "dotted_shorthand_duplicates",+ ["issues.opened", "issues.opened"],+ None,+ ["issues"],+ {"actions": ["opened"]},+ ),+ (+ "dotted_shorthand_multiple_actions",+ ["issues.opened", "issues.reopened"],+ None,+ ["issues"],+ {"actions": ["opened", "reopened"]},+ ),+ (+ "dotted_shorthand_merges_with_explicit_actions",+ ["pull_request.opened"],+ {"actions": ["synchronize"]},+ ["pull_request"],+ {"actions": ["synchronize", "opened"]},+ ),+ ]+ )+ def test_event_shorthand_normalization(self, _name, events, filters, expected_events, expected_filters):+ created = self._create_loop(self.owner_client, triggers=[self._github_trigger(events, filters)])+ config = created["triggers"][0]["config"]+ self.assertEqual(config["events"], expected_events)+ self.assertEqual(config["filters"], expected_filters)++ @parameterized.expand(+ [+ ("unknown_event", ["nonsense"]),+ ("unknown_dotted_event", ["nonsense.opened"]),+ ("dotted_event_with_empty_action", ["issues."]),+ # Shorthand mixed with a bare event is ambiguous: the folded actions filter would+ # silently apply to the bare event too.+ ("shorthand_mixed_with_bare_event", ["issues.opened", "push"]),+ ("shorthand_mixed_with_its_own_bare_event", ["issues.opened", "issues"]),+ # Cross-event shorthand would flatten into a cartesian product: pull_request.opened+ # would fire here even though only pull_request.synchronize was requested.+ ("shorthand_spanning_multiple_events", ["issues.opened", "pull_request.synchronize"]),+ ]+ )+ def test_invalid_events_are_rejected(self, _name, events):+ response = self.owner_client.post(+ self._loops_url(),+ self._valid_loop_payload(triggers=[self._github_trigger(events)]),+ format="json",+ )+ self.assertEqual(response.status_code, status.HTTP_400_BAD_REQUEST, response.content)++class LoopServiceReadbackAPITest(LoopsAPITestCase):def _psak(self, scopes) -> str:raw_token = generate_random_token_secret()diff --git a/products/tasks/docs/LOOPS-FOLLOWUPS.md b/products/tasks/docs/LOOPS-FOLLOWUPS.mddeleted file mode 100644index 528a07cbea1f..000000000000--- a/products/tasks/docs/LOOPS-FOLLOWUPS.md+++ /dev/null@@ -1,84 +0,0 @@-# Loops: deferred hardening and next steps--Backlog left over from a loop-primitive hardening pass that closed the schedule-teardown gaps and the critical write, run and audit-log bugs.-Everything below is a known, accepted gap, not a surprise.-The "Landed" section is here for context; the rest of the doc is the actionable backlog.-File paths are relative to `products/tasks/backend/` unless noted.--## Landed--- Schedule teardown (no more zombie Temporal schedules): every path that removes a loop or trigger now tears down its Temporal Schedule, team/org/project deletion, Django admin hard-delete, soft-delete, reconciliation, trigger type-change and the one-time (`run_at`) fire.-- Critical bugs: org activity-log crash, loop hijack via ownership takeover, partial-PATCH config corruption, terminal-run resurrection, owner-deactivation not stopping the sandbox, `LoopFire` `CASCADE` wiping rate-cap history and dead run notifications.-- Hardening: retention-sweep failure isolation, reconciliation no longer swallowing the Celery soft time limit and a locked create-cap check.--## Deferred--### Needs a product or contract decision--- **Server-side confirmation for externally-triggered write-capable loops.**- `LOOPS.md` documents a save-time confirmation for `github`/`api`-triggered loops that carry full MCP scope or `create_prs`, but nothing enforces it and the `loops-review` MCP tool is a pure echo.- Decide the gate (an `acknowledged_external_write_risk` flag?), then enforce it in the write serializer and facade validator.- Files: `presentation/serializers_loops.py`, `facade/loops.py` (`validate_loop_write`), `services/mcp/src/tools/loops/loopsReview.ts`.-- **Manual and API fire idempotency default.**- When no `Idempotency-Key` header is sent, `fire_key` falls back to a fresh UUID, so a double-click or an HTTP retry under `overlap_policy=allow` spawns a duplicate paid run.- Decide: require the header, or derive a stable default from request attributes.- Files: `facade/loops.py` (`fire_loop_manual` / `fire_loop_api`), `presentation/views/loops.py`.-- **User-bound approval for MCP loop creation.**- `loops-create` uses the typed-confirm prepare/execute split, which is friction, not enforcement: the model supplies the `confirmation` argument, so a sufficiently steered agent can self-confirm (see the tradeoff note in `services/mcp/src/tools/confirmed-action-runtime.ts`).- A hard guarantee needs an approval channel the model cannot reach: client-rendered elicitation once host support is broad enough, a host-asserted origin marker on UI-app `callServerTool` calls, or removing `loops-create` from the agent tool surface and making the review card the only creation path.- All three are product/protocol decisions; MCP Apps today gives the review card no channel the model can't also read.- Files: `services/mcp/src/tools/confirmed-action-runtime.ts`, `products/tasks/mcp/tools.yaml`, `services/mcp/src/ui-apps/apps/loops-review.tsx`.--### Needs cross-repo (agent-prompt) work--- **Server-assigned run branch.**- The self-trigger exclusion assumes a `loop/`-prefixed branch, but nothing pins the agent to one, so a `create_prs` + `watch_ci` loop's own commits can re-fire its push trigger, and two concurrent runs of one loop can pick colliding branch names.- Generate a unique `loop/{slug}/{run-shortid}` per fire (mirror `generate_wizard_head_branch` in `prompts.py`) and thread it into the run prompt.- Files: `logic/services/loop_runs.py`, `loop_github_events.py`, plus the agent prompt.--### Lower-priority hardening--- **Auto-pause on repeated zombie crashes.**- The zombie-run reap in `fire_loop` uses a bulk `update()` that bypasses `handle_loop_run_terminal`, so a loop that systematically zombie-crashes never trips `consecutive_failures` or auto-pause.- This is a judgment call: a zombie is an infrastructure failure, so auto-pausing on it may be too aggressive.- If we want it, invoke the terminal bookkeeping per reaped run.- File: `logic/services/loop_runs.py`.-- **Rate-cap accounting.**- `LoopFire` rows are written before the rate checks run, so a trigger already past its own daily cap keeps writing counted rows that can exhaust the shared per-team pool, and GitHub-webhook fires have no request-level throttle.- Exclude non-`created` outcomes from the cap counts (or check caps before writing the row), and add a per-installation or per-repository throttle ahead of webhook-sourced fires.- Files: `logic/services/loop_runs.py`, `loop_github_events.py`.-- **Re-check enabled/deleted under the advisory lock.**- `fire_loop` checks `enabled`/`deleted` on in-memory objects before taking the per-team advisory lock, so a pause that lands mid-flight can still race a fire through.- Re-read and re-check inside the locked section before creating the run.- File: `logic/services/loop_runs.py`.-- **Retention batch cap.**- The task-retention sweep ranks with a window function but has no per-run cap or saturation log, unlike the reconciliation sweep.- Cap it per run and log when the cap is hit.- File: `loop_retention.py`.-- **Retention horizon vs dedup window.**- The 200-task-per-loop retention can soft-delete a task still inside `LoopFire`'s 7-day dedup window, so a delayed retry against an old idempotency key can return a soft-deleted `task_id`.- Skip tasks still referenced by a live `LoopFire.outcome_task_id`, or align the two horizons.- File: `loop_retention.py`.--### Observability and docs--- **`LoopFire` read surface.**- No admin, API or MCP surface exposes `LoopFire`, and the `disabled` and `overlap_skipped` outcomes are not always recorded, so "why didn't my loop fire" needs a raw DB query.- Add a read-only `LoopFireAdmin` and/or a `loops/:id/fires/` action, and record every outcome.- Files: `admin.py`, `logic/services/loop_runs.py`.-- **`LoopTrigger` activity logging.**- Only `Loop` emits activity-log entries; trigger edits (repository, schedule, filters) are invisible, contrary to LOOPS.md's audit-trail claim.- Add `ModelActivityMixin` plus a `sender=LoopTrigger` receiver, or log explicitly in `_sync_triggers`.- Deprioritized.- Files: `activity_logging.py`, `models.py`, `facade/loops.py`.-- **Reconciliation live-diff.**- LOOPS.md claims reconciliation diffs against live Temporal state in both directions, but it only re-syncs rows already flagged `pending`/`failed`.- Either implement the live diff (list Temporal schedules, recreate or delete to match) or narrow the doc.- Files: `loop_reconciliation.py`, `docs/LOOPS.md`.--## Pre-existing branch drift (not from the hardening pass)--A master merge landed on the branch mid-pass. Two unrelated things to clear before the PR is fully green:--- OpenAPI generated types are stale: run `hogli build:openapi` after merging master.-- An unused `# type: ignore` in `posthog/personhog_client/converters.py` that repo-wide mypy may flag.diff --git a/products/tasks/docs/LOOPS-MODELS.md b/products/tasks/docs/LOOPS-MODELS.mddeleted file mode 100644index a8ccfcbac2f6..000000000000--- a/products/tasks/docs/LOOPS-MODELS.md+++ /dev/null@@ -1,96 +0,0 @@-# Loop data model--Concise reference for the three loop tables and the deltas they add to `Task` / `TaskRun`.-Source of truth: `products/tasks/backend/models.py`.-Design rationale and the wider feature live in [LOOPS.md](./LOOPS.md).--Three tables carry the feature:--- `Loop` is the automation a user (or a backend flow) defines.-- `LoopTrigger` is a firing condition attached to a loop.-- `LoopFire` is a per-fire dedup record.--Each firing spawns an ordinary `Task` + `TaskRun` on the standard `process-task` pipeline, so nothing here is a new execution engine.-All three are team-scoped through `TeamScopedRootMixin` (the fail-closed manager), and each carries its own `team` column because that manager filters on a local field, not through an FK.--## Loop--`posthog_task_loop`. The top-level object: instructions plus model config, soft-deletable, owned by a user.--| Field | Notes |-| ---------------------------------------------------------------------- | ---------------------------------------------------------------------------------------------------------------------------------------------------------------------- |-| `team` | FK to Team, `db_constraint=False` (hot table), app-level `CASCADE`. |-| `created_by` | FK to User, the loop's **execution identity** for GitHub authorship, OAuth minting and MCP resolution. `SET_NULL`, `db_constraint=False`. |-| `name`, `description` | Display. |-| `visibility` | `personal` (default) or `team`. Controls who can see and edit the loop (see LOOPS.md "Access control"). |-| `instructions` | The prompt delivered to the agent on every run. |-| `runtime_adapter`, `model`, `reasoning_effort` | Agent selection, validated against the catalog at fire time. |-| `repositories` | JSON list of `{github_integration_id, full_name}`, ordered. Capped at `MAX_LOOP_REPOSITORIES` (1 today); may be empty for report-only loops. |-| `sandbox_environment` | Optional FK to `SandboxEnvironment` (encrypted env vars + network allowlist). `SET_NULL`. |-| `enabled` | Pausing disables every trigger. |-| `overlap_policy` | `skip` (default), `allow` or `cancel_previous`. Applies when a trigger fires while a run is active. |-| `behaviors` | JSON, validated: `{create_prs, watch_ci, fix_review_comments, max_fix_iterations}`. |-| `connectors` | JSON: MCP Store installation ids + `posthog_mcp_scopes` (`read_only` default). |-| `notifications` | JSON: per channel (`push`, `email`, `slack`) an enabled flag, an event filter and channel params. |-| `context_target` | JSON context attachment `{folder_id, name, outputs}`, or `{}` when unattached. |-| `internal` | `True` for loops a backend flow owns; excluded from the user-facing CRUD. |-| `origin_product` | Attribution for what created the loop (mirrors `Task.origin_product`). |-| `last_run_at`, `last_run_status`, `last_error`, `consecutive_failures` | Bookkeeping, updated per run. |-| `disabled_reason` | Why a non-owner pause happened (integration disconnected, owner deactivated), so the UI can explain it and a reactivation can clear it. Null for a normal owner pause. |-| `deleted` | Soft delete. |--Inherits `ModelActivityMixin`, so config edits land in the activity log with before/after diffs.-`_get_before_update` routes the prior-state lookup through `.unscoped()` because loop saves happen from webhook handlers and Temporal activities with no ambient team scope.--## LoopTrigger--`posthog_task_loop_trigger`. A loop has many triggers, each independently enable/disable-able.--| Field | Notes |-| ---------------------------------------------------- | -------------------------------------------------------------------------------------------------------------------------------------------------------------- |-| `team` | Own FK (denormalized off `loop.team`), `db_constraint=False`, `CASCADE`. |-| `loop` | FK to Loop, `CASCADE`. |-| `type` | `schedule`, `github` or `api`. |-| `enabled` | Per-trigger toggle. |-| `config` | JSON, validated per type (see below). |-| `github_integration_id`, `repository`, `event_types` | Denormalized off `config` for `type=github` rows in `save()`, so webhook fan-out matches an indexed column instead of scanning the JSON. Null for other types. |-| `schedule_sync_status` | `pending`, `synced` or `failed`: whether the row's Temporal Schedule is in sync. |-| `last_fired_at` | Bookkeeping. |--`config` shapes by type:--- `schedule`: `{cron_expression, timezone}` for a recurring run, or `{run_at}` (ISO 8601, future) for a one-time run.-- `github`: `{github_integration_id, repository, events, filters}`.-- `api`: no config; fires on `POST /api/projects/:team_id/loops/:loop_id/trigger/`.--Schedule triggers are backed by a Temporal Schedule whose identity is the `schedule_id` property (`loop-trigger-{id}`).-Because that identity hangs off the row PK, trigger rows are **updated in place, never delete-and-recreated** (nested writes match by `id`).-Indexed on `(github_integration_id, repository)` for webhook routing.--## LoopFire--`posthog_task_loop_fire`. A per-fire dedup record, so schedule replays, webhook redeliveries, API retries and double-clicked manual runs never double-spawn a run.--| Field | Notes |-| ---------------------------------------------------------- | ---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |-| `team` | Own FK, `db_constraint=False`, `CASCADE`. |-| `loop` | FK to Loop, nullable, `db_constraint=False`, `CASCADE`. Always set in practice; direct (not via trigger) so manual fires have a dedup scope and rate-cap/retention queries hit an index. |-| `loop_trigger` | FK to LoopTrigger, nullable (`CASCADE`). Null for manual "run now" fires, which have no trigger. |-| `fire_key` | The dedup key: the Temporal workflow id (schedule), the `X-GitHub-Delivery` GUID (webhook) or the client `Idempotency-Key` (API/manual). |-| `outcome_reason`, `outcome_task_id`, `outcome_task_run_id` | The created run's ids and terminal reason, so a dedup hit (a retry) returns the original outcome instead of a bare "deduped". |--Two partial unique constraints enforce dedup:--- `(loop_trigger, fire_key)` when a trigger is present.-- `(loop, fire_key)` when there is no trigger (manual fires), so manual fires don't all collide on a shared `NULL` trigger.--## Task / TaskRun deltas--Loop-spawned tasks are system artifacts, not personal tasks:--- `Task.loop`: nullable, indexed FK back to the loop (the loop detail UI lists runs through it).-- `Task.origin_product`: gains a `loop` value; loop-spawned rows are also `internal=True`, so they never surface in a user's inbox or sidebar.-- `Task.created_by` is the loop owner, because sandbox OAuth minting reads it. Attribution, not ownership.-- `TaskRun` carries the babysitting chain (`origin_run_id`, `fix_iteration`) used to bound follow-up runs across workflow boundaries.--Everything a run uses is snapshotted into `TaskRun` state at fire time (repositories, model config, behaviors, resolved connectors, `loop_id`, `loop_trigger_id`, `trigger_context`), so editing a loop never affects in-flight or queued runs and every run stays self-describing for audit.diff --git a/products/tasks/docs/LOOPS-ONE-OFF-TASKS.md b/products/tasks/docs/LOOPS-ONE-OFF-TASKS.mddeleted file mode 100644index 28acf190a5f6..000000000000--- a/products/tasks/docs/LOOPS-ONE-OFF-TASKS.md+++ /dev/null@@ -1,68 +0,0 @@-# Scheduling tasks from a PostHog product (via loops)--"Run a task in a week, use this prompt, go."-Any PostHog product can hand an agent a job to do once in the future, or on a recurring schedule, without standing up its own infrastructure.-A delayed task is just a loop with a schedule trigger: the schedule lives in Temporal (all cloud, no sandbox TTL problems), and each firing spawns an ordinary Task + TaskRun on the standard `process-task` pipeline.--Schedule config comes in two shapes:--- one-time: `{"type": "schedule", "config": {"run_at": <ISO 8601 datetime, must be in the future>}}` becomes a one-shot Temporal Schedule (`remaining_actions=1`) that fires once.-- recurring: `{"type": "schedule", "config": {"cron_expression": "0 9 * * 1-5", "timezone": "UTC"}}` (standard 5-field cron).--Always go through the facade (`products/tasks/backend/facade/loops.py`), never the models directly.--## Internal (product-managed) loops — recommended--Pass `internal=True` and an `origin_product` so the loop is owned by your flow, not a person.-Internal loops never appear in the user-facing loop CRUD, and the facade gives you helpers to find and tear them down by product.--```python-from datetime import timedelta--from django.utils import timezone--from products.tasks.backend.facade import loops as loops_facade--loop = loops_facade.create_loop(- team_id,- system_user, # execution identity for GitHub authorship, OAuth and MCP, so pass a real user- {- "name": f"PR follow-up: {repo}#{pr_number}",- "instructions": f"PR {pr_url} merged for report {report_id}. Verify the underlying issue is actually fixed. ...",- "runtime_adapter": "claude",- "model": "claude-sonnet-5",- "repositories": [{"github_integration_id": integration_id, "full_name": repo}],- "triggers": [{"type": "schedule", "config": {"run_at": (timezone.now() + timedelta(days=1)).isoformat()}}],- "internal": True,- "origin_product": "your_product",- },-)-```--Lifecycle, all keyed by team and (optionally) your `origin_product`:--- `loops_facade.get_internal_loop(loop_id, team_id)` reads one back.-- `loops_facade.list_internal_loops(team_id, origin_product="your_product")` lists yours.-- `loops_facade.delete_internal_loop(loop_id, team_id)` soft-deletes it and pauses its schedule when the job is done.--These skip user and visibility checks by design: there is no owning end user, and the caller is trusted server code.--## User-visible loops--If a person should see and manage the automation, omit `internal` (defaults to `False`) and pass a real owning `user`.-It then shows up in that user's loop list and is managed through the normal API (`update_loop` / `soft_delete_loop`).-The PR-follow-up example above works the same way, just drop `internal` / `origin_product` and it becomes the owner's loop.--## From anywhere else--- REST: `POST /api/projects/:team_id/loops/` with the same payload, from any key with `loop:write` scope (PSAKs included).-- MCP: the `loops-create` tool, so agents can schedule their own follow-ups.--Both of these paths are user-facing (`internal=False`); the `internal` flag is backend-only.--## Gotchas--- Clean up one-time loops. The Temporal schedule exhausts itself after the single fire, but the `Loop` row stays. Call `delete_internal_loop` (or `soft_delete_loop` for a user loop) once the follow-up lands, or reconcile with `list_internal_loops(origin_product=...)`.-- One repo per loop for now (`MAX_LOOP_REPOSITORIES = 1`).-- Per-fire context is baked into `instructions` at creation time for schedule triggers, so put the report and PR specifics in the prompt.-- If you'd rather trigger on merge directly, `github` triggers support `pull_request` with an `actions: ["closed"]` filter, but "merged vs abandoned" lives in `payload.pull_request.merged`, which filters don't inspect. Doing the check in your own webhook code is cleaner.diff --git a/products/tasks/docs/LOOPS.md b/products/tasks/docs/LOOPS.mddeleted file mode 100644index 2c90018a277c..000000000000--- a/products/tasks/docs/LOOPS.md+++ /dev/null@@ -1,382 +0,0 @@-# Loops--Status: draft spec (v1), revised after pressure testing and a codebase fact-check round-Backend: this repo, `products/tasks/`-Frontend: PostHog Code monorepo (`PostHog/code`), desktop + mobile-Execution: cloud only. Local scheduled execution is out of scope for now.--## Summary--A Loop is a named, cloud-executed agent automation.-The user writes instructions once, picks a model, attaches triggers and the loop runs in a sandbox on our existing tasks pipeline whenever a trigger fires.-A loop is personal or team-visible, chosen by its creator.-Loops talk in product scope, not repo scope: one loop can operate across multiple repositories in a single run.-Loops connect to the outside world through MCP connectors, control their own PR behavior (open PRs, watch CI, fix review comments) and notify the user through push, email or Slack.-Loops can be created from the UI, the REST API or by natural language through the remote PostHog MCP.-Loops are stateless: each run starts fresh, with no carryover of previous output.--Prior art: Claude Code cloud "routines" (schedule / GitHub event / API triggers) and ChatGPT Codex scheduled tasks (title, prompt, repeats, model).--## Goals--- Named loops with instructions (prompt), model selection (adapter + model + reasoning effort) and enable/pause.-- Personal or team visibility per loop, chosen by the creator.-- Triggers: cron schedule, one-time future run, GitHub webhook events (repository selected per trigger) and authenticated POST from user code.-- Multiple repositories per loop, coordinated in one agent session.-- MCP connectors per loop (Slack, email, Linear, anything in the MCP Store) plus scoped PostHog MCP access.-- Behaviors: open PRs, watch CI and review comments on loop-created PRs, auto-fix.-- Per-loop notification config: push, email, Slack; per-channel event filters.-- Auditable: activity log on every config change, per-run config snapshot.-- Creatable via API and via MCP tools (natural language from any chat surface).--## Non-goals (for now)--- Local execution of scheduled loops.-- Auto-merge of loop-created PRs.-- Cross-team or org-level loops.-- Memory between runs. A loop that needs "since last time" derives it through its connectors.-- A visual workflow builder. A loop is one prompt plus config, not a DAG (see `agent_platform` for that direction).--## What we build on (already exists)--| Capability | Where | State |-| ------------------------------------------------------- | -------------------------------------------------------------------------------------------------------------- | ----------------------------------------------------------------------------------------------------------------------------------- |-| Cloud agent execution (sandbox, Claude Code / Codex) | `products/tasks/backend/temporal/process_task/`, Modal sandbox, `@posthog/agent` agent-server | Production |-| Model / adapter / reasoning effort per run | `TaskRun.state` via `RunState`, env passthrough to agent-server | Production, just not exposed on automations |-| Cron scheduling via Temporal Schedules | `automation_service.py`, `run-task-automation` workflow | Production (TaskAutomation). No one-time runs; schedule policy left at SDK defaults |-| CI + review-comment follow-up loop | `process_task` workflow: `pr_loop_enabled`, `get_pr_context`, `send_followup_to_sandbox`, `MAX_CI_REPETITIONS` | Production. Iteration count is in-workflow state only, it does not survive the workflow |-| GitHub App integration, repo enumeration, token minting | `posthog/models/integration.py`, `github_integration_base.py` | Production |-| Inbound GitHub webhook (HMAC verified, single endpoint) | `posthog/urls.py::github_webhook` | Production, hardcoded 3-way dispatch, no shared delivery dedup (only the conversations consumer dedups, with its own Redis pattern) |-| MCP connectors injected into sandbox runs | `mcp_store` installations + `get_user_mcp_server_configs` / `get_sandbox_ph_mcp_configs` | Production |-| Sandbox secrets + network policy | `SandboxEnvironment` (encrypted env vars, domain allowlist), resolved per run via `TaskRun.state` | Production |-| Sandbox snapshots + warming | `SandboxSnapshot`, `create_snapshot` workflow | Production. Snapshot storage supports N repos, the warming workflow itself is single-repo |-| Push notifications (Expo, device tokens) | `posthog/push_notifications.py`, `products/tasks/backend/push_dispatcher.py` | Production |-| Email (Customer.io / SMTP) | `posthog/email.py` | Production |-| Slack send | `SlackIntegration(...).client.chat_postMessage` | Production |-| In-app notifications | `products/notifications/backend/facade/api.py::create_notification` | Production |-| MCP tool codegen from OpenAPI | `products/tasks/mcp/tools.yaml` + `hogli build:openapi` | `task-automations-*` scaffolded, all disabled |-| Project secret API keys (phs\_) for service auth | PSAK infra (`adding-project-secret-api-key-auth`) | Production, read-only scopes only so far |--What TaskAutomation (PR #52752) lacks that Loops adds: trigger types beyond cron, model pinning, multi-repo, connectors config, behavior config, notification config, visibility model, secrets, activity logging, concurrency control and write-scoped PostHog MCP.--## Primitives--### Loop--The top-level object. Team-scoped, owned by a user, soft-deletable.--- `name`, `description`-- `visibility`: `personal` (default) | `team`. Personal loops are visible and controllable only by their owner. Team loops are visible to the whole team; edit semantics are in Access control.-- `owner` (`created_by`): the execution identity for GitHub authorship, OAuth token minting and MCP resolution.-- `instructions`: the prompt delivered to the agent on every run-- `runtime_adapter` (`claude` | `codex`), `model`, `reasoning_effort`: validated against the existing catalog in `process_task/utils.py`-- `repositories`: ordered list of `{github_integration_id, full_name}`, max 5; may be empty (report-only loops that work purely through connectors, e.g. a daily brief). Validated to length 1 until Phase 5 ships multi-repo execution.-- `sandbox_environment`: optional FK to `SandboxEnvironment`, carrying encrypted env vars and the network allowlist into every run. This is the secrets story for loops that call non-MCP endpoints (e.g. a staging API); the per-task mechanism already exists, loops just get a handle to it.-- `enabled`: pausing disables all triggers-- `overlap_policy`: `skip` (default) | `allow` | `cancel_previous`; applies when a trigger fires while a run is active-- `behaviors`: JSON, validated: `{create_prs: bool, watch_ci: bool, fix_review_comments: bool, max_fix_iterations: int}`. `max_fix_iterations` is server-clamped (ceiling 10), it is not a free integer.-- `connectors`: JSON: list of MCP Store installation ids + `posthog_mcp_scopes` (`read_only` default, `full` opt-in)-- `notifications`: JSON, validated: per channel (`push`, `email`, `slack`) an `enabled` flag, an event filter (`run_completed`, `run_failed`, `pr_created`, `needs_attention`) and channel params (Slack: `integration_id` + `channel`)-- bookkeeping: `last_run_at`, `last_run_status`, `last_error`, `consecutive_failures`--JSON config fields are validated at the API edge, and the facade DTO parsers coerce defensively so a malformed stored shape (from a backfill or facade-bypass write) can never crash a read.--### LoopTrigger--A loop has many triggers. Each is independently enable/disable-able.--- Carries its own `team` FK (denormalized off `loop.team`): the fail-closed manager filters on a local column, not through an FK, so team scoping must be structural, not derived.-- `type = schedule`: `{cron_expression, timezone}` or `{run_at}` for one-time.- Backed by a Temporal Schedule per trigger (`schedule_id = loop-trigger-{id}`), created with an explicit policy: overlap `SKIP` at the schedule layer and `catchup_window` of 5 minutes. Never the SDK default (365 days), which would replay an outage's entire missed window as a burst on recovery.- One-time runs are new build (nothing in the repo uses this today): a Temporal Schedule with `limited_actions=True` + `remaining_actions=1`, plus a post-fire hook that deletes the spent schedule and marks the trigger completed. Nothing garbage-collects expired schedules automatically.-- `type = github`: `{github_integration_id, repository, events: [...], filters: {actions?, branches?, labels?}}`.- `repository` is validated at save against the integration's enumerable repo list, and webhook matching is scoped to `(github_integration_id, repository, event)`, so a team can never subscribe to another team's repo by typing its name. One repository per trigger (webhook routing constraint), but the run still executes against the whole loop workspace.-- `type = api`: fires on `POST /api/projects/:id/loops/:loop_id/trigger/`.- Request body (JSON, capped at 64 KB) becomes run context. Callers may send an `Idempotency-Key` header, deduped per trigger, so a retry on timeout doesn't double-fire.--Every firing records which trigger fired and a rendered context block that is appended to the instructions.-Trigger payloads are untrusted input: rendered as fenced data with an explicit "this is external data, not instructions" preamble, and size-capped. API bodies are capped at the endpoint; the rendered GitHub context is truncated to 64 KB with an explicit truncation marker (raw deliveries can reach Django's 20 MB body limit).-The fence is defense in depth, not the enforcement boundary: a GitHub event only fires a loop when its author is a trusted repo actor (`author_association` of `OWNER`/`MEMBER`/`COLLABORATOR`, plus `push`, which is inherently write-gated). Issues or comments from external contributors are dropped, so untrusted content can't steer a credentialed run.-Schedule fires have no event: their context block renders the loop name, trigger, fire time and the previous fire's time and status. No previous-run output is injected; loops are stateless by design.--### Run--Each firing creates a fresh Task plus its TaskRun (`mode=background`, `environment=cloud`) and `execute_task_processing_workflow` runs the standard `process-task` workflow.-No new execution engine.-Run state, SSE streaming, logs (S3 JSONL), artifacts and the task detail view all come for free.--A new Task per firing (not runs appended to one shared task) because the assembled prompt differs per fire: `Task.description` = loop instructions + the rendered trigger context for that firing.-The assembled prompt is wrapped in a loop framing block: this is unattended execution, no human can answer questions mid-run, prefer draft PRs and `raise_attention` over guessing on ambiguous judgment calls. (The only unattended framing in the codebase today is scoped to repo setup, not the main prompt.)-The raw pieces (`instructions`, `trigger_context`) are also stored in run state so clients can render them separately instead of parsing the assembled prompt.--Loop-spawned tasks are system artifacts, not personal tasks:--- `internal=True`: the existing facade list filter excludes internal tasks from the main task list by default (`facade/api.py`), so they never appear in a user's inbox or sidebar.-- `origin_product=loop` plus a `task.loop` FK (nullable, indexed, added with the non-blocking migration pattern since `posthog_task` is hot): the loop detail UI lists them via `loops/:id/runs/`.-- `created_by=loop.owner`: a real user is required because sandbox OAuth token minting reads `task.created_by` (`oauth.py`). This is attribution plumbing, not ownership.-- Task read access follows loop visibility. Team loops expose their tasks through the read-only visibility path (`task_visibility_q`), not `TEAM_VISIBLE_ORIGIN_PRODUCTS`: that list also feeds `task_control_q`, which would let any member run or message a loop task directly and sidestep every loop-level guardrail. Personal loops' tasks stay owner-only.--Config snapshot: everything a run uses is snapshotted into run state at fire time: `repositories`, `runtime_adapter` / `model` / `reasoning_effort`, `behaviors`, resolved connector installation ids + `posthog_mcp_scopes` and `notifications`, plus `loop_id`, `loop_trigger_id` and `trigger_context`.-One rule, no exceptions: editing a loop never affects in-flight or queued runs, and every run is self-describing for audit.--Idempotency: a fire-record row unique on `(loop_trigger_id, fire_key)`, enforced by a DB constraint.-The fire key is the Temporal workflow id for schedule fires, the `X-GitHub-Delivery` GUID for webhook fires and the client `Idempotency-Key` (or a generated one) for API and manual fires.-Scoping by trigger matters: one GitHub delivery legitimately fires many loops, so the GUID alone is not a valid dedup key.--Overlap: a transaction-scoped Postgres advisory lock keyed on the loop id wraps the check-and-create; "active" means a TaskRun in any non-terminal status. `skip` drops the fire and records it; `cancel_previous` cancels the active run, then creates.--Rate: atomic Redis counters. Per-loop 100 runs/day (default), a per-team aggregate cap (default 500/day) and a per-team concurrent-run bound, since `overlap_policy` only governs one loop against itself and does nothing for N loops (or N distinct events on one loop) firing in a tight window. Cap-exhausted fires are dropped, recorded and flip `needs_attention`.--Cleanup: a Celery beat sweep (registered alongside the existing tasks cleanup in `posthog/tasks/scheduled.py`) soft-deletes loop tasks beyond the latest 200 per loop. Tasks with non-terminal runs are never deletion candidates regardless of recency.--### Workspace (multi-repo)--Target design: one sandbox, N repos.--- `Task.repository` (single string) grows a sibling `Task.repositories` (list, nullable, no backfill; the single field stays the source of truth until Phase 5). Existing single-repo behavior unchanged.-- Sandbox provisioning clones every repo into the existing layout `/tmp/workspace/repos/{org}/{repo}` (the layout and `SandboxSnapshot.repos` already support N).-- agent-server (in `PostHog/code`, `packages/agent`) accepts a workspace root + repo manifest instead of a single `--repositoryPath`. This is a wire-contract change with real blast radius: the sandbox image installs `@posthog/agent@latest`, so the rollout pins the agent version for the window, agent-server accepts both invocation contracts for at least one release and multi-repo exposure is feature-flagged on a confirmed minimum agent-server version.-- Branch naming is shared across repos: `loop/{loop-slug}/{run-shortid}`.-- The agent opens one PR per repo it actually touched; each lands in `TaskRun.output.prs[]` (superset of today's `output.pr_url`). The webhook backstop's matcher today is a scalar equality on `output.pr_url` and is adapted to array membership.-- PR reuse: before opening, check for an existing open PR from the same loop against the same repo and update it instead of stacking a new competing PR per fire.-- Partial failure is best-effort, stated: per-repo status recorded in `output.prs[]` (`opened`, `push_failed`, `skipped`), any failed repo flips `needs_attention`, branch and PR creation are idempotent on retry. No cross-repo rollback. `cancel_previous` mid-push records the same partial state through the same path.-- CI watch (`get_pr_context`) iterates all open PRs of the run; fix sessions are scoped to the failing repos only.--Snapshot warming: `SandboxSnapshot.repos` and its matching logic support N repos, but the warming workflow (`create_snapshot`) takes a single repository today. Phase 5 adds a repo-set variant.--### Connectors (MCP)--Reuse both existing mechanisms, no new connector model:--- MCP Store installations (`MCPServerInstallation`, team + user scoped): validated against `get_active_installations(team_id, loop.owner_id)` on save and snapshotted into run state at fire time. At run boot they resolve to proxy URLs exactly like interactive runs do today.-- PostHog MCP: injected as today, with scopes from the run's snapshotted `posthog_mcp_scopes`. This fixes the current automation default of hardcoded `read_only` by making it explicit and configurable.-- If run-boot resolution fails because an installation was uninstalled since the fire, the run fails with a distinct `connector_unavailable` error and a `needs_attention` notification, not a generic agent failure.-- Loop CRUD MCP tools are blocked inside loop-fired runs regardless of scope. A triggered run has no legitimate reason to create loops, and this closes the injected-instructions-plant-a-persistent-loop path.--Slack and email as loop outputs (e.g. "post the summary to #standup") are just MCP connectors from the agent's point of view. Notification delivery (below) is separate, deterministic plumbing.--### Behaviors--- `create_prs=false`: report-only loop; sandbox still clones repos (read access) but the signed-commit tool and PR flow are disabled.-- `watch_ci` / `fix_review_comments`: enables the existing in-workflow CI follow-up loop for loop runs, bounded by `max_fix_iterations`.-- CI failures caused by policy gates (branch protection, missing required review) are not treated as fixable: they surface as `needs_attention` instead of burning fix iterations against a wall.-- Phase 2 of babysitting: after the run's workflow ends, `pull_request` / review-comment webhooks that match a loop-created open PR (via `output.prs[]`) spawn a follow-up run on the same loop with the PR context. The iteration bound is persisted, not asserted: `TaskRun.origin_run_id` chains follow-ups to their parent and `TaskRun.fix_iteration` (parent + 1) is checked against `max_fix_iterations` before a follow-up is spawned. The cap holds across workflow boundaries, not just inside one execution.-- Self-trigger exclusion: deliveries authored by the loop's own identity or on `loop/`-prefixed branches never match that loop's own triggers, and a commit that already spawned a phase-2 follow-up is excluded from `push`-trigger matching. An auto-fix commit cannot re-fire its own loop or double-spawn through both paths.--### Notifications--A new dispatcher module `products/tasks/backend/loop_notifications.py`, modeled on `push_dispatcher.py`:--- push: `send_user_push.delay(...)` to the loop owner (Expo tokens already registered by the mobile app)-- email: `EmailMessage` with a new `loop_run_summary` template. Three steps, two in code: a `CUSTOMER_IO_TEMPLATE_ID_MAP` entry, a Django template file under `posthog/templates/email/` (required even for HTTP sends) and the Customer.io dashboard setup.-- Slack: `chat_postMessage` to the configured integration + channel. Permanent failures (`channel_not_found`, `is_archived`, `account_inactive`) auto-disable that channel on the loop and say so in-app, rather than logging and swallowing forever.-- in-app: `create_notification(...)` always, cheap and useful--Hooks: run terminal status and PR events from the webhook handler. Loops get their own terminal-status hook: `update_automation_run_result` is not reusable as-is, it only fires on FAILED and CANCELLED (never COMPLETED) and only sets `last_error`, so `run_completed` would never dispatch through it.-Per-channel event filters from `loop.notifications`. A Redis cooldown per (loop, event kind) applies to failure and attention kinds only; `run_completed` and `pr_created` are never cooldown-dropped. In-app is always on and needs no per-channel opt-in, but failure/attention kinds respect the cooldown there too, so a capped or crash-looping loop can't flood the notification table.--`needs_attention`, defined. Raised by the system on: usage-gate rejection, `connector_unavailable`, auto-pause, partial multi-repo failure, policy-gated CI, permanent notification-channel failure and cap-exhausted fires. Raised by the agent through a `raise_attention` tool exposed in loop runs.--### Contexts--A loop can be attached to a context (a "#channel" / desktop folder), stored on `Loop.context_target`: `{folder_id, name, outputs: {post_to_feed, update_context, canvas_id}}`, or `{}` when unattached.-Attachment is identity-bearing (owner-only to change on a team loop) and drives three independent outputs, each a toggle:--- `post_to_feed`: each run's task is filed into the context's feed (`Task.channel`, resolved from the context name at fire time), so the run shows up as a card alongside interactive tasks. No new feed machinery, just the channel the run's task already supports.-- `update_context`: each run reads the context's `context.md` and republishes it, reflecting the latest state. Reuses the existing `desktop-file-system-instructions-retrieve` / `-partial-update` MCP tools (the same contract the "Build with agent" flow uses); the run does a read-modify-write since there is no server-side append.-- `canvas_id`: the loop maintains one canvas (a living dashboard) in the context, rewriting the complete single-file React source each run via `desktop-file-system-canvas-partial-update`.--The publish contract (folder id, canvas id, which tool to call) is appended to the run's prompt only when a write output is on; a feed-only attachment needs no prompt change.-Write outputs widen the run's PostHog MCP scopes by exactly `file_system:read` + `file_system:write` on top of whatever the loop already carries, rather than promoting it to the broad `full` write surface.-A feed-only attachment grants no extra scope.-`folder_id` and `canvas_id` are validated against the team's desktop file system on write; the resolved feed channel is always team-scoped, so no cross-team id can be attached.-A context-attached loop must have `team` visibility: its runs are filed into the context's public feed channel (team-readable regardless of loop visibility) and maintain team-shared artifacts, so `personal` would leak the loop's output while hiding the loop itself. Enforced on the effective post-write state in the facade (`create_loop` / `update_loop`), surfaced as a 400; detaching and downgrading in the same PATCH is allowed.--#### How context outputs are delivered--Nothing a context owns is local to anyone's machine.-Contexts, `context.md` and canvases are all rows on the cloud `desktop_file_system` surface (`/api/projects/:team_id/desktop_file_system/`); the desktop app is just another API client of them, and a sandboxed run reaches the same rows through the PostHog MCP `desktop-file-system-*` tools, so a loop writes them through the exact path the app does.--The wiring lives in `products/tasks/backend/logic/services/loop_runs.py`:--- Prompt: when a write output is on, `render_context_target_block` appends a publish contract to the run's prompt. For `context.md` the agent reads with `desktop-file-system-instructions-retrieve` (id: folder id), revises, then publishes the full markdown with `desktop-file-system-instructions-partial-update` (id: folder id, `base_version`: the version it just read), a read-modify-write with optimistic concurrency (same contract as the desktop "Build with agent" flow). For a canvas it republishes the complete single-file React source with `desktop-file-system-canvas-partial-update` (id: canvas id), whole file each time.-- Feed: `post_to_feed` needs no prompt. The run's `Task` is created with `channel_id` resolved from the context name (`_resolve_feed_channel_id`), so the card appears regardless of what the agent does.-- Scopes: `_augment_scopes_for_context` widens the run's MCP scopes by exactly `file_system:read` + `file_system:write` (defined in `services/mcp/definitions/core.yaml`), never to `full`; a feed-only attachment grants nothing extra.-- Guardrails: `folder_id` and `canvas_id` are validated against the team's desktop file system on write, context-attached loops must be `team` visibility, and the loop only ever updates an existing canvas, never creates one.--`tests/test_loop_runs.py` covers which tools and scopes each output combination gets. The client schema and form are `packages/api-client/src/loops.ts` and `packages/ui/src/features/loops/components/LoopContextFields.tsx` in the PostHog Code repo.--## Access control--- Personal loops: owner-only for everything (view, edit, fire, run history).-- Team loops: every member can view config and run history. Identity-bearing config (instructions, repositories, connectors, behaviors, triggers) is editable only by the owner; another member editing these first takes ownership explicitly, which re-validates repositories and connectors against their own GitHub and MCP access. Non-identity fields (name, description, notifications, enable/pause) are editable by any member. Any member can manually fire a team loop; fires are attributed in the activity log.-- Removing a team loop from the team is separated from ownership so a takeover can't double as a way to steal or destroy a shared automation: un-sharing it (visibility team to personal) and deleting it require the loop's original `creator` (an immutable field, distinct from the takeover-mutable `created_by`) or a project admin. A member who takes a team loop over gains edit rights but not the power to privatize or delete it.-- Project admins can pause or delete any loop regardless of visibility (the kill switch when an owner is unavailable).-- Rationale: a loop executes as its owner. Letting someone else edit its instructions without takeover is acting as the owner.-- `scope_object = "loop"`: a new `APIScopeObject` (update `posthog/scopes.py`, the frontend scope lists and the MCP OAuth scope codegen). Deliberately not `task`: reusing `task` would retroactively grant every existing `task:write` credential the power to create automations that run arbitrary prompts as the key's owner.-- Loop is not added to `ACCESS_CONTROL_RESOURCES` in v1 (no dedicated RBAC UI); the visibility model is the primary restriction mechanism. `AccessControl` rows with `resource="loop"` are still creatable through the RBAC API, and when they exist the facade enforces them at object level (viewer to read, editor to write/fire — `_rbac_denied` / `_rbac_filter_visible`), since the viewset's facade pattern never runs DRF's `check_object_permissions`. Owners keep access to their own loops via the RBAC creator precheck.-- Creating a loop through the MCP tool surface is a confirmed action (`loops-create-prepare` / `loops-create-execute`): the expected confirmation is human (the review card's "Create loop" button or a typed 'confirm' in chat), but the model relays the confirmation argument, so this is deliberate friction rather than a hard guarantee — a steered agent can still self-confirm. A model-unreachable approval channel is a tracked product decision (LOOPS-FOLLOWUPS.md "User-bound approval for MCP loop creation").--## Activity log and audit--- `Loop` joins `ActivityScope`. Create, update, delete and trigger changes log with before/after diffs (`log_activity_from_viewset`, the HogFunction pattern: user-authored config with external side effects). Bookkeeping fields (`last_run_at`, `last_run_status`, `consecutive_failures`) are excluded so only meaningful edits log.-- Ownership takeovers and manual fires are logged.-- Exposed through the standard activity_log endpoint scoped to `Loop`; the loop detail UI in `PostHog/code` gets a history tab (the `ActivityLog` component lives in this repo's frontend and cannot be imported there, so that tab is a build, not a reuse).-- The per-run config snapshot (see Run) plus the activity log make any historical run attributable to the exact config and the edit that produced it.--## Lifecycle and reconciliation--Postgres rows and Temporal Schedules must not drift; the plan states the mechanism instead of assuming the happy path:--- Row first, schedule after commit. DB writes commit inside `transaction.atomic()`, the Temporal call runs after the block (repo convention: no side effects inside atomic blocks). If the Temporal call fails, the trigger is marked `schedule_sync_failed` and surfaced in the API response; today's `automation_service.py` has no handling here and would 500 with a committed row.-- A periodic reconciliation sweep diffs enabled LoopTrigger rows against live Temporal Schedules and repairs both directions: creates missing schedules, deletes orphans. This backstops every path that bypasses the CRUD facade.-- Loop soft-delete pauses every trigger's schedule; restore unpauses, or recreates if the schedule is gone. Hard-deletion paths that bypass the facade entirely (team deletion cascade, bulk Integration deletes) are covered by an explicit cleanup step in team deletion plus the sweep. Django's cascade collector never talks to Temporal on its own.-- Nested trigger writes match by id: update in place (preserving id and `schedule_id`), create only genuinely new entries, delete only rows explicitly absent from a full triggers payload; partial updates never delete. The Alert serializer's delete-then-recreate precedent is unsafe here because schedule identity hangs off the row PK.-- GitHub App uninstall: the `installation` webhook hard-deletes the Integration row today with zero downstream hooks, and loop references to it are JSON, so no FK machinery helps. The handler additionally auto-pauses every loop referencing that integration in `repositories` or triggers and fires `needs_attention` naming the disconnected integration.-- Owner deactivation: loops pause immediately (not lazily at next fire) and in-flight runs of that owner's loops are cancelled. Deactivation is often the security response; it must not leave a live sandbox holding freshly minted credentials.--## Data model (Django, `products/tasks/backend/models.py`)--```text-Loop team FK, owner FK, name, description, visibility,- instructions, runtime_adapter, model, reasoning_effort,- repositories JSON, sandbox_environment FK (nullable),- enabled, overlap_policy,- behaviors JSON, connectors JSON, notifications JSON,- context_target JSON (context attachment + outputs, {} when unattached),- internal, origin_product,- last_run_at, last_run_status, last_error,- consecutive_failures, created_at, updated_at, deleted--LoopTrigger team FK, loop FK, type (schedule|github|api), enabled,- config JSON (validated per type),- schedule_sync_status, last_fired_at, created_at, updated_at--LoopFire team FK, loop_trigger FK, fire_key,- unique(loop_trigger, fire_key)-```--Both `Loop` and `LoopTrigger` are team-scoped with the fail-closed manager; `LoopTrigger` carries its own `team` column because the manager filters on a local field.-JSON config fields are validated by serializers/pydantic, not free-form.-Task changes: `loop` FK (nullable, indexed), `repositories` list (nullable sibling of the legacy single `repository`), a `loop` value on `OriginProduct` and `internal=True` on loop-spawned rows.-TaskRun changes: `origin_run_id` (nullable self-FK) and `fix_iteration` for the babysitting chain.-`TaskAutomation` is deleted, not migrated (see Takeover).--## API surface (DRF, `/api/projects/:team_id/loops/`)--`scope_object = "loop"` on the viewset.--- `loops/` CRUD (list, retrieve, create, partial_update, destroy)-- `loops/:id/run/` manual fire (session auth), returns the created run-- `loops/:id/trigger/` external fire (PSAK auth), body becomes run context-- `loops/:id/runs/` run history. Cursor-paginated: this is a continuously appended feed across N tasks, and offset pagination skips or duplicates rows under insertion. Needs a new loop-scoped run query; the existing `list_task_runs` is single-task.-- `loops/:id/preview/` dry run: renders the assembled instructions + trigger context for a supplied sample payload (or a synthetic schedule fire) without creating a task, run or side effects. This is how a user verifies a GitHub filter or API payload shape before a `create_prs=true` loop fires for real.-- activity via the standard activity_log endpoint, `scope=Loop`-- triggers managed inline on the loop serializer (nested create/update, id-stable semantics per Lifecycle)--All serializers carry `help_text` and schema annotations so the generated OpenAPI feeds three consumers: the PostHog Code api-client, MCP tools and docs.-Follow `/improving-drf-endpoints` and regenerate with `hogli build:openapi`.--### API trigger auth--Decision: project secret API keys (`phs_...`) with a `loop:write` scope.-The scope grammar is `object:read|write` only (`APIScopeActions`), so a literal `loop:trigger` scope is invalid; instead the `trigger` DRF action maps into the write bucket and `("loop", "write")` joins `PROJECT_SECRET_API_KEY_ALLOWED_API_SCOPE_ACTION`.-This is the first write-capable PSAK scope (the current allowlist is `endpoint:read` and `feature_flag:read`) and PSAKs are project-wide: one leaked key can fire any loop in the project with a chosen payload. That blast radius is accepted and documented; optional per-loop key binding is an open hardening question.-The project-wide bypass is exclusive to the PSAK service credential.-A non-PSAK caller (session, PAT or OAuth) hitting the same endpoint is held to the personal/team visibility split — it can only fire loops it can see, so a teammate cannot fire another member's personal loop by UUID.-Revocable, project-scoped, user-less, throttled per key.-Alternative considered and rejected: per-trigger bespoke secrets (more objects to manage, new auth code path).--## GitHub event triggers: infrastructure changes--1. Registry refactor ships first as a behavior-preserving no-op PR: `posthog/urls.py::github_webhook` goes from the hardcoded 3-way `if/elif` to `event_type -> [handlers]` with per-handler exception isolation (one consumer's failure never drops another's delivery). Signature verification and JSON parse stay shared, done once. Existing consumers (conversations, tasks PR backstop, installation lifecycle) re-register unchanged; the Loops handler lands as a separate additive PR.-2. Delivery dedup is computed once at the dispatcher: Redis-backed, keyed on `X-GitHub-Delivery`, TTL bounded to GitHub's redelivery window, fail-open if Redis is unavailable. This is shared new build, not reuse: today only the conversations consumer dedups (with its own Redis pattern), the tasks PR consumer does not. Loop-level dedup additionally scopes by trigger (see Run) since one delivery fans out to many loops.-3. Fan-out is new behavior: both existing consumers deliberately resolve an installation id to a single team (`.first()` / settings-claimed); their resolution is untouched. The Loops handler resolves the installation to all its teams, then matches enabled `LoopTrigger(type=github)` rows on indexed `(integration, repository, event)` columns first, JSON `filters` evaluated last.-4. Ops prerequisite, blocking: the GitHub App's subscribed event list is an app-level setting on github.com and today effectively covers `installation`, `issues`, `issue_comment`, `pull_request`. The App subscription update is a hard precondition for Phase 3 enablement, and the v1 event allowlist is enforced server-side: a trigger for an unsubscribed event is rejected at save, not silently never-firing. v1 events: what the App already receives plus `push`.-5. Observability: a structured log and a bounded-cardinality outcome counter (`matched`, `deduped`, `overlap_skipped`, `gate_blocked`, `cap_dropped`, `fired`) at the matching decision point, keyed by delivery GUID and trigger id. "Why didn't my loop fire" must be answerable; the current webhook handler logs nothing on any path.--## Natural language creation (MCP)--No local MCP server needed. The remote PostHog MCP is the right surface:--- `products/tasks/mcp/tools.yaml` already scaffolds automation tools (all `enabled: false`). Once the loops API exists, expose `loops-list`, `loops-retrieve`, `loops-create`, `loops-partial-update`, `loops-destroy`, `loops-run-create` with proper scopes and descriptions, then `hogli build:openapi`.-- Every PostHog Code cloud session already gets the PostHog MCP injected, so "make me a loop that posts failing CI summaries to Slack every morning" works from a task chat, from Claude, from anything speaking MCP.-- Loop CRUD tools are blocked from within loop-fired runs regardless of scope (see Connectors). Interactive sessions are the NL-creation surface.--## Frontend (PostHog Code repo)--All UI lives in `PostHog/code`. Nothing is added to posthog `frontend/`.--Desktop (`apps/code` + `packages/ui`):--- New top-level "Loops" sidebar item (peer of Tasks/Agents), with list (split by personal / team visibility), detail (config + run history + activity history tab) and a create/edit form.-- Reuse: prompt composer patterns from `task-detail`, model selection from `AgentModelConfig` / `cloudRunOptions`, repo picker (extended to multi-select), MCP attach via `useMcpConnect` + `AddCustomServerDialog` + `ToolPermissionList`, Slack channel picker from existing settings sections, `NotificationBus` for local surfacing of loop events.-- Preview flow: the trigger editor exercises `loops/:id/preview/` before a loop is enabled.-- Consume the generated `packages/api-client` types (note: `apps/code/scripts/update-openapi-client.ts` has a stale `OUTPUT_PATH`; fix when regenerating). The cross-repo contract has no automated sync: the code repo regenerates manually against `/api/schema/`, so document the regeneration trigger and add a contract check before renaming or deleting operations. The TaskAutomation removal is exactly such a rename.-- Trigger editor mirrors the Claude Code pattern: Schedule / GitHub event (repo select first) / API (shows endpoint + key instructions).--Mobile (`apps/mobile`):--- Evolve the existing automation screens into Loops: keep `ScheduleEditor` and the form skeleton, add trigger list, model picker, connectors and notification toggles.-- Push notification plumbing already done (token registration, deep links).--## Security and guardrails--- Loop runs execute as the owner for GitHub authorship and MCP identity; the visibility/takeover model above governs who can point that identity at new instructions. Project admins always have the pause/delete kill switch.-- Cloud usage limit gate (same check as `TaskViewSet.run`) applies to every fire, with teeth for unattended fires: gate rejections count toward `consecutive_failures` and emit `needs_attention` (nobody is watching a synchronous 429 on a cron fire). The gate client fails open on gateway errors with only a log line today, so an outcome counter (`checked_allowed` / `checked_blocked` / `fail_open`) is added and monitored; a degraded gateway must not silently remove the only cost backstop.-- Cost attribution: run identifiers (task, run, loop) are passed as metadata to the LLM gateway, or the usage figures the agent-server returns are recorded on `TaskRun.output` tagged with `loop_id`. Today metering is keyed only on (user, team), which makes a runaway loop indistinguishable from its owner's interactive usage even after the fact.-- Per-loop rate limits: 100 runs/day default, per-team aggregate cap, per-team concurrency bound, API trigger throttled per key, webhook fires deduped by delivery GUID per trigger. GitHub webhook events are throttled per (installation, repository) ahead of matching, and capped fire attempts write no `LoopFire` rows, so a sustained stream of unique deliveries can't grow the fire or notification tables.-- Auto-pause after N consecutive failures (default 5, gate rejections included) with a notification. Loops that exhaust `max_fix_iterations` on several consecutive runs flag `needs_attention` even when each run reports success; expensive-but-green is also a failure mode.-- Trigger payloads and webhook bodies are data, not instructions: rendered fenced, size-capped, never interpolated into system context. Fencing is not treated as sufficient on its own: externally-triggered loops (`github`, `api`) with `posthog_mcp_scopes=full` or write-capable connectors require explicit confirmation at save, and loop CRUD is blocked in-run.-- Sandbox egress and network policy come from `SandboxEnvironment` per run; git-guard signed commits are baked into the sandbox image unconditionally and apply to loop runs as-is.--## Instrumentation and operations--- `TaskRun.capture_event` gains `loop_id` and `loop_trigger_id` standard properties (the `signal_report_id` pattern), so per-loop analytics need no Postgres join. `INSTRUMENTATION.md` is updated in the same PR.-- Model events: `loop_created`, `loop_trigger_fired` (with trigger type), `loop_auto_paused`, `loop_needs_attention`.-- Prometheus: auto-pause counter, trigger-fire outcome counter (GitHub section above), usage-gate outcome counter. Bounded cardinality, following the wizard-run metrics convention.-- Django admin: `LoopAdmin` and `LoopTriggerAdmin` with read-only operational fields (`enabled`, `last_fired_at`, `schedule_sync_status`), following `TaskAdmin` / `TaskRunAdmin`. On-call should never need raw SQL to answer "is this loop's schedule in sync".--## Takeover of TaskAutomation--Status, verified: the model, viewset (registered under the projects router) and facade all exist and are reachable, but every `task-automations-*` MCP tool is disabled and there are zero non-generated call sites in this repo. Confirm the production row count is ~0 before dropping; if real rows exist, this section gets revisited.--1. Retirement follows the `/django-migrations` two-phase pattern (code removal, then schema drop), not a single-step delete. The mobile automation screens are feature-flagged off a release before the API is removed, so pinned builds degrade instead of hitting hard 404s.-2. Reuse the worthwhile parts under new names: `automation_service.py` becomes `loop_service.py` (Temporal Schedule create/update/pause/delete plus the run-now path, now with explicit policy and sync-failure handling), the `run-task-automation` workflow becomes `run-loop` and its per-fire idempotency pattern carries over per trigger.-3. Stray `task-automation-*` Temporal Schedules are removed by a named, idempotent management command that runs before the workflow-type rename deploys; a schedule referencing a renamed workflow type fires into nothing.-4. `origin_product=automation` stays in the enum for historical rows; new runs use `loop`.--Note: the tasks execution engine is mid-migration (`task_management` / `execute_sandbox` workflows exist but are unregistered; `process_task` is slated for deletion). Loops must integrate only through the facade entry points (`execute_task_processing_workflow`, run creation on the backing task), never against workflow internals, so the engine swap does not touch loops.--## Rollout and phasing--Gating: Loops sits behind its own feature flag layered on tasks access (`has_tasks_access`), with a `check-access`-style capability endpoint so higher-risk capabilities (GitHub triggers, full MCP scopes, multi-repo) can be enabled per stage independently of the base flag.--1. **Schema + API + safety floor**: `Loop`, `LoopTrigger` (own team FK), visibility model, `loop` scope, serializers, activity logging, config snapshots, model pinning plumbed into run state, task-per-firing with read-only visibility, schedule triggers with explicit policy, manual run, auto-pause, in-app notifications, Django admin, TaskAutomation retirement started. Auto-pause and in-app notifications belong in Phase 1: a failing cron loop must never be silent.-2. **API trigger + notification channels**: PSAK `loop:write` trigger endpoint, notification dispatcher (push, email, Slack), preview endpoint, reconciliation sweep.-3. **GitHub event triggers**: registry no-op refactor, then the Loops handler, delivery dedup, fan-out matching, server-side event allowlist, App event-subscription ops (blocking), trigger observability.-4. **MCP surface**: connectors config on the loop, scoped PostHog MCP, enable `loops-*` tools for NL creation, in-run loop CRUD block.-5. **Multi-repo workspace**: `Task.repositories`, multi-clone provisioning, repo-set snapshot warming, agent-server workspace manifest behind version pinning and a dual-contract release, per-repo PRs with reuse, CI watch across PRs.-6. **PR babysitting phase 2**: persisted chain counter (`origin_run_id` + `fix_iteration`), webhook-driven follow-up runs, self-trigger exclusion.--Desktop and mobile UI track phases 1-4 in the code repo in parallel.--## Decided--1. TaskAutomation is taken over: renamed/reused where worthwhile, retired via the standard two-phase pattern otherwise. No data migration, no compat shim (pending the row-count check).-2. Multi-repo: one sandbox, N repos, coordinated changes, one PR per touched repo, best-effort partial failure with per-repo status.-3. Each firing creates a fresh internal Task attached to the loop; loop tasks are hidden from personal task lists and surfaced through the loop UI, read-only for team members on team loops.-4. API triggers authenticate with project secret API keys, scope `loop:write` (the `trigger` action mapped into the write bucket).-5. The name stays "Loops" despite the existing loops.so warehouse source of the same name; accepted collision, revisit before GA. No code-level collision: the warehouse classes are `LoopsSource` / `LoopsSourceConfig`.-6. Visibility: `personal` | `team`, chosen by the creator. Identity-bearing edits on team loops require explicit ownership takeover with re-validation; admins can always pause/delete.-7. API scope is a new `loop` `APIScopeObject`, not a reuse of `task`.-8. Loops are stateless in v1: no output carryover between runs; schedule fires render fire-time metadata only.-9. Everything is snapshotted at fire time (repos, model, behaviors, connectors, notifications); edits never affect in-flight or queued runs.-10. Retention: keep the latest 200 terminal tasks per loop, swept by Celery beat, never touching non-terminal runs.--## Open questions--1. Which additional GitHub App event types to subscribe at launch (`push` is certain; `release`, `workflow_run`, `check_suite` on demand). Blocks Phase 3 only.-2. Ownership takeover UX on team loops: an explicit "take ownership" action (spec assumes this) vs an implicit prompt when a non-owner edits. Blocks Phase 1 UI only.-3. Whether PSAK keys should optionally bind to a single loop id, hardening beyond the project-wide `loop:write` blast radius. Blocks nothing; Phase 2 hardening.-4. Whether Loop should eventually join `ACCESS_CONTROL_RESOURCES` for role-scoped loops in RBAC orgs. Blocks nothing; post-v1.diff --git a/products/tasks/frontend/generated/api.schemas.ts b/products/tasks/frontend/generated/api.schemas.tsindex 522bf387ac2a..c6fd286b2937 100644--- a/products/tasks/frontend/generated/api.schemas.ts+++ b/products/tasks/frontend/generated/api.schemas.ts@@ -355,7 +355,7 @@ export interface LoopTriggerWriteApi {type: LoopTriggerTypeEnumApi/** Whether this trigger is active. Disabling pauses only this trigger. */enabled?: boolean- /** Trigger configuration, shape validated per `type`: schedule takes `{cron_expression, timezone}` or `{run_at}` for a one-time run; github takes `{github_integration_id, repository, events, filters}`; api takes no config. */+ /** Trigger configuration, shape validated per `type`: schedule takes `{cron_expression, timezone}` or `{run_at}` for a one-time run; github takes `{github_integration_id, repository, events, filters}` where `events` is one or more of `issues`, `issue_comment`, `pull_request`, `push` (`event.action` shorthand like `issues.opened` is folded into an `actions` filter, one event per trigger) and `filters` takes `{actions, branches, labels}`; api takes no config. */config?: unknown}diff --git a/products/tasks/frontend/generated/api.zod.ts b/products/tasks/frontend/generated/api.zod.tsindex 93b85b99f591..294875c7dd4c 100644--- a/products/tasks/frontend/generated/api.zod.ts+++ b/products/tasks/frontend/generated/api.zod.ts@@ -307,7 +307,7 @@ export const LoopsCreateBody = /* @__PURE__ */ zod.unknown().optional().describe(- 'Trigger configuration, shape validated per `type`: schedule takes `{cron_expression, timezone}` or `{run_at}` for a one-time run; github takes `{github_integration_id, repository, events, filters}`; api takes no config.'+ 'Trigger configuration, shape validated per `type`: schedule takes `{cron_expression, timezone}` or `{run_at}` for a one-time run; github takes `{github_integration_id, repository, events, filters}` where `events` is one or more of `issues`, `issue_comment`, `pull_request`, `push` (`event.action` shorthand like `issues.opened` is folded into an `actions` filter, one event per trigger) and `filters` takes `{actions, branches, labels}`; api takes no config.'),}))@@ -608,7 +608,7 @@ export const LoopsPartialUpdateBody = /* @__PURE__ */ zod.unknown().optional().describe(- 'Trigger configuration, shape validated per `type`: schedule takes `{cron_expression, timezone}` or `{run_at}` for a one-time run; github takes `{github_integration_id, repository, events, filters}`; api takes no config.'+ 'Trigger configuration, shape validated per `type`: schedule takes `{cron_expression, timezone}` or `{run_at}` for a one-time run; github takes `{github_integration_id, repository, events, filters}` where `events` is one or more of `issues`, `issue_comment`, `pull_request`, `push` (`event.action` shorthand like `issues.opened` is folded into an `actions` filter, one event per trigger) and `filters` takes `{actions, branches, labels}`; api takes no config.'),}))diff --git a/products/tasks/mcp/apps/LoopReviewView.test.ts b/products/tasks/mcp/apps/LoopReviewView.test.tsindex 4e914e854e41..b54dd3d9c09c 100644--- a/products/tasks/mcp/apps/LoopReviewView.test.ts+++ b/products/tasks/mcp/apps/LoopReviewView.test.ts@@ -1,4 +1,6 @@-jest.mock('@posthog/mcp-ui', () => ({ DescriptionList: () => null }), { virtual: true })+jest.mock('@posthog/mcp-ui', () => ({ DescriptionList: () => null }), {+ virtual: true,+})jest.mock('@posthog/quill', () => ({ Button: () => null, Card: () => null, CardContent: () => null }), {virtual: true,})@@ -33,15 +35,130 @@ describe('LoopReviewView', () => {['no triggers shows the default overlap policy', {}, 'Manual only · skips overlapping runs'],['cron schedule trigger',- { triggers: [{ type: 'schedule', config: { cron_expression: '0 9 * * 1' } }] },+ {+ triggers: [+ {+ type: 'schedule',+ config: { cron_expression: '0 9 * * 1' },+ },+ ],+ },'Schedule (0 9 * * 1) · skips overlapping runs',],+ [+ 'github trigger without events',+ {+ triggers: [+ {+ type: 'github',+ config: { repository: 'posthog/code' },+ },+ ],+ },+ 'GitHub (posthog/code) · skips overlapping runs',+ ],+ [+ 'github trigger lists its events',+ {+ triggers: [+ {+ type: 'github',+ config: {+ repository: 'posthog/code',+ events: ['issues', 'pull_request'],+ },+ },+ ],+ },+ 'GitHub (posthog/code: issues, pull_request) · skips overlapping runs',+ ],+ [+ 'github trigger with an actions filter',+ {+ triggers: [+ {+ type: 'github',+ config: {+ repository: 'posthog/code',+ events: ['issues'],+ filters: { actions: ['opened'] },+ },+ },+ ],+ },+ 'GitHub (posthog/code: issues opened) · skips overlapping runs',+ ],+ [+ 'github trigger with singular action alias',+ {+ triggers: [+ {+ type: 'github',+ config: {+ repository: 'posthog/code',+ events: ['issues'],+ filters: { action: 'opened' },+ },+ },+ ],+ },+ 'GitHub (posthog/code: issues opened) · skips overlapping runs',+ ],+ [+ 'github trigger with dotted event shorthand',+ {+ triggers: [+ {+ type: 'github',+ config: {+ repository: 'posthog/code',+ events: ['issues.opened'],+ },+ },+ ],+ },+ 'GitHub (posthog/code: issues opened) · skips overlapping runs',+ ],+ [+ 'github push trigger with a branch filter',+ {+ triggers: [+ {+ type: 'github',+ config: {+ repository: 'posthog/code',+ events: ['push'],+ filters: { branches: ['main'] },+ },+ },+ ],+ },+ 'GitHub (posthog/code: push on main) · skips overlapping runs',+ ],+ [+ 'github trigger with a label filter',+ {+ triggers: [+ {+ type: 'github',+ config: {+ repository: 'posthog/code',+ events: ['issues'],+ filters: { labels: ['bug'] },+ },+ },+ ],+ },+ 'GitHub (posthog/code: issues labeled bug) · skips overlapping runs',+ ],['paused loop', { enabled: false }, 'Manual only · paused · skips overlapping runs'],['allow policy', { overlap_policy: 'allow' }, 'Manual only · allows overlapping runs'],['cancel_previous policy', { overlap_policy: 'cancel_previous' }, 'Manual only · cancels the previous run'],['unknown policy falls back to the raw value',- { overlap_policy: 'defer' as unknown as LoopReviewData['overlap_policy'] },+ {+ overlap_policy: 'defer' as unknown as LoopReviewData['overlap_policy'],+ },'Manual only · defer',],])('%s', (_label, data, expected) => {diff --git a/products/tasks/mcp/apps/LoopReviewView.tsx b/products/tasks/mcp/apps/LoopReviewView.tsxindex a267175489e8..d55f183c00d2 100644--- a/products/tasks/mcp/apps/LoopReviewView.tsx+++ b/products/tasks/mcp/apps/LoopReviewView.tsx@@ -82,6 +82,51 @@ const ADAPTER_LABELS: Record<string, string> = {codex: 'Codex',}+function stringList(value: unknown): string[] {+ if (typeof value === 'string') {+ return [value]+ }+ if (Array.isArray(value)) {+ return value.filter((item): item is string => typeof item === 'string')+ }+ return []+}++function describeGithubTrigger(config: Record<string, unknown>): string {+ const repository = typeof config.repository === 'string' ? config.repository : 'a repo'+ const filters = (config.filters ?? {}) as Record<string, unknown>+ const actions = [...stringList(filters.actions), ...stringList(filters.action)]+ const events: string[] = []+ for (const raw of stringList(config.events)) {+ // the API also accepts `issues.opened` shorthand; render it as event + action+ const dot = raw.indexOf('.')+ const event = dot > 0 ? raw.slice(0, dot) : raw+ const action = dot > 0 ? raw.slice(dot + 1) : ''+ if (!events.includes(event)) {+ events.push(event)+ }+ if (action && !actions.includes(action)) {+ actions.push(action)+ }+ }+ if (events.length === 0) {+ return `GitHub (${repository})`+ }+ let summary = events.join(', ')+ if (actions.length > 0) {+ summary += ` ${actions.join('/')}`+ }+ const branches = [...stringList(filters.branches), ...stringList(filters.branch)]+ if (branches.length > 0) {+ summary += ` on ${branches.join(', ')}`+ }+ const labels = [...stringList(filters.labels), ...stringList(filters.label)]+ if (labels.length > 0) {+ summary += ` labeled ${labels.join(', ')}`+ }+ return `GitHub (${repository}: ${summary})`+}+function describeTrigger(trigger: LoopReviewTrigger): string {if (trigger.type === 'schedule') {const config = trigger.config ?? {}@@ -94,8 +139,7 @@ function describeTrigger(trigger: LoopReviewTrigger): string {return 'Schedule'}if (trigger.type === 'github') {- const repository = (trigger.config?.repository as string | undefined) ?? 'a repo'- return `GitHub (${repository})`+ return describeGithubTrigger(trigger.config ?? {})}return 'API'}@@ -203,7 +247,10 @@ export function LoopReviewView({ data, onCreate, state }: LoopReviewViewProps):const items: { label: string; value: ReactNode }[] = [{ label: 'Name', value: data.name?.trim() || 'Not set' },...(data.description?.trim() ? [{ label: 'Description', value: data.description }] : []),- { label: 'Visibility', value: data.visibility === 'team' ? 'Team' : 'Personal' },+ {+ label: 'Visibility',+ value: data.visibility === 'team' ? 'Team' : 'Personal',+ },{label: 'What it does',value: <span className="whitespace-pre-wrap">{data.instructions?.trim() || 'No prompt'}</span>,@@ -212,13 +259,25 @@ export function LoopReviewView({ data, onCreate, state }: LoopReviewViewProps):{ label: 'Context', value: describeContext(data.context_target) },{ label: 'Repository', value: describeRepository(data.repositories) },{ label: 'Model', value: describeModel(data) },- { label: 'Opens PRs', value: data.behaviors?.create_prs ? 'Yes' : 'No' },+ {+ label: 'Opens PRs',+ value: data.behaviors?.create_prs ? 'Yes' : 'No',+ },{ label: 'Watches CI', value: data.behaviors?.watch_ci ? 'Yes' : 'No' },- { label: 'Fixes review comments', value: describeFixReviewComments(data.behaviors) },- { label: 'PostHog access', value: describePosthogAccess(data.connectors) },+ {+ label: 'Fixes review comments',+ value: describeFixReviewComments(data.behaviors),+ },+ {+ label: 'PostHog access',+ value: describePosthogAccess(data.connectors),+ },{ label: 'Connectors', value: describeConnectors(data.connectors) },{ label: 'Sandbox', value: data.sandbox_environment || 'None' },- { label: 'Notifications', value: describeNotifications(data.notifications) },+ {+ label: 'Notifications',+ value: describeNotifications(data.notifications),+ },]const creating = state?.loading ?? falsediff --git a/services/mcp/src/api/generated.ts b/services/mcp/src/api/generated.tsindex d404636898a1..e4c9ffef90e7 100644--- a/services/mcp/src/api/generated.ts+++ b/services/mcp/src/api/generated.ts@@ -38130,7 +38130,7 @@ export namespace Schemas {type: LoopTriggerTypeEnum;/** Whether this trigger is active. Disabling pauses only this trigger. */enabled?: boolean;- /** Trigger configuration, shape validated per `type`: schedule takes `{cron_expression, timezone}` or `{run_at}` for a one-time run; github takes `{github_integration_id, repository, events, filters}`; api takes no config. */+ /** Trigger configuration, shape validated per `type`: schedule takes `{cron_expression, timezone}` or `{run_at}` for a one-time run; github takes `{github_integration_id, repository, events, filters}` where `events` is one or more of `issues`, `issue_comment`, `pull_request`, `push` (`event.action` shorthand like `issues.opened` is folded into an `actions` filter, one event per trigger) and `filters` takes `{actions, branches, labels}`; api takes no config. */config?: unknown;}diff --git a/services/mcp/src/generated/tasks/api.ts b/services/mcp/src/generated/tasks/api.tsindex 40eef91953a0..9dd6825ab222 100644--- a/services/mcp/src/generated/tasks/api.ts+++ b/services/mcp/src/generated/tasks/api.ts@@ -319,7 +319,7 @@ export const LoopsCreateBody = /* @__PURE__ */ zod.unknown().optional().describe(- 'Trigger configuration, shape validated per `type`: schedule takes `{cron_expression, timezone}` or `{run_at}` for a one-time run; github takes `{github_integration_id, repository, events, filters}`; api takes no config.'+ 'Trigger configuration, shape validated per `type`: schedule takes `{cron_expression, timezone}` or `{run_at}` for a one-time run; github takes `{github_integration_id, repository, events, filters}` where `events` is one or more of `issues`, `issue_comment`, `pull_request`, `push` (`event.action` shorthand like `issues.opened` is folded into an `actions` filter, one event per trigger) and `filters` takes `{actions, branches, labels}`; api takes no config.'),}))@@ -632,7 +632,7 @@ export const LoopsPartialUpdateBody = /* @__PURE__ */ zod.unknown().optional().describe(- 'Trigger configuration, shape validated per `type`: schedule takes `{cron_expression, timezone}` or `{run_at}` for a one-time run; github takes `{github_integration_id, repository, events, filters}`; api takes no config.'+ 'Trigger configuration, shape validated per `type`: schedule takes `{cron_expression, timezone}` or `{run_at}` for a one-time run; github takes `{github_integration_id, repository, events, filters}` where `events` is one or more of `issues`, `issue_comment`, `pull_request`, `push` (`event.action` shorthand like `issues.opened` is folded into an `actions` filter, one event per trigger) and `filters` takes `{actions, branches, labels}`; api takes no config.'),}))diff --git a/services/mcp/tests/unit/__snapshots__/tool-schemas/loops-create-prepare.json b/services/mcp/tests/unit/__snapshots__/tool-schemas/loops-create-prepare.jsonindex b4a7864bcf42..cca47d2ab54b 100644--- a/services/mcp/tests/unit/__snapshots__/tool-schemas/loops-create-prepare.json+++ b/services/mcp/tests/unit/__snapshots__/tool-schemas/loops-create-prepare.json@@ -278,7 +278,7 @@"items": {"properties": {"config": {- "description": "Trigger configuration, shape validated per `type`: schedule takes `{cron_expression, timezone}` or `{run_at}` for a one-time run; github takes `{github_integration_id, repository, events, filters}`; api takes no config."+ "description": "Trigger configuration, shape validated per `type`: schedule takes `{cron_expression, timezone}` or `{run_at}` for a one-time run; github takes `{github_integration_id, repository, events, filters}` where `events` is one or more of `issues`, `issue_comment`, `pull_request`, `push` (`event.action` shorthand like `issues.opened` is folded into an `actions` filter, one event per trigger) and `filters` takes `{actions, branches, labels}`; api takes no config."},"enabled": {"default": true,diff --git a/services/mcp/tests/unit/__snapshots__/tool-schemas/loops-partial-update.json b/services/mcp/tests/unit/__snapshots__/tool-schemas/loops-partial-update.jsonindex 5b94f4817a6b..ca953575d316 100644--- a/services/mcp/tests/unit/__snapshots__/tool-schemas/loops-partial-update.json+++ b/services/mcp/tests/unit/__snapshots__/tool-schemas/loops-partial-update.json@@ -275,7 +275,7 @@"items": {"properties": {"config": {- "description": "Trigger configuration, shape validated per `type`: schedule takes `{cron_expression, timezone}` or `{run_at}` for a one-time run; github takes `{github_integration_id, repository, events, filters}`; api takes no config."+ "description": "Trigger configuration, shape validated per `type`: schedule takes `{cron_expression, timezone}` or `{run_at}` for a one-time run; github takes `{github_integration_id, repository, events, filters}` where `events` is one or more of `issues`, `issue_comment`, `pull_request`, `push` (`event.action` shorthand like `issues.opened` is folded into an `actions` filter, one event per trigger) and `filters` takes `{actions, branches, labels}`; api takes no config."},"enabled": {"default": true,diff --git a/services/mcp/tests/unit/__snapshots__/tool-schemas/loops-review.json b/services/mcp/tests/unit/__snapshots__/tool-schemas/loops-review.jsonindex b4a7864bcf42..cca47d2ab54b 100644--- a/services/mcp/tests/unit/__snapshots__/tool-schemas/loops-review.json+++ b/services/mcp/tests/unit/__snapshots__/tool-schemas/loops-review.json@@ -278,7 +278,7 @@"items": {"properties": {"config": {- "description": "Trigger configuration, shape validated per `type`: schedule takes `{cron_expression, timezone}` or `{run_at}` for a one-time run; github takes `{github_integration_id, repository, events, filters}`; api takes no config."+ "description": "Trigger configuration, shape validated per `type`: schedule takes `{cron_expression, timezone}` or `{run_at}` for a one-time run; github takes `{github_integration_id, repository, events, filters}` where `events` is one or more of `issues`, `issue_comment`, `pull_request`, `push` (`event.action` shorthand like `issues.opened` is folded into an `actions` filter, one event per trigger) and `filters` takes `{actions, branches, labels}`; api takes no config."},"enabled": {"default": true,