Skip to content
Source
Sign inSign up
pingdotgg/t3code

fix(codex): avoid quadratic app-server input buffering #8605

Mergedjuliusmarmingemerged 2 commits intomain fromrefs/pull/8605/headmerged by juliusmarminge View on GitHub

juliusmarmingecommented

Closes #5389

What Changed

Updated Codex app-server JSONL parsing to retain incomplete input as fragments and scan each incoming chunk once, avoiding quadratic buffering for large messages. Added coverage for large fragmented notifications, UTF-8 and CRLF framing, and malformed final lines. Documented the framing behavior.

Why

Large app-server messages could require repeated concatenation and splitting of the pending input, causing quadratic processing and degraded performance. Fragment-based buffering preserves framing correctness while keeping parsing efficient.

UI Changes

Not applicable.

Checklist

  • This PR is small and focused
  • I explained what changed and why
  • I included before/after screenshots for any UI changes
  • I included a video for animation/interaction changes

Note

Medium Risk Changes core wire-protocol framing for all Codex app-server stdin traffic; behavior is heavily tested but mistakes could break large payloads or stream shutdown.

Overview Fixes quadratic stdin buffering in makeCodexAppServerPatchedProtocol when large JSONL messages arrive in many small chunks.

Incomplete lines are kept as a fragment array and each decoded chunk is scanned once for \n, instead of repeatedly concatenating the full pending buffer and splitting it. Complete lines are joined from fragments, trailing \r is stripped, and the tail stays fragmented until the next chunk or stream end.

On input stream close, any leftover fragments are joined and passed through handleLine (including parse errors for truncated JSON) before normal termination handling.

Tests cover a ~4MB notification split into 1KB chunks, JSONL/UTF-8/CRLF framing at chunk sizes 1/7/1024, and malformed fragmented response lines that fail the pending request.

Reviewed by [Cursor Bugbot](https://cursor.com/bugbot) for commit d310c203a308ed0becfaa2e5444859e7659d0208. Bugbot is set up for automated code reviews on this repo. Configure [here](https://www.cursor.com/dashboard/bugbot).

Note

Fix quadratic input buffering in makeCodexAppServerPatchedProtocol line assembly

Replaces the Ref<string> remainder with a local Array<string> fragment buffer in the stdin stream consumer. Each chunk is scanned once for newlines; completed lines are emitted by joining fragments, and only the unfinished tail is retained—avoiding repeated string concatenation across thousands of fragmented chunks.

  • At stream termination, any remaining fragments are now unconditionally joined and passed to handleLine, including when the remainder is empty or whitespace-only.
  • Adds tests covering large fragmented notifications, JSONL framing across varied chunk sizes, and malformed final-line error propagation.
  • Risk: handleLine is now called once more at termination with a possibly empty string; any handler that assumes it only receives non-empty content may behave differently.
Macroscope summarized d310c20.
    1. coderabbitai[bot](GitHub profile, opens in a new tab)commented

      Important

      Review skipped

      Auto reviews are disabled on this repository. Please check the settings in the CodeRabbit UI or the .coderabbit.yaml file in this repository. To trigger a single review, invoke the @coderabbitai review command.

      ⚙️ Run configuration

      Configuration used: Repository UI

      Review profile: CHILL

      Plan: Pro Plus

      Run ID: c1b524ff-de67-4067-b7cb-e10b9b568dd7

      You can disable this status message by setting the reviews.review_status to false in the CodeRabbit configuration file.

      Use the checkbox below for a quick retry:

      • 🔍 Trigger review

      Comment `@coderabbitai help` to get the list of available commands.
    2. github-actions[bot](GitHub profile, opens in a new tab) added labelsvouch:trusted

    3. github-actions[bot](GitHub profile, opens in a new tab)commented

      Thread transfer impact

      ✅ Thread transfer remains within every enforced ceiling.

      ProviderMetricMain baselineThis PRImpactPR ceiling
      CodexTotal thread wire13.3 KiB13.3 KiB+18 B (+0.1%)15.1 KiB✅
      CodexThread snapshot wire6.9 KiB6.9 KiB+1 B (+0.0%)7.3 KiB✅
      CodexLive turn WebSocket wire6.4 KiB6.5 KiB+17 B (+0.3%)7.8 KiB✅
      CodexLive turn WebSocket decoded55.6 KiB55.6 KiB0 B (0.0%)66.4 KiB✅
      CodexLive turn messages11110 (0.0%)21✅
      ClaudeTotal thread wire13.4 KiB13.4 KiB+5 B (+0.0%)15.1 KiB✅
      ClaudeThread snapshot wire6.9 KiB6.9 KiB+2 B (+0.0%)7.3 KiB✅
      ClaudeLive turn WebSocket wire6.5 KiB6.5 KiB+3 B (+0.0%)7.8 KiB✅
      ClaudeLive turn WebSocket decoded56.4 KiB56.4 KiB0 B (0.0%)66.4 KiB✅
      ClaudeLive turn messages11110 (0.0%)21✅

      Baseline: 2bc9e8e · PR result: d310c20 · Source CI: success

      Scenario and decoded snapshot size

      10 historical turns, 5 command tools per turn, 878.9 KiB retained MCP result per historical turn, and a 1.05 MiB retained result in the measured turn.

      • Codex decoded thread snapshot: 109.4 KiB
      • Claude decoded thread snapshot: 110.1 KiB

      Updated in place by a trusted workflow. PR artifacts are strictly validated and never executed.

    4. Approvability

      Verdict: Approved at d310c20

      Macroscope's review found this PR approvable — This is a focused Codex protocol performance fix that makes fragmented JSONL buffering linear while retaining existing framing and termination behavior. The production logic is small and isolated, with targeted tests covering large payloads, UTF-8/CRLF boundaries, and malformed final input.

      Notes:

      • No code objects were reviewed. Approvability was decided on eligibility alone.
      You can add or adjust custom eligibility rules. [Learn more](https://docs.macroscope.com/approvability#example-custom-eligibility-rules).
    5. juliusmarminge merged this702a6ad

    6. juliusmarminge closed this

Sign in to join this conversation.