Skip to content

fix(traces): normalize tool_use arguments JSON to fix span dedup - #2013

Merged
looplj merged 1 commit into
looplj:unstablefrom
ChancenJ:fix/trace-tool-use-dedup
Jul 16, 2026
Merged

looplj merged 1 commit into
looplj:unstablefrom
ChancenJ:fix/trace-tool-use-dedup

Conversation

@ChancenJ

Copy link
Copy Markdown
Contributor

问题

追踪页面中,连续工具调用的 tool_use span 出现重复显示。例如:

  • 段N 显示:bash工具调用、工具结果、write工具调用
  • 段N+1 显示:write工具调用、工具结果、edit工具调用

其中 write工具调用 在两个段里都出现了。

根因

spanToKey 函数生成去重 key 时,直接使用 tool_use span 的原始 arguments 字符串。但同一个 tool call 的 arguments 在两个地方的 JSON 格式不同:

来源 格式 示例
请求体(客户端发送) 紧凑 JSON {"command":"pwd"}
响应体(流式聚合后) 格式化 JSON {"command": "pwd"}

Go 的 json.Marshal 在冒号后加空格,与客户端的紧凑 JSON 不一致。导致同一个 tool call 生成了不同的 key,去重失败。

修复

spanToKeytool_use 分支,对 arguments 调用 normalizeJSON(解析再重新序列化),消除 JSON 格式差异。

The spanToKey function used raw arguments strings for tool_use span
comparison. Request body arguments (compact JSON from client) and
response body arguments (re-serialized by stream aggregation) had
different whitespace formatting, causing identical tool calls to
produce different keys and fail deduplication.

Added normalizeJSON to re-parse and re-serialize arguments before
key generation, eliminating formatting differences.
@greptile-apps

greptile-apps Bot commented Jul 14, 2026

Copy link
Copy Markdown
Contributor

Greptile Summary

This PR normalizes tool-use trace keys to reduce duplicate spans. The main changes are:

  • Adds a JSON reparse-and-marshal helper in trace handling.
  • Applies normalized arguments when building tool_use deduplication keys.
  • Keeps the original argument string when parsing fails.

Confidence Score: 5/5

This looks safe to merge after considering a small trace-deduplication edge case.

  • No blocking issues found in the changed code.
  • The noted edge case is limited to trace display deduplication when same-id tool arguments differ only in JSON details that Go canonicalizes away.

Important Files Changed

Filename Overview
internal/server/biz/trace.go Adds JSON normalization for tool_use span keys, with one edge case where distinct valid JSON argument strings can collide after canonicalization.

Reviews (1): Last reviewed commit: "fix(traces): normalize tool_use argument..." | Re-trigger Greptile

Comment thread internal/server/biz/trace.go
@looplj
looplj merged commit 5316bf3 into looplj:unstable Jul 16, 2026
5 checks passed
@ChancenJ
ChancenJ deleted the fix/trace-tool-use-dedup branch July 17, 2026 03:53
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.

2 participants