Skip to content

feat(chatd): journal workflow runs so a restart resumes them - #257

Merged
ibetitsmike merged 11 commits into
mainfrom
mike/chat-workflows-2a-journal
Nov 12, 2026
Merged

ibetitsmike merged 11 commits into
mainfrom
mike/chat-workflows-2a-journal

Conversation

@ibetitsmike

@ibetitsmike ibetitsmike commented Nov 12, 2026 •

Copy link
Copy Markdown
Owner

Part of #226 (workflow runs): the durable run journal. PR1a (#228) wired the workflow tools, PR1b (#238) added the in-memory engine, PR1c (#242) gated them to deployment owners. This PR journals every run so a restart resumes it instead of dropping it.

This PR was authored by Xum, a coding agent, working on Mike's fork. All commits, reviews and CI runs here are agent-driven work done for Mike.

What changed

  • chatdmig node 01a1d041-1628-77af-9c39-d4e3e711e0d3_workflow_journal.up.sql: workflow_runs (id, chat_id, model_config_id, script, args, status running|interrupted|completed|failed|stopped, error, result jsonb, created_at) and workflow_steps (id = the step's child chat id, run_id, spec_key, occurrence, ordinal, title, status running|reminded|reported|failed, value jsonb, error), UNIQUE(run_id, spec_key, occurrence).
  • forkdb queries (queries/workflow_runs.sql): 8 journal queries (get/insert run and step, list steps of a run, update run status + step, mark open runs interrupted). Nullable jsonb columns and params use pqtype.NullRawMessage so an empty document journals as a real SQL NULL, not an empty string.
  • Engine journaling (fork_workflow_journal.go): startWorkflowRun writes the run row before the script executes, startStep writes each step after its child chat exists (a crash between the two self-heals on resume), waitStep journals reminded/reported transitions, and finish journals the terminal status with the full result snapshot. The startup sweep marks still-running rows interrupted with a fixed error, so resume_run_id can pick them up.
  • Resume semantics: resume_run_id on an open run re-executes the deterministic script from the top; the k-th call of a spec maps onto journaled occurrence k, finished steps return their journaled report with reused: true and unfinished ones reuse their child chat. Resuming a terminal (completed/failed/stopped) run replays the stored snapshot without executing anything.
  • Authorization and metrics: every new query is chat-scoped in dbauthz/dbmetrics (MarkOpenWorkflowRunsInterrupted is system-scoped).

Resume observability note

A resumed open run answers run_workflow before the script re-executes, so that snapshot has steps: [] and status: running; reused: true appears in the await_workflow result. A resumed terminal run returns the full step list with reused: true immediately.

Known limits

  • Live logs of a running run are lost on restart; the terminal snapshot keeps them.
  • Journal updates on a live run are best-effort (warn on failure); the initial run/step inserts fail closed.
  • Multi-replica coordination is out of scope: the startup sweep is unscoped (every open run is marked interrupted), so this stack assumes the single-replica chatd deployment the engine has required since PR1b. Replica leases are follow-up work.
  • A child chat left running after a mid-turn crash is pre-existing PR1b behavior.

Size note

Hand-written code plus tests is ~940 lines; the 372-line gen commit is sqlc/goimports/mockgen output over the 10 fork queries. The lead approved landing the stack as one PR instead of splitting tests out.

Validation

Container slot r8 (ops-verified, script sha1 693e3e731c): sqlc regen + build + vet + gofmt + chatdmig check green on 8577273's source commits, and the full TestForkWorkflow* chatd suite passed on real Postgres for both this branch (step2 rc=0) and the stacked 2b compile+tests (step3/step4 rc=0). Log sha1 f8919b7bec. fork-ci on this head is the authority for the coderd route tests and everything else.

A nil json.RawMessage parameter is not a driver NULL: database/sql
converts it to a zero-length []byte, which lib/pq sends as an empty
string and Postgres rejects on a jsonb column (invalid input syntax
for type json). Route the three nullable jsonb params through
pqtype.NullRawMessage via sqlc column overrides and a jsonbValue
helper.
…olumns

The sqlc.arg(...)::jsonb casts cut the params off from the column
overrides, so InsertWorkflowStepParams.Value and the Update* result and
value params generated as json.RawMessage and did not compile against
jsonbValue's pqtype.NullRawMessage. An uncast param keeps the override:
the generated param is NullRawMessage, which sends a real SQL NULL for
an empty document instead of the zero-length string Postgres rejects
on jsonb. The models already scanned NullRawMessage, so this completes
the same fix as the column overrides.
A resumed run returned journaled steps without setting reused, so the
await result showed reused false for steps the restart did not run
again. The journal read path already hardcodes reused for the same
reason.
@chatgpt-codex-connector

chatgpt-codex-connector Bot commented Nov 12, 2026 •

Copy link
Copy Markdown
Codex Review Summary: ✅ Completed

This comment shows the latest Codex review activity on this pull request.

Review Status Commit Review trigger
📝 Code Review ✅ Completed 2026-11-12T17:11:21.441117Z 45558e0 Manual request
ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review" or "@codex security review".

Codex reacts with 👀 while any review is running, comments if it has suggestions, and reacts with 👍 once all reviews finish with no findings.

@chatgpt-codex-connector chatgpt-codex-connector 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.

💡 Codex Review

Here are some automated review suggestions for this pull request.

Reviewed commit: 85772738e5

ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".

Comment thread coderd/x/chatd/fork_workflow.go Resolved
Comment thread coderd/x/chatd/fork_workflow.go Resolved
Comment thread coderd/x/chatd/fork_workflow_journal.go Resolved
@ibetitsmike

Copy link
Copy Markdown
Owner Author

@codex review

This review request is posted by Xum, a coding agent, working for Mike on this fork.

@chatgpt-codex-connector chatgpt-codex-connector 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.

💡 Codex Review

Here are some automated review suggestions for this pull request.

Reviewed commit: 45558e0bf5

ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".

Comment thread coderd/x/chatd/fork_workflow_journal.go Resolved
@ibetitsmike
ibetitsmike merged commit 349969c into main Nov 12, 2026
9 checks passed
@ibetitsmike
ibetitsmike deleted the mike/chat-workflows-2a-journal branch November 12, 2026 17:12
@ibetitsmike

Copy link
Copy Markdown
Owner Author

Xum merged this PR on behalf of @ibetitsmike.

Merged head 45558e0bf5 as squash commit 349969c54a. Gates on that head:

  • fork-ci run 43263770579: all 7 jobs passed.
  • Codex receipt 5878792642: one P2 (fail startup when the interruption sweep fails), declined as an edge case off the normal path. 0 unresolved threads.
  • Ops containerized validation r8 passed 4/4: forkdb generation, build, vet, chatdmig check and the chatd TestForkWorkflow* tests for this PR and the run API PR stacked on it.
  • No live UAT: the engine is owner-only and this PR has no UI. The replay tests failed in r6 and r7 before the reuse fixes and passed in r8.

Known limit: the startup sweep is unscoped, so this assumes a single chatd replica. Replica leases are follow-up work.

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

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant