Skip to content

oom(foundation): bound shared readers/limits + add BoundedMap primitive - #10299

Merged
nwparker merged 6 commits into
mainfrom
oom/06-b-shared-wiring
Jul 25, 2026
Merged

nwparker merged 6 commits into
mainfrom
oom/06-b-shared-wiring

Conversation

@nwparker

@nwparker nwparker commented Jul 24, 2026 •

Copy link
Copy Markdown
Contributor

Shared bounded-memory foundation (slices 03–06 of the #10179 re-land)

Re-lands the src/shared layer of #10179 ("bound OOM-prone accumulators", reverted in #10255 for size) as one atomic PR, plus a new reusable primitive.

Why one PR: these modules are mutually coupled — the new bounded readers depend on types/budgets that the wiring commit modifies, whose own tests depend on the readers. No slice ordering makes them individually CI-green, so splitting them further would only produce PRs that cannot pass CI. Slices 01–02 (pure additive primitives) already merged separately as #10294 / #10295.

What's in it

  • Bounded readers & limits for filesystem/quick-open/markdown listings, remote-runtime & relay admission, image/media/PDF ceilings, secure-path, MCP, hermes, setup-script, CSV/YAML/JSON structure limits.
  • Wiring of existing shared modules onto those bounded readers.
  • BoundedMap — a new count- and byte-bounded LRU primitive (src/shared/bounded-map.ts), replacing hand-rolled "Map + counter + byte ledger + evict-oldest" copies. Three call sites migrated onto it (secure-path-hardening-cache, github/conflict-summary-cache, runtime/orca-runtime); both duplicate setBoundedMapEntry helpers are deleted.

BoundedMap contract (documented in-file, each clause tested)

set() never reports success for an entry it did not retain · weights must be non-negative safe integers · an unmeasurable or throwing sizeOf fails closed (entry rejected, ledger intact — never crashes a relay on someone else's machine) · maxEntryBytes is clamped to maxBytes · keys()/values()/entries() return snapshots so a touching read during traversal terminates (peek() reads without reordering) · onEvict fires only for capacity eviction, only after both bounds are restored, and one throwing hook cannot skip the others.

Verification

  • Full suite green in CI.
  • 26 BoundedMap unit tests (exact-limit + limit+1, LRU order, NaN-key protection, safe-integer boundary, non-destructive rejection, reentrant/throwing disposal).
  • 72,000-op randomized invariant fuzz across 9 configs (incl. the real production cache shape) asserting after every op that the ledger equals the shadow sum and both ceilings hold.
  • Two decomposition bugs found and fixed by running the full suite at this checkpoint: git/status.test.ts and ImageViewer.test.tsx had been split from the sources they cover.

Review

Adversarially reviewed over three rounds (fable-xhigh + gpt-5.6-sol-xhigh, every claim verified by reproduction). Round 1 found 3 real defects, round 2 found 4 more, round 3 returned zero adopter-reachable findings — all fixed here.

Two further primitives (AdmissionController, BoundedQueue) were deliberately dropped: they had no callers and review showed each was mis-shaped for the site it named (withAdmission is lexical-only and would have released the watcher's cross-IPC claim too early; BoundedQueue's shift() storage hits a V8 cliff at ~16k, right at the 16,384 PTY span cap). They return when a real adopter shapes them.

OSS alternatives evaluated and rejected: installed lru-cache@11 requires node ≥20 while the relay bundles at node18; it throws on a zero sizeCalculation (count-only is our most common mode), silently refuses undefined values, and — verified by probe — produces a negative calculatedSize under a config it accepts.


🤖 Decomposed, reviewed & assembled by Claude Code from the reverted #10179.

@nwparker
nwparker force-pushed the oom/05-a5-shared-misc branch from 64ee51d to f50920e Compare July 24, 2026 07:48
@nwparker
nwparker force-pushed the oom/06-b-shared-wiring branch from 68c79b6 to 3cad51f Compare July 24, 2026 07:48
@nwparker
nwparker force-pushed the oom/05-a5-shared-misc branch from f50920e to 21bea06 Compare July 25, 2026 02:21
@nwparker
nwparker force-pushed the oom/06-b-shared-wiring branch from 3cad51f to 18835cd Compare July 25, 2026 02:21
@nwparker
nwparker force-pushed the oom/05-a5-shared-misc branch from 21bea06 to 1085da2 Compare July 25, 2026 02:39
@nwparker
nwparker force-pushed the oom/06-b-shared-wiring branch from 18835cd to 7330bb7 Compare July 25, 2026 02:39
@nwparker
nwparker force-pushed the oom/05-a5-shared-misc branch from 1085da2 to 18ea52a Compare July 25, 2026 03:10
@nwparker
nwparker force-pushed the oom/06-b-shared-wiring branch from 7330bb7 to 02ff993 Compare July 25, 2026 03:10
nwparker and others added 6 commits July 24, 2026 20:33
Files: 18 applied, 0 deleted (from 6eb70d8)

Co-authored-by: Orca <[email protected]>
Files: 7 applied, 0 deleted (from 6eb70d8)

Co-authored-by: Orca <[email protected]>
Files: 21 applied, 0 deleted (from 6eb70d8)

Co-authored-by: Orca <[email protected]>
Files: 8 applied, 0 deleted (from 6eb70d8)

Co-authored-by: Orca <[email protected]>
Files: 28 applied, 0 deleted (from 6eb70d8)

Co-authored-by: Orca <[email protected]>
Files: 81 applied, 0 deleted (from 6eb70d8)

Co-authored-by: Orca <[email protected]>
@nwparker
nwparker force-pushed the oom/05-a5-shared-misc branch from 18ea52a to e73e980 Compare July 25, 2026 03:34
@nwparker
nwparker force-pushed the oom/06-b-shared-wiring branch from 02ff993 to b4009ef Compare July 25, 2026 03:34
@nwparker
nwparker changed the base branch from oom/05-a5-shared-misc to main July 25, 2026 04:35
@nwparker nwparker changed the title oom(06/29): wire shared modules to bounded readers & limits oom(foundation): bound shared readers/limits + add BoundedMap primitive Jul 25, 2026
@nwparker
nwparker merged commit 879aad7 into main Jul 25, 2026
1 check passed
brennanb2025 added a commit that referenced this pull request Jul 25, 2026
…800MiB

The filler colored per line, not per cell as its comment claimed, so the
serialized seed only tracked plain-text size and needed 26k buffer rows to
clear 16MiB. The xterm buffer costs rows x cols, which made this single file
raise the whole src/main/daemon/ suite's peak RSS 5.2x (241MiB -> 1244MiB) —
a real OOM risk on CI right after #10299 bounded readers for that reason.

Carrying the bytes in SGR runs instead of rows reaches a larger seed from
5.5k rows: suite peak RSS 1244MiB -> 793MiB, and the margins improve too
(seed 25.16MiB = 1.57x the threshold vs 1.19x before, checkpoint.json
46.1MiB = 2.88x vs 1.20x). Also makes the fixture match its own comment and
be more representative of real colored agent output.

Re-verified all three mutations still fail: cap at 16MiB -> "expected null
not to be null"; endedAt gate deleted -> test 2 fails; writeSync
unavailable -> both fail.
brennanb2025 added a commit that referenced this pull request Jul 25, 2026
#10500)

* test(terminal): pin checkpoint-only cold restore of a large checkpoint

Triaging a report that checkpoint-only cold restore renders blank at every
checkpoint size found no v1.4.156 regression: an A/B of v1.4.155 against
origin/main returned byte-identical ColdRestoreInfo at 1.01, 5.56, 11.30 and
20.61 MiB, all 300 marker lines intact on both refs. Blankness tracks the
meta.endedAt eligibility gate, not size — a cleanly ended session refuses to
cold-restore at any size, which is by design and unchanged between the refs.

What the triage did surface is a coverage gap. #10179's 16MiB checkpoint read
cap sat under what the unbounded writer emits, so a large checkpoint threw,
was swallowed to checkpoint=null, and the pane reopened empty; #10479 raised
the cap but nothing pinned the round trip it had broken. history-reader-memory
covers the bounded reader at its limit, not writer→reader recovery.

Adds that round trip through the real HistoryManager writer and HistoryReader
over a header-only log, and pins the endedAt gate that has now been mistaken
for a size regression twice. Fails at the pre-#10479 cap with the exact
production symptom (detectColdRestore returns null).

* test(terminal): fail loudly when writeSync is unavailable

writeLargeScrollback ignored writeSync's boolean, which is false when
xterm's private _core.writeSync goes away. The size assertions catch that
in the large-checkpoint test (416 bytes vs 16MiB), but the endedAt gate is
size-independent, so that test silently passed on an empty snapshot —
pinning the gate over no scrollback at all.

* test(terminal): cut large-checkpoint fixture peak RSS from 1.2GiB to 800MiB

The filler colored per line, not per cell as its comment claimed, so the
serialized seed only tracked plain-text size and needed 26k buffer rows to
clear 16MiB. The xterm buffer costs rows x cols, which made this single file
raise the whole src/main/daemon/ suite's peak RSS 5.2x (241MiB -> 1244MiB) —
a real OOM risk on CI right after #10299 bounded readers for that reason.

Carrying the bytes in SGR runs instead of rows reaches a larger seed from
5.5k rows: suite peak RSS 1244MiB -> 793MiB, and the margins improve too
(seed 25.16MiB = 1.57x the threshold vs 1.19x before, checkpoint.json
46.1MiB = 2.88x vs 1.20x). Also makes the fixture match its own comment and
be more representative of real colored agent output.

Re-verified all three mutations still fail: cap at 16MiB -> "expected null
not to be null"; endedAt gate deleted -> test 2 fails; writeSync
unavailable -> both fail.

* test(terminal): tighten timeout, reuse prod dir-name helper, dispose first

Review follow-ups, all test-only:
- Import getHistorySessionDirName instead of hand-rolling encodeURIComponent.
  Equivalent today, but that helper exists to absorb encoding changes, so the
  hand-rolled copy would silently diverge from the writer it is checking.
- Index FILLER_ROWS by its own length so growing the array cannot leave rows
  unused.
- 300_000ms -> 60_000ms. The file runs in ~2.8s and vitest's own default is
  30s; a 5-minute ceiling turns a hung regression into a stalled job rather
  than a failure. 60s keeps Windows headroom.
- Take the snapshot, dispose, then checkpoint, so a throwing checkpoint()
  cannot leak the buffer. Note this is memory-neutral, not a saving: peak RSS
  is set at getSnapshot(), where the buffer and the serialized strings
  coexist, and measured ~800MiB either way.

Mutations re-verified: cap at 16MiB -> "expected null not to be null";
endedAt gate deleted -> test 2 fails; writeSync unavailable -> both fail.
enrick-dev pushed a commit to enrick-dev/orca that referenced this pull request Oct 15, 2026
…ve (stablyai#10299)

* oom(01): A1-shared-readers — reintroduce stablyai#10179 subset

Files: 18 applied, 0 deleted (from 925d4a7)

Co-authored-by: Orca <[email protected]>

* oom(02): A2-shared-image-media — reintroduce stablyai#10179 subset

Files: 7 applied, 0 deleted (from 925d4a7)

Co-authored-by: Orca <[email protected]>

* oom(03): A3-shared-fs-listing — reintroduce stablyai#10179 subset

Files: 21 applied, 0 deleted (from 925d4a7)

Co-authored-by: Orca <[email protected]>

* oom(04): A4-shared-remote-relay — reintroduce stablyai#10179 subset

Files: 8 applied, 0 deleted (from 925d4a7)

Co-authored-by: Orca <[email protected]>

* oom(05): A5-shared-misc — reintroduce stablyai#10179 subset

Files: 28 applied, 0 deleted (from 925d4a7)

Co-authored-by: Orca <[email protected]>

* oom(06): B-shared-wiring — reintroduce stablyai#10179 subset

Files: 81 applied, 0 deleted (from 925d4a7)

Co-authored-by: Orca <[email protected]>

---------

Co-authored-by: Orca <[email protected]>
enrick-dev pushed a commit to enrick-dev/orca that referenced this pull request Oct 15, 2026
stablyai#10500)

* test(terminal): pin checkpoint-only cold restore of a large checkpoint

Triaging a report that checkpoint-only cold restore renders blank at every
checkpoint size found no v1.4.156 regression: an A/B of v1.4.155 against
origin/main returned byte-identical ColdRestoreInfo at 1.01, 5.56, 11.30 and
20.61 MiB, all 300 marker lines intact on both refs. Blankness tracks the
meta.endedAt eligibility gate, not size — a cleanly ended session refuses to
cold-restore at any size, which is by design and unchanged between the refs.

What the triage did surface is a coverage gap. stablyai#10179's 16MiB checkpoint read
cap sat under what the unbounded writer emits, so a large checkpoint threw,
was swallowed to checkpoint=null, and the pane reopened empty; stablyai#10479 raised
the cap but nothing pinned the round trip it had broken. history-reader-memory
covers the bounded reader at its limit, not writer→reader recovery.

Adds that round trip through the real HistoryManager writer and HistoryReader
over a header-only log, and pins the endedAt gate that has now been mistaken
for a size regression twice. Fails at the pre-stablyai#10479 cap with the exact
production symptom (detectColdRestore returns null).

* test(terminal): fail loudly when writeSync is unavailable

writeLargeScrollback ignored writeSync's boolean, which is false when
xterm's private _core.writeSync goes away. The size assertions catch that
in the large-checkpoint test (416 bytes vs 16MiB), but the endedAt gate is
size-independent, so that test silently passed on an empty snapshot —
pinning the gate over no scrollback at all.

* test(terminal): cut large-checkpoint fixture peak RSS from 1.2GiB to 800MiB

The filler colored per line, not per cell as its comment claimed, so the
serialized seed only tracked plain-text size and needed 26k buffer rows to
clear 16MiB. The xterm buffer costs rows x cols, which made this single file
raise the whole src/main/daemon/ suite's peak RSS 5.2x (241MiB -> 1244MiB) —
a real OOM risk on CI right after stablyai#10299 bounded readers for that reason.

Carrying the bytes in SGR runs instead of rows reaches a larger seed from
5.5k rows: suite peak RSS 1244MiB -> 793MiB, and the margins improve too
(seed 25.16MiB = 1.57x the threshold vs 1.19x before, checkpoint.json
46.1MiB = 2.88x vs 1.20x). Also makes the fixture match its own comment and
be more representative of real colored agent output.

Re-verified all three mutations still fail: cap at 16MiB -> "expected null
not to be null"; endedAt gate deleted -> test 2 fails; writeSync
unavailable -> both fail.

* test(terminal): tighten timeout, reuse prod dir-name helper, dispose first

Review follow-ups, all test-only:
- Import getHistorySessionDirName instead of hand-rolling encodeURIComponent.
  Equivalent today, but that helper exists to absorb encoding changes, so the
  hand-rolled copy would silently diverge from the writer it is checking.
- Index FILLER_ROWS by its own length so growing the array cannot leave rows
  unused.
- 300_000ms -> 60_000ms. The file runs in ~2.8s and vitest's own default is
  30s; a 5-minute ceiling turns a hung regression into a stalled job rather
  than a failure. 60s keeps Windows headroom.
- Take the snapshot, dispose, then checkpoint, so a throwing checkpoint()
  cannot leak the buffer. Note this is memory-neutral, not a saving: peak RSS
  is set at getSnapshot(), where the buffer and the serialized strings
  coexist, and measured ~800MiB either way.

Mutations re-verified: cap at 16MiB -> "expected null not to be null";
endedAt gate deleted -> test 2 fails; writeSync unavailable -> both fail.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant