Skip to content

Link an Automation run to its Conversation with a foreign key #2176

Closed

Description

@sentry-junior

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 in junior_event_tasks (juniorEventAutomations). No single table exists for a foreign key to point at. So every reference is a kind plus an ID. Examples are junior_task_executions.kind + task_id, junior_automation_versions.kind + automation_id, and junior_conversations.origin_type + origin_id.
  • No run row until the run ends. junior_task_executions is written only on a terminal transition (blocked, completed, failed). Its conversation_id is 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_id when a run starts. The title lookup (readAutomationTitlesByConversationId in chat/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 kind column. 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_id foreign 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 the origin_* lookup.

Work

  • Merge junior_scheduler_tasks and junior_event_tasks into one Automation table. Move junior_automation_versions and junior_task_executions to a single automation_id foreign key.
  • Create the run row at dispatch, together with its Conversation. Decide if junior_scheduler_runs becomes 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 readAutomationTitlesByConversationId and the origin_* 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

via David Cramer.

--

View Junior Session [Sentry]

Activity

  1. sentry-junior commented on Nov 9, 2026

    @sentry-junior
    ContributorAuthor

    Fixed on main: one junior_automation_runs table with a conversation_id foreign key (#2179, #2183, #2184).

    Closed during a tracker cleanup. Reopen if something here is still missing.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions