mirror of
https://github.com/openai/codex.git
synced 2026-12-03 00:41:03 +00:00
Centralize agent resume handling in LocalAgentControl (#47244)
## What changed Add `LocalAgentControl::resume_agent` to reuse an existing thread or restore it from its rollout when lookup returns `ThreadNotFound`. Return the agent's current status, metadata, and configuration snapshot. Route the `resume_agent` tool through this method, replacing its status-based resume check and separate restoration helper. GitOrigin-RevId: 2ae0572a030f169eca1760ba0aff1fb9e2ab6651
This commit is contained in:
@@ -76,6 +76,7 @@ mod inspection;
|
||||
mod interrupt;
|
||||
mod legacy;
|
||||
mod residency;
|
||||
mod resume;
|
||||
mod sender_context;
|
||||
mod service_tier;
|
||||
mod spawn;
|
||||
|
||||
@@ -0,0 +1,37 @@
|
||||
//! Reopens an agent only when its runtime is absent, then returns its effective state.
|
||||
//! Rollout and descendant restoration remain in the existing local startup path.
|
||||
|
||||
use super::LocalAgentControl;
|
||||
use crate::agent::types::LiveAgent;
|
||||
use crate::codex_thread::ThreadConfigSnapshot;
|
||||
use crate::config::Config;
|
||||
use codex_protocol::ThreadId;
|
||||
use codex_protocol::error::CodexErrorDetails;
|
||||
use codex_protocol::error::Result as CodexResult;
|
||||
use codex_protocol::protocol::SessionSource;
|
||||
|
||||
impl LocalAgentControl {
|
||||
pub(crate) async fn resume_agent(
|
||||
&self,
|
||||
config: Config,
|
||||
thread_id: ThreadId,
|
||||
source: SessionSource,
|
||||
) -> CodexResult<(LiveAgent, ThreadConfigSnapshot)> {
|
||||
let manager = self.upgrade()?;
|
||||
let thread = match manager.get_thread(thread_id).await {
|
||||
Ok(thread) => thread,
|
||||
Err(err) if matches!(err.details(), CodexErrorDetails::ThreadNotFound(_)) => {
|
||||
Box::pin(self.resume_agent_from_rollout(config, thread_id, source)).await?;
|
||||
manager.get_thread(thread_id).await?
|
||||
}
|
||||
Err(err) => return Err(err),
|
||||
};
|
||||
let agent = LiveAgent {
|
||||
thread_id,
|
||||
metadata: self.get_agent_metadata(thread_id).unwrap_or_default(),
|
||||
status: thread.agent_status().await,
|
||||
};
|
||||
let config = thread.config_snapshot().await;
|
||||
Ok((agent, config))
|
||||
}
|
||||
}
|
||||
@@ -1,11 +1,8 @@
|
||||
use super::*;
|
||||
use crate::agent::child_config::build_agent_resume_config;
|
||||
use crate::agent::next_thread_spawn_depth;
|
||||
use crate::session::session::Session;
|
||||
use crate::session::turn_context::TurnContext;
|
||||
use crate::tools::handlers::multi_agents_spec::create_resume_agent_tool;
|
||||
use codex_tools::ToolSpec;
|
||||
use std::sync::Arc;
|
||||
|
||||
pub(crate) struct Handler;
|
||||
|
||||
@@ -83,46 +80,36 @@ async fn handle_resume_agent(
|
||||
)
|
||||
.await;
|
||||
|
||||
let mut status = session
|
||||
.services
|
||||
.agent_control
|
||||
.get_status(receiver_thread_id)
|
||||
.await;
|
||||
let (receiver_agent, error) = if matches!(status, AgentStatus::NotFound) {
|
||||
match Box::pin(try_resume_closed_agent(
|
||||
&session,
|
||||
&turn,
|
||||
receiver_thread_id,
|
||||
let result = async {
|
||||
let config = build_agent_resume_config(&turn).map_err(FunctionCallError::RespondToModel)?;
|
||||
let source = thread_spawn_source(
|
||||
session.thread_id(),
|
||||
&turn.session_source,
|
||||
child_depth,
|
||||
))
|
||||
/*agent_role*/ None,
|
||||
/*task_name*/ None,
|
||||
)?;
|
||||
Box::pin(
|
||||
session
|
||||
.services
|
||||
.agent_control
|
||||
.resume_agent(config, receiver_thread_id, source),
|
||||
)
|
||||
.await
|
||||
{
|
||||
Ok(()) => {
|
||||
status = session
|
||||
.services
|
||||
.agent_control
|
||||
.get_status(receiver_thread_id)
|
||||
.await;
|
||||
(
|
||||
session
|
||||
.services
|
||||
.agent_control
|
||||
.get_agent_metadata(receiver_thread_id)
|
||||
.unwrap_or(receiver_agent),
|
||||
None,
|
||||
)
|
||||
}
|
||||
Err(err) => {
|
||||
status = session
|
||||
.services
|
||||
.agent_control
|
||||
.get_status(receiver_thread_id)
|
||||
.await;
|
||||
(receiver_agent, Some(err))
|
||||
}
|
||||
}
|
||||
} else {
|
||||
(receiver_agent, None)
|
||||
.map_err(|err| collab_agent_error(receiver_thread_id, err))
|
||||
}
|
||||
.await;
|
||||
let (status, receiver_agent, error) = match result {
|
||||
Ok((agent, _)) => (agent.status, agent.metadata, None),
|
||||
Err(err) => (
|
||||
session
|
||||
.services
|
||||
.agent_control
|
||||
.get_status(receiver_thread_id)
|
||||
.await,
|
||||
receiver_agent,
|
||||
Some(err),
|
||||
),
|
||||
};
|
||||
session
|
||||
.emit_turn_item_completed(
|
||||
@@ -188,27 +175,3 @@ impl ToolOutput for ResumeAgentResult {
|
||||
tool_output_code_mode_result(self, "resume_agent")
|
||||
}
|
||||
}
|
||||
|
||||
async fn try_resume_closed_agent(
|
||||
session: &Arc<Session>,
|
||||
turn: &Arc<TurnContext>,
|
||||
receiver_thread_id: ThreadId,
|
||||
child_depth: i32,
|
||||
) -> Result<(), FunctionCallError> {
|
||||
let config =
|
||||
build_agent_resume_config(turn.as_ref()).map_err(FunctionCallError::RespondToModel)?;
|
||||
Box::pin(session.services.agent_control.resume_agent_from_rollout(
|
||||
config,
|
||||
receiver_thread_id,
|
||||
thread_spawn_source(
|
||||
session.thread_id(),
|
||||
&turn.session_source,
|
||||
child_depth,
|
||||
/*agent_role*/ None,
|
||||
/*task_name*/ None,
|
||||
)?,
|
||||
))
|
||||
.await
|
||||
.map(|_| ())
|
||||
.map_err(|err| collab_agent_error(receiver_thread_id, err))
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user