mirror of
https://github.com/openai/codex.git
synced 2026-11-19 22:30:42 +00:00
Load persisted sender context for Guardian reviews (#51734)
## Why Guardian cannot capture sender instructions from another host when the sender thread is absent from the receiver's local runtime. ## What changed Fall back to the authenticated thread store to reconstruct sender history without resuming the sender. Respect compaction and rollback, and label persisted evidence to indicate that newer unpersisted messages may be missing. Limit storage reads to five seconds and capture sender context before acquiring the active-turn lock. Bind the captured evidence to the turn that accepts the delivery, allowing an existing turn to finish while the read is pending. ## Testing Add cross-host scenarios for legacy and paginated history that verify sender evidence reaches only Guardian and excludes rolled-back permissions. Add a gated storage-read test that verifies turn completion remains unblocked and evidence is bound to the newly accepted turn. GitOrigin-RevId: fe896f4e82fe3e32017fe4884846f3fa90a87949
This commit is contained in:
@@ -5,14 +5,15 @@ use codex_history::ResponseItemEnvelope;
|
||||
use codex_history::TurnAttribution;
|
||||
use codex_protocol::protocol::SessionContextWindow;
|
||||
use codex_protocol::protocol::ThreadHistoryMode;
|
||||
use codex_utils_output_truncation::TruncationPolicy;
|
||||
use uuid::Uuid;
|
||||
|
||||
// Return value of `Session::reconstruct_history_from_rollout`, bundling the rebuilt history with
|
||||
// the resume/fork hydration metadata derived from the same replay.
|
||||
#[derive(Debug, PartialEq)]
|
||||
pub(super) struct RolloutReconstruction {
|
||||
pub(crate) struct RolloutReconstruction {
|
||||
pub(super) history: Vec<ResponseItemEnvelope>,
|
||||
pub(super) retained_context: codex_history::RetainedContext,
|
||||
pub(crate) retained_context: codex_history::RetainedContext,
|
||||
pub(super) guardian_history: Option<codex_history::GuardianHistoryCheckpoint>,
|
||||
pub(super) last_started_turn_id: Option<String>,
|
||||
pub(super) previous_turn_settings: Option<PreviousTurnSettings>,
|
||||
@@ -189,6 +190,26 @@ impl Session {
|
||||
&self,
|
||||
turn_context: &TurnContext,
|
||||
rollout_items: &[RolloutItem],
|
||||
) -> RolloutReconstruction {
|
||||
ContextManager::reconstruct_rollout(
|
||||
rollout_items,
|
||||
turn_context.history_mode,
|
||||
ContextManager::for_session(
|
||||
&turn_context.session_source,
|
||||
&turn_context.config.features,
|
||||
),
|
||||
turn_context.model_info().truncation_policy.into(),
|
||||
)
|
||||
}
|
||||
}
|
||||
|
||||
impl ContextManager {
|
||||
/// Replays stored history without starting a session or acquiring its writer.
|
||||
pub(crate) fn reconstruct_rollout(
|
||||
rollout_items: &[RolloutItem],
|
||||
history_mode: ThreadHistoryMode,
|
||||
mut history: Self,
|
||||
truncation_policy: TruncationPolicy,
|
||||
) -> RolloutReconstruction {
|
||||
// Select the compaction and suffix that can affect reconstruction.
|
||||
let has_legacy_compaction_without_window_number =
|
||||
@@ -207,7 +228,7 @@ impl Session {
|
||||
_ => None,
|
||||
})
|
||||
};
|
||||
let input_checkpoint = select_input_compaction(rollout_items, turn_context.history_mode);
|
||||
let input_checkpoint = select_input_compaction(rollout_items, history_mode);
|
||||
let replay_items = input_checkpoint.map_or(rollout_items, |checkpoint| checkpoint.suffix);
|
||||
let resume_metadata =
|
||||
input_checkpoint.and_then(|checkpoint| checkpoint.compacted.resume_metadata.as_ref());
|
||||
@@ -469,10 +490,6 @@ impl Session {
|
||||
.unwrap_or(u64::MAX);
|
||||
|
||||
// Build model-visible history from the selected compaction and its newer suffix.
|
||||
let mut history = ContextManager::for_session(
|
||||
&turn_context.session_source,
|
||||
&turn_context.config.features,
|
||||
);
|
||||
let mut saw_legacy_compaction_without_replacement_history = false;
|
||||
if let Some(checkpoint) = history_checkpoint
|
||||
&& let Some(items) = &checkpoint.compacted.replacement_history
|
||||
@@ -494,17 +511,11 @@ impl Session {
|
||||
history.record_retained_context(event);
|
||||
}
|
||||
RolloutItem::ResponseItem(response_item) => {
|
||||
history.replay_annotated_item(
|
||||
response_item,
|
||||
turn_context.model_info().truncation_policy.into(),
|
||||
);
|
||||
history.replay_annotated_item(response_item, truncation_policy);
|
||||
}
|
||||
RolloutItem::InterAgentCommunication(communication) => {
|
||||
let response_item = communication.to_model_input_item();
|
||||
history.record_items(
|
||||
std::iter::once(&response_item),
|
||||
turn_context.model_info().truncation_policy.into(),
|
||||
);
|
||||
history.record_items(std::iter::once(&response_item), truncation_policy);
|
||||
}
|
||||
RolloutItem::InterAgentCommunicationMetadata { .. } => {}
|
||||
RolloutItem::Compacted(compacted) => {
|
||||
|
||||
@@ -10,6 +10,7 @@
|
||||
//! Host shutdown admission is checked before reserving or starting a new turn.
|
||||
//! Parent-delegated subagent input bypasses drain; automatic starts remain gated.
|
||||
//! Realtime drain refusals are returned to the fanout for ordered session teardown.
|
||||
//! Sender evidence is read without the active-turn lock and bound to the accepted turn.
|
||||
|
||||
use super::TurnInput;
|
||||
use super::session::Session;
|
||||
@@ -19,11 +20,14 @@ use super::thread_settings;
|
||||
use super::turn_context::NewTurnContextOptions;
|
||||
use super::turn_context::TurnContext;
|
||||
use crate::WithTurnExtensionData;
|
||||
use crate::context::ContextualUserFragment;
|
||||
use crate::context::GuardianSenderMessages;
|
||||
use crate::state::ActiveTurn;
|
||||
use crate::state::TurnState;
|
||||
use crate::tasks::RegularTask;
|
||||
use codex_history::CodexHarnessMetadata;
|
||||
use codex_history::ResponseItemEnvelope;
|
||||
use codex_history::SenderUserMessages;
|
||||
use codex_history::UserInputOrigin;
|
||||
use codex_protocol::config_types::ModeKind;
|
||||
use codex_protocol::error::CodexErr;
|
||||
@@ -356,9 +360,11 @@ async fn start_or_steer(
|
||||
start,
|
||||
)
|
||||
.await?;
|
||||
let sender_messages = capture_sender_user_messages(session, &mut input).await;
|
||||
match session
|
||||
.steer_input(
|
||||
&mut input,
|
||||
sender_messages.as_ref(),
|
||||
additional_context.clone(),
|
||||
/*expected_turn_id*/ None,
|
||||
settings.required_active_final_output_json_schema(),
|
||||
@@ -416,8 +422,16 @@ async fn start_or_steer(
|
||||
}
|
||||
let mut task_input = merge_additional_context_input(session, additional_context).await;
|
||||
if has_explicit_input {
|
||||
task_input
|
||||
.push(pending_turn_input(session, input, &turn_context.sub_id, origin).await);
|
||||
task_input.push(
|
||||
pending_turn_input(
|
||||
session,
|
||||
input,
|
||||
sender_messages.as_ref(),
|
||||
&turn_context.sub_id,
|
||||
origin,
|
||||
)
|
||||
.await,
|
||||
);
|
||||
}
|
||||
session
|
||||
.spawn_task(Arc::clone(&turn_context), task_input, RegularTask::new())
|
||||
@@ -447,7 +461,7 @@ async fn start_if_idle(
|
||||
turn_extension_init,
|
||||
} = request;
|
||||
let TurnInputRequest {
|
||||
input,
|
||||
mut input,
|
||||
thread_settings,
|
||||
start,
|
||||
additional_context,
|
||||
@@ -559,13 +573,23 @@ async fn start_if_idle(
|
||||
.await;
|
||||
|
||||
let mut task_input = merge_additional_context_input(session, additional_context).await;
|
||||
let sender_messages = capture_sender_user_messages(session, &mut input).await;
|
||||
match kind {
|
||||
TurnStartKind::User => {
|
||||
session.clear_connector_selection().await;
|
||||
if let SubmittedTurnInput::UserInput { content, .. } = &input {
|
||||
turn_context.session_telemetry.user_prompt(content);
|
||||
}
|
||||
task_input.push(pending_turn_input(session, input, &turn_context.sub_id, origin).await);
|
||||
task_input.push(
|
||||
pending_turn_input(
|
||||
session,
|
||||
input,
|
||||
sender_messages.as_ref(),
|
||||
&turn_context.sub_id,
|
||||
origin,
|
||||
)
|
||||
.await,
|
||||
);
|
||||
}
|
||||
TurnStartKind::Automatic | TurnStartKind::Recovery => {
|
||||
// Empty automatic user input resumes sampling without a new message.
|
||||
@@ -575,7 +599,14 @@ async fn start_if_idle(
|
||||
.extend_pending_input_for_turn_state(
|
||||
turn_state.as_ref(),
|
||||
vec![
|
||||
pending_turn_input(session, input, &turn_context.sub_id, origin).await,
|
||||
pending_turn_input(
|
||||
session,
|
||||
input,
|
||||
sender_messages.as_ref(),
|
||||
&turn_context.sub_id,
|
||||
origin,
|
||||
)
|
||||
.await,
|
||||
],
|
||||
)
|
||||
.await;
|
||||
@@ -627,6 +658,7 @@ async fn steer(
|
||||
match session
|
||||
.steer_input(
|
||||
&mut input,
|
||||
/*sender_messages*/ None,
|
||||
additional_context,
|
||||
Some(expected_turn_id.as_str()),
|
||||
settings.required_active_final_output_json_schema(),
|
||||
@@ -755,9 +787,14 @@ impl Session {
|
||||
clippy::await_holding_invalid_type,
|
||||
reason = "active turn checks and turn state updates must remain atomic"
|
||||
)]
|
||||
#[expect(
|
||||
clippy::too_many_arguments,
|
||||
reason = "sender evidence is prepared separately before acquiring the admission lock"
|
||||
)]
|
||||
async fn steer_input(
|
||||
&self,
|
||||
input: &mut SubmittedTurnInput,
|
||||
sender_messages: Option<&GuardianSenderMessages>,
|
||||
additional_context: BTreeMap<String, AdditionalContextEntry>,
|
||||
expected_turn_id: Option<&str>,
|
||||
required_final_output_json_schema: Option<&Value>,
|
||||
@@ -832,7 +869,10 @@ impl Session {
|
||||
},
|
||||
}
|
||||
}
|
||||
input => pending_turn_input(self, input.clone(), active_turn_id, origin).await,
|
||||
input => {
|
||||
pending_turn_input(self, input.clone(), sender_messages, active_turn_id, origin)
|
||||
.await
|
||||
}
|
||||
};
|
||||
pending_input.push(input);
|
||||
self.input_queue
|
||||
@@ -863,9 +903,28 @@ async fn merge_additional_context_input(
|
||||
.collect()
|
||||
}
|
||||
|
||||
async fn capture_sender_user_messages(
|
||||
session: &Session,
|
||||
input: &mut SubmittedTurnInput,
|
||||
) -> Option<GuardianSenderMessages> {
|
||||
let SubmittedTurnInput::ResponseItem(
|
||||
item @ ResponseItem::FunctionCallOutput { call_id: None, .. },
|
||||
) = input
|
||||
else {
|
||||
return None;
|
||||
};
|
||||
Session::assign_missing_response_item_id(item);
|
||||
session
|
||||
.services
|
||||
.local_agent_runtime
|
||||
.capture_sender_user_messages(item, session.thread_id)
|
||||
.await
|
||||
}
|
||||
|
||||
async fn pending_turn_input(
|
||||
session: &Session,
|
||||
input: SubmittedTurnInput,
|
||||
sender_messages: Option<&GuardianSenderMessages>,
|
||||
turn_id: &str,
|
||||
origin: UserInputOrigin,
|
||||
) -> TurnInput {
|
||||
@@ -878,19 +937,19 @@ async fn pending_turn_input(
|
||||
origin,
|
||||
},
|
||||
},
|
||||
SubmittedTurnInput::ResponseItem(mut item)
|
||||
SubmittedTurnInput::ResponseItem(item)
|
||||
if matches!(
|
||||
&item,
|
||||
ResponseItem::FunctionCallOutput { call_id: None, .. }
|
||||
) =>
|
||||
{
|
||||
Session::assign_missing_response_item_id(&mut item);
|
||||
let metadata = if let Some(messages) = session
|
||||
.services
|
||||
.local_agent_runtime
|
||||
.capture_sender_user_messages(&item, session.thread_id, turn_id)
|
||||
.await
|
||||
{
|
||||
let metadata = if let Some(fragment) = sender_messages {
|
||||
let mut messages = SenderUserMessages {
|
||||
receiver_turn_id: turn_id.to_owned(),
|
||||
receiver_message_id: fragment.delivery.clone(),
|
||||
text: fragment.render(),
|
||||
};
|
||||
messages.bound();
|
||||
Some(CodexHarnessMetadata {
|
||||
user_input_order: Some(session.reserve_user_input_order().await),
|
||||
sender_user_messages: Some(Box::new(messages)),
|
||||
|
||||
Reference in New Issue
Block a user