Skip to content

fix: format search_file_contents results grep-style, one line per match - #774

Merged
will-lamerton merged 2 commits into
Nano-Collective:mainfrom
MsfPablo:fix/search-results-grep-format
Aug 7, 2026
Merged

will-lamerton merged 2 commits into
Nano-Collective:mainfrom
MsfPablo:fix/search-results-grep-format

Conversation

@MsfPablo

@MsfPablo MsfPablo commented Aug 3, 2026

Copy link
Copy Markdown
Contributor

Summary

Fixes #766. executeSearchFileContents formatted each match across three output lines (a file:line header, an indented content line, and a blank separator), which costs tokens the model doesn't need — the newline, the 2-space indent, and the blank line between matches are pure formatting overhead.

Switched to standard grep format: file:line:content, one line per match.

Per the issue's note, contextLines produces multi-line content (already prefixed per-line with its own line number by the search itself), so a single file:line:content line doesn't work for that case. For those matches, the header uses - instead of : as the separator (standard grep convention for marking a context block vs. an exact match line) and the content block keeps its own lines rather than being squeezed onto one:

# no context
src/foo.ts:42:const x = 1

# contextLines: 1
src/foo.ts:42-
41: const before = 1
42: const x = 1
43: const after = 1

Test plan

  • pnpm run test:format, test:lint, test:types — all clean
  • pnpm run test:ava source/tools/search-file-contents.spec.tsx — 56 tests pass, including the contextLines cases
  • One existing test (search_file_contents truncates very long matching lines to 300 chars) located "the content line" and checked its raw length, assuming the old two-line-per-match layout where content had its own line. Updated it to strip the file:line: prefix before measuring, since that prefix now shares the line with content.

executeSearchFileContents formatted each match across three lines
(header, indented content, blank separator), which the model doesn't
need: extra newline, 2-space indent, and a blank line between matches
all cost tokens without adding information.

Switch to standard grep format: `file:line:content` on one line per
match. When contextLines produces multi-line content (already
per-line-prefixed by the search itself), use `-` as the header
separator instead of `:`, matching how grep marks context vs. exact
matches, and keep that block on its own lines rather than forcing it
onto one.

Updated the one existing test that located "the content line" and
checked its length assuming the old two-line-per-match layout
(search-file-contents.spec.tsx's truncation test) to account for the
`file:line:` prefix now sharing the line.

Fixes Nano-Collective#766
@akramcodez

Copy link
Copy Markdown
Member

@MsfPablo Thanks for the PR! The grep-style formatting looks good, and I like the - vs : distinction for contextLines. I noticed one small thing though: the results are still joined with \n\n, which leaves a blank line between every match. Since the goal of this issue is to reduce formatting overhead, could you change that to \n (or only add separators where they're actually needed for multi-line context)? Once that's updated and the tests still pass, this looks good to me.

@will-lamerton will-lamerton left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Verified locally: checked out the branch, 56 tests pass.

The contextLines handling is the part that needed care and you got it right. I confirmed against `source/utils/file-search.ts:305-308` that context content really is built as `${lineNum}: ${line}` per line and joined with newlines, and that non-context content is `currentLine.trim()` at `:297` so it can never contain one. That makes `match.content.includes("\n")` a sound discriminator, and `-` as the context header separator is the correct grep convention. Nice to see the code read rather than the issue text pattern-matched.

The spec change is a legitimate adaptation rather than a weakened assertion: the old test measured the whole line because content used to have a line to itself, so stripping the `file:line:` prefix before measuring is right. Tightening 302 to 301 while you were in there is a good catch.

Two follow-ups, neither blocking, and I will pick them up myself:

  1. .join("\n\n") keeps a blank line between every match, which #766 also flagged. Measured on a real 30-match search: main 3045 chars, this PR 2985, single-newline 2956. So this lands about two thirds of the available saving. Single newline between plain matches with a blank line only around multi-line context blocks is probably the end state.

  2. Minor edge case: when a context block collapses to a single line it falls into the plain branch and renders with the wrong separator and a doubled line number. Reproduced on a single-line file with contextLines: 3, giving single.ts:1:1: const onlyLine = 1;. Also reachable if a first context line exceeds MAX_CONTEXT_CONTENT_LENGTH. Cosmetic and rare.

Also worth correcting my own issue text: I estimated 3-4 tokens per match there, and the real figure is closer to 2 characters per match. The win is real and free but smaller than I wrote. That is on me, not on this PR.

Thanks for picking this one up.

@will-lamerton
will-lamerton merged commit 712a93e into Nano-Collective:main Aug 7, 2026
1 check passed
A-S-Manoj pushed a commit to A-S-Manoj/nanocoder that referenced this pull request Aug 7, 2026
search_file_contents put a blank line between every match, which Nano-Collective#766
flagged as waste alongside the two-line-per-match layout that Nano-Collective#774
already removed. Context-free matches now join with a single newline.

Layout is also now decided by the contextLines argument rather than by
sniffing each match's content for a newline. contextLines applies to the
whole call, so it is the accurate signal; sniffing misread any context
block that collapsed to one line, which happens on single-line files and
when truncation drops every newline, rendering them with the exact-match
header and a doubled line number.
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.

search_file_contents wastes tokens on multi-line result formatting

3 participants