@culture.dev/agents 0.0.0-stage → 0.15.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 +201 -0
- package/README.md +59 -2
- package/dist/contract-schemas/agent.json +128 -0
- package/dist/contract-schemas/allowlist.json +25 -0
- package/dist/contract-schemas/api.json +501 -0
- package/dist/contract-schemas/capabilities.json +208 -0
- package/dist/contract-schemas/common.json +123 -0
- package/dist/contract-schemas/control-action.json +279 -0
- package/dist/contract-schemas/error.json +269 -0
- package/dist/contract-schemas/event.json +316 -0
- package/dist/contract-schemas/run.json +336 -0
- package/dist/contract-schemas/session.json +77 -0
- package/dist/contract-schemas/version.json +22 -0
- package/dist/src/contract/api.d.ts +41 -0
- package/dist/src/contract/api.js +35 -0
- package/dist/src/contract/api.js.map +1 -0
- package/dist/src/contract/errors.d.ts +21 -0
- package/dist/src/contract/errors.js +25 -0
- package/dist/src/contract/errors.js.map +1 -0
- package/dist/src/contract/generated/agent.d.ts +31 -0
- package/dist/src/contract/generated/agent.js +3 -0
- package/dist/src/contract/generated/agent.js.map +1 -0
- package/dist/src/contract/generated/allowlist.d.ts +10 -0
- package/dist/src/contract/generated/allowlist.js +3 -0
- package/dist/src/contract/generated/allowlist.js.map +1 -0
- package/dist/src/contract/generated/capabilities.d.ts +46 -0
- package/dist/src/contract/generated/capabilities.js +3 -0
- package/dist/src/contract/generated/capabilities.js.map +1 -0
- package/dist/src/contract/generated/common.d.ts +40 -0
- package/dist/src/contract/generated/common.js +3 -0
- package/dist/src/contract/generated/common.js.map +1 -0
- package/dist/src/contract/generated/control-action.d.ts +73 -0
- package/dist/src/contract/generated/control-action.js +3 -0
- package/dist/src/contract/generated/control-action.js.map +1 -0
- package/dist/src/contract/generated/error.d.ts +25 -0
- package/dist/src/contract/generated/error.js +3 -0
- package/dist/src/contract/generated/error.js.map +1 -0
- package/dist/src/contract/generated/event.d.ts +80 -0
- package/dist/src/contract/generated/event.js +3 -0
- package/dist/src/contract/generated/event.js.map +1 -0
- package/dist/src/contract/generated/index.d.ts +81 -0
- package/dist/src/contract/generated/index.js +3 -0
- package/dist/src/contract/generated/index.js.map +1 -0
- package/dist/src/contract/generated/run.d.ts +63 -0
- package/dist/src/contract/generated/run.js +3 -0
- package/dist/src/contract/generated/run.js.map +1 -0
- package/dist/src/contract/generated/session.d.ts +12 -0
- package/dist/src/contract/generated/session.js +3 -0
- package/dist/src/contract/generated/session.js.map +1 -0
- package/dist/src/contract/generated/version.d.ts +9 -0
- package/dist/src/contract/generated/version.js +3 -0
- package/dist/src/contract/generated/version.js.map +1 -0
- package/dist/src/contract/ids.d.ts +36 -0
- package/dist/src/contract/ids.js +85 -0
- package/dist/src/contract/ids.js.map +1 -0
- package/dist/src/contract/index.d.ts +17 -0
- package/dist/src/contract/index.js +13 -0
- package/dist/src/contract/index.js.map +1 -0
- package/dist/src/contract/run-state.d.ts +13 -0
- package/dist/src/contract/run-state.js +24 -0
- package/dist/src/contract/run-state.js.map +1 -0
- package/dist/src/contract/schemas.d.ts +18 -0
- package/dist/src/contract/schemas.js +37 -0
- package/dist/src/contract/schemas.js.map +1 -0
- package/dist/src/contract/validate.d.ts +17 -0
- package/dist/src/contract/validate.js +68 -0
- package/dist/src/contract/validate.js.map +1 -0
- package/dist/src/daemon/allowlist.d.ts +23 -0
- package/dist/src/daemon/allowlist.js +57 -0
- package/dist/src/daemon/allowlist.js.map +1 -0
- package/dist/src/daemon/daemon.d.ts +31 -0
- package/dist/src/daemon/daemon.js +141 -0
- package/dist/src/daemon/daemon.js.map +1 -0
- package/dist/src/daemon/errors.d.ts +14 -0
- package/dist/src/daemon/errors.js +26 -0
- package/dist/src/daemon/errors.js.map +1 -0
- package/dist/src/daemon/harness-catalog.d.ts +11 -0
- package/dist/src/daemon/harness-catalog.js +21 -0
- package/dist/src/daemon/harness-catalog.js.map +1 -0
- package/dist/src/daemon/http.d.ts +15 -0
- package/dist/src/daemon/http.js +76 -0
- package/dist/src/daemon/http.js.map +1 -0
- package/dist/src/daemon/lock.d.ts +47 -0
- package/dist/src/daemon/lock.js +167 -0
- package/dist/src/daemon/lock.js.map +1 -0
- package/dist/src/daemon/main.d.ts +32 -0
- package/dist/src/daemon/main.js +204 -0
- package/dist/src/daemon/main.js.map +1 -0
- package/dist/src/daemon/paths.d.ts +29 -0
- package/dist/src/daemon/paths.js +26 -0
- package/dist/src/daemon/paths.js.map +1 -0
- package/dist/src/daemon/ports.d.ts +58 -0
- package/dist/src/daemon/ports.js +2 -0
- package/dist/src/daemon/ports.js.map +1 -0
- package/dist/src/daemon/routes.d.ts +15 -0
- package/dist/src/daemon/routes.js +259 -0
- package/dist/src/daemon/routes.js.map +1 -0
- package/dist/src/daemon/sse.d.ts +8 -0
- package/dist/src/daemon/sse.js +56 -0
- package/dist/src/daemon/sse.js.map +1 -0
- package/dist/src/harness/availability.d.ts +9 -0
- package/dist/src/harness/availability.js +52 -0
- package/dist/src/harness/availability.js.map +1 -0
- package/dist/src/harness/claude-code/agent.d.ts +4 -0
- package/dist/src/harness/claude-code/agent.js +33 -0
- package/dist/src/harness/claude-code/agent.js.map +1 -0
- package/dist/src/harness/claude-code/backend.d.ts +48 -0
- package/dist/src/harness/claude-code/backend.js +83 -0
- package/dist/src/harness/claude-code/backend.js.map +1 -0
- package/dist/src/harness/claude-code/capabilities.d.ts +7 -0
- package/dist/src/harness/claude-code/capabilities.js +11 -0
- package/dist/src/harness/claude-code/capabilities.js.map +1 -0
- package/dist/src/harness/claude-code/index.d.ts +15 -0
- package/dist/src/harness/claude-code/index.js +20 -0
- package/dist/src/harness/claude-code/index.js.map +1 -0
- package/dist/src/harness/claude-code/map.d.ts +30 -0
- package/dist/src/harness/claude-code/map.js +108 -0
- package/dist/src/harness/claude-code/map.js.map +1 -0
- package/dist/src/harness/common/capabilities.d.ts +34 -0
- package/dist/src/harness/common/capabilities.js +60 -0
- package/dist/src/harness/common/capabilities.js.map +1 -0
- package/dist/src/harness/common/guard.d.ts +18 -0
- package/dist/src/harness/common/guard.js +31 -0
- package/dist/src/harness/common/guard.js.map +1 -0
- package/dist/src/harness/common/processes.d.ts +21 -0
- package/dist/src/harness/common/processes.js +46 -0
- package/dist/src/harness/common/processes.js.map +1 -0
- package/dist/src/harness/common/queue.d.ts +20 -0
- package/dist/src/harness/common/queue.js +76 -0
- package/dist/src/harness/common/queue.js.map +1 -0
- package/dist/src/harness/common/run-driver.d.ts +88 -0
- package/dist/src/harness/common/run-driver.js +317 -0
- package/dist/src/harness/common/run-driver.js.map +1 -0
- package/dist/src/harness/common/sandbox-workspace.d.ts +72 -0
- package/dist/src/harness/common/sandbox-workspace.js +155 -0
- package/dist/src/harness/common/sandbox-workspace.js.map +1 -0
- package/dist/src/harness/common/text.d.ts +9 -0
- package/dist/src/harness/common/text.js +49 -0
- package/dist/src/harness/common/text.js.map +1 -0
- package/dist/src/harness/common/types.d.ts +111 -0
- package/dist/src/harness/common/types.js +2 -0
- package/dist/src/harness/common/types.js.map +1 -0
- package/dist/src/harness/config.d.ts +43 -0
- package/dist/src/harness/config.js +200 -0
- package/dist/src/harness/config.js.map +1 -0
- package/dist/src/harness/index.d.ts +21 -0
- package/dist/src/harness/index.js +89 -0
- package/dist/src/harness/index.js.map +1 -0
- package/dist/src/harness/opencode/backend.d.ts +37 -0
- package/dist/src/harness/opencode/backend.js +114 -0
- package/dist/src/harness/opencode/backend.js.map +1 -0
- package/dist/src/harness/opencode/capabilities.d.ts +8 -0
- package/dist/src/harness/opencode/capabilities.js +11 -0
- package/dist/src/harness/opencode/capabilities.js.map +1 -0
- package/dist/src/harness/opencode/client.d.ts +70 -0
- package/dist/src/harness/opencode/client.js +189 -0
- package/dist/src/harness/opencode/client.js.map +1 -0
- package/dist/src/harness/opencode/index.d.ts +13 -0
- package/dist/src/harness/opencode/index.js +19 -0
- package/dist/src/harness/opencode/index.js.map +1 -0
- package/dist/src/harness/opencode/lines.d.ts +14 -0
- package/dist/src/harness/opencode/lines.js +99 -0
- package/dist/src/harness/opencode/lines.js.map +1 -0
- package/dist/src/harness/opencode/map.d.ts +13 -0
- package/dist/src/harness/opencode/map.js +84 -0
- package/dist/src/harness/opencode/map.js.map +1 -0
- package/dist/src/harness/opencode/turn.d.ts +23 -0
- package/dist/src/harness/opencode/turn.js +143 -0
- package/dist/src/harness/opencode/turn.js.map +1 -0
- package/dist/src/harness/reclaim.d.ts +13 -0
- package/dist/src/harness/reclaim.js +18 -0
- package/dist/src/harness/reclaim.js.map +1 -0
- package/dist/src/harness/stub/backend.d.ts +11 -0
- package/dist/src/harness/stub/backend.js +125 -0
- package/dist/src/harness/stub/backend.js.map +1 -0
- package/dist/src/harness/stub/index.d.ts +18 -0
- package/dist/src/harness/stub/index.js +21 -0
- package/dist/src/harness/stub/index.js.map +1 -0
- package/dist/src/harness/stub/workspace.d.ts +29 -0
- package/dist/src/harness/stub/workspace.js +92 -0
- package/dist/src/harness/stub/workspace.js.map +1 -0
- package/dist/src/index.d.ts +2 -0
- package/dist/src/index.js +3 -0
- package/dist/src/index.js.map +1 -0
- package/dist/src/runs/agents.d.ts +10 -0
- package/dist/src/runs/agents.js +49 -0
- package/dist/src/runs/agents.js.map +1 -0
- package/dist/src/runs/control.d.ts +31 -0
- package/dist/src/runs/control.js +105 -0
- package/dist/src/runs/control.js.map +1 -0
- package/dist/src/runs/index.d.ts +8 -0
- package/dist/src/runs/index.js +7 -0
- package/dist/src/runs/index.js.map +1 -0
- package/dist/src/runs/ports.d.ts +31 -0
- package/dist/src/runs/ports.js +5 -0
- package/dist/src/runs/ports.js.map +1 -0
- package/dist/src/runs/scan.d.ts +38 -0
- package/dist/src/runs/scan.js +149 -0
- package/dist/src/runs/scan.js.map +1 -0
- package/dist/src/runs/service.d.ts +70 -0
- package/dist/src/runs/service.js +239 -0
- package/dist/src/runs/service.js.map +1 -0
- package/dist/src/sandbox/harness-session.d.ts +11 -0
- package/dist/src/sandbox/harness-session.js +170 -0
- package/dist/src/sandbox/harness-session.js.map +1 -0
- package/dist/src/sandbox/host-command.d.ts +19 -0
- package/dist/src/sandbox/host-command.js +41 -0
- package/dist/src/sandbox/host-command.js.map +1 -0
- package/dist/src/sandbox/index.d.ts +11 -0
- package/dist/src/sandbox/index.js +15 -0
- package/dist/src/sandbox/index.js.map +1 -0
- package/dist/src/sandbox/naming.d.ts +49 -0
- package/dist/src/sandbox/naming.js +106 -0
- package/dist/src/sandbox/naming.js.map +1 -0
- package/dist/src/sandbox/openshell-client.d.ts +81 -0
- package/dist/src/sandbox/openshell-client.js +49 -0
- package/dist/src/sandbox/openshell-client.js.map +1 -0
- package/dist/src/sandbox/policy.d.ts +26 -0
- package/dist/src/sandbox/policy.js +100 -0
- package/dist/src/sandbox/policy.js.map +1 -0
- package/dist/src/sandbox/run-sandbox.d.ts +38 -0
- package/dist/src/sandbox/run-sandbox.js +247 -0
- package/dist/src/sandbox/run-sandbox.js.map +1 -0
- package/dist/src/sandbox/run-sandboxes.d.ts +18 -0
- package/dist/src/sandbox/run-sandboxes.js +225 -0
- package/dist/src/sandbox/run-sandboxes.js.map +1 -0
- package/dist/src/sandbox/timeout.d.ts +12 -0
- package/dist/src/sandbox/timeout.js +39 -0
- package/dist/src/sandbox/timeout.js.map +1 -0
- package/dist/src/sandbox/types.d.ts +125 -0
- package/dist/src/sandbox/types.js +15 -0
- package/dist/src/sandbox/types.js.map +1 -0
- package/dist/src/sandbox/worktree.d.ts +72 -0
- package/dist/src/sandbox/worktree.js +157 -0
- package/dist/src/sandbox/worktree.js.map +1 -0
- package/dist/src/store/derive.d.ts +39 -0
- package/dist/src/store/derive.js +68 -0
- package/dist/src/store/derive.js.map +1 -0
- package/dist/src/store/event-log.d.ts +79 -0
- package/dist/src/store/event-log.js +202 -0
- package/dist/src/store/event-log.js.map +1 -0
- package/dist/src/store/files.d.ts +51 -0
- package/dist/src/store/files.js +103 -0
- package/dist/src/store/files.js.map +1 -0
- package/dist/src/store/index.d.ts +15 -0
- package/dist/src/store/index.js +11 -0
- package/dist/src/store/index.js.map +1 -0
- package/dist/src/store/liveness.d.ts +61 -0
- package/dist/src/store/liveness.js +150 -0
- package/dist/src/store/liveness.js.map +1 -0
- package/dist/src/store/run-store.d.ts +67 -0
- package/dist/src/store/run-store.js +211 -0
- package/dist/src/store/run-store.js.map +1 -0
- package/dist/src/util/sequence.d.ts +58 -0
- package/dist/src/util/sequence.js +119 -0
- package/dist/src/util/sequence.js.map +1 -0
- package/dist/src/worker/control.d.ts +60 -0
- package/dist/src/worker/control.js +205 -0
- package/dist/src/worker/control.js.map +1 -0
- package/dist/src/worker/delivery.d.ts +17 -0
- package/dist/src/worker/delivery.js +43 -0
- package/dist/src/worker/delivery.js.map +1 -0
- package/dist/src/worker/driver.d.ts +88 -0
- package/dist/src/worker/driver.js +6 -0
- package/dist/src/worker/driver.js.map +1 -0
- package/dist/src/worker/fake-driver.d.ts +65 -0
- package/dist/src/worker/fake-driver.js +123 -0
- package/dist/src/worker/fake-driver.js.map +1 -0
- package/dist/src/worker/index.d.ts +15 -0
- package/dist/src/worker/index.js +11 -0
- package/dist/src/worker/index.js.map +1 -0
- package/dist/src/worker/main.d.ts +1 -0
- package/dist/src/worker/main.js +57 -0
- package/dist/src/worker/main.js.map +1 -0
- package/dist/src/worker/retained.d.ts +13 -0
- package/dist/src/worker/retained.js +40 -0
- package/dist/src/worker/retained.js.map +1 -0
- package/dist/src/worker/run-loop.d.ts +63 -0
- package/dist/src/worker/run-loop.js +325 -0
- package/dist/src/worker/run-loop.js.map +1 -0
- package/dist/src/worker/spawn.d.ts +68 -0
- package/dist/src/worker/spawn.js +201 -0
- package/dist/src/worker/spawn.js.map +1 -0
- package/image/Dockerfile +81 -0
- package/image/build.sh +62 -0
- package/image/pnpm-shim.sh +25 -0
- package/package.json +54 -4
- package/policy/profiles/culture-agents-claude-subscription.yaml +59 -0
- package/policy/profiles/culture-agents-local-gateway.yaml +49 -0
- package/policy/run-sandbox.policy.json +10 -0
|
@@ -0,0 +1,106 @@
|
|
|
1
|
+
import { createHash } from "node:crypto";
|
|
2
|
+
/** OpenShell rejects sandbox names longer than this (`name exceeds maximum length`). */
|
|
3
|
+
export const MAX_SANDBOX_NAME = 19;
|
|
4
|
+
/** Default sandbox-name prefix for runs started by the core. */
|
|
5
|
+
export const DEFAULT_PREFIX = "ca-";
|
|
6
|
+
/** Label keys every run sandbox carries, so a crashed process can find its sandboxes again. */
|
|
7
|
+
export const LABEL_MANAGED_BY = "managed-by";
|
|
8
|
+
export const LABEL_MANAGED_BY_VALUE = "culture-agents";
|
|
9
|
+
/** The full run id, when it fits one label value (at most 63 characters, alphanumeric at both ends). */
|
|
10
|
+
export const LABEL_RUN_ID = "run-id";
|
|
11
|
+
/** A hash of the run id; always present and always a valid label value, whatever the id's length. */
|
|
12
|
+
export const LABEL_RUN_KEY = "run-key";
|
|
13
|
+
/** Prefix of the labels holding a run id too long for one label value: `run-id-1`, `run-id-2`, ... */
|
|
14
|
+
export const LABEL_RUN_ID_PART = "run-id-";
|
|
15
|
+
/** Longest run id: the contract's `<node>-<agent>.run.<ULID>` with a 96-character mesh id. */
|
|
16
|
+
export const MAX_RUN_ID = 127;
|
|
17
|
+
/** OpenShell (Kubernetes-style) label values: 63 characters, alphanumeric at both ends. */
|
|
18
|
+
const LABEL_VALUE = /^[A-Za-z0-9]([A-Za-z0-9._-]{0,61}[A-Za-z0-9])?$/;
|
|
19
|
+
/** Characters of the run id per part label; each part is wrapped in `x...x` to keep both ends alphanumeric. */
|
|
20
|
+
const PART = 61;
|
|
21
|
+
const RUN_ID = new RegExp(`^[A-Za-z0-9][A-Za-z0-9._-]{0,${MAX_RUN_ID - 1}}$`);
|
|
22
|
+
const PREFIX = /^[a-z][a-z0-9-]{0,8}$/;
|
|
23
|
+
/** Thrown for a run id or prefix the sandbox layer refuses; never reaches the gateway. */
|
|
24
|
+
export class InvalidRunIdError extends Error {
|
|
25
|
+
name = "InvalidRunIdError";
|
|
26
|
+
}
|
|
27
|
+
/**
|
|
28
|
+
* Validate a run id. It becomes a label value and part of the `runs/<id>` git
|
|
29
|
+
* branch, so it is held to a charset that is safe in both places.
|
|
30
|
+
*/
|
|
31
|
+
export function validateRunId(runId) {
|
|
32
|
+
if (!RUN_ID.test(runId) || runId.includes("..") || runId.endsWith(".lock") || runId.endsWith(".")) {
|
|
33
|
+
throw new InvalidRunIdError(`invalid run id ${JSON.stringify(runId)}: use 1-${MAX_RUN_ID} of [A-Za-z0-9._-], ` +
|
|
34
|
+
"starting alphanumeric, without '..' or a trailing '.' or '.lock'");
|
|
35
|
+
}
|
|
36
|
+
return runId;
|
|
37
|
+
}
|
|
38
|
+
export function validatePrefix(prefix) {
|
|
39
|
+
if (!PREFIX.test(prefix)) {
|
|
40
|
+
throw new InvalidRunIdError(`invalid sandbox prefix ${JSON.stringify(prefix)}: 1-9 of [a-z0-9-], starting with a letter`);
|
|
41
|
+
}
|
|
42
|
+
return prefix;
|
|
43
|
+
}
|
|
44
|
+
/**
|
|
45
|
+
* Deterministic sandbox name for a run: the prefix plus a sha256 of the run id,
|
|
46
|
+
* cut to OpenShell's 19-character cap. The same run id always maps to the same
|
|
47
|
+
* name, so create, resume and delete need nothing but the run id.
|
|
48
|
+
*/
|
|
49
|
+
export function sandboxName(runId, prefix = DEFAULT_PREFIX) {
|
|
50
|
+
validateRunId(runId);
|
|
51
|
+
validatePrefix(prefix);
|
|
52
|
+
const hash = createHash("sha256").update(runId).digest("hex");
|
|
53
|
+
return `${prefix}${hash}`.slice(0, MAX_SANDBOX_NAME);
|
|
54
|
+
}
|
|
55
|
+
/** The git branch a run works on, inside the sandbox and on the host after collect. */
|
|
56
|
+
export function runBranch(runId) {
|
|
57
|
+
return `runs/${validateRunId(runId)}`;
|
|
58
|
+
}
|
|
59
|
+
/** The run-key label value: 48 hex digits of sha256(run id). Selector-safe for any run id. */
|
|
60
|
+
export function runKey(runId) {
|
|
61
|
+
return createHash("sha256").update(`run-key:${runId}`).digest("hex").slice(0, 48);
|
|
62
|
+
}
|
|
63
|
+
/**
|
|
64
|
+
* Labels of a run's sandbox. Label values are capped at 63 characters and must be alphanumeric at
|
|
65
|
+
* both ends, but a contract run id can be 127 characters long. So every sandbox carries
|
|
66
|
+
* `run-key` (a hash, always valid) and the full id: in `run-id` when it is a valid label value,
|
|
67
|
+
* otherwise split over `run-id-1`, `run-id-2`, ..., each part wrapped as `x<part>x`.
|
|
68
|
+
*/
|
|
69
|
+
export function runLabels(runId) {
|
|
70
|
+
validateRunId(runId);
|
|
71
|
+
const labels = { [LABEL_MANAGED_BY]: LABEL_MANAGED_BY_VALUE, [LABEL_RUN_KEY]: runKey(runId) };
|
|
72
|
+
if (LABEL_VALUE.test(runId)) {
|
|
73
|
+
labels[LABEL_RUN_ID] = runId;
|
|
74
|
+
return labels;
|
|
75
|
+
}
|
|
76
|
+
for (let i = 0; i * PART < runId.length; i++)
|
|
77
|
+
labels[`${LABEL_RUN_ID_PART}${i + 1}`] = `x${runId.slice(i * PART, (i + 1) * PART)}x`;
|
|
78
|
+
return labels;
|
|
79
|
+
}
|
|
80
|
+
/** The full run id recorded in a sandbox's labels (see `runLabels`), or null when there is none. */
|
|
81
|
+
export function runIdFromLabels(labels) {
|
|
82
|
+
const whole = labels[LABEL_RUN_ID];
|
|
83
|
+
if (whole)
|
|
84
|
+
return whole;
|
|
85
|
+
let id = "";
|
|
86
|
+
for (let i = 1;; i++) {
|
|
87
|
+
const part = labels[`${LABEL_RUN_ID_PART}${i}`];
|
|
88
|
+
if (part === undefined)
|
|
89
|
+
break;
|
|
90
|
+
if (part.length < 2 || !part.startsWith("x") || !part.endsWith("x"))
|
|
91
|
+
return null;
|
|
92
|
+
id += part.slice(1, -1);
|
|
93
|
+
}
|
|
94
|
+
return id === "" ? null : id;
|
|
95
|
+
}
|
|
96
|
+
/**
|
|
97
|
+
* Whether a sandbox's labels belong to `runId`: the recorded full id matches, and so does the run
|
|
98
|
+
* key when there is one (sandboxes created before the key existed carry only `run-id`).
|
|
99
|
+
*/
|
|
100
|
+
export function labelsMatchRun(labels, runId) {
|
|
101
|
+
const key = labels[LABEL_RUN_KEY];
|
|
102
|
+
if (key !== undefined && key !== runKey(runId))
|
|
103
|
+
return false;
|
|
104
|
+
return runIdFromLabels(labels) === runId;
|
|
105
|
+
}
|
|
106
|
+
//# sourceMappingURL=naming.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"naming.js","sourceRoot":"","sources":["../../../src/sandbox/naming.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,UAAU,EAAE,MAAM,aAAa,CAAC;AAEzC,wFAAwF;AACxF,MAAM,CAAC,MAAM,gBAAgB,GAAG,EAAE,CAAC;AAEnC,gEAAgE;AAChE,MAAM,CAAC,MAAM,cAAc,GAAG,KAAK,CAAC;AAEpC,+FAA+F;AAC/F,MAAM,CAAC,MAAM,gBAAgB,GAAG,YAAY,CAAC;AAC7C,MAAM,CAAC,MAAM,sBAAsB,GAAG,gBAAgB,CAAC;AACvD,wGAAwG;AACxG,MAAM,CAAC,MAAM,YAAY,GAAG,QAAQ,CAAC;AACrC,qGAAqG;AACrG,MAAM,CAAC,MAAM,aAAa,GAAG,SAAS,CAAC;AACvC,sGAAsG;AACtG,MAAM,CAAC,MAAM,iBAAiB,GAAG,SAAS,CAAC;AAE3C,8FAA8F;AAC9F,MAAM,CAAC,MAAM,UAAU,GAAG,GAAG,CAAC;AAC9B,2FAA2F;AAC3F,MAAM,WAAW,GAAG,iDAAiD,CAAC;AACtE,+GAA+G;AAC/G,MAAM,IAAI,GAAG,EAAE,CAAC;AAEhB,MAAM,MAAM,GAAG,IAAI,MAAM,CAAC,gCAAgC,UAAU,GAAG,CAAC,IAAI,CAAC,CAAC;AAC9E,MAAM,MAAM,GAAG,uBAAuB,CAAC;AAEvC,0FAA0F;AAC1F,MAAM,OAAO,iBAAkB,SAAQ,KAAK;IACxB,IAAI,GAAG,mBAAmB,CAAC;CAC9C;AAED;;;GAGG;AACH,MAAM,UAAU,aAAa,CAAC,KAAa;IACzC,IAAI,CAAC,MAAM,CAAC,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AQ,CAAC,GAAG,CAAC;YAAE,OAAO,IAAI,CAAC;QACjF,EAAE,IAAI,IAAI,CAAC,KAAK,CAAC,CAAC,EAAE,CAAC,CAAC,CAAC,CAAC;IAC1B,CAAC;IACD,OAAO,EAAE,KAAK,EAAE,CAAC,CAAC,CAAC,IAAI,CAAC,CAAC,CAAC,EAAE,CAAC;AAC/B,CAAC;AAED;;;GAGG;AACH,MAAM,UAAU,cAAc,CAAC,MAAwC,EAAE,KAAa;IACpF,MAAM,GAAG,GAAG,MAAM,CAAC,aAAa,CAAC,CAAC;IAClC,IAAI,GAAG,KAAK,SAAS,IAAI,GAAG,KAAK,MAAM,CAAC,KAAK,CAAC;QAAE,OAAO,KAAK,CAAC;IAC7D,OAAO,eAAe,CAAC,MAAM,CAAC,KAAK,KAAK,CAAC;AAC3C,CAAC"}
|
|
@@ -0,0 +1,81 @@
|
|
|
1
|
+
import { SandboxClient, type SandboxSpec } from "@ai-openshell/sdk";
|
|
2
|
+
export type SandboxPolicyInit = NonNullable<SandboxSpec["policy"]>;
|
|
3
|
+
export interface CreateSpec {
|
|
4
|
+
name: string;
|
|
5
|
+
image: string;
|
|
6
|
+
providers: string[];
|
|
7
|
+
labels: Record<string, string>;
|
|
8
|
+
policy?: SandboxPolicyInit;
|
|
9
|
+
}
|
|
10
|
+
export interface SandboxInfo {
|
|
11
|
+
name: string;
|
|
12
|
+
phase: string;
|
|
13
|
+
labels: Record<string, string>;
|
|
14
|
+
}
|
|
15
|
+
export interface ExecRequest {
|
|
16
|
+
workdir?: string;
|
|
17
|
+
environment?: Record<string, string>;
|
|
18
|
+
stdin?: Buffer;
|
|
19
|
+
noLoginShell?: boolean;
|
|
20
|
+
signal?: AbortSignal;
|
|
21
|
+
}
|
|
22
|
+
export interface ExecReply {
|
|
23
|
+
exitCode: number;
|
|
24
|
+
stdout: Buffer;
|
|
25
|
+
stderr: Buffer;
|
|
26
|
+
}
|
|
27
|
+
export type StreamEvent = {
|
|
28
|
+
stream: "stdout" | "stderr";
|
|
29
|
+
data: Buffer;
|
|
30
|
+
} | {
|
|
31
|
+
type: "exit";
|
|
32
|
+
exitCode: number;
|
|
33
|
+
};
|
|
34
|
+
export interface InteractiveControl {
|
|
35
|
+
output: AsyncIterable<StreamEvent>;
|
|
36
|
+
write(data: Buffer): void;
|
|
37
|
+
closeInput(): void;
|
|
38
|
+
cancel(): void;
|
|
39
|
+
}
|
|
40
|
+
export interface ForwardRef {
|
|
41
|
+
localHost: string;
|
|
42
|
+
localPort: number;
|
|
43
|
+
close(): Promise<void>;
|
|
44
|
+
}
|
|
45
|
+
export interface OpenShellClientLike {
|
|
46
|
+
create(spec: CreateSpec): Promise<SandboxInfo>;
|
|
47
|
+
get(name: string): Promise<SandboxInfo>;
|
|
48
|
+
listAll(options?: {
|
|
49
|
+
labelSelector?: string;
|
|
50
|
+
}): Promise<SandboxInfo[]>;
|
|
51
|
+
waitReady(name: string, timeoutSecs: number, options?: {
|
|
52
|
+
signal?: AbortSignal;
|
|
53
|
+
}): Promise<unknown>;
|
|
54
|
+
delete(name: string, options?: {
|
|
55
|
+
allowMissing?: boolean;
|
|
56
|
+
}): Promise<unknown>;
|
|
57
|
+
waitDeleted(name: string, timeoutSecs: number): Promise<void>;
|
|
58
|
+
exec(name: string, command: string[], options?: ExecRequest): Promise<ExecReply>;
|
|
59
|
+
execInteractive(name: string, command: string[], options?: ExecRequest & {
|
|
60
|
+
tty?: boolean;
|
|
61
|
+
}): Promise<InteractiveControl>;
|
|
62
|
+
forward(name: string, options: {
|
|
63
|
+
targetPort: number;
|
|
64
|
+
onConnectionError?: (error: Error) => void;
|
|
65
|
+
}): Promise<ForwardRef>;
|
|
66
|
+
}
|
|
67
|
+
export interface GatewayOptions {
|
|
68
|
+
/** Default `https://127.0.0.1:17670` (the local gateway). */
|
|
69
|
+
readonly gateway?: string;
|
|
70
|
+
/** Directory holding `ca.crt` and `client/tls.{crt,key}`; default `$OPENSHELL_LOCAL_TLS_DIR`. */
|
|
71
|
+
readonly tlsDir?: string;
|
|
72
|
+
}
|
|
73
|
+
export declare const DEFAULT_GATEWAY = "https://127.0.0.1:17670";
|
|
74
|
+
/** Connect to an OpenShell gateway over mTLS. Lazy: no RPC is made until first use. */
|
|
75
|
+
export declare function connectOpenShell(options?: GatewayOptions): Promise<OpenShellClientLike>;
|
|
76
|
+
/** Narrow a real SDK client to the structural interface (and check it at compile time). */
|
|
77
|
+
export declare function adaptSandboxClient(client: SandboxClient): OpenShellClientLike;
|
|
78
|
+
/** True when an SDK (or fake) error means the sandbox does not exist. */
|
|
79
|
+
export declare function isNotFound(error: unknown): boolean;
|
|
80
|
+
export declare function isAlreadyExists(error: unknown): boolean;
|
|
81
|
+
export declare function isAuthFailure(error: unknown): boolean;
|
|
@@ -0,0 +1,49 @@
|
|
|
1
|
+
// The slice of the OpenShell TypeScript SDK this directory uses, as a
|
|
2
|
+
// structural interface so tests can inject a fake gateway. OpenShell types stay
|
|
3
|
+
// inside src/sandbox/.
|
|
4
|
+
import { readFileSync } from "node:fs";
|
|
5
|
+
import { join } from "node:path";
|
|
6
|
+
import { SandboxClient } from "@ai-openshell/sdk";
|
|
7
|
+
export const DEFAULT_GATEWAY = "https://127.0.0.1:17670";
|
|
8
|
+
/** Connect to an OpenShell gateway over mTLS. Lazy: no RPC is made until first use. */
|
|
9
|
+
export async function connectOpenShell(options = {}) {
|
|
10
|
+
const tlsDir = options.tlsDir ?? process.env["OPENSHELL_LOCAL_TLS_DIR"];
|
|
11
|
+
if (!tlsDir)
|
|
12
|
+
throw new Error("OpenShell TLS directory not set (OPENSHELL_LOCAL_TLS_DIR)");
|
|
13
|
+
const client = await SandboxClient.connect({
|
|
14
|
+
gateway: options.gateway ?? DEFAULT_GATEWAY,
|
|
15
|
+
caCert: readFileSync(join(tlsDir, "ca.crt")),
|
|
16
|
+
clientCert: readFileSync(join(tlsDir, "client", "tls.crt")),
|
|
17
|
+
clientKey: readFileSync(join(tlsDir, "client", "tls.key")),
|
|
18
|
+
});
|
|
19
|
+
return adaptSandboxClient(client);
|
|
20
|
+
}
|
|
21
|
+
/** Narrow a real SDK client to the structural interface (and check it at compile time). */
|
|
22
|
+
export function adaptSandboxClient(client) {
|
|
23
|
+
return {
|
|
24
|
+
create: async (spec) => toInfo(await client.create(spec)),
|
|
25
|
+
get: async (name) => toInfo(await client.get(name)),
|
|
26
|
+
listAll: async (options) => (await client.listAll(options ?? null)).map(toInfo),
|
|
27
|
+
waitReady: (name, timeoutSecs, options) => client.waitReady(name, timeoutSecs, options ?? null),
|
|
28
|
+
delete: (name, options) => client.delete(name, options ?? null),
|
|
29
|
+
waitDeleted: (name, timeoutSecs) => client.waitDeleted(name, timeoutSecs),
|
|
30
|
+
exec: (name, command, options) => client.exec(name, command, options ?? null),
|
|
31
|
+
execInteractive: (name, command, options) => client.execInteractive(name, command, options ?? null),
|
|
32
|
+
forward: (name, options) => client.forward(name, options),
|
|
33
|
+
};
|
|
34
|
+
}
|
|
35
|
+
function toInfo(ref) {
|
|
36
|
+
return { name: ref.name, phase: ref.phase, labels: { ...ref.labels } };
|
|
37
|
+
}
|
|
38
|
+
/** True when an SDK (or fake) error means the sandbox does not exist. */
|
|
39
|
+
export function isNotFound(error) {
|
|
40
|
+
return error?.code === "not_found";
|
|
41
|
+
}
|
|
42
|
+
export function isAlreadyExists(error) {
|
|
43
|
+
return error?.code === "already_exists";
|
|
44
|
+
}
|
|
45
|
+
export function isAuthFailure(error) {
|
|
46
|
+
const code = error?.code;
|
|
47
|
+
return code === "auth" || code === "tls";
|
|
48
|
+
}
|
|
49
|
+
//# sourceMappingURL=openshell-client.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"openshell-client.js","sourceRoot":"","sources":["../../../src/sandbox/openshell-client.ts"],"names":[],"mappings":"AAAA,sEAAsE;AACtE,gFAAgF;AAChF,uBAAuB;AACvB,OAAO,EAAE,YAAY,EAAE,MAAM,SAAS,CAAC;AACvC,OAAO,EAAE,IAAI,EAAE,MAAM,WAAW,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IAC1C,MAAM,IAAI,GAAI,KAAmC,EAAE,IAAI,CAAC;IACxD,OAAO,IAAI,KAAK,MAAM,IAAI,IAAI,KAAK,KAAK,CAAC;AAC3C,CAAC"}
|
|
@@ -0,0 +1,26 @@
|
|
|
1
|
+
import type { SandboxPolicyInit } from "./openshell-client.js";
|
|
2
|
+
/** `core/policy/` resolved from compiled output (`dist/src/sandbox/`) or source. */
|
|
3
|
+
export declare const POLICY_DIR: string;
|
|
4
|
+
export declare const DEFAULT_POLICY_PATH: string;
|
|
5
|
+
export declare class PolicyError extends Error {
|
|
6
|
+
readonly name = "PolicyError";
|
|
7
|
+
}
|
|
8
|
+
export interface AuthoredPolicy {
|
|
9
|
+
version: 1;
|
|
10
|
+
filesystem_policy?: {
|
|
11
|
+
include_workdir?: boolean;
|
|
12
|
+
read_only?: string[];
|
|
13
|
+
read_write?: string[];
|
|
14
|
+
};
|
|
15
|
+
landlock?: {
|
|
16
|
+
compatibility: "best_effort" | "hard_requirement";
|
|
17
|
+
};
|
|
18
|
+
process?: {
|
|
19
|
+
run_as_user?: string;
|
|
20
|
+
run_as_group?: string;
|
|
21
|
+
};
|
|
22
|
+
network_policies?: Record<string, never>;
|
|
23
|
+
}
|
|
24
|
+
export declare function loadPolicyFile(path?: string): SandboxPolicyInit;
|
|
25
|
+
/** Validate an authored policy object and convert it to the SDK's create-time shape. */
|
|
26
|
+
export declare function toSdkPolicy(input: unknown): SandboxPolicyInit;
|
|
@@ -0,0 +1,100 @@
|
|
|
1
|
+
// Loads the authored run policy (core/policy/run-sandbox.policy.json) and maps
|
|
2
|
+
// it to the create-time policy shape the OpenShell SDK takes.
|
|
3
|
+
//
|
|
4
|
+
// The file is OpenShell's authored policy schema written as JSON, so
|
|
5
|
+
// `openshell policy lint core/policy/run-sandbox.policy.json` validates it
|
|
6
|
+
// offline. This loader supports exactly the sections the run policy uses and
|
|
7
|
+
// refuses anything else, so a field it does not understand can never be
|
|
8
|
+
// dropped silently on the way to the gateway.
|
|
9
|
+
import { readFileSync } from "node:fs";
|
|
10
|
+
import { fileURLToPath } from "node:url";
|
|
11
|
+
/** `core/policy/` resolved from compiled output (`dist/src/sandbox/`) or source. */
|
|
12
|
+
export const POLICY_DIR = fileURLToPath(new URL("../../../policy/", import.meta.url));
|
|
13
|
+
export const DEFAULT_POLICY_PATH = `${POLICY_DIR}run-sandbox.policy.json`;
|
|
14
|
+
export class PolicyError extends Error {
|
|
15
|
+
name = "PolicyError";
|
|
16
|
+
}
|
|
17
|
+
const TOP = new Set(["version", "filesystem_policy", "landlock", "process", "network_policies"]);
|
|
18
|
+
const FS = new Set(["include_workdir", "read_only", "read_write"]);
|
|
19
|
+
export function loadPolicyFile(path = DEFAULT_POLICY_PATH) {
|
|
20
|
+
let parsed;
|
|
21
|
+
try {
|
|
22
|
+
parsed = JSON.parse(readFileSync(path, "utf8"));
|
|
23
|
+
}
|
|
24
|
+
catch (error) {
|
|
25
|
+
throw new PolicyError(`cannot read policy ${path}: ${error.message}`);
|
|
26
|
+
}
|
|
27
|
+
return toSdkPolicy(parsed);
|
|
28
|
+
}
|
|
29
|
+
function strings(value, where) {
|
|
30
|
+
if (!Array.isArray(value) || !value.every((v) => typeof v === "string" && v.startsWith("/"))) {
|
|
31
|
+
throw new PolicyError(`${where} must be a list of absolute paths`);
|
|
32
|
+
}
|
|
33
|
+
return [...value];
|
|
34
|
+
}
|
|
35
|
+
function object(value, where) {
|
|
36
|
+
if (value === null || typeof value !== "object" || Array.isArray(value)) {
|
|
37
|
+
throw new PolicyError(`${where} must be an object`);
|
|
38
|
+
}
|
|
39
|
+
return value;
|
|
40
|
+
}
|
|
41
|
+
function onlyKeys(obj, allowed, where) {
|
|
42
|
+
for (const key of Object.keys(obj)) {
|
|
43
|
+
if (!allowed.has(key))
|
|
44
|
+
throw new PolicyError(`unsupported field ${where}${key}`);
|
|
45
|
+
}
|
|
46
|
+
}
|
|
47
|
+
function filesystemPolicy(value) {
|
|
48
|
+
const fs = object(value, "filesystem_policy");
|
|
49
|
+
onlyKeys(fs, FS, "filesystem_policy.");
|
|
50
|
+
return {
|
|
51
|
+
includeWorkdir: fs["include_workdir"] === undefined ? false : fs["include_workdir"] === true,
|
|
52
|
+
readOnly: fs["read_only"] === undefined ? [] : strings(fs["read_only"], "read_only"),
|
|
53
|
+
readWrite: fs["read_write"] === undefined ? [] : strings(fs["read_write"], "read_write"),
|
|
54
|
+
};
|
|
55
|
+
}
|
|
56
|
+
function landlockPolicy(value) {
|
|
57
|
+
const ll = object(value, "landlock");
|
|
58
|
+
onlyKeys(ll, new Set(["compatibility"]), "landlock.");
|
|
59
|
+
const c = ll["compatibility"];
|
|
60
|
+
if (c !== "best_effort" && c !== "hard_requirement") {
|
|
61
|
+
throw new PolicyError("landlock.compatibility must be best_effort or hard_requirement");
|
|
62
|
+
}
|
|
63
|
+
return { compatibility: c };
|
|
64
|
+
}
|
|
65
|
+
function processPolicy(value) {
|
|
66
|
+
const p = object(value, "process");
|
|
67
|
+
onlyKeys(p, new Set(["run_as_user", "run_as_group"]), "process.");
|
|
68
|
+
return {
|
|
69
|
+
runAsUser: typeof p["run_as_user"] === "string" ? p["run_as_user"] : "",
|
|
70
|
+
runAsGroup: typeof p["run_as_group"] === "string" ? p["run_as_group"] : "",
|
|
71
|
+
};
|
|
72
|
+
}
|
|
73
|
+
/**
|
|
74
|
+
* Default deny: the run policy grants no network rule of its own. Endpoints
|
|
75
|
+
* come only from attached provider profiles, which OpenShell composes in.
|
|
76
|
+
*/
|
|
77
|
+
function checkNetworkPolicies(value) {
|
|
78
|
+
const net = object(value, "network_policies");
|
|
79
|
+
if (Object.keys(net).length > 0) {
|
|
80
|
+
throw new PolicyError("network_policies must be empty: run sandboxes get network access only from provider profiles");
|
|
81
|
+
}
|
|
82
|
+
}
|
|
83
|
+
/** Validate an authored policy object and convert it to the SDK's create-time shape. */
|
|
84
|
+
export function toSdkPolicy(input) {
|
|
85
|
+
const doc = object(input, "policy");
|
|
86
|
+
onlyKeys(doc, TOP, "");
|
|
87
|
+
if (doc["version"] !== 1)
|
|
88
|
+
throw new PolicyError("policy version must be 1");
|
|
89
|
+
const out = { version: 1, networkPolicies: {} };
|
|
90
|
+
if (doc["filesystem_policy"] !== undefined)
|
|
91
|
+
out.filesystem = filesystemPolicy(doc["filesystem_policy"]);
|
|
92
|
+
if (doc["landlock"] !== undefined)
|
|
93
|
+
out.landlock = landlockPolicy(doc["landlock"]);
|
|
94
|
+
if (doc["process"] !== undefined)
|
|
95
|
+
out.process = processPolicy(doc["process"]);
|
|
96
|
+
if (doc["network_policies"] !== undefined)
|
|
97
|
+
checkNetworkPolicies(doc["network_policies"]);
|
|
98
|
+
return out;
|
|
99
|
+
}
|
|
100
|
+
//# sourceMappingURL=policy.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"policy.js","sourceRoot":"","sources":["../../../src/sandbox/policy.ts"],"names":[],"mappings":"AAAA,+EAA+E;AAC/E,8DAA8D;AAC9D,EAAE;AACF,qEAAqE;AACrE,2EAA2E;AAC3E,6EAA6E;AAC7E,wEAAwE;AACxE,8CAA8C;AAC9C,OAAO,EAAE,YAAY,EAAE,MAAM,SAAS,CAAC;AACvC,OAAO,EAAE,aAAa,EAAE,MAAM,UAAU,CAAC;AAIzC,oFAAoF;AACpF,MAAM,CAAC,MAAM,UAAU,GAAG,aAAa,CAAC,IAAI,GAAG,CAAC,kBAAkB,EAAE,MAAM,CAAC,IAAI,CAAC,GAAG,CAAC,CAAC,CAAC;AACtF,MAAM,CAAC,MAAM,mBAAmB,GAAG,GAAG,UAAU,yBAAyB,CAAC;AAE1E,MAAM,OAAO,WAAY,SAAQ,KAAK;IAClB,IAAI,GAAG,aAAa,CAAC;CACxC;AAED,MAAM,GAAG,GAAG,IAAI,GAAG,CAAC,CAAC,SAAS,EAAE,mBAAmB,EAAE,UAAU,EAAE,SAAS,EAAE,kBAAkB,CAAC,CAAC,CAAC;AACjG,MAAM,EAAE,GAAG,IAAI,GAAG,CAAC,CAAC,i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CAAC;IACxG,IAAI,GAAG,CAAC,UAAU,CAAC,KAAK,SAAS;QAAE,GAAG,CAAC,QAAQ,GAAG,cAAc,CAAC,GAAG,CAAC,UAAU,CAAC,CAAC,CAAC;IAClF,IAAI,GAAG,CAAC,SAAS,CAAC,KAAK,SAAS;QAAE,GAAG,CAAC,OAAO,GAAG,aAAa,CAAC,GAAG,CAAC,SAAS,CAAC,CAAC,CAAC;IAC9E,IAAI,GAAG,CAAC,kBAAkB,CAAC,KAAK,SAAS;QAAE,oBAAoB,CAAC,GAAG,CAAC,kBAAkB,CAAC,CAAC,CAAC;IACzF,OAAO,GAAG,CAAC;AACb,CAAC"}
|
|
@@ -0,0 +1,38 @@
|
|
|
1
|
+
import type { OpenShellClientLike } from "./openshell-client.js";
|
|
2
|
+
import type { ExecOptions, ExecResult, PortEndpoint, RunProcess, RunSandbox, RunSandboxState, SpawnOptions } from "./types.js";
|
|
3
|
+
/** Operator credentials that must never be passed into a sandbox, whatever the caller asks. */
|
|
4
|
+
export declare const SCRUBBED_ENV: readonly ["GH_TOKEN", "GITHUB_TOKEN", "SSH_AUTH_SOCK"];
|
|
5
|
+
/** Exit code a sentinel script returns when a path does not exist. */
|
|
6
|
+
export declare const NOT_FOUND_EXIT = 44;
|
|
7
|
+
/** Bytes per exec call when writing a file (kept well under gRPC message limits). */
|
|
8
|
+
export declare const WRITE_CHUNK: number;
|
|
9
|
+
/** Exit code reported for a process that ended without sending one (cancelled). */
|
|
10
|
+
export declare const CANCELLED_EXIT = 137;
|
|
11
|
+
export declare function shellQuote(value: string): string;
|
|
12
|
+
export declare function scrubEnv(env?: Readonly<Record<string, string>>): Record<string, string> | undefined;
|
|
13
|
+
/** Delete by name with retries and wait until gone; throws SandboxCleanupError. */
|
|
14
|
+
export type Deleter = (name: string, runId: string) => Promise<void>;
|
|
15
|
+
export declare class OpenShellRunSandbox implements RunSandbox {
|
|
16
|
+
#private;
|
|
17
|
+
readonly runId: string;
|
|
18
|
+
readonly name: string;
|
|
19
|
+
readonly home: string;
|
|
20
|
+
private readonly client;
|
|
21
|
+
private readonly deleter;
|
|
22
|
+
private readonly onDestroyed;
|
|
23
|
+
private readonly log?;
|
|
24
|
+
constructor(runId: string, name: string, home: string, client: OpenShellClientLike, deleter: Deleter, onDestroyed: () => void, log?: ((message: string) => void) | undefined);
|
|
25
|
+
get state(): RunSandboxState;
|
|
26
|
+
resolve(path: string): string;
|
|
27
|
+
exec(script: string, options?: ExecOptions): Promise<ExecResult>;
|
|
28
|
+
readFile(path: string, options?: {
|
|
29
|
+
signal?: AbortSignal;
|
|
30
|
+
}): Promise<Uint8Array | null>;
|
|
31
|
+
writeFile(path: string, content: Uint8Array, options?: {
|
|
32
|
+
signal?: AbortSignal;
|
|
33
|
+
}): Promise<void>;
|
|
34
|
+
spawn(command: string, options?: SpawnOptions): Promise<RunProcess>;
|
|
35
|
+
endpoint(port: number): Promise<PortEndpoint>;
|
|
36
|
+
stop(): Promise<void>;
|
|
37
|
+
destroy(): Promise<void>;
|
|
38
|
+
}
|