add thread history projection observers

This commit is contained in:
Tom Wiltzius
2026-06-04 21:40:44 -07:00
parent 2ca3810005
commit ca445e0899
25 changed files with 1263 additions and 43 deletions
@@ -0,0 +1,6 @@
load("//:defs.bzl", "codex_rust_crate")
codex_rust_crate(
name = "thread-store-protocol",
crate_name = "codex_thread_store_protocol",
)
+21
View File
@@ -0,0 +1,21 @@
[package]
edition.workspace = true
license.workspace = true
name = "codex-thread-store-protocol"
version.workspace = true
[lib]
name = "codex_thread_store_protocol"
path = "src/lib.rs"
doctest = false
[lints]
workspace = true
[dependencies]
codex-protocol = { workspace = true }
serde = { workspace = true, features = ["derive"] }
serde_json = { workspace = true }
[dev-dependencies]
pretty_assertions = { workspace = true }
+71
View File
@@ -0,0 +1,71 @@
use codex_protocol::protocol::RolloutItem;
use serde::Deserialize;
use serde::Serialize;
use serde_json::Value;
#[cfg(test)]
#[path = "tests.rs"]
mod tests;
/// Metadata shared by one derived thread-history mutation.
#[derive(Serialize, Deserialize, Debug, Clone, PartialEq)]
pub struct ThreadHistoryMutationMetadata {
pub schema_version: u32,
}
/// One typed thread-item mutation emitted alongside a durable rollout append.
#[derive(Serialize, Deserialize, Debug, Clone, PartialEq)]
pub struct ThreadItemMutation {
#[serde(flatten)]
pub metadata: ThreadHistoryMutationMetadata,
pub payload: Value,
}
/// One typed turn-summary mutation emitted alongside a durable rollout append.
#[derive(Serialize, Deserialize, Debug, Clone, PartialEq)]
pub struct TurnSummaryMutation {
#[serde(flatten)]
pub metadata: ThreadHistoryMutationMetadata,
pub payload: Value,
}
/// One typed lifecycle mutation emitted alongside a durable rollout append.
#[derive(Serialize, Deserialize, Debug, Clone, PartialEq)]
pub struct LifecycleMutation {
#[serde(flatten)]
pub metadata: ThreadHistoryMutationMetadata,
pub payload: Value,
}
/// Derived thread-history mutation emitted alongside a durable rollout append.
///
/// Store implementations match this finite union to route each mutation to its durable indexed
/// view. Payloads stay storage-neutral here because this crate cannot depend on app-server
/// `ThreadItem`; examples include thread-item materializations, turn summaries, and lifecycle
/// outbox rows derived from the same accepted rollout append.
#[derive(Serialize, Deserialize, Debug, Clone, PartialEq)]
#[serde(tag = "type", rename_all = "snake_case")]
pub enum ThreadHistoryMutation {
ThreadItem(ThreadItemMutation),
TurnSummary(TurnSummaryMutation),
Lifecycle(LifecycleMutation),
}
/// Observes a live append batch and derives typed thread-history mutations for the store.
///
/// Implementations may materialize higher-level ThreadItem views from rollout items, but the
/// store boundary only receives storage-neutral mutation payloads. Examples include app-server
/// thread-item rows, turn summary rows, or lifecycle/search updates that should be committed
/// alongside the accepted canonical rollout append.
///
/// `persisted_rollout_items` are the new canonical items that the store will append.
/// `projection_source_events` are the same append's new unfiltered raw rollout items so observers
/// can see filtered events such as exec begin; open/non-terminal projected items belong in the
/// observer's checkpoint, not in this append argument.
pub trait ThreadHistoryProjectionObserver: Send {
fn observe_append(
&mut self,
persisted_rollout_items: &[RolloutItem],
projection_source_events: &[RolloutItem],
) -> Vec<ThreadHistoryMutation>;
}
@@ -0,0 +1,27 @@
use super::*;
use pretty_assertions::assert_eq;
use serde_json::json;
#[test]
fn thread_history_mutation_round_trips_typed_variant() {
let mutation = ThreadHistoryMutation::ThreadItem(ThreadItemMutation {
metadata: ThreadHistoryMutationMetadata { schema_version: 1 },
payload: json!({ "itemKey": "item-1" }),
});
let value = json!({
"type": "thread_item",
"schema_version": 1,
"payload": {
"itemKey": "item-1",
},
});
assert_eq!(
serde_json::to_value(&mutation).expect("serialize mutation"),
value
);
assert_eq!(
serde_json::from_value::<ThreadHistoryMutation>(value).expect("deserialize mutation"),
mutation
);
}