magi-code 0.96.1

Repository-aware CLI coding agent for terminal work
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
use crossterm::event::{KeyCode, KeyEvent, KeyModifiers};
use std::sync::LazyLock;

pub(crate) const REWIND_MODE_KEY: KeyCode = KeyCode::Tab;
pub(crate) const REWIND_MODE_HELP: &str = "[Tab] mode";
pub(crate) const THEME_VARIANT_HELP: &str = "[Tab/Shift-Tab] tabs";

pub(crate) fn is_main_tab_cycle_key(key: KeyEvent) -> bool {
    (key.code == KeyCode::BackTab
        && (key.modifiers.is_empty() || key.modifiers == KeyModifiers::SHIFT))
        || (key.code == KeyCode::Tab && key.modifiers == KeyModifiers::SHIFT)
}

pub(super) fn is_theme_variant_switch_key(key: KeyEvent) -> bool {
    matches!(key.code, KeyCode::Tab | KeyCode::BackTab)
        && (key.modifiers.is_empty() || key.modifiers == KeyModifiers::SHIFT)
}

#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
pub(crate) enum KeyCommand {
    OpenActivityDetail,
    SubmitPrompt,
    InsertPromptNewline,
    Exit,
    ClearInputOrExit,
    FocusPrompt,
    FocusActivityTree,
    CyclePrimaryAgent,
    ToggleHelp,
    ToggleActivityTreeModal,
    ToggleSessionFilesModal,
    ToggleHeaderModalsPinned,
    MoveSelectionUp,
    MoveSelectionDown,
    CollapseSelected,
    ExpandSelected,
    ExpandAll,
    CollapseAll,
    SelectFailed,
    SelectRunning,
    ScrollDetailUp,
    ScrollDetailDown,
    PageDetailUp,
    PageDetailDown,
    ScrollTranscriptUp,
    ScrollTranscriptDown,
    MovePromptCursorLeft,
    MovePromptCursorRight,
    MovePromptCursorUp,
    MovePromptCursorDown,
    UndoPrompt,
    RedoPrompt,
    MovePromptWordLeft,
    MovePromptWordRight,
    DeletePromptWord,
    DeletePromptNextWord,
    ExtendPromptSelectionLeft,
    ExtendPromptSelectionRight,
    ExtendPromptSelectionUp,
    ExtendPromptSelectionDown,
    SelectAllPrompt,
    CycleThinkingLevel,
    OpenModel,
    ToggleSummary,
    ToggleSummaryModal,
    ScrollSummaryUp,
    ScrollSummaryDown,
    PageSummaryUp,
    PageSummaryDown,
}

#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
pub(crate) enum KeyContext {
    Global,
    Prompt,
    Transcript,
    ActivityTree,
    ActivityDetail,
    Summary,
}

#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
pub(crate) struct KeyPattern {
    code: KeyCode,
    modifiers: KeyModifiers,
}

impl KeyPattern {
    const fn new(code: KeyCode, modifiers: KeyModifiers) -> Self {
        Self { code, modifiers }
    }

    fn matches(self, key: KeyEvent) -> bool {
        key.code == self.code && key.modifiers == self.modifiers
    }
}

#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
pub(crate) struct KeyBinding {
    pub(crate) action: KeyCommand,
    pub(crate) context: KeyContext,
    pub(crate) key: KeyPattern,
    pub(crate) label: &'static str,
    pub(crate) description: &'static str,
}

pub(super) const DEFAULT_KEYBINDINGS: &[KeyBinding] = &[
    KeyBinding {
        action: KeyCommand::ToggleSummary,
        context: KeyContext::Summary,
        key: KeyPattern::new(KeyCode::Enter, KeyModifiers::NONE),
        label: "Enter",
        description: "start/stop summary",
    },
    KeyBinding {
        action: KeyCommand::ScrollSummaryUp,
        context: KeyContext::Summary,
        key: KeyPattern::new(KeyCode::Up, KeyModifiers::NONE),
        label: "Up",
        description: "scroll summary up",
    },
    KeyBinding {
        action: KeyCommand::ScrollSummaryDown,
        context: KeyContext::Summary,
        key: KeyPattern::new(KeyCode::Down, KeyModifiers::NONE),
        label: "Down",
        description: "scroll summary down",
    },
    KeyBinding {
        action: KeyCommand::PageSummaryUp,
        context: KeyContext::Summary,
        key: KeyPattern::new(KeyCode::PageUp, KeyModifiers::NONE),
        label: "PgUp",
        description: "page summary up",
    },
    KeyBinding {
        action: KeyCommand::PageSummaryDown,
        context: KeyContext::Summary,
        key: KeyPattern::new(KeyCode::PageDown, KeyModifiers::NONE),
        label: "PgDn",
        description: "page summary down",
    },
    KeyBinding {
        action: KeyCommand::ToggleHelp,
        context: KeyContext::Global,
        key: KeyPattern::new(KeyCode::F(1), KeyModifiers::NONE),
        label: "F1",
        description: "toggle help",
    },
    KeyBinding {
        action: KeyCommand::Exit,
        context: KeyContext::Global,
        key: KeyPattern::new(KeyCode::Char('d'), KeyModifiers::CONTROL),
        label: "Ctrl-D",
        description: "exit",
    },
    KeyBinding {
        action: KeyCommand::ClearInputOrExit,
        context: KeyContext::Global,
        key: KeyPattern::new(KeyCode::Char('c'), KeyModifiers::CONTROL),
        label: "Ctrl-C",
        description: "copy selected text; otherwise clear prompt, or exit when empty",
    },
    KeyBinding {
        action: KeyCommand::FocusActivityTree,
        context: KeyContext::Global,
        key: KeyPattern::new(KeyCode::Char('t'), KeyModifiers::CONTROL),
        label: "Ctrl-T",
        description: "open Activity Tree modal",
    },
    KeyBinding {
        action: KeyCommand::CyclePrimaryAgent,
        context: KeyContext::Global,
        key: KeyPattern::new(KeyCode::Char('p'), KeyModifiers::ALT),
        label: "Alt-P",
        description: "cycle primary agent selection",
    },
    KeyBinding {
        action: KeyCommand::ToggleActivityTreeModal,
        context: KeyContext::Global,
        key: KeyPattern::new(KeyCode::Char('1'), KeyModifiers::ALT),
        label: "Alt-1",
        description: "show/hide Activity Tree",
    },
    KeyBinding {
        action: KeyCommand::ToggleSessionFilesModal,
        context: KeyContext::Global,
        key: KeyPattern::new(KeyCode::Char('2'), KeyModifiers::ALT),
        label: "Alt-2",
        description: "show/hide Session Files",
    },
    KeyBinding {
        action: KeyCommand::ToggleSummaryModal,
        context: KeyContext::Global,
        key: KeyPattern::new(KeyCode::Char('3'), KeyModifiers::ALT),
        label: "Alt-3",
        description: "show/hide Summary",
    },
    KeyBinding {
        action: KeyCommand::ToggleHeaderModalsPinned,
        context: KeyContext::Global,
        key: KeyPattern::new(KeyCode::Char('a'), KeyModifiers::ALT),
        label: "Alt-A",
        description: "pin/unpin all column modals beside Transcript; pinned modals hide on Changes",
    },
    KeyBinding {
        action: KeyCommand::FocusPrompt,
        context: KeyContext::Global,
        key: KeyPattern::new(KeyCode::Esc, KeyModifiers::NONE),
        label: "Esc",
        description: "focus prompt and close help",
    },
    KeyBinding {
        action: KeyCommand::CycleThinkingLevel,
        context: KeyContext::Global,
        key: KeyPattern::new(KeyCode::Char('t'), KeyModifiers::ALT),
        label: "Alt-T",
        description: "cycle reasoning level",
    },
    KeyBinding {
        action: KeyCommand::OpenModel,
        context: KeyContext::Global,
        key: KeyPattern::new(KeyCode::Char('m'), KeyModifiers::ALT),
        label: "Alt-M",
        description: "open model picker",
    },
    KeyBinding {
        action: KeyCommand::SubmitPrompt,
        context: KeyContext::Prompt,
        key: KeyPattern::new(KeyCode::Enter, KeyModifiers::NONE),
        label: "Enter",
        description: "submit prompt",
    },
    KeyBinding {
        action: KeyCommand::InsertPromptNewline,
        context: KeyContext::Prompt,
        key: KeyPattern::new(KeyCode::Enter, KeyModifiers::SHIFT),
        label: "Shift-Enter",
        description: "insert newline",
    },
    KeyBinding {
        action: KeyCommand::InsertPromptNewline,
        context: KeyContext::Prompt,
        key: KeyPattern::new(KeyCode::Char('j'), KeyModifiers::CONTROL),
        label: "Ctrl-J",
        description: "insert newline (portable fallback)",
    },
    KeyBinding {
        action: KeyCommand::InsertPromptNewline,
        context: KeyContext::Prompt,
        key: KeyPattern::new(KeyCode::Enter, KeyModifiers::CONTROL),
        label: "Ctrl-Enter",
        description: "insert newline (requires terminal support)",
    },
    KeyBinding {
        action: KeyCommand::MovePromptCursorLeft,
        context: KeyContext::Prompt,
        key: KeyPattern::new(KeyCode::Left, KeyModifiers::NONE),
        label: "Left",
        description: "move prompt cursor left",
    },
    KeyBinding {
        action: KeyCommand::MovePromptCursorRight,
        context: KeyContext::Prompt,
        key: KeyPattern::new(KeyCode::Right, KeyModifiers::NONE),
        label: "Right",
        description: "move prompt cursor right",
    },
    KeyBinding {
        action: KeyCommand::MovePromptCursorUp,
        context: KeyContext::Prompt,
        key: KeyPattern::new(KeyCode::Up, KeyModifiers::NONE),
        label: "Up",
        description: "move prompt cursor up",
    },
    KeyBinding {
        action: KeyCommand::MovePromptCursorDown,
        context: KeyContext::Prompt,
        key: KeyPattern::new(KeyCode::Down, KeyModifiers::NONE),
        label: "Down",
        description: "move prompt cursor down",
    },
    KeyBinding {
        action: KeyCommand::UndoPrompt,
        context: KeyContext::Prompt,
        key: KeyPattern::new(KeyCode::Char('z'), KeyModifiers::CONTROL),
        label: "Ctrl-Z",
        description: "undo prompt edit",
    },
    KeyBinding {
        action: KeyCommand::RedoPrompt,
        context: KeyContext::Prompt,
        key: KeyPattern::new(KeyCode::Char('y'), KeyModifiers::CONTROL),
        label: "Ctrl-Y",
        description: "redo prompt edit",
    },
    KeyBinding {
        action: KeyCommand::MovePromptWordLeft,
        context: KeyContext::Prompt,
        key: KeyPattern::new(KeyCode::Left, KeyModifiers::CONTROL),
        label: "Ctrl-Left",
        description: "move prompt cursor left by word",
    },
    KeyBinding {
        action: KeyCommand::MovePromptWordRight,
        context: KeyContext::Prompt,
        key: KeyPattern::new(KeyCode::Right, KeyModifiers::CONTROL),
        label: "Ctrl-Right",
        description: "move prompt cursor right by word",
    },
    KeyBinding {
        action: KeyCommand::DeletePromptWord,
        context: KeyContext::Prompt,
        key: KeyPattern::new(KeyCode::Backspace, KeyModifiers::CONTROL),
        label: "Ctrl-Backspace",
        description: "delete previous prompt word",
    },
    KeyBinding {
        action: KeyCommand::DeletePromptNextWord,
        context: KeyContext::Prompt,
        key: KeyPattern::new(KeyCode::Delete, KeyModifiers::CONTROL),
        label: "Ctrl-Delete",
        description: "delete next prompt word",
    },
    KeyBinding {
        action: KeyCommand::ExtendPromptSelectionLeft,
        context: KeyContext::Prompt,
        key: KeyPattern::new(KeyCode::Left, KeyModifiers::SHIFT),
        label: "Shift-Left",
        description: "extend prompt selection left",
    },
    KeyBinding {
        action: KeyCommand::ExtendPromptSelectionRight,
        context: KeyContext::Prompt,
        key: KeyPattern::new(KeyCode::Right, KeyModifiers::SHIFT),
        label: "Shift-Right",
        description: "extend prompt selection right",
    },
    KeyBinding {
        action: KeyCommand::ExtendPromptSelectionUp,
        context: KeyContext::Prompt,
        key: KeyPattern::new(KeyCode::Up, KeyModifiers::SHIFT),
        label: "Shift-Up",
        description: "extend prompt selection up",
    },
    KeyBinding {
        action: KeyCommand::ExtendPromptSelectionDown,
        context: KeyContext::Prompt,
        key: KeyPattern::new(KeyCode::Down, KeyModifiers::SHIFT),
        label: "Shift-Down",
        description: "extend prompt selection down",
    },
    KeyBinding {
        action: KeyCommand::SelectAllPrompt,
        context: KeyContext::Prompt,
        key: KeyPattern::new(KeyCode::Char('a'), KeyModifiers::CONTROL),
        label: "Ctrl-A",
        description: "select all prompt text",
    },
    KeyBinding {
        action: KeyCommand::ScrollTranscriptUp,
        context: KeyContext::Prompt,
        key: KeyPattern::new(KeyCode::Up, KeyModifiers::ALT),
        label: "Alt-Up",
        description: "scroll transcript up",
    },
    KeyBinding {
        action: KeyCommand::ScrollTranscriptDown,
        context: KeyContext::Prompt,
        key: KeyPattern::new(KeyCode::Down, KeyModifiers::ALT),
        label: "Alt-Down",
        description: "scroll transcript down",
    },
    KeyBinding {
        action: KeyCommand::ScrollTranscriptUp,
        context: KeyContext::Transcript,
        key: KeyPattern::new(KeyCode::Up, KeyModifiers::NONE),
        label: "Up",
        description: "scroll transcript up",
    },
    KeyBinding {
        action: KeyCommand::ScrollTranscriptDown,
        context: KeyContext::Transcript,
        key: KeyPattern::new(KeyCode::Down, KeyModifiers::NONE),
        label: "Down",
        description: "scroll transcript down",
    },
    KeyBinding {
        action: KeyCommand::OpenActivityDetail,
        context: KeyContext::ActivityTree,
        key: KeyPattern::new(KeyCode::Enter, KeyModifiers::NONE),
        label: "Enter",
        description: "open activity details",
    },
    KeyBinding {
        action: KeyCommand::MoveSelectionDown,
        context: KeyContext::ActivityTree,
        key: KeyPattern::new(KeyCode::Char('j'), KeyModifiers::ALT),
        label: "Alt-J",
        description: "move activity selection down",
    },
    KeyBinding {
        action: KeyCommand::MoveSelectionUp,
        context: KeyContext::ActivityTree,
        key: KeyPattern::new(KeyCode::Char('k'), KeyModifiers::ALT),
        label: "Alt-K",
        description: "move activity selection up",
    },
    KeyBinding {
        action: KeyCommand::MoveSelectionUp,
        context: KeyContext::ActivityTree,
        key: KeyPattern::new(KeyCode::Up, KeyModifiers::NONE),
        label: "Up",
        description: "move activity selection up",
    },
    KeyBinding {
        action: KeyCommand::MoveSelectionDown,
        context: KeyContext::ActivityTree,
        key: KeyPattern::new(KeyCode::Down, KeyModifiers::NONE),
        label: "Down",
        description: "move activity selection down",
    },
    KeyBinding {
        action: KeyCommand::CollapseSelected,
        context: KeyContext::ActivityTree,
        key: KeyPattern::new(KeyCode::Left, KeyModifiers::NONE),
        label: "Left",
        description: "collapse selected activity",
    },
    KeyBinding {
        action: KeyCommand::ExpandSelected,
        context: KeyContext::ActivityTree,
        key: KeyPattern::new(KeyCode::Right, KeyModifiers::NONE),
        label: "Right",
        description: "expand selected activity",
    },
    KeyBinding {
        action: KeyCommand::CollapseSelected,
        context: KeyContext::ActivityTree,
        key: KeyPattern::new(KeyCode::Char('h'), KeyModifiers::ALT),
        label: "Alt-H",
        description: "collapse selected activity",
    },
    KeyBinding {
        action: KeyCommand::ExpandSelected,
        context: KeyContext::ActivityTree,
        key: KeyPattern::new(KeyCode::Char('l'), KeyModifiers::ALT),
        label: "Alt-L",
        description: "expand selected activity",
    },
    KeyBinding {
        action: KeyCommand::ExpandAll,
        context: KeyContext::ActivityTree,
        key: KeyPattern::new(KeyCode::Char('e'), KeyModifiers::ALT),
        label: "Alt-E",
        description: "expand all activities",
    },
    KeyBinding {
        action: KeyCommand::CollapseAll,
        context: KeyContext::ActivityTree,
        key: KeyPattern::new(KeyCode::Char('c'), KeyModifiers::ALT),
        label: "Alt-C",
        description: "collapse all activities",
    },
    KeyBinding {
        action: KeyCommand::SelectFailed,
        context: KeyContext::ActivityTree,
        key: KeyPattern::new(KeyCode::Char('f'), KeyModifiers::ALT),
        label: "Alt-F",
        description: "select first failed activity",
    },
    KeyBinding {
        action: KeyCommand::SelectRunning,
        context: KeyContext::ActivityTree,
        key: KeyPattern::new(KeyCode::Char('r'), KeyModifiers::ALT),
        label: "Alt-R",
        description: "select first running activity",
    },
    KeyBinding {
        action: KeyCommand::ScrollDetailUp,
        context: KeyContext::ActivityDetail,
        key: KeyPattern::new(KeyCode::Up, KeyModifiers::NONE),
        label: "Up",
        description: "scroll selected activity detail up",
    },
    KeyBinding {
        action: KeyCommand::ScrollDetailDown,
        context: KeyContext::ActivityDetail,
        key: KeyPattern::new(KeyCode::Down, KeyModifiers::NONE),
        label: "Down",
        description: "scroll selected activity detail down",
    },
    KeyBinding {
        action: KeyCommand::PageDetailUp,
        context: KeyContext::ActivityDetail,
        key: KeyPattern::new(KeyCode::PageUp, KeyModifiers::NONE),
        label: "PageUp",
        description: "scroll selected activity detail up one page",
    },
    KeyBinding {
        action: KeyCommand::PageDetailDown,
        context: KeyContext::ActivityDetail,
        key: KeyPattern::new(KeyCode::PageDown, KeyModifiers::NONE),
        label: "PageDown",
        description: "scroll selected activity detail down one page",
    },
    KeyBinding {
        action: KeyCommand::ScrollDetailUp,
        context: KeyContext::ActivityDetail,
        key: KeyPattern::new(KeyCode::Up, KeyModifiers::ALT),
        label: "Alt-Up",
        description: "scroll selected activity detail up",
    },
    KeyBinding {
        action: KeyCommand::ScrollDetailDown,
        context: KeyContext::ActivityDetail,
        key: KeyPattern::new(KeyCode::Down, KeyModifiers::ALT),
        label: "Alt-Down",
        description: "scroll selected activity detail down",
    },
];

fn bracketed_label(label: &str) -> String {
    format!("[{label}]")
}

static HELP_TEXT: LazyLock<String> = LazyLock::new(build_help_text);

pub(super) fn help_text() -> &'static str {
    &HELP_TEXT
}

fn build_help_text() -> String {
    let mut lines = vec![
        "Mission Control Help".to_string(),
        "".to_string(),
        "Focus: [Tab] cycles Prompt, Transcript or the Changes columns. Unpinned header modals stay outside the Prompt/Transcript cycle. When pinned, [Tab] cycles Prompt, Transcript, then open modals in opening order. [Shift-Tab] switches Transcript/Changes without closing header modals.".to_string(),
        "Prompt: type normally when focused; [Enter] submits; [Shift-Enter] inserts a newline; arrows move the cursor; [Ctrl-Z]/[Ctrl-Y] undo/redo; [Ctrl-Left]/[Ctrl-Right] move by word; [Ctrl-Backspace]/[Ctrl-Delete] delete by word; [Shift-Arrow] selects; [Ctrl-A] selects all.".to_string(),
        "Newline compatibility: [Ctrl-J] is the portable fallback; [Ctrl-Enter] is another newline shortcut where supported. Some terminals send [Shift-Enter] and [Ctrl-Enter] as plain [Enter], which submits instead.".to_string(),
        "Running prompt: [Alt-C] restores queued steering to the editor, joined by newlines; with none queued, it cancels the run.".to_string(),
        "Transcript: [Alt-Up]/[Alt-Down] scroll transcript while Prompt is focused; mouse wheel over Transcript also scrolls transcript.".to_string(),
        "Activity Tree: click a row or press [Enter] to open details; [Up]/[Down] move selection; [Left]/[Right] collapse/expand; [Alt-E]/[Alt-C] expand/collapse all; [Alt-F]/[Alt-R] select failed/running. [Ctrl-T] opens Activity Tree; [Esc] closes it only when unpinned.".to_string(),
        "Selected Activity Detail modal: [Up]/[Down]/[PageUp]/[PageDown] or mouse wheel scroll detail; [Esc] closes and returns to the tree.".to_string(),
        "Long prompt: use cursor keys to navigate; the prompt does not scroll with the mouse wheel.".to_string(),
        "Header: Panels shows [Alt-1] Activities, [Alt-2] Files, [Alt-3] Summary; a filled dot marks each open panel. Open modals stack in opening order and share height equally. Closing one expands the others; reopening adds it at the bottom. Hide shortcuts stay visible regardless of focus or pinning. When unpinned, [Esc] closes the focused modal, or the last opened when no modal has focus, after dismissing autocomplete. Letters, numbers, or / focus Prompt; editing, paste, and submission keep modals open. Click a rail tab or press [Shift-Tab] to switch views.".to_string(),
        "Pin: [Alt-A] or the header Pin toggle pins/unpins all column modals. Pinning with none open opens all three; unpinning then closes them. Otherwise unpinning restores overlays. Pinned modals reserve a column beside Transcript and hide on Changes without closing. They ignore [Esc]. Other shortcut hints and focused border color follow modal focus. Closing the last modal turns pin off. Open panels, their order, and pin behavior survive session switches and save globally for restart. Startup focuses Prompt; focus and scroll are not saved.".to_string(),
        "Help: [F1] toggles this overlay; mouse wheel over Help scrolls this text.".to_string(),
        "Autocomplete: first-character / shows supported TUI slash commands; @ anywhere tags cwd files with fuzzy matching; $ anywhere tags enabled skills with fuzzy matching; [Up]/[Down] navigate; [Tab]/[Enter] accept only when suggestions are visible; [Esc] dismiss.".to_string(),
        "System prompt modal: [Esc]/[q] close; [Up]/[Down]/[Alt-Up]/[Alt-Down]/[PageUp]/[PageDown]/[Home]/[End] and mouse wheel scroll.".to_string(),
        "Sessions modal: two columns when space permits; [Tab]/[Shift-Tab] focus list or preview; [Up]/[Down] moves list or scrolls preview; mouse wheel scrolls hovered column; [Enter] switches; [Esc] closes. [Shift-Tab] depends on terminal delivery.".to_string(),
        "TUI slash commands: /help opens this overlay; /theme opens the runtime theme picker; /login openai-codex authenticates; /logout openai-codex removes local auth; /model selects cached models; /settings manages settings and refreshes model catalogs; /system-prompt opens read-only prompt inspection; /skills manages skills; /sessions opens the preview session switcher; /new starts a fresh session and clears transcript/activity/detail/help scroll; /quit exits.".to_string(),
        "Mouse: wheel follows the hovered scrollable pane; the Prompt editor is not mouse-scrollable; click activity items to select, click selected parents to expand/collapse.".to_string(),
        "".to_string(),
    ];
    for context in [
        KeyContext::Global,
        KeyContext::Prompt,
        KeyContext::Transcript,
        KeyContext::ActivityTree,
        KeyContext::ActivityDetail,
        KeyContext::Summary,
    ] {
        lines.push(format!("{context:?}"));
        for binding in DEFAULT_KEYBINDINGS
            .iter()
            .filter(|binding| binding.context == context)
        {
            lines.push(format!(
                "  {:<18} {}",
                bracketed_label(binding.label),
                binding.description
            ));
        }
        lines.push(String::new());
    }
    lines.join("\n")
}

pub(super) fn activity_detail_action_for_key(key: KeyEvent) -> Option<KeyCommand> {
    DEFAULT_KEYBINDINGS
        .iter()
        .find(|binding| binding.context == KeyContext::ActivityDetail && binding.key.matches(key))
        .map(|binding| binding.action)
}

pub(super) fn action_for_key(key: KeyEvent, active_context: KeyContext) -> Option<KeyCommand> {
    DEFAULT_KEYBINDINGS
        .iter()
        .find(|binding| {
            (binding.context == KeyContext::Global || binding.context == active_context)
                && binding.key.matches(key)
        })
        .map(|binding| binding.action)
}