fix(codex): remove legacy hooks json update hook

This commit is contained in:
Tom Boucher
2026-05-10 17:38:03 -04:00
committed by GitHub
parent 5541ea3fb4
commit 14e47acd9c
3 changed files with 187 additions and 0 deletions

View File

@@ -759,6 +759,76 @@ function rewriteLegacyCodexHookBlock(content, absoluteRunner) {
return { content: updated, changed };
}
function isManagedCodexHookCommand(command, targetDir) {
if (typeof command !== 'string') return false;
if (typeof targetDir !== 'string' || targetDir.length === 0) return false;
const normalizedCommand = command.replace(/\\/g, '/');
const managedHooksDir = `${path.join(targetDir, 'hooks').replace(/\\/g, '/')}/`;
if (!normalizedCommand.includes(managedHooksDir)) return false;
return /(^|[\\/\s"'])(gsd-check-update\.js|gsd-update-check\.js)(?=$|[\s"'])/.test(normalizedCommand);
}
function pruneGsdManagedHooksJsonValue(value, targetDir) {
if (Array.isArray(value)) {
let changed = false;
const next = [];
for (const item of value) {
const pruned = pruneGsdManagedHooksJsonValue(item, targetDir);
if (pruned.changed) changed = true;
if (!isStructurallyEmpty(pruned.value)) next.push(pruned.value);
else changed = true;
}
return { value: next, changed };
}
if (value && typeof value === 'object') {
if (isManagedCodexHookCommand(value.command, targetDir)) {
return { value: null, changed: true };
}
let changed = false;
const next = {};
for (const [key, child] of Object.entries(value)) {
const pruned = pruneGsdManagedHooksJsonValue(child, targetDir);
if (pruned.changed) changed = true;
if (!isStructurallyEmpty(pruned.value)) next[key] = pruned.value;
else changed = true;
}
return { value: next, changed };
}
return { value, changed: false };
}
function isStructurallyEmpty(value) {
if (value === null || value === undefined) return true;
if (Array.isArray(value)) return value.length === 0;
return typeof value === 'object' && Object.keys(value).length === 0;
}
function cleanupLegacyCodexHooksJson(targetDir) {
const hooksPath = path.join(targetDir, 'hooks.json');
if (!fs.existsSync(hooksPath)) return { changed: false, removedFile: false };
let parsed;
try {
parsed = JSON.parse(fs.readFileSync(hooksPath, 'utf8'));
} catch {
return { changed: false, removedFile: false, skipped: 'invalid_json' };
}
const pruned = pruneGsdManagedHooksJsonValue(parsed, targetDir);
if (!pruned.changed) return { changed: false, removedFile: false };
if (isStructurallyEmpty(pruned.value)) {
fs.unlinkSync(hooksPath);
return { changed: true, removedFile: true };
}
atomicWriteFileSync(hooksPath, JSON.stringify(pruned.value, null, 2) + '\n', 'utf8');
return { changed: true, removedFile: false };
}
/**
* Build a hook command path using forward slashes for cross-platform compatibility.
* On Windows, $HOME is not expanded by cmd.exe/PowerShell, so we use the actual path.
@@ -8586,6 +8656,10 @@ function install(isGlobal, runtime = 'claude') {
throw wrapped;
}
console.log(` ${green}✓${reset} Configured Codex hooks (SessionStart)`);
const legacyHooksCleanup = cleanupLegacyCodexHooksJson(targetDir);
if (legacyHooksCleanup.changed) {
console.log(` ${green}✓${reset} Removed legacy GSD hooks.json entries`);
}
} catch (e) {
// #2760 — schema-validation and write failures must be loud and fatal
// so the user is never left with a config Codex refuses to load (or no
@@ -10613,6 +10687,7 @@ if (process.env.GSD_TEST_MODE) {
rewriteLegacyManagedNodeHookCommands,
buildCodexHookBlock,
rewriteLegacyCodexHookBlock,
cleanupLegacyCodexHooksJson,
};
} else {