diff --git a/codex-rs/core/src/agent/control/spawn.rs b/codex-rs/core/src/agent/control/spawn.rs index e84cd3e151..8d1c15c040 100644 --- a/codex-rs/core/src/agent/control/spawn.rs +++ b/codex-rs/core/src/agent/control/spawn.rs @@ -905,7 +905,13 @@ impl LocalAgentControl { }; // Fork startup can persist copied history before returning a live child. // Publish its deletion relationship before any child storage is created. - let child_thread_id = self.runtime.generate_thread_id(); + let child_thread_id = if let (Some(parent), Some(generate)) = + (parent_thread_id, &state.child_thread_id_generator) + { + generate(parent) + } else { + self.runtime.generate_thread_id() + }; if state.threads.read().await.contains_key(&child_thread_id) { return Err(CodexErr::InvalidRequest(format!( "thread {child_thread_id} is already running", diff --git a/codex-rs/core/src/agent/control/spawn_publication_tests.rs b/codex-rs/core/src/agent/control/spawn_publication_tests.rs index c1a1f27a5b..9b75feade9 100644 --- a/codex-rs/core/src/agent/control/spawn_publication_tests.rs +++ b/codex-rs/core/src/agent/control/spawn_publication_tests.rs @@ -114,7 +114,7 @@ impl AgentGraphStore for FaultingGraphStore { async fn publication_harness( failure: SpawnEdgeFailure, history: PublicationHistory, - generate_thread_id: impl Fn() -> ThreadId + Send + Sync + 'static, + generate_child_id: impl Fn(ThreadId) -> ThreadId + Send + Sync + 'static, ) -> (AgentControlHarness, Arc) { let mut harness = AgentControlHarness::new().await; let graph = Arc::new(FaultingGraphStore { @@ -152,7 +152,7 @@ async fn publication_harness( /*attestation_provider*/ None, /*external_time_provider*/ None, ) - .with_thread_id_generator(generate_thread_id); + .with_child_thread_id_generator(generate_child_id); harness.control = harness.manager.agent_control(); (harness, graph) } @@ -226,9 +226,12 @@ async fn cancelled_spawn_edge_publication_cleanup( failure: SpawnEdgeFailure, committed_children: usize, ) { - let (harness, graph) = - publication_harness(failure, PublicationHistory::Local, ThreadId::new).await; let parent = ThreadId::new(); + let (harness, graph) = publication_harness(failure, PublicationHistory::Local, move |id| { + assert_eq!(id, parent); + ThreadId::new() + }) + .await; let mut spawn = Box::pin(spawn_publication_child( &harness, parent, @@ -278,8 +281,10 @@ impl SpanExporter for SpawnSpanExporter { #[test_case::test_case(PublicationHistory::InMemory; "span_timing")] #[tokio::test(flavor = "multi_thread", worker_threads = 1)] async fn failed_spawn_edge_publication_cleanup(history: PublicationHistory) { - let (harness, graph) = - publication_harness(SpawnEdgeFailure::ErrorAfterCommit, history, ThreadId::new).await; + let (harness, graph) = publication_harness(SpawnEdgeFailure::ErrorAfterCommit, history, |_| { + ThreadId::new() + }) + .await; let (parent, thread) = match history { PublicationHistory::Local => harness.start_paginated_thread().await, PublicationHistory::InMemory => harness.start_thread().await, @@ -364,7 +369,7 @@ async fn failed_spawn_without_publication_does_not_close_graph(skipped: SkippedP let (mut harness, graph) = publication_harness( SpawnEdgeFailure::ErrorAfterCommit, PublicationHistory::Local, - ThreadId::new, + |_| ThreadId::new(), ) .await; let parent = ThreadId::new(); @@ -421,15 +426,13 @@ async fn failed_spawn_without_publication_does_not_close_graph(skipped: SkippedP #[tokio::test] async fn repeated_child_id_preserves_existing_thread_and_graph() { - let repeated_id = ThreadId::new(); let (harness, graph) = publication_harness( SpawnEdgeFailure::ErrorAfterCommit, PublicationHistory::Local, - move || repeated_id, + |parent| parent, // Reuse the parent's ID to force a collision. ) .await; let (parent, existing_thread) = harness.start_thread().await; - assert_eq!(parent, repeated_id); let children_before = graph .list_thread_spawn_children(parent, /*status_filter*/ None) .await diff --git a/codex-rs/core/src/thread_manager.rs b/codex-rs/core/src/thread_manager.rs index 363cf73e41..3e76f15ae9 100644 --- a/codex-rs/core/src/thread_manager.rs +++ b/codex-rs/core/src/thread_manager.rs @@ -153,6 +153,7 @@ static FORCE_TEST_THREAD_MANAGER_BEHAVIOR: AtomicBool = AtomicBool::new(false); type CapturedOps = Vec<(ThreadId, Op)>; type SharedCapturedOps = Arc>; pub(crate) type ThreadIdGenerator = Arc ThreadId + Send + Sync>; +type ChildThreadIdGenerator = Box ThreadId + Send + Sync>; type AgentControlFactory = Arc BoxFuture<'static, CodexResult>> + Send + Sync>; @@ -669,6 +670,7 @@ pub(crate) struct ThreadManagerState { shared_thread_instructions: shared_instructions::SharedThreadInstructionsProviders, thread_created_tx: broadcast::Sender, thread_id_generator: ThreadIdGenerator, + pub(crate) child_thread_id_generator: Option, agent_control_factory: Option, auth_manager: Arc, runtime_auth: crate::runtime_auth::RuntimeAuthSlot, @@ -774,6 +776,7 @@ impl ThreadManager { shared_thread_instructions: Default::default(), thread_created_tx, thread_id_generator: default_thread_id_generator(), + child_thread_id_generator: None, agent_control_factory: None, model_catalog, git_root_discovery: Arc::default(), @@ -802,7 +805,7 @@ impl ThreadManager { } } - /// Generate every new thread identifier with the caller-provided factory. + /// Generate new thread IDs unless a local child has its own generator. pub fn with_thread_id_generator( mut self, generator: impl Fn() -> ThreadId + Send + Sync + 'static, @@ -814,6 +817,20 @@ impl ThreadManager { self } + /// Use the parent's ID to allocate a local child ID before saving the spawn edge. + /// Install the generator before sharing the manager. + /// Without a parent or this callback, Core uses the regular thread ID generator. + pub fn with_child_thread_id_generator( + mut self, + generator: impl Fn(ThreadId) -> ThreadId + Send + Sync + 'static, + ) -> Self { + let Some(state) = Arc::get_mut(&mut self.state) else { + unreachable!("child thread ID generator must be set before thread manager is shared"); + }; + state.child_thread_id_generator = Some(Box::new(generator)); + self + } + /// Selects the host-owned agent controller for every new or cold-resumed thread. /// /// The factory receives the thread's final ID and must return the controller for @@ -997,6 +1014,7 @@ impl ThreadManager { shared_thread_instructions: Default::default(), thread_created_tx, thread_id_generator: default_thread_id_generator(), + child_thread_id_generator: None, agent_control_factory: None, model_catalog: model_catalog::ThreadModelCatalog::new( provider,