From d12137e1b7589bba2cccc57a01c2ed38f32dbcef Mon Sep 17 00:00:00 2001 From: Misty-Star <144589976+Misty-Star@users.noreply.github.com> Date: Tue, 3 Nov 2026 18:59:28 +0800 Subject: [PATCH] fix: adapt Grok hooks for Windows tool execution --- .grok-plugin/README.md | 6 ++++ scripts/lib/install-targets/grok-home.js | 11 +++---- scripts/lib/install/content-transform.js | 39 ++++++++++++++++++++++ tests/lib/grok-canonical-install.test.js | 14 ++++++-- tests/lib/grok-harness-adapter.test.js | 41 ++++++++++++++++++++++++ 5 files changed, 103 insertions(+), 8 deletions(-) diff --git a/.grok-plugin/README.md b/.grok-plugin/README.md index 380f0b96e..0ce8cc813 100644 --- a/.grok-plugin/README.md +++ b/.grok-plugin/README.md @@ -69,6 +69,12 @@ The Grok target adapter maps it onto the shared root contract in the installed hook configuration when hooks are consented. Without hook consent, the installed copy has no `hooks/hooks.json`. +The Grok-only hook projection maps shared `Bash` and `PowerShell` matchers to +Grok's `run_terminal_command` tool. On Windows it starts hook commands with the +Node executable used by the canonical installer, so hook startup does not rely +on Grok's inherited `PATH`. Other harnesses continue to use the shared hook +configuration unchanged. + Grok does not honor Claude `userConfig`. Hook enablement and profile stay on environment variables after explicit hook consent: diff --git a/scripts/lib/install-targets/grok-home.js b/scripts/lib/install-targets/grok-home.js index a16f12435..df9885d93 100644 --- a/scripts/lib/install-targets/grok-home.js +++ b/scripts/lib/install-targets/grok-home.js @@ -10,11 +10,10 @@ const { normalizeRelativePath, } = require('./helpers'); -function grokHookBoundaryTransform() { +function grokHookTransform() { return { - type: 'text-replace', - search: 'var e=process.env.CLAUDE_PLUGIN_ROOT;', - replace: 'var e=process.env.GROK_PLUGIN_ROOT||process.env.CLAUDE_PLUGIN_ROOT;', + type: 'grok-hooks', + ...(process.platform === 'win32' ? { nodeExecutable: process.execPath } : {}), }; } @@ -91,7 +90,7 @@ module.exports = createInstallTargetAdapter({ ...operation, destinationPath: path.join(adapter.resolveRoot(input), 'plugins', 'ecc', sourceRelativePath), ...(normalizeRelativePath(sourceRelativePath) === 'hooks/hooks.json' - ? { contentTransform: grokHookBoundaryTransform() } + ? { contentTransform: grokHookTransform() } : {}), }; }); @@ -103,7 +102,7 @@ module.exports = createInstallTargetAdapter({ operations.push({ ...adapter.createScaffoldOperation('hooks-runtime', 'hooks/hooks.json', input), destinationPath: hooksManifestPath, - contentTransform: grokHookBoundaryTransform(), + contentTransform: grokHookTransform(), }); } operations.push({ diff --git a/scripts/lib/install/content-transform.js b/scripts/lib/install/content-transform.js index cb78368ab..b3bfa3eaf 100644 --- a/scripts/lib/install/content-transform.js +++ b/scripts/lib/install/content-transform.js @@ -90,6 +90,44 @@ function transformJsonFilterObjectKeys(contract, content, sourceRelativePath) { return formatJson(replaceObjectAtPath(parsed, contract.path, filtered)); } +function transformGrokHooks(contract, content, sourceRelativePath) { + const config = parseTransformObject(content, sourceRelativePath); + if (!config.hooks || typeof config.hooks !== 'object' || Array.isArray(config.hooks)) { + throw new Error(`Invalid Grok hooks config: ${sourceRelativePath}`); + } + const nodeExecutable = typeof contract.nodeExecutable === 'string' && contract.nodeExecutable + ? `"${contract.nodeExecutable}"` + : null; + const matcherMap = { Bash: 'run_terminal_command', PowerShell: 'run_terminal_command' }; + const mapMatcherName = (name) => Object.hasOwn(matcherMap, name) ? matcherMap[name] : name; + const mapMatcher = (matcher) => { + if (Array.isArray(matcher)) return [...new Set(matcher.map(mapMatcherName))]; + if (typeof matcher !== 'string') return matcher; + if (/^[\w-]+(?:\|[\w-]+)*$/.test(matcher)) { + return [...new Set(matcher.split('|').map(mapMatcherName))].join('|'); + } + return matcher.replace(/\b(Bash|PowerShell)\b/g, (name) => mapMatcherName(name)); + }; + const hooks = Object.fromEntries(Object.entries(config.hooks).map(([event, entries]) => [ + event, + Array.isArray(entries) ? entries.map((entry) => { + const matcher = mapMatcher(entry.matcher); + return { + ...entry, + ...(matcher === undefined ? {} : { matcher }), + hooks: Array.isArray(entry.hooks) ? entry.hooks.map((hook) => { + if (hook.type !== 'command' || typeof hook.command !== 'string') return hook; + const command = hook.command + .replaceAll('var e=process.env.CLAUDE_PLUGIN_ROOT;', 'var e=process.env.GROK_PLUGIN_ROOT||process.env.CLAUDE_PLUGIN_ROOT;') + .replace(/^node(?=\s)/, nodeExecutable || 'node'); + return { ...hook, command }; + }) : entry.hooks, + }; + }) : entries, + ])); + return formatJson({ ...config, hooks }); +} + function transformByContract(contract, content, sourceRelativePath) { if (!contract || typeof contract !== 'object' || Array.isArray(contract)) { throw new Error('Invalid install content transform contract'); @@ -98,6 +136,7 @@ function transformByContract(contract, content, sourceRelativePath) { 'text-replace': transformTextReplace, 'json-set': transformJsonSet, 'json-filter-object-keys': transformJsonFilterObjectKeys, + 'grok-hooks': transformGrokHooks, }; const handler = handlers[contract.type]; if (!handler) throw new Error(`Unknown install content transform contract: ${contract.type}`); diff --git a/tests/lib/grok-canonical-install.test.js b/tests/lib/grok-canonical-install.test.js index 5329885c3..bb59fc1a3 100644 --- a/tests/lib/grok-canonical-install.test.js +++ b/tests/lib/grok-canonical-install.test.js @@ -168,7 +168,7 @@ function runTests() { assert.ok(installOperations.every(operation => typeof operation.contentTransform === 'object')); assert.ok(installOperations.every(operation => !Object.keys(operation).some(key => key.startsWith('grok')))); assert.ok(installOperations.some(operation => operation.contentTransform.type === 'json-set')); - assert.ok(installOperations.some(operation => operation.contentTransform.type === 'text-replace')); + assert.ok(installOperations.some(operation => operation.contentTransform.type === 'grok-hooks')); assert.ok(installOperations.some(operation => operation.contentTransform.type === 'json-filter-object-keys')); const manifest = JSON.parse(fs.readFileSync(path.join(installedRoot(fixture), '.grok-plugin', 'plugin.json'), 'utf8')); @@ -179,7 +179,17 @@ function runTests() { assert.ok(!JSON.stringify(mcp).includes('chrome-devtools')); const installedHooks = fs.readFileSync(path.join(installedRoot(fixture), 'hooks', 'hooks.json'), 'utf8'); assert.ok(installedHooks.includes('GROK_PLUGIN_ROOT')); - assert.ok(!fs.readFileSync(path.join(repoRoot, 'hooks', 'hooks.json'), 'utf8').includes('GROK_PLUGIN_ROOT')); + const installedHooksConfig = JSON.parse(installedHooks); + const terminalHooks = installedHooksConfig.hooks.PreToolUse.filter(entry => entry.matcher === 'run_terminal_command'); + assert.strictEqual(terminalHooks.length, 2); + assert.ok(!installedHooksConfig.hooks.PreToolUse.some(entry => ['Bash', 'PowerShell'].includes(entry.matcher))); + if (process.platform === 'win32') { + assert.ok(terminalHooks.every(entry => entry.hooks[0].command.startsWith(`"${process.execPath}" `))); + } + const sharedHooks = fs.readFileSync(path.join(repoRoot, 'hooks', 'hooks.json'), 'utf8'); + assert.ok(!sharedHooks.includes('GROK_PLUGIN_ROOT')); + assert.ok(sharedHooks.includes('"matcher": "Bash"')); + assert.ok(sharedHooks.includes('"matcher": "PowerShell"')); const inspection = inspectWithGrok(fixture); if (inspection) { const plugin = inspection.plugins.find((item) => item.name === 'ecc'); diff --git a/tests/lib/grok-harness-adapter.test.js b/tests/lib/grok-harness-adapter.test.js index 429c3a8e2..3f1d0e87c 100644 --- a/tests/lib/grok-harness-adapter.test.js +++ b/tests/lib/grok-harness-adapter.test.js @@ -16,6 +16,7 @@ const { INLINE_RESOLVE, resolveEccRoot, } = require('../../scripts/lib/resolve-ecc-root'); +const { transformInstallContent } = require('../../scripts/lib/install/content-transform'); const repoRoot = path.resolve(__dirname, '..', '..'); let passed = 0; @@ -98,6 +99,46 @@ function runTests() { assert.ok(rootMcp.mcpServers['chrome-devtools']); })) passed++; else failed++; + if (test('Grok hook projection maps terminal tools and pins Windows Node without mutating source', () => { + const source = { + hooks: { + PreToolUse: [ + { matcher: 'Bash', hooks: [{ type: 'command', command: 'node -e "runBash()"' }] }, + { matcher: 'PowerShell', hooks: [{ type: 'command', command: 'node scripts/check.js' }] }, + { matcher: 'Bash|PowerShell|Write', hooks: [{ type: 'command', command: 'node scripts/combined.js' }] }, + { matcher: '^(Bash|PowerShell)$', hooks: [{ type: 'command', command: 'node scripts/regex.js' }] }, + { matcher: 'Write', hooks: [{ type: 'command', command: 'node scripts/write-check.js' }] }, + ], + }, + }; + const original = JSON.stringify(source); + const projected = JSON.parse(transformInstallContent({ + sourceRelativePath: 'hooks/hooks.json', + contentTransform: { + type: 'grok-hooks', + nodeExecutable: 'C:\\Program Files\\nodejs\\node.exe', + }, + }, JSON.stringify(source))); + const terminal = projected.hooks.PreToolUse.filter((entry) => entry.matcher === 'run_terminal_command'); + + assert.strictEqual(terminal.length, 2); + assert.ok(terminal.every((entry) => entry.hooks[0].command.startsWith('"C:\\Program Files\\nodejs\\node.exe" '))); + assert.strictEqual(projected.hooks.PreToolUse[2].matcher, 'run_terminal_command|Write'); + assert.ok(projected.hooks.PreToolUse[2].hooks[0].command.startsWith('"C:\\Program Files\\nodejs\\node.exe" ')); + assert.strictEqual(projected.hooks.PreToolUse[3].matcher, '^(run_terminal_command|run_terminal_command)$'); + assert.strictEqual(projected.hooks.PreToolUse[4].matcher, 'Write'); + assert.strictEqual(JSON.stringify(source), original); + })) passed++; else failed++; + + if (test('Grok hook projection keeps Node lookup portable outside Windows', () => { + const source = { hooks: { SessionStart: [{ matcher: '.*', hooks: [{ type: 'command', command: 'node -e "run()"' }] }] } }; + const projected = JSON.parse(transformInstallContent({ + sourceRelativePath: 'hooks/hooks.json', + contentTransform: { type: 'grok-hooks' }, + }, JSON.stringify(source))); + assert.strictEqual(projected.hooks.SessionStart[0].hooks[0].command, 'node -e "run()"'); + })) passed++; else failed++; + if (test('marketplace source is the pinned whole repository', () => { const source = readPinnedMarketplaceSource(repoRoot); assert.strictEqual(source.url, 'https://github.com/affaan-m/ECC.git');