req-cli 0.5.0-rc.6

Managed requirements CLI for LLM agents and humans
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
// REQ-0139: the staged verification dossier (plan → analysis → testing →
// statement → verdict) and the promotion gate it drives. Each test maps to
// the requirement it covers.
mod common;
use common::{stderr, stdout, Sandbox};

/// Add one ordinary functional requirement and walk it to Implemented.
fn implemented_req(s: &Sandbox) {
    s.init("p");
    s.run(&[
        "add",
        "--title",
        "Stop on demand requirement",
        "--statement",
        "The system shall stop the process on operator demand.",
        "--rationale",
        "Operator safety.",
        "--kind",
        "functional",
        "--accept",
        "process halts within one second",
    ]);
    for st in ["proposed", "approved", "implemented"] {
        let r = s.run(&["update", "REQ-0001", "--status", st, "--reason", "step"]);
        assert!(r.status.success(), "step to {}: {}", st, stderr(&r));
    }
}

/// REQ-0139: the full dossier walks plan → analysis → testing → conclude
/// and `--promote` flips status to Verified; the project then conforms
/// cleanly (no REQ-V-0032).
#[test]
fn req_0139_full_dossier_promotes_a_requirement() {
    let s = Sandbox::new();
    implemented_req(&s);
    assert!(s
        .run(&[
            "verification",
            "plan",
            "REQ-0001",
            "--plan",
            "review + run unit test"
        ])
        .status
        .success());
    assert!(s
        .run(&[
            "verification",
            "analysis",
            "REQ-0001",
            "--findings",
            "logic matches the obligation",
            "--result",
            "pass",
        ])
        .status
        .success());
    assert!(s
        .run(&[
            "verification",
            "test",
            "REQ-0001",
            "--findings",
            "suite green",
            "--result",
            "pass",
        ])
        .status
        .success());
    let done = s.run(&[
        "verification",
        "conclude",
        "REQ-0001",
        "--statement",
        "meets the obligation",
        "--promote",
    ]);
    assert!(done.status.success(), "conclude: {}", stderr(&done));
    assert!(stdout(&s.run(&["show", "REQ-0001"])).contains("verified"));
    assert!(s.run(&["conform"]).status.success(), "should conform clean");
}

/// REQ-0139: a requirement cannot be promoted to Verified via `req verify
/// --promote` without a passing dossier.
#[test]
fn req_0139_verify_promote_blocked_without_dossier() {
    let s = Sandbox::new();
    implemented_req(&s);
    let out = s.run(&[
        "verify",
        "REQ-0001",
        "--by",
        "inspection",
        "--notes",
        "looked",
        "--promote",
    ]);
    assert!(!out.status.success(), "promote must be blocked");
    assert!(
        stderr(&out).contains("verification dossier"),
        "stderr: {}",
        stderr(&out)
    );
}

/// REQ-0139: the `verification-exempt` tag lets an ordinary requirement reach
/// Verified without a dossier, and the conformance checker stays quiet.
#[test]
fn req_0139_exempt_tag_bypasses_the_gate() {
    let s = Sandbox::new();
    s.init("p");
    s.run(&[
        "add",
        "--title",
        "Trivial config default",
        "--statement",
        "The system shall default the timeout to thirty seconds.",
        "--rationale",
        "Sane default.",
        "--kind",
        "functional",
        "--accept",
        "default is thirty seconds",
        "--tag",
        "verification-exempt",
    ]);
    for st in ["proposed", "approved", "implemented"] {
        s.run(&["update", "REQ-0001", "--status", st, "--reason", "step"]);
    }
    let out = s.run(&[
        "verify",
        "REQ-0001",
        "--by",
        "inspection",
        "--notes",
        "ok",
        "--promote",
    ]);
    assert!(out.status.success(), "exempt promote: {}", stderr(&out));
    assert!(s.run(&["conform"]).status.success());
}

/// REQ-0139: `req verify --no-dossier --reason` records an audited
/// exemption and promotes; --no-dossier without a reason is rejected.
#[test]
fn req_0139_no_dossier_override_records_audited_exemption() {
    let s = Sandbox::new();
    implemented_req(&s);
    // Missing reason → clap rejects.
    let no_reason = s.run(&[
        "verify",
        "REQ-0001",
        "--by",
        "inspection",
        "--notes",
        "x",
        "--promote",
        "--no-dossier",
    ]);
    assert!(!no_reason.status.success(), "--no-dossier needs --reason");
    // With a reason → promotes and conforms clean.
    let ok = s.run(&[
        "verify",
        "REQ-0001",
        "--by",
        "inspection",
        "--notes",
        "x",
        "--promote",
        "--no-dossier",
        "--reason",
        "trivial; covered by integration suite",
    ]);
    assert!(ok.status.success(), "override: {}", stderr(&ok));
    assert!(s.run(&["conform"]).status.success());
    assert!(stdout(&s.run(&["verification", "show", "REQ-0001"])).contains("exemption"));
}

/// REQ-0139: stage-order is enforced — analysis before plan, testing before
/// analysis, and conclude before testing all fail.
#[test]
fn req_0139_stage_order_is_enforced() {
    let s = Sandbox::new();
    implemented_req(&s);
    // analysis before plan
    assert!(!s
        .run(&[
            "verification",
            "analysis",
            "REQ-0001",
            "--findings",
            "x",
            "--result",
            "pass"
        ])
        .status
        .success());
    s.run(&["verification", "plan", "REQ-0001", "--plan", "p"]);
    // testing before analysis
    assert!(!s
        .run(&[
            "verification",
            "test",
            "REQ-0001",
            "--findings",
            "x",
            "--result",
            "pass"
        ])
        .status
        .success());
    // conclude before testing
    s.run(&[
        "verification",
        "analysis",
        "REQ-0001",
        "--findings",
        "a",
        "--result",
        "pass",
    ]);
    assert!(!s
        .run(&[
            "verification",
            "conclude",
            "REQ-0001",
            "--statement",
            "s",
            "--promote"
        ])
        .status
        .success());
}

/// REQ-0139: a failing stage yields a FAIL verdict, and a FAIL verdict
/// cannot be promoted to Verified.
#[test]
fn req_0139_fail_verdict_blocks_promotion() {
    let s = Sandbox::new();
    implemented_req(&s);
    s.run(&["verification", "plan", "REQ-0001", "--plan", "p"]);
    s.run(&[
        "verification",
        "analysis",
        "REQ-0001",
        "--findings",
        "ok",
        "--result",
        "pass",
    ]);
    s.run(&[
        "verification",
        "test",
        "REQ-0001",
        "--findings",
        "a test fails",
        "--result",
        "fail",
    ]);
    let out = s.run(&[
        "verification",
        "conclude",
        "REQ-0001",
        "--statement",
        "s",
        "--promote",
    ]);
    assert!(!out.status.success(), "FAIL verdict must not promote");
    assert!(stderr(&out).to_lowercase().contains("fail"));
    // Concluding WITHOUT promote is allowed and records the FAIL verdict.
    let recorded = s.run(&["verification", "conclude", "REQ-0001", "--statement", "s"]);
    assert!(recorded.status.success(), "{}", stderr(&recorded));
    assert!(stdout(&s.run(&["verification", "show", "REQ-0001"])).contains("FAIL"));
}

/// REQ-V-0032: a Verified requirement with no passing dossier is a hard
/// verification error.
#[test]
fn req_0139_validator_flags_verified_without_dossier() {
    let s = Sandbox::new();
    s.init("p");
    s.run(&[
        "add",
        "--title",
        "Some verified behaviour",
        "--statement",
        "The system shall expose the baseline behaviour reliably.",
        "--rationale",
        "Fixture.",
        "--kind",
        "constraint",
        "--priority",
        "could",
    ]);
    // Force straight to Verified, bypassing the verify gate at mutation time.
    for st in ["proposed", "approved", "implemented", "verified"] {
        s.run(&[
            "update", "REQ-0001", "--status", st, "--reason", "f", "--force",
        ]);
    }
    let out = s.run(&["conform", "--json"]);
    assert!(!out.status.success(), "conform should fail");
    assert!(
        stdout(&out).contains("REQ-V-0032"),
        "expected REQ-V-0032; got {}",
        stdout(&out)
    );
    // backfill clears it.
    let bf = s.run(&[
        "verification",
        "backfill",
        "--all",
        "--reason",
        "grandfathered",
    ]);
    assert!(bf.status.success(), "backfill: {}", stderr(&bf));
    assert!(
        s.run(&["conform"]).status.success(),
        "conform clean after backfill"
    );
}

/// REQ-0139: re-opening a concluded dossier clears the verdict so the item
/// can be re-verified (e.g. after code changed).
#[test]
fn req_0139_reopen_clears_the_verdict() {
    let s = Sandbox::new();
    implemented_req(&s);
    s.run(&["verification", "plan", "REQ-0001", "--plan", "p"]);
    s.run(&[
        "verification",
        "analysis",
        "REQ-0001",
        "--findings",
        "a",
        "--result",
        "pass",
    ]);
    s.run(&[
        "verification",
        "test",
        "REQ-0001",
        "--findings",
        "t",
        "--result",
        "pass",
    ]);
    s.run(&["verification", "conclude", "REQ-0001", "--statement", "s"]);
    // A second plan without --reopen is rejected.
    assert!(!s
        .run(&["verification", "plan", "REQ-0001", "--plan", "p2"])
        .status
        .success());
    // With --reopen + reason it succeeds and the verdict is cleared.
    let re = s.run(&[
        "verification",
        "plan",
        "REQ-0001",
        "--plan",
        "p2",
        "--reopen",
        "--reason",
        "code changed",
    ]);
    assert!(re.status.success(), "{}", stderr(&re));
    assert!(stdout(&s.run(&["verification", "show", "REQ-0001"])).contains("not concluded"));
}

// ---------------------------------------------------------------------------
// safety requirements
// ---------------------------------------------------------------------------

/// Build a SIL-bearing SR realized through a hazard/SF and walk it to
/// Implemented. Returns nothing; the realized safety requirement is the first one.
fn implemented_sr(s: &Sandbox, sil_high: bool) {
    s.init("p");
    s.enable_safety();
    // C_C/F_B/P_B/W3 → SIL3 (high); C_B/F_A/P_A/W1 → no/low SIL.
    let (c, f, p, w) = if sil_high {
        ("C_C", "F_B", "P_B", "W3")
    } else {
        ("C_B", "F_B", "P_B", "W1")
    };
    s.run(&[
        "hazard", "add", "-t", "H", "--harm", "hurt", "-C", c, "-F", f, "-P", p, "-W", w,
    ]);
    s.run(&["sf", "add", "-t", "F", "--mitigates", "HAZ-0001"]);
    s.run(&[
        "sreq",
        "add",
        "-t",
        "Stop the blade",
        "-s",
        "The system shall stop the blade on demand.",
        "-r",
        "Operator safety.",
        "-a",
        "stops within 200ms",
        "--realizes",
        "SF-0001",
    ]);
    for st in ["approved", "implemented"] {
        s.run(&[
            "sreq", "update", "SR-0001", "--status", st, "--reason", "step",
        ]);
    }
}

/// REQ-0139: a safety requirement cannot be promoted via `sreq verify
/// --promote` without a passing dossier, and there is NO tag exemption.
#[test]
fn req_0139_safety_requirement_requires_dossier_no_exemption() {
    let s = Sandbox::new();
    implemented_sr(&s, false);
    let out = s.run(&[
        "sreq",
        "verify",
        "SR-0001",
        "--by",
        "automated",
        "--promote",
    ]);
    assert!(
        !out.status.success(),
        "SR promote must be blocked without dossier"
    );
    assert!(
        stderr(&out).contains("verification dossier"),
        "stderr: {}",
        stderr(&out)
    );
}

/// REQ-0139: a full dossier promotes a (low-SIL) safety requirement, and
/// the safety case conforms with no REQ-V-0033.
#[test]
fn req_0139_full_dossier_promotes_a_safety_requirement() {
    let s = Sandbox::new();
    implemented_sr(&s, false);
    s.run(&[
        "verification",
        "plan",
        "SR-0001",
        "--plan",
        "review + bench",
    ]);
    s.run(&[
        "verification",
        "analysis",
        "SR-0001",
        "--findings",
        "logic ok",
        "--result",
        "pass",
    ]);
    s.run(&[
        "verification",
        "test",
        "SR-0001",
        "--findings",
        "bench ok",
        "--result",
        "pass",
    ]);
    let done = s.run(&[
        "verification",
        "conclude",
        "SR-0001",
        "--statement",
        "stop obligation met",
        "--promote",
    ]);
    assert!(done.status.success(), "conclude: {}", stderr(&done));
    assert!(stdout(&s.run(&["sreq", "show", "SR-0001"]))
        .to_lowercase()
        .contains("verified"));
    // REQ-0145: a Verified safety requirement also needs a human confirmation
    // of the verification result before the safety case conforms clean.
    assert!(
        s.run(&["verification", "confirm", "SR-0001"])
            .status
            .success(),
        "human confirmation of the SR verification"
    );
    assert!(
        s.run(&["conform"]).status.success(),
        "safety case conforms clean"
    );
}

/// REQ-0139: the SIL-rigour gate still bites under `verification conclude
/// --promote` — a SIL3 SR with only analysis/inspection evidence cannot be
/// promoted without --force.
#[test]
fn req_0139_conclude_promote_respects_sil_gate() {
    let s = Sandbox::new();
    implemented_sr(&s, true); // SIL3, no automated test evidence recorded
    s.run(&["verification", "plan", "SR-0001", "--plan", "review only"]);
    s.run(&[
        "verification",
        "analysis",
        "SR-0001",
        "--findings",
        "reviewed",
        "--result",
        "pass",
    ]);
    s.run(&[
        "verification",
        "test",
        "SR-0001",
        "--findings",
        "manual check",
        "--result",
        "pass",
    ]);
    let blocked = s.run(&[
        "verification",
        "conclude",
        "SR-0001",
        "--statement",
        "ok",
        "--promote",
    ]);
    assert!(
        !blocked.status.success(),
        "SIL3 promote without strong evidence must block"
    );
    assert!(
        stderr(&blocked).contains("SIL-rigour gate"),
        "stderr: {}",
        stderr(&blocked)
    );
    // Concluding without promote still records the dossier.
    assert!(s
        .run(&["verification", "conclude", "SR-0001", "--statement", "ok"])
        .status
        .success());
}

// ---------------------------------------------------------------------------
// REQ-0142: verification provenance report
// ---------------------------------------------------------------------------

/// REQ-0142: a genuinely-concluded dossier classifies as `genuine`, and the
/// report counts it as such; `--not-genuine` then hides it.
#[test]
fn req_0142_report_marks_genuine_dossier() {
    let s = Sandbox::new();
    implemented_req(&s);
    s.run(&[
        "verification",
        "plan",
        "REQ-0001",
        "--plan",
        "review + test",
    ]);
    s.run(&[
        "verification",
        "analysis",
        "REQ-0001",
        "--findings",
        "ok",
        "--result",
        "pass",
    ]);
    s.run(&[
        "verification",
        "test",
        "REQ-0001",
        "--findings",
        "green",
        "--result",
        "pass",
    ]);
    s.run(&[
        "verification",
        "conclude",
        "REQ-0001",
        "--statement",
        "met",
        "--promote",
    ]);
    let rep = s.run(&["verification", "report", "--json"]);
    assert!(rep.status.success(), "report: {}", stderr(&rep));
    let out = stdout(&rep);
    assert!(
        out.contains("\"genuine\": 1"),
        "expected genuine=1; got {}",
        out
    );
    assert!(out.contains("\"exempt_backfilled\": 0"), "got {}", out);
    // --not-genuine hides the genuine item.
    let only_bad = stdout(&s.run(&["verification", "report", "--not-genuine"]));
    assert!(
        !only_bad.contains("REQ-0001 "),
        "genuine item should be filtered out; got {}",
        only_bad
    );
}

/// REQ-0142: a backfilled exemption is reported as `exempt:backfilled`, is
/// surfaced under `--not-genuine`, and `req status` splits it out of the
/// genuine count.
#[test]
fn req_0142_report_marks_backfilled_exemption() {
    let s = Sandbox::new();
    s.init("p");
    s.run(&[
        "add",
        "--title",
        "Some verified behaviour",
        "--statement",
        "The system shall expose the baseline behaviour reliably.",
        "--rationale",
        "Fixture.",
        "--kind",
        "constraint",
        "--priority",
        "could",
    ]);
    for st in ["proposed", "approved", "implemented", "verified"] {
        s.run(&[
            "update", "REQ-0001", "--status", st, "--reason", "f", "--force",
        ]);
    }
    s.run(&[
        "verification",
        "backfill",
        "--all",
        "--reason",
        "grandfathered",
    ]);
    let rep = stdout(&s.run(&["verification", "report", "--json"]));
    assert!(rep.contains("\"genuine\": 0"), "got {}", rep);
    assert!(rep.contains("\"exempt_backfilled\": 1"), "got {}", rep);
    assert!(rep.contains("exempt:backfilled"), "got {}", rep);
    // --not-genuine still lists it.
    let only_bad = stdout(&s.run(&["verification", "report", "--not-genuine"]));
    assert!(only_bad.contains("REQ-0001"), "got {}", only_bad);
    // status splits the verified bucket.
    let st = stdout(&s.run(&["status", "--json"]));
    assert!(
        st.contains("\"verified_provenance\""),
        "status json missing provenance; got {}",
        st
    );
    assert!(st.contains("\"exempt\": 1"), "got {}", st);
}

/// REQ-0143: a safety requirement may NOT be exempted. `backfill <SR>` is
/// refused, `backfill --all` skips it, and a Verified SR without a genuine
/// dossier stays a hard REQ-V-0033 error.
#[test]
fn req_0143_safety_requirement_cannot_be_exempted() {
    let s = Sandbox::new();
    implemented_sr(&s, false);
    // Force the SR to Verified with no dossier (bypassing the verify gate).
    s.run(&[
        "sreq", "update", "SR-0001", "--status", "verified", "--reason", "force",
    ]);
    // It is now a hard conformance error.
    let out = s.run(&["conform", "--json"]);
    assert!(!out.status.success(), "conform should fail");
    assert!(
        stdout(&out).contains("REQ-V-0033"),
        "expected REQ-V-0033; got {}",
        stdout(&out)
    );
    // backfill by id is refused for a safety requirement.
    let bf = s.run(&[
        "verification",
        "backfill",
        "SR-0001",
        "--reason",
        "grandfather",
    ]);
    assert!(!bf.status.success(), "backfill of an SR must be refused");
    assert!(
        stderr(&bf).to_lowercase().contains("safety requirement"),
        "stderr: {}",
        stderr(&bf)
    );
    // backfill --all skips it, so the error remains.
    let bfa = s.run(&[
        "verification",
        "backfill",
        "--all",
        "--reason",
        "grandfather",
    ]);
    assert!(bfa.status.success(), "backfill --all: {}", stderr(&bfa));
    assert!(
        !s.run(&["conform"]).status.success(),
        "SR error must persist — --all does not exempt safety requirements"
    );
}

/// REQ-0188: a Verified safety requirement with a genuine passing dossier but
/// no human confirmation is reported as `unconfirmed` (not `genuine`), and is
/// excluded from the genuine count. A human co-sign flips it to `genuine`.
#[test]
fn req_0150_genuine_but_unconfirmed_sr_reports_unconfirmed() {
    let s = Sandbox::new();
    implemented_sr(&s, false);
    s.run(&[
        "verification",
        "plan",
        "SR-0001",
        "--plan",
        "review + bench",
    ]);
    s.run(&[
        "verification",
        "analysis",
        "SR-0001",
        "--findings",
        "logic ok",
        "--result",
        "pass",
    ]);
    s.run(&[
        "verification",
        "test",
        "SR-0001",
        "--findings",
        "bench ok",
        "--result",
        "pass",
    ]);
    // REQ-0187: conclude --promote on a safety requirement records the genuine
    // dossier but leaves it at Implemented awaiting the human co-sign.
    let done = s.run(&[
        "verification",
        "conclude",
        "SR-0001",
        "--statement",
        "stop obligation met",
        "--promote",
    ]);
    assert!(done.status.success(), "conclude: {}", stderr(&done));

    // REQ-0188: the SR is enumerated with an awaiting-cosign standing, never as
    // verified/genuine, and never omitted.
    let rep = stdout(&s.run(&["verification", "report", "--json"]));
    let rv: serde_json::Value = serde_json::from_str(&rep).expect("report json");
    assert_eq!(
        rv["counts"]["genuine"], 0,
        "un-co-signed SR is not genuine: {}",
        rep
    );
    let srs = rv["safety_requirements"].as_array().unwrap();
    assert!(
        srs.iter()
            .any(|x| x["id"] == "SR-0001" && x["standing"] == "awaiting-cosign"),
        "SR-0001 must be listed as awaiting-cosign: {}",
        rv["safety_requirements"]
    );
    let human = stdout(&s.run(&["verification", "report"]));
    assert!(
        human.contains("awaiting-cosign"),
        "human report should label it awaiting-cosign; got {}",
        human
    );

    // A human co-sign (REQ_ACTOR_KIND unset in tests = human) promotes it to
    // Verified and flips its standing to genuine/verified.
    assert!(
        s.run(&["verification", "confirm", "SR-0001"])
            .status
            .success(),
        "human confirmation of the SR verification"
    );
    let rep2 = stdout(&s.run(&["verification", "report", "--json"]));
    let rv2: serde_json::Value = serde_json::from_str(&rep2).expect("report json");
    assert_eq!(
        rv2["counts"]["genuine"], 1,
        "after co-sign the SR is genuine; got {}",
        rep2
    );
    assert!(
        rv2["safety_requirements"]
            .as_array()
            .unwrap()
            .iter()
            .any(|x| x["id"] == "SR-0001" && x["standing"] == "verified"),
        "after co-sign SR-0001 standing is verified: {}",
        rv2["safety_requirements"]
    );
}

// ---------- REQ-0185: report surfaces the unverified requirements ----------

#[test]
fn req_0185_report_lists_unvalidated_by_stage() {
    let s = Sandbox::new();
    s.init("p");
    // A bare draft (no dossier) and a planned-only requirement.
    let _ = s.run(&[
        "add",
        "-t",
        "Draft requirement here",
        "-s",
        "The system shall do A.",
        "-r",
        "seed",
        "-k",
        "constraint",
        "-p",
        "could",
    ]);
    let _ = s.run(&[
        "add",
        "-t",
        "Planned requirement here",
        "-s",
        "The system shall do B.",
        "-r",
        "seed",
        "-k",
        "constraint",
        "-p",
        "could",
    ]);
    let _ = s.run(&[
        "verification",
        "plan",
        "REQ-0002",
        "--plan",
        "will analyse and test B",
    ]);
    let rep = s.run(&["verification", "report", "--json"]);
    let v: serde_json::Value = serde_json::from_str(&stdout(&rep)).expect("report json");
    assert_eq!(v["unverified_total"], 2, "both reqs are unverified: {}", v);
    let by = &v["unverified_by_stage"];
    assert_eq!(by["no-plan"], 1, "one bare draft: {}", by);
    assert_eq!(by["plan-only"], 1, "one planned-only: {}", by);
}

// ---------- REQ-0162: structured dossier exemption kind ----------

#[test]
fn req_0162_no_dossier_waiver_is_structured() {
    let s = Sandbox::new();
    implemented_req(&s);
    // Waive the dossier explicitly.
    let w = s.run(&[
        "verify",
        "REQ-0001",
        "--by",
        "inspection",
        "--notes",
        "n",
        "--promote",
        "--no-dossier",
        "--reason",
        "covered by external review",
    ]);
    assert!(w.status.success(), "no-dossier waiver: {}", stderr(&w));
    // The structured kind is recorded, not inferred from the plan prefix.
    let show = s.run(&["verification", "show", "REQ-0001", "--json"]);
    let dv: serde_json::Value = serde_json::from_str(&stdout(&show)).expect("show json");
    assert_eq!(
        dv["verification"]["exemption_kind"], "no-dossier",
        "structured kind: {}",
        dv
    );
    // And the provenance report classifies it as the no-dossier waiver.
    let rep = s.run(&["verification", "report", "--json"]);
    let rv: serde_json::Value = serde_json::from_str(&stdout(&rep)).expect("report json");
    assert!(
        rv["items"]
            .as_array()
            .unwrap()
            .iter()
            .any(|i| i["provenance"] == "exempt:no-dossier"),
        "report should show exempt:no-dossier: {}",
        rv["items"]
    );
}

// ---------- REQ-0167: on-behalf-of human attribution ----------

#[test]
fn req_0167_records_on_behalf_of_human() {
    use std::process::Command;
    let s = Sandbox::new();
    implemented_req(&s);
    // An agent makes a change on behalf of a named human.
    let out = Command::new(env!("CARGO_BIN_EXE_req"))
        .args([
            "--file",
            s.path().to_str().unwrap(),
            "update",
            "REQ-0001",
            "--add-tag",
            "reviewed",
        ])
        .env_remove("REQ_FILE")
        .env("REQ_ACTOR", "claude")
        .env("REQ_ACTOR_KIND", "agent")
        .env("REQ_ON_BEHALF_OF", "Alice <alice@example.com>")
        .output()
        .expect("invoke req");
    assert!(
        out.status.success(),
        "update: {}",
        String::from_utf8_lossy(&out.stderr)
    );
    let show = s.run(&["show", "REQ-0001"]);
    let body = stdout(&show);
    assert!(
        body.contains("for Alice <alice@example.com>"),
        "history should record the on-behalf-of human:\n{}",
        body
    );
}

// ---------- REQ-0191: verification status reachable under an obvious name ----------

#[test]
fn req_0191_verification_status_alias_and_status_pointer() {
    let s = Sandbox::new();
    s.init("p");
    let _ = s.run(&[
        "add",
        "-t",
        "Seed requirement here",
        "-s",
        "The system shall do a thing.",
        "-r",
        "seed",
        "-k",
        "constraint",
        "-p",
        "could",
    ]);
    // `req verification status` is an alias of the report (same shape).
    let st = s.run(&["verification", "status", "--json"]);
    assert!(st.status.success(), "verification status: {}", stderr(&st));
    let v: serde_json::Value = serde_json::from_str(&stdout(&st)).expect("status json");
    assert!(
        v.get("counts").is_some(),
        "status carries the provenance counts: {}",
        v
    );
    assert!(
        v.get("unverified_total").is_some(),
        "and the unverified surface"
    );
    // `req status` points to the true verification standing.
    let status = stdout(&s.run(&["status"]));
    assert!(
        status.contains("verification status"),
        "status must point to V&V truth:\n{}",
        status
    );
}

/// REQ-0200: `reverify --by-tests` re-anchors a stale ordinary requirement
/// whose tests pass, recording the test run as the evidence — no agent review.
#[test]
fn req_0200_reverify_by_tests_reanchors_stale_passing() {
    use std::process::Command;
    let s = Sandbox::new();
    s.init("p");
    let dir = s.dir.path();
    let pf = s.path();
    let pf_s = pf.to_str().unwrap().to_string();
    // Run req with cwd = sandbox so the source root "." resolves to the sandbox.
    let run = |args: &[&str]| {
        let mut full: Vec<String> = vec!["--file".into(), pf_s.clone()];
        full.extend(args.iter().map(|a| a.to_string()));
        Command::new(env!("CARGO_BIN_EXE_req"))
            .current_dir(dir)
            .args(&full)
            .env_remove("REQ_FILE")
            .output()
            .expect("invoke req")
    };
    // A source file carrying the REQ-0001 marker, so the dossier anchors on it.
    let impl_path = dir.join("impl.rs");
    std::fs::write(&impl_path, "// REQ-0001: stop on demand\nfn stop() {}\n").unwrap();
    assert!(run(&[
        "add",
        "--title",
        "Stop on demand",
        "--statement",
        "The system shall stop the process on operator demand.",
        "--rationale",
        "operator safety",
        "--accept",
        "stops on demand",
        "-k",
        "functional",
        "-p",
        "must",
    ])
    .status
    .success());
    run(&[
        "update",
        "REQ-0001",
        "--status",
        "implemented",
        "--reason",
        "implemented for reverify test",
        "--force",
    ]);
    run(&[
        "verification",
        "plan",
        "REQ-0001",
        "--plan",
        "review + test",
    ]);
    run(&[
        "verification",
        "analysis",
        "REQ-0001",
        "--result",
        "pass",
        "--findings",
        "reviewed impl.rs",
    ]);
    run(&[
        "verification",
        "test",
        "REQ-0001",
        "--result",
        "pass",
        "--findings",
        "tested",
    ]);
    let c = run(&[
        "verification",
        "conclude",
        "REQ-0001",
        "--statement",
        "met",
        "--promote",
    ]);
    assert!(
        c.status.success(),
        "conclude: {}",
        String::from_utf8_lossy(&c.stderr)
    );

    // Drift the linked file → the dossier goes stale.
    std::fs::write(
        &impl_path,
        "// REQ-0001: stop on demand\nfn stop() { /* changed */ }\n",
    )
    .unwrap();
    // A captured cargo-test log with a passing req_0001 test.
    std::fs::write(dir.join("log.txt"), "test req_0001_stops ... ok\n").unwrap();

    let out = run(&[
        "verification",
        "reverify",
        "--by-tests",
        "--from-file",
        "log.txt",
        "--json",
    ]);
    assert!(
        out.status.success(),
        "reverify: {}",
        String::from_utf8_lossy(&out.stderr)
    );
    let v: serde_json::Value = serde_json::from_str(&stdout(&out)).expect("reverify json");
    let reanchored: Vec<String> = v["reanchored"]
        .as_array()
        .unwrap()
        .iter()
        .map(|x| x.as_str().unwrap().to_string())
        .collect();
    assert!(
        reanchored.contains(&"REQ-0001".to_string()),
        "REQ-0001 should be re-anchored from its passing test:\n{}",
        stdout(&out)
    );
    // A second reverify finds nothing stale (the anchor is fresh again).
    let again = run(&[
        "verification",
        "reverify",
        "--by-tests",
        "--from-file",
        "log.txt",
        "--json",
    ]);
    let v2: serde_json::Value = serde_json::from_str(&stdout(&again)).unwrap();
    assert!(
        v2["reanchored"].as_array().unwrap().is_empty(),
        "nothing should remain stale after re-anchor:\n{}",
        stdout(&again)
    );
}