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
This commit is contained in:
Ruslan Nigmatullin
2026-11-20 01:20:38 +00:00
committed by copyberry
parent 583ec348cf
commit 30607648d9
5 changed files with 69 additions and 30 deletions
+1 -1
View File
@@ -13,7 +13,7 @@ workspace = true
[dependencies]
codex-protocol = { workspace = true }
uuid = { workspace = true, features = ["v7"] }
uuid = { workspace = true, features = ["v5", "v7"] }
[dev-dependencies]
pretty_assertions = { workspace = true }
+36
View File
@@ -2,6 +2,8 @@
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.
@@ -31,4 +33,38 @@ impl ThreadContextIds {
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))
}
}
}
}