mirror of
https://github.com/openai/codex.git
synced 2026-11-20 22:40:37 +00:00
## 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
874 lines
32 KiB
Rust
874 lines
32 KiB
Rust
//! Model-history and persisted-rollout domain types.
|
|
|
|
mod heartbeat;
|
|
pub use heartbeat::HEARTBEAT_CONTENT_KIND;
|
|
pub use heartbeat::Heartbeat;
|
|
pub use heartbeat::UserInputOrigin;
|
|
|
|
mod compaction_resume_metadata;
|
|
pub use codex_protocol::turn_input::TurnAttribution;
|
|
pub use compaction_resume_metadata::CompactionResumeMetadata;
|
|
pub use compaction_resume_metadata::DynamicToolsSnapshot;
|
|
pub use compaction_resume_metadata::PreviousTurnSettings;
|
|
pub use compaction_resume_metadata::resume_multi_agent_version;
|
|
|
|
mod compaction_checkpoint;
|
|
pub use compaction_checkpoint::CompactionCheckpoint;
|
|
|
|
mod initialization;
|
|
pub use initialization::HistoryInitialization;
|
|
|
|
use std::borrow::Borrow;
|
|
use std::borrow::Cow;
|
|
use std::ops::Deref;
|
|
use std::ops::DerefMut;
|
|
use std::path::PathBuf;
|
|
use std::sync::Arc;
|
|
|
|
use codex_protocol::ThreadId;
|
|
use codex_protocol::capabilities::SelectedCapabilityRoot;
|
|
use codex_protocol::dynamic_tools::DynamicToolSpec;
|
|
use codex_protocol::mcp::McpResourceOriginCheckpoint;
|
|
use codex_protocol::models::BaseInstructions;
|
|
use codex_protocol::models::ContentItem;
|
|
use codex_protocol::models::ResponseItem;
|
|
use codex_protocol::protocol::EventMsg;
|
|
use codex_protocol::protocol::InterAgentCommunication;
|
|
use codex_protocol::protocol::MultiAgentVersion;
|
|
use codex_protocol::protocol::SessionMeta;
|
|
use codex_protocol::protocol::SessionMetaLine;
|
|
use codex_protocol::protocol::SessionSource;
|
|
use codex_protocol::protocol::ThreadHistoryMode;
|
|
use codex_protocol::protocol::ThreadSource;
|
|
use codex_protocol::protocol::TokenUsageRecord;
|
|
use codex_protocol::protocol::TurnContextItem;
|
|
use codex_protocol::protocol::WorldStateItem;
|
|
use codex_protocol::realtime::RealtimeHandoffMode;
|
|
use codex_protocol::realtime::RealtimeItem;
|
|
use codex_protocol::realtime::RealtimeItemContent;
|
|
use codex_protocol::realtime::SessionInputReason;
|
|
use codex_protocol::security_risk::SecurityRiskScore;
|
|
use codex_protocol::turn_input::InjectedResponseItem;
|
|
use codex_protocol::turn_input::ItemRetention;
|
|
use schemars::JsonSchema;
|
|
use serde::Deserialize;
|
|
use serde::Deserializer;
|
|
use serde::Serialize;
|
|
use serde::Serializer;
|
|
use serde::de::Error as _;
|
|
|
|
/// A model-history item with room for history-only metadata.
|
|
///
|
|
/// Persistence keeps the response item intact and stores its metadata separately.
|
|
#[derive(Debug, Clone, PartialEq)]
|
|
pub struct ResponseItemEnvelope {
|
|
pub item: ResponseItem,
|
|
pub metadata: Option<CodexHarnessMetadata>,
|
|
}
|
|
|
|
/// Metadata owned by the Codex harness and persisted with a response item.
|
|
///
|
|
#[derive(Serialize, Deserialize, Debug, Clone, Default, PartialEq, Eq, JsonSchema)]
|
|
pub struct CodexHarnessMetadata {
|
|
/// Realtime provenance for an accepted ordinary turn input. Never model-visible.
|
|
#[serde(default, skip_serializing_if = "Option::is_none")]
|
|
pub realtime_handoff: Option<Box<RealtimeHandoffMetadata>>,
|
|
|
|
/// Provenance of a transcript chunk saved in ordinary model history.
|
|
#[serde(default, skip_serializing_if = "Option::is_none")]
|
|
pub realtime_session_input: Option<Box<RealtimeSessionInputMetadata>>,
|
|
/// End-of-call transcript context, recorded without creating a user turn.
|
|
#[serde(default, skip_serializing_if = "std::ops::Not::not")]
|
|
pub realtime_session_end_input: bool,
|
|
|
|
/// Thread-local append sequence. Compaction keeps the identities of retained inputs.
|
|
#[serde(default, skip_serializing_if = "Option::is_none")]
|
|
pub history_sequence: Option<u64>,
|
|
|
|
/// Highest input sequence included in a regular request that recorded model output.
|
|
/// Compaction carries this checkpoint without advancing it. Never model-visible.
|
|
#[serde(default, skip_serializing_if = "Option::is_none")]
|
|
pub sampled_through_sequence: Option<u64>,
|
|
|
|
/// Original inline images to sanitize again on resume or fork.
|
|
#[serde(default, skip_serializing_if = "Vec::is_empty")]
|
|
pub image_replay_sources: Vec<ImageReplaySource>,
|
|
|
|
/// Complete retained records actually delivered by this Guardian message. Host-only.
|
|
#[serde(default, skip_serializing_if = "Vec::is_empty")]
|
|
pub guardian_sources: Vec<RetainedSource>,
|
|
|
|
/// Completed sync reviews delivered by this complete Guardian message. Host-only.
|
|
#[serde(default, skip_serializing_if = "Vec::is_empty")]
|
|
pub guardian_review_ids: Vec<codex_protocol::ResponseItemId>,
|
|
|
|
/// This complete message delivered the meaning of retained source-order labels.
|
|
#[serde(default, skip_serializing_if = "std::ops::Not::not")]
|
|
pub guardian_source_order_guidance: bool,
|
|
|
|
/// Section-scoped omission delivery proof; None means unknown, not complete.
|
|
#[serde(default, skip_serializing_if = "Option::is_none")]
|
|
pub guardian_retained_omissions: Option<GuardianRetainedOmissions>,
|
|
|
|
/// Original retained evidence represented by this exact history item.
|
|
#[serde(default, skip_serializing_if = "Option::is_none")]
|
|
pub retained_source: Option<RetainedSource>,
|
|
|
|
/// The harness rendered client-supplied untrusted additional context as a standalone tool output.
|
|
#[serde(default, skip_serializing_if = "std::ops::Not::not")]
|
|
pub additional_context_tool_output: bool,
|
|
|
|
/// Whether a developer message was supplied by a client or a client requested tool-output retention.
|
|
///
|
|
/// Use this field only to interpret older rollouts.
|
|
/// Store new retention decisions in `retain_across_windows`.
|
|
#[serde(default)]
|
|
pub client_authored: bool,
|
|
|
|
/// Per-item override of the thread's context-window retention policy.
|
|
/// Never sent to the model.
|
|
#[serde(default, skip_serializing_if = "Option::is_none")]
|
|
pub retain_across_windows: Option<bool>,
|
|
|
|
/// Override inheritance by spawned agents, including full-history forks.
|
|
/// Older rollouts pin items in both scopes when `retain_across_windows` is true.
|
|
#[serde(default, skip_serializing_if = "Option::is_none")]
|
|
pub retain_in_children: Option<bool>,
|
|
|
|
/// The originating history budget, including any tool-specific allowance.
|
|
/// Measured in tokens and reused when replaying persisted history.
|
|
#[serde(
|
|
default,
|
|
rename = "fallback_token_limit_override",
|
|
skip_serializing_if = "Option::is_none"
|
|
)]
|
|
pub history_truncation_token_limit: Option<usize>,
|
|
|
|
/// Fresh tool output has already passed a producer limit. Used only to budget
|
|
/// storage-error placeholders before the item is recorded. Never persisted.
|
|
#[serde(skip)]
|
|
pub producer_bounded_tool_audio: bool,
|
|
|
|
/// Core applied the history limit while tool audio still had its inline cost.
|
|
#[serde(default, skip_serializing_if = "std::ops::Not::not")]
|
|
pub tool_audio_budget_applied: bool,
|
|
|
|
/// Bounded assistant text confirmed by a successful messaging tool result.
|
|
/// Captured after input hooks; untrusted context, never user authorization.
|
|
#[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,
|
|
|
|
/// The exact input to this opaque checkpoint contained no audio or file-backed images.
|
|
#[serde(default, skip_serializing_if = "std::ops::Not::not")]
|
|
pub compaction_without_file_error_sources: bool,
|
|
|
|
/// Producer compatibility for an opaque compaction item, never the currently selected model.
|
|
#[serde(default, skip_serializing_if = "Option::is_none")]
|
|
pub compaction_model_hash: Option<String>,
|
|
|
|
/// User acceptance or assistant delivery order, independent of queued input recording.
|
|
/// The serialized name is retained for compatibility with saved history.
|
|
#[serde(default, skip_serializing_if = "Option::is_none")]
|
|
pub user_input_order: Option<u64>,
|
|
|
|
/// Output generated for compaction is not an original user-visible assistant message.
|
|
#[serde(default, skip_serializing_if = "std::ops::Not::not")]
|
|
pub compaction_output: bool,
|
|
|
|
/// Copied parent user/assistant context must not become child-local authorization.
|
|
/// The serialized field name is retained for compatibility with saved history.
|
|
#[serde(default, skip_serializing_if = "std::ops::Not::not")]
|
|
pub inherited_user_message: bool,
|
|
|
|
/// Sender context captured by the host when this task message was accepted.
|
|
#[serde(default, skip_serializing_if = "Option::is_none")]
|
|
pub sender_user_messages: Option<Box<SenderUserMessages>>,
|
|
}
|
|
|
|
/// Facts that connect an ordinary accepted input to its originating realtime frames.
|
|
#[derive(Serialize, Deserialize, Debug, Clone, PartialEq, Eq, JsonSchema)]
|
|
pub struct RealtimeHandoffMetadata {
|
|
pub event_id: String,
|
|
pub realtime_session_id: String,
|
|
pub observed_at_ms: i64,
|
|
pub handoff_id: String,
|
|
pub mode: RealtimeHandoffMode,
|
|
pub source_event_ids: Vec<String>,
|
|
}
|
|
|
|
/// Realtime provenance for a saved transcript chunk; the response item owns its text.
|
|
#[derive(Serialize, Deserialize, Debug, Clone, PartialEq, Eq, JsonSchema)]
|
|
pub struct RealtimeSessionInputMetadata {
|
|
pub event_id: String,
|
|
pub realtime_session_id: String,
|
|
pub observed_at_ms: i64,
|
|
pub source_event_ids: Vec<String>,
|
|
}
|
|
|
|
#[derive(Serialize, Deserialize, Clone, PartialEq, Eq, JsonSchema)]
|
|
pub struct ImageReplaySource {
|
|
pub prepared_url_digest: Vec<u8>,
|
|
pub detail: Option<codex_protocol::models::ImageDetail>,
|
|
pub original_data_url: Arc<String>,
|
|
}
|
|
|
|
impl std::fmt::Debug for ImageReplaySource {
|
|
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
|
|
f.debug_struct("ImageReplaySource")
|
|
.field("original_data_url_len", &self.original_data_url.len())
|
|
.finish_non_exhaustive()
|
|
}
|
|
}
|
|
|
|
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();
|
|
self.guardian_source_order_guidance = false;
|
|
self.guardian_retained_omissions = None;
|
|
if let Some(source) = &mut self.retained_source {
|
|
source.complete = false;
|
|
}
|
|
for source in &mut self.guardian_sources {
|
|
source.complete = false;
|
|
}
|
|
}
|
|
}
|
|
|
|
impl ResponseItemEnvelope {
|
|
fn validated_realtime_handoff(&self) -> Option<(&RealtimeHandoffMetadata, &str)> {
|
|
let metadata = self.metadata.as_ref()?.realtime_handoff.as_deref()?;
|
|
let ResponseItem::Message {
|
|
id: Some(_),
|
|
role,
|
|
content,
|
|
..
|
|
} = &self.item
|
|
else {
|
|
return None;
|
|
};
|
|
let [ContentItem::InputText { text }] = content.as_slice() else {
|
|
return None;
|
|
};
|
|
if role != "user" || self.item.turn_id().is_none() {
|
|
return None;
|
|
}
|
|
Some((metadata, text))
|
|
}
|
|
|
|
/// Projects the realtime view of an accepted input without storing a second copy.
|
|
pub fn realtime_handoff_item(&self) -> Option<RealtimeItem> {
|
|
let (metadata, text) = self.validated_realtime_handoff()?;
|
|
Some(RealtimeItem {
|
|
id: metadata.event_id.clone(),
|
|
realtime_session_id: metadata.realtime_session_id.clone(),
|
|
observed_at_ms: Some(metadata.observed_at_ms),
|
|
content: RealtimeItemContent::TurnInputAccepted {
|
|
handoff_id: metadata.handoff_id.clone(),
|
|
turn_id: self.item.turn_id()?.to_string(),
|
|
mode: metadata.mode,
|
|
source_event_ids: metadata.source_event_ids.clone(),
|
|
input_item_id: self.item.id()?.to_string(),
|
|
input: text.to_string(),
|
|
},
|
|
})
|
|
}
|
|
|
|
/// Projects a chunk receipt from its canonical model input.
|
|
pub fn realtime_session_input_item(&self) -> Option<RealtimeItem> {
|
|
let metadata = self.metadata.as_ref()?.realtime_session_input.as_ref()?;
|
|
let ResponseItem::Message {
|
|
id: Some(id),
|
|
role,
|
|
content,
|
|
..
|
|
} = &self.item
|
|
else {
|
|
return None;
|
|
};
|
|
let [ContentItem::InputText { text }] = content.as_slice() else {
|
|
return None;
|
|
};
|
|
if role != "user" {
|
|
return None;
|
|
}
|
|
Some(RealtimeItem {
|
|
id: metadata.event_id.clone(),
|
|
realtime_session_id: metadata.realtime_session_id.clone(),
|
|
observed_at_ms: Some(metadata.observed_at_ms),
|
|
content: RealtimeItemContent::SessionInputSaved {
|
|
reason: SessionInputReason::Chunk,
|
|
source_event_ids: metadata.source_event_ids.clone(),
|
|
input_item_id: id.to_string(),
|
|
input: text.clone(),
|
|
},
|
|
})
|
|
}
|
|
|
|
/// Whether the client requested that this item survive context-window changes.
|
|
pub fn is_always_retained(&self) -> bool {
|
|
self.metadata
|
|
.as_ref()
|
|
.is_some_and(|metadata| metadata.retain_across_windows == Some(true))
|
|
}
|
|
|
|
/// Explicitly include or exclude the item when spawning an agent. With no override,
|
|
/// a full fork applies its ordinary history rules and a fresh child skips the item.
|
|
pub fn child_retention(&self) -> Option<bool> {
|
|
let metadata = self.metadata.as_ref()?;
|
|
metadata
|
|
.retain_in_children
|
|
.or_else(|| metadata.retain_across_windows.filter(|retain| *retain))
|
|
}
|
|
|
|
/// An explicit injection policy exempts the item from the soft carryover budget
|
|
/// wherever its policy pins it. Older developer messages and named turn outputs
|
|
/// could also have been retained by a feature, so keep their historical pricing.
|
|
pub fn has_explicit_retention_pin(&self) -> bool {
|
|
let Some(metadata) = &self.metadata else {
|
|
return false;
|
|
};
|
|
if metadata.retain_in_children.is_some() {
|
|
return true;
|
|
}
|
|
self.is_always_retained()
|
|
&& match &self.item {
|
|
ResponseItem::Message { role, .. } => role != "developer",
|
|
ResponseItem::FunctionCallOutput {
|
|
name: Some(_),
|
|
call_id: None,
|
|
..
|
|
} => false,
|
|
_ => true,
|
|
}
|
|
}
|
|
|
|
/// Wraps a raw Responses API item for persisted history.
|
|
pub fn new(item: ResponseItem) -> Self {
|
|
Self {
|
|
item,
|
|
metadata: None,
|
|
}
|
|
}
|
|
|
|
/// Unwraps the raw Responses API item.
|
|
pub fn into_item(self) -> ResponseItem {
|
|
self.item
|
|
}
|
|
}
|
|
|
|
impl From<ResponseItem> for ResponseItemEnvelope {
|
|
fn from(item: ResponseItem) -> Self {
|
|
Self::new(item)
|
|
}
|
|
}
|
|
|
|
impl From<InjectedResponseItem> for ResponseItemEnvelope {
|
|
fn from(injected: InjectedResponseItem) -> Self {
|
|
let mut envelope = Self::new(injected.item);
|
|
let (across_windows, in_children) = match injected.retention {
|
|
ItemRetention::Unspecified => return envelope,
|
|
ItemRetention::Default => (false, None),
|
|
ItemRetention::Always => (true, Some(true)),
|
|
ItemRetention::AlwaysInThread => (true, Some(false)),
|
|
ItemRetention::AlwaysInChildren => (false, Some(true)),
|
|
};
|
|
let metadata = envelope.metadata.get_or_insert_default();
|
|
metadata.retain_across_windows = Some(across_windows);
|
|
metadata.retain_in_children = in_children;
|
|
envelope
|
|
}
|
|
}
|
|
|
|
impl Deref for ResponseItemEnvelope {
|
|
type Target = ResponseItem;
|
|
|
|
fn deref(&self) -> &Self::Target {
|
|
&self.item
|
|
}
|
|
}
|
|
|
|
impl DerefMut for ResponseItemEnvelope {
|
|
fn deref_mut(&mut self) -> &mut Self::Target {
|
|
&mut self.item
|
|
}
|
|
}
|
|
|
|
impl Borrow<ResponseItem> for ResponseItemEnvelope {
|
|
fn borrow(&self) -> &ResponseItem {
|
|
&self.item
|
|
}
|
|
}
|
|
|
|
/// Persisted rollout item used by core history and rollout storage.
|
|
#[derive(Debug, Clone)]
|
|
pub enum RolloutItem {
|
|
SessionMeta(Box<SessionMetaLine>),
|
|
/// Complete dynamic-tool catalog for subsequent model steps, including an empty catalog.
|
|
DynamicToolsUpdated {
|
|
thread_id: ThreadId,
|
|
tools: Vec<DynamicToolSpec>,
|
|
},
|
|
ResponseItem(ResponseItemEnvelope),
|
|
InterAgentCommunication(InterAgentCommunication),
|
|
InterAgentCommunicationMetadata {
|
|
trigger_turn: bool,
|
|
},
|
|
Compacted(Box<CompactedItem>),
|
|
TurnContext(TurnContextItem),
|
|
TokenUsageRecord(TokenUsageRecord),
|
|
WorldState(WorldStateItem),
|
|
SecurityRiskScore(SecurityRiskScore),
|
|
RetainedContext(RetainedContextEvent),
|
|
EventMsg(EventMsg),
|
|
/// Sparse, model-invisible facts used to reconstruct realtime presentation.
|
|
RealtimeItem(RealtimeItem),
|
|
}
|
|
|
|
impl RolloutItem {
|
|
/// Returns the handoff ID only when the rollout item is a valid acceptance receipt.
|
|
pub fn realtime_handoff_id(&self) -> Option<&str> {
|
|
match self {
|
|
Self::RealtimeItem(RealtimeItem {
|
|
content: RealtimeItemContent::TurnInputAccepted { handoff_id, .. },
|
|
..
|
|
}) => Some(handoff_id),
|
|
Self::ResponseItem(envelope) => envelope
|
|
.validated_realtime_handoff()
|
|
.map(|(metadata, _)| metadata.handoff_id.as_str()),
|
|
_ => None,
|
|
}
|
|
}
|
|
|
|
/// Returns the canonical realtime observation time without constructing a projection.
|
|
pub fn realtime_observed_at_ms(&self) -> Option<i64> {
|
|
match self {
|
|
Self::RealtimeItem(item) => item.observed_at_ms,
|
|
Self::ResponseItem(envelope) => envelope
|
|
.metadata
|
|
.as_ref()?
|
|
.realtime_handoff
|
|
.as_ref()
|
|
.map(|metadata| metadata.observed_at_ms)
|
|
.or_else(|| {
|
|
envelope
|
|
.metadata
|
|
.as_ref()?
|
|
.realtime_session_input
|
|
.as_ref()
|
|
.map(|metadata| metadata.observed_at_ms)
|
|
}),
|
|
_ => None,
|
|
}
|
|
}
|
|
|
|
/// Returns the realtime presentation of this canonical rollout item, if any.
|
|
pub fn realtime_item(&self) -> Option<Cow<'_, RealtimeItem>> {
|
|
match self {
|
|
Self::RealtimeItem(item) => Some(Cow::Borrowed(item)),
|
|
Self::ResponseItem(envelope) => envelope
|
|
.realtime_handoff_item()
|
|
.or_else(|| envelope.realtime_session_input_item())
|
|
.map(Cow::Owned),
|
|
_ => None,
|
|
}
|
|
}
|
|
}
|
|
|
|
impl Serialize for RolloutItem {
|
|
fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
|
|
where
|
|
S: Serializer,
|
|
{
|
|
rollout_payload::RolloutItemWire::from(self).serialize(serializer)
|
|
}
|
|
}
|
|
|
|
impl<'de> Deserialize<'de> for RolloutItem {
|
|
fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
|
|
where
|
|
D: Deserializer<'de>,
|
|
{
|
|
rollout_payload::RolloutItemWire::deserialize(deserializer).map(Into::into)
|
|
}
|
|
}
|
|
|
|
impl JsonSchema for RolloutItem {
|
|
fn schema_name() -> String {
|
|
"RolloutItem".to_string()
|
|
}
|
|
|
|
fn schema_id() -> std::borrow::Cow<'static, str> {
|
|
std::borrow::Cow::Borrowed(concat!(module_path!(), "::RolloutItem"))
|
|
}
|
|
|
|
fn json_schema(generator: &mut schemars::SchemaGenerator) -> schemars::schema::Schema {
|
|
rollout_payload::RolloutItemWire::json_schema(generator)
|
|
}
|
|
}
|
|
|
|
mod guardian_history;
|
|
mod initial_context_ids;
|
|
pub use initial_context_ids::InitialContextIds;
|
|
pub use initial_context_ids::context_ids_from_rollout;
|
|
mod model_id_ordinals;
|
|
pub use model_id_ordinals::next_model_id_ordinals;
|
|
pub use model_id_ordinals::stamp_item_ordinals;
|
|
mod reconciled_retained_context;
|
|
mod retained_context;
|
|
mod sender_user_messages;
|
|
|
|
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;
|
|
pub use retained_context::RetainedContextOrder;
|
|
pub use retained_context::RetainedInputSource;
|
|
pub use retained_context::RetainedSource;
|
|
pub use retained_context::RetainedSourceId;
|
|
pub use retained_context::RetainedSourceRole;
|
|
pub use retained_context::RetainedUserMessage;
|
|
pub use retained_context::VerifiedAnswer;
|
|
pub use retained_context::VerifiedQuestionAnswer;
|
|
mod rollout_payload;
|
|
|
|
pub use guardian_history::GuardianHistoryCheckpoint;
|
|
pub use guardian_history::GuardianRetainedOmissions;
|
|
|
|
#[derive(Clone, Debug, PartialEq)]
|
|
pub struct CompactedItem {
|
|
pub message: String,
|
|
pub replacement_history: Option<Vec<ResponseItemEnvelope>>,
|
|
pub guardian_history: Option<GuardianHistoryCheckpoint>,
|
|
pub retained_context: Option<RetainedContext>,
|
|
pub mcp_resource_origins: Option<McpResourceOriginCheckpoint>,
|
|
pub window_number: Option<u64>,
|
|
pub first_window_id: Option<codex_protocol::ContextWindowId>,
|
|
pub previous_window_id: Option<codex_protocol::ContextWindowId>,
|
|
pub window_id: Option<codex_protocol::ContextWindowId>,
|
|
/// Responses API ID for the model-backed compaction request, when one exists.
|
|
pub compaction_response_id: Option<String>,
|
|
/// Snapshot of the latest reachable token usage record when this compaction was written.
|
|
///
|
|
/// `thread/resume` can restore token usage totals from this field without scanning arbitrarily
|
|
/// far past the compaction.
|
|
pub latest_token_usage_record: Option<TokenUsageRecord>,
|
|
/// Resume metadata for values not represented by the companion rollout records.
|
|
/// Presence distinguishes explicitly persisted values from legacy fallback reconstruction.
|
|
pub resume_metadata: Option<CompactionResumeMetadata>,
|
|
}
|
|
|
|
impl Serialize for CompactedItem {
|
|
fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
|
|
where
|
|
S: Serializer,
|
|
{
|
|
rollout_payload::CompactedItemWire::from(self).serialize(serializer)
|
|
}
|
|
}
|
|
|
|
impl<'de> Deserialize<'de> for CompactedItem {
|
|
fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
|
|
where
|
|
D: Deserializer<'de>,
|
|
{
|
|
rollout_payload::CompactedItemWire::deserialize(deserializer)?
|
|
.try_into()
|
|
.map_err(D::Error::custom)
|
|
}
|
|
}
|
|
|
|
impl JsonSchema for CompactedItem {
|
|
fn schema_name() -> String {
|
|
"CompactedItem".to_string()
|
|
}
|
|
|
|
fn schema_id() -> std::borrow::Cow<'static, str> {
|
|
std::borrow::Cow::Borrowed(concat!(module_path!(), "::CompactedItem"))
|
|
}
|
|
|
|
fn json_schema(generator: &mut schemars::SchemaGenerator) -> schemars::schema::Schema {
|
|
rollout_payload::CompactedItemWire::json_schema(generator)
|
|
}
|
|
}
|
|
|
|
impl From<CompactedItem> for ResponseItem {
|
|
fn from(value: CompactedItem) -> Self {
|
|
ResponseItem::Message {
|
|
status: None,
|
|
encrypted_content: None,
|
|
id: None,
|
|
role: "assistant".to_string(),
|
|
content: vec![ContentItem::OutputText {
|
|
annotations: None,
|
|
logprobs: None,
|
|
text: value.message,
|
|
}],
|
|
phase: None,
|
|
internal_chat_message_metadata_passthrough: None,
|
|
}
|
|
}
|
|
}
|
|
|
|
/// One persisted rollout JSONL record.
|
|
///
|
|
/// This intentionally does not implement Deserialize: JSONL readers must use
|
|
/// codex_rollout's canonical parser so nested decimal values survive the flattened envelope.
|
|
#[derive(Serialize, Clone, JsonSchema)]
|
|
pub struct RolloutLine {
|
|
pub timestamp: String,
|
|
#[serde(default, skip_serializing_if = "Option::is_none")]
|
|
pub ordinal: Option<u64>,
|
|
#[serde(flatten)]
|
|
pub item: RolloutItem,
|
|
}
|
|
|
|
#[derive(Debug, Clone, Deserialize, Serialize)]
|
|
pub struct ResumedHistory {
|
|
pub conversation_id: ThreadId,
|
|
pub history: Arc<Vec<RolloutItem>>,
|
|
/// Store-issued revision for this exact snapshot; clear it when modifying the history.
|
|
#[serde(skip)]
|
|
pub history_revision: Option<String>,
|
|
pub rollout_path: Option<PathBuf>,
|
|
}
|
|
|
|
#[derive(Debug, Clone, Deserialize, Serialize)]
|
|
pub enum InitialHistory {
|
|
New,
|
|
Cleared,
|
|
Resumed(ResumedHistory),
|
|
Forked(Vec<RolloutItem>),
|
|
}
|
|
|
|
impl InitialHistory {
|
|
pub fn scan_rollout_items(&self, mut predicate: impl FnMut(&RolloutItem) -> bool) -> bool {
|
|
match self {
|
|
Self::New | Self::Cleared => false,
|
|
Self::Resumed(resumed) => resumed.history.iter().any(&mut predicate),
|
|
Self::Forked(items) => items.iter().any(predicate),
|
|
}
|
|
}
|
|
|
|
pub fn forked_from_id(&self) -> Option<ThreadId> {
|
|
match self {
|
|
Self::New | Self::Cleared => None,
|
|
Self::Resumed(resumed) => resumed.history.iter().find_map(|item| match item {
|
|
RolloutItem::SessionMeta(meta_line) => meta_line.meta.forked_from_id,
|
|
_ => None,
|
|
}),
|
|
Self::Forked(items) => items.iter().find_map(|item| match item {
|
|
RolloutItem::SessionMeta(meta_line) => Some(meta_line.meta.id),
|
|
_ => None,
|
|
}),
|
|
}
|
|
}
|
|
|
|
pub fn session_cwd(&self) -> Option<PathBuf> {
|
|
match self {
|
|
Self::New | Self::Cleared => None,
|
|
Self::Resumed(resumed) => session_cwd_from_items(&resumed.history),
|
|
Self::Forked(items) => session_cwd_from_items(items),
|
|
}
|
|
}
|
|
|
|
pub fn get_rollout_items(&self) -> &[RolloutItem] {
|
|
match self {
|
|
Self::New | Self::Cleared => &[],
|
|
Self::Resumed(resumed) => &resumed.history,
|
|
Self::Forked(items) => items,
|
|
}
|
|
}
|
|
|
|
pub fn get_base_instructions(&self) -> Option<BaseInstructions> {
|
|
match self {
|
|
Self::New | Self::Cleared => None,
|
|
Self::Resumed(resumed) => resumed.history.iter().find_map(|item| match item {
|
|
RolloutItem::SessionMeta(meta_line) => meta_line.meta.base_instructions.clone(),
|
|
_ => None,
|
|
}),
|
|
Self::Forked(items) => items.iter().find_map(|item| match item {
|
|
RolloutItem::SessionMeta(meta_line) => meta_line.meta.base_instructions.clone(),
|
|
_ => None,
|
|
}),
|
|
}
|
|
}
|
|
|
|
pub fn get_dynamic_tools(&self) -> Option<Vec<DynamicToolSpec>> {
|
|
latest_dynamic_tools(self.get_rollout_items()).map(<[_]>::to_vec)
|
|
}
|
|
|
|
pub fn get_selected_capability_roots(&self) -> Vec<SelectedCapabilityRoot> {
|
|
self.get_session_meta()
|
|
.map(|meta| meta.selected_capability_roots.clone())
|
|
.unwrap_or_default()
|
|
}
|
|
|
|
pub fn get_multi_agent_version(&self) -> Option<MultiAgentVersion> {
|
|
match self {
|
|
Self::New | Self::Cleared => None,
|
|
Self::Resumed(resumed) => {
|
|
multi_agent_version_from_items(&resumed.history, Some(resumed.conversation_id))
|
|
}
|
|
Self::Forked(items) => multi_agent_version_from_items(items, /*thread_id*/ None),
|
|
}
|
|
}
|
|
|
|
pub fn get_history_mode(&self, default_history_mode: ThreadHistoryMode) -> ThreadHistoryMode {
|
|
match self {
|
|
Self::New | Self::Cleared => default_history_mode,
|
|
Self::Resumed(_) | Self::Forked(_) => self
|
|
.get_session_meta()
|
|
.map(|meta| meta.history_mode)
|
|
.unwrap_or(default_history_mode),
|
|
}
|
|
}
|
|
|
|
pub fn get_resumed_session_sources(&self) -> Option<(SessionSource, Option<ThreadSource>)> {
|
|
let meta = self.get_resumed_session_meta()?;
|
|
Some((meta.source.clone(), meta.thread_source.clone()))
|
|
}
|
|
|
|
pub fn get_resumed_thread_source(&self) -> Option<ThreadSource> {
|
|
self.get_resumed_session_meta()
|
|
.and_then(|meta| meta.thread_source.clone())
|
|
}
|
|
|
|
pub fn get_session_originator(&self) -> Option<String> {
|
|
self.get_session_meta()
|
|
.map(|meta| meta.originator.clone())
|
|
.filter(|originator| !originator.is_empty())
|
|
}
|
|
|
|
pub fn get_resumed_parent_thread_id(&self) -> Option<ThreadId> {
|
|
self.get_resumed_session_meta()
|
|
.and_then(|meta| meta.parent_thread_id)
|
|
}
|
|
|
|
fn get_session_meta(&self) -> Option<&SessionMeta> {
|
|
match self {
|
|
Self::New | Self::Cleared => None,
|
|
Self::Resumed(resumed) => resumed.history.iter().find_map(|item| match item {
|
|
RolloutItem::SessionMeta(meta_line) => Some(&meta_line.meta),
|
|
_ => None,
|
|
}),
|
|
Self::Forked(items) => items.iter().find_map(|item| match item {
|
|
RolloutItem::SessionMeta(meta_line) => Some(&meta_line.meta),
|
|
_ => None,
|
|
}),
|
|
}
|
|
}
|
|
|
|
fn get_resumed_session_meta(&self) -> Option<&SessionMeta> {
|
|
match self {
|
|
Self::New | Self::Cleared | Self::Forked(_) => None,
|
|
Self::Resumed(resumed) => resumed.history.iter().find_map(|item| match item {
|
|
RolloutItem::SessionMeta(meta_line) => Some(&meta_line.meta),
|
|
_ => None,
|
|
}),
|
|
}
|
|
}
|
|
}
|
|
|
|
fn session_cwd_from_items(items: &[RolloutItem]) -> Option<PathBuf> {
|
|
items.iter().find_map(|item| match item {
|
|
RolloutItem::SessionMeta(meta_line) => Some(meta_line.meta.cwd.clone()),
|
|
_ => None,
|
|
})
|
|
}
|
|
|
|
/// Returns the latest catalog, preserving explicit clears and falling back to creation metadata.
|
|
pub fn latest_dynamic_tools(items: &[RolloutItem]) -> Option<&[DynamicToolSpec]> {
|
|
let metadata = items.iter().find_map(|item| match item {
|
|
RolloutItem::SessionMeta(meta) => Some(&meta.meta),
|
|
_ => None,
|
|
});
|
|
items
|
|
.iter()
|
|
.rev()
|
|
.find_map(|item| {
|
|
let (thread_id, tools) = match item {
|
|
RolloutItem::DynamicToolsUpdated { thread_id, tools } => (thread_id, tools),
|
|
RolloutItem::Compacted(compacted) => {
|
|
let snapshot = compacted.resume_metadata.as_ref()?.dynamic_tools.as_ref()?;
|
|
(&snapshot.thread_id, &snapshot.tools)
|
|
}
|
|
_ => return None,
|
|
};
|
|
metadata
|
|
.is_none_or(|meta| meta.id == *thread_id)
|
|
.then_some(tools.as_slice())
|
|
})
|
|
.or_else(|| metadata.and_then(|meta| meta.dynamic_tools.as_deref()))
|
|
}
|
|
|
|
/// Returns a thread's latest plugin selection, with a turn-context fallback.
|
|
///
|
|
/// Forked history may contain ancestor snapshots, and compaction may append a
|
|
/// frozen turn context after an update. Neither can replace thread-owned settings.
|
|
/// Without an owned snapshot, only the latest turn context supplies the initial
|
|
/// selection; a missing field must not resurrect a selection from an older turn.
|
|
pub fn latest_disabled_plugin_ids(items: &[RolloutItem], thread_id: ThreadId) -> Option<&[String]> {
|
|
if let Some(ids) = items.iter().rev().find_map(|item| {
|
|
if let RolloutItem::EventMsg(EventMsg::ThreadSettingsApplied(event)) = item
|
|
&& event.thread_id == Some(thread_id)
|
|
{
|
|
Some(event.thread_settings.disabled_plugin_ids.as_slice())
|
|
} else {
|
|
None
|
|
}
|
|
}) {
|
|
return Some(ids);
|
|
}
|
|
items
|
|
.iter()
|
|
.rev()
|
|
.find_map(|item| match item {
|
|
RolloutItem::TurnContext(context) => Some(context),
|
|
_ => None,
|
|
})
|
|
.and_then(|context| context.disabled_plugin_ids.as_deref())
|
|
}
|
|
|
|
fn multi_agent_version_from_items(
|
|
items: &[RolloutItem],
|
|
thread_id: Option<ThreadId>,
|
|
) -> Option<MultiAgentVersion> {
|
|
let session_meta_version = items.iter().rev().find_map(|item| match item {
|
|
RolloutItem::SessionMeta(meta_line)
|
|
if thread_id.is_none_or(|thread_id| meta_line.meta.id == thread_id) =>
|
|
{
|
|
meta_line.meta.multi_agent_version
|
|
}
|
|
_ => None,
|
|
});
|
|
|
|
session_meta_version.or_else(|| items.iter().rev().find_map(resume_multi_agent_version))
|
|
}
|
|
|
|
#[cfg(test)]
|
|
#[path = "tests.rs"]
|
|
mod tests;
|