Preserve content metadata when truncating messages (#40264)

## Why

Rebuilding a truncated message from its content fields can drop passthrough
metadata and leave positional content classifications out of sync with the
items that remain.

## What changed

- Convert message content and its classifications into `AnnotatedContent`
  before applying the token budget, then write both back together.
- Preserve other message and envelope metadata during truncation.
- Assign the `unknown` classification to content from legacy messages that do
  not provide classifications.

## Testing

Expanded truncation coverage to verify that retained text and image items keep
their matching classifications and that unrelated passthrough metadata is
preserved.

GitOrigin-RevId: f84e2ebc3e97392e45f321dec3c171713864bdcd
This commit is contained in:
pakrym-oai
2026-08-23 18:53:15 +00:00
committed by copyberry
parent 2161ec272a
commit 0a80fab240
3 changed files with 123 additions and 29 deletions
@@ -1,5 +1,6 @@
use codex_protocol::models::ContentItem;
use codex_protocol::models::ContentItemKind;
use codex_protocol::models::ResponseItem;
/// Model-visible content paired with its harness-owned classification.
#[derive(Clone, Debug, PartialEq)]
@@ -24,6 +25,11 @@ impl AnnotatedContent {
&self.content
}
/// Returns the model-visible content for an in-place update.
pub fn content_mut(&mut self) -> &mut ContentItem {
&mut self.content
}
/// Returns the classification associated with the content.
pub fn kind(&self) -> &ContentItemKind {
&self.kind
@@ -34,3 +40,59 @@ impl AnnotatedContent {
(self.content, self.kind)
}
}
/// Takes a message's content together with its positional classifications.
///
/// Legacy messages, including persisted rollouts, may not have classifications.
/// Missing entries are classified as unknown so the message remains usable.
pub fn to_annotated_content(item: &mut ResponseItem) -> Option<Vec<AnnotatedContent>> {
let ResponseItem::Message {
content,
internal_chat_message_metadata_passthrough,
..
} = item
else {
return None;
};
let kinds = internal_chat_message_metadata_passthrough
.as_mut()
.and_then(|metadata| metadata.content_item_kinds.take())
.unwrap_or_default();
Some(
std::mem::take(content)
.into_iter()
.zip(kinds.into_iter().chain(std::iter::repeat_with(|| {
ContentItemKind("unknown".to_string())
})))
.map(|(content, kind)| AnnotatedContent::new(content, kind))
.collect(),
)
}
/// Replaces a message's content and positional classifications together.
pub fn set_annotated_content(
item: &mut ResponseItem,
annotated_content: Vec<AnnotatedContent>,
) -> Option<()> {
let ResponseItem::Message {
content,
internal_chat_message_metadata_passthrough,
..
} = item
else {
return None;
};
let (updated_content, content_item_kinds): (Vec<_>, Vec<_>) = annotated_content
.into_iter()
.map(AnnotatedContent::into_parts)
.unzip();
*content = updated_content;
internal_chat_message_metadata_passthrough
.get_or_insert_default()
.content_item_kinds = Some(content_item_kinds);
Some(())
}
+2
View File
@@ -5,5 +5,7 @@ mod fragment;
pub use additional_context::AdditionalContextDeveloperFragment;
pub use additional_context::AdditionalContextUserFragment;
pub use annotated_content::AnnotatedContent;
pub use annotated_content::set_annotated_content;
pub use annotated_content::to_annotated_content;
pub use fragment::ContextualUserFragment;
pub use fragment::RenderedFragment;
+59 -29
View File
@@ -33,6 +33,8 @@ use codex_analytics::CompactionImplementation;
use codex_analytics::CompactionPhase;
use codex_analytics::CompactionReason;
use codex_analytics::CompactionTrigger;
use codex_context_fragments::set_annotated_content;
use codex_context_fragments::to_annotated_content;
use codex_features::Feature;
use codex_history::CodexHarnessMetadata;
use codex_history::ResponseItemEnvelope;
@@ -648,28 +650,15 @@ fn message_text_token_count(item: &ResponseItem) -> usize {
}
fn truncate_message_text_to_token_budget(
envelope: ResponseItemEnvelope,
mut envelope: ResponseItemEnvelope,
max_tokens: usize,
) -> Option<ResponseItemEnvelope> {
let ResponseItemEnvelope {
item,
metadata: harness_metadata,
} = envelope;
let ResponseItem::Message {
id,
role,
content,
phase,
internal_chat_message_metadata_passthrough: passthrough_metadata,
} = item
else {
return None;
};
let content = to_annotated_content(&mut envelope.item)?;
let mut remaining = max_tokens;
let mut truncated_content = Vec::with_capacity(content.len());
for mut content_item in content {
match &mut content_item {
match content_item.content_mut() {
ContentItem::InputText { text } | ContentItem::OutputText { text } => {
if remaining == 0 {
continue;
@@ -696,22 +685,16 @@ fn truncate_message_text_to_token_budget(
return None;
}
Some(ResponseItemEnvelope {
item: ResponseItem::Message {
id,
role,
content: truncated_content,
phase,
internal_chat_message_metadata_passthrough: passthrough_metadata,
},
metadata: harness_metadata,
})
set_annotated_content(&mut envelope.item, truncated_content)?;
Some(envelope)
}
#[cfg(test)]
mod tests {
use super::*;
use codex_protocol::models::ContentItem;
use codex_protocol::models::ContentItemKind;
use codex_protocol::models::InternalChatMessageMetadataPassthrough;
use codex_protocol::models::MessagePhase;
use pretty_assertions::assert_eq;
use tokio::sync::mpsc;
@@ -957,7 +940,20 @@ mod tests {
assert_eq!(
truncated,
vec![
message("user", "midd…1 tokens truncated…1234", /*phase*/ None),
ResponseItem::Message {
id: None,
role: "user".to_string(),
content: vec![ContentItem::InputText {
text: "midd…1 tokens truncated…1234".to_string(),
}],
phase: None,
internal_chat_message_metadata_passthrough: Some(
InternalChatMessageMetadataPassthrough {
content_item_kinds: Some(vec![ContentItemKind("unknown".to_string())]),
..Default::default()
},
),
},
new,
]
);
@@ -979,9 +975,28 @@ mod tests {
ContentItem::OutputText {
text: "uvwxyz".to_string(),
},
ContentItem::InputText {
text: "discarded after the text budget is exhausted".to_string(),
},
ContentItem::InputImage {
image_url: "data:image/png;base64,def".to_string(),
detail: None,
},
],
phase: None,
internal_chat_message_metadata_passthrough: None,
internal_chat_message_metadata_passthrough: Some(
InternalChatMessageMetadataPassthrough {
turn_id: Some("turn-1".to_string()),
content_item_kinds: Some(vec![
ContentItemKind("user.text".to_string()),
ContentItemKind("user.image".to_string()),
ContentItemKind("user.text".to_string()),
ContentItemKind("user.text".to_string()),
ContentItemKind("user.image".to_string()),
]),
..Default::default()
},
),
};
let truncated = truncate_without_metadata(vec![item], /*max_tokens*/ 3);
@@ -1002,9 +1017,24 @@ mod tests {
ContentItem::OutputText {
text: "uv…1 tokens truncated…yz".to_string(),
},
ContentItem::InputImage {
image_url: "data:image/png;base64,def".to_string(),
detail: None,
},
],
phase: None,
internal_chat_message_metadata_passthrough: None,
internal_chat_message_metadata_passthrough: Some(
InternalChatMessageMetadataPassthrough {
turn_id: Some("turn-1".to_string()),
content_item_kinds: Some(vec![
ContentItemKind("user.text".to_string()),
ContentItemKind("user.image".to_string()),
ContentItemKind("user.text".to_string()),
ContentItemKind("user.image".to_string()),
]),
..Default::default()
},
),
}]
);
}