Skip to content

[codex] Add async subagent startup for thread spawns - #23575

Closed
cassirer-openai wants to merge 4 commits into
mainfrom
codex/async-subagent-startup
Closed

cassirer-openai wants to merge 4 commits into
mainfrom
codex/async-subagent-startup

Conversation

@cassirer-openai

Copy link
Copy Markdown
Contributor

Summary

This changes spawned MultiAgentV2 thread children so they can register their thread before slow startup work finishes, instead of blocking the parent on required MCP initialization.

The new path is gated behind features.multi_agent_v2.async_subagent_startup and is intentionally limited to pathful ThreadSpawn children. That keeps delayed startup failures compatible with the existing parent notification and mailbox semantics.

What changed

  • add a session startup state machine and deferred startup handoff for eligible spawned subagents
  • register thread-spawn children before background MCP startup, then resume initialization after ThreadManager has published the child
  • keep mailbox-triggered work queued while startup is still initializing
  • reject direct turn-starting requests with explicit errors while startup is initializing or after startup failure
  • notify the parent when deferred startup fails and suppress late failure reporting after shutdown
  • add config/schema coverage for the new flag
  • add targeted tests for pathful gating, queued mailbox behavior, direct input rejection, parent notification, and shutdown suppression

Root cause

Required MCP startup was part of synchronous session creation, so a spawned child could not become visible to its parent until all required MCP servers had either initialized or failed. That made slow MCP startup block thread registration entirely.

Impact

With the flag enabled, the parent can observe the spawned child immediately, while the child stays in PendingInit until deferred startup reaches a terminal state. If required MCP startup fails, the parent receives the same style of subagent notification without needing the child to have started a turn first.

@cassirer-openai
cassirer-openai force-pushed the codex/async-subagent-startup branch from 6a6f1ba to f45a111 Compare May 19, 2026 23:13
@cassirer-openai
cassirer-openai force-pushed the codex/async-subagent-startup branch from 7fde110 to 119cea0 Compare May 20, 2026 00:25
@cassirer-openai

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: 28f465ed5d

ℹ️ 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 on lines +197 to +200
msg: EventMsg::Error(ErrorEvent {
message: "thread startup is still in progress".to_string(),
codex_error_info: Some(CodexErrorInfo::Other),
}),

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.

P2 Badge Avoid marking successful startup as errored

When a direct turn-starting request reaches an async-starting child while startup is still initializing, this request-level rejection is emitted as EventMsg::Error; deliver_event_raw records every error as AgentStatus::Errored, and a later successful transition to SessionStartupState::Ready does not emit any status update to clear it. In that scenario the child can finish startup successfully but remain listed as errored until some later turn happens, so this transient rejection should not use the fatal session error path or should restore status after successful startup.

Useful? React with 👍 / 👎.

{
let mut cancel_guard = self.services.mcp_startup_cancellation_token.lock().await;
cancel_guard.cancel();
*cancel_guard = CancellationToken::new();

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.

P2 Badge Preserve cancelled startup before MCP init

When an async-spawned child is shut down or interrupted immediately after registration but before the deferred startup task first polls, cancel_mcp_startup() cancels the initial token; this assignment then replaces that already-cancelled token with a fresh one, so MCP startup and any required-server wait proceed after the child was stopped. Please preserve the cancelled state or return early when the agent is already shut down before starting MCP initialization.

Useful? React with 👍 / 👎.

// settled so the rest of the session never observes a half-ready
// manager through the shared RwLock.
let mut manager_guard = self.services.mcp_connection_manager.write().await;
*manager_guard = mcp_connection_manager;

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.

P2 Badge Check shutdown before publishing MCP manager

When async subagent startup is enabled, a child can receive Op::Shutdown while finish_initial_startup is still building otherwise healthy MCP clients. shutdown_session_runtime only shuts down the manager currently stored on the session; this assignment can replace that already-shut-down manager after the shutdown path has passed, so the newly created MCP clients never go through begin_shutdown and can leave server processes/resources running.

Useful? React with 👍 / 👎.

Comment on lines +104 to +107
let session_configuration = {
let state = self.state.lock().await;
state.session_configuration.clone()
};

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.

P2 Badge Re-read settings before deferred MCP startup

For an async-spawned child, Op::ThreadSettings is still accepted while the session is PendingInit; if the caller changes cwd/environments/permission profile after this snapshot is taken but before MCP initialization finishes, the new manager is built from stale settings and the first turn can run with MCP servers started in the wrong environment or permission profile. Either gate settings updates during initialization or re-read the current configuration immediately before constructing the MCP runtime/manager.

Useful? React with 👍 / 👎.

Comment on lines +200 to +201
let failures = mcp_connection_manager
.required_startup_failures(&required_mcp_servers)

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.

P2 Badge Route startup elicitations to the live manager

When a required MCP server asks for user elicitation during startup, the request is stored in the newly created mcp_connection_manager and an ElicitationRequest event is sent, but this wait happens before that manager is published to self.services.mcp_connection_manager. In that window Op::ResolveElicitation still targets the old uninitialized manager, so the user's response cannot be delivered and the required server can time out or fail despite approval.

Useful? React with 👍 / 👎.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant