pi-odd-worktree 3.1.1 → 3.3.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/README.md +2 -1
- package/extensions/odd-worktree-tools.ts +83 -3
- package/lib/bash-activity.js +405 -32
- package/lib/bash-inline-script.js +314 -0
- package/lib/codegraph-guard.js +36 -19
- package/lib/codegraph-prompt/generic.md +1 -1
- package/lib/codegraph-prompt/orchestrator.md +7 -7
- package/lib/codegraph-session.js +10 -2
- package/lib/odd-prompt/odd-worktrees-child.md +3 -0
- package/lib/odd-prompt/odd-worktrees.md +11 -7
- package/lib/odd-worktrees-prompt.js +68 -14
- package/lib/skill-read-guard.js +397 -0
- package/lib/worktree-guard.js +205 -5
- package/package.json +1 -1
- package/tools/bash-activity.test.js +400 -35
- package/tools/codegraph-guard.test.js +109 -11
- package/tools/codegraph-rules.test.js +8 -8
- package/tools/codegraph-session.test.js +45 -3
- package/tools/odd-worktrees-prompt.test.js +98 -11
- package/tools/package-surface.test.js +15 -1
- package/tools/skill-read-guard.test.js +316 -0
- package/tools/worktree-write-guard.test.js +516 -0
|
@@ -0,0 +1,316 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
// invariant 22 (T1, T2): the skill-read guard — the reader a `bash` call must not use
|
|
3
|
+
// on a skill file, and the sentence that says so.
|
|
4
|
+
//
|
|
5
|
+
// The guarded forms are one table: every refused and every allowed spelling, through
|
|
6
|
+
// the module's one entry point and, for the extension half, through the `tool_call`
|
|
7
|
+
// handler the host actually receives. The commands are INPUT TEXT only — the guard
|
|
8
|
+
// reads the string and never runs it — so no row below spawns anything, and the only
|
|
9
|
+
// scratch this file creates is the throwaway cwd the handler is called with, removed
|
|
10
|
+
// in teardown.
|
|
11
|
+
//
|
|
12
|
+
// Public behavior only: what `decideSkillReadGuard(command)` returns and what the
|
|
13
|
+
// handler the extension registered returns.
|
|
14
|
+
|
|
15
|
+
const { test, describe, before, after } = require('node:test');
|
|
16
|
+
const assert = require('node:assert/strict');
|
|
17
|
+
const fs = require('node:fs');
|
|
18
|
+
const os = require('node:os');
|
|
19
|
+
const path = require('node:path');
|
|
20
|
+
const { decideSkillReadGuard } = require('../lib/skill-read-guard.js');
|
|
21
|
+
const { loadExtension } = require('./fixtures/load-extension.js');
|
|
22
|
+
|
|
23
|
+
// The refusal, written out: a reworded sentence fails here. It says the call did not
|
|
24
|
+
// run, names the path it read and the ONE tool that replaces the command.
|
|
25
|
+
const refusalFor = (skillPath) => `This bash call was not run: it reads the skill file ${skillPath} — read skill files with the \`read\` tool instead.`;
|
|
26
|
+
|
|
27
|
+
const SKILL_PATH = '/home/dev/.agents/skills/release/SKILL.md';
|
|
28
|
+
const REFERENCE_PATH = '/home/dev/.agents/skills/release/references/commands.md';
|
|
29
|
+
|
|
30
|
+
// The offending call as it was issued, abridged at the end: one skill read mid-chain,
|
|
31
|
+
// among ordinary reading that must keep working.
|
|
32
|
+
const REAL_COMMAND = `cd /home/jona/dev/projects/odd-worktree && sed -n '1,80p' /home/jona/.agents/skills/release/SKILL.md; echo '=== reference: commits/PRs + cut a release ==='; sed -n '/,$p' docs/reference.md | head -60; echo '=== release.yml ==='; cat .github/workflows/release.yml | head -80`;
|
|
33
|
+
const REAL_SKILL_PATH = '/home/jona/.agents/skills/release/SKILL.md';
|
|
34
|
+
|
|
35
|
+
// Every refused spelling, with the path the reason must name.
|
|
36
|
+
const BLOCKED = [
|
|
37
|
+
[`sed -n '1,80p' ${SKILL_PATH}`, SKILL_PATH],
|
|
38
|
+
[`head -80 ${REFERENCE_PATH}`, REFERENCE_PATH],
|
|
39
|
+
['cat skills/x/SKILL.md', 'skills/x/SKILL.md'],
|
|
40
|
+
['cat ./skills/x/SKILL.md', './skills/x/SKILL.md'],
|
|
41
|
+
['cat "skills/x/SKILL.md"', 'skills/x/SKILL.md'],
|
|
42
|
+
["cat 'skills/my skill/SKILL.md'", 'skills/my skill/SKILL.md'],
|
|
43
|
+
['cat ../skills/x/references/y.md', '../skills/x/references/y.md'],
|
|
44
|
+
['cat ~/.agents/skills/gentle-ai/SKILL.md', '~/.agents/skills/gentle-ai/SKILL.md'],
|
|
45
|
+
['head -n 20 skills/x/SKILL.md', 'skills/x/SKILL.md'],
|
|
46
|
+
['tail -5 skills/x/SKILL.md', 'skills/x/SKILL.md'],
|
|
47
|
+
['less skills/x/SKILL.md', 'skills/x/SKILL.md'],
|
|
48
|
+
['more skills/x/SKILL.md', 'skills/x/SKILL.md'],
|
|
49
|
+
['bat skills/x/SKILL.md', 'skills/x/SKILL.md'],
|
|
50
|
+
['batcat skills/x/SKILL.md', 'skills/x/SKILL.md'],
|
|
51
|
+
['nl skills/x/SKILL.md', 'skills/x/SKILL.md'],
|
|
52
|
+
["awk '{print}' skills/x/SKILL.md", 'skills/x/SKILL.md'],
|
|
53
|
+
["sed -e 's/a/b/' skills/x/SKILL.md", 'skills/x/SKILL.md'],
|
|
54
|
+
["sed -n '/name/p' skills/x/references/commands.md", 'skills/x/references/commands.md'],
|
|
55
|
+
['cat skills/x/SKILL.md | head -5', 'skills/x/SKILL.md'],
|
|
56
|
+
['cat skills/x/SKILL.md 2>/dev/null', 'skills/x/SKILL.md'],
|
|
57
|
+
['cat 2>/dev/null skills/x/SKILL.md', 'skills/x/SKILL.md'],
|
|
58
|
+
// The command position, not the first word: assignments and wrappers come before it.
|
|
59
|
+
['SKILL_DIR=1 cat skills/x/SKILL.md', 'skills/x/SKILL.md'],
|
|
60
|
+
['env FOO=1 cat skills/x/SKILL.md', 'skills/x/SKILL.md'],
|
|
61
|
+
['time cat skills/x/SKILL.md', 'skills/x/SKILL.md'],
|
|
62
|
+
['nice -n 5 cat skills/x/SKILL.md', 'skills/x/SKILL.md'],
|
|
63
|
+
// A skill read beside ordinary work refuses the whole call.
|
|
64
|
+
['git status && cat skills/x/SKILL.md', 'skills/x/SKILL.md'],
|
|
65
|
+
['echo start; sed -n 1p skills/x/SKILL.md', 'skills/x/SKILL.md'],
|
|
66
|
+
[REAL_COMMAND, REAL_SKILL_PATH],
|
|
67
|
+
];
|
|
68
|
+
|
|
69
|
+
// Every allowed spelling: general Markdown, ODD artifacts, a skill path in argument
|
|
70
|
+
// text, a heredoc body, search, writes and listings.
|
|
71
|
+
const ALLOWED = [
|
|
72
|
+
'sed -n 1p docs/reference.md',
|
|
73
|
+
'sed -n 1,80p docs/reference.md',
|
|
74
|
+
"sed -n '/,$p' docs/reference.md | head -60",
|
|
75
|
+
'cat README.md',
|
|
76
|
+
'head -60 README.md',
|
|
77
|
+
'cat .github/workflows/release.yml | head -80',
|
|
78
|
+
'cat odd/tasks/feature.md',
|
|
79
|
+
'cat odd/tasks/feature.md | head -20',
|
|
80
|
+
'git commit -m "touched skills/x/SKILL.md"',
|
|
81
|
+
"echo 'see skills/x/SKILL.md'",
|
|
82
|
+
"echo '=== skills/x/SKILL.md ==='",
|
|
83
|
+
'grep -n SKILL skills/x/SKILL.md',
|
|
84
|
+
"rg --files-with-matches 'SKILL' skills/",
|
|
85
|
+
'grep -rn skills .github',
|
|
86
|
+
"sed -i 's/a/b/' skills/x/SKILL.md",
|
|
87
|
+
"sed -i.bak 's/a/b/' skills/x/SKILL.md",
|
|
88
|
+
"sed --in-place 's/a/b/' skills/x/SKILL.md",
|
|
89
|
+
'cat > skills/x/SKILL.md',
|
|
90
|
+
'cat >> skills/x/SKILL.md',
|
|
91
|
+
'cat notes/a.md > skills/x/SKILL.md',
|
|
92
|
+
'tee skills/x/SKILL.md',
|
|
93
|
+
'ls skills/',
|
|
94
|
+
'ls -la skills/x',
|
|
95
|
+
'cat $HOME/.agents/skills/x/SKILL.md',
|
|
96
|
+
'cat skills/*/SKILL.md',
|
|
97
|
+
'cat -',
|
|
98
|
+
'cat',
|
|
99
|
+
'cat docs/SKILL.md',
|
|
100
|
+
'cat notes/SKILL.md',
|
|
101
|
+
"sed -e '/skills/p' README.md",
|
|
102
|
+
"awk '/skills/p' README.md",
|
|
103
|
+
'git diff docs/reference.md',
|
|
104
|
+
'',
|
|
105
|
+
];
|
|
106
|
+
|
|
107
|
+
// A heredoc hands its body to the command as data: the skill path in it is never an
|
|
108
|
+
// argument, and the same is true of a body a reader cannot delimit.
|
|
109
|
+
const HEREDOCS = [
|
|
110
|
+
"cat <<'EOF' > notes.md\nsee skills/x/SKILL.md\nEOF",
|
|
111
|
+
'cat <<EOF\ncat skills/x/SKILL.md\nEOF',
|
|
112
|
+
"git commit -F - <<'MSG'\nmoved skills/x/SKILL.md\nMSG",
|
|
113
|
+
];
|
|
114
|
+
|
|
115
|
+
const bash = (command) => ({ toolName: 'bash', input: { command } });
|
|
116
|
+
|
|
117
|
+
// ---- the decision itself ----
|
|
118
|
+
|
|
119
|
+
describe('the decision (invariant 22)', () => {
|
|
120
|
+
test('refuses a reader pointed at a skill file, and names that path', () => {
|
|
121
|
+
for (const [command, skillPath] of BLOCKED) {
|
|
122
|
+
assert.deepEqual(decideSkillReadGuard(command), { block: true, reason: refusalFor(skillPath) }, command);
|
|
123
|
+
}
|
|
124
|
+
});
|
|
125
|
+
|
|
126
|
+
test('one call that reads two skills names the first one', () => {
|
|
127
|
+
assert.deepEqual(
|
|
128
|
+
decideSkillReadGuard('cat skills/a/SKILL.md; cat skills/b/SKILL.md'),
|
|
129
|
+
{ block: true, reason: refusalFor('skills/a/SKILL.md') },
|
|
130
|
+
);
|
|
131
|
+
});
|
|
132
|
+
|
|
133
|
+
test('the refusal says the call did not run and names one tool', () => {
|
|
134
|
+
const { reason } = decideSkillReadGuard('cat skills/x/SKILL.md');
|
|
135
|
+
assert.equal(reason.includes('!'), false, `no exclamation: ${reason}`);
|
|
136
|
+
assert.equal(/[\u{1F300}-\u{1FAFF}\u{2600}-\u{27BF}]/u.test(reason), false, `no emoji: ${reason}`);
|
|
137
|
+
assert.match(reason, /was not run:/, `says the command did not run: ${reason}`);
|
|
138
|
+
assert.ok(reason.endsWith('.'), `ends with a period: ${reason}`);
|
|
139
|
+
assert.equal((reason.match(/`[a-z_]+`/g) || []).length, 1, `exactly one tool is named: ${reason}`);
|
|
140
|
+
assert.match(reason, /`read`/, `names read: ${reason}`);
|
|
141
|
+
});
|
|
142
|
+
|
|
143
|
+
test('allows everything that is not a read of a skill file', () => {
|
|
144
|
+
for (const command of [...ALLOWED, ...HEREDOCS]) {
|
|
145
|
+
assert.equal(decideSkillReadGuard(command), undefined, command);
|
|
146
|
+
}
|
|
147
|
+
});
|
|
148
|
+
|
|
149
|
+
test('garbage never throws and always allows', () => {
|
|
150
|
+
const values = [
|
|
151
|
+
undefined,
|
|
152
|
+
null,
|
|
153
|
+
42,
|
|
154
|
+
{},
|
|
155
|
+
[],
|
|
156
|
+
['cat'],
|
|
157
|
+
() => 'cat skills/x/SKILL.md',
|
|
158
|
+
"cat 'unterminated",
|
|
159
|
+
"sed -n '1,80p skills/x/SKILL.md",
|
|
160
|
+
'cat <<EOF\nskills/x/SKILL.md',
|
|
161
|
+
'${cat} skills/x/SKILL.md',
|
|
162
|
+
'\u0000',
|
|
163
|
+
];
|
|
164
|
+
for (const value of values) {
|
|
165
|
+
assert.equal(decideSkillReadGuard(value), undefined, String(value));
|
|
166
|
+
}
|
|
167
|
+
});
|
|
168
|
+
});
|
|
169
|
+
|
|
170
|
+
// ---- the handler the extension registers ----
|
|
171
|
+
|
|
172
|
+
const roots = [];
|
|
173
|
+
|
|
174
|
+
function track(dir) {
|
|
175
|
+
roots.push(dir);
|
|
176
|
+
return dir;
|
|
177
|
+
}
|
|
178
|
+
|
|
179
|
+
// The host's `tool_call` round trip: every registered handler in turn, and the first
|
|
180
|
+
// block answer is the block the host sees.
|
|
181
|
+
async function callTool(hooks, event, cwd) {
|
|
182
|
+
const handlers = hooks.filter(([name]) => name === 'tool_call').map(([, handler]) => handler);
|
|
183
|
+
for (const handler of handlers) {
|
|
184
|
+
const answer = await handler(event, { cwd });
|
|
185
|
+
if (answer !== undefined) return answer;
|
|
186
|
+
}
|
|
187
|
+
return undefined;
|
|
188
|
+
}
|
|
189
|
+
|
|
190
|
+
describe('the tool_call handler the extension registers', () => {
|
|
191
|
+
let loaded;
|
|
192
|
+
let cwd;
|
|
193
|
+
|
|
194
|
+
before(async () => {
|
|
195
|
+
loaded = await loadExtension({});
|
|
196
|
+
cwd = track(fs.mkdtempSync(path.join(os.tmpdir(), 'odd-skill-read-guard-')));
|
|
197
|
+
});
|
|
198
|
+
|
|
199
|
+
after(() => {
|
|
200
|
+
for (const dir of roots) {
|
|
201
|
+
try {
|
|
202
|
+
fs.rmSync(dir, { recursive: true, force: true });
|
|
203
|
+
} catch {
|
|
204
|
+
// Best effort: a temp directory that is already gone is fine.
|
|
205
|
+
}
|
|
206
|
+
}
|
|
207
|
+
});
|
|
208
|
+
|
|
209
|
+
test('refuses a bash call that reads a skill file', async () => {
|
|
210
|
+
for (const [command, skillPath] of BLOCKED) {
|
|
211
|
+
assert.deepEqual(
|
|
212
|
+
await callTool(loaded.hooks, bash(command), cwd),
|
|
213
|
+
{ block: true, reason: refusalFor(skillPath) },
|
|
214
|
+
command,
|
|
215
|
+
);
|
|
216
|
+
}
|
|
217
|
+
});
|
|
218
|
+
|
|
219
|
+
test('lets every other call through', async () => {
|
|
220
|
+
for (const command of [...ALLOWED, ...HEREDOCS]) {
|
|
221
|
+
assert.equal(await callTool(loaded.hooks, bash(command), cwd), undefined, command);
|
|
222
|
+
}
|
|
223
|
+
});
|
|
224
|
+
|
|
225
|
+
test('leaves every non-bash tool call exactly as the host proposed it', async () => {
|
|
226
|
+
const calls = [
|
|
227
|
+
{ toolName: 'read', input: { path: 'skills/x/SKILL.md' } },
|
|
228
|
+
{ toolName: 'grep', input: { pattern: 'SKILL', path: 'skills/x' } },
|
|
229
|
+
{ toolName: 'edit', input: { path: 'skills/x/SKILL.md' } },
|
|
230
|
+
{ toolName: 'worktree_list', input: {} },
|
|
231
|
+
];
|
|
232
|
+
for (const event of calls) {
|
|
233
|
+
assert.equal(await callTool(loaded.hooks, event, cwd), undefined, JSON.stringify(event));
|
|
234
|
+
}
|
|
235
|
+
});
|
|
236
|
+
|
|
237
|
+
test('ODD_WORKTREE_GUARD=0 turns it off, and only that value', async () => {
|
|
238
|
+
const previous = process.env.ODD_WORKTREE_GUARD;
|
|
239
|
+
const command = 'cat skills/x/SKILL.md';
|
|
240
|
+
const expected = { block: true, reason: refusalFor('skills/x/SKILL.md') };
|
|
241
|
+
try {
|
|
242
|
+
for (const value of ['0', 'false', '', '1']) {
|
|
243
|
+
process.env.ODD_WORKTREE_GUARD = value;
|
|
244
|
+
const answer = await callTool(loaded.hooks, bash(command), cwd);
|
|
245
|
+
if (value === '0') assert.equal(answer, undefined, 'value "0" turns it off');
|
|
246
|
+
else assert.deepEqual(answer, expected, `value ${JSON.stringify(value)}`);
|
|
247
|
+
}
|
|
248
|
+
delete process.env.ODD_WORKTREE_GUARD;
|
|
249
|
+
assert.deepEqual(await callTool(loaded.hooks, bash(command), cwd), expected, 'unset keeps it on');
|
|
250
|
+
} finally {
|
|
251
|
+
if (previous === undefined) delete process.env.ODD_WORKTREE_GUARD;
|
|
252
|
+
else process.env.ODD_WORKTREE_GUARD = previous;
|
|
253
|
+
}
|
|
254
|
+
});
|
|
255
|
+
|
|
256
|
+
test('the worktree guard keeps precedence when one call does both', async () => {
|
|
257
|
+
const answer = await callTool(loaded.hooks, bash('git worktree add ../odd-b && cat skills/x/SKILL.md'), cwd);
|
|
258
|
+
assert.equal(answer.block, true, 'the call is refused');
|
|
259
|
+
assert.match(answer.reason, /`worktree_create`/, `the worktree refusal is the one shown: ${answer.reason}`);
|
|
260
|
+
});
|
|
261
|
+
|
|
262
|
+
test('garbage events never throw and always allow', async () => {
|
|
263
|
+
const events = [
|
|
264
|
+
{ toolName: 'bash' },
|
|
265
|
+
{ toolName: 'bash', input: {} },
|
|
266
|
+
{ toolName: 'bash', input: { command: 42 } },
|
|
267
|
+
{ toolName: 'bash', input: { command: {} } },
|
|
268
|
+
{ toolName: 'bash', input: { command: "cat 'unterminated" } },
|
|
269
|
+
{},
|
|
270
|
+
null,
|
|
271
|
+
];
|
|
272
|
+
for (const event of events) {
|
|
273
|
+
assert.equal(await callTool(loaded.hooks, event, cwd), undefined, JSON.stringify(event));
|
|
274
|
+
}
|
|
275
|
+
});
|
|
276
|
+
});
|
|
277
|
+
|
|
278
|
+
// ---- regressions an independent verification found ----
|
|
279
|
+
test('a skill is read through a real argument, not a search pattern or a normalised-away folder', () => {
|
|
280
|
+
const skill = 'skills/x/SKILL.md';
|
|
281
|
+
for (const command of [
|
|
282
|
+
`sed -ne'1p' ${skill}`,
|
|
283
|
+
`cat < ${skill}`,
|
|
284
|
+
`timeout 5s head ${skill}`,
|
|
285
|
+
`sudo cat ${skill}`,
|
|
286
|
+
`cat <<'EOF' | head ${skill}\ntext\nEOF`,
|
|
287
|
+
`cat "skills/x/[draft].md"`,
|
|
288
|
+
]) {
|
|
289
|
+
const decision = decideSkillReadGuard(command);
|
|
290
|
+
assert.equal(decision && decision.block, true, command);
|
|
291
|
+
}
|
|
292
|
+
for (const command of ['cat skills/../README.md', `less +/${skill} docs/reference.md`, 'cat skills/x/[draft].md']) {
|
|
293
|
+
assert.equal(decideSkillReadGuard(command), undefined, command);
|
|
294
|
+
}
|
|
295
|
+
});
|
|
296
|
+
|
|
297
|
+
test('a skill read in shell syntax is refused, and a mention or a usage request is not', () => {
|
|
298
|
+
const skill = 'skills/x/SKILL.md';
|
|
299
|
+
for (const command of [
|
|
300
|
+
`cat <<'EOF' ${skill}\nhello\nEOF`,
|
|
301
|
+
`command cat ${skill}`,
|
|
302
|
+
`bash -c 'cat ${skill}'`,
|
|
303
|
+
`if true; then cat ${skill}; fi`,
|
|
304
|
+
]) {
|
|
305
|
+
const decision = decideSkillReadGuard(command);
|
|
306
|
+
assert.equal(decision && decision.block, true, command);
|
|
307
|
+
assert.ok(decision.reason.includes(skill), command);
|
|
308
|
+
}
|
|
309
|
+
for (const command of [
|
|
310
|
+
`cat <<'ONE' <<'TWO'\nfirst\nONE\ncat ${skill}\nTWO`,
|
|
311
|
+
`echo hello # && cat ${skill}`,
|
|
312
|
+
`cat --help ${skill}`,
|
|
313
|
+
]) {
|
|
314
|
+
assert.equal(decideSkillReadGuard(command), undefined, command);
|
|
315
|
+
}
|
|
316
|
+
});
|