agents-tools
Shared Rust implementations of agent tools for Math & Magic projects.
Filesystem apply_diff
The default filesystem feature provides apply_diff(root, patch). It parses a
complete patch envelope, supports multiple file blocks and multiple update
sections, validates paths beneath the supplied root, and performs atomic file
replacement on Linux, macOS, and Windows.
*** Begin Patch
*** Add File: src/new.rs
+pub fn new() {}
*** Update File: src/old.rs
@@ pub fn old() {
- before();
+ after();
*** Update File: src/name.rs
*** Move to: src/renamed.rs
*** Delete File: src/unused.rs
*** End Patch
Each logical file is planned completely in memory before it is committed. A
failure in one file leaves that file unchanged and does not prevent independent
files in the same patch from being applied. The returned ApplyDiffReport
contains both committed changes and per-file failures.
Overlapping paths are serialized within the process. Before every commit, the implementation verifies that the file still matches the snapshot used to build the update. Complete contents are written and synchronized before an atomic replacement, so readers see either the previous file or the complete new file.
In-memory text API
apply_diff_to_text applies a complete update body, including multiple anchored
sections, to a string. apply_diff_to_text_with_mode also exposes create mode.
use ;
let updated = apply_diff_to_text?;
assert_eq!;
let created = apply_diff_to_text_with_mode?;
assert_eq!;
# Ok::
Consumers that only need in-memory updates can omit the filesystem dependency set:
= { = "0.1", = false }
License
Licensed under the Apache License, Version 2.0.