@inflexa-ai/harness 0.3.0 → 0.4.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.
|
@@ -20,7 +20,7 @@ import { clearSandboxRef } from "../state/index.js";
|
|
|
20
20
|
import { type AwaitExecOptions } from "./await-exec.js";
|
|
21
21
|
import type { SandboxClient } from "./client.js";
|
|
22
22
|
import { type SubmitExecDeps } from "./submit-exec.js";
|
|
23
|
-
import { type SandboxTransport } from "./types.js";
|
|
23
|
+
import { type CreateSandboxMeta, type SandboxTransport } from "./types.js";
|
|
24
24
|
/**
|
|
25
25
|
* Narrow config slice the sandbox factory reads off `Env` — the backend
|
|
26
26
|
* default and the K8s namespace fallback. Composition roots map their
|
|
@@ -62,6 +62,25 @@ export interface CreateSandboxClientConfig {
|
|
|
62
62
|
refStorePath?: string;
|
|
63
63
|
/** Docker: force the container platform (e.g. `linux/amd64`) so the sandbox matches the mounted lib store's arch. */
|
|
64
64
|
platform?: string;
|
|
65
|
+
/**
|
|
66
|
+
* Docker: unix socket of the Docker-API engine to dial — a Docker daemon
|
|
67
|
+
* socket or a podman Docker-compat socket. Unset preserves dockerode's
|
|
68
|
+
* default resolution (`DOCKER_HOST`, then the default Docker socket).
|
|
69
|
+
*/
|
|
70
|
+
engineSocketPath?: string;
|
|
71
|
+
/**
|
|
72
|
+
* Docker: the engine's bind mounts expose honest host file ownership to
|
|
73
|
+
* the container — podman machine's virtiofs and native Linux binds present
|
|
74
|
+
* the embedder user's real uid and modes, which the uid-1000 sandbox
|
|
75
|
+
* workload cannot write; Docker Desktop's file-sharing layer masks that
|
|
76
|
+
* mismatch, so it stays unset there. When set, the client loosens each
|
|
77
|
+
* step's pre-created write tree so workload writes land. The field names
|
|
78
|
+
* the engine fact rather than the remediation, so how the harness
|
|
79
|
+
* compensates can evolve without changing the embedder surface; a
|
|
80
|
+
* single-valued union (not a boolean) so future ownership classes extend
|
|
81
|
+
* it without a breaking change.
|
|
82
|
+
*/
|
|
83
|
+
engineBindOwnership?: "host-preserved";
|
|
65
84
|
/** K8s: PVC claim backing the session PVC the workspace roots live under. */
|
|
66
85
|
sessionPvc?: string;
|
|
67
86
|
/**
|
|
@@ -100,6 +119,25 @@ export interface CreateSandboxClientConfig {
|
|
|
100
119
|
* Exported for tests.
|
|
101
120
|
*/
|
|
102
121
|
export declare function composeAwaitOptions(base: AwaitExecOptions | undefined, transport: SandboxTransport, isAlive: NonNullable<AwaitExecOptions["isAlive"]>): AwaitExecOptions;
|
|
122
|
+
/**
|
|
123
|
+
* Backend-agnostic pre-creation of the writable step tree under the analysis's
|
|
124
|
+
* resolved workspace root. On Docker that root is the host dir the container
|
|
125
|
+
* binds; on K8s it lives under the session PVC the sandbox pod mounts via
|
|
126
|
+
* subPath. `mkdir(recursive)` is idempotent, so an existing tree (replay, retry)
|
|
127
|
+
* is not an error. A read-only sandbox has no writable step mount, so there is
|
|
128
|
+
* nothing to pre-create.
|
|
129
|
+
*
|
|
130
|
+
* `stepTreeAccess: "world-writable"` chmods the step dir and each subdir so the
|
|
131
|
+
* uid-1000 sandbox workload can write them through engines that honor host bind
|
|
132
|
+
* ownership. The chmod is explicit, not `mkdir`'s `mode` option: the process
|
|
133
|
+
* umask masks `mkdir`'s mode, and on replay the dirs already exist so `mkdir`
|
|
134
|
+
* would not touch them either way. Scoped to the step write tree — the
|
|
135
|
+
* read-only mount sources are never re-moded. Exported for tests.
|
|
136
|
+
*/
|
|
137
|
+
export declare function precreateStepTree(deps: {
|
|
138
|
+
resolveWorkspaceRoot: ResolveWorkspaceRoot;
|
|
139
|
+
stepTreeAccess?: "world-writable";
|
|
140
|
+
}, meta: CreateSandboxMeta): Promise<void>;
|
|
103
141
|
export declare function createSandboxClient(config: CreateSandboxClientConfig): SandboxClient;
|
|
104
142
|
/** Re-exports so consumers import once from this module. */
|
|
105
143
|
export { clearSandboxRef };
|
|
@@ -11,7 +11,7 @@
|
|
|
11
11
|
* coupling (they just call the closures we hand them). `submitExec` and
|
|
12
12
|
* `awaitExec` are backend-agnostic — they only need the SandboxRef.
|
|
13
13
|
*/
|
|
14
|
-
import { mkdir } from "node:fs/promises";
|
|
14
|
+
import { chmod, mkdir } from "node:fs/promises";
|
|
15
15
|
import { join } from "node:path";
|
|
16
16
|
import { clampResources } from "../config/resource-limits.js";
|
|
17
17
|
import { stepWritePrefix } from "../workspace/paths.js";
|
|
@@ -34,6 +34,36 @@ import { toPersistedRef } from "./types.js";
|
|
|
34
34
|
export function composeAwaitOptions(base, transport, isAlive) {
|
|
35
35
|
return { isAlive, ...base, transport };
|
|
36
36
|
}
|
|
37
|
+
/**
|
|
38
|
+
* Backend-agnostic pre-creation of the writable step tree under the analysis's
|
|
39
|
+
* resolved workspace root. On Docker that root is the host dir the container
|
|
40
|
+
* binds; on K8s it lives under the session PVC the sandbox pod mounts via
|
|
41
|
+
* subPath. `mkdir(recursive)` is idempotent, so an existing tree (replay, retry)
|
|
42
|
+
* is not an error. A read-only sandbox has no writable step mount, so there is
|
|
43
|
+
* nothing to pre-create.
|
|
44
|
+
*
|
|
45
|
+
* `stepTreeAccess: "world-writable"` chmods the step dir and each subdir so the
|
|
46
|
+
* uid-1000 sandbox workload can write them through engines that honor host bind
|
|
47
|
+
* ownership. The chmod is explicit, not `mkdir`'s `mode` option: the process
|
|
48
|
+
* umask masks `mkdir`'s mode, and on replay the dirs already exist so `mkdir`
|
|
49
|
+
* would not touch them either way. Scoped to the step write tree — the
|
|
50
|
+
* read-only mount sources are never re-moded. Exported for tests.
|
|
51
|
+
*/
|
|
52
|
+
export async function precreateStepTree(deps, meta) {
|
|
53
|
+
if (meta.readOnly)
|
|
54
|
+
return;
|
|
55
|
+
const stepDir = stepWritePrefix({
|
|
56
|
+
workspaceRoot: deps.resolveWorkspaceRoot(meta.analysisId),
|
|
57
|
+
runId: meta.runId,
|
|
58
|
+
stepId: meta.stepId,
|
|
59
|
+
});
|
|
60
|
+
await mkdir(stepDir, { recursive: true });
|
|
61
|
+
await Promise.all(STEP_SUBDIRS.map((sub) => mkdir(join(stepDir, sub), { recursive: true })));
|
|
62
|
+
if (deps.stepTreeAccess === "world-writable") {
|
|
63
|
+
await chmod(stepDir, 0o777);
|
|
64
|
+
await Promise.all(STEP_SUBDIRS.map((sub) => chmod(join(stepDir, sub), 0o777)));
|
|
65
|
+
}
|
|
66
|
+
}
|
|
37
67
|
export function createSandboxClient(config) {
|
|
38
68
|
const backend = config.backend ?? config.env.backend;
|
|
39
69
|
const transport = config.transport ?? "poll";
|
|
@@ -64,26 +94,10 @@ export function createSandboxClient(config) {
|
|
|
64
94
|
libStorePath: config.libStorePath,
|
|
65
95
|
refStorePath: config.refStorePath,
|
|
66
96
|
platform: config.platform,
|
|
97
|
+
engineSocketPath: config.engineSocketPath,
|
|
67
98
|
logger: config.logger,
|
|
68
99
|
registerSandbox,
|
|
69
100
|
});
|
|
70
|
-
// Backend-agnostic pre-creation of the writable step tree under the
|
|
71
|
-
// analysis's resolved workspace root. On Docker that root is the host dir
|
|
72
|
-
// the container binds; on K8s it lives under the session PVC the sandbox
|
|
73
|
-
// pod mounts via subPath. mkdir(recursive) is idempotent, so an existing
|
|
74
|
-
// tree (replay, retry) is not an error.
|
|
75
|
-
const precreateStepTree = async (meta) => {
|
|
76
|
-
// A read-only sandbox has no writable step mount — nothing to pre-create.
|
|
77
|
-
if (meta.readOnly)
|
|
78
|
-
return;
|
|
79
|
-
const stepDir = stepWritePrefix({
|
|
80
|
-
workspaceRoot: config.resolveWorkspaceRoot(meta.analysisId),
|
|
81
|
-
runId: meta.runId,
|
|
82
|
-
stepId: meta.stepId,
|
|
83
|
-
});
|
|
84
|
-
await mkdir(stepDir, { recursive: true });
|
|
85
|
-
await Promise.all(STEP_SUBDIRS.map((sub) => mkdir(join(stepDir, sub), { recursive: true })));
|
|
86
|
-
};
|
|
87
101
|
// `teardown` needs the registry-clear closure but the SandboxRef alone
|
|
88
102
|
// doesn't carry runId/stepId. Wrap the backend impl so callers pass the
|
|
89
103
|
// ref + step coordinates and the wrapper does the clear after the
|
|
@@ -109,7 +123,13 @@ export function createSandboxClient(config) {
|
|
|
109
123
|
const isAlive = async (ref) => unwrapOrThrow(await ops.isAlive(ref));
|
|
110
124
|
return {
|
|
111
125
|
createSandbox: async (meta, identity) => {
|
|
112
|
-
|
|
126
|
+
// The config states an engine fact (binds preserve host ownership);
|
|
127
|
+
// which remediation that fact demands — today, a world-writable step
|
|
128
|
+
// write tree — is harness-owned and chosen here, not by the embedder.
|
|
129
|
+
await precreateStepTree({
|
|
130
|
+
resolveWorkspaceRoot: config.resolveWorkspaceRoot,
|
|
131
|
+
stepTreeAccess: config.engineBindOwnership === "host-preserved" ? "world-writable" : undefined,
|
|
132
|
+
}, meta);
|
|
113
133
|
// Every caller must declare resources — a sandbox with no cpu/memory
|
|
114
134
|
// request is a semantic error, not something to paper over with a
|
|
115
135
|
// default (a DBOS replay of a pre-resources workflow input lands here).
|
|
@@ -53,6 +53,13 @@ export interface DockerClientConfig {
|
|
|
53
53
|
* create rather than crashing inside the sandbox.
|
|
54
54
|
*/
|
|
55
55
|
platform?: string;
|
|
56
|
+
/**
|
|
57
|
+
* Unix socket of the Docker-API engine to dial — a Docker daemon socket or a
|
|
58
|
+
* podman Docker-compat socket (both serve the identical REST API). Unset
|
|
59
|
+
* preserves dockerode's default resolution (`DOCKER_HOST`, then the default
|
|
60
|
+
* Docker socket).
|
|
61
|
+
*/
|
|
62
|
+
engineSocketPath?: string;
|
|
56
63
|
/** Injected for tests. */
|
|
57
64
|
docker?: Docker;
|
|
58
65
|
/** Injected for tests so `/health` polling can be stubbed. */
|
|
@@ -66,6 +73,14 @@ export interface DockerClientConfig {
|
|
|
66
73
|
/** Hook called after the registry row is written. */
|
|
67
74
|
registerSandbox: (meta: CreateSandboxMeta, ref: SandboxRef) => Promise<void>;
|
|
68
75
|
}
|
|
76
|
+
/**
|
|
77
|
+
* dockerode connection options for a configured engine socket, or `undefined`
|
|
78
|
+
* to leave construction as a bare `new Docker()` — which preserves dockerode's
|
|
79
|
+
* default resolution (`DOCKER_HOST`, then the default Docker socket). A set
|
|
80
|
+
* `engineSocketPath` dials that unix socket instead (a Docker daemon or a
|
|
81
|
+
* podman Docker-compat socket).
|
|
82
|
+
*/
|
|
83
|
+
export declare function engineConnectionOptions(engineSocketPath: string | undefined): Docker.DockerOptions | undefined;
|
|
69
84
|
export declare function createDockerSandboxOps(config: DockerClientConfig): {
|
|
70
85
|
createSandbox(meta: CreateSandboxMeta, identity: SandboxIdentity): ResultAsync<SandboxRef, SandboxError>;
|
|
71
86
|
teardown(ref: SandboxRef): ResultAsync<void, SandboxError>;
|
|
@@ -97,36 +97,58 @@ async function pollHealth(fetchImpl, url, timeoutMs) {
|
|
|
97
97
|
}
|
|
98
98
|
throw new Error(`sandbox /health did not return 200 within ${timeoutMs}ms: ${lastErr instanceof Error ? lastErr.message : String(lastErr)}`);
|
|
99
99
|
}
|
|
100
|
+
/**
|
|
101
|
+
* dockerode connection options for a configured engine socket, or `undefined`
|
|
102
|
+
* to leave construction as a bare `new Docker()` — which preserves dockerode's
|
|
103
|
+
* default resolution (`DOCKER_HOST`, then the default Docker socket). A set
|
|
104
|
+
* `engineSocketPath` dials that unix socket instead (a Docker daemon or a
|
|
105
|
+
* podman Docker-compat socket).
|
|
106
|
+
*/
|
|
107
|
+
export function engineConnectionOptions(engineSocketPath) {
|
|
108
|
+
return engineSocketPath === undefined ? undefined : { socketPath: engineSocketPath };
|
|
109
|
+
}
|
|
100
110
|
/**
|
|
101
111
|
* Create the container, or adopt the one already standing under this
|
|
102
|
-
* checkpointed name
|
|
103
|
-
*
|
|
104
|
-
*
|
|
105
|
-
* name
|
|
112
|
+
* checkpointed name on a recovery re-run. Any create failure triggers an
|
|
113
|
+
* inspect of the name: engines disagree on the duplicate-name status (Docker
|
|
114
|
+
* answers 409, podman's compat API 500), so the reconcile keys on whether a
|
|
115
|
+
* container stands under the name, not on a status code. A standing container
|
|
116
|
+
* this workflow owns is adopted as-is if running, removed and recreated if
|
|
117
|
+
* stopped; one owned by a different workflow is a refused name collision — never
|
|
118
|
+
* adopt or remove someone else's container. When no container stands, the
|
|
119
|
+
* failure was not a name collision and the original create error is returned;
|
|
120
|
+
* the inspect's own failure is never surfaced, so a transient engine outage
|
|
121
|
+
* (both calls fail) still reports the create failure.
|
|
106
122
|
*/
|
|
107
|
-
async function createOrAdopt(docker, createOpts, name, ownerWorkflowId, createFailed) {
|
|
123
|
+
async function createOrAdopt(docker, createOpts, name, ownerWorkflowId, createFailed, logger) {
|
|
108
124
|
const created = await trySandbox(() => docker.createContainer(createOpts), createFailed);
|
|
109
125
|
if (created.isOk())
|
|
110
126
|
return ok({ container: created.value, alreadyRunning: false });
|
|
111
|
-
if (statusOf(created.error) !== 409)
|
|
112
|
-
return err(created.error);
|
|
113
127
|
const existing = docker.getContainer(name);
|
|
114
128
|
const inspected = await trySandbox(() => existing.inspect(), createFailed);
|
|
129
|
+
// No container stands under the name → the create failure was not a name
|
|
130
|
+
// collision, so surface the original create error rather than the inspect's.
|
|
115
131
|
if (inspected.isErr())
|
|
116
|
-
return err(
|
|
132
|
+
return err(created.error);
|
|
117
133
|
const info = inspected.value;
|
|
118
134
|
const owner = info.Config?.Labels?.[OWNER_WORKFLOW_LABEL];
|
|
119
135
|
if (owner !== ownerWorkflowId) {
|
|
120
136
|
return err({ type: "name_conflict", op: "docker.createSandbox", sandboxId: name, owner: owner ?? null });
|
|
121
137
|
}
|
|
122
|
-
|
|
138
|
+
// Both mutating reconcile outcomes are logged: adoption reuses a container the
|
|
139
|
+
// current attempt did not create, so without a record here nothing ties the
|
|
140
|
+
// step's sandbox back to the prior attempt that made it.
|
|
141
|
+
if (info.State.Running) {
|
|
142
|
+
logger.info("adopted standing container on recovery re-run", { sandboxId: name, ownerWorkflowId });
|
|
123
143
|
return ok({ container: existing, alreadyRunning: true });
|
|
144
|
+
}
|
|
124
145
|
const removed = await removeContainerIgnoreMissing(docker, name);
|
|
125
146
|
if (removed.isErr())
|
|
126
147
|
return err(removed.error);
|
|
127
148
|
const recreated = await trySandbox(() => docker.createContainer(createOpts), createFailed);
|
|
128
149
|
if (recreated.isErr())
|
|
129
150
|
return err(recreated.error);
|
|
151
|
+
logger.info("removed stopped prior attempt and recreated", { sandboxId: name, ownerWorkflowId });
|
|
130
152
|
return ok({ container: recreated.value, alreadyRunning: false });
|
|
131
153
|
}
|
|
132
154
|
function removeContainerIgnoreMissing(docker, sandboxId) {
|
|
@@ -145,7 +167,11 @@ function removeContainerIgnoreMissing(docker, sandboxId) {
|
|
|
145
167
|
statusOf(e) === 404 ? ok(undefined) : err(e));
|
|
146
168
|
}
|
|
147
169
|
export function createDockerSandboxOps(config) {
|
|
148
|
-
|
|
170
|
+
// The injected test instance wins; otherwise a configured socket dials that
|
|
171
|
+
// engine and an unset one stays a bare `new Docker()` (default resolution).
|
|
172
|
+
const connection = engineConnectionOptions(config.engineSocketPath);
|
|
173
|
+
const docker = config.docker ?? (connection ? new Docker(connection) : new Docker());
|
|
174
|
+
const logger = (config.logger ?? createNoopLogger()).named("docker-client");
|
|
149
175
|
const fetchImpl = config.fetch ?? fetch;
|
|
150
176
|
const transport = config.transport ?? "poll";
|
|
151
177
|
const pollMode = transport === "poll";
|
|
@@ -159,9 +185,7 @@ export function createDockerSandboxOps(config) {
|
|
|
159
185
|
// since an otherwise-silent libs-mount drop is invisible to operators.
|
|
160
186
|
const libsMounted = !!config.libStorePath && libStoreUsable(config.libStorePath);
|
|
161
187
|
if (config.libStorePath && !libsMounted) {
|
|
162
|
-
(config.
|
|
163
|
-
.named("docker-client")
|
|
164
|
-
.warn("lib store configured but `current` is missing or incomplete at sandbox creation — mounting no library store (sandbox degrades to available:false)", { libStorePath: config.libStorePath, sandboxId });
|
|
188
|
+
logger.warn("lib store configured but `current` is missing or incomplete at sandbox creation — mounting no library store (sandbox degrades to available:false)", { libStorePath: config.libStorePath, sandboxId });
|
|
165
189
|
}
|
|
166
190
|
const plan = buildMountPlan(meta, {
|
|
167
191
|
libs: libsMounted,
|
|
@@ -234,7 +258,7 @@ export function createDockerSandboxOps(config) {
|
|
|
234
258
|
AutoRemove: false,
|
|
235
259
|
},
|
|
236
260
|
};
|
|
237
|
-
const sandbox = await createOrAdopt(docker, createOpts, sandboxId, meta.childWorkflowId, createFailed);
|
|
261
|
+
const sandbox = await createOrAdopt(docker, createOpts, sandboxId, meta.childWorkflowId, createFailed, logger);
|
|
238
262
|
if (sandbox.isErr())
|
|
239
263
|
return err(sandbox.error);
|
|
240
264
|
if (!sandbox.value.alreadyRunning) {
|
package/package.json
CHANGED