Macro is a unified workspace for teams: email, chat, docs, tasks, agents, calls, and CRM — @-linked together with shared AI memory.
TypeScript agentaiai-agentsall-in-onecrmcrm-systememaillinearmcpmessagingnotesnotionofficerustslackslack-alternativestartupstartup-toolstypescriptworkspace
fix(codex-fold): end turns when a background command finishes late (#8973)
Wolf Mermelsteina62a979cd574
diff --git a/crates/coding_agent_worker/src/herdr/acp_agent.rs b/crates/coding_agent_worker/src/herdr/acp_agent.rsindex 479850d44e..14056a40d7 100644--- a/crates/coding_agent_worker/src/herdr/acp_agent.rs+++ b/crates/coding_agent_worker/src/herdr/acp_agent.rs@@ -1014,6 +1014,15 @@ impl Adapter {ended = Some(stop);break;}+ // A waiting Macro prompt owns the next turn: the item joins+ // it, and only the prompt's own turn end may answer it.+ LogEvent::LateUpdate(update) => {+ moved = true;+ self.notify_update(&session.id, update);+ if !session.prompt_pending.load(Ordering::SeqCst) {+ self.turn_complete(&session.id, "end_turn");+ }+ }}}Ok((moved, ended))diff --git a/crates/coding_agent_worker/src/herdr/acp_agent/test.rs b/crates/coding_agent_worker/src/herdr/acp_agent/test.rsindex 1519507ee0..31534d7bf7 100644--- a/crates/coding_agent_worker/src/herdr/acp_agent/test.rs+++ b/crates/coding_agent_worker/src/herdr/acp_agent/test.rs@@ -551,3 +551,87 @@ esac);adapter.shutdown.cancel();}++#[tokio::test]+async fn a_late_codex_item_never_answers_a_waiting_macro_prompt() {+ use std::io::Write as _;+ let root = tempfile::tempdir().unwrap();+ let id = uuid::Uuid::new_v4().to_string();+ let transcript = root.path().join("rollout.jsonl");+ std::fs::write(&transcript, "").unwrap();+ Store(root.path().to_owned())+ .save(&Record {+ version: 1,+ id: id.clone(),+ kind: TuiAgent::Codex,+ cwd: root.path().to_owned(),+ model: "gpt-5".into(),+ native_id: None,+ transcript: Some(transcript.clone()),+ started: 1,+ })+ .unwrap();+ let (mut adapter, mut output) = test_adapter(root.path());+ Arc::get_mut(&mut adapter).unwrap().options.kind = TuiAgent::Codex;+ adapter+ .load_session(&json!({"sessionId":id, "cwd":root.path()}))+ .await+ .unwrap();+ while output.try_recv().is_ok() {}+ let session = adapter.session(&json!({"sessionId":id})).unwrap();+ session.prompt_pending.store(true, Ordering::SeqCst);+ // Held throughout, so the observer stays out of the way.+ let mut guard = session.live.lock().await;+ let live = guard.as_mut().unwrap();+ let append = |payload: Value| {+ let mut file = std::fs::OpenOptions::new()+ .append(true)+ .open(&transcript)+ .unwrap();+ writeln!(file, "{}", json!({"type":"event_msg", "payload":payload})).unwrap();+ };+ let late = |id: &str| {+ json!({"type":"item_completed", "item":{"type":"CommandExecution", "id":id,+ "command":["bash","-lc","bun run type-check"], "status":"completed", "exit_code":0}})+ };++ // Submitted, but Codex has not started the prompt's turn yet.+ append(late("exec-1"));+ assert_eq!(+ adapter.forward(&session, live, &mut None).unwrap(),+ (true, None)+ );+ assert_eq!(+ output.try_recv().unwrap()["params"]["update"]["toolCallId"],+ "exec-1"+ );+ assert!(output.try_recv().is_err());++ append(json!({"type":"task_started"}));+ append(+ json!({"type":"item_completed", "item":{"type":"AgentMessage", "id":"m1", "content":[{"type":"Text", "text":"Done."}]}}),+ );+ append(json!({"type":"task_complete"}));+ assert_eq!(+ adapter.forward(&session, live, &mut None).unwrap(),+ (true, Some("end_turn"))+ );+ while output.try_recv().is_ok() {}++ // With no prompt waiting, the late item is a finished turn of its own.+ session.prompt_pending.store(false, Ordering::SeqCst);+ append(late("exec-2"));+ assert_eq!(+ adapter.forward(&session, live, &mut None).unwrap(),+ (true, None)+ );+ assert_eq!(+ output.try_recv().unwrap()["params"]["update"]["toolCallId"],+ "exec-2"+ );+ assert_eq!(+ output.try_recv().unwrap()["method"],+ "_session/turn_complete"+ );+ adapter.shutdown.cancel();+}diff --git a/crates/folds/README.md b/crates/folds/README.mdindex b600ca552f..e09f608f69 100644--- a/crates/folds/README.md+++ b/crates/folds/README.md@@ -12,5 +12,6 @@ Package names remain stable; only their workspace paths live under `folds/`.Run each package's tests from the repository root.Native folds implement `agent_fold::domain::transcript::Fold`. Their fixtures are-synthetic transcripts with pinned output snapshots. Runtime adapters consume the+synthetic transcripts with pinned output snapshots, plus sanitized excerpts of+real transcripts for regressions. Runtime adapters consume thesame events; there is no separate parsing implementation in macrod.diff --git a/crates/folds/agent_fold/src/domain/transcript.rs b/crates/folds/agent_fold/src/domain/transcript.rsindex c1c544acfe..06aedebba3 100644--- a/crates/folds/agent_fold/src/domain/transcript.rs+++ b/crates/folds/agent_fold/src/domain/transcript.rs@@ -21,6 +21,10 @@ pub enum LogEvent {Update(Value),/// The turn is over, with this ACP stop reason.TurnEnded(&'static str),+ /// A `session/update` for an item that finished after its turn ended.+ /// It is a finished turn of its own, unless a prompt is already waiting+ /// for the agent's next turn.+ LateUpdate(Value),}/// Bound tool output without splitting UTF-8 characters.diff --git a/crates/folds/claude_fold/src/test.rs b/crates/folds/claude_fold/src/test.rsindex 405fe25ddf..bad1cc3181 100644--- a/crates/folds/claude_fold/src/test.rs+++ b/crates/folds/claude_fold/src/test.rs@@ -146,7 +146,8 @@ fn api_messages_snapshot() {.flat_map(|line| native.entry(line)){let (method, params) = match event {- LogEvent::ModelChanged(_) => continue,+ // Claude transcripts never report an item after its turn.+ LogEvent::ModelChanged(_) | LogEvent::LateUpdate(_) => continue,LogEvent::Update(update) => {("session/update", json!({"sessionId":"s", "update":update}))}diff --git a/crates/folds/codex_fold/fixtures/background_command.jsonl b/crates/folds/codex_fold/fixtures/background_command.jsonlnew file mode 100644index 0000000000..4756964d4a--- /dev/null+++ b/crates/folds/codex_fold/fixtures/background_command.jsonl@@ -0,0 +1,7 @@+{"timestamp":"2026-10-16T15:42:50.484Z","ordinal":137,"type":"event_msg","payload":{"type":"task_started","turn_attribution":{"turn_id":"00000000-0000-7000-8000-000000000002","turn_trigger":"user","parent_turn_id":null,"initiating_agent_path":null,"root_turn_id":"00000000-0000-7000-8000-000000000002"},"turn_id":"00000000-0000-7000-8000-000000000002","root_turn_id":"00000000-0000-7000-8000-000000000002","started_at":1792165370,"model_context_window":475000,"collaboration_mode_kind":"default"}}+{"timestamp":"2026-10-16T15:42:50.514Z","ordinal":142,"type":"event_msg","payload":{"type":"item_completed","thread_id":"00000000-0000-7000-8000-000000000001","turn_id":"00000000-0000-7000-8000-000000000002","item":{"type":"UserMessage","id":"00000000-0000-7000-8000-000000000003","client_id":"00000000-0000-4000-8000-000000000004","content":[{"type":"text","text":"Make Enter move to the next row.","text_elements":[]}]},"started_at_ms":1792165370514,"completed_at_ms":1792165370514}}+{"timestamp":"2026-10-16T15:42:52.551Z","ordinal":143,"type":"event_msg","payload":{"type":"item_completed","thread_id":"00000000-0000-7000-8000-000000000001","turn_id":"00000000-0000-7000-8000-000000000002","item":{"type":"AgentMessage","id":"msg_commentary","content":[{"type":"Text","text":"I'll trace how Enter is handled in the grid."}],"phase":"commentary"},"started_at_ms":1792165371841,"completed_at_ms":1792165372551}}+{"timestamp":"2026-10-16T15:42:54.332Z","ordinal":147,"type":"event_msg","payload":{"type":"item_completed","thread_id":"00000000-0000-7000-8000-000000000001","turn_id":"00000000-0000-7000-8000-000000000002","item":{"type":"CommandExecution","id":"exec-search","process_id":"28807","command":["/bin/zsh","-lc","rg -n onKeyDown src/grid.tsx"],"cwd":"file:///repo","parsed_cmd":[{"type":"search","cmd":"rg -n onKeyDown src/grid.tsx","query":"onKeyDown","path":"src/grid.tsx"}],"source":"unified_exec_startup","status":"completed","aggregated_output":"42: onKeyDown={handleKey}\n","exit_code":0,"duration":{"secs":0,"nanos":13465}},"started_at_ms":1792165374331,"completed_at_ms":1792165374332}}+{"timestamp":"2026-10-16T15:51:22.831Z","ordinal":625,"type":"event_msg","payload":{"type":"item_completed","thread_id":"00000000-0000-7000-8000-000000000001","turn_id":"00000000-0000-7000-8000-000000000002","item":{"type":"AgentMessage","id":"msg_final","content":[{"type":"Text","text":"Enter now moves to the next row. Type checking is running in the background."}],"phase":"final_answer"},"started_at_ms":1792165881366,"completed_at_ms":1792165882830}}+{"timestamp":"2026-10-16T15:51:22.846Z","ordinal":629,"type":"event_msg","payload":{"type":"task_complete","root_turn_id":"00000000-0000-7000-8000-000000000002","turn_id":"00000000-0000-7000-8000-000000000002","last_agent_message":"Enter now moves to the next row. Type checking is running in the background.","started_at":1792165370,"completed_at":1792165882,"duration_ms":512364,"time_to_first_token_ms":1360}}+{"timestamp":"2026-10-16T15:51:23.964Z","ordinal":630,"type":"event_msg","payload":{"type":"item_completed","thread_id":"00000000-0000-7000-8000-000000000001","turn_id":"00000000-0000-7000-8000-000000000002","item":{"type":"CommandExecution","id":"exec-background","process_id":"26634","command":["/bin/zsh","-lc","bun run type-check > /tmp/types.log 2>&1"],"cwd":"file:///repo","parsed_cmd":[{"type":"unknown","cmd":"bun run type-check > /tmp/types.log 2>&1"}],"source":"unified_exec_startup","status":"completed","aggregated_output":"","exit_code":0,"duration":{"secs":13,"nanos":410327988}},"started_at_ms":1792165870553,"completed_at_ms":1792165883964}}diff --git a/crates/folds/codex_fold/fixtures/conversation.jsonl b/crates/folds/codex_fold/fixtures/conversation.jsonlindex ef3cc9ddba..828237aec1 100644--- a/crates/folds/codex_fold/fixtures/conversation.jsonl+++ b/crates/folds/codex_fold/fixtures/conversation.jsonl@@ -1,3 +1,4 @@+{"type":"event_msg","payload":{"type":"task_started"}}{"type":"event_msg","payload":{"type":"item_completed","item":{"type":"UserMessage","id":"u1","content":[{"type":"Text","text":"Read the file."}]}}}{"type":"event_msg","payload":{"type":"item_completed","item":{"type":"Reasoning","id":"r1","summary_text":["I will inspect it."]}}}{"type":"event_msg","payload":{"type":"item_completed","item":{"type":"CommandExecution","id":"t1","command":["bash","-lc","cat a.txt"],"parsed_cmd":[{"type":"read"}],"status":"completed","exit_code":1,"aggregated_output":"File missing"}}}@@ -7,6 +8,7 @@{"type":"event_msg","payload":{"type":"item_completed","item":{"type":"AgentMessage","id":"a1","content":[{"type":"Text","text":"The file is missing."}]}}}{"type":"event_msg","payload":{"type":"item_completed","item":{"type":"AgentMessage","id":"a1","content":[{"type":"Text","text":"The file is missing."}]}}}{"type":"event_msg","payload":{"type":"task_complete"}}+{"type":"event_msg","payload":{"type":"task_started"}}{"type":"event_msg","payload":{"type":"item_completed","item":{"type":"UserMessage","id":"u2","content":[{"text":"Stop."}]}}}{"type":"event_msg","payload":{"type":"turn_aborted"}}{"type":"future_record"}diff --git a/crates/folds/codex_fold/src/lib.rs b/crates/folds/codex_fold/src/lib.rsindex 1d7a411209..9cdc7d209f 100644--- a/crates/folds/codex_fold/src/lib.rs+++ b/crates/folds/codex_fold/src/lib.rs@@ -4,7 +4,9 @@//! `~/.codex/sessions/YYYY/MM/DD/rollout-<time>-<session-id>.jsonl`. Its//! `event_msg` entries carry one `item_completed` per finished item (agent//! messages, reasoning, commands, file changes, tool calls) and a-//! `task_complete` or `turn_aborted` when a turn ends.+//! `task_complete` or `turn_aborted` when a turn ends. A background command+//! can finish after its turn ended; that item is a [`LogEvent::LateUpdate`],+//! so no turn is left open.#![deny(missing_docs)]@@ -22,6 +24,7 @@ const TITLE_LIMIT: usize = 80;#[derive(Debug, Default)]pub struct CodexLog {seen: HashSet<String>,+ in_turn: bool,}impl Fold for CodexLog {@@ -55,13 +58,27 @@ impl Fold for CodexLog {{return Vec::new();}- item_update(item)- .map(LogEvent::Update)- .into_iter()- .collect()+ let Some(update) = item_update(item) else {+ return Vec::new();+ };+ if self.in_turn {+ vec![LogEvent::Update(update)]+ } else {+ vec![LogEvent::LateUpdate(update)]+ }+ }+ Some("task_started") => {+ self.in_turn = true;+ Vec::new()+ }+ Some("task_complete") => {+ self.in_turn = false;+ vec![LogEvent::TurnEnded("end_turn")]+ }+ Some("turn_aborted") => {+ self.in_turn = false;+ vec![LogEvent::TurnEnded("cancelled")]}- Some("task_complete") => vec![LogEvent::TurnEnded("end_turn")],- Some("turn_aborted") => vec![LogEvent::TurnEnded("cancelled")],_ => Vec::new(),}}diff --git a/crates/folds/codex_fold/src/test.rs b/crates/folds/codex_fold/src/test.rsindex 61fb15836e..668df8b64c 100644--- a/crates/folds/codex_fold/src/test.rs+++ b/crates/folds/codex_fold/src/test.rs@@ -12,6 +12,10 @@ fn completed(item: Value) -> String {#[test]fn a_command_turn_streams_messages_commands_and_its_end() {let mut log = CodexLog::default();+ assert!(+ log.entry(&event(json!({"type": "task_started"})))+ .is_empty()+ );assert_eq!(log.entry(&completed(json!({"type": "AgentMessage", "id": "m1", "phase": "commentary",@@ -53,6 +57,7 @@ fn a_command_turn_streams_messages_commands_and_its_end() {#[test]fn failed_multiline_commands_are_clipped_and_marked() {let mut log = CodexLog::default();+ log.entry(&event(json!({"type": "task_started"})));let events = log.entry(&completed(json!({"type": "CommandExecution", "id": "exec-2","command": ["/bin/bash", "-lc", "npm test\necho done"],@@ -71,6 +76,7 @@ fn failed_multiline_commands_are_clipped_and_marked() {#[test]fn reasoning_file_changes_and_aborts_are_translated() {let mut log = CodexLog::default();+ log.entry(&event(json!({"type": "task_started"})));assert_eq!(log.entry(&completed(json!({"type": "Reasoning", "id": "r", "summary_text": ["Plan the app"], "raw_content": []}))),[LogEvent::Update(json!({@@ -142,32 +148,77 @@ fn malformed_records_do_not_poison_following_records() {assert_eq!(actual, expected);}-#[test]-fn api_messages_snapshot() {+/// The ACP lines macrod sends the harness for a rollout, as `herdr::acp_agent`+/// forwards them.+fn macrod_wire(rollout: &str) -> Vec<String> {let mut native = CodexLog::default();- let mut wire = String::new();- for event in include_str!("../fixtures/conversation.jsonl")+ rollout.lines().flat_map(|line| native.entry(line))- {- let (method, params) = match event {- LogEvent::ModelChanged(_) => continue,- LogEvent::Update(update) => {- ("session/update", json!({"sessionId":"s", "update":update}))- }- LogEvent::TurnEnded(stop) => (- "_session/turn_complete",- json!({"sessionId":"s", "outcome":{"kind":if stop == "cancelled" {"cancelled"} else {"finished"}}}),- ),- };- wire.push_str(&json!({"direction":"to_server", "content":{"type":"acp", "jsonrpc":"2.0", "method":method, "params":params}}).to_string());- wire.push('\n');- }+ .filter_map(|event| {+ let (method, params) = match event {+ LogEvent::ModelChanged(_) => return None,+ LogEvent::Update(update) => {+ ("session/update", json!({"sessionId":"s", "update":update}))+ }+ // With no prompt waiting, macrod ends the late item's turn.+ LogEvent::LateUpdate(update) => {+ return Some(vec![+ json!({"direction":"to_server", "content":{"type":"acp", "jsonrpc":"2.0", "method":"session/update", "params":{"sessionId":"s", "update":update}}}).to_string(),+ json!({"direction":"to_server", "content":{"type":"acp", "jsonrpc":"2.0", "method":"_session/turn_complete", "params":{"sessionId":"s", "outcome":{"kind":"finished"}}}}).to_string(),+ ]);+ }+ LogEvent::TurnEnded(stop) => (+ "_session/turn_complete",+ json!({"sessionId":"s", "outcome":{"kind":if stop == "cancelled" {"cancelled"} else {"finished"}}}),+ ),+ };+ Some(vec![json!({"direction":"to_server", "content":{"type":"acp", "jsonrpc":"2.0", "method":method, "params":params}}).to_string()])+ })+ .flatten()+ .collect()+}++#[test]+fn api_messages_snapshot() {+ let wire = macrod_wire(include_str!("../fixtures/conversation.jsonl")).join("\n");let messages = agent_fold::domain::fold::fold(agent_fold::testing::parse_log(&wire));assert!(messages.len() >= 3, "native turns must reach API messages");insta::assert_json_snapshot!(messages);}+#[test]+fn a_background_command_finishing_after_its_turn_leaves_the_session_idle() {+ use agent_fold::domain::lifecycle::LifecycleFold;+ use agent_fold::domain::model::TurnState;++ // Recorded: Codex logs a background command's completion after the+ // `task_complete` of the turn that started it.+ let wire = macrod_wire(include_str!("../fixtures/background_command.jsonl"));+ let mut observed = LifecycleFold::new();+ for entry in agent_fold::testing::parse_log(&wire.join("\n")) {+ let _ = observed.push(entry);+ }+ assert_eq!(observed.inner().metadata().turn, TurnState::Idle);++ // The same turn prompted from Macro: macrod answers the prompt when the+ // turn ends, and the late command arrives after that answer.+ let (turn, late) = wire.split_at(wire.len() - 2);+ let mut prompted = vec![+ json!({"direction":"to_runtime", "content":{"type":"acp", "jsonrpc":"2.0", "id":1, "method":"session/prompt", "params":{"sessionId":"s", "prompt":[{"type":"text", "text":"Make Enter move to the next row."}]}}}).to_string(),+ ];+ prompted.extend_from_slice(turn);+ prompted.push(+ json!({"direction":"to_server", "content":{"type":"acp", "jsonrpc":"2.0", "id":1, "result":{"stopReason":"end_turn"}}}).to_string(),+ );+ prompted.extend_from_slice(late);+ let mut fold = LifecycleFold::new();+ for entry in agent_fold::testing::parse_log(&prompted.join("\n")) {+ let _ = fold.push(entry);+ }+ assert_eq!(fold.inner().metadata().turn, TurnState::Idle);+}+#[test]fn model_changes_snapshot() {let mut fold = CodexLog::default();