Skip to content

fix(bettergrep): clamp context lines to 50 instead of rejecting them - #20

Merged
dhaern merged 1 commit into
mainfrom
fix/grep-context-clamp
Oct 30, 2026
Merged

dhaern merged 1 commit into
mainfrom
fix/grep-context-clamp

Conversation

@dhaern

@dhaern dhaern commented Oct 30, 2026

Copy link
Copy Markdown
Owner

OpenCode 2 validates tool arguments against the schema, so grep calls with context, before_context or after_context above 20 failed with Too big: expected number to be <=20 and the model had to retry. OpenCode 1 does not enforce the schema maximum, so it clamped those values to 20 without telling the model.

In the session history of one install, 189 of 14,818 v1 context values were above 20, ranging from 21 to 120, and all 10 such calls in v2 were rejected. A cap of 50 covers 99.8% of the v1 values.

Production changes

  • constants.ts: MAX_GREP_CONTEXT goes from 20 to 50.
  • schema.ts: the three context fields drop .max(). Both hosts now accept larger values, and the existing clamp in normalize.ts brings them down to 50. Each field description states the cap.
  • format.ts: the formatter appended the output body with lines.push(...formattedBody). With 50 context lines and 5,000 matches a result can reach about 520k lines. Bun 1.4.4 throws RangeError at about 638k spread arguments, and Node already fails at about 153k, so the body is now appended in a loop.
  • README.md: the context row states the clamp.
  • Version 1.2.1 for the three packages and the root.

Tests

  • Three tests in src/v2/plugin.test.ts: context, before_context and after_context set to 85 are accepted and run with 50. They fail on main with the schema error.
  • typecheck, check and build are clean, and 693 tests pass on Bun 1.4.4 (197 glob, 315 grep, 181 read).

OpenCode 2 validates tool arguments against the schema, so grep calls with
context, before_context or after_context above 20 failed with "Too big".
OpenCode 1 skips that check and clamped them to 20 without telling the model.

The cap is now 50 and the schema no longer sets a maximum, so both hosts accept
larger values and clamp them to 50. The field descriptions state the cap.

The formatter appended the output body with a spread call. With 50 context
lines a result can reach about 520k lines, close to where Bun's argument limit
throws RangeError, so it now appends in a loop.

Release 1.2.1 for the three packages.
@dhaern
dhaern merged commit cada29e into main Oct 30, 2026
@dhaern
dhaern deleted the fix/grep-context-clamp branch October 30, 2026 23:33
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