Skip to content

feat(automations): Store all Automation runs in one table - #2183

Merged
dcramer merged 1 commit into
mainfrom
feat/automation-runs
Oct 17, 2026
Merged

dcramer merged 1 commit into
mainfrom
feat/automation-runs

Conversation

@sentry-junior

@sentry-junior sentry-junior Bot commented Oct 17, 2026

Copy link
Copy Markdown
Contributor

PR 2 of #2176. Adds junior_automation_runs, which replaces junior_scheduler_runs and junior_task_executions. All run readers and writers now use it.

Table

  • id keeps today's run ids: <automation id>:<scheduled time> for Scheduled automation runs, and the dispatch id for event automation runs.
  • automation_id is a required FK to junior_automations (cascade delete).
  • conversation_id is a nullable, unique FK to junior_conversations. Automation code sets it at dispatch. It is set only when the dispatch Conversation exists. A later write of the same run fills a missing link. A skipped run, or a run that failed before dispatch, has no Conversation.
  • status is one of pending, running, completed, failed, blocked, skipped. There is no namespace column.
  • The scheduler state that used to live in the record JSON is now columns.

Migration 0056

Drizzle generated the DDL. The backfill is hand-written:

  1. Copy every junior_scheduler_runs row first, because it has the full run state.
  2. Copy the junior_task_executions rows that are still missing (ON CONFLICT DO NOTHING).
  3. Set conversation_id = 'agent-dispatch:' || dispatch id only where that Conversation exists.

Rows of Automations that no longer exist, and rows with an unknown status, kind, or namespace, are not copied.

Readers and writers

  • Scheduler claim and settle (scheduled-automations/runs.ts, tasks.ts, tools/update.ts). The heartbeat no longer writes a separate execution row.
  • Event automations: ingest inserts a running run after dispatch. The dispatch store records its outcome. The heartbeat settles running event runs from their dispatch record, and fails a run whose dispatch record is gone. Either write can come first. The outcome is kept, and a missing Conversation link is filled.
  • Execution stats, the dashboard stats, and the Automation list read only runs that have an outcome (blocked, completed, failed).
  • readAutomationTitlesByConversationId is a plain join on conversation_id. This deletes the "newest execution wins" title fallback from fix(automations): Title automation runs instead of "Internal" #2169.

Deploy

Accepted for the deploy window: old workers still write the old tables, and those writes are lost.

Left for PR 3

  • Drop junior_scheduler_runs and junior_task_executions.
  • Drop the origin_type, origin_id, and origin_run_id Conversation columns and their index. No code reads them. Today's Conversation upsert still writes them, so dropping them here would break old workers during the deploy. They go with the old tables.

TODOs mark each of these.

Tests

Extended existing scenarios. There are no migration tests.

  • heartbeat.test.ts: a dispatched Scheduled automation run links its Conversation.
  • event-automations.test.ts: each dispatch starts a linked running run. The heartbeat settles a completed dispatch and fails a run whose dispatch record is gone.
  • Existing automation, stats, conversation detail, and Slack card tests now read the new table.

Refs #2176

via David Cramer.

--

View Junior Session [Sentry]

Add junior_automation_runs. It replaces junior_scheduler_runs and
junior_task_executions for Scheduled and event automations. Each run has
a required automation_id and an optional, unique conversation_id that
automation code sets at dispatch.

Migration 0056 copies the scheduler runs, then the missing executions.
Scheduler claim and settle, event run recording, execution and dashboard
stats, and Automation title lookup now read and write the new table. The
heartbeat also settles running event automation runs.

The old tables and the unused origin_* Conversation columns stay until
the next change.

Refs #2176

Co-Authored-By: David Cramer <[email protected]>
@vercel

vercel Bot commented Oct 17, 2026 •

Copy link
Copy Markdown

The latest updates on your projects. Learn more about Vercel for GitHub.

Project Deployment Actions Updated
junior-docs Ready Ready Preview Oct 17, 2026 2:17pm UTC

Request Review

@github-actions github-actions Bot added the risk: high PR risk score: high label Oct 17, 2026

@cursor cursor Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Cursor Bugbot has reviewed your changes and found 1 potential issue.

Fix All in Cursor

❌ Bugbot Autofix is OFF. To automatically fix reported issues with cloud agents, enable autofix in the Cursor dashboard.

Reviewed by Cursor Bugbot for commit 391251d. Configure here.

executionId: run.id,
nowMs: dispatch?.updatedAtMs ?? nowMs,
status,
});

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Event settle omits conversation link

Medium Severity

settleEventAutomationRuns records the outcome without a conversationId, so heartbeat settlement cannot attach the dispatch Conversation. recordEventAutomationRunStarted also uses onConflictDoNothing, so a later start write cannot fill a missing link either. Event runs whose Conversation appears after the first write, or whose outcome is stored only by settle, stay unlinked.

Additional Locations (1)
Fix in Cursor Fix in Web

Reviewed by Cursor Bugbot for commit 391251d. Configure here.

@dcramer
dcramer merged commit 9c26630 into main Oct 17, 2026
46 checks passed
@dcramer
dcramer deleted the feat/automation-runs branch October 17, 2026 16:56
dcramer added a commit that referenced this pull request Oct 18, 2026
Follow-up cleanup after the Automation table migrations (#2179, #2183,
#2184).

## Bug

The heartbeat settled Scheduled automation runs only for automations
that were not deleted. It did this through `listIncompleteScheduledRuns`
→ `listScheduledAutomations`, which filters `status <> 'deleted'`.
Delete marks only *pending* runs as skipped. So when an automation was
deleted while its run was dispatched, the run stayed `running` forever
and never became an execution. Event runs already settle in this case.

`listIncompleteScheduledRuns` now reads `junior_automation_runs` joined
to `kind = 'scheduled'`, with no status filter. One function now covers
both the heartbeat and the per-automation claim check. This also stops
the heartbeat from loading and decoding every Scheduled automation on
each tick. The new heartbeat integration case fails on `main` (the run
stays `running`) and passes with this change.

## Dead code

- `scheduled-automations/personal.ts`: removed
`listViewerScheduledAutomations` and its cursor code. `collection.ts`
replaced it, and it had no callers.
- Removed `scheduledAutomationPrincipalLabel` and
`ScheduledAutomationCredentialMode`, which had no callers.
- Removed the `scheduled-automations/index.ts` and `tools.ts` barrels.
Their two consumers now import the tool files directly, as AGENTS.md
asks.
- Removed the "adopts deployed Scheduler rows" component test. It builds
the dropped legacy tables to test migration SQL, which the testing
policy rules out.
- Removed the `kind` tie-breaker from the Automation collection order.
Ids are unique in one table, so it never decided anything.
- Removed README notes about older workers during rolling deploys.

## Not in this PR

These change behavior or contracts, so they need their own PRs:

- `executionActor` is written and never read. The schema is `.strict()`
and stored rows contain the field, so removing it needs a data migration
first.
- Ownership uses two rules: `creator_identity_id` for scheduled rows,
`team_id + creator_slack_user_id` for event rows.
- Remove `kind` from `/api/automations/:kind/:id` and the dashboard
`/automations/:kind/:id/...` URLs. Both already have it today; ids are
unique, so the routes can key on id alone.
- Two locking schemes: an advisory lock for scheduled rows, `FOR UPDATE`
for event rows.
- Run writes are split across `execution-stats.ts` and the two `runs.ts`
files. `recordAutomationExecution` also swallows write errors.
- Migration 0058, which drops `junior_automation_versions.kind` and
`junior_conversations.origin_*`.

## Checks

- `pnpm typecheck` passed.
- `pnpm lint` passed. It includes the file-length, migration-metadata
and test-architecture checks.
- `vitest`: `heartbeat`, `scheduled-automations-sql`, `api/automations`
and `api/automation-edits` all pass.
- `slack-schedule-tools` and `event-automations`: everything passes with
`--testTimeout=30000`, except "delivers the current automation card and
retains its reference in web history". That test fails the same way on
`main` in my sandbox. At the default 5s timeout, unrelated tests in
these files time out in my sandbox on `main` too.

<!-- junior-request-attribution:start -->
via **David Cramer**.
<!-- junior-request-attribution:end -->

<!-- junior-session-footer:start -->
<!-- junior-conversation-id:slack%3AC0B595QDZLL%3A1792312176.174969 -->

--

[View Junior
Session](https://junior-prod.sentry.dev/conversations/slack%3AC0B595QDZLL%3A1792312176.174969)
[[Sentry]](https://sentry.sentry.io/explore/conversations/slack%3AC0B595QDZLL%3A1792312176.174969/?project=4510944073809921)

<!-- junior-session-footer:end -->

Co-authored-by: sentry-junior[bot] <264270552+sentry-junior[bot]@users.noreply.github.com>
Co-authored-by: David Cramer <[email protected]>

This branch was successfully deployed

1 active deployment
Preview – junior-docs — 391251d1 Deployed Oct 17, 2026 by vercel[bot]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

risk: high PR risk score: high

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant