Repository navigation
oom(01/29): bound shared file/stream/buffer readers - #10294
Conversation
|
Note Reviews pausedIt looks like this branch is under active development. To avoid overwhelming you with review comments due to an influx of new commits, CodeRabbit has automatically paused this review. You can configure this behavior by changing the Use the following commands to manage reviews:
Use the checkboxes below for quick actions:
No actionable comments were generated in the recent review. 🎉 ℹ️ Recent review info⚙️ Run configurationConfiguration used: Repository UI Review profile: CHILL Plan: Pro Plus Run ID: 📥 CommitsReviewing files that changed from the base of the PR and between 8eef7fb114110acd78c5328cf60cf6b8b2253e00 and f072935. 📒 Files selected for processing (18)
🚧 Files skipped from review as they are similar to previous changes (17)
📝 WalkthroughWalkthroughThe change adds shared utilities for enforcing byte and JSON-structure limits across persisted state, secure JSON files, fetch responses, and async readable input. It introduces resizable byte buffering, bounded subprocess line accumulation, string chunk compaction, file-content comparison helpers, and NUL-delimited field iteration. Persisted-state functionality includes bounded parsing and serialization, secret capacity checks, replacement sizing, surrogate-safe hashing, and atomic backup restoration. Comprehensive Vitest coverage exercises boundary, streaming, sparse-file, cleanup, and UTF-8 behaviors. 🚥 Pre-merge checks | ✅ 4 | ❌ 1❌ Failed checks (1 warning)
✅ Passed checks (4 passed)
✨ Finishing Touches📝 Generate docstrings
Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out. Comment |
There was a problem hiding this comment.
Actionable comments posted: 3
🧹 Nitpick comments (1)
src/shared/search-subprocess-lines.ts (1)
5-6: 📐 Maintainability & Code Quality | 🔵 Trivial | ⚡ Quick winDuplicate buffer-growth logic — consider reusing
GrowingByteBuffer.The private
buffer/append/takeLinetrio reimplements the same doubling-growth accumulator already implemented and tested inGrowingByteBuffer(src/shared/growing-byte-buffer.ts). Sincepush()already enforces themaxLineBytescap before callingappend(Line 21), the internalMath.min(this.maxLineBytes, ...)clamp is redundant, and aGrowingByteBufferinstance (byteLength,append,takeString,clear) could replace this logic entirely.Note:
search-subprocess-lines.test.tsreflects into the privatebufferfield expecting a rawBuffer | null(Lines 47, 49), so adopting this would also require updating those assertions.♻️ Sketch of the reuse
+import { GrowingByteBuffer } from './growing-byte-buffer' + export class SearchSubprocessLineAccumulator { - private buffer: Buffer | null = null - private bytes = 0 + private readonly buffer = new GrowingByteBuffer() ... push(rawChunk: Buffer | string, onLine: (line: string) => void): boolean { ... - if (this.bytes + segmentBytes > this.maxLineBytes) { + if (this.buffer.byteLength + segmentBytes > this.maxLineBytes) { this.clear() return false } - if (newline !== -1 && this.bytes === 0) { + if (newline !== -1 && this.buffer.byteLength === 0) {Also applies to: 54-74
ℹ️ Review info
⚙️ Run configuration
Configuration used: Repository UI
Review profile: CHILL
Plan: Pro Plus
Run ID: 99b56c57-b508-4135-8cbc-b7a05f131361
📒 Files selected for processing (18)
src/shared/bounded-secure-json-file.tssrc/shared/fetch-response-body.tssrc/shared/growing-byte-buffer.test.tssrc/shared/growing-byte-buffer.tssrc/shared/node-file-content-equality.test.tssrc/shared/node-file-content-equality.tssrc/shared/node-readable-text.test.tssrc/shared/node-readable-text.tssrc/shared/node-source-copy-content-equality.test.tssrc/shared/node-source-copy-content-equality.tssrc/shared/nul-delimited-fields.test.tssrc/shared/nul-delimited-fields.tssrc/shared/persisted-state-file-bounds.test.tssrc/shared/persisted-state-file-bounds.tssrc/shared/search-subprocess-lines.test.tssrc/shared/search-subprocess-lines.tssrc/shared/string-chunk-compaction.test.tssrc/shared/string-chunk-compaction.ts
| export async function readFetchResponseTextWithinLimit( | ||
| response: Response, | ||
| maxBytes = API_RESPONSE_MAX_BYTES | ||
| ): Promise<string> { | ||
| if (isHighLevelOnlyResponse(response)) { | ||
| return response.text() | ||
| } | ||
| return new TextDecoder().decode(await readFetchResponseBytesWithinLimit(response, maxBytes)) | ||
| } | ||
|
|
||
| export async function readFetchResponseJsonWithinLimit<T>( | ||
| response: Response, | ||
| maxBytes = API_RESPONSE_MAX_BYTES, | ||
| structureLimits: JsonTextStructureLimits = API_RESPONSE_JSON_LIMITS | ||
| ): Promise<T> { | ||
| if (isHighLevelOnlyResponse(response)) { | ||
| return response.json() as Promise<T> | ||
| } | ||
| const content = await readFetchResponseTextWithinLimit(response, maxBytes) | ||
| assertJsonTextStructureWithinLimits(content, structureLimits) | ||
| return JSON.parse(content) as T |
There was a problem hiding this comment.
🩺 Stability & Availability | 🟠 Major | 🏗️ Heavy lift
Do not bypass limits for high-level-only responses.
response.text() is unbounded, and response.json() also skips assertJsonTextStructureWithinLimits. An adapter without headers and body can therefore bypass the API’s byte, depth, and token limits. Require a bounded body-reader contract from adapters, or reject this response shape instead of falling back.
| export function nodeSourceAndCopyContentsEqualSync(sourcePath: string, copyPath: string): boolean { | ||
| try { | ||
| // Why: source links are intentional, but an owned copy must remain a regular file. | ||
| if (!statSync(sourcePath).isFile() || !lstatSync(copyPath).isFile()) { | ||
| return false | ||
| } | ||
| } catch { | ||
| return false | ||
| } | ||
|
|
||
| let sourceDescriptor: number | null = null | ||
| let copyDescriptor: number | null = null | ||
| let matches = false | ||
| let failed = false | ||
| try { | ||
| sourceDescriptor = openSync(sourcePath, 'r') | ||
| copyDescriptor = openSync(copyPath, 'r') |
There was a problem hiding this comment.
🔒 Security & Privacy | 🟠 Major | ⚡ Quick win
Bind regular-file validation to the opened descriptor.
copyPath can be swapped for a symlink after Line 20 and before Line 33; openSync follows it, so this may compare an unintended target and return true. Capture the initial lstatSync identity and verify it against fstatSync(copyDescriptor) before comparing bytes, with a race regression test.
1be8938 to
8eef7fb
Compare
Files: 18 applied, 0 deleted (from 6eb70d8) Co-authored-by: Orca <help@stably.ai>
8eef7fb to
f072935
Compare
…yai#10294) Files: 18 applied, 0 deleted (from 925d4a7) Co-authored-by: Orca <help@stably.ai>
OOM hardening slice 01/29 — bound shared file/stream/buffer readers
Summary
Adds 10 new additive src/shared bounded-reader modules (+8 test files) for file/stream/buffer/text/JSON: growing-byte-buffer, node-readable-text, fetch-response-body, string-chunk-compaction, node-file-content-equality, node-source-copy-content-equality, search-subprocess-lines, nul-delimited-fields, bounded-secure-json-file, persisted-state-file-bounds. No existing file changed; no in-slice consumers. Imported deps (node-bounded-json-stringify, secure-file, json-text-structure-limit, node-bounded-file-reader) exist at BASE with the exact exports used, so the slice compiles standalone. Verified: node typecheck clean, 29/29 vitest pass, oxlint clean.
ELI5
These are new tape-measure tools that read files/streams/network bodies a little at a time and stop once the data gets too big, instead of swallowing an entire giant blob into memory. This slice just adds the tools to the toolbox and tests them; nothing in the app uses them yet, so nothing behaves differently for users.
Proof of OOM fix
Regressions
Normal-path (ordinary use, below the new limits): none — behavior is unchanged below the ceilings.
Overload-only (extreme input / sustained backpressure) — intended, documented degradations:
Build / CI
Adversarial review
Files
18 files changed (+1166 / −0).
🤖 Decomposed, reviewed & assembled by Claude Code from the reverted #10179. Draft — do not merge out of order.
Made with Orca 🐋