@buildanagent/harness 0.0.0-stage → 0.1.0-alpha.28
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 +54 -0
- package/LICENSE-APACHE +202 -0
- package/LICENSE-MIT +21 -0
- package/README.md +716 -2
- package/dist/ablation.d.ts +2 -0
- package/dist/ablation.js +3 -0
- package/dist/ablation.js.map +1 -0
- package/dist/completion/check.d.ts +64 -0
- package/dist/completion/check.js +152 -0
- package/dist/completion/check.js.map +1 -0
- package/dist/completion/checks.d.ts +45 -0
- package/dist/completion/checks.js +173 -0
- package/dist/completion/checks.js.map +1 -0
- package/dist/completion/flags.d.ts +25 -0
- package/dist/completion/flags.js +87 -0
- package/dist/completion/flags.js.map +1 -0
- package/dist/completion/index.d.ts +8 -0
- package/dist/completion/index.js +9 -0
- package/dist/completion/index.js.map +1 -0
- package/dist/completion/snapshot.d.ts +33 -0
- package/dist/completion/snapshot.js +83 -0
- package/dist/completion/snapshot.js.map +1 -0
- package/dist/context/cap.d.ts +32 -0
- package/dist/context/cap.js +50 -0
- package/dist/context/cap.js.map +1 -0
- package/dist/context/estimate.d.ts +18 -0
- package/dist/context/estimate.js +20 -0
- package/dist/context/estimate.js.map +1 -0
- package/dist/context/file-spill.d.ts +5 -0
- package/dist/context/file-spill.js +25 -0
- package/dist/context/file-spill.js.map +1 -0
- package/dist/context/index.d.ts +10 -0
- package/dist/context/index.js +10 -0
- package/dist/context/index.js.map +1 -0
- package/dist/context/mask.d.ts +27 -0
- package/dist/context/mask.js +71 -0
- package/dist/context/mask.js.map +1 -0
- package/dist/context/mid-turn.d.ts +40 -0
- package/dist/context/mid-turn.js +82 -0
- package/dist/context/mid-turn.js.map +1 -0
- package/dist/context/policy.d.ts +9 -0
- package/dist/context/policy.js +140 -0
- package/dist/context/policy.js.map +1 -0
- package/dist/context/spill-ledger.d.ts +25 -0
- package/dist/context/spill-ledger.js +54 -0
- package/dist/context/spill-ledger.js.map +1 -0
- package/dist/context/spill.d.ts +6 -0
- package/dist/context/spill.js +14 -0
- package/dist/context/spill.js.map +1 -0
- package/dist/context/summarize.d.ts +53 -0
- package/dist/context/summarize.js +204 -0
- package/dist/context/summarize.js.map +1 -0
- package/dist/context/summary-template.d.ts +29 -0
- package/dist/context/summary-template.js +69 -0
- package/dist/context/summary-template.js.map +1 -0
- package/dist/context/types.d.ts +150 -0
- package/dist/context/types.js +2 -0
- package/dist/context/types.js.map +1 -0
- package/dist/context/write-cap.d.ts +19 -0
- package/dist/context/write-cap.js +41 -0
- package/dist/context/write-cap.js.map +1 -0
- package/dist/create-harness.d.ts +7 -0
- package/dist/create-harness.js +21 -0
- package/dist/create-harness.js.map +1 -0
- package/dist/credentials/addresses.d.ts +14 -0
- package/dist/credentials/addresses.js +144 -0
- package/dist/credentials/addresses.js.map +1 -0
- package/dist/credentials/allowlist.d.ts +34 -0
- package/dist/credentials/allowlist.js +77 -0
- package/dist/credentials/allowlist.js.map +1 -0
- package/dist/credentials/egress-proxy.d.ts +70 -0
- package/dist/credentials/egress-proxy.js +482 -0
- package/dist/credentials/egress-proxy.js.map +1 -0
- package/dist/credentials/index.d.ts +9 -0
- package/dist/credentials/index.js +10 -0
- package/dist/credentials/index.js.map +1 -0
- package/dist/credentials/scrub.d.ts +16 -0
- package/dist/credentials/scrub.js +93 -0
- package/dist/credentials/scrub.js.map +1 -0
- package/dist/errors.d.ts +56 -0
- package/dist/errors.js +63 -0
- package/dist/errors.js.map +1 -0
- package/dist/extensions/compose.d.ts +27 -0
- package/dist/extensions/compose.js +83 -0
- package/dist/extensions/compose.js.map +1 -0
- package/dist/extensions/context-files-node.d.ts +28 -0
- package/dist/extensions/context-files-node.js +74 -0
- package/dist/extensions/context-files-node.js.map +1 -0
- package/dist/extensions/context-files.d.ts +68 -0
- package/dist/extensions/context-files.js +214 -0
- package/dist/extensions/context-files.js.map +1 -0
- package/dist/extensions/digest.d.ts +6 -0
- package/dist/extensions/digest.js +68 -0
- package/dist/extensions/digest.js.map +1 -0
- package/dist/extensions/index.d.ts +10 -0
- package/dist/extensions/index.js +10 -0
- package/dist/extensions/index.js.map +1 -0
- package/dist/extensions/load.d.ts +42 -0
- package/dist/extensions/load.js +211 -0
- package/dist/extensions/load.js.map +1 -0
- package/dist/extensions/lock.d.ts +8 -0
- package/dist/extensions/lock.js +95 -0
- package/dist/extensions/lock.js.map +1 -0
- package/dist/extensions/node.d.ts +10 -0
- package/dist/extensions/node.js +11 -0
- package/dist/extensions/node.js.map +1 -0
- package/dist/extensions/project-dirs.d.ts +24 -0
- package/dist/extensions/project-dirs.js +97 -0
- package/dist/extensions/project-dirs.js.map +1 -0
- package/dist/extensions/skills-node.d.ts +34 -0
- package/dist/extensions/skills-node.js +123 -0
- package/dist/extensions/skills-node.js.map +1 -0
- package/dist/extensions/skills.d.ts +51 -0
- package/dist/extensions/skills.js +175 -0
- package/dist/extensions/skills.js.map +1 -0
- package/dist/extensions/trust-file.d.ts +3 -0
- package/dist/extensions/trust-file.js +62 -0
- package/dist/extensions/trust-file.js.map +1 -0
- package/dist/extensions/trust-store-node.d.ts +10 -0
- package/dist/extensions/trust-store-node.js +33 -0
- package/dist/extensions/trust-store-node.js.map +1 -0
- package/dist/extensions/types.d.ts +42 -0
- package/dist/extensions/types.js +2 -0
- package/dist/extensions/types.js.map +1 -0
- package/dist/harness.d.ts +156 -0
- package/dist/harness.js +2 -0
- package/dist/harness.js.map +1 -0
- package/dist/index.d.ts +4 -0
- package/dist/index.js +5 -0
- package/dist/index.js.map +1 -0
- package/dist/mcp/connection.d.ts +22 -0
- package/dist/mcp/connection.js +267 -0
- package/dist/mcp/connection.js.map +1 -0
- package/dist/mcp/content.d.ts +27 -0
- package/dist/mcp/content.js +177 -0
- package/dist/mcp/content.js.map +1 -0
- package/dist/mcp/errors.d.ts +19 -0
- package/dist/mcp/errors.js +21 -0
- package/dist/mcp/errors.js.map +1 -0
- package/dist/mcp/index.d.ts +10 -0
- package/dist/mcp/index.js +10 -0
- package/dist/mcp/index.js.map +1 -0
- package/dist/mcp/manager.d.ts +7 -0
- package/dist/mcp/manager.js +173 -0
- package/dist/mcp/manager.js.map +1 -0
- package/dist/mcp/record.d.ts +7 -0
- package/dist/mcp/record.js +20 -0
- package/dist/mcp/record.js.map +1 -0
- package/dist/mcp/stdio.d.ts +39 -0
- package/dist/mcp/stdio.js +382 -0
- package/dist/mcp/stdio.js.map +1 -0
- package/dist/mcp/types.d.ts +172 -0
- package/dist/mcp/types.js +2 -0
- package/dist/mcp/types.js.map +1 -0
- package/dist/memory/index.d.ts +2 -0
- package/dist/memory/index.js +3 -0
- package/dist/memory/index.js.map +1 -0
- package/dist/permissions/default-rules.d.ts +7 -0
- package/dist/permissions/default-rules.js +42 -0
- package/dist/permissions/default-rules.js.map +1 -0
- package/dist/permissions/errors.d.ts +11 -0
- package/dist/permissions/errors.js +14 -0
- package/dist/permissions/errors.js.map +1 -0
- package/dist/permissions/index.d.ts +9 -0
- package/dist/permissions/index.js +9 -0
- package/dist/permissions/index.js.map +1 -0
- package/dist/permissions/match.d.ts +33 -0
- package/dist/permissions/match.js +287 -0
- package/dist/permissions/match.js.map +1 -0
- package/dist/permissions/pipeline.d.ts +9 -0
- package/dist/permissions/pipeline.js +235 -0
- package/dist/permissions/pipeline.js.map +1 -0
- package/dist/permissions/rules.d.ts +22 -0
- package/dist/permissions/rules.js +74 -0
- package/dist/permissions/rules.js.map +1 -0
- package/dist/permissions/shadow.d.ts +5 -0
- package/dist/permissions/shadow.js +22 -0
- package/dist/permissions/shadow.js.map +1 -0
- package/dist/permissions/types.d.ts +104 -0
- package/dist/permissions/types.js +2 -0
- package/dist/permissions/types.js.map +1 -0
- package/dist/sandbox/base.d.ts +35 -0
- package/dist/sandbox/base.js +60 -0
- package/dist/sandbox/base.js.map +1 -0
- package/dist/sandbox/docker/index.d.ts +35 -0
- package/dist/sandbox/docker/index.js +448 -0
- package/dist/sandbox/docker/index.js.map +1 -0
- package/dist/sandbox/errors.d.ts +16 -0
- package/dist/sandbox/errors.js +22 -0
- package/dist/sandbox/errors.js.map +1 -0
- package/dist/sandbox/host-files.d.ts +63 -0
- package/dist/sandbox/host-files.js +248 -0
- package/dist/sandbox/host-files.js.map +1 -0
- package/dist/sandbox/index.d.ts +8 -0
- package/dist/sandbox/index.js +8 -0
- package/dist/sandbox/index.js.map +1 -0
- package/dist/sandbox/none.d.ts +17 -0
- package/dist/sandbox/none.js +108 -0
- package/dist/sandbox/none.js.map +1 -0
- package/dist/sandbox/process.d.ts +61 -0
- package/dist/sandbox/process.js +253 -0
- package/dist/sandbox/process.js.map +1 -0
- package/dist/sandbox/spawn-env.d.ts +20 -0
- package/dist/sandbox/spawn-env.js +128 -0
- package/dist/sandbox/spawn-env.js.map +1 -0
- package/dist/sandbox/srt/index.d.ts +35 -0
- package/dist/sandbox/srt/index.js +264 -0
- package/dist/sandbox/srt/index.js.map +1 -0
- package/dist/sandbox/srt/settings.d.ts +48 -0
- package/dist/sandbox/srt/settings.js +31 -0
- package/dist/sandbox/srt/settings.js.map +1 -0
- package/dist/sandbox/types.d.ts +88 -0
- package/dist/sandbox/types.js +6 -0
- package/dist/sandbox/types.js.map +1 -0
- package/dist/server/answers.d.ts +16 -0
- package/dist/server/answers.js +43 -0
- package/dist/server/answers.js.map +1 -0
- package/dist/server/connection.d.ts +99 -0
- package/dist/server/connection.js +309 -0
- package/dist/server/connection.js.map +1 -0
- package/dist/server/dispatcher.d.ts +86 -0
- package/dist/server/dispatcher.js +800 -0
- package/dist/server/dispatcher.js.map +1 -0
- package/dist/server/index.d.ts +13 -0
- package/dist/server/index.js +12 -0
- package/dist/server/index.js.map +1 -0
- package/dist/server/local-transport.d.ts +44 -0
- package/dist/server/local-transport.js +103 -0
- package/dist/server/local-transport.js.map +1 -0
- package/dist/server/outbox.d.ts +31 -0
- package/dist/server/outbox.js +130 -0
- package/dist/server/outbox.js.map +1 -0
- package/dist/server/params.d.ts +25 -0
- package/dist/server/params.js +112 -0
- package/dist/server/params.js.map +1 -0
- package/dist/server/protocol.schema.json +2448 -0
- package/dist/server/rpc-session.d.ts +43 -0
- package/dist/server/rpc-session.js +167 -0
- package/dist/server/rpc-session.js.map +1 -0
- package/dist/server/stdio-streams.d.ts +67 -0
- package/dist/server/stdio-streams.js +218 -0
- package/dist/server/stdio-streams.js.map +1 -0
- package/dist/server/stdio.d.ts +59 -0
- package/dist/server/stdio.js +189 -0
- package/dist/server/stdio.js.map +1 -0
- package/dist/server/websocket.d.ts +106 -0
- package/dist/server/websocket.js +327 -0
- package/dist/server/websocket.js.map +1 -0
- package/dist/session/approvals.d.ts +81 -0
- package/dist/session/approvals.js +217 -0
- package/dist/session/approvals.js.map +1 -0
- package/dist/session/code-call-log.d.ts +42 -0
- package/dist/session/code-call-log.js +63 -0
- package/dist/session/code-call-log.js.map +1 -0
- package/dist/session/code-call-result.d.ts +19 -0
- package/dist/session/code-call-result.js +95 -0
- package/dist/session/code-call-result.js.map +1 -0
- package/dist/session/code-call.d.ts +49 -0
- package/dist/session/code-call.js +345 -0
- package/dist/session/code-call.js.map +1 -0
- package/dist/session/effect-scope.d.ts +11 -0
- package/dist/session/effect-scope.js +10 -0
- package/dist/session/effect-scope.js.map +1 -0
- package/dist/session/errors.d.ts +19 -0
- package/dist/session/errors.js +27 -0
- package/dist/session/errors.js.map +1 -0
- package/dist/session/file-index.d.ts +8 -0
- package/dist/session/file-index.js +70 -0
- package/dist/session/file-index.js.map +1 -0
- package/dist/session/file-key-store.d.ts +16 -0
- package/dist/session/file-key-store.js +169 -0
- package/dist/session/file-key-store.js.map +1 -0
- package/dist/session/file-lock.d.ts +52 -0
- package/dist/session/file-lock.js +324 -0
- package/dist/session/file-lock.js.map +1 -0
- package/dist/session/index.d.ts +22 -0
- package/dist/session/index.js +20 -0
- package/dist/session/index.js.map +1 -0
- package/dist/session/jsonl-store.d.ts +37 -0
- package/dist/session/jsonl-store.js +386 -0
- package/dist/session/jsonl-store.js.map +1 -0
- package/dist/session/jsonl.d.ts +9 -0
- package/dist/session/jsonl.js +10 -0
- package/dist/session/jsonl.js.map +1 -0
- package/dist/session/lock.d.ts +19 -0
- package/dist/session/lock.js +2 -0
- package/dist/session/lock.js.map +1 -0
- package/dist/session/memory-store.d.ts +12 -0
- package/dist/session/memory-store.js +60 -0
- package/dist/session/memory-store.js.map +1 -0
- package/dist/session/migrate.d.ts +41 -0
- package/dist/session/migrate.js +86 -0
- package/dist/session/migrate.js.map +1 -0
- package/dist/session/pointer.d.ts +15 -0
- package/dist/session/pointer.js +22 -0
- package/dist/session/pointer.js.map +1 -0
- package/dist/session/rebuild.d.ts +53 -0
- package/dist/session/rebuild.js +230 -0
- package/dist/session/rebuild.js.map +1 -0
- package/dist/session/resume.d.ts +26 -0
- package/dist/session/resume.js +128 -0
- package/dist/session/resume.js.map +1 -0
- package/dist/session/retention.d.ts +97 -0
- package/dist/session/retention.js +702 -0
- package/dist/session/retention.js.map +1 -0
- package/dist/session/runner.d.ts +156 -0
- package/dist/session/runner.js +656 -0
- package/dist/session/runner.js.map +1 -0
- package/dist/session/scheduler.d.ts +47 -0
- package/dist/session/scheduler.js +85 -0
- package/dist/session/scheduler.js.map +1 -0
- package/dist/session/secret-key-store.d.ts +47 -0
- package/dist/session/secret-key-store.js +79 -0
- package/dist/session/secret-key-store.js.map +1 -0
- package/dist/session/session.d.ts +106 -0
- package/dist/session/session.js +353 -0
- package/dist/session/session.js.map +1 -0
- package/dist/session/sticky.d.ts +48 -0
- package/dist/session/sticky.js +232 -0
- package/dist/session/sticky.js.map +1 -0
- package/dist/session/store.d.ts +108 -0
- package/dist/session/store.js +163 -0
- package/dist/session/store.js.map +1 -0
- package/dist/telemetry/index.d.ts +2 -0
- package/dist/telemetry/index.js +3 -0
- package/dist/telemetry/index.js.map +1 -0
- package/dist/testing/fake-sandbox.d.ts +34 -0
- package/dist/testing/fake-sandbox.js +221 -0
- package/dist/testing/fake-sandbox.js.map +1 -0
- package/dist/testing/index.d.ts +10 -0
- package/dist/testing/index.js +11 -0
- package/dist/testing/index.js.map +1 -0
- package/dist/testing/key-store-conformance.d.ts +22 -0
- package/dist/testing/key-store-conformance.js +97 -0
- package/dist/testing/key-store-conformance.js.map +1 -0
- package/dist/testing/run-cases.d.ts +21 -0
- package/dist/testing/run-cases.js +20 -0
- package/dist/testing/run-cases.js.map +1 -0
- package/dist/testing/sandbox-conformance.d.ts +64 -0
- package/dist/testing/sandbox-conformance.js +361 -0
- package/dist/testing/sandbox-conformance.js.map +1 -0
- package/dist/testing/scheduler-conformance.d.ts +32 -0
- package/dist/testing/scheduler-conformance.js +72 -0
- package/dist/testing/scheduler-conformance.js.map +1 -0
- package/dist/testing/store-conformance.d.ts +21 -0
- package/dist/testing/store-conformance.js +320 -0
- package/dist/testing/store-conformance.js.map +1 -0
- package/dist/tools/errors.d.ts +13 -0
- package/dist/tools/errors.js +17 -0
- package/dist/tools/errors.js.map +1 -0
- package/dist/tools/index.d.ts +8 -0
- package/dist/tools/index.js +8 -0
- package/dist/tools/index.js.map +1 -0
- package/dist/tools/registry.d.ts +3 -0
- package/dist/tools/registry.js +234 -0
- package/dist/tools/registry.js.map +1 -0
- package/dist/tools/tool-search.d.ts +30 -0
- package/dist/tools/tool-search.js +118 -0
- package/dist/tools/tool-search.js.map +1 -0
- package/dist/tools/types.d.ts +106 -0
- package/dist/tools/types.js +2 -0
- package/dist/tools/types.js.map +1 -0
- package/llms.txt +435 -0
- package/package.json +118 -3
|
@@ -0,0 +1,221 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* `fakeSandbox()`: a sandbox in memory, for tests of the code that runs on one. It enforces the policy it is given, so
|
|
3
|
+
* a test can see a denial, and it records every `exec`, but it confines no process and runs no program: it understands
|
|
4
|
+
* a few commands, listed at `FAKE_COMMANDS`. It is portable, so a test can use it on every target.
|
|
5
|
+
*/
|
|
6
|
+
import { HarnessError } from "../errors.js";
|
|
7
|
+
import { SandboxDeniedError, SandboxUnavailableError } from "../sandbox/errors.js";
|
|
8
|
+
/**
|
|
9
|
+
* The commands the fake runs, in the form `[name, ...arguments]`: `echo` prints its arguments, `env` prints the
|
|
10
|
+
* environment it was given as `NAME=value` lines, `cat <path>` prints a file, `write <path> <text>` writes one,
|
|
11
|
+
* `true` and `false` exit with 0 and 1, `sleep <seconds>` waits and honours the timeout and the signal, and
|
|
12
|
+
* `net-probe <host>` reaches a host only if the policy allows it. Any other command exits with 127.
|
|
13
|
+
*/
|
|
14
|
+
export const FAKE_COMMANDS = ["echo", "env", "cat", "write", "true", "false", "sleep", "net-probe"];
|
|
15
|
+
const ENCODER = new TextEncoder();
|
|
16
|
+
const DECODER = new TextDecoder();
|
|
17
|
+
function normalize(path) {
|
|
18
|
+
const parts = [];
|
|
19
|
+
for (const part of path.split("/")) {
|
|
20
|
+
if (part === "" || part === ".")
|
|
21
|
+
continue;
|
|
22
|
+
if (part === "..")
|
|
23
|
+
parts.pop();
|
|
24
|
+
else
|
|
25
|
+
parts.push(part);
|
|
26
|
+
}
|
|
27
|
+
return `/${parts.join("/")}`;
|
|
28
|
+
}
|
|
29
|
+
const within = (path, root) => {
|
|
30
|
+
const base = normalize(root);
|
|
31
|
+
return base === "/" || path === base || path.startsWith(`${base}/`);
|
|
32
|
+
};
|
|
33
|
+
class FakeSandbox {
|
|
34
|
+
disposed = false;
|
|
35
|
+
// What ends each command still running, so `dispose()` can reject them as a real backend does.
|
|
36
|
+
running = new Set();
|
|
37
|
+
store = new Map();
|
|
38
|
+
links = new Map();
|
|
39
|
+
root;
|
|
40
|
+
policy;
|
|
41
|
+
log;
|
|
42
|
+
constructor(root, policy, log) {
|
|
43
|
+
this.root = normalize(root);
|
|
44
|
+
this.policy = policy;
|
|
45
|
+
this.log = log;
|
|
46
|
+
}
|
|
47
|
+
live() {
|
|
48
|
+
if (this.disposed)
|
|
49
|
+
throw new HarnessError("sandbox_disposed", "the sandbox was disposed");
|
|
50
|
+
}
|
|
51
|
+
/** The absolute path a name stands for: joined to the root, tidied of `.` and `..`, then followed through links. */
|
|
52
|
+
resolve(path, base = this.root) {
|
|
53
|
+
const tidy = normalize(path.startsWith("/") ? path : `${base}/${path}`);
|
|
54
|
+
for (const [link, target] of this.links) {
|
|
55
|
+
if (tidy === link || tidy.startsWith(`${link}/`))
|
|
56
|
+
return normalize(`${target}${tidy.slice(link.length)}`);
|
|
57
|
+
}
|
|
58
|
+
return tidy;
|
|
59
|
+
}
|
|
60
|
+
readDenial(path) {
|
|
61
|
+
const roots = [...this.policy.readRoots, ...this.policy.writeRoots];
|
|
62
|
+
if (roots.some((root) => within(path, root)))
|
|
63
|
+
return undefined;
|
|
64
|
+
return { kind: "filesystem_read", target: path, reason: "the path is outside the read roots" };
|
|
65
|
+
}
|
|
66
|
+
writeDenial(path) {
|
|
67
|
+
if (this.policy.writeRoots.some((root) => within(path, root)))
|
|
68
|
+
return undefined;
|
|
69
|
+
return { kind: "filesystem_write", target: path, reason: "the path is outside the write roots" };
|
|
70
|
+
}
|
|
71
|
+
networkDenial(host) {
|
|
72
|
+
const { allow, deny } = this.policy.network;
|
|
73
|
+
if (allow.includes(host) && !deny.includes(host))
|
|
74
|
+
return undefined;
|
|
75
|
+
return { kind: "network", target: host, reason: "the host is not on the network allowlist" };
|
|
76
|
+
}
|
|
77
|
+
async readFile(path) {
|
|
78
|
+
this.live();
|
|
79
|
+
const target = this.resolve(path);
|
|
80
|
+
const denial = this.readDenial(target);
|
|
81
|
+
if (denial)
|
|
82
|
+
throw new SandboxDeniedError(denial);
|
|
83
|
+
const data = this.store.get(target);
|
|
84
|
+
if (data === undefined)
|
|
85
|
+
throw new HarnessError("sandbox_not_found", `no such file: ${target}`);
|
|
86
|
+
return data.slice();
|
|
87
|
+
}
|
|
88
|
+
async writeFile(path, data) {
|
|
89
|
+
this.live();
|
|
90
|
+
const target = this.resolve(path);
|
|
91
|
+
const denial = this.writeDenial(target);
|
|
92
|
+
if (denial)
|
|
93
|
+
throw new SandboxDeniedError(denial);
|
|
94
|
+
this.store.set(target, data.slice());
|
|
95
|
+
}
|
|
96
|
+
/** Writes a file without a policy check, for the manifest's own files. */
|
|
97
|
+
seed(path, data) {
|
|
98
|
+
this.store.set(this.resolve(path), data.slice());
|
|
99
|
+
}
|
|
100
|
+
async exec(command, options) {
|
|
101
|
+
this.live();
|
|
102
|
+
if (!Array.isArray(command) || command.length === 0) {
|
|
103
|
+
throw new HarnessError("invalid_sandbox_options", "a sandbox command is a non-empty list of strings");
|
|
104
|
+
}
|
|
105
|
+
options.signal.throwIfAborted();
|
|
106
|
+
const cwd = this.resolve(options.cwd ?? ".");
|
|
107
|
+
const denial = this.readDenial(cwd);
|
|
108
|
+
if (denial)
|
|
109
|
+
return { denied: denial };
|
|
110
|
+
this.log.push({ command: [...command], cwd: options.cwd, env: { ...options.env }, timeoutMs: options.timeoutMs });
|
|
111
|
+
const [name = "", ...args] = command;
|
|
112
|
+
return this.run(name, args, options);
|
|
113
|
+
}
|
|
114
|
+
async run(name, args, options) {
|
|
115
|
+
const ok = (stdout = "") => ({ exitCode: 0, stdout, stderr: "" });
|
|
116
|
+
switch (name) {
|
|
117
|
+
case "echo":
|
|
118
|
+
return ok(`${args.join(" ")}\n`);
|
|
119
|
+
case "env":
|
|
120
|
+
return ok(Object.entries(options.env)
|
|
121
|
+
.map(([key, value]) => `${key}=${value}\n`)
|
|
122
|
+
.join(""));
|
|
123
|
+
case "true":
|
|
124
|
+
return ok();
|
|
125
|
+
case "false":
|
|
126
|
+
return { exitCode: 1, stdout: "", stderr: "" };
|
|
127
|
+
case "cat":
|
|
128
|
+
return this.cat(args[0] ?? "");
|
|
129
|
+
case "write":
|
|
130
|
+
return this.writeCommand(args[0] ?? "", args.slice(1).join(" "));
|
|
131
|
+
case "net-probe": {
|
|
132
|
+
const host = args[0] ?? "";
|
|
133
|
+
const denial = this.networkDenial(host);
|
|
134
|
+
return denial ? { denied: denial } : ok(`reached ${host}\n`);
|
|
135
|
+
}
|
|
136
|
+
case "sleep":
|
|
137
|
+
return this.sleep(Number(args[0] ?? 0) * 1000, options);
|
|
138
|
+
default:
|
|
139
|
+
return { exitCode: 127, stdout: "", stderr: `command not found: ${name}\n` };
|
|
140
|
+
}
|
|
141
|
+
}
|
|
142
|
+
cat(path) {
|
|
143
|
+
const target = this.resolve(path);
|
|
144
|
+
const denial = this.readDenial(target);
|
|
145
|
+
if (denial)
|
|
146
|
+
return { denied: denial };
|
|
147
|
+
const data = this.store.get(target);
|
|
148
|
+
return data === undefined
|
|
149
|
+
? { exitCode: 1, stdout: "", stderr: `no such file: ${target}\n` }
|
|
150
|
+
: { exitCode: 0, stdout: DECODER.decode(data), stderr: "" };
|
|
151
|
+
}
|
|
152
|
+
writeCommand(path, text) {
|
|
153
|
+
const target = this.resolve(path);
|
|
154
|
+
const denial = this.writeDenial(target);
|
|
155
|
+
if (denial)
|
|
156
|
+
return { denied: denial };
|
|
157
|
+
this.store.set(target, ENCODER.encode(text));
|
|
158
|
+
return { exitCode: 0, stdout: "", stderr: "" };
|
|
159
|
+
}
|
|
160
|
+
sleep(ms, options) {
|
|
161
|
+
return new Promise((resolve, reject) => {
|
|
162
|
+
const done = (result) => {
|
|
163
|
+
clearTimeout(timer);
|
|
164
|
+
options.signal.removeEventListener("abort", aborted);
|
|
165
|
+
this.running.delete(disposed);
|
|
166
|
+
resolve(result);
|
|
167
|
+
};
|
|
168
|
+
const aborted = () => {
|
|
169
|
+
clearTimeout(timer);
|
|
170
|
+
this.running.delete(disposed);
|
|
171
|
+
reject(options.signal.reason);
|
|
172
|
+
};
|
|
173
|
+
const disposed = () => {
|
|
174
|
+
clearTimeout(timer);
|
|
175
|
+
options.signal.removeEventListener("abort", aborted);
|
|
176
|
+
reject(new HarnessError("sandbox_disposed", "the sandbox was disposed"));
|
|
177
|
+
};
|
|
178
|
+
this.running.add(disposed);
|
|
179
|
+
const stopAt = options.timeoutMs !== undefined && options.timeoutMs < ms ? options.timeoutMs : ms;
|
|
180
|
+
const timer = setTimeout(() => {
|
|
181
|
+
const timedOut = stopAt < ms;
|
|
182
|
+
done({ exitCode: timedOut ? 124 : 0, stdout: "", stderr: timedOut ? `timed out after ${stopAt} ms\n` : "" });
|
|
183
|
+
}, stopAt);
|
|
184
|
+
options.signal.addEventListener("abort", aborted, { once: true });
|
|
185
|
+
});
|
|
186
|
+
}
|
|
187
|
+
async dispose() {
|
|
188
|
+
this.disposed = true;
|
|
189
|
+
for (const stop of [...this.running])
|
|
190
|
+
stop();
|
|
191
|
+
this.running.clear();
|
|
192
|
+
}
|
|
193
|
+
}
|
|
194
|
+
/**
|
|
195
|
+
* Builds a fake backend with the tier `os` and the stops `mistakes` and `exfiltration`, because it enforces the
|
|
196
|
+
* policy's file roots and network allowlist in memory. That says nothing about a real process: it runs none.
|
|
197
|
+
*/
|
|
198
|
+
export function fakeSandbox(options = {}) {
|
|
199
|
+
const execs = [];
|
|
200
|
+
let latest;
|
|
201
|
+
return {
|
|
202
|
+
tier: "os",
|
|
203
|
+
stops: ["mistakes", "exfiltration"],
|
|
204
|
+
execs,
|
|
205
|
+
files: () => latest?.store ?? new Map(),
|
|
206
|
+
symlink(link, target) {
|
|
207
|
+
latest?.links.set(normalize(link), normalize(target));
|
|
208
|
+
},
|
|
209
|
+
async provision(manifest, policy) {
|
|
210
|
+
if (options.unavailable !== undefined)
|
|
211
|
+
throw new SandboxUnavailableError("fake", options.unavailable);
|
|
212
|
+
const sandbox = new FakeSandbox(manifest.root, policy, execs);
|
|
213
|
+
for (const [path, data] of Object.entries(manifest.files ?? {})) {
|
|
214
|
+
sandbox.seed(path, typeof data === "string" ? ENCODER.encode(data) : data);
|
|
215
|
+
}
|
|
216
|
+
latest = sandbox;
|
|
217
|
+
return sandbox;
|
|
218
|
+
},
|
|
219
|
+
};
|
|
220
|
+
}
|
|
221
|
+
//# sourceMappingURL=fake-sandbox.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"fake-sandbox.js","sourceRoot":"","sources":["../../src/testing/fake-sandbox.ts"],"names":[],"mappings":"AAAA;;;;GAIG;AACH,OAAO,EAAE,YA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AC;QACnC,KAAK;QACL,KAAK,EAAE,GAAG,EAAE,CAAC,MAAM,EAAE,KAAK,IAAI,IAAI,GAAG,EAAE;QACvC,OAAO,CAAC,IAAY,EAAE,MAAc;YAClC,MAAM,EAAE,KAAK,CAAC,GAAG,CAAC,SAAS,CAAC,IAAI,CAAC,EAAE,SAAS,CAAC,MAAM,CAAC,CAAC,CAAC;QACxD,CAAC;QACD,KAAK,CAAC,SAAS,CAAC,QAA2B,EAAE,MAAqB;YAChE,IAAI,OAAO,CAAC,WAAW,KAAK,SAAS;gBAAE,MAAM,IAAI,uBAAuB,CAAC,MAAM,EAAE,OAAO,CAAC,WAAW,CAAC,CAAC;YACtG,MAAM,OAAO,GAAG,IAAI,WAAW,CAAC,QAAQ,CAAC,IAAI,EAAE,MAAM,EAAE,KAAK,CAAC,CAAC;YAC9D,KAAK,MAAM,CAAC,IAAI,EAAE,IAAI,CAAC,IAAI,MAAM,CAAC,OAAO,CAAC,QAAQ,CAAC,KAAK,IAAI,EAAE,CAAC,EAAE,CAAC;gBAChE,OAAO,CAAC,IAAI,CAAC,IAAI,EAAE,OAAO,IAAI,KAAK,QAAQ,CAAC,CAAC,CAAC,OAAO,CAAC,MAAM,CAAC,IAAI,CAAC,CAAC,CAAC,CAAC,IAAI,CAAC,CAAC;YAC7E,CAAC;YACD,MAAM,GAAG,OAAO,CAAC;YACjB,OAAO,OAAO,CAAC;QACjB,CAAC;KACF,CAAC;AACJ,CAAC","sourcesContent":["/**\n * `fakeSandbox()`: a sandbox in memory, for tests of the code that runs on one. It enforces the policy it is given, so\n * a test can see a denial, and it records every `exec`, but it confines no process and runs no program: it understands\n * a few commands, listed at `FAKE_COMMANDS`. It is portable, so a test can use it on every target.\n */\nimport { HarnessError } from \"../errors.js\";\nimport { SandboxDeniedError, SandboxUnavailableError } from \"../sandbox/errors.js\";\nimport type {\n ExecOptions,\n ExecResult,\n Sandbox,\n SandboxClient,\n SandboxDenial,\n SandboxPolicy,\n WorkspaceManifest,\n} from \"../sandbox/types.js\";\n\n/** The options of `fakeSandbox()`. */\nexport interface FakeSandboxOptions {\n /** Makes `provision()` throw `SandboxUnavailableError` with this reason, as a backend that can't start does. */\n readonly unavailable?: string;\n}\n\n/** One command the fake received, with what the caller passed. */\nexport interface RecordedExec {\n readonly command: readonly string[];\n readonly cwd: string | undefined;\n readonly env: Readonly<Record<string, string>>;\n readonly timeoutMs: number | undefined;\n}\n\n/** A fake backend, and what it has seen. */\nexport interface FakeSandboxClient extends SandboxClient {\n /** Every command run through any sandbox this client provisioned, in order. */\n readonly execs: readonly RecordedExec[];\n /** The files of the latest sandbox, by absolute path. */\n files(): ReadonlyMap<string, Uint8Array>;\n /** Makes `link` an absolute path that stands for `target` in the latest sandbox, as a symlink does. */\n symlink(link: string, target: string): void;\n}\n\n/**\n * The commands the fake runs, in the form `[name, ...arguments]`: `echo` prints its arguments, `env` prints the\n * environment it was given as `NAME=value` lines, `cat <path>` prints a file, `write <path> <text>` writes one,\n * `true` and `false` exit with 0 and 1, `sleep <seconds>` waits and honours the timeout and the signal, and\n * `net-probe <host>` reaches a host only if the policy allows it. Any other command exits with 127.\n */\nexport const FAKE_COMMANDS: readonly string[] = [\"echo\", \"env\", \"cat\", \"write\", \"true\", \"false\", \"sleep\", \"net-probe\"];\n\nconst ENCODER = new TextEncoder();\nconst DECODER = new TextDecoder();\n\nfunction normalize(path: string): string {\n const parts: string[] = [];\n for (const part of path.split(\"/\")) {\n if (part === \"\" || part === \".\") continue;\n if (part === \"..\") parts.pop();\n else parts.push(part);\n }\n return `/${parts.join(\"/\")}`;\n}\n\nconst within = (path: string, root: string): boolean => {\n const base = normalize(root);\n return base === \"/\" || path === base || path.startsWith(`${base}/`);\n};\n\nclass FakeSandbox implements Sandbox {\n private disposed = false;\n // What ends each command still running, so `dispose()` can reject them as a real backend does.\n private readonly running = new Set<() => void>();\n readonly store = new Map<string, Uint8Array>();\n readonly links = new Map<string, string>();\n private readonly root: string;\n private readonly policy: SandboxPolicy;\n private readonly log: RecordedExec[];\n\n constructor(root: string, policy: SandboxPolicy, log: RecordedExec[]) {\n this.root = normalize(root);\n this.policy = policy;\n this.log = log;\n }\n\n private live(): void {\n if (this.disposed) throw new HarnessError(\"sandbox_disposed\", \"the sandbox was disposed\");\n }\n\n /** The absolute path a name stands for: joined to the root, tidied of `.` and `..`, then followed through links. */\n resolve(path: string, base = this.root): string {\n const tidy = normalize(path.startsWith(\"/\") ? path : `${base}/${path}`);\n for (const [link, target] of this.links) {\n if (tidy === link || tidy.startsWith(`${link}/`)) return normalize(`${target}${tidy.slice(link.length)}`);\n }\n return tidy;\n }\n\n private readDenial(path: string): SandboxDenial | undefined {\n const roots = [...this.policy.readRoots, ...this.policy.writeRoots];\n if (roots.some((root) => within(path, root))) return undefined;\n return { kind: \"filesystem_read\", target: path, reason: \"the path is outside the read roots\" };\n }\n\n private writeDenial(path: string): SandboxDenial | undefined {\n if (this.policy.writeRoots.some((root) => within(path, root))) return undefined;\n return { kind: \"filesystem_write\", target: path, reason: \"the path is outside the write roots\" };\n }\n\n private networkDenial(host: string): SandboxDenial | undefined {\n const { allow, deny } = this.policy.network;\n if (allow.includes(host) && !deny.includes(host)) return undefined;\n return { kind: \"network\", target: host, reason: \"the host is not on the network allowlist\" };\n }\n\n async readFile(path: string): Promise<Uint8Array> {\n this.live();\n const target = this.resolve(path);\n const denial = this.readDenial(target);\n if (denial) throw new SandboxDeniedError(denial);\n const data = this.store.get(target);\n if (data === undefined) throw new HarnessError(\"sandbox_not_found\", `no such file: ${target}`);\n return data.slice();\n }\n\n async writeFile(path: string, data: Uint8Array): Promise<void> {\n this.live();\n const target = this.resolve(path);\n const denial = this.writeDenial(target);\n if (denial) throw new SandboxDeniedError(denial);\n this.store.set(target, data.slice());\n }\n\n /** Writes a file without a policy check, for the manifest's own files. */\n seed(path: string, data: Uint8Array): void {\n this.store.set(this.resolve(path), data.slice());\n }\n\n async exec(command: readonly string[], options: ExecOptions): Promise<ExecResult> {\n this.live();\n if (!Array.isArray(command) || command.length === 0) {\n throw new HarnessError(\"invalid_sandbox_options\", \"a sandbox command is a non-empty list of strings\");\n }\n options.signal.throwIfAborted();\n const cwd = this.resolve(options.cwd ?? \".\");\n const denial = this.readDenial(cwd);\n if (denial) return { denied: denial };\n this.log.push({ command: [...command], cwd: options.cwd, env: { ...options.env }, timeoutMs: options.timeoutMs });\n const [name = \"\", ...args] = command;\n return this.run(name, args, options);\n }\n\n private async run(name: string, args: string[], options: ExecOptions): Promise<ExecResult> {\n const ok = (stdout = \"\"): ExecResult => ({ exitCode: 0, stdout, stderr: \"\" });\n switch (name) {\n case \"echo\":\n return ok(`${args.join(\" \")}\\n`);\n case \"env\":\n return ok(\n Object.entries(options.env)\n .map(([key, value]) => `${key}=${value}\\n`)\n .join(\"\"),\n );\n case \"true\":\n return ok();\n case \"false\":\n return { exitCode: 1, stdout: \"\", stderr: \"\" };\n case \"cat\":\n return this.cat(args[0] ?? \"\");\n case \"write\":\n return this.writeCommand(args[0] ?? \"\", args.slice(1).join(\" \"));\n case \"net-probe\": {\n const host = args[0] ?? \"\";\n const denial = this.networkDenial(host);\n return denial ? { denied: denial } : ok(`reached ${host}\\n`);\n }\n case \"sleep\":\n return this.sleep(Number(args[0] ?? 0) * 1000, options);\n default:\n return { exitCode: 127, stdout: \"\", stderr: `command not found: ${name}\\n` };\n }\n }\n\n private cat(path: string): ExecResult {\n const target = this.resolve(path);\n const denial = this.readDenial(target);\n if (denial) return { denied: denial };\n const data = this.store.get(target);\n return data === undefined\n ? { exitCode: 1, stdout: \"\", stderr: `no such file: ${target}\\n` }\n : { exitCode: 0, stdout: DECODER.decode(data), stderr: \"\" };\n }\n\n private writeCommand(path: string, text: string): ExecResult {\n const target = this.resolve(path);\n const denial = this.writeDenial(target);\n if (denial) return { denied: denial };\n this.store.set(target, ENCODER.encode(text));\n return { exitCode: 0, stdout: \"\", stderr: \"\" };\n }\n\n private sleep(ms: number, options: ExecOptions): Promise<ExecResult> {\n return new Promise((resolve, reject) => {\n const done = (result: ExecResult) => {\n clearTimeout(timer);\n options.signal.removeEventListener(\"abort\", aborted);\n this.running.delete(disposed);\n resolve(result);\n };\n const aborted = () => {\n clearTimeout(timer);\n this.running.delete(disposed);\n reject(options.signal.reason);\n };\n const disposed = () => {\n clearTimeout(timer);\n options.signal.removeEventListener(\"abort\", aborted);\n reject(new HarnessError(\"sandbox_disposed\", \"the sandbox was disposed\"));\n };\n this.running.add(disposed);\n const stopAt = options.timeoutMs !== undefined && options.timeoutMs < ms ? options.timeoutMs : ms;\n const timer = setTimeout(() => {\n const timedOut = stopAt < ms;\n done({ exitCode: timedOut ? 124 : 0, stdout: \"\", stderr: timedOut ? `timed out after ${stopAt} ms\\n` : \"\" });\n }, stopAt);\n options.signal.addEventListener(\"abort\", aborted, { once: true });\n });\n }\n\n async dispose(): Promise<void> {\n this.disposed = true;\n for (const stop of [...this.running]) stop();\n this.running.clear();\n }\n}\n\n/**\n * Builds a fake backend with the tier `os` and the stops `mistakes` and `exfiltration`, because it enforces the\n * policy's file roots and network allowlist in memory. That says nothing about a real process: it runs none.\n */\nexport function fakeSandbox(options: FakeSandboxOptions = {}): FakeSandboxClient {\n const execs: RecordedExec[] = [];\n let latest: FakeSandbox | undefined;\n return {\n tier: \"os\",\n stops: [\"mistakes\", \"exfiltration\"],\n execs,\n files: () => latest?.store ?? new Map(),\n symlink(link: string, target: string) {\n latest?.links.set(normalize(link), normalize(target));\n },\n async provision(manifest: WorkspaceManifest, policy: SandboxPolicy) {\n if (options.unavailable !== undefined) throw new SandboxUnavailableError(\"fake\", options.unavailable);\n const sandbox = new FakeSandbox(manifest.root, policy, execs);\n for (const [path, data] of Object.entries(manifest.files ?? {})) {\n sandbox.seed(path, typeof data === \"string\" ? ENCODER.encode(data) : data);\n }\n latest = sandbox;\n return sandbox;\n },\n };\n}\n"]}
|
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* `@buildanagent/harness/testing`: test doubles and conformance suites for the harness's components. The in-memory
|
|
3
|
+
* sandbox, and the conformance suites of the sandbox, the session store, the scheduler and the key store, are here so
|
|
4
|
+
* far.
|
|
5
|
+
*/
|
|
6
|
+
export { FAKE_COMMANDS, type FakeSandboxClient, type FakeSandboxOptions, fakeSandbox, type RecordedExec, } from "./fake-sandbox.js";
|
|
7
|
+
export { type KeyStoreCase, type KeyStoreConformanceReport, type KeyStoreFactory, type KeyStoreUnderTest, keyStoreConformanceCases, runKeyStoreConformance, } from "./key-store-conformance.js";
|
|
8
|
+
export { runSandboxConformance, type SandboxCase, type SandboxConformanceOptions, type SandboxConformanceReport, type SandboxFactory, type SandboxUnderTest, sandboxConformanceCases, } from "./sandbox-conformance.js";
|
|
9
|
+
export { type DeliveredWake, runSchedulerConformance, type SchedulerCase, type SchedulerConformanceReport, type SchedulerFactory, type SchedulerUnderTest, schedulerConformanceCases, } from "./scheduler-conformance.js";
|
|
10
|
+
export { runStoreConformance, type StoreCase, type StoreConformanceReport, type StoreFactory, type StoreUnderTest, storeConformanceCases, } from "./store-conformance.js";
|
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* `@buildanagent/harness/testing`: test doubles and conformance suites for the harness's components. The in-memory
|
|
3
|
+
* sandbox, and the conformance suites of the sandbox, the session store, the scheduler and the key store, are here so
|
|
4
|
+
* far.
|
|
5
|
+
*/
|
|
6
|
+
export { FAKE_COMMANDS, fakeSandbox, } from "./fake-sandbox.js";
|
|
7
|
+
export { keyStoreConformanceCases, runKeyStoreConformance, } from "./key-store-conformance.js";
|
|
8
|
+
export { runSandboxConformance, sandboxConformanceCases, } from "./sandbox-conformance.js";
|
|
9
|
+
export { runSchedulerConformance, schedulerConformanceCases, } from "./scheduler-conformance.js";
|
|
10
|
+
export { runStoreConformance, storeConformanceCases, } from "./store-conformance.js";
|
|
11
|
+
//# sourceMappingURL=index.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.js","sourceRoot":"","sources":["../../src/testing/index.ts"],"names":[],"mappings":"AAAA;;;;GAIG;AACH,OAAO,EACL,aAAa,EAGb,WAAW,GAEZ,MAAM,mBAAmB,CAAC;AAC3B,OAAO,EAKL,wBAAwB,EACxB,sBAAsB,GACvB,MAAM,4BAA4B,CAAC;AACpC,OAAO,EACL,qBAAqB,EAMrB,uBAAuB,GACxB,MAAM,0BAA0B,CAAC;AAClC,OAAO,EAEL,uBAAuB,EAKvB,yBAAyB,GAC1B,MAAM,4BAA4B,CAAC;AACpC,OAAO,EACL,mBAAmB,EAKnB,qBAAqB,GACtB,MAAM,wBAAwB,CAAC","sourcesContent":["/**\n * `@buildanagent/harness/testing`: test doubles and conformance suites for the harness's components. The in-memory\n * sandbox, and the conformance suites of the sandbox, the session store, the scheduler and the key store, are here so\n * far.\n */\nexport {\n FAKE_COMMANDS,\n type FakeSandboxClient,\n type FakeSandboxOptions,\n fakeSandbox,\n type RecordedExec,\n} from \"./fake-sandbox.js\";\nexport {\n type KeyStoreCase,\n type KeyStoreConformanceReport,\n type KeyStoreFactory,\n type KeyStoreUnderTest,\n keyStoreConformanceCases,\n runKeyStoreConformance,\n} from \"./key-store-conformance.js\";\nexport {\n runSandboxConformance,\n type SandboxCase,\n type SandboxConformanceOptions,\n type SandboxConformanceReport,\n type SandboxFactory,\n type SandboxUnderTest,\n sandboxConformanceCases,\n} from \"./sandbox-conformance.js\";\nexport {\n type DeliveredWake,\n runSchedulerConformance,\n type SchedulerCase,\n type SchedulerConformanceReport,\n type SchedulerFactory,\n type SchedulerUnderTest,\n schedulerConformanceCases,\n} from \"./scheduler-conformance.js\";\nexport {\n runStoreConformance,\n type StoreCase,\n type StoreConformanceReport,\n type StoreFactory,\n type StoreUnderTest,\n storeConformanceCases,\n} from \"./store-conformance.js\";\n"]}
|
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* The key store conformance suite: what every `KeyStore` must do. `load()` returns a ring a signer can use, and the
|
|
3
|
+
* same ring each time until something changes it. `rotate()` and `prune()` are optional, because a read-only secret
|
|
4
|
+
* is a valid store (ADR 0035), so their cases skip with a reason when a store leaves them out. The cases import no
|
|
5
|
+
* test runner and no Node module, so any runner can call them, in workerd too. No message names a key's bytes.
|
|
6
|
+
*/
|
|
7
|
+
import { type KeyStore } from "@buildanagent/agent-safety";
|
|
8
|
+
import { type CaseReport, type RunnableCase } from "./run-cases.js";
|
|
9
|
+
/** What a factory returns: the store under test. */
|
|
10
|
+
export interface KeyStoreUnderTest {
|
|
11
|
+
readonly store: KeyStore;
|
|
12
|
+
}
|
|
13
|
+
/** Makes a fresh store for each case. */
|
|
14
|
+
export type KeyStoreFactory = () => KeyStoreUnderTest | Promise<KeyStoreUnderTest>;
|
|
15
|
+
/** One key store case. */
|
|
16
|
+
export type KeyStoreCase = RunnableCase;
|
|
17
|
+
/** What `runKeyStoreConformance()` reports. */
|
|
18
|
+
export type KeyStoreConformanceReport = CaseReport;
|
|
19
|
+
/** The cases, each run against a store from `factory`. */
|
|
20
|
+
export declare function keyStoreConformanceCases(factory: KeyStoreFactory): readonly KeyStoreCase[];
|
|
21
|
+
/** Runs every case and reports which passed, which skipped and which failed. */
|
|
22
|
+
export declare function runKeyStoreConformance(factory: KeyStoreFactory): Promise<KeyStoreConformanceReport>;
|
|
@@ -0,0 +1,97 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* The key store conformance suite: what every `KeyStore` must do. `load()` returns a ring a signer can use, and the
|
|
3
|
+
* same ring each time until something changes it. `rotate()` and `prune()` are optional, because a read-only secret
|
|
4
|
+
* is a valid store (ADR 0035), so their cases skip with a reason when a store leaves them out. The cases import no
|
|
5
|
+
* test runner and no Node module, so any runner can call them, in workerd too. No message names a key's bytes.
|
|
6
|
+
*/
|
|
7
|
+
import { hmacSigner } from "@buildanagent/agent-safety";
|
|
8
|
+
import { runCases } from "./run-cases.js";
|
|
9
|
+
function check(condition, message) {
|
|
10
|
+
if (!condition)
|
|
11
|
+
throw new Error(`KeyStore conformance: ${message}`);
|
|
12
|
+
}
|
|
13
|
+
const PAYLOAD = new TextEncoder().encode("key store conformance payload");
|
|
14
|
+
function sameBytes(a, b) {
|
|
15
|
+
if (a === undefined || b === undefined || a.length !== b.length)
|
|
16
|
+
return false;
|
|
17
|
+
return a.every((byte, index) => byte === b[index]);
|
|
18
|
+
}
|
|
19
|
+
function sameRing(a, b) {
|
|
20
|
+
const ids = Object.keys(a.keys).sort();
|
|
21
|
+
const other = Object.keys(b.keys).sort();
|
|
22
|
+
return (a.current === b.current &&
|
|
23
|
+
ids.length === other.length &&
|
|
24
|
+
ids.every((id, index) => id === other[index] && sameBytes(a.keys[id], b.keys[id])));
|
|
25
|
+
}
|
|
26
|
+
async function loadsUsableRing(store) {
|
|
27
|
+
const ring = await store.load();
|
|
28
|
+
check(typeof ring.current === "string", "the ring has no current key id");
|
|
29
|
+
check(ring.keys[ring.current] instanceof Uint8Array, "the current key id is not in the ring's keys");
|
|
30
|
+
return undefined;
|
|
31
|
+
}
|
|
32
|
+
async function loadsSameRing(store) {
|
|
33
|
+
check(sameRing(await store.load(), await store.load()), "two loads returned different rings");
|
|
34
|
+
return undefined;
|
|
35
|
+
}
|
|
36
|
+
async function signsAndVerifies(store) {
|
|
37
|
+
const signer = hmacSigner(await store.load());
|
|
38
|
+
const signature = await signer.sign(PAYLOAD);
|
|
39
|
+
check(await signer.verify(signer.keyId, PAYLOAD, signature), "a signature from the ring did not verify");
|
|
40
|
+
const reloaded = hmacSigner(await store.load());
|
|
41
|
+
check(await reloaded.verify(signer.keyId, PAYLOAD, signature), "a reloaded ring did not verify the signature");
|
|
42
|
+
check(!(await reloaded.verify(signer.keyId, new TextEncoder().encode("another payload"), signature)), "a signature verified for another payload");
|
|
43
|
+
return undefined;
|
|
44
|
+
}
|
|
45
|
+
async function rotates(store) {
|
|
46
|
+
if (store.rotate === undefined)
|
|
47
|
+
return { skipped: "the store is read-only: it has no rotate()" };
|
|
48
|
+
const before = await store.load();
|
|
49
|
+
const signature = await hmacSigner(before).sign(PAYLOAD);
|
|
50
|
+
const id = await store.rotate();
|
|
51
|
+
check(id !== before.current, "rotate returned the id of the key that was already current");
|
|
52
|
+
const after = await store.load();
|
|
53
|
+
check(after.current === id, "the rotated key is not current");
|
|
54
|
+
check(sameBytes(after.keys[before.current], before.keys[before.current]), "the old key left the ring");
|
|
55
|
+
check(await hmacSigner(after).verify(before.current, PAYLOAD, signature), "a signature under the old key did not verify after the rotation");
|
|
56
|
+
return undefined;
|
|
57
|
+
}
|
|
58
|
+
async function prunes(store) {
|
|
59
|
+
if (store.prune === undefined)
|
|
60
|
+
return { skipped: "the store is read-only: it has no prune()" };
|
|
61
|
+
// With rotate() the ring holds three keys: one in use, one free to remove, and the current one.
|
|
62
|
+
const inUse = new Set();
|
|
63
|
+
if (store.rotate !== undefined) {
|
|
64
|
+
inUse.add((await store.load()).current);
|
|
65
|
+
await store.rotate();
|
|
66
|
+
await store.rotate();
|
|
67
|
+
}
|
|
68
|
+
const before = await store.load();
|
|
69
|
+
const removed = await store.prune(inUse);
|
|
70
|
+
const after = await store.load();
|
|
71
|
+
check(after.current === before.current, "prune changed the current key");
|
|
72
|
+
check(after.keys[after.current] instanceof Uint8Array, "prune removed the current key");
|
|
73
|
+
check([...inUse].every((id) => sameBytes(after.keys[id], before.keys[id])), "prune removed a key that a pause still names");
|
|
74
|
+
const gone = Object.keys(before.keys)
|
|
75
|
+
.filter((id) => !(id in after.keys))
|
|
76
|
+
.sort();
|
|
77
|
+
check(removed.length === gone.length && [...removed].sort().every((id, index) => id === gone[index]), "prune's returned ids are not the ids it removed");
|
|
78
|
+
if (store.rotate !== undefined)
|
|
79
|
+
check(gone.length === 1, "prune did not remove the one key nothing names");
|
|
80
|
+
return undefined;
|
|
81
|
+
}
|
|
82
|
+
const CASES = [
|
|
83
|
+
["load returns a ring whose current key is in its keys", loadsUsableRing],
|
|
84
|
+
["load twice returns the same ring", loadsSameRing],
|
|
85
|
+
["the loaded ring signs and verifies through hmacSigner", signsAndVerifies],
|
|
86
|
+
["rotate adds a new current key and keeps the old one", rotates],
|
|
87
|
+
["prune keeps the current key and every key in use, and returns the ids it removed", prunes],
|
|
88
|
+
];
|
|
89
|
+
/** The cases, each run against a store from `factory`. */
|
|
90
|
+
export function keyStoreConformanceCases(factory) {
|
|
91
|
+
return CASES.map(([name, body]) => ({ name, run: async () => body((await factory()).store) }));
|
|
92
|
+
}
|
|
93
|
+
/** Runs every case and reports which passed, which skipped and which failed. */
|
|
94
|
+
export function runKeyStoreConformance(factory) {
|
|
95
|
+
return runCases(keyStoreConformanceCases(factory));
|
|
96
|
+
}
|
|
97
|
+
//# sourceMappingURL=key-store-conformance.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"key-store-conformance.js","sourceRoot":"","sources":["../../src/testing/key-store-conformance.ts"],"names":[],"mappings":"AAAA;;;;;GAKG;AACH,OAAO,EAAE,UAAU,EAA+B,MAAM,4BAA4B,CAAC;AACrF,OAAO,EAAsC,QAAQ,EAAE,MAAM,gBAAgB,CAAC;AAgB9E,SAAS,KAAK,CAAC,SAAkB,EAAE,OAAe;IAChD,IAAI,CAAC,SAAS;QAAE,MAAM,IAAI,KAAK,CAAC,yBAAyB,OAAO,EAAE,CAAC,CAAC;AACtE,CAAC;AAED,MAAM,OAAO,GAAG,IAAI,WAAW,EAAE,CAAC,MAAM,CAAC,+BAA+B,CAAC,CAAC;AAE1E,SAAS,SAAS,CAAC,CAAyB,EAAE,CAAyB;IACrE,IAAI,CAAC,KAAK,SAAS,IAAI,CAAC,KAAK,SAAS,IAAI,CAAC,CAAC,MAAM,KAAK,CAAC,CAAC,MAAM;QAAE,OAAO,KAAK,CAAC;IAC9E,OAAO,CAAC,CAAC,KAAK,CAAC,CAAC,IAAI,EAAE,KAAK,EAAE,EAAE,CAAC,IAAI,KAAK,CAAC,CAAC,KAAK,CAAC,CAAC,CAAC;AACrD,CAAC;AAED,SAAS,QAAQ,CAAC,CAAU,EAAE,CAAU;IACtC,MAAM,GAAG,GAAG,MAAM,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CAAC,CAAC,CAAC;AACrD,CAAC","sourcesContent":["/**\n * The key store conformance suite: what every `KeyStore` must do. `load()` returns a ring a signer can use, and the\n * same ring each time until something changes it. `rotate()` and `prune()` are optional, because a read-only secret\n * is a valid store (ADR 0035), so their cases skip with a reason when a store leaves them out. The cases import no\n * test runner and no Node module, so any runner can call them, in workerd too. No message names a key's bytes.\n */\nimport { hmacSigner, type KeyRing, type KeyStore } from \"@buildanagent/agent-safety\";\nimport { type CaseReport, type RunnableCase, runCases } from \"./run-cases.js\";\n\n/** What a factory returns: the store under test. */\nexport interface KeyStoreUnderTest {\n readonly store: KeyStore;\n}\n\n/** Makes a fresh store for each case. */\nexport type KeyStoreFactory = () => KeyStoreUnderTest | Promise<KeyStoreUnderTest>;\n\n/** One key store case. */\nexport type KeyStoreCase = RunnableCase;\n\n/** What `runKeyStoreConformance()` reports. */\nexport type KeyStoreConformanceReport = CaseReport;\n\nfunction check(condition: boolean, message: string): asserts condition {\n if (!condition) throw new Error(`KeyStore conformance: ${message}`);\n}\n\nconst PAYLOAD = new TextEncoder().encode(\"key store conformance payload\");\n\nfunction sameBytes(a: Uint8Array | undefined, b: Uint8Array | undefined): boolean {\n if (a === undefined || b === undefined || a.length !== b.length) return false;\n return a.every((byte, index) => byte === b[index]);\n}\n\nfunction sameRing(a: KeyRing, b: KeyRing): boolean {\n const ids = Object.keys(a.keys).sort();\n const other = Object.keys(b.keys).sort();\n return (\n a.current === b.current &&\n ids.length === other.length &&\n ids.every((id, index) => id === other[index] && sameBytes(a.keys[id], b.keys[id]))\n );\n}\n\ntype Outcome = { readonly skipped: string } | undefined;\n\nasync function loadsUsableRing(store: KeyStore): Promise<Outcome> {\n const ring = await store.load();\n check(typeof ring.current === \"string\", \"the ring has no current key id\");\n check(ring.keys[ring.current] instanceof Uint8Array, \"the current key id is not in the ring's keys\");\n return undefined;\n}\n\nasync function loadsSameRing(store: KeyStore): Promise<Outcome> {\n check(sameRing(await store.load(), await store.load()), \"two loads returned different rings\");\n return undefined;\n}\n\nasync function signsAndVerifies(store: KeyStore): Promise<Outcome> {\n const signer = hmacSigner(await store.load());\n const signature = await signer.sign(PAYLOAD);\n check(await signer.verify(signer.keyId, PAYLOAD, signature), \"a signature from the ring did not verify\");\n const reloaded = hmacSigner(await store.load());\n check(await reloaded.verify(signer.keyId, PAYLOAD, signature), \"a reloaded ring did not verify the signature\");\n check(\n !(await reloaded.verify(signer.keyId, new TextEncoder().encode(\"another payload\"), signature)),\n \"a signature verified for another payload\",\n );\n return undefined;\n}\n\nasync function rotates(store: KeyStore): Promise<Outcome> {\n if (store.rotate === undefined) return { skipped: \"the store is read-only: it has no rotate()\" };\n const before = await store.load();\n const signature = await hmacSigner(before).sign(PAYLOAD);\n const id = await store.rotate();\n check(id !== before.current, \"rotate returned the id of the key that was already current\");\n const after = await store.load();\n check(after.current === id, \"the rotated key is not current\");\n check(sameBytes(after.keys[before.current], before.keys[before.current]), \"the old key left the ring\");\n check(\n await hmacSigner(after).verify(before.current, PAYLOAD, signature),\n \"a signature under the old key did not verify after the rotation\",\n );\n return undefined;\n}\n\nasync function prunes(store: KeyStore): Promise<Outcome> {\n if (store.prune === undefined) return { skipped: \"the store is read-only: it has no prune()\" };\n // With rotate() the ring holds three keys: one in use, one free to remove, and the current one.\n const inUse = new Set<string>();\n if (store.rotate !== undefined) {\n inUse.add((await store.load()).current);\n await store.rotate();\n await store.rotate();\n }\n const before = await store.load();\n const removed = await store.prune(inUse);\n const after = await store.load();\n check(after.current === before.current, \"prune changed the current key\");\n check(after.keys[after.current] instanceof Uint8Array, \"prune removed the current key\");\n check(\n [...inUse].every((id) => sameBytes(after.keys[id], before.keys[id])),\n \"prune removed a key that a pause still names\",\n );\n const gone = Object.keys(before.keys)\n .filter((id) => !(id in after.keys))\n .sort();\n check(\n removed.length === gone.length && [...removed].sort().every((id, index) => id === gone[index]),\n \"prune's returned ids are not the ids it removed\",\n );\n if (store.rotate !== undefined) check(gone.length === 1, \"prune did not remove the one key nothing names\");\n return undefined;\n}\n\nconst CASES: readonly (readonly [string, (store: KeyStore) => Promise<Outcome>])[] = [\n [\"load returns a ring whose current key is in its keys\", loadsUsableRing],\n [\"load twice returns the same ring\", loadsSameRing],\n [\"the loaded ring signs and verifies through hmacSigner\", signsAndVerifies],\n [\"rotate adds a new current key and keeps the old one\", rotates],\n [\"prune keeps the current key and every key in use, and returns the ids it removed\", prunes],\n];\n\n/** The cases, each run against a store from `factory`. */\nexport function keyStoreConformanceCases(factory: KeyStoreFactory): readonly KeyStoreCase[] {\n return CASES.map(([name, body]) => ({ name, run: async () => body((await factory()).store) }));\n}\n\n/** Runs every case and reports which passed, which skipped and which failed. */\nexport function runKeyStoreConformance(factory: KeyStoreFactory): Promise<KeyStoreConformanceReport> {\n return runCases(keyStoreConformanceCases(factory));\n}\n"]}
|
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
/** The report a conformance suite returns: which cases passed, which skipped with a reason and which failed. */
|
|
2
|
+
export interface CaseReport {
|
|
3
|
+
readonly passed: readonly string[];
|
|
4
|
+
readonly skipped: readonly {
|
|
5
|
+
readonly name: string;
|
|
6
|
+
readonly reason: string;
|
|
7
|
+
}[];
|
|
8
|
+
readonly failed: readonly {
|
|
9
|
+
readonly name: string;
|
|
10
|
+
readonly error: string;
|
|
11
|
+
}[];
|
|
12
|
+
}
|
|
13
|
+
/** A case a runner can call: a name, and a check that resolves with `{ skipped }` when the case doesn't apply. */
|
|
14
|
+
export interface RunnableCase {
|
|
15
|
+
readonly name: string;
|
|
16
|
+
run(): Promise<{
|
|
17
|
+
readonly skipped: string;
|
|
18
|
+
} | undefined>;
|
|
19
|
+
}
|
|
20
|
+
/** Runs each case once and sorts the outcomes: a rejection fails the case, and `{ skipped }` skips it with its reason. */
|
|
21
|
+
export declare function runCases(cases: readonly RunnableCase[]): Promise<CaseReport>;
|
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
/** Runs each case once and sorts the outcomes: a rejection fails the case, and `{ skipped }` skips it with its reason. */
|
|
2
|
+
export async function runCases(cases) {
|
|
3
|
+
const passed = [];
|
|
4
|
+
const skipped = [];
|
|
5
|
+
const failed = [];
|
|
6
|
+
for (const conformance of cases) {
|
|
7
|
+
try {
|
|
8
|
+
const outcome = await conformance.run();
|
|
9
|
+
if (outcome === undefined)
|
|
10
|
+
passed.push(conformance.name);
|
|
11
|
+
else
|
|
12
|
+
skipped.push({ name: conformance.name, reason: outcome.skipped });
|
|
13
|
+
}
|
|
14
|
+
catch (error) {
|
|
15
|
+
failed.push({ name: conformance.name, error: error instanceof Error ? error.message : String(error) });
|
|
16
|
+
}
|
|
17
|
+
}
|
|
18
|
+
return { passed, skipped, failed };
|
|
19
|
+
}
|
|
20
|
+
//# sourceMappingURL=run-cases.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"run-cases.js","sourceRoot":"","sources":["../../src/testing/run-cases.ts"],"names":[],"mappings":"AAaA,0HAA0H;AAC1H,MAAM,CAAC,KAAK,UAAU,QAAQ,CAAC,KAA8B;IAC3D,MAAM,MAAM,GAAa,EAAE,CAAC;IAC5B,MAAM,OAAO,GAAuC,EAAE,CAAC;IACvD,MAAM,MAAM,GAAsC,EAAE,CAAC;IACrD,KAAK,MAAM,WAAW,IAAI,KAAK,EAAE,CAAC;QAChC,IAAI,CAAC;YACH,MAAM,OAAO,GAAG,MAAM,WAAW,CAAC,GAAG,EAAE,CAAC;YACxC,IAAI,OAAO,KAAK,SAAS;gBAAE,MAAM,CAAC,IAAI,CAAC,WAAW,CAAC,IAAI,CAAC,CAAC;;gBACpD,OAAO,CAAC,IAAI,CAAC,EAAE,IAAI,EAAE,WAAW,CAAC,IAAI,EAAE,MAAM,EAAE,OAAO,CAAC,OAAO,EAAE,CAAC,CAAC;QACzE,CAAC;QAAC,OAAO,KAAK,EAAE,CAAC;YACf,MAAM,CAAC,IAAI,CAAC,EAAE,IAAI,EAAE,WAAW,CAAC,IAAI,EAAE,KAAK,EAAE,KAAK,YAAY,KAAK,CAAC,CAAC,CAAC,KAAK,CAAC,OAAO,CAAC,CAAC,CAAC,MAAM,CAAC,KAAK,CAAC,EAAE,CAAC,CAAC;QACzG,CAAC;IACH,CAAC;IACD,OAAO,EAAE,MAAM,EAAE,OAAO,EAAE,MAAM,EAAE,CAAC;AACrC,CAAC","sourcesContent":["/** The report a conformance suite returns: which cases passed, which skipped with a reason and which failed. */\nexport interface CaseReport {\n readonly passed: readonly string[];\n readonly skipped: readonly { readonly name: string; readonly reason: string }[];\n readonly failed: readonly { readonly name: string; readonly error: string }[];\n}\n\n/** A case a runner can call: a name, and a check that resolves with `{ skipped }` when the case doesn't apply. */\nexport interface RunnableCase {\n readonly name: string;\n run(): Promise<{ readonly skipped: string } | undefined>;\n}\n\n/** Runs each case once and sorts the outcomes: a rejection fails the case, and `{ skipped }` skips it with its reason. */\nexport async function runCases(cases: readonly RunnableCase[]): Promise<CaseReport> {\n const passed: string[] = [];\n const skipped: { name: string; reason: string }[] = [];\n const failed: { name: string; error: string }[] = [];\n for (const conformance of cases) {\n try {\n const outcome = await conformance.run();\n if (outcome === undefined) passed.push(conformance.name);\n else skipped.push({ name: conformance.name, reason: outcome.skipped });\n } catch (error) {\n failed.push({ name: conformance.name, error: error instanceof Error ? error.message : String(error) });\n }\n }\n return { passed, skipped, failed };\n}\n"]}
|
|
@@ -0,0 +1,64 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* The sandbox conformance suite: what every `SandboxClient` must do, whatever its tier. The cases that need a backend to
|
|
3
|
+
* enforce something run only when its `stops` claim it, so a `none` backend passes the rest and an honest `srt` or
|
|
4
|
+
* Docker backend passes them all. A backend that can't start makes every case skip with its reason, never fail. The cases
|
|
5
|
+
* import no test runner and no Node module, so any runner can call them, in workerd too.
|
|
6
|
+
*/
|
|
7
|
+
import type { SandboxClient } from "../sandbox/types.js";
|
|
8
|
+
import { type CaseReport, type RunnableCase } from "./run-cases.js";
|
|
9
|
+
/** What a factory returns: the backend, and the absolute folder to use as the workspace root. */
|
|
10
|
+
export interface SandboxUnderTest {
|
|
11
|
+
readonly client: SandboxClient;
|
|
12
|
+
/** An absolute folder that exists, empty. A sibling named `<root>-outside` stands for a path the policy refuses. */
|
|
13
|
+
readonly root: string;
|
|
14
|
+
/**
|
|
15
|
+
* Makes `link` an absolute path inside the workspace that stands for `target`, as a symlink does, before or after the
|
|
16
|
+
* sandbox starts. Without it the symlink case skips, because a portable command to make one doesn't exist.
|
|
17
|
+
*/
|
|
18
|
+
readonly symlink?: (link: string, target: string) => void | Promise<void>;
|
|
19
|
+
}
|
|
20
|
+
/** Makes a fresh backend and workspace for each case. */
|
|
21
|
+
export type SandboxFactory = () => SandboxUnderTest | Promise<SandboxUnderTest>;
|
|
22
|
+
/** The options of the suite. */
|
|
23
|
+
export interface SandboxConformanceOptions {
|
|
24
|
+
/**
|
|
25
|
+
* The network case: `command(host)` is a command that exits 0 if it reaches `host`. The policy allows only `allowed`,
|
|
26
|
+
* so both hosts must accept a connection from the test, such as two loopback listeners the test owns: a host that never
|
|
27
|
+
* resolves would fail with no policy at all and prove nothing. The case checks that `allowed` is reached and `blocked`
|
|
28
|
+
* is not. Without it that case skips, because a portable command to open a connection doesn't exist.
|
|
29
|
+
*/
|
|
30
|
+
readonly network?: {
|
|
31
|
+
readonly allowed: string;
|
|
32
|
+
readonly blocked: string;
|
|
33
|
+
/** The command that exits 0 if it reaches `host`. */
|
|
34
|
+
readonly command: (host: string) => readonly string[];
|
|
35
|
+
};
|
|
36
|
+
/**
|
|
37
|
+
* Variables the backend adds to a command's environment on its own, such as the proxy variables an OS sandbox sets so a
|
|
38
|
+
* program finds its network proxy. The environment case accepts these besides the given ones, and still fails on any
|
|
39
|
+
* other variable and on any credential.
|
|
40
|
+
*/
|
|
41
|
+
readonly addedEnvironment?: readonly string[];
|
|
42
|
+
/**
|
|
43
|
+
* The case that checks an abort and a timeout end the command's whole tree. `command` starts a background process,
|
|
44
|
+
* writes its process id to the file `pid` in the workspace and then waits for it, and `isAlive` says whether a process
|
|
45
|
+
* is still running. Without it the case skips, because a portable command that forks doesn't exist.
|
|
46
|
+
*/
|
|
47
|
+
readonly processTree?: {
|
|
48
|
+
/** The command that starts a background process, writes its id to `pid` and waits for it. */
|
|
49
|
+
readonly command: readonly string[];
|
|
50
|
+
/** Whether the process with id `pid` is still running. */
|
|
51
|
+
readonly isAlive: (pid: number) => boolean | Promise<boolean>;
|
|
52
|
+
};
|
|
53
|
+
}
|
|
54
|
+
/** One case of the sandbox conformance suite, which a runner can register as a test of its own. */
|
|
55
|
+
export type SandboxCase = RunnableCase;
|
|
56
|
+
/** The report the sandbox conformance suite returns: which cases passed, which skipped with a reason and which failed. */
|
|
57
|
+
export type SandboxConformanceReport = CaseReport;
|
|
58
|
+
/** The cases, for a backend from `factory`. Run each one; a case that doesn't apply resolves with `{ skipped }`. */
|
|
59
|
+
export declare function sandboxConformanceCases(factory: SandboxFactory, options?: SandboxConformanceOptions): readonly SandboxCase[];
|
|
60
|
+
/**
|
|
61
|
+
* Runs every case against backends from `factory` and reports which passed, which skipped with a reason and which
|
|
62
|
+
* failed. A backend that reports itself unavailable skips; any other error fails the case.
|
|
63
|
+
*/
|
|
64
|
+
export declare function runSandboxConformance(factory: SandboxFactory, options?: SandboxConformanceOptions): Promise<SandboxConformanceReport>;
|