1use async_trait::async_trait;
2use bamboo_agent_core::{Tool, ToolError, ToolExecutionContext, ToolResult};
3use serde::Deserialize;
4use serde_json::json;
5use std::collections::HashSet;
6use std::path::Path;
7
8use super::read_tracker::ReadState;
9use super::{content_diagnostics, file_change, read_tracker};
10
11const MAX_PATCH_BYTES: usize = 256 * 1024;
12const MAX_PATCH_BLOCKS: usize = 128;
13const MAX_PATCH_BLOCK_BYTES: usize = 64 * 1024;
14const MAX_SAFE_EDIT_SCOPE_LINES: usize = 120;
15const MAX_SAFE_REPLACE_ALL_OCCURRENCES: usize = 8;
16const MAX_SAFE_REPLACE_ALL_SCOPE_LINES: usize = 80;
17const MIN_REPLACE_ALL_NON_WHITESPACE_CHARS: usize = 2;
18const MIN_REPLACE_ALL_LINES: usize = 1;
19
20#[derive(Debug, Deserialize)]
21struct EditArgs {
22 file_path: String,
23 #[serde(default)]
24 old_string: Option<String>,
25 #[serde(default)]
26 new_string: Option<String>,
27 #[serde(default)]
28 replace_all: Option<bool>,
29 #[serde(default)]
30 patch: Option<String>,
31 #[serde(default)]
32 line_number: Option<usize>,
33}
34
35pub struct EditTool;
36
37#[derive(Debug, Clone)]
38struct ReplacementCandidate {
39 start: usize,
40 matched_len: usize,
41 replacement: String,
42 start_line: usize,
43 end_line: usize,
44}
45
46#[derive(Debug, Clone)]
47struct AppliedEdit {
48 updated: String,
49 replacements: usize,
50}
51
52impl EditTool {
53 pub fn new() -> Self {
54 Self
55 }
56
57 fn to_lf(value: &str) -> String {
58 value.replace("\r\n", "\n")
59 }
60
61 fn to_crlf(value: &str) -> String {
62 Self::to_lf(value).replace('\n', "\r\n")
63 }
64
65 fn has_meaningful_optional_text(value: Option<&str>) -> bool {
66 value.is_some_and(|text| !text.is_empty())
67 }
68
69 fn line_starts(content: &str) -> Vec<usize> {
70 let mut starts = vec![0usize];
71 for (idx, byte) in content.bytes().enumerate() {
72 if byte == b'\n' && idx + 1 < content.len() {
73 starts.push(idx + 1);
74 }
75 }
76 starts
77 }
78
79 fn line_for_offset(line_starts: &[usize], offset: usize) -> usize {
80 line_starts.partition_point(|line_start| *line_start <= offset)
81 }
82
83 fn has_candidate_containing_line(
84 candidates: &[ReplacementCandidate],
85 line_number: usize,
86 ) -> bool {
87 candidates.iter().any(|candidate| {
88 candidate.start_line <= line_number && line_number <= candidate.end_line
89 })
90 }
91
92 fn validate_replace_all_scope(old_string: &str) -> Result<(), ToolError> {
93 let non_whitespace_chars = old_string.chars().filter(|ch| !ch.is_whitespace()).count();
94 if non_whitespace_chars < MIN_REPLACE_ALL_NON_WHITESPACE_CHARS {
95 return Err(ToolError::InvalidArguments(format!(
96 "replace_all requires old_string to contain at least {} non-whitespace characters",
97 MIN_REPLACE_ALL_NON_WHITESPACE_CHARS
98 )));
99 }
100
101 let non_empty_lines = old_string
102 .lines()
103 .filter(|line| !line.trim().is_empty())
104 .count();
105 if non_empty_lines < MIN_REPLACE_ALL_LINES {
106 return Err(ToolError::InvalidArguments(
107 "replace_all requires old_string to contain at least one non-empty line"
108 .to_string(),
109 ));
110 }
111
112 Ok(())
113 }
114
115 fn ensure_safe_scope(
116 replace_all: bool,
117 replacements: usize,
118 touched_lines: usize,
119 ) -> Result<(), ToolError> {
120 if replace_all && replacements > MAX_SAFE_REPLACE_ALL_OCCURRENCES {
121 return Err(ToolError::Execution(format!(
122 "replace_all would modify {} occurrences, exceeding the safe limit of {}; provide a more specific old_string or use patch mode",
123 replacements, MAX_SAFE_REPLACE_ALL_OCCURRENCES
124 )));
125 }
126
127 let max_scope = if replace_all {
128 MAX_SAFE_REPLACE_ALL_SCOPE_LINES
129 } else {
130 MAX_SAFE_EDIT_SCOPE_LINES
131 };
132
133 if touched_lines > max_scope {
134 let guidance = if replace_all {
135 "provide a more specific old_string or use patch mode"
136 } else {
137 "split the change into smaller patches or use Write for intentional full-file rewrites"
138 };
139 return Err(ToolError::Execution(format!(
140 "Edit would touch {} diff lines, exceeding the safe limit of {}; {}",
141 touched_lines, max_scope, guidance
142 )));
143 }
144
145 Ok(())
146 }
147
148 fn replacement_variants(
149 content: &str,
150 old_text: &str,
151 new_text: &str,
152 ) -> Vec<(String, String)> {
153 let mut variants: Vec<(String, String)> = Vec::new();
154 let mut seen_variants: HashSet<(String, String)> = HashSet::new();
155 let mut push_variant = |search: String, replace: String| {
156 if seen_variants.insert((search.clone(), replace.clone())) {
157 variants.push((search, replace));
158 }
159 };
160
161 push_variant(old_text.to_string(), new_text.to_string());
162 push_variant(Self::to_lf(old_text), Self::to_lf(new_text));
163 if content.contains("\r\n") {
164 push_variant(Self::to_crlf(old_text), Self::to_crlf(new_text));
165 }
166
167 variants
168 }
169
170 fn collect_candidates(
171 content: &str,
172 old_text: &str,
173 new_text: &str,
174 ) -> Vec<ReplacementCandidate> {
175 let variants = Self::replacement_variants(content, old_text, new_text);
176 let line_starts = Self::line_starts(content);
177 let mut out: Vec<ReplacementCandidate> = Vec::new();
178 let mut seen_matches: HashSet<(usize, usize, String)> = HashSet::new();
179
180 for (search, replacement) in variants {
181 if search.is_empty() {
182 continue;
183 }
184 for (start, _) in content.match_indices(&search) {
185 let matched_len = search.len();
186 let end = start + matched_len - 1;
187 let candidate = ReplacementCandidate {
188 start,
189 matched_len,
190 replacement: replacement.clone(),
191 start_line: Self::line_for_offset(&line_starts, start),
192 end_line: Self::line_for_offset(&line_starts, end),
193 };
194 if seen_matches.insert((start, matched_len, candidate.replacement.clone())) {
195 out.push(candidate);
196 }
197 }
198 }
199
200 out.sort_by_key(|candidate| candidate.start);
201 out
202 }
203
204 fn candidate_line_summary(candidates: &[ReplacementCandidate]) -> String {
205 let mut lines = candidates
206 .iter()
207 .map(|candidate| candidate.start_line.to_string())
208 .collect::<Vec<_>>();
209 lines.sort();
210 lines.dedup();
211 lines.join(", ")
212 }
213
214 fn choose_candidate_with_line_hint(
215 candidates: &[ReplacementCandidate],
216 line_number: usize,
217 ) -> Option<ReplacementCandidate> {
218 let containing = candidates
219 .iter()
220 .filter(|candidate| {
221 candidate.start_line <= line_number && line_number <= candidate.end_line
222 })
223 .cloned()
224 .collect::<Vec<_>>();
225
226 if containing.is_empty() {
227 return None;
228 }
229
230 let mut best: Option<ReplacementCandidate> = None;
231 let mut best_distance = usize::MAX;
232 let mut tie = false;
233
234 for candidate in containing {
235 let distance = candidate.start_line.abs_diff(line_number);
236 if distance < best_distance {
237 best_distance = distance;
238 best = Some(candidate);
239 tie = false;
240 } else if distance == best_distance {
241 tie = true;
242 }
243 }
244
245 if tie {
246 None
247 } else {
248 best
249 }
250 }
251
252 fn apply_single_replacement(
253 content: &str,
254 old_string: &str,
255 new_string: &str,
256 replace_all: bool,
257 line_number: Option<usize>,
258 ) -> Result<AppliedEdit, ToolError> {
259 if old_string == new_string {
260 return Err(ToolError::InvalidArguments(
261 "new_string must be different from old_string".to_string(),
262 ));
263 }
264 if old_string.is_empty() {
265 return Err(ToolError::InvalidArguments(
266 "old_string must be non-empty".to_string(),
267 ));
268 }
269
270 if let Some(line) = line_number {
271 if line == 0 {
272 return Err(ToolError::InvalidArguments(
273 "line_number must be >= 1".to_string(),
274 ));
275 }
276 if replace_all {
277 return Err(ToolError::InvalidArguments(
278 "line_number cannot be combined with replace_all=true".to_string(),
279 ));
280 }
281 }
282
283 let candidates = Self::collect_candidates(content, old_string, new_string);
284
285 if candidates.is_empty() {
286 return Err(ToolError::Execution(
287 "old_string not found in target file".to_string(),
288 ));
289 }
290
291 if !replace_all && candidates.len() != 1 && line_number.is_none() {
292 return Err(ToolError::Execution(format!(
293 "old_string matched {} times; provide a more specific old_string, set line_number, or use patch mode with additional context",
294 candidates.len()
295 )));
296 }
297
298 if replace_all {
299 Self::validate_replace_all_scope(old_string)?;
300 let variants = Self::replacement_variants(content, old_string, new_string);
301 for (search, replacement) in variants {
302 let matches = content.match_indices(&search).count();
303 if matches > 0 {
304 return Ok(AppliedEdit {
305 updated: content.replace(&search, &replacement),
306 replacements: matches,
307 });
308 }
309 }
310
311 return Ok(AppliedEdit {
312 updated: content.to_string(),
313 replacements: 0,
314 });
315 }
316
317 let chosen = if let Some(line) = line_number {
318 match Self::choose_candidate_with_line_hint(&candidates, line) {
319 Some(candidate) => candidate,
320 None if Self::has_candidate_containing_line(&candidates, line) => {
321 return Err(ToolError::Execution(format!(
322 "old_string matched {} times and line_number={} was not unique among candidates containing that line; candidate start lines: {}. Provide a more specific old_string or patch context",
323 candidates.len(),
324 line,
325 Self::candidate_line_summary(&candidates),
326 )));
327 }
328 None => {
329 return Err(ToolError::Execution(format!(
330 "line_number={} did not match any old_string candidate; candidate start lines: {}. Provide a line_number within the target match or use patch context",
331 line,
332 Self::candidate_line_summary(&candidates),
333 )));
334 }
335 }
336 } else {
337 candidates[0].clone()
338 };
339
340 let mut next = String::with_capacity(
341 content.len().saturating_sub(chosen.matched_len) + chosen.replacement.len(),
342 );
343 next.push_str(&content[..chosen.start]);
344 next.push_str(&chosen.replacement);
345 next.push_str(&content[chosen.start + chosen.matched_len..]);
346
347 Ok(AppliedEdit {
348 updated: next,
349 replacements: 1,
350 })
351 }
352
353 fn parse_patch_blocks(patch: &str) -> Result<Vec<(String, String)>, ToolError> {
354 const SEARCH: &str = "<<<<<<< SEARCH\n";
355 const SEP: &str = "\n=======\n";
356 const REPLACE: &str = "\n>>>>>>> REPLACE";
357
358 let normalized = patch.replace("\r\n", "\n");
359 if normalized.trim().is_empty() {
360 return Err(ToolError::InvalidArguments(
361 "patch must be non-empty".to_string(),
362 ));
363 }
364 if normalized.len() > MAX_PATCH_BYTES {
365 return Err(ToolError::InvalidArguments(format!(
366 "patch exceeds max size of {} bytes",
367 MAX_PATCH_BYTES
368 )));
369 }
370
371 let mut cursor = 0usize;
372 let mut blocks = Vec::new();
373 while let Some(start_rel) = normalized[cursor..].find(SEARCH) {
374 if blocks.len() >= MAX_PATCH_BLOCKS {
375 return Err(ToolError::InvalidArguments(format!(
376 "patch exceeds max block count of {}",
377 MAX_PATCH_BLOCKS
378 )));
379 }
380 let search_start = cursor + start_rel + SEARCH.len();
381 let sep_rel = normalized[search_start..].find(SEP).ok_or_else(|| {
382 ToolError::InvalidArguments("Malformed patch block: missing =======".to_string())
383 })?;
384 let sep_idx = search_start + sep_rel;
385 let replace_start = sep_idx + SEP.len();
386 let replace_rel = normalized[replace_start..].find(REPLACE).ok_or_else(|| {
387 ToolError::InvalidArguments(
388 "Malformed patch block: missing >>>>>>> REPLACE".to_string(),
389 )
390 })?;
391 let replace_idx = replace_start + replace_rel;
392
393 let old_block = normalized[search_start..sep_idx].to_string();
394 let new_block = normalized[replace_start..replace_idx].to_string();
395 if old_block.is_empty() {
396 return Err(ToolError::InvalidArguments(
397 "Patch SEARCH block must be non-empty".to_string(),
398 ));
399 }
400 if old_block.len() > MAX_PATCH_BLOCK_BYTES || new_block.len() > MAX_PATCH_BLOCK_BYTES {
401 return Err(ToolError::InvalidArguments(format!(
402 "Patch block exceeds max block size of {} bytes",
403 MAX_PATCH_BLOCK_BYTES
404 )));
405 }
406 blocks.push((old_block, new_block));
407
408 cursor = replace_idx + REPLACE.len();
409 if normalized[cursor..].starts_with('\n') {
410 cursor += 1;
411 }
412 }
413
414 if blocks.is_empty() {
415 return Err(ToolError::InvalidArguments(
416 "patch must contain at least one SEARCH/REPLACE block".to_string(),
417 ));
418 }
419
420 Ok(blocks)
421 }
422
423 fn apply_patch_mode(
424 content: &str,
425 patch: &str,
426 line_number: Option<usize>,
427 ) -> Result<AppliedEdit, ToolError> {
428 if let Some(line) = line_number {
429 if line == 0 {
430 return Err(ToolError::InvalidArguments(
431 "line_number must be >= 1".to_string(),
432 ));
433 }
434 }
435 let blocks = Self::parse_patch_blocks(patch)?;
436 let mut updated = content.to_string();
437 let mut replacements = 0usize;
438
439 for (idx, (old_block, new_block)) in blocks.iter().enumerate() {
440 let candidates = Self::collect_candidates(&updated, old_block, new_block);
441
442 if candidates.is_empty() {
443 return Err(ToolError::Execution(format!(
444 "Patch block {} SEARCH content not found in target file",
445 idx + 1
446 )));
447 }
448
449 let chosen = if candidates.len() == 1 {
450 candidates[0].clone()
451 } else if let Some(line) = line_number {
452 match Self::choose_candidate_with_line_hint(&candidates, line) {
453 Some(candidate) => candidate,
454 None if Self::has_candidate_containing_line(&candidates, line) => {
455 return Err(ToolError::Execution(format!(
456 "Patch block {} SEARCH content matched {} times and line_number={} was not unique among candidates containing that line; candidate start lines: {}. Add more context to make it unique",
457 idx + 1,
458 candidates.len(),
459 line,
460 Self::candidate_line_summary(&candidates),
461 )));
462 }
463 None => {
464 return Err(ToolError::Execution(format!(
465 "Patch block {} line_number={} did not match any SEARCH candidate; candidate start lines: {}. Add more context or use a line within the target block",
466 idx + 1,
467 line,
468 Self::candidate_line_summary(&candidates),
469 )));
470 }
471 }
472 } else {
473 return Err(ToolError::Execution(format!(
474 "Patch block {} SEARCH content matched {} times; set line_number or add more context to make it unique",
475 idx + 1,
476 candidates.len()
477 )));
478 };
479
480 let mut next = String::with_capacity(
481 updated.len().saturating_sub(chosen.matched_len) + chosen.replacement.len(),
482 );
483 next.push_str(&updated[..chosen.start]);
484 next.push_str(&chosen.replacement);
485 next.push_str(&updated[chosen.start + chosen.matched_len..]);
486 updated = next;
487 replacements += 1;
488 }
489
490 Ok(AppliedEdit {
491 updated,
492 replacements,
493 })
494 }
495}
496
497impl Default for EditTool {
498 fn default() -> Self {
499 Self::new()
500 }
501}
502
503#[async_trait]
504impl Tool for EditTool {
505 fn name(&self) -> &str {
506 "Edit"
507 }
508
509 fn description(&self) -> &str {
510 "Edit existing files via exact replacements or SEARCH/REPLACE patch blocks. IMPORTANT: call Read first in this session or Edit will fail."
511 }
512
513 fn parameters_schema(&self) -> serde_json::Value {
514 json!({
515 "type": "object",
516 "properties": {
517 "file_path": {
518 "type": "string",
519 "description": "The absolute path to the file to modify"
520 },
521 "old_string": {
522 "type": "string",
523 "description": "Legacy mode only: exact text to replace. Do not send with patch mode."
524 },
525 "new_string": {
526 "type": "string",
527 "description": "Legacy mode only: replacement text. Do not send with patch mode."
528 },
529 "replace_all": {
530 "type": "boolean",
531 "default": false,
532 "description": "Legacy mode only: replace all occurrences. Do not send with patch mode."
533 },
534 "patch": {
535 "type": "string",
536 "description": "Patch mode: one or more blocks using <<<<<<< SEARCH / ======= / >>>>>>> REPLACE. Preferred mode. Do not combine with non-empty old_string/new_string or replace_all=true."
537 },
538 "line_number": {
539 "type": "integer",
540 "minimum": 1,
541 "description": "Optional 1-based line hint to disambiguate duplicate matches"
542 }
543 },
544 "required": ["file_path"],
545 "additionalProperties": false
546 })
547 }
548
549 async fn execute(&self, args: serde_json::Value) -> Result<ToolResult, ToolError> {
550 self.execute_with_context(args, ToolExecutionContext::none("Edit"))
551 .await
552 }
553
554 async fn execute_with_context(
555 &self,
556 args: serde_json::Value,
557 ctx: ToolExecutionContext<'_>,
558 ) -> Result<ToolResult, ToolError> {
559 let parsed: EditArgs = serde_json::from_value(args)
560 .map_err(|e| ToolError::InvalidArguments(format!("Invalid Edit args: {}", e)))?;
561
562 let file_path = parsed.file_path.trim();
563 let path = Path::new(file_path);
564 if !path.is_absolute() {
565 return Err(ToolError::InvalidArguments(
566 "file_path must be an absolute path".to_string(),
567 ));
568 }
569
570 if let Some(session_id) = ctx.session_id {
571 match read_tracker::read_state(session_id, file_path).await {
572 ReadState::Unread => {
573 return Err(ToolError::Execution(
574 "Edit requires reading the target file first via Read".to_string(),
575 ));
576 }
577 ReadState::Stale => {
578 return Err(ToolError::Execution(
579 "Target file changed after last Read; call Read again before Edit"
580 .to_string(),
581 ));
582 }
583 ReadState::Fresh => {}
584 }
585 }
586
587 let content = tokio::fs::read_to_string(path)
588 .await
589 .map_err(|e| ToolError::Execution(format!("Failed to read file: {}", e)))?;
590
591 let patch = parsed
592 .patch
593 .as_deref()
594 .map(str::trim)
595 .filter(|value| !value.is_empty());
596 let old_string = parsed.old_string.as_deref();
597 let new_string = parsed.new_string.as_deref();
598
599 let requested_replace_all = parsed.replace_all.unwrap_or(false);
600 let line_number_hint = parsed.line_number;
601 let used_patch_mode = patch.is_some();
602
603 let AppliedEdit {
604 updated,
605 replacements,
606 } = if let Some(patch_text) = patch {
607 if Self::has_meaningful_optional_text(old_string)
608 || Self::has_meaningful_optional_text(new_string)
609 || requested_replace_all
610 {
611 return Err(ToolError::InvalidArguments(
612 "patch mode cannot be combined with old_string/new_string/replace_all"
613 .to_string(),
614 ));
615 }
616 Self::apply_patch_mode(&content, patch_text, parsed.line_number)?
617 } else {
618 let old = old_string.ok_or_else(|| {
619 ToolError::InvalidArguments(
620 "old_string is required unless patch mode is used".to_string(),
621 )
622 })?;
623 let new = new_string.ok_or_else(|| {
624 ToolError::InvalidArguments(
625 "new_string is required unless patch mode is used".to_string(),
626 )
627 })?;
628 Self::apply_single_replacement(
629 &content,
630 old,
631 new,
632 requested_replace_all,
633 parsed.line_number,
634 )?
635 };
636 let mode_label = if used_patch_mode { "patch" } else { "legacy" };
637 let touched_lines = file_change::touched_line_count(&content, &updated);
638
639 Self::ensure_safe_scope(requested_replace_all, replacements, touched_lines)?;
640
641 let checkpoint = file_change::create_checkpoint(path, Some(content.as_bytes())).await?;
642
643 file_change::atomic_write_text(path, &updated).await?;
644
645 let changed_bytes = updated.len().abs_diff(content.len());
646 let changed_lines = updated.lines().count().abs_diff(content.lines().count());
647
648 let mut payload = file_change::build_file_change_payload_value(
649 "Edit",
650 path,
651 format!(
652 "Edited file: {} (mode: {}, replacements: {})",
653 file_path, mode_label, replacements
654 ),
655 checkpoint,
656 &content,
657 &updated,
658 );
659 if let Some(obj) = payload.as_object_mut() {
660 obj.insert("edit_mode".to_string(), json!(mode_label));
661 obj.insert("replacements".to_string(), json!(replacements));
662 obj.insert(
663 "requested_replace_all".to_string(),
664 json!(requested_replace_all),
665 );
666 obj.insert("used_patch_mode".to_string(), json!(used_patch_mode));
667 obj.insert("line_number_hint".to_string(), json!(line_number_hint));
668 obj.insert("changed_bytes".to_string(), json!(changed_bytes));
669 obj.insert("changed_lines".to_string(), json!(changed_lines));
670 obj.insert("touched_lines".to_string(), json!(touched_lines));
671 }
672 content_diagnostics::attach_file_diagnostics(&mut payload, path, &updated);
673
674 Ok(ToolResult {
675 success: true,
676 result: payload.to_string(),
677 display_preference: Some("Default".to_string()),
678 images: Vec::new(),
679 })
680 }
681}
682
683#[cfg(test)]
684mod tests {
685 use super::*;
686 use crate::tools::ReadTool;
687 use serde_json::json;
688
689 #[tokio::test]
690 async fn edit_requires_unique_match_without_replace_all() {
691 let file = tempfile::NamedTempFile::new().unwrap();
692 tokio::fs::write(file.path(), "foo\nfoo\n").await.unwrap();
693
694 let tool = EditTool::new();
695 let result = tool
696 .execute(json!({
697 "file_path": file.path(),
698 "old_string": "foo",
699 "new_string": "bar"
700 }))
701 .await;
702
703 assert!(result.is_err());
704 }
705
706 #[tokio::test]
707 async fn edit_supports_replace_all() {
708 let file = tempfile::NamedTempFile::new().unwrap();
709 tokio::fs::write(file.path(), "foo\nfoo\n").await.unwrap();
710
711 let tool = EditTool::new();
712 let result = tool
713 .execute(json!({
714 "file_path": file.path(),
715 "old_string": "foo",
716 "new_string": "bar",
717 "replace_all": true
718 }))
719 .await
720 .unwrap();
721
722 assert!(result.success);
723 let updated = tokio::fs::read_to_string(file.path()).await.unwrap();
724 assert_eq!(updated, "bar\nbar\n");
725 }
726
727 #[tokio::test]
728 async fn edit_replace_all_does_not_reprocess_newly_inserted_matches() {
729 let file = tempfile::NamedTempFile::new().unwrap();
730 tokio::fs::write(file.path(), "a\n").await.unwrap();
731
732 let tool = EditTool::new();
733 let result = tool
734 .execute(json!({
735 "file_path": file.path(),
736 "old_string": "aa",
737 "new_string": "bb",
738 "replace_all": true
739 }))
740 .await;
741
742 assert!(matches!(
743 result,
744 Err(ToolError::Execution(_)) | Err(ToolError::InvalidArguments(_))
745 ));
746 }
747
748 #[tokio::test]
749 async fn edit_replace_all_rejects_excessive_occurrence_count() {
750 let file = tempfile::NamedTempFile::new().unwrap();
751 let content = (0..=MAX_SAFE_REPLACE_ALL_OCCURRENCES)
752 .map(|_| "foo")
753 .collect::<Vec<_>>()
754 .join("\n");
755 tokio::fs::write(file.path(), format!("{content}\n"))
756 .await
757 .unwrap();
758
759 let tool = EditTool::new();
760 let result = tool
761 .execute(json!({
762 "file_path": file.path(),
763 "old_string": "foo",
764 "new_string": "bar",
765 "replace_all": true
766 }))
767 .await;
768
769 assert!(
770 matches!(result, Err(ToolError::Execution(msg)) if msg.contains("replace_all would modify"))
771 );
772 }
773
774 #[tokio::test]
775 async fn edit_replace_all_rejects_too_short_old_string() {
776 let file = tempfile::NamedTempFile::new().unwrap();
777 tokio::fs::write(file.path(), "a\na\n").await.unwrap();
778
779 let tool = EditTool::new();
780 let result = tool
781 .execute(json!({
782 "file_path": file.path(),
783 "old_string": "a",
784 "new_string": "b",
785 "replace_all": true
786 }))
787 .await;
788
789 assert!(
790 matches!(result, Err(ToolError::InvalidArguments(msg)) if msg.contains("non-whitespace characters"))
791 );
792 }
793
794 #[tokio::test]
795 async fn edit_replace_all_rejects_whitespace_only_old_string() {
796 let file = tempfile::NamedTempFile::new().unwrap();
797 tokio::fs::write(file.path(), " \n \n").await.unwrap();
798
799 let tool = EditTool::new();
800 let result = tool
801 .execute(json!({
802 "file_path": file.path(),
803 "old_string": " ",
804 "new_string": "x",
805 "replace_all": true
806 }))
807 .await;
808
809 assert!(
810 matches!(result, Err(ToolError::InvalidArguments(msg)) if msg.contains("non-whitespace characters") || msg.contains("non-empty line"))
811 );
812 }
813
814 #[tokio::test]
815 async fn edit_requires_read_first_when_session_context_exists() {
816 let file = tempfile::NamedTempFile::new().unwrap();
817 tokio::fs::write(file.path(), "hello world\n")
818 .await
819 .unwrap();
820 let call_id = "call_1";
821
822 let edit_tool = EditTool::new();
823 let read_tool = ReadTool::new();
824
825 let denied = edit_tool
826 .execute_with_context(
827 json!({
828 "file_path": file.path(),
829 "old_string": "world",
830 "new_string": "rust"
831 }),
832 ToolExecutionContext {
833 session_id: Some("session_1"),
834 tool_call_id: call_id,
835 event_tx: None,
836 available_tool_schemas: None,
837 },
838 )
839 .await;
840 assert!(denied.is_err());
841
842 let _ = read_tool
843 .execute_with_context(
844 json!({"file_path": file.path()}),
845 ToolExecutionContext {
846 session_id: Some("session_1"),
847 tool_call_id: call_id,
848 event_tx: None,
849 available_tool_schemas: None,
850 },
851 )
852 .await
853 .unwrap();
854
855 let allowed = edit_tool
856 .execute_with_context(
857 json!({
858 "file_path": file.path(),
859 "old_string": "world",
860 "new_string": "rust"
861 }),
862 ToolExecutionContext {
863 session_id: Some("session_1"),
864 tool_call_id: call_id,
865 event_tx: None,
866 available_tool_schemas: None,
867 },
868 )
869 .await
870 .unwrap();
871
872 assert!(allowed.success);
873 }
874
875 #[tokio::test]
876 async fn edit_rejects_empty_old_string() {
877 let file = tempfile::NamedTempFile::new().unwrap();
878 tokio::fs::write(file.path(), "hello").await.unwrap();
879
880 let tool = EditTool::new();
881 let result = tool
882 .execute(json!({
883 "file_path": file.path(),
884 "old_string": "",
885 "new_string": "x",
886 "replace_all": true
887 }))
888 .await;
889
890 assert!(matches!(result, Err(ToolError::InvalidArguments(_))));
891 }
892
893 #[tokio::test]
894 async fn edit_legacy_mode_handles_crlf_when_old_string_uses_lf() {
895 let file = tempfile::NamedTempFile::new().unwrap();
896 tokio::fs::write(file.path(), "alpha\r\nbeta\r\n")
897 .await
898 .unwrap();
899
900 let tool = EditTool::new();
901 let result = tool
902 .execute(json!({
903 "file_path": file.path(),
904 "old_string": "alpha\nbeta\n",
905 "new_string": "gamma\ndelta\n"
906 }))
907 .await
908 .unwrap();
909
910 assert!(result.success);
911 let updated = tokio::fs::read_to_string(file.path()).await.unwrap();
912 assert_eq!(updated, "gamma\r\ndelta\r\n");
913 }
914
915 #[tokio::test]
916 async fn edit_legacy_mode_line_number_disambiguates_duplicates() {
917 let file = tempfile::NamedTempFile::new().unwrap();
918 tokio::fs::write(file.path(), "foo\nbar\nfoo\n")
919 .await
920 .unwrap();
921
922 let tool = EditTool::new();
923 let result = tool
924 .execute(json!({
925 "file_path": file.path(),
926 "old_string": "foo",
927 "new_string": "baz",
928 "line_number": 3
929 }))
930 .await
931 .unwrap();
932 assert!(result.success);
933
934 let updated = tokio::fs::read_to_string(file.path()).await.unwrap();
935 assert_eq!(updated, "foo\nbar\nbaz\n");
936 }
937
938 #[tokio::test]
939 async fn edit_legacy_mode_rejects_line_number_when_no_candidate_contains_it() {
940 let file = tempfile::NamedTempFile::new().unwrap();
941 tokio::fs::write(file.path(), "foo\nbar\nfoo\n")
942 .await
943 .unwrap();
944
945 let tool = EditTool::new();
946 let result = tool
947 .execute(json!({
948 "file_path": file.path(),
949 "old_string": "foo",
950 "new_string": "baz",
951 "line_number": 2
952 }))
953 .await;
954
955 assert!(
956 matches!(result, Err(ToolError::Execution(msg)) if msg.contains("did not match any old_string candidate"))
957 );
958 }
959
960 #[tokio::test]
961 async fn edit_legacy_mode_rejects_line_number_with_replace_all() {
962 let file = tempfile::NamedTempFile::new().unwrap();
963 tokio::fs::write(file.path(), "foo\nfoo\n").await.unwrap();
964
965 let tool = EditTool::new();
966 let result = tool
967 .execute(json!({
968 "file_path": file.path(),
969 "old_string": "foo",
970 "new_string": "bar",
971 "replace_all": true,
972 "line_number": 1
973 }))
974 .await;
975
976 assert!(
977 matches!(result, Err(ToolError::InvalidArguments(msg)) if msg.contains("line_number cannot be combined"))
978 );
979 }
980
981 #[tokio::test]
982 async fn edit_patch_mode_can_target_second_duplicate_with_context() {
983 let file = tempfile::NamedTempFile::new().unwrap();
984 tokio::fs::write(
985 file.path(),
986 "fn a() {\n let v = 1;\n}\n\nfn b() {\n let v = 1;\n}\n",
987 )
988 .await
989 .unwrap();
990
991 let tool = EditTool::new();
992 let result = tool
993 .execute(json!({
994 "file_path": file.path(),
995 "patch": "<<<<<<< SEARCH\nfn b() {\n let v = 1;\n}\n=======\nfn b() {\n let v = 2;\n}\n>>>>>>> REPLACE"
996 }))
997 .await
998 .unwrap();
999 assert!(result.success);
1000
1001 let updated = tokio::fs::read_to_string(file.path()).await.unwrap();
1002 assert!(updated.contains("fn a() {\n let v = 1;\n}"));
1003 assert!(updated.contains("fn b() {\n let v = 2;\n}"));
1004 }
1005
1006 #[tokio::test]
1007 async fn edit_patch_mode_handles_crlf_when_patch_uses_lf() {
1008 let file = tempfile::NamedTempFile::new().unwrap();
1009 tokio::fs::write(file.path(), "fn b() {\r\n let v = 1;\r\n}\r\n")
1010 .await
1011 .unwrap();
1012
1013 let tool = EditTool::new();
1014 let result = tool
1015 .execute(json!({
1016 "file_path": file.path(),
1017 "patch": "<<<<<<< SEARCH\nfn b() {\n let v = 1;\n}\n=======\nfn b() {\n let v = 2;\n}\n>>>>>>> REPLACE"
1018 }))
1019 .await
1020 .unwrap();
1021 assert!(result.success);
1022
1023 let updated = tokio::fs::read_to_string(file.path()).await.unwrap();
1024 assert_eq!(updated, "fn b() {\r\n let v = 2;\r\n}\r\n");
1025 }
1026
1027 #[tokio::test]
1028 async fn edit_patch_mode_line_number_disambiguates_duplicates() {
1029 let file = tempfile::NamedTempFile::new().unwrap();
1030 tokio::fs::write(file.path(), "x = 1;\nx = 1;\n")
1031 .await
1032 .unwrap();
1033
1034 let tool = EditTool::new();
1035 let result = tool
1036 .execute(json!({
1037 "file_path": file.path(),
1038 "line_number": 2,
1039 "patch": "<<<<<<< SEARCH\nx = 1;\n=======\nx = 2;\n>>>>>>> REPLACE"
1040 }))
1041 .await
1042 .unwrap();
1043 assert!(result.success);
1044
1045 let updated = tokio::fs::read_to_string(file.path()).await.unwrap();
1046 assert_eq!(updated, "x = 1;\nx = 2;\n");
1047 }
1048
1049 #[tokio::test]
1050 async fn edit_patch_mode_rejects_line_number_when_no_candidate_contains_it() {
1051 let file = tempfile::NamedTempFile::new().unwrap();
1052 tokio::fs::write(file.path(), "x = 1;\ny = 0;\nx = 1;\n")
1053 .await
1054 .unwrap();
1055
1056 let tool = EditTool::new();
1057 let result = tool
1058 .execute(json!({
1059 "file_path": file.path(),
1060 "line_number": 2,
1061 "patch": "<<<<<<< SEARCH\nx = 1;\n=======\nx = 2;\n>>>>>>> REPLACE"
1062 }))
1063 .await;
1064
1065 assert!(
1066 matches!(result, Err(ToolError::Execution(msg)) if msg.contains("did not match any SEARCH candidate"))
1067 );
1068 }
1069
1070 #[tokio::test]
1071 async fn edit_patch_mode_rejects_ambiguous_search_block() {
1072 let file = tempfile::NamedTempFile::new().unwrap();
1073 tokio::fs::write(file.path(), "x = 1;\nx = 1;\n")
1074 .await
1075 .unwrap();
1076
1077 let tool = EditTool::new();
1078 let result = tool
1079 .execute(json!({
1080 "file_path": file.path(),
1081 "patch": "<<<<<<< SEARCH\nx = 1;\n=======\nx = 2;\n>>>>>>> REPLACE"
1082 }))
1083 .await;
1084
1085 assert!(
1086 matches!(result, Err(ToolError::Execution(msg)) if msg.contains("matched 2 times"))
1087 );
1088 }
1089
1090 #[tokio::test]
1091 async fn edit_patch_mode_rejects_large_scope_edits() {
1092 let file = tempfile::NamedTempFile::new().unwrap();
1093 let old_block = (0..70)
1094 .map(|idx| format!("line {idx}"))
1095 .collect::<Vec<_>>()
1096 .join("\n");
1097 let new_block = (0..70)
1098 .map(|idx| format!("updated {idx}"))
1099 .collect::<Vec<_>>()
1100 .join("\n");
1101 tokio::fs::write(file.path(), format!("{old_block}\n"))
1102 .await
1103 .unwrap();
1104
1105 let patch = format!("<<<<<<< SEARCH\n{old_block}\n=======\n{new_block}\n>>>>>>> REPLACE");
1106
1107 let tool = EditTool::new();
1108 let result = tool
1109 .execute(json!({
1110 "file_path": file.path(),
1111 "patch": patch
1112 }))
1113 .await;
1114
1115 assert!(
1116 matches!(result, Err(ToolError::Execution(msg)) if msg.contains("exceeding the safe limit"))
1117 );
1118 }
1119
1120 #[tokio::test]
1121 async fn edit_rejects_mixed_patch_and_legacy_args() {
1122 let file = tempfile::NamedTempFile::new().unwrap();
1123 tokio::fs::write(file.path(), "hello").await.unwrap();
1124
1125 let tool = EditTool::new();
1126 let result = tool
1127 .execute(json!({
1128 "file_path": file.path(),
1129 "old_string": "hello",
1130 "new_string": "world",
1131 "patch": "<<<<<<< SEARCH\nhello\n=======\nworld\n>>>>>>> REPLACE"
1132 }))
1133 .await;
1134
1135 assert!(
1136 matches!(result, Err(ToolError::InvalidArguments(msg)) if msg.contains("cannot be combined"))
1137 );
1138 }
1139
1140 #[tokio::test]
1141 async fn edit_patch_mode_ignores_empty_legacy_placeholders() {
1142 let file = tempfile::NamedTempFile::new().unwrap();
1143 tokio::fs::write(file.path(), "hello").await.unwrap();
1144
1145 let tool = EditTool::new();
1146 let result = tool
1147 .execute(json!({
1148 "file_path": file.path(),
1149 "old_string": "",
1150 "new_string": "",
1151 "replace_all": false,
1152 "patch": "<<<<<<< SEARCH\nhello\n=======\nworld\n>>>>>>> REPLACE"
1153 }))
1154 .await
1155 .unwrap();
1156
1157 assert!(result.success);
1158 let updated = tokio::fs::read_to_string(file.path()).await.unwrap();
1159 assert_eq!(updated, "world");
1160 }
1161
1162 #[tokio::test]
1163 async fn edit_patch_rejects_oversized_patch_payload() {
1164 let file = tempfile::NamedTempFile::new().unwrap();
1165 tokio::fs::write(file.path(), "hello world").await.unwrap();
1166 let huge = "a".repeat(MAX_PATCH_BYTES + 1);
1167
1168 let tool = EditTool::new();
1169 let result = tool
1170 .execute(json!({
1171 "file_path": file.path(),
1172 "patch": huge
1173 }))
1174 .await;
1175
1176 assert!(
1177 matches!(result, Err(ToolError::InvalidArguments(msg)) if msg.contains("max size"))
1178 );
1179 }
1180
1181 #[tokio::test]
1182 async fn edit_patch_rejects_excessive_block_count() {
1183 let file = tempfile::NamedTempFile::new().unwrap();
1184 tokio::fs::write(file.path(), "hello world").await.unwrap();
1185 let mut patch = String::new();
1186 for _ in 0..=MAX_PATCH_BLOCKS {
1187 patch.push_str("<<<<<<< SEARCH\nx\n=======\ny\n>>>>>>> REPLACE\n");
1188 }
1189
1190 let tool = EditTool::new();
1191 let result = tool
1192 .execute(json!({
1193 "file_path": file.path(),
1194 "patch": patch
1195 }))
1196 .await;
1197
1198 assert!(
1199 matches!(result, Err(ToolError::InvalidArguments(msg)) if msg.contains("max block count"))
1200 );
1201 }
1202
1203 #[tokio::test]
1204 async fn edit_includes_json_diagnostics_after_change() {
1205 let file = tempfile::Builder::new().suffix(".json").tempfile().unwrap();
1206 tokio::fs::write(file.path(), r#"{"ok":true}"#)
1207 .await
1208 .unwrap();
1209
1210 let read_tool = ReadTool::new();
1211 let _ = read_tool
1212 .execute_with_context(
1213 json!({ "file_path": file.path() }),
1214 ToolExecutionContext {
1215 session_id: Some("session_edit_diag"),
1216 tool_call_id: "call_1",
1217 event_tx: None,
1218 available_tool_schemas: None,
1219 },
1220 )
1221 .await
1222 .unwrap();
1223
1224 let tool = EditTool::new();
1225 let result = tool
1226 .execute_with_context(
1227 json!({
1228 "file_path": file.path(),
1229 "old_string": r#"{"ok":true}"#,
1230 "new_string": "{"
1231 }),
1232 ToolExecutionContext {
1233 session_id: Some("session_edit_diag"),
1234 tool_call_id: "call_2",
1235 event_tx: None,
1236 available_tool_schemas: None,
1237 },
1238 )
1239 .await
1240 .unwrap();
1241
1242 assert!(result.success);
1243 let payload: serde_json::Value = serde_json::from_str(&result.result).unwrap();
1244 assert_eq!(payload["diagnostics"]["format"], "json");
1245 assert_eq!(payload["diagnostics"]["valid"], false);
1246 assert_eq!(payload["touched_lines"], 2);
1247 }
1248}