Skip to content

feat(chatd): give workflow runs an owner and a heartbeat - #379

Merged
ibetitsmike merged 10 commits into
mainfrom
mike/issue-303-run-owner
Nov 17, 2026
Merged

ibetitsmike merged 10 commits into
mainfrom
mike/issue-303-run-owner

Conversation

@ibetitsmike

@ibetitsmike ibetitsmike commented Nov 17, 2026 •

Copy link
Copy Markdown
Owner

Coder Agents opened this PR on behalf of @ibetitsmike.

Part of #303 (part A: run ownership). Part B, the step signal from workflow_report, follows in a second PR that closes #303.

What changed

Behind the existing fork-workflows experiment.

  • Two new chatdmig nodes: workflow_runs.owner_id and heartbeat_at, and a partial index workflow_runs_running_idx on heartbeat_at of running rows (built concurrently, outside a transaction). No node already on main was edited.
  • A run's owner is the chatd worker ID (coderd's per-process replica ID, api.ID), so there is no new identity scheme. The owner renews the heartbeat of its live runs every 10 s. A running run whose heartbeat is older than 30 s belongs to a dead process.
  • The startup sweep now interrupts only stale runs, and every heartbeat tick runs the same sweep. A new process leaves the live runs of other processes alone.
  • Close releases the runs this process still owns: after the script goroutines drain it marks them interrupted at once, so a graceful restart (autodeploy) needs no 30 s wait. After a crash, another process's tick sweeps them within about 40 s. I rejected a "heartbeat older than my start is dead" rule: with live replicas it would interrupt runs they still execute.
  • Engine journal writes (status, phases, step insert and update, failing open steps, logs) carry the owner and land only while the row is running and owned by this process. A fenced write cancels the run as lost; a lost run ends without writing or notifying. workflow_report and the reuse mark stay unfenced.
  • A resume claims the run in one statement (interrupted, own, or stale). A resume that finds a live owner elsewhere, or loses the race to another resume, returns the journal snapshot instead of executing the script.
  • A stop on a process that does not own a live run publishes on the owner's pubsub channel fork_workflow_owner:<owner>. The journaled stop path stays for runs whose owner is gone, claims the run first, and fails the stop when its stopped write fails.
  • A terminal write that fails keeps the row running: the run keeps heartbeating and every tick retries the write, and Close tries once more before it releases. A journal read error no longer counts as ownership. A local stop confirms the ownership first and goes to the new owner when the run moved. Two resumes of one run on one process serialize from the claim to the registry. (Codex review fixes, f5a8f2a.)
  • A stop that finds a row owned by this process while the registry is empty takes the resume lock, so it stops the run a local resume registers instead of journaling a stop under it. An ended run whose terminal write never landed and whose row another process claimed leaves the registry. The journal snapshot of a run that another process executes keeps the rows' reuse flag. (Second Codex round, 82bde3b.)
  • A cleanup that cannot read the journal to confirm the ownership retries every heartbeat interval instead of ending the run with its steps still open. (Third Codex round, 7920bc2.)
  • The heartbeat also renews the row a resume claimed while it rehydrates the run, so a slow rehydration is not swept before the run is registered. (Fifth Codex round, 7c3920d.)
  • A journal-only stop keeps its claimed run in the heartbeat set until the stop lands. (Sixth Codex round, 2b09223.)
  • The resume lock is per run now (reserveRun), so a slow resume or journal-only stop of one run does not hold up the others, as on main, and a wait for it ends with the request context. The heartbeat renews every reserved run. (Seventh Codex round, d7b82eb.)
  • coderd/x/chatd/ARCHITECTURE.md gains a "Fork-workflows run ownership" section.

Hook lines in upstream files: chatd.go passes workerID to forkInitWorkflowJournal and calls forkCloseWorkflowJournal in Close.

Tests run

All with flock ~/.fork-go.lock, Postgres 16 on 127.0.0.1.

  • go test ./coderd/x/chatd/... -count=1: all 21 packages ok.
  • go test -race ./coderd/x/chatd/ -run TestForkWorkflowOwnership -count=5: ok. New subtests: NewProcessKeepsALiveRun, StaleRunResumesOnAnotherProcess, ConcurrentResumeExecutesOnce, StopFromAnotherProcessReachesTheOwner, HeartbeatNoticesALostRun, CloseReleasesTheRuns, and after the review TerminalWriteRetries, StopOnALostRunReachesTheNewOwner, AnEndedRunThatLostItsRowLeavesTheRegistry, StopWaitsForAResumeOfThisProcess, CleanupRetriesAFailedOwnershipCheck, JournaledStopReturnsAFailedWrite, HeartbeatRenewsAClaimWhileItRehydrates, HeartbeatRenewsAClaimWhileAJournaledStopRuns, ReservationOfOneRunLeavesOthersAlone (-race -count=2 -run 'Workflow|Waker', ok). No sleeps; the timing tests use a quartz mock clock.
  • go test -race ./coderd/x/chatd/ -run Workflow -count=1: ok.
  • go test ./coderd/database/... -count=1: ok except TestPubsub_Disconnect, which needs Docker (dial unix /var/run/docker.sock), not present in this workspace.
  • CHATDMIG_TEST_DATABASE_URL=... go test ./coderd/database/migrations/chatdmig/... -count=1: ok. chatdmig check: ok.
  • go test ./coderd/ -run TestForkWorkflowRunsRoutes -count=1 -v: 7 PASS.
  • TestWorkflowRunSweepPlan (forkdb): runs EXPLAIN (FORMAT JSON) of the sweep over 10000 rows, 1% running, after ANALYZE, and asserts the plan uses workflow_runs_running_idx and no Seq Scan.
  • golangci-lint run --new-from-merge-base=origin/main on the changed packages: 0 issues. Same with .golangci.new-code.yaml. make lint/emdash lint/architecture lint/markdown lint/migrations: ok.

Negative controls:

  • Dropping the index in the plan test fails it with "the sweep scans workflow_runs in full".
  • Dropping terminalPending from liveRuns fails TerminalWriteRetries (the peer sweep interrupts the ended run). Skipping the ownership check in stopHeldRun fails StopOnALostRunReachesTheNewOwner.
  • Second round: dropping the resume lock in stopOwnerlessRun fails StopWaitsForAResumeOfThisProcess ("the stop went past the resume"); skipping the eviction in recheckOwnership fails AnEndedRunThatLostItsRowLeavesTheRegistry; the old hardcoded Reused: true fails NewProcessKeepsALiveRun.
  • Third round: calling confirmOwnership once in interruptOpenSteps again fails CleanupRetriesAFailedOwnershipCheck (the retry timer is never created).
  • Fourth round: the old updateRunStatus call in stopJournaledRun fails JournaledStopReturnsAFailedWrite ("An error is expected but got nil").
  • Fifth round: leaving the claimed run out of the heartbeat fails HeartbeatRenewsAClaimWhileItRehydrates (expected "running", actual "interrupted").
  • Sixth round: dropping the claim from stopOwnerlessRun fails HeartbeatRenewsAClaimWhileAJournaledStopRuns (expected "running", actual "interrupted").
  • Seventh round: one process-wide reservation (every run keyed alike) fails ReservationOfOneRunLeavesOthersAlone (RequireReceive: context expired).
  • Turning the owner fence of UpdateWorkflowRunStatus into a no-op fails StaleRunResumesOnAnotherProcess ("the old owner's terminal write is fenced", "the fenced write left the new owner's row").

Not verified

  • No run against two real coderd processes or a real autodeploy; the multi-process cases use several Server instances on one test database.
  • Clock skew between replicas is not tested; the heartbeat and the sweep use each process's own clock, documented in ARCHITECTURE.md.
  • Known gaps, documented: await_workflow on a non-owner returns the journal snapshot at once instead of waiting, and a runner can send one step reminder before its next heartbeat notices it lost the run.
  • Size: about 1680 changed lines without generated sqlc and mock files, over the 500 line target. About 500 of them are the new tests; the rest is the fenced queries, their wrappers and the owner loop, which only work together.

A journaled workflow run now names the chatd process that executes it
(owner_id, the chatd worker ID) and carries its heartbeat. The startup
sweep and every heartbeat tick interrupt only runs whose heartbeat went
stale, so a new process leaves the live runs of other processes alone.
Close releases the runs of this process at once. Engine journal writes
are fenced by the owner, a resume claims the run in one statement, and a
stop on a non-owner reaches the owner through pubsub.

Part of #303
@ibetitsmike

Copy link
Copy Markdown
Owner Author

Coder Agents acted on behalf of @ibetitsmike.

@codex review

@chatgpt-codex-connector

chatgpt-codex-connector Bot commented Nov 17, 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-17T12:26:26.095100Z d7b82eb 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: 429313ae19

ℹ️ 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_owner.go Resolved
Comment thread coderd/x/chatd/fork_workflow_owner.go Resolved
Comment thread coderd/x/chatd/fork_workflow_owner.go Resolved
Comment thread coderd/x/chatd/fork_workflow_owner.go Resolved
Comment thread coderd/x/chatd/fork_workflow_journal.go Resolved
…wnership

A failed terminal write keeps the run heartbeating and retries the write,
so no sweep reopens a run that ended. A journal read error no longer
counts as ownership. A local stop confirms the ownership first and goes
to the new owner when the run moved. Resumes of one run on one process
serialize from the claim to the registry.
@ibetitsmike

Copy link
Copy Markdown
Owner Author

Coder Agents acted on behalf of @ibetitsmike.

@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: f5a8f2a062

ℹ️ 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_journal.go Resolved
Comment thread coderd/x/chatd/fork_workflow_owner.go Resolved
…ed copies

A stop that finds a run's row owned by this process while the registry
is empty now takes the resume lock, so it stops the run a resume
registers instead of journaling a stop under it. An ended run whose row
another process claimed leaves the registry. The journal snapshot of a
run that another process executes keeps the reuse flag of its rows.
@ibetitsmike

Copy link
Copy Markdown
Owner Author

Coder Agents acted on behalf of @ibetitsmike.

@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: 82bde3b6ef

ℹ️ 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_owner.go Resolved
@ibetitsmike

Copy link
Copy Markdown
Owner Author

Coder Agents acted on behalf of @ibetitsmike.

@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: 410aa627ab

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

Copy link
Copy Markdown
Owner Author

Coder Agents acted on behalf of @ibetitsmike.

@codex review

@chatgpt-codex-connector

Copy link
Copy Markdown

Codex Review: Didn't find any major issues. Can't wait for the next one!

Reviewed commit: 7920bc2854

ℹ️ 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".

@ibetitsmike

Copy link
Copy Markdown
Owner Author

Coder Agents acted on behalf of @ibetitsmike.

@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: 980a5872fc

ℹ️ 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

Copy link
Copy Markdown
Owner Author

$> Coder Agents acted on behalf of @ibetitsmike.\n\n@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: 7c3920d169

ℹ️ 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
@ibetitsmike

Copy link
Copy Markdown
Owner Author

Coder Agents acted on behalf of @ibetitsmike.

@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: 2b09223728

ℹ️ 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_owner.go Resolved
Comment thread coderd/x/chatd/fork_workflow_journal.go Resolved
@ibetitsmike

Copy link
Copy Markdown
Owner Author

Coder Agents acted on behalf of @ibetitsmike.

@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: d7b82eba65

ℹ️ 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
@ibetitsmike

Copy link
Copy Markdown
Owner Author

Coder Agents acted on behalf of @ibetitsmike.

@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: d7b82eba65

ℹ️ 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

Copy link
Copy Markdown
Owner Author

Coder Agents acted on behalf of @ibetitsmike.

@codex review

@chatgpt-codex-connector

Copy link
Copy Markdown

Codex Review: Didn't find any major issues. More of your lovely PRs please.

Reviewed commit: d7b82eba65

ℹ️ 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".

@ibetitsmike
ibetitsmike merged commit da6d496 into main Nov 17, 2026
10 checks passed
@ibetitsmike
ibetitsmike deleted the mike/issue-303-run-owner branch November 17, 2026 12:48
@ibetitsmike

Copy link
Copy Markdown
Owner Author

Coder Agents acted on behalf of @ibetitsmike.

Merged at head d7b82eba650880824e8291a46cbf27437fd0e996 as da6d496, on main ed5b24c331fdb626a3a5180236c4874825ac3317, with the server running that main.

Gates on this exact head:

  1. The head matched the SHA I verified (--match-head-commit).
  2. Every check run passed. fork-ci run 44077037374 passed on this head.
  3. Codex receipt for this head: feat(chatd): give workflow runs an owner and a heartbeat #379 (comment) ("Didn't find any major issues").
  4. No unresolved review thread.
  5. 0 commits behind main.
  6. I ran the tests below on this exact head.

Tests I ran on d7b82eba65 (detached worktree, under flock ~/.fork-go.lock):

  • go test -count=1 -race -v -run TestForkWorkflowOwnership ./coderd/x/chatd/: 16 PASS, 0 FAIL.
  • go test -count=1 -race -v -run Workflow ./coderd/x/chatd/: 159 PASS, 0 FAIL.
  • go test -count=1 ./coderd/x/chatd/...: 21 packages ok.
  • go test -count=1 -v ./coderd/database/forkdb/...: 74 PASS, 0 FAIL. This includes the EXPLAIN test that asserts the sweep uses workflow_runs_running_idx.
  • go test -count=1 -run TestMethodTestSuite ./coderd/database/dbauthz/, ./coderd/database/dbpurge/: ok.
  • go test -count=1 -v -run TestForkWorkflowRunsRoutes ./coderd/: 7 PASS, 0 FAIL.
  • Negative control: with the stale-heartbeat filter of MarkStaleWorkflowRunsInterrupted made always true, TestForkWorkflowOwnership/NewProcessKeepsALiveRun fails. The source was restored after the control.

Review notes:

Not exercised:

  • two real coderd processes, and a real autodeploy restart;
  • clock skew between replicas.

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.

chatd: workflow runs have no owner, and a waiting step polls and rescans chat history

1 participant