Skip to content

fix(grep-code): give each context line its own line number - #135

Merged
spelech merged 2 commits into
mainfrom
fix/grep-code-context-line-numbers
Nov 12, 2026
Merged

spelech merged 2 commits into
mainfrom
fix/grep-code-context-line-numbers

Conversation

@spelech

@spelech spelech commented Nov 12, 2026

Copy link
Copy Markdown
Owner

Problem

grep_code can give wrong line numbers for context lines. An agent searched a local source (hub-prefix) with is_regex and context lines. All context lines in one block had the same number, for example 47 or 63.

The disk scan and the extended scan give correct numbers. The vector-store chunk path does not:

  • The indexer strips the chunk text. Blank lines at the start of a chunk are lost, so the lines after them move up by one or more numbers.
  • Markdown chunks count lines from the body after the frontmatter. frontmatter.loads also removes blank lines at the start of the file. Thus all numbers in the file are too small.
  • File summaries are stored as chunks with start_line=1. A match in a summary gives line numbers that are not in the file.
  • The context stops at the edge of the chunk that has the match.

Fix

  • New module app/services/code_grep_chunks.py. It puts all chunks of a file together in one line map before the scan. It reads the chunks with get_file_chunks when the store has it. It finds the position of a stripped chunk from the lines that are already known. It ignores summary chunks. Each context line now gets its own file line number, and the context can continue into the next chunk.
  • execute_code_grep uses this module for the chunk path.
  • _sanitize_match_lines checks the context numbers. If the numbers do not go up line by line, it counts them again from the match line. This is a safety net for all paths.
  • The indexer adds the number of lines before the Markdown body to the chunk and link line numbers. Existing indexes keep the old numbers until the files are indexed again.

Tests

  • Added tests/test_grep_code_context_lines.py. It tests the indexed-path scan, the extended scan, the chunk path with and without get_file_chunks, the sanitizer, and the frontmatter offset.
  • Three of the new tests fail on origin/main (chunk path and sanitizer). All pass with this change.
  • Regenerated REQUIREMENTS.md.

🤖 Generated with Claude Code

Steve Pelech and others added 2 commits November 12, 2026 15:39
@spelech
spelech merged commit 6bbd20d into main Nov 12, 2026
15 checks passed
@spelech
spelech deleted the fix/grep-code-context-line-numbers branch November 12, 2026 22:07
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