fix(codex): avoid quadratic app-server input buffering #8605
main fromrefs/pull/8605/head·merged by juliusmarminge View on GitHubCloses #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:
handleLineis now called once more at termination with a possibly empty string; any handler that assumes it only receives non-empty content may behave differently.
- 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:M
Thread transfer impact#
✅ Thread transfer remains within every enforced ceiling.
Provider Metric Main baseline This PR Impact PR ceiling Codex Total thread wire 13.3 KiB 13.3 KiB +18 B (+0.1%) 15.1 KiB ✅ Codex Thread snapshot wire 6.9 KiB 6.9 KiB +1 B (+0.0%) 7.3 KiB ✅ Codex Live turn WebSocket wire 6.4 KiB 6.5 KiB +17 B (+0.3%) 7.8 KiB ✅ Codex Live turn WebSocket decoded 55.6 KiB 55.6 KiB 0 B (0.0%) 66.4 KiB ✅ Codex Live turn messages 11 11 0 (0.0%) 21 ✅ Claude Total thread wire 13.4 KiB 13.4 KiB +5 B (+0.0%) 15.1 KiB ✅ Claude Thread snapshot wire 6.9 KiB 6.9 KiB +2 B (+0.0%) 7.3 KiB ✅ Claude Live turn WebSocket wire 6.5 KiB 6.5 KiB +3 B (+0.0%) 7.8 KiB ✅ Claude Live turn WebSocket decoded 56.4 KiB 56.4 KiB 0 B (0.0%) 66.4 KiB ✅ Claude Live turn messages 11 11 0 (0.0%) 21 ✅ Baseline:
2bc9e8e· PR result:d310c20· Source CI: successScenario 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.
macroscopeapp[bot](GitHub profile, opens in a new tab) approved these changes
Approvability#
Verdict: Approved at
d310c20Macroscope'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.
juliusmarminge merged this702a6ad
juliusmarminge closed this
Sign in to join this conversation.
Important
Review skipped#
Auto reviews are disabled on this repository. Please check the settings in the CodeRabbit UI or the
.coderabbit.yamlfile in this repository. To trigger a single review, invoke the@coderabbitai reviewcommand.⚙️ Run configuration
Configuration used: Repository UI
Review profile: CHILL
Plan: Pro Plus
Run ID:
c1b524ff-de67-4067-b7cb-e10b9b568dd7You can disable this status message by setting the
reviews.review_statustofalsein the CodeRabbit configuration file.Use the checkbox below for a quick retry:
Comment `@coderabbitai help` to get the list of available commands.