修复: 走查发现(空 id 跨轮全局唯一 + generate_image SSRF/OOM + 技能日志 + fallback 测试)
- 空 tool_call id fallback 改全局 AtomicU64 计数器(跨轮跨 assistant 唯一,治空气泡+ L1 误熔断+三元组错位,实证 af2fab4e);fallback 测试硬编码改 starts_with(解耦计数器) - generate_image provider POST 补 SSRF 防护(复用 http.rs validate_url/resolve_and_check_host, 治 base_url 用户可配打内网)+ b64_json 解码前估算长度防 OOM - SkillResolver 加 tracing 日志(定位技能发送后未注入:name 匹配/read 失败)
This commit is contained in:
@@ -125,22 +125,43 @@ impl ToolCall {
|
||||
/// 路由结果,id 空时所有结果落到同一 key(`audit/mod.rs:203` 的 `seen_ids` 去重把空 id
|
||||
/// 视为相同,只留首个 tool_call)→ AI 看到「所有调用同一结果」,工具全失败。
|
||||
///
|
||||
/// 兜底在**解析点**生成 fallback id:raw 非空用 raw,空用 `format!("{prefix}_{index}")`
|
||||
/// (index 取 tool_call 在数组中的位置,保证同 assistant 内多 tool_call id 唯一)。
|
||||
/// 兜底在**解析点**生成 fallback id:raw 非空用 raw,空用 `format!("{prefix}_{n}")`
|
||||
/// (n 取自下方 `FALLBACK_ID_COUNTER` **全局递增计数器**,跨轮跨 assistant 唯一)。
|
||||
/// 下游(工具执行 / tool 结果回填 tool_call_id)从解析后的 `ToolCall.id` 取,不重复生成,
|
||||
/// 确保 assistant tool_call.id 与 tool 结果 tool_call_id 匹配(防 sanitize 三元组断裂)。
|
||||
///
|
||||
/// 三处解析点共用本 helper(DRY):OpenAI 同步 `parse_tool_calls`(prefix=`gen_tool`)、
|
||||
/// OpenAI 流式 chunk(prefix=`gen_stream`)、Anthropic 同步 + 流式(prefix=`gen_anthropic` /
|
||||
/// `gen_anthropic_stream`)。正常 provider(OpenAI/Claude/GLM id 非空)原样透传零介入。
|
||||
pub fn tool_call_id_or_fallback(raw: &str, index: usize, prefix: &str) -> String {
|
||||
///
|
||||
/// # 为何用全局计数器而非单轮 index(实证 af2fab4e)
|
||||
///
|
||||
/// 旧实现 fallback 用 `format!("{prefix}_{index}")`,index 是**单轮** tool_call 数组
|
||||
/// 位置。跨轮(不同 assistant)index 都从 0 起 → `gen_stream_0` 跨轮重复。agentic 的
|
||||
/// `id_to_name`(`insert(id, name)`)后者覆盖前者 → run_command 的 exit=1 被误标
|
||||
/// grep::exit=1 → L1 误熔断 grep(冤枉)→ loop 停 → 最后 assistant 空 content tool_calls
|
||||
/// 没执行(空气泡)。更严重:id 重复 → tool 结果配错 tool_call(三元组配对错位)。
|
||||
///
|
||||
/// 全局 `AtomicU64`(SeqCst)跨轮跨 assistant 严格递增,fallback id 永不重复。`index`
|
||||
/// 参数保留仅为签名兼容(4 处调用点 parse_tool_calls / 流式 chunk / push / agentic 都传),
|
||||
/// fallback 内部不再使用 index。
|
||||
///
|
||||
/// 单测跨进程实例计数器从 0 起;并发场景下两线程拿到的 fallback id 也严格递增(SeqCst),
|
||||
/// 保证全局唯一。
|
||||
pub fn tool_call_id_or_fallback(raw: &str, _index: usize, prefix: &str) -> String {
|
||||
if !raw.is_empty() {
|
||||
raw.to_string()
|
||||
} else {
|
||||
format!("{prefix}_{index}")
|
||||
let n = FALLBACK_ID_COUNTER.fetch_add(1, std::sync::atomic::Ordering::SeqCst);
|
||||
format!("{prefix}_{n}")
|
||||
}
|
||||
}
|
||||
|
||||
/// fallback id 全局计数器:跨轮跨 assistant 严格递增,保证空 id fallback 永不重复。
|
||||
///
|
||||
/// 见 `tool_call_id_or_fallback` 文档说明(实证 af2fab4e 跨轮重复根因)。
|
||||
static FALLBACK_ID_COUNTER: std::sync::atomic::AtomicU64 = std::sync::atomic::AtomicU64::new(0);
|
||||
|
||||
/// LLM Provider trait
|
||||
#[async_trait]
|
||||
pub trait LlmProvider: Send + Sync {
|
||||
@@ -462,33 +483,51 @@ mod tests {
|
||||
/// CR-空 id:tool_call_id_or_fallback 共享 helper —— 空 raw → fallback,非空原样。
|
||||
#[test]
|
||||
fn tool_call_id_or_fallback_non_empty_passthrough() {
|
||||
// 非空 raw 原样透传(provider 真 id 如 call_xxx 保留),与 index/prefix 无关
|
||||
assert_eq!(tool_call_id_or_fallback("call_abc", 0, "gen_tool"), "call_abc");
|
||||
assert_eq!(tool_call_id_or_fallback("x", 5, "p"), "x");
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn tool_call_id_or_fallback_empty_generates_with_index() {
|
||||
assert_eq!(tool_call_id_or_fallback("", 0, "gen_tool"), "gen_tool_0");
|
||||
assert_eq!(tool_call_id_or_fallback("", 1, "gen_tool"), "gen_tool_1");
|
||||
assert_eq!(tool_call_id_or_fallback("", 7, "gen_stream"), "gen_stream_7");
|
||||
fn tool_call_id_or_fallback_empty_starts_with_prefix() {
|
||||
// 空 raw → "{prefix}_{n}",n 取自全局计数器(跨进程实例从 0 起,单测不假设具体值)
|
||||
let a = tool_call_id_or_fallback("", 0, "gen_tool");
|
||||
assert!(a.starts_with("gen_tool_"), "空 fallback 应以 gen_tool_ 开头, got: {a}");
|
||||
let b = tool_call_id_or_fallback("", 7, "gen_stream");
|
||||
assert!(b.starts_with("gen_stream_"), "空 fallback 应以 gen_stream_ 开头, got: {b}");
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn tool_call_id_or_fallback_empty_unique_per_index() {
|
||||
// 同 prefix 不同 index → 不同 fallback(保证同 assistant 多 tool_call id 唯一)
|
||||
let ids: Vec<String> = (0..3).map(|i| tool_call_id_or_fallback("", i, "gen_tool")).collect();
|
||||
let mut sorted = ids.clone();
|
||||
sorted.sort();
|
||||
sorted.dedup();
|
||||
assert_eq!(ids.len(), sorted.len(), "各 index fallback 应唯一: {:?}", ids);
|
||||
fn tool_call_id_or_fallback_empty_globally_unique() {
|
||||
// 跨轮跨 assistant 唯一:连续两次空 fallback id 必不同(全局计数器递增)。
|
||||
// 这是修复 af2fab4e 跨轮重复(旧单轮 index 跨轮都从 0 起 → 重复)的核心断言。
|
||||
let a = tool_call_id_or_fallback("", 0, "gen_tool");
|
||||
let b = tool_call_id_or_fallback("", 0, "gen_tool");
|
||||
assert_ne!(a, b, "两次空 fallback 应不同(全局计数器跨轮唯一): {a} vs {b}");
|
||||
// 即使同 index(模拟跨轮 index 都从 0 起),fallback 也必唯一
|
||||
let c = tool_call_id_or_fallback("", 0, "gen_tool");
|
||||
let mut set = std::collections::HashSet::new();
|
||||
assert!(set.insert(a), "fallback a 应唯一");
|
||||
assert!(set.insert(b), "fallback b 应唯一");
|
||||
assert!(set.insert(c), "fallback c 应唯一");
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn tool_call_id_or_fallback_index_unused() {
|
||||
// index 参数仅为签名兼容保留(4 处调用点都传),fallback 不再使用 index。
|
||||
// 同 prefix + 同 index 连续两次 → 不同 fallback(全局计数器递增,与 index 无关)。
|
||||
let a = tool_call_id_or_fallback("", 3, "gen_tool");
|
||||
let b = tool_call_id_or_fallback("", 3, "gen_tool");
|
||||
assert_ne!(a, b, "同 index 两次空 fallback 应不同: {a} vs {b}");
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn tool_call_id_or_fallback_prefix_distinguishes_sources() {
|
||||
// 不同 prefix 区分来源(同步 gen_tool / 流式 gen_stream / anthropic gen_anthropic)
|
||||
assert_ne!(
|
||||
tool_call_id_or_fallback("", 0, "gen_tool"),
|
||||
tool_call_id_or_fallback("", 0, "gen_stream")
|
||||
);
|
||||
// 注意:两次空 fallback 因全局计数器递增 id 不同,故只比 prefix 前缀
|
||||
let a = tool_call_id_or_fallback("", 0, "gen_tool");
|
||||
let b = tool_call_id_or_fallback("", 0, "gen_stream");
|
||||
assert!(a.starts_with("gen_tool_"));
|
||||
assert!(b.starts_with("gen_stream_"));
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1167,9 +1167,14 @@ mod tests {
|
||||
);
|
||||
}
|
||||
|
||||
/// CR-空 id:parse_tool_calls 对空 id 按 index 生成 gen_tool_{i} fallback,非空原样。
|
||||
/// 根因:SenseNova 等兼容缺陷 provider 发空 tool_call.id,多 tool_call 同 id(空串)
|
||||
/// 致 audit/mod.rs:203 seen_ids 去重只留首个 → 所有工具结果路由到首个。
|
||||
/// CR-空 id:parse_tool_calls 对空 id 生成 gen_tool_{n} fallback(n 取自全局计数器,
|
||||
/// 跨轮跨 assistant 严格递增),非空原样。根因:SenseNova 等兼容缺陷 provider 发空
|
||||
/// tool_call.id,多 tool_call 同 id(空串)致 audit/mod.rs:203 seen_ids 去重只留首个
|
||||
/// → 所有工具结果路由到首个。
|
||||
///
|
||||
/// 断言策略:fallback id 由全局 FALLBACK_ID_COUNTER 决定具体序号,**同进程其他测试先
|
||||
/// 消费计数器即非 0 起**(非确定性),故不假设具体序号,改断言 prefix + 唯一性 + 透传
|
||||
/// 无损(对齐 provider.rs:495 helper 单测的 starts_with 模式,2026-08-02 走查修复)。
|
||||
#[test]
|
||||
fn openai_parse_tool_calls_empty_id_fallback_unique() {
|
||||
let calls = vec![
|
||||
@@ -1191,9 +1196,17 @@ mod tests {
|
||||
];
|
||||
let parsed = OpenAICompatProvider::parse_tool_calls(calls);
|
||||
assert_eq!(parsed.len(), 3);
|
||||
// 空 id → fallback(按 index),保证唯一
|
||||
assert_eq!(parsed[0].id, "gen_tool_0");
|
||||
assert_eq!(parsed[1].id, "gen_tool_1");
|
||||
// 空 id → fallback(prefix=gen_tool_,具体序号由全局计数器决定,非确定性,不断言序号)
|
||||
assert!(
|
||||
parsed[0].id.starts_with("gen_tool_"),
|
||||
"空 fallback 应以 gen_tool_ 开头, got: {}",
|
||||
parsed[0].id
|
||||
);
|
||||
assert!(
|
||||
parsed[1].id.starts_with("gen_tool_"),
|
||||
"空 fallback 应以 gen_tool_ 开头, got: {}",
|
||||
parsed[1].id
|
||||
);
|
||||
// 非空 id 原样透传
|
||||
assert_eq!(parsed[2].id, "call_abc123");
|
||||
// name/args 透传无损
|
||||
@@ -1211,24 +1224,40 @@ mod tests {
|
||||
}
|
||||
|
||||
/// CR-空 id 流式:SSE chunk 携带 `"id":""`(SenseNova 兼容缺陷)→ ToolCallDelta.id
|
||||
/// 转为 `gen_stream_{index}` fallback(非 None),保证下游 accumulate_tool_calls 写入
|
||||
/// draft.id 非空。chunk 完全无 id 字段(None)保持 None(OpenAI 协议:仅首 chunk 有 id,
|
||||
/// 后续 chunk 无 id 不应覆盖首 chunk 权威 id),由 agentic 转换点兜底。
|
||||
/// 转为 `gen_stream_{n}` fallback(n 取自全局计数器,跨轮跨 assistant 递增,非 None),
|
||||
/// 保证下游 accumulate_tool_calls 写入 draft.id 非空。chunk 完全无 id 字段(None)保持
|
||||
/// None(OpenAI 协议:仅首 chunk 有 id,后续 chunk 无 id 不应覆盖首 chunk 权威 id),
|
||||
/// 由 agentic 转换点兜底。
|
||||
///
|
||||
/// 断言策略:fallback id 具体序号由全局 FALLBACK_ID_COUNTER 决定,**同进程其他测试先
|
||||
/// 消费计数器即非 0 起**(非确定性),故不假设具体序号,改断言 prefix + 跨 chunk 唯一 +
|
||||
/// None/非空透传(对齐 provider.rs:530 helper 单测的 starts_with 模式,2026-08-02 走查修复)。
|
||||
#[test]
|
||||
fn openai_stream_chunk_empty_id_fallback() {
|
||||
let mut acc: Option<TokenUsage> = None;
|
||||
// chunk 1: tool_call index=0, id="" → fallback gen_stream_0
|
||||
// chunk 1: tool_call index=0, id="" → fallback gen_stream_{n}
|
||||
let data1 = r#"{"choices":[{"delta":{"tool_calls":[{"index":0,"id":"","type":"function","function":{"name":"list_dir","arguments":"{\"path\":\"docs\"}"}}]}}]}"#;
|
||||
let c1 = apply_openai_sse(data1, &mut acc);
|
||||
let tc1 = c1.tool_calls.as_ref().expect("应有 tool_calls").first().unwrap();
|
||||
assert_eq!(tc1.index, 0);
|
||||
assert_eq!(tc1.id.as_deref(), Some("gen_stream_0"), "空 id 应转 fallback");
|
||||
let id1 = tc1.id.as_deref().expect("空 id 应转 fallback(非 None)");
|
||||
assert!(
|
||||
id1.starts_with("gen_stream_"),
|
||||
"空 fallback 应以 gen_stream_ 开头, got: {}",
|
||||
id1
|
||||
);
|
||||
|
||||
// chunk 2: tool_call index=1, id="" → fallback gen_stream_1(与 index=0 不同,唯一)
|
||||
// chunk 2: tool_call index=1, id="" → fallback gen_stream_{n+1}(与 chunk 1 不同,唯一)
|
||||
let data2 = r#"{"choices":[{"delta":{"tool_calls":[{"index":1,"id":"","type":"function","function":{"name":"read_file","arguments":""}}]}}]}"#;
|
||||
let c2 = apply_openai_sse(data2, &mut acc);
|
||||
let tc2 = c2.tool_calls.as_ref().expect("应有 tool_calls").first().unwrap();
|
||||
assert_eq!(tc2.id.as_deref(), Some("gen_stream_1"), "不同 index fallback 应不同");
|
||||
let id2 = tc2.id.as_deref().expect("空 id 应转 fallback(非 None)");
|
||||
assert!(
|
||||
id2.starts_with("gen_stream_"),
|
||||
"空 fallback 应以 gen_stream_ 开头, got: {}",
|
||||
id2
|
||||
);
|
||||
assert_ne!(id1, id2, "两次空 id 的 fallback 应不同(全局计数器递增唯一)");
|
||||
|
||||
// chunk 3: tool_call index=0, 无 id 字段(None)→ 保持 None(不覆盖首 chunk)
|
||||
let data3 = r#"{"choices":[{"delta":{"tool_calls":[{"index":0,"function":{"arguments":"更多参数"}}]}}]}"#;
|
||||
|
||||
Reference in New Issue
Block a user