Sanitize developer context in full-history agent forks (#39641)

## Why

Developer messages can contain both inherited agent policy and unrelated context. Full-history forks need to replace the parent policy without dropping the unrelated content that shares its message.

## What changed

- Filter fork-specific developer instructions by content item instead of excluding an entire developer message.
- Remove inherited multi-agent mode instructions before applying the child agent's current mode.
- Preserve unrelated content items and discard developer messages only when filtering leaves them empty.

## Testing

Extend agent-control and subagent notification tests to cover compound compacted messages, multi-agent mode instructions, and proactive-to-explicit mode transitions.

GitOrigin-RevId: 028834e237d8636c13b17a724574bc8eb09a55ba
This commit is contained in:
evanz-oai authored and copyberry committed 2026-08-20 07:29:43 +00:00
1 parent d944ce83a2
commit 663da53823
5 files changed
+211 -21

No files matched your search

+18 -12
View File
@@ -4,6 +4,7 @@ use crate::agent::role::apply_role_to_config;
use crate::config::PermissionProfileSnapshot;
use crate::context::ContextualUserFragment;
use crate::context::CurrentTimeReminder;
use crate::context::MultiAgentModeInstructions;
use crate::context::MultiAgentRoleInstructions;
use crate::session::multi_agents::resolve_usage_hints;
use codex_extension_api::ExtensionDataInit;
@@ -85,22 +86,27 @@ fn keep_forked_rollout_item(item: &RolloutItem, preserve_reference_context_item:
}
}
fn is_fork_excluded_developer_message(item: &ResponseItem, usage_hint_texts: &[String]) -> bool {
fn retain_forked_developer_message(item: &mut ResponseItem, usage_hint_texts: &[String]) -> bool {
let ResponseItem::Message { role, content, .. } = item else {
return false;
return true;
};
if role != "developer" {
return false;
return true;
}
let [ContentItem::InputText { text }] = content.as_slice() else {
return false;
};
MultiAgentRoleInstructions::matches_text(text)
|| CurrentTimeReminder::matches_text(text)
|| usage_hint_texts
.iter()
.any(|usage_hint_text| usage_hint_text == text)
content.retain(|content_item| {
let ContentItem::InputText { text } = content_item else {
return true;
};
!(MultiAgentRoleInstructions::matches_text(text)
|| MultiAgentModeInstructions::matches_text(text)
|| CurrentTimeReminder::matches_text(text)
|| usage_hint_texts
.iter()
.any(|usage_hint_text| usage_hint_text == text))
});
!content.is_empty()
}
async fn load_agent_model_context(
@@ -724,7 +730,7 @@ impl AgentControl {
if matches!(response_item, ResponseItem::AgentMessage { .. }) {
return false;
}
if is_fork_excluded_developer_message(
if !retain_forked_developer_message(
response_item,
&multi_agent_v2_usage_hint_texts_to_filter,
) {