fix: detect engine task death mid-turn and recover UI immediately
Problem
When the engine task panics (or exits unexpectedly) between TurnStarted and TurnComplete, the event channel tx_event is dropped and the mpsc channel closes silently. The UI's event loop uses while let Ok(event) = rx.try_recv() — on Err(TryRecvError::Disconnected) the loop simply exits without any recovery. The turn remains stuck with is_loading=true and runtime_turn_status=Some("in_progress") until the 300-second TURN_STALL_WATCHDOG_TIMEOUT fires in reconcile_turn_liveness().
The root cause is in spawn_supervised (utils.rs): it wraps the future in catch_unwind, logs the panic, writes a dump file, and exits — but never sends an EngineEvent::Error over the channel. The UI has no way to know the engine died.
Fix
Add a post-loop check for TryRecvError::Disconnected after the existing while let Ok(event) event processing loop (ui.rs). If the channel is disconnected while app.is_loading is true, the UI immediately:
- Finalizes in-flight streaming thinking / assistant / tool cells
- Resets
is_loading,runtime_turn_status,turn_started_at,dispatch_started_at, etc. - Pushes an error toast: "Engine process has terminated unexpectedly."
This reduces the recovery window from 300 seconds to ~1 frame (~16ms).
Testing
engine_event_channel_disconnect_recovers_mid_turn_ui_state — creates a real mpsc channel, drops the sender to simulate engine death, sets up the app in mid-turn state, applies the recovery logic, and verifies the UI state is fully cleaned up.
Related
Fixes the symptom described in #2583. The root cause of why the engine panics can be diagnosed via crash dumps in ~/.codewhale/crashes/.
Greptile Summary
This PR adds an immediate recovery path for when the engine task dies (panics or exits) mid-turn, eliminating the previous 300-second stall window. It hooks into the existing event-loop tick by checking rx.is_closed() after the try_recv drain loop and resets all in-flight UI state when a disconnect is detected while the app is in a loading/compacting/purging state.
ui.rs: Adds a post-drain-loop disconnect guard inside the engine-event read scope; clears all per-turn app flags (is_loading,is_compacting,is_purging, streaming indices, agent progress, etc.), drains pending steers into the visible queue, pushes an error toast, and setsneeds_redraw.tests.rs: Addsengine_event_channel_disconnect_recovers_mid_turn_ui_statewhich creates a real mpsc channel, drops the sender to simulate engine death, and verifies the resulting app state matches expectations.
Confidence Score: 5/5
Safe to merge — the disconnect check is one-shot and self-disabling, uses no await points inside the recovery block, and the guard condition prevents re-firing after cleanup.
The recovery block is logically correct: rx.is_closed() reliably returns true when all senders are dropped (confirmed by tokio 1.49 docs), the while-drain loop runs first so no buffered events are skipped, and the guard (is_loading || is_compacting || is_purging) ensures the block only fires when there is actually live turn state to clean up. Previously-flagged concerns around needs_redraw and pending-steer loss are addressed in this version.
No files require special attention beyond what was already discussed in prior review threads.
Important Files Changed
| Filename | Overview |
|---|---|
| crates/tui/src/tui/ui.rs | Adds a one-shot post-drain disconnect check using rx.is_closed(); correctly clears all per-turn UI state, drains pending steers, and triggers an immediate redraw. Guard condition prevents re-firing after the first recovery. |
| crates/tui/src/tui/ui/tests.rs | New test verifies the disconnect recovery behavior end-to-end. The test body inlines the production recovery logic verbatim rather than calling an extracted function, so future divergence won't be caught by the test. |
Sequence Diagram
sequenceDiagram
participant E as Engine Task
participant CH as mpsc Channel
participant UI as UI Event Loop
participant APP as App State
E->>CH: TurnStarted
UI->>CH: try_recv() loop processes TurnStarted
UI->>APP: "is_loading = true"
Note over E: Panic / unexpected exit
E--xCH: tx_event dropped (channel closed)
loop Each UI tick
UI->>CH: try_recv() drain loop exits
UI->>CH: rx.is_closed()?
CH-->>UI: true
alt is_loading or is_compacting or is_purging
UI->>APP: finalize streaming cells
UI->>APP: reset all per-turn flags
UI->>APP: drain pending_steers to queue
UI->>APP: push error toast
UI->>APP: "needs_redraw = true"
end
end
Reviews (6): Last reviewed commit: "fix: preserve pending steers on engine d..." | Re-trigger Greptile
Thanks @gordonlu for taking the time to contribute.
This repository is currently observing a maintainer-managed contribution gate in dry-run mode, so this pull request is staying open. When enforcement is enabled, pull requests from contributors who are not listed in .github/APPROVED_CONTRIBUTORS will be closed automatically.
Please read CONTRIBUTING.md for the expected contribution shape. A maintainer can grant PR access by commenting /lgtm on a pull request.
Want your agent to iterate on Greptile's feedback? Try greploops.
This was harvested into the v0.8.51 release — thank you for the contribution!