mirror of
https://github.com/pchuan98/codex.git
synced 2026-07-01 00:31:56 +08:00
[codex-generated] ## Updated PR Description (Ready To Paste) ## Problem When a sub-agent thread emits `ShutdownComplete`, the TUI switches back to the primary thread. That was also happening for user-requested exits (for example `Ctrl+C`), which could prevent a clean app exit and unexpectedly resurrect the main thread. ## Mental model The app has one primary thread and one active thread. A non-primary active thread shutting down usually means "agent died, fail back to primary," but during `ExitMode::ShutdownFirst` shutdown means "the user is exiting," not "recover this session." ## Non-goals No change to thread lifecycle, thread-manager ownership, or shutdown protocol wire format. No behavioral changes to non-shutdown events. ## Tradeoffs This adds a small local marker (`pending_shutdown_exit_thread_id`) instead of inferring intent from event timing. It is deterministic and simple, but relies on correctly setting and clearing that marker around exit. ## Architecture `App` tracks which thread is intentionally being shut down for exit. `active_non_primary_shutdown_target` centralizes failover eligibility for `ShutdownComplete` and skips failover when shutdown matches the pending-exit thread. `handle_active_thread_event` handles non-primary failover before generic forwarding and clears the pending-exit marker only when the matching active thread completes shutdown. ## Observability User-facing info/error messages continue to indicate whether failover to the main thread succeeded. The shutdown-intent path is now explicitly documented inline for easier debugging. ## Tests Added targeted tests for `active_non_primary_shutdown_target` covering non-shutdown events, primary-thread shutdown, non-primary shutdown failover, pending exit on active thread (no failover), and pending exit for another thread (still failover). Validated with: - `cargo test -p codex-tui` (pass) --------- Co-authored-by: Josh McKinney <joshka@openai.com>