You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
Stop code_search when context lines exceed the output budget - #1737
With context flags such as -A 10, ripgrep emits context events after a match. code_search adds those lines to its collected output, but checks the output budget only inside the isMatch branch. A context-heavy search can therefore keep collecting output beyond the configured budget until another match, process exit, or timeout.
Changes
Move the existing result-limit check after the match-counter update so every included JSON line checks the output budget. Context lines still do not increment match counters.
Add one regression using the existing process-broker seam: a match followed by context lines exhausts a 128-character budget while process completion remains pending. The search must settle early, request SIGTERM, and report one match with the output-size notice.
Verification
Bun 1.3.14 on Windows, with dotenv autoload disabled, fixture environment values, and an isolated HOME:
New regression: 1 fail before the production change; 1 pass after.
Six related files (SDK search, file records, sponsored broker, formatter, and flag parser): 131 pass, 30 fail after; 130 pass, 31 fail with the production change removed. Comparing failure names leaves only the new regression; the other 30 failures are unchanged Windows path/separator and error-expectation failures.
bun node_modules/typescript/bin/tsc --noEmit -p sdk: pass before and after.
Full SDK build, including declarations: pass.
bun freebuff/cli/build.ts 0.0.0-ci: pass (Windows binary).
cli/bin/freebuff.exe --version: 0.0.0-ci.
Official smoke-binary.ts: pass for tree-sitter, dotenv isolation, and the login boot screen.
Prettier: new test passes; the production file has the same warning on unmodified main.
git diff --check: pass.
Notes / Scope
Only the JSON streaming budget check and one regression test change. Existing per-file/global match counting, plain-text record modes, and process-cleanup behavior stay in place. This does not establish a strict cap on every stream buffer or test native process-kill escalation.
This is a locally discovered bug with no existing issue number. Linux CI and a fresh frozen-lockfile install have not been run locally; existing installed dependencies were reused.
Good catch and a minimal, well-targeted fix. In sdk/src/tools/code-search.ts, moving the atResultLimit() check out of the isMatch branch (previously only triggered after a match) so it runs after every JSON line, including context events, correctly closes the gap where -A/-B/-C flags could let output balloon past maxOutputStringLength until the next match or process exit.
The regression test in code-search-output-budget.test.ts is well constructed: it uses the existing process-broker seam to simulate a match followed by context lines that blow the budget while the process is still alive, then asserts the search settles early, sends SIGTERM, and reports the output-size notice. That's exactly the kind of test this repo expects for a streaming/budget bug.
A few things worth double-checking before porting:
Confirm atResultLimit() doesn't have side effects tied to match-count state that would behave oddly when called on a context line with zero matches so far (e.g., an empty-budget edge case on the very first context line before any match).
The PR notes 30/31 pre-existing failures as unrelated Windows path/separator issues — worth a quick confirming run on Linux CI since that wasn't done locally, but this shouldn't block review of the logic itself.
Overall this is a small, correct, well-tested fix to a real streaming bug. No forbidden paths touched. Recommending as a port candidate.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Problem / Why
With context flags such as
-A 10, ripgrep emitscontextevents after a match.code_searchadds those lines to its collected output, but checks the output budget only inside theisMatchbranch. A context-heavy search can therefore keep collecting output beyond the configured budget until another match, process exit, or timeout.Changes
Move the existing result-limit check after the match-counter update so every included JSON line checks the output budget. Context lines still do not increment match counters.
Add one regression using the existing process-broker seam: a match followed by context lines exhausts a 128-character budget while process completion remains pending. The search must settle early, request SIGTERM, and report one match with the output-size notice.
Verification
Bun 1.3.14 on Windows, with dotenv autoload disabled, fixture environment values, and an isolated HOME:
bun node_modules/typescript/bin/tsc --noEmit -p sdk: pass before and after.bun freebuff/cli/build.ts 0.0.0-ci: pass (Windows binary).cli/bin/freebuff.exe --version:0.0.0-ci.smoke-binary.ts: pass for tree-sitter, dotenv isolation, and the login boot screen.git diff --check: pass.Notes / Scope
Only the JSON streaming budget check and one regression test change. Existing per-file/global match counting, plain-text record modes, and process-cleanup behavior stay in place. This does not establish a strict cap on every stream buffer or test native process-kill escalation.
This is a locally discovered bug with no existing issue number. Linux CI and a fresh frozen-lockfile install have not been run locally; existing installed dependencies were reused.