Skip to content

fix(search): bound rg JSON and context memory without hiding partial results - #46

Merged
lidge-jun merged 1 commit into
devfrom
fix/search-byte-budgets-45
Sep 22, 2026
Merged

lidge-jun merged 1 commit into
devfrom
fix/search-byte-budgets-45

Conversation

@lidge-jun

Copy link
Copy Markdown
Owner

Summary

Fixes #45.

Bound content/count search data before it can overwhelm the host-to-guest transfer or the guest's 128 MiB worker:

  • Discard rg JSON records above 256 KiB while receiving chunks, before constructing/parsing a full record; resume after its delimiter.
  • Stop after 4 MiB cumulative rg stdout, even when individual rows fit the record limit.
  • Bound content's logical retained JSON to 4 MiB, charging repeated before/after context, JSON escaping and object overhead, not just unique text storage.
  • Disclose loss using partial and complete:false; early output/result stops also set truncated:true, including search.count.
  • Reset context state across discarded records. Keep UTF-8 split handling, unterminated-record handling, exact row-cap semantics, cancellation and file-list NUL delimiters.
  • Document the limits in the catalog, English/Korean README and structure contracts.

Triage / design decisions

  • Retain rg JSON: --max-columns and --only-matching do not reduce its payload. A post-parse slice alone is too late.
  • No implicit maxFilesize or ignore exclusions: those would change the requested corpus and could turn a safety fix into a misleading absence claim.
  • No fabricated hit/path/line placeholder for an unparsed record. Partial empty results are explicitly incomplete.
  • search.files keeps its existing uncapped stream-byte policy; this change targets content/count JSON.
  • Counts remain matching-line counts. A compact --count parser is deferred: it needs dedicated framing for newline-containing filenames and contract tests, not a drop-in --count-matches semantic change.
  • The byte limits are conservative resource policy, not a promise of exact exhaustive results on large corpora. Split path/glob, preserve completeness metadata, or inspect oversized files via byte-range reads. This does not bound rg's own memory for searching a single very large line, nor arbitrary guest code that accumulates many calls.

Verification

  • New regression suite: 14 passed. Includes oversized UTF-8 records, EOF without delimiter, aggregate count truncation, NUL-delimited paths, context amplification, escaped/empty context, and actual guest transport.
  • Full suite: 1,074 tests; 1,073 passed; 0 failed; 1 skipped on local macOS / Node 26.
  • npm run structure:check passed.
  • Original local failing search (max:80, ignored/hidden/excluded files included) through the modified runner and real guest worker: ok:true, 26 rows, complete:false, truncated:true, budget warnings; no OOM. The corpus is private and mutable, not committed or used as a portable benchmark.

Corrections to earlier issue analysis

The visible failure was a guest worker OOM. Host JSON parsing and retained matches contribute to size, but the original report did not isolate the precise peak-allocation stage. This PR tests the worker boundary instead of claiming host parsing alone explains it. Also, the current public hit schema already drops rg submatches; it does not expose the offset invariant discussed upstream. We skip oversized records instead of attempting to rewrite them.

References: BurntSushi/ripgrep#1451 and BurntSushi/ripgrep#2821.

@chatgpt-codex-connector

chatgpt-codex-connector Bot commented Sep 22, 2026 •

Copy link
Copy Markdown

Codex Review Summary

This comment shows the latest Codex review activity on this pull request.

Review Status Commit Review trigger
📝 Code Review ✅ Completed 2026-09-22T08:27:19.019285Z 6c28220 PR opened
ℹ️ 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" or "@codex security review".

Codex reacts with 👀 while any review is running, comments if it has suggestions, and reacts with 👍 once all reviews finish with no findings.

@lidge-jun
lidge-jun force-pushed the fix/search-byte-budgets-45 branch from 6c28220 to 1116376 Compare September 22, 2026 08:22

@chatgpt-codex-connector chatgpt-codex-connector Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

💡 Codex Review

Here are some automated review suggestions for this pull request.

Reviewed commit: 6c282209eb

ℹ️ 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".

Comment thread src/rg.js
Comment on lines +451 to +455
if (retainedBytes + hitBytes + beforeBytes > maxRetainedBytes) {
return {
stop: true,
warning: `retained-budget-exceeded: retained output exceeded limit of ${maxRetainedBytes} bytes`,
};

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

P1 Badge Enforce the retained budget after normalization merges

When a non-ASCII query or glob has distinct NFC/NFD forms, src/host/search.js runs two to four content searches and unions their rows in mergeRows; this check therefore permits each component result to retain up to 4 MiB independently. The final guest-bound result can consequently reach 8–16 MiB while still reporting complete: true and no warning—for example, disjoint composed/decomposed matches produce a 4.45 MiB serialized result despite the documented 4 MiB content-result limit. Apply the retained-byte budget to the merged result as well so normalization cannot bypass the memory bound.

Useful? React with 👍 / 👎.

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