@venizia/vulcan 0.4.0 → 0.4.2
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/base/claude/remote-hosts.json +1 -1
- package/base/claude/settings.json +75 -3
- package/base/hooks/guard-git.ts +55 -0
- package/base/hooks/guard-manifest.ts +1 -1
- package/base/hooks/guard-outside.ts +66 -0
- package/base/hooks/guard-package.ts +3 -2
- package/base/hooks/guard-remote-hosts.ts +3 -2
- package/base/hooks/guard-writes.ts +1 -1
- package/base/hooks/post-tool.ts +36 -0
- package/base/hooks/prompt-submit.ts +52 -0
- package/base/hooks/stop.ts +38 -0
- package/base/hooks/subagent-start.ts +18 -0
- package/base/references/delegation-brief.md +63 -0
- package/base/references/design-pass.md +39 -0
- package/base/references/evidence.md +36 -0
- package/base/references/phase-boundaries.md +20 -0
- package/base/references/testing.md +89 -0
- package/base/rules.md +11 -2
- package/base/skills/build-a-feature/SKILL.md +49 -23
- package/base/skills/docs-update/SKILL.md +12 -1
- package/base/skills/docs-writing/SKILL.md +2 -2
- package/base/skills/docs-writing/references/agent-documents.md +53 -0
- package/base/skills/fix-a-bug/SKILL.md +84 -0
- package/base/skills/fix-a-bug/evals/evals.json +17 -0
- package/base/skills/knowledge-sync/SKILL.md +4 -1
- package/base/skills/knowledge-sync/knowledge-sync.workflow.js +10 -10
- package/base/skills/plan-a-change/SKILL.md +95 -0
- package/base/skills/plan-a-change/evals/evals.json +23 -0
- package/base/skills/plan-a-change/references/spec-card.md +39 -0
- package/base/skills/report-back/SKILL.md +112 -0
- package/base/skills/report-back/evals/evals.json +23 -0
- package/base/skills/research/SKILL.md +64 -0
- package/base/skills/research/evals/evals.json +17 -0
- package/base/skills/retro/SKILL.md +69 -0
- package/base/skills/retro/evals/evals.json +17 -0
- package/base/skills/review-a-change/SKILL.md +90 -0
- package/base/skills/review-a-change/evals/evals.json +17 -0
- package/base/skills/ship/SKILL.md +82 -0
- package/base/skills/ship/evals/evals.json +23 -0
- package/base/skills/visualize-work/SKILL.md +87 -0
- package/base/skills/visualize-work/evals/evals.json +17 -0
- package/base/skills/visualize-work/references/spec.md +65 -0
- package/package.json +3 -3
- package/src/agents/common/schema.ts +2 -2
- package/src/agents/detect.ts +5 -5
- package/src/agents/exclude.ts +3 -3
- package/src/agents/local.ts +1 -1
- package/src/agents/prompt.ts +2 -2
- package/src/agents/render.ts +2 -2
- package/src/agents/resolve.ts +6 -6
- package/src/agents/table.ts +1 -1
- package/src/commands/agents.command.ts +4 -4
- package/src/commands/clean.command.ts +72 -0
- package/src/commands/help.command.ts +11 -0
- package/src/commands/index.ts +6 -0
- package/src/commands/init.command.ts +4 -1
- package/src/commands/profile.command.ts +255 -0
- package/src/commands/sync.command.ts +6 -4
- package/src/commands/upgrade.command.ts +1 -1
- package/src/commands/visualize.command.ts +107 -0
- package/src/common/constants.ts +23 -0
- package/src/config/common/constants.ts +2 -0
- package/src/config/loader.ts +4 -4
- package/src/config/packages.ts +1 -1
- package/src/config/schema.ts +16 -5
- package/src/create/add-app.ts +15 -15
- package/src/create/add-lib.ts +7 -7
- package/src/create/bunfig.ts +1 -1
- package/src/create/dockerignore.ts +4 -4
- package/src/create/init-monorepo.ts +3 -1
- package/src/create/lib-preset.ts +1 -1
- package/src/create/names.ts +7 -6
- package/src/create/next.ts +4 -4
- package/src/create/preset-menu.ts +4 -4
- package/src/create/preset.ts +3 -3
- package/src/create/scratch.ts +27 -6
- package/src/create/workspace.ts +11 -11
- package/src/doctor/checks.ts +231 -45
- package/src/doctor/common/constants.ts +4 -0
- package/src/doctor/makefile.ts +3 -3
- package/src/doctor/runner.ts +16 -1
- package/src/doctor/skills.ts +8 -8
- package/src/doctor/tags.ts +5 -5
- package/src/hooks/common/constants.ts +29 -0
- package/src/hooks/common/types.ts +20 -0
- package/src/hooks/decision.ts +15 -0
- package/src/hooks/guard-git.ts +288 -0
- package/src/hooks/guard-manifest.ts +1 -1
- package/src/hooks/guard-outside.ts +399 -0
- package/src/hooks/guard-package.ts +4 -4
- package/src/hooks/guard-writes.ts +5 -5
- package/src/hooks/index.ts +6 -0
- package/src/hooks/owner-reply.ts +148 -0
- package/src/hooks/post-tool.ts +137 -0
- package/src/hooks/remote-hosts.ts +25 -23
- package/src/hooks/session-digest.ts +24 -4
- package/src/hooks/shell.ts +325 -0
- package/src/hooks/subagent-card.ts +32 -0
- package/src/init/scaffold.ts +6 -6
- package/src/local/clean.ts +88 -0
- package/src/local/constants.ts +50 -0
- package/src/local/format.ts +25 -0
- package/src/local/index.ts +4 -0
- package/src/local/scan.ts +368 -0
- package/src/manifest/render.ts +1 -1
- package/src/owner/card.ts +85 -0
- package/src/owner/constants.ts +105 -0
- package/src/owner/index.ts +8 -0
- package/src/owner/profile.ts +123 -0
- package/src/owner/schema.ts +147 -0
- package/src/owner/shape.ts +388 -0
- package/src/owner/survey.ts +289 -0
- package/src/owner/view.ts +76 -0
- package/src/owner/violations.ts +202 -0
- package/src/router.ts +18 -1
- package/src/rules/base.ts +1 -1
- package/src/rules/parser.ts +5 -5
- package/src/signals/claims.ts +71 -0
- package/src/signals/classify.ts +132 -0
- package/src/signals/constants.ts +88 -0
- package/src/signals/index.ts +5 -0
- package/src/signals/loop.ts +46 -0
- package/src/signals/state.ts +276 -0
- package/src/sync/agent-files.ts +4 -4
- package/src/sync/agent-removal.ts +6 -6
- package/src/sync/atlas-block.ts +1 -1
- package/src/sync/base-rules.ts +1 -1
- package/src/sync/exclude.ts +9 -9
- package/src/sync/local-choice.ts +35 -5
- package/src/sync/planner.ts +16 -16
- package/src/sync/prune.ts +3 -3
- package/src/sync/settings.ts +15 -15
- package/src/ui/prompt.ts +3 -3
- package/src/visualize/assets.ts +80 -0
- package/src/visualize/constants.ts +126 -0
- package/src/visualize/index.ts +6 -0
- package/src/visualize/layout.ts +107 -0
- package/src/visualize/page.ts +211 -0
- package/src/visualize/render.ts +161 -0
- package/src/visualize/schema.ts +178 -0
- package/src/visualize/validate.ts +127 -0
|
@@ -0,0 +1,399 @@
|
|
|
1
|
+
import { lstatSync, readlinkSync, realpathSync } from 'node:fs';
|
|
2
|
+
import { tmpdir } from 'node:os';
|
|
3
|
+
import { dirname, isAbsolute, join, resolve, sep } from 'node:path';
|
|
4
|
+
import { basenameOf, parseShell, programOf } from './shell.ts';
|
|
5
|
+
import type { ISimpleCommand } from './shell.ts';
|
|
6
|
+
import { HookConstants } from './common/index.ts';
|
|
7
|
+
import type { IManifestHookInput, IOutsideHit } from './common/index.ts';
|
|
8
|
+
|
|
9
|
+
const FILE_TOOLS = new Set(['Write', 'Edit', 'MultiEdit', 'NotebookEdit']);
|
|
10
|
+
|
|
11
|
+
type TEnvironment = Record<string, string | undefined>;
|
|
12
|
+
|
|
13
|
+
/** `~` and `$NAME` / `${NAME}` expanded; `undefined` when a variable is unset or the word is not a plain path. */
|
|
14
|
+
const expandPath = (opts: {
|
|
15
|
+
word: string;
|
|
16
|
+
home: string;
|
|
17
|
+
env: TEnvironment;
|
|
18
|
+
}): string | undefined => {
|
|
19
|
+
if (opts.word === '' || /[`*?[]/.test(opts.word) || opts.word.includes('$(')) {
|
|
20
|
+
return undefined;
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
let unresolved = false;
|
|
24
|
+
const home = opts.word === '~' || opts.word.startsWith('~/') ? opts.home : undefined;
|
|
25
|
+
const withHome = home === undefined ? opts.word : `${home}${opts.word.slice(1)}`;
|
|
26
|
+
const expanded = withHome.replace(/\$\{?(\w+)\}?/g, (_, name: string) => {
|
|
27
|
+
const value = opts.env[name];
|
|
28
|
+
|
|
29
|
+
if (value === undefined || value === '') {
|
|
30
|
+
unresolved = true;
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
return value ?? '';
|
|
34
|
+
});
|
|
35
|
+
|
|
36
|
+
return unresolved ? undefined : expanded;
|
|
37
|
+
};
|
|
38
|
+
|
|
39
|
+
/**
|
|
40
|
+
* The path with the nearest existing ancestor resolved through symlinks, so a link cannot walk out of the
|
|
41
|
+
* project; a dangling link is followed to where it points.
|
|
42
|
+
*/
|
|
43
|
+
const realOf = (opts: { path: string; depth?: number }): string => {
|
|
44
|
+
const depth = opts.depth ?? 0;
|
|
45
|
+
let existing = opts.path;
|
|
46
|
+
const rest: string[] = [];
|
|
47
|
+
|
|
48
|
+
while (dirname(existing) !== existing) {
|
|
49
|
+
try {
|
|
50
|
+
lstatSync(existing);
|
|
51
|
+
break;
|
|
52
|
+
} catch {
|
|
53
|
+
rest.unshift(existing.slice(dirname(existing).length + 1));
|
|
54
|
+
existing = dirname(existing);
|
|
55
|
+
}
|
|
56
|
+
}
|
|
57
|
+
|
|
58
|
+
let base: string;
|
|
59
|
+
|
|
60
|
+
try {
|
|
61
|
+
base = realpathSync(existing);
|
|
62
|
+
} catch {
|
|
63
|
+
// A dangling link: its target is where a write through it lands.
|
|
64
|
+
const target = depth < 8 ? readlinkSync(existing) : existing;
|
|
65
|
+
|
|
66
|
+
base = realOf({ path: resolve(dirname(existing), target), depth: depth + 1 });
|
|
67
|
+
}
|
|
68
|
+
|
|
69
|
+
return join(base, ...rest);
|
|
70
|
+
};
|
|
71
|
+
|
|
72
|
+
const isInside = (opts: { path: string; root: string }): boolean =>
|
|
73
|
+
opts.path === opts.root || opts.path.startsWith(`${opts.root}${sep}`);
|
|
74
|
+
|
|
75
|
+
/**
|
|
76
|
+
* A tool-owned pattern as an absolute glob: `~` and variables expanded, a bare directory taking everything
|
|
77
|
+
* below it. A pattern with an unset variable is dropped - it would otherwise match from the root.
|
|
78
|
+
*/
|
|
79
|
+
const allowPatterns = (opts: { patterns: string[]; home: string; env: TEnvironment }): string[] =>
|
|
80
|
+
opts.patterns.flatMap((pattern) => {
|
|
81
|
+
if ((pattern === '~' || pattern.startsWith('~/')) && opts.home === '') {
|
|
82
|
+
return [];
|
|
83
|
+
}
|
|
84
|
+
|
|
85
|
+
const withHome =
|
|
86
|
+
pattern === '~' || pattern.startsWith('~/') ? `${opts.home}${pattern.slice(1)}` : pattern;
|
|
87
|
+
let unresolved = false;
|
|
88
|
+
const concrete = withHome.replace(/\$\{?(\w+)\}?/g, (_, name: string) => {
|
|
89
|
+
const value = opts.env[name];
|
|
90
|
+
|
|
91
|
+
if (value === undefined || value === '') {
|
|
92
|
+
unresolved = true;
|
|
93
|
+
}
|
|
94
|
+
|
|
95
|
+
return value ?? '';
|
|
96
|
+
});
|
|
97
|
+
|
|
98
|
+
if (unresolved || !isAbsolute(concrete)) {
|
|
99
|
+
return [];
|
|
100
|
+
}
|
|
101
|
+
|
|
102
|
+
return [concrete, /[*?[]/.test(concrete) ? concrete : `${concrete}/**`];
|
|
103
|
+
});
|
|
104
|
+
|
|
105
|
+
const isAllowed = (opts: { path: string; patterns: string[] }): boolean =>
|
|
106
|
+
opts.patterns.some((pattern) => new Bun.Glob(pattern).match(opts.path));
|
|
107
|
+
|
|
108
|
+
interface ITarget {
|
|
109
|
+
word: string;
|
|
110
|
+
message: string;
|
|
111
|
+
/** The directory a relative `word` is taken from: the hook's, moved by `cd` and `git -C`. */
|
|
112
|
+
base: string;
|
|
113
|
+
}
|
|
114
|
+
|
|
115
|
+
const positional = (opts: { args: string[] }): string[] =>
|
|
116
|
+
opts.args.filter((word) => !word.startsWith('-'));
|
|
117
|
+
|
|
118
|
+
/** The value of a flag, long (`--output X`, `--output=X`) or short, alone or last in a cluster (`-sLo X`). */
|
|
119
|
+
const valueOf = (opts: {
|
|
120
|
+
args: string[];
|
|
121
|
+
long?: string[];
|
|
122
|
+
short?: string[];
|
|
123
|
+
}): string | undefined => {
|
|
124
|
+
for (const [index, word] of opts.args.entries()) {
|
|
125
|
+
for (const flag of opts.long ?? []) {
|
|
126
|
+
if (word === flag) {
|
|
127
|
+
return opts.args[index + 1];
|
|
128
|
+
}
|
|
129
|
+
|
|
130
|
+
if (word.startsWith(`${flag}=`)) {
|
|
131
|
+
return word.slice(flag.length + 1);
|
|
132
|
+
}
|
|
133
|
+
}
|
|
134
|
+
|
|
135
|
+
for (const letter of opts.short ?? []) {
|
|
136
|
+
if (new RegExp(`^-[a-zA-Z]*${letter}$`).test(word)) {
|
|
137
|
+
return opts.args[index + 1];
|
|
138
|
+
}
|
|
139
|
+
}
|
|
140
|
+
}
|
|
141
|
+
|
|
142
|
+
return undefined;
|
|
143
|
+
};
|
|
144
|
+
|
|
145
|
+
/** Options of `git clone` that take the next word, so it is not the URL or the directory. */
|
|
146
|
+
const CLONE_VALUE_FLAGS = new Set([
|
|
147
|
+
'-b',
|
|
148
|
+
'--branch',
|
|
149
|
+
'--depth',
|
|
150
|
+
'-c',
|
|
151
|
+
'--config',
|
|
152
|
+
'-o',
|
|
153
|
+
'--origin',
|
|
154
|
+
'--reference',
|
|
155
|
+
'--separate-git-dir',
|
|
156
|
+
'--template',
|
|
157
|
+
'-j',
|
|
158
|
+
'--jobs',
|
|
159
|
+
'-u',
|
|
160
|
+
'--upload-pack',
|
|
161
|
+
'--filter',
|
|
162
|
+
'--shallow-since',
|
|
163
|
+
'--shallow-exclude',
|
|
164
|
+
]);
|
|
165
|
+
|
|
166
|
+
/** Words after `git`'s global options (`-C dir`, `-c k=v`, `--no-pager`) and the directory `-C` names. */
|
|
167
|
+
const gitArguments = (opts: { args: string[] }): { rest: string[]; dir: string | undefined } => {
|
|
168
|
+
let dir: string | undefined;
|
|
169
|
+
let index = 0;
|
|
170
|
+
|
|
171
|
+
while (index < opts.args.length && opts.args[index].startsWith('-')) {
|
|
172
|
+
const word = opts.args[index];
|
|
173
|
+
|
|
174
|
+
if (word === '-C') {
|
|
175
|
+
dir = opts.args[index + 1];
|
|
176
|
+
index += 2;
|
|
177
|
+
} else if (['-c', '--git-dir', '--work-tree', '--namespace'].includes(word)) {
|
|
178
|
+
index += 2;
|
|
179
|
+
} else {
|
|
180
|
+
index += 1;
|
|
181
|
+
}
|
|
182
|
+
}
|
|
183
|
+
|
|
184
|
+
return { rest: opts.args.slice(index), dir };
|
|
185
|
+
};
|
|
186
|
+
|
|
187
|
+
const gitTargets = (opts: { rest: string[]; base: string; dir: string | undefined }): ITarget[] => {
|
|
188
|
+
const base = opts.dir === undefined ? opts.base : resolve(opts.base, opts.dir);
|
|
189
|
+
const [sub, second] = positional({ args: opts.rest });
|
|
190
|
+
|
|
191
|
+
if (sub === 'worktree' && second === 'add') {
|
|
192
|
+
const afterAdd = opts.rest.slice(opts.rest.indexOf('add') + 1);
|
|
193
|
+
const skipped = new Set<number>();
|
|
194
|
+
|
|
195
|
+
for (const [index, word] of afterAdd.entries()) {
|
|
196
|
+
if (word === '-b' || word === '-B') {
|
|
197
|
+
skipped.add(index);
|
|
198
|
+
skipped.add(index + 1);
|
|
199
|
+
}
|
|
200
|
+
}
|
|
201
|
+
|
|
202
|
+
const path = afterAdd.find((word, index) => !word.startsWith('-') && !skipped.has(index));
|
|
203
|
+
|
|
204
|
+
return path === undefined
|
|
205
|
+
? []
|
|
206
|
+
: [{ word: path, base, message: `git worktree add creates ${path}` }];
|
|
207
|
+
}
|
|
208
|
+
|
|
209
|
+
if (sub === 'clone') {
|
|
210
|
+
const afterClone = opts.rest.slice(opts.rest.indexOf('clone') + 1);
|
|
211
|
+
const words: string[] = [];
|
|
212
|
+
|
|
213
|
+
for (let index = 0; index < afterClone.length; index += 1) {
|
|
214
|
+
if (CLONE_VALUE_FLAGS.has(afterClone[index])) {
|
|
215
|
+
index += 1;
|
|
216
|
+
} else if (!afterClone[index].startsWith('-')) {
|
|
217
|
+
words.push(afterClone[index]);
|
|
218
|
+
}
|
|
219
|
+
}
|
|
220
|
+
|
|
221
|
+
const [, destination] = words;
|
|
222
|
+
|
|
223
|
+
return words.length === 0
|
|
224
|
+
? []
|
|
225
|
+
: [
|
|
226
|
+
{
|
|
227
|
+
word: destination ?? '.',
|
|
228
|
+
base,
|
|
229
|
+
message:
|
|
230
|
+
destination === undefined
|
|
231
|
+
? 'git clone writes into the current directory'
|
|
232
|
+
: `git clone writes ${destination}`,
|
|
233
|
+
},
|
|
234
|
+
];
|
|
235
|
+
}
|
|
236
|
+
|
|
237
|
+
return [];
|
|
238
|
+
};
|
|
239
|
+
|
|
240
|
+
/** The words a simple command would create or write, with a name for what writes them. */
|
|
241
|
+
const targetsOf = (opts: { command: ISimpleCommand; base: string }): ITarget[] => {
|
|
242
|
+
const program = programOf({ command: opts.command });
|
|
243
|
+
const name = basenameOf({ word: program.name });
|
|
244
|
+
const { args } = program;
|
|
245
|
+
const { base } = opts;
|
|
246
|
+
const targets: ITarget[] = opts.command.redirects.map((word) => ({
|
|
247
|
+
word,
|
|
248
|
+
base,
|
|
249
|
+
message: `a shell redirect writes ${word}`,
|
|
250
|
+
}));
|
|
251
|
+
const assignments = [
|
|
252
|
+
...opts.command.assignments,
|
|
253
|
+
...(['export', 'declare', 'typeset', 'local', 'readonly'].includes(name) ? args : []),
|
|
254
|
+
];
|
|
255
|
+
|
|
256
|
+
for (const assignment of assignments.filter((word) => word.startsWith('TMPDIR='))) {
|
|
257
|
+
targets.push({
|
|
258
|
+
word: assignment.slice('TMPDIR='.length),
|
|
259
|
+
base,
|
|
260
|
+
message: 'TMPDIR points outside the project',
|
|
261
|
+
});
|
|
262
|
+
}
|
|
263
|
+
|
|
264
|
+
const last = positional({ args }).at(-1);
|
|
265
|
+
const add = (word: string, message: string): number => targets.push({ word, base, message });
|
|
266
|
+
|
|
267
|
+
if (['mkdir', 'touch'].includes(name)) {
|
|
268
|
+
for (const word of positional({ args })) {
|
|
269
|
+
add(word, `${name} creates ${word}`);
|
|
270
|
+
}
|
|
271
|
+
} else if (['cp', 'mv', 'install', 'ln', 'rsync'].includes(name)) {
|
|
272
|
+
const directory = valueOf({ args, long: ['--target-directory'], short: ['t'] });
|
|
273
|
+
|
|
274
|
+
if (directory !== undefined) {
|
|
275
|
+
add(directory, `${name} writes into ${directory}`);
|
|
276
|
+
} else if (last !== undefined) {
|
|
277
|
+
add(last, `${name} writes ${last}`);
|
|
278
|
+
}
|
|
279
|
+
} else if (name === 'tee') {
|
|
280
|
+
for (const word of positional({ args })) {
|
|
281
|
+
add(word, `tee writes ${word}`);
|
|
282
|
+
}
|
|
283
|
+
} else if (name === 'dd') {
|
|
284
|
+
const output = args.find((word) => word.startsWith('of='));
|
|
285
|
+
|
|
286
|
+
if (output !== undefined) {
|
|
287
|
+
add(output.slice(3), `dd writes ${output.slice(3)}`);
|
|
288
|
+
}
|
|
289
|
+
} else if (name === 'curl') {
|
|
290
|
+
const output = valueOf({ args, long: ['--output'], short: ['o'] });
|
|
291
|
+
const directory = valueOf({ args, long: ['--output-dir'] });
|
|
292
|
+
|
|
293
|
+
if (output !== undefined) {
|
|
294
|
+
add(output, `curl saves to ${output}`);
|
|
295
|
+
}
|
|
296
|
+
|
|
297
|
+
if (directory !== undefined) {
|
|
298
|
+
add(directory, `curl saves into ${directory}`);
|
|
299
|
+
}
|
|
300
|
+
} else if (name === 'wget') {
|
|
301
|
+
const output = valueOf({ args, long: ['--output-document'], short: ['O'] });
|
|
302
|
+
const directory = valueOf({ args, long: ['--directory-prefix'], short: ['P'] });
|
|
303
|
+
|
|
304
|
+
for (const word of [output, directory]) {
|
|
305
|
+
if (word !== undefined) {
|
|
306
|
+
add(word, `wget saves to ${word}`);
|
|
307
|
+
}
|
|
308
|
+
}
|
|
309
|
+
} else if (name === 'mktemp') {
|
|
310
|
+
const directory = valueOf({ args, long: ['--tmpdir'], short: ['p'] });
|
|
311
|
+
const inTmpdir = args.some((word) => /^-[a-zA-Z]*t[a-zA-Z]*$/.test(word));
|
|
312
|
+
const [template] = positional({ args }).filter((word) => word !== directory);
|
|
313
|
+
|
|
314
|
+
if (directory !== undefined) {
|
|
315
|
+
add(directory, 'mktemp creates a temporary file');
|
|
316
|
+
} else if (inTmpdir || template === undefined) {
|
|
317
|
+
add(tmpdir(), 'mktemp creates a file in the system temp directory');
|
|
318
|
+
} else {
|
|
319
|
+
add(template, 'mktemp creates a temporary file');
|
|
320
|
+
}
|
|
321
|
+
} else if (name === 'git') {
|
|
322
|
+
const { rest, dir } = gitArguments({ args });
|
|
323
|
+
|
|
324
|
+
targets.push(...gitTargets({ rest, base, dir }));
|
|
325
|
+
}
|
|
326
|
+
|
|
327
|
+
return targets;
|
|
328
|
+
};
|
|
329
|
+
|
|
330
|
+
/**
|
|
331
|
+
* Every file, worktree, clone or temp file `input` would create outside the project. Files a package
|
|
332
|
+
* manager, a runtime or the agent tool manage themselves are allowed (`toolOwnedPaths`, extending the
|
|
333
|
+
* defaults); pointing `TMPDIR` or `mktemp` at the system temp directory is not. It reads common
|
|
334
|
+
* forms - redirects, mkdir, cp, mv, tee, curl -o, git clone, git worktree add, mktemp, `cd` - and is a
|
|
335
|
+
* guard against drift, not a sandbox: a path it cannot work out (an unset variable, a glob) passes.
|
|
336
|
+
*/
|
|
337
|
+
export const inspectOutside = (opts: {
|
|
338
|
+
input: IManifestHookInput;
|
|
339
|
+
projectRoot: string;
|
|
340
|
+
toolOwnedPaths?: string[];
|
|
341
|
+
home?: string;
|
|
342
|
+
env?: TEnvironment;
|
|
343
|
+
}): IOutsideHit[] => {
|
|
344
|
+
const env = opts.env ?? process.env;
|
|
345
|
+
const home = opts.home ?? env.HOME ?? '';
|
|
346
|
+
const root = realOf({ path: resolve(opts.projectRoot) });
|
|
347
|
+
const patterns = allowPatterns({
|
|
348
|
+
patterns: [...HookConstants.TOOL_OWNED_PATHS, ...(opts.toolOwnedPaths ?? [])],
|
|
349
|
+
home,
|
|
350
|
+
env,
|
|
351
|
+
});
|
|
352
|
+
const start = opts.input.cwd ?? opts.projectRoot;
|
|
353
|
+
const targets: ITarget[] = [];
|
|
354
|
+
|
|
355
|
+
if (FILE_TOOLS.has(opts.input.tool) && opts.input.filePath !== undefined) {
|
|
356
|
+
targets.push({
|
|
357
|
+
word: opts.input.filePath,
|
|
358
|
+
base: start,
|
|
359
|
+
message: `${opts.input.tool} writes ${opts.input.filePath}`,
|
|
360
|
+
});
|
|
361
|
+
}
|
|
362
|
+
|
|
363
|
+
if (opts.input.command !== undefined) {
|
|
364
|
+
let base = start;
|
|
365
|
+
|
|
366
|
+
for (const command of parseShell({ command: opts.input.command })) {
|
|
367
|
+
const program = programOf({ command });
|
|
368
|
+
|
|
369
|
+
if (basenameOf({ word: program.name }) === 'cd') {
|
|
370
|
+
const [destination] = positional({ args: program.args });
|
|
371
|
+
const expanded =
|
|
372
|
+
destination === undefined ? undefined : expandPath({ word: destination, home, env });
|
|
373
|
+
|
|
374
|
+
base = expanded === undefined ? base : resolve(base, expanded);
|
|
375
|
+
continue;
|
|
376
|
+
}
|
|
377
|
+
|
|
378
|
+
targets.push(...targetsOf({ command, base }));
|
|
379
|
+
}
|
|
380
|
+
}
|
|
381
|
+
|
|
382
|
+
const hits: IOutsideHit[] = [];
|
|
383
|
+
|
|
384
|
+
for (const target of targets) {
|
|
385
|
+
const expanded = expandPath({ word: target.word, home, env });
|
|
386
|
+
|
|
387
|
+
if (expanded === undefined) {
|
|
388
|
+
continue;
|
|
389
|
+
}
|
|
390
|
+
|
|
391
|
+
const path = realOf({ path: resolve(target.base, expanded) });
|
|
392
|
+
|
|
393
|
+
if (!isInside({ path, root }) && !isAllowed({ path, patterns })) {
|
|
394
|
+
hits.push({ path, message: target.message });
|
|
395
|
+
}
|
|
396
|
+
}
|
|
397
|
+
|
|
398
|
+
return hits;
|
|
399
|
+
};
|
|
@@ -172,11 +172,11 @@ export const inspectPackageWrite = (opts: {
|
|
|
172
172
|
const what = isLib ? 'lib' : `${key} app`;
|
|
173
173
|
const commands = isLib
|
|
174
174
|
? project.presets
|
|
175
|
-
.filter(preset => preset.lib)
|
|
176
|
-
.map(preset => `${ADD} --preset ${preset.spec} --lib ${created.name}`)
|
|
175
|
+
.filter((preset) => preset.lib)
|
|
176
|
+
.map((preset) => `${ADD} --preset ${preset.spec} --lib ${created.name}`)
|
|
177
177
|
: project.presets
|
|
178
|
-
.filter(preset => preset.side === key)
|
|
179
|
-
.map(preset => appCommand({ preset, name: created.name }));
|
|
178
|
+
.filter((preset) => preset.side === key)
|
|
179
|
+
.map((preset) => appCommand({ preset, name: created.name }));
|
|
180
180
|
|
|
181
181
|
if (commands.length > 0) {
|
|
182
182
|
return {
|
|
@@ -65,7 +65,7 @@ const SCRIPT_CHECKS: IWriteCheck[] = [
|
|
|
65
65
|
];
|
|
66
66
|
|
|
67
67
|
const extractScriptTargets = (opts: { command: string }): string[] =>
|
|
68
|
-
[...opts.command.matchAll(/\bbun\s+(?:run\s+)?(\S+\.ts)\b/g)].map(match => match[1]);
|
|
68
|
+
[...opts.command.matchAll(/\bbun\s+(?:run\s+)?(\S+\.ts)\b/g)].map((match) => match[1]);
|
|
69
69
|
|
|
70
70
|
const readScript = (opts: { target: string; cwd: string }): string | undefined => {
|
|
71
71
|
const path = isAbsolute(opts.target) ? opts.target : join(opts.cwd, opts.target);
|
|
@@ -75,8 +75,8 @@ const readScript = (opts: { target: string; cwd: string }): string | undefined =
|
|
|
75
75
|
|
|
76
76
|
/** Every reason `command` looks like a write the base rules reserve for a human (V-W-02, V-W-03). */
|
|
77
77
|
export const inspectCommand = (opts: { command: string; cwd: string }): IWriteHit[] => {
|
|
78
|
-
const hits: IWriteHit[] = COMMAND_CHECKS.filter(check => check.pattern.test(opts.command)).map(
|
|
79
|
-
check => ({
|
|
78
|
+
const hits: IWriteHit[] = COMMAND_CHECKS.filter((check) => check.pattern.test(opts.command)).map(
|
|
79
|
+
(check) => ({
|
|
80
80
|
rule: check.rule,
|
|
81
81
|
message: check.what,
|
|
82
82
|
}),
|
|
@@ -89,7 +89,7 @@ export const inspectCommand = (opts: { command: string; cwd: string }): IWriteHi
|
|
|
89
89
|
continue;
|
|
90
90
|
}
|
|
91
91
|
|
|
92
|
-
for (const check of SCRIPT_CHECKS.filter(scriptCheck => scriptCheck.pattern.test(source))) {
|
|
92
|
+
for (const check of SCRIPT_CHECKS.filter((scriptCheck) => scriptCheck.pattern.test(source))) {
|
|
93
93
|
hits.push({ rule: check.rule, message: `${target} looks like it is ${check.what}` });
|
|
94
94
|
}
|
|
95
95
|
}
|
|
@@ -136,7 +136,7 @@ export const buildGuardDecision = (opts: { hits: IWriteHit[] }) => ({
|
|
|
136
136
|
permissionDecision: HookConstants.GUARD_DECISION,
|
|
137
137
|
permissionDecisionReason: [
|
|
138
138
|
`This command looks like ${opts.hits.length === 1 ? 'an action' : 'actions'} the base rules reserve for a human:`,
|
|
139
|
-
...opts.hits.map(hit => ` - ${hit.rule}: ${hit.message}`),
|
|
139
|
+
...opts.hits.map((hit) => ` - ${hit.rule}: ${hit.message}`),
|
|
140
140
|
'',
|
|
141
141
|
'The rules reject "it is only dev", "it is only a test row" and "it is reversible".',
|
|
142
142
|
'Produce the script, the SQL or the manifest and hand it over instead of running it.',
|
package/src/hooks/index.ts
CHANGED
|
@@ -1,6 +1,12 @@
|
|
|
1
1
|
export * from './common/index.ts';
|
|
2
|
+
export * from './decision.ts';
|
|
3
|
+
export * from './guard-git.ts';
|
|
2
4
|
export * from './guard-manifest.ts';
|
|
5
|
+
export * from './guard-outside.ts';
|
|
3
6
|
export * from './guard-package.ts';
|
|
4
7
|
export * from './guard-writes.ts';
|
|
5
8
|
export * from './session-digest.ts';
|
|
9
|
+
export * from './subagent-card.ts';
|
|
10
|
+
export * from './owner-reply.ts';
|
|
6
11
|
export * from './remote-hosts.ts';
|
|
12
|
+
export * from './shell.ts';
|
|
@@ -0,0 +1,148 @@
|
|
|
1
|
+
import { loadConfig } from '../config/index.ts';
|
|
2
|
+
import {
|
|
3
|
+
checkOwnerShape,
|
|
4
|
+
OWNER_SCHEMA,
|
|
5
|
+
OwnerEnforcement,
|
|
6
|
+
readLastClasses,
|
|
7
|
+
readProfile,
|
|
8
|
+
markBlocked,
|
|
9
|
+
recordReply,
|
|
10
|
+
wasBlockedRecently,
|
|
11
|
+
resolveOwnerView,
|
|
12
|
+
ShapeClasses,
|
|
13
|
+
} from '../owner/index.ts';
|
|
14
|
+
import type { IOwnerView } from '../owner/index.ts';
|
|
15
|
+
import { checkClaims, readSignals, startTurn } from '../signals/index.ts';
|
|
16
|
+
|
|
17
|
+
/** What to do about each class, in the words of the block message. */
|
|
18
|
+
const FIXES: Record<string, string> = {
|
|
19
|
+
[ShapeClasses.HEADLINE_NOT_SENTENCE]:
|
|
20
|
+
'open with a plain sentence that answers first, not a heading, list or table',
|
|
21
|
+
[ShapeClasses.HEADLINE_TOO_LONG]: 'cut the headline to the sentence cap',
|
|
22
|
+
[ShapeClasses.MINIMAP_MISSING]: 'add the minimap table with the configured header',
|
|
23
|
+
[ShapeClasses.MINIMAP_STATUS]: 'use only the configured status words in the minimap',
|
|
24
|
+
[ShapeClasses.BLOCKED_MISSING]: 'add the Blocked line, or its none string',
|
|
25
|
+
[ShapeClasses.DECISIONS_MISSING]: 'add the decisions section, or exactly its none string',
|
|
26
|
+
[ShapeClasses.DECISIONS_MALFORMED]: 'number the decisions',
|
|
27
|
+
[ShapeClasses.DECISIONS_NO_RECOMMENDATION]: 'end each decision with the recommendation pattern',
|
|
28
|
+
[ShapeClasses.SECTION_ORDER]: 'put the sections in the configured order',
|
|
29
|
+
[ShapeClasses.EMOJI]: 'remove emoji',
|
|
30
|
+
[ShapeClasses.SHOUTING]: 'no all-caps phrases',
|
|
31
|
+
[ShapeClasses.RELATIVE_PATH]: 'name a file by its full absolute path',
|
|
32
|
+
};
|
|
33
|
+
|
|
34
|
+
/** The owner's view of the project in `projectRoot`; a project whose config cannot load gets the defaults. */
|
|
35
|
+
export const loadOwnerView = async (opts: { projectRoot: string }): Promise<IOwnerView> => {
|
|
36
|
+
const loaded = await loadConfig({ root: opts.projectRoot });
|
|
37
|
+
const profile = readProfile({ root: opts.projectRoot });
|
|
38
|
+
|
|
39
|
+
return resolveOwnerView({
|
|
40
|
+
owner: loaded.ok ? loaded.config.owner : OWNER_SCHEMA.parse(undefined),
|
|
41
|
+
profile: profile.ok ? profile.profile : undefined,
|
|
42
|
+
});
|
|
43
|
+
};
|
|
44
|
+
|
|
45
|
+
/**
|
|
46
|
+
* The Stop hook's answer for the last reply: it is checked and recorded (classes and date only), and
|
|
47
|
+
* only under `block`, once per message and only for a structural fault, is it sent back to be restated.
|
|
48
|
+
* Anything it cannot read passes: it never stops a session by failing.
|
|
49
|
+
*/
|
|
50
|
+
export const buildStopOutput = async (opts: {
|
|
51
|
+
projectRoot: string;
|
|
52
|
+
input: unknown;
|
|
53
|
+
}): Promise<Record<string, unknown> | undefined> => {
|
|
54
|
+
const { input } = opts;
|
|
55
|
+
|
|
56
|
+
if (typeof input !== 'object' || input === null || !('last_assistant_message' in input)) {
|
|
57
|
+
return undefined;
|
|
58
|
+
}
|
|
59
|
+
|
|
60
|
+
const { last_assistant_message: text } = input;
|
|
61
|
+
const isRetry = 'stop_hook_active' in input && input.stop_hook_active === true;
|
|
62
|
+
const session =
|
|
63
|
+
'session_id' in input && typeof input.session_id === 'string' ? input.session_id : '';
|
|
64
|
+
const view = await loadOwnerView({ projectRoot: opts.projectRoot });
|
|
65
|
+
|
|
66
|
+
if (typeof text !== 'string' || view.enforce === OwnerEnforcement.OFF) {
|
|
67
|
+
return undefined;
|
|
68
|
+
}
|
|
69
|
+
|
|
70
|
+
const result = checkOwnerShape({ text, view });
|
|
71
|
+
// A claim is checked on every reply, a short one too: "Published." is the shortest wrong one.
|
|
72
|
+
const claims = checkClaims({
|
|
73
|
+
text,
|
|
74
|
+
state: readSignals({ root: opts.projectRoot, session }),
|
|
75
|
+
publicWords: view.claimWords.public,
|
|
76
|
+
doneWords: view.claimWords.done,
|
|
77
|
+
});
|
|
78
|
+
const classes = [...result.classes, ...claims];
|
|
79
|
+
|
|
80
|
+
// Every reply updates the note the next prompt reads, an exempt short one too; a write that fails never
|
|
81
|
+
// costs the decision below.
|
|
82
|
+
try {
|
|
83
|
+
recordReply({ root: opts.projectRoot, classes });
|
|
84
|
+
} catch {
|
|
85
|
+
// The log is advisory.
|
|
86
|
+
}
|
|
87
|
+
|
|
88
|
+
if (!result.isChecked) {
|
|
89
|
+
return undefined;
|
|
90
|
+
}
|
|
91
|
+
|
|
92
|
+
const structural = result.classes.filter((name) => ShapeClasses.isStructural(name));
|
|
93
|
+
|
|
94
|
+
if (
|
|
95
|
+
view.enforce !== OwnerEnforcement.BLOCK ||
|
|
96
|
+
isRetry ||
|
|
97
|
+
structural.length === 0 ||
|
|
98
|
+
wasBlockedRecently({ root: opts.projectRoot })
|
|
99
|
+
) {
|
|
100
|
+
return undefined;
|
|
101
|
+
}
|
|
102
|
+
|
|
103
|
+
try {
|
|
104
|
+
markBlocked({ root: opts.projectRoot });
|
|
105
|
+
} catch {
|
|
106
|
+
// Without the note the stop_hook_active flag is the only guard against a second block.
|
|
107
|
+
}
|
|
108
|
+
|
|
109
|
+
return {
|
|
110
|
+
decision: 'block',
|
|
111
|
+
reason: [
|
|
112
|
+
'The reply to the owner breaks the report shape (see "How to answer the owner" in the session rules). Restate it once, in full:',
|
|
113
|
+
...structural.map((name) => ` - ${name}: ${FIXES[name] ?? name}`),
|
|
114
|
+
].join('\n'),
|
|
115
|
+
};
|
|
116
|
+
};
|
|
117
|
+
|
|
118
|
+
/**
|
|
119
|
+
* One line added to every prompt while the shape is held: the contract, and the faults of the reply
|
|
120
|
+
* before when it had any. Empty when the shape is not enforced.
|
|
121
|
+
*/
|
|
122
|
+
export const buildPromptContract = async (opts: {
|
|
123
|
+
projectRoot: string;
|
|
124
|
+
/** The session the prompt belongs to; its turn begins here. */
|
|
125
|
+
session?: string;
|
|
126
|
+
}): Promise<string> => {
|
|
127
|
+
// A new turn begins with this prompt: the notes of the last one are forgotten, whatever is enforced.
|
|
128
|
+
try {
|
|
129
|
+
startTurn({ root: opts.projectRoot, session: opts.session ?? '' });
|
|
130
|
+
} catch {
|
|
131
|
+
// The notes are advisory.
|
|
132
|
+
}
|
|
133
|
+
|
|
134
|
+
const view = await loadOwnerView({ projectRoot: opts.projectRoot });
|
|
135
|
+
|
|
136
|
+
if (view.enforce === OwnerEnforcement.OFF) {
|
|
137
|
+
return '';
|
|
138
|
+
}
|
|
139
|
+
|
|
140
|
+
// A claim match is a hint, so it is logged but never said back to the model as a fault of the last reply.
|
|
141
|
+
const last = readLastClasses({ root: opts.projectRoot }).filter((name) =>
|
|
142
|
+
ShapeClasses.isValid(name),
|
|
143
|
+
);
|
|
144
|
+
|
|
145
|
+
return last.length === 0
|
|
146
|
+
? 'Owner reply contract: follow the report shape from the session rules.'
|
|
147
|
+
: `Owner reply contract: follow the report shape from the session rules. Your last reply broke it - ${last.map((name) => FIXES[name] ?? name).join('; ')}.`;
|
|
148
|
+
};
|