abstractcore-console 0.8.1

A terminal console for AbstractCore: configure models, providers and capability routes, browse, download and delete local models, and install local engines (Ollama, LM Studio, MLX, llama.cpp). Also a library of the shared Models/Engines screens, rendered by AbstractTUI.
Documentation
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
//! UI root: one PageHost shell over the 8 screens, two modes — browse
//! (free tabs) and wizard (a gated linear walk over the CLI wizard's
//! phases). Screens are plain component functions; durable UI state
//! (selections, wizard step) lives in [`UiState`] created at the root,
//! so screen remounts on tab switches lose nothing (PageHost disposes
//! page scopes on switch by design).

pub mod editors;
pub mod email;
pub mod forms;
pub mod model_field;
pub mod overview;
pub mod providers;
pub mod review;
pub mod routes;
pub mod sections;
pub mod switch;
pub mod util;
pub mod widths;
pub mod wizard;

use std::cell::RefCell;
use std::rc::Rc;
use std::sync::mpsc::Sender;
use std::time::Duration;

use abstracttui::app::{Modal, Overlays, ThemeSwitcher, Toast};
use abstracttui::prelude::*;
use abstracttui::reactive::IntervalHandle;
use abstracttui::widgets::PageHost;

use crate::config::FileState;
use crate::store::{Loadable, Store};
use crate::worker::Cmd;
use util::{fit_width, hints, line, span, span_bold};

/// APPEND ONLY: indices are the wizard's step targets, the footer's
/// hint arms and the tests' `goto_screen` arguments. Screens 9 and 10
/// are the shared library screens (`crate::screens`); the tenth is
/// reached with `0`, the eleventh (Email) with `@`.
pub const SCREENS: [&str; 11] = [
    "Overview",
    "Model",
    "Providers",
    "Routes",
    "Media",
    "Embeddings",
    "Server",
    "Review",
    crate::screens::CATALOG_TITLE,
    crate::screens::ENGINES_TITLE,
    "Email",
];

/// Index of the shared Models screen (`9`).
pub const SCREEN_CATALOG: usize = 8;
/// Index of the shared Engines screen (`0`).
pub const SCREEN_ENGINES: usize = 9;
/// Index of the Email screen (`@`).
pub const SCREEN_EMAIL: usize = 10;

/// The key that jumps to screen `i` (1-9, then 0 for the tenth, @ for
/// the eleventh).
pub fn screen_key(i: usize) -> char {
    match i {
        9 => '0',
        10 => '@',
        _ => char::from_digit(i as u32 + 1, 10).expect("screens 1-9"),
    }
}

/// Stable PageHost page ids, parallel to `SCREENS`. `ui.screen: usize`
/// stays the source of truth; a two-way equality-guarded bridge keeps
/// PageHost's string `active` in lockstep.
pub const SCREEN_IDS: [&str; 11] = [
    "overview",
    "model",
    "providers",
    "routes",
    "media",
    "embeddings",
    "server",
    "review",
    crate::screens::CATALOG_ID,
    crate::screens::ENGINES_ID,
    "email",
];

/// Which screen edits a given config section — the overview's
/// jump-to-owner map and the wizard's per-phase target.
pub fn screen_for_section(name: &str) -> usize {
    match name {
        "default_models" | "app_defaults" => 1,
        "provider_profiles" | "api_keys" => 2,
        "capability_defaults" => 3,
        "vision" | "audio" | "video" => 4,
        "embeddings" => 5,
        "email" => SCREEN_EMAIL,
        _ => 6, // server, logging, streaming, timeouts, offline, maintenance, cache
    }
}

/// Durable per-screen UI state (Copy: all signals).
#[derive(Clone, Copy)]
pub struct UiState {
    /// true = wizard chrome (gated steps); false = browse tabs.
    pub wizard: Signal<bool>,
    pub screen: Signal<usize>,
    /// The wizard's step index into `wizard::STEPS`.
    pub step: Signal<usize>,
    /// The section a wizard step edits — section pages filter to it.
    pub focus_section: Signal<Option<&'static str>>,

    pub overview_sel: Signal<usize>,
    pub route_sel: Signal<usize>,
    /// The Providers screen's ONE selection. It used to be two (a
    /// provider-inventory table plus a profiles table underneath);
    /// the screen has one unified list now, so it has one highlight.
    pub profile_sel: Signal<usize>,
    /// Field-table selections for the section pages.
    pub model_sel: Signal<usize>,
    pub media_sel: Signal<usize>,
    pub embeddings_sel: Signal<usize>,
    pub server_sel: Signal<usize>,

    /// (form_id, outcome) — write completions routed back to the one
    /// open form. Single slot is correct under the one-modal invariant.
    pub write_done: Signal<Option<(u64, Result<String, String>)>>,
    /// Bumped whenever the shared modal slot closes.
    pub modal_epoch: Signal<u64>,
    /// The Email screen's drafts and inline outcomes (root-owned: they
    /// survive screen switches, and a late timer never writes a
    /// disposed signal).
    pub email: email::EmailUi,
}

impl UiState {
    pub fn create(cx: Scope) -> UiState {
        UiState {
            wizard: cx.signal(false),
            screen: cx.signal(0),
            step: cx.signal(0),
            focus_section: cx.signal(None),
            overview_sel: cx.signal(0),
            route_sel: cx.signal(0),
            profile_sel: cx.signal(0),
            model_sel: cx.signal(0),
            media_sel: cx.signal(0),
            embeddings_sel: cx.signal(0),
            server_sel: cx.signal(0),
            write_done: cx.signal(None),
            modal_epoch: cx.signal(0),
            email: email::EmailUi::create(cx),
        }
    }
}

/// Cloneable UI context: the command channel, overlay store, quit and
/// the single-modal slot (stacked modals are an engine hazard — one at
/// a time, sequenced).
#[derive(Clone)]
pub struct Ctx {
    pub tx: Sender<Cmd>,
    pub overlays: Overlays,
    pub quitter: abstracttui::app::Quitter,
    pub store: Store,
    pub ui: UiState,
    pub modal: Rc<RefCell<Option<Modal>>>,
    /// The shared Models / Engines screens (library, `crate::screens`),
    /// over the `abstractcore` CLI transport in the binary.
    pub screens: crate::screens::ScreensCtx,
}

impl Ctx {
    pub fn send(&self, cmd: Cmd) {
        // A dropped worker only happens at quit; ignore then.
        let _ = self.tx.send(cmd);
    }

    pub fn close_modal(&self) {
        if let Some(m) = self.modal.borrow_mut().take() {
            m.close();
        }
        self.ui.modal_epoch.update(|e| *e += 1);
    }

    /// Send a test probe — single-flight: tests run real generations
    /// and live discoveries; a queued duplicate would silently double
    /// the cost behind one busy label. The flag is set HERE,
    /// synchronously (the UI thread owns signals): waiting for the
    /// worker's begin-post left every probe queued behind a busy
    /// worker invisible to the guard (M3 review P2-2). The worker
    /// still clears it on completion and on panic.
    pub fn send_probe(&self, spec: crate::probes::ProbeSpec) {
        if self.store.probe_busy.get_untracked() {
            self.store.notice.set(Some(
                "a test is already running — its result lands in the journal and on Review (8)"
                    .into(),
            ));
            return;
        }
        self.store.probe_busy.set(true);
        self.store.notice.set(Some(format!("⟳ {} …", spec.label)));
        self.send(Cmd::Probe(spec));
    }

    /// The file IDENTITY (mtime, ino, size) of the snapshot the
    /// operator is looking at — every WriteSpec carries it so the
    /// worker can refuse when another writer landed in between (no
    /// lock exists; mtime alone misses same-second rewrites).
    pub fn write_base(&self) -> Option<crate::config::FileStamp> {
        self.store.cfg.with_untracked(|c| {
            c.ready().and_then(|m| match &m.state {
                FileState::Ready(snap) => snap.stamp,
                _ => None,
            })
        })
    }

    /// Can a CLI-routed write run right now? Posts the reason and
    /// returns false when not — the corrupt/refused states are
    /// structural write-refusals at the UI door too (the worker
    /// re-checks on its own fresh read).
    pub fn writable_now(&self) -> bool {
        self.writable(true)
    }

    /// The RMW door: direct writes preserve every byte they don't
    /// touch, so a Python-REFUSED file stays editable through them
    /// (fixing the offending rows may even need it) — with a warning.
    /// Corrupt/unreadable still refuse everything (M2 review P2-3:
    /// the worker allowed this split but the door didn't, making the
    /// CHANGELOG claim dead code).
    pub fn writable_now_rmw(&self) -> bool {
        self.writable(false)
    }

    fn writable(&self, cli_routed: bool) -> bool {
        let refused = self.store.python_fallback.with_untracked(|f| f.is_some())
            || self.store.cfg.with_untracked(|c| {
                matches!(
                    c.ready().map(|m| &m.state),
                    Some(FileState::Ready(snap)) if !snap.python_refusals.is_empty()
                )
            });
        if refused {
            if cli_routed {
                self.store.notice.set(Some(
                    "Python refuses this file — CLI writes would RESET it; fix the named rows \
                     first (direct-write fields stay editable)"
                        .into(),
                ));
                return false;
            }
            self.store.notice.set(Some(
                "careful: Python refuses this file — this direct write preserves it, but fix \
                 the named rows (Overview) before any CLI write"
                    .into(),
            ));
            // fall through: RMW allowed
        }
        let verdict = self.store.cfg.with_untracked(|c| match c {
            Loadable::Ready(m) => match &m.state {
                FileState::Ready(_) | FileState::Missing => Ok(()),
                FileState::Corrupt { .. } => Err(
                    "the config file is corrupt — fix it first (Overview names the backups)"
                        .to_string(),
                ),
                FileState::Unreadable { error } => Err(format!("config unreadable: {error}")),
            },
            _ => Err("config not loaded yet — press r".to_string()),
        });
        match verdict {
            Ok(()) => true,
            Err(e) => {
                self.store.notice.set(Some(e));
                false
            }
        }
    }

    /// Reload everything: the file mirror and both derived views. One
    /// verb — this is a mirror of one file; partial staleness would
    /// lie. `reset_domains` first (the ONE exhaustive-destructure
    /// list), so caches with no Loading state of their own — the
    /// per-provider model lists — cannot serve stale entries across a
    /// reload (M2 review P3-9: the reset fn existed unwired).
    pub fn reload_all(&self) {
        self.store.reset_domains();
        self.store.cfg.set(Loadable::Loading);
        self.store.routes.set(Loadable::Loading);
        self.store.profiles.set(Loadable::Loading);
        self.store.availability.set(Loadable::Loading);
        self.send(Cmd::LoadConfig);
        self.send(Cmd::LoadRoutes);
        self.send(Cmd::LoadProfiles);
        // A reload is exactly when an operator expects a download that
        // just finished to show up in the weights column.
        self.send(Cmd::LoadAvailability);
    }
}

/// The root component.
pub fn root(cx: Scope, ctx: Ctx) -> View {
    let theme = use_theme(cx);
    let ui = ctx.ui;

    install_effects(cx, &ctx);

    let quit = ctx.quitter.clone();
    let ctx_q = ctx.clone();
    let ctx_refresh = ctx.clone();
    let ctx_next = ctx.clone();
    let ctx_back = ctx.clone();
    let ctx_esc = ctx.clone();
    let ctx_left = ctx.clone();
    let ctx_right = ctx.clone();
    let ctx_finish = ctx.clone();
    let ctx_wiz = ctx.clone();

    let mut root_el = Element::new()
        .style(LayoutStyle::column())
        .shortcut(KeyChord::plain(Key::Char('q')), move |_| {
            // q quits from browse; in wizard it is refused WITH A
            // REASON (a swallowed key reads as a dead app). A write in
            // flight also refuses — quitting mid-write abandons a
            // spawned setter that lands AFTER exit, unverified and
            // unjournaled (M2 review P3-4). Ctrl+C stays the force.
            if ui.wizard.get_untracked() {
                ctx_q.store.notice.set(Some(
                    "q quits in browse mode — Ctrl+C quits anywhere, f finishes the wizard".into(),
                ));
                return;
            }
            if ctx_q.store.busy.with_untracked(|b| !b.is_empty()) {
                ctx_q.store.notice.set(Some(
                    "an operation is in flight — wait for it (Ctrl+C force-quits; a running \
                     write would land unverified)"
                        .into(),
                ));
                return;
            }
            // A download/install child dies with this process (its pipes
            // close) — quitting would silently cancel it. (A paused
            // install waits for a person and blocks nothing.)
            if ctx_q.screens.store.job_running() {
                ctx_q.store.notice.set(Some(
                    "a models/engines job is running — c on Models/Engines cancels it \
                     (Ctrl+C force-quits and stops it)"
                        .into(),
                ));
                return;
            }
            quit.quit();
        })
        .shortcut(KeyChord::plain(Key::Char('r')), move |_| {
            // The immediate ack: fast reloads repaint identically —
            // the notice IS the visible trace.
            ctx_refresh
                .store
                .notice
                .set(Some("⟳ reloading config file + derived views…".into()));
            ctx_refresh.reload_all();
        })
        // Ctrl chords survive focused text inputs; ]/[ work outside
        // them. Both advance the wizard in wizard mode and the tabs in
        // browse.
        .shortcut(KeyChord::new(Mods::CTRL, Key::Char('n')), move |_| {
            nav_next(&ctx_next);
        })
        .shortcut(KeyChord::new(Mods::CTRL, Key::Char('p')), move |_| {
            nav_back(&ctx_back);
        })
        // ←/→: previous/next screen, only when the focused element does
        // not use the arrows (see `arrow_tab`).
        .shortcut(KeyChord::plain(Key::Left), move |_| {
            arrow_tab(&ctx_left, -1)
        })
        .shortcut(KeyChord::plain(Key::Right), move |_| {
            arrow_tab(&ctx_right, 1)
        })
        .shortcut(KeyChord::plain(Key::Escape), move |_| {
            if ctx_esc.ui.wizard.get_untracked() {
                wizard::back(&ctx_esc);
            }
        })
        .shortcut(KeyChord::plain(Key::Char('f')), move |_| {
            if ctx_finish.ui.wizard.get_untracked() {
                wizard::finish(&ctx_finish);
            }
        })
        .shortcut(KeyChord::plain(Key::Char('g')), {
            let ctx_gen = ctx.clone();
            move |_| {
                // The definition-of-done verb: one cheap generation
                // over the CONFIGURED default route, from any screen.
                ctx_gen.send_probe(crate::probes::generate_default());
            }
        })
        .shortcut(KeyChord::plain(Key::Char('w')), move |_| {
            if !ctx_wiz.ui.wizard.get_untracked() {
                ctx_wiz.ui.wizard.set(true);
                wizard::apply_step(&ctx_wiz, 0);
                ctx_wiz.store.notice.set(Some(
                    "wizard mode — Ctrl+N next step, Esc back, f finish".into(),
                ));
            }
        });
    // (Ctrl+L rides the GLOBAL action registry — registered in lib.rs
    // so it stays live inside focus-trapped modals.)

    // Digit REFUSALS (wizard only): PageHost owns digit jumps in
    // browse; the wizard's refusal-with-a-reason stays a root shortcut
    // so a swallowed digit never reads as a dead app.
    // PageHost's own digit jumps stop at 9, so the tenth screen's `0`
    // is this root shortcut's job in browse mode.
    for i in 0..SCREENS.len() {
        let ctx_i = ctx.clone();
        let key = screen_key(i);
        root_el = root_el.shortcut(KeyChord::plain(Key::Char(key)), move |_| {
            if ctx_i.ui.wizard.get_untracked() {
                ctx_i.store.notice.set(Some(
                    "digit jumps work in browse mode — walk the wizard with Ctrl+N, finish with f"
                        .into(),
                ));
            } else if i >= 9 {
                ctx_i.ui.screen.set(i);
            }
        });
    }

    // ui.screen (usize, the wizard gate's truth) ⇄ PageHost's string
    // `active`. Both effects are equality-guarded — one hop, no
    // oscillation.
    let active =
        cx.signal(SCREEN_IDS[ui.screen.get_untracked().min(SCREEN_IDS.len() - 1)].to_string());
    cx.effect(move || {
        let id = SCREEN_IDS[ui.screen.get().min(SCREEN_IDS.len() - 1)];
        if active.with_untracked(|a| a != id) {
            active.set(id.to_string());
        }
    });
    cx.effect(move || {
        let pos = active.with(|a| SCREEN_IDS.iter().position(|s| s == a));
        if let Some(i) = pos {
            if ui.screen.get_untracked() != i {
                ui.screen.set(i);
            }
        }
    });

    // ONE PageHost carries the tab bar + page region. Free navigation
    // is ARMED in browse and fully DISARMED in wizard (empty chord
    // sets + number_jump(false)): the gate logic stays app-side. The
    // host rebuilds when the MODE flips; page state survives in
    // UiState.
    let host_ctx = ctx.clone();
    let host = dyn_view_scoped(LayoutStyle::default().grow(1.0), move |hcx| {
        let wizard_now = ui.wizard.get();
        let (prev_chords, next_chords) = if wizard_now {
            (Vec::new(), Vec::new())
        } else {
            (
                vec![
                    KeyChord::new(Mods::CTRL, Key::PageUp),
                    KeyChord::new(Mods::CTRL, Key::Char('p')),
                ],
                vec![
                    KeyChord::new(Mods::CTRL, Key::PageDown),
                    KeyChord::new(Mods::CTRL, Key::Char('n')),
                ],
            )
        };
        let c: Vec<Ctx> = (0..SCREEN_IDS.len()).map(|_| host_ctx.clone()).collect();
        let [c0, c1, c2, c3, c4, c5, c6, c7, c8, c9, c10]: [Ctx; 11] =
            c.try_into().ok().expect("11 screens");
        PageHost::new()
            .page(SCREEN_IDS[0], "1 Overview", move |gcx| {
                overview::view(gcx, &c0, theme)
            })
            .page(SCREEN_IDS[1], "2 Model", move |gcx| {
                sections::page(
                    gcx,
                    &c1,
                    theme,
                    &["default_models", "app_defaults"],
                    c1.ui.model_sel,
                )
            })
            .page(SCREEN_IDS[2], "3 Providers", move |gcx| {
                providers::view(gcx, &c2, theme)
            })
            .page(SCREEN_IDS[3], "4 Routes", move |gcx| {
                routes::view(gcx, &c3, theme)
            })
            .page(SCREEN_IDS[4], "5 Media", move |gcx| {
                sections::page(
                    gcx,
                    &c4,
                    theme,
                    &["vision", "audio", "video"],
                    c4.ui.media_sel,
                )
            })
            .page(SCREEN_IDS[5], "6 Embeddings", move |gcx| {
                sections::embeddings_page(gcx, &c5, theme)
            })
            .page(SCREEN_IDS[6], "7 Server", move |gcx| {
                sections::page(
                    gcx,
                    &c6,
                    theme,
                    &[
                        "server",
                        "logging",
                        "streaming",
                        "timeouts",
                        "offline",
                        "maintenance",
                        "cache",
                    ],
                    c6.ui.server_sel,
                )
            })
            .page(SCREEN_IDS[7], "8 Review", move |gcx| {
                review::view(gcx, &c7, theme)
            })
            .page(SCREEN_IDS[8], "9 Models", move |gcx| {
                crate::screens::catalog(gcx, &c8.screens)
            })
            .page(SCREEN_IDS[9], "0 Engines", move |gcx| {
                crate::screens::engines(gcx, &c9.screens)
            })
            .page(SCREEN_IDS[10], "@ Email", move |gcx| {
                email::view(gcx, &c10, theme)
            })
            .active(active)
            .number_jump(!wizard_now)
            .chords(&prev_chords, &next_chords)
            .view(hcx)
    });

    // The wizard's step/goal line: auto-height — one row in wizard
    // mode, zero in browse (a permanently blank row is dead space at
    // 60x16).
    let goal = dyn_view(LayoutStyle::column().shrink(0.0), move || {
        let t = theme.get().tokens;
        if !ui.wizard.get() {
            return Element::new().style(LayoutStyle::default().h(0)).build();
        }
        let i = ui.step.get().min(wizard::STEPS.len() - 1);
        let step = &wizard::STEPS[i];
        line(vec![
            span_bold(
                format!(
                    " Step {}/{} · {} — ",
                    i + 1,
                    wizard::STEPS.len(),
                    step.title
                ),
                t.accent,
            ),
            span(step.goal, t.text_muted),
        ])
    });

    root_el
        .child(header(cx, &ctx, theme))
        .child(
            Element::new()
                .style(LayoutStyle::default().grow(1.0))
                .child(host)
                .build(),
        )
        .child(goal)
        .child(footer(cx, &ctx, theme))
        .build()
}

/// Left/Right (backlog 0984): the previous/next screen, wrapping at both
/// ends — the Ctrl+P/Ctrl+N cycle on the arrows. These are ROOT
/// shortcuts, so they fire only for an arrow nobody under the focus used:
/// a focused text field moves its caret, the focused screen bar moves
/// itself, and a modal keeps every key (routing law: handlers before
/// shortcuts). The wizard gates its order: there the arrows refuse with
/// the keys that walk it.
fn arrow_tab(ctx: &Ctx, dir: isize) {
    if ctx.ui.wizard.get_untracked() {
        ctx.store.notice.set(Some(
            "←/→ switch screens in browse mode — walk the wizard with Ctrl+N, finish with f".into(),
        ));
        return;
    }
    let n = SCREENS.len() as isize;
    let cur = ctx.ui.screen.get_untracked().min(SCREENS.len() - 1) as isize;
    ctx.ui.screen.set((cur + dir).rem_euclid(n) as usize);
}

fn nav_next(ctx: &Ctx) {
    if ctx.ui.wizard.get_untracked() {
        wizard::next(ctx);
    } else if ctx.ui.screen.get_untracked() + 1 < SCREENS.len() {
        ctx.ui.screen.update(|s| *s += 1);
    } else {
        ctx.store
            .notice
            .set(Some("already on the last screen".into()));
    }
}

fn nav_back(ctx: &Ctx) {
    if ctx.ui.wizard.get_untracked() {
        wizard::back(ctx);
    } else if ctx.ui.screen.get_untracked() > 0 {
        ctx.ui.screen.update(|s| *s -= 1);
    } else {
        ctx.store
            .notice
            .set(Some("already on the first screen".into()));
    }
}

fn install_effects(cx: Scope, ctx: &Ctx) {
    let store = ctx.store;
    let ui = ctx.ui;

    // Screen switches retire the footer notice — a stale toast line
    // must not outlive the context it was about.
    {
        let last_screen = Rc::new(std::cell::Cell::new(usize::MAX));
        cx.effect(move || {
            let s = ui.screen.get();
            if last_screen.get() != usize::MAX && last_screen.get() != s {
                store.notice.set(None);
            }
            last_screen.set(s);
        });
    }

    // Busy ticker: exists only while ops are in flight (zero idle cost).
    {
        let ticker: Rc<RefCell<Option<IntervalHandle>>> = Rc::new(RefCell::new(None));
        cx.effect(move || {
            let any = store.busy.with(|b| !b.is_empty());
            let mut slot = ticker.borrow_mut();
            match (any, slot.is_some()) {
                (true, false) => {
                    *slot = Some(abstracttui::reactive::interval(
                        cx,
                        Duration::from_millis(500),
                        move || store.tick.update(|t| *t += 1),
                    ));
                }
                (false, true) => {
                    if let Some(h) = slot.take() {
                        h.cancel();
                    }
                }
                _ => {}
            }
        });
    }

    // Notices → toast (and the footer mirrors the latest one).
    {
        let overlays = ctx.overlays.clone();
        cx.effect(move || {
            if let Some(n) = store.notice.get() {
                let viewport = abstracttui::app::use_viewport(cx).get_untracked();
                Toast::show(
                    &overlays,
                    cx,
                    viewport,
                    util::ellipsize(&n, (viewport.w as usize).saturating_sub(6).max(20)),
                    Duration::from_secs(4),
                );
            }
        });
    }
}

/// Humanize a raw engine startup notice for the operator footer: the
/// zero-collapse diagnostic reads as a crash log verbatim. The raw
/// line stays behind the debug env flag.
pub fn humanize_engine_notice(raw: &str) -> String {
    if raw.trim_start().starts_with("layout:") {
        if std::env::var("ABSTRACTCORE_CONSOLE_DEBUG").is_ok() {
            return raw.to_string();
        }
        return "display degraded — a panel over-demanded space; rows may overlap \
                (ABSTRACTCORE_CONSOLE_DEBUG=1 for details)"
            .to_string();
    }
    raw.to_string()
}

fn header(cx: Scope, ctx: &Ctx, theme: Signal<&'static abstracttui::theme::Theme>) -> View {
    let store = ctx.store;
    let ui = ctx.ui;
    let viewport = abstracttui::app::use_viewport(cx);
    // shrink(0.0): the title bar is CHROME — without the pin, a page
    // whose content minimum over-demands height flex-shrinks this
    // fixed row to zero and the tab bar paints at row 0 (the sibling's
    // vanishing-title-bar incident; engine finding 0240's class).
    dyn_view(LayoutStyle::line(1).shrink(0.0), move || {
        let t = theme.get().tokens;
        let avail = viewport.get().w;
        // A file the mirror parsed but PYTHON refuses (loader raise →
        // defaults + backup) must not wear the green dot — either
        // detection lane (fold-side refusals, CLI #FALLBACK stderr)
        // flips the header (review P1-1).
        let python_refuses = store.python_fallback.with(|f| f.is_some())
            || store.cfg.with(|c| {
                matches!(
                    c.ready().map(|m| &m.state),
                    Some(FileState::Ready(snap)) if !snap.python_refusals.is_empty()
                )
            });
        let (path, dot, dot_ink, label) = match store.cfg.get() {
            Loadable::Ready(m) => {
                let path = m.path.path.display().to_string();
                match &m.state {
                    FileState::Ready(_) if python_refuses => (
                        path,
                        "●",
                        t.error,
                        "loaded — but Python REFUSES it (Overview)".to_string(),
                    ),
                    FileState::Ready(_) => (path, "●", t.ok, "loaded".to_string()),
                    FileState::Missing => {
                        (path, "○", t.text_muted, "no config file yet".to_string())
                    }
                    FileState::Corrupt { .. } => (path, "●", t.error, "CORRUPT".to_string()),
                    FileState::Unreadable { .. } => (path, "●", t.error, "unreadable".to_string()),
                }
            }
            Loadable::Loading => (String::new(), "◌", t.info, "reading…".to_string()),
            _ => (String::new(), "○", t.text_muted, "not loaded".to_string()),
        };
        let mode = if ui.wizard.get() { "wizard" } else { "browse" };
        // STATE FIRST: the header truncates last-span-first, and at 60
        // cols a long path was evicting the state label — the single
        // most important span (a CORRUPT flag pushed off-screen is a
        // lying header).
        //
        // The path is the ELASTIC span, and its budget is the row it is
        // actually drawn on — a constant 46 cut `…/.abstract…nfig/
        // abstractcore.json` on a 200-cell terminal with a hundred cells
        // still free. The FILENAME is what identifies the file, so the
        // cut keeps the tail.
        let brand = " AbstractCore Console ";
        let state = format!("{dot} {label} ");
        let lead = format!("· {mode} · ");
        let path = widths::middle_fit(
            &path,
            widths::elastic_budget(&[brand, &state, &lead], avail),
        );
        line(vec![
            span_bold(brand.to_string(), t.accent),
            span(format!("{dot} "), dot_ink),
            span(format!("{label} "), t.text),
            span(format!("{lead}{path}"), t.text_muted),
        ])
    })
}

fn footer(cx: Scope, ctx: &Ctx, theme: Signal<&'static abstracttui::theme::Theme>) -> View {
    let store = ctx.store;
    let ui = ctx.ui;
    let screen_caps = ctx.screens.caps;
    let screen_access = ctx.screens.store.access;
    let engine_notices = abstracttui::app::use_startup_notices(cx);
    Element::new()
        // Chrome rows: pinned like the header — the hint line
        // disappearing under content pressure would take the app's
        // teachable surface with it.
        .style(LayoutStyle::column().shrink(0.0))
        .child(dyn_view(LayoutStyle::line(1).shrink(0.0), move || {
            // Busy strip: in-flight ops with elapsed seconds; app
            // notice next; engine startup notices when idle (the lane
            // the engine names layout crushes into — unread means
            // invisible).
            let t = theme.get().tokens;
            let _ = store.tick.get();
            let ops = store.busy.get();
            if ops.is_empty() {
                let notice = store.notice.get();
                return match notice {
                    Some(n) => line(vec![span(format!(" {n}"), t.text_muted)]),
                    None => {
                        // Diagnostic engine notices only — the ambient
                        // caps summary reads as a permanent warning.
                        // Exact "caps:" prefix: a broader match would
                        // also hide a future diagnostic that merely
                        // starts with the word.
                        let last = engine_notices.with(|v| {
                            v.iter()
                                .rev()
                                .find(|n| !n.trim_start().starts_with("caps:"))
                                .cloned()
                        });
                        match last {
                            Some(en) => line(vec![span(
                                format!(" engine: {}", humanize_engine_notice(&en)),
                                t.warn,
                            )]),
                            None => line(vec![span(String::new(), t.text_muted)]),
                        }
                    }
                };
            }
            let mut parts = Vec::new();
            for (i, op) in ops.iter().enumerate() {
                if i > 0 {
                    parts.push(span(" · ", t.text_faint));
                }
                let secs = op.started.elapsed().as_secs();
                parts.push(span(format!(" ⟳ {}… {}s", op.label, secs), t.info));
            }
            line(parts)
        }))
        .child(
            Element::new()
                .style(LayoutStyle::row().shrink(0.0))
                .child(
                    Element::new()
                        .style(LayoutStyle::default().grow(1.0).h(1))
                        .child(dyn_view(LayoutStyle::line(1), move || {
                            let t = theme.get().tokens;
                            let screen = ui.screen.get();
                            let wizard = ui.wizard.get();
                            let mut pairs: Vec<(&str, &str)> = Vec::new();
                            // Universal pairs FIRST — the hint row
                            // truncates right-edge-first and the quit
                            // affordance must survive at 60 cols.
                            if wizard {
                                pairs.push(("Ctrl+N", "next step"));
                                pairs.push(("Esc", "back"));
                                pairs.push(("f", "finish"));
                                pairs.push(("Ctrl+C", "quit"));
                            } else {
                                pairs.push(("1-9,0,@ ←/→", "screens"));
                                pairs.push(("q", "quit"));
                                // Models owns `w` (download) — the root's
                                // wizard key never reaches it there.
                                if screen != SCREEN_CATALOG {
                                    pairs.push(("w", "wizard"));
                                }
                            }
                            pairs.push(("r", "reload"));
                            match screen {
                                0 => pairs.push(("Enter", "open section")),
                                1 | 4 | 5 | 6 => {
                                    pairs.push(("Enter/e", "edit field"));
                                    pairs.push(("space", "switch"));
                                    pairs.push(("x", "clear field"));
                                }
                                2 => {
                                    // ONE list, the gateway console's
                                    // verbs, in its order.
                                    pairs.push(("a", "add connection"));
                                    pairs.push(("e", "edit"));
                                    pairs.push(("d", "delete"));
                                    pairs.push(("m", "models"));
                                    pairs.push(("t", "test"));
                                    pairs.push(("k", "set key"));
                                }
                                3 => {
                                    pairs.push(("Enter/e", "edit route"));
                                    pairs.push(("x", "clear route"));
                                    pairs.push(("t", "test route"));
                                    // `d` is "delete" on Providers and
                                    // "download" here. The two never share
                                    // a screen, the hint row names the verb
                                    // for the screen you are on, and the
                                    // download confirms with the artifact
                                    // spelled out before it spends a byte.
                                    pairs.push(("w", "download weights"));
                                }
                                SCREEN_CATALOG => pairs.extend(crate::screens::catalog::hints(
                                    screen_caps,
                                    &screen_access.get(),
                                )),
                                SCREEN_ENGINES => pairs.extend(crate::screens::engines::hints(
                                    screen_caps,
                                    &screen_access.get(),
                                )),
                                SCREEN_EMAIL => pairs.extend(email::HINTS.iter().copied()),
                                _ => {}
                            }
                            pairs.push(("g", "test default route"));
                            hints(&t, &pairs)
                        }))
                        .build(),
                )
                // The theme control: one cell of chrome, engine-owned
                // popup (opens upward from the footer automatically).
                .child(
                    Element::new()
                        .style(LayoutStyle::default().w(3).h(1).shrink(0.0))
                        .child(ThemeSwitcher::new().view(cx))
                        .build(),
                )
                .build(),
        )
        .build()
}

/// Path display shared by overview/review: name + where it came from.
pub fn config_identity_line(t: &TokenSet, store: &Store) -> Vec<util::SpanSpec> {
    match store.cfg.get() {
        Loadable::Ready(m) => {
            let mut spans = vec![
                span(m.path.path.display().to_string(), t.text),
                span(format!("  ({})", m.path.source.label()), t.text_faint),
            ];
            if let FileState::Ready(snap) = &m.state {
                spans.push(span(
                    format!("  · {}", util::human_bytes(snap.bytes)),
                    t.text_muted,
                ));
                if let Some(mode) = snap.mode {
                    let ink = if mode & 0o077 != 0 {
                        t.warn
                    } else {
                        t.text_muted
                    };
                    spans.push(span(format!("  · mode {mode:03o}"), ink));
                    if mode & 0o077 != 0 {
                        spans.push(span("  (secrets readable by others!)", t.warn));
                    }
                }
            }
            spans.push(span(format!("  · read {}", m.loaded_at), t.text_faint));
            spans
        }
        Loadable::Loading => vec![span("⟳ reading…", t.info)],
        _ => vec![span("— not loaded", t.text_muted)],
    }
}

/// One-line truncation helper for table cells built from spans.
pub fn cell(s: &str, max: usize) -> String {
    fit_width(s, max)
}