@webpieces/ai-hook-rules 0.4.587 → 0.4.588
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/README.md +25 -7
- package/package.json +2 -2
- package/src/adapters/hook-core.js +12 -3
- package/src/adapters/hook-core.js.map +1 -1
- package/src/bin/guarantee-root.d.ts +5 -0
- package/src/bin/guarantee-root.js +13 -0
- package/src/bin/guarantee-root.js.map +1 -1
- package/src/bin/hook-registration.d.ts +134 -0
- package/src/bin/hook-registration.js +320 -0
- package/src/bin/hook-registration.js.map +1 -0
- package/src/bin/setup.d.ts +1 -15
- package/src/bin/setup.js +84 -77
- package/src/bin/setup.js.map +1 -1
- package/src/bin/shim.d.ts +1 -1
- package/src/bin/shim.js +63 -11
- package/src/bin/shim.js.map +1 -1
- package/src/bin/upgrade-shim.js +48 -14
- package/src/bin/upgrade-shim.js.map +1 -1
- package/src/core/l0-matrix.js +18 -11
- package/src/core/l0-matrix.js.map +1 -1
- package/templates/ai-hook.sh +30 -6
|
@@ -0,0 +1,320 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.REGISTRATION_SURFACE = exports.GUARANTEE_ROOT_SURFACE = exports.SHIM_SURFACE = exports.GUARANTEE_ROOT_ENTRY = exports.GUARANTEE_ROOT_COMMAND = exports.GUARANTEE_ROOT_MATCHER = exports.GUARDS_MATCHER = exports.RULES_MATCHER = exports.GUARDS_BIN = exports.RULES_BIN = exports.HookRegistrationEntry = void 0;
|
|
4
|
+
exports.shimCommand = shimCommand;
|
|
5
|
+
exports.guardHookEntry = guardHookEntry;
|
|
6
|
+
exports.isManagedCommand = isManagedCommand;
|
|
7
|
+
exports.managedEntries = managedEntries;
|
|
8
|
+
exports.registeredBins = registeredBins;
|
|
9
|
+
exports.expectedEntries = expectedEntries;
|
|
10
|
+
exports.registrationStale = registrationStale;
|
|
11
|
+
exports.dropManagedEntries = dropManagedEntries;
|
|
12
|
+
exports.addHookEntry = addHookEntry;
|
|
13
|
+
exports.repairRegistration = repairRegistration;
|
|
14
|
+
exports.projectSettingsPaths = projectSettingsPaths;
|
|
15
|
+
exports.readSettings = readSettings;
|
|
16
|
+
exports.writeSettings = writeSettings;
|
|
17
|
+
exports.registrationStaleAt = registrationStaleAt;
|
|
18
|
+
exports.repairRegistrationAt = repairRegistrationAt;
|
|
19
|
+
exports.managedSurfaceDrift = managedSurfaceDrift;
|
|
20
|
+
const tslib_1 = require("tslib");
|
|
21
|
+
const fs = tslib_1.__importStar(require("fs"));
|
|
22
|
+
const path = tslib_1.__importStar(require("path"));
|
|
23
|
+
const to_error_1 = require("../core/to-error");
|
|
24
|
+
const shim_1 = require("./shim");
|
|
25
|
+
const guarantee_root_1 = require("./guarantee-root");
|
|
26
|
+
/**
|
|
27
|
+
* THE INSTALLED HOOK SURFACE — three hooks, and the ONE place their spelling is defined.
|
|
28
|
+
*
|
|
29
|
+
* ─── Why three, and why two of them are RELATIVE ───────────────────────────────────────────────────
|
|
30
|
+
* `.claude/settings.json` used to register two hooks, BOTH absolute via `$CLAUDE_PROJECT_DIR`. That
|
|
31
|
+
* variable NEVER moves — proven from four separate worktrees' own logs, every line reading
|
|
32
|
+
* `root=<worktree> projectDir=<primary>` — so every tree was governed by the PRIMARY's shim, the
|
|
33
|
+
* PRIMARY's binary and the PRIMARY's pin, forever. A worktree could never be judged by the release its
|
|
34
|
+
* own branch pins, and measuring one tree while running another's binary is the non-convergent
|
|
35
|
+
* "two-tree straddle" recorded in shim.ts (an agent gave up after four cures).
|
|
36
|
+
*
|
|
37
|
+
* The hooks reference says "the hook runs in the `cwd` value from the JSON input", so a RELATIVE
|
|
38
|
+
* command resolves against the tool call's own tree. Hence:
|
|
39
|
+
*
|
|
40
|
+
* H1 absolute sh "$CLAUDE_PROJECT_DIR/.claude/webpieces/guarantee-root.sh" matcher Bash
|
|
41
|
+
* H2 relative sh ".claude/webpieces/ai-hook.sh" wp-ai-guards-hook matcher Write|…|Read
|
|
42
|
+
* H3 relative sh ".claude/webpieces/ai-hook.sh" wp-ai-rules-hook matcher Write|Edit|MultiEdit
|
|
43
|
+
*
|
|
44
|
+
* H1 stays absolute because it is the one hook that must ALWAYS resolve: a relative hook that cannot
|
|
45
|
+
* resolve exits 127, and per the same reference any non-2 non-zero exit is a NON-BLOCKING error — i.e.
|
|
46
|
+
* a SILENT UNGUARDED ALLOW. H1 refuses any `cd` that would park the shell where H2/H3 cannot launch,
|
|
47
|
+
* which is what makes the relative pair admissible at all. See guarantee-root.ts.
|
|
48
|
+
*
|
|
49
|
+
* H1 matches `Bash` alone because only Bash can move the shell — the same reason stated at the top of
|
|
50
|
+
* guarantee-root.ts, and the reason the rendered guarantee-root.sh exits 0 immediately for every other
|
|
51
|
+
* tool. Registering it wider would spawn a process per Write/Read to do nothing.
|
|
52
|
+
*
|
|
53
|
+
* ─── Why the registration is a DRIFT SURFACE, not just an install step ─────────────────────────────
|
|
54
|
+
* Nothing used to validate `.claude/settings.json` at all, so a settings file left on the old
|
|
55
|
+
* two-absolute-hook form silently reverted a repo to per-PRIMARY governance and disabled H1 — the one
|
|
56
|
+
* component whose whole job is failing closed. The installed surface is therefore THREE things
|
|
57
|
+
* (ai-hook.sh, guarantee-root.sh, the registration), all three are compared against this release, and
|
|
58
|
+
* `wp-upgrade-shim` regenerates all three. A cure that fixes one of three is worse than no cure,
|
|
59
|
+
* because it reports success.
|
|
60
|
+
*/
|
|
61
|
+
/** One PreToolUse hook entry as webpieces registers it. Data-only → a class, per CLAUDE.md. */
|
|
62
|
+
class HookRegistrationEntry {
|
|
63
|
+
matcher;
|
|
64
|
+
command;
|
|
65
|
+
constructor(matcher, command) {
|
|
66
|
+
this.matcher = matcher;
|
|
67
|
+
this.command = command;
|
|
68
|
+
}
|
|
69
|
+
sameAs(other) {
|
|
70
|
+
return this.matcher === other.matcher && this.command === other.command;
|
|
71
|
+
}
|
|
72
|
+
}
|
|
73
|
+
exports.HookRegistrationEntry = HookRegistrationEntry;
|
|
74
|
+
exports.RULES_BIN = 'wp-ai-rules-hook';
|
|
75
|
+
exports.GUARDS_BIN = 'wp-ai-guards-hook';
|
|
76
|
+
exports.RULES_MATCHER = 'Write|Edit|MultiEdit';
|
|
77
|
+
// Guards match Bash (git/PR guards), Write|Edit|MultiEdit (file-scoped guards) AND Read — Read carries
|
|
78
|
+
// no guard, but the guards hook owns the per-invocation audit log, so matching Read records every file
|
|
79
|
+
// the AI opens (log-and-allow fast path in hook-core.ts; a Read is never blocked).
|
|
80
|
+
exports.GUARDS_MATCHER = 'Write|Edit|MultiEdit|Bash|Read';
|
|
81
|
+
// Only Bash can move the shell, and moving the shell is the only thing H1 judges.
|
|
82
|
+
exports.GUARANTEE_ROOT_MATCHER = 'Bash';
|
|
83
|
+
/**
|
|
84
|
+
* The RELATIVE guard-hook command — `sh ".claude/webpieces/ai-hook.sh" <bin>`.
|
|
85
|
+
*
|
|
86
|
+
* Relative, NOT `$CLAUDE_PROJECT_DIR/…`: that is the whole point (see the header). Invoked via `sh
|
|
87
|
+
* <file>` rather than executed directly so a missing executable bit on the checked-in shim (fresh
|
|
88
|
+
* clone, a filesystem that drops the bit, git core.fileMode quirks) can never break the hook with a raw
|
|
89
|
+
* `Permission denied` on every tool call. Quoted to survive spaces in the path.
|
|
90
|
+
*/
|
|
91
|
+
// webpieces-disable no-function-outside-class -- this module must load on a tree too broken to build a DI container (upgrade-shim.ts depends on that), so it is module-scope like its siblings shim.ts / guarantee-root.ts
|
|
92
|
+
function shimCommand(bin) {
|
|
93
|
+
return `sh "${shim_1.SHIM_MARKER}" ${bin}`;
|
|
94
|
+
}
|
|
95
|
+
/** H1's command. ABSOLUTE on purpose — it must resolve from ANY cwd or it cannot fail closed. */
|
|
96
|
+
exports.GUARANTEE_ROOT_COMMAND = `sh "$CLAUDE_PROJECT_DIR/${guarantee_root_1.GUARANTEE_ROOT_MARKER}"`;
|
|
97
|
+
exports.GUARANTEE_ROOT_ENTRY = new HookRegistrationEntry(exports.GUARANTEE_ROOT_MATCHER, exports.GUARANTEE_ROOT_COMMAND);
|
|
98
|
+
/** The registration entry for one guard bin. */
|
|
99
|
+
// webpieces-disable no-function-outside-class -- module-scope for the same dependency-free reason as shimCommand above
|
|
100
|
+
function guardHookEntry(bin) {
|
|
101
|
+
return new HookRegistrationEntry(bin === exports.RULES_BIN ? exports.RULES_MATCHER : exports.GUARDS_MATCHER, shimCommand(bin));
|
|
102
|
+
}
|
|
103
|
+
/** True when this PreToolUse command is one webpieces owns (either .sh file, in any spelling). */
|
|
104
|
+
// webpieces-disable no-function-outside-class -- module-scope for the same dependency-free reason as shimCommand above
|
|
105
|
+
function isManagedCommand(command) {
|
|
106
|
+
return command.includes(shim_1.SHIM_MARKER) || command.includes(guarantee_root_1.GUARANTEE_ROOT_MARKER);
|
|
107
|
+
}
|
|
108
|
+
// webpieces-disable no-function-outside-class -- module-scope for the same dependency-free reason as shimCommand above
|
|
109
|
+
function preToolUse(settings) {
|
|
110
|
+
return settings.hooks?.PreToolUse ?? [];
|
|
111
|
+
}
|
|
112
|
+
/** Every webpieces-managed entry of one settings file, flattened to matcher + command pairs. */
|
|
113
|
+
// webpieces-disable no-function-outside-class -- module-scope for the same dependency-free reason as shimCommand above
|
|
114
|
+
function managedEntries(settings) {
|
|
115
|
+
const found = [];
|
|
116
|
+
for (const entry of preToolUse(settings)) {
|
|
117
|
+
for (const hook of entry.hooks) {
|
|
118
|
+
if (isManagedCommand(hook.command))
|
|
119
|
+
found.push(new HookRegistrationEntry(entry.matcher, hook.command));
|
|
120
|
+
}
|
|
121
|
+
}
|
|
122
|
+
return found;
|
|
123
|
+
}
|
|
124
|
+
/**
|
|
125
|
+
* Which guard bins this settings file registers, in installer order. A file registering NEITHER is not
|
|
126
|
+
* a project (relative) install and is therefore never judged — a global/absolute install names the bin
|
|
127
|
+
* path directly and carries no shim marker at all.
|
|
128
|
+
*/
|
|
129
|
+
// webpieces-disable no-function-outside-class -- module-scope for the same dependency-free reason as shimCommand above
|
|
130
|
+
function registeredBins(settings) {
|
|
131
|
+
const commands = managedEntries(settings).map((e) => e.command);
|
|
132
|
+
return [exports.GUARDS_BIN, exports.RULES_BIN].filter((bin) => commands.some((c) => c.includes(bin)));
|
|
133
|
+
}
|
|
134
|
+
/**
|
|
135
|
+
* The exact set of entries THIS RELEASE expects in a settings file that registers `bins`.
|
|
136
|
+
*
|
|
137
|
+
* H1 rides with the GUARDS hook, not the rules hook: H1 judges Bash, and `Bash` is in the guards
|
|
138
|
+
* matcher. A file carrying only the rules hook (the supported split install, where a team ships the
|
|
139
|
+
* guards and a developer keeps the code-style rules local) gets no H1 and needs none.
|
|
140
|
+
*/
|
|
141
|
+
// webpieces-disable no-function-outside-class -- module-scope for the same dependency-free reason as shimCommand above
|
|
142
|
+
function expectedEntries(bins) {
|
|
143
|
+
const wanted = [];
|
|
144
|
+
if (bins.includes(exports.GUARDS_BIN))
|
|
145
|
+
wanted.push(exports.GUARANTEE_ROOT_ENTRY);
|
|
146
|
+
for (const bin of bins)
|
|
147
|
+
wanted.push(guardHookEntry(bin));
|
|
148
|
+
return wanted;
|
|
149
|
+
}
|
|
150
|
+
/**
|
|
151
|
+
* True when a settings file registers webpieces hooks in a shape this release does not expect — the
|
|
152
|
+
* old two-absolute-hook form, a missing guarantee-root entry, a stray duplicate, a wrong matcher.
|
|
153
|
+
*
|
|
154
|
+
* Compared as a SET, not a sequence: Claude Code runs all matching hooks in parallel, so array order
|
|
155
|
+
* carries no meaning and reordering must not read as drift.
|
|
156
|
+
*/
|
|
157
|
+
// webpieces-disable no-function-outside-class -- module-scope for the same dependency-free reason as shimCommand above
|
|
158
|
+
function registrationStale(settings) {
|
|
159
|
+
const bins = registeredBins(settings);
|
|
160
|
+
if (bins.length === 0)
|
|
161
|
+
return false;
|
|
162
|
+
const have = managedEntries(settings);
|
|
163
|
+
const want = expectedEntries(bins);
|
|
164
|
+
if (have.length !== want.length)
|
|
165
|
+
return true;
|
|
166
|
+
return want.some((w) => !have.some((h) => h.sameAs(w)));
|
|
167
|
+
}
|
|
168
|
+
/** Drop every webpieces-managed PreToolUse command; returns true if anything was removed. */
|
|
169
|
+
// webpieces-disable no-function-outside-class -- module-scope for the same dependency-free reason as shimCommand above
|
|
170
|
+
function dropManagedEntries(settings) {
|
|
171
|
+
const entries = settings.hooks?.PreToolUse;
|
|
172
|
+
if (!entries)
|
|
173
|
+
return false;
|
|
174
|
+
let changed = false;
|
|
175
|
+
const kept = [];
|
|
176
|
+
for (const entry of entries) {
|
|
177
|
+
const hooks = entry.hooks.filter((h) => !isManagedCommand(h.command));
|
|
178
|
+
if (hooks.length !== entry.hooks.length)
|
|
179
|
+
changed = true;
|
|
180
|
+
if (hooks.length > 0)
|
|
181
|
+
kept.push({ matcher: entry.matcher, hooks });
|
|
182
|
+
}
|
|
183
|
+
if (changed)
|
|
184
|
+
settings.hooks.PreToolUse = kept;
|
|
185
|
+
return changed;
|
|
186
|
+
}
|
|
187
|
+
/** Append one PreToolUse entry. */
|
|
188
|
+
// webpieces-disable no-function-outside-class -- module-scope for the same dependency-free reason as shimCommand above
|
|
189
|
+
function addHookEntry(settings, entry) {
|
|
190
|
+
if (!settings.hooks)
|
|
191
|
+
settings.hooks = {};
|
|
192
|
+
if (!Array.isArray(settings.hooks.PreToolUse))
|
|
193
|
+
settings.hooks.PreToolUse = [];
|
|
194
|
+
settings.hooks.PreToolUse.push({ matcher: entry.matcher, hooks: [{ type: 'command', command: entry.command }] });
|
|
195
|
+
}
|
|
196
|
+
/**
|
|
197
|
+
* Bring one settings object to the three-hook form IN PLACE. Returns true when it changed.
|
|
198
|
+
*
|
|
199
|
+
* REMOVE-then-ADD, never add-beside: two spellings of one registration is exactly the compatibility
|
|
200
|
+
* shim the backwards-compat reviewer rejects, and leaving the `$CLAUDE_PROJECT_DIR/`-prefixed entry
|
|
201
|
+
* beside the relative one would run the PRIMARY's binary alongside the tree's own — the straddle this
|
|
202
|
+
* whole change exists to delete.
|
|
203
|
+
*/
|
|
204
|
+
// webpieces-disable no-function-outside-class -- module-scope for the same dependency-free reason as shimCommand above
|
|
205
|
+
function repairRegistration(settings) {
|
|
206
|
+
const bins = registeredBins(settings);
|
|
207
|
+
if (bins.length === 0 || !registrationStale(settings))
|
|
208
|
+
return false;
|
|
209
|
+
dropManagedEntries(settings);
|
|
210
|
+
for (const entry of expectedEntries(bins))
|
|
211
|
+
addHookEntry(settings, entry);
|
|
212
|
+
return true;
|
|
213
|
+
}
|
|
214
|
+
/** The two project settings files the installer can write. */
|
|
215
|
+
// webpieces-disable no-function-outside-class -- module-scope for the same dependency-free reason as shimCommand above
|
|
216
|
+
function projectSettingsPaths(projectRoot) {
|
|
217
|
+
return [
|
|
218
|
+
path.join(projectRoot, '.claude', 'settings.json'),
|
|
219
|
+
path.join(projectRoot, '.claude', 'settings.local.json'),
|
|
220
|
+
];
|
|
221
|
+
}
|
|
222
|
+
// webpieces-disable no-function-outside-class -- module-scope for the same dependency-free reason as shimCommand above
|
|
223
|
+
function readSettings(settingsPath) {
|
|
224
|
+
if (!fs.existsSync(settingsPath))
|
|
225
|
+
return {};
|
|
226
|
+
const raw = fs.readFileSync(settingsPath, 'utf8');
|
|
227
|
+
if (raw.trim() === '')
|
|
228
|
+
return {};
|
|
229
|
+
// eslint-disable-next-line @webpieces/no-unmanaged-exceptions
|
|
230
|
+
try {
|
|
231
|
+
return JSON.parse(raw);
|
|
232
|
+
}
|
|
233
|
+
catch (err) {
|
|
234
|
+
const error = (0, to_error_1.toError)(err);
|
|
235
|
+
throw new Error(`${settingsPath} has invalid JSON — fix it, then retry: ${error.message}`, { cause: error });
|
|
236
|
+
}
|
|
237
|
+
}
|
|
238
|
+
// webpieces-disable no-function-outside-class -- module-scope for the same dependency-free reason as shimCommand above
|
|
239
|
+
function writeSettings(settingsPath, settings) {
|
|
240
|
+
fs.mkdirSync(path.dirname(settingsPath), { recursive: true });
|
|
241
|
+
fs.writeFileSync(settingsPath, JSON.stringify(settings, null, 4) + '\n');
|
|
242
|
+
}
|
|
243
|
+
/** True when either project settings file under `root` carries a stale registration. */
|
|
244
|
+
// webpieces-disable no-function-outside-class -- module-scope for the same dependency-free reason as shimCommand above
|
|
245
|
+
function registrationStaleAt(root) {
|
|
246
|
+
if (root === null)
|
|
247
|
+
return false;
|
|
248
|
+
// eslint-disable-next-line @webpieces/no-unmanaged-exceptions
|
|
249
|
+
try {
|
|
250
|
+
return projectSettingsPaths(root).some((p) => registrationStale(readSettings(p)));
|
|
251
|
+
}
|
|
252
|
+
catch (err) {
|
|
253
|
+
const error = (0, to_error_1.toError)(err);
|
|
254
|
+
void error; // best-effort: unreadable/invalid settings counts as "not stale" so it never wedges a call
|
|
255
|
+
return false;
|
|
256
|
+
}
|
|
257
|
+
}
|
|
258
|
+
/** Rewrite every stale project settings file under `root`; returns the paths actually changed. */
|
|
259
|
+
// webpieces-disable no-function-outside-class -- module-scope for the same dependency-free reason as shimCommand above
|
|
260
|
+
function repairRegistrationAt(root) {
|
|
261
|
+
const changed = [];
|
|
262
|
+
for (const settingsPath of projectSettingsPaths(root)) {
|
|
263
|
+
if (!fs.existsSync(settingsPath))
|
|
264
|
+
continue;
|
|
265
|
+
const settings = readSettings(settingsPath);
|
|
266
|
+
if (!repairRegistration(settings))
|
|
267
|
+
continue;
|
|
268
|
+
writeSettings(settingsPath, settings);
|
|
269
|
+
changed.push(settingsPath);
|
|
270
|
+
}
|
|
271
|
+
return changed;
|
|
272
|
+
}
|
|
273
|
+
/** The three names the drift check reports, so a deny can say WHICH of them moved. */
|
|
274
|
+
exports.SHIM_SURFACE = shim_1.SHIM_MARKER;
|
|
275
|
+
exports.GUARANTEE_ROOT_SURFACE = guarantee_root_1.GUARANTEE_ROOT_MARKER;
|
|
276
|
+
exports.REGISTRATION_SURFACE = '.claude/settings.json hook registration';
|
|
277
|
+
/**
|
|
278
|
+
* WHICH of the three managed surfaces disagree with this release — the input to fault S.
|
|
279
|
+
*
|
|
280
|
+
* All three are checked against the SAME root, resolved from the RUNNING MODULE (governingShimRoot),
|
|
281
|
+
* never from cwd and never from `$CLAUDE_PROJECT_DIR`: the files we compare and the renderers we
|
|
282
|
+
* compare them TO must come from one install, or the check straddles two trees and can never converge.
|
|
283
|
+
*/
|
|
284
|
+
// webpieces-disable no-function-outside-class -- module-scope for the same dependency-free reason as shimCommand above
|
|
285
|
+
function managedSurfaceDrift(root) {
|
|
286
|
+
const drifted = [];
|
|
287
|
+
if ((0, shim_1.committedShimStale)(root))
|
|
288
|
+
drifted.push(exports.SHIM_SURFACE);
|
|
289
|
+
if ((0, guarantee_root_1.committedGuaranteeRootStale)(root) || guaranteeRootRegisteredButMissing(root))
|
|
290
|
+
drifted.push(exports.GUARANTEE_ROOT_SURFACE);
|
|
291
|
+
if (registrationStaleAt(root))
|
|
292
|
+
drifted.push(exports.REGISTRATION_SURFACE);
|
|
293
|
+
return drifted;
|
|
294
|
+
}
|
|
295
|
+
/**
|
|
296
|
+
* The L-1 hook is REGISTERED but its file is gone.
|
|
297
|
+
*
|
|
298
|
+
* `committedGuaranteeRootStale()` answers false for a missing file on purpose — a repo that has not
|
|
299
|
+
* adopted L-1 yet is not "stale", it is simply still on the old registration, and that case is already
|
|
300
|
+
* reported by `registrationStaleAt()`. But a settings file that REGISTERS the hook while the file is
|
|
301
|
+
* absent is the worst case of all: the hook cannot launch, exit 127 is a NON-BLOCKING error, and per
|
|
302
|
+
* the hooks reference the tool call proceeds — every `cd` unjudged, with nothing surfaced. The
|
|
303
|
+
* registration is what distinguishes "not adopted" from "adopted and broken", so it is what is asked.
|
|
304
|
+
*/
|
|
305
|
+
// webpieces-disable no-function-outside-class -- module-scope for the same dependency-free reason as shimCommand above
|
|
306
|
+
function guaranteeRootRegisteredButMissing(root) {
|
|
307
|
+
if (root === null)
|
|
308
|
+
return false;
|
|
309
|
+
// eslint-disable-next-line @webpieces/no-unmanaged-exceptions
|
|
310
|
+
try {
|
|
311
|
+
const registered = projectSettingsPaths(root).some((p) => managedEntries(readSettings(p)).some((e) => e.command === exports.GUARANTEE_ROOT_COMMAND));
|
|
312
|
+
return registered && !fs.existsSync((0, guarantee_root_1.guaranteeRootPath)(root));
|
|
313
|
+
}
|
|
314
|
+
catch (err) {
|
|
315
|
+
const error = (0, to_error_1.toError)(err);
|
|
316
|
+
void error; // best-effort: unreadable settings counts as "not drifted" so it never wedges a call
|
|
317
|
+
return false;
|
|
318
|
+
}
|
|
319
|
+
}
|
|
320
|
+
//# sourceMappingURL=hook-registration.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"hook-registration.js","sourceRoot":"","sources":["../../../../../../packages/tooling/ai-hook-rules/src/bin/hook-registration.ts"],"names":[],"mappings":";;;AAmFA,kCAEC;AASD,wCAEC;AAID,4CAEC;AASD,wCAQC;AAQD,wCAGC;AAUD,0CAKC;AAUD,8CAOC;AAID,gDAYC;AAID,oCAIC;AAWD,gDAMC;AAID,oDAKC;AAGD,oCAWC;AAGD,sCAGC;AAID,kDAUC;AAID,oDAUC;AAeD,kDAMC;;AAzRD,+CAAyB;AACzB,mDAA6B;AAE7B,+CAA2C;AAC3C,iCAAyD;AACzD,qDAAyG;AAEzG;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;GAkCG;AAEH,+FAA+F;AAC/F,MAAa,qBAAqB;IAEjB;IACA;IAFb,YACa,OAAe,EACf,OAAe;QADf,YAAO,GAAP,OAAO,CAAQ;QACf,YAAO,GAAP,OAAO,CAAQ;IACzB,CAAC;IAEJ,MAAM,CAAC,KAA4B;QAC/B,OAAO,IAAI,CAAC,OAAO,KAAK,KAAK,CAAC,OAAO,IAAI,IAAI,CAAC,OAAO,KAAK,KAAK,CAAC,OAAO,CAAC;IAC5E,CAAC;CACJ;AATD,sDASC;AAWY,QAAA,SAAS,GAAG,kBAAkB,CAAC;AAC/B,QAAA,UAAU,GAAG,mBAAmB,CAAC;AACjC,QAAA,aAAa,GAAG,sBAAsB,CAAC;AACpD,uGAAuG;AACvG,uGAAuG;AACvG,mFAAmF;A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,CAAC,CAAC,IAAI,CAAC,CAAC,CAAwB,EAAW,EAAE,CAAC,CAAC,CAAC,OAAO,KAAK,8BAAsB,CAAC,CAAC,CAAC;QACvH,OAAO,UAAU,IAAI,CAAC,EAAE,CAAC,UAAU,CAAC,IAAA,kCAAiB,EAAC,IAAI,CAAC,CAAC,CAAC;IACjE,CAAC;IAAC,OAAO,GAAY,EAAE,CAAC;QACpB,MAAM,KAAK,GAAG,IAAA,kBAAO,EAAC,GAAG,CAAC,CAAC;QAC3B,KAAK,KAAK,CAAC,CAAC,qFAAqF;QACjG,OAAO,KAAK,CAAC;IACjB,CAAC;AACL,CAAC","sourcesContent":["import * as fs from 'fs';\nimport * as path from 'path';\n\nimport { toError } from '../core/to-error';\nimport { SHIM_MARKER, committedShimStale } from './shim';\nimport { GUARANTEE_ROOT_MARKER, committedGuaranteeRootStale, guaranteeRootPath } from './guarantee-root';\n\n/**\n * THE INSTALLED HOOK SURFACE — three hooks, and the ONE place their spelling is defined.\n *\n * ─── Why three, and why two of them are RELATIVE ───────────────────────────────────────────────────\n * `.claude/settings.json` used to register two hooks, BOTH absolute via `$CLAUDE_PROJECT_DIR`. That\n * variable NEVER moves — proven from four separate worktrees' own logs, every line reading\n * `root=<worktree> projectDir=<primary>` — so every tree was governed by the PRIMARY's shim, the\n * PRIMARY's binary and the PRIMARY's pin, forever. A worktree could never be judged by the release its\n * own branch pins, and measuring one tree while running another's binary is the non-convergent\n * \"two-tree straddle\" recorded in shim.ts (an agent gave up after four cures).\n *\n * The hooks reference says \"the hook runs in the `cwd` value from the JSON input\", so a RELATIVE\n * command resolves against the tool call's own tree. Hence:\n *\n * H1 absolute sh \"$CLAUDE_PROJECT_DIR/.claude/webpieces/guarantee-root.sh\" matcher Bash\n * H2 relative sh \".claude/webpieces/ai-hook.sh\" wp-ai-guards-hook matcher Write|…|Read\n * H3 relative sh \".claude/webpieces/ai-hook.sh\" wp-ai-rules-hook matcher Write|Edit|MultiEdit\n *\n * H1 stays absolute because it is the one hook that must ALWAYS resolve: a relative hook that cannot\n * resolve exits 127, and per the same reference any non-2 non-zero exit is a NON-BLOCKING error — i.e.\n * a SILENT UNGUARDED ALLOW. H1 refuses any `cd` that would park the shell where H2/H3 cannot launch,\n * which is what makes the relative pair admissible at all. See guarantee-root.ts.\n *\n * H1 matches `Bash` alone because only Bash can move the shell — the same reason stated at the top of\n * guarantee-root.ts, and the reason the rendered guarantee-root.sh exits 0 immediately for every other\n * tool. Registering it wider would spawn a process per Write/Read to do nothing.\n *\n * ─── Why the registration is a DRIFT SURFACE, not just an install step ─────────────────────────────\n * Nothing used to validate `.claude/settings.json` at all, so a settings file left on the old\n * two-absolute-hook form silently reverted a repo to per-PRIMARY governance and disabled H1 — the one\n * component whose whole job is failing closed. The installed surface is therefore THREE things\n * (ai-hook.sh, guarantee-root.sh, the registration), all three are compared against this release, and\n * `wp-upgrade-shim` regenerates all three. A cure that fixes one of three is worse than no cure,\n * because it reports success.\n */\n\n/** One PreToolUse hook entry as webpieces registers it. Data-only → a class, per CLAUDE.md. */\nexport class HookRegistrationEntry {\n constructor(\n readonly matcher: string,\n readonly command: string,\n ) {}\n\n sameAs(other: HookRegistrationEntry): boolean {\n return this.matcher === other.matcher && this.command === other.command;\n }\n}\n\n// webpieces-disable no-any-unknown -- settings.json is opaque consumer JSON\nexport interface HookCommand { type: string; command: string; }\nexport interface HookEntry { matcher: string; hooks: HookCommand[]; }\nexport interface ClaudeSettings {\n hooks?: { PreToolUse?: HookEntry[] };\n // webpieces-disable no-any-unknown -- opaque settings bag; arbitrary keys allowed\n [key: string]: unknown;\n}\n\nexport const RULES_BIN = 'wp-ai-rules-hook';\nexport const GUARDS_BIN = 'wp-ai-guards-hook';\nexport const RULES_MATCHER = 'Write|Edit|MultiEdit';\n// Guards match Bash (git/PR guards), Write|Edit|MultiEdit (file-scoped guards) AND Read — Read carries\n// no guard, but the guards hook owns the per-invocation audit log, so matching Read records every file\n// the AI opens (log-and-allow fast path in hook-core.ts; a Read is never blocked).\nexport const GUARDS_MATCHER = 'Write|Edit|MultiEdit|Bash|Read';\n// Only Bash can move the shell, and moving the shell is the only thing H1 judges.\nexport const GUARANTEE_ROOT_MATCHER = 'Bash';\n\n/**\n * The RELATIVE guard-hook command — `sh \".claude/webpieces/ai-hook.sh\" <bin>`.\n *\n * Relative, NOT `$CLAUDE_PROJECT_DIR/…`: that is the whole point (see the header). Invoked via `sh\n * <file>` rather than executed directly so a missing executable bit on the checked-in shim (fresh\n * clone, a filesystem that drops the bit, git core.fileMode quirks) can never break the hook with a raw\n * `Permission denied` on every tool call. Quoted to survive spaces in the path.\n */\n// webpieces-disable no-function-outside-class -- this module must load on a tree too broken to build a DI container (upgrade-shim.ts depends on that), so it is module-scope like its siblings shim.ts / guarantee-root.ts\nexport function shimCommand(bin: string): string {\n return `sh \"${SHIM_MARKER}\" ${bin}`;\n}\n\n/** H1's command. ABSOLUTE on purpose — it must resolve from ANY cwd or it cannot fail closed. */\nexport const GUARANTEE_ROOT_COMMAND = `sh \"$CLAUDE_PROJECT_DIR/${GUARANTEE_ROOT_MARKER}\"`;\n\nexport const GUARANTEE_ROOT_ENTRY = new HookRegistrationEntry(GUARANTEE_ROOT_MATCHER, GUARANTEE_ROOT_COMMAND);\n\n/** The registration entry for one guard bin. */\n// webpieces-disable no-function-outside-class -- module-scope for the same dependency-free reason as shimCommand above\nexport function guardHookEntry(bin: string): HookRegistrationEntry {\n return new HookRegistrationEntry(bin === RULES_BIN ? RULES_MATCHER : GUARDS_MATCHER, shimCommand(bin));\n}\n\n/** True when this PreToolUse command is one webpieces owns (either .sh file, in any spelling). */\n// webpieces-disable no-function-outside-class -- module-scope for the same dependency-free reason as shimCommand above\nexport function isManagedCommand(command: string): boolean {\n return command.includes(SHIM_MARKER) || command.includes(GUARANTEE_ROOT_MARKER);\n}\n\n// webpieces-disable no-function-outside-class -- module-scope for the same dependency-free reason as shimCommand above\nfunction preToolUse(settings: ClaudeSettings): readonly HookEntry[] {\n return settings.hooks?.PreToolUse ?? [];\n}\n\n/** Every webpieces-managed entry of one settings file, flattened to matcher + command pairs. */\n// webpieces-disable no-function-outside-class -- module-scope for the same dependency-free reason as shimCommand above\nexport function managedEntries(settings: ClaudeSettings): readonly HookRegistrationEntry[] {\n const found: HookRegistrationEntry[] = [];\n for (const entry of preToolUse(settings)) {\n for (const hook of entry.hooks) {\n if (isManagedCommand(hook.command)) found.push(new HookRegistrationEntry(entry.matcher, hook.command));\n }\n }\n return found;\n}\n\n/**\n * Which guard bins this settings file registers, in installer order. A file registering NEITHER is not\n * a project (relative) install and is therefore never judged — a global/absolute install names the bin\n * path directly and carries no shim marker at all.\n */\n// webpieces-disable no-function-outside-class -- module-scope for the same dependency-free reason as shimCommand above\nexport function registeredBins(settings: ClaudeSettings): readonly string[] {\n const commands = managedEntries(settings).map((e: HookRegistrationEntry): string => e.command);\n return [GUARDS_BIN, RULES_BIN].filter((bin: string): boolean => commands.some((c: string): boolean => c.includes(bin)));\n}\n\n/**\n * The exact set of entries THIS RELEASE expects in a settings file that registers `bins`.\n *\n * H1 rides with the GUARDS hook, not the rules hook: H1 judges Bash, and `Bash` is in the guards\n * matcher. A file carrying only the rules hook (the supported split install, where a team ships the\n * guards and a developer keeps the code-style rules local) gets no H1 and needs none.\n */\n// webpieces-disable no-function-outside-class -- module-scope for the same dependency-free reason as shimCommand above\nexport function expectedEntries(bins: readonly string[]): readonly HookRegistrationEntry[] {\n const wanted: HookRegistrationEntry[] = [];\n if (bins.includes(GUARDS_BIN)) wanted.push(GUARANTEE_ROOT_ENTRY);\n for (const bin of bins) wanted.push(guardHookEntry(bin));\n return wanted;\n}\n\n/**\n * True when a settings file registers webpieces hooks in a shape this release does not expect — the\n * old two-absolute-hook form, a missing guarantee-root entry, a stray duplicate, a wrong matcher.\n *\n * Compared as a SET, not a sequence: Claude Code runs all matching hooks in parallel, so array order\n * carries no meaning and reordering must not read as drift.\n */\n// webpieces-disable no-function-outside-class -- module-scope for the same dependency-free reason as shimCommand above\nexport function registrationStale(settings: ClaudeSettings): boolean {\n const bins = registeredBins(settings);\n if (bins.length === 0) return false;\n const have = managedEntries(settings);\n const want = expectedEntries(bins);\n if (have.length !== want.length) return true;\n return want.some((w: HookRegistrationEntry): boolean => !have.some((h: HookRegistrationEntry): boolean => h.sameAs(w)));\n}\n\n/** Drop every webpieces-managed PreToolUse command; returns true if anything was removed. */\n// webpieces-disable no-function-outside-class -- module-scope for the same dependency-free reason as shimCommand above\nexport function dropManagedEntries(settings: ClaudeSettings): boolean {\n const entries = settings.hooks?.PreToolUse;\n if (!entries) return false;\n let changed = false;\n const kept: HookEntry[] = [];\n for (const entry of entries) {\n const hooks = entry.hooks.filter((h: HookCommand): boolean => !isManagedCommand(h.command));\n if (hooks.length !== entry.hooks.length) changed = true;\n if (hooks.length > 0) kept.push({ matcher: entry.matcher, hooks });\n }\n if (changed) settings.hooks!.PreToolUse = kept;\n return changed;\n}\n\n/** Append one PreToolUse entry. */\n// webpieces-disable no-function-outside-class -- module-scope for the same dependency-free reason as shimCommand above\nexport function addHookEntry(settings: ClaudeSettings, entry: HookRegistrationEntry): void {\n if (!settings.hooks) settings.hooks = {};\n if (!Array.isArray(settings.hooks.PreToolUse)) settings.hooks.PreToolUse = [];\n settings.hooks.PreToolUse.push({ matcher: entry.matcher, hooks: [{ type: 'command', command: entry.command }] });\n}\n\n/**\n * Bring one settings object to the three-hook form IN PLACE. Returns true when it changed.\n *\n * REMOVE-then-ADD, never add-beside: two spellings of one registration is exactly the compatibility\n * shim the backwards-compat reviewer rejects, and leaving the `$CLAUDE_PROJECT_DIR/`-prefixed entry\n * beside the relative one would run the PRIMARY's binary alongside the tree's own — the straddle this\n * whole change exists to delete.\n */\n// webpieces-disable no-function-outside-class -- module-scope for the same dependency-free reason as shimCommand above\nexport function repairRegistration(settings: ClaudeSettings): boolean {\n const bins = registeredBins(settings);\n if (bins.length === 0 || !registrationStale(settings)) return false;\n dropManagedEntries(settings);\n for (const entry of expectedEntries(bins)) addHookEntry(settings, entry);\n return true;\n}\n\n/** The two project settings files the installer can write. */\n// webpieces-disable no-function-outside-class -- module-scope for the same dependency-free reason as shimCommand above\nexport function projectSettingsPaths(projectRoot: string): readonly string[] {\n return [\n path.join(projectRoot, '.claude', 'settings.json'),\n path.join(projectRoot, '.claude', 'settings.local.json'),\n ];\n}\n\n// webpieces-disable no-function-outside-class -- module-scope for the same dependency-free reason as shimCommand above\nexport function readSettings(settingsPath: string): ClaudeSettings {\n if (!fs.existsSync(settingsPath)) return {};\n const raw = fs.readFileSync(settingsPath, 'utf8');\n if (raw.trim() === '') return {};\n // eslint-disable-next-line @webpieces/no-unmanaged-exceptions\n try {\n return JSON.parse(raw) as ClaudeSettings;\n } catch (err: unknown) {\n const error = toError(err);\n throw new Error(`${settingsPath} has invalid JSON — fix it, then retry: ${error.message}`, { cause: error });\n }\n}\n\n// webpieces-disable no-function-outside-class -- module-scope for the same dependency-free reason as shimCommand above\nexport function writeSettings(settingsPath: string, settings: ClaudeSettings): void {\n fs.mkdirSync(path.dirname(settingsPath), { recursive: true });\n fs.writeFileSync(settingsPath, JSON.stringify(settings, null, 4) + '\\n');\n}\n\n/** True when either project settings file under `root` carries a stale registration. */\n// webpieces-disable no-function-outside-class -- module-scope for the same dependency-free reason as shimCommand above\nexport function registrationStaleAt(root: string | null): boolean {\n if (root === null) return false;\n // eslint-disable-next-line @webpieces/no-unmanaged-exceptions\n try {\n return projectSettingsPaths(root).some((p: string): boolean => registrationStale(readSettings(p)));\n } catch (err: unknown) {\n const error = toError(err);\n void error; // best-effort: unreadable/invalid settings counts as \"not stale\" so it never wedges a call\n return false;\n }\n}\n\n/** Rewrite every stale project settings file under `root`; returns the paths actually changed. */\n// webpieces-disable no-function-outside-class -- module-scope for the same dependency-free reason as shimCommand above\nexport function repairRegistrationAt(root: string): readonly string[] {\n const changed: string[] = [];\n for (const settingsPath of projectSettingsPaths(root)) {\n if (!fs.existsSync(settingsPath)) continue;\n const settings = readSettings(settingsPath);\n if (!repairRegistration(settings)) continue;\n writeSettings(settingsPath, settings);\n changed.push(settingsPath);\n }\n return changed;\n}\n\n/** The three names the drift check reports, so a deny can say WHICH of them moved. */\nexport const SHIM_SURFACE = SHIM_MARKER;\nexport const GUARANTEE_ROOT_SURFACE = GUARANTEE_ROOT_MARKER;\nexport const REGISTRATION_SURFACE = '.claude/settings.json hook registration';\n\n/**\n * WHICH of the three managed surfaces disagree with this release — the input to fault S.\n *\n * All three are checked against the SAME root, resolved from the RUNNING MODULE (governingShimRoot),\n * never from cwd and never from `$CLAUDE_PROJECT_DIR`: the files we compare and the renderers we\n * compare them TO must come from one install, or the check straddles two trees and can never converge.\n */\n// webpieces-disable no-function-outside-class -- module-scope for the same dependency-free reason as shimCommand above\nexport function managedSurfaceDrift(root: string | null): readonly string[] {\n const drifted: string[] = [];\n if (committedShimStale(root)) drifted.push(SHIM_SURFACE);\n if (committedGuaranteeRootStale(root) || guaranteeRootRegisteredButMissing(root)) drifted.push(GUARANTEE_ROOT_SURFACE);\n if (registrationStaleAt(root)) drifted.push(REGISTRATION_SURFACE);\n return drifted;\n}\n\n/**\n * The L-1 hook is REGISTERED but its file is gone.\n *\n * `committedGuaranteeRootStale()` answers false for a missing file on purpose — a repo that has not\n * adopted L-1 yet is not \"stale\", it is simply still on the old registration, and that case is already\n * reported by `registrationStaleAt()`. But a settings file that REGISTERS the hook while the file is\n * absent is the worst case of all: the hook cannot launch, exit 127 is a NON-BLOCKING error, and per\n * the hooks reference the tool call proceeds — every `cd` unjudged, with nothing surfaced. The\n * registration is what distinguishes \"not adopted\" from \"adopted and broken\", so it is what is asked.\n */\n// webpieces-disable no-function-outside-class -- module-scope for the same dependency-free reason as shimCommand above\nfunction guaranteeRootRegisteredButMissing(root: string | null): boolean {\n if (root === null) return false;\n // eslint-disable-next-line @webpieces/no-unmanaged-exceptions\n try {\n const registered = projectSettingsPaths(root).some((p: string): boolean =>\n managedEntries(readSettings(p)).some((e: HookRegistrationEntry): boolean => e.command === GUARANTEE_ROOT_COMMAND));\n return registered && !fs.existsSync(guaranteeRootPath(root));\n } catch (err: unknown) {\n const error = toError(err);\n void error; // best-effort: unreadable settings counts as \"not drifted\" so it never wedges a call\n return false;\n }\n}\n"]}
|
package/src/bin/setup.d.ts
CHANGED
|
@@ -1,4 +1,5 @@
|
|
|
1
1
|
import { renderShim } from './shim';
|
|
2
|
+
import { ClaudeSettings } from './hook-registration';
|
|
2
3
|
export { renderShim };
|
|
3
4
|
declare class HookSpec {
|
|
4
5
|
readonly key: string;
|
|
@@ -35,21 +36,6 @@ interface MigrateResult {
|
|
|
35
36
|
changes: string[];
|
|
36
37
|
}
|
|
37
38
|
export declare function migrate(existing: Json): MigrateResult;
|
|
38
|
-
interface HookCommand {
|
|
39
|
-
type: string;
|
|
40
|
-
command: string;
|
|
41
|
-
}
|
|
42
|
-
interface HookEntry {
|
|
43
|
-
matcher: string;
|
|
44
|
-
hooks: HookCommand[];
|
|
45
|
-
}
|
|
46
|
-
interface ClaudeSettings {
|
|
47
|
-
hooks?: {
|
|
48
|
-
PreToolUse?: HookEntry[];
|
|
49
|
-
};
|
|
50
|
-
[key: string]: unknown;
|
|
51
|
-
}
|
|
52
|
-
export declare function readSettings(settingsPath: string): ClaudeSettings;
|
|
53
39
|
export declare function hasHook(settings: ClaudeSettings, bin: string): boolean;
|
|
54
40
|
export declare function applyHook(hook: HookSpec, chosen: InstallTarget | null, targets: InstallTarget[], projectRoot: string): void;
|
|
55
41
|
export declare function resolveTargetChoice(name: string): string | null;
|