Repository navigation
Closed
Description
Linking an Automation run to its Conversation should be a plain foreign key. Today it needs a lookup across several tables, a fallback path, and a kind value on every link. This issue tracks a data model change to make it simple.
It came up while fixing run titles and links in #2169, #2170 and #2174. Each of these PRs had to work around the current schema.
Current model (main)
- Two Automation tables. Scheduled Automations are in
junior_scheduler_tasks(juniorSchedulerTasks). Event Automations are injunior_event_tasks(juniorEventAutomations). No single table exists for a foreign key to point at. So every reference is akindplus an ID. Examples arejunior_task_executions.kind+task_id,junior_automation_versions.kind+automation_id, andjunior_conversations.origin_type+origin_id. - No run row until the run ends.
junior_task_executionsis written only on a terminal transition (blocked,completed,failed). Itsconversation_idis nullable, because the Conversation might not be saved yet. - Run state is in more than one place. Scheduled runs also have
junior_scheduler_runs. Event runs only have the agent-dispatch record, and that record has a TTL. - A polymorphic origin on the Conversation. fix(automations): Link runs to their Automation when they start #2174 writes
origin_type/origin_id/origin_run_idwhen a run starts. The title lookup (readAutomationTitlesByConversationIdinchat/automations/execution-stats.ts) must left-join both Automation tables. For older runs it then falls back to run history.
Because of this, titles, the run card, run history and stats each rebuild the run-to-Automation-to-Conversation link in their own way.
Target model
- One Automation table with a
kindcolumn. Scheduled and event fields go in the definition or in kind-specific columns. - One run row per dispatch. Create it in the same step as the run's Conversation. Give it a required foreign key to the Automation and a required foreign key to the Conversation, or put a single
automation_run_idforeign key on the Conversation. The run status changes on that row from start to end. - Use plain joins for titles, the run card, run history, stats and "last run". Remove the
kind+ ID pairs and theorigin_*lookup.
Work
- Merge
junior_scheduler_tasksandjunior_event_tasksinto one Automation table. Movejunior_automation_versionsandjunior_task_executionsto a singleautomation_idforeign key. - Create the run row at dispatch, together with its Conversation. Decide if
junior_scheduler_runsbecomes this table or is merged into it. - Backfill existing rows. Old runs may have no Conversation link, because the link was only written when the Conversation existed at the end.
- Point readers at the new joins: the title fallback in
api/conversations/projection.ts,readConversationSourceTask, execution stats, and the dashboard run card. - Remove the run-history fallback in
readAutomationTitlesByConversationIdand theorigin_*writes for Automation runs. - Decide how to settle runs that never reach a terminal state, for example an event run whose queue message is lost. With a run row that exists from the start, these runs show as stuck. Today they are not recorded at all.
Related
- Rethink how automations execute #2051 Rethink how automations execute (umbrella for the run itself; this issue covers the data model)
- Overhaul Junior automations #1717 Overhaul Junior automations (closed)
- Simplify Junior around one core Conversation and optional Location #1563 One core Conversation and optional Location
- fix(automations): Title automation runs instead of "Internal" #2169, feat(dashboard): Show an automation card at the top of automation runs #2170, fix(automations): Link runs to their Automation when they start #2174: the fixes that led to this issue
via David Cramer.
--
Metadata
Metadata
Assignees
Labels
No labels
Activity
ref(automations): Simplify the Automation table cutover
fix(automations): Copy legacy JSON creator identity in the backfill
sentry-junior commentedon Nov 9, 2026
Fixed on main: one
junior_automation_runstable with aconversation_idforeign key (#2179, #2183, #2184).Closed during a tracker cleanup. Reopen if something here is still missing.