Skip to content

fix(coderd/x/chatd): coalesce deltas when persisting interrupted turns - #29661

Merged
mafredri merged 22 commits into
mainfrom
fix/chat-interrupted-turn-parts
Oct 23, 2026
Merged

mafredri merged 22 commits into
mainfrom
fix/chat-interrupted-turn-parts

Conversation

@mafredri

@mafredri mafredri commented Sep 21, 2026 •

Copy link
Copy Markdown
Member

An interrupted turn is built from the buffered stream deltas and was stored with one ChatMessagePart per delta, while processStepStream gives a completed turn one part per block. On a benchmark deployment around 2026-09-21, a 28,539-character interrupted response was stored as 3,987 text parts and 1 MB of JSON, and a /messages page with interrupted turns was 11.7 MB, against 117 KB for 50 completed messages. On current main, a seven-character delta serializes to a 385-byte part. Interrupts are routine (the stop button, or sending while a turn runs), so long chats collect these rows. Whitespace-only deltas were also separate parts, which prompt replay drops, so words and paragraphs ran together in the next turn's prompt.

bufferedPartsToPartialMessages now joins adjacent deltas of the same type into one part, which keeps the first delta's fields. A text run ends at a part of another type, at a tool result, or where the narration flag changes. Each reasoning delta's CreatedAt is its block's start time, so a reasoning run ends where CreatedAt changes. The open run accumulates in a strings.Builder, so persisting N deltas is linear in N.

Limits: text deltas have no block identity, so adjacent text blocks still merge, and two reasoning blocks that start in the same microsecond merge. Rows already stored are not rewritten.

These tests fail on main and pass here:

TestBufferedPartsToPartialMessages_CoalescesStreamedDeltas
TestBufferedPartsToPartialMessages_ToolResultEndsTextRun
TestBufferedPartsToPartialMessages_SplitsAdjacentReasoningBlocks
TestBufferedPartsToPartialMessages_SplitsNarrationFromAnswer

TestProcessStepStreamSetsReasoningDeltaCreatedAtToBlockStart passes on both; it checks that processStepStream sets each reasoning delta's CreatedAt to its block's start time, which the reasoning split depends on.

BenchmarkBufferedPartsToPartialMessages_StreamedTextDeltas, one call persisting N seven-byte deltas as one run:

N main B/op main ns/op this PR B/op this PR ns/op
1,000 4.9 MB 5.7 ms 0.74 MB 0.64 ms
2,500 12.2 MB 12.4 ms 1.8 MB 1.9 ms

Found while investigating side-by-side chat rendering performance (#29596). Stream history replay on connect is #29662.

Test output on main
CoalescesStreamedDeltas
actual: []string{"reasoning:think", "reasoning:ing", "text:Hel", "text:lo,", "text: ", "text:world", "text:\n\n", "tool-call:", "text:after", "text: the call"}
ToolResultEndsTextRun
actual: [][]string{[]string{"assistant", "text:a", "tool-call:", "text:b", "text:c"}, []string{"tool", "tool-result:"}, []string{"assistant", "text:d", "text:e"}}
SplitsAdjacentReasoningBlocks
actual: []string{"first @05:06:01", "thought@05:06:01", "second @05:06:02", "thought@05:06:02"}
SplitsNarrationFromAnswer
actual: []string{"narration=true:Checking the ", "narration=true:repo.", "narration=false:The answer ", "narration=false:is 42.", "narration=true:Next, the tests."}

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

@linear-code

linear-code Bot commented Sep 30, 2026

Copy link
Copy Markdown

CODAGT-1115

@mafredri
mafredri force-pushed the fix/chat-interrupted-turn-parts branch from ddb90bd to 8973b1a Compare October 7, 2026 11:20
@mafredri

mafredri commented Oct 7, 2026

Copy link
Copy Markdown
Member Author

@codex review
/coder-agents-review

@coder-agents-review

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

Copy link
Copy Markdown
Contributor

Chat: Review posted | View chat
Requested: 2026-10-09 15:10 UTC by @mafredri

Review history
  • R1 (2026-10-07): 9 reviewers, 3 Nit, 4 P3, 6 P4, COMMENT. Review
  • R2 (2026-10-07): 9 reviewers, 6 Nit, 6 P3, 10 P4, COMMENT. Review
  • R4 (2026-10-09): 4 reviewers, 6 Nit, 6 P3, 12 P4, COMMENT. Review
  • R5 (2026-10-09): 4 reviewers, 6 Nit, 6 P3, 12 P4, APPROVE. Review

deep-review v0.13.0 | Round 5 | 782fb30..b48cabb

Last posted: Round 5, 24 findings (6 P3, 12 P4, 6 Nit), APPROVE. Review

Finding inventory

Findings

# Sev Status Location Summary Round Reviewer Posted
CRF-1 P3 Author fixed (00af619) message_conversion.go:898 Metadata/timestamp merges in appendStreamedDelta are unreachable for current producers and untested R1 Netero Yes
CRF-2 P4 Author contested; panel closed R2 (1/1 accept) message_conversion.go:796 streamedRunIndex duplicates len(assistantParts)-1 R1 Netero Yes
CRF-3 P3 Author fixed (cbdf027) message_conversion_test.go:990 Benchmark and its comment size input at 3,987/4000 deltas, above what the 1 MiB episode buffer holds (~2,585) R1 Pariston P2, Gon P2 Yes
CRF-4 P3 Author fixed (d6b5985) message_conversion.go:792 streamedRun field comment omits stale-Text and close-before-append invariant; describes rejected first commit R1 Gon P2, Leorio P4, Mafuuu Nit Yes
CRF-5 P3 Author fixed (d6b5985) message_conversion.go:882 appendStreamedDelta doc claims parity with processStepStream that the code lacks R1 Gon P2, Leorio P4, Mafuuu Note Yes
CRF-6 P4 Author fixed (82b8e65) message_conversion.go:886 appendStreamedDelta bool return undocumented; name says append R1 Gon P2, Leorio P4 Yes
CRF-7 P4 Author fixed (82b8e65) message_conversion.go:1004 toolCall appends directly and closes the run by hand; route through appendAssistantPart R1 Knov Yes
CRF-8 P4 Author fixed (2022aca) message_conversion.go:920 Redundant streamedRun.Reset in appendAssistantPart R1 Bisky Yes
CRF-9 P4 Author fixed (cbdf027) message_conversion_test.go:1012 Benchmark uses ResetTimer + range b.N instead of b.Loop R1 Ging-go Yes
CRF-10 P4 Author fixed (e87dde9) message_conversion.go:832 Whitespace-only deltas now render in interrupted rows; no test pins it R1 Mafuuu Note, Ryosuke Note Yes
CRF-11 Nit Author fixed (d6b5985) message_conversion.go:910 appendAssistantPart doc comment restates body R1 Gon P2 Yes
CRF-12 Nit Author fixed (d6b5985) message_conversion.go:924 closeStreamedRun doc comment restates body R1 Gon P2 Yes
CRF-13 Nit Author fixed (d6b5985) message_conversion_test.go:954 Test comment repeats assertion message R1 Gon P2 Yes
CRF-14 OOS Out of scope (body) message_conversion.go:882 Interrupted rows stored before this PR keep per-token parts R1 Pariston Note, Killua No
CRF-15 OOS Out of scope (body) generation.go:1414 publish discards AddPart ErrEpisodeFull; long interrupted turns lose their tail silently R1 Pariston No
CRF-16 OOS Out of scope (body) messagepartbuffer/message_part_buffer.go:309 Episode cap charges full JSON envelope per delta (~400 B per 7 chars) R1 Pariston No
CRF-17 OOS Out of scope (body) codersdk/chats.go:485 ChatMessagePart serializes every empty field (367 B per 7-char part) R1 Pariston No
CRF-18 OOS Out of scope (body) site/.../messageParsing.ts:23 Frontend drops whitespace-only parts, so pre-PR interrupted rows render joined words R1 Ryosuke, Mafuuu No
CRF-19 OOS Out of scope (body) *_test.go (repo) 48 benchmarks use range b.N instead of b.Loop R1 Ging-go No
CRF-20 Note Dropped by orchestrator (positive observation, no action requested) message_conversion_test.go:951 New test pins each run boundary R1 Bisky No
CRF-21 Note Dropped by orchestrator (positive observation, no action requested) PR description Scope paragraph is useful R1 Leorio No
CRF-22 P3 Author fixed (fdd9f06) message_conversion.go:1028 No test closes an open run at a mid-stream flushAssistant (durable tool result while a run is open) R2 Bisky Yes
CRF-23 P3 Author fixed (ed7727d) message_conversion.go:792 streamedRun comment says close before reading assistantParts, but appendAssistantPart reads Type/CreatedAt with the run open R2 Gon P2 Yes
CRF-24 P4 Author fixed (ed7727d) message_conversion.go:792 streamedRun comment no longer says why a builder is used R2 Leorio Yes
CRF-25 P4 Author fixed (32ae36e) message_conversion.go:887 Reasoning run boundary relies on chatloop stamping block start time on every delta; no test pins the producer contract R2 Ryosuke Yes
CRF-26 P4 Author fixed (PR description edit) PR description 1 MiB / 367 B is 2,857; 2,585 comes from the 406 B envelope R2 Leorio Yes
CRF-27 P4 Author fixed (7d4655e) message_conversion_test.go:997 reasoning closure duplicates withCreatedAt; NotNil+deref duplicates requireNotNilTime R2 Zoro Yes
CRF-28 Nit Author contested; panel closed R3 (1/1 accept) message_conversion.go:879 appendAssistantPart doc first clause restates body R2 Gon P2 Yes
CRF-29 Nit Author contested; panel closed R3 (1/1 accept) message_conversion_test.go:1027 Benchmark comment restates fixture; cap not tied to defaultMaxEpisodeBytes R2 Gon P2 Yes
CRF-30 Nit Author fixed (78b0261) message_conversion.go:883 Local streamed overloads a term used by modelStreamedAssistant; mergeable R2 Gon Yes
CRF-31 OOS Out of scope (body) message_conversion.go:828 Every interrupted reasoning part gets CompletedAt=interruptedAt even when ReasoningEnd already fired R2 Pariston No
CRF-32 OOS Out of scope (body) message_conversion.go:789 modelStreamedAssistant means "model produced content", not "streamed" R2 Gon No
CRF-33 Note Dropped by orchestrator (positive observation, no action requested) message_conversion.go:879 Merge rule doc matches producers R2 Leorio, Zoro, Bisky, Gon No
CRF-34 P4 Author fixed (44544e2) message_conversion.go:887 Reasoning blocks split on microsecond-rounded start time; two blocks starting in the same microsecond merge R3 Komugi Yes
CRF-35 OOS Out of scope (body) chatloop/chatloop.go:1000 Buffered reasoning deltas carry no provider block ID, so block identity has to be inferred from wall time R3 Komugi No
CRF-36 Note Dropped by orchestrator (positive/no-action observation) message_conversion.go:913 sameTime nil branch uncovered and unreachable in production R3 Netero, Bisky No
CRF-37 Note Dropped by orchestrator (positive observation) message_conversion_test.go:996 R2 fix tests verified by mutation; chatloop test deterministic R3 Bisky, Komugi No
CRF-38 P4 Author fixed (PR description edit) PR description Fix section says each reasoning block keeps its own part; same-microsecond blocks merge R4 Leorio Yes
CRF-39 Note Dropped by orchestrator (positive observation) message_conversion.go:880 CRF-34 doc comment accurate; tests deterministic R4 Komugi, Gon, Leorio No

Contested and acknowledged

CRF-2 (P4, message_conversion.go:796) - streamedRunIndex duplicates len(assistantParts)-1

  • Finding: streamedRunIndex always equals len(s.assistantParts)-1 while streamedRunOpen is true; proposed using the last element of assistantParts and dropping the field so it cannot drift from the slice.
  • Author defense: With one append path (82b8e65, toolCall now appends through appendAssistantPart) the index cannot drift from the slice, and the explicit index states which part the run belongs to. Left as is.
  • Panel closure (R2, 1/1): Netero, the agent that raised it (reused via message_agent), verified appendAssistantPart is the only site that grows assistantParts (toolCall at :988 calls it) and flushAssistant closes the run before resetting the slice (:1028), so the index cannot point at another part; no runtime defect remains.

CRF-28 (Nit, message_conversion.go:879) - appendAssistantPart doc first clause restates body

  • Finding: The first clause ("appends part, merging a delta into the open run of its type") repeats the body; keep only the second sentence (text merges across blocks; reasoning run ends when CreatedAt changes).
  • Author defense: That clause is the doc comment's summary: it says what the method does, and the merge is not in its name. Left as is.
  • Panel closure (R3, 1/1): Gon, the agent that raised it (reused R2 agent via message_agent), accepted: the clause states that the method merges as well as appends, which the name does not say, so it is contract, not restatement.

CRF-29 (Nit, message_conversion_test.go:1027) - Benchmark comment restates fixture; cap not tied to defaultMaxEpisodeBytes

  • Finding: The benchmark comment opens by restating the fixture and gives the cap as "1 MiB" without naming defaultMaxEpisodeBytes.
  • Author defense: The first sentence is the benchmark's doc summary, and 2,585 is exact against the 1 MiB cap. Naming the unexported constant from messagepartbuffer adds a lookup, not information. Left as is.
  • Panel closure (R3, 1/1): Gon, the agent that raised it (reused R2 agent via message_agent), accepted: naming the constant would not keep 2,585 correct if the constant changed, and benchmark behavior does not depend on the comment.

Round log

Round 1

Round type: round 1 (full panel). Netero: 1 P3, 1 P4, no P0, panel proceeds.
Panel: ging-go, killua, ryosuke (Go specialists), pariston, mafuuu (core), bisky (tests changed), gon, leorio (floor), knov (wildcard). 9 total, 8 plus wildcard (focused target). Hisoka not added to stay in target.
Severity adjustments: Pariston P2 at :886 -> CRF-3 P3: keep-argument was that long interrupted turns stay truncated silently; the truncation lives in generation.go/messagepartbuffer, which the PR does not change (CRF-15, CRF-16 OOS), and the in-scope defect is the benchmark/description sizing. Gon P2 comment findings: keep-argument for P2 was that a false or missing comment misleads the next change; no runtime behavior is wrong, so P3 for false/missing contracts (CRF-4, CRF-5), P4 for naming (CRF-6), Nit for comments that restate the body (CRF-11..13, AGENTS.md comment rule).
Interactions: CRF-1 and CRF-5 (deleting unreachable merges removes the metadata sentence of the doc comment). CRF-4, CRF-7, CRF-2 (single append path reduces the invariant the comment must state). CRF-3 and CRF-15/16.
Result: 4 P3, 6 P4, 3 Nit, 6 OOS. Reviewed against 01aee8d..8973b1a.

Round 2

Churn guard: PROCEED. 12 author fixed, CRF-2 contested. Round type: restructure (appendStreamedDelta deleted, appendAssistantPart rewritten to own the merge, new sameTime helper, new reasoning-split behavior from 8394793).
Netero (reused R1 agent): no new findings; accepted CRF-2 defense (1/1). Panel: ging-go, killua, ryosuke, pariston, mafuuu, bisky, gon, leorio + zoro (wildcard). All reviewers reused new agents except Netero; no other contested findings.
Prior fixes verified by reviewers: CRF-1, 3, 4 (partially, see CRF-23), 5, 6, 7, 9, 10.
Severity adjustments: Gon P2 x3 comment findings -> CRF-23 P3 (inaccurate rule, same calibration as CRF-4), CRF-28/29 Nit (restating comments, R1 calibration). Keep-argument for P2: an inaccurate comment invites a wrong edit; no runtime behavior is wrong, so P3 holds. CRF-26 has no file line; posted as a folded body item via the author's latest issue comment.
Result: 2 P3, 4 P4, 3 Nit, 2 OOS. Reviewed against 5ca77e2..d6b5985.

Round 3

Churn guard: PROCEED. CRF-22..27, CRF-30 author fixed; CRF-28, CRF-29 contested (Gon). Round type: fix round (commits only address R2 findings; chatloop_internal_test.go added to the diff only as the CRF-25 test).
Netero (new agent; R1/R2 Netero agent not reused since no Netero finding contested): no findings, 1 Note. Panel: gon (reused, dispute check-in), bisky, leorio, ryosuke, zoro, ging-go (authors of R2 findings / specialists for changed Go and test files), komugi (test and timestamp determinism), chopper (wildcard). Fix round, 8 panel reviewers.
Workspace agent disconnected mid-panel; bisky and komugi wrote their output files after the workspace returned, using the same agents.
CRF-34: verified clockNow = dbtime.Time = Round(time.Microsecond). Keep-argument for higher: two thinking blocks silently merge with block 0's timestamp. Kept at P4: needs two ReasoningStart events within 1 us, and the frontend already joins adjacent same-type reasoning parts. Root-cause fix (block ID on deltas) touches chatloop.go production code, so CRF-35 OOS.
Result: 1 P4, 1 OOS. Reviewed against a4b68ac..5d7f912.

Round 4

Churn guard: PROCEED. CRF-34 author fixed (44544e2). Round type: fix round (only change since R3 head 5d7f912 in PR files is a 2-line doc comment edit in appendAssistantPart).
Netero (new agent): no findings. Panel: komugi (raised CRF-34, code touched), gon and leorio (doc comment changed), meruem (wildcard). Fix round, 4 panel reviewers.
CRF-34 verified fixed by Netero, Komugi, Gon. CRF-38 verified by orchestrator in meta/pr.json body line 7 ("each block keeps its own part"). Posted as a reply on the author's latest issue comment (folds into body; no file line).
Result: 1 P4. Reviewed against 6bae268..44544e2.

Round 5

Churn guard: PROCEED. CRF-38 author fixed (description edit). Round type: fix round. Rebase onto 782fb30 only; git range-diff 6bae268487..44544e22d8 782fb30f09..HEAD shows every PR commit unchanged, and PR files are byte-identical to the R4 head.
Netero (new agent): no findings. Panel: leorio (raised CRF-38), gon, mafuuu (core) + zoro (wildcard). Fix round, 4 panel reviewers. All: no findings. CRF-38 verified against meta/pr.json body line 7.
No new findings. All in-scope findings resolved. Reviewed against 782fb30..b48cabb.

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-22T10:22:32.986812Z cd5edc7 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: 8973b1a1d8

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

Comment thread coderd/x/chatd/message_conversion.go Resolved

@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 merges adjacent text and reasoning deltas into one part each when an interrupted turn is persisted. Findings: 4 P3, 6 P4, 3 Nit. The P3s are an unreachable and untested merge path, a benchmark sized above the episode buffer cap, and two comments that state the wrong contract.

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

  • coderd/x/chatd/generation.go:1414: publish discards ErrEpisodeFull from AddPart, so an interrupted turn longer than about 18 KB of streamed text is persisted without its tail and nothing is logged.
  • coderd/x/chatd/messagepartbuffer/message_part_buffer.go:309: the episode cap charges each delta its full JSON envelope (about 400 bytes for 7 characters), so 1 MiB holds about 18 KB of text.
  • codersdk/chats.go:485: ChatMessagePart serializes every empty field (367 bytes for a 7-character text part), which inflates the buffer, stream replay, and stored completed messages.
  • coderd/x/chatd/message_conversion.go:882: interrupted rows stored before this PR keep one part per token, so /messages pages containing them stay multi-megabyte.
  • site/src/pages/AgentsPage/components/ChatConversation/messageParsing.ts:23: whitespace-only parts are dropped, so those older rows render with words and paragraphs joined.
  • Repository _test.go files: 48 benchmarks use for range b.N against 9 using b.Loop().

🤖 This review was automatically generated with Coder Agents.

Comment thread coderd/x/chatd/message_conversion.go Resolved
Comment thread coderd/x/chatd/message_conversion.go Resolved
Comment thread coderd/x/chatd/message_conversion.go Resolved
Comment thread coderd/x/chatd/message_conversion.go Resolved
Comment thread coderd/x/chatd/message_conversion.go Resolved
Comment thread coderd/x/chatd/message_conversion.go Resolved
Comment thread coderd/x/chatd/message_conversion.go Resolved
@mafredri
mafredri force-pushed the fix/chat-interrupted-turn-parts branch from 8973b1a to d6b5985 Compare October 7, 2026 16:01
@mafredri

mafredri commented Oct 7, 2026

Copy link
Copy Markdown
Member Author

@codex review
/coder-agents-review

@chatgpt-codex-connector

Copy link
Copy Markdown

Codex Review: Didn't find any major issues. Bravo.

Reviewed commit: d6b5985922

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

@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 round re-reviews the restructured merge: appendAssistantPart now does every append, and a reasoning run ends when CreatedAt changes. Findings: 2 P3, 4 P4, 3 Nit.

Twelve round-1 findings are fixed; the explanation for keeping streamedRunIndex is accepted. The P3s are an untested mid-stream run close and a field comment whose rule contradicts appendAssistantPart.

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

  • coderd/x/chatd/message_conversion.go:828: every interrupted reasoning part gets CompletedAt = interruptedAt, including blocks whose ReasoningEnd already fired, so a finished block shows a duration that runs to the interrupt.
  • coderd/x/chatd/message_conversion.go:789: modelStreamedAssistant is true for tool calls and reasoning too, so the name says "streamed" where it means "the model produced content".

In reply to IC_kwDOGkVX1s8AAAABaCX6JA:

P4 [CRF-26] The PR description says a seven-character part "serializes to about 367 bytes, so the 1 MiB episode buffer stops after about 2,585" deltas, but 1 MiB / 367 B is 2,857. (Leorio)

The buffer charges serializedPartBytes (messagepartbuffer/message_part_buffer.go:658), which marshals the {seq, role, part} envelope: 406 B for that part, and 1,048,576 / 406 = 2,582. State both numbers so the benchmark sizing can be checked.

🤖

🤖 This review was automatically generated with Coder Agents.

Comment thread coderd/x/chatd/message_conversion.go Resolved
Comment thread coderd/x/chatd/message_conversion.go Resolved
Comment thread coderd/x/chatd/message_conversion.go Resolved
Comment thread coderd/x/chatd/message_conversion.go Resolved
Comment thread coderd/x/chatd/message_conversion.go Resolved
Comment thread coderd/x/chatd/message_conversion.go Resolved
@mafredri
mafredri force-pushed the fix/chat-interrupted-turn-parts branch from d6b5985 to 5d7f912 Compare October 8, 2026 11:38
@mafredri

mafredri commented Oct 8, 2026

Copy link
Copy Markdown
Member Author

In reply to CRF-26 (P4, coderd/x/chatd/messagepartbuffer/message_part_buffer.go:658): #29661 (review)

Partly. 1 MiB / 367 B is not 2,585, but 2,582 is not right either: the envelope is 403 to 406 B as seq gains digits, and charging serializedPartBytes until defaultMaxEpisodeBytes fills gives exactly 2,585. The description now says the buffer charges about 405 B per delta and keeps 2,585.

🤖 Posted using /amend-review skill via Coder Agents.

@mafredri

mafredri commented Oct 8, 2026

Copy link
Copy Markdown
Member Author

@codex review
/coder-agents-review

🤖 This review was automatically generated with Coder Agents.

@chatgpt-codex-connector

Copy link
Copy Markdown

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

Reviewed commit: 5d7f912dc3

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

@mafredri

mafredri commented Oct 8, 2026

Copy link
Copy Markdown
Member Author

/coder-agents-review

🤖 This review was automatically generated with 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 round checks the fixes for round 2. Findings: 1 P4.

Seven round-2 findings are fixed: CRF-22 through CRF-27 and CRF-30. The new tests fail when their targets are mutated. The explanations for the two doc-comment Nits are accepted.

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

  • coderd/x/chatd/chatloop/chatloop.go:1000: buffered reasoning deltas carry no provider block ID, so interrupted-turn persistence has to infer block identity from the start time.

🤖 This review was automatically generated with Coder Agents.

Comment thread coderd/x/chatd/message_conversion.go Resolved
@mafredri
mafredri force-pushed the fix/chat-interrupted-turn-parts branch from 5d7f912 to 44544e2 Compare October 9, 2026 10:35
@mafredri

mafredri commented Oct 9, 2026

Copy link
Copy Markdown
Member Author

@codex review
/coder-agents-review

🤖 This review was automatically generated with Coder Agents.

@chatgpt-codex-connector

Copy link
Copy Markdown

Codex Review: Didn't find any major issues. Another round soon, please!

Reviewed commit: 44544e22d8

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

@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 round checks the fix for the same-microsecond reasoning merge (now documented in the appendAssistantPart comment). Findings: 1 P4, in the PR description. It is in the item below, because it has no code line to attach to. No code findings remain.


In reply to IC_kwDOGkVX1s8AAAABaluopA:

P4 [CRF-38] The PR description's Fix section says reasoning deltas split on CreatedAt "and each block keeps its own part", which the new appendAssistantPart comment contradicts: blocks that start in the same microsecond merge. (Leorio)

clockNow rounds to the microsecond (chatloop.go:955, dbtime.go:17), so two ReasoningStart events in one microsecond produce equal CreatedAt. Suggested: "Reasoning deltas carry their block's start time, rounded to the microsecond, as CreatedAt, so a reasoning run ends when CreatedAt changes; two blocks that start in the same microsecond merge into one part."

🤖

🤖 This review was automatically generated with Coder Agents.

@mafredri

mafredri commented Oct 9, 2026

Copy link
Copy Markdown
Member Author

In reply to CRF-38 (P4, PR description:1): #29661 (review)

The Fix section now says reasoning blocks that start in the same microsecond merge into one part, matching the appendAssistantPart comment.

🤖 Posted using /amend-review skill via Coder Agents.

@mafredri

mafredri commented Oct 9, 2026

Copy link
Copy Markdown
Member Author

$@codex review\n/coder-agents-review\n\n> 🤖 This review was automatically generated with Coder Agents.

@chatgpt-codex-connector

Copy link
Copy Markdown

Codex Review: Didn't find any major issues. More of your lovely PRs please.

Reviewed commit: b48cabbb2f

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

@mafredri

mafredri commented Oct 9, 2026

Copy link
Copy Markdown
Member Author

/coder-agents-review

🤖 This review was automatically generated with Coder Agents.

@mafredri
mafredri marked this pull request as ready for review October 22, 2026 10:21
@chatgpt-codex-connector

Copy link
Copy Markdown

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

Reviewed commit: cd5edc7b5c

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

An interrupted turn was persisted from the buffered stream deltas one
part per token. A completed turn takes another path and stores one text
part per run, so the two shapes diverged: a 28 KB interrupted response
became 3,987 text parts and 1 MB of JSON, each part repeating the same
empty fields, and every client that loaded the page parsed and retained
thousands of objects for it. Interrupts are routine (stop button, a new
message sent while running), so long chats accumulate these.

Merge adjacent text and reasoning deltas into the previous part while
converting buffered parts, mirroring how processStepStream accumulates
a run: keep the latest non-empty provider metadata, the first start
time and the latest completion time. Tool-call deltas were already
merged. The new test fails on the previous code with 8 parts where a
completed turn would store 4.
Merging each delta with prev.Text += part.Text copied the whole run
so far on every token: a 3,987-delta interrupted turn allocated 61 MB
to persist 28 KB of text. Keep the open run in a strings.Builder and
write it back when the run closes. Same input now allocates 2.6 MB.
Benchmark added; the tool-call accumulators already used builders.
"Linear" named no variable, and a benchmark comment said allocations
"must" grow linearly when a benchmark performs no such check. Say
linear in N, and tell the reader to compare B/op across the two sizes.
…cannot show

The comments narrated the merge steps and call sites visible below
them. Keep the reason for the builder and the parity with
processStepStream; drop the rest.
Reasoning deltas from consecutive provider blocks are adjacent in the
buffer, so merging by type alone joined them into one part and dropped
the second block's start time. Every delta of a block carries the
block's start time as CreatedAt, so a changed CreatedAt starts a new
part, as the completed path keeps one part per block.
No producer sets ProviderMetadata or CompletedAt on text or reasoning
deltas, and every reasoning delta in a run now has the same CreatedAt,
so the merges never changed a value and no test reached the metadata
branch. The run keeps its first delta's fields.
toolCall appended its placeholder directly and had to close the open
run itself, and the merge helper returned a bool its name did not
explain. appendAssistantPart now either merges a delta into the open
run or closes the run and appends, so it is the only code that grows
assistantParts.
closeStreamedRun resets the builder whenever a run was open, so the
builder is already empty when a new run opens.
The 1 MiB episode buffer stops accepting seven-byte text deltas after
about 2,585, so the 4,000-delta case measured an input the interrupt
path cannot receive. Use 2,500, and b.Loop so setup stays out of the
timing without ResetTimer.
On main each whitespace-only delta was its own part, and both the
frontend and prompt replay skip whitespace-only text parts, so words
and paragraphs ran together. Merged into the run, the whitespace is
kept; the test now covers it.
The comments claimed parity with completed turns and metadata merging
that the code does not do, described an earlier commit, and left out
that the run's part holds stale text until the run closes. State the
merge rule and the stale-text rule, drop the comment that restated
closeStreamedRun, and say why the test feeds whitespace-only deltas.
An interrupted turn merged every adjacent text delta into one part with
the first delta's fields, so narration followed by answer text was
stored as one narration part. The web client folds narration into the
working block, so the partial answer disappeared into it after the
interrupt. A text run now ends when Narration changes, which is also
where the web client splits streamed text.
@mafredri
mafredri force-pushed the fix/chat-interrupted-turn-parts branch from cd5edc7 to 94698e8 Compare October 23, 2026 10:06
…ect the episode-cap count

The benchmark comment said a 1 MiB episode holds about 2,585 seven-byte deltas; on current main the part serializes to 385 bytes and the buffer charges 421 to 424 bytes, so it holds about 2,475. The whitespace test comment no longer says the frontend drops whitespace-only parts, which #30551 changed.
@mafredri
mafredri merged commit 3c36790 into main Oct 23, 2026
33 checks passed
@mafredri
mafredri deleted the fix/chat-interrupted-turn-parts branch October 23, 2026 20:59
@github-actions github-actions Bot locked and limited conversation to collaborators Oct 23, 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