| 1 | # TUI modularization plan |
| 2 | |
| 3 | ## Objective |
| 4 | |
| 5 | Turn `crates/tui/src/tui/ui.rs` into a small composition root without changing |
| 6 | the observable terminal contract or creating a second turn loop. The final file |
| 7 | should own shared UI types, module wiring, and top-level orchestration only. |
| 8 | |
| 9 | The baseline at `c93d21373` was 4,248 lines. Slices 1–9 are all extracted |
| 10 | (2026-08-21): `ui.rs` is now 2,130 lines of shared UI types, module wiring, and |
| 11 | top-level orchestration. Follow-on debt (partitioning `ui/event_loop.rs` and |
| 12 | `ui/tests.rs` by event source) is deliberately a second phase so it does not |
| 13 | hide the `ui.rs` extraction behind a wholesale rewrite. |
| 14 | |
| 15 | ## Rules for every extraction |
| 16 | |
| 17 | - Move one cohesive responsibility at a time; do not redesign behavior while |
| 18 | moving it. |
| 19 | - Give the new module explicit imports. Temporary parent re-exports are allowed |
| 20 | only where existing sibling modules or tests still depend on them. |
| 21 | - Keep `Engine::run_turn` as the only turn loop. |
| 22 | - Run formatting, a `codewhale-tui` library check, and the focused tests that |
| 23 | protect the moved behavior before starting the next extraction. |
| 24 | - Land conflict-prone slices only after the active owner has finished. In |
| 25 | particular, the remote-control bridge must incorporate the current `/rc` |
| 26 | recovery work rather than overwriting it. |
| 27 | |
| 28 | ## Ordered slices |
| 29 | |
| 30 | 1. **Terminal input and event fairness — extracted** |
| 31 | `ui/terminal_input.rs` owns the input thread, liveness recovery, child-terminal |
| 32 | pause/resume, and the engine-drain budget. |
| 33 | |
| 34 | 2. **Approval routing — extracted** |
| 35 | `ui/approval_routing.rs` owns session approval/denial resolution and durable |
| 36 | denial receipts. |
| 37 | |
| 38 | 3. **Remote-control bridge — extracted** |
| 39 | Move enrollment events, local-turn attachment, command acknowledgement, and |
| 40 | start/stop UI projection to `ui/remote_control_bridge.rs`. Do this after the |
| 41 | active `/rc` changes in the v0.9.11 integration lane are committed, because |
| 42 | those edits currently overlap this exact block. |
| 43 | |
| 44 | 4. **Observer hooks — extracted** |
| 45 | Move subagent and turn-end hook payload construction, preview bounding, and |
| 46 | completion classification to `ui/observer_hooks.rs`. |
| 47 | |
| 48 | 5. **Task and shell projection — extracted** |
| 49 | Move task-panel refresh, shell live-output reconciliation, detached-job |
| 50 | projection, and RLM task entries to `ui/task_projection.rs`. |
| 51 | |
| 52 | 6. **Paused-command and dispatch preparation — extracted** |
| 53 | Move pause/resume planning and the dispatch preparation/outcome types to |
| 54 | `ui/dispatch_prepare.rs`; keep actual dispatch execution in `dispatch.rs`. |
| 55 | |
| 56 | 7. **Compaction UI state — extracted** |
| 57 | Move manual/automatic compaction queueing, settlement, receipts, and cancel |
| 58 | behavior to `ui/compaction_flow.rs`. |
| 59 | |
| 60 | 8. **Provider configuration — extracted** |
| 61 | Move web-config event draining, rollback snapshots, key verification, and |
| 62 | provider setup tests to `ui/provider_setup.rs` and |
| 63 | `ui/provider_setup/tests.rs`. |
| 64 | |
| 65 | 9. **Small residual policies — extracted** |
| 66 | Move context-pressure warnings, update notices, and notification decisions |
| 67 | into their existing domain modules. Leave `ui.rs` as wiring plus genuinely |
| 68 | shared aliases. |
| 69 | |
| 70 | ## Follow-on debt |
| 71 | |
| 72 | `ui/event_loop.rs` and `ui/tests.rs` are larger than `ui.rs`. Once the composition |
| 73 | root split is complete, partition the event loop by event source and move tests |
| 74 | next to the modules they exercise. This is a second phase so it does not hide |
| 75 | the `ui.rs` extraction behind a wholesale rewrite. |
| 76 |