Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 10 additions & 1 deletion src/claude/settings-merge.ts
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,16 @@ function hookBlocks(command: (arg: string) => string): Record<string, Json[]> {
// 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__<server>__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_<name>_<server>__…`). 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
Expand Down
17 changes: 16 additions & 1 deletion test/claude-settings-merge.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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');
Expand Down
2 changes: 1 addition & 1 deletion test/trail-init-upgrade.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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"],
Expand Down