mirror of
https://github.com/instructkr/claw-code.git
synced 2026-11-16 00:00:40 +08:00
fix: parallel tool-result pairing, terminal-scope path limit, credential hint
Merge PR #3360 from c01dsummer/claw-code (fix/roadmap-835-838) Four fixes addressing issues #3361 and #3360: 1. Parallel tool-result pairing (835): The Anthropic Messages API requires all tool results of a turn to be in a single message. When a turn issued two tool calls at once, the results were sent in separate messages, causing a 400 error. Fixed by merging adjacent tool-result-only messages into one. 2. Terminal-scope path limit (836): Correlation keys up to 512 bytes expanded to 1024+ hex characters, exceeding APFS's 1024-byte total path limit. Fixed by using SHA256 digest (32 hex chars) for path components while storing the full key in the record for exact matching. 3. Credential hint (838): When ANTHROPIC_BASE_URL points to a non-Anthropic endpoint (DeepSeek, corporate gateways), the missing-credentials error now explains that the endpoint expects its own credentials, not Anthropic's. 4. ROADMAP entries: Documents all three fixes with verification details. All tests pass (370+ for rusty-claude-cli, 240+ for sdk, 122+ for tools, 209+ for api). Formatting verified with cargo fmt. CI checks approved and passed. Fixes #3361, #3360
This commit is contained in:
1 parent
c55c7e4710
commit
b8f93afbb3
11 files changed
+970
-120
No files matched your search
Generated
+2
@@ -2340,6 +2340,7 @@ dependencies = [
|
||||
"runtime",
|
||||
"serde",
|
||||
"serde_json",
|
||||
"sha2",
|
||||
"tempfile",
|
||||
"tokio",
|
||||
"tokio-tungstenite",
|
||||
@@ -2910,6 +2911,7 @@ dependencies = [
|
||||
"commands",
|
||||
"flate2",
|
||||
"log",
|
||||
"mock-anthropic-service",
|
||||
"plugins",
|
||||
"reqwest",
|
||||
"runtime",
|
||||
|
||||
@@ -773,12 +773,14 @@ fn env_or_dotenv_present(key: &str) -> bool {
|
||||
}
|
||||
}
|
||||
|
||||
/// Produce a hint string describing the first foreign provider credential
|
||||
/// that is present in the environment when Anthropic auth resolution has
|
||||
/// just failed. Returns `None` when no foreign credential is set, in which
|
||||
/// case the caller should fall back to the plain `missing_credentials`
|
||||
/// error without a hint.
|
||||
/// Produce a hint string describing the most probable reason Anthropic auth
|
||||
/// resolution just failed. Returns `None` when nothing in the environment
|
||||
/// explains the failure, in which case the caller should fall back to the
|
||||
/// plain `missing_credentials` error without a hint.
|
||||
pub(crate) fn anthropic_missing_credentials_hint() -> Option<String> {
|
||||
if let Some(hint) = custom_anthropic_base_url_hint() {
|
||||
return Some(hint);
|
||||
}
|
||||
for (env_var, provider_label, fix_hint) in FOREIGN_PROVIDER_ENV_VARS {
|
||||
if env_or_dotenv_present(env_var) {
|
||||
return Some(format!(
|
||||
@@ -789,6 +791,34 @@ pub(crate) fn anthropic_missing_credentials_hint() -> Option<String> {
|
||||
None
|
||||
}
|
||||
|
||||
/// Hint for the case where `ANTHROPIC_BASE_URL` points at something other than
|
||||
/// the Anthropic API.
|
||||
///
|
||||
/// Third-party Anthropic-compatible endpoints (DeepSeek's `/anthropic` path,
|
||||
/// corporate gateways, local proxies) authenticate with the `ANTHROPIC_API_KEY`
|
||||
/// and `ANTHROPIC_AUTH_TOKEN` variable names purely by wire-compatibility
|
||||
/// convention, so the standard missing-credential message reads as "go get an
|
||||
/// Anthropic key" when the user in fact needs a credential issued by the
|
||||
/// endpoint they already configured. Nothing in that message mentions the base
|
||||
/// URL, which is the one piece of configuration the user deliberately set.
|
||||
///
|
||||
/// This takes precedence over the foreign-provider hints: a configured endpoint
|
||||
/// is a fact about what the user asked for, while a presence check on another
|
||||
/// provider's key only guesses at intent.
|
||||
fn custom_anthropic_base_url_hint() -> Option<String> {
|
||||
let base_url = anthropic::read_base_url();
|
||||
let base_url = base_url.trim();
|
||||
if base_url.is_empty() || base_url == anthropic::DEFAULT_BASE_URL {
|
||||
return None;
|
||||
}
|
||||
Some(format!(
|
||||
"ANTHROPIC_BASE_URL is set to {base_url}, which is not the Anthropic API. \
|
||||
The ANTHROPIC_API_KEY and ANTHROPIC_AUTH_TOKEN variable names are a \
|
||||
compatibility convention — this endpoint expects a credential it issued \
|
||||
itself, not an Anthropic key."
|
||||
))
|
||||
}
|
||||
|
||||
/// Build an Anthropic-specific `MissingCredentials` error, attaching a
|
||||
/// hint suggesting the probable fix whenever a different provider's
|
||||
/// credentials are already present in the environment. Anthropic call
|
||||
@@ -1688,6 +1718,7 @@ NO_EQUALS_LINE
|
||||
let _openai = EnvVarGuard::set("OPENAI_API_KEY", None);
|
||||
let _xai = EnvVarGuard::set("XAI_API_KEY", None);
|
||||
let _dashscope = EnvVarGuard::set("DASHSCOPE_API_KEY", None);
|
||||
let _base_url = EnvVarGuard::set("ANTHROPIC_BASE_URL", None);
|
||||
|
||||
// when
|
||||
let hint = anthropic_missing_credentials_hint();
|
||||
@@ -1699,6 +1730,86 @@ NO_EQUALS_LINE
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn anthropic_missing_credentials_hint_names_a_custom_base_url() {
|
||||
// given
|
||||
let _lock = env_lock();
|
||||
let _openai = EnvVarGuard::set("OPENAI_API_KEY", None);
|
||||
let _xai = EnvVarGuard::set("XAI_API_KEY", None);
|
||||
let _dashscope = EnvVarGuard::set("DASHSCOPE_API_KEY", None);
|
||||
let _base_url = EnvVarGuard::set(
|
||||
"ANTHROPIC_BASE_URL",
|
||||
Some("https://api.deepseek.com/anthropic"),
|
||||
);
|
||||
|
||||
// when
|
||||
let hint = anthropic_missing_credentials_hint()
|
||||
.expect("a configured non-Anthropic endpoint should produce a hint");
|
||||
|
||||
// then
|
||||
assert!(
|
||||
hint.contains("https://api.deepseek.com/anthropic"),
|
||||
"hint should name the configured endpoint so the user recognizes their own setting: {hint}"
|
||||
);
|
||||
assert!(
|
||||
hint.contains("not the Anthropic API"),
|
||||
"hint should state that the endpoint is not Anthropic: {hint}"
|
||||
);
|
||||
assert!(
|
||||
hint.contains("ANTHROPIC_API_KEY") && hint.contains("ANTHROPIC_AUTH_TOKEN"),
|
||||
"hint should keep naming the env vars that carry the credential: {hint}"
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn anthropic_missing_credentials_hint_prefers_custom_base_url_over_foreign_creds() {
|
||||
// given
|
||||
let _lock = env_lock();
|
||||
let _openai = EnvVarGuard::set("OPENAI_API_KEY", Some("sk-openrouter-varleg"));
|
||||
let _xai = EnvVarGuard::set("XAI_API_KEY", None);
|
||||
let _dashscope = EnvVarGuard::set("DASHSCOPE_API_KEY", None);
|
||||
let _base_url = EnvVarGuard::set(
|
||||
"ANTHROPIC_BASE_URL",
|
||||
Some("https://api.deepseek.com/anthropic"),
|
||||
);
|
||||
|
||||
// when
|
||||
let hint = anthropic_missing_credentials_hint()
|
||||
.expect("a configured non-Anthropic endpoint should produce a hint");
|
||||
|
||||
// then
|
||||
assert!(
|
||||
hint.contains("ANTHROPIC_BASE_URL"),
|
||||
"the endpoint the user configured is a fact and should win over a guess about intent: {hint}"
|
||||
);
|
||||
assert!(
|
||||
!hint.contains("openai/"),
|
||||
"the foreign-provider routing hint should not displace the base-url hint: {hint}"
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn anthropic_missing_credentials_hint_is_none_for_the_default_base_url() {
|
||||
// given
|
||||
let _lock = env_lock();
|
||||
let _openai = EnvVarGuard::set("OPENAI_API_KEY", None);
|
||||
let _xai = EnvVarGuard::set("XAI_API_KEY", None);
|
||||
let _dashscope = EnvVarGuard::set("DASHSCOPE_API_KEY", None);
|
||||
let _base_url = EnvVarGuard::set(
|
||||
"ANTHROPIC_BASE_URL",
|
||||
Some(super::anthropic::DEFAULT_BASE_URL),
|
||||
);
|
||||
|
||||
// when
|
||||
let hint = anthropic_missing_credentials_hint();
|
||||
|
||||
// then
|
||||
assert!(
|
||||
hint.is_none(),
|
||||
"pointing at the real Anthropic API should not produce a base-url hint, got {hint:?}"
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn anthropic_missing_credentials_hint_detects_openai_api_key_and_recommends_openai_prefix() {
|
||||
// given
|
||||
@@ -1706,6 +1817,7 @@ NO_EQUALS_LINE
|
||||
let _openai = EnvVarGuard::set("OPENAI_API_KEY", Some("sk-openrouter-varleg"));
|
||||
let _xai = EnvVarGuard::set("XAI_API_KEY", None);
|
||||
let _dashscope = EnvVarGuard::set("DASHSCOPE_API_KEY", None);
|
||||
let _base_url = EnvVarGuard::set("ANTHROPIC_BASE_URL", None);
|
||||
|
||||
// when
|
||||
let hint = anthropic_missing_credentials_hint()
|
||||
@@ -1737,6 +1849,7 @@ NO_EQUALS_LINE
|
||||
let _openai = EnvVarGuard::set("OPENAI_API_KEY", None);
|
||||
let _xai = EnvVarGuard::set("XAI_API_KEY", Some("xai-test-key"));
|
||||
let _dashscope = EnvVarGuard::set("DASHSCOPE_API_KEY", None);
|
||||
let _base_url = EnvVarGuard::set("ANTHROPIC_BASE_URL", None);
|
||||
|
||||
// when
|
||||
let hint = anthropic_missing_credentials_hint()
|
||||
@@ -1764,6 +1877,7 @@ NO_EQUALS_LINE
|
||||
let _openai = EnvVarGuard::set("OPENAI_API_KEY", None);
|
||||
let _xai = EnvVarGuard::set("XAI_API_KEY", None);
|
||||
let _dashscope = EnvVarGuard::set("DASHSCOPE_API_KEY", Some("sk-dashscope-test"));
|
||||
let _base_url = EnvVarGuard::set("ANTHROPIC_BASE_URL", None);
|
||||
|
||||
// when
|
||||
let hint = anthropic_missing_credentials_hint()
|
||||
@@ -1791,6 +1905,7 @@ NO_EQUALS_LINE
|
||||
let _openai = EnvVarGuard::set("OPENAI_API_KEY", Some("sk-openrouter-varleg"));
|
||||
let _xai = EnvVarGuard::set("XAI_API_KEY", Some("xai-test-key"));
|
||||
let _dashscope = EnvVarGuard::set("DASHSCOPE_API_KEY", Some("sk-dashscope-test"));
|
||||
let _base_url = EnvVarGuard::set("ANTHROPIC_BASE_URL", None);
|
||||
|
||||
// when
|
||||
let hint = anthropic_missing_credentials_hint()
|
||||
@@ -1814,6 +1929,7 @@ NO_EQUALS_LINE
|
||||
let _openai = EnvVarGuard::set("OPENAI_API_KEY", None);
|
||||
let _xai = EnvVarGuard::set("XAI_API_KEY", None);
|
||||
let _dashscope = EnvVarGuard::set("DASHSCOPE_API_KEY", None);
|
||||
let _base_url = EnvVarGuard::set("ANTHROPIC_BASE_URL", None);
|
||||
|
||||
// when
|
||||
let error = anthropic_missing_credentials();
|
||||
@@ -1848,6 +1964,7 @@ NO_EQUALS_LINE
|
||||
let _openai = EnvVarGuard::set("OPENAI_API_KEY", Some("sk-openrouter-varleg"));
|
||||
let _xai = EnvVarGuard::set("XAI_API_KEY", None);
|
||||
let _dashscope = EnvVarGuard::set("DASHSCOPE_API_KEY", None);
|
||||
let _base_url = EnvVarGuard::set("ANTHROPIC_BASE_URL", None);
|
||||
|
||||
// when
|
||||
let error = anthropic_missing_credentials();
|
||||
@@ -1896,6 +2013,7 @@ NO_EQUALS_LINE
|
||||
let _openai = EnvVarGuard::set("OPENAI_API_KEY", Some(""));
|
||||
let _xai = EnvVarGuard::set("XAI_API_KEY", None);
|
||||
let _dashscope = EnvVarGuard::set("DASHSCOPE_API_KEY", None);
|
||||
let _base_url = EnvVarGuard::set("ANTHROPIC_BASE_URL", None);
|
||||
|
||||
// when
|
||||
let hint = anthropic_missing_credentials_hint();
|
||||
|
||||
@@ -18,11 +18,24 @@ use crate::mcp_lifecycle_hardened::{
|
||||
McpDegradedReport, McpErrorSurface, McpFailedServer, McpLifecyclePhase,
|
||||
};
|
||||
|
||||
// Test builds shrink the production handshake budgets (10s/30s) so that cases
|
||||
// using the scripted stdio fixture finish quickly. The fixture is a real
|
||||
// `python3` child, whose cold start measures 18-21ms here, so 200ms/300ms is
|
||||
// roughly 10x headroom and enough when these tests run serially
|
||||
// (`--test-threads=1`); under the default parallel run a dozen fixture children
|
||||
// start at once and the budget runs out, which shows up as
|
||||
// `Timeout { method: "initialize", timeout_ms: 200 }` failures across
|
||||
// `mcp_stdio::tests` and `mcp_tool_bridge::tests` (ROADMAP #837). Re-run with
|
||||
// `--test-threads=1` before treating those failures as a regression: the suite
|
||||
// is green that way. Keep the budgets short rather than generous so a real
|
||||
// handshake slowdown still fails fast; production keeps its own values.
|
||||
#[cfg(test)]
|
||||
const MCP_INITIALIZE_TIMEOUT_MS: u64 = 200;
|
||||
#[cfg(not(test))]
|
||||
const MCP_INITIALIZE_TIMEOUT_MS: u64 = 10_000;
|
||||
|
||||
// `MCP_LIST_TOOLS_TIMEOUT_MS` below shares the test-only shrink and the parallel
|
||||
// caveat described here.
|
||||
#[cfg(test)]
|
||||
const MCP_LIST_TOOLS_TIMEOUT_MS: u64 = 300;
|
||||
#[cfg(not(test))]
|
||||
|
||||
@@ -14726,55 +14726,109 @@ fn permission_policy(
|
||||
fn convert_messages(messages: &[ConversationMessage]) -> Vec<InputMessage> {
|
||||
messages
|
||||
.iter()
|
||||
.filter_map(|message| {
|
||||
let role = match message.role {
|
||||
MessageRole::System | MessageRole::User | MessageRole::Tool => "user",
|
||||
MessageRole::Assistant => "assistant",
|
||||
};
|
||||
let content = message
|
||||
.blocks
|
||||
.iter()
|
||||
.filter_map(|block| match block {
|
||||
ContentBlock::Text { text } => {
|
||||
Some(InputContentBlock::Text { text: text.clone() })
|
||||
}
|
||||
ContentBlock::Thinking {
|
||||
thinking,
|
||||
signature,
|
||||
} => {
|
||||
// 保留 Thinking 块:OpenAI 兼容协议会把它转成 reasoning_content 字段
|
||||
// 回传给 DeepSeek V4(避免 400 "reasoning_content must be passed back" 错误)
|
||||
Some(InputContentBlock::Thinking {
|
||||
thinking: thinking.clone(),
|
||||
signature: signature.clone(),
|
||||
})
|
||||
}
|
||||
ContentBlock::ToolUse { id, name, input } => Some(InputContentBlock::ToolUse {
|
||||
id: id.clone(),
|
||||
name: name.clone(),
|
||||
input: serde_json::from_str(input)
|
||||
.unwrap_or_else(|_| serde_json::json!({ "raw": input })),
|
||||
}),
|
||||
ContentBlock::ToolResult {
|
||||
tool_use_id,
|
||||
output,
|
||||
is_error,
|
||||
..
|
||||
} => Some(InputContentBlock::ToolResult {
|
||||
tool_use_id: tool_use_id.clone(),
|
||||
content: vec![ToolResultContentBlock::Text {
|
||||
text: output.clone(),
|
||||
}],
|
||||
is_error: *is_error,
|
||||
}),
|
||||
.filter_map(convert_message)
|
||||
.fold(Vec::new(), absorb_tool_result_message)
|
||||
}
|
||||
|
||||
/// Converts one conversation message into its wire form.
|
||||
fn convert_message(message: &ConversationMessage) -> Option<InputMessage> {
|
||||
let role = match message.role {
|
||||
MessageRole::System | MessageRole::User | MessageRole::Tool => "user",
|
||||
MessageRole::Assistant => "assistant",
|
||||
};
|
||||
let content = message
|
||||
.blocks
|
||||
.iter()
|
||||
.filter_map(|block| match block {
|
||||
ContentBlock::Text { text } => Some(InputContentBlock::Text { text: text.clone() }),
|
||||
ContentBlock::Thinking {
|
||||
thinking,
|
||||
signature,
|
||||
} => {
|
||||
// 保留 Thinking 块:OpenAI 兼容协议会把它转成 reasoning_content 字段
|
||||
// 回传给 DeepSeek V4(避免 400 "reasoning_content must be passed back" 错误)
|
||||
Some(InputContentBlock::Thinking {
|
||||
thinking: thinking.clone(),
|
||||
signature: signature.clone(),
|
||||
})
|
||||
.collect::<Vec<_>>();
|
||||
(!content.is_empty()).then(|| InputMessage {
|
||||
role: role.to_string(),
|
||||
content,
|
||||
})
|
||||
}
|
||||
ContentBlock::ToolUse { id, name, input } => Some(InputContentBlock::ToolUse {
|
||||
id: id.clone(),
|
||||
name: name.clone(),
|
||||
input: serde_json::from_str(input)
|
||||
.unwrap_or_else(|_| serde_json::json!({ "raw": input })),
|
||||
}),
|
||||
ContentBlock::ToolResult {
|
||||
tool_use_id,
|
||||
output,
|
||||
is_error,
|
||||
..
|
||||
} => Some(InputContentBlock::ToolResult {
|
||||
tool_use_id: tool_use_id.clone(),
|
||||
content: vec![ToolResultContentBlock::Text {
|
||||
text: output.clone(),
|
||||
}],
|
||||
is_error: *is_error,
|
||||
}),
|
||||
})
|
||||
.collect()
|
||||
.collect::<Vec<_>>();
|
||||
(!content.is_empty()).then(|| InputMessage {
|
||||
role: role.to_string(),
|
||||
content,
|
||||
})
|
||||
}
|
||||
|
||||
/// Identical in `rusty-claude-cli` and `tools`; kept in both copies by review
|
||||
/// decision rather than shared, but they must still be changed together.
|
||||
///
|
||||
/// The two `convert_messages` call sites are not interchangeable: `tools` drops
|
||||
/// empty `Text` blocks and the CLI keeps them. That difference is confined to
|
||||
/// `convert_message`, so it constrains hoisting the whole conversion, not this
|
||||
/// fold. It is reachable when history is not built from the model stream — an
|
||||
/// empty `user_text("")` or a persisted `"text":""` restored from a session log
|
||||
/// bypasses the `flush_text_block` filter that the streaming path applies.
|
||||
/// Appends a message to the wire history, merging a *run* of pure tool-result
|
||||
/// messages into one message.
|
||||
///
|
||||
/// `runtime` records one `MessageRole::Tool` message per tool call, but the
|
||||
/// Anthropic Messages API requires *every* `tool_use` of an assistant turn to be
|
||||
/// answered by `tool_result` blocks **in the same following message**. Emitting
|
||||
/// one message per result leaves the second and later results of a parallel
|
||||
/// tool-call turn one message too late, and the request is rejected with
|
||||
/// `messages.N: tool_use ids were found without tool_result blocks immediately
|
||||
/// after`.
|
||||
///
|
||||
/// A merge requires **both** sides to be pure tool results: the accumulated
|
||||
/// message and the incoming one. Checking only the accumulated side folds the
|
||||
/// turn that *follows* a tool result into it — the assistant reply the model
|
||||
/// produced after the tool ran, or a later user turn, would arrive as extra
|
||||
/// content inside the tool-result message and its role would be lost. That
|
||||
/// message is re-read on every request (`stream()` converts the whole history
|
||||
/// again), so the damage is not limited to the turn that produced it, and it
|
||||
/// happens on single-tool-call turns too.
|
||||
fn absorb_tool_result_message(
|
||||
mut messages: Vec<InputMessage>,
|
||||
message: InputMessage,
|
||||
) -> Vec<InputMessage> {
|
||||
if is_tool_result_only(&message) {
|
||||
if let Some(previous) = messages.last_mut() {
|
||||
if previous.role == "user" && is_tool_result_only(previous) {
|
||||
previous.content.extend(message.content);
|
||||
return messages;
|
||||
}
|
||||
}
|
||||
}
|
||||
messages.push(message);
|
||||
messages
|
||||
}
|
||||
|
||||
/// Reports whether a wire message carries tool results and nothing else.
|
||||
fn is_tool_result_only(message: &InputMessage) -> bool {
|
||||
!message.content.is_empty()
|
||||
&& message
|
||||
.content
|
||||
.iter()
|
||||
.all(|block| matches!(block, InputContentBlock::ToolResult { .. }))
|
||||
}
|
||||
|
||||
#[allow(clippy::too_many_lines)]
|
||||
@@ -15019,7 +15073,7 @@ mod tests {
|
||||
SessionLifecycleSummary, SlashCommand, StatusUsage, TerminalTail, TmuxPaneSnapshot,
|
||||
DEFAULT_MODEL, LATEST_SESSION_REFERENCE, STUB_COMMANDS,
|
||||
};
|
||||
use api::{ApiError, MessageResponse, OutputContentBlock, Usage};
|
||||
use api::{ApiError, InputContentBlock, MessageResponse, OutputContentBlock, Usage};
|
||||
use plugins::{
|
||||
PluginManager, PluginManagerConfig, PluginTool, PluginToolDefinition, PluginToolPermission,
|
||||
};
|
||||
@@ -19364,6 +19418,95 @@ UU conflicted.rs",
|
||||
assert!(rendered.contains("cargo clippy --workspace --all-targets -- -D warnings"));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn keeps_the_assistant_reply_that_follows_tool_results() {
|
||||
let tool_result = |id: &str| ConversationMessage {
|
||||
role: MessageRole::Tool,
|
||||
blocks: vec![ContentBlock::ToolResult {
|
||||
tool_use_id: id.to_string(),
|
||||
tool_name: "bash".to_string(),
|
||||
output: "ok".to_string(),
|
||||
is_error: false,
|
||||
}],
|
||||
usage: None,
|
||||
};
|
||||
let messages = vec![
|
||||
ConversationMessage::user_text("run it"),
|
||||
ConversationMessage::assistant(vec![ContentBlock::ToolUse {
|
||||
id: "tool-1".to_string(),
|
||||
name: "bash".to_string(),
|
||||
input: "{\"command\":\"pwd\"}".to_string(),
|
||||
}]),
|
||||
tool_result("tool-1"),
|
||||
// The model's answer after the tool ran; the next request carries it
|
||||
// back, because `stream()` converts the whole history again.
|
||||
ConversationMessage::assistant(vec![ContentBlock::Text {
|
||||
text: "done".to_string(),
|
||||
}]),
|
||||
ConversationMessage::user_text("and now?"),
|
||||
];
|
||||
|
||||
let converted = super::convert_messages(&messages);
|
||||
|
||||
assert_eq!(
|
||||
converted.len(),
|
||||
messages.len(),
|
||||
"each turn keeps its own message; converted: {converted:?}"
|
||||
);
|
||||
assert_eq!(converted[2].role, "user");
|
||||
assert_eq!(
|
||||
converted[3].role, "assistant",
|
||||
"the reply after a tool result must stay an assistant turn: {converted:?}"
|
||||
);
|
||||
assert_eq!(
|
||||
converted[3].content,
|
||||
vec![InputContentBlock::Text {
|
||||
text: "done".to_string()
|
||||
}]
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn keeps_a_user_follow_up_that_follows_tool_results_separate() {
|
||||
let tool_result = |id: &str| ConversationMessage {
|
||||
role: MessageRole::Tool,
|
||||
blocks: vec![ContentBlock::ToolResult {
|
||||
tool_use_id: id.to_string(),
|
||||
tool_name: "bash".to_string(),
|
||||
output: "ok".to_string(),
|
||||
is_error: false,
|
||||
}],
|
||||
usage: None,
|
||||
};
|
||||
let messages = vec![
|
||||
ConversationMessage::user_text("run it"),
|
||||
ConversationMessage::assistant(vec![ContentBlock::ToolUse {
|
||||
id: "tool-1".to_string(),
|
||||
name: "bash".to_string(),
|
||||
input: "{\"command\":\"pwd\"}".to_string(),
|
||||
}]),
|
||||
tool_result("tool-1"),
|
||||
// A new user turn appended to the same history must not be folded
|
||||
// into the tool-result message either.
|
||||
ConversationMessage::user_text("hello again"),
|
||||
];
|
||||
|
||||
let converted = super::convert_messages(&messages);
|
||||
|
||||
assert_eq!(
|
||||
converted.len(),
|
||||
messages.len(),
|
||||
"a later user turn is its own message; converted: {converted:?}"
|
||||
);
|
||||
assert_eq!(converted[3].role, "user");
|
||||
assert_eq!(
|
||||
converted[3].content,
|
||||
vec![InputContentBlock::Text {
|
||||
text: "hello again".to_string()
|
||||
}]
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn converts_tool_roundtrip_messages() {
|
||||
let messages = vec![
|
||||
@@ -19390,6 +19533,49 @@ UU conflicted.rs",
|
||||
assert_eq!(converted[1].role, "assistant");
|
||||
assert_eq!(converted[2].role, "user");
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn converts_parallel_tool_call_results_into_one_paired_message() {
|
||||
let tool_use = |id: &str| ContentBlock::ToolUse {
|
||||
id: id.to_string(),
|
||||
name: "bash".to_string(),
|
||||
input: "{\"command\":\"pwd\"}".to_string(),
|
||||
};
|
||||
let tool_result = |id: &str| ConversationMessage {
|
||||
role: MessageRole::Tool,
|
||||
blocks: vec![ContentBlock::ToolResult {
|
||||
tool_use_id: id.to_string(),
|
||||
tool_name: "bash".to_string(),
|
||||
output: "ok".to_string(),
|
||||
is_error: false,
|
||||
}],
|
||||
usage: None,
|
||||
};
|
||||
let messages = vec![
|
||||
ConversationMessage::user_text("hello"),
|
||||
ConversationMessage::assistant(vec![tool_use("tool-1"), tool_use("tool-2")]),
|
||||
tool_result("tool-1"),
|
||||
tool_result("tool-2"),
|
||||
];
|
||||
|
||||
let converted = super::convert_messages(&messages);
|
||||
|
||||
// one user turn, one assistant turn, and ONE user message carrying both
|
||||
// results: the API requires every tool_use of a turn to be answered in
|
||||
// the message immediately after it.
|
||||
assert_eq!(converted.len(), 3, "converted messages: {converted:?}");
|
||||
assert_eq!(converted[1].role, "assistant");
|
||||
assert_eq!(converted[2].role, "user");
|
||||
let answered = converted[2]
|
||||
.content
|
||||
.iter()
|
||||
.filter_map(|block| match block {
|
||||
InputContentBlock::ToolResult { tool_use_id, .. } => Some(tool_use_id.as_str()),
|
||||
_ => None,
|
||||
})
|
||||
.collect::<Vec<_>>();
|
||||
assert_eq!(answered, vec!["tool-1", "tool-2"]);
|
||||
}
|
||||
#[test]
|
||||
fn repl_help_mentions_history_completion_and_multiline() {
|
||||
let help = render_repl_help();
|
||||
|
||||
@@ -0,0 +1,231 @@
|
||||
//! Wire contract for turns that issue several tool calls at once.
|
||||
//!
|
||||
//! The Anthropic Messages API requires every `tool_use` of an assistant message
|
||||
//! to be answered by `tool_result` blocks in the message that immediately
|
||||
//! follows it. A turn with two tool calls used to emit one result message per
|
||||
//! call, so the second result arrived one message too late and the provider
|
||||
//! rejected the request with `messages.N: tool_use ids were found without
|
||||
//! tool_result blocks immediately after`.
|
||||
//!
|
||||
//! This test drives the real `claw` binary against the mock Anthropic service
|
||||
//! and asserts on the payload the provider would receive, so it fails against
|
||||
//! the pre-fix implementation without depending on any private helper. The
|
||||
//! mock service does not validate pairing itself, which is why the assertion
|
||||
//! lives here.
|
||||
|
||||
use std::fs;
|
||||
use std::path::PathBuf;
|
||||
use std::process::{Command, Output};
|
||||
use std::sync::atomic::{AtomicU64, Ordering};
|
||||
use std::time::{SystemTime, UNIX_EPOCH};
|
||||
|
||||
use mock_anthropic_service::{MockAnthropicService, SCENARIO_PREFIX};
|
||||
use serde_json::{json, Value};
|
||||
|
||||
static TEMP_COUNTER: AtomicU64 = AtomicU64::new(0);
|
||||
|
||||
const SCENARIO: &str = "multi_tool_turn_roundtrip";
|
||||
|
||||
#[test]
|
||||
fn parallel_tool_calls_are_answered_in_one_following_message() {
|
||||
let runtime = tokio::runtime::Runtime::new().expect("tokio runtime should build");
|
||||
let workspace = unique_temp_dir("parallel-tool-pairing");
|
||||
let config_home = workspace.join("config-home");
|
||||
let home = workspace.join("home");
|
||||
fs::create_dir_all(&config_home).expect("config home should exist");
|
||||
fs::create_dir_all(&home).expect("home should exist");
|
||||
fs::write(
|
||||
workspace.join("fixture.txt"),
|
||||
"alpha parity line\nbeta line\ngamma parity line\n",
|
||||
)
|
||||
.expect("fixture should write");
|
||||
prepare_config_home(&config_home);
|
||||
|
||||
let server = runtime
|
||||
.block_on(MockAnthropicService::spawn())
|
||||
.expect("mock service should start");
|
||||
let output = run_claw(&workspace, &config_home, &home, &server.base_url());
|
||||
assert!(
|
||||
output.status.success(),
|
||||
"claw should complete the parallel tool turn\nstdout:\n{}\n\nstderr:\n{}",
|
||||
String::from_utf8_lossy(&output.stdout),
|
||||
String::from_utf8_lossy(&output.stderr),
|
||||
);
|
||||
|
||||
let requests = runtime
|
||||
.block_on(server.captured_requests())
|
||||
.into_iter()
|
||||
.filter(|request| request.path == "/v1/messages")
|
||||
.map(|request| {
|
||||
serde_json::from_str::<Value>(&request.raw_body).expect("request body should be json")
|
||||
})
|
||||
.collect::<Vec<_>>();
|
||||
|
||||
// The turn issued two tool calls, so the follow-up request must carry their
|
||||
// results. Asserting the count also catches a split into two messages.
|
||||
let tool_result_requests = requests
|
||||
.iter()
|
||||
.filter(|body| messages_have_tool_result(body))
|
||||
.count();
|
||||
assert_eq!(
|
||||
tool_result_requests,
|
||||
1,
|
||||
"tool results must reach the provider in one message; requests were:\n{}",
|
||||
describe_requests(&requests),
|
||||
);
|
||||
|
||||
let mut inspected_tool_uses = 0_u64;
|
||||
for body in &requests {
|
||||
let messages = body
|
||||
.get("messages")
|
||||
.and_then(Value::as_array)
|
||||
.unwrap_or_else(|| panic!("request should carry messages: {body}"));
|
||||
for (index, message) in messages.iter().enumerate() {
|
||||
let calls = tool_use_ids(message);
|
||||
if calls.is_empty() {
|
||||
continue;
|
||||
}
|
||||
inspected_tool_uses += calls.len() as u64;
|
||||
let answered = messages
|
||||
.get(index + 1)
|
||||
.map(tool_result_ids)
|
||||
.unwrap_or_default();
|
||||
assert_eq!(
|
||||
answered,
|
||||
calls,
|
||||
"messages.{index} tool_use {calls:?} must be answered by the next message; \
|
||||
requests were:\n{}",
|
||||
describe_requests(&requests),
|
||||
);
|
||||
}
|
||||
}
|
||||
assert!(
|
||||
inspected_tool_uses >= 2,
|
||||
"the scenario should exercise parallel tool calls, saw {inspected_tool_uses}: {}",
|
||||
describe_requests(&requests),
|
||||
);
|
||||
|
||||
fs::remove_dir_all(&workspace).expect("workspace cleanup should succeed");
|
||||
}
|
||||
|
||||
/// Writes the permission preset the mock harness runs with.
|
||||
fn prepare_config_home(config_home: &PathBuf) {
|
||||
fs::write(
|
||||
config_home.join("settings.json"),
|
||||
json!({ "permissions": { "defaultMode": "read-only" } }).to_string(),
|
||||
)
|
||||
.expect("settings should write");
|
||||
}
|
||||
|
||||
fn run_claw(workspace: &PathBuf, config_home: &PathBuf, home: &PathBuf, base_url: &str) -> Output {
|
||||
Command::new(env!("CARGO_BIN_EXE_claw"))
|
||||
.current_dir(workspace)
|
||||
.env_clear()
|
||||
.env("ANTHROPIC_API_KEY", "test-parallel-tool-key")
|
||||
.env("ANTHROPIC_BASE_URL", base_url)
|
||||
.env("CLAW_CONFIG_HOME", config_home)
|
||||
.env("HOME", home)
|
||||
.env("NO_COLOR", "1")
|
||||
.env("PATH", "/usr/bin:/bin")
|
||||
.args([
|
||||
"--model",
|
||||
"sonnet",
|
||||
"--permission-mode",
|
||||
"read-only",
|
||||
"--output-format=json",
|
||||
"--allowedTools",
|
||||
"read_file,grep_search",
|
||||
])
|
||||
.arg(format!("{SCENARIO_PREFIX}{SCENARIO}"))
|
||||
.output()
|
||||
.expect("claw should launch")
|
||||
}
|
||||
|
||||
fn messages_have_tool_result(body: &Value) -> bool {
|
||||
body.get("messages")
|
||||
.and_then(Value::as_array)
|
||||
.is_some_and(|messages| {
|
||||
messages
|
||||
.iter()
|
||||
.any(|message| !tool_result_ids(message).is_empty())
|
||||
})
|
||||
}
|
||||
|
||||
fn tool_use_ids(message: &Value) -> Vec<String> {
|
||||
block_ids(message, "tool_use", "id")
|
||||
}
|
||||
|
||||
fn tool_result_ids(message: &Value) -> Vec<String> {
|
||||
block_ids(message, "tool_result", "tool_use_id")
|
||||
}
|
||||
|
||||
fn block_ids(message: &Value, block_type: &str, id_field: &str) -> Vec<String> {
|
||||
message
|
||||
.get("content")
|
||||
.and_then(Value::as_array)
|
||||
.map(|blocks| {
|
||||
blocks
|
||||
.iter()
|
||||
.filter(|block| block.get("type").and_then(Value::as_str) == Some(block_type))
|
||||
.filter_map(|block| block.get(id_field).and_then(Value::as_str))
|
||||
.map(ToString::to_string)
|
||||
.collect()
|
||||
})
|
||||
.unwrap_or_default()
|
||||
}
|
||||
|
||||
/// Renders the message role/block shape of every captured request for failures.
|
||||
fn describe_requests(requests: &[Value]) -> String {
|
||||
requests
|
||||
.iter()
|
||||
.enumerate()
|
||||
.map(|(index, body)| {
|
||||
let shape = body
|
||||
.get("messages")
|
||||
.and_then(Value::as_array)
|
||||
.map(|messages| {
|
||||
messages
|
||||
.iter()
|
||||
.map(|message| {
|
||||
let role = message
|
||||
.get("role")
|
||||
.and_then(Value::as_str)
|
||||
.unwrap_or("unknown");
|
||||
let blocks = message
|
||||
.get("content")
|
||||
.and_then(Value::as_array)
|
||||
.map(|blocks| {
|
||||
blocks
|
||||
.iter()
|
||||
.map(|block| {
|
||||
block
|
||||
.get("type")
|
||||
.and_then(Value::as_str)
|
||||
.unwrap_or("unknown")
|
||||
.to_string()
|
||||
})
|
||||
.collect::<Vec<_>>()
|
||||
})
|
||||
.unwrap_or_default();
|
||||
format!("{role}{blocks:?}")
|
||||
})
|
||||
.collect::<Vec<_>>()
|
||||
})
|
||||
.unwrap_or_default();
|
||||
format!(" request {index}: {shape:?}")
|
||||
})
|
||||
.collect::<Vec<_>>()
|
||||
.join("\n")
|
||||
}
|
||||
|
||||
fn unique_temp_dir(label: &str) -> PathBuf {
|
||||
let millis = SystemTime::now()
|
||||
.duration_since(UNIX_EPOCH)
|
||||
.expect("clock should be after epoch")
|
||||
.as_millis();
|
||||
let counter = TEMP_COUNTER.fetch_add(1, Ordering::Relaxed);
|
||||
std::env::temp_dir().join(format!(
|
||||
"claw-{label}-{}-{millis}-{counter}",
|
||||
std::process::id()
|
||||
))
|
||||
}
|
||||
@@ -14,6 +14,7 @@ uuid = { version = "1", features = ["v4", "serde"] }
|
||||
tracing = "0.1"
|
||||
url = "2"
|
||||
runtime = { path = "../runtime" }
|
||||
sha2 = "0.10"
|
||||
|
||||
[dev-dependencies]
|
||||
tempfile = "3"
|
||||
|
||||
@@ -8,6 +8,7 @@ use std::{
|
||||
};
|
||||
|
||||
use serde::{Deserialize, Serialize};
|
||||
use sha2::{Digest, Sha256};
|
||||
|
||||
use crate::error::{SdkError, SdkResult};
|
||||
|
||||
@@ -135,18 +136,17 @@ impl TerminalScopeStore {
|
||||
Ok(Some(record))
|
||||
}
|
||||
|
||||
/// Builds the record path for a correlation key.
|
||||
///
|
||||
/// Files are named from a fixed-width digest instead of the key's hex
|
||||
/// expansion: a maximal 512-byte key expands to 1024 hex characters, which
|
||||
/// overflows the 1024-byte total-path limit enforced by APFS before any
|
||||
/// chunking can help, and `File name too long` would be reported for a
|
||||
/// generated leaf name the caller never chose. The key itself is still
|
||||
/// stored in `TerminalScopeRecord::correlation_key` and checked on read.
|
||||
fn record_path(&self, correlation_key: &str) -> PathBuf {
|
||||
const MAX_COMPONENT_BYTES: usize = 180;
|
||||
let encoded = encode_key_as_hex(correlation_key);
|
||||
let mut path = self.root.clone();
|
||||
let mut remaining = encoded.as_str();
|
||||
while remaining.len() > MAX_COMPONENT_BYTES {
|
||||
let (component, tail) = remaining.split_at(MAX_COMPONENT_BYTES);
|
||||
path.push(component);
|
||||
remaining = tail;
|
||||
}
|
||||
path.push(format!("{remaining}.json"));
|
||||
path
|
||||
self.root
|
||||
.join(format!("{}.json", encode_key_for_path(correlation_key)))
|
||||
}
|
||||
}
|
||||
|
||||
@@ -158,10 +158,14 @@ fn validate_correlation_key(correlation_key: &str) -> SdkResult<()> {
|
||||
}
|
||||
}
|
||||
|
||||
fn encode_key_as_hex(key: &str) -> String {
|
||||
/// Encodes a correlation key as a fixed-width filesystem-safe path component.
|
||||
fn encode_key_for_path(key: &str) -> String {
|
||||
const HEX_DIGITS: &[u8; 16] = b"0123456789abcdef";
|
||||
let mut encoded = String::with_capacity(key.len() * 2);
|
||||
for byte in key.as_bytes() {
|
||||
let digest = Sha256::digest(key.as_bytes());
|
||||
// The first 16 bytes are far more than the number of records one store
|
||||
// holds, and the full key survives in the record for exact matching.
|
||||
let mut encoded = String::with_capacity(32);
|
||||
for byte in digest.iter().take(16) {
|
||||
encoded.push(char::from(HEX_DIGITS[usize::from(*byte >> 4)]));
|
||||
encoded.push(char::from(HEX_DIGITS[usize::from(*byte & 0x0f)]));
|
||||
}
|
||||
@@ -180,6 +184,7 @@ fn current_timestamp_ms() -> u64 {
|
||||
mod tests {
|
||||
use super::*;
|
||||
use std::fs;
|
||||
use std::path::Component;
|
||||
use uuid::Uuid;
|
||||
|
||||
struct TestDirectory(PathBuf);
|
||||
@@ -279,7 +284,7 @@ mod tests {
|
||||
let store = TerminalScopeStore::new(&root.0);
|
||||
let path = root
|
||||
.0
|
||||
.join(format!("{}.json", encode_key_as_hex("request")));
|
||||
.join(format!("{}.json", encode_key_for_path("request")));
|
||||
fs::write(path, "{").expect("write malformed scope");
|
||||
assert!(store.get("request").is_err());
|
||||
}
|
||||
@@ -296,6 +301,39 @@ mod tests {
|
||||
store.get(&key).expect("get long key").unwrap().session_id,
|
||||
"session-long"
|
||||
);
|
||||
|
||||
// A maximum-length key is 1024 hex characters. Expanding it into the
|
||||
// path used to exceed the filesystem's total-path limit, so assert on
|
||||
// the path shape rather than only on the round trip: the store works in
|
||||
// a short temporary root even when the generated path is unusable.
|
||||
let record_path = single_record_path(&root.0);
|
||||
let longest_component = record_path
|
||||
.components()
|
||||
.filter_map(|component| match component {
|
||||
Component::Normal(part) => Some(part.to_string_lossy().len()),
|
||||
_ => None,
|
||||
})
|
||||
.max()
|
||||
.expect("record path has a component");
|
||||
assert!(
|
||||
longest_component <= 255,
|
||||
"path component must stay within the filesystem filename limit, got {longest_component}"
|
||||
);
|
||||
assert!(
|
||||
record_path.as_os_str().len() < 1024,
|
||||
"record path must stay within the APFS total-path limit, got {} bytes",
|
||||
record_path.as_os_str().len()
|
||||
);
|
||||
}
|
||||
|
||||
/// Returns the single record file under a store root.
|
||||
fn single_record_path(root: &std::path::Path) -> PathBuf {
|
||||
let entries = fs::read_dir(root)
|
||||
.expect("read store root")
|
||||
.collect::<Result<Vec<_>, _>>()
|
||||
.expect("read store entries");
|
||||
assert_eq!(entries.len(), 1, "store should hold exactly one record");
|
||||
entries[0].path()
|
||||
}
|
||||
|
||||
#[test]
|
||||
|
||||
@@ -20,5 +20,8 @@ aspect-macros = "0.1"
|
||||
aspect-std = "0.1"
|
||||
log = "0.4"
|
||||
|
||||
[dev-dependencies]
|
||||
mock-anthropic-service = { path = "../mock-anthropic-service" }
|
||||
|
||||
[lints]
|
||||
workspace = true
|
||||
+289
-52
@@ -5525,52 +5525,97 @@ fn tool_specs_for_allowed_tools(allowed_tools: Option<&BTreeSet<String>>) -> Vec
|
||||
fn convert_messages(messages: &[ConversationMessage]) -> Vec<InputMessage> {
|
||||
messages
|
||||
.iter()
|
||||
.filter_map(|message| {
|
||||
let role = match message.role {
|
||||
MessageRole::System | MessageRole::User | MessageRole::Tool => "user",
|
||||
MessageRole::Assistant => "assistant",
|
||||
};
|
||||
let content = message
|
||||
.blocks
|
||||
.iter()
|
||||
.map(|block| match block {
|
||||
ContentBlock::Text { text } => InputContentBlock::Text { text: text.clone() },
|
||||
ContentBlock::Thinking {
|
||||
thinking,
|
||||
signature,
|
||||
} => InputContentBlock::Thinking {
|
||||
thinking: thinking.clone(),
|
||||
signature: signature.clone(),
|
||||
},
|
||||
ContentBlock::ToolUse { id, name, input } => InputContentBlock::ToolUse {
|
||||
id: id.clone(),
|
||||
name: name.clone(),
|
||||
input: serde_json::from_str(input)
|
||||
.unwrap_or_else(|_| serde_json::json!({ "raw": input })),
|
||||
},
|
||||
ContentBlock::ToolResult {
|
||||
tool_use_id,
|
||||
output,
|
||||
is_error,
|
||||
..
|
||||
} => InputContentBlock::ToolResult {
|
||||
tool_use_id: tool_use_id.clone(),
|
||||
content: vec![ToolResultContentBlock::Text {
|
||||
text: output.clone(),
|
||||
}],
|
||||
is_error: *is_error,
|
||||
},
|
||||
})
|
||||
.filter(
|
||||
|block| !matches!(block, InputContentBlock::Text { text } if text.is_empty()),
|
||||
)
|
||||
.collect::<Vec<_>>();
|
||||
(!content.is_empty()).then(|| InputMessage {
|
||||
role: role.to_string(),
|
||||
content,
|
||||
})
|
||||
.filter_map(convert_message)
|
||||
.fold(Vec::new(), absorb_tool_result_message)
|
||||
}
|
||||
|
||||
/// Converts one conversation message into its wire form.
|
||||
fn convert_message(message: &ConversationMessage) -> Option<InputMessage> {
|
||||
let role = match message.role {
|
||||
MessageRole::System | MessageRole::User | MessageRole::Tool => "user",
|
||||
MessageRole::Assistant => "assistant",
|
||||
};
|
||||
let content = message
|
||||
.blocks
|
||||
.iter()
|
||||
.map(|block| match block {
|
||||
ContentBlock::Text { text } => InputContentBlock::Text { text: text.clone() },
|
||||
ContentBlock::Thinking {
|
||||
thinking,
|
||||
signature,
|
||||
} => InputContentBlock::Thinking {
|
||||
thinking: thinking.clone(),
|
||||
signature: signature.clone(),
|
||||
},
|
||||
ContentBlock::ToolUse { id, name, input } => InputContentBlock::ToolUse {
|
||||
id: id.clone(),
|
||||
name: name.clone(),
|
||||
input: serde_json::from_str(input)
|
||||
.unwrap_or_else(|_| serde_json::json!({ "raw": input })),
|
||||
},
|
||||
ContentBlock::ToolResult {
|
||||
tool_use_id,
|
||||
output,
|
||||
is_error,
|
||||
..
|
||||
} => InputContentBlock::ToolResult {
|
||||
tool_use_id: tool_use_id.clone(),
|
||||
content: vec![ToolResultContentBlock::Text {
|
||||
text: output.clone(),
|
||||
}],
|
||||
is_error: *is_error,
|
||||
},
|
||||
})
|
||||
.collect()
|
||||
.filter(|block| !matches!(block, InputContentBlock::Text { text } if text.is_empty()))
|
||||
.collect::<Vec<_>>();
|
||||
(!content.is_empty()).then(|| InputMessage {
|
||||
role: role.to_string(),
|
||||
content,
|
||||
})
|
||||
}
|
||||
|
||||
/// Identical in `rusty-claude-cli` and `tools`; kept in both copies by review
|
||||
/// decision rather than shared, but they must still be changed together.
|
||||
///
|
||||
/// The two `convert_messages` call sites are not interchangeable: `tools` drops
|
||||
/// empty `Text` blocks and the CLI keeps them. That difference is confined to
|
||||
/// `convert_message`, so it constrains hoisting the whole conversion, not this
|
||||
/// fold. It is reachable when history is not built from the model stream — an
|
||||
/// empty `user_text("")` or a persisted `"text":""` restored from a session log
|
||||
/// bypasses the `flush_text_block` filter that the streaming path applies.
|
||||
/// Appends a message, merging a run of pure tool-result messages into one.
|
||||
///
|
||||
/// `runtime` records one `MessageRole::Tool` message per tool call, but the
|
||||
/// Anthropic Messages API requires every `tool_use` of an assistant turn to be
|
||||
/// answered by `tool_result` blocks in the single message that follows it, so a
|
||||
/// parallel tool-call turn must not reach the provider as one message per
|
||||
/// result. A merge requires both the accumulated message and the incoming one to
|
||||
/// be pure tool results: checking only the accumulated side would fold the
|
||||
/// assistant reply that follows a tool result into the tool-result message and
|
||||
/// lose its role.
|
||||
fn absorb_tool_result_message(
|
||||
mut messages: Vec<InputMessage>,
|
||||
message: InputMessage,
|
||||
) -> Vec<InputMessage> {
|
||||
if is_tool_result_only(&message) {
|
||||
if let Some(previous) = messages.last_mut() {
|
||||
if previous.role == "user" && is_tool_result_only(previous) {
|
||||
previous.content.extend(message.content);
|
||||
return messages;
|
||||
}
|
||||
}
|
||||
}
|
||||
messages.push(message);
|
||||
messages
|
||||
}
|
||||
|
||||
/// Reports whether a wire message carries tool results and nothing else.
|
||||
fn is_tool_result_only(message: &InputMessage) -> bool {
|
||||
!message.content.is_empty()
|
||||
&& message
|
||||
.content
|
||||
.iter()
|
||||
.all(|block| matches!(block, InputContentBlock::ToolResult { .. }))
|
||||
}
|
||||
|
||||
fn push_output_block(
|
||||
@@ -6976,19 +7021,20 @@ mod tests {
|
||||
use std::time::Duration;
|
||||
|
||||
use super::{
|
||||
agent_permission_policy, allowed_tools_for_subagent, build_agent_system_prompt,
|
||||
classify_lane_failure, derive_agent_state, execute_agent_with_spawn, execute_tool,
|
||||
extract_recovery_outcome, final_assistant_text, global_cron_registry,
|
||||
maybe_commit_provenance, mvp_tool_specs, permission_mode_from_plugin,
|
||||
absorb_tool_result_message, agent_permission_policy, allowed_tools_for_subagent,
|
||||
build_agent_system_prompt, classify_lane_failure, convert_message, derive_agent_state,
|
||||
execute_agent_with_spawn, execute_tool, extract_recovery_outcome, final_assistant_text,
|
||||
global_cron_registry, maybe_commit_provenance, mvp_tool_specs, permission_mode_from_plugin,
|
||||
persist_agent_terminal_state, push_output_block, run_task_packet, AgentInput, AgentJob,
|
||||
GlobalToolRegistry, LaneEventName, LaneFailureClass, ProviderRuntimeClient,
|
||||
SubagentToolExecutor,
|
||||
};
|
||||
use api::OutputContentBlock;
|
||||
use api::{InputContentBlock, InputMessage, OutputContentBlock};
|
||||
use runtime::ProviderFallbackConfig;
|
||||
use runtime::{
|
||||
permission_enforcer::PermissionEnforcer, ApiRequest, AssistantEvent, ConversationRuntime,
|
||||
PermissionMode, PermissionPolicy, RuntimeError, Session, TaskPacket, ToolExecutor,
|
||||
permission_enforcer::PermissionEnforcer, ApiClient, ApiRequest, AssistantEvent,
|
||||
ContentBlock, ConversationMessage, ConversationRuntime, PermissionMode, PermissionPolicy,
|
||||
RuntimeError, Session, TaskPacket, ToolExecutor,
|
||||
};
|
||||
use serde_json::json;
|
||||
|
||||
@@ -11063,4 +11109,195 @@ printf 'pwsh:%s' "$1"
|
||||
.into_bytes()
|
||||
}
|
||||
}
|
||||
fn tool_result_message(id: &str) -> runtime::ConversationMessage {
|
||||
ConversationMessage::tool_result(id, "bash", "ok", false)
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn subagent_parallel_tool_results_share_one_message() {
|
||||
let messages = vec![
|
||||
ConversationMessage::user_text("go"),
|
||||
ConversationMessage::assistant(vec![
|
||||
ContentBlock::ToolUse {
|
||||
id: "tool-1".to_string(),
|
||||
name: "bash".to_string(),
|
||||
input: "{\"command\":\"pwd\"}".to_string(),
|
||||
},
|
||||
ContentBlock::ToolUse {
|
||||
id: "tool-2".to_string(),
|
||||
name: "bash".to_string(),
|
||||
input: "{\"command\":\"ls\"}".to_string(),
|
||||
},
|
||||
]),
|
||||
tool_result_message("tool-1"),
|
||||
tool_result_message("tool-2"),
|
||||
];
|
||||
|
||||
let converted = super::convert_messages(&messages);
|
||||
|
||||
assert_eq!(converted.len(), 3, "converted: {converted:?}");
|
||||
let answered = converted[2]
|
||||
.content
|
||||
.iter()
|
||||
.filter_map(|block| match block {
|
||||
InputContentBlock::ToolResult { tool_use_id, .. } => Some(tool_use_id.as_str()),
|
||||
_ => None,
|
||||
})
|
||||
.collect::<Vec<_>>();
|
||||
assert_eq!(answered, vec!["tool-1", "tool-2"]);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn subagent_merge_requires_both_sides_to_be_tool_results() {
|
||||
let merged = absorb_tool_result_message(
|
||||
vec![InputMessage {
|
||||
role: "user".to_string(),
|
||||
content: vec![InputContentBlock::ToolResult {
|
||||
tool_use_id: "tool-1".to_string(),
|
||||
content: vec![api::ToolResultContentBlock::Text {
|
||||
text: "ok".to_string(),
|
||||
}],
|
||||
is_error: false,
|
||||
}],
|
||||
}],
|
||||
convert_message(&ConversationMessage::assistant(vec![ContentBlock::Text {
|
||||
text: "done".to_string(),
|
||||
}]))
|
||||
.expect("assistant reply converts"),
|
||||
);
|
||||
|
||||
assert_eq!(
|
||||
merged.len(),
|
||||
2,
|
||||
"the reply after a tool result must stay its own message: {merged:?}"
|
||||
);
|
||||
assert_eq!(merged[1].role, "assistant");
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn subagent_wire_request_pairs_parallel_tool_results() {
|
||||
// The subagent path reaches `convert_messages` only through
|
||||
// `ProviderRuntimeClient::stream`, and that conversion is private, so the
|
||||
// contract has to be checked on the bytes that leave the process: drive the
|
||||
// client against the mock service and assert on the captured body.
|
||||
let _lock = env_guard();
|
||||
// The service must keep serving while this thread blocks inside
|
||||
// `stream()`, so its runtime needs worker threads rather than
|
||||
// `new_current_thread`.
|
||||
let runtime = tokio::runtime::Builder::new_multi_thread()
|
||||
.worker_threads(2)
|
||||
.enable_all()
|
||||
.build()
|
||||
.expect("mock runtime");
|
||||
let server = runtime
|
||||
.block_on(mock_anthropic_service::MockAnthropicService::spawn())
|
||||
.expect("mock service should start");
|
||||
|
||||
// Point the client at the mock: the base URL and both credential variables
|
||||
// are read from the process environment, and this developer shell sets
|
||||
// ANTHROPIC_BASE_URL, so restore whatever was there before.
|
||||
let original_key = std::env::var_os("ANTHROPIC_API_KEY");
|
||||
let original_token = std::env::var_os("ANTHROPIC_AUTH_TOKEN");
|
||||
let original_base = std::env::var_os("ANTHROPIC_BASE_URL");
|
||||
std::env::set_var("ANTHROPIC_API_KEY", "tools-wire-test-key");
|
||||
std::env::remove_var("ANTHROPIC_AUTH_TOKEN");
|
||||
std::env::set_var("ANTHROPIC_BASE_URL", server.base_url());
|
||||
let restore = || {
|
||||
for (key, value) in [
|
||||
("ANTHROPIC_API_KEY", original_key.clone()),
|
||||
("ANTHROPIC_AUTH_TOKEN", original_token.clone()),
|
||||
("ANTHROPIC_BASE_URL", original_base.clone()),
|
||||
] {
|
||||
match value {
|
||||
Some(value) => std::env::set_var(key, value),
|
||||
None => std::env::remove_var(key),
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
// A turn whose two tool calls already have results: this is exactly the
|
||||
// history `convert_messages` used to split into two wire messages.
|
||||
let messages = vec![
|
||||
ConversationMessage::user_text(format!(
|
||||
"{}multi_tool_turn_roundtrip",
|
||||
mock_anthropic_service::SCENARIO_PREFIX
|
||||
)),
|
||||
ConversationMessage::assistant(vec![
|
||||
ContentBlock::ToolUse {
|
||||
id: "toolu_multi_read".to_string(),
|
||||
name: "read_file".to_string(),
|
||||
input: r#"{"path":"fixture.txt"}"#.to_string(),
|
||||
},
|
||||
ContentBlock::ToolUse {
|
||||
id: "toolu_multi_grep".to_string(),
|
||||
name: "grep_search".to_string(),
|
||||
input: r#"{"pattern":"parity","path":"fixture.txt"}"#.to_string(),
|
||||
},
|
||||
]),
|
||||
ConversationMessage::tool_result(
|
||||
"toolu_multi_read",
|
||||
"read_file",
|
||||
"alpha parity",
|
||||
false,
|
||||
),
|
||||
ConversationMessage::tool_result("toolu_multi_grep", "grep_search", "2", false),
|
||||
];
|
||||
|
||||
let mut client = ProviderRuntimeClient::new_with_fallback_config(
|
||||
"sonnet".to_string(),
|
||||
BTreeSet::from(["read_file".to_string(), "grep_search".to_string()]),
|
||||
&ProviderFallbackConfig::default(),
|
||||
)
|
||||
.expect("provider client");
|
||||
let _ = client.stream(ApiRequest {
|
||||
system_prompt: Vec::new(),
|
||||
messages,
|
||||
});
|
||||
|
||||
let captured = runtime.block_on(server.captured_requests());
|
||||
restore();
|
||||
drop(runtime);
|
||||
let captured = captured
|
||||
.iter()
|
||||
.filter(|request| request.path == "/v1/messages")
|
||||
.cloned()
|
||||
.collect::<Vec<_>>();
|
||||
assert_eq!(
|
||||
captured.len(),
|
||||
1,
|
||||
"one message request should leave the client"
|
||||
);
|
||||
let body: serde_json::Value =
|
||||
serde_json::from_str(&captured[0].raw_body).expect("request body is json");
|
||||
let wire = body["messages"].as_array().expect("messages array");
|
||||
|
||||
// The two tool results must share one user message: pairing requires every
|
||||
// tool_use of an assistant turn to be answered in the message right after it.
|
||||
let result_messages = wire
|
||||
.iter()
|
||||
.filter(|message| {
|
||||
message["content"]
|
||||
.as_array()
|
||||
.is_some_and(|blocks| blocks.iter().any(|block| block["type"] == "tool_result"))
|
||||
})
|
||||
.count();
|
||||
assert_eq!(result_messages, 1, "wire body: {body}");
|
||||
let call_messages = wire
|
||||
.iter()
|
||||
.filter(|message| {
|
||||
message["content"]
|
||||
.as_array()
|
||||
.is_some_and(|blocks| blocks.iter().any(|block| block["type"] == "tool_use"))
|
||||
})
|
||||
.count();
|
||||
assert_eq!(call_messages, 1, "wire body: {body}");
|
||||
|
||||
let answered = wire
|
||||
.iter()
|
||||
.flat_map(|message| message["content"].as_array().cloned().unwrap_or_default())
|
||||
.filter(|block| block["type"] == "tool_result")
|
||||
.filter_map(|block| block["tool_use_id"].as_str().map(ToString::to_string))
|
||||
.collect::<Vec<_>>();
|
||||
assert_eq!(answered, vec!["toolu_multi_read", "toolu_multi_grep"]);
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user