Skip to content

fix: 移除 fcIdx -1 偏移,修复并发工具调用撞键问题 - #5095

Merged
seefs001 merged 1 commit into
QuantumNous:mainfrom
learner-i:fix/concurrent-toolcall-fcidx-collision
May 25, 2026
Merged

fix: 移除 fcIdx -1 偏移,修复并发工具调用撞键问题#5095
seefs001 merged 1 commit into
QuantumNous:mainfrom
learner-i:fix/concurrent-toolcall-fcidx-collision

Conversation

@learner-i

@learner-i learner-i commented May 25, 2026

Copy link
Copy Markdown
Contributor

当 Claude 直接以多个 tool_use 块起始(无文本前导,index=0)时,
-1 偏移导致 index=0 和 index=1 同时映射到 fcIdx=0:

  • index=0 的工具 args 先流完,发出一次合法调用 ✓
  • index=1 的 args 追加到同一 map 槽位,污染后为非法 JSON,该工具丢失 ✗
  • index=2 以后的工具各自独占唯一 fcIdx,正常发出 ✓

结果:每轮并发调用中第 2 个工具必然丢失,
模型收不到对应的工具结果后重试剩余工具,
产生雪球效应(10个→9个→8个...逐轮收缩)。

修复:直接使用 Claude 的 block index 作为 fcIdx,不做偏移。
fcIdx 仅作为本地 map 的 key,只需保证唯一性,无需从 0 开始。

⚠️ 提交说明 / PR Notice

Important

  • 请提供人工撰写的简洁摘要,避免直接粘贴未经整理的 AI 输出。

📝 变更描述 / Description

(简述:做了什么?为什么这样改能生效?请基于你对代码逻辑的理解来写,避免粘贴未经整理的内容)

🚀 变更类型 / Type of change

  • [ ✅] 🐛 Bug 修复 (Bug fix) - 请关联对应 Issue,避免将设计取舍、理解偏差或预期不一致直接归类为 bug
  • ✨ 新功能 (New feature) - 重大特性建议先通过 Issue 沟通
  • ⚡ 性能优化 / 重构 (Refactor)
  • 📝 文档更新 (Documentation)

🔗 关联任务 / Related Issue

✅ 提交前检查项 / Checklist

  • [ ✅] 人工确认: 我已亲自整理并撰写此描述,没有直接粘贴未经处理的 AI 输出。
  • 非重复提交: 我已搜索现有的 IssuesPRs,确认不是重复提交。
  • [✅ ] Bug fix 说明: 若此 PR 标记为 Bug fix,我已提交或关联对应 Issue,且不会将设计取舍、预期不一致或理解偏差直接归类为 bug。
  • [ ✅] 变更理解: 我已理解这些更改的工作原理及可能影响。
  • [ ✅] 范围聚焦: 本 PR 未包含任何与当前任务无关的代码改动。
  • [✅ ] 本地验证: 已在本地运行并通过测试或手动验证,维护者可以据此复核结果。
  • [ ✅] 安全合规: 代码中无敏感凭据,且符合项目代码规范。

📸 运行证明 / Proof of Work

(请在此粘贴截图、关键日志或测试报告,以证明变更生效)

修复前(fcIdx -1 偏移,撞键):
场景 2:并发工具调用(同时写两个文件)
模型: aws.claude-opus-4.6
收到 SSE chunk 共 2 个
其中包含 functionCall 的 chunk: 1 个
✅ functionCall[0]: name=write_file args={'content': 'AAA', 'path': 'a.txt'}
⚠️ 预期 2 个并发调用,实际只收到 1 个

修复后(直接映射,无撞键):
场景 2:并发工具调用(同时写两个文件)
模型: aws.claude-opus-4.6
收到 SSE chunk 共 3 个
其中包含 functionCall 的 chunk: 2 个
✅ functionCall[0]: name=write_file args={'content': 'AAA', 'path': 'a.txt'}
✅ functionCall[1]: name=write_file args={'content': 'BBB', 'path': 'b.txt'}
✅ 全部通过

复现环境:

  • 渠道:Anthropic(14) / AWS Bedrock
  • 模型:aws.claude-opus-4.6(该模型倾向于直接 tool_use 起始,无文本前导)
  • 请求:Gemini 客户端格式,并发调用 ≥ 2 个工具

验证方法:
发送 streamGenerateContent 请求,要求模型同时调用两次 write_file,
统计收到的 functionCall 数量。
修复前:2 个并发工具调用只收到 1 个(index=1 工具丢失)。
修复后:2 个均正确到达。

Summary by CodeRabbit

  • Bug Fixes
    • Fixed tool call indexing in streaming responses to ensure proper index assignment.

Review Change Stack

当 Claude 直接以多个 tool_use 块起始(无文本前导,index=0)时,
-1 偏移导致 index=0 和 index=1 同时映射到 fcIdx=0:
- index=0 的工具 args 先流完,发出一次合法调用 ✓
- index=1 的 args 追加到同一 map 槽位,污染后为非法 JSON,该工具丢失 ✗
- index=2 以后的工具各自独占唯一 fcIdx,正常发出 ✓

结果:每轮并发调用中第 2 个工具必然丢失,
模型收不到对应的工具结果后重试剩余工具,
产生雪球效应(10个→9个→8个...逐轮收缩)。

修复:直接使用 Claude 的 block index 作为 fcIdx,不做偏移。
fcIdx 仅作为本地 map 的 key,只需保证唯一性,无需从 0 开始。
@coderabbitai

coderabbitai Bot commented May 25, 2026

Copy link
Copy Markdown
Contributor

No actionable comments were generated in the recent review. 🎉

ℹ️ Recent review info
⚙️ Run configuration

Configuration used: Organization UI

Review profile: CHILL

Plan: Pro

Run ID: a6ce6460-3417-47b1-bf23-d4bacfa1a3cc

📥 Commits

Reviewing files that changed from the base of the PR and between 583da45 and 3682539.

📒 Files selected for processing (1)
  • relay/channel/claude/relay-claude.go

Walkthrough

The PR fixes a tool call index collision bug in Claude streaming responses where concurrent tool calls at index 0 would collide with clamped negative indices, causing arguments to be overwritten in the index map and tool calls to be filtered out.

Changes

Claude Tool Call Index Fix

Layer / File(s) Summary
Tool call index calculation simplification
relay/channel/claude/relay-claude.go
fcIdx now uses claudeResponse.Index directly as a map key instead of subtracting 1 and clamping to 0, eliminating collisions when models respond with tool calls starting at index 0 without text prefix.

Estimated code review effort

🎯 2 (Simple) | ⏱️ ~8 minutes

Possibly related PRs

Suggested reviewers

  • Calcium-Ion

Poem

A rabbit hops through indexes bright,
Where tools collided in the night,
No more the -1 clamp that erred—
Direct the path, the call is heard! 🐰

🚥 Pre-merge checks | ✅ 4 | ❌ 1

❌ Failed checks (1 warning)

Check name Status Explanation Resolution
Docstring Coverage ⚠️ Warning Docstring coverage is 0.00% which is insufficient. The required threshold is 80.00%. Write docstrings for the functions missing them to satisfy the coverage threshold.
✅ Passed checks (4 passed)
Check name Status Explanation
Description Check ✅ Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Title check ✅ Passed Title is in Chinese and clearly describes the main change: removing the -1 offset for fcIdx to fix concurrent tool call collision issues, which directly aligns with the code change and PR objectives.
Linked Issues check ✅ Passed Code changes directly address issue #5018 by removing the -1 offset logic and using claudeResponse.Index directly as fcIdx, exactly matching the specified repair objective.
Out of Scope Changes check ✅ Passed The single change in relay-claude.go (removing -1 offset and clamping logic) is directly scoped to the linked issue; no out-of-scope modifications are present.

✏️ Tip: You can configure your own custom pre-merge checks in the settings.

✨ Finishing Touches
🧪 Generate unit tests (beta)
  • Create PR with unit tests

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

Comment @coderabbitai help to get the list of available commands and usage tips.

@seefs001
seefs001 merged commit ff06067 into QuantumNous:main May 25, 2026
2 checks passed
lyz52789 pushed a commit to lyz52789/new-api that referenced this pull request May 29, 2026
当 Claude 直接以多个 tool_use 块起始(无文本前导,index=0)时,
-1 偏移导致 index=0 和 index=1 同时映射到 fcIdx=0:
- index=0 的工具 args 先流完,发出一次合法调用 ✓
- index=1 的 args 追加到同一 map 槽位,污染后为非法 JSON,该工具丢失 ✗
- index=2 以后的工具各自独占唯一 fcIdx,正常发出 ✓

结果:每轮并发调用中第 2 个工具必然丢失,
模型收不到对应的工具结果后重试剩余工具,
产生雪球效应(10个→9个→8个...逐轮收缩)。

修复:直接使用 Claude 的 block index 作为 fcIdx,不做偏移。
fcIdx 仅作为本地 map 的 key,只需保证唯一性,无需从 0 开始。
xyfacai pushed a commit to xyfacai/new-api that referenced this pull request May 30, 2026
当 Claude 直接以多个 tool_use 块起始(无文本前导,index=0)时,
-1 偏移导致 index=0 和 index=1 同时映射到 fcIdx=0:
- index=0 的工具 args 先流完,发出一次合法调用 ✓
- index=1 的 args 追加到同一 map 槽位,污染后为非法 JSON,该工具丢失 ✗
- index=2 以后的工具各自独占唯一 fcIdx,正常发出 ✓

结果:每轮并发调用中第 2 个工具必然丢失,
模型收不到对应的工具结果后重试剩余工具,
产生雪球效应(10个→9个→8个...逐轮收缩)。

修复:直接使用 Claude 的 block index 作为 fcIdx,不做偏移。
fcIdx 仅作为本地 map 的 key,只需保证唯一性,无需从 0 开始。
isboyjc added a commit to isboyjc/amux-api that referenced this pull request May 31, 2026
当 Claude 以多个 tool_use 块起始(无文本前导,index=0)时,-1 偏移导致
index=0 和 index=1 同时映射到 fcIdx=0,第二个工具的 args 污染同一 map 槽位后
变为非法 JSON 而丢失。直接使用 Claude block index 作为 fcIdx(仅需唯一性)。

upstream ff06067 QuantumNous#5095
SamuelSxy pushed a commit to SamuelSxy/new-api-rh that referenced this pull request Jun 7, 2026
当 Claude 直接以多个 tool_use 块起始(无文本前导,index=0)时,
-1 偏移导致 index=0 和 index=1 同时映射到 fcIdx=0:
- index=0 的工具 args 先流完,发出一次合法调用 ✓
- index=1 的 args 追加到同一 map 槽位,污染后为非法 JSON,该工具丢失 ✗
- index=2 以后的工具各自独占唯一 fcIdx,正常发出 ✓

结果:每轮并发调用中第 2 个工具必然丢失,
模型收不到对应的工具结果后重试剩余工具,
产生雪球效应(10个→9个→8个...逐轮收缩)。

修复:直接使用 Claude 的 block index 作为 fcIdx,不做偏移。
fcIdx 仅作为本地 map 的 key,只需保证唯一性,无需从 0 开始。
szxufan pushed a commit to szxufan/new-api that referenced this pull request Jun 9, 2026
当 Claude 直接以多个 tool_use 块起始(无文本前导,index=0)时,
-1 偏移导致 index=0 和 index=1 同时映射到 fcIdx=0:
- index=0 的工具 args 先流完,发出一次合法调用 ✓
- index=1 的 args 追加到同一 map 槽位,污染后为非法 JSON,该工具丢失 ✗
- index=2 以后的工具各自独占唯一 fcIdx,正常发出 ✓

结果:每轮并发调用中第 2 个工具必然丢失,
模型收不到对应的工具结果后重试剩余工具,
产生雪球效应(10个→9个→8个...逐轮收缩)。

修复:直接使用 Claude 的 block index 作为 fcIdx,不做偏移。
fcIdx 仅作为本地 map 的 key,只需保证唯一性,无需从 0 开始。
fx247562340 pushed a commit to fx247562340/vancine-platform that referenced this pull request Jun 11, 2026
当 Claude 直接以多个 tool_use 块起始(无文本前导,index=0)时,
-1 偏移导致 index=0 和 index=1 同时映射到 fcIdx=0:
- index=0 的工具 args 先流完,发出一次合法调用 ✓
- index=1 的 args 追加到同一 map 槽位,污染后为非法 JSON,该工具丢失 ✗
- index=2 以后的工具各自独占唯一 fcIdx,正常发出 ✓

结果:每轮并发调用中第 2 个工具必然丢失,
模型收不到对应的工具结果后重试剩余工具,
产生雪球效应(10个→9个→8个...逐轮收缩)。

修复:直接使用 Claude 的 block index 作为 fcIdx,不做偏移。
fcIdx 仅作为本地 map 的 key,只需保证唯一性,无需从 0 开始。
QQhuxuhui added a commit to QQhuxuhui/new-api that referenced this pull request Jun 30, 2026
dev 与 upstream 无共同祖先、不能 cherry-pick,按文件手工 port:
- 502858d fix(QuantumNous#5543): tool 调用 arguments 为空时保留 tool_use 块
- 45cc95a fix(gemini): ToolConfig 加 IncludeServerSideToolInvocations 字段
- 8ca1033 fix: ReasoningContent/Reasoning 改 *string,修复空思考内容转发被静默丢弃
  (并适配 dev 自研 openaicompat/responses_to_chat.go 的 *string 赋值)
- 867d8ac fix(QuantumNous#5390): kimi-k2.6 温度归一化(import 冲突已解,去掉未用的 channelconstant)
- ff06067 fix(QuantumNous#5095): 移除 fcIdx -1 偏移,修复并发工具调用撞键

go build ./... 与 go vet 均通过。

Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
QQhuxuhui added a commit to QQhuxuhui/new-api that referenced this pull request Jun 30, 2026
包含:QuantumNous#5543 tool_use / gemini 字段 / ReasoningContent *string / QuantumNous#5390 kimi 温度 /
QuantumNous#5095 并发工具撞键 / aa56667 A 防上游 request-id 覆盖
ysicing pushed a commit to ysicing/new-api that referenced this pull request Aug 7, 2026
当 Claude 直接以多个 tool_use 块起始(无文本前导,index=0)时,
-1 偏移导致 index=0 和 index=1 同时映射到 fcIdx=0:
- index=0 的工具 args 先流完,发出一次合法调用 ✓
- index=1 的 args 追加到同一 map 槽位,污染后为非法 JSON,该工具丢失 ✗
- index=2 以后的工具各自独占唯一 fcIdx,正常发出 ✓

结果:每轮并发调用中第 2 个工具必然丢失,
模型收不到对应的工具结果后重试剩余工具,
产生雪球效应(10个→9个→8个...逐轮收缩)。

修复:直接使用 Claude 的 block index 作为 fcIdx,不做偏移。
fcIdx 仅作为本地 map 的 key,只需保证唯一性,无需从 0 开始。
330079598 pushed a commit to 330079598/new-api that referenced this pull request Aug 19, 2026
当 Claude 直接以多个 tool_use 块起始(无文本前导,index=0)时,
-1 偏移导致 index=0 和 index=1 同时映射到 fcIdx=0:
- index=0 的工具 args 先流完,发出一次合法调用 ✓
- index=1 的 args 追加到同一 map 槽位,污染后为非法 JSON,该工具丢失 ✗
- index=2 以后的工具各自独占唯一 fcIdx,正常发出 ✓

结果:每轮并发调用中第 2 个工具必然丢失,
模型收不到对应的工具结果后重试剩余工具,
产生雪球效应(10个→9个→8个...逐轮收缩)。

修复:直接使用 Claude 的 block index 作为 fcIdx,不做偏移。
fcIdx 仅作为本地 map 的 key,只需保证唯一性,无需从 0 开始。
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Bug: StreamResponseClaude2OpenAI fcIdx 撞键导致并发工具调用丢失

2 participants