Skip to content

feat(chatd): end wait_agent early for a pending family message - #428

Merged
ibetitsmike merged 2 commits into
mainfrom
mike/issue-422-wait-agent-family-messages
Nov 23, 2026
Merged

ibetitsmike merged 2 commits into
mainfrom
mike/issue-422-wait-agent-family-messages

Conversation

@ibetitsmike

Copy link
Copy Markdown
Owner

Coder Agents opened this PR on behalf of @ibetitsmike.

Closes #422

Problem

A parent blocked in wait_agent got a message_family_chat message only after the wait ended. Family messages are steer queued messages, and steers reach the model only at a delivery point (before each model call, FinishTurn, FinishInterruption). awaitSubagentCompletion watches only the child, so a message sat in the queue until the child finished or the wait timed out.

Design

Behind the existing fork-family-messages experiment, appendForkTools (fork file, no upstream hook line) wraps wait_agent last in familyMessageWaitTool (coderd/x/chatd/fork_family_message_wait.go).

  • For a chat the caller may wait on (isSubagentTarget), the wrapper subscribes to the waiting chat's ChatStateUpdateChannel before the first read, reads the queue, then reads it again on each notification and on a 5 second fallback tick (200 ms if the subscription fails).
  • When the steer lane holds a message whose text starts with the family envelope (<agent-message from_chat_id="), it cancels the inner wait and returns {chat_id, family_message_pending: true, note, status, title}.
  • A steer under edit holds the whole lane, so the wrapper keeps waiting while one exists. That avoids a loop where every wait returns at once but nothing is delivered.
  • The wrapper never changes the queue. The next delivery point moves the message into history once, as for any steer. So no message is lost or delivered twice, and a wait_agent call replayed after a restart reads the queue again and returns at once if the message is still queued.
  • A wait that completes while a message arrives keeps its own result. If the cancel lands in an inner fork wrapper that still reads more after the child finished (the nested subagent question read, diff summary), and the child has finished, the wrapper runs the wait again with a 5 second bound and returns the real result.
  • familyMessageEnvelopePrefix is a new constant in fork_family_messages.go. The envelope text is unchanged.

Tradeoffs:

The same design is written as prose in the fork section of coderd/x/chatd/ARCHITECTURE.md.

Tests

All run in coderd/x/chatd:

  • flock ~/.fork-go.lock go test ./ -run 'TestWaitAgentEndsForFamilyMessage' -count=1 -v: PASS (0.31s). The parent spawns a child and calls wait_agent with timeout_seconds 3600. The child sends message_family_chat and then blocks. The test asserts the parent's next model call sees the message, there is one wait_agent result with family_message_pending true and status running, the message is in history once, the queue is empty and the child is still running.
  • Negative control: with the hook in fork_tools.go replaced by _ = p.withFamilyMessageWait, the same test FAILS after 25.13s at the Eventually deadline (fork_family_message_wait_test.go:122). Hook restored.
  • TestFamilyMessageWaitPending and TestFamilyMessageWaitTool (internal): user steer is not pending, a family message is, a steer under edit holds the lane; experiment off leaves the tool unwrapped; a blocking wait ends for a family message behind a user steer and leaves both queued; a wait started with the message already queued (replay) ends at once; a finished child whose inner wrapper was canceled gets its real result from a second wait; a completed wait keeps its result.
  • TestChatFamilySurvivesRestart is adapted: it asserted that family messages stay queued during wait_agent, which this change fixes on purpose. It now checks that each blocked worker's message is delivered once before and after the restart, and that every wait result that is not a report has family_message_pending true.
  • flock ~/.fork-go.lock go test ./ -run 'TestFamilyMessageWait|TestWaitAgentEndsForFamilyMessage|TestChatFamilySurvivesRestart' -count=5 -race: PASS. A read-only reviewer ran the same set with -count=25 -race: PASS.
  • flock ~/.fork-go.lock go test ./coderd/x/chatd/... -count=1: PASS.
  • golangci-lint run --config=.golangci.new-code.yaml --new-from-merge-base=origin/main ./coderd/x/chatd/: 0 issues. Full config on the package: no findings in changed files.

Not exercised: a real coderd restart on a live server, and the 200 ms poll path when the pubsub subscription fails.

Size: 604 added lines, about 390 of them tests.

Pre-commit hook

The pre-commit hook is off in this worktree only (git config --worktree coder.pre-commit false, see #407). Targeted checks run instead: gofmt -l, the tests above, golangci-lint (new-code config and full config on coderd/x/chatd), make lint/emdash, make lint/architecture, make lint/typos (only findings already on main, none in changed files).

@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 commented Nov 23, 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-23T06:47:57.865344Z 88fc781 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

Copy link
Copy Markdown

Codex Review: Didn't find any major issues. Keep it up!

Reviewed commit: 88e33ff2d0

ℹ️ 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\n@codex review

@chatgpt-codex-connector

Copy link
Copy Markdown

Codex Review: Didn't find any major issues. You're on a roll.

Reviewed commit: 88fc781d01

ℹ️ 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 7321fda into main Nov 23, 2026
9 checks passed
@ibetitsmike
ibetitsmike deleted the mike/issue-422-wait-agent-family-messages branch November 23, 2026 07:27
@ibetitsmike

Copy link
Copy Markdown
Owner Author

Coder Agents acted on behalf of @ibetitsmike.

Merged at head 88fc781d0173ce4b28ff892a138d1f3247bac8f1 as 7321fda, on main d191dbb5c96b1eca7c0eadeb3598147410418bd5, 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 45126258571 passed on this head.
  3. Codex receipt for this head: feat(chatd): end wait_agent early for a pending family message #428 (comment) ("Didn't find any major issues", Reviewed commit 88fc781d01).
  4. No review threads.
  5. 0 commits behind main.
  6. Test evidence I ran in the PR worktree at 88e33ff2d0. The only later commit merges main, and the PR diff against main is line for line the same as at that head:
    • flock ~/.fork-go.lock go test ./coderd/x/chatd -run 'TestWaitAgentEndsForFamilyMessage|TestFamilyMessageWait|TestChatFamilySurvivesRestart' -count=1: ok.
    • Negative control: with the withFamilyMessageWait hook in appendForkTools disabled, TestWaitAgentEndsForFamilyMessage fails at its 25 s deadline. Reverted, and the worktree was clean afterwards.
    • The workstream also ran the same set with -count=5 -race and go test ./coderd/x/chatd/... -count=1: pass.

What this changes: behind fork-family-messages, a family message queued for a chat that is blocked in wait_agent ends the wait at once. The tool returns the child's ID, title and status with family_message_pending: true, and the message reaches the next model call. The queue is never changed. Closes #422.

Not exercised: a live coderd restart during a wait, and the 200 ms poll path used when the pubsub subscription fails.

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: a parent blocked in wait_agent gets family messages only after the wait ends

1 participant