Skip to content

fix(coderd/x/chatd): make replayed notes calls change a note once - #315

Merged
ibetitsmike merged 1 commit into
mainfrom
mike/issue-283-token-budget-notes-replay
Nov 14, 2026
Merged

ibetitsmike merged 1 commit into
mainfrom
mike/issue-283-token-budget-notes-replay

Conversation

@ibetitsmike

Copy link
Copy Markdown
Owner

Coder Agents opened this PR on behalf of @ibetitsmike.

Part of #283

Problem

Codex raised this as a P2 on #285. Tool calls run at least once. If the server restarts after a notes append committed but before the tool result was saved, the call runs again on resume and the note gets the same text twice. A replayed write or delete is harmless on its own, but it can undo a later change in the same batch.

Change

  • New chatdmig node 01a1d903-ff52-75e8-91c2-e50d900ac0b7_chat_note_calls adds the table chat_note_calls. It requires the chat_notes node. It records each notes call that changed a note, keyed by the tool call ID, with the result the call returned.
  • WriteChatNote and DeleteChatNote are now one statement each. The change runs only when the call ID is not recorded yet, and the record is written in the same statement. Records older than one day are dropped in the same statement, so the table stays small.
  • GetChatNoteCall reads a record. dbauthz authorizes it as a read of the chat.
  • fork_chat_notes.go takes the call ID from workspacesdk.ToolCallIDFromContext. A recorded call returns its recorded result and changes nothing. Two runs of the same call can race, so after a write error or a delete that removed nothing, the tool reads the record again and returns it if the other run won. This follows the fix(coderd/x/chatd): deliver a replayed message_agent message once #282 re-read pattern.
  • Calls without a call ID run as before.
  • Docs: coderd/x/chatd/ARCHITECTURE.md lists the table and the at-least-once exception. docs/fork/pstack-adapter.md has a user sentence and evidence lines.

Tests

  • TestChatNotesToolReplay in coderd/x/chatd/fork_chat_notes_internal_test.go:
    • a replayed batch returns identical results and does not change the notes
    • 8 concurrent runs of one append call append once
    • calls without an ID always run
    • records older than one day are dropped
  • Without the Go change the test fails (log.md has 11 bytes, want 7).

Commands I ran, all under flock ~/.fork-go.lock:

  • go build ./coderd/... and go vet: passed
  • go run ./coderd/database/migrations/chatdmig/cmd/chatdmig check: passed
  • go test ./coderd/database/migrations/chatdmig/... ./coderd/database/forkdb/... ./coderd/database/dbauthz/ ./coderd/database/dbmetrics/ with CHATDMIG_TEST_DATABASE_URL set: passed
  • go test -count=5 -race -run 'TestChatNotesTool' ./coderd/x/chatd/: passed
  • go test -count=1 ./coderd/x/chatd/... ./coderd/database/dbauthz/ ./coderd/database/forkdb/...: passed
  • golangci-lint run --config=.golangci.new-code.yaml --new-from-merge-base=origin/main on the touched packages: 0 issues
  • make lint/emdash lint/markdown lint/architecture lint/agents: passed

Not verified

  • A real server crash between the note change and the saved tool result. The test replays the call directly.

@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 14, 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-14T07:57:51.053612Z ac06a9c 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. Can't wait for the next one!

Reviewed commit: ac06a9c93e

ℹ️ 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 6ae5568 into main Nov 14, 2026
10 checks passed
@ibetitsmike
ibetitsmike deleted the mike/issue-283-token-budget-notes-replay branch November 14, 2026 08:08
@ibetitsmike

Copy link
Copy Markdown
Owner Author

Coder Agents acted on behalf of @ibetitsmike.

Merged as 6ae5568 (squash) at 08:08Z. Before the merge, the server was confirmed on the previous deploy, 9c4c333.

Gates on head ac06a9c:

  1. The head matched the SHA I verified, and main was 9c4c333 at merge time.
  2. All check runs passed. fork-ci run 43573299984 passed on this exact head.
  3. Codex receipt: issuecomment-6874187627, "Didn't find any major issues", reviewed commit ac06a9c.
  4. 0 unresolved threads.
  5. The branch was 0 commits behind main.
  6. My tests on the exact head, run under flock:
    • chatdmig check: passed, and the graph lists the new node 01a1d903..._chat_note_calls, which requires chat_notes.
    • CHATDMIG_TEST_DATABASE_URL=... go test -count=1 ./coderd/database/migrations/chatdmig/... ./coderd/database/forkdb/...: passed.
    • go test -run '(TestForkQuerier)' ./coderd/database/dbauthz/: passed.
    • go test -count=1 -race -v -run '(TestChatNotesTool|TestChatHistoryTool|TestForkMessageAgentReplay)' ./coderd/x/chatd/: 5 top-level tests passed, including TestChatNotesToolReplay.
    • I also reviewed the migration and the queries. The new table is keyed by the tool call UUID from workspacesdk.ToolCallIDFromContext and cascades on chat delete. Every query is scoped to the chat and uses bound parameters.

Not exercised: a real server crash between the note change and the saved tool result. The test replays the call directly. Records expire after 1 day, so a replay later than that would apply again. That is accepted as out of scope.

This fixes the P2 that was dropped on #285. Part of #283.

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