The agent harness performance optimization system. Skills, instincts, memory, security, and research-first development for Claude Code, Codex, Opencode, Cursor and beyond.
fix: Windows path support, error handling, and dedup in validators
Affaan Mustafa639c9aaca375
diff --git a/scripts/ci/validate-hooks.js b/scripts/ci/validate-hooks.jsindex f91f9407c..dc3f6f424 100644--- a/scripts/ci/validate-hooks.js+++ b/scripts/ci/validate-hooks.js@@ -10,6 +10,39 @@ const vm = require('vm');const HOOKS_FILE = path.join(__dirname, '../../hooks/hooks.json');const VALID_EVENTS = ['PreToolUse', 'PostToolUse', 'PreCompact', 'SessionStart', 'SessionEnd', 'Stop', 'Notification', 'SubagentStop'];+/**+ * Validate a single hook entry has required fields and valid inline JS+ * @param {object} hook - Hook object with type and command fields+ * @param {string} label - Label for error messages (e.g., "PreToolUse[0].hooks[1]")+ * @returns {boolean} true if errors were found+ */+function validateHookEntry(hook, label) {+ let hasErrors = false;++ if (!hook.type || typeof hook.type !== 'string') {+ console.error(`ERROR: ${label} missing or invalid 'type' field`);+ hasErrors = true;+ }++ if (!hook.command || (typeof hook.command !== 'string' && !Array.isArray(hook.command))) {+ console.error(`ERROR: ${label} missing or invalid 'command' field`);+ hasErrors = true;+ } else if (typeof hook.command === 'string') {+ // Validate inline JS syntax in node -e commands+ const nodeEMatch = hook.command.match(/^node -e "(.*)"$/s);+ if (nodeEMatch) {+ try {+ new vm.Script(nodeEMatch[1].replace(/\\"/g, '"').replace(/\\n/g, '\n').replace(/\\t/g, '\t').replace(/\\\\/g, '\\'));+ } catch (syntaxErr) {+ console.error(`ERROR: ${label} has invalid inline JS: ${syntaxErr.message}`);+ hasErrors = true;+ }+ }+ }++ return hasErrors;+}+function validateHooks() {if (!fs.existsSync(HOOKS_FILE)) {console.log('No hooks.json found, skipping validation');@@ -61,26 +94,9 @@ function validateHooks() {} else {// Validate each hook entryfor (let j = 0; j < matcher.hooks.length; j++) {- const hook = matcher.hooks[j];- if (!hook.type || typeof hook.type !== 'string') {- console.error(`ERROR: ${eventType}[${i}].hooks[${j}] missing or invalid 'type' field`);+ if (validateHookEntry(matcher.hooks[j], `${eventType}[${i}].hooks[${j}]`)) {hasErrors = true;}- if (!hook.command || (typeof hook.command !== 'string' && !Array.isArray(hook.command))) {- console.error(`ERROR: ${eventType}[${i}].hooks[${j}] missing or invalid 'command' field`);- hasErrors = true;- } else if (typeof hook.command === 'string') {- // Validate inline JS syntax in node -e commands- const nodeEMatch = hook.command.match(/^node -e "(.*)"$/s);- if (nodeEMatch) {- try {- new vm.Script(nodeEMatch[1].replace(/\\"/g, '"').replace(/\\n/g, '\n').replace(/\\t/g, '\t').replace(/\\\\/g, '\\'));- } catch (syntaxErr) {- console.error(`ERROR: ${eventType}[${i}].hooks[${j}] has invalid inline JS: ${syntaxErr.message}`);- hasErrors = true;- }- }- }}}totalMatchers++;@@ -100,25 +116,8 @@ function validateHooks() {} else {// Validate each hook entryfor (let j = 0; j < hook.hooks.length; j++) {- const h = hook.hooks[j];- if (!h.type || typeof h.type !== 'string') {- console.error(`ERROR: Hook ${i}.hooks[${j}] missing or invalid 'type' field`);- hasErrors = true;- }- if (!h.command || (typeof h.command !== 'string' && !Array.isArray(h.command))) {- console.error(`ERROR: Hook ${i}.hooks[${j}] missing or invalid 'command' field`);+ if (validateHookEntry(hook.hooks[j], `Hook ${i}.hooks[${j}]`)) {hasErrors = true;- } else if (typeof h.command === 'string') {- // Validate inline JS syntax in node -e commands- const nodeEMatch = h.command.match(/^node -e "(.*)"$/s);- if (nodeEMatch) {- try {- new vm.Script(nodeEMatch[1].replace(/\\"/g, '"').replace(/\\n/g, '\n').replace(/\\t/g, '\t').replace(/\\\\/g, '\\'));- } catch (syntaxErr) {- console.error(`ERROR: Hook ${i}.hooks[${j}] has invalid inline JS: ${syntaxErr.message}`);- hasErrors = true;- }- }}}}diff --git a/scripts/lib/package-manager.d.ts b/scripts/lib/package-manager.d.tsindex ecfa78a61..90a9573fe 100644--- a/scripts/lib/package-manager.d.ts+++ b/scripts/lib/package-manager.d.ts@@ -68,7 +68,7 @@ export function getPackageManager(options?: GetPackageManagerOptions): PackageMa/*** Set the user's globally preferred package manager.* Saves to ~/.claude/package-manager.json.- * @throws If pmName is not a known package manager+ * @throws If pmName is not a known package manager or if save fails*/export function setPreferredPackageManager(pmName: PackageManagerName): { packageManager: string; setAt: string };diff --git a/scripts/lib/package-manager.js b/scripts/lib/package-manager.jsindex de4d1e43c..f767282d8 100644--- a/scripts/lib/package-manager.js+++ b/scripts/lib/package-manager.js@@ -246,7 +246,12 @@ function setPreferredPackageManager(pmName) {const config = loadConfig() || {};config.packageManager = pmName;config.setAt = new Date().toISOString();- saveConfig(config);++ try {+ saveConfig(config);+ } catch (err) {+ throw new Error(`Failed to save package manager preference: ${err.message}`);+ }return config;}diff --git a/scripts/lib/session-manager.d.ts b/scripts/lib/session-manager.d.tsindex 31e903b12..7fbbc695a 100644--- a/scripts/lib/session-manager.d.ts+++ b/scripts/lib/session-manager.d.ts@@ -97,7 +97,8 @@ export function parseSessionMetadata(content: string | null): SessionMetadata;/*** Calculate statistics for a session.- * Accepts either a file path (ending in .tmp) or pre-read content string.+ * Accepts either a file path (absolute, ending in .tmp) or pre-read content string.+ * Supports both Unix (/path/to/session.tmp) and Windows (C:\path\to\session.tmp) paths.*/export function getSessionStats(sessionPathOrContent: string): SessionStats;diff --git a/scripts/lib/session-manager.js b/scripts/lib/session-manager.jsindex e449030fd..c3331b508 100644--- a/scripts/lib/session-manager.js+++ b/scripts/lib/session-manager.js@@ -146,12 +146,14 @@ function parseSessionMetadata(content) {*/function getSessionStats(sessionPathOrContent) {// Accept pre-read content string to avoid redundant file reads.- // If the argument looks like a file path (no newlines, ends with .tmp),- // read from disk. Otherwise treat it as content.- const content = (typeof sessionPathOrContent === 'string' &&+ // If the argument looks like a file path (no newlines, ends with .tmp,+ // starts with / on Unix or drive letter on Windows), read from disk.+ // Otherwise treat it as content.+ const looksLikePath = typeof sessionPathOrContent === 'string' &&!sessionPathOrContent.includes('\n') &&- sessionPathOrContent.startsWith('/') &&- sessionPathOrContent.endsWith('.tmp'))+ sessionPathOrContent.endsWith('.tmp') &&+ (sessionPathOrContent.startsWith('/') || /^[A-Za-z]:[/\\]/.test(sessionPathOrContent));+ const content = looksLikePath? getSessionContent(sessionPathOrContent): sessionPathOrContent;