Skip to content

refactor(tui): UI-neutral input seam, crossterm as an adapter - #551

Merged
ajianaz merged 1 commit into
developfrom
refactor/tui-input-seam
Nov 10, 2026
Merged

ajianaz merged 1 commit into
developfrom
refactor/tui-input-seam

Conversation

@ajianaz

@ajianaz ajianaz commented Nov 10, 2026

Copy link
Copy Markdown
Contributor

What

First part of #526 (UI epic #523): the TUI's key logic takes a UI-neutral Input instead of a crossterm KeyEvent. App::on_input(Input) holds the logic; a crossterm adapter maps key events onto it. Refs #526, does not close it.

Why

A second UI (the GPUI desktop, #527) can only reuse tole-tui's state machine if that logic is not welded to the terminal. App::on_key took a crossterm KeyEvent and mixed key decoding with state. The issue's plan is to do this inside tole-tui (no new crate until a second consumer exists), in small behavior-preserving steps; this is the input one, the biggest of the three.

Changes

  • input.rs (new, no terminal types): Key::{Char, Enter, Esc, Tab, BackTab, Backspace, Up, Down, Other}, Mods { ctrl, alt, shift }, Input { key, mods } with small constructors. These are exactly the keys the logic distinguishes today; docs/tui.md's key table stays the behavior contract.
  • term_input.rs (new, crossterm adapter): input_from(KeyEvent) -> Option<Input> (a key release is dropped, as before) and App::on_key(KeyEvent), kept as the terminal entry point.
  • lib.rs: on_key becomes on_input; picker_key and answer take a Key. The key-matching logic is unchanged apart from the types. The crossterm key imports are gone from the production code (the event loop keeps its terminal imports; App's input path has none).
  • Key::Other is deliberate: every key the logic does not distinguish (Left, Home, F5, ...) still counts as "a key was pressed", so it still disarms a pending Ctrl+C and re-asks an approval, exactly as before. Mapping them to None would have silently changed Ctrl+C, Left, Ctrl+C from "arms twice" to "quits".
  • docs/tui.md (Architecture: the input seam), CHANGELOG (Changed; new public items tole_tui::{Input, Key, Mods}) and docs/epics.md updated in the same PR (AGENTS.md §7); README unaffected.

Testing

  • cargo fmt --all -- --check, cargo clippy --workspace --all-targets -- -D warnings, cargo check -p tole-cli --no-default-features, cargo test --workspace (1195 passed: the previous 1187 plus 8). The 55 existing tole-tui tests pass unchanged (they drive on_key through the adapter), and the real-terminal tui_e2e suite (a pty against a scripted provider) passes.
  • Local cora review --staged: not run (cora is not installed in this environment); the CI Cora Review job is the verdict.
  • 8 new tests: the adapter maps every distinguished key and each modifier (alone and combined), maps ignored keys to Key::Other (not dropped), drops a release and acts on a repeat, and a release does not disarm Ctrl+C; and App driven with Inputs alone (no crossterm): type and submit, Shift/Alt/Ctrl+J newlines versus plain Enter, an ignored key still disarming Ctrl+C, and BackTab being its own key.
  • Mutations, each caught: unmapped keys dropped instead of Other (adapter test), the Enter-newline rule shift || alt turned into && (four tests, including a new one), and the adapter reading Ctrl from the Alt flag (caught as a hang of an existing scripted event-loop test that never reaches its quit key, which the timeout reported as a failure, not as a clean assertion).

Next in #526

Part 2: collapse the per-command TuiHost methods into one apply(HostCmd) path plus run_turn, footer and sessions, with a fake host in tests for the interface a remote adapter will implement. Part 3: model a pending approval as source-agnostic UI state that carries its risk tier (a UI never offers allow-always for Destructive).

Related Issues

Refs #526, part of #523. Related: #464, #463.


Generated by Claude Code

App::on_input(Input) holds the key logic with no terminal types; term_input maps crossterm key events to Input (releases dropped, unmapped keys become Key::Other so they still disarm Ctrl+C). on_key remains the terminal entry point, so every existing test passes unchanged.
@github-actions

Copy link
Copy Markdown

🔍 Cora AI Code Review

✅ No issues found. Code looks good!


Review powered by cora-code · BYOK · MIT

@ajianaz
ajianaz merged commit 6a0bad3 into develop Nov 10, 2026
15 of 16 checks passed
ajianaz added a commit that referenced this pull request Nov 10, 2026
## What

Second part of #526 (UI epic #523): `TuiHost` shrinks from eleven
methods to three (`footer`, `run_turn`, `apply`). `apply(HostCmd) ->
Result<HostReply, String>` replaces `models`, `set_model`, `set_effort`,
`request_compaction`, `new_session`, `sessions`, `switch_session`,
`status_lines` and `set_name`. Refs #526, does not close it.

## Why

Eight of the eleven methods defaulted to `unsupported()` and duplicated
the `HostCmd` enum, and the trait had one adapter, so a second UI (GPUI,
a remote client) had a wide interface to implement for what is really
"apply this command". With one call, a remote adapter writes three
methods. Agreed as part of the #526 plan (the issue's item 3); the input
seam (#551) was part 1.

## Changes

- `tole-tui`:
- `HostReply { Done, Session(id), Models, Sessions, Lines }`: data only.
The wording shown to the user ("model → x", "new session s-9", "(failed:
...)") and the pickers stay in the UI (`run_host_cmd`), so a host has no
UI text to produce.
- `TuiHost::apply` default: supports `Status` (no lines) and refuses the
rest with "not supported by this host", exactly what the removed
defaults did.
- `run_host_cmd` decides what to say before handing the command over,
then maps the reply. A reply of the wrong kind (e.g. a model list for a
model change) is reported in the transcript as `(failed: unexpected host
reply ...)`, never a panic.
- `tole-cli` `TuiSession`: `footer` and `run_turn` stay in the trait
impl; the nine operations become plain inherent methods (same bodies,
same errors), and `apply` dispatches to them. Its existing unit tests
call those methods and are unchanged.
- No slash command is lost; user-visible behavior is unchanged.
- CHANGELOG (Changed), `docs/tui.md` (Architecture: host seam) and
`docs/epics.md` updated in the same PR (AGENTS.md §7); README
unaffected.
- **Breaking for anyone implementing `TuiHost` outside this repository**
(override `apply` instead of the removed methods); new public item
`tole_tui::HostReply`. Called out in the CHANGELOG.

## Testing

- [x] `cargo fmt --all -- --check`, `cargo clippy --workspace
--all-targets -- -D warnings`, `cargo check -p tole-cli
--no-default-features`, `cargo test --workspace` (1203 passed: the
previous 1195 plus 8). The 63 existing `tole-tui` tests, including the
loop-level footer-refresh test (its host now overrides `apply`), and the
real-terminal `tui_e2e` suite pass.
- [ ] Local `cora review --staged`: not run (cora is not installed in
this environment); the CI Cora Review job is the verdict.
- [x] The acceptance item "a fake host in tests exercises the same
interface a remote adapter will use": `FakeHost` has only the three
methods, records every command and answers with scripted replies. 7 new
`tole-tui` tests: each change is exactly one `apply` call with the UI's
wording and a stale footer; New/Switch report the session id the host
names; model and session pickers open from the host's data, and empty or
failed ones say so; status lines are shown as given; a failing host is
reported and leaves the footer alone; a wrong-kind reply is reported; a
host that implements nothing supports `Status` and refuses the rest.
- [x] 1 new `tole-cli` test: `TuiSession::apply` dispatches every
command to its session operation (what each wrote, read back through the
session's own readers; bad input refused with the operation's own error;
`New`/`Switch` answer with the session now in use; `Status` lines;
`PickSession` without an index).
- Mutations, each caught: New/Switch wording swapped, the footer never
marked stale (3 tests), the adapter's `Compact` doing nothing, and
`Name` routed to the wrong operation.

## Unrelated flake seen while testing

One full-workspace run failed `tole-core`
`turn_loop::turn_survives_reopen_between_steps` with `Locked { holder:
<our own pid> }`; it passed on the next full run (1203). It is the same
signature as an earlier flake, and I reproduced the cause with a
throwaway probe (207 of 3000 reopens fail while other threads spawn
child processes, 0 of 3000 without) and filed it as #552 with the
evidence and a proposed fix. Not part of this PR.

## Next in #526

Part 3: model a pending approval as source-agnostic UI state that
carries its risk tier (a UI never offers allow-always for `Destructive`;
today the in-process `Prompter`, remotely the `/approvals` queue).

## Related Issues

Refs #526, part of #523. Related: #464, #463, #552.

---
_Generated by [Claude
Code](https://claude.ai/code/session_01GwJr9GzLbDFeXo3HZHZpyP)_

Co-authored-by: Claude <[email protected]>
@ajianaz
ajianaz deleted the refactor/tui-input-seam branch November 11, 2026 04:10
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants