Skip to content

fix(coderd/x/chatd): deliver a replayed message_agent message once - #282

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

ibetitsmike merged 7 commits into
mainfrom
mike/issue-252-message-agent-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 in the middle of a tool batch, the next worker runs the whole batch again with the same tool call IDs. A message_agent call that had already delivered its message in that batch delivers it again, so the child gets the same message twice. The restart probe from #252 reproduced this on main: the child received its NOTE twice.

Fix

Behind fork-nested-subagents, which owns the nested subagent tools:

  • New table chatd.chat_subagent_messages (migration node 01a1d6ec). id is the tool call ID, with organization_id (copied from the sender chat, as in chat_family_messages), sender_chat_id, recipient_chat_id and created_at. Rows cascade on delete of either chat and of the organization.
  • fork_message_agent_replay.go wraps message_agent. A call with a tool call ID first looks up its row by (id, organization, sender). When the row exists, the call returns the recipient and sends nothing. Otherwise it runs the tool with the call ID in its context. When that send fails, the wrapper reads the row again and returns the recipient if another run of the same call recorded it meanwhile, for example a run that a takeover left going. The losing send rolls back on the primary key, so the message goes out once.
  • sendSubagentMessage passes a forkAdmit hook. For a send from the wrapper, the hook inserts the row in the same SendMessage transaction that delivers the message, and deletes the sender's rows in its organization that are 24 hours old or older. Workflow steps call the same function without the context key and record nothing.
  • dbauthz: the lookup reads the sender chat, the insert updates the recipient chat, and the delete updates the sender chat. dbmetrics wraps the three queries.
  • ARCHITECTURE.md lists the table and describes the mechanism next to the family message dedupe. The at least once paragraph lists message_agent and message_family_chat as replay safe.

Tests

  • flock ~/.fork-go.lock go test ./coderd/x/chatd -run 'TestForkMessageAgentReplay$' -count=1: ok. With the wrapper line in fork_tools.go removed it fails with expected: 1, actual: 2 ("a replay does not send again").
  • flock ~/.fork-go.lock go test ./coderd/x/chatd -run '^TestForkMessageAgentReplay' -count=1 -v: both tests pass. TestForkMessageAgentReplayConcurrentRun records the call just before the real send runs. With the re-read disabled it fails with the duplicate key tool error.
  • After merging main (test(coderd/x/chatd): prove a busy chat family survives a chatd restart #279, fix(coderd/x/chatd): spawn each child once when a tool batch is replayed #280): flock ~/.fork-go.lock go test ./coderd/x/chatd -run '(?i)(messageagent|message_agent|familymessage|subagent|family|spawn|archive)' -count=1 -v: ok, 156 top-level tests passed, none failed or skipped, including TestChatFamilySurvivesRestart. The regex is grouped because Go splits -run on top-level |, so an ungrouped (?i) covers only the first alternative (fork-ci: Chat API tests step skips skill, MCP and plugin tests #286).
  • flock ~/.fork-go.lock go test ./coderd/database/forkdb/... -count=1: ok, including the new TestChatSubagentMessages against Postgres.
  • flock ~/.fork-go.lock go test ./coderd/database/dbauthz -run TestForkQuerier -count=1: ok.
  • flock ~/.fork-go.lock go test ./coderd/database/migrations/chatdmig/... -count=1: ok.
  • flock ~/.fork-go.lock go test ./coderd/x/chatd -run '(?i)message|subagent|family|secret|workflow' -count=1: ok before the main merge. This ungrouped regex ran only part of the matching tests; the grouped run above is the full set.
  • End to end: with the restart probe TestChatFamilyRestartReplaysMessageAgentOnce copied in temporarily, go test ./coderd/x/chatd -run 'TestChatFamilyRestartReplaysMessageAgentOnce$|TestChatFamilySurvivesRestart$' -count=2: ok. The probe is not committed here; it joins the harness from test(coderd/x/chatd): prove a busy chat family survives a chatd restart #279 once this lands.
  • golangci-lint run --fix=false on the changed packages: no findings in the changed files.

Not covered

  • The record lives 24 hours. A replay of a batch whose chat stayed unacquired for longer than that would send again.
  • message_agent calls without a tool call ID run as before. chatd always sets one for model tool calls.
  • message_family_chat has the same check then insert pattern without the re-read, so a concurrent run of a family message reports a failure after the other run delivered it. That code is outside this PR.

@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-14T00:27:18.076613Z 314f266 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: 8a698decdd

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

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: 403b01fdaa

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

Added organization scope to the replay records in 2487f77. @codex review

@chatgpt-codex-connector

Copy link
Copy Markdown

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

Reviewed commit: 2487f77b4c

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

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

@chatgpt-codex-connector

Copy link
Copy Markdown

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

Reviewed commit: 36da8f9023

ℹ️ 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 5c34d7e (#280) with a merge commit, which was clean. A follow-up docs commit lists message_agent and message_family_chat as exceptions in the at-least-once paragraph of ARCHITECTURE.md. The chatd replay tests, the forkdb, dbauthz and chatdmig tests, and chatdmig check all pass on the merged tree.\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: 5efe64e722

ℹ️ 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\nPushed 314f266. It fixes the concurrent replay insert: on a failed send, the wrapper reads the record again and returns the recipient. A new test covers this. I answered the organization index finding: the index layout matches chat_family_messages.\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: 314f266ce3

ℹ️ 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 94caf1e into main Nov 14, 2026
10 checks passed
@ibetitsmike
ibetitsmike deleted the mike/issue-252-message-agent-replay branch November 14, 2026 00:38
@ibetitsmike

Copy link
Copy Markdown
Owner Author

Coder Agents acted on behalf of @ibetitsmike.

Merged as 94caf1e (squash, --match-head-commit 314f266ce3).

Gates on head 314f266:

  1. Head SHA matched the verified SHA at merge time. Main was 5c34d7e and the server ran it.
  2. Every check run on the head passed. fork-ci run 43533860456 on this head passed.
  3. Codex receipt on this head: fix(coderd/x/chatd): deliver a replayed message_agent message once #282 (comment)
  4. Unresolved review threads: 0.
  5. Behind main: 0.
  6. Test evidence run by the maintainer on the exact head, all passed:
    • go test -count=1 -v -run '(?i)(messageagent|message_agent|familymessage|subagent|family|spawn|archive)' ./coderd/x/chatd/
    • go test -count=1 -v -run '^(TestForkMessageAgentReplay|TestChatFamilySurvivesRestart$)' ./coderd/x/chatd/ (TestForkMessageAgentReplay, TestForkMessageAgentReplayConcurrentRun, TestChatFamilySurvivesRestart)
    • go test -count=1 ./coderd/database/forkdb/...
    • go test -count=1 -run '^TestForkQuerier$' ./coderd/database/dbauthz/
    • go test -count=1 ./coderd/database/migrations/chatdmig/... against local Postgres, and chatdmig check (exit 0, new node 01a1d6ec chat_subagent_messages)

Not exercised: the new chatdmig node applying on the deployed server, and a real restart during a live message_agent batch. The same race in message_family_chat is filed as #287.

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