mirror of
https://github.com/openai/codex.git
synced 2026-11-27 23:31:09 +00:00
Continue interrupted work after managed daemon restarts (#45820)
## Why Daemon recovery reloads threads but leaves interrupted work unfinished. Resume eligible work automatically from the saved conversation, even without a connected client. ## What changed - Attempt one new continuation turn immediately after restoration, including in Plan mode. Mark the old turn interrupted and supply recovery context without creating a user message or granting new authorization. - Require an idle thread, matching permissions, and the same single local environment configured by the thread. Skip completed, aborted, or superseded work and snapshots without environment identity. - Add `continue_turn_if_idle` with an atomic previous-turn check so newer tasks or standalone settings changes invalidate pending continuation. - Preserve the output schema, service tier, and root turn ID, and emit a “Resuming interrupted work” warning when continuation starts. ## Testing Add daemon restart coverage for continuation without a client, Plan mode, permission and environment mismatches, and legacy snapshots. Add core coverage for preserved continuation metadata, absence of user-message events, rejection of superseded continuations, and exclusion of remote execution from recovery snapshots. GitOrigin-RevId: 2b290b75d9399fc58258bcda2e85c74e9b5b3b09
This commit is contained in:
1 parent
7c709f0ffd
commit
f2b5b81f39
21 files changed
+641
-55
No files matched your search
@@ -357,6 +357,23 @@ impl CodexThread {
|
||||
}
|
||||
}
|
||||
|
||||
/// Starts a new internal continuation turn when idle, including in Plan mode.
|
||||
/// Rejects if a newer task has started, even if it has already finished.
|
||||
/// The input must be a response item; it is never treated as user authorization.
|
||||
pub async fn continue_turn_if_idle(
|
||||
&self,
|
||||
request: TurnInputRequest,
|
||||
expected_previous_turn_id: String,
|
||||
) -> CodexResult<TurnInputSubmission> {
|
||||
self.submit_turn_input_with_mode(
|
||||
request,
|
||||
TurnInputMode::ContinueIfIdle {
|
||||
expected_previous_turn_id,
|
||||
},
|
||||
)
|
||||
.await
|
||||
}
|
||||
|
||||
/// Resumes an interrupted regular turn only when the thread is idle.
|
||||
///
|
||||
/// Recovery starts no new user input and preserves the turn ID that was
|
||||
@@ -498,7 +515,9 @@ impl CodexThread {
|
||||
}
|
||||
|
||||
/// Captures a regular turn only after its input is recorded. The caller must flush the rollout.
|
||||
pub async fn interrupted_turn(&self) -> Option<(String, TurnStartOptions)> {
|
||||
pub async fn interrupted_turn(
|
||||
&self,
|
||||
) -> Option<(String, TurnStartOptions, TurnEnvironmentSelection)> {
|
||||
self.session.interrupted_turn().await
|
||||
}
|
||||
|
||||
|
||||
@@ -1,14 +1,18 @@
|
||||
//! Captures recorded, uncanceled regular turns for managed daemon recovery.
|
||||
//! Captures recorded, uncanceled regular turns using one thread-configured local executor.
|
||||
//! Callers must flush the rollout after capture before persisting the snapshot.
|
||||
|
||||
use super::Session;
|
||||
use codex_protocol::protocol::EnvironmentConfigState;
|
||||
use codex_protocol::protocol::TurnEnvironmentSelection;
|
||||
|
||||
/// Turn input and turn-start injections have entered the rollout writer.
|
||||
pub(super) struct RecordedTurnInput;
|
||||
|
||||
impl Session {
|
||||
/// Captures a regular turn only after its input is recorded. The caller must flush the rollout.
|
||||
pub(crate) async fn interrupted_turn(&self) -> Option<(String, crate::TurnStartOptions)> {
|
||||
pub(crate) async fn interrupted_turn(
|
||||
&self,
|
||||
) -> Option<(String, crate::TurnStartOptions, TurnEnvironmentSelection)> {
|
||||
let active = self.active_turn.lock().await;
|
||||
let task = active.as_ref()?.task.as_ref()?;
|
||||
if task.kind != crate::state::TaskKind::Regular || task.cancellation_token.is_cancelled() {
|
||||
@@ -16,6 +20,14 @@ impl Session {
|
||||
}
|
||||
let context = &task.turn_context;
|
||||
context.extension_data.get::<RecordedTurnInput>()?;
|
||||
// Remote identities/configuration are not persisted across daemon restarts.
|
||||
if context.environments.environments.len() != 1 {
|
||||
return None;
|
||||
}
|
||||
let environment = context.environments.single_local_environment()?.selection();
|
||||
if environment.config != EnvironmentConfigState::FromThread {
|
||||
return None;
|
||||
}
|
||||
Some((
|
||||
context.sub_id.clone(),
|
||||
crate::TurnStartOptions {
|
||||
@@ -34,6 +46,7 @@ impl Session {
|
||||
cyber_access_program: context.cyber_access_program,
|
||||
..Default::default()
|
||||
},
|
||||
environment,
|
||||
))
|
||||
}
|
||||
}
|
||||
@@ -1445,6 +1445,12 @@ impl Session {
|
||||
&session_configuration.session_source,
|
||||
),
|
||||
);
|
||||
state.last_started_turn_id = initial_history.get_rollout_items().iter().rev().find_map(|item| {
|
||||
match item {
|
||||
RolloutItem::EventMsg(EventMsg::TurnStarted(event)) => Some(event.turn_id.clone()),
|
||||
_ => None,
|
||||
}
|
||||
});
|
||||
state.base_instructions_provenance = base_instructions_provenance.clone();
|
||||
state.active_disabled_plugin_ids = session_configuration.disabled_plugin_ids.clone();
|
||||
let managed_network_requirements_configured = config
|
||||
|
||||
@@ -50,6 +50,9 @@ pub(super) async fn update(
|
||||
}),
|
||||
})
|
||||
.await;
|
||||
} else {
|
||||
// Standalone settings changes supersede a pending automatic continuation.
|
||||
session.state.lock().await.last_started_turn_id = None;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -216,7 +216,31 @@ pub(super) async fn handle(
|
||||
| SubmittedTurnInput::ResponseItem(_)
|
||||
| SubmittedTurnInput::InterAgentCommunication(_) => TurnStartKind::Automatic,
|
||||
};
|
||||
start_if_idle(session, request, submission_id, kind).await
|
||||
start_if_idle(
|
||||
session,
|
||||
request,
|
||||
submission_id,
|
||||
kind,
|
||||
/*expected_previous_turn_id*/ None,
|
||||
)
|
||||
.await
|
||||
}
|
||||
TurnInputMode::ContinueIfIdle {
|
||||
expected_previous_turn_id,
|
||||
} => {
|
||||
if !matches!(&request.input, SubmittedTurnInput::ResponseItem(_)) {
|
||||
return Err(CodexErr::InvalidRequest(
|
||||
"continuation requires internal response input".to_string(),
|
||||
));
|
||||
}
|
||||
start_if_idle(
|
||||
session,
|
||||
request,
|
||||
submission_id,
|
||||
TurnStartKind::Recovery,
|
||||
Some(expected_previous_turn_id),
|
||||
)
|
||||
.await
|
||||
}
|
||||
TurnInputMode::Steer { expected_turn_id } => {
|
||||
steer(session, request, expected_turn_id, submission_id).await
|
||||
@@ -236,7 +260,14 @@ pub(super) async fn handle_recovery(
|
||||
turn_trigger: Some("retry".to_string()),
|
||||
..start_options
|
||||
});
|
||||
start_if_idle(session, request, submission_id, TurnStartKind::Recovery).await
|
||||
start_if_idle(
|
||||
session,
|
||||
request,
|
||||
submission_id,
|
||||
TurnStartKind::Recovery,
|
||||
/*expected_previous_turn_id*/ None,
|
||||
)
|
||||
.await
|
||||
}
|
||||
|
||||
async fn start_or_steer(
|
||||
@@ -335,11 +366,16 @@ async fn start_or_steer(
|
||||
}
|
||||
}
|
||||
|
||||
#[expect(
|
||||
clippy::await_holding_invalid_type,
|
||||
reason = "the previous turn check and idle reservation must be atomic"
|
||||
)]
|
||||
async fn start_if_idle(
|
||||
session: &Arc<Session>,
|
||||
request: TurnInputRequest,
|
||||
submission_id: String,
|
||||
kind: TurnStartKind,
|
||||
expected_previous_turn_id: Option<String>,
|
||||
) -> CodexResult<TurnInputSubmission> {
|
||||
let TurnInputRequest {
|
||||
input,
|
||||
@@ -392,6 +428,13 @@ async fn start_if_idle(
|
||||
reason: NotSubmittedReason::NotIdle,
|
||||
});
|
||||
}
|
||||
if let Some(expected) = expected_previous_turn_id
|
||||
&& session.state.lock().await.last_started_turn_id.as_ref() != Some(&expected)
|
||||
{
|
||||
return Ok(TurnInputSubmission::NotSubmitted {
|
||||
reason: NotSubmittedReason::Superseded,
|
||||
});
|
||||
}
|
||||
let active_turn = active_turn.get_or_insert_with(ActiveTurn::default);
|
||||
Arc::clone(&active_turn.turn_state)
|
||||
};
|
||||
@@ -445,7 +488,7 @@ async fn start_if_idle(
|
||||
}
|
||||
task_input.push(pending_turn_input(session, input).await);
|
||||
}
|
||||
TurnStartKind::Automatic => {
|
||||
TurnStartKind::Automatic | TurnStartKind::Recovery => {
|
||||
// Empty automatic user input resumes sampling without a new message.
|
||||
if !matches!(&input, SubmittedTurnInput::UserInput { .. }) {
|
||||
session
|
||||
@@ -457,9 +500,6 @@ async fn start_if_idle(
|
||||
.await;
|
||||
}
|
||||
}
|
||||
TurnStartKind::Recovery => {
|
||||
// Recovery resumes an existing turn without a new empty user message.
|
||||
}
|
||||
}
|
||||
session
|
||||
.start_task(turn_context, task_input, RegularTask::new())
|
||||
@@ -508,6 +548,11 @@ async fn steer(
|
||||
}
|
||||
|
||||
impl Session {
|
||||
/// Called under the active-turn lock before running any task or lifecycle callback.
|
||||
pub(crate) async fn record_started_turn(&self, turn_id: &str) {
|
||||
self.state.lock().await.last_started_turn_id = Some(turn_id.to_string());
|
||||
}
|
||||
|
||||
pub(crate) async fn route_realtime_text_input(
|
||||
self: &Arc<Self>,
|
||||
text: String,
|
||||
|
||||
@@ -84,6 +84,9 @@ pub(crate) struct SessionState {
|
||||
/// model/realtime handling on subsequent regular turns (including full-context
|
||||
/// reinjection after resume or `/compact`).
|
||||
previous_turn_settings: Option<PreviousTurnSettings>,
|
||||
/// Latest task admitted in this runtime, retained across completion and history edits.
|
||||
/// Cleared by standalone settings changes to invalidate pending continuation.
|
||||
pub(crate) last_started_turn_id: Option<String>,
|
||||
/// Runtime accounting state for the active auto-compaction window.
|
||||
auto_compact_window: AutoCompactWindow,
|
||||
/// Original request effort for the current model while configuration updates remain active.
|
||||
@@ -127,6 +130,7 @@ impl SessionState {
|
||||
mcp_dependency_prompted: HashSet::new(),
|
||||
additional_context: AdditionalContextStore::default(),
|
||||
previous_turn_settings: None,
|
||||
last_started_turn_id: None,
|
||||
auto_compact_window: AutoCompactWindow::new_with_ids(auto_compact_window_ids),
|
||||
reasoning_effort_pin: ReasoningEffortPin::Unset,
|
||||
startup_prewarm: None,
|
||||
|
||||
@@ -278,6 +278,10 @@ impl Session {
|
||||
self.start_task(turn_context, input, task).await;
|
||||
}
|
||||
|
||||
#[expect(
|
||||
clippy::await_holding_invalid_type,
|
||||
reason = "record the started turn atomically with its active reservation"
|
||||
)]
|
||||
pub(crate) async fn start_task<T: SessionTask>(
|
||||
self: &Arc<Self>,
|
||||
turn_context: Arc<TurnContext>,
|
||||
@@ -309,6 +313,7 @@ impl Session {
|
||||
let (pending_items, _) = self.input_queue.drain_mailbox_input_items().await;
|
||||
let turn_state = {
|
||||
let mut active = self.active_turn.lock().await;
|
||||
self.record_started_turn(&turn_context.sub_id).await;
|
||||
let turn = active.get_or_insert_with(ActiveTurn::default);
|
||||
debug_assert!(turn.task.is_none());
|
||||
Arc::clone(&turn.turn_state)
|
||||
|
||||
Reference in new issue
Block a user