From 30607648d995f17baa93f23b6bc385f52158bbd4 Mon Sep 17 00:00:00 2001 From: Ruslan Nigmatullin Date: Thu, 19 Nov 2026 23:53:30 +0000 Subject: [PATCH] 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 --- codex-rs/core/src/client.rs | 20 ++++---------- codex-rs/core/src/client_tests.rs | 15 +++++------ codex-rs/core/src/session/mod.rs | 26 ++++++++++++++----- codex-rs/thread-context-ids/Cargo.toml | 2 +- codex-rs/thread-context-ids/src/lib.rs | 36 ++++++++++++++++++++++++++ 5 files changed, 69 insertions(+), 30 deletions(-) diff --git a/codex-rs/core/src/client.rs b/codex-rs/core/src/client.rs index 92a8082575..c233ebdd8c 100644 --- a/codex-rs/core/src/client.rs +++ b/codex-rs/core/src/client.rs @@ -122,7 +122,6 @@ use tokio_util::sync::CancellationToken; use tracing::instrument; use tracing::trace; use tracing::warn; -use uuid::Uuid; use crate::attestation::AttestationContext; use crate::attestation::AttestationProvider; @@ -1266,21 +1265,13 @@ impl ModelClient { }); } let is_openai = self.state.provider.info().is_openai(); - // These prompt-only items are rebuilt on every request. Hash their visible payloads - // within the thread so retries and resumed sessions preserve their identity. - let prefix_namespace = Uuid::new_v5( - &Uuid::NAMESPACE_OID, - self.state.thread_id.to_string().as_bytes(), - ); + let request_ids = self.state.context_ids.request_ids(self.state.thread_id); let mut prefix = Vec::new(); let tools = if model_info.use_responses_lite { if !prompt.tools.is_empty() { let tools = create_tools_json_for_responses_lite(&prompt.tools)?; prefix.push(ResponseItem::AdditionalTools { - id: Some(ResponseItemId::with_suffix( - "at", - Uuid::new_v5(&prefix_namespace, &serde_json::to_vec(&tools)?), - )), + id: Some(request_ids.id_by_content("at", &serde_json::to_vec(&tools)?)), role: "developer".to_string(), tools: Arc::from(tools), }); @@ -1293,10 +1284,9 @@ impl ModelClient { let mut instructions = ContextualUserFragment::into(BaseInstructionsFragment( prompt.base_instructions.text.clone(), )); - instructions.set_id(Some(ResponseItemId::with_suffix( - "msg", - Uuid::new_v5(&prefix_namespace, prompt.base_instructions.text.as_bytes()), - ))); + instructions.set_id(Some( + request_ids.id_by_content("msg", prompt.base_instructions.text.as_bytes()), + )); prefix.push(instructions); } input.splice(0..0, prefix); diff --git a/codex-rs/core/src/client_tests.rs b/codex-rs/core/src/client_tests.rs index cf1f02e93f..85618ca893 100644 --- a/codex-rs/core/src/client_tests.rs +++ b/codex-rs/core/src/client_tests.rs @@ -77,6 +77,7 @@ use codex_runtime_agent_instance::AgentHostAuthorization; use codex_runtime_agent_instance::RuntimeAuthManager; use codex_runtime_agent_instance::RuntimeRegistrationMetadata; use codex_runtime_agent_instance::RuntimeRole; +use codex_thread_context_ids::ThreadContextIds; use codex_thread_context_ids_test_support::thread_context_ids; use codex_tools::JsonSchema; use codex_tools::ResponsesApiTool; @@ -114,19 +115,20 @@ use wiremock::matchers::path; const TEST_INSTALLATION_ID: &str = "11111111-1111-4111-8111-111111111111"; fn test_model_client(session_source: SessionSource) -> ModelClient { - test_model_client_with_thread_id(ThreadId::new(), session_source) + test_model_client_with_ids(ThreadId::new(), session_source, thread_context_ids()) } -fn test_model_client_with_thread_id( +fn test_model_client_with_ids( thread_id: ThreadId, session_source: SessionSource, + context_ids: ThreadContextIds, ) -> ModelClient { let provider = create_oss_provider_with_base_url("https://example.com/v1", WireApi::Responses); ModelClient::new( codex_model_provider::create_model_provider(provider, /*auth_manager*/ None), AgentIdentityAuthPolicy::JwtOnly, thread_id, - thread_context_ids(), + context_ids, session_source, "test_originator".to_string(), /*model_verbosity*/ None, @@ -1081,7 +1083,7 @@ async fn responses_http_preserves_raw_tool_metadata_for_openai_custom_endpoint() #[test_case::test_case(true; "responses_lite")] fn prefix_ids_track_thread_and_payload(responses_lite: bool) -> anyhow::Result<()> { let thread_id = ThreadId::new(); - let client = test_model_client_with_thread_id(thread_id, SessionSource::Cli); + let client = test_model_client_with_ids(thread_id, SessionSource::Cli, thread_context_ids()); let mut model = test_model_info(); model.use_responses_lite = responses_lite; let instructions_index = usize::from(responses_lite); @@ -1147,10 +1149,7 @@ fn prefix_ids_track_thread_and_payload(responses_lite: bool) -> anyhow::Result<( ); } - let independent = build( - &test_model_client_with_thread_id(ThreadId::new(), SessionSource::Cli), - &prompt, - )?; + let independent = build(&test_model_client(SessionSource::Cli), &prompt)?; assert_ne!( independent.input[instructions_index].id(), changed_tools.input[instructions_index].id() diff --git a/codex-rs/core/src/session/mod.rs b/codex-rs/core/src/session/mod.rs index b3cff9a9e8..a3ab7c6982 100644 --- a/codex-rs/core/src/session/mod.rs +++ b/codex-rs/core/src/session/mod.rs @@ -5024,6 +5024,17 @@ impl Session { metadata, started_item, } = replacement; + // History assigns sequences on append and on legacy replay. Only newly + // rendered context in this replacement should receive a prefix ID. + let fresh = items.iter_mut().filter(|entry| { + entry + .metadata + .as_ref() + .and_then(|m| m.history_sequence) + .is_none() + }); + self.context_ids + .assign_prefix_ids(fresh.map(|entry| &mut entry.item)); for envelope in &mut items { self.assign_missing_response_item_id(&mut envelope.item); } @@ -5708,12 +5719,15 @@ impl Session { if self.reference_context_item().await.is_some() { return Ok(None); } - let items = crate::context_manager::updates::merge_world_state_updates(updates); - self.state - .lock() - .await - .history - .set_world_state_baseline(snapshot.clone()); + let mut items = crate::context_manager::updates::merge_world_state_updates(updates); + { + let mut state = self.state.lock().await; + // Recovery may need to append full instructions after a surviving prefix. + if state.history.annotated_items().is_empty() { + self.context_ids.assign_prefix_ids(items.iter_mut()); + } + state.history.set_world_state_baseline(snapshot.clone()); + } self.publish_step_context( step_context, items, diff --git a/codex-rs/thread-context-ids/Cargo.toml b/codex-rs/thread-context-ids/Cargo.toml index 115682e3ed..246cb19312 100644 --- a/codex-rs/thread-context-ids/Cargo.toml +++ b/codex-rs/thread-context-ids/Cargo.toml @@ -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 } diff --git a/codex-rs/thread-context-ids/src/lib.rs b/codex-rs/thread-context-ids/src/lib.rs index ff9bc76003..967442a8c8 100644 --- a/codex-rs/thread-context-ids/src/lib.rs +++ b/codex-rs/thread-context-ids/src/lib.rs @@ -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) { + 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)) + } + } + } }