Allow internal sessions to fork from selected history (#43495)

## What changed

Add `ThreadManager::fork_internal_session` to start an internal session from caller-selected committed history without reading an in-flight parent turn or appending an interruption marker. Preserve parent authentication and budget sharing, and propagate fork lineage. Keep `spawn_internal_session` starting with fresh history.

## Testing

Add a regression test verifying that the fork contains only the selected history, retains its parent association and authentication manager, and stays outside the public thread registry.

GitOrigin-RevId: df4260ed3adfa0abe7a07c2b6d8b8c6ef6ba3d16
This commit is contained in:
jif authored and copyberry committed 2026-09-07 15:27:52 +00:00
1 parent d0a8dcd157
commit 9f70e348e0
2 files changed
+102 -1

No files matched your search

+31 -1
View File
@@ -958,9 +958,37 @@ impl ThreadManager {
/// Starts a fresh internal session associated with an existing parent thread.
pub async fn spawn_internal_session(
&self,
parent_thread_id: ThreadId,
options: StartThreadOptions,
) -> CodexResult<NewThread> {
self.spawn_internal_session_with_history(parent_thread_id, options, InitialHistory::New)
.await
}
/// Starts an internal session from an explicitly selected, committed history.
///
/// The caller selects the history; this never reads an in-flight parent turn or
/// appends an interruption marker. Authentication and budget still come from the parent.
pub async fn fork_internal_session(
&self,
parent_thread_id: ThreadId,
options: StartThreadOptions,
history: Vec<RolloutItem>,
) -> CodexResult<NewThread> {
self.spawn_internal_session_with_history(
parent_thread_id,
options,
InitialHistory::Forked(history),
)
.await
}
async fn spawn_internal_session_with_history(
&self,
parent_thread_id: ThreadId,
mut options: StartThreadOptions,
history: InitialHistory,
) -> CodexResult<NewThread> {
if !matches!(options.session_source, Some(SessionSource::Internal(_))) {
return Err(CodexErr::InvalidRequest(
@@ -968,13 +996,15 @@ impl ThreadManager {
));
}
let parent = self.get_thread(parent_thread_id).await?;
options.initial_history = InitialHistory::New;
let forked_from_thread_id = history.forked_from_id();
options.initial_history = history;
let mut request = ThreadSpawnRequest::new(
options,
Arc::clone(&parent.session.services.auth_manager),
parent.session.services.agent_control.clone(),
);
request.parent_thread_id = Some(parent_thread_id);
request.forked_from_thread_id = forked_from_thread_id;
Box::pin(self.state.spawn_thread(request)).await
}