mirror of
https://github.com/instructkr/claw-code.git
synced 2026-11-18 00:00:00 +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
@@ -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();
|
||||
|
||||
Reference in new issue
Block a user