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
+52 -4
View File
@@ -1,6 +1,8 @@
//! Loads stored agent context and prepares the history a child inherits when it forks.
use crate::agent::control::LocalAgentRuntime;
use crate::agent::types::SpawnAgentOptions;
use crate::compact_remote_v2::retained_client_items;
use crate::config::Config;
use crate::context::BaseInstructionsFragment;
use crate::context::ContextualUserFragment;
@@ -22,6 +24,7 @@ use crate::thread_manager::ThreadManagerState;
use crate::thread_manager::fork_history_from_snapshot;
use codex_context_fragments::set_annotated_content;
use codex_context_fragments::to_annotated_content;
use codex_history::CodexHarnessMetadata;
use codex_history::InitialHistory;
use codex_history::ResponseItemEnvelope;
use codex_history::RolloutItem;
@@ -39,8 +42,51 @@ use codex_protocol::protocol::SubAgentSource;
use codex_protocol::protocol::ThreadHistoryMode;
use codex_thread_store::LoadThreadHistoryParams;
impl LocalAgentRuntime {
pub(crate) async fn retained_items_for_fresh_child(
&self,
initial_history: &InitialHistory,
session_source: &SessionSource,
) -> Vec<ResponseItemEnvelope> {
// Full-history forks already carry their retained items.
let (InitialHistory::New, Some(parent_thread_id)) =
(initial_history, session_source.parent_thread_id())
else {
return Vec::new();
};
let Ok(manager) = self.upgrade() else {
return Vec::new();
};
let Ok(parent) = manager.get_thread(parent_thread_id).await else {
return Vec::new();
};
let history = parent.session.clone_history().await;
let mut items: Vec<_> = retained_client_items(
history.annotated_items(),
&history.file_audio_durations,
ResponseItemEnvelope::is_always_retained,
)
.into_values()
.collect();
for item in &mut items {
let parent_metadata = item.metadata.take().unwrap_or_default();
// Keep item metadata needed for retention and replay. Checkpoints belong to the child.
item.metadata = Some(CodexHarnessMetadata {
retain_across_windows: parent_metadata.retain_across_windows,
retained_source: parent_metadata.retained_source,
history_truncation_token_limit: parent_metadata.history_truncation_token_limit,
image_replay_sources: parent_metadata.image_replay_sources,
inherited_user_message: true,
..Default::default()
});
}
items
}
}
fn keep_forked_rollout_item(item: &RolloutItem, preserve_context_baselines: bool) -> bool {
match item {
RolloutItem::ResponseItem(envelope) if envelope.is_always_retained() => true,
RolloutItem::ResponseItem(envelope) => match &envelope.item {
ResponseItem::Message { role, phase, .. } => match role.as_str() {
"system" | "developer" | "user" => true,
@@ -256,9 +302,11 @@ pub(super) async fn prepare_forked_history(
forked_rollout_items = items;
}
let retain_forked_item = |envelope: &mut ResponseItemEnvelope, replaced: &mut bool| {
if multi_agent_version == MultiAgentVersion::V2
&& (preserve_fork_prefix
|| matches!(&envelope.item, ResponseItem::Message { role, .. } if role == "user" || role == "assistant"))
let explicitly_retained = envelope.is_always_retained();
if explicitly_retained
|| (multi_agent_version == MultiAgentVersion::V2
&& (preserve_fork_prefix
|| matches!(&envelope.item, ResponseItem::Message { role, .. } if role == "user" || role == "assistant")))
{
// Resume must not recapture copied parent messages as local authorization.
envelope
@@ -273,7 +321,7 @@ pub(super) async fn prepare_forked_history(
// Assistant and tool positions belong to the parent counter, not the child.
metadata.user_input_order = None;
}
if preserve_fork_prefix {
if preserve_fork_prefix || explicitly_retained {
return true;
}
let response_item = &mut envelope.item;