Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .github/workflows/lint.yml
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
name: Lint SKILL.md
name: Lint and guard tests

on:
pull_request:
Expand Down
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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/<drive>/` paths map on Windows, bash installers reject an invalid `--scope` (#110)

## [1.5.0] - 2026-11-19

### Added
Expand Down
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
26 changes: 17 additions & 9 deletions hooks/lib/manage/commands.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -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 = [];
Expand All @@ -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' : ''})`);
Expand Down Expand Up @@ -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;
Expand Down
2 changes: 2 additions & 0 deletions hooks/lib/manage/registry.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -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 };
}
Expand Down
5 changes: 3 additions & 2 deletions hooks/lib/paths.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
2 changes: 1 addition & 1 deletion hooks/test/check.test.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -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 = [];
Expand Down
11 changes: 11 additions & 0 deletions hooks/test/doctor.test.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -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 });
Expand Down
2 changes: 1 addition & 1 deletion hooks/test/levels.test.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -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 }));
Expand Down
21 changes: 21 additions & 0 deletions hooks/test/manage.test.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,27 @@ 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('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);
Expand Down
9 changes: 9 additions & 0 deletions hooks/test/shell.test.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -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');
});
5 changes: 5 additions & 0 deletions scripts/install_linux.sh
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
5 changes: 5 additions & 0 deletions scripts/install_macos.sh
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
Loading