@zgeoff/skill-gate 0.2.1 → 0.2.3

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@zgeoff/skill-gate",
3
- "version": "0.2.1",
3
+ "version": "0.2.3",
4
4
  "description": "A Claude Code PreToolUse hook that denies an edit until the session loads the skills the path needs, and warns on a Bash command that writes a gated path",
5
5
  "keywords": [
6
6
  "claude-code",
@@ -14,15 +14,13 @@
14
14
  "directory": "packages/skill-gate"
15
15
  },
16
16
  "files": [
17
- "src",
18
- "!src/*.test.ts",
19
17
  "dist"
20
18
  ],
21
19
  "type": "module",
22
20
  "exports": {
23
21
  ".": {
24
22
  "types": "./dist/index.d.ts",
25
- "default": "./src/index.ts"
23
+ "default": "./dist/index.js"
26
24
  }
27
25
  },
28
26
  "publishConfig": {
@@ -30,17 +28,15 @@
30
28
  },
31
29
  "scripts": {
32
30
  "build": "rm -rf dist && bun run build:outdir dist",
33
- "build:outdir": "tsc -p tsconfig.build.json --outDir",
31
+ "build:outdir": "sh -c 'bun build src/index.ts --target bun --outdir \"$0\" && tsc -p tsconfig.build.json --outDir \"$0\"'",
34
32
  "prepare": "bun run build",
35
33
  "typecheck": "tsc"
36
34
  },
37
- "dependencies": {
38
- "@zgeoff/std": "0.3.0",
39
- "ts-pattern": "5.9.0"
40
- },
41
35
  "devDependencies": {
42
36
  "@types/bun": "1.4.2",
37
+ "@zgeoff/std": "0.3.0",
43
38
  "tiny-invariant": "1.3.3",
39
+ "ts-pattern": "5.9.0",
44
40
  "typescript": "7.0.2"
45
41
  },
46
42
  "engines": {
@@ -1,41 +0,0 @@
1
- import { formatSkillList } from './format-skill-list.ts';
2
- import type { SkillGateHookOutput } from './types.ts';
3
-
4
- const RULES_PATH = '.claude/skill-gate.json';
5
-
6
- // Builds the PreToolUse hook output for a Bash command, or null when it writes no gated path. The
7
- // command still runs: the hook only reminds the agent, because a shell parser misses some writes.
8
- export function buildBashSkillGateOutput(
9
- gatedPaths: readonly string[],
10
- missing: readonly string[],
11
- context: { readonly compacted?: boolean } = {},
12
- ): SkillGateHookOutput | null {
13
- if (gatedPaths.length === 0) {
14
- return null;
15
- }
16
-
17
- const subject =
18
- gatedPaths.length === 1
19
- ? `${gatedPaths[0]} is a gated path, so change it`
20
- : `${gatedPaths.join(', ')} are gated paths, so change them`;
21
-
22
- const load = buildLoadInstruction(missing, context.compacted === true);
23
- const rule = `${subject} with the Edit, Write or MultiEdit tool, never through Bash.`;
24
-
25
- return {
26
- hookSpecificOutput: {
27
- hookEventName: 'PreToolUse',
28
- additionalContext: `skill-gate: this Bash command writes a gated path and runs anyway.${load} ${rule} ${RULES_PATH} lists the skills each path needs.`,
29
- },
30
- };
31
- }
32
-
33
- function buildLoadInstruction(missing: readonly string[], compacted: boolean): string {
34
- if (missing.length === 0) {
35
- return '';
36
- }
37
-
38
- return compacted
39
- ? ` Load the ${formatSkillList(missing)} again with the Skill tool before the next change. This session was compacted, and a skill loaded before the compaction no longer counts.`
40
- : ` Load the ${formatSkillList(missing)} with the Skill tool before the next change.`;
41
- }
@@ -1,54 +0,0 @@
1
- import { formatSkillList } from './format-skill-list.ts';
2
- import type { SkillGateHookOutput, SkillGateOutputContext, SkillGatePlan } from './types.ts';
3
-
4
- const RULES_PATH = '.claude/skill-gate.json';
5
-
6
- // A gated path changes only through these tools; the gate warns on a Bash write and blocks an edit.
7
- const EDIT_TOOLS_NOTE =
8
- 'Change a gated path only with Edit, Write or MultiEdit, never through Bash.';
9
-
10
- // Builds the PreToolUse hook output for one edit, or null when the edit goes ahead in silence. A
11
- // missing skill denies the edit. A broken rules file or a rule that names a skill the repo does
12
- // not have never denies: it warns the user and the agent, and the edit goes ahead.
13
- export function buildSkillGateOutput(
14
- filePath: string,
15
- plan: SkillGatePlan,
16
- context: SkillGateOutputContext = {},
17
- ): SkillGateHookOutput | null {
18
- const warnings = [
19
- ...(context.rulesError === undefined
20
- ? []
21
- : [`skill-gate: ${RULES_PATH} ${context.rulesError}.`]),
22
- ...plan.unknown.map(
23
- (entry) =>
24
- `skill-gate: the rule "${entry.match}" in ${RULES_PATH} names the skill "${entry.skill}", but .claude/skills/${entry.skill}/SKILL.md does not exist. Fix the rule or add the skill; until then the gate skips that skill.`,
25
- ),
26
- ];
27
-
28
- const warning = warnings.length === 0 ? undefined : warnings.join('\n');
29
-
30
- if (plan.missing.length > 0) {
31
- const reason =
32
- context.compacted === true
33
- ? `Load the ${formatSkillList(plan.missing)} again with the Skill tool before editing ${filePath}, then retry the edit. This session was compacted, and a skill loaded before the compaction no longer counts. ${EDIT_TOOLS_NOTE} ${RULES_PATH} lists the skills each path needs.`
34
- : `Load the ${formatSkillList(plan.missing)} with the Skill tool before editing ${filePath}, then retry the edit. ${EDIT_TOOLS_NOTE} ${RULES_PATH} lists the skills each path needs.`;
35
-
36
- return {
37
- ...(warning === undefined ? {} : { systemMessage: warning }),
38
- hookSpecificOutput: {
39
- hookEventName: 'PreToolUse',
40
- permissionDecision: 'deny',
41
- permissionDecisionReason: warning === undefined ? reason : `${reason}\n${warning}`,
42
- },
43
- };
44
- }
45
-
46
- if (warning === undefined) {
47
- return null;
48
- }
49
-
50
- return {
51
- systemMessage: warning,
52
- hookSpecificOutput: { hookEventName: 'PreToolUse', additionalContext: warning },
53
- };
54
- }
@@ -1,19 +0,0 @@
1
- import type { SkillGateRules } from './types.ts';
2
-
3
- // Collects the skills that every gate matching a path names, in gate order and with repeats. A
4
- // path that holds an ignore segment matches no gate.
5
- export function collectGateSkills(rules: SkillGateRules, relativePath: string): readonly string[] {
6
- if (rules.ignore.some((segment) => relativePath.includes(segment))) {
7
- return [];
8
- }
9
-
10
- return rules.gates
11
- .filter((gate) => isGlobMatch(gate.match, relativePath))
12
- .flatMap((gate) => gate.skills);
13
- }
14
-
15
- function isGlobMatch(pattern: string, relativePath: string): boolean {
16
- const glob = new Bun.Glob(pattern);
17
-
18
- return glob.match(relativePath);
19
- }
@@ -1,98 +0,0 @@
1
- import { isRecord } from '@zgeoff/std';
2
- import type { LoadedSkills } from './types.ts';
3
-
4
- // Collects the skills a transcript loaded since its last compaction, which voids earlier loads even
5
- // though Claude Code re-attaches their text. A skill loads through a Skill tool call that succeeded,
6
- // or a slash command the user types; a subagent's transcript has no user to type one.
7
- export function collectLoadedSkills(transcript: string): LoadedSkills {
8
- const entries = transcript.split('\n').map((line) => parseEntry(line));
9
- const lastBoundary = entries.findLastIndex((entry) => isCompactBoundary(entry));
10
- const current = entries.slice(lastBoundary + 1);
11
- const succeeded = collectSucceededToolUseIDs(current);
12
- const skills = current.flatMap((entry) => collectEntrySkills(entry, succeeded));
13
-
14
- return { skills: [...new Set(skills)], compacted: lastBoundary !== -1 };
15
- }
16
-
17
- function parseEntry(line: string): unknown {
18
- try {
19
- return JSON.parse(line);
20
- } catch {
21
- return null;
22
- }
23
- }
24
-
25
- function isCompactBoundary(entry: unknown): boolean {
26
- return isRecord(entry) && entry['type'] === 'system' && entry['subtype'] === 'compact_boundary';
27
- }
28
-
29
- function collectSucceededToolUseIDs(entries: readonly unknown[]): readonly string[] {
30
- return entries
31
- .flatMap((entry) => collectContentBlocks(entry, 'user'))
32
- .filter((block) => block['type'] === 'tool_result' && block['is_error'] !== true)
33
- .flatMap((block) => (typeof block['tool_use_id'] === 'string' ? [block['tool_use_id']] : []));
34
- }
35
-
36
- function collectContentBlocks(
37
- entry: unknown,
38
- type: string,
39
- ): readonly Readonly<Record<string, unknown>>[] {
40
- if (!isRecord(entry) || entry['type'] !== type || !isRecord(entry['message'])) {
41
- return [];
42
- }
43
-
44
- const content = entry['message']['content'];
45
-
46
- return Array.isArray(content) ? content.filter((block: unknown) => isRecord(block)) : [];
47
- }
48
-
49
- function collectEntrySkills(entry: unknown, succeeded: readonly string[]): readonly string[] {
50
- const command = findSlashCommand(entry);
51
-
52
- if (command !== undefined) {
53
- return [command];
54
- }
55
-
56
- return collectContentBlocks(entry, 'assistant').flatMap((block) =>
57
- collectSkillCall(block, succeeded),
58
- );
59
- }
60
-
61
- // A slash command opens the user message with its tags, in either order.
62
- const SLASH_COMMAND_PATTERN =
63
- /^\s*(?:<command-message>[^<]*<\/command-message>\s*)?<command-name>\/(?<skill>[^<\s]+)<\/command-name>/u;
64
-
65
- function findSlashCommand(entry: unknown): string | undefined {
66
- if (
67
- !isRecord(entry) ||
68
- entry['type'] !== 'user' ||
69
- entry['isSidechain'] === true ||
70
- !isRecord(entry['message'])
71
- ) {
72
- return undefined;
73
- }
74
-
75
- const content = entry['message']['content'];
76
-
77
- return typeof content === 'string'
78
- ? SLASH_COMMAND_PATTERN.exec(content)?.groups?.['skill']
79
- : undefined;
80
- }
81
-
82
- function collectSkillCall(
83
- block: Readonly<Record<string, unknown>>,
84
- succeeded: readonly string[],
85
- ): readonly string[] {
86
- const input = block['input'];
87
- const id = block['id'];
88
-
89
- if (block['type'] !== 'tool_use' || block['name'] !== 'Skill' || !isRecord(input)) {
90
- return [];
91
- }
92
-
93
- const skill = input['skill'];
94
-
95
- return typeof skill === 'string' && typeof id === 'string' && succeeded.includes(id)
96
- ? [skill]
97
- : [];
98
- }