mirror of
https://github.com/openai/codex.git
synced 2026-12-03 00:41:03 +00:00
Allow parent-aware thread ID allocation for local child agents (#65359)
## What changed Add `ThreadManager::with_child_thread_id_generator` so callers can allocate a local child's thread ID using its parent's ID before the spawn edge is saved. Use the regular thread ID generator when there is no parent or no child-specific generator. ## Testing Update the spawn publication tests to check that the callback receives the parent ID and to force a collision by returning that ID, verifying that the existing thread and graph are preserved. GitOrigin-RevId: a074d5872b576e0464cd9c29ee67128767098ee2
This commit is contained in:
@@ -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",
|
||||
|
||||
@@ -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<FaultingGraphStore>) {
|
||||
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
|
||||
|
||||
@@ -153,6 +153,7 @@ static FORCE_TEST_THREAD_MANAGER_BEHAVIOR: AtomicBool = AtomicBool::new(false);
|
||||
type CapturedOps = Vec<(ThreadId, Op)>;
|
||||
type SharedCapturedOps = Arc<std::sync::Mutex<CapturedOps>>;
|
||||
pub(crate) type ThreadIdGenerator = Arc<dyn Fn() -> ThreadId + Send + Sync>;
|
||||
type ChildThreadIdGenerator = Box<dyn Fn(ThreadId) -> ThreadId + Send + Sync>;
|
||||
type AgentControlFactory =
|
||||
Arc<dyn Fn(ThreadId) -> BoxFuture<'static, CodexResult<Arc<dyn AgentControl>>> + Send + Sync>;
|
||||
|
||||
@@ -669,6 +670,7 @@ pub(crate) struct ThreadManagerState {
|
||||
shared_thread_instructions: shared_instructions::SharedThreadInstructionsProviders,
|
||||
thread_created_tx: broadcast::Sender<ThreadId>,
|
||||
thread_id_generator: ThreadIdGenerator,
|
||||
pub(crate) child_thread_id_generator: Option<ChildThreadIdGenerator>,
|
||||
agent_control_factory: Option<AgentControlFactory>,
|
||||
auth_manager: Arc<AuthManager>,
|
||||
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,
|
||||
|
||||
Reference in New Issue
Block a user