Skip to content

feat: switch on dying sub-agents - #11477

Merged
jif-oai merged 4 commits into
mainfrom
jif/switch-on-dying-agents
Feb 13, 2026
Merged

jif-oai merged 4 commits into
mainfrom
jif/switch-on-dying-agents

Conversation

@jif-oai

@jif-oai jif-oai commented Feb 11, 2026 •

Copy link
Copy Markdown
Contributor

[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)

@jif-oai

jif-oai commented Feb 11, 2026

Copy link
Copy Markdown
Contributor Author

@codex review

@chatgpt-codex-connector chatgpt-codex-connector Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

💡 Codex Review

Here are some automated review suggestions for this pull request.

Reviewed commit: c2fcaf9e89

ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".

Comment thread codex-rs/tui/src/app.rs Resolved
@jif-oai

jif-oai commented Feb 12, 2026

Copy link
Copy Markdown
Contributor Author

@codex review

@chatgpt-codex-connector

Copy link
Copy Markdown
Contributor

Codex Review: Didn't find any major issues. Delightful!

ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".

Document the shutdown intent model in App when agent threads close.
Clarify the difference between thread-switch shutdown suppression and
app-exit shutdown tracking, and document the event-ordering invariants
that prevent unintended failover during user-requested exit.
@joshka-oai

Copy link
Copy Markdown
Contributor

[codex-generated]

Documentation pass follow-up for shutdown/failover clarity in codex-rs/tui/src/app.rs:

  • Clarified dual shutdown intent in App state:
    • suppress_shutdown_complete is a one-shot guard for intentional thread switching.
    • pending_shutdown_exit_thread_id is thread-scoped tracking for user-requested app exit.
  • Documented failover gate conditions in active_non_primary_shutdown_target(...) (event kind, non-primary active thread, and no matching pending-exit marker).
  • Added function-level docs and ordering invariants in handle_active_thread_event(...) so it is explicit why failover handling happens before normal event forwarding.
  • Normalized inline numbered list style to 1. 2. 3. with a blank line before the list per project comment style preference.

Validation run:

  • just fmt
  • cargo test -p codex-tui (pass: 838 passed, 0 failed)

@joshka-oai

Copy link
Copy Markdown
Contributor

[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)

@jif-oai
jif-oai merged commit 851fcc3 into main Feb 13, 2026
36 of 38 checks passed
@jif-oai
jif-oai deleted the jif/switch-on-dying-agents branch February 13, 2026 18:29
@github-actions github-actions Bot locked and limited conversation to collaborators Feb 13, 2026
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants