Skip to content
Source
Sign inSign up
pingdotgg/t3code

perf(server): streamed replies are no longer rescanned from the top #19990

ClosedEmanuelMedinaAraujo(GitHub profile, opens in a new tab)wants to merge 1 commit intomain fromrefs/pull/19990/headopened View on GitHub

Problem

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.
  • splitBufferedAssistantText keeps 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 splitBufferedAssistantText scan 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.
  • assistantStreaming.test.ts and RunExecutionService.test.ts pass (63 tests). Server typecheck, lint and format are clean.

Benchmark (local, Node 26, 10-character appends at ~66 updates/s; harness overhead ~2 ms):

CaseBeforeAfter
50 KB reply inside a code fence282 ms12 ms
~50 KB paragraph with no newline129 ms9.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

    2. coderabbitai[bot](GitHub profile, opens in a new tab)commented
      Review in Change Stack →

      No actionable comments were generated in the recent review. 🎉

      ℹ️ Recent review info
      ⚙️ Run configuration
      • Configuration used: Path: .coderabbit.config.ts
      • Review profile: CHILL
      • Plan: Advanced
      • Run ID: 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.ts
      • apps/server/src/orchestration-v2/assistantStreaming.ts

      Included 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

      Layer / File(s)Summary
      Resumable text scanning
      apps/server/src/orchestration-v2/assistantStreaming.ts, apps/server/src/orchestration-v2/assistantStreaming.test.ts
      The scanner stores its state and resumes from the last scanned prefix. It checks partial lines for complete list markers and preserves existing boundary behavior. Tests cover partial markers.
      Per-message streaming integration
      apps/server/src/orchestration-v2/assistantStreaming.ts, apps/server/src/orchestration-v2/assistantStreaming.test.ts
      The filter caches a splitter per message and removes it when streaming ends. Tests compare incremental deliveries with fresh scans and check behavior when streamed text is replaced.

      Priority: ➖ Normal

      Estimated code review effort: 3 (Moderate) | ~20 minutes

      Change: Refactor

      Suggested reviewers: juliusmarminge

      Merge Risk: ⚪ Minimal · up to 0b574

      This is a server-side performance change that preserves delivered streaming output. No concrete merge-blocking risk was identified.

      Pre-merge checks: Passed 4
      Check nameStatusExplanation
      Linked Issues checkPassedCheck skipped because no linked issues were found for this pull request.
      Out of Scope Changes checkPassedCheck skipped because no linked issues were found for this pull request.
      Title checkPassedThe title is concise, specific, and accurately describes the main performance change: streamed replies no longer rescan from the beginning.
      Description checkPassedThe description includes all required sections. It clearly explains the problem, implementation, scope and approval, focused verification, benchmark results, and limitations.
      ✨ Finishing Touches
      🧪 Generate unit tests (beta)
      • Create a new PR
      • Autofix · Keep fixing CodeRabbit findings and required CI, and resolving merge conflicts
      Comment `@coderabbitai help` to get the list of available commands.
    3. Note

      Written by claude-opus-5-5 on behalf of Maria

      Thanks 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.

    4. maria-rcks closed this

    5. EmanuelMedinaAraujo(GitHub profile, opens in a new tab)commented

      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 0b57497ef61ffd66bf0b63f5138f476ca004e9d2 with parent 26fffe5aee22ea9bdd39c5b4c400f643c560b4a0.

      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 measurementOriginalCandidate
      Total time across the run437.30 ms35.03 ms
      Slowest individual invocation0.371 ms0.159 ms
      Text-event invocation p950.189 ms0.0098 ms
      Warmed replay median total for the captured events402.30 ms16.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 sizeUpdatesSimulated durationOriginal total scan timeCandidate total scan time
      50 KB1,35477 seconds54.6–60.0 ms1.14–1.26 ms
      250 KB6,8166.47 minutes1.38–1.39 seconds14.9–15.0 ms
      1 MB27,26625.89 minutes22.8–23.0 seconds218–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.ts

      Measured with GPT-6.1-Sol through the Codex harness in T3 Code.

Sign in to join this conversation.