Skip to content

feat(tools): read_file v2 with a hash header, ranges, outline, and info (T3) - #35

Merged
lg2m merged 5 commits into
mainfrom
feat/tool-layer-t3-read-file
Sep 14, 2026
Merged

lg2m merged 5 commits into
mainfrom
feat/tool-layer-t3-read-file

Conversation

@lg2m

@lg2m lg2m commented Sep 14, 2026

Copy link
Copy Markdown
Collaborator

T3: read_file v2 — hash header, ranges, outline, info, if_changed_since

Spec: docs/plans/tool-layer.md § D2 (read_file), task T3 Linear: ENG-773 Follows: #32 (T2)

What changed

User-visible

  • Every read_file result opens with read <path> L<a>-<b>[,<c>-<d>]/<total> h:<hash> and numbers each line (<n>\t<text>, one gutter width per call). The header is what a pruned stub keeps, so the model retains the hash after the body is dropped.
  • ranges: ["12", "40-80", "400-"] — up to eight windows in one call, merged when they overlap or touch, ascending, separated by --. offset/limit remains the one-range form; passing both is invalid_ranges.
  • if_changed_since: "h:<12 hex>" — when the file is unchanged the whole answer is read <path> unchanged h:<hash> lines=<n> and the file is still recorded for the staleness guard. A re-read before an edit now costs one line.
  • mode: "outline" — L<line> <kind> <name> nested by indentation (≤ 400 rows) from the same per-language tables search mode=definition uses (Rust, TS/JS, Python, Go, Zig, C/C++, Markdown headings). outline_unsupported for other languages.
  • mode: "info" — size= lines= h: utf8= eol=lf|crlf|none perms= binary=[ mime=] on one line, for any file including binaries, which lines/outline refuse with not_text.
  • A read is never cut mid-line: the 32 KiB default budget stops on a whole row, the header says truncated=bytes, and the marker names offset=<next>. Replaces the head/tail cut for reads (the shared bounding still backstops at 128 KiB).
  • CRLF files render without the CR (hash is of the bytes). Lines over 2 000 bytes clip with …+N and clipped= counts them.
  • Images (png jpg gif webp) answer info plus hint=image_unsupported_by_model from every mode until T11.
  • Typed failures: invalid_ranges, invalid_offset, invalid_limit, invalid_if_changed_since, range_out_of_bounds (with last_line=), not_a_file, not_text, path_not_found, path_escapes_workspace, outline_unsupported.
  • TUI row metric comes from the header (Read a.rs L1-40 of 120 lines, … · truncated, unchanged, 3 items, info); the header is hidden in the expanded body. Prompt teaches the hash, ranges, and outline.

Design

  • tools/read.rs rewritten around one load() (contained path → metadata → read to the 4 MiB cap → hash) and three renderers. Rows are pushed whole-or-not-at-all against an escaped-byte budget so no partial last line ever reaches the model.
  • lang.rs gains Language::outline() — named-group tables compiled once per process (OnceLock), run multi-line over the whole buffer, with a small statement-keyword filter (if/for/return…) for the C function form and an indented-const filter for TS/Zig locals. The definition tables are unchanged.
  • Header::token() added for bare header tokens (L1-40/120, h:…, unchanged) alongside field().
  • Perf fix found by the gate: the first candidate pre-allocated a 32 KiB body per read (+4 % on tool_dispatch). The body is now sized to the file.

How verified

  • cargo test --workspace: 1346 passed / 0 failed; fmt and clippy -D warnings clean.
  • 6 tests added: outline tables across six languages (nesting, Go receivers, Markdown levels, statement filters); ranges merge/align/open-end/out-of-bounds plus nine typed failures; if_changed_since unchanged-then-changed with recording asserted both ways; outline/info/CRLF/binary/image; byte-budget stop with whole rows and the resume offset; TUI header metrics for all shapes. One test rewritten (read_file_stops_at_its_byte_budget_and_names_the_resume_offset replaces the head/tail assertion). Runtime/session assertions updated for the header.

Measurements

Gate Result
tool_dispatch A/B (pinned core, 10 pairs, median) 43.7 → 41.4 µs
first candidate before e441b17 44.7 → 46.4 µs (+4 %, fixed)

Evidence: target/qq-perf/t3-2026-09-14/ (untracked).

Docs

  • docs/design/tools.md: § Built-In Tools updated; § Reading Files (new).
  • docs/plans/progress/tool-layer.md: T2 shipped, T3 in review + receipt.

Compatibility

  • PROTOCOL / DESCRIPTOR / schema versions: unchanged. Tool declaration hash changed (expected; asserted).
  • Model-visible: read_file output gains a header line and a line-number gutter. Old offset/limit calls keep working. Persisted transcripts replay as-is; the TUI falls back to counting body lines for headerless results.
  • Deviations from the plan noted in the ledger: if_changed_since compares the 12-hex short hash the header shows; ranges accept an open end ("400-"); per-tool ceiling remains the shared 128 KiB.

Follow-ups

  • T5 can take h: as an optional edit_file precondition. T11 replaces the image hint with an image block. T12's @ ranges reuse parse_ranges.

…fo (T3)

read_file's result now opens with `read <path> L<a>-<b>[,<c>-<d>]/<total>
h:<hash>` and numbers every line (`<n>\t<text>`, one gutter width per
call), so edit anchors and re-reads line up and a pruned stub keeps the
hash. New arguments: `ranges` (up to eight, merged and ascending,
separated by `--`), `mode=outline` (items from the per-language tables
that search already uses, nested by indentation, <= 400 rows),
`mode=info` (size, lines, hash, utf8, eol, perms, binary/mime), and
`if_changed_since=h:<hash>`, which answers `read <path> unchanged
h:<hash> lines=<n>` and still records the file for the staleness guard.

Bounds: 32 KiB model-facing default with the walk stopping on a whole
row and naming the resume offset instead of a head/tail cut; 2000-byte
line clip; the 4 MiB scan cap is unchanged. Typed failures:
invalid_ranges, invalid_offset, invalid_limit, invalid_if_changed_since,
range_out_of_bounds{last_line}, not_a_file, not_text, path_not_found,
path_escapes_workspace, outline_unsupported. Images answer info plus
hint=image_unsupported_by_model until T11.

TUI reads the row metric from the header (`L1-40 of 120 lines`,
`unchanged`, `3 items`, `info`) and hides it in the expanded body.
Prompt teaches the hash, ranges, and outline.
A 32 KiB String per read showed as +4% on tool_dispatch. Pinned A/B, 10
pairs: base 43.7 µs → cand 41.4 µs median.
cap_std::fs::PermissionsExt is cfg(unix); Windows reports ro/rw.
@lg2m
lg2m force-pushed the feat/tool-layer-t3-read-file branch from 7f2b242 to 181ae4f Compare September 14, 2026 16:53
@lg2m
lg2m merged commit eecc76b into main Sep 14, 2026
4 checks passed
@lg2m
lg2m deleted the feat/tool-layer-t3-read-file branch September 14, 2026 17:06
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