Add per-item retention policies for injected history (#61344)

## What changed

Allow `thread/inject_items` to accept wrapped items with `retention: "default"` or `retention: "always"`, while preserving support for raw response items. Persist explicit policies separately from legacy client-authored metadata.

Carry explicitly retained items through local and remote compaction, context-window changes, and child-agent creation, subject to retention budgets. Preserve history order and keep retained tool exchanges together; require matching calls and outputs to be adjacent in the injection request and both marked `always`.

Update Quinn dependencies and record exceptions for additional Hickory DNSSEC advisories where DNSSEC is disabled.

## Testing

Update injection tests for explicit retention metadata and extend the compaction scenario to cover a retained tool exchange across repeated compaction and restart.

GitOrigin-RevId: 1195a4ed62dc162a62e58bc2ad733262fbabea27
This commit is contained in:
Vivian Fang
2026-11-13 18:46:13 +00:00
committed by copyberry
parent d17887a8d1
commit c33002259e
29 changed files with 578 additions and 300 deletions
+18 -13
View File
@@ -272,6 +272,15 @@ fn compacted_user_message<'a>(text: &str, original: &'a ResponseItem) -> Compact
}
}
fn compact_items(items: &[ResponseItemEnvelope], summary: &str) -> Vec<ResponseItemEnvelope> {
build_compacted_history_with_limit(
items,
summary,
COMPACT_USER_MESSAGE_MAX_TOKENS,
&FileAudioDurations::default(),
)
}
#[tokio::test]
async fn extension_window_prefix_precedes_retained_turns_and_is_not_repeated() {
use codex_extension_api::PreviousWorldStateSection;
@@ -429,7 +438,9 @@ fn collect_annotated_user_messages_extracts_user_text_only(
ResponseItemEnvelope::new(ResponseItem::Other),
];
let collected = collect_annotated_user_messages(&items);
let collected: Vec<_> = annotated_user_messages(&items)
.map(|(_, message)| message)
.collect();
if !preserve_content {
item = user_message(expected_text);
@@ -458,7 +469,7 @@ fn collect_annotated_user_messages_extracts_user_text_only(
}]
);
assert_eq!(
build_compacted_history(Vec::new(), &collected, "summary"),
compact_items(&items, "summary"),
vec![
expected,
ResponseItemEnvelope::new(ContextualUserFragment::into(CompactionSummary::new(
@@ -576,10 +587,10 @@ fn build_token_limited_compacted_history_truncates_overlong_user_messages(
.set_id(Some(ResponseItemId::with_suffix("msg", "long-user")));
original.metadata = Some(CodexHarnessMetadata::default());
let history = super::build_compacted_history_with_limit(
Vec::new(),
&collect_annotated_user_messages(std::slice::from_ref(&original)),
std::slice::from_ref(&original),
"SUMMARY",
max_tokens,
&FileAudioDurations::default(),
);
assert_eq!(history.len(), 2);
@@ -653,12 +664,10 @@ fn build_token_limited_compacted_history_truncates_overlong_user_messages(
#[test]
fn build_token_limited_compacted_history_appends_summary_message() {
let initial_context: Vec<ResponseItemEnvelope> = Vec::new();
let original = user_message("first user message");
let user_messages = collect_user_messages(std::slice::from_ref(&original));
let items = annotated(vec![user_message("first user message")]);
let summary_text = "summary text";
let history = build_compacted_history(initial_context, &user_messages, summary_text);
let history = compact_items(&items, summary_text);
assert!(
!history.is_empty(),
"expected compacted history to include summary"
@@ -704,11 +713,7 @@ fn build_compacted_history_preserves_user_message_passthrough_metadata(attribute
item: original,
metadata: Some(CodexHarnessMetadata::default()),
};
let history = build_compacted_history(
Vec::new(),
&collect_annotated_user_messages(std::slice::from_ref(&original)),
"summary text",
);
let history = compact_items(std::slice::from_ref(&original), "summary text");
assert_eq!(
history,