aristo-cli 0.1.0

Aristo CLI binary (the `aristo` command).
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
//! `aristo verify` (slice 22) — imperative tests for dispatcher
//! semantics that trycmd's `[..]`-wildcarded scenario can't pin down.
//!
//! Slice 22 ships the dispatcher + `verify = false` no-op arm; the
//! other verify methods return `NotImplemented` with their target
//! slice pointer.

use assert_cmd::Command;
use predicates::str::contains;
use std::fs;
use std::path::Path;

fn aristo_in(dir: &Path) -> Command {
    let mut cmd = Command::cargo_bin("aristo").unwrap();
    cmd.current_dir(dir);
    cmd
}

/// Hand-craft an index with one intent at the given verify level.
/// Tests use this instead of going through stamp so the workspace
/// shape is predictable and doesn't depend on the walker.
fn workspace_with_one_intent_at(dir: &Path, verify_line: &str) {
    aristo_in(dir).arg("init").assert().success();
    let zero_hash = format!("sha256:{}", "0".repeat(64));
    let index = format!(
        "[__meta__]\nschema_version = 1\n\n\
         [my_intent]\nkind = \"intent\"\ntext = \"the property\"\n\
         {verify_line}\nstatus = \"unknown\"\n\
         text_hash = \"{zero_hash}\"\nbody_hash = \"{zero_hash}\"\n\
         file = \"src/x.rs\"\nsite = \"fn x (line 1)\"\n\
         covered_region = \"function\"\n",
    );
    fs::write(dir.join(".aristo/index.toml"), index).unwrap();
}

#[test]
fn errors_outside_a_workspace() {
    let tmp = tempfile::tempdir().unwrap();
    aristo_in(tmp.path())
        .arg("verify")
        .assert()
        .failure()
        .code(2)
        .stderr(contains("not inside an Aristo workspace"));
}

#[test]
fn empty_workspace_reports_zero_verified_zero_skipped() {
    let tmp = tempfile::tempdir().unwrap();
    aristo_in(tmp.path()).arg("init").assert().success();

    aristo_in(tmp.path())
        .arg("verify")
        .assert()
        .success()
        .stdout(contains(
            "ok: 0 annotations verified, 0 skipped (documentation-only).",
        ));
}

#[test]
fn verify_false_intent_is_counted_as_skipped_documentation_only() {
    let tmp = tempfile::tempdir().unwrap();
    workspace_with_one_intent_at(tmp.path(), "verify = false");

    aristo_in(tmp.path())
        .arg("verify")
        .assert()
        .success()
        .stdout(contains(
            "ok: 0 annotations verified, 1 skipped (documentation-only).",
        ));
}

#[test]
fn verify_neural_intent_writes_pending_request_file() {
    let tmp = tempfile::tempdir().unwrap();
    workspace_with_one_intent_at(tmp.path(), "verify = \"neural\"");

    aristo_in(tmp.path())
        .arg("verify")
        .assert()
        .success()
        .stdout(contains("1 entry pending neural verification"))
        .stdout(contains("/aristo-neural-verify"));

    let task_path = tmp
        .path()
        .join(".aristo/verify-queue/pending/my_intent.toml");
    let task = fs::read_to_string(&task_path)
        .unwrap_or_else(|e| panic!("expected queue task at {:?}: {e}", task_path));
    assert!(
        task.contains("id = \"my_intent\""),
        "queue task must list the id; got:\n{task}"
    );
    assert!(
        task.contains("text_hash"),
        "queue task must include text_hash for the subagent to copy"
    );
}

#[test]
fn verify_test_intent_returns_not_implemented_pointing_to_deferred_design() {
    let tmp = tempfile::tempdir().unwrap();
    workspace_with_one_intent_at(tmp.path(), "verify = \"test\"");

    aristo_in(tmp.path())
        .arg("verify")
        .assert()
        .failure()
        .code(64)
        .stderr(contains("is not implemented yet"))
        .stderr(contains("post-MVP"))
        .stderr(contains("verify-test-design.md"));
}

#[test]
fn verify_full_intent_returns_not_implemented_pointing_to_deferred_design() {
    let tmp = tempfile::tempdir().unwrap();
    workspace_with_one_intent_at(tmp.path(), "verify = \"full\"");

    aristo_in(tmp.path())
        .arg("verify")
        .assert()
        .failure()
        .code(64)
        .stderr(contains("is not implemented yet"))
        .stderr(contains("post-MVP"));
}

#[test]
fn filter_id_narrows_to_one_entry() {
    let tmp = tempfile::tempdir().unwrap();
    workspace_with_one_intent_at(tmp.path(), "verify = false");

    // Filter MISSES the only entry — should report zero skipped.
    aristo_in(tmp.path())
        .args(["verify", "--filter", "id=nonexistent"])
        .assert()
        .success()
        .stdout(contains(
            "ok: 0 annotations verified, 0 skipped (documentation-only).",
        ));

    // Filter HITS the entry — should count it.
    aristo_in(tmp.path())
        .args(["verify", "--filter", "id=my_intent"])
        .assert()
        .success()
        .stdout(contains(
            "ok: 0 annotations verified, 1 skipped (documentation-only).",
        ));
}

#[test]
fn unknown_filter_key_exits_2() {
    let tmp = tempfile::tempdir().unwrap();
    workspace_with_one_intent_at(tmp.path(), "verify = false");

    aristo_in(tmp.path())
        .args(["verify", "--filter", "kind=intent"])
        .assert()
        .failure()
        .code(2)
        .stderr(contains("unknown filter key"));
}

#[test]
fn rerun_does_not_reverify_verify_false_entries() {
    // verify=false entries are always skipped (documentation-only),
    // even under --rerun. --rerun forces re-processing of clean
    // verified entries (Status::Verified|Tested|Neural), not of
    // intentional opt-outs.
    let tmp = tempfile::tempdir().unwrap();
    workspace_with_one_intent_at(tmp.path(), "verify = false");

    aristo_in(tmp.path())
        .args(["verify", "--rerun"])
        .assert()
        .success()
        .stdout(contains(
            "ok: 0 annotations verified, 1 skipped (documentation-only).",
        ));
}

#[test]
fn assume_entries_are_treated_as_documentation_only() {
    // Assume entries have no `verify` field by design — they describe
    // external trust. The dispatcher resolves them to Bool(false) so
    // they take the same docs-only skip path as opt-out intents.
    let tmp = tempfile::tempdir().unwrap();
    aristo_in(tmp.path()).arg("init").assert().success();
    let zero_hash = format!("sha256:{}", "0".repeat(64));
    let index = format!(
        "[__meta__]\nschema_version = 1\n\n\
         [my_assume]\nkind = \"assume\"\ntext = \"OS guarantees X\"\n\
         status = \"unknown\"\n\
         text_hash = \"{zero_hash}\"\nbody_hash = \"{zero_hash}\"\n\
         file = \"src/x.rs\"\nsite = \"fn x (line 1)\"\n\
         covered_region = \"function\"\n",
    );
    fs::write(tmp.path().join(".aristo/index.toml"), index).unwrap();

    aristo_in(tmp.path())
        .arg("verify")
        .assert()
        .success()
        .stdout(contains(
            "ok: 0 annotations verified, 1 skipped (documentation-only).",
        ));
}

// ─── --apply-verdicts (slice 23) ────────────────────────────────────────

fn write_proof(dir: &Path, id_filename_stem: &str, contents: &str) {
    let proofs = dir.join(".aristo/proofs");
    fs::create_dir_all(&proofs).unwrap();
    fs::write(proofs.join(format!("{id_filename_stem}.proof")), contents).unwrap();
}

/// Build a workspace + index with one verify=neural intent.
/// Returns the text_hash string the validator will check against.
fn workspace_with_one_neural_intent(dir: &Path, id: &str, text: &str) -> String {
    aristo_in(dir).arg("init").assert().success();
    let zero_hash = format!("sha256:{}", "0".repeat(64));
    let text_h = aristo_core::hash::text_hash(text).to_string();
    let index = format!(
        "[__meta__]\nschema_version = 1\n\n\
         [{id}]\nkind = \"intent\"\ntext = \"{text}\"\nverify = \"neural\"\nstatus = \"unknown\"\n\
         text_hash = \"{text_h}\"\nbody_hash = \"{zero_hash}\"\n\
         file = \"src/x.rs\"\nsite = \"fn x (line 1)\"\n\
         covered_region = \"function\"\n",
    );
    fs::write(dir.join(".aristo/index.toml"), index).unwrap();
    text_h
}

#[test]
fn apply_verdicts_with_no_proofs_dir_is_no_op() {
    let tmp = tempfile::tempdir().unwrap();
    aristo_in(tmp.path()).arg("init").assert().success();

    aristo_in(tmp.path())
        .args(["verify", "--apply-verdicts"])
        .assert()
        .success()
        .stdout(contains("no pending verdict files"));
}

#[test]
fn apply_verdicts_accepts_valid_verified_proof_and_flips_status() {
    let tmp = tempfile::tempdir().unwrap();
    let text_h = workspace_with_one_neural_intent(tmp.path(), "my_intent", "the property holds");
    let zero_hash = format!("sha256:{}", "0".repeat(64));
    let proof = format!(
        r#"[verdict]
type = "verified"
method = "neural"
produced_at_text_hash = "{text_h}"
produced_at_body_hash = "{zero_hash}"
produced_by = "test@0"
attempts = 1
property_kind = "invariant"

[verified.proof]
conclusion = "the property holds"

[[verified.proof.steps]]
path = "0"
claim = "trivially"
relation_to_parent = "decomposes"
grounds = [{{ kind = "composition", reason = "single-step proof" }}]
"#
    );
    write_proof(tmp.path(), "my_intent", &proof);

    aristo_in(tmp.path())
        .args(["verify", "--apply-verdicts"])
        .assert()
        .success()
        .stdout(contains("applied: 1/1 verdict(s)"));

    aristo_in(tmp.path())
        .args(["show", "my_intent"])
        .assert()
        .success()
        .stdout(contains("status:"))
        .stdout(contains("neural"));
}

#[test]
fn apply_verdicts_rejects_stale_text_hash() {
    let tmp = tempfile::tempdir().unwrap();
    let _text_h = workspace_with_one_neural_intent(tmp.path(), "my_intent", "the property holds");
    let zero_hash = format!("sha256:{}", "0".repeat(64));
    let stale = format!("sha256:{}", "f".repeat(64));
    let proof = format!(
        r#"[verdict]
type = "verified"
method = "neural"
produced_at_text_hash = "{stale}"
produced_at_body_hash = "{zero_hash}"
produced_by = "test@0"
attempts = 1
property_kind = "invariant"

[verified.proof]
conclusion = "x"

[[verified.proof.steps]]
path = "0"
claim = "trivially"
relation_to_parent = "decomposes"
grounds = [{{ kind = "composition", reason = "trivial" }}]
"#
    );
    write_proof(tmp.path(), "my_intent", &proof);

    aristo_in(tmp.path())
        .args(["verify", "--apply-verdicts"])
        .assert()
        .failure()
        .code(1)
        .stderr(contains("text drifted since verification"));
}

#[test]
fn apply_verdicts_rejects_attempts_over_budget() {
    let tmp = tempfile::tempdir().unwrap();
    let text_h = workspace_with_one_neural_intent(tmp.path(), "my_intent", "the property holds");
    let zero_hash = format!("sha256:{}", "0".repeat(64));
    let proof = format!(
        r#"[verdict]
type = "verified"
method = "neural"
produced_at_text_hash = "{text_h}"
produced_at_body_hash = "{zero_hash}"
produced_by = "test@0"
attempts = 99
property_kind = "invariant"

[verified.proof]
conclusion = "x"

[[verified.proof.steps]]
path = "0"
claim = "trivially"
relation_to_parent = "decomposes"
grounds = [{{ kind = "composition", reason = "trivial" }}]
"#
    );
    write_proof(tmp.path(), "my_intent", &proof);

    aristo_in(tmp.path())
        .args(["verify", "--apply-verdicts"])
        .assert()
        .failure()
        .code(1)
        .stderr(contains("exceeds budget"));
}

#[test]
fn apply_verdicts_counterexample_flips_to_counterexample_status() {
    let tmp = tempfile::tempdir().unwrap();
    let text_h = workspace_with_one_neural_intent(tmp.path(), "my_intent", "the property holds");
    let zero_hash = format!("sha256:{}", "0".repeat(64));
    let proof = format!(
        r#"[verdict]
type = "counterexample"
method = "neural"
produced_at_text_hash = "{text_h}"
produced_at_body_hash = "{zero_hash}"
produced_by = "test@0"
attempts = 1
property_kind = "invariant"

[counterexample.violation]
description = "the property fails when input is empty"
violated_step_path = "0"

[[counterexample.violation.trigger_steps]]
path = "0"
claim = "empty input bypasses the check"
relation_to_parent = "decomposes"
grounds = [{{ kind = "composition", reason = "by inspection of source" }}]
"#
    );
    write_proof(tmp.path(), "my_intent", &proof);

    aristo_in(tmp.path())
        .args(["verify", "--apply-verdicts"])
        .assert()
        .success()
        .stdout(contains("applied: 1/1 verdict(s)"));

    aristo_in(tmp.path())
        .args(["show", "my_intent"])
        .assert()
        .success()
        .stdout(contains("counterexample"));
}

#[test]
fn apply_verdicts_rejects_unparseable_proof() {
    let tmp = tempfile::tempdir().unwrap();
    workspace_with_one_neural_intent(tmp.path(), "my_intent", "the property");
    write_proof(tmp.path(), "my_intent", "this is not valid TOML at all {");

    aristo_in(tmp.path())
        .args(["verify", "--apply-verdicts"])
        .assert()
        .failure()
        .code(1)
        .stderr(contains("parse:"));
}

#[test]
fn apply_verdicts_stamps_code_hash_when_agent_omitted_it() {
    // Agent-written proof with a Code ground that omits code_text_hash.
    // The validator accepts (existence + line-range checks pass), then
    // apply stamps the computed hash into the saved proof file.
    let tmp = tempfile::tempdir().unwrap();
    let text_h = workspace_with_one_neural_intent(tmp.path(), "my_intent", "the property holds");
    let zero_hash = format!("sha256:{}", "0".repeat(64));
    fs::create_dir_all(tmp.path().join("src")).unwrap();
    fs::write(tmp.path().join("src/x.rs"), "line1\nline2\nline3\n").unwrap();
    let proof = format!(
        r#"[verdict]
type = "verified"
method = "neural"
produced_at_text_hash = "{text_h}"
produced_at_body_hash = "{zero_hash}"
produced_by = "test@0"
attempts = 1
property_kind = "invariant"

[verified.proof]
conclusion = "the property holds"

[[verified.proof.steps]]
path = "0"
claim = "by inspection"
relation_to_parent = "decomposes"
grounds = [{{ kind = "code", file = "src/x.rs", lines = "1-2", reason = "see literal" }}]
"#
    );
    write_proof(tmp.path(), "my_intent", &proof);

    aristo_in(tmp.path())
        .args(["verify", "--apply-verdicts"])
        .assert()
        .success()
        .stdout(contains("applied: 1/1 verdict(s)"));

    let written = fs::read_to_string(tmp.path().join(".aristo/proofs/my_intent.proof")).unwrap();
    assert!(
        written.contains("code_text_hash = \"sha256:"),
        "expected stamped code_text_hash, got:\n{written}"
    );
}

#[test]
fn apply_verdicts_rewrite_hashes_migrates_proof_with_fabricated_hash() {
    // Simulates a pre-migration proof file: the agent wrote a wrong
    // (fabricated) code_text_hash. Without --rewrite-hashes the
    // validator rejects on staleness. With it, the stamped hash is
    // cleared, validated, and re-stamped.
    let tmp = tempfile::tempdir().unwrap();
    let text_h = workspace_with_one_neural_intent(tmp.path(), "my_intent", "the property holds");
    let zero_hash = format!("sha256:{}", "0".repeat(64));
    let fake_hash = format!("sha256:{}", "a".repeat(64));
    fs::create_dir_all(tmp.path().join("src")).unwrap();
    fs::write(tmp.path().join("src/x.rs"), "line1\nline2\nline3\n").unwrap();
    let proof = format!(
        r#"[verdict]
type = "verified"
method = "neural"
produced_at_text_hash = "{text_h}"
produced_at_body_hash = "{zero_hash}"
produced_by = "test@0"
attempts = 1
property_kind = "invariant"

[verified.proof]
conclusion = "the property holds"

[[verified.proof.steps]]
path = "0"
claim = "by inspection"
relation_to_parent = "decomposes"
grounds = [{{ kind = "code", file = "src/x.rs", lines = "1-2", code_text_hash = "{fake_hash}", reason = "see literal" }}]
"#
    );
    write_proof(tmp.path(), "my_intent", &proof);

    // Strict default: reject on stale stamped hash.
    aristo_in(tmp.path())
        .args(["verify", "--apply-verdicts"])
        .assert()
        .failure()
        .code(1)
        .stderr(contains("code drifted since verification"));

    // Migration flag: clear + recompute + accept.
    aristo_in(tmp.path())
        .args(["verify", "--apply-verdicts", "--rewrite-hashes"])
        .assert()
        .success()
        .stdout(contains("applied: 1/1 verdict(s)"));

    let written = fs::read_to_string(tmp.path().join(".aristo/proofs/my_intent.proof")).unwrap();
    assert!(
        !written.contains(&fake_hash),
        "fabricated hash must be replaced post-migration; got:\n{written}"
    );
    assert!(
        written.contains("code_text_hash = \"sha256:"),
        "expected a stamped (real) code_text_hash post-migration; got:\n{written}"
    );
}

// ─── validator-at-list-time (skip logic consults validator) ────────────

#[test]
fn entry_with_valid_proof_is_skipped_on_subsequent_verify() {
    // After --apply-verdicts flips status to Neural AND stamps hashes
    // into the proof, the next `aristo verify` should consider this
    // entry done and not re-add it to pending.
    let tmp = tempfile::tempdir().unwrap();
    let text_h = workspace_with_one_neural_intent(tmp.path(), "my_intent", "the property holds");
    let zero_hash = format!("sha256:{}", "0".repeat(64));
    let proof = format!(
        r#"[verdict]
type = "verified"
method = "neural"
produced_at_text_hash = "{text_h}"
produced_at_body_hash = "{zero_hash}"
produced_by = "test@0"
attempts = 1
property_kind = "invariant"

[verified.proof]
conclusion = "the property holds"

[[verified.proof.steps]]
path = "0"
claim = "trivially"
relation_to_parent = "decomposes"
grounds = [{{ kind = "composition", reason = "single-step proof" }}]
"#
    );
    write_proof(tmp.path(), "my_intent", &proof);
    aristo_in(tmp.path())
        .args(["verify", "--apply-verdicts"])
        .assert()
        .success();

    // Next plain `verify` should skip — proof on disk + validator passes.
    aristo_in(tmp.path()).arg("verify").assert().success();

    assert!(
        !tmp.path()
            .join(".aristo/verify-queue/pending/my_intent.toml")
            .exists(),
        "no queue task should be enqueued when nothing's pending"
    );
}

#[test]
fn counterexample_status_with_passing_proof_is_skipped() {
    // GAP-2: Counterexample used to be excluded from the terminal-clean
    // set, so it re-ran every verify cycle. The new skip logic should
    // treat it as terminal AS LONG AS the proof on disk still validates.
    let tmp = tempfile::tempdir().unwrap();
    let text_h = workspace_with_one_neural_intent(tmp.path(), "my_intent", "the property holds");
    let zero_hash = format!("sha256:{}", "0".repeat(64));
    let proof = format!(
        r#"[verdict]
type = "counterexample"
method = "neural"
produced_at_text_hash = "{text_h}"
produced_at_body_hash = "{zero_hash}"
produced_by = "test@0"
attempts = 1
property_kind = "invariant"

[counterexample.violation]
description = "the property fails when input is empty"
violated_step_path = "0"

[[counterexample.violation.trigger_steps]]
path = "0"
claim = "empty input bypasses the check"
relation_to_parent = "decomposes"
grounds = [{{ kind = "composition", reason = "by inspection of source" }}]
"#
    );
    write_proof(tmp.path(), "my_intent", &proof);
    aristo_in(tmp.path())
        .args(["verify", "--apply-verdicts"])
        .assert()
        .success();

    // status is now Counterexample. Next verify should NOT re-add to pending.
    aristo_in(tmp.path()).arg("verify").assert().success();
    assert!(
        !tmp.path()
            .join(".aristo/verify-queue/pending/my_intent.toml")
            .exists(),
        "counterexample with valid proof must not be re-pended (GAP-2 fix)"
    );
}

#[test]
fn terminal_status_without_proof_file_forces_reverify() {
    // Inconsistency case: index says terminal but no proof exists on disk.
    // The skip logic conservatively forces re-verification.
    let tmp = tempfile::tempdir().unwrap();
    workspace_with_one_neural_intent(tmp.path(), "my_intent", "the property holds");
    // Manually flip status to Neural in the index without writing a proof.
    let idx_path = tmp.path().join(".aristo/index.toml");
    let idx = fs::read_to_string(&idx_path).unwrap();
    let idx = idx.replace("status = \"unknown\"", "status = \"neural\"");
    fs::write(&idx_path, idx).unwrap();

    aristo_in(tmp.path())
        .arg("verify")
        .assert()
        .success()
        .stdout(contains("1 entry pending neural verification"));
}

// ─── GAP-9: attempts persistence across re-spawns ─────────────────────

#[test]
fn pending_carries_prior_attempts_from_existing_proof() {
    // A rejected proof for an entry leaves attempts=2 (say) on disk.
    // Next `aristo verify` must include `prior_attempts = 2` so the
    // skill's next subagent writes attempts=3, not attempts=1.
    let tmp = tempfile::tempdir().unwrap();
    let text_h = workspace_with_one_neural_intent(tmp.path(), "my_intent", "the property");
    let zero_hash = format!("sha256:{}", "0".repeat(64));
    // Write a proof with attempts=2 that will validate at hash-check
    // (focal hashes correct) but never gets accepted because we want
    // it to coexist with the verify-list-time check.
    let proof = format!(
        r#"[verdict]
type = "inconclusive"
method = "neural"
produced_at_text_hash = "{text_h}"
produced_at_body_hash = "{zero_hash}"
produced_by = "test@0"
attempts = 2
property_kind = "invariant"

[inconclusive.gap]
description = "stuck"
unfilled_path = "0"
[[inconclusive.gap.suggested_annotations]]
kind = "assume"
suggested_text = "something not in the index"
at_site = "fn x (line 1)"
rationale = "would close gap"
"#
    );
    write_proof(tmp.path(), "my_intent", &proof);
    // Apply once to flip status to Inconclusive.
    aristo_in(tmp.path())
        .args(["verify", "--apply-verdicts"])
        .assert()
        .success();

    // Now corrupt the proof so the validator-at-list-time check
    // rejects it (forcing re-pending), but the prior attempts read
    // still works.
    let path = tmp.path().join(".aristo/proofs/my_intent.proof");
    let raw = fs::read_to_string(&path).unwrap();
    // Replace produced_at_text_hash with garbage → validator rejects on
    // focal-text-hash mismatch at list time.
    let stale = format!("sha256:{}", "f".repeat(64));
    let raw = raw.replace(&text_h, &stale);
    fs::write(&path, raw).unwrap();

    aristo_in(tmp.path()).arg("verify").assert().success();
    let task_path = tmp
        .path()
        .join(".aristo/verify-queue/pending/my_intent.toml");
    let task = fs::read_to_string(&task_path)
        .unwrap_or_else(|e| panic!("expected queue task at {:?}: {e}", task_path));
    assert!(
        task.contains("prior_attempts = 2"),
        "queue task must carry prior_attempts; got:\n{task}"
    );
}

#[test]
fn pending_skips_and_warns_when_budget_exhausted() {
    // attempts=3 on the existing proof = budget exhausted. Next verify
    // must NOT include this id in pending (no point re-spawning to hit
    // attempts=4 which the validator would reject anyway) AND must warn
    // the user loudly on stderr.
    let tmp = tempfile::tempdir().unwrap();
    let text_h = workspace_with_one_neural_intent(tmp.path(), "stuck_intent", "the property");
    let zero_hash = format!("sha256:{}", "0".repeat(64));
    let proof = format!(
        r#"[verdict]
type = "inconclusive"
method = "neural"
produced_at_text_hash = "{text_h}"
produced_at_body_hash = "{zero_hash}"
produced_by = "test@0"
attempts = 3
property_kind = "invariant"

[inconclusive.gap]
description = "stuck"
unfilled_path = "0"
[[inconclusive.gap.suggested_annotations]]
kind = "assume"
suggested_text = "something not in the index"
at_site = "fn x (line 1)"
rationale = "would close gap"
"#
    );
    write_proof(tmp.path(), "stuck_intent", &proof);
    aristo_in(tmp.path())
        .args(["verify", "--apply-verdicts"])
        .assert()
        .success();

    // Corrupt the produced_at_text_hash → validator at list time rejects
    // → entry would re-pend, except prior_attempts=3 is at budget cap so
    // the SDK excludes it and warns instead.
    let path = tmp.path().join(".aristo/proofs/stuck_intent.proof");
    let raw = fs::read_to_string(&path).unwrap();
    let stale = format!("sha256:{}", "f".repeat(64));
    let raw = raw.replace(&text_h, &stale);
    fs::write(&path, raw).unwrap();

    aristo_in(tmp.path())
        .arg("verify")
        .assert()
        .success()
        .stderr(contains("exhausted the repair budget"))
        .stderr(contains("stuck_intent"));

    // Queue must NOT have a task file for the budget-exhausted entry.
    let task_path = tmp
        .path()
        .join(".aristo/verify-queue/pending/stuck_intent.toml");
    assert!(
        !task_path.exists(),
        "budget-exhausted entry must not be enqueued"
    );
}