mirror of
https://github.com/openai/codex.git
synced 2026-11-25 23:30:49 +00:00
add thread history projection observers
This commit is contained in:
@@ -20,6 +20,7 @@ codex-install-context = { workspace = true }
|
||||
codex-protocol = { workspace = true }
|
||||
codex-rollout = { workspace = true }
|
||||
codex-state = { workspace = true }
|
||||
codex-thread-store-protocol = { workspace = true }
|
||||
codex-utils-path = { workspace = true }
|
||||
serde = { workspace = true, features = ["derive"] }
|
||||
serde_json = { workspace = true }
|
||||
|
||||
@@ -212,13 +212,14 @@ impl ThreadStore for InMemoryThreadStore {
|
||||
}
|
||||
|
||||
async fn append_items(&self, params: AppendThreadItemsParams) -> ThreadStoreResult<()> {
|
||||
let AppendThreadItemsParams {
|
||||
thread_id,
|
||||
items,
|
||||
thread_history_mutations: _,
|
||||
} = params;
|
||||
let mut state = self.state.lock().await;
|
||||
state.calls.append_items += 1;
|
||||
state
|
||||
.histories
|
||||
.entry(params.thread_id)
|
||||
.or_default()
|
||||
.extend(params.items);
|
||||
state.histories.entry(thread_id).or_default().extend(items);
|
||||
Ok(())
|
||||
}
|
||||
|
||||
|
||||
@@ -12,6 +12,12 @@ mod store;
|
||||
mod thread_metadata_sync;
|
||||
mod types;
|
||||
|
||||
pub use codex_thread_store_protocol::LifecycleMutation;
|
||||
pub use codex_thread_store_protocol::ThreadHistoryMutation;
|
||||
pub use codex_thread_store_protocol::ThreadHistoryMutationMetadata;
|
||||
pub use codex_thread_store_protocol::ThreadHistoryProjectionObserver;
|
||||
pub use codex_thread_store_protocol::ThreadItemMutation;
|
||||
pub use codex_thread_store_protocol::TurnSummaryMutation;
|
||||
pub use error::ThreadStoreError;
|
||||
pub use error::ThreadStoreResult;
|
||||
pub use in_memory::InMemoryThreadStore;
|
||||
|
||||
@@ -16,6 +16,7 @@ use crate::ReadThreadParams;
|
||||
use crate::ResumeThreadParams;
|
||||
use crate::StoredThread;
|
||||
use crate::StoredThreadHistory;
|
||||
use crate::ThreadHistoryProjectionObserver;
|
||||
use crate::ThreadMetadataPatch;
|
||||
use crate::ThreadStore;
|
||||
use crate::ThreadStoreResult;
|
||||
@@ -32,6 +33,7 @@ pub struct LiveThread {
|
||||
thread_id: ThreadId,
|
||||
thread_store: Arc<dyn ThreadStore>,
|
||||
metadata_sync: Arc<Mutex<ThreadMetadataSync>>,
|
||||
thread_history_projection_observers: Arc<Mutex<Vec<Box<dyn ThreadHistoryProjectionObserver>>>>,
|
||||
}
|
||||
|
||||
/// Owns a live thread while session initialization is still fallible.
|
||||
@@ -95,6 +97,7 @@ impl LiveThread {
|
||||
thread_id,
|
||||
thread_store,
|
||||
metadata_sync: Arc::new(Mutex::new(metadata_sync)),
|
||||
thread_history_projection_observers: default_thread_history_projection_observers(),
|
||||
})
|
||||
}
|
||||
|
||||
@@ -126,18 +129,37 @@ impl LiveThread {
|
||||
thread_id,
|
||||
thread_store,
|
||||
metadata_sync: Arc::new(Mutex::new(metadata_sync)),
|
||||
thread_history_projection_observers: default_thread_history_projection_observers(),
|
||||
})
|
||||
}
|
||||
|
||||
/// Installs append observers that derive typed thread-history mutations for this live thread.
|
||||
pub fn with_thread_history_projection_observers(
|
||||
mut self,
|
||||
thread_history_projection_observers: Vec<Box<dyn ThreadHistoryProjectionObserver>>,
|
||||
) -> Self {
|
||||
self.thread_history_projection_observers =
|
||||
Arc::new(Mutex::new(thread_history_projection_observers));
|
||||
self
|
||||
}
|
||||
|
||||
pub async fn append_items(&self, items: &[RolloutItem]) -> ThreadStoreResult<()> {
|
||||
let canonical_items = persisted_rollout_items(items);
|
||||
if canonical_items.is_empty() {
|
||||
let thread_history_mutations = self
|
||||
.thread_history_projection_observers
|
||||
.lock()
|
||||
.await
|
||||
.iter_mut()
|
||||
.flat_map(|observer| observer.observe_append(canonical_items.as_slice(), items))
|
||||
.collect::<Vec<_>>();
|
||||
if canonical_items.is_empty() && thread_history_mutations.is_empty() {
|
||||
return Ok(());
|
||||
}
|
||||
self.thread_store
|
||||
.append_items(AppendThreadItemsParams {
|
||||
thread_id: self.thread_id,
|
||||
items: canonical_items.clone(),
|
||||
thread_history_mutations,
|
||||
})
|
||||
.await?;
|
||||
let update = self
|
||||
@@ -294,3 +316,8 @@ impl LiveThread {
|
||||
Ok(())
|
||||
}
|
||||
}
|
||||
|
||||
fn default_thread_history_projection_observers()
|
||||
-> Arc<Mutex<Vec<Box<dyn ThreadHistoryProjectionObserver>>>> {
|
||||
Arc::new(Mutex::new(Vec::new()))
|
||||
}
|
||||
|
||||
@@ -77,11 +77,18 @@ pub(super) async fn append_items(
|
||||
store: &LocalThreadStore,
|
||||
params: AppendThreadItemsParams,
|
||||
) -> ThreadStoreResult<()> {
|
||||
let recorder = store.live_recorder(params.thread_id).await?;
|
||||
let AppendThreadItemsParams {
|
||||
thread_id,
|
||||
items,
|
||||
thread_history_mutations: _,
|
||||
} = params;
|
||||
let recorder = store.live_recorder(thread_id).await?;
|
||||
recorder
|
||||
.record_canonical_items(params.items.as_slice())
|
||||
.record_canonical_items(items.as_slice())
|
||||
.await
|
||||
.map_err(thread_store_io_error)?;
|
||||
// TODO: Persist accepted thread-history mutations here if the local store grows durable
|
||||
// ThreadItem, turn summary, or lifecycle tables.
|
||||
// LiveThread applies metadata immediately after append_items returns. Wait for the local
|
||||
// writer so SQLite never gets ahead of JSONL for accepted live appends.
|
||||
recorder.flush().await.map_err(thread_store_io_error)
|
||||
|
||||
@@ -329,6 +329,7 @@ mod tests {
|
||||
.append_items(AppendThreadItemsParams {
|
||||
thread_id,
|
||||
items: vec![user_message_item("first live write")],
|
||||
thread_history_mutations: Vec::new(),
|
||||
})
|
||||
.await
|
||||
.expect("append live item");
|
||||
@@ -351,6 +352,7 @@ mod tests {
|
||||
.append_items(AppendThreadItemsParams {
|
||||
thread_id,
|
||||
items: vec![user_message_item("write after shutdown")],
|
||||
thread_history_mutations: Vec::new(),
|
||||
})
|
||||
.await
|
||||
.expect_err("shutdown should remove the live thread writer");
|
||||
@@ -382,6 +384,7 @@ mod tests {
|
||||
.append_items(AppendThreadItemsParams {
|
||||
thread_id,
|
||||
items: vec![user_message_item("raw append")],
|
||||
thread_history_mutations: Vec::new(),
|
||||
})
|
||||
.await
|
||||
.expect("append raw item");
|
||||
@@ -666,6 +669,7 @@ mod tests {
|
||||
.append_items(AppendThreadItemsParams {
|
||||
thread_id,
|
||||
items: vec![user_message_item("write after discard")],
|
||||
thread_history_mutations: Vec::new(),
|
||||
})
|
||||
.await
|
||||
.expect_err("discard should remove the live thread writer");
|
||||
@@ -689,6 +693,7 @@ mod tests {
|
||||
.append_items(AppendThreadItemsParams {
|
||||
thread_id,
|
||||
items: vec![user_message_item("before resume")],
|
||||
thread_history_mutations: Vec::new(),
|
||||
})
|
||||
.await
|
||||
.expect("append initial item");
|
||||
@@ -724,6 +729,7 @@ mod tests {
|
||||
.append_items(AppendThreadItemsParams {
|
||||
thread_id,
|
||||
items: vec![user_message_item("after resume")],
|
||||
thread_history_mutations: Vec::new(),
|
||||
})
|
||||
.await
|
||||
.expect("append resumed item");
|
||||
@@ -835,6 +841,7 @@ mod tests {
|
||||
.append_items(AppendThreadItemsParams {
|
||||
thread_id,
|
||||
items: vec![user_message_item("external history item")],
|
||||
thread_history_mutations: Vec::new(),
|
||||
})
|
||||
.await
|
||||
.expect("append live item");
|
||||
@@ -921,6 +928,7 @@ mod tests {
|
||||
.append_items(AppendThreadItemsParams {
|
||||
thread_id,
|
||||
items: vec![user_message_item("archived live history item")],
|
||||
thread_history_mutations: Vec::new(),
|
||||
})
|
||||
.await
|
||||
.expect("append live item");
|
||||
@@ -979,6 +987,7 @@ mod tests {
|
||||
.append_items(AppendThreadItemsParams {
|
||||
thread_id,
|
||||
items: vec![user_message_item("path read")],
|
||||
thread_history_mutations: Vec::new(),
|
||||
})
|
||||
.await
|
||||
.expect("append item");
|
||||
|
||||
@@ -15,11 +15,11 @@ use codex_protocol::protocol::SessionSource;
|
||||
use codex_protocol::protocol::ThreadMemoryMode as MemoryMode;
|
||||
use codex_protocol::protocol::ThreadSource;
|
||||
use codex_protocol::protocol::TokenUsage;
|
||||
use codex_thread_store_protocol::ThreadHistoryMutation;
|
||||
use serde::Deserialize;
|
||||
use serde::Deserializer;
|
||||
use serde::Serialize;
|
||||
use serde::Serializer;
|
||||
|
||||
mod optional_option {
|
||||
use super::*;
|
||||
|
||||
@@ -101,6 +101,9 @@ pub struct AppendThreadItemsParams {
|
||||
pub thread_id: ThreadId,
|
||||
/// Items to append in order.
|
||||
pub items: Vec<RolloutItem>,
|
||||
/// Typed derived thread-history mutations committed with the canonical rollout append.
|
||||
#[serde(default)]
|
||||
pub thread_history_mutations: Vec<ThreadHistoryMutation>,
|
||||
}
|
||||
|
||||
/// Parameters for loading persisted history for resume, fork, rollback, and memory jobs.
|
||||
|
||||
Reference in New Issue
Block a user