@capybara-hq/device 0.0.0-stage → 0.1.0-rc.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +202 -0
- package/NOTICE +2 -0
- package/README.md +103 -2
- package/bin/capybara-device.mjs +11 -0
- package/dist/api.d.ts +33 -0
- package/dist/api.js +48 -0
- package/dist/args.d.ts +39 -0
- package/dist/args.js +93 -0
- package/dist/cli.d.ts +18 -0
- package/dist/cli.js +93 -0
- package/dist/client.d.ts +39 -0
- package/dist/client.js +340 -0
- package/dist/command-log.d.ts +17 -0
- package/dist/command-log.js +28 -0
- package/dist/commands/drain.d.ts +2 -0
- package/dist/commands/drain.js +5 -0
- package/dist/commands/login.d.ts +20 -0
- package/dist/commands/login.js +132 -0
- package/dist/commands/logout.d.ts +13 -0
- package/dist/commands/logout.js +36 -0
- package/dist/commands/policy.d.ts +15 -0
- package/dist/commands/policy.js +74 -0
- package/dist/commands/service.d.ts +17 -0
- package/dist/commands/service.js +97 -0
- package/dist/commands/signal.d.ts +2 -0
- package/dist/commands/signal.js +18 -0
- package/dist/commands/start.d.ts +16 -0
- package/dist/commands/start.js +73 -0
- package/dist/commands/status.d.ts +2 -0
- package/dist/commands/status.js +32 -0
- package/dist/commands/stop.d.ts +2 -0
- package/dist/commands/stop.js +5 -0
- package/dist/exec/docker.d.ts +120 -0
- package/dist/exec/docker.js +673 -0
- package/dist/exec/fs-helper.d.ts +12 -0
- package/dist/exec/fs-helper.js +85 -0
- package/dist/exec/os.d.ts +80 -0
- package/dist/exec/os.js +398 -0
- package/dist/exec/procs.d.ts +11 -0
- package/dist/exec/procs.js +67 -0
- package/dist/exec/shared.d.ts +71 -0
- package/dist/exec/shared.js +240 -0
- package/dist/exec/types.d.ts +34 -0
- package/dist/exec/types.js +9 -0
- package/dist/index.d.ts +3 -0
- package/dist/index.js +3 -0
- package/dist/pid.d.ts +6 -0
- package/dist/pid.js +36 -0
- package/dist/policy.d.ts +42 -0
- package/dist/policy.js +97 -0
- package/dist/server-ops.d.ts +10 -0
- package/dist/server-ops.js +74 -0
- package/dist/service/launchd.d.ts +17 -0
- package/dist/service/launchd.js +59 -0
- package/dist/service/systemd.d.ts +5 -0
- package/dist/service/systemd.js +28 -0
- package/dist/state.d.ts +40 -0
- package/dist/state.js +117 -0
- package/dist/version.d.ts +2 -0
- package/dist/version.js +11 -0
- package/package.json +42 -3
|
@@ -0,0 +1,74 @@
|
|
|
1
|
+
import { validateAllowHost } from "@capybara-hq/plugin-sdk";
|
|
2
|
+
import { policyPath, readPolicy, writePolicy } from "../state.js";
|
|
3
|
+
const KEYS = ["allowedDomains", "allowCredentials", "cpu", "memoryMb"];
|
|
4
|
+
/** What the OS mode does not enforce yet, shown with every `show` and `set`. */
|
|
5
|
+
export const NOT_ENFORCED_NOTE = "Note: in OS mode cpu and memoryMb are not enforced yet, and allowCredentials is not used: " +
|
|
6
|
+
"commands get no local credentials.";
|
|
7
|
+
function list(value) {
|
|
8
|
+
return value
|
|
9
|
+
.split(",")
|
|
10
|
+
.map((s) => s.trim())
|
|
11
|
+
.filter((s) => s !== "");
|
|
12
|
+
}
|
|
13
|
+
function ceiling(key, value) {
|
|
14
|
+
if (value === "none" || value === "null")
|
|
15
|
+
return null;
|
|
16
|
+
const n = Number(value);
|
|
17
|
+
if (!Number.isFinite(n) || n <= 0) {
|
|
18
|
+
throw new Error(`${key} must be a positive number or none, got "${value}"`);
|
|
19
|
+
}
|
|
20
|
+
return n;
|
|
21
|
+
}
|
|
22
|
+
/**
|
|
23
|
+
* `policy show` prints the local policy; `policy set <key> <value>` changes one field. Only the
|
|
24
|
+
* machine's user edits this file: the cloud never writes it. Changes apply at the next start.
|
|
25
|
+
*/
|
|
26
|
+
export function policyCommand(cmd, deps) {
|
|
27
|
+
const { stateDir, out, err } = deps;
|
|
28
|
+
let policy;
|
|
29
|
+
try {
|
|
30
|
+
policy = readPolicy(stateDir);
|
|
31
|
+
}
|
|
32
|
+
catch (e) {
|
|
33
|
+
err(`Cannot read ${policyPath(stateDir)}: ${e.message}`);
|
|
34
|
+
return 1;
|
|
35
|
+
}
|
|
36
|
+
if (cmd.action === "show") {
|
|
37
|
+
out(JSON.stringify(policy, null, 2));
|
|
38
|
+
out(NOT_ENFORCED_NOTE);
|
|
39
|
+
return 0;
|
|
40
|
+
}
|
|
41
|
+
const { key, value } = cmd;
|
|
42
|
+
const next = { ...policy };
|
|
43
|
+
try {
|
|
44
|
+
switch (key) {
|
|
45
|
+
case "allowedDomains": {
|
|
46
|
+
const domains = list(value).map((d) => d.toLowerCase());
|
|
47
|
+
for (const d of domains) {
|
|
48
|
+
const errors = validateAllowHost(d);
|
|
49
|
+
if (errors.length > 0)
|
|
50
|
+
throw new Error(`"${d}": ${errors.join("; ")}`);
|
|
51
|
+
}
|
|
52
|
+
next.allowedDomains = [...new Set(domains)];
|
|
53
|
+
break;
|
|
54
|
+
}
|
|
55
|
+
case "allowCredentials":
|
|
56
|
+
next.allowCredentials = [...new Set(list(value))];
|
|
57
|
+
break;
|
|
58
|
+
case "cpu":
|
|
59
|
+
case "memoryMb":
|
|
60
|
+
next[key] = ceiling(key, value);
|
|
61
|
+
break;
|
|
62
|
+
default:
|
|
63
|
+
throw new Error(`unknown policy key "${key}"; one of ${KEYS.join(", ")}`);
|
|
64
|
+
}
|
|
65
|
+
}
|
|
66
|
+
catch (e) {
|
|
67
|
+
err(e.message);
|
|
68
|
+
return 2;
|
|
69
|
+
}
|
|
70
|
+
writePolicy(stateDir, next);
|
|
71
|
+
out(`Saved ${policyPath(stateDir)}. Restart capybara-device for it to take effect.`);
|
|
72
|
+
out(NOT_ENFORCED_NOTE);
|
|
73
|
+
return 0;
|
|
74
|
+
}
|
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
export interface ServiceDeps {
|
|
2
|
+
stateDir: string;
|
|
3
|
+
home: string;
|
|
4
|
+
platform: NodeJS.Platform;
|
|
5
|
+
env: Record<string, string | undefined>;
|
|
6
|
+
out: (line: string) => void;
|
|
7
|
+
err: (line: string) => void;
|
|
8
|
+
/** Runs a system command; throws when it fails (default: execFileSync). */
|
|
9
|
+
run?: (cmd: string, args: string[]) => void;
|
|
10
|
+
uid?: number;
|
|
11
|
+
nodePath?: string;
|
|
12
|
+
binPath?: string;
|
|
13
|
+
}
|
|
14
|
+
/** What the service needs from the installing shell: PATH (docker lives there) and the state dir. */
|
|
15
|
+
export declare function serviceEnv(env: Record<string, string | undefined>): Record<string, string>;
|
|
16
|
+
/** `service install|uninstall`: runs `capybara-device start` at login (launchd) or boot (systemd user). */
|
|
17
|
+
export declare function serviceCommand(action: "install" | "uninstall", deps: ServiceDeps): number;
|
|
@@ -0,0 +1,97 @@
|
|
|
1
|
+
import { execFileSync } from "node:child_process";
|
|
2
|
+
import { existsSync, mkdirSync, rmSync, writeFileSync } from "node:fs";
|
|
3
|
+
import { dirname } from "node:path";
|
|
4
|
+
import { fileURLToPath } from "node:url";
|
|
5
|
+
import { LAUNCHD_LABEL, plistContent, plistPath } from "../service/launchd.js";
|
|
6
|
+
import { SYSTEMD_UNIT, unitContent, unitPath } from "../service/systemd.js";
|
|
7
|
+
function defaultRun(cmd, args) {
|
|
8
|
+
execFileSync(cmd, args, { stdio: "pipe" });
|
|
9
|
+
}
|
|
10
|
+
/** The bin script next to this package's `dist` (or `src`) directory. */
|
|
11
|
+
function defaultBinPath() {
|
|
12
|
+
return fileURLToPath(new URL("../../bin/capybara-device.mjs", import.meta.url));
|
|
13
|
+
}
|
|
14
|
+
/** What the service needs from the installing shell: PATH (docker lives there) and the state dir. */
|
|
15
|
+
export function serviceEnv(env) {
|
|
16
|
+
const out = {};
|
|
17
|
+
const path = env.PATH;
|
|
18
|
+
out.PATH = path !== undefined && path !== "" ? path : "/usr/local/bin:/usr/bin:/bin";
|
|
19
|
+
const xdg = env.XDG_CONFIG_HOME;
|
|
20
|
+
if (xdg !== undefined && xdg !== "")
|
|
21
|
+
out.XDG_CONFIG_HOME = xdg;
|
|
22
|
+
return out;
|
|
23
|
+
}
|
|
24
|
+
/** `service install|uninstall`: runs `capybara-device start` at login (launchd) or boot (systemd user). */
|
|
25
|
+
export function serviceCommand(action, deps) {
|
|
26
|
+
const run = deps.run ?? defaultRun;
|
|
27
|
+
const spec = {
|
|
28
|
+
nodePath: deps.nodePath ?? process.execPath,
|
|
29
|
+
binPath: deps.binPath ?? defaultBinPath(),
|
|
30
|
+
stateDir: deps.stateDir,
|
|
31
|
+
env: serviceEnv(deps.env),
|
|
32
|
+
};
|
|
33
|
+
try {
|
|
34
|
+
if (deps.platform === "darwin")
|
|
35
|
+
return launchd(action, spec, deps, run);
|
|
36
|
+
if (deps.platform === "linux")
|
|
37
|
+
return systemd(action, spec, deps, run);
|
|
38
|
+
}
|
|
39
|
+
catch (e) {
|
|
40
|
+
deps.err(`capybara-device service ${action} failed: ${e instanceof Error ? e.message : String(e)}`);
|
|
41
|
+
return 1;
|
|
42
|
+
}
|
|
43
|
+
deps.err("capybara-device service needs macOS (launchd) or Linux (systemd).");
|
|
44
|
+
return 1;
|
|
45
|
+
}
|
|
46
|
+
function launchd(action, spec, deps, run) {
|
|
47
|
+
const file = plistPath(deps.home);
|
|
48
|
+
const domain = `gui/${deps.uid ?? process.getuid?.() ?? 501}`;
|
|
49
|
+
const tryRun = (args) => {
|
|
50
|
+
try {
|
|
51
|
+
run("launchctl", args);
|
|
52
|
+
}
|
|
53
|
+
catch {
|
|
54
|
+
// Not loaded yet / already gone.
|
|
55
|
+
}
|
|
56
|
+
};
|
|
57
|
+
if (action === "install") {
|
|
58
|
+
mkdirSync(dirname(file), { recursive: true });
|
|
59
|
+
mkdirSync(spec.stateDir, { recursive: true });
|
|
60
|
+
if (existsSync(file))
|
|
61
|
+
tryRun(["bootout", `${domain}/${LAUNCHD_LABEL}`]);
|
|
62
|
+
writeFileSync(file, plistContent(spec));
|
|
63
|
+
run("launchctl", ["bootstrap", domain, file]);
|
|
64
|
+
deps.out(`Installed ${file}; capybara-device now starts at login.`);
|
|
65
|
+
return 0;
|
|
66
|
+
}
|
|
67
|
+
tryRun(["bootout", `${domain}/${LAUNCHD_LABEL}`]);
|
|
68
|
+
rmSync(file, { force: true });
|
|
69
|
+
deps.out("Removed the capybara-device login service.");
|
|
70
|
+
return 0;
|
|
71
|
+
}
|
|
72
|
+
function systemd(action, spec, deps, run) {
|
|
73
|
+
const file = unitPath(deps.home);
|
|
74
|
+
if (action === "install") {
|
|
75
|
+
mkdirSync(dirname(file), { recursive: true });
|
|
76
|
+
writeFileSync(file, unitContent(spec));
|
|
77
|
+
run("systemctl", ["--user", "daemon-reload"]);
|
|
78
|
+
run("systemctl", ["--user", "enable", "--now", SYSTEMD_UNIT]);
|
|
79
|
+
deps.out(`Installed ${file}; capybara-device now starts at login. A user unit runs only while you have a session unless you run: loginctl enable-linger $USER`);
|
|
80
|
+
return 0;
|
|
81
|
+
}
|
|
82
|
+
try {
|
|
83
|
+
run("systemctl", ["--user", "disable", "--now", SYSTEMD_UNIT]);
|
|
84
|
+
}
|
|
85
|
+
catch {
|
|
86
|
+
// Not installed.
|
|
87
|
+
}
|
|
88
|
+
rmSync(file, { force: true });
|
|
89
|
+
try {
|
|
90
|
+
run("systemctl", ["--user", "daemon-reload"]);
|
|
91
|
+
}
|
|
92
|
+
catch {
|
|
93
|
+
// No user manager: nothing is loaded.
|
|
94
|
+
}
|
|
95
|
+
deps.out("Removed the capybara-device user service.");
|
|
96
|
+
return 0;
|
|
97
|
+
}
|
|
@@ -0,0 +1,2 @@
|
|
|
1
|
+
/** Sends `signal` to the running program; exit code 0 when it was signalled, 1 when none runs. */
|
|
2
|
+
export declare function signalRunning(stateDir: string, signal: NodeJS.Signals, message: string, out: (line: string) => void, err: (line: string) => void, kill?: typeof process.kill): number;
|
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
import { readRunningPid } from "../pid.js";
|
|
2
|
+
/** Sends `signal` to the running program; exit code 0 when it was signalled, 1 when none runs. */
|
|
3
|
+
export function signalRunning(stateDir, signal, message, out, err, kill = process.kill) {
|
|
4
|
+
const pid = readRunningPid(stateDir, kill);
|
|
5
|
+
if (pid === undefined) {
|
|
6
|
+
err("capybara-device is not running.");
|
|
7
|
+
return 1;
|
|
8
|
+
}
|
|
9
|
+
try {
|
|
10
|
+
kill(pid, signal);
|
|
11
|
+
}
|
|
12
|
+
catch (e) {
|
|
13
|
+
err(`Could not signal pid ${pid}: ${e.message}`);
|
|
14
|
+
return 1;
|
|
15
|
+
}
|
|
16
|
+
out(message);
|
|
17
|
+
return 0;
|
|
18
|
+
}
|
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
import { connect } from "../client.js";
|
|
2
|
+
import type { Executor } from "../exec/types.js";
|
|
3
|
+
import { type Credential, type Policy } from "../state.js";
|
|
4
|
+
export interface StartDeps {
|
|
5
|
+
stateDir: string;
|
|
6
|
+
/** Builds the executor for the credential's mode (the OS and Docker executors). */
|
|
7
|
+
createExecutor?: (cred: Credential, policy: Policy) => Promise<Executor> | Executor;
|
|
8
|
+
out: (line: string) => void;
|
|
9
|
+
err: (line: string) => void;
|
|
10
|
+
/** The process whose signals stop and drain the program. */
|
|
11
|
+
proc?: Pick<NodeJS.Process, "on" | "off" | "pid">;
|
|
12
|
+
kill?: typeof process.kill;
|
|
13
|
+
connect?: typeof connect;
|
|
14
|
+
}
|
|
15
|
+
/** `start`: connect and serve in the foreground until stopped, drained, revoked or replaced. */
|
|
16
|
+
export declare function start(deps: StartDeps): Promise<number>;
|
|
@@ -0,0 +1,73 @@
|
|
|
1
|
+
import { connect } from "../client.js";
|
|
2
|
+
import { CommandLog } from "../command-log.js";
|
|
3
|
+
import { readRunningPid, removePid, writePid } from "../pid.js";
|
|
4
|
+
import { rotateServiceLog } from "../service/launchd.js";
|
|
5
|
+
import { readCredential, readPolicy } from "../state.js";
|
|
6
|
+
/** `start`: connect and serve in the foreground until stopped, drained, revoked or replaced. */
|
|
7
|
+
export async function start(deps) {
|
|
8
|
+
const { stateDir, out, err } = deps;
|
|
9
|
+
const proc = deps.proc ?? process;
|
|
10
|
+
let cred;
|
|
11
|
+
try {
|
|
12
|
+
cred = readCredential(stateDir);
|
|
13
|
+
}
|
|
14
|
+
catch (e) {
|
|
15
|
+
err(e.message);
|
|
16
|
+
return 1;
|
|
17
|
+
}
|
|
18
|
+
if (cred === undefined) {
|
|
19
|
+
// Terminal: exit 0 so a service manager does not restart it until someone logs in.
|
|
20
|
+
err("Not logged in. Run capybara-device login --url <url> first.");
|
|
21
|
+
return 0;
|
|
22
|
+
}
|
|
23
|
+
if (deps.createExecutor === undefined) {
|
|
24
|
+
err("capybara-device start: no execution backend is available in this build");
|
|
25
|
+
return 1;
|
|
26
|
+
}
|
|
27
|
+
rotateServiceLog(stateDir);
|
|
28
|
+
const running = readRunningPid(stateDir, deps.kill);
|
|
29
|
+
if (running !== undefined && running !== proc.pid) {
|
|
30
|
+
err(`capybara-device is already running (pid ${running}). Stop it first: capybara-device stop`);
|
|
31
|
+
return 1;
|
|
32
|
+
}
|
|
33
|
+
// Signals arriving while the executor is still being prepared are applied once connected.
|
|
34
|
+
let conn;
|
|
35
|
+
let stopRequested = false;
|
|
36
|
+
let drainRequested = false;
|
|
37
|
+
const onStop = () => {
|
|
38
|
+
stopRequested = true;
|
|
39
|
+
conn?.stop();
|
|
40
|
+
};
|
|
41
|
+
const onDrain = () => {
|
|
42
|
+
drainRequested = true;
|
|
43
|
+
out("Draining: no new commands; stopping when the running ones finish.");
|
|
44
|
+
conn?.drain();
|
|
45
|
+
};
|
|
46
|
+
proc.on("SIGTERM", onStop);
|
|
47
|
+
proc.on("SIGINT", onStop);
|
|
48
|
+
proc.on("SIGUSR2", onDrain);
|
|
49
|
+
writePid(stateDir, proc.pid);
|
|
50
|
+
try {
|
|
51
|
+
let executor;
|
|
52
|
+
try {
|
|
53
|
+
executor = await deps.createExecutor(cred, readPolicy(stateDir));
|
|
54
|
+
}
|
|
55
|
+
catch (e) {
|
|
56
|
+
err(`Cannot start: ${e.message}`);
|
|
57
|
+
return 1;
|
|
58
|
+
}
|
|
59
|
+
if (stopRequested)
|
|
60
|
+
return 0;
|
|
61
|
+
conn = (deps.connect ?? connect)(cred, executor, { log: new CommandLog(stateDir), err });
|
|
62
|
+
if (drainRequested)
|
|
63
|
+
conn.drain();
|
|
64
|
+
out(`capybara-device: serving environment ${cred.environmentId} (${cred.mode} mode)`);
|
|
65
|
+
return await conn.done;
|
|
66
|
+
}
|
|
67
|
+
finally {
|
|
68
|
+
proc.off("SIGTERM", onStop);
|
|
69
|
+
proc.off("SIGINT", onStop);
|
|
70
|
+
proc.off("SIGUSR2", onDrain);
|
|
71
|
+
removePid(stateDir, proc.pid);
|
|
72
|
+
}
|
|
73
|
+
}
|
|
@@ -0,0 +1,32 @@
|
|
|
1
|
+
import { readCredential } from "../state.js";
|
|
2
|
+
/** `status`: exit 0 when logged in, 1 when not (or the credential file is unusable). */
|
|
3
|
+
export function status(stateDir, json, out, err) {
|
|
4
|
+
let cred;
|
|
5
|
+
try {
|
|
6
|
+
cred = readCredential(stateDir);
|
|
7
|
+
}
|
|
8
|
+
catch (e) {
|
|
9
|
+
err(e.message);
|
|
10
|
+
return 1;
|
|
11
|
+
}
|
|
12
|
+
if (cred === undefined) {
|
|
13
|
+
out(json ? JSON.stringify({ loggedIn: false }) : "Not logged in.");
|
|
14
|
+
return 1;
|
|
15
|
+
}
|
|
16
|
+
// The credential itself is never printed.
|
|
17
|
+
const { credential: _secret, ...shown } = cred;
|
|
18
|
+
if (json) {
|
|
19
|
+
out(JSON.stringify({ loggedIn: true, ...shown }));
|
|
20
|
+
}
|
|
21
|
+
else {
|
|
22
|
+
out([
|
|
23
|
+
"Logged in.",
|
|
24
|
+
` deployment: ${cred.url}`,
|
|
25
|
+
` organization: ${cred.orgId}`,
|
|
26
|
+
` environment: ${cred.environmentId}`,
|
|
27
|
+
` directory: ${cred.dir}`,
|
|
28
|
+
` mode: ${cred.mode}`,
|
|
29
|
+
].join("\n"));
|
|
30
|
+
}
|
|
31
|
+
return 0;
|
|
32
|
+
}
|
|
@@ -0,0 +1,120 @@
|
|
|
1
|
+
import { type DeviceExecArgs, type ExecResult, type SandboxRoots } from "@capybara-hq/plugin-sdk";
|
|
2
|
+
import type { Policy } from "../state.js";
|
|
3
|
+
import { type Executor } from "./types.js";
|
|
4
|
+
/** The version of the published sandbox images this program runs commands in. */
|
|
5
|
+
export declare const SANDBOX_IMAGE_TAG = "0.1.0-rc.5";
|
|
6
|
+
export declare const SANDBOX_IMAGE = "ghcr.io/capybara-hq/capybara-sandbox:0.1.0-rc.5";
|
|
7
|
+
/**
|
|
8
|
+
* The user commands run as. On Linux the bind-mounted environment directory belongs to the host user,
|
|
9
|
+
* so the container runs as that uid:gid (never root: a root host user falls back to the image's user).
|
|
10
|
+
* Docker Desktop (macOS) maps file ownership itself, so the image's user is used there.
|
|
11
|
+
*/
|
|
12
|
+
export declare function containerUser(platform?: NodeJS.Platform, uid?: number | undefined, gid?: number | undefined): string;
|
|
13
|
+
/** Container name of an environment. */
|
|
14
|
+
export declare function containerName(environmentId: string): string;
|
|
15
|
+
/** Whether the docker CLI can reach a daemon, and why not. */
|
|
16
|
+
export declare function dockerAvailability(): Promise<{
|
|
17
|
+
ok: true;
|
|
18
|
+
} | {
|
|
19
|
+
ok: false;
|
|
20
|
+
reason: string;
|
|
21
|
+
}>;
|
|
22
|
+
export interface DockerExecutorOptions {
|
|
23
|
+
/** The environment directory (from login). */
|
|
24
|
+
dir: string;
|
|
25
|
+
environmentId: string;
|
|
26
|
+
policy: Policy;
|
|
27
|
+
}
|
|
28
|
+
/**
|
|
29
|
+
* Runs every command with `docker exec` in one long-lived container of the published sandbox
|
|
30
|
+
* image: the environment directory's workspace, skills (read only) and tmp are mounted at
|
|
31
|
+
* /workspace, /skills and /tmp. Workspace and tmp files are read and written inside the container
|
|
32
|
+
* (as the `sandbox` user, so a link a command plants resolves in the container and never on the
|
|
33
|
+
* host); the skills directory, which commands cannot write, is accessed from the host with the
|
|
34
|
+
* same real-path checks as the OS executor. Network: none unless the effective egress is
|
|
35
|
+
* non-empty, then the default bridge (Docker mode has no domain allowlist).
|
|
36
|
+
*/
|
|
37
|
+
export declare class DockerExecutor implements Executor {
|
|
38
|
+
private readonly opts;
|
|
39
|
+
readonly mode: "docker";
|
|
40
|
+
readonly roots: SandboxRoots;
|
|
41
|
+
readonly name: string;
|
|
42
|
+
private readonly dir;
|
|
43
|
+
private readonly hostRoots;
|
|
44
|
+
private readonly guard;
|
|
45
|
+
private readonly hostFiles;
|
|
46
|
+
/** Marks every process of this environment's commands: `<dirKey>.<tag>`. */
|
|
47
|
+
private readonly dirKey;
|
|
48
|
+
private readonly running;
|
|
49
|
+
private chain;
|
|
50
|
+
private ready;
|
|
51
|
+
/** The container has the default bridge (commands may reach the network). */
|
|
52
|
+
private netOpen;
|
|
53
|
+
private inflight;
|
|
54
|
+
private readonly queue;
|
|
55
|
+
private pumping;
|
|
56
|
+
private dirty;
|
|
57
|
+
constructor(opts: DockerExecutorOptions);
|
|
58
|
+
ensureRoot(): Promise<"ok" | "gone">;
|
|
59
|
+
markerOf(tag: string): string;
|
|
60
|
+
private serial;
|
|
61
|
+
private inspect;
|
|
62
|
+
private wanted;
|
|
63
|
+
/** Every `docker run` flag of the container except its name and spec label. */
|
|
64
|
+
private runArgs;
|
|
65
|
+
/** A hash of the run flags: a container made with other flags is recreated. */
|
|
66
|
+
private specHash;
|
|
67
|
+
private matches;
|
|
68
|
+
private mustDocker;
|
|
69
|
+
/** Creates the container, or reuses the existing one when it matches; call inside `serial`. */
|
|
70
|
+
private ensureContainer;
|
|
71
|
+
/**
|
|
72
|
+
* Whether a reused container's mounts still show this directory's roots: after the directory was
|
|
73
|
+
* removed and recreated (`login` again), a Linux bind mount still holds the deleted one.
|
|
74
|
+
*/
|
|
75
|
+
private seesHostRoots;
|
|
76
|
+
private inspectNetworks;
|
|
77
|
+
private setNet;
|
|
78
|
+
/**
|
|
79
|
+
* Waits until the container exists with the wanted network state and counts the caller as
|
|
80
|
+
* running. Callers are served in arrival order: once one needs the other network state, later
|
|
81
|
+
* callers for the current state queue behind it, so it is not starved.
|
|
82
|
+
*/
|
|
83
|
+
private acquire;
|
|
84
|
+
private pump;
|
|
85
|
+
private dequeue;
|
|
86
|
+
private drain;
|
|
87
|
+
private release;
|
|
88
|
+
/** A docker-level failure that means the container vanished: the next call recreates it. */
|
|
89
|
+
private noteFailure;
|
|
90
|
+
exec(args: DeviceExecArgs, signal: AbortSignal): Promise<ExecResult>;
|
|
91
|
+
private run;
|
|
92
|
+
/** `path` made absolute and normalized; refused unless lexically under a root. */
|
|
93
|
+
private containerPath;
|
|
94
|
+
/** The host path of a /skills path (the skills directory is mounted read only). */
|
|
95
|
+
private skillsHostPath;
|
|
96
|
+
private inSkills;
|
|
97
|
+
/** Runs one file script in the container; `null` for a missing file. */
|
|
98
|
+
private fileOp;
|
|
99
|
+
private fileResult;
|
|
100
|
+
readFile(path: string, maxBytes: number): Promise<{
|
|
101
|
+
data: Uint8Array;
|
|
102
|
+
truncated: boolean;
|
|
103
|
+
} | null>;
|
|
104
|
+
writeFile(path: string, data: Uint8Array): Promise<void>;
|
|
105
|
+
stat(path: string): Promise<{
|
|
106
|
+
size: number;
|
|
107
|
+
isDir: boolean;
|
|
108
|
+
} | null>;
|
|
109
|
+
/** Kills the container processes of one marker (exact) or marker prefix. */
|
|
110
|
+
private killScript;
|
|
111
|
+
private killMarker;
|
|
112
|
+
killTagged(tags: string[]): Promise<void>;
|
|
113
|
+
killAll(): Promise<void>;
|
|
114
|
+
/** Kills every process carrying this environment's marker (also a previous run's). */
|
|
115
|
+
killLeftovers(): Promise<void>;
|
|
116
|
+
/** Kills every running command; the container stays for the next start. */
|
|
117
|
+
dispose(): Promise<void>;
|
|
118
|
+
}
|
|
119
|
+
/** The Docker executor: checks the daemon and kills what a previous run left running. */
|
|
120
|
+
export declare function createDockerExecutor(opts: DockerExecutorOptions): Promise<DockerExecutor>;
|