Preserve content annotations when filtering forked agent history (#40266)

## Why

Developer-message content and its positional `content_item_kinds` metadata must stay aligned when parent history is prepared for a spawned agent.

## What changed

- Filter and rewrite developer-message content as annotated content so each retained item keeps its matching kind.
- Rebuild both message content and annotation metadata together after removing parent-only instructions and multi-agent usage hints.

## Testing

- Extend forked-history coverage to verify retained annotation kinds.
- Assert that spawned child requests contain the same number of content items and annotation kinds.

GitOrigin-RevId: 59748d9e8c4fc06ef8e5b90389bcf40465589f90
This commit is contained in:
pakrym-oai authored and copyberry committed 2026-08-23 19:01:22 +00:00
1 parent 0a80fab240
commit 184ff338ad
3 files changed
+52 -13

No files matched your search

+15 -11
View File
@@ -8,6 +8,8 @@ use crate::context::ManagedDeveloperInstructions;
use crate::context::MultiAgentModeInstructions;
use crate::context::MultiAgentRoleInstructions;
use crate::session::multi_agents::resolve_usage_hints;
use codex_context_fragments::set_annotated_content;
use codex_context_fragments::to_annotated_content;
use codex_extension_api::ExtensionDataInit;
const AGENT_NAMES: &str = include_str!("../agent_names.txt");
@@ -91,15 +93,15 @@ fn keep_forked_rollout_item(item: &RolloutItem, preserve_reference_context_item:
}
fn retain_forked_developer_message(item: &mut ResponseItem, usage_hint_texts: &[String]) -> bool {
let ResponseItem::Message { role, content, .. } = item else {
return true;
};
if role != "developer" {
if !matches!(item, ResponseItem::Message { role, .. } if role == "developer") {
return true;
}
let Some(mut content) = to_annotated_content(item) else {
return false;
};
content.retain(|content_item| {
let ContentItem::InputText { text } = content_item else {
let ContentItem::InputText { text } = content_item.content() else {
return true;
};
@@ -110,7 +112,7 @@ fn retain_forked_developer_message(item: &mut ResponseItem, usage_hint_texts: &[
.iter()
.any(|usage_hint_text| usage_hint_text == text))
});
!content.is_empty()
!content.is_empty() && set_annotated_content(item, content).is_some()
}
async fn load_agent_model_context(
@@ -741,11 +743,12 @@ impl AgentControl {
return false;
}
if let ResponseItem::Message { role, content, .. } = response_item
&& role == "developer"
{
if matches!(response_item, ResponseItem::Message { role, .. } if role == "developer") {
let Some(mut content) = to_annotated_content(response_item) else {
return false;
};
content.retain_mut(|content_item| {
let ContentItem::InputText { text } = content_item else {
let ContentItem::InputText { text } = content_item.content_mut() else {
return true;
};
if ManagedDeveloperInstructions::matches_text(text) {
@@ -777,7 +780,8 @@ impl AgentControl {
*text = text.replace(parent_developer_instructions, replacement);
!text.is_empty()
});
return !content.is_empty();
return !content.is_empty()
&& set_annotated_content(response_item, content).is_some();
}
true