From f237238a017f82eebaaf3736b0d8454c5eedf0d1 Mon Sep 17 00:00:00 2001 From: Hrijul Dey <44521405+hr1juldey@users.noreply.github.com> Date: Wed, 18 Nov 2026 19:22:28 +0530 Subject: [PATCH] fix(claude): tool-savings matcher never matched MCP tools MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Claude Code reads a PostToolUse matcher made of plain characters as a list of exact tool names, so `Bash|mcp__graft__|mcp__trail__|…` never fired for a real `mcp____trail_find_code` (or graft) call, and graftReads/savedTokens stayed flat after MCP-only sessions. The handler already classifies MCP names across every host prefix, so only the matcher moved: anchored regex — exact for the native tools, MCP matched by tool-name suffix so any server key fires (plugin-scoped included). The upgrade test proves a 0.23-era install re-wires to the new matcher on the next init. Users with a repo-level `mcp____.*` workaround entry should drop it after upgrading, or each MCP call will count twice. Fixes #834 --- src/claude/settings-merge.ts | 11 ++++++++++- test/claude-settings-merge.test.ts | 17 ++++++++++++++++- test/trail-init-upgrade.test.ts | 2 +- 3 files changed, 27 insertions(+), 3 deletions(-) diff --git a/src/claude/settings-merge.ts b/src/claude/settings-merge.ts index dbd5948e3..5342d9684 100644 --- a/src/claude/settings-merge.ts +++ b/src/claude/settings-merge.ts @@ -76,7 +76,16 @@ function hookBlocks(command: (arg: string) => string): Record { // but the handler no-ops instantly unless there is something to record, so an // unrelated Bash or a plain Read costs only a stdin read. `Skill` is the // agent using a skill: one of the repo's goes to the workspace as a run. - { matcher: 'Bash|mcp__graft__|mcp__trail__|Read|Grep|Glob|Skill', hooks: [{ type: 'command', command: hookCmd_('tool-savings'), timeout: 8 }] }, + // The matcher is anchored regex, not bare names: Claude Code reads a + // matcher of plain characters as a list of EXACT tool names, so a bare + // `mcp__trail__` would match only a tool literally named that and never a + // real `mcp____trail_find_code`. The native names are anchored + // (`^…$`) so they stay exact once the value is a regex, and MCP tools are + // matched by their tool-name suffix so any server key fires — including + // plugin-scoped servers (`mcp__plugin____…`). The handler + // still filters by the real tool-name list, so third-party MCP tools + // classify as neither and cost a stdin read. + { matcher: '^(Bash|Read|Grep|Glob|Skill)$|^mcp__.+__(graft|trail)_', hooks: [{ type: 'command', command: hookCmd_('tool-savings'), timeout: 8 }] }, ], // Longer than the other hooks: its `graft ask` is a real query, and a query // brings the graph up to date first (graph/refresh.ts). The hook itself diff --git a/test/claude-settings-merge.test.ts b/test/claude-settings-merge.test.ts index d05114fdb..462e2c6bf 100644 --- a/test/claude-settings-merge.test.ts +++ b/test/claude-settings-merge.test.ts @@ -17,12 +17,27 @@ test('empty settings gets the full Graft blocks', () => { // accumulator over the retrieval tools (Bash `graft …`, the graft_* MCP tools) // and the source-read tools (Read/Grep/Glob) it scores against. const savings = merged.hooks.PostToolUse[1]; - assert.equal(savings.matcher, 'Bash|mcp__graft__|mcp__trail__|Read|Grep|Glob|Skill'); + assert.equal(savings.matcher, '^(Bash|Read|Grep|Glob|Skill)$|^mcp__.+__(graft|trail)_'); assert.ok(savings.hooks[0].command.includes('tool-savings'), 'savings hook wired'); assert.equal(merged.footerLinksRegexes, undefined); assert.deepEqual(warnings, []); }); +test('the tool-savings matcher is regex: MCP tools fire under any server key, native tools stay exact', () => { + const { merged } = mergeGraftSettings({}); + const re = new RegExp(merged.hooks.PostToolUse[1].matcher); + const fired = [ + 'Bash', 'Read', 'Grep', 'Glob', 'Skill', + 'mcp__trail__trail_find_code', 'mcp__graft__graft_find_code', + 'mcp__trail-map__trail_find_code', 'mcp__plugin_pack_trail__trail_find_code', + ]; + for (const t of fired) assert.match(t, re, `${t} should fire the tool-savings hook`); + // A plain-char matcher would be read as exact names; anchored regex keeps the + // native names from leaking onto near-misses once the value is a regex. + const quiet = ['BashOutput', 'ExitPlanMode', 'mcp__trail__', 'mcp__notes__remember']; + for (const t of quiet) assert.doesNotMatch(t, re, `${t} must not fire the tool-savings hook`); +}); + test('foreign statusLine is preserved with a warning; Graft not forced in', () => { const { merged, warnings } = mergeGraftSettings({ statusLine: { type: 'command', command: 'my-bar.sh' } }); assert.equal(merged.statusLine.command, 'my-bar.sh'); diff --git a/test/trail-init-upgrade.test.ts b/test/trail-init-upgrade.test.ts index 3247f1ada..a9535eec7 100644 --- a/test/trail-init-upgrade.test.ts +++ b/test/trail-init-upgrade.test.ts @@ -64,7 +64,7 @@ function assertWired(repo: string, home: string): void { assert.deepEqual(shared.SessionStart.at(-1).hooks[0], { type: "command", command: 'node "${CLAUDE_PROJECT_DIR:-.}/.trail/hook.cjs" session-start || exit 0', timeout: 10 }); const user = read(join(home, ".claude", "settings.json")).hooks; assert.deepEqual(ours(user), { - PostToolUse: ["Write|Edit|MultiEdit:post-edit", "Bash|mcp__graft__|mcp__trail__|Read|Grep|Glob|Skill:tool-savings"], + PostToolUse: ["Write|Edit|MultiEdit:post-edit", "^(Bash|Read|Grep|Glob|Skill)$|^mcp__.+__(graft|trail)_:tool-savings"], UserPromptSubmit: [":prompt"], PreToolUse: ["Write|Edit|MultiEdit:pre-edit", "Bash:pre-bash"], PostToolUseFailure: ["Bash:post-bash-fail"],