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
//! Session-digest composer.
//!
//! Produces a bounded, ranked bootstrap packet so a new session can ground on
//! what prior sessions in the same project actually did — decisions made,
//! files touched, open tasks — without re-discovering from scratch.
//!
//! The digest is extractive (no model call): it composes real rows from
//! `sessions`, `event_log`, `graph_entities` (ReasoningLog / Memory) and
//! `discoveries` into a compact plain-text or JSON packet, ranked by recency
//! and truncated to a token budget.
//!
//! Activity counts are computed from `event_log` rather than trusted from the
//! `sessions` columns, because the session ledger records session start but
//! does not always backfill `tool_call_count` / `file_write_count` at session
//! end. Computing from events is accurate regardless of how the session was
//! recorded.

use anyhow::Result;
use rusqlite::OptionalExtension;
use serde_json::Value;

use super::{AtheneumGraph, EntityType};

/// `graph_entities.kind` for reasoning-log nodes. Not in the `EntityType` enum
/// (that enum covers the first-class ontology kinds), so it is referenced by
/// literal here.
const REASONING_LOG_KIND: &str = "ReasoningLog";

/// One file-access entry for a session, ranked by access frequency.
#[derive(Debug, serde::Serialize)]
struct FileAccess {
    path: String,
    count: i64,
}

/// One reasoning-log decision attributed to a session (via `observed_in`).
#[derive(Debug, serde::Serialize)]
struct DigestReasoning {
    entity_id: i64,
    summary: String,
}

/// One discovery attributed to a session (via `discoveries.session_id`).
#[derive(Debug, serde::Serialize)]
struct DigestDiscovery {
    target: String,
    discovery_type: String,
    summary: Option<String>,
}

/// One `Decision` discovery attributed to a session, surfaced separately so
/// the digest reliably shows the session's structured decisions (from any
/// capture source) rather than only the most recent discoveries of any type.
/// `source` is the capture layer (`askuser` / `exitplan` / `taskcreate` /
/// `todowrite` for the live watcher, `llm-extract` for the post-hoc
/// extractor, `NULL` for a manual `store_discovery`).
#[derive(Debug, serde::Serialize)]
struct DigestDecision {
    target: String,
    source: Option<String>,
    chosen: Option<String>,
    rationale: Option<String>,
}

/// Per-session block in the digest.
#[derive(Debug, serde::Serialize)]
struct DigestSession {
    session_id: String,
    short_id: String,
    started_at: String,
    project: String,
    git_branch: Option<String>,
    tool: String,
    parent_session_id: Option<String>,
    tool_calls: i64,
    file_writes: i64,
    commits: i64,
    top_files: Vec<FileAccess>,
    last_tool: Option<String>,
    last_tool_summary: Option<String>,
    reasoning: Vec<DigestReasoning>,
    discoveries: Vec<DigestDiscovery>,
    decisions: Vec<DigestDecision>,
}

/// One project-scoped memory entry.
#[derive(Debug, serde::Serialize)]
struct DigestMemory {
    key: String,
    content: String,
    confidence: f64,
}

/// One open task for the project.
#[derive(Debug, serde::Serialize)]
struct DigestTask {
    title: String,
    status: String,
}

/// Full structured digest, serialized for `--json` and rendered to text.
#[derive(Debug, serde::Serialize)]
pub struct DigestReport {
    project: Option<String>,
    fell_back_to_all_projects: bool,
    sessions: Vec<DigestSession>,
    project_memory: Vec<DigestMemory>,
    open_tasks: Vec<DigestTask>,
    thread_anchors: Vec<i64>,
}

impl AtheneumGraph {
    /// Compose a bounded plain-text digest for bootstrap grounding.
    ///
    /// `project` filters sessions; if the filter yields nothing the digest
    /// falls back to the most recent sessions across all projects and flags
    /// it. `last_n` bounds the session count; `tokens` bounds output size
    /// (estimated at 4 chars/token).
    pub fn compose_digest(
        &self,
        project: Option<&str>,
        last_n: i64,
        tokens: usize,
    ) -> Result<String> {
        let report = self.collect_digest(project, last_n)?;
        Ok(render_digest_text(&report, tokens))
    }

    /// Compose the structured digest (unbounded) for `--json` output.
    pub fn compose_digest_value(&self, project: Option<&str>, last_n: i64) -> Result<DigestReport> {
        self.collect_digest(project, last_n)
    }

    /// Compose the digest as a JSON value (for `--json` CLI output).
    pub fn compose_digest_json(&self, project: Option<&str>, last_n: i64) -> Result<Value> {
        let report = self.collect_digest(project, last_n)?;
        digest_report_to_json(&report)
    }

    fn collect_digest(&self, project: Option<&str>, last_n: i64) -> Result<DigestReport> {
        let project_owned = project.map(|s| s.to_string());

        // Try the project filter first; fall back to all projects if empty so
        // the digest is still useful when project tagging is sparse (many
        // sessions are tagged "tmp").
        let mut sessions = self.query_sessions(project, last_n, None)?;
        let fell_back = if sessions.is_empty() && project.is_some() {
            sessions = self.query_sessions(None, last_n, None)?;
            !sessions.is_empty()
        } else {
            false
        };

        let mut digest_sessions = Vec::with_capacity(sessions.len());
        for s in &sessions {
            digest_sessions.push(self.collect_session_detail(s)?);
        }

        let project_memory = self.collect_project_memory(project_owned.as_deref(), 5)?;
        let open_tasks = self.collect_open_tasks(project_owned.as_deref())?;
        let thread_anchors = self.collect_thread_anchors(3)?;

        Ok(DigestReport {
            project: project_owned,
            fell_back_to_all_projects: fell_back,
            sessions: digest_sessions,
            project_memory,
            open_tasks,
            thread_anchors,
        })
    }

    /// Gather per-session detail in one connection round-trip.
    fn collect_session_detail(&self, s: &super::SessionSummary) -> Result<DigestSession> {
        let session_id = s.session_id.clone();
        let tool_call_count = s.tool_call_count;

        let detail = self.with_raw_connection(|conn| {
            // Activity from event_log — accurate even when the sessions
            // ledger columns are zero.
            let computed_tool_calls: i64 = if tool_call_count > 0 {
                tool_call_count
            } else {
                conn.query_row(
                    "SELECT COUNT(*) FROM event_log
                     WHERE session_id = ?1 AND event_type = 'tool_call'",
                    rusqlite::params![session_id],
                    |row| row.get(0),
                )
                .optional()?
                .unwrap_or(0)
            };

            let file_writes: i64 = if s.file_write_count > 0 {
                s.file_write_count
            } else {
                // Two ingest paths record writes: `record_evidence_file_write`
                // emits a `file_write` event, while the transcript-sync path
                // emits `file_access` events with `access_type = "write"`.
                // Count both so the activity is accurate regardless of which
                // recorder produced the session's events.
                conn.query_row(
                    "SELECT COUNT(*) FROM event_log
                     WHERE session_id = ?1
                       AND (event_type = 'file_write'
                            OR (event_type = 'file_access'
                                AND json_extract(payload, '$.access_type') = 'write'))",
                    rusqlite::params![session_id],
                    |row| row.get(0),
                )
                .optional()?
                .unwrap_or(0)
            };

            // Top files accessed (distinct paths, by frequency). Both
            // `file_access` and `file_write` events carry the path under
            // `payload.file_path`. We pull more rows than needed and merge by
            // basename in Rust, because distinct files in different
            // directories can share a basename (e.g. several projects'
            // `SKILL.md`) — grouping by full path would list `SKILL.md`
            // three times.
            let mut raw_files = Vec::new();
            let mut stmt = conn.prepare(
                "SELECT json_extract(payload, '$.file_path') AS p, COUNT(*) AS c
                 FROM event_log
                 WHERE session_id = ?1
                   AND event_type IN ('file_access', 'file_write')
                   AND json_extract(payload, '$.file_path') IS NOT NULL
                 GROUP BY p ORDER BY c DESC LIMIT 20",
            )?;
            let rows = stmt.query_map(rusqlite::params![session_id], |row| {
                Ok(FileAccess {
                    path: row.get::<_, Option<String>>(0)?.unwrap_or_default(),
                    count: row.get(1)?,
                })
            })?;
            for row in rows {
                raw_files.push(row?);
            }
            drop(stmt);
            let top_files = merge_by_basename(raw_files, 5);

            // ReasoningLog decisions linked to this session via `observed_in`.
            // Session graph entities are named `<tool>:<session_id>` (e.g.
            // `claude-code:07ff7531-...`), so matching `name = session_id`
            // never hits. Join on the Session entity's `data.session_id`
            // instead, which also tolerates multiple Session entities for the
            // same session id.
            let mut reasoning = Vec::new();
            // ReasoningLog entities use either `content_summary` (transcript
            // sync path) or `content` (the `insert_reasoning_log` audit path)
            // for their text — coalesce both so decisions surface regardless
            // of which recorder produced them.
            let mut stmt = conn.prepare(
                "SELECT rl.id,
                        COALESCE(json_extract(rl.data, '$.content_summary'),
                                 json_extract(rl.data, '$.content'))
                 FROM graph_entities rl
                 JOIN graph_edges e
                   ON e.from_id = rl.id AND e.edge_type = 'observed_in'
                 JOIN graph_entities s
                   ON e.to_id = s.id AND s.kind = ?1
                 WHERE rl.kind = ?2
                   AND json_extract(s.data, '$.session_id') = ?3
                 ORDER BY rl.id DESC LIMIT 2",
            )?;
            let rows = stmt.query_map(
                rusqlite::params![EntityType::Session.as_str(), REASONING_LOG_KIND, session_id],
                |row| {
                    Ok(DigestReasoning {
                        entity_id: row.get(0)?,
                        summary: row.get::<_, Option<String>>(1)?.unwrap_or_default(),
                    })
                },
            )?;
            for row in rows {
                reasoning.push(row?);
            }

            // Discoveries attributed to this session via the session_id column.
            let mut discoveries = Vec::new();
            let mut stmt = conn.prepare(
                "SELECT target, discovery_type, json_extract(metadata, '$.summary')
                 FROM discoveries
                 WHERE session_id = ?1
                 ORDER BY id DESC LIMIT 3",
            )?;
            let rows = stmt.query_map(rusqlite::params![session_id], |row| {
                Ok(DigestDiscovery {
                    target: row.get(0)?,
                    discovery_type: row.get(1)?,
                    summary: row.get(2)?,
                })
            })?;
            for row in rows {
                discoveries.push(row?);
            }
            drop(stmt);

            // Structured `Decision` rows for this session — filtered to
            // `discovery_type = 'Decision'` and labeled with their capture
            // `source` so the digest surfaces decisions from every layer
            // (live watcher, backfiller, manual) rather than only the most
            // recent discoveries of any type.
            let mut decisions = Vec::new();
            let mut stmt = conn.prepare(
                "SELECT target,
                        json_extract(metadata, '$.source'),
                        json_extract(metadata, '$.chosen'),
                        json_extract(metadata, '$.rationale')
                 FROM discoveries
                 WHERE session_id = ?1 AND discovery_type = 'Decision'
                 ORDER BY id DESC LIMIT 5",
            )?;
            let rows = stmt.query_map(rusqlite::params![session_id], |row| {
                Ok(DigestDecision {
                    target: row.get(0)?,
                    source: row.get(1)?,
                    chosen: row.get(2)?,
                    rationale: row.get(3)?,
                })
            })?;
            for row in rows {
                decisions.push(row?);
            }

            Ok::<SessionDetail, anyhow::Error>(SessionDetail {
                computed_tool_calls,
                file_writes,
                top_files,
                reasoning,
                discoveries,
                decisions,
            })
        })?;

        Ok(DigestSession {
            session_id: s.session_id.clone(),
            short_id: short_id(&s.session_id),
            started_at: s.started_at.clone(),
            project: s.project.clone(),
            git_branch: s.git_branch.clone(),
            tool: if s.tool.is_empty() {
                s.trigger.clone()
            } else {
                s.tool.clone()
            },
            parent_session_id: s.parent_session_id.clone(),
            tool_calls: detail.computed_tool_calls,
            file_writes: detail.file_writes,
            commits: s.commit_count,
            top_files: detail.top_files,
            last_tool: s.last_tool.clone(),
            last_tool_summary: s.last_tool_summary.clone(),
            reasoning: detail.reasoning,
            discoveries: detail.discoveries,
            decisions: detail.decisions,
        })
    }

    /// Project-scoped durable memory, highest confidence first.
    fn collect_project_memory(
        &self,
        project: Option<&str>,
        limit: usize,
    ) -> Result<Vec<DigestMemory>> {
        let entities = self.list_memory_page(None, project, 0, limit)?;
        let mut out = Vec::new();
        for e in entities {
            let key = e
                .data
                .get("key")
                .and_then(|v| v.as_str())
                .unwrap_or(&e.name)
                .to_string();
            let content = e
                .data
                .get("content")
                .and_then(|v| v.as_str())
                .unwrap_or("")
                .to_string();
            let confidence = e
                .data
                .get("confidence")
                .and_then(|v| v.as_f64())
                .unwrap_or(1.0);
            out.push(DigestMemory {
                key,
                content,
                confidence,
            });
        }
        // Highest confidence first, then leave insertion order as tiebreaker.
        out.sort_by(|a, b| {
            b.confidence
                .partial_cmp(&a.confidence)
                .unwrap_or(std::cmp::Ordering::Equal)
        });
        Ok(out)
    }

    /// Non-archived tasks for the project, limited to open statuses.
    fn collect_open_tasks(&self, project: Option<&str>) -> Result<Vec<DigestTask>> {
        let entities = self.list_tasks(project)?;
        let open = ["TODO", "IN_PROGRESS", "BLOCKED"];
        let mut out = Vec::new();
        for e in entities {
            let status = e
                .data
                .get("status")
                .and_then(|v| v.as_str())
                .unwrap_or("TODO")
                .to_string();
            if !open.contains(&status.as_str()) {
                continue;
            }
            let title = e.name.clone();
            out.push(DigestTask { title, status });
        }
        Ok(out)
    }

    /// Most recent ReasoningLog entity ids — anchors for `atheneum navigate`
    /// follow-up to walk a decision thread.
    fn collect_thread_anchors(&self, limit: i64) -> Result<Vec<i64>> {
        self.with_raw_connection(|conn| {
            let mut stmt = conn.prepare(
                "SELECT id FROM graph_entities
                 WHERE kind = ?1
                 ORDER BY id DESC LIMIT ?2",
            )?;
            let rows = stmt.query_map(rusqlite::params![REASONING_LOG_KIND, limit], |row| {
                row.get::<_, i64>(0)
            })?;
            let mut out = Vec::new();
            for row in rows {
                out.push(row?);
            }
            Ok(out)
        })
    }
}

struct SessionDetail {
    computed_tool_calls: i64,
    file_writes: i64,
    top_files: Vec<FileAccess>,
    reasoning: Vec<DigestReasoning>,
    discoveries: Vec<DigestDiscovery>,
    decisions: Vec<DigestDecision>,
}

fn short_id(session_id: &str) -> String {
    session_id
        .split('-')
        .next()
        .unwrap_or(session_id)
        .to_string()
}

/// Merge file-access rows by basename (last path segment), summing counts.
/// Distinct files in different directories often share a basename (several
/// projects ship a `SKILL.md`); without merging, the top-N list would show
/// the same basename repeatedly. Input is pre-sorted by count desc; output
/// preserves that order after merging, truncated to `limit`.
fn merge_by_basename(rows: Vec<FileAccess>, limit: usize) -> Vec<FileAccess> {
    use std::collections::BTreeMap;
    let mut merged: BTreeMap<String, i64> = BTreeMap::new();
    // BTreeMap iteration order is by basename, not by count — preserve the
    // original count-desc order via a separate ordering pass.
    let mut order: Vec<String> = Vec::new();
    for f in rows {
        let base = f
            .path
            .rsplit('/')
            .next()
            .filter(|s| !s.is_empty())
            .unwrap_or(&f.path)
            .to_string();
        let entry = merged.entry(base.clone()).or_insert(0);
        if *entry == 0 {
            order.push(base);
        }
        *entry += f.count;
    }
    let mut out: Vec<FileAccess> = order
        .into_iter()
        .map(|name| {
            let count = merged[&name];
            FileAccess { path: name, count }
        })
        .collect();
    out.sort_by_key(|b| std::cmp::Reverse(b.count));
    out.truncate(limit);
    out
}

/// Render the structured report as a compact, LLM-dense plain-text packet,
/// truncated to `tokens` (4 chars/token). Sections are added in priority
/// order; lowest-priority sections are dropped first when over budget so the
/// most recent session block always survives.
fn render_digest_text(report: &DigestReport, tokens: usize) -> String {
    let budget = tokens.saturating_mul(4);
    let mut out = String::new();

    let header = match &report.project {
        Some(p) => format!(
            "== PRIOR SESSIONS (project: {}, last {}) ==\n",
            p,
            report.sessions.len()
        ),
        None => format!("== PRIOR SESSIONS (last {}) ==\n", report.sessions.len()),
    };
    out.push_str(&header);

    if report.fell_back_to_all_projects {
        out.push_str("note: no sessions tagged with the requested project; showing most recent across all projects.\n");
    }

    // Session blocks are highest priority — render them first and keep them
    // even if later sections get dropped.
    for s in &report.sessions {
        out.push_str(&render_session_block(s));
    }

    let project_memory_block = render_memory_block(&report.project_memory);
    let open_tasks_block = render_tasks_block(&report.open_tasks);
    let thread_anchors_block = render_anchors_block(&report.thread_anchors);

    // Append lower-priority sections only while within budget.
    let mut tail = String::new();
    for block in [project_memory_block, open_tasks_block, thread_anchors_block] {
        if block.is_empty() {
            continue;
        }
        if out.len() + tail.len() + block.len() > budget {
            tail.push_str("[truncated]\n");
            break;
        }
        tail.push_str(&block);
    }
    out.push_str(&tail);

    // Hard cap: if session blocks alone blew the budget, cut from the end.
    if out.len() > budget {
        out.truncate(budget.saturating_sub("[truncated]\n".len()));
        out.push_str("[truncated]\n");
    }

    out
}

fn render_session_block(s: &DigestSession) -> String {
    let mut out = String::new();
    let ts = s.started_at.split('T').next().unwrap_or(&s.started_at);
    let ts_time = s
        .started_at
        .split('T')
        .nth(1)
        .map(|t| t.split('+').next().unwrap_or(t))
        .unwrap_or("");
    out.push_str(&format!(
        "\n[{} {}] {} branch={} tool={}\n",
        ts,
        ts_time,
        s.short_id,
        s.git_branch.as_deref().unwrap_or("-"),
        s.tool
    ));
    if let Some(parent) = &s.parent_session_id {
        let pshort = short_id(parent);
        out.push_str(&format!("  parent: {}\n", pshort));
    }
    // Activity line: always show tool calls; show file writes and commits
    // only when non-zero (the session ledger leaves these zero when the
    // recorder does not backfill them, and there is no commit event type,
    // so they are usually 0 — printing them adds noise).
    let mut activity = format!("{} tool calls", s.tool_calls);
    if s.file_writes > 0 {
        activity.push_str(&format!(", {} file writes", s.file_writes));
    }
    if s.commits > 0 {
        activity.push_str(&format!(", {} commits", s.commits));
    }
    out.push_str(&format!("  activity: {}\n", activity));
    if !s.top_files.is_empty() {
        let files: Vec<String> = s
            .top_files
            .iter()
            .map(|f| {
                let name = f.path.rsplit('/').next().unwrap_or(&f.path);
                format!("{} (x{})", name, f.count)
            })
            .collect();
        out.push_str(&format!("  files: {}\n", files.join(", ")));
    }
    if let Some(lt) = &s.last_tool {
        let summary = s
            .last_tool_summary
            .as_deref()
            .map(|s| truncate_summary(s, 80))
            .unwrap_or_default();
        out.push_str(&format!("  last: {} {}\n", lt, summary));
    }
    for r in &s.reasoning {
        if !r.summary.is_empty() {
            out.push_str(&format!(
                "  decision: {} (#{})\n",
                truncate_summary(&r.summary, 120),
                r.entity_id
            ));
        }
    }
    for d in &s.discoveries {
        let summary = d
            .summary
            .as_deref()
            .map(|s| format!("{}", truncate_summary(s, 100)))
            .unwrap_or_default();
        out.push_str(&format!(
            "  discovery: {} [{}]{}\n",
            d.target, d.discovery_type, summary
        ));
    }
    for d in &s.decisions {
        // `chosen` is the most useful one-line signal for a decision; fall
        // back to the target if it is absent (e.g. a manual store_discovery
        // with no structured fields). `source` names the capture layer.
        let head = d.chosen.as_deref().unwrap_or(&d.target);
        let src = d.source.as_deref().unwrap_or("manual");
        let tail = d
            .rationale
            .as_deref()
            .filter(|s| !s.is_empty())
            .map(|s| format!("{}", truncate_summary(s, 100)))
            .unwrap_or_default();
        out.push_str(&format!(
            "  decision: {} [{}]{}\n",
            truncate_summary(head, 120),
            src,
            tail,
        ));
    }
    out
}

fn render_memory_block(mem: &[DigestMemory]) -> String {
    if mem.is_empty() {
        return String::new();
    }
    let mut out = String::from("\n== PROJECT MEMORY ==\n");
    for m in mem {
        out.push_str(&format!(
            "- [{}] (conf {:.2}): {}\n",
            m.key,
            m.confidence,
            truncate_summary(&m.content, 160)
        ));
    }
    out
}

fn render_tasks_block(tasks: &[DigestTask]) -> String {
    if tasks.is_empty() {
        return String::new();
    }
    let mut out = String::from("\n== OPEN TASKS ==\n");
    for t in tasks {
        out.push_str(&format!("- [{}] {}\n", t.status, t.title));
    }
    out
}

fn render_anchors_block(anchors: &[i64]) -> String {
    if anchors.is_empty() {
        return String::new();
    }
    let ids: Vec<String> = anchors.iter().map(|a| format!("#{}", a)).collect();
    format!(
        "\n== THREAD ANCHORS (traverse with `atheneum navigate`) ==\n{}\n",
        ids.join(", ")
    )
}

fn truncate_summary(s: &str, max_chars: usize) -> String {
    let s = s.trim();
    if s.chars().count() <= max_chars {
        return s.to_string();
    }
    let truncated: String = s.chars().take(max_chars.saturating_sub(1)).collect();
    format!("{}", truncated.trim_end())
}

/// Serialize a digest report to a JSON value (for `--json` output).
pub fn digest_report_to_json(report: &DigestReport) -> Result<Value> {
    serde_json::to_value(report).map_err(|e| anyhow::anyhow!("digest serialization failed: {}", e))
}

#[cfg(test)]
mod tests {
    use super::*;
    use serde_json::json;

    #[test]
    fn truncate_summary_short_unchanged() {
        assert_eq!(truncate_summary("hello", 10), "hello");
    }

    #[test]
    fn truncate_summary_long_clamped() {
        let s = "x".repeat(200);
        let out = truncate_summary(&s, 10);
        assert!(out.ends_with(''));
        assert!(out.chars().count() <= 10);
    }

    #[test]
    fn render_empty_report_has_header() {
        let report = DigestReport {
            project: Some("rocmforge".into()),
            fell_back_to_all_projects: false,
            sessions: vec![],
            project_memory: vec![],
            open_tasks: vec![],
            thread_anchors: vec![],
        };
        let text = render_digest_text(&report, 500);
        assert!(text.contains("PRIOR SESSIONS"));
        assert!(text.contains("rocmforge"));
    }

    #[test]
    fn render_drops_low_priority_when_over_budget() {
        let session = DigestSession {
            session_id: "abcdef-1234".into(),
            short_id: "abcdef".into(),
            started_at: "2026-06-22T08:03:53+00:00".into(),
            project: "p".into(),
            git_branch: Some("HEAD".into()),
            tool: "claude-code".into(),
            parent_session_id: None,
            tool_calls: 230,
            file_writes: 0,
            commits: 0,
            top_files: vec![FileAccess {
                path: "src/main.rs".into(),
                count: 5,
            }],
            last_tool: Some("Bash".into()),
            last_tool_summary: Some("cargo build".into()),
            reasoning: vec![DigestReasoning {
                entity_id: 42,
                summary: "decided X".into(),
            }],
            discoveries: vec![],
            decisions: vec![],
        };
        let report = DigestReport {
            project: Some("p".into()),
            fell_back_to_all_projects: false,
            sessions: vec![session],
            project_memory: vec![DigestMemory {
                key: "k".into(),
                content: "m".into(),
                confidence: 1.0,
            }],
            open_tasks: vec![DigestTask {
                title: "t".into(),
                status: "TODO".into(),
            }],
            thread_anchors: vec![7],
        };
        // Tiny budget: session block survives, tail is truncated.
        let text = render_digest_text(&report, 30);
        assert!(text.contains("abcdef"));
        assert!(text.contains("[truncated]"));
    }

    #[test]
    fn digest_report_serializes_to_json() {
        let report = DigestReport {
            project: None,
            fell_back_to_all_projects: false,
            sessions: vec![],
            project_memory: vec![],
            open_tasks: vec![],
            thread_anchors: vec![],
        };
        let v = digest_report_to_json(&report).unwrap();
        assert_eq!(v["sessions"], json!([]));
    }

    #[test]
    fn short_id_takes_first_segment() {
        assert_eq!(short_id("c663d1ff-d1e1-4525-bd70-84af021679bc"), "c663d1ff");
        assert_eq!(short_id("plain"), "plain");
    }

    #[test]
    fn merge_by_basename_sums_shared_names() {
        let rows = vec![
            FileAccess {
                path: "/a/SKILL.md".into(),
                count: 26,
            },
            FileAccess {
                path: "/b/SKILL.md".into(),
                count: 16,
            },
            FileAccess {
                path: "src/layer.rs".into(),
                count: 20,
            },
            FileAccess {
                path: "/c/SKILL.md".into(),
                count: 7,
            },
        ];
        let merged = merge_by_basename(rows, 5);
        // SKILL.md summed to 49 (first), layer.rs second.
        assert_eq!(merged.len(), 2);
        assert_eq!(merged[0].path, "SKILL.md");
        assert_eq!(merged[0].count, 49);
        assert_eq!(merged[1].path, "layer.rs");
        assert_eq!(merged[1].count, 20);
    }

    #[test]
    fn merge_by_basename_truncates_to_limit() {
        let rows: Vec<FileAccess> = (0..10)
            .map(|i| FileAccess {
                path: format!("src/f{}.rs", i),
                count: 10 - i,
            })
            .collect();
        let merged = merge_by_basename(rows, 3);
        assert_eq!(merged.len(), 3);
        assert_eq!(merged[0].path, "f0.rs");
    }
}