inkhaven 1.2.23

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
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
//! 1.2.19+ C.3 — the continuity bible.
//!
//! Turns the Characters book from a manual scaffold into
//! a living continuity checker.  An AI pass
//! (`inkhaven continuity extract`) walks the manuscript
//! and records per-character *established facts* —
//! appearance, relationships, possessions, origin — as
//! `(character, attribute, value, chapter)` rows in a
//! project sidecar.  The `continuity-drift` doctor scan
//! then flags attributes that *change across chapters*
//! ("ch.3: eyes green; ch.17: eyes brown").
//!
//! ## Multilingual
//!
//! Two tiers, as the 1.2.19 plan lays out:
//!
//!   * **Extraction (Tier 1)** — the AI pass uses a
//!     per-language prompt (`continuity-extract`), so
//!     facts come out in the manuscript's language.
//!   * **Drift comparison (Tier 2)** — values are
//!     normalised through the project's Snowball stemmer
//!     (+ a `ё`→`е` fold) before comparison, so inflected
//!     restatements of the same fact don't false-flag
//!     (ru `зелёные глаза` / `зелёными глазами` collapse).
//!
//! Embedding-based synonym-aware comparison ("green" vs
//! "emerald" → consistent) is a C.3.b refinement; C.3
//! ships the stemmer-normalised comparison, which catches
//! the genuine "green vs brown" drift while tolerating
//! inflection.
//!
//! ## This module
//!
//! Pure: the fact model, the sidecar JSON round-trip, and
//! the `detect_drift` classifier.  The AI extraction +
//! the doctor-scan wiring live in the CLI.

use std::collections::BTreeMap;
use std::path::{Path, PathBuf};

use serde::{Deserialize, Serialize};

use crate::config::parse_stemmer_language;
use rust_stemmers::Stemmer;

/// One established fact about a character, as of a
/// particular chapter.
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
pub struct CharacterFact {
    /// Character name (as it appears in the Characters
    /// book / prose).
    pub character: String,
    /// Canonical attribute key (`eye_color`, `hometown`,
    /// `occupation`, …) — the AI is prompted to use a
    /// stable snake_case key.
    pub attribute: String,
    /// The value as established in `chapter`.
    pub value: String,
    /// Chapter title the fact was drawn from.
    pub chapter: String,
}

/// The whole bible — every extracted fact.  Serialised to
/// `<project>/.inkhaven/continuity.json`.
#[derive(Debug, Clone, Default, Serialize, Deserialize)]
pub struct ContinuityBible {
    /// Inkhaven version that produced the extraction.
    #[serde(default)]
    pub version: String,
    /// Language the facts were extracted in (drives the
    /// drift comparison's stemmer).
    #[serde(default)]
    pub language: String,
    pub facts: Vec<CharacterFact>,
}

impl ContinuityBible {
    /// Sidecar path for a project.
    pub fn sidecar_path(project_root: &Path) -> PathBuf {
        project_root.join(".inkhaven").join("continuity.json")
    }

    /// Load the bible from the project sidecar.  Returns
    /// an empty bible (not an error) when the sidecar
    /// doesn't exist yet.
    pub fn load(project_root: &Path) -> std::io::Result<Self> {
        let path = Self::sidecar_path(project_root);
        match std::fs::read_to_string(&path) {
            Ok(s) => serde_json::from_str(&s).map_err(|e| {
                std::io::Error::new(std::io::ErrorKind::InvalidData, e)
            }),
            Err(e) if e.kind() == std::io::ErrorKind::NotFound => {
                Ok(Self::default())
            }
            Err(e) => Err(e),
        }
    }

    /// Persist the bible atomically to the sidecar.
    pub fn save(&self, project_root: &Path) -> std::io::Result<()> {
        let path = Self::sidecar_path(project_root);
        if let Some(parent) = path.parent() {
            std::fs::create_dir_all(parent)?;
        }
        let body = serde_json::to_vec_pretty(self).map_err(|e| {
            std::io::Error::new(std::io::ErrorKind::InvalidData, e)
        })?;
        crate::io_atomic::write(&path, &body)
    }

    /// Distinct character names, sorted.
    pub fn characters(&self) -> Vec<String> {
        let mut names: Vec<String> =
            self.facts.iter().map(|f| f.character.clone()).collect();
        names.sort();
        names.dedup();
        names
    }
}

/// A flagged continuity drift: one character's attribute
/// taking different values across chapters.
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct Drift {
    pub character: String,
    pub attribute: String,
    /// The conflicting `(chapter, value)` rows, in the
    /// order first seen.
    pub conflicts: Vec<(String, String)>,
}

/// Normalise a value for comparison: lowercase, fold
/// `ё`→`е`, stem each word with the language's Snowball
/// algorithm (when available), join.  This is what makes
/// inflected restatements compare equal.
fn normalise(value: &str, stemmer: &Option<Stemmer>) -> String {
    value
        .split_whitespace()
        .map(|w| {
            let trimmed = w.trim_matches(|c: char| !c.is_alphanumeric());
            crate::text::normalize_stem(trimmed, stemmer)
        })
        .filter(|w| !w.is_empty())
        .collect::<Vec<_>>()
        .join(" ")
}

/// Detect drift: group facts by `(character, attribute)`
/// and flag any group whose normalised values disagree
/// across chapters.
///
/// `language` selects the comparison stemmer.  Pure.
pub fn detect_drift(bible: &ContinuityBible, language: &str) -> Vec<Drift> {
    let stemmer = parse_stemmer_language(language).map(Stemmer::create);

    // (character_lc, attribute_lc) → ordered (chapter,
    // value, normalised) rows.
    let mut groups: BTreeMap<(String, String), Vec<(String, String, String)>> =
        BTreeMap::new();
    for f in &bible.facts {
        let key = (
            f.character.trim().to_lowercase(),
            f.attribute.trim().to_lowercase(),
        );
        let norm = normalise(&f.value, &stemmer);
        groups.entry(key).or_default().push((
            f.chapter.clone(),
            f.value.clone(),
            norm,
        ));
    }

    let mut drifts = Vec::new();
    for ((_char_lc, _attr_lc), rows) in groups {
        // Distinct normalised values?
        let distinct: std::collections::HashSet<&String> =
            rows.iter().map(|(_, _, n)| n).collect();
        if distinct.len() < 2 {
            continue;
        }
        // Emit the conflict, keeping one (chapter, value)
        // per distinct normalised value (first seen).
        let mut seen: std::collections::HashSet<String> =
            std::collections::HashSet::new();
        let mut conflicts: Vec<(String, String)> = Vec::new();
        // Recover the original-case character + attribute
        // from the first row's fact.
        for (chapter, value, norm) in &rows {
            if seen.insert(norm.clone()) {
                conflicts.push((chapter.clone(), value.clone()));
            }
        }
        // Find the original-case names from the bible.
        let (character, attribute) = bible
            .facts
            .iter()
            .find(|f| {
                f.character.trim().to_lowercase() == _char_lc
                    && f.attribute.trim().to_lowercase() == _attr_lc
            })
            .map(|f| (f.character.clone(), f.attribute.clone()))
            .unwrap_or((_char_lc.clone(), _attr_lc.clone()));
        drifts.push(Drift {
            character,
            attribute,
            conflicts,
        });
    }

    // Deterministic order.
    drifts.sort_by(|a, b| {
        a.character
            .cmp(&b.character)
            .then_with(|| a.attribute.cmp(&b.attribute))
    });
    drifts
}

/// Parse the AI extraction response — one fact per line in
/// the pipe-delimited form `character | attribute | value`.
/// Tolerant: blank lines + malformed lines are skipped;
/// surrounding markdown / preamble is ignored.  Pure, so
/// the (non-deterministic) live call is the only untested
/// boundary.
pub fn parse_extraction(raw: &str, chapter: &str) -> Vec<CharacterFact> {
    let mut out = Vec::new();
    for line in raw.lines() {
        let line = line.trim().trim_start_matches(['-', '*', '']).trim();
        if line.is_empty() {
            continue;
        }
        let parts: Vec<&str> = line.splitn(3, '|').map(str::trim).collect();
        if parts.len() != 3 {
            continue;
        }
        let (character, attribute, value) = (parts[0], parts[1], parts[2]);
        if character.is_empty() || attribute.is_empty() || value.is_empty() {
            continue;
        }
        // Skip a header row the model might echo.
        if character.eq_ignore_ascii_case("character")
            && attribute.eq_ignore_ascii_case("attribute")
        {
            continue;
        }
        out.push(CharacterFact {
            character: character.to_string(),
            attribute: attribute.to_string().to_lowercase().replace(' ', "_"),
            value: value.to_string(),
            chapter: chapter.to_string(),
        });
    }
    out
}

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

    fn fact(c: &str, a: &str, v: &str, ch: &str) -> CharacterFact {
        CharacterFact {
            character: c.into(),
            attribute: a.into(),
            value: v.into(),
            chapter: ch.into(),
        }
    }

    // ── drift detection ───────────────────────────────

    #[test]
    fn flags_changed_attribute_across_chapters() {
        let bible = ContinuityBible {
            facts: vec![
                fact("Helena", "eye_color", "green", "Ch1"),
                fact("Helena", "eye_color", "brown", "Ch9"),
            ],
            ..Default::default()
        };
        let d = detect_drift(&bible, "english");
        assert_eq!(d.len(), 1);
        assert_eq!(d[0].character, "Helena");
        assert_eq!(d[0].attribute, "eye_color");
        assert_eq!(d[0].conflicts.len(), 2);
    }

    #[test]
    fn consistent_attribute_not_flagged() {
        let bible = ContinuityBible {
            facts: vec![
                fact("Helena", "eye_color", "green", "Ch1"),
                fact("Helena", "eye_color", "green", "Ch9"),
            ],
            ..Default::default()
        };
        assert!(detect_drift(&bible, "english").is_empty());
    }

    #[test]
    fn inflected_value_not_flagged_via_stemmer() {
        // "green eyes" vs "green eye" — same stem; the
        // plural/singular inflection must not flag.
        let bible = ContinuityBible {
            facts: vec![
                fact("Helena", "eyes", "green eyes", "Ch1"),
                fact("Helena", "eyes", "green eye", "Ch9"),
            ],
            ..Default::default()
        };
        assert!(
            detect_drift(&bible, "english").is_empty(),
            "stemmer should collapse eyes/eye",
        );
    }

    #[test]
    fn russian_inflected_value_not_flagged() {
        // "зелёные" vs "зелёными" — same stem after the
        // ё-fold; must not flag.
        let bible = ContinuityBible {
            facts: vec![
                fact("Елена", "глаза", "зелёные", "Гл1"),
                fact("Елена", "глаза", "зелёными", "Гл9"),
            ],
            ..Default::default()
        };
        assert!(
            detect_drift(&bible, "russian").is_empty(),
            "Russian stemmer + ё-fold should collapse the inflections",
        );
    }

    #[test]
    fn russian_genuine_change_flagged() {
        // зелёные (green) vs карие (brown) → real drift.
        let bible = ContinuityBible {
            facts: vec![
                fact("Елена", "глаза", "зелёные", "Гл1"),
                fact("Елена", "глаза", "карие", "Гл9"),
            ],
            ..Default::default()
        };
        let d = detect_drift(&bible, "russian");
        assert_eq!(d.len(), 1);
    }

    #[test]
    fn different_attributes_isolated() {
        let bible = ContinuityBible {
            facts: vec![
                fact("Helena", "eye_color", "green", "Ch1"),
                fact("Helena", "eye_color", "brown", "Ch9"),
                fact("Helena", "hometown", "Harbor", "Ch1"),
                fact("Helena", "hometown", "Harbor", "Ch9"),
            ],
            ..Default::default()
        };
        let d = detect_drift(&bible, "english");
        // Only eye_color drifts; hometown is consistent.
        assert_eq!(d.len(), 1);
        assert_eq!(d[0].attribute, "eye_color");
    }

    #[test]
    fn different_characters_isolated() {
        let bible = ContinuityBible {
            facts: vec![
                fact("Helena", "eye_color", "green", "Ch1"),
                fact("Marcus", "eye_color", "brown", "Ch1"),
            ],
            ..Default::default()
        };
        // Different characters — not a contradiction.
        assert!(detect_drift(&bible, "english").is_empty());
    }

    #[test]
    fn case_insensitive_grouping() {
        let bible = ContinuityBible {
            facts: vec![
                fact("helena", "Eye_Color", "green", "Ch1"),
                fact("Helena", "eye_color", "brown", "Ch9"),
            ],
            ..Default::default()
        };
        // Same character + attribute despite case → drift.
        assert_eq!(detect_drift(&bible, "english").len(), 1);
    }

    // ── sidecar round-trip ────────────────────────────

    #[test]
    fn sidecar_round_trips() {
        let tmp = tempfile::tempdir().unwrap();
        let bible = ContinuityBible {
            version: "1.2.19".into(),
            language: "english".into(),
            facts: vec![fact("Helena", "eye_color", "green", "Ch1")],
        };
        bible.save(tmp.path()).unwrap();
        let loaded = ContinuityBible::load(tmp.path()).unwrap();
        assert_eq!(loaded.facts, bible.facts);
        assert_eq!(loaded.language, "english");
    }

    #[test]
    fn load_missing_sidecar_is_empty() {
        let tmp = tempfile::tempdir().unwrap();
        let b = ContinuityBible::load(tmp.path()).unwrap();
        assert!(b.facts.is_empty());
    }

    #[test]
    fn characters_sorted_and_deduped() {
        let bible = ContinuityBible {
            facts: vec![
                fact("Marcus", "a", "x", "Ch1"),
                fact("Helena", "a", "x", "Ch1"),
                fact("Helena", "b", "y", "Ch2"),
            ],
            ..Default::default()
        };
        assert_eq!(bible.characters(), vec!["Helena", "Marcus"]);
    }

    // ── extraction parsing ────────────────────────────

    #[test]
    fn parses_pipe_delimited_facts() {
        let raw = "Helena | eye color | green\n\
                   Helena | hometown | the Harbor\n\
                   Marcus | occupation | ledger-keeper";
        let f = parse_extraction(raw, "Chapter 1");
        assert_eq!(f.len(), 3);
        assert_eq!(f[0].character, "Helena");
        // "eye color" → snake_case key.
        assert_eq!(f[0].attribute, "eye_color");
        assert_eq!(f[0].value, "green");
        assert_eq!(f[0].chapter, "Chapter 1");
    }

    #[test]
    fn extraction_skips_malformed_and_preamble() {
        let raw = "Here are the facts I found:\n\
                   \n\
                   - Helena | eye color | green\n\
                   this line has no pipes\n\
                   character | attribute | value\n\
                   Marcus | mood |\n\
                   * Marcus | weapon | a lacquered box";
        let f = parse_extraction(raw, "Ch1");
        // Only the two well-formed rows (header + empty-value skipped).
        assert_eq!(f.len(), 2);
        assert_eq!(f[0].character, "Helena");
        assert_eq!(f[1].character, "Marcus");
        assert_eq!(f[1].attribute, "weapon");
    }

    #[test]
    fn extraction_strips_list_markers() {
        let raw = "• Helena | hair | dark";
        let f = parse_extraction(raw, "Ch1");
        assert_eq!(f.len(), 1);
        assert_eq!(f[0].character, "Helena");
        assert_eq!(f[0].value, "dark");
    }
}