Overview

The TUI input system dispatches keyboard events from Mordant to selection screens, tool approval prompts, extension shortcuts, and chat prompt editing commands.

Key Routing & Guarding Rules

Keyboard event handling in InputStateMachine.handle(event, screen) follows a strict priority chain:

  1. Custom Overlay Handling:

    • If a CustomViewSession overlay is active, keys emit directly to the overlay session.

  2. Extension Shortcut Guarding:

    • Custom extension shortcuts registered in ShortcutRegistry are evaluated only when:

      • Current visible screen is TuiScreen.CHAT

      • AND !callbacks.isApprovalPending() (no pending tool approval prompt)

    • If both conditions hold and a matching shortcut is registered, the shortcut action executes and input state machine returns without further dispatch.

    • On selection screens (MODEL_SELECTION, SESSION_SELECTION, EFFORT_SELECTION, TREE_SELECTION, FORK_SELECTION) or when tool approval is pending, shortcut lookup is bypassed so keys like "j" or "k" navigate screens or respond to tool prompts without triggering extension callbacks.

  3. Screen Key Dispatch:

    • Events delegate to polymorphic ScreenKeyHandler strategy implementations configured for the active TuiScreen.

    • On TuiScreen.CHAT, if isApprovalPending() is true, events delegate to callbacks.onApprovalKey(event).

    • Otherwise, events dispatch through the polymorphic ChatKeyHandler pipeline.

Polymorphic Key Handler Architecture

Monolithic conditional key handling in handleChatKey is decomposed using Martin Fowler refactoring patterns (Replace Conditional with Polymorphism and Extract Class):

1. ScreenKeyHandler

Polymorphic strategy for per-screen dispatch (TuiScreen.CHAT, MODEL_SELECTION, SESSION_SELECTION, EFFORT_SELECTION, TREE_SELECTION, FORK_SELECTION).

2. ChatKeyHandler Pipeline

Sequential functional handlers implementing single-responsibility strategy logic:

  • SessionLifecycleKeyHandler: Ctrl+C (agent cancellation / ctrl_c session shutdown) and Ctrl+D (empty buffer ctrl_d session shutdown).

  • ViewportKeyHandler: Ctrl+L (screen clearing) and Ctrl+T (thinking visibility toggle).

  • EffortKeyHandler: Ctrl+E, Alt+E, and Ctrl+Shift+E reasoning effort cycling.

  • ExternalEditorKeyHandler: Ctrl+G external editor launcher.

  • InputHistoryKeyHandler: Alt+Up and Alt+Down input history navigation.

  • CompletionAndScrollKeyHandler: ArrowUp, ArrowDown, PageUp, and PageDown scrolling and completion overlay navigation.

  • BufferEditorKeyHandler: Text typing, Enter prompt submission / slash-command execution, Tab completion trigger, Backspace character deletion, and Escape buffer clearing.

3. CompletionController

Class extracted from InputStateMachine managing file completion (@/#), slash command completion (/), and completion item selection/application.

Thread-Safe Single-Writer Editor Mutation Queue

Programmatic editor text APIs in MordantExtensionUI (setEditorText, pasteToEditor) write mutations into a lock-free single-writer Channel<EditorMutation> (SetText, Insert, Clear).

InputStateMachine.drainEditorMutations(channel) sequentially applies queued mutations to inputBuffer on frame loop ticks without mutexes or synchronized race conditions.

Conflict Key Definitions

Built-in conflict keys (including ctrl+shift+e) are defined in :jvm:cli at CliShortcutConflicts.BUILT_IN_CONFLICT_KEYS and injected into ShortcutRegistry at construction in TuiModule.