forked from mattpocock/sandcastle
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdocker.ts
More file actions
439 lines (411 loc) · 14.5 KB
/
docker.ts
File metadata and controls
439 lines (411 loc) · 14.5 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
/**
* Docker sandbox provider — wraps DockerLifecycle into a SandboxProvider.
*
* Usage:
* import { docker } from "sandcastle/sandboxes/docker";
* await run({ agent: claudeCode("claude-opus-4-7"), sandbox: docker() });
*/
import {
execFile,
execFileSync,
spawn,
type StdioOptions,
} from "node:child_process";
import { randomUUID } from "node:crypto";
import { createInterface } from "node:readline";
import { Effect } from "effect";
import { startContainer, removeContainer } from "../DockerLifecycle.js";
import {
createBindMountSandboxProvider,
type SandboxProvider,
type BindMountCreateOptions,
type BindMountSandboxHandle,
type ExecResult,
type InteractiveExecOptions,
} from "../SandboxProvider.js";
import type { MountConfig } from "../MountConfig.js";
import type { SelinuxLabel } from "../mountUtils.js";
import {
defaultImageName,
resolveUserMounts,
processFileMountParents,
} from "../mountUtils.js";
import { BoundedTail, MAX_TAIL_CHARS } from "../boundedTail.js";
import { registerShutdown } from "../shutdownRegistry.js";
export interface DockerOptions {
/** Docker image name (default: derived from repo directory name). */
readonly imageName?: string;
/**
* The UID of the `agent` user inside the container image (default: host UID via `process.getuid()`, or 1000).
*
* Must match the UID baked into the image at build time. Used as the `--user` flag value
* and checked against the image's configured UID in the pre-flight diagnostic.
*/
readonly containerUid?: number;
/**
* The GID of the `agent` user inside the container image (default: host GID via `process.getgid()`, or 1000).
*
* Must match the GID baked into the image at build time. Used as the `--user` flag value.
*/
readonly containerGid?: number;
/**
* SELinux volume label suffix applied to bind mounts.
*
* - `"z"` — shared label (default). No-op on non-SELinux systems.
* - `"Z"` — private label; only this container can access the mount.
* - `false` — disable labeling entirely.
*/
readonly selinuxLabel?: SelinuxLabel;
/**
* Additional host directories to bind-mount into the sandbox.
*
* Each entry specifies a `hostPath` (tilde-expanded) and `sandboxPath`.
* If `hostPath` does not exist, sandbox creation fails with a clear error.
*/
readonly mounts?: readonly MountConfig[];
/** Environment variables injected by this provider. Merged at launch time with env resolver and agent provider env. */
readonly env?: Record<string, string>;
/**
* Docker network(s) to attach the container to.
*
* - `"my-network"` → `--network my-network`
* - `["net1", "net2"]` → `--network net1 --network net2`
*
* When omitted, Docker's default bridge network is used.
*/
readonly network?: string | readonly string[];
/**
* Supplementary groups to add the container user to, via `--group-add`.
*
* Accepts group names or numeric GIDs:
*
* - `["docker"]` → `--group-add docker`
* - `[999]` → `--group-add 999`
* - `["docker", 999]` → `--group-add docker --group-add 999`
*
* Useful for granting access to a bind-mounted Docker socket (Docker-outside-of-Docker).
* When omitted, no `--group-add` flags are added.
*/
readonly groups?: readonly (string | number)[];
/**
* Host devices to expose to the container, via `--device`.
*
* Each entry is a full device spec in `host[:container[:permissions]]` form:
*
* - `["/dev/kvm"]` → `--device /dev/kvm`
* - `["/dev/sda:/dev/xvda:rwm"]` → `--device /dev/sda:/dev/xvda:rwm`
* - `["/dev/kvm", "/dev/fuse"]` → `--device /dev/kvm --device /dev/fuse`
*
* When omitted, no `--device` flags are added.
*/
readonly devices?: readonly string[];
/**
* Maximum number of characters of streamed `exec` output retained per stream
* (stdout and stderr) when an `onLine` callback is supplied (default: 64KiB).
*
* Output is delivered live to `onLine` regardless; this only bounds the tail
* returned in `ExecResult`, preventing a long-running agent's output from
* overflowing V8's max string length and crashing the run.
*/
readonly maxOutputTailChars?: number;
/**
* Limit the CPU resources available to the container, via `--cpus`.
*
* Maps directly to `docker run --cpus`. Accepts fractional values:
*
* - `2` → `--cpus 2` (at most 2 CPUs)
* - `1.5` → `--cpus 1.5` (at most 1.5 CPUs)
*
* When omitted, no `--cpus` flag is added and the container is unconstrained.
*/
readonly cpus?: number;
}
/**
* Create a Docker sandbox provider.
*
* The returned provider creates Docker containers with bind-mounts
* for the worktree and git directories.
*/
export const docker = (options?: DockerOptions): SandboxProvider => {
const configuredImageName = options?.imageName;
const selinuxLabel = options?.selinuxLabel ?? "z";
const maxOutputTailChars = options?.maxOutputTailChars ?? MAX_TAIL_CHARS;
const sandboxHomedir = "/home/agent";
const userMounts = options?.mounts
? resolveUserMounts(options.mounts, sandboxHomedir)
: [];
// Validate file mounts and collect parent dirs to create at container start.
// Throws at construction time if any file mount parent is outside sandboxHomedir.
const parentDirsToCreate = processFileMountParents(
userMounts,
sandboxHomedir,
);
return createBindMountSandboxProvider({
name: "docker",
env: options?.env,
sandboxHomedir,
create: async (
createOptions: BindMountCreateOptions,
): Promise<BindMountSandboxHandle> => {
const containerName = `sandcastle-${randomUUID()}`;
const worktreePath =
createOptions.mounts.find(
(m) => m.hostPath === createOptions.worktreePath,
)?.sandboxPath ?? "/home/agent/workspace";
// Build volume mount list (internal mounts + user-provided mounts)
const allMounts = [...createOptions.mounts, ...userMounts];
const volumeMounts = allMounts.map((m) => ({
hostPath: m.hostPath,
sandboxPath: m.sandboxPath,
readonly: m.readonly,
}));
// Resolve image name
const imageName =
configuredImageName ?? defaultImageName(createOptions.hostRepoPath);
const containerUid = options?.containerUid ?? process.getuid?.() ?? 1000;
const containerGid = options?.containerGid ?? process.getgid?.() ?? 1000;
// Pre-flight: verify image exists and UID matches
await checkImageUid(imageName, containerUid);
// Start container
await Effect.runPromise(
startContainer(
containerName,
imageName,
{
...createOptions.env,
HOME: "/home/agent",
},
{
volumeMounts,
workdir: worktreePath,
user: `${containerUid}:${containerGid}`,
network: options?.network,
groups: options?.groups,
devices: options?.devices,
cpus: options?.cpus,
selinuxLabel,
},
),
);
// Create parent directories for file mounts and chown to the container user
for (const dir of parentDirsToCreate) {
await new Promise<void>((resolve, reject) => {
execFile(
"docker",
[
"exec",
"--user",
"0:0",
containerName,
"sh",
"-c",
`mkdir -p "$1" && chown "$2" "$1"`,
"sh",
dir,
`${containerUid}:${containerGid}`,
],
(error) => {
if (error) {
reject(
new Error(
`Failed to create parent directory '${dir}' in container: ${error.message}`,
),
);
} else {
resolve();
}
},
);
});
}
// Register synchronous container cleanup via the shared shutdown registry
// so concurrent sandboxes share a single exit/SIGINT/SIGTERM listener
// instead of tripping Node's MaxListenersExceededWarning.
const removeContainerSync = () => {
try {
execFileSync("docker", ["rm", "-f", containerName], {
stdio: "ignore",
});
} catch {
/* best-effort */
}
};
const unregisterShutdown = registerShutdown(removeContainerSync);
const handle: BindMountSandboxHandle = {
worktreePath,
exec: (
command: string,
opts?: {
onLine?: (line: string) => void;
cwd?: string;
sudo?: boolean;
stdin?: string;
},
): Promise<ExecResult> => {
const effectiveCommand = opts?.sudo ? `sudo ${command}` : command;
const args = ["exec"];
if (opts?.stdin !== undefined) args.push("-i");
if (opts?.cwd) args.push("-w", opts.cwd);
args.push(containerName, "sh", "-c", effectiveCommand);
return new Promise((resolve, reject) => {
const proc = spawn("docker", args, {
stdio: [
opts?.stdin !== undefined ? "pipe" : "ignore",
"pipe",
"pipe",
],
});
if (opts?.stdin !== undefined) {
proc.stdin!.write(opts.stdin);
proc.stdin!.end();
}
proc.on("error", (error) => {
reject(new Error(`docker exec failed: ${error.message}`));
});
if (opts?.onLine) {
const onLine = opts.onLine;
const stdoutTail = new BoundedTail(maxOutputTailChars, "\n");
const stderrTail = new BoundedTail(maxOutputTailChars, "");
const rl = createInterface({ input: proc.stdout! });
rl.on("line", (line) => {
stdoutTail.push(line);
onLine(line);
});
proc.stderr!.on("data", (chunk: Buffer) => {
stderrTail.push(chunk.toString());
});
proc.on("close", (code) => {
resolve({
stdout: stdoutTail.toString(),
stderr: stderrTail.toString(),
exitCode: code ?? 0,
});
});
} else {
const stdoutChunks: string[] = [];
const stderrChunks: string[] = [];
proc.stdout!.on("data", (chunk: Buffer) => {
stdoutChunks.push(chunk.toString());
});
proc.stderr!.on("data", (chunk: Buffer) => {
stderrChunks.push(chunk.toString());
});
proc.on("close", (code) => {
resolve({
stdout: stdoutChunks.join(""),
stderr: stderrChunks.join(""),
exitCode: code ?? 0,
});
});
}
});
},
interactiveExec: (
args: string[],
opts: InteractiveExecOptions,
): Promise<{ exitCode: number }> => {
return new Promise((resolve, reject) => {
const dockerArgs = ["exec"];
// Allocate a pseudo-terminal when stdin looks like a TTY
if (
"isTTY" in opts.stdin &&
(opts.stdin as { isTTY?: boolean }).isTTY
) {
dockerArgs.push("-it");
} else {
dockerArgs.push("-i");
}
if (opts.cwd) dockerArgs.push("-w", opts.cwd);
dockerArgs.push(containerName, ...args);
const proc = spawn("docker", dockerArgs, {
stdio: [opts.stdin, opts.stdout, opts.stderr] as StdioOptions,
});
proc.on("error", (error: Error) => {
reject(new Error(`docker exec failed: ${error.message}`));
});
proc.on("close", (code: number | null) => {
resolve({ exitCode: code ?? 0 });
});
});
},
copyFileIn: (hostPath: string, sandboxPath: string): Promise<void> =>
new Promise((resolve, reject) => {
execFile(
"docker",
["cp", hostPath, `${containerName}:${sandboxPath}`],
(error) => {
if (error) {
reject(new Error(`docker cp (in) failed: ${error.message}`));
} else {
resolve();
}
},
);
}),
copyFileOut: (sandboxPath: string, hostPath: string): Promise<void> =>
new Promise((resolve, reject) => {
execFile(
"docker",
["cp", `${containerName}:${sandboxPath}`, hostPath],
(error) => {
if (error) {
reject(new Error(`docker cp (out) failed: ${error.message}`));
} else {
resolve();
}
},
);
}),
close: async (): Promise<void> => {
unregisterShutdown();
await Effect.runPromise(removeContainer(containerName));
},
};
return handle;
},
});
};
// Re-export for backwards compatibility
export { defaultImageName };
const checkImageUid = (imageName: string, expectedUid: number): Promise<void> =>
new Promise<void>((resolve, reject) => {
execFile(
"docker",
["image", "inspect", imageName, "--format", "{{.Config.User}}"],
(error, stdout) => {
if (error) {
reject(
new Error(
`Image '${imageName}' not found locally. Build it first with 'sandcastle docker build-image'.`,
),
);
return;
}
const imageUser = (stdout ?? "").toString().trim();
if (!imageUser) {
// No USER directive in image — skip check
resolve();
return;
}
const uidPart = imageUser.split(":")[0]!;
const imageUid = parseInt(uidPart, 10);
if (isNaN(imageUid)) {
// Non-numeric user (e.g. "agent") — can't compare, skip check
resolve();
return;
}
if (imageUid !== expectedUid) {
reject(
new Error(
`UID mismatch: image '${imageName}' was built with UID ${imageUid}, ` +
`but the expected UID is ${expectedUid}. ` +
`Rebuild the image with 'sandcastle docker build-image', ` +
`or pass containerUid: ${imageUid} to docker() to match the image.`,
),
);
} else {
resolve();
}
},
);
});