Repository navigation
fix: format search_file_contents results grep-style, one line per match - #774
Conversation
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
|
@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
left a comment
There was a problem hiding this comment.
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:
-
.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. -
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, givingsingle.ts:1:1: const onlyLine = 1;. Also reachable if a first context line exceedsMAX_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.
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.
Summary
Fixes #766.
executeSearchFileContentsformatted each match across three output lines (afile:lineheader, 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,
contextLinesproduces multi-linecontent(already prefixed per-line with its own line number by the search itself), so a singlefile:line:contentline 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:Test plan
pnpm run test:format,test:lint,test:types— all cleanpnpm run test:ava source/tools/search-file-contents.spec.tsx— 56 tests pass, including thecontextLinescasessearch_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 thefile:line:prefix before measuring, since that prefix now shares the line with content.