mirror of
https://github.com/openai/codex.git
synced 2026-11-09 20:50:33 +00:00
Centralize Guardian context mode and checkpoint policy (#43458)
## What changed Resolve `GuardianContextMode` once at session construction and share it across history retention, replay, evidence capture, compaction, and review consumers. Extract synchronous reviewer context policy and consolidate Guardian V2 parent checkpoint selection so sampling and fast approval use the same eligibility checks. Preserve legacy checkpoint omission and strict compatibility requirements for thread-owned context. ## Testing Adapt existing review and history tests to the explicit context mode. Move checkpoint tests alongside the extracted selector, retaining coverage for latest-item identity, unusable checkpoints, and complete-item size limits. GitOrigin-RevId: c73fb6fea556921acfffe758a5f709d7494019dd
This commit is contained in:
@@ -1,3 +1,4 @@
|
||||
use crate::context::GuardianContextMode;
|
||||
use std::borrow::Cow;
|
||||
use std::collections::HashMap;
|
||||
use std::collections::HashSet;
|
||||
@@ -3777,7 +3778,7 @@ impl Session {
|
||||
for envelope in &mut items {
|
||||
Self::assign_missing_response_item_id(&mut envelope.item);
|
||||
}
|
||||
if self.enabled(Feature::GuardianThreadContext)
|
||||
if self.guardian_context_mode == GuardianContextMode::ThreadOwned
|
||||
&& let Some(checkpoint) = items.iter_mut().rev().find(|envelope| {
|
||||
matches!(
|
||||
envelope.item,
|
||||
|
||||
@@ -1,6 +1,6 @@
|
||||
//! Orders retained inputs and records host-verified facts at the context checkpoint boundary.
|
||||
|
||||
use codex_features::Feature;
|
||||
use crate::context::GuardianContextMode;
|
||||
use codex_history::RetainedContextEvent;
|
||||
use codex_history::RolloutItem;
|
||||
|
||||
@@ -10,7 +10,7 @@ use super::thread_settings;
|
||||
impl Session {
|
||||
/// Legacy mode neither reserves a sequence nor takes the session-state lock.
|
||||
pub(crate) async fn reserve_user_input_order(&self) -> Option<u64> {
|
||||
if !self.enabled(Feature::GuardianThreadContext) {
|
||||
if self.guardian_context_mode == GuardianContextMode::Legacy {
|
||||
return None;
|
||||
}
|
||||
Some(self.state.lock().await.history.reserve_input_order())
|
||||
|
||||
@@ -1,4 +1,5 @@
|
||||
use super::*;
|
||||
use crate::context::GuardianContextMode;
|
||||
use crate::context::world_state::WorldStateSnapshot;
|
||||
use crate::context_manager::is_user_turn_boundary;
|
||||
use codex_history::ResponseItemEnvelope;
|
||||
@@ -338,10 +339,7 @@ impl Session {
|
||||
)
|
||||
.unwrap_or(u64::MAX);
|
||||
|
||||
let mut history = ContextManager::new();
|
||||
if self.enabled(Feature::GuardianThreadContext) {
|
||||
history.enable_user_message_retention();
|
||||
}
|
||||
let mut history = ContextManager::with_guardian_context_mode(self.guardian_context_mode);
|
||||
let mut saw_legacy_compaction_without_replacement_history = false;
|
||||
if let Some(checkpoint) = base_compaction
|
||||
&& let Some(items) = &checkpoint.compacted.replacement_history
|
||||
@@ -389,11 +387,12 @@ impl Session {
|
||||
// prompt shape.
|
||||
// TODO(ccunningham): if we drop support for None replacement_history compaction items,
|
||||
// we can get rid of this second loop entirely and just build `history` directly in the first loop.
|
||||
let identity = if self.enabled(Feature::GuardianThreadContext) {
|
||||
compact::CompactedMessageIdentity::Preserve
|
||||
} else {
|
||||
compact::CompactedMessageIdentity::Regenerate
|
||||
};
|
||||
let identity =
|
||||
if self.guardian_context_mode == GuardianContextMode::ThreadOwned {
|
||||
compact::CompactedMessageIdentity::Preserve
|
||||
} else {
|
||||
compact::CompactedMessageIdentity::Regenerate
|
||||
};
|
||||
let user_messages = compact::collect_annotated_user_messages(
|
||||
history.annotated_items(),
|
||||
identity,
|
||||
|
||||
@@ -7,6 +7,7 @@ use super::step_settings::StepSettingsUpdate;
|
||||
use super::*;
|
||||
use crate::agents_md_manager::AgentsMdManager;
|
||||
use crate::config::ConstraintError;
|
||||
use crate::context::GuardianContextMode;
|
||||
use crate::environment_selection::ThreadEnvironments;
|
||||
use crate::environment_selection::TurnEnvironmentSnapshot;
|
||||
use crate::hook_mcp_executor::CoreHookMcpExecutor;
|
||||
@@ -54,6 +55,7 @@ pub(crate) struct Session {
|
||||
/// The set of enabled features should be invariant for the lifetime of the
|
||||
/// session.
|
||||
pub(super) features: ManagedFeatures,
|
||||
pub(crate) guardian_context_mode: GuardianContextMode,
|
||||
pub(crate) windows_sandbox_proxy_settings_mode:
|
||||
codex_sandboxing::WindowsSandboxProxySettingsMode,
|
||||
pub(super) multi_agent_version: OnceLock<MultiAgentVersion>,
|
||||
@@ -840,9 +842,10 @@ impl Session {
|
||||
thread_id.to_string(),
|
||||
thread_extension_init,
|
||||
);
|
||||
// Select the answer path once, before extensions or tool handlers observe the thread.
|
||||
thread_extension_data.insert(crate::context::GuardianReviewEvidence::from_features(
|
||||
&config.features,
|
||||
// Resolve once for live history, replay, and all reviewer consumers.
|
||||
let guardian_context_mode = GuardianContextMode::from_features(&config.features);
|
||||
thread_extension_data.insert(crate::context::GuardianReviewEvidence::new(
|
||||
guardian_context_mode,
|
||||
));
|
||||
// Kick off independent async setup tasks in parallel to reduce startup latency.
|
||||
//
|
||||
@@ -1264,10 +1267,8 @@ impl Session {
|
||||
let mut state = SessionState::new_with_auto_compact_window_ids(
|
||||
session_configuration.clone(),
|
||||
initial_auto_compact_window_ids,
|
||||
guardian_context_mode,
|
||||
);
|
||||
if config.features.enabled(Feature::GuardianThreadContext) {
|
||||
state.history.enable_user_message_retention();
|
||||
}
|
||||
state.base_instructions_provenance = base_instructions_provenance.clone();
|
||||
let managed_network_requirements_configured = config
|
||||
.config_layer_stack
|
||||
@@ -1505,6 +1506,7 @@ impl Session {
|
||||
thread_settings_persistence: Semaphore::new(/*permits*/ 1),
|
||||
managed_network_proxy_refresh_lock: Semaphore::new(/*permits*/ 1),
|
||||
features: config.features.clone(),
|
||||
guardian_context_mode,
|
||||
windows_sandbox_proxy_settings_mode,
|
||||
multi_agent_version,
|
||||
mcp_refresh: McpRefresh::new(),
|
||||
|
||||
@@ -12,6 +12,7 @@ use crate::config::ConfigOverrides;
|
||||
use crate::config::test_config;
|
||||
use crate::context::ContextualUserFragment;
|
||||
use crate::context::DeveloperInstructions;
|
||||
use crate::context::GuardianContextMode;
|
||||
use crate::context::TurnAborted;
|
||||
use crate::environment_selection::EnvironmentConfigOrigin;
|
||||
use crate::environment_selection::ThreadEnvironments;
|
||||
@@ -6620,6 +6621,7 @@ pub(crate) async fn make_session_and_context() -> (Session, TurnContext) {
|
||||
thread_settings_persistence: Semaphore::new(/*permits*/ 1),
|
||||
managed_network_proxy_refresh_lock: Semaphore::new(/*permits*/ 1),
|
||||
features: config.features.clone(),
|
||||
guardian_context_mode: GuardianContextMode::from_features(&config.features),
|
||||
windows_sandbox_proxy_settings_mode:
|
||||
codex_sandboxing::WindowsSandboxProxySettingsMode::Reconcile,
|
||||
multi_agent_version: OnceLock::from(config.multi_agent_version_from_features()),
|
||||
@@ -8784,9 +8786,9 @@ where
|
||||
);
|
||||
|
||||
let mut state = SessionState::new(session_configuration.clone());
|
||||
if config.features.enabled(Feature::GuardianThreadContext) {
|
||||
state.history.enable_user_message_retention();
|
||||
}
|
||||
state.history = ContextManager::with_guardian_context_mode(GuardianContextMode::from_features(
|
||||
&config.features,
|
||||
));
|
||||
let (environment_manager, resolved_turn_environments) =
|
||||
resolved_environments_for_configuration(&session_configuration, &default_environments)
|
||||
.await;
|
||||
@@ -8925,6 +8927,7 @@ where
|
||||
thread_settings_persistence: Semaphore::new(/*permits*/ 1),
|
||||
managed_network_proxy_refresh_lock: Semaphore::new(/*permits*/ 1),
|
||||
features: config.features.clone(),
|
||||
guardian_context_mode: GuardianContextMode::from_features(&config.features),
|
||||
windows_sandbox_proxy_settings_mode:
|
||||
codex_sandboxing::WindowsSandboxProxySettingsMode::Reconcile,
|
||||
multi_agent_version: OnceLock::from(config.multi_agent_version_from_features()),
|
||||
|
||||
Reference in New Issue
Block a user