newt-tui 0.8.0

Newt-Agent TUI surfaces (ratatui): code mode + pilot mode
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
//! `/prompt` · `/vi` · `/emacs` · `/nano` · `/edit-mode` · `/thinking` · `/nudge` ·
//! `/psyche` — the session-setting commands. The editor-family ones set an
//! `NEWT_*` env var a later step in `run_chat` picks up; `/psyche` owns every
//! effort dial (#1665): bare `/psyche` opens the panel (intercepted in
//! `chat.rs` on a rich TTY; here it renders the text status view for the
//! piped/lean path), and `/psyche cognition|tenacity|initiative <level>` are the text
//! setters that mutate the process-globals. The retired top-level
//! `/cognition` + `/tenacity` print a redirect and mutate NOTHING. Moved from
//! the `dispatch_slash` match in `lib.rs`.

use newt_core::agentic::print_newt;

use crate::settings_form::{self, Field};
use crate::{current_prompt_and_preview, prompt_token_help, strip_one_quote_pair};

/// Perform one setting change through the single mutation path (#1981) and
/// return the line it produced.
///
/// Every absorbed verb in this module goes through here, and there is no arm
/// that skips it. `Ok` and `Err` collapse to one string because a caller has
/// nothing different to do with a refusal — the message already says what the
/// field accepts.
fn apply_setting(field: Field, value: &str, via: &str) -> String {
    match settings_form::apply_and_record(field, value, via) {
        Ok(msg) | Err(msg) => msg,
    }
}

/// Handle the session-setting command family. Always returns `Ok(true)` (none
/// of these end the session).
pub(crate) fn dispatch(
    cmd: &str,
    arg1: &str,
    input: &str,
    workspace: &str,
    color: bool,
    verbose: bool,
) -> anyhow::Result<bool> {
    match cmd {
        "prompt" if arg1 == "set" => {
            // Everything after "prompt set " is the literal template — taken
            // from the RAW input so internal/trailing spaces survive, with one
            // layer of surrounding quotes stripped. Applies for the session
            // (via NEWT_PROMPT, which the per-turn prompt build reads first);
            // put it in `[tui] prompt` to persist.
            let template = input
                .trim_start_matches('/')
                .strip_prefix("prompt")
                .and_then(|s| s.trim_start().strip_prefix("set"))
                .map(|s| s.strip_prefix(' ').unwrap_or(s))
                .map(strip_one_quote_pair)
                .unwrap_or("");
            if template.is_empty() {
                print_newt(
                    "usage: /prompt set \"<template>\"  (try /prompt for the token list)",
                    color,
                    verbose,
                );
            } else {
                // #2009 PR5: ABSORBED into `/settings prompt`. The verb keeps
                // its prose — a rendered preview says more than a template
                // echoed back — but no longer owns a write. This arm used to
                // call `set_var` itself, which is exactly the "one mutation
                // path is aspirational rather than true" the `/vi` arm below
                // warns about, and it is why the setting had no receipt.
                apply_setting(Field::Prompt, template, "/prompt set");
                let (_t, preview) = current_prompt_and_preview(workspace);
                print_newt(
                    &format!("prompt set for this session — preview: {preview}"),
                    color,
                    verbose,
                );
                print_newt(
                    "(add to [tui] prompt to persist — use $NAME macros there to avoid TOML escaping)",
                    color,
                    verbose,
                );
            }
        }

        "prompt" if matches!(arg1, "reset" | "default" | "clear") => {
            // Through the same writer, so the release is recorded too. `reset`
            // is a VALUE of the field, not a second act.
            apply_setting(Field::Prompt, "reset", "/prompt reset");
            print_newt(
                "prompt reset to your [tui] prompt / the built-in default.",
                color,
                verbose,
            );
        }

        "prompt" => {
            print_newt(
                "Prompt tokens — `/prompt set \"<template>\"` to change, or `[tui] prompt` to persist:",
                color,
                verbose,
            );
            for line in prompt_token_help() {
                println!("{line}");
            }
            print_newt(
                "In config.toml prefer the $NAME macros — the \\x forms are eaten by TOML \
                 (use a 'literal string' or doubled \\\\).",
                color,
                verbose,
            );
            let (tmpl, preview) = current_prompt_and_preview(workspace);
            print_newt(&format!("current: {tmpl:?}"), color, verbose);
            print_newt(&format!("preview: {preview}"), color, verbose);
        }

        "vi" | "emacs" | "nano" | "edit-mode" => {
            // Switch the line-editor key bindings for the rest of the session.
            // Sets NEWT_EDIT_MODE; the editor rebuild + the is_vi/caret recompute
            // back in `run_chat` (after every slash command) pick it up.
            let want = match cmd {
                "vi" => Some("vi"),
                "emacs" => Some("emacs"),
                "nano" => Some("nano"),
                _ => match arg1.to_lowercase().as_str() {
                    "vi" | "vim" => Some("vi"),
                    "emacs" => Some("emacs"),
                    "nano" => Some("nano"),
                    _ => None,
                },
            };
            // #1981: ABSORBED into `/settings edit-mode`. These verbs still
            // work — operators have muscle memory, and a removed command that
            // says "unknown" is worse than the four verbs were — but they no
            // longer own a mutation of their own. Everything routes through
            // `settings_form::apply`, which is the single site a provenance
            // receipt can attach to (#1965). Leaving the old `set_var` here
            // would make "one mutation path" aspirational rather than true.
            match want {
                Some(m) => {
                    print_newt(
                        &apply_setting(Field::EditMode, m, &format!("/{cmd}")),
                        color,
                        verbose,
                    );
                    print_newt(
                        &settings_form::moved_notice(cmd, Field::EditMode),
                        color,
                        verbose,
                    );
                }
                None => print_newt(
                    "usage: /settings edit-mode <vi|emacs|nano>  (or just /vi, /emacs, /nano)",
                    color,
                    verbose,
                ),
            }
        }

        // #1981: ABSORBED into `/settings nudge`. The verb keeps its prose —
        // "nudges OFF for this session" says more than "action-pressure
        // nudges: off" — but no longer owns a write: `settings_form::apply`
        // performs every one.
        "nudge" => match arg1 {
            "off" => {
                apply_setting(Field::Nudge, "off", "/nudge");
                print_newt(
                    "action-pressure nudges OFF for this session (narration rescue, \
                     workflow repair steering, plan pushes) — factual corrections stay on",
                    color,
                    verbose,
                );
            }
            "on" => {
                apply_setting(Field::Nudge, "on", "/nudge");
                print_newt("action-pressure nudges ON (default)", color, verbose);
            }
            "" | "status" => {
                print_newt(
                    &format!(
                        "action-pressure nudges: {}  (/settings nudge <on|off>)",
                        if settings_form::nudges_off() {
                            "OFF"
                        } else {
                            "on"
                        }
                    ),
                    color,
                    verbose,
                );
            }
            _ => print_newt(
                "usage: /settings nudge <on|off>  (/nudge status shows it)",
                color,
                verbose,
            ),
        },

        // #1665: retired top-levels. Redirect WITHOUT mutating — a habitual
        // `/tenacity relentless` must not half-work through a deprecation shim,
        // or the shim never gets to die.
        "tenacity" | "cognition" => print_newt(&retired_dial_redirect(cmd), color, verbose),

        // #2044: retired the same way, and for the same reason — one door per
        // setting. The reasoning display is a `/settings` field like any
        // other, and a second verb that only ever forwarded to it was a second
        // place for the vocabulary to drift. It already had: this arm matched
        // a literal `"on" | "off"` while the field had grown `fold | stream |
        // off`, so `/thinking fold` was refused for a value the panel took.
        //
        // Redirects WITHOUT mutating, per the retirement discipline above: a
        // habitual `/thinking fold` must not half-work through a shim, or the
        // shim never gets to die.
        "thinking" => print_newt(
            &format!(
                "/thinking folded into /settings — /settings opens the form; \
                 text: /settings thinking <{}>  (nothing changed)",
                Field::Thinking.accepts_hint()
            ),
            color,
            verbose,
        ),

        // /psyche owns the dials (#1665): bare = panel (intercepted in chat.rs
        // on a rich TTY; HERE bare renders the text status for piped/lean),
        // `status` = text view, `cognition`/`tenacity`/`initiative` = text setters,
        // `obsessive` = max the live dials. Subcommand args live past arg1, so
        // re-derive the full remainder from the raw input.
        "psyche" => {
            let rest = input
                .trim_start()
                .trim_start_matches('/')
                .strip_prefix("psyche")
                .unwrap_or("")
                .trim();
            print_newt(&psyche_command(rest), color, verbose);
        }

        other => {
            unreachable!("commands::settings::dispatch routed a non-setting command: {other:?}")
        }
    }
    Ok(true)
}

/// The round-budget side effect is intentionally keyed to the raw operator
/// override, not an automatic persona/config/model-family resolution.
fn explicit_relentless_round_note() -> Option<&'static str> {
    matches!(
        newt_core::tenacity::cli_tenacity(),
        Some(newt_core::Tenacity::Relentless)
    )
    .then_some(
        "explicit relentless tenacity makes the initial tool-round allowance effectively unlimited; an explicit `/rounds` overrides the initial value; progress renewal still applies",
    )
}

/// The `<auto|level…>` usage fragment for a dial, built from its `all()` so a
/// level added to the enum cannot be missing from the help.
fn level_usage<'a>(labels: impl IntoIterator<Item = &'a str>) -> String {
    std::iter::once("auto")
        .chain(labels)
        .collect::<Vec<_>>()
        .join("|")
}

/// Build the `/psyche tenacity` response and, when `arg` names a level,
/// install it as the explicit override (tenacity's only input). Pure for the
/// show/list/error paths; the set path mutates the process-global via
/// `set_cli_tenacity`.
fn tenacity_command(arg: &str) -> String {
    use newt_core::tenacity::{effective_tenacity, Tenacity};
    let usage = format!(
        "(/psyche tenacity <{}>|list)",
        level_usage(Tenacity::all().map(Tenacity::label))
    );
    match arg.trim() {
        "" | "status" | "show" => {
            let t = effective_tenacity();
            let rounds = explicit_relentless_round_note()
                .map(|note| format!("  {note}."))
                .unwrap_or_default();
            format!(
                "tenacity: {} — {}{rounds}  {usage}",
                t.label(),
                t.describe()
            )
        }
        "list" => {
            let mut out = String::from("tenacity levels (normal → relentless):");
            out.push_str("\n  auto       normal, unless set explicitly");
            // Snapshot the active level ONCE, so a concurrent override change
            // cannot slip the "← active" marker off every row.
            let active_level = effective_tenacity();
            for t in Tenacity::all() {
                let active = if t == active_level { " ← active" } else { "" };
                out.push_str(&format!("\n  {:<10} {}{active}", t.label(), t.describe()));
            }
            if let Some(note) = explicit_relentless_round_note() {
                out.push_str(&format!("\n  rounds     {note}."));
            }
            out
        }
        // review-2 #2: clear the override so tenacity is `normal` again.
        "auto" | "inherit" | "reset" => {
            // #1981: the clear AND the #1668 unpin both live in
            // `settings_form::apply` now — releasing the dial is an operator
            // action, and an action that leaves no record is the #1965 defect.
            apply_setting(Field::Tenacity, "auto", "/psyche tenacity");
            format!(
                "tenacity → auto (override cleared) — now {}",
                effective_tenacity().label()
            )
        }
        other => match other.parse::<Tenacity>() {
            Ok(level) => {
                // Only a PARSED level reaches the mutation path — the error
                // arm below mutates nothing and records nothing.
                apply_setting(Field::Tenacity, level.label(), "/psyche tenacity");
                let rounds = if level == Tenacity::Relentless {
                    "; initial tool-round allowance → effectively unlimited (an explicit `/rounds` overrides the initial value; progress renewal still applies)"
                } else {
                    ""
                };
                format!(
                    "tenacity → {} — {}{rounds}",
                    level.label(),
                    level.describe()
                )
            }
            Err(e) => format!("{e}  {usage}"),
        },
    }
}

/// Build the `/psyche initiative` response and, when `arg` names a level,
/// install it as the explicit override (the highest-priority input in
/// [`newt_core::initiative::effective_initiative`]). Pure for the
/// show/list/error paths.
fn initiative_command(arg: &str) -> String {
    use newt_core::initiative::{effective_initiative, Initiative};
    let usage = format!(
        "(/psyche initiative <{}>|list)",
        level_usage(Initiative::all().map(Initiative::label))
    );
    match arg.trim() {
        "" | "status" | "show" => {
            let i = effective_initiative();
            format!("initiative: {} — {}  {usage}", i.label(), i.describe())
        }
        "list" => {
            let mut out = String::from("initiative levels (patient → eager):");
            out.push_str("\n  auto       inherit from the persona / config / model family");
            let active_level = effective_initiative();
            for i in Initiative::all() {
                let active = if i == active_level { " ← active" } else { "" };
                out.push_str(&format!("\n  {:<10} {}{active}", i.label(), i.describe()));
            }
            out
        }
        "auto" | "inherit" | "reset" => {
            apply_setting(Field::Initiative, "auto", "/psyche initiative");
            format!(
                "initiative → auto (override cleared) — now {} (from persona / config / model family)",
                effective_initiative().label()
            )
        }
        other => match other.parse::<Initiative>() {
            Ok(level) => {
                apply_setting(Field::Initiative, level.label(), "/psyche initiative");
                format!("initiative → {} — {}", level.label(), level.describe())
            }
            Err(e) => format!("{e}  {usage}"),
        },
    }
}

/// Build the `/cognition` response and, when `arg` names a level (or `off`/`auto`),
/// install the session override — the highest-priority input in
/// [`newt_core::cognition::resolve_cognition`], layered over the active persona's
/// `cognition:`. Pure for the show/list/error paths; the set paths mutate the
/// process-global via `set_cli_cognition`.
fn cognition_command(arg: &str) -> String {
    use newt_core::cognition::{cli_cognition, CognitionOverride};
    use newt_core::role_profile::Cognition;
    let levels: Vec<&str> = Cognition::all().into_iter().map(Cognition::label).collect();
    let usage = format!("(/psyche cognition <{}>|off|auto|list)", levels.join("|"));
    match arg.trim() {
        "" | "status" | "show" => {
            match cli_cognition() {
                CognitionOverride::Unset => {
                    format!("cognition: auto — follows the active persona's `cognition:` (or off)  {usage}")
                }
                CognitionOverride::Off => {
                    format!("cognition: off — no reasoning controls sent, overriding any persona  {usage}")
                }
                CognitionOverride::Set(c) => format!(
                    "cognition: {} — {}  (session override, beats the persona)  {usage}",
                    c.label(),
                    c.describe()
                ),
            }
        }
        "list" => {
            let active = cli_cognition();
            let mut out = String::from("cognition levels (light → deep):");
            for c in Cognition::all() {
                let mark = if matches!(active, CognitionOverride::Set(a) if a == c) {
                    " ← override"
                } else {
                    ""
                };
                out.push_str(&format!("\n  {:<14} {}{mark}", c.label(), c.describe()));
            }
            out.push_str("\n  off            no reasoning controls (overrides the persona)");
            out.push_str("\n  auto           follow the active persona (default)");
            out
        }
        // #1981: all three set paths go through `settings_form::apply`, which
        // owns the write AND the #1668 posture mark — including `auto`, which
        // UNPINS the axis and is just as much an operator action.
        "off" | "none" => {
            apply_setting(Field::Cognition, "off", "/psyche cognition");
            "cognition → off — no reasoning controls will be sent".to_string()
        }
        "auto" | "reset" | "persona" => {
            apply_setting(Field::Cognition, "auto", "/psyche cognition");
            "cognition → auto — following the active persona".to_string()
        }
        other => match other.parse::<Cognition>() {
            Ok(level) => {
                apply_setting(Field::Cognition, level.label(), "/psyche cognition");
                format!("cognition → {} — {}", level.label(), level.describe())
            }
            Err(e) => format!("{e}  {usage}"),
        },
    }
}

/// The retired top-level dials' redirect line (#1665) — pure so the exact
/// wording is pinned by test: it must name the panel AND the text-setter form,
/// and say plainly that nothing changed.
fn retired_dial_redirect(cmd: &str) -> String {
    format!(
        "/{cmd} folded into /psyche — /psyche opens the dial panel; \
         text: /settings {cmd} <level>  (nothing changed)"
    )
}

/// `/psyche` — every effort dial under one command (#1665). `rest` is the raw
/// remainder after "psyche": empty / `status` renders the text posture view
/// (bare `/psyche` only reaches here on the piped/lean path — a rich TTY
/// intercepts it in `chat.rs` and opens the panel), `cognition …` /
/// `tenacity …` / `initiative …` delegate to the text setters, `obsessive`
/// engages the max-effort posture, and `edit` points at the panel's TTY
/// requirement.
fn psyche_command(rest: &str) -> String {
    use newt_core::cognition::{cli_cognition, CognitionOverride};
    use newt_core::initiative::effective_initiative;
    use newt_core::tenacity::effective_tenacity;
    let rest = rest.trim();
    let (sub, arg) = match rest.split_once(char::is_whitespace) {
        Some((s, a)) => (s, a.trim()),
        None => (rest, ""),
    };
    // `/psyche obsessive` — engage the max-effort posture's two dials.
    // Initiative is left alone, by design: the deepest thinking and acting
    // after one round pull against each other.
    // Crew is a startup gate (crew_runner is built once at launch), so it can't
    // be turned on mid-session; say so honestly and point at the launch flag.
    if sub.eq_ignore_ascii_case("obsessive") || sub.eq_ignore_ascii_case("obsessive-relentless") {
        // #1981: the posture still means what `psyche` says it means — the
        // constants are that module's to own — but engaging it is two ordinary
        // setting changes, so it takes the same path, the same #1668 pins and
        // the same record as `/settings cognition meticulous` would.
        let (cog, ten) = (
            newt_core::psyche::OBSESSIVE_COGNITION,
            newt_core::psyche::OBSESSIVE_TENACITY,
        );
        apply_setting(Field::Cognition, cog.label(), "/psyche obsessive");
        apply_setting(Field::Tenacity, ten.label(), "/psyche obsessive");
        return format!(
            "obsessive engaged (live): cognition → {}, tenacity → {}, \
             initial tool-round allowance → effectively unlimited (an explicit \
             `/rounds` overrides the initial value; progress renewal still applies); initiative stays {}.\n\
             crew is a launch gate — relaunch with `newt --obsessive` (or set \
             NEWT_TEAM) to add the crew this session.",
            cog.label(),
            ten.label(),
            effective_initiative().label()
        );
    }
    match sub {
        "cognition" => return cognition_command(arg),
        "tenacity" => return tenacity_command(arg),
        "initiative" => return initiative_command(arg),
        // Reached only where chat.rs did NOT open the panel (piped / lean).
        // Reached only when chat.rs did NOT open the panel: a lean build, a
        // piped session, or a malformed alias (extra arguments) — so the
        // message must never assert the terminal is incapable (review v2:
        // `/psyche edit extra` on a rich TTY landed here and was told its
        // interactive terminal wasn't one).
        "edit" => {
            return if arg.is_empty() {
                "/psyche opens the dial panel on the rich TUI with an \
                 interactive terminal; here, /psyche status shows the text \
                 view and /psyche cognition / tenacity / initiative <level> \
                 change the dials."
                    .to_string()
            } else {
                format!(
                    "usage: /psyche edit takes no arguments (got `{arg}`) — \
                     bare /psyche opens the dial panel"
                )
            }
        }
        "" | "status" | "show" => {}
        other => {
            return format!(
                "unknown /psyche subcommand `{other}` — usage: /psyche \
                 [status|cognition <level>|tenacity <level>|initiative <level>|obsessive]"
            )
        }
    }
    // Show the EFFECTIVE cognition + where it resolves from (review-2 #6): a
    // status view, not just an override inspector.
    let cog = match cli_cognition() {
        CognitionOverride::Off => "off — no reasoning controls (session override)".to_string(),
        CognitionOverride::Set(c) => {
            format!("{} — {} (session override)", c.label(), c.describe())
        }
        CognitionOverride::Unset => match newt_core::cognition::persona_cognition() {
            Some(c) => format!("{} — {} (from the active persona)", c.label(), c.describe()),
            None => "auto — no reasoning controls (no persona sets it)".to_string(),
        },
    };
    let ten = effective_tenacity();
    let init = effective_initiative();
    let init_from = if newt_core::initiative::cli_initiative().is_some() {
        "session override"
    } else if newt_core::initiative::persona_initiative().is_some() {
        "from the active persona"
    } else if newt_core::initiative::model_default_initiative().is_some() {
        "model default"
    } else {
        "config default"
    };
    // Mirror newt-cli's startup gate: the crew runner is built iff NEWT_TEAM is set.
    let crew = if std::env::var("NEWT_TEAM").is_ok() {
        "on"
    } else {
        "off"
    };
    let mut out = String::from("psyche — how hard the agent works (orthogonal dials):");
    out.push_str(&format!("\n  cognition   {cog}"));
    out.push_str(
        "\n              backend-specific reasoning depth             (/psyche cognition)",
    );
    out.push_str(&format!(
        "\n  tenacity    {} — {}",
        ten.label(),
        ten.describe()
    ));
    out.push_str("\n              how long it pursues the task            (/psyche tenacity)");
    if let Some(note) = explicit_relentless_round_note() {
        out.push_str(&format!("\n              {note}."));
    }
    out.push_str(&format!(
        "\n  initiative  {} — {} ({init_from})",
        init.label(),
        init.describe()
    ));
    out.push_str("\n              how much it looks before acting         (/psyche initiative)");
    out.push_str(&format!("\n  crew        {crew}"));
    out.push_str(
        "\n              how many minds work the task                   (NEWT_TEAM / newt crew)",
    );
    out.push_str(&format!(
        "\nobsessive = cognition {} + tenacity {} + crew on; initiative is left alone.",
        newt_core::psyche::OBSESSIVE_COGNITION.label(),
        newt_core::psyche::OBSESSIVE_TENACITY.label()
    ));
    out.push_str(
        "\n/psyche — open the dial panel (rich TUI build + interactive terminal) · \
         /psyche cognition|tenacity|initiative <level> — text setters.",
    );
    out
}

#[cfg(test)]
mod tests {
    use super::{cognition_command, tenacity_command};
    use crate::settings_form::{Field, ValueSpace};

    /// **Every value the field declares must be reachable through the slash
    /// command.** Not "fold and stream are accepted" — that would be a third
    /// copy of the list, and a fourth value would desync it again.
    ///
    /// This is the defect the test exists for, observed live: the dispatcher
    /// matched a literal `"on" | "off"` while `Field::Thinking` had grown to
    /// `fold | stream | off`. `/thinking fold` answered
    /// `usage: /settings thinking <on|off>` for a value the settings panel
    /// accepted, so the two doors onto one setting disagreed about what the
    /// setting was. The arm now delegates to `Field::accepts`, and this walks
    /// the declared vocabulary so a future value cannot ship half-wired.
    #[test]
    fn every_declared_thinking_value_is_reachable_through_the_slash_command() {
        let ValueSpace::Choice(values) = Field::Thinking.value_space() else {
            panic!("thinking is a fixed vocabulary, not a number");
        };
        assert!(
            values.len() >= 3,
            "the three-way vocabulary is the point: {values:?}"
        );
        for (value, _) in &values {
            assert_eq!(
                Field::Thinking.accepts(value).as_deref(),
                Some(*value),
                "`/thinking {value}` must reach the field it is declared on"
            );
        }
        // The alias predates the three-way vocabulary and still means "show me
        // the reasoning", which is now the default rather than a third thing.
        assert_eq!(Field::Thinking.accepts("on").as_deref(), Some("fold"));
        // …and nonsense is still refused, so delegating did not open the door.
        assert_eq!(Field::Thinking.accepts("maybe"), None);
    }

    /// The no-argument usage line names the REAL vocabulary rather than a
    /// frozen literal, so it cannot go stale the way the match arm did.
    #[test]
    fn thinking_usage_is_derived_from_the_declared_values() {
        let hint = Field::Thinking.accepts_hint();
        for want in ["fold", "stream", "off"] {
            assert!(hint.contains(want), "usage omits `{want}`: {hint}");
        }
        assert!(
            !hint.contains("on|off"),
            "the stale two-value literal is back: {hint}"
        );
    }

    #[test]
    fn cognition_status_list_set_off_auto_and_error_are_informative() {
        use newt_core::cognition::{cli_cognition, set_cli_cognition, CognitionOverride};
        use newt_core::role_profile::Cognition;
        let _g = newt_core::test_guard::GlobalSettingsGuard::acquire();
        // Start clean; the guard restores the process-global on drop.
        let restore = cli_cognition();
        set_cli_cognition(CognitionOverride::Unset);

        // Status (unset) explains it follows the persona and shows usage —
        // pointing at the /psyche subcommand form, not the retired top-level.
        let status = cognition_command("");
        assert!(status.starts_with("cognition: auto"), "{status}");
        assert!(status.contains("/psyche cognition"), "{status}");
        // List enumerates every level plus off/auto.
        let list = cognition_command("list");
        for label in ["zen", "rational", "thoughtful", "meticulous", "off", "auto"] {
            assert!(list.contains(label), "list missing {label}: {list}");
        }
        // Setting a level installs the override live.
        let msg = cognition_command("meticulous");
        assert!(msg.contains("meticulous"), "{msg}");
        assert_eq!(
            cli_cognition(),
            CognitionOverride::Set(Cognition::Meticulous)
        );
        // `off` forces the off state; `auto` returns to following the persona.
        cognition_command("off");
        assert_eq!(cli_cognition(), CognitionOverride::Off);
        cognition_command("auto");
        assert_eq!(cli_cognition(), CognitionOverride::Unset);
        // An unknown level explains itself rather than silently doing nothing.
        let err = cognition_command("banana");
        assert!(err.contains("unknown cognition"), "{err}");

        set_cli_cognition(restore);
    }

    #[test]
    fn psyche_panel_shows_all_the_dials_and_how_to_change_them() {
        let out = super::psyche_command("");
        for k in ["cognition", "tenacity", "initiative", "crew", "obsessive"] {
            assert!(out.contains(k), "psyche panel missing '{k}': {out}");
        }
        assert!(
            out.contains("/psyche cognition")
                && out.contains("/psyche tenacity")
                && out.contains("/psyche initiative"),
            "status view points at the /psyche subcommand setters: {out}"
        );
        assert!(
            !out.contains("/psyche edit"),
            "bare /psyche IS the panel now — the footer must not advertise \
             the old edit subcommand: {out}"
        );
    }

    #[test]
    fn psyche_obsessive_engages_the_max_live_dials_and_notes_crew() {
        use newt_core::cognition::{cli_cognition, set_cli_cognition, CognitionOverride};
        use newt_core::initiative::{cli_initiative, set_cli_initiative, Initiative};
        use newt_core::role_profile::Cognition;
        use newt_core::tenacity::{effective_tenacity, set_cli_tenacity, Tenacity};
        let _g = newt_core::test_guard::GlobalSettingsGuard::acquire();
        // Reset to a non-obsessive baseline so the assertions are meaningful.
        set_cli_cognition(CognitionOverride::Unset);
        set_cli_tenacity(Tenacity::Normal);
        set_cli_initiative(Initiative::Patient);

        let out = super::psyche_command("obsessive");
        // The two dials it owns are actually set.
        assert_eq!(
            cli_cognition(),
            CognitionOverride::Set(Cognition::Meticulous)
        );
        assert_eq!(effective_tenacity(), Tenacity::Relentless);
        // Slice 1b: initiative is left exactly where it was, and the message
        // says so rather than implying the posture still forces edits.
        assert_eq!(cli_initiative(), Some(Initiative::Patient));
        assert!(out.contains("initiative stays patient"), "{out}");
        // The message is honest about crew being a launch gate.
        assert!(out.to_lowercase().contains("crew"), "{out}");
        assert!(out.contains("--obsessive"), "{out}");
        assert!(out.contains("round"), "{out}");
        assert!(out.contains("/rounds"), "{out}");
    }

    #[test]
    fn tenacity_status_and_list_and_error_are_informative() {
        use newt_core::tenacity::{set_cli_tenacity, Tenacity};
        // Hold the global-settings lock and pin a known level: sibling tests
        // mutate the process-global tenacity override, and `list` marks whichever
        // level is *active*. Without the guard this read raced the mutators and
        // the "← active" marker intermittently vanished once the hosted runners
        // upped test parallelism (the CPU-capped self-hosted pods never hit it).
        let _g = newt_core::test_guard::GlobalSettingsGuard::acquire();
        set_cli_tenacity(Tenacity::Normal);
        // Status names the active level and the usage hint (no mutation).
        let status = tenacity_command("");
        assert!(status.starts_with("tenacity: "), "{status}");
        assert!(
            status.contains("/psyche tenacity <auto|normal|relentless>"),
            "{status}"
        );
        // List enumerates every level, marking the active one.
        let list = tenacity_command("list");
        for label in ["normal", "relentless"] {
            assert!(list.contains(label), "list missing {label}: {list}");
        }
        assert!(
            list.contains("← active"),
            "list marks the active level: {list}"
        );
        // An unknown level explains itself rather than silently doing nothing.
        let err = tenacity_command("banana");
        assert!(err.contains("unknown tenacity"), "{err}");
        // A pre-split level names the initiative that replaced it.
        let err = tenacity_command("insistent");
        assert!(err.contains("--initiative decisive"), "{err}");
    }

    #[test]
    fn initiative_status_and_list_and_error_are_informative() {
        use newt_core::initiative::{set_cli_initiative, Initiative};
        let _g = newt_core::test_guard::GlobalSettingsGuard::acquire();
        newt_core::initiative::set_initiative_config(Default::default());
        set_cli_initiative(Initiative::Measured);
        let status = super::initiative_command("");
        assert!(status.starts_with("initiative: measured"), "{status}");
        assert!(
            status.contains("/psyche initiative <auto|patient|measured|decisive|eager>"),
            "{status}"
        );
        let list = super::initiative_command("list");
        for label in ["patient", "measured", "decisive", "eager", "auto"] {
            assert!(list.contains(label), "list missing {label}: {list}");
        }
        assert!(list.contains("after 12 read-only"), "rounds shown: {list}");
        assert!(list.contains("← active"), "{list}");
        let err = super::initiative_command("banana");
        assert!(err.contains("unknown initiative"), "{err}");
    }

    #[test]
    fn explicit_relentless_status_views_explain_the_round_budget() {
        use newt_core::tenacity::{set_cli_tenacity, Tenacity};

        let _g = newt_core::test_guard::GlobalSettingsGuard::acquire();
        set_cli_tenacity(Tenacity::Relentless);

        for out in [
            tenacity_command("status"),
            tenacity_command("list"),
            super::psyche_command("status"),
        ] {
            assert!(out.contains("effectively unlimited"), "{out}");
            assert!(out.contains("/rounds"), "{out}");
        }
    }

    #[test]
    fn psyche_subcommands_route_to_the_dial_setters() {
        // #1665: /psyche cognition|tenacity <level> are the text setters — the
        // same functions the retired top-levels used, reached through /psyche.
        use newt_core::cognition::{cli_cognition, set_cli_cognition, CognitionOverride};
        use newt_core::initiative::{effective_initiative, Initiative};
        use newt_core::role_profile::Cognition;
        use newt_core::tenacity::{effective_tenacity, set_cli_tenacity, Tenacity};
        let _g = newt_core::test_guard::GlobalSettingsGuard::acquire();
        set_cli_cognition(CognitionOverride::Unset);
        set_cli_tenacity(Tenacity::Normal);

        let msg = super::psyche_command("cognition thoughtful");
        assert!(msg.contains("thoughtful"), "{msg}");
        assert_eq!(
            cli_cognition(),
            CognitionOverride::Set(Cognition::Thoughtful)
        );
        let msg = super::psyche_command("tenacity relentless");
        assert!(msg.contains("relentless"), "{msg}");
        assert_eq!(effective_tenacity(), Tenacity::Relentless);
        let msg = super::psyche_command("initiative eager");
        assert!(msg.contains("eager"), "{msg}");
        assert_eq!(effective_initiative(), Initiative::Eager);
        // Bare subcommand = that dial's status view, no mutation.
        let status = super::psyche_command("cognition");
        assert!(status.starts_with("cognition:"), "{status}");
        // `status`/`show`/empty render the posture view; `edit` explains the
        // TTY requirement (only reached where chat.rs did not open the panel).
        for rest in ["", "status", "show"] {
            let out = super::psyche_command(rest);
            assert!(out.contains("psyche — how hard"), "{rest:?}: {out}");
        }
        let edit = super::psyche_command("edit");
        assert!(edit.contains("dial panel"), "{edit}");
        assert!(
            !edit.contains("needs an interactive rich terminal"),
            "review v2: the arm must not assert the terminal is incapable \
             (it also fires for malformed aliases ON a rich TTY): {edit}"
        );
        // Extra arguments are a usage error, not a lecture about terminals.
        let extra = super::psyche_command("edit extra");
        assert!(extra.contains("takes no arguments"), "{extra}");
        // An unknown subcommand explains itself.
        let err = super::psyche_command("banana");
        assert!(err.contains("unknown /psyche subcommand"), "{err}");
    }

    /// #1668: the dial setters mark a posture ACTION on exactly the axis they
    /// set — and the read-only / refused forms mark nothing, which is what
    /// keeps merely LOOKING at the dials out of a conversation's pin. Driven
    /// through the real `/psyche` seam, not a hand-copy of the mark calls.
    #[test]
    fn psyche_setters_mark_exactly_the_axis_they_change() {
        use newt_core::cognition::CognitionOverride;
        use newt_core::initiative::Initiative;
        use newt_core::role_profile::Cognition;
        use newt_core::runtime::drain_preference_actions;
        use newt_core::tenacity::Tenacity;
        let _g = newt_core::test_guard::GlobalSettingsGuard::acquire();
        let _ = drain_preference_actions();

        // Status / list / unknown / error forms: read-only, so no action.
        for rest in [
            "",
            "status",
            "cognition",
            "cognition list",
            "cognition transcending",
            "tenacity",
            "tenacity list",
            "tenacity nonsense",
            "initiative",
            "initiative list",
            "initiative relaxed",
            "banana",
            "edit",
        ] {
            let _ = super::psyche_command(rest);
            assert!(
                drain_preference_actions().is_empty(),
                "`/psyche {rest}` must not pin anything"
            );
        }

        // A level sets exactly its own axis.
        let _ = super::psyche_command("cognition thoughtful");
        let a = drain_preference_actions();
        assert_eq!(
            a.cognition,
            Some(CognitionOverride::Set(Cognition::Thoughtful))
        );
        assert_eq!((a.tenacity, a.backend, a.model), (None, None, None));

        let _ = super::psyche_command("tenacity relentless");
        let a = drain_preference_actions();
        assert_eq!(a.tenacity, Some(Some(Tenacity::Relentless)));
        assert_eq!(a.cognition, None);

        let _ = super::psyche_command("initiative decisive");
        let a = drain_preference_actions();
        assert_eq!(a.initiative, Some(Some(Initiative::Decisive)));
        assert_eq!((a.cognition, a.tenacity), (None, None));

        // `off` and `auto` are actions too — `auto` UNPINS the axis.
        let _ = super::psyche_command("cognition off");
        assert_eq!(
            drain_preference_actions().cognition,
            Some(CognitionOverride::Off)
        );
        let _ = super::psyche_command("cognition auto");
        assert_eq!(
            drain_preference_actions().cognition,
            Some(CognitionOverride::Unset)
        );
        let _ = super::psyche_command("tenacity auto");
        assert_eq!(drain_preference_actions().tenacity, Some(None));

        let _ = super::psyche_command("initiative auto");
        assert_eq!(drain_preference_actions().initiative, Some(None));

        // `obsessive` sets its two dials, so it pins both — and never
        // initiative, which it leaves alone.
        let _ = super::psyche_command("obsessive");
        let a = drain_preference_actions();
        assert!(a.cognition.is_some() && a.tenacity.is_some(), "{a:?}");
        assert_eq!(a.initiative, None, "obsessive does not pin initiative");
        assert_eq!((a.backend, a.model), (None, None), "crew/backend untouched");
    }

    #[test]
    fn the_redirect_line_names_both_the_panel_and_the_text_setter() {
        // #1665 review: the redirect wording was previously unasserted anywhere.
        // #1981 moved the SETTER half to `/settings <dial> <level>` — the panel
        // is still `/psyche`, because a chooser performs and a setter sets. Both
        // affordances must still be named, which is what this asserts.
        for cmd in ["tenacity", "cognition"] {
            let line = super::retired_dial_redirect(cmd);
            assert!(line.contains("folded into /psyche"), "{line}");
            assert!(line.contains("/psyche opens the dial panel"), "{line}");
            assert!(line.contains(&format!("/settings {cmd} <level>")), "{line}");
            assert!(line.contains("(nothing changed)"), "{line}");
        }
    }

    #[test]
    fn retired_top_level_dials_redirect_without_mutating() {
        // #1665: `/tenacity relentless` and `/cognition meticulous` must NOT
        // half-work through the deprecation shim — redirect only, zero writes.
        use newt_core::cognition::{cli_cognition, set_cli_cognition, CognitionOverride};
        use newt_core::tenacity::{cli_tenacity, set_cli_tenacity, Tenacity};
        let _g = newt_core::test_guard::GlobalSettingsGuard::acquire();
        set_cli_cognition(CognitionOverride::Unset);
        set_cli_tenacity(Tenacity::Normal);

        super::dispatch(
            "tenacity",
            "relentless",
            "/tenacity relentless",
            ".",
            false,
            false,
        )
        .unwrap();
        assert_eq!(
            cli_tenacity(),
            Some(Tenacity::Normal),
            "retired /tenacity must not mutate the override"
        );
        super::dispatch(
            "cognition",
            "meticulous",
            "/cognition meticulous",
            ".",
            false,
            false,
        )
        .unwrap();
        assert_eq!(
            cli_cognition(),
            CognitionOverride::Unset,
            "retired /cognition must not mutate the override"
        );
    }

    #[test]
    fn tenacity_command_sets_the_level_live() {
        use newt_core::tenacity::{effective_tenacity, set_cli_tenacity, Tenacity};
        let _g = newt_core::test_guard::GlobalSettingsGuard::acquire();
        let restore = effective_tenacity();
        let msg = tenacity_command("relentless");
        assert!(msg.contains("relentless"), "{msg}");
        assert!(msg.contains("effectively unlimited"), "{msg}");
        assert!(msg.contains("/rounds"), "{msg}");
        assert_eq!(effective_tenacity(), Tenacity::Relentless);
        // review-2 #2: `/tenacity auto` releases the override (was un-undoable).
        let msg = tenacity_command("auto");
        assert!(msg.contains("cleared"), "{msg}");
        assert_eq!(
            newt_core::tenacity::cli_tenacity(),
            None,
            "/tenacity auto clears the session override"
        );
        // `inherit` / `reset` are aliases for the same clear.
        set_cli_tenacity(Tenacity::Relentless);
        tenacity_command("reset");
        assert_eq!(newt_core::tenacity::cli_tenacity(), None);
        // Restore so the process-global doesn't leak into sibling tests.
        set_cli_tenacity(restore);
    }
}