Honor Guardian retention settings for assistant messages (#62828)

## Why

The fixed retention window can discard assistant messages before elastic
Guardian reviews can use them as context for a user's reply.

## What changed

Apply the thread's Guardian retention policy when recording assistant messages,
including confirmed messages sent through the user messaging tool. Keep full
text within the configured record limit and the overall assistant history byte
budget. Preserve an incomplete marker when a confirmed delivery exceeds the
record limit.

Recover the thread's retention settings from saved history and compaction
checkpoints. Fixed-budget reviews continue to use the fixed retention window.

## Testing

Add coverage for record limits during capture and restore, thread-owned
settings recovery, and replay of oversized confirmed deliveries.

GitOrigin-RevId: e828f06824ce6712910a9cc5ef42a438d1ddf27e
This commit is contained in:
ioanbudea-OAI
2026-11-20 20:10:51 +00:00
committed by copyberry
parent 7fcb825b46
commit ab7a93771e
33 changed files with 608 additions and 122 deletions
@@ -126,10 +126,12 @@ impl LocalAgentRuntime {
)
.ok()?;
}
let retention =
codex_history::MessageRetention::saved(&history, source_thread_id);
let context = ContextManager::reconstruct_rollout(
&history,
history_mode,
ContextManager::new,
|ids| ContextManager::new(ids, retention),
// Tool bodies are not sender authorization evidence.
TruncationPolicy::Bytes(0),
)
@@ -54,6 +54,7 @@ impl RootConversationSource for CapturedRootConversation {
let root_history = &self.history;
// Join calls to host-confirmed outputs in this snapshot. Older outputs without
// captured text cannot establish what was sent, including after a hook rewrite.
let mut missing_delivered_context = false;
let delivered_candidates = root_history
.annotated_items()
.iter()
@@ -66,17 +67,14 @@ impl RootConversationSource for CapturedRootConversation {
else {
return None;
};
let text = envelope
.metadata
.as_ref()?
.delivered_assistant_message
.as_deref()?;
Some(RootDeliveredMessage { call_id, text })
let (text, incomplete) = envelope.metadata.as_ref()?.confirmed_delivery()?;
missing_delivered_context |= incomplete;
(!incomplete).then_some(RootDeliveredMessage { call_id, text })
})
.collect::<Vec<_>>();
let retained_context = root_history.retained_context();
let retained_context = budgeting.retained_assistant_view(root_history.retained_context());
let reconciled = ReconciledRetainedContext::new(
Some(retained_context),
Some(&retained_context),
root_history
.annotated_items()
.iter()
@@ -242,7 +240,8 @@ impl RootConversationSource for CapturedRootConversation {
handoff_boundaries: self.handoff_boundaries.clone(),
latest_user_turn_id: reconciled.latest_user_turn_id.clone(),
missing_root_instructions: original_missing_root_instructions,
missing_assistant_context: retained_context.has_omitted_assistant_messages(),
missing_assistant_context: missing_delivered_context
|| retained_context.has_omitted_assistant_messages(),
missing_verified_answers: !retained_context.verified_answers_complete(),
};
codex_guardian_context::prepare_root_context(
+28 -5
View File
@@ -44,6 +44,7 @@ use codex_guardian_context::SectionHistory;
use codex_guardian_context::TranscriptHistory;
use codex_history::CodexHarnessMetadata;
use codex_history::GuardianHistoryCheckpoint;
use codex_history::MessageRetention;
use codex_history::ResponseItemEnvelope;
use codex_history::RetainedContext;
use codex_history::RetainedContextEntry;
@@ -312,7 +313,10 @@ impl SharedConversationHistory {
#[cfg(test)]
impl Default for ContextManager {
fn default() -> Self {
Self::new(codex_thread_context_ids_test_support::thread_context_ids())
Self::new(
codex_thread_context_ids_test_support::thread_context_ids(),
/*retention*/ None,
)
}
}
@@ -321,7 +325,7 @@ impl ContextManager {
&self.context_ids
}
pub(crate) fn new(context_ids: ThreadContextIds) -> Self {
pub(crate) fn new(context_ids: ThreadContextIds, retention: Option<MessageRetention>) -> Self {
Self {
context_ids,
sampling: Default::default(),
@@ -329,7 +333,7 @@ impl ContextManager {
items: Arc::new(Vec::new()),
review_history: None,
review_budgeting: ContextBudgeting::Fixed,
retained_context: Arc::default(),
retained_context: Arc::new(RetainedContext::new(retention)),
rejected_audio_file_ids: Arc::default(),
file_audio_durations: Arc::default(),
guardian_review_mode: GuardianContextMode::ThreadOwned,
@@ -371,6 +375,7 @@ impl ContextManager {
reuse_parent_compaction: bool,
budgeting: ContextBudgeting,
context_ids: ThreadContextIds,
retention: Option<MessageRetention>,
) -> Self {
let independent = !reuse_parent_compaction;
Self {
@@ -385,7 +390,7 @@ impl ContextManager {
review_budgeting: budgeting,
review_history: independent
.then(|| TranscriptHistory::new(/*generation*/ 0, budgeting)),
..Self::new(context_ids)
..Self::new(context_ids, retention)
}
}
@@ -478,6 +483,12 @@ impl ContextManager {
})
.collect::<Vec<_>>()
.join("\n");
if source_role == "assistant"
&& message.complete
&& text == message.text
{
return true;
}
guardian_truncate_text(&text, GUARDIAN_MAX_ROOT_MESSAGE_TOKENS).0
== message.text
})
@@ -637,6 +648,18 @@ impl ContextManager {
) {
for envelope in items {
self.sampling.record(envelope);
if self
.retained_context
.elastic_assistant_record_bytes()
.is_some()
&& let Some((message, _)) =
self.retained_direct_delivery(&envelope.item, envelope.metadata.as_ref())
&& !message.complete
{
let metadata = envelope.metadata.get_or_insert_default();
metadata.delivered_assistant_message = None;
metadata.delivered_assistant_message_incomplete = true;
}
if let Some(source) =
self.record_item_with_metadata(&envelope.item, envelope.metadata.as_ref(), policy)
{
@@ -940,7 +963,7 @@ impl ContextManager {
self.restored_through_sequence = self
.restored_through_sequence
.min(self.sampling.last_sequence);
self.retained_context = Arc::default();
self.retained_context = Arc::new(self.retained_context.empty_with_same_limits());
self.user_message_revision = self.user_message_revision.saturating_add(1);
if let Some(review_history) = &mut self.review_history {
review_history.reset(
@@ -162,6 +162,7 @@ fn root_history() -> ContextManager {
/*reuse_parent_compaction*/ true,
codex_guardian_context::ContextBudgeting::Fixed,
thread_context_ids(),
/*retention*/ None,
)
}
@@ -596,6 +597,7 @@ fn guardian_checkpoint_applies_additional_context_policy_after_resume(include: b
features.enabled(Feature::GuardianReuseParentCompaction),
codex_guardian_context::ContextBudgeting::Fixed,
thread_context_ids(),
/*retention*/ None,
);
let additional_context: ResponseItem = crate::context::AdditionalContextToolOutput::new(
"browser_info",
@@ -640,6 +642,7 @@ fn guardian_checkpoint_applies_additional_context_policy_after_resume(include: b
features.enabled(Feature::GuardianReuseParentCompaction),
codex_guardian_context::ContextBudgeting::Fixed,
thread_context_ids(),
/*retention*/ None,
);
resumed.replace_annotated(compacted);
resumed.restore_review_context(
@@ -2205,6 +2208,90 @@ fn session_end_input_does_not_consume_a_rollback_turn() {
}
}
#[test]
fn oversized_direct_delivery_retains_its_incomplete_marker_across_replay() {
let call_id = "question";
let mut call: ResponseItem = serde_json::from_value(serde_json::json!({
"type": "function_call", "call_id": call_id,
"namespace": "mcp__codex_apps", "name": "user_messaging_send_message",
"arguments": "{}",
}))
.unwrap();
call.set_id(Some(ResponseItemId::with_suffix("call", "question")));
call.set_turn_id_if_missing("turn");
const RECORD_BYTES: usize = 512;
let original = "x".repeat(RECORD_BYTES);
let expected = codex_history::RetainedUserMessage {
turn_id: "turn".to_owned(),
message_id: call.id().map(|id| id.as_str().to_owned()),
text: String::new(),
complete: false,
origin: codex_history::UserInputOrigin::User,
phase: None,
};
let retention = Some(MessageRetention {
thread_id: codex_protocol::ThreadId::new(),
policy: serde_json::from_value(serde_json::json!({"assistant_record_bytes": RECORD_BYTES}))
.unwrap(),
});
let new_history = || ContextManager::new(thread_context_ids(), retention);
let mut live = new_history();
let mut items = [
ResponseItemEnvelope {
item: call,
metadata: Some(CodexHarnessMetadata {
user_input_order: Some(0),
..Default::default()
}),
},
ResponseItemEnvelope {
item: serde_json::from_value(serde_json::json!({
"type": "function_call_output", "call_id": call_id, "output": "Sent.",
}))
.unwrap(),
metadata: Some(CodexHarnessMetadata {
delivered_assistant_message: Some(original),
..Default::default()
}),
},
];
const TEST_HISTORY_TOKENS: usize = 10_000;
live.record_annotated_items(&mut items, TruncationPolicy::Tokens(TEST_HISTORY_TOKENS));
let saved = serde_json::to_vec(&items.map(codex_history::RolloutItem::ResponseItem)).unwrap();
let mut replayed = new_history();
let replay_items = serde_json::from_slice::<Vec<codex_history::RolloutItem>>(&saved)
.unwrap()
.into_iter()
.map(|item| match item {
codex_history::RolloutItem::ResponseItem(envelope) => envelope,
_ => panic!("expected a saved response item"),
})
.collect();
replayed.replace_annotated(replay_items);
replayed.restore_retained_context(/*checkpoint*/ None);
for history in [live, replayed] {
let metadata = history.annotated_items()[1].metadata.as_ref().unwrap();
assert_eq!(
(
metadata.delivered_assistant_message.as_deref(),
metadata.delivered_assistant_message_incomplete
),
(None, true)
);
assert!(history.retained_context().has_omitted_assistant_messages());
let mut entries = history.retained_context().ordered_entries();
let Some((order, codex_history::RetainedContextEntry::AssistantMessage(message))) =
entries.next()
else {
panic!("missing confirmed delivery");
};
assert_eq!(
(order, message),
(codex_history::RetainedContextOrder::Local(0), &expected)
);
}
}
#[test]
fn rollback_removes_assistant_sources_recorded_ahead_of_queued_input() {
let mut history = root_history();
@@ -20,6 +20,7 @@ use codex_history::RetainedUserMessage;
use codex_protocol::items::TurnItem;
use codex_protocol::models::ContentItem;
use codex_protocol::models::ResponseItem;
use codex_utils_string::serialized_json_bytes;
/// Checkpoint items may have been shortened while preserving their original metadata.
pub(super) enum RetainedMessageSource {
@@ -83,7 +84,8 @@ impl ContextManager {
envelope
.metadata
.as_ref()
.is_some_and(|metadata| metadata.delivered_assistant_message.is_some())
.and_then(CodexHarnessMetadata::confirmed_delivery)
.is_some()
}) {
let _ = self.record_retained_message(
&envelope.item,
@@ -112,38 +114,55 @@ impl ContextManager {
}
}
pub(super) fn retained_direct_delivery(
&self,
item: &ResponseItem,
metadata: Option<&CodexHarnessMetadata>,
) -> Option<(RetainedUserMessage, RetainedInputSource)> {
let ResponseItem::FunctionCallOutput {
call_id: Some(call_id),
..
} = item
else {
return None;
};
let (text, incomplete) = metadata?.confirmed_delivery()?;
let call = self.items.iter().rev().find(|envelope| {
matches!(&envelope.item, ResponseItem::FunctionCall { call_id: id, .. } if id == call_id)
})?;
let source = RetainedInputSource::from(call.metadata.as_ref());
if source == RetainedInputSource::Inherited && !self.retain_inherited_user_messages {
return None;
}
// The host captured the confirmed text before post-tool hooks. Use the
// original call's position even when its output is delayed or aborted.
let mut message = RetainedUserMessage {
phase: None,
origin: codex_history::UserInputOrigin::User,
turn_id: call.item.turn_id().unwrap_or_default().to_owned(),
message_id: call.item.id().map(|id| id.as_str().to_owned()),
text: text.to_owned(),
complete: !incomplete,
};
if message.complete
&& let Some(limit) = self.retained_context.elastic_assistant_record_bytes()
&& !serialized_json_bytes(&message).is_ok_and(|bytes| bytes <= limit)
{
message.text = String::new();
message.complete = false;
}
Some((message, source))
}
pub(super) fn record_retained_message(
&mut self,
item: &ResponseItem,
metadata: Option<&CodexHarnessMetadata>,
source: RetainedMessageSource,
) -> Option<codex_history::RetainedSource> {
if let Some(text) =
metadata.and_then(|metadata| metadata.delivered_assistant_message.as_ref())
&& let ResponseItem::FunctionCallOutput {
call_id: Some(call_id),
..
} = item
{
let call = self.items.iter().rev().find(|envelope| {
matches!(&envelope.item, ResponseItem::FunctionCall { call_id: id, .. } if id == call_id)
})?;
let source = RetainedInputSource::from(call.metadata.as_ref());
if source == RetainedInputSource::Inherited && !self.retain_inherited_user_messages {
return None;
}
// The host captured this bounded text before post-tool hooks. The output
// may be rejected, aborted, or truncated; retain the confirmed text at
// the original call's position, not at its later completion position.
if let Some((message, source)) = self.retained_direct_delivery(item, metadata) {
Arc::make_mut(&mut self.retained_context).record_assistant_message(
RetainedUserMessage {
phase: None,
origin: codex_history::UserInputOrigin::User,
turn_id: call.item.turn_id().unwrap_or_default().to_owned(),
message_id: call.item.id().map(|id| id.as_str().to_owned()),
text: text.clone(),
complete: true,
},
message,
source,
&self.context_ids,
);
@@ -181,6 +200,8 @@ impl ContextManager {
complete &= metadata
.and_then(|metadata| metadata.retained_source.as_ref())
.is_none_or(|source| source.complete);
let retain_assistant_original =
is_assistant && self.retained_context.retains_original_assistant_messages();
let text = content
.iter()
.filter_map(|content| match content {
@@ -194,11 +215,15 @@ impl ContextManager {
})
.collect::<Vec<_>>()
.join("\n");
// Keep the same bounded text that child reviewers receive before
// compaction, instead of letting storage discard a large source.
// Local instruction sections still omit incomplete originals whole.
let (text, truncated) = guardian_truncate_text(&text, GUARDIAN_MAX_ROOT_MESSAGE_TOKENS);
complete &= !truncated;
let text = if retain_assistant_original {
// The recorder applies the configured byte limit to the source text.
text
} else {
let (text, truncated) =
guardian_truncate_text(&text, GUARDIAN_MAX_ROOT_MESSAGE_TOKENS);
complete &= !truncated;
text
};
let message = RetainedUserMessage {
phase: phase.clone(),
origin: codex_history::UserInputOrigin::from_message(item),
+11 -2
View File
@@ -111,8 +111,15 @@ pub(super) async fn collect_guardian_prompt_items_with_parent_turn(
.get_guardian_package(session.thread_id)
.await
.and_then(|snapshot| snapshot.request_candidates);
let trusted_user_inputs = history
let budgeting = parent_context.map_or_else(
|| config.guardian_context_budgeting(),
|context| context.turn().config.guardian_context_budgeting(),
);
let retained_context = history
.retained_context()
.map(|context| budgeting.retained_assistant_view(context));
let trusted_user_inputs = retained_context
.as_deref()
.map(codex_guardian_context::collect_verified_answer_candidates)
.unwrap_or_default();
let planned_action = codex_guardian_reviewer::build_guardian_planned_action(
@@ -162,6 +169,7 @@ pub(super) async fn collect_guardian_prompt_items_with_parent_turn(
target: profile.target,
history: &GuardianReviewHistory {
history,
retained_context: retained_context.as_deref(),
use_parent_checkpoint,
},
transcript: &profile.transcript,
@@ -211,12 +219,13 @@ pub(super) fn collect_guardian_context(
struct GuardianReviewHistory<'a> {
history: &'a dyn ConversationHistorySnapshot,
retained_context: Option<&'a codex_history::RetainedContext>,
use_parent_checkpoint: bool,
}
impl SectionHistory for GuardianReviewHistory<'_> {
fn retained_context(&self) -> Option<&codex_history::RetainedContext> {
self.history.retained_context()
self.retained_context
}
fn items(&self) -> Box<dyn Iterator<Item = &ResponseItem> + Send + '_> {
@@ -12,7 +12,7 @@ use pretty_assertions::assert_eq;
#[test]
fn history_sums_measured_and_unknown_file_audio() {
let mut history = ContextManager::new(thread_context_ids());
let mut history = ContextManager::new(thread_context_ids(), /*retention*/ None);
let item = ResponseInputItem::Message {
role: "user".to_string(),
content: ["short", "unknown"]
@@ -119,7 +119,7 @@ async fn producer_bounded_audio_keeps_the_same_parts_as_inline_history() {
)
.unwrap();
codex_utils_audio::prepare_response_items(std::slice::from_mut(&mut inline));
let mut history = ContextManager::new(thread_context_ids());
let mut history = ContextManager::new(thread_context_ids(), /*retention*/ None);
history.record_items([&inline], policy);
let expected =
serde_json::to_value(history.raw_items().last().unwrap()).unwrap()["output"].take();
@@ -41,6 +41,7 @@ async fn guardian_checkpoint_preserves_live_context_without_storage(
/*reuse_parent_compaction*/ true,
codex_guardian_context::ContextBudgeting::Fixed,
thread_context_ids(),
/*retention*/ None,
);
state
.history
@@ -115,6 +116,7 @@ async fn guardian_checkpoint_preserves_live_context_without_storage(
/*reuse_parent_compaction*/ true,
codex_guardian_context::ContextBudgeting::Fixed,
thread_context_ids(),
/*retention*/ None,
);
fork.record_initial_history(InitialHistory::Forked(items))
.await;
+13 -1
View File
@@ -196,7 +196,7 @@ impl Session {
/// Starts recording confirmed delivery synchronously while the tool's admission is held.
pub(crate) fn record_delivered_assistant_message(
self: &Arc<Self>,
message: RetainedUserMessage,
mut message: RetainedUserMessage,
) -> (JoinHandle<()>, watch::Receiver<()>) {
let session = Arc::clone(self);
let persistence_session = Arc::clone(self);
@@ -208,6 +208,18 @@ impl Session {
.await
.unwrap_or_else(|_| unreachable!("communication boundary remains open"));
let mut state = session.state.lock().await;
if !state
.history
.retained_context()
.retains_original_assistant_messages()
{
let (text, truncated) = crate::guardian::guardian_truncate_text(
&message.text,
crate::guardian::GUARDIAN_MAX_ROOT_MESSAGE_TOKENS,
);
message.text = text;
message.complete &= !truncated;
}
let mut event = RetainedContextEvent::DeliveredAssistantMessage {
message,
retained_source: None,
@@ -193,6 +193,16 @@ impl Session {
turn_context: &TurnContext,
rollout_items: &[RolloutItem],
) -> RolloutReconstruction {
let policy = self
.state
.lock()
.await
.session_configuration
.guardian_retention;
let retention = policy.map(|policy| codex_history::MessageRetention {
thread_id: self.thread_id(),
policy,
});
ContextManager::reconstruct_rollout(
rollout_items,
turn_context.history_mode,
@@ -203,6 +213,7 @@ impl Session {
turn_context.config.guardian_reuses_parent_compaction(),
turn_context.config.guardian_context_budgeting(),
ids,
retention,
)
},
turn_context.model_info().truncation_policy.into(),
@@ -132,6 +132,7 @@ async fn sender_context_follows_its_delivery_through_checkpoint_and_rollback() {
/*reuse_parent_compaction*/ true,
codex_guardian_context::ContextBudgeting::Fixed,
thread_context_ids(),
/*retention*/ None,
);
let mut items = Vec::new();
let mut snapshots = Vec::new();
+4
View File
@@ -1298,6 +1298,9 @@ impl Session {
error!("failed to initialize thread persistence: {e:#}");
e
})?;
let retention = session_configuration
.guardian_retention
.map(|policy| codex_history::MessageRetention { thread_id, policy });
// Keep the large initialization future out of callers' stack frames.
let session_result: anyhow::Result<Arc<Self>> = Box::pin(async {
if let InitialHistory::Resumed(resumed) = &mut initial_history {
@@ -1618,6 +1621,7 @@ impl Session {
config.guardian_reuses_parent_compaction(),
config.guardian_context_budgeting(),
context_ids.clone(),
retention,
),
);
if matches!(initial_history, InitialHistory::Resumed(_)) {
+2
View File
@@ -6636,6 +6636,7 @@ pub(crate) async fn make_session_and_context() -> (Session, TurnContext) {
config.guardian_reuses_parent_compaction(),
config.guardian_context_budgeting(),
context_ids.clone(),
/*retention*/ None,
);
let (environment_manager, resolved_environments) =
resolved_environments_for_configuration(&session_configuration, &default_environments)
@@ -9001,6 +9002,7 @@ where
config.guardian_reuses_parent_compaction(),
config.guardian_context_budgeting(),
context_ids.clone(),
/*retention*/ None,
);
let (environment_manager, resolved_turn_environments) =
resolved_environments_for_configuration(&session_configuration, &default_environments)
+5 -1
View File
@@ -1201,7 +1201,11 @@ async fn handle_any_tool(
result: output,
post_tool_use_payload,
};
super::user_messaging::capture_delivery(&result, call_state);
super::user_messaging::capture_delivery(
&result,
call_state,
invocation.turn.config.guardian_context_budgeting(),
);
if result.result.contains_external_context()
&& invocation.turn.config.memories.disable_on_external_context
{
+18 -8
View File
@@ -8,6 +8,7 @@ use crate::session::session::Session;
use crate::tools::context::ToolCallSource;
use crate::tools::context::ToolCallState;
use crate::tools::registry::AnyToolResult;
use codex_guardian_context::ContextBudgeting;
use codex_history::RetainedUserMessage;
use codex_mcp::PreparedMcpCall;
use codex_protocol::mcp::CallToolResult;
@@ -33,11 +34,15 @@ pub(super) fn admit_code_mode_send(
}
/// Captures confirmed delivery before any further await, including post-tool hooks.
pub(super) fn capture_delivery(result: &AnyToolResult, call_state: Option<&ToolCallState>) {
pub(super) fn capture_delivery(
result: &AnyToolResult,
call_state: Option<&ToolCallState>,
budgeting: ContextBudgeting,
) {
if let Some(call_state) = call_state
&& let Some(text) = result.delivered_assistant_message()
&& let Some(delivery) = result.delivered_assistant_message(budgeting)
{
let _ = call_state.delivered_assistant_message.set(text);
let _ = call_state.delivered_assistant_message.set(delivery);
}
}
@@ -70,20 +75,19 @@ pub(crate) fn record_confirmed_code_mode_send(
if text.trim().is_empty() {
return;
}
let (text, truncated) = guardian_truncate_text(text, GUARDIAN_MAX_ROOT_MESSAGE_TOKENS);
let message = RetainedUserMessage {
origin: codex_history::UserInputOrigin::User,
turn_id: turn_id.to_owned(),
message_id: Some(call_id.to_owned()),
text,
complete: !truncated,
text: text.to_owned(),
complete: true,
phase: None,
};
let _ = session.record_delivered_assistant_message(message);
}
impl AnyToolResult {
fn delivered_assistant_message(&self) -> Option<String> {
fn delivered_assistant_message(&self, budgeting: ContextBudgeting) -> Option<String> {
if !self.result.success_for_logging() {
return None;
}
@@ -103,7 +107,13 @@ impl AnyToolResult {
if text.trim().is_empty() {
return None;
}
Some(guardian_truncate_text(text, GUARDIAN_MAX_ROOT_MESSAGE_TOKENS).0)
Some(match budgeting {
ContextBudgeting::Fixed => {
guardian_truncate_text(text, GUARDIAN_MAX_ROOT_MESSAGE_TOKENS).0
}
// History can account for the complete record once it has the call's IDs.
ContextBudgeting::Elastic => text.to_owned(),
})
}
}
@@ -5,6 +5,7 @@ use crate::tools::hook_names::HookToolName;
use crate::tools::registry::AnyToolResult;
use crate::tools::registry::PostToolUsePayload;
use crate::tools::user_messaging::capture_delivery;
use codex_guardian_context::ContextBudgeting;
use pretty_assertions::assert_eq;
use serde_json::Value;
use serde_json::json;
@@ -16,24 +17,40 @@ use test_case::test_case;
#[test_case("mcp__codex_apps__user_messaging__send_message", json!({"text": " "}), true; "empty_text")]
fn undelivered_or_unrelated_messages_have_no_evidence(name: &str, input: Value, success: bool) {
let state = ToolCallState::default();
capture_delivery(&messaging_result(name, input, success), Some(&state));
capture_delivery(
&messaging_result(name, input, success),
Some(&state),
ContextBudgeting::Fixed,
);
assert_eq!(state.delivered_assistant_message.get(), None);
}
#[test]
fn capture_keeps_the_first_confirmed_rewritten_message() {
const LONG_MESSAGE_BYTES: usize = 4_000;
#[test_case(ContextBudgeting::Fixed; "fixed")]
#[test_case(ContextBudgeting::Elastic; "elastic")]
fn capture_keeps_the_first_confirmed_rewritten_message(budgeting: ContextBudgeting) {
let state = ToolCallState::default();
let name = "mcp__codex_apps__user_message__send_message";
for text in ["Staging only?", "Replace the question?"] {
let rewritten = "x".repeat(LONG_MESSAGE_BYTES);
for text in [rewritten.as_str(), "Replace the question?"] {
capture_delivery(
&messaging_result(name, json!({"text": text}), /*success*/ true),
Some(&state),
budgeting,
);
}
assert_eq!(
state.delivered_assistant_message.get().map(String::as_str),
Some("Staging only?")
);
let expected = match budgeting {
ContextBudgeting::Fixed => {
crate::guardian::guardian_truncate_text(
&rewritten,
crate::guardian::GUARDIAN_MAX_ROOT_MESSAGE_TOKENS,
)
.0
}
ContextBudgeting::Elastic => rewritten,
};
assert_eq!(state.delivered_assistant_message.get(), Some(&expected));
}
fn messaging_result(name: &str, input: Value, success: bool) -> AnyToolResult {
@@ -1,5 +1,6 @@
//! Exercises retained review history through compaction, resume, fork, and eviction.
use anyhow::Context;
use anyhow::Result;
use base64::Engine;
use base64::engine::general_purpose::STANDARD as BASE64_STANDARD;
@@ -755,8 +756,10 @@ async fn guardian_storage_policy_applies_before_capture_and_resume(
budgeting: codex_guardian_context::ContextBudgeting,
) -> Result<()> {
use codex_guardian_context as guardian_context;
use codex_history::RetainedContextEntry::AssistantMessage;
const HISTORY_ENTRIES: usize = 140;
const FIXED_ENTRY_LIMIT: usize = 128;
const FIXED_ASSISTANT_LIMIT: usize = 8;
const ENTRY_TOKENS: usize = 900;
skip_if_no_network!(Ok(()));
let server = start_mock_server().await;
@@ -813,6 +816,18 @@ async fn guardian_storage_policy_applies_before_capture_and_resume(
guardian_context::ContextBudgeting::Elastic => originals.as_slice(),
};
assert_eq!(retained, expected);
let assistant_count = history
.retained_context()
.context("retained context")?
.ordered_entries()
.filter(|(_, entry)| matches!(entry, AssistantMessage(_)))
.count();
let expected_count = if budgeting == guardian_context::ContextBudgeting::Elastic {
HISTORY_ENTRIES
} else {
FIXED_ASSISTANT_LIMIT
};
assert_eq!(assistant_count, expected_count);
}
test.codex.shutdown_and_wait().await?;
Ok(())
@@ -40,7 +40,11 @@ async fn cross_host_sender_context(history_mode: ThreadHistoryMode) -> Result<()
.with_thread_store(store.clone())
.with_history_mode(history_mode)
.with_auth(CodexAuth::create_dummy_chatgpt_auth_for_testing())
.with_config(super::configure_scenario_catalog)
.with_config(|config| {
super::configure_scenario_catalog(config);
config.guardian_sync_elastic_budget =
Some(core_test_support::guardian::elastic_budget_config());
})
.build_with_auto_env(&server)
.await?;
// Separate managers share durable storage, as cloud workers do. Neither loads the other.
@@ -98,6 +102,15 @@ async fn cross_host_sender_context(history_mode: ThreadHistoryMode) -> Result<()
ThreadRolledBackEvent { num_turns: 1 },
))])
.await?;
const FIXED_ASSISTANT_LIMIT: usize = 8;
let later = (0..FIXED_ASSISTANT_LIMIT)
.map(|index| {
serde_json::from_value(
ev_assistant_message(&format!("later-{index}"), "Waiting.")["item"].clone(),
)
})
.collect::<Result<Vec<_>, _>>()?;
sender.codex.inject_response_items(later).await?;
sender.codex.flush_rollout().await?;
let expected = [
@@ -26,6 +26,7 @@ use codex_extension_api::ExtensionMetrics;
use codex_extension_api::ExtensionWarning;
use codex_extension_api::McpToolContext;
use codex_extension_api::ResponseItem;
use codex_guardian_context::ContextBudgeting;
use codex_guardian_context::ContextTarget;
use codex_guardian_context::PlannedAction;
use codex_guardian_context::PlannedActionKind;
@@ -84,7 +85,6 @@ pub(super) struct Classification {
pub(super) mcp_tool: Option<McpToolContext>,
pub(super) planned_action: String,
pub(super) sync_reviews: Vec<Arc<GuardianReviewEvidenceRecord>>,
pub(super) trusted_user_inputs: Vec<codex_guardian_context::TrustedUserAnswer>,
pub(super) authorization_version: GuardianAuthorizationVersion,
pub(super) history: Arc<dyn ConversationHistorySnapshot>,
pub(super) local_trusted_skill_paths: Vec<String>,
@@ -141,7 +141,6 @@ impl Classification {
mcp_tool,
planned_action,
sync_reviews,
trusted_user_inputs,
authorization_version,
history,
local_trusted_skill_paths,
@@ -251,6 +250,18 @@ impl Classification {
.collect::<Vec<_>>();
let transcript =
PreviousReviews::try_from_fragments(review_fragments).and_then(|reviews| {
let budgeting = if guardian_config.transcript.elastic.is_some() {
ContextBudgeting::Elastic
} else {
ContextBudgeting::Fixed
};
let retained = history
.retained_context()
.map(|context| budgeting.retained_assistant_view(context));
let answers = retained
.as_deref()
.map(codex_guardian_context::collect_verified_answer_candidates)
.unwrap_or_default();
let context_input = ContextInput {
target: ContextTarget::Async,
history: history.as_ref(),
@@ -261,7 +272,7 @@ impl Classification {
root_conversation_candidates: root_snapshot
.as_ref()
.and_then(|snapshot| snapshot.request_candidates.as_ref()),
trusted_user_answers: &trusted_user_inputs,
trusted_user_answers: &answers,
planned_action: Some(&action_section),
permissions: Some(&score_authorization.permissions),
previous_reviews: Some(&reviews),
@@ -269,7 +280,9 @@ impl Classification {
trusted_skill_paths: &trusted_skill_paths,
node_repl_images: Some(&node_repl_images),
};
guardian_config.transcript.collect_context(context_input)
guardian_config
.transcript
.collect_with_retained(context_input, retained.as_deref())
});
let evidence = match transcript {
Ok(transcript) => transcript,
@@ -446,11 +446,6 @@ impl GuardianV2Extension {
let sync_reviews = guardian_evidence.snapshot();
let authorization_version =
guardian_evidence.authorization_version(input.conversation_history.as_ref());
let trusted_user_inputs = input
.conversation_history
.retained_context()
.map(codex_guardian_context::collect_verified_answer_candidates)
.unwrap_or_default();
let history = Arc::clone(&input.conversation_history);
let local_trusted_skill_paths = guardian_evidence.trusted_skill_paths(input.turn_id);
let node_repl_images = if guardian_config.transcript.include_images {
@@ -514,7 +509,6 @@ impl GuardianV2Extension {
mcp_tool,
planned_action,
sync_reviews,
trusted_user_inputs,
authorization_version,
history,
local_trusted_skill_paths,
@@ -26,6 +26,7 @@ use codex_guardian_context::TranscriptSelection;
use codex_guardian_context::TrustedTool;
use codex_guardian_context::default_registry;
use codex_guardian_context::prepare_async_context;
use codex_history::RetainedContext;
const DEFAULT_PROFILE: &ContextProfile = &ContextProfile::asynchronous();
pub(crate) const MAX_MESSAGE_ENTRY_TOKENS: usize =
@@ -83,9 +84,10 @@ impl Default for TranscriptConfig {
}
impl TranscriptConfig {
pub(super) fn collect_context(
pub(super) fn collect_with_retained(
&self,
input: ContextInput<'_>,
retained_context: Option<&RetainedContext>,
) -> Result<CollectedTranscript, SectionError> {
let ContextInput {
target,
@@ -101,7 +103,7 @@ impl TranscriptConfig {
node_repl_images,
} = input;
let history_version = history.review_history_version();
let history = SnapshotHistory(history);
let history = SnapshotHistory(history, retained_context);
let profile = ContextProfile {
target: ContextTarget::Async,
transcript_format: self.format,
@@ -198,11 +200,14 @@ impl CollectedTranscript {
}
}
struct SnapshotHistory<'a>(&'a dyn ConversationHistorySnapshot);
struct SnapshotHistory<'a>(
&'a dyn ConversationHistorySnapshot,
Option<&'a RetainedContext>,
);
impl SectionHistory for SnapshotHistory<'_> {
fn retained_context(&self) -> Option<&codex_history::RetainedContext> {
self.0.retained_context()
self.1
}
fn items(&self) -> Box<dyn Iterator<Item = &ResponseItem> + Send + '_> {
@@ -29,6 +29,14 @@ use super::TranscriptSource;
// Existing rendering tests exercise the same fresh-conversation preparation and
// delivery filtering as the classifier, without retaining the snapshot formatter.
impl super::TranscriptConfig {
pub(in crate::async_scorer) fn collect_context(
&self,
input: ContextInput<'_>,
) -> Result<super::CollectedTranscript, codex_guardian_context::SectionError> {
let retained = input.history.retained_context();
self.collect_with_retained(input, retained)
}
fn build_context(
&self,
input: ContextInput<'_>,
+1 -1
View File
@@ -231,7 +231,7 @@ impl ElasticBudgetConfig {
/// complete selected duplicates. Only the headroom between each minimum and
/// maximum is scaled to the remaining budget.
/// Actual request admission still accounts for history, framing and exact actions.
/// These settings do not change storage bounds or activate elastic allocation.
/// The shared recorder also uses the retained-instructions entry limit.
#[derive(Clone, Copy, Debug, Eq, PartialEq, serde::Serialize)]
pub struct ElasticSectionLimits {
min_total_section_tokens: usize,
@@ -24,6 +24,7 @@ use crate::composition::FixedImageBudget;
use crate::retained_instructions::RetainedInstructionKind;
use crate::retained_instructions::USER_OMISSION;
use codex_history::ResponseItemEnvelope;
use codex_history::RetainedContext;
use codex_protocol::models::ImageDetail;
use codex_protocol::models::ImageReference;
use codex_protocol::models::ResponseItem;
@@ -46,6 +47,17 @@ pub enum ContextBudgeting {
Elastic,
}
impl ContextBudgeting {
/// Use the retained assistant window for this reviewer.
pub fn retained_assistant_view(self, context: &RetainedContext) -> Cow<'_, RetainedContext> {
if self == Self::Fixed {
context.fixed_assistant_view()
} else {
Cow::Borrowed(context)
}
}
}
#[cfg(test)]
#[path = "source_tests.rs"]
mod source_tests;
@@ -224,7 +224,12 @@ fn prepare_root_assistant_messages(
let text = retained
.and_then(|retained| root_retained_assistant(retained.message, budgeting))
.unwrap_or_else(|| match budgeting {
ContextBudgeting::Fixed => crate::truncate_text(&text, MAX_ROOT_MESSAGE_TOKENS),
ContextBudgeting::Fixed => {
let bounded = crate::truncate_text(&text, MAX_ROOT_MESSAGE_TOKENS);
missing_assistant_context |=
retained.is_some() && bounded.len() < text.len();
bounded
}
ContextBudgeting::Elastic => text,
});
let message = if order.is_some() {
@@ -346,7 +346,6 @@ impl ComposedContext {
let SectionDelivery::UserContent(items) = &mut section.delivery else {
continue;
};
// At most sixteen retained originals are checked; no history-sized index is needed.
items.retain(|item| {
let keep = item.source.as_ref().is_none_or(|source| {
!delivered_sources.clone().any(|delivered| {
+15
View File
@@ -158,6 +158,10 @@ pub struct CodexHarnessMetadata {
#[serde(default, skip_serializing_if = "Option::is_none")]
pub delivered_assistant_message: Option<String>,
/// A confirmed delivery whose full text was unavailable within the retention limit.
#[serde(default, skip_serializing_if = "std::ops::Not::not")]
pub delivered_assistant_message_incomplete: bool,
/// Whether a response configuration update was created by the Codex harness itself.
#[serde(default, skip_serializing_if = "std::ops::Not::not")]
pub harness_authored_configuration: bool,
@@ -225,6 +229,16 @@ impl std::fmt::Debug for ImageReplaySource {
}
impl CodexHarnessMetadata {
/// The captured delivery text and whether it is incomplete.
pub fn confirmed_delivery(&self) -> Option<(&str, bool)> {
if self.delivered_assistant_message_incomplete {
return Some(("", true));
}
self.delivered_assistant_message
.as_deref()
.map(|text| (text, text.is_empty()))
}
/// Shortened messages no longer prove complete instruction or review delivery.
pub fn mark_retained_sources_incomplete(&mut self) {
self.guardian_review_ids.clear();
@@ -526,6 +540,7 @@ pub use sender_user_messages::SenderUserMessages;
pub use reconciled_retained_context::ReconciledRetainedContext;
pub use retained_context::AcceptedFileAudio;
pub use retained_context::MessageRetention;
pub use retained_context::RetainedContext;
pub use retained_context::RetainedContextEntry;
pub use retained_context::RetainedContextEvent;
@@ -8,7 +8,6 @@ use super::RetainedInputSource;
use super::RetainedSource;
use super::RetainedSourceRole;
use super::RetainedUserMessage;
use super::bound_family;
use codex_thread_context_ids::ThreadContextIds;
impl RetainedContext {
@@ -37,10 +36,11 @@ impl RetainedContext {
/// Returns the captured source for the original envelope, independently of buffer eviction.
pub fn record_assistant_message(
&mut self,
message: RetainedUserMessage,
mut message: RetainedUserMessage,
source: RetainedInputSource,
context_ids: &ThreadContextIds,
) -> Option<RetainedSource> {
message.bound(self.assistant_record_bytes());
self.record_assistant_message_with_revision(
message,
source,
@@ -51,7 +51,7 @@ impl RetainedContext {
pub(super) fn record_assistant_message_with_revision(
&mut self,
mut message: RetainedUserMessage,
message: RetainedUserMessage,
source: RetainedInputSource,
context_ids: &ThreadContextIds,
revision: Option<&codex_protocol::ResponseItemId>,
@@ -61,7 +61,6 @@ impl RetainedContext {
// ordering alone does not establish an omission from reviewer context.
return None;
}
message.bound();
let empty = message.complete && message.text.is_empty();
let inherited = source == RetainedInputSource::Inherited;
// A confirmed delivery is a distinct rollout fact even when the bounded live
@@ -100,10 +99,7 @@ impl RetainedContext {
};
let source = entry.source(RetainedSourceRole::Assistant);
self.assistant_messages.push_back(entry);
bound_family(
&mut self.assistant_messages,
&mut self.assistant_messages_incomplete,
);
self.bound_assistant_family();
source
}
}
+152 -26
View File
@@ -11,7 +11,11 @@ pub use source::RetainedSource;
pub use source::RetainedSourceId;
pub use source::RetainedSourceRole;
use codex_protocol::ThreadId;
use codex_protocol::protocol::EventMsg;
use codex_protocol::protocol::GuardianRetentionPolicy;
use codex_thread_context_ids::ThreadContextIds;
use std::borrow::Cow;
use std::collections::BTreeSet;
use std::collections::VecDeque;
use std::sync::Arc;
@@ -22,6 +26,7 @@ use serde::Serialize;
use crate::CodexHarnessMetadata;
use crate::ResponseItemEnvelope;
use crate::RolloutItem;
use crate::SenderUserMessages;
const MAX_FAMILY_RECORDS: usize = 8;
@@ -185,6 +190,39 @@ pub enum RetainedContextEvent {
},
}
/// The recorder policy owned by the thread that produced this history.
#[derive(Clone, Copy, Debug, PartialEq, Serialize, Deserialize, JsonSchema)]
pub struct MessageRetention {
pub thread_id: ThreadId,
#[serde(flatten)]
pub policy: GuardianRetentionPolicy,
}
impl MessageRetention {
/// Recover the latest owned settings even when replay begins at a compaction.
pub fn saved(items: &[RolloutItem], thread_id: ThreadId) -> Option<Self> {
items.iter().rev().find_map(|item| match item {
RolloutItem::EventMsg(EventMsg::ThreadSettingsApplied(event))
if event.thread_id == Some(thread_id) =>
{
Some(
event
.thread_settings
.guardian_retention
.map(|policy| Self { thread_id, policy }),
)
}
RolloutItem::Compacted(item) => item
.retained_context
.as_ref()?
.message_retention
.filter(|retention| retention.thread_id == thread_id)
.map(Some),
_ => None,
})?
}
}
/// Bounded snapshot of retained families, persisted with the parent compaction checkpoint.
/// Facts live until their instruction boundary is rolled back; compaction does not expire them.
#[derive(Debug, Clone, Default, PartialEq, Serialize, Deserialize, JsonSchema)]
@@ -203,6 +241,8 @@ pub struct RetainedContext {
/// Assistant context has a separate budget so it cannot evict user restrictions.
#[serde(default)]
assistant_messages: VecDeque<Ordered<RetainedUserMessage>>,
#[serde(default, skip_serializing_if = "Option::is_none")]
message_retention: Option<MessageRetention>,
/// Observed assistant storage omissions; absent legacy metadata is not evidence of loss.
#[serde(default)]
assistant_messages_incomplete: bool,
@@ -218,21 +258,48 @@ fn missing_message_history() -> bool {
}
fn bound_family<T: Serialize>(items: &mut VecDeque<Ordered<T>>, incomplete: &mut bool) {
bound_family_with_limit(items, incomplete, MAX_FAMILY_RECORDS);
}
fn bound_family_with_limit<T: Serialize>(
items: &mut VecDeque<Ordered<T>>,
incomplete: &mut bool,
max_records: usize,
) {
// Queued instructions can be recorded after later-accepted answers. Evict by
// acceptance order, not by the order in which persistence happened to finish.
items.make_contiguous().sort_by_key(Ordered::key);
while items.len() > MAX_FAMILY_RECORDS
|| serde_json::to_vec(items).map_or(true, |bytes| bytes.len() > MAX_FAMILY_BYTES)
{
items.pop_front();
*incomplete = true;
let count_evicted = items.len().saturating_sub(max_records);
*incomplete |= count_evicted != 0;
items.drain(..count_evicted);
if serde_json::to_vec(items).is_ok_and(|bytes| bytes.len() <= MAX_FAMILY_BYTES) {
return;
}
// Count the newest fitting suffix once. Each element uses a separator or
// closing bracket in addition to its JSON bytes, after the opening bracket.
let mut bytes = 1;
let mut keep = 0;
for item in items.iter().rev() {
let Ok(encoded) = serde_json::to_vec(item) else {
break;
};
bytes += encoded.len() + 1;
if bytes > MAX_FAMILY_BYTES {
break;
}
keep += 1;
}
let evicted = items.len() - keep;
*incomplete |= evicted != 0;
items.drain(..evicted);
}
impl RetainedUserMessage {
fn bound(&mut self) {
if serde_json::to_vec(self).map_or(true, |bytes| bytes.len() > MAX_RECORD_BYTES) {
self.text.clear();
fn bound(&mut self, max_record_bytes: usize) {
if self.text.len() > max_record_bytes
|| serde_json::to_vec(self).map_or(true, |bytes| bytes.len() > max_record_bytes)
{
self.text = String::new();
self.complete = false;
self.turn_id
.truncate(self.turn_id.floor_char_boundary(1_024));
@@ -245,7 +312,7 @@ impl RetainedUserMessage {
impl RetainedContextEvent {
/// Bounds a persisted event before it enters the rollout or the live snapshot.
pub fn bound(&mut self) {
fn bound(&mut self, assistant_record_bytes: usize) {
match self {
Self::VerifiedAnswer { answer, .. } => {
if serde_json::to_vec(answer).map_or(true, |bytes| bytes.len() > MAX_RECORD_BYTES) {
@@ -259,7 +326,9 @@ impl RetainedContextEvent {
.truncate(answer.call_id.floor_char_boundary(1_024));
}
}
Self::DeliveredAssistantMessage { message, .. } => message.bound(),
Self::DeliveredAssistantMessage { message, .. } => {
message.bound(assistant_record_bytes)
}
// An oversized audio record is omitted in record(); IDs must never be truncated.
Self::AcceptedFileAudio { .. } => {}
}
@@ -267,6 +336,62 @@ impl RetainedContextEvent {
}
impl RetainedContext {
/// Initializes a recorder for the policy owning this history.
pub fn new(message_retention: Option<MessageRetention>) -> Self {
Self {
message_retention,
..Self::default()
}
}
/// Whether the recorder admits whole assistant messages using an Elastic entry limit.
pub fn retains_original_assistant_messages(&self) -> bool {
self.message_retention.is_some()
}
/// The configured Elastic per-record limit, if supplied by the session.
pub fn elastic_assistant_record_bytes(&self) -> Option<usize> {
self.message_retention
.and_then(|retention| retention.policy.assistant_record_bytes)
}
fn assistant_record_bytes(&self) -> usize {
self.elastic_assistant_record_bytes()
.unwrap_or(MAX_RECORD_BYTES)
}
/// Clears retained evidence while keeping the recording policy.
pub fn empty_with_same_limits(&self) -> Self {
Self::new(self.message_retention)
}
fn bound_assistant_family(&mut self) {
let max_records = self
.message_retention
.map_or(MAX_FAMILY_RECORDS, |_| usize::MAX);
bound_family_with_limit(
&mut self.assistant_messages,
&mut self.assistant_messages_incomplete,
max_records,
);
}
/// Projects the assistant family for consumers using the Fixed retention window.
pub fn fixed_assistant_view(&self) -> Cow<'_, Self> {
if self.assistant_messages.len() <= MAX_FAMILY_RECORDS
&& self.assistant_record_bytes() <= MAX_RECORD_BYTES
{
return Cow::Borrowed(self);
}
let mut view = self.clone();
view.message_retention = None;
for entry in &mut view.assistant_messages {
entry.value.bound(MAX_RECORD_BYTES);
}
view.bound_assistant_family();
Cow::Owned(view)
}
pub fn accepted_file_audio(&self, model: &str, provider: &str) -> Option<&AcceptedFileAudio> {
self.accepted_file_audio
.as_deref()
@@ -392,7 +517,7 @@ impl RetainedContext {
source: RetainedInputSource,
context_ids: &ThreadContextIds,
) -> Option<RetainedSource> {
message.bound();
message.bound(MAX_RECORD_BYTES);
let inherited = source == RetainedInputSource::Inherited;
// An unchanged invocation is not a new instruction. Keep the original
// acceptance order; compare only the latest version of this automation,
@@ -468,7 +593,7 @@ impl RetainedContext {
&& let Some(text) = message.message_id.as_deref().and_then(&mut excerpt_for_id)
{
message.text = text;
message.bound();
message.bound(MAX_RECORD_BYTES);
}
}
bound_family(&mut self.user_messages, &mut self.user_messages_incomplete);
@@ -485,7 +610,7 @@ impl RetainedContext {
event: &mut RetainedContextEvent,
context_ids: &ThreadContextIds,
) -> bool {
event.bound();
event.bound(self.assistant_record_bytes());
match event {
RetainedContextEvent::AcceptedFileAudio { evidence } => {
if !evidence.fits_record_budget()
@@ -528,10 +653,7 @@ impl RetainedContext {
retained_source,
} => {
if let Some(existing) = self.assistant_messages.iter().find(|entry| {
message.message_id.is_some()
&& entry.value.message_id == message.message_id
&& entry.value == *message
&& !entry.inherited
message.message_id.is_some() && entry.value == *message && !entry.inherited
}) {
*retained_source = existing.source(RetainedSourceRole::Assistant);
return false;
@@ -564,10 +686,13 @@ impl RetainedContext {
/// A missing checkpoint cannot establish complete historical user instructions.
/// Surviving local input metadata also advances the counter when its retained record is missing.
pub fn restore(&mut self, checkpoint: Option<&Self>, surviving_items: &[ResponseItemEnvelope]) {
let message_retention = self.message_retention;
*self = checkpoint.cloned().unwrap_or_else(|| Self {
user_messages_incomplete: true,
..Self::default()
});
self.message_retention = message_retention;
let assistant_record_bytes = self.assistant_record_bytes();
self.accepted_file_audio
.take_if(|evidence| !evidence.fits_record_budget());
for entry in &mut self.verified_answers {
@@ -575,19 +700,23 @@ impl RetainedContext {
answer: entry.value.clone(),
acceptance_order: Some(entry.order),
};
event.bound();
event.bound(assistant_record_bytes);
let RetainedContextEvent::VerifiedAnswer { answer, .. } = event else {
unreachable!("verified answer constructed above");
};
entry.value = answer;
self.next_order = self.next_order.max(entry.order.saturating_add(1));
}
for entry in self
let user = self
.user_messages
.iter_mut()
.chain(&mut self.assistant_messages)
{
entry.value.bound();
.map(|entry| (entry, MAX_RECORD_BYTES));
let assistant = self
.assistant_messages
.iter_mut()
.map(|entry| (entry, assistant_record_bytes));
for (entry, max_record_bytes) in user.chain(assistant) {
entry.value.bound(max_record_bytes);
// Also repair checkpoints written before adoption recorded the answer gap.
self.verified_answers_incomplete |= entry.inherited;
if !entry.inherited {
@@ -618,10 +747,7 @@ impl RetainedContext {
&mut self.verified_answers_incomplete,
);
bound_family(&mut self.user_messages, &mut self.user_messages_incomplete);
bound_family(
&mut self.assistant_messages,
&mut self.assistant_messages_incomplete,
);
self.bound_assistant_family();
}
/// Keeps legacy answers whose source calls survive when no retained instruction boundary exists.
@@ -123,6 +123,68 @@ fn retained_evidence_preserves_order_through_recovery_checkpoint_and_rollback()
);
}
#[test]
fn assistant_record_limit_applies_on_capture_and_restore() {
const LARGE_RECORD_BYTES: usize = 24_000;
let context_ids = thread_context_ids();
let thread_id = ThreadId::new();
let recorder = |assistant_record_bytes| {
RetainedContext::new(Some(MessageRetention {
thread_id,
policy: GuardianRetentionPolicy {
assistant_record_bytes,
},
}))
};
let mut elastic = recorder(Some(LARGE_RECORD_BYTES));
let text = "x".repeat(MAX_RECORD_BYTES);
for index in 0..=MAX_FAMILY_BYTES / text.len() {
let order = elastic.reserve_order();
elastic.record_assistant_message(
RetainedUserMessage {
turn_id: "turn-1".to_owned(),
message_id: Some(index.to_string()),
text: text.clone(),
complete: true,
origin: crate::UserInputOrigin::User,
phase: None,
},
RetainedInputSource::Local(Some(order)),
&context_ids,
);
}
assert_eq!(elastic.assistant_messages[0].value.text, text);
assert!(elastic.has_omitted_assistant_messages());
let order = elastic.reserve_order();
let mut delivery = delivered_message("long-send", &text, order);
assert!(elastic.record_and_capture(&mut delivery, &context_ids));
let RetainedContextEvent::DeliveredAssistantMessage { message, .. } = delivery else {
unreachable!("recording preserves the event variant");
};
assert_eq!((message.text, message.complete), (text, true));
let checkpoint = serde_json::from_value(serde_json::json!({
"type": "compacted", "payload": {"message": "", "retained_context": &elastic}
}))
.unwrap();
let saved = MessageRetention::saved(&[checkpoint], thread_id);
assert_eq!(saved, elastic.message_retention);
let mut restored = recorder(/*assistant_record_bytes*/ None);
restored.restore(Some(&elastic), &[]);
assert_eq!(
restored.assistant_messages,
elastic.fixed_assistant_view().assistant_messages
);
let omitted = &restored.assistant_messages[0].value;
assert_eq!((omitted.complete, omitted.text.capacity()), (false, 0));
let mut restored = recorder(Some(1));
let order = restored.reserve_order();
let delivery = delivered_message("confirmed", "Deploy?", order);
assert!(restored.record(&delivery, &context_ids));
assert!(!restored.record(&delivery, &context_ids));
}
#[test]
fn retained_families_enforce_storage_limits_without_changing_snapshots() {
let mut context = RetainedContext::default();
+12 -2
View File
@@ -129,10 +129,15 @@ fn thread_settings_snapshot(disabled_plugin_ids: Vec<String>) -> Result<ThreadSe
}
#[test]
fn latest_disabled_plugin_ids_preserves_owned_updates_and_clears() -> Result<()> {
fn latest_thread_settings_preserve_owned_updates_and_clears() -> Result<()> {
let thread_id = ThreadId::new();
let ancestor_id = ThreadId::new();
let selected = thread_settings_snapshot(vec!["example@marketplace".to_string()])?;
let mut selected = thread_settings_snapshot(vec!["example@marketplace".to_string()])?;
const RECORD_BYTES: usize = 4_096;
let policy = codex_protocol::protocol::GuardianRetentionPolicy {
assistant_record_bytes: Some(RECORD_BYTES),
};
selected.guardian_retention = Some(policy);
let cleared = thread_settings_snapshot(Vec::new())?;
let item = |thread_id, thread_settings| {
RolloutItem::EventMsg(EventMsg::ThreadSettingsApplied(
@@ -154,6 +159,10 @@ fn latest_disabled_plugin_ids_preserves_owned_updates_and_clears() -> Result<()>
latest_disabled_plugin_ids(&history, thread_id),
Some(selected.disabled_plugin_ids.as_slice())
);
assert_eq!(
MessageRetention::saved(&history, thread_id),
Some(MessageRetention { thread_id, policy })
);
history.push(item(Some(thread_id), cleared.clone()));
history.extend(foreign_items);
@@ -164,6 +173,7 @@ fn latest_disabled_plugin_ids_preserves_owned_updates_and_clears() -> Result<()>
latest_disabled_plugin_ids(&history, thread_id),
Some(cleared.disabled_plugin_ids.as_slice())
);
assert_eq!(MessageRetention::saved(&history, thread_id), None);
Ok(())
}
@@ -164,6 +164,11 @@ impl RollbackPlanner {
RolloutItem::DynamicToolsUpdated { .. } | RolloutItem::SessionMeta(_) => {
self.record_boundaries[index] = None
}
RolloutItem::EventMsg(EventMsg::ThreadSettingsApplied(event))
if event.thread_id.is_some() =>
{
self.record_boundaries[index] = None;
}
RolloutItem::ResponseItem(response) => {
if matches!(&response.item, ResponseItem::Message { role, .. } if role == "assistant")
|| matches!(&response.item, ResponseItem::FunctionCall { .. })