Repository navigation
feat(tools): edit_file v2 batches with a matching cascade; write_file flags (T5) - #37
Merged
Merged
Conversation
… 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.
This was referenced Sep 14, 2026
Merged
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
T5:
edit_filev2 — batches, a matching cascade, honest partial applies;write_fileflagsSpec: 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}old/new,insert_before/new, orinsert_after/new. Later edits see earlier results. The whole batch applies or none does.apply_lockre-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-hexh:fromread_file's header proves currency without a recorded read in this session — the proof an@-mentioned file (T12) needs.tools/matching.rs):exact→line_trimmed→whitespace_normalized→indent_flexible(re-indentsnewby 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 toreplace_all. Every non-exact match is named:via=indent_flexible.not_foundcarriesclosest 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}.edit ok files=2 edits=3then<path> h:<new12> L96 -1+1 | L140 -0+6 via=indent_flexibleper file.dry_runreturns the same shape and writes nothing.write_filecreate_only(→exists),if_hash, missing parents created up to 8 deep (never through..), headerwrite <path> created|replaced bytes= lines= h:.hint=use_edit_filewhen > 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 throughRuntimeEvent::ToolCallFinished→finish_tool_call→record_session_fileper entry.Edit a.rs +1 files;edit/writeheaders hidden from the body.if_hash,via=,dry_run.tree-sitterpromotion filed inroot.mdahead of that slice.How verified
cargo test --workspace: 1376 passed / 0 failed; fmt andclippy -D warningsclean.dry_run+if_hashwithout a read +fuzzy=false;partial_applyvia a read-only directory (unix) asserting the first file landed, the second didn't, and hashes;write_filehint.write_filetest extended (nested parents,too_deep,..,create_only,if_hashproof/mismatch).Measurements
edit_batch(new) 32 exact edits, 1 MiB, dry-runtool_dispatchA/B (15 pairs interleaved, idle core, median)An earlier 4 %
tool_dispatchgap 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.Compatibility
edit_fileargument 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 getsinvalid argumentswith the field names.edit ok …/write <path> …headers replace the prose summaries.ToolCallDisplayunchanged (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.Deviations from the plan (in the ledger)
line_trimmedtrims trailing whitespace only soindent_flexibleis the one strategy that moves depth.conflicting_editsis defined as a replacement inside a prior edit's output; inserting next to it is allowed.Follow-ups
ToolCallDisplayvariant when the changes pane needs it (protocol 20).if_hashfor@files; T13 measures the cascade's real hit rate per strategy.