use super::path::PathAccess;
use crate::{Tool, Workspace};
use anyhow::Result;
use async_trait::async_trait;
use serde_json::json;
use std::path::Path;
#[cfg(test)]
use std::path::PathBuf;
pub struct EditTool {
access: PathAccess,
}
impl EditTool {
#[must_use]
pub(crate) const fn new(access: PathAccess) -> Self {
Self { access }
}
#[must_use]
pub fn confined() -> Self {
Self::new(PathAccess::Workspace)
}
}
#[async_trait]
impl Tool for EditTool {
fn name(&self) -> &'static str {
"edit"
}
fn description(&self) -> String {
let path_policy = crate::prompt::load_prompt(match self.access {
PathAccess::Unrestricted => "tool/edit_paths_admin.md",
PathAccess::Workspace | PathAccess::Allowlisted => "tool/edit_paths_workspace.md",
});
crate::tools::tool_reference::standing(
self.name(),
&crate::prompt::substitute(
&crate::prompt::load_prompt("tool/edit.md"),
&[("{{path_policy}}", &path_policy)],
),
)
}
fn parameters_schema(&self) -> serde_json::Value {
super::tool_params_schema(
&json!({
"path": {
"type": "string",
"description": "Path to the file."
},
"old_string": {
"type": "string",
"description": "Omit or leave empty to create; otherwise the text to replace."
},
"new_string": {
"type": "string",
"description": "Content when creating, or the replacement text (empty deletes)."
},
"multiple": {
"type": "boolean",
"description": "Replace every occurrence of `old_string` (default: false).",
"default": false
}
}),
&["path", "new_string"],
)
}
async fn execute(&self, ws: &Workspace, args: serde_json::Value) -> Result<String> {
let path = super::get_str(&args, "path")?.to_string();
let new_string = super::get_str(&args, "new_string")?;
let old_string = super::get_opt_str(&args, "old_string");
match old_string {
None | Some("") => self.execute_write(ws, &path, new_string).await,
Some(old) => {
let multiple = super::get_bool(&args, "multiple", false)?;
self.execute_edit(ws, &path, old, new_string, multiple)
.await
}
}
}
}
fn not_found_error(path: &str, detail: &str) -> anyhow::Error {
anyhow::anyhow!(
"not-found: cannot edit {path}: old_string not found in file{detail} \
— hint: re-read the file and copy old_string exactly from its current contents"
)
}
impl EditTool {
async fn execute_write(&self, ws: &Workspace, path: &str, new_string: &str) -> Result<String> {
let resolved_target =
super::path::resolve_write_target(ws.as_path(), path, true, self.access).await?;
if tokio::fs::try_exists(&resolved_target)
.await
.map_err(|e| anyhow::anyhow!("Cannot verify whether {path} exists: {e}"))?
{
anyhow::bail!(
"File already exists: {path}. Use `old_string` to edit it instead of overwriting."
);
}
tokio::fs::write(&resolved_target, new_string)
.await
.map_err(|e| anyhow::anyhow!("io: cannot write {path}: failed to write file: {e} — hint: check that the parent directory exists and is writable"))?;
update_search_index_after_write(ws, &resolved_target);
Ok(format!("Written {} bytes to {path}", new_string.len()))
}
async fn execute_edit(
&self,
ws: &Workspace,
path: &str,
old_string: &str,
new_string: &str,
multiple: bool,
) -> Result<String> {
if old_string == new_string {
anyhow::bail!("old_string equals new_string — no change needed");
}
let resolved_target =
super::path::resolve_write_target(ws.as_path(), path, false, self.access).await?;
let use_ws_matching = is_ws_insensitive_extension(path);
match tokio::fs::metadata(&resolved_target).await {
Ok(meta) => {
super::check_size_within(&meta, super::MAX_FILE_SIZE_BYTES, "File too large")?;
}
Err(e) => anyhow::bail!("Cannot access file {path}: {e}"),
}
let content = match tokio::fs::read_to_string(&resolved_target).await {
Ok(c) => c,
Err(e) => {
anyhow::bail!(
"io: cannot edit {path}: failed to read file: {e} — hint: verify the file exists and contains valid UTF-8 text"
);
}
};
let plan = plan_edits(
path,
&content,
old_string,
new_string,
multiple,
use_ws_matching,
)?;
let edits = &plan.edits;
let mut new_content = String::with_capacity(content.len());
let mut cursor = 0;
for edit in edits {
new_content.push_str(&content[cursor..edit.span.start]);
new_content.push_str(&edit.replacement);
cursor = edit.span.end;
}
new_content.push_str(&content[cursor..]);
if new_content == content && !plan.whitespace_fallback {
anyhow::bail!(
"old_string equals new_string once line endings are accounted for — no change needed"
);
}
tokio::fs::write(&resolved_target, &new_content)
.await
.map_err(|e| anyhow::anyhow!("io: cannot edit {path}: failed to write file: {e} — hint: check disk space and file permissions"))?;
update_search_index_after_write(ws, &resolved_target);
Ok(format!(
"Edited {path}: replaced {} occurrence{} ({} bytes)",
edits.len(),
if edits.len() == 1 { "" } else { "s" },
new_content.len()
))
}
}
struct Edit {
span: std::ops::Range<usize>,
replacement: String,
}
impl Edit {
fn new(
content: &str,
span: std::ops::Range<usize>,
new_string: &str,
prevailing: Option<LineEnding>,
) -> Self {
let replacement = match local_ending(content, span.start).or(prevailing) {
Some(ending) => {
let adapted = ending.apply(new_string);
if adapted.starts_with("\r\n") && content[..span.start].ends_with('\r') {
adapted[1..].to_string()
} else {
adapted
}
}
None => new_string.to_string(),
};
Self { span, replacement }
}
}
struct Plan {
edits: Vec<Edit>,
whitespace_fallback: bool,
}
fn plan_edits(
path: &str,
content: &str,
old_string: &str,
new_string: &str,
multiple: bool,
use_ws_matching: bool,
) -> Result<Plan> {
let exact = exact_spans(content, old_string);
if !exact.is_empty() {
return Ok(Plan {
edits: resolve(exact, content, new_string, multiple)?,
whitespace_fallback: false,
});
}
let by_ending = line_ending_spans(content, old_string);
if !by_ending.is_empty() {
return Ok(Plan {
edits: resolve(by_ending, content, new_string, multiple)?,
whitespace_fallback: false,
});
}
if !multiple && use_ws_matching {
return match find_ws_insensitive(content, old_string)? {
Some(span) => Ok(Plan {
edits: resolve(vec![span], content, new_string, multiple)?,
whitespace_fallback: true,
}),
None => Err(not_found_error(
path,
" (whitespace-insensitive matching tried)",
)),
};
}
if multiple {
if use_ws_matching && find_ws_insensitive(content, old_string).is_ok_and(|s| s.is_some()) {
return Err(not_found_error(
path,
" (whitespace differs; try without multiple=true)",
));
}
return Err(not_found_error(path, " (multiple=true mode)"));
}
Err(not_found_error(
path,
" (exact match required apart from line endings)",
))
}
fn resolve(
spans: Vec<std::ops::Range<usize>>,
content: &str,
new_string: &str,
multiple: bool,
) -> Result<Vec<Edit>> {
let spans = if multiple {
spans
} else {
match spans.as_slice() {
[span] => vec![span.clone()],
spans => anyhow::bail!(
"old_string matches {} times; must match exactly once (or pass multiple=true to replace all)",
spans.len()
),
}
};
let mut previous_end = 0;
let spans: Vec<_> = spans
.into_iter()
.map(|span| {
let widened = span.start > previous_end
&& content[..span.start].ends_with('\r')
&& content[span.start..].starts_with('\n');
previous_end = span.end;
let start = if widened { span.start - 1 } else { span.start };
start..span.end
})
.collect();
let prevailing = prevailing_ending(content);
Ok(spans
.into_iter()
.map(|span| Edit::new(content, span, new_string, prevailing))
.collect())
}
fn exact_spans(content: &str, old_string: &str) -> Vec<std::ops::Range<usize>> {
content
.match_indices(old_string)
.map(|(start, matched)| start..start + matched.len())
.collect()
}
fn line_ending_spans(content: &str, old_string: &str) -> Vec<std::ops::Range<usize>> {
if !content.contains("\r\n") && !old_string.contains("\r\n") {
return Vec::new();
}
let view = LfView::new(content);
let needle = LfView::new(old_string).text;
view.text
.match_indices(&needle)
.map(|(start, matched)| view.raw_span(start, start + matched.len()))
.collect()
}
#[derive(Clone, Copy, Debug)]
enum LineEnding {
Lf,
CrLf,
}
impl LineEnding {
fn apply(self, text: &str) -> String {
let lf_only = text.replace("\r\n", "\n");
match self {
Self::Lf => lf_only,
Self::CrLf => lf_only.replace('\n', "\r\n"),
}
}
}
fn ending_of(content: &str, newline: usize) -> LineEnding {
if newline > 0 && content.as_bytes()[newline - 1] == b'\r' {
LineEnding::CrLf
} else {
LineEnding::Lf
}
}
fn local_ending(content: &str, at: usize) -> Option<LineEnding> {
content[at..]
.find('\n')
.map(|offset| ending_of(content, at + offset))
}
fn prevailing_ending(content: &str) -> Option<LineEnding> {
let bytes = content.as_bytes();
let (mut crlf, mut lf) = (0usize, 0usize);
let mut first = None;
for (i, byte) in bytes.iter().enumerate() {
if *byte != b'\n' {
continue;
}
let ending = ending_of(content, i);
first.get_or_insert(ending);
match ending {
LineEnding::CrLf => crlf += 1,
LineEnding::Lf => lf += 1,
}
}
match crlf.cmp(&lf) {
std::cmp::Ordering::Greater => Some(LineEnding::CrLf),
std::cmp::Ordering::Less => Some(LineEnding::Lf),
std::cmp::Ordering::Equal => first,
}
}
struct LfView {
text: String,
removed_cr: Vec<usize>,
}
impl LfView {
fn new(content: &str) -> Self {
let mut text = String::with_capacity(content.len());
let mut removed_cr = Vec::new();
let mut chars = content.chars().peekable();
while let Some(ch) = chars.next() {
if ch == '\r' && chars.peek() == Some(&'\n') {
removed_cr.push(text.len());
continue;
}
text.push(ch);
}
Self { text, removed_cr }
}
fn raw_span(&self, start: usize, end: usize) -> std::ops::Range<usize> {
let removed_before = |at: usize| self.removed_cr.partition_point(|&cr| cr < at);
start + removed_before(start)..end + removed_before(end)
}
}
fn update_search_index_after_write(ws: &Workspace, file_path: &std::path::Path) {
let ws_root =
std::fs::canonicalize(ws.as_path()).unwrap_or_else(|_| ws.as_path().to_path_buf());
if !crate::util::is_within(file_path, &ws_root) {
return;
}
let Some(entry) = crate::workspace_index::get_engine_by_name(&ws.name) else {
return;
};
if let Ok(mut guard) = entry.picker.write()
&& let Some(ref mut picker) = *guard
&& picker.handle_create_or_modify(file_path).is_none()
{
tracing::warn!(
workspace = ws.name,
path = %file_path.display(),
"Search index capacity exhausted after file write — \
background rescan needed"
);
}
}
const WS_INSENSITIVE_EXTENSIONS: &[&str] = &[
"rs", "js", "jsx", "ts", "tsx", "c", "h", "cpp", "hpp", "cc", "cxx", "java", "kt", "kts", "go",
"swift", "dart", "cs", "zig", "scala",
];
fn is_ws_insensitive_extension(path: &str) -> bool {
let ext = Path::new(path)
.extension()
.and_then(|e| e.to_str())
.unwrap_or("");
WS_INSENSITIVE_EXTENSIONS.contains(&ext.to_ascii_lowercase().as_str())
}
#[derive(Debug, Clone)]
struct Segment {
norm_range: std::ops::Range<usize>,
orig_range: std::ops::Range<usize>,
}
fn normalize_ws(s: &str) -> (String, Vec<Segment>) {
let mut normalized = String::new();
let mut segments = Vec::new();
let mut chars = s.char_indices().peekable();
while let Some((i, ch)) = chars.next() {
let norm_start = normalized.len();
let orig_start = i;
let mut orig_end = i;
match ch {
'"' | '\'' | '`' => {
normalized.push(ch);
while let Some((j, next_ch)) = chars.next() {
normalized.push(next_ch);
orig_end = j.saturating_add(next_ch.len_utf8());
if next_ch == '\\' {
if let Some((k, esc_ch)) = chars.next() {
normalized.push(esc_ch);
orig_end = k.saturating_add(esc_ch.len_utf8());
}
} else if next_ch == ch {
break;
}
}
}
_ if ch.is_ascii_whitespace() => {
orig_end = i.saturating_add(ch.len_utf8());
normalized.push(' ');
while let Some(&(j, next_ch)) = chars.peek() {
if next_ch.is_ascii_whitespace() {
chars.next();
orig_end = j.saturating_add(next_ch.len_utf8());
} else {
break;
}
}
}
_ => {
normalized.push(ch);
orig_end = i.saturating_add(ch.len_utf8());
while let Some(&(j, next_ch)) = chars.peek() {
if next_ch.is_ascii_whitespace()
|| next_ch == '"'
|| next_ch == '\''
|| next_ch == '`'
{
break;
}
normalized.push(next_ch);
chars.next();
orig_end = j.saturating_add(next_ch.len_utf8());
}
}
}
let norm_end = normalized.len();
if norm_end > norm_start {
segments.push(Segment {
norm_range: norm_start..norm_end,
orig_range: orig_start..orig_end,
});
}
}
(normalized, segments)
}
fn segment_at(pos: usize, segments: &[Segment]) -> Result<&Segment> {
segments
.iter()
.find(|seg| pos < seg.norm_range.end && pos >= seg.norm_range.start)
.ok_or_else(|| {
anyhow::anyhow!(
"segment_at: position {pos} not found in {len} segments",
len = segments.len()
)
})
}
fn map_norm_span(
norm_start: usize,
norm_end: usize,
segments: &[Segment],
) -> Result<std::ops::Range<usize>> {
let seg = segment_at(norm_start, segments)?;
let orig_start = if seg.orig_range.len() == seg.norm_range.len() {
seg.orig_range
.start
.saturating_add(norm_start.saturating_sub(seg.norm_range.start))
} else {
seg.orig_range.start
};
let end_seg = segment_at(norm_end.saturating_sub(1), segments)?;
let orig_end = if end_seg.orig_range.len() == end_seg.norm_range.len() {
end_seg
.orig_range
.start
.saturating_add(norm_end.saturating_sub(end_seg.norm_range.start))
} else {
end_seg.orig_range.end
};
Ok(orig_start..orig_end)
}
fn find_ws_insensitive(content: &str, old_string: &str) -> Result<Option<std::ops::Range<usize>>> {
if old_string.is_empty() || content.is_empty() {
return Ok(None);
}
let (norm_content, segments) = normalize_ws(content);
let (norm_old, _) = normalize_ws(old_string);
let Some(norm_pos) = norm_content.find(&norm_old) else {
return Ok(None);
};
let norm_end = norm_pos + norm_old.len();
let first_char_len = norm_old.chars().next().unwrap().len_utf8();
let search_start = norm_pos + first_char_len;
if norm_content[search_start..].find(&norm_old).is_some() {
anyhow::bail!(
"old_string matches multiple times after whitespace normalization; \
provide more surrounding context to disambiguate"
);
}
Ok(Some(map_norm_span(norm_pos, norm_end, &segments)?))
}
#[cfg(test)]
mod tests {
use super::*;
use crate::workspace::test_ws;
use tempfile::TempDir;
async fn with_temp_workspace<F, Fut>(files: &[(&str, &str)], test: F)
where
F: FnOnce(PathBuf) -> Fut,
Fut: std::future::Future<Output = ()>,
{
let dir = TempDir::new().unwrap();
for (filename, content) in files {
let full_path = dir.path().join(filename);
tokio::fs::create_dir_all(
full_path
.parent()
.expect("TempDir-joined path always has a parent"),
)
.await
.unwrap();
tokio::fs::write(&full_path, content).await.unwrap();
}
let path = dir.path().to_path_buf();
test(path).await;
}
#[test]
fn ws_insensitive_extensions() {
let cases: &[(&str, bool)] = &[
("main.rs", true),
("src/lib.rs", true),
("app.js", true),
("component.jsx", true),
("app.ts", true),
("component.tsx", true),
("main.c", true),
("main.h", true),
("main.cpp", true),
("main.hpp", true),
("main.cc", true),
("main.cxx", true),
("Main.java", true),
("Main.kt", true),
("Main.kts", true),
("main.go", true),
("main.swift", true),
("main.dart", true),
("Program.cs", true),
("main.zig", true),
("Main.scala", true),
("Main.rs", true),
("Main.RS", true),
("App.JS", true),
("Main.Rs", true),
("config.toml", false),
("config.json", false),
("config.yaml", false),
("config.yml", false),
("readme.md", false),
("Dockerfile", false),
("Makefile", false),
("main.py", false),
("main.rb", false),
("main.php", false),
("style.css", false),
("script.sh", false),
("docker-compose", false),
];
for &(path, expected_match) in cases {
assert_eq!(
is_ws_insensitive_extension(path),
expected_match,
"case: {path}"
);
}
}
const MATCH_CASES: &[(&str, &str, &str)] = &[
("let x = 5;", "let x = 5;", "let x = 5;"),
("let x = 5;", "let x = 5;", "let x = 5;"),
("let\tx\t=\t5;", "let x = 5;", "let\tx\t=\t5;"),
("let\nx\n=\n5;", "let x = 5;", "let\nx\n=\n5;"),
("\nlet x = 5;", "let x = 5;", "let x = 5;"),
(" \n\t ", " ", " \n\t "),
(" \n\t ", " \n ", " \n\t "),
("let x = \"\";", "let x = \"\";", "let x = \"\";"),
(
"let msg = \"hello world\"; let y = 5;",
"let msg = \"hello world\";",
"let msg = \"hello world\";",
),
(
"function hello() {\n return 42;\n}",
"hello() {",
"hello() {",
),
(
"pub fn foo<T>(x: T) -> T where T: Debug { x }",
"pub fn foo<T>(x: T) -> T where T: Debug { x }",
"pub fn foo<T>(x: T) -> T where T: Debug { x }",
),
(
"fn main() {\n let x = 5;\n let y = 10;\n x + y\n}",
"fn main() {\n let x = 5;\n let y = 10;\n x + y\n}",
"fn main() {\n let x = 5;\n let y = 10;\n x + y\n}",
),
(
"let c: char = 'x';",
"let c: char = 'x';",
"let c: char = 'x';",
),
(
"const fn = (x) => { return x * 2; };",
"const fn = (x) => { return x * 2; };",
"const fn = (x) => { return x * 2; };",
),
(
"let\t x\t= 5;\n\tlet\ty = 10;",
"let x = 5;\nlet y = 10;",
"let\t x\t= 5;\n\tlet\ty = 10;",
),
(
" let x = helper( arg1, arg2 );",
"helper( arg1, arg2 )",
"helper( arg1, arg2 )",
),
("a\nb", "a b", "a\nb"),
("x y ", "x y ", "x y "),
("abc123", "abc123", "abc123"),
(
"let name = \"café créme\";",
"let name = \"café créme\";",
"let name = \"café créme\";",
),
(
"let x = \"a\" + \"b\";",
"let x = \"a\" + \"b\";",
"let x = \"a\" + \"b\";",
),
(
"fn foo() {\n\tlet x = 1;\n}",
"fn foo() {\n\tlet x = 1;\n}",
"fn foo() {\n\tlet x = 1;\n}",
),
(
"const x = \"hello world\";",
"const x = \"hello world\";",
"const x = \"hello world\";",
),
("fn foo() {}", "fn foo()", "fn foo()"),
("fn foo() {}", "foo() {}", "foo() {}"),
("x + y", "x + y", "x + y"),
(" a + b", " a + b", " a + b"),
("a + b ", "a + b ", "a + b "),
(
"fn main() {}\nfn other() {}",
"fn main() {}",
"fn main() {}",
),
(
"fn main() {}\nfn other() {}",
"fn other() {}",
"fn other() {}",
),
(
"let x = \"hello \\\"world\\\" foo\";",
"let x = \"hello \\\"world\\\" foo\";",
"let x = \"hello \\\"world\\\" foo\";",
),
(
"let s1 = 'simple', s2 = \"double\", s3 = `template`;",
"let s1 = 'simple', s2 = \"double\", s3 = `template`;",
"let s1 = 'simple', s2 = \"double\", s3 = `template`;",
),
("hello world", "hello world", "hello world"),
("let x = 5;\n", "let x = 5;", "let x = 5;"),
];
const NOMATCH_CASES: &[(&str, &str)] = &[
("let x = \"hello world\";", "hello world"),
("let msg = \"a b\";", "let msg = \"a b\""),
(
"let x = \"hello \\\"world\\\" foo\";",
"let x = \"hello \\\"world\\\" foo\";",
),
("let x = 5;", "let y = 5;"),
("hello", ""),
("", "hello"),
("let msg = \"hello world\";", "let msg = \"hello world\";"),
("let msg = \"hello\nworld\";", "let msg = \"hello world\";"),
("let x = 'hello world';", "let x = 'hello world';"),
("let x = `hello world`;", "let x = `hello world`;"),
];
#[test]
fn ws_insensitive_should_match() {
for (content, old, expected) in MATCH_CASES {
let m = find_ws_insensitive(content, old)
.unwrap()
.unwrap_or_else(|| panic!("Expected match: content={content:?} old={old:?}"));
assert_eq!(&content[m], *expected, "content={content:?} old={old:?}");
}
}
#[test]
fn ws_insensitive_should_not_match() {
for (content, old) in NOMATCH_CASES {
assert!(
find_ws_insensitive(content, old).unwrap().is_none(),
"Expected no match: content={content:?} old={old:?}"
);
}
}
#[test]
fn ws_match_is_ambiguous() {
let cases: &[(&str, &str, &str, bool)] = &[
("repeated_pattern", "a b a b a b", "a b", true),
(
"two_lines_normalize_same",
"let x = 1;\nlet x = 1;",
"let x = 1;",
true,
),
("overlapping_repeated_tokens", "a a a", "a a", true),
("multibyte_2byte_latin", "ñ b ñ b", "ñ b", true),
("multibyte_3byte_cjk", "å— ç¬¦ å— ç¬¦", "å— ç¬¦", true),
("multibyte_4byte_emoji", "🚀 b 🚀 b", "🚀 b", true),
("single_match", "fn foo() {}", "fn foo() {}", false),
(
"two_functions_one_matches",
"fn foo() {}\nfn bar() {}",
"fn bar() {}",
false,
),
(
"appears_once_after_normalization",
"let x = 5;\nlet y = 10;\n x + y",
"let x = 5;",
false,
),
("empty_old_string", "anything", "", false),
("not_found", "fn foo() {}", "fn bar() {}", false),
(
"multibyte_2byte_latin_single",
"fn ñ foo() {}",
"fn ñ foo()",
false,
),
(
"multibyte_3byte_cjk_single",
"let å— = 1;",
"let å— = 1;",
false,
),
(
"multibyte_4byte_emoji_single",
"let 🚀 = 1;",
"let 🚀 = 1;",
false,
),
];
for &(name, content, old, expected_ambiguous) in cases {
let result = find_ws_insensitive(content, old);
if expected_ambiguous {
assert!(
result.is_err(),
"case: {name} — expected ambiguous error, content={content:?} old={old:?} got {result:?}"
);
let err = format!("{}", result.unwrap_err());
assert!(
err.contains("multiple times after whitespace normalization"),
"case: {name} — error should mention ambiguity, got: {err}"
);
} else {
assert!(
result.is_ok(),
"case: {name} — expected no ambiguity error, content={content:?} old={old:?} got {result:?}"
);
}
}
}
#[tokio::test]
async fn file_edit_multiple_replacements() {
let dir = TempDir::new().unwrap();
tokio::fs::write(dir.path().join("test.txt"), "a b a c a d")
.await
.unwrap();
let result = EditTool::confined()
.execute(
&test_ws(dir.path()),
json!({"path": "test.txt", "old_string": "b", "new_string": "x"}),
)
.await;
assert!(result.is_ok(), "edit should succeed: {result:?}");
let result = result.unwrap();
assert!(result.contains("replaced 1 occurrence"));
assert_eq!(
tokio::fs::read_to_string(dir.path().join("test.txt"))
.await
.unwrap(),
"a x a c a d"
);
let result = EditTool::confined()
.execute(
&test_ws(dir.path()),
json!({"path": "test.txt", "old_string": "a", "new_string": "y", "multiple": true}),
)
.await;
assert!(result.is_ok(), "multiple edit should succeed: {result:?}");
let result = result.unwrap();
assert!(result.contains("replaced 3 occurrences"));
assert_eq!(
tokio::fs::read_to_string(dir.path().join("test.txt"))
.await
.unwrap(),
"y x y c y d"
);
let result = EditTool::confined()
.execute(
&test_ws(dir.path()),
json!({"path": "test.txt", "old_string": "z", "new_string": "w", "multiple": true}),
)
.await;
assert!(
result.is_err(),
"edit with no matches should fail: {result:?}"
);
let err = format!("{}", result.unwrap_err());
assert!(
err.contains("not-found: cannot edit test.txt: old_string not found in file"),
"canonical not-found shape: {err}"
);
tokio::fs::write(dir.path().join("test.txt"), "only one")
.await
.unwrap();
let result = EditTool::confined()
.execute(&Workspace::from_path(dir.path()), json!({"path": "test.txt", "old_string": "one", "new_string": "two", "multiple": true}))
.await;
assert!(
result.is_ok(),
"single match with multiple flag: {result:?}"
);
let result = result.unwrap();
assert!(result.contains("replaced 1 occurrence"));
assert_eq!(
tokio::fs::read_to_string(dir.path().join("test.txt"))
.await
.unwrap(),
"only two"
);
}
#[tokio::test]
async fn file_edit_match_operations() {
let dir = TempDir::new().unwrap();
tokio::fs::write(dir.path().join("test.txt"), "hello world")
.await
.unwrap();
let result = EditTool::confined()
.execute(
&test_ws(dir.path()),
json!({"path": "test.txt", "old_string": "hello", "new_string": "goodbye"}),
)
.await;
assert!(result.is_ok(), "edit should succeed: {result:?}");
let result = result.unwrap();
assert!(result.contains("replaced 1 occurrence"));
assert_eq!(
tokio::fs::read_to_string(dir.path().join("test.txt"))
.await
.unwrap(),
"goodbye world"
);
let result = EditTool::confined().execute(&Workspace::from_path(dir.path()), json!({"path": "test.txt", "old_string": "nonexistent", "new_string": "replacement"})).await;
assert!(
result.is_err(),
"edit with nonexistent string should fail: {result:?}"
);
let err = format!("{}", result.unwrap_err());
assert!(
err.contains("not-found: cannot edit test.txt: old_string not found in file"),
"canonical not-found shape: {err}"
);
tokio::fs::write(dir.path().join("test.txt"), "aaa bbb aaa")
.await
.unwrap();
let result = EditTool::confined()
.execute(
&test_ws(dir.path()),
json!({"path": "test.txt", "old_string": "aaa", "new_string": "ccc"}),
)
.await;
assert!(result.is_err(), "multiple matches should fail: {result:?}");
let err = format!("{}", result.unwrap_err());
assert!(err.contains("matches 2 times"));
assert_eq!(
tokio::fs::read_to_string(dir.path().join("test.txt"))
.await
.unwrap(),
"aaa bbb aaa"
);
}
#[tokio::test]
async fn file_edit_delete_via_empty_new_string() {
with_temp_workspace(&[("test.txt", "keep remove keep")], |dir| async move {
let result = EditTool::confined()
.execute(
&test_ws(&dir),
json!({"path": "test.txt", "old_string": " remove", "new_string": ""}),
)
.await;
assert!(
result.is_ok(),
"delete edit should succeed: {:?}",
result.as_ref().unwrap_err()
);
let content = tokio::fs::read_to_string(dir.join("test.txt"))
.await
.unwrap();
assert_eq!(content, "keep keep");
})
.await;
}
#[tokio::test]
async fn edit_write_mode_creates_parent_dirs() {
let dir = TempDir::new().unwrap();
let result = EditTool::confined()
.execute(
&test_ws(dir.path()),
json!({"path": "a/b/c/deep.txt", "new_string": "deep"}),
)
.await;
assert!(result.is_ok(), "write with parent dirs: {result:?}");
let content = tokio::fs::read_to_string(dir.path().join("a/b/c/deep.txt"))
.await
.unwrap();
assert_eq!(content, "deep");
for (name, old_string) in [
("an omitted old_string", None),
("an empty old_string", Some("")),
] {
let dir = TempDir::new().unwrap();
let mut args = json!({"path": "out.txt", "new_string": "written!"});
if let Some(old) = old_string {
args["old_string"] = json!(old);
}
let result = EditTool::confined()
.execute(&test_ws(dir.path()), args)
.await;
assert!(result.is_ok(), "{name} should write: {result:?}");
assert!(
result.unwrap().contains("8 bytes"),
"{name} should report the written size"
);
assert_eq!(
tokio::fs::read_to_string(dir.path().join("out.txt"))
.await
.unwrap(),
"written!",
"{name}"
);
}
}
#[tokio::test]
async fn file_edit_blocks_dangerous_paths() {
with_temp_workspace(&[], |dir| async move {
let result = EditTool::confined()
.execute(
&test_ws(&dir),
json!({"path": "../../etc/passwd", "old_string": "root", "new_string": "x"}),
)
.await;
assert!(result.is_err(), "traversal should be blocked: {result:?}");
let err = format!("{}", result.unwrap_err());
assert!(
err.contains("forbidden: cannot write to ../../etc/passwd"),
"canonical forbidden shape: {err}"
);
let result = EditTool::confined()
.execute(
&test_ws(&dir),
json!({"path": "/etc/passwd", "old_string": "root", "new_string": "x"}),
)
.await;
assert!(
result.is_err(),
"absolute path should be blocked: {result:?}"
);
let err = format!("{}", result.unwrap_err());
assert!(
err.contains("forbidden: cannot write to /etc/passwd"),
"canonical forbidden shape: {err}"
);
})
.await;
}
#[tokio::test]
async fn file_edit_normalizes_relative_path() {
with_temp_workspace(
&[("workspace/nested/target.txt", "hello world")],
|root| async move {
let workspace = root.join("workspace");
let result = EditTool::confined()
.execute(
&test_ws(&workspace), json!({"path": "nested/target.txt", "old_string": "world", "new_string": "mahbot"}),
)
.await;
assert!(result.is_ok(), "relative path edit: {result:?}");
let content = tokio::fs::read_to_string(workspace.join("nested/target.txt")).await.unwrap();
assert_eq!(content, "hello mahbot");
let workspace = tokio::fs::canonicalize(&workspace).await.unwrap();
let absolute_edit = workspace.join("nested/target.txt");
let result = EditTool::confined()
.execute(
&test_ws(&workspace),
json!({
"path": absolute_edit.to_string_lossy().to_string(),
"old_string": "mahbot",
"new_string": "absolute",
}),
)
.await;
assert!(
result.is_ok(),
"editing via an absolute workspace path: {result:?}"
);
assert_eq!(
tokio::fs::read_to_string(&absolute_edit).await.unwrap(),
"hello absolute"
);
let absolute_write = workspace.join("written.txt");
let result = EditTool::confined()
.execute(
&test_ws(&workspace),
json!({
"path": absolute_write.to_string_lossy().to_string(),
"new_string": "written",
}),
)
.await;
assert!(
result.is_ok(),
"writing via an absolute workspace path: {result:?}"
);
assert_eq!(
tokio::fs::read_to_string(&absolute_write).await.unwrap(),
"written"
);
},
)
.await;
}
#[cfg(unix)]
#[tokio::test]
async fn file_edit_blocks_symlink_target_file() {
use std::os::unix::fs::symlink;
let root = TempDir::new().unwrap();
let workspace = root.path().join("workspace");
let outside = root.path().join("outside");
tokio::fs::create_dir_all(&workspace).await.unwrap();
tokio::fs::create_dir_all(&outside).await.unwrap();
tokio::fs::write(outside.join("target.txt"), "original")
.await
.unwrap();
symlink(outside.join("target.txt"), workspace.join("linked.txt")).unwrap();
let result = EditTool::confined()
.execute(
&test_ws(&workspace),
json!({
"path": "linked.txt",
"old_string": "original",
"new_string": "hacked"
}),
)
.await;
assert!(
result.is_err(),
"editing through symlink must be blocked: {result:?}"
);
let err = format!("{}", result.unwrap_err());
assert!(
err.contains("symlink"),
"error should mention symlink, got: {err}"
);
let content = tokio::fs::read_to_string(outside.join("target.txt"))
.await
.unwrap();
assert_eq!(content, "original", "original file must not be modified");
}
#[tokio::test]
async fn ws_ambiguous_rejects_multiple_matches() {
with_temp_workspace(
&[("lib.rs", "let x = 1;\nlet x = 1;\nlet y = 2;\n")],
|dir| async move {
let result = EditTool::confined()
.execute(
&test_ws(&dir),
json!({
"path": "lib.rs",
"old_string": "let x = 1;", "new_string": "let x = 42;"
}),
)
.await;
assert!(result.is_err(), "WS-ambiguous edit should fail: {result:?}");
let err = format!("{}", result.unwrap_err());
assert!(
err.contains("multiple times after whitespace normalization"),
"Error should mention whitespace normalization ambiguity, got: {err}"
);
assert!(
err.contains("surrounding context"),
"Error should suggest adding surrounding context, got: {err}"
);
let content = tokio::fs::read_to_string(dir.join("lib.rs")).await.unwrap();
assert_eq!(content, "let x = 1;\nlet x = 1;\nlet y = 2;\n");
},
)
.await;
}
async fn file_bytes(dir: &Path, name: &str) -> Vec<u8> {
tokio::fs::read(dir.join(name))
.await
.expect("read file bytes")
}
async fn read_view(ws: &Workspace, path: &str) -> String {
let out = crate::tools::ReadTool::general()
.execute(ws, json!({"path": path}))
.await
.expect("read should succeed");
out.lines()
.skip(1) .map(|line| line.split_once(": ").expect("numbered line").1)
.collect::<Vec<_>>()
.join("\n")
}
#[tokio::test]
async fn read_then_edit_round_trip_for_every_file_type_and_ending() {
for name in [
"notes.md",
"notes.txt",
"script.py",
"config.json",
"lib.rs",
] {
for ending in ["\n", "\r\n"] {
let dir = TempDir::new().unwrap();
let initial = format!("alpha{ending}beta{ending}gamma{ending}");
tokio::fs::write(dir.path().join(name), &initial)
.await
.unwrap();
let ws = test_ws(dir.path());
assert_eq!(
read_view(&ws, name).await,
"alpha\nbeta\ngamma",
"{name} ({ending:?}) is read with LF"
);
let result = EditTool::confined()
.execute(
&ws,
json!({
"path": name,
"old_string": "beta\ngamma", "new_string": "beta\nGAMMA\nadded",
}),
)
.await;
assert!(
result.is_ok(),
"{name} ({ending:?}): edit built from a read must land: {result:?}"
);
assert_eq!(
file_bytes(dir.path(), name).await,
format!("alpha{ending}beta{ending}GAMMA{ending}added{ending}").as_bytes(),
"{name} ({ending:?}): the change landed, the untouched line kept \
its bytes and the file kept its endings"
);
}
}
}
struct LineEndingStep {
old: &'static str,
new: &'static str,
multiple: bool,
outcome: Result<&'static [u8], &'static [&'static str]>,
note: &'static str,
}
struct LineEndingCase {
name: &'static str,
file: &'static str,
initial: &'static [u8],
read_view: Option<&'static str>,
steps: &'static [LineEndingStep],
}
const LINE_ENDING_CASES: &[LineEndingCase] = &[
LineEndingCase {
name: "multiple_matches_every_occurrence_of_a_read_spelled_pattern",
file: "data.txt",
initial: b"item = 1\r\nkeep = 0\r\nitem = 1\r\nkeep = 0\r\n",
read_view: Some("item = 1\nkeep = 0\nitem = 1\nkeep = 0"),
steps: &[LineEndingStep {
old: "item = 1\nkeep = 0",
new: "item = 2\nkeep = 1",
multiple: true,
outcome: Ok(b"item = 2\r\nkeep = 1\r\nitem = 2\r\nkeep = 1\r\n"),
note: "replaced 2 occurrences",
}],
},
LineEndingCase {
name: "a_cr_spelled_old_string_still_matches",
file: "zoom.txt",
initial: b"one\r\ntwo\r\nthree\r\n",
read_view: None,
steps: &[LineEndingStep {
old: "one\r\ntwo",
new: "one\ntwo\nhalf",
multiple: false,
outcome: Ok(b"one\r\ntwo\r\nhalf\r\nthree\r\n"),
note: "",
}],
},
LineEndingCase {
name: "unix_endings_are_preserved_and_no_foreign_ending_is_introduced",
file: "unix.txt",
initial: b"a\nb\nc\n",
read_view: None,
steps: &[
LineEndingStep {
old: "b\nc",
new: "b\nC",
multiple: false,
outcome: Ok(b"a\nb\nC\n"),
note: "",
},
LineEndingStep {
old: "a\nb",
new: "a\r\nB",
multiple: false,
outcome: Ok(b"a\nB\nC\n"),
note: "",
},
],
},
LineEndingCase {
name: "a_mixed_file_keeps_its_mixture_outside_the_edit",
file: "mixed.txt",
initial: b"head\nmid\r\ntail\n",
read_view: None,
steps: &[LineEndingStep {
old: "mid",
new: "mid\nmiddle",
multiple: false,
outcome: Ok(b"head\nmid\r\nmiddle\r\ntail\n"),
note: "",
}],
},
LineEndingCase {
name: "a_lone_carriage_return_is_content",
file: "classic.txt",
initial: b"a\rb\r\nc\r\n",
read_view: None,
steps: &[
LineEndingStep {
old: "a\rb",
new: "a\rB",
multiple: false,
outcome: Ok(b"a\rB\r\nc\r\n"),
note: "",
},
LineEndingStep {
old: "c",
new: "c\rd\ne",
multiple: false,
outcome: Ok(b"a\rB\r\nc\rd\r\ne\r\n"),
note: "",
},
LineEndingStep {
old: "a\nb",
new: "x",
multiple: false,
outcome: Err(&[]),
note: "",
},
],
},
LineEndingCase {
name: "respelling_the_files_own_endings_is_a_no_op",
file: "lf_spelling.txt",
initial: b"a\r\nb\r\n",
read_view: None,
steps: &[LineEndingStep {
old: "a\r\nb",
new: "a\nb",
multiple: false,
outcome: Err(&["no change needed"]),
note: "",
}],
},
LineEndingCase {
name: "respelling_the_files_own_endings_is_a_no_op_the_other_way",
file: "crlf_spelling.txt",
initial: b"a\r\nb\r\n",
read_view: None,
steps: &[LineEndingStep {
old: "a\nb",
new: "a\r\nb",
multiple: false,
outcome: Err(&["no change needed"]),
note: "",
}],
},
LineEndingCase {
name: "several_matches_are_ambiguous_in_a_text_file",
file: "dup.txt",
initial: b"x = 1\r\nkeep\r\nx = 1\r\nkeep\r\n",
read_view: None,
steps: &[LineEndingStep {
old: "x = 1\nkeep",
new: "x = 2\nkeep",
multiple: false,
outcome: Err(&["matches 2 times", "must match exactly once"]),
note: "",
}],
},
LineEndingCase {
name: "several_matches_are_ambiguous_in_a_code_file",
file: "dup.rs",
initial: b"x = 1\r\nkeep\r\nx = 1\r\nkeep\r\n",
read_view: None,
steps: &[LineEndingStep {
old: "x = 1\nkeep",
new: "x = 2\nkeep",
multiple: false,
outcome: Err(&["matches 2 times", "must match exactly once"]),
note: "",
}],
},
LineEndingCase {
name: "a_cr_spelled_old_string_matches_an_lf_file",
file: "unix.txt",
initial: b"a\nb\n",
read_view: None,
steps: &[LineEndingStep {
old: "a\r\nb",
new: "a\nB",
multiple: false,
outcome: Ok(b"a\nB\n"),
note: "",
}],
},
LineEndingCase {
name: "written_text_takes_the_ending_of_the_line_it_lands_in",
file: "mid.txt",
initial: b"one two\r\nthree\r\n",
read_view: None,
steps: &[LineEndingStep {
old: "ne",
new: "NE\nline",
multiple: false,
outcome: Ok(b"oNE\r\nline two\r\nthree\r\n"),
note: "",
}],
},
LineEndingCase {
name: "written_text_over_an_unterminated_last_line",
file: "tail.txt",
initial: b"a\r\nb",
read_view: None,
steps: &[LineEndingStep {
old: "b",
new: "b\nx",
multiple: false,
outcome: Ok(b"a\r\nb\r\nx"),
note: "",
}],
},
LineEndingCase {
name: "a_file_with_no_ending_gets_the_text_as_written",
file: "oneline",
initial: b"abc",
read_view: None,
steps: &[LineEndingStep {
old: "b",
new: "x\ny\r\nz",
multiple: false,
outcome: Ok(b"ax\ny\r\nzc"),
note: "",
}],
},
LineEndingCase {
name: "a_pattern_starting_on_the_lf_of_a_pair_stays_on_crlf",
file: "pair.txt",
initial: b"alpha\r\nbeta\r\ngamma\r\n",
read_view: None,
steps: &[LineEndingStep {
old: "\nbeta",
new: "\nBETA\ninserted",
multiple: false,
outcome: Ok(b"alpha\r\nBETA\r\ninserted\r\ngamma\r\n"),
note: "",
}],
},
LineEndingCase {
name: "deleting_a_line_break_takes_the_whole_ending",
file: "windows.txt",
initial: b"a\r\nb",
read_view: None,
steps: &[LineEndingStep {
old: "\n",
new: " ",
multiple: false,
outcome: Ok(b"a b"),
note: "",
}],
},
];
#[tokio::test]
async fn line_ending_cases() {
for case in LINE_ENDING_CASES {
let dir = TempDir::new().unwrap();
tokio::fs::write(dir.path().join(case.file), case.initial)
.await
.unwrap();
let ws = test_ws(dir.path());
if let Some(reading) = case.read_view {
assert_eq!(
read_view(&ws, case.file).await,
reading,
"{}: the reading hides the endings",
case.name
);
}
let mut bytes = case.initial.to_vec();
for step in case.steps {
let result = EditTool::confined()
.execute(
&ws,
json!({
"path": case.file,
"old_string": step.old,
"new_string": step.new,
"multiple": step.multiple,
}),
)
.await;
match step.outcome {
Ok(after) => {
let note = result
.unwrap_or_else(|e| panic!("{}: the edit must land: {e}", case.name));
if !step.note.is_empty() {
assert!(
note.contains(step.note),
"{}: the note does not say {:?}: {note}",
case.name,
step.note
);
}
bytes = after.to_vec();
}
Err(words) => {
let err = format!(
"{}",
result.err().unwrap_or_else(|| panic!(
"{}: the edit must be refused",
case.name
))
);
for word in words {
assert!(
err.contains(word),
"{}: the refusal does not say {word:?}: {err}",
case.name
);
}
}
}
assert_eq!(
file_bytes(dir.path(), case.file).await,
bytes,
"{}: the file's bytes after the edit",
case.name
);
}
}
}
#[tokio::test]
async fn line_ending_tolerance_is_not_whitespace_tolerance() {
let dir = TempDir::new().unwrap();
tokio::fs::write(dir.path().join("readme.txt"), b"a b\r\nc\r\n")
.await
.unwrap();
let result = EditTool::confined()
.execute(
&test_ws(dir.path()),
json!({"path": "readme.txt", "old_string": "a b", "new_string": "a+b"}),
)
.await;
assert!(
result.is_err(),
"a .txt file still gets no whitespace tolerance: {result:?}"
);
}
#[tokio::test]
async fn whitespace_fallback_in_a_crlf_code_file_keeps_the_ending() {
let dir = TempDir::new().unwrap();
tokio::fs::write(
dir.path().join("lib.rs"),
b"let x = 1;\r\nlet y = 2;\r\n",
)
.await
.unwrap();
let result = EditTool::confined()
.execute(
&test_ws(dir.path()),
json!({
"path": "lib.rs",
"old_string": "let x = 1;\nlet y = 2;", "new_string": "let x = 42;\nlet y = 2;",
}),
)
.await;
assert!(result.is_ok(), "whitespace fallback: {result:?}");
assert_eq!(
file_bytes(dir.path(), "lib.rs").await,
b"let x = 42;\r\nlet y = 2;\r\n".as_slice()
);
}
}