perf(server): streamed replies are no longer rescanned from the top #19990
main fromrefs/pull/19990/head·opened View on GitHubProblem#
While a reply streams, makeAssistantStreamingFilter decides how much of it is safe to deliver by calling splitBufferedAssistantText on the whole accumulated text. Inside a code fence or a long paragraph no new boundary appears, so once the 400 ms window has passed, every update rescans the entire reply from the top, line by line with regexes. Total work grows quadratically with reply length and runs on the server event loop.
Change#
- The boundary scan is resumable. The filter keeps one incremental splitter per streaming message. It resumes from the last fully terminated line while the text keeps growing, and rescans from the start when a provider replaces the text.
- The partial last line's list-item lookahead now matches in place instead of copying and trimming the line, so a long paragraph with no newline yet is not rescanned either.
splitBufferedAssistantTextkeeps its behavior; it is now a full scan through the same code.
Scope and approval#
Fixes #19992. This is a focused, behavior-preserving performance fix for an obvious defect: the filter's work per update grows with the full reply length. It changes no delivered output, contract, or client.
Verification#
assistantStreaming.test.ts:- Feeds every prefix of a mixed-markdown fixture through the filter and asserts it delivers exactly what a full
splitBufferedAssistantTextscan would, plus a replaced-text case. The fixture covers fences, nested, tab, CRLF and trailing-space list items, headings and bold titles. Breaking the resume state makes it fail. - A direct test for the partial-line list-item lookahead. It passes against both the new and the previous implementation, and fails if the trailing-whitespace check is removed.
- Feeds every prefix of a mixed-markdown fixture through the filter and asserts it delivers exactly what a full
assistantStreaming.test.tsandRunExecutionService.test.tspass (63 tests). Server typecheck, lint and format are clean.
Benchmark (local, Node 26, 10-character appends at ~66 updates/s; harness overhead ~2 ms):
| Case | Before | After |
|---|---|---|
| 50 KB reply inside a code fence | 282 ms | 12 ms |
| ~50 KB paragraph with no newline | 129 ms | 9.7 ms |
Follow-up verification (2026-10-17) exercised real Codex output through orchestration, persistence, and a headless WebSocket client. See the provider/client measurements and limitations. Browser rendering for the final long run remains unverified.
Implemented with Claude Opus 5.5 in T3 Code. Reviewed with GPT-6.1-Sol.
🤖 Generated with Claude Code
- 1
github-actions[bot](GitHub profile, opens in a new tab) added labelsvouch:unvouched
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: The incremental assistant-text scanner is a performance change supported by an isolated benchmark. Please establish its impact through an actual provider and client streaming path and agree on scope with maintainers first.
If you think this was a mistake, message me on Twitter at @maria_rcks and I'll take another look.
maria-rcks closed this
Follow-up measurements: real provider and client streaming path#
I followed up on the request to establish the impact through an actual provider and client path. The optimization reduces scan work, but the measured absolute saving is modest for one session. These results do not establish a noticeable day-to-day UI speedup or replace agreement on scope.
When this change matters#
The relevant input is a long individual streaming reply, particularly a multi-line fenced code block whose delivery boundary does not advance while more text arrives. A long thread can contain such a reply, but a large number of earlier turns alone does not increase this scanner's input.
The Codex adapter coalesces text updates every 50 ms before this filter. Provider behavior also matters: in the earlier Claude Haiku check, the 45 KB answer arrived as completed text on the tested adapter path and bypassed incremental answer scanning. That check does not rule out benefits for Claude reasoning or other paths.
Real Codex run#
Environment: Linux, Node v26.8.2, Intel Core i7-11800H, isolated worktree-local development state, paragraph streaming mode. Compared PR head
0b57497ef61ffd66bf0b63f5138f476ca004e9d2with parent26fffe5aee22ea9bdd39c5b4c400f643c560b4a0.Path exercised: Codex GPT-6.1-Sol → actual adapter → RunExecutionService/filter → event ingestion and persistence → authenticated WebSocket client.
Workload: one fenced TypeScript block containing 1,500 sequential export statements, followed by a completion marker. The client received 112,932 answer characters over 375.93 seconds. There were 4,016 incremental text events through the filter.
Both implementations processed the same actual adapter events and timestamps in the running server, alternating measurement order. Each call was timed with
performance.now(). The candidate supplied the live delivery path; the original ran alongside it for comparison. This was one provider run, not two independent whole-app latency runs.Filter measurement Original Candidate Total time across the run 437.30 ms 35.03 ms Slowest individual invocation 0.371 ms 0.159 ms Text-event invocation p95 0.189 ms 0.0098 ms Warmed replay median total for the captured events 402.30 ms 16.91 ms Correctness: All 1,500 received lines exactly matched their requested sequential contents, and the completion marker arrived. There were zero live delivery/text mismatches; returned filtered events also matched on replay.
Practical effect: About 402 ms of filter work saved across more than six minutes. The original's slowest measured scan was already below 0.4 ms. These are function timings, not a measured reduction in provider generation time, whole-process CPU, or client rendering latency.
Separate synthetic scaling check#
The following uses generated longer fenced fixtures with chunk sizes and mean arrival spacing taken from the earlier recorded 45 KB Codex run. It measures the filter only. The simulated stream duration is represented by timestamps; the test does not wait that long, generate these fixtures with a model, or exercise the server/client path.
Generated reply size Updates Simulated duration Original total scan time Candidate total scan time 50 KB 1,354 77 seconds 54.6–60.0 ms 1.14–1.26 ms 250 KB 6,816 6.47 minutes 1.38–1.39 seconds 14.9–15.0 ms 1 MB 27,266 25.89 minutes 22.8–23.0 seconds 218–220 ms There was a full warmup and two measured passes per implementation, with alternating order. The largest original invocation in the 1 MB fixture was 3.1 ms. Its cumulative saving is spread across a simulated 26-minute stream; it is not a 23-second pause removed from the UI.
Reproduction and limits#
For the live workload, ask the provider to emit all 1,500 lines inside one fence. Line N is
export const rowNNNN = { id: N, label: "streaming performance sample" };, with NNNN zero-padded to four digits. Temporarily compare the two filter factories on the same incoming events and timestamps, record per-call durations, and check both delivery/null decisions and returned text. Capture deltas for replay instead of retaining every full text prefix. For the recorded-event comparison above, the first two replay rounds were warmups and the next seven were measured per implementation.Focused correctness tests passed (63 tests):
vp test run apps/server/src/orchestration-v2/assistantStreaming.test.ts apps/server/src/orchestration-v2/RunExecutionService.test.tsMeasured with GPT-6.1-Sol through the Codex harness in T3 Code.
Sign in to join this conversation.
No actionable comments were generated in the recent review. 🎉
ℹ️ Recent review info
⚙️ Run configuration
fe4f4624-b239-4629-8a79-5d933ffd87e4📥 Commits
Reviewing files that changed from the base of the PR and between 26fffe5aee22ea9bdd39c5b4c400f643c560b4a0 and 0b57497ef61ffd66bf0b63f5138f476ca004e9d2.
📒 Files selected for processing: 2
apps/server/src/orchestration-v2/assistantStreaming.test.tsapps/server/src/orchestration-v2/assistantStreaming.tsIncluded review availability: This review used your included allowance. Your plan provides up to 10 included reviews per hour; 9 remain after this review.
📝 Walkthrough
The assistant streaming splitter now resumes scans when text retains its scanned prefix and restarts when that prefix changes. It checks incomplete final lines for list markers. The streaming filter stores a splitter per message and clears it when streaming ends.
Changes: Assistant streaming splitter
apps/server/src/orchestration-v2/assistantStreaming.ts,apps/server/src/orchestration-v2/assistantStreaming.test.tsapps/server/src/orchestration-v2/assistantStreaming.ts,apps/server/src/orchestration-v2/assistantStreaming.test.tsPriority: ➖ Normal
Estimated code review effort: 3 (Moderate) | ~20 minutes
Change: Refactor
Suggested reviewers:
juliusmarmingeMerge Risk: ⚪ Minimal · up to
0b574This is a server-side performance change that preserves delivered streaming output. No concrete merge-blocking risk was identified.
Pre-merge checks:
4
✨ Finishing Touches
🧪 Generate unit tests (beta)
- Autofix · Keep fixing CodeRabbit findings and required CI, and resolving merge conflicts
Comment `@coderabbitai help` to get the list of available commands.