mirror of
https://github.com/openai/codex.git
synced 2026-11-18 22:20:48 +00:00
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:
@@ -7,6 +7,8 @@ use codex_protocol::error::CodexErr;
|
||||
use codex_protocol::error::Result as CodexResult;
|
||||
use codex_protocol::models::ResponseItem;
|
||||
use codex_protocol::openai_models::ModelInfo;
|
||||
use std::sync::Arc;
|
||||
use tokio_util::sync::CancellationToken;
|
||||
|
||||
/// Uploaded files need the admission checks on typed turn input before entering history.
|
||||
pub(crate) fn validate_injected_response_item(item: &ResponseItem) -> CodexResult<()> {
|
||||
@@ -19,6 +21,45 @@ pub(crate) fn validate_injected_response_item(item: &ResponseItem) -> CodexResul
|
||||
}
|
||||
|
||||
impl Session {
|
||||
/// History injection can precede the first turn. Capture full initial context
|
||||
/// only when the thread has no reference context, including after compaction.
|
||||
pub(crate) async fn history_injection_context(
|
||||
self: &Arc<Self>,
|
||||
) -> CodexResult<Arc<TurnContext>> {
|
||||
if self.reference_context_item().await.is_some() {
|
||||
let turn = self.new_inject_items_context().await;
|
||||
// Compaction can clear the baseline while we build this context.
|
||||
// Rebuilding initial context requires a full skills snapshot.
|
||||
if self.reference_context_item().await.is_some() {
|
||||
return Ok(turn);
|
||||
}
|
||||
}
|
||||
let turn = self.new_default_turn().await;
|
||||
if self.reference_context_item().await.is_none() {
|
||||
let step = self
|
||||
.capture_step_context(Arc::clone(&turn), &CancellationToken::new())
|
||||
.await?;
|
||||
self.record_context_updates_and_set_reference_context_item(step.as_ref())
|
||||
.await?;
|
||||
}
|
||||
Ok(turn)
|
||||
}
|
||||
|
||||
pub(crate) async fn record_inherited_items(
|
||||
self: &Arc<Self>,
|
||||
items: Vec<ResponseItemEnvelope>,
|
||||
) -> CodexResult<()> {
|
||||
if items.is_empty() {
|
||||
return Ok(());
|
||||
}
|
||||
let turn = self.history_injection_context().await?;
|
||||
// The parent already prepared the images and their dimension notices.
|
||||
self.record_prepared_conversation_items(&turn, turn.model_info(), items, Vec::new())
|
||||
.await;
|
||||
self.flush_rollout().await?;
|
||||
Ok(())
|
||||
}
|
||||
|
||||
/// Returns the input if there is no active turn to inject into.
|
||||
#[expect(
|
||||
clippy::await_holding_invalid_type,
|
||||
@@ -76,19 +117,19 @@ impl Session {
|
||||
Ok(())
|
||||
}
|
||||
|
||||
/// Preserves trusted client provenance while items wait for an active turn.
|
||||
/// Applies client developer-message retention before queueing or recording items.
|
||||
#[expect(
|
||||
clippy::await_holding_invalid_type,
|
||||
reason = "active turn checks and turn state updates must remain atomic"
|
||||
)]
|
||||
pub(crate) async fn inject_client_response_items(
|
||||
&self,
|
||||
items: Vec<ResponseItem>,
|
||||
items: Vec<impl Into<ResponseItemEnvelope>>,
|
||||
turn_context: &TurnContext,
|
||||
) {
|
||||
let items = items
|
||||
.into_iter()
|
||||
.map(ResponseItemEnvelope::new)
|
||||
.map(Into::into)
|
||||
.map(|item| self.annotate_client_response_item(item))
|
||||
.collect::<Vec<_>>();
|
||||
let mut active = self.active_turn.lock().await;
|
||||
@@ -120,7 +161,11 @@ impl Session {
|
||||
if self.enabled(Feature::RetainClientDeveloperMessages)
|
||||
&& matches!(&envelope.item, ResponseItem::Message { role, .. } if role == "developer")
|
||||
{
|
||||
envelope.metadata.get_or_insert_default().client_authored = true;
|
||||
envelope
|
||||
.metadata
|
||||
.get_or_insert_default()
|
||||
.retain_across_windows
|
||||
.get_or_insert(true);
|
||||
}
|
||||
envelope
|
||||
}
|
||||
|
||||
@@ -225,8 +225,6 @@ use uuid::Uuid;
|
||||
use crate::client::ModelClient;
|
||||
use crate::codex_thread::CodexThreadSettingsOverrides;
|
||||
use crate::codex_thread::ThreadConfigSnapshot;
|
||||
#[cfg(test)]
|
||||
use crate::compact::collect_user_messages;
|
||||
use crate::config::Config;
|
||||
use crate::config::ConstraintResult;
|
||||
use crate::config::PermissionProfileSnapshot;
|
||||
@@ -689,7 +687,15 @@ impl Session {
|
||||
})
|
||||
}
|
||||
|
||||
async fn spawn_internal(args: SessionSpawnArgs) -> CodexResult<(Arc<Self>, SessionIo)> {
|
||||
// Construct the startup future outside the caller's poll frame.
|
||||
#[inline(never)]
|
||||
fn spawn_internal(
|
||||
args: SessionSpawnArgs,
|
||||
) -> BoxFuture<'static, CodexResult<(Arc<Self>, SessionIo)>> {
|
||||
Box::pin(Self::initialize(args))
|
||||
}
|
||||
|
||||
async fn initialize(args: SessionSpawnArgs) -> CodexResult<(Arc<Self>, SessionIo)> {
|
||||
args.config
|
||||
.config_layer_stack
|
||||
.ensure_config_server_applied()?;
|
||||
@@ -5472,24 +5478,16 @@ impl Session {
|
||||
let turn_context = step_context.turn.as_ref();
|
||||
let history = self.clone_history().await;
|
||||
let input_goal_ids = crate::context::UserGoalUpdate::message_ids(history.raw_items());
|
||||
let retained_client_items =
|
||||
crate::compact_remote_v2::truncate_retained_messages_for_remote_compaction(
|
||||
history
|
||||
.annotated_items()
|
||||
.iter()
|
||||
.filter(|item| {
|
||||
(self.enabled(Feature::RetainClientToolOutputs)
|
||||
&& crate::compact_retained_tool_outputs::is_retained_tool_output(item))
|
||||
|| (self.enabled(Feature::RetainClientDeveloperMessages)
|
||||
&& crate::compact_remote_v2::is_client_authored_developer_message(
|
||||
item,
|
||||
))
|
||||
})
|
||||
.cloned()
|
||||
.collect(),
|
||||
crate::compact_remote_v2::RETAINED_MESSAGE_TOKEN_BUDGET,
|
||||
&history.file_audio_durations,
|
||||
);
|
||||
let retained_client_items = crate::compact_remote_v2::retained_client_items(
|
||||
history.annotated_items(),
|
||||
&history.file_audio_durations,
|
||||
|item| {
|
||||
crate::compact_remote_v2::should_retain_client_item(
|
||||
item,
|
||||
&turn_context.config.features,
|
||||
)
|
||||
},
|
||||
);
|
||||
// Resolve the roster before allocating a new context window.
|
||||
let rendered = world_state.render_full_with_host_roster().await;
|
||||
let (window_number, window_ids) = self.state.lock().await.start_new_context_window();
|
||||
@@ -5499,7 +5497,7 @@ impl Session {
|
||||
crate::context_manager::updates::merge_world_state_updates(context_updates)
|
||||
.into_iter()
|
||||
.map(ResponseItemEnvelope::new)
|
||||
.chain(retained_client_items)
|
||||
.chain(retained_client_items.into_values())
|
||||
.collect();
|
||||
let turn_context_item = step_context.to_turn_context_item();
|
||||
self.replace_compacted_history(
|
||||
|
||||
@@ -561,13 +561,8 @@ impl ContextManager {
|
||||
// prompt shape.
|
||||
// TODO(ccunningham): if we drop support for None replacement_history compaction items,
|
||||
// we can get rid of this second loop entirely and just build `history` directly in the first loop.
|
||||
let user_messages =
|
||||
compact::collect_annotated_user_messages(history.annotated_items());
|
||||
let rebuilt = compact::build_compacted_history(
|
||||
Vec::new(),
|
||||
&user_messages,
|
||||
&compacted.message,
|
||||
);
|
||||
let rebuilt =
|
||||
compact::build_compacted_history(&history, &compacted.message);
|
||||
let retained_context = history.retained_context().clone();
|
||||
history.replace_annotated(rebuilt);
|
||||
history.restore_retained_context(Some(&retained_context));
|
||||
|
||||
@@ -1017,6 +1017,10 @@ impl Session {
|
||||
SessionId::from(thread_id)
|
||||
}
|
||||
});
|
||||
let inherited_items = agent_control
|
||||
.runtime()
|
||||
.retained_items_for_fresh_child(&initial_history, &session_source)
|
||||
.await;
|
||||
let has_provided_agent_control =
|
||||
matches!(&agent_control, AgentControlInit::Provided { .. });
|
||||
let (agent_control, local_agent_runtime): (Arc<dyn AgentControl>, _) = match agent_control {
|
||||
@@ -2129,6 +2133,7 @@ impl Session {
|
||||
|
||||
// record_initial_history can emit events. We record only after the SessionConfiguredEvent is emitted.
|
||||
Box::pin(sess.record_initial_history(initial_history)).await;
|
||||
Box::pin(sess.record_inherited_items(inherited_items)).await?;
|
||||
if restore_child_window {
|
||||
sess.state.lock().await.restore_auto_compact_window(
|
||||
/*window_number*/ 0,
|
||||
|
||||
@@ -13433,9 +13433,7 @@ async fn sample_rollout(
|
||||
rollout_items.push(RolloutItem::ResponseItem(assistant1.clone().into()));
|
||||
|
||||
let summary1 = "summary one";
|
||||
let snapshot1 = raw_history_items(&live_history);
|
||||
let user_messages1 = collect_user_messages(&snapshot1);
|
||||
let rebuilt1 = compact::build_compacted_history(Vec::new(), &user_messages1, summary1);
|
||||
let rebuilt1 = compact::build_compacted_history(&live_history, summary1);
|
||||
live_history.replace_annotated(rebuilt1);
|
||||
let (window_number, window_ids) = session.advance_auto_compact_window().await;
|
||||
rollout_items.push(RolloutItem::Compacted(Box::new(CompactedItem {
|
||||
@@ -13468,9 +13466,7 @@ async fn sample_rollout(
|
||||
rollout_items.push(RolloutItem::ResponseItem(assistant2.clone().into()));
|
||||
|
||||
let summary2 = "summary two";
|
||||
let snapshot2 = raw_history_items(&live_history);
|
||||
let user_messages2 = collect_user_messages(&snapshot2);
|
||||
let rebuilt2 = compact::build_compacted_history(Vec::new(), &user_messages2, summary2);
|
||||
let rebuilt2 = compact::build_compacted_history(&live_history, summary2);
|
||||
live_history.replace_annotated(rebuilt2);
|
||||
let (window_number, window_ids) = session.advance_auto_compact_window().await;
|
||||
rollout_items.push(RolloutItem::Compacted(Box::new(CompactedItem {
|
||||
|
||||
@@ -1127,7 +1127,7 @@ async fn pending_turn_input(
|
||||
SubmittedTurnInput::AnnotatedResponseItem(input) => input.annotations.retain,
|
||||
_ => false,
|
||||
};
|
||||
let mut input = match input {
|
||||
match input {
|
||||
SubmittedTurnInput::UserInput { content, client_id } => TurnInput::UserInput {
|
||||
content,
|
||||
client_id,
|
||||
@@ -1143,7 +1143,7 @@ async fn pending_turn_input(
|
||||
ResponseItem::FunctionCallOutput { call_id: None, .. }
|
||||
) =>
|
||||
{
|
||||
let metadata = if let Some(fragment) = sender_messages {
|
||||
let mut metadata = if let Some(fragment) = sender_messages {
|
||||
let mut messages = SenderUserMessages {
|
||||
receiver_turn_id: turn_id.to_owned(),
|
||||
receiver_message_id: fragment.delivery.clone(),
|
||||
@@ -1158,6 +1158,12 @@ async fn pending_turn_input(
|
||||
} else {
|
||||
None
|
||||
};
|
||||
if retain
|
||||
&& session.enabled(Feature::RetainClientToolOutputs)
|
||||
&& matches!(item, ResponseItem::FunctionCallOutput { name: Some(_), .. })
|
||||
{
|
||||
metadata.get_or_insert_default().retain_across_windows = Some(true);
|
||||
}
|
||||
TurnInput::FunctionCallOutput(ResponseItemEnvelope { item, metadata })
|
||||
}
|
||||
SubmittedTurnInput::ResponseItem(item)
|
||||
@@ -1167,13 +1173,5 @@ async fn pending_turn_input(
|
||||
SubmittedTurnInput::InterAgentCommunication(communication) => {
|
||||
TurnInput::InterAgentCommunication(communication.into())
|
||||
}
|
||||
};
|
||||
if retain
|
||||
&& let TurnInput::ResponseItem(envelope) | TurnInput::FunctionCallOutput(envelope) =
|
||||
&mut input
|
||||
&& matches!(envelope.item, ResponseItem::FunctionCallOutput { .. })
|
||||
{
|
||||
envelope.metadata.get_or_insert_default().client_authored = true;
|
||||
}
|
||||
input
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user