@parcha/agentrun-computer 0.1.0-beta.40 → 0.1.0-beta.41
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/README.md +31 -14
- package/box/Dockerfile +6 -4
- package/box/README.md +8 -0
- package/box/build-daytona.py +3 -1
- package/box/install.sh +68 -7
- package/dist/attach.d.ts +46 -1
- package/dist/attach.js +48 -4
- package/dist/box-image.d.ts +2 -0
- package/dist/box-image.js +10 -0
- package/dist/computerd-linux-amd64 +0 -0
- package/dist/computerd-linux-arm64 +0 -0
- package/dist/computerd.json +2 -2
- package/dist/conformance/cases/provider.js +42 -26
- package/dist/conformance/cli.js +4 -1
- package/dist/conformance/daytona.d.ts +25 -0
- package/dist/conformance/daytona.js +26 -1
- package/dist/conformance/exec-stream.js +14 -6
- package/dist/conformance/runner.js +8 -4
- package/dist/conformance/sweep.d.ts +7 -2
- package/dist/conformance/sweep.js +17 -7
- package/dist/conformance/types.d.ts +21 -0
- package/dist/daytona.d.ts +8 -1
- package/dist/daytona.js +124 -101
- package/dist/descriptor.d.ts +22 -1
- package/dist/descriptor.js +18 -0
- package/dist/descriptors/daytona.d.ts +12 -3
- package/dist/descriptors/daytona.js +15 -7
- package/dist/descriptors/index.d.ts +8 -1
- package/dist/e2b.js +1 -1
- package/dist/host.d.ts +61 -37
- package/dist/hosted/framed-exec.d.ts +8 -19
- package/dist/hosted/framed-exec.js +21 -87
- package/dist/hosted/process.d.ts +2 -0
- package/dist/hosted/process.js +9 -2
- package/dist/hosted/provider.d.ts +41 -4
- package/dist/hosted/provider.js +289 -115
- package/dist/hosted/rest.d.ts +19 -0
- package/dist/hosted/rest.js +76 -0
- package/dist/hosted/scripts.d.ts +50 -0
- package/dist/hosted/scripts.js +116 -1
- package/dist/hosted/ssh-exec.d.ts +7 -6
- package/dist/hosted/ssh-exec.js +35 -52
- package/dist/index.d.ts +2 -2
- package/dist/index.js +2 -2
- package/dist/kinds.d.ts +2 -0
- package/dist/modal.js +21 -8
- package/dist/providers.d.ts +4 -2
- package/package.json +1 -1
package/dist/hosted/rest.d.ts
CHANGED
|
@@ -70,6 +70,25 @@ export interface Rest {
|
|
|
70
70
|
stream(method: string, url: string, init?: RestInit, signal?: AbortSignal): Promise<Result<RestStream>>;
|
|
71
71
|
}
|
|
72
72
|
export declare function rest(options: RestOptions): Rest;
|
|
73
|
+
/** The JSON value of each line of a body of newline-delimited JSON, as the lines come; a line that is not JSON throws. */
|
|
74
|
+
export declare function ndjson(body: AsyncIterable<Uint8Array>): AsyncIterable<unknown>;
|
|
75
|
+
/** One event of a text/event-stream body: its type, its data, and the last id and retry the stream had set by then. */
|
|
76
|
+
export interface SseEvent {
|
|
77
|
+
/** "message" unless an `event:` field named it. */
|
|
78
|
+
event: string;
|
|
79
|
+
/** Its `data:` fields, joined by "\n". */
|
|
80
|
+
data: string;
|
|
81
|
+
/** The last `id:` so far ("" for none): what a host that resumes a dropped stream asks from. */
|
|
82
|
+
id: string;
|
|
83
|
+
/** The last `retry:` so far, the ms the vendor asks a reconnect to wait. */
|
|
84
|
+
retry?: number;
|
|
85
|
+
}
|
|
86
|
+
/**
|
|
87
|
+
* The events of a body of server-sent events (text/event-stream), as they come, read as the HTML standard's event
|
|
88
|
+
* stream says: lines end at \r\n, \n or \r; a line that starts with ":" is a comment (a vendor's keep-alive); a blank
|
|
89
|
+
* line ends an event, and one with no data is none. An event the body ends inside is dropped.
|
|
90
|
+
*/
|
|
91
|
+
export declare function sse(body: AsyncIterable<Uint8Array>): AsyncGenerator<SseEvent, void, undefined>;
|
|
73
92
|
/**
|
|
74
93
|
* Every item of a paged listing. next gets the cursor of the page to read (none for the first) and answers its items and
|
|
75
94
|
* the cursor of the page after it, or nothing at the end. A cursor that comes back twice, or a listing past max pages,
|
package/dist/hosted/rest.js
CHANGED
|
@@ -109,6 +109,82 @@ export function rest(options) {
|
|
|
109
109
|
stream: (method, url, init = {}, signal) => exchange(method, url, init, signal, async (response) => ({ status: response.status, body: response.body ?? NO_BODY })),
|
|
110
110
|
};
|
|
111
111
|
}
|
|
112
|
+
/** The JSON value of each line of a body of newline-delimited JSON, as the lines come; a line that is not JSON throws. */
|
|
113
|
+
export async function* ndjson(body) {
|
|
114
|
+
const decoder = new TextDecoder();
|
|
115
|
+
let rest = "";
|
|
116
|
+
for await (const chunk of body) {
|
|
117
|
+
rest += decoder.decode(chunk, { stream: true });
|
|
118
|
+
let at = rest.indexOf("\n");
|
|
119
|
+
while (at >= 0) {
|
|
120
|
+
const line = rest.slice(0, at).trim();
|
|
121
|
+
rest = rest.slice(at + 1);
|
|
122
|
+
if (line !== "")
|
|
123
|
+
yield JSON.parse(line);
|
|
124
|
+
at = rest.indexOf("\n");
|
|
125
|
+
}
|
|
126
|
+
}
|
|
127
|
+
rest += decoder.decode();
|
|
128
|
+
if (rest.trim() !== "")
|
|
129
|
+
yield JSON.parse(rest);
|
|
130
|
+
}
|
|
131
|
+
/**
|
|
132
|
+
* The events of a body of server-sent events (text/event-stream), as they come, read as the HTML standard's event
|
|
133
|
+
* stream says: lines end at \r\n, \n or \r; a line that starts with ":" is a comment (a vendor's keep-alive); a blank
|
|
134
|
+
* line ends an event, and one with no data is none. An event the body ends inside is dropped.
|
|
135
|
+
*/
|
|
136
|
+
export async function* sse(body) {
|
|
137
|
+
// Not ignoreBOM: the standard drops a U+FEFF that starts the stream.
|
|
138
|
+
const decoder = new TextDecoder();
|
|
139
|
+
let rest = "";
|
|
140
|
+
let data = [];
|
|
141
|
+
let event = "";
|
|
142
|
+
let id = "";
|
|
143
|
+
let retry;
|
|
144
|
+
// The last line ended at a \r that ended what came: a \n that starts what comes next is that \r's, no line of its own.
|
|
145
|
+
let halfCrLf = false;
|
|
146
|
+
const eol = /\r\n|\r|\n/g;
|
|
147
|
+
function* lines(text) {
|
|
148
|
+
if (halfCrLf && text !== "") {
|
|
149
|
+
if (text.startsWith("\n"))
|
|
150
|
+
text = text.slice(1);
|
|
151
|
+
halfCrLf = false;
|
|
152
|
+
}
|
|
153
|
+
// What is left from before holds no line end: the search starts at what came.
|
|
154
|
+
eol.lastIndex = rest.length;
|
|
155
|
+
rest += text;
|
|
156
|
+
let from = 0;
|
|
157
|
+
for (let end = eol.exec(rest); end !== null; end = eol.exec(rest)) {
|
|
158
|
+
const line = rest.slice(from, end.index);
|
|
159
|
+
from = eol.lastIndex;
|
|
160
|
+
halfCrLf = end[0] === "\r" && from === rest.length;
|
|
161
|
+
if (line === "") {
|
|
162
|
+
if (data.length > 0)
|
|
163
|
+
yield { event: event || "message", data: data.join("\n"), id, ...(retry === undefined ? {} : { retry }) };
|
|
164
|
+
data = [];
|
|
165
|
+
event = "";
|
|
166
|
+
continue;
|
|
167
|
+
}
|
|
168
|
+
if (line.startsWith(":"))
|
|
169
|
+
continue;
|
|
170
|
+
const colon = line.indexOf(":");
|
|
171
|
+
const field = colon < 0 ? line : line.slice(0, colon);
|
|
172
|
+
const value = colon < 0 ? "" : line.slice(line[colon + 1] === " " ? colon + 2 : colon + 1);
|
|
173
|
+
if (field === "data")
|
|
174
|
+
data.push(value);
|
|
175
|
+
else if (field === "event")
|
|
176
|
+
event = value;
|
|
177
|
+
else if (field === "id" && !value.includes("\0"))
|
|
178
|
+
id = value;
|
|
179
|
+
else if (field === "retry" && /^[0-9]+$/.test(value))
|
|
180
|
+
retry = Number(value);
|
|
181
|
+
}
|
|
182
|
+
rest = rest.slice(from);
|
|
183
|
+
}
|
|
184
|
+
for await (const chunk of body)
|
|
185
|
+
yield* lines(decoder.decode(chunk, { stream: true }));
|
|
186
|
+
yield* lines(decoder.decode());
|
|
187
|
+
}
|
|
112
188
|
/**
|
|
113
189
|
* Every item of a paged listing. next gets the cursor of the page to read (none for the first) and answers its items and
|
|
114
190
|
* the cursor of the page after it, or nothing at the end. A cursor that comes back twice, or a listing past max pages,
|
package/dist/hosted/scripts.d.ts
CHANGED
|
@@ -1,3 +1,4 @@
|
|
|
1
|
+
import { PassThrough } from "node:stream";
|
|
1
2
|
import type { ComputerCapabilities, ComputerConnection } from "../provider.js";
|
|
2
3
|
import { type Result } from "../result.js";
|
|
3
4
|
/** Who ran install, from its output's `installer <uid> <name>[ sudo]` line, in words; undefined when it has none. */
|
|
@@ -75,6 +76,12 @@ export interface ChildCommandOptions {
|
|
|
75
76
|
/** The network form init's egress proxy serves (./network.ts networkFormOf): open, or allow: and a list; none, no proxy. */
|
|
76
77
|
network?: string;
|
|
77
78
|
}
|
|
79
|
+
/**
|
|
80
|
+
* init's flags, as start and a pid1 entrypoint give them: the deadline, the user, --own-network, and the most
|
|
81
|
+
* deadline.extend may move it to and the network form where given. A dial machine's AGENTRUN_INIT_ARGS, space-separated:
|
|
82
|
+
* no value holds a space.
|
|
83
|
+
*/
|
|
84
|
+
export declare function initFlags({ deadline, user, maxDeadline, network }: Omit<ChildCommandOptions, "staging" | "display">): string[];
|
|
78
85
|
/**
|
|
79
86
|
* The three shell commands a child-daemon provider sends into a computer, exactly: install (the upload moved into place,
|
|
80
87
|
* its sha256 printed), init (long-lived) and stdio (one per connection). Each is one string for a shell, `sh -c` with
|
|
@@ -100,3 +107,46 @@ export interface DaemonExpected {
|
|
|
100
107
|
* working directory and binary. Each read is bounded by timeoutMs and the caller's signal.
|
|
101
108
|
*/
|
|
102
109
|
export declare function daemonDrift(connection: ComputerConnection, expected: DaemonExpected, timeoutMs: number, signal?: AbortSignal): Promise<Result<string[]>>;
|
|
110
|
+
/**
|
|
111
|
+
* The sh a process starts under where the vendor has no kill that ends it (framedExec with no kill, SSH, Modal): one
|
|
112
|
+
* line on stderr, its pid and its start time (field 22 of /proc/<pid>/stat, counted past the command's name in
|
|
113
|
+
* parentheses, which may hold spaces), then the command in its place, which keeps both. pidLine takes the line off.
|
|
114
|
+
*/
|
|
115
|
+
export declare const PID_WRAPPER: string[];
|
|
116
|
+
/**
|
|
117
|
+
* The sh pidKill starts, given the pid and start time: a pid whose process has ended, and may be another's now, is let
|
|
118
|
+
* be. Otherwise it kills the process and every process under it (an upload's head and base64 would wait on the open
|
|
119
|
+
* stdin, and hold stderr open, past their sh; a child that left the process group too): each is stopped as it is found,
|
|
120
|
+
* so none forks away, by its parent pid (field 4 of its stat) until a pass over /proc finds no more, and then all are
|
|
121
|
+
* killed. When the pid leads its own process group (field 5), as a command an sshd starts does, every process in that
|
|
122
|
+
* group is taken too: a background job whose parent exited is under no process the walk finds, but still in the group.
|
|
123
|
+
* A pid that leads no group of its own is let be past its tree, so a kill never reaches what it didn't start.
|
|
124
|
+
*/
|
|
125
|
+
export declare const PID_KILL: string;
|
|
126
|
+
/** A process's pid and start time, from PID_WRAPPER's line. */
|
|
127
|
+
export interface PidOf {
|
|
128
|
+
pid: string;
|
|
129
|
+
start: string;
|
|
130
|
+
}
|
|
131
|
+
/** The argv that kills a process started under PID_WRAPPER, and every process under it, while its pid has that start time. */
|
|
132
|
+
export declare const pidKill: ({ pid, start }: PidOf) => string[];
|
|
133
|
+
/** What pidLine and pidStderr answer: the pid and start time once the line ends, waited for at most ms. */
|
|
134
|
+
export type PidWithin = (ms: number) => Promise<PidOf | undefined>;
|
|
135
|
+
/**
|
|
136
|
+
* PID_WRAPPER's line taken off a process's stderr: take() is given each piece of stderr and answers what of it is the
|
|
137
|
+
* process's own (nothing while the line has not ended). pid answers once the line ends; undefined for a line that is
|
|
138
|
+
* no pid and start time, once end() came first (the stream ended), or past ms.
|
|
139
|
+
*/
|
|
140
|
+
export declare function pidLine(): {
|
|
141
|
+
take(bytes: Uint8Array): Buffer;
|
|
142
|
+
end(): void;
|
|
143
|
+
pid: PidWithin;
|
|
144
|
+
};
|
|
145
|
+
/**
|
|
146
|
+
* A stream's stderr with PID_WRAPPER's line taken off (pidLine), read from its start whether or not anyone reads what
|
|
147
|
+
* it passes on, so the pid comes; the rest waits for its reader.
|
|
148
|
+
*/
|
|
149
|
+
export declare function pidStderr(source: AsyncIterable<Uint8Array>): {
|
|
150
|
+
stderr: PassThrough;
|
|
151
|
+
pid: PidWithin;
|
|
152
|
+
};
|
package/dist/hosted/scripts.js
CHANGED
|
@@ -4,6 +4,7 @@
|
|
|
4
4
|
// a pid1 host's machine runs the entrypoint as its main process, which waits for the daemon and becomes init. Every
|
|
5
5
|
// connection runs stdio. Each is one argv, `sh -c '<script>' sh <args>`; shellCommand gives it as one string for a
|
|
6
6
|
// vendor whose exec takes a shell line.
|
|
7
|
+
import { PassThrough } from "node:stream";
|
|
7
8
|
import { DAEMON_IN_COMPUTER, helloDrift, SOCKET_DIR, WORK } from "../drift.js";
|
|
8
9
|
import { readOwnNetwork, TOOL_USER } from "../isolation.js";
|
|
9
10
|
import { fail, ok } from "../result.js";
|
|
@@ -210,7 +211,7 @@ export const ARCHIL_GATE = `${DAEMON_IN_COMPUTER.slice(0, DAEMON_IN_COMPUTER.las
|
|
|
210
211
|
*/
|
|
211
212
|
export function hostedCommands({ staging = "", deadline = "", user = TOOL_USER, display = "", maxDeadline = "", atomic = false, waitS = 600, gate, sha256 = "", nonce = "", sockets = SESSION_BUSES, rootOnly = [], network = "", } = {}) {
|
|
212
213
|
const init = initScriptArgs({ deadline, user, display, maxDeadline, network });
|
|
213
|
-
const initArgs = ["init",
|
|
214
|
+
const initArgs = ["init", ...initFlags({ deadline, user, maxDeadline, network })];
|
|
214
215
|
return {
|
|
215
216
|
install: argvOf(INSTALL_SCRIPT, [staging, DAEMON_IN_COMPUTER, WORK, SOCKET_DIR, ...(atomic ? ["1", sha256, nonce] : [])]),
|
|
216
217
|
start: argvOf(START_SCRIPT, [INIT_LOG, ...argvOf(INIT_SCRIPT, init)]),
|
|
@@ -221,6 +222,14 @@ export function hostedCommands({ staging = "", deadline = "", user = TOOL_USER,
|
|
|
221
222
|
entrypoint: argvOf(ENTRYPOINT_SCRIPT, [DAEMON_IN_COMPUTER, String(waitS), WORK, display, nonce, ...(gate === undefined ? [] : ["--gate", gate]), ...initArgs]),
|
|
222
223
|
};
|
|
223
224
|
}
|
|
225
|
+
/**
|
|
226
|
+
* init's flags, as start and a pid1 entrypoint give them: the deadline, the user, --own-network, and the most
|
|
227
|
+
* deadline.extend may move it to and the network form where given. A dial machine's AGENTRUN_INIT_ARGS, space-separated:
|
|
228
|
+
* no value holds a space.
|
|
229
|
+
*/
|
|
230
|
+
export function initFlags({ deadline = "", user = TOOL_USER, maxDeadline = "", network = "" }) {
|
|
231
|
+
return ["--deadline", deadline, "--user", user, "--own-network", ...(maxDeadline === "" ? [] : ["--max-deadline", maxDeadline]), ...(network === "" ? [] : ["--network", network])];
|
|
232
|
+
}
|
|
224
233
|
/** The init script's arguments: the daemon, the deadline, the user, home and the socket's folder, then the display, the
|
|
225
234
|
* maximum and the network form, each empty when not given, where any of them is. */
|
|
226
235
|
function initScriptArgs({ deadline = "", user = TOOL_USER, display = "", maxDeadline = "", network = "" }) {
|
|
@@ -301,3 +310,109 @@ export async function daemonDrift(connection, expected, timeoutMs, signal) {
|
|
|
301
310
|
}
|
|
302
311
|
return ok(found);
|
|
303
312
|
}
|
|
313
|
+
/**
|
|
314
|
+
* The sh a process starts under where the vendor has no kill that ends it (framedExec with no kill, SSH, Modal): one
|
|
315
|
+
* line on stderr, its pid and its start time (field 22 of /proc/<pid>/stat, counted past the command's name in
|
|
316
|
+
* parentheses, which may hold spaces), then the command in its place, which keeps both. pidLine takes the line off.
|
|
317
|
+
*/
|
|
318
|
+
export const PID_WRAPPER = ["sh", "-c", 'echo "$$ $(s=$(cat /proc/$$/stat) && set -- ${s##*") "} && echo "${20}")" >&2; exec "$@"', "sh"];
|
|
319
|
+
/**
|
|
320
|
+
* The sh pidKill starts, given the pid and start time: a pid whose process has ended, and may be another's now, is let
|
|
321
|
+
* be. Otherwise it kills the process and every process under it (an upload's head and base64 would wait on the open
|
|
322
|
+
* stdin, and hold stderr open, past their sh; a child that left the process group too): each is stopped as it is found,
|
|
323
|
+
* so none forks away, by its parent pid (field 4 of its stat) until a pass over /proc finds no more, and then all are
|
|
324
|
+
* killed. When the pid leads its own process group (field 5), as a command an sshd starts does, every process in that
|
|
325
|
+
* group is taken too: a background job whose parent exited is under no process the walk finds, but still in the group.
|
|
326
|
+
* A pid that leads no group of its own is let be past its tree, so a kill never reaches what it didn't start.
|
|
327
|
+
*/
|
|
328
|
+
export const PID_KILL = [
|
|
329
|
+
"pid=$1 start=$2",
|
|
330
|
+
'read -r s 2>/dev/null <"/proc/$pid/stat" || exit 0',
|
|
331
|
+
'set -- ${s##*") "}',
|
|
332
|
+
'[ "${20}" = "$start" ] || exit 0',
|
|
333
|
+
'group=; [ "$3" = "$pid" ] && group=$pid',
|
|
334
|
+
'kill -STOP "$pid" 2>/dev/null',
|
|
335
|
+
'all=" $pid " new=$pid',
|
|
336
|
+
'while [ -n "$new" ]; do',
|
|
337
|
+
"\tnew=",
|
|
338
|
+
"\tfor f in /proc/[0-9]*/stat; do",
|
|
339
|
+
'\t\tread -r s 2>/dev/null <"$f" || continue',
|
|
340
|
+
"\t\tp=${s%% *}",
|
|
341
|
+
'\t\tset -- ${s##*") "}',
|
|
342
|
+
'\t\tcase "$all" in *" $p "*) continue ;; esac',
|
|
343
|
+
'\t\tcase "$all" in *" $2 "*) ;; *) [ -n "$group" ] && [ "$3" = "$group" ] || continue ;; esac',
|
|
344
|
+
'\t\tall="$all$p " new="$new $p"; kill -STOP "$p" 2>/dev/null',
|
|
345
|
+
"\tdone",
|
|
346
|
+
"done",
|
|
347
|
+
"kill -KILL $all 2>/dev/null",
|
|
348
|
+
"exit 0",
|
|
349
|
+
].join("\n");
|
|
350
|
+
/** The argv that kills a process started under PID_WRAPPER, and every process under it, while its pid has that start time. */
|
|
351
|
+
export const pidKill = ({ pid, start }) => ["sh", "-c", PID_KILL, "sh", pid, start];
|
|
352
|
+
/**
|
|
353
|
+
* PID_WRAPPER's line taken off a process's stderr: take() is given each piece of stderr and answers what of it is the
|
|
354
|
+
* process's own (nothing while the line has not ended). pid answers once the line ends; undefined for a line that is
|
|
355
|
+
* no pid and start time, once end() came first (the stream ended), or past ms.
|
|
356
|
+
*/
|
|
357
|
+
export function pidLine() {
|
|
358
|
+
let held = Buffer.alloc(0);
|
|
359
|
+
let got = () => { };
|
|
360
|
+
const found = new Promise((done) => (got = done));
|
|
361
|
+
return {
|
|
362
|
+
take(bytes) {
|
|
363
|
+
const piece = Buffer.from(bytes.buffer, bytes.byteOffset, bytes.byteLength);
|
|
364
|
+
if (held === undefined)
|
|
365
|
+
return piece;
|
|
366
|
+
held = Buffer.concat([held, piece]);
|
|
367
|
+
const at = held.indexOf(0x0a);
|
|
368
|
+
if (at < 0)
|
|
369
|
+
return Buffer.alloc(0);
|
|
370
|
+
const line = /^([1-9][0-9]*) ([0-9]+)$/.exec(held.subarray(0, at).toString("latin1").trim());
|
|
371
|
+
got(line ? { pid: line[1], start: line[2] } : undefined);
|
|
372
|
+
const rest = held.subarray(at + 1);
|
|
373
|
+
held = undefined;
|
|
374
|
+
return rest;
|
|
375
|
+
},
|
|
376
|
+
end: () => got(undefined),
|
|
377
|
+
pid: async (ms) => {
|
|
378
|
+
let timer;
|
|
379
|
+
const late = new Promise((done) => (timer = setTimeout(() => done(undefined), ms)));
|
|
380
|
+
try {
|
|
381
|
+
return await Promise.race([found, late]);
|
|
382
|
+
}
|
|
383
|
+
finally {
|
|
384
|
+
clearTimeout(timer);
|
|
385
|
+
}
|
|
386
|
+
},
|
|
387
|
+
};
|
|
388
|
+
}
|
|
389
|
+
/**
|
|
390
|
+
* A stream's stderr with PID_WRAPPER's line taken off (pidLine), read from its start whether or not anyone reads what
|
|
391
|
+
* it passes on, so the pid comes; the rest waits for its reader.
|
|
392
|
+
*/
|
|
393
|
+
export function pidStderr(source) {
|
|
394
|
+
const line = pidLine();
|
|
395
|
+
const stderr = new PassThrough().on("error", () => { });
|
|
396
|
+
const drained = () => new Promise((done) => {
|
|
397
|
+
const go = () => {
|
|
398
|
+
stderr.off("drain", go).off("close", go);
|
|
399
|
+
done();
|
|
400
|
+
};
|
|
401
|
+
stderr.on("drain", go).on("close", go);
|
|
402
|
+
});
|
|
403
|
+
void (async () => {
|
|
404
|
+
try {
|
|
405
|
+
for await (const bytes of source) {
|
|
406
|
+
const own = line.take(bytes);
|
|
407
|
+
if (own.length > 0 && !stderr.destroyed && !stderr.write(own))
|
|
408
|
+
await drained();
|
|
409
|
+
}
|
|
410
|
+
}
|
|
411
|
+
catch {
|
|
412
|
+
// A stream that broke ends here, as one that ended.
|
|
413
|
+
}
|
|
414
|
+
line.end();
|
|
415
|
+
stderr.end();
|
|
416
|
+
})();
|
|
417
|
+
return { stderr, pid: line.pid };
|
|
418
|
+
}
|
|
@@ -7,7 +7,7 @@ export interface Ssh2Readable {
|
|
|
7
7
|
}
|
|
8
8
|
/**
|
|
9
9
|
* The slice of an ssh2 channel this provider uses (ssh2@1.17.0 lib/Channel.js): the channel is stdout. No signal
|
|
10
|
-
* request: ssh2 sends one only while stdin is open (Channel.js:237-246), so a kill goes by the
|
|
10
|
+
* request: ssh2 sends one only while stdin is open (Channel.js:237-246), so a kill goes by the pid instead (pidKill).
|
|
11
11
|
*/
|
|
12
12
|
export interface Ssh2Channel extends Ssh2Readable {
|
|
13
13
|
readonly stderr: Ssh2Readable;
|
|
@@ -83,11 +83,12 @@ export declare class SshExec {
|
|
|
83
83
|
/** Ends every connection to machine id, channels and all: the vendor calls it once the machine is deleted. */
|
|
84
84
|
closeAll(id: string): void;
|
|
85
85
|
/**
|
|
86
|
-
* argv as the host's login over SSH, under
|
|
87
|
-
* stdout and stderr are the channel's, piped
|
|
88
|
-
* exit-status, or -1 for an exit by signal or none. kill
|
|
89
|
-
*
|
|
90
|
-
*
|
|
86
|
+
* argv as the host's login over SSH, under PID_WRAPPER (./scripts.ts), as one channel on the machine's shared
|
|
87
|
+
* connection (#start). stdout and stderr are the channel's, piped, the wrapper's line taken off stderr; end sends end
|
|
88
|
+
* of file. wait answers once the channel closes, with the exit-status, or -1 for an exit by signal or none. kill
|
|
89
|
+
* starts pidKill over a channel of its own (on a connection with room), which ends the process and every process
|
|
90
|
+
* under it, then closes the exec's channel, never a connection; if the close does not come, wait answers -1 and the
|
|
91
|
+
* connection takes no new channel.
|
|
91
92
|
*/
|
|
92
93
|
exec(id: string, argv: readonly string[], ctx: HostCall): Promise<MachineProcess>;
|
|
93
94
|
}
|
package/dist/hosted/ssh-exec.js
CHANGED
|
@@ -1,12 +1,11 @@
|
|
|
1
1
|
// MachineHost.exec over SSH, for a vendor whose machines run an sshd: one connection per machine shared by its execs,
|
|
2
|
-
// each process under
|
|
3
|
-
// (Agent37's WebSocket tunnel) and as the login the host gives (Agent37's root and key). Every error it throws or
|
|
2
|
+
// each process under the wrapper that names its pid (./scripts.ts, PID_WRAPPER), through the stream the host gives to
|
|
3
|
+
// the machine's sshd (Agent37's WebSocket tunnel) and as the login the host gives (Agent37's root and key). Every error it throws or
|
|
4
4
|
// rejects with is redacted of the login's secrets. The host's own ssh2 package does the protocol; this package names
|
|
5
5
|
// only the slice it uses (Ssh2Module) and imports nothing at load.
|
|
6
|
-
import { randomBytes } from "node:crypto";
|
|
7
6
|
import { PassThrough } from "node:stream";
|
|
8
7
|
import { boundOf, redact } from "./process.js";
|
|
9
|
-
import { shellLine } from "./scripts.js";
|
|
8
|
+
import { PID_WRAPPER, pidKill, pidStderr, shellLine } from "./scripts.js";
|
|
10
9
|
/**
|
|
11
10
|
* ssh2's keepalive, which also keeps the edge from seeing an idle tunnel (the CLI's ServerAliveInterval is 60 s, cli.js:32).
|
|
12
11
|
* ssh2 pings every KEEPALIVE_MS and ends the connection at the tick after KEEPALIVE_COUNT_MAX pings went unanswered
|
|
@@ -35,26 +34,6 @@ const CHANNELS_PER_CONNECTION = 10;
|
|
|
35
34
|
* socket are both referenced), so the wait is short: a process that is done with its computers exits at most this late.
|
|
36
35
|
*/
|
|
37
36
|
const IDLE_MS = 2_000;
|
|
38
|
-
/**
|
|
39
|
-
* Every command runs under this sh, which writes its pid to $0 first and removes the file when the command ends. The
|
|
40
|
-
* instance's sshd starts each command as a session and group leader (seen live: sh's PID, PGID and SID are one), so
|
|
41
|
-
* the pid names the group a kill ends; ssh2 sends no signal once stdin has ended, and a signal ends one process only.
|
|
42
|
-
*/
|
|
43
|
-
const WRAPPER = '{ echo $$ > "$0"; } 2>/dev/null; "$@"; c=$?; rm -f "$0"; exit $c';
|
|
44
|
-
/**
|
|
45
|
-
* The kill: waits up to a second for the pid file, then kills the group when the pid leads one (the process alone
|
|
46
|
-
* otherwise, so a kill never reaches past what it started) and removes the file.
|
|
47
|
-
*/
|
|
48
|
-
const KILL_SCRIPT = [
|
|
49
|
-
"i=0",
|
|
50
|
-
'while [ ! -s "$0" ] && [ $i -lt 10 ]; do sleep 0.1; i=$((i+1)); done',
|
|
51
|
-
'p=$(cat "$0" 2>/dev/null); rm -f "$0"',
|
|
52
|
-
'[ -n "$p" ] || exit 0',
|
|
53
|
-
'g=$(cut -d" " -f5 "/proc/$p/stat" 2>/dev/null)',
|
|
54
|
-
// No "--": dash's kill reads it as a pid (seen live: "Illegal number: -").
|
|
55
|
-
'if [ "$g" = "$p" ]; then kill -KILL "-$p"; else kill -KILL "$p"; fi 2>/dev/null',
|
|
56
|
-
"exit 0",
|
|
57
|
-
].join("\n");
|
|
58
37
|
/** A tunnel failed before its SSH handshake, or while it carried the stream: the one failure a retry may repeat. */
|
|
59
38
|
export class SshTunnelError extends Error {
|
|
60
39
|
name = "SshTunnelError";
|
|
@@ -350,22 +329,23 @@ export class SshExec {
|
|
|
350
329
|
return error;
|
|
351
330
|
}
|
|
352
331
|
/**
|
|
353
|
-
* argv as the host's login over SSH, under
|
|
354
|
-
* stdout and stderr are the channel's, piped
|
|
355
|
-
* exit-status, or -1 for an exit by signal or none. kill
|
|
356
|
-
*
|
|
357
|
-
*
|
|
332
|
+
* argv as the host's login over SSH, under PID_WRAPPER (./scripts.ts), as one channel on the machine's shared
|
|
333
|
+
* connection (#start). stdout and stderr are the channel's, piped, the wrapper's line taken off stderr; end sends end
|
|
334
|
+
* of file. wait answers once the channel closes, with the exit-status, or -1 for an exit by signal or none. kill
|
|
335
|
+
* starts pidKill over a channel of its own (on a connection with room), which ends the process and every process
|
|
336
|
+
* under it, then closes the exec's channel, never a connection; if the close does not come, wait answers -1 and the
|
|
337
|
+
* connection takes no new channel.
|
|
358
338
|
*/
|
|
359
339
|
async exec(id, argv, ctx) {
|
|
360
|
-
const
|
|
361
|
-
const opened = await this.#start(id, shellLine(["sh", "-c", WRAPPER, pidFile, ...argv]), ctx, `the exec in ${id}`).catch((error) => {
|
|
340
|
+
const opened = await this.#start(id, shellLine([...PID_WRAPPER, ...argv]), ctx, `the exec in ${id}`).catch((error) => {
|
|
362
341
|
throw this.#redacted(id, error);
|
|
363
342
|
});
|
|
364
343
|
const { link: held, channel } = opened;
|
|
365
344
|
const stdout = new PassThrough();
|
|
366
|
-
const
|
|
345
|
+
const raw = new PassThrough();
|
|
367
346
|
channel.pipe(stdout);
|
|
368
|
-
channel.stderr.pipe(
|
|
347
|
+
channel.stderr.pipe(raw);
|
|
348
|
+
const { stderr, pid } = pidStderr(raw);
|
|
369
349
|
let ended = false;
|
|
370
350
|
let closed = false;
|
|
371
351
|
// ssh2 closes every channel when the connection goes (utils.js, ChannelManager.cleanup), so the close is enough.
|
|
@@ -398,32 +378,35 @@ export class SshExec {
|
|
|
398
378
|
kill: async () => {
|
|
399
379
|
if (closed)
|
|
400
380
|
return;
|
|
401
|
-
// The pid
|
|
402
|
-
|
|
403
|
-
|
|
404
|
-
|
|
405
|
-
killer.channel.stderr.resume();
|
|
406
|
-
killer.channel.eof();
|
|
407
|
-
const grace = boundOf(KILL_GRACE_MS, undefined, `the kill in ${id}`);
|
|
381
|
+
// The pid is the machine's, so the kill's channel goes on any connection with room, counted like an exec's. No
|
|
382
|
+
// pid line within the grace (or the process ended meanwhile): closing the channel below is what is left.
|
|
383
|
+
const target = await pid(KILL_GRACE_MS);
|
|
384
|
+
if (target !== undefined && !closed)
|
|
408
385
|
try {
|
|
409
|
-
await
|
|
410
|
-
|
|
411
|
-
|
|
386
|
+
const killer = await this.#start(id, shellLine(pidKill(target)), { timeoutMs: KILL_GRACE_MS }, `the kill in ${id}`);
|
|
387
|
+
killer.channel.resume();
|
|
388
|
+
killer.channel.stderr.resume();
|
|
389
|
+
killer.channel.eof();
|
|
390
|
+
const grace = boundOf(KILL_GRACE_MS, undefined, `the kill in ${id}`);
|
|
412
391
|
try {
|
|
413
|
-
killer.
|
|
392
|
+
await Promise.race([killer.closed, grace.promise]);
|
|
414
393
|
}
|
|
415
394
|
catch {
|
|
416
|
-
|
|
395
|
+
try {
|
|
396
|
+
killer.channel.close();
|
|
397
|
+
}
|
|
398
|
+
catch {
|
|
399
|
+
// Closed already.
|
|
400
|
+
}
|
|
401
|
+
}
|
|
402
|
+
finally {
|
|
403
|
+
grace.clear();
|
|
404
|
+
this.#letGo(killer.link);
|
|
417
405
|
}
|
|
418
406
|
}
|
|
419
|
-
|
|
420
|
-
|
|
421
|
-
this.#letGo(killer.link);
|
|
407
|
+
catch {
|
|
408
|
+
// No connection took the kill in time: closing the channel below is what is left.
|
|
422
409
|
}
|
|
423
|
-
}
|
|
424
|
-
catch {
|
|
425
|
-
// No connection took the kill in time: closing the channel below is what is left.
|
|
426
|
-
}
|
|
427
410
|
try {
|
|
428
411
|
channel.close();
|
|
429
412
|
}
|
package/dist/index.d.ts
CHANGED
|
@@ -10,7 +10,7 @@ export { pullTar, pullTarStream, pushTar, readFile, writeFile, type PulledTar }
|
|
|
10
10
|
export { listProcesses, readAll, type ReadAllResult } from "./processes.js";
|
|
11
11
|
export { type ActionMapper, createActionMapper, daemonWatch, redactWords, type ScreenFrame, type ScreenFrames, type ScreenWatch, ScreenWatchError, type ScreenWatchHandlers, toAction, type ViewerEvent, type WatchEndCode, watchEndCode, type WatchOptions, watchScreen, } from "./screen-watch.js";
|
|
12
12
|
export type { HostCall, HostFacts, Machine, MachineHost, MachineProcess, MachineRequest } from "./host.js";
|
|
13
|
-
export { HostedProvider, type HostedOptions, type HostedTimeouts } from "./hosted/provider.js";
|
|
13
|
+
export { HostedProvider, type HostedDial, type HostedOptions, type HostedTimeouts, hostedDaemonPath } from "./hosted/provider.js";
|
|
14
14
|
export { type DockerVolume, VOLUME_SUBPATH_ENGINE } from "./docker/volume.js";
|
|
15
15
|
export { DEFAULT_DOCKER_IMAGE, DOCKER_BASE, DOCKER_RECIPE, DOCKER_RECIPE_HAS, ensureImage, type EnsureImageOptions, IMAGE_REPOSITORY, IMAGE_TIMEOUT_MS, type ImageSource, imageSourceProblem, type Making, RECIPE_LABEL, recipeHash, recipeTag, } from "./docker/image.js";
|
|
16
16
|
export { EGRESS_IMAGE, EGRESS_LABEL, EGRESS_MOUNT, EGRESS_SOCKET } from "./docker/egress.js";
|
|
@@ -23,7 +23,7 @@ export { type ReleasedOlder, releaseOlder, sweep, type SweepOptions, type Swept
|
|
|
23
23
|
export { EPOCH_LABEL, epochOf, epochProblem, IDLE_TTL_LABEL, OWNER_LABEL, OWNER_SERVER_LABEL, ownerFilter, ownerLabels, ownerOf, RUN_HASH_LABEL, RUN_LABEL, runHash, runLabels, runProblem } from "./labels.js";
|
|
24
24
|
export { waitUntil, type WaitOptions } from "./wait.js";
|
|
25
25
|
export { type BuiltinProviderId, type ConformanceModule, descriptorOf, isProviderId, type ProviderEnv, providerFor, type ProviderId, providerIds, type ProviderModule, providerModule, type ProviderOptions, providers, } from "./providers.js";
|
|
26
|
-
export { type DescriptorKey, type DescriptorSetting, defineProvider, descriptorProblems, type HostCommand, type ParkedConfiguration, type ProviderDescriptor } from "./descriptor.js";
|
|
26
|
+
export { type DescriptorKey, type DescriptorSetting, defineProvider, descriptorProblems, type HostCommand, mountsOf, type ParkedConfiguration, type ProviderDescriptor } from "./descriptor.js";
|
|
27
27
|
export { AGENT37, AGENT37_BASE_TEMPLATE, BROWSER_IMAGE, BROWSERBASE, DAYTONA, DAYTONA_DEFAULT_SNAPSHOT, DAYTONA_GPU_SNAPSHOT, type DaytonaGpuSnapshot, DESCRIPTORS, daytonaGpu, daytonaGpuSnapshot, DOCKER, E2B, KERNEL, LOCAL, MODAL, ORGO, ORGO_BASE_IMAGE, PHONE_GROUPS, PHONE_HARNESS, PHONE_IMAGE, } from "./descriptors/index.js";
|
|
28
28
|
export type { PhoneLedger, PhoneLedgerEntry } from "./phone-harness.js";
|
|
29
29
|
export { type ComputerSettingKind, configurationOf, entryOf, imagesOf, type KindCapabilities, kindOf, kindServed, type ProviderEntry, PROVIDERS, SETTING_KIND_NAMES, SETTING_KINDS, type SettingKindSpec, serves, servingRefusals, type TestConnection, type TestConnectionOptions, } from "./kinds.js";
|
package/dist/index.js
CHANGED
|
@@ -9,7 +9,7 @@ export { checkActions, isKey } from "./actions.js";
|
|
|
9
9
|
export { pullTar, pullTarStream, pushTar, readFile, writeFile } from "./files.js";
|
|
10
10
|
export { listProcesses, readAll } from "./processes.js";
|
|
11
11
|
export { createActionMapper, daemonWatch, redactWords, ScreenWatchError, toAction, watchEndCode, watchScreen, } from "./screen-watch.js";
|
|
12
|
-
export { HostedProvider } from "./hosted/provider.js";
|
|
12
|
+
export { HostedProvider, hostedDaemonPath } from "./hosted/provider.js";
|
|
13
13
|
// The provider classes and their option types are not here: each provider loads by its id through the registry
|
|
14
14
|
// (providerFor), or from its own subpath (@parcha/agentrun-computer/docker, /daytona, /modal, /e2b, /agent37, /orgo,
|
|
15
15
|
// /phone-harness, /browserbase, /kernel, /local: each the provider file's name).
|
|
@@ -27,7 +27,7 @@ export { releaseOlder, sweep } from "./janitor.js";
|
|
|
27
27
|
export { EPOCH_LABEL, epochOf, epochProblem, IDLE_TTL_LABEL, OWNER_LABEL, OWNER_SERVER_LABEL, ownerFilter, ownerLabels, ownerOf, RUN_HASH_LABEL, RUN_LABEL, runHash, runLabels, runProblem } from "./labels.js";
|
|
28
28
|
export { waitUntil } from "./wait.js";
|
|
29
29
|
export { descriptorOf, isProviderId, providerFor, providerIds, providerModule, providers, } from "./providers.js";
|
|
30
|
-
export { defineProvider, descriptorProblems } from "./descriptor.js";
|
|
30
|
+
export { defineProvider, descriptorProblems, mountsOf } from "./descriptor.js";
|
|
31
31
|
export { AGENT37, AGENT37_BASE_TEMPLATE, BROWSER_IMAGE, BROWSERBASE, DAYTONA, DAYTONA_DEFAULT_SNAPSHOT, DAYTONA_GPU_SNAPSHOT, DESCRIPTORS, daytonaGpu, daytonaGpuSnapshot, DOCKER, E2B, KERNEL, LOCAL, MODAL, ORGO, ORGO_BASE_IMAGE, PHONE_GROUPS, PHONE_HARNESS, PHONE_IMAGE, } from "./descriptors/index.js";
|
|
32
32
|
export { configurationOf, entryOf, imagesOf, kindOf, kindServed, PROVIDERS, SETTING_KIND_NAMES, SETTING_KINDS, serves, servingRefusals, } from "./kinds.js";
|
|
33
33
|
export * from "./conformance/index.js";
|
package/dist/kinds.d.ts
CHANGED
|
@@ -68,6 +68,8 @@ export interface ProviderEntry {
|
|
|
68
68
|
readonly mounts: readonly MountKind[];
|
|
69
69
|
/** Why its computers take none of these mount kinds, each in one sentence (descriptor.ts). */
|
|
70
70
|
readonly unmounted?: Unmounted;
|
|
71
|
+
/** Mount kinds its computers take only where the operator sets the key named, by kind (descriptor.ts, mountsOf). */
|
|
72
|
+
readonly mountsWith?: Readonly<Partial<Record<MountKind, string>>>;
|
|
71
73
|
/**
|
|
72
74
|
* The network forms its computers make, its descriptor's: ["none"], a remote computer whose programs run in a network
|
|
73
75
|
* of their own; ["open"], the vendor's own network, which no allowlist of this server reaches; Docker's all three.
|
package/dist/modal.js
CHANGED
|
@@ -3,6 +3,7 @@ import { MODAL } from "./descriptors/modal.js";
|
|
|
3
3
|
import { DEFAULT_CPUS, DEFAULT_MEMORY_MB, WORK } from "./drift.js";
|
|
4
4
|
import { HostError } from "./host.js";
|
|
5
5
|
import { HostedProvider } from "./hosted/provider.js";
|
|
6
|
+
import { PID_WRAPPER, pidKill, pidStderr } from "./hosted/scripts.js";
|
|
6
7
|
import { isWhole } from "./hosted/values.js";
|
|
7
8
|
import { ok } from "./result.js";
|
|
8
9
|
/** A GPU as the SDK takes it: the type, then :count past one ("T4", "H100:8"). */
|
|
@@ -287,21 +288,33 @@ class ModalHost {
|
|
|
287
288
|
}
|
|
288
289
|
}
|
|
289
290
|
/**
|
|
290
|
-
* exec in binary mode, as root (Modal containers always run as root), outside computerd
|
|
291
|
-
*
|
|
291
|
+
* exec in binary mode, as root (Modal containers always run as root), outside computerd, under PID_WRAPPER
|
|
292
|
+
* (./hosted/scripts.ts): Modal has no kill for an exec, so kill runs pidKill in an exec of its own, which ends the
|
|
293
|
+
* process and every process under it, then closes its stdin. Not once wait() read its exit: its pid may be another's.
|
|
292
294
|
*/
|
|
293
295
|
async exec(id, argv, ctx) {
|
|
294
|
-
void ctx;
|
|
295
296
|
const sandbox = await this.#sandbox(id);
|
|
296
|
-
const program = await sandbox.exec([...argv], { mode: "binary" });
|
|
297
|
-
const
|
|
297
|
+
const program = await sandbox.exec([...PID_WRAPPER, ...argv], { mode: "binary" });
|
|
298
|
+
const { stderr, pid } = pidStderr(program.stderr);
|
|
299
|
+
let exited = false;
|
|
300
|
+
let waited;
|
|
298
301
|
return {
|
|
299
302
|
stdout: program.stdout,
|
|
300
|
-
stderr
|
|
303
|
+
stderr,
|
|
301
304
|
write: (bytes) => program.stdin.writeBytes(bytes),
|
|
302
305
|
end: () => program.stdin.close(),
|
|
303
|
-
wait: () => program.wait(),
|
|
304
|
-
kill: async () =>
|
|
306
|
+
wait: () => (waited ??= program.wait().then((code) => ((exited = true), code))),
|
|
307
|
+
kill: async () => {
|
|
308
|
+
try {
|
|
309
|
+
const target = exited ? undefined : await pid(ctx.timeoutMs);
|
|
310
|
+
if (target !== undefined && !exited)
|
|
311
|
+
await (await sandbox.exec(pidKill(target), { mode: "binary" })).wait();
|
|
312
|
+
}
|
|
313
|
+
catch {
|
|
314
|
+
// Best effort: kill never throws.
|
|
315
|
+
}
|
|
316
|
+
await program.stdin.close().catch(() => { });
|
|
317
|
+
},
|
|
305
318
|
};
|
|
306
319
|
}
|
|
307
320
|
}
|
package/dist/providers.d.ts
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import type { RunFolder } from "./conformance/types.js";
|
|
1
|
+
import type { DialKit, RunFolder } from "./conformance/types.js";
|
|
2
2
|
import type { ProviderDescriptor } from "./descriptor.js";
|
|
3
3
|
import { DESCRIPTORS } from "./descriptors/index.js";
|
|
4
4
|
import type { MachineHost } from "./host.js";
|
|
@@ -9,7 +9,7 @@ export type ProviderEnv = Readonly<Record<string, string | undefined>>;
|
|
|
9
9
|
/**
|
|
10
10
|
* What a conformance module exports, the CLI's contract for a module given by path too: createProvider(), and optionally
|
|
11
11
|
* createDriftedProvider() for the drift case, conformanceImage() for a run that names no image, conformanceNotes for
|
|
12
|
-
* the report, mountFor() for the mount cases, and settingsFromEnv(env), the provider's options it reads from the
|
|
12
|
+
* the report, mountFor() for the mount cases, dialKit() for a dial host, and settingsFromEnv(env), the provider's options it reads from the
|
|
13
13
|
* environment by its COMPUTER_<ID>_* names, with ENV, those names by role. The CLI passes each function its options,
|
|
14
14
|
* which a module may ignore.
|
|
15
15
|
*/
|
|
@@ -23,6 +23,8 @@ export interface ConformanceModule {
|
|
|
23
23
|
signal?: AbortSignal;
|
|
24
24
|
}): string | Promise<string>;
|
|
25
25
|
conformanceNotes?: string[];
|
|
26
|
+
/** For a host whose machines dial (facts.connect "dial"): the server they dial, for provider.dial-token. */
|
|
27
|
+
dialKit?(options?: object): DialKit | Promise<DialKit>;
|
|
26
28
|
/**
|
|
27
29
|
* A provider on the shared adapter: its MachineHost from the same settings, with no live switch, since it makes
|
|
28
30
|
* nothing by itself. The live sweep lists and deletes through it, and the exec pre-check (--exec-stream) calls its
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@parcha/agentrun-computer",
|
|
3
|
-
"version": "0.1.0-beta.
|
|
3
|
+
"version": "0.1.0-beta.41",
|
|
4
4
|
"description": "The open platform's computer protocol: a daemon in every computer, and one provisioner per vendor, starting with Docker.",
|
|
5
5
|
"keywords": [
|
|
6
6
|
"agentrun",
|