@devrik-tools/claude-gates 0.7.2 → 0.9.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.
- package/.claude-plugin/marketplace.json +3 -3
- package/README.es.md +129 -11
- package/README.md +120 -10
- package/cli/doctor.mjs +132 -0
- package/cli/evidence.mjs +78 -0
- package/cli/hooks-manifest.mjs +89 -0
- package/cli/index.mjs +124 -6
- package/cli/init.mjs +97 -3
- package/cli/log.mjs +70 -0
- package/cli/materialize.mjs +36 -2
- package/cli/registry.mjs +12 -1
- package/cli/selection.mjs +18 -0
- package/cli/smoke-fixtures.json +159 -25
- package/cli/task.mjs +104 -11
- package/cli/toggle.mjs +125 -0
- package/package.json +4 -3
- package/plugins/gates/.claude-plugin/plugin.json +1 -1
- package/plugins/gates/hooks/doctor.mjs +47 -1
- package/plugins/gates/hooks/gates/atomic-commit/index.mjs +92 -119
- package/plugins/gates/hooks/gates/audit-before-build/index.mjs +101 -66
- package/plugins/gates/hooks/gates/autonomous-mode/index.mjs +8 -8
- package/plugins/gates/hooks/gates/autonomous-mode/stop.mjs +13 -64
- package/plugins/gates/hooks/gates/bash-commands/index.mjs +186 -163
- package/plugins/gates/hooks/gates/block-remote-publish/index.mjs +100 -101
- package/plugins/gates/hooks/gates/brief-approved/index.mjs +71 -140
- package/plugins/gates/hooks/gates/brief-before-delegate/index.mjs +56 -137
- package/plugins/gates/hooks/gates/capability-map/index.mjs +280 -506
- package/plugins/gates/hooks/gates/circuit-breaker/index.mjs +94 -215
- package/plugins/gates/hooks/gates/circuit-breaker/track.mjs +285 -0
- package/plugins/gates/hooks/gates/dependency-skills/index.mjs +127 -46
- package/plugins/gates/hooks/gates/diagnosis-before-patch/index.mjs +81 -19
- package/plugins/gates/hooks/gates/engram-first/index.mjs +41 -0
- package/plugins/gates/hooks/gates/engram-first/session-start.mjs +64 -0
- package/plugins/gates/hooks/gates/engram-first/shared.mjs +90 -0
- package/plugins/gates/hooks/gates/engram-first/stop.mjs +23 -0
- package/plugins/gates/hooks/gates/engram-first/track.mjs +97 -0
- package/plugins/gates/hooks/gates/feature-catalog/index.mjs +156 -50
- package/plugins/gates/hooks/gates/force-parallel/index.mjs +53 -95
- package/plugins/gates/hooks/gates/forge-flow/index.mjs +51 -83
- package/plugins/gates/hooks/gates/implementation-pipeline/index.mjs +40 -80
- package/plugins/gates/hooks/gates/intent-flow/index.mjs +23 -149
- package/plugins/gates/hooks/gates/library-docs/index.mjs +338 -0
- package/plugins/gates/hooks/gates/library-docs/track.mjs +95 -0
- package/plugins/gates/hooks/gates/lint-commit/index.mjs +88 -97
- package/plugins/gates/hooks/gates/mandatory-flow/index.mjs +48 -95
- package/plugins/gates/hooks/gates/neutral-spanish/index.mjs +69 -31
- package/plugins/gates/hooks/gates/never-assume/index.mjs +23 -18
- package/plugins/gates/hooks/gates/no-blocking/index.mjs +127 -86
- package/plugins/gates/hooks/gates/no-coauthor/index.mjs +108 -88
- package/plugins/gates/hooks/gates/no-explanatory-comments/index.mjs +281 -0
- package/plugins/gates/hooks/gates/no-lint-suppression/index.mjs +170 -110
- package/plugins/gates/hooks/gates/no-memory-dependency/index.mjs +46 -77
- package/plugins/gates/hooks/gates/no-reconfirm/index.mjs +85 -47
- package/plugins/gates/hooks/gates/no-trivial-scripts/index.mjs +114 -0
- package/plugins/gates/hooks/gates/protected-paths/index.mjs +137 -90
- package/plugins/gates/hooks/gates/recurrence-lock/index.mjs +95 -51
- package/plugins/gates/hooks/gates/require-monitor/index.mjs +126 -0
- package/plugins/gates/hooks/gates/require-task-split/index.mjs +88 -0
- package/plugins/gates/hooks/gates/reuse-before-build/index.mjs +72 -216
- package/plugins/gates/hooks/gates/risk-level/index.mjs +51 -204
- package/plugins/gates/hooks/gates/root-cause-first/index.mjs +34 -17
- package/plugins/gates/hooks/gates/root-whitelist/index.mjs +160 -95
- package/plugins/gates/hooks/gates/rule-skill-autodiscovery/index.mjs +196 -97
- package/plugins/gates/hooks/gates/sdd-specs/index.mjs +90 -143
- package/plugins/gates/hooks/gates/staged-lint/index.mjs +98 -106
- package/plugins/gates/hooks/gates/stop-pending/index.mjs +47 -148
- package/plugins/gates/hooks/gates/test-after-implementation/index.mjs +78 -68
- package/plugins/gates/hooks/gates/test-matrix/index.mjs +53 -88
- package/plugins/gates/hooks/gates/tool-map/index.mjs +80 -110
- package/plugins/gates/hooks/hooks.json +152 -42
- package/plugins/gates/hooks/lib/config.mjs +6 -3
- package/plugins/gates/hooks/lib/delegation.mjs +352 -0
- package/plugins/gates/hooks/lib/gate-log.mjs +141 -0
- package/plugins/gates/hooks/lib/git.mjs +262 -0
- package/plugins/gates/hooks/lib/hook-io.mjs +451 -83
- package/plugins/gates/hooks/lib/session-state.mjs +150 -0
- package/plugins/gates/hooks/lib/signals.mjs +18 -13
- package/plugins/gates/hooks/lib/testing.mjs +148 -0
- package/plugins/gates/hooks/lib/tools.mjs +144 -0
- package/plugins/tasks/.claude-plugin/plugin.json +1 -1
- package/plugins/tasks/hooks/lib/task-store.mjs +12 -6
- package/plugins/tasks/hooks/register-requests.mjs +37 -10
- package/registry.json +249 -38
|
@@ -1,22 +1,18 @@
|
|
|
1
|
-
// protected-paths — denies
|
|
2
|
-
//
|
|
3
|
-
//
|
|
4
|
-
//
|
|
5
|
-
//
|
|
6
|
-
//
|
|
7
|
-
//
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
// command may target. Replaces the built-in list wholesale.
|
|
11
|
-
// mutatingCommands shell command names treated as mutating, so a merely-reading
|
|
12
|
-
// command that mentions a protected path (echo, cat, grep) is
|
|
13
|
-
// never denied. Replaces the built-in list wholesale.
|
|
14
|
-
// The defaults live here, in the source, so a project reads them and knows exactly what
|
|
15
|
-
// its override replaces.
|
|
16
|
-
|
|
1
|
+
// protected-paths — denies a write, a shell redirection or a mutating shell command that
|
|
2
|
+
// targets a protected path (.env files, lockfiles, the harness's own hooks). Deliberate
|
|
3
|
+
// semantics: a fragment without a slash names a file by its exact basename (`.env` is not
|
|
4
|
+
// `.env.example`), a fragment ending in a slash names a contiguous directory sequence
|
|
5
|
+
// (`.claude/hooks/` is not `src/hooks/`). Only the command segment that RUNS a mutating
|
|
6
|
+
// command is judged, so `npm install; cat .env` reads and is allowed. A path built from a
|
|
7
|
+
// variable is not resolved.
|
|
8
|
+
|
|
9
|
+
import { posix } from 'node:path';
|
|
17
10
|
import {
|
|
18
|
-
|
|
11
|
+
compileRegexList,
|
|
19
12
|
deny,
|
|
13
|
+
runGate,
|
|
14
|
+
shellCommandOf,
|
|
15
|
+
shellWrittenPaths,
|
|
20
16
|
toolInGroups,
|
|
21
17
|
writtenPathOf,
|
|
22
18
|
} from '../../lib/hook-io.mjs';
|
|
@@ -26,94 +22,145 @@ const CONFIG_KEY = 'blockWritesToProtectedPaths';
|
|
|
26
22
|
|
|
27
23
|
const DEFAULT_PROTECTED_PATHS = [
|
|
28
24
|
'.env',
|
|
25
|
+
'.env.local',
|
|
26
|
+
'.env.production',
|
|
27
|
+
'.env.development',
|
|
29
28
|
'package-lock.json',
|
|
30
29
|
'yarn.lock',
|
|
31
30
|
'pnpm-lock.yaml',
|
|
32
|
-
'hooks/',
|
|
31
|
+
'.claude/hooks/',
|
|
33
32
|
];
|
|
34
33
|
|
|
34
|
+
// Regex sources matched at the start of a command segment. A command is mutating when its
|
|
35
|
+
// ARGUMENTS name the protected path; redirections are covered separately by shellWrittenPaths.
|
|
35
36
|
const DEFAULT_MUTATING_COMMANDS = [
|
|
36
37
|
'touch',
|
|
37
38
|
'rm',
|
|
38
39
|
'cp',
|
|
39
40
|
'mv',
|
|
40
|
-
|
|
41
|
+
String.raw`sed\s+(?:-i|--in-place)`,
|
|
41
42
|
'tee',
|
|
42
43
|
'install',
|
|
43
44
|
'chmod',
|
|
44
45
|
'chown',
|
|
45
46
|
'truncate',
|
|
47
|
+
'unlink',
|
|
48
|
+
'dd',
|
|
49
|
+
String.raw`perl\s+-\w*i`,
|
|
50
|
+
String.raw`git\s+checkout\s+--`,
|
|
51
|
+
String.raw`git\s+restore`,
|
|
52
|
+
'Set-Content',
|
|
53
|
+
'Add-Content',
|
|
54
|
+
'Out-File',
|
|
55
|
+
'Remove-Item',
|
|
56
|
+
'del',
|
|
57
|
+
'erase',
|
|
58
|
+
'Copy-Item',
|
|
59
|
+
'Move-Item',
|
|
60
|
+
'New-Item',
|
|
61
|
+
'Rename-Item',
|
|
62
|
+
'copy',
|
|
63
|
+
'move',
|
|
64
|
+
'ren',
|
|
46
65
|
];
|
|
47
66
|
|
|
48
|
-
|
|
49
|
-
|
|
67
|
+
// ── Path matching ───────────────────────────────────────────────────────────────────
|
|
68
|
+
function pathSegmentsOf(path) {
|
|
69
|
+
const normalized = posix.normalize(String(path).replace(/\\/g, '/'));
|
|
70
|
+
return normalized.toLowerCase().split('/').filter(Boolean);
|
|
50
71
|
}
|
|
51
72
|
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
73
|
+
function fragmentRule(fragment) {
|
|
74
|
+
const segments = pathSegmentsOf(fragment);
|
|
75
|
+
return {
|
|
76
|
+
fragment,
|
|
77
|
+
segments,
|
|
78
|
+
anchoredAtEnd: !String(fragment).endsWith('/'),
|
|
79
|
+
};
|
|
58
80
|
}
|
|
59
81
|
|
|
60
|
-
function
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
normalized.includes(fragment.toLowerCase()),
|
|
82
|
+
function segmentsMatchAt(pathSegments, ruleSegments, offset) {
|
|
83
|
+
return ruleSegments.every(
|
|
84
|
+
(segment, index) => pathSegments[offset + index] === segment,
|
|
64
85
|
);
|
|
65
86
|
}
|
|
66
87
|
|
|
67
|
-
function
|
|
68
|
-
const
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
)
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
88
|
+
function ruleMatches(rule, pathSegments) {
|
|
89
|
+
const { segments, anchoredAtEnd } = rule;
|
|
90
|
+
if (segments.length === 0 || pathSegments.length < segments.length)
|
|
91
|
+
return false;
|
|
92
|
+
if (anchoredAtEnd) {
|
|
93
|
+
return segmentsMatchAt(
|
|
94
|
+
pathSegments,
|
|
95
|
+
segments,
|
|
96
|
+
pathSegments.length - segments.length,
|
|
97
|
+
);
|
|
98
|
+
}
|
|
99
|
+
const lastOffset = pathSegments.length - segments.length;
|
|
100
|
+
for (let offset = 0; offset <= lastOffset; offset += 1) {
|
|
101
|
+
if (segmentsMatchAt(pathSegments, segments, offset)) return true;
|
|
102
|
+
}
|
|
103
|
+
return false;
|
|
83
104
|
}
|
|
84
105
|
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
106
|
+
function protectedFragmentOf(path, rules) {
|
|
107
|
+
const pathSegments = pathSegmentsOf(path);
|
|
108
|
+
return rules.find((rule) => ruleMatches(rule, pathSegments))?.fragment;
|
|
109
|
+
}
|
|
110
|
+
|
|
111
|
+
// ── Shell command arguments ─────────────────────────────────────────────────────────
|
|
112
|
+
const SEGMENT_SEPARATOR = /;|&&|\|\||\||\n/;
|
|
113
|
+
const ARGUMENT_PATTERN = /"([^"]*)"|'([^']*)'|(\S+)/g;
|
|
114
|
+
const KEY_VALUE_PREFIX = /^-{0,2}[\w-]+=/;
|
|
115
|
+
|
|
116
|
+
function stripLeadingWrappers(segment) {
|
|
117
|
+
const tokens = segment.trim().split(/\s+/);
|
|
118
|
+
while (tokens.length > 0 && /^(?:sudo|command|env|\w+=\S*)$/i.test(tokens[0]))
|
|
119
|
+
tokens.shift();
|
|
120
|
+
return tokens.join(' ');
|
|
121
|
+
}
|
|
122
|
+
|
|
123
|
+
function argumentsOf(text) {
|
|
124
|
+
const found = [];
|
|
125
|
+
for (const match of text.matchAll(ARGUMENT_PATTERN)) {
|
|
126
|
+
const token =
|
|
127
|
+
match[1] ?? match[2] ?? match[3].replace(KEY_VALUE_PREFIX, '');
|
|
128
|
+
if (token && !/[$`%]/.test(token)) found.push(token);
|
|
93
129
|
}
|
|
130
|
+
return found;
|
|
94
131
|
}
|
|
95
132
|
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
const
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
(
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
|
|
133
|
+
function mutatingCommandPatterns(sources) {
|
|
134
|
+
return compileRegexList(
|
|
135
|
+
sources.map((source) => String.raw`^(?:${source})(?=\s|$)`),
|
|
136
|
+
).patterns;
|
|
137
|
+
}
|
|
138
|
+
|
|
139
|
+
function mutatedArguments(command, patterns) {
|
|
140
|
+
const found = [];
|
|
141
|
+
for (const rawSegment of String(command).split(SEGMENT_SEPARATOR)) {
|
|
142
|
+
const segment = stripLeadingWrappers(rawSegment);
|
|
143
|
+
const matched = patterns
|
|
144
|
+
.map((pattern) => pattern.exec(segment))
|
|
145
|
+
.find(Boolean);
|
|
146
|
+
if (matched) found.push(...argumentsOf(segment.slice(matched[0].length)));
|
|
147
|
+
}
|
|
148
|
+
return found;
|
|
149
|
+
}
|
|
150
|
+
|
|
151
|
+
function denyProtected(target, fragment) {
|
|
152
|
+
deny(
|
|
153
|
+
CONFIG_KEY,
|
|
154
|
+
`Writing to '${target}' is not allowed: it matches the protected path '${fragment}'. ` +
|
|
155
|
+
'Reading it is fine; to change it, ask the user, or edit protectedPaths under ' +
|
|
156
|
+
`${CONFIG_KEY} in .ai/config.json.`,
|
|
157
|
+
);
|
|
158
|
+
}
|
|
159
|
+
|
|
160
|
+
function checkTargets(targets, rules) {
|
|
161
|
+
for (const target of targets) {
|
|
162
|
+
const fragment = protectedFragmentOf(target, rules);
|
|
163
|
+
if (fragment) denyProtected(target, fragment);
|
|
117
164
|
}
|
|
118
165
|
}
|
|
119
166
|
|
|
@@ -128,22 +175,22 @@ runGate(
|
|
|
128
175
|
},
|
|
129
176
|
},
|
|
130
177
|
({ toolName, toolInput, parameters }) => {
|
|
131
|
-
const
|
|
132
|
-
|
|
133
|
-
|
|
178
|
+
const rules = parameters.protectedPaths
|
|
179
|
+
.map(fragmentRule)
|
|
180
|
+
.filter((rule) => rule.segments.length > 0);
|
|
181
|
+
if (rules.length === 0) return;
|
|
134
182
|
|
|
135
|
-
|
|
136
|
-
|
|
137
|
-
|
|
138
|
-
if (isWrite) {
|
|
139
|
-
checkWrite(toolInput, protectedPaths);
|
|
183
|
+
if (toolInGroups(toolName, ['write'])) {
|
|
184
|
+
checkTargets([writtenPathOf(toolInput)].filter(Boolean), rules);
|
|
140
185
|
return;
|
|
141
186
|
}
|
|
142
|
-
|
|
143
|
-
|
|
144
|
-
|
|
145
|
-
|
|
146
|
-
|
|
147
|
-
);
|
|
187
|
+
if (!toolInGroups(toolName, ['shell'])) return;
|
|
188
|
+
|
|
189
|
+
const command = shellCommandOf(toolInput);
|
|
190
|
+
if (!command.trim()) return;
|
|
191
|
+
checkTargets(shellWrittenPaths(command), rules);
|
|
192
|
+
const patterns = mutatingCommandPatterns(parameters.mutatingCommands);
|
|
193
|
+
if (patterns.length > 0)
|
|
194
|
+
checkTargets(mutatedArguments(command, patterns), rules);
|
|
148
195
|
},
|
|
149
196
|
);
|
|
@@ -1,66 +1,108 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
|
|
1
|
+
// recurrence-lock — while a registered defect class is open and at/above the threshold,
|
|
2
|
+
// mutating work is denied until its root cause is closed. Whatever is needed to CLOSE the
|
|
3
|
+
// class stays allowed (editing the recurrence file, read-only inspection, read-only
|
|
4
|
+
// delegation), otherwise the lock would deadlock on its own remedy.
|
|
5
|
+
|
|
6
|
+
import { basename, join } from 'node:path';
|
|
7
|
+
import { projectRootOf, readJsonOrNull } from '../../lib/config.mjs';
|
|
8
|
+
import { isExemptQuery } from '../../lib/delegation.mjs';
|
|
9
|
+
import {
|
|
10
|
+
runGate,
|
|
11
|
+
deny,
|
|
12
|
+
toolInGroups,
|
|
13
|
+
writtenPathOf,
|
|
14
|
+
shellCommandOf,
|
|
15
|
+
shellWrittenPaths,
|
|
16
|
+
delegationPromptOf,
|
|
17
|
+
} from '../../lib/hook-io.mjs';
|
|
4
18
|
|
|
5
19
|
const GATE_ID = 'recurrence-lock';
|
|
6
20
|
const CONFIG_KEY = 'blockRegisteredRecurrences';
|
|
7
21
|
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
22
|
+
const RECURRENCES_FILE = 'reincidencias.json';
|
|
23
|
+
const RECURRENCES_RELATIVE_PATH = join('.ai', RECURRENCES_FILE);
|
|
24
|
+
const DEFAULT_THRESHOLD = 2;
|
|
25
|
+
|
|
26
|
+
const READ_ONLY_COMMANDS = [
|
|
27
|
+
'git status',
|
|
28
|
+
'git log',
|
|
29
|
+
'git diff',
|
|
30
|
+
'git show',
|
|
31
|
+
'git branch',
|
|
32
|
+
'cat',
|
|
33
|
+
'ls',
|
|
34
|
+
'dir',
|
|
35
|
+
'pwd',
|
|
36
|
+
'echo',
|
|
37
|
+
'grep',
|
|
38
|
+
'rg',
|
|
39
|
+
'find',
|
|
40
|
+
'head',
|
|
41
|
+
'tail',
|
|
42
|
+
'wc',
|
|
43
|
+
'type',
|
|
44
|
+
'get-content',
|
|
45
|
+
'get-childitem',
|
|
46
|
+
'node --test',
|
|
47
|
+
'npm test',
|
|
48
|
+
'npm run lint',
|
|
49
|
+
];
|
|
50
|
+
const SEGMENT_SEPARATOR = /&&|\|\||[;|\n]/;
|
|
51
|
+
|
|
52
|
+
function segmentIsReadOnly(segment) {
|
|
53
|
+
const words = segment.trim().toLowerCase().split(/\s+/);
|
|
54
|
+
return READ_ONLY_COMMANDS.some((command) => {
|
|
55
|
+
const expected = command.split(' ');
|
|
56
|
+
return expected.every((word, index) => words[index] === word);
|
|
57
|
+
});
|
|
58
|
+
}
|
|
59
|
+
|
|
60
|
+
function isReadOnlyCommand(command) {
|
|
61
|
+
if (!command.trim()) return false;
|
|
62
|
+
if (shellWrittenPaths(command).length > 0) return false;
|
|
63
|
+
return command.split(SEGMENT_SEPARATOR).every(segmentIsReadOnly);
|
|
64
|
+
}
|
|
65
|
+
|
|
66
|
+
function isExemptCall(toolName, toolInput) {
|
|
67
|
+
if (toolInGroups(toolName, ['write']))
|
|
68
|
+
return basename(writtenPathOf(toolInput)) === RECURRENCES_FILE;
|
|
69
|
+
if (toolInGroups(toolName, ['shell']))
|
|
70
|
+
return isReadOnlyCommand(shellCommandOf(toolInput));
|
|
71
|
+
if (toolInGroups(toolName, ['delegation']))
|
|
72
|
+
return isExemptQuery(delegationPromptOf(toolInput));
|
|
73
|
+
return false;
|
|
74
|
+
}
|
|
75
|
+
|
|
76
|
+
// Identity is id/hash when present, else the value itself: the same occurrence logged
|
|
77
|
+
// twice must not count as two.
|
|
16
78
|
function dedupOccurrences(occurrences) {
|
|
17
79
|
const seen = new Set();
|
|
18
|
-
|
|
19
|
-
for (const occurrence of occurrences) {
|
|
80
|
+
return occurrences.filter((occurrence) => {
|
|
20
81
|
const identity =
|
|
21
82
|
occurrence && typeof occurrence === 'object'
|
|
22
83
|
? String(occurrence.id ?? occurrence.hash ?? JSON.stringify(occurrence))
|
|
23
84
|
: String(occurrence);
|
|
24
|
-
if (seen.has(identity))
|
|
85
|
+
if (seen.has(identity)) return false;
|
|
25
86
|
seen.add(identity);
|
|
26
|
-
|
|
27
|
-
}
|
|
28
|
-
return deduped;
|
|
87
|
+
return true;
|
|
88
|
+
});
|
|
29
89
|
}
|
|
30
90
|
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
// If neither the module nor the file exists, it allows silently — there is
|
|
38
|
-
// nothing to enforce without a recurrence record.
|
|
39
|
-
function loadOpenRecurrences(projectRoot, thresholdAppearances) {
|
|
40
|
-
const filePath = join(projectRoot, '.ai', 'reincidencias.json');
|
|
41
|
-
if (!existsSync(filePath)) return [];
|
|
42
|
-
|
|
43
|
-
let parsed;
|
|
44
|
-
try {
|
|
45
|
-
parsed = JSON.parse(readFileSync(filePath, 'utf8'));
|
|
46
|
-
} catch {
|
|
47
|
-
return [];
|
|
48
|
-
}
|
|
91
|
+
function isClosed(entry) {
|
|
92
|
+
const status = String(entry?.status ?? '')
|
|
93
|
+
.trim()
|
|
94
|
+
.toLowerCase();
|
|
95
|
+
return status === 'closed' || status === 'cerrada';
|
|
96
|
+
}
|
|
49
97
|
|
|
98
|
+
function loadOpenRecurrences(root, thresholdAppearances) {
|
|
99
|
+
const parsed = readJsonOrNull(join(root, RECURRENCES_RELATIVE_PATH));
|
|
50
100
|
const classes = Array.isArray(parsed?.classes) ? parsed.classes : [];
|
|
51
101
|
return classes.filter((entry) => {
|
|
52
102
|
const occurrences = Array.isArray(entry?.occurrences)
|
|
53
103
|
? dedupOccurrences(entry.occurrences)
|
|
54
104
|
: [];
|
|
55
|
-
|
|
56
|
-
// Trim before lowercasing so a padded status ("Closed ", "cerrada\n") is recognized —
|
|
57
|
-
// a status compared without trimming left a validly-closed (but padded) recurrence
|
|
58
|
-
// stuck as still-open, blocking unrelated work indefinitely.
|
|
59
|
-
const status = String(entry?.status ?? '')
|
|
60
|
-
.trim()
|
|
61
|
-
.toLowerCase();
|
|
62
|
-
const isClosed = status === 'closed' || status === 'cerrada';
|
|
63
|
-
return occurrenceCount >= thresholdAppearances && !isClosed;
|
|
105
|
+
return occurrences.length >= thresholdAppearances && !isClosed(entry);
|
|
64
106
|
});
|
|
65
107
|
}
|
|
66
108
|
|
|
@@ -70,15 +112,16 @@ runGate(
|
|
|
70
112
|
configKey: CONFIG_KEY,
|
|
71
113
|
enabledByDefault: true,
|
|
72
114
|
defaultParams: {
|
|
73
|
-
thresholdAppearances:
|
|
115
|
+
thresholdAppearances: DEFAULT_THRESHOLD,
|
|
74
116
|
},
|
|
75
117
|
},
|
|
76
|
-
({ toolName, parameters }) => {
|
|
118
|
+
({ toolName, toolInput, parameters, cwd }) => {
|
|
77
119
|
if (!toolInGroups(toolName, ['execution', 'delegation'])) return;
|
|
120
|
+
if (isExemptCall(toolName, toolInput)) return;
|
|
78
121
|
|
|
79
|
-
const
|
|
122
|
+
const root = projectRootOf(cwd) ?? cwd;
|
|
80
123
|
const openRecurrences = loadOpenRecurrences(
|
|
81
|
-
|
|
124
|
+
root,
|
|
82
125
|
parameters.thresholdAppearances,
|
|
83
126
|
);
|
|
84
127
|
if (openRecurrences.length === 0) return;
|
|
@@ -88,9 +131,10 @@ runGate(
|
|
|
88
131
|
CONFIG_KEY,
|
|
89
132
|
`Registered recurring issue class(es) still open and at/above the ` +
|
|
90
133
|
`${parameters.thresholdAppearances}-occurrence threshold: ${names}. Fix the root ` +
|
|
91
|
-
`cause of the class (not this one instance), then in ${
|
|
92
|
-
'class\'s "status" to "closed" (or "cerrada") before proceeding —
|
|
93
|
-
'
|
|
134
|
+
`cause of the class (not this one instance), then in ${RECURRENCES_RELATIVE_PATH} set that ` +
|
|
135
|
+
'class\'s "status" to "closed" (or "cerrada") before proceeding — editing that file, ' +
|
|
136
|
+
'read-only commands (git status/log/diff, cat, grep, tests) and read-only delegations ' +
|
|
137
|
+
'stay allowed so you can do exactly that.',
|
|
94
138
|
);
|
|
95
139
|
},
|
|
96
140
|
);
|
|
@@ -0,0 +1,126 @@
|
|
|
1
|
+
// require-monitor — enforces that every background command has an associated Monitor.
|
|
2
|
+
// Two-phase enforcement:
|
|
3
|
+
// 1. PreToolUse on shell: a command with run_in_background: true MUST carry the marker
|
|
4
|
+
// MONITOR-PLANNED: <reason>. Without it, the background is denied.
|
|
5
|
+
// 2. PreToolUse on execution+monitor: if the session has pending unmonitored backgrounds
|
|
6
|
+
// and the current tool is NOT Monitor, deny — forcing the agent to create a Monitor
|
|
7
|
+
// before doing anything else. When the current tool IS Monitor, the pending background
|
|
8
|
+
// is cleared.
|
|
9
|
+
//
|
|
10
|
+
// The marker is deterministic: no model judgment required. The pending-check is deterministic
|
|
11
|
+
// too: state is on disk, the check is a read + compare, and the deny is unconditional.
|
|
12
|
+
|
|
13
|
+
import {
|
|
14
|
+
deny,
|
|
15
|
+
runGate,
|
|
16
|
+
shellCommandOf,
|
|
17
|
+
toolInGroups,
|
|
18
|
+
} from '../../lib/hook-io.mjs';
|
|
19
|
+
import {
|
|
20
|
+
readSessionState,
|
|
21
|
+
writeSessionState,
|
|
22
|
+
} from '../../lib/session-state.mjs';
|
|
23
|
+
|
|
24
|
+
const GATE_ID = 'require-monitor';
|
|
25
|
+
const CONFIG_KEY = 'requireMonitorForBackground';
|
|
26
|
+
|
|
27
|
+
const DEFAULT_MARKER = 'MONITOR-PLANNED:';
|
|
28
|
+
const MARKER_REASON_PATTERN = /\S\s\S/;
|
|
29
|
+
const MAX_PENDING = 20;
|
|
30
|
+
const MAX_COMMAND_PREVIEW_LENGTH = 200;
|
|
31
|
+
|
|
32
|
+
function hasMarker(command, marker) {
|
|
33
|
+
if (!marker) return false;
|
|
34
|
+
const markerIndex = command.indexOf(marker);
|
|
35
|
+
if (markerIndex === -1) return false;
|
|
36
|
+
const afterMarker = command.slice(markerIndex + marker.length);
|
|
37
|
+
return MARKER_REASON_PATTERN.test(afterMarker);
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
function pendingBackgrounds(state) {
|
|
41
|
+
return Array.isArray(state.pending) ? state.pending : [];
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
function addPending(state, entry) {
|
|
45
|
+
const pending = [...pendingBackgrounds(state), entry].slice(-MAX_PENDING);
|
|
46
|
+
return { ...state, pending };
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
function clearOldestPending(state) {
|
|
50
|
+
const pending = pendingBackgrounds(state);
|
|
51
|
+
if (pending.length === 0) return state;
|
|
52
|
+
return { ...state, pending: pending.slice(1) };
|
|
53
|
+
}
|
|
54
|
+
|
|
55
|
+
runGate(
|
|
56
|
+
{
|
|
57
|
+
id: GATE_ID,
|
|
58
|
+
configKey: CONFIG_KEY,
|
|
59
|
+
enabledByDefault: true,
|
|
60
|
+
defaultParams: {
|
|
61
|
+
monitorPlannedMarker: DEFAULT_MARKER,
|
|
62
|
+
},
|
|
63
|
+
},
|
|
64
|
+
({ toolName, toolInput, sessionId, parameters, cwd }) => {
|
|
65
|
+
const isShell = toolInGroups(toolName, ['shell']);
|
|
66
|
+
const isMonitor = toolInGroups(toolName, ['monitor']);
|
|
67
|
+
const isExecution = toolInGroups(toolName, ['execution']);
|
|
68
|
+
|
|
69
|
+
if (!isShell && !isMonitor && !isExecution) return;
|
|
70
|
+
|
|
71
|
+
const stateOptions = { cwd };
|
|
72
|
+
const state = readSessionState(GATE_ID, sessionId, {}, stateOptions);
|
|
73
|
+
const marker = String(parameters.monitorPlannedMarker ?? DEFAULT_MARKER);
|
|
74
|
+
|
|
75
|
+
// Phase 2: if Monitor tool, clear oldest pending and allow.
|
|
76
|
+
if (isMonitor) {
|
|
77
|
+
const pending = pendingBackgrounds(state);
|
|
78
|
+
if (pending.length > 0) {
|
|
79
|
+
writeSessionState(
|
|
80
|
+
GATE_ID,
|
|
81
|
+
sessionId,
|
|
82
|
+
clearOldestPending(state),
|
|
83
|
+
stateOptions,
|
|
84
|
+
);
|
|
85
|
+
}
|
|
86
|
+
return;
|
|
87
|
+
}
|
|
88
|
+
|
|
89
|
+
// Phase 1: shell with run_in_background requires the marker.
|
|
90
|
+
if (isShell && toolInput.run_in_background === true) {
|
|
91
|
+
const command = shellCommandOf(toolInput);
|
|
92
|
+
if (!hasMarker(command, marker)) {
|
|
93
|
+
deny(
|
|
94
|
+
CONFIG_KEY,
|
|
95
|
+
`A background command MUST declare its Monitor plan. Add "${marker} <what the ` +
|
|
96
|
+
'Monitor will watch for>" to the command. Every background process needs a ' +
|
|
97
|
+
'Monitor — an unmonitored background is invisible work that can hang or fail ' +
|
|
98
|
+
'silently. The marker is a commitment to create a Monitor immediately after.',
|
|
99
|
+
);
|
|
100
|
+
}
|
|
101
|
+
writeSessionState(
|
|
102
|
+
GATE_ID,
|
|
103
|
+
sessionId,
|
|
104
|
+
addPending(state, {
|
|
105
|
+
command: command.slice(0, MAX_COMMAND_PREVIEW_LENGTH),
|
|
106
|
+
startedAt: Date.now(),
|
|
107
|
+
}),
|
|
108
|
+
stateOptions,
|
|
109
|
+
);
|
|
110
|
+
return;
|
|
111
|
+
}
|
|
112
|
+
|
|
113
|
+
// Phase 2: any execution/shell tool while backgrounds are unmonitored.
|
|
114
|
+
const pending = pendingBackgrounds(state);
|
|
115
|
+
if (pending.length > 0) {
|
|
116
|
+
const oldest = pending[0];
|
|
117
|
+
deny(
|
|
118
|
+
CONFIG_KEY,
|
|
119
|
+
`There are ${pending.length} background command(s) without a Monitor. Create a ` +
|
|
120
|
+
`Monitor for the pending background before doing anything else. Oldest pending: ` +
|
|
121
|
+
`"${oldest.command ?? '(unknown)'}". Use the Monitor tool to observe it, then ` +
|
|
122
|
+
'proceed with your next action.',
|
|
123
|
+
);
|
|
124
|
+
}
|
|
125
|
+
},
|
|
126
|
+
);
|