Skip to content

fix(claude): enforce history window quota while reading - #21021

Merged
nwparker merged 3 commits into
mainfrom
np-oom-scan-claude-history-window-budget
Sep 20, 2026
Merged

nwparker merged 3 commits into
mainfrom
np-oom-scan-claude-history-window-budget

Conversation

@OrcaWin

@OrcaWin OrcaWin commented Sep 16, 2026 •

Copy link
Copy Markdown
Collaborator
Files Added Deleted Net
Test 2 $\color{#1a7f37}{\Huge{\mathbf{+}}}$​188 $\color{#cf222e}{\Huge{\mathbf{−}}}$​1 $\color{#1a7f37}{\Huge{\mathbf{+}}}$​187
Prod 8 $\color{#1a7f37}{\Huge{\mathbf{+}}}$​386 $\color{#cf222e}{\Huge{\mathbf{−}}}$​6 $\color{#1a7f37}{\Huge{\mathbf{+}}}$​380

ELI5

After Claude restarts, Orca reads history to check whether a message was delivered. That read already has a 16 MiB limit, but a growing file could slip past it. This change enforces the limit during the read too.

What Changed

Reuse the existing bounded reader instead of checking the file size and then reading without a bound. An oversized or failed read leaves delivery unconfirmed rather than treating an incomplete prefix as proof that a message was absent.

Why

The initial size check cannot protect against later growth. Prefix-only history could produce a false delivery decision.

Tradeoff

A history file that crosses the quota can leave delivery unresolved; Orca does not automatically resend or discard the message. A zero-loss replacement requires disk-backed canonical fingerprints and reconciliation, not just a smaller read buffer.

Linked Issue

Related to #19768 and #19831 as memory hardening; this reader did not exist in v1.4.198.

Visual Proof

No before/after UI capture is attached; this is a lifecycle behavior change. Automated behavior evidence is linked under Testing.

Testing

A real-file proof grew 305 bytes to 17 MiB after the size check; the fixed reader stopped at 16 MiB plus one byte. Controls cover exact limits, permitted growth, read failures, missing anchors, and descriptor cleanup. Audit evidence is in docs/audits/claude-history-window-budget/.

Review

The pre-existing unused test import that failed typecheck and lint has been removed. The renderer typecheck and focused lint pass; fresh CI is pending. The audit now declares its patching dependency directly (diff 8.0.4), and rerunning it reproduces the checked-in results exactly: six baseline controls pass with one expected failure; all seven fixed cases pass. Production source change: 11 lines changed (6 added, 5 deleted), excluding tests, audit documents, and development dependencies.

The unresolved-delivery behavior is unchanged: crossing the existing quota leaves reconciliation unresolved. This is a small preventive quota fix, without measured incident-scale OOM savings.

@pullfrog pullfrog Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ℹ️ No critical issues — one optional nitpick below.

Reviewed changes

  • Bounded history read — readClaudeProviderHistoryWindow now reads the Claude project JSONL through readNodeFileWithinLimit(..., MAX_HISTORY_WINDOW_SOURCE_BYTES) instead of a stat size check followed by an unbounded readFile, closing the stat/read race where post-check growth was fully allocated and accepted. Overflow throws NodeFileReadTooLargeError, which the existing catch maps to the same INCONSISTENT result. No new size policy; the constant is unchanged.
  • Source-budget tests — a new seven-case suite mocks node:fs/promises to inject an append immediately after the size snapshot and pins accepted bytes, overflow rejection, permitted in-quota growth, read-error and missing-anchor behavior, plus descriptor open/close parity.
  • Audit harness — docs/audits/claude-history-window-budget/ adds a reproducible before/after ablation (reproduce.mjs + fix.patch + results.json) recording read bytes, size snapshots, descriptor counts and source hashes.

I verified the focused suite passes (7/7), typecheck:node and check:code-quality:changed pass, and the committed results.json hashes match the working tree (after/current sha256, and before equals applyPatch(current, reversePatch(fix.patch))). An on-disk ablation of the fix failed exactly the one overflow test and passed the other six, matching the recorded before/after split — the test is not theatre.

ℹ️ Nitpicks

  • docs/audits/** has no .gitignore allow-list entry, so these three new audit files are present only because they were force-added. The docs/** rule at .gitignore:96 still matches them, so a clean git add after any later edit to docs/audits/claude-history-window-budget/* silently skips it. Add the documented pair for a new docs/<dir>/: !docs/audits/ and !docs/audits/**.

Pullfrog  | Fix it ➔ | View workflow run | Using DeepSeek Flash (free via Pullfrog for OSS) | 𝕏

@coderabbitai

coderabbitai Bot commented Sep 16, 2026 •

Copy link
Copy Markdown
Contributor

Review Change StackReview Change Stack

No actionable comments were generated in the recent review. 🎉

ℹ️ Recent review info
⚙️ Run configuration

Configuration used: Repository UI

Review profile: CHILL

Plan: Advanced

Run ID: b7c98c69-f887-41d5-9d58-e9b48004a791

📥 Commits

Reviewing files that changed from the base of the PR and between f59ada2 and b1fa4ce.

📒 Files selected for processing (2)
  • src/main/claude/claude-structured-history-window.ts
  • src/main/native-chat/agent-session-journal/journal-restart-reconciliation.ts

Included review availability: Your plan provides up to 10 included reviews per hour; 4 remain after this review.


📝 Walkthrough

Walkthrough

The Claude history reader now uses readNodeFileWithinLimit with the existing 16 MiB source limit. It decodes the bounded buffer as UTF-8 and retains the existing inconsistent-history result for read failures and oversized files. New tests cover stable, exact-limit, oversized, growing, failing, and anchor-missing sources. The audit harness compares baseline and fixed behavior and records filesystem observations and test results. The journal reconciliation loop now logs unknown outcomes.

Priority: ⬇️ Low

🚥 Pre-merge checks | ✅ 4 | ❌ 1

❌ Failed checks (1 warning)

Check name Status Explanation Resolution
Docstring Coverage ⚠️ Warning Docstring coverage is 33.33% which is insufficient. The required threshold is 80.00%. Docstring coverage is scoped to functions touched by this diff. Analyzed 3 functions across 4 files. Write docstrings for the functions missing them to satisfy the coverage threshold.
✅ Passed checks (4 passed)
Check name Status Explanation
Title check ✅ Passed The title clearly and concisely describes the main change: enforcing the Claude history-window quota during reads.
Description check ✅ Passed The description covers the main required sections, explains the problem and solution, identifies related issues, documents behavior changes, and describes testing. It omits some template checklist sec…
Linked Issues check ✅ Passed Check skipped because no linked issues were found for this pull request.
Out of Scope Changes check ✅ Passed Check skipped because no linked issues were found for this pull request.
  • Fix all pre-merge checks with AI
✨ Finishing Touches 💡 1
📝 Generate docstrings 💡
  • Create stacked PR
  • Commit on current branch

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.

❤️ Share

Comment @coderabbitai help to get the list of available commands.

…firmed

Two silent paths hid the cause of an unconfirmed submission. The reconciler's
bare `continue` on an `unknown` outcome dropped the reason it already carried,
and the transcript read swallowed its error, collapsing an oversize file and a
genuine read failure into the same verdict.

Log both. No control flow changes.

@pullfrog pullfrog Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

✅ No new issues found.

Reviewed changes

  • Oversize vs. read-failure logging — readClaudeProviderHistoryWindow's catch now takes error, distinguishes NodeFileReadTooLargeError (oversize: N bytes exceeds M byte window budget) from a generic read failed, and warns with the transcript path and session id. The INCONSISTENT verdict is unchanged.
  • Unconfirmed-submission logging — reconcileJournalSubmissionsAgainstHistory now logs the unknown outcome's clientMessageId, reason, and fence before its continue, so a submission left unconfirmed is no longer silent.

No control-flow changes; both logs use raw console.warn, matching the existing main-process convention. The focused suites pass (16/16) and oxlint on both changed files is clean.

ℹ️ The static analysis check is red on main, not on this PR

The only failing check on this head is static analysis (and verify, which fails transitively), reporting src/renderer/src/components/editor/DiffViewer.tsx — a file this PR does not touch — at 402 counted lines against the 400 max-lines budget. That file was last changed by main commit 3e7da29 (#11955, merged after this PR's earlier green run), so the failure is pre-existing and unrelated; no change is needed here.

Technical details
# Pre-existing `max-lines` failure on main

## Affected sites
- `src/renderer/src/components/editor/DiffViewer.tsx` — not in this PR's diff; 402 counted lines vs the 400 budget in `.oxlintrc.json`.

## Evidence
- Check runs for `b1fa4ce`: only `static analysis` fails; its annotation is the `DiffViewer.tsx` max-lines error.
- `repos/stablyai/orca/commits?path=.../DiffViewer.tsx` → latest change is main `3e7da29` (`#11955`), 2026-09-19T22:53Z.
- `pnpm exec oxlint` on both files this commit touches: 0 errors.

## Required outcome
- None in this PR. The `max-lines` budget is owned by main; do not add a disable or per-file bump here (AGENTS.md forbids both).

Pullfrog  | View workflow run | Using DeepSeek Flash (free via Pullfrog for OSS) | 𝕏

@nwparker
nwparker merged commit db7b57b into main Sep 20, 2026
28 of 30 checks passed
@nwparker
nwparker deleted the np-oom-scan-claude-history-window-budget branch September 20, 2026 00:25
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.

2 participants