Skip to content

Commit edbde7f

Browse files
committed
fix(app-server): announce auto-attached subagents
1 parent 08ba14b commit edbde7f

2 files changed

Lines changed: 90 additions & 2 deletions

File tree

‎codex-rs/app-server/src/request_processors/thread_processor.rs‎

Lines changed: 25 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -374,6 +374,22 @@ fn validate_dynamic_tools(tools: &[DynamicToolSpec]) -> Result<(), String> {
374374
Ok(())
375375
}
376376

377+
async fn send_auto_attached_thread_started(
378+
outgoing: &Arc<OutgoingMessageSender>,
379+
connection_id: ConnectionId,
380+
thread: &Thread,
381+
) {
382+
if thread.parent_thread_id.is_none() {
383+
return;
384+
}
385+
outgoing
386+
.send_server_notification_to_connections(
387+
&[connection_id],
388+
ServerNotification::ThreadStarted(thread_started_notification(thread.clone())),
389+
)
390+
.await;
391+
}
392+
377393
#[derive(Clone)]
378394
pub(crate) struct ThreadRequestProcessor {
379395
pub(super) auth_manager: Arc<AuthManager>,
@@ -2636,15 +2652,19 @@ impl ThreadRequestProcessor {
26362652
connection_ids: Vec<ConnectionId>,
26372653
) {
26382654
let mut raw_events_enabled = false;
2655+
let mut loaded_thread = None;
26392656
if let Ok(thread) = self.thread_manager.get_thread(thread_id).await {
26402657
let config_snapshot = thread.config_snapshot().await;
2641-
let loaded_thread = build_thread_from_snapshot(
2658+
let thread_snapshot = build_thread_from_snapshot(
26422659
thread_id,
26432660
thread.session_configured().session_id.to_string(),
26442661
&config_snapshot,
26452662
thread.rollout_path(),
26462663
);
2647-
self.thread_watch_manager.upsert_thread(loaded_thread).await;
2664+
self.thread_watch_manager
2665+
.upsert_thread(thread_snapshot.clone())
2666+
.await;
2667+
loaded_thread = Some(thread_snapshot);
26482668
if let Some(parent_thread_id) = config_snapshot.parent_thread_id {
26492669
raw_events_enabled = self
26502670
.thread_state_manager
@@ -2657,6 +2677,9 @@ impl ThreadRequestProcessor {
26572677
}
26582678

26592679
for connection_id in connection_ids {
2680+
if let Some(thread) = loaded_thread.as_ref() {
2681+
send_auto_attached_thread_started(&self.outgoing, connection_id, thread).await;
2682+
}
26602683
log_listener_attach_result(
26612684
self.ensure_conversation_listener(thread_id, connection_id, raw_events_enabled)
26622685
.await,

‎codex-rs/app-server/src/request_processors/thread_processor_tests.rs‎

Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1202,6 +1202,71 @@ mod thread_processor_behavior_tests {
12021202
Ok(())
12031203
}
12041204

1205+
#[tokio::test]
1206+
async fn auto_attached_subagent_is_announced_to_connection() -> Result<()> {
1207+
let connection_id = ConnectionId(7);
1208+
let thread_id = ThreadId::from_string("bfd12a78-5900-467b-9bc5-d3d35df08191")?;
1209+
let parent_thread_id = ThreadId::from_string("ad7f0408-99b8-4f6e-a46f-bd0eec433370")?;
1210+
let (outgoing_tx, mut outgoing_rx) = tokio::sync::mpsc::channel(8);
1211+
let outgoing = Arc::new(OutgoingMessageSender::new(
1212+
outgoing_tx,
1213+
codex_analytics::AnalyticsEventsClient::disabled(),
1214+
));
1215+
let thread = Thread {
1216+
id: thread_id.to_string(),
1217+
extra: None,
1218+
session_id: parent_thread_id.to_string(),
1219+
forked_from_id: None,
1220+
parent_thread_id: Some(parent_thread_id.to_string()),
1221+
preview: String::new(),
1222+
ephemeral: false,
1223+
history_mode: ThreadHistoryMode::Legacy.into(),
1224+
model_provider: "openai".to_string(),
1225+
created_at: 0,
1226+
updated_at: 0,
1227+
recency_at: None,
1228+
status: ThreadStatus::Idle,
1229+
path: None,
1230+
cwd: AbsolutePathBuf::from_absolute_path("/")?,
1231+
cli_version: "test".to_string(),
1232+
source: SessionSource::SubAgent(SubAgentSource::ThreadSpawn {
1233+
parent_thread_id,
1234+
depth: 1,
1235+
agent_path: None,
1236+
agent_nickname: None,
1237+
agent_role: None,
1238+
})
1239+
.into(),
1240+
thread_source: Some(codex_app_server_protocol::ThreadSource::Subagent),
1241+
agent_nickname: None,
1242+
agent_role: None,
1243+
git_info: None,
1244+
name: None,
1245+
turns: Vec::new(),
1246+
};
1247+
1248+
send_auto_attached_thread_started(&outgoing, connection_id, &thread).await;
1249+
1250+
let envelope = outgoing_rx
1251+
.recv()
1252+
.await
1253+
.expect("thread/started notification");
1254+
let OutgoingEnvelope::ToConnection {
1255+
connection_id: actual_connection_id,
1256+
message:
1257+
OutgoingMessage::AppServerNotification(ServerNotification::ThreadStarted(notification)),
1258+
..
1259+
} = envelope
1260+
else {
1261+
panic!("expected connection-scoped thread/started notification");
1262+
};
1263+
assert_eq!(
1264+
(actual_connection_id, notification.thread),
1265+
(connection_id, thread)
1266+
);
1267+
Ok(())
1268+
}
1269+
12051270
#[test]
12061271
fn summary_from_state_db_metadata_preserves_agent_nickname() -> Result<()> {
12071272
let conversation_id = ThreadId::from_string("bfd12a78-5900-467b-9bc5-d3d35df08191")?;

0 commit comments

Comments
 (0)