Skip to content

fix(coderd/x/chatd): spawn each child once when a tool batch is replayed - #280

Merged
ibetitsmike merged 7 commits into
mainfrom
mike/issue-252-spawn-replay
Nov 14, 2026
Merged

ibetitsmike merged 7 commits into
mainfrom
mike/issue-252-spawn-replay

Conversation

@ibetitsmike

@ibetitsmike ibetitsmike commented Nov 13, 2026 •

Copy link
Copy Markdown
Owner

Coder Agents opened this PR on behalf of @ibetitsmike.

Part of #252

Problem

When chatd stops while a tool batch runs, the next worker runs the whole batch again with the same tool call IDs. A spawn_agent call in that batch then creates a second child. The restart test of #279 showed it: a quick worker spawned in the same batch as three blocking wait_agent calls existed twice after the restart.

Fix

Behind fork-nested-subagents:

  • fork_spawn_replay.go wraps every spawn_agent tool. With a tool call ID in the context, it derives candidate child IDs: id0 = uuid.NewSHA1(callID, "spawn_agent"), then id(n+1) = uuid.NewSHA1(id(n), "spawn_agent"), at most 4 per call.
  • For each candidate it looks up the chat:
    • Absent: the inner tool runs with that ID in the context. forkChildChatID(ctx) (the existing hook in createChildSubagentChatWithOptions) uses it. A workflow step ID still wins.
    • Ready (an unarchived, started child of the caller): the call returns it and creates nothing.
    • Retired (archived, or a workspace isolation child whose workspace was never recorded): an unarchived one is discarded, then the next ID is tried.
    • Owned by another parent: the call fails.
  • If the inner tool fails, the wrapper checks the ID again and returns the child when a concurrent run created it.
  • If the inner tool fails because the worktree already exists (HTTP 409 from ForkCreateWorktree, flagged by one hook line in createSubagentWorktree), the next ID is tried, so the replay gets a fresh branch.
  • Database errors come back as tool error responses.
  • Owner fence: the wrapper puts the turn's chat in the context. createSubagentChatWithID calls forkCheckSpawnOwner first in its transaction (one hook line). It takes a share lock on the root and the parent and refuses the child when the parent's worker_id or runner_id differs from the turn's. So a run that a takeover replaced, and that still goes on, creates no child after the new run moved past its 409. The share lock makes a new acquire of the parent wait until a child of the old run commits, so the replay then sees that child.
  • Retirement of an unstarted workspace child is serialized with its start. The wrapper calls discardSubagentUnless with a check that runs under the child's lock and keeps the child when its workspace was recorded meanwhile. createSubagentWorkspace refuses to record a workspace or send the prompt to an archived child. discardSubagent now also stops the bound workspace of a child that was archived already, so a run that bound a workspace after a replay archived its child still stops that workspace.
  • The wrapper sits right after the nested tools, under the isolation, stored type and computer use wrappers, so their result decoration still applies.
  • ARCHITECTURE.md describes the replay safe spawn and that a graceful shutdown replays a cut batch.

Tests

  • TestForkSpawnAgentReplay: two runs with one tool call ID, from a root and from a child, give one child each. A new call ID gives a new child. A derived ID owned by another parent fails.
  • TestForkSpawnAgentReplayRetiredChild (Archived, UnstartedWorkspace): the replay skips the retired child and creates a new one.
  • TestForkSpawnAgentReplayWorktreeConflict: a 409 on the first worktree makes the replay use the next ID.
  • TestForkSpawnAgentReplayConcurrentRun: an inner error after a concurrent insert returns the existing child.
  • TestForkSpawnAgentReplayTakenOver: a turn with a runner ID other than the chat's creates no child, and a turn of the current runner does. It fails without the forkCheckSpawnOwner line (verified by removing it).
  • TestForkSpawnAgentReplayRetiredWhileStarting: ArchivedBeforeBind retires the child while the original run creates its workspace; the spawn fails, the child gets no prompt and its workspace is stopped. KeepsStarted keeps a child whose workspace was recorded. Each of the three parts (the archived check, the stop of an already archived child, the keep check) was removed in turn, and the test failed each time.
  • Each new test fails with the wrapper of the first commit (verified by swapping that file back in).
  • flock ~/.fork-go.lock go test ./coderd/x/chatd -run 'TestForkSpawnAgentReplay' -count=1: ok.
  • flock ~/.fork-go.lock go test ./coderd/x/chatd -run '(?i)spawn|subagent|workflow|isolation|computeruse|agentdefinition' -count=1: ok.
  • End to end: with the restart harness of test(coderd/x/chatd): prove a busy chat family survives a chatd restart #279 plus its spawn probe copied in temporarily, TestChatFamilyRestartReplaysSpawnAgentOnce and TestChatFamilySurvivesRestart passed twice each (-count=2). Without this fix the probe fails with 4 children expected and 5 found. The probe is not committed here. It lands with the harness after test(coderd/x/chatd): prove a busy chat family survives a chatd restart #279.
  • golangci-lint run --fix=false ./coderd/x/chatd/...: no new findings.

Not covered

  • An orphan worktree left by a crashed earlier run stays in place. The replay moves to a new branch instead of reusing or removing it.
  • A workspace bound to a retired child by a run that crashed before its cleanup stays running.
  • TestForkSpawnAgentReplayConcurrentRun uses a stub inner tool, not a real race between two workers.
  • A call that needs more than 4 candidate IDs fails.
  • A call with an empty or repeated tool call ID has no stable identity to derive from, so it keeps the upstream behavior and is not deduplicated.

@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 13, 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-13T23:35:40.578246Z 3898c2d 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: def544736d

ℹ️ 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_spawn_replay.go Resolved
Comment thread coderd/x/chatd/fork_spawn_replay.go Resolved
Comment thread coderd/x/chatd/ARCHITECTURE.md Resolved
Comment thread coderd/x/chatd/fork_spawn_replay.go Resolved
@ibetitsmike

Copy link
Copy Markdown
Owner Author

Coder Agents acted on behalf of @ibetitsmike.

@codex review

@ibetitsmike

Copy link
Copy Markdown
Owner Author

Coder Agents acted on behalf of @ibetitsmike.

Merged main (#278) into the branch. @codex review

@chatgpt-codex-connector

Copy link
Copy Markdown

Codex Review: Didn't find any major issues. Hooray!

Reviewed commit: 35f4c2fa8f

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

@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: 38d0b4d193

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

Copy link
Copy Markdown
Owner Author

Coder Agents acted on behalf of @ibetitsmike.

@codex review

# Conflicts:
#	coderd/x/chatd/fork_tools.go
@ibetitsmike

Copy link
Copy Markdown
Owner Author

Coder Agents acted on behalf of @ibetitsmike.

Merged main (#281) into the branch. @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: 7bd806209c

ℹ️ 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_spawn_replay.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. Another round soon, please!

Reviewed commit: 4c3813dddd

ℹ️ 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.\n\nMerged main 607a95b (#279) with a merge commit. The merge was clean. TestChatFamilySurvivesRestart and the spawn replay tests pass on the merged tree.\n\n@codex review

@chatgpt-codex-connector

Copy link
Copy Markdown

Codex Review: Didn't find any major issues. Breezy!

Reviewed commit: 3898c2d360

ℹ️ 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 5c34d7e into main Nov 14, 2026
9 checks passed
@ibetitsmike
ibetitsmike deleted the mike/issue-252-spawn-replay branch November 14, 2026 00:12
@ibetitsmike

Copy link
Copy Markdown
Owner Author

Coder Agents acted on behalf of @ibetitsmike.

Merged as 5c34d7e (squash, --match-head-commit 3898c2d360).

Gates on head 3898c2d:

  1. Head SHA matched the verified SHA at merge time. Main was 607a95b and the server ran it.
  2. Every check run on the head passed. fork-ci run 43527895636 on this head passed.
  3. Codex receipt on this head: fix(coderd/x/chatd): spawn each child once when a tool batch is replayed #280 (comment)
  4. Unresolved review threads: 0.
  5. Behind main: 0.
  6. Test evidence run by the maintainer on the exact head: go test -count=1 -v -run '(TestChatFamilySurvivesRestart$|TestForkSpawnAgentReplay|TestForkSpawnIsolationWorkspace)' ./coderd/x/chatd/ passed: TestChatFamilySurvivesRestart, six TestForkSpawnAgentReplay* tests, TestForkSpawnIsolationWorkspace.

Not exercised: a real server restart during a live spawn_agent batch on the deployed server. Replay is covered by the DB-backed tests above.

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