vtcode-core 0.177.1

Core library for VT Code - a Rust-based terminal coding agent
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
use super::*;
use serde_json::json;

#[tokio::test]
async fn required_inspections_accept_successful_and_chains_without_masked_status_or_context_changes() {
    for (command, passes) in [
        ("sed -n '35,90p' README.md && sed -n '90,240p' README.md", true),
        ("sed -n '35,90p' README.md; true", false),
        ("sed -n '35,90p' README.md || true", false),
        ("! sed -n '35,90p' README.md && true", false),
        ("! (false && sed -n '35,90p' README.md) && true", false),
        ("sed -n '35,90p' README.md | cat", false),
        ("cd other && sed -n '35,90p' README.md", false),
        ("sed -n '35,90p' README.md && rm README.md", false),
        ("echo \"sed -n '35,90p' README.md\" && true", false),
    ] {
        let dir = tempfile::tempdir().unwrap();
        let registry = ToolRegistry::new(dir.path().to_path_buf()).await;
        registry.restore_completion_verification_debt();
        registry.set_required_completion_verifiers(vec!["sed -n '35,90p' README.md".into()]);
        let args = json!({"cmd":command});
        let id = registry.begin_completion_verifier(&args);
        registry.observe_completion_verifier(id, &args, &Ok(json!({"exit_code":0})));
        assert_eq!(registry.completion_verification_pending(), !passes, "{command}");
        if passes {
            for index in 0..40 {
                let optional = json!({"cmd":format!("cargo check -p optional_{index}")});
                let id = registry.begin_completion_verifier(&optional);
                registry.observe_completion_verifier(id, &optional, &Ok(json!({"exit_code":1})));
            }
            assert!(!registry.completion_verification_pending(), "required chain evidence must stay pinned");
            registry.restore_completion_verification_debt();
        }
    }
}

#[tokio::test]
async fn completion_verification_bulk_status_sync_retains_required_checks() {
    for planning in [false, true] {
        let dir = tempfile::tempdir().unwrap();
        let registry = ToolRegistry::new(dir.path().to_path_buf()).await;
        registry.allow_all_tools().await.unwrap();
        registry.restore_completion_verification_debt();
        if planning {
            let state = registry.planning_workflow_state();
            let plan_file = state.plans_dir().join("verification.md");
            std::fs::create_dir_all(plan_file.parent().unwrap()).unwrap();
            std::fs::write(&plan_file, "# Verification\n").unwrap();
            state.set_plan_file(Some(plan_file)).await;
            state.enable();
            registry.enable_planning();
        }
        registry
            .execute_tool(
                tools::TASK_TRACKER,
                json!({"action":"create", "items":[
                    {"description":"Check build", "verify":["cargo check"]},
                    {"description":"Check lints", "verify":["cargo clippy"]}
                ]}),
            )
            .await
            .unwrap();
        let args = json!({"action":"update", "items":["[x] Check lints", "[x] Check build"]});
        let output = registry.execute_tool(tools::TASK_TRACKER, args.clone()).await.unwrap();
        assert_eq!(output["status"], "updated", "{output}");
        assert_eq!(registry.missing_required_completion_verifiers(), vec!["cargo clippy", "cargo check"]);
        let verifier_args = json!({"cmd":"cargo check"});
        let id = registry.begin_completion_verifier(&verifier_args);
        registry.observe_completion_verifier(id, &verifier_args, &Ok(json!({"exit_code":0})));
        assert_eq!(registry.missing_required_completion_verifiers(), vec!["cargo clippy"]);
        registry.begin_tracker_request(false);
        assert!(!registry.tracker_adopted_for_request());
        let repeated = registry.execute_tool(tools::TASK_TRACKER, args).await.unwrap();
        assert_eq!(repeated["status"], "unchanged", "{repeated}");
        assert!(registry.tracker_adopted_for_request(), "unchanged live updates adopt the new request");
        assert_eq!(registry.missing_required_completion_verifiers(), vec!["cargo clippy"]);
    }
}

#[test]
fn completion_verification_requires_fresh_terminal_evidence_after_one_five_and_six_edits() {
    for edits in [1, 5, 6] {
        let mut state = CompletionVerification::default();
        assert!(!state.pending());
        for _ in 0..edits {
            state.mutate();
        }
        assert!(state.pending());
        let verifier = state.launch("cargo check".into());
        state.observe(verifier, &json!({"session_id":"check-a"}));
        assert!(state.pending());
        state.observe(verifier, &json!({"exit_code":0}));
        assert!(!state.pending());
        state.mutate();
        assert!(state.pending());
    }
}

#[test]
fn completion_verification_rejects_stale_and_superseded_checks() {
    let mut state = CompletionVerification::default();
    let old = state.launch("pytest".into());
    state.mutate();
    state.observe(old, &json!({"exit_code":0}));
    assert!(state.pending());
    let current = state.launch("pytest".into());
    state.observe(current, &json!({"exit_code":7}));
    assert!(state.pending());
    state.observe(old, &json!({"exit_code":0}));
    assert!(state.pending());
}

#[test]
fn completion_verification_all_required_tracker_checks_must_pass() {
    let mut state = CompletionVerification::default();
    state.mutate();
    state.required_commands = vec!["cargo check".into(), "cargo clippy".into()];
    for (command, pending) in [("pytest", true), ("cargo check", true), ("cargo clippy", false)] {
        let id = state.launch(command.into());
        state.observe(id, &json!({"exit_code":0}));
        assert_eq!(state.pending(), pending);
    }
}

#[test]
fn completion_verification_bounds_superseded_chain_evidence_per_requirement() {
    let mut state = CompletionVerification::default();
    state.mutate();
    state.required_commands = vec!["cargo check".into(), "cargo clippy".into()];
    let both = state.launch("cargo check && cargo clippy".into());
    state.observe(both, &json!({"exit_code":0}));
    let live = state.launch("cargo check && printf 'live'".into());
    state.observe(live, &json!({"session_id":"older-live-chain"}));
    for index in 0..64 {
        let id = state.launch(format!("cargo check && printf 'check-{index}'"));
        state.observe(id, &json!({"exit_code":0}));
        assert!(!state.pending(), "the older chain still owns the clippy verdict");
        assert!(state.evidence.len() <= OPTIONAL_EVIDENCE_LIMIT + 3);
    }
    assert_eq!(state.evidence.len(), OPTIONAL_EVIDENCE_LIMIT + 3);
    assert!(state.evidence.iter().any(|entry| entry.identity == live));
    state.observe(live, &json!({"exit_code":7}));
    assert_eq!(state.evidence.len(), OPTIONAL_EVIDENCE_LIMIT + 2);
    assert!(!state.pending(), "the older live failure cannot override the latest check verdict");
    let latest = state.launch("cargo check && printf 'latest'".into());
    state.observe(latest, &json!({"exit_code":7}));
    assert!(state.pending(), "an older pass cannot override the latest failure");
    assert!(state.command_passed("cargo clippy"));
    assert!(!state.command_passed("cargo check"));
}

#[tokio::test]
async fn completion_verification_resumes_required_segments_of_live_chains() {
    for exit_code in [0, 7] {
        let dir = tempfile::tempdir().unwrap();
        let registry = ToolRegistry::new(dir.path().to_path_buf()).await;
        registry.restore_completion_verification_debt();
        registry.set_required_completion_verifiers(vec!["cargo check".into(), "cargo clippy".into()]);
        let args = json!({"cmd":"cargo check && cargo clippy"});
        let launch = registry.begin_completion_verifier(&args);
        assert!(launch.is_some());
        registry.observe_completion_verifier(launch, &args, &Ok(json!({"session_id":"owned-chain"})));
        for required in ["cargo check", "cargo clippy"] {
            assert_eq!(registry.pending_completion_verifier_session(required).as_deref(), Some("owned-chain"));
            assert!(!registry.completion_verifier_passed(required));
        }
        registry.observe_completion_verifier(
            None,
            &json!({"action":"wait", "session_id":"owned-chain"}),
            &Ok(json!({"session_id":"owned-chain", "exit_code":exit_code})),
        );
        assert_eq!(registry.completion_verification_pending(), exit_code != 0);
        for required in ["cargo check", "cargo clippy"] {
            assert!(registry.pending_completion_verifier_session(required).is_none());
            assert_eq!(registry.completion_verifier_passed(required), exit_code == 0);
        }
    }
}

#[test]
fn completion_verification_retains_required_checks_under_optional_evidence_pressure() {
    for required_count in [1, 31, 32, 33, 40] {
        let mut state = CompletionVerification::default();
        state.mutate();
        state.required_commands = (0..required_count)
            .map(|index| format!("cargo check --package package_{index}"))
            .collect();
        for command in state.required_commands.clone() {
            let id = state.launch(command);
            state.observe(id, &json!({"exit_code":0}));
        }
        assert!(!state.pending());
        for index in 0..64 {
            let id = state.launch(format!("pytest optional_{index}"));
            state.observe(id, &json!({"exit_code":7}));
            assert!(!state.pending(), "optional check {index} must not erase {required_count} required passes");
        }
        assert_eq!(state.evidence.len(), required_count + 32);
        assert!(state.evidence.iter().any(|entry| entry.command == "pytest optional_32"));
        assert!(!state.evidence.iter().any(|entry| entry.command == "pytest optional_31"));
        state.mutate();
        assert!(state.pending(), "retained required passes must still become stale after edits");
    }
}

#[tokio::test]
async fn completion_verification_unpins_old_requirements_and_preserves_pipeline_identity() {
    let dir = tempfile::tempdir().unwrap();
    let registry = ToolRegistry::new(dir.path().to_path_buf()).await;
    registry.restore_completion_verification_debt();
    let commands: Vec<_> = (0..40)
        .map(|index| format!("cargo check --package package_{index} 2>&1 | tail -5"))
        .collect();
    registry.set_required_completion_verifiers(commands.clone());
    for command in &commands {
        let args = json!({"action":"run", "command":command});
        let id = registry.begin_completion_verifier(&args);
        assert!(id.is_some());
        registry.observe_completion_verifier(id, &args, &Ok(json!({"exit_code":0})));
    }
    let args = json!({"action":"run", "command":"pytest optional"});
    let id = registry.begin_completion_verifier(&args);
    registry.observe_completion_verifier(id, &args, &Ok(json!({"exit_code":0})));
    assert!(!registry.completion_verification_pending());
    assert!(registry.completion_verifier_passed(&commands[0]));
    registry.begin_tracker_request(false);
    let state = registry.harness_context.completion_verification.lock();
    assert_eq!(state.evidence.len(), 32, "released requirements return to the ordinary evidence bound");
    assert_eq!(state.evidence.back().unwrap().command, "pytest optional");
}

#[tokio::test]
async fn completion_verification_reports_the_exact_remaining_check() {
    let dir = tempfile::tempdir().unwrap();
    let registry = ToolRegistry::new(dir.path().to_path_buf()).await;
    registry.restore_completion_verification_debt();
    registry.set_required_completion_verifiers(vec!["cargo check".into(), "grep -n '^## ' README.md".into()]);
    let args = json!({"cmd":"cargo check"});
    let launch = registry.begin_completion_verifier(&args);
    registry.observe_completion_verifier(launch, &args, &Ok(json!({"exit_code":0})));
    let reason = registry.unverified_completion_reason();
    assert!(reason.contains("Next required tracker check: `grep -n '^## ' README.md`"), "{reason}");
    assert!(reason.contains("a different passing check does not satisfy it"), "{reason}");
    assert!(registry.completion_verification_pending());
}

#[tokio::test]
async fn completion_verification_npx_logging_does_not_invalidate_required_evidence() {
    let dir = tempfile::tempdir().unwrap();
    let registry = ToolRegistry::new(dir.path().to_path_buf()).await;
    registry.restore_completion_verification_debt();
    registry.set_required_completion_verifiers(vec!["cargo check".into()]);
    let args = json!({"cmd":"cargo check"});
    let launch = registry.begin_completion_verifier(&args);
    registry.observe_completion_verifier(launch, &args, &Ok(json!({"exit_code":0})));
    for (command, pending) in [
        ("npx --yes --loglevel=error markdownlint-cli2@0.23.3 README.md", false),
        ("npx --yes --loglevel=error markdownlint-cli2@0.23.3 --fix README.md", true),
    ] {
        registry.observe_completion_mutation(tools::EXEC_COMMAND, &json!({"cmd":command}), &json!({"exit_code":0}));
        assert_eq!(registry.completion_verification_pending(), pending, "{command}");
        assert_eq!(registry.completion_verifier_passed("cargo check"), !pending, "{command}");
    }
}

#[test]
fn completion_verification_required_yielded_check_survives_eviction_and_supersession() {
    let mut state = CompletionVerification::default();
    state.mutate();
    state.required_commands = vec!["cargo check 2>&1 | tail -5".into()];
    let running = state.launch("cargo check 2>&1".into());
    state.observe(running, &json!({"session_id":"required-check"}));
    for index in 0..64 {
        let id = state.launch(format!("pytest optional_{index}"));
        state.observe(id, &json!({"exit_code":0}));
    }
    assert!(state.pending(), "optional passes cannot satisfy the yielded requirement");
    assert!(
        state
            .evidence
            .iter()
            .any(|entry| entry.session_id.as_deref() == Some("required-check"))
    );
    state.observe(running, &json!({"exit_code":0}));
    assert!(!state.pending());
    let replacement = state.launch("cargo check 2>&1".into());
    state.observe(running, &json!({"exit_code":0}));
    assert!(state.pending(), "superseded evidence cannot replace the newest attempt");
    state.observe(replacement, &json!({"exit_code":7}));
    assert!(state.pending());
    assert_eq!(state.evidence.len(), 33, "one required check plus 32 optional checks");
}

#[tokio::test]
async fn completion_verification_preserves_debt_across_compaction_and_ignores_noop_and_prose() {
    let dir = tempfile::tempdir().unwrap();
    let registry = ToolRegistry::new(dir.path().to_path_buf()).await;
    for (path, output) in [
        ("README.md", json!({"success":true})),
        (".vtcode/plans/plan.md", json!({"success":true})),
        ("src/main.rs", json!({"success":false,"error":"conflict"})),
        ("src/noop.rs", json!({"success":true,"diff_preview":{"is_empty":true}})),
    ] {
        registry.observe_completion_mutation(tools::WRITE_FILE, &json!({"path":path}), &output);
        assert!(!registry.completion_verification_pending());
    }
    registry.observe_completion_mutation(tools::WRITE_FILE, &json!({"path":"src/main.rs"}), &json!({"success":true}));
    assert!(registry.completion_verification_pending());
    registry.reconcile_source_inspection(&[]);
    assert!(registry.completion_verification_pending());
}

#[tokio::test]
async fn completion_verification_unrelated_lost_cancelled_and_cleanup_sessions_cannot_pass() {
    let dir = tempfile::tempdir().unwrap();
    let registry = ToolRegistry::new(dir.path().to_path_buf()).await;
    registry.restore_completion_verification_debt();
    let args = json!({"action":"run", "command":"cargo check"});
    let launch = registry.begin_completion_verifier(&args);
    assert!(launch.is_some());
    registry.observe_completion_verifier(launch, &args, &Ok(json!({"session_id":"owned"})));
    registry.observe_completion_verifier(
        None,
        &json!({"action":"wait", "session_id":"other"}),
        &Ok(json!({"exit_code":0})),
    );
    assert!(registry.completion_verification_pending());
    registry.observe_completion_verifier(
        None,
        &json!({"action":"wait", "session_id":"owned"}),
        &Ok(json!({"cancelled":true, "exit_code":0})),
    );
    assert!(registry.completion_verification_pending());
    registry.observe_completion_verifier(
        None,
        &json!({"action":"wait", "session_id":"owned"}),
        &Ok(json!({"exit_code":0})),
    );
    assert!(registry.completion_verification_pending());
    let launch = registry.begin_completion_verifier(&args);
    registry.observe_completion_verifier(launch, &args, &Ok(json!({"session_id":"fresh"})));
    registry.observe_completion_verifier(
        None,
        &json!({"action":"wait", "session_id":"fresh"}),
        &Ok(json!({"exit_code":0})),
    );
    assert!(!registry.completion_verification_pending());
}

#[tokio::test]
async fn completion_verification_runtime_evidence_survives_source_projection_clearing_in_both_modes() {
    use crate::llm::provider::{Message, ToolCall};
    for mode in [
        vtcode_config::core::agent::InspectionMode::Warn,
        vtcode_config::core::agent::InspectionMode::Enforce,
    ] {
        let dir = tempfile::tempdir().unwrap();
        let registry = ToolRegistry::new(dir.path().to_path_buf()).await;
        registry.set_inspection_mode(mode);
        registry.allow_all_tools().await.unwrap();
        let patch = json!({"input":"*** Begin Patch\n*** Add File: main.rs\n+fn main() {}\n*** End Patch"});
        let output = registry.execute_tool(tools::APPLY_PATCH, patch).await.unwrap();
        assert!(output.get("error").is_none(), "{output}");
        assert!(registry.completion_verification_pending());
        let check = registry
            .execute_harness_command_session(json!({"action":"run", "command":"rustc --version"}))
            .await
            .unwrap();
        assert_eq!(check["exit_code"], 0, "{check}");
        assert!(!registry.completion_verification_pending());
        registry.reconcile_source_inspection(&[]);
        assert!(!registry.completion_verification_pending());
        let failed_args = json!({"action":"run", "command":"cargo check"});
        let launch = registry.begin_completion_verifier(&failed_args);
        registry.observe_completion_verifier(launch, &failed_args, &Ok(json!({"exit_code":7})));
        assert!(registry.completion_verification_pending());
        let generation_before_repair = registry.harness_context.completion_verification.lock().generation;
        let repair =
            json!({"path":"main.rs","old_str":"fn main() {}","new_str":"fn main() { println!(\"changed\"); }"});
        let rejected_or_advisory = registry.execute_tool(tools::EDIT_FILE, repair.clone()).await.unwrap();
        if mode == vtcode_config::core::agent::InspectionMode::Enforce {
            assert!(rejected_or_advisory.get("error").is_some(), "{rejected_or_advisory}");
            assert!(registry.completion_verification_pending(), "the failed verifier remains pending");
            assert_eq!(
                registry.harness_context.completion_verification.lock().generation,
                generation_before_repair,
                "rejected edit adds no debt"
            );
            let args = json!({"path":"main.rs"});
            let read = registry.execute_tool(tools::READ_FILE, args.clone()).await.unwrap();
            registry.reconcile_source_inspection(&[
                Message::assistant_with_tools(
                    String::new(),
                    vec![ToolCall::function(
                        "read".into(),
                        tools::READ_FILE.into(),
                        args.to_string(),
                    )],
                ),
                Message::tool_response("read".into(), read.to_string()),
            ]);
            let output = registry.execute_tool(tools::EDIT_FILE, repair).await.unwrap();
            assert!(output.get("error").is_none(), "{output}");
        } else {
            assert!(rejected_or_advisory.get("error").is_none(), "{rejected_or_advisory}");
        }
        assert!(registry.completion_verification_pending());
        assert_eq!(registry.harness_context.completion_verification.lock().generation, generation_before_repair + 1);
    }
}

#[tokio::test]
async fn completion_verification_restore_rejects_historical_pass_and_cleared_results() {
    use crate::llm::provider::{Message, ToolCall};
    for mutation in [json!({"success":true}), json!({"cleared":"tool_result"})] {
        let dir = tempfile::tempdir().unwrap();
        let registry = ToolRegistry::new(dir.path().to_path_buf()).await;
        registry.restore_completion_verification_from_history(&[
            Message::assistant_with_tools(
                String::new(),
                vec![ToolCall::function(
                    "edit".into(),
                    tools::WRITE_FILE.into(),
                    json!({"path":"main.rs"}).to_string(),
                )],
            ),
            Message::tool_response("edit".into(), mutation.to_string()),
            Message::assistant_with_tools(
                String::new(),
                vec![ToolCall::function(
                    "check".into(),
                    tools::EXEC_COMMAND.into(),
                    json!({"cmd":"rustc --version"}).to_string(),
                )],
            ),
            Message::tool_response("check".into(), json!({"exit_code":0}).to_string()),
        ]);
        assert!(registry.completion_verification_pending());
    }
}

#[tokio::test]
async fn completion_verification_lost_and_closed_sessions_have_no_late_verdict() {
    let dir = tempfile::tempdir().unwrap();
    let registry = ToolRegistry::new(dir.path().to_path_buf()).await;
    registry.restore_completion_verification_debt();
    let launch_args = json!({"action":"run","command":"cargo check"});
    for cleanup in [false, true] {
        let id = registry.begin_completion_verifier(&launch_args);
        registry.observe_completion_verifier(id, &launch_args, &Ok(json!({"session_id":"check"})));
        registry.observe_completion_verifier(
            None,
            &json!({"action":"wait","session_id":"check"}),
            &Ok(json!({"session_id":"unrelated","exit_code":0})),
        );
        assert!(registry.completion_verification_pending());
        let (args, result) = if cleanup {
            (json!({"action":"close","session_id":"check"}), Ok(json!({"success":true})))
        } else {
            (json!({"action":"wait","session_id":"check"}), Err(anyhow::anyhow!("exec session not found")))
        };
        registry.observe_completion_verifier(None, &args, &result);
        registry.observe_completion_verifier(
            None,
            &json!({"action":"wait","session_id":"check"}),
            &Ok(json!({"exit_code":0})),
        );
        assert!(registry.completion_verification_pending());
    }
}

#[tokio::test]
async fn completion_verification_waits_for_mutating_sessions_before_accepting_checks() {
    let dir = tempfile::tempdir().unwrap();
    let registry = ToolRegistry::new(dir.path().to_path_buf()).await;
    registry.observe_completion_mutation(
        tools::EXEC_COMMAND,
        &json!({"cmd":"cargo fmt"}),
        &json!({"session_id":"formatter"}),
    );
    let args = json!({"action":"run","command":"cargo check"});
    let id = registry.begin_completion_verifier(&args);
    registry.observe_completion_verifier(id, &args, &Ok(json!({"exit_code":0})));
    assert!(registry.completion_verification_pending(), "formatter is still active");
    registry.observe_completion_verifier(
        None,
        &json!({"action":"wait","session_id":"formatter"}),
        &Ok(json!({"exit_code":0})),
    );
    assert!(registry.completion_verification_pending(), "formatter completion invalidates the earlier check");
    let id = registry.begin_completion_verifier(&args);
    registry.observe_completion_verifier(id, &args, &Ok(json!({"exit_code":0})));
    assert!(!registry.completion_verification_pending());
}

#[cfg(all(unix, feature = "tui"))]
#[tokio::test]
async fn completion_verification_bulk_termination_reconciles_only_gone_sessions() {
    use crate::tools::pty::PtySize;
    use hashbrown::HashMap;
    let dir = tempfile::tempdir().unwrap();
    let registry = ToolRegistry::new(dir.path().to_path_buf()).await;
    // Spawn sessions under the registry's canonical workspace root: on macOS
    // the tempdir path goes through the `/var` → `/private/var` symlink and
    // the session guard rejects non-canonical working dirs.
    let workspace = registry.workspace_root().clone();
    let manager = registry.exec_session_manager();
    let size = PtySize {
        rows: 24,
        cols: 80,
        pixel_width: 0,
        pixel_height: 0,
    };
    let spawn = |id: &str| vec!["/bin/sh".to_string(), "-c".to_string(), format!("sleep 30 # {id}")];
    manager
        .create_pty_session(
            "foreground-mutator".to_string().into(),
            spawn("foreground-mutator"),
            workspace.clone(),
            size,
            HashMap::new(),
            None,
        )
        .await
        .unwrap();
    manager
        .create_pty_session_for_managed_background(
            "background-mutator".to_string().into(),
            spawn("background-mutator"),
            workspace.clone(),
            size,
            HashMap::new(),
            None,
            HashMap::new(),
            false,
        )
        .await
        .unwrap();
    for session in ["foreground-mutator", "background-mutator"] {
        registry.observe_completion_mutation(
            tools::EXEC_COMMAND,
            &json!({"cmd":"cargo fmt"}),
            &json!({"session_id":session}),
        );
    }
    assert!(registry.completion_verification_pending());
    registry.terminate_active_exec_sessions_async().await.unwrap();
    let live: Vec<String> = manager
        .list_sessions()
        .await
        .into_iter()
        .map(|session| session.id.to_string())
        .collect();
    assert!(!live.contains(&"foreground-mutator".to_string()), "foreground session must be gone: {live:?}");
    assert!(live.contains(&"background-mutator".to_string()), "background session is preserved: {live:?}");
    assert!(registry.completion_verification_pending(), "the preserved background mutator still owns the gate");
    registry.terminate_all_exec_sessions_async().await.unwrap();
    assert!(
        registry.completion_verification_pending(),
        "terminated mutators release the session block, not the code debt"
    );
    let args = json!({"action":"run", "command":"cargo check"});
    let launch = registry.begin_completion_verifier(&args);
    assert!(launch.is_some());
    registry.observe_completion_verifier(launch, &args, &Ok(json!({"exit_code":0})));
    assert!(
        !registry.completion_verification_pending(),
        "a fresh check after bulk termination must satisfy the gate"
    );
}

#[tokio::test]
async fn completion_verification_restored_mutators_create_debt_without_live_handles() {
    use crate::llm::provider::{Message, ToolCall};
    for archived_terminal in [None, Some(0), Some(7)] {
        let dir = tempfile::tempdir().unwrap();
        let registry = ToolRegistry::new(dir.path().to_path_buf()).await;
        let mut history = vec![
            Message::assistant_with_tools(
                String::new(),
                vec![ToolCall::function(
                    "format".into(),
                    tools::EXEC_COMMAND.into(),
                    json!({"cmd":"cargo fmt"}).to_string(),
                )],
            ),
            Message::tool_response("format".into(), json!({"session_id":"archived-formatter"}).to_string()),
        ];
        if let Some(exit_code) = archived_terminal {
            history.extend([
                Message::assistant_with_tools(
                    String::new(),
                    vec![ToolCall::function(
                        "wait".into(),
                        tools::WRITE_STDIN.into(),
                        json!({"action":"wait","session_id":"archived-formatter"}).to_string(),
                    )],
                ),
                Message::tool_response("wait".into(), json!({"exit_code":exit_code}).to_string()),
            ]);
        }
        registry.restore_completion_verification_from_history(&history);
        assert!(registry.completion_verification_pending());
        assert!(
            registry
                .harness_context
                .completion_verification
                .lock()
                .pending_mutation_sessions
                .is_empty(),
            "archived sessions cannot be owned by this registry"
        );
        let fresh = registry
            .execute_harness_command_session(json!({"action":"run","command":"rustc --version"}))
            .await
            .unwrap();
        assert_eq!(fresh["exit_code"], 0, "{fresh}");
        assert!(!registry.completion_verification_pending());
    }
}

#[tokio::test]
async fn completion_verification_code_targets_cannot_use_prose_or_planning_exemptions() {
    for path in ["CMakeLists.txt", "requirements.txt"] {
        let dir = tempfile::tempdir().unwrap();
        let registry = ToolRegistry::new(dir.path().to_path_buf()).await;
        registry.allow_all_tools().await.unwrap();
        let check = registry
            .execute_harness_command_session(json!({"action":"run", "command":"rustc --version"}))
            .await
            .unwrap();
        assert_eq!(check["exit_code"], 0, "{check}");
        let output = registry
            .execute_tool(tools::WRITE_FILE, json!({"path":path, "content":"changed\n"}))
            .await
            .unwrap();
        assert!(output.get("error").is_none(), "{output}");
        assert_eq!(std::fs::read_to_string(dir.path().join(path)).unwrap(), "changed\n");
        assert!(registry.completion_verification_pending(), "code target {path}: {output}");
        assert!(!registry.completion_verifier_passed("rustc --version"));
    }
}

#[test]
fn completion_verification_temp_plans_root_matches_real_temp_dir() {
    use crate::tools::tool_intent::tmp_plans_dir;
    let root = tmp_plans_dir();
    let root_str = root.to_str().expect("temp plans root must be UTF-8").to_owned();
    for path in [
        root_str.clone(),
        format!("{root_str}/draft.md"),
        format!("{root_str}/workspace-slug/draft.md"),
    ] {
        assert!(
            tool_intent::is_plan_artifact_write(tools::WRITE_FILE, &json!({"path": path})),
            "{path} must classify as a plan artifact under {}",
            root.display()
        );
    }
}

#[test]
fn completion_verification_exemptions_require_all_normalized_targets() {
    for args in [
        json!({"action":"move", "path":".vtcode/plans/draft.rs", "destination":"src/main.rs"}),
        json!({"action":"write", "path":"src/main.rs", "filePath":".vtcode/plans/draft.rs"}),
        json!({"action":"write", "path":".vtcode/plans/../../src/main.rs"}),
    ] {
        assert!(!tool_intent::is_plan_artifact_write(tools::UNIFIED_FILE, &args), "{args}");
    }
    assert!(tool_intent::is_plan_artifact_write(tools::WRITE_FILE, &json!({"path":".vtcode/plans/draft.md"})));
    assert!(tool_intent::is_docs_only_write(tools::WRITE_FILE, &json!({"path":"notes.txt"})));
}

#[cfg(unix)]
#[tokio::test]
async fn completion_verification_uses_effective_targets_for_prose_and_plan_symlinks() {
    for alias in [".vtcode/plans/alias.rs", "notes.txt"] {
        let dir = tempfile::tempdir().unwrap();
        let registry = ToolRegistry::new(dir.path().to_path_buf()).await;
        registry.allow_all_tools().await.unwrap();
        let code = dir.path().join("main.rs");
        std::fs::write(&code, "original\n").unwrap();
        let link = dir.path().join(alias);
        std::fs::create_dir_all(link.parent().unwrap()).unwrap();
        std::os::unix::fs::symlink(&code, &link).unwrap();
        let check = registry
            .execute_harness_command_session(json!({"action":"run", "command":"rustc --version"}))
            .await
            .unwrap();
        assert_eq!(check["exit_code"], 0, "{check}");
        let output = registry
            .execute_tool(tools::WRITE_FILE, json!({"path":alias, "content":"changed\n", "overwrite":true}))
            .await
            .unwrap();
        assert!(output.get("error").is_none(), "{output}");
        assert_eq!(std::fs::read_to_string(&code).unwrap(), "changed\n");
        assert!(registry.completion_verification_pending(), "effective code target via {alias}: {output}");
    }
}

#[tokio::test]
async fn completion_verification_exempt_file_results_preserve_existing_debt() {
    for debt in [false, true] {
        let dir = tempfile::tempdir().unwrap();
        let registry = ToolRegistry::new(dir.path().to_path_buf()).await;
        registry.allow_all_tools().await.unwrap();
        if debt {
            registry.restore_completion_verification_debt();
        }
        let patch = registry
            .execute_tool(
                tools::APPLY_PATCH,
                json!({
                    "input":"*** Begin Patch\n*** Add File: README.md\n+Documentation\n*** End Patch"
                }),
            )
            .await
            .unwrap();
        assert!(patch.get("error").is_none(), "{patch}");
        assert_eq!(registry.completion_verification_pending(), debt, "{patch}");
        for path in ["notes.txt", ".vtcode/plans/draft.rs"] {
            let output = registry
                .execute_tool(tools::WRITE_FILE, json!({"path":path, "content":"prose\n"}))
                .await
                .unwrap();
            assert!(output.get("error").is_none(), "{output}");
            assert_eq!(registry.completion_verification_pending(), debt, "{output}");
        }
    }
}

#[tokio::test]
async fn completion_verification_required_pipelines_match_executed_evidence() {
    for command in ["rustc --version 2>&1 | tail -5", "rustc --version 2>&1 | grep rustc"] {
        let dir = tempfile::tempdir().unwrap();
        let registry = ToolRegistry::new(dir.path().to_path_buf()).await;
        registry.allow_all_tools().await.unwrap();
        registry.restore_completion_verification_debt();
        let tracker = registry
            .execute_tool(
                tools::TASK_TRACKER,
                json!({
                    "action":"create", "title":"Verification", "items":[{
                        "description":"Check toolchain", "status":"completed", "verify":[command]
                    }]
                }),
            )
            .await
            .unwrap();
        assert!(tracker.get("error").is_none(), "{tracker}");
        assert!(registry.tracker_adopted_for_request());
        assert_eq!(registry.next_required_completion_verifier().as_deref(), Some(command));
        let result = registry
            .execute_tool(tools::EXEC_COMMAND, json!({"cmd":command}))
            .await
            .unwrap();
        assert_eq!(result["exit_code"], 0, "{result}");
        assert!(registry.completion_verifier_passed(command));
        assert!(registry.missing_required_completion_verifiers().is_empty());
        assert!(registry.next_required_completion_verifier().is_none());
        assert!(!registry.completion_verification_pending());
    }
}

#[tokio::test]
async fn completion_verification_pipeline_wait_identity_and_failure_stay_consistent() {
    let dir = tempfile::tempdir().unwrap();
    let registry = ToolRegistry::new(dir.path().to_path_buf()).await;
    registry.restore_completion_verification_debt();
    let command = "cargo check 2>&1 | tail -5";
    registry.set_required_completion_verifiers(vec![command.into()]);
    let args = json!({"action":"run", "command":command});
    let launch = registry.begin_completion_verifier(&args);
    registry.observe_completion_verifier(launch, &args, &Ok(json!({"session_id":"pipeline"})));
    assert_eq!(registry.pending_completion_verifier_session(command).as_deref(), Some("pipeline"));
    registry.observe_completion_verifier(
        None,
        &json!({"action":"wait", "session_id":"pipeline"}),
        &Ok(json!({"exit_code":7})),
    );
    assert!(!registry.completion_verifier_passed(command));
    assert!(registry.completion_verification_pending());
}

#[cfg(unix)]
#[tokio::test]
async fn completion_verification_declared_custom_checks_do_not_invalidate_their_verdicts() {
    use std::os::unix::fs::PermissionsExt;
    for exit_code in [0, 7] {
        let dir = tempfile::tempdir().unwrap();
        let script = dir.path().join("verify.sh");
        std::fs::write(&script, format!("#!/bin/sh\nprintf 'custom-check-evidence\\n'\nexit {exit_code}\n")).unwrap();
        std::fs::set_permissions(&script, std::fs::Permissions::from_mode(0o700)).unwrap();
        let registry = ToolRegistry::new(dir.path().to_path_buf()).await;
        registry.allow_all_tools().await.unwrap();
        let mut commands = registry.commands_config();
        commands.allow_list.push("./verify.sh".into());
        registry.apply_commands_config(&commands);
        registry.restore_completion_verification_debt();
        let tracker = registry
            .execute_tool(
                tools::TASK_TRACKER,
                json!({
                    "action":"create", "title":"Custom verification", "items":[{
                        "description":"Check custom invariant", "status":"completed", "verify":["./verify.sh"]
                    }]
                }),
            )
            .await
            .unwrap();
        assert!(tracker.get("error").is_none(), "{tracker}");
        let generation = registry.harness_context.completion_verification.lock().generation;
        let result = registry
            .execute_tool(tools::EXEC_COMMAND, json!({"cmd":"./verify.sh"}))
            .await
            .unwrap();
        assert_eq!(result["exit_code"], exit_code, "{result}");
        assert_eq!(registry.harness_context.completion_verification.lock().generation, generation);
        assert_eq!(registry.completion_verifier_passed("./verify.sh"), exit_code == 0);
        assert_eq!(registry.completion_verification_pending(), exit_code != 0);
        if exit_code == 0 {
            commands.deny_list.push("./verify.sh".into());
            registry.apply_commands_config(&commands);
            registry.observe_completion_mutation(
                tools::WRITE_FILE,
                &json!({"path":"main.rs"}),
                &json!({"success":true}),
            );
            let denied = registry
                .execute_tool(tools::EXEC_COMMAND, json!({"cmd":"./verify.sh"}))
                .await
                .unwrap();
            assert_eq!(denied["error"]["error_type"], "PolicyViolation", "{denied}");
            assert!(denied.get("exit_code").is_none());
            assert!(registry.completion_verification_pending());
            assert!(!registry.completion_verifier_passed("./verify.sh"));
        }
    }
}

#[cfg(unix)]
#[tokio::test]
async fn completion_verification_undeclared_custom_commands_still_create_mutation_debt() {
    use std::os::unix::fs::PermissionsExt;
    let dir = tempfile::tempdir().unwrap();
    let script = dir.path().join("verify.sh");
    std::fs::write(&script, "#!/bin/sh\nprintf done\n").unwrap();
    std::fs::set_permissions(&script, std::fs::Permissions::from_mode(0o700)).unwrap();
    let registry = ToolRegistry::new(dir.path().to_path_buf()).await;
    registry.allow_all_tools().await.unwrap();
    let mut commands = registry.commands_config();
    commands.allow_list.push("./verify.sh".into());
    registry.apply_commands_config(&commands);
    let result = registry
        .execute_tool(tools::EXEC_COMMAND, json!({"cmd":"./verify.sh"}))
        .await
        .unwrap();
    assert_eq!(result["exit_code"], 0, "{result}");
    assert!(registry.completion_verification_pending());
    assert!(!registry.completion_verifier_passed("./verify.sh"));
}

#[tokio::test]
async fn completion_verification_accepts_empty_adapter_fields_but_rejects_effective_context() {
    let dir = tempfile::tempdir().unwrap();
    let registry = ToolRegistry::new(dir.path().to_path_buf()).await;
    for env in [json!([]), json!({})] {
        let args = json!({"action":"run", "command":"cargo check", "args":[], "env":env});
        assert!(registry.begin_completion_verifier(&args).is_some(), "{args}");
    }
    for (field, value) in [
        ("args", json!(["--locked"])),
        ("args", Value::Null),
        ("args", json!("")),
        ("env", json!([{"name":"MODE", "value":"fixture"}])),
        ("env", json!({"MODE":"fixture"})),
        ("env", Value::Null),
        ("shell", json!("/bin/sh")),
        ("login", json!(false)),
        ("stdin", json!("")),
    ] {
        let mut args = json!({"action":"run", "command":"cargo check", "args":[], "env":[]});
        args[field] = value;
        assert!(registry.begin_completion_verifier(&args).is_none(), "context must not be dropped: {args}");
    }
}

#[cfg(unix)]
#[tokio::test]
async fn completion_verification_native_terminal_adapter_observes_yielded_verdicts() {
    use std::os::unix::fs::PermissionsExt;
    for exit_code in [0, 7] {
        let dir = tempfile::tempdir().unwrap();
        let script = dir.path().join("rustc");
        std::fs::write(&script, format!("#!/bin/sh\nwhile [ ! -e release-check ]; do sleep 0.01; done\nprintf 'native-verified\\n'\nexit {exit_code}\n")).unwrap();
        std::fs::set_permissions(&script, std::fs::Permissions::from_mode(0o700)).unwrap();
        let registry = ToolRegistry::new(dir.path().to_path_buf()).await;
        let mut commands = registry.commands_config();
        commands.allow_list.push("./rustc".into());
        registry.apply_commands_config(&commands);
        registry.restore_completion_verification_debt();
        // Match Copilot terminal_run_args, including its empty adapter defaults.
        let result = registry
            .execute_harness_command_session_terminal_run(json!({
                "action":"run", "command":"./rustc --version", "args":[], "cwd":null,
                "tty":true, "yield_time_ms":100, "env":[],
            }))
            .await
            .unwrap();
        let session_id = result["session_id"].as_str().expect("retained native session");
        assert!(result["exit_code"].is_null(), "the release file prevents early completion: {result}");
        assert!(registry.completion_verification_pending());
        assert_eq!(registry.pending_completion_verifier_session("./rustc --version").as_deref(), Some(session_id));
        std::fs::write(dir.path().join("release-check"), "release").unwrap();
        let observed = tokio::time::timeout(std::time::Duration::from_secs(5), async {
            loop {
                if let Some(code) = registry.harness_exec_session_completed(session_id).await.unwrap() {
                    break code;
                }
                tokio::time::sleep(std::time::Duration::from_millis(10)).await;
            }
        })
        .await;
        registry.close_harness_exec_session(session_id).await.unwrap();
        assert_eq!(observed.unwrap(), exit_code);
        assert_eq!(registry.completion_verifier_passed("./rustc --version"), exit_code == 0);
        assert_eq!(registry.completion_verification_pending(), exit_code != 0);
    }
}

#[tokio::test]
async fn completion_verification_bulk_termination_releases_mutators_without_erasing_debt() {
    let dir = tempfile::tempdir().unwrap();
    let registry = ToolRegistry::new(dir.path().to_path_buf()).await;
    for session in ["formatter-a", "formatter-b"] {
        registry.observe_completion_mutation(
            tools::EXEC_COMMAND,
            &json!({"cmd":"cargo fmt"}),
            &json!({"session_id":session}),
        );
    }
    assert!(registry.completion_verification_pending());
    // Terminating an unrelated session changes nothing.
    registry.observe_completion_exec_sessions_terminated(&["unrelated".to_owned()]);
    assert!(registry.completion_verification_pending());
    // Releasing one mutator leaves the other pending.
    registry.observe_completion_exec_sessions_terminated(&["formatter-a".to_owned()]);
    assert!(registry.completion_verification_pending());
    // Releasing the last mutator keeps code debt but unblocks fresh verification.
    registry.observe_completion_exec_sessions_terminated(&["formatter-b".to_owned()]);
    assert!(
        registry.completion_verification_pending(),
        "terminated mutators release the session block, not the code debt"
    );
    let args = json!({"action":"run", "command":"cargo check"});
    let launch = registry.begin_completion_verifier(&args);
    assert!(launch.is_some());
    registry.observe_completion_verifier(launch, &args, &Ok(json!({"exit_code":0})));
    assert!(
        !registry.completion_verification_pending(),
        "a fresh check after bulk termination must satisfy the gate"
    );
}

#[cfg(unix)]
#[tokio::test]
async fn required_inspections_bypass_primed_results_and_record_actual_verdicts() {
    for (command, expected_exit) in [
        ("rg -n original fixture.txt", 1),
        ("grep -n original fixture.txt", 1),
        ("grep -n '---\\s\\+\\|' fixture.txt", 2),
    ] {
        let dir = tempfile::tempdir().unwrap();
        std::fs::write(dir.path().join("fixture.txt"), "original\n").unwrap();
        let registry = ToolRegistry::new(dir.path().to_path_buf()).await;
        registry.allow_all_tools().await.unwrap();
        let mut commands = registry.commands_config();
        commands.allow_list.push("rg".into());
        registry.apply_commands_config(&commands);
        let args = json!({"cmd":command, "yield_time_ms":10000});
        let first = registry.execute_tool(tools::EXEC_COMMAND, args.clone()).await.unwrap();
        assert_eq!(first["exit_code"], if expected_exit == 2 { 2 } else { 0 }, "{command}: {first}");
        std::fs::write(dir.path().join("fixture.txt"), "changed\n").unwrap();
        registry.restore_completion_verification_debt();
        registry.set_required_completion_verifiers(vec![command.into()]);
        let result = registry.execute_tool(tools::EXEC_COMMAND, args).await.unwrap();
        assert_eq!(result["exit_code"], expected_exit, "{command}: {result}");
        assert_ne!(result["reused_recent_result"], true);
        assert!(!registry.completion_verifier_passed(command));
        assert!(registry.completion_verification_pending());
    }
}

#[tokio::test]
async fn inspection_accounting_preserves_generation_but_concrete_changes_invalidate_passes() {
    let dir = tempfile::tempdir().unwrap();
    let registry = ToolRegistry::new(dir.path().to_path_buf()).await;
    registry.restore_completion_verification_debt();
    registry.set_required_completion_verifiers(vec!["cargo check".into()]);
    let args = json!({"cmd":"cargo check"});
    let id = registry.begin_completion_verifier(&args);
    registry.observe_completion_verifier(id, &args, &Ok(json!({"exit_code":0})));
    for command in [
        "for p in $(grep -o 'x' README.md | sort -u); do test -f \"$p\" || echo \"missing: $p\"; done",
        "grep x README.md; echo \"exit:$?\"",
        "rg -n \"MD013|line_length\" docs/development/*.md | head",
    ] {
        registry.observe_completion_mutation(tools::EXEC_COMMAND, &json!({"cmd":command}), &json!({"exit_code":0}));
        assert!(registry.completion_verifier_passed("cargo check"), "{command}");
    }
    registry.observe_completion_mutation(
        tools::EXEC_COMMAND,
        &json!({"cmd":"grep x README.md"}),
        &json!({"exit_code":0, "modified_files":["src/lib.rs"]}),
    );
    assert!(!registry.completion_verifier_passed("cargo check"));
}

#[cfg(unix)]
#[tokio::test]
async fn required_inspection_repetition_limit_rejects_instead_of_replaying_a_verdict() {
    let dir = tempfile::tempdir().unwrap();
    std::fs::write(dir.path().join("fixture.txt"), "original\n").unwrap();
    let registry = ToolRegistry::new(dir.path().to_path_buf()).await;
    registry.allow_all_tools().await.unwrap();
    registry.execution_history.set_loop_detection_limits(5, 2);
    let command = "grep -n original fixture.txt";
    let args = json!({"cmd":command});
    for _ in 0..2 {
        let result = registry.execute_tool(tools::EXEC_COMMAND, args.clone()).await.unwrap();
        assert_eq!(result["exit_code"], 0);
    }
    registry.restore_completion_verification_debt();
    registry.set_required_completion_verifiers(vec![command.into()]);
    let result = registry.execute_tool(tools::EXEC_COMMAND, args).await.unwrap();
    assert_eq!(result["loop_detected"], true, "{result}");
    assert!(result["error"]["message"].as_str().unwrap().contains("Loop detection"), "{result}");
    assert!(result.get("exit_code").is_none());
    assert!(!registry.completion_verifier_passed(command));
    assert!(registry.completion_verification_pending());
}

#[tokio::test]
async fn docs_session_path_glob_does_not_activate_a_malformed_tracker_verifier() {
    let dir = tempfile::tempdir().unwrap();
    let registry = ToolRegistry::new(dir.path().to_path_buf()).await;
    registry.set_required_completion_verifiers(vec![
        "rg -n \"brew trust\" README.md && ! rg -n \"brew tap\" README.md".into(),
    ]);
    registry.observe_completion_mutation(
        tools::APPLY_PATCH,
        &json!({"input":"*** Begin Patch\n*** Update File: README.md\n@@\n-old\n+new\n*** End Patch"}),
        &json!({"success":true,"modified_files":["README.md"]}),
    );
    registry.observe_completion_mutation(
        tools::EXEC_COMMAND,
        &json!({"cmd":"rg -n \"MD013|line_length\" docs/development/*.md | head"}),
        &json!({"exit_code":0}),
    );
    assert_eq!(registry.harness_context.completion_verification.lock().generation, 0);
    assert!(!registry.completion_verification_pending());
    assert_eq!(
        registry.next_required_completion_verifier().as_deref(),
        Some("rg -n \"brew trust\" README.md && ! rg -n \"brew tap\" README.md")
    );
}

#[tokio::test]
async fn declared_source_repair_cannot_clear_completion_verification() {
    let dir = tempfile::tempdir().unwrap();
    let registry = ToolRegistry::new(dir.path().to_path_buf()).await;
    for command in [
        "cargo clippy --fix",
        "ruff check --fix-only .",
        "uv run --locked ruff check --fix .",
        "echo before && cargo clippy --fix",
    ] {
        registry.restore_completion_verification_debt();
        registry.set_required_completion_verifiers(vec![command.into()]);
        let args = json!({"cmd":command});
        let id = registry.begin_completion_verifier(&args);
        assert!(id.is_none(), "repair cannot launch a verification attempt: {command}");
        let generation = registry.harness_context.completion_verification.lock().generation;
        registry.observe_completion_mutation(tools::EXEC_COMMAND, &args, &json!({"exit_code":0}));
        assert_eq!(registry.harness_context.completion_verification.lock().generation, generation + 1, "{command}");
        registry.observe_completion_verifier(id, &args, &Ok(json!({"exit_code":0})));
        assert!(registry.completion_verification_pending(), "{command}");
        assert_eq!(
            registry.next_required_completion_verifier().as_deref(),
            Some(command),
            "requirements must not be silently replaced"
        );
    }
    registry.set_required_completion_verifiers(vec!["cargo clippy --locked".into()]);
    let args = json!({"cmd":"cargo clippy --locked"});
    let id = registry.begin_completion_verifier(&args);
    assert!(id.is_some());
    registry.observe_completion_verifier(id, &args, &Ok(json!({"exit_code":0})));
    assert!(!registry.completion_verification_pending());
}