@parcha/agentrun-computer 0.1.0-beta.36 → 0.1.0-beta.38
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/NOTICE +2 -0
- package/README.md +547 -478
- package/box/Dockerfile +18 -1
- package/box/README.md +13 -0
- package/box/build-agent37.mjs +158 -0
- package/box/build-daytona.py +2 -2
- package/box/build-e2b.mjs +41 -18
- package/box/recipe.mjs +43 -0
- package/dist/agent37-ssh.d.ts +89 -0
- package/dist/agent37-ssh.js +445 -0
- package/dist/agent37.d.ts +104 -0
- package/dist/agent37.js +402 -0
- package/dist/attach.d.ts +146 -0
- package/dist/attach.js +469 -0
- package/dist/browserbase.d.ts +2 -10
- package/dist/browserbase.js +3 -6
- package/dist/client.d.ts +45 -4
- package/dist/client.js +92 -4
- package/dist/computerd-linux-amd64 +0 -0
- package/dist/computerd-linux-arm64 +0 -0
- package/dist/computerd.json +4 -0
- package/dist/conformance/agent37.d.ts +96 -0
- package/dist/conformance/agent37.js +154 -0
- package/dist/conformance/cases/apps.d.ts +11 -0
- package/dist/conformance/cases/apps.js +173 -73
- package/dist/conformance/cases/health.js +14 -0
- package/dist/conformance/cases/index.js +2 -0
- package/dist/conformance/cases/isolation.d.ts +7 -5
- package/dist/conformance/cases/isolation.js +44 -14
- package/dist/conformance/cases/mount.js +1 -1
- package/dist/conformance/cases/provider.js +27 -1
- package/dist/conformance/cases/screen.d.ts +3 -0
- package/dist/conformance/cases/screen.js +18 -0
- package/dist/conformance/cases/settings.d.ts +29 -23
- package/dist/conformance/cases/settings.js +76 -58
- package/dist/conformance/cases/touch.js +19 -17
- package/dist/conformance/cases/ui.d.ts +2 -0
- package/dist/conformance/cases/ui.js +475 -0
- package/dist/conformance/cli.d.ts +9 -0
- package/dist/conformance/cli.js +94 -9
- package/dist/conformance/daytona.d.ts +9 -20
- package/dist/conformance/daytona.js +12 -21
- package/dist/conformance/e2b.d.ts +6 -0
- package/dist/conformance/e2b.js +9 -2
- package/dist/conformance/exec-stream.d.ts +45 -0
- package/dist/conformance/exec-stream.js +486 -0
- package/dist/conformance/fixture.d.ts +1 -1
- package/dist/conformance/fixture.js +25 -1
- package/dist/conformance/modal.d.ts +17 -13
- package/dist/conformance/modal.js +18 -9
- package/dist/conformance/orgo.d.ts +3 -3
- package/dist/conformance/orgo.js +6 -8
- package/dist/conformance/phone-harness.d.ts +6 -2
- package/dist/conformance/phone-harness.js +21 -4
- package/dist/conformance/report.js +1 -1
- package/dist/conformance/runner.js +23 -4
- package/dist/conformance/sweep.d.ts +50 -0
- package/dist/conformance/sweep.js +187 -0
- package/dist/conformance/types.d.ts +13 -5
- package/dist/conformance/ui-fixture.d.ts +56 -0
- package/dist/conformance/ui-fixture.js +43 -0
- package/dist/create-requests.d.ts +51 -0
- package/dist/create-requests.js +121 -0
- package/dist/daytona.d.ts +20 -133
- package/dist/daytona.js +405 -1449
- package/dist/descriptor.d.ts +11 -1
- package/dist/descriptor.js +9 -0
- package/dist/descriptors/agent37.d.ts +41 -0
- package/dist/descriptors/agent37.js +28 -0
- package/dist/descriptors/daytona.d.ts +6 -5
- package/dist/descriptors/daytona.js +9 -5
- package/dist/descriptors/index.d.ts +42 -7
- package/dist/descriptors/index.js +4 -2
- package/dist/descriptors/orgo.d.ts +2 -1
- package/dist/descriptors/orgo.js +3 -2
- package/dist/descriptors/phone-harness.d.ts +1 -1
- package/dist/descriptors/phone-harness.js +2 -2
- package/dist/descriptors/shared.d.ts +1 -1
- package/dist/descriptors/shared.js +2 -2
- package/dist/desktop.d.ts +18 -4
- package/dist/desktop.js +89 -9
- package/dist/docker.d.ts +7 -10
- package/dist/docker.js +272 -34
- package/dist/drift.js +4 -4
- package/dist/e2b.d.ts +16 -290
- package/dist/e2b.js +230 -1393
- package/dist/fake-ui-driver.d.ts +97 -0
- package/dist/fake-ui-driver.js +717 -0
- package/dist/host.d.ts +201 -0
- package/dist/host.js +8 -0
- package/dist/hosted/process.d.ts +81 -0
- package/dist/hosted/process.js +259 -0
- package/dist/hosted/provider.d.ts +160 -0
- package/dist/hosted/provider.js +1415 -0
- package/dist/hosted/rest.d.ts +66 -0
- package/dist/hosted/rest.js +121 -0
- package/dist/hosted/scripts.d.ts +82 -0
- package/dist/{child.js → hosted/scripts.js} +118 -12
- package/dist/hosted/values.d.ts +5 -0
- package/dist/hosted/values.js +8 -0
- package/dist/index.d.ts +7 -3
- package/dist/index.js +7 -4
- package/dist/kernel.d.ts +2 -10
- package/dist/kernel.js +3 -6
- package/dist/kinds.d.ts +11 -2
- package/dist/kinds.js +20 -4
- package/dist/local-chrome.d.ts +2 -10
- package/dist/local-chrome.js +3 -6
- package/dist/modal.d.ts +22 -257
- package/dist/modal.js +183 -1280
- package/dist/mount-archil.d.ts +8 -4
- package/dist/mount-archil.js +22 -11
- package/dist/mount.d.ts +10 -4
- package/dist/mount.js +8 -4
- package/dist/orgo-channel.d.ts +3 -2
- package/dist/orgo-channel.js +5 -2
- package/dist/orgo-desktop.d.ts +9 -4
- package/dist/orgo-desktop.js +33 -13
- package/dist/orgo-workspaces.d.ts +47 -0
- package/dist/orgo-workspaces.js +94 -0
- package/dist/orgo.d.ts +10 -13
- package/dist/orgo.js +32 -21
- package/dist/phone-harness.d.ts +31 -28
- package/dist/phone-harness.js +510 -702
- package/dist/png.d.ts +4 -0
- package/dist/png.js +27 -0
- package/dist/protocol.d.ts +339 -33
- package/dist/protocol.js +97 -3
- package/dist/provider.d.ts +36 -8
- package/dist/provider.js +16 -0
- package/dist/providers.d.ts +11 -12
- package/dist/result.d.ts +1 -1
- package/dist/screen-watch.js +34 -6
- package/dist/surface/api.d.ts +260 -0
- package/dist/surface/api.js +1 -0
- package/dist/surface/core.d.ts +55 -0
- package/dist/surface/core.js +475 -0
- package/dist/surface/index.d.ts +5 -0
- package/dist/surface/index.js +3 -0
- package/dist/surface/outline.d.ts +10 -0
- package/dist/surface/outline.js +51 -0
- package/dist/surface/script.d.ts +10 -0
- package/dist/surface/script.js +94 -0
- package/dist/ui-engine.d.ts +114 -0
- package/dist/ui-engine.js +814 -0
- package/package.json +18 -3
- package/dist/child.d.ts +0 -42
- package/dist/conformance/daytona-sweep.d.ts +0 -18
- package/dist/conformance/daytona-sweep.js +0 -126
- package/dist/conformance/e2b-diagnose.d.ts +0 -16
- package/dist/conformance/e2b-diagnose.js +0 -61
- package/dist/conformance/e2b-sweep.d.ts +0 -18
- package/dist/conformance/e2b-sweep.js +0 -132
- package/dist/conformance/modal-sweep.d.ts +0 -22
- package/dist/conformance/modal-sweep.js +0 -213
package/dist/host.d.ts
ADDED
|
@@ -0,0 +1,201 @@
|
|
|
1
|
+
import type { ProviderDescriptor } from "./descriptor.js";
|
|
2
|
+
import type { ComputerCapabilities, ComputerSpec, GpuCapability, GpuSpec, Refusal } from "./provider.js";
|
|
3
|
+
import type { ComputerErrorCode } from "./result.js";
|
|
4
|
+
/**
|
|
5
|
+
* One vendor's machines. Every call answers its value or throws: a failure whose code the host knows throws a
|
|
6
|
+
* HostError with that code, and any other error is read through errorCode (default "vendor"); an AbortError or a
|
|
7
|
+
* TimeoutError the adapter reads itself. The adapter bounds each call by ctx.timeoutMs and ctx.signal; a host passes
|
|
8
|
+
* both on to its vendor call when it can.
|
|
9
|
+
*/
|
|
10
|
+
export interface MachineHost {
|
|
11
|
+
/**
|
|
12
|
+
* The provider's descriptor (./descriptors): its id names the vendor of every error of code vendor, its label is the
|
|
13
|
+
* vendor in words, and capabilities() and check() read its network forms, mount kinds and unmounted kinds.
|
|
14
|
+
*/
|
|
15
|
+
readonly descriptor: HostDescriptor;
|
|
16
|
+
readonly facts: HostFacts;
|
|
17
|
+
/**
|
|
18
|
+
* Makes one machine, by its options, and answers its id once the vendor has one. It does not wait for the machine to
|
|
19
|
+
* run (the adapter polls get while it is starting) or to be listed (the adapter waits for that too). The machine
|
|
20
|
+
* carries req.name where the vendor can find it again by listing: the vendor's native name, or a label. An answer that
|
|
21
|
+
* never comes is not a machine the adapter forgets: it lists by req.labels and deletes what it finds.
|
|
22
|
+
*/
|
|
23
|
+
create(req: MachineRequest, ctx: HostCall): Promise<{
|
|
24
|
+
id: string;
|
|
25
|
+
}>;
|
|
26
|
+
/** One machine as the vendor reads it now; undefined once the vendor no longer has it (not found is that, or a HostError not_found). */
|
|
27
|
+
get(id: string, ctx: HostCall): Promise<Machine | undefined>;
|
|
28
|
+
/**
|
|
29
|
+
* The machines of the account the vendor's listing shows for these labels, every page, within ctx.timeoutMs in all.
|
|
30
|
+
* The listing finds; get answers: the adapter takes only the ids from it and reads each one it keeps by get, for its
|
|
31
|
+
* state, labels and end, so a listing may be stale (one still showing a deleted machine, or an end from before a
|
|
32
|
+
* timer moved: E2B's). It may answer more machines than carry all the labels (a vendor that filters by some, or
|
|
33
|
+
* none): the adapter keeps those whose get does. A listing may lag a create: two creates of one request on two
|
|
34
|
+
* provider instances can then both make a machine, and the adapter deletes every one but the oldest (createdAt), so
|
|
35
|
+
* a vendor may see a second create deleted.
|
|
36
|
+
*/
|
|
37
|
+
list(labels: Record<string, string>, ctx: HostCall): Promise<Machine[]>;
|
|
38
|
+
/**
|
|
39
|
+
* Deletes a machine; not found is ok. It answers once the vendor took the delete: the adapter then waits until get
|
|
40
|
+
* answers none, twice running (a machine get reads gone is still going, and holds the wait).
|
|
41
|
+
*/
|
|
42
|
+
delete(id: string, ctx: HostCall): Promise<void>;
|
|
43
|
+
/**
|
|
44
|
+
* Starts argv in the machine as root (the vendor's own command user when it is root, else one with passwordless
|
|
45
|
+
* sudo), the vendor's agent's own process, outside the computer's tools, and answers once it has started.
|
|
46
|
+
*
|
|
47
|
+
* exec is the whole protocol: the adapter installs and starts its daemon through it, then talks to the daemon over
|
|
48
|
+
* one exec'd process's stdin and stdout for the computer's whole life (the stdio bridge, spec/computer/stdio.md). So:
|
|
49
|
+
* - Two-way and live. Each write() reaches the process's stdin as it is written, and stdout and stderr bytes come
|
|
50
|
+
* back as the process writes them, while it runs. Not request and response, never held until the process exits.
|
|
51
|
+
* - Byte-exact. Every byte value 0-255, both ways: no UTF-8 decode or encode, no newline translation, no terminal,
|
|
52
|
+
* nothing added or dropped. A client that hands output over as text breaks this (E2B's commands.run does).
|
|
53
|
+
* - No timeout. The process runs until it exits or kill() ends it: a vendor's command timeout, and any idle timeout
|
|
54
|
+
* on the stream, are off.
|
|
55
|
+
* - stdout and stderr stay apart. wait() answers the process's own exit code. kill() ends the process itself.
|
|
56
|
+
* The process reads what write() gives until end(); the adapter ends stdin at once where it gives none. The exec
|
|
57
|
+
* pre-check (conformance --exec-stream, src/conformance/exec-stream.ts) checks each of these on a machine made by
|
|
58
|
+
* hand, with exec alone.
|
|
59
|
+
*/
|
|
60
|
+
exec(id: string, argv: readonly string[], ctx: HostCall): Promise<MachineProcess>;
|
|
61
|
+
/**
|
|
62
|
+
* Puts bytes at path in the machine, as root. Optional: a vendor with no file API leaves it out, and the adapter
|
|
63
|
+
* streams the file through exec's stdin.
|
|
64
|
+
*/
|
|
65
|
+
upload?(id: string, path: string, bytes: Uint8Array, ctx: HostCall): Promise<void>;
|
|
66
|
+
/**
|
|
67
|
+
* The code of an error the vendor's client threw: one of ComputerErrorCode. Optional: the adapter reads a HostError's
|
|
68
|
+
* own code, an AbortError as "aborted" and a TimeoutError as "timeout" itself, and anything else a host does not map
|
|
69
|
+
* is "vendor".
|
|
70
|
+
*/
|
|
71
|
+
errorCode?(error: unknown): ComputerErrorCode;
|
|
72
|
+
/**
|
|
73
|
+
* Moves the machine's end to endsAt, never past its agentrun.computer.max-deadline label, the most it may live: the
|
|
74
|
+
* vendor's own timer (facts.lifetime "vendor"), or the janitor's deadline label (facts.lifetime "janitor"). A refusal
|
|
75
|
+
* (a HostError limit, in words) when no end the vendor takes lands there without moving it earlier than it is or past
|
|
76
|
+
* the most: nothing moves then. The adapter reads the end back with get (deadlineOf, the deadline label: a vendor that
|
|
77
|
+
* keeps no label change reads its moved timer as that label), and init's deadline follows it. Optional: a host without
|
|
78
|
+
* it has no extend.
|
|
79
|
+
*/
|
|
80
|
+
extend?(id: string, endsAt: Date, ctx: HostCall): Promise<void>;
|
|
81
|
+
/**
|
|
82
|
+
* The vendor's own rules of a spec, over the adapter's shared ones: an image name it takes, a GPU's image. No call. The
|
|
83
|
+
* spec is an object, as the caller gave it: its fields are not checked yet (an image may be a number).
|
|
84
|
+
*/
|
|
85
|
+
check?(spec: Partial<ComputerSpec>): Refusal[];
|
|
86
|
+
}
|
|
87
|
+
/** A host's failure whose code it knows: thrown by a host member, read by the adapter as that code, in these words. */
|
|
88
|
+
export declare class HostError extends Error {
|
|
89
|
+
readonly code: ComputerErrorCode;
|
|
90
|
+
constructor(code: ComputerErrorCode, message: string);
|
|
91
|
+
}
|
|
92
|
+
/** Every call's bound: the adapter's timeout for this kind of call, and the caller's signal when it has one. */
|
|
93
|
+
export interface HostCall {
|
|
94
|
+
signal?: AbortSignal;
|
|
95
|
+
timeoutMs: number;
|
|
96
|
+
}
|
|
97
|
+
/** What create asks of the vendor: the adapter fills every field but the ones facts.specFields leaves out. */
|
|
98
|
+
export interface MachineRequest {
|
|
99
|
+
/** computer-<12 hex digits>, from the create's request id: the vendor's native name, or a label it lists by. */
|
|
100
|
+
name: string;
|
|
101
|
+
/** The image or template, as the spec names it (host.check holds it to the vendor's rule). */
|
|
102
|
+
image: string;
|
|
103
|
+
/** The caller's labels and the adapter's own (agentrun.computer*, the Archil mount's), every one to be read back. */
|
|
104
|
+
labels: Record<string, string>;
|
|
105
|
+
/** With facts.lifetime "vendor": the vendor's timer, the spec's lifetime. Absent for a janitor's machine. */
|
|
106
|
+
lifetimeMs?: number;
|
|
107
|
+
/**
|
|
108
|
+
* The vendor's network for the machine: "open" only for an Archil computer's root, which reaches Archil; its tools'
|
|
109
|
+
* network is init's own, none, read from inside.
|
|
110
|
+
*/
|
|
111
|
+
network: "none" | "open";
|
|
112
|
+
/** facts.daemon "pid1" alone: the machine's main process (the entrypoint that waits for the daemon, then becomes init). */
|
|
113
|
+
command?: readonly string[];
|
|
114
|
+
gpu?: GpuSpec;
|
|
115
|
+
cpus?: number;
|
|
116
|
+
memoryMb?: number;
|
|
117
|
+
workdir?: string;
|
|
118
|
+
user?: string;
|
|
119
|
+
}
|
|
120
|
+
/**
|
|
121
|
+
* One machine as the vendor reads it back. The adapter holds the rest itself: what create asked is in the labels, what
|
|
122
|
+
* runs is read from inside (the daemon's hello and sha256, the network probe).
|
|
123
|
+
*/
|
|
124
|
+
export interface Machine {
|
|
125
|
+
id: string;
|
|
126
|
+
/** Every label it carries, the adapter's own included. */
|
|
127
|
+
labels: Record<string, string>;
|
|
128
|
+
/**
|
|
129
|
+
* starting: not yet running; stopped: there, not running, and not being deleted (paused, asleep): listed, so the
|
|
130
|
+
* janitor still ends it; gone: being deleted, ended, or failed (why says how).
|
|
131
|
+
*/
|
|
132
|
+
state: "starting" | "running" | "stopped" | "gone";
|
|
133
|
+
/** With facts.lifetime "vendor": when the vendor's timer ends it. */
|
|
134
|
+
endsAt?: Date;
|
|
135
|
+
/**
|
|
136
|
+
* When the vendor made it, by its clock, where it says. Two creates of one request on two provider instances can each
|
|
137
|
+
* make a machine before either is listed: the oldest is kept, by this, or by the lowest id where a machine has none.
|
|
138
|
+
*/
|
|
139
|
+
createdAt?: Date;
|
|
140
|
+
/** With facts.gpuReadBack: the GPUs the vendor reads back, absent for a machine with none. A retry is held to them. */
|
|
141
|
+
gpu?: GpuSpec;
|
|
142
|
+
/** The vendor's words for a machine that is gone because it failed: a create stops in them. */
|
|
143
|
+
why?: string;
|
|
144
|
+
}
|
|
145
|
+
/** A process exec started: its output as it comes, its stdin, and its end. */
|
|
146
|
+
export interface MachineProcess {
|
|
147
|
+
stdout: AsyncIterable<Uint8Array>;
|
|
148
|
+
stderr: AsyncIterable<Uint8Array>;
|
|
149
|
+
/** Writes to its stdin, in order; rejects once the process has ended. */
|
|
150
|
+
write(bytes: Uint8Array): Promise<void>;
|
|
151
|
+
/** Ends its stdin, so it reads end of file. */
|
|
152
|
+
end(): Promise<void>;
|
|
153
|
+
/** Its exit code once it has ended. */
|
|
154
|
+
wait(): Promise<number>;
|
|
155
|
+
/** Ends it, best effort, and lets go of its streams; never throws. */
|
|
156
|
+
kill(): Promise<void>;
|
|
157
|
+
}
|
|
158
|
+
/** What the host reads of its provider's descriptor. */
|
|
159
|
+
export type HostDescriptor = Pick<ProviderDescriptor, "id" | "label" | "network" | "mounts" | "unmounted">;
|
|
160
|
+
/** What a vendor's machines are, static: with its descriptor and the adapter's options, capabilities() is built from these alone. */
|
|
161
|
+
export interface HostFacts {
|
|
162
|
+
/** child: the vendor's agent is the machine's first process and starts init; pid1: init is the machine's main process. */
|
|
163
|
+
daemon: "child" | "pid1";
|
|
164
|
+
isolation: ComputerCapabilities["isolation"];
|
|
165
|
+
/** Who ends a machine at its deadline: the vendor's timer, or the janitor by label. */
|
|
166
|
+
lifetime: "vendor" | "janitor";
|
|
167
|
+
/** The vendor's hard cap of a machine's life; the adapter's maxLifetimeMs may only lower it. */
|
|
168
|
+
maxLifetimeMs: number;
|
|
169
|
+
arch: "amd64" | "arm64";
|
|
170
|
+
/** The TCP ports the vendor's own agent listens on inside the machine (E2B's envd 49983). */
|
|
171
|
+
vendorAgentPorts: number[];
|
|
172
|
+
/**
|
|
173
|
+
* The vendor's own path unix sockets outside /run, as shell patterns with no space (Daytona's D-Bus session bus,
|
|
174
|
+
* /tmp/dbus-*). Init takes others' access off every socket under /run itself; a child host's create does the same
|
|
175
|
+
* for each socket these name, as root, before init starts, since a network namespace does not cut a path socket.
|
|
176
|
+
*/
|
|
177
|
+
vendorSockets?: string[];
|
|
178
|
+
/** Whether its desktops run WINDOW_MANAGER (xfwm4, which its image has) once the display answers. Default none. */
|
|
179
|
+
windowManager?: boolean;
|
|
180
|
+
/**
|
|
181
|
+
* Whether get reads a machine's GPUs back from the vendor (Machine.gpu, absent for a machine with none): a retry of a
|
|
182
|
+
* request is held to them only then. A vendor that reads none back (Modal) answers the request's computer as made.
|
|
183
|
+
*/
|
|
184
|
+
gpuReadBack?: boolean;
|
|
185
|
+
gpu?: GpuCapability;
|
|
186
|
+
/** What spec.user may ask for, beside the daemon user. */
|
|
187
|
+
user?: Array<"root" | "uid">;
|
|
188
|
+
/** The spec's size and place fields the vendor takes, passed in MachineRequest; any other is refused. */
|
|
189
|
+
specFields?: Array<"cpus" | "memoryMb" | "workdir" | "user">;
|
|
190
|
+
/**
|
|
191
|
+
* The vendor's label rules: how many labels a machine takes, and the bytes they come to, the adapter's own at their
|
|
192
|
+
* longest with them: every name and value ("pairs", the default), or their JSON ("json").
|
|
193
|
+
*/
|
|
194
|
+
labelRules?: {
|
|
195
|
+
max?: number;
|
|
196
|
+
maxBytes?: number;
|
|
197
|
+
measure?: "pairs" | "json";
|
|
198
|
+
};
|
|
199
|
+
/** The runtime a machine of this request runs: "gvisor" only where the vendor runs one (a pid1 host's GPU). */
|
|
200
|
+
runtime?(req: MachineRequest): "vm" | "gvisor";
|
|
201
|
+
}
|
package/dist/host.js
ADDED
|
@@ -0,0 +1,81 @@
|
|
|
1
|
+
import { type HostCall, type MachineHost } from "../host.js";
|
|
2
|
+
import type { ArchilRoot } from "../mount-archil.js";
|
|
3
|
+
import type { ComputerConnection } from "../provider.js";
|
|
4
|
+
import { type ComputerErrorCode, type Result } from "../result.js";
|
|
5
|
+
/** What a call's race adds to the bound it gives the vendor's own call, so the vendor's timeout, not the race, reports a slow call. */
|
|
6
|
+
export declare const CALL_SLACK_MS = 5000;
|
|
7
|
+
export declare const seconds: (ms: number) => string;
|
|
8
|
+
/** An error in words, the vendor's name kept, cut to one line of 300 characters. */
|
|
9
|
+
export declare const words: (error: unknown) => string;
|
|
10
|
+
/** A pause that ends early when signal aborts; never rejects. */
|
|
11
|
+
export declare const pause: (ms: number, signal?: AbortSignal) => Promise<void>;
|
|
12
|
+
/** A signal that aborts with the caller's, or once ms pass. */
|
|
13
|
+
export declare const bounded: (ms: number, signal?: AbortSignal) => AbortSignal;
|
|
14
|
+
/**
|
|
15
|
+
* A promise that rejects with an AbortError (the caller's signal aborted) or a TimeoutError (ms passed), and clear to
|
|
16
|
+
* stop listening when the work finished first.
|
|
17
|
+
*/
|
|
18
|
+
export declare function boundOf(ms: number, signal: AbortSignal | undefined, what: string): {
|
|
19
|
+
promise: Promise<never>;
|
|
20
|
+
clear: () => void;
|
|
21
|
+
};
|
|
22
|
+
/**
|
|
23
|
+
* The code of an error a host threw: a HostError's own, AbortError and TimeoutError by name, else the host's errorCode,
|
|
24
|
+
* else "vendor".
|
|
25
|
+
*/
|
|
26
|
+
export declare function codeOfError(host: Pick<MachineHost, "errorCode">, error: unknown): ComputerErrorCode;
|
|
27
|
+
/** A Result's value, or a HostError in its words: for a host whose own client answers Results (hosted/rest.ts). */
|
|
28
|
+
export declare function orThrow<T>(result: Result<T>): T;
|
|
29
|
+
/**
|
|
30
|
+
* Settles with the promise's value, or a timeout or abort in words once ms pass or signal aborts. A host call that
|
|
31
|
+
* throws is read through codeOfError.
|
|
32
|
+
*/
|
|
33
|
+
export declare function race<T>(host: Pick<MachineHost, "descriptor" | "errorCode">, promise: Promise<T>, what: string, ms: number, signal?: AbortSignal): Promise<Result<T>>;
|
|
34
|
+
/**
|
|
35
|
+
* One host call, bounded: ctx.timeoutMs is the vendor's own bound, and the race gives it CALL_SLACK_MS more. A call
|
|
36
|
+
* that throws at once is an error like one that rejects. A call given up on (timeout or abort), by the race or by the
|
|
37
|
+
* host's own client (a HostError timeout), hands onLate what it answers: its value, or its failure, once it settles.
|
|
38
|
+
*/
|
|
39
|
+
export declare function hostCall<T>(host: Pick<MachineHost, "descriptor" | "errorCode">, what: string, run: (ctx: HostCall) => Promise<T>, ms: number, signal?: AbortSignal, onLate?: (late: Result<T>) => unknown): Promise<Result<T>>;
|
|
40
|
+
/** What one process run to its end printed: its exit code, and both streams (each cut to its last 64 KiB). */
|
|
41
|
+
export interface RunResult {
|
|
42
|
+
exitCode: number;
|
|
43
|
+
stdout: string;
|
|
44
|
+
stderr: string;
|
|
45
|
+
}
|
|
46
|
+
/**
|
|
47
|
+
* argv run in the machine as root through exec, stdin (when given) written and ended, waited for until it ends, within
|
|
48
|
+
* ms and the caller's signal; a process past them is killed, and the kill waited for at most ms (and CALL_SLACK_MS).
|
|
49
|
+
* A non-zero exit is a result, not an error.
|
|
50
|
+
*/
|
|
51
|
+
export declare function runToExit(host: MachineHost, id: string, argv: readonly string[], { stdin, ms, signal, what }: {
|
|
52
|
+
stdin?: Uint8Array;
|
|
53
|
+
ms: number;
|
|
54
|
+
signal?: AbortSignal;
|
|
55
|
+
what: string;
|
|
56
|
+
}): Promise<Result<RunResult>>;
|
|
57
|
+
/** The default upload, for a vendor with no file API: the bytes streamed into `cat` as root through exec's stdin. */
|
|
58
|
+
export declare function uploadThroughExec(host: MachineHost, id: string, path: string, bytes: Uint8Array, ms: number, signal?: AbortSignal): Promise<Result<void>>;
|
|
59
|
+
/**
|
|
60
|
+
* The root channel into a machine (for the shared Archil steps): argv as root through exec, stdin (one line, then
|
|
61
|
+
* closed) when given. What it printed is both streams, stdout first; a non-zero exit is a result. An error never holds
|
|
62
|
+
* what stdin was given.
|
|
63
|
+
*/
|
|
64
|
+
export declare function rootOf(host: MachineHost, id: string, name: string): ArchilRoot;
|
|
65
|
+
/** A connection the bridge opened, with why it could not be: notListening when no daemon listens yet (try again). */
|
|
66
|
+
export type Opened = Result<ComputerConnection> & {
|
|
67
|
+
notListening?: true;
|
|
68
|
+
};
|
|
69
|
+
/**
|
|
70
|
+
* One connection to the daemon over a process the stdio command runs: its stdout is what the client reads, writes go to
|
|
71
|
+
* its stdin in order, and close kills it. Hello is asked within helloMs; a bridge that ended before hello, with no
|
|
72
|
+
* daemon to reach, is notListening.
|
|
73
|
+
*/
|
|
74
|
+
export declare function openBridge(host: MachineHost, id: string, { argv, name, version, requestMs, helloMs, signal }: {
|
|
75
|
+
argv: readonly string[];
|
|
76
|
+
name: string;
|
|
77
|
+
version: string;
|
|
78
|
+
requestMs: number;
|
|
79
|
+
helloMs: number;
|
|
80
|
+
signal?: AbortSignal;
|
|
81
|
+
}): Promise<Opened>;
|
|
@@ -0,0 +1,259 @@
|
|
|
1
|
+
// The adapter's plumbing over a MachineHost: one bounded call, a process run to its end, a file put through exec when
|
|
2
|
+
// the vendor has no file API, the root channel an Archil mount runs through, and the stdio bridge that carries the
|
|
3
|
+
// daemon's wire over one exec'd process.
|
|
4
|
+
import { PassThrough, Writable } from "node:stream";
|
|
5
|
+
import { DaemonClient } from "../client.js";
|
|
6
|
+
import { HostError } from "../host.js";
|
|
7
|
+
import { failFor, ok } from "../result.js";
|
|
8
|
+
/** What a call's race adds to the bound it gives the vendor's own call, so the vendor's timeout, not the race, reports a slow call. */
|
|
9
|
+
export const CALL_SLACK_MS = 5_000;
|
|
10
|
+
export const seconds = (ms) => `${ms / 1000} s`;
|
|
11
|
+
/** An error in words, the vendor's name kept, cut to one line of 300 characters. */
|
|
12
|
+
export const words = (error) => {
|
|
13
|
+
const name = error?.name;
|
|
14
|
+
const message = error instanceof Error ? error.message : String(error);
|
|
15
|
+
return `${typeof name === "string" && name !== "Error" ? `${name}: ` : ""}${message}`.replace(/\s+/g, " ").slice(0, 300);
|
|
16
|
+
};
|
|
17
|
+
/** A pause that ends early when signal aborts; never rejects. */
|
|
18
|
+
export const pause = (ms, signal) => new Promise((done) => {
|
|
19
|
+
const timer = setTimeout(finish, ms);
|
|
20
|
+
function finish() {
|
|
21
|
+
clearTimeout(timer);
|
|
22
|
+
signal?.removeEventListener("abort", finish);
|
|
23
|
+
done();
|
|
24
|
+
}
|
|
25
|
+
if (signal?.aborted)
|
|
26
|
+
finish();
|
|
27
|
+
else
|
|
28
|
+
signal?.addEventListener("abort", finish, { once: true });
|
|
29
|
+
});
|
|
30
|
+
/** A signal that aborts with the caller's, or once ms pass. */
|
|
31
|
+
export const bounded = (ms, signal) => (signal ? AbortSignal.any([signal, AbortSignal.timeout(ms)]) : AbortSignal.timeout(ms));
|
|
32
|
+
/**
|
|
33
|
+
* A promise that rejects with an AbortError (the caller's signal aborted) or a TimeoutError (ms passed), and clear to
|
|
34
|
+
* stop listening when the work finished first.
|
|
35
|
+
*/
|
|
36
|
+
export function boundOf(ms, signal, what) {
|
|
37
|
+
const bound = bounded(ms, signal);
|
|
38
|
+
let onAbort = () => { };
|
|
39
|
+
const promise = new Promise((_, reject) => {
|
|
40
|
+
onAbort = () => {
|
|
41
|
+
const timedOut = !signal?.aborted;
|
|
42
|
+
const error = new Error(timedOut ? `${what} did not answer within ${seconds(ms)}` : `${what} was aborted`);
|
|
43
|
+
error.name = timedOut ? "TimeoutError" : "AbortError";
|
|
44
|
+
reject(error);
|
|
45
|
+
};
|
|
46
|
+
if (bound.aborted)
|
|
47
|
+
onAbort();
|
|
48
|
+
else
|
|
49
|
+
bound.addEventListener("abort", onAbort, { once: true });
|
|
50
|
+
});
|
|
51
|
+
promise.catch(() => { });
|
|
52
|
+
return { promise, clear: () => bound.removeEventListener("abort", onAbort) };
|
|
53
|
+
}
|
|
54
|
+
/**
|
|
55
|
+
* The code of an error a host threw: a HostError's own, AbortError and TimeoutError by name, else the host's errorCode,
|
|
56
|
+
* else "vendor".
|
|
57
|
+
*/
|
|
58
|
+
export function codeOfError(host, error) {
|
|
59
|
+
if (error instanceof HostError)
|
|
60
|
+
return error.code;
|
|
61
|
+
const name = error?.name;
|
|
62
|
+
if (name === "AbortError")
|
|
63
|
+
return "aborted";
|
|
64
|
+
if (name === "TimeoutError")
|
|
65
|
+
return "timeout";
|
|
66
|
+
return host.errorCode?.(error) ?? "vendor";
|
|
67
|
+
}
|
|
68
|
+
/** A Result's value, or a HostError in its words: for a host whose own client answers Results (hosted/rest.ts). */
|
|
69
|
+
export function orThrow(result) {
|
|
70
|
+
if (result.ok)
|
|
71
|
+
return result.value;
|
|
72
|
+
throw new HostError(result.error.code, result.error.message);
|
|
73
|
+
}
|
|
74
|
+
/**
|
|
75
|
+
* Settles with the promise's value, or a timeout or abort in words once ms pass or signal aborts. A host call that
|
|
76
|
+
* throws is read through codeOfError.
|
|
77
|
+
*/
|
|
78
|
+
export function race(host, promise, what, ms, signal) {
|
|
79
|
+
const fail = failFor(host.descriptor.id);
|
|
80
|
+
if (signal?.aborted) {
|
|
81
|
+
promise.catch(() => { });
|
|
82
|
+
return Promise.resolve(fail("aborted", `${what} was aborted`));
|
|
83
|
+
}
|
|
84
|
+
return new Promise((settle) => {
|
|
85
|
+
const timer = setTimeout(() => done(fail("timeout", `${what} did not answer within ${seconds(ms)}`)), ms);
|
|
86
|
+
const onAbort = () => done(fail("aborted", `${what} was aborted`));
|
|
87
|
+
signal?.addEventListener("abort", onAbort, { once: true });
|
|
88
|
+
let settled = false;
|
|
89
|
+
function done(result) {
|
|
90
|
+
if (settled)
|
|
91
|
+
return;
|
|
92
|
+
settled = true;
|
|
93
|
+
clearTimeout(timer);
|
|
94
|
+
signal?.removeEventListener("abort", onAbort);
|
|
95
|
+
settle(result);
|
|
96
|
+
}
|
|
97
|
+
promise.then((value) => done(ok(value)), (error) => done(signal?.aborted ? fail("aborted", `${what} was aborted`) : fail(codeOfError(host, error), `${what} failed: ${words(error)}`)));
|
|
98
|
+
});
|
|
99
|
+
}
|
|
100
|
+
/**
|
|
101
|
+
* One host call, bounded: ctx.timeoutMs is the vendor's own bound, and the race gives it CALL_SLACK_MS more. A call
|
|
102
|
+
* that throws at once is an error like one that rejects. A call given up on (timeout or abort), by the race or by the
|
|
103
|
+
* host's own client (a HostError timeout), hands onLate what it answers: its value, or its failure, once it settles.
|
|
104
|
+
*/
|
|
105
|
+
export async function hostCall(host, what, run, ms, signal, onLate) {
|
|
106
|
+
const fail = failFor(host.descriptor.id);
|
|
107
|
+
if (signal?.aborted)
|
|
108
|
+
return fail("aborted", `${what} was aborted before it was called`);
|
|
109
|
+
let promise;
|
|
110
|
+
try {
|
|
111
|
+
promise = Promise.resolve(run({ timeoutMs: ms, ...(signal ? { signal } : {}) }));
|
|
112
|
+
}
|
|
113
|
+
catch (error) {
|
|
114
|
+
return fail(codeOfError(host, error), `${what} failed: ${words(error)}`);
|
|
115
|
+
}
|
|
116
|
+
const raced = await race(host, promise, what, ms + CALL_SLACK_MS, signal);
|
|
117
|
+
if (!raced.ok && onLate && (raced.error.code === "timeout" || raced.error.code === "aborted")) {
|
|
118
|
+
promise.then((value) => onLate(ok(value)), (error) => onLate(fail(codeOfError(host, error), `${what} failed: ${words(error)}`)));
|
|
119
|
+
}
|
|
120
|
+
return raced;
|
|
121
|
+
}
|
|
122
|
+
/** Reads every chunk of a stream into one buffer, at most `max` bytes kept (the last ones). */
|
|
123
|
+
async function drain(stream, max) {
|
|
124
|
+
let kept = Buffer.alloc(0);
|
|
125
|
+
try {
|
|
126
|
+
for await (const chunk of stream) {
|
|
127
|
+
kept = Buffer.concat([kept, Buffer.from(chunk)]);
|
|
128
|
+
if (kept.length > max)
|
|
129
|
+
kept = kept.subarray(kept.length - max);
|
|
130
|
+
}
|
|
131
|
+
}
|
|
132
|
+
catch {
|
|
133
|
+
// A stream that ends in an error has given what it had.
|
|
134
|
+
}
|
|
135
|
+
return kept;
|
|
136
|
+
}
|
|
137
|
+
const OUTPUT_MAX = 64 * 1024;
|
|
138
|
+
/**
|
|
139
|
+
* argv run in the machine as root through exec, stdin (when given) written and ended, waited for until it ends, within
|
|
140
|
+
* ms and the caller's signal; a process past them is killed, and the kill waited for at most ms (and CALL_SLACK_MS).
|
|
141
|
+
* A non-zero exit is a result, not an error.
|
|
142
|
+
*/
|
|
143
|
+
export async function runToExit(host, id, argv, { stdin, ms, signal, what }) {
|
|
144
|
+
const fail = failFor(host.descriptor.id);
|
|
145
|
+
const started = await hostCall(host, what, (ctx) => host.exec(id, argv, ctx), ms, signal);
|
|
146
|
+
if (!started.ok)
|
|
147
|
+
return started;
|
|
148
|
+
const proc = started.value;
|
|
149
|
+
const out = drain(proc.stdout, OUTPUT_MAX);
|
|
150
|
+
const err = drain(proc.stderr, OUTPUT_MAX);
|
|
151
|
+
const ran = (async () => {
|
|
152
|
+
// In pieces, so a vendor whose stdin takes one request at a time (E2B's sendStdin) is never handed a whole binary;
|
|
153
|
+
// with none to give, stdin ends at once, so the process reads end of file (one that ended already has no stdin). Not
|
|
154
|
+
// awaited: a vendor's close that stalls (E2B's closeStdin is a request of its own) never holds up a command that
|
|
155
|
+
// reads no input.
|
|
156
|
+
if (stdin === undefined)
|
|
157
|
+
Promise.resolve().then(() => proc.end()).catch(() => { });
|
|
158
|
+
else {
|
|
159
|
+
for (let at = 0; at < stdin.length; at += STDIN_CHUNK)
|
|
160
|
+
await proc.write(stdin.subarray(at, at + STDIN_CHUNK));
|
|
161
|
+
await proc.end();
|
|
162
|
+
}
|
|
163
|
+
const exitCode = await proc.wait();
|
|
164
|
+
return { exitCode, stdout: (await out).toString("utf8"), stderr: (await err).toString("utf8") };
|
|
165
|
+
})();
|
|
166
|
+
const result = await race(host, ran, what, ms + CALL_SLACK_MS, signal);
|
|
167
|
+
if (!result.ok) {
|
|
168
|
+
// Bounded, as the bridge's close is: a kill the vendor never answers (a remote call that stalls) must not hold
|
|
169
|
+
// up the caller, which may be a create on its way to deleting the whole machine.
|
|
170
|
+
const killing = Promise.resolve().then(() => proc.kill());
|
|
171
|
+
await race(host, killing, `${what}'s kill`, Math.min(ms, CALL_SLACK_MS));
|
|
172
|
+
return result.error.code === "timeout" ? fail("timeout", `${what} did not end within ${seconds(ms)}`) : result;
|
|
173
|
+
}
|
|
174
|
+
return result;
|
|
175
|
+
}
|
|
176
|
+
const STDIN_CHUNK = 256 * 1024;
|
|
177
|
+
/** The default upload, for a vendor with no file API: the bytes streamed into `cat` as root through exec's stdin. */
|
|
178
|
+
export async function uploadThroughExec(host, id, path, bytes, ms, signal) {
|
|
179
|
+
const ran = await runToExit(host, id, ["sh", "-c", 'umask 077 && cat > "$1"', "sh", path], { stdin: bytes, ms, ...(signal ? { signal } : {}), what: `the upload to ${path}` });
|
|
180
|
+
if (!ran.ok)
|
|
181
|
+
return ran;
|
|
182
|
+
return ran.value.exitCode === 0 ? ok(undefined) : failFor(host.descriptor.id)("vendor", `the upload to ${path} exited with ${ran.value.exitCode}: ${`${ran.value.stdout} ${ran.value.stderr}`.trim().slice(0, 300)}`);
|
|
183
|
+
}
|
|
184
|
+
/**
|
|
185
|
+
* The root channel into a machine (for the shared Archil steps): argv as root through exec, stdin (one line, then
|
|
186
|
+
* closed) when given. What it printed is both streams, stdout first; a non-zero exit is a result. An error never holds
|
|
187
|
+
* what stdin was given.
|
|
188
|
+
*/
|
|
189
|
+
export function rootOf(host, id, name) {
|
|
190
|
+
return {
|
|
191
|
+
run: async (argv, { stdin, timeoutMs, signal }) => {
|
|
192
|
+
const ran = await runToExit(host, id, argv, { ...(stdin === undefined ? {} : { stdin: new Uint8Array(Buffer.from(stdin, "utf8")) }), ms: timeoutMs, ...(signal ? { signal } : {}), what: `root's ${argv[0] === undefined ? "command" : argv[0].split("/").pop()} in ${name}` });
|
|
193
|
+
return ran.ok ? ok({ exitCode: ran.value.exitCode, output: `${ran.value.stdout}${ran.value.stderr}` }) : ran;
|
|
194
|
+
},
|
|
195
|
+
};
|
|
196
|
+
}
|
|
197
|
+
/**
|
|
198
|
+
* One connection to the daemon over a process the stdio command runs: its stdout is what the client reads, writes go to
|
|
199
|
+
* its stdin in order, and close kills it. Hello is asked within helloMs; a bridge that ended before hello, with no
|
|
200
|
+
* daemon to reach, is notListening.
|
|
201
|
+
*/
|
|
202
|
+
export async function openBridge(host, id, { argv, name, version, requestMs, helloMs, signal }) {
|
|
203
|
+
const fail = failFor(host.descriptor.id);
|
|
204
|
+
// Never signalled, only raced: the bridge lives on after this call. It has no command timeout, so one the vendor
|
|
205
|
+
// starts after this was given up on is killed as it lands.
|
|
206
|
+
const started = await hostCall(host, `starting the daemon's stdio bridge in ${name}`, (ctx) => host.exec(id, argv, { timeoutMs: ctx.timeoutMs }), requestMs, signal, (late) => late.ok && late.value.kill());
|
|
207
|
+
if (!started.ok) {
|
|
208
|
+
const { code, message } = started.error;
|
|
209
|
+
return code === "timeout" || code === "aborted" ? fail(code, `${message}; a bridge that starts after this is killed`) : started;
|
|
210
|
+
}
|
|
211
|
+
const proc = started.value;
|
|
212
|
+
const readable = new PassThrough();
|
|
213
|
+
let stderr = "";
|
|
214
|
+
void (async () => {
|
|
215
|
+
try {
|
|
216
|
+
for await (const chunk of proc.stdout)
|
|
217
|
+
if (!readable.destroyed)
|
|
218
|
+
readable.write(Buffer.from(chunk));
|
|
219
|
+
}
|
|
220
|
+
catch {
|
|
221
|
+
// The bridge's end, for any reason, ends what the client reads.
|
|
222
|
+
}
|
|
223
|
+
readable.end();
|
|
224
|
+
})();
|
|
225
|
+
void (async () => {
|
|
226
|
+
try {
|
|
227
|
+
for await (const chunk of proc.stderr)
|
|
228
|
+
stderr = (stderr + Buffer.from(chunk).toString("utf8")).slice(-4096);
|
|
229
|
+
}
|
|
230
|
+
catch {
|
|
231
|
+
// Its words are what came before.
|
|
232
|
+
}
|
|
233
|
+
})();
|
|
234
|
+
const writable = new Writable({
|
|
235
|
+
write(chunk, _encoding, done) {
|
|
236
|
+
proc.write(new Uint8Array(chunk)).then(() => done(), (error) => done(error instanceof Error ? error : new Error(String(error))));
|
|
237
|
+
},
|
|
238
|
+
});
|
|
239
|
+
const client = new DaemonClient({ readable, writable });
|
|
240
|
+
let closed;
|
|
241
|
+
const close = () => (closed ??= (async () => {
|
|
242
|
+
client.close();
|
|
243
|
+
await race(host, proc.kill(), "the stdio bridge's kill", requestMs).catch(() => { });
|
|
244
|
+
})());
|
|
245
|
+
const hello = await client.call("hello", { client: `@parcha/agentrun-computer ${version}` }, { signal: bounded(helloMs, signal) });
|
|
246
|
+
if (!hello.ok) {
|
|
247
|
+
await close();
|
|
248
|
+
if (signal?.aborted)
|
|
249
|
+
return fail("aborted", `hello to the daemon in ${name} was aborted`);
|
|
250
|
+
if (hello.error.code === "aborted")
|
|
251
|
+
return fail("timeout", `the daemon in ${name} did not answer hello within ${seconds(helloMs)}`);
|
|
252
|
+
if (hello.error.code !== "gone")
|
|
253
|
+
return hello;
|
|
254
|
+
// Its stderr may come a moment after its stdout ended.
|
|
255
|
+
await pause(20);
|
|
256
|
+
return { ...fail("vendor", `cannot reach the daemon in ${name}: ${stderr.trim() || hello.error.message}`), notListening: true };
|
|
257
|
+
}
|
|
258
|
+
return ok({ client, hello: hello.value, close });
|
|
259
|
+
}
|