edda-ask 0.1.0

Cross-source decision query engine for Edda
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
use edda_core::Event;
use edda_ledger::sqlite_store::DecisionRow;
use edda_ledger::Ledger;
use serde::Serialize;

// ── Input type detection ─────────────────────────────────────────────

/// Detected input type for a query string.
#[derive(Debug, PartialEq)]
pub enum InputType {
    /// "db.engine" — contains '.' matching word.word
    ExactKey(String),
    /// "db" — matches a known domain
    Domain(String),
    /// "postgres" — default keyword search
    Keyword(String),
    /// Empty query — show all active decisions
    Overview,
}

/// Classify a query string into one of the four input types.
pub fn detect_input_type(query: &str, known_domains: &[String]) -> InputType {
    let q = query.trim();
    if q.is_empty() {
        return InputType::Overview;
    }
    // Check for exact key pattern: word.word (e.g. "db.engine")
    if q.contains('.') && q.split('.').count() >= 2 && q.split('.').all(|p| !p.is_empty()) {
        return InputType::ExactKey(q.to_string());
    }
    // Check if query matches a known domain (case-insensitive)
    let q_lower = q.to_lowercase();
    if known_domains.iter().any(|d| d.to_lowercase() == q_lower) {
        return InputType::Domain(q.to_string());
    }
    InputType::Keyword(q.to_string())
}

// ── Result types ─────────────────────────────────────────────────────

#[derive(Debug, Serialize)]
pub struct AskResult {
    pub query: String,
    pub input_type: String,
    pub decisions: Vec<DecisionHit>,
    pub timeline: Vec<DecisionHit>,
    pub related_commits: Vec<CommitHit>,
    pub related_notes: Vec<NoteHit>,
    pub conversations: Vec<ConversationHit>,
}

#[derive(Debug, Clone, Serialize)]
pub struct DecisionHit {
    pub event_id: String,
    pub key: String,
    pub value: String,
    pub reason: String,
    pub domain: String,
    pub branch: String,
    pub ts: String,
    pub is_active: bool,
}

#[derive(Debug, Clone, Serialize)]
pub struct CommitHit {
    pub event_id: String,
    pub title: String,
    pub purpose: String,
    pub ts: String,
    pub branch: String,
    pub match_type: String,
}

#[derive(Debug, Clone, Serialize)]
pub struct NoteHit {
    pub event_id: String,
    pub text: String,
    pub ts: String,
    pub branch: String,
}

#[derive(Debug, Clone, Serialize)]
pub struct ConversationHit {
    pub doc_id: String,
    pub session_id: String,
    pub ts: String,
    pub snippet: String,
    pub rank: f64,
}

// ── Options ──────────────────────────────────────────────────────────

pub struct AskOptions {
    pub limit: usize,
    pub include_superseded: bool,
    pub branch: Option<String>,
}

impl Default for AskOptions {
    fn default() -> Self {
        Self {
            limit: 20,
            include_superseded: false,
            branch: None,
        }
    }
}

/// Transcript search callback type.
pub type TranscriptSearchFn = dyn Fn(&str, usize) -> Vec<ConversationHit>;

// ── Core ask function ────────────────────────────────────────────────

pub fn ask(
    ledger: &Ledger,
    query: &str,
    opts: &AskOptions,
    transcript_search: Option<&TranscriptSearchFn>,
) -> anyhow::Result<AskResult> {
    let domains = ledger.list_domains()?;
    let input_type = detect_input_type(query, &domains);

    // Branch filter helper: keep only decisions matching the requested branch
    let branch_filter = |hits: Vec<DecisionHit>| -> Vec<DecisionHit> {
        match &opts.branch {
            Some(b) => hits.into_iter().filter(|d| d.branch == *b).collect(),
            None => hits,
        }
    };

    let (decisions, timeline) = match &input_type {
        InputType::ExactKey(key) => {
            let all = ledger
                .decision_timeline(key)?
                .into_iter()
                .map(|r| to_decision_hit(&r))
                .collect::<Vec<_>>();
            let all = branch_filter(all);
            let active: Vec<DecisionHit> = if opts.include_superseded {
                all.clone()
            } else {
                all.iter().filter(|d| d.is_active).cloned().collect()
            };
            (active, all)
        }
        InputType::Domain(domain) => {
            let active = branch_filter(
                ledger
                    .active_decisions(Some(domain), None)?
                    .into_iter()
                    .map(|r| to_decision_hit(&r))
                    .collect(),
            );
            let tl = branch_filter(
                ledger
                    .domain_timeline(domain)?
                    .into_iter()
                    .map(|r| to_decision_hit(&r))
                    .collect(),
            );
            (active, tl)
        }
        InputType::Keyword(kw) => {
            let mut hits = branch_filter(
                ledger
                    .active_decisions(None, Some(kw))?
                    .into_iter()
                    .map(|r| to_decision_hit(&r))
                    .collect(),
            );
            if opts.include_superseded {
                // Also scan all events for superseded decisions matching keyword
                let events = ledger.iter_events()?;
                let kw_lower = kw.to_lowercase();
                for event in &events {
                    if let Some(ref b) = opts.branch {
                        if event.branch != *b {
                            continue;
                        }
                    }
                    if is_decision_event(event) {
                        let (key, value, reason) = extract_decision_fields(event);
                        if key.to_lowercase().contains(&kw_lower)
                            || value.to_lowercase().contains(&kw_lower)
                            || reason.to_lowercase().contains(&kw_lower)
                        {
                            // Check if already in hits
                            if !hits.iter().any(|h| h.event_id == event.event_id) {
                                hits.push(DecisionHit {
                                    event_id: event.event_id.clone(),
                                    key,
                                    value,
                                    reason,
                                    domain: extract_domain(event),
                                    branch: event.branch.clone(),
                                    ts: event.ts.clone(),
                                    is_active: false,
                                });
                            }
                        }
                    }
                }
            }
            (hits, vec![])
        }
        InputType::Overview => {
            let active = branch_filter(
                ledger
                    .active_decisions(None, None)?
                    .into_iter()
                    .map(|r| to_decision_hit(&r))
                    .collect(),
            );
            (active, vec![])
        }
    };

    // Collect decision event_ids for evidence chain matching
    let decision_event_ids: Vec<&str> = decisions
        .iter()
        .map(|d| d.event_id.as_str())
        .chain(timeline.iter().map(|d| d.event_id.as_str()))
        .collect();

    let q = query.trim();
    // Load events once for both commit and note searches
    let events = ledger.iter_events()?;
    let related_commits =
        find_related_commits(&events, &decision_event_ids, q, opts.limit, &opts.branch);
    let related_notes = find_related_notes(&events, q, opts.limit, &opts.branch);

    let conversations = match transcript_search {
        Some(search_fn) if !q.is_empty() => search_fn(q, opts.limit),
        _ => vec![],
    };

    let input_type_str = match &input_type {
        InputType::ExactKey(_) => "exact_key",
        InputType::Domain(_) => "domain",
        InputType::Keyword(_) => "keyword",
        InputType::Overview => "overview",
    };

    Ok(AskResult {
        query: q.to_string(),
        input_type: input_type_str.to_string(),
        decisions,
        timeline,
        related_commits,
        related_notes,
        conversations,
    })
}

// ── Related event helpers ────────────────────────────────────────────

fn find_related_commits(
    events: &[Event],
    decision_event_ids: &[&str],
    query: &str,
    limit: usize,
    branch: &Option<String>,
) -> Vec<CommitHit> {
    let mut hits: Vec<CommitHit> = Vec::new();
    let q_lower = query.to_lowercase();

    for event in events.iter().rev() {
        if event.event_type != "commit" {
            continue;
        }
        if let Some(b) = branch {
            if event.branch != *b {
                continue;
            }
        }

        let title = event
            .payload
            .get("title")
            .and_then(|v| v.as_str())
            .unwrap_or("");
        let purpose = event
            .payload
            .get("purpose")
            .and_then(|v| v.as_str())
            .unwrap_or("");

        // Check evidence chain: does this commit reference any of our decision events?
        let mut match_type = None;

        // Check refs.events for evidence links
        for ref_id in &event.refs.events {
            if decision_event_ids.contains(&ref_id.as_str()) {
                match_type = Some("evidence");
                break;
            }
        }
        // Also check provenance
        if match_type.is_none() {
            for prov in &event.refs.provenance {
                if decision_event_ids.contains(&prov.target.as_str()) {
                    match_type = Some("evidence");
                    break;
                }
            }
        }

        // Title/purpose keyword match
        if match_type.is_none()
            && !query.is_empty()
            && (title.to_lowercase().contains(&q_lower)
                || purpose.to_lowercase().contains(&q_lower))
        {
            match_type = Some("title");
        }

        if let Some(mt) = match_type {
            // Deduplicate
            if !hits.iter().any(|h| h.event_id == event.event_id) {
                hits.push(CommitHit {
                    event_id: event.event_id.clone(),
                    title: title.to_string(),
                    purpose: purpose.to_string(),
                    ts: event.ts.clone(),
                    branch: event.branch.clone(),
                    match_type: mt.to_string(),
                });
                if hits.len() >= limit {
                    break;
                }
            }
        }
    }

    hits
}

fn find_related_notes(
    events: &[Event],
    query: &str,
    limit: usize,
    branch: &Option<String>,
) -> Vec<NoteHit> {
    if query.is_empty() {
        return vec![];
    }

    let q_lower = query.to_lowercase();
    let mut hits: Vec<NoteHit> = Vec::new();

    for event in events.iter().rev() {
        if event.event_type != "note" {
            continue;
        }
        if let Some(b) = branch {
            if event.branch != *b {
                continue;
            }
        }
        // Skip decision notes — those are already in decisions section
        if is_decision_event(event) {
            continue;
        }

        let text = event
            .payload
            .get("text")
            .and_then(|v| v.as_str())
            .unwrap_or("");

        if text.to_lowercase().contains(&q_lower) {
            hits.push(NoteHit {
                event_id: event.event_id.clone(),
                text: text.to_string(),
                ts: event.ts.clone(),
                branch: event.branch.clone(),
            });
            if hits.len() >= limit {
                break;
            }
        }
    }

    hits
}

// ── Human-readable formatting ────────────────────────────────────────

pub fn format_human(result: &AskResult) -> String {
    let mut out = String::new();

    if !result.decisions.is_empty() {
        out.push_str("── Decisions ──────────────────────────\n");
        for d in &result.decisions {
            let status = if d.is_active { "active" } else { "superseded" };
            out.push_str(&format!(
                "  {} = {}{}\n  branch: {} | {} | {}\n\n",
                d.key, d.value, d.reason, d.branch, d.ts, status
            ));
        }
    }

    if !result.timeline.is_empty() {
        out.push_str("── Timeline ───────────────────────────\n");
        for d in &result.timeline {
            let status = if d.is_active { "active" } else { "superseded" };
            out.push_str(&format!(
                "  {}  {} = {}  ({})\n",
                d.ts, d.key, d.value, status
            ));
        }
        out.push('\n');
    }

    if !result.related_commits.is_empty() {
        out.push_str("── Related Commits ────────────────────\n");
        for c in &result.related_commits {
            out.push_str(&format!(
                "  {} ({}, {})\n  match: {}\n\n",
                c.title, c.ts, c.branch, c.match_type
            ));
        }
    }

    if !result.related_notes.is_empty() {
        out.push_str("── Related Notes ──────────────────────\n");
        for n in &result.related_notes {
            out.push_str(&format!("  \"{}\" ({}, {})\n\n", n.text, n.ts, n.branch));
        }
    }

    if !result.conversations.is_empty() {
        out.push_str("── Conversations ──────────────────────\n");
        for c in &result.conversations {
            out.push_str(&format!(
                "  [{}] {}\n  rank: {:.2}\n\n",
                c.session_id, c.snippet, c.rank
            ));
        }
    }

    if out.is_empty() {
        out.push_str("No results found.\n");
    }

    out
}

// ── Internal helpers ─────────────────────────────────────────────────

fn to_decision_hit(row: &DecisionRow) -> DecisionHit {
    DecisionHit {
        event_id: row.event_id.clone(),
        key: row.key.clone(),
        value: row.value.clone(),
        reason: row.reason.clone(),
        domain: row.domain.clone(),
        branch: row.branch.clone(),
        ts: row.ts.clone().unwrap_or_default(),
        is_active: row.is_active,
    }
}

fn is_decision_event(event: &Event) -> bool {
    event.event_type == "note"
        && event
            .payload
            .get("tags")
            .and_then(|v| v.as_array())
            .map(|arr| arr.iter().any(|t| t.as_str() == Some("decision")))
            .unwrap_or(false)
}

fn extract_decision_fields(event: &Event) -> (String, String, String) {
    if let Some(d) = event.payload.get("decision") {
        let key = d
            .get("key")
            .and_then(|v| v.as_str())
            .unwrap_or("")
            .to_string();
        let value = d
            .get("value")
            .and_then(|v| v.as_str())
            .unwrap_or("")
            .to_string();
        let reason = d
            .get("reason")
            .and_then(|v| v.as_str())
            .unwrap_or("")
            .to_string();
        return (key, value, reason);
    }
    let text = event
        .payload
        .get("text")
        .and_then(|v| v.as_str())
        .unwrap_or("");
    let (key, rest) = match text.split_once(": ") {
        Some((k, r)) => (k.to_string(), r),
        None => return (String::new(), text.to_string(), String::new()),
    };
    let (value, reason) = match rest.split_once("") {
        Some((v, r)) => (v.to_string(), r.to_string()),
        None => (rest.to_string(), String::new()),
    };
    (key, value, reason)
}

fn extract_domain(event: &Event) -> String {
    if let Some(d) = event.payload.get("decision") {
        if let Some(key) = d.get("key").and_then(|v| v.as_str()) {
            return key.split('.').next().unwrap_or(key).to_string();
        }
    }
    String::new()
}

// ── Tests ────────────────────────────────────────────────────────────

#[cfg(test)]
mod tests {
    use super::*;
    use edda_core::event::{finalize_event, new_note_event};
    use edda_core::Provenance;
    use edda_ledger::ledger::{init_branches_json, init_head, init_workspace};
    use edda_ledger::paths::EddaPaths;
    use std::sync::atomic::{AtomicU64, Ordering};

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

    fn setup() -> (std::path::PathBuf, Ledger) {
        let n = TEST_COUNTER.fetch_add(1, Ordering::SeqCst);
        let tmp = std::env::temp_dir().join(format!("edda_ask_test_{}_{n}", std::process::id()));
        let _ = std::fs::remove_dir_all(&tmp);
        let paths = EddaPaths::discover(&tmp);
        init_workspace(&paths).unwrap();
        init_head(&paths, "main").unwrap();
        init_branches_json(&paths, "main").unwrap();
        let ledger = Ledger::open(&tmp).unwrap();
        (tmp, ledger)
    }

    fn make_decision(
        branch: &str,
        key: &str,
        value: &str,
        reason: Option<&str>,
        supersedes: Option<&str>,
    ) -> Event {
        let text = match reason {
            Some(r) => format!("{key}: {value}{r}"),
            None => format!("{key}: {value}"),
        };
        let tags = vec!["decision".to_string()];
        let mut event = new_note_event(branch, None, "system", &text, &tags).unwrap();
        let decision_obj = match reason {
            Some(r) => serde_json::json!({"key": key, "value": value, "reason": r}),
            None => serde_json::json!({"key": key, "value": value}),
        };
        event.payload["decision"] = decision_obj;
        if let Some(target) = supersedes {
            event.refs.provenance.push(Provenance {
                target: target.to_string(),
                rel: "supersedes".to_string(),
                note: Some(format!("key '{key}' re-decided")),
            });
        }
        finalize_event(&mut event);
        event
    }

    fn make_commit(branch: &str, title: &str, purpose: &str, evidence: &[&str]) -> Event {
        use edda_core::event::finalize_event;
        let payload = serde_json::json!({
            "title": title,
            "purpose": purpose,
            "sha": "abc123",
        });
        let mut event = Event {
            event_id: format!("evt_{}", ulid::Ulid::new().to_string().to_lowercase()),
            ts: time_now(),
            event_type: "commit".to_string(),
            branch: branch.to_string(),
            parent_hash: None,
            hash: String::new(),
            payload,
            refs: edda_core::Refs {
                events: evidence.iter().map(|s| s.to_string()).collect(),
                ..Default::default()
            },
            schema_version: 1,
            digests: vec![],
            event_family: None,
            event_level: None,
        };
        finalize_event(&mut event);
        event
    }

    fn make_note(branch: &str, text: &str) -> Event {
        new_note_event(branch, None, "user", text, &[]).unwrap()
    }

    fn time_now() -> String {
        let now = time::OffsetDateTime::now_utc();
        now.format(&time::format_description::well_known::Rfc3339)
            .expect("rfc3339")
    }

    // ── detect_input_type tests ──────────────────────────────────────

    #[test]
    fn detect_exact_key() {
        let domains = vec!["db".into(), "auth".into()];
        assert_eq!(
            detect_input_type("db.engine", &domains),
            InputType::ExactKey("db.engine".into())
        );
    }

    #[test]
    fn detect_domain() {
        let domains = vec!["db".into(), "auth".into()];
        assert_eq!(
            detect_input_type("db", &domains),
            InputType::Domain("db".into())
        );
    }

    #[test]
    fn detect_domain_case_insensitive() {
        let domains = vec!["db".into()];
        assert_eq!(
            detect_input_type("DB", &domains),
            InputType::Domain("DB".into())
        );
    }

    #[test]
    fn detect_keyword() {
        let domains = vec!["db".into()];
        assert_eq!(
            detect_input_type("postgres", &domains),
            InputType::Keyword("postgres".into())
        );
    }

    #[test]
    fn detect_overview() {
        assert_eq!(detect_input_type("", &[]), InputType::Overview);
        assert_eq!(detect_input_type("  ", &[]), InputType::Overview);
    }

    // ── ask() tests ──────────────────────────────────────────────────

    #[test]
    fn ask_exact_key() {
        let (tmp, ledger) = setup();
        let d1 = make_decision("main", "db.engine", "sqlite", Some("MVP"), None);
        let d1_id = d1.event_id.clone();
        ledger.append_event(&d1).unwrap();

        let d2 = make_decision("main", "db.engine", "postgres", Some("JSONB"), Some(&d1_id));
        ledger.append_event(&d2).unwrap();

        let result = ask(&ledger, "db.engine", &AskOptions::default(), None).unwrap();
        assert_eq!(result.input_type, "exact_key");
        // decisions = only active (default)
        assert_eq!(result.decisions.len(), 1);
        assert_eq!(result.decisions[0].value, "postgres");
        // timeline = both
        assert_eq!(result.timeline.len(), 2);

        let _ = std::fs::remove_dir_all(&tmp);
    }

    #[test]
    fn ask_domain() {
        let (tmp, ledger) = setup();
        ledger
            .append_event(&make_decision("main", "db.engine", "postgres", None, None))
            .unwrap();
        ledger
            .append_event(&make_decision("main", "db.pool", "10", None, None))
            .unwrap();
        ledger
            .append_event(&make_decision("main", "auth.method", "JWT", None, None))
            .unwrap();

        let result = ask(&ledger, "db", &AskOptions::default(), None).unwrap();
        assert_eq!(result.input_type, "domain");
        assert_eq!(result.decisions.len(), 2);
        assert!(result.timeline.len() >= 2);

        let _ = std::fs::remove_dir_all(&tmp);
    }

    #[test]
    fn ask_keyword() {
        let (tmp, ledger) = setup();
        ledger
            .append_event(&make_decision(
                "main",
                "db.engine",
                "postgres",
                Some("JSONB"),
                None,
            ))
            .unwrap();
        ledger
            .append_event(&make_decision("main", "auth.method", "JWT", None, None))
            .unwrap();

        let result = ask(&ledger, "postgres", &AskOptions::default(), None).unwrap();
        assert_eq!(result.input_type, "keyword");
        assert_eq!(result.decisions.len(), 1);
        assert_eq!(result.decisions[0].key, "db.engine");

        let _ = std::fs::remove_dir_all(&tmp);
    }

    #[test]
    fn ask_overview() {
        let (tmp, ledger) = setup();
        ledger
            .append_event(&make_decision("main", "db.engine", "postgres", None, None))
            .unwrap();
        ledger
            .append_event(&make_decision("main", "auth.method", "JWT", None, None))
            .unwrap();

        let result = ask(&ledger, "", &AskOptions::default(), None).unwrap();
        assert_eq!(result.input_type, "overview");
        assert_eq!(result.decisions.len(), 2);

        let _ = std::fs::remove_dir_all(&tmp);
    }

    #[test]
    fn ask_with_transcript_callback() {
        let (tmp, ledger) = setup();
        ledger
            .append_event(&make_decision("main", "db.engine", "postgres", None, None))
            .unwrap();

        let callback = |_query: &str, _limit: usize| -> Vec<ConversationHit> {
            vec![ConversationHit {
                doc_id: "t1".into(),
                session_id: "s1".into(),
                ts: "2026-02-14T10:00:00Z".into(),
                snippet: "discussed postgres JSONB".into(),
                rank: 5.0,
            }]
        };

        let result = ask(&ledger, "postgres", &AskOptions::default(), Some(&callback)).unwrap();
        assert_eq!(result.conversations.len(), 1);
        assert_eq!(result.conversations[0].snippet, "discussed postgres JSONB");

        let _ = std::fs::remove_dir_all(&tmp);
    }

    #[test]
    fn ask_without_transcript_callback() {
        let (tmp, ledger) = setup();
        ledger
            .append_event(&make_decision("main", "db.engine", "postgres", None, None))
            .unwrap();

        let result = ask(&ledger, "postgres", &AskOptions::default(), None).unwrap();
        assert!(result.conversations.is_empty());

        let _ = std::fs::remove_dir_all(&tmp);
    }

    // ── Related events tests ─────────────────────────────────────────

    #[test]
    fn find_related_commits_evidence_chain() {
        let (tmp, ledger) = setup();
        let d1 = make_decision("main", "db.engine", "postgres", Some("JSONB"), None);
        let d1_id = d1.event_id.clone();
        ledger.append_event(&d1).unwrap();

        let c1 = make_commit(
            "main",
            "feat: migrate to postgres",
            "db migration",
            &[&d1_id],
        );
        ledger.append_event(&c1).unwrap();

        let result = ask(&ledger, "db.engine", &AskOptions::default(), None).unwrap();
        assert_eq!(result.related_commits.len(), 1);
        assert_eq!(result.related_commits[0].match_type, "evidence");

        let _ = std::fs::remove_dir_all(&tmp);
    }

    #[test]
    fn find_related_commits_title_match() {
        let (tmp, ledger) = setup();
        ledger
            .append_event(&make_decision("main", "db.engine", "postgres", None, None))
            .unwrap();

        // Commit not linked via evidence, but title matches
        let c1 = make_commit("main", "fix: postgres connection pool", "pool fix", &[]);
        ledger.append_event(&c1).unwrap();

        let result = ask(&ledger, "postgres", &AskOptions::default(), None).unwrap();
        assert!(result
            .related_commits
            .iter()
            .any(|c| c.match_type == "title"));

        let _ = std::fs::remove_dir_all(&tmp);
    }

    #[test]
    fn find_related_notes_text_match() {
        let (tmp, ledger) = setup();
        ledger
            .append_event(&make_decision("main", "db.engine", "postgres", None, None))
            .unwrap();

        // Regular note (not a decision)
        ledger
            .append_event(&make_note(
                "main",
                "discussed mysql but rejected for licensing",
            ))
            .unwrap();

        let result = ask(&ledger, "mysql", &AskOptions::default(), None).unwrap();
        assert_eq!(result.related_notes.len(), 1);
        assert!(result.related_notes[0].text.contains("mysql"));

        let _ = std::fs::remove_dir_all(&tmp);
    }

    #[test]
    fn find_related_notes_excludes_decision_notes() {
        let (tmp, ledger) = setup();
        ledger
            .append_event(&make_decision(
                "main",
                "db.engine",
                "postgres",
                Some("JSONB"),
                None,
            ))
            .unwrap();

        let result = ask(&ledger, "postgres", &AskOptions::default(), None).unwrap();
        // Decision note should NOT appear in related_notes
        assert!(result.related_notes.is_empty());

        let _ = std::fs::remove_dir_all(&tmp);
    }

    #[test]
    fn ask_branch_filter() {
        let (tmp, ledger) = setup();
        ledger
            .append_event(&make_decision(
                "main",
                "db.engine",
                "postgres",
                Some("prod"),
                None,
            ))
            .unwrap();
        ledger
            .append_event(&make_decision(
                "dev",
                "db.engine",
                "sqlite",
                Some("dev speed"),
                None,
            ))
            .unwrap();

        // No branch filter → both
        let result = ask(&ledger, "", &AskOptions::default(), None).unwrap();
        assert_eq!(result.decisions.len(), 2);

        // Filter to dev → only sqlite
        let opts = AskOptions {
            branch: Some("dev".into()),
            ..Default::default()
        };
        let result = ask(&ledger, "", &opts, None).unwrap();
        assert_eq!(result.decisions.len(), 1);
        assert_eq!(result.decisions[0].value, "sqlite");

        let _ = std::fs::remove_dir_all(&tmp);
    }

    #[test]
    fn format_human_contains_sections() {
        let result = AskResult {
            query: "postgres".into(),
            input_type: "keyword".into(),
            decisions: vec![DecisionHit {
                event_id: "e1".into(),
                key: "db.engine".into(),
                value: "postgres".into(),
                reason: "JSONB".into(),
                domain: "db".into(),
                branch: "main".into(),
                ts: "2026-02-15".into(),
                is_active: true,
            }],
            timeline: vec![],
            related_commits: vec![CommitHit {
                event_id: "c1".into(),
                title: "feat: migrate".into(),
                purpose: "migration".into(),
                ts: "2026-02-15".into(),
                branch: "main".into(),
                match_type: "evidence".into(),
            }],
            related_notes: vec![],
            conversations: vec![],
        };

        let output = format_human(&result);
        assert!(output.contains("Decisions"));
        assert!(output.contains("postgres"));
        assert!(output.contains("Related Commits"));
        assert!(output.contains("feat: migrate"));
    }

    #[test]
    fn format_human_empty_result() {
        let result = AskResult {
            query: "nonexistent".into(),
            input_type: "keyword".into(),
            decisions: vec![],
            timeline: vec![],
            related_commits: vec![],
            related_notes: vec![],
            conversations: vec![],
        };

        let output = format_human(&result);
        assert!(output.contains("No results found"));
    }
}