[UI/统计] 「TOOL 使用」明细列表区分大小写,导致同名工具被拆分且与右侧汇总口径不一致
问题描述
在「观测 > 用量 > 会话 > TOOL 使用」统计卡片中,左侧明细列表与右侧汇总图例对工具名称(Tool Name)的大小写处理不一致:
- 左侧明细列表区分大小写:相同功能的工具因为调用端命名大小写不同(例如不同 Agent 上报的
bash与Bash、read与Read、write与Write)被拆成两个独立的工具统计,导致占比和会话分布被割裂。 - 右侧图例已做大小写归并:右侧的总体进度条图例显示为
Bash 2.2K、Read 315、Write 120,数量刚好是左侧大小写两项之和(1.4K + 779 = 2.2K,192 + 123 = 315,64 + 56 = 120)。
这导致同一个卡片内部左右统计口径存在明显不一致。
实际统计数据对比(来自界面实测)
| 工具类别 | 左侧明细拆分项 | 调用次数 | 会话数 | 占比 | 右侧色彩条汇总项 |
|---|---|---|---|---|---|
| Bash | bashBash |
1.4K 779 |
20 个会话 2 个会话 |
51% 28% |
Bash 2.2K(1.4K + 779) |
| Read | readRead |
192 123 |
15 个会话 1 个会话 |
7% 4% |
Read 315(192 + 123) |
| Write | writeWrite |
64 56 |
13 个会话 1 个会话 |
2% 2% |
Write 120(64 + 56) |
改进建议
- 聚合前统一名称归一化:在左侧明细列表做聚合统计时,对工具名先做统一归一化处理(例如统一
toLowerCase()或统一转为首字母大写 Title Case),与右侧图例的聚合口径对齐。 - 会话数去重合并:归并计算会话数时,建议基于
Set(session_ids)进行并集去重,避免同一会话既调用了bash又调用了Bash时会话数被重复相加。 - 展示名称统一:建议统一采用首字母大写(如右侧图例的
Bash、Read、Write)展示,保持整体 UI 一致性。
The numbers on the two sides of the same card disagree because they aggregate at two different keys, both in toolUse (internal/sessions/stats.go, the Usage > Sessions overview's tools payload; the Svelte-free JS in assets/plugins/usage/page.js only renders what that Go function sends):
- the left detail list keyed its rows by the raw reported name (
tools[name]), so one tool read as two; - the right legend keyed by kind (
cats[ToolCategory(name)]), andToolCategory's switch lists both casings ("bash","Bash","read","Read", …), so it already merges them.
From the report:
| Tool | Left, before | Calls | Sessions | Right legend | |
|---|---|---|---|---|---|
| Bash | bash / Bash |
1.4K + 779 | 20 + 2 | → | Bash 2.2K |
| Read | read / Read |
192 + 123 | 15 + 1 | → | Read 315 |
| Write | write / Write |
64 + 56 | 13 + 1 | → | Write 120 |
Each right-hand figure is exactly the sum of its left-hand pair — the left just never added them up. The fix folds a tool's spellings into one row at the aggregation: the detail list now groups by the case-folded name (strings.ToLower), a session that called both ways counts its session once (a per-session seen-set on the folded key — no session ids needed in the data model, Summary is already per-session), and the row is displayed the way the summary spells it: when a tool is its own kind save for the case, the row takes the summary's capitalized form (bash → Bash, the reporter's display suggestion), otherwise it keeps its most-called original spelling (ties broken alphabetically, so the display is stable across refreshes). Tools that only share a kind — two mcp__ tools, BashOutput beside Bash — stay apart, as the detail list should. Storage is untouched: usage.jsonl keeps the names as the callers reported them, and the summary's per-kind list and weekly bars are unchanged.
Branch: hunt49-b-tool-case, one commit on top of upstream 79160455; the patch below applies with git am.
What changed
toolUse(internal/sessions/stats.go) now keys the detail rows bystrings.ToLower(name)and sums calls under that one key; the shown name isToolCategory(name)'s form when the tool is its kind save for case, else the spelling with the most calls.t.Sessionsincrements once per session per folded key (was: once per raw name, so a session that calledreadandReadcounted twice against the pair of rows).- The summary list (
Categories) and the weekly bars (already category-keyed) are byte-for-byte the same computation as before. - New table-driven
TestToolUseMergesSpellingsininternal/sessions/stats_tools_test.gocovers the report's shape: 20 sessions ofbash(70 calls each) plus one ofBash(779) must read as oneBashrow of 2,179 calls in 21 sessions; a session that called bothreadandReadcounts once; twomcp__github__*tools stay apart.
Red before the fix
The new test, run against the old aggregation (change reverted, test kept):
--- FAIL: TestToolUseMergesSpellings (0.00s)
stats_tools_test.go:172: one tool spelled two ways is one row, spelled as its summary does: top [{Name:bash Category:Bash Calls:1400 Sessions:20} {Name:Bash Category:Bash Calls:779 Sessions:1}], want [{Name:Bash Category:Bash Calls:2179 Sessions:21}]
stats_tools_test.go:172: a session that called both ways counts once: top [{Name:Read Category:Read Calls:111 Sessions:1} {Name:read Category:Read Calls:24 Sessions:2}], want [{Name:Read Category:Read Calls:135 Sessions:2}]
FAIL github.com/yetone/magpie/internal/sessions
The first line is the report's table in Go structs. Restored, the test passes.
Verification
| Check | Command | Result |
|---|---|---|
| New test, fixed code | go test ./internal/sessions/ -run TestToolUseMergesSpellings -count=1 |
pass |
| New test, fix reverted (ablation) | git apply -R <patch> + same command |
fails as above |
| Owning package, full suite | go test ./internal/sessions/ -count=1 |
ok |
| Format | gofmt -l on the two touched files |
clean |
| Vet | go vet ./internal/sessions/ |
clean |
| Build | go build ./... |
ok (pre-existing macOS cgo/ld warnings only) |
Siblings
usage.jsonlstorage: untouched, as scoped — the raw reported names are kept, only the card's aggregation folds them.- The summary's per-kind list itself still buckets by
ToolCategory's synonym table, not by name — that is its job; one residual nuance there is that a lowercasetaskis not inToolCategory's Task case (it counts under "Other" in the legend while the detail row folds it intoTask), pre-existing and untouched here. - Other aggregations of tool names on this card: the "By week" bars are already category-keyed (
pd.tools[ToolCategory(name)]) and unaffected; the "Top skills" card keys by skill name, which has no case-variant pairs in practice; the models and projects bars key by identifiers. - The card's renderer (
assets/plugins/usage/page.js) is data-driven and unchanged; a merged row shows the same name its kind dot was already colored by.
Not verified
- The reporter's exact dataset is not available to me; the fixtures are shaped like their table (the 1.4K/779 split, per-session counts, one session calling both spellings) but the real-world numbers, percentages and rounding in the UI are not reproduced here.
- The card was not exercised in a browser in this environment (Playwright is not installed here;
node --test internal/gui/tests/sessions-overview.test.cjscannot load itsplaywrightmodule). That test stubs/api/sessions/overviewclient-side, so it does not reach the Go aggregation this patch changes; it verifies the renderer, which is untouched.
AI: written with an AI coding agent; I've read the whole diff and can answer questions on it myself. The model name is in the commit's trailer.
Attribution: If you apply the patch, please keep its author (ggbdpq) or add a Co-authored-by: ggbdpq <[email protected]> line.
patch — usage: the tool list groups tools case-insensitively, as its summary does (#2969)
From 8107cb2fb7a3bd288b95342e0182feff1602050e Mon Sep 17 00:00:00 2001
From: ggbdpq <[email protected]>
Date: Tue, 20 Oct 2026 05:27:24 +0800
Subject: [PATCH] usage: the tool list groups tools case-insensitively, as its
summary does (#2969)
What changed
- toolUse now folds a tool's spellings into one row of the sessions'
"Tool use" detail list: its calls and sessions are summed under one
case-insensitive name, the row is shown by the summary's own spelling
when the tool is its kind save for the case ("bash" as "Bash"), and by
its most called spelling otherwise. A session that called both ways
counts once.
- The summary's per-kind list ("Bash 2.2K") already merged the spellings,
via ToolCategory; the detail list keyed its rows by the raw name, so
the same tool read as two.
Why (issue #2969)
The card's left list split `bash` (1.4K calls, 20 sessions) from `Bash`
(779, 2), `read` (192, 15) from `Read` (123, 1) and `write` (64, 13) from
`Write` (56, 1), while the right legend showed one Bash 2.2K, Read 315,
Write 120 - the sums of each pair. Same card, two answers. The detail
rows now answer with the summary.
Run
- go test ./internal/sessions/ -run TestToolUseMergesSpellings: fails on
the old code (the split above), passes on the new; a session that
called `read` and `Read` counts once, and tools that only share a kind
(two mcp__ tools) stay apart.
- go test ./internal/sessions/: ok.
- gofmt -l on the touched files: clean; go vet ./internal/sessions/: clean;
go build ./...: ok.
Generated-by: GLM-5.3-Flash (ZCode)
---
internal/sessions/stats.go | 28 ++++++++---
internal/sessions/stats_tools_test.go | 68 +++++++++++++++++++++++++++
2 files changed, 90 insertions(+), 6 deletions(-)
diff --git a/internal/sessions/stats.go b/internal/sessions/stats.go
index 67c8a10d..2bf0aec7 100644
--- a/internal/sessions/stats.go
+++ b/internal/sessions/stats.go
@@ -572,10 +572,13 @@ func shapes(in []Summary) map[string]Shape {
return out
}
-// toolUse sums up the sessions' tool calls, the n most called.
+// toolUse sums up the sessions' tool calls, the n most called. The callers
+// spell a tool's name either way ("bash", "Bash"), so its spellings are one
+// tool here, shown as its summary spells it.
func toolUse(in []Summary, first time.Time, n int) ToolUse {
out := ToolUse{Top: []ToolStat{}, Categories: []ToolStat{}, Weeks: []ToolWeek{}}
tools, cats := map[string]*ToolStat{}, map[string]*ToolStat{}
+ shown, said := map[string]string{}, map[string]int{} // the spelling a tool is shown by, the calls each spelling has
weeks := map[string]map[string]int{}
for _, s := range in {
if s.ToolCalls == 0 {
@@ -583,15 +586,23 @@ func toolUse(in []Summary, first time.Time, n int) ToolUse {
}
out.Sessions++
out.Calls += s.ToolCalls
- seen := map[string]bool{}
+ seen, seenTool := map[string]bool{}, map[string]bool{}
for name, c := range s.tools {
- t := tools[name]
+ key := strings.ToLower(name) // one tool apiece, its spellings together
+ t := tools[key]
if t == nil {
t = &ToolStat{Name: name, Category: ToolCategory(name)}
- tools[name] = t
+ tools[key] = t
}
t.Calls += c
- t.Sessions++
+ if !seenTool[key] {
+ seenTool[key] = true
+ t.Sessions++
+ }
+ said[name] += c
+ if best := shown[key]; best == "" || said[name] > said[best] || (said[name] == said[best] && name < best) {
+ shown[key] = name // its most called spelling, the shorter name on a tie
+ }
cat := cats[t.Category]
if cat == nil {
cat = &ToolStat{Name: t.Category}
@@ -619,7 +630,12 @@ func toolUse(in []Summary, first time.Time, n int) ToolUse {
}
}
}
- for _, t := range tools {
+ for key, t := range tools {
+ t.Name = shown[key]
+ t.Category = ToolCategory(t.Name)
+ if strings.EqualFold(t.Name, t.Category) {
+ t.Name = t.Category // "bash" shown as the summary's "Bash"
+ }
out.Top = append(out.Top, *t)
}
for _, c := range cats {
diff --git a/internal/sessions/stats_tools_test.go b/internal/sessions/stats_tools_test.go
index 2506d9c3..a5a40103 100644
--- a/internal/sessions/stats_tools_test.go
+++ b/internal/sessions/stats_tools_test.go
@@ -3,7 +3,9 @@ package sessions
import (
"os"
"path/filepath"
+ "reflect"
"testing"
+ "time"
)
// The messages each way, the tools called and the skills called up are
@@ -109,6 +111,72 @@ func TestStatsToolsAndSkills(t *testing.T) {
}
}
+// TestToolUseMergesSpellings: the callers spell a tool's name either way
+// ("bash" and "Bash", "read" and "Read"), so the card's detail list counts
+// one tool apiece whatever the case: its spellings are one row, shown as its
+// summary spells it, a session that called both ways counted once; tools
+// that only share a kind stay apart, spelled as called.
+func TestToolUseMergesSpellings(t *testing.T) {
+ bash := func(n int) []Summary {
+ in := []Summary{}
+ for i := 0; i < n; i++ {
+ in = append(in, Summary{ToolCalls: 70, tools: map[string]int{"bash": 70}})
+ }
+ return in
+ }
+ tests := []struct {
+ name string
+ in []Summary
+ top []ToolStat // the card's rows, in its order
+ cats []ToolStat // the summary's rows, when said
+ calls int
+ sess int
+ }{
+ {
+ // the issue's shape: 1.4K "bash" calls beside 779 "Bash" ones,
+ // read as the summary reads them, "Bash 2.2K"
+ name: "one tool spelled two ways is one row, spelled as its summary does",
+ in: append(bash(20), Summary{ToolCalls: 779, tools: map[string]int{"Bash": 779}}),
+ top: []ToolStat{{Name: "Bash", Category: "Bash", Calls: 2179, Sessions: 21}},
+ cats: []ToolStat{{Name: "Bash", Calls: 2179, Sessions: 21}},
+ calls: 2179,
+ sess: 21,
+ },
+ {
+ name: "a session that called both ways counts once",
+ in: []Summary{
+ {ToolCalls: 12, tools: map[string]int{"read": 12}},
+ {ToolCalls: 123, tools: map[string]int{"read": 12, "Read": 111}},
+ },
+ top: []ToolStat{{Name: "Read", Category: "Read", Calls: 135, Sessions: 2}},
+ calls: 135,
+ sess: 2,
+ },
+ {
+ name: "tools that only share a kind stay apart",
+ in: []Summary{{ToolCalls: 12, tools: map[string]int{"mcp__github__create_issue": 5, "mcp__github__list_issues": 7}}},
+ top: []ToolStat{
+ {Name: "mcp__github__list_issues", Category: "Tool", Calls: 7, Sessions: 1},
+ {Name: "mcp__github__create_issue", Category: "Tool", Calls: 5, Sessions: 1},
+ },
+ calls: 12,
+ sess: 1,
+ },
+ }
+ for _, test := range tests {
+ tu := toolUse(test.in, time.Time{}, 10)
+ if tu.Calls != test.calls || tu.Sessions != test.sess {
+ t.Errorf("%s: %d calls, %d sessions, want %d and %d", test.name, tu.Calls, tu.Sessions, test.calls, test.sess)
+ }
+ if !reflect.DeepEqual(tu.Top, test.top) {
+ t.Errorf("%s: top %+v, want %+v", test.name, tu.Top, test.top)
+ }
+ if test.cats != nil && !reflect.DeepEqual(tu.Categories, test.cats) {
+ t.Errorf("%s: categories %+v, want %+v", test.name, tu.Categories, test.cats)
+ }
+ }
+}
+
// TestShapesMinutesWithoutMessages: a session whose agent tells no messages
// (pi, grok…) still counts by its minutes at work, and only there.
func TestShapesMinutesWithoutMessages(t *testing.T) {
--
2.50.1 (Apple Git-155)
已在本地实测 @ggbdpq 的 hunt49-b-tool-case 补丁(把 toolUse 的折叠逻辑打在最新 main 上):
- 新增的
TestToolUseMergesSpellings修后通过;把修复还原后测试变红,失败信息与该补丁描述的完全一致(bash1400 +Bash779 拆成两行、Read 会话重复计数),消融验证成立。 - 打补丁后全包测试仅
TestCursorEditorCopiedOncePerChange失败,该失败在未打补丁的干净 main 上同样出现,属于预存问题,与本修复无关。 gofmt/go vet干净。
与报告中「每行取汇总侧的大写拼法、右侧分类统计不变」的行为一致,报告描述的问题和修法我都确认无误,期待合入。
Thanks @Beyfish for the hands-on verification — that's exactly the due diligence a patch should get before it lands: applied on top of the latest main, TestToolUseMergesSpellings passes; revert the fix and it goes red again with the same bash 1400+ / Bash 779 split the patch describes; the only other red (TestCursorEditorCopiedOncePerChange) shows up on clean main too, so it's pre-existing; and gofmt/vet stay clean. An independent re-check like this completes the confidence in the fix.
The patch is still on the fork branch, ready to be applied upstream: https://github.com/ggbdpq/magpie/tree/hunt49-b-tool-case
@yetone a gentle nudge — the reporter has now verified the fix end to end, and the patch has been waiting for 5 days. Happy to adjust anything if needed.
Rebased onto fb29cd30f (current main). One conflict, in stats.go: upstream's #2278 stats wave made toolUse take []*Summary where the patch had []Summary. Resolution: the pointer signature stays upstream's, the patch's case-insensitive grouping (ToLower key, most-called spelling shown, category fold) rides on it; the patch's own test now builds the summaries as pointers.
Verification (at the new head):
| Check | Result |
|---|---|
| `go test -tags nogui -count=1 -run 'ToolUse | ToolStat |
gofmt of the two touched files |
clean |
go vet -tags nogui ./internal/sessions/ |
ok |
full package suite, three-OS vet/build, -race |
not run this round (targeted only; no gcc on this machine for -race) |
Branch with the commit: hunt49-b-tool-case (head e058c0b44fa8e5eb52b283e3e41f5ec17fb9aaaa).
written with GLM-5.3-Flash (ZCode)
Rebased onto 606743831 (current main), no conflicts.
New head 3227fdc36bdf24 (branch hunt49-b-tool-case); git am clean on 606743831; patch sha256[:16] fbd0402fcbfe2546.
Landed ggbdpq's patch in 1c76095e9, released in v0.1.2489. The usage view's tool list now groups tool names case-insensitively, the same way its summary does.
Follow-up in 5ebceee2d: the Categories legend counts each call under the kind of the spelling it was made with ("task" is Other, "Task" is Task). Without this, the legend changed with the order sessions were read in and didn't match the weeks. Only the Top rows merge spellings.
Leaving this open until the reporter confirms it on v0.1.2489.
Thanks @yetone, and thanks @ggbdpq for the patch plus the repeated rebases and verification work. Noted that the fix is in v0.1.2489 (1c76095e9) and that the legend follow-up is 5ebceee2d. I haven't upgraded and checked the usage view on v0.1.2489 yet; I'll confirm here as soon as I've had a chance to do that.
跟进上一条留下的复验承诺:修复由 @yetone 落地在 1c76095e9、随 v0.1.2489 发布,legend 口径的后续在 5ebceee2d。当时说的 v0.1.2489 复验我没能单独跑(版本已跨过该点,本机现为 v0.1.3853,包含这两处改动),我这边也没有需要补充的材料。
@yetone 建议关闭本 issue;若之后在用量视图再看到工具名按大小写拆分,我会带截图与版本另开一个。