#1868·repomix

grep_repomix_output: -- separators inside contiguous context blocks, matches printed with the context prefix, and RangeError on large outputs

Author: sxh313Created Sep 20, 2026Updated Sep 20, 2026
Labelsbug

Description

grep_repomix_output returns a formattedOutput array that is documented as "Formatted grep-style output with context lines", but the -- separator and the :/- line prefixes are not derived the way grep -C derives them. On a large enough output the same code path stops producing output at all and the tool fails with SEARCH_ERROR.

Three separate symptoms, all from formatSearchResults in src/mcp/tools/grepRepomixOutputTool.ts:

  1. -- is inserted between context blocks that touch or overlap, so a contiguous run of lines looks like it has gaps.
  2. A line that itself matched is printed with the - context prefix when an earlier match's context happened to emit it first, so the caller cannot tell matches from context.
  3. The gap check start > Math.min(...addedLines) + 1 spreads every line emitted so far into Math.min once per match. That is quadratic, and past roughly 200k accumulated context lines it throws RangeError: Maximum call stack size exceeded.

Usage Context

Repomix CLI — specifically repomix --mcp (the stdio MCP server), tool grep_repomix_output.

Repomix Version

1.18.0 (also current main, 9f01703a)

Node.js Version

v24.18.0

Steps to Reproduce

Pack a directory whose file has several matches a couple of lines apart, then grep it through the MCP server. Minimal fixture, src/greetings.js:

javascript
export const hello = 'hello';
const padHello = (s) => s.trim();
export const helloWorld = 'hello world';
export const helloThere = 'hello there';
const goodbye = 'goodbye';
export const helloAgain = 'hello again';

Start repomix --mcp over stdio, call pack_codebase on that directory (outputStyle: "plain"), then call the tool with the returned outputId:

json
{
  "name": "grep_repomix_output",
  "arguments": { "outputId": "<id>", "pattern": "export", "contextLines": 1 }
}

Actual Behavior

Lines 50–57 of the packed file are consecutive. Every line from the first match's context to the last is present, yet the result contains three -- separators, and line 54 — one of the four matches — carries the - context prefix:

50-<file path="src/greetings.js">
51:export const hello = 'hello';
52-const padHello = (s) => s.trim();
--
53:export const helloWorld = 'hello world';
54-export const helloThere = 'hello there';
--
55-const goodbye = 'goodbye';
--
56:export const helloAgain = 'hello again';
57-</file>

totalMatches is 4, so the client can cross-check that line 54 is a match, but nothing in formattedOutput says so.

For the third symptom, calling formatSearchResults directly with disjoint matches 8 lines apart (context 1/1), timed on one core. Left column is main, right column is main with only the gap check changed to track the previous block's end:

matches file lines main previous-block end
10,000 80,010 7.3 s 16 ms
30,000 240,010 79 s 27 ms
60,000 480,010 RangeError: Maximum call stack size exceeded 91 ms
100,000 800,010 RangeError: Maximum call stack size exceeded 223 ms

Through the tool, that throw is caught and returned as an error result with reason: "SEARCH_ERROR" and the message Error: Maximum call stack size exceeded, so the search reports no match information at all. Repomix exists to pack whole repositories, so an output with hundreds of thousands of lines is the normal case rather than a stress case.

Expected Behavior

grep -C 1 semantics: -- only where lines are actually missing from the output, and : on every matching line regardless of which block emitted it. For the fixture above that is a single run with no separators:

50-<file path="src/greetings.js">
51:export const hello = 'hello';
52-const padHello = (s) => s.trim();
53:export const helloWorld = 'hello world';
54:export const helloThere = 'hello there';
55-const goodbye = 'goodbye';
56:export const helloAgain = 'hello again';
57-</file>

Root cause

typescript
// Add separator if there's a gap between previous and current context
if (resultLines.length > 0 && start > Math.min(...addedLines) + 1) {

addedLines holds the index of every line emitted so far, so Math.min(...addedLines) is the start of the first block, not the end of the previous one. The condition therefore asks "does this block start after the very first line?" — true for essentially every second block — instead of "are lines missing between these two blocks?". The same expression is re-evaluated per match, which is the quadratic term and the argument-list overflow.

The prefix is picked per iteration (i === match.lineNumber - 1), and lines already in addedLines are skipped, so a match emitted as an earlier match's context keeps the prefix it was first printed with.

Notes

Existing tests encode the current output ('3-line 3' for a matching line, -- between overlapping blocks), so they need updating alongside the fix.

I have a working fix for all three plus tests; I'll open a PR referencing this issue.