@parcha/agentrun-computer 0.1.0-beta.37 → 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.
Files changed (93) hide show
  1. package/README.md +259 -203
  2. package/box/Dockerfile +18 -1
  3. package/box/README.md +13 -0
  4. package/box/build-agent37.mjs +8 -27
  5. package/box/build-daytona.py +2 -2
  6. package/box/build-e2b.mjs +41 -18
  7. package/box/recipe.mjs +43 -0
  8. package/dist/agent37-ssh.d.ts +89 -0
  9. package/dist/agent37-ssh.js +445 -0
  10. package/dist/agent37.d.ts +2 -63
  11. package/dist/agent37.js +104 -461
  12. package/dist/browserbase.d.ts +0 -6
  13. package/dist/browserbase.js +1 -5
  14. package/dist/client.d.ts +12 -3
  15. package/dist/client.js +39 -4
  16. package/dist/computerd-linux-amd64 +0 -0
  17. package/dist/computerd-linux-arm64 +0 -0
  18. package/dist/computerd.json +4 -0
  19. package/dist/conformance/cases/apps.d.ts +6 -0
  20. package/dist/conformance/cases/apps.js +18 -5
  21. package/dist/conformance/cases/isolation.d.ts +7 -5
  22. package/dist/conformance/cases/isolation.js +44 -14
  23. package/dist/conformance/cases/mount.js +1 -1
  24. package/dist/conformance/cli.js +66 -6
  25. package/dist/conformance/daytona.d.ts +9 -20
  26. package/dist/conformance/daytona.js +12 -21
  27. package/dist/conformance/exec-stream.d.ts +45 -0
  28. package/dist/conformance/exec-stream.js +486 -0
  29. package/dist/conformance/sweep.js +8 -8
  30. package/dist/conformance/types.d.ts +2 -2
  31. package/dist/create-requests.d.ts +1 -1
  32. package/dist/daytona.d.ts +20 -131
  33. package/dist/daytona.js +405 -1450
  34. package/dist/descriptor.d.ts +6 -1
  35. package/dist/descriptor.js +7 -0
  36. package/dist/descriptors/agent37.d.ts +1 -1
  37. package/dist/descriptors/agent37.js +1 -1
  38. package/dist/descriptors/daytona.d.ts +6 -5
  39. package/dist/descriptors/daytona.js +9 -5
  40. package/dist/descriptors/index.d.ts +4 -5
  41. package/dist/descriptors/shared.d.ts +1 -1
  42. package/dist/descriptors/shared.js +2 -2
  43. package/dist/desktop.d.ts +18 -4
  44. package/dist/desktop.js +89 -9
  45. package/dist/docker.d.ts +0 -6
  46. package/dist/docker.js +1 -5
  47. package/dist/drift.js +4 -4
  48. package/dist/e2b.d.ts +10 -128
  49. package/dist/e2b.js +130 -233
  50. package/dist/host.d.ts +106 -87
  51. package/dist/host.js +8 -1
  52. package/dist/hosted/process.d.ts +22 -8
  53. package/dist/hosted/process.js +75 -44
  54. package/dist/hosted/provider.d.ts +35 -21
  55. package/dist/hosted/provider.js +312 -261
  56. package/dist/hosted/rest.d.ts +66 -0
  57. package/dist/hosted/rest.js +121 -0
  58. package/dist/hosted/scripts.d.ts +8 -2
  59. package/dist/hosted/scripts.js +27 -2
  60. package/dist/hosted/values.d.ts +5 -0
  61. package/dist/hosted/values.js +8 -0
  62. package/dist/index.d.ts +1 -1
  63. package/dist/index.js +1 -1
  64. package/dist/kernel.d.ts +0 -6
  65. package/dist/kernel.js +1 -5
  66. package/dist/kinds.d.ts +3 -1
  67. package/dist/local-chrome.d.ts +0 -6
  68. package/dist/local-chrome.js +1 -5
  69. package/dist/modal.d.ts +7 -84
  70. package/dist/modal.js +50 -174
  71. package/dist/mount-archil.d.ts +8 -4
  72. package/dist/mount-archil.js +22 -11
  73. package/dist/mount.d.ts +10 -4
  74. package/dist/mount.js +8 -4
  75. package/dist/orgo.d.ts +0 -9
  76. package/dist/orgo.js +2 -9
  77. package/dist/phone-harness.d.ts +0 -6
  78. package/dist/phone-harness.js +0 -4
  79. package/dist/protocol.d.ts +4 -1
  80. package/dist/protocol.js +1 -0
  81. package/dist/provider.d.ts +2 -2
  82. package/dist/providers.d.ts +11 -12
  83. package/dist/screen-watch.js +34 -6
  84. package/dist/surface/api.d.ts +14 -2
  85. package/dist/surface/core.d.ts +16 -3
  86. package/dist/surface/core.js +95 -13
  87. package/dist/surface/outline.d.ts +2 -0
  88. package/dist/surface/outline.js +2 -1
  89. package/dist/surface/script.js +2 -0
  90. package/dist/ui-engine.js +19 -3
  91. package/package.json +2 -3
  92. package/dist/conformance/daytona-sweep.d.ts +0 -18
  93. package/dist/conformance/daytona-sweep.js +0 -126
package/dist/host.d.ts CHANGED
@@ -1,69 +1,93 @@
1
- import type { MountKind } from "./mount.js";
2
- import type { ComputerCapabilities, ComputerSpec, GpuCapability, GpuSpec, NetworkForm, NetworkPolicy, Refusal } from "./provider.js";
3
- import type { ComputerErrorCode, Result } from "./result.js";
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
4
  /**
5
- * One vendor's machines. Every call answers a Result: an expected failure (the vendor refused, the machine is not
6
- * there) is a result with a code from errorCode, never a throw. A call that throws is read through errorCode as well.
7
- * The adapter bounds each call by ctx.timeoutMs and ctx.signal; a host passes both on to its vendor call when it can.
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.
8
9
  */
9
10
  export interface MachineHost {
10
- /** The provider's descriptor id ("e2b"): the vendor named by every error of code vendor. */
11
- readonly id: string;
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;
12
16
  readonly facts: HostFacts;
13
17
  /**
14
- * Makes one machine, by its options, and answers once the vendor has its id. It does not wait for the machine to run
15
- * (the adapter polls get while it is starting) or to be listed (the adapter waits for that too). The machine carries
16
- * req.name where the vendor can find it again by listing: the vendor's native name, or a label. An answer that never
17
- * comes is not a machine the adapter forgets: it lists by req.labels and deletes what it finds.
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.
18
22
  */
19
- create(req: MachineRequest, ctx: HostCall): Promise<Result<{
23
+ create(req: MachineRequest, ctx: HostCall): Promise<{
20
24
  id: string;
21
- }>>;
22
- /** One machine as the vendor reads it now; undefined once the vendor no longer has it (not found is that, not an error). */
23
- get(id: string, ctx: HostCall): Promise<Result<Machine | undefined>>;
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[]>;
24
38
  /**
25
- * Every machine of the account that carries all of these labels, every page, each as get reads it (network included,
26
- * where the vendor reads one back), within ctx.timeoutMs in all. A machine the vendor is deleting may still be listed.
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).
27
41
  */
28
- list(labels: Record<string, string>, ctx: HostCall): Promise<Result<Machine[]>>;
29
- /** Deletes a machine; not found is ok. It answers once the vendor took the delete: the adapter waits for gone. */
30
- delete(id: string, ctx: HostCall): Promise<Result<void>>;
42
+ delete(id: string, ctx: HostCall): Promise<void>;
31
43
  /**
32
44
  * Starts argv in the machine as root (the vendor's own command user when it is root, else one with passwordless
33
- * sudo), the vendor's agent's own process, outside the computer's tools. With opts.stdin, the process reads what
34
- * write() gives until end(). No command timeout: the adapter kills it, or it ends. Answers once it has started.
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.
35
59
  */
36
- exec(id: string, argv: readonly string[], opts: {
37
- stdin: boolean;
38
- }, ctx: HostCall): Promise<Result<MachineProcess>>;
60
+ exec(id: string, argv: readonly string[], ctx: HostCall): Promise<MachineProcess>;
39
61
  /**
40
62
  * Puts bytes at path in the machine, as root. Optional: a vendor with no file API leaves it out, and the adapter
41
63
  * streams the file through exec's stdin.
42
64
  */
43
- upload?(id: string, path: string, bytes: Uint8Array, ctx: HostCall): Promise<Result<void>>;
44
- /** The code of an error the vendor's client threw: one of ComputerErrorCode, "vendor" for anything else. */
45
- errorCode(error: unknown): ComputerErrorCode;
46
- /** Vendor settings a machine takes once it runs and before the daemon goes in (egress rules, an idle timer). */
47
- prepare?(id: string, ctx: HostCall): Promise<Result<void>>;
48
- /** Stops a machine, keeping it; declared with facts.release "stop". It answers once the machine reads stopped (or gone with stopDeletes). */
49
- stop?(id: string, ctx: HostCall): Promise<Result<void>>;
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>;
50
81
  /**
51
- * Moves the vendor's own timer (facts.lifetime "vendor") so the machine ends at want.end, never past want.most, the
52
- * most it may live. A refusal (limit, in words) when no timer the vendor takes lands there without moving the end
53
- * earlier than it is or past want.most: nothing moves then. The adapter reads the end back with get.
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).
54
84
  */
55
- setTimer?(id: string, want: {
56
- end: Date;
57
- most: Date;
58
- }, ctx: HostCall): Promise<Result<void>>;
59
- /** Replaces some labels of a machine (the janitor's deadline label, at extend); facts.lifetime "janitor". */
60
- setLabels?(id: string, labels: Record<string, string>, ctx: HostCall): Promise<Result<void>>;
61
- /** 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. */
62
- check?(spec: ComputerSpec): Refusal[];
63
- /** Settings' test, where a list would make something (an app); default: one list by the adapter's label, counted. */
64
- testConnection?(ctx: HostCall): Promise<Result<{
65
- detail: string;
66
- }>>;
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);
67
91
  }
68
92
  /** Every call's bound: the adapter's timeout for this kind of call, and the caller's signal when it has one. */
69
93
  export interface HostCall {
@@ -93,44 +117,29 @@ export interface MachineRequest {
93
117
  workdir?: string;
94
118
  user?: string;
95
119
  }
96
- /** One machine as the vendor reads it back. */
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
+ */
97
124
  export interface Machine {
98
125
  id: string;
99
126
  /** Every label it carries, the adapter's own included. */
100
127
  labels: Record<string, string>;
101
- /** starting: not yet running; going: being deleted or ended; failed: the vendor gave up on it (why says how). */
102
- state: "starting" | "running" | "stopped" | "going" | "failed";
103
- /** The vendor's own read of its network; absent when the vendor reads none back (the probe stands for it then). */
104
- network?: NetworkPolicy;
105
- /** What the vendor resolved the image to, by its own name (a template's id, a snapshot). */
106
- image?: {
107
- resolved?: string;
108
- };
109
128
  /**
110
- * When the vendor made it, by its clock, where it says. Two creates of one request on two provider instances can each
111
- * make a machine before either is listed: the oldest is kept, by this, or by the lowest id where a machine has none.
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).
112
131
  */
113
- createdAt?: Date;
132
+ state: "starting" | "running" | "stopped" | "gone";
114
133
  /** With facts.lifetime "vendor": when the vendor's timer ends it. */
115
134
  endsAt?: Date;
116
135
  /**
117
- * With facts.lifetime "vendor": the vendor's timer was moved past where create set it (by an extend), so endsAt,
118
- * held to the most the machine may live, stands for its deadline: the vendor keeps no label change.
119
- */
120
- timerMoved?: boolean;
121
- /**
122
- * The vendor's own create options read back wrong, in words, one each ("lifecycle onTimeout: declared ..."). Held at
123
- * create and at attach alike, so only what holds for the machine's whole life: a timer an extend moved is timerMoved,
124
- * never drift (create refuses a machine whose timer reads moved before any extend).
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.
125
138
  */
126
- drift?: string[];
127
- /** The size the vendor reads back, in CPUs and MiB: attach reports it. */
128
- size?: {
129
- cpus?: number;
130
- memoryMb?: number;
131
- };
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. */
132
141
  gpu?: GpuSpec;
133
- /** The vendor's words for a failed state. */
142
+ /** The vendor's words for a machine that is gone because it failed: a create stops in them. */
134
143
  why?: string;
135
144
  }
136
145
  /** A process exec started: its output as it comes, its stdin, and its end. */
@@ -146,7 +155,9 @@ export interface MachineProcess {
146
155
  /** Ends it, best effort, and lets go of its streams; never throws. */
147
156
  kill(): Promise<void>;
148
157
  }
149
- /** What a vendor's machines are, static: with the adapter's options, capabilities() is built from these alone. */
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. */
150
161
  export interface HostFacts {
151
162
  /** child: the vendor's agent is the machine's first process and starts init; pid1: init is the machine's main process. */
152
163
  daemon: "child" | "pid1";
@@ -158,24 +169,32 @@ export interface HostFacts {
158
169
  arch: "amd64" | "arm64";
159
170
  /** The TCP ports the vendor's own agent listens on inside the machine (E2B's envd 49983). */
160
171
  vendorAgentPorts: number[];
161
- /** Whether get reads the network back from the vendor. */
162
- networkReadBack: boolean;
163
- /** The network forms its computers take: of its descriptor's. */
164
- network: NetworkForm[];
165
- /** The mount kinds it takes: of its descriptor's. */
166
- mounts?: MountKind[];
167
- /** How release may end one; "stop" needs host.stop. */
168
- release: Array<"stop" | "delete">;
169
- stopDeletes?: boolean;
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;
170
185
  gpu?: GpuCapability;
171
186
  /** What spec.user may ask for, beside the daemon user. */
172
187
  user?: Array<"root" | "uid">;
173
188
  /** The spec's size and place fields the vendor takes, passed in MachineRequest; any other is refused. */
174
189
  specFields?: Array<"cpus" | "memoryMb" | "workdir" | "user">;
175
- /** The vendor's label rules: how many labels a machine takes, and the names it takes. */
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
+ */
176
194
  labelRules?: {
177
195
  max?: number;
178
- key?: RegExp;
196
+ maxBytes?: number;
197
+ measure?: "pairs" | "json";
179
198
  };
180
199
  /** The runtime a machine of this request runs: "gvisor" only where the vendor runs one (a pid1 host's GPU). */
181
200
  runtime?(req: MachineRequest): "vm" | "gvisor";
package/dist/host.js CHANGED
@@ -1 +1,8 @@
1
- export {};
1
+ /** A host's failure whose code it knows: thrown by a host member, read by the adapter as that code, in these words. */
2
+ export class HostError extends Error {
3
+ code;
4
+ constructor(code, message) {
5
+ super(message);
6
+ this.code = code;
7
+ }
8
+ }
@@ -1,4 +1,4 @@
1
- import type { HostCall, MachineHost } from "../host.js";
1
+ import { type HostCall, type MachineHost } from "../host.js";
2
2
  import type { ArchilRoot } from "../mount-archil.js";
3
3
  import type { ComputerConnection } from "../provider.js";
4
4
  import { type ComputerErrorCode, type Result } from "../result.js";
@@ -11,18 +11,32 @@ export declare const words: (error: unknown) => string;
11
11
  export declare const pause: (ms: number, signal?: AbortSignal) => Promise<void>;
12
12
  /** A signal that aborts with the caller's, or once ms pass. */
13
13
  export declare const bounded: (ms: number, signal?: AbortSignal) => AbortSignal;
14
- /** The failures of one host: an error of code vendor names it. */
15
- export declare const failOf: (host: Pick<MachineHost, "id">) => (code: ComputerErrorCode, message: string) => Result<never>;
16
14
  /**
17
- * Settles with the promise's result, or a timeout or abort in words once ms pass or signal aborts. A host call that
18
- * answers a Result is passed on as it answers; one that throws is read through the host's errorCode.
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.
19
17
  */
20
- export declare function race<T>(host: Pick<MachineHost, "id" | "errorCode">, promise: Promise<T>, what: string, ms: number, signal?: AbortSignal): Promise<Result<T>>;
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>>;
21
34
  /**
22
35
  * One host call, bounded: ctx.timeoutMs is the vendor's own bound, and the race gives it CALL_SLACK_MS more. A call
23
- * that throws at once is an error like one that rejects; a Result the host answers keeps its own words, prefixed.
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.
24
38
  */
25
- export declare function hostCall<T>(host: Pick<MachineHost, "id" | "errorCode">, what: string, run: (ctx: HostCall) => Promise<Result<T>>, ms: number, signal?: AbortSignal): Promise<Result<T>>;
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>>;
26
40
  /** What one process run to its end printed: its exit code, and both streams (each cut to its last 64 KiB). */
27
41
  export interface RunResult {
28
42
  exitCode: number;
@@ -3,7 +3,8 @@
3
3
  // daemon's wire over one exec'd process.
4
4
  import { PassThrough, Writable } from "node:stream";
5
5
  import { DaemonClient } from "../client.js";
6
- import { errorOf, ok } from "../result.js";
6
+ import { HostError } from "../host.js";
7
+ import { failFor, ok } from "../result.js";
7
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. */
8
9
  export const CALL_SLACK_MS = 5_000;
9
10
  export const seconds = (ms) => `${ms / 1000} s`;
@@ -28,14 +29,54 @@ export const pause = (ms, signal) => new Promise((done) => {
28
29
  });
29
30
  /** A signal that aborts with the caller's, or once ms pass. */
30
31
  export const bounded = (ms, signal) => (signal ? AbortSignal.any([signal, AbortSignal.timeout(ms)]) : AbortSignal.timeout(ms));
31
- /** The failures of one host: an error of code vendor names it. */
32
- export const failOf = (host) => (code, message) => ({ ok: false, error: errorOf(code, message, host.id) });
33
32
  /**
34
- * Settles with the promise's result, or a timeout or abort in words once ms pass or signal aborts. A host call that
35
- * answers a Result is passed on as it answers; one that throws is read through the host's errorCode.
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.
36
77
  */
37
78
  export function race(host, promise, what, ms, signal) {
38
- const fail = failOf(host);
79
+ const fail = failFor(host.descriptor.id);
39
80
  if (signal?.aborted) {
40
81
  promise.catch(() => { });
41
82
  return Promise.resolve(fail("aborted", `${what} was aborted`));
@@ -53,15 +94,16 @@ export function race(host, promise, what, ms, signal) {
53
94
  signal?.removeEventListener("abort", onAbort);
54
95
  settle(result);
55
96
  }
56
- promise.then((value) => done(ok(value)), (error) => done(signal?.aborted ? fail("aborted", `${what} was aborted`) : fail(host.errorCode(error), `${what} failed: ${words(error)}`)));
97
+ promise.then((value) => done(ok(value)), (error) => done(signal?.aborted ? fail("aborted", `${what} was aborted`) : fail(codeOfError(host, error), `${what} failed: ${words(error)}`)));
57
98
  });
58
99
  }
59
100
  /**
60
101
  * One host call, bounded: ctx.timeoutMs is the vendor's own bound, and the race gives it CALL_SLACK_MS more. A call
61
- * that throws at once is an error like one that rejects; a Result the host answers keeps its own words, prefixed.
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.
62
104
  */
63
- export async function hostCall(host, what, run, ms, signal) {
64
- const fail = failOf(host);
105
+ export async function hostCall(host, what, run, ms, signal, onLate) {
106
+ const fail = failFor(host.descriptor.id);
65
107
  if (signal?.aborted)
66
108
  return fail("aborted", `${what} was aborted before it was called`);
67
109
  let promise;
@@ -69,15 +111,13 @@ export async function hostCall(host, what, run, ms, signal) {
69
111
  promise = Promise.resolve(run({ timeoutMs: ms, ...(signal ? { signal } : {}) }));
70
112
  }
71
113
  catch (error) {
72
- return fail(host.errorCode(error), `${what} failed: ${words(error)}`);
114
+ return fail(codeOfError(host, error), `${what} failed: ${words(error)}`);
73
115
  }
74
116
  const raced = await race(host, promise, what, ms + CALL_SLACK_MS, signal);
75
- if (!raced.ok)
76
- return raced;
77
- const answered = raced.value;
78
- if (answered.ok)
79
- return answered;
80
- return { ok: false, error: { ...answered.error, message: `${what} failed: ${answered.error.message}` } };
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;
81
121
  }
82
122
  /** Reads every chunk of a stream into one buffer, at most `max` bytes kept (the last ones). */
83
123
  async function drain(stream, max) {
@@ -101,16 +141,21 @@ const OUTPUT_MAX = 64 * 1024;
101
141
  * A non-zero exit is a result, not an error.
102
142
  */
103
143
  export async function runToExit(host, id, argv, { stdin, ms, signal, what }) {
104
- const fail = failOf(host);
105
- const started = await hostCall(host, what, (ctx) => host.exec(id, argv, { stdin: stdin !== undefined }, ctx), ms, signal);
144
+ const fail = failFor(host.descriptor.id);
145
+ const started = await hostCall(host, what, (ctx) => host.exec(id, argv, ctx), ms, signal);
106
146
  if (!started.ok)
107
147
  return started;
108
148
  const proc = started.value;
109
149
  const out = drain(proc.stdout, OUTPUT_MAX);
110
150
  const err = drain(proc.stderr, OUTPUT_MAX);
111
151
  const ran = (async () => {
112
- if (stdin !== undefined) {
113
- // In pieces, so a vendor whose stdin takes one request at a time (E2B's sendStdin) is never handed a whole binary.
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 {
114
159
  for (let at = 0; at < stdin.length; at += STDIN_CHUNK)
115
160
  await proc.write(stdin.subarray(at, at + STDIN_CHUNK));
116
161
  await proc.end();
@@ -134,7 +179,7 @@ export async function uploadThroughExec(host, id, path, bytes, ms, signal) {
134
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}` });
135
180
  if (!ran.ok)
136
181
  return ran;
137
- return ran.value.exitCode === 0 ? ok(undefined) : failOf(host)("vendor", `the upload to ${path} exited with ${ran.value.exitCode}: ${`${ran.value.stdout} ${ran.value.stderr}`.trim().slice(0, 300)}`);
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)}`);
138
183
  }
139
184
  /**
140
185
  * The root channel into a machine (for the shared Archil steps): argv as root through exec, stdin (one line, then
@@ -155,29 +200,15 @@ export function rootOf(host, id, name) {
155
200
  * daemon to reach, is notListening.
156
201
  */
157
202
  export async function openBridge(host, id, { argv, name, version, requestMs, helloMs, signal }) {
158
- const fail = failOf(host);
159
- const what = `starting the daemon's stdio bridge in ${name}`;
160
- if (signal?.aborted)
161
- return fail("aborted", `${what} was aborted before it was called`);
162
- let starting;
163
- try {
164
- starting = Promise.resolve(host.exec(id, argv, { stdin: true }, { timeoutMs: requestMs }));
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;
165
210
  }
166
- catch (error) {
167
- return fail(host.errorCode(error), `${what} failed: ${words(error)}`);
168
- }
169
- const raced = await race(host, starting, what, requestMs + CALL_SLACK_MS, signal);
170
- if (!raced.ok) {
171
- // The bridge has no command timeout: one the vendor starts after this was given up on is killed as it lands.
172
- if (raced.error.code === "timeout" || raced.error.code === "aborted") {
173
- starting.then((late) => (late.ok ? late.value.kill() : undefined), () => { });
174
- return fail(raced.error.code, `${raced.error.message}; a bridge that starts after this is killed`);
175
- }
176
- return raced;
177
- }
178
- if (!raced.value.ok)
179
- return fail(raced.value.error.code, `${what} failed: ${raced.value.error.message}`);
180
- const proc = raced.value.value;
211
+ const proc = started.value;
181
212
  const readable = new PassThrough();
182
213
  let stderr = "";
183
214
  void (async () => {
@@ -18,9 +18,9 @@ export interface HostedTimeouts {
18
18
  helloMs: number;
19
19
  /** The probe, run through the daemon: it takes at most 20 s (10 s to connect, 10 s to answer, every target at once). */
20
20
  probeMs: number;
21
- /** One whole list, every page and every machine's read-back. */
21
+ /** One whole listing, every page; each listed machine's get after it has requestMs. */
22
22
  listMs: number;
23
- /** From a delete to a machine the vendor no longer has and no longer lists. */
23
+ /** From a delete to get answering none for the machine. */
24
24
  goneMs: number;
25
25
  /** The first pause between two reads while release waits; the pauses double up to 5 s. */
26
26
  pollMs: number;
@@ -35,8 +35,6 @@ export interface HostedTimeouts {
35
35
  mountMs: number;
36
36
  }
37
37
  export interface HostedOptions {
38
- /** The vendor's name in words ("E2B"), for refusals and Settings' test. */
39
- label: string;
40
38
  /** desktop: an X server started through the daemon; linux (default): no screen. */
41
39
  kind?: "desktop" | "linux";
42
40
  /** The display and its X server, for kind desktop. Default Xvfb on :0, 1280x800. */
@@ -70,17 +68,21 @@ export declare const NAME_LABEL = "agentrun.computer.name";
70
68
  export declare const IMAGE_LABEL = "agentrun.computer.image";
71
69
  /** The most extend may move the deadline to: create's time plus the cap, by this machine's clock, as init was given. */
72
70
  export declare const MAX_LABEL = "agentrun.computer.max-deadline";
71
+ /** The adapter's timeouts, the caller's over its defaults: what a host reads for its own requestMs or pollMs. */
72
+ export declare const timeoutsOf: (timeouts?: Partial<HostedTimeouts>) => HostedTimeouts;
73
+ /** An ISO time a label holds, or undefined when it is missing or not the ISO time create writes. */
74
+ export declare function isoOf(written: string | undefined): Date | undefined;
73
75
  /**
74
- * When a machine ends: the deadline create wrote; or, once an extend moved the vendor's own timer past where create set
75
- * it (the vendor keeps no label change), that end, up to the most the computer may live, which is what init was given.
76
+ * When a machine ends: its deadline label, as the vendor reads it back. A vendor that keeps no label change reads it
77
+ * as the end its timer moved to (E2B's get), so a restarted provider reads an extend all the same.
76
78
  */
77
- export declare function deadlineOf(machine: Pick<Machine, "labels" | "endsAt" | "timerMoved">): Date | undefined;
79
+ export declare const deadlineOf: (machine: Pick<Machine, "labels">) => Date | undefined;
78
80
  /** Only a machine this adapter made counts: its label, a name of its making, and the name asked for when one is. */
79
81
  export declare const ours: (labels: Record<string, string> | undefined, name?: string) => boolean;
80
82
  type Extend = (ref: ComputerRef, ttlMs: number, context?: CallContext) => Promise<Result<{
81
83
  deadline: Date;
82
84
  }>>;
83
- /** hostedProvider's provider: a ComputerProvider whose vendor is host. */
85
+ /** The shared adapter's provider: a ComputerProvider whose vendor is host. */
84
86
  export declare class HostedProvider implements ComputerProvider {
85
87
  #private;
86
88
  readonly id: string;
@@ -109,38 +111,50 @@ export declare class HostedProvider implements ComputerProvider {
109
111
  labels: Record<string, string>;
110
112
  }, { signal }?: AttachContext): Promise<Result<AttachedComputer>>;
111
113
  /**
112
- * Settings' test: the host's own, or one list by the adapter's label, counted, which makes nothing; a refused key is
113
- * unauthorized, a vendor not reached unreachable, the message never carrying a key.
114
+ * Settings' test: one list by the adapter's label, its computers counted as the listing reads them (no get of each),
115
+ * which makes nothing; a refused key is unauthorized, a vendor not reached unreachable, the message never carrying a key.
114
116
  */
115
117
  testConnection({ signal }?: CallContext): Promise<Result<{
116
118
  detail: string;
117
119
  }>>;
118
120
  /**
119
- * Every computer this provider made that carries all of these labels, running or stopped, each one's network read
120
- * back. One deadline, listMs from the call, bounds every page and every read-back.
121
+ * Every computer this provider made that carries all of these labels, running or stopped: the listing finds them, and
122
+ * a get of each reads its state, labels and end (foundBy). Its network is none, as create made it and the probe reads
123
+ * it from inside.
121
124
  */
122
125
  list(labels: Record<string, string>, { signal }?: CallContext): Promise<Result<ComputerInfo[]>>;
123
- /** The vendor reads the policy back (or the probe stands for it). A change is refused: the network is fixed at create. */
126
+ /** The probe reads the policy back from inside. A change is refused: the network is fixed at create. */
124
127
  network(ref: ComputerRef, policy?: NetworkSpec, { signal }?: CallContext): Promise<Result<NetworkPolicy>>;
125
128
  /**
126
129
  * delete reads the machine back to check it is this provider's, then deletes it, both never signalled, so a release
127
130
  * after an abort, or one aborted during the read, still sends the delete; it is idempotent: one the vendor no longer
128
- * has is ok. The vendor answers a delete before the machine is gone, so release then waits (#settle) until the vendor
129
- * reads it gone and no longer lists it, within goneMs and the caller's signal, the one step the signal ends. A running
131
+ * has is ok. The vendor answers a delete before the machine is gone, so release then waits (#settle) until get
132
+ * answers none (twice running), within goneMs and the caller's signal, the one step the signal ends. A running
130
133
  * Archil computer lets go of the run's folder first (archilRelease: the final sync through computerd, then checkin as
131
134
  * root); the delete runs whatever that answers. A folder not let go cleanly loses nothing (every call that changed it
132
135
  * synced, and the next claim revokes the hold), so it is said through onNote, and the release succeeds once the
133
- * machine is gone. stop, where the host declares it, stops the machine and keeps it.
136
+ * machine is gone. A computer here is deleted, never stopped: release stop is refused in words.
134
137
  */
135
138
  release(ref: ComputerRef, how: ReleaseHow, { signal }?: ReleaseContext): Promise<Result<void>>;
136
139
  }
137
140
  /**
138
- * Waits until the host reads a machine gone and no longer lists it under its name, twice running (see #settle). Shared
139
- * with the live sweep, which waits the same way.
141
+ * The host's listing by labels, bounded by ms: only the machines that carry every one of them, since a vendor's listing
142
+ * may answer more (MachineHost.list).
140
143
  */
141
- export declare function settleGone(host: MachineHost, ref: ComputerRef, { requestMs, listMs, pollMs, goneMs, signal }: Pick<HostedTimeouts, "requestMs" | "listMs" | "pollMs" | "goneMs"> & {
144
+ export declare function listedBy(host: MachineHost, what: string, labels: Record<string, string>, ms: number, signal?: AbortSignal): Promise<Result<Machine[]>>;
145
+ /**
146
+ * The listing finds, get answers (MachineHost.list): the machines the host's listing by labels names, each read now by
147
+ * readMachine, within requestMs. One the vendor no longer has (not found) is dropped, and so is one whose get carries
148
+ * not every label. A read that fails otherwise fails the whole.
149
+ */
150
+ export declare function foundBy(host: MachineHost, what: string, labels: Record<string, string>, { listMs, requestMs }: Pick<HostedTimeouts, "listMs" | "requestMs">, signal?: AbortSignal): Promise<Result<Machine[]>>;
151
+ /**
152
+ * Waits, through waitUntil, until get answers none for a machine after its delete, twice running: the vendor no longer
153
+ * has it. A machine read gone is still going (a vendor still destroying it), so it holds the wait like any other state.
154
+ * A listing may show a deleted machine for a while (MachineHost.list), so it is not read. Pauses double from pollMs to
155
+ * 5 s, bounded by goneMs and the caller's signal; a failed read ends the wait with that error. Shared with the live sweep.
156
+ */
157
+ export declare function settle(host: MachineHost, ref: ComputerRef, { requestMs, pollMs, goneMs, signal }: Pick<HostedTimeouts, "requestMs" | "pollMs" | "goneMs"> & {
142
158
  signal?: AbortSignal;
143
159
  }): Promise<Result<void>>;
144
- /** The shared machine-host adapter: host's machines as a ComputerProvider (see HostedProvider). */
145
- export declare function hostedProvider(host: MachineHost, options: HostedOptions): HostedProvider;
146
160
  export {};