From 34c7cbb454b359ce7dd9f30f728de60618322755 Mon Sep 17 00:00:00 2001 From: rhan-oai Date: Tue, 17 Nov 2026 01:46:32 +0000 Subject: [PATCH] Allow host-started forks to inherit the root session ID (#61945) ## Why Host-started forks rejected `InheritedSessionId`, preventing hosts from assigning the root session ID to forked children as they already could for fresh children. ## What changed Accept `InheritedSessionId` for new or forked threads started directly by the host. Resumed threads continue to use persisted session IDs, and native children continue to use their controller's session ID. ## Testing Extend hosted-child tests to cover prepared forks, verify that inherited session IDs persist across resume while thread IDs remain distinct, and check that explicit session inheritance is rejected for resumed threads and native children. GitOrigin-RevId: 90fd24a25e4e927c48395dfae7186bb7fd0e537e --- codex-rs/core/src/agent/host/types.rs | 2 +- codex-rs/core/src/thread_manager.rs | 8 +- .../core/tests/suite/agent_control_tests.rs | 77 ++++++++++++++++--- 3 files changed, 72 insertions(+), 15 deletions(-) diff --git a/codex-rs/core/src/agent/host/types.rs b/codex-rs/core/src/agent/host/types.rs index 6c1301d9f2..68c8795ab2 100644 --- a/codex-rs/core/src/agent/host/types.rs +++ b/codex-rs/core/src/agent/host/types.rs @@ -32,7 +32,7 @@ use std::sync::Arc; #[derive(Clone, Copy, Debug)] pub struct RestrictRootCapabilities; -/// The root's session ID, supplied by a trusted host when starting a fresh child. +/// The root's session ID, supplied by a trusted host when starting a new or forked child. /// /// Insert this into the thread's startup [`codex_extension_api::ExtensionDataInit`]. /// The child keeps its own distinct thread ID. Resumed threads take their session diff --git a/codex-rs/core/src/thread_manager.rs b/codex-rs/core/src/thread_manager.rs index a6e32a22e9..3d40069a1e 100644 --- a/codex-rs/core/src/thread_manager.rs +++ b/codex-rs/core/src/thread_manager.rs @@ -2611,11 +2611,13 @@ impl ThreadManagerState { let inherited_environments = captured_environments.or(inherited_environments); let session_source = session_source.unwrap_or_else(|| self.session_source.clone()); if thread_extension_init.get::().is_some() - && (!matches!(&initial_history, InitialHistory::New) - || session_source.is_non_root_agent()) + && (!matches!( + &initial_history, + InitialHistory::New | InitialHistory::Forked(_) + ) || session_source.is_non_root_agent()) { return Err(CodexErr::InvalidRequest( - "an inherited agent session ID requires a fresh thread started directly by the host" + "an inherited agent session ID requires a new or forked thread started directly by the host" .to_string(), )); } diff --git a/codex-rs/core/tests/suite/agent_control_tests.rs b/codex-rs/core/tests/suite/agent_control_tests.rs index 078a13f9bc..36ae27736d 100644 --- a/codex-rs/core/tests/suite/agent_control_tests.rs +++ b/codex-rs/core/tests/suite/agent_control_tests.rs @@ -60,7 +60,9 @@ use codex_protocol::protocol::ThreadSource; use codex_protocol::protocol::TokenUsage; use codex_protocol::user_input::UserInput; use codex_rollout_trace::ThreadTraceContext; +use codex_thread_store::ForkBoundary; use codex_thread_store::LoadThreadHistoryParams; +use codex_thread_store::PrepareForkParams; use core_test_support::ThreadIdle; use core_test_support::responses; use core_test_support::skip_if_no_network; @@ -593,15 +595,25 @@ async fn host_factory_follows_thread_lifecycle() -> anyhow::Result<()> { Ok(()) } -#[test_case(ThreadHistoryMode::Legacy; "legacy")] -#[test_case(ThreadHistoryMode::Paginated; "paginated")] +enum HostedChildHistory { + Fresh, + PreparedFork, +} + +#[test_case(ThreadHistoryMode::Legacy, HostedChildHistory::Fresh; "fresh_legacy")] +#[test_case(ThreadHistoryMode::Paginated, HostedChildHistory::Fresh; "fresh_paginated")] +#[test_case(ThreadHistoryMode::Paginated, HostedChildHistory::PreparedFork; "prepared_fork")] #[tokio::test(flavor = "multi_thread", worker_threads = 2)] -async fn fresh_hosted_child_persists_inherited_session_id( +async fn hosted_child_persists_inherited_session_id( history_mode: ThreadHistoryMode, + child_history: HostedChildHistory, ) -> anyhow::Result<()> { skip_if_no_network!(Ok(())); let server = responses::start_mock_server().await; - let root = test_codex().build_with_auto_env(&server).await?; + let root = test_codex() + .with_history_mode(history_mode) + .build_with_auto_env(&server) + .await?; let root_session_id = root.session_configured.session_id; let manager = &root.thread_manager; let options = || StartThreadOptions { @@ -610,11 +622,39 @@ async fn fresh_hosted_child_persists_inherited_session_id( environments: Some(vec![root.executor_environment().request()]), ..StartThreadOptions::new(root.config.clone()) }; + let mut native_options = options(); + native_options.session_source = Some(SessionSource::SubAgent(SubAgentSource::Review)); + native_options + .thread_extension_init + .insert(InheritedSessionId(root_session_id)); + let Err(error) = manager.start_thread(native_options).await else { + anyhow::bail!("native children must use their controller's session ID"); + }; + assert!(matches!( + error.details(), + CodexErrorDetails::InvalidRequest(_) + )); let mut child_options = options(); child_options .thread_extension_init .insert(InheritedSessionId(root_session_id)); - let child = manager.start_thread(child_options).await?; + let child = match child_history { + HostedChildHistory::Fresh => manager.start_thread(child_options).await?, + HostedChildHistory::PreparedFork => { + root.codex.ensure_rollout_materialized().await; + root.codex.flush_rollout().await?; + let prepared = root + .thread_store + .prepare_fork(PrepareForkParams { + thread_id: root.session_configured.thread_id, + boundary: ForkBoundary::Latest, + }) + .await?; + manager + .fork_prepared_thread(child_options, prepared) + .await? + } + }; assert_ne!(SessionId::from(child.thread_id), root_session_id); assert_eq!( ( @@ -640,14 +680,29 @@ async fn fresh_hosted_child_persists_inherited_session_id( (child.thread_id, root_session_id, history_mode) ); child.thread.shutdown_and_wait().await?; + let resumed_history = InitialHistory::Resumed(ResumedHistory { + history_revision: history.revision, + conversation_id: child.thread_id, + history: Arc::new(history.items), + rollout_path: child.thread.rollout_path(), + }); + let mut invalid_resume = StartThreadOptions { + initial_history: resumed_history.clone(), + ..options() + }; + invalid_resume + .thread_extension_init + .insert(InheritedSessionId(root_session_id)); + let Err(error) = manager.start_thread(invalid_resume).await else { + anyhow::bail!("resuming must use the persisted session ID"); + }; + assert!(matches!( + error.details(), + CodexErrorDetails::InvalidRequest(_) + )); let resumed = manager .start_thread(StartThreadOptions { - initial_history: InitialHistory::Resumed(ResumedHistory { - history_revision: history.revision, - conversation_id: child.thread_id, - history: Arc::new(history.items), - rollout_path: child.thread.rollout_path(), - }), + initial_history: resumed_history, ..options() }) .await?;