atheneum 0.12.2

Agent coordination graph database - episodic and semantic memory for multi-agent workflows
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
use atheneum::graph::{
    AtheneumGraph, EdgeType, FileAccessParams, ProvenanceData, SessionParams, ToolCallParams,
};
use serde_json::json;
use tempfile::tempdir;

#[test]
fn query_session_by_id_returns_recent_summary() {
    let dir = tempdir().expect("tempdir");
    let db_path = dir.path().join("atheneum.db");
    let graph = AtheneumGraph::open(&db_path).expect("open graph");

    graph
        .record_session(SessionParams {
            session_id: "sess-1".to_string(),
            agent_name: "hermes".to_string(),
            project: "envoy".to_string(),
            tool: "hermes".to_string(),
            trigger: "cli".to_string(),
            model: Some("openai/gpt-test".to_string()),
            git_branch: Some("main".to_string()),
            git_head: None,
            parent_session_id: None,
            relations: vec![],
        })
        .expect("record session");

    graph
        .record_evidence_tool_call(ToolCallParams {
            session_id: "sess-1".to_string(),
            tool_name: "magellan".to_string(),
            tool_version: None,
            sequence: Some(0),
            source: None,
            input_hash: None,
            input_summary: Some("status".to_string()),
            output_hash: None,
            output_summary: Some("ok".to_string()),
            exit_status: "ok".to_string(),
            latency_ms: 42,
            input_tokens_est: Some(12),
            tool_category: "grounded".to_string(),
            relations: vec![],
        })
        .expect("record tool call");

    let summary = graph
        .query_session_by_id("sess-1")
        .expect("query session")
        .expect("session exists");

    assert_eq!(summary.session_id, "sess-1");
    assert_eq!(summary.project, "envoy");
    assert_eq!(summary.last_tool.as_deref(), Some("magellan"));
    assert_eq!(summary.last_tool_summary.as_deref(), Some("status"));
}

#[test]
fn recent_discoveries_filters_by_project_and_agent() {
    let dir = tempdir().expect("tempdir");
    let db_path = dir.path().join("atheneum.db");
    let graph = AtheneumGraph::open(&db_path).expect("open graph");

    graph
        .store_discovery_in_project(
            "hermes",
            "Finding",
            "session-observability",
            Some("envoy"),
            json!({"summary": "first"}),
        )
        .expect("store discovery");
    graph
        .store_discovery_in_project(
            "codex",
            "Finding",
            "session-observability",
            Some("envoy"),
            json!({"summary": "second"}),
        )
        .expect("store discovery");
    graph
        .store_discovery_in_project(
            "hermes",
            "Finding",
            "session-observability",
            Some("atheneum"),
            json!({"summary": "third"}),
        )
        .expect("store discovery");

    let envoy_only = graph
        .recent_discoveries(Some("envoy"), None, None, None, 10)
        .expect("recent discoveries");
    assert_eq!(envoy_only.len(), 2);

    let hermes_envoy = graph
        .recent_discoveries(Some("envoy"), Some("hermes"), None, None, 10)
        .expect("recent discoveries");
    assert_eq!(hermes_envoy.len(), 1);
    assert_eq!(hermes_envoy[0].data["agent"].as_str(), Some("hermes"));
    assert_eq!(hermes_envoy[0].data["project_id"].as_str(), Some("envoy"));
}

/// `recent_discoveries` session filter: `json_extract(data,'$.session_id')`.
/// Backs the `discoveries-recent --session` flag used by the Phase-3 decision
/// backfill idempotency pre-scan.
#[test]
fn recent_discoveries_filters_by_session() {
    let dir = tempdir().expect("tempdir");
    let db_path = dir.path().join("atheneum.db");
    let graph = AtheneumGraph::open(&db_path).expect("open graph");

    // Two discoveries in sess_a, one in sess_b — all project envoy.
    graph
        .store_discovery_in_project(
            "claude",
            "Decision",
            "pick_a1",
            Some("envoy"),
            json!({"session_id": "sess_a", "sequence": 1}),
        )
        .expect("store a1");
    graph
        .store_discovery_in_project(
            "claude",
            "Decision",
            "pick_a2",
            Some("envoy"),
            json!({"session_id": "sess_a", "sequence": 2}),
        )
        .expect("store a2");
    graph
        .store_discovery_in_project(
            "claude",
            "Decision",
            "pick_b1",
            Some("envoy"),
            json!({"session_id": "sess_b", "sequence": 1}),
        )
        .expect("store b1");

    let sess_a = graph
        .recent_discoveries(None, None, Some("sess_a"), None, 100)
        .expect("recent sess_a");
    assert_eq!(sess_a.len(), 2, "only the two sess_a decisions");
    assert!(sess_a.iter().all(|d| d.data["session_id"] == "sess_a"));

    let sess_b = graph
        .recent_discoveries(None, None, Some("sess_b"), None, 100)
        .expect("recent sess_b");
    assert_eq!(sess_b.len(), 1);
    assert_eq!(sess_b[0].data["target"].as_str(), Some("pick_b1"));

    let none = graph
        .recent_discoveries(None, None, Some("sess_missing"), None, 100)
        .expect("recent missing");
    assert!(none.is_empty(), "unknown session -> empty, not error");
}

#[test]
fn recent_discoveries_filter_by_discovery_type() {
    // The `--type` / `discovery_type` filter narrows to one discovery_type and
    // combines with the session filter. Three Decisions across sess_a/sess_b
    // plus one Bug; `Decision` + no session = 3, `Decision` + sess_a = 2,
    // `Bug` = 1, an absent type = 0.
    let dir = tempdir().expect("tempdir");
    let db_path = dir.path().join("atheneum.db");
    let graph = AtheneumGraph::open(&db_path).expect("open graph");

    graph
        .store_discovery_in_project(
            "claude",
            "Decision",
            "pick_a1",
            Some("envoy"),
            json!({"session_id": "sess_a", "sequence": 1}),
        )
        .expect("store a1");
    graph
        .store_discovery_in_project(
            "claude",
            "Decision",
            "pick_a2",
            Some("envoy"),
            json!({"session_id": "sess_a", "sequence": 2}),
        )
        .expect("store a2");
    graph
        .store_discovery_in_project(
            "claude",
            "Decision",
            "pick_b1",
            Some("envoy"),
            json!({"session_id": "sess_b", "sequence": 1}),
        )
        .expect("store b1");
    graph
        .store_discovery_in_project(
            "claude",
            "Bug",
            "bug_x",
            Some("envoy"),
            json!({"session_id": "sess_b", "sequence": 2}),
        )
        .expect("store bug");

    let decisions = graph
        .recent_discoveries(None, None, None, Some("Decision"), 100)
        .expect("recent decisions");
    assert_eq!(decisions.len(), 3, "all three Decisions, no the Bug");

    let sess_a_decisions = graph
        .recent_discoveries(None, None, Some("sess_a"), Some("Decision"), 100)
        .expect("recent sess_a decisions");
    assert_eq!(sess_a_decisions.len(), 2, "type + session combine");

    let bugs = graph
        .recent_discoveries(None, None, None, Some("Bug"), 100)
        .expect("recent bugs");
    assert_eq!(bugs.len(), 1);

    let absent = graph
        .recent_discoveries(None, None, None, Some("Invariant"), 100)
        .expect("recent absent type");
    assert!(absent.is_empty(), "unknown type -> empty, not error");
}

#[test]
fn recent_handoffs_filter_by_agent_and_project() {
    let dir = tempdir().expect("tempdir");
    let db_path = dir.path().join("atheneum.db");
    let graph = AtheneumGraph::open(&db_path).expect("open graph");

    graph
        .store_handoff_in_project(
            "claude3",
            "claude4",
            Some("envoy"),
            json!({"task": "inspect sessions"}),
        )
        .expect("store handoff");
    graph
        .store_handoff_in_project(
            "codex",
            "claude4",
            Some("atheneum"),
            json!({"task": "inspect handoffs"}),
        )
        .expect("store handoff");

    let envoy_only = graph
        .recent_handoffs(Some("envoy"), None, 10)
        .expect("recent handoffs");
    assert_eq!(envoy_only.len(), 1);
    assert_eq!(envoy_only[0].data["project_id"].as_str(), Some("envoy"));

    let claude4_only = graph
        .recent_handoffs(None, Some("claude4"), 10)
        .expect("recent handoffs");
    assert_eq!(claude4_only.len(), 2);

    let envoy_claude4 = graph
        .recent_handoffs(Some("envoy"), Some("claude4"), 10)
        .expect("recent handoffs");
    assert_eq!(envoy_claude4.len(), 1);
    assert_eq!(envoy_claude4[0].data["to_agent"].as_str(), Some("claude4"));
}

#[test]
fn recent_sessions_filter_by_project_and_agent() {
    let dir = tempdir().expect("tempdir");
    let db_path = dir.path().join("atheneum.db");
    let graph = AtheneumGraph::open(&db_path).expect("open graph");

    graph
        .record_session(SessionParams {
            session_id: "sess-envoy-hermes".to_string(),
            agent_name: "hermes".to_string(),
            project: "envoy".to_string(),
            tool: "hermes".to_string(),
            trigger: "cli".to_string(),
            model: Some("local/hermes".to_string()),
            git_branch: Some("main".to_string()),
            git_head: None,
            parent_session_id: None,
            relations: vec![],
        })
        .expect("record session");
    graph
        .record_session(SessionParams {
            session_id: "sess-atheneum-codex".to_string(),
            agent_name: "codex".to_string(),
            project: "atheneum".to_string(),
            tool: "codex".to_string(),
            trigger: "cli".to_string(),
            model: Some("local/codex".to_string()),
            git_branch: Some("main".to_string()),
            git_head: None,
            parent_session_id: None,
            relations: vec![],
        })
        .expect("record session");

    let envoy_sessions = graph
        .query_sessions_recent(Some("envoy"), None, 10, &[])
        .expect("recent sessions");
    assert_eq!(envoy_sessions.len(), 1);
    assert_eq!(envoy_sessions[0].session_id, "sess-envoy-hermes");

    let hermes_sessions = graph
        .query_sessions_recent(None, Some("hermes"), 10, &[])
        .expect("recent sessions");
    assert_eq!(hermes_sessions.len(), 1);
    assert_eq!(hermes_sessions[0].project, "envoy");

    let atheneum_codex = graph
        .query_sessions_recent(Some("atheneum"), Some("codex"), 10, &[])
        .expect("recent sessions");
    assert_eq!(atheneum_codex.len(), 1);
    assert_eq!(atheneum_codex[0].session_id, "sess-atheneum-codex");
}

/// `--exclude-project` hides named project buckets from `sessions-recent`
/// without re-attributing the rows. The `LIMIT` applies after exclusion.
#[test]
fn recent_sessions_exclude_project_hides_named_buckets() {
    let dir = tempdir().expect("tempdir");
    let db_path = dir.path().join("atheneum.db");
    let graph = AtheneumGraph::open(&db_path).expect("open graph");

    let mk = |sid: &str, agent: &str, project: &str| {
        graph
            .record_session(SessionParams {
                session_id: sid.to_string(),
                agent_name: agent.to_string(),
                project: project.to_string(),
                tool: "claude-code".to_string(),
                trigger: "cli".to_string(),
                model: None,
                git_branch: None,
                git_head: None,
                parent_session_id: None,
                relations: vec![],
            })
            .expect("record session");
    };
    mk("sess-tmp-a", "claude", "tmp");
    mk("sess-tmp-b", "claude", "tmp");
    mk("sess-atheneum", "claude", "atheneum");
    mk("sess-envoy", "claude", "envoy");

    // No exclude: all four present.
    let all = graph
        .query_sessions_recent(None, None, 100, &[])
        .expect("recent sessions");
    assert_eq!(all.len(), 4);

    // Exclude "tmp": the two tmp sessions drop, others remain.
    let excluded = graph
        .query_sessions_recent(None, None, 100, &["tmp".to_string()])
        .expect("recent sessions");
    let projects: Vec<String> = excluded.iter().map(|s| s.project.clone()).collect();
    assert!(!projects.contains(&"tmp".to_string()));
    assert_eq!(excluded.len(), 2);
    assert!(projects.contains(&"atheneum".to_string()));
    assert!(projects.contains(&"envoy".to_string()));

    // Exclude two buckets at once.
    let excluded2 = graph
        .query_sessions_recent(None, None, 100, &["tmp".to_string(), "envoy".to_string()])
        .expect("recent sessions");
    let projects2: Vec<String> = excluded2.iter().map(|s| s.project.clone()).collect();
    assert_eq!(excluded2.len(), 1);
    assert_eq!(projects2[0], "atheneum");

    // LIMIT applies after exclusion: excluding tmp (2 rows) leaves 2, limit 1
    // returns exactly one non-tmp session.
    let limited = graph
        .query_sessions_recent(None, None, 1, &["tmp".to_string()])
        .expect("recent sessions");
    assert_eq!(limited.len(), 1);
    assert_ne!(limited[0].project, "tmp");

    // Combine project filter + exclude (exclude is redundant here but must not
    // break the query).
    let combined = graph
        .query_sessions_recent(Some("atheneum"), None, 100, &["tmp".to_string()])
        .expect("recent sessions");
    assert_eq!(combined.len(), 1);
    assert_eq!(combined[0].session_id, "sess-atheneum");
}

/// `session-digest` composes a bounded bootstrap packet with activity
/// computed from `event_log` (not the zero `sessions` ledger columns),
/// ReasoningLog decisions linked via `observed_in`, project memory, and
/// discoveries attributed via the new `discoveries.session_id` column.
#[test]
fn session_digest_computes_activity_and_surfaces_decisions() {
    let dir = tempdir().expect("tempdir");
    let db_path = dir.path().join("atheneum.db");
    let graph = AtheneumGraph::open(&db_path).expect("open graph");

    // Session with no backfilled ledger counts — the digest must compute
    // activity from event_log instead of trusting the zero columns.
    let session_id = "digest-sess-1".to_string();
    graph
        .record_session(SessionParams {
            session_id: session_id.clone(),
            agent_name: "claude".to_string(),
            project: "atheneum".to_string(),
            tool: "claude-code".to_string(),
            trigger: "cli".to_string(),
            model: Some("claude-fable-5".to_string()),
            git_branch: Some("main".to_string()),
            git_head: None,
            parent_session_id: None,
            relations: vec![],
        })
        .expect("record session");

    // Three tool calls recorded as events.
    for (i, tool) in ["Bash", "Edit", "Write"].iter().enumerate() {
        graph
            .record_evidence_tool_call(ToolCallParams {
                session_id: session_id.clone(),
                tool_name: tool.to_string(),
                tool_version: None,
                sequence: Some(i as i64),
                source: None,
                input_hash: None,
                input_summary: Some(format!("{} input", tool)),
                output_hash: None,
                output_summary: Some("ok".to_string()),
                exit_status: "ok".to_string(),
                latency_ms: 10,
                input_tokens_est: Some(5),
                tool_category: "grounded".to_string(),
                relations: vec![],
            })
            .expect("record tool call");
    }

    // Two file writes (transcript-sync style: file_access + access_type=write)
    // and two reads of other files.
    for (i, path) in [
        ("src/digest.rs", "write"),
        ("src/digest.rs", "write"),
        ("README.md", "read"),
        ("Cargo.toml", "read"),
    ]
    .iter()
    .enumerate()
    {
        graph
            .record_evidence_file_access(FileAccessParams {
                session_id: session_id.clone(),
                file_path: path.0.to_string(),
                sequence: i as i64,
                access_type: path.1.to_string(),
                tool_name: Some("claude-code".to_string()),
                source: Some("test".to_string()),
                relations: vec![],
            })
            .expect("record file access");
    }

    // A ReasoningLog decision linked to the session via observed_in — the
    // edge direction the digest joins on (reasoning -[observed_in]-> session).
    let reasoning_id = graph
        .insert_reasoning_log(
            "claude",
            "Use file_access.access_type=write, not file_write",
            None,
        )
        .expect("insert reasoning log");
    let session_entity_id = graph
        .entities_by_kind("Session")
        .expect("list session entities")
        .into_iter()
        .find(|e| e.data.get("session_id").and_then(|v| v.as_str()) == Some(&session_id))
        .expect("session entity exists")
        .id;
    graph
        .insert_edge(
            reasoning_id,
            session_entity_id,
            EdgeType::ObservedIn,
            json!({"provenance": ProvenanceData::new("test").to_value()}),
        )
        .expect("link reasoning to session");

    // Project memory + a discovery attributed to the session.
    graph
        .store_memory(
            "digest-format",
            "Digest is extractive plain-text, bounded to a token budget.",
            "project",
            0.9,
            Some("atheneum"),
            None,
        )
        .expect("store memory");
    graph
        .store_discovery(
            "claude",
            "Decision",
            "session-digest",
            json!({
                "session_id": session_id,
                "project_id": "atheneum",
                "summary": "compute activity from event_log",
            }),
        )
        .expect("store discovery");

    let text = graph
        .compose_digest(Some("atheneum"), 3, 500)
        .expect("compose digest");

    // Header + session tool (not the last tool call) + computed activity.
    assert!(
        text.contains("PRIOR SESSIONS (project: atheneum"),
        "header: {}",
        text
    );
    assert!(text.contains("tool=claude-code"), "session tool: {}", text);
    assert!(
        text.contains("3 tool calls"),
        "computed tool calls: {}",
        text
    );
    assert!(
        text.contains("2 file writes"),
        "computed file writes: {}",
        text
    );
    // Top files include the written file basename.
    assert!(text.contains("digest.rs"), "top files: {}", text);
    // ReasoningLog decision surfaced via the observed_in join.
    assert!(
        text.contains("file_access.access_type=write"),
        "reasoning decision: {}",
        text
    );
    // Project memory + session-attributed discovery.
    assert!(text.contains("PROJECT MEMORY"), "memory header: {}", text);
    assert!(
        text.contains("extractive plain-text"),
        "memory content: {}",
        text
    );
    assert!(
        text.contains("discovery: session-digest"),
        "discovery: {}",
        text
    );
    // Thread anchors present for navigate follow-up.
    assert!(text.contains("THREAD ANCHORS"), "anchors: {}", text);

    // Bounded: the body must fit within the 500-token budget (4 chars/token)
    // plus the truncation marker allowance used by the renderer.
    let budget = 500 * 4;
    assert!(
        text.len() <= budget + "[truncated]\n".len(),
        "digest not bounded ({} > {}): {}",
        text.len(),
        budget,
        text
    );

    // --json path produces a serializable value with the same computed counts.
    let value = graph
        .compose_digest_json(Some("atheneum"), 3)
        .expect("compose digest json");
    let sessions = value
        .get("sessions")
        .and_then(|v| v.as_array())
        .expect("sessions array");
    assert_eq!(sessions.len(), 1);
    assert_eq!(sessions[0]["tool_calls"], json!(3));
    assert_eq!(sessions[0]["file_writes"], json!(2));
    assert_eq!(sessions[0]["tool"], json!("claude-code"));
    assert_eq!(sessions[0]["tool"].as_str().unwrap(), "claude-code");
}

/// The digest's `decisions` block surfaces `Decision` discoveries from every
/// capture source — filtered to `discovery_type = 'Decision'` and labeled with
/// the capture `source` — without dropping the existing `discoveries` block.
/// A non-Decision discovery (Bug) must NOT appear in the decisions block.
#[test]
fn session_digest_surfaces_structured_decisions_with_source() {
    let dir = tempdir().expect("tempdir");
    let db_path = dir.path().join("atheneum.db");
    let graph = AtheneumGraph::open(&db_path).expect("open graph");

    let session_id = "digest-dec-sess".to_string();
    graph
        .record_session(SessionParams {
            session_id: session_id.clone(),
            agent_name: "claude".to_string(),
            project: "atheneum".to_string(),
            tool: "claude-code".to_string(),
            trigger: "cli".to_string(),
            model: None,
            git_branch: Some("main".to_string()),
            git_head: None,
            parent_session_id: None,
            relations: vec![],
        })
        .expect("record session");

    // A live-watcher AskUserQuestion decision with structured fields.
    graph
        .store_discovery(
            "claude",
            "Decision",
            "Index",
            json!({
                "session_id": session_id,
                "project_id": "atheneum",
                "source": "askuser",
                "chosen": "HNSW",
                "alternatives": ["HNSW", "brute"],
                "rationale": "fast ANN",
                "sequence": 1,
            }),
        )
        .expect("store askuser decision");
    // A Bug in the same session — must surface in `discoveries` but NOT in
    // `decisions` (the decisions block is Decision-only).
    graph
        .store_discovery(
            "claude",
            "Bug",
            "query_sessions",
            json!({
                "session_id": session_id,
                "project_id": "atheneum",
                "summary": "anonymous ? params",
            }),
        )
        .expect("store bug");

    let text = graph
        .compose_digest(Some("atheneum"), 3, 800)
        .expect("compose digest");

    // Decision block: chosen label + source tag + rationale.
    assert!(
        text.contains("decision: HNSW [askuser] — fast ANN"),
        "structured decision with source: {}",
        text
    );
    // The Bug appears in the discoveries block, not the decisions block.
    assert!(
        text.contains("discovery: query_sessions [Bug]"),
        "bug still in discoveries: {}",
        text
    );
    // No fabricated "decision:" line for the Bug.
    assert!(
        !text.contains("decision: query_sessions"),
        "bug must not leak into decisions block: {}",
        text
    );

    // --json: the decisions array carries source/chosen/rationale.
    let value = graph
        .compose_digest_json(Some("atheneum"), 3)
        .expect("compose digest json");
    let decisions = value["sessions"][0]["decisions"]
        .as_array()
        .expect("decisions array");
    assert_eq!(decisions.len(), 1, "only the Decision, not the Bug");
    assert_eq!(decisions[0]["target"], json!("Index"));
    assert_eq!(decisions[0]["source"], json!("askuser"));
    assert_eq!(decisions[0]["chosen"], json!("HNSW"));
    assert_eq!(decisions[0]["rationale"], json!("fast ANN"));
}

#[test]
fn thread_query_walks_discovery_chain_in_order() {
    let graph = AtheneumGraph::open_in_memory().expect("open graph");

    let session_id = "thread-sess-1".to_string();
    graph
        .record_session(SessionParams {
            session_id: session_id.clone(),
            agent_name: "claude".to_string(),
            project: "atheneum".to_string(),
            tool: "claude-code".to_string(),
            trigger: "cli".to_string(),
            model: None,
            git_branch: Some("main".to_string()),
            git_head: None,
            parent_session_id: None,
            relations: vec![],
        })
        .expect("record session");

    let session_entity_id = graph
        .entities_by_kind("Session")
        .expect("list session entities")
        .into_iter()
        .find(|e| e.data.get("session_id").and_then(|v| v.as_str()) == Some(&session_id))
        .expect("session entity exists")
        .id;

    // Three chained discoveries in the same session. Each shares the token
    // "chainstep" so lexical_search surfaces all three as thread entry points.
    let mut discovery_ids: Vec<i64> = Vec::new();
    for (tag, summary) in [
        ("alpha", "first decision: adopt graph chain"),
        ("beta", "second decision: add led_to inverse"),
        ("gamma", "third decision: bound thread to tokens"),
    ] {
        let id = graph
            .store_discovery(
                "claude",
                "Decision",
                &format!("chainstep {}", tag),
                json!({
                    "session_id": session_id,
                    "project_id": "atheneum",
                    "summary": summary,
                }),
            )
            .expect("store discovery");
        discovery_ids.push(id);
    }
    let (d1, d2, d3) = (discovery_ids[0], discovery_ids[1], discovery_ids[2]);

    // Every discovery got an observed_in -> Session edge from store_discovery.
    for &d in &[d1, d2, d3] {
        let has_obs = graph
            .outgoing_edges(d)
            .expect("out edges")
            .iter()
            .any(|e| e.edge_type == "observed_in" && e.to_id == session_entity_id);
        assert!(has_obs, "discovery {} missing observed_in -> Session", d);
    }

    // caused_by chain: d2 -> d1, d3 -> d2 (each links to the prior same-session
    // decision by entity id = insert/chronological order).
    let d2_causes: Vec<i64> = graph
        .outgoing_edges(d2)
        .expect("out d2")
        .iter()
        .filter(|e| e.edge_type == "caused_by")
        .map(|e| e.to_id)
        .collect();
    assert!(
        d2_causes.contains(&d1),
        "d2 should caused_by d1, got {:?}",
        d2_causes
    );
    let d3_causes: Vec<i64> = graph
        .outgoing_edges(d3)
        .expect("out d3")
        .iter()
        .filter(|e| e.edge_type == "caused_by")
        .map(|e| e.to_id)
        .collect();
    assert!(
        d3_causes.contains(&d2),
        "d3 should caused_by d2, got {:?}",
        d3_causes
    );

    // led_to inverse: d1 -> d2, d2 -> d3.
    let d1_led: Vec<i64> = graph
        .outgoing_edges(d1)
        .expect("out d1")
        .iter()
        .filter(|e| e.edge_type == "led_to")
        .map(|e| e.to_id)
        .collect();
    assert!(
        d1_led.contains(&d2),
        "d1 should led_to d2, got {:?}",
        d1_led
    );
    let d2_led: Vec<i64> = graph
        .outgoing_edges(d2)
        .expect("out d2")
        .iter()
        .filter(|e| e.edge_type == "led_to")
        .map(|e| e.to_id)
        .collect();
    assert!(
        d2_led.contains(&d3),
        "d2 should led_to d3, got {:?}",
        d2_led
    );

    // d1 is a thread root: no caused_by edge.
    let d1_causes: Vec<i64> = graph
        .outgoing_edges(d1)
        .expect("out d1")
        .iter()
        .filter(|e| e.edge_type == "caused_by")
        .map(|e| e.to_id)
        .collect();
    assert!(
        d1_causes.is_empty(),
        "d1 should be a thread root, got caused_by {:?}",
        d1_causes
    );

    // thread_query returns the chain as subgraph views. truncate_subgraph
    // stores the entry separately from `entities`, so the full chain for a
    // view is {entry} ∪ {entities}.
    let views = graph
        .thread_query("chainstep", 3, 3, Some("atheneum"), 1500)
        .expect("thread query");
    assert!(!views.is_empty(), "thread query returned no views");

    let view_entity_sets: Vec<Vec<i64>> = views
        .iter()
        .map(|v| {
            let mut ids: Vec<i64> = std::iter::once(v.entry.id)
                .chain(v.entities.iter().map(|e| e.id))
                .collect();
            ids.sort();
            ids
        })
        .collect();
    let any_full_chain = view_entity_sets.iter().any(|ids| ids == &[d1, d2, d3]);
    assert!(
        any_full_chain,
        "no single view contained the full d1/d2/d3 chain; views: {:?}",
        view_entity_sets
    );

    // The view anchored on d3 walks caused_by backward to d1 and d2; ordering
    // the full entity set (entry + entities) by id yields [d1, d2, d3].
    let d3_view = views
        .iter()
        .find(|v| v.entry.id == d3)
        .expect("a view anchored on d3");
    let mut ordered: Vec<i64> = std::iter::once(d3_view.entry.id)
        .chain(d3_view.entities.iter().map(|e| e.id))
        .collect();
    ordered.sort();
    assert_eq!(ordered, vec![d1, d2, d3], "d3-view chain order by id");
}

#[test]
fn decision_exists_chosen_keys_on_session_target_source_chosen() {
    // Phase 5 dedup guard for the skill / manual `/decision` layer. Unlike
    // `decision_exists` (sequence-keyed, for the transcript watcher), this
    // keys on `(session_id, target, source, chosen)` because a re-fired skill
    // decision has no stable sequence. A repeat of the exact same quadruple
    // is a duplicate; any field differing is a distinct decision. Cross-layer
    // doubles (different `source`) are intentionally NOT collapsed here.
    let dir = tempdir().expect("tempdir");
    let db_path = dir.path().join("atheneum.db");
    let graph = AtheneumGraph::open(&db_path).expect("open graph");

    graph
        .store_discovery(
            "claude",
            "Decision",
            "storage-engine",
            json!({
                "session_id": "sess-skill",
                "source": "skill",
                "chosen": "csr-adjacency",
                "alternatives": ["btree", "skip-list"],
                "rationale": "scan-heavy read path favors CSR",
            }),
        )
        .expect("store skill decision");

    // Exact repeat → duplicate.
    assert!(
        graph
            .decision_exists_chosen("sess-skill", "storage-engine", "skill", "csr-adjacency")
            .expect("exists check"),
        "exact (session,target,source,chosen) must be a duplicate"
    );

    // Same decision captured by a different layer (source=askuser) is NOT
    // collapsed — the plan's documented cross-layer tradeoff.
    assert!(
        !graph
            .decision_exists_chosen("sess-skill", "storage-engine", "askuser", "csr-adjacency")
            .expect("exists check askuser"),
        "different source is a different layer, not a duplicate"
    );

    // Different chosen option for the same target → distinct decision.
    assert!(
        !graph
            .decision_exists_chosen("sess-skill", "storage-engine", "skill", "btree")
            .expect("exists check btree"),
        "a different chosen is a distinct decision, not a duplicate"
    );

    // Different target → distinct decision.
    assert!(
        !graph
            .decision_exists_chosen("sess-skill", "auth-strategy", "skill", "csr-adjacency")
            .expect("exists check other target"),
        "a different target is a distinct decision"
    );

    // Different session → distinct decision (session-scoped).
    assert!(
        !graph
            .decision_exists_chosen("sess-other", "storage-engine", "skill", "csr-adjacency")
            .expect("exists check other session"),
        "a different session is a distinct decision"
    );
}