Skip to content

fix: send only changed chat history over the stream - #29662

Merged
mafredri merged 95 commits into
mainfrom
fix/chat-stream-history-replay
Nov 13, 2026
Merged

mafredri merged 95 commits into
mainfrom
fix/chat-stream-history-replay

Conversation

@mafredri

@mafredri mafredri commented Sep 21, 2026 •

Copy link
Copy Markdown
Member

A client can resume the chat stream (GET /api/v2/chats/{chat}/stream) only with after_id, a message ID. A message ID does not identify what a client received:

  • Another client sent a message with a lower ID than after_id. A reconnect skips that message.
  • The client missed an edit and then sent a message. A reconnect with that message's ID sends no history_reset, so the client keeps the messages that the edit deleted, and it never receives the messages written between the edit and its own message.
  • after_id is the ID of a message that an edit deleted. The stream sends history_reset and then every message in the chat that isn't deleted.

No messages page or stream event includes the chat's snapshot_version, so a client also cannot tell whether a page is older than a stream event.

This PR adds snapshot_version to the newest messages page and to the message, queue_update and history_reset events, and a snapshot_version parameter to the stream. A stream opened with the parameter sends what changed at that snapshot version or later. For an edit it sends one history_reset with from_message_id, the ID of the edited message, and then only the messages written by the edit or after it. This implements H1 to H4 of the RFC Chat stream contract: one copy of each chat in the web client, which describes the design and the alternatives. #30478 changes the web client to use it.

A stream opened without snapshot_version sends the same events as on main. One change applies to every stream: when the server can't subscribe to the chat or read it, it returns HTTP 500 before the WebSocket upgrade. main upgrades and sends an error event, which the web client shows as a chat error.

Two changes are not part of the RFC:

  • Six queries that only tests called are removed. They write messages or the queue outside a chat state transition. Such a write does not increase snapshot_version, so an open stream sends it late or never. SoftDeleteChatMessageByID is also removed: an edit now deletes the edited message in the same SoftDeleteChatMessagesAfterID call as the messages after it.
  • Step 4 of the Tasks to Chats migration guide told clients to reconnect with after_id and described message envelopes that the server does not send. It now describes snapshot_version.
Decision log

Implementation choices that the RFC does not state. The code comment at each one gives the reason.

  • The messages page reads the chat's snapshot_version, the messages and the queue in separate statements, without a transaction. The page can therefore include changes with a higher snapshot version, and the stream sends those changes again. The API reference states this.
  • The stream finds edits from the deleted rows: one history_reset for each distinct revision among them, with the lowest ID of that revision as from_message_id. It reads no full history (versionedMessageEvents).
  • The loop builds the events as on main, then sorts them by snapshot version with a stable sort (applyDBSnapshot).
  • subscribeWithInitialError is deleted. SubscribeAuthorized returns ok == false for a setup failure, and the handler returns HTTP 500 before the upgrade.

🤖 This PR was created with the help of Coder Agents, and will be reviewed by a human. 🏂🏻

@mafredri
mafredri force-pushed the fix/chat-stream-history-replay branch 2 times, most recently from ad7a0f3 to f9b402f Compare September 22, 2026 13:45
@mafredri mafredri changed the title fix: sync chat history incrementally over the stream fix: start the chat stream sync from the client's page version Sep 22, 2026
@mafredri
mafredri force-pushed the fix/chat-stream-history-replay branch 2 times, most recently from a82cfd8 to 381fb14 Compare September 22, 2026 14:53
@linear-code

linear-code Bot commented Sep 30, 2026 •

Copy link
Copy Markdown

CODAGT-93

CODAGT-1115

@mafredri
mafredri force-pushed the fix/chat-stream-history-replay branch from 381fb14 to cb8ddd6 Compare October 7, 2026 10:50
@mafredri mafredri changed the title fix: start the chat stream sync from the client's page version fix: send only changed chat history over the stream Oct 7, 2026
@mafredri

This comment was marked as outdated.

@coder-agents-review

coder-agents-review Bot commented Oct 7, 2026 •

Copy link
Copy Markdown
Contributor

Chat: Review posted | View chat
Requested: 2026-10-30 19:56 UTC by @mafredri

Review history
  • R1 (2026-10-07): 13 reviewers, 2 Nit, 7 P3, 5 P4, COMMENT. Review
  • R2 (2026-10-08): 14 reviewers, 6 Nit, 8 P3, 8 P4, COMMENT. Review
  • R3 (2026-10-08): 11 reviewers, 7 Nit, 9 P3, 9 P4, COMMENT. Review
  • R4 (2026-10-09): 9 reviewers, 8 Nit, 9 P3, 11 P4, COMMENT. Review
  • R5 (2026-10-09): 7 reviewers, 8 Nit, 9 P3, 13 P4, COMMENT. Review
  • R6 (2026-10-09): 7 reviewers, 8 Nit, 9 P3, 14 P4, COMMENT. Review
  • R7 (2026-10-09): 5 reviewers, 9 Nit, 9 P3, 14 P4, COMMENT. Review
  • R8 (2026-10-26): 14 reviewers, 15 Nit, 13 P3, 25 P4, COMMENT. Review
  • R9 (2026-10-26), 15 Nit, 13 P3, 25 P4, COMMENT. Review
  • R10 (2026-10-26): 10 reviewers, 19 Nit, 13 P3, 28 P4, COMMENT. Review
  • R11 (2026-10-27): 7 reviewers, 19 Nit, 13 P3, 28 P4, COMMENT. Review
  • R12 (2026-10-27): 4 reviewers, 19 Nit, 13 P3, 29 P4, COMMENT. Review
  • R13 (2026-10-27): 4 reviewers, 19 Nit, 13 P3, 29 P4, COMMENT. Review
  • R14 (2026-10-30): 4 reviewers, 19 Nit, 13 P3, 29 P4, COMMENT. Review
  • R15 (2026-10-30): 4 reviewers, 19 Nit, 13 P3, 29 P4, COMMENT. Review

deep-review v0.13.0 | Round 15 | f0ddf43..36d77b8

Last posted: Round 15, 61 findings (13 P3, 29 P4, 19 Nit), COMMENT. Review

Finding inventory

Finding inventory: PR #29662

Findings

# Sev Status Location Summary Round Reviewer Posted
CRF-1 P3 Author fixed (d69899f) stream_loop.go:229 requires_action history selection untested for either reset kind R1 Netero Yes
CRF-2 P4 Author fixed (8a8e660) stream_loop.go:205 min loop re-derives lowest ID the query ORDER BY already guarantees R1 Netero Yes
CRF-3 Note Dropped by orchestrator (superseded by CRF-47 at P4) exp_chats.go:3550 after_revision above chat history_version is accepted and suppresses sync R1 Netero No
CRF-4 Note Author fixed (bfdc902) useChatStore.ts:183 Remount reconnects from cached page's original history_version R1 Netero No
CRF-5 P3 Deferred (#30478) useChatStore.ts:664 Reconnect cursor never lowered when a stale REST page rolls the store back R1 Hisoka P3, Melody P3, Mafuuu P3 Yes
CRF-6 P3 Author fixed (f555159) stream_loop.go:102 Mid-turn reconnect on unedited chat resends all steps since last status R1 Pariston Yes
CRF-7 P3 Author fixed (f555159) stream_loop.go:303 status.history_version sent on after_id-only streams and adopted as cursor R1 Razor Yes
CRF-8 P4 Author fixed (99be3f6) codersdk/chats.go:3146 Docs say "set" but after_revision=0 keeps after_id and full resets R1 Mafuuu, Leorio, Razor Yes
CRF-9 P3 Author fixed (f555159) exp_chats_test.go:10251 Live-edit step waits for a status event that may never be emitted R1 Bisky Yes
CRF-10 P3 Author contested; panel closed R3 (1/1 accept) AgentChatPage.tsx:296 No test covers the page passing history_version to useChatStore R1 Bisky Yes
CRF-11 P4 Author fixed (1c07b42) useChatStore.ts:578 commitHistoryReplacement in history_reset branch is unreachable and untested R1 Bisky Yes
CRF-12 P3 Author fixed (d3c3041) codersdk/chats.go:2209 from_message_id has no field doc, API reference is blank R1 Leorio Yes
CRF-13 P4 Author fixed (72d54e4) useChatStore.ts:772 connect() comment says message ID controls what server sends R1 Leorio Yes
CRF-14 P4 Author fixed (99be3f6) exp_chats.go:3522 after_revision name differs from history_version it carries R1 Ryosuke Yes
CRF-15 Nit Author fixed (5068a48) exp_chats_test.go:14013 sortedIDs can be slices.Sorted(maps.Keys(...)) R1 Ging-Go Yes
CRF-16 Nit Author fixed (5068a48) chatStore.test.tsx:839 [...x].reverse() instead of toReversed() R1 Ging-TS Yes
CRF-17 Nit Dropped by orchestrator (repo squash-merges; PR title complies) cb8ddd6..6787013 Commit subject scopes and length R1 Leorio No
CRF-18 Note Note (body) stream_loop.go:100 Cursor advance depends on commitHistoryReplacement running before status handling R1 Ryosuke No
CRF-19 Note Dropped by orchestrator (confirmation, no action) stream_loop.go:387 DeleteFunc covers both reset kinds R1 Ging-Go No
CRF-20 Note Dropped by orchestrator (confirmation, no action) useChatStore.ts:664 Cursor advances after applied messages; store and cache agree R1 Nami No
CRF-21 Note Dropped by orchestrator (confirmation, no action) stream_loop.go:300 Remaining producer/consumer pairs agree R1 Melody No
CRF-22 Note Dropped by orchestrator (confirmation, no action) stream_loop.go:204 Partial-reset bound correct for any deletion pattern R1 Pariston No
CRF-23 Note Dropped by orchestrator (confirmation, no action) exp_chats_test.go:10278 Other new tests check real behavior R1 Bisky No
CRF-24 Note Dropped by orchestrator (confirmation, no action) exp_chats.go:3552 New 400 response well written R1 Leorio No
CRF-25 OOS Out of scope (body) useChatStore.ts:298 In-flight refetch resolving after stream events overwrites cache and store R1 Nami, Hisoka No
CRF-26 OOS Out of scope (body) stream_loop.go:233 requires_action reads full history on every sync R1 Killua No
CRF-27 OOS Out of scope (body) chats.sql:451 No (chat_id, revision) index for GetChatMessagesByRevisionForStream R1 Killua No
CRF-28 P3 Author fixed (d2420ca) chatStore.test.tsx:4083 "page refetch does not advance the version" step is vacuous since preview_reset overwrites unconditionally R2 Bisky Yes
CRF-29 Nit Author fixed (e912c65) exp_chats_test.go:13951 waitForChatStatus uses require.Eventually, ignoring ctx deadline R2 Ging-Go Yes
CRF-30 P4 Author contested; panel closed R3 (1/1 accept) useChatStore.ts:580 Full reset upserts earlier-frame messages then replaces them, firing wasted invalidations R2 Nami Yes
CRF-31 P4 Author fixed (da8d443) ARCHITECTURE.md:1339 Doc says "opened with history_version" where code requires non-zero R2 Mafuuu Yes
CRF-32 P4 Author fixed (af74e4b) stream_loop.go:58 hasHistoryVersion reads as "historyVersion is set" but means client opened with one R2 Gon P3 Yes
CRF-33 Nit Author contested; panel closed R3 (1/1 accept) exp_chats_test.go:10270 Comment repeats the assertion message below it R2 Gon P2 Yes
CRF-34 Nit Author fixed (e22502b) useChatStore.ts:542 Deleted comment still describes retained historyReplacementBuf discard R2 Gon Yes
CRF-35 Nit Author contested; panel closed R3 (1/1 accept) stream_loop_internal_test.go:503 reset(messages int) parameter names an event count "messages" R2 Gon Yes
CRF-36 OOS Out of scope (body) useChatStore.ts:239 replaceCacheMessages does not invalidate chat prompts after another tab's edit R2 Nami No
CRF-37 OOS Out of scope (body) chats.ts:2350 editChatMessage comment refers to nonexistent "FullRefresh" R2 Nami No
CRF-38 Note Dropped by orchestrator (confirmation, no action) stream_loop.go:353 preview_reset producer/consumer pairs agree R2 Melody, Ryosuke, Pariston, Kite, Leorio No
CRF-39 P3 Author fixed (4a3ce86) useChatStore.ts:189 !pageLoaded guard untested; removing it pins cold-opened chats to after_id R3 Bisky Yes
CRF-40 Nit Author fixed (67fea0a) stream_loop.go:57 openedWithHistoryVersion comment clause refers to the version, not the history R3 Leorio Yes
CRF-41 P4 Author fixed (ce67ada) exp_chats_test.go:14008 applyUntil timeout message names neither condition nor events received R3 Chopper Yes
CRF-42 Note Dropped by orchestrator (confirmation, no action) useChatStore.ts:184 8c68ff3 gating and version-0 pinning verified R3 Nami, Ryosuke No
CRF-43 Nit Author fixed (18a1079) useChatStore.ts:771 Comment claims write order matters; the two synchronous cache writes commute R4 Nami Nit, Hisoka Nit, Bisky Note Yes
CRF-44 P4 Author contested; panel closed R5 (1/1 accept) codersdk/chats.go:3163 StreamChatOptions.HistoryVersion pointer adds nil state equal to zero R4 Zoro Yes
CRF-45 P4 Author fixed (c0bcee4) useChatStore.ts:78 writeHistoryVersionToCache duplicates writeQueuedMessagesToCache R4 Zoro Yes
CRF-46 Note Dropped by orchestrator (confirmation, no action) useChatStore.ts:78 bfdc902 resolves CRF-4; remount test fails without the cache write R4 Nami, Bisky, Chopper, Leorio, Zoro No
CRF-47 P4 Author fixed (c7e9f5f) exp_chats.go:3568 history_version above chat's version accepted; rows the client lacks at or below the chat's version are silently never sent R5 Knov (CRF-3 Netero R1) Yes
CRF-48 P4 Author fixed (92bccbf) codersdk/chats.go:972 HistoryVersion doc states the page can be ahead of the version but not that the stream resends it R5 Leorio Yes
CRF-49 OOS Out of scope (body) useChatStore.ts:245 Pre-existing useCallback wrappers in a compiler-memoized hook R5 Ging-React No
CRF-50 Note Dropped by orchestrator (confirmation, no action) stream_loop.go:356 Codex empty-chat thread premise false: insertChat requires initial messages R5 Nami, Hisoka, Knov No
CRF-51 P4 Author fixed (e1cdf14) exp_chats.go:3583 400 detail omits both versions and where a valid history_version comes from R6 Leorio (Hisoka note) Yes
CRF-52 Note Note (body) exp_chats.go:3580 After a DB rollback, a tab retries with a version above the chat's and gets 400 indefinitely R6 Hisoka No
CRF-53 Note Dropped by orchestrator (confirmation, no action) exp_chats.go:3580 CRF-47 fix mutation-verified, > boundary pinned by existing tests R6 Knov, Bisky, Chopper, Ging-Go, Zoro, Netero No
CRF-54 Nit Author fixed (code removed from PR in rewrite to e2bae13) useChatStore.ts:41 patchNewestPage comment restates the code; "unchanged" means same reference R7 Gon P2 Yes
CRF-55 OOS Dropped by orchestrator (superseded by in-scope CRF-69 after rewrite) codersdk/chats.go:3159 AfterID doc says after_id streams can skip full history; a chat with a deleted message resends it all R7 Gon No
CRF-56 Nit Dropped by orchestrator (repo squash-merges; commit body does not reach main) 698f065 Commit body claims preview_reset only on history change; also sent on non-zero generation_attempt R7 Leorio No
CRF-57 P4 Author fixed (cdf4789) stream_loop.go:488 knownMessages check in versionedMessageEvents can never skip a row R8 Netero Yes
CRF-58 P4 Author fixed (f1007e9) stream_subscribe.go:59 Pubsub subscribe failure path (ok == false) has no test R8 Netero Yes
CRF-59 P3 Author contested; panel closed R13 (1/1 accept, author decision) stream_loop.go:474 Versioned reset relies on deleted rows keeping their edit's revision; nothing enforces it R8 Ryosuke Yes
CRF-60 P3 Author fixed (cdf4789) stream_loop.go:475 SoftDeleteChatMessageByID alone is a non-suffix delete; versioned clients then drop live messages (reproduced) R8 Kite Yes
CRF-61 P4 Author fixed (cdf4789) chats.sql:2115 InsertChatQueuedMessage kept though it writes the queue outside a transition like the five deleted queries R8 Knuckle, Melody, Pariston (Mafuuu Note, Kite OOS) Yes
CRF-62 P4 Author fixed (f1007e9) exp_chats.go:3775 Setup-failure 500 says "Chat streaming is not available." with no cause for transient failures R8 Mafuuu, Leorio, Ryosuke Yes
CRF-63 P4 Author fixed (f1007e9) exp_chats.go:3691 Doc says snapshot_version increases on every committed change; title/labels/archive do not bump it R8 Mafuuu Yes
CRF-64 P3 Author fixed (f1007e9) tasks-to-chats-migration.md:187 action_required row tells clients to run the tool per event; reconnects resend it, so tools run twice R8 Pen Botter Yes
CRF-65 P4 Author fixed (f1007e9) tasks-to-chats-migration.md:214 Discard a before_id response after history_reset but no instruction to request it again R8 Pen Botter, Leorio Yes
CRF-66 P4 Author fixed (f1007e9) exp_chats.go:3765 snapshot_version 400 lost CRF-51's "what to send" sentence; comment says version never decreases, contrary to restore R8 Leorio, Gon Yes
CRF-67 P4 Author fixed (f1007e9) stream_subscribe.go:244 SubscribeAuthorized doc omits snapshotVersion and the new ok == false meaning R8 Leorio Yes
CRF-68 P4 Author fixed (f1007e9) tasks-to-chats-migration.md:198 Reconnect steps 7 to 9 have no stated condition R8 Leorio Yes
CRF-69 P3 Author fixed (f1007e9) codersdk/chats.go:3321 SnapshotVersion zero-value sentence documents AfterID and contradicts the AfterID doc above it R8 Gon Yes
CRF-70 Nit Author fixed (f1007e9) stream_loop_internal_test.go:841 syncAgainst comment restates name; omits that it fails when no change R8 Gon P2 Yes
CRF-71 Nit Author fixed (f1007e9) stream_loop_internal_test.go:893 streamEvent comment first sentence restates the type R8 Gon P2 Yes
CRF-72 Nit Author fixed (f1007e9) stream_loop_internal_test.go:917 applyStreamEvents comment restates name; garbled phrase R8 Gon P2 Yes
CRF-73 Nit Author fixed (f1007e9) exp_chats_test.go:14634 applyUntil comment restates name; omits failure behavior R8 Gon P2 Yes
CRF-74 P4 Author fixed (f1007e9) stream_loop_internal_test.go:834 streamStore described as one sync's state but holds all rows R8 Gon Yes
CRF-75 Nit Author fixed (f1007e9) exp_chats_test.go:14593 chatTab.version holds a snapshot version; rename R8 Gon Yes
CRF-76 Nit Author fixed (f1007e9) chatd_internal_test.go:1904 TestSubscribeRejectsTransientLookupFailure uses "Rejects" for a read failure R8 Gon Yes
CRF-77 P4 Author fixed (f1007e9) stream_loop_internal_test.go:981 afterID 4 row names a deleted message, so it cannot catch after_id no longer being ignored R8 Bisky Yes
CRF-78 P4 Dropped by orchestrator (merged into CRF-58: adding the SubscribeFails row gives the table its second case) exp_chats_test.go:10924 SetupFailureReturnsHTTPError table has one row R8 Bisky No
CRF-79 OOS Out of scope (body) dump.sql:5435 Queue update trigger ignores reasoning_effort-only updates R8 Knuckle No
CRF-80 OOS Out of scope (body) stream_subscribe.go:214 Server.Subscribe has no production caller R8 Ryosuke No
CRF-81 OOS Out of scope (body) chats.sql:457 GetChatMessagesByRevisionForStream loads content of deleted rows the versioned path discards R8 Killua No
CRF-82 Note Dropped by orchestrator (confirmation, no action) stream_loop.go:336 Versioned contract pairings, schema invariants, auth scoping, and mutation-checked tests verified R8 Melody, Knuckle, Takumi, Kurapika, Pariston, Bisky, Killua, Pen Botter No
CRF-83 P4 Author fixed (cae1742); verified R11 stream_loop.go:466 versionedMessageEvents comment says the client has received none of the changed rows; false on the first sync, which resends commit snapshot_version R10 Mafuuu, Leorio, Gon, Bisky Yes
CRF-84 P4 Author fixed (cae1742); verified R11 stream_subscribe.go:247 SubscribeAuthorized doc says non-zero snapshotVersion; code checks > 0 R10 Leorio Yes
CRF-85 P4 Author fixed (48e4497); verified R11 codersdk/chats.go:3322 StreamChatOptions.SnapshotVersion doc still repeats the event-type list from ChatStreamEvent.SnapshotVersion (CRF-69 follow-up) R10 Gon Yes
CRF-86 Nit Author fixed (0ac1bcb); verified R11 exp_chats_chatstate_test.go:594 Comment restates the two statements below it R10 Gon Yes
CRF-87 Nit Author fixed (0ac1bcb); verified R11 transitions.go:770 Inline comment repeats the query name; only the exclusive bound needs explaining R10 Gon Yes
CRF-88 Nit Author fixed (0ac1bcb); verified R11 stream_loop_internal_test.go:895 Field comment sits above type streamEvent and becomes its doc comment R10 Gon Yes
CRF-89 Nit Author fixed (0ac1bcb); verified R11 exp_chats_test.go:10695 channel local vs failEvent field name one value two names R10 Gon Yes
CRF-90 OOS Out of scope (body) codersdk/chats.go:3392 StreamChat turns a WebSocket read failure into a synthetic error event, indistinguishable from a chat processing error R10 Pen Botter No
CRF-91 P4 Dropped by orchestrator (churn on posted CRF-83 wording) stream_loop.go:466 CRF-83 fix comment restates newStreamLoop's start point (Gon P2) and does not say why the resend is harmless (Leorio P4) R11 Gon, Leorio No
CRF-92 P4 Author fixed (3399896); verified R13 tasks-to-chats-migration.md:181 message event row says replace your message with the same ID; gives no instruction for a new message ID R12 Pen Botter, Leorio Yes
CRF-93 P4 Dropped by orchestrator (pre-existing pattern in main's test, adapted mechanically) hooks_test.go:667 require.NoError inside httptest handler goroutine; old line had the same defect; 17 sibling require calls in handlers in the file come from main R15 Netero No
CRF-94 Note Dropped by orchestrator (main's test, not this PR) hooks_test.go:664 TestEditQueuedMessageUserPromptSubmitDispatchFailure passes with the queued-row delete removed R15 Bisky No

Contested and acknowledged

CRF-5 (P3, useChatStore.ts:664) - Reconnect cursor never lowered when a stale REST page rolls the store back

CRF-10 (P3, AgentChatPage.tsx:296) - No test covers the page passing history_version to useChatStore

  • Finding: Nothing tests that AgentChatPage passes pages[0].history_version into useChatStore; deleting the line or reading the wrong page leaves every test passing while the client reverts to full resets. Proposed a page-level story/test asserting watchChat receives the version.
  • Author defense: If the line breaks, the client opens without a version and gets the old protocol, which the server still supports. Covering it needs a page-level WebSocket mock for one prop; watchChat is a module export the stories do not stub.
  • Panel decision (R2, 0/1 accept): Bisky, the agent that raised it (same agent, messaged), re-raised. The defense's premise is wrong: AgentChatPage.stories.tsx already runs under the withWebSocket decorator (meta decorators, line 923), which replaces window.WebSocket and routes events by URL substring, so a story keyed on history_version=5 tests the line. Orchestrator confirmed testHelpers/storybook.tsx:92. Consequence if broken: the 24 MB full replay this PR removes returns with no failing test.

CRF-30 (P4, useChatStore.ts:580) - Full reset upserts earlier-frame messages then replaces them

  • Finding: On a full history_reset, messages received earlier in the frame are upserted into store and cache and immediately replaced, firing prompt/automation invalidations for discarded messages. Proposed applying earlierMessages only when from_message_id is set.
  • Author defense: A history_reset only follows an edit that deleted messages, and an edit changes prompt history. The reset path never invalidates prompts, so the upsert of the earlier messages is what refreshes them; skipping it leaves prompt history stale.

CRF-33 (Nit, exp_chats_test.go:10270) - Comment repeats the assertion message

  • Finding: The step comment "A client without a history version gets a full reset." repeats the assertion message five lines below; delete it.
  • Author defense: Each step of the test opens with such a comment; they are its structure, and removing one leaves that step unmarked.

CRF-35 (Nit, stream_loop_internal_test.go:503) - reset(messages int) parameter name

  • Finding: reset := func(messages int) names an event count "messages" where messages and rows hold []database.ChatMessage; rename to resent.
  • Author defense: No messages slice exists in this test; cases use rows. reset(2) reads as a reset with two messages.

CRF-10 R3 update

  • Author defense (R3): Concedes the URL-routed WebSocket mock exists. Argues a story play assertion is ruled out by site/AGENTS.md FE1 (Pixel screenshots every story; assertions belong in Vitest), no Vitest test renders AgentChatPage, building that harness for one field is out of proportion, and a broken line falls back to correct full resets.

R3 panel closures

  • CRF-10 (R3, 1/1): Bisky, the agent that raised it (R1 agent, messaged), accepted: FE1 in site/AGENTS.md forbids behavior assertions in story play functions (orchestrator confirmed site/AGENTS.md:7), no Vitest harness renders AgentChatPage, and a broken line falls back to correct full resets. Bisky raised the testable part inside useChatStore as CRF-39.
  • CRF-30 (R3, 1/1): Nami, the agent that raised it (R2 agent, messaged), accepted on consequence: full resets reach the new client only from older servers (every chat has history_version >= 1 because CreateChat always inserts a system message and snapshot_version defaults to 1), and the extra refetch is invisible. Nami rejected the author's reasoning that the upsert refreshes prompts after a reset; the missing refresh is CRF-36.
  • CRF-33 (R3, 1/1): Gon (R2 agent, messaged) accepted: all five steps open with a step comment, so the comment is structural.
  • CRF-35 (R3, 1/1): Gon (R2 agent, messaged) accepted: the test has no messages variable, so the round 2 evidence was wrong.

CRF-44 (P4, codersdk/chats.go:3163) - StreamChatOptions.HistoryVersion pointer adds nil state equal to zero

  • Finding: HistoryVersion is *int64 but nil and zero mean the same to the server; the doc must say "nil or zero", the caller zero-checks before taking the address, and typesGenerated gets number | null. Proposed int64, sent when non-zero.
  • Author defense: Keeps *int64 because the sibling AfterID is also *int64, and the server reads both into an int64 cursor field where zero means unset, so the two options behave alike.
  • Panel closure (R5, 1/1): Zoro, the agent that raised it (R4 agent, messaged), accepted: AfterID uses the same *int64 shape with zero meaning unset in StreamCursor, so changing only HistoryVersion would give StreamChatOptions two conventions; the cost is one caller-side check and an unused null in TS.

CRF-59 (P3, stream_loop.go:474) - Versioned reset relies on deleted rows keeping their edit's revision

  • Finding: versionedMessageEvents treats each soft-deleted row in the delta as an edit at that row's revision; nothing in the database prevents a later UPDATE of a deleted row from raising its revision, after which versioned clients drop live messages above it permanently. Proposed: set_chat_message_revision_before raises when OLD.deleted is true after its no-op check.
  • Author defense: The PR does not change which queries update deleted rows, and none change one: SoftDeleteChatMessagesAfterID only sets deleted = true again, which the revision trigger skips as no change, and both search backfills select deleted = false. A trigger would block a write no code makes, so no migration.

Law analysis

R1: effective +1163 (head 6787013). Verdict: Don't split. Enforcement: Advisory.

Round log

Round 1

Full panel (round 1). Netero + Law first (Law: don't split, advisory). Panel: ging-go, ging-ts, ging-react, nami, melody, ryosuke, killua (specialists), pariston, mafuuu (core), bisky, hisoka, leorio (pool), razor (wildcard). Gon deferred to next panel round to hold the 12-reviewer target; Knuckle and Takumi not triggered (no query/schema or goroutine changes). CRF-5 is convergent (Hisoka, Melody, Mafuuu; Nami listed the root race as OOS); kept at P3 because on main the same stale state also persisted until a reconnect or refetch, so the PR's delta is the lost repair on reconnect. Reviewed against 5552152..6787013.

Round 2

Restructure round (exported SDK types changed: ChatStreamStatus.HistoryVersion removed, StreamChatOptions.AfterRevision renamed to HistoryVersion, ChatStreamPreviewReset added; wire param renamed). Churn guard PROCEED: 12 addressed, CRF-10 contested, CRF-5 deferred to #30478. Law skipped (+37 effective). Netero: no new findings, CRF-1/CRF-2 verified. Full panel: ging-go, ging-ts, ging-react, nami, melody, ryosuke, killua, pariston, mafuuu, gon (once-per-PR floor), leorio, hisoka, kite (wildcard), plus bisky (dispute check-in, R1 agent reused). CRF-10 re-raised by Bisky (0/1 accept). CRF-12 re-raised by Leorio (event field docs still missing; author declined that part). Gon P2 (redundant comment) downgraded to Nit: keep-argument is noise in test, but no behavior or reader trap. Gon P3 (hasHistoryVersion naming) downgraded to P4: keep-argument is a plausible refactor breaking old clients, but the field comment states the meaning and no current behavior is wrong. Reviewed against c9680e4..0e0a8ec.

Round 3

Fix round (no exported signature changes; SDK edits are comments; one behavior commit 8c68ff3 in useChatStore). Churn guard PROCEED: 6 addressed, 4 contested, CRF-5 deferred. Law skipped (+76 effective since R2). Netero: no findings. Panel: bisky, nami, gon (dispute check-ins, prior agents messaged), leorio, mafuuu, ging-go (fix verification), ging-ts, ging-react, ryosuke, hisoka (files/behavior changed since R2), chopper (wildcard). Workspace agent disconnected mid-round; after restart the 10 reviewers without output files were resumed via message_agent with the same prompt (Gon had already written). All four contested findings closed 1/1. New: 1 P3, 1 P4, 1 Nit. Reviewed against a4b68ac..f19806a.

Round 4

Fix round (no new files or exported signature changes; one behavior commit bfdc902 writes the preview_reset version into cached page 0, resolving CRF-4 and a Codex thread). Churn guard PROCEED: CRF-39, CRF-40, CRF-41 addressed; CRF-5 deferred. Law skipped (+137 effective since R3). Netero: no findings, both new tests mutation-verified. Panel: bisky, leorio, chopper (fix verification), nami, ging-ts, ging-react, ging-go (changed files), hisoka (new cache state), zoro (wildcard). All three fixes verified. New: 2 P4, 1 Nit (convergent). Zoro P4 on the HistoryVersion pointer kept at P4: keep-argument is the generated number|null and caller zero-checks on an experimental API that is cheap to change now; counter-argument is the sibling AfterID *int64, which does not remove the meaningless state. Reviewed against 6bae268..bfdc902.

Round 5

Fix round (refactor and doc comments only since R4). Churn guard PROCEED: CRF-43, CRF-45 addressed; CRF-44 contested; CRF-5 deferred. Netero: no findings. Panel: zoro (dispute check-in, R4 agent messaged), nami, hisoka, leorio, ging-ts, ging-react, knov (wildcard). CRF-44 closed 1/1. Codex thread PRRT_kwDOGkVX1s6q1d9h (empty chat at version 0) refuted by Nami, Hisoka, Knov; orchestrator confirmed insertChat rejects empty InitialMessages (transitions.go:126). Knov raised CRF-3 at P4 claiming revisions between chat version and the client value are permanently lost; orchestrator disproved that mechanism (applyDBSnapshot sets l.state.historyVersion = chat.HistoryVersion unconditionally at stream_loop.go:361, so later syncs use the real version). Kept at P4 as CRF-47 with the residual consequence (rows at or below the chat's version the client never held are silently never sent; public parameter, one-comparison fix). Keep-argument for P3: silent incomplete history on a public API; settled P4 because only a caller sending a version it never read hits it. Reviewed against 782fb30..a73dc4e.

Round 6

Fix round (one validation check, its test, doc text). Churn guard PROCEED: CRF-47, CRF-48 addressed; CRF-5 deferred. Netero: no findings. Panel: knov, leorio (fix verification), bisky, chopper, hisoka, ging-go, zoro (wildcard). Both fixes verified; > vs >= boundary pinned (Bisky, Chopper mutation). New: 1 P4 (error detail), 1 Note (DB rollback retry loop, no change requested). Reviewed against 782fb30..77ab16b.

Round 7

Fix round (CRF-51 detail text, two comments). Churn guard PROCEED: CRF-51 addressed; CRF-5 deferred. Netero: no findings. Panel: leorio, hisoka (raised CRF-51), gon (comments), chopper, kite (wildcard). Gon first reported its prompt missing (read during select); re-sent the same prompt and it completed. CRF-51 verified. Gon P2 on the patchNewestPage comment downgraded to Nit: keep-argument is the misleading reference-vs-equality contract for a future caller; downgrade because the cost of misreading is one redundant setQueryData, and Leorio R5 judged the comment accurate. Gon AfterID doc item is pre-existing text (OOS). Reviewed against 782fb30..698f065.

Round 8

Restructure round: PR rewritten from history_version cursor to per-event snapshot_version; web client changes moved to #30478; five queries deleted; migration guide rewritten. Churn guard PROCEED: CRF-54 addressed (code removed), CRF-5 deferred. Law skipped (effective +1056, below R1). Netero: 2 P4 (CRF-57, CRF-58); Netero prompt race (spawned in same batch as select) resent. Full panel 14: ging-go, knuckle, ryosuke, melody, killua, takumi, kurapika, pen-botter (specialists: Go, queries, architecture, wire format, hot path, concurrency, dbauthz, docs), pariston, mafuuu (core), bisky, gon, leorio (pool; gon/leorio mandatory on restructure), kite (wildcard). Exceeds 12 because eight specialists triggered. Convergence: unenforced write invariants behind the versioned path (Ryosuke, Kite, Pariston framing 4, Takumi note) kept as two P3s with distinct fixes. Gon P2s on test-helper comments downgraded to Nit (same reason as R7). Bisky single-row table finding merged into CRF-58. Reviewed against b9e76bd..e2bae13.

Round 9 update

BLOCKED. CRF-59 (P3) silent: no reply and no change to set_chat_message_revision_before. 20 findings addressed in cdf4789 and f1007e9 (claimed fixed per churn guard; not yet verified by the panel). No review panel. Reviewed against 9cda919..556131b.

Round 10

Fix round. Churn guard PROCEED: CRF-57, CRF-58, CRF-60 to CRF-77 addressed (cdf4789, f1007e9); CRF-59 contested; CRF-5 deferred. Netero: no findings, CRF-57 and CRF-58 verified. Panel: ryosuke (dispute check-in, R8 agent messaged), kite, knuckle, mafuuu, leorio, pen-botter, gon, bisky, ging-go, knov (wildcard). All checked fixes verified (CRF-57, 58, 60 to 68, 77; Gon confirmed 70 to 76 applied). CRF-59 re-raised by Ryosuke (0/1 accept) with new evidence: migration 000435 backfilled chat_messages with no deleted filter. Convergent comment finding (4 reviewers) on versionedMessageEvents: Gon P3, others P4; kept P4 because it is comment accuracy, code behavior is tested. Gon P2s on comment repetition downgraded: CRF-85 to P4 (two lists to keep in sync), CRF-86 and CRF-87 to Nit (same reason as R7/R8). Pen Botter OOS: codersdk/chats.go:3392 StreamChat turns read failure into synthetic error event (body). Reviewed against 9cda919..e5e63f8.

Round 11

Fix round (comments, one test local rename, two docs sentences; e5e63f8..1e7c5fd). Churn guard PROCEED: CRF-83 to CRF-89 addressed, CRF-59 contested again, CRF-5 deferred. Netero: no findings, CRF-83 to CRF-89 verified. Panel: ryosuke (dispute check-in, R8 agent messaged), gon, leorio (raised CRF-83 to CRF-89; comment and doc changes), mafuuu, bisky (CRF-83 co-raisers), pen-botter (docs changed), knov (wildcard). All seven fixes verified. CRF-59: author's R11 defense (000435 predates 000519, no write since updates a deleted row, trigger would block a write no code makes) answers the R8 raise-exception fix, not the R10 keep-revision fix, which blocks nothing; Ryosuke re-raised (0/1 accept). Second contest round with no movement, so the body asks for a human decision. Gon P2 and Leorio P4 on the CRF-83 replacement comment dropped as CRF-91: the author applied the wording the R10 comment proposed, the comment is accurate, and the remaining points are wording preference. Reviewed against 9cda919..1e7c5fd.

Round 12

Fix round (docs only: bc14706 rewords the migration guide event table). Churn guard PROCEED: CRF-59 contested a third time (author: only current UPDATEs are SoftDeleteChatMessagesAfterID and the search queries, none restamps a deleted row; the comment states the assumption), CRF-5 deferred. Netero: no findings. Panel: ryosuke (dispute check-in, R8 agent messaged), pen-botter, leorio (docs changed), mafuuu (wildcard). Ryosuke accepts the defense as correct for current code but keeps the finding (0/1 accept): documented vs enforced invariant, no new facts on either side; needs a human decision. New: CRF-92 P4 convergent (Pen Botter, Leorio). Leorio's line used (row only; line 213 explains why resends are harmless and can stay). Reviewed against 9cda919..bc14706.

Round 13

Fix round (docs only: 3399896 migration guide message row, 7be3f1c SnapshotVersion doc semicolon plus regenerated apidoc, schemas.md, typesGenerated.ts). Churn guard PROCEED: CRF-92 addressed, CRF-59 contested with explicit author decision (PRRC_kwDOGkVX1s8AAAABBfQW1g), CRF-5 deferred. Netero: no findings. Panel: ryosuke (dispute check-in, R8 agent messaged), pen-botter, leorio (CRF-92 raisers, docs), mafuuu (wildcard). CRF-92 and Codex thread PRRT_kwDOGkVX1s6w59V9 verified. CRF-59 closed 1/1: Ryosuke accepts the author's explicit owner decision to keep the documented invariant (no current writer violates it, stated at stream_loop.go:477 and ARCHITECTURE.md, enforceable later with one function migration). No new findings. Reviewed against 9cda919..7be3f1c.

Round 14

Rebase round (onto eba39ae; head 4ed589d). Not a restructure: PR diff unchanged except main-rename context lines and 98d206d (TestGetChatAttachments moved from deleted SoftDeleteChatMessageByID to SoftDeleteChatMessagesAfterID). Main added no chat_messages or chat_queued_messages writer (new read query GetChatMessagesWithFilePartsByChatID; migrations 000634 to 000637 do not touch chat tables). Churn guard PROCEED: CRF-5 deferred, no other open findings. Netero: no findings, 98d206d mutation-verified. Panel: bisky (test changed), knuckle (queries from main), mafuuu (core), knov (wildcard). All no findings; Bisky and Netero mutation-verified the fixture's delete. Reviewed against eba39ae..4ed589d.

Round 15

Rebase round (onto f0ddf43; head 36d77b8). PR diff unchanged except main context lines and 36d77b8 (hooks_test.go from main moved off deleted InsertChatQueuedMessage and DeleteChatQueuedMessage). Main added migration 000638 (editing_since, paused, trigger now fires on editing_since) and two queue writers (UpdateChatQueuedMessageEditing, UpdateChatQueuedMessageContent), all called from chatstate Tx methods after LockChatAndBumpSnapshotVersion; orchestrator, Netero and Mafuuu verified both fire the queue_version trigger (UPDATE OF fires on SET targets, and Content always sets content), so CRF-79 still has no writer that triggers it. Churn guard PROCEED: CRF-5 deferred only. Netero: 1 P4 (require in handler goroutine) dropped as CRF-93: the replaced line had the same defect and the file has 17 such calls from main; asking this PR to fix one is inconsistent and out of its scope. Panel: bisky (test changed), ging-go (Go idioms, Netero finding area; no findings), mafuuu (core), razor (wildcard). CI failure is the docs-analysis job, not build or tests (job log not readable: gh token 401). Reviewed against f0ddf43..36d77b8.

About deep-review

CRF = Coder Review Finding (P0-P4, Nit, Note)

Reviewer Focus
Bisky tests
Chopper ops/errors
Churn-guard change verification
Ging language modernization
Gon naming
Hisoka edge cases
Killua perf
Kite change integrity
Knov contracts
Knuckle SQL
Komugi flake/determinism
Kurapika security
Law decomposition
Leorio docs
Luffy product
Mafu-san process
Mafuuu contracts
Melody dispatch/pairing
Meruem structural
Nami frontend
Netero mechanical checks
Pariston premise testing
Pen-botter product gaps
Razor verification
Robin duplication
Ryosuke Go arch
Takumi concurrency
Zoro shape

🤖 Managed by Coder Agents.

@chatgpt-codex-connector

chatgpt-codex-connector Bot commented Oct 7, 2026 •

Copy link
Copy Markdown

Codex Review Summary

This comment shows the latest Codex review activity on this pull request.

Review Status Commit Review trigger
📝 Code Review ✅ Completed 2026-10-30T19:59:31.952556Z 36d77b8 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

This comment was marked as outdated.

coder-agents-review[bot]

This comment was marked as outdated.

@mafredri

This comment was marked as outdated.

@chatgpt-codex-connector

This comment was marked as outdated.

coder-agents-review[bot]

This comment was marked as outdated.

@mafredri
mafredri force-pushed the fix/chat-stream-history-replay branch from 0e0a8ec to f19806a Compare October 8, 2026 11:44
@mafredri

This comment was marked as outdated.

chatgpt-codex-connector[bot]

This comment was marked as outdated.

@mafredri

This comment was marked as outdated.

coder-agents-review[bot]

This comment was marked as outdated.

@mafredri
mafredri force-pushed the fix/chat-stream-history-replay branch from f19806a to bfdc902 Compare October 9, 2026 10:35
@mafredri

This comment was marked as outdated.

@chatgpt-codex-connector

This comment was marked as outdated.

coder-agents-review[bot]

This comment was marked as outdated.

…teChatMessagesAfterID

TestGetChatAttachments, added on main in 3f055e7, deleted a message
with SoftDeleteChatMessageByID, which this branch removes. The helper
deletes the message right after inserting it, so deleting from its ID
on removes only that message.
Replaying the action-response commit and its revert onto main put back
the hand-written request that main's c80aafe replaced with
requestNoContent.
#31645 moved the migration guide up one directory, so the link this PR
added to the stream reference pointed outside docs/.
Review found the snapshot_version and history_reset comments hard to
follow. They said the stream resends commits, but the stream sends the
current state of the messages and queue that changed. They described
from_message_id and after_id by what a client holds, not by what the
server does with them.

Two statements were wrong. The messages page does not always include a
change made between its reads, because the messages and the queue are
read separately. A chat with a deleted message is not what makes a
stream without snapshot_version send the whole history: after_id being
unset or naming a deleted message is.

The AfterID comment no longer mentions relay connections, which use
/stream/parts and never set after_id.
Replaying the action-response commit and its revert onto main put back
PromoteQueuedResult and the two-value PromoteQueued call that main's
63e08e2 removed.
Main's 63e08e2 removed the ReorderChatQueuedMessageToHead query and
this branch removes ReorderChatQueuedMessageToFront, so neither is
left. Their dbauthz and dbmetrics methods are removed by hand, because
the generators keep methods whose query is gone.
Main's 63e08e2 added a required busy_behavior parameter to
InsertChatQueuedMessageWithCreator. This branch's tests insert queued
rows with that query, because the branch removes
InsertChatQueuedMessage, and they left the parameter empty. They now
pass queue, the column default the removed query relied on.
The migration guide said a message event reports a changed message, but
no query changes a message that isn't deleted. Its test step promised
message_part events, which the stream sends only while the agent writes
its response.

Tell clients to ignore event types and fields they don't recognize, so
that the server can add them later.

The other edits apply the docs style guide to sentences this branch
added: one action per step, the event name instead of "retry notice",
"that has" instead of "with", and contractions in the Swagger text.
ARCHITECTURE.md called from_message_id "the lowest message ID the edit
removed" and said a client reconnects with the highest version it
"applied". The SDK comments say "the ID of the edited message",
"deleted" and "received". The API reference said "the reply in
progress" where the migration guide says "the response that the agent
is writing". Each concept now has one term in all four places.
The SDK comments and the API reference say an edit deletes messages.
The guide said "removed" for the same thing.
An event type or field that a client doesn't recognize can change the
chat's state, so a client that ignores it can show wrong state.
A page requested with before_id has no snapshot_version, so a client
cannot tell from the page whether an edit deleted some of its messages.
The replay conflicted with main's change to steerQueuedMessageWithModel
(#31632). The helper keeps main's version and inserts the row with
InsertChatQueuedMessageWithCreator, because this branch deletes
InsertChatQueuedMessage. The generated files gain the chat-automations
experiment that main added.
@mafredri
mafredri force-pushed the fix/chat-stream-history-replay branch from ab2f9c8 to 5f53220 Compare November 12, 2026 13:29
@mafredri
mafredri temporarily deployed to main November 12, 2026 13:29 — with GitHub Actions Inactive
@mafredri
mafredri merged commit d639ced into main Nov 13, 2026
30 of 31 checks passed
@mafredri
mafredri deleted the fix/chat-stream-history-replay branch November 13, 2026 11:02
@mafredri
mafredri temporarily deployed to main November 13, 2026 11:02 — with GitHub Actions Inactive
@mafredri
mafredri temporarily deployed to docs-preview November 13, 2026 11:02 — with GitHub Actions Inactive
@github-actions github-actions Bot locked and limited conversation to collaborators Nov 13, 2026

This branch was previously deployed

2 inactive deployments
docs-preview — 5f532205 Deployed Nov 13, 2026 by mafredri via cleanup #31
main — 5f532205 Deployed Nov 13, 2026 by mafredri via cla #256322
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants