This commit is contained in:
jif-oai
2026-01-22 18:56:04 +01:00
parent 4d48d4e0c2
commit a3c07aabd0
23 changed files with 534 additions and 75 deletions
+25 -9
View File
@@ -5,6 +5,8 @@ use crate::error::Result as CodexResult;
use crate::thread_manager::ThreadManagerState;
use codex_protocol::ThreadId;
use codex_protocol::protocol::Op;
use codex_protocol::protocol::SpawnedThreadKind;
use codex_protocol::protocol::ThreadOrigin;
use codex_protocol::user_input::UserInput;
use std::sync::Arc;
use std::sync::Weak;
@@ -39,12 +41,22 @@ impl AgentControl {
&self,
config: crate::config::Config,
prompt: String,
parent_thread_id: ThreadId,
) -> CodexResult<ThreadId> {
let state = self.upgrade()?;
let reservation = self.state.reserve_spawn_slot(config.agent_max_threads)?;
// The same `AgentControl` is sent to spawn the thread.
let new_thread = state.spawn_new_thread(config, self.clone()).await?;
let new_thread = state
.spawn_new_thread(
config,
ThreadOrigin::SpawnedByThread {
parent_thread_id,
kind: SpawnedThreadKind::Other("agent".to_string()),
},
self.clone(),
)
.await?;
reservation.commit(new_thread.thread_id);
// Notify a new thread has been created. This notification will be processed by clients
@@ -268,7 +280,7 @@ mod tests {
let control = AgentControl::default();
let (_home, config) = test_config().await;
let err = control
.spawn_agent(config, "hello".to_string())
.spawn_agent(config, "hello".to_string(), ThreadId::new())
.await
.expect_err("spawn_agent should fail without a manager");
assert_eq!(
@@ -370,7 +382,11 @@ mod tests {
let harness = AgentControlHarness::new().await;
let thread_id = harness
.control
.spawn_agent(harness.config.clone(), "spawned".to_string())
.spawn_agent(
harness.config.clone(),
"spawned".to_string(),
ThreadId::new(),
)
.await
.expect("spawn_agent should succeed");
let _thread = harness
@@ -417,12 +433,12 @@ mod tests {
.expect("start thread");
let first_agent_id = control
.spawn_agent(config.clone(), "hello".to_string())
.spawn_agent(config.clone(), "hello".to_string(), ThreadId::new())
.await
.expect("spawn_agent should succeed");
let err = control
.spawn_agent(config, "hello again".to_string())
.spawn_agent(config, "hello again".to_string(), ThreadId::new())
.await
.expect_err("spawn_agent should respect max threads");
let CodexErr::AgentLimitReached {
@@ -455,7 +471,7 @@ mod tests {
let control = manager.agent_control();
let first_agent_id = control
.spawn_agent(config.clone(), "hello".to_string())
.spawn_agent(config.clone(), "hello".to_string(), ThreadId::new())
.await
.expect("spawn_agent should succeed");
let _ = control
@@ -464,7 +480,7 @@ mod tests {
.expect("shutdown agent");
let second_agent_id = control
.spawn_agent(config.clone(), "hello again".to_string())
.spawn_agent(config.clone(), "hello again".to_string(), ThreadId::new())
.await
.expect("spawn_agent should succeed after shutdown");
let _ = control
@@ -490,12 +506,12 @@ mod tests {
let cloned = control.clone();
let first_agent_id = cloned
.spawn_agent(config.clone(), "hello".to_string())
.spawn_agent(config.clone(), "hello".to_string(), ThreadId::new())
.await
.expect("spawn_agent should succeed");
let err = control
.spawn_agent(config, "hello again".to_string())
.spawn_agent(config, "hello again".to_string(), ThreadId::new())
.await
.expect_err("spawn_agent should respect shared guard");
let CodexErr::AgentLimitReached { max_threads } = err else {
+1 -1
View File
@@ -226,7 +226,7 @@ impl ModelClient {
let mut extra_headers = ApiHeaderMap::new();
if let SessionSource::SubAgent(sub) = &self.state.session_source {
let subagent = if let crate::protocol::SubAgentSource::Other(label) = sub {
let subagent = if let crate::protocol::SpawnedThreadKind::Other(label) = sub {
label.clone()
} else {
serde_json::to_value(sub)
+13 -1
View File
@@ -48,6 +48,7 @@ use codex_protocol::protocol::RawResponseItemEvent;
use codex_protocol::protocol::ReviewRequest;
use codex_protocol::protocol::RolloutItem;
use codex_protocol::protocol::SessionSource;
use codex_protocol::protocol::ThreadOrigin;
use codex_protocol::protocol::TurnAbortReason;
use codex_protocol::protocol::TurnContextItem;
use codex_protocol::protocol::TurnStartedEvent;
@@ -228,6 +229,7 @@ fn maybe_push_chat_wire_api_deprecation(
impl Codex {
/// Spawn a new [`Codex`] and initialize the session.
#[allow(clippy::too_many_arguments)]
pub(crate) async fn spawn(
config: Config,
auth_manager: Arc<AuthManager>,
@@ -235,6 +237,7 @@ impl Codex {
skills_manager: Arc<SkillsManager>,
conversation_history: InitialHistory,
session_source: SessionSource,
thread_origin: ThreadOrigin,
agent_control: AgentControl,
) -> CodexResult<CodexSpawnOk> {
let (tx_sub, rx_sub) = async_channel::bounded(SUBMISSION_CHANNEL_CAPACITY);
@@ -324,6 +327,7 @@ impl Codex {
agent_status_tx.clone(),
conversation_history,
session_source_clone,
thread_origin.clone(),
skills_manager,
agent_control,
)
@@ -589,6 +593,7 @@ impl Session {
agent_status: watch::Sender<AgentStatus>,
initial_history: InitialHistory,
session_source: SessionSource,
thread_origin: ThreadOrigin,
skills_manager: Arc<SkillsManager>,
agent_control: AgentControl,
) -> anyhow::Result<Arc<Self>> {
@@ -604,7 +609,13 @@ impl Session {
));
}
let forked_from_id = initial_history.forked_from_id();
let forked_from_id = match &thread_origin {
ThreadOrigin::Forked { parent_thread_id }
| ThreadOrigin::SpawnedByThread {
parent_thread_id, ..
} => Some(*parent_thread_id),
_ => initial_history.forked_from_id(),
};
let (conversation_id, rollout_params) = match &initial_history {
InitialHistory::New | InitialHistory::Forked(_) => {
@@ -614,6 +625,7 @@ impl Session {
RolloutRecorderParams::new(
conversation_id,
forked_from_id,
thread_origin,
session_source,
BaseInstructions {
text: session_configuration.base_instructions.clone(),
+7 -2
View File
@@ -12,8 +12,9 @@ use codex_protocol::protocol::ExecApprovalRequestEvent;
use codex_protocol::protocol::Op;
use codex_protocol::protocol::RequestUserInputEvent;
use codex_protocol::protocol::SessionSource;
use codex_protocol::protocol::SubAgentSource;
use codex_protocol::protocol::SpawnedThreadKind;
use codex_protocol::protocol::Submission;
use codex_protocol::protocol::ThreadOrigin;
use codex_protocol::request_user_input::RequestUserInputArgs;
use codex_protocol::request_user_input::RequestUserInputResponse;
use codex_protocol::user_input::UserInput;
@@ -55,7 +56,11 @@ pub(crate) async fn run_codex_thread_interactive(
models_manager,
Arc::clone(&parent_session.services.skills_manager),
initial_history.unwrap_or(InitialHistory::New),
SessionSource::SubAgent(SubAgentSource::Review),
SessionSource::SubAgent(SpawnedThreadKind::Review),
ThreadOrigin::SpawnedByThread {
parent_thread_id: parent_session.conversation_id,
kind: SpawnedThreadKind::Review,
},
parent_session.services.agent_control.clone(),
)
.await?;
+6
View File
@@ -36,6 +36,7 @@ use codex_protocol::protocol::RolloutLine;
use codex_protocol::protocol::SessionMeta;
use codex_protocol::protocol::SessionMetaLine;
use codex_protocol::protocol::SessionSource;
use codex_protocol::protocol::ThreadOrigin;
/// Records all [`ResponseItem`]s for a session and flushes them to disk after
/// every update.
@@ -57,6 +58,7 @@ pub enum RolloutRecorderParams {
Create {
conversation_id: ThreadId,
forked_from_id: Option<ThreadId>,
thread_origin: ThreadOrigin,
source: SessionSource,
base_instructions: BaseInstructions,
},
@@ -80,12 +82,14 @@ impl RolloutRecorderParams {
pub fn new(
conversation_id: ThreadId,
forked_from_id: Option<ThreadId>,
thread_origin: ThreadOrigin,
source: SessionSource,
base_instructions: BaseInstructions,
) -> Self {
Self::Create {
conversation_id,
forked_from_id,
thread_origin,
source,
base_instructions,
}
@@ -161,6 +165,7 @@ impl RolloutRecorder {
RolloutRecorderParams::Create {
conversation_id,
forked_from_id,
thread_origin,
source,
base_instructions,
} => {
@@ -185,6 +190,7 @@ impl RolloutRecorder {
Some(SessionMeta {
id: session_id,
forked_from_id,
thread_origin: Some(thread_origin),
timestamp,
cwd: config.cwd.clone(),
originator: originator().value,
+1
View File
@@ -772,6 +772,7 @@ async fn test_updated_at_uses_file_mtime() -> Result<()> {
meta: SessionMeta {
id: conversation_id,
forked_from_id: None,
thread_origin: None,
timestamp: ts.to_string(),
cwd: ".".into(),
originator: "test_originator".into(),
+15 -1
View File
@@ -26,6 +26,7 @@ use codex_protocol::protocol::McpServerRefreshConfig;
use codex_protocol::protocol::Op;
use codex_protocol::protocol::RolloutItem;
use codex_protocol::protocol::SessionSource;
use codex_protocol::protocol::ThreadOrigin;
use std::collections::HashMap;
use std::path::PathBuf;
use std::sync::Arc;
@@ -201,6 +202,7 @@ impl ThreadManager {
config,
InitialHistory::New,
Arc::clone(&self.state.auth_manager),
ThreadOrigin::UserRequested,
self.agent_control(),
)
.await
@@ -224,7 +226,13 @@ impl ThreadManager {
auth_manager: Arc<AuthManager>,
) -> CodexResult<NewThread> {
self.state
.spawn_thread(config, initial_history, auth_manager, self.agent_control())
.spawn_thread(
config,
initial_history,
auth_manager,
ThreadOrigin::Unknown,
self.agent_control(),
)
.await
}
@@ -253,6 +261,7 @@ impl ThreadManager {
nth_user_message: usize,
config: Config,
path: PathBuf,
thread_origin: ThreadOrigin,
) -> CodexResult<NewThread> {
let history = RolloutRecorder::get_rollout_history(&path).await?;
let history = truncate_before_nth_user_message(history, nth_user_message);
@@ -261,6 +270,7 @@ impl ThreadManager {
config,
history,
Arc::clone(&self.state.auth_manager),
thread_origin,
self.agent_control(),
)
.await
@@ -312,12 +322,14 @@ impl ThreadManagerState {
pub(crate) async fn spawn_new_thread(
&self,
config: Config,
thread_origin: ThreadOrigin,
agent_control: AgentControl,
) -> CodexResult<NewThread> {
self.spawn_thread(
config,
InitialHistory::New,
Arc::clone(&self.auth_manager),
thread_origin,
agent_control,
)
.await
@@ -329,6 +341,7 @@ impl ThreadManagerState {
config: Config,
initial_history: InitialHistory,
auth_manager: Arc<AuthManager>,
thread_origin: ThreadOrigin,
agent_control: AgentControl,
) -> CodexResult<NewThread> {
let CodexSpawnOk {
@@ -340,6 +353,7 @@ impl ThreadManagerState {
Arc::clone(&self.skills_manager),
initial_history,
self.session_source.clone(),
thread_origin,
agent_control,
)
.await?;
+1 -1
View File
@@ -125,7 +125,7 @@ mod spawn {
let result = session
.services
.agent_control
.spawn_agent(config, prompt.clone())
.spawn_agent(config, prompt.clone(), session.conversation_id)
.await
.map_err(collab_spawn_error);
let (new_thread_id, status) = match &result {