Repository navigation
fix(streaming): make fragmented line decoding amortized linear - #2336
Conversation
jbeckwith-oai
left a comment
There was a problem hiding this comment.
[Medium] Bound the decoder buffer retained after a large completed line.
At src/internal/decoders/line.ts:94-99, consuming the active range resets only the cursors; the geometric backing buffer remains at the remotely influenced high-water capacity for the rest of the long-lived stream. Capacity can approach twice the anomalous line size. The previous implementation replaced that allocation on the next nonempty append, so this is a material retention regression across concurrent streams.
Please replace oversized empty buffers with a baseline or documented modest retention threshold (for example 64 KiB) once #start === #end, preserving geometric reuse for normal lines. Add a large-line → newline → small-line regression that proves the high-water allocation is released or bounded.
The amortized-linear decoding logic, boundary semantics, tests, and exact-head CI otherwise look correct.
There was a problem hiding this comment.
💡 Codex Review
Here are some automated review suggestions for this pull request.
Reviewed commit: 644bd82739
ℹ️ About Codex in GitHub
Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you
- Open a pull request for review
- Mark a draft as ready
- Comment "@codex review".
If Codex has suggestions, it will comment; otherwise it will react with 👍.
Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".
There was a problem hiding this comment.
Exact-head re-review at 644bd82.
The new 64 KiB reset only runs when every buffered byte has been consumed. A chunk containing a very large completed line followed by one unfinished byte still retains the entire high-water allocation for the remaining stream lifetime: #start < #end skips the reset, and later compaction uses copyWithin on the same oversized array.
Please compact a small active remainder into a fresh bounded buffer when capacity exceeds the retention limit, normalize the cursors, and add a same-chunk large-line + newline + one-byte-tail regression. The amortized-linear decoder behavior, protocol handling, test structure, and exact-head CI otherwise look good.
There was a problem hiding this comment.
💡 Codex Review
Here are some automated review suggestions for this pull request.
Reviewed commit: c346e21321
ℹ️ About Codex in GitHub
Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you
- Open a pull request for review
- Mark a draft as ready
- Comment "@codex review".
If Codex has suggestions, it will comment; otherwise it will react with 👍.
Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".
jbeckwith-oai
left a comment
There was a problem hiding this comment.
Exact-head re-review at c346e21.
The prior tiny-tail case is fixed, but the retention policy still has a one-byte cliff: any live suffix above 64 KiB disables shrinking entirely. A huge completed line followed by a 65,537-byte unterminated suffix can therefore retain the arbitrarily large high-water allocation while the stream idles; the current over-threshold test actually permits a 65,553-byte suffix to keep a 524,288-byte buffer.
Please shrink whenever capacity is disproportionately larger than the live suffix, using a bounded/geometric ratio such as capacity > max(64 KiB, liveLength * 2), and update the >64 KiB regression to assert that ratio while preserving bytes/UTF-8. Decoder correctness, performance, thermo, and exact CI otherwise look good.
There was a problem hiding this comment.
💡 Codex Review
Here are some automated review suggestions for this pull request.
Reviewed commit: f32c8936d5
ℹ️ About Codex in GitHub
Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you
- Open a pull request for review
- Mark a draft as ready
- Comment "@codex review".
If Codex has suggestions, it will comment; otherwise it will react with 👍.
Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".
jbeckwith-oai
left a comment
There was a problem hiding this comment.
Re-reviewed exact head f32c893. The prior retention blocker is resolved: completed-line suffix storage is now bounded for empty, small, and >64 KiB live suffixes without reintroducing full rescans or quadratic copying. Cursor rebasing, CR/LF and split-UTF-8 handling, flush behavior, and repeated-cycle performance remain sound; focused regressions cover the former threshold cliff. Strict thermo is clean and all exact-head CI is green. No remaining findings.
jbeckwith-oai
left a comment
There was a problem hiding this comment.
Re-reviewed exact head 0b70bb5. The latest delta is a test-only consolidation that preserves the important capacity boundaries, reuse, scan/copy linearity, compaction, UTF-8/CRLF, and flush regressions while substantially reducing duplication. Production remains amortized-linear with bounded retained capacity across every suffix size; public streaming behavior remains covered by adjacent suites. Strict thermo is improved, no suppressions or unsuitable doubles were added, and all exact-head CI is green. No findings.
HAYDEN-OAI
left a comment
There was a problem hiding this comment.
Reviewed exact head 0b70bb5. The LineDecoder cursor/compaction design is amortized linear, releases oversized historical storage for both small and large suffixes, and preserves CR/LF, UTF-8, and flush semantics. Focused coverage and exact-head CI are green; no findings.
Summary
Why
LineDecoder.decode()previously concatenated its entire unfinished line with every incoming chunk and then searched that entire line again from byte zero.Stream.fromReadableStream()passes each raw NDJSON fragment directly to this decoder, so adversarial or naturally fragmented transports could turn a 32 KiB response line into more than 512 MiB of byte copying plus quadratic scanning and CPU consumption.The earlier SSE event-framing fix in #2324 changed
src/core/streaming.ts; it did not address this independent handwritten line decoder. This change touches only the handwritten decoder and its handwritten unit tests. It introduces no maximum line size and does not change the public API.Regression-first verification
Before the implementation changed, the new focused suite failed independently in both dimensions:
The first test feeds 16,384 one-byte fragments and instruments
Uint8Array.prototype.set. The second independently tracks indexed typed-array reads and fails as soon as scanning exceeds a linear bound.Measured before / after
Measurements use the real SDK implementation, one-byte fragments, and the same Node.js v26.7.0 remote devbox. Copied-byte totals were measured with
Uint8Array.prototype.set; timings are illustrative wall-clock measurements from the same instrumented runs.Stream.fromReadableStreambeforeAt 32 KiB this is approximately 8,224× fewer copied bytes, 68× faster direct decoding, and 14× faster public NDJSON streaming.
Independent indexed-byte instrumentation also verifies the scan improvement separately from copying:
The fixed decoder performs exactly two indexed comparisons per unfinished input byte instead of repeatedly revisiting the entire accumulated line.
Validation
./scripts/test tests/internal/decoders/line.test.ts tests/lib/streaming-core.test.ts— 74 passedPATH="/tmp/openai-node-line-decoder-bin-mst5e1tw:$PATH" ./node_modules/.bin/vitest run --config vitest.config.mts --update=none— 2,094 passed across 70 files./scripts/lint./node_modules/.bin/tsc --noEmit./scripts/build./node_modules/typescript-4-9/bin/tsc --project dist/src/tsconfig.json --noEmit./node_modules/.bin/tsc --project dist/src/tsconfig.json --noEmitThe temporary
PATHentry supplies the repository's existing pnpm command in the sandbox; no package-manager, lockfile, generated-file, or public-API changes are included.