octofs 0.3.0

Standalone MCP filesystem tools server — view, edit, shell, workdir
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
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
// Copyright 2026 Muvon Un Limited
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
//     http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

// Text editing module - handling string replacement, line operations, and insertions

use super::super::McpToolCall;
use super::core::save_file_history;
use anyhow::{anyhow, bail, Result};
use serde_json::{json, Value};
use std::collections::HashMap;
use std::path::Path;
use std::sync::{Arc, Mutex, OnceLock};
use tokio::fs as tokio_fs;
use tokio::sync::Mutex as AsyncMutex;

// Thread-safe file locking infrastructure for concurrent write protection.
// Outer map uses std::sync::Mutex (held briefly, no await while locked).
// Per-file locks use tokio::sync::Mutex (held across async file I/O).
static FILE_LOCKS: OnceLock<Mutex<HashMap<String, Arc<AsyncMutex<()>>>>> = OnceLock::new();

fn get_file_locks() -> &'static Mutex<HashMap<String, Arc<AsyncMutex<()>>>> {
	FILE_LOCKS.get_or_init(|| Mutex::new(HashMap::new()))
}
// Acquire a file-specific lock to prevent concurrent writes to the same file
async fn acquire_file_lock(path: &Path) -> Result<Arc<AsyncMutex<()>>> {
	let path_str = path.to_string_lossy().to_string();

	let file_lock = {
		let mut locks = get_file_locks().lock().expect("file locks poisoned");
		locks
			.entry(path_str)
			.or_insert_with(|| Arc::new(AsyncMutex::new(())))
			.clone()
	};

	Ok(file_lock)
}

// Helper function to resolve line indices, supporting negative indexing
// Negative indices count from the end: -1 = last line, -2 = second-to-last, etc.
fn resolve_line_index(index: i64, total_lines: usize) -> Result<usize, String> {
	if index == 0 {
		return Err("Line numbers are 1-indexed, use 1 for first line".to_string());
	}

	if index > 0 {
		let pos_index = index as usize;
		if pos_index > total_lines {
			return Err(format!(
				"Line {} exceeds file length ({} lines)",
				index, total_lines
			));
		}
		Ok(pos_index)
	} else {
		// Negative indexing: -1 = last line, -2 = second-to-last, etc.
		let from_end = (-index) as usize;
		if from_end > total_lines {
			return Err(format!(
				"Negative index {} exceeds file length ({} lines)",
				index, total_lines
			));
		}
		Ok(total_lines - from_end + 1)
	}
}

// Helper function to resolve line range with negative indexing support
fn resolve_line_range_batch(
	start: i64,
	end: i64,
	total_lines: usize,
) -> Result<(usize, usize), String> {
	let resolved_start = resolve_line_index(start, total_lines)?;
	let resolved_end = resolve_line_index(end, total_lines)?;

	if resolved_start > resolved_end {
		return Err(format!(
			"Start line ({}) cannot be greater than end line ({})",
			start, end
		));
	}

	Ok((resolved_start, resolved_end))
}

// Batch operation structures for the new single-file, multi-operation approach
#[derive(Debug, Clone)]
struct BatchOperation {
	operation_type: OperationType,
	line_range: LineRange,
	content: String,
	operation_index: usize,
}

// Unresolved batch operation with raw line indices (may be negative)
#[derive(Debug, Clone)]
struct UnresolvedBatchOperation {
	operation_type: OperationType,
	line_range: UnresolvedLineRange,
	content: String,
	operation_index: usize,
}

#[derive(Debug, Clone, PartialEq)]
enum OperationType {
	Insert,
	Replace,
}

#[derive(Debug, Clone)]
enum LineRange {
	Single(usize),       // Insert after this line (0 = beginning of file)
	Range(usize, usize), // Replace this range (inclusive, 1-indexed)
}

#[derive(Debug, Clone)]
enum UnresolvedLineRange {
	Single(i64),               // Insert after this line (may be negative)
	Range(i64, i64),           // Replace this range (may be negative)
	Hash(String),              // Insert after line identified by hash
	HashRange(String, String), // Replace range identified by hashes
}

// Resolve unresolved line range to actual line range using file length.
// `lines` is needed for hash resolution (the full file's lines).
fn resolve_unresolved_line_range(
	unresolved: &UnresolvedLineRange,
	total_lines: usize,
	lines: &[&str],
) -> Result<LineRange, String> {
	match unresolved {
		UnresolvedLineRange::Single(line) => {
			// Insert: 0 = before line 1 (beginning of file) — valid special case
			if *line == 0 {
				return Ok(LineRange::Single(0));
			}
			let resolved = resolve_line_index(*line, total_lines)?;
			Ok(LineRange::Single(resolved))
		}
		UnresolvedLineRange::Range(start, end) => {
			let (resolved_start, resolved_end) =
				resolve_line_range_batch(*start, *end, total_lines)?;
			Ok(LineRange::Range(resolved_start, resolved_end))
		}
		UnresolvedLineRange::Hash(hash) => {
			let line = crate::utils::line_hash::resolve_hash_to_line(hash, lines)?;
			Ok(LineRange::Single(line))
		}
		UnresolvedLineRange::HashRange(start_hash, end_hash) => {
			let start = crate::utils::line_hash::resolve_hash_to_line(start_hash, lines)?;
			let end = crate::utils::line_hash::resolve_hash_to_line(end_hash, lines)?;
			if start > end {
				return Err(format!(
					"Hash range is reversed: '{}' is line {} but '{}' is line {} (which comes before it). \
					Did you mean line_range: [\"{}\", \"{}\"]?",
					start_hash, start, end_hash, end, end_hash, start_hash
				));
			}
			Ok(LineRange::Range(start, end))
		}
	}
}

/// Normalize a string for whitespace-insensitive comparison.
/// Trims each line and collapses runs of whitespace into a single space.
fn normalize_whitespace(s: &str) -> String {
	s.lines()
		.map(|line| line.split_whitespace().collect::<Vec<_>>().join(" "))
		.collect::<Vec<_>>()
		.join("\n")
}

/// Find all byte-offset positions of `needle` in `haystack` (non-overlapping).
fn find_all_positions(haystack: &str, needle: &str) -> Vec<usize> {
	let mut positions = Vec::new();
	let mut start = 0;
	while let Some(pos) = haystack[start..].find(needle) {
		positions.push(start + pos);
		start += pos + needle.len();
	}
	positions
}

/// Convert a byte offset in `content` to a 1-indexed line number.
fn byte_offset_to_line(content: &str, offset: usize) -> usize {
	content[..offset].matches('\n').count() + 1
}

/// Compute line-by-line similarity ratio between two multi-line strings (0.0..1.0).
/// Uses a simple longest-common-subsequence ratio per line, averaged.
fn similarity_ratio(a: &str, b: &str) -> f64 {
	let a_lines: Vec<&str> = a.lines().collect();
	let b_lines: Vec<&str> = b.lines().collect();
	if a_lines.is_empty() && b_lines.is_empty() {
		return 1.0;
	}
	let max_lines = a_lines.len().max(b_lines.len());
	let mut total = 0.0;
	for i in 0..max_lines {
		let la = a_lines.get(i).unwrap_or(&"");
		let lb = b_lines.get(i).unwrap_or(&"");
		total += line_similarity(la, lb);
	}
	total / max_lines as f64
}

/// Character-level similarity between two strings using longest common subsequence.
fn line_similarity(a: &str, b: &str) -> f64 {
	let a_chars: Vec<char> = a.chars().collect();
	let b_chars: Vec<char> = b.chars().collect();
	let total = a_chars.len() + b_chars.len();
	if total == 0 {
		return 1.0;
	}
	let lcs_len = lcs_length(&a_chars, &b_chars);
	(2.0 * lcs_len as f64) / total as f64
}

/// Longest common subsequence length (O(n*m) DP, capped for performance).
fn lcs_length(a: &[char], b: &[char]) -> usize {
	// Cap to avoid quadratic blowup on very large inputs
	const MAX_CHARS: usize = 2000;
	let a = if a.len() > MAX_CHARS {
		&a[..MAX_CHARS]
	} else {
		a
	};
	let b = if b.len() > MAX_CHARS {
		&b[..MAX_CHARS]
	} else {
		b
	};

	let mut prev = vec![0usize; b.len() + 1];
	let mut curr = vec![0usize; b.len() + 1];
	for &ac in a {
		for (j, &bc) in b.iter().enumerate() {
			curr[j + 1] = if ac == bc {
				prev[j] + 1
			} else {
				prev[j + 1].max(curr[j])
			};
		}
		std::mem::swap(&mut prev, &mut curr);
		curr.iter_mut().for_each(|v| *v = 0);
	}
	*prev.last().unwrap_or(&0)
}

/// Diagnose why two text blocks differ.
fn diagnose_mismatch(expected: &str, actual: &str) -> String {
	let exp_norm = normalize_whitespace(expected);
	let act_norm = normalize_whitespace(actual);
	if exp_norm == act_norm {
		return "whitespace/indentation mismatch only".to_string();
	}
	// Check if it's just leading whitespace per line
	let exp_trimmed: Vec<&str> = expected.lines().map(|l| l.trim()).collect();
	let act_trimmed: Vec<&str> = actual.lines().map(|l| l.trim()).collect();
	if exp_trimmed == act_trimmed {
		return "indentation mismatch only".to_string();
	}
	"content differs".to_string()
}

/// Find the top N closest matching windows in `content` for `needle`.
/// Returns vec of (start_line_1indexed, window_text, similarity).
fn find_closest_matches(content: &str, needle: &str, top_n: usize) -> Vec<(usize, String, f64)> {
	let content_lines: Vec<&str> = content.lines().collect();
	let needle_lines: Vec<&str> = needle.lines().collect();
	let needle_count = needle_lines.len().max(1);

	if content_lines.len() < needle_count {
		return Vec::new();
	}

	let mut candidates: Vec<(usize, String, f64)> = Vec::new();

	for start in 0..=(content_lines.len() - needle_count) {
		let window: String = content_lines[start..start + needle_count].join("\n");
		let sim = similarity_ratio(needle, &window);
		// Only consider windows with at least 40% similarity
		if sim >= 0.4 {
			candidates.push((start + 1, window, sim));
		}
	}

	// Sort by similarity descending
	candidates.sort_by(|a, b| b.2.partial_cmp(&a.2).unwrap_or(std::cmp::Ordering::Equal));
	candidates.truncate(top_n);
	candidates
}

/// Detect the leading whitespace (indentation) of the first non-empty line.
fn detect_indent(text: &str) -> &str {
	for line in text.lines() {
		if !line.trim().is_empty() {
			let trimmed = line.trim_start();
			return &line[..line.len() - trimmed.len()];
		}
	}
	""
}

/// Adjust indentation of `new_text` to match the actual indentation at the match site.
/// `provided_old` is the old_text as given by the caller (may have wrong indent).
/// `actual_old` is the actual text found in the file at the match location.
fn adjust_indentation(new_text: &str, provided_old: &str, actual_old: &str) -> String {
	let provided_indent = detect_indent(provided_old);
	let actual_indent = detect_indent(actual_old);

	if provided_indent == actual_indent {
		return new_text.to_string();
	}

	// Determine if we're using tabs or spaces from the actual file
	let provided_len = provided_indent.len();

	new_text
		.lines()
		.map(|line| {
			if line.trim().is_empty() {
				return line.to_string();
			}
			// Strip the provided indent prefix if present, then prepend actual indent
			if provided_len > 0 && line.starts_with(provided_indent) {
				format!("{}{}", actual_indent, &line[provided_len..])
			} else {
				// Line doesn't start with expected indent — prepend the delta
				let line_indent_len = line.len() - line.trim_start().len();
				if line_indent_len >= provided_len {
					// Extra indent beyond base — preserve the extra part
					format!("{}{}", actual_indent, &line[provided_len..])
				} else {
					// Less indent than base — just prepend actual
					format!("{}{}", actual_indent, line.trim_start())
				}
			}
		})
		.collect::<Vec<_>>()
		.join("\n")
}

/// Atomic write: write to a temp file in the same directory, then rename over the target.
/// Guarantees the file is never in a partial/corrupt state if the process is interrupted.
pub async fn atomic_write(path: &Path, content: &str) -> Result<()> {
	let parent_dir = path.parent().unwrap_or_else(|| std::path::Path::new("."));
	let tmp_path = parent_dir.join(format!(
		".octofs_tmp_{}.tmp",
		path.file_name().unwrap_or_default().to_string_lossy()
	));
	tokio_fs::write(&tmp_path, content)
		.await
		.map_err(|e| anyhow!("Failed to write temp file for '{}': {}", path.display(), e))?;
	if let Err(e) = tokio_fs::rename(&tmp_path, path).await {
		// Clean up temp file on rename failure
		let _ = tokio_fs::remove_file(&tmp_path).await;
		return Err(anyhow!(
			"Failed to atomically replace '{}': {}",
			path.display(),
			e
		));
	}
	Ok(())
}
// Replace a string in a file with progressive matching strategy:
// 1. Exact match (original behavior)
// 2. Whitespace-normalized fuzzy match with indentation adjustment
// 3. Rich diagnostics with closest candidates on failure
pub async fn str_replace_spec(path: &Path, old_text: &str, new_text: &str) -> Result<String> {
	if !path.exists() {
		bail!("File not found");
	}

	// Acquire file lock to prevent concurrent writes
	let file_lock = acquire_file_lock(path).await?;
	let _lock_guard = file_lock.lock().await;

	// Read the file content
	let content = tokio_fs::read_to_string(path)
		.await
		.map_err(|e| anyhow!("Permission denied. Cannot read file: {}", e))?;

	// === Stage 1: Exact match ===
	let occurrences = content.matches(old_text).count();

	if occurrences == 1 {
		// Perfect exact match — replace directly
		save_file_history(path).await?;
		let new_content = content.replace(old_text, new_text);
		atomic_write(path, &new_content).await?;

		let line_count = old_text.lines().count();
		if line_count > 1 {
			crate::mcp::hint_accumulator::push_hint(&format!(
				"`str_replace` matched {} lines. Prefer `batch_edit` when you know the line range — it's faster and avoids content-search ambiguity.",
				line_count
			));
		}

		return Ok("Successfully replaced text at exactly one location.".to_string());
	}

	if occurrences > 1 {
		// Multiple exact matches — show locations to help disambiguate
		let positions = find_all_positions(&content, old_text);
		let use_hashes = crate::utils::line_hash::is_hash_mode();
		let file_lines: Vec<&str> = content.lines().collect();
		let hashes: Vec<String> = if use_hashes {
			crate::utils::line_hash::compute_line_hashes(&file_lines)
		} else {
			Vec::new()
		};
		let locations: Vec<String> = positions
			.iter()
			.enumerate()
			.map(|(i, &offset)| {
				let line = byte_offset_to_line(&content, offset);
				if use_hashes {
					format!("  {}. hash {} (line {})", i + 1, hashes[line - 1], line)
				} else {
					format!("  {}. line {}", i + 1, line)
				}
			})
			.collect();

		bail!(
			"Found {} matches for replacement text at:\n{}\nAdd more surrounding context to make a unique match, or use `batch_edit` with the specific {}.",
			occurrences,
			locations.join("\n"),
			if use_hashes { "hash range" } else { "line range" }
		);
	}

	// === Stage 2: Whitespace-normalized fuzzy match ===
	let norm_old = normalize_whitespace(old_text);
	let norm_content = normalize_whitespace(&content);
	let norm_occurrences = norm_content.matches(&norm_old).count();

	if norm_occurrences == 1 {
		// Found exactly one whitespace-normalized match — map back to original content
		// We need to find the actual text in the original content that corresponds
		let content_lines: Vec<&str> = content.lines().collect();
		let old_lines: Vec<&str> = old_text.lines().collect();
		let old_line_count = old_lines.len();

		let mut match_start = None;
		for start in 0..=content_lines.len().saturating_sub(old_line_count) {
			let window: Vec<&str> = content_lines[start..start + old_line_count].to_vec();
			let window_norm: Vec<String> = window
				.iter()
				.map(|l| l.split_whitespace().collect::<Vec<_>>().join(" "))
				.collect();
			let old_norm: Vec<String> = old_lines
				.iter()
				.map(|l| l.split_whitespace().collect::<Vec<_>>().join(" "))
				.collect();
			if window_norm == old_norm {
				match_start = Some(start);
				break;
			}
		}

		if let Some(start) = match_start {
			let actual_old = content_lines[start..start + old_line_count].join("\n");
			let adjusted_new = adjust_indentation(new_text, old_text, &actual_old);

			save_file_history(path).await?;
			let new_content = content.replace(&actual_old, &adjusted_new);
			atomic_write(path, &new_content).await?;

			return Ok(format!(
				"Successfully replaced text via fuzzy match (whitespace-normalized) at line {}. Indentation was auto-adjusted to match the file.",
				start + 1
			));
		}
	}

	// === Stage 3: No match — provide rich diagnostics ===
	let candidates = find_closest_matches(&content, old_text, 3);

	let mut msg = String::from(
		"No exact match found. Make sure you pass raw content (no escaped \\t, \\n).\n",
	);

	if candidates.is_empty() {
		msg.push_str("No similar text found in the file. Verify the content exists.");
	} else {
		let use_hashes = crate::utils::line_hash::is_hash_mode();
		let diag_lines: Vec<&str> = content.lines().collect();
		let diag_hashes: Vec<String> = if use_hashes {
			crate::utils::line_hash::compute_line_hashes(&diag_lines)
		} else {
			Vec::new()
		};

		msg.push_str("Closest matches:\n");
		let old_line_count = old_text.lines().count();
		for (i, (line_num, window, sim)) in candidates.iter().enumerate() {
			let diagnosis = diagnose_mismatch(old_text, window);
			let end_line = line_num + old_line_count - 1;
			if use_hashes {
				let start_hash = &diag_hashes[line_num - 1];
				let end_hash = &diag_hashes[end_line - 1];
				msg.push_str(&format!(
					"\n  {}. Hashes {}-{} ({:.0}% similar, {})\n",
					i + 1,
					start_hash,
					end_hash,
					sim * 100.0,
					diagnosis
				));
			} else {
				msg.push_str(&format!(
					"\n  {}. Lines {}-{} ({:.0}% similar, {})\n",
					i + 1,
					line_num,
					end_line,
					sim * 100.0,
					diagnosis
				));
			}
			// Show first 3 lines of the candidate as preview
			for (j, line) in window.lines().take(3).enumerate() {
				let pfx = if use_hashes {
					diag_hashes[line_num - 1 + j].clone()
				} else {
					format!("{}", line_num + j)
				};
				msg.push_str(&format!("     {}: {}\n", pfx, line));
			}
			if old_line_count > 3 {
				msg.push_str(&format!("     ... ({} more lines)\n", old_line_count - 3));
			}
		}
		msg.push_str(&format!(
			"\nTip: use `batch_edit` with the {} shown above, or fix the `old_text` content.",
			if use_hashes {
				"hash range"
			} else {
				"line range"
			}
		));
	}

	bail!("{}", msg);
}

// Returns true for lines that are pure structural punctuation (e.g. `}`, `]`, `}`).
// These are exempt from duplicate-line detection because they legitimately appear
// at range boundaries without indicating the AI included surrounding context.
//
// Only SINGLE closing tokens (optionally trailed by one `,` or `;`) qualify.
// Compound closers like `});` or `}),` are NOT noise — they carry real semantic
// meaning and duplicating them breaks code.
fn is_structural_noise(line: &str) -> bool {
	let trimmed = line.trim();
	// Empty or only whitespace
	if trimmed.is_empty() {
		return true;
	}
	// Strip one optional trailing `,` or `;`
	let core = trimmed.trim_end_matches([',', ';']);
	// Must be exactly one closing bracket/brace/paren after stripping the trailer
	matches!(core, "}" | "]" | ")")
}

// Check whether `content` (the replacement) duplicates the line immediately
// before or after the range [start_line, end_line] (1-indexed) in `file_lines`.
//
// Returns an error string if duplication is detected, Ok(()) otherwise.
// Structural noise lines are exempt — they legitimately appear at boundaries.
fn check_replace_duplicates(
	content_lines: &[&str],
	file_lines: &[&str],
	start_line: usize,
	end_line: usize,
	operation_index: usize,
	hashes: &[String],
	use_hashes: bool,
) -> Result<(), String> {
	if content_lines.is_empty() {
		return Ok(());
	}

	// Format a line reference as hash or number
	let line_id = |line_1idx: usize| -> String {
		if use_hashes {
			hashes[line_1idx - 1].clone()
		} else {
			format!("line {}", line_1idx)
		}
	};
	let range_id = |s: usize, e: usize| -> String {
		if use_hashes {
			format!("[{},{}]", hashes[s - 1], hashes[e - 1])
		} else {
			format!("[{}-{}]", s, e)
		}
	};

	// First content line matches the line immediately before the range
	if start_line > 1 {
		let line_before = file_lines[start_line - 2];
		if content_lines[0] == line_before && !is_structural_noise(line_before) {
			return Err(format!(
				"Duplicate line detected in operation {}: content's first line matches {} \
				(just before the replacement range {}). \
				{}: {:?}. Do NOT include surrounding unchanged lines — \
				only provide the lines that replace {}.",
				operation_index,
				line_id(start_line - 1),
				range_id(start_line, end_line),
				line_id(start_line - 1),
				line_before,
				range_id(start_line, end_line)
			));
		}
	}
	// Last content line matches the line immediately after the range
	if end_line < file_lines.len() {
		let line_after = file_lines[end_line];
		let last = content_lines[content_lines.len() - 1];
		if last == line_after && !is_structural_noise(line_after) {
			return Err(format!(
				"Duplicate line detected in operation {}: content's last line matches {} \
				(just after the replacement range {}). \
				{}: {:?}. Do NOT include surrounding unchanged lines — \
				only provide the lines that replace {}.",
				operation_index,
				line_id(end_line + 1),
				range_id(start_line, end_line),
				line_id(end_line + 1),
				line_after,
				range_id(start_line, end_line)
			));
		}
	}
	Ok(())
}

// Check for conflicting operations that would corrupt the file.
//
// Conflict rules:
//   Replace vs Replace — ranges overlap → conflict (both try to modify same lines)
//   Insert  vs Insert  — same anchor line → conflict (ambiguous ordering)
//   Insert  vs Replace — NEVER conflict. Insert operates in the *gap* after a line,
//                        Replace operates on the line's *content*. They are independent.
fn detect_conflicts(
	operations: &[BatchOperation],
	hashes: &[String],
	use_hashes: bool,
) -> Result<(), String> {
	let id = |line_1idx: usize| -> String {
		if use_hashes {
			hashes[line_1idx - 1].clone()
		} else {
			format!("{}", line_1idx)
		}
	};

	for i in 0..operations.len() {
		for j in (i + 1)..operations.len() {
			let op1 = &operations[i];
			let op2 = &operations[j];

			match (&op1.operation_type, &op2.operation_type) {
				// Two replaces: check if ranges overlap
				(OperationType::Replace, OperationType::Replace) => {
					let (s1, e1) = replace_range(&op1.line_range);
					let (s2, e2) = replace_range(&op2.line_range);
					// Ranges overlap when s1 <= e2 AND s2 <= e1
					if s1 <= e2 && s2 <= e1 {
						return Err(format!(
							"Conflicting operations: operation {} (replace [{},{}]) and {} (replace [{},{}]) have overlapping ranges",
							op1.operation_index, id(s1), id(e1), op2.operation_index, id(s2), id(e2)
						));
					}
				}
				// Two inserts: conflict only if same anchor line (ambiguous order)
				(OperationType::Insert, OperationType::Insert) => {
					let line1 = insert_anchor(&op1.line_range);
					let line2 = insert_anchor(&op2.line_range);
					if line1 == line2 {
						return Err(format!(
							"Conflicting operations: operation {} and {} both insert after {}",
							op1.operation_index,
							op2.operation_index,
							id(line1)
						));
					}
				}
				// Insert + Replace: never conflict — they operate on different
				// conceptual positions (gap vs content)
				(OperationType::Insert, OperationType::Replace)
				| (OperationType::Replace, OperationType::Insert) => {}
			}
		}
	}
	Ok(())
}

// Extract the (start, end) range from a replace operation's LineRange
fn replace_range(line_range: &LineRange) -> (usize, usize) {
	match line_range {
		LineRange::Range(start, end) => (*start, *end),
		LineRange::Single(line) => (*line, *line),
	}
}

// Extract the anchor line from an insert operation's LineRange
fn insert_anchor(line_range: &LineRange) -> usize {
	match line_range {
		LineRange::Single(line) => *line,
		LineRange::Range(start, _) => *start,
	}
}

// Apply all operations to the original file content.
//
// Two-phase approach: replaces first, then inserts.
// All line numbers reference the ORIGINAL file. Replaces among themselves are
// applied in reverse order (highest start first) so earlier replaces don't shift
// later ones. After all replaces, we compute an offset map so inserts can find
// their correct position in the (now modified) line array.
async fn apply_batch_operations(
	original_content: &str,
	operations: &[BatchOperation],
) -> Result<String> {
	let mut lines: Vec<String> = original_content.lines().map(|s| s.to_string()).collect();
	let original_len = lines.len();

	// Separate into replaces and inserts
	let mut replaces: Vec<&BatchOperation> = operations
		.iter()
		.filter(|op| op.operation_type == OperationType::Replace)
		.collect();
	let mut inserts: Vec<&BatchOperation> = operations
		.iter()
		.filter(|op| op.operation_type == OperationType::Insert)
		.collect();

	// Sort replaces by start position descending (highest first)
	replaces.sort_by(|a, b| {
		let sa = match &a.line_range {
			LineRange::Range(s, _) => *s,
			LineRange::Single(l) => *l,
		};
		let sb = match &b.line_range {
			LineRange::Range(s, _) => *s,
			LineRange::Single(l) => *l,
		};
		sb.cmp(&sa)
	});

	// Phase 1: Apply all replaces (reverse order preserves original line refs)
	// Track each replace's offset: (original_end, delta) where delta = new_lines - old_lines
	let mut replace_deltas: Vec<(usize, usize, i64)> = Vec::with_capacity(replaces.len());

	for operation in &replaces {
		let (start, end) = match operation.line_range {
			LineRange::Range(start, end) => (start, end),
			LineRange::Single(line) => (line, line),
		};

		// Validate line range (1-indexed)
		if start == 0 || end == 0 {
			return Err(anyhow!("Line numbers must be 1-indexed (start from 1)"));
		}
		if start > original_len || end > original_len {
			return Err(anyhow!(
				"Line range [{}, {}] is beyond file length {}",
				start,
				end,
				original_len
			));
		}
		if start > end {
			return Err(anyhow!("Invalid line range: start {} > end {}", start, end));
		}

		let old_count = end - start + 1;
		let content_lines: Vec<String> = operation.content.lines().map(|s| s.to_string()).collect();
		let new_count = content_lines.len();

		// Remove old lines (0-indexed)
		let start_idx = start - 1;
		for _ in 0..old_count {
			lines.remove(start_idx);
		}

		// Insert new content
		for (i, line) in content_lines.into_iter().enumerate() {
			lines.insert(start_idx + i, line);
		}

		replace_deltas.push((start, end, new_count as i64 - old_count as i64));
	}

	// Phase 2: Apply inserts with adjusted positions.
	// For each insert's original anchor line, compute how much it shifted due to replaces.
	// Sort inserts descending so they don't interfere with each other.
	inserts.sort_by(|a, b| {
		let la = match &a.line_range {
			LineRange::Single(l) => *l,
			LineRange::Range(s, _) => *s,
		};
		let lb = match &b.line_range {
			LineRange::Single(l) => *l,
			LineRange::Range(s, _) => *s,
		};
		lb.cmp(&la)
	});

	for operation in &inserts {
		let original_anchor = match operation.line_range {
			LineRange::Single(line) => line,
			_ => return Err(anyhow!("Insert operation must use single line number")),
		};

		// Validate against original file length
		if original_anchor > original_len {
			return Err(anyhow!(
				"Insert position {} is beyond file length {}",
				original_anchor,
				original_len
			));
		}

		// Compute adjusted position: start from original anchor, apply offsets
		// from all replaces that END at or before this anchor.
		// A replace at [s,e] with delta D shifts everything after line e by D.
		// If anchor >= e (insert is after the replaced region), apply the full delta.
		// If anchor < s (insert is before the replaced region), no shift.
		// If s <= anchor < e (insert is inside the replaced region), the anchor
		// falls within replacement content — shift by (anchor - s) positions into
		// the new content, capped at the new content length.
		let mut adjusted = original_anchor as i64;
		for &(rs, re, delta) in &replace_deltas {
			if original_anchor >= re {
				// Insert anchor is at or after the replace's end — full shift
				adjusted += delta;
			} else if original_anchor >= rs {
				// Insert anchor is inside the replaced range.
				// Map it proportionally: anchor was at offset (anchor - rs) into
				// the old range. In the new content, cap at new_count.
				let old_count = (re - rs + 1) as i64;
				let new_count = old_count + delta;
				let offset_in_old = (original_anchor - rs) as i64;
				// Proportional position in new content, capped
				let offset_in_new = offset_in_old.min(new_count);
				// The replace starts at rs in original. After this replace,
				// position rs maps to rs (unchanged start). So anchor maps to
				// rs + offset_in_new. But we started with adjusted = original_anchor,
				// so the delta to apply is: (rs as i64 + offset_in_new) - original_anchor as i64
				adjusted += (rs as i64 + offset_in_new) - original_anchor as i64;
			}
			// else: anchor is before the replace — no shift needed
		}

		let insert_pos = adjusted.max(0) as usize;

		// Split content by lines and insert
		let content_lines: Vec<String> = operation.content.lines().map(|s| s.to_string()).collect();

		if insert_pos == 0 {
			for (i, line) in content_lines.into_iter().enumerate() {
				lines.insert(i, line);
			}
		} else {
			let clamped = insert_pos.min(lines.len());
			for (i, line) in content_lines.into_iter().enumerate() {
				lines.insert(clamped + i, line);
			}
		}
	}

	// Preserve original file ending format
	let result = lines.join("\n");
	if original_content.ends_with('\n') && !result.ends_with('\n') {
		Ok(format!("{}\n", result))
	} else {
		Ok(result)
	}
}

// Parse line_range from JSON value (supports numbers, arrays, and hash strings)
fn parse_line_range(
	value: &Value,
	operation_type: &OperationType,
) -> Result<UnresolvedLineRange, String> {
	match value {
		Value::Number(n) => {
			let line = n.as_i64().ok_or("Line number must be an integer")?;
			match operation_type {
				// 0 is valid for insert: means "insert at beginning of file"
				OperationType::Insert => Ok(UnresolvedLineRange::Single(line)),
				OperationType::Replace => {
					if line == 0 {
						return Err(
							"Replace line numbers are 1-indexed, use 1 for first line".to_string()
						);
					}
					Ok(UnresolvedLineRange::Range(line, line)) // Single line replace
				}
			}
		}
		Value::String(s) => {
			// Hash-based line identifier (e.g., "a3bd")
			Ok(UnresolvedLineRange::Hash(s.clone()))
		}
		Value::Array(arr) => {
			if arr.is_empty() {
				return Err("Line range array must have 1 or 2 elements".to_string());
			}
			// Check if array contains strings (hash range) or numbers (line range)
			if arr[0].is_string() {
				// Hash range: ["start_hash", "end_hash"]
				if arr.len() != 2 {
					return Err("Hash range array must have exactly 2 elements".to_string());
				}
				let start_hash = arr[0].as_str().ok_or("Hash must be a string")?.to_string();
				let end_hash = arr[1].as_str().ok_or("Hash must be a string")?.to_string();
				match operation_type {
					OperationType::Insert => {
						Err("Insert operation cannot use hash range - use single hash".to_string())
					}
					OperationType::Replace => {
						Ok(UnresolvedLineRange::HashRange(start_hash, end_hash))
					}
				}
			} else if arr.len() == 1 {
				let line = arr[0].as_i64().ok_or("Line number must be an integer")?;
				match operation_type {
					// 0 is valid for insert: means "insert at beginning of file"
					OperationType::Insert => Ok(UnresolvedLineRange::Single(line)),
					OperationType::Replace => {
						if line == 0 {
							return Err("Replace line numbers are 1-indexed, use 1 for first line"
								.to_string());
						}
						Ok(UnresolvedLineRange::Range(line, line))
					}
				}
			} else if arr.len() == 2 {
				let start = arr[0].as_i64().ok_or("Start line must be an integer")?;
				let end = arr[1].as_i64().ok_or("End line must be an integer")?;
				if start == 0 || end == 0 {
					return Err(
						"Replace line numbers are 1-indexed, use 1 for first line".to_string()
					);
				}
				match operation_type {
					OperationType::Insert => Err(
						"Insert operation cannot use line range - use single line number"
							.to_string(),
					),
					OperationType::Replace => Ok(UnresolvedLineRange::Range(start, end)),
				}
			} else {
				Err("Line range array must have 1 or 2 elements".to_string())
			}
		}
		_ => Err("Line range must be a number, array, or hash string".to_string()),
	}
}

// NEW REVOLUTIONARY BATCH_EDIT: Single file, multiple operations, original line numbers
pub async fn batch_edit_spec(call: &McpToolCall, operations: &[Value]) -> Result<String> {
	// Extract path from the call parameters - NEW: single file only
	let path_str = match call.parameters.get("path").and_then(|v| v.as_str()) {
		Some(p) => p,
		None => {
			bail!("Missing required 'path' parameter for batch_edit");
		}
	};

	// Fail fast: validate operations array before touching the filesystem
	if operations.is_empty() {
		bail!("Operations array is empty — nothing to do.");
	}

	const MAX_OPERATIONS: usize = 50;
	if operations.len() > MAX_OPERATIONS {
		bail!(
			"Too many operations: {} (max {}). Split into multiple calls.",
			operations.len(),
			MAX_OPERATIONS
		);
	}

	let path = super::core::resolve_path(path_str);

	// Check if file exists
	if !path.exists() {
		bail!("File not found: {}", path_str);
	}

	// Acquire file lock to prevent concurrent writes
	let file_lock = acquire_file_lock(&path).await?;
	let _lock_guard = file_lock.lock().await;

	// Read original file content
	let original_content = tokio_fs::read_to_string(&path)
		.await
		.map_err(|e| anyhow!("Failed to read file '{}': {}", path_str, e))?;

	// Parse and validate all operations (with unresolved line ranges)
	let mut unresolved_operations = Vec::new();
	let mut failed_operations = 0;
	let mut operation_details = Vec::new();

	for (index, operation) in operations.iter().enumerate() {
		let operation_obj = match operation.as_object() {
			Some(obj) => obj,
			None => {
				failed_operations += 1;
				operation_details.push(json!({
					"operation_index": index,
					"status": "failed",
					"error": "Operation must be an object"
				}));
				continue;
			}
		};

		// Extract operation type
		let op_type_str = match operation_obj.get("operation").and_then(|v| v.as_str()) {
			Some(op) => op,
			None => {
				failed_operations += 1;
				operation_details.push(json!({
					"operation_index": index,
					"status": "failed",
					"error": "Missing 'operation' field"
				}));
				continue;
			}
		};

		// Parse operation type
		let operation_type = match op_type_str {
			"insert" => OperationType::Insert,
			"replace" => OperationType::Replace,
			_ => {
				failed_operations += 1;
				operation_details.push(json!({
					"operation_index": index,
					"operation": op_type_str,
					"status": "failed",
					"error": format!("Unsupported operation type: '{}'. Supported operations: insert, replace", op_type_str)
				}));
				continue;
			}
		};

		// Extract line_range
		let line_range = match operation_obj.get("line_range") {
			Some(range_value) => match parse_line_range(range_value, &operation_type) {
				Ok(range) => range,
				Err(e) => {
					failed_operations += 1;
					operation_details.push(json!({
						"operation_index": index,
						"operation": op_type_str,
						"status": "failed",
						"error": format!("Invalid 'line_range': {}", e)
					}));
					continue;
				}
			},
			None => {
				failed_operations += 1;
				operation_details.push(json!({
					"operation_index": index,
					"operation": op_type_str,
					"status": "failed",
					"error": "Missing 'line_range' field"
				}));
				continue;
			}
		};

		// Extract content
		let content = match operation_obj.get("content").and_then(|v| v.as_str()) {
			Some(c) => c.to_string(),
			None => {
				failed_operations += 1;
				operation_details.push(json!({
					"operation_index": index,
					"operation": op_type_str,
					"status": "failed",
					"error": "Missing 'content' field"
				}));
				continue;
			}
		};

		// Create unresolved batch operation
		let unresolved_op = UnresolvedBatchOperation {
			operation_type,
			line_range: line_range.clone(),
			content,
			operation_index: index,
		};

		unresolved_operations.push(unresolved_op);

		operation_details.push(json!({
			"operation_index": index,
			"operation": op_type_str,
			"status": "parsed",
			"line_range": match &line_range {
				UnresolvedLineRange::Single(line) => json!(line),
				UnresolvedLineRange::Range(start, end) => json!([start, end]),
				UnresolvedLineRange::Hash(h) => json!(h),
				UnresolvedLineRange::HashRange(s, e) => json!([s, e]),
			}
		}));
	}

	// If all operations failed during parsing, return error
	if unresolved_operations.is_empty() {
		bail!(
			"No valid operations found. {} operations failed during parsing.",
			failed_operations
		);
	}

	// Resolve negative line indices (and hash identifiers) now that we have the file content
	let total_lines = original_content.lines().count();
	let original_lines_for_resolve: Vec<&str> = original_content.lines().collect();
	let mut batch_operations = Vec::new();

	for unresolved_op in unresolved_operations {
		match resolve_unresolved_line_range(
			&unresolved_op.line_range,
			total_lines,
			&original_lines_for_resolve,
		) {
			Ok(resolved_range) => {
				batch_operations.push(BatchOperation {
					operation_type: unresolved_op.operation_type,
					line_range: resolved_range,
					content: unresolved_op.content,
					operation_index: unresolved_op.operation_index,
				});
			}
			Err(err) => {
				bail!(
					"Invalid line range in operation {}: {}",
					unresolved_op.operation_index,
					err
				);
			}
		}
	}

	// Compute hashes once for all validation and output (used in hash mode)
	let original_lines: Vec<&str> = original_content.lines().collect();
	let use_hashes = crate::utils::line_hash::is_hash_mode();
	let orig_hashes: Vec<String> = if use_hashes {
		crate::utils::line_hash::compute_line_hashes(&original_lines)
	} else {
		Vec::new()
	};

	// Check for conflicts between operations
	if let Err(conflict_error) = detect_conflicts(&batch_operations, &orig_hashes, use_hashes) {
		bail!("{}", conflict_error);
	}

	// Duplicate-line detection: validate operations against original content before applying.
	// Catches the #1 AI mistake of including surrounding/already-existing lines.
	// Helper: format a line reference as hash or number for error messages
	let orig_line_id = |line_1idx: usize| -> String {
		if use_hashes {
			orig_hashes[line_1idx - 1].clone()
		} else {
			format!("line {}", line_1idx)
		}
	};
	for op in &batch_operations {
		let content_lines: Vec<&str> = op.content.lines().collect();
		if content_lines.is_empty() {
			continue;
		}
		match op.operation_type {
			OperationType::Replace => {
				let (start, end) = match op.line_range {
					LineRange::Range(s, e) => (s, e),
					LineRange::Single(line) => (line, line),
				};
				if let Err(e) = check_replace_duplicates(
					&content_lines,
					&original_lines,
					start,
					end,
					op.operation_index,
					&orig_hashes,
					use_hashes,
				) {
					bail!("{}", e);
				}
			}
			OperationType::Insert => {
				// insert_after=N means content goes between line N and line N+1.
				let insert_after = match op.line_range {
					LineRange::Single(line) => line,
					_ => continue, // malformed; apply_batch_operations will catch it
				};
				// Single-line insert: content[0] must not duplicate the line right after
				// the insert point, unless it is structural noise.
				if content_lines.len() == 1 {
					if insert_after < original_lines.len() {
						let line_after = original_lines[insert_after];
						if content_lines[0] == line_after && !is_structural_noise(line_after) {
							bail!(
								"Duplicate line detected in operation {}: inserting after {} would duplicate {} which already reads {:?}. Do NOT re-insert content that already exists in the file.",
								op.operation_index, orig_line_id(insert_after), orig_line_id(insert_after + 1), line_after
							);
						}
					}
				} else {
					// Multi-line insert (>=2 lines): full block match is unambiguous duplication — no noise exemption.
					let available = original_lines.len().saturating_sub(insert_after);
					let check_len = content_lines.len().min(available);
					if check_len >= 2
						&& content_lines[..check_len]
							== original_lines[insert_after..insert_after + check_len]
					{
						bail!(
							"Duplicate block detected in operation {}: the {} inserted lines starting after {} already exist verbatim at {}-{}. Do NOT re-insert content that already exists in the file.",
							op.operation_index, check_len, orig_line_id(insert_after), orig_line_id(insert_after + 1), orig_line_id(insert_after + check_len)
						);
					}
				}
			}
		}
	}

	// Apply all operations to the original content
	let final_content = apply_batch_operations(&original_content, &batch_operations)
		.await
		.map_err(|e| anyhow!("Failed to apply operations: {}", e))?;

	// Save file history for undo functionality
	save_file_history(&path).await?;

	atomic_write(&path, &final_content)
		.await
		.map_err(|e| anyhow!("Atomic write failed for '{}': {}", path_str, e))?;

	// Update operation details with success status
	for detail in &mut operation_details {
		if detail["status"] == "parsed" {
			detail["status"] = json!("success");
		}
	}

	// Build annotated diff for each operation so the AI can verify edits landed correctly.
	// In hash mode: uses content-based hashes as line prefixes.
	// In number mode: uses sequential line numbers as before.
	const CONTEXT: usize = 3;
	let new_lines: Vec<&str> = final_content.lines().collect();
	let new_hashes: Vec<String> = if use_hashes {
		crate::utils::line_hash::compute_line_hashes(&new_lines)
	} else {
		Vec::new()
	};

	// Helper closures: produce a line prefix from index (0-based for hashes, 1-based number for display)
	let orig_prefix = |line_1idx: usize| -> String {
		if use_hashes {
			orig_hashes[line_1idx - 1].clone()
		} else {
			format!("{}", line_1idx)
		}
	};
	let new_prefix = |line_1idx: usize| -> String {
		if use_hashes {
			new_hashes[line_1idx - 1].clone()
		} else {
			format!("{}", line_1idx)
		}
	};

	let mut diffs: Vec<String> = Vec::new();

	// Sort ops by original start line (ascending) for readable diff output
	let mut display_ops = batch_operations.clone();
	display_ops.sort_by_key(|op| match &op.line_range {
		LineRange::Single(line) => *line,
		LineRange::Range(start, _) => *start,
	});

	for op in &display_ops {
		match op.operation_type {
			OperationType::Replace => {
				let (start, end) = match op.line_range {
					LineRange::Range(s, e) => (s, e),
					LineRange::Single(line) => (line, line),
				};
				let content_lines: Vec<&str> = op.content.lines().collect();
				let removed: Vec<String> = original_lines[start - 1..end]
					.iter()
					.map(|l| l.to_string())
					.collect();

				let mut diff: Vec<String> = Vec::new();
				let ctx_before_start = start.saturating_sub(CONTEXT).max(1);
				if ctx_before_start > 1 {
					diff.push("...".to_string());
				}
				for i in ctx_before_start..start {
					diff.push(format!("{}: {}", orig_prefix(i), original_lines[i - 1]));
				}
				for (i, old_line) in removed.iter().enumerate() {
					diff.push(format!("-{}: {}", orig_prefix(start + i), old_line));
				}
				for (i, new_line) in content_lines.iter().enumerate() {
					let idx = start + i;
					let pfx = if idx <= new_lines.len() {
						new_prefix(idx)
					} else {
						format!("{}", idx)
					};
					diff.push(format!("+{}: {}", pfx, new_line));
				}
				let new_after_start = start + content_lines.len();
				let new_after_end = (new_after_start + CONTEXT - 1).min(new_lines.len());
				for new_i in new_after_start..=new_after_end {
					if new_i >= 1 && new_i <= new_lines.len() {
						diff.push(format!("{}: {}", new_prefix(new_i), new_lines[new_i - 1]));
					}
				}
				if new_after_end < new_lines.len() {
					diff.push("...".to_string());
				}
				diffs.push(diff.join("\n"));
			}
			OperationType::Insert => {
				// For inserts just note where lines were added
				let after = match op.line_range {
					LineRange::Single(line) => line,
					LineRange::Range(start, _) => start,
				};
				let content_lines: Vec<&str> = op.content.lines().collect();
				let insert_at = after + 1; // new line numbers start here
				let mut diff: Vec<String> = Vec::new();
				for (i, new_line) in content_lines.iter().enumerate() {
					let idx = insert_at + i;
					let pfx = if idx <= new_lines.len() {
						new_prefix(idx)
					} else {
						format!("{}", idx)
					};
					diff.push(format!("+{}: {}", pfx, new_line));
				}
				diffs.push(diff.join("\n"));
			}
		}
	}

	// The diff IS the result — plain text, same style as `view` output.
	// LLM reads it to verify edits landed correctly without needing a separate view call.
	let diff_output = diffs.join("\n---\n");

	Ok(diff_output)
}