diff --git a/src/harness/fx.rs b/src/harness/fx.rs index 47d0660..73ff225 100644 --- a/src/harness/fx.rs +++ b/src/harness/fx.rs @@ -539,7 +539,10 @@ fn body_from_messages(meta: &Meta, messages: &[Message]) -> FxSession { while i < messages.len() && !is_prompt(&messages[i]) { i += 1; } - let body = &messages[body_start..i]; + // A prompt carrying tool results also closes this turn; `i` stays on + // it so its text still opens the next. + let body_end = i + usize::from(messages.get(i).is_some_and(has_tool_result)); + let body = &messages[body_start..body_end]; let ts = prompt .or_else(|| body.first()) .map_or(meta.timestamp, |m| m.timestamp); diff --git a/tests/regression/fx_tool_result.rs b/tests/regression/fx_tool_result.rs new file mode 100644 index 0000000..eb158d8 --- /dev/null +++ b/tests/regression/fx_tool_result.rs @@ -0,0 +1,80 @@ +#![allow(clippy::expect_used, clippy::panic, clippy::unwrap_used)] + +use serde_json::json; +use txcript::common::{Block, Role, ToolOutput}; +use txcript::harness::{claude_code::ClaudeCode, fx::Fx}; +use txcript::{Codec, TextCodec}; + +/// A user message can hold a tool result and text together. The fx writer +/// took it for a new prompt and read only its text, so the call reached fx +/// with no result. +#[test] +fn a_tool_result_sharing_a_user_message_with_text_reaches_fx() { + let line = |kind: &str, uuid: &str, secs: u32, content: serde_json::Value| { + json!({ + "type": kind, "uuid": uuid, "sessionId": "sess-mixed", "cwd": "/work/repo", + "timestamp": format!("2026-01-02T03:04:{secs:02}.000Z"), + "message": {"role": kind, "content": content}, + }) + .to_string() + }; + let source = [ + line("user", "u1", 5, json!("run the tests")), + line( + "assistant", + "a1", + 6, + json!([ + {"type": "tool_use", "id": "t1", "name": "Bash", "input": {"command": "cargo test"}}, + {"type": "tool_use", "id": "t2", "name": "Bash", "input": {"command": "cargo clippy"}}, + ]), + ), + line( + "user", + "u2", + 7, + json!([ + {"type": "tool_result", "tool_use_id": "t1", "content": "2 failed"}, + {"type": "tool_result", "tool_use_id": "t2", "content": "rejected", "is_error": true}, + {"type": "text", "text": "skip clippy, fix the tests first"}, + ]), + ), + line("assistant", "a2", 8, json!([{"type": "text", "text": "Fixing them."}])), + ] + .join("\n"); + let common = ClaudeCode::to_common(&ClaudeCode::from_text(&source).unwrap()).unwrap(); + assert_eq!(common.body.len(), 4); + assert_eq!(common.body[2].content.len(), 3, "one message, three blocks"); + + let fx = Fx::from_common(&common).unwrap(); + let back = Fx::to_common(&fx).unwrap(); + + let blocks: Vec<(Role, &Block)> = back + .body + .iter() + .flat_map(|m| m.content.iter().map(|b| (m.role, b))) + .collect(); + let result = |id: &str, text: &str, is_error| Block::ToolResult { + tool_use_id: id.into(), + content: ToolOutput::Text(text.into()), + is_error, + }; + let text = |text: &str| Block::Text { text: text.into() }; + assert_eq!( + blocks, + [ + (Role::User, &text("run the tests")), + (Role::Assistant, &common.body[1].content[0]), + (Role::Assistant, &common.body[1].content[1]), + (Role::User, &result("t1", "2 failed", false)), + (Role::User, &result("t2", "rejected", true)), + (Role::User, &text("skip clippy, fix the tests first")), + (Role::Assistant, &text("Fixing them.")), + ] + ); + assert_eq!(fx.body.events.len(), 3, "session header and two turns"); + assert_eq!( + Fx::to_common(&Fx::from_common(&back).unwrap()).unwrap(), + back + ); +} diff --git a/tests/regression/main.rs b/tests/regression/main.rs index dc8cae5..47f9eca 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 fx_tool_result; use chrono::{DateTime, Utc}; use txcript::common::{Block, Message, Meta, Role, Tool, ToolOutput};