Make Guardian thread context capture unconditional (#47689)

## What changed

Always use `GuardianContextMode::ThreadOwned` for session capture and subagent history forks, regardless of `guardianv2.thread_context`. Continue selecting reviewer policy from saved checkpoints, preserving legacy review for incompatible checkpoints and removing the feature gate from reviewer compaction compatibility checks.

## Testing

Update coverage for unconditional retained context across compaction, resume, and subagent forks. Exercise legacy review with saved checkpoints, and verify that local and managed opt-outs do not disable sender context capture or checkpoint migration.

GitOrigin-RevId: cd74e2029c83f12bc655b6563242d06f29dd46af
This commit is contained in:
felixxia-oai
2026-09-23 22:50:48 +00:00
committed by copyberry
parent 1d516d8b3d
commit 920e1a34dc
16 changed files with 169 additions and 343 deletions
+1 -1
View File
@@ -980,7 +980,7 @@ impl LocalAgentControl {
break;
}
}
let context_mode = GuardianContextMode::from_features(&config.features);
let context_mode = GuardianContextMode::ThreadOwned;
let mut replaced_parent_developer_instructions = false;
// Scrub inherited hints and replace only the parent's developer-instruction fragment.
// Compaction stores response items separately, so sanitize both top-level messages and
+5 -22
View File
@@ -2536,18 +2536,10 @@ async fn spawn_agent_can_fork_parent_thread_history_with_sanitized_items() {
.expect("parent shutdown should submit");
}
#[test_case::test_case(true; "thread context enabled")]
#[test_case::test_case(false; "thread context disabled")]
#[tokio::test]
async fn spawn_agent_fork_strips_parent_usage_hints_from_compacted_history(
thread_context_enabled: bool,
) {
async fn spawn_agent_fork_strips_parent_usage_hints_from_compacted_history() {
let harness = AgentControlHarness::new().await;
let mut parent_config = harness.config.clone();
parent_config
.features
.set_enabled(Feature::GuardianThreadContext, thread_context_enabled)
.expect("test context mode");
let _ = parent_config.features.enable(Feature::MultiAgentV2);
parent_config.developer_instructions = Some("Parent developer instructions.".to_string());
parent_config.multi_agent_v2.root_agent_usage_hint_text =
@@ -2555,10 +2547,6 @@ async fn spawn_agent_fork_strips_parent_usage_hints_from_compacted_history(
parent_config.multi_agent_v2.subagent_usage_hint_text =
Some("Parent subagent guidance.".to_string());
let mut child_config = harness.config.clone();
child_config
.features
.set_enabled(Feature::GuardianThreadContext, thread_context_enabled)
.expect("test context mode");
let _ = child_config.features.enable(Feature::MultiAgentV2);
child_config.developer_instructions = Some("Child developer instructions.".to_string());
child_config.multi_agent_v2.subagent_developer_instructions =
@@ -2736,17 +2724,12 @@ async fn spawn_agent_fork_strips_parent_usage_hints_from_compacted_history(
),
"a subagent must not inherit its parent review checkpoint",
);
assert_eq!(
history_contains_text(history.raw_items(), "parent-private-release"),
!thread_context_enabled,
"only retained mode changes parent approval inheritance",
assert!(
!history_contains_text(history.raw_items(), "parent-private-release"),
"a subagent must not inherit parent-local approval messages",
);
let mut inherited_context = codex_history::RetainedContext::default();
if thread_context_enabled {
inherited_context.reserve_order();
} else {
inherited_context.mark_user_messages_incomplete();
}
inherited_context.reserve_order();
assert_eq!(history.retained_context(), &inherited_context);
assert!(
history_contains_text(history.raw_items(), "compacted parent summary"),
+3 -4
View File
@@ -342,10 +342,9 @@ async fn run_remote_compact_task_inner_impl(
replacement_history: &replacement_history,
});
}
let reviewer_compaction_hash = if sess.enabled(Feature::GuardianThreadContext)
&& crate::context::GuardianContextMode::from_history(
sess.conversation_history_snapshot().await.as_ref(),
) == crate::context::GuardianContextMode::Legacy
let reviewer_compaction_hash = if crate::context::GuardianContextMode::from_history(
sess.conversation_history_snapshot().await.as_ref(),
) == crate::context::GuardianContextMode::Legacy
&& let Some(review_turn) = sess.turn_context_for_sub_id(&turn_context.sub_id).await
{
// Previous-model compaction must remain compatible with the continuing turn's
@@ -2,8 +2,6 @@
//! Unknown or incompatible checkpoints keep legacy review alongside retained user evidence.
use codex_extension_api::ConversationHistorySnapshot;
use codex_features::Feature;
use codex_features::Features;
use codex_history::ResponseItemEnvelope;
/// Selects legacy compatibility or thread-owned evidence.
@@ -24,14 +22,6 @@ impl GuardianContextMode {
}
}
pub(crate) fn from_features(features: &Features) -> Self {
if features.enabled(Feature::GuardianThreadContext) {
Self::ThreadOwned
} else {
Self::Legacy
}
}
pub(crate) fn for_checkpoint(
self,
items: &[ResponseItemEnvelope],
@@ -27,10 +27,6 @@ async fn run_review_preserves_evidence_during_parent_compaction() {
codex_login::CodexAuth::from_api_key("Test API Key"),
Vec::new(),
|config| {
config
.features
.enable(Feature::GuardianThreadContext)
.unwrap();
config.features.disable(Feature::TokenBudget).unwrap();
},
)
@@ -64,7 +60,7 @@ async fn run_review_preserves_evidence_during_parent_compaction() {
&params.spawn_config,
parent.inherited_instructions().await,
params.parent_history.history_version(),
parent.guardian_context_mode,
GuardianContextMode::ThreadOwned,
)
.with_environments(params.parent_context.environments())
.with_node_repl_policy_eligibility(
@@ -298,7 +294,7 @@ async fn spawned_guardian_reuse_key_matches_inherited_instructions() {
..Default::default()
},
/*parent_history_version*/ 0,
parent.guardian_context_mode,
GuardianContextMode::ThreadOwned,
);
let expected_key = GuardianReviewSessionReuseKey {
user_instructions: latest_global,
@@ -448,17 +444,14 @@ async fn guardian_review_session_config_change_invalidates_cached_session() {
);
}
#[test_case::test_case(true; "thread owned")]
#[test_case::test_case(false; "legacy")]
#[test_case::test_case(GuardianContextMode::ThreadOwned; "thread owned")]
#[test_case::test_case(GuardianContextMode::Legacy; "legacy checkpoint")]
#[tokio::test]
async fn encrypted_parent_compaction_requires_original_item_id(thread_context_enabled: bool) {
async fn encrypted_parent_compaction_requires_original_item_id(mode: GuardianContextMode) {
let (session, _) = crate::session::tests::make_session_and_context().await;
let mut features = session.get_config().await.features.clone();
features
.set_enabled(Feature::GuardianThreadContext, thread_context_enabled)
.expect("context mode");
let policy =
ReviewContextPolicy::for_context(GuardianContextMode::from_features(&features), &features);
let features = session.get_config().await.features.clone();
let policy = ReviewContextPolicy::for_context(mode, &features);
let item = ResponseItem::Compaction {
id: Some(codex_protocol::ResponseItemId::from_server(
"cmp_guardian_parent_summary".to_string(),
@@ -493,7 +486,7 @@ async fn encrypted_parent_compaction_requires_original_item_id(thread_context_en
);
history.replace_annotated(items);
let result = policy.parent_compaction(&history);
if thread_context_enabled {
if mode == GuardianContextMode::ThreadOwned {
assert!(result.is_err());
} else {
assert_eq!(result.expect("legacy omission"), None);
+2 -2
View File
@@ -988,8 +988,8 @@ impl Session {
thread_id.to_string(),
thread_extension_init,
);
// Capture follows the flag; replay selects reviewer policy from the saved checkpoint.
let guardian_context_mode = GuardianContextMode::from_features(&config.features);
// Capture is unconditional; replay selects reviewer policy from the saved checkpoint.
let guardian_context_mode = GuardianContextMode::ThreadOwned;
thread_extension_data.insert(crate::context::GuardianReviewEvidence::default());
// Kick off independent async setup tasks in parallel to reduce startup latency.
//
+4 -4
View File
@@ -6403,7 +6403,7 @@ pub(crate) async fn make_session_and_context() -> (Session, TurnContext) {
let mut state = SessionState::new(session_configuration.clone());
state.history = ContextManager::with_guardian_context_mode(
GuardianContextMode::from_features(&config.features),
GuardianContextMode::ThreadOwned,
&session_configuration.session_source,
);
let (environment_manager, resolved_environments) =
@@ -6551,7 +6551,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),
guardian_context_mode: GuardianContextMode::ThreadOwned,
isolation: codex_extension_api::SessionIsolation::Inherit,
tool_policy: Arc::default(),
windows_sandbox_proxy_settings_mode:
@@ -8675,7 +8675,7 @@ where
let mut state = SessionState::new(session_configuration.clone());
state.history = ContextManager::with_guardian_context_mode(
GuardianContextMode::from_features(&config.features),
GuardianContextMode::ThreadOwned,
&session_configuration.session_source,
);
let (environment_manager, resolved_turn_environments) =
@@ -8822,7 +8822,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),
guardian_context_mode: GuardianContextMode::ThreadOwned,
isolation: codex_extension_api::SessionIsolation::Inherit,
tool_policy: Arc::default(),
windows_sandbox_proxy_settings_mode: