From c5889b68b91705f7ee918cc0dd363a080d22c140 Mon Sep 17 00:00:00 2001 From: Nishant Joshi <51093026+NishantJoshi00@users.noreply.github.com> Date: Mon, 16 Nov 2026 18:48:51 -0800 Subject: [PATCH] fix(pi,claude_code): keep parallel tool calls in one response so their results replay Sources that record one block per message (Codex function_call items, Claude Code's one-line-per-block JSONL) give a parallel call batch as consecutive assistant messages with every result after them. The pi and Claude Code writers replayed each call as its own assistant message, so the first call was no longer followed by its result: - pi paired it with a synthetic "No result provided" error and left the real result orphaned; - Claude Code replaced its result with "[Tool result missing due to internal error]", so the real output never reached the model. A run of consecutive assistant messages with a tool call before its last message is now treated as one model response, written in each target's native shape: pi merges the run into one assistant message (blocks in order, first timestamp/model, last stop reason/usage), and Claude Code keeps one line per message but gives the run's lines a shared deterministic message.id, which Claude Code merges back into one API message on resume. Sequential calls and other runs are written exactly as before. --- src/harness/claude_code.rs | 45 +++- src/harness/pi.rs | 67 ++++++ tests/regression/main.rs | 1 + tests/regression/parallel_tool_calls.rs | 299 ++++++++++++++++++++++++ 4 files changed, 411 insertions(+), 1 deletion(-) create mode 100644 tests/regression/parallel_tool_calls.rs diff --git a/src/harness/claude_code.rs b/src/harness/claude_code.rs index 9e90871..05ace03 100644 --- a/src/harness/claude_code.rs +++ b/src/harness/claude_code.rs @@ -265,6 +265,7 @@ pub(crate) fn messages_to_records(meta: &Meta, messages: &[Message]) -> Vec None, }) .collect(); + let response_ids = response_message_ids(&session_id, messages); let mut parent_uuid: Option = None; for (i, msg) in messages.iter().enumerate() { let uuid = entry_uuid(&session_id, i); @@ -294,7 +295,10 @@ pub(crate) fn messages_to_records(meta: &Meta, messages: &[Message]) -> Vec Vec Vec> { + let mut ids = vec![None; messages.len()]; + let mut start = 0; + while start < messages.len() { + let mut end = start + 1; + if messages[start].role == Role::Assistant { + while end < messages.len() && messages[end].role == Role::Assistant { + end += 1; + } + let calls_before_last = messages[start..end - 1] + .iter() + .any(|m| m.content.iter().any(|b| matches!(b, Block::ToolUse { .. }))); + if calls_before_last { + let id = format!("msg_{}", entry_uuid(session_id, start).replace('-', "")); + ids[start..end].fill(Some(id)); + } + } + start = end; + } + ids +} + /// Claude Code's native artifact is a normal `Artifact` tool call followed /// by a tool result. Lower path-backed Common artifacts into that observed /// shape while leaving the rest of the serializer unchanged. diff --git a/src/harness/pi.rs b/src/harness/pi.rs index 5105966..134295f 100644 --- a/src/harness/pi.rs +++ b/src/harness/pi.rs @@ -296,7 +296,21 @@ pub(crate) fn messages_to_records(meta: &Meta, messages: &[Message]) -> Vec = HashMap::new(); let mut parent_id: Option = None; + let joins = joins_previous_response(messages); + for (i, msg) in messages.iter().enumerate() { + // Written as part of the response that starts before it. + if joins[i] { + continue; + } + let run_end = i + 1 + joins[i + 1..].iter().take_while(|&&j| j).count(); + let merged; + let msg = if run_end > i + 1 { + merged = merge_response(&messages[i..run_end]); + &merged + } else { + msg + }; let ts_iso = msg.timestamp.to_rfc3339_opts(SecondsFormat::Millis, true); let ts_ms = msg.timestamp.timestamp_millis(); for (j, payload) in pi_payloads_for(msg, ts_ms, &mut tool_names) @@ -317,6 +331,59 @@ pub(crate) fn messages_to_records(meta: &Meta, messages: &[Message]) -> Vec Vec { + let mut joins = vec![false; messages.len()]; + let mut start = 0; + while start < messages.len() { + let mut end = start + 1; + if messages[start].role == Role::Assistant { + while end < messages.len() && messages[end].role == Role::Assistant { + end += 1; + } + let calls_before_last = messages[start..end - 1] + .iter() + .any(|m| m.content.iter().any(|b| matches!(b, Block::ToolUse { .. }))); + if calls_before_last { + joins[start + 1..end].fill(true); + } + } + start = end; + } + joins +} + +/// One assistant message for a response recorded over several: the blocks in +/// order, the first timestamp and model, and the last stop reason and usage +/// (a response's final ones). +fn merge_response(run: &[Message]) -> Message { + let mut merged = run[0].clone(); + for msg in &run[1..] { + merged.content.extend(msg.content.iter().cloned()); + if merged.model.is_none() { + merged.model.clone_from(&msg.model); + } + if msg.stop_reason.is_some() { + merged.stop_reason.clone_from(&msg.stop_reason); + } + if msg.usage.is_some() { + merged.usage.clone_from(&msg.usage); + } + } + merged +} + fn pi_payloads_for( msg: &Message, ts_ms: i64, diff --git a/tests/regression/main.rs b/tests/regression/main.rs index dc8cae5..df0b624 100644 --- a/tests/regression/main.rs +++ b/tests/regression/main.rs @@ -10,6 +10,7 @@ mod claude_tool_result; #[cfg(feature = "codex_zstd")] mod codex_compressed; mod codex_pairing; +mod parallel_tool_calls; use chrono::{DateTime, Utc}; use txcript::common::{Block, Message, Meta, Role, Tool, ToolOutput}; diff --git a/tests/regression/parallel_tool_calls.rs b/tests/regression/parallel_tool_calls.rs new file mode 100644 index 0000000..1a26e23 --- /dev/null +++ b/tests/regression/parallel_tool_calls.rs @@ -0,0 +1,299 @@ +//! Parallel tool calls recorded one call per message (Codex `function_call` +//! items; Claude Code's one-line-per-block JSONL) reach Common as +//! consecutive assistant messages with every result after them. The pi and +//! Claude Code writers replayed each call as its own assistant message, so +//! the first call was no longer followed by its result: +//! - pi paired it with a synthetic "No result provided" error and orphaned +//! the real result; +//! - Claude Code replaced its result with "[Tool result missing due to +//! internal error]". +//! +//! Each target's own replay rule is modelled below and checked on the +//! written session. + +use std::collections::HashSet; + +use serde_json::{Value, json}; +use txcript::common::{Block, Role}; +use txcript::harness::{claude_code, codex, pi}; +use txcript::{Codec, TextCodec, Transcript}; + +fn codex_session(parallel: bool) -> String { + let call = |id: &str, file: &str| json!({"type": "response_item", "payload": {"type": "function_call", "call_id": id, "name": "exec_command", "arguments": json!({"cmd": format!("cat {file}")}).to_string()}}); + let output = |id: &str, text: &str| json!({"type": "response_item", "payload": {"type": "function_call_output", "call_id": id, "output": text}}); + let mut lines = vec![ + json!({"type": "session_meta", "payload": {"id": "parallel", "cwd": "/work/repo"}}), + json!({"type": "response_item", "payload": {"type": "message", "role": "user", "content": [{"type": "input_text", "text": "read a and b"}]}}), + json!({"type": "response_item", "payload": {"type": "message", "role": "assistant", "content": [{"type": "output_text", "text": "Reading both."}]}}), + ]; + if parallel { + lines.extend([ + call("call_a", "a.txt"), + call("call_b", "b.txt"), + output("call_a", "alpha"), + output("call_b", "bravo"), + ]); + } else { + lines.extend([ + call("call_a", "a.txt"), + output("call_a", "alpha"), + call("call_b", "b.txt"), + output("call_b", "bravo"), + ]); + } + lines.push(json!({"type": "response_item", "payload": {"type": "message", "role": "assistant", "content": [{"type": "output_text", "text": "a is alpha, b is bravo"}]}})); + lines + .into_iter() + .map(|mut line| { + line["timestamp"] = json!("2026-11-16T10:00:00.000Z"); + line.to_string() + }) + .collect::>() + .join("\n") +} + +/// Claude Code's own JSONL for the same parallel batch: one line per block, +/// each result on its own user line. +fn claude_session() -> String { + let line = |uuid: &str, parent: Option<&str>, kind: &str, content: Value| { + json!({"type": kind, "uuid": uuid, "parentUuid": parent, "sessionId": "parallel-cc", + "timestamp": "2026-11-16T10:00:00.000Z", "cwd": "/work/repo", + "message": {"role": kind, "content": content}}) + .to_string() + }; + [ + line("u1", None, "user", json!("read a and b")), + line("a1", Some("u1"), "assistant", json!([{"type": "text", "text": "Reading both."}])), + line("a2", Some("a1"), "assistant", json!([{"type": "tool_use", "id": "call_a", "name": "Read", "input": {"file_path": "/work/repo/a.txt"}}])), + line("a3", Some("a2"), "assistant", json!([{"type": "tool_use", "id": "call_b", "name": "Read", "input": {"file_path": "/work/repo/b.txt"}}])), + line("u2", Some("a3"), "user", json!([{"type": "tool_result", "tool_use_id": "call_a", "content": "alpha"}])), + line("u3", Some("u2"), "user", json!([{"type": "tool_result", "tool_use_id": "call_b", "content": "bravo"}])), + line("a4", Some("u3"), "assistant", json!([{"type": "text", "text": "a is alpha, b is bravo"}])), + ] + .join("\n") +} + +fn from_codex(parallel: bool) -> Transcript { + codex::Codex::to_common(&codex::Codex::from_text(&codex_session(parallel)).unwrap()).unwrap() +} + +fn from_claude() -> Transcript { + claude_code::ClaudeCode::to_common( + &claude_code::ClaudeCode::from_text(&claude_session()).unwrap(), + ) + .unwrap() +} + +/// pi's replay rule: an assistant message's tool calls must be answered by +/// the `toolResult` messages that directly follow it; anything else first +/// fills the open calls with "No result provided". Returns the synthetic +/// errors pi would add and the results it would find unmatched. +fn pi_replay_violations(text: &str) -> (Vec, Vec) { + let mut missing = Vec::new(); + let mut orphaned = Vec::new(); + let mut open: Vec = Vec::new(); + for line in text.lines() { + let line: Value = serde_json::from_str(line).unwrap(); + if line["type"] != "message" { + continue; + } + let msg = &line["message"]; + if msg["role"] == "toolResult" { + let id = msg["toolCallId"].as_str().unwrap().to_string(); + match open.iter().position(|o| *o == id) { + Some(p) => { + open.remove(p); + } + None => orphaned.push(id), + } + continue; + } + missing.append(&mut open); + if msg["role"] == "assistant" { + for block in msg["content"].as_array().unwrap() { + if block["type"] == "toolCall" { + open.push(block["id"].as_str().unwrap().to_string()); + } + } + } + } + missing.append(&mut open); + (missing, orphaned) +} + +/// Claude Code's replay rule: consecutive assistant lines with the same +/// `message.id` are one API message, consecutive user lines are one API +/// message, and every `tool_use` must be answered in the very next user +/// message; otherwise its result is replaced with "[Tool result missing due +/// to internal error]". Returns the ids that would be replaced. +fn claude_replay_violations(text: &str) -> Vec { + /// One API message: role, message id, `tool_use` ids, `tool_result` ids. + type ApiMessage = (String, Option, Vec, HashSet); + let mut api: Vec = Vec::new(); + for line in text.lines() { + let line: Value = serde_json::from_str(line).unwrap(); + let role = match line["type"].as_str() { + Some(r @ ("user" | "assistant")) => r.to_string(), + _ => continue, + }; + let id = line["message"]["id"].as_str().map(String::from); + let blocks = line["message"]["content"] + .as_array() + .cloned() + .unwrap_or_default(); + let uses: Vec = blocks + .iter() + .filter(|b| b["type"] == "tool_use") + .map(|b| b["id"].as_str().unwrap().to_string()) + .collect(); + let results: HashSet = blocks + .iter() + .filter(|b| b["type"] == "tool_result") + .map(|b| b["tool_use_id"].as_str().unwrap().to_string()) + .collect(); + let merge = api.last().is_some_and(|(r, last_id, _, _)| { + *r == role && (role == "user" || (id.is_some() && *last_id == id)) + }); + if merge { + let last = api.last_mut().unwrap(); + last.2.extend(uses); + last.3.extend(results); + } else { + api.push((role, id, uses, results)); + } + } + let mut missing = Vec::new(); + for (i, (role, _, uses, _)) in api.iter().enumerate() { + if role != "assistant" { + continue; + } + let next = api.get(i + 1).filter(|(r, ..)| r == "user"); + for id in uses { + if !next.is_some_and(|(.., results)| results.contains(id)) { + missing.push(id.clone()); + } + } + } + missing +} + +#[test] +fn parallel_calls_from_codex_and_claude_code_replay_with_their_results_in_pi() { + for (source, common) in [("codex", from_codex(true)), ("claude_code", from_claude())] { + // The source shape that triggers it: the calls in separate assistant + // messages, the results after both. + let roles: Vec = common.body.iter().map(|m| m.role).collect(); + assert_eq!( + roles, + [ + Role::User, + Role::Assistant, + Role::Assistant, + Role::Assistant, + Role::User, + Role::User, + Role::Assistant + ], + "{source}" + ); + + let text = pi::Pi::to_text(&pi::Pi::from_common(&common).unwrap()).unwrap(); + assert_eq!( + pi_replay_violations(&text), + (vec![], vec![]), + "{source}: {text}" + ); + + // Nothing is lost: both calls and both results, in order. + let back = pi::Pi::to_common(&pi::Pi::from_text(&text).unwrap()).unwrap(); + let blocks: Vec = back + .body + .iter() + .flat_map(|m| &m.content) + .map(|b| match b { + Block::Text { text } => format!("text:{text}"), + Block::ToolUse { id, .. } => format!("call:{id}"), + Block::ToolResult { tool_use_id, .. } => format!("result:{tool_use_id}"), + other => format!("{other:?}"), + }) + .collect(); + assert_eq!( + blocks, + [ + "text:read a and b", + "text:Reading both.", + "call:call_a", + "call:call_b", + "result:call_a", + "result:call_b", + "text:a is alpha, b is bravo" + ], + "{source}" + ); + } +} + +#[test] +fn parallel_calls_from_codex_and_claude_code_replay_with_their_results_in_claude_code() { + for (source, common) in [("codex", from_codex(true)), ("claude_code", from_claude())] { + let text = claude_code::ClaudeCode::to_text( + &claude_code::ClaudeCode::from_common(&common).unwrap(), + ) + .unwrap(); + assert_eq!( + claude_replay_violations(&text), + Vec::::new(), + "{source}: {text}" + ); + + // The response's lines share one id, as Claude Code writes them; the + // later, separate response does not. + let assistant_ids: Vec> = text + .lines() + .map(|l| serde_json::from_str::(l).unwrap()) + .filter(|l| l["type"] == "assistant") + .map(|l| l["message"]["id"].as_str().map(String::from)) + .collect(); + assert_eq!(assistant_ids.len(), 4, "{source}"); + assert!( + assistant_ids[0] + .as_deref() + .is_some_and(|id| id.starts_with("msg_")) + ); + assert_eq!(assistant_ids[0], assistant_ids[1], "{source}"); + assert_eq!(assistant_ids[1], assistant_ids[2], "{source}"); + assert_eq!(assistant_ids[3], None, "{source}"); + + // Reading the file back gives the same conversation. + let back = + claude_code::ClaudeCode::to_common(&claude_code::ClaudeCode::from_text(&text).unwrap()) + .unwrap(); + assert_eq!(back.body, common.body, "{source}"); + } +} + +/// Sequential calls (call, result, call, result) were already fine and must +/// be written exactly as before: separate pi messages, no Claude Code ids. +#[test] +fn sequential_calls_are_written_unchanged() { + let common = from_codex(false); + let text = pi::Pi::to_text(&pi::Pi::from_common(&common).unwrap()).unwrap(); + assert_eq!(pi_replay_violations(&text), (vec![], vec![])); + let assistants = text + .lines() + .map(|l| serde_json::from_str::(l).unwrap()) + .filter(|l| l["message"]["role"] == "assistant") + .count(); + assert_eq!(assistants, 4); + + let text = + claude_code::ClaudeCode::to_text(&claude_code::ClaudeCode::from_common(&common).unwrap()) + .unwrap(); + assert_eq!(claude_replay_violations(&text), Vec::::new()); + assert!( + text.lines() + .map(|l| serde_json::from_str::(l).unwrap()) + .all(|l| l["message"].get("id").is_none()), + "{text}" + ); +}