Skip to content

feat: workflow run list, detail, and stop API - #258

Merged
ibetitsmike merged 24 commits into
mainfrom
mike/chat-workflows-2b-run-api
Nov 12, 2026
Merged

ibetitsmike merged 24 commits into
mainfrom
mike/chat-workflows-2b-run-api

Conversation

@ibetitsmike

@ibetitsmike ibetitsmike commented Nov 12, 2026 •

Copy link
Copy Markdown
Owner

Part of #226 (items 3-4 of the W12 workflow stack: stop_workflow plus the run list/detail API). Follows #257 (durable journal).

This PR was authored and iterated by Xum (an agent) working for Mike; review comments are triaged by Xum under Mike's standing policy.

What

Stops and introspection for workflow runs, on top of the PR2a journal:

  • POST /api/v2/chats/fork/{chat}/workflow-runs/{run}/stop: stops a running run. The script context is canceled, open steps fail with "stopped by user" (FailOpenWorkflowStepsOfRun journals them), and their child chats stop. The run reads stopped afterwards. A run that already ended answers 409; an interrupted run is not stoppable either, because the agent continues it with resume_run_id. Sentinels: ErrForkWorkflowRunNotFound, ErrForkWorkflowRunTerminal.
  • GET /api/v2/chats/fork/{chat}/workflow-runs: one keyset page (?limit default 50 at most 100, ?after = <RFC3339 created_at>/<run id>), newest first, with per-run step counts (total/running/done/failed; done = reported, running = running or reminded). List items omit the script and carry script_preview (first 200 chars).
  • GET /api/v2/chats/fork/{chat}/workflow-runs/{run}: full detail: script, steps (id == child chat id, status, reused, ordinal, started/finished), terminal result and the last 50 log lines of the terminal snapshot.
  • chatdmig node 01a1d05b-..._workflow_run_timestamps.up.sql: workflow_runs.updated_at/finished_at, workflow_steps.started_at/finished_at; UpdateWorkflowRunStatus/UpdateWorkflowStep stamp them via CASE on the terminal statuses.
  • The engine's own stop path: requestStop cancels the script context, execute maps stopRequested && ctx.Err() to stopped, interruptOpenSteps runs on a fresh inflight context, and shutdown still journals interrupted, not stopped.

All three routes live under the fork chat routes with the experiment flag and the owner gate (deployment owners only, 403 fail-closed), and carry @x-apidocgen {"skip": true} so the markdown reference docs are untouched; swagger.json, docs.go and typesGenerated.ts are regenerated and committed.

Known gaps

  • Live log lines while a run is running are absent: the journal keeps no live logs, so a detail read mid-run returns no logs and a restart loses the live lines (terminal snapshots keep them). Write-through logging is a later PR.
  • Multi-replica coordination is out of scope: a second replica cannot stop a run registered in the first one's memory; the journal path covers restart cases.
  • A child chat left in status running after a mid-turn crash is pre-existing PR1b behavior.
  • The detail endpoint reads journal rows, which carry no reused flag; only the terminal result snapshot has it. The detail view of a resumed run therefore reports reused: false for its steps; the await_workflow result is authoritative for reuse.
  • The registry stop waits up to 5s for the script to unwind, then the journal path fails the open steps; a wedged script can therefore take up to 5s to answer stop.

Tests

  • chatd: TestForkWorkflowStop/{StopsALiveRun,InterruptedRunIsNotStoppable} (registry stop, interrupted 409 semantics), journal stop path in the restart tests.
  • coderd: TestForkWorkflowRunsRoutes/{ExperimentOff,ListAndDetail} (403 with the flag off, list step counts, detail logs/result, 409 stopping a terminal run, 404 unknown run).
  • dbauthz: TestForkQuerier cases for GetWorkflowRunsByChat, GetWorkflowRunStepCountsByChat, FailOpenWorkflowStepsOfRun (read/update through the owning chat; FailOpenWorkflowStepsOfRun authorizes via the run's chat).

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.
The run list, run detail and stop_workflow of a chat, as the API side
of the fork-workflows experiment:

- GET  /api/v2/chats/fork/{chat}/workflow-runs (keyset page, step counts)
- GET  /api/v2/chats/fork/{chat}/workflow-runs/{run} (script, steps,
  result and logs of the terminal snapshot)
- POST /api/v2/chats/fork/{chat}/workflow-runs/{run}/stop (409 terminal)

Every route is gated like the workflow tools: the fork-workflows
experiment and the chat owner's deployment owner role. The stop runs
through chatd.ForkStopWorkflowRun: a live run unwinds on a canceled
script context and journals stopped with a snapshot that carries the
failed open steps; a run this replica does not hold is stopped through
its journal rows.

A chatdmig node adds workflow_runs.updated_at/finished_at and
workflow_steps.started_at/finished_at, which the run status updates
stamp. The generated forkdb output, the apidoc swagger entries and
typesGenerated.ts are not in this commit yet.
…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.
…s-2b-run-api

# Conflicts:
#	coderd/database/forkdb/queries/workflow_runs.sql
The merge of the jsonb param fix duplicated the method after
FailOpenWorkflowStepsOfRun; vet caught the redeclaration.
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.
…b-run-api

# Conflicts:
#	coderd/database/dbauthz/fork_forkdb.go
#	coderd/database/dbmetrics/fork_forkdb.go
#	coderd/database/forkdb/queries/workflow_runs.sql
#	coderd/database/forkdb/sqlc.yaml
#	coderd/x/chatd/fork_workflow.go
#	coderd/x/chatd/fork_workflow_journal.go
#	coderd/x/chatd/fork_workflow_journal_internal_test.go
@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-12T18:16:50.218348Z 0e786e7 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

res.Status = workflowStatusCompleted
res.Result = value

P2 Badge Honor an accepted stop after the script returns

If a stop request wins the requestStop lock after program.run has returned successfully but before finish stores the result, requestStop reports success and cancels the context, yet this path only checks stopRequested when err != nil and unconditionally records completed for the successful return. The caller therefore receives a successful stop even though the run completes normally; check the stop flag after program.run regardless of its error result.

ℹ️ 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_stop.go Resolved
Comment thread coderd/x/chatd/fork_workflow_stop.go Resolved
Comment thread codersdk/fork_workflow_runs.go Resolved
Comment thread coderd/fork_workflow_runs.go Resolved
@ibetitsmike

Copy link
Copy Markdown
Owner Author

This review round is requested by Xum, an agent working for Mike.

@codex review

@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: 0e786e767e

ℹ️ 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_stop.go Resolved
Comment thread site/src/api/typesGenerated.ts Resolved
@ibetitsmike
ibetitsmike merged commit 704d820 into main Nov 12, 2026
10 checks passed
@ibetitsmike
ibetitsmike deleted the mike/chat-workflows-2b-run-api branch November 12, 2026 18:18
@ibetitsmike

Copy link
Copy Markdown
Owner Author

Xum merged this PR on behalf of @ibetitsmike.

Merged head 0e786e767e as squash commit 704d820a11. Gates on that head:

  • fork-ci run 43274694897: all 7 jobs passed, including TestForkQuerier and the TestForkWorkflowStop subtests that were flaky on earlier heads.
  • Codex receipt 5879722510: two P2s, both declined (the five-second stop wait timeout, and the generated typing of the raw JSON payloads). 0 unresolved threads.
  • Ops containerized regen v4 passed. The generated files in aee11f5663 match the tested output byte for byte.
  • No live UAT here: the API is owner-gated and has no UI of its own. It gets UI UAT through the Workflows panel in feat(site): add a Workflows panel to agent chats #256.

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