Add rollout reference protocol

This commit is contained in:
Friel
2026-06-12 21:16:50 +00:00
parent 8f2d6416ce
commit 0f8d24a68a
4 changed files with 122 additions and 0 deletions
@@ -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(_) => {}
}
+1
View File
@@ -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;
+30
View File
@@ -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<SegmentId>,
#[serde(skip_serializing_if = "Option::is_none")]
pub forked_from_id: Option<ThreadId>,
#[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<GitInfo>,
}
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<ThreadId>,
#[serde(default, skip_serializing_if = "Option::is_none")]
pub rollout_timestamp: Option<String>,
#[serde(default, skip_serializing_if = "Option::is_none")]
pub segment_id: Option<SegmentId>,
#[serde(default = "default_rollout_reference_depth")]
pub max_depth: usize,
#[serde(default, skip_serializing_if = "Option::is_none")]
pub nth_user_message: Option<usize>,
#[serde(default, skip_serializing_if = "Option::is_none")]
pub compacted_replacement_history_filter_texts: Option<Vec<String>>,
}
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),
+90
View File
@@ -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<ThreadId> for String {
}
}
impl SegmentId {
pub fn new() -> Self {
Self {
uuid: Uuid::now_v7(),
}
}
pub fn from_string(s: &str) -> Result<Self, uuid::Error> {
Ok(Self {
uuid: Uuid::parse_str(s)?,
})
}
}
impl TryFrom<&str> for SegmentId {
type Error = uuid::Error;
fn try_from(value: &str) -> Result<Self, Self::Error> {
Self::from_string(value)
}
}
impl TryFrom<String> for SegmentId {
type Error = uuid::Error;
fn try_from(value: String) -> Result<Self, Self::Error> {
Self::from_string(value.as_str())
}
}
impl From<SegmentId> 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<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
where
@@ -71,6 +125,15 @@ impl Serialize for ThreadId {
}
}
impl Serialize for SegmentId {
fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
where
S: serde::Serializer,
{
serializer.collect_str(&self.uuid)
}
}
impl<'de> Deserialize<'de> for ThreadId {
fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
where
@@ -82,6 +145,17 @@ impl<'de> Deserialize<'de> for ThreadId {
}
}
impl<'de> Deserialize<'de> for SegmentId {
fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
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 {
<String>::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());
}
}