Keep explicitly pinned history outside the carryover token budget (#62677)

## Why

An exhausted carryover budget could drop explicitly pinned items, and an
oversized pin could crowd out ordinary user messages during local compaction.

## What changed

Preserve eligible pins in full and in their original order for the current
thread, child threads, or both, according to their retention policy. Apply
the carryover budget to the remaining history, including items retained by
features without an explicit pin.

## Testing

Add regression tests for destination-specific pins at zero budget and for
keeping an ordinary user message alongside an oversized pin during local
compaction.

GitOrigin-RevId: fc8529bcc254d8e7a4a010337ce760e94daa4bef
This commit is contained in:
Vivian Fang
2026-11-20 05:28:11 +00:00
committed by copyberry
parent 15c5444e87
commit ea8ccc1d20
4 changed files with 93 additions and 5 deletions
+1 -1
View File
@@ -772,8 +772,8 @@ fn build_compacted_history_with_limit(
else {
continue;
};
// A user already selected by retained_client_items is explicitly pinned.
if selected.contains_key(&position) {
remaining = remaining.saturating_sub(tokens);
continue;
}
let mut harness_metadata = message.harness_metadata.cloned();
+49 -3
View File
@@ -735,7 +735,7 @@ fn truncate_retained_messages(
.collect()
}
/// Select newest first, then return the items at their original positions.
/// Keep explicit pins whole, spend the carryover budget newest first, and restore source order.
fn select_retained_items(
items: &[ResponseItemEnvelope],
should_retain: impl Fn(&ResponseItemEnvelope) -> bool,
@@ -746,11 +746,19 @@ fn select_retained_items(
) -> BTreeMap<usize, ResponseItemEnvelope> {
let mut remaining = max_tokens;
let mut selected = BTreeMap::new();
for group in newest_history_item_groups(items, is_pinned) {
if is_pinned(group.source)
&& group.source.has_explicit_retention_pin()
&& should_retain(group.source)
{
group.keep(&mut selected, group.source.clone());
}
}
for group in newest_history_item_groups(items, is_pinned) {
if remaining == 0 {
break;
}
if !should_retain(group.source) {
if selected.contains_key(&group.source_position) || !should_retain(group.source) {
continue;
}
@@ -778,7 +786,7 @@ fn select_retained_items(
)
.max(1)
});
// Explicitly retained items and client developer messages also pay for their media.
// Feature-retained client items pay for their media.
let content_tokens =
retained_message_token_count(&group.source.item, image_budget, audio_durations);
let source_tokens = if charge_full_item {
@@ -1171,6 +1179,44 @@ mod tests {
}
}
#[test]
fn explicit_pins_survive_an_exhausted_budget_only_in_their_destination() {
use codex_protocol::turn_input::InjectedResponseItem;
use codex_protocol::turn_input::ItemRetention;
let pin = |text, retention| {
ResponseItemEnvelope::from(InjectedResponseItem {
item: message("developer", text, /*phase*/ None),
retention,
})
};
let shared = pin("shared", ItemRetention::Always);
let thread = pin("thread", ItemRetention::AlwaysInThread);
let child = pin("child", ItemRetention::AlwaysInChildren);
let mut configured = pin("feature-retained developer log", ItemRetention::Unspecified);
configured
.metadata
.get_or_insert_default()
.retain_across_windows = Some(true);
let items = [shared.clone(), thread.clone(), child.clone(), configured];
let in_child: fn(&ResponseItemEnvelope) -> bool =
|item| item.child_retention() == Some(true);
for (destination, expected) in [
(in_child, [shared.clone(), child]),
(ResponseItemEnvelope::is_always_retained, [shared, thread]),
] {
let selected = select_retained_items(
&items,
destination,
destination,
/*max_tokens*/ 0,
RetainedImageBudget::Disabled,
&FileAudioDurations::default(),
);
assert_eq!(selected.into_values().collect::<Vec<_>>(), expected);
}
}
#[test]
fn retained_history_truncation_preserves_metadata() {
let item = ResponseItemEnvelope {
+20
View File
@@ -661,6 +661,26 @@ fn build_token_limited_compacted_history_truncates_overlong_user_messages(
assert_eq!(history[1].metadata, None);
}
#[test]
fn local_compaction_keeps_an_ordinary_user_beside_a_pinned_user() {
use codex_protocol::turn_input::InjectedResponseItem;
use codex_protocol::turn_input::ItemRetention;
let ordinary = ResponseItemEnvelope::new(user_message("abcd"));
let pin = ResponseItemEnvelope::from(InjectedResponseItem {
item: user_message("larger than the optional budget"),
retention: ItemRetention::AlwaysInThread,
});
let items = [ordinary.clone(), pin.clone()];
let compacted = build_compacted_history_with_limit(
&items,
"summary",
/*max_tokens*/ 1,
&FileAudioDurations::default(),
);
assert_eq!(&compacted[..2], &[ordinary, pin]);
}
#[test]
fn build_token_limited_compacted_history_appends_summary_message() {
let items = annotated(vec![user_message("first user message")]);
+23 -1
View File
@@ -358,6 +358,28 @@ impl ResponseItemEnvelope {
.or_else(|| metadata.retain_across_windows.filter(|retain| *retain))
}
/// An explicit injection policy exempts the item from the soft carryover budget
/// wherever its policy pins it. Older developer messages and named turn outputs
/// could also have been retained by a feature, so keep their historical pricing.
pub fn has_explicit_retention_pin(&self) -> bool {
let Some(metadata) = &self.metadata else {
return false;
};
if metadata.retain_in_children.is_some() {
return true;
}
self.is_always_retained()
&& match &self.item {
ResponseItem::Message { role, .. } => role != "developer",
ResponseItem::FunctionCallOutput {
name: Some(_),
call_id: None,
..
} => false,
_ => true,
}
}
/// Wraps a raw Responses API item for persisted history.
pub fn new(item: ResponseItem) -> Self {
Self {
@@ -384,7 +406,7 @@ impl From<InjectedResponseItem> for ResponseItemEnvelope {
let (across_windows, in_children) = match injected.retention {
ItemRetention::Unspecified => return envelope,
ItemRetention::Default => (false, None),
ItemRetention::Always => (true, None),
ItemRetention::Always => (true, Some(true)),
ItemRetention::AlwaysInThread => (true, Some(false)),
ItemRetention::AlwaysInChildren => (false, Some(true)),
};