Skip to content

feat(tools): edit_file v2 batches with a matching cascade; write_file flags (T5) - #37

Merged
lg2m merged 3 commits into
mainfrom
feat/tool-layer-t5-edit-v2
Sep 14, 2026
Merged

lg2m merged 3 commits into
mainfrom
feat/tool-layer-t5-edit-v2

Conversation

@lg2m

@lg2m lg2m commented Sep 14, 2026

Copy link
Copy Markdown
Collaborator

T5: edit_file v2 — batches, a matching cascade, honest partial applies; write_file flags

Spec: docs/plans/tool-layer.md § D3 Linear: ENG-775 Follows: #36 (T4)

What changed

edit_file (model-visible — schema changed)

{"edits":[
  {"path":"src/a.rs","old":"fn one() {}","new":"fn one() { 1 }"},
  {"path":"src/a.rs","insert_after":"fn one() { 1 }","new":"\nfn two() {}"},
  {"path":"src/b.rs","old":"let y = 2;\n","new":"let y = 20;\n","if_hash":"h:3fa9c2d1e07b"}
 ],"fuzzy":true,"dry_run":false}
  • Up to 32 edits across files in one call; each is old/new, insert_before/new, or insert_after/new. Later edits see earlier results. The whole batch applies or none does.
  • Two phases. Plan in memory without the lock (resolve, prove currency, match, apply in order); then under apply_lock re-hash every file and temp+rename in path order. A rename failure midway → partial_apply{applied=[…] failed=…}, applied files' hashes recorded so a retry of the rest doesn't trip the guard.
  • if_hash = the 12-hex h: from read_file's header proves currency without a recorded read in this session — the proof an @-mentioned file (T12) needs.
  • Matching cascade (tools/matching.rs): exact → line_trimmed → whitespace_normalized → indent_flexible (re-indents new by the delta) → block_anchor (≥ 3 lines, anchors + 0.7 middle similarity + ≤ 25 % size delta, disproportionate guard, skipped > 20k lines, 2 s soft deadline). First strategy with exactly one match wins; several at any level → ambiguous{count, lines}. Fuzzy never applies to replace_all. Every non-exact match is named: via=indent_flexible.
  • not_found carries closest L<n> distance=0.08 + a 3-line excerpt so the retry needs no read. A replacement landing inside text a prior edit wrote → conflicting_edits{a, b}.
  • Result: edit ok files=2 edits=3 then <path> h:<new12> L96 -1+1 | L140 -0+6 via=indent_flexible per file. dry_run returns the same shape and writes nothing.
  • The UI payload is the unified diff of what changed on disk (all files), not an echo of the arguments as before.

write_file

  • create_only (→ exists), if_hash, missing parents created up to 8 deep (never through ..), header write <path> created|replaced bytes= lines= h:.
  • hint=use_edit_file when > 80 % of lines are kept (line-hash LCS, both < 4000 lines).

Plumbing

  • ToolOutput.file_state: Option<_> → file_states: Vec<_> (a batch records every file); flows through RuntimeEvent::ToolCallFinished → finish_tool_call → record_session_file per entry.
  • Approval preview renders a batch grouped by path with anchors as context. TUI edit rows: Edit a.rs +1 files; edit/write headers hidden from the body.
  • Prompt teaches batches, if_hash, via=, dry_run.
  • Root request for T6's tree-sitter promotion filed in root.md ahead of that slice.

How verified

  • cargo test --workspace: 1376 passed / 0 failed; fmt and clippy -D warnings clean.
  • 8 cascade unit tests (one per strategy incl. ambiguity at the fuzzy tier, disproportionate guard, dissimilar middle, closest-line hint, no-trailing-newline spans, re-indent).
  • 5 new tool tests: multi-file batch with anchors and per-edit line report; whole-batch failure table (11 codes) asserting nothing is written; dry_run + if_hash without a read + fuzzy=false; partial_apply via a read-only directory (unix) asserting the first file landed, the second didn't, and hashes; write_file hint. write_file test extended (nested parents, too_deep, .., create_only, if_hash proof/mismatch).
  • 25 fixtures converted, 6 result assertions updated, schema hash updated.

Measurements

Gate Result
edit_batch (new) 32 exact edits, 1 MiB, dry-run 10.3 ms
… apply (incl. temp+rename) 10.4 ms
… 32 fuzzy-drifted edits (all three line tiers exercised) 31.7 ms
single exact edit, 1 MiB 1.6 ms
tool_dispatch A/B (15 pairs interleaved, idle core, median) 48.27 → 48.28 µs

An earlier 4 % tool_dispatch gap measured on a loaded core reproduced in the same-binary A/A control — noise. Evidence: target/qq-perf/t5-2026-09-14/.

Docs

  • docs/design/tools.md: § Built-In Tools, § Edit Semantics (rewritten: two phases, cascade table, result grammar, failures, write_file), § Optimistic Concurrency step 2/4.
  • Ledger: T4 shipped, T5 in review + receipt.

Compatibility

  • edit_file argument schema changed (path/old_string/new_string → edits[]). Persisted transcripts replay as-is; the TUI still renders legacy single-path arguments. A model on an old cached schema gets invalid arguments with the field names.
  • Result shapes changed: edit ok … / write <path> … headers replace the prose summaries.
  • ToolCallDisplay unchanged (no protocol bump): Diff { path, diff } keeps the first path and carries a multi-file unified diff. The changes pane attributes a batch's counts to that first path until a per-file variant is worth protocol 20 — noted as open in the ledger.
  • Tool declaration hash changed (expected; asserted).

Deviations from the plan (in the ledger)

  • line_trimmed trims trailing whitespace only so indent_flexible is the one strategy that moves depth.
  • conflicting_edits is defined as a replacement inside a prior edit's output; inserting next to it is allowed.

Follow-ups

  • Per-file ToolCallDisplay variant when the changes pane needs it (protocol 20).
  • T12 passes if_hash for @ files; T13 measures the cascade's real hit rate per strategy.

… flags (T5)

edit_file takes edits=[{path, old|insert_before|insert_after, new,
replace_all, if_hash}] (<= 32), fuzzy (default on), dry_run. Phase 1 (no
lock) resolves and proves currency per file (recorded read, or if_hash
equal to the 12-hex header hash), applies every edit in memory in order
so later edits see earlier results, and refuses a replacement that lands
inside text an earlier edit wrote (conflicting_edits). Phase 2 (under
apply_lock) re-hashes, then temp+renames each file in path order; a
failure midway reports partial_apply{applied, failed} and records the
hashes that landed.

tools/matching.rs is the cascade: exact -> line_trimmed ->
whitespace_normalized -> indent_flexible (re-indents new by the delta)
-> block_anchor (>= 3 lines; anchors + 0.7 middle similarity + 25% size
delta, disproportionate guard, skipped above 20k lines). First strategy
with exactly one match wins; several at any level is ambiguous; fuzzy
never applies to replace_all. not_found carries the closest line and a
3-line excerpt so the retry needs no read.

Result: `edit ok files=N edits=M` then `<path> h:<new> L96 -1+1 |
L140 -0+6 via=indent_flexible` per file; the unified diff of what
changed on disk rides in ui_payload (multi-file, first path as subject).
write_file: create_only (exists), if_hash, missing parents created up
to eight deep, and hint=use_edit_file when > 80% of lines are kept;
header `write <path> created|replaced bytes= lines= h:`.

ToolOutput.file_state becomes file_states: a batch records every file.
The approval preview renders a batch grouped by path with anchors as
context. TUI edit rows show the first path plus a file count.
@lg2m
lg2m merged commit 95fef1b into main Sep 14, 2026
4 checks passed
@lg2m
lg2m deleted the feat/tool-layer-t5-edit-v2 branch September 14, 2026 19:37
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