ai-engineering-loop 1.16.0 → 1.17.0

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.
Files changed (38) hide show
  1. package/.agents/skills/ai-engineering-loop/references/loop-deliver.md +2 -1
  2. package/.agents/skills/ai-engineering-loop/references/loop-goal.md +1 -0
  3. package/.agents/skills/ai-engineering-loop/references/loop-maker.md +1 -0
  4. package/.claude/skills/ai-engineering-loop/references/loop-deliver.md +2 -1
  5. package/.claude/skills/ai-engineering-loop/references/loop-goal.md +1 -0
  6. package/.claude/skills/ai-engineering-loop/references/loop-maker.md +1 -0
  7. package/CHANGELOG.md +32 -0
  8. package/README.md +24 -22
  9. package/adapters/dot/gitlab.md +6 -0
  10. package/adapters/github/pull-request.md +5 -0
  11. package/adapters/gitlab/merge-request.md +5 -0
  12. package/adapters/quality/skills/task-impact-inquiry/SKILL.md +2 -0
  13. package/bin/ai-engineering-loop.js +294 -4
  14. package/core/blast-radius-map.md +37 -0
  15. package/core/learnings.md +24 -0
  16. package/core/pr-receipt.md +35 -0
  17. package/core/runtime-safety.md +24 -0
  18. package/lib/cli-args.js +5 -1
  19. package/lib/doctor.js +4 -1
  20. package/lib/gates.js +39 -0
  21. package/lib/guard-hooks.js +115 -0
  22. package/lib/guard.js +336 -0
  23. package/lib/hosts/commands.js +4 -3
  24. package/lib/hosts/doctor.js +3 -3
  25. package/lib/hosts/registry.js +21 -2
  26. package/lib/hosts/table.js +6 -4
  27. package/lib/impact-map.js +501 -0
  28. package/lib/learn.js +303 -0
  29. package/lib/receipt.js +362 -0
  30. package/lib/safe-context.js +14 -0
  31. package/lib/smart-context.js +21 -0
  32. package/package.json +1 -1
  33. package/schemas/delivery.schema.json +11 -1
  34. package/schemas/finding-ledger.schema.json +2 -1
  35. package/schemas/goal-contract.schema.json +9 -0
  36. package/schemas/impact-map.schema.json +78 -0
  37. package/schemas/learnings.schema.json +47 -0
  38. package/schemas/receipt.schema.json +102 -0
package/lib/gates.js CHANGED
@@ -157,9 +157,29 @@ function validateGoalContract(contract) {
157
157
  }
158
158
  });
159
159
  }
160
+ errors.push(...validateGoalScope(contract.scope));
160
161
  return { valid: errors.length === 0, errors };
161
162
  }
162
163
 
164
+ // Optional Goal scope (#59): absent = no path enforcement.
165
+ function validateGoalScope(scope) {
166
+ if (scope === undefined) return [];
167
+ if (!scope || typeof scope !== 'object' || Array.isArray(scope)) return ['scope must be an object'];
168
+ const errors = [];
169
+ for (const key of Object.keys(scope)) {
170
+ if (!['allowedPaths', 'deniedPaths'].includes(key)) errors.push(`scope.${key} is not supported (use allowedPaths, deniedPaths)`);
171
+ }
172
+ for (const key of ['allowedPaths', 'deniedPaths']) {
173
+ if (scope[key] === undefined) continue;
174
+ if (!Array.isArray(scope[key]) || scope[key].some((item) => typeof item !== 'string' || !item.trim())) {
175
+ errors.push(`scope.${key} must be an array of non-empty glob strings`);
176
+ } else if (scope[key].some((item) => item.startsWith('/') || item.split('/').includes('..'))) {
177
+ errors.push(`scope.${key} globs must be repository-relative`);
178
+ }
179
+ }
180
+ return errors;
181
+ }
182
+
163
183
  function validateVerificationBundle(bundle, run, diffHash, currentRevision) {
164
184
  const errors = [];
165
185
  requireRunMetadata(bundle, run, 'verification');
@@ -310,6 +330,24 @@ function writeDeterministicVerdictIfEligible(rootDir, run) {
310
330
  return true;
311
331
  }
312
332
 
333
+ // Out-of-map changes recorded by verify (#71) become LOW ledger warnings,
334
+ // so they reach the Judge and the receipt without blocking delivery.
335
+ function mergeImpactDrift(rootDir, run) {
336
+ const { loadImpactDrift, mergeDriftIntoLedger } = require('./impact-map.js');
337
+ const drift = loadImpactDrift(rootDir, run.runId);
338
+ const filePath = artifactPath(rootDir, run.runId, GATE_FILES.review);
339
+ if (!drift?.outOfMap?.length || !fs.existsSync(filePath)) return 0;
340
+ let ledger;
341
+ try {
342
+ ledger = JSON.parse(fs.readFileSync(filePath, 'utf8'));
343
+ } catch {
344
+ return 0;
345
+ }
346
+ const merged = mergeDriftIntoLedger(ledger, drift);
347
+ if (merged.added) fs.writeFileSync(filePath, `${JSON.stringify(merged.ledger, null, 2)}\n`, { mode: 0o600 });
348
+ return merged.added;
349
+ }
350
+
313
351
  function applyGate(rootDir, gate, { runId, transitionOptions = {} } = {}) {
314
352
  if (!Object.hasOwn(GATE_FILES, gate)) {
315
353
  throw gateError(`Unknown gate: ${gate}. Use ${Object.keys(GATE_FILES).join('|')}`);
@@ -412,6 +450,7 @@ function applyGate(rootDir, gate, { runId, transitionOptions = {} } = {}) {
412
450
  assertArtifactCurrent(rootDir, run, 'diff');
413
451
  assertArtifactCurrent(rootDir, run, 'verification');
414
452
  if (tierOf(run) === 'T0') writeT0FindingLedger(rootDir, run);
453
+ mergeImpactDrift(rootDir, run);
415
454
  const { filePath, value } = readJsonArtifact(rootDir, run.runId, GATE_FILES.review);
416
455
  requireRunMetadata(value, run, 'Finding Ledger');
417
456
  if (value.diffHash !== run.artifacts.diff?.sha256) {
@@ -0,0 +1,115 @@
1
+ 'use strict';
2
+
3
+ // `hooks install | uninstall | status` (#59): native pre-tool hooks that call
4
+ // `guard check`. Claude Code first; hosts without a hook API stay advisory.
5
+ // Project-level settings, because the guard reads this repository's Run.
6
+
7
+ const fs = require('fs');
8
+ const path = require('path');
9
+ const { HOSTS, getHost, guardrailSupport } = require('./hosts/registry.js');
10
+
11
+ const HOOK_MARKER = 'ai-engineering-loop guard check';
12
+ const CLAUDE_MATCHER = 'Edit|Write|MultiEdit|NotebookEdit|Bash';
13
+ const CLAUDE_COMMAND = 'npx --yes ai-engineering-loop guard check --hook claude';
14
+
15
+ function hooksError(message, code = 'INVALID_USAGE') {
16
+ const error = new Error(message);
17
+ error.code = code;
18
+ return error;
19
+ }
20
+
21
+ function hookHost(hostId = 'claude') {
22
+ const host = getHost(hostId);
23
+ if (!host) throw hooksError(`Unknown host: ${hostId}`);
24
+ if (guardrailSupport(host) !== 'enforced') {
25
+ const enforced = HOSTS.filter((item) => guardrailSupport(item) === 'enforced').map((item) => item.id);
26
+ throw hooksError(`${host.name} has no hook API; its guardrails are advisory. Hosts with hooks: ${enforced.join(', ')}`, 'HOOKS_UNSUPPORTED');
27
+ }
28
+ return host;
29
+ }
30
+
31
+ function settingsPath(rootDir, host) {
32
+ return path.join(rootDir, host.hooks.settings);
33
+ }
34
+
35
+ function readSettings(filePath) {
36
+ if (!fs.existsSync(filePath)) return {};
37
+ const stat = fs.lstatSync(filePath);
38
+ if (stat.isSymbolicLink() || !stat.isFile()) throw hooksError(`${filePath} must be a regular file`, 'UNSAFE_HOOKS_PATH');
39
+ try {
40
+ const value = JSON.parse(fs.readFileSync(filePath, 'utf8'));
41
+ if (!value || typeof value !== 'object' || Array.isArray(value)) throw new Error('not an object');
42
+ return value;
43
+ } catch (cause) {
44
+ throw hooksError(`Cannot update ${path.basename(filePath)}: ${cause.message}. Nothing was written.`, 'INVALID_HOOKS_SETTINGS');
45
+ }
46
+ }
47
+
48
+ function writeSettings(filePath, value) {
49
+ fs.mkdirSync(path.dirname(filePath), { recursive: true });
50
+ const temporary = `${filePath}.${process.pid}.tmp`;
51
+ fs.writeFileSync(temporary, `${JSON.stringify(value, null, 2)}\n`);
52
+ fs.renameSync(temporary, filePath);
53
+ }
54
+
55
+ function isOurHook(hook) {
56
+ return typeof hook?.command === 'string' && hook.command.includes(HOOK_MARKER);
57
+ }
58
+
59
+ function ourEntries(settings) {
60
+ return (settings.hooks?.PreToolUse || []).filter((entry) => (entry.hooks || []).some(isOurHook));
61
+ }
62
+
63
+ function hooksStatus(rootDir, { host: hostId = 'claude' } = {}) {
64
+ const host = hookHost(hostId);
65
+ const filePath = settingsPath(rootDir, host);
66
+ const settings = readSettings(filePath);
67
+ return {
68
+ host: host.id,
69
+ guardrails: guardrailSupport(host),
70
+ settings: host.hooks.settings,
71
+ installed: ourEntries(settings).length > 0
72
+ };
73
+ }
74
+
75
+ function installHooks(rootDir, { host: hostId = 'claude' } = {}) {
76
+ const host = hookHost(hostId);
77
+ const filePath = settingsPath(rootDir, host);
78
+ const settings = readSettings(filePath);
79
+ if (ourEntries(settings).length) return { ...hooksStatus(rootDir, { host: host.id }), changed: false };
80
+ const hooks = settings.hooks && typeof settings.hooks === 'object' ? settings.hooks : {};
81
+ const preToolUse = Array.isArray(hooks.PreToolUse) ? hooks.PreToolUse : [];
82
+ writeSettings(filePath, {
83
+ ...settings,
84
+ hooks: {
85
+ ...hooks,
86
+ PreToolUse: [...preToolUse, { matcher: CLAUDE_MATCHER, hooks: [{ type: 'command', command: CLAUDE_COMMAND }] }]
87
+ }
88
+ });
89
+ return { ...hooksStatus(rootDir, { host: host.id }), changed: true };
90
+ }
91
+
92
+ function uninstallHooks(rootDir, { host: hostId = 'claude' } = {}) {
93
+ const host = hookHost(hostId);
94
+ const filePath = settingsPath(rootDir, host);
95
+ if (!fs.existsSync(filePath)) return { ...hooksStatus(rootDir, { host: host.id }), changed: false };
96
+ const settings = readSettings(filePath);
97
+ if (!ourEntries(settings).length) return { ...hooksStatus(rootDir, { host: host.id }), changed: false };
98
+ const preToolUse = settings.hooks.PreToolUse
99
+ .map((entry) => ({ ...entry, hooks: (entry.hooks || []).filter((hook) => !isOurHook(hook)) }))
100
+ .filter((entry) => entry.hooks.length);
101
+ const { PreToolUse: _removed, ...otherHooks } = settings.hooks;
102
+ const hooks = preToolUse.length ? { ...otherHooks, PreToolUse: preToolUse } : otherHooks;
103
+ const { hooks: _old, ...rest } = settings;
104
+ writeSettings(filePath, Object.keys(hooks).length ? { ...rest, hooks } : rest);
105
+ return { ...hooksStatus(rootDir, { host: host.id }), changed: true };
106
+ }
107
+
108
+ module.exports = {
109
+ HOOK_MARKER,
110
+ CLAUDE_COMMAND,
111
+ CLAUDE_MATCHER,
112
+ hooksStatus,
113
+ installHooks,
114
+ uninstallHooks
115
+ };
package/lib/guard.js ADDED
@@ -0,0 +1,336 @@
1
+ 'use strict';
2
+
3
+ // Runtime guardrails (#59). `guard check` decides allow | warn | block for one
4
+ // tool call before it runs: Edit/Write outside the Run's scope, and Bash
5
+ // commands that destroy files, history, or data. ASSISTED warns (the host
6
+ // asks the human), UNATTENDED and REPORT_ONLY block. Every warn/block is
7
+ // appended to the Run as evidence.
8
+
9
+ const fs = require('fs');
10
+ const path = require('path');
11
+ const { RUN_MODES, TERMINAL_STATES, atomicWriteJson, getCurrentRun, loadRun, runsDir } = require('./run-state.js');
12
+ const { loadPolicy } = require('./runtime-policy.js');
13
+ const { redactHomePaths, redactSecrets } = require('./safe-context.js');
14
+ const { impactScope, loadImpactMap, matchesAnyGlob } = require('./impact-map.js');
15
+
16
+ const GUARD_EVENTS_FILE = 'guard-events.json';
17
+ const MAX_EVENTS = 500;
18
+ const MAX_TARGET_CHARS = 300;
19
+ const DECISIONS = Object.freeze(['allow', 'warn', 'block']);
20
+ const EDIT_TOOLS = new Set(['edit', 'write', 'multiedit', 'notebookedit']);
21
+ const BASH_TOOLS = new Set(['bash', 'shell']);
22
+
23
+ function guardError(message, code = 'INVALID_USAGE') {
24
+ const error = new Error(message);
25
+ error.code = code;
26
+ return error;
27
+ }
28
+
29
+ function normalizeTool(tool) {
30
+ const value = String(tool || '').trim().toLowerCase();
31
+ if (EDIT_TOOLS.has(value)) return 'edit';
32
+ if (BASH_TOOLS.has(value)) return 'bash';
33
+ return value ? 'other' : null;
34
+ }
35
+
36
+ // Accepts a Claude Code PreToolUse payload ({ tool_name, tool_input }) or a
37
+ // plain { path } / { command } object.
38
+ function parseGuardInput(input, tool) {
39
+ const value = input && typeof input === 'object' ? input : {};
40
+ const toolInput = value.tool_input && typeof value.tool_input === 'object' ? value.tool_input : value;
41
+ const kind = normalizeTool(tool || value.tool_name);
42
+ if (!kind) throw guardError('guard check needs --tool edit|bash or a payload with tool_name');
43
+ return {
44
+ tool: kind,
45
+ toolName: value.tool_name || tool,
46
+ path: toolInput.file_path || toolInput.notebook_path || toolInput.path || null,
47
+ command: typeof toolInput.command === 'string' ? toolInput.command : null
48
+ };
49
+ }
50
+
51
+ // Shell segments split on ; && || | and newlines; quotes are kept simple on
52
+ // purpose: a quoted "rm -rf" still warns, which errs on the safe side.
53
+ function segments(command) {
54
+ return String(command || '')
55
+ .split(/\n|;|&&|\|\||\|/)
56
+ .map((part) => part.trim().split(/\s+/).filter(Boolean).map((token) => token.replace(/^['"]|['"]$/g, '')))
57
+ .filter((tokens) => tokens.length);
58
+ }
59
+
60
+ function shortFlags(tokens) {
61
+ return tokens.filter((token) => /^-[A-Za-z]+$/.test(token)).join('').replace(/-/g, '');
62
+ }
63
+
64
+ function commandIndex(tokens, name) {
65
+ return tokens.findIndex((token) => token === name || token.endsWith(`/${name}`));
66
+ }
67
+
68
+ function gitSubcommand(tokens) {
69
+ const start = commandIndex(tokens, 'git');
70
+ if (start === -1) return null;
71
+ for (let index = start + 1; index < tokens.length; index += 1) {
72
+ const token = tokens[index];
73
+ if (token === '-C' || token === '-c') {
74
+ index += 1;
75
+ continue;
76
+ }
77
+ if (token.startsWith('-')) continue;
78
+ return { name: token, args: tokens.slice(index + 1) };
79
+ }
80
+ return null;
81
+ }
82
+
83
+ const DESTRUCTIVE_RULES = Object.freeze([
84
+ {
85
+ id: 'rm-recursive-force',
86
+ reason: 'rm with recursive and force flags deletes files without confirmation',
87
+ test: (tokens) => {
88
+ const start = commandIndex(tokens, 'rm');
89
+ if (start === -1) return false;
90
+ const args = tokens.slice(start + 1);
91
+ const flags = shortFlags(args);
92
+ const recursive = /[rR]/.test(flags) || args.includes('--recursive');
93
+ const force = flags.includes('f') || args.includes('--force');
94
+ return recursive && force;
95
+ }
96
+ },
97
+ {
98
+ id: 'git-push-force',
99
+ reason: 'git push --force rewrites remote history',
100
+ test: (tokens) => {
101
+ const git = gitSubcommand(tokens);
102
+ if (git?.name !== 'push') return false;
103
+ return git.args.some((arg) => arg === '--force' || arg.startsWith('--force-with-lease') ||
104
+ arg === '--force-if-includes' || (/^-[A-Za-z]+$/.test(arg) && arg.includes('f')) ||
105
+ (/^\+[^\s]/.test(arg)));
106
+ }
107
+ },
108
+ {
109
+ id: 'git-reset-hard',
110
+ reason: 'git reset --hard discards uncommitted work',
111
+ test: (tokens) => {
112
+ const git = gitSubcommand(tokens);
113
+ return git?.name === 'reset' && git.args.includes('--hard');
114
+ }
115
+ },
116
+ {
117
+ id: 'git-clean-force',
118
+ reason: 'git clean -fd deletes untracked files and directories',
119
+ test: (tokens) => {
120
+ const git = gitSubcommand(tokens);
121
+ if (git?.name !== 'clean') return false;
122
+ const flags = shortFlags(git.args);
123
+ const force = flags.includes('f') || git.args.includes('--force');
124
+ const dirs = flags.includes('d');
125
+ return force && dirs;
126
+ }
127
+ }
128
+ ]);
129
+
130
+ const SQL_RULES = Object.freeze([
131
+ { id: 'sql-drop', reason: 'DROP TABLE/DATABASE/SCHEMA destroys data', pattern: /\bdrop\s+(?:table|database|schema)\b/i },
132
+ { id: 'sql-truncate', reason: 'TRUNCATE deletes every row of a table', pattern: /\btruncate\s+(?:table\s+)?[`"'[]?[A-Za-z_]/i }
133
+ ]);
134
+
135
+ function destructiveFindings(command) {
136
+ const findings = new Map();
137
+ for (const tokens of segments(command)) {
138
+ for (const rule of DESTRUCTIVE_RULES) {
139
+ if (rule.test(tokens)) findings.set(rule.id, rule.reason);
140
+ }
141
+ }
142
+ for (const rule of SQL_RULES) {
143
+ if (rule.pattern.test(String(command || ''))) findings.set(rule.id, rule.reason);
144
+ }
145
+ return [...findings].map(([id, reason]) => ({ id, reason }));
146
+ }
147
+
148
+ function activeRun(rootDir, runId) {
149
+ if (runId) return loadRun(rootDir, runId);
150
+ const current = getCurrentRun(rootDir);
151
+ return current && !TERMINAL_STATES.has(current.state) ? current : null;
152
+ }
153
+
154
+ function readFrozenGoal(rootDir, run) {
155
+ const filePath = path.join(runsDir(rootDir), run.runId, 'goal-contract.json');
156
+ if (!fs.existsSync(filePath)) return null;
157
+ try {
158
+ return JSON.parse(fs.readFileSync(filePath, 'utf8'));
159
+ } catch {
160
+ return null;
161
+ }
162
+ }
163
+
164
+ // Allowed = the frozen Goal's scope.allowedPaths plus the confirmed impact
165
+ // map (#71); denied = the Goal's scope.deniedPaths. No allowed list means no
166
+ // path enforcement, but denied paths always apply.
167
+ function runScope(rootDir, run) {
168
+ if (!run) return { allowedPaths: null, deniedPaths: [], sources: [] };
169
+ const goal = readFrozenGoal(rootDir, run);
170
+ const mapScope = impactScope(loadImpactMap(rootDir, run.runId));
171
+ const sources = [];
172
+ let allowed = null;
173
+ if (Array.isArray(goal?.scope?.allowedPaths)) {
174
+ allowed = [...goal.scope.allowedPaths];
175
+ sources.push('goal');
176
+ }
177
+ if (mapScope) {
178
+ allowed = [...(allowed || []), ...mapScope.allowedPaths];
179
+ sources.push('impact-map');
180
+ }
181
+ return {
182
+ allowedPaths: allowed ? [...new Set(allowed)].sort() : null,
183
+ deniedPaths: Array.isArray(goal?.scope?.deniedPaths) ? [...goal.scope.deniedPaths] : [],
184
+ sources
185
+ };
186
+ }
187
+
188
+ // Real path of a file that may not exist yet: resolve the nearest existing
189
+ // ancestor (macOS /var -> /private/var, symlinked checkouts) and re-append.
190
+ function realish(target) {
191
+ let current = path.resolve(target);
192
+ const rest = [];
193
+ for (;;) {
194
+ try {
195
+ return path.join(fs.realpathSync(current), ...rest);
196
+ } catch {
197
+ const parent = path.dirname(current);
198
+ if (parent === current) return path.resolve(target);
199
+ rest.unshift(path.basename(current));
200
+ current = parent;
201
+ }
202
+ }
203
+ }
204
+
205
+ function repoRelative(rootDir, filePath) {
206
+ const absolute = path.resolve(rootDir, String(filePath));
207
+ const pairs = [[path.resolve(rootDir), absolute], [realish(rootDir), realish(absolute)]];
208
+ for (const [root, target] of pairs) {
209
+ const rel = path.relative(root, target);
210
+ if (rel !== '..' && !rel.startsWith(`..${path.sep}`) && !path.isAbsolute(rel)) return rel.split(path.sep).join('/');
211
+ }
212
+ return null;
213
+ }
214
+
215
+ function scopeFindings(rootDir, filePath, scope) {
216
+ if (!filePath) return [];
217
+ const relative = repoRelative(rootDir, filePath);
218
+ if (relative != null && (relative === '.ai-engineering-loop' || relative.startsWith('.ai-engineering-loop/'))) return [];
219
+ if (relative == null) {
220
+ return scope.allowedPaths ? [{ id: 'outside-repository', reason: 'edit target is outside the repository and the Run scope' }] : [];
221
+ }
222
+ if (matchesAnyGlob(relative, scope.deniedPaths)) {
223
+ return [{ id: 'scope-denied', reason: `${relative} matches scope.deniedPaths` }];
224
+ }
225
+ if (scope.allowedPaths && !matchesAnyGlob(relative, scope.allowedPaths)) {
226
+ return [{ id: 'scope-outside', reason: `${relative} is outside the Run scope (${scope.sources.join(' + ')})` }];
227
+ }
228
+ return [];
229
+ }
230
+
231
+ function decisionFor(mode, findings) {
232
+ if (!findings.length) return 'allow';
233
+ return mode === RUN_MODES.ASSISTED ? 'warn' : 'block';
234
+ }
235
+
236
+ function safeTarget(parsed, rootDir) {
237
+ const raw = parsed.tool === 'bash' ? parsed.command : (repoRelative(rootDir, parsed.path || '') ?? parsed.path);
238
+ return redactHomePaths(redactSecrets(String(raw || '')).text).slice(0, MAX_TARGET_CHARS);
239
+ }
240
+
241
+ function recordGuardEvent(rootDir, runId, event) {
242
+ const filePath = path.join(runsDir(rootDir), runId, GUARD_EVENTS_FILE);
243
+ let events = [];
244
+ if (fs.existsSync(filePath)) {
245
+ try {
246
+ const parsed = JSON.parse(fs.readFileSync(filePath, 'utf8'));
247
+ if (Array.isArray(parsed.events)) events = parsed.events;
248
+ } catch {
249
+ events = [];
250
+ }
251
+ }
252
+ events.push(event);
253
+ atomicWriteJson(filePath, { schemaVersion: 1, runId, events: events.slice(-MAX_EVENTS) });
254
+ }
255
+
256
+ function checkGuard(rootDir, { tool, input, runId, now = new Date() } = {}) {
257
+ const parsed = parseGuardInput(input, tool);
258
+ const run = activeRun(rootDir, runId);
259
+ const mode = run?.mode || loadPolicy(rootDir).defaultMode;
260
+ let findings = [];
261
+ if (parsed.tool === 'edit') {
262
+ if (run && mode === RUN_MODES.REPORT_ONLY) {
263
+ findings.push({ id: 'report-only-mutation', reason: 'REPORT_ONLY runs must not edit the repository' });
264
+ }
265
+ findings.push(...scopeFindings(rootDir, parsed.path, runScope(rootDir, run)));
266
+ } else if (parsed.tool === 'bash') {
267
+ findings = destructiveFindings(parsed.command);
268
+ }
269
+ const decision = decisionFor(mode, findings);
270
+ const result = {
271
+ decision,
272
+ tool: parsed.tool,
273
+ mode,
274
+ runId: run?.runId || null,
275
+ rules: findings.map((item) => item.id),
276
+ reasons: findings.map((item) => item.reason)
277
+ };
278
+ if (run && decision !== 'allow') {
279
+ recordGuardEvent(rootDir, run.runId, {
280
+ at: now.toISOString(),
281
+ tool: parsed.toolName || parsed.tool,
282
+ decision,
283
+ mode,
284
+ rules: result.rules,
285
+ reasons: result.reasons,
286
+ target: safeTarget(parsed, rootDir)
287
+ });
288
+ }
289
+ return result;
290
+ }
291
+
292
+ function loadGuardEvents(rootDir, runId) {
293
+ const filePath = path.join(runsDir(rootDir), runId, GUARD_EVENTS_FILE);
294
+ if (!fs.existsSync(filePath)) return [];
295
+ try {
296
+ const parsed = JSON.parse(fs.readFileSync(filePath, 'utf8'));
297
+ return Array.isArray(parsed.events) ? parsed.events : [];
298
+ } catch {
299
+ return [];
300
+ }
301
+ }
302
+
303
+ function guardSummary(rootDir, runId) {
304
+ const events = loadGuardEvents(rootDir, runId);
305
+ return {
306
+ warn: events.filter((event) => event.decision === 'warn').length,
307
+ block: events.filter((event) => event.decision === 'block').length,
308
+ rules: [...new Set(events.flatMap((event) => event.rules || []))].sort()
309
+ };
310
+ }
311
+
312
+ // Claude Code PreToolUse output: deny blocks the call, ask hands it to the
313
+ // human, and allow prints nothing so the normal permission flow continues.
314
+ function claudeHookOutput(result) {
315
+ if (result.decision === 'allow') return null;
316
+ return {
317
+ hookSpecificOutput: {
318
+ hookEventName: 'PreToolUse',
319
+ permissionDecision: result.decision === 'block' ? 'deny' : 'ask',
320
+ permissionDecisionReason: `ai-engineering-loop guard (${result.mode}): ${result.reasons.join('; ')}`
321
+ }
322
+ };
323
+ }
324
+
325
+ module.exports = {
326
+ DECISIONS,
327
+ GUARD_EVENTS_FILE,
328
+ DESTRUCTIVE_RULES,
329
+ parseGuardInput,
330
+ destructiveFindings,
331
+ runScope,
332
+ checkGuard,
333
+ loadGuardEvents,
334
+ guardSummary,
335
+ claudeHookOutput
336
+ };
@@ -5,7 +5,7 @@
5
5
  // then runs the normal sync (manifest-based copy and cleanup).
6
6
 
7
7
  const path = require('path');
8
- const { HOSTS, HOST_IDS, OUT_OF_SCOPE, SHARED_SKILLS_DIR, getHost, usesSharedSkills } = require('./registry.js');
8
+ const { HOSTS, HOST_IDS, OUT_OF_SCOPE, SHARED_SKILLS_DIR, getHost, guardrailSupport, usesSharedSkills } = require('./registry.js');
9
9
  const { detectHosts } = require('./detect.js');
10
10
  const { readUserConfig, writeUserConfig } = require('../user-state.js');
11
11
 
@@ -35,6 +35,7 @@ function listHosts({ home, env = process.env, selection }) {
35
35
  id: host.id,
36
36
  name: host.name,
37
37
  certification: host.certification,
38
+ guardrails: guardrailSupport(host),
38
39
  detected: d.detected,
39
40
  via: [...d.dirs.map((dir) => `~/${dir}`), ...d.bins.map((bin) => `PATH:${path.basename(bin)}`)],
40
41
  enabled: selection.enabled.has(host.id),
@@ -47,10 +48,10 @@ function listHosts({ home, env = process.env, selection }) {
47
48
 
48
49
  function formatHostsList(rows, selection) {
49
50
  const lines = [`Hosts (${selection.source === 'config' ? `saved selection, mode ${selection.mode}` : 'no setup yet: hosts with a config folder are synced'})`];
50
- lines.push(` ${'id'.padEnd(16)} ${'enabled'.padEnd(8)} ${'certification'.padEnd(17)} ${'detected'.padEnd(28)} skills`);
51
+ lines.push(` ${'id'.padEnd(16)} ${'enabled'.padEnd(8)} ${'certification'.padEnd(17)} ${'guardrails'.padEnd(10)} ${'detected'.padEnd(28)} skills`);
51
52
  for (const row of rows) {
52
53
  const state = row.enabled ? 'yes' : row.disabled ? 'off' : '-';
53
- lines.push(` ${row.id.padEnd(16)} ${state.padEnd(8)} ${row.certification.padEnd(17)} ${(row.detected ? row.via.join(',') : '-').slice(0, 28).padEnd(28)} ${row.skills}`);
54
+ lines.push(` ${row.id.padEnd(16)} ${state.padEnd(8)} ${row.certification.padEnd(17)} ${row.guardrails.padEnd(10)} ${(row.detected ? row.via.join(',') : '-').slice(0, 28).padEnd(28)} ${row.skills}`);
54
55
  }
55
56
  lines.push(`Out of scope (no native skills): ${OUT_OF_SCOPE.map((h) => h.name).join(', ')}.`);
56
57
  return lines.join('\n');
@@ -7,7 +7,7 @@
7
7
  const crypto = require('crypto');
8
8
  const fs = require('fs');
9
9
  const path = require('path');
10
- const { HOSTS, usesSharedSkills } = require('./registry.js');
10
+ const { HOSTS, guardrailSupport, usesSharedSkills } = require('./registry.js');
11
11
  const { certificationLabel } = require('./table.js');
12
12
 
13
13
  const CORE_SKILL = 'ai-engineering-loop';
@@ -55,7 +55,7 @@ function hostsDoctor({ home, selection, plan }) {
55
55
  if (!copies.length) problems.push('core skill not installed');
56
56
  if (hashes.size > 1) problems.push(`${copies.length} copies with different content`);
57
57
  if (resync.length) problems.push(`${resync.length} file(s) need sync`);
58
- rows.push({ id: host.id, name: host.name, certification: host.certification, certifiedBy: host.certifiedBy || null, copies, identical: hashes.size <= 1, resync, ok: problems.length === 0, problems });
58
+ rows.push({ id: host.id, name: host.name, certification: host.certification, certifiedBy: host.certifiedBy || null, guardrails: guardrailSupport(host), copies, identical: hashes.size <= 1, resync, ok: problems.length === 0, problems });
59
59
  }
60
60
  return { ok: rows.every((row) => row.ok), hosts: rows };
61
61
  }
@@ -65,7 +65,7 @@ function formatHostsDoctor(report) {
65
65
  const lines = ['hosts doctor (core skill copies each host can see):'];
66
66
  for (const row of report.hosts) {
67
67
  const where = row.copies.map((c) => c.path).join(', ') || '-';
68
- lines.push(` ${row.ok ? '✓' : '✗'} ${row.id.padEnd(16)} [${certificationLabel(row)}] ${row.copies.length} cop${row.copies.length === 1 ? 'y' : 'ies'}${row.copies.length > 1 ? (row.identical ? ' (identical)' : ' (DIFFERENT)') : ''}: ${where}`);
68
+ lines.push(` ${row.ok ? '✓' : '✗'} ${row.id.padEnd(16)} [${certificationLabel(row)}, guardrails ${row.guardrails}] ${row.copies.length} cop${row.copies.length === 1 ? 'y' : 'ies'}${row.copies.length > 1 ? (row.identical ? ' (identical)' : ' (DIFFERENT)') : ''}: ${where}`);
69
69
  for (const problem of row.problems) lines.push(` ${problem}`);
70
70
  for (const file of row.resync.slice(0, 5)) lines.push(` needs sync: ${file}`);
71
71
  if (row.resync.length > 5) lines.push(` ... and ${row.resync.length - 5} more`);
@@ -20,6 +20,9 @@
20
20
  // subagents are installed but no such run is recorded; 'instruction-only' =
21
21
  // no file-based subagents, the loop runs CONTEXT_ISOLATION_ONLY. Promote a
22
22
  // host to 'full' only with a run record.
23
+ // - `hooks` (#59): native pre-tool hook API that `ael hooks install` writes
24
+ // to, or absent. A host with hooks gets **enforced** runtime guardrails;
25
+ // every other host is **advisory** (the loop instructions only).
23
26
 
24
27
  const SHARED_SKILLS_DIR = '.agents/skills';
25
28
  const CERTIFICATIONS = Object.freeze(['full', 'experimental', 'instruction-only']);
@@ -34,7 +37,8 @@ const HOSTS = Object.freeze([
34
37
  skills: { dir: '.claude/skills' },
35
38
  reads: ['.claude/skills'],
36
39
  agents: { format: 'claude-md', dir: '.claude/agents' },
37
- docs: ['https://code.claude.com/docs/en/skills', 'https://code.claude.com/docs/en/sub-agents'],
40
+ hooks: { format: 'claude-pretooluse', settings: '.claude/settings.json' },
41
+ docs: ['https://code.claude.com/docs/en/skills', 'https://code.claude.com/docs/en/sub-agents', 'https://code.claude.com/docs/en/hooks'],
38
42
  verified: '2026-10-11'
39
43
  },
40
44
  {
@@ -295,4 +299,19 @@ function usesSharedSkills(host) {
295
299
  return host.skills === 'shared';
296
300
  }
297
301
 
298
- module.exports = { CERTIFICATIONS, HOSTS, HOST_IDS, OUT_OF_SCOPE, SHARED_SKILLS_DIR, getHost, usesSharedSkills };
302
+ // 'enforced' = native hooks block or ask before a tool runs; 'advisory' = the
303
+ // guardrail lives in the loop instructions only.
304
+ function guardrailSupport(host) {
305
+ return host?.hooks ? 'enforced' : 'advisory';
306
+ }
307
+
308
+ module.exports = {
309
+ CERTIFICATIONS,
310
+ HOSTS,
311
+ HOST_IDS,
312
+ OUT_OF_SCOPE,
313
+ SHARED_SKILLS_DIR,
314
+ getHost,
315
+ guardrailSupport,
316
+ usesSharedSkills
317
+ };
@@ -2,7 +2,7 @@
2
2
 
3
3
  // README host table generated from the registry (#52); `npm test` fails on drift.
4
4
 
5
- const { HOSTS, OUT_OF_SCOPE, SHARED_SKILLS_DIR, usesSharedSkills } = require('./registry.js');
5
+ const { HOSTS, OUT_OF_SCOPE, SHARED_SKILLS_DIR, guardrailSupport, usesSharedSkills } = require('./registry.js');
6
6
 
7
7
  const START = '<!-- hosts-table:start (generated from lib/hosts/registry.js; run npm run build:hosts-table) -->';
8
8
  const END = '<!-- hosts-table:end -->';
@@ -18,16 +18,18 @@ function renderHostTable() {
18
18
  const skills = usesSharedSkills(host) ? `\`~/${SHARED_SKILLS_DIR}\` (shared)` : `\`~/${host.skills.dir}\``;
19
19
  const agents = host.agents ? `\`~/${host.agents.dir}\`` : 'none (in-session review)';
20
20
  const detect = [...host.detect.dirs.map((d) => `\`~/${d}\``), ...(host.detect.bins || []).map((b) => `\`${b}\``)].join(', ');
21
- return `| ${host.name} | \`${host.id}\` | ${certificationLabel(host)} | ${skills} | ${agents} | ${detect} |`;
21
+ return `| ${host.name} | \`${host.id}\` | ${certificationLabel(host)} | ${guardrailSupport(host)} | ${skills} | ${agents} | ${detect} |`;
22
22
  });
23
23
  return [
24
24
  START,
25
- '| Host | id | Certification | Skill folder | Reviewer agents | Detected by |',
26
- '|---|---|---|---|---|---|',
25
+ '| Host | id | Certification | Guardrails | Skill folder | Reviewer agents | Detected by |',
26
+ '|---|---|---|---|---|---|---|',
27
27
  ...rows,
28
28
  '',
29
29
  'Certification: **full** = a real `default` recipe run with reviewer subagents and every gate is recorded for this host; **experimental** = reviewer subagents install, no recorded run; **instruction-only** = no subagents, review runs in-session (`CONTEXT_ISOLATION_ONLY`; compact digest: `npx ai-engineering-loop docs AGENTS`).',
30
30
  '',
31
+ 'Guardrails: **enforced** = `npx ai-engineering-loop hooks install` adds native pre-tool hooks that check scope and destructive commands; **advisory** = the host has no hook API, so the guardrail is the loop instructions only.',
32
+ '',
31
33
  `Not supported (no native skills): ${OUT_OF_SCOPE.map((h) => h.name).join(', ')}.`,
32
34
  END
33
35
  ].join('\n');