From ba94a025273b0af57e0b8166aead7d41421a8e97 Mon Sep 17 00:00:00 2001 From: Nishant Joshi <51093026+NishantJoshi00@users.noreply.github.com> Date: Mon, 16 Nov 2026 18:00:22 -0800 Subject: [PATCH 1/2] fix(codex): keep code-mode exec results and wrap freeform inputs for opencode Codex code mode writes custom_tool_call_output.output as an array of input_text content items; the reader only accepted strings, so the result was dropped for every target. Collapse all-text item lists to text (keep other lists as JSON) for both custom_tool_call_output and function_call_output. The OpenCode writer passed a non-object tool input (the raw JS of an exec call) into state.input, which opencode import rejects. Wrap it as {"input": ...}, matching the Claude Code export. Fixes #104 --- src/harness/codex.rs | 52 +++++++++++--- src/harness/opencode.rs | 9 +++ tests/regression/codex_code_mode.rs | 102 ++++++++++++++++++++++++++++ tests/regression/main.rs | 1 + 4 files changed, 153 insertions(+), 11 deletions(-) create mode 100644 tests/regression/codex_code_mode.rs diff --git a/src/harness/codex.rs b/src/harness/codex.rs index 3cc4402..494d2b9 100644 --- a/src/harness/codex.rs +++ b/src/harness/codex.rs @@ -331,12 +331,11 @@ fn lines_to_messages(lines: &[Line], fallback_ts: DateTime) -> Vec .and_then(Value::as_str) .map(String::from) { - let content = payload - .get("output") - .and_then(Value::as_str) - .map_or(ToolOutput::Text(String::new()), |s| { - ToolOutput::Text(s.to_string()) - }); + let content = match payload.get("output") { + Some(Value::String(s)) => ToolOutput::Text(s.clone()), + Some(Value::Array(items)) => content_items_output(items), + _ => ToolOutput::Text(String::new()), + }; queued.push(tool_result( ts, result_key(&call_id), @@ -382,11 +381,15 @@ fn lines_to_messages(lines: &[Line], fallback_ts: DateTime) -> Vec .and_then(Value::as_str) .map(String::from) { - let raw = payload - .get("output") - .and_then(Value::as_str) - .unwrap_or_default(); - let (content, is_error) = parse_custom_tool_output(raw); + // Code mode (`exec`) writes the output as an + // array of `input_text` content items rather + // than a string; reading only strings dropped + // the whole result. + let (content, is_error) = match payload.get("output") { + Some(Value::String(raw)) => parse_custom_tool_output(raw), + Some(Value::Array(items)) => (content_items_output(items), false), + _ => parse_custom_tool_output(""), + }; canonical_results.insert(result_key(&call_id)); queued.push(tool_result( ts, @@ -1654,6 +1657,33 @@ fn format_exec_output(payload: &Value) -> String { } } +/// A tool output written as a list of content items (`input_text`, +/// `output_text`, `input_image`, ...). All-text lists collapse to their text, +/// one item per line; anything else is kept verbatim as JSON so no part of the +/// result is lost. +fn content_items_output(items: &[Value]) -> ToolOutput { + let texts: Option> = items + .iter() + .map(|item| match item.get("type").and_then(Value::as_str) { + Some("input_text" | "output_text" | "text") => item.get("text").and_then(Value::as_str), + _ => None, + }) + .collect(); + match texts { + Some(texts) => { + let mut out = String::new(); + for text in texts { + if !out.is_empty() && !out.ends_with('\n') { + out.push('\n'); + } + out.push_str(text); + } + ToolOutput::Text(out) + } + None => ToolOutput::Json(Value::Array(items.to_vec())), + } +} + fn parse_custom_tool_output(raw: &str) -> (ToolOutput, bool) { match parse_json_string_or_raw(raw) { Value::Object(ref obj) => { diff --git a/src/harness/opencode.rs b/src/harness/opencode.rs index 09da176..47ab839 100644 --- a/src/harness/opencode.rs +++ b/src/harness/opencode.rs @@ -567,6 +567,15 @@ fn assistant_part( Block::ToolUse { id, tool } => { let (name, input) = tool.to_canonical(); let (oc_name, oc_input) = denormalize_tool(&name, input); + // `opencode import` rejects a non-object `state.input` ("Expected + // object"), and Codex freeform calls (code-mode `exec`, + // `apply_patch`) carry a raw string. Keep the payload inside an + // object, as the Claude Code export does. + let oc_input = if oc_input.is_object() { + oc_input + } else { + json!({ "input": oc_input }) + }; let mut state = json!({ "status": "completed", "input": oc_input, diff --git a/tests/regression/codex_code_mode.rs b/tests/regression/codex_code_mode.rs new file mode 100644 index 0000000..1d80c10 --- /dev/null +++ b/tests/regression/codex_code_mode.rs @@ -0,0 +1,102 @@ +//! Issue #104: Codex code mode (`custom_tool_call` named `exec`) stores the +//! call input as a raw JS string and the result as an array of `input_text` +//! content items. The Codex reader only accepted string outputs, so the +//! result arrived empty in every target (Claude Code resumed with "the file +//! read never returned anything"), and the `OpenCode` writer passed the +//! string input through, which `opencode import` rejects with "Expected +//! object ... at `["state"]["input"]`". + +use serde_json::{Value, json}; +use txcript::common::{Block, ToolOutput}; +use txcript::harness::{claude_code, codex, opencode}; +use txcript::{Codec, TextCodec}; + +const SCRIPT: &str = + "text(await tools.exec_command({cmd:\"cat marker.txt\",max_output_tokens:1000}));\n"; +const MARKER: &str = "The harbour gate code is 4471."; + +fn code_mode_session() -> String { + let call_id = "call_bb06566fce2b4dc9bc3d78aeccea79c7"; + [ + json!({"type": "session_meta", "payload": {"id": "code-mode", "cwd": "/work/repo"}}), + json!({"type": "response_item", "payload": {"type": "message", "role": "user", "content": [{"type": "input_text", "text": "Read marker.txt and tell me what it says"}]}}), + json!({"type": "response_item", "payload": {"type": "custom_tool_call", "status": "completed", "call_id": call_id, "name": "exec", "input": SCRIPT}}), + json!({"type": "response_item", "payload": {"type": "custom_tool_call_output", "call_id": call_id, "output": [ + {"type": "input_text", "text": "Script completed\nWall time 0.2 seconds\nOutput:\n"}, + {"type": "input_text", "text": format!("{{\"chunk_id\":\"61df54\",\"exit_code\":0,\"output\":\"{MARKER}\\n\"}}")}, + ]}}), + ] + .map(|mut line| { + line["timestamp"] = json!("2026-11-15T03:52:12.000Z"); + line.to_string() + }) + .join("\n") +} + +fn find_all<'a>(v: &'a Value, pred: &dyn Fn(&Value) -> bool, out: &mut Vec<&'a Value>) { + if pred(v) { + out.push(v); + } + match v { + Value::Array(items) => items.iter().for_each(|i| find_all(i, pred, out)), + Value::Object(map) => map.values().for_each(|i| find_all(i, pred, out)), + _ => {} + } +} + +#[test] +fn codex_code_mode_result_survives_and_opencode_input_is_an_object() { + let native = codex::Codex::from_text(&code_mode_session()).unwrap(); + let common = codex::Codex::to_common(&native).unwrap(); + + // The array-of-content-items output must be read, not dropped. + let result = common + .body + .iter() + .flat_map(|m| &m.content) + .find_map(|b| match b { + Block::ToolResult { content, .. } => Some(content), + _ => None, + }) + .expect("tool result"); + match result { + ToolOutput::Text(text) => { + assert!(text.starts_with("Script completed\n"), "{text}"); + assert!(text.contains(MARKER), "{text}"); + } + other @ ToolOutput::Json(_) => panic!("expected text output, got {other:?}"), + } + + // Claude Code: the tool_result carries the output. + let claude = + claude_code::ClaudeCode::to_text(&claude_code::ClaudeCode::from_common(&common).unwrap()) + .unwrap(); + let lines: Vec = claude + .lines() + .map(|l| serde_json::from_str(l).unwrap()) + .collect(); + let mut results = Vec::new(); + for line in &lines { + find_all(line, &|v| v["type"] == "tool_result", &mut results); + } + assert_eq!(results.len(), 1, "{claude}"); + assert!(results[0].to_string().contains("4471"), "{}", results[0]); + + // OpenCode: `state.input` must be an object that keeps the script, and + // the folded output must carry the result. + let oc = opencode::OpenCode::from_common(&common).unwrap(); + let oc_value: Value = serde_json::from_str(&opencode::OpenCode::to_text(&oc).unwrap()).unwrap(); + let mut tools = Vec::new(); + find_all(&oc_value, &|v| v["type"] == "tool", &mut tools); + assert_eq!(tools.len(), 1, "{oc_value}"); + let state = &tools[0]["state"]; + assert!(state["input"].is_object(), "{state}"); + assert_eq!(state["input"], json!({ "input": SCRIPT })); + assert!( + state["output"] + .as_str() + .unwrap_or_default() + .contains(MARKER), + "{state}" + ); +} diff --git a/tests/regression/main.rs b/tests/regression/main.rs index dc8cae5..c6fd0ea 100644 --- a/tests/regression/main.rs +++ b/tests/regression/main.rs @@ -7,6 +7,7 @@ //! belong in `tests/integration/`; see `tests/README.md`. mod claude_tool_result; +mod codex_code_mode; #[cfg(feature = "codex_zstd")] mod codex_compressed; mod codex_pairing; From 687bb9196f125ddcca025f34816bd3de5559e976 Mon Sep 17 00:00:00 2001 From: Nishant Joshi <51093026+NishantJoshi00@users.noreply.github.com> Date: Mon, 16 Nov 2026 18:24:25 -0800 Subject: [PATCH 2/2] fix(codex): flag failed code-mode scripts and unwrap freeform inputs from opencode Follow-ups to #104. Codex never writes a call's `success` flag to the rollout; a content-item list serializes as the bare array. For code mode the only on-disk failure signal is the status header Codex inserts as the first item ("Script failed\nWall time ... seconds\nOutput:\n"). Array results in custom_tool_call_output and function_call_output now set is_error from that header (matched whole, so script output that merely starts with the words is not mistaken for it); lists without it stay non-errors. The OpenCode writer wraps a non-object tool input as {"input": } because `opencode import` requires an object. It now flags that wrap in state.metadata (txcriptWrappedInput: true), and the reader unwraps only flagged parts whose input is still exactly that one-key object, so Codex -> OpenCode -> common restores the original freeform input while a genuine {"input": ...} tool input is left untouched. --- src/harness/codex.rs | 43 ++++++- src/harness/opencode.rs | 37 +++++- tests/regression/codex_code_mode.rs | 172 ++++++++++++++++++++++++++++ 3 files changed, 242 insertions(+), 10 deletions(-) diff --git a/src/harness/codex.rs b/src/harness/codex.rs index 494d2b9..18c584f 100644 --- a/src/harness/codex.rs +++ b/src/harness/codex.rs @@ -331,16 +331,16 @@ fn lines_to_messages(lines: &[Line], fallback_ts: DateTime) -> Vec .and_then(Value::as_str) .map(String::from) { - let content = match payload.get("output") { - Some(Value::String(s)) => ToolOutput::Text(s.clone()), - Some(Value::Array(items)) => content_items_output(items), - _ => ToolOutput::Text(String::new()), + let (content, is_error) = match payload.get("output") { + Some(Value::String(s)) => (ToolOutput::Text(s.clone()), false), + Some(Value::Array(items)) => parse_content_items_output(items), + _ => (ToolOutput::Text(String::new()), false), }; queued.push(tool_result( ts, result_key(&call_id), content, - false, + is_error, true, )); } @@ -387,7 +387,7 @@ fn lines_to_messages(lines: &[Line], fallback_ts: DateTime) -> Vec // the whole result. let (content, is_error) = match payload.get("output") { Some(Value::String(raw)) => parse_custom_tool_output(raw), - Some(Value::Array(items)) => (content_items_output(items), false), + Some(Value::Array(items)) => parse_content_items_output(items), _ => parse_custom_tool_output(""), }; canonical_results.insert(result_key(&call_id)); @@ -1661,6 +1661,37 @@ fn format_exec_output(payload: &Value) -> String { /// `output_text`, `input_image`, ...). All-text lists collapse to their text, /// one item per line; anything else is kept verbatim as JSON so no part of the /// result is lost. +/// Read a content-item list output together with its error bit. +/// +/// Codex keeps a call's `success` flag as internal metadata and never writes +/// it to the rollout: a content-item list serializes as the bare array. The +/// only on-disk failure signal is the status header code mode inserts as the +/// first item, `"{status}\nWall time ...\nOutput:\n"`, where `status` is +/// `Script failed` exactly when the script raised (`error_text` is set, so +/// `success` is false). `Script completed`, `Script terminated`, and +/// `Script running with cell ID ...` all carry `success: true`. Lists without +/// that header (MCP results, images) have no error signal and stay +/// non-errors, as string `function_call_output`s do. +fn parse_content_items_output(items: &[Value]) -> (ToolOutput, bool) { + let is_error = items.first().is_some_and(|first| { + first.get("type").and_then(Value::as_str) == Some("input_text") + && first + .get("text") + .and_then(Value::as_str) + .is_some_and(is_failed_script_header) + }); + (content_items_output(items), is_error) +} + +/// `Script failed\nWall time seconds[ (...)]\nOutput:\n`, the +/// whole item and nothing else, so script output that merely starts with +/// those words is not mistaken for the header. +fn is_failed_script_header(text: &str) -> bool { + text.strip_prefix("Script failed\nWall time ") + .and_then(|rest| rest.strip_suffix("\nOutput:\n")) + .is_some_and(|timing| !timing.contains('\n') && timing.contains(" seconds")) +} + fn content_items_output(items: &[Value]) -> ToolOutput { let texts: Option> = items .iter() diff --git a/src/harness/opencode.rs b/src/harness/opencode.rs index 47ab839..0895f43 100644 --- a/src/harness/opencode.rs +++ b/src/harness/opencode.rs @@ -287,6 +287,7 @@ fn parse_tool(part: &Value) -> Option<(Block, Option)> { .and_then(|s| s.get("input")) .cloned() .unwrap_or(Value::Object(Map::new())); + let raw_input = unwrap_freeform_input(state, raw_input); let (name, input) = normalize_tool(tool, raw_input); let use_block = Block::ToolUse { id: call_id.clone(), @@ -571,17 +572,22 @@ fn assistant_part( // object"), and Codex freeform calls (code-mode `exec`, // `apply_patch`) carry a raw string. Keep the payload inside an // object, as the Claude Code export does. - let oc_input = if oc_input.is_object() { - oc_input + // The wrap is flagged in `state.metadata` so the reader can undo + // exactly this wrap and never a genuine `{"input": ...}` input. + let (oc_input, oc_metadata) = if oc_input.is_object() { + (oc_input, json!({})) } else { - json!({ "input": oc_input }) + ( + json!({ "input": oc_input }), + json!({ (WRAPPED_INPUT_KEY): true }), + ) }; let mut state = json!({ "status": "completed", "input": oc_input, "output": "", "title": oc_name, - "metadata": {}, + "metadata": oc_metadata, "time": time, }); // Fold matching ToolResult from the following user turn into state.output. @@ -659,6 +665,29 @@ fn normalize_tool(tool: &str, input: Value) -> (String, Value) { } } +/// `state.metadata` flag set when the writer wrapped a non-object tool input +/// (for example a Codex code-mode script) as `{"input": }`, since +/// `opencode import` only accepts object inputs. +const WRAPPED_INPUT_KEY: &str = "txcriptWrappedInput"; + +/// Undo the writer's `{"input": }` wrap. Only parts that carry the +/// metadata flag are unwrapped, and only when the input is still exactly +/// that one-key object: a real tool whose input happens to be +/// `{"input": ...}` has no flag and passes through untouched. +fn unwrap_freeform_input(state: Option<&Value>, input: Value) -> Value { + let flagged = state + .and_then(|s| s.get("metadata")) + .and_then(|m| m.get(WRAPPED_INPUT_KEY)) + .and_then(Value::as_bool) + .unwrap_or(false); + match input { + Value::Object(mut obj) if flagged && obj.len() == 1 && obj.contains_key("input") => { + obj.remove("input").unwrap_or(Value::Null) + } + other => other, + } +} + fn denormalize_tool(name: &str, input: Value) -> (String, Value) { match name { "Bash" => ("bash".to_string(), input), diff --git a/tests/regression/codex_code_mode.rs b/tests/regression/codex_code_mode.rs index 1d80c10..8434cbd 100644 --- a/tests/regression/codex_code_mode.rs +++ b/tests/regression/codex_code_mode.rs @@ -100,3 +100,175 @@ fn codex_code_mode_result_survives_and_opencode_input_is_an_object() { "{state}" ); } + +fn header(status: &str) -> Value { + json!({"type": "input_text", "text": format!("{status}\nWall time 0.1 seconds\nOutput:\n")}) +} + +/// Follow-up to #104: Codex never writes a call's `success` flag; for code +/// mode the only on-disk failure signal is the `Script failed` status header +/// (the first content item). Array results used to always import as +/// non-errors, so a failed script looked successful in every target. +#[test] +fn codex_code_mode_failed_script_imports_as_an_error() { + let cases = [ + // (result kind, output items, expected is_error) + ( + "custom_tool_call_output", + json!([header("Script failed"), {"type": "input_text", "text": "Script error:\nReferenceError: nope is not defined"}]), + true, + ), + ( + "function_call_output", + json!([header("Script failed"), {"type": "input_text", "text": "Script error:\nboom"}]), + true, + ), + ( + "custom_tool_call_output", + json!([header("Script completed"), {"type": "input_text", "text": "ok"}]), + false, + ), + ( + "custom_tool_call_output", + json!([header("Script terminated")]), + false, + ), + // No code-mode header: no error signal, like a string output. + ( + "function_call_output", + json!([{"type": "input_text", "text": "Script failed\nWall time is a phrase in this file"}]), + false, + ), + ( + "function_call_output", + json!([{"type": "input_text", "text": "caption"}, {"type": "input_image", "image_url": "data:image/png;base64,AA=="}]), + false, + ), + ]; + for (kind, output, expected) in cases { + let call = if kind == "function_call_output" { + json!({"type": "function_call", "call_id": "c1", "name": "wait", "arguments": "{\"cell_id\":\"1\"}"}) + } else { + json!({"type": "custom_tool_call", "call_id": "c1", "name": "exec", "input": SCRIPT}) + }; + let source = [ + json!({"type": "session_meta", "payload": {"id": "code-mode-err", "cwd": "/work/repo"}}), + json!({"type": "response_item", "payload": {"type": "message", "role": "user", "content": [{"type": "input_text", "text": "go"}]}}), + json!({"type": "response_item", "payload": call}), + json!({"type": "response_item", "payload": {"type": kind, "call_id": "c1", "output": output}}), + ] + .map(|mut line| { + line["timestamp"] = json!("2026-11-15T03:52:12.000Z"); + line.to_string() + }) + .join("\n"); + let common = codex::Codex::to_common(&codex::Codex::from_text(&source).unwrap()).unwrap(); + let is_error = common + .body + .iter() + .flat_map(|m| &m.content) + .find_map(|b| match b { + Block::ToolResult { is_error, .. } => Some(*is_error), + _ => None, + }) + .expect("tool result"); + assert_eq!(is_error, expected, "{kind}: {output}"); + + // The flag reaches a target: Claude Code's tool_result.is_error. + let claude = claude_code::ClaudeCode::to_text( + &claude_code::ClaudeCode::from_common(&common).unwrap(), + ) + .unwrap(); + let mut results = Vec::new(); + for line in claude.lines() { + let line: Value = serde_json::from_str(line).unwrap(); + let mut found = Vec::new(); + find_all(&line, &|v| v["type"] == "tool_result", &mut found); + results.extend(found.into_iter().cloned()); + } + assert_eq!(results.len(), 1, "{claude}"); + assert_eq!( + results[0]["is_error"].as_bool().unwrap_or(false), + expected, + "{}", + results[0] + ); + } +} + +/// Follow-up to #104: the `OpenCode` writer wraps a non-object input as +/// `{"input": }` and flags it in `state.metadata`; the reader must undo +/// exactly that wrap so Codex -> `OpenCode` -> common returns the original +/// freeform input, while a genuine `{"input": ...}` tool input (no flag) is +/// left alone. +#[test] +fn opencode_round_trip_restores_codex_freeform_input() { + use txcript::common::Tool; + + let common = + codex::Codex::to_common(&codex::Codex::from_text(&code_mode_session()).unwrap()).unwrap(); + let original = common + .body + .iter() + .flat_map(|m| &m.content) + .find_map(|b| match b { + Block::ToolUse { tool, .. } => Some(tool.clone()), + _ => None, + }) + .unwrap(); + assert_eq!(original.to_canonical(), ("exec".to_string(), json!(SCRIPT))); + + let text = + opencode::OpenCode::to_text(&opencode::OpenCode::from_common(&common).unwrap()).unwrap(); + let back = + opencode::OpenCode::to_common(&opencode::OpenCode::from_text(&text).unwrap()).unwrap(); + let back_tool = back + .body + .iter() + .flat_map(|m| &m.content) + .find_map(|b| match b { + Block::ToolUse { tool, .. } => Some(tool.clone()), + _ => None, + }) + .unwrap(); + assert_eq!(back_tool, original); + // And a second hop writes the same OpenCode session again. + assert_eq!( + opencode::OpenCode::to_text(&opencode::OpenCode::from_common(&back).unwrap()).unwrap(), + text + ); + + // A genuine object input `{"input": "..."}` is not unwrapped. + let genuine = Tool::Raw { + tool_name: "custom_tool".into(), + input: json!({"input": "original"}), + }; + let mut with_genuine = common.clone(); + for block in with_genuine + .body + .iter_mut() + .flat_map(|m| m.content.iter_mut()) + { + if let Block::ToolUse { tool, .. } = block { + *tool = genuine.clone(); + } + } + let text = + opencode::OpenCode::to_text(&opencode::OpenCode::from_common(&with_genuine).unwrap()) + .unwrap(); + let back = + opencode::OpenCode::to_common(&opencode::OpenCode::from_text(&text).unwrap()).unwrap(); + let back_tool = back + .body + .iter() + .flat_map(|m| &m.content) + .find_map(|b| match b { + Block::ToolUse { tool, .. } => Some(tool.clone()), + _ => None, + }) + .unwrap(); + assert_eq!( + back_tool.to_canonical(), + ("custom_tool".to_string(), json!({"input": "original"})) + ); +}