From a19f78fbf05c5863f3a48e4464318e34c56bb601 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Bir=C3=B3=2C=20Csaba=20Attila?= Date: Thu, 19 Nov 2026 10:55:50 +0100 Subject: [PATCH 1/8] fix(guard): drop the empty hooks key on uninstall When our entries were the only hooks in a harness config, unregister left an empty "hooks": {} behind. Remove the key in that case. Refs #110 --- hooks/lib/manage/registry.mjs | 2 ++ hooks/test/manage.test.mjs | 9 +++++++++ 2 files changed, 11 insertions(+) diff --git a/hooks/lib/manage/registry.mjs b/hooks/lib/manage/registry.mjs index 2810692..80ea164 100644 --- a/hooks/lib/manage/registry.mjs +++ b/hooks/lib/manage/registry.mjs @@ -118,6 +118,8 @@ export function unregister(harness, env = process.env) { changed = true; if (kept.length) cfg.hooks[event] = kept; else delete cfg.hooks[event]; } + // Leave no empty "hooks": {} behind when ours were the only hooks. + if (changed && Object.keys(cfg.hooks).length === 0) delete cfg.hooks; if (changed) writeConfig(file, cfg); return { changed }; } diff --git a/hooks/test/manage.test.mjs b/hooks/test/manage.test.mjs index 1616a6a..4da41e7 100644 --- a/hooks/test/manage.test.mjs +++ b/hooks/test/manage.test.mjs @@ -87,6 +87,15 @@ test('register writes through a symlinked settings file and keeps its mode', { s assert.equal(fs.lstatSync(file).isSymbolicLink(), true); }); +test('unregister drops the hooks key when only our entries were in it', () => { + const env = makeEnv(); + const file = HARNESSES.claude.configPath(env); + fs.writeFileSync(file, JSON.stringify({ theme: 'dark' })); + register('claude', GUARD, env); + assert.equal(unregister('claude', env).changed, true); + assert.deepEqual(readJson(file), { theme: 'dark' }); +}); + test('unregister removes only ours', () => { const env = makeEnv(); const file = HARNESSES.claude.configPath(env); From 25349d4a2b7ae87f61d409d004eb83776756cea3 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Bir=C3=B3=2C=20Csaba=20Attila?= Date: Thu, 19 Nov 2026 10:55:50 +0100 Subject: [PATCH 2/8] fix(guard): handle a bad user config alike in install and repair install overwrote an unparsable user config without a backup, while repair kept a .bak copy; repair also spread a non-array harnesses value into characters. Share one backup helper and one harness-merge helper between both commands. Refs #110 --- hooks/lib/manage/commands.mjs | 26 +++++++++++++++++--------- hooks/test/doctor.test.mjs | 11 +++++++++++ hooks/test/manage.test.mjs | 12 ++++++++++++ 3 files changed, 40 insertions(+), 9 deletions(-) diff --git a/hooks/lib/manage/commands.mjs b/hooks/lib/manage/commands.mjs index 0a41acd..2d65a7c 100644 --- a/hooks/lib/manage/commands.mjs +++ b/hooks/lib/manage/commands.mjs @@ -24,6 +24,16 @@ function codexFeatureEnabled(env) { } catch { return false; } } +// Keep the user's broken file: a typo must not silently reset scope or other settings. +function backUpInvalid(user, lines) { + if (user.value || user.missing) return; + fs.copyFileSync(user.file, `${user.file}.bak`); + lines.push(`invalid user config ${user.file} (${user.error}) backed up to ${user.file}.bak`); +} + +const mergeHarnesses = (existing, registered) => + [...new Set([...(Array.isArray(existing) ? existing : []), ...registered])]; + export function install({ scope, harnesses, env = process.env } = {}) { if (scope !== undefined && scope !== 'opt-in' && scope !== 'all') throw new Error(`invalid scope ${JSON.stringify(scope)}; use opt-in or all`); const lines = []; @@ -43,13 +53,15 @@ export function install({ scope, harnesses, env = process.env } = {}) { lines.push(`${name}: error: ${e.message}; left untouched`); } } - const file = userConfigPath(env); - const existing = readUserConfig(env).value; + const user = readUserConfig(env); + backUpInvalid(user, lines); + const file = user.file; + const existing = user.value; const kept = scope === undefined && (existing?.scope === 'all' || existing?.scope === 'opt-in'); const finalScope = scope ?? (kept ? existing.scope : 'opt-in'); const cfg = { scope: finalScope, version: skillVersion(), guardPath: GUARD_PATH, - harnesses: [...new Set([...(Array.isArray(existing?.harnesses) ? existing.harnesses : []), ...registered])], + harnesses: mergeHarnesses(existing?.harnesses, registered), }; writeFileThrough(file, `${JSON.stringify(cfg, null, 2)}\n`); lines.push(`user config written to ${file} (scope: ${finalScope}${kept ? ', kept' : ''})`); @@ -263,13 +275,9 @@ export function repair({ env = process.env } = {}) { } } const user = readUserConfig(env); - if (!user.value && !user.missing) { - // Keep the user's broken file: a typo must not silently reset scope or other settings. - fs.copyFileSync(user.file, `${user.file}.bak`); - lines.push(`invalid user config ${user.file} (${user.error}) backed up to ${user.file}.bak`); - } + backUpInvalid(user, lines); const base = user.value ?? {}; - const cfg = { ...base, scope: base.scope === 'all' ? 'all' : 'opt-in', version: skillVersion(), guardPath: GUARD_PATH, harnesses: [...new Set([...(base.harnesses ?? []), ...registered])] }; + const cfg = { ...base, scope: base.scope === 'all' ? 'all' : 'opt-in', version: skillVersion(), guardPath: GUARD_PATH, harnesses: mergeHarnesses(base.harnesses, registered) }; writeFileThrough(user.file, `${JSON.stringify(cfg, null, 2)}\n`); lines.push(`user config rewritten at ${user.file} (version ${cfg.version})`); return lines; diff --git a/hooks/test/doctor.test.mjs b/hooks/test/doctor.test.mjs index cd11700..d0f2eb6 100644 --- a/hooks/test/doctor.test.mjs +++ b/hooks/test/doctor.test.mjs @@ -64,6 +64,17 @@ test('registered path pointing to a deleted file fails claude-path; repair fixes assert.ok(after.hooks.PreToolUse.some((e) => e.hooks[0].command === 'echo foreign'), 'foreign entry kept'); }); +test('repair ignores a non-array harnesses list', () => { + const env = makeEnv(); + install({ scope: 'all', env }); + const file = userConfigPath(env); + fs.writeFileSync(file, JSON.stringify({ scope: 'all', harnesses: 'claude' })); + repair({ env }); + const { harnesses } = JSON.parse(fs.readFileSync(file, 'utf8')); + assert.ok(Array.isArray(harnesses)); + assert.ok(!harnesses.includes('c'), JSON.stringify(harnesses)); +}); + test('repair backs up an invalid user config before rewriting it', () => { const env = makeEnv(); install({ scope: 'all', env }); diff --git a/hooks/test/manage.test.mjs b/hooks/test/manage.test.mjs index 4da41e7..268717a 100644 --- a/hooks/test/manage.test.mjs +++ b/hooks/test/manage.test.mjs @@ -96,6 +96,18 @@ test('unregister drops the hooks key when only our entries were in it', () => { assert.deepEqual(readJson(file), { theme: 'dark' }); }); +test('install backs up an invalid user config before rewriting it', () => { + const env = makeEnv(); + install({ scope: 'all', env }); + const file = userConfigPath(env); + fs.writeFileSync(file, '{"scope": "all",'); + const lines = install({ env }); + assert.equal(fs.readFileSync(`${file}.bak`, 'utf8'), '{"scope": "all",'); + assert.ok(lines.some((l) => l.includes(`${file}.bak`)), lines.join('\n')); + assert.equal(readJson(file).scope, 'opt-in'); + assert.ok(!install({ env }).some((l) => l.includes('.bak'))); +}); + test('unregister removes only ours', () => { const env = makeEnv(); const file = HARNESSES.claude.configPath(env); From 883bddeb66090f8d9b43e0ceb724fd071ed341c9 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Bir=C3=B3=2C=20Csaba=20Attila?= Date: Thu, 19 Nov 2026 10:56:13 +0100 Subject: [PATCH 3/8] fix(guard): map cygwin drive paths on windows Treat /cygdrive/c/... like the Git Bash /c/... form, so cd and git -C into a Cygwin path keep the protected-branch and freshness checks. Refs #110 --- hooks/lib/paths.mjs | 5 +++-- hooks/test/shell.test.mjs | 9 +++++++++ 2 files changed, 12 insertions(+), 2 deletions(-) diff --git a/hooks/lib/paths.mjs b/hooks/lib/paths.mjs index 8448982..2bc2cea 100644 --- a/hooks/lib/paths.mjs +++ b/hooks/lib/paths.mjs @@ -3,10 +3,11 @@ import path from 'node:path'; export const pathApi = (platform = process.platform) => (platform === 'win32' ? path.win32 : path.posix); -// Git Bash (how Claude Code and Codex run commands on Windows) names drives as /c/... or /C; +// Git Bash (how Claude Code and Codex run commands on Windows) names drives as /c/... or /C, +// Cygwin as /cygdrive/c/...; // Node would read that as \c\... on the current drive. Other platforms are left untouched. export function fromGitBash(p, platform = process.platform) { if (platform !== 'win32' || typeof p !== 'string') return p; - const m = /^\/([a-zA-Z])(?:\/|$)/.exec(p); + const m = /^\/(?:cygdrive\/)?([a-zA-Z])(?:\/|$)/.exec(p); return m ? `${m[1].toUpperCase()}:/${p.slice(m[0].length)}` : p; } diff --git a/hooks/test/shell.test.mjs b/hooks/test/shell.test.mjs index f97bc5c..cea965d 100644 --- a/hooks/test/shell.test.mjs +++ b/hooks/test/shell.test.mjs @@ -175,3 +175,12 @@ test('cd to a Git Bash drive path maps to the drive on win32 only', () => { const l = parseCommandLine('cd /c/Users/x && git status', { cwd: '/r', platform: 'linux' }); assert.equal(l.commands[1].cwd, '/c/Users/x'); }); + +test('cd to a Cygwin drive path maps to the drive on win32 only', () => { + const w = parseCommandLine('cd /cygdrive/c/Users/x && git status; cd /cygdrive/d && git log; cd /cygdrive && git log', { cwd: 'C:\\r', platform: 'win32' }); + assert.equal(w.commands[1].cwd, 'C:/Users/x'); + assert.equal(w.commands[3].cwd, 'D:/'); + assert.notEqual(w.commands[5].cwd, 'C:/'); + const l = parseCommandLine('cd /cygdrive/c/Users/x && git status', { cwd: '/r', platform: 'linux' }); + assert.equal(l.commands[1].cwd, '/cygdrive/c/Users/x'); +}); From 3194afec57768768db64a3fc4f708dbfac3b37a9 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Bir=C3=B3=2C=20Csaba=20Attila?= Date: Thu, 19 Nov 2026 10:56:22 +0100 Subject: [PATCH 4/8] test(guard): isolate two tests from the windows user config check.test.mjs and levels.test.mjs set only XDG_CONFIG_HOME and HOME, so on Windows a real %APPDATA% config could leak in. Set APPDATA and USERPROFILE too. Refs #110 --- hooks/test/check.test.mjs | 2 +- hooks/test/levels.test.mjs | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/hooks/test/check.test.mjs b/hooks/test/check.test.mjs index 7dc86f8..cedb894 100644 --- a/hooks/test/check.test.mjs +++ b/hooks/test/check.test.mjs @@ -16,7 +16,7 @@ const mkRepo = (name, optIn = true) => { return dir; }; const repo = mkRepo('repo'); -const env = { XDG_CONFIG_HOME: xdg, HOME: tmp }; +const env = { XDG_CONFIG_HOME: xdg, APPDATA: xdg, HOME: tmp, USERPROFILE: tmp }; const spy = (impl) => { const calls = []; diff --git a/hooks/test/levels.test.mjs b/hooks/test/levels.test.mjs index b04b93b..d83d05e 100644 --- a/hooks/test/levels.test.mjs +++ b/hooks/test/levels.test.mjs @@ -47,7 +47,7 @@ test('runCheck: "on" denies attribution; raw fallback honours warn and off', asy const tmp = fs.mkdtempSync(path.join(os.tmpdir(), 'guard-levels-')); const repo = path.join(tmp, 'repo'); fs.mkdirSync(path.join(repo, '.git'), { recursive: true }); - const env = { XDG_CONFIG_HOME: path.join(tmp, 'xdg'), HOME: tmp }; + const env = { XDG_CONFIG_HOME: path.join(tmp, 'xdg'), APPDATA: path.join(tmp, 'xdg'), HOME: tmp, USERPROFILE: tmp }; const exec = async () => ({ code: 1, stdout: '', stderr: '' }); const run = (command, rules) => { fs.writeFileSync(path.join(repo, '.git-workflow.json'), JSON.stringify({ rules })); From a9a09fb5beccdc3df0795e64a3b04106e0769c5f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Bir=C3=B3=2C=20Csaba=20Attila?= Date: Thu, 19 Nov 2026 10:56:45 +0100 Subject: [PATCH 5/8] fix(scripts): reject an invalid scope in the bash installers An unknown --scope value reached the guard, which printed "install failed" while the script exited 0. Validate opt-in|all up front and exit with usage, like the PowerShell ValidateSet. Refs #110 --- scripts/install_linux.sh | 5 +++++ scripts/install_macos.sh | 5 +++++ 2 files changed, 10 insertions(+) diff --git a/scripts/install_linux.sh b/scripts/install_linux.sh index 2de1f8c..99196ec 100755 --- a/scripts/install_linux.sh +++ b/scripts/install_linux.sh @@ -105,6 +105,11 @@ while [[ $# -gt 0 ]]; do shift done +case "$SCOPE" in + ""|opt-in|all) ;; + *) fail "Invalid --scope value: $SCOPE (use opt-in or all)"; usage ;; +esac + if [[ "$ACTION" == "uninstall" ]]; then uninstall else diff --git a/scripts/install_macos.sh b/scripts/install_macos.sh index 563873f..8bb2102 100755 --- a/scripts/install_macos.sh +++ b/scripts/install_macos.sh @@ -116,6 +116,11 @@ while [[ $# -gt 0 ]]; do shift done +case "$SCOPE" in + ""|opt-in|all) ;; + *) fail "Invalid --scope value: $SCOPE (use opt-in or all)"; usage ;; +esac + if [[ "$ACTION" == "uninstall" ]]; then uninstall else From 7114d88688d0d34dd7b2207bb01bcbf890b6b8ae Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Bir=C3=B3=2C=20Csaba=20Attila?= Date: Thu, 19 Nov 2026 10:56:54 +0100 Subject: [PATCH 6/8] ci: name the workflow after both of its jobs The workflow runs the guard tests as well as the SKILL.md lint. The README badge points at the file, so the rename does not break it; no branch protection refers to the workflow name. Refs #110 --- .github/workflows/lint.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.github/workflows/lint.yml b/.github/workflows/lint.yml index 7626d38..c2386ad 100644 --- a/.github/workflows/lint.yml +++ b/.github/workflows/lint.yml @@ -1,4 +1,4 @@ -name: Lint SKILL.md +name: Lint and guard tests on: pull_request: From a3e201d97699718ef6b8097883b80de7064d8b52 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Bir=C3=B3=2C=20Csaba=20Attila?= Date: Thu, 19 Nov 2026 10:56:54 +0100 Subject: [PATCH 7/8] docs(readme): show the absolute guard path in the command list Refs #110 --- README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/README.md b/README.md index e250890..039bf97 100644 --- a/README.md +++ b/README.md @@ -126,7 +126,7 @@ The install scripts register the guard right after the skill symlink when `node` .\scripts\install_windows.ps1 -Scope all # -NoHooks and -Uninstall work too ``` -You can also ask Claude Code ("install the guard", "is the guard working?") or run the guard directly: `node hooks/guard.mjs install|update|doctor|repair|uninstall|enable|disable`. `install` accepts `--scope opt-in|all` and `--harness claude,codex`. Re-running `install` without `--scope` keeps the scope you chose earlier. Run `doctor` to check the registration. +You can also ask Claude Code ("install the guard", "is the guard working?") or run the guard directly: `node /path/to/clone/hooks/guard.mjs install|update|doctor|repair|uninstall|enable|disable` (use the absolute path; `install` prints it, and `enable`/`disable` act on the repository of the current directory). `install` accepts `--scope opt-in|all` and `--harness claude,codex`. Re-running `install` without `--scope` keeps the scope you chose earlier. Run `doctor` to check the registration. ### Scope and per-repository config From 44f8556179dc1f9da13dbe77300ebf6fdbb2b006 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Bir=C3=B3=2C=20Csaba=20Attila?= Date: Thu, 19 Nov 2026 10:57:00 +0100 Subject: [PATCH 8/8] docs(changelog): add guard rough-edge fixes to unreleased Refs #110 --- CHANGELOG.md | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index ed9b57c..5d84d76 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -12,6 +12,10 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 - `docs/*`, `ci/*` and `perf/*` working branches for single-type changes: base and merge target `develop` (or an epic), accepted by the guard's default `branchTypes` and `pr-base` table (#103) - Small-issue bundles: two or three small, related issues may share one branch and one PR, with atomic commits per issue and one `Closes #N` line each (#98) +### Fixed + +- Guard rough edges: uninstall no longer leaves an empty `"hooks": {}`, install backs up an unparsable user config like repair, a non-array `harnesses` value is ignored, `/cygdrive//` paths map on Windows, bash installers reject an invalid `--scope` (#110) + ## [1.5.0] - 2026-11-19 ### Added