mirror of
https://github.com/openai/codex.git
synced 2026-11-21 22:50:37 +00:00
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:
@@ -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(
|
||||
|
||||
@@ -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),
|
||||
|
||||
@@ -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;
|
||||
|
||||
@@ -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();
|
||||
|
||||
@@ -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(_)) {
|
||||
|
||||
@@ -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)
|
||||
|
||||
@@ -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
|
||||
{
|
||||
|
||||
@@ -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 {
|
||||
|
||||
Reference in New Issue
Block a user