@agentswf/sandbox 0.1.0-beta.5 → 0.1.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +21 -0
- package/README.md +16 -0
- package/package.json +7 -3
- package/src/docker/args.ts +3 -0
- package/src/docker/index.ts +4 -3
- package/src/index.ts +1 -1
- package/src/resolve.ts +43 -18
- package/src/seam.ts +5 -2
- package/src/srt/index.ts +35 -13
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2026 the agents.wf authors
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
package/README.md
ADDED
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
# @agentswf/sandbox
|
|
2
|
+
|
|
3
|
+
The sandboxes a workflow can open for its agents, and their providers: docker and srt.
|
|
4
|
+
|
|
5
|
+
This package is internal to [agents.wf](https://agents.wf). It is published so that
|
|
6
|
+
[`agentswf`](https://www.npmjs.com/package/agentswf) can depend on it, and its exports change
|
|
7
|
+
without notice between versions. Install `agentswf` instead:
|
|
8
|
+
|
|
9
|
+
```sh
|
|
10
|
+
bun add -g agentswf
|
|
11
|
+
```
|
|
12
|
+
|
|
13
|
+
A workflow imports `agentswf/workflow`, its tests `agentswf/testing`, and a program that drives
|
|
14
|
+
runs `agentswf/client`. The [guide](https://agents.wf/guide/) starts there.
|
|
15
|
+
|
|
16
|
+
MIT licensed. Source: [github.com/romanlv/agentswf](https://github.com/romanlv/agentswf/tree/main/packages/sandbox).
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@agentswf/sandbox",
|
|
3
|
-
"version": "0.1.0
|
|
3
|
+
"version": "0.1.0",
|
|
4
4
|
"description": "agents.wf: the sandboxes a workflow opens, and their providers",
|
|
5
5
|
"license": "MIT",
|
|
6
6
|
"homepage": "https://agents.wf",
|
|
@@ -22,13 +22,17 @@
|
|
|
22
22
|
},
|
|
23
23
|
"files": [
|
|
24
24
|
"src",
|
|
25
|
+
"docker",
|
|
25
26
|
"!src/**/*.test.ts",
|
|
26
27
|
"!src/**/*.eval.ts",
|
|
28
|
+
"!src/**/*.typecheck.ts",
|
|
29
|
+
"!src/**/*.fixture.ts",
|
|
27
30
|
"!src/**/fixtures",
|
|
28
|
-
"
|
|
31
|
+
"README.md",
|
|
32
|
+
"LICENSE"
|
|
29
33
|
],
|
|
30
34
|
"dependencies": {
|
|
31
|
-
"@agentswf/contract": "0.1.0
|
|
35
|
+
"@agentswf/contract": "0.1.0"
|
|
32
36
|
},
|
|
33
37
|
"publishConfig": {
|
|
34
38
|
"access": "public"
|
package/src/docker/args.ts
CHANGED
|
@@ -48,10 +48,13 @@ export function execArgs(options: {
|
|
|
48
48
|
secrets: readonly string[];
|
|
49
49
|
pidFile: string;
|
|
50
50
|
argv: readonly string[];
|
|
51
|
+
/** A person's terminal, for a TUI: a tty in the box. */
|
|
52
|
+
terminal?: true;
|
|
51
53
|
}): string[] {
|
|
52
54
|
return [
|
|
53
55
|
"exec",
|
|
54
56
|
"-i",
|
|
57
|
+
...(options.terminal ? ["-t"] : []),
|
|
55
58
|
...(options.cwd ? ["-w", options.cwd] : []),
|
|
56
59
|
...Object.entries(options.env).flatMap(([name, value]) => ["-e", `${name}=${value}`]),
|
|
57
60
|
...options.secrets.flatMap((name) => ["-e", name]),
|
package/src/docker/index.ts
CHANGED
|
@@ -9,7 +9,7 @@ import { protectedPaths } from "../git";
|
|
|
9
9
|
import { KILL_GROUPS, RECORD_LEADER } from "../groups";
|
|
10
10
|
import { onceUnlessFailed } from "../once";
|
|
11
11
|
import { panePrelude } from "../pane";
|
|
12
|
-
import { contains } from "../resolve";
|
|
12
|
+
import { contains, deepestRoot } from "../resolve";
|
|
13
13
|
import type {
|
|
14
14
|
AgentContext,
|
|
15
15
|
Occupant,
|
|
@@ -562,6 +562,7 @@ async function admitAgent(
|
|
|
562
562
|
secrets: Object.keys(agent.harness.secrets),
|
|
563
563
|
pidFile,
|
|
564
564
|
argv: root.argv,
|
|
565
|
+
...(root.terminal ? { terminal: true as const } : {}),
|
|
565
566
|
}),
|
|
566
567
|
],
|
|
567
568
|
env: clientEnvironment,
|
|
@@ -841,7 +842,7 @@ function killIn(client: DockerClient, box: string, files: readonly string[], use
|
|
|
841
842
|
* or no root holds it.
|
|
842
843
|
*/
|
|
843
844
|
async function linkAt(path: string, roots: readonly string[]): Promise<string | undefined> {
|
|
844
|
-
const root = roots
|
|
845
|
+
const root = deepestRoot(roots, path);
|
|
845
846
|
if (root === undefined) return undefined;
|
|
846
847
|
let highest: string | undefined;
|
|
847
848
|
for (let at = path; at !== root && contains(root, at); at = dirname(at)) {
|
|
@@ -852,7 +853,7 @@ async function linkAt(path: string, roots: readonly string[]): Promise<string |
|
|
|
852
853
|
|
|
853
854
|
/** Which directories `path` is in below the deepest of `roots` holding it, by their identity. */
|
|
854
855
|
async function directoriesOf(path: string, roots: readonly string[]): Promise<string> {
|
|
855
|
-
const root = roots
|
|
856
|
+
const root = deepestRoot(roots, path);
|
|
856
857
|
const identities: string[] = [];
|
|
857
858
|
for (
|
|
858
859
|
let at = dirname(path);
|
package/src/index.ts
CHANGED
package/src/resolve.ts
CHANGED
|
@@ -1,9 +1,20 @@
|
|
|
1
1
|
import { readdir, readFile, realpath, stat } from "node:fs/promises";
|
|
2
2
|
import { homedir } from "node:os";
|
|
3
|
-
import { dirname, isAbsolute, join, resolve } from "node:path";
|
|
3
|
+
import { basename, dirname, isAbsolute, join, resolve } from "node:path";
|
|
4
4
|
import { isRecord } from "@agentswf/contract";
|
|
5
|
-
import {
|
|
6
|
-
|
|
5
|
+
import {
|
|
6
|
+
type Gitdir,
|
|
7
|
+
SANDBOX_ENVIRONMENTS,
|
|
8
|
+
type SandboxEnvironmentKey,
|
|
9
|
+
} from "@agentswf/contract/workflow";
|
|
10
|
+
import type { ResolvedSandbox, SandboxProviders } from "./seam";
|
|
11
|
+
|
|
12
|
+
/**
|
|
13
|
+
* What names a directory under the control root that is an agent's door or an srt agent's short
|
|
14
|
+
* directory: a sandbox path in one is refused. Distinct enough that a project in `/tmp`, the temp
|
|
15
|
+
* directory on Linux, never matches it.
|
|
16
|
+
*/
|
|
17
|
+
export const DOORS_PREFIX = "awf-doors-";
|
|
7
18
|
|
|
8
19
|
const REACH_KEYS = ["read", "write", "network", "sockets", "webSearch"] as const;
|
|
9
20
|
|
|
@@ -32,8 +43,8 @@ export type ResolveOptions = {
|
|
|
32
43
|
/** The operator's harness state, where its environment put it: no sandbox path may overlap it. */
|
|
33
44
|
harnessState: readonly string[];
|
|
34
45
|
/**
|
|
35
|
-
* Where the engine keeps every run's doors: `/tmp`, holding
|
|
36
|
-
* sandbox path may contain it, or lie in one of those directories: an agent could rewrite
|
|
46
|
+
* Where the engine keeps every run's doors: `/tmp`, holding a `DOORS_PREFIX` directory per run.
|
|
47
|
+
* No sandbox path may contain it, or lie in one of those directories: an agent could rewrite
|
|
37
48
|
* another agent's launcher, which runs on the host.
|
|
38
49
|
*/
|
|
39
50
|
controlRoot?: string;
|
|
@@ -85,18 +96,14 @@ export async function resolveSandbox(
|
|
|
85
96
|
|
|
86
97
|
const home = await realpath(options.home ?? homedir());
|
|
87
98
|
const runRoot = await realpath(options.runRoot);
|
|
88
|
-
const machine = await
|
|
99
|
+
const machine = await realpathMissing(options.machineRoot);
|
|
89
100
|
const controlRoot =
|
|
90
|
-
options.controlRoot === undefined
|
|
91
|
-
? undefined
|
|
92
|
-
: await realpath(options.controlRoot).catch(() => options.controlRoot);
|
|
101
|
+
options.controlRoot === undefined ? undefined : await realpathMissing(options.controlRoot);
|
|
93
102
|
const coordinatorRoot =
|
|
94
103
|
options.coordinatorRoot === undefined
|
|
95
104
|
? undefined
|
|
96
|
-
: await
|
|
97
|
-
const harnessState = await Promise.all(
|
|
98
|
-
options.harnessState.map((path) => realpath(path).catch(() => path)),
|
|
99
|
-
);
|
|
105
|
+
: await realpathMissing(options.coordinatorRoot);
|
|
106
|
+
const harnessState = await Promise.all(options.harnessState.map(realpathMissing));
|
|
100
107
|
const forbidden = (path: string, field: string) => {
|
|
101
108
|
if (contains(path, home)) throw new Error(`sandbox ${field} ${path} would expose ~`);
|
|
102
109
|
// A path within the run root would be another run's. One holding it is `hidden`.
|
|
@@ -219,17 +226,35 @@ export function writableIn(
|
|
|
219
226
|
reach: { read: readonly string[]; write: readonly string[] },
|
|
220
227
|
path: string,
|
|
221
228
|
): boolean {
|
|
222
|
-
const deepest = [...reach.read, ...reach.write]
|
|
223
|
-
.filter((root) => contains(root, path))
|
|
224
|
-
.sort((a, b) => b.length - a.length)[0];
|
|
229
|
+
const deepest = deepestRoot([...reach.read, ...reach.write], path);
|
|
225
230
|
return deepest !== undefined && reach.write.includes(deepest);
|
|
226
231
|
}
|
|
227
232
|
|
|
228
|
-
/**
|
|
233
|
+
/** The deepest of `roots` holding `path`, if any does. */
|
|
234
|
+
export function deepestRoot(roots: readonly string[], path: string): string | undefined {
|
|
235
|
+
return roots.filter((root) => contains(root, path)).sort((a, b) => b.length - a.length)[0];
|
|
236
|
+
}
|
|
237
|
+
|
|
238
|
+
/**
|
|
239
|
+
* `path` by its real path, though it may not exist yet: its deepest existing ancestor resolved,
|
|
240
|
+
* the rest kept. A root made after the check, under a linked directory such as macOS's `/var`,
|
|
241
|
+
* is then still compared by the path a sandbox's would be.
|
|
242
|
+
*/
|
|
243
|
+
async function realpathMissing(path: string): Promise<string> {
|
|
244
|
+
const rest: string[] = [];
|
|
245
|
+
for (let at = path; ; at = dirname(at)) {
|
|
246
|
+
const found = await realpath(at).catch(() => undefined);
|
|
247
|
+
if (found !== undefined) return join(found, ...rest.reverse());
|
|
248
|
+
if (at === dirname(at)) return path;
|
|
249
|
+
rest.push(basename(at));
|
|
250
|
+
}
|
|
251
|
+
}
|
|
252
|
+
|
|
253
|
+
/** Whether `path` lies in one of the engine's door directories under its control root. */
|
|
229
254
|
function insideDoors(controlRoot: string, path: string): boolean {
|
|
230
255
|
if (path === controlRoot || !contains(controlRoot, path)) return false;
|
|
231
256
|
const [first = ""] = path.slice(controlRoot.length + 1).split("/");
|
|
232
|
-
return first.startsWith(
|
|
257
|
+
return first.startsWith(DOORS_PREFIX);
|
|
233
258
|
}
|
|
234
259
|
|
|
235
260
|
/** Whether `path` is `root` or lies under it. Both are absolute and resolved. */
|
package/src/seam.ts
CHANGED
|
@@ -54,8 +54,6 @@ export type ResolvedSandbox<E> = {
|
|
|
54
54
|
environment: E;
|
|
55
55
|
};
|
|
56
56
|
|
|
57
|
-
export type { Gitdir };
|
|
58
|
-
|
|
59
57
|
export type SandboxContext = {
|
|
60
58
|
/** Every run's directory. Denied inside, and in the spec's `hidden` when an allowed path holds it. */
|
|
61
59
|
runRoot: string;
|
|
@@ -138,6 +136,11 @@ export type SandboxProcess = {
|
|
|
138
136
|
stdin?: string;
|
|
139
137
|
timeoutMs: number;
|
|
140
138
|
signal?: AbortSignal;
|
|
139
|
+
/**
|
|
140
|
+
* Run on the terminal of whoever runs the command, a person's, for a TUI (story 042): its stdio
|
|
141
|
+
* is that terminal, and a provider that cannot give it one throws.
|
|
142
|
+
*/
|
|
143
|
+
terminal?: true;
|
|
141
144
|
};
|
|
142
145
|
|
|
143
146
|
/**
|
package/src/srt/index.ts
CHANGED
|
@@ -19,7 +19,7 @@ import { protectedPaths } from "../git";
|
|
|
19
19
|
import { LaunchedGroups } from "../groups";
|
|
20
20
|
import { onceUnlessFailed } from "../once";
|
|
21
21
|
import { panePrelude } from "../pane";
|
|
22
|
-
import { contains, repositoryOf } from "../resolve";
|
|
22
|
+
import { contains, DOORS_PREFIX, repositoryOf } from "../resolve";
|
|
23
23
|
import type {
|
|
24
24
|
OpenedSandbox,
|
|
25
25
|
PaneTerminal,
|
|
@@ -49,6 +49,8 @@ export type SrtOptions = SrtHost & {
|
|
|
49
49
|
|
|
50
50
|
/** How long `srt --version` may take before srt is taken to be missing. */
|
|
51
51
|
const VERSION_MS = 10_000;
|
|
52
|
+
/** The longest a `setTimeout` waits: above it, the timer fires at once. */
|
|
53
|
+
const MAX_TIMER_MS = 2_147_483_647;
|
|
52
54
|
|
|
53
55
|
/** The system's own directories, last on every agent's `PATH`; srt needs `bash` there (H6). */
|
|
54
56
|
const SYSTEM_PATH = ["/usr/bin", "/bin", "/usr/sbin", "/sbin"];
|
|
@@ -118,7 +120,7 @@ function openSandbox(
|
|
|
118
120
|
}
|
|
119
121
|
// `mkdtemp` makes it 0700, by a name no co-tenant can guess ahead.
|
|
120
122
|
const short = agent.harness.shortDirectory
|
|
121
|
-
? await mkdtemp(join(SHORT_ROOT,
|
|
123
|
+
? await mkdtemp(join(SHORT_ROOT, DOORS_PREFIX))
|
|
122
124
|
: undefined;
|
|
123
125
|
if (short) shorts.push(short);
|
|
124
126
|
for (const shared of agent.harness.sharedWrites ?? []) await sharedDirectory(shared);
|
|
@@ -126,16 +128,30 @@ function openSandbox(
|
|
|
126
128
|
checkProfile(settings, options, context, spec);
|
|
127
129
|
const profile = join(profiles, `${randomUUID()}.json`);
|
|
128
130
|
await writeFile(profile, JSON.stringify(settings, null, 2), { mode: 0o600 });
|
|
131
|
+
// A person's TUI sets its terminal raw, which srt allows only with a pty: the same reach,
|
|
132
|
+
// as a pane's has (story 042, M10).
|
|
133
|
+
const terminalProfile = join(profiles, `${randomUUID()}-terminal.json`);
|
|
134
|
+
await writeFile(terminalProfile, JSON.stringify({ ...settings, allowPty: true }, null, 2), {
|
|
135
|
+
mode: 0o600,
|
|
136
|
+
});
|
|
129
137
|
// git refuses an unreadable `~/.gitconfig`; this one is empty (X4). Made anew, never through
|
|
130
|
-
// a link a co-tenant planted in the home
|
|
138
|
+
// a link a co-tenant planted in the home; one a home admitted again kept is left as it is
|
|
139
|
+
// when it is a file of its own (story 042).
|
|
131
140
|
const gitconfig = join(agent.home, "gitconfig");
|
|
132
|
-
|
|
133
|
-
await
|
|
134
|
-
|
|
135
|
-
|
|
136
|
-
|
|
137
|
-
|
|
138
|
-
|
|
141
|
+
try {
|
|
142
|
+
await (
|
|
143
|
+
await open(
|
|
144
|
+
gitconfig,
|
|
145
|
+
constants.O_CREAT | constants.O_EXCL | constants.O_NOFOLLOW | constants.O_WRONLY,
|
|
146
|
+
0o600,
|
|
147
|
+
)
|
|
148
|
+
).close();
|
|
149
|
+
} catch (error) {
|
|
150
|
+
if ((error as NodeJS.ErrnoException).code !== "EEXIST") throw error;
|
|
151
|
+
const kept = await lstat(gitconfig);
|
|
152
|
+
if (!kept.isFile() || kept.nlink !== 1)
|
|
153
|
+
throw new Error(`srt: ${gitconfig} is not a file of its own`);
|
|
154
|
+
}
|
|
139
155
|
// The agent's whole environment: nothing of the engine's reaches it (X20).
|
|
140
156
|
const environment: Record<string, string> = {
|
|
141
157
|
HOME: options.home,
|
|
@@ -164,7 +180,8 @@ function openSandbox(
|
|
|
164
180
|
// `~/.local/bin` and its links are denied, so the harness runs by its real path; `--`
|
|
165
181
|
// keeps srt from reading the harness's flags as its own (Task 0).
|
|
166
182
|
const command = first === agent.harness.command ? agent.harness.executable : first!;
|
|
167
|
-
const
|
|
183
|
+
const chosen = root.terminal ? terminalProfile : profile;
|
|
184
|
+
const wrapped = groups.wrap([...options.command, "-s", chosen, "--", command, ...rest]);
|
|
168
185
|
return {
|
|
169
186
|
...root,
|
|
170
187
|
argv: wrapped.argv,
|
|
@@ -289,8 +306,7 @@ async function probe(options: SrtOptions, context: SandboxContext): Promise<void
|
|
|
289
306
|
// It ended meanwhile.
|
|
290
307
|
}
|
|
291
308
|
},
|
|
292
|
-
|
|
293
|
-
Math.min(Math.max(0, context.deadline.unixMilliseconds - Date.now()), 2_147_483_647),
|
|
309
|
+
Math.min(Math.max(0, context.deadline.unixMilliseconds - Date.now()), MAX_TIMER_MS),
|
|
294
310
|
);
|
|
295
311
|
const [output, stderr] = await Promise.all([
|
|
296
312
|
new Response(child.stdout).text(),
|
|
@@ -322,7 +338,13 @@ async function probe(options: SrtOptions, context: SandboxContext): Promise<void
|
|
|
322
338
|
export async function findSrt(
|
|
323
339
|
environment: Readonly<Record<string, string | undefined>>,
|
|
324
340
|
harnessState: readonly string[],
|
|
341
|
+
platform: NodeJS.Platform = process.platform,
|
|
325
342
|
): Promise<SrtOptions | undefined> {
|
|
343
|
+
// On Linux srt 0.0.81 loses a write nested in a read, git, the network with `/tmp` denied, and
|
|
344
|
+
// every Unix socket, the door's among them (docs/findings/sandbox-providers.md, X24).
|
|
345
|
+
if (platform !== "darwin") {
|
|
346
|
+
throw new Error('srt is macOS-only in awf for now; on Linux name "docker": {}');
|
|
347
|
+
}
|
|
326
348
|
const home = await realpath(environment.HOME ?? homedir());
|
|
327
349
|
const state = await Promise.all(harnessState.map((root) => realpath(root).catch(() => root)));
|
|
328
350
|
const path = (environment.PATH ?? "").split(":").filter(Boolean);
|