magi-cli 0.269.1

Blind multi-agent implementation competition: N agents implement, M judges rank blind, deliberate, vote privately, winner survives double review + E2E gate
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
//! Structured answers extracted from free-form agent output.
//!
//! Agents are asked to end with a fenced `json` block. They mostly do, and
//! sometimes they narrate afterwards, emit two blocks, or wrap the object in
//! prose. [`extract_json`] therefore scans for *every* balanced top-level
//! object in the text and returns the last one that deserializes into the type
//! the caller wants, rather than trusting a single regex to find the right one.
use std::collections::{BTreeMap, BTreeSet};
use std::fmt;
use std::path::{Component, Path};

use anyhow::{Result, bail};
use serde::{Deserialize, Serialize};

/// A judge's independent ranking of the candidates.
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct Ranking {
    /// Labels, best first. Must be a permutation of the presented labels.
    pub ranking: Vec<String>,
    /// Per-label justification.
    #[serde(default)]
    pub reasons: BTreeMap<String, String>,
    /// Self-reported confidence, 1-5.
    #[serde(default)]
    pub confidence: Option<u8>,
    /// Per-label strengths worth porting onto the winner, written for the
    /// candidates the judge did not rank first. Optional: a judge that
    /// names none simply leaves the graft phase nothing to do.
    #[serde(default, deserialize_with = "lenient_strengths")]
    pub strengths: BTreeMap<String, Vec<String>>,
}

/// Read `strengths` without ever failing: this field is optional, so a value of
/// the wrong shape (`null`, a string, a map of plain strings) must cost the
/// judge nothing but the strengths. Entries that are not a string array keep
/// their string members only; anything else is dropped.
fn lenient_strengths<'de, D>(d: D) -> Result<BTreeMap<String, Vec<String>>, D::Error>
where
    D: serde::Deserializer<'de>,
{
    let value = serde_json::Value::deserialize(d)?;
    let mut out = BTreeMap::new();
    if let serde_json::Value::Object(map) = value {
        for (label, v) in map {
            match v {
                serde_json::Value::Array(items) => {
                    let list: Vec<String> = items
                        .into_iter()
                        .filter_map(|i| i.as_str().map(str::to_owned))
                        .collect();
                    if !list.is_empty() {
                        out.insert(label, list);
                    }
                }
                serde_json::Value::String(s) if !s.trim().is_empty() => {
                    out.insert(label, vec![s]);
                }
                _ => {}
            }
        }
    }
    Ok(out)
}

impl Ranking {
    /// The judge's first choice.
    pub fn top(&self) -> Option<&str> {
        self.ranking.first().map(String::as_str)
    }

    /// Reject a ranking that is not a permutation of `labels`, so a malformed
    /// verdict is retried instead of silently skewing the tally.
    pub fn validate(&self, labels: &[char]) -> Result<()> {
        let mut got: Vec<char> = self
            .ranking
            .iter()
            .filter_map(|s| s.trim().chars().next())
            .map(|c| c.to_ascii_uppercase())
            .collect();
        got.sort_unstable();
        got.dedup();
        let mut want: Vec<char> = labels.to_vec();
        want.sort_unstable();
        if got != want {
            bail!(
                "ranking {:?} is not a permutation of the candidate labels {:?}",
                self.ranking,
                labels
            );
        }
        Ok(())
    }

    /// Normalise labels to single uppercase characters.
    pub fn normalized(&self) -> Vec<char> {
        self.ranking
            .iter()
            .filter_map(|s| s.trim().chars().next())
            .map(|c| c.to_ascii_uppercase())
            .collect()
    }
}

/// A judge's final vote, collected privately.
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct FinalVote {
    /// The winning label, in this judge's view.
    pub vote: String,
    /// Why.
    #[serde(default)]
    pub reason: String,
}

impl FinalVote {
    /// The voted label as a single uppercase char.
    pub fn label(&self) -> Option<char> {
        self.vote
            .trim()
            .chars()
            .next()
            .map(|c| c.to_ascii_uppercase())
    }
}

/// How bad a review finding is.
#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Serialize, Deserialize)]
#[serde(rename_all = "lowercase")]
pub enum Severity {
    /// Cosmetic.
    Nit,
    /// Should fix, does not block.
    Minor,
    /// Should fix before merge.
    Major,
    /// Must fix before merge.
    Blocker,
}

impl Severity {
    /// Does this finding hold the merge?
    pub fn blocks(self) -> bool {
        matches!(self, Self::Major | Self::Blocker)
    }
}

impl std::fmt::Display for Severity {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        f.write_str(match self {
            Self::Nit => "nit",
            Self::Minor => "minor",
            Self::Major => "major",
            Self::Blocker => "blocker",
        })
    }
}

/// One review finding.
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct Finding {
    /// Assigned by magi after parsing, e.g. `R1-1-2`. Never trusted from the
    /// agent, because the fixer's adoption report is keyed by it.
    #[serde(default)]
    pub id: String,
    /// How bad.
    pub severity: Severity,
    /// File it concerns.
    #[serde(default)]
    pub file: Option<String>,
    /// Line it concerns.
    #[serde(default)]
    pub line: Option<u32>,
    /// One-line summary.
    pub title: String,
    /// The argument.
    #[serde(default)]
    pub detail: String,
}

/// A reviewer seat's verdict on the patch, cast alongside its findings.
///
/// Three values, not a boolean, because "fine to proceed" and "fine, but
/// look at these" are different signals for the operator watching the run —
/// collapsing them would hide exactly the middle case a lens-based reviewer
/// is most likely to land on. Ord follows declaration order (least to most
/// cautious) so a round's [`ReviewVote::worst`] is a plain `max`, the same
/// trick [`Severity`] uses for `blocks`.
#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Serialize, Deserialize)]
#[serde(rename_all = "snake_case")]
pub enum ReviewVote {
    /// No reservations.
    Approve,
    /// Fine to proceed, but the findings below are worth fixing.
    ApproveWithFindings,
    /// Do not proceed as-is.
    Reject,
}

impl ReviewVote {
    /// Operator-facing label, used in events and the report — never
    /// `Debug`, so a rename of a variant does not silently reword history.
    pub fn label(self) -> &'static str {
        match self {
            Self::Approve => "approve",
            Self::ApproveWithFindings => "approve with findings",
            Self::Reject => "reject",
        }
    }

    /// The most cautious of a set of votes, or `None` if none were cast.
    ///
    /// A round's recorded verdict must never read softer than its most
    /// cautious seat — the same reason a single blocking finding, not an
    /// average of severities, decides whether a round is clean.
    pub fn worst(votes: impl IntoIterator<Item = Self>) -> Option<Self> {
        votes.into_iter().max()
    }
}

/// A reviewer's report.
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct Review {
    /// Findings, worst first is conventional but not required.
    #[serde(default)]
    pub findings: Vec<Finding>,
    /// Reviewer's overall verdict prose.
    #[serde(default)]
    pub summary: String,
    /// This seat's vote. Required, not defaulted: a reviewer that skips it
    /// is retried the same as one that skipped `severity` on a finding,
    /// rather than silently counted as an `approve`.
    pub vote: ReviewVote,
}

/// A reviewer seat's revote after a split round, cast once it has read every
/// seat's findings and votes (still numbered, never named — see
/// [`crate::prompt::review_reconsider`]).
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct ReviewRevote {
    /// The seat's revote.
    pub vote: ReviewVote,
    /// Why, one or two sentences.
    #[serde(default)]
    pub reason: String,
}

/// The fixer's response to a round of findings.
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct FixReport {
    /// Finding ids that were acted on.
    #[serde(default)]
    pub addressed: Vec<String>,
    /// Finding ids that were deliberately not acted on, with the reason.
    #[serde(default)]
    pub rejected: Vec<Rejection>,
    /// What changed.
    #[serde(default)]
    pub notes: String,
}

/// A finding the fixer declined, and why.
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct Rejection {
    /// Finding id.
    pub id: String,
    /// Argument for not acting.
    #[serde(default)]
    pub why: String,
}

/// A judge's position during deliberation.
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct Position {
    /// Where the judge currently stands.
    #[serde(default)]
    pub tentative: Option<String>,
}

/// One advisor's independent design proposal, gathered by
/// `graph::Runner::advise` before a run's implementer seats begin.
///
/// No patch and no plan of tool calls - a design proposal is cheap exactly
/// because it is a few paragraphs an agent can write without touching the
/// repository, which is what makes running several of them, on every run,
/// affordable in a way several full implementations were not.
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct Proposal {
    /// What to do and how, in the advisor's own words.
    pub approach: String,
    /// The one tradeoff this design turns on.
    pub key_tradeoff: String,
    /// What could go wrong.
    #[serde(default)]
    pub risks: Vec<String>,
    /// Files or modules the design touches.
    #[serde(default)]
    pub touches: Vec<String>,
    /// Why this earns its complexity over the obvious first draft - the
    /// question that keeps a proposal from being a restatement of the task.
    pub why_not_naive: String,
}

impl Proposal {
    /// Reject a proposal with an empty field that matters, so a malformed or
    /// lazy answer is retried instead of silently thinning the panel down to
    /// prose nobody can act on.
    pub fn validate(&self) -> Result<()> {
        for (field, value) in [
            ("approach", &self.approach),
            ("key_tradeoff", &self.key_tradeoff),
            ("why_not_naive", &self.why_not_naive),
        ] {
            if value.trim().is_empty() {
                bail!("`{field}` is empty");
            }
        }
        Ok(())
    }
}

/// Extract the last balanced JSON object in `text` that parses as `T`.
///
/// Handles fenced blocks, trailing prose, and multiple objects. Strings and
/// escapes are tracked so a `}` inside a string literal does not close an
/// object early.
pub fn extract_json<T: serde::de::DeserializeOwned>(text: &str) -> Result<T> {
    let bytes = text.as_bytes();
    let mut spans: Vec<(usize, usize)> = Vec::new();
    let mut i = 0usize;
    while i < bytes.len() {
        if bytes[i] != b'{' {
            i += 1;
            continue;
        }
        let mut depth = 0usize;
        let mut in_str = false;
        let mut escaped = false;
        let mut j = i;
        while j < bytes.len() {
            let c = bytes[j];
            if in_str {
                if escaped {
                    escaped = false;
                } else if c == b'\\' {
                    escaped = true;
                } else if c == b'"' {
                    in_str = false;
                }
            } else {
                match c {
                    b'"' => in_str = true,
                    b'{' => depth += 1,
                    b'}' => {
                        depth -= 1;
                        if depth == 0 {
                            spans.push((i, j + 1));
                            break;
                        }
                    }
                    _ => {}
                }
            }
            j += 1;
        }
        // Skip past this object's opening brace either way; a truncated object
        // must not make the scan quadratic on long transcripts.
        i = if depth == 0 && j < bytes.len() {
            j + 1
        } else {
            i + 1
        };
    }

    let mut last_err = None;
    for (start, end) in spans.iter().rev() {
        match serde_json::from_str::<T>(&text[*start..*end]) {
            Ok(v) => return Ok(v),
            Err(e) => last_err = Some(e),
        }
    }
    match last_err {
        Some(e) => bail!("no JSON object in the reply matched the expected shape: {e}"),
        None => bail!("the reply contained no JSON object"),
    }
}

/// Pull the text after a `## <heading>` marker, to the end or the next heading.
///
/// Used for the prose sections agents are asked to emit alongside their JSON.
pub fn section(text: &str, heading: &str) -> Option<String> {
    let want = heading.to_ascii_lowercase();
    let mut out: Option<String> = None;
    for line in text.lines() {
        let trimmed = line.trim();
        if let Some(rest) = trimmed.strip_prefix("##") {
            let name = rest.trim_start_matches('#').trim().to_ascii_lowercase();
            if name == want {
                out = Some(String::new());
                continue;
            }
            if out.is_some() {
                break;
            }
            continue;
        }
        if let Some(buf) = out.as_mut() {
            buf.push_str(line);
            buf.push('\n');
        }
    }
    out.map(|s| s.trim().to_owned()).filter(|s| !s.is_empty())
}

/// The marker `prompt::implement`'s reply format documents for an
/// implementer that investigated and concluded, with concrete positive
/// evidence, that no code change belongs in this worktree — see that
/// prompt's "Reply format" section for the structured JSON schema required
/// after it.
pub const NO_CHANGE_NEEDED_MARKER: &str = "NO CHANGE NEEDED:";

/// Declared investigation status in a structured `NO CHANGE NEEDED:` report.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
#[serde(rename_all = "snake_case")]
pub enum NoopStatus {
    /// Every requirement was verified to already be satisfied with nothing left unresolved.
    AlreadySatisfied,
    /// Investigation did not finish or could not verify satisfaction.
    Incomplete,
    /// Investigation or verification failed.
    Failed,
}

/// Concrete category of verification evidence cited by a [`NoopCheck`].
#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
#[serde(rename_all = "snake_case")]
pub enum NoopEvidenceKind {
    /// A git commit SHA present in the repository's base history.
    Commit,
    /// A test or test suite executed to confirm the behavior.
    Test,
    /// A command executed to verify repository state or behavior.
    Command,
    /// A repository-relative path inspected in the worktree.
    Path,
}

impl NoopEvidenceKind {
    /// Stable lower-case name matching the wire JSON spelling.
    pub fn as_str(self) -> &'static str {
        match self {
            Self::Commit => "commit",
            Self::Test => "test",
            Self::Command => "command",
            Self::Path => "path",
        }
    }
}

impl fmt::Display for NoopEvidenceKind {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        f.write_str(self.as_str())
    }
}

/// One requirement evaluated by the implementer and the passing check ids that
/// prove it is already satisfied.
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
#[serde(deny_unknown_fields)]
pub struct NoopRequirement {
    /// Short statement of the requirement checked.
    pub requirement: String,
    /// Whether this requirement is already satisfied.
    pub satisfied: bool,
    /// Check ids (referencing [`NoopCheck::id`]) proving satisfaction.
    pub evidence: Vec<String>,
}

/// One concrete verification check actually carried out during investigation.
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
#[serde(deny_unknown_fields)]
pub struct NoopCheck {
    /// Unique identifier referenced by [`NoopRequirement::evidence`].
    pub id: String,
    /// Evidence kind: `commit`, `test`, `command`, or `path`.
    pub kind: NoopEvidenceKind,
    /// What was inspected or executed (commit SHA, test name, command, or repo-relative path).
    pub target: String,
    /// How verification was performed.
    pub method: String,
    /// Concrete observed output or finding.
    pub observed: String,
    /// Whether this check succeeded.
    pub passed: bool,
}

/// Structured JSON payload required immediately after [`NO_CHANGE_NEEDED_MARKER`].
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
#[serde(deny_unknown_fields)]
pub struct VerifiedNoopReport {
    /// Overall investigation outcome (`already_satisfied`, `incomplete`, or `failed`).
    pub status: NoopStatus,
    /// Per-requirement satisfaction assessments referencing concrete checks.
    pub requirements: Vec<NoopRequirement>,
    /// Ordered list of concrete verification checks performed.
    pub checks: Vec<NoopCheck>,
    /// Remaining unresolved issues; must be empty for `already_satisfied`.
    pub unresolved: Vec<String>,
}

impl VerifiedNoopReport {
    /// Enforce all structural and semantic invariants of an evidence-backed
    /// no-change declaration.
    pub fn validate(&self) -> std::result::Result<(), String> {
        match self.status {
            NoopStatus::AlreadySatisfied => {}
            NoopStatus::Incomplete => {
                return Err(
                    "status is `incomplete`; investigation did not establish that no change is needed"
                        .to_owned(),
                );
            }
            NoopStatus::Failed => {
                return Err("status is `failed`; investigation did not succeed".to_owned());
            }
        }

        if !self.unresolved.is_empty() {
            let items: Vec<&str> = self
                .unresolved
                .iter()
                .map(|s| s.trim())
                .filter(|s| !s.is_empty())
                .collect();
            if items.is_empty() {
                return Err(format!(
                    "unresolved list is non-empty ({} blank entry/entries)",
                    self.unresolved.len()
                ));
            }
            return Err(format!("unresolved item(s) remain: {}", items.join("; ")));
        }

        if self.checks.is_empty() {
            return Err("no verification checks were provided".to_owned());
        }

        let mut checks_by_id: BTreeMap<String, &NoopCheck> = BTreeMap::new();
        for check in &self.checks {
            let id = check.id.trim();
            if id.is_empty() {
                return Err("a verification check has an empty `id`".to_owned());
            }
            let id_key = id.to_ascii_lowercase();
            if checks_by_id.contains_key(&id_key) {
                return Err(format!("duplicate check id `{id}`"));
            }

            let target = check.target.trim();
            if target.is_empty() || is_placeholder_value(target) {
                return Err(format!(
                    "check `{id}` has empty or placeholder `target` (`{target}`)"
                ));
            }
            let method = check.method.trim();
            if method.is_empty() || is_placeholder_value(method) || is_placeholder_prose(method) {
                return Err(format!(
                    "check `{id}` has empty or placeholder `method` (`{method}`)"
                ));
            }
            let observed = check.observed.trim();
            if observed.is_empty()
                || is_placeholder_value(observed)
                || is_placeholder_prose(observed)
            {
                return Err(format!(
                    "check `{id}` has empty or placeholder `observed` (`{observed}`)"
                ));
            }

            match check.kind {
                NoopEvidenceKind::Commit if !is_commit_sha(target) => {
                    return Err(format!(
                        "check `{id}` (`commit`) target `{target}` must be a 7-40 character hex commit SHA"
                    ));
                }
                NoopEvidenceKind::Path
                    if !is_safe_repo_relative_path(strip_path_locator(target)) =>
                {
                    return Err(format!(
                        "check `{id}` (`path`) target `{target}` must be a repository-relative path"
                    ));
                }
                _ => {}
            }

            if check.passed {
                if let Some(phrase) = incomplete_admission(method) {
                    return Err(format!(
                        "check `{id}` claims `passed: true` but `method` admits incomplete work (`{phrase}`)"
                    ));
                }
                if let Some(phrase) = incomplete_admission(observed) {
                    return Err(format!(
                        "check `{id}` claims `passed: true` but `observed` admits incomplete work (`{phrase}`)"
                    ));
                }
            }

            checks_by_id.insert(id_key, check);
        }

        if let Some(last) = self.checks.last()
            && !last.passed
        {
            return Err(format!(
                "final verification check `{}` did not pass",
                last.id.trim()
            ));
        }

        if self.requirements.is_empty() {
            return Err("no requirements were evaluated".to_owned());
        }

        let mut seen_reqs = BTreeSet::new();
        for req in &self.requirements {
            let text = req.requirement.trim();
            if text.is_empty() || is_placeholder_value(text) {
                return Err(format!(
                    "requirement entry has empty or placeholder description (`{text}`)"
                ));
            }
            let req_key = normalize_whitespace_lower(text);
            if !seen_reqs.insert(req_key) {
                return Err(format!("duplicate requirement `{text}`"));
            }
            if !req.satisfied {
                return Err(format!("requirement `{text}` is not satisfied"));
            }
            if req.evidence.is_empty() {
                return Err(format!("requirement `{text}` has no supporting evidence"));
            }

            let mut seen_ev = BTreeSet::new();
            for raw_id in &req.evidence {
                let ev_id = raw_id.trim();
                if ev_id.is_empty() {
                    return Err(format!(
                        "requirement `{text}` contains an empty evidence id"
                    ));
                }
                let ev_key = ev_id.to_ascii_lowercase();
                if !seen_ev.insert(ev_key.clone()) {
                    return Err(format!(
                        "requirement `{text}` references duplicate check id `{ev_id}`"
                    ));
                }
                let Some(check) = checks_by_id.get(&ev_key) else {
                    return Err(format!(
                        "requirement `{text}` references unknown check `{ev_id}`"
                    ));
                };
                if !check.passed {
                    return Err(format!(
                        "contradictory evidence: requirement `{text}` cites failed check `{ev_id}`"
                    ));
                }
            }
        }

        Ok(())
    }

    /// Unique checks referenced by [`Self::requirements`], in check order.
    pub fn cited_checks(&self) -> Vec<&NoopCheck> {
        let cited: BTreeSet<String> = self
            .requirements
            .iter()
            .flat_map(|r| r.evidence.iter())
            .map(|id| id.trim().to_ascii_lowercase())
            .collect();
        self.checks
            .iter()
            .filter(|c| cited.contains(&c.id.trim().to_ascii_lowercase()))
            .collect()
    }

    /// Concise, single-line human-readable evidence summary for reports and
    /// diagnostics.
    pub fn summary_text(&self) -> String {
        let checks_by_id: BTreeMap<String, &NoopCheck> = self
            .checks
            .iter()
            .map(|c| (c.id.trim().to_ascii_lowercase(), c))
            .collect();
        let parts: Vec<String> = self
            .requirements
            .iter()
            .map(|req| {
                let ev: Vec<String> = req
                    .evidence
                    .iter()
                    .filter_map(|id| checks_by_id.get(&id.trim().to_ascii_lowercase()))
                    .map(|c| {
                        format!(
                            "{} {}: {}",
                            c.kind,
                            collapse_ws(c.target.trim()),
                            collapse_ws(c.observed.trim())
                        )
                    })
                    .collect();
                format!(
                    "{} [{}]",
                    collapse_ws(req.requirement.trim()),
                    ev.join("; ")
                )
            })
            .collect();
        format!("already satisfied: {}", parts.join(" | "))
    }
}

/// Strip an optional trailing line/range locator (`:123`, `:123-145`, `:123:4`,
/// `#L123`, `#L123-L145`) from a repository-relative path reference.
pub fn strip_path_locator(target: &str) -> &str {
    let target = target.trim();
    if let Some((path, frag)) = target.rsplit_once("#L")
        && !path.is_empty()
        && is_line_range_fragment(frag)
    {
        return path;
    }
    if let Some((path, suffix)) = target.rsplit_once(':')
        && !path.is_empty()
        && !(path.len() == 1 && path.as_bytes()[0].is_ascii_alphabetic())
    {
        // Check `path:line:col` before `path:line`, since `:col` is also all digits.
        if !suffix.is_empty()
            && suffix.bytes().all(|b| b.is_ascii_digit())
            && let Some((base, line)) = path.rsplit_once(':')
            && !base.is_empty()
            && !(base.len() == 1 && base.as_bytes()[0].is_ascii_alphabetic())
            && !line.is_empty()
            && line.bytes().all(|b| b.is_ascii_digit())
        {
            return base;
        }
        if is_colon_line_suffix(suffix) {
            return path;
        }
    }
    target
}

fn is_line_range_fragment(frag: &str) -> bool {
    if let Some((a, b)) = frag.split_once('-') {
        let b = b.strip_prefix('L').unwrap_or(b);
        !a.is_empty()
            && !b.is_empty()
            && a.bytes().all(|c| c.is_ascii_digit())
            && b.bytes().all(|c| c.is_ascii_digit())
    } else {
        !frag.is_empty() && frag.bytes().all(|c| c.is_ascii_digit())
    }
}

fn is_colon_line_suffix(suffix: &str) -> bool {
    if let Some((a, b)) = suffix.split_once('-') {
        !a.is_empty()
            && !b.is_empty()
            && a.bytes().all(|c| c.is_ascii_digit())
            && b.bytes().all(|c| c.is_ascii_digit())
    } else {
        !suffix.is_empty() && suffix.bytes().all(|c| c.is_ascii_digit())
    }
}

fn is_commit_sha(s: &str) -> bool {
    (7..=40).contains(&s.len()) && s.bytes().all(|b| b.is_ascii_hexdigit())
}

fn is_safe_repo_relative_path(s: &str) -> bool {
    if s.is_empty() || s.contains('\0') {
        return false;
    }
    if s.starts_with('/') || s.starts_with('\\') {
        return false;
    }
    let bytes = s.as_bytes();
    if bytes.len() >= 2 && bytes[0].is_ascii_alphabetic() && bytes[1] == b':' {
        return false;
    }
    let mut has_normal = false;
    for comp in Path::new(s).components() {
        match comp {
            Component::Normal(_) => has_normal = true,
            Component::CurDir => {}
            Component::ParentDir | Component::RootDir | Component::Prefix(_) => return false,
        }
    }
    has_normal
}

fn collapse_ws(s: &str) -> String {
    s.split_whitespace().collect::<Vec<_>>().join(" ")
}

fn normalize_whitespace_lower(s: &str) -> String {
    collapse_ws(s).to_ascii_lowercase()
}

fn is_placeholder_value(s: &str) -> bool {
    let norm = normalize_whitespace_lower(s);
    matches!(
        norm.as_str(),
        "" | "-"
            | "--"
            | "..."
            | "?"
            | "???"
            | "n/a"
            | "na"
            | "none"
            | "null"
            | "nil"
            | "tbd"
            | "todo"
            | "unknown"
            | "pending"
            | "unchecked"
            | "unverified"
            | "not run"
            | "not checked"
            | "not tested"
            | "skipped"
            | "omitted"
            | "placeholder"
    )
}

fn is_placeholder_prose(s: &str) -> bool {
    let norm = normalize_whitespace_lower(s);
    matches!(
        norm.as_str(),
        "ok" | "okay"
            | "pass"
            | "passed"
            | "passing"
            | "true"
            | "yes"
            | "done"
            | "checked"
            | "verified"
            | "confirmed"
            | "satisfied"
            | "same"
            | "lgtm"
            | "looks good"
            | "works"
            | "good"
    )
}

/// Auxiliary defense against passing checks whose prose explicitly admits
/// incomplete or unverified work. Kept narrow and word-boundary-aware so
/// legitimate reports describing resolved past failures (e.g. "previously
/// failed; now passes with 0 failed") or symbol names (e.g.
/// `has_unconfirmed_command`) are never rejected.
fn incomplete_admission(s: &str) -> Option<&'static str> {
    const PHRASES: &[&str] = &[
        "not established",
        "remains unresolved",
        "still unresolved",
        "left unresolved",
        "not verified",
        "not confirmed",
        "could not verify",
        "unable to verify",
        "failed to verify",
        "could not confirm",
        "unable to confirm",
        "failed to confirm",
        "could not complete",
        "unable to complete",
        "did not complete",
        "not completed",
        "was not completed",
        "were not completed",
        "did not run",
        "was not run",
        "were not run",
        "not executed",
        "skipped verification",
        "investigation incomplete",
        "incomplete investigation",
        "no verification checks",
        "no reproduction checks",
        "no checks were completed",
        "no checks were run",
        "inconclusive",
    ];
    let norm = normalize_whitespace_lower(s);
    PHRASES
        .iter()
        .copied()
        .find(|phrase| contains_whole_phrase(&norm, phrase))
}

fn contains_whole_phrase(haystack: &str, needle: &str) -> bool {
    let bytes = haystack.as_bytes();
    let is_word = |b: u8| b.is_ascii_alphanumeric() || b == b'_';
    let mut start = 0;
    while let Some(pos) = haystack[start..].find(needle) {
        let abs = start + pos;
        let end = abs + needle.len();
        let before_ok = abs == 0 || !is_word(bytes[abs - 1]);
        let after_ok = end == bytes.len() || !is_word(bytes[end]);
        if before_ok && after_ok {
            return true;
        }
        start = abs + 1;
    }
    false
}

/// Strip at most one outer Markdown code fence (` ``` ` or ` ```json `) with no
/// trailing content after the closing fence.
fn strip_single_json_fence(body: &str) -> std::result::Result<&str, String> {
    let trimmed = body.trim();
    if !trimmed.starts_with("```") {
        return Ok(trimmed);
    }
    let Some((first_line, rest)) = trimmed.split_once('\n') else {
        return Err("unterminated fenced JSON block after NO CHANGE NEEDED:".to_owned());
    };
    let lang = first_line.trim().trim_start_matches('`').trim();
    if !lang.is_empty() && !lang.eq_ignore_ascii_case("json") {
        return Err(format!(
            "unexpected fenced block language `{lang}`; expected a JSON object"
        ));
    }
    let rest_trimmed = rest.trim_end();
    let Some(inner) = rest_trimmed.strip_suffix("```") else {
        return Err(
            "unterminated fenced JSON block or trailing prose after closing fence".to_owned(),
        );
    };
    if !inner.is_empty() && !inner.ends_with('\n') && !inner.ends_with('\r') {
        return Err("closing fence must sit on its own line".to_owned());
    }
    Ok(inner.trim())
}

/// Strictly parse and validate a `NO CHANGE NEEDED:` declaration from an
/// implementer's `## SUMMARY` section.
///
/// - Returns `None` when the summary does not open with
///   [`NO_CHANGE_NEEDED_MARKER`] (an ordinary change summary or plain prose
///   explanation).
/// - Returns `Some(Ok(report))` only when the payload is a single strict JSON
///   object matching [`VerifiedNoopReport`] and passing
///   [`VerifiedNoopReport::validate`].
/// - Returns `Some(Err(reason))` with a concrete rejection reason when the
///   summary opened with the marker but failed parsing or validation (legacy
///   freeform prose, incomplete/failed status, unresolved items, missing or
///   failed checks, placeholders, duplicate keys/ids, or contradictory claims).
pub fn parse_verified_noop(
    summary: &str,
) -> Option<std::result::Result<VerifiedNoopReport, String>> {
    let rest = summary.trim_start().strip_prefix(NO_CHANGE_NEEDED_MARKER)?;
    let body = rest.trim();
    if body.is_empty() {
        return Some(Err(
            "empty NO CHANGE NEEDED declaration with no structured JSON evidence".to_owned(),
        ));
    }
    let json_text = match strip_single_json_fence(body) {
        Ok(s) => s,
        Err(e) => return Some(Err(e)),
    };
    if !json_text.starts_with('{') || !json_text.ends_with('}') {
        return Some(Err(
            "expected a single JSON object after NO CHANGE NEEDED:; prose-only or legacy declarations are rejected"
                .to_owned(),
        ));
    }
    let report: VerifiedNoopReport = match serde_json::from_str(json_text) {
        Ok(r) => r,
        Err(e) => {
            return Some(Err(format!(
                "invalid structured JSON after NO CHANGE NEEDED:: {e}"
            )));
        }
    };
    if let Err(reason) = report.validate() {
        return Some(Err(reason));
    }
    Some(Ok(report))
}

/// Pull the validated evidence summary out of an implementer's `## SUMMARY`
/// section when it opens with [`NO_CHANGE_NEEDED_MARKER`] and passes strict
/// structured validation.
///
/// Returns `None` for an ordinary bullet-list summary, and also for any
/// malformed, legacy prose-only, incomplete, contradictory, or unsupported
/// declaration. Whether the declaration is trusted against repository state and
/// CLI command evidence is [`crate::graph`]'s adoption guard's call.
pub fn verified_noop(summary: &str) -> Option<String> {
    parse_verified_noop(summary)?
        .ok()
        .map(|report| report.summary_text())
}

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

    #[test]
    fn severity_has_explicit_operator_labels() {
        for (severity, label) in [
            (Severity::Nit, "nit"),
            (Severity::Minor, "minor"),
            (Severity::Major, "major"),
            (Severity::Blocker, "blocker"),
        ] {
            assert_eq!(severity.to_string(), label);
        }
    }

    #[test]
    fn malformed_strengths_never_cost_the_ranking() {
        for bad in [
            "null",
            r#"{"B":"one point"}"#,
            r#""x""#,
            r#"[1]"#,
            r#"{"B":[1,"ok"]}"#,
        ] {
            let text = format!(r#"{{"ranking":["A","B"],"strengths":{bad}}}"#);
            let r: Ranking = extract_json(&text).unwrap();
            assert_eq!(r.ranking, ["A", "B"]);
        }
    }

    #[test]
    fn fenced_block_is_found() {
        let text = "Here is my verdict.\n\n```json\n{\"ranking\":[\"B\",\"A\"]}\n```\n";
        let r: Ranking = extract_json(text).unwrap();
        assert_eq!(r.top(), Some("B"));
    }

    #[test]
    fn last_matching_object_wins_over_an_earlier_example() {
        let text = concat!(
            "The format is {\"ranking\":[\"X\"]} for illustration.\n",
            "```json\n{\"ranking\":[\"C\",\"A\",\"B\"],\"confidence\":4}\n```\n",
            "Happy to elaborate.\n"
        );
        let r: Ranking = extract_json(text).unwrap();
        assert_eq!(r.normalized(), ['C', 'A', 'B']);
        assert_eq!(r.confidence, Some(4));
    }

    #[test]
    fn braces_inside_strings_do_not_close_the_object() {
        let text = r#"{"ranking":["A"],"reasons":{"A":"uses format!(\"{}\", x) safely}"}}"#;
        let r: Ranking = extract_json(text).unwrap();
        assert_eq!(r.top(), Some("A"));
        assert!(r.reasons["A"].contains("format!"));
    }

    #[test]
    fn objects_of_the_wrong_shape_are_skipped() {
        let text = concat!(
            "```json\n{\"ranking\":[\"A\",\"B\"]}\n```\n",
            "and some telemetry: {\"tokens\":123}\n"
        );
        let r: Ranking = extract_json(text).unwrap();
        assert_eq!(r.normalized(), ['A', 'B']);
    }

    #[test]
    fn no_json_is_an_error_not_a_default() {
        let err = extract_json::<Ranking>("I decline to produce JSON.").unwrap_err();
        assert!(err.to_string().contains("no JSON object"));
    }

    #[test]
    fn truncated_object_does_not_hang() {
        let err = extract_json::<Ranking>("{\"ranking\": [\"A\"").unwrap_err();
        assert!(err.to_string().contains("no JSON object"));
    }

    #[test]
    fn ranking_validation_rejects_a_non_permutation() {
        let r = Ranking {
            ranking: vec!["A".to_owned(), "A".to_owned()],
            reasons: BTreeMap::new(),
            confidence: None,
            strengths: BTreeMap::new(),
        };
        assert!(r.validate(&['A', 'B', 'C']).is_err());

        let r = Ranking {
            ranking: vec!["c".to_owned(), "B".to_owned(), "A".to_owned()],
            reasons: BTreeMap::new(),
            confidence: None,
            strengths: BTreeMap::new(),
        };
        r.validate(&['A', 'B', 'C']).expect("case is normalised");
        assert_eq!(r.normalized(), ['C', 'B', 'A']);
    }

    #[test]
    fn final_vote_label_is_normalised() {
        let v: FinalVote = extract_json(r#"{"vote":" b ","reason":"tests"}"#).unwrap();
        assert_eq!(v.label(), Some('B'));
    }

    #[test]
    fn severity_blocking_is_major_and_up() {
        assert!(Severity::Blocker.blocks());
        assert!(Severity::Major.blocks());
        assert!(!Severity::Minor.blocks());
        assert!(!Severity::Nit.blocks());
        assert!(Severity::Blocker > Severity::Nit);
    }

    #[test]
    fn review_parses_with_optional_fields_missing() {
        let r: Review = extract_json(
            r#"{"vote":"reject","findings":[{"severity":"blocker","title":"panics on empty input"}]}"#,
        )
        .unwrap();
        assert_eq!(r.findings.len(), 1);
        assert!(r.findings[0].file.is_none());
        assert_eq!(r.findings[0].id, "");
        assert_eq!(r.vote, ReviewVote::Reject);
    }

    #[test]
    fn review_without_a_vote_is_rejected_rather_than_defaulted() {
        let err = extract_json::<Review>(r#"{"findings":[]}"#).unwrap_err();
        assert!(err.to_string().contains("no JSON object"), "{err}");
    }

    #[test]
    fn review_vote_worst_is_the_most_cautious() {
        assert_eq!(
            ReviewVote::worst([ReviewVote::Approve, ReviewVote::Reject, ReviewVote::Approve]),
            Some(ReviewVote::Reject)
        );
        assert_eq!(
            ReviewVote::worst([ReviewVote::Approve, ReviewVote::ApproveWithFindings]),
            Some(ReviewVote::ApproveWithFindings)
        );
        assert_eq!(ReviewVote::worst(Vec::<ReviewVote>::new()), None);
    }

    #[test]
    fn review_revote_parses_the_reconsideration_shape() {
        let r: ReviewRevote =
            extract_json(r#"{"vote":"approve","reason":"the other findings do not hold"}"#)
                .unwrap();
        assert_eq!(r.vote, ReviewVote::Approve);
        assert_eq!(r.reason, "the other findings do not hold");
    }

    #[test]
    fn fix_report_parses_rejections() {
        let f: FixReport = extract_json(
            r#"{"addressed":["R1-1-1"],"rejected":[{"id":"R1-2-1","why":"not reachable"}]}"#,
        )
        .unwrap();
        assert_eq!(f.addressed, ["R1-1-1"]);
        assert_eq!(f.rejected[0].id, "R1-2-1");
    }

    #[test]
    fn proposal_parses_with_optional_fields_missing() {
        let p: Proposal = extract_json(
            r#"{"approach":"do X","key_tradeoff":"simpler now, slower later","why_not_naive":"the naive version corrupts state under a retry"}"#,
        )
        .unwrap();
        assert!(p.risks.is_empty());
        assert!(p.touches.is_empty());
        p.validate()
            .expect("a proposal with only required fields is valid");
    }

    #[test]
    fn proposal_validation_rejects_an_empty_required_field() {
        let p = Proposal {
            approach: String::new(),
            key_tradeoff: "t".to_owned(),
            risks: Vec::new(),
            touches: Vec::new(),
            why_not_naive: "w".to_owned(),
        };
        let err = p.validate().unwrap_err();
        assert!(err.to_string().contains("approach"));
    }

    #[test]
    fn sections_are_sliced_by_heading() {
        let text = "## SUMMARY\nchanged the retry loop.\nadded a test.\n\n## NOTES\nignore me\n";
        assert_eq!(
            section(text, "summary").unwrap(),
            "changed the retry loop.\nadded a test."
        );
        assert_eq!(section(text, "notes").unwrap(), "ignore me");
        assert!(section(text, "missing").is_none());
    }

    const REGRESSION_QUOTED_SUMMARY: &str = "NO CHANGE NEEDED: Not established. No source changes, commit, or reproduction checks were completed. The reported Git configuration lock contention remains unresolved.";

    fn valid_noop_json() -> &'static str {
        r#"{"status":"already_satisfied","requirements":[{"requirement":"retry loop already fixed on base","satisfied":true,"evidence":["c1","c2"]}],"checks":[{"id":"c1","kind":"commit","target":"b32cfc4","method":"git merge-base --is-ancestor b32cfc4 HEAD","observed":"exit 0; b32cfc4 is on main and fixes the retry loop","passed":true},{"id":"c2","kind":"test","target":"retry_loop_recovers","method":"cargo test --lib retry_loop_recovers","observed":"test result: ok. 1 passed; 0 failed","passed":true}],"unresolved":[]}"#
    }

    #[test]
    fn structured_no_change_needed_report_yields_validated_evidence() {
        let inline = format!("NO CHANGE NEEDED: {}", valid_noop_json());
        let report = parse_verified_noop(&inline)
            .expect("marker present")
            .expect("valid structured report");
        assert_eq!(report.status, NoopStatus::AlreadySatisfied);
        let summary = verified_noop(&inline).expect("valid report yields summary");
        assert!(summary.contains("b32cfc4"), "{summary}");
        assert!(summary.contains("retry_loop_recovers"), "{summary}");

        let fenced = format!("NO CHANGE NEEDED:\n```json\n{}\n```\n", valid_noop_json());
        assert!(
            verified_noop(&fenced).is_some(),
            "single fenced json block must be accepted"
        );
    }

    #[test]
    fn operational_regression_quoted_response_is_rejected() {
        let err = parse_verified_noop(REGRESSION_QUOTED_SUMMARY)
            .expect("marker present")
            .expect_err("incomplete prose response must be rejected");
        assert!(err.contains("single JSON object"), "{err}");
        assert!(verified_noop(REGRESSION_QUOTED_SUMMARY).is_none());

        // Even if the exact regression text is stuffed into a valid-shaped JSON
        // object claiming `already_satisfied` and `passed: true`, auxiliary
        // contradiction detection rejects it.
        let stuffed = r#"NO CHANGE NEEDED: {"status":"already_satisfied","requirements":[{"requirement":"resolve git lock contention","satisfied":true,"evidence":["c1"]}],"checks":[{"id":"c1","kind":"command","target":"git config --list","method":"ran investigation","observed":"Not established. No source changes, commit, or reproduction checks were completed. The reported Git configuration lock contention remains unresolved.","passed":true}],"unresolved":[]}"#;
        let err = parse_verified_noop(stuffed)
            .expect("marker present")
            .expect_err("stuffed incomplete admission must be rejected");
        assert!(err.contains("admits incomplete work"), "{err}");
        assert!(verified_noop(stuffed).is_none());
    }

    #[test]
    fn legacy_freeform_and_empty_no_change_declarations_are_rejected() {
        let legacy = "NO CHANGE NEEDED: already fixed by b32cfc4, which is on main \
                      (git merge-base --is-ancestor confirms it).";
        assert!(matches!(parse_verified_noop(legacy), Some(Err(_))));
        assert!(verified_noop(legacy).is_none());
        assert!(matches!(
            parse_verified_noop("NO CHANGE NEEDED:"),
            Some(Err(_))
        ));
        assert!(matches!(
            parse_verified_noop("NO CHANGE NEEDED:   \n  "),
            Some(Err(_))
        ));
    }

    #[test]
    fn an_ordinary_summary_is_not_verified() {
        assert!(parse_verified_noop("- changed the retry loop.\n- added a test.").is_none());
        assert!(verified_noop("- changed the retry loop.\n- added a test.").is_none());
        // The marker only counts leading the section, not mentioned in passing.
        assert!(
            parse_verified_noop("I considered NO CHANGE NEEDED: but wrote a fix instead.")
                .is_none()
        );
    }

    #[test]
    fn incomplete_failed_unresolved_or_unverified_reports_are_rejected() {
        let cases = [
            (
                "incomplete status",
                r#"NO CHANGE NEEDED: {"status":"incomplete","requirements":[{"requirement":"req","satisfied":true,"evidence":["c1"]}],"checks":[{"id":"c1","kind":"path","target":"src/graph.rs","method":"read src/graph.rs","observed":"read file contents","passed":true}],"unresolved":[]}"#,
                "incomplete",
            ),
            (
                "failed status",
                r#"NO CHANGE NEEDED: {"status":"failed","requirements":[{"requirement":"req","satisfied":true,"evidence":["c1"]}],"checks":[{"id":"c1","kind":"path","target":"src/graph.rs","method":"read src/graph.rs","observed":"read file contents","passed":true}],"unresolved":[]}"#,
                "failed",
            ),
            (
                "unresolved items",
                r#"NO CHANGE NEEDED: {"status":"already_satisfied","requirements":[{"requirement":"req","satisfied":true,"evidence":["c1"]}],"checks":[{"id":"c1","kind":"path","target":"src/graph.rs","method":"read src/graph.rs","observed":"read file contents","passed":true}],"unresolved":["Git lock contention remains unresolved"]}"#,
                "unresolved",
            ),
            (
                "empty checks",
                r#"NO CHANGE NEEDED: {"status":"already_satisfied","requirements":[{"requirement":"req","satisfied":true,"evidence":["c1"]}],"checks":[],"unresolved":[]}"#,
                "no verification checks",
            ),
            (
                "empty requirements",
                r#"NO CHANGE NEEDED: {"status":"already_satisfied","requirements":[],"checks":[{"id":"c1","kind":"path","target":"src/graph.rs","method":"read src/graph.rs","observed":"read file contents","passed":true}],"unresolved":[]}"#,
                "no requirements",
            ),
            (
                "unsatisfied requirement",
                r#"NO CHANGE NEEDED: {"status":"already_satisfied","requirements":[{"requirement":"req","satisfied":false,"evidence":["c1"]}],"checks":[{"id":"c1","kind":"path","target":"src/graph.rs","method":"read src/graph.rs","observed":"read file contents","passed":true}],"unresolved":[]}"#,
                "not satisfied",
            ),
            (
                "requirement citing failed check",
                r#"NO CHANGE NEEDED: {"status":"already_satisfied","requirements":[{"requirement":"req","satisfied":true,"evidence":["c1"]}],"checks":[{"id":"c1","kind":"test","target":"lock_test","method":"cargo test --lib lock_test","observed":"FAILED (exit 101)","passed":false},{"id":"c2","kind":"path","target":"src/graph.rs","method":"read src/graph.rs","observed":"contains lock helper","passed":true}],"unresolved":[]}"#,
                "contradictory evidence",
            ),
            (
                "final check failed",
                r#"NO CHANGE NEEDED: {"status":"already_satisfied","requirements":[{"requirement":"req","satisfied":true,"evidence":["c1"]}],"checks":[{"id":"c1","kind":"path","target":"src/graph.rs","method":"read src/graph.rs","observed":"contains lock helper","passed":true},{"id":"c2","kind":"test","target":"lock_test","method":"cargo test --lib lock_test","observed":"FAILED (exit 101)","passed":false}],"unresolved":[]}"#,
                "final verification check",
            ),
            (
                "unknown check reference",
                r#"NO CHANGE NEEDED: {"status":"already_satisfied","requirements":[{"requirement":"req","satisfied":true,"evidence":["c9"]}],"checks":[{"id":"c1","kind":"path","target":"src/graph.rs","method":"read src/graph.rs","observed":"contains lock helper","passed":true}],"unresolved":[]}"#,
                "unknown check",
            ),
            (
                "placeholder observed",
                r#"NO CHANGE NEEDED: {"status":"already_satisfied","requirements":[{"requirement":"req","satisfied":true,"evidence":["c1"]}],"checks":[{"id":"c1","kind":"path","target":"src/graph.rs","method":"read src/graph.rs","observed":"ok","passed":true}],"unresolved":[]}"#,
                "placeholder",
            ),
            (
                "duplicate check id",
                r#"NO CHANGE NEEDED: {"status":"already_satisfied","requirements":[{"requirement":"req","satisfied":true,"evidence":["c1"]}],"checks":[{"id":"c1","kind":"path","target":"src/graph.rs","method":"read src/graph.rs","observed":"found guard","passed":true},{"id":"c1","kind":"path","target":"src/run.rs","method":"read src/run.rs","observed":"found struct","passed":true}],"unresolved":[]}"#,
                "duplicate check id",
            ),
            (
                "duplicate json key",
                r#"NO CHANGE NEEDED: {"status":"incomplete","status":"already_satisfied","requirements":[{"requirement":"req","satisfied":true,"evidence":["c1"]}],"checks":[{"id":"c1","kind":"path","target":"src/graph.rs","method":"read src/graph.rs","observed":"found guard","passed":true}],"unresolved":[]}"#,
                "duplicate field",
            ),
            (
                "unknown json field",
                r#"NO CHANGE NEEDED: {"status":"already_satisfied","extra":1,"requirements":[{"requirement":"req","satisfied":true,"evidence":["c1"]}],"checks":[{"id":"c1","kind":"path","target":"src/graph.rs","method":"read src/graph.rs","observed":"found guard","passed":true}],"unresolved":[]}"#,
                "unknown field",
            ),
            (
                "trailing second json object",
                r#"NO CHANGE NEEDED: {"status":"already_satisfied","requirements":[{"requirement":"req","satisfied":true,"evidence":["c1"]}],"checks":[{"id":"c1","kind":"path","target":"src/graph.rs","method":"read src/graph.rs","observed":"found guard","passed":true}],"unresolved":[]} {"extra":true}"#,
                "trailing",
            ),
            (
                "non-sha commit target",
                r#"NO CHANGE NEEDED: {"status":"already_satisfied","requirements":[{"requirement":"req","satisfied":true,"evidence":["c1"]}],"checks":[{"id":"c1","kind":"commit","target":"HEAD","method":"git log -1 HEAD","observed":"commit exists on HEAD","passed":true}],"unresolved":[]}"#,
                "hex commit SHA",
            ),
            (
                "parent traversal path target",
                r#"NO CHANGE NEEDED: {"status":"already_satisfied","requirements":[{"requirement":"req","satisfied":true,"evidence":["c1"]}],"checks":[{"id":"c1","kind":"path","target":"../outside.rs","method":"read file","observed":"found text","passed":true}],"unresolved":[]}"#,
                "repository-relative path",
            ),
        ];

        for (label, input, want) in cases {
            let err = parse_verified_noop(input)
                .unwrap_or_else(|| panic!("{label}: expected marker match"))
                .expect_err(&format!("{label}: expected rejection"));
            assert!(
                err.contains(want),
                "{label}: expected error containing `{want}`, got `{err}`"
            );
        }
    }

    #[test]
    fn resolved_past_failure_and_earlier_exploratory_failure_are_accepted() {
        let input = r#"NO CHANGE NEEDED: {"status":"already_satisfied","requirements":[{"requirement":"previous unconfirmed command bug is fixed","satisfied":true,"evidence":["c2","c3"]}],"checks":[{"id":"c1","kind":"command","target":"git show deadbeef","method":"git show deadbeef","observed":"initial lookup failed: old commit id was from another branch","passed":false},{"id":"c2","kind":"commit","target":"b32cfc4","method":"git merge-base --is-ancestor b32cfc4 HEAD","observed":"exit 0; previously failed lock issue was fixed by b32cfc4 on main","passed":true},{"id":"c3","kind":"test","target":"has_unconfirmed_command","method":"cargo test --lib has_unconfirmed_command","observed":"test result: ok. 3 passed; 0 failed","passed":true}],"unresolved":[]}"#;
        let report = parse_verified_noop(input)
            .expect("marker present")
            .expect("resolved past failure + passing final check must be accepted");
        assert_eq!(report.cited_checks().len(), 2);
        assert!(verified_noop(input).is_some());
    }

    #[test]
    fn path_locator_stripping_keeps_clean_relative_paths_and_rejects_drive_letters() {
        assert_eq!(strip_path_locator("src/graph.rs:123"), "src/graph.rs");
        assert_eq!(strip_path_locator("src/graph.rs:123-145"), "src/graph.rs");
        assert_eq!(strip_path_locator("src/graph.rs:123:4"), "src/graph.rs");
        assert_eq!(strip_path_locator("src/graph.rs#L123"), "src/graph.rs");
        assert_eq!(strip_path_locator("src/graph.rs#L10-L20"), "src/graph.rs");
        assert!(!is_safe_repo_relative_path(strip_path_locator(
            "C:/tmp/a.rs"
        )));
        assert!(!is_safe_repo_relative_path(strip_path_locator(
            "/etc/hosts"
        )));
        assert!(is_safe_repo_relative_path(strip_path_locator(
            "src/graph.rs:42"
        )));
    }
}