inkhaven 2.3.0

Inkhaven — TUI literary work editor for Typst books
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
//! `inkhaven check` — the unified review pass (road to 1.4.0).
//!
//! Runs every applicable **fast / deterministic** checker over a scope in one pass
//! and prints a consolidated summary: the world fact-checker, the Inner Socrates
//! fast track, and the timeline critique. Each native finding also emits to the
//! Output store (a no-op headless, so the TUI path in P1 is free); the CLI prints
//! the findings + per-checker counts to stdout. The fast tracks are LLM-free, so a
//! default `check` is instant and costs nothing.

use std::path::Path;

use anyhow::{anyhow, Result};
use uuid::Uuid;

use crate::config::Config;
use crate::project::ProjectLayout;
use crate::store::hierarchy::Hierarchy;
use crate::store::node::{Node, NodeKind};
use crate::store::Store;

/// A normalized finding for the summary + stdout display (the native finding still
/// carries full per-kind metadata into the Output pane).
struct CheckFinding {
    checker: &'static str,
    severity: String,
    label: String,
    body: String,
    paragraph: Option<Uuid>,
}

#[allow(clippy::too_many_arguments)]
pub fn run(
    project: &Path,
    paragraph: Option<&str>,
    book_name: Option<&str>,
    no_fact: bool,
    no_socrates: bool,
    no_timeline: bool,
    no_continuity: bool,
    no_lector: bool,
) -> Result<()> {
    let layout = ProjectLayout::new(project);
    layout.require_initialized()?;
    let cfg = Config::load_layered(&layout.config_path())?;
    let store = Store::open(layout.clone(), &cfg)?;
    let hierarchy = Hierarchy::load(&store)?;

    let book_filter_id = match book_name {
        Some(name) => Some(
            crate::cli::resolve_user_book(&hierarchy, Some(name), "check")
                .map_err(|m| anyhow!(m))?
                .id,
        ),
        None => None,
    };

    let scope = scope_paragraphs(&store, &hierarchy, paragraph, book_filter_id)?;
    if scope.is_empty() {
        println!("(no prose paragraphs in scope — nothing to check)");
        return Ok(());
    }

    let mut findings: Vec<CheckFinding> = Vec::new();

    // ── per-paragraph checkers: fact-check + socrates ────────────────────────
    let world = (!no_fact).then(|| crate::cli::realworld::build_world_context(project));
    let socratic = (!no_socrates).then(|| {
        let ledger = crate::inner_socrates::storage::InnerSocratesStore::open_for_project(project)
            .ok()
            .and_then(|s| s.load_ledger().ok())
            .unwrap_or_default();
        let persona = crate::inner_socrates::personas::active(project);
        (persona, ledger)
    });

    for (id, prose) in &scope {
        if let Some((ledger, ctx)) = &world {
            for f in crate::world::fact_check::check_paragraph(prose, ledger, &[], ctx.as_ref()) {
                crate::world::fact_check::emit_finding(&f, Some(*id));
                findings.push(CheckFinding {
                    checker: "fact-check",
                    severity: f.severity.clone(),
                    label: f.category.clone(),
                    body: f.body.clone(),
                    paragraph: Some(*id),
                });
            }
        }
        if let Some((persona, ledger)) = &socratic {
            let fctx = crate::inner_socrates::intent::FindingContext {
                paragraph_id: Some(id.to_string()),
                ..Default::default()
            };
            for f in crate::inner_socrates::fast::check_paragraph(prose, persona, ledger, &fctx) {
                crate::inner_socrates::output::emit_finding(&f, Some(*id));
                findings.push(CheckFinding {
                    checker: "socrates",
                    severity: f.severity.label().to_string(),
                    label: f.category.id().to_string(),
                    body: f.question.clone(),
                    paragraph: Some(*id),
                });
            }
        }
    }

    // ── project-wide checker: timeline critique ──────────────────────────────
    let mut timeline_ran = false;
    if !no_timeline && cfg.timeline.enabled && cfg.timeline.critique.enabled {
        timeline_ran = true;
        run_timeline_critique(&hierarchy, &cfg, &mut findings);
    }

    // ── project-wide checker: SENTINEL continuity ledger ─────────────────────
    // `timeline` is skipped — the critique above already covers orphan / overlap.
    let mut continuity_ran = false;
    if !no_continuity && cfg.continuity.enabled {
        continuity_ran = true;
        run_continuity(&layout, &cfg, &store, &hierarchy, &mut findings);
    }

    // ── project-wide checker: LECTOR read-through (deterministic) ─────────────
    let mut lector_ran = false;
    if !no_lector && cfg.lector.enabled {
        lector_ran = true;
        run_lector(&layout, &cfg, &store, &hierarchy, &mut findings);
    }

    print_report(
        &findings,
        scope.len(),
        &cfg,
        world.is_some(),
        socratic.is_some(),
        timeline_ran,
        continuity_ran,
        lector_ran,
        &hierarchy,
    );
    Ok(())
}

/// LECTOR (LR-P6) — the deterministic read-through (forward reader walk + scene/
/// sequel arrhythmia; the synthetic first-read stays explicit) folded into the
/// `check` summary.
fn run_lector(
    layout: &ProjectLayout,
    cfg: &Config,
    store: &Store,
    hierarchy: &Hierarchy,
    findings: &mut Vec<CheckFinding>,
) {
    use crate::lector::{walk, Severity};
    let rt = walk::read_forward(store, cfg, layout, hierarchy);
    for f in crate::lector::deterministic_findings(&rt, layout, hierarchy, cfg) {
        let severity = match f.severity {
            Severity::Concern => "warning",
            Severity::Notice => "warning",
            Severity::Info => "info",
        };
        findings.push(CheckFinding {
            checker: "read-through",
            severity: severity.to_string(),
            label: f.kind.to_string(),
            body: f.message.clone(),
            paragraph: f.anchor,
        });
    }
}

/// SENTINEL (CT-P4) — run the unified continuity engine (minus `timeline`, which
/// has its own section) and fold its findings into the `check` summary. Each
/// native finding also lands in the Output store via the engine's own emit path
/// on the TUI; here we collect the summary rows.
fn run_continuity(
    layout: &ProjectLayout,
    cfg: &Config,
    store: &Store,
    hierarchy: &Hierarchy,
    findings: &mut Vec<CheckFinding>,
) {
    use crate::continuity_intel::{engine, Severity};
    let sel = engine::selector(&[], &["timeline".to_string()]);
    for f in engine::run(store, cfg, layout, hierarchy, &sel) {
        let severity = match f.severity {
            Severity::Contradiction => "contradiction",
            Severity::Warning => "warning",
            Severity::Info => "info",
        };
        findings.push(CheckFinding {
            checker: "continuity",
            severity: severity.to_string(),
            label: f.source.to_string(),
            body: f.message.clone(),
            paragraph: f.anchor,
        });
    }
}

/// Resolve the scope to `(paragraph_id, prose)` pairs: a single `--paragraph`, or
/// every prose paragraph (non-event) under the `--book` (or every user book).
fn scope_paragraphs(
    store: &Store,
    hierarchy: &Hierarchy,
    paragraph: Option<&str>,
    book_filter_id: Option<Uuid>,
) -> Result<Vec<(Uuid, String)>> {
    let ids: Vec<Uuid> = match paragraph {
        Some(pid) => {
            let id = Uuid::parse_str(pid.trim())
                .map_err(|e| anyhow!("bad paragraph id `{pid}`: {e}"))?;
            vec![id]
        }
        None => hierarchy
            .flatten()
            .into_iter()
            .filter(|(n, _)| n.kind == NodeKind::Paragraph && n.event.is_none())
            .filter(|(n, _)| match book_filter_id {
                Some(bid) => node_in_book(hierarchy, n, bid),
                None => node_under_user_book(hierarchy, n),
            })
            .map(|(n, _)| n.id)
            .collect(),
    };
    let mut out = Vec::new();
    for id in ids {
        let prose = store
            .get_content(id)
            .ok()
            .flatten()
            .map(|b| String::from_utf8_lossy(&b).into_owned())
            .unwrap_or_default();
        if !prose.trim().is_empty() {
            out.push((id, prose));
        }
    }
    Ok(out)
}

/// The Book ancestor of a node, if any.
fn book_of<'a>(hierarchy: &'a Hierarchy, node: &'a Node) -> Option<&'a Node> {
    let mut cur = node;
    loop {
        if cur.kind == NodeKind::Book {
            return Some(cur);
        }
        cur = hierarchy.get(cur.parent_id?)?;
    }
}

fn node_in_book(hierarchy: &Hierarchy, node: &Node, book_id: Uuid) -> bool {
    book_of(hierarchy, node).map(|b| b.id == book_id).unwrap_or(false)
}

/// True when the node's root book is a user book (not a system book like Notes /
/// Help / Prompts), so `check` skips system-book scratch.
fn node_under_user_book(hierarchy: &Hierarchy, node: &Node) -> bool {
    book_of(hierarchy, node).map(|b| b.system_tag.is_none()).unwrap_or(false)
}

/// Run the orphan + fuzzy-overlap critique over the whole project's events and emit
/// + collect the findings.
fn run_timeline_critique(hierarchy: &Hierarchy, cfg: &Config, out: &mut Vec<CheckFinding>) {
    use crate::timeline::critique;
    use crate::timeline::{Calendar, Precision, TimelinePoint};

    let calendar = Calendar::from_config(cfg.timeline.calendar.clone());
    let default_track = cfg.timeline.default_track.clone();
    let now = chrono::Utc::now();
    let events: Vec<critique::CritiqueEvent> = hierarchy
        .flatten()
        .into_iter()
        .filter_map(|(n, _)| n.event.as_ref().map(|e| (n, e)))
        .map(|(n, ev)| critique::CritiqueEvent {
            id: n.id,
            title: n.title.clone(),
            start_ticks: ev.start_ticks,
            end_ticks: ev.end_ticks,
            precision: ev.precision,
            track: ev.track.clone().unwrap_or_else(|| default_track.clone()),
            is_orphan: ev.is_orphan(&n.linked_paragraphs),
            linked_paragraph_count: n.linked_paragraphs.len(),
            characters: ev.characters.clone(),
            places: ev.places.clone(),
            age_days: Some((now - n.modified_at).num_days().max(0)),
        })
        .collect();
    if events.is_empty() {
        return;
    }
    let cc = &cfg.timeline.critique;
    let fuzz = critique::fuzz_windows(&calendar);
    let mut report = critique::run(
        &events,
        &fuzz,
        cc.min_significance(),
        cc.min_suspicion(),
        cc.fuzzy_overlap.cluster_min_size.max(2),
        critique::DEFAULT_STALENESS_DAYS,
    );
    if !cc.orphan.enabled {
        report.orphans.clear();
    }
    if !cc.fuzzy_overlap.enabled {
        report.overlaps.clear();
    }
    let lang = critique::lang::lang_from_name(&cfg.language);
    for f in &report.orphans {
        let date = calendar.format(TimelinePoint::from_ticks(f.start_ticks), f.precision);
        critique::pane::emit_orphan(f, &date, None, lang);
        out.push(CheckFinding {
            checker: "timeline",
            severity: f.severity.label().to_string(),
            label: "orphan".into(),
            body: format!("\"{}\"{}", f.title, f.reasons.join(" ")),
            paragraph: None,
        });
    }
    for f in &report.overlaps {
        let window = calendar.format(TimelinePoint::from_ticks(f.overlap_window.0), Precision::Season);
        critique::pane::emit_overlap(f, &window, &[], &[], None, lang);
        out.push(CheckFinding {
            checker: "timeline",
            severity: f.severity.label().to_string(),
            label: "fuzzy_overlap".into(),
            body: format!("{}{}", f.titles.join(" + "), f.reasons.join(" ")),
            paragraph: None,
        });
    }
}

#[allow(clippy::too_many_arguments)]
fn print_report(
    findings: &[CheckFinding],
    paragraphs: usize,
    cfg: &Config,
    fact_ran: bool,
    socratic_ran: bool,
    timeline_ran: bool,
    continuity_ran: bool,
    lector_ran: bool,
    hierarchy: &Hierarchy,
) {
    if findings.is_empty() {
        println!("✓ no issues across {paragraphs} paragraph(s)");
    } else {
        for f in findings {
            let icon = match f.severity.as_str() {
                "contradiction" | "Probe" | "Contradiction" => "",
                "warning" | "Inquiry" | "Warning" => "",
                _ => "",
            };
            let para = f
                .paragraph
                .and_then(|id| hierarchy.get(id))
                .map(|n| format!("  ({})", n.title))
                .unwrap_or_default();
            println!("{icon} [{}/{}] {}{para}", f.checker, f.label, f.body);
        }
        println!();
    }

    // Per-checker summary.
    let count = |c: &str| findings.iter().filter(|f| f.checker == c).count();
    println!("review pass · {paragraphs} paragraph(s)");
    if fact_ran {
        println!("  fact-check : {}", count("fact-check"));
    } else {
        println!("  fact-check : skipped");
    }
    if socratic_ran {
        println!("  socrates   : {}", count("socrates"));
    } else {
        println!("  socrates   : skipped");
    }
    if timeline_ran {
        println!("  timeline   : {}", count("timeline"));
    } else {
        let why = if !cfg.timeline.enabled { " (timeline off)" } else { "" };
        println!("  timeline   : skipped{why}");
    }
    if continuity_ran {
        println!("  continuity : {}", count("continuity"));
    } else {
        println!("  continuity : skipped");
    }
    if lector_ran {
        println!("  read-through: {}", count("read-through"));
    } else {
        println!("  read-through: skipped");
    }
    println!("  ─────────────");
    println!("  TOTAL      : {}", findings.len());
}

#[cfg(test)]
mod tests {
    use super::*;

    fn node(v: serde_json::Value) -> Node {
        serde_json::from_value(v).expect("test node")
    }

    fn base(id: uuid::Uuid, kind: &str, slug: &str, parent: Option<uuid::Uuid>) -> serde_json::Value {
        serde_json::json!({
            "id": id, "kind": kind, "title": slug, "slug": slug,
            "path": [], "parent_id": parent, "order": 1, "file": null,
            "modified_at": "2026-01-01T00:00:00Z",
        })
    }

    #[test]
    fn scope_includes_user_book_paragraphs_excludes_system_books() {
        let user_book = uuid::Uuid::now_v7();
        let sys_book = uuid::Uuid::now_v7();
        let user_para = uuid::Uuid::now_v7();
        let sys_para = uuid::Uuid::now_v7();
        let mut sysb = base(sys_book, "book", "notes", None);
        sysb["system_tag"] = serde_json::json!("notes");
        let h = Hierarchy::from_nodes_for_test(vec![
            node(base(user_book, "book", "velmaron", None)),
            node(sysb),
            node(base(user_para, "paragraph", "scene", Some(user_book))),
            node(base(sys_para, "paragraph", "jotting", Some(sys_book))),
        ]);
        let up = h.get(user_para).unwrap();
        let sp = h.get(sys_para).unwrap();
        assert!(node_under_user_book(&h, up), "user-book paragraph is in scope");
        assert!(!node_under_user_book(&h, sp), "system-book paragraph is skipped");
        assert!(node_in_book(&h, up, user_book));
        assert!(!node_in_book(&h, up, sys_book));
    }
}