#!/usr/bin/env node /** * patch-x12-confirm-hook-alignment.js * 验证 agent-isolation-gate.js 文件头注释与 settings.json hookEventName 一致 * 仅验证, 不修改 (除非发现不一致则更新注释) * * 幂等: 验证型补丁 */ 'use strict'; const fs = require('fs'); const path = require('path'); const HOOKS_DIR = path.join(__dirname, '..', '..', 'hooks'); const SETTINGS_PATH = path.join(__dirname, '..', '..', 'settings.json'); const GATE_PATH = path.join(HOOKS_DIR, 'agent-isolation-gate.js'); if (!fs.existsSync(GATE_PATH) || !fs.existsSync(SETTINGS_PATH)) { console.log('SKIP: files not found'); process.exit(0); } const gateSrc = fs.readFileSync(GATE_PATH, 'utf8'); const settings = JSON.parse(fs.readFileSync(SETTINGS_PATH, 'utf8')); // 从 settings.json 找 agent-isolation-gate 注册位置 const hooks = settings.hooks || {}; const registeredEvents = []; for (const [eventName, entries] of Object.entries(hooks)) { for (const entry of entries) { const entryHooks = entry.hooks || []; for (const h of entryHooks) { if (h.command && h.command.includes('agent-isolation-gate')) { registeredEvents.push({ event: eventName, matcher: entry.matcher || '*' }); } } } } if (registeredEvents.length === 0) { console.error('WARN: agent-isolation-gate not found in settings.json hooks'); process.exit(0); } console.log('Settings registration:'); for (const r of registeredEvents) { console.log(' ' + r.event + ' (matcher: ' + r.matcher + ')'); } // 检查文件头注释 const headerMatch = gateSrc.match(/\* (PreToolUse|PostToolUse|UserPromptSubmit)\b.*Hook.*\(matcher:\s*([^)]+)\)/); if (headerMatch) { const docEvent = headerMatch[1]; const docMatcher = headerMatch[2].trim(); const settingsEvent = registeredEvents[0].event; const settingsMatcher = registeredEvents[0].matcher; if (docEvent === settingsEvent && docMatcher === settingsMatcher) { console.log('OK: X12 header comment matches settings.json (' + docEvent + ' / ' + docMatcher + ')'); } else { console.error('MISMATCH: header says ' + docEvent + '/' + docMatcher + ' but settings says ' + settingsEvent + '/' + settingsMatcher); process.exit(1); } } else { console.log('WARN: header comment format not recognized, manual check needed'); }