@runuai/host 0.9.68 → 0.9.70

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.
@@ -0,0 +1,334 @@
1
+ /**
2
+ * ADR-121: the `local` MachineProvider — task "machines" as containers on
3
+ * the local Docker daemon, so the whole machine-backed path (launch, in-env
4
+ * clone, SSH exec, tunnels, recovery-by-describe) is exercised on the
5
+ * uai-linux guinea pig VM with zero cloud spend.
6
+ *
7
+ * Machines are containers named `uai-machine-<taskId>` carrying the label
8
+ * `com.uai.machine=<taskId>`. The label is deliberately NOT `com.uai.task`:
9
+ * task-app containers own that label and every existing sweep/proof keys on
10
+ * it — a machine must never be mistaken for an app container by GC that
11
+ * predates machines.
12
+ *
13
+ * Absence grammar: `docker inspect` answers a missing object with exit 1,
14
+ * empty stdout, and an "Error: No such object: <id>" line naming the EXACT
15
+ * id. Only that whole answer proves absence (a not-found for "<id>-shadow"
16
+ * proves nothing about "<id>"); anything else is `unknown`.
17
+ */
18
+
19
+ import { dockerCli, type DockerResult } from "./docker-exec";
20
+ import type {
21
+ MachineInfo,
22
+ MachineProvider,
23
+ MachineSpec,
24
+ MachineState,
25
+ } from "./machine-provider";
26
+
27
+ export const MACHINE_LABEL = "com.uai.machine";
28
+
29
+ export function localMachineName(taskId: string): string {
30
+ return `uai-machine-${taskId.toLowerCase()}`;
31
+ }
32
+
33
+ type Runner = (args: string[]) => Promise<DockerResult>;
34
+
35
+ const defaultRunner: Runner = (args) => dockerCli(args, { timeoutMs: 60_000 });
36
+
37
+ function machineAbsent(id: string, res: DockerResult): boolean {
38
+ // Daemons vary the casing of the not-found line — Docker Desktop answers
39
+ // "Error: No such object:", OrbStack "error: no such object:" (caught
40
+ // live in the first smoke run: the machine WAS gone and the proof
41
+ // refused it). The prefix is matched case-insensitively; the id itself
42
+ // stays exact — a not-found for "<id>-shadow" still proves nothing.
43
+ // OrbStack also answers with "[]" on stdout beside the error (Docker
44
+ // Desktop prints nothing) — an empty JSON array carries no confusable
45
+ // record, so both are admissible; anything else on stdout is not.
46
+ const stdout = res.stdout.trim();
47
+ return (
48
+ typeof res.status === "number" &&
49
+ res.status !== 0 &&
50
+ (stdout === "" || stdout === "[]") &&
51
+ res.stderr.split("\n").some((line) => {
52
+ const match = /^error: no such object: (.+)$/i.exec(line.trim());
53
+ return match !== null && match[1] === id;
54
+ })
55
+ );
56
+ }
57
+
58
+ function stateFromDocker(status: unknown): MachineState {
59
+ switch (status) {
60
+ case "running":
61
+ return "running";
62
+ case "created":
63
+ case "restarting":
64
+ return "pending";
65
+ case "paused":
66
+ case "exited":
67
+ case "dead":
68
+ return "stopped";
69
+ default:
70
+ return "unknown";
71
+ }
72
+ }
73
+
74
+ interface InspectShape {
75
+ name: string;
76
+ taskLabel: string | null;
77
+ state: MachineState;
78
+ address: string | null;
79
+ sshPort: number | null;
80
+ }
81
+
82
+ /** Strict single-record parse. Exactly one record whose Name is the
83
+ * requested id — a confused or multi-record answer proves nothing and maps
84
+ * to null (the caller reports `unknown`). */
85
+ function parseInspect(id: string, stdout: string): InspectShape | null {
86
+ let parsed: unknown;
87
+ try {
88
+ parsed = JSON.parse(stdout);
89
+ } catch {
90
+ return null;
91
+ }
92
+ if (!Array.isArray(parsed) || parsed.length !== 1) return null;
93
+ const record = parsed[0];
94
+ if (typeof record !== "object" || record === null) return null;
95
+ const rawName = (record as { Name?: unknown }).Name;
96
+ if (typeof rawName !== "string") return null;
97
+ const name = rawName.replace(/^\//, "");
98
+ if (name !== id) return null;
99
+ const stateObj = (record as { State?: { Status?: unknown } }).State;
100
+ const labels = (record as { Config?: { Labels?: unknown } }).Config?.Labels;
101
+ let taskLabel: string | null = null;
102
+ if (labels !== undefined && labels !== null) {
103
+ if (typeof labels !== "object" || Array.isArray(labels)) return null;
104
+ const value = (labels as Record<string, unknown>)[MACHINE_LABEL];
105
+ if (value !== undefined && typeof value !== "string") return null;
106
+ taskLabel = typeof value === "string" ? value : null;
107
+ }
108
+ const settings = (
109
+ record as {
110
+ NetworkSettings?: {
111
+ Networks?: Record<string, { IPAddress?: unknown }>;
112
+ Ports?: Record<
113
+ string,
114
+ Array<{ HostIp?: unknown; HostPort?: unknown }> | null
115
+ >;
116
+ };
117
+ }
118
+ ).NetworkSettings;
119
+ let address: string | null = null;
120
+ const networks = settings?.Networks;
121
+ if (networks && typeof networks === "object") {
122
+ for (const net of Object.values(networks)) {
123
+ const ip = net?.IPAddress;
124
+ if (typeof ip === "string" && ip.length > 0) {
125
+ address = ip;
126
+ break;
127
+ }
128
+ }
129
+ }
130
+ // A published SSH port (macOS mode) supersedes the bridge IP: the bridge
131
+ // address exists but is unreachable from the Mac, and half-truthful
132
+ // reachability info is worse than none.
133
+ let sshPort: number | null = null;
134
+ const published = settings?.Ports?.["22/tcp"];
135
+ if (Array.isArray(published)) {
136
+ for (const binding of published) {
137
+ const port = Number(binding?.HostPort);
138
+ if (Number.isInteger(port) && port > 0) {
139
+ address = typeof binding?.HostIp === "string" && binding.HostIp.length > 0
140
+ ? binding.HostIp
141
+ : "127.0.0.1";
142
+ sshPort = port;
143
+ break;
144
+ }
145
+ }
146
+ }
147
+ return {
148
+ name,
149
+ taskLabel,
150
+ state: stateFromDocker(stateObj?.Status),
151
+ address,
152
+ sshPort,
153
+ };
154
+ }
155
+
156
+ function unknown(id: string, detail: string): MachineInfo {
157
+ return { id, taskLabel: null, state: "unknown", address: null, detail };
158
+ }
159
+
160
+ export function createLocalMachineProvider(
161
+ runner: Runner = defaultRunner,
162
+ ): MachineProvider {
163
+ async function describe(id: string): Promise<MachineInfo> {
164
+ const res = await runner(["inspect", id]);
165
+ if (res.status === 0) {
166
+ const record = parseInspect(id, res.stdout);
167
+ if (record === null) {
168
+ return unknown(id, "inspect answered with a confusing record");
169
+ }
170
+ return {
171
+ id,
172
+ taskLabel: record.taskLabel,
173
+ state: record.state,
174
+ address: record.state === "running" ? record.address : null,
175
+ ...(record.state === "running" && record.sshPort !== null
176
+ ? { sshPort: record.sshPort }
177
+ : {}),
178
+ };
179
+ }
180
+ if (machineAbsent(id, res)) {
181
+ return { id, taskLabel: null, state: "absent", address: null };
182
+ }
183
+ return unknown(
184
+ id,
185
+ res.stderr.trim() || `inspect exited ${res.status ?? "killed"}`,
186
+ );
187
+ }
188
+
189
+ return {
190
+ kind: "local",
191
+
192
+ async launch(spec: MachineSpec): Promise<MachineInfo> {
193
+ const name = localMachineName(spec.taskId);
194
+ const args = [
195
+ "run",
196
+ "-d",
197
+ "--name",
198
+ name,
199
+ "--label",
200
+ `${MACHINE_LABEL}=${spec.taskId}`,
201
+ "--cpus",
202
+ String(spec.cpus),
203
+ "--memory",
204
+ `${spec.memoryMiB}m`,
205
+ // The image's entrypoint installs this for the ssh user before sshd
206
+ // starts — the local analog of cloud user-data key delivery.
207
+ ...(spec.authorizedPublicKey
208
+ ? ["-e", `UAI_AUTHORIZED_KEY=${spec.authorizedPublicKey}`]
209
+ : []),
210
+ // macOS: bridge IPs are not host-routable; SSH rides a
211
+ // loopback-published ephemeral port instead (never exposed beyond
212
+ // 127.0.0.1).
213
+ ...(spec.publishSsh ? ["-p", "127.0.0.1:0:22"] : []),
214
+ spec.image,
215
+ ...(spec.command ?? []),
216
+ ];
217
+ const res = await runner(args);
218
+ if (res.status !== 0) {
219
+ throw new Error(
220
+ `machine launch failed for ${name}: ${
221
+ res.stderr.trim() || `exit ${res.status ?? "killed"}`
222
+ }`,
223
+ );
224
+ }
225
+ const info = await describe(name);
226
+ if (info.state === "absent" || info.state === "unknown") {
227
+ throw new Error(
228
+ `machine ${name} launched but could not be described (${info.state})`,
229
+ );
230
+ }
231
+ return info;
232
+ },
233
+
234
+ async stop(id: string): Promise<void> {
235
+ const res = await runner(["stop", "--time", "10", id]);
236
+ if (res.status !== 0) {
237
+ throw new Error(
238
+ `machine stop failed for ${id}: ${
239
+ res.stderr.trim() || `exit ${res.status ?? "killed"}`
240
+ }`,
241
+ );
242
+ }
243
+ },
244
+
245
+ async start(id: string): Promise<MachineInfo> {
246
+ const res = await runner(["start", id]);
247
+ if (res.status !== 0) {
248
+ throw new Error(
249
+ `machine start failed for ${id}: ${
250
+ res.stderr.trim() || `exit ${res.status ?? "killed"}`
251
+ }`,
252
+ );
253
+ }
254
+ return describe(id);
255
+ },
256
+
257
+ async terminate(id: string): Promise<void> {
258
+ // Removal is allowed to fail (already gone, races) — absence is not.
259
+ await runner(["rm", "-f", id]);
260
+ const after = await describe(id);
261
+ if (after.state !== "absent") {
262
+ throw new Error(
263
+ `machine ${id} could not be proven absent after terminate (${after.state}${
264
+ after.detail ? `: ${after.detail}` : ""
265
+ })`,
266
+ );
267
+ }
268
+ },
269
+
270
+ describe,
271
+
272
+ async list(): Promise<MachineInfo[]> {
273
+ const ids = await runner([
274
+ "ps",
275
+ "-a",
276
+ "--filter",
277
+ `label=${MACHINE_LABEL}`,
278
+ "--format",
279
+ "{{.Names}}",
280
+ ]);
281
+ if (ids.status !== 0) {
282
+ throw new Error(
283
+ `machine listing failed: ${
284
+ ids.stderr.trim() || `exit ${ids.status ?? "killed"}`
285
+ }`,
286
+ );
287
+ }
288
+ const names = ids.stdout
289
+ .split("\n")
290
+ .map((line) => line.trim())
291
+ .filter(Boolean);
292
+ if (names.length === 0) return [];
293
+ const inspected = await runner(["inspect", ...names]);
294
+ if (inspected.status !== 0) {
295
+ throw new Error(
296
+ `machine inventory inspect failed: ${
297
+ inspected.stderr.trim() || `exit ${inspected.status ?? "killed"}`
298
+ }`,
299
+ );
300
+ }
301
+ let parsed: unknown;
302
+ try {
303
+ parsed = JSON.parse(inspected.stdout);
304
+ } catch {
305
+ throw new Error("machine inventory was unparseable");
306
+ }
307
+ if (!Array.isArray(parsed) || parsed.length !== names.length) {
308
+ throw new Error("machine inventory was unparseable");
309
+ }
310
+ const machines: MachineInfo[] = [];
311
+ for (let index = 0; index < names.length; index += 1) {
312
+ const record = parseInspect(
313
+ names[index]!,
314
+ JSON.stringify([parsed[index]]),
315
+ );
316
+ if (record === null) {
317
+ // A partially-readable inventory could hide exactly the labeled
318
+ // machine an orphan proof needs to see — no answer at all.
319
+ throw new Error("machine inventory was unparseable");
320
+ }
321
+ machines.push({
322
+ id: record.name,
323
+ taskLabel: record.taskLabel,
324
+ state: record.state,
325
+ address: record.state === "running" ? record.address : null,
326
+ ...(record.state === "running" && record.sshPort !== null
327
+ ? { sshPort: record.sshPort }
328
+ : {}),
329
+ });
330
+ }
331
+ return machines;
332
+ },
333
+ };
334
+ }
@@ -0,0 +1,86 @@
1
+ /**
2
+ * ADR-121: the per-task MACHINE lifecycle seam.
3
+ *
4
+ * A "machine" is the isolated computer a task runs on — an EC2 instance, a
5
+ * GCP instance, or (for the zero-cloud-spend guinea pig) an SSH-reachable
6
+ * container on the local daemon. The seam is deliberately small enough that
7
+ * nothing in it is EC2-specific: launch / stop / start / terminate /
8
+ * describe / list, with an ownership tag and a reachable address.
9
+ *
10
+ * State discipline (this repo's absence rule, applied to machines):
11
+ * - `absent` means PROVEN absent — the provider observed its backend's exact
12
+ * not-found answer for this exact id. Only `absent` authorizes forgetting
13
+ * a machine or deleting anything derived from it.
14
+ * - `unknown` means the query failed or answered confusingly. Unknown is
15
+ * never absence; callers defer and retry.
16
+ * - `terminate` does not return until absence is proven; an unprovable
17
+ * termination throws rather than reporting success.
18
+ */
19
+
20
+ export type MachineState =
21
+ | "pending"
22
+ | "running"
23
+ | "stopped"
24
+ | "absent"
25
+ | "unknown";
26
+
27
+ export interface MachineSpec {
28
+ /** Task ownership — recorded on the machine (tag/label) so recovery and
29
+ * orphan sweeps key on it, mirroring `com.uai.task` on containers. */
30
+ taskId: string;
31
+ /** Backend image reference: AMI id, GCP image, or container image. */
32
+ image: string;
33
+ cpus: number;
34
+ memoryMiB: number;
35
+ /** Optional long-running entrypoint override (the local backend's images
36
+ * must keep the machine alive themselves — typically sshd). */
37
+ command?: string[];
38
+ /** Public key the machine authorizes for the orchestrator's SSH access.
39
+ * Cloud backends deliver it via instance user-data; the local backend via
40
+ * an env var its image's entrypoint installs. */
41
+ authorizedPublicKey?: string;
42
+ /** Local backend on macOS only: container bridge IPs are not
43
+ * host-routable there, so SSH must ride a loopback-published port. The
44
+ * Linux guinea pig and cloud backends dial the address directly. */
45
+ publishSsh?: boolean;
46
+ }
47
+
48
+ export interface MachineInfo {
49
+ /** Backend-native identifier: instance id or container id/name. */
50
+ id: string;
51
+ /** The recorded task ownership label, null when the machine carries none
52
+ * (a foreign machine occupying a reserved name is never ours to touch). */
53
+ taskLabel: string | null;
54
+ state: MachineState;
55
+ /** Address the orchestrator can dial for SSH/exec/tunnels, when running.
56
+ * Reachability is a deployment property: VPC-private IPs (cloud) and
57
+ * bridge IPs (local daemon on the SAME machine as the orchestrator) are
58
+ * reachable; container bridge IPs are NOT host-routable from macOS —
59
+ * the local backend is for the Linux guinea pig, not Mac hosts. */
60
+ address: string | null;
61
+ /** SSH port when it differs from 22 (local backend with `publishSsh`). */
62
+ sshPort?: number;
63
+ /** Present only alongside `unknown` — what the backend actually said. */
64
+ detail?: string;
65
+ }
66
+
67
+ export interface MachineProvider {
68
+ readonly kind: "local" | "aws" | "gcp";
69
+ /** Create and start a machine. Rejects on a name/identity conflict —
70
+ * adopting an existing machine is a caller decision, never implicit. */
71
+ launch(spec: MachineSpec): Promise<MachineInfo>;
72
+ /** Graceful stop (sleep-when-idle; cloud backends keep the disk). */
73
+ stop(id: string): Promise<void>;
74
+ /** Start a stopped machine and report its (possibly new) address. */
75
+ start(id: string): Promise<MachineInfo>;
76
+ /** Destroy the machine. Resolves only after absence is PROVEN. */
77
+ terminate(id: string): Promise<void>;
78
+ /** One machine's state. `absent` only on the backend's exact not-found
79
+ * answer for this exact id; anything else unprovable is `unknown`. */
80
+ describe(id: string): Promise<MachineInfo>;
81
+ /** Every machine carrying this provider's ownership labels — the orphan
82
+ * sweep's inventory. A partially-readable answer is no answer: malformed
83
+ * rows reject the whole listing (they could hide exactly the labeled
84
+ * machine a proof needs to see). */
85
+ list(): Promise<MachineInfo[]>;
86
+ }
@@ -22,6 +22,10 @@ import { lstatSync } from "node:fs";
22
22
  import path from "node:path";
23
23
 
24
24
  import type { TaskDownResult } from "../agent";
25
+ import {
26
+ hostWorkspaceRead,
27
+ hostWorkspaceWrite,
28
+ } from "./workspace-files";
25
29
  import { assertSafeHostTaskId } from "../task-identity";
26
30
  import {
27
31
  capturedCliExec,
@@ -272,6 +276,23 @@ class AppleContainerTaskEnvironmentHandle
272
276
  };
273
277
  }
274
278
 
279
+ async readWorkspaceFile(filePath: string): Promise<Uint8Array | null> {
280
+ return hostWorkspaceRead(
281
+ this.descriptor,
282
+ path.join(this.#value.hostWorktreePath, "workspace"),
283
+ filePath,
284
+ );
285
+ }
286
+
287
+ async writeWorkspaceFile(filePath: string, bytes: Uint8Array): Promise<void> {
288
+ hostWorkspaceWrite(
289
+ this.descriptor,
290
+ path.join(this.#value.hostWorktreePath, "workspace"),
291
+ filePath,
292
+ bytes,
293
+ );
294
+ }
295
+
275
296
  exec(request: TaskEnvironmentExecRequest): Promise<TaskEnvironmentExecResult> {
276
297
  assertTaskEnvironmentProcessRequest(request);
277
298
  return capturedCliExec(
@@ -15,6 +15,10 @@ import {
15
15
  type TaskUpResult,
16
16
  } from "../agent";
17
17
  import { dockerCli, type DockerResult } from "../docker-exec";
18
+ import {
19
+ hostWorkspaceRead,
20
+ hostWorkspaceWrite,
21
+ } from "./workspace-files";
18
22
  import { taskDir } from "../env";
19
23
  import { PREVIEW_SIDECAR_TASK_LABEL } from "../preview-sidecar";
20
24
  import { assertSafeHostTaskId } from "../task-identity";
@@ -271,6 +275,23 @@ class DockerTaskEnvironmentHandle
271
275
  };
272
276
  }
273
277
 
278
+ async readWorkspaceFile(filePath: string): Promise<Uint8Array | null> {
279
+ return hostWorkspaceRead(
280
+ this.descriptor,
281
+ path.join(this.#value.hostWorktreePath, "workspace"),
282
+ filePath,
283
+ );
284
+ }
285
+
286
+ async writeWorkspaceFile(filePath: string, bytes: Uint8Array): Promise<void> {
287
+ hostWorkspaceWrite(
288
+ this.descriptor,
289
+ path.join(this.#value.hostWorktreePath, "workspace"),
290
+ filePath,
291
+ bytes,
292
+ );
293
+ }
294
+
274
295
  exec(request: TaskEnvironmentExecRequest): Promise<TaskEnvironmentExecResult> {
275
296
  assertTaskEnvironmentProcessRequest(request);
276
297
  return capturedCliExec(