oxi-cli 0.1.4-alpha

Terminal-based AI coding assistant — multi-provider, streaming-first, extensible
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
//! Super-review skill for oxi
//!
//! Performs deep, honest system analysis that goes beyond superficial
//! pass/fail checklists. The skill:
//!
//! 1. **Understands the project first** — reads manifests, docs, structure
//!    to grasp the system's purpose before judging anything
//! 2. **Evaluates each domain against the system's purpose** — architecture
//!    isn't judged in isolation; it's judged by how well it serves the system
//! 3. **Produces honest assessments with evidence** — every claim cites
//!    specific code, every severity rating matches reality
//! 4. **Finds cross-cutting patterns** — connects dots across domains to
//!    surface issues invisible to per-domain analysis
//!
//! The module provides:
//! - A [`SuperReviewSession`] that tracks the review lifecycle
//! - Domain-specific analysis types ([`DomainAssessment`], [`Evidence`], etc.)
//! - A markdown renderer that produces the structured review document
//! - Project context gathering (manifest reading, directory walking)
//! - A skill prompt generator for the LLM-driven review workflow

use anyhow::{bail, Context, Result};
use chrono::Utc;
use serde::{Deserialize, Serialize};
use std::fmt;
use std::fs;
use std::path::{Path, PathBuf};

// ── Types ──────────────────────────────────────────────────────────────

/// The phase a super-review session is currently in.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
#[serde(rename_all = "snake_case")]
pub enum Phase {
    /// Reading project context to understand the system.
    Understand,
    /// Identifying relevant review domains.
    MapDomains,
    /// Deep analysis of each domain.
    Analyze,
    /// Looking for patterns that span domains.
    CrossCutting,
    /// Writing the review document.
    Produce,
    /// Review complete.
    Done,
}

impl Default for Phase {
    fn default() -> Self {
        Phase::Understand
    }
}

impl fmt::Display for Phase {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        match self {
            Phase::Understand => write!(f, "Understand"),
            Phase::MapDomains => write!(f, "Map Domains"),
            Phase::Analyze => write!(f, "Analyze"),
            Phase::CrossCutting => write!(f, "Cross-Cutting"),
            Phase::Produce => write!(f, "Produce"),
            Phase::Done => write!(f, "Done"),
        }
    }
}

/// Assessment rating for a domain.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
#[serde(rename_all = "snake_case")]
pub enum Rating {
    /// Purposeful, well-executed, couldn't reasonably be better.
    Excellent,
    /// Solid work, minor improvements possible.
    Good,
    /// Works, but has clear room for improvement.
    Adequate,
    /// Problems that will cause issues under real conditions.
    Concerning,
    /// Fundamental problems that undermine the system's purpose.
    Poor,
    /// Broken, dangerous, or actively harmful.
    Critical,
}

impl fmt::Display for Rating {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        match self {
            Rating::Excellent => write!(f, "Excellent"),
            Rating::Good => write!(f, "Good"),
            Rating::Adequate => write!(f, "Adequate"),
            Rating::Concerning => write!(f, "Concerning"),
            Rating::Poor => write!(f, "Poor"),
            Rating::Critical => write!(f, "Critical"),
        }
    }
}

/// Severity of a finding (concern or issue).
#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Serialize, Deserialize)]
#[serde(rename_all = "snake_case")]
pub enum Severity {
    /// Style, naming, minor polish.
    Nit,
    /// Clear improvement opportunity but not causing problems today.
    Minor,
    /// Will cause problems under real conditions.
    Important,
    /// Causes incorrect behavior, data loss risk, or security vulnerability.
    Critical,
}

impl fmt::Display for Severity {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        match self {
            Severity::Nit => write!(f, "nit"),
            Severity::Minor => write!(f, "minor"),
            Severity::Important => write!(f, "important"),
            Severity::Critical => write!(f, "critical"),
        }
    }
}

/// A piece of evidence pointing to specific code.
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct Evidence {
    /// File path (relative to project root).
    pub file: String,
    /// Optional line range (start, end).
    pub lines: Option<(usize, usize)>,
    /// What this evidence demonstrates.
    pub observation: String,
}

impl fmt::Display for Evidence {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        match self.lines {
            Some((start, end)) => write!(f, "`{}:{}-{}` — {}", self.file, start, end, self.observation),
            None => write!(f, "`{}` — {}", self.file, self.observation),
        }
    }
}

/// A single finding within a domain assessment (strength or concern).
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct Finding {
    /// What was found.
    pub description: String,
    /// Severity (for concerns) or relevance (for strengths).
    pub severity: Severity,
    /// Supporting evidence.
    pub evidence: Vec<Evidence>,
}

/// Assessment of a single review domain.
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct DomainAssessment {
    /// Domain name (e.g., "Error Handling", "Architecture").
    pub domain: String,
    /// Why this domain matters for THIS system.
    pub relevance: String,
    /// Overall rating.
    pub rating: Rating,
    /// Detailed analysis text.
    pub analysis: String,
    /// Things the system does well in this domain.
    pub strengths: Vec<Finding>,
    /// Problems or concerns in this domain.
    pub concerns: Vec<Finding>,
    /// Direct evidence citations.
    pub evidence: Vec<Evidence>,
}

/// A cross-cutting finding that spans multiple domains.
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct CrossCuttingFinding {
    /// Title of the finding.
    pub title: String,
    /// Description of the pattern or issue.
    pub description: String,
    /// Which domains this connects.
    pub connected_domains: Vec<String>,
    /// Evidence from multiple domains.
    pub evidence: Vec<Evidence>,
    /// How important is this?
    pub severity: Severity,
}

/// A ranked recommendation from the review.
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct Recommendation {
    /// What to do.
    pub action: String,
    /// Why it matters.
    pub rationale: String,
    /// Estimated effort (e.g., "2 hours", "1 sprint").
    pub effort: String,
    /// Which domains or findings this addresses.
    pub addresses: Vec<String>,
    /// Priority ordering (lower = higher priority).
    pub priority: u32,
}

/// The system-level understanding captured during Phase 1.
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct SystemUnderstanding {
    /// What problem does this system solve?
    pub purpose: String,
    /// Who are the primary users?
    pub users: String,
    /// What promises does the system make?
    pub core_contract: String,
    /// What workflows MUST work?
    pub critical_paths: Vec<String>,
    /// Hard constraints (performance, security, compatibility).
    pub constraints: Vec<String>,
    /// Free-form context notes.
    pub notes: String,
}

/// Project context gathered from the filesystem.
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct ProjectContext {
    /// Project name from manifest.
    pub name: String,
    /// Version from manifest.
    pub version: String,
    /// Language / ecosystem.
    pub language: String,
    /// Top-level directory structure.
    pub directory_structure: String,
    /// Key files read and their summaries.
    pub key_files: Vec<(String, String)>,
    /// Dependencies found.
    pub dependencies: Vec<String>,
    /// Has tests directory.
    pub has_tests: bool,
    /// Has CI configuration.
    pub has_ci: bool,
    /// Has documentation directory.
    pub has_docs: bool,
}

/// The complete super-review report.
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct SuperReviewReport {
    /// Report metadata.
    pub meta: ReportMeta,
    /// System understanding from Phase 1.
    pub system_understanding: SystemUnderstanding,
    /// Project context from Phase 1.
    pub project_context: ProjectContext,
    /// Domains selected and their analyses.
    pub domains: Vec<DomainAssessment>,
    /// Cross-cutting findings.
    pub cross_cutting: Vec<CrossCuttingFinding>,
    /// What's genuinely working well.
    pub working_well: Vec<String>,
    /// What needs attention, ranked.
    pub needs_attention: Vec<String>,
    /// What's at risk.
    pub at_risk: Vec<String>,
    /// The bottom-line verdict.
    pub verdict: String,
    /// Ordered recommendations.
    pub recommendations: Vec<Recommendation>,
}

/// Metadata for a review report.
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct ReportMeta {
    /// ISO date string (YYYY-MM-DD).
    pub date: String,
    /// URL-friendly slug.
    pub slug: String,
    /// What was reviewed.
    pub scope: String,
    /// Reviewer identifier.
    pub reviewer: String,
}

// ── Session ──────────────────────────────────────────────────────────

/// A super-review session that tracks the full review lifecycle.
///
/// The session progresses through phases: Understand → MapDomains →
/// Analyze → CrossCutting → Produce → Done. Each phase builds on the
/// previous one, and the final report is rendered from the accumulated
/// data.
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct SuperReviewSession {
    /// Current phase.
    pub phase: Phase,
    /// Optional project root directory.
    pub project_root: Option<PathBuf>,
    /// Optional scope description (what's being reviewed).
    pub scope: Option<String>,
    /// System understanding (populated in Phase 1).
    pub system_understanding: Option<SystemUnderstanding>,
    /// Project context (populated in Phase 1).
    pub project_context: Option<ProjectContext>,
    /// Domain assessments (populated in Phase 3).
    pub domains: Vec<DomainAssessment>,
    /// Cross-cutting findings (populated in Phase 4).
    pub cross_cutting: Vec<CrossCuttingFinding>,
    /// Working well (populated in Phase 5).
    pub working_well: Vec<String>,
    /// Needs attention (populated in Phase 5).
    pub needs_attention: Vec<String>,
    /// At risk (populated in Phase 5).
    pub at_risk: Vec<String>,
    /// Verdict (populated in Phase 5).
    pub verdict: Option<String>,
    /// Recommendations (populated in Phase 5).
    pub recommendations: Vec<Recommendation>,
}

impl SuperReviewSession {
    /// Create a new super-review session.
    pub fn new() -> Self {
        Self {
            phase: Phase::Understand,
            project_root: None,
            scope: None,
            system_understanding: None,
            project_context: None,
            domains: Vec::new(),
            cross_cutting: Vec::new(),
            working_well: Vec::new(),
            needs_attention: Vec::new(),
            at_risk: Vec::new(),
            verdict: None,
            recommendations: Vec::new(),
        }
    }

    /// Set the project root directory.
    pub fn with_project_root(mut self, root: impl Into<PathBuf>) -> Self {
        self.project_root = Some(root.into());
        self
    }

    /// Set the review scope.
    pub fn with_scope(mut self, scope: impl Into<String>) -> Self {
        self.scope = Some(scope.into());
        self
    }

    /// Advance to the next phase.
    ///
    /// Returns an error if trying to advance past `Done`.
    pub fn advance(&mut self) -> Result<()> {
        let next = match self.phase {
            Phase::Understand => Phase::MapDomains,
            Phase::MapDomains => Phase::Analyze,
            Phase::Analyze => Phase::CrossCutting,
            Phase::CrossCutting => Phase::Produce,
            Phase::Produce => Phase::Done,
            Phase::Done => bail!("Cannot advance past Done"),
        };
        self.phase = next;
        Ok(())
    }

    /// Set the phase directly.
    pub fn set_phase(&mut self, phase: Phase) {
        self.phase = phase;
    }

    /// Set the system understanding (Phase 1).
    pub fn set_system_understanding(&mut self, understanding: SystemUnderstanding) {
        self.system_understanding = Some(understanding);
    }

    /// Set the project context (Phase 1).
    pub fn set_project_context(&mut self, context: ProjectContext) {
        self.project_context = Some(context);
    }

    /// Add a domain assessment (Phase 3).
    pub fn add_domain(&mut self, assessment: DomainAssessment) {
        self.domains.push(assessment);
    }

    /// Add a cross-cutting finding (Phase 4).
    pub fn add_cross_cutting(&mut self, finding: CrossCuttingFinding) {
        self.cross_cutting.push(finding);
    }

    /// Set the working-well items (Phase 5).
    pub fn set_working_well(&mut self, items: Vec<String>) {
        self.working_well = items;
    }

    /// Set the needs-attention items (Phase 5).
    pub fn set_needs_attention(&mut self, items: Vec<String>) {
        self.needs_attention = items;
    }

    /// Set the at-risk items (Phase 5).
    pub fn set_at_risk(&mut self, items: Vec<String>) {
        self.at_risk = items;
    }

    /// Set the verdict (Phase 5).
    pub fn set_verdict(&mut self, verdict: impl Into<String>) {
        self.verdict = Some(verdict.into());
    }

    /// Add a recommendation (Phase 5).
    pub fn add_recommendation(&mut self, rec: Recommendation) {
        self.recommendations.push(rec);
    }

    /// Sort recommendations by priority.
    pub fn sort_recommendations(&mut self) {
        self.recommendations.sort_by_key(|r| r.priority);
    }

    /// Number of domain assessments.
    pub fn domain_count(&self) -> usize {
        self.domains.len()
    }

    /// Number of cross-cutting findings.
    pub fn cross_cutting_count(&self) -> usize {
        self.cross_cutting.len()
    }

    /// Gather project context from the filesystem.
    ///
    /// Reads the project manifest, directory structure, and key files.
    /// Requires `project_root` to be set.
    pub fn gather_project_context(&self, max_bytes: usize) -> Result<ProjectContext> {
        let root = self
            .project_root
            .as_ref()
            .context("Project root not set — call with_project_root() first")?;

        let mut name = String::from("unknown");
        let mut version = String::from("0.0.0");
        let mut language = String::from("unknown");
        let mut key_files = Vec::new();
        let mut dependencies = Vec::new();
        let has_tests;
        let has_ci;
        let mut has_docs = false;

        // Detect Rust project
        let cargo_toml = root.join("Cargo.toml");
        if cargo_toml.exists() {
            language = "Rust".to_string();
            if let Ok(content) = fs::read_to_string(&cargo_toml) {
                name = extract_toml_value(&content, "name").unwrap_or_else(|| "unknown".to_string());
                version = extract_toml_value(&content, "version").unwrap_or_else(|| "0.0.0".to_string());
                key_files.push(("Cargo.toml".to_string(), summarize_cargo_toml(&content)));
                Self::extract_deps_from_cargo(&content, &mut dependencies);
            }
        }

        // Detect Node.js project
        let package_json = root.join("package.json");
        if package_json.exists() && language == "unknown" {
            language = "JavaScript/TypeScript".to_string();
            if let Ok(content) = fs::read_to_string(&package_json) {
                if let Ok(json) = serde_json::from_str::<serde_json::Value>(&content) {
                    name = json
                        .get("name")
                        .and_then(|v| v.as_str())
                        .unwrap_or("unknown")
                        .to_string();
                    version = json
                        .get("version")
                        .and_then(|v| v.as_str())
                        .unwrap_or("0.0.0")
                        .to_string();
                    key_files.push(("package.json".to_string(), summarize_package_json(&content)));
                }
            }
        }

        // Read README if present
        for readme_name in &["README.md", "README.txt", "README"] {
            let readme_path = root.join(readme_name);
            if readme_path.exists() {
                if let Ok(content) = fs::read_to_string(&readme_path) {
                    let summary = truncate_str(&content, 500);
                    key_files.push((readme_name.to_string(), summary));
                }
                break;
            }
        }

        // Read design docs
        let docs_dir = root.join("docs");
        if docs_dir.is_dir() {
            has_docs = true;
            if let Ok(entries) = fs::read_dir(&docs_dir) {
                for entry in entries.flatten() {
                    let path = entry.path();
                    if path.extension().map_or(false, |e| e == "md") {
                        let file_name = path.file_name().unwrap_or_default().to_string_lossy().to_string();
                        if let Ok(content) = fs::read_to_string(&path) {
                            key_files.push((
                                format!("docs/{}", file_name),
                                truncate_str(&content, 300),
                            ));
                        }
                    }
                }
            }
        }

        // Read AGENTS.md if present
        let agents_md = root.join("AGENTS.md");
        if agents_md.exists() {
            if let Ok(content) = fs::read_to_string(&agents_md) {
                key_files.push(("AGENTS.md".to_string(), truncate_str(&content, 500)));
            }
        }

        // Check for tests and CI
        has_tests = root.join("tests").is_dir()
            || root.join("src").join("test").is_dir()
            || root.join("test").is_dir();
        has_ci = root.join(".github").join("workflows").is_dir()
            || root.join(".gitlab-ci.yml").exists()
            || root.join("Jenkinsfile").exists();

        // Build directory structure
        let directory_structure = Self::build_directory_tree(root, 0, 3, max_bytes / 4);

        // Truncate key_files to fit budget
        let mut total_bytes = directory_structure.len();
        key_files.retain(|(_, content)| {
            total_bytes += content.len();
            total_bytes < max_bytes
        });

        Ok(ProjectContext {
            name,
            version,
            language,
            directory_structure,
            key_files,
            dependencies,
            has_tests,
            has_ci,
            has_docs,
        })
    }

    /// Build a text representation of the directory tree.
    fn build_directory_tree(dir: &Path, depth: usize, max_depth: usize, max_bytes: usize) -> String {
        if depth > max_depth {
            return "  ".repeat(depth) + "...\n";
        }

        let mut tree = String::new();
        let indent = "  ".repeat(depth);

        let Ok(entries) = fs::read_dir(dir) else {
            return tree;
        };

        let mut entries: Vec<_> = entries
            .filter_map(|e| e.ok())
            .filter(|e| {
                let name = e.file_name().to_string_lossy().to_string();
                // Skip hidden dirs and noise
                !name.starts_with('.')
                    && name != "target"
                    && name != "node_modules"
                    && name != "__pycache__"
                    && name != "dist"
                    && name != "build"
                    && name != "vendor"
                    && name != "coverage"
                    && name != ".git"
            })
            .collect();

        entries.sort_by_key(|e| {
            let is_dir = e.path().is_dir();
            // Directories first, then files
            (!is_dir, e.file_name().to_string_lossy().to_string())
        });

        for entry in entries {
            let name = entry.file_name().to_string_lossy().to_string();
            let path = entry.path();

            if path.is_dir() {
                tree.push_str(&format!("{}{}/\n", indent, name));
                let subtree = Self::build_directory_tree(&path, depth + 1, max_depth, max_bytes);
                if tree.len() + subtree.len() > max_bytes {
                    tree.push_str(&format!("{}  ...\n", indent));
                    break;
                }
                tree.push_str(&subtree);
            } else {
                tree.push_str(&format!("{}{}\n", indent, name));
            }

            if tree.len() > max_bytes {
                tree.push_str(&format!("{}...\n", indent));
                break;
            }
        }

        tree
    }

    /// Extract dependency names from a Cargo.toml.
    fn extract_deps_from_cargo(content: &str, deps: &mut Vec<String>) {
        let mut in_deps = false;
        for line in content.lines() {
            let trimmed = line.trim();
            if trimmed == "[dependencies]" || trimmed == "[dev-dependencies]" {
                in_deps = true;
                continue;
            }
            if trimmed.starts_with('[') {
                in_deps = false;
                continue;
            }
            if in_deps {
                if let Some((name, _)) = trimmed.split_once('=') {
                    deps.push(name.trim().to_string());
                } else if trimmed.ends_with(",\"workspace\"") || trimmed.contains("workspace = true") {
                    // Workspace dependency — extract name from key
                    if let Some((name, _)) = trimmed.split_once('=') {
                        deps.push(format!("{} (workspace)", name.trim()));
                    }
                }
            }
        }
    }

    /// Render the review as a markdown string.
    pub fn render_markdown(&self) -> Result<String> {
        let system = self.system_understanding.as_ref()
            .context("System understanding not set — complete Phase 1 first")?;
        let context = self.project_context.as_ref()
            .context("Project context not set — complete Phase 1 first")?;

        if self.domains.is_empty() {
            bail!("No domain assessments — complete Phase 3 first");
        }

        let mut md = String::with_capacity(8192);

        // Title and metadata
        let scope = self.scope.as_deref().unwrap_or(&context.name);
        let date = Utc::now().format("%Y-%m-%d").to_string();
        md.push_str(&format!("# Super-Review: {}\n\n", context.name));
        md.push_str(&format!("> Date: {}\n", date));
        md.push_str(&format!("> Scope: {}\n", scope));
        md.push_str("> Reviewer: oxi super-review\n\n");

        // System Understanding
        md.push_str("## System Understanding\n\n");
        md.push_str(&system.purpose);
        md.push_str("\n\n");
        if !system.users.is_empty() {
            md.push_str(&format!("**Users:** {}\n\n", system.users));
        }
        if !system.core_contract.is_empty() {
            md.push_str(&format!("**Core Contract:** {}\n\n", system.core_contract));
        }
        if !system.critical_paths.is_empty() {
            md.push_str("**Critical Paths:**\n");
            for path in &system.critical_paths {
                md.push_str(&format!("- {}\n", path));
            }
            md.push('\n');
        }
        if !system.constraints.is_empty() {
            md.push_str("**Constraints:**\n");
            for constraint in &system.constraints {
                md.push_str(&format!("- {}\n", constraint));
            }
            md.push('\n');
        }
        if !system.notes.is_empty() {
            md.push_str(&system.notes);
            md.push_str("\n\n");
        }

        // Domain Assessments
        md.push_str("## Domain Assessments\n\n");
        for domain in &self.domains {
            md.push_str(&format!("### {}{}\n\n", domain.domain, domain.rating));
            md.push_str(&domain.analysis);
            md.push_str("\n\n");

            if !domain.strengths.is_empty() {
                md.push_str("**Strengths:**\n");
                for strength in &domain.strengths {
                    md.push_str(&format!("- {}\n", strength.description));
                    for ev in &strength.evidence {
                        md.push_str(&format!("  - {}\n", ev));
                    }
                }
                md.push('\n');
            }

            if !domain.concerns.is_empty() {
                md.push_str("**Concerns:**\n");
                for concern in &domain.concerns {
                    md.push_str(&format!("- [{}] {}\n", concern.severity, concern.description));
                    for ev in &concern.evidence {
                        md.push_str(&format!("  - {}\n", ev));
                    }
                }
                md.push('\n');
            }

            if !domain.evidence.is_empty() {
                md.push_str("**Evidence:**\n");
                for ev in &domain.evidence {
                    md.push_str(&format!("- {}\n", ev));
                }
                md.push_str("\n---\n\n");
            }
        }

        // Cross-Cutting Findings
        if !self.cross_cutting.is_empty() {
            md.push_str("## Cross-Cutting Findings\n\n");
            for finding in &self.cross_cutting {
                md.push_str(&format!("### {}\n\n", finding.title));
                md.push_str(&finding.description);
                md.push_str("\n\n");
                md.push_str(&format!(
                    "**Connected domains:** {}\n",
                    finding.connected_domains.join(", ")
                ));
                md.push_str(&format!("**Severity:** {}\n\n", finding.severity));
                if !finding.evidence.is_empty() {
                    md.push_str("**Evidence:**\n");
                    for ev in &finding.evidence {
                        md.push_str(&format!("- {}\n", ev));
                    }
                    md.push('\n');
                }
            }
        }

        // System-Level Assessment
        md.push_str("## System-Level Assessment\n\n");

        if !self.working_well.is_empty() {
            md.push_str("### What's Working Well\n\n");
            for item in &self.working_well {
                md.push_str(&format!("- {}\n", item));
            }
            md.push('\n');
        }

        if !self.needs_attention.is_empty() {
            md.push_str("### What Needs Attention\n\n");
            for (i, item) in self.needs_attention.iter().enumerate() {
                md.push_str(&format!("{}. {}\n", i + 1, item));
            }
            md.push('\n');
        }

        if !self.at_risk.is_empty() {
            md.push_str("### What's At Risk\n\n");
            for item in &self.at_risk {
                md.push_str(&format!("- {}\n", item));
            }
            md.push('\n');
        }

        // Verdict
        if let Some(ref verdict) = self.verdict {
            md.push_str("## Verdict\n\n");
            md.push_str(verdict);
            md.push_str("\n\n");
        }

        // Recommendations
        if !self.recommendations.is_empty() {
            md.push_str("## Recommendations\n\n");
            for (i, rec) in self.recommendations.iter().enumerate() {
                md.push_str(&format!(
                    "{}. **{}** (effort: {})\n   {}\n",
                    i + 1,
                    rec.action,
                    rec.effort,
                    rec.rationale,
                ));
                if !rec.addresses.is_empty() {
                    md.push_str(&format!(
                        "   Addresses: {}\n",
                        rec.addresses.join(", ")
                    ));
                }
            }
            md.push('\n');
        }

        Ok(md)
    }

    /// Write the review document to disk.
    ///
    /// Creates the output directory if needed. Returns the path to the written file.
    pub fn write_document(&self, explicit_path: Option<&Path>) -> Result<PathBuf> {
        if self.system_understanding.is_none() {
            bail!("System understanding not set — complete Phase 1 first");
        }
        if self.domains.is_empty() {
            bail!("No domain assessments — complete Phase 3 first");
        }

        let md = self.render_markdown()?;

        let path = if let Some(explicit) = explicit_path {
            explicit.to_path_buf()
        } else {
            let root = self.project_root.as_ref()
                .context("Project root not set and no explicit path given")?;
            let date = Utc::now().format("%Y-%m-%d").to_string();
            let scope_slug = self.scope.as_deref().unwrap_or("review");
            let slug = slugify(scope_slug);
            let dir = root.join("docs").join("review");
            fs::create_dir_all(&dir)
                .with_context(|| format!("Failed to create directory: {}", dir.display()))?;
            dir.join(format!("{}-{}.md", date, slug))
        };

        // Ensure parent directory exists
        if let Some(parent) = path.parent() {
            fs::create_dir_all(parent)
                .with_context(|| format!("Failed to create directory: {}", parent.display()))?;
        }

        fs::write(&path, &md)
            .with_context(|| format!("Failed to write review to {}", path.display()))?;

        tracing::info!("Super-review written to {}", path.display());
        Ok(path)
    }
}

impl Default for SuperReviewSession {
    fn default() -> Self {
        Self::new()
    }
}

// ── Skill Prompt ─────────────────────────────────────────────────────

/// Generate the system-prompt fragment for the super-review skill.
///
/// This is appended to the base system prompt when the super-review
/// skill is active, telling the LLM how to conduct the deep analysis.
pub fn super_review_skill_prompt() -> String {
    include_str!("super_review_prompt.md").to_string()
}

// ── Helpers ────────────────────────────────────────────────────────────

/// Convert a string into a filesystem-safe slug.
fn slugify(s: &str) -> String {
    s.to_lowercase()
        .chars()
        .map(|c| {
            if c.is_ascii_alphanumeric() {
                c
            } else if c == ' ' || c == '_' || c == '-' {
                '-'
            } else {
                '\0'
            }
        })
        .filter(|c| *c != '\0')
        .collect::<String>()
        .trim_matches('-')
        .to_string()
}

/// Extract a value from a simple TOML `key = "value"` line.
fn extract_toml_value(content: &str, key: &str) -> Option<String> {
    let prefix = format!("{} = \"", key);
    for line in content.lines() {
        let trimmed = line.trim();
        if let Some(rest) = trimmed.strip_prefix(&prefix) {
            if let Some(end) = rest.find('"') {
                return Some(rest[..end].to_string());
            }
        }
        // Also handle `key = { ... name = "..." ... }` patterns (for workspace inheritance)
        if trimmed.starts_with(&format!("{} = {{", key)) {
            // Look for name within the braces
            let name_prefix = "name = \"";
            if let Some(idx) = trimmed.find(name_prefix) {
                let after = &trimmed[idx + name_prefix.len()..];
                if let Some(end) = after.find('"') {
                    return Some(after[..end].to_string());
                }
            }
        }
    }
    None
}

/// Truncate a string to at most `max_len` characters, appending "..." if truncated.
fn truncate_str(s: &str, max_len: usize) -> String {
    if s.len() <= max_len {
        s.to_string()
    } else {
        let mut end = max_len;
        // Try to break at a word boundary
        if let Some(pos) = s[..max_len].rfind(' ') {
            if pos > max_len / 2 {
                end = pos;
            }
        }
        format!("{}...", &s[..end])
    }
}

/// Summarize a Cargo.toml for the project context.
fn summarize_cargo_toml(content: &str) -> String {
    let mut summary = String::new();
    if let Some(name) = extract_toml_value(content, "name") {
        summary.push_str(&format!("Rust crate: {}", name));
    }
    if let Some(version) = extract_toml_value(content, "version") {
        summary.push_str(&format!(" v{}", version));
    }
    if let Some(edition) = extract_toml_value(content, "edition") {
        summary.push_str(&format!(" (edition {})", edition));
    }
    if summary.is_empty() {
        summary = "Rust project".to_string();
    }
    summary
}

/// Summarize a package.json for the project context.
fn summarize_package_json(content: &str) -> String {
    let mut summary = String::from("Node.js project");
    if let Ok(json) = serde_json::from_str::<serde_json::Value>(content) {
        if let Some(desc) = json.get("description").and_then(|v| v.as_str()) {
            summary.push_str(&format!(": {}", desc));
        }
    }
    summary
}

// ── Tests ────────────────────────────────────────────────────────────

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

    // ── Phase & Rating display ──────────────────────────────────

    #[test]
    fn test_phase_display() {
        assert_eq!(format!("{}", Phase::Understand), "Understand");
        assert_eq!(format!("{}", Phase::MapDomains), "Map Domains");
        assert_eq!(format!("{}", Phase::Analyze), "Analyze");
        assert_eq!(format!("{}", Phase::CrossCutting), "Cross-Cutting");
        assert_eq!(format!("{}", Phase::Produce), "Produce");
        assert_eq!(format!("{}", Phase::Done), "Done");
    }

    #[test]
    fn test_rating_display() {
        assert_eq!(format!("{}", Rating::Excellent), "Excellent");
        assert_eq!(format!("{}", Rating::Good), "Good");
        assert_eq!(format!("{}", Rating::Adequate), "Adequate");
        assert_eq!(format!("{}", Rating::Concerning), "Concerning");
        assert_eq!(format!("{}", Rating::Poor), "Poor");
        assert_eq!(format!("{}", Rating::Critical), "Critical");
    }

    #[test]
    fn test_severity_display() {
        assert_eq!(format!("{}", Severity::Nit), "nit");
        assert_eq!(format!("{}", Severity::Minor), "minor");
        assert_eq!(format!("{}", Severity::Important), "important");
        assert_eq!(format!("{}", Severity::Critical), "critical");
    }

    #[test]
    fn test_severity_ordering() {
        assert!(Severity::Critical > Severity::Important);
        assert!(Severity::Important > Severity::Minor);
        assert!(Severity::Minor > Severity::Nit);
    }

    // ── Session lifecycle ──────────────────────────────────────

    #[test]
    fn test_session_new() {
        let session = SuperReviewSession::new();
        assert_eq!(session.phase, Phase::Understand);
        assert!(session.project_root.is_none());
        assert!(session.domains.is_empty());
        assert!(session.cross_cutting.is_empty());
        assert!(session.recommendations.is_empty());
    }

    #[test]
    fn test_session_with_project_root() {
        let session = SuperReviewSession::new().with_project_root("/tmp/project");
        assert_eq!(session.project_root, Some(PathBuf::from("/tmp/project")));
    }

    #[test]
    fn test_session_with_scope() {
        let session = SuperReviewSession::new().with_scope("API layer");
        assert_eq!(session.scope, Some("API layer".to_string()));
    }

    #[test]
    fn test_phase_advance() {
        let mut session = SuperReviewSession::new();
        assert_eq!(session.phase, Phase::Understand);

        session.advance().unwrap();
        assert_eq!(session.phase, Phase::MapDomains);

        session.advance().unwrap();
        assert_eq!(session.phase, Phase::Analyze);

        session.advance().unwrap();
        assert_eq!(session.phase, Phase::CrossCutting);

        session.advance().unwrap();
        assert_eq!(session.phase, Phase::Produce);

        session.advance().unwrap();
        assert_eq!(session.phase, Phase::Done);

        // Cannot advance past Done
        assert!(session.advance().is_err());
    }

    #[test]
    fn test_set_phase() {
        let mut session = SuperReviewSession::new();
        session.set_phase(Phase::Analyze);
        assert_eq!(session.phase, Phase::Analyze);
    }

    // ── Domain assessment ──────────────────────────────────────

    #[test]
    fn test_add_domain() {
        let mut session = SuperReviewSession::new();
        session.add_domain(DomainAssessment {
            domain: "Error Handling".to_string(),
            relevance: "Critical for CLI tool reliability".to_string(),
            rating: Rating::Good,
            analysis: "Errors are propagated with context".to_string(),
            strengths: vec![Finding {
                description: "Uses anyhow::Context consistently".to_string(),
                severity: Severity::Nit,
                evidence: vec![Evidence {
                    file: "src/main.rs".to_string(),
                    lines: Some((42, 50)),
                    observation: "All file reads use .context()".to_string(),
                }],
            }],
            concerns: vec![Finding {
                description: "Some unwrap() calls in non-test code".to_string(),
                severity: Severity::Minor,
                evidence: vec![Evidence {
                    file: "src/session.rs".to_string(),
                    lines: Some((100, 100)),
                    observation: "Direct unwrap() on Option".to_string(),
                }],
            }],
            evidence: vec![],
        });

        assert_eq!(session.domain_count(), 1);
        assert_eq!(session.domains[0].domain, "Error Handling");
        assert_eq!(session.domains[0].rating, Rating::Good);
    }

    #[test]
    fn test_add_cross_cutting() {
        let mut session = SuperReviewSession::new();
        session.add_cross_cutting(CrossCuttingFinding {
            title: "Error handling ignores module boundaries".to_string(),
            description: "Architecture defines clean boundaries but error handling leaks internals".to_string(),
            connected_domains: vec!["Architecture".to_string(), "Error Handling".to_string()],
            evidence: vec![],
            severity: Severity::Important,
        });
        assert_eq!(session.cross_cutting_count(), 1);
    }

    #[test]
    fn test_recommendations() {
        let mut session = SuperReviewSession::new();
        session.add_recommendation(Recommendation {
            action: "Add input validation layer".to_string(),
            rationale: "User inputs reach business logic without validation".to_string(),
            effort: "4 hours".to_string(),
            addresses: vec!["Security".to_string(), "Correctness".to_string()],
            priority: 1,
        });
        session.add_recommendation(Recommendation {
            action: "Document public API".to_string(),
            rationale: "Public functions lack doc comments".to_string(),
            effort: "1 sprint".to_string(),
            addresses: vec!["Documentation".to_string()],
            priority: 3,
        });
        session.add_recommendation(Recommendation {
            action: "Add integration tests".to_string(),
            rationale: "Only unit tests exist".to_string(),
            effort: "2 days".to_string(),
            addresses: vec!["Testing".to_string()],
            priority: 2,
        });

        session.sort_recommendations();
        assert_eq!(session.recommendations[0].action, "Add input validation layer");
        assert_eq!(session.recommendations[1].action, "Add integration tests");
        assert_eq!(session.recommendations[2].action, "Document public API");
    }

    // ── Project context gathering ──────────────────────────────

    #[test]
    fn test_gather_project_context_no_root() {
        let session = SuperReviewSession::new();
        assert!(session.gather_project_context(10000).is_err());
    }

    #[test]
    fn test_gather_project_context_rust() {
        let tmp = tempfile::tempdir().unwrap();
        let src = tmp.path().join("src");
        let tests = tmp.path().join("tests");
        let docs = tmp.path().join("docs");
        fs::create_dir_all(&src).unwrap();
        fs::create_dir_all(&tests).unwrap();
        fs::create_dir_all(&docs).unwrap();
        fs::write(
            tmp.path().join("Cargo.toml"),
            r#"[package]
name = "test-project"
version = "0.2.0"
edition = "2021"

[dependencies]
serde = { version = "1", features = ["derive"] }
tokio = "1"
anyhow = "1"

[dev-dependencies]
tempfile = "3"
"#,
        )
        .unwrap();
        fs::write(src.join("main.rs"), "fn main() {}").unwrap();
        fs::write(tmp.path().join("README.md"), "# Test Project\nA test project.").unwrap();

        let session = SuperReviewSession::new().with_project_root(tmp.path());
        let ctx = session.gather_project_context(10000).unwrap();

        assert_eq!(ctx.name, "test-project");
        assert_eq!(ctx.version, "0.2.0");
        assert_eq!(ctx.language, "Rust");
        assert!(ctx.has_tests);
        assert!(ctx.has_docs);
        assert!(ctx.dependencies.iter().any(|d| d == "serde"));
        assert!(ctx.dependencies.iter().any(|d| d == "tokio"));
        assert!(ctx.dependencies.iter().any(|d| d == "anyhow"));
        assert!(ctx.directory_structure.contains("src"));
    }

    #[test]
    fn test_gather_project_context_node() {
        let tmp = tempfile::tempdir().unwrap();
        let src = tmp.path().join("src");
        fs::create_dir_all(&src).unwrap();
        fs::write(
            tmp.path().join("package.json"),
            r#"{"name": "test-app", "version": "1.0.0", "description": "A test app", "dependencies": {"express": "^4.18"}}"#,
        )
        .unwrap();

        let session = SuperReviewSession::new().with_project_root(tmp.path());
        let ctx = session.gather_project_context(10000).unwrap();

        assert_eq!(ctx.name, "test-app");
        assert_eq!(ctx.language, "JavaScript/TypeScript");
    }

    #[test]
    fn test_gather_project_context_ci() {
        let tmp = tempfile::tempdir().unwrap();
        let workflows = tmp.path().join(".github").join("workflows");
        fs::create_dir_all(&workflows).unwrap();
        fs::write(
            tmp.path().join("Cargo.toml"),
            "[package]\nname = \"ci-test\"\nversion = \"0.1.0\"\n",
        )
        .unwrap();
        fs::write(workflows.join("ci.yml"), "name: CI\non: push\n").unwrap();

        let session = SuperReviewSession::new().with_project_root(tmp.path());
        let ctx = session.gather_project_context(10000).unwrap();
        assert!(ctx.has_ci);
    }

    // ── Markdown rendering ─────────────────────────────────────

    fn minimal_session() -> SuperReviewSession {
        let mut session = SuperReviewSession::new();
        session.set_system_understanding(SystemUnderstanding {
            purpose: "A CLI tool for managing tasks".to_string(),
            users: "Developers".to_string(),
            core_contract: "Reliable task CRUD via CLI".to_string(),
            critical_paths: vec!["task create".to_string(), "task list".to_string()],
            constraints: vec!["Must work offline".to_string()],
            notes: String::new(),
        });
        session.project_context = Some(ProjectContext {
            name: "task-cli".to_string(),
            version: "1.0.0".to_string(),
            language: "Rust".to_string(),
            directory_structure: "src/\n  main.rs\n".to_string(),
            key_files: vec![],
            dependencies: vec!["clap".to_string()],
            has_tests: true,
            has_ci: false,
            has_docs: false,
        });
        session.add_domain(DomainAssessment {
            domain: "Error Handling".to_string(),
            relevance: "CLI tools must handle errors gracefully".to_string(),
            rating: Rating::Good,
            analysis: "Errors are propagated with context throughout".to_string(),
            strengths: vec![Finding {
                description: "Consistent use of anyhow".to_string(),
                severity: Severity::Nit,
                evidence: vec![],
            }],
            concerns: vec![Finding {
                description: "Some unwrap() in tests only".to_string(),
                severity: Severity::Nit,
                evidence: vec![],
            }],
            evidence: vec![Evidence {
                file: "src/main.rs".to_string(),
                lines: None,
                observation: "All error paths use .context()".to_string(),
            }],
        });
        session
    }

    #[test]
    fn test_render_markdown_minimal() {
        let session = minimal_session();
        let md = session.render_markdown().unwrap();

        assert!(md.contains("# Super-Review: task-cli"));
        assert!(md.contains("## System Understanding"));
        assert!(md.contains("A CLI tool for managing tasks"));
        assert!(md.contains("## Domain Assessments"));
        assert!(md.contains("### Error Handling — Good"));
        assert!(md.contains("Errors are propagated with context"));
        assert!(md.contains("**Strengths:**"));
        assert!(md.contains("**Concerns:**"));
        assert!(md.contains("**Evidence:**"));
        assert!(md.contains("`src/main.rs`"));
    }

    #[test]
    fn test_render_markdown_full() {
        let mut session = minimal_session();

        session.add_cross_cutting(CrossCuttingFinding {
            title: "Architecture and error handling are aligned".to_string(),
            description: "Module boundaries match error propagation boundaries".to_string(),
            connected_domains: vec!["Architecture".to_string(), "Error Handling".to_string()],
            evidence: vec![],
            severity: Severity::Nit,
        });

        session.set_working_well(vec!["Clean module structure".to_string()]);
        session.set_needs_attention(vec!["Add more integration tests".to_string()]);
        session.set_at_risk(vec!["CI is not configured".to_string()]);
        session.set_verdict("Solid foundation with room for improvement in testing and CI.");

        session.add_recommendation(Recommendation {
            action: "Set up CI pipeline".to_string(),
            rationale: "No CI means regressions are caught late".to_string(),
            effort: "2 hours".to_string(),
            addresses: vec!["Build & CI".to_string()],
            priority: 1,
        });

        let md = session.render_markdown().unwrap();

        assert!(md.contains("## Cross-Cutting Findings"));
        assert!(md.contains("Architecture and error handling are aligned"));
        assert!(md.contains("## System-Level Assessment"));
        assert!(md.contains("### What's Working Well"));
        assert!(md.contains("Clean module structure"));
        assert!(md.contains("### What Needs Attention"));
        assert!(md.contains("Add more integration tests"));
        assert!(md.contains("### What's At Risk"));
        assert!(md.contains("CI is not configured"));
        assert!(md.contains("## Verdict"));
        assert!(md.contains("Solid foundation"));
        assert!(md.contains("## Recommendations"));
        assert!(md.contains("Set up CI pipeline"));
    }

    #[test]
    fn test_render_markdown_no_system_understanding() {
        let mut session = SuperReviewSession::new();
        session.add_domain(DomainAssessment {
            domain: "Test".to_string(),
            relevance: "test".to_string(),
            rating: Rating::Adequate,
            analysis: "test".to_string(),
            strengths: vec![],
            concerns: vec![],
            evidence: vec![],
        });
        assert!(session.render_markdown().is_err());
    }

    #[test]
    fn test_render_markdown_no_domains() {
        let mut session = SuperReviewSession::new();
        session.set_system_understanding(SystemUnderstanding {
            purpose: "test".to_string(),
            users: String::new(),
            core_contract: String::new(),
            critical_paths: vec![],
            constraints: vec![],
            notes: String::new(),
        });
        session.project_context = Some(ProjectContext {
            name: "test".to_string(),
            version: "0.1.0".to_string(),
            language: "Rust".to_string(),
            directory_structure: String::new(),
            key_files: vec![],
            dependencies: vec![],
            has_tests: false,
            has_ci: false,
            has_docs: false,
        });
        assert!(session.render_markdown().is_err());
    }

    // ── File writing ───────────────────────────────────────────

    #[test]
    fn test_write_document_to_file() {
        let tmp = tempfile::tempdir().unwrap();
        let mut session = minimal_session();
        session.project_root = Some(tmp.path().to_path_buf());

        let path = session.write_document(None).unwrap();
        assert!(path.exists());
        assert!(path.to_string_lossy().contains("docs/review"));

        let content = fs::read_to_string(&path).unwrap();
        assert!(content.contains("# Super-Review: task-cli"));
    }

    #[test]
    fn test_write_document_explicit_path() {
        let tmp = tempfile::tempdir().unwrap();
        let session = minimal_session();

        let explicit = tmp.path().join("custom-review.md");
        let path = session.write_document(Some(&explicit)).unwrap();
        assert_eq!(path, explicit);
        assert!(path.exists());
    }

    #[test]
    fn test_write_document_no_understanding() {
        let session = SuperReviewSession::new();
        assert!(session.write_document(None).is_err());
    }

    #[test]
    fn test_write_document_no_domains() {
        let mut session = SuperReviewSession::new();
        session.set_system_understanding(SystemUnderstanding {
            purpose: "test".to_string(),
            users: String::new(),
            core_contract: String::new(),
            critical_paths: vec![],
            constraints: vec![],
            notes: String::new(),
        });
        assert!(session.write_document(None).is_err());
    }

    // ── Helpers ────────────────────────────────────────────────

    #[test]
    fn test_slugify() {
        assert_eq!(slugify("Hello World"), "hello-world");
        assert_eq!(slugify("API Layer Review!"), "api-layer-review");
        assert_eq!(slugify("foo_bar baz"), "foo-bar-baz");
        assert_eq!(slugify("  spaces  "), "spaces");
        assert_eq!(slugify(""), "");
    }

    #[test]
    fn test_truncate_str_short() {
        assert_eq!(truncate_str("hello", 10), "hello");
    }

    #[test]
    fn test_truncate_str_exact() {
        assert_eq!(truncate_str("hello", 5), "hello");
    }

    #[test]
    fn test_truncate_str_long() {
        let result = truncate_str("hello world foo bar", 12);
        assert!(result.len() <= 15); // 12 + "..."
        assert!(result.ends_with("..."));
    }

    #[test]
    fn test_extract_toml_value() {
        let content = r#"[package]
name = "my-crate"
version = "0.1.0"
edition = "2021"
"#;
        assert_eq!(extract_toml_value(content, "name"), Some("my-crate".to_string()));
        assert_eq!(extract_toml_value(content, "version"), Some("0.1.0".to_string()));
        assert_eq!(extract_toml_value(content, "missing"), None);
    }

    #[test]
    fn test_extract_toml_value_with_braces() {
        let content = r#"[package]
name = { workspace = true }
version = "0.1.0"
"#;
        // The braces form won't match the simple pattern, should return None
        assert_eq!(extract_toml_value(content, "name"), None);
        assert_eq!(extract_toml_value(content, "version"), Some("0.1.0".to_string()));
    }

    // ── Evidence display ───────────────────────────────────────

    #[test]
    fn test_evidence_display_with_lines() {
        let ev = Evidence {
            file: "src/main.rs".to_string(),
            lines: Some((42, 50)),
            observation: "Uses context".to_string(),
        };
        assert_eq!(format!("{}", ev), "`src/main.rs:42-50` — Uses context");
    }

    #[test]
    fn test_evidence_display_without_lines() {
        let ev = Evidence {
            file: "src/main.rs".to_string(),
            lines: None,
            observation: "General pattern".to_string(),
        };
        assert_eq!(format!("{}", ev), "`src/main.rs` — General pattern");
    }

    // ── Skill prompt ───────────────────────────────────────────

    #[test]
    fn test_super_review_skill_prompt() {
        let prompt = super_review_skill_prompt();
        assert!(prompt.contains("Super-Review Skill"));
        assert!(prompt.contains("Phase 1: Understand the System"));
        assert!(prompt.contains("Phase 2: Map the Domains"));
        assert!(prompt.contains("Phase 3: Deep Domain Analysis"));
        assert!(prompt.contains("Phase 4: Cross-Cutting Analysis"));
        assert!(prompt.contains("Phase 5: Produce the Review"));
        assert!(prompt.contains("Excellent"));
        assert!(prompt.contains("Critical"));
    }

    // ── Serialization roundtrips ───────────────────────────────

    #[test]
    fn test_session_serde_roundtrip() {
        let mut session = SuperReviewSession::new().with_scope("API layer");
        session.set_phase(Phase::Analyze);
        session.set_system_understanding(SystemUnderstanding {
            purpose: "Test".to_string(),
            users: "Devs".to_string(),
            core_contract: "Works".to_string(),
            critical_paths: vec!["create".to_string()],
            constraints: vec![],
            notes: String::new(),
        });
        session.add_domain(DomainAssessment {
            domain: "Testing".to_string(),
            relevance: "Must be reliable".to_string(),
            rating: Rating::Adequate,
            analysis: "Some tests".to_string(),
            strengths: vec![],
            concerns: vec![],
            evidence: vec![],
        });

        let json = serde_json::to_string(&session).unwrap();
        let parsed: SuperReviewSession = serde_json::from_str(&json).unwrap();

        assert_eq!(parsed.phase, Phase::Analyze);
        assert_eq!(parsed.scope, Some("API layer".to_string()));
        assert_eq!(parsed.domains.len(), 1);
        assert!(parsed.system_understanding.is_some());
    }

    #[test]
    fn test_report_types_serde_roundtrip() {
        let evidence = Evidence {
            file: "src/lib.rs".to_string(),
            lines: Some((10, 20)),
            observation: "Pattern observed".to_string(),
        };

        let finding = Finding {
            description: "Test finding".to_string(),
            severity: Severity::Important,
            evidence: vec![evidence.clone()],
        };

        let domain = DomainAssessment {
            domain: "Security".to_string(),
            relevance: "Important".to_string(),
            rating: Rating::Concerning,
            analysis: "Issues found".to_string(),
            strengths: vec![finding.clone()],
            concerns: vec![finding.clone()],
            evidence: vec![evidence],
        };

        let json = serde_json::to_string_pretty(&domain).unwrap();
        let parsed: DomainAssessment = serde_json::from_str(&json).unwrap();
        assert_eq!(parsed.domain, "Security");
        assert_eq!(parsed.rating, Rating::Concerning);
        assert_eq!(parsed.strengths.len(), 1);
        assert_eq!(parsed.concerns.len(), 1);
    }

    #[test]
    fn test_recommendation_serde_roundtrip() {
        let rec = Recommendation {
            action: "Fix the thing".to_string(),
            rationale: "It's broken".to_string(),
            effort: "2 hours".to_string(),
            addresses: vec!["Correctness".to_string()],
            priority: 1,
        };

        let json = serde_json::to_string(&rec).unwrap();
        let parsed: Recommendation = serde_json::from_str(&json).unwrap();
        assert_eq!(parsed.action, "Fix the thing");
        assert_eq!(parsed.priority, 1);
    }
}