gobby-wiki 0.7.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
use std::collections::HashSet;
use std::fs::{self, File};
use std::io::{ErrorKind, Read};
use std::path::{Path, PathBuf};

use flate2::read::GzDecoder;

use crate::WikiError;
use crate::ingest::{IngestResult, index_after_ingest};
use crate::paths::{derived_markdown_path, raw_source_path};
use crate::sources::{SourceKind, SourceManifest, SourceRecord};
use crate::store::WikiIndexStore;

use super::session::{
    SessionFileSnapshot, SessionSummarizer, SessionWikiFileSnapshot,
    ingest_session_file_without_index, ingest_session_wiki_file_without_index,
};

/// Compound suffix of daemon-synthesized session wiki files in the wiki dir.
const SESSION_WIKI_SUFFIX: &str = ".md";
/// Compound suffix of raw session transcript archives in the archive dir.
const SESSION_ARCHIVE_SUFFIX: &str = ".jsonl.gz";

#[derive(Debug, Clone, PartialEq, Eq)]
pub(crate) struct AcceptedSessionArchive {
    pub(crate) archive_path: PathBuf,
    pub(crate) result: IngestResult,
}

#[derive(Debug, Clone, PartialEq, Eq)]
pub(crate) struct SkippedSessionArchive {
    pub(crate) archive_path: PathBuf,
    pub(crate) content_hash: String,
    pub(crate) reason: String,
}

/// A previously ingested session page whose source vanished from discovery and
/// was reconciled away (manifest entry + derived/raw files removed).
#[derive(Debug, Clone, PartialEq, Eq)]
pub(crate) struct ReconciledSessionArchive {
    pub(crate) source_id: String,
    pub(crate) canonical_location: String,
    pub(crate) content_hash: String,
}

#[derive(Debug, Clone, PartialEq, Eq)]
pub(crate) struct SessionArchiveFailure {
    pub(crate) archive_path: PathBuf,
    pub(crate) code: String,
    pub(crate) message: String,
}

#[derive(Debug, Clone, PartialEq, Eq)]
pub(crate) struct SessionArchiveBatchIngest {
    pub(crate) archive_dir: PathBuf,
    pub(crate) scanned: usize,
    pub(crate) accepted: Vec<AcceptedSessionArchive>,
    pub(crate) skipped: Vec<SkippedSessionArchive>,
    pub(crate) failed: Vec<SessionArchiveFailure>,
    pub(crate) reconciled: Vec<ReconciledSessionArchive>,
}

impl SessionArchiveBatchIngest {
    pub(crate) fn status(&self) -> &'static str {
        if !self.accepted.is_empty() {
            return if self.failed.is_empty() {
                "ingested"
            } else {
                "partial"
            };
        }
        if !self.failed.is_empty() {
            // No successful ingest. A reconcile/skip riding alongside failures is
            // a mixed (partial) outcome; pure failure stays "failed".
            return if self.reconciled.is_empty() && self.skipped.is_empty() {
                "failed"
            } else {
                "partial"
            };
        }
        if !self.reconciled.is_empty() {
            // Deletions reconciled with nothing newly ingested.
            return "reconciled";
        }
        if !self.skipped.is_empty() {
            return "skipped";
        }
        "empty"
    }

    /// True when the batch changed vault state — newly accepted sessions or
    /// reconciled deletions — so downstream index/Qdrant/Falkor sync must run.
    pub(crate) fn has_changes(&self) -> bool {
        !self.accepted.is_empty() || !self.reconciled.is_empty()
    }

    pub(crate) fn exit_code(&self) -> u8 {
        u8::from(!self.failed.is_empty() && self.accepted.is_empty())
    }
}

/// One discovered session source: a daemon-synthesized wiki page (authoritative)
/// or a raw transcript archive (fallback). Synthesis is sourced first so it can
/// supersede the raw page for the same session.
enum SessionSourceFile {
    Synthesis(PathBuf),
    RawArchive(PathBuf),
}

/// How to treat a raw transcript archive that has no daemon synthesis. Daemon
/// synthesis pages are always ingested regardless of this mode.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub(crate) enum RawArchiveMode {
    /// Ignore raw archives (default): only daemon synthesis pages are ingested.
    Skip,
    /// Ingest a structural skeleton page from the raw transcript (`--raw`).
    Skeleton,
    /// Generate a daemon-equivalent summary via the shared handoff prompt,
    /// falling back to the skeleton when AI is unavailable (`--summarize`).
    Summarize,
}

pub(crate) fn sync_session_transcript_archives(
    vault_root: &Path,
    store: &mut impl WikiIndexStore,
    archive_dir: &Path,
    wiki_dir: &Path,
    limit: Option<usize>,
    raw_mode: RawArchiveMode,
    fetched_at: &str,
) -> Result<SessionArchiveBatchIngest, WikiError> {
    if matches!(limit, Some(0)) {
        return Err(WikiError::InvalidInput {
            field: "sync-sessions.limit",
            message: "limit must be greater than 0".to_string(),
        });
    }

    // Daemon synthesis is the authoritative source. Collect it first and key
    // suppression on the bare external id (the `.md` stem) so a raw archive whose
    // session already has a synthesized page is never re-parsed into a second page.
    let synthesis_paths = session_wiki_paths(wiki_dir)?;
    let synthesized = synthesis_paths
        .iter()
        .filter_map(|path| session_external_id(path, SESSION_WIKI_SUFFIX))
        .collect::<HashSet<_>>();
    let archive_paths = session_archive_paths(archive_dir)?;

    // Present session-source locations across the FULL pre-limit discovery.
    // Deletion reconciliation diffs the manifest against this set, so it must be
    // built before `limit` truncation — otherwise a bounded run would treat
    // every session beyond the cap as vanished and false-delete it.
    let present_locations = present_session_locations(&synthesized, &archive_paths);

    let mut work = synthesis_paths
        .into_iter()
        .map(SessionSourceFile::Synthesis)
        .collect::<Vec<_>>();
    work.extend(archive_paths.into_iter().map(SessionSourceFile::RawArchive));
    if let Some(limit) = limit {
        work.truncate(limit);
    }

    // One manifest snapshot drives dedup, supersede, and reconcile decisions for
    // the whole batch; removals below re-read under the manifest lock as needed.
    let manifest = SourceManifest::read(vault_root)?;
    let mut known_session_hashes = manifest
        .entries
        .iter()
        .filter(|entry| entry.kind == SourceKind::Session)
        .map(|entry| (entry.canonical_location.clone(), entry.content_hash.clone()))
        .collect::<HashSet<(String, String)>>();
    // Session pages already in the manifest. The `--summarize` path is gated on
    // page *existence* (not content hash) because LLM output is nondeterministic:
    // re-summarizing every run would superfluously regenerate and supersede. A
    // later daemon synthesis still supersedes a standalone page via the Synthesis
    // arm, which does not consult this set.
    let existing_session_pages = manifest
        .entries
        .iter()
        .filter(|entry| entry.kind == SourceKind::Session)
        .map(|entry| entry.canonical_location.clone())
        .collect::<HashSet<String>>();
    // Resolve AI once for the whole batch; `None` means standalone summarization
    // is unavailable (mode not Summarize, AI routed off, or no `ai` feature) and
    // raw archives fall back to skeleton pages.
    let summarizer = SessionSummarizer::resolve(raw_mode == RawArchiveMode::Summarize);
    let scanned = work.len();
    let mut accepted = Vec::new();
    let mut skipped = Vec::new();
    let mut failed = Vec::new();

    for item in work {
        match item {
            SessionSourceFile::Synthesis(path) => {
                let Some(external_id) = session_external_id(&path, SESSION_WIKI_SUFFIX) else {
                    failed.push(SessionArchiveFailure::new(
                        &path,
                        "session_wiki_name",
                        "session wiki file name has no external id stem",
                    ));
                    continue;
                };
                let bytes = match fs::read(&path) {
                    Ok(bytes) => bytes,
                    Err(error) => {
                        failed.push(SessionArchiveFailure::new(
                            &path,
                            "session_wiki_read",
                            format!("failed to read session wiki file: {error}"),
                        ));
                        continue;
                    }
                };
                let content_hash = gobby_core::indexing::content_hash(&bytes);
                // Dedup per canonical location, not globally: identical content
                // at different session locations must ingest independently.
                let canonical_location = format!("session:{external_id}");
                if known_session_hashes
                    .contains(&(canonical_location.clone(), content_hash.clone()))
                {
                    skipped.push(SkippedSessionArchive {
                        archive_path: path,
                        content_hash,
                        reason: "content_hash_already_ingested".to_string(),
                    });
                    continue;
                }
                let snapshot = SessionWikiFileSnapshot {
                    external_id: external_id.clone(),
                    path: path.clone(),
                    fetched_at: fetched_at.to_string(),
                    bytes,
                };
                match ingest_session_wiki_file_without_index(vault_root, snapshot) {
                    Ok(result) => {
                        let archive_path = path.clone();
                        let new_id = result.record.id.clone();
                        known_session_hashes.insert((
                            result.record.canonical_location.clone(),
                            result.record.content_hash.clone(),
                        ));
                        accepted.push(AcceptedSessionArchive {
                            archive_path: path,
                            result,
                        });
                        if let Err(error) =
                            supersede_session_page(vault_root, &manifest, &external_id, &new_id)
                        {
                            failed
                                .push(SessionArchiveFailure::from_wiki_error(&archive_path, error));
                        }
                    }
                    Err(error) => failed.push(SessionArchiveFailure::from_wiki_error(&path, error)),
                }
            }
            SessionSourceFile::RawArchive(path) => {
                let external_id = session_external_id(&path, SESSION_ARCHIVE_SUFFIX);
                if let Some(external_id) = &external_id
                    && synthesized.contains(external_id)
                {
                    skipped.push(SkippedSessionArchive {
                        archive_path: path,
                        content_hash: String::new(),
                        reason: "superseded_by_synthesis".to_string(),
                    });
                    continue;
                }
                // Only `Skip` mode ignores raw archives; both Skeleton and
                // Summarize process them (Summarize even without --raw).
                if raw_mode == RawArchiveMode::Skip {
                    skipped.push(SkippedSessionArchive {
                        archive_path: path,
                        content_hash: String::new(),
                        reason: "raw_fallback_disabled".to_string(),
                    });
                    continue;
                }
                let bytes = match read_gzipped_archive(&path) {
                    Ok(bytes) => bytes,
                    Err(failure) => {
                        failed.push(failure);
                        continue;
                    }
                };
                let content_hash = gobby_core::indexing::content_hash(&bytes);
                let file_name = archive_file_name(&path);
                // Key the raw fallback on the canonical `session:{external_id}` too,
                // so a later synthesis (or re-run) supersedes this page. `file_name`
                // still seeds the title and the `source_archive` provenance field.
                let external_id = external_id.unwrap_or_else(|| file_name.clone());
                let canonical_location = format!("session:{external_id}");

                // Standalone summary path: generate the daemon-format page once per
                // session. Gated on page existence (idempotent) because LLM output
                // is nondeterministic; a later daemon synthesis still supersedes it.
                if raw_mode == RawArchiveMode::Summarize {
                    if existing_session_pages.contains(&canonical_location) {
                        skipped.push(SkippedSessionArchive {
                            archive_path: path,
                            content_hash,
                            reason: "session_page_present".to_string(),
                        });
                        continue;
                    }
                    if let Some(summarizer) = &summarizer
                        && let Some(md_bytes) =
                            summarizer.summarize_archive(&path, &bytes, &external_id)
                    {
                        let snapshot = SessionWikiFileSnapshot {
                            external_id: external_id.clone(),
                            path: path.clone(),
                            fetched_at: fetched_at.to_string(),
                            bytes: md_bytes,
                        };
                        match ingest_session_wiki_file_without_index(vault_root, snapshot) {
                            Ok(result) => {
                                let archive_path = path.clone();
                                let new_id = result.record.id.clone();
                                known_session_hashes.insert((
                                    result.record.canonical_location.clone(),
                                    result.record.content_hash.clone(),
                                ));
                                accepted.push(AcceptedSessionArchive {
                                    archive_path: path,
                                    result,
                                });
                                if let Err(error) = supersede_session_page(
                                    vault_root,
                                    &manifest,
                                    &external_id,
                                    &new_id,
                                ) {
                                    failed.push(SessionArchiveFailure::from_wiki_error(
                                        &archive_path,
                                        error,
                                    ));
                                }
                            }
                            Err(error) => {
                                failed.push(SessionArchiveFailure::from_wiki_error(&path, error));
                            }
                        }
                        continue;
                    }
                    // Summary unavailable (AI off/empty/error): fall through to the
                    // skeleton below so the session still lands as a page.
                }

                // Skeleton page: the default --raw path, or the summarize fallback.
                // Dedup per canonical location keeps raw re-ingests idempotent.
                if known_session_hashes
                    .contains(&(canonical_location.clone(), content_hash.clone()))
                {
                    skipped.push(SkippedSessionArchive {
                        archive_path: path,
                        content_hash,
                        reason: "content_hash_already_ingested".to_string(),
                    });
                    continue;
                }
                let snapshot = SessionFileSnapshot {
                    location: canonical_location,
                    file_name,
                    fetched_at: fetched_at.to_string(),
                    path: path.clone(),
                    bytes,
                };
                match ingest_session_file_without_index(vault_root, snapshot) {
                    Ok(result) => {
                        let archive_path = path.clone();
                        let new_id = result.record.id.clone();
                        known_session_hashes.insert((
                            result.record.canonical_location.clone(),
                            result.record.content_hash.clone(),
                        ));
                        accepted.push(AcceptedSessionArchive {
                            archive_path: path,
                            result,
                        });
                        if let Err(error) =
                            supersede_session_page(vault_root, &manifest, &external_id, &new_id)
                        {
                            failed
                                .push(SessionArchiveFailure::from_wiki_error(&archive_path, error));
                        }
                    }
                    Err(error) => failed.push(SessionArchiveFailure::from_wiki_error(&path, error)),
                }
            }
        }
    }

    // Reconcile deletions: any manifest session whose source vanished from the
    // full discovery is removed here so the index cascade (IndexEvent::Deleted →
    // delete_derived_rows) prunes PostgreSQL rows, Qdrant points, and Falkor
    // nodes downstream. Driven by the pre-loop snapshot against the full
    // pre-limit present set so a `--limit` run never false-deletes uncapped
    // sessions. Entries supersede already removed return `None` here (no-op).
    let mut reconciled = Vec::new();
    if !present_locations.is_empty() {
        for entry in &manifest.entries {
            if entry.kind != SourceKind::Session
                || present_locations.contains(&entry.canonical_location)
            {
                continue;
            }
            if let Some(removed) = remove_session_page(vault_root, &entry.id)? {
                reconciled.push(ReconciledSessionArchive {
                    source_id: removed.id,
                    canonical_location: removed.canonical_location,
                    content_hash: removed.content_hash,
                });
            }
        }
    }

    let batch = SessionArchiveBatchIngest {
        archive_dir: archive_dir.to_path_buf(),
        scanned,
        accepted,
        skipped,
        failed,
        reconciled,
    };

    // Deletions alone still need downstream sync, so index on any change
    // (accepted or reconciled), not just newly accepted sessions.
    if batch.has_changes() {
        index_after_ingest(vault_root, store)?;
    }

    Ok(batch)
}

/// Bare session external id from a session file name, stripping the *full*
/// compound suffix (`abc.jsonl.gz` → `abc`, not `abc.jsonl` as `Path::file_stem`
/// would). `None` for names without the suffix or with an empty stem.
fn session_external_id(path: &Path, suffix: &str) -> Option<String> {
    let name = path.file_name().and_then(|name| name.to_str())?;
    let stem = name.strip_suffix(suffix)?;
    (!stem.is_empty()).then(|| stem.to_string())
}

/// Lists daemon-synthesized session wiki files (`*.md`) in `wiki_dir`, sorted.
/// A missing directory is treated as empty.
fn session_wiki_paths(wiki_dir: &Path) -> Result<Vec<PathBuf>, WikiError> {
    let mut paths = Vec::new();
    let entries = match fs::read_dir(wiki_dir) {
        Ok(entries) => entries,
        Err(error) if error.kind() == ErrorKind::NotFound => return Ok(paths),
        Err(error) => {
            return Err(WikiError::Io {
                action: "read session wiki directory",
                path: Some(wiki_dir.to_path_buf()),
                source: error,
            });
        }
    };
    for entry in entries {
        let entry = entry.map_err(|error| WikiError::Io {
            action: "read session wiki directory entry",
            path: Some(wiki_dir.to_path_buf()),
            source: error,
        })?;
        let path = entry.path();
        if path.is_file() && is_session_wiki_md(&path) {
            paths.push(path);
        }
    }
    paths.sort();
    Ok(paths)
}

fn is_session_wiki_md(path: &Path) -> bool {
    path.file_name()
        .and_then(|name| name.to_str())
        .is_some_and(|name| name.ends_with(SESSION_WIKI_SUFFIX))
}

/// Canonical session-source locations for every discovered session, across the
/// full pre-limit synthesis + archive sets. Includes the canonical
/// `session:{external_id}` form, the legacy `session_transcripts/{id}.jsonl.gz`
/// location used before #926, and the `session:{file_name}` fallback for raw
/// archives whose external id cannot be stripped — so an entry is reconciled
/// away only when none of its possible location forms is present.
fn present_session_locations(
    synthesized: &HashSet<String>,
    archive_paths: &[PathBuf],
) -> HashSet<String> {
    let mut present = HashSet::new();
    for external_id in synthesized {
        insert_session_location_forms(&mut present, external_id);
    }
    for path in archive_paths {
        match session_external_id(path, SESSION_ARCHIVE_SUFFIX) {
            Some(external_id) => insert_session_location_forms(&mut present, &external_id),
            None => {
                present.insert(format!("session:{}", archive_file_name(path)));
            }
        }
    }
    present
}

fn insert_session_location_forms(present: &mut HashSet<String>, external_id: &str) {
    present.insert(format!("session:{external_id}"));
    present.insert(format!(
        "session_transcripts/{external_id}{SESSION_ARCHIVE_SUFFIX}"
    ));
}

/// Latest-wins replacement: remove any prior page for this session — under the
/// canonical `session:{external_id}` location or the legacy raw archive-path
/// location used before #926 — whose manifest id differs from the freshly
/// ingested record. Per-location dedup (issue A) lets a legacy raw entry share
/// the new entry's content hash, so supersede keys on id/location, never the
/// hash. Driven by the pre-loop snapshot, so it needs no extra manifest read.
/// Exactly one page survives per session.
fn supersede_session_page(
    vault_root: &Path,
    snapshot: &SourceManifest,
    external_id: &str,
    new_id: &str,
) -> Result<(), WikiError> {
    let canonical = format!("session:{external_id}");
    let legacy = format!("session_transcripts/{external_id}{SESSION_ARCHIVE_SUFFIX}");
    for entry in &snapshot.entries {
        if entry.kind == SourceKind::Session
            && entry.id != new_id
            && (entry.canonical_location == canonical || entry.canonical_location == legacy)
        {
            remove_session_page(vault_root, &entry.id)?;
        }
    }
    Ok(())
}

/// Removes a session page's manifest entry and its derived/raw files crash-
/// consistently: file cleanup runs first under the manifest lock, then the entry
/// is dropped and the manifest rewritten atomically. fs + manifest are not
/// transactional; an interrupted cleanup is retried on the next run because the
/// entry still points at the orphaned files. Returns the removed record, or
/// `None` if the id was already gone (e.g. supersede beat reconcile to it).
fn remove_session_page(vault_root: &Path, id: &str) -> Result<Option<SourceRecord>, WikiError> {
    SourceManifest::remove_with_cleanup(vault_root, id, |entry| {
        remove_vault_file(vault_root, &derived_markdown_path(entry)?)?;
        remove_vault_file(vault_root, &raw_source_path(&entry.id)?)?;
        Ok(())
    })
}

/// Removes a vault-relative file if present; a missing file is not an error.
fn remove_vault_file(vault_root: &Path, relative: &Path) -> Result<(), WikiError> {
    let path = vault_root.join(relative);
    match fs::remove_file(&path) {
        Ok(()) => Ok(()),
        Err(error) if error.kind() == ErrorKind::NotFound => Ok(()),
        Err(error) => Err(WikiError::Io {
            action: "remove superseded session page",
            path: Some(path),
            source: error,
        }),
    }
}

fn session_archive_paths(archive_dir: &Path) -> Result<Vec<PathBuf>, WikiError> {
    let mut paths = Vec::new();
    let entries = match fs::read_dir(archive_dir) {
        Ok(entries) => entries,
        Err(error) if error.kind() == ErrorKind::NotFound => return Ok(paths),
        Err(error) => {
            return Err(WikiError::Io {
                action: "read session transcript archive directory",
                path: Some(archive_dir.to_path_buf()),
                source: error,
            });
        }
    };
    for entry in entries {
        let entry = entry.map_err(|error| WikiError::Io {
            action: "read session transcript archive directory entry",
            path: Some(archive_dir.to_path_buf()),
            source: error,
        })?;
        let path = entry.path();
        if path.is_file() && is_jsonl_gz(&path) {
            paths.push(path);
        }
    }
    paths.sort();
    Ok(paths)
}

fn is_jsonl_gz(path: &Path) -> bool {
    path.file_name()
        .and_then(|name| name.to_str())
        .is_some_and(|name| name.ends_with(".jsonl.gz"))
}

fn read_gzipped_archive(path: &Path) -> Result<Vec<u8>, SessionArchiveFailure> {
    let file = File::open(path).map_err(|error| {
        SessionArchiveFailure::new(path, "io", format!("failed to open archive: {error}"))
    })?;
    let mut decoder = GzDecoder::new(file);
    let mut bytes = Vec::new();
    decoder.read_to_end(&mut bytes).map_err(|error| {
        SessionArchiveFailure::new(
            path,
            "gzip_decode",
            format!("failed to decompress archive: {error}"),
        )
    })?;
    Ok(bytes)
}

fn archive_file_name(path: &Path) -> String {
    path.file_name()
        .and_then(|name| name.to_str())
        .unwrap_or("session.jsonl.gz")
        .to_string()
}

impl SessionArchiveFailure {
    fn new(path: &Path, code: impl Into<String>, message: impl Into<String>) -> Self {
        Self {
            archive_path: path.to_path_buf(),
            code: code.into(),
            message: message.into(),
        }
    }

    fn from_wiki_error(path: &Path, error: WikiError) -> Self {
        Self::new(path, error.code(), error.to_string())
    }
}

#[cfg(test)]
mod tests;