Skip to content

fix(roadmap): search_code shows code lines first and skips the archive #5462

Description

@dan-petty

Problem or Motivation

search_code (InvestigationTools.search_code in src/devops_cli/roadmap/refine/refine_investigation.py) runs git grep -n -I -F -i over the whole tree at the staged commit, with no path filter. cut then keeps at most 240 lines and 8,000 characters. git grep gives the lines in path order. So lines from CHANGELOG.md, .agents, .github, docs and openspec/changes/archive fill the answer before any line from src/ or tests/. The answer ends with a cut notice that does not say where the left-out matches are. The stage toolbox (StageTools) and the refine investigation share this tool.

Measured at origin/main 499b42c with the same command and the same cut:

Text Matches In the archive In src/ or tests/ Lines kept Kept lines in src/ or tests/
ci test 5,368 4,360 765 23 0
ci docs 791 526 220 27 0
check_proposal( 117 11 98 18 0
read_spec 138 59 62 15 0
trace gate 103 55 27 14 0

Effects on the stage job:

  • search_code answers of plan and review runs were about 19M characters in 3 days (devops_cli_roadmap_tool_answer_chars), about as large as the read_file answers.
  • In the comment sample of 54 items (8,213 calls), 3,251 searches used only 2,467 distinct texts.
  • The planner searched ci test in 18 items, got no code line, and then read all of src/devops_cli/commands/ci.py (about 23.7k tokens).

Proposed Solution & Architectural Design

  1. Leave out openspec/changes/archive/ by default with a git grep pathspec exclude (:(exclude)openspec/changes/archive).
  2. Put the lines of code files first, then the lines of documents (.md, .rst, .txt, CHANGELOG.md). Keep the path order inside each class.
  3. Cut each line at about 200 characters.
  4. End a cut answer with the count of left-out matches for each top directory, such as left out: src 742, tests 23, docs 31.
  5. Add an optional path argument: a directory or a glob pathspec. With it the model can search the archive or one folder.
  6. Change the tool description so that the model knows items 1, 4 and 5.

The rules apply to every target repository: they use the file class and the pathspec, not the layout of devops-cli.

Target Subsystem

SDLC, Git & PR Governance: src/devops_cli/roadmap/refine/refine_investigation.py (search_code, cut), src/devops_cli/roadmap/stage/stage_tools.py, the tool rules of src/devops_cli/ai/tasks/.

Existing Tool Considered

git grep pathspec magic (:(exclude), :(glob)) does the filter, and the tool already uses git grep. ripgrep has file types, but it reads the working tree, not a commit, and it is a new runner dependency. Cost to build: about 0.6M tokens and 2 hours (estimate), about 60 lines and 6 tests. Keep cost: low.

Acceptance Criteria

  • Test first: in a fixture repository where an archive file and a long CHANGELOG.md line match before a src/ line, the answer holds the src/ line, holds no archive line, and ends with the left-out count for each top directory.
  • Test first: search_code(text, path="openspec/changes/archive") returns the archive lines.
  • A test: no answer line is longer than the line cut.
  • Measure: devops roadmap eval stage --replay keeps every known-bad plan refused. From the stage comments, for 7 days: the characters of each search_code answer, the searches of each run, and the read_file calls on src/devops_cli/commands/ci.py and src/devops_cli/roadmap/stage/stage_change.py.
  • Live proof after release: in real Service stage runs, the share of search_code answers that hold at least one code line when the commit has code matches (baseline: 0 of the 5 texts above).

Dependencies

None. It is separate from the command card item, which removes the need to search for the devops ci forms.

Standards Compliance

  • I confirm this proposal aligns with AGENTS.md engineering principles (cyclomatic complexity <= 10, nesting <= 4 blocks, zero bare exceptions, tests that cover the changed lines).

Alternatives check of 2026-11-23: in-use.
The repository already uses this tool.
Tool: git/git (https://github.com/git/git), NOASSERTION, last activity 2026-11-22, maintained.
Reason: Searches found no tool that shapes code-search output; git grep pathspec magic covers the default archive exclusion and an optional path filter, but the code-first order, per-line cut and left-out counts stay custom.

Covers:

  • :(exclude) pathspec leaves out the archive by default
  • A pathspec argument limits the search to one directory
  • --count gives exact totals for left-out counts

Lacks:

  • No code-before-document ordering of matches
  • No per-line length cut
  • No left-out counts per top directory
  • No tool description for the model

Proposed design

Problem statement

Fix search_code to put code lines first, leave out the archive by default, accept an optional path, cut long lines and report left-out counts. Today git grep returns matches in path order, so CHANGELOG.md, .agents, .github, docs and the archive fill the answer before src/ or tests/. The cut notice does not say where the left-out matches are. Stage and refine runs then spend tokens on large file reads.

Acceptance criteria

Requirement: Default archive exclusion

search_code SHALL leave out openspec/changes/archive from a search that names no path.

Scenario: Archive match is not in the default answer

  • WHEN search_code("needle") runs on a fixture commit where an archive file and a src/ file hold the text
  • THEN the answer holds the src/ line and holds no archive line.

Requirement: Optional path filter

search_code SHALL accept an optional path argument that limits the search to one repository directory or git pathspec.

Scenario: Archive search uses the path argument

  • WHEN search_code("needle", path="openspec/changes/archive") runs on the fixture commit
  • THEN the answer holds the archive line.

Requirement: Code lines before document lines

search_code SHALL put matches from code files before matches from .md, .rst and .txt documents.

Scenario: Code match comes before a document match

  • WHEN a fixture commit holds a matching CHANGELOG.md line and a matching src/ line and the answer keeps both
  • THEN the src/ line comes before the CHANGELOG.md line.

Requirement: Per-line cut

search_code SHALL cut each formatted match line to at most 200 characters.

Scenario: Long match line is shortened

  • WHEN a fixture commit holds a matching line longer than 200 characters
  • THEN no line of the answer is longer than 200 characters.

Requirement: Left-out counts by top directory

search_code SHALL end a cut answer with the count of left-out matches for each top directory.

Scenario: Cut answer reports the left-out counts

  • WHEN the fixture commit holds more matches than the answer keeps in src, tests and docs
  • THEN the answer ends with a line such as left out: src <n>, tests <n>, docs <n> with the left-out count for each directory.

Requirement: Tool description states the search rules

The search_code tool description SHALL state the default archive exclusion, the optional path argument and the left-out counts.

Scenario: Tool description tells the model the rules

  • WHEN the tool list of InvestigationTools and StageTools is built
  • THEN each search_code description names the archive exclusion, the path argument and the left-out counts.

Requirement: Stage toolbox shares the search

StageTools SHALL use the same search_code behavior as InvestigationTools.

Scenario: Stage toolbox applies the archive exclusion

  • WHEN StageTools.search_code runs on the fixture commit
  • THEN the answer holds the src/ line and holds no archive line.

Requirement: Stage replay keeps refusals

The stage replay SHALL keep every known-bad plan refused after the search change.

Scenario: Stage replay keeps refusals

  • WHEN uv run devops roadmap eval stage --replay runs
  • THEN every known-bad plan is refused.

Existing Tool Considered

git/git (https://github.com/git/git) has verdict in-use; see the alternatives check in the issue body for what it covers and lacks. The proposal uses its pathspec magic for the default archive exclusion and the optional path filter. It adds the code-first order, the 200-character line cut, the per-top-directory left-out counts and the tool description in search_code, which the tool lacks. The issue estimates about 60 lines and 6 tests, 0.6M tokens and 2 hours to build, with low keep cost.

Key questions

  • How should search_code exclude the archive and still let the model search it?: Use a default :(exclude)openspec/changes/archive pathspec. Add an optional path argument that replaces the default filter. Validate the path as a repository-relative git pathspec. (best practice) [sources: src/devops_cli/roadmap/refine/refine_investigation.py]
    • Chosen: Default exclude plus optional path. git grep pathspec magic covers the exclusion and the path filter, and one code path serves both jobs.
    • Rejected: Exclude only with a separate flag. The model must know a second argument and can forget it.
    • Rejected: No default exclusion. Archive lines still fill the answer.
  • How should the tool count left-out matches when git grep --max-count caps each file?: When the answer is cut, run git grep --count for the same search. Subtract the kept matches for each top directory. Report the difference. Keep the per-file line cap for the line output. (best practice) [sources: src/devops_cli/roadmap/refine/refine_investigation.py]
    • Chosen: Second git grep --count call. It gives exact totals without holding every match line in memory.
    • Rejected: Count from the capped lines. A file with more matches than the cap undercounts the left-out matches.
    • Rejected: Remove the per-file cap. A file with millions of matches can exhaust memory.
  • How should StageTools and InvestigationTools share the new search behavior?: Keep one search_code implementation in InvestigationTools. Have StageTools call it. Do not copy the filter, order, cut and count code. (best practice) [sources: src/devops_cli/roadmap/refine/refine_investigation.py, src/devops_cli/roadmap/stage/stage_tools.py]
    • Chosen: One shared implementation. Both jobs get the same behavior and one test set.
    • Rejected: Separate implementation in each toolbox. The two copies can drift and double the tests.
  • How should the tool order code lines before document lines?: Classify .md, .rst and .txt paths as documents. Put all other paths first. Keep the git path order inside each class. (best practice) [sources: src/devops_cli/roadmap/refine/refine_investigation.py]
    • Chosen: Suffix-based document class. It is simple, works for any target repository, and follows the issue.
    • Rejected: Repository-specific directory list. It breaks the rule for other target repositories.
    • Rejected: No ordering. Documents still fill the answer before code.

Fit

Fits in one pull request.

Suspected block

None.

Dependencies

None.

Activity

  1. added
    source/agent
    type/bug
    scope/roadmap
    status/new
    value/medium
    effort/low
    on Nov 13, 2026
  2. dan-petty commented on Nov 13, 2026

    @dan-petty
    OwnerAuthor
  3. added
    priority/p2-medium
    on Nov 13, 2026
  4. devops-cli-service commented on Nov 17, 2026

    @devops-cli-service
    Contributor

    Alternatives check: in-use

    Reason: git grep already searches the tree at a given commit, and its pathspec magic (:(exclude), :(glob)) covers the archive exclusion and the optional directory/glob filter that the issue asks for; it is also the grep the tool already runs. It does not order code before documents, cut long lines, or count left-out matches per top directory, so the rest of the fix stays in search_code.

    The repository already uses this tool.

    Tool: git/git (https://github.com/git/git)

    • License: NOASSERTION
    • Last activity: 2026-11-16
    • Maintained: yes

    Covers:

    • Searches the tree at a given commit, not only the working tree
    • A :(exclude)openspec/changes/archive pathspec leaves the archive out by default
    • A directory or glob pathspec restricts a search to one folder, such as the archive

    Lacks:

    • No code-first ordering: matches stay in path order, so CHANGELOG.md, docs/ and the archive still precede src/ and tests/
    • No per-line truncation at about 200 characters
    • No count of left-out matches for each top directory
    • No tool description or model guidance; that remains in search_code

    Tools considered:

    Searches run:

    • git grep pathspec exclude directory :(exclude) documentation, results: 5
    • ripgrep search a specific git commit tree without checkout, results: 5
    • git-scm git grep pathspec magic exclude, results: 5
    • BurntSushi ripgrep user guide glob max-columns, results: 5
    • git grep documentation, results: 5
    • ripgrep, results: 5
    • github.com BurntSushi ripgrep repository, results: 5
    • git scm git-grep man page, results: 5
    • search source code in git history with path filter command line tool, results: 5
    • tool to search code lines at a git commit excluding a directory, results: 5

    Sources:

    Checked on 2026-11-17. A person decides what this means for the item. The check changes no label and closes no issue.

  5. added
    status/ready
    and removed
    status/new
    on Nov 18, 2026
  6. devops-cli-service commented on Nov 18, 2026

    @devops-cli-service
    Contributor

    Status set to Ready at 9be129e (see proposed design in issue body).

    Moved to new items: #6239.

  7. added
    owner/agent
    status/in-progress
    status/in-review
    and removed
    status/ready
    status/in-progress
    on Nov 29, 2026
  8. removed
    status/in-review
    owner/agent
    on Nov 29, 2026
  9. devops-cli-service commented on Nov 29, 2026

    @devops-cli-service
    Contributor

    Live since 2026-11-29 16:17 UTC

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    effort/lowpriority/p2-mediumscope/roadmapsource/agenttype/bugvalue/medium

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions