From 0f8d24a68af9237289e6215e03545567b385b1e6 Mon Sep 17 00:00:00 2001 From: Friel Date: Fri, 12 Jun 2026 21:16:50 +0000 Subject: [PATCH] Add rollout reference protocol --- .../src/protocol/thread_history.rs | 1 + codex-rs/protocol/src/lib.rs | 1 + codex-rs/protocol/src/protocol.rs | 30 +++++++ codex-rs/protocol/src/thread_id.rs | 90 +++++++++++++++++++ 4 files changed, 122 insertions(+) diff --git a/codex-rs/app-server-protocol/src/protocol/thread_history.rs b/codex-rs/app-server-protocol/src/protocol/thread_history.rs index f499e568fe..09bfa10a5c 100644 --- a/codex-rs/app-server-protocol/src/protocol/thread_history.rs +++ b/codex-rs/app-server-protocol/src/protocol/thread_history.rs @@ -242,6 +242,7 @@ impl ThreadHistoryBuilder { RolloutItem::Compacted(payload) => self.handle_compacted(payload), RolloutItem::ResponseItem(item) => self.handle_response_item(item), RolloutItem::InterAgentCommunication(_) + | RolloutItem::RolloutReference(_) | RolloutItem::TurnContext(_) | RolloutItem::SessionMeta(_) => {} } diff --git a/codex-rs/protocol/src/lib.rs b/codex-rs/protocol/src/lib.rs index 3be16d08b7..d8cb2e5cd9 100644 --- a/codex-rs/protocol/src/lib.rs +++ b/codex-rs/protocol/src/lib.rs @@ -6,6 +6,7 @@ mod thread_id; mod tool_name; pub use agent_path::AgentPath; pub use session_id::SessionId; +pub use thread_id::SegmentId; pub use thread_id::ThreadId; pub use tool_name::ToolName; pub mod approvals; diff --git a/codex-rs/protocol/src/protocol.rs b/codex-rs/protocol/src/protocol.rs index f3b9a8ff35..616a12382c 100644 --- a/codex-rs/protocol/src/protocol.rs +++ b/codex-rs/protocol/src/protocol.rs @@ -15,6 +15,7 @@ use std::time::Duration; use strum_macros::EnumIter; use crate::AgentPath; +use crate::SegmentId; use crate::SessionId; use crate::ThreadId; use crate::approvals::ElicitationRequestEvent; @@ -2802,6 +2803,7 @@ fn multi_agent_version_from_items( items.iter().rev().find_map(|item| match item { RolloutItem::TurnContext(turn_context) => turn_context.multi_agent_version, RolloutItem::SessionMeta(_) + | RolloutItem::RolloutReference(_) | RolloutItem::ResponseItem(_) | RolloutItem::InterAgentCommunication(_) | RolloutItem::Compacted(_) @@ -2827,6 +2829,8 @@ pub enum MultiAgentVersion { #[derive(Serialize, Deserialize, Clone, Debug, JsonSchema, TS)] pub struct SessionMeta { pub id: ThreadId, + #[serde(default, skip_serializing_if = "Option::is_none")] + pub segment_id: Option, #[serde(skip_serializing_if = "Option::is_none")] pub forked_from_id: Option, #[serde(skip_serializing_if = "Option::is_none")] @@ -2866,6 +2870,7 @@ impl Default for SessionMeta { fn default() -> Self { SessionMeta { id: ThreadId::default(), + segment_id: None, forked_from_id: None, parent_thread_id: None, timestamp: String::new(), @@ -2894,10 +2899,35 @@ pub struct SessionMetaLine { pub git: Option, } +pub const DEFAULT_ROLLOUT_REFERENCE_DEPTH: usize = 2; + +#[derive(Serialize, Deserialize, Debug, Clone, JsonSchema, TS)] +pub struct RolloutReferenceItem { + pub rollout_path: PathBuf, + #[serde(default, skip_serializing_if = "Option::is_none")] + pub thread_id: Option, + #[serde(default, skip_serializing_if = "Option::is_none")] + pub rollout_timestamp: Option, + #[serde(default, skip_serializing_if = "Option::is_none")] + pub segment_id: Option, + #[serde(default = "default_rollout_reference_depth")] + pub max_depth: usize, + #[serde(default, skip_serializing_if = "Option::is_none")] + pub nth_user_message: Option, + #[serde(default, skip_serializing_if = "Option::is_none")] + pub compacted_replacement_history_filter_texts: Option>, +} + +fn default_rollout_reference_depth() -> usize { + DEFAULT_ROLLOUT_REFERENCE_DEPTH +} + #[derive(Serialize, Deserialize, Debug, Clone, JsonSchema, TS)] #[serde(tag = "type", content = "payload", rename_all = "snake_case")] pub enum RolloutItem { SessionMeta(SessionMetaLine), + #[serde(alias = "fork_reference")] + RolloutReference(RolloutReferenceItem), ResponseItem(ResponseItem), /// Durable delivery metadata reconstructed as a model-visible `agent_message`. InterAgentCommunication(InterAgentCommunication), diff --git a/codex-rs/protocol/src/thread_id.rs b/codex-rs/protocol/src/thread_id.rs index d6e9a8825e..c5eb65588b 100644 --- a/codex-rs/protocol/src/thread_id.rs +++ b/codex-rs/protocol/src/thread_id.rs @@ -14,6 +14,12 @@ pub struct ThreadId { pub(crate) uuid: Uuid, } +#[derive(Debug, Clone, Copy, PartialEq, Eq, TS, Hash)] +#[ts(type = "string")] +pub struct SegmentId { + uuid: Uuid, +} + impl ThreadId { pub fn new() -> Self { Self { @@ -50,18 +56,66 @@ impl From for String { } } +impl SegmentId { + pub fn new() -> Self { + Self { + uuid: Uuid::now_v7(), + } + } + + pub fn from_string(s: &str) -> Result { + Ok(Self { + uuid: Uuid::parse_str(s)?, + }) + } +} + +impl TryFrom<&str> for SegmentId { + type Error = uuid::Error; + + fn try_from(value: &str) -> Result { + Self::from_string(value) + } +} + +impl TryFrom for SegmentId { + type Error = uuid::Error; + + fn try_from(value: String) -> Result { + Self::from_string(value.as_str()) + } +} + +impl From for String { + fn from(value: SegmentId) -> Self { + value.to_string() + } +} + impl Default for ThreadId { fn default() -> Self { Self::new() } } +impl Default for SegmentId { + fn default() -> Self { + Self::new() + } +} + impl Display for ThreadId { fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { Display::fmt(&self.uuid, f) } } +impl Display for SegmentId { + fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { + Display::fmt(&self.uuid, f) + } +} + impl Serialize for ThreadId { fn serialize(&self, serializer: S) -> Result where @@ -71,6 +125,15 @@ impl Serialize for ThreadId { } } +impl Serialize for SegmentId { + fn serialize(&self, serializer: S) -> Result + where + S: serde::Serializer, + { + serializer.collect_str(&self.uuid) + } +} + impl<'de> Deserialize<'de> for ThreadId { fn deserialize(deserializer: D) -> Result where @@ -82,6 +145,17 @@ impl<'de> Deserialize<'de> for ThreadId { } } +impl<'de> Deserialize<'de> for SegmentId { + fn deserialize(deserializer: D) -> Result + where + D: serde::Deserializer<'de>, + { + let value = String::deserialize(deserializer)?; + let uuid = Uuid::parse_str(&value).map_err(serde::de::Error::custom)?; + Ok(Self { uuid }) + } +} + impl JsonSchema for ThreadId { fn schema_name() -> String { "ThreadId".to_string() @@ -92,6 +166,16 @@ impl JsonSchema for ThreadId { } } +impl JsonSchema for SegmentId { + fn schema_name() -> String { + "SegmentId".to_string() + } + + fn json_schema(generator: &mut SchemaGenerator) -> Schema { + ::json_schema(generator) + } +} + #[cfg(test)] mod tests { use super::*; @@ -100,4 +184,10 @@ mod tests { let id = ThreadId::default(); assert_ne!(id.uuid, Uuid::nil()); } + + #[test] + fn test_segment_id_default_is_not_zeroes() { + let id = SegmentId::default(); + assert_ne!(id.uuid, Uuid::nil()); + } }