edda-derive 0.2.0

View rebuilding and tiered history 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
mod helpers;
mod session;

use anyhow::Result;
use edda_ledger::Ledger;
use std::collections::{BTreeMap, HashSet};

use crate::snapshot::build_branch_snapshot;
use crate::types::*;

use helpers::cmd_base_key;
use session::render_session_history;

pub fn render_context(ledger: &Ledger, branch: &str, opt: DeriveOptions) -> Result<String> {
    let snap = build_branch_snapshot(ledger, branch)?;
    let n = opt.depth.max(1);

    let commits: Vec<_> = snap.commits.iter().rev().take(n).collect::<Vec<_>>();
    let commits: Vec<_> = commits.into_iter().rev().collect();

    // Filter signals to last 2 hours to avoid showing stale errors
    let sig_cutoff = {
        let now = time::OffsetDateTime::now_utc();
        let cutoff = now - time::Duration::hours(2);
        cutoff
            .format(&time::format_description::well_known::Rfc3339)
            .unwrap_or_default()
    };
    let recent_sigs: Vec<_> = snap
        .signals
        .iter()
        .filter(|s| s.ts.as_str() >= sig_cutoff.as_str())
        .collect();
    let sigs: Vec<_> = recent_sigs
        .iter()
        .rev()
        .take(n)
        .copied()
        .collect::<Vec<_>>();
    let sigs: Vec<_> = sigs.into_iter().rev().collect();

    let head = ledger.head_branch().unwrap_or_else(|_| "main".to_string());

    let mut out = String::new();
    out.push_str("# CONTEXT SNAPSHOT\n\n");

    out.push_str("## Project (main)\n");
    out.push_str(&format!("- head: {head}\n"));
    out.push_str(&format!("- branch: {}\n", snap.branch));
    if let Some(c) = &snap.last_commit {
        out.push_str(&format!(
            "- uncommitted_events: {}\n",
            snap.uncommitted_events
        ));
        out.push_str(&format!(
            "- last_commit: {} {} \"{}\"\n",
            c.ts, c.event_id, c.title
        ));
    } else if snap.uncommitted_events > 0 {
        // No edda commits — show event count without misleading "uncommitted" framing
        out.push_str(&format!("- events: {}\n", snap.uncommitted_events));
    }
    // Session count and date span from digests
    if !snap.session_digests.is_empty() {
        let count = snap.session_digests.len();
        let dates: Vec<&str> = snap
            .session_digests
            .iter()
            .filter_map(|d| d.ts.get(..10))
            .collect();
        if count == 1 {
            if let Some(date) = dates.first() {
                out.push_str(&format!("- sessions: 1 ({date})\n"));
            }
        } else if let (Some(oldest), Some(newest)) = (dates.first(), dates.last()) {
            out.push_str(&format!("- sessions: {count} ({oldest}{newest})\n"));
        }
    }
    out.push('\n');

    out.push_str("## Branch\n");
    out.push_str(&format!("- name: {}\n\n", snap.branch));

    // Tiered session history rendering
    let session_history = render_session_history(&snap.session_digests);
    if !session_history.is_empty() {
        out.push_str(&session_history);
    }

    out.push_str(&format!("## Recent Commits (last {n})\n"));
    if commits.is_empty() {
        out.push_str("- (none)\n\n");
    } else {
        for (i, c) in commits.iter().enumerate() {
            out.push_str(&format!(
                "{}. {} {} ({})\n",
                i + 1,
                c.ts,
                c.title,
                c.event_id
            ));
            out.push_str(&format!(
                "   - contribution: {}\n",
                if c.contribution.is_empty() {
                    "(empty)"
                } else {
                    &c.contribution
                }
            ));
            if c.evidence_lines.is_empty() {
                out.push_str("   - evidence: (none)\n");
            } else {
                out.push_str(&format!("   - evidence: {}\n", c.evidence_lines.join(", ")));
            }
        }
        out.push('\n');
    }

    let merge_list: Vec<_> = snap.merges.iter().rev().take(n).collect::<Vec<_>>();
    let merge_list: Vec<_> = merge_list.into_iter().rev().collect();

    out.push_str(&format!("## Recent Merges (last {n})\n"));
    if merge_list.is_empty() {
        out.push_str("- (none)\n\n");
    } else {
        for m in merge_list {
            out.push_str(&format!(
                "- {} {} {}->{} adopted={} reason=\"{}\"\n",
                m.ts,
                m.event_id,
                m.src,
                m.dst,
                m.adopted_commits.len(),
                m.reason
            ));
        }
        out.push('\n');
    }

    // Decisions — no time cutoff (decisions are long-lived)
    // Build superseded set: any event targeted by a "supersedes" provenance link is inactive
    let all_decisions: Vec<_> = snap
        .signals
        .iter()
        .filter(|s| matches!(s.kind, SignalKind::NoteDecision))
        .collect();
    let superseded: HashSet<&str> = all_decisions
        .iter()
        .filter_map(|d| d.supersedes.as_deref())
        .collect();
    let active_decisions: Vec<_> = all_decisions
        .iter()
        .filter(|d| !superseded.contains(d.event_id.as_str()))
        .rev()
        .take(n.max(5))
        .copied()
        .collect::<Vec<_>>();
    let active_decisions: Vec<_> = active_decisions.into_iter().rev().collect();

    if !active_decisions.is_empty() {
        out.push_str(&format!("## Decisions (last {})\n", active_decisions.len()));
        for d in &active_decisions {
            out.push_str(&format!("- {} ({})\n", d.text, d.event_id));
        }
        out.push('\n');
    }

    out.push_str(&format!("## Recent Signals (last {n})\n"));
    // Filter out decisions from signals (they have their own section)
    let non_decision_sigs: Vec<_> = sigs
        .iter()
        .filter(|s| !matches!(s.kind, SignalKind::NoteDecision))
        .collect();
    if non_decision_sigs.is_empty() {
        out.push_str("- (none)\n\n");
    } else {
        // Aggregate CmdFail signals by command base; keep NoteTodo as-is
        let mut cmd_groups: BTreeMap<String, Vec<&SignalEntry>> = BTreeMap::new();
        let mut todos: Vec<&SignalEntry> = Vec::new();

        for s in &non_decision_sigs {
            match s.kind {
                SignalKind::NoteTodo => todos.push(s),
                SignalKind::CmdFail => {
                    let key = cmd_base_key(&s.text);
                    cmd_groups.entry(key).or_default().push(s);
                }
                SignalKind::NoteDecision => {} // handled above
            }
        }

        for s in &todos {
            out.push_str(&format!("- NOTE(todo): {} ({})\n", s.text, s.event_id));
        }
        for (base, group) in &cmd_groups {
            if group.len() == 1 {
                out.push_str(&format!(
                    "- CMD fail: {} ({})\n",
                    group[0].text, group[0].event_id
                ));
            } else {
                out.push_str(&format!("- CMD fail: {} ({}x)\n", base, group.len(),));
            }
        }
        out.push('\n');
    }

    out.push_str("## How to cite evidence\n");
    out.push_str("- Use event_id to locate raw trace in .edda/ledger/events.jsonl\n");
    out.push_str("- Use blob:sha256:* to open stdout/stderr artifacts in .edda/ledger/blobs/\n");

    Ok(out)
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::test_support::setup_workspace;
    use edda_core::event::{
        new_cmd_event, new_commit_event, new_note_event, CmdEventParams, CommitEventParams,
    };

    use super::session::digest_helpers::*;

    #[test]
    fn render_context_includes_commits_and_signals() {
        let (tmp, ledger) = setup_workspace();

        // Add a todo note (becomes a signal)
        let todo_tags = vec!["todo".to_string()];
        let note = new_note_event("main", None, "user", "fix the bug", &todo_tags).unwrap();
        ledger.append_event(&note).unwrap();

        // Add a commit
        let mut params = CommitEventParams {
            branch: "main",
            parent_hash: None,
            title: "implement feature X",
            purpose: Some("deliver value"),
            prev_summary: "",
            contribution: "new feature",
            evidence: vec![],
            labels: vec![],
        };
        let commit = new_commit_event(&mut params).unwrap();
        ledger.append_event(&commit).unwrap();

        let ctx = render_context(&ledger, "main", DeriveOptions::default()).unwrap();

        assert!(ctx.contains("CONTEXT SNAPSHOT"));
        assert!(ctx.contains("main"));
        assert!(ctx.contains("implement feature X"));

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

    #[test]
    fn session_digest_surfaced_in_render_context() {
        let (tmp, ledger) = setup_workspace();

        // Write a session_digest note to the workspace ledger
        let digest = make_digest_note(
            "main",
            "sess-abc1",
            15,
            &["/src/main.rs", "/src/lib.rs"],
            &["fix: UTF-8 truncation", "feat: add digest"],
            &["cargo check -p edda-mcp"],
            45,
        );
        ledger.append_event(&digest).unwrap();

        let ctx = render_context(&ledger, "main", DeriveOptions::default()).unwrap();

        // Should contain Session History section
        assert!(
            ctx.contains("## Session History"),
            "missing Session History section in:\n{ctx}"
        );
        assert!(ctx.contains("sess-abc"), "missing session id in:\n{ctx}");
        assert!(ctx.contains("45 min"), "missing duration in:\n{ctx}");
        assert!(
            ctx.contains("15 tool calls"),
            "missing tool calls in:\n{ctx}"
        );
        assert!(ctx.contains("main.rs"), "missing file name in:\n{ctx}");
        assert!(ctx.contains("lib.rs"), "missing file name in:\n{ctx}");
        assert!(
            ctx.contains("fix: UTF-8 truncation"),
            "missing commit msg in:\n{ctx}"
        );
        assert!(
            ctx.contains("cargo check -p edda-mcp"),
            "missing failed cmd in:\n{ctx}"
        );

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

    #[test]
    fn no_digests_no_section() {
        let (tmp, ledger) = setup_workspace();

        // Only a regular note, no session_digest
        let note = new_note_event("main", None, "user", "hello", &[]).unwrap();
        ledger.append_event(&note).unwrap();

        let ctx = render_context(&ledger, "main", DeriveOptions::default()).unwrap();

        assert!(
            !ctx.contains("Session History"),
            "should not show Session History when none exist"
        );

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

    #[test]
    fn render_context_no_commits_shows_events_not_uncommitted() {
        let (tmp, ledger) = setup_workspace();

        // Add notes without any commit
        let n1 = new_note_event("main", None, "user", "note 1", &[]).unwrap();
        let n2 = new_note_event("main", None, "user", "note 2", &[]).unwrap();
        ledger.append_event(&n1).unwrap();
        ledger.append_event(&n2).unwrap();

        let ctx = render_context(&ledger, "main", DeriveOptions::default()).unwrap();

        // Should show "events: 2" instead of "uncommitted_events: 2"
        assert!(
            ctx.contains("- events: 2"),
            "expected '- events: 2' in:\n{ctx}"
        );
        assert!(
            !ctx.contains("uncommitted_events"),
            "should not contain 'uncommitted_events'"
        );
        assert!(
            !ctx.contains("last_commit: (none)"),
            "should not contain 'last_commit: (none)'"
        );

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

    #[test]
    fn signals_aggregate_repeated_cmd_fails() {
        let (tmp, ledger) = setup_workspace();

        // Add 3 "cargo check" failures and 1 "cargo test" failure
        for _ in 0..3 {
            let argv = vec![
                "cargo".to_string(),
                "check".to_string(),
                "-p".to_string(),
                "edda-mcp".to_string(),
            ];
            let cmd = new_cmd_event(&CmdEventParams {
                branch: "main",
                parent_hash: None,
                argv: &argv,
                cwd: ".",
                exit_code: 1,
                duration_ms: 500,
                stdout_blob: "",
                stderr_blob: "",
            })
            .unwrap();
            ledger.append_event(&cmd).unwrap();
        }
        let argv2 = vec!["cargo".to_string(), "test".to_string()];
        let cmd2 = new_cmd_event(&CmdEventParams {
            branch: "main",
            parent_hash: None,
            argv: &argv2,
            cwd: ".",
            exit_code: 1,
            duration_ms: 200,
            stdout_blob: "",
            stderr_blob: "",
        })
        .unwrap();
        ledger.append_event(&cmd2).unwrap();

        let ctx = render_context(&ledger, "main", DeriveOptions::default()).unwrap();

        // "cargo check" should be aggregated as 3x
        assert!(
            ctx.contains("cargo check (3x)"),
            "expected aggregated 'cargo check (3x)' in:\n{ctx}"
        );
        // "cargo test" should appear individually (only 1)
        assert!(
            ctx.contains("cargo test (exit=1)"),
            "expected individual 'cargo test' in:\n{ctx}"
        );

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

    #[test]
    fn signals_single_cmd_not_aggregated() {
        let (tmp, ledger) = setup_workspace();

        let argv = vec!["npm".to_string(), "install".to_string()];
        let cmd = new_cmd_event(&CmdEventParams {
            branch: "main",
            parent_hash: None,
            argv: &argv,
            cwd: ".",
            exit_code: 127,
            duration_ms: 100,
            stdout_blob: "",
            stderr_blob: "",
        })
        .unwrap();
        ledger.append_event(&cmd).unwrap();

        let ctx = render_context(&ledger, "main", DeriveOptions::default()).unwrap();

        // Single failure — show full text, not aggregated
        assert!(
            ctx.contains("CMD fail: npm install (exit=127)"),
            "expected individual signal in:\n{ctx}"
        );
        assert!(
            !ctx.contains("(1x)"),
            "should not show count for single failure"
        );

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

    #[test]
    fn decision_notes_rendered_in_own_section() {
        let (tmp, ledger) = setup_workspace();

        let tags = vec!["decision".to_string()];
        let d1 = new_note_event(
            "main",
            None,
            "user",
            "Use PostgreSQL for concurrent writes",
            &tags,
        )
        .unwrap();
        let d2 = new_note_event(
            "main",
            None,
            "user",
            "REST over GraphQL for simplicity",
            &tags,
        )
        .unwrap();
        ledger.append_event(&d1).unwrap();
        ledger.append_event(&d2).unwrap();

        let ctx = render_context(&ledger, "main", DeriveOptions::default()).unwrap();

        // Decisions section should exist
        assert!(
            ctx.contains("## Decisions"),
            "missing Decisions section in:\n{ctx}"
        );
        assert!(
            ctx.contains("Use PostgreSQL"),
            "missing decision text in:\n{ctx}"
        );
        assert!(
            ctx.contains("REST over GraphQL"),
            "missing decision text in:\n{ctx}"
        );

        // Decisions should NOT appear in Recent Signals
        let signals_section = ctx.split("## Recent Signals").nth(1).unwrap_or("");
        assert!(
            !signals_section.contains("PostgreSQL"),
            "decision leaked into Signals in:\n{ctx}"
        );

        // Decisions section should come before Recent Signals
        let dec_pos = ctx.find("## Decisions").unwrap();
        let sig_pos = ctx.find("## Recent Signals").unwrap();
        assert!(dec_pos < sig_pos, "Decisions should come before Signals");

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

    #[test]
    fn decision_notes_no_time_cutoff() {
        let (tmp, ledger) = setup_workspace();

        // Create a decision note with an old timestamp (> 2 hours ago)
        use edda_core::event::finalize_event;
        use edda_core::types::SCHEMA_VERSION;

        let payload = serde_json::json!({
            "role": "user",
            "text": "JWT for auth tokens",
            "tags": ["decision"]
        });
        let mut event = edda_core::Event {
            event_id: "evt_old_decision".to_string(),
            ts: "2020-01-01T00:00:00Z".to_string(), // very old
            event_type: "note".to_string(),
            branch: "main".to_string(),
            parent_hash: None,
            hash: String::new(),
            payload,
            refs: Default::default(),
            schema_version: SCHEMA_VERSION,
            digests: Vec::new(),
            event_family: None,
            event_level: None,
        };
        finalize_event(&mut event).unwrap();
        ledger.append_event(&event).unwrap();

        let ctx = render_context(&ledger, "main", DeriveOptions::default()).unwrap();

        // Old decision should still appear (no 2hr cutoff)
        assert!(
            ctx.contains("JWT for auth tokens"),
            "old decision should survive cutoff in:\n{ctx}"
        );
        assert!(
            ctx.contains("## Decisions"),
            "Decisions section missing in:\n{ctx}"
        );

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

    #[test]
    fn no_decisions_no_section() {
        let (tmp, ledger) = setup_workspace();

        // Only a todo note, no decision
        let tags = vec!["todo".to_string()];
        let note = new_note_event("main", None, "user", "fix bug", &tags).unwrap();
        ledger.append_event(&note).unwrap();

        let ctx = render_context(&ledger, "main", DeriveOptions::default()).unwrap();

        assert!(
            !ctx.contains("## Decisions"),
            "should not have Decisions section when none exist"
        );

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

    #[test]
    fn superseded_decision_hidden_in_context() {
        use edda_core::event::finalize_event;
        use edda_core::types::{Provenance, SCHEMA_VERSION};

        let (tmp, ledger) = setup_workspace();

        // First decision: db = mysql
        let tags = vec!["decision".to_string()];
        let d1 = new_note_event("main", None, "system", "db: mysql", &tags).unwrap();
        let d1_id = d1.event_id.clone();
        ledger.append_event(&d1).unwrap();

        // Second decision: db = postgres, supersedes d1
        let payload = serde_json::json!({
            "role": "system",
            "text": "db: postgres — need JSONB",
            "tags": ["decision"],
            "decision": {"key": "db", "value": "postgres", "reason": "need JSONB"}
        });
        let mut d2 = edda_core::Event {
            event_id: "evt_d2".to_string(),
            ts: "2026-02-17T12:00:00Z".to_string(),
            event_type: "note".to_string(),
            branch: "main".to_string(),
            parent_hash: None,
            hash: String::new(),
            payload,
            refs: edda_core::types::Refs {
                provenance: vec![Provenance {
                    target: d1_id.clone(),
                    rel: "supersedes".to_string(),
                    note: Some("key 'db' re-decided".to_string()),
                }],
                ..Default::default()
            },
            schema_version: SCHEMA_VERSION,
            digests: Vec::new(),
            event_family: None,
            event_level: None,
        };
        finalize_event(&mut d2).unwrap();
        ledger.append_event(&d2).unwrap();

        let ctx = render_context(&ledger, "main", DeriveOptions::default()).unwrap();

        // Only d2 (postgres) should appear, not d1 (mysql)
        assert!(
            ctx.contains("postgres"),
            "active decision missing in:\n{ctx}"
        );
        assert!(
            !ctx.contains("mysql"),
            "superseded decision should be hidden in:\n{ctx}"
        );

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

    #[test]
    fn supersession_chain_resolves() {
        use edda_core::event::finalize_event;
        use edda_core::types::Provenance;

        let (tmp, ledger) = setup_workspace();
        let tags = vec!["decision".to_string()];

        // A: db = sqlite
        let a = new_note_event("main", None, "system", "db: sqlite", &tags).unwrap();
        let a_id = a.event_id.clone();
        ledger.append_event(&a).unwrap();

        // B: db = mysql, supersedes A
        let mut b = new_note_event("main", None, "system", "db: mysql", &tags).unwrap();
        let b_id = b.event_id.clone();
        b.refs.provenance.push(Provenance {
            target: a_id,
            rel: "supersedes".to_string(),
            note: None,
        });
        finalize_event(&mut b).unwrap();
        ledger.append_event(&b).unwrap();

        // C: db = postgres, supersedes B
        let mut c = new_note_event("main", None, "system", "db: postgres", &tags).unwrap();
        c.refs.provenance.push(Provenance {
            target: b_id,
            rel: "supersedes".to_string(),
            note: None,
        });
        finalize_event(&mut c).unwrap();
        ledger.append_event(&c).unwrap();

        let ctx = render_context(&ledger, "main", DeriveOptions::default()).unwrap();

        assert!(
            ctx.contains("postgres"),
            "final decision missing in:\n{ctx}"
        );
        assert!(
            !ctx.contains("mysql"),
            "superseded B should be hidden in:\n{ctx}"
        );
        assert!(
            !ctx.contains("sqlite"),
            "superseded A should be hidden in:\n{ctx}"
        );

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

    #[test]
    fn old_decision_without_structured_fields_still_renders() {
        let (tmp, ledger) = setup_workspace();

        // Old-format decision (no payload.decision field)
        let tags = vec!["decision".to_string()];
        let d = new_note_event(
            "main",
            None,
            "system",
            "orm: sqlx — compile-time checks",
            &tags,
        )
        .unwrap();
        ledger.append_event(&d).unwrap();

        let ctx = render_context(&ledger, "main", DeriveOptions::default()).unwrap();

        assert!(
            ctx.contains("orm: sqlx"),
            "old-format decision should render in:\n{ctx}"
        );
        assert!(
            ctx.contains("## Decisions"),
            "Decisions section missing in:\n{ctx}"
        );

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

    #[test]
    fn decisions_decoupled_from_depth() {
        let (tmp, ledger) = setup_workspace();

        // Create 8 active decisions
        let tags = vec!["decision".to_string()];
        for i in 1..=8 {
            let d = new_note_event(
                "main",
                None,
                "user",
                &format!("Decision {i}: choice {i}"),
                &tags,
            )
            .unwrap();
            ledger.append_event(&d).unwrap();
        }

        // Render with depth=1 — decisions should still show up to 5
        let opts = DeriveOptions { depth: 1 };
        let ctx = render_context(&ledger, "main", opts).unwrap();

        assert!(
            ctx.contains("## Decisions"),
            "missing Decisions section in:\n{ctx}"
        );
        // At least 5 decisions visible even at depth=1
        let decision_count = (1..=8)
            .filter(|i| ctx.contains(&format!("Decision {i}: choice {i}")))
            .count();
        assert!(
            decision_count >= 5,
            "expected at least 5 decisions visible at depth=1, got {decision_count} in:\n{ctx}"
        );

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

    #[test]
    fn project_header_shows_session_count_single() {
        let (tmp, ledger) = setup_workspace();

        let d = make_digest_note_with_ts("main", "sess-001", "2026-02-17T10:00:00Z", &[], &[]);
        ledger.append_event(&d).unwrap();

        let ctx = render_context(&ledger, "main", DeriveOptions::default()).unwrap();

        assert!(
            ctx.contains("- sessions: 1 (2026-02-17)"),
            "missing single-session header in:\n{ctx}"
        );

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

    #[test]
    fn project_header_shows_session_count_range() {
        let (tmp, ledger) = setup_workspace();

        let d1 = make_digest_note_with_ts("main", "sess-001", "2026-02-12T10:00:00Z", &[], &["c1"]);
        let d2 = make_digest_note_with_ts("main", "sess-002", "2026-02-14T10:00:00Z", &[], &["c2"]);
        let d3 = make_digest_note_with_ts("main", "sess-003", "2026-02-17T10:00:00Z", &[], &["c3"]);
        ledger.append_event(&d1).unwrap();
        ledger.append_event(&d2).unwrap();
        ledger.append_event(&d3).unwrap();

        let ctx = render_context(&ledger, "main", DeriveOptions::default()).unwrap();

        assert!(
            ctx.contains("- sessions: 3 (2026-02-12 — 2026-02-17)"),
            "missing session range header in:\n{ctx}"
        );

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

    #[test]
    fn project_header_no_sessions_without_digests() {
        let (tmp, ledger) = setup_workspace();

        let note = new_note_event("main", None, "user", "hello", &[]).unwrap();
        ledger.append_event(&note).unwrap();

        let ctx = render_context(&ledger, "main", DeriveOptions::default()).unwrap();

        assert!(
            !ctx.contains("- sessions:"),
            "should not show sessions line without digests:\n{ctx}"
        );

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