Files
codex/codex-rs/thread-context-ids/src/lib.rs
T
Ruslan Nigmatullin 30607648d9 Centralize request and prefix ID handling in ThreadContextIds (#62635)
## What changed

Move ID derivation for base instructions and Responses Lite tool definitions
into `ThreadContextIds`. These items keep the same thread-scoped,
content-derived UUIDs when rebuilt for a request.

Invoke prefix ID assignment for newly rendered items during history
replacement and when publishing initial context into empty history.
The UUID allocator leaves these items to ordinary response item ID
allocation.

GitOrigin-RevId: f230a58e3264894bddfd5a250a4b6d1baeabecf0
2026-11-20 01:20:38 +00:00

71 lines
2.1 KiB
Rust

//! Allocates thread context IDs shared by history and inference.
use codex_protocol::ContextWindowId;
use codex_protocol::ResponseItemId;
use codex_protocol::ThreadId;
use codex_protocol::models::ResponseItem;
use uuid::Uuid;
/// Allocates IDs for one thread's context.
#[derive(Debug, Clone)]
pub struct ThreadContextIds(Allocator);
#[derive(Debug, Clone)]
enum Allocator {
Uuid,
}
impl ThreadContextIds {
/// Creates a thread allocator. Live sessions should use `InitialHistory::initialize_context_ids`
/// to initialize the active window. For fixtures use `codex_thread_context_ids_test_support::thread_context_ids()`.
pub fn for_thread() -> Self {
Self(Allocator::Uuid)
}
pub fn next_item(&self, kind: &str) -> ResponseItemId {
match &self.0 {
Allocator::Uuid => ResponseItemId::with_suffix(kind, Uuid::now_v7()),
}
}
pub fn next_window(&self) -> ContextWindowId {
match &self.0 {
Allocator::Uuid => ContextWindowId::new_uuid_v7(),
}
}
/// Assigns IDs to newly rendered window prefix items before ordinary item allocation.
pub fn assign_prefix_ids<'a>(&self, _items: impl Iterator<Item = &'a mut ResponseItem>) {
match &self.0 {
Allocator::Uuid => {}
}
}
/// Derives IDs for the prompt-only items rebuilt on each request.
pub fn request_ids(&self, thread: ThreadId) -> RequestIds {
RequestIds(match &self.0 {
Allocator::Uuid => RequestAllocator::Uuid(Uuid::new_v5(
&Uuid::NAMESPACE_OID,
thread.to_string().as_bytes(),
)),
})
}
}
/// Allocates IDs for the prompt-only items in one request.
pub struct RequestIds(RequestAllocator);
enum RequestAllocator {
Uuid(Uuid),
}
impl RequestIds {
pub fn id_by_content(&self, kind: &str, content: &[u8]) -> ResponseItemId {
match &self.0 {
RequestAllocator::Uuid(namespace) => {
ResponseItemId::with_suffix(kind, Uuid::new_v5(namespace, content))
}
}
}
}