perf(server): stop re-emitting whole assistant text on every delta #15767
main fromrefs/pull/15767/head·opened View on GitHubProblem#
The provider adapters already shipped ProviderTextDeltaCoalescer, but only Codex and Claude
adopted it. Every other adapter re-emitted the entire accumulated assistant text on every
single text delta:
CursorAdapterV2-- three full-text events per delta (node.updated,message.updated,turn_item.updated), each carrying the whole accumulated text. Shell output was worse: every shell delta re-serialized all accumulated output and re-scanned the tool map.OpenCodeAdapterV2-- the same three-event amplification on two paths,message.part.deltaand themessage.part.updatedsnapshot. The snapshot path is also per-token: this repo's own logger discards streaming text/reasoningmessage.part.updatedparts as transient (provider/Layers/EventNdjsonLogger.ts), which only makes sense if they repeat.PiAdapterV2-- one event per delta, plus a hand-rolled 50ms flush timer.OpenCode2AdapterV2-- the same three-event amplification onsession.text.delta. It is live for every OpenCode 2.x user, becauseOpenCodeDriver.tspicks v1 or v2 per probe result.
The cause is that wiring an adapter to the coalescer is a per-adapter decision with no shared helper at the seam, so the omission was invisible from the coalescer's own tests -- Codex and Claude passing says nothing about Cursor or Pi.
The cost is O(deltas x text length) of event and websocket bytes. A 4,000-token response put on the order of 8M characters on the wire for a single item. Users see this as dropped frames and stuttering output on long turns.
Change#
- Adopts the existing
ProviderTextDeltaCoalescerinCursorAdapterV2,OpenCodeAdapterV2,PiAdapterV2andOpenCode2AdapterV2. Pi's bespoke timer is deleted and the flush interval passed through unchanged, so cadence does not regress. - Adds
setTextto the coalescer, which replaces buffered text where a native event carries an authoritative full snapshot. This is what makes the OpenCode snapshot path coalescable without double-counting corrections and truncation. - Routes the OpenCode
message.part.updatedsnapshot branch throughsetTextrather than emitting directly, so that path stops triple-emitting too. - Memoizes Cursor's shell lookup, replacing a reverse scan over live tools on every shell delta.
Build output is where this hurts -- an
npm installstreams megabytes.
A session that closes mid-response still projects everything the coalescer had buffered, but it
does not settle the turn. Terminal-on-close was a separate, independent fix and has been split
out to #16181, along with the removal of OpenCode's write-only state.messages map, which
belongs to #15760.
Not changed: no contract, no client, and no tool-item projection. OpenCodeToolItems is
shared with the v1 adapter and is deliberately untouched, so tool items project identically
before and after.
Scope and approval#
Follow-up work from a static audit of the merged orchestrator-v2 work (#2829). The four adapters share one three-line pattern, so they are one problem rather than four, and splitting them across PRs would buy nothing.
This is the follow-through for a closed-as-done issue. #5110 ("Per-chunk assistant persistence causes progressive live output lag and rapid SQLite growth") was closed as completed on 2026-10-02. Its suggested direction was to "coalesce high-frequency assistant deltas before persistence and broadcast using a bounded time/size cadence" -- which is what #2829 shipped for Codex and Claude only, and what this branch does for the four adapters it missed. #4323 attempted the same thing in July and was closed unmerged, so the failure mode is known.
Verification#
pnpm exec vp test run on the four adapter suites -- 209/209 pass, no pre-existing failures.
pnpm --filter t3 typecheck -- exit 0. Targeted lint -- no new diagnostics.
The load-bearing tests, and what each one actually pins:
- Byte-identity. Coalesced text equals the un-coalesced input exactly, asserted with non-ASCII characters and a trailing newline so a naive UTF-16 truncation or a lost final newline cannot pass.
replaces what append buffered when setText carries a snapshot-- a direct coalescer test. This is the one that matters most, because the property is not observable through the OpenCode adapter: its emit callback reads the authoritative part and discards the coalescer's text, so append-vs-replace would be invisible there and every test would still pass.projects every text block in full when the execution ends(OpenCode2) -- a turn ending with a block still open must still project its full text. OpenCode2'sfinishTurnclearsstate.activeafter the flush for exactly this reason; had it stayed at the top, the emit callback would return early and every open block would silently lose its final text.- Ordering asserted on the event array via
findIndex, not on item ordinals. Ordinals are allocated at first delta and are order-independent of when an event was queued, so ordinal comparisons never actually proved emission order. Asserts reasoning-before-tool and that post-tool text is not pulled forward. - Every intermediate shell snapshot is a prefix of the final output, which catches truncation, duplication and reordering that a final-value-only check would miss.
- OpenCode's
{completed,failed,interrupted,teardown} x {delta,snapshot,mixed}matrix -- the snapshot and mixed arms prove a snapshot never doubles text. - Pi's flush timer is now genuinely exercised via
TestClock.adjust(50). Previously the test only advanced the clock by 0, soSTREAM_FLUSH_MSnever fired and every emission came from an explicit flush. Two batching bounds in that test could not fail under either implementation and were removed rather than left as decoration. - Session close flushes without settling -- each adapter's close path is asserted to project its buffered text and emit no terminal, pinning the boundary against the split to #16181.
Not verified: no live-client pass and no benchmark. Frame pacing and perceived streaming
smoothness on a long Cursor/OpenCode/Pi turn are untested end to end, and the wire-byte saving is
reasoned from the event shapes rather than measured. Per AGENTS.md this should be confirmed in a
real client before merge, since dropped frames are the user-visible symptom.
Known pre-existing issue, out of scope: before and thinking are each re-emitted as
completed twice -- once by handleAssistantCompleted and once by finalizeTurn -- because
completeTextPart does not check whether the part was already completed. Pre-existing and
unchanged by this branch.
Implemented with gpt-6.1-sol through the Codex harness (initial implementation) and opencode/space-bunny-free through the OpenCode harness (audit fixes and new coverage).
- 1
github-actions[bot](GitHub profile, opens in a new tab) added labelsvouch:trusted
github-actions[bot](GitHub profile, opens in a new tab) added labelssize:L
Note
Written by
claude-opus-5-5on behalf of MariaThanks for this. I'm closing it as part of PR triage. Right now we're only taking focused bug fixes from outside contributors (see Read this first), and this PR is not one.
From the review: Streaming still repeats accumulated text, but this four-adapter performance rewrite supplies no real-client measurements; pi already batches at the same interval.
It has also had no activity for 8 days. If this is still important to you, message me on Twitter at @maria_rcks and I'll take another look.
maria-rcks closed this
Sign in to join this conversation.
Note
This comment is posted by Julius' dot
The coalescing tests cover text preservation and event ordering. The Cursor last-shell cache also removes a separate per-delta tool-map scan. Can you explain why that cache is required for coalescing, or split it out under one problem per PR? Please also link the maintainer's agreed scope for extending the batching behavior across these adapters; #5110 establishes the reported problem but its comments do not approve this implementation.