phi-kernel-tools 0.1.1

Kernel tools for phi-agent — file system, multi-agent, and shell tools
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
use std::path::PathBuf;

use agent_base::{AgentResult, Tool, ToolContext, ToolControlFlow, ToolOutput};
use async_trait::async_trait;
use serde::{Deserialize, Serialize};
use serde_json::{Value, json};

use super::resolve_path;

/// A single edit operation: replace `old_text` with `new_text`.
#[derive(Debug, Deserialize, Serialize)]
struct Edit {
    old_text: String,
    new_text: String,
}

/// Precision replacement tool — replaces exact text blocks in a file.
///
/// Unlike `write_file`, this tool only requires the LLM to provide the
/// specific text to replace, not the entire file. This is more token-efficient
/// and reduces the risk of modifying unrelated parts of the file.
///
/// # Matching strategy (4-level fallback)
///
/// 1. **Exact match** — the `old_text` must appear exactly once in the file
/// 2. **Rstrip** — trailing whitespace stripped from both sides
/// 3. **Trim** — leading and trailing whitespace stripped from both sides
/// 4. **Unicode NFC normalization** — normalizes Unicode before comparison
///
/// # Safety guarantees
///
/// - Each `old_text` must be unique in the file (appears exactly once)
/// - Edits must not overlap — overlapping edits are rejected
/// - Atomic write: content is written to a temp file then renamed
/// - Original line endings (`\n` vs `\r\n`) are preserved
pub struct EditFileTool {
    workspace_root: PathBuf,
}

impl EditFileTool {
    pub fn new(workspace_root: PathBuf) -> Self {
        Self { workspace_root }
    }
}

#[async_trait]
impl Tool for EditFileTool {
    fn name(&self) -> &'static str {
        "edit_file"
    }

    fn definition(&self) -> Value {
        json!({
            "type": "function",
            "function": {
                "name": "edit_file",
                "description": "Make precise text replacements in an existing file.\n\
                Provide one or more edits, each with old_text (text to find) and new_text (replacement).\n\
                Each old_text must appear exactly once in the file.\n\
                Multiple edits are applied to the original file (not incrementally).\n\
                Use this instead of write_file when you only need to change specific parts of a file.",
                "parameters": {
                    "type": "object",
                    "properties": {
                        "path": {
                            "type": "string",
                            "description": "Path to the file to edit, relative to the workspace root."
                        },
                        "edits": {
                            "type": "array",
                            "description": "List of edit operations. Each edit has old_text and new_text.",
                            "items": {
                                "type": "object",
                                "properties": {
                                    "old_text": {
                                        "type": "string",
                                        "description": "The exact text to find and replace. Must appear exactly once in the file."
                                    },
                                    "new_text": {
                                        "type": "string",
                                        "description": "The replacement text."
                                    }
                                },
                                "required": ["old_text", "new_text"]
                            }
                        }
                    },
                    "required": ["path", "edits"]
                }
            }
        })
    }

    fn metadata(&self) -> agent_base::ToolMetadata {
        agent_base::ToolMetadata {
            name: self.name().to_string(),
            description:
                "Precision text replacement in workspace files with uniqueness checks and atomic writes."
                    .to_string(),
            origin: "phi-kernel-tools".to_string(),
            version: env!("CARGO_PKG_VERSION").to_string(),
            requirements: vec![],
        }
    }

    async fn call(&self, args: &Value, _ctx: &ToolContext) -> AgentResult<ToolOutput> {
        let path_str = args
            .get("path")
            .and_then(Value::as_str)
            .unwrap_or("")
            .trim()
            .to_string();

        if path_str.is_empty() {
            return Ok(error_output("No file path provided.", &path_str));
        }

        let edits: Vec<Edit> = match args.get("edits") {
            Some(raw) => match serde_json::from_value(raw.clone()) {
                Ok(edits) => edits,
                Err(e) => {
                    return Ok(ToolOutput {
                        summary: format!(
                            "[Error]: Failed to parse edits: {}. Expected array of {{old_text, new_text}} objects.",
                            e
                        ),
                        raw: Some(json!({"error": e.to_string(), "path": path_str})),
                        control_flow: ToolControlFlow::Break,
                        truncation: None,
                    });
                }
            },
            None => {
                return Ok(ToolOutput {
                    summary: "[Error]: No edits provided. Expected array of {old_text, new_text} objects."
                        .to_string(),
                    raw: Some(json!({"error": "no edits provided", "path": path_str})),
                    control_flow: ToolControlFlow::Break,
                    truncation: None,
                });
            }
        };

        if edits.is_empty() {
            return Ok(ToolOutput {
                summary: "[Error]: Edits array is empty. Provide at least one edit operation."
                    .to_string(),
                raw: Some(json!({"error": "empty edits", "path": path_str})),
                control_flow: ToolControlFlow::Break,
                truncation: None,
            });
        }

        // Resolve and validate the path
        let file_path = match resolve_path(&self.workspace_root, &path_str) {
            Ok(p) => p,
            Err(e) => {
                return Ok(error_output(&format!("Path error: {}", e), &path_str));
            }
        };

        if !file_path.exists() {
            return Ok(ToolOutput {
                summary: format!("[Error]: File not found: {}", path_str),
                raw: Some(json!({"error": "file not found", "path": path_str})),
                control_flow: ToolControlFlow::Break,
                truncation: None,
            });
        }

        if !file_path.is_file() {
            return Ok(ToolOutput {
                summary: format!("[Error]: Path is not a file: {}", path_str),
                raw: Some(json!({"error": "not a file", "path": path_str})),
                control_flow: ToolControlFlow::Break,
                truncation: None,
            });
        }

        // Read original file content
        let original = match std::fs::read_to_string(&file_path) {
            Ok(c) => c,
            Err(e) => {
                return Ok(ToolOutput {
                    summary: format!("[Error]: Failed to read file: {}", e),
                    raw: Some(json!({"error": e.to_string(), "path": path_str})),
                    control_flow: ToolControlFlow::Break,
                    truncation: None,
                });
            }
        };

        // Detect line ending style
        let line_ending = detect_line_ending(&original);

        // Check for overlapping edits
        if let Err(overlap_err) = check_overlaps(&original, &edits) {
            return Ok(ToolOutput {
                summary: format!("[Error]: {}", overlap_err),
                raw: Some(json!({"error": overlap_err, "path": path_str})),
                control_flow: ToolControlFlow::Break,
                truncation: None,
            });
        }

        // Apply edits with 4-level fallback matching
        let mut modified = original.clone();
        let mut applied = 0;

        for (i, edit) in edits.iter().enumerate() {
            match find_and_replace(&modified, &original, edit, i) {
                Ok(new_content) => {
                    modified = new_content;
                    applied += 1;
                }
                Err(err) => {
                    return Ok(ToolOutput {
                        summary: format!("[Error]: Edit {} failed: {}", i, err),
                        raw: Some(json!({
                            "error": err,
                            "path": path_str,
                            "edit_index": i,
                            "old_text": edit.old_text,
                        })),
                        control_flow: ToolControlFlow::Break,
                        truncation: None,
                    });
                }
            }
        }

        // Normalize line endings to match original
        let modified = normalize_line_endings(&modified, line_ending);

        // Atomic write: write to temp file then rename
        let temp_path = temp_path_for(&file_path)?;
        if let Err(e) = std::fs::write(&temp_path, &modified) {
            let _ = std::fs::remove_file(&temp_path);
            return Ok(ToolOutput {
                summary: format!("[Error]: Failed to write file: {}", e),
                raw: Some(json!({"error": e.to_string(), "path": path_str})),
                control_flow: ToolControlFlow::Break,
                truncation: None,
            });
        }

        if let Err(e) = std::fs::rename(&temp_path, &file_path) {
            let _ = std::fs::remove_file(&temp_path);
            return Ok(ToolOutput {
                summary: format!("[Error]: Failed to save file (rename): {}", e),
                raw: Some(json!({"error": e.to_string(), "path": path_str})),
                control_flow: ToolControlFlow::Break,
                truncation: None,
            });
        }

        tracing::info!(
            path = %path_str,
            edits = applied,
            "edit_file"
        );

        Ok(ToolOutput {
            summary: format!("Successfully applied {} edit(s) to {}.", applied, path_str),
            raw: Some(json!({
                "path": path_str,
                "edits_applied": applied,
            })),
            control_flow: ToolControlFlow::Break,
            truncation: None,
        })
    }
}

// ── Helpers ──

fn error_output(message: &str, path: &str) -> ToolOutput {
    ToolOutput {
        summary: format!("[Error]: {}", message),
        raw: Some(json!({"error": message, "path": path})),
        control_flow: ToolControlFlow::Break,
        truncation: None,
    }
}

/// Detect the dominant line ending style in the content.
/// Returns `"\r\n"` if any CRLF is found, otherwise `"\n"`.
fn detect_line_ending(content: &str) -> &'static str {
    if content.contains("\r\n") {
        "\r\n"
    } else {
        "\n"
    }
}

/// Normalize all line endings in `content` to the given `line_ending`.
fn normalize_line_endings(content: &str, line_ending: &str) -> String {
    // Replace all CRLF with LF first, then optionally back to CRLF
    let normalized = content.replace("\r\n", "\n");
    if line_ending == "\r\n" {
        normalized.replace('\n', "\r\n")
    } else {
        normalized
    }
}

/// Generate a temp file path next to the target file.
fn temp_path_for(file_path: &std::path::Path) -> AgentResult<PathBuf> {
    let parent = file_path
        .parent()
        .unwrap_or_else(|| std::path::Path::new("."));
    let file_name = file_path
        .file_name()
        .unwrap_or_else(|| std::ffi::OsStr::new("tmp"));
    let mut temp_name = file_name.to_os_string();
    temp_name.push(".phi-tmp");
    Ok(parent.join(temp_name))
}

/// Check that no two edits overlap in the original content.
/// Returns an error string if overlap is detected.
fn check_overlaps(original: &str, edits: &[Edit]) -> Result<(), String> {
    let mut ranges: Vec<(usize, usize, usize)> = Vec::new(); // (start, end, idx)

    for (i, edit) in edits.iter().enumerate() {
        let positions = find_all_positions(original, &edit.old_text);
        if positions.len() != 1 {
            // Uniqueness is checked later; skip non-unique for overlap check
            continue;
        }
        let start = positions[0];
        let end = start + edit.old_text.len();
        ranges.push((start, end, i));
    }

    // Sort by start position
    ranges.sort_by_key(|(s, _, _)| *s);

    for w in ranges.windows(2) {
        let (_, end_a, idx_a) = w[0];
        let (start_b, _, idx_b) = w[1];
        if end_a > start_b {
            return Err(format!(
                "Edits {} and {} overlap. Edit {} ends at byte {} but edit {} starts at byte {}. \
                 Merge these edits into a single edit.",
                idx_a, idx_b, idx_a, end_a, idx_b, start_b
            ));
        }
    }

    Ok(())
}

/// Find all byte positions where `needle` appears in `haystack`.
fn find_all_positions(haystack: &str, needle: &str) -> Vec<usize> {
    if needle.is_empty() {
        return vec![];
    }
    let mut positions = Vec::new();
    let mut start = 0;
    while let Some(pos) = haystack[start..].find(needle) {
        let abs_pos = start + pos;
        positions.push(abs_pos);
        start = abs_pos + 1; // allow overlapping matches
    }
    positions
}

/// Try to find and replace `old_text` with `new_text` in `current`, using the
/// `original` content for uniqueness verification.
///
/// Matching is performed against `current` (which reflects previous edits),
/// but uniqueness is verified against `original` to prevent ambiguity.
///
/// Returns the new content on success, or an error string.
fn find_and_replace(
    current: &str,
    original: &str,
    edit: &Edit,
    _idx: usize,
) -> Result<String, String> {
    // Level 1: Exact match
    let matches = find_all_positions(current, &edit.old_text);
    if matches.len() == 1 {
        let pos = matches[0];
        return Ok(apply_replace(current, pos, &edit.old_text, &edit.new_text));
    }
    if matches.is_empty() {
        // Fall through to relaxed matching
    } else {
        // Multiple exact matches — ambiguity
        // Check if it was unique in the original
        let orig_matches = find_all_positions(original, &edit.old_text);
        if orig_matches.len() == 1 {
            // Was unique in original but now multiple — ambiguity from prior edits
            return Err(format!(
                "old_text is not unique in the file after previous edits. \
                 Found {} occurrences. Merge edits that affect the same text.",
                matches.len()
            ));
        }
        return Err(format!(
            "old_text is not unique — found {} occurrences. \
             Provide more surrounding context to make it unique.",
            matches.len()
        ));
    }

    // Level 2: Rstrip (trailing whitespace)
    let old_rstrip = edit.old_text.trim_end();
    if old_rstrip != edit.old_text {
        let current_rstrip = find_rstrip_matches(current, old_rstrip);
        if current_rstrip.len() == 1 {
            let (pos, actual) = current_rstrip[0];
            return Ok(apply_replace(current, pos, actual, &edit.new_text));
        }
        if current_rstrip.len() > 1 {
            return Err(format!(
                "old_text (with trailing whitespace stripped) is not unique — found {} matches. \
                 Provide more context.",
                current_rstrip.len()
            ));
        }
    }

    // Level 3: Trim (leading + trailing whitespace)
    let old_trim = edit.old_text.trim();
    if old_trim != edit.old_text && old_trim != old_rstrip {
        let current_trim = find_trim_matches(current, old_trim);
        if current_trim.len() == 1 {
            let (pos, actual) = current_trim[0];
            return Ok(apply_replace(current, pos, actual, &edit.new_text));
        }
        if current_trim.len() > 1 {
            return Err(format!(
                "old_text (with whitespace trimmed) is not unique — found {} matches. \
                 Provide more context.",
                current_trim.len()
            ));
        }
    }

    // Level 4: Unicode NFC normalization
    let old_nfc = unicode_normalize(&edit.old_text);
    if old_nfc != edit.old_text && old_nfc != old_rstrip && old_nfc != old_trim {
        let current_nfc = find_nfc_matches(current, &old_nfc);
        if current_nfc.len() == 1 {
            let (pos, actual) = current_nfc[0];
            return Ok(apply_replace(current, pos, actual, &edit.new_text));
        }
        if current_nfc.len() > 1 {
            return Err(format!(
                "old_text (with Unicode NFC normalization) is not unique — found {} matches. \
                 Provide more context.",
                current_nfc.len()
            ));
        }
    }

    // Exhausted all fallback levels
    Err(
        "old_text not found in the file. Check that the text matches exactly, \
         including whitespace and indentation."
            .to_string(),
    )
}

/// Find positions where `needle` matches with trailing whitespace stripped from lines.
fn find_rstrip_matches<'a>(haystack: &'a str, needle: &str) -> Vec<(usize, &'a str)> {
    let mut results = Vec::new();
    let mut start = 0;
    let needle_len = needle.len();
    while start + needle_len <= haystack.len() {
        let candidate = &haystack[start..];
        if candidate.starts_with(needle) {
            // Found a prefix match — find the actual extent (needle + trailing whitespace)
            let actual_len = candidate[needle_len..]
                .chars()
                .take_while(|c| c.is_whitespace() && *c != '\n' && *c != '\r')
                .map(|c| c.len_utf8())
                .sum::<usize>()
                + needle_len;
            let actual = &haystack[start..start + actual_len];
            results.push((start, actual));
            // Skip past the entire match to avoid overlapping results
            start += actual_len;
        } else {
            // Advance by one char
            if let Some(c) = candidate.chars().next() {
                start += c.len_utf8();
            } else {
                break;
            }
        }
    }
    results
}

/// Find positions where `needle` matches with whitespace trimmed (but not across newlines).
fn find_trim_matches<'a>(haystack: &'a str, needle: &str) -> Vec<(usize, &'a str)> {
    let mut results = Vec::new();
    let mut start = 0;
    while start < haystack.len() {
        let rest = &haystack[start..];

        // Skip leading whitespace (not newlines)
        let ws_skip: usize = rest
            .chars()
            .take_while(|c| c.is_whitespace() && *c != '\n' && *c != '\r')
            .map(|c| c.len_utf8())
            .sum();
        let after_ws = start + ws_skip;

        if let Some(after_ws_str) = haystack.get(after_ws..)
            && let Some(stripped) = after_ws_str.strip_prefix(needle)
        {
            // Found a match — find the actual extent
            let actual_len = ws_skip
                + needle.len()
                + stripped
                    .chars()
                    .take_while(|c| c.is_whitespace() && *c != '\n' && *c != '\r')
                    .map(|c| c.len_utf8())
                    .sum::<usize>();
            let actual = &haystack[start..start + actual_len];
            results.push((start, actual));
            // Skip past the entire match
            start += actual_len;
            continue;
        }
        // Advance by one char
        if let Some(c) = rest.chars().next() {
            start += c.len_utf8();
        } else {
            break;
        }
    }
    results
}

/// Find positions where `needle` matches after NFC normalization.
fn find_nfc_matches<'a>(haystack: &'a str, needle_nfc: &str) -> Vec<(usize, &'a str)> {
    let mut results = Vec::new();
    // Walk through haystack char by char, comparing NFC-normalized slices
    let chars: Vec<char> = haystack.chars().collect();
    let needle_chars: Vec<char> = needle_nfc.chars().collect();
    let needle_len = needle_chars.len();

    for i in 0..=chars.len().saturating_sub(needle_len) {
        let candidate: String = chars[i..i + needle_len].iter().collect();
        let candidate_nfc = unicode_normalize(&candidate);
        if candidate_nfc == needle_nfc {
            let start = chars[..i].iter().map(|c| c.len_utf8()).sum();
            let end = start
                + chars[i..i + needle_len]
                    .iter()
                    .map(|c| c.len_utf8())
                    .sum::<usize>();
            results.push((start, &haystack[start..end]));
        }
    }
    results
}

/// NFC-normalize a string. Falls back to the original if normalization fails.
fn unicode_normalize(s: &str) -> String {
    // Full NFC normalization requires the `unicode-normalization` crate.
    // For now, return the input unchanged — most strings are already NFC.
    // This function exists as a hook point for future enhancement.
    s.to_string()
}

/// Apply a replacement at the given byte position.
fn apply_replace(content: &str, pos: usize, old: &str, new: &str) -> String {
    let mut result = String::with_capacity(content.len() - old.len() + new.len());
    result.push_str(&content[..pos]);
    result.push_str(new);
    result.push_str(&content[pos + old.len()..]);
    result
}

#[cfg(test)]
mod tests {
    use super::*;

    fn dummy_ctx() -> ToolContext {
        let (tx, _rx) = tokio::sync::mpsc::unbounded_channel();
        ToolContext {
            session_id: agent_base::SessionId::new(0),
            user_event_tx: tx,
            llm_client: None,
            session_store: None,
            language: agent_base::Language::En,
            cancel_token: tokio_util::sync::CancellationToken::new(),
        }
    }

    fn setup_temp_workspace() -> (tempfile::TempDir, EditFileTool) {
        let dir = tempfile::tempdir().unwrap();
        let tool = EditFileTool::new(dir.path().to_path_buf());
        (dir, tool)
    }

    // ── find_all_positions ──

    #[test]
    fn test_find_all_positions_single() {
        let pos = find_all_positions("hello world", "world");
        assert_eq!(pos, vec![6]);
    }

    #[test]
    fn test_find_all_positions_multiple() {
        let pos = find_all_positions("foo bar foo baz foo", "foo");
        assert_eq!(pos, vec![0, 8, 16]);
    }

    #[test]
    fn test_find_all_positions_none() {
        let pos = find_all_positions("hello", "xyz");
        assert!(pos.is_empty());
    }

    #[test]
    fn test_find_all_positions_empty_needle() {
        let pos = find_all_positions("hello", "");
        assert!(pos.is_empty());
    }

    // ── detect_line_ending ──

    #[test]
    fn test_detect_line_ending_lf() {
        assert_eq!(detect_line_ending("hello\nworld\n"), "\n");
    }

    #[test]
    fn test_detect_line_ending_crlf() {
        assert_eq!(detect_line_ending("hello\r\nworld\r\n"), "\r\n");
    }

    #[test]
    fn test_detect_line_ending_mixed_prefers_crlf() {
        assert_eq!(detect_line_ending("hello\nworld\r\n"), "\r\n");
    }

    // ── normalize_line_endings ──

    #[test]
    fn test_normalize_to_lf() {
        assert_eq!(
            normalize_line_endings("hello\r\nworld\r\n", "\n"),
            "hello\nworld\n"
        );
    }

    #[test]
    fn test_normalize_to_crlf() {
        assert_eq!(
            normalize_line_endings("hello\nworld\n", "\r\n"),
            "hello\r\nworld\r\n"
        );
    }

    #[test]
    fn test_normalize_no_change() {
        assert_eq!(
            normalize_line_endings("hello\nworld\n", "\n"),
            "hello\nworld\n"
        );
    }

    // ── apply_replace ──

    #[test]
    fn test_apply_replace_basic() {
        let result = apply_replace("hello world", 6, "world", "earth");
        assert_eq!(result, "hello earth");
    }

    #[test]
    fn test_apply_replace_beginning() {
        let result = apply_replace("fn old() {}", 3, "old", "new");
        assert_eq!(result, "fn new() {}");
    }

    // ── check_overlaps ──

    #[test]
    fn test_check_overlaps_no_overlap() {
        let content = "line1\nline2\nline3\n";
        let edits = vec![
            Edit {
                old_text: "line1".to_string(),
                new_text: "LINE1".to_string(),
            },
            Edit {
                old_text: "line3".to_string(),
                new_text: "LINE3".to_string(),
            },
        ];
        assert!(check_overlaps(content, &edits).is_ok());
    }

    #[test]
    fn test_check_overlaps_detected() {
        let content = "hello world";
        let edits = vec![
            Edit {
                old_text: "hello world".to_string(),
                new_text: "hi earth".to_string(),
            },
            Edit {
                old_text: "world".to_string(),
                new_text: "earth".to_string(),
            },
        ];
        let err = check_overlaps(content, &edits).unwrap_err();
        assert!(err.contains("overlap"));
    }

    // ── find_and_replace ──

    #[test]
    fn test_find_and_replace_exact() {
        let result = find_and_replace(
            "hello world",
            "hello world",
            &Edit {
                old_text: "world".to_string(),
                new_text: "earth".to_string(),
            },
            0,
        )
        .unwrap();
        assert_eq!(result, "hello earth");
    }

    #[test]
    fn test_find_and_replace_not_unique() {
        let err = find_and_replace(
            "foo bar foo",
            "foo bar foo",
            &Edit {
                old_text: "foo".to_string(),
                new_text: "baz".to_string(),
            },
            0,
        )
        .unwrap_err();
        assert!(err.contains("not unique"));
    }

    #[test]
    fn test_find_and_replace_not_found() {
        let err = find_and_replace(
            "hello world",
            "hello world",
            &Edit {
                old_text: "xyz".to_string(),
                new_text: "abc".to_string(),
            },
            0,
        )
        .unwrap_err();
        assert!(err.contains("not found"));
    }

    // ── Full tool integration tests ──

    #[tokio::test]
    async fn test_edit_file_single_edit() {
        let (dir, tool) = setup_temp_workspace();
        std::fs::write(dir.path().join("test.txt"), "hello world\n").unwrap();

        let result = tool
            .call(
                &json!({
                    "path": "test.txt",
                    "edits": [{"old_text": "hello", "new_text": "hi"}]
                }),
                &dummy_ctx(),
            )
            .await
            .unwrap();

        assert!(result.summary.contains("Successfully applied 1 edit"));
        let content = std::fs::read_to_string(dir.path().join("test.txt")).unwrap();
        assert_eq!(content, "hi world\n");
    }

    #[tokio::test]
    async fn test_edit_file_multiple_edits() {
        let (dir, tool) = setup_temp_workspace();
        std::fs::write(dir.path().join("test.rs"), "fn old() {}\nfn other() {}\n").unwrap();

        let result = tool
            .call(
                &json!({
                    "path": "test.rs",
                    "edits": [
                        {"old_text": "fn old() {}", "new_text": "fn new() {}"},
                        {"old_text": "fn other() {}", "new_text": "fn another() {}"}
                    ]
                }),
                &dummy_ctx(),
            )
            .await
            .unwrap();

        assert!(result.summary.contains("Successfully applied 2 edit"));
        let content = std::fs::read_to_string(dir.path().join("test.rs")).unwrap();
        assert_eq!(content, "fn new() {}\nfn another() {}\n");
    }

    #[tokio::test]
    async fn test_edit_file_not_found() {
        let (_dir, tool) = setup_temp_workspace();
        let result = tool
            .call(
                &json!({
                    "path": "nonexistent.txt",
                    "edits": [{"old_text": "x", "new_text": "y"}]
                }),
                &dummy_ctx(),
            )
            .await
            .unwrap();
        assert!(result.summary.contains("not found"));
    }

    #[tokio::test]
    async fn test_edit_file_duplicate_old_text() {
        let (dir, tool) = setup_temp_workspace();
        std::fs::write(dir.path().join("dup.txt"), "foo bar foo\n").unwrap();

        let result = tool
            .call(
                &json!({
                    "path": "dup.txt",
                    "edits": [{"old_text": "foo", "new_text": "baz"}]
                }),
                &dummy_ctx(),
            )
            .await
            .unwrap();

        assert!(result.summary.contains("not unique"));
    }

    #[tokio::test]
    async fn test_edit_file_no_path() {
        let (_dir, tool) = setup_temp_workspace();
        let result = tool
            .call(
                &json!({"edits": [{"old_text": "x", "new_text": "y"}]}),
                &dummy_ctx(),
            )
            .await
            .unwrap();
        assert!(result.summary.contains("No file path"));
    }

    #[tokio::test]
    async fn test_edit_file_no_edits() {
        let (_dir, tool) = setup_temp_workspace();
        let result = tool
            .call(&json!({"path": "test.txt"}), &dummy_ctx())
            .await
            .unwrap();
        assert!(result.summary.contains("No edits provided"));
    }

    #[tokio::test]
    async fn test_edit_file_empty_edits() {
        let (_dir, tool) = setup_temp_workspace();
        let result = tool
            .call(&json!({"path": "test.txt", "edits": []}), &dummy_ctx())
            .await
            .unwrap();
        assert!(result.summary.contains("empty"));
    }

    #[tokio::test]
    async fn test_edit_file_preserves_line_endings_crlf() {
        let (dir, tool) = setup_temp_workspace();
        std::fs::write(dir.path().join("crlf.txt"), "line1\r\nline2\r\nline3\r\n").unwrap();

        let result = tool
            .call(
                &json!({
                    "path": "crlf.txt",
                    "edits": [{"old_text": "line2", "new_text": "LINE2"}]
                }),
                &dummy_ctx(),
            )
            .await
            .unwrap();

        assert!(result.summary.contains("Successfully applied 1 edit"));
        let content = std::fs::read_to_string(dir.path().join("crlf.txt")).unwrap();
        assert_eq!(content, "line1\r\nLINE2\r\nline3\r\n");
    }

    #[tokio::test]
    async fn test_edit_file_overlapping_edits_rejected() {
        let (dir, tool) = setup_temp_workspace();
        std::fs::write(dir.path().join("overlap.txt"), "hello world\n").unwrap();

        let result = tool
            .call(
                &json!({
                    "path": "overlap.txt",
                    "edits": [
                        {"old_text": "hello world", "new_text": "hi"},
                        {"old_text": "world", "new_text": "earth"}
                    ]
                }),
                &dummy_ctx(),
            )
            .await
            .unwrap();

        assert!(result.summary.contains("overlap"));
    }

    #[tokio::test]
    async fn test_edit_file_path_traversal_rejected() {
        let (_dir, tool) = setup_temp_workspace();
        let result = tool
            .call(
                &json!({
                    "path": "../outside.txt",
                    "edits": [{"old_text": "x", "new_text": "y"}]
                }),
                &dummy_ctx(),
            )
            .await
            .unwrap();
        assert!(result.summary.contains("Error"));
    }

    #[tokio::test]
    async fn test_name_and_definition() {
        let tool = EditFileTool::new(PathBuf::from("/tmp"));
        assert_eq!(tool.name(), "edit_file");
        let def = tool.definition();
        assert_eq!(def["function"]["name"], "edit_file");
    }

    #[tokio::test]
    async fn test_metadata() {
        let tool = EditFileTool::new(PathBuf::from("/tmp"));
        let meta = tool.metadata();
        assert_eq!(meta.name, "edit_file");
        assert_eq!(meta.origin, "phi-kernel-tools");
    }

    #[test]
    fn test_find_rstrip_matches() {
        let content = "hello   \nworld\n";
        let matches = find_rstrip_matches(content, "hello");
        assert_eq!(matches.len(), 1);
        assert_eq!(matches[0].1, "hello   ");
    }

    #[test]
    fn test_find_trim_matches() {
        let content = "  hello  \nworld\n";
        let matches = find_trim_matches(content, "hello");
        assert_eq!(matches.len(), 1);
        assert_eq!(matches[0].1, "  hello  ");
    }

    #[test]
    fn test_level2_rstrip_fallback() {
        let content = "hello   \nworld\n";
        let result = find_and_replace(
            content,
            content,
            &Edit {
                old_text: "hello   ".to_string(),
                new_text: "hi".to_string(),
            },
            0,
        )
        .unwrap();
        assert_eq!(result, "hi\nworld\n");
    }
}