mirror of
https://github.com/openai/codex.git
synced 2026-11-27 23:31:09 +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 parent
f3f53ee949
commit
8260619cb6
26 files changed
+599
-482
No files matched your search
@@ -1,3 +1,7 @@
|
||||
#[path = "review_session_context.rs"]
|
||||
mod context_policy;
|
||||
use context_policy::ReviewContextPolicy;
|
||||
|
||||
use std::collections::HashMap;
|
||||
use std::collections::HashSet;
|
||||
use std::future::Future;
|
||||
@@ -52,6 +56,7 @@ use crate::config::ManagedFeatures;
|
||||
use crate::config::NetworkProxySpec;
|
||||
use crate::config::Permissions;
|
||||
use crate::context::ContextualUserFragment;
|
||||
use crate::context::GuardianContextMode;
|
||||
use crate::context::GuardianFollowupReviewReminder;
|
||||
use crate::context::GuardianNodeReplPolicy;
|
||||
use crate::context_manager::ContextManager;
|
||||
@@ -228,19 +233,17 @@ impl GuardianReviewSessionReuseKey {
|
||||
spawn_config: &Config,
|
||||
user_instructions: Option<Instructions>,
|
||||
parent_history_version: u64,
|
||||
context_mode: GuardianContextMode,
|
||||
) -> Self {
|
||||
Self {
|
||||
root_authorization_version: None,
|
||||
parent_history_version: if spawn_config
|
||||
.features
|
||||
.enabled(Feature::GuardianReuseParentCompaction)
|
||||
|| spawn_config
|
||||
.features
|
||||
.enabled(Feature::GuardianThreadContext)
|
||||
{
|
||||
parent_history_version
|
||||
} else {
|
||||
0
|
||||
parent_history_version: match ReviewContextPolicy::for_context(
|
||||
context_mode,
|
||||
&spawn_config.features,
|
||||
) {
|
||||
ReviewContextPolicy::Legacy => 0,
|
||||
ReviewContextPolicy::LegacyWithCheckpointReuse
|
||||
| ReviewContextPolicy::ThreadOwned => parent_history_version,
|
||||
},
|
||||
node_repl_auto_review_required: false,
|
||||
node_repl_policy: String::new(),
|
||||
@@ -287,64 +290,6 @@ impl GuardianReviewSessionReuseKey {
|
||||
}
|
||||
}
|
||||
|
||||
fn encrypted_parent_compaction(
|
||||
history: &ContextManager,
|
||||
features: &ManagedFeatures,
|
||||
reviewer_compaction_hash: Option<&str>,
|
||||
) -> anyhow::Result<Option<ResponseItem>> {
|
||||
let strict = features.enabled(Feature::GuardianThreadContext);
|
||||
if !strict && !features.enabled(Feature::GuardianReuseParentCompaction) {
|
||||
return Ok(None);
|
||||
}
|
||||
let Some(envelope) = history.annotated_items().iter().rev().find(|envelope| {
|
||||
matches!(
|
||||
envelope.item,
|
||||
ResponseItem::Compaction { .. } | ResponseItem::ContextCompaction { .. }
|
||||
)
|
||||
}) else {
|
||||
return Ok(None);
|
||||
};
|
||||
|
||||
let item = &envelope.item;
|
||||
let valid = match item {
|
||||
ResponseItem::Compaction {
|
||||
id: Some(_),
|
||||
encrypted_content,
|
||||
..
|
||||
} if !encrypted_content.is_empty() => true,
|
||||
ResponseItem::ContextCompaction {
|
||||
id: Some(_),
|
||||
encrypted_content: Some(encrypted_content),
|
||||
..
|
||||
} if !encrypted_content.is_empty() => true,
|
||||
_ => false,
|
||||
};
|
||||
if !valid && !strict {
|
||||
return Ok(None);
|
||||
}
|
||||
anyhow::ensure!(
|
||||
valid,
|
||||
"parent compaction checkpoint is unusable for Guardian review"
|
||||
);
|
||||
if strict {
|
||||
// A resumed parent may now use a different model. Compare the actual
|
||||
// checkpoint producer with the selected reviewer, not the live parent model.
|
||||
let producer_hash = envelope
|
||||
.metadata
|
||||
.as_ref()
|
||||
.and_then(|metadata| metadata.compaction_model_hash.as_deref());
|
||||
anyhow::ensure!(
|
||||
producer_hash
|
||||
.zip(reviewer_compaction_hash)
|
||||
.is_some_and(|(producer, reviewer)| {
|
||||
!producer.is_empty() && producer == reviewer
|
||||
}),
|
||||
"parent compaction checkpoint is incompatible with the Guardian review model or its compatibility is unknown"
|
||||
);
|
||||
}
|
||||
Ok(Some(item.clone()))
|
||||
}
|
||||
|
||||
pub(crate) fn prompt_cache_key_override_for_review_session(
|
||||
session_source: &SessionSource,
|
||||
parent_thread_id: Option<ThreadId>,
|
||||
@@ -473,20 +418,15 @@ impl GuardianReviewSessionManager {
|
||||
guardian_review_session_config(&parent_session, &parent_turn).await?;
|
||||
let spawn_config = session_config.spawn_config;
|
||||
let parent_history = parent_session.clone_history().await;
|
||||
let root_authorization_version =
|
||||
if parent_session.enabled(Feature::GuardianThreadContext) {
|
||||
parent_session
|
||||
.services
|
||||
.agent_control
|
||||
.root_user_authorization(parent_session.thread_id)
|
||||
.await
|
||||
.map(|snapshot| snapshot.authorization_version)
|
||||
} else {
|
||||
None
|
||||
};
|
||||
let parent_compaction = encrypted_parent_compaction(
|
||||
&parent_history,
|
||||
let context_policy = ReviewContextPolicy::for_context(
|
||||
parent_session.guardian_context_mode,
|
||||
&spawn_config.features,
|
||||
);
|
||||
let root_authorization_version = context_policy
|
||||
.root_authorization_version(&parent_session)
|
||||
.await;
|
||||
let parent_compaction = context_policy.parent_compaction(
|
||||
&parent_history,
|
||||
session_config.compaction_model_hash.as_deref(),
|
||||
)?;
|
||||
let parent_context = GuardianReviewContext::from(parent_turn);
|
||||
@@ -494,6 +434,7 @@ impl GuardianReviewSessionManager {
|
||||
&spawn_config,
|
||||
parent_session.user_instructions().await,
|
||||
parent_history.history_version(),
|
||||
parent_session.guardian_context_mode,
|
||||
)
|
||||
.with_environments(parent_context.environments())
|
||||
.with_node_repl_policy_eligibility(
|
||||
@@ -575,25 +516,16 @@ impl GuardianReviewSessionManager {
|
||||
) -> (GuardianReviewSessionOutcome, GuardianReviewAnalyticsResult) {
|
||||
let deadline = params.deadline;
|
||||
let parent_history = ¶ms.parent_history;
|
||||
let root_authorization_version = if params
|
||||
.parent_session
|
||||
.enabled(Feature::GuardianThreadContext)
|
||||
{
|
||||
params
|
||||
.parent_session
|
||||
.services
|
||||
.agent_control
|
||||
.root_user_authorization(params.parent_session.thread_id)
|
||||
.await
|
||||
.map(|snapshot| snapshot.authorization_version)
|
||||
} else {
|
||||
None
|
||||
};
|
||||
let parent_compaction = match encrypted_parent_compaction(
|
||||
parent_history,
|
||||
let context_policy = ReviewContextPolicy::for_context(
|
||||
params.parent_session.guardian_context_mode,
|
||||
¶ms.spawn_config.features,
|
||||
params.compaction_model_hash.as_deref(),
|
||||
) {
|
||||
);
|
||||
let root_authorization_version = context_policy
|
||||
.root_authorization_version(¶ms.parent_session)
|
||||
.await;
|
||||
let parent_compaction = match context_policy
|
||||
.parent_compaction(parent_history, params.compaction_model_hash.as_deref())
|
||||
{
|
||||
Ok(checkpoint) => checkpoint,
|
||||
Err(error) => {
|
||||
return (
|
||||
@@ -606,6 +538,7 @@ impl GuardianReviewSessionManager {
|
||||
¶ms.spawn_config,
|
||||
params.parent_session.user_instructions().await,
|
||||
parent_history.history_version(),
|
||||
params.parent_session.guardian_context_mode,
|
||||
)
|
||||
.with_environments(params.parent_context.environments())
|
||||
.with_node_repl_policy_eligibility(
|
||||
@@ -626,9 +559,7 @@ impl GuardianReviewSessionManager {
|
||||
.await
|
||||
{
|
||||
Ok(mut state) => {
|
||||
if !params
|
||||
.parent_session
|
||||
.enabled(Feature::GuardianThreadContext)
|
||||
if context_policy != ReviewContextPolicy::ThreadOwned
|
||||
&& parent_compaction.is_none()
|
||||
&& let Some(trunk) = state.trunk.as_ref()
|
||||
{
|
||||
@@ -752,6 +683,7 @@ impl GuardianReviewSessionManager {
|
||||
session.get_config().await.as_ref(),
|
||||
session.user_instructions().await,
|
||||
session.clone_history().await.history_version(),
|
||||
session.guardian_context_mode,
|
||||
);
|
||||
self.state.lock().await.trunk = Some(Arc::new(GuardianReviewSession {
|
||||
reuse_key,
|
||||
@@ -775,6 +707,7 @@ impl GuardianReviewSessionManager {
|
||||
session.get_config().await.as_ref(),
|
||||
session.user_instructions().await,
|
||||
session.clone_history().await.history_version(),
|
||||
session.guardian_context_mode,
|
||||
);
|
||||
self.state
|
||||
.lock()
|
||||
@@ -1129,9 +1062,8 @@ async fn run_review_on_session(
|
||||
.sync_session_approved_hosts_to(&review_session.session.services.network_approval)
|
||||
.await;
|
||||
|
||||
let history = if params
|
||||
.parent_session
|
||||
.enabled(Feature::GuardianThreadContext)
|
||||
let history = if params.parent_session.guardian_context_mode
|
||||
== GuardianContextMode::ThreadOwned
|
||||
{
|
||||
params.parent_history.conversation_history_snapshot()
|
||||
} else {
|
||||
|
||||
Reference in new issue
Block a user