fix: adapt Grok hooks for Windows tool execution

This commit is contained in:
Misty-Star committed 2026-11-03 18:59:28 +08:00
1 parent 79bbe939df
commit d12137e1b7
5 files changed
+103 -8

No files matched your search

+6
View File
@@ -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:
+5 -6
View File
@@ -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({
+39
View File
@@ -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}`);
+12 -2
View File
@@ -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');
+41
View File
@@ -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');