mirror of
https://github.com/openai/codex.git
synced 2026-11-24 23:20:47 +00:00
Restore saved threads when the managed daemon restarts (#44314)
## Why Saved threads need to resume after a managed daemon restart so active goals can continue without waiting for a client to reconnect. ## What changed - Consume the recovery snapshot at startup and restore threads in the background through the shared cold-resume path. - Trigger idle lifecycle work without attaching a client subscription, and leave already-loaded threads untouched. - Continue recovery after individual thread failures and abort background recovery during shutdown. ## Testing Extend the daemon restart integration test to verify goal continuation before client reconnection, restored conversation history and tools, idle-thread restoration, and recovery snapshot consumption. Include malformed snapshots and an invalid thread ID. GitOrigin-RevId: 90e7b8f74ddc0ae327bacde1b2108b9749c65f2a
This commit is contained in:
@@ -460,6 +460,12 @@ pub(crate) struct ThreadRequestProcessor {
|
||||
pub(super) initial_config_warnings: Arc<Vec<ConfigWarningNotification>>,
|
||||
}
|
||||
|
||||
/// Whether resume attaches a client or restores a cold runtime during daemon startup.
|
||||
pub(crate) enum ThreadResumeTarget {
|
||||
Client(ConnectionRequestId),
|
||||
DaemonRecovery,
|
||||
}
|
||||
|
||||
/// Outcome of trying to satisfy a resume request from an already loaded thread.
|
||||
enum RunningThreadResumeResult {
|
||||
/// The request was delegated to the loaded thread.
|
||||
@@ -547,7 +553,7 @@ impl ThreadRequestProcessor {
|
||||
|
||||
pub(crate) async fn thread_resume(
|
||||
&self,
|
||||
request_id: ConnectionRequestId,
|
||||
target: ThreadResumeTarget,
|
||||
params: ThreadResumeParams,
|
||||
app_server_client_name: Option<String>,
|
||||
app_server_client_version: Option<String>,
|
||||
@@ -556,7 +562,7 @@ impl ThreadRequestProcessor {
|
||||
let mut prepared_config = None;
|
||||
// Keep the resume future off the request handler's stack.
|
||||
while Box::pin(self.thread_resume_inner(
|
||||
request_id.clone(),
|
||||
&target,
|
||||
¶ms,
|
||||
app_server_client_name.clone(),
|
||||
app_server_client_version.clone(),
|
||||
@@ -3634,7 +3640,7 @@ impl ThreadRequestProcessor {
|
||||
|
||||
async fn thread_resume_inner(
|
||||
&self,
|
||||
request_id: ConnectionRequestId,
|
||||
target: &ThreadResumeTarget,
|
||||
params: &ThreadResumeParams,
|
||||
app_server_client_name: Option<String>,
|
||||
app_server_client_version: Option<String>,
|
||||
@@ -3648,56 +3654,47 @@ impl ThreadRequestProcessor {
|
||||
.await
|
||||
.contains(&thread_id)
|
||||
{
|
||||
self.outgoing
|
||||
.send_error(
|
||||
request_id,
|
||||
invalid_request(format!(
|
||||
"thread {thread_id} is closing; retry thread/resume after the thread is closed"
|
||||
)),
|
||||
)
|
||||
.await;
|
||||
return Ok(ControlFlow::Break(()));
|
||||
return Err(invalid_request(format!(
|
||||
"thread {thread_id} is closing; retry thread/resume after the thread is closed"
|
||||
)));
|
||||
}
|
||||
|
||||
if params.sandbox.is_some() && params.permissions.is_some() {
|
||||
self.outgoing
|
||||
.send_error(
|
||||
request_id,
|
||||
invalid_request("`permissions` cannot be combined with `sandbox`"),
|
||||
)
|
||||
.await;
|
||||
return Ok(ControlFlow::Break(()));
|
||||
return Err(invalid_request(
|
||||
"`permissions` cannot be combined with `sandbox`",
|
||||
));
|
||||
}
|
||||
let redact_resume_payloads =
|
||||
should_redact_thread_resume_payloads(app_server_client_name.as_deref());
|
||||
|
||||
let _thread_list_state_permit = match self.acquire_thread_list_state_permit().await {
|
||||
Ok(permit) => permit,
|
||||
Err(error) => {
|
||||
self.outgoing.send_error(request_id, error).await;
|
||||
return Ok(ControlFlow::Break(()));
|
||||
}
|
||||
};
|
||||
let stored_thread_from_running_probe = match self
|
||||
.resume_running_thread(
|
||||
&request_id,
|
||||
params,
|
||||
app_server_client_name.clone(),
|
||||
app_server_client_version.clone(),
|
||||
/*cold_resume_history*/ None,
|
||||
)
|
||||
.await
|
||||
{
|
||||
Ok(RunningThreadResumeResult::Handled(completion)) => {
|
||||
// The listener may need this permit to finish the response.
|
||||
drop(_thread_list_state_permit);
|
||||
let _ = completion.await;
|
||||
return Ok(ControlFlow::Break(()));
|
||||
}
|
||||
Ok(RunningThreadResumeResult::NotRunning(stored_thread)) => stored_thread,
|
||||
Err(error) => {
|
||||
self.outgoing.send_error(request_id, error).await;
|
||||
return Ok(ControlFlow::Break(()));
|
||||
let _thread_list_state_permit = self.acquire_thread_list_state_permit().await?;
|
||||
let stored_thread_from_running_probe = match target {
|
||||
ThreadResumeTarget::Client(request_id) => match self
|
||||
.resume_running_thread(
|
||||
request_id,
|
||||
params,
|
||||
app_server_client_name.clone(),
|
||||
app_server_client_version.clone(),
|
||||
/*cold_resume_history*/ None,
|
||||
)
|
||||
.await?
|
||||
{
|
||||
RunningThreadResumeResult::Handled(completion) => {
|
||||
// The listener may need this permit to finish the response.
|
||||
drop(_thread_list_state_permit);
|
||||
let _ = completion.await;
|
||||
return Ok(ControlFlow::Break(()));
|
||||
}
|
||||
RunningThreadResumeResult::NotRunning(stored_thread) => stored_thread,
|
||||
},
|
||||
ThreadResumeTarget::DaemonRecovery => {
|
||||
let thread_id = ThreadId::from_string(¶ms.thread_id)
|
||||
.map_err(|err| invalid_request(format!("invalid thread id: {err}")))?;
|
||||
// Recheck under the same permit as client resume, including after config loading.
|
||||
if self.thread_manager.get_thread(thread_id).await.is_ok() {
|
||||
return Ok(ControlFlow::Break(()));
|
||||
}
|
||||
None
|
||||
}
|
||||
};
|
||||
|
||||
@@ -3747,13 +3744,7 @@ impl ThreadRequestProcessor {
|
||||
Err(error) => Err(error),
|
||||
}
|
||||
};
|
||||
let (thread_history, resume_source_thread) = match resume_result {
|
||||
Ok(value) => value,
|
||||
Err(error) => {
|
||||
self.outgoing.send_error(request_id, error).await;
|
||||
return Ok(ControlFlow::Break(()));
|
||||
}
|
||||
};
|
||||
let (thread_history, resume_source_thread) = resume_result?;
|
||||
if let InitialHistory::Resumed(resumed) = &thread_history
|
||||
&& self
|
||||
.pending_thread_unloads
|
||||
@@ -3770,7 +3761,11 @@ impl ThreadRequestProcessor {
|
||||
matches!(thread.history_mode, ThreadHistoryMode::Paginated).then_some(thread.thread_id)
|
||||
});
|
||||
let paginated_resume = paginated_thread_id.is_some();
|
||||
if paginated_resume && include_turns && prepared_config.is_none() {
|
||||
if paginated_resume
|
||||
&& include_turns
|
||||
&& prepared_config.is_none()
|
||||
&& let ThreadResumeTarget::Client(request_id) = target
|
||||
{
|
||||
self.send_deprecation_notice(
|
||||
request_id.connection_id,
|
||||
PAGINATED_FULL_HISTORY_DEPRECATION_SUMMARY,
|
||||
@@ -3783,6 +3778,9 @@ impl ThreadRequestProcessor {
|
||||
&& let Some((source, _)) = thread_history.get_resumed_session_sources()
|
||||
&& !can_accept_direct_input(thread_history.get_multi_agent_version(), &source)
|
||||
{
|
||||
let ThreadResumeTarget::Client(request_id) = target else {
|
||||
return Ok(ControlFlow::Break(()));
|
||||
};
|
||||
let child_thread_id = resumed_history.conversation_id;
|
||||
self.thread_manager
|
||||
.ensure_multi_agent_v2_child_loaded(child_thread_id)
|
||||
@@ -3808,7 +3806,7 @@ impl ThreadRequestProcessor {
|
||||
};
|
||||
return match self
|
||||
.resume_running_thread(
|
||||
&request_id,
|
||||
request_id,
|
||||
&attach_params,
|
||||
app_server_client_name,
|
||||
app_server_client_version,
|
||||
@@ -3913,20 +3911,9 @@ impl ThreadRequestProcessor {
|
||||
.as_mut()
|
||||
.and_then(|overrides| overrides.remove("approval_policy"))
|
||||
{
|
||||
let approval_policy = match serde_json::from_value(value) {
|
||||
Ok(approval_policy) => approval_policy,
|
||||
Err(err) => {
|
||||
self.outgoing
|
||||
.send_error(
|
||||
request_id,
|
||||
invalid_params(format!(
|
||||
"invalid `approval_policy` config override: {err}"
|
||||
)),
|
||||
)
|
||||
.await;
|
||||
return Ok(ControlFlow::Break(()));
|
||||
}
|
||||
};
|
||||
let approval_policy = serde_json::from_value(value).map_err(|err| {
|
||||
invalid_params(format!("invalid `approval_policy` config override: {err}"))
|
||||
})?;
|
||||
typesafe_overrides.approval_policy = Some(approval_policy);
|
||||
}
|
||||
let has_explicit_model_resume_override =
|
||||
@@ -3959,19 +3946,11 @@ impl ThreadRequestProcessor {
|
||||
_ => {
|
||||
// Config loading can call back into Desktop; release the permit during host work.
|
||||
drop(_thread_list_state_permit);
|
||||
let config = match self
|
||||
let config = self
|
||||
.config_manager
|
||||
.load_for_cwd(request_overrides, typesafe_overrides, history_cwd)
|
||||
.await
|
||||
{
|
||||
Ok(config) => config,
|
||||
Err(err) => {
|
||||
self.outgoing
|
||||
.send_error(request_id, config_load_error(&err))
|
||||
.await;
|
||||
return Ok(ControlFlow::Break(()));
|
||||
}
|
||||
};
|
||||
.map_err(|err| config_load_error(&err))?;
|
||||
*prepared_config = Some(PreparedResumeConfig {
|
||||
state: config_state,
|
||||
config,
|
||||
@@ -3992,7 +3971,12 @@ impl ThreadRequestProcessor {
|
||||
config,
|
||||
thread_history,
|
||||
self.auth_manager.clone(),
|
||||
self.request_trace_context(&request_id).await,
|
||||
match target {
|
||||
ThreadResumeTarget::Client(request_id) => {
|
||||
self.request_trace_context(request_id).await
|
||||
}
|
||||
ThreadResumeTarget::DaemonRecovery => None,
|
||||
},
|
||||
client_mcp_extensions,
|
||||
)
|
||||
.await
|
||||
@@ -4003,6 +3987,21 @@ impl ThreadRequestProcessor {
|
||||
session_configured,
|
||||
..
|
||||
}) => {
|
||||
let ThreadResumeTarget::Client(request_id) = target else {
|
||||
// Observe lifecycle events without attaching a client subscription.
|
||||
self.thread_watch_manager
|
||||
.upsert_thread(&thread_id.to_string())
|
||||
.await;
|
||||
// Invoke idle work before arming subscriber-based unloading.
|
||||
codex_thread
|
||||
.emit_thread_idle_lifecycle_if_idle(ThreadIdleCause::Completed)
|
||||
.await;
|
||||
let state = self.thread_state_manager.thread_state(thread_id).await;
|
||||
self.ensure_listener_task_running(thread_id, Arc::clone(&codex_thread), state)
|
||||
.await?;
|
||||
return Ok(ControlFlow::Break(()));
|
||||
};
|
||||
let request_id = request_id.clone();
|
||||
if let Err(err) = Self::set_app_server_client_info(
|
||||
codex_thread.as_ref(),
|
||||
app_server_client_name,
|
||||
@@ -4213,7 +4212,7 @@ impl ThreadRequestProcessor {
|
||||
CodexErrorDetails::InvalidRequest(message) => invalid_request(message.clone()),
|
||||
_ => internal_error(format!("error resuming thread: {err}")),
|
||||
};
|
||||
self.outgoing.send_error(request_id, error).await;
|
||||
return Err(error);
|
||||
}
|
||||
}
|
||||
Ok(ControlFlow::Break(()))
|
||||
|
||||
Reference in New Issue
Block a user