Skip to main content

Module run

Module run 

Source
Expand description

The shared render/event loop (Slice 2, #3414).

Why: DOC-50 §2.2 puts the event loop itself in trusty-code-tui so tagent and tcode stop each owning a bespoke copy — this module is the shared engine that shape lives in, generalized from the proven design in crates/trusty-agents/src/repl/tui/run.rs::event_loop (100ms redraw tick, tokio::select! over the tick and the event channel, a dedicated OS thread for the key read so it never parks the tokio runtime).

What: three pieces. spawn_key_reader is the OS thread that polls for crossterm events and forwards translated ReplEvents (key presses via crate::keys::translate_key_event, resizes, mouse-wheel scroll) onto an mpsc channel; it returns a KeyReaderGuard rather than a bare JoinHandle (see that type’s doc comment for why). event_loop is the terminal-generic tick/recv select loop — generic over ratatui::backend::Backend (not just CrosstermBackend) specifically so it can be unit-tested against ratatui::backend::TestBackend without a real TTY. run is the opinionated top-level entry point that wires a TerminalGuard, the key reader, and an E: TuiEngine together the way a real product binary actually wants to call this crate.

Deliberately NOT decided here: what M (the render/reducer model) looks like inside a product. Slice 4 owns the shared chat/statusline/picker widgets and the concrete model they render from; this module only needs to know when to redraw (every tick or every event) and when to stop (M::should_quit). That is the full contract apply/render/TuiModel below capture — deliberately mirroring crates/trusty-agents/src/repl/tui/run.rs::event_loop’s app: Arc<Mutex<ReplApp>> + draw(f, &snap) + process_event shape, generalized to not require Arc<Mutex<_>> (the caller decides its own interior mutability, if any).

A prior revision of run blocked crossterm::event::read() directly on the key-reader thread and shut it down with drop(tx); thread.join(). That hangs: read() only notices the channel closed on its NEXT send(), which can’t happen until a key is actually pressed — and by the time run reaches shutdown, the user has usually just pressed the key that made should_quit() true, so the thread is parked in a SECOND, indefinite read(). Worse, the terminal restore ran after that join, so the terminal stayed frozen in raw/alt-screen mode with no feedback while hung. It also leaked the thread entirely on the engine.setup/ subscribe_workstream_events error paths, which returned via ? before ever reaching the join. KeyReaderGuard and the restructured run fix both: the thread polls with a bounded timeout and checks an atomic stop flag every poll, and terminal restoration happens unconditionally, before the (now-bounded) thread join, on every exit path.

§Spec References

Structs§

KeyReaderGuard
RAII handle for the key-reader thread spawned by spawn_key_reader.

Constants§

TICK
How often event_loop redraws when no event arrives.

Traits§

TuiModel
A render/reducer model the shared event_loop/run can drive without knowing its concrete shape.

Functions§

event_loop
The terminal-generic tick/event select loop.
run
spawn_key_reader
Spawn the dedicated OS thread that polls for crossterm events and forwards translated events onto tx, returning a KeyReaderGuard that stops and joins it on drop.