claudette 0.6.0

Local-first AI personal secretary for Ollama. Telegram bot, voice, persistent scheduler, Gmail and Calendar. Single-binary Rust.
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
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
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
//! Codet — the code-validator sidecar.
//!
//! Every time Claudette writes a code file (via `write_file`), Codet validates
//! it by running syntax checks and unit tests, then attempts to fix any bugs
//! via a secondary LLM (the coder model, default `qwen3-coder:30b` `MoE`;
//! set `CLAUDETTE_CODER_MODEL=qwen2.5-coder:14b` on RAM-constrained hosts,
//! or use the `/coder` slash command to change the active config). The fix-loop
//! conversation lives entirely inside Codet — Claudette's main context only
//! sees a one-line summary in the tool result, so there's zero context
//! pollution.
//!
//! **VRAM strategy:** the brain and the coder don't run simultaneously.
//! Ollama hot-swaps them in VRAM automatically when `codet.rs` calls the
//! coder model. For the 30b `MoE` (19 GB) on 8 GB VRAM boxes this REQUIRES
//! `OLLAMA_MAX_LOADED_MODELS=1` — without it, Ollama tries to keep both
//! loaded and OOMs the 30b load. Cold-load cost is ~5-10 s per swap for
//! 7b/14b; ~30-40 s for 30b.
//!
//! **Safety rule:** Codet never applies a fix that doesn't measurably improve
//! the validation outcome. Specifically:
//! - A syntax fix is accepted iff `py_compile` was failing and now passes.
//! - A test fix is accepted iff a test that was failing now passes AND no
//!   previously-passing test starts failing (no regressions).

use std::path::Path;

use crate::{
    ApiClient, ApiRequest, AssistantEvent, ContentBlock, ConversationMessage, MessageRole,
};
use serde_json::json;

use crate::api::OllamaApiClient;
use crate::test_runner::{
    check_python_imports, has_python_tests, has_rust_tests, run_js_syntax_check,
    run_python_syntax_check, run_python_unittest, run_rust_syntax_check, run_ts_syntax_check,
};

// Coder defaults now live in `model_config::ModelConfig::from_preset`.
// The `CLAUDETTE_CODER_*` env vars are still honored via the env-merge
// pass in model_config; the slash `/coder` command mutates the active
// config in place. The constants below are historical references for the
// defaults (30b, 49 K ctx, 12 K predict) kept for documentation only.

fn coder_num_ctx() -> u32 {
    crate::model_config::active().coder.num_ctx
}

fn coder_num_predict() -> u32 {
    crate::model_config::active().coder.num_predict
}

/// Maximum fix iterations before Codet gives up and reports
/// `CodetStatus::CouldNotFix`.
const MAX_FIX_ATTEMPTS: u32 = 3;

/// Resolve the coder model name. Sprint 14: reads from
/// `model_config::active().coder.model`, which itself merges the
/// `CLAUDETTE_CODER_MODEL` env var on first init.
#[must_use]
pub fn coder_model() -> String {
    crate::model_config::active().coder.model
}

/// Whether code validation is enabled. Defaults to true; set
/// `CLAUDETTE_VALIDATE_CODE=false` to disable (useful for debugging
/// or when the coder model isn't pulled yet).
#[must_use]
pub fn validation_enabled() -> bool {
    std::env::var("CLAUDETTE_VALIDATE_CODE").map_or(true, |v| {
        !matches!(v.to_lowercase().as_str(), "false" | "0" | "no" | "off")
    })
}

// ────────────────────────────────────────────────────────────────────────────
// Public result types
// ────────────────────────────────────────────────────────────────────────────

/// Outcome of a Codet validation run. Serialized into the `write_file` tool
/// result JSON so Claudette sees a one-line summary and the REPL can print
/// a warning on failure.
#[derive(Debug, Clone)]
pub struct CodetResult {
    pub syntax_ok: bool,
    pub tests_found: bool,
    pub tests_passed: u32,
    pub tests_failed: u32,
    pub tests_errors: u32,
    /// Number of fix attempts that actually **landed** — i.e. produced an
    /// improved re-check. Always ≤ `attempts_made`.
    pub fixes_applied: u32,
    /// Number of fix attempts **tried**, whether or not they landed.
    /// Useful for diagnosing a `CouldNotFix` outcome: "0 landed after 3
    /// attempts" reads very differently from "0 landed after 0 attempts."
    pub attempts_made: u32,
    pub fix_summary: String,
    pub status: CodetStatus,
}

/// A file the user referenced in the generation/validation prompt.
/// Passed to the coder so it can read real class/method names instead of
/// fabricating them (brownfield API-matching). Collected by
/// `tools::collect_reference_files` from the `generate_code` description
/// and threaded through every coder call (generation, fix, surgical patch).
#[derive(Debug, Clone)]
pub struct ReferenceFile {
    /// Display path — shown to the coder verbatim, so use whatever the user
    /// typed (tilde form preferred) rather than the resolved absolute path.
    pub path: String,
    /// File contents, already truncated to the per-file cap by the collector.
    pub content: String,
}

/// Format a reference-file block for inclusion in a coder prompt.
/// Returns an empty string when there are no references, so callers can
/// concat unconditionally.
fn format_reference_block(references: &[ReferenceFile]) -> String {
    if references.is_empty() {
        return String::new();
    }
    let mut s = String::from(
        "\n\n## Reference files (read before writing code)\n\
         These existing files are the ground truth. Use ONLY the class names, \
         method names, and signatures that actually appear below — do not \
         invent or rename any API.\n\n",
    );
    use std::fmt::Write as _;
    for rf in references {
        let _ = write!(s, "### `{}`\n```\n{}\n```\n\n", rf.path, rf.content);
    }
    s
}

/// Terminal state of a validation run.
#[derive(Debug, Clone)]
pub enum CodetStatus {
    /// All checks passed on the first try — no fixes needed.
    AllPassed,
    /// Some checks failed; Codet fixed them and all checks pass now.
    FixedAll,
    /// Codet hit `MAX_FIX_ATTEMPTS` without achieving a clean run.
    CouldNotFix { last_error: String },
    /// File extension isn't a known code type — validation skipped.
    Skipped,
}

impl CodetResult {
    /// One-line JSON fragment suitable for embedding in a `write_file` tool
    /// result. Compact enough to not bloat Claudette's context.
    #[must_use]
    pub fn to_json(&self) -> serde_json::Value {
        json!({
            "syntax_ok": self.syntax_ok,
            "tests_found": self.tests_found,
            "tests_passed": self.tests_passed,
            "tests_failed": self.tests_failed,
            "fixes_applied": self.fixes_applied,
            "attempts_made": self.attempts_made,
            "fix_summary": self.fix_summary,
            "status": match &self.status {
                CodetStatus::AllPassed => "all_passed".to_string(),
                CodetStatus::FixedAll => "fixed_all".to_string(),
                CodetStatus::CouldNotFix { last_error } => format!("could_not_fix: {last_error}"),
                CodetStatus::Skipped => "skipped".to_string(),
            },
        })
    }
}

// ────────────────────────────────────────────────────────────────────────────
// Public entry point
// ────────────────────────────────────────────────────────────────────────────

/// Main entry point — called from `run_write_file` after a successful code
/// write. Returns `None` if the file isn't a known code type or validation
/// is disabled via `CLAUDETTE_VALIDATE_CODE=false`.
#[must_use]
pub fn validate_code_file(path: &Path, references: &[ReferenceFile]) -> Option<CodetResult> {
    if !validation_enabled() {
        return None;
    }
    let ext = path.extension()?.to_str()?;
    let validator: fn(&Path, &[ReferenceFile]) -> CodetResult = match ext {
        "py" => validate_python,
        "rs" => validate_rust,
        "js" | "mjs" | "cjs" => validate_js,
        "ts" | "tsx" => validate_ts,
        _ => return None,
    };
    let _swap = CoderSwapGuard::begin(&coder_model());
    Some(validator(path, references))
}

// ────────────────────────────────────────────────────────────────────────────
// VRAM-swap guard (P0, 2026-05-07; coalesced 2026-05-15)
// ────────────────────────────────────────────────────────────────────────────

/// Coalescing lease over the coder model's VRAM slot.
///
/// Originally a per-call RAII guard that ran brain-evict on construction
/// and coder-evict on drop — but the audit on 2026-05-15 flagged that
/// `generate_code` and multi-file `write_file` patterns triggered a fresh
/// evict/reload cycle for every call, even when the coder we just dropped
/// was the same model the next call wants. The lease coalesces consecutive
/// guards over the same coder model so the swap fires once per cluster,
/// not once per call:
///
/// 1. First `begin()` evicts the brain and "loads" the coder (Ollama's
///    on-demand load runs when the coder is actually called).
/// 2. Subsequent `begin()` calls for the same coder reuse the lease —
///    no evict, no load. Ref count bumps.
/// 3. `Drop` decrements the ref count. When it reaches zero the lease
///    is marked dormant but the coder is NOT immediately evicted; a
///    background thread evicts it after [`COALESCE_WINDOW`] unless a new
///    `begin()` resurrects the lease first.
/// 4. [`drain_pending_coder_lease`] is called from `run_turn_with_retry`
///    so the next brain turn synchronously gets its VRAM back regardless
///    of how recently codet ran.
///
/// **Backend dispatch** lives in `brain_selector` and is gated by
/// `CLAUDETTE_OPENAI_COMPAT`: LM Studio uses `lms unload --all`, Ollama
/// uses `keep_alive: 0` per-model.
///
/// **Safety on tight-VRAM hosts.** The brain HTTP prep latency between
/// tool batches typically exceeds [`COALESCE_WINDOW`], so the coder is
/// usually evicted before the brain actually tries to load. Hosts with
/// `OLLAMA_MAX_LOADED_MODELS=1` are doubly safe — Ollama arbitrates if a
/// race ever does happen.
struct CoderSwapGuard {
    needs_swap: bool,
}

/// How long the lease stays warm after the last guard drops. Long enough
/// to bridge the gap between two consecutive `generate_code` /
/// `write_file` tool calls in the same brain iteration; short enough that
/// an idle drop releases VRAM before the user notices.
const COALESCE_WINDOW: std::time::Duration = std::time::Duration::from_millis(750);

#[derive(Default)]
struct LeaseState {
    /// Coder model currently leased (or pending evict). `None` means no
    /// outstanding lease and no coder VRAM held.
    coder_model: Option<String>,
    /// Number of live `CoderSwapGuard`s holding the lease.
    active_count: usize,
    /// Bumped on every `begin` / `drop` so the background evictor can
    /// tell whether the state changed while it was sleeping.
    generation: u64,
}

static LEASE: std::sync::Mutex<LeaseState> = std::sync::Mutex::new(LeaseState {
    coder_model: None,
    active_count: 0,
    generation: 0,
});

impl CoderSwapGuard {
    fn begin(coder_model: &str) -> Self {
        let brain_model = crate::model_config::active().brain.model.clone();
        let needs_swap = crate::brain_selector::should_swap_for_coder(&brain_model, coder_model);

        if !needs_swap {
            return Self { needs_swap: false };
        }

        // Re-use the lease when the previous codet call left the same
        // coder model warm. The coder isn't actually "loaded" yet in
        // Ollama's sense — it loads on first HTTP call — but the brain is
        // already evicted, which is the expensive half of the swap.
        let mut state = LEASE.lock().expect("CODER_LEASE poisoned");
        let reusing_same_coder = state.coder_model.as_deref() == Some(coder_model);

        if !reusing_same_coder {
            // Different coder than the previous lease (or no lease).
            // Evict whatever previous coder was holding the slot, then
            // evict the brain. Order matters: evicting the brain after
            // the previous coder makes the contention window smaller.
            if let Some(prev) = state.coder_model.take() {
                crate::brain_selector::evict_coder_after_codet(&prev);
            }
            eprintln!(
                "  {} {}",
                crate::theme::dim("\u{21bb}"),
                crate::theme::dim(&format!(
                    "codet: swapping brain ({brain_model}) \u{2192} coder ({coder_model})..."
                )),
            );
            crate::brain_selector::evict_brain_for_codet(&brain_model);
            state.coder_model = Some(coder_model.to_string());
        }
        state.active_count += 1;
        state.generation += 1;

        Self { needs_swap: true }
    }
}

impl Drop for CoderSwapGuard {
    fn drop(&mut self) {
        if !self.needs_swap {
            return;
        }
        let scheduled_gen = {
            let mut state = LEASE.lock().expect("CODER_LEASE poisoned");
            state.active_count = state.active_count.saturating_sub(1);
            state.generation += 1;
            if state.active_count > 0 {
                // Still in use — another guard is keeping the lease alive.
                return;
            }
            state.generation
        };

        // Schedule a deferred evict. If a new `begin()` fires before the
        // window elapses, `generation` advances and the evictor will see
        // that the lease moved on, leaving the new lease intact.
        std::thread::spawn(move || {
            std::thread::sleep(COALESCE_WINDOW);
            let to_evict = {
                let mut state = LEASE.lock().expect("CODER_LEASE poisoned");
                if state.generation != scheduled_gen || state.active_count > 0 {
                    // Either a new lease started after we slept, or another
                    // guard came in during the window. Leave the lease.
                    None
                } else {
                    state.coder_model.take()
                }
            };
            if let Some(coder) = to_evict {
                crate::brain_selector::evict_coder_after_codet(&coder);
            }
        });
    }
}

/// Synchronously evict the coder model if a deferred lease is still
/// outstanding. Called at the top of every brain turn so the brain reclaims
/// its VRAM slot regardless of how recently codet ran. Returns true if it
/// actually evicted something, mostly for tests.
pub fn drain_pending_coder_lease() -> bool {
    let to_evict = {
        let Ok(mut state) = LEASE.lock() else {
            return false;
        };
        if state.active_count > 0 {
            // A codet operation is in flight (re-entrant brain call inside
            // the same conv-loop iteration is rare but possible). Don't
            // evict from under it — Ollama's `MAX_LOADED_MODELS=1` will
            // arbitrate.
            return false;
        }
        state.coder_model.take()
    };
    if let Some(coder) = to_evict {
        crate::brain_selector::evict_coder_after_codet(&coder);
        true
    } else {
        false
    }
}

// ────────────────────────────────────────────────────────────────────────────
// Python validation
// ────────────────────────────────────────────────────────────────────────────

fn validate_python(path: &Path, references: &[ReferenceFile]) -> CodetResult {
    let mut fixes_applied: u32 = 0;
    let mut attempts_made: u32 = 0;
    let mut fix_descriptions: Vec<String> = Vec::new();

    // ── Step 1: Syntax check ────────────────────────────────────────────
    let syntax = run_python_syntax_check(path);
    if !syntax.success {
        // Try to fix the syntax error via the coder model.
        let content = std::fs::read_to_string(path).unwrap_or_default();
        let error_msg = format!("{}\n{}", syntax.stdout, syntax.stderr);
        match try_fix_loop(path, &content, &error_msg, FixTarget::Syntax, references) {
            FixLoopOutcome::Fixed {
                description,
                attempts_tried,
            } => {
                fixes_applied += 1;
                attempts_made += attempts_tried;
                fix_descriptions.push(description);
            }
            FixLoopOutcome::GaveUp {
                last_error,
                attempts_tried,
            } => {
                attempts_made += attempts_tried;
                return CodetResult {
                    syntax_ok: false,
                    tests_found: false,
                    tests_passed: 0,
                    tests_failed: 0,
                    tests_errors: 0,
                    fixes_applied,
                    attempts_made,
                    fix_summary: fix_descriptions.join("; "),
                    status: CodetStatus::CouldNotFix { last_error },
                };
            }
        }
    }

    // ── Step 2: Test detection ──────────────────────────────────────────
    let content = std::fs::read_to_string(path).unwrap_or_default();
    if !has_python_tests(&content) {
        return CodetResult {
            syntax_ok: true,
            tests_found: false,
            tests_passed: 0,
            tests_failed: 0,
            tests_errors: 0,
            fixes_applied,
            attempts_made,
            fix_summary: fix_descriptions.join("; "),
            status: if fixes_applied > 0 {
                CodetStatus::FixedAll
            } else {
                CodetStatus::AllPassed
            },
        };
    }

    // ── Step 2.5: Import pre-flight ─────────────────────────────────────
    // `python -m unittest` wraps ImportError-at-load as a `_FailedTest`,
    // which looks like a test failure but is actually an environment
    // problem (missing package). The fix loop cannot repair it — the
    // coder keeps returning near-identical code because the code isn't
    // wrong. Detect missing packages up front and bail out with a clean
    // message instead of burning 3 regen attempts.
    let imports = check_python_imports(path);
    if !imports.missing.is_empty() {
        return CodetResult {
            syntax_ok: true,
            tests_found: true,
            tests_passed: 0,
            tests_failed: 0,
            tests_errors: 0,
            fixes_applied,
            attempts_made,
            fix_summary: fix_descriptions.join("; "),
            status: CodetStatus::CouldNotFix {
                last_error: format!(
                    "cannot validate tests — Python package(s) not importable: {}. \
                     Install them in the active Python environment and retry with /validate.",
                    imports.missing.join(", "),
                ),
            },
        };
    }

    // ── Step 3: Run unit tests ──────────────────────────────────────────
    let test_result = run_python_unittest(path);
    if test_result.all_passed {
        return CodetResult {
            syntax_ok: true,
            tests_found: true,
            tests_passed: test_result.passed,
            tests_failed: 0,
            tests_errors: 0,
            fixes_applied,
            attempts_made,
            fix_summary: fix_descriptions.join("; "),
            status: if fixes_applied > 0 {
                CodetStatus::FixedAll
            } else {
                CodetStatus::AllPassed
            },
        };
    }

    // ── Step 4: Fix loop for failing tests ──────────────────────────────
    match try_fix_loop(
        path,
        &content,
        &test_result.error_output,
        FixTarget::Tests,
        references,
    ) {
        FixLoopOutcome::Fixed {
            description,
            attempts_tried,
        } => {
            fixes_applied += 1;
            attempts_made += attempts_tried;
            fix_descriptions.push(description);
            // Re-read final test counts after the fix.
            let final_tests = run_python_unittest(path);
            CodetResult {
                syntax_ok: true,
                tests_found: true,
                tests_passed: final_tests.passed,
                tests_failed: final_tests.failed,
                tests_errors: final_tests.errors,
                fixes_applied,
                attempts_made,
                fix_summary: fix_descriptions.join("; "),
                status: if final_tests.all_passed {
                    CodetStatus::FixedAll
                } else {
                    CodetStatus::CouldNotFix {
                        last_error: final_tests.error_output,
                    }
                },
            }
        }
        FixLoopOutcome::GaveUp {
            last_error,
            attempts_tried,
        } => {
            attempts_made += attempts_tried;
            CodetResult {
                syntax_ok: true,
                tests_found: true,
                tests_passed: test_result.passed,
                tests_failed: test_result.failed,
                tests_errors: test_result.errors,
                fixes_applied,
                attempts_made,
                fix_summary: fix_descriptions.join("; "),
                status: CodetStatus::CouldNotFix { last_error },
            }
        }
    }
}

// ────────────────────────────────────────────────────────────────────────────
// Rust validation (Sprint 10)
// ────────────────────────────────────────────────────────────────────────────

fn validate_rust(path: &Path, references: &[ReferenceFile]) -> CodetResult {
    let mut fixes_applied: u32 = 0;
    let mut attempts_made: u32 = 0;
    let mut fix_descriptions: Vec<String> = Vec::new();

    let syntax = run_rust_syntax_check(path);
    if !syntax.success {
        let content = std::fs::read_to_string(path).unwrap_or_default();
        let error_msg = format!("{}\n{}", syntax.stdout, syntax.stderr);
        match try_fix_loop(
            path,
            &content,
            &error_msg,
            FixTarget::RustSyntax,
            references,
        ) {
            FixLoopOutcome::Fixed {
                description,
                attempts_tried,
            } => {
                fixes_applied += 1;
                attempts_made += attempts_tried;
                fix_descriptions.push(description);
            }
            FixLoopOutcome::GaveUp {
                last_error,
                attempts_tried,
            } => {
                attempts_made += attempts_tried;
                return CodetResult {
                    syntax_ok: false,
                    tests_found: false,
                    tests_passed: 0,
                    tests_failed: 0,
                    tests_errors: 0,
                    fixes_applied,
                    attempts_made,
                    fix_summary: fix_descriptions.join("; "),
                    status: CodetStatus::CouldNotFix { last_error },
                };
            }
        }
    }

    let content = std::fs::read_to_string(path).unwrap_or_default();
    CodetResult {
        syntax_ok: true,
        tests_found: has_rust_tests(&content),
        tests_passed: 0,
        tests_failed: 0,
        tests_errors: 0,
        fixes_applied,
        attempts_made,
        fix_summary: fix_descriptions.join("; "),
        status: if fixes_applied > 0 {
            CodetStatus::FixedAll
        } else {
            CodetStatus::AllPassed
        },
    }
}

// ────────────────────────────────────────────────────────────────────────────
// JavaScript validation (Sprint 10)
// ────────────────────────────────────────────────────────────────────────────

fn validate_js(path: &Path, references: &[ReferenceFile]) -> CodetResult {
    let mut fixes_applied: u32 = 0;
    let mut attempts_made: u32 = 0;
    let mut fix_descriptions: Vec<String> = Vec::new();

    let syntax = run_js_syntax_check(path);
    if !syntax.success {
        let content = std::fs::read_to_string(path).unwrap_or_default();
        let error_msg = format!("{}\n{}", syntax.stdout, syntax.stderr);
        match try_fix_loop(path, &content, &error_msg, FixTarget::JsSyntax, references) {
            FixLoopOutcome::Fixed {
                description,
                attempts_tried,
            } => {
                fixes_applied += 1;
                attempts_made += attempts_tried;
                fix_descriptions.push(description);
            }
            FixLoopOutcome::GaveUp {
                last_error,
                attempts_tried,
            } => {
                attempts_made += attempts_tried;
                return CodetResult {
                    syntax_ok: false,
                    tests_found: false,
                    tests_passed: 0,
                    tests_failed: 0,
                    tests_errors: 0,
                    fixes_applied,
                    attempts_made,
                    fix_summary: fix_descriptions.join("; "),
                    status: CodetStatus::CouldNotFix { last_error },
                };
            }
        }
    }

    CodetResult {
        syntax_ok: true,
        tests_found: false,
        tests_passed: 0,
        tests_failed: 0,
        tests_errors: 0,
        fixes_applied,
        attempts_made,
        fix_summary: fix_descriptions.join("; "),
        status: if fixes_applied > 0 {
            CodetStatus::FixedAll
        } else {
            CodetStatus::AllPassed
        },
    }
}

// ────────────────────────────────────────────────────────────────────────────
// TypeScript validation (Sprint 10)
// ────────────────────────────────────────────────────────────────────────────

fn validate_ts(path: &Path, references: &[ReferenceFile]) -> CodetResult {
    let mut fixes_applied: u32 = 0;
    let mut attempts_made: u32 = 0;
    let mut fix_descriptions: Vec<String> = Vec::new();

    let syntax = run_ts_syntax_check(path);
    if !syntax.success {
        // Check if tsc is actually available — if npx/tsc not installed,
        // skip rather than reporting a false failure.
        if syntax.stderr.contains("not found")
            || syntax.stderr.contains("not recognized")
            || syntax.stderr.contains("spawn")
        {
            return CodetResult {
                syntax_ok: true,
                tests_found: false,
                tests_passed: 0,
                tests_failed: 0,
                tests_errors: 0,
                fixes_applied: 0,
                attempts_made: 0,
                fix_summary: "tsc not available, syntax check skipped".to_string(),
                status: CodetStatus::Skipped,
            };
        }

        let content = std::fs::read_to_string(path).unwrap_or_default();
        let error_msg = format!("{}\n{}", syntax.stdout, syntax.stderr);
        match try_fix_loop(path, &content, &error_msg, FixTarget::TsSyntax, references) {
            FixLoopOutcome::Fixed {
                description,
                attempts_tried,
            } => {
                fixes_applied += 1;
                attempts_made += attempts_tried;
                fix_descriptions.push(description);
            }
            FixLoopOutcome::GaveUp {
                last_error,
                attempts_tried,
            } => {
                attempts_made += attempts_tried;
                return CodetResult {
                    syntax_ok: false,
                    tests_found: false,
                    tests_passed: 0,
                    tests_failed: 0,
                    tests_errors: 0,
                    fixes_applied,
                    attempts_made,
                    fix_summary: fix_descriptions.join("; "),
                    status: CodetStatus::CouldNotFix { last_error },
                };
            }
        }
    }

    CodetResult {
        syntax_ok: true,
        tests_found: false,
        tests_passed: 0,
        tests_failed: 0,
        tests_errors: 0,
        fixes_applied,
        attempts_made,
        fix_summary: fix_descriptions.join("; "),
        status: if fixes_applied > 0 {
            CodetStatus::FixedAll
        } else {
            CodetStatus::AllPassed
        },
    }
}

// ────────────────────────────────────────────────────────────────────────────
// Fix loop — shared between syntax fixes and test fixes
// ────────────────────────────────────────────────────────────────────────────

#[derive(Debug, Clone, Copy)]
enum FixTarget {
    Syntax,
    Tests,
    RustSyntax,
    JsSyntax,
    TsSyntax,
}

enum FixLoopOutcome {
    Fixed {
        description: String,
        attempts_tried: u32,
    },
    GaveUp {
        last_error: String,
        attempts_tried: u32,
    },
}

/// A surgical search-replace patch: locate `search` verbatim in the file and
/// substitute `replace`. Emitted by the coder in SEARCH/REPLACE block format.
#[derive(Debug, Clone)]
struct Patch {
    search: String,
    replace: String,
}

fn try_fix_loop(
    path: &Path,
    original_content: &str,
    initial_error: &str,
    target: FixTarget,
    references: &[ReferenceFile],
) -> FixLoopOutcome {
    let mut current_content = original_content.to_string();
    let mut last_error = initial_error.to_string();

    // Surgical path (SEARCH/REPLACE patches) is far cheaper than full-file regen
    // for syntax fixes — a 1-3 line bug becomes ~50 output tokens instead of
    // a full file rewrite. Test fixes often need multi-function edits, so
    // those stay on the full-regen path.
    let use_surgical_path = matches!(
        target,
        FixTarget::Syntax | FixTarget::RustSyntax | FixTarget::JsSyntax | FixTarget::TsSyntax
    );

    for attempt in 0..MAX_FIX_ATTEMPTS {
        eprintln!(
            "  {} {}",
            crate::theme::dim(""),
            crate::theme::dim(&format!(
                "codet: fix attempt {}/{MAX_FIX_ATTEMPTS}",
                attempt + 1
            )),
        );

        let fixed_content = if use_surgical_path {
            ask_coder_for_patches(&current_content, &last_error, references)
                .and_then(|patches| apply_patches(&current_content, &patches))
                .or_else(|| {
                    eprintln!(
                        "  {} {}",
                        crate::theme::dim(""),
                        crate::theme::dim(
                            "codet: surgical path failed, falling back to full regen...",
                        ),
                    );
                    ask_coder_to_fix(&current_content, &last_error, references)
                })
        } else {
            ask_coder_to_fix(&current_content, &last_error, references)
        };

        let Some(fixed_content) = fixed_content else {
            eprintln!(
                "  {} {}",
                crate::theme::warn(crate::theme::WARN_GLYPH),
                crate::theme::warn("codet: coder returned no usable fix, retrying..."),
            );
            continue;
        };

        // Write the candidate fix to disk.
        if std::fs::write(path, &fixed_content).is_err() {
            continue;
        }

        // Re-validate — did the fix actually help?
        let improved = match target {
            FixTarget::Syntax => run_python_syntax_check(path).success,
            FixTarget::RustSyntax => run_rust_syntax_check(path).success,
            FixTarget::JsSyntax => run_js_syntax_check(path).success,
            FixTarget::TsSyntax => run_ts_syntax_check(path).success,
            FixTarget::Tests => {
                let recheck = run_python_unittest(path);
                recheck.all_passed
                    || (recheck.failed + recheck.errors) < (count_failures_from_error(&last_error))
            }
        };

        if improved {
            let desc = match target {
                FixTarget::Syntax => "fixed Python syntax error".to_string(),
                FixTarget::RustSyntax => "fixed Rust syntax error".to_string(),
                FixTarget::JsSyntax => "fixed JavaScript syntax error".to_string(),
                FixTarget::TsSyntax => "fixed TypeScript syntax error".to_string(),
                FixTarget::Tests => "fixed failing test(s)".to_string(),
            };
            eprintln!(
                "  {} {}",
                crate::theme::ok(crate::theme::OK_GLYPH),
                crate::theme::ok(&format!("codet: {desc}")),
            );
            return FixLoopOutcome::Fixed {
                description: desc,
                attempts_tried: attempt + 1,
            };
        }
        eprintln!(
            "  {} {}",
            crate::theme::warn(crate::theme::WARN_GLYPH),
            crate::theme::warn("codet: fix didn't improve results, retrying..."),
        );

        // Fix didn't help (or made things worse) — update context for next
        // attempt but restore the original content so we don't compound
        // bad fixes.
        let _ = std::fs::write(path, &current_content);
        current_content = fixed_content;
        let retest_err = match target {
            FixTarget::Syntax => {
                let r = run_python_syntax_check(path);
                format!("{}\n{}", r.stdout, r.stderr)
            }
            FixTarget::RustSyntax => {
                let r = run_rust_syntax_check(path);
                format!("{}\n{}", r.stdout, r.stderr)
            }
            FixTarget::JsSyntax => {
                let r = run_js_syntax_check(path);
                format!("{}\n{}", r.stdout, r.stderr)
            }
            FixTarget::TsSyntax => {
                let r = run_ts_syntax_check(path);
                format!("{}\n{}", r.stdout, r.stderr)
            }
            FixTarget::Tests => {
                let r = run_python_unittest(path);
                r.error_output
            }
        };
        if !retest_err.trim().is_empty() {
            last_error = retest_err;
        }
    }

    // Restore the original content — never leave a bad fix on disk.
    let _ = std::fs::write(path, original_content);
    FixLoopOutcome::GaveUp {
        last_error,
        attempts_tried: MAX_FIX_ATTEMPTS,
    }
}

/// Quick heuristic to count how many failure lines exist in an error
/// string. Used by the "did the fix help?" check to see if the failure
/// count dropped even if it's not zero yet.
fn count_failures_from_error(error: &str) -> u32 {
    let mut count = 0u32;
    for line in error.lines() {
        let trimmed = line.trim_end();
        if trimmed.ends_with("... FAIL") || trimmed.ends_with("... ERROR") {
            count += 1;
        }
    }
    if count == 0 && !error.trim().is_empty() {
        1 // at least one unknown failure
    } else {
        count
    }
}

// ────────────────────────────────────────────────────────────────────────────
// Coder model interaction
// ────────────────────────────────────────────────────────────────────────────

// ────────────────────────────────────────────────────────────────────────────
// Code generation — the coder writes code from a description
// ────────────────────────────────────────────────────────────────────────────

/// Ask the coder model to generate code from a natural-language description.
/// Called by the `generate_code` tool. Returns `Some(code)` on success.
/// The coder model is better at writing code than Claudette (general-purpose),
/// so routing code generation through here produces higher-quality output.
pub fn generate_code(
    description: &str,
    language: &str,
    references: &[ReferenceFile],
) -> Option<String> {
    let model = coder_model();
    let _swap = CoderSwapGuard::begin(&model);
    if !references.is_empty() {
        eprintln!(
            "  {} {}",
            crate::theme::dim(""),
            crate::theme::dim(&format!(
                "codet: generating {language} code via {model} with {} reference file(s)...",
                references.len()
            )),
        );
    } else {
        eprintln!(
            "  {} {}",
            crate::theme::dim(""),
            crate::theme::dim(&format!("codet: generating {language} code via {model}...")),
        );
    }

    let mut client = OllamaApiClient::new(model.clone(), json!([]))
        .with_context(coder_num_ctx())
        .with_max_predict(coder_num_predict());

    let reference_block = format_reference_block(references);
    let prompt = format!(
        "Write a {language} file matching this description:\n\n\
        {description}{reference_block}\n\n\
        Requirements:\n\
        - Write clean, well-structured, production-quality code\n\
        - Include proper comments where the logic isn't obvious\n\
        - If the description mentions tests, include them in the same file\n\
        - If reference files are provided, use ONLY the real class/method \
          names from them — never invent or rename APIs\n\
        - Output ONLY the file content — no explanations, no markdown fences"
    );

    let request = ApiRequest {
        system_prompt: vec![format!(
            "You are an expert {language} developer. Output ONLY code. \
             No explanations, no markdown, no commentary."
        )],
        messages: vec![ConversationMessage {
            role: MessageRole::User,
            blocks: vec![ContentBlock::Text { text: prompt }],
            usage: None,
        }]
        .into(),
    };

    let events = match client.stream(&request) {
        Ok(ev) => ev,
        Err(e) => {
            eprintln!(
                "  {} {}",
                crate::theme::error(crate::theme::ERR_GLYPH),
                crate::theme::error(&format!("codet: {model} request failed: {e}")),
            );
            return None;
        }
    };

    let mut code = String::new();
    for event in events {
        if let AssistantEvent::TextDelta(text) = event {
            code.push_str(&text);
        }
    }

    eprintln!(
        "  {} {}",
        crate::theme::dim(""),
        crate::theme::dim(&format!(
            "codet: generated {} chars of {language}",
            code.len()
        )),
    );

    let code = strip_code_blocks(&code);
    if code.trim().is_empty() {
        eprintln!(
            "  {} {}",
            crate::theme::warn(crate::theme::WARN_GLYPH),
            crate::theme::warn("codet: coder returned empty response"),
        );
        return None;
    }
    Some(code)
}

// ────────────────────────────────────────────────────────────────────────────
// Code fixing — the coder repairs broken code
// ────────────────────────────────────────────────────────────────────────────

/// Ask the coder model to fix the bugs in `file_content` given the
/// `error_output`. Returns `Some(corrected_code)` or `None` if the
/// model couldn't produce anything usable. Diagnostic messages go to
/// stderr so the user can see why fixes fail.
fn ask_coder_to_fix(
    file_content: &str,
    error_output: &str,
    references: &[ReferenceFile],
) -> Option<String> {
    let model = coder_model();
    eprintln!(
        "  {} {}",
        crate::theme::dim(""),
        crate::theme::dim(&format!("codet: asking {model} to fix...")),
    );

    let mut client = OllamaApiClient::new(model.clone(), json!([]))
        .with_context(coder_num_ctx())
        .with_max_predict(coder_num_predict());

    let reference_block = format_reference_block(references);
    let prompt = format!(
        "The following Python file has bug(s). Fix them.\n\n\
        ## File content\n```python\n{file_content}\n```\n\n\
        ## Error output\n```\n{error_output}\n```{reference_block}\n\n\
        Output ONLY the corrected Python file. No explanations, no markdown fences. \
        If reference files are provided above, use ONLY the real class/method names \
        from them."
    );

    let request = ApiRequest {
        system_prompt: vec![
            "You are a Python code fixer. Output ONLY the corrected code. \
             No explanations, no markdown, no commentary."
                .to_string(),
        ],
        messages: vec![ConversationMessage {
            role: MessageRole::User,
            blocks: vec![ContentBlock::Text { text: prompt }],
            usage: None,
        }]
        .into(),
    };

    let events = match client.stream(&request) {
        Ok(ev) => ev,
        Err(e) => {
            eprintln!(
                "  {} {}",
                crate::theme::error(crate::theme::ERR_GLYPH),
                crate::theme::error(&format!("codet: {model} request failed: {e}")),
            );
            return None;
        }
    };

    let mut code = String::new();
    for event in events {
        if let AssistantEvent::TextDelta(text) = event {
            code.push_str(&text);
        }
    }

    eprintln!(
        "  {} {}",
        crate::theme::dim(""),
        crate::theme::dim(&format!("codet: got {} chars of response", code.len())),
    );

    let code = strip_code_blocks(&code);
    if code.trim().is_empty() {
        eprintln!(
            "  {} {}",
            crate::theme::warn(crate::theme::WARN_GLYPH),
            crate::theme::warn("codet: response was empty after stripping fences"),
        );
        return None;
    }
    Some(code)
}

/// Ask the coder for surgical SEARCH/REPLACE patches instead of a full-file
/// rewrite. Returns `None` if the response contains no parseable blocks —
/// the caller falls back to `ask_coder_to_fix` for the full-regen path.
fn ask_coder_for_patches(
    file_content: &str,
    error_output: &str,
    references: &[ReferenceFile],
) -> Option<Vec<Patch>> {
    let model = coder_model();
    eprintln!(
        "  {} {}",
        crate::theme::dim(""),
        crate::theme::dim(&format!("codet: asking {model} for surgical patches...")),
    );

    let mut client = OllamaApiClient::new(model.clone(), json!([]))
        .with_context(coder_num_ctx())
        .with_max_predict(coder_num_predict());

    let reference_block = format_reference_block(references);
    let prompt = format!(
        "The following file has bug(s). Fix them by emitting SEARCH/REPLACE blocks.\n\n\
         ## File content\n```\n{file_content}\n```\n\n\
         ## Error output\n```\n{error_output}\n```{reference_block}\n\n\
         ## Output format\n\
         For each bug, emit EXACTLY this format (no markdown fences around the blocks):\n\n\
         <<<<<<< SEARCH\n\
         [text from the file to find — must match EXACTLY, whitespace included]\n\
         =======\n\
         [replacement text]\n\
         >>>>>>> REPLACE\n\n\
         Rules:\n\
         - SEARCH must match the file character-for-character (whitespace, newlines, indentation)\n\
         - Include JUST ENOUGH context so SEARCH is unique in the file — don't copy whole functions\n\
         - Emit one block per distinct bug; multiple blocks are fine\n\
         - NO commentary, NO explanations, NO markdown fences — only the blocks themselves"
    );

    let request = ApiRequest {
        system_prompt: vec![
            "You output SEARCH/REPLACE patch blocks only. No prose, no commentary, no fences."
                .to_string(),
        ],
        messages: vec![ConversationMessage {
            role: MessageRole::User,
            blocks: vec![ContentBlock::Text { text: prompt }],
            usage: None,
        }]
        .into(),
    };

    let events = match client.stream(&request) {
        Ok(ev) => ev,
        Err(e) => {
            eprintln!(
                "  {} {}",
                crate::theme::error(crate::theme::ERR_GLYPH),
                crate::theme::error(&format!("codet: {model} patch request failed: {e}")),
            );
            return None;
        }
    };

    let mut response = String::new();
    for event in events {
        if let AssistantEvent::TextDelta(text) = event {
            response.push_str(&text);
        }
    }

    let patches = parse_search_replace_blocks(&response);
    if patches.is_empty() {
        eprintln!(
            "  {} {}",
            crate::theme::warn(crate::theme::WARN_GLYPH),
            crate::theme::warn(&format!(
                "codet: no valid SEARCH/REPLACE blocks in {} chars of response",
                response.len()
            )),
        );
        return None;
    }
    eprintln!(
        "  {} {}",
        crate::theme::dim(""),
        crate::theme::dim(&format!(
            "codet: parsed {} surgical block(s) from {} chars",
            patches.len(),
            response.len()
        )),
    );
    Some(patches)
}

/// Parse SEARCH/REPLACE blocks from the coder's response. Tolerates surrounding
/// prose or partial malformed blocks — only well-formed triplets are accepted.
fn parse_search_replace_blocks(response: &str) -> Vec<Patch> {
    const SEARCH_MARK: &str = "<<<<<<< SEARCH";
    const SEP_MARK: &str = "=======";
    const REPLACE_MARK: &str = ">>>>>>> REPLACE";

    let mut patches = Vec::new();
    let mut cursor = 0usize;

    while cursor < response.len() {
        let Some(search_rel) = response[cursor..].find(SEARCH_MARK) else {
            break;
        };
        let search_start = cursor + search_rel + SEARCH_MARK.len();

        let Some(sep_rel) = response[search_start..].find(SEP_MARK) else {
            break;
        };
        let sep_start = search_start + sep_rel;
        let replace_content_start = sep_start + SEP_MARK.len();

        let Some(end_rel) = response[replace_content_start..].find(REPLACE_MARK) else {
            break;
        };
        let replace_end = replace_content_start + end_rel;

        let search = response[search_start..sep_start]
            .trim_start_matches(['\r', '\n'])
            .trim_end_matches(['\r', '\n'])
            .to_string();
        let replace = response[replace_content_start..replace_end]
            .trim_start_matches(['\r', '\n'])
            .trim_end_matches(['\r', '\n'])
            .to_string();

        if !search.is_empty() {
            patches.push(Patch { search, replace });
        }

        cursor = replace_end + REPLACE_MARK.len();
    }

    patches
}

/// Apply patches sequentially. Returns `None` if any SEARCH string is not
/// uniquely located in the current content — the caller then falls back to
/// full-regen so we never corrupt a file with a wrong-anchor replacement.
fn apply_patches(content: &str, patches: &[Patch]) -> Option<String> {
    let mut result = content.to_string();
    for patch in patches {
        // Exact match first.
        if let Some(idx) = find_unique(&result, &patch.search) {
            result = format!(
                "{}{}{}",
                &result[..idx],
                patch.replace,
                &result[idx + patch.search.len()..]
            );
            continue;
        }
        // Whitespace-tolerant fallback: normalize trailing whitespace per line
        // for both haystack and needle, try to find a match.
        if let Some((start, end)) = fuzzy_find(&result, &patch.search) {
            result = format!("{}{}{}", &result[..start], patch.replace, &result[end..]);
            continue;
        }
        // Could not locate the anchor — fail the whole patch set.
        return None;
    }
    Some(result)
}

/// Find `needle` in `haystack` and return its byte offset only if it appears
/// exactly once. Prevents accidental replacement when the anchor text is
/// repeated in the file.
fn find_unique(haystack: &str, needle: &str) -> Option<usize> {
    let first = haystack.find(needle)?;
    let second = haystack[first + needle.len()..].find(needle);
    if second.is_some() {
        None
    } else {
        Some(first)
    }
}

/// Whitespace-tolerant fallback: match the needle against the haystack after
/// normalizing trailing whitespace on each line. Returns (start, end) byte
/// offsets in the ORIGINAL haystack. Requires a unique match.
fn fuzzy_find(haystack: &str, needle: &str) -> Option<(usize, usize)> {
    let norm = |s: &str| -> String { s.lines().map(str::trim_end).collect::<Vec<_>>().join("\n") };
    let normalized_haystack = norm(haystack);
    let normalized_needle = norm(needle);
    if normalized_needle.is_empty() {
        return None;
    }

    let first = normalized_haystack.find(&normalized_needle)?;
    let second = normalized_haystack[first + normalized_needle.len()..].find(&normalized_needle);
    if second.is_some() {
        return None;
    }

    // Map normalized offset back to original by counting lines + per-line char delta.
    // Simpler approach: find the first line of the needle in haystack and count
    // matching lines from there.
    let needle_lines: Vec<&str> = normalized_needle.lines().collect();
    if needle_lines.is_empty() {
        return None;
    }
    let haystack_lines: Vec<&str> = haystack.lines().collect();
    let mut line_offsets: Vec<usize> = Vec::with_capacity(haystack_lines.len() + 1);
    let mut off = 0usize;
    for line in &haystack_lines {
        line_offsets.push(off);
        off += line.len() + 1; // +1 for '\n'
    }
    line_offsets.push(off);

    for (i, _) in haystack_lines.iter().enumerate() {
        if i + needle_lines.len() > haystack_lines.len() {
            break;
        }
        let mut ok = true;
        for (j, nline) in needle_lines.iter().enumerate() {
            if haystack_lines[i + j].trim_end() != *nline {
                ok = false;
                break;
            }
        }
        if ok {
            let start = line_offsets[i];
            let end = line_offsets[i + needle_lines.len()].saturating_sub(1);
            return Some((start, end.min(haystack.len())));
        }
    }
    None
}

/// Strip markdown code fences from the coder model's response. The model
/// is told "output ONLY the corrected code, no markdown" but small models
/// often ignore that and wrap anyway.
///
/// **Conservative line-start matching:** a ```` ``` ```` only counts as a
/// fence when it appears at the start of a line (beginning of the string or
/// after `\n`). This prevents false matches on triple backticks *inside*
/// code — e.g. `re.compile(r'```...```')` for matching Markdown fenced code
/// blocks used to get sliced in half by the previous naive substring scan.
///
/// Behavior:
/// - Response starts at a fence (with or without a language tag) → strip the
///   outer fence pair, return the content.
/// - Response has prose followed by a fenced block → extract the first
///   fenced block's content.
/// - Response is raw code with no line-start fence → returned as-is, even if
///   it contains triple backticks in string literals or comments.
/// - Multi-block responses → take the first block only; trailing blocks
///   (typically "here are tests" preamble + second fence) are discarded.
fn strip_code_blocks(s: &str) -> String {
    let trimmed = s.trim();
    if !trimmed.contains("```") {
        return trimmed.to_string();
    }

    // Find the opening fence: ``` at start-of-line (either index 0 or after \n).
    let Some(open_pos) = find_line_start_fence(trimmed, 0) else {
        // No fence at a line start — response isn't fence-wrapped. Leave alone
        // so we don't eat triple backticks embedded in code strings.
        return trimmed.to_string();
    };

    // Skip the opening ``` and optional language tag on the same line.
    let after_open = &trimmed[open_pos + 3..];
    let code_start = match after_open.find('\n') {
        Some(nl) => open_pos + 3 + nl + 1,
        None => {
            // Opener with no newline after (`\`\`\`code`) — unusual, strip opener and return.
            return after_open.trim().to_string();
        }
    };

    // Find the first closing fence that is ALSO at a line start.
    if let Some(close_pos) = find_line_start_fence(trimmed, code_start) {
        return trimmed[code_start..close_pos]
            .trim_end_matches('\n')
            .to_string();
    }

    // No line-start closer found — strip the opener and return the rest.
    trimmed[code_start..].trim().to_string()
}

/// Return the byte index of the next ```` ``` ```` that sits at the start of
/// a line (either at `from` if it's position 0 or immediately after a `\n`
/// at position `from`, or later after some newline), starting the search at
/// `from`. `None` if no such fence exists.
fn find_line_start_fence(s: &str, from: usize) -> Option<usize> {
    if from == 0 && s.starts_with("```") {
        return Some(0);
    }
    s[from..]
        .match_indices("\n```")
        .next()
        .map(|(i, _)| from + i + 1)
}

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

    #[test]
    fn strip_code_blocks_removes_python_fence() {
        let input = "```python\ndef greet():\n    return 'hi'\n```";
        assert_eq!(strip_code_blocks(input), "def greet():\n    return 'hi'");
    }

    #[test]
    fn strip_code_blocks_removes_bare_fence() {
        let input = "```\nx = 42\n```";
        assert_eq!(strip_code_blocks(input), "x = 42");
    }

    #[test]
    fn strip_code_blocks_noop_without_fences() {
        let input = "def greet():\n    return 'hi'";
        assert_eq!(strip_code_blocks(input), input);
    }

    #[test]
    fn strip_code_blocks_handles_trailing_whitespace() {
        let input = "  ```python\ncode\n```  ";
        assert_eq!(strip_code_blocks(input), "code");
    }

    #[test]
    fn strip_code_blocks_extracts_from_text_before_fence() {
        let input = "Here's the corrected code:\n```python\ndef greet():\n    return 'hi'\n```\n";
        assert_eq!(strip_code_blocks(input), "def greet():\n    return 'hi'");
    }

    #[test]
    fn strip_code_blocks_multi_block_takes_first() {
        // Multi-block responses: take the first fenced block only. Subsequent
        // prose + fences are ignored. (The old concatenating behavior was
        // replaced by line-start-only matching to avoid eating code-internal
        // triple backticks.)
        let input =
            "```bash\n#!/bin/bash\necho hello\n```\n\n# Test cases\n\n```bash\necho test\n```";
        assert_eq!(strip_code_blocks(input), "#!/bin/bash\necho hello");
    }

    #[test]
    fn strip_code_blocks_preserves_internal_triple_backticks() {
        // Regression (md2html.py): Python regex containing triple backticks
        // for matching Markdown fenced code blocks. Old logic sliced the code
        // at the first inner ``` and discarded the closing part of the regex.
        // New line-start rule leaves code with no outer fence completely
        // alone.
        let input = "import re\nPATTERN = re.compile(r'```(.*?)```', re.DOTALL)\nprint(PATTERN)";
        assert_eq!(strip_code_blocks(input), input);
    }

    #[test]
    fn strip_code_blocks_fenced_code_with_internal_backticks() {
        // Fenced wrapper around code that ALSO contains inner triple backticks.
        // Should strip outer fence pair; inner backticks (mid-line) ignored.
        let input = "```python\nimport re\nP = re.compile(r'```x```')\n```";
        assert_eq!(
            strip_code_blocks(input),
            "import re\nP = re.compile(r'```x```')"
        );
    }

    #[test]
    fn strip_code_blocks_extracts_from_text_before_and_after() {
        let input = "Fix applied:\n```python\nx = 42\n```\nThe bug was on line 3.";
        assert_eq!(strip_code_blocks(input), "x = 42");
    }

    #[test]
    fn count_failures_from_error_counts_fail_and_error_lines() {
        let error = "test_a ... ok\ntest_b ... FAIL\ntest_c ... ERROR\ntest_d ... ok\n";
        assert_eq!(count_failures_from_error(error), 2);
    }

    #[test]
    fn count_failures_from_error_returns_one_for_unknown_errors() {
        let error = "SyntaxError: invalid syntax\n";
        assert_eq!(count_failures_from_error(error), 1);
    }

    #[test]
    fn count_failures_from_error_returns_zero_for_empty() {
        assert_eq!(count_failures_from_error(""), 0);
    }

    #[test]
    fn validation_skips_non_code_files() {
        // A .txt file should return None (not validated).
        let path = Path::new("some-notes.txt");
        assert!(validate_code_file(path, &[]).is_none());
    }

    #[test]
    fn validation_skips_unknown_extensions() {
        let path = Path::new("data.csv");
        assert!(validate_code_file(path, &[]).is_none());
    }

    #[test]
    fn codet_result_to_json_contains_all_fields() {
        let result = CodetResult {
            syntax_ok: true,
            tests_found: true,
            tests_passed: 10,
            tests_failed: 1,
            tests_errors: 0,
            fixes_applied: 1,
            attempts_made: 2,
            fix_summary: "fixed test_get_age".to_string(),
            status: CodetStatus::FixedAll,
        };
        let j = result.to_json();
        assert_eq!(j["syntax_ok"], true);
        assert_eq!(j["tests_passed"], 10);
        assert_eq!(j["tests_failed"], 1);
        assert_eq!(j["fixes_applied"], 1);
        assert_eq!(j["attempts_made"], 2);
        assert!(j["status"].as_str().unwrap().contains("fixed_all"));
    }

    // ── Surgical fix loop: SEARCH/REPLACE patches ────────────────────────

    #[test]
    fn parse_single_search_replace_block() {
        let input = "<<<<<<< SEARCH\nfoo\n=======\nbar\n>>>>>>> REPLACE";
        let patches = parse_search_replace_blocks(input);
        assert_eq!(patches.len(), 1);
        assert_eq!(patches[0].search, "foo");
        assert_eq!(patches[0].replace, "bar");
    }

    #[test]
    fn parse_multiple_blocks() {
        let input = "<<<<<<< SEARCH\nfoo\n=======\nbar\n>>>>>>> REPLACE\n\n<<<<<<< SEARCH\nbaz\n=======\nqux\n>>>>>>> REPLACE";
        let patches = parse_search_replace_blocks(input);
        assert_eq!(patches.len(), 2);
        assert_eq!(patches[0].search, "foo");
        assert_eq!(patches[1].search, "baz");
    }

    #[test]
    fn parse_tolerates_surrounding_prose() {
        let input = "Here is the fix:\n<<<<<<< SEARCH\nold\n=======\nnew\n>>>>>>> REPLACE\nDone.";
        let patches = parse_search_replace_blocks(input);
        assert_eq!(patches.len(), 1);
        assert_eq!(patches[0].search, "old");
    }

    #[test]
    fn parse_rejects_missing_closing_mark() {
        let input = "<<<<<<< SEARCH\nfoo\n=======\nbar\n"; // no >>>>>>> REPLACE
        let patches = parse_search_replace_blocks(input);
        assert!(patches.is_empty());
    }

    #[test]
    fn parse_rejects_missing_separator() {
        let input = "<<<<<<< SEARCH\nfoo\n>>>>>>> REPLACE"; // no =======
        let patches = parse_search_replace_blocks(input);
        assert!(patches.is_empty());
    }

    #[test]
    fn parse_multiline_replacement() {
        let input = "<<<<<<< SEARCH\nfoo\n=======\nbar\nbaz\nqux\n>>>>>>> REPLACE";
        let patches = parse_search_replace_blocks(input);
        assert_eq!(patches.len(), 1);
        assert_eq!(patches[0].replace, "bar\nbaz\nqux");
    }

    #[test]
    fn apply_single_patch_replaces_exact_match() {
        let content = "def foo():\n    return x\n";
        let patches = vec![Patch {
            search: "return x".to_string(),
            replace: "return 42".to_string(),
        }];
        let result = apply_patches(content, &patches).unwrap();
        assert_eq!(result, "def foo():\n    return 42\n");
    }

    #[test]
    fn apply_multiple_patches_sequential() {
        let content = "a = 1\nb = 2\n";
        let patches = vec![
            Patch {
                search: "a = 1".to_string(),
                replace: "a = 10".to_string(),
            },
            Patch {
                search: "b = 2".to_string(),
                replace: "b = 20".to_string(),
            },
        ];
        let result = apply_patches(content, &patches).unwrap();
        assert_eq!(result, "a = 10\nb = 20\n");
    }

    #[test]
    fn apply_fails_when_search_missing() {
        let content = "a = 1\n";
        let patches = vec![Patch {
            search: "b = 2".to_string(),
            replace: "c = 3".to_string(),
        }];
        assert!(apply_patches(content, &patches).is_none());
    }

    #[test]
    fn apply_fails_on_non_unique_search() {
        // Safety: if the SEARCH anchor appears twice, we refuse to guess
        // which one to replace — caller falls back to full regen.
        let content = "x = 1\ny = 2\nx = 1\n";
        let patches = vec![Patch {
            search: "x = 1".to_string(),
            replace: "x = 99".to_string(),
        }];
        assert!(apply_patches(content, &patches).is_none());
    }

    #[test]
    fn apply_fuzzy_match_tolerates_trailing_whitespace() {
        // Haystack has trailing spaces on a line; needle doesn't. Should still match.
        let content = "def foo():    \n    return x\n";
        let patches = vec![Patch {
            search: "def foo():\n    return x".to_string(),
            replace: "def foo():\n    return 42".to_string(),
        }];
        let result = apply_patches(content, &patches).unwrap();
        assert!(result.contains("return 42"));
    }

    #[test]
    fn fixes_real_syntax_break_from_md2html() {
        // Regression: unterminated raw-string literal like 30b emitted on
        // md2html.py. A 1-block surgical fix should close the quote.
        let content = "patterns = {\n    'block_code': re.compile(r'\n    'paragraph': re.compile(r'.+'),\n}\n";
        let patches = vec![Patch {
            search: "'block_code': re.compile(r'".to_string(),
            replace: "'block_code': re.compile(r'```'),".to_string(),
        }];
        let result = apply_patches(content, &patches).unwrap();
        assert!(result.contains("```'),"));
    }
}