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
//! Bounded LLM explainer synthesis for compiled wiki articles.
//!
//! `compile` keeps its deterministic skeleton; this module adds the optional
//! prose layer: a single bounded prompt over the accepted sources, a
//! transport-agnostic generation seam, and grounding that validates every
//! citation marker against the accepted sources before prose reaches the
//! vault. Generation failure degrades to the structural skeleton with
//! explicit degradation markers; AI off skips synthesis without markers.

use std::path::Path;

use serde::Serialize;

use crate::synthesis::{SynthesisInput, relative_path};

/// Hard cap on the estimated token count of the explainer prompt (system +
/// user), matching `gwiki ask`'s prompt budget discipline.
pub const EXPLAINER_PROMPT_TOKEN_BUDGET: usize = 12_000;

/// Per-source excerpt bound entering the prompt.
pub const EXPLAINER_SOURCE_EXCERPT_MAX_CHARS: usize = 2_400;

/// Conservative ~4 chars/token estimate, rounded up.
pub fn estimate_tokens(chars: usize) -> usize {
    chars.div_ceil(4)
}

/// System prompt for the explainer completion. Citations use machine-checkable
/// `[source: <path>]` markers so grounding can validate them afterwards.
pub const EXPLAINER_SYSTEM: &str = "You write a grounded wiki explainer from supplied source \
     excerpts. Use only facts stated in the excerpts. Write each requested section as a markdown \
     '## <section>' heading followed by one or two short paragraphs. Plain paragraphs only - no \
     extra headings, no lists, no code fences. Cite supporting sources inline with \
     [source: <path>] markers, copying a path exactly as listed. Never invent paths. If the \
     excerpts do not cover a section, state that the sources do not cover it.";

/// Fully assembled explainer prompt plus budget accounting.
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct ExplainerPrompt {
    pub system: &'static str,
    pub user: String,
    pub tokens_estimated: usize,
    /// Accepted sources dropped because the prompt hit the token budget.
    pub truncated_sources: usize,
}

/// One successful completion from a transport (daemon or direct).
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct ExplainerResponse {
    pub text: String,
    pub model: Option<String>,
    pub route: &'static str,
}

/// Transport seam: the CLI wires daemon/direct gcore transports; tests inject
/// deterministic closures.
pub type ExplainerGenerator<'a> =
    &'a mut dyn FnMut(&ExplainerPrompt) -> Result<ExplainerResponse, String>;

/// Outcome of one explainer attempt, mirroring codewiki's honesty contract:
/// `Failed` means an attempt was made and degrades the page; `Skipped` means
/// synthesis was off by intent and the structural skeleton is not degraded.
#[derive(Debug, Clone, PartialEq, Eq)]
pub enum ExplainerGeneration {
    Generated {
        body: String,
        model: Option<String>,
        route: &'static str,
    },
    Failed {
        error: String,
    },
    Skipped,
}

/// Per-article explainer accounting surfaced through the compile payload.
#[derive(Debug, Clone, PartialEq, Eq, Serialize)]
pub struct ExplainerReport {
    pub status: &'static str,
    pub route: Option<&'static str>,
    pub model: Option<String>,
    pub error: Option<String>,
    pub citations_kept: usize,
    pub citations_stripped: usize,
    pub fallback_sections: usize,
}

impl ExplainerReport {
    pub fn skipped() -> Self {
        Self {
            status: "skipped",
            route: None,
            model: None,
            error: None,
            citations_kept: 0,
            citations_stripped: 0,
            fallback_sections: 0,
        }
    }
}

/// A citation the model is allowed to make: the exact path key listed in the
/// prompt, the wiki link a kept citation is rewritten to, and the source text
/// used for lexical fallback ranking.
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct CitationTarget {
    pub key: String,
    pub link: String,
    pub corpus: String,
}

/// Grounded explainer body plus citation accounting.
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct GroundedExplainer {
    pub body: String,
    pub citations_kept: usize,
    pub citations_stripped: usize,
    pub fallback_sections: usize,
}

/// Assemble the bounded explainer prompt: topic, requested sections, then
/// accepted sources in order until the next entry would exceed the budget.
pub fn build_explainer_prompt(vault_root: &Path, input: &SynthesisInput) -> ExplainerPrompt {
    let mut user = format!("Topic: {}\n", input.topic);
    user.push_str("Write the explainer with exactly these sections:\n");
    if input.outline.is_empty() {
        user.push_str("- Overview\n");
    } else {
        for heading in &input.outline {
            user.push_str("- ");
            user.push_str(heading);
            user.push('\n');
        }
    }
    user.push_str("\nSource excerpts:\n");

    let system_chars = EXPLAINER_SYSTEM.chars().count();
    let mut truncated_sources = 0usize;
    if input.accepted_sources.is_empty() {
        user.push_str("- No accepted sources.\n");
    }
    for (index, source) in input.accepted_sources.iter().enumerate() {
        let key = relative_path(vault_root, &source.path);
        let excerpt = bounded_excerpt(
            &source.chunks.join("\n"),
            EXPLAINER_SOURCE_EXCERPT_MAX_CHARS,
        );
        let entry = format!(
            "{}. {} [source: {key}]\n{excerpt}\n\n",
            index + 1,
            source.title
        );
        let projected = user.chars().count() + entry.chars().count() + system_chars;
        if estimate_tokens(projected) > EXPLAINER_PROMPT_TOKEN_BUDGET {
            truncated_sources = input.accepted_sources.len() - index;
            break;
        }
        user.push_str(&entry);
    }

    let tokens_estimated = estimate_tokens(user.chars().count() + system_chars);
    ExplainerPrompt {
        system: EXPLAINER_SYSTEM,
        user,
        tokens_estimated,
        truncated_sources,
    }
}

/// Run one explainer attempt through the injected generator. No generator or
/// no accepted sources means synthesis is structurally off, not degraded.
pub fn generate_explainer(
    input: &SynthesisInput,
    prompt: &ExplainerPrompt,
    generator: Option<ExplainerGenerator<'_>>,
) -> ExplainerGeneration {
    let Some(generator) = generator else {
        return ExplainerGeneration::Skipped;
    };
    if input.accepted_sources.is_empty() {
        return ExplainerGeneration::Skipped;
    }
    match generator(prompt) {
        Ok(response) => {
            let body = response.text.trim().to_string();
            if body.is_empty() {
                ExplainerGeneration::Failed {
                    error: "model returned an empty explainer".to_string(),
                }
            } else {
                ExplainerGeneration::Generated {
                    body,
                    model: response.model,
                    route: response.route,
                }
            }
        }
        Err(error) => ExplainerGeneration::Failed { error },
    }
}

/// Validate generated citations against the accepted sources: a marker whose
/// path matches a target is rewritten to that target's wiki link; any other
/// marker is stripped. Sections left with prose but no surviving citation get
/// a fallback citation to the lexically closest source.
pub fn ground_explainer(body: &str, targets: &[CitationTarget]) -> GroundedExplainer {
    let mut citations_kept = 0usize;
    let mut citations_stripped = 0usize;
    let mut grounded = String::with_capacity(body.len());
    let mut rest = body;

    while let Some(start) = rest.find("[source:") {
        let (before, marker_on) = rest.split_at(start);
        let Some(end) = marker_on.find(']') else {
            grounded.push_str(before);
            grounded.push_str(marker_on);
            rest = "";
            break;
        };
        let key = marker_on["[source:".len()..end].trim();
        match targets
            .iter()
            .find(|target| citation_key_matches(&target.key, key))
        {
            Some(target) => {
                citations_kept += 1;
                grounded.push_str(before);
                grounded.push_str(&target.link);
            }
            None => {
                citations_stripped += 1;
                grounded.push_str(before.strip_suffix(' ').unwrap_or(before));
            }
        }
        rest = &marker_on[end + 1..];
    }
    grounded.push_str(rest);

    let (body, fallback_sections) = apply_section_fallbacks(&grounded, targets);
    GroundedExplainer {
        body,
        citations_kept,
        citations_stripped,
        fallback_sections,
    }
}

fn citation_key_matches(target_key: &str, cited: &str) -> bool {
    target_key == cited
        || target_key
            .strip_suffix(".md")
            .is_some_and(|trimmed| trimmed == cited)
}

/// Append a fallback citation to each `##` section (and the preamble) that
/// has prose but no surviving citation link.
fn apply_section_fallbacks(body: &str, targets: &[CitationTarget]) -> (String, usize) {
    if targets.is_empty() {
        return (body.to_string(), 0);
    }
    let mut sections: Vec<String> = Vec::new();
    for line in body.lines() {
        if line.starts_with("## ") || sections.is_empty() {
            sections.push(String::new());
        }
        let section = sections.last_mut().expect("section pushed above");
        section.push_str(line);
        section.push('\n');
    }

    let mut fallback_sections = 0usize;
    let mut out = String::new();
    for section in &sections {
        let prose: String = section
            .lines()
            .filter(|line| !line.starts_with("## "))
            .collect::<Vec<_>>()
            .join(" ");
        let has_citation = targets.iter().any(|target| section.contains(&target.link));
        out.push_str(section);
        if !prose.trim().is_empty()
            && !has_citation
            && let Some(target) = best_fallback_target(&prose, targets)
        {
            fallback_sections += 1;
            if !out.ends_with("\n\n") {
                out.push('\n');
            }
            out.push_str(&format!("_Source: {}_\n", target.link));
        }
    }
    (out, fallback_sections)
}

/// Rank fallback targets by significant-token overlap with the section prose;
/// ties keep accepted-source order.
fn best_fallback_target<'a>(
    prose: &str,
    targets: &'a [CitationTarget],
) -> Option<&'a CitationTarget> {
    let prose_tokens: std::collections::BTreeSet<String> = significant_tokens(prose);
    targets
        .iter()
        .enumerate()
        .max_by_key(|(index, target)| {
            let overlap = significant_tokens(&target.corpus)
                .intersection(&prose_tokens)
                .count();
            (overlap, std::cmp::Reverse(*index))
        })
        .map(|(_, target)| target)
}

fn significant_tokens(text: &str) -> std::collections::BTreeSet<String> {
    text.split(|ch: char| !ch.is_alphanumeric())
        .filter(|token| token.chars().count() >= 3)
        .map(|token| token.to_lowercase())
        .collect()
}

/// Char-boundary-safe truncation that preserves newlines and marks elision.
fn bounded_excerpt(text: &str, max_chars: usize) -> String {
    let trimmed = text.trim();
    if trimmed.chars().count() <= max_chars {
        return trimmed.to_string();
    }
    let mut excerpt: String = trimmed.chars().take(max_chars).collect();
    excerpt.push('…');
    excerpt
}

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

    use super::*;
    use crate::synthesis::{ArticleKind, SynthesisSource};

    fn input_with_sources(sources: Vec<SynthesisSource>) -> SynthesisInput {
        SynthesisInput {
            handoff_id: "handoff-1".to_string(),
            topic: "Compile Behavior".to_string(),
            outline: vec!["Overview".to_string(), "Evidence".to_string()],
            target_kind: ArticleKind::Topic,
            accepted_sources: sources,
            citations: vec![],
            conflicting_claims: vec![],
            missing_evidence: vec![],
        }
    }

    fn source(title: &str, path: &str, chunk: &str) -> SynthesisSource {
        SynthesisSource {
            title: title.to_string(),
            path: PathBuf::from(path),
            chunks: vec![chunk.to_string()],
        }
    }

    #[test]
    fn explainer_prompt_lists_sections_and_cited_source_paths() {
        let input = input_with_sources(vec![source(
            "Compile Notes",
            "raw/research/compile.md",
            "Compile turns accepted notes into grounded articles.",
        )]);
        let prompt = build_explainer_prompt(Path::new("/vault"), &input);

        assert_eq!(prompt.system, EXPLAINER_SYSTEM);
        assert!(prompt.user.contains("Topic: Compile Behavior"));
        assert!(prompt.user.contains("- Overview\n- Evidence"));
        assert!(
            prompt
                .user
                .contains("1. Compile Notes [source: raw/research/compile.md]")
        );
        assert!(prompt.user.contains("Compile turns accepted notes"));
        assert!(prompt.tokens_estimated > 0);
        assert_eq!(prompt.truncated_sources, 0);
    }

    #[test]
    fn explainer_prompt_without_outline_requests_an_overview_section() {
        let mut input = input_with_sources(vec![]);
        input.outline.clear();
        let prompt = build_explainer_prompt(Path::new("/vault"), &input);

        assert!(prompt.user.contains("- Overview\n"));
        assert!(prompt.user.contains("- No accepted sources."));
    }

    #[test]
    fn explainer_prompt_never_exceeds_token_budget() {
        let big_chunk = "evidence ".repeat(1_000);
        let sources = (0..40)
            .map(|index| {
                source(
                    &format!("Source {index}"),
                    &format!("raw/{index}.md"),
                    &big_chunk,
                )
            })
            .collect();
        let prompt = build_explainer_prompt(Path::new("/vault"), &input_with_sources(sources));

        assert!(prompt.tokens_estimated <= EXPLAINER_PROMPT_TOKEN_BUDGET);
        assert!(prompt.truncated_sources > 0);
    }

    #[test]
    fn explainer_prompt_bounds_each_source_excerpt() {
        let oversized = "x".repeat(EXPLAINER_SOURCE_EXCERPT_MAX_CHARS + 500);
        let prompt = build_explainer_prompt(
            Path::new("/vault"),
            &input_with_sources(vec![source("Big", "raw/big.md", &oversized)]),
        );

        let excerpt_line = prompt
            .user
            .lines()
            .find(|line| line.starts_with('x'))
            .expect("excerpt line");
        assert_eq!(
            excerpt_line.chars().count(),
            EXPLAINER_SOURCE_EXCERPT_MAX_CHARS + 1
        );
        assert!(excerpt_line.ends_with('…'));
    }

    #[test]
    fn grounding_rewrites_valid_markers_and_strips_invented_ones() {
        let targets = vec![CitationTarget {
            key: "raw/research/compile.md".to_string(),
            link: "[[knowledge/sources/compile-notes|Compile Notes]]".to_string(),
            corpus: "Compile Notes compile turns accepted notes".to_string(),
        }];
        let body = "## Overview\nCompile is grounded [source: raw/research/compile.md].\n\
                    It never invents sources [source: raw/research/madeup.md].\n";

        let grounded = ground_explainer(body, &targets);

        assert!(
            grounded
                .body
                .contains("grounded [[knowledge/sources/compile-notes|Compile Notes]].")
        );
        assert!(!grounded.body.contains("[source:"));
        assert!(!grounded.body.contains("madeup"));
        assert_eq!(grounded.citations_kept, 1);
        assert_eq!(grounded.citations_stripped, 1);
        assert_eq!(grounded.fallback_sections, 0);
    }

    #[test]
    fn grounding_appends_fallback_citation_to_uncited_sections() {
        let targets = vec![
            CitationTarget {
                key: "raw/a.md".to_string(),
                link: "[[knowledge/sources/alpha|Alpha]]".to_string(),
                corpus: "alpha indexing pipeline walker".to_string(),
            },
            CitationTarget {
                key: "raw/b.md".to_string(),
                link: "[[knowledge/sources/beta|Beta]]".to_string(),
                corpus: "beta search ranking fusion".to_string(),
            },
        ];
        let body = "## Overview\nThe search ranking fusion merges results.\n";

        let grounded = ground_explainer(body, &targets);

        assert_eq!(grounded.fallback_sections, 1);
        assert!(
            grounded
                .body
                .contains("_Source: [[knowledge/sources/beta|Beta]]_")
        );
    }

    #[test]
    fn generation_without_sources_or_generator_is_skipped_not_degraded() {
        let input = input_with_sources(vec![]);
        let prompt = build_explainer_prompt(Path::new("/vault"), &input);
        let mut generator = |_prompt: &ExplainerPrompt| -> Result<ExplainerResponse, String> {
            panic!("generator must not run without accepted sources");
        };

        assert_eq!(
            generate_explainer(&input, &prompt, Some(&mut generator)),
            ExplainerGeneration::Skipped
        );
        assert_eq!(
            generate_explainer(&input, &prompt, None),
            ExplainerGeneration::Skipped
        );
    }

    #[test]
    fn generation_failure_and_empty_output_mark_failed() {
        let input = input_with_sources(vec![source("A", "raw/a.md", "alpha")]);
        let prompt = build_explainer_prompt(Path::new("/vault"), &input);

        let mut failing = |_prompt: &ExplainerPrompt| Err("transport down".to_string());
        assert_eq!(
            generate_explainer(&input, &prompt, Some(&mut failing)),
            ExplainerGeneration::Failed {
                error: "transport down".to_string()
            }
        );

        let mut empty = |_prompt: &ExplainerPrompt| {
            Ok(ExplainerResponse {
                text: "  \n".to_string(),
                model: None,
                route: "direct",
            })
        };
        assert!(matches!(
            generate_explainer(&input, &prompt, Some(&mut empty)),
            ExplainerGeneration::Failed { .. }
        ));
    }

    #[test]
    fn generation_success_trims_and_carries_route_and_model() {
        let input = input_with_sources(vec![source("A", "raw/a.md", "alpha")]);
        let prompt = build_explainer_prompt(Path::new("/vault"), &input);
        let mut generator = |_prompt: &ExplainerPrompt| {
            Ok(ExplainerResponse {
                text: "\n## Overview\nAlpha grounds compile [source: raw/a.md].\n".to_string(),
                model: Some("gemma".to_string()),
                route: "daemon",
            })
        };

        let generation = generate_explainer(&input, &prompt, Some(&mut generator));
        match generation {
            ExplainerGeneration::Generated { body, model, route } => {
                assert!(body.starts_with("## Overview"));
                assert_eq!(model.as_deref(), Some("gemma"));
                assert_eq!(route, "daemon");
            }
            other => panic!("expected generated explainer, got {other:?}"),
        }
    }
}