@clearideas/agent-runtime-sandbox 0.5.0 → 0.6.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.
@@ -0,0 +1,29 @@
1
+ import { type SandboxCommand, type SandboxFileInfo, type SandboxHandle, type SandboxInputFile, type SandboxProcessEvent, type SandboxProvider, type SandboxSpec } from "../contracts.js";
2
+ /**
3
+ * TEST-ONLY provider that runs commands as ordinary host processes inside a
4
+ * temporary directory, rewriting `/workspace` paths in arguments and
5
+ * environment and the runtime-owned workspace index to that directory.
6
+ * It provides no isolation whatsoever and
7
+ * exists so the run sandbox can be exercised end to end (including the real
8
+ * helper script) on hosts without bubblewrap or Docker. It is deliberately not
9
+ * exported from the package entry point.
10
+ */
11
+ export declare class HostProcessTestProvider implements SandboxProvider {
12
+ readonly name = "host-process-test";
13
+ readonly capabilities: {
14
+ readOnlyAttachments: boolean;
15
+ };
16
+ readonly roots: Map<string, string>;
17
+ readonly commands: SandboxCommand[];
18
+ readonly specs: SandboxSpec[];
19
+ terminated: string[];
20
+ create(spec: SandboxSpec): Promise<SandboxHandle>;
21
+ translate(handle: SandboxHandle, sandboxPath: string): string;
22
+ putFiles(handle: SandboxHandle, files: SandboxInputFile[]): Promise<void>;
23
+ execute(handle: SandboxHandle, command: SandboxCommand, options?: {
24
+ signal?: AbortSignal;
25
+ }): AsyncIterable<SandboxProcessEvent>;
26
+ listFiles(handle: SandboxHandle, directory: string): Promise<SandboxFileInfo[]>;
27
+ readFile(handle: SandboxHandle, filePath: string): Promise<Uint8Array>;
28
+ terminate(handle: SandboxHandle): Promise<void>;
29
+ }
@@ -0,0 +1,168 @@
1
+ import { spawn } from "node:child_process";
2
+ import { randomUUID } from "node:crypto";
3
+ import { chmod, lstat, mkdir, mkdtemp, readdir, readFile, rm, writeFile, } from "node:fs/promises";
4
+ import { tmpdir } from "node:os";
5
+ import path from "node:path";
6
+ import { assertSandboxPath, } from "../contracts.js";
7
+ import { SANDBOX_WORKSPACES_FILE } from "../policy.js";
8
+ /**
9
+ * TEST-ONLY provider that runs commands as ordinary host processes inside a
10
+ * temporary directory, rewriting `/workspace` paths in arguments and
11
+ * environment and the runtime-owned workspace index to that directory.
12
+ * It provides no isolation whatsoever and
13
+ * exists so the run sandbox can be exercised end to end (including the real
14
+ * helper script) on hosts without bubblewrap or Docker. It is deliberately not
15
+ * exported from the package entry point.
16
+ */
17
+ export class HostProcessTestProvider {
18
+ name = "host-process-test";
19
+ capabilities = { readOnlyAttachments: false };
20
+ roots = new Map();
21
+ commands = [];
22
+ specs = [];
23
+ terminated = [];
24
+ async create(spec) {
25
+ this.specs.push(spec);
26
+ const root = await mkdtemp(path.join(tmpdir(), "agent-runtime-hostproc-"));
27
+ const id = `host-${randomUUID()}`;
28
+ this.roots.set(id, root);
29
+ return {
30
+ id,
31
+ provider: this.name,
32
+ createdAt: new Date().toISOString(),
33
+ providerData: { root },
34
+ };
35
+ }
36
+ translate(handle, sandboxPath) {
37
+ const root = this.roots.get(handle.id);
38
+ if (!root)
39
+ throw new Error("Unknown sandbox handle.");
40
+ const normalized = assertSandboxPath(sandboxPath);
41
+ return path.join(root, ...normalized.slice("/workspace".length).split("/").filter(Boolean));
42
+ }
43
+ async putFiles(handle, files) {
44
+ for (const file of files) {
45
+ const target = this.translate(handle, file.path);
46
+ await mkdir(path.dirname(target), { recursive: true });
47
+ const existing = await lstat(target).catch(() => undefined);
48
+ if (existing)
49
+ await chmod(target, 0o600);
50
+ let content = file.content;
51
+ // This runtime-owned index must describe the same host paths as argv
52
+ // and environment. Never rewrite workspace files or arbitrary JSON.
53
+ if (file.path === SANDBOX_WORKSPACES_FILE) {
54
+ const index = JSON.parse(typeof content === "string"
55
+ ? content
56
+ : new TextDecoder().decode(content));
57
+ for (const mount of Object.values(index.mounts)) {
58
+ mount.path = this.translate(handle, mount.path);
59
+ }
60
+ index.output = this.translate(handle, index.output);
61
+ content = JSON.stringify(index);
62
+ }
63
+ await writeFile(target, content);
64
+ await chmod(target, file.mode ?? 0o600);
65
+ }
66
+ }
67
+ async *execute(handle, command, options) {
68
+ this.commands.push(command);
69
+ const root = this.roots.get(handle.id);
70
+ const rewrite = (value) => value.replace(/\/workspace(?=\/|$)/gu, root);
71
+ const controller = new AbortController();
72
+ const onAbort = () => controller.abort(options?.signal?.reason);
73
+ options?.signal?.addEventListener("abort", onAbort, { once: true });
74
+ const timeout = command.timeoutMs
75
+ ? setTimeout(() => controller.abort(new Error("timed out")), command.timeoutMs)
76
+ : undefined;
77
+ try {
78
+ const child = spawn(rewrite(command.command), (command.args ?? []).map(rewrite), {
79
+ cwd: rewrite(command.cwd ?? "/workspace"),
80
+ env: {
81
+ PATH: process.env.PATH ?? "",
82
+ ...Object.fromEntries(Object.entries(command.environment ?? {}).map(([key, value]) => [
83
+ key,
84
+ rewrite(value),
85
+ ])),
86
+ },
87
+ stdio: ["ignore", "pipe", "pipe"],
88
+ signal: controller.signal,
89
+ killSignal: "SIGKILL",
90
+ });
91
+ const events = [];
92
+ let wake;
93
+ let ended = false;
94
+ const append = (event) => {
95
+ events.push(event);
96
+ wake?.();
97
+ wake = undefined;
98
+ };
99
+ child.stdout.setEncoding("utf8");
100
+ child.stderr.setEncoding("utf8");
101
+ child.stdout.on("data", (data) => append({ type: "stdout", data }));
102
+ child.stderr.on("data", (data) => append({ type: "stderr", data }));
103
+ child.once("error", (error) => {
104
+ if (error.code !== "ABORT_ERR")
105
+ append({ type: "stderr", data: error.message });
106
+ });
107
+ child.once("exit", (code, signal) => {
108
+ append({
109
+ type: "exit",
110
+ exitCode: code ?? 137,
111
+ ...(signal ? { signal } : {}),
112
+ });
113
+ ended = true;
114
+ wake?.();
115
+ });
116
+ while (!ended || events.length > 0) {
117
+ const event = events.shift();
118
+ if (event)
119
+ yield event;
120
+ else
121
+ await new Promise((resolve) => (wake = resolve));
122
+ }
123
+ }
124
+ finally {
125
+ if (timeout)
126
+ clearTimeout(timeout);
127
+ options?.signal?.removeEventListener("abort", onAbort);
128
+ }
129
+ }
130
+ async listFiles(handle, directory) {
131
+ const files = [];
132
+ const pending = [assertSandboxPath(directory)];
133
+ while (pending.length > 0) {
134
+ const current = pending.shift();
135
+ let entries;
136
+ try {
137
+ entries = await readdir(this.translate(handle, current), {
138
+ withFileTypes: true,
139
+ });
140
+ }
141
+ catch {
142
+ continue;
143
+ }
144
+ for (const entry of entries.sort((a, b) => (a.name < b.name ? -1 : 1))) {
145
+ if (entry.isSymbolicLink())
146
+ continue;
147
+ const sandboxPath = `${current}/${entry.name}`;
148
+ if (entry.isDirectory())
149
+ pending.push(sandboxPath);
150
+ else if (entry.isFile()) {
151
+ const stats = await lstat(this.translate(handle, sandboxPath));
152
+ files.push({ path: sandboxPath, size: stats.size });
153
+ }
154
+ }
155
+ }
156
+ return files;
157
+ }
158
+ async readFile(handle, filePath) {
159
+ return readFile(this.translate(handle, filePath));
160
+ }
161
+ async terminate(handle) {
162
+ this.terminated.push(handle.id);
163
+ const root = this.roots.get(handle.id);
164
+ this.roots.delete(handle.id);
165
+ if (root)
166
+ await rm(root, { recursive: true, force: true });
167
+ }
168
+ }
@@ -0,0 +1 @@
1
+ export { HostProcessTestProvider } from "./host-process-provider.js";
@@ -0,0 +1 @@
1
+ export { HostProcessTestProvider } from "./host-process-provider.js";
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@clearideas/agent-runtime-sandbox",
3
- "version": "0.5.0",
3
+ "version": "0.6.1",
4
4
  "description": "Provider-neutral sandbox lifecycle and code execution adapters for Clear Ideas Agent Runtime.",
5
5
  "license": "Apache-2.0",
6
6
  "author": "Clear Ideas Incorporated",
@@ -16,6 +16,7 @@
16
16
  },
17
17
  "files": [
18
18
  "dist",
19
+ "assets",
19
20
  "NOTICE",
20
21
  "README.md"
21
22
  ],
@@ -26,6 +27,10 @@
26
27
  ".": {
27
28
  "types": "./dist/index.d.ts",
28
29
  "default": "./dist/index.js"
30
+ },
31
+ "./testing": {
32
+ "types": "./dist/testing/index.d.ts",
33
+ "default": "./dist/testing/index.js"
29
34
  }
30
35
  },
31
36
  "scripts": {
@@ -36,8 +41,8 @@
36
41
  "postpack": "node ../../scripts/cleanup-package-license.mjs"
37
42
  },
38
43
  "dependencies": {
39
- "@clearideas/agent-runtime-contracts": "0.5.0",
40
- "@clearideas/agent-runtime-core": "0.5.0"
44
+ "@clearideas/agent-runtime-contracts": "0.6.1",
45
+ "@clearideas/agent-runtime-core": "0.6.1"
41
46
  },
42
47
  "devDependencies": {
43
48
  "@types/node": "^26.4.0",