Skip to content

fix(coderd/x/chatd): reset stream history only if after_id is deleted - #29808

Merged
jscottmiller merged 7 commits into
mainfrom
scott/chat-history-full-load
Oct 16, 2026
Merged

jscottmiller merged 7 commits into
mainfrom
scott/chat-history-full-load

Conversation

@jscottmiller

@jscottmiller jscottmiller commented Sep 23, 2026 •

Copy link
Copy Markdown
Contributor

Fixes CODAGT-1175

tl;dr: The chat stream no longer sends the full history on every connect when a chat contains old edit tombstones.

On its first sync, the chat stream WebSocket treated any soft-deleted message (edits create them) as a history reset and sent history_reset plus every message, ignoring after_id. The client replaced its paginated cache with that history, so every connect, reconnect and PWA resume of a long chat that had ever been edited rendered the whole transcript. A dogfood chat of about 3.2k messages sent 7.1 MB per connect.

historyResetRequired in stream_loop.go now resets on the first sync with an after_id only if message after_id is missing or deleted. Messages are only deleted as a suffix of the chat (an edit deletes the edited message and every later one), so while that message is live the client holds none of the deleted rows. Otherwise the stream sends only live messages newer than after_id, and messageEvents never sends tombstones as messages. Deletions after the first sync still reset. stream_loop_internal_test.go covers the initial-sync cases and the deletion of a streamed message after the first sync, TestSubscribeAfterMessageIDWithEdit runs a real EditMessage against the database, and ARCHITECTURE.md gets TODOs for the new rule.

Generated by Coder Agents on behalf of @jscottmiller.

@linear-code

linear-code Bot commented Sep 24, 2026

Copy link
Copy Markdown

CODAGT-1175

@jscottmiller

Copy link
Copy Markdown
Contributor Author

/coder-agents-review

@coder-agents-review

coder-agents-review Bot commented Sep 24, 2026 •

Copy link
Copy Markdown
Contributor

Chat: Review posted | View chat
Requested: 2026-09-24 22:03 UTC by @jscottmiller

Review history
  • R1 (2026-09-24): 11 reviewers, 2 Nit, 2 P2, 7 P3, 2 P4, COMMENT. Review

deep-review v0.13.0 | Round 1 | 7555486..5fe2b98

Last posted: Round 1, 13 findings (2 P2, 7 P3, 2 P4, 2 Nit), COMMENT. Review

Finding inventory

Finding inventory: PR #29808

Findings

# Sev Status Location Summary Round Reviewer Posted
CRF-1 P2 Open ARCHITECTURE.md:1217 Architecture doc still says any soft-deleted fetched row triggers history_reset; new invariant not listed R1 Netero Yes
CRF-2 P3 Open stream_loop_internal_test.go:323 Reset rule tested only with hand-picked mock revisions; no DB-backed EditMessage test R1 Netero Yes
CRF-3 OOS Out of scope (body) stream_loop_internal_test.go:126 Existing test models an edit as an in-place update, contradicting the new invariant R1 Netero No
CRF-4 Note Dropped by orchestrator (job log unreadable here; author can see the diff has no site/ changes) CI Storybook CI failure not caused by this diff R1 Netero No
CRF-5 P3 Open stream_loop.go:226 Revision comparison never catches a needed reset with current writers; only adds full resets for tombstones above after_id R1 Ryosuke P3, Knuckle P3, Pariston P3, Mafuuu P4 Yes
CRF-6 P2 Open stream_loop.go:202 Doc comment names no-in-place-update invariant but omits tail-only deletes, which the rule depends on R1 Gon P2, Leorio P3, Kite P3 Yes
CRF-7 P3 Open stream_loop.go:213 No test fails if the !hasDeleted early return is removed (full reset on every change after first sync) R1 Ryosuke Yes
CRF-8 P3 Open stream_loop_internal_test.go:470 No case for a tombstone above after_id with revision above message after_id R1 Bisky Yes
CRF-9 P4 Open stream_loop_internal_test.go:582 Subtest passes with the initialMessageSyncDone guard removed R1 Bisky Yes
CRF-10 P3 Open stream_loop_internal_test.go:447 Case names mix id and revision orderings under older/newer/after R1 Gon P3, Bisky P4 Yes
CRF-11 P3 Open stream_loop.go:348 Comment restates the Deleted skip and omits why tombstones reach the loop and why skipping is safe R1 Gon P2, Leorio Nit, Kite Nit Yes
CRF-12 Nit Open stream_loop_internal_test.go:326 Literal after_id 3 tied to case names only in prose; use a const R1 Gon Yes
CRF-13 P2 Dropped by orchestrator (existing two sentences already state the nil contract; rewrite is wording preference) stream_loop_internal_test.go:332 fullHistory field comment longer than its contract R1 Gon No
CRF-14 P2 Dropped by orchestrator (comment states the edit scenario the row/flag fixture encodes only implicitly) stream_loop_internal_test.go:621 Test comment restates the edit fixture R1 Gon No
CRF-15 Nit Open stream_loop.go:226 horizon is a second name for the revision of message after_id R1 Gon Yes
CRF-16 P4 Open exp_chats.go:3170 after_id docs (endpoint and codersdk StreamChatOptions) do not state the new contract R1 Mafuuu Yes
CRF-17 Note Note (body) stream_loop.go:223 7.1 MB measurement used after_id past newest message; a nonexistent id still resets under this PR R1 Mafuuu No
CRF-18 P3 Open stream_loop.go:160 "Fixes CODAGT-1175" closes the ticket while real edits still send full history; follow-up design untracked R1 Kite P3, Leorio P4 Yes
CRF-19 Nit Note (body) PR title PR title is 81 characters; CONTRIBUTING.md caps commit titles at 72 R1 Leorio No
CRF-20 OOS Out of scope (body) chats.sql:2441 Unused SoftDeleteContextFileMessages is the only non-tail delete query R1 Knuckle, Kite, Ryosuke, Pariston No
CRF-21 OOS Out of scope (body) dump.sql:1448 Revision trigger accepts content changes and un-delete on any row R1 Ryosuke, Kite No
CRF-22 OOS Out of scope (body) stream_loop.go:357 First sync drops changed rows with id <= after_id, so in-place updates of held rows are never sent R1 Ryosuke No
CRF-23 OOS Out of scope (body) stream_loop.go:151 First sync reads every visible row with content on every connect R1 Killua, Knuckle No
CRF-24 Note Dropped by orchestrator (meta observation; covered by CRF-11 and CRF-12) stream_loop.go:202 Comment quality pattern across the PR R1 Gon No
CRF-25 Note Dropped by orchestrator (confirms the SQL premise; asks for no change) stream_loop.go:227 SQL side of the revision rule holds R1 Knuckle No
CRF-26 Note Dropped by orchestrator (praise; asks for no change) stream_loop_internal_test.go:510 nil fullHistory makes gomock assert the query does not run R1 Kite No
CRF-27 Note Dropped by orchestrator (praise; asks for no change) commit b3c8064 Commit body states symptom, cause, and rule R1 Leorio No

Contested and acknowledged

Law analysis

Not run. Effective additions 369 (threshold 1000).

Round log

Round 1

Full panel (round 1). Netero first pass: 1 P2, 1 P3, 2 Notes, no build or test failure, so the panel ran. Panel: ging-go, Ryosuke, Melody, Knuckle, Killua (domain specialists: Go files, Go code, streaming producer/consumer cursor, Go-to-SQL revision semantics, per-connect hot path), Pariston and Mafuuu (core), Bisky (tests changed), Gon and Leorio (once-per-PR floor), Kite (wildcard, random draw from the distilled set). Size 11 exceeds the focused target of 6-8 because five specialists matched and Bisky, Gon, and Leorio are required. ging-go, Melody, Killua: no findings.

Cross-check: the revision comparison finding converged across Ryosuke, Knuckle, Pariston, Mafuuu. Contradiction: Ryosuke, Knuckle, Pariston recommend no reset for tombstones above after_id; Mafuuu (id comparison) and Bisky (pin the reset in a test) keep it. Presented as a choice in CRF-5, with CRF-8 asking to pin whichever result is chosen. Bisky's justification cites the PR caveat about lastMessageIdRef as a cursor that understates holdings; the caveat describes a cursor that overstates holdings, so that reasoning was not carried into posted text. Orchestrator reproduced CRF-7: removing the !hasDeleted early return leaves DB=ci go test ./coderd/x/chatd/ passing. Kite's P3 on unenforced invariants (stream_loop.go:206) merged into CRF-6; its enforcement fix (delete the query, add a trigger) moved to CRF-20 and CRF-21 as out of scope. Leorio's P4 on the alternative proposal in the PR description merged into CRF-18. Gon's comment P2s downgraded or dropped: CRF-11 to P3 (behavior is test-pinned; the comment is incomplete, not wrong), CRF-12 to Nit (style convention), CRF-13 and CRF-14 dropped with reasons. Severities: 2 P2, 7 P3, 2 P4, 2 Nit posted. Reviewed against 7555486..5fe2b98.

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.

@coder-agents-review coder-agents-review Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This PR limits the first stream sync's history_reset to cases where message after_id is missing or deleted, or a tombstone has a higher revision than it. Findings: 2 P2, 7 P3, 2 P4, 2 Nit.

Reviewers disagree on one design point: dropping the full reset for tombstones above after_id (see the finding on the revision comparison) or keeping it in case a client's cursor understates what it holds. The finding on the missing test case asks you to pin whichever you choose.

The Caveats list gaps with no ticket (see the finding on "Fixes CODAGT-1175"), including one this PR introduces: a cursor that overstates the client's state is no longer repaired by the next connect, so deleted messages can stay on screen until reload. This needs a human decision: file a ticket or accept the gap.

The PR title is 81 characters; docs/about/contributing/CONTRIBUTING.md caps commit titles at 72, and the squash commit uses the title.

Out of scope (needs a ticket or explicit acceptance by a human):

  • coderd/database/queries/chats.sql:2441: SoftDeleteContextFileMessages has no caller and is the only query that can delete a message without deleting every message above it; a caller would break the tail-only assumption the stream reset rule depends on.
  • coderd/database/dump.sql:1448: set_chat_message_revision_before accepts content changes and deleted = true to false on any row, so nothing in the database stops the in-place updates the reset rule does not handle.
  • coderd/x/chatd/stream_loop.go:357: on the first sync, messageEvents drops every changed row with id <= after_id, so an in-place update of a message the client holds is not sent until reload.
  • coderd/x/chatd/stream_loop.go:151: the first sync still reads every visible row of the chat, tombstones and content included, on every connect; only the (chat_id) index serves it, so connect cost grows with chat length.
  • coderd/x/chatd/stream_loop_internal_test.go:126: TestStreamLoopMessageSyncAfterIDAndEdits models an edit as an in-place update, the shape the new doc comment says does not happen, so readers who copy it learn the wrong edit shape.

Notes:

  • The 7.1 MB measurement used an after_id past the chat's newest message. If that id matched no row, this PR still resets for it (the idx < 0 branch at stream_loop.go:223), so the measurement shows the bug but not the fix.

coderd/x/chatd/ARCHITECTURE.md:1217

P2 [CRF-1] This section still says any soft-deleted fetched row triggers history_reset, which is no longer true for the first sync with an after_id. (Netero)

The root AGENTS.md requires updating this document when chatd behavior changes. Describe the first-sync rule here, and add the invariant the rule depends on to the Required invariant list at line 1224 (see the finding on the historyResetRequired doc comment).

🤖

coderd/exp_chats.go:3170

P4 [CRF-16] The after_id docs describe a filter, but the server now also treats it as the id of the newest message the caller holds and resets when that message is missing or deleted. (Mafuuu)

On a chat with any tombstone, an after_id that names no live row in the chat, such as a value past the newest id, gets history_reset plus the full history, while codersdk.StreamChatOptions.AfterID (codersdk/chats.go:2635-2638) says it lets relay callers skip the full history. State on both that after_id must be the id of the newest message the caller holds, and that a missing or deleted id causes a full reset.

🤖

🤖 This review was automatically generated with Coder Agents.

Comment thread coderd/x/chatd/stream_loop.go Resolved
Comment thread coderd/x/chatd/stream_loop.go Resolved
Comment thread coderd/x/chatd/stream_loop.go Resolved
Comment thread coderd/x/chatd/stream_loop.go Resolved
Comment thread coderd/x/chatd/stream_loop.go Resolved
@jscottmiller jscottmiller changed the title fix(coderd/x/chatd): skip stream history reset for tombstones older than after_id fix(coderd/x/chatd): reset stream history only if after_id is deleted Oct 16, 2026
@jscottmiller
jscottmiller marked this pull request as ready for review October 16, 2026 19:59
Scott Miller added 7 commits October 16, 2026 20:17
A new stream with after_id replays the full history when the chat has any
soft-deleted message, even when the deletion predates the client's newest
message. These cases fail until the initial sync bounds resets by the
revision of the after_id message.
…evision

A new stream with any soft-deleted message in the chat sent history_reset
and every message, ignoring after_id. Chats with an edited message replayed
their full history on every connect.

On the initial sync with after_id, reset only when message after_id is
missing or deleted, or a deletion has a higher revision than it. Otherwise
send messages newer than after_id and skip deleted rows. Resets after the
initial sync are unchanged.
Add cases for a tombstone at the same revision as after_id, a tombstone
above after_id with a newer live message, a tombstone after after_id at
or below its revision, the initial sync without after_id, and a deletion
after the initial sync. Document the in-place update invariant that
historyResetRequired relies on.
… deleted

Messages are only deleted as a suffix of the chat, so on the initial sync
the client holds a deleted message only if message after_id is deleted or
missing. Drop the revision comparison, which only added full resets for
tombstones above after_id, and the initial-sync guard, which matched the
missing-row check for every input current writers produce.
@jscottmiller
jscottmiller force-pushed the scott/chat-history-full-load branch from db5f9af to 8d6ebc6 Compare October 16, 2026 20:20
@jscottmiller
jscottmiller merged commit 939146b into main Oct 16, 2026
29 checks passed
@jscottmiller
jscottmiller deleted the scott/chat-history-full-load branch October 16, 2026 20:40
@github-actions github-actions Bot locked and limited conversation to collaborators Oct 16, 2026
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.

2 participants