@@ -277,7 +277,9 @@ pub(super) async fn submission_loop(
277277 if !seen_batches.insert(trimmed.to_string()) {
278278 continue;
279279 }
280- cancelled += manager.cancel_batch(trimmed).await;
280+ cancelled += manager
281+ .cancel_batch_for_session(trimmed, sess_arc.session_uuid())
282+ .await;
281283 }
282284
283285 for agent_id in agent_ids {
@@ -288,7 +290,10 @@ pub(super) async fn submission_loop(
288290 if !seen_agents.insert(trimmed.to_string()) {
289291 continue;
290292 }
291- if manager.cancel_agent(trimmed).await {
293+ if manager
294+ .cancel_agent_for_session(trimmed, sess_arc.session_uuid())
295+ .await
296+ {
292297 cancelled += 1;
293298 }
294299 }
@@ -8911,7 +8916,7 @@ async fn handle_check_agent_status(
89118916 Ok(params) => {
89128917 let manager = AGENT_MANAGER.read().await;
89138918
8914- if let Some(agent) = manager.get_agent (¶ms.agent_id) {
8919+ if let Some(agent) = manager.get_agent_for_session (¶ms.agent_id, sess.session_uuid() ) {
89158920 match agent.batch_id.as_deref() {
89168921 Some(batch) if batch == params.batch_id => {}
89178922 _ => {
@@ -8958,7 +8963,7 @@ async fn handle_check_agent_status(
89588963 };
89598964 // Re-acquire manager to get fresh progress after potential delay
89608965 let manager = AGENT_MANAGER.read().await;
8961- if let Some(agent) = manager.get_agent (¶ms.agent_id) {
8966+ if let Some(agent) = manager.get_agent_for_session (¶ms.agent_id, sess.session_uuid() ) {
89628967 let joined = agent.progress.join("\n");
89638968 match write_agent_file(&dir, "progress.log", &joined) {
89648969 Ok(p) => progress_file = Some(p.display().to_string()),
@@ -9037,7 +9042,7 @@ async fn handle_get_agent_result(
90379042 Ok(params) => {
90389043 let manager = AGENT_MANAGER.read().await;
90399044
9040- if let Some(agent) = manager.get_agent (¶ms.agent_id) {
9045+ if let Some(agent) = manager.get_agent_for_session (¶ms.agent_id, sess.session_uuid() ) {
90419046 match agent.batch_id.as_deref() {
90429047 Some(batch) if batch == params.batch_id => {}
90439048 _ => {
@@ -9171,7 +9176,7 @@ async fn handle_cancel_agent(
91719176 };
91729177 }
91739178 };
9174- if let Some(agent) = manager.get_agent (&agent_id) {
9179+ if let Some(agent) = manager.get_agent_for_session (&agent_id, sess.session_uuid() ) {
91759180 if agent.batch_id.as_deref() != Some(batch_id.as_str()) {
91769181 return ResponseInputItem::FunctionCallOutput {
91779182 call_id: call_id_clone,
@@ -9184,7 +9189,7 @@ async fn handle_cancel_agent(
91849189 };
91859190 }
91869191 }
9187- if manager.cancel_agent (&agent_id).await {
9192+ if manager.cancel_agent_for_session (&agent_id, sess.session_uuid() ).await {
91889193 ResponseInputItem::FunctionCallOutput {
91899194 call_id: call_id_clone,
91909195 output: FunctionCallOutputPayload {
@@ -9200,7 +9205,7 @@ async fn handle_cancel_agent(
92009205 }
92019206 }
92029207 } else if let Some(batch_id) = params.batch_id {
9203- let count = manager.cancel_batch (&batch_id).await;
9208+ let count = manager.cancel_batch_for_session (&batch_id, sess.session_uuid() ).await;
92049209 ResponseInputItem::FunctionCallOutput {
92059210 call_id: call_id_clone,
92069211 output: FunctionCallOutputPayload {
@@ -9273,7 +9278,7 @@ async fn handle_wait_for_agent(
92739278 let manager = AGENT_MANAGER.read().await;
92749279
92759280 if let Some(agent_id) = ¶ms.agent_id {
9276- if let Some(agent) = manager.get_agent (agent_id) {
9281+ if let Some(agent) = manager.get_agent_for_session (agent_id, sess.session_uuid() ) {
92779282 match agent.batch_id.as_deref() {
92789283 Some(batch) if batch == batch_id => {}
92799284 _ => {
@@ -9362,7 +9367,7 @@ async fn handle_wait_for_agent(
93629367 }
93639368 }
93649369 } else {
9365- let agents = manager.list_agents (None, Some(batch_id.clone()), false);
9370+ let agents = manager.list_agents_for_session (None, Some(batch_id.clone()), false, sess.session_uuid() );
93669371
93679372 // Separate terminal vs non-terminal agents
93689373 let completed_agents: Vec<_> = agents
@@ -9678,10 +9683,11 @@ async fn handle_list_agents(
96789683 _ => None,
96799684 });
96809685
9681- let agents = manager.list_agents (
9686+ let agents = manager.list_agents_for_session (
96829687 status_filter,
96839688 Some(batch_id.clone()),
96849689 params.recent_only.unwrap_or(false),
9690+ sess.session_uuid(),
96859691 );
96869692
96879693 // Count running agents for status update
@@ -10719,6 +10725,7 @@ async fn handle_container_exec_with_params(
1071910725 let suppress_event_flag_task = suppress_event_flag.clone();
1072010726 let display_label_task = display_label.clone();
1072110727 let tool_output_max_bytes = sess.tool_output_max_bytes;
10728+ let sess_for_background_completion = sess.self_handle.upgrade();
1072210729 let task_handle = tokio::spawn(async move {
1072310730 // Build stdout stream with tail capture. We cannot stamp via `Session` here,
1072410731 // but deltas will be delivered with neutral ordering which the UI tolerates.
@@ -10809,10 +10816,10 @@ async fn handle_container_exec_with_params(
1080910816 format!("{label} completed in background")
1081010817 };
1081110818 let bg_event = EventMsg::BackgroundEvent(BackgroundEventEvent { message });
10812- let ev = Event { id: sub_id_for_events.clone(), event_seq: 0, msg: bg_event, order: None };
10813- let _ = tx_event.send(ev).await;
10819+ if let Some(sess_arc) = sess_for_background_completion.as_ref() {
10820+ let ev = sess_arc.make_event(&sub_id_for_events, bg_event);
10821+ sess_arc.send_event(ev).await;
1081410822
10815- if let Some(tx) = TX_SUB_GLOBAL.get() {
1081610823 let header_label = if label.is_empty() {
1081710824 format!("call_id={}", call_id_for_events)
1081810825 } else {
@@ -10828,9 +10835,30 @@ async fn handle_container_exec_with_params(
1082810835 tool_output_max_bytes,
1082910836 );
1083010837 let dev_text = format!("{}\n\n{}", header, body);
10831- let _ = tx
10832- .send(Submission { id: uuid::Uuid::new_v4().to_string(), op: Op::AddPendingInputDeveloper { text: dev_text } })
10833- .await;
10838+ let dev_msg = ResponseInputItem::Message {
10839+ role: "developer".to_string(),
10840+ content: vec![ContentItem::InputText { text: dev_text }],
10841+ };
10842+ if sess_arc.enqueue_out_of_turn_item(dev_msg) {
10843+ sess_arc.cleanup_old_status_items().await;
10844+ let turn_context = sess_arc.make_turn_context();
10845+ let sub_id = sess_arc.next_internal_sub_id();
10846+ let sentinel_input = vec![InputItem::Text {
10847+ text: PENDING_ONLY_SENTINEL.to_string(),
10848+ }];
10849+ let agent = AgentTask::spawn(
10850+ Arc::clone(sess_arc),
10851+ turn_context,
10852+ sub_id,
10853+ sentinel_input,
10854+ TaskOriginKind::OutOfTurnDeveloper,
10855+ false,
10856+ );
10857+ sess_arc.set_task(agent);
10858+ }
10859+ } else {
10860+ let ev = Event { id: sub_id_for_events.clone(), event_seq: 0, msg: bg_event, order: None };
10861+ let _ = tx_event.send(ev).await;
1083410862 }
1083510863 }
1083610864 if let Some(n) = ANY_BG_NOTIFY.get() { n.notify_waiters(); }
0 commit comments