@usesketchbook/opencode-plugin 0.0.0-stage → 0.1.0-rc.1
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/LICENSE +21 -0
- package/README.md +123 -2
- package/dist/generated/baseline-manifest.d.ts +2 -0
- package/dist/generated/baseline-manifest.js +1590 -0
- package/dist/generated/catalog-baseline.d.ts +2 -0
- package/dist/generated/catalog-baseline.js +255 -0
- package/dist/gui-experimental.cjs +111 -0
- package/dist/gui.d.ts +22 -0
- package/dist/gui.js +34 -0
- package/dist/index.d.ts +3 -0
- package/dist/index.js +20 -0
- package/dist/rpc.d.ts +1173 -0
- package/dist/rpc.js +212 -0
- package/dist/server/catalog.d.ts +9 -0
- package/dist/server/catalog.js +74 -0
- package/dist/server/decision-guard-hooks.d.ts +2 -0
- package/dist/server/decision-guard-hooks.js +57 -0
- package/dist/server/decision-guards.d.ts +11 -0
- package/dist/server/decision-guards.js +36 -0
- package/dist/server/engine.d.ts +6 -0
- package/dist/server/engine.js +38 -0
- package/dist/server/guard-hooks.d.ts +3 -0
- package/dist/server/guard-hooks.js +35 -0
- package/dist/server/guards.d.ts +13 -0
- package/dist/server/guards.js +42 -0
- package/dist/server/operations.d.ts +93 -0
- package/dist/server/operations.js +300 -0
- package/dist/server/permissions.d.ts +29 -0
- package/dist/server/permissions.js +97 -0
- package/dist/server/rpc.d.ts +126 -0
- package/dist/server/rpc.js +308 -0
- package/dist/server/runner.d.ts +27 -0
- package/dist/server/runner.js +136 -0
- package/dist/server/schema.d.ts +4 -0
- package/dist/server/schema.js +66 -0
- package/dist/server/tools.d.ts +2 -0
- package/dist/server/tools.js +176 -0
- package/dist/server/tui.d.ts +70 -0
- package/dist/server/tui.js +172 -0
- package/dist/shared/catalog.d.ts +52 -0
- package/dist/shared/catalog.js +84 -0
- package/dist/shared/envelope.d.ts +24 -0
- package/dist/shared/envelope.js +28 -0
- package/dist/shared/manifest.d.ts +53 -0
- package/dist/shared/manifest.js +135 -0
- package/dist/test/baseline.test.d.ts +1 -0
- package/dist/test/baseline.test.js +11 -0
- package/dist/test/catalog.test.d.ts +1 -0
- package/dist/test/catalog.test.js +232 -0
- package/dist/test/decision-guards.test.d.ts +1 -0
- package/dist/test/decision-guards.test.js +30 -0
- package/dist/test/engine.test.d.ts +1 -0
- package/dist/test/engine.test.js +23 -0
- package/dist/test/fixtures.d.ts +12 -0
- package/dist/test/fixtures.js +55 -0
- package/dist/test/guard-hooks.integration.test.d.ts +1 -0
- package/dist/test/guard-hooks.integration.test.js +321 -0
- package/dist/test/guard-hooks.test.d.ts +1 -0
- package/dist/test/guard-hooks.test.js +14 -0
- package/dist/test/guards.test.d.ts +1 -0
- package/dist/test/guards.test.js +24 -0
- package/dist/test/gui.test.d.ts +1 -0
- package/dist/test/gui.test.js +61 -0
- package/dist/test/manifest.test.d.ts +1 -0
- package/dist/test/manifest.test.js +86 -0
- package/dist/test/operations.test.d.ts +1 -0
- package/dist/test/operations.test.js +263 -0
- package/dist/test/parity.test.d.ts +1 -0
- package/dist/test/parity.test.js +56 -0
- package/dist/test/permissions.integration.test.d.ts +1 -0
- package/dist/test/permissions.integration.test.js +535 -0
- package/dist/test/permissions.test.d.ts +1 -0
- package/dist/test/permissions.test.js +42 -0
- package/dist/test/rpc.test.d.ts +1 -0
- package/dist/test/rpc.test.js +389 -0
- package/dist/test/runner.test.d.ts +1 -0
- package/dist/test/runner.test.js +47 -0
- package/dist/test/schema.test.d.ts +1 -0
- package/dist/test/schema.test.js +51 -0
- package/dist/test/tui.test.d.ts +1 -0
- package/dist/test/tui.test.js +226 -0
- package/dist/tprobe.d.ts +1 -0
- package/dist/tprobe.js +2 -0
- package/dist/tui.d.ts +10 -0
- package/dist/tui.js +21 -0
- package/dist/views/operations.d.ts +50 -0
- package/dist/views/operations.js +80 -0
- package/package.json +71 -4
|
@@ -0,0 +1,535 @@
|
|
|
1
|
+
import { test } from "node:test";
|
|
2
|
+
import assert from "node:assert/strict";
|
|
3
|
+
import { mkdtemp, readFile, rm, writeFile } from "node:fs/promises";
|
|
4
|
+
import { join } from "node:path";
|
|
5
|
+
import { tmpdir } from "node:os";
|
|
6
|
+
import { commandPermissions, directoryInputFrom, isWithin, } from "../server/permissions.js";
|
|
7
|
+
import { applyPreview, executeOperation, previewOperation, } from "../server/operations.js";
|
|
8
|
+
import { RPC_ERROR_CODES, createSketchbookRpcHandlers, resetOperations, } from "../server/rpc.js";
|
|
9
|
+
import { BASELINE_MANIFEST } from "../generated/baseline-manifest.js";
|
|
10
|
+
import { COMMAND_RISKS } from "../shared/manifest.js";
|
|
11
|
+
import { makeEngineProject, STUB_ENGINE_VERSION } from "./fixtures.js";
|
|
12
|
+
/**
|
|
13
|
+
* Mocks the host permission surface the way the other plugin tests do: every
|
|
14
|
+
* `assert` is recorded, and an optional predicate turns an action into a
|
|
15
|
+
* denial (the host rejects by throwing).
|
|
16
|
+
*/
|
|
17
|
+
const permissionMock = (_root) => {
|
|
18
|
+
const calls = [];
|
|
19
|
+
let denyFn;
|
|
20
|
+
const permission = {
|
|
21
|
+
assert: async (input) => {
|
|
22
|
+
calls.push({
|
|
23
|
+
action: input.action,
|
|
24
|
+
resource: input.resources[0] ?? "",
|
|
25
|
+
...(input.metadata ? { metadata: input.metadata } : {}),
|
|
26
|
+
});
|
|
27
|
+
if (denyFn?.(input.action)) {
|
|
28
|
+
throw new Error(`${input.action} denied by policy`);
|
|
29
|
+
}
|
|
30
|
+
},
|
|
31
|
+
};
|
|
32
|
+
return {
|
|
33
|
+
permission,
|
|
34
|
+
calls,
|
|
35
|
+
deny: (deny) => {
|
|
36
|
+
denyFn = deny;
|
|
37
|
+
},
|
|
38
|
+
};
|
|
39
|
+
};
|
|
40
|
+
const rejectsType = (promise, type, code) => assert.rejects(promise, (error) => {
|
|
41
|
+
assert.equal(error.type, type);
|
|
42
|
+
if (code)
|
|
43
|
+
assert.equal(error.data?.code, code);
|
|
44
|
+
return true;
|
|
45
|
+
});
|
|
46
|
+
const tmpProject = (prefix) => mkdtemp(join(tmpdir(), prefix));
|
|
47
|
+
const readLog = async (root, name) => {
|
|
48
|
+
try {
|
|
49
|
+
return (await readFile(join(root, name), "utf8")).split("\n").filter(Boolean);
|
|
50
|
+
}
|
|
51
|
+
catch {
|
|
52
|
+
return [];
|
|
53
|
+
}
|
|
54
|
+
};
|
|
55
|
+
const manifestObject = (commands) => ({
|
|
56
|
+
ok: true,
|
|
57
|
+
command: "capabilities",
|
|
58
|
+
changed: [],
|
|
59
|
+
warnings: [],
|
|
60
|
+
errors: [],
|
|
61
|
+
next: null,
|
|
62
|
+
data: {
|
|
63
|
+
protocolVersion: "1.0.0",
|
|
64
|
+
envelopeVersion: 1,
|
|
65
|
+
engine: { name: "@usesketchbook/cli", version: STUB_ENGINE_VERSION },
|
|
66
|
+
groups: { project: "Project" },
|
|
67
|
+
commands,
|
|
68
|
+
},
|
|
69
|
+
});
|
|
70
|
+
const spec = (overrides = {}) => ({
|
|
71
|
+
name: "mutate",
|
|
72
|
+
group: "project",
|
|
73
|
+
summary: "A mutating test command.",
|
|
74
|
+
usage: "sketchbook mutate",
|
|
75
|
+
args: [],
|
|
76
|
+
options: {},
|
|
77
|
+
examples: [],
|
|
78
|
+
mutates: true,
|
|
79
|
+
...overrides,
|
|
80
|
+
});
|
|
81
|
+
// An engine that logs a start line and an end line per invocation (runs.log),
|
|
82
|
+
// serves the given capabilities manifest, and optionally pauses mid-run so
|
|
83
|
+
// concurrency (or its absence) is observable in the log order.
|
|
84
|
+
const loggingProject = async (prefix, commands, { delayMs = 0 } = {}) => {
|
|
85
|
+
const dir = await makeEngineProject(prefix);
|
|
86
|
+
await writeFile(join(dir, "node_modules/@usesketchbook/cli/harness/cli.mjs"), `#!/usr/bin/env node
|
|
87
|
+
import { appendFileSync } from "node:fs";
|
|
88
|
+
import { join } from "node:path";
|
|
89
|
+
const [, , command] = process.argv;
|
|
90
|
+
const argv = process.argv.slice(2).join(" ");
|
|
91
|
+
const log = (line) => appendFileSync(join(process.cwd(), "runs.log"), line + "\\n");
|
|
92
|
+
log("start " + argv);
|
|
93
|
+
if (command === "capabilities") {
|
|
94
|
+
log("end " + command);
|
|
95
|
+
process.stdout.write(${JSON.stringify(JSON.stringify(manifestObject(commands)))});
|
|
96
|
+
} else {
|
|
97
|
+
if (${delayMs}) await new Promise((resolve) => setTimeout(resolve, ${delayMs}));
|
|
98
|
+
log("end " + command);
|
|
99
|
+
process.stdout.write(JSON.stringify({
|
|
100
|
+
ok: true,
|
|
101
|
+
command,
|
|
102
|
+
changed: [],
|
|
103
|
+
warnings: [],
|
|
104
|
+
errors: [],
|
|
105
|
+
next: null,
|
|
106
|
+
data: null,
|
|
107
|
+
}));
|
|
108
|
+
}
|
|
109
|
+
`);
|
|
110
|
+
return dir;
|
|
111
|
+
};
|
|
112
|
+
// ------------------------------------------------------------- risk ladder
|
|
113
|
+
const RISK_ACTIONS = {
|
|
114
|
+
read: ["sketchbook.run"],
|
|
115
|
+
write: ["sketchbook.run", "sketchbook.mutate"],
|
|
116
|
+
delete: ["sketchbook.run", "sketchbook.mutate", "sketchbook.delete"],
|
|
117
|
+
"external-write": ["sketchbook.run", "sketchbook.mutate", "sketchbook.external"],
|
|
118
|
+
install: ["sketchbook.run", "sketchbook.mutate", "sketchbook.install"],
|
|
119
|
+
upgrade: ["sketchbook.run", "sketchbook.mutate", "sketchbook.upgrade"],
|
|
120
|
+
privileged: ["sketchbook.run", "sketchbook.mutate", "sketchbook.privileged"],
|
|
121
|
+
};
|
|
122
|
+
const expectedFor = (mutates, risk) => RISK_ACTIONS[risk ?? (mutates ? "write" : "read")];
|
|
123
|
+
test("every command and subcommand in the baseline manifest maps risk to the permission ladder", () => {
|
|
124
|
+
const root = "/project";
|
|
125
|
+
for (const command of BASELINE_MANIFEST.commands) {
|
|
126
|
+
const risk = command.risk ?? (command.mutates ? "write" : "read");
|
|
127
|
+
const expected = [...expectedFor(command.mutates, command.risk)];
|
|
128
|
+
if (command.name === "init")
|
|
129
|
+
expected.push("sketchbook.init");
|
|
130
|
+
const topLevel = commandPermissions(root, command, {});
|
|
131
|
+
assert.deepEqual([...topLevel.actions].sort(), [...expected].sort(), `command ${command.name} actions follow its risk (${command.risk})`);
|
|
132
|
+
assert.equal(topLevel.checks.some((check) => check.action === "edit"), risk !== "read", `command ${command.name} asserts edit exactly when it is not read-only`);
|
|
133
|
+
// In-root targets never assert external_directory.
|
|
134
|
+
assert.equal(topLevel.checks.some((check) => check.action === "external_directory"), false, `command ${command.name} targets the location and stays internal`);
|
|
135
|
+
if (command.name === "init") {
|
|
136
|
+
assert.ok(topLevel.actions.includes("sketchbook.init"));
|
|
137
|
+
}
|
|
138
|
+
else {
|
|
139
|
+
assert.equal(topLevel.actions.includes("sketchbook.init"), false, `only init asserts sketchbook.init (${command.name} does not)`);
|
|
140
|
+
}
|
|
141
|
+
for (const subcommand of command.subcommands ?? []) {
|
|
142
|
+
const perm = commandPermissions(root, command, { subcommand: subcommand.name });
|
|
143
|
+
assert.deepEqual([...perm.actions].sort(), [...expectedFor(subcommand.mutates, subcommand.risk ?? command.risk)].sort(), `subcommand ${command.name} ${subcommand.name} actions follow its risk`);
|
|
144
|
+
assert.equal(perm.metadata.risk, subcommand.risk ?? command.risk ?? (subcommand.mutates ? "write" : "read"));
|
|
145
|
+
}
|
|
146
|
+
}
|
|
147
|
+
});
|
|
148
|
+
test("the baseline manifest exercises every manifest risk in the ladder", () => {
|
|
149
|
+
const seen = new Set();
|
|
150
|
+
for (const command of BASELINE_MANIFEST.commands) {
|
|
151
|
+
if (command.risk)
|
|
152
|
+
seen.add(command.risk);
|
|
153
|
+
for (const subcommand of command.subcommands ?? []) {
|
|
154
|
+
if (subcommand.risk)
|
|
155
|
+
seen.add(subcommand.risk);
|
|
156
|
+
}
|
|
157
|
+
}
|
|
158
|
+
for (const risk of ["read", "write", "delete", "install", "upgrade"]) {
|
|
159
|
+
assert.ok(seen.has(risk), `the baseline manifest pins the ${risk} rung`);
|
|
160
|
+
}
|
|
161
|
+
// external-write and privileged are valid rungs the baseline does not use;
|
|
162
|
+
// they are pinned by the synthetic commands below.
|
|
163
|
+
});
|
|
164
|
+
test("every risk rung maps to its documented permission actions", () => {
|
|
165
|
+
const root = "/project";
|
|
166
|
+
for (const risk of COMMAND_RISKS) {
|
|
167
|
+
const perm = commandPermissions(root, spec({ risk }), {});
|
|
168
|
+
assert.deepEqual([...perm.actions].sort(), [...RISK_ACTIONS[risk]].sort(), `risk ${risk} maps to its ladder actions`);
|
|
169
|
+
assert.equal(perm.metadata.risk, risk);
|
|
170
|
+
assert.equal(perm.checks.some((check) => check.action === "edit"), risk !== "read");
|
|
171
|
+
if (risk === "read") {
|
|
172
|
+
assert.equal(perm.actions.includes("sketchbook.mutate"), false, "read commands never assert mutate");
|
|
173
|
+
}
|
|
174
|
+
}
|
|
175
|
+
// A legacy manifest without risk falls back to the mutation-based default.
|
|
176
|
+
assert.deepEqual([...commandPermissions(root, spec({ mutates: false }), {}).actions].sort(), ["sketchbook.run"]);
|
|
177
|
+
assert.ok(commandPermissions(root, spec({ mutates: true }), {}).actions.includes("sketchbook.mutate"));
|
|
178
|
+
});
|
|
179
|
+
// ------------------------------------------------------ external_directory
|
|
180
|
+
test("external_directory is asserted from outside the project root only", () => {
|
|
181
|
+
const root = "/tmp/sketch/project";
|
|
182
|
+
const outside = "/tmp/sketch/other/project";
|
|
183
|
+
const init = spec({ name: "init", risk: "write" });
|
|
184
|
+
const external = commandPermissions(root, init, { directory: outside });
|
|
185
|
+
const externalCheck = external.checks.find((check) => check.action === "external_directory");
|
|
186
|
+
assert.ok(externalCheck, "init outside the location asserts external_directory");
|
|
187
|
+
assert.equal(externalCheck.resource, `${outside}/*`);
|
|
188
|
+
// The init target itself is the new directory, not the location.
|
|
189
|
+
assert.deepEqual(external.resources, [outside]);
|
|
190
|
+
// Inside the location (including a fresh subdirectory) stays internal.
|
|
191
|
+
const internal = commandPermissions(root, init, { directory: join(root, "fresh") });
|
|
192
|
+
assert.equal(internal.checks.some((check) => check.action === "external_directory"), false);
|
|
193
|
+
// Siblings that share a path prefix are still outside the root.
|
|
194
|
+
const sibling = commandPermissions(root, init, { directory: "/tmp/sketch/project-evil" });
|
|
195
|
+
assert.ok(sibling.checks.some((check) => check.action === "external_directory"), "a sibling directory sharing a prefix is external");
|
|
196
|
+
assert.equal(isWithin(root, "/tmp/sketch/project-evil"), false);
|
|
197
|
+
assert.equal(isWithin(root, root), true);
|
|
198
|
+
assert.equal(isWithin(root, join(root, "nested", "deep")), true);
|
|
199
|
+
});
|
|
200
|
+
test("non-init commands target the location and do not assert external_directory", () => {
|
|
201
|
+
const perm = commandPermissions("/tmp/sketch/project", spec({ risk: "write" }), { directory: "/tmp/sketch/elsewhere" });
|
|
202
|
+
assert.equal(perm.checks.some((check) => check.action === "external_directory"), false);
|
|
203
|
+
assert.deepEqual(perm.resources, ["/tmp/sketch/project"]);
|
|
204
|
+
});
|
|
205
|
+
// ------------------------------------------- decision/permission × RPC layer
|
|
206
|
+
test("a permission denial maps to sketchbook_permission and nothing is written", async () => {
|
|
207
|
+
resetOperations();
|
|
208
|
+
const dir = await loggingProject("sketchbook-plugin-rpc-deny-", [
|
|
209
|
+
spec({ dryRun: true, risk: "write" }),
|
|
210
|
+
]);
|
|
211
|
+
try {
|
|
212
|
+
const mock = permissionMock(dir);
|
|
213
|
+
// Engine trust is granted; the mutation itself is denied.
|
|
214
|
+
mock.deny((action) => action === "sketchbook.mutate");
|
|
215
|
+
const handlers = createSketchbookRpcHandlers({
|
|
216
|
+
root: dir,
|
|
217
|
+
permission: mock.permission,
|
|
218
|
+
sessionID: "test-session",
|
|
219
|
+
});
|
|
220
|
+
await rejectsType(handlers.run({ command: "mutate", args: [], dryRun: false }, {}), "sketchbook_permission", RPC_ERROR_CODES.permission);
|
|
221
|
+
// The mutation never reached the engine: the engine logs every run, and
|
|
222
|
+
// no mutate invocation appears.
|
|
223
|
+
const log = await readLog(dir, "runs.log");
|
|
224
|
+
assert.equal(log.some((line) => line.includes("mutate")), false, "a denied mutation never spawns the engine for the command");
|
|
225
|
+
// The failed operation is still tracked with a terminal status.
|
|
226
|
+
const status = await handlers.status({}, {});
|
|
227
|
+
const entry = status.operations[0];
|
|
228
|
+
assert.equal(entry?.status, "failed");
|
|
229
|
+
// The denial came from the mutate check, after engine trust and run.
|
|
230
|
+
const actions = mock.calls.map((call) => call.action);
|
|
231
|
+
assert.ok(actions.includes("sketchbook.engine"));
|
|
232
|
+
assert.ok(actions.includes("sketchbook.run"));
|
|
233
|
+
assert.ok(actions.includes("sketchbook.mutate"));
|
|
234
|
+
assert.equal(actions.indexOf("sketchbook.mutate"), actions.length - 1);
|
|
235
|
+
}
|
|
236
|
+
finally {
|
|
237
|
+
await rm(dir, { recursive: true, force: true });
|
|
238
|
+
}
|
|
239
|
+
});
|
|
240
|
+
test("denying the built-in edit boundary also blocks a mutation", async () => {
|
|
241
|
+
resetOperations();
|
|
242
|
+
const dir = await loggingProject("sketchbook-plugin-rpc-edit-", [
|
|
243
|
+
spec({ dryRun: true, risk: "write" }),
|
|
244
|
+
]);
|
|
245
|
+
try {
|
|
246
|
+
const mock = permissionMock(dir);
|
|
247
|
+
mock.deny((action) => action === "edit");
|
|
248
|
+
const handlers = createSketchbookRpcHandlers({
|
|
249
|
+
root: dir,
|
|
250
|
+
permission: mock.permission,
|
|
251
|
+
sessionID: "test-session",
|
|
252
|
+
});
|
|
253
|
+
await rejectsType(handlers.run({ command: "mutate", args: [], dryRun: false }, {}), "sketchbook_permission", RPC_ERROR_CODES.permission);
|
|
254
|
+
const log = await readLog(dir, "runs.log");
|
|
255
|
+
assert.equal(log.some((line) => line.includes("mutate")), false, "denying edit blocks the mutation the same way");
|
|
256
|
+
}
|
|
257
|
+
finally {
|
|
258
|
+
await rm(dir, { recursive: true, force: true });
|
|
259
|
+
}
|
|
260
|
+
});
|
|
261
|
+
test("a mutating RPC run asserts the full check set with dry-run metadata", async () => {
|
|
262
|
+
resetOperations();
|
|
263
|
+
const dir = await loggingProject("sketchbook-plugin-rpc-checks-", [
|
|
264
|
+
spec({ dryRun: true, risk: "write" }),
|
|
265
|
+
]);
|
|
266
|
+
try {
|
|
267
|
+
const mock = permissionMock(dir);
|
|
268
|
+
const handlers = createSketchbookRpcHandlers({
|
|
269
|
+
root: dir,
|
|
270
|
+
permission: mock.permission,
|
|
271
|
+
sessionID: "test-session",
|
|
272
|
+
});
|
|
273
|
+
const result = await handlers.run({ command: "mutate", args: [] }, {});
|
|
274
|
+
// Preview-by-default propagation: the dry run is reported in the envelope.
|
|
275
|
+
assert.equal(result.dryRun, true);
|
|
276
|
+
const mutateCall = mock.calls.find((call) => call.action === "sketchbook.mutate");
|
|
277
|
+
assert.ok(mutateCall);
|
|
278
|
+
assert.equal(mutateCall.metadata?.dryRun, true);
|
|
279
|
+
assert.equal(mutateCall.metadata?.risk, "write");
|
|
280
|
+
assert.equal(mutateCall.metadata?.command, "mutate");
|
|
281
|
+
// The edit check targets the resolved project root.
|
|
282
|
+
const editCall = mock.calls.find((call) => call.action === "edit");
|
|
283
|
+
assert.ok(editCall);
|
|
284
|
+
assert.equal(editCall.resource, dir);
|
|
285
|
+
// And the engine actually ran, as a dry run: one capabilities load plus
|
|
286
|
+
// one dry-run mutation, each with a start and an end line.
|
|
287
|
+
const log = await readLog(dir, "runs.log");
|
|
288
|
+
assert.equal(log.filter((line) => line.includes("mutate")).length, 2);
|
|
289
|
+
assert.equal(log.filter((line) => line.startsWith("start") && line.includes("--dry-run")).length, 1, "the mutation ran once, as a dry run");
|
|
290
|
+
}
|
|
291
|
+
finally {
|
|
292
|
+
await rm(dir, { recursive: true, force: true });
|
|
293
|
+
}
|
|
294
|
+
});
|
|
295
|
+
// ------------------------------------------------------ mutation serialization
|
|
296
|
+
test("concurrent mutations on one location run back to back, reads stay concurrent", async () => {
|
|
297
|
+
resetOperations();
|
|
298
|
+
const delayMs = 150;
|
|
299
|
+
const dir = await loggingProject("sketchbook-plugin-serial-", [
|
|
300
|
+
spec({ dryRun: true, risk: "write" }),
|
|
301
|
+
spec({ name: "probe", mutates: false, risk: "read" }),
|
|
302
|
+
], { delayMs });
|
|
303
|
+
try {
|
|
304
|
+
// Two mutating operations: the second must wait for the first to finish.
|
|
305
|
+
const [first, second] = await Promise.all([
|
|
306
|
+
executeOperation({ root: dir, command: "mutate", dryRun: false }),
|
|
307
|
+
executeOperation({ root: dir, command: "mutate", dryRun: false }),
|
|
308
|
+
]);
|
|
309
|
+
assert.equal(first.envelope.ok, true);
|
|
310
|
+
assert.equal(second.envelope.ok, true);
|
|
311
|
+
const lines = await readLog(dir, "runs.log");
|
|
312
|
+
const phaseIndex = (phase, name) => lines
|
|
313
|
+
.map((line, index) => line.startsWith(phase) && line.includes(name) ? index : -1)
|
|
314
|
+
.filter((index) => index >= 0);
|
|
315
|
+
const mutStarts = phaseIndex("start", "mutate");
|
|
316
|
+
const mutEnds = phaseIndex("end", "mutate");
|
|
317
|
+
assert.equal(mutStarts.length, 2, "each mutation started exactly once");
|
|
318
|
+
assert.equal(mutEnds.length, 2, "each mutation ended exactly once");
|
|
319
|
+
assert.ok(mutStarts[0] < mutEnds[0] && mutEnds[0] < mutStarts[1] && mutStarts[1] < mutEnds[1], `mutations never interleave: ${JSON.stringify(lines)}`);
|
|
320
|
+
// Two read operations on the same location overlap: serialization covers
|
|
321
|
+
// mutations only. Both reads start before either one ends.
|
|
322
|
+
const readDir = await loggingProject("sketchbook-plugin-serial-read-", [spec({ name: "probe", mutates: false, risk: "read" })], { delayMs });
|
|
323
|
+
try {
|
|
324
|
+
await Promise.all([
|
|
325
|
+
executeOperation({ root: readDir, command: "probe" }),
|
|
326
|
+
executeOperation({ root: readDir, command: "probe" }),
|
|
327
|
+
]);
|
|
328
|
+
const readLines = await readLog(readDir, "runs.log");
|
|
329
|
+
const phaseIndexOf = (phase) => readLines
|
|
330
|
+
.map((line, index) => (line.startsWith(phase) && line.includes("probe") ? index : -1))
|
|
331
|
+
.filter((index) => index >= 0);
|
|
332
|
+
const readStarts = phaseIndexOf("start");
|
|
333
|
+
const readEnds = phaseIndexOf("end");
|
|
334
|
+
assert.equal(readStarts.length, 2);
|
|
335
|
+
assert.equal(readEnds.length, 2);
|
|
336
|
+
assert.ok(Math.max(...readStarts) < Math.min(...readEnds), `reads overlap: ${JSON.stringify(readLines)}`);
|
|
337
|
+
}
|
|
338
|
+
finally {
|
|
339
|
+
await rm(readDir, { recursive: true, force: true });
|
|
340
|
+
}
|
|
341
|
+
}
|
|
342
|
+
finally {
|
|
343
|
+
await rm(dir, { recursive: true, force: true });
|
|
344
|
+
}
|
|
345
|
+
});
|
|
346
|
+
test("the same preview applied twice concurrently applies exactly once", async () => {
|
|
347
|
+
resetOperations();
|
|
348
|
+
const dir = await loggingProject("sketchbook-plugin-apply-", [
|
|
349
|
+
spec({ dryRun: true, risk: "write" }),
|
|
350
|
+
]);
|
|
351
|
+
try {
|
|
352
|
+
const preview = await previewOperation({ root: dir, command: "mutate", args: [] });
|
|
353
|
+
const [first, second] = await Promise.allSettled([
|
|
354
|
+
applyPreview({ operationID: preview.id, root: dir }),
|
|
355
|
+
applyPreview({ operationID: preview.id, root: dir }),
|
|
356
|
+
]);
|
|
357
|
+
const fulfilled = [first, second].filter((outcome) => outcome.status === "fulfilled");
|
|
358
|
+
const rejected = [first, second].filter((outcome) => outcome.status === "rejected");
|
|
359
|
+
assert.equal(fulfilled.length, 1, "exactly one apply wins");
|
|
360
|
+
assert.equal(rejected.length, 1, "the loser is rejected");
|
|
361
|
+
assert.match(String(rejected[0]?.reason?.message ?? ""), /already applied/);
|
|
362
|
+
assert.equal(preview.status, "applied");
|
|
363
|
+
// Engine invocations: one capabilities load, the initial preview, one
|
|
364
|
+
// re-validation preview, and exactly one real apply.
|
|
365
|
+
const log = await readLog(dir, "runs.log");
|
|
366
|
+
const starts = log.filter((line) => line.startsWith("start"));
|
|
367
|
+
const applies = starts.filter((line) => !line.includes("--dry-run") && !line.includes("capabilities"));
|
|
368
|
+
assert.equal(applies.length, 1, "the mutation runs exactly once");
|
|
369
|
+
assert.equal(starts.filter((line) => line.includes("--dry-run")).length, 2);
|
|
370
|
+
}
|
|
371
|
+
finally {
|
|
372
|
+
await rm(dir, { recursive: true, force: true });
|
|
373
|
+
}
|
|
374
|
+
});
|
|
375
|
+
test("a failed queued mutation does not block the next one or leak rejections", async () => {
|
|
376
|
+
resetOperations();
|
|
377
|
+
const dir = await loggingProject("sketchbook-plugin-queue-", [
|
|
378
|
+
spec({ dryRun: true, risk: "write" }),
|
|
379
|
+
spec({ name: "probe", mutates: false, risk: "read" }),
|
|
380
|
+
]);
|
|
381
|
+
try {
|
|
382
|
+
const mock = permissionMock(dir);
|
|
383
|
+
let denials = 0;
|
|
384
|
+
mock.deny((action) => {
|
|
385
|
+
if (action === "sketchbook.mutate") {
|
|
386
|
+
// Deny only the first mutation; the queued one is allowed through.
|
|
387
|
+
return denials++ === 0;
|
|
388
|
+
}
|
|
389
|
+
return false;
|
|
390
|
+
});
|
|
391
|
+
const runOperation = () => executeOperation({
|
|
392
|
+
root: dir,
|
|
393
|
+
command: "mutate",
|
|
394
|
+
dryRun: false,
|
|
395
|
+
permission: mock.permission,
|
|
396
|
+
sessionID: "test-session",
|
|
397
|
+
});
|
|
398
|
+
const [blocked, allowed] = await Promise.allSettled([
|
|
399
|
+
runOperation(),
|
|
400
|
+
runOperation(),
|
|
401
|
+
]);
|
|
402
|
+
assert.equal(blocked.status, "rejected", "the denied mutation fails");
|
|
403
|
+
assert.equal(allowed.status, "fulfilled", "the queued mutation still runs");
|
|
404
|
+
const log = await readLog(dir, "runs.log");
|
|
405
|
+
assert.equal(log.some((line) => !line.includes("capabilities") && !line.includes("--dry-run")), true, "the second mutation reached the engine");
|
|
406
|
+
// The queue drained: a later mutation runs immediately and succeeds.
|
|
407
|
+
const after = await executeOperation({ root: dir, command: "mutate", dryRun: false });
|
|
408
|
+
assert.equal(after.envelope.ok, true);
|
|
409
|
+
}
|
|
410
|
+
finally {
|
|
411
|
+
await rm(dir, { recursive: true, force: true });
|
|
412
|
+
}
|
|
413
|
+
});
|
|
414
|
+
// ------------------------------------------- directory option × external_directory
|
|
415
|
+
test("directoryInputFrom reads both spellings of the directory option", () => {
|
|
416
|
+
assert.equal(directoryInputFrom({ directory: "/somewhere" }), "/somewhere");
|
|
417
|
+
assert.equal(directoryInputFrom({ dir: "/somewhere" }), "/somewhere");
|
|
418
|
+
// Explicitly empty or non-string values are not a directory.
|
|
419
|
+
assert.equal(directoryInputFrom({ dir: " " }), undefined);
|
|
420
|
+
assert.equal(directoryInputFrom({ dir: 42 }), undefined);
|
|
421
|
+
assert.equal(directoryInputFrom({ directory: undefined, dir: undefined }), undefined);
|
|
422
|
+
assert.equal(directoryInputFrom(undefined), undefined);
|
|
423
|
+
// The first known key wins.
|
|
424
|
+
assert.equal(directoryInputFrom({ directory: "/first", dir: "/second" }), "/first");
|
|
425
|
+
});
|
|
426
|
+
const initOutside = async ({ deny }) => {
|
|
427
|
+
const dir = await loggingProject("sketchbook-plugin-init-dir-", [
|
|
428
|
+
spec({ name: "init", risk: "write", dryRun: true, options: { dir: { type: "string", description: "Install folder" } } }),
|
|
429
|
+
]);
|
|
430
|
+
const outside = join(await tmpProject("sketchbook-plugin-init-out-"), "fresh");
|
|
431
|
+
const mock = permissionMock(dir);
|
|
432
|
+
mock.deny((action) => deny === true && action === "external_directory");
|
|
433
|
+
return { dir, outside, mock };
|
|
434
|
+
};
|
|
435
|
+
test("an out-of-root init --dir asserts external_directory through the operation service", async () => {
|
|
436
|
+
resetOperations();
|
|
437
|
+
const { dir, outside, mock } = await initOutside({});
|
|
438
|
+
try {
|
|
439
|
+
const result = await executeOperation({
|
|
440
|
+
root: dir,
|
|
441
|
+
command: "init",
|
|
442
|
+
options: { dir: outside },
|
|
443
|
+
dryRun: false,
|
|
444
|
+
permission: mock.permission,
|
|
445
|
+
sessionID: "test-session",
|
|
446
|
+
});
|
|
447
|
+
assert.equal(result.envelope.ok, true);
|
|
448
|
+
// The external boundary is asserted with the documented pattern.
|
|
449
|
+
const externalCall = mock.calls.find((call) => call.action === "external_directory");
|
|
450
|
+
assert.ok(externalCall, "external_directory is asserted for an out-of-root target");
|
|
451
|
+
assert.equal(externalCall.resource, `${outside}/*`);
|
|
452
|
+
// The in-root run/edit checks now target the new install directory.
|
|
453
|
+
const runCall = mock.calls.find((call) => call.action === "sketchbook.run");
|
|
454
|
+
assert.equal(runCall?.resource, outside);
|
|
455
|
+
const editCall = mock.calls.find((call) => call.action === "edit");
|
|
456
|
+
assert.equal(editCall?.resource, outside);
|
|
457
|
+
// And the engine ran the init once (no dry run: apply was explicit).
|
|
458
|
+
const log = await readLog(dir, "runs.log");
|
|
459
|
+
assert.equal(log.filter((line) => line.startsWith("start init")).length, 1);
|
|
460
|
+
}
|
|
461
|
+
finally {
|
|
462
|
+
await rm(dir, { recursive: true, force: true });
|
|
463
|
+
await rm(outside, { recursive: true, force: true }).catch(() => { });
|
|
464
|
+
}
|
|
465
|
+
});
|
|
466
|
+
test("denying external_directory blocks an out-of-root init as a typed permission error with zero engine runs", async () => {
|
|
467
|
+
resetOperations();
|
|
468
|
+
const { dir, outside, mock } = await initOutside({ deny: true });
|
|
469
|
+
try {
|
|
470
|
+
const handlers = createSketchbookRpcHandlers({
|
|
471
|
+
root: dir,
|
|
472
|
+
permission: mock.permission,
|
|
473
|
+
sessionID: "test-session",
|
|
474
|
+
});
|
|
475
|
+
await rejectsType(handlers.run({ command: "init", options: { dir: outside }, dryRun: false }, {}), "sketchbook_permission", RPC_ERROR_CODES.permission);
|
|
476
|
+
// The engine never ran init: no start line for the command.
|
|
477
|
+
const log = await readLog(dir, "runs.log");
|
|
478
|
+
assert.equal(log.some((line) => line.startsWith("start init")), false, "a denied external_directory never spawns the engine");
|
|
479
|
+
// The denial is the last check attempted.
|
|
480
|
+
const actions = mock.calls.map((call) => call.action);
|
|
481
|
+
assert.equal(actions[actions.length - 1], "external_directory");
|
|
482
|
+
}
|
|
483
|
+
finally {
|
|
484
|
+
await rm(dir, { recursive: true, force: true });
|
|
485
|
+
await rm(outside, { recursive: true, force: true }).catch(() => { });
|
|
486
|
+
}
|
|
487
|
+
});
|
|
488
|
+
test("in-root and subdirectory init targets never assert external_directory", async () => {
|
|
489
|
+
resetOperations();
|
|
490
|
+
const dir = await loggingProject("sketchbook-plugin-init-in-", [
|
|
491
|
+
spec({ name: "init", risk: "write", dryRun: true }),
|
|
492
|
+
]);
|
|
493
|
+
try {
|
|
494
|
+
const mock = permissionMock(dir);
|
|
495
|
+
for (const target of [dir, join(dir, "fresh-install")]) {
|
|
496
|
+
await executeOperation({
|
|
497
|
+
root: dir,
|
|
498
|
+
command: "init",
|
|
499
|
+
options: { directory: target },
|
|
500
|
+
dryRun: false,
|
|
501
|
+
permission: mock.permission,
|
|
502
|
+
sessionID: "test-session",
|
|
503
|
+
});
|
|
504
|
+
}
|
|
505
|
+
assert.equal(mock.calls.some((call) => call.action === "external_directory"), false, "in-root targets stay internal");
|
|
506
|
+
const log = await readLog(dir, "runs.log");
|
|
507
|
+
assert.equal(log.filter((line) => line.startsWith("start init")).length, 2);
|
|
508
|
+
}
|
|
509
|
+
finally {
|
|
510
|
+
await rm(dir, { recursive: true, force: true });
|
|
511
|
+
}
|
|
512
|
+
});
|
|
513
|
+
test("non-init commands with a dir option keep the location as their permission target", async () => {
|
|
514
|
+
resetOperations();
|
|
515
|
+
const dir = await loggingProject("sketchbook-plugin-dir-other-", [
|
|
516
|
+
spec({ dryRun: true, risk: "write" }),
|
|
517
|
+
]);
|
|
518
|
+
try {
|
|
519
|
+
const mock = permissionMock(dir);
|
|
520
|
+
await executeOperation({
|
|
521
|
+
root: dir,
|
|
522
|
+
command: "mutate",
|
|
523
|
+
options: { dir: "/elsewhere/project" },
|
|
524
|
+
dryRun: false,
|
|
525
|
+
permission: mock.permission,
|
|
526
|
+
sessionID: "test-session",
|
|
527
|
+
});
|
|
528
|
+
assert.equal(mock.calls.some((call) => call.action === "external_directory"), false, "only init treats a directory option as the target");
|
|
529
|
+
const runCall = mock.calls.find((call) => call.action === "sketchbook.run");
|
|
530
|
+
assert.equal(runCall?.resource, dir);
|
|
531
|
+
}
|
|
532
|
+
finally {
|
|
533
|
+
await rm(dir, { recursive: true, force: true });
|
|
534
|
+
}
|
|
535
|
+
});
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export {};
|
|
@@ -0,0 +1,42 @@
|
|
|
1
|
+
import { test } from "node:test";
|
|
2
|
+
import assert from "node:assert/strict";
|
|
3
|
+
import { commandPermissions } from "../server/permissions.js";
|
|
4
|
+
const command = (overrides = {}) => ({
|
|
5
|
+
name: "mutate",
|
|
6
|
+
group: "project",
|
|
7
|
+
summary: "A test command.",
|
|
8
|
+
usage: "sketchbook mutate",
|
|
9
|
+
args: [],
|
|
10
|
+
options: {},
|
|
11
|
+
examples: [],
|
|
12
|
+
mutates: true,
|
|
13
|
+
...overrides,
|
|
14
|
+
});
|
|
15
|
+
test("read operations do not assert edit", () => {
|
|
16
|
+
const perm = commandPermissions("/project", command({ mutates: false, risk: "read" }), {});
|
|
17
|
+
assert.equal(perm.checks.some((check) => check.action === "edit"), false);
|
|
18
|
+
});
|
|
19
|
+
test("write operations also assert the built-in edit boundary", () => {
|
|
20
|
+
const perm = commandPermissions("/project", command(), {});
|
|
21
|
+
assert.ok(perm.actions.includes("sketchbook.mutate"));
|
|
22
|
+
assert.ok(perm.checks.some((check) => check.action === "edit" && check.resource === "/project"));
|
|
23
|
+
});
|
|
24
|
+
test("init outside the location asserts external_directory", () => {
|
|
25
|
+
const perm = commandPermissions("/project", command({ name: "init", risk: "write" }), { directory: "/elsewhere/project" });
|
|
26
|
+
assert.ok(perm.checks.some((check) => check.action === "external_directory"));
|
|
27
|
+
});
|
|
28
|
+
test("selected subcommand risk overrides the parent command risk", () => {
|
|
29
|
+
const spec = command({
|
|
30
|
+
risk: "write",
|
|
31
|
+
subcommands: [
|
|
32
|
+
{ name: "remove", usage: "mutate remove", summary: "Remove", mutates: true, risk: "delete", examples: [] },
|
|
33
|
+
],
|
|
34
|
+
});
|
|
35
|
+
const perm = commandPermissions("/project", spec, { subcommand: "remove" });
|
|
36
|
+
assert.ok(perm.actions.includes("sketchbook.delete"));
|
|
37
|
+
assert.equal(perm.metadata.risk, "delete");
|
|
38
|
+
});
|
|
39
|
+
test("init inside the location does not assert external_directory", () => {
|
|
40
|
+
const perm = commandPermissions("/project", command({ name: "init", risk: "write" }), { directory: "/project/new-install" });
|
|
41
|
+
assert.equal(perm.checks.some((check) => check.action === "external_directory"), false);
|
|
42
|
+
});
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export {};
|