diff --git a/crates/ide-agent/src/native/codex/mod.rs b/crates/ide-agent/src/native/codex/mod.rs index 55c0a6376..7e5632cfc 100644 --- a/crates/ide-agent/src/native/codex/mod.rs +++ b/crates/ide-agent/src/native/codex/mod.rs @@ -1415,6 +1415,19 @@ impl CodexShim { t.turn = None; } t.completed = Some(id.to_owned()); + // A command still open when its turn completed goes on in the background (a + // background terminal): its call returned, so it didn't fail with the turn. Its + // own completion reports how it ended. Sent before the prompt's answer settles the + // turn. + if completed { + let commands = t.items.values().filter(|i| i["type"] == "commandExecution"); + for id in commands.filter_map(|i| i["id"].as_str()) { + self.emit( + thread, + json!({ "sessionUpdate": "tool_call_update", "toolCallId": id, "status": "completed" }), + ); + } + } t.items.clear(); t.last_chunk = None; let error = t.error.take(); diff --git a/crates/ide-agent/src/native/codex/tests.rs b/crates/ide-agent/src/native/codex/tests.rs index 6b4c67571..fe4221b0c 100644 --- a/crates/ide-agent/src/native/codex/tests.rs +++ b/crates/ide-agent/src/native/codex/tests.rs @@ -2369,3 +2369,31 @@ async fn a_codex_without_background_terminals_reports_none() { acp.kill(); native.kill(); } + +#[tokio::test(flavor = "multi_thread")] +async fn a_command_left_running_by_a_completed_turn_is_not_a_failure() { + for (status, settled) in [("completed", true), ("interrupted", false)] { + let server = TerminalServer::new(Vec::new(), true); + let client = Arc::new(Client::default()); + let (native, acp) = terminal_session(server, client.clone()).await; + let item = json!({"type": "commandExecution", "id": "call_1", "command": "/bin/zsh -lc 'sleep 240'", + "cwd": "/w", "status": "inProgress", "commandActions": []}); + native.notify("item/started", json!({"threadId": "s", "turnId": "t1", "item": item})).unwrap(); + native.notify("turn/completed", json!({"threadId": "s", "turn": {"id": "t1", "status": status}})).unwrap(); + until("the command card", || client.of("s").iter().any(|u| u["sessionUpdate"] == "tool_call")).await; + tokio::time::sleep(Duration::from_millis(50)).await; + let updates: Vec = client + .of("s") + .into_iter() + .filter(|u| u["sessionUpdate"] == "tool_call_update" && u["toolCallId"] == "call_1") + .collect(); + let expected = if settled { + vec![json!({"sessionUpdate": "tool_call_update", "toolCallId": "call_1", "status": "completed"})] + } else { + Vec::new() + }; + assert_eq!(updates, expected, "turn {status}"); + acp.kill(); + native.kill(); + } +}