feat(tools): Add btw_tool_files_patch for diff-style file editing#192
Merged
Conversation
Adds a new files tool that applies structured patch envelopes (apply_patch wire format) to one or more files atomically. Models can produce edits without a prior read because hunks are anchored by context lines rather than hashlines. The parser rejects pure-insert hunks (no context or delete line) so every edit has a concrete anchor; use *** Add File for new files. Fixes #190
`seq(search_start, n_file - seq_len_val + 1L)` produced a descending range when the match sequence was longer than the file, causing an out-of-bounds read and a confusing "missing value where TRUE/FALSE needed" error. Guard with `search_start <= last_start` so the not-found path runs cleanly. Adds regression tests for shorter-than- hunk and empty files.
R CMD check --as-cran with error_on='warning' rejects any non-ASCII in R source. Replace em dashes with -- and the moved-arrow with -> so the file is portable.
Add a display block to btw_tool_files_patch_impl that shows the input patch in a diff fence followed by the operations summary, and hides the raw tool call.
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.
Closes #190
Summary
Adds a new files tool,
btw_tool_files_patch(), that applies a structured diff-style patch envelope to make coordinated changes across multiple files in a single call. One envelope can add, update, delete, and rename files; every operation is validated (parser, path safety, filesystem preconditions, hunk matching) before any file is written, so a partial failure leaves the working tree untouched.Key design choices:
*** Add Filefor new files.type = "context"|"delete"|"insert") rather than the spec's separate context/deletes/inserts decomposition; both matching and applying become one-pass operations.BtwToolResultrather thanBtwFileDiffToolResultbecause a patch can touch multiple files, and the single-file diff viewer shape doesn't map cleanly.Verification
Tests cover the parser, syntax/filesystem validators, hunk matcher, applier (add/update/delete/move + atomic failure), and tool-integration snapshots. 69 tests pass via
testthat::test_file("tests/testthat/test-tool-files-patch.R").