A self-improving RLM agent for coding workflows and long-running autonomous tasks.
Rust
fix: name or drop tool calls streamed without a function name; strip them on replay (#3752)
kt5e9505f1ced2
diff --git a/crates/pa-ai/src/providers/mistral/mod.rs b/crates/pa-ai/src/providers/mistral/mod.rsindex d3ec0a7284..f90e65112c 100644--- a/crates/pa-ai/src/providers/mistral/mod.rs+++ b/crates/pa-ai/src/providers/mistral/mod.rs@@ -21,6 +21,8 @@ use serde_json::{json, Value};mod convert;mod stream;+#[cfg(test)]+mod wire_tests;pub use stream::stream_mistral;diff --git a/crates/pa-ai/src/providers/mistral/stream.rs b/crates/pa-ai/src/providers/mistral/stream.rsindex 325d1699c2..61907baf12 100644--- a/crates/pa-ai/src/providers/mistral/stream.rs+++ b/crates/pa-ai/src/providers/mistral/stream.rs@@ -17,7 +17,9 @@ use crate::providers::mistral::convert::{build_chat_payload, derive_mistral_tool_call_id, MistralToolCallIdNormalizer,};use crate::providers::mistral::{build_request_headers, MistralOptions, API_MISTRAL_CONVERSATIONS};-use crate::providers::transform_messages::transform_messages_with_normalizer;+use crate::providers::transform_messages::{+ repair_unnamed_tool_calls, transform_messages_with_normalizer,+};use crate::types::{done_reason, error_reason, AssistantContent, AssistantMessage, Context, Model, StopReason,TextContent, ThinkingContent, ToolCall, Usage,@@ -361,8 +363,12 @@ async fn run_stream(if let Err(error) = stream_result {state.settle_partial_tool_calls(output);+ repair_unnamed_tool_calls(output);return Err(error);}+ // After the `*_end` events and the error settle, which address blocks by their streamed+ // content indices; the final message is what the session stores.+ repair_unnamed_tool_calls(output);Ok(())}@@ -370,7 +376,7 @@ async fn run_stream(/// Scratch state for the streaming loop (`consumeChatStream` in the TS).struct MistralStreamState {current_block: Option<CurrentBlock>,- tool_blocks_by_key: HashMap<String, usize>,+ tool_blocks_by_index: HashMap<i64, usize>,tool_partial_args: HashMap<usize, StreamingJsonAccumulator>,}@@ -383,7 +389,7 @@ impl MistralStreamState {fn new() -> Self {Self {current_block: None,- tool_blocks_by_key: HashMap::new(),+ tool_blocks_by_index: HashMap::new(),tool_partial_args: HashMap::new(),}}@@ -619,20 +625,20 @@ impl MistralStreamState {if self.current_block.is_some() {self.finish_current_block(output, writer);}- let call_id = match tool_call.get("id").and_then(Value::as_str) {- Some(id) if !id.is_empty() && id != "null" => id.to_string(),- _ => derive_mistral_tool_call_id(- &format!(- "toolcall:{}",- tool_call.get("index").and_then(Value::as_i64).unwrap_or(0)- ),- 0,- ),- };- let tool_index = tool_call.get("index").and_then(Value::as_i64).unwrap_or(0);- let key = format!("{call_id}:{}", tool_index.max(0));-- let existing_index = self.tool_blocks_by_key.get(&key).copied();+ let tool_index = tool_call+ .get("index")+ .and_then(Value::as_i64)+ .unwrap_or(0)+ .max(0);+ let call_id = tool_call+ .get("id")+ .and_then(Value::as_str)+ .filter(|id| !id.is_empty() && *id != "null");+ let name = tool_call+ .pointer("/function/name")+ .and_then(Value::as_str)+ .filter(|name| !name.is_empty());+ let existing_index = self.tool_blocks_by_index.get(&tool_index).copied();let block_index = match existing_index {Some(index)if matches!(@@ -643,20 +649,18 @@ impl MistralStreamState {index}_ => {- let name = tool_call- .pointer("/function/name")- .and_then(Value::as_str)- .unwrap_or_default()- .to_string();output.content.push(AssistantContent::ToolCall(ToolCall {- id: call_id.clone(),- name,+ id: call_id.map_or_else(+ || derive_mistral_tool_call_id(&format!("toolcall:{tool_index}"), 0),+ str::to_string,+ ),+ name: name.unwrap_or_default().to_string(),arguments: Map::default(),thought_signature: None,rest: Map::default(),}));let index = output.content.len() - 1;- self.tool_blocks_by_key.insert(key.clone(), index);+ self.tool_blocks_by_index.insert(tool_index, index);writer.push(AssistantMessageEvent::ToolcallStart {content_index: index as u64,partial: output.clone(),@@ -665,10 +669,19 @@ impl MistralStreamState {}};+ if let AssistantContent::ToolCall(block) = &mut output.content[block_index] {+ if let Some(id) = call_id {+ block.id = id.to_string();+ }+ if let Some(name) = name {+ block.name = name.to_string();+ }+ }+let args_delta = match tool_call.pointer("/function/arguments") {Some(Value::String(text)) => text.clone(),Some(other) => serde_json::to_string(other).unwrap_or_else(|_| "{}".to_string()),- None => "{}".to_string(),+ None => String::new(),};let parsed = self.tool_partial_args@@ -691,7 +704,7 @@ impl MistralStreamState {/// Port of the trailing block finalization.fn finish(&mut self, output: &mut AssistantMessage, writer: &AssistantMessageEventWriter) {self.finish_current_block(output, writer);- let mut indexes: Vec<usize> = self.tool_blocks_by_key.values().copied().collect();+ let mut indexes: Vec<usize> = self.tool_blocks_by_index.values().copied().collect();indexes.sort_unstable();indexes.dedup();for index in indexes {diff --git a/crates/pa-ai/src/providers/mistral/wire_tests.rs b/crates/pa-ai/src/providers/mistral/wire_tests.rsnew file mode 100644index 0000000000..feceaec53c--- /dev/null+++ b/crates/pa-ai/src/providers/mistral/wire_tests.rs@@ -0,0 +1,173 @@+use super::*;+use std::fmt::Write as _;+use tokio::io::{AsyncReadExt, AsyncWriteExt};++/// Serve one streamed response of `deltas` (one `tool_calls` delta per event) ending in+/// `finish_reason`, and return the provider's final message.+async fn stream_tool_call_deltas(+ deltas: Vec<Value>,+ finish_reason: &'static str,+) -> AssistantMessage {+ let listener = tokio::net::TcpListener::bind("127.0.0.1:0").await.unwrap();+ let addr = listener.local_addr().unwrap();+ let server = tokio::spawn(async move {+ let (mut socket, _) = listener.accept().await.unwrap();+ let mut request = Vec::new();+ let mut chunk = [0u8; 4096];+ let (body_start, content_length) = loop {+ let read = socket.read(&mut chunk).await.unwrap();+ assert_ne!(read, 0, "request ended before its headers");+ request.extend_from_slice(&chunk[..read]);+ if let Some(pos) = request.windows(4).position(|w| w == b"\r\n\r\n") {+ let content_length = String::from_utf8_lossy(&request[..pos])+ .lines()+ .find_map(|line| {+ let (name, value) = line.split_once(':')?;+ name.eq_ignore_ascii_case("content-length")+ .then(|| value.trim().parse::<usize>().unwrap())+ })+ .expect("POST has a content length");+ break (pos + 4, content_length);+ }+ };+ while request.len() < body_start + content_length {+ let read = socket.read(&mut chunk).await.unwrap();+ assert_ne!(read, 0, "request ended before its body");+ request.extend_from_slice(&chunk[..read]);+ }+ let mut response_body = String::new();+ for delta in deltas {+ let event = json!({"id": "response1", "choices": [{"index": 0, "delta": {"tool_calls": [delta]}}]});+ write!(response_body, "data: {event}\n\n").unwrap();+ }+ let finish =+ json!({"choices": [{"index": 0, "delta": {}, "finish_reason": finish_reason}]});+ write!(response_body, "data: {finish}\n\ndata: [DONE]\n\n").unwrap();+ let response = format!(+ "HTTP/1.1 200 OK\r\nContent-Type: text/event-stream\r\nContent-Length: {}\r\nConnection: close\r\n\r\n{response_body}",+ response_body.len(),+ );+ socket.write_all(response.as_bytes()).await.unwrap();+ });+ let model: Model = serde_json::from_value(json!({+ "id": "zai-glm-5-3", "name": "GLM",+ "api": API_MISTRAL_CONVERSATIONS, "provider": "mistral",+ "baseUrl": format!("http://{addr}"), "reasoning": false,+ "input": ["text"],+ "cost": {"input": 0, "output": 0, "cacheRead": 0, "cacheWrite": 0},+ "contextWindow": 128_000, "maxTokens": 128,+ }))+ .unwrap();+ let options = SimpleStreamOptions {+ base: StreamOptions {+ api_key: Some("test".into()),+ ..Default::default()+ },+ ..Default::default()+ };+ let context = Context {+ system_prompt: None,+ messages: vec![],+ tools: None,+ };+ let result = stream_simple_mistral(&model, &context, Some(&options))+ .result()+ .await;+ server.await.unwrap();+ result+}++// Keep the split/interleaved fixtures beside their full HTTP/SSE verification.+#[allow(clippy::too_many_lines)]+#[tokio::test]+async fn streamed_tool_calls_keep_index_identity_and_complete_arguments() {+ let cases = [+ (+ vec![+ json!({"index": 0, "id": "python123", "function": {"name": "ipython", "arguments": "{\"code\":\""}}),+ json!({"index": 0, "function": {"arguments": "print(1)"}}),+ json!({"index": 0, "function": {"arguments": "\\nprint(2)\"}"}}),+ ],+ json!([{"id": "python123", "name": "ipython", "arguments": {"code": "print(1)\nprint(2)"}}]),+ ),+ (+ vec![+ json!({"index": 0, "id": "python123", "function": {"name": "ipython"}}),+ json!({"index": 1, "id": "other1234", "function": {"name": "edit", "arguments": "{\"path\":"}}),+ json!({"index": 0, "function": {"arguments": "{\"code\":\"print(1)\"}"}}),+ json!({"index": 1, "function": {"arguments": "\"file.txt\"}"}}),+ ],+ json!([+ {"id": "python123", "name": "ipython", "arguments": {"code": "print(1)"}},+ {"id": "other1234", "name": "edit", "arguments": {"path": "file.txt"}},+ ]),+ ),+ (+ vec![+ json!({"index": 0, "function": {"arguments": "{\"code\":"}}),+ json!({"index": 0, "id": "python123", "function": {"name": "ipython"}}),+ json!({"index": 0, "function": {"arguments": "\"print(1)\"}"}}),+ ],+ json!([{"id": "python123", "name": "ipython", "arguments": {"code": "print(1)"}}]),+ ),+ (+ // A phantom call that never gets a name (#3648) is dropped from the final message.+ vec![+ json!({"index": 0, "id": "python123", "function": {"name": "ipython", "arguments": "{\"code\":\"print(1)\"}"}}),+ json!({"index": 1, "function": {"arguments": "{\"code\":\"print(2)\"}"}}),+ ],+ json!([{"id": "python123", "name": "ipython", "arguments": {"code": "print(1)"}}]),+ ),+ ];+ for (deltas, expected) in cases {+ let result = stream_tool_call_deltas(deltas, "tool_calls").await;+ assert_eq!(+ result.stop_reason,+ StopReason::ToolUse,+ "{:?}",+ result.error_message+ );+ let actual: Vec<Value> = result+ .content+ .iter()+ .map(|content| {+ let crate::types::AssistantContent::ToolCall(call) = content else {+ panic!("expected only tool calls, got {content:?}");+ };+ json!({"id": call.id, "name": call.name, "arguments": call.arguments})+ })+ .collect();+ assert_eq!(json!(actual), expected);+ }+}++/// An error stop settles partial tool-call arguments by streamed content index; dropping a+/// nameless call first would shift the named call into its slot and overwrite its arguments with+/// the nameless call's buffered text.+#[tokio::test]+async fn an_error_stop_keeps_the_named_call_arguments_beside_a_dropped_nameless_call() {+ // A large first delta, then a small one: the accumulator throttles the reparse, so the error+ // settle has unflushed text to write back.+ let large = "x".repeat(16 * 1024);+ let deltas = vec![+ json!({"index": 0, "function": {"arguments": format!("{{\"code\":\"{large}")}}),+ json!({"index": 0, "function": {"arguments": "y\"}"}}),+ json!({"index": 1, "id": "python123", "function": {"name": "ipython", "arguments": "{\"code\":\"print(1)\"}"}}),+ ];+ let result = stream_tool_call_deltas(deltas, "error").await;+ assert_eq!(result.stop_reason, StopReason::Error);+ let actual: Vec<Value> = result+ .content+ .iter()+ .map(|content| {+ let crate::types::AssistantContent::ToolCall(call) = content else {+ panic!("expected only tool calls, got {content:?}");+ };+ json!({"id": call.id, "name": call.name, "arguments": call.arguments})+ })+ .collect();+ assert_eq!(+ json!(actual),+ json!([{"id": "python123", "name": "ipython", "arguments": {"code": "print(1)"}}])+ );+}diff --git a/crates/pa-ai/src/providers/openai_completions/stream.rs b/crates/pa-ai/src/providers/openai_completions/stream.rsindex 8aa3d5bb17..4a10793d70 100644--- a/crates/pa-ai/src/providers/openai_completions/stream.rs+++ b/crates/pa-ai/src/providers/openai_completions/stream.rs@@ -20,6 +20,7 @@ use crate::providers::openai_completions::{REASONING_FIELDS,};use crate::providers::openai_responses_hooks::apply_service_tier_pricing;+use crate::providers::transform_messages::repair_unnamed_tool_calls;use crate::types::{done_reason, error_reason, AssistantContent, AssistantMessage, CacheRetention, Context, Model,StopReason, TextContent, ThinkingContent, ToolCall, Usage,@@ -304,7 +305,11 @@ fn handle_chunk(if let Some(tool_calls) = delta.get("tool_calls").and_then(|value| value.as_array()) {for tool_call in tool_calls {let stream_index = tool_call.get("index").and_then(serde_json::Value::as_u64);- let id = tool_call.get("id").and_then(|value| value.as_str());+ // An empty id counts as absent, as TS's truthiness check treats it.+ let id = tool_call+ .get("id")+ .and_then(|value| value.as_str())+ .filter(|id| !id.is_empty());let index = state.ensure_tool_call_block(stream_index, id, writer);if let Some(AssistantContent::ToolCall(block)) = state.output.content.get_mut(index) {if block.id.is_empty() {@@ -640,6 +645,7 @@ async fn run_stream(// calls before the error event carries the message.encode_reasoning_details_signature(&mut state);settle_partial_tool_calls(&mut state);+ repair_unnamed_tool_calls(&mut state.output);*output = state.output;return Err(error);}@@ -681,6 +687,9 @@ async fn run_stream(encode_reasoning_details_signature(&mut state);finish_blocks(&mut state, writer);+ // After the `*_end` events, so the content indices already streamed to+ // the client stay valid; the final message is what the session stores.+ repair_unnamed_tool_calls(&mut state.output);*output = state.output;if base_options@@ -776,6 +785,11 @@ mod tests {#[path = "stream_wire.rs"]mod stream_wire;+ // The nameless-tool-call pins (#3742): calls the provider streamed+ // without a function name.+ #[path = "stream_tool_call_name.rs"]+ mod stream_tool_call_name;+/// Serve one SSE response body for the provider's POST and return the/// bound address.async fn serve_sse(body: String) -> SocketAddr {diff --git a/crates/pa-ai/src/providers/openai_completions/stream/tests/stream_tool_call_name.rs b/crates/pa-ai/src/providers/openai_completions/stream/tests/stream_tool_call_name.rsnew file mode 100644index 0000000000..fc3abd9e7a--- /dev/null+++ b/crates/pa-ai/src/providers/openai_completions/stream/tests/stream_tool_call_name.rs@@ -0,0 +1,93 @@+//! The nameless-tool-call pins (#3742): a gateway that converts a model's+//! native tool-call template can emit a `tool_calls` entry with no+//! `function.name` (and often no `id`). Stored and replayed, that call makes+//! strict providers reject every later request, so the stream must never let+//! a nameless call into the final message.++use super::*;+use crate::providers::transform_messages::UNNAMED_TOOL_CALL;++/// One chat-completions chunk carrying `delta`.+fn chunk(delta: &Value) -> String {+ let chunk = json!({+ "id": "c1",+ "object": "chat.completion.chunk",+ "model": "glm-test",+ "choices": [{"index": 0, "delta": delta, "finish_reason": null}],+ });+ format!("data: {chunk}\n\n")+}++/// The stop signal for a tool-call turn.+const TOOLCALL_FINISH: &str = "data: {\"id\":\"c1\",\"object\":\"chat.completion.chunk\",\"model\":\"glm-test\",\"choices\":[{\"index\":0,\"delta\":{},\"finish_reason\":\"tool_calls\"}]}\n\n";++fn tool_call(id: &str, name: &str, arguments: &Value) -> AssistantContent {+ AssistantContent::ToolCall(ToolCall {+ id: id.to_string(),+ name: name.to_string(),+ arguments: arguments.as_object().cloned().unwrap_or_default(),+ thought_signature: None,+ rest: Map::default(),+ })+}++fn text(text: &str) -> AssistantContent {+ AssistantContent::Text(TextContent {+ text: text.to_string(),+ text_signature: None,+ rest: Map::default(),+ })+}++async fn stream(chunks: &[String]) -> AssistantMessage {+ stream_final_message(+ completions_model("glm-test", "zenmux", 0.0, 0.0),+ format!("{}{TOOLCALL_FINISH}{DONE}", chunks.concat()),+ )+ .await+}++#[tokio::test]+async fn a_turn_of_only_nameless_calls_keeps_going_as_unknown_tool() {+ // The reported shape: the model's template leaks as text, and the gateway+ // adds a tool call with neither `function.name` nor `id`. The call is named+ // so the loop answers it with a not-found error and the model can retry.+ let message = stream(&[+ chunk(&json!({"role": "assistant", "content": "probing:"})),+ chunk(&json!({"tool_calls": [{+ "index": 0,+ "function": {"arguments": "{\"python_repl\":\"x\"}"},+ }]})),+ ])+ .await;+ let id = format!("call_unnamed_{}_1", message.timestamp);+ assert_eq!(+ message.content,+ vec![+ text("probing:"),+ tool_call(&id, UNNAMED_TOOL_CALL, &json!({"python_repl": "x"}))+ ]+ );+ assert_eq!(message.stop_reason, StopReason::ToolUse);+}++#[tokio::test]+async fn a_nameless_call_beside_a_named_one_is_dropped_alone() {+ let message = stream(&[+ chunk(&json!({"tool_calls": [{+ "index": 0,+ "id": "call_1",+ "function": {"name": "python_repl", "arguments": "{\"code\":\"x\"}"},+ }]})),+ chunk(&json!({"tool_calls": [{+ "index": 1,+ "function": {"arguments": "{\"code\":\"y\"}"},+ }]})),+ ])+ .await;+ assert_eq!(+ message.content,+ vec![tool_call("call_1", "python_repl", &json!({"code": "x"}))]+ );+ assert_eq!(message.stop_reason, StopReason::ToolUse);+}diff --git a/crates/pa-ai/src/providers/transform_messages.rs b/crates/pa-ai/src/providers/transform_messages.rsindex 78ab8e14fb..b3b7e0509e 100644--- a/crates/pa-ai/src/providers/transform_messages.rs+++ b/crates/pa-ai/src/providers/transform_messages.rs@@ -69,10 +69,52 @@ fn downgrade_unsupported_images(messages: &[Message], model: &Model) -> Vec<Mess.collect()}+/// A tool call without a function name: some gateways synthesize a+/// `tool_calls` entry from a model's native tool-call template and lose the+/// name (often the id too). Nothing can execute it, and strict providers+/// reject every request that replays it (`tool_calls[N] is missing a+/// function name`), which would leave the session failing on every turn.+fn is_unnamed_tool_call(tool_call: &ToolCall) -> bool {+ tool_call.name.trim().is_empty()+}++/// Name given to the calls of a turn whose every tool call came without a function name. Strict+/// providers accept it on replay, and the agent loop answers it with a `Tool unknown_tool not found`+/// error, so the model learns its call was malformed and can retry in the same turn.+pub(crate) const UNNAMED_TOOL_CALL: &str = "unknown_tool";++/// Repair the tool calls a provider streamed without a function name in a finished assistant+/// message, before it reaches the agent loop or the session file. A nameless call beside named+/// calls is dropped: the turn goes on through the named ones. When a tool-use turn has only+/// nameless calls, they are named [`UNNAMED_TOOL_CALL`] and given a stable id instead, so the turn+/// keeps going with an error result rather than ending silently.+pub(crate) fn repair_unnamed_tool_calls(output: &mut AssistantMessage) {+ let has_named_call = output.content.iter().any(+ |block| matches!(block, AssistantContent::ToolCall(tool_call) if !is_unnamed_tool_call(tool_call)),+ );+ if !has_named_call && output.stop_reason == StopReason::ToolUse {+ let timestamp = output.timestamp;+ for (index, block) in output.content.iter_mut().enumerate() {+ if let AssistantContent::ToolCall(tool_call) = block {+ tool_call.name = UNNAMED_TOOL_CALL.to_string();+ if tool_call.id.is_empty() {+ tool_call.id = format!("call_unnamed_{timestamp}_{index}");+ }+ }+ }+ return;+ }+ output.content.retain(+ |block| !matches!(block, AssistantContent::ToolCall(tool_call) if is_unnamed_tool_call(tool_call)),+ );+}+type Normalizer<'a> = dyn Fn(&str, &Model, &AssistantMessage) -> Option<String> + 'a;/// Tool-call ID normalization plus synthetic tool results for unanswered calls, dropping errored-/// assistant turns (TS `transformMessages`).+/// assistant turns (TS `transformMessages`). Unnamed tool calls already stored in a session (written+/// before the stream dropped them) are stripped here, and their results go with them as orphans, so+/// a session poisoned by one replays again.// Long by design: mirrors the provider's stream shape.#[allow(clippy::too_many_lines)]pub fn transform_messages_with_normalizer(@@ -83,20 +125,22 @@ pub fn transform_messages_with_normalizer(let mut tool_call_id_map: HashMap<String, String> = HashMap::new();let image_aware = downgrade_unsupported_images(messages, model);- let transformed: Vec<Message> = image_aware+ // `None` marks a dropped unnamed-only assistant turn: it still ends the previous turn's pending+ // calls, so its own results cannot be replayed against an earlier call that reused the id.+ let transformed: Vec<Option<Message>> = image_aware.iter().map(|msg| match msg {- Message::User(_) => msg.clone(),+ Message::User(_) => Some(msg.clone()),Message::ToolResult(tool_result) => {if let Some(normalized) = tool_call_id_map.get(&tool_result.tool_call_id) {if normalized != &tool_result.tool_call_id {- return Message::ToolResult(ToolResultMessage {+ return Some(Message::ToolResult(ToolResultMessage {tool_call_id: normalized.clone(),..tool_result.clone()- });+ }));}}- msg.clone()+ Some(msg.clone())}Message::Assistant(assistant) => {let is_same_model = assistant.provider == model.provider@@ -132,6 +176,11 @@ pub fn transform_messages_with_normalizer(})]}AssistantContent::Text(_) => vec![block.clone()],+ AssistantContent::ToolCall(tool_call)+ if is_unnamed_tool_call(tool_call) =>+ {+ vec![]+ }AssistantContent::ToolCall(tool_call) => {let mut normalized: ToolCall = tool_call.clone();if !is_same_model && tool_call.thought_signature.is_some() {@@ -153,10 +202,18 @@ pub fn transform_messages_with_normalizer(}).collect();- Message::Assistant(crate::types::AssistantMessage {+ // A turn that held nothing but unnamed tool calls has nothing left to replay.+ if transformed_content.is_empty()+ && assistant.content.iter().any(|block| {+ matches!(block, AssistantContent::ToolCall(tool_call) if is_unnamed_tool_call(tool_call))+ })+ {+ return None;+ }+ Some(Message::Assistant(crate::types::AssistantMessage {content: transformed_content,..assistant.clone()- })+ }))}}).collect();@@ -194,6 +251,14 @@ pub fn transform_messages_with_normalizer(};for msg in transformed {+ let Some(msg) = msg else {+ insert_synthetic_tool_results(+ &mut result,+ &mut pending_tool_calls,+ &mut existing_tool_result_ids,+ );+ continue;+ };match &msg {Message::Assistant(assistant) => {insert_synthetic_tool_results(@@ -374,4 +439,108 @@ mod tests {transform_messages_with_normalizer(&messages, &test_model(), &|_, _, _| None);assert_eq!(transformed.len(), 2);}++ fn tool_call(id: &str, name: &str) -> AssistantContent {+ AssistantContent::ToolCall(ToolCall {+ id: id.to_string(),+ name: name.to_string(),+ arguments: Map::default(),+ thought_signature: None,+ rest: Map::default(),+ })+ }++ fn text(text: &str) -> AssistantContent {+ AssistantContent::Text(TextContent {+ text: text.to_string(),+ text_signature: None,+ rest: Map::default(),+ })+ }++ fn assistant(content: Vec<AssistantContent>, stop_reason: StopReason) -> AssistantMessage {+ let Message::Assistant(assistant) = assistant_with_tool_call("unused") else {+ unreachable!()+ };+ AssistantMessage {+ content,+ stop_reason,+ ..assistant+ }+ }++ fn tool_result(id: &str, name: &str, text: &str) -> Message {+ Message::ToolResult(ToolResultMessage {+ tool_call_id: id.into(),+ tool_name: name.into(),+ content: vec![UserOrToolContent::Text(TextContent {+ text: text.into(),+ text_signature: None,+ rest: Map::default(),+ })],+ details: None,+ is_error: true,+ timestamp: 0,+ rest: Map::default(),+ })+ }++ #[test]+ fn a_session_poisoned_by_an_unnamed_tool_call_replays_without_it() {+ // The #3742 transcript: the gateway left the call without a name or id, the+ // loop answered `Tool not found`, and both rows were persisted.+ let named = tool_call("call-1", "bash");+ let messages = vec![+ user("hi"),+ Message::Assistant(assistant(+ vec![text("probing"), tool_call("", "")],+ StopReason::ToolUse,+ )),+ tool_result("", "", "Tool not found"),+ Message::Assistant(assistant(vec![tool_call("", "")], StopReason::ToolUse)),+ tool_result("", "", "Tool not found"),+ Message::Assistant(assistant(+ vec![named.clone(), tool_call("call-2", " ")],+ StopReason::ToolUse,+ )),+ tool_result("call-1", "bash", "ok"),+ tool_result("call-2", " ", "Tool not found"),+ user("again"),+ ];+ let transformed =+ transform_messages_with_normalizer(&messages, &test_model(), &|_, _, _| None);+ let expected = vec![+ user("hi"),+ Message::Assistant(assistant(vec![text("probing")], StopReason::ToolUse)),+ Message::Assistant(assistant(vec![named], StopReason::ToolUse)),+ tool_result("call-1", "bash", "ok"),+ user("again"),+ ];+ assert_eq!(transformed, expected);+ }++ #[test]+ fn a_dropped_unnamed_turn_does_not_answer_an_earlier_call_with_the_same_id() {+ let named = tool_call("call-1", "bash");+ let messages = vec![+ user("hi"),+ Message::Assistant(assistant(vec![named.clone()], StopReason::ToolUse)),+ tool_result("call-1", "bash", "ok"),+ Message::Assistant(assistant(+ vec![tool_call("call-1", "")],+ StopReason::ToolUse,+ )),+ tool_result("call-1", "", "Tool not found"),+ user("again"),+ ];+ let transformed =+ transform_messages_with_normalizer(&messages, &test_model(), &|_, _, _| None);+ let expected = vec![+ user("hi"),+ Message::Assistant(assistant(vec![named], StopReason::ToolUse)),+ tool_result("call-1", "bash", "ok"),+ user("again"),+ ];+ assert_eq!(transformed, expected);+ }}