inkhaven 1.9.1

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
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
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
//! WBLD-1 (WB-P0) — the `WorldbuilderApp` shell.
//!
//! A third [`crate::tui_host::TuiHost`] consumer beside `research::ResearchApp`
//! and `linguistic::LinguisticApp`. WB-P0 stands up the frame: two (empty) left
//! panes over the Facts and World books, a full-width Query prompt, a right pane
//! (Chat | Research | Map | Ledger), a status bar, the focus/Tab model, and the
//! session sidecar. Later phases fill the panes with behaviour.

use anyhow::Result;
use crossterm::event::{KeyCode, KeyEvent, KeyModifiers};
use ratatui::Frame;
use ratatui::Terminal;
use ratatui::backend::Backend;
use tui_textarea::TextArea;

use tokio::sync::mpsc;

use crate::ai::stream::{ChatTurn as AiTurn, StreamMsg, spawn_chat_stream};
use crate::config::Config;
use crate::project::ProjectLayout;
use crate::store::hierarchy::Hierarchy;
use crate::store::{NodeKind, SYSTEM_TAG_FACTS, SYSTEM_TAG_WORLD, Store};
use crate::system_tree::SystemBookTree;
use crate::tui::theme::Theme;
use crate::tui_host::TuiHost;
use uuid::Uuid;

use super::WorldbuilderInvocation;
use super::focus::{Focus, RightPane};
use super::session::WorldbuilderSession;

/// The tag that marks a Facts-book paragraph as an *invented-world* fact — the
/// `/wfact` output and the `Ctrl+T` toggle. Rendered `◎` in the Facts pane.
pub(super) const FACT_WORLD_TAG: &str = "fact:world";

/// AI cost-dashboard category for worldbuilder chat inferences.
const WB_CATEGORY: &str = "worldbuilder";

/// One chat exchange in the worldbuilder conversation.
pub(super) struct WorldbuilderTurn {
    pub prompt: String,
    pub response: String,
    pub streaming: bool,
}

/// World-book chapters owned by `realworld compile` — read-only in the
/// worldbuilder (change them by editing `world.hjson` + recompiling, not by hand).
/// Matched case-insensitively against a node's title or an ancestor's.
const WORLD_COMPILER_CHAPTERS: &[&str] =
    &["astronomy", "geology", "climate", "hydrology", "demographics"];

pub(crate) struct WorldbuilderApp {
    pub(super) layout: ProjectLayout,
    // Read by WB-P2 (system prompt), WB-P3 (plausibility weights), etc.
    #[allow(dead_code)]
    pub(super) cfg: Config,
    // Read by WB-P2+ (retrieval, world state, fact insert).
    #[allow(dead_code)]
    pub(super) store: Store,
    #[allow(dead_code)]
    pub(super) hierarchy: Hierarchy,

    // — Left pane A: Facts book (rendered + navigated in WB-P1) ————————
    #[allow(dead_code)]
    pub(super) facts_tree: SystemBookTree,
    #[allow(dead_code)]
    pub(super) facts_pins: Vec<Uuid>,

    // — Left pane B: World book (WB-P1) ————————————————————————————————
    #[allow(dead_code)]
    pub(super) world_tree: SystemBookTree,
    #[allow(dead_code)]
    pub(super) world_pins: Vec<Uuid>,

    // — Sizing (persisted to the session) ——————————————————————————————
    pub(super) left_split: u8,  // Facts / World vertical split   (2–8)
    pub(super) split_ratio: u8, // left column / right pane ratio (2–8)

    // — Focus + right pane ————————————————————————————————————————————
    pub(super) focus: Focus,
    pub(super) prev_focus: Focus,
    pub(super) right_pane: RightPane,
    /// `z` zooms one left pane to fill the left column; `Some(pane)` while zoomed.
    pub(super) zoom: Option<Focus>,
    /// `Shift+F` in the Facts pane: emphasise only `fact:world` paragraphs.
    pub(super) facts_filter_world: bool,

    // — Query prompt (full width) ——————————————————————————————————————
    pub(super) query: TextArea<'static>,

    // — Chat (RightPane::Chat) + streaming ————————————————————————————
    pub(super) chat: Vec<WorldbuilderTurn>,
    pub(super) chat_scroll: u16,
    stream_rx: Option<mpsc::UnboundedReceiver<StreamMsg>>,
    streaming_turn: Option<usize>,

    // — World shaping (WB-P4) ——————————————————————————————————————————
    /// Accepted-but-uncommitted edits; `/write` folds them into world.hjson.
    pub(super) pending_ops: Vec<super::commands::Op>,
    /// A shaping delta awaiting the author's y/n confirmation.
    pub(super) hjson_preview: Option<(String, Vec<super::commands::Op>)>,

    // — Plausibility (WB-P3) ——————————————————————————————————————————
    pub(super) plausibility_score: Option<u8>,
    plausibility_prev: Option<u8>,
    plausibility_warnings: Vec<crate::world::plausibility::Warning>,

    // — Compiled world state (WB-P5) ——————————————————————————————————
    /// The last `/compile` result: a deterministic prose summary of the compiled
    /// layer chain. Fed to the chat system prompt instead of the declaration-only
    /// summary when present. Invalidated whenever the world changes.
    compiled_summary: Option<String>,
    /// The compiled layer grids from the last `/compile`, rendered as an ASCII
    /// biome minimap in the Map right-pane (WB-P6). Invalidated with the summary.
    pub(super) compiled_layers: Option<crate::world::plausibility::CompiledLayers>,

    // — World-fact research (WB-P7) ————————————————————————————————————
    /// The last `/research` query + its retrieved Facts passages, shown in the
    /// Research right-pane. `◎` marks passages already tagged `fact:world`.
    pub(super) research_query: Option<String>,
    pub(super) research_hits: Vec<crate::book_rag::RetrievedPassage>,

    // — Interview (WB-P8) ——————————————————————————————————————————————
    /// The active guided interview, if the author started one (`/interview` or
    /// `--interview`). While `Some`, plain Query input answers the current step.
    pub(super) interview: Option<super::interview::Interview>,

    // — Magic ledger (WB-P9) ———————————————————————————————————————————
    /// The magic ledger of the current world (disk + pending), refreshed on every
    /// world change. Rendered + linted in the Ledger right-pane. `None` = no magic.
    pub(super) ledger_snapshot: Option<crate::world::types::MagicLedger>,

    // — Session ————————————————————————————————————————————————————————
    pub(super) session: WorldbuilderSession,

    pub(super) should_quit: bool,
    pub(super) status: String,
    pub(super) show_hints: bool,
    pub(super) theme: Theme,
}

impl WorldbuilderApp {
    pub(crate) fn new(
        layout: ProjectLayout,
        cfg: Config,
        store: Store,
        hierarchy: Hierarchy,
        inv: WorldbuilderInvocation,
    ) -> Result<WorldbuilderApp> {
        let facts_tree = SystemBookTree::new(&hierarchy, SYSTEM_TAG_FACTS);
        let world_tree = SystemBookTree::new(&hierarchy, SYSTEM_TAG_WORLD);

        let now = chrono::Utc::now().to_rfc3339();
        let session_name = inv.session.as_deref().unwrap_or("default");
        let session = WorldbuilderSession::open_or_create(&layout, session_name, now)?;

        let theme = Theme::from_config(&cfg.theme);
        let mut query = TextArea::default();
        query.set_cursor_line_style(ratatui::style::Style::default());

        let left_split = session.left_split.clamp(2, 8);
        let split_ratio = session.split_ratio.clamp(2, 8);

        let mut app = WorldbuilderApp {
            layout,
            cfg,
            store,
            hierarchy,
            facts_tree,
            facts_pins: Vec::new(),
            world_tree,
            world_pins: Vec::new(),
            left_split,
            split_ratio,
            focus: Focus::QueryPrompt,
            prev_focus: Focus::QueryPrompt,
            right_pane: RightPane::Chat,
            zoom: None,
            facts_filter_world: false,
            query,
            chat: Vec::new(),
            chat_scroll: 0,
            stream_rx: None,
            streaming_turn: None,
            // WB-P10 — restore the accepted-but-uncommitted delta from the session.
            pending_ops: session.pending_ops.clone(),
            hjson_preview: None,
            plausibility_score: None,
            compiled_summary: None,
            compiled_layers: None,
            research_query: None,
            research_hits: Vec::new(),
            interview: None,
            ledger_snapshot: None,
            plausibility_prev: None,
            plausibility_warnings: Vec::new(),
            session,
            should_quit: false,
            status: "worldbuilder — Tab cycles panes · Ctrl+Q quits".to_string(),
            show_hints: true,
            theme,
        };
        app.refresh_plausibility();
        if !app.pending_ops.is_empty() {
            app.status = format!(
                "restored {} pending delta(s) from session · /diff to review · /write to commit",
                app.pending_ops.len()
            );
        }
        // `--interview` (and, until the plakat map-first flow lands, `--from-map`)
        // opens straight into the guided interview.
        if inv.interview || inv.from_map {
            app.start_interview();
        }
        Ok(app)
    }

    pub(crate) fn run<B: Backend>(&mut self, terminal: &mut Terminal<B>) -> Result<()> {
        crate::tui_host::run_loop(self, terminal)
    }

    /// The `world_name` for the title bar (session, else placeholder).
    pub(super) fn world_name(&self) -> &str {
        if self.session.world_name.is_empty() {
            "untitled world"
        } else {
            &self.session.world_name
        }
    }

    /// Persist pane sizing back to the session (best-effort).
    fn persist_sizing(&mut self) {
        self.session.left_split = self.left_split;
        self.session.split_ratio = self.split_ratio;
        if let Err(e) = self.session.save(&self.layout) {
            self.status = format!("session save failed: {e}");
        }
    }

    /// WB-P10 — mirror the live pending delta + world name into the session and
    /// persist. Called after every change to `pending_ops`, so an accepted-but-
    /// uncommitted delta survives a quit.
    fn persist_pending(&mut self) {
        self.session.pending_ops = self.pending_ops.clone();
        if self.session.world_name.is_empty() {
            self.session.world_name = self.world_name().to_string();
        }
        if let Err(e) = self.session.save(&self.layout) {
            self.status = format!("session save failed: {e}");
        }
    }

    /// WB-P10 — append a turn to the session timeline (the "Worldbuilding
    /// Journey") and persist. `facts` are Facts-book node ids created this turn.
    fn record_turn(&mut self, user: String, summary: String, facts: Vec<String>) {
        let seq = self.session.turns.len() as u64 + 1;
        let at = chrono::Utc::now().to_rfc3339();
        self.session.turns.push(super::session::SessionTurn {
            seq,
            at,
            user,
            assistant_summary: summary,
            plausibility_before: self.plausibility_prev,
            plausibility_after: self.plausibility_score,
            facts_inserted: facts,
            ..Default::default()
        });
        // Keep the pending delta persisted in the same write.
        self.session.pending_ops = self.pending_ops.clone();
        if let Err(e) = self.session.save(&self.layout) {
            self.status = format!("session save failed: {e}");
        }
    }

    fn handle_key(&mut self, key: KeyEvent) {
        let ctrl = key.modifiers.contains(KeyModifiers::CONTROL);
        // Universal quit.
        if ctrl && matches!(key.code, KeyCode::Char('c') | KeyCode::Char('q')) {
            self.should_quit = true;
            return;
        }
        // Ctrl+R cycles the right-pane view from anywhere.
        if ctrl && key.code == KeyCode::Char('r') {
            self.right_pane = self.right_pane.next();
            self.status = format!("right pane: {}", self.right_pane.title());
            return;
        }
        // Focus movement (the overlay, once it exists, is sticky via Focus::next).
        match key.code {
            KeyCode::Tab => {
                self.focus = self.focus.next();
                return;
            }
            KeyCode::BackTab => {
                self.focus = self.focus.prev();
                return;
            }
            _ => {}
        }

        // Pane-specific keys.
        match self.focus {
            Focus::QueryPrompt => match key.code {
                KeyCode::Esc => {
                    // Esc leaves an active interview first; then clears the line;
                    // then steps focus back to where we came from.
                    if self.interview.is_some() {
                        self.interview = None;
                        self.query.select_all();
                        self.query.cut();
                        self.status = "interview left — pending edits kept (/diff to review)".into();
                    } else if self.query.is_empty() {
                        self.focus = self.prev_focus;
                    } else {
                        self.query.select_all();
                        self.query.cut();
                    }
                }
                KeyCode::Enter => {
                    let text = self.query.lines().join("\n");
                    self.query.select_all();
                    self.query.cut();
                    let trimmed = text.trim();
                    // While interviewing, plain text answers the current step; a
                    // `/command` still runs (so /diff, /write work mid-interview).
                    if self.interview.is_some() && !trimmed.starts_with('/') {
                        self.submit_interview_answer(trimmed);
                    } else if trimmed.is_empty() {
                        // nothing to send
                    } else if trimmed.starts_with('/') {
                        self.dispatch_command(&text);
                    } else {
                        self.send_chat(text);
                    }
                }
                _ => {
                    self.query.input(key);
                }
            },
            Focus::FactsPane => self.left_pane_key(key, true),
            Focus::WorldPane => self.left_pane_key(key, false),
            Focus::RightPane => {
                if self.common_pane_key(key) {
                    return;
                }
                if self.right_pane == RightPane::Chat {
                    match key.code {
                        // `u16::MAX` pins to the bottom (also while streaming).
                        KeyCode::Char('G') => self.chat_scroll = u16::MAX,
                        KeyCode::Char('g') => self.chat_scroll = 0,
                        KeyCode::Char('k') | KeyCode::Up => {
                            if self.chat_scroll != u16::MAX {
                                self.chat_scroll = self.chat_scroll.saturating_sub(1);
                            }
                        }
                        KeyCode::Char('j') | KeyCode::Down => {
                            if self.chat_scroll != u16::MAX {
                                self.chat_scroll = self.chat_scroll.saturating_add(1);
                            }
                        }
                        _ => {}
                    }
                }
            }
            Focus::ConfirmationOverlay => match key.code {
                KeyCode::Char('y') => self.accept_pending(),
                KeyCode::Char('n') | KeyCode::Esc => {
                    self.hjson_preview = None;
                    self.focus = Focus::QueryPrompt;
                    self.status = "delta discarded".into();
                }
                _ => {}
            },
        }
    }

    /// Route a `/command` from the Query prompt (WB-P4).
    fn dispatch_command(&mut self, input: &str) {
        use super::commands::Command;
        match super::commands::parse(input) {
            Command::Shape { label, ops } => {
                // Preview before it enters the pending delta.
                self.hjson_preview = Some((label, ops));
                self.focus = Focus::ConfirmationOverlay;
                self.status = "review the delta — y accept · n discard".into();
            }
            Command::Write => self.write_pending(),
            Command::Undo => self.undo_pending(),
            Command::Reset => {
                let n = self.pending_ops.len();
                self.pending_ops.clear();
                self.refresh_plausibility();
                self.persist_pending();
                self.status = format!("reset — discarded {n} pending delta(s)");
            }
            Command::Diff => {
                self.status = if self.pending_ops.is_empty() {
                    "no pending deltas".into()
                } else {
                    format!(
                        "{} pending delta(s): {}",
                        self.pending_ops.len(),
                        self.pending_ops.iter().map(|o| o.preview()).collect::<Vec<_>>().join(" · ")
                    )
                };
            }
            Command::Compile => self.run_compile(),
            Command::Validate => self.run_validate(),
            Command::Wfact(text) => self.run_wfact(&text),
            Command::Research(query) => self.run_research(&query),
            Command::Interview => self.start_interview(),
            Command::Journey => self.run_journey(),
            Command::Sessions => self.run_sessions(),
            Command::Export => self.run_export(),
            Command::Unknown(msg) => self.status = msg,
        }
    }

    /// Collect every `fact:world` paragraph as `(title, body)`, in tree order.
    fn collect_world_facts(&self) -> Vec<(String, String)> {
        let Some(book_id) = self.facts_tree.root else { return Vec::new() };
        let mut out = Vec::new();
        // Walk the Facts tree depth-first so facts export in reading order.
        let mut stack: Vec<Uuid> = self
            .hierarchy
            .children_of(Some(book_id))
            .into_iter()
            .rev()
            .map(|n| n.id)
            .collect();
        while let Some(id) = stack.pop() {
            if let Some(node) = self.hierarchy.get(id) {
                if node.kind == NodeKind::Paragraph
                    && node.tags.iter().any(|t| t == FACT_WORLD_TAG)
                {
                    let body = self
                        .store
                        .get_content(id)
                        .ok()
                        .flatten()
                        .map(|b| String::from_utf8_lossy(&b).into_owned())
                        .unwrap_or_default();
                    out.push((node.title.clone(), body));
                }
                for child in self.hierarchy.children_of(Some(id)).into_iter().rev() {
                    stack.push(child.id);
                }
            }
        }
        out
    }

    /// `/export` — assemble a readable Markdown dossier (compiled state,
    /// plausibility, ledger, `fact:world` facts, and the Journey) and write it
    /// atomically under `exports/`. Compiles the world fresh so it works even
    /// before `/compile`.
    fn run_export(&mut self) {
        let compiled = self
            .current_world_def()
            .map(|def| {
                let layers = crate::world::plausibility::compile_layers(&def);
                crate::world::plausibility::summarise_compiled(&def, &layers)
            });
        let facts = self.collect_world_facts();
        let at = chrono::Utc::now().to_rfc3339();
        let world_name = self.world_name().to_string();
        let md = {
            let input = super::export::DossierInput {
                world_name: &world_name,
                generated_at: &at,
                compiled: compiled.as_deref(),
                score: self.plausibility_score,
                warnings: &self.plausibility_warnings,
                ledger: self.ledger_snapshot.as_ref(),
                facts: &facts,
                journey: &self.session.turns,
            };
            super::export::build_dossier(&input)
        };
        let dir = self.layout.root.join("exports");
        if let Err(e) = std::fs::create_dir_all(&dir) {
            self.status = format!("export failed: {e}");
            return;
        }
        let path = dir.join(format!("dossier-{}.md", self.session.slug));
        match crate::io_atomic::write(&path, md.as_bytes()) {
            Ok(()) => {
                let rel = path.strip_prefix(&self.layout.root).unwrap_or(&path).display();
                self.push_turn("/export".into(), format!("Wrote world dossier → {rel}"));
                self.status = format!("exported → {rel}");
            }
            Err(e) => self.status = format!("export failed: {e}"),
        }
    }

    /// `/journey` — render the session timeline (the Worldbuilding Journey) into
    /// the Chat pane: each recorded turn with its plausibility arc.
    fn run_journey(&mut self) {
        let count = self.session.turns.len();
        if count == 0 {
            self.push_turn("/journey".into(), "No journey yet — shape the world and it fills in.".into());
            self.status = "journey — empty".into();
            return;
        }
        let mut body = format!(
            "Worldbuilding Journey · {} · {count} step(s)\n",
            self.session.name,
        );
        for t in &self.session.turns {
            // Trim the timestamp to the date+minute for a compact line.
            let when = t.at.get(..16).unwrap_or(&t.at);
            let arc = match (t.plausibility_before, t.plausibility_after) {
                (Some(b), Some(a)) if a != b => format!("{b}{a}"),
                (_, Some(a)) => format!("{a}"),
                _ => String::new(),
            };
            let facts = if t.facts_inserted.is_empty() {
                String::new()
            } else {
                format!("{}", t.facts_inserted.len())
            };
            body.push_str(&format!(
                "{:>3}. {when}  {}{}{arc}{facts}\n",
                t.seq,
                t.user.trim(),
                t.assistant_summary.trim(),
            ));
        }
        self.push_turn("/journey".into(), body);
        self.status = format!("journey — {count} step(s)");
    }

    /// `/sessions` — list the project's worldbuilder sessions (the current one
    /// marked). Session switching is a later refinement; this surfaces what exists.
    fn run_sessions(&mut self) {
        let slugs = super::session::WorldbuilderSession::list(&self.layout);
        if slugs.is_empty() {
            self.status = "no sessions".into();
            return;
        }
        let list = slugs
            .iter()
            .map(|s| if *s == self.session.slug { format!("{s} (current)") } else { format!("  {s}") })
            .collect::<Vec<_>>()
            .join("\n");
        self.push_turn("/sessions".into(), format!("Sessions:\n{list}"));
        self.status = format!("{} session(s) — re-open with `--session <name>`", slugs.len());
    }

    /// Start (or restart) the guided interview: focus the Query prompt, surface
    /// the Chat pane, and post the first question. Answers accumulate into the
    /// pending delta (reviewable with `/diff`, committed with `/write`).
    fn start_interview(&mut self) {
        let iv = super::interview::Interview::new();
        self.interview = Some(iv);
        self.right_pane = RightPane::Chat;
        self.focus = Focus::QueryPrompt;
        self.push_turn(
            String::new(),
            "Interview — I'll ask about the sky, land, people, and rules. Answer in your own \
             words (blank to skip a question, Esc to leave). Your answers become pending edits; \
             review them with /diff and commit with /write, then /compile."
                .to_string(),
        );
        self.post_interview_question();
        self.status = "interview started — answer in the Query prompt · Esc to leave".into();
    }

    /// Post the current interview step as a chat turn (pinned to the bottom, so it
    /// stays visible as the conversation grows).
    fn post_interview_question(&mut self) {
        let q = self.interview.as_ref().and_then(|iv| {
            iv.current().map(|s| {
                let (n, total) = iv.progress();
                format!("[{} · {n}/{total}] {}", s.stage.label(), s.prompt)
            })
        });
        if let Some(q) = q {
            self.push_turn(String::new(), q);
        }
    }

    /// Feed one answer to the active interview: fill the current step's command
    /// template, record its ops into the pending delta, and advance. A blank
    /// answer skips; a malformed one is reported and the step is retried.
    fn submit_interview_answer(&mut self, answer: &str) {
        let answer = answer.trim();
        let Some(step) = self.interview.as_ref().and_then(|iv| iv.current()) else {
            self.interview = None;
            return;
        };
        // Blank → skip this question.
        if answer.is_empty() {
            if let Some(iv) = self.interview.as_mut() {
                iv.advance();
            }
            self.push_turn(String::new(), "(skipped)".into());
            self.after_interview_step();
            return;
        }

        let cmd = step.template.replace("{}", answer);
        match super::commands::parse(&cmd) {
            super::commands::Command::Shape { label, ops } => {
                self.pending_ops.extend(ops);
                self.refresh_plausibility();
                let d = self.plausibility_delta_chip();
                let note = if d.is_empty() {
                    format!("recorded · {label}")
                } else {
                    format!("recorded · {label}  (★ {d})")
                };
                self.push_turn(answer.to_string(), note);
                self.record_turn(format!("interview: {answer}"), label, Vec::new());
                if let Some(iv) = self.interview.as_mut() {
                    iv.advance();
                }
                self.after_interview_step();
            }
            super::commands::Command::Unknown(msg) => {
                // Keep the step; let the author retry.
                self.push_turn(answer.to_string(), format!("didn't take that — {msg}"));
            }
            _ => {
                // A template that parses to a session command shouldn't happen;
                // skip defensively rather than loop.
                if let Some(iv) = self.interview.as_mut() {
                    iv.advance();
                }
                self.after_interview_step();
            }
        }
    }

    /// Post the next question, or close the interview when the script is done.
    fn after_interview_step(&mut self) {
        let done = self.interview.as_ref().map(|iv| iv.done()).unwrap_or(true);
        if done {
            self.interview = None;
            let n = self.pending_ops.len();
            self.push_turn(
                String::new(),
                format!(
                    "That's the frame — {n} pending edit(s). Review with /diff, commit with \
                     /write, then /compile to see the world your choices imply."
                ),
            );
            self.status = format!("interview complete — {n} pending edit(s) · /diff · /write");
        } else {
            self.post_interview_question();
            self.status = "interview — answer in the Query prompt · Esc to leave".into();
        }
    }

    /// `/wfact <statement>` — record an author-decided world fact into the Facts
    /// book, tagged `fact:world`, using the shared research insertion primitive
    /// (auto-reembeds → immediately retrievable). No AI: the author decides, the
    /// worldbuilder records. Inserts near the tree cursor when it is inside Facts,
    /// else at the end of the book; then reveals the new fact.
    fn run_wfact(&mut self, text: &str) {
        let text = text.trim();
        if text.is_empty() {
            return;
        }
        let Some(book_id) = self.facts_tree.root else {
            self.status = "no Facts book — create one in the editor first".into();
            return;
        };
        // A short title from the first clause/line; the full statement is the body.
        let title: String = text.split(['.', '\n']).next().unwrap_or(text).trim().chars().take(60).collect();
        let target = self.facts_tree.selected();
        let new_id = match crate::research::insert::insert_paragraph(
            &self.store,
            &self.cfg,
            &self.hierarchy,
            book_id,
            target,
            &title,
            text,
        ) {
            Ok(id) => id,
            Err(e) => {
                self.status = format!("fact write failed: {e}");
                return;
            }
        };
        // Tag it fact:world (the whole point — it must show as ◎ and feed the
        // world-fact RAG). Reload first so the node is in the hierarchy.
        self.reload_hierarchy();
        if let Some(node) = self.hierarchy.get(new_id) {
            let mut updated = node.clone();
            if !updated.tags.iter().any(|t| t == FACT_WORLD_TAG) {
                updated.tags.push(FACT_WORLD_TAG.to_string());
                if let Err(e) = self.store.raw().update_metadata(new_id, updated.to_json()) {
                    self.status = format!("recorded, but tag write failed: {e}");
                    return;
                }
            }
        }
        self.reload_hierarchy();
        self.facts_tree.reveal(&self.hierarchy, new_id);
        self.record_turn(format!("/wfact {title}"), "◎ recorded fact:world".into(), vec![new_id.to_string()]);
        self.status = "◎ recorded fact:world".into();
    }

    /// `/research <query>` — semantically retrieve related Facts (the whole Facts
    /// book, not only `fact:world`, so the author can discover material to promote)
    /// and surface them in the Research right-pane. Pure retrieval, no generation.
    fn run_research(&mut self, query: &str) {
        let query = query.trim();
        if query.is_empty() {
            return;
        }
        let Some(book_id) = self.facts_tree.root else {
            self.status = "no Facts book to research".into();
            return;
        };
        match crate::book_rag::retrieval::retrieve(
            &self.store,
            &self.hierarchy,
            &self.cfg.book_rag,
            book_id,
            query,
        ) {
            Ok(hits) => {
                let n = hits.len();
                self.research_hits = hits;
                self.research_query = Some(query.to_string());
                self.right_pane = RightPane::Research;
                self.status = format!("research · {n} passage(s) for “{query}” · Ctrl+R → Research");
            }
            Err(e) => self.status = format!("research failed: {e}"),
        }
    }

    /// `/compile` — run the pure layer chain over the current world (disk +
    /// pending), cache a compiled-state summary for the chat prompt, and echo it
    /// into the conversation as a Simulation turn. No LLM, no disk writes.
    fn run_compile(&mut self) {
        match self.current_world_def() {
            Some(def) => {
                let layers = crate::world::plausibility::compile_layers(&def);
                let summary = crate::world::plausibility::summarise_compiled(&def, &layers);
                self.compiled_summary = Some(summary.clone());
                self.compiled_layers = Some(layers);
                self.push_turn("/compile".into(), format!("Compiled world state —\n{summary}"));
                self.status =
                    "compiled — chat reasons over the simulated world · Ctrl+R → Map".into();
            }
            None => {
                self.status = "no world to compile — declare one first (interview or /set)".into();
            }
        }
    }

    /// `/validate` — run the deterministic plausibility lints over the current
    /// world and report the score + warnings into the conversation. Reuses the
    /// already-computed `plausibility_warnings` (kept fresh by every delta).
    fn run_validate(&mut self) {
        match self.plausibility_score {
            Some(score) => {
                let mut body = format!("Plausibility {score}/100");
                if self.plausibility_warnings.is_empty() {
                    body.push_str(" — no warnings.");
                } else {
                    body.push_str(&format!("{} warning(s):\n", self.plausibility_warnings.len()));
                    for w in &self.plausibility_warnings {
                        let sev = match w.severity {
                            crate::world::plausibility::Severity::High => "HIGH",
                            crate::world::plausibility::Severity::Medium => "MED ",
                            crate::world::plausibility::Severity::Low => "LOW ",
                        };
                        body.push_str(&format!("  [{sev}] {}\n", w.text));
                    }
                }
                self.push_turn("/validate".into(), body);
                self.status = format!("validated — plausibility {score}/100");
            }
            None => {
                self.status = "no world to validate — declare one first (interview or /set)".into();
            }
        }
    }

    /// `y` in the preview — fold the previewed ops into the pending delta and
    /// rescore (the score responds before `/write` commits to disk).
    fn accept_pending(&mut self) {
        if let Some((label, ops)) = self.hjson_preview.take() {
            self.pending_ops.extend(ops);
            self.refresh_plausibility();
            let d = self.plausibility_delta_chip();
            self.status = if d.is_empty() {
                format!("{label} · /write to commit")
            } else {
                format!("{label}{d} · /write to commit")
            };
            self.record_turn(label, "shaping delta accepted".into(), Vec::new());
        }
        self.focus = Focus::QueryPrompt;
    }

    /// `/write` — fold every pending op into `world.hjson` atomically.
    fn write_pending(&mut self) {
        if self.pending_ops.is_empty() {
            self.status = "nothing to write".into();
            return;
        }
        let path = self.layout.root.join("world.hjson");
        let mut value = std::fs::read_to_string(&path)
            .ok()
            .and_then(|r| serde_hjson::from_str::<serde_json::Value>(&r).ok())
            .unwrap_or_else(|| serde_json::json!({}));
        for op in &self.pending_ops {
            op.apply(&mut value);
        }
        let json = serde_json::to_string_pretty(&value).unwrap_or_default();
        match crate::io_atomic::write(&path, json.as_bytes()) {
            Ok(()) => {
                let n = self.pending_ops.len();
                self.pending_ops.clear();
                self.refresh_plausibility();
                self.status = format!("✓ wrote {n} delta(s) to world.hjson");
                self.record_turn("/write".into(), format!("committed {n} delta(s)"), Vec::new());
            }
            Err(e) => self.status = format!("write failed: {e}"),
        }
    }

    /// `/undo` — drop the last pending op and rescore.
    fn undo_pending(&mut self) {
        if self.pending_ops.pop().is_some() {
            self.refresh_plausibility();
            self.persist_pending();
            self.status = format!("undone — {} pending delta(s) left", self.pending_ops.len());
        } else {
            self.status = "nothing to undo".into();
        }
    }

    /// Shell-wide keys reachable from any navigable pane. Returns whether the key
    /// was consumed.
    fn common_pane_key(&mut self, key: KeyEvent) -> bool {
        match key.code {
            KeyCode::Char('?') => {
                self.show_hints = !self.show_hints;
                true
            }
            KeyCode::Char('q') => {
                self.should_quit = true;
                true
            }
            KeyCode::Char('{') => {
                self.left_split = self.left_split.saturating_sub(1).clamp(2, 8);
                self.persist_sizing();
                true
            }
            KeyCode::Char('}') => {
                self.left_split = (self.left_split + 1).clamp(2, 8);
                self.persist_sizing();
                true
            }
            KeyCode::Char('[') => {
                self.split_ratio = self.split_ratio.saturating_sub(1).clamp(2, 8);
                self.persist_sizing();
                true
            }
            KeyCode::Char(']') => {
                self.split_ratio = (self.split_ratio + 1).clamp(2, 8);
                self.persist_sizing();
                true
            }
            _ => false,
        }
    }

    /// Cursor / fold navigation on the focused tree. Returns whether consumed.
    fn tree_nav_key(&mut self, key: KeyEvent) -> bool {
        let (tree, h) = match self.focus {
            Focus::WorldPane => (&mut self.world_tree, &self.hierarchy),
            _ => (&mut self.facts_tree, &self.hierarchy),
        };
        match key.code {
            KeyCode::Char('j') | KeyCode::Down => {
                tree.move_down();
                true
            }
            KeyCode::Char('k') | KeyCode::Up => {
                tree.move_up();
                true
            }
            KeyCode::Char('g') => {
                tree.to_top();
                true
            }
            KeyCode::Char('G') => {
                tree.to_bottom();
                true
            }
            KeyCode::Char('l') | KeyCode::Right => {
                tree.step_in(h);
                true
            }
            KeyCode::Char('h') | KeyCode::Left => {
                tree.step_out(h);
                true
            }
            KeyCode::Enter => {
                tree.toggle(h);
                true
            }
            _ => false,
        }
    }

    /// Keys for a left (Facts / World) pane. `is_facts` selects Facts-only keys.
    fn left_pane_key(&mut self, key: KeyEvent, is_facts: bool) {
        let ctrl = key.modifiers.contains(KeyModifiers::CONTROL);
        if ctrl {
            match key.code {
                KeyCode::Char('p') => {
                    self.pin_toggle();
                    return;
                }
                KeyCode::Char('t') if is_facts => {
                    self.toggle_world_tag();
                    return;
                }
                _ => {}
            }
        }
        if self.common_pane_key(key) {
            return;
        }
        if self.tree_nav_key(key) {
            return;
        }
        match key.code {
            KeyCode::Char('z') => {
                self.zoom = if self.zoom == Some(self.focus) { None } else { Some(self.focus) };
            }
            KeyCode::Char('F') if is_facts => {
                self.facts_filter_world = !self.facts_filter_world;
                self.status = if self.facts_filter_world {
                    "filter: fact:world only".into()
                } else {
                    "filter off".into()
                };
            }
            // Tree editing: WB-P1 rejects compiler-owned World nodes; author edits
            // (add / edit / rename / delete modals) land in a later phase.
            KeyCode::Char('n') | KeyCode::Char('e') | KeyCode::Char('r') | KeyCode::Char('d') => {
                self.reject_or_stub_edit(is_facts);
            }
            _ => {}
        }
    }

    /// `Ctrl+P` — toggle the selected node in the focused pane's pin list.
    fn pin_toggle(&mut self) {
        let sel = match self.focus {
            Focus::WorldPane => self.world_tree.selected(),
            _ => self.facts_tree.selected(),
        };
        let Some(id) = sel else { return };
        let pins = match self.focus {
            Focus::WorldPane => &mut self.world_pins,
            _ => &mut self.facts_pins,
        };
        if let Some(pos) = pins.iter().position(|p| *p == id) {
            pins.remove(pos);
            self.status = "unpinned".into();
        } else {
            pins.push(id);
            self.status = "pinned".into();
        }
    }

    /// `Ctrl+T` (Facts pane) — add/remove the `fact:world` tag on the selected
    /// paragraph, persisted to the store.
    fn toggle_world_tag(&mut self) {
        let Some(id) = self.facts_tree.selected() else { return };
        let Some(node) = self.hierarchy.get(id) else { return };
        if node.kind != NodeKind::Paragraph {
            self.status = "select a fact (a paragraph) to tag fact:world".into();
            return;
        }
        let mut updated = node.clone();
        let now_tagged = match updated.tags.iter().position(|t| t == FACT_WORLD_TAG) {
            Some(pos) => {
                updated.tags.remove(pos);
                false
            }
            None => {
                updated.tags.push(FACT_WORLD_TAG.to_string());
                true
            }
        };
        if let Err(e) = self.store.raw().update_metadata(id, updated.to_json()) {
            self.status = format!("tag write failed: {e}");
            return;
        }
        self.reload_hierarchy();
        self.status = if now_tagged {
            "◎ tagged fact:world".into()
        } else {
            "untagged fact:world".into()
        };
    }

    fn reject_or_stub_edit(&mut self, is_facts: bool) {
        if !is_facts {
            if let Some(id) = self.world_tree.selected() {
                if self.is_world_compiler_owned(id) {
                    self.status =
                        "Managed by realworld compile — edit world.hjson, then /compile".into();
                    return;
                }
            }
        }
        self.status = "tree editing (add/edit/rename/delete) lands in a later phase".into();
    }

    /// Send a plain-language question to the World Builder AI, grounding it in the
    /// world state, pins, and retrieved world facts (WB-P2). Slash commands are
    /// routed elsewhere (WB-P4).
    fn send_chat(&mut self, query: String) {
        let query = query.trim().to_string();
        if query.is_empty() {
            return;
        }
        if self.stream_rx.is_some() {
            self.status = "a response is still streaming…".into();
            return;
        }
        let ai = match crate::ai::AiClient::from_config(&self.cfg.llm) {
            Ok(a) => a,
            Err(e) => {
                self.push_turn(query, format!("[no LLM provider: {e}]"));
                return;
            }
        };
        let (model, _env) = match ai.resolve_provider(&self.cfg.llm, None) {
            Ok(m) => m,
            Err(e) => {
                self.push_turn(query, format!("[provider error: {e}]"));
                return;
            }
        };

        // Assemble the four context sections (a leading warnings section arrives
        // in WB-P3). All immutable borrows — build the owned `system` before we
        // mutate the chat below.
        // Prefer the compiled world state (from `/compile`) — it reports what the
        // physics produced, not just what was declared — falling back to the
        // declaration summary until the author runs `/compile`.
        let world_state = self
            .compiled_summary
            .clone()
            .or_else(|| super::prompt::world_declaration_summary(&self.layout.root))
            .unwrap_or_default();
        let pinned_world = super::prompt::pinned_nodes_text(&self.store, &self.hierarchy, &self.world_pins);
        let world_facts = self
            .facts_tree
            .root
            .map(|bid| {
                super::prompt::retrieve_world_facts(&self.store, &self.hierarchy, &self.cfg, bid, &query)
            })
            .unwrap_or_default();
        let pinned_facts = super::prompt::pinned_nodes_text(&self.store, &self.hierarchy, &self.facts_pins);
        let warnings = self.plausibility_warnings_text();
        let system = super::prompt::build_system_prompt(
            self.world_name(),
            &self.cfg.language,
            &warnings,
            &world_state,
            &pinned_world,
            &world_facts,
            &pinned_facts,
        );
        let history = self.replay_history();

        self.chat.push(WorldbuilderTurn {
            prompt: query.clone(),
            response: String::new(),
            streaming: true,
        });
        self.streaming_turn = Some(self.chat.len() - 1);
        self.chat_scroll = u16::MAX; // pin to bottom while streaming
        self.right_pane = RightPane::Chat; // surface the answer
        self.status = "asking the World Builder…".into();

        let rx = spawn_chat_stream(
            ai.client.clone(),
            model.to_string(),
            Some(system),
            history,
            query,
            WB_CATEGORY,
        );
        self.stream_rx = Some(rx);
    }

    fn push_turn(&mut self, prompt: String, response: String) {
        self.chat.push(WorldbuilderTurn { prompt, response, streaming: false });
        self.chat_scroll = u16::MAX;
    }

    /// Prior completed turns, replayed to the model for follow-up context.
    fn replay_history(&self) -> Vec<AiTurn> {
        let mut h = Vec::new();
        for t in &self.chat {
            if t.streaming {
                continue;
            }
            h.push(AiTurn::User(t.prompt.clone()));
            if !t.response.trim().is_empty() && !t.response.starts_with('[') {
                h.push(AiTurn::Assistant(t.response.clone()));
            }
        }
        h
    }

    /// Drain the streaming channel into the in-flight turn (called each frame).
    fn drain_stream(&mut self) {
        let Some(rx) = self.stream_rx.as_mut() else { return };
        let Some(idx) = self.streaming_turn else { return };
        let mut done = false;
        loop {
            match rx.try_recv() {
                Ok(StreamMsg::Token(t)) => {
                    if let Some(turn) = self.chat.get_mut(idx) {
                        turn.response.push_str(&t);
                    }
                }
                Ok(StreamMsg::Done(_)) => {
                    done = true;
                    break;
                }
                Ok(StreamMsg::Error(e)) => {
                    if let Some(turn) = self.chat.get_mut(idx) {
                        turn.response.push_str(&format!("\n[error: {e}]"));
                    }
                    done = true;
                    break;
                }
                Err(mpsc::error::TryRecvError::Empty) => break,
                Err(mpsc::error::TryRecvError::Disconnected) => {
                    done = true;
                    break;
                }
            }
        }
        if done {
            self.stream_rx = None;
            self.streaming_turn = None;
            if let Some(turn) = self.chat.get_mut(idx) {
                turn.streaming = false;
            }
            self.status = "ready".into();
        }
    }

    /// WB-P3 — recompute the deterministic plausibility score + warnings from
    /// `world.hjson`. `None` when there is no world yet. Called on load; WB-P4
    /// deltas will re-trigger it. No LLM — `run_fast` compiles + lints.
    /// The world as it currently stands: `world.hjson` on disk PLUS the pending
    /// (accepted-but-uncommitted) deltas. `None` when there is no parseable world
    /// yet. Shared by the plausibility score, `/compile`, and `/validate` so they
    /// all reason over the same in-progress world.
    pub(super) fn current_world_def(&self) -> Option<crate::world::types::WorldDefinition> {
        let mut value = std::fs::read_to_string(self.layout.root.join("world.hjson"))
            .ok()
            .and_then(|r| serde_hjson::from_str::<serde_json::Value>(&r).ok())
            .unwrap_or_else(|| serde_json::json!({}));
        for op in &self.pending_ops {
            op.apply(&mut value);
        }
        serde_json::from_value::<crate::world::types::WorldDefinition>(value).ok()
    }

    pub(super) fn refresh_plausibility(&mut self) {
        // Score the world.hjson on disk PLUS the pending (accepted-but-uncommitted)
        // deltas, so the score responds the moment a delta is accepted. Any world
        // change also invalidates the cached `/compile` summary + map grids.
        self.compiled_summary = None;
        self.compiled_layers = None;
        let def = self.current_world_def();
        self.ledger_snapshot = def.as_ref().and_then(|d| d.magic.clone());
        self.plausibility_prev = self.plausibility_score;
        match def {
            Some(def) => {
                let warnings = crate::world::plausibility::run_fast(&def);
                self.plausibility_score =
                    Some(crate::world::plausibility::compute_plausibility_score(&warnings));
                self.plausibility_warnings = warnings;
            }
            None => {
                self.plausibility_score = None;
                self.plausibility_warnings.clear();
            }
        }
    }

    /// The score change since the previous recompute, as a status chip.
    pub(super) fn plausibility_delta_chip(&self) -> String {
        match (self.plausibility_prev, self.plausibility_score) {
            (Some(p), Some(c)) if c > p => format!("{}", c - p),
            (Some(p), Some(c)) if c < p => format!("{}", p - c),
            _ => String::new(),
        }
    }

    /// The plausibility warnings formatted for the chat system prompt's WARNINGS
    /// section (top N; `run_fast` order already groups by layer).
    fn plausibility_warnings_text(&self) -> String {
        let mut s = String::new();
        for w in self.plausibility_warnings.iter().take(8) {
            s.push_str(&format!("! {}\n", w.text));
        }
        s
    }

    /// Whether a World node is owned by `realworld compile` (one of the compiled
    /// layers, or a descendant of one) — read-only in the worldbuilder.
    pub(super) fn is_world_compiler_owned(&self, id: Uuid) -> bool {
        node_is_compiler_owned(&self.hierarchy, id)
    }

    fn reload_hierarchy(&mut self) {
        if let Ok(h) = Hierarchy::load(&self.store) {
            self.hierarchy = h;
            self.facts_tree.rebuild(&self.hierarchy);
            self.world_tree.rebuild(&self.hierarchy);
        }
    }
}

impl TuiHost for WorldbuilderApp {
    fn should_quit(&self) -> bool {
        self.should_quit
    }

    fn poll_async(&mut self) {
        self.drain_stream();
    }

    fn render(&self, frame: &mut Frame) {
        super::render::render(frame, self);
    }

    fn on_key(&mut self, key: KeyEvent) {
        // Remember where non-query focus was, so Esc from the prompt returns there.
        if self.focus != Focus::QueryPrompt {
            self.prev_focus = self.focus;
        }
        self.handle_key(key);
    }
}

/// Whether a World-book node is owned by `realworld compile` — it *is* one of the
/// compiled layer chapters (Astronomy/…/Demographics), or descends from one.
/// Free function so it's testable against a `Hierarchy` without a live store.
pub(super) fn node_is_compiler_owned(h: &Hierarchy, id: Uuid) -> bool {
    let Some(node) = h.get(id) else { return false };
    let is_layer = |n: &crate::store::node::Node| {
        WORLD_COMPILER_CHAPTERS.contains(&n.title.trim().to_lowercase().as_str())
    };
    is_layer(node) || h.ancestors(node).iter().any(|a| is_layer(a))
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::store::node::Node;

    fn wnode(
        id: Uuid,
        kind: &str,
        title: &str,
        parent: Option<Uuid>,
        order: u32,
        system_tag: Option<&str>,
        tags: &[&str],
    ) -> Node {
        let mut raw = serde_json::json!({
            "id": id,
            "kind": kind,
            "title": title,
            "slug": title.to_lowercase(),
            "path": [],
            "parent_id": parent,
            "order": order,
            "file": null,
            "modified_at": "2026-01-01T00:00:00Z",
            "tags": tags,
        });
        if let Some(t) = system_tag {
            raw["system_tag"] = serde_json::json!(t);
        }
        serde_json::from_value(raw).expect("test node deserialises")
    }

    #[test]
    fn compiler_owned_covers_layer_chapters_and_their_descendants() {
        let world = Uuid::now_v7();
        let astronomy = Uuid::now_v7(); // compiled layer chapter
        let calendar = Uuid::now_v7(); // paragraph under Astronomy → owned
        let culture = Uuid::now_v7(); // author chapter
        let ethos = Uuid::now_v7(); // paragraph under Culture → author-owned
        let nodes = vec![
            wnode(world, "book", "World", None, 1, Some("world"), &[]),
            wnode(astronomy, "chapter", "Astronomy", Some(world), 1, None, &[]),
            wnode(calendar, "paragraph", "Calendar", Some(astronomy), 1, None, &[]),
            wnode(culture, "chapter", "Culture", Some(world), 2, None, &[]),
            wnode(ethos, "paragraph", "Ethos", Some(culture), 1, None, &[]),
        ];
        let h = Hierarchy::from_nodes_for_test(nodes);
        assert!(node_is_compiler_owned(&h, astronomy), "the layer chapter itself");
        assert!(node_is_compiler_owned(&h, calendar), "a descendant of a layer");
        assert!(!node_is_compiler_owned(&h, culture), "an author chapter");
        assert!(!node_is_compiler_owned(&h, ethos), "an author paragraph");
        assert!(!node_is_compiler_owned(&h, Uuid::now_v7()), "an unknown id");
    }

    #[test]
    fn fact_world_tag_marks_a_world_fact() {
        let tagged = wnode(Uuid::now_v7(), "paragraph", "Velmari harbours", None, 1, None, &[FACT_WORLD_TAG]);
        let plain = wnode(Uuid::now_v7(), "paragraph", "Bronze ports", None, 2, None, &[]);
        assert!(tagged.tags.iter().any(|t| t == FACT_WORLD_TAG));
        assert!(!plain.tags.iter().any(|t| t == FACT_WORLD_TAG));
    }
}