mindfork 0.10.1

A terminal AI chat written in Rust: local models via llama.cpp or OpenAI, Anthropic, Gemini and Grok in the cloud, with persistent memory, notes, RAG and tools.
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
//! Running a typed command (`features::ui_command`) on the chat screen.
//!
//! **A command is its key, plus a voice.** Every arm below reaches the action
//! through the *same* handler the chord uses — `handle_ctrl_shortcut` for the
//! `Ctrl` keys, the same intent for the function keys — so the two routes cannot
//! drift into two semantics, confirmation popups and gates included. What a
//! command adds is an answer: a chord that does nothing is cheap, while a typed
//! command that vanishes reads as the app refusing to act, so every precondition
//! that blocks a command says so and names the route that works
//! (docs/lessons.md §4).
//!
//! See [docs/history/command-only-control.md](../../../docs/history/command-only-control.md)
//! §4.3 and spec §11.7.

use super::*;
use crate::features::profile_command::TextEdit;
use crate::features::profiles::ProfileEdit;
use crate::features::ui_command::{self, UiCommand};

/// Which of the assistant profile's two free-text fields a `/profile` text
/// subcommand edits. Both fields are copied into a chat at creation, so both
/// edits apply to new conversations — the notes say so.
#[derive(Clone, Copy, PartialEq)]
enum ProfileText {
    System,
    Greeting,
}

/// Case-insensitive exact match first, then an unambiguous prefix, over
/// `(id, name)` pairs — the one matching rule every command that names a
/// profile or a persona uses (`/new`, `/profile delete`,
/// `/impersonation delete|use`). `Err` carries the candidates that matched
/// (empty — nothing did), for the caller's message.
fn resolve_named(items: &[(Uuid, String)], wanted: &str) -> Result<(Uuid, String), Vec<String>> {
    let wanted = wanted.to_lowercase();
    let matching = |exact: bool| -> Vec<&(Uuid, String)> {
        items
            .iter()
            .filter(|(_, name)| {
                let name = name.to_lowercase();
                if exact {
                    name == wanted
                } else {
                    name.starts_with(&wanted)
                }
            })
            .collect()
    };
    let mut hits = matching(true);
    if hits.is_empty() {
        hits = matching(false);
    }
    if let [hit] = hits.as_slice() {
        return Ok((*hit).clone());
    }
    Err(hits.into_iter().map(|(_, name)| name.clone()).collect())
}

/// The kind words of `/tasks stop <kind>` (docs/research/tasks-stop-command.md
/// §3.1): each the word of the command or screen the task belongs with
/// (`/self` is the self-model, `/compact` the roll, the notes are the notes),
/// one spelling, matched without case — protocol, like every command word,
/// never localized. In the order the tasks screen lists the rows; the notes
/// quote this table, so a word cannot exist unadvertised.
static TASK_KINDS: [(&str, BackgroundKind); 4] = [
    ("reflection", BackgroundKind::Reflection),
    ("notes", BackgroundKind::Consolidation),
    ("self", BackgroundKind::SelfConsolidation),
    ("compact", BackgroundKind::Compaction),
];

/// The four words, joined for a note.
fn task_words() -> String {
    TASK_KINDS
        .iter()
        .map(|(word, _)| *word)
        .collect::<Vec<_>>()
        .join(" · ")
}

impl ChatScreen {
    /// The registry's commands (`/settings`, `/find`, `/regen`, …) — intercepted
    /// on `Enter` and never sent as a message. Returns `None` when the text is
    /// not one of them.
    ///
    /// The box is cleared first, like every other command: each keystroke has
    /// already reached the orchestrator as a draft, so leaving the command in it
    /// would greet the user with it on the next launch (journal ui-input.md,
    /// `/exit`).
    pub(super) fn try_ui_command(&mut self, text: &str) -> Option<Option<ChatIntent>> {
        let parsed = ui_command::parse(text, self.loc)?;
        self.input.clear();
        self.mark_input_changed();
        Some(match parsed {
            Ok(cmd) => self.run_ui_command(cmd.command, cmd.argument, cmd.alias),
            Err(msg) => {
                self.push_note(&msg);
                None
            }
        })
    }

    /// Dispatches a parsed command. Exhaustive on [`UiCommand`], so a new
    /// registry row cannot ship without an answer here.
    ///
    /// One line per command: an arm that needs a precondition checked delegates
    /// to a named method below rather than spelling the check out here. That
    /// keeps this readable as the dispatch *table* it is — and keeps its
    /// cognitive complexity off the analyzer's bar, which a table of twenty
    /// arms reaches on the strength of a few `if`s alone.
    fn run_ui_command(
        &mut self,
        command: UiCommand,
        argument: String,
        alias: &'static str,
    ) -> Option<ChatIntent> {
        match command {
            // The screens.
            UiCommand::Settings => self.open_settings_screen(),
            // Bare — the screen; `clear` — the wipe it offers behind `Ctrl+K`
            // twice, behind a confirmation here for the same reason.
            UiCommand::SelfModel if argument == "clear" => self.ask_clear_self_model(),
            UiCommand::SelfModel => Some(ChatIntent::OpenSelfModel),
            // Deliberately not `Esc`'s behaviour: `Esc` cancels a running
            // generation first, and this command is the half that always means
            // "the chat list" (`/stop` is the other half).
            UiCommand::Chats => Some(ChatIntent::OpenChatList),
            UiCommand::Changes => Some(ChatIntent::OpenChanges),
            UiCommand::Tasks => self.typed_tasks(argument),
            // The runtime owns the overlay; the typed route reports the intent
            // it opens on, and is the chat's only route of its own — `?` was
            // dropped, `F1` never reaches the screen (spec §11.7).
            UiCommand::Help => Some(ChatIntent::OpenHelp),
            // The conversation.
            UiCommand::NewChat if argument.is_empty() => self.request_new_chat(),
            UiCommand::NewChat => self.new_chat_by_name(&argument),
            UiCommand::Rename => self.rename_active_chat(argument),
            UiCommand::AutoTitle => self.typed_autotitle(),
            UiCommand::Clone => self.active_chat.map_or_else(
                || self.note("ui.cmd.no_chat"),
                |id| Some(ChatIntent::CloneChat(id)),
            ),
            UiCommand::Copy => self.active_chat.map_or_else(
                || self.note("ui.cmd.no_chat"),
                |id| Some(ChatIntent::CopyChat(id)),
            ),
            UiCommand::Regen | UiCommand::Takeback => self.typed_destructive(command, alias),
            UiCommand::Continue => self.typed_continue(alias),
            UiCommand::Impersonate => self.typed_impersonate(argument, alias),
            UiCommand::Stop => self.typed_stop(),
            // Finding things.
            UiCommand::Find => self.typed_find(argument),
            UiCommand::Search => Some(ChatIntent::SearchMessages { query: argument }),
            UiCommand::Links => {
                // Says what a reference looks like when the chat holds none.
                self.open_chat_links();
                None
            }
            // The feed.
            UiCommand::Thoughts => self.handle_ctrl_shortcut('t').flatten(),
            UiCommand::ToolCalls => self.handle_ctrl_shortcut('o').flatten(),
            UiCommand::Subagents => self.typed_subagents(argument),
            UiCommand::Mouse => self.handle_ctrl_shortcut('w').flatten(),
            UiCommand::Emoji => self.handle_ctrl_shortcut('b').flatten(),
        }
    }

    /// `/settings` — `Ctrl+P` silently does nothing until the first `Settings`
    /// event has arrived; the command says why instead.
    fn open_settings_screen(&mut self) -> Option<ChatIntent> {
        if self.settings_snapshot.is_none() {
            return self.note("ui.cmd.settings_pending");
        }
        self.handle_ctrl_shortcut('p').flatten()
    }

    /// `/self clear` — the model belongs to the *active profile*, so with no
    /// chat open the orchestrator would silently drop the edit: say so instead.
    fn ask_clear_self_model(&mut self) -> Option<ChatIntent> {
        if self.active_chat.is_none() {
            return self.note("ui.cmd.no_chat");
        }
        self.confirm = Some(ConfirmAction::ClearSelfModel);
        None
    }

    /// `/subagents [expand|collapse]` — the chat list's `Ctrl+O` for the open
    /// chat: folds or unfolds its sub-agent transcripts in the list
    /// (spec §11.2); bare it toggles like the key, the two words set the state
    /// outright. The effect lives on another screen, so the note says which
    /// way it went and where. From an open transcript the parent's list is
    /// what folds — the state is the parent's, as everywhere. With no
    /// transcripts (or before the list snapshot has arrived) the command
    /// explains instead: the rows appear once the assistant calls a sub-agent.
    fn typed_subagents(&mut self, argument: String) -> Option<ChatIntent> {
        let Some(active) = self.active_chat else {
            return self.note("ui.cmd.no_chat");
        };
        // `stop [n]`: a background run, not the fold (spec §9.3.2).
        if let Some(which) = argument.strip_prefix("stop") {
            return self.typed_subagents_stop(active, which.trim());
        }
        // The parser normalized the word to the registry's spelling; anything
        // else it reported itself, so bare is the only other shape here.
        let want = match argument.as_str() {
            "expand" => Some(true),
            "collapse" => Some(false),
            _ => None,
        };
        let target = self
            .chats
            .iter()
            .find(|c| c.id == active || c.child_ids().any(|k| k == active))
            .filter(|c| !c.children.is_empty())
            .map(|c| (c.id, want.unwrap_or(!c.children_expanded)));
        let Some((id, expanded)) = target else {
            return self.note("ui.cmd.subagents_none");
        };
        self.push_note(self.loc.t(if expanded {
            "ui.cmd.subagents_expanded"
        } else {
            "ui.cmd.subagents_collapsed"
        }));
        Some(ChatIntent::SetChildrenExpanded { id, expanded })
    }

    /// `/subagents stop [n]` (spec §9.3.2): on an open transcript, that run;
    /// on a chat, the n-th background run out under it — or the only one when
    /// there is one. Resolved from the list's cards, where *out* is
    /// `running && background`; the orchestrator ignores an id that is not.
    /// `pub(super)` because `F6` on the transcript is this very route
    /// (`input.rs`), not a second one.
    pub(super) fn typed_subagents_stop(&mut self, active: Uuid, which: &str) -> Option<ChatIntent> {
        let parent = self
            .chats
            .iter()
            .find(|c| c.id == active || c.child_ids().any(|k| k == active));
        let out: Vec<(Uuid, String)> = parent
            .map(|c| {
                c.children
                    .iter()
                    .filter(|r| r.running && r.background)
                    .map(|r| (r.id, r.title.clone()))
                    .collect()
            })
            .unwrap_or_default();
        let on_transcript = parent.is_some_and(|c| c.id != active);
        let chosen = if on_transcript {
            out.iter().find(|(id, _)| *id == active).cloned()
        } else if let Ok(n) = which.parse::<usize>() {
            n.checked_sub(1).and_then(|i| out.get(i).cloned())
        } else if which.is_empty() && out.len() > 1 {
            let text = self.loc.tf(
                "ui.cmd.subagents_stop_which",
                &[("n", &out.len().to_string())],
            );
            self.push_note(&text);
            return None;
        } else if which.is_empty() {
            out.first().cloned()
        } else {
            None
        };
        let Some((id, title)) = chosen else {
            return self.note("ui.cmd.subagents_no_running");
        };
        let text = self
            .loc
            .tf("ui.cmd.subagents_stopped", &[("title", &title)]);
        self.push_note(&text);
        Some(ChatIntent::StopSubagentRun { id })
    }

    /// `/tasks [stop <kind>]` (spec §11.10): bare — the screen, as `F7`;
    /// `stop` — one of the app's own silent tasks, the typed route to `F6` on
    /// its row there (docs/research/tasks-stop-command.md §3.2). The parser
    /// normalized `stop` and handed the kind over as typed.
    fn typed_tasks(&mut self, argument: String) -> Option<ChatIntent> {
        let Some(which) = argument.strip_prefix("stop") else {
            return Some(ChatIntent::OpenTasks);
        };
        self.typed_tasks_stop(which.trim())
    }

    /// `/tasks stop [kind]`: the named task if it is running, else a note that
    /// says so; bare, the only running task — several are listed by word,
    /// none is a note (the `/subagents stop` shape). *Running* is the chat
    /// screen's own flag for the kind — the status bar's source, on exactly
    /// while the task's slot is taken, streaming or waiting — so the answer is
    /// immediate and the same predicate the tasks screen's `F6` uses. The
    /// intent ends in the very command that key sends; a kind that landed
    /// while the command was typed reaches an idle slot and is ignored there.
    fn typed_tasks_stop(&mut self, which: &str) -> Option<ChatIntent> {
        // `all`: every running task, in the table's order — a modifier, not
        // a kind, so it is checked before the table and never listed as one
        // (docs/research/tasks-stop-all.md §3). The stops fan out in the
        // runtime into the very command `F6` sends, once per kind.
        if which.eq_ignore_ascii_case("all") {
            let running = self.running_tasks();
            if running.is_empty() {
                return self.note("ui.cmd.tasks_none_running");
            }
            let names = running
                .iter()
                .map(|(_, kind)| self.loc.t(kind.label_key()))
                .collect::<Vec<_>>()
                .join(", ");
            let text = self
                .loc
                .tf("ui.cmd.tasks_stopping_all", &[("tasks", &names)]);
            self.push_note(&text);
            let kinds = running.into_iter().map(|(_, kind)| kind).collect();
            return Some(ChatIntent::StopBackgroundTasks { kinds });
        }
        let kind = if which.is_empty() {
            let running = self.running_tasks();
            match running.as_slice() {
                [] => return self.note("ui.cmd.tasks_none_running"),
                [(_, kind)] => *kind,
                several => {
                    let words = several
                        .iter()
                        .map(|(word, _)| *word)
                        .collect::<Vec<_>>()
                        .join(" · ");
                    let text = self.loc.tf("ui.cmd.tasks_stop_which", &[("kinds", &words)]);
                    self.push_note(&text);
                    return None;
                }
            }
        } else {
            let known = TASK_KINDS
                .iter()
                .find(|(word, _)| which.eq_ignore_ascii_case(word));
            let Some((_, kind)) = known else {
                let text = self.loc.tf(
                    "ui.cmd.tasks_bad_kind",
                    &[("arg", which), ("kinds", &task_words())],
                );
                self.push_note(&text);
                return None;
            };
            *kind
        };
        let task = self.loc.t(kind.label_key());
        if !self.task_running(kind) {
            let text = self.loc.tf("ui.cmd.tasks_not_running", &[("task", task)]);
            self.push_note(&text);
            return None;
        }
        let text = self.loc.tf("ui.cmd.tasks_stopping", &[("task", task)]);
        self.push_note(&text);
        Some(ChatIntent::StopBackgroundTasks { kinds: vec![kind] })
    }

    /// The app's tasks whose slot is taken right now, with their words, in
    /// the table's order — what bare `stop` chooses among and `all` takes.
    fn running_tasks(&self) -> Vec<(&'static str, BackgroundKind)> {
        TASK_KINDS
            .iter()
            .copied()
            .filter(|(_, kind)| self.task_running(*kind))
            .collect()
    }

    /// `/regen`·`/retry` and `/takeback`. Both are ignored during generation by
    /// `trigger_destructive`, and both honour `confirm_destructive_keys` — the
    /// popup is the key's, reached through the key's own path. The only thing
    /// added here is the answer the key does not owe.
    fn typed_destructive(&mut self, command: UiCommand, alias: &'static str) -> Option<ChatIntent> {
        if self.generating {
            return self.busy_note(alias);
        }
        let chord = if command == UiCommand::Regen {
            'r'
        } else {
            'e'
        };
        self.handle_ctrl_shortcut(chord).flatten()
    }

    /// `/continue` — resume the last interrupted reply (spec §6.4). The screen
    /// answers the two states it can see (a running turn, no chat); whether
    /// the tail is continuable and whether the mode supports continuation is
    /// the orchestrator's knowledge, and its refusals answer through the same
    /// feed notes (`ui.cmd.continue_*`).
    fn typed_continue(&mut self, alias: &'static str) -> Option<ChatIntent> {
        if self.generating {
            return self.busy_note(alias);
        }
        if self.active_chat.is_none() {
            return self.note("ui.cmd.no_chat");
        }
        Some(ChatIntent::ContinueLast)
    }

    /// `/impersonate [text]` — the seed is the rest of the line, where the key
    /// takes whatever was already in the box (the command consumed the box).
    fn typed_impersonate(&mut self, seed: String, alias: &'static str) -> Option<ChatIntent> {
        if self.generating {
            return self.busy_note(alias);
        }
        Some(ChatIntent::Impersonate { seed })
    }

    /// `/stop` — the explicit half of `Esc`, and the half that has something to
    /// say when there is nothing to cancel.
    fn typed_stop(&mut self) -> Option<ChatIntent> {
        if self.generating {
            return Some(ChatIntent::Cancel);
        }
        self.note("ui.cmd.not_generating")
    }

    /// `/find [text]` — `open_feed_search` resumes the chat's last query, so
    /// seeding it here is what makes `/find <text>` land on a match without a
    /// second keystroke.
    fn typed_find(&mut self, query: String) -> Option<ChatIntent> {
        if !query.is_empty() {
            self.search_last = query;
        }
        self.open_feed_search();
        None
    }

    /// Renames the open chat. With a title — straight through, as `F2` in the
    /// chat list does. Bare, it hands the **current title back as an editable
    /// command line** rather than opening a popup of its own: the box is right
    /// there and already empty (a bare command is all it holds), so the user
    /// edits the title and presses `Enter`. That also teaches the syntax by
    /// example, which a popup would not.
    fn rename_active_chat(&mut self, title: String) -> Option<ChatIntent> {
        // Not `let id = self.active_chat?` — that swallows the refusal, which is
        // the very silence this track exists to remove (a test caught it here).
        let Some(id) = self.active_chat else {
            return self.note("ui.cmd.no_chat");
        };
        if title.is_empty() {
            self.input.set_text(&format!("/rename {}", self.title));
            self.mark_input_changed();
            return None;
        }
        // No ceiling: a title is cut by the surface drawing it, in columns
        // and with a marker (`shared::title::sanitize_title`, spec §11.2).
        Some(ChatIntent::RenameChat { id, title })
    }

    /// `/autotitle` — the chat list's `Ctrl+R` for the open conversation: ask
    /// the model to title it (spec §11.2). The key is browser-taken (`Ctrl+R`
    /// reloads the tab), which is what earned this action a typed route. The
    /// task takes seconds on a local model, so the command answers right away;
    /// the result is the visible rename, and a failure falls back to a feed
    /// note (`ChatListError` routing in `runtime::dispatch`).
    fn typed_autotitle(&mut self) -> Option<ChatIntent> {
        let Some(id) = self.active_chat else {
            return self.note("ui.cmd.no_chat");
        };
        self.push_note(self.loc.t("ui.cmd.autotitle_started"));
        Some(ChatIntent::AutoTitleChat(id))
    }

    /// `/new <profile>`: the shared name resolver, then the same intent
    /// `Ctrl+N` produces once a profile is chosen.
    fn new_chat_by_name(&mut self, wanted: &str) -> Option<ChatIntent> {
        let (id, _) = self.resolve_profile(wanted, "ui.cmd.route_new")?;
        Some(ChatIntent::NewChat {
            profile_id: Some(id),
        })
    }

    /// Resolves a profile by name: an exact match first (case-insensitively),
    /// then an unambiguous prefix (fork F3). On a miss or an ambiguity it leaves
    /// a note naming the candidates and the `route` that shows them, and returns
    /// `None`.
    ///
    /// One resolver for every command that names a profile (`/new <profile>`,
    /// `/profile delete <name>`) — the family's *contract* is shared, so its
    /// wording and its prefix rule are shared too, rather than being written
    /// twice and drifting (docs/lessons.md §2). `route` is a whole bundle key,
    /// never a built one: the callers want different next steps. The matching
    /// itself is [`resolve_named`], which `/impersonation` reuses over the
    /// personas.
    fn resolve_profile(&mut self, wanted: &str, route: &'static str) -> Option<(Uuid, String)> {
        let items: Vec<(Uuid, String)> = self
            .profiles
            .iter()
            .map(|p| (p.id, p.name.clone()))
            .collect();
        match resolve_named(&items, wanted) {
            Ok(hit) => Some(hit),
            Err(hits) => {
                let all = items.into_iter().map(|(_, name)| name).collect();
                self.report_unresolved(
                    wanted,
                    hits,
                    all,
                    ("ui.cmd.no_profile", "ui.cmd.many_profiles"),
                    route,
                );
                None
            }
        }
    }

    /// The note for a name [`resolve_named`] could not settle: nothing matched
    /// (`keys.0`, listing everything there is) or several did (`keys.1`,
    /// listing the contenders). Both name the `route` that shows the full list.
    fn report_unresolved(
        &mut self,
        wanted: &str,
        hits: Vec<String>,
        all: Vec<String>,
        keys: (&'static str, &'static str),
        route: &'static str,
    ) {
        let (key, names) = if hits.is_empty() {
            (keys.0, all.join(", "))
        } else {
            (keys.1, hits.join(", "))
        };
        let msg = self.loc.tf(
            key,
            &[
                ("name", &wanted.to_lowercase()),
                ("names", &names),
                ("route", self.loc.t(route)),
            ],
        );
        self.push_note(&msg);
    }

    /// The profile commands (`/profile list|new|delete`) — the settings screen's
    /// `Ctrl+N`/`Ctrl+D`, reachable from a host that keeps those keys. Stage 2 of
    /// docs/history/command-only-control.md (fork F5). Returns `None` when the
    /// text is not a `/profile` command.
    pub(super) fn try_profile_command(&mut self, text: &str) -> Option<Option<ChatIntent>> {
        use crate::features::profile_command::{self, ProfileCommand};
        let parsed = profile_command::parse(text, self.loc)?;
        self.input.clear();
        self.mark_input_changed();
        Some(match parsed {
            Ok(ProfileCommand::List) => {
                self.list_profiles();
                None
            }
            // The same default name the settings screen's `Ctrl+N` uses, so the
            // two routes create the same thing. The persona stays empty either
            // way — it is written in the settings screen afterwards, which is
            // what the orchestrator's notice says.
            Ok(ProfileCommand::New { name }) => Some(ChatIntent::CreateProfile {
                name: name
                    .unwrap_or_else(|| self.loc.t("ui.settings.new_profile_name").to_string()),
            }),
            Ok(ProfileCommand::Delete { name }) => self.confirm_delete_profile(&name),
            // Stage 3 (docs/history/commands-stage3.md §3.2): the profile's two text
            // fields, editable without the settings screen.
            Ok(ProfileCommand::System(edit)) => {
                self.profile_text_command(ProfileText::System, edit)
            }
            Ok(ProfileCommand::Greeting(edit)) => {
                self.profile_text_command(ProfileText::Greeting, edit)
            }
            Err(msg) => {
                self.push_note(&msg);
                None
            }
        })
    }

    /// `/impersonation …` — the impersonation profiles (the user personas,
    /// spec §11.8), plus the active profile's link to one. Stage 3 of
    /// docs/history/command-only-control.md (docs/history/commands-stage3.md §3.3):
    /// their CRUD lives behind the settings screen's browser-taken
    /// `Ctrl+N`/`Ctrl+D`, and the persona's text behind its editor. Returns
    /// `None` when the text is not an `/impersonation` command.
    pub(super) fn try_impersonation_command(&mut self, text: &str) -> Option<Option<ChatIntent>> {
        let parsed = crate::features::impersonation_command::parse(text, self.loc)?;
        self.input.clear();
        self.mark_input_changed();
        Some(match parsed {
            Ok(cmd) => self.run_impersonation_command(cmd),
            Err(msg) => {
                self.push_note(&msg);
                None
            }
        })
    }

    /// Dispatches a parsed `/impersonation` command. Every arm works over the
    /// settings snapshot (the personas live in the config), so its absence is
    /// answered once here — the `/settings` rule.
    fn run_impersonation_command(
        &mut self,
        cmd: crate::features::impersonation_command::ImpersonationCommand,
    ) -> Option<ChatIntent> {
        use crate::features::impersonation_command::ImpersonationCommand as Cmd;
        if self.settings_snapshot.is_none() {
            return self.note("ui.cmd.settings_pending");
        }
        match cmd {
            Cmd::List => {
                self.list_impersonations();
                None
            }
            Cmd::New { name } => self.create_impersonation(name),
            Cmd::Delete { name } => self.confirm_delete_impersonation(&name),
            Cmd::Use { name } => self.link_impersonation(&name),
            Cmd::UseDefault => self.unlink_impersonation(),
            Cmd::System(edit) => self.impersonation_text_command(edit),
        }
    }

    /// The export command (`/export [md|json] [path]`) — the conversation to a
    /// file, for when the clipboard cannot reach the user's machine at all
    /// (JupyterLab drops OSC 52, spec §11.7). The orchestrator writes it: it
    /// owns the conversation and the disk. Returns `None` when the text is not
    /// an `/export` command.
    pub(super) fn try_export_command(&mut self, text: &str) -> Option<Option<ChatIntent>> {
        let parsed = crate::features::export_command::parse(text, self.loc)?;
        self.input.clear();
        self.mark_input_changed();
        Some(match parsed {
            Ok(cmd) => match self.active_chat {
                Some(id) => Some(ChatIntent::ExportChat {
                    id,
                    format: cmd.format,
                    path: cmd.path,
                }),
                None => self.note("ui.cmd.no_chat"),
            },
            Err(msg) => {
                self.push_note(&msg);
                None
            }
        })
    }

    /// `/profile list` — the names, with the open chat's own profile marked.
    /// The screen already holds this snapshot, so it answers locally.
    fn list_profiles(&mut self) {
        if self.profiles.is_empty() {
            self.note("ui.profile.none");
            return;
        }
        let active = self
            .active_chat
            .and_then(|id| self.chats.iter().find(|c| c.id == id))
            .map(|c| c.profile_id);
        // The same marker the screens use for "this is the selected one"; it is
        // one column wide in both glyph sets, so the names stay aligned.
        let marker = self.palette.glyphs().title_marker;
        let names: Vec<String> = self
            .profiles
            .iter()
            .map(|p| {
                if Some(p.id) == active {
                    format!("{marker} {}", p.name)
                } else {
                    format!("  {}", p.name)
                }
            })
            .collect();
        let msg = self
            .loc
            .tf("ui.profile.list", &[("names", &names.join("\n"))]);
        self.push_note(&msg);
    }

    /// `/profile delete <name>` — resolve the name, then **always** ask.
    ///
    /// The key does not ask (`Ctrl+D` in the settings screen deletes the
    /// selected row outright), and this is the one place stage 2 departs from
    /// "a command is its key" — deliberately. The screen shows you the profile
    /// you are about to delete, and a typed prefix can resolve to one you did
    /// not picture; the popup is what puts the target, and the conversations
    /// going with it, back in front of you. Independent of
    /// `confirm_destructive_keys`, which is about the two chat-level keys.
    fn confirm_delete_profile(&mut self, wanted: &str) -> Option<ChatIntent> {
        let (id, name) = self.resolve_profile(wanted, "ui.cmd.route_profile_list")?;
        // The orchestrator refuses to delete the last profile (there would be
        // nothing to create chats from), so say so here instead of asking a
        // question whose "yes" is then declined.
        if self.profiles.len() <= 1 {
            return self.note("ui.profile.last");
        }
        let chats = self.chats.iter().filter(|c| c.profile_id == id).count();
        self.confirm = Some(ConfirmAction::DeleteProfile { id, name, chats });
        None
    }

    /// The active chat's full profile, cloned out of the settings snapshot —
    /// the working copy the text commands read and prefill from. `Err` is the
    /// note key that says which precondition failed: no chat open (a sub-agent
    /// transcript's id is not in the chat list, so it lands here too), or the
    /// snapshot not yet arrived / not yet carrying the profile.
    fn active_full_profile(&self) -> Result<Profile, &'static str> {
        let Some(chat_id) = self.active_chat else {
            return Err("ui.cmd.no_chat");
        };
        let Some(profile_id) = self
            .chats
            .iter()
            .find(|c| c.id == chat_id)
            .map(|c| c.profile_id)
        else {
            return Err("ui.cmd.no_chat");
        };
        let Some((_, profiles, ..)) = self.settings_snapshot.as_ref() else {
            return Err("ui.cmd.settings_pending");
        };
        profiles
            .iter()
            .find(|p| p.id == profile_id)
            .cloned()
            .ok_or("ui.cmd.settings_pending")
    }

    /// A working copy of the snapshot's config for a persona edit — the same
    /// clone-and-commit the settings screen's persona editors do.
    fn snapshot_config(&self) -> Option<AppConfig> {
        self.settings_snapshot
            .as_ref()
            .map(|(config, ..)| config.clone())
    }

    /// The personas as `(id, name)` pairs, for listing and resolving.
    fn personas(&self) -> Vec<(Uuid, String)> {
        self.settings_snapshot
            .as_ref()
            .map(|(config, ..)| {
                config
                    .impersonation_profiles
                    .iter()
                    .map(|p| (p.id, p.name.clone()))
                    .collect()
            })
            .unwrap_or_default()
    }

    /// `/profile system|greeting [text|clear]` on the active chat's profile.
    /// `Show` hands the current text back as an editable command line (the
    /// `/rename` pattern); `Set`/`Clear` commit through the same
    /// `UpdateProfile` the settings editors use, and the note states the
    /// scope honestly: both fields are copied into a chat at creation, so the
    /// edit reaches **new** conversations (spec §5.1, §10).
    fn profile_text_command(&mut self, field: ProfileText, edit: TextEdit) -> Option<ChatIntent> {
        let profile = match self.active_full_profile() {
            Ok(p) => p,
            Err(key) => return self.note(key),
        };
        if edit == TextEdit::Show {
            return self.show_profile_text(field, &profile);
        }
        let text = match edit {
            TextEdit::Set(t) => Some(t),
            _ => None,
        };
        let (profile_edit, key) = match field {
            ProfileText::System => (
                ProfileEdit {
                    system_message: Some(text.clone().unwrap_or_default()),
                    ..Default::default()
                },
                if text.is_some() {
                    "ui.profile.system_set"
                } else {
                    "ui.profile.system_cleared"
                },
            ),
            ProfileText::Greeting => (
                ProfileEdit {
                    greeting: Some(text.clone()),
                    ..Default::default()
                },
                if text.is_some() {
                    "ui.profile.greeting_set"
                } else {
                    "ui.profile.greeting_cleared"
                },
            ),
        };
        let msg = self.loc.tf(key, &[("name", &profile.name)]);
        self.push_note(&msg);
        Some(ChatIntent::UpdateProfile {
            id: profile.id,
            edit: Box::new(profile_edit),
        })
    }

    /// The bare (`Show`) half of a `/profile` text subcommand: prefill the
    /// current value for editing, or say there is nothing yet — and what sets
    /// one — when the field is empty (an empty prefill would teach nothing).
    fn show_profile_text(&mut self, field: ProfileText, profile: &Profile) -> Option<ChatIntent> {
        let (word, current, empty_key) = match field {
            ProfileText::System => (
                "system",
                profile.default_system_message.clone(),
                "ui.profile.system_empty",
            ),
            ProfileText::Greeting => (
                "greeting",
                profile.greeting.clone().unwrap_or_default(),
                "ui.profile.greeting_empty",
            ),
        };
        if current.is_empty() {
            let msg = self.loc.tf(empty_key, &[("name", &profile.name)]);
            self.push_note(&msg);
        } else {
            self.input.set_text(&format!("/profile {word} {current}"));
            self.mark_input_changed();
        }
        None
    }

    /// `/impersonation list` — the personas, with the active chat's profile's
    /// one marked (the `/profile list` shape).
    fn list_impersonations(&mut self) {
        let personas = self.personas();
        if personas.is_empty() {
            self.note("ui.imp.none");
            return;
        }
        let used = self
            .active_full_profile()
            .ok()
            .and_then(|p| p.impersonation_profile_id);
        let marker = self.palette.glyphs().title_marker;
        let names: Vec<String> = personas
            .iter()
            .map(|(id, name)| {
                if Some(*id) == used {
                    format!("{marker} {name}")
                } else {
                    format!("  {name}")
                }
            })
            .collect();
        let msg = self.loc.tf("ui.imp.list", &[("names", &names.join("\n"))]);
        self.push_note(&msg);
    }

    /// `/impersonation new [name]` — create a persona with an empty system
    /// message (the settings screen's `Ctrl+N`, same default name). Creating
    /// does not link it (fork F4): the note names the two next steps instead.
    fn create_impersonation(&mut self, name: Option<String>) -> Option<ChatIntent> {
        let Some(mut config) = self.snapshot_config() else {
            return self.note("ui.cmd.settings_pending");
        };
        let name = name.unwrap_or_else(|| self.loc.t("ui.settings.new_profile_name").to_string());
        config
            .impersonation_profiles
            .push(crate::shared::config::ImpersonationProfile::new(
                name.clone(),
                String::new(),
            ));
        let msg = self.loc.tf("ui.imp.created", &[("name", &name)]);
        self.push_note(&msg);
        Some(ChatIntent::UpdateConfig(Box::new(config)))
    }

    /// `/impersonation delete <name>` — resolve the name, then **always** ask
    /// (the `/profile delete` rule: a typed prefix can resolve to a persona the
    /// user did not picture, and its system message is unrecoverable).
    fn confirm_delete_impersonation(&mut self, wanted: &str) -> Option<ChatIntent> {
        let (id, name) = self.resolve_impersonation(wanted)?;
        self.confirm = Some(ConfirmAction::DeleteImpersonation { id, name });
        None
    }

    /// The confirmed half of `/impersonation delete`: build the config without
    /// the persona from the **current** snapshot (it may have refreshed while
    /// the popup was open). Referencing profiles are left alone — a dangling
    /// id reads as "not set" (spec §11.8), which the popup's question said.
    pub(super) fn delete_impersonation(&mut self, id: Uuid, name: &str) -> Option<ChatIntent> {
        let Some(mut config) = self.snapshot_config() else {
            return self.note("ui.cmd.settings_pending");
        };
        let before = config.impersonation_profiles.len();
        config.impersonation_profiles.retain(|p| p.id != id);
        if config.impersonation_profiles.len() == before {
            // Gone between the question and the answer — nothing to delete.
            return self.note("ui.imp.none");
        }
        let msg = self.loc.tf("ui.imp.deleted", &[("name", name)]);
        self.push_note(&msg);
        Some(ChatIntent::UpdateConfig(Box::new(config)))
    }

    /// `/impersonation use <name>` — link the persona to the active chat's
    /// profile, through the same `UpdateProfile` the settings field commits.
    /// Applies live: the next impersonation resolves the link at `Ctrl+U` time.
    fn link_impersonation(&mut self, wanted: &str) -> Option<ChatIntent> {
        let profile = match self.active_full_profile() {
            Ok(p) => p,
            Err(key) => return self.note(key),
        };
        let (persona_id, persona_name) = self.resolve_impersonation(wanted)?;
        let msg = self.loc.tf(
            "ui.imp.linked",
            &[("profile", &profile.name), ("name", &persona_name)],
        );
        self.push_note(&msg);
        Some(ChatIntent::UpdateProfile {
            id: profile.id,
            edit: Box::new(ProfileEdit {
                impersonation_profile_id: Some(Some(persona_id)),
                ..Default::default()
            }),
        })
    }

    /// `/impersonation use default` — unlink: impersonation falls back to the
    /// shared default text (the settings choice's "not set" option).
    fn unlink_impersonation(&mut self) -> Option<ChatIntent> {
        let profile = match self.active_full_profile() {
            Ok(p) => p,
            Err(key) => return self.note(key),
        };
        let msg = self
            .loc
            .tf("ui.imp.unlinked", &[("profile", &profile.name)]);
        self.push_note(&msg);
        Some(ChatIntent::UpdateProfile {
            id: profile.id,
            edit: Box::new(ProfileEdit {
                impersonation_profile_id: Some(None),
                ..Default::default()
            }),
        })
    }

    /// `/impersonation system [text|clear]` — the system message of the persona
    /// the active profile is linked to, resolved the way impersonation itself
    /// resolves it. No link (or a dangling one) answers with the two routes
    /// that create one; an edit applies to the **next** impersonation
    /// everywhere, unlike the profile texts, and the notes reflect that.
    fn impersonation_text_command(&mut self, edit: TextEdit) -> Option<ChatIntent> {
        let profile = match self.active_full_profile() {
            Ok(p) => p,
            Err(key) => return self.note(key),
        };
        let persona = profile.impersonation_profile_id.and_then(|id| {
            self.settings_snapshot.as_ref().and_then(|(config, ..)| {
                config
                    .impersonation_profiles
                    .iter()
                    .find(|p| p.id == id)
                    .cloned()
            })
        });
        let Some(persona) = persona else {
            let msg = self
                .loc
                .tf("ui.imp.not_linked", &[("profile", &profile.name)]);
            self.push_note(&msg);
            return None;
        };
        match edit {
            TextEdit::Show => {
                if persona.system_message.is_empty() {
                    let msg = self
                        .loc
                        .tf("ui.imp.system_empty", &[("name", &persona.name)]);
                    self.push_note(&msg);
                } else {
                    self.input
                        .set_text(&format!("/impersonation system {}", persona.system_message));
                    self.mark_input_changed();
                }
                None
            }
            TextEdit::Clear | TextEdit::Set(_) => {
                let text = match edit {
                    TextEdit::Set(t) => t,
                    _ => String::new(),
                };
                let key = if text.is_empty() {
                    "ui.imp.system_cleared"
                } else {
                    "ui.imp.system_set"
                };
                let Some(mut config) = self.snapshot_config() else {
                    return self.note("ui.cmd.settings_pending");
                };
                let Some(p) = config
                    .impersonation_profiles
                    .iter_mut()
                    .find(|p| p.id == persona.id)
                else {
                    return self.note("ui.cmd.settings_pending");
                };
                p.system_message = text;
                let msg = self.loc.tf(key, &[("name", &persona.name)]);
                self.push_note(&msg);
                Some(ChatIntent::UpdateConfig(Box::new(config)))
            }
        }
    }

    /// Resolves a persona by name — [`resolve_named`], the profile resolver's
    /// rule, over the snapshot's personas. An empty list answers with the
    /// route that creates one instead of an empty "there is:" enumeration.
    fn resolve_impersonation(&mut self, wanted: &str) -> Option<(Uuid, String)> {
        let personas = self.personas();
        if personas.is_empty() {
            self.note("ui.imp.none");
            return None;
        }
        match resolve_named(&personas, wanted) {
            Ok(hit) => Some(hit),
            Err(hits) => {
                let all = personas.into_iter().map(|(_, name)| name).collect();
                self.report_unresolved(
                    wanted,
                    hits,
                    all,
                    ("ui.imp.no_persona", "ui.imp.many_personas"),
                    "ui.cmd.route_imp_list",
                );
                None
            }
        }
    }

    /// A localized note with no arguments, and no intent — the shape most
    /// refusals take here.
    fn note(&mut self, key: &'static str) -> Option<ChatIntent> {
        self.push_note(self.loc.t(key));
        None
    }

    /// "Not while a reply is being generated" — naming the command that was
    /// typed and the two ways to stop the turn, so the refusal ends with a route.
    fn busy_note(&mut self, alias: &'static str) -> Option<ChatIntent> {
        let msg = self.loc.tf("ui.cmd.generating", &[("cmd", alias)]);
        self.push_note(&msg);
        None
    }
}