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
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
//! Capability routes: the 24-route derived view from
//! `abstractcore config defaults --json` — coverage decorations,
//! the output.text alias, per-route provider/model/options — with the
//! route editor (e) and clear (x) writing through `config
//! set-default`/`clear-default`, verified against the fresh view.

use abstracttui::prelude::*;
use abstracttui::widgets::{Block, Button, Table};

use crate::schema;
use crate::store::{Loadable, RouteRow, RoutesData};
use crate::worker::{next_form_id, Cmd};
use crate::writes;

use super::forms::{confirm_danger, install_write_done, message_slot, open_form_guarded};
use super::util::{field, line, loadable_view, or_dash, span, span_bold};
use super::widths;
use super::Ctx;

pub fn view(cx: Scope, ctx: &Ctx, theme: Signal<&'static abstracttui::theme::Theme>) -> View {
    let store = ctx.store;
    let ui = ctx.ui;
    let ctx_edit = ctx.clone();
    let ctx_clear = ctx.clone();

    // The banner reads exactly like the gateway console's: the write
    // door first (writable / read-only), the grid's coverage second.
    // The alias teaching moved INTO the row that carries it ("derived
    // ← input.text") — a permanent banner sentence about one row was
    // noise on the other 23 and made every substring assertion lie.
    let banner = dyn_view(LayoutStyle::line(1).shrink(0.0), move || {
        let t = theme.get().tokens;
        match store.routes.get() {
            Loadable::Ready(d) => {
                let mut spans = if d.writable {
                    vec![span(" writable", t.ok)]
                } else {
                    vec![span_bold(" read-only (config file refused?)", t.warn)]
                };
                spans.push(span(
                    format!(
                        "  ·  {} of {} configured",
                        d.configured_count(),
                        d.rows.len()
                    ),
                    t.text,
                ));
                // The other half of the weights banner below: that line
                // says whether the recommended MODELS are here, this verb
                // makes the ROUTES name them. It rides the always-present
                // banner because the weights line can fill with a missing
                // list and truncate anything appended to it.
                spans.push(span(
                    "  ·  a applies the recommended routes".to_string(),
                    t.text_faint,
                ));
                if !d.errors.is_empty() {
                    spans.push(span_bold(
                        format!("  ·  errors: {}", d.errors.join(" | ")),
                        t.error,
                    ));
                }
                line(spans)
            }
            _ => line(vec![span(String::new(), t.text)]),
        }
    });

    // WEIGHTS BANNER. A route with NOTHING serving it cannot run, and the
    // recommended model for it is the one-click way out — invisible in
    // the grid above, which is the fresh-install confusion this line
    // exists for. Absent entirely while the probe has not answered — a
    // blank is honest, a zero would not be.
    //
    // IT USED TO READ "recommended models: 2 of 3 present · missing:
    // lmstudio qwen/qwen3.5-9b@4bit" ON A FULLY CONFIGURED MACHINE, in
    // warn amber, forever: the operator had routed text generation at
    // their own model, so the starter kit's build was absent and would
    // stay absent. A warning whose only cure is installing the model you
    // chose against is not a warning, it is noise — and noise on the
    // healthy path is how an operator learns to skip the line that
    // matters. `mark_recommended_route_gaps` decides which recommended
    // models belong to an UNANSWERED route; a machine with none gets no
    // banner at all.
    //
    // THE MISSING LIST IS THE ELASTIC PART. It can name a dozen
    // artifacts; the verb after it is the only actionable text on the
    // row, and `line()` clips last-span-first — which is how the
    // operator saw `w downloads the selecte…`. So the verb (and the
    // count, and the `missing:` label) are reserved, the list gets what
    // is left, and it keeps its TAIL: the `@4bit`-style tag is what
    // distinguishes one absent artifact from another.
    let viewport = abstracttui::app::use_viewport(cx);
    let weights_banner = dyn_view(LayoutStyle::line(1).shrink(0.0), move || {
        let t = theme.get().tokens;
        let avail = viewport.get().w;
        match store.availability.get() {
            Loadable::Ready(a) if !a.missing.is_empty() => {
                let head = format!(
                    " {} route{} with no model yet",
                    a.missing.len(),
                    if a.missing.len() == 1 { "" } else { "s" }
                );
                let routes = a
                    .missing
                    .iter()
                    .map(|(route, _, _)| route.as_str())
                    .filter(|route| !route.is_empty())
                    .collect::<Vec<_>>()
                    .join(", ");
                let routes = if routes.is_empty() {
                    String::new()
                } else {
                    format!("  ·  {routes}")
                };
                let mut spans = vec![
                    span_bold(head.clone(), t.warn),
                    span(routes.clone(), t.text),
                ];
                const LABEL: &str = "  ·  recommended: ";
                const VERB: &str = "  ·  w downloads the selected route's weights";
                let list = a
                    .missing
                    .iter()
                    .map(|(_, p, art)| format!("{p} {art}"))
                    .collect::<Vec<_>>()
                    .join(", ");
                let budget = widths::elastic_budget(&[&head, &routes, LABEL, VERB], avail);
                // Two honest lines, widest first: name the artifacts, or
                // say nothing about them — but never at the verb's
                // expense, and never by clipping a word. The COUNT is
                // already in the head, so there is no third form.
                if budget >= 8 {
                    spans.push(span(
                        format!("{LABEL}{}", widths::middle_fit(&list, budget)),
                        t.warn,
                    ));
                }
                spans.push(span(VERB.to_string(), t.text_faint));
                line(spans)
            }
            Loadable::Failed(e) => line(vec![span(
                format!(" model availability unavailable: {e}"),
                t.text_muted,
            )]),
            _ => line(vec![span(String::new(), t.text)]),
        }
    });

    let ctx_table = ctx.clone();
    let table = dyn_view_scoped(LayoutStyle::default().grow(1.0), move |gcx| {
        let t = theme.get().tokens;
        let data = store.routes.get();
        loadable_view(
            &t,
            &data,
            |d: &RoutesData| d.rows.is_empty(),
            "the CLI reported no routes",
            |d| routes_table(gcx, cx, &ctx_table, &t, d, ui.route_sel),
        )
    });

    // Selected-row extras: options / base_url / package hint — the
    // fields that don't earn a column but answer "why is this row odd".
    let detail = dyn_view(LayoutStyle::line(1).shrink(0.0), move || {
        let t = theme.get().tokens;
        let row: Option<RouteRow> = store.routes.with(|d| {
            d.ready()
                .and_then(|d| d.rows.get(ui.route_sel.get()).cloned())
        });
        match row {
            Some(r) => {
                // The FULL key leads, always: the route column now shows a
                // task row as `└ text_to_image` under its parent, and this
                // is where `output.image.text_to_image` stays readable and
                // copyable for `config set-default`.
                let mut spans = vec![span_bold(format!(" {} ", r.key), t.accent)];
                // WHAT THE PARENT ROW IS FOR, in words. The grid alone made
                // an operator ask whether `output.image` was dead code next
                // to t2i/i2i/upscale; it is the opposite — the ONE value
                // that serves every image task with no row of its own, and
                // the simple setting for someone who wants one image model.
                if r.is_task_parent() {
                    spans.push(span(
                        format!("serves any {} task with no row of its own  ", r.modality),
                        t.text_muted,
                    ));
                    if r.covered_by_tasks {
                        spans.push(span(
                            format!(
                                "· all {} task rows below are set, so nothing reads it  ",
                                r.task_keys.len()
                            ),
                            t.text_faint,
                        ));
                    }
                } else if let Some(parent) = &r.broad_key {
                    spans.push(span(
                        if r.inherits_broad {
                            format!("no value of its own — {parent} answers it  ")
                        } else {
                            format!("overrides {parent}  ")
                        },
                        t.text_muted,
                    ));
                }
                // A route this computer cannot run, in Core's words: the
                // configured one (`route_unavailable`), or the reason an
                // unset row has no recommendation (`recommendation_unavailable`).
                if let Some(u) = r.route_unavailable.as_ref().filter(|_| r.configured) {
                    spans.push(span(
                        format!(
                            "configured but cannot run on this computer: {} — Enter edits it, \
                             x clears it  ",
                            u.reason
                        ),
                        t.error,
                    ));
                } else if let Some(m) = r
                    .engine_missing
                    .as_ref()
                    .filter(|m| r.configured && m.is_unknown_engine())
                {
                    // Not an engine at all (nothing to install): Core's
                    // sentence says what the id is not, which engines
                    // exist, and what to do — never "not installed".
                    spans.push(span(format!("{} — Enter edits it  ", m.reason), t.error));
                } else if let Some(m) = r.engine_missing.as_ref().filter(|_| r.configured) {
                    // Runnable here, not installed: Core's sentence carries
                    // the exact install command; `i` on Engines runs it when
                    // the engine has a row there.
                    let via = match &m.engine_row {
                        Some(row) => format!(" (Engines: {row}, i installs it)"),
                        None => String::new(),
                    };
                    spans.push(span(
                        format!("engine not installed: {}{via}  ", m.reason),
                        t.warn,
                    ));
                } else if let Some(u) = r
                    .recommendation_unavailable
                    .as_ref()
                    .filter(|_| !r.configured)
                {
                    let what = u.pair_text();
                    spans.push(span(
                        if what.is_empty() {
                            format!("nothing recommended runs on this computer: {}  ", u.reason)
                        } else {
                            format!(
                                "the recommended {what} cannot run on this computer: {}  ",
                                u.reason
                            )
                        },
                        t.warn,
                    ));
                }
                if let Some(u) = &r.base_url {
                    spans.push(span(format!("base_url {u}  "), t.text_muted));
                }
                if let Some(o) = &r.options {
                    spans.push(span(format!("options {}  ", mask_options(o)), t.text_muted));
                }
                if !r.configured {
                    if let Some(h) = &r.package_hint {
                        spans.push(span(format!("needs: {h}"), t.text_faint));
                    }
                }
                // The weights answer for THIS row, with the evidence or
                // the actionable line the probe produced — the detail a
                // 14-cell column cannot carry.
                if let Some(wr) = store
                    .availability
                    .with(|d| d.ready().and_then(|a| a.by_route.get(&r.key).cloned()))
                {
                    let tone = match wr.status.as_str() {
                        "installed" => t.ok,
                        "absent" => t.warn,
                        _ => t.text_muted,
                    };
                    spans.push(span(format!("  weights {} ", wr.label()), tone));
                    // The engine runs weights another provider fetches
                    // (transcription: faster-whisper runs `base`, the
                    // Hugging Face repo Systran/faster-whisper-base).
                    if let Some(engine) = &wr.route_provider {
                        spans.push(span(
                            format!(
                                "· run by {engine}, fetched from {} {} ",
                                wr.provider, wr.artifact
                            ),
                            t.text_faint,
                        ));
                    }
                    if wr.status == "absent" && !wr.artifact.is_empty() {
                        spans.push(span(
                            format!("· w downloads {} ", wr.artifact),
                            t.text_faint,
                        ));
                    } else if !wr.detail.is_empty() {
                        spans.push(span(format!("· {} ", wr.detail), t.text_faint));
                    }
                }
                line(spans)
            }
            None => line(vec![span(String::new(), t.text)]),
        }
    });

    Element::new()
        .style(LayoutStyle::column().grow(1.0))
        .shortcut(KeyChord::plain(Key::Char('e')), move |_| {
            edit_selected(cx, &ctx_edit);
        })
        .shortcut(KeyChord::plain(Key::Char('x')), move |_| {
            clear_selected(cx, &ctx_clear);
        })
        .shortcut(KeyChord::plain(Key::Char('t')), {
            let ctx_test = ctx.clone();
            move |_| test_selected(&ctx_test)
        })
        // `w` for WEIGHTS, and deliberately not `d`. `d` deletes on the
        // Providers screen, and a key that means "delete" one screen over must
        // not mean "download" here: downloads are safe and frequent, so `d`
        // would train a fast confirm-and-move-on reflex that an operator then
        // carries onto a destructive prompt. `w` also names the column the
        // operator is looking at.
        .shortcut(KeyChord::plain(Key::Char('w')), {
            let ctx_dl = ctx.clone();
            move |_| download_selected(cx, &ctx_dl)
        })
        // `a` for APPLY, the other half of the weights banner. Safe by
        // default: without --force the CLI keeps every route the operator
        // configured differently, so this key can never quietly overrule a
        // deliberate choice — the prompt offers that as a separate answer.
        .shortcut(KeyChord::plain(Key::Char('a')), {
            let ctx_apply = ctx.clone();
            move |_| apply_recommended(cx, &ctx_apply)
        })
        .child(banner)
        .child(weights_banner)
        .child(table)
        .child(detail)
        .build()
}

/// `w` — download the selected route's model weights.
///
/// NEVER AUTOMATIC, ALWAYS CONFIRMED. This is the one verb in the
/// console that spends gigabytes, so it names the artifact and the
/// provider tool before it runs. It refuses (with the reason) rather
/// than guessing when: nothing is selected, the route names no model,
/// the weights are already here, the provider has no download verb, or
/// availability says `unknown` — a guess there costs the operator's
/// disk, and the honest answer is the instruction the probe already
/// produced.
fn download_selected(cx: Scope, ctx: &Ctx) {
    let Some(row) = selected_route(ctx) else {
        ctx.store
            .notice
            .set(Some("no route selected — nothing to download".into()));
        return;
    };
    let weights = ctx
        .store
        .availability
        .with_untracked(|d| d.ready().and_then(|a| a.by_route.get(&row.key).cloned()));
    let Some(weights) = weights else {
        ctx.store.notice.set(Some(format!(
            "{}: no weight information yet (r reloads) — nothing to download",
            row.key
        )));
        return;
    };
    match weights.status.as_str() {
        "installed" => {
            ctx.store.notice.set(Some(format!(
                "{} is already installed{}",
                weights.artifact,
                if weights.detail.is_empty() {
                    String::new()
                } else {
                    format!(" ({})", weights.detail)
                }
            )));
            return;
        }
        "not_applicable" => {
            ctx.store.notice.set(Some(format!(
                "{} serves models remotely — there is nothing to download",
                weights.provider
            )));
            return;
        }
        "unknown" => {
            let hint = if weights.instruction.is_empty() {
                "the provider's tool could not be consulted".to_string()
            } else {
                weights.instruction.clone()
            };
            ctx.store.notice.set(Some(format!(
                "{}: availability is unknown — {hint}",
                row.key
            )));
            return;
        }
        _ => {}
    }
    if !weights.downloadable {
        ctx.store
            .notice
            .set(Some(if weights.instruction.is_empty() {
                format!("{} has no download tool on this machine", weights.provider)
            } else {
                weights.instruction.clone()
            }));
        return;
    }

    let ctx2 = ctx.clone();
    let provider = weights.provider.clone();
    let artifact = weights.artifact.clone();
    super::forms::confirm_danger(
        cx,
        ctx.ui,
        format!(
            "Download {artifact} with {provider}? This runs the provider's own tool and \
             may fetch several gigabytes."
        ),
        "Download",
        "Not now",
        move || {
            ctx2.send(Cmd::DownloadModel {
                provider: provider.clone(),
                artifact: artifact.clone(),
            });
            // The worker lane is SERIAL, so this re-probe runs after the
            // download returns — the weights column tells the truth the
            // moment the operator looks back at it, with no timer and no
            // second definition of "finished".
            ctx2.send(Cmd::LoadAvailability);
        },
    );
}

/// Test the selected route: the configured model must be among what
/// the provider ACTUALLY serves (live discovery) — capability-agnostic
/// (a voice route can't be chat-tested, but its model's existence can
/// always be checked). The effective pair covers alias/covered rows.
fn test_selected(ctx: &Ctx) {
    let Some(row) = selected_route(ctx) else {
        ctx.store
            .notice
            .set(Some("no route selected — nothing to test".into()));
        return;
    };
    let (Some(provider), Some(model)) = (row.provider.clone(), row.model.clone()) else {
        ctx.store.notice.set(Some(format!(
            "{} resolves to no provider/model — nothing to test (e edits a route)",
            row.key
        )));
        return;
    };
    // `endpoint:<id>` routes usually carry no route-level base_url —
    // the PROFILE holds it, and the console knows the profile. Without
    // this lookup the same target got weaker evidence from this screen
    // than from the Providers picker ("no known endpoint to
    // reach-check" while the endpoint sat in store.profiles — M3
    // review P2-4).
    let base_url = row.base_url.clone().or_else(|| {
        provider.strip_prefix("endpoint:").and_then(|id| {
            ctx.store.profiles.with_untracked(|d| {
                d.ready()
                    .and_then(|d| d.profiles.iter().find(|p| p.id == id))
                    .map(|p| p.base_url.clone())
            })
        })
    });
    let spec = crate::probes::route_check(&row.key, &provider, &model, base_url.as_deref());
    ctx.send_probe(spec);
}

fn selected_route(ctx: &Ctx) -> Option<RouteRow> {
    let idx = ctx.ui.route_sel.get_untracked();
    ctx.store
        .routes
        .with_untracked(|d| d.ready().and_then(|d| d.rows.get(idx).cloned()))
}

/// The ONE refusal vocabulary, word for word the gateway console's —
/// derived from the row's own payload fields, never a hardcoded key.
fn not_editable_reason(row: &RouteRow) -> String {
    if let Some(d) = &row.derived_from {
        format!("{} derives from {} — edit that route instead", row.key, d)
    } else if row.covered_by.is_some() {
        format!("{} is covered and not overrideable", row.key)
    } else {
        format!("{} is read-only", row.key)
    }
}

fn edit_selected(cx: Scope, ctx: &Ctx) {
    let Some(row) = selected_route(ctx) else {
        ctx.store
            .notice
            .set(Some("no route selected — nothing to edit".into()));
        return;
    };
    if !row.editable() {
        ctx.store.notice.set(Some(not_editable_reason(&row)));
        return;
    }
    if !ctx.writable_now() {
        return;
    }
    open_route_editor(cx, ctx, row);
}

fn clear_selected(cx: Scope, ctx: &Ctx) {
    let Some(row) = selected_route(ctx) else {
        ctx.store
            .notice
            .set(Some("no route selected — nothing to clear".into()));
        return;
    };
    // Clearing only makes sense for an explicitly configured row — a
    // derived or covered one has no override of its own to drop (the
    // gateway console's exact gate).
    if !row.editable() {
        ctx.store.notice.set(Some(not_editable_reason(&row)));
        return;
    }
    if !row.configured || row.covered_by.is_some() {
        ctx.store.notice.set(Some(format!(
            "{} has no explicit override to clear",
            row.key
        )));
        return;
    }
    if !ctx.writable_now() {
        return;
    }
    confirm_clear(cx, ctx, row);
}

/// `a` — make this machine's routes match the framework recommendation.
///
/// Three answers, because the honest action has three: fill only the
/// empty routes (safe, the default), replace the operator's choices too
/// (the `--force` spelling, offered as its own option so it can never be
/// the accidental one), or nothing. The CLI's report — every route:
/// applied, kept, or not runnable on this computer with the reason —
/// rides the journal and the notice (`WriteVerb::ApplyRecommended`).
fn apply_recommended(cx: Scope, ctx: &Ctx) {
    if !ctx.writable_now() {
        return;
    }
    let ctx_keep = ctx.clone();
    let ctx_force = ctx.clone();
    let prompt = abstracttui::app::ChoicePrompt::new(
        // No route list in this sentence: Core owns which routes it
        // recommends (video joined text, voice and image) — the report
        // the write journals names every one, with what happened to it.
        "Apply the framework's recommended routes for this computer? The report names each one."
            .to_string(),
    )
    .option("keep", "Apply — keep routes I configured")
    .option_with(
        abstracttui::app::ChoiceOption::new("force", "Apply — replace mine too").danger(true),
    )
    .option("cancel", "Cancel")
    .initial("keep");
    super::forms::open_prompt(cx, ctx.ui, prompt, move |outcome| {
        if let abstracttui::app::ChoiceOutcome::Answered(a) = outcome {
            let choice = a.selected.first().cloned().unwrap_or_default();
            let (ctx2, force) = match choice.as_str() {
                "keep" => (ctx_keep, false),
                "force" => (ctx_force, true),
                _ => return,
            };
            let spec = writes::apply_recommended(force, ctx2.write_base(), None);
            ctx2.send(Cmd::Write(Box::new(spec)));
        }
    });
}

/// ONE confirm policy for clearing a route — shared by the table's `x`
/// and the editor's Clear button (the editor closes itself first, then
/// prompts on the screen scope).
fn confirm_clear(cx: Scope, ctx: &Ctx, row: RouteRow) {
    let ctx2 = ctx.clone();
    let key = row.key.clone();
    confirm_danger(
        cx,
        ctx.ui,
        format!(
            "Clear the override on {} ({})? The engine default takes over.",
            row.key,
            row.pair_text()
        ),
        "Clear the route",
        "Keep the override",
        move || {
            let spec = writes::clear_route(&key, ctx2.write_base(), None);
            ctx2.send(Cmd::Write(Box::new(spec)));
        },
    );
}

/// "Applies now" — derived ONLY from the stored row, never from local
/// picks. The gateway console's line, verbatim, plus the reasoning the
/// text route carries.
fn applies_now(t: &TokenSet, row: &RouteRow) -> View {
    let mut spans = vec![span_bold("Applies now: ".to_string(), t.text)];
    if row.configured {
        spans.push(span(row.pair_text(), t.ok));
        if let Some(r) = &row.reasoning {
            spans.push(span(format!("  · reasoning {r}"), t.ok));
        }
        if let Some(by) = &row.covered_by {
            spans.push(span(format!("  (covered by {by})"), t.info));
        } else {
            spans.push(span(format!("  (source: {})", row.source), t.text_muted));
        }
    } else {
        spans.push(span(
            "nothing configured — engine decides".to_string(),
            t.text_muted,
        ));
        if let Some(hint) = &row.package_hint {
            spans.push(span(format!("  (needs {hint})"), t.text_faint));
        }
    }
    line(spans)
}

/// The route editor: "Applies now" (stored truth), provider (Select
/// with a placeholder at index 0), model (text + `m` picker over live
/// discovery), base URL, reasoning (text routes only), options as
/// `k=v` pairs — the gateway console's field set and wording, over
/// core's CLI write path.
///
/// THE SAVE SENDS WHAT THE OPERATOR EDITED, AND NOTHING ELSE. Every
/// control is diffed against the value it opened with; an untouched
/// field is not named at all, so the store keeps it. Echoing back what
/// the grid last rendered would let a stale row overwrite a setting
/// made from the gateway console between render and save.
fn open_route_editor(cx: Scope, ctx: &Ctx, row: RouteRow) {
    let theme = use_theme(cx);
    let ctx2 = ctx.clone();
    // The SCREEN scope: the editor's Clear button prompts on it after
    // closing the editor (the modal scope dies with the editor).
    let screen_cx = cx;
    // Provider choices: the 10 static + enabled endpoint profiles.
    let mut providers: Vec<String> = schema::STATIC_PROVIDERS
        .iter()
        .map(|s| s.to_string())
        .collect();
    if let Loadable::Ready(p) = ctx.store.profiles.get_untracked() {
        for prof in &p.profiles {
            if prof.enabled {
                providers.push(prof.virtual_provider());
            }
        }
    }
    // The drift base is captured at editor OPEN — a base read at
    // submit time would be silently re-armed by any mirror refresh
    // landing while the form is up (M2 review P3-2).
    let base = ctx.write_base();
    let providers = std::rc::Rc::new(providers);
    // A prefilled provider kicks discovery at OPEN: the model picker
    // must be populated when the operator reaches it, not only after
    // re-committing the provider they already had.
    if let Some(p) = row.provider.as_deref() {
        if providers.iter().any(|q| q == p) {
            super::model_field::kick_discovery(ctx, p);
        }
    }
    open_form_guarded(ctx, cx, Size::new(84, 24), move |mcx, close, guard| {
        let t = theme.get().tokens;
        let mut popts = vec![SelectOption::new("— engine decides —")];
        let mut pinitial = 0usize;
        for (i, p) in providers.iter().enumerate() {
            popts.push(SelectOption::new(p.clone()));
            if Some(p.as_str()) == row.provider.as_deref() {
                pinitial = i + 1;
            }
        }
        let provider_sel = mcx.signal(pinitial);
        let model = mcx.signal(row.model.clone().unwrap_or_default());
        let base_url = mcx.signal(row.base_url.clone().unwrap_or_default());
        // Reasoning is a fixed vocabulary, not free text: the same
        // five choices the web console offers, index 0 = "not set".
        let is_text = row.is_text_generation();
        let reasoning_init_ix = crate::store::reasoning_index(row.reasoning.as_deref());
        let reasoning_sel = mcx.signal(reasoning_init_ix);
        let mtp_init_ix = mtp_policy_index(row.options.as_ref());
        let mtp_sel = mcx.signal(mtp_init_ix);
        let options = mcx.signal(
            row.options
                .as_ref()
                .and_then(|o| o.as_object())
                .map(render_options)
                .unwrap_or_default(),
        );
        let form_error: Signal<Option<String>> = mcx.signal(None);
        let in_flight = mcx.signal(false);
        let esc_armed = mcx.signal(false);
        // The dirty set must track EVERY editable control — a form
        // dirty only in the select used to discard on the first Esc
        // (M2 review P3-10).
        // These are also the DIFF BASE the save reads: what the form
        // opened with, so an untouched field is never sent.
        let p0 = pinitial;
        let model_init = model.get_untracked();
        let url_init = base_url.get_untracked();
        let options_init = options.get_untracked();
        {
            let model_init = model_init.clone();
            let url_init = url_init.clone();
            let options_init = options_init.clone();
            super::forms::install_dirty_guard_with(
                mcx,
                &guard,
                move || {
                    provider_sel.get_untracked() != p0
                        || model.with_untracked(|v| v != &model_init)
                        || base_url.with_untracked(|v| v != &url_init)
                        || reasoning_sel.get_untracked() != reasoning_init_ix
                        || mtp_sel.get_untracked() != mtp_init_ix
                        || options.with_untracked(|v| v != &options_init)
                },
                move || {
                    let _ = provider_sel.get();
                    let _ = model.get();
                    let _ = base_url.get();
                    let _ = reasoning_sel.get();
                    let _ = mtp_sel.get();
                    let _ = options.get();
                },
                esc_armed,
                form_error,
            );
        }
        let form_id = next_form_id();
        install_write_done(mcx, &ctx2, form_id, in_flight, form_error, close.clone());

        // Provider commit kicks model discovery (same lane the pair
        // editor uses).
        let providers2 = providers.clone();
        let ctx_models = ctx2.clone();
        let on_provider = move |i: usize| {
            if i > 0 {
                super::model_field::kick_discovery(&ctx_models, &providers2[i - 1]);
            }
        };

        let key = row.key.clone();
        let providers3 = providers.clone();
        let ctx3 = ctx2.clone();
        let submit = move || {
            if in_flight.get_untracked() {
                return;
            }
            form_error.set(None);
            let i = provider_sel.get_untracked();
            let m = model.get_untracked().trim().to_string();
            let u = base_url.get_untracked().trim().to_string();
            let r_ix = reasoning_sel.get_untracked();
            let mut opts = match parse_options(&options.get_untracked()) {
                Ok(o) => o,
                Err(e) => {
                    form_error.set(Some(e));
                    return;
                }
            };
            let mtp_changed = is_text && mtp_sel.get_untracked() != mtp_init_ix;
            if mtp_changed {
                set_mtp_policy_option(&mut opts, mtp_sel.get_untracked());
            }
            // THE DIFF. Each field is named only when the operator
            // moved it; an emptied field is named as "" so the store
            // CLEARS it rather than silently keeping the old value.
            let edit = writes::RouteEdit {
                provider: (i != p0).then(|| {
                    if i > 0 {
                        providers3[i - 1].clone()
                    } else {
                        String::new()
                    }
                }),
                model: (m != model_init.trim()).then(|| m.clone()),
                base_url: (u != url_init.trim()).then(|| u.clone()),
                reasoning: (is_text && r_ix != reasoning_init_ix).then(|| {
                    crate::store::REASONING_LEVELS
                        .get(r_ix.wrapping_sub(1))
                        .map(|s| (*s).to_string())
                        .unwrap_or_default()
                }),
                options: (options.get_untracked() != options_init || mtp_changed).then_some(opts),
            };
            if edit.is_empty() {
                form_error.set(Some(
                    "nothing changed — edit a field, or Clear override drops the route".into(),
                ));
                return;
            }
            let spec = writes::set_route(&key, &edit, base, Some(form_id));
            in_flight.set(true);
            ctx3.send(Cmd::Write(Box::new(spec)));
        };
        let submit_btn = submit.clone();

        // The model row: discovery-backed picker filtered by the
        // route's MODALITY (embedding.* → embedding models; the rest
        // → generative), free-typing fallback preserved.
        let mf = super::model_field::model_field(
            mcx,
            theme,
            super::model_field::ModelFieldSpec {
                store: ctx2.store,
                providers: providers.clone(),
                provider_sel,
                model_text: model,
                class: crate::models::class_for_route(&row.kind, &row.modality),
                submit: None,
            },
        );
        let mf_handle = mf.handle.clone();
        let mf_custom = mf.custom;

        let hint = row
            .option_examples
            .as_ref()
            .map(|e| format!("option examples: {e}"))
            .unwrap_or_default();
        let reasoning_opts: Vec<SelectOption> = std::iter::once(SelectOption::new("not set"))
            .chain(
                crate::store::REASONING_LEVELS
                    .iter()
                    .map(|l| SelectOption::new(*l)),
            )
            .collect();
        let row_for_clear = row.clone();
        let ctx_clear = ctx2.clone();
        let close_after_clear = close.clone();
        let clear_enabled = row.configured && row.covered_by.is_none();
        Block::new()
            .title(format!("Route — {} ({})", row.label, row.key))
            .layout(LayoutStyle::column().grow(1.0))
            .child(
                Element::new()
                    .style(LayoutStyle::column().gap(0))
                    .shortcut(KeyChord::plain(Key::Char('m')), {
                        let ctx_m = ctx2.clone();
                        move |_| {
                            if !mf_handle.open() {
                                ctx_m.store.notice.set(Some(
                                    "no picker to open — pick a provider first (or Ctrl+P \
                                     leaves custom typing)"
                                        .into(),
                                ));
                            }
                        }
                    })
                    .shortcut(KeyChord::plain(Key::Char('c')), move |_| {
                        mf_custom.set(Some(true));
                    })
                    .shortcut(KeyChord::new(Mods::CTRL, Key::Char('p')), move |_| {
                        mf_custom.set(Some(false));
                    })
                    .child(applies_now(&t, &row))
                    .child(field(
                        &t,
                        "provider",
                        Select::new(popts)
                            .value(provider_sel)
                            .on_change(on_provider)
                            .view(mcx),
                    ))
                    .child(mf.view)
                    .child(field(
                        &t,
                        "base URL",
                        TextInput::new()
                            .layout(LayoutStyle::default().grow(1.0).h(1))
                            .value(base_url)
                            .placeholder("optional — http(s)://…")
                            .view(mcx),
                    ))
                    // Reasoning belongs to TEXT GENERATION only — the
                    // row is absent (not disabled) on every other
                    // route, exactly as the web console hides it.
                    .child(if is_text {
                        field(
                            &t,
                            "reasoning",
                            Select::new(reasoning_opts).value(reasoning_sel).view(mcx),
                        )
                    } else {
                        Element::new().style(LayoutStyle::default().h(0)).build()
                    })
                    .child(field(
                        &t,
                        "options",
                        TextInput::new()
                            .layout(LayoutStyle::default().grow(1.0).h(1))
                            .value(options)
                            .placeholder("k=v pairs, space-separated (voice=M2, k=\"a b\")")
                            .view(mcx),
                    ))
                    .child(if is_text {
                        field(&t, "MTP policy", Select::new(
                            ["inherit / not set", "Off", "Depth 2", "Depth 3", "Depth 4", "Depth 5", "custom (keep options)"]
                                .iter().map(|s| SelectOption::new(*s)).collect::<Vec<_>>()
                        ).value(mtp_sel).view(mcx))
                    } else { Element::new().style(LayoutStyle::default().h(0)).build() })
                    .child(if is_text {
                        line(vec![span(" MTP: compatible models only; missing heads require provisioning/reload.", t.text_faint)])
                    } else { Element::new().style(LayoutStyle::default().h(0)).build() })
                    .child(line(vec![span(format!(" {hint}"), t.text_faint)]))
                    .child(message_slot(theme, form_error, in_flight))
                    .child(
                        Element::new()
                            .style(LayoutStyle::row().gap(2).shrink(0.0))
                            .child(Button::new("Save").on_click(submit_btn).view(mcx))
                            .child(
                                // ONE policy for a destructive clear:
                                // the same danger confirm as the
                                // table's x. Close this editor first
                                // (prompt-over-modal would stack two
                                // modals — the engine hazard), then
                                // prompt on the SCREEN scope, which
                                // outlives the editor.
                                Button::new("Clear override")
                                    .disabled(!clear_enabled)
                                    .on_click(move || {
                                        close_after_clear();
                                        confirm_clear(screen_cx, &ctx_clear, row_for_clear.clone());
                                    })
                                    .view(mcx),
                            )
                            .child(
                                Button::new("Cancel (Esc)")
                                    .on_click({
                                        let close = close.clone();
                                        move || close()
                                    })
                                    .view(mcx),
                            )
                            .build(),
                    )
                    .build(),
            )
            .element(&t)
            .build()
    });
}

/// Render stored options for the edit buffer. Values containing
/// whitespace are double-quoted (so they re-parse), and STRING values
/// that would JSON-parse as scalars ("true", "42") are quoted too —
/// the CLI JSON-parses option values, so an unquoted round trip would
/// silently flip their type (M2 review P3-6).
fn mtp_policy_index(options: Option<&serde_json::Value>) -> usize {
    use serde_json::Value;
    match options.and_then(|o| o.get("speculation")) {
        None | Some(Value::Null) => 0,
        Some(Value::Bool(false)) => 1,
        Some(v) if v.get("mode").and_then(Value::as_str) == Some("off") => 1,
        Some(v) => match v.get("num_draft_tokens").and_then(Value::as_u64) {
            Some(n @ 2..=5) => n as usize,
            _ => 6,
        },
    }
}

fn set_mtp_policy_option(options: &mut Vec<(String, String)>, index: usize) {
    if index > 5 {
        return;
    }
    options.retain(|(key, _)| key != "speculation");
    if index == 1 {
        options.push(("speculation".into(), "false".into()));
    } else if index >= 2 {
        options.push((
            "speculation".into(),
            serde_json::json!({
                "mode": "native_mtp", "num_draft_tokens": index, "require_acceleration": false,
            })
            .to_string(),
        ));
    }
}

fn render_options(o: &serde_json::Map<String, serde_json::Value>) -> String {
    o.iter()
        .map(|(k, v)| {
            let vs = match v {
                serde_json::Value::String(s) => {
                    let ambiguous = serde_json::from_str::<serde_json::Value>(s)
                        .map(|p| !p.is_string())
                        .unwrap_or(false);
                    if s.contains(char::is_whitespace) || ambiguous {
                        format!("\"{s}\"")
                    } else {
                        s.clone()
                    }
                }
                other => other.to_string(),
            };
            format!("{k}={vs}")
        })
        .collect::<Vec<_>>()
        .join(" ")
}

/// Parse the edit buffer back to k=v pairs. Double quotes group a
/// value containing spaces AND force string typing downstream (the
/// quotes travel in argv; Python's json.loads reads them as a string).
pub fn parse_options(raw: &str) -> Result<Vec<(String, String)>, String> {
    let mut out = Vec::new();
    let mut tokens: Vec<String> = Vec::new();
    let mut cur = String::new();
    let mut in_quotes = false;
    for ch in raw.chars() {
        match ch {
            '"' => {
                in_quotes = !in_quotes;
                cur.push(ch);
            }
            c if c.is_whitespace() && !in_quotes => {
                if !cur.is_empty() {
                    tokens.push(std::mem::take(&mut cur));
                }
            }
            c => cur.push(c),
        }
    }
    if in_quotes {
        return Err("unterminated quote in options".into());
    }
    if !cur.is_empty() {
        tokens.push(cur);
    }
    for tok in tokens {
        match tok.split_once('=') {
            Some((k, v)) if !k.is_empty() => out.push((k.to_string(), v.to_string())),
            _ => {
                return Err(format!(
                    "options are space-separated k=v pairs (quote spaced values: k=\"a b\") — \
                     \"{tok}\" is not"
                ))
            }
        }
    }
    Ok(out)
}

/// Route options are the one route field with free-form operator
/// content (unknown scalars fold into it, capability_defaults.py:246-287)
/// — a key-shaped option value must not echo on screen (review P3-11).
fn mask_options(options: &serde_json::Value) -> String {
    let Some(obj) = options.as_object() else {
        return options.to_string();
    };
    let parts: Vec<String> = obj
        .iter()
        .map(|(k, v)| {
            let kl = k.to_ascii_lowercase();
            if kl.contains("key") || kl.contains("token") || kl.contains("secret") {
                format!("{k}=«redacted»")
            } else {
                format!("{k}={v}")
            }
        })
        .collect();
    format!("{{{}}}", parts.join(", "))
}

/// `gcx` builds the table; `page_cx` is the scope activation opens its
/// modal on — the routes dyn is disposed on every reload, and a form
/// parented to it would vanish mid-edit.
fn routes_table(
    gcx: Scope,
    page_cx: Scope,
    ctx: &Ctx,
    t: &TokenSet,
    data: &RoutesData,
    sel: Signal<usize>,
) -> View {
    // Width-aware columns: which columns APPEAR is a breakpoint decision
    // (source drops first, provider second — the sibling's 0900-class
    // recipe); how wide the survivors are is measured from the rows
    // themselves by `ui::widths`, never from a constant. The old policy
    // spent constants here (`Cells(30)`, `Cells(20)`, `ellipsize(model,
    // 40)`) and handed the leftover to a Flex model column, which is how
    // a 200-cell terminal ended up printing
    // `AbstractFramework/wan2.2-t2v-a14b-diffu…` beside seventy blank
    // cells.
    let w = abstracttui::app::use_viewport(gcx).get().w;
    // Read reactively: when the weights probe lands, the grid repaints.
    // An empty map (probe not answered) prints nothing, never "absent".
    let weights = ctx
        .store
        .availability
        .with(|d| d.ready().map(|a| a.by_route.clone()).unwrap_or_default());
    let mut rows: Vec<Vec<String>> = data
        .rows
        .iter()
        .map(|r| {
            // THE shared state vocabulary, straight off the row model —
            // the same four strings the gateway console prints.
            let state = r.state_label();
            // `⊘` U+2298, NOT `🔒` U+1F512: the padlock is Emoji=Yes and
            // measures 2 cells, and terminals/fonts routinely draw it at
            // a different advance than the engine measured — the row's
            // later columns then slide and overlap (visible on the alias
            // row, and only there, since it is the row that carries the
            // marker). The engine's own glyph research rejects emoji
            // outright and lands on U+2298: width 1 under BOTH
            // unicode-width conventions, in Mathematical Operators, a
            // block emoji-data never touches. The `state` column already
            // spells the reason in words, so the glyph is garnish.
            let lock = if !r.editable() { " ⊘" } else { "" };
            // THE ROUTE COLUMN CARRIES THE HIERARCHY. `output.image` is
            // the PARENT of `output.image.*` — one value for every image
            // task, overridden per task by the rows beneath it. Printed
            // as four flat siblings with the parent on top reading "not
            // configured", it looked like a leftover key ("why do we have
            // output.image AND t2i/i2i/upscale?"). `display_key()` indents
            // the children under a tree marker and drops the repeated
            // parent prefix; the parent names what it is for.
            let mut row = vec![format!("{}{}", r.display_key(), lock), state];
            if w >= 96 {
                row.push(or_dash(&r.provider));
            }
            // The FULL model name — the column solver sizes to it and
            // cuts it only if the terminal cannot carry it. A constant
            // cap here truncated the payload while the space to print it
            // whole sat unused one column over.
            row.push(or_dash(&r.model));
            // WEIGHTS: is this route's model actually on the machine?
            // Blank while unprobed and for rows that name no model — the
            // absence of an answer must not read as an answer.
            row.push(
                weights
                    .get(&r.key)
                    .map(|w| w.label().to_string())
                    .unwrap_or_default(),
            );
            if w >= 112 {
                row.push(r.source.clone());
            }
            row
        })
        .collect();
    // The rules carry a FLOOR, not a width: nothing is capped while the
    // terminal has room. Route keys, provider ids, model artifacts and
    // source modules all discriminate on their TAIL
    // (`…image_to_scene3d`, `…-t2v-a14b-diffusers-8bit`), so those cut in
    // the middle; the state and weights vocabularies read from the left.
    // `state` and `weights` print CLOSED VOCABULARIES, so their floor is
    // the widest word each can say — a squeezed vocabulary column is not
    // a shorter answer, it is a different (wrong) one. The open columns
    // (route/provider/model/source) carry operator content and take the
    // squeeze on its behalf.
    let mut rules = vec![
        widths::ColRule::tail("route", 18),
        widths::ColRule::head("state", 21),
    ];
    if w >= 96 {
        rules.push(widths::ColRule::tail("provider", 10));
    }
    rules.push(widths::ColRule::tail("model", 22));
    // The weights column earns its floor at every width: "configured
    // but not downloaded" is the single most common reason a route that
    // LOOKS right does not run, and hiding it on a narrow terminal
    // hides exactly the machine most likely to be a fresh install. 14
    // cells is the widest label it prints ("not downloaded"), so this
    // floor is the whole vocabulary, never a stub.
    rules.push(widths::ColRule::head("weights", 14));
    if w >= 112 {
        // Source attribution per row — the payload's own `source`
        // string, the same column the gateway console shows.
        rules.push(widths::ColRule::tail("source", 12));
    }
    // This screen mounts straight into PageHost's page region, which adds
    // no horizontal chrome of its own — the table's rect IS the viewport.
    // (The gateway console's routes screen wraps the same grid in a
    // bordered block and subtracts 2; the policy is shared, the chrome is
    // per-screen.)
    let cols = widths::columns(&rules, &mut rows, w);
    let ctx_act = ctx.clone();
    Table::new(cols)
        .rows(rows)
        .selection(sel)
        // Enter, Space and double-click = the `e` verb, to the row the
        // Table just selected (select() runs before activate, so
        // `ui.route_sel` is already the activated row). Routing through
        // edit_selected keeps ONE set of refusals — the output.text
        // alias, covered/read-only rows, and the write door all answer
        // the same way whether the user pressed a key or double-clicked.
        .on_activate(move |_| edit_selected(page_cx, &ctx_act))
        .layout(LayoutStyle::default().grow(1.0))
        .element(gcx, t)
        .autofocus()
        .build()
}

#[cfg(test)]
mod tests {
    use super::{mtp_policy_index, parse_options, render_options, set_mtp_policy_option};
    use serde_json::json;

    #[test]
    fn mtp_policy_preserves_options_off_and_inherit() {
        let mut pairs = vec![("other".into(), "4".into())];
        set_mtp_policy_option(&mut pairs, 2);
        assert_eq!(pairs[0], ("other".into(), "4".into()));
        let value: serde_json::Value = serde_json::from_str(&pairs[1].1).unwrap();
        assert_eq!(value["num_draft_tokens"], 2);
        assert_eq!(value["require_acceleration"], false);
        assert_eq!(mtp_policy_index(Some(&json!({"speculation":value}))), 2);
        set_mtp_policy_option(&mut pairs, 1);
        assert_eq!(pairs[1], ("speculation".into(), "false".into()));
        assert_eq!(mtp_policy_index(Some(&json!({"speculation":false}))), 1);
        set_mtp_policy_option(&mut pairs, 0);
        assert_eq!(pairs, vec![("other".into(), "4".into())]);
    }

    #[test]
    fn custom_policy_survives_round_trip() {
        let value = json!({"speculation":{"num_draft_tokens":7}, "other":true});
        assert_eq!(mtp_policy_index(Some(&value)), 6);
        let mut pairs = parse_options(&render_options(value.as_object().unwrap())).unwrap();
        let before = pairs.clone();
        set_mtp_policy_option(&mut pairs, 6);
        assert_eq!(pairs, before);
    }

    /// The options round trip (M2 review P3-6): spaced values quote
    /// and re-parse; ambiguous strings keep their type via quoting.
    #[test]
    fn options_round_trip_spaces_and_types() {
        let stored = json!({
            "voice": "M2",
            "prompt": "hello world",
            "flag": true,
            "stringy": "true",
            "n": 42
        });
        let rendered = render_options(stored.as_object().unwrap());
        assert!(rendered.contains("voice=M2"), "{rendered}");
        assert!(rendered.contains("prompt=\"hello world\""), "{rendered}");
        assert!(rendered.contains("flag=true"), "{rendered}");
        assert!(
            rendered.contains("stringy=\"true\""),
            "string-typed 'true' quotes so a re-save keeps it a string: {rendered}"
        );
        let parsed = parse_options(&rendered).unwrap();
        assert!(parsed
            .iter()
            .any(|(k, v)| k == "prompt" && v == "\"hello world\""));
        assert!(parsed
            .iter()
            .any(|(k, v)| k == "stringy" && v == "\"true\""));

        assert!(parse_options("k=\"unterminated").is_err());
        let err = parse_options("novalue").unwrap_err();
        assert!(err.contains("k=v"), "{err}");
        assert!(parse_options("").unwrap().is_empty());
    }
}