Repository navigation
[codex] Add async subagent startup for thread spawns - #23575
cassirer-openai wants to merge 4 commits into
Conversation
6a6f1ba to
f45a111
Compare
7fde110 to
119cea0
Compare
|
@codex review |
There was a problem hiding this comment.
💡 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".
| msg: EventMsg::Error(ErrorEvent { | ||
| message: "thread startup is still in progress".to_string(), | ||
| codex_error_info: Some(CodexErrorInfo::Other), | ||
| }), |
There was a problem hiding this comment.
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(); |
There was a problem hiding this comment.
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; |
There was a problem hiding this comment.
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 👍 / 👎.
| let session_configuration = { | ||
| let state = self.state.lock().await; | ||
| state.session_configuration.clone() | ||
| }; |
There was a problem hiding this comment.
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 👍 / 👎.
| let failures = mcp_connection_manager | ||
| .required_startup_failures(&required_mcp_servers) |
There was a problem hiding this comment.
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 👍 / 👎.
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_startupand is intentionally limited to pathfulThreadSpawnchildren. That keeps delayed startup failures compatible with the existing parent notification and mailbox semantics.What changed
ThreadManagerhas published the childRoot 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
PendingInituntil 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.