Skip to content

feat(tools): search v2 and tree over an ignore-aware contained walk (T2) - #32

Merged
lg2m merged 4 commits into
mainfrom
feat/tool-layer-t2-search-tree
Sep 13, 2026
Merged

lg2m merged 4 commits into
mainfrom
feat/tool-layer-t2-search-tree

Conversation

@lg2m

@lg2m lg2m commented Sep 12, 2026

Copy link
Copy Markdown
Collaborator

T2: search v2 and tree over one ignore-aware, contained walk

Spec: docs/plans/tool-layer.md#d2--read-side-tools-search-read_file-tree-t2-t3 Linear: ENG-772

What changed

User-visible

  • search gains modes (content | names | definition | references), regex, case=smart|sensitive|insensitive, include/exclude globs, context, limit/max_per_file, include_ignored, and a cursor. Results group by file as L<n>: text; the header carries matches=<shown>/<total>[+] files= scanned= [skipped=] [next=<cursor>] [truncated=bytes] [partial=scan|bytes|time] [hint=case_insensitive_matches=N]. The walk honours .gitignore/.ignore/.git/info/exclude (stacked, git semantics), skips hidden entries and target/node_modules/dist/build/.venv/__pycache__/.git, and never follows symlinks. Files > 4 MiB and binary files are skipped and counted.
  • search never cuts the middle out of a result: the walk stops at its 12 KiB body budget and hands back a cursor, so every match is reachable across pages and none repeats.
  • tree replaces list_dir in the advertised catalog: breadth-first fill (top level complete first), (Nf Nd) counts, single-child chain collapse (deep/er/est/), packed leaf rows with sizes, @ symlinks, …ignored markers, depth ≤ 6, limit ≤ 500, glob.
  • list_dir stays a hidden alias for tree depth=1 (ignored entries included) so persisted transcripts and grants resolve; it resolves only while tree is exposed and exposed_tools: ["list_dir"] exposes tree.
  • Typed failures the model can act on: invalid_regex, regex_too_large, bad_glob, path_not_found, path_escapes_workspace, cursor_invalid, invalid_symbol, not_a_directory, invalid_depth/invalid_limit/…
  • System prompt steers to search/tree over grep/rg/find/ls. TUI reads hit/entry counts from the header (25/90+ hits · 3 files, 3/12 entries).

Design

  • tools/walk.rs (new): list_children reads one directory through the workspace's cap-std Dir; IgnoreStack uses ignore::gitignore::Gitignore as a matcher only (never ignore::Walk, which opens ambient paths). Containment keeps one addressing scheme. Children sort as name/ for directories so a DFS yields bytewise path order — the property the cursor depends on.
  • tools/lang.rs (new): definition templates per language as data; compiled lazily per symbol per language; definition mode prefilters candidate lines with the word-boundary scan (2× on the full-scan case).
  • tools/search.rs: whole-buffer regex::bytes scan, matches mapped to lines afterwards; cursor = base64url(path \0 line); skips_dir prunes whole directories that sort before the cursor without allocating.
  • tools/tree.rs: BFS queue of (dir, depth, parent); counts from a bounded sub-walk; ListDirArgs → TreeArgs alias.
  • Perf fix found by the gate: the larger schemas made every run start re-serialize and re-hash the static tool list (+7 % tool_dispatch, +20 % plan_compile). StaticTool now carries its serialized schema (built-ins serialized once per process), and ToolCatalog precomputes the static/full measurements at compile; the run loop asks the catalog and measures afresh only for pinned lists.

Dependencies — ignore 0.4 (new; brings globset, bstr, crossbeam-deque/epoch), regex 1 (already locked via tree-sitter), base64 (workspace) added to qq-core. Root Cargo.toml gained the two [workspace.dependencies] rows; recorded in root.md. No version bumps.

How verified

  • cargo test --workspace: 1315 passed / 0 failed. cargo test -p qq-provider --no-default-features --features test-support: 162 passed. cargo fmt --check ✓, cargo clippy --workspace --all-targets --all-features -D warnings ✓.
  • 15 tests added, covering the plan's adversarial fixtures: gitignored hit must not appear (root + nested .gitignore, negation, /vendor/, hidden, target/, and include_ignored lifting all but .git); match perf(provider): prompt-cache breakpoints for Anthropic Messages and Bedrock Converse (C4) #61 reachable by cursor with no repeats across pages; oversized (> 4 MiB) and NUL-binary files skipped and counted; invalid UTF-8 mid-file; CRLF (CR stripped); symlink loop; byte budget → cursor, never a mid-body marker; per-file cap +N more in file; definition/references/regex/context/globs/smart-case/hint; typed failures; tree BFS/counts/chains/packing/…ignored/glob/limit/not_a_directory/invalid_depth; alias exposure; catalog measurement equality; TUI header metrics.
  • Regression assertions updated where the contract changed (exact tree output for the old list_dir cases, schema hash).

Measurements

Gate Result Budget
search_walk content, no match, full 10k-file scan (page cache) 22 ms ≤ 150 ms
search_walk content, rare symbol 5.4 ms
search_walk content, common symbol first page 0.3 ms
search_walk definition / references (full scan) 5.4 ms / 21 ms
search_walk names 2.2 ms
search_walk tree depth 2 (200 dirs + ignored target/) 6.7 ms
tool_dispatch A/B (pinned core, 12 pairs, median) 42.8 → 41.4 µs no regression
plan_compile 22.2 → 23.1 µs (27.7 before the perf commit)

Evidence: target/qq-perf/t2-2026-09-12/ (untracked).

Docs

  • docs/design/tools.md: § Built-In Tools updated; § Read-Side Walk (new).
  • docs/plans/progress/tool-layer.md: T1 shipped, T2 in review + receipt. root.md: dependency rows. Plan dependency table updated.

Compatibility

  • PROTOCOL / DESCRIPTOR / schema versions: unchanged. Tool declaration hash changed (expected; asserted in built_in_tool_declarations_keep_their_order_and_schema_identity).
  • Model-visible: search output grammar changed from path:line:text to grouped L<n>: text under a header; list_dir results now carry a tree header. Persisted transcripts replay as-is; list_dir calls keep executing.
  • MAX_SEARCH_BYTES (16 MiB) removed; the scan bound is now 64 MiB with partial=bytes.

Follow-ups

  • T3 reuses lang tables for read_file mode=outline; T4 gives truncated=bytes a spill handle; T12 reuses the walker for @ completion.
  • AGENTS.md still names Linear team DEV; workspace team is ENG (issue filed under ENG/qq).

…(T2)

search: modes content/names/definition/references, regex and smart case,
include/exclude globs, context, limit and max_per_file, exact resume cursor
(base64url path\0line), header with matches=shown/total files= scanned=
skipped= next= partial= hint=, byte budget respected by the walk itself so
nothing is cut mid-body. Zero sensitive results hint the insensitive count.

tree: breadth-first fill, (Nf Nd) counts from a bounded sub-walk,
single-child chain collapse, packed leaf rows, sizes, @ symlinks,
…ignored markers; list_dir stays a hidden alias for tree depth=1 and
resolves while tree is exposed.

walk: cap-std listing with a stacked gitignore matcher (.git/info/exclude,
ancestors, per-directory), generated-directory list, binary sniff, 4 MiB
file cap, shared scan budget with entry/byte/time stops.

lang: per-language definition tables (Rust, TS/JS, Python, Go, Zig, C/C++,
Markdown) compiled lazily per symbol.

Deps: ignore, regex, base64 added to qq-core (regex/base64 were already in
the lock; ignore brings globset, bstr, crossbeam-deque/epoch).

Slice T2 of docs/plans/tool-layer.md.
… and TUI (T2)

- list_dir resolves as a hidden alias while tree is exposed; an exposure
  list naming list_dir exposes tree.
- Config vocabulary, pruning list, and the system prompt name tree and
  steer the model to search/tree over shell grep, rg, find, ls.
- TUI reads search and tree metrics from the header (shown/total hits,
  entries) instead of counting body lines.
- New search_walk bench over a synthetic 10k-file workspace (content,
  definition, references, names, tree) with a same-size target/ tree the
  walk must skip.
- Definition mode prefilters candidate lines with the word-boundary scan
  before running the anchored table pattern (2x on the full-scan case).
…s measurement

The T2 schemas are ~40% larger than the six they replace, which showed up
as +7% on tool_dispatch (run start re-serialized and re-hashed every
static schema) and +20% on plan_compile.

- StaticTool carries its serialized schema; built-ins are serialized once
  per process behind a OnceLock and cloned as Arc<str>.
- ToolCatalog computes the static and full-exposure measurements at
  compile from the schemas it already serialized; the run loop asks the
  catalog instead of re-measuring, and only pinned lists are measured
  afresh.

tool_dispatch A/B (pinned core, 12 pairs): base 42.8 µs → cand 41.4 µs
median. plan_compile 22.2 → 23.1 µs (was 27.7 µs).
Design: § Built-In Tools names tree and the list_dir alias; § Read-Side
Walk (new) covers the cap-std-fed ignore stack, search modes, output
grammar, cursor and partial semantics, and tree's fill order and bounds.
Ledger: T1 shipped, T2 in review with receipt; root.md records the
workspace dependency rows; plan dependency table updated.
@lg2m
lg2m merged commit 8bb4050 into main Sep 13, 2026
4 checks passed
@lg2m
lg2m deleted the feat/tool-layer-t2-search-tree branch September 13, 2026 21:23
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