gemel 0.9.0

Evidence-native version control for agentic software development: canonical object encoding, content-addressed identity, immutable object store, change workflow, Git-carried exchange rollups, and CLI.
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
//! Phase 2 integration tests (SPECIFICATION.md §43, AGENT_PROTOCOL.md §5–§6).
//!
//! These tests prove the Phase 2 exit criteria: `why`, `claims`, `evidence`,
//! `residuals`, `attempts`, `trajectory`, `checkpoint`, `context` with
//! machine-readable JSON forms, and the §52 acceptance demo — Agent A
//! discovers a rejected attempt, records claims/evidence/residuals, leaves a
//! checkpoint; Agent B resumes from repository state alone.

// Test closures return the rich store error type; boxed variants would
// obscure construction sites for no measurable gain.
#![allow(clippy::result_large_err)]

use gemel::query::{self, ClaimStatus, IncludeFlags};
use gemel::store::{InitOptions, Repo, REF_STATE_HEAD};
use gemel::workflow::{
    self, BeginOptions, ClaimSpec, CloseTrajectoryOptions, EvidenceSpec, FinishOptions,
    ResidualSpec, ResolveResidualOptions,
};
use std::path::{Path, PathBuf};
use std::sync::atomic::{AtomicU64, Ordering};

static COUNTER: AtomicU64 = AtomicU64::new(0);

fn temp_root(tag: &str) -> PathBuf {
    let n = COUNTER.fetch_add(1, Ordering::SeqCst);
    let dir = std::env::temp_dir().join(format!("gemel-p2-{tag}-{}-{n}", std::process::id()));
    std::fs::remove_dir_all(&dir).ok();
    std::fs::create_dir_all(&dir).unwrap();
    dir
}

fn write_file(root: &Path, rel: &str, content: &str) {
    let path = root.join(rel);
    std::fs::create_dir_all(path.parent().unwrap()).unwrap();
    std::fs::write(path, content).unwrap();
}

/// Seeds the §52 scenario: a rejected first attempt T1 (strict RFC decode),
/// then Agent A's second attempt T2 with a claim, mixed evidence, and an open
/// residual. Returns the repo and the ids Agent A produced.
struct Demo {
    _repo: Repo,
    intent: gemel::gid::Gid,
    t17_rejected: gemel::gid::Gid,
    t18: gemel::gid::Gid,
    claim: gemel::gid::Gid,
    evidence_pass: gemel::gid::Gid,
    evidence_mismatch: gemel::gid::Gid,
    residual: gemel::gid::Gid,
}

fn seed_acceptance_demo(root: &Path) -> Demo {
    write_file(
        root,
        "parser.rs",
        "pub fn decode(name: &[u8]) -> String {\n    String::from_utf8_lossy(name).into_owned()\n}\n",
    );
    let repo = Repo::init(root, &InitOptions::default()).unwrap();

    // Previous agent: T1 (strict RFC decode) → rejected, FreeBSD diverged.
    workflow::begin_change(
        &repo,
        &BeginOptions {
            intent_summary: Some("Fix parser compatibility problem".into()),
            ..Default::default()
        },
    )
    .unwrap();
    write_file(
        root,
        "parser.rs",
        "pub fn decode(name: &[u8]) -> String {\n    strict_decode(name)\n}\nfn strict_decode(name: &[u8]) -> String {\n    String::from_utf8_lossy(name).into_owned()\n}\n",
    );
    workflow::finish_change(
        &repo,
        &FinishOptions {
            summary: "strict RFC decode".into(),
            evidence: vec![
                EvidenceSpec {
                    subject: Some("parser.rs".into()),
                    outcome: "mismatch".into(),
                    kind: "oracle_comparison".into(),
                },
                EvidenceSpec {
                    subject: Some("parser.rs".into()),
                    outcome: "fail".into(),
                    kind: "court_receipt".into(),
                },
            ],
            residuals: vec![ResidualSpec {
                summary: "FreeBSD diverges from the oracle".into(),
                severity: "high".into(),
                classification: "platform_divergence".into(),
                affected_claims: Vec::new(),
                origin_evidence: None,
                affected_changes: Vec::new(),
            }],
            ..Default::default()
        },
    )
    .unwrap();
    workflow::close_trajectory(
        &repo,
        &CloseTrajectoryOptions {
            trajectory: "T1".into(),
            outcome: "rejected".into(),
            reason: Some("strict RFC behavior diverged in 17 oracle cases".into()),
            producer: None,
        },
    )
    .unwrap();

    // Agent A: second attempt T2, with a claim and mixed evidence.
    workflow::begin_change(
        &repo,
        &BeginOptions {
            intent: Some(repo.resolve("I1").unwrap()),
            ..Default::default()
        },
    )
    .unwrap();
    write_file(
        root,
        "parser.rs",
        "pub fn decode(name: &[u8]) -> String {\n    reject_loops(name);\n    String::from_utf8_lossy(name).into_owned()\n}\nfn reject_loops(name: &[u8]) {\n    // pointer loops > 16 rejected\n}\n",
    );
    let finished = workflow::finish_change(
        &repo,
        &FinishOptions {
            summary: "loop rejection".into(),
            claims: vec![ClaimSpec {
                subject: Some("parser.rs".into()),
                predicate: "parser now matches upstream".into(),
                kind: "compatibility".into(),
                // Explicit: both evidence objects bear on this claim.
                evidence: vec![0, 1],
            }],
            evidence: vec![
                EvidenceSpec {
                    subject: Some("parser.rs".into()),
                    outcome: "pass".into(),
                    kind: "court_receipt".into(),
                },
                EvidenceSpec {
                    subject: Some("parser.rs".into()),
                    outcome: "mismatch".into(),
                    kind: "oracle_comparison".into(),
                },
            ],
            residuals: vec![ResidualSpec {
                summary: "FreeBSD diverges from the oracle".into(),
                severity: "high".into(),
                classification: "platform_divergence".into(),
                // Explicit: the divergence affects the compatibility claim
                // and was first observed by the oracle comparison.
                affected_claims: vec![0],
                origin_evidence: Some(1),
                affected_changes: Vec::new(),
            }],
            ..Default::default()
        },
    )
    .unwrap();
    Demo {
        _repo: repo.clone(),
        intent: repo.resolve("I1").unwrap(),
        t17_rejected: repo.resolve("T1").unwrap(),
        t18: repo.resolve("T2").unwrap(),
        claim: finished.claims[0],
        evidence_pass: finished.evidence[0],
        evidence_mismatch: finished.evidence[1],
        residual: finished.residuals[0],
    }
}

// ---------------------------------------------------------------------------
// The §52 acceptance demo
// ---------------------------------------------------------------------------

#[test]
fn acceptance_demo_two_agents() {
    let root = temp_root("demo");
    let demo = seed_acceptance_demo(&root);
    let repo = Repo::find(&root).unwrap();

    // Agent A discovers the rejected attempt before writing new code.
    let attempts = query::attempts(&repo, "parser.rs").unwrap();
    let t1 = attempts
        .iter()
        .find(|a| a.name.as_deref() == Some("T1"))
        .unwrap();
    assert_eq!(t1.outcome.as_deref(), Some("rejected"));
    assert_eq!(
        t1.termination_reason.as_deref(),
        Some("strict RFC behavior diverged in 17 oracle cases")
    );
    assert!(t1.touched_subject);

    // The claim is partially supported (pass + mismatch evidence).
    let (status, supporting, contradicting) = query::claim_status(&repo, &demo.claim).unwrap();
    assert_eq!(status, ClaimStatus::PartiallySupported);
    assert_eq!(supporting, vec![demo.evidence_pass]);
    assert_eq!(contradicting, vec![demo.evidence_mismatch]);

    // Agent A stops; the checkpoint is a continuation boundary.
    let cp = workflow::create_checkpoint(&repo, &workflow::CheckpointOptions::default()).unwrap();
    assert_eq!(cp.name, "K1");
    assert_eq!(cp.plan.intent, Some(demo.intent));
    assert_eq!(cp.plan.open_claims, vec![demo.claim]);
    assert!(cp.plan.unresolved_residuals.contains(&demo.residual));
    assert!(cp
        .plan
        .continuation_scope
        .iter()
        .any(|s| s.contains("FreeBSD diverges")));

    // Agent A marks the trajectory interrupted (context limits).
    workflow::close_trajectory(
        &repo,
        &CloseTrajectoryOptions {
            trajectory: "T2".into(),
            outcome: "interrupted".into(),
            reason: Some("context limit reached".into()),
            producer: None,
        },
    )
    .unwrap();
    // Closing re-points the name at the newest chained version.
    let t18_latest = repo.resolve("T2").unwrap();
    assert_ne!(t18_latest, demo.t18);

    // Agent B receives ONLY the repository + the intent. It asks Gemel for
    // the smallest sufficient context and must discover: T1 rejected, T2
    // interrupted, C1 partially supported, E1/E2, R1.
    let bundle = query::context_bundle(
        &repo,
        "parser.rs",
        Some("I1"),
        4096,
        IncludeFlags::parse("").unwrap(),
    )
    .unwrap();
    let ids: Vec<String> = bundle.items.iter().map(|i| i.id.to_string()).collect();
    assert!(
        ids.contains(&demo.t17_rejected.to_string()),
        "T1 rejected must be in the bundle: {ids:?}"
    );
    assert!(
        ids.contains(&t18_latest.to_string()),
        "T2 interrupted must be in the bundle: {ids:?}"
    );
    assert!(
        ids.contains(&demo.claim.to_string()),
        "C1 must be in the bundle: {ids:?}"
    );
    assert!(
        ids.contains(&demo.evidence_pass.to_string()),
        "E1 must be in the bundle: {ids:?}"
    );
    assert!(
        ids.contains(&demo.evidence_mismatch.to_string()),
        "E2 must be in the bundle: {ids:?}"
    );
    assert!(
        ids.contains(&demo.residual.to_string()),
        "R1 must be in the bundle: {ids:?}"
    );
    // Attempts are labeled with their outcomes.
    let traj: Vec<&query::BundleItem> = bundle
        .items
        .iter()
        .filter(|i| i.family == gemel::family::Family::Trajectory)
        .collect();
    assert!(
        traj.iter().any(|t| t.summary.contains("rejected")),
        "rejected attempt labeled: {:?}",
        traj.iter().map(|t| &t.summary).collect::<Vec<_>>()
    );
    assert!(
        traj.iter().any(|t| t.summary.contains("interrupted")),
        "interrupted attempt labeled: {:?}",
        traj.iter().map(|t| &t.summary).collect::<Vec<_>>()
    );

    // Agent B resolves the FreeBSD discrepancy (the residual is still open).
    assert_eq!(
        query::residual_disposition(&repo, &demo.residual).unwrap(),
        "open"
    );
    let resolved = workflow::resolve_residual(
        &repo,
        &ResolveResidualOptions {
            residual: demo.residual.to_string(),
            disposition: "resolved".into(),
            reason: Some("FreeBSD court now matches after loop rejection".into()),
            producer: None,
        },
    )
    .unwrap();
    // The derived disposition comes from the latest chain version.
    assert_eq!(
        query::residual_disposition(&repo, &resolved.version).unwrap(),
        "resolved"
    );

    // Agent B creates T3 (a new attempt: T2 was closed interrupted).
    workflow::begin_change(
        &repo,
        &BeginOptions {
            intent: Some(demo.intent),
            ..Default::default()
        },
    )
    .unwrap();
    write_file(
        &root,
        "parser.rs",
        "pub fn decode(name: &[u8]) -> String {\n    reject_loops(name);\n    String::from_utf8_lossy(name).into_owned()\n}\nfn reject_loops(name: &[u8]) {\n    // pointer loops > 16 rejected; FreeBSD court matches\n}\n",
    );
    let t3 = workflow::finish_change(
        &repo,
        &FinishOptions {
            summary: "FreeBSD loop match".into(),
            ..Default::default()
        },
    )
    .unwrap();
    assert_eq!(t3.trajectory_name, "T3");
    assert!(t3.is_new_trajectory);

    // `why` walks source → Change → Intent → Claim → Evidence → Residual.
    let report = query::why(&repo, "parser.rs").unwrap();
    let node = report.introduced_by.expect("introduced by a change");
    assert_eq!(node.intent, Some(demo.intent));
    assert!(node.claim.is_some());
    assert_eq!(
        node.claim.as_ref().unwrap().status,
        ClaimStatus::PartiallySupported
    );
    assert_eq!(node.evidence.len(), 2, "claim links both evidence objects");
    assert!(!node.residuals.is_empty(), "residual surfaces in why");
    assert!(
        report
            .previous_approaches
            .iter()
            .any(|a| a.outcome.as_deref() == Some("rejected")),
        "rejected approach surfaces in why"
    );
}

// ---------------------------------------------------------------------------
// Query behaviors
// ---------------------------------------------------------------------------

#[test]
fn closed_trajectory_spawns_fresh_attempt() {
    let root = temp_root("close");
    write_file(&root, "a.txt", "one\n");
    let repo = Repo::init(&root, &InitOptions::default()).unwrap();
    workflow::begin_change(
        &repo,
        &BeginOptions {
            intent_summary: Some("Shared intent".into()),
            ..Default::default()
        },
    )
    .unwrap();
    write_file(&root, "a.txt", "two\n");
    workflow::finish_change(
        &repo,
        &FinishOptions {
            summary: "first".into(),
            ..Default::default()
        },
    )
    .unwrap();

    // Open trajectory continues on the same intent.
    workflow::begin_change(
        &repo,
        &BeginOptions {
            intent: Some(repo.resolve("I1").unwrap()),
            ..Default::default()
        },
    )
    .unwrap();
    write_file(&root, "a.txt", "three\n");
    let f2 = workflow::finish_change(
        &repo,
        &FinishOptions {
            summary: "second".into(),
            ..Default::default()
        },
    )
    .unwrap();
    assert_eq!(f2.trajectory_name, "T1", "open trajectory continues");

    // Closing it makes it terminal: new work spawns a fresh attempt.
    workflow::close_trajectory(
        &repo,
        &CloseTrajectoryOptions {
            trajectory: "T1".into(),
            outcome: "superseded".into(),
            reason: Some("replaced by a better approach".into()),
            producer: None,
        },
    )
    .unwrap();
    workflow::begin_change(
        &repo,
        &BeginOptions {
            intent: Some(repo.resolve("I1").unwrap()),
            ..Default::default()
        },
    )
    .unwrap();
    write_file(&root, "a.txt", "four\n");
    let f3 = workflow::finish_change(
        &repo,
        &FinishOptions {
            summary: "third".into(),
            ..Default::default()
        },
    )
    .unwrap();
    assert_eq!(f3.trajectory_name, "T2", "closed trajectory not continued");
    assert!(f3.is_new_trajectory);

    // The chained trajectory version preserves the outcome.
    let detail = query::trajectory_detail(&repo, "T1").unwrap();
    assert_eq!(detail.outcome.as_deref(), Some("superseded"));
    assert_eq!(
        detail.termination_reason.as_deref(),
        Some("replaced by a better approach")
    );
    // The full sequence is the concatenation across the chain.
    assert_eq!(detail.sequence.len(), 2, "both changes materialized");
}

#[test]
fn log_attributes_each_change_to_its_own_trajectory() {
    let root = temp_root("log-attr");
    write_file(&root, "a.txt", "one\n");
    let repo = Repo::init(&root, &InitOptions::default()).unwrap();
    // T1: two changes on intent I1.
    workflow::begin_change(
        &repo,
        &BeginOptions {
            intent_summary: Some("I1".into()),
            ..Default::default()
        },
    )
    .unwrap();
    write_file(&root, "a.txt", "two\n");
    workflow::finish_change(
        &repo,
        &FinishOptions {
            summary: "c1".into(),
            ..Default::default()
        },
    )
    .unwrap();
    workflow::begin_change(
        &repo,
        &BeginOptions {
            intent: Some(repo.resolve("I1").unwrap()),
            ..Default::default()
        },
    )
    .unwrap();
    write_file(&root, "a.txt", "three\n");
    workflow::finish_change(
        &repo,
        &FinishOptions {
            summary: "c2".into(),
            ..Default::default()
        },
    )
    .unwrap();
    // T2: one change on a different intent — becomes the *current*
    // trajectory.
    workflow::begin_change(
        &repo,
        &BeginOptions {
            intent_summary: Some("I2".into()),
            ..Default::default()
        },
    )
    .unwrap();
    write_file(&root, "b.txt", "b\n");
    workflow::finish_change(
        &repo,
        &FinishOptions {
            summary: "c3".into(),
            ..Default::default()
        },
    )
    .unwrap();

    let entries = query::log(&repo, 10).unwrap();
    assert_eq!(entries.len(), 3);
    // Newest first: c3 (T2), c2 (T1), c1 (T1). Each change is attributed to
    // the trajectory that actually contains it, not the current one.
    assert_eq!(entries[0].summary, "c3");
    assert_eq!(entries[0].trajectory.as_deref(), Some("T2"));
    assert_eq!(entries[1].summary, "c2");
    assert_eq!(entries[1].trajectory.as_deref(), Some("T1"));
    assert_eq!(entries[2].summary, "c1");
    assert_eq!(entries[2].trajectory.as_deref(), Some("T1"));
    // And the current trajectory is T2 — but that must not bleed into the
    // historical entries.
    assert_eq!(
        query::current_trajectory_name(&repo).unwrap().as_deref(),
        Some("T2")
    );
}

#[test]
fn claims_filter_and_pagination() {
    let root = temp_root("claims");
    let repo = Repo::init(&root, &InitOptions::default()).unwrap();
    for (i, predicate) in ["claim alpha", "claim beta", "claim gamma"]
        .iter()
        .enumerate()
    {
        workflow::begin_change(
            &repo,
            &BeginOptions {
                intent_summary: Some(format!("intent {i}")),
                ..Default::default()
            },
        )
        .unwrap();
        write_file(&root, "f.txt", &format!("content {i}\n"));
        workflow::finish_change(
            &repo,
            &FinishOptions {
                summary: format!("change {i}"),
                claims: vec![ClaimSpec {
                    subject: Some(format!("subject-{i}")),
                    predicate: (*predicate).into(),
                    kind: "correctness".into(),
                    evidence: Vec::new(),
                }],
                ..Default::default()
            },
        )
        .unwrap();
    }

    // Subject filter.
    let (rows, next) = query::claims(
        &repo,
        &query::ClaimsFilter {
            subject: Some("subject-1".into()),
            ..Default::default()
        },
    )
    .unwrap();
    assert_eq!(rows.len(), 1);
    assert_eq!(rows[0].predicate, "claim beta");
    assert!(next.is_none(), "no more items after the last page");

    // Status filter: all three claims are UNVERIFIED (no evidence).
    let (rows, _) = query::claims(
        &repo,
        &query::ClaimsFilter {
            status: Some(ClaimStatus::Unverified),
            ..Default::default()
        },
    )
    .unwrap();
    assert_eq!(rows.len(), 3);

    // Cursor pagination: limit 1 walks all three pages deterministically.
    let (p1, c1) = query::claims(
        &repo,
        &query::ClaimsFilter {
            limit: 1,
            ..Default::default()
        },
    )
    .unwrap();
    assert_eq!(p1.len(), 1);
    assert!(c1.is_some(), "more items follow");
    let (p2, c2) = query::claims(
        &repo,
        &query::ClaimsFilter {
            limit: 1,
            cursor: c1.clone(),
            ..Default::default()
        },
    )
    .unwrap();
    assert_eq!(p2.len(), 1);
    assert!(c2.is_some());
    let (p3, c3) = query::claims(
        &repo,
        &query::ClaimsFilter {
            limit: 1,
            cursor: c2.clone(),
            ..Default::default()
        },
    )
    .unwrap();
    assert_eq!(p3.len(), 1);
    assert!(c3.is_none(), "exhausted");
    // Re-requesting from the same cursor is idempotent (same page back).
    let (p4, _) = query::claims(
        &repo,
        &query::ClaimsFilter {
            limit: 1,
            cursor: c2,
            ..Default::default()
        },
    )
    .unwrap();
    assert_eq!(
        p4.iter().map(|r| r.gid).collect::<Vec<_>>(),
        p3.iter().map(|r| r.gid).collect::<Vec<_>>(),
        "cursor re-request returns the same page"
    );
    // No overlap across pages.
    let all_ids: Vec<String> = [&p1, &p2, &p3]
        .iter()
        .flat_map(|p| p.iter().map(|r| r.gid.to_string()))
        .collect();
    assert_eq!(all_ids.len(), 3);
    let unique: std::collections::HashSet<&String> = all_ids.iter().collect();
    assert_eq!(unique.len(), 3, "no duplicated items across pages");
}

#[test]
fn residual_resolution_chains_and_derives_disposition() {
    let root = temp_root("residual");
    let repo = Repo::init(&root, &InitOptions::default()).unwrap();
    workflow::begin_change(&repo, &BeginOptions::default()).unwrap();
    write_file(&root, "a.txt", "x\n");
    let f = workflow::finish_change(
        &repo,
        &FinishOptions {
            summary: "seed".into(),
            residuals: vec![ResidualSpec {
                summary: "expected output differs from oracle".into(),
                severity: "medium".into(),
                classification: "expected_mismatch".into(),
                affected_claims: Vec::new(),
                origin_evidence: None,
                affected_changes: Vec::new(),
            }],
            ..Default::default()
        },
    )
    .unwrap();
    let r1 = f.residuals[0];
    assert_eq!(query::residual_disposition(&repo, &r1).unwrap(), "open");

    // Acknowledge, then resolve: each is a chained version.
    let ack = workflow::resolve_residual(
        &repo,
        &ResolveResidualOptions {
            residual: r1.to_string(),
            disposition: "acknowledged".into(),
            reason: Some("accepted for now".into()),
            producer: None,
        },
    )
    .unwrap();
    assert_eq!(
        query::residual_disposition(&repo, &ack.version).unwrap(),
        "acknowledged"
    );
    let done = workflow::resolve_residual(
        &repo,
        &ResolveResidualOptions {
            residual: r1.to_string(),
            disposition: "resolved".into(),
            reason: Some("fixed".into()),
            producer: None,
        },
    )
    .unwrap();
    assert_eq!(
        query::residual_disposition(&repo, &done.version).unwrap(),
        "resolved"
    );
    // The latest chain version is found from any version.
    assert_eq!(query::chain_latest(&repo, &r1).unwrap(), done.version);
    assert_eq!(
        query::chain_latest(&repo, &ack.version).unwrap(),
        done.version
    );

    // The list endpoint resolves each residual to its latest version.
    let (rows, _) = query::residuals(&repo, &query::ResidualsFilter::default()).unwrap();
    assert_eq!(rows.len(), 1);
    assert_eq!(rows[0].gid, done.version);
    assert_eq!(rows[0].disposition, "resolved");
}

#[test]
fn trajectory_detail_materializes_sequence_and_handoff() {
    let root = temp_root("traj");
    let repo = Repo::init(&root, &InitOptions::default()).unwrap();
    workflow::begin_change(
        &repo,
        &BeginOptions {
            intent_summary: Some("Implement X".into()),
            ..Default::default()
        },
    )
    .unwrap();
    write_file(&root, "a.txt", "1\n");
    workflow::finish_change(
        &repo,
        &FinishOptions {
            summary: "step one".into(),
            ..Default::default()
        },
    )
    .unwrap();
    workflow::begin_change(
        &repo,
        &BeginOptions {
            intent: Some(repo.resolve("I1").unwrap()),
            ..Default::default()
        },
    )
    .unwrap();
    write_file(&root, "a.txt", "1\n2\n");
    workflow::finish_change(
        &repo,
        &FinishOptions {
            summary: "step two".into(),
            ..Default::default()
        },
    )
    .unwrap();

    let detail = query::trajectory_detail(&repo, "T1").unwrap();
    assert_eq!(detail.name.as_deref(), Some("T1"));
    assert_eq!(detail.sequence.len(), 2);
    assert_eq!(detail.sequence[0].summary, "step one");
    assert_eq!(detail.sequence[1].summary, "step two");
    assert!(detail.sequence[0].state.is_some());
    assert_eq!(detail.outcome, None, "open trajectory has no outcome");
}

#[test]
fn evidence_freshness_tracks_evaluated_state() {
    let root = temp_root("fresh");
    let repo = Repo::init(&root, &InitOptions::default()).unwrap();
    workflow::begin_change(
        &repo,
        &BeginOptions {
            intent_summary: Some("i".into()),
            ..Default::default()
        },
    )
    .unwrap();
    write_file(&root, "a.txt", "1\n");
    workflow::finish_change(
        &repo,
        &FinishOptions {
            summary: "c1".into(),
            evidence: vec![EvidenceSpec {
                subject: Some("a.txt".into()),
                outcome: "pass".into(),
                kind: "test_result".into(),
            }],
            ..Default::default()
        },
    )
    .unwrap();
    let head = repo.read_ref(REF_STATE_HEAD).unwrap().unwrap();
    let (rows, _) = query::residuals(&repo, &query::ResidualsFilter::default()).unwrap();
    let _ = rows;
    let all = query::claims(&repo, &query::ClaimsFilter::default())
        .unwrap()
        .0;
    assert!(all.is_empty());
    // The only evidence object: CURRENT while head state matches.
    let evidence = {
        let mut out = Vec::new();
        for (_, latest) in query::all_trajectories(&repo).unwrap() {
            for (_, tobj) in query::trajectory_versions(&repo, &latest).unwrap() {
                let fs = tobj.field_sequence().unwrap_or(&[]);
                for c in query::gid_list(fs, 0x06) {
                    if let Ok(cobj) = repo.load(&c) {
                        let cfs = cobj.field_sequence().unwrap_or(&[]);
                        out.extend(query::gid_list(cfs, 0x0D));
                    }
                }
            }
        }
        out
    };
    assert_eq!(evidence.len(), 1);
    let row = query::evidence_show(&repo, &evidence[0].to_string()).unwrap();
    // No evaluated_state anchor: fresh by default.
    assert_eq!(row.freshness, query::Freshness::Current);
    // With an explicit anchor at the head state it is CURRENT; advancing the
    // head conservatively marks MAY_REQUIRE_REFRESH.
    let obj = repo.load(&evidence[0]).unwrap();
    let mut fields = obj.field_sequence().unwrap_or(&[]).to_vec();
    fields.push(gemel::value::Field::new(
        0x11,
        gemel::value::Value::Gid(head),
    ));
    fields.sort_by_key(|f| f.tag);
    let anchored = gemel::value::Object::fields(gemel::family::Family::Evidence, fields);
    let anchored_id = repo.insert_object(&anchored).unwrap();
    assert_eq!(
        query::evidence_freshness(&repo, &anchored_id).unwrap(),
        query::Freshness::Current
    );
}

#[test]
fn context_bundle_is_bounded_and_deterministic() {
    let root = temp_root("ctx");
    let demo = seed_acceptance_demo(&root);
    let repo = Repo::find(&root).unwrap();

    let a = query::context_bundle(
        &repo,
        "parser.rs",
        Some("I1"),
        4096,
        IncludeFlags::parse("").unwrap(),
    )
    .unwrap();
    let b = query::context_bundle(
        &repo,
        "parser.rs",
        Some("I1"),
        4096,
        IncludeFlags::parse("").unwrap(),
    )
    .unwrap();
    // Deterministic: identical inputs → identical bundles.
    let key = |x: &query::ContextBundle| {
        x.items
            .iter()
            .map(|i| format!("{}:{}:{}", i.id, i.level, i.summary))
            .collect::<Vec<_>>()
    };
    assert_eq!(key(&a), key(&b));
    assert!(a.consumed <= a.budget_tokens);
    assert!(!a.items.is_empty());
    let _ = demo;

    // A tiny budget bounds the bundle and reports the expansion point.
    let tiny = query::context_bundle(
        &repo,
        "parser.rs",
        None,
        16,
        IncludeFlags::parse("claims,attempts,evidence,residuals").unwrap(),
    )
    .unwrap();
    assert!(tiny.consumed <= tiny.budget_tokens);
    assert!(
        tiny.items.len() < a.items.len(),
        "smaller budget yields a smaller bundle ({} vs {})",
        tiny.items.len(),
        a.items.len()
    );
}

#[test]
fn why_reports_uncertainty_when_subject_absent() {
    let root = temp_root("why-none");
    write_file(&root, "a.txt", "x\n");
    let repo = Repo::init(&root, &InitOptions::default()).unwrap();
    let report = query::why(&repo, "ghost.rs").unwrap();
    assert!(report.introduced_by.is_none());
    assert!(
        !report.uncertainty.is_empty(),
        "explicit uncertainty, never invented history"
    );
    assert!(report.previous_approaches.is_empty());
}

#[test]
fn checkpoint_chains_versions() {
    let root = temp_root("cp");
    let repo = Repo::init(&root, &InitOptions::default()).unwrap();
    workflow::begin_change(
        &repo,
        &BeginOptions {
            intent_summary: Some("long work".into()),
            ..Default::default()
        },
    )
    .unwrap();
    write_file(&root, "a.txt", "1\n");
    workflow::finish_change(
        &repo,
        &FinishOptions {
            summary: "c1".into(),
            ..Default::default()
        },
    )
    .unwrap();
    let k1 = workflow::create_checkpoint(&repo, &workflow::CheckpointOptions::default()).unwrap();
    let k2 = workflow::create_checkpoint(&repo, &workflow::CheckpointOptions::default()).unwrap();
    assert_eq!(k1.name, "K1");
    assert_eq!(k2.name, "K2");
    // The second checkpoint chains to the first.
    let obj = repo.load(&k2.checkpoint).unwrap();
    let prev = query::gid_field(obj.field_sequence().unwrap_or(&[]), 0x01);
    assert_eq!(prev, Some(k1.checkpoint));
    // refs/checkpoints/current points at the latest.
    let current = repo
        .read_ref(&format!("{}/current", gemel::store::REF_CHECKPOINTS))
        .unwrap();
    assert_eq!(current, Some(k2.checkpoint));
}

#[test]
fn cli_json_forms_are_parseable() {
    let root = temp_root("cli");
    let demo = seed_acceptance_demo(&root);
    let _ = demo;
    let bin = env!("CARGO_BIN_EXE_gemel");
    let run = |args: &[&str]| -> serde_json::Value {
        let mut cmd = std::process::Command::new(bin);
        cmd.arg("--repo").arg(&root);
        cmd.args(args);
        let out = cmd.output().expect("run gemel");
        assert_eq!(
            out.status.code(),
            Some(0),
            "args {args:?}: {}",
            String::from_utf8_lossy(&out.stderr)
        );
        serde_json::from_slice(&out.stdout).expect("valid gemel.query.v1 JSON")
    };
    let why = run(&["why", "parser.rs", "--json"]);
    assert_eq!(why["schema"], "gemel.query.v1");
    assert_eq!(why["request"]["command"], "why");
    assert!(why["result"]["introduced_by"]["claim"].is_object());
    let claims = run(&["claims", "--subject", "parser.rs", "--json"]);
    assert_eq!(claims["pagination"]["count"], 1);
    assert_eq!(
        claims["result"]["claims"][0]["status"],
        "PARTIALLY_SUPPORTED"
    );
    let residuals = run(&["residuals", "--disposition", "open", "--json"]);
    assert!(!residuals["result"]["residuals"]
        .as_array()
        .unwrap()
        .is_empty());
    let attempts = run(&["attempts", "parser.rs", "--json"]);
    let names: Vec<&str> = attempts["result"]["attempts"]
        .as_array()
        .unwrap()
        .iter()
        .filter_map(|a| a["name"].as_str())
        .collect();
    assert!(names.contains(&"T1"));
    let traj = run(&["trajectory", "T2", "--json"]);
    assert_eq!(traj["result"]["outcome"], serde_json::Value::Null);
    let cp = run(&["checkpoint", "--json"]);
    assert!(cp["result"]["name"].as_str().unwrap().starts_with('K'));
    let ctx = run(&["context", "parser.rs", "--budget", "2000", "--json"]);
    assert!(ctx["result"]["bundle"]["objects"].as_array().unwrap().len() >= 5);
}