gobby-wiki 0.2.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
use std::collections::BTreeMap;
use std::fmt;
use std::path::{Component, Path, PathBuf};

use gobby_core::indexing::{
    Chunk, IndexEvent, WalkerSettings, file_content_hash, index_events_from_hashes,
};

use crate::links as wiki_links;
use crate::store::{
    StoreError, WikiChunk, WikiDocument, WikiDocumentKind, WikiIndexStore, WikiIngestion,
    WikiIngestionEvent, WikiLink, WikiSource, configured_memory_index_limit_bytes,
};

#[derive(Debug)]
pub enum IndexError {
    Io(std::io::Error),
    Walk(String),
    Store(StoreError),
    PathOutsideVault { path: PathBuf, vault_root: PathBuf },
    MemoryIndexTooLarge { total_bytes: u64, limit_bytes: u64 },
}

impl fmt::Display for IndexError {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        match self {
            Self::Io(error) => write!(f, "wiki index I/O failed: {error}"),
            Self::Walk(error) => write!(f, "wiki index walk failed: {error}"),
            Self::Store(error) => write!(f, "{error}"),
            Self::PathOutsideVault { path, vault_root } => write!(
                f,
                "wiki index path {} is outside vault {}",
                path.display(),
                vault_root.display()
            ),
            Self::MemoryIndexTooLarge {
                total_bytes,
                limit_bytes,
            } => write!(
                f,
                "wiki memory index input is {total_bytes} bytes, exceeding {limit_bytes} bytes from GWIKI_MAX_MEMORY_INDEX_BYTES"
            ),
        }
    }
}

impl std::error::Error for IndexError {
    fn source(&self) -> Option<&(dyn std::error::Error + 'static)> {
        match self {
            Self::Io(source) => Some(source),
            Self::Store(source) => Some(source),
            _ => None,
        }
    }
}

impl From<StoreError> for IndexError {
    fn from(error: StoreError) -> Self {
        Self::Store(error)
    }
}

impl From<std::io::Error> for IndexError {
    fn from(error: std::io::Error) -> Self {
        Self::Io(error)
    }
}

pub fn index_vault(
    vault_root: impl AsRef<Path>,
    store: &mut impl WikiIndexStore,
) -> Result<(), IndexError> {
    let vault_root = vault_root.as_ref();
    let previous_hashes = store.indexed_hashes()?;
    let current_hashes = discover_indexable_hashes(vault_root)?;

    for event in index_events_from_hashes(&previous_hashes, &current_hashes) {
        match event {
            IndexEvent::Added(path) => {
                index_file(
                    vault_root,
                    store,
                    path,
                    WikiIngestionEvent::Added,
                    &current_hashes,
                )?;
            }
            IndexEvent::Changed(path) => {
                index_file(
                    vault_root,
                    store,
                    path,
                    WikiIngestionEvent::Changed,
                    &current_hashes,
                )?;
            }
            IndexEvent::Deleted(path) => {
                store.delete_derived_rows(&path)?;
                store.record_ingestion(WikiIngestion {
                    path,
                    event: WikiIngestionEvent::Deleted,
                    content_hash: None,
                })?;
            }
            IndexEvent::Unchanged(path) => {
                let content_hash = current_hashes
                    .get(&path)
                    .or_else(|| previous_hashes.get(&path))
                    .cloned();
                store.record_ingestion(WikiIngestion {
                    path,
                    event: WikiIngestionEvent::Unchanged,
                    content_hash,
                })?;
            }
            IndexEvent::Skipped { path, .. } => {
                store.record_ingestion(WikiIngestion {
                    path,
                    event: WikiIngestionEvent::Skipped,
                    content_hash: None,
                })?;
            }
        }
    }

    Ok(())
}

fn discover_indexable_hashes(vault_root: &Path) -> Result<BTreeMap<PathBuf, String>, IndexError> {
    discover_indexable_hashes_with_limit(vault_root, configured_memory_index_limit_bytes())
}

fn discover_indexable_hashes_with_limit(
    vault_root: &Path,
    memory_limit: Option<u64>,
) -> Result<BTreeMap<PathBuf, String>, IndexError> {
    let mut current_hashes = BTreeMap::new();
    let walker = WalkerSettings::new(vault_root).into_walker().build();
    let mut total_indexable_bytes = 0u64;

    for entry in walker {
        let entry = entry.map_err(|error| IndexError::Walk(error.to_string()))?;
        let path = entry.path();
        if !path.is_file() {
            continue;
        }

        let relative = path
            .strip_prefix(vault_root)
            .map_err(|_| IndexError::PathOutsideVault {
                path: path.to_path_buf(),
                vault_root: vault_root.to_path_buf(),
            })?;

        if is_indexable_vault_path(relative) {
            total_indexable_bytes = total_indexable_bytes.saturating_add(path.metadata()?.len());
            if let Some(limit_bytes) = memory_limit
                && total_indexable_bytes > limit_bytes
            {
                return Err(IndexError::MemoryIndexTooLarge {
                    total_bytes: total_indexable_bytes,
                    limit_bytes,
                });
            }
            current_hashes.insert(relative.to_path_buf(), file_content_hash(path)?);
        }
    }

    Ok(current_hashes)
}

fn index_file(
    vault_root: &Path,
    store: &mut impl WikiIndexStore,
    path: PathBuf,
    event: WikiIngestionEvent,
    current_hashes: &BTreeMap<PathBuf, String>,
) -> Result<(), IndexError> {
    let content_hash = current_hashes
        .get(&path)
        .ok_or_else(|| {
            IndexError::Walk(format!(
                "index event path {} was missing from current hash snapshot",
                path.display()
            ))
        })?
        .clone();
    let body = std::fs::read_to_string(vault_root.join(&path))?;
    let kind = document_kind(&path).ok_or_else(|| {
        IndexError::Walk(format!(
            "indexable path {} had no document kind",
            path.display()
        ))
    })?;
    let parsed = parse_wiki_document(&path, kind, content_hash.clone(), body);

    store.upsert_document(parsed.document)?;
    store.replace_chunks(&path, parsed.chunks)?;
    store.replace_links(&path, parsed.links)?;
    store.upsert_source(parsed.source)?;
    store.record_file_hash(path.clone(), content_hash.clone())?;
    store.record_ingestion(WikiIngestion {
        path,
        event,
        content_hash: Some(content_hash),
    })?;

    Ok(())
}

fn is_indexable_vault_path(path: &Path) -> bool {
    if path == Path::new("raw").join("INDEX.md") {
        return true;
    }

    if path.extension().and_then(|extension| extension.to_str()) != Some("md") {
        return false;
    }

    let components = normal_components(path);
    matches!(
        components.as_slice(),
        ["wiki", "sources", ..]
            | ["wiki", "concepts", ..]
            | ["wiki", "topics", ..]
            | ["wiki", "code", ..]
    )
}

fn document_kind(path: &Path) -> Option<WikiDocumentKind> {
    if path == Path::new("raw").join("INDEX.md") {
        return Some(WikiDocumentKind::SourceCatalog);
    }

    match normal_components(path).as_slice() {
        ["wiki", "sources", ..] => Some(WikiDocumentKind::SourceNote),
        ["wiki", "concepts", ..] => Some(WikiDocumentKind::Concept),
        ["wiki", "topics", ..] => Some(WikiDocumentKind::Topic),
        // `wiki/code/**` stores generated code documentation, not source code.
        ["wiki", "code", ..] => Some(WikiDocumentKind::CodeDoc),
        _ => None,
    }
}

fn normal_components(path: &Path) -> Vec<&str> {
    path.components()
        .filter_map(|component| match component {
            Component::Normal(value) => value.to_str(),
            _ => None,
        })
        .collect()
}

struct ParsedWikiDocument {
    document: WikiDocument,
    chunks: Vec<WikiChunk>,
    links: Vec<WikiLink>,
    source: WikiSource,
}

fn parse_wiki_document(
    path: &Path,
    kind: WikiDocumentKind,
    content_hash: String,
    body: String,
) -> ParsedWikiDocument {
    let title = first_heading(&body);
    let core_chunks = chunks_for_markdown(path, title.clone(), &body);
    let chunks = core_chunks
        .into_iter()
        .enumerate()
        .map(|(chunk_index, chunk)| WikiChunk {
            path: chunk.file_path,
            chunk_index,
            byte_start: chunk.byte_start,
            byte_end: chunk.byte_end,
            heading: chunk.heading,
            content: body
                .get(chunk.byte_start..chunk.byte_end)
                .unwrap_or_default()
                .to_string(),
        })
        .collect();
    let links = extract_links(path, &body);

    ParsedWikiDocument {
        document: WikiDocument {
            path: path.to_path_buf(),
            kind,
            title,
            content_hash: content_hash.clone(),
            body,
        },
        chunks,
        links,
        source: WikiSource {
            path: path.to_path_buf(),
            document_path: path.to_path_buf(),
            kind,
            content_hash,
        },
    }
}

fn chunks_for_markdown(path: &Path, fallback_heading: Option<String>, body: &str) -> Vec<Chunk> {
    let heading_spans = heading_spans(body);
    if heading_spans.is_empty() {
        return vec![Chunk {
            file_path: path.to_path_buf(),
            byte_start: 0,
            byte_end: body.len(),
            heading: fallback_heading,
            metadata: serde_json::Value::Null,
        }];
    }

    heading_spans
        .iter()
        .enumerate()
        .map(|(index, (byte_start, heading))| {
            let byte_end = heading_spans
                .get(index + 1)
                .map(|(next_start, _)| *next_start)
                .unwrap_or(body.len());
            Chunk {
                file_path: path.to_path_buf(),
                byte_start: *byte_start,
                byte_end,
                heading: Some(heading.clone()),
                metadata: serde_json::Value::Null,
            }
        })
        .collect()
}

fn heading_spans(body: &str) -> Vec<(usize, String)> {
    let mut spans = Vec::new();
    let mut byte_start = 0;

    for line in body.split_inclusive('\n') {
        if let Some(heading) = parse_heading(line) {
            spans.push((byte_start, heading));
        }
        byte_start += line.len();
    }

    spans
}

fn first_heading(body: &str) -> Option<String> {
    body.lines().find_map(parse_heading)
}

fn parse_heading(line: &str) -> Option<String> {
    crate::markdown::parse_atx_heading(line)
        .map(|(_, heading)| heading)
        .filter(|heading| !heading.is_empty())
}

fn extract_links(path: &Path, body: &str) -> Vec<WikiLink> {
    let mut links = wiki_links::extract_links(body, std::iter::empty::<&str>())
        .into_iter()
        .map(|link| WikiLink {
            path: path.to_path_buf(),
            target: link.target,
            alias: link.alias,
            byte_start: link.byte_start,
            byte_end: link.byte_end,
        })
        .collect::<Vec<_>>();
    links.sort_by_key(|link| link.byte_start);
    links
}

#[cfg(test)]
mod tests {
    use std::path::{Path, PathBuf};

    use gobby_core::indexing::content_hash;

    use super::*;
    use crate::store::{
        MemoryWikiStore, WikiDocument, WikiDocumentKind, WikiIngestionEvent, WikiLink, WikiSource,
    };

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

    fn seed_derived_rows(store: &mut MemoryWikiStore, relative: &str) {
        let path = PathBuf::from(relative);
        store.documents.insert(
            path.clone(),
            WikiDocument {
                path: path.clone(),
                kind: WikiDocumentKind::Topic,
                title: Some("Stale".to_string()),
                content_hash: "old".to_string(),
                body: "stale".to_string(),
            },
        );
        store.chunks.insert(path.clone(), Vec::new());
        store.links.insert(
            path.clone(),
            vec![WikiLink {
                path: path.clone(),
                target: "Old".to_string(),
                alias: None,
                byte_start: 0,
                byte_end: 7,
            }],
        );
        store.sources.insert(
            path.clone(),
            WikiSource {
                path: path.clone(),
                document_path: path.clone(),
                kind: WikiDocumentKind::Topic,
                content_hash: "old".to_string(),
            },
        );
        store.file_hashes.insert(path, "old".to_string());
    }

    #[test]
    fn index_writer_upserts_documents_chunks_links_sources_and_ingestions() {
        let tempdir = tempfile::tempdir().expect("tempdir");
        write_file(
            tempdir.path(),
            "wiki/topics/rust.md",
            "# Rust\n\nSee [[Ownership|ownership]] and [Cargo](wiki/concepts/cargo.md).\n",
        );
        let mut store = MemoryWikiStore::default();

        index_vault(tempdir.path(), &mut store).expect("index vault");

        let path = PathBuf::from("wiki/topics/rust.md");
        assert_eq!(store.documents[&path].kind, WikiDocumentKind::Topic);
        assert!(!store.chunks[&path].is_empty());
        assert_eq!(store.links[&path].len(), 2);
        assert_eq!(store.sources[&path].kind, WikiDocumentKind::Topic);
        assert_eq!(store.ingestions[0].event, WikiIngestionEvent::Added);
    }

    #[test]
    fn deleted_file_removes_derived_rows_only() {
        let tempdir = tempfile::tempdir().expect("tempdir");
        write_file(tempdir.path(), "raw/source.txt", "raw source stays\n");
        let raw_before = std::fs::read_to_string(tempdir.path().join("raw/source.txt"))
            .expect("read raw source");
        let mut store = MemoryWikiStore::default();
        seed_derived_rows(&mut store, "wiki/topics/stale.md");

        index_vault(tempdir.path(), &mut store).expect("index vault");

        let stale = PathBuf::from("wiki/topics/stale.md");
        assert!(!store.documents.contains_key(&stale));
        assert!(!store.chunks.contains_key(&stale));
        assert!(!store.links.contains_key(&stale));
        assert!(!store.sources.contains_key(&stale));
        assert!(!store.file_hashes.contains_key(&stale));
        assert_eq!(store.deleted_paths, vec![stale]);
        assert_eq!(
            std::fs::read_to_string(tempdir.path().join("raw/source.txt"))
                .expect("read raw source after indexing"),
            raw_before
        );
    }

    #[test]
    fn raw_sources_are_immutable() {
        let tempdir = tempfile::tempdir().expect("tempdir");
        write_file(tempdir.path(), "raw/article.txt", "original raw bytes\n");
        write_file(
            tempdir.path(),
            "raw/INDEX.md",
            "# Sources\n\n- article.txt\n",
        );
        let raw_path = tempdir.path().join("raw/article.txt");
        let raw_before = std::fs::read_to_string(&raw_path).expect("read raw source");
        let mut store = MemoryWikiStore::default();

        index_vault(tempdir.path(), &mut store).expect("index vault");

        assert_eq!(
            std::fs::read_to_string(raw_path).expect("read raw source after indexing"),
            raw_before
        );
        assert!(store.documents.contains_key(&PathBuf::from("raw/INDEX.md")));
        assert!(
            !store
                .documents
                .contains_key(&PathBuf::from("raw/article.txt"))
        );
    }

    #[test]
    fn unchanged_files_are_skipped() {
        let tempdir = tempfile::tempdir().expect("tempdir");
        let body = "# Stable\n\nNo changes.\n";
        write_file(tempdir.path(), "wiki/concepts/stable.md", body);
        let mut store = MemoryWikiStore::default();

        index_vault(tempdir.path(), &mut store).expect("first index");
        assert_eq!(
            store.file_hashes[&PathBuf::from("wiki/concepts/stable.md")],
            content_hash(body.as_bytes())
        );
        let document_upserts = store.document_upserts;
        let chunk_replacements = store.chunk_replacements;
        let link_replacements = store.link_replacements;
        let source_upserts = store.source_upserts;

        index_vault(tempdir.path(), &mut store).expect("second index");

        assert_eq!(store.document_upserts, document_upserts);
        assert_eq!(store.chunk_replacements, chunk_replacements);
        assert_eq!(store.link_replacements, link_replacements);
        assert_eq!(store.source_upserts, source_upserts);
        assert_eq!(
            store
                .ingestions
                .last()
                .expect("unchanged ingestion recorded")
                .event,
            WikiIngestionEvent::Unchanged
        );
    }

    #[test]
    fn indexes_codedocs_with_provenance() {
        let tempdir = tempfile::tempdir().expect("tempdir");
        write_file(
            tempdir.path(),
            "wiki/code/crates/gwiki/src/indexer.md",
            "---\nsource_files:\n  - crates/gwiki/src/indexer.rs\n---\n# Indexer\n\nSee [[wiki/code/crates/gwiki/src/store|store docs]].\n",
        );
        let mut store = MemoryWikiStore::default();

        index_vault(tempdir.path(), &mut store).expect("index vault");

        let path = PathBuf::from("wiki/code/crates/gwiki/src/indexer.md");
        let document = store.documents.get(&path).expect("codedoc document");
        assert_eq!(document.kind, WikiDocumentKind::CodeDoc);
        assert!(document.body.contains("source_files:"));
        assert_eq!(store.sources[&path].kind, WikiDocumentKind::CodeDoc);
        assert_eq!(store.links[&path].len(), 1);
        assert_eq!(
            store.links[&path][0].target,
            "wiki/code/crates/gwiki/src/store"
        );
        assert_eq!(store.links[&path][0].alias.as_deref(), Some("store docs"));
        assert_eq!(store.ingestions[0].event, WikiIngestionEvent::Added);
    }

    #[test]
    fn codedoc_tree_indexes_idempotently() {
        let tempdir = tempfile::tempdir().expect("tempdir");
        write_file(tempdir.path(), "wiki/code/a.md", "# A\n\nSee [[B]].\n");
        write_file(tempdir.path(), "wiki/code/nested/b.md", "# B\n\nStable.\n");
        write_file(
            tempdir.path(),
            "wiki/code/nested/ignored.txt",
            "not markdown\n",
        );
        let mut store = MemoryWikiStore::default();

        index_vault(tempdir.path(), &mut store).expect("first index");

        let a_path = PathBuf::from("wiki/code/a.md");
        let b_path = PathBuf::from("wiki/code/nested/b.md");
        assert_eq!(store.documents[&a_path].kind, WikiDocumentKind::CodeDoc);
        assert_eq!(store.documents[&b_path].kind, WikiDocumentKind::CodeDoc);
        assert!(
            !store
                .documents
                .contains_key(&PathBuf::from("wiki/code/nested/ignored.txt"))
        );
        assert_eq!(store.document_upserts, 2);
        assert_eq!(store.ingestions.len(), 2);

        write_file(tempdir.path(), "wiki/code/a.md", "# A\n\nChanged.\n");
        index_vault(tempdir.path(), &mut store).expect("second index");

        assert_eq!(store.document_upserts, 3);
        assert_eq!(store.chunk_replacements, 3);
        assert_eq!(store.link_replacements, 3);
        assert_eq!(store.source_upserts, 3);
        assert_eq!(store.ingestions[2].path, a_path);
        assert_eq!(store.ingestions[2].event, WikiIngestionEvent::Changed);
        assert_eq!(store.ingestions[3].path, b_path);
        assert_eq!(store.ingestions[3].event, WikiIngestionEvent::Unchanged);
        assert_eq!(store.links[&b_path].len(), 0);
    }

    #[test]
    fn memory_index_limit_rejects_large_vaults() {
        let temp = tempfile::tempdir().expect("tempdir");
        write_file(temp.path(), "wiki/topics/large.md", "# Large\n\nabcdef\n");

        let error = discover_indexable_hashes_with_limit(temp.path(), Some(4))
            .expect_err("limit rejects vault");

        assert!(matches!(error, IndexError::MemoryIndexTooLarge { .. }));
    }
}