gobby-wiki 0.8.0

Gobby wiki CLI shell
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
//! `gwiki recap`: write the day's session recap page.
//!
//! The session-ingestion payoff: deterministic selection of the day's session
//! digests, one bounded single-shot synthesis (a single completion, never an
//! agent loop, so it survives local models), and a `recaps/YYYY-MM-DD.md`
//! page citing its digests. The rolling "Recent work" block in `_index.md`'s
//! Overview is derived from the `recaps/` directory by catalog regeneration,
//! so it survives every deterministic rebuild and rides the session-start
//! injection.
//!
//! Day attribution is UTC: a session belongs to the target date when its
//! digest's `session_started_at` frontmatter (falling back to the manifest
//! record's `fetched_at`) parses to that UTC calendar day. Timestamps arrive
//! in both `unix-ms:<millis>` (gwiki's `collect_timestamp`) and RFC 3339
//! (transcript adapters) forms. A day with no sessions writes nothing and is
//! not an error.

use std::fs;
use std::path::{Path, PathBuf};

use chrono::{DateTime, NaiveDate, Utc};
use serde::Serialize;

use crate::explainer::{
    CitationTarget, ExplainerGeneration, ExplainerGenerator, build_explainer_prompt,
    generate_explainer, ground_explainer,
};
use crate::frontmatter::parse_frontmatter;
use crate::sources::{SourceKind, SourceManifest, SourceRecord};
use crate::synthesis::{ArticleKind, SynthesisInput, SynthesisSource, relative_path, wiki_link};
use crate::{ScopeIdentity, WikiError, catalog, paths};

/// Vault-relative directory holding recap pages.
pub(crate) const RECAPS_DIRECTORY: &str = "recaps";

/// One recap run's outcome, serialized as the command payload.
#[derive(Debug, Clone, Serialize)]
pub struct RecapReport {
    pub command: &'static str,
    pub scope: ScopeIdentity,
    pub timestamp: String,
    /// Target day (`YYYY-MM-DD`, UTC attribution).
    pub date: String,
    pub sessions_selected: usize,
    /// Selected session source ids, in page listing order.
    pub session_ids: Vec<String>,
    /// Selected digests dropped from the prompt by the token budget; they
    /// still appear in the page's Sessions listing.
    pub sources_truncated: usize,
    /// `generated` / `skipped` / `failed`.
    pub synthesis: &'static str,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub page_path: Option<PathBuf>,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub page_action: Option<&'static str>,
    pub citations_kept: usize,
    pub citations_stripped: usize,
    pub fallback_sections: usize,
    pub notes: Vec<String>,
}

/// One session digest selected for the target day.
struct SelectedSession {
    record: SourceRecord,
    /// Vault-relative digest path (`knowledge/sources/<id>.md`).
    digest_relative: PathBuf,
    /// Digest body with frontmatter stripped; empty when the digest file is
    /// missing or unreadable.
    body: String,
    title: String,
    /// Best attributed instant, for deterministic chronological ordering.
    instant: DateTime<Utc>,
}

/// Write the recap page for `date` from the day's session digests.
pub fn run(
    vault_root: &Path,
    scope: ScopeIdentity,
    date: NaiveDate,
    generator: Option<ExplainerGenerator<'_>>,
    timestamp: &str,
) -> Result<RecapReport, WikiError> {
    let date_label = date.format("%Y-%m-%d").to_string();
    let mut notes: Vec<String> = Vec::new();
    let mut sessions = select_sessions(vault_root, date, &mut notes)?;
    sessions.sort_by(|left, right| {
        left.instant
            .cmp(&right.instant)
            .then_with(|| left.record.id.cmp(&right.record.id))
    });
    let session_ids: Vec<String> = sessions
        .iter()
        .map(|session| session.record.id.clone())
        .collect();

    let mut report = RecapReport {
        command: "recap",
        scope: scope.clone(),
        timestamp: timestamp.to_string(),
        date: date_label.clone(),
        sessions_selected: sessions.len(),
        session_ids,
        sources_truncated: 0,
        synthesis: "skipped",
        page_path: None,
        page_action: None,
        citations_kept: 0,
        citations_stripped: 0,
        fallback_sections: 0,
        notes,
    };
    // A day with no sessions produces no page, no catalog churn, no log line.
    if sessions.is_empty() {
        return Ok(report);
    }

    let page_relative = PathBuf::from(RECAPS_DIRECTORY).join(format!("{date_label}.md"));
    let page_absolute = vault_root.join(&page_relative);
    let existing_page_body = read_existing_body(&page_absolute)?;
    let page_action = if existing_page_body.is_some() {
        "updated"
    } else {
        "created"
    };

    let input = SynthesisInput {
        handoff_id: format!("recap-{date_label}"),
        topic: format!("Recap of {date_label}"),
        // Empty outline requests a single Overview section from the explainer.
        outline: Vec::new(),
        target_kind: ArticleKind::Topic,
        accepted_sources: sessions
            .iter()
            .map(|session| SynthesisSource {
                title: session.title.clone(),
                path: vault_root.join(&session.digest_relative),
                chunks: vec![session.body.clone()],
                existing_page: Some(vault_root.join(&session.digest_relative)),
            })
            .collect(),
        citations: Vec::new(),
        conflicting_claims: Vec::new(),
        missing_evidence: Vec::new(),
        existing_page_body,
        aliases: Vec::new(),
        extra_tags: Vec::new(),
    };
    let prompt = build_explainer_prompt(vault_root, &input);
    report.sources_truncated = prompt.truncated_sources;

    let targets: Vec<CitationTarget> = sessions
        .iter()
        .map(|session| {
            let absolute = vault_root.join(&session.digest_relative);
            CitationTarget {
                key: relative_path(vault_root, &absolute),
                link: wiki_link(vault_root, &absolute, &session.title),
                corpus: session.body.clone(),
            }
        })
        .collect();

    let overview = match generate_explainer(&input, &prompt, generator) {
        ExplainerGeneration::Generated { body, .. } => {
            let grounded = ground_explainer(&body, &targets);
            report.synthesis = "generated";
            report.citations_kept = grounded.citations_kept;
            report.citations_stripped = grounded.citations_stripped;
            report.fallback_sections = grounded.fallback_sections;
            grounded.body
        }
        ExplainerGeneration::Failed { error } => {
            report.synthesis = "failed";
            report
                .notes
                .push(format!("recap synthesis failed: {error}"));
            fallback_overview()
        }
        ExplainerGeneration::Skipped => {
            report.synthesis = "skipped";
            fallback_overview()
        }
    };

    let page = render_page(vault_root, &date_label, &overview, &sessions);
    write_page(&page_absolute, &page)?;
    report.page_path = Some(page_relative.clone());
    report.page_action = Some(page_action);

    // Fold the new recap into the deterministic catalog so `_index.md`'s
    // Overview picks up the rolling Recent work block.
    catalog::regenerate(vault_root, &scope)?;
    crate::log::append_logs(
        vault_root,
        None,
        &crate::log::LogEntry {
            timestamp: timestamp.to_string(),
            scope,
            action: crate::log::ACTION_RECAP_COMPLETED.to_string(),
            summary: format!(
                "date={date_label} sessions={} synthesis={} page={page_action}",
                report.sessions_selected, report.synthesis,
            ),
            artifacts: vec![page_relative],
        },
    )?;

    Ok(report)
}

/// Deterministically select the target day's session digests: manifest
/// records of kind `session` whose attributed UTC day equals `date`.
fn select_sessions(
    vault_root: &Path,
    date: NaiveDate,
    notes: &mut Vec<String>,
) -> Result<Vec<SelectedSession>, WikiError> {
    let manifest = SourceManifest::read(vault_root)?;
    let mut selected = Vec::new();
    for record in manifest.entries {
        if record.kind != SourceKind::Session {
            continue;
        }
        let digest_relative = paths::derived_markdown_path(&record)?;
        let digest = read_digest(&vault_root.join(&digest_relative));
        if digest.is_none() {
            notes.push(format!("digest missing for session source {}", record.id));
        }
        let (body, started_at) = digest.unwrap_or_default();
        let Some(instant) = started_at
            .as_deref()
            .and_then(parse_instant)
            .or_else(|| parse_instant(&record.fetched_at))
        else {
            notes.push(format!(
                "session source {} has no parseable timestamp",
                record.id
            ));
            continue;
        };
        if instant.date_naive() != date {
            continue;
        }
        let title = record
            .title
            .clone()
            .unwrap_or_else(|| format!("Session {}", record.id));
        selected.push(SelectedSession {
            record,
            digest_relative,
            body,
            title,
            instant,
        });
    }
    Ok(selected)
}

/// Parse a digest page into its body and `session_started_at` frontmatter
/// value. Returns `None` when the file is missing or unreadable — the record
/// still recaps from its manifest metadata.
fn read_digest(path: &Path) -> Option<(String, Option<String>)> {
    let markdown = fs::read_to_string(path).ok()?;
    match parse_frontmatter(&markdown) {
        Ok(parsed) => {
            let started_at = parsed
                .metadata
                .unknown
                .get("session_started_at")
                .and_then(|value| value.as_str())
                .map(str::to_string);
            Some((parsed.body.to_string(), started_at))
        }
        Err(_) => Some((markdown, None)),
    }
}

/// Parse `unix-ms:<millis>`, RFC 3339, or a bare `YYYY-MM-DD` prefix into a
/// UTC instant.
fn parse_instant(value: &str) -> Option<DateTime<Utc>> {
    let value = value.trim();
    if let Some(millis) = value.strip_prefix("unix-ms:") {
        return millis
            .parse::<i64>()
            .ok()
            .and_then(DateTime::<Utc>::from_timestamp_millis);
    }
    if let Ok(parsed) = DateTime::parse_from_rfc3339(value) {
        return Some(parsed.with_timezone(&Utc));
    }
    let date = NaiveDate::parse_from_str(value.get(..10)?, "%Y-%m-%d").ok()?;
    Some(DateTime::from_naive_utc_and_offset(
        date.and_hms_opt(0, 0, 0)?,
        Utc,
    ))
}

fn fallback_overview() -> String {
    "## Overview\n\nSynthesis was unavailable for this run; the day's sessions are listed below."
        .to_string()
}

fn render_page(
    vault_root: &Path,
    date_label: &str,
    overview: &str,
    sessions: &[SelectedSession],
) -> String {
    let mut page = String::from("---\n");
    page.push_str(&format!("title: \"Recap: {date_label}\"\n"));
    page.push_str("tags:\n  - gwiki\n  - recap\n");
    page.push_str(&format!("recap_date: {date_label}\n"));
    page.push_str("---\n");
    page.push_str(&format!("# Recap: {date_label}\n\n"));
    page.push_str(overview.trim_end());
    page.push_str("\n\n## Sessions\n\n");
    for session in sessions {
        let absolute = vault_root.join(&session.digest_relative);
        page.push_str("- ");
        page.push_str(&wiki_link(vault_root, &absolute, &session.title));
        page.push('\n');
    }
    page
}

fn write_page(path: &Path, content: &str) -> Result<(), WikiError> {
    if let Some(parent) = path.parent() {
        fs::create_dir_all(parent).map_err(|error| WikiError::Io {
            action: "create recaps directory",
            path: Some(parent.to_path_buf()),
            source: error,
        })?;
    }
    fs::write(path, content).map_err(|error| WikiError::Io {
        action: "write recap page",
        path: Some(path.to_path_buf()),
        source: error,
    })
}

fn read_existing_body(path: &Path) -> Result<Option<String>, WikiError> {
    let markdown = match fs::read_to_string(path) {
        Ok(markdown) => markdown,
        Err(error) if error.kind() == std::io::ErrorKind::NotFound => return Ok(None),
        Err(error) => {
            return Err(WikiError::Io {
                action: "read existing recap page",
                path: Some(path.to_path_buf()),
                source: error,
            });
        }
    };
    let body = match parse_frontmatter(&markdown) {
        Ok(parsed) => parsed.body.to_string(),
        Err(_) => markdown,
    };
    Ok(Some(body))
}

/// Human-readable summary of one recap run.
pub fn render_text(report: &RecapReport) -> String {
    let mut text = format!(
        "recap {}: {} session(s), synthesis {}",
        report.date, report.sessions_selected, report.synthesis
    );
    match (&report.page_path, report.page_action) {
        (Some(path), Some(action)) => {
            text.push_str(&format!(", {action} {}", path.display()));
        }
        _ => text.push_str(", no page written"),
    }
    text.push('\n');
    for note in &report.notes {
        text.push_str("note: ");
        text.push_str(note);
        text.push('\n');
    }
    text
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::explainer::{ExplainerPrompt, ExplainerResponse};
    use crate::sources::{CompileStatus, IngestionMethod};

    /// 2026-07-04T04:13:20Z.
    const TIMESTAMP: &str = "unix-ms:1783138400000";

    fn scope() -> ScopeIdentity {
        ScopeIdentity::topic("recap-test")
    }

    fn target_date() -> NaiveDate {
        NaiveDate::from_ymd_opt(2026, 7, 4).expect("valid date")
    }

    fn session_record(id: &str, fetched_at: &str) -> SourceRecord {
        SourceRecord {
            id: id.to_string(),
            location: format!("session:{id}"),
            canonical_location: format!("session:{id}"),
            kind: SourceKind::Session,
            fetched_at: fetched_at.to_string(),
            content_hash: format!("{id}-hash"),
            title: Some(format!("Session {id}")),
            citation: None,
            license: None,
            ingestion_method: IngestionMethod::Manual,
            compile_status: CompileStatus::Pending,
            replay: None,
        }
    }

    fn write_file(root: &Path, relative: &str, content: &str) {
        let path = root.join(relative);
        fs::create_dir_all(path.parent().expect("parent")).expect("create parent");
        fs::write(path, content).expect("write file");
    }

    /// Register a session source with a digest page. `started_at: None` omits
    /// the `session_started_at` frontmatter so attribution falls back to the
    /// record's `fetched_at`.
    fn seed_session(root: &Path, id: &str, fetched_at: &str, started_at: Option<&str>, body: &str) {
        let record = session_record(id, fetched_at);
        let mut digest = String::from("---\n");
        digest.push_str(&format!("title: \"Session {id}\"\n"));
        if let Some(started_at) = started_at {
            digest.push_str(&format!("session_started_at: \"{started_at}\"\n"));
        }
        digest.push_str("---\n");
        digest.push_str(body);
        write_file(root, &format!("knowledge/sources/{id}.md"), &digest);
        SourceManifest::update(root, |manifest| {
            manifest.entries.push(record.clone());
            Ok(true)
        })
        .expect("seed manifest entry");
    }

    fn cited_generator(
        citation: &'static str,
    ) -> impl FnMut(&ExplainerPrompt) -> Result<ExplainerResponse, String> {
        move |_prompt: &ExplainerPrompt| {
            Ok(ExplainerResponse {
                text: format!("## Overview\n\nShipped the recap feature [source: {citation}]."),
                model: Some("test-model".to_string()),
                route: "test",
                tool_use_count: None,
                turns: None,
                usage: None,
            })
        }
    }

    #[test]
    fn recap_writes_cited_page_recent_work_block_and_log() {
        let temp = tempfile::tempdir().expect("tempdir");
        let root = temp.path();
        seed_session(
            root,
            "s-late",
            TIMESTAMP,
            Some("2026-07-04T15:00:00Z"),
            "Later work on the recap page.\n",
        );
        seed_session(
            root,
            "s-early",
            TIMESTAMP,
            Some("2026-07-04T09:00:00Z"),
            "Morning work on gwiki.\n",
        );
        seed_session(
            root,
            "s-other-day",
            TIMESTAMP,
            Some("2026-07-03T12:00:00Z"),
            "Yesterday's work.\n",
        );
        // Same-day non-session sources never join a recap.
        SourceManifest::update(root, |manifest| {
            manifest.entries.push(SourceRecord {
                kind: SourceKind::Markdown,
                ..session_record("not-a-session", "2026-07-04T10:00:00Z")
            });
            Ok(true)
        })
        .expect("seed non-session entry");

        let mut generate = cited_generator("knowledge/sources/s-early.md");
        let report =
            run(root, scope(), target_date(), Some(&mut generate), TIMESTAMP).expect("recap run");

        assert_eq!(report.sessions_selected, 2);
        assert_eq!(report.session_ids, vec!["s-early", "s-late"]);
        assert_eq!(report.synthesis, "generated");
        assert_eq!(report.page_action, Some("created"));
        assert_eq!(report.citations_kept, 1);
        assert_eq!(
            report.page_path.as_deref(),
            Some(Path::new("recaps/2026-07-04.md"))
        );

        let page =
            fs::read_to_string(root.join("recaps/2026-07-04.md")).expect("recap page written");
        assert!(page.contains("title: \"Recap: 2026-07-04\""), "{page}");
        assert!(page.contains("  - recap\n"), "recap tag rendered: {page}");
        assert!(
            page.contains("[[knowledge/sources/s-early|Session s-early]]"),
            "citation grounded to the digest wiki link: {page}"
        );
        assert!(page.contains("## Sessions"), "{page}");
        assert!(
            page.contains("[[knowledge/sources/s-late|Session s-late]]"),
            "all selected digests listed: {page}"
        );
        assert!(
            !page.contains("s-other-day") && !page.contains("not-a-session"),
            "other days and non-session sources excluded: {page}"
        );

        let index = fs::read_to_string(root.join("_index.md")).expect("index regenerated");
        assert!(
            index.contains("Recent work: [[recaps/2026-07-04|2026-07-04]]"),
            "recent work block folded into the Overview: {index}"
        );

        let log = fs::read_to_string(root.join("log.md")).expect("log written");
        assert!(log.contains("recap_completed:"), "{log}");
    }

    #[test]
    fn recap_day_with_no_sessions_writes_nothing() {
        let temp = tempfile::tempdir().expect("tempdir");
        let root = temp.path();
        seed_session(
            root,
            "s-other-day",
            TIMESTAMP,
            Some("2026-07-03T12:00:00Z"),
            "Yesterday's work.\n",
        );

        let report = run(root, scope(), target_date(), None, TIMESTAMP).expect("recap run");

        assert_eq!(report.sessions_selected, 0);
        assert!(report.page_path.is_none());
        assert!(report.page_action.is_none());
        assert!(!root.join(RECAPS_DIRECTORY).exists(), "no recaps directory");
        assert!(!root.join("log.md").exists(), "no log line for a no-op day");
        assert!(!root.join("_index.md").exists(), "no catalog churn");
    }

    #[test]
    fn recap_selection_prefers_started_at_and_falls_back_to_fetched_at() {
        let temp = tempfile::tempdir().expect("tempdir");
        let root = temp.path();
        // In-day started_at wins over an out-of-day fetched_at.
        seed_session(
            root,
            "s-started-in-day",
            "unix-ms:1751000000000",
            Some("2026-07-04T08:00:00Z"),
            "Work.\n",
        );
        // No started_at: the in-day unix-ms fetched_at attributes the day.
        seed_session(root, "s-fetched-in-day", TIMESTAMP, None, "Work.\n");
        // Out-of-day started_at excludes even with an in-day fetched_at.
        seed_session(
            root,
            "s-started-other-day",
            TIMESTAMP,
            Some("2026-07-03T23:00:00Z"),
            "Work.\n",
        );

        let report = run(root, scope(), target_date(), None, TIMESTAMP).expect("recap run");

        assert_eq!(
            report.session_ids,
            vec!["s-fetched-in-day", "s-started-in-day"],
            "selection is by attributed day, ordered chronologically"
        );
    }

    #[test]
    fn recap_rerun_updates_existing_page_and_carries_its_body() {
        let temp = tempfile::tempdir().expect("tempdir");
        let root = temp.path();
        seed_session(
            root,
            "s-a",
            TIMESTAMP,
            Some("2026-07-04T09:00:00Z"),
            "Work.\n",
        );

        let mut generate = cited_generator("knowledge/sources/s-a.md");
        let first =
            run(root, scope(), target_date(), Some(&mut generate), TIMESTAMP).expect("first run");
        assert_eq!(first.page_action, Some("created"));

        let mut seen_existing_body = false;
        let mut second_generate = |prompt: &ExplainerPrompt| {
            seen_existing_body = prompt.user.contains("Current page content");
            Ok(ExplainerResponse {
                text: "## Overview\n\nUpdated [source: knowledge/sources/s-a.md].".to_string(),
                model: None,
                route: "test",
                tool_use_count: None,
                turns: None,
                usage: None,
            })
        };
        let second = run(
            root,
            scope(),
            target_date(),
            Some(&mut second_generate),
            TIMESTAMP,
        )
        .expect("second run");

        assert_eq!(second.page_action, Some("updated"));
        assert!(
            seen_existing_body,
            "rerun folds the existing page body into the prompt"
        );
    }

    #[test]
    fn recap_synthesis_failure_still_writes_deterministic_listing() {
        let temp = tempfile::tempdir().expect("tempdir");
        let root = temp.path();
        seed_session(
            root,
            "s-a",
            TIMESTAMP,
            Some("2026-07-04T09:00:00Z"),
            "Work.\n",
        );

        let mut generate =
            |_prompt: &ExplainerPrompt| Err::<ExplainerResponse, _>("model exploded".to_string());
        let report =
            run(root, scope(), target_date(), Some(&mut generate), TIMESTAMP).expect("recap run");

        assert_eq!(report.synthesis, "failed");
        assert!(
            report
                .notes
                .iter()
                .any(|note| note.contains("model exploded")),
            "failure recorded: {:?}",
            report.notes
        );
        let page =
            fs::read_to_string(root.join("recaps/2026-07-04.md")).expect("page still written");
        assert!(page.contains("Synthesis was unavailable"), "{page}");
        assert!(
            page.contains("[[knowledge/sources/s-a|Session s-a]]"),
            "deterministic listing survives: {page}"
        );
    }

    #[test]
    fn recap_skipped_synthesis_writes_deterministic_page() {
        let temp = tempfile::tempdir().expect("tempdir");
        let root = temp.path();
        seed_session(
            root,
            "s-a",
            TIMESTAMP,
            Some("2026-07-04T09:00:00Z"),
            "Work.\n",
        );

        let report = run(root, scope(), target_date(), None, TIMESTAMP).expect("recap run");

        assert_eq!(report.synthesis, "skipped");
        assert_eq!(report.page_action, Some("created"));
        let page = fs::read_to_string(root.join("recaps/2026-07-04.md")).expect("page written");
        assert!(page.contains("## Sessions"), "{page}");
    }

    #[test]
    fn parse_instant_handles_unix_ms_rfc3339_and_bare_dates() {
        assert_eq!(
            parse_instant(TIMESTAMP)
                .expect("unix-ms parses")
                .date_naive(),
            target_date()
        );
        assert_eq!(
            parse_instant("2026-07-04T23:59:59-01:00")
                .expect("rfc3339 parses")
                .date_naive(),
            NaiveDate::from_ymd_opt(2026, 7, 5).expect("valid date"),
            "offsets normalize to UTC"
        );
        assert_eq!(
            parse_instant("2026-07-04 (approximate)")
                .expect("bare date prefix parses")
                .date_naive(),
            target_date()
        );
        assert!(parse_instant("unix-ms:not-a-number").is_none());
        assert!(parse_instant("last tuesday").is_none());
        assert!(parse_instant("").is_none());
    }
}