@parcha/agentrun-serve 0.1.0-beta.30 → 0.1.0-beta.31
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/CHANGELOG.md +50 -0
- package/README.md +182 -26
- package/dist/agents.d.ts +8 -3
- package/dist/agents.js +4 -1
- package/dist/app.js +23 -8
- package/dist/background.d.ts +2 -1
- package/dist/background.js +41 -20
- package/dist/box-egress-vm.d.ts +125 -0
- package/dist/box-egress-vm.js +350 -0
- package/dist/box-egress.d.ts +3 -2
- package/dist/box-relay-vm.d.ts +4 -0
- package/dist/box-relay-vm.js +76 -55
- package/dist/boxes.d.ts +7 -2
- package/dist/boxes.js +30 -11
- package/dist/cap.d.ts +2 -1
- package/dist/cap.js +2 -1
- package/dist/cli.js +30 -4
- package/dist/console/assets/{index-34jfpIbQ.js → index-CnJBf7Lc.js} +90 -85
- package/dist/console/index.html +1 -1
- package/dist/decisions.d.ts +3 -3
- package/dist/decisions.js +4 -3
- package/dist/files.d.ts +3 -0
- package/dist/files.js +3 -0
- package/dist/grade.d.ts +5 -2
- package/dist/grade.js +13 -5
- package/dist/harness-app.js +10 -1
- package/dist/host-check.d.ts +12 -0
- package/dist/host-check.js +34 -0
- package/dist/key-words.d.ts +37 -0
- package/dist/key-words.js +40 -0
- package/dist/learn/grade.d.ts +3 -0
- package/dist/learn/grade.js +3 -1
- package/dist/learn/index.d.ts +1 -1
- package/dist/learn/index.js +1 -1
- package/dist/learn/reflect.js +2 -0
- package/dist/learn/skills.d.ts +2 -0
- package/dist/learn/skills.js +16 -0
- package/dist/learning-routes.d.ts +30 -1
- package/dist/learning-routes.js +173 -19
- package/dist/learning-run.d.ts +1 -1
- package/dist/learning-run.js +25 -4
- package/dist/learning-words.d.ts +7 -3
- package/dist/learning-words.js +26 -8
- package/dist/learning.d.ts +9 -1
- package/dist/learning.js +57 -2
- package/dist/mcp-gateway.d.ts +96 -0
- package/dist/mcp-gateway.js +408 -0
- package/dist/mcp-meta.d.ts +17 -0
- package/dist/mcp-meta.js +38 -0
- package/dist/mcp-servers.d.ts +26 -5
- package/dist/mcp-servers.js +53 -10
- package/dist/openapi.js +52 -9
- package/dist/page.d.ts +2 -0
- package/dist/page.js +3 -0
- package/dist/place.d.ts +3 -2
- package/dist/place.js +11 -3
- package/dist/print.d.ts +7 -0
- package/dist/print.js +13 -0
- package/dist/run-box.js +15 -5
- package/dist/runnable.d.ts +8 -1
- package/dist/runnable.js +51 -2
- package/dist/runs.d.ts +15 -8
- package/dist/runs.js +66 -13
- package/dist/secrets.d.ts +15 -0
- package/dist/secrets.js +20 -2
- package/dist/server.d.ts +10 -1
- package/dist/server.js +111 -29
- package/dist/tool-gate.d.ts +18 -3
- package/dist/tool-gate.js +44 -13
- package/dist/tool-keys.d.ts +2 -1
- package/dist/tool-keys.js +4 -8
- package/dist/tool-plane.d.ts +5 -1
- package/dist/tool-plane.js +30 -6
- package/dist/tools.js +8 -5
- package/dist/web-fetch.js +3 -3
- package/package.json +12 -16
package/dist/background.js
CHANGED
|
@@ -18,14 +18,16 @@ export const BACKGROUND_SIGNALS = ["TERM", "INT", "KILL"];
|
|
|
18
18
|
/** The tools, by name. */
|
|
19
19
|
export const BACKGROUND_TOOLS = ["bash_background", "bash_output", "bash_input", "bash_signal", "bash_list"];
|
|
20
20
|
/** The tools that run a command in the box, never on this server: bash, and the background tools. The tool gate holds
|
|
21
|
-
* each to bash's setting (tool-gate.ts SHELL_FAMILY)
|
|
21
|
+
* each to bash's setting (tool-gate.ts SHELL_FAMILY); under bash: ask, bash_output, bash_list and bash_signal run unasked
|
|
22
|
+
* (tool-gate.ts SHELL_WATCHERS), since an id reaches only this run's own processes (idOf, noSuch). */
|
|
22
23
|
export const SHELL_TOOLS = ["bash", ...BACKGROUND_TOOLS];
|
|
23
24
|
const MIB = BACKGROUND_LOG_BYTES / (1024 * 1024);
|
|
24
25
|
/** Each signal's number, for an exit code past 128. */
|
|
25
26
|
const SIGNAL_NAMES = { 1: "HUP", 2: "INT", 3: "QUIT", 6: "ABRT", 9: "KILL", 13: "PIPE", 14: "ALRM", 15: "TERM" };
|
|
26
27
|
/**
|
|
27
28
|
* The runner, started under its own session by a start: `$1` the log's directory, `$2` the box-only directory, `$3` the
|
|
28
|
-
* log's cap, `$4` work/, `$5` the command
|
|
29
|
+
* log's cap, `$4` work/, `$5` the command, `$6` 1 to run it on a terminal: `script -qfec`, which gives it a pty, copies
|
|
30
|
+
* what the fifo brings to the pty and what the pty shows to the log, and exits with the command's code. It holds the fifo's write end open (a `sleep`), runs the command under a
|
|
29
31
|
* session of its own (so a signal to the command's group never reaches the runner) with the fifo as stdin, and keeps
|
|
30
32
|
* the first `$3` bytes of its output, written to the log as they come (head, unbuffered by stdbuf where the box has it;
|
|
31
33
|
* else the last few KiB may wait in head's buffer until the process ends). Once the output's last writer is gone, it
|
|
@@ -50,11 +52,12 @@ const RUNNER = [
|
|
|
50
52
|
'sleep 2147483647 > "$C/in" & h=$!; echo "$h" > "$C/hold"',
|
|
51
53
|
// The command's group goes in C, beside the fifo (pgid), the only copy any call trusts; one byte past the cap marks
|
|
52
54
|
// the log capped.
|
|
53
|
-
`{ setsid -w sh -c 'echo $$ > "$1/.pgid" && mv -f "$1/.pgid" "$1/pgid"; cd "$2" || exit 126;
|
|
55
|
+
`{ setsid -w sh -c 'echo $$ > "$1/.pgid" && mv -f "$1/.pgid" "$1/pgid"; cd "$2" || exit 126; b=$(command -v bash 2>/dev/null) || b=; if [ "$4" = 1 ]; then SHELL=\${b:-/bin/sh}; AGENTRUN_BG_CMD=$3; AGENTRUN_BG_SID=$1/tsid; export SHELL AGENTRUN_BG_CMD AGENTRUN_BG_SID; exec script -qfec "echo \\$\\$ > \\"\\$AGENTRUN_BG_SID\\"; c=\\$AGENTRUN_BG_CMD; unset AGENTRUN_BG_CMD AGENTRUN_BG_SID; exec \\"\\$SHELL\\" -c \\"\\$c\\"" /dev/null; fi; [ -n "$b" ] && exec bash -c "$3"; exec sh -c "$3"' agentrun-background "$C" "$4" "$5" "$6" < "$C/in" 2>&1; echo $? > "$C/code"; } | { keep; [ -n "$(head -c 1 | od -An -c)" ] && { : > "$C/capped"; put capped ""; }; cat > /dev/null; }`,
|
|
54
56
|
// A child the command left running (a server started with &, its output sent elsewhere) keeps the process going: the
|
|
55
|
-
// group is waited for,
|
|
56
|
-
// the
|
|
57
|
-
|
|
57
|
+
// group is waited for, and on a terminal the terminal's session (tsid, script's command's own pid), whose processes
|
|
58
|
+
// outlive script; so it stays running, signalled, counted and ended at the run's end until no live process of either
|
|
59
|
+
// is left.
|
|
60
|
+
`g=$(cat "$C/pgid" 2>/dev/null); live() { [ -n "$g" ] || return 1; s=$(cat "$C/tsid" 2>/dev/null); if [ -r /proc/self/stat ]; then cat /proc/[0-9]*/stat 2>/dev/null | awk -v g="$g" -v s="$s" '{ sub(/^[0-9]+ \\(.*\\) /, ""); if ($1 != "Z" && ($3 == g || (s != "" && $4 == s))) n++ } END { exit n > 0 ? 0 : 1 }'; else kill -0 "-$g" 2>/dev/null; fi; }; while live; do sleep 0.5; done`,
|
|
58
61
|
'kill "$h" 2>/dev/null; code=$(cat "$C/code" 2>/dev/null); [ -n "$code" ] || code=255',
|
|
59
62
|
'printf \'%s\\n\' "$code" > "$C/.exit" && mv -f "$C/.exit" "$C/exit"; put exit "$code"; rm -f "$C/in"',
|
|
60
63
|
].join("\n");
|
|
@@ -82,8 +85,21 @@ const SCRIPT = [
|
|
|
82
85
|
// by its real path, an exclusive new file there renamed over its name on the same filesystem; a failed check skips it.
|
|
83
86
|
`put() { [ -n "$rw" ] || return 0; ( cd -P "$d/$1" 2>/dev/null && [ "$(pwd -P)" = "$rw/background/$1" ] || exit 0; t=$(mktemp ./.put.XXXXXX) || exit 0; if [ "$4" = n ]; then printf '%s\\n' "$3"; else printf '%s' "$3"; fi > "$t" && { mv -fT "$t" "./$2" 2>/dev/null || { [ -L "./$2" ] && rm -f "./$2"; mv -f "$t" "./$2"; }; } || rm -f "$t" ) 2>/dev/null; return 0; }`,
|
|
84
87
|
'known() { [ -d "$c/$1" ] || { [ ! -L "$d" ] && [ -d "$d/$1" ]; }; }',
|
|
88
|
+
// A tty process's command runs in the pty's own session (tsid: script's command, its leader), whose processes may
|
|
89
|
+
// outlive script: the groups of that session (sgs), and its terminal's foreground group (fgs, the tpgid its processes
|
|
90
|
+
// read): an interactive shell's running job is a group of its own, which Ctrl-C reaches and the shell's own does not.
|
|
91
|
+
`sgs() { cat /proc/[0-9]*/stat 2>/dev/null | awk -v s="$1" '{ sub(/^[0-9]+ \\(.*\\) /, ""); if ($1 != "Z" && $4 == s) print $3 }' | sort -u; }`,
|
|
92
|
+
`fgs() { cat /proc/[0-9]*/stat 2>/dev/null | awk -v s="$1" '{ sub(/^[0-9]+ \\(.*\\) /, ""); if ($1 != "Z" && $4 == s && $6 > 0) print $6 }' | sort -u; }`,
|
|
93
|
+
// Signal $2 to process $1's group (its box-only copy, never work/'s); on a tty process (its box-only mark), to its
|
|
94
|
+
// terminal's processes instead, as a terminal delivers it: INT to the foreground group alone (Ctrl-C), TERM and KILL to
|
|
95
|
+
// every group of the terminal's session (script would turn INT into TERM, and a script signalled itself exits 0):
|
|
96
|
+
// script exits with the code its command ended with, and what its command left on the terminal is reached after
|
|
97
|
+
// script is gone. Before script has its command, script itself. Each group is signalled once, and a send that finds
|
|
98
|
+
// its group already gone is no failure: only a missing group is.
|
|
99
|
+
'hit() { g=$(cat "$c/$1/pgid" 2>/dev/null); [ -n "$g" ] || return 1; t=; if [ -f "$c/$1/tty" ]; then s=$(cat "$c/$1/tsid" 2>/dev/null); if [ -n "$s" ]; then if [ "$2" = INT ]; then t=$(fgs "$s"); fi; [ -n "$t" ] || t=$(sgs "$s"); fi; fi; if [ -z "$t" ]; then kill -"$2" -"$g" 2>/dev/null; return 0; fi; for p in $t; do kill -"$2" -"$p" 2>/dev/null; done; return 0; }',
|
|
85
100
|
'case $op in',
|
|
86
101
|
'start)',
|
|
102
|
+
' if [ "$5" = 1 ] && ! command -v script >/dev/null 2>&1; then echo noscript; exit 0; fi',
|
|
87
103
|
' mkdir -p "$c" || exit 1; [ -L "$d" ] && { echo linkdir; exit 0; }; mkdir -p "$d" || exit 1',
|
|
88
104
|
' k=0; until mkdir "$c/.lock" 2>/dev/null; do k=$((k+1)); if [ $k -ge 100 ]; then rmdir "$c/.lock" 2>/dev/null; k=0; fi; sleep 0.05; done',
|
|
89
105
|
' trap \'rmdir "$c/.lock" 2>/dev/null\' EXIT',
|
|
@@ -92,8 +108,8 @@ const SCRIPT = [
|
|
|
92
108
|
' mkdir "$d/$i" && mkdir "$c/$i" || exit 1',
|
|
93
109
|
' printf \'%s\' "$1" > "$c/$i/command"; date -u +%Y-%m-%dT%H:%M:%SZ > "$c/$i/started"; : > "$d/$i/output.log"',
|
|
94
110
|
' put "$i" command "$1"; put "$i" started "$(cat "$c/$i/started")" n',
|
|
95
|
-
' mkfifo "$c/$i/in" || exit 1',
|
|
96
|
-
' setsid -f nohup sh -c "$4" agentrun-background "$d/$i" "$c/$i" "$3" "$w" "$1" </dev/null >/dev/null 2>"$c/$i/err" || exit 1',
|
|
111
|
+
' mkfifo "$c/$i/in" || exit 1; if [ "$5" = 1 ]; then : > "$c/$i/tty"; put "$i" tty ""; fi',
|
|
112
|
+
' setsid -f nohup sh -c "$4" agentrun-background "$d/$i" "$c/$i" "$3" "$w" "$1" "$5" </dev/null >/dev/null 2>"$c/$i/err" || exit 1',
|
|
97
113
|
' k=0; while [ ! -s "$c/$i/pgid" ] && [ ! -f "$c/$i/exit" ] && [ $k -lt 200 ]; do sleep 0.05; k=$((k+1)); done',
|
|
98
114
|
' [ -s "$c/$i/pgid" ] || [ -f "$c/$i/exit" ] || { cat "$c/$i/err" >&2; echo "process $i did not start" >&2; exit 1; }',
|
|
99
115
|
' put "$i" pid "$(cat "$c/$i/pgid" 2>/dev/null)" n; echo "started $i $(cat "$c/$i/pgid" 2>/dev/null)";;',
|
|
@@ -114,8 +130,7 @@ const SCRIPT = [
|
|
|
114
130
|
'signal)',
|
|
115
131
|
' known "$1" || { echo none; exit 0; }',
|
|
116
132
|
' st=$(state "$1"); [ "$st" = running ] || { echo "was $st"; exit 0; }',
|
|
117
|
-
'
|
|
118
|
-
' kill -"$2" -"$g" 2>/dev/null',
|
|
133
|
+
' hit "$1" "$2" || { echo "was $st"; exit 0; }',
|
|
119
134
|
' k=0; while [ ! -f "$c/$1/exit" ] && [ $k -lt 40 ]; do sleep 0.05; k=$((k+1)); done',
|
|
120
135
|
' echo "state $(state "$1")";;',
|
|
121
136
|
'list)',
|
|
@@ -123,11 +138,11 @@ const SCRIPT = [
|
|
|
123
138
|
// however many ended before.
|
|
124
139
|
' t=0; o=0; picked=""; for i in $(ids | sort -rn); do t=$((t+1)); if [ -f "$c/$i/exit" ] || [ "$(state "$i")" != running ]; then [ $o -lt "$1" ] || continue; o=$((o+1)); fi; picked="$picked $i"; done',
|
|
125
140
|
' echo "total $t"',
|
|
126
|
-
` for i in $picked; do printf '%s\\t%s\\t%s\\t%s\\t%s\\t' "$i" "$(state "$i")" "$(size "$i")" "$(cat "$c/$i/started" 2>/dev/null)" "$([ -f "$c/$i/ended" ] && echo ended)"; head -c 200 "$c/$i/command" 2>/dev/null | base64 | tr -d '\\n'; echo; done;;`,
|
|
141
|
+
` for i in $picked; do printf '%s\\t%s\\t%s\\t%s\\t%s\\t%s\\t' "$i" "$(state "$i")" "$(size "$i")" "$(cat "$c/$i/started" 2>/dev/null)" "$([ -f "$c/$i/ended" ] && echo ended)" "$([ -f "$c/$i/tty" ] && echo tty)"; head -c 200 "$c/$i/command" 2>/dev/null | base64 | tr -d '\\n'; echo; done;;`,
|
|
127
142
|
'end)',
|
|
128
|
-
' n=0; for i in $(running); do : > "$c/$i/ended"; put "$i" ended "";
|
|
143
|
+
' n=0; for i in $(running); do : > "$c/$i/ended"; put "$i" ended ""; hit "$i" TERM; n=$((n+1)); done',
|
|
129
144
|
' k=0; while [ $k -lt 60 ] && [ -n "$(running)" ]; do sleep 0.05; k=$((k+1)); done',
|
|
130
|
-
' for i in $(running); do
|
|
145
|
+
' for i in $(running); do hit "$i" KILL; done',
|
|
131
146
|
' k=0; while [ $k -lt 40 ] && [ -n "$(running)" ]; do sleep 0.05; k=$((k+1)); done',
|
|
132
147
|
' for i in $(running); do r=$(cat "$c/$i/runner" 2>/dev/null); [ -n "$r" ] && kill -KILL -"$r" 2>/dev/null; done',
|
|
133
148
|
' echo "ended $n";;',
|
|
@@ -208,13 +223,19 @@ const ID_SCHEMA = { type: "string", pattern: "^[1-9][0-9]*$", description: "The
|
|
|
208
223
|
const schema = (properties, required) => ({ type: "object", additionalProperties: false, properties, required });
|
|
209
224
|
const bashBackground = defineTool({
|
|
210
225
|
name: "bash_background",
|
|
211
|
-
description: `Start a shell command in the background in the run's box, from its work/, and answer at once with its id. Each call is a fresh shell (bash, else sh). The command runs until it and every child it left in its process group (a server started with &) have exited, bash_signal ends it, or the run ends, which ends it. Its stdout and stderr go together to work/background/<id>/output.log, which the run's files keep: the first ${MIB} MiB of them, later output dropped while it runs on. Its stdin is a pipe that bash_input writes. At most ${BACKGROUND_MAX} run at once in a run's box. Read its output with bash_output, list them with bash_list.`,
|
|
212
|
-
parameters: schema({
|
|
226
|
+
description: `Start a shell command in the background in the run's box, from its work/, and answer at once with its id. Each call is a fresh shell (bash, else sh). The command runs until it and every child it left in its process group (a server started with &) have exited, bash_signal ends it, or the run ends, which ends it. Its stdout and stderr go together to work/background/<id>/output.log, which the run's files keep: the first ${MIB} MiB of them, later output dropped while it runs on. Its stdin is a pipe that bash_input writes; tty: true gives it a terminal instead. At most ${BACKGROUND_MAX} run at once in a run's box. Read its output with bash_output, list them with bash_list.`,
|
|
227
|
+
parameters: schema({
|
|
228
|
+
command: { type: "string", minLength: 1, description: "The command, as you would give bash." },
|
|
229
|
+
tty: { type: "boolean", description: "Run it on a terminal (a pty, through script in the box), for a program that needs one: a prompt, a REPL, a progress display. Its log then holds what the terminal shows (your input echoed, lines ending in \\r\\n); bash_input types into the terminal, and bash_signal INT is Ctrl-C to its foreground job." },
|
|
230
|
+
}, ["command"]),
|
|
213
231
|
execute: async (args, api, context) => {
|
|
214
232
|
const command = String(args.command ?? "");
|
|
233
|
+
const tty = args.tty === true;
|
|
215
234
|
if (!command.trim())
|
|
216
235
|
throw new Error("command is the shell command to start");
|
|
217
|
-
const said = (await inBox(api.env, "start", [command, String(BACKGROUND_MAX), String(BACKGROUND_LOG_BYTES), RUNNER], context)).trim();
|
|
236
|
+
const said = (await inBox(api.env, "start", [command, String(BACKGROUND_MAX), String(BACKGROUND_LOG_BYTES), RUNNER, tty ? "1" : "0"], context)).trim();
|
|
237
|
+
if (said === "noscript")
|
|
238
|
+
throw new Error("this box has no `script` program (util-linux), so tty: true cannot run here: start the command without tty");
|
|
218
239
|
if (said === "linkdir")
|
|
219
240
|
throw new Error("work/background is a link, so no background process starts: remove it, and its logs go in a directory of their own again");
|
|
220
241
|
const full = /^full (\d+)/.exec(said);
|
|
@@ -225,7 +246,7 @@ const bashBackground = defineTool({
|
|
|
225
246
|
throw new Error(`the run's box did not start the process: ${said.slice(0, 300) || "it said nothing"}`);
|
|
226
247
|
const [, id, group] = started;
|
|
227
248
|
const log = `${BACKGROUND_DIR}/${id}/output.log`;
|
|
228
|
-
return forModel(`Started background process ${id}${group ? ` (process group ${group})` : ""}. Its output goes to ${log} in work/: read it with bash_output {"id": "${id}"}, write its stdin with bash_input, end it with bash_signal.`, { details: { id, log, ...(group ? { pid: Number(group) } : {}) } });
|
|
249
|
+
return forModel(`Started background process ${id}${group ? ` (process group ${group})` : ""}${tty ? " on a terminal" : ""}. Its output goes to ${log} in work/: read it with bash_output {"id": "${id}"}, write its stdin with bash_input, end it with bash_signal.`, { details: { id, log, ...(group ? { pid: Number(group) } : {}), ...(tty ? { tty } : {}) } });
|
|
229
250
|
},
|
|
230
251
|
});
|
|
231
252
|
const bashOutput = defineTool({
|
|
@@ -332,14 +353,14 @@ const bashList = defineTool({
|
|
|
332
353
|
const [first = "", ...rows] = said.split("\n");
|
|
333
354
|
const total = Number(/^total (\d+)$/.exec(first)?.[1] ?? 0);
|
|
334
355
|
const all = rows.filter(Boolean).map((line) => {
|
|
335
|
-
const [id = "", state = "", size = "0", started = "", ended = "", command = ""] = line.split("\t");
|
|
336
|
-
return { id, ...stateOf(state), size: Number(size) || 0, ...(started ? { started } : {}), ...(ended ? { ended_at_run_end: true } : {}), command: new TextDecoder().decode(decode(command)) };
|
|
356
|
+
const [id = "", state = "", size = "0", started = "", ended = "", tty = "", command = ""] = line.split("\t");
|
|
357
|
+
return { id, ...stateOf(state), size: Number(size) || 0, ...(started ? { started } : {}), ...(ended ? { ended_at_run_end: true } : {}), ...(tty ? { tty: true } : {}), command: new TextDecoder().decode(decode(command)) };
|
|
337
358
|
}).sort((a, b) => Number(b.id) - Number(a.id));
|
|
338
359
|
const shown = [...all.filter((p) => p.state === "running"), ...all.filter((p) => p.state !== "running")].slice(0, BACKGROUND_LIST_MAX);
|
|
339
360
|
if (!shown.length)
|
|
340
361
|
return forModel("No background processes in this run's box: bash_background starts one.", { details: { processes: [] } });
|
|
341
362
|
const running = all.filter((p) => p.state === "running").length;
|
|
342
|
-
const lines = shown.map((p) => `${p.id}: ${stateWords(p, p.ended_at_run_end === true)}; ${p.size.toLocaleString("en-US")} bytes of output; started ${p.started ?? "at an unknown time"}; ${JSON.stringify(p.command.replace(/\s+/g, " ").slice(0, 120))}`);
|
|
363
|
+
const lines = shown.map((p) => `${p.id}: ${stateWords(p, p.ended_at_run_end === true)}; ${p.size.toLocaleString("en-US")} bytes of output; started ${p.started ?? "at an unknown time"}; ${JSON.stringify(p.command.replace(/\s+/g, " ").slice(0, 120))}${p.tty ? " (tty)" : ""}`);
|
|
343
364
|
const left = total - shown.length;
|
|
344
365
|
return forModel(`${running} of at most ${BACKGROUND_MAX} running.\n${lines.join("\n")}${left > 0 ? `\n(and ${left} older ones that ended)` : ""}`, { details: { running, processes: shown } });
|
|
345
366
|
},
|
|
@@ -0,0 +1,125 @@
|
|
|
1
|
+
import { type BoxEgress } from "./box-egress.js";
|
|
2
|
+
/** What `docker info` is asked on a Mac: the daemon's OS type, its kernel, the name it gives its OS, and its host name. */
|
|
3
|
+
export declare const VM_INFO_FORMAT = "{{.OSType}}|{{.KernelVersion}}|{{.OperatingSystem}}|{{.Name}}";
|
|
4
|
+
/** The VM runtimes a box's network goes on for, on a Mac. */
|
|
5
|
+
export declare const VM_RUNTIMES: readonly ["Docker Desktop", "OrbStack", "colima"];
|
|
6
|
+
export type VmRuntime = (typeof VM_RUNTIMES)[number];
|
|
7
|
+
/** Where this Docker runs, by `docker info`'s answer to VM_INFO_FORMAT (`answer`), on a server with `platform` and
|
|
8
|
+
* `kernel` (os.release()): a VM runtime this server knows, or why not. Pure. A runtime counts only when docker info
|
|
9
|
+
* names it (OperatingSystem `Docker Desktop` or `OrbStack`, or a colima host Name) AND its kernel is not this
|
|
10
|
+
* machine's: a VM in between. Anything else is a reason, never a guess. */
|
|
11
|
+
export declare function vmDockerOf(answer: string, here: {
|
|
12
|
+
platform: string;
|
|
13
|
+
kernel: string;
|
|
14
|
+
}): {
|
|
15
|
+
runtime: VmRuntime;
|
|
16
|
+
} | {
|
|
17
|
+
reason: string;
|
|
18
|
+
};
|
|
19
|
+
/** `docker`, with `args`, in the server's environment through the cut a run's takes, nothing on its stdin, within
|
|
20
|
+
* `timeoutMs`: its exit code (or `timeout`, or the spawn's error code) and its output. */
|
|
21
|
+
export type DockerCall = (args: readonly string[], timeoutMs: number, signal?: AbortSignal) => Promise<{
|
|
22
|
+
code: number | string;
|
|
23
|
+
stdout: string;
|
|
24
|
+
stderr: string;
|
|
25
|
+
}>;
|
|
26
|
+
export declare const dockerCall: (env: NodeJS.ProcessEnv) => DockerCall;
|
|
27
|
+
/** Where this Mac's Docker runs (vmDockerOf), asked of `docker info` within its bound; a daemon the docker CLI does not
|
|
28
|
+
* reach by a local unix socket (DOCKER_HOST, or a context, elsewhere) is not this Mac's VM, and gets no network. */
|
|
29
|
+
export declare function vmDocker(env: NodeJS.ProcessEnv, options?: {
|
|
30
|
+
platform?: string;
|
|
31
|
+
kernel?: string;
|
|
32
|
+
timeoutMs?: number;
|
|
33
|
+
docker?: DockerCall;
|
|
34
|
+
}): Promise<{
|
|
35
|
+
runtime: VmRuntime;
|
|
36
|
+
} | {
|
|
37
|
+
reason: string;
|
|
38
|
+
}>;
|
|
39
|
+
/** The proxy container's image: `node:22-bookworm-slim` (the kernel needs Node 22.19 or later), pinned to the multi-arch
|
|
40
|
+
* index digest Docker Hub served for that tag on 2026-11-05 (Node 22.23.4), as BOX_BASE is pinned (box-image.ts), so every Mac runs the
|
|
41
|
+
* same Node. To move it, read the tag's index digest again the way BOX_BASE says, and put it here. */
|
|
42
|
+
export declare const EGRESS_IMAGE = "node:22-bookworm-slim@sha256:4b150b48b4116e2a22f2c39cc27025fbf4f153e2078fd3b14d159fdb5c7279da";
|
|
43
|
+
/** The label the proxy container and its volume carry: the server's key (egressKeyOf). */
|
|
44
|
+
export declare const EGRESS_LABEL = "agentrun.egress";
|
|
45
|
+
/** Where the volume is in the proxy container, and its code. */
|
|
46
|
+
export declare const EGRESS_VOLUME_MOUNT = "/egress";
|
|
47
|
+
export declare const EGRESS_CODE_MOUNT = "/code";
|
|
48
|
+
/** What the entry logs once its socket listens. */
|
|
49
|
+
export declare const EGRESS_READY = "egress ready";
|
|
50
|
+
/** How long the start waits for EGRESS_READY once the container runs. */
|
|
51
|
+
export declare const EGRESS_READY_MS = 10000;
|
|
52
|
+
/** How often the server looks for its proxy container, to start it again when it is gone. */
|
|
53
|
+
export declare const EGRESS_WATCH_MS = 5000;
|
|
54
|
+
/** How long the start's pull of EGRESS_IMAGE may take. */
|
|
55
|
+
export declare const EGRESS_PULL_MS: number;
|
|
56
|
+
/** Connections open at once through the server's one proxy, every run's together. */
|
|
57
|
+
export declare const VM_MAX_TUNNELS = 256;
|
|
58
|
+
/** This server's key: its data root's hash, so two servers on one daemon get two proxies and two volumes. */
|
|
59
|
+
export declare const egressKeyOf: (data: string) => string;
|
|
60
|
+
/** The proxy container's name, and its volume's, for a key. */
|
|
61
|
+
export declare const egressNameOf: (key: string) => string;
|
|
62
|
+
/** The entry the proxy container runs (`node /code/main.js <socket>`): it refuses to start with no list, for a proxy with
|
|
63
|
+
* none would reach every public host, and logs EGRESS_READY once its socket listens. Its decisions go to its log. */
|
|
64
|
+
export declare const MAIN: string;
|
|
65
|
+
/** The kernel's two proxy files, as built: they import only `node:` modules and each other. */
|
|
66
|
+
export declare const PROXY_FILES: readonly ["egress-proxy.js", "egress-table.js"];
|
|
67
|
+
/** The code the proxy container binds read-only: PROXY_FILES from the kernel this server runs, and MAIN, written into
|
|
68
|
+
* `<data>/egress/<12 hex of their hash>/` once (a directory per version, so a running container's code never changes
|
|
69
|
+
* under it). Its path, real (Docker binds it by that). */
|
|
70
|
+
export declare function egressCode(data: string): Promise<string>;
|
|
71
|
+
/** `docker volume create` of the server's volume: tmpfs, so no stale socket outlives the VM, and owned by the server's
|
|
72
|
+
* uid:gid with mode 0700, as the proxy's socket (0600) and the box (both run as that uid) need. */
|
|
73
|
+
export declare const egressVolumeArgs: (key: string, uid: number, gid: number) => string[];
|
|
74
|
+
/** `docker run` of the proxy container. Docker's default network, the only container with one; no published port, no
|
|
75
|
+
* Docker socket, no variable but the list; a read-only root, no capability, no privilege to gain, the server's uid:gid;
|
|
76
|
+
* bounded memory, processes and log. Pinned by a golden test. */
|
|
77
|
+
export declare const egressRunArgs: (options: {
|
|
78
|
+
key: string;
|
|
79
|
+
uid: number;
|
|
80
|
+
gid: number;
|
|
81
|
+
code: string;
|
|
82
|
+
allow: readonly string[];
|
|
83
|
+
}) => string[];
|
|
84
|
+
/** The proxy container's state: `ready` once it listens; `preparing` while it is pulled, started or started again;
|
|
85
|
+
* `unavailable` when it could not start, with why. */
|
|
86
|
+
export type EgressState = {
|
|
87
|
+
state: "ready";
|
|
88
|
+
} | {
|
|
89
|
+
state: "preparing" | "unavailable";
|
|
90
|
+
reason: string;
|
|
91
|
+
};
|
|
92
|
+
/** `volume`: the volume a run's box mounts, once it is made; absent when it could not be. */
|
|
93
|
+
export type EgressContainer = {
|
|
94
|
+
readonly volume?: string;
|
|
95
|
+
state(): EgressState;
|
|
96
|
+
close(): Promise<void>;
|
|
97
|
+
};
|
|
98
|
+
/**
|
|
99
|
+
* The server's proxy container (see the head of this file): made ready now when Docker has its image (within
|
|
100
|
+
* EGRESS_READY_MS), else pulled and started in the background; then watched every `watchMs`, and started again when it
|
|
101
|
+
* is gone or stopped. `onChange` is told each new state. `close` stops the watch and removes the container; its volume
|
|
102
|
+
* stays, for boxes still mounting it and the next start.
|
|
103
|
+
*/
|
|
104
|
+
export declare function startEgressContainer(options: {
|
|
105
|
+
env: NodeJS.ProcessEnv;
|
|
106
|
+
data: string;
|
|
107
|
+
allow: readonly string[];
|
|
108
|
+
uid: number;
|
|
109
|
+
gid: number;
|
|
110
|
+
onChange?: (state: EgressState) => void;
|
|
111
|
+
docker?: DockerCall;
|
|
112
|
+
watchMs?: number;
|
|
113
|
+
readyMs?: number;
|
|
114
|
+
pullMs?: number;
|
|
115
|
+
}): Promise<EgressContainer>;
|
|
116
|
+
/** What the server hands a run's process on a Mac with the proxy container: its volume's name, which the run's Docker
|
|
117
|
+
* boxes mount read-only at EGRESS_MOUNT. Unset on Linux, where the run's process starts its own proxy (box-egress.ts). */
|
|
118
|
+
export declare const RUN_BOX_VOLUME_ENV = "AGENTRUN_RUN_BOX_VOLUME";
|
|
119
|
+
/** The run's process's reading of RUN_BOX_VOLUME_ENV: the volume, or undefined when it is unset or not a name this
|
|
120
|
+
* server makes. */
|
|
121
|
+
export declare function runBoxVolumeOf(env: NodeJS.ProcessEnv): string | undefined;
|
|
122
|
+
/** A run's box's egress on a Mac: the server's proxy container, its socket in the volume the Docker provider mounts on
|
|
123
|
+
* every box (run-box.ts), so nothing for the run's process to start or bind. The volume outlives each proxy, so a box
|
|
124
|
+
* made before one started again reaches the new one. */
|
|
125
|
+
export declare const vmEgress: BoxEgress;
|