gobby-wiki 0.6.5

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
use super::*;
use crate::explainer::{ExplainerPrompt, ExplainerResponse};
use crate::provenance::ProvenanceGraph;
use crate::session::{AcceptedResearchNote, ResearchScope, ResearchSession};
use crate::synthesis::SynthesizedPage;

fn session_with_note(scope: &ResearchScope, title: &str, relative_path: &str) -> ResearchSession {
    ResearchSession {
        session_id: "research-compile-test".to_string(),
        question: "How should compile handoff work?".to_string(),
        prompt: "Compile source-grounded research".to_string(),
        scope: scope.clone(),
        source_constraints: vec!["accepted notes only".to_string()],
        agent_count: 1,
        dispatch_task_id: Some("#302".to_string()),
        dispatch: None,
        accepted_notes: vec![AcceptedResearchNote {
            title: title.to_string(),
            path: scope.root().join(relative_path),
            code_citations: Vec::new(),
            degradation: None,
        }],
        compile_state: None,
    }
}

#[test]
fn compile_bundle_contains_required_sections() {
    let temp = tempfile::tempdir().expect("tempdir");
    let scope = ResearchScope::project_for_id("project-1", temp.path());
    let note_path = scope.root().join("raw/research/compile.md");
    std::fs::create_dir_all(note_path.parent().expect("note parent")).expect("raw dir");
    std::fs::write(
        &note_path,
        "---\ntitle: Compile behavior\nsource: daemon notes\n---\n\nCitation: Example Docs, Compile API\nConflict: Workers disagree about overwrite behavior.\nGap: Missing benchmark evidence.\nAccepted chunk about durable synthesis handoff.",
    )
    .expect("note written");
    let mut session = session_with_note(&scope, "Compile behavior", "raw/research/compile.md");

    let outcome = prepare_handoff(
        &mut session,
        CompileRequest {
            topic: "Compile behavior".to_string(),
            outline: vec![
                "Durable handoff".to_string(),
                "Synthesis inputs".to_string(),
            ],
            target_page: Some(PathBuf::from("compile-behavior.md")),
            write_intent: false,
        },
    )
    .expect("compile handoff prepared");

    assert_eq!(outcome.bundle.outline.len(), 2);
    assert_eq!(outcome.bundle.accepted_sources.len(), 1);
    assert_eq!(outcome.bundle.citations, vec!["Example Docs, Compile API"]);
    assert_eq!(
        outcome.bundle.conflicting_claims,
        vec!["Workers disagree about overwrite behavior."]
    );
    assert_eq!(
        outcome.bundle.missing_evidence,
        vec!["Missing benchmark evidence."]
    );

    let rendered = std::fs::read_to_string(&outcome.bundle.path).expect("bundle written");
    assert!(rendered.contains("## Topic outline"));
    assert!(rendered.contains("## Accepted sources"));
    assert!(rendered.contains("## Citations"));
    assert!(rendered.contains("## Conflicting claims"));
    assert!(rendered.contains("## Missing evidence"));
}

#[test]
fn compile_handoff_is_non_destructive_by_default() {
    let temp = tempfile::tempdir().expect("tempdir");
    let scope = ResearchScope::project_for_id("project-1", temp.path());
    let page_path = scope.root().join("compile-behavior.md");
    std::fs::write(&page_path, "human-authored wiki page").expect("page written");
    let note_path = scope.root().join("raw/research/compile.md");
    std::fs::create_dir_all(note_path.parent().expect("note parent")).expect("raw dir");
    std::fs::write(&note_path, "Citation: Example Docs").expect("note written");
    let mut session = session_with_note(&scope, "Compile behavior", "raw/research/compile.md");

    let outcome = prepare_handoff(
        &mut session,
        CompileRequest {
            topic: "Compile behavior".to_string(),
            outline: vec!["Durable handoff".to_string()],
            target_page: Some(PathBuf::from("compile-behavior.md")),
            write_intent: false,
        },
    )
    .expect("compile handoff prepared");

    assert_eq!(
        std::fs::read_to_string(&page_path).expect("page retained"),
        "human-authored wiki page"
    );
    assert_ne!(outcome.bundle.path, page_path);
    assert!(!outcome.state.write_intent);
}

#[test]
fn prepare_handoff_does_not_write_target_page() {
    let temp = tempfile::tempdir().expect("tempdir");
    let scope = ResearchScope::project_for_id("project-1", temp.path());
    let page_path = scope.root().join("compile-behavior.md");
    std::fs::write(&page_path, "human-authored wiki page").expect("page written");
    let note_path = scope.root().join("raw/research/compile.md");
    std::fs::create_dir_all(note_path.parent().expect("note parent")).expect("raw dir");
    std::fs::write(&note_path, "Citation: Example Docs").expect("note written");
    let mut session = session_with_note(&scope, "Compile behavior", "raw/research/compile.md");

    let outcome = prepare_handoff(
        &mut session,
        CompileRequest {
            topic: "Compile behavior".to_string(),
            outline: vec!["Durable handoff".to_string()],
            target_page: Some(PathBuf::from("compile-behavior.md")),
            write_intent: true,
        },
    )
    .expect("compile handoff prepared");

    assert_eq!(
        std::fs::read_to_string(&page_path).expect("page retained"),
        "human-authored wiki page"
    );
    assert!(outcome.state.write_intent);
}

#[test]
fn compile_fails_on_out_of_scope_accepted_note() {
    let in_scope = tempfile::tempdir().expect("in scope tempdir");
    let out_of_scope = tempfile::tempdir().expect("out of scope tempdir");
    let scope = ResearchScope::project_for_id("project-1", in_scope.path());
    let in_scope_path = scope.root().join("raw/research/in-scope.md");
    std::fs::create_dir_all(in_scope_path.parent().expect("note parent")).expect("raw dir");
    std::fs::write(&in_scope_path, "Citation: In-scope citation").expect("note written");
    let mut session = session_with_note(&scope, "In scope", "raw/research/in-scope.md");
    session.accepted_notes.push(AcceptedResearchNote {
        title: "Out of scope".to_string(),
        path: out_of_scope.path().join("raw/research/out-of-scope.md"),
        code_citations: Vec::new(),
        degradation: None,
    });
    let out_path = out_of_scope.path().join("raw/research/out-of-scope.md");
    std::fs::create_dir_all(out_path.parent().expect("out parent")).expect("out raw dir");
    std::fs::write(&out_path, "Out of scope citation").expect("out note written");

    let err = prepare_handoff(
        &mut session,
        CompileRequest {
            topic: "Scoped compile".to_string(),
            outline: vec!["Scoped sources".to_string()],
            target_page: None,
            write_intent: false,
        },
    )
    .expect_err("out-of-scope accepted note must fail fast");

    assert!(matches!(
        err,
        WikiError::InvalidInput {
            field: "accepted_note",
            ..
        }
    ));
}

#[test]
fn compile_rejects_absolute_or_escaping_target_pages() {
    let temp = tempfile::tempdir().expect("tempdir");
    let scope = ResearchScope::project_for_id("project-1", temp.path());
    let note_path = scope.root().join("raw/research/compile.md");
    std::fs::create_dir_all(note_path.parent().expect("note parent")).expect("raw dir");
    std::fs::write(&note_path, "Citation: Example Docs").expect("note written");
    let mut absolute_session =
        session_with_note(&scope, "Compile behavior", "raw/research/compile.md");

    let absolute = prepare_handoff(
        &mut absolute_session,
        CompileRequest {
            topic: "Compile behavior".to_string(),
            outline: vec!["Overview".to_string()],
            target_page: Some(scope.root().join("absolute.md")),
            write_intent: false,
        },
    )
    .expect_err("absolute target page must be rejected");
    assert!(matches!(
        absolute,
        WikiError::InvalidInput {
            field: "target_page",
            ..
        }
    ));

    let mut escaping_session =
        session_with_note(&scope, "Compile behavior", "raw/research/compile.md");
    let escaping = prepare_handoff(
        &mut escaping_session,
        CompileRequest {
            topic: "Compile behavior".to_string(),
            outline: vec!["Overview".to_string()],
            target_page: Some(PathBuf::from("../outside.md")),
            write_intent: false,
        },
    )
    .expect_err("escaping target page must be rejected");
    assert!(matches!(
        escaping,
        WikiError::InvalidInput {
            field: "target_page",
            ..
        }
    ));
}

#[cfg(unix)]
#[test]
fn compile_rejects_target_page_through_symlinked_parent() {
    let vault = tempfile::tempdir().expect("vault tempdir");
    let outside = tempfile::tempdir().expect("outside tempdir");
    std::os::unix::fs::symlink(outside.path(), vault.path().join("linked"))
        .expect("symlink outside");

    let error = write_target_page(
        vault.path(),
        &vault.path().join("linked/outside.md"),
        "# Outside\n",
    )
    .expect_err("symlinked target parent rejected");

    assert!(matches!(
        error,
        WikiError::InvalidInput {
            field: "target_page",
            ..
        }
    ));
}

#[cfg(windows)]
#[test]
fn compile_rejects_target_page_through_symlinked_parent() {
    let vault = tempfile::tempdir().expect("vault tempdir");
    let outside = tempfile::tempdir().expect("outside tempdir");
    if let Err(error) =
        std::os::windows::fs::symlink_dir(outside.path(), vault.path().join("linked"))
    {
        if matches!(
            error.kind(),
            std::io::ErrorKind::PermissionDenied | std::io::ErrorKind::Unsupported
        ) {
            eprintln!("skipping Windows symlink assertion: {error}");
            return;
        }
        panic!("symlink outside: {error}");
    }

    let error = write_target_page(
        vault.path(),
        &vault.path().join("linked/outside.md"),
        "# Outside\n",
    )
    .expect_err("symlinked target parent rejected");

    assert!(matches!(
        error,
        WikiError::InvalidInput {
            field: "target_page",
            ..
        }
    ));
}

#[test]
fn compile_writes_obsidian_markdown() {
    let temp = tempfile::tempdir().expect("tempdir");
    let scope = ResearchScope::project_for_id("project-1", temp.path());
    let note_path = scope.root().join("raw/research/compile.md");
    std::fs::create_dir_all(note_path.parent().expect("note parent")).expect("raw dir");
    std::fs::write(
        &note_path,
        concat!(
            "---\n",
            "title: Compile behavior\n",
            "source: daemon notes\n",
            "---\n\n",
            "Citation: Example Docs, Compile API\n",
            "Compile turns accepted notes into source-grounded wiki articles.\n",
            "Evidence sections keep claims traceable to their matching outline entries."
        ),
    )
    .expect("note written");
    let mut session = session_with_note(&scope, "Compile behavior", "raw/research/compile.md");

    let outcome = compile_to_wiki(
        &mut session,
        CompileRequest {
            topic: "Durable Compile".to_string(),
            outline: vec!["Overview".to_string(), "Evidence".to_string()],
            target_page: None,
            write_intent: false,
        },
    )
    .expect("wiki articles compiled");

    let page = std::fs::read_to_string(&outcome.article_path).expect("article written");
    assert!(
        outcome
            .article_path
            .ends_with("knowledge/topics/durable-compile.md")
    );
    assert!(page.starts_with("---\n"));
    assert!(page.contains("title: \"Durable Compile\""));
    assert!(page.contains("source_kind: \"topic\""));
    assert!(page.contains("[[knowledge/sources/compile-behavior|Compile behavior]]"));
    assert!(page.contains("Example Docs, Compile API"));

    let source_page = scope.root().join("knowledge/sources/compile-behavior.md");
    assert!(source_page.exists());
    let provenance =
        std::fs::read_to_string(scope.root().join("meta/provenance.json")).expect("provenance");
    assert!(provenance.contains("knowledge/topics/durable-compile.md"));
    assert!(provenance.contains("raw/research/compile.md"));
    let provenance = ProvenanceGraph::load_from_vault(scope.root()).expect("load provenance graph");
    let links = provenance.links();
    assert_eq!(links.len(), 2);
    assert_eq!(links[0].section.section_id, "durable-compile");
    assert_eq!(links[1].section.section_id, "evidence");
    let article_page = std::path::Path::new("knowledge/topics/durable-compile.md");
    assert_eq!(
        provenance
            .links_for_page_section(article_page, "durable-compile")
            .len(),
        1
    );
    assert_eq!(
        provenance
            .links_for_page_section(article_page, "evidence")
            .len(),
        1
    );
    let source = &provenance.links()[0].source;
    assert!(source.byte_end > source.byte_start);
}

#[test]
fn index_update_preserves_unrelated_entries() {
    let temp = tempfile::tempdir().expect("tempdir");
    let scope = ResearchScope::project_for_id("project-1", temp.path());
    std::fs::write(
        scope.root().join("_index.md"),
        "# Wiki Index\n\n- [[knowledge/topics/existing|Existing Entry]]\n",
    )
    .expect("index written");
    let note_path = scope.root().join("raw/research/index.md");
    std::fs::create_dir_all(note_path.parent().expect("note parent")).expect("raw dir");
    std::fs::write(&note_path, "Index updates keep unrelated entries.").expect("note written");
    let mut session = session_with_note(&scope, "Index behavior", "raw/research/index.md");

    compile_to_wiki(
        &mut session,
        CompileRequest {
            topic: "Index Preservation".to_string(),
            outline: vec!["Overview".to_string()],
            target_page: None,
            write_intent: false,
        },
    )
    .expect("wiki article compiled");

    let index = std::fs::read_to_string(scope.root().join("_index.md")).expect("index read");
    assert!(index.contains("[[knowledge/topics/existing|Existing Entry]]"));
    assert!(index.contains("[[knowledge/topics/index-preservation|Index Preservation]]"));
}

#[test]
fn index_update_uses_structural_heading_and_link_checks() {
    let temp = tempfile::tempdir().expect("tempdir");
    let article = SynthesizedPage {
        path: temp.path().join("knowledge/topics/exact.md"),
        title: "Exact".to_string(),
        markdown: "# Exact\n\n".to_string(),
        explainer: None,
    };
    std::fs::write(
        temp.path().join("_index.md"),
        concat!(
            "# Wiki Index\n\n",
            "## Compiled pages archive\n\n",
            "- [[knowledge/topics/exact|Exact]] archived copy\n"
        ),
    )
    .expect("index written");

    update_wiki_index(temp.path(), &article).expect("index updated");

    let index = std::fs::read_to_string(temp.path().join("_index.md")).expect("index read");
    assert!(index.lines().any(|line| line == "## Compiled pages"));
    assert_eq!(
        index
            .lines()
            .filter(|line| *line == "- [[knowledge/topics/exact|Exact]]")
            .count(),
        1
    );
}

#[test]
fn insert_compiled_page_link_creates_missing_compiled_heading() {
    let mut index = "# Wiki Index\n\n".to_string();

    insert_compiled_page_link(&mut index, "[[knowledge/topics/missing|Missing]]")
        .expect("missing heading is created");

    assert!(index.contains("## Compiled pages\n\n- [[knowledge/topics/missing|Missing]]\n"));
}

#[test]
fn write_target_page_rejects_existing_page_without_overwrite_race() {
    let vault = tempfile::tempdir().expect("vault tempdir");
    let target = vault.path().join("existing.md");
    std::fs::write(&target, "human-authored wiki page").expect("existing page");

    let error = write_target_page(vault.path(), &target, "# Replacement\n")
        .expect_err("existing target rejected");

    assert!(matches!(
        error,
        WikiError::InvalidInput {
            field: "write_intent",
            ..
        }
    ));
    assert_eq!(
        std::fs::read_to_string(&target).expect("existing page retained"),
        "human-authored wiki page"
    );
}

#[test]
fn compile_explainer_generates_grounded_prose_sections() {
    let temp = tempfile::tempdir().expect("tempdir");
    let scope = ResearchScope::project_for_id("project-1", temp.path());
    let note_path = scope.root().join("raw/research/compile.md");
    std::fs::create_dir_all(note_path.parent().expect("note parent")).expect("raw dir");
    std::fs::write(
        &note_path,
        "---\ntitle: Compile behavior\n---\n\nCompile turns accepted notes into grounded articles.",
    )
    .expect("note written");
    let mut session = session_with_note(&scope, "Compile behavior", "raw/research/compile.md");

    let mut prompts = Vec::new();
    let outcome = {
        let mut generator = |prompt: &ExplainerPrompt| {
            prompts.push(prompt.user.clone());
            Ok(ExplainerResponse {
                text: "## Overview\nCompile grounds articles in accepted notes \
                       [source: raw/research/compile.md]. It never keeps invented citations \
                       [source: raw/research/invented.md].\n"
                    .to_string(),
                model: Some("mock-model".to_string()),
                route: "daemon",
            })
        };
        compile_to_wiki_with_options(
            &mut session,
            CompileRequest {
                topic: "Durable Compile".to_string(),
                outline: vec!["Overview".to_string()],
                target_page: None,
                write_intent: false,
            },
            WikiCompileOptions::default(),
            Some(&mut generator),
        )
        .expect("wiki article compiled")
    };

    let page = std::fs::read_to_string(&outcome.article_path).expect("article written");
    assert!(page.contains("synthesis_mode: \"daemon\""), "{page}");
    assert!(!page.contains("degraded:"), "{page}");
    assert!(page.contains("## Overview"), "{page}");
    assert!(
        page.contains("accepted notes [[knowledge/sources/compile-behavior|Compile behavior]]."),
        "{page}"
    );
    assert!(!page.contains("[source:"), "{page}");
    assert!(!page.contains("invented.md"), "{page}");

    let report = outcome.explainer.expect("explainer report");
    assert_eq!(report.status, "generated");
    assert_eq!(report.route, Some("daemon"));
    assert_eq!(report.model.as_deref(), Some("mock-model"));
    assert_eq!(report.citations_kept, 1);
    assert_eq!(report.citations_stripped, 1);

    assert!(outcome.prompt.tokens_estimated > 0);
    assert_eq!(outcome.prompt.truncated_sources, 0);
    let prompt_user = prompts.first().expect("explainer prompt captured");
    assert!(
        prompt_user.contains("[source: raw/research/compile.md]"),
        "{prompt_user}"
    );
    assert!(
        prompt_user.contains("Compile turns accepted notes"),
        "{prompt_user}"
    );
}

#[test]
fn compile_explainer_failure_degrades_and_keeps_structural_skeleton() {
    let temp = tempfile::tempdir().expect("tempdir");
    let scope = ResearchScope::project_for_id("project-1", temp.path());
    let note_path = scope.root().join("raw/research/compile.md");
    std::fs::create_dir_all(note_path.parent().expect("note parent")).expect("raw dir");
    std::fs::write(&note_path, "Accepted compile evidence.").expect("note written");
    let mut session = session_with_note(&scope, "Compile behavior", "raw/research/compile.md");

    let mut generator = |_prompt: &ExplainerPrompt| {
        Err::<ExplainerResponse, _>("text lane unavailable".to_string())
    };
    let outcome = compile_to_wiki_with_options(
        &mut session,
        CompileRequest {
            topic: "Degraded Compile".to_string(),
            outline: vec!["Overview".to_string()],
            target_page: None,
            write_intent: false,
        },
        WikiCompileOptions::default(),
        Some(&mut generator),
    )
    .expect("wiki article compiled despite explainer failure");

    let page = std::fs::read_to_string(&outcome.article_path).expect("article written");
    assert!(page.contains("synthesis_mode: \"fallback\""), "{page}");
    assert!(page.contains("degraded: true"), "{page}");
    assert!(page.contains("degraded_sources:"), "{page}");
    assert!(page.contains("  - model_provider_unavailable"), "{page}");
    assert!(page.contains("## Overview"), "{page}");

    let report = outcome.explainer.expect("explainer report");
    assert_eq!(report.status, "failed");
    assert_eq!(report.error.as_deref(), Some("text lane unavailable"));
    assert_eq!(report.citations_kept, 0);
}

#[test]
fn compile_without_generator_stays_structural_without_degradation() {
    let temp = tempfile::tempdir().expect("tempdir");
    let scope = ResearchScope::project_for_id("project-1", temp.path());
    let note_path = scope.root().join("raw/research/compile.md");
    std::fs::create_dir_all(note_path.parent().expect("note parent")).expect("raw dir");
    std::fs::write(&note_path, "Accepted compile evidence.").expect("note written");
    let mut session = session_with_note(&scope, "Compile behavior", "raw/research/compile.md");

    let outcome = compile_to_wiki_with_options(
        &mut session,
        CompileRequest {
            topic: "Structural Compile".to_string(),
            outline: vec!["Overview".to_string()],
            target_page: None,
            write_intent: false,
        },
        WikiCompileOptions::default(),
        None,
    )
    .expect("wiki article compiled");

    let page = std::fs::read_to_string(&outcome.article_path).expect("article written");
    assert!(page.contains("synthesis_mode: \"fallback\""), "{page}");
    assert!(!page.contains("degraded:"), "{page}");
    assert!(page.contains("## Overview"), "{page}");

    let report = outcome.explainer.expect("explainer report");
    assert_eq!(report.status, "skipped");
}