@parcha/agentrun-serve 0.1.0-beta.29 → 0.1.0-beta.30
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 +188 -0
- package/README.md +564 -97
- package/dist/adapters.d.ts +35 -6
- package/dist/adapters.js +152 -26
- package/dist/agents.d.ts +11 -5
- package/dist/agents.js +16 -10
- package/dist/app.js +80 -20
- package/dist/background.d.ts +28 -0
- package/dist/background.js +376 -0
- package/dist/box-egress.d.ts +98 -0
- package/dist/box-egress.js +259 -0
- package/dist/box-image.d.ts +1 -1
- package/dist/box-image.js +9 -7
- package/dist/box-relay-vm.d.ts +80 -0
- package/dist/box-relay-vm.js +389 -0
- package/dist/box-relay.d.ts +8 -5
- package/dist/box-relay.js +14 -9
- package/dist/boxes.d.ts +21 -3
- package/dist/boxes.js +14 -7
- package/dist/browser-cost.d.ts +41 -0
- package/dist/browser-cost.js +158 -0
- package/dist/cap.d.ts +129 -21
- package/dist/cap.js +338 -67
- package/dist/coding-tools.d.ts +7 -3
- package/dist/coding-tools.js +16 -10
- package/dist/console/THIRD-PARTY-NOTICES.txt +580 -0
- package/dist/console/assets/index-34jfpIbQ.js +481 -0
- package/dist/console/assets/index-DUHbWcIP.css +1 -0
- package/dist/console/index.html +2 -2
- package/dist/content-filter.d.ts +7 -0
- package/dist/content-filter.js +23 -0
- package/dist/decision-rows.d.ts +11 -3
- package/dist/decision-rows.js +18 -11
- package/dist/errors.d.ts +2 -0
- package/dist/errors.js +5 -2
- package/dist/files.d.ts +6 -1
- package/dist/files.js +7 -2
- package/dist/grade.d.ts +7 -10
- package/dist/grade.js +31 -51
- package/dist/graph-served.d.ts +23 -0
- package/dist/graph-served.js +75 -0
- package/dist/graph-view.d.ts +7 -5
- package/dist/graph-view.js +10 -3
- package/dist/harness-app.d.ts +4 -2
- package/dist/harness-app.js +101 -19
- package/dist/harness-opencode/adapter.d.ts +45 -0
- package/dist/harness-opencode/adapter.js +272 -0
- package/dist/harness-opencode/binary.d.ts +18 -0
- package/dist/harness-opencode/binary.js +87 -0
- package/dist/harness-opencode/config.d.ts +24 -0
- package/dist/harness-opencode/config.js +61 -0
- package/dist/harness-opencode/harness.d.ts +77 -0
- package/dist/harness-opencode/harness.js +620 -0
- package/dist/harness-opencode/host/mcp.d.ts +40 -0
- package/dist/harness-opencode/host/mcp.js +208 -0
- package/dist/harness-opencode/host/policy.d.ts +71 -0
- package/dist/harness-opencode/host/policy.js +419 -0
- package/dist/harness-opencode/host/server.d.ts +55 -0
- package/dist/harness-opencode/host/server.js +165 -0
- package/dist/harness-opencode/index.d.ts +9 -0
- package/dist/harness-opencode/index.js +6 -0
- package/dist/harness-opencode/verify.d.ts +32 -0
- package/dist/harness-opencode/verify.js +75 -0
- package/dist/harness-opencode/versions.d.ts +33 -0
- package/dist/harness-opencode/versions.js +64 -0
- package/dist/harness.d.ts +4 -1
- package/dist/harness.js +5 -2
- package/dist/host-tools.d.ts +12 -2
- package/dist/host-tools.js +26 -12
- package/dist/host.js +6 -2
- package/dist/hosted-search.js +5 -1
- package/dist/import.d.ts +2 -2
- package/dist/import.js +4 -3
- package/dist/key-check.d.ts +3 -2
- package/dist/key-check.js +7 -3
- package/dist/key-refusal.js +8 -4
- package/dist/learn/check.d.ts +10 -1
- package/dist/learn/check.js +19 -1
- package/dist/learn/grade.d.ts +3 -2
- package/dist/learn/grade.js +25 -13
- package/dist/learn/index.d.ts +2 -2
- package/dist/learn/index.js +2 -2
- package/dist/learn/notes.js +2 -1
- package/dist/learn/reflect.d.ts +2 -2
- package/dist/learn/reflect.js +51 -5
- package/dist/learn/secrets.js +28 -8
- package/dist/learn/skills.d.ts +17 -4
- package/dist/learn/skills.js +79 -6
- package/dist/learned-workflow.d.ts +59 -0
- package/dist/learned-workflow.js +191 -0
- package/dist/learning-run.d.ts +7 -5
- package/dist/learning-run.js +45 -23
- package/dist/learning-words.d.ts +6 -3
- package/dist/learning-words.js +8 -5
- package/dist/learning.d.ts +44 -3
- package/dist/learning.js +148 -3
- package/dist/mcp-servers.d.ts +7 -4
- package/dist/mcp-servers.js +5 -0
- package/dist/mcp.js +5 -5
- package/dist/model.d.ts +13 -11
- package/dist/model.js +11 -10
- package/dist/openapi/agentrun-v2.openapi.json +1 -0
- package/dist/openapi.js +358 -48
- package/dist/paid-calls.d.ts +5 -1
- package/dist/paid-calls.js +1 -1
- package/dist/pause.d.ts +2 -0
- package/dist/place.d.ts +27 -4
- package/dist/place.js +42 -10
- package/dist/propose.d.ts +2 -0
- package/dist/propose.js +38 -8
- package/dist/request-start.d.ts +12 -4
- package/dist/request-start.js +139 -15
- package/dist/run-box.d.ts +56 -4
- package/dist/run-box.js +173 -23
- package/dist/runnable.d.ts +5 -2
- package/dist/runnable.js +12 -4
- package/dist/runs.d.ts +258 -36
- package/dist/runs.js +830 -89
- package/dist/secrets.d.ts +12 -0
- package/dist/secrets.js +18 -0
- package/dist/server.d.ts +131 -35
- package/dist/server.js +467 -105
- package/dist/sign-ins.d.ts +99 -0
- package/dist/sign-ins.js +288 -0
- package/dist/skills.d.ts +16 -4
- package/dist/skills.js +35 -7
- package/dist/stats.d.ts +7 -2
- package/dist/stats.js +28 -7
- package/dist/tool-gate.d.ts +266 -0
- package/dist/tool-gate.js +554 -0
- package/dist/tool-keys.d.ts +46 -10
- package/dist/tool-keys.js +44 -2
- package/dist/tool-plane.d.ts +112 -27
- package/dist/tool-plane.js +205 -47
- package/dist/tools.d.ts +69 -14
- package/dist/tools.js +143 -24
- package/dist/trigger-secrets.d.ts +32 -0
- package/dist/trigger-secrets.js +143 -0
- package/dist/triggers.d.ts +439 -0
- package/dist/triggers.js +863 -0
- package/dist/turn-end.d.ts +3 -2
- package/dist/turn-end.js +3 -2
- package/dist/usage-lines.d.ts +26 -0
- package/dist/usage-lines.js +28 -0
- package/dist/validate.d.ts +2 -0
- package/dist/validate.js +11 -2
- package/dist/webhooks.d.ts +11 -4
- package/dist/webhooks.js +17 -1
- package/launcher/agentrun-serve +1 -1
- package/package.json +19 -13
- package/dist/console/assets/index-BN7oVcO4.js +0 -445
- package/dist/console/assets/index-By-jkqki.css +0 -1
|
@@ -0,0 +1,376 @@
|
|
|
1
|
+
import { defineTool } from "@earendil-works/pi-durable";
|
|
2
|
+
import { BOX_WORK } from "./place.js";
|
|
3
|
+
import { forModel } from "./result.js";
|
|
4
|
+
/** The most background processes that run at once in one run's box. */
|
|
5
|
+
export const BACKGROUND_MAX = 8;
|
|
6
|
+
/** The most bytes of output one background process's log keeps: later output is dropped, and the process runs on. */
|
|
7
|
+
export const BACKGROUND_LOG_BYTES = 16 * 1024 * 1024;
|
|
8
|
+
/** The most bytes of output one `bash_output` call answers, and its default. */
|
|
9
|
+
export const BACKGROUND_PAGE_BYTES = 48 * 1024;
|
|
10
|
+
/** The most bytes one `bash_input` call writes. */
|
|
11
|
+
export const BACKGROUND_INPUT_BYTES = 64 * 1024;
|
|
12
|
+
/** The most processes `bash_list` names: every running one, then the newest that ended. */
|
|
13
|
+
export const BACKGROUND_LIST_MAX = 40;
|
|
14
|
+
/** Where a background process's log and state live, under the run's work/. */
|
|
15
|
+
export const BACKGROUND_DIR = "background";
|
|
16
|
+
/** The signals `bash_signal` sends. */
|
|
17
|
+
export const BACKGROUND_SIGNALS = ["TERM", "INT", "KILL"];
|
|
18
|
+
/** The tools, by name. */
|
|
19
|
+
export const BACKGROUND_TOOLS = ["bash_background", "bash_output", "bash_input", "bash_signal", "bash_list"];
|
|
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). */
|
|
22
|
+
export const SHELL_TOOLS = ["bash", ...BACKGROUND_TOOLS];
|
|
23
|
+
const MIB = BACKGROUND_LOG_BYTES / (1024 * 1024);
|
|
24
|
+
/** Each signal's number, for an exit code past 128. */
|
|
25
|
+
const SIGNAL_NAMES = { 1: "HUP", 2: "INT", 3: "QUIT", 6: "ABRT", 9: "KILL", 13: "PIPE", 14: "ALRM", 15: "TERM" };
|
|
26
|
+
/**
|
|
27
|
+
* 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. It holds the fifo's write end open (a `sleep`), runs the command under a
|
|
29
|
+
* session of its own (so a signal to the command's group never reaches the runner) with the fifo as stdin, and keeps
|
|
30
|
+
* the first `$3` bytes of its output, written to the log as they come (head, unbuffered by stdbuf where the box has it;
|
|
31
|
+
* else the last few KiB may wait in head's buffer until the process ends). Once the output's last writer is gone, it
|
|
32
|
+
* writes the exit code.
|
|
33
|
+
*/
|
|
34
|
+
const RUNNER = [
|
|
35
|
+
'L=$1; C=$2; cap=$3; echo $$ > "$C/runner"',
|
|
36
|
+
// A display copy in work/ (put): from inside the log's directory, entered with cd -P and checked to be
|
|
37
|
+
// work/background/<id> itself by its real path, written to a new file there (mktemp: an exclusive create, so never a
|
|
38
|
+
// planted link) and renamed over its name on that same filesystem, the name taken as a file even where a link to a
|
|
39
|
+
// directory stands there (mv -T; else that link is removed first), so a link planted at the name, or at the
|
|
40
|
+
// directory, is never followed. A check that fails skips the copy; the temporary file (hidden, a dot name) is removed
|
|
41
|
+
// on every path; no call reads a copy back.
|
|
42
|
+
`put() { [ -n "$rw" ] || return 0; ( cd -P "$L" 2>/dev/null && [ "$(pwd -P)" = "$rw/background/\${L##*/}" ] || exit 0; t=$(mktemp ./.put.XXXXXX) || exit 0; printf '%s\\n' "$2" > "$t" && { mv -fT "$t" "./$1" 2>/dev/null || { [ -L "./$1" ] && rm -f "./$1"; mv -f "$t" "./$1"; }; } || rm -f "$t" ) 2>/dev/null; return 0; }`,
|
|
43
|
+
// The log is opened once, here, where start has just made it a regular file, then checked as opened: the file the
|
|
44
|
+
// open reached must be work/background/<id>/output.log itself (by its real path, so through no link) and a regular
|
|
45
|
+
// file, else nothing is kept. What later stands at its name is never written through.
|
|
46
|
+
'rw=$(cd "$4" 2>/dev/null && pwd -P); [ -f "$L/output.log" ] && [ ! -L "$L/output.log" ] && exec 4>>"$L/output.log" || exec 4>/dev/null',
|
|
47
|
+
'[ -n "$rw" ] && [ "$(readlink /proc/self/fd/4 2>/dev/null)" = "$rw/background/\${L##*/}/output.log" ] && [ -f /proc/self/fd/4 ] || exec 4>/dev/null',
|
|
48
|
+
// The first $cap bytes, written as they come: head holds its output in a buffer unless stdbuf unbuffers it.
|
|
49
|
+
'if stdbuf -o0 true 2>/dev/null; then keep() { stdbuf -o0 head -c "$cap" >&4; }; else keep() { head -c "$cap" >&4; }; fi',
|
|
50
|
+
'sleep 2147483647 > "$C/in" & h=$!; echo "$h" > "$C/hold"',
|
|
51
|
+
// The command's group goes in C, beside the fifo (pgid), the only copy any call trusts; one byte past the cap marks
|
|
52
|
+
// the log capped.
|
|
53
|
+
`{ setsid -w sh -c 'echo $$ > "$1/.pgid" && mv -f "$1/.pgid" "$1/pgid"; cd "$2" || exit 126; if command -v bash >/dev/null 2>&1; then exec bash -c "$3"; fi; exec sh -c "$3"' agentrun-background "$C" "$4" "$5" < "$C/in" 2>&1; echo $? > "$C/code"; } | { keep; [ -n "$(head -c 1 | od -An -c)" ] && { : > "$C/capped"; put capped ""; }; cat > /dev/null; }`,
|
|
54
|
+
// A child the command left running (a server started with &, its output sent elsewhere) keeps the process going: the
|
|
55
|
+
// group is waited for, so it stays running, signalled, counted and ended at the run's end until no live process of
|
|
56
|
+
// the group is left.
|
|
57
|
+
`g=$(cat "$C/pgid" 2>/dev/null); live() { [ -n "$g" ] || return 1; if [ -r /proc/self/stat ]; then cat /proc/[0-9]*/stat 2>/dev/null | awk -v g="$g" '{ sub(/^[0-9]+ \\(.*\\) /, ""); if ($1 != "Z" && $3 == g) n++ } END { exit n > 0 ? 0 : 1 }'; else kill -0 "-$g" 2>/dev/null; fi; }; while live; do sleep 0.5; done`,
|
|
58
|
+
'kill "$h" 2>/dev/null; code=$(cat "$C/code" 2>/dev/null); [ -n "$code" ] || code=255',
|
|
59
|
+
'printf \'%s\\n\' "$code" > "$C/.exit" && mv -f "$C/.exit" "$C/exit"; put exit "$code"; rm -f "$C/in"',
|
|
60
|
+
].join("\n");
|
|
61
|
+
/**
|
|
62
|
+
* Every call's script: `$1` work/, `$2` the operation, the rest its arguments. Each answers on stdout in lines a parse
|
|
63
|
+
* below reads. A state is `running`, `exited <code>` or `lost`. What a call trusts (the group, the exit code, the start
|
|
64
|
+
* time, the command, the run's-end mark) is read from the box-only directory `c` alone, which the run's file tools
|
|
65
|
+
* cannot write; of work/background/<id>/ only output.log is read, and only as a regular file reached through no link.
|
|
66
|
+
*/
|
|
67
|
+
const SCRIPT = [
|
|
68
|
+
'w=$1; op=$2; shift 2; d=$w/background; c=${TMPDIR:-/tmp}/.agentrun-background; rw=$(cd "$w" 2>/dev/null && pwd -P)',
|
|
69
|
+
`alive() { [ -n "$1" ] || return 1; if [ -r /proc/self/stat ]; then s=$(sed 's/^.*) //' "/proc/$1/stat" 2>/dev/null | cut -d' ' -f1); [ -n "$s" ] && [ "$s" != Z ] && [ "$s" != X ]; else kill -0 "$1" 2>/dev/null; fi; }`,
|
|
70
|
+
'state() { if [ -f "$c/$1/exit" ]; then echo "exited $(cat "$c/$1/exit")"; elif alive "$(cat "$c/$1/runner" 2>/dev/null)"; then echo running; else echo lost; fi; }',
|
|
71
|
+
// The ids this box started (c), and with them, for the list alone, those of work/ (a box before this one's).
|
|
72
|
+
'cids() { for p in "$c"/*; do i=${p##*/}; case $i in ""|*[!0-9]*) continue;; esac; [ -d "$p" ] && echo "$i"; done; }',
|
|
73
|
+
'ids() { { cids; [ -L "$d" ] || for p in "$d"/*; do i=${p##*/}; case $i in ""|*[!0-9]*) continue;; esac; echo "$i"; done; } | sort -un; }',
|
|
74
|
+
'running() { for i in $(cids); do [ -f "$c/$i/exit" ] && continue; [ "$(state "$i")" = running ] && echo "$i"; done; }',
|
|
75
|
+
// $1's log, opened first and then checked as opened (fd 3): the file the open reached is work/background/<id>/output.log
|
|
76
|
+
// itself, by its real path (so through no link), and a regular file. Every read after is of that open file
|
|
77
|
+
// (/proc/self/fd/3), so a link swapped in at its name after the check is never read. 0: open; 1: there is no log;
|
|
78
|
+
// 2: it is not to be read (a link, not a regular file, or a box with no /proc to check it by).
|
|
79
|
+
'openlog() { exec 3<&-; f="$d/$1/output.log"; [ -e "$f" ] || [ -L "$f" ] || return 1; [ -f "$f" ] && [ ! -L "$f" ] && [ -n "$rw" ] || return 2; exec 3< "$f" 2>/dev/null || return 2; [ "$(readlink /proc/self/fd/3 2>/dev/null)" = "$rw/background/$1/output.log" ] && [ -f /proc/self/fd/3 ] || { exec 3<&-; return 2; }; }',
|
|
80
|
+
'size() { if openlog "$1"; then echo $(($(wc -c < /proc/self/fd/3))); exec 3<&-; else echo 0; fi; }',
|
|
81
|
+
// A display copy in work/ (the runner's put, by id): from inside work/background/<id>, entered with cd -P and checked
|
|
82
|
+
// by its real path, an exclusive new file there renamed over its name on the same filesystem; a failed check skips it.
|
|
83
|
+
`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
|
+
'known() { [ -d "$c/$1" ] || { [ ! -L "$d" ] && [ -d "$d/$1" ]; }; }',
|
|
85
|
+
'case $op in',
|
|
86
|
+
'start)',
|
|
87
|
+
' mkdir -p "$c" || exit 1; [ -L "$d" ] && { echo linkdir; exit 0; }; mkdir -p "$d" || exit 1',
|
|
88
|
+
' 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
|
+
' trap \'rmdir "$c/.lock" 2>/dev/null\' EXIT',
|
|
90
|
+
' n=$(running | wc -l); if [ "$n" -ge "$2" ]; then echo "full $n"; exit 0; fi',
|
|
91
|
+
' i=1; while [ -e "$d/$i" ] || [ -L "$d/$i" ] || [ -e "$c/$i" ]; do i=$((i+1)); done',
|
|
92
|
+
' mkdir "$d/$i" && mkdir "$c/$i" || exit 1',
|
|
93
|
+
' printf \'%s\' "$1" > "$c/$i/command"; date -u +%Y-%m-%dT%H:%M:%SZ > "$c/$i/started"; : > "$d/$i/output.log"',
|
|
94
|
+
' 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',
|
|
97
|
+
' k=0; while [ ! -s "$c/$i/pgid" ] && [ ! -f "$c/$i/exit" ] && [ $k -lt 200 ]; do sleep 0.05; k=$((k+1)); done',
|
|
98
|
+
' [ -s "$c/$i/pgid" ] || [ -f "$c/$i/exit" ] || { cat "$c/$i/err" >&2; echo "process $i did not start" >&2; exit 1; }',
|
|
99
|
+
' put "$i" pid "$(cat "$c/$i/pgid" 2>/dev/null)" n; echo "started $i $(cat "$c/$i/pgid" 2>/dev/null)";;',
|
|
100
|
+
'output)',
|
|
101
|
+
' known "$1" || { echo none; exit 0; }',
|
|
102
|
+
' st=$(state "$1"); echo "state $st"',
|
|
103
|
+
' openlog "$1"; r=$?; [ $r = 2 ] && { echo notlog; exit 0; }; n=0; [ $r = 0 ] && n=$(($(wc -c < /proc/self/fd/3))); echo "size $n"',
|
|
104
|
+
' [ -f "$c/$1/capped" ] && echo capped; [ -f "$c/$1/ended" ] && echo ended; echo --',
|
|
105
|
+
' m=$((n - $2)); [ $m -gt "$3" ] && m=$3',
|
|
106
|
+
' if [ $m -gt 0 ] && [ $r = 0 ]; then tail -c +$(($2 + 1)) /proc/self/fd/3 | head -c $m | base64; fi;;',
|
|
107
|
+
'input)',
|
|
108
|
+
' known "$1" || { echo none; exit 0; }',
|
|
109
|
+
' st=$(state "$1"); [ "$st" = running ] || { echo "state $st"; exit 0; }',
|
|
110
|
+
' [ -f "$c/$1/closed" ] && { echo closed; exit 0; }',
|
|
111
|
+
' if [ -n "$2" ]; then exec 3<>"$c/$1/in" || exit 1; printf \'%s\' "$2" >&3 || exit 1; exec 3>&-; fi',
|
|
112
|
+
' if [ "$3" = 1 ]; then : > "$c/$1/closed"; kill "$(cat "$c/$1/hold" 2>/dev/null)" 2>/dev/null; fi',
|
|
113
|
+
' echo sent;;',
|
|
114
|
+
'signal)',
|
|
115
|
+
' known "$1" || { echo none; exit 0; }',
|
|
116
|
+
' st=$(state "$1"); [ "$st" = running ] || { echo "was $st"; exit 0; }',
|
|
117
|
+
' g=$(cat "$c/$1/pgid" 2>/dev/null); [ -n "$g" ] || { echo "was $st"; exit 0; }',
|
|
118
|
+
' kill -"$2" -"$g" 2>/dev/null',
|
|
119
|
+
' k=0; while [ ! -f "$c/$1/exit" ] && [ $k -lt 40 ]; do sleep 0.05; k=$((k+1)); done',
|
|
120
|
+
' echo "state $(state "$1")";;',
|
|
121
|
+
'list)',
|
|
122
|
+
// Every running one, then the newest $1 others, chosen before any of their details is read: the list costs the same
|
|
123
|
+
// however many ended before.
|
|
124
|
+
' 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
|
+
' 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;;`,
|
|
127
|
+
'end)',
|
|
128
|
+
' n=0; for i in $(running); do : > "$c/$i/ended"; put "$i" ended ""; g=$(cat "$c/$i/pgid" 2>/dev/null); [ -n "$g" ] && kill -TERM -"$g" 2>/dev/null; n=$((n+1)); done',
|
|
129
|
+
' k=0; while [ $k -lt 60 ] && [ -n "$(running)" ]; do sleep 0.05; k=$((k+1)); done',
|
|
130
|
+
' for i in $(running); do g=$(cat "$c/$i/pgid" 2>/dev/null); [ -n "$g" ] && kill -KILL -"$g" 2>/dev/null; done',
|
|
131
|
+
' k=0; while [ $k -lt 40 ] && [ -n "$(running)" ]; do sleep 0.05; k=$((k+1)); done',
|
|
132
|
+
' for i in $(running); do r=$(cat "$c/$i/runner" 2>/dev/null); [ -n "$r" ] && kill -KILL -"$r" 2>/dev/null; done',
|
|
133
|
+
' echo "ended $n";;',
|
|
134
|
+
'esac',
|
|
135
|
+
].join("\n");
|
|
136
|
+
/** One operation in the box, on the run's environment: its stdout. A call the box cannot run, or that fails, throws. */
|
|
137
|
+
async function inBox(given, op, args, context, timeout = 30) {
|
|
138
|
+
if (!given)
|
|
139
|
+
throw new Error("this call has no box to run in");
|
|
140
|
+
const env = given;
|
|
141
|
+
let out = "";
|
|
142
|
+
let said = "";
|
|
143
|
+
const work = typeof env.cwd === "string" && env.cwd ? env.cwd : BOX_WORK;
|
|
144
|
+
const ran = await env.exec(["sh", "-c", SCRIPT, "agentrun-background", work, op, ...args], {
|
|
145
|
+
timeout,
|
|
146
|
+
onOutput: (text, _context, info) => {
|
|
147
|
+
if (info.stream === "stdout")
|
|
148
|
+
out += text;
|
|
149
|
+
else if (said.length < 2_000)
|
|
150
|
+
said += text;
|
|
151
|
+
},
|
|
152
|
+
}, context);
|
|
153
|
+
if (!ran.ok)
|
|
154
|
+
throw new Error(`the run's box did not answer: ${ran.error.message}`);
|
|
155
|
+
if (ran.value.exitCode !== 0)
|
|
156
|
+
throw new Error(`the run's box answered with exit code ${ran.value.exitCode}${said.trim() ? `: ${said.trim().slice(0, 500)}` : ""}`);
|
|
157
|
+
return out;
|
|
158
|
+
}
|
|
159
|
+
/** A process's id as a call gives it: digits only, so no call names a path. */
|
|
160
|
+
function idOf(value) {
|
|
161
|
+
const id = typeof value === "number" && Number.isSafeInteger(value) ? String(value) : typeof value === "string" ? value.trim() : "";
|
|
162
|
+
if (!/^[1-9][0-9]{0,8}$/.test(id))
|
|
163
|
+
throw new Error(`${JSON.stringify(value)} is no background process id: an id is the number bash_background answered, such as "1" (bash_list shows them)`);
|
|
164
|
+
return id;
|
|
165
|
+
}
|
|
166
|
+
/** A state as the script says it. */
|
|
167
|
+
function stateOf(said) {
|
|
168
|
+
if (said === "running")
|
|
169
|
+
return { state: "running" };
|
|
170
|
+
const exited = /^exited (\d+)$/.exec(said);
|
|
171
|
+
if (!exited)
|
|
172
|
+
return { state: "lost" };
|
|
173
|
+
const code = Number(exited[1]);
|
|
174
|
+
const signal = code > 128 ? SIGNAL_NAMES[code - 128] : undefined;
|
|
175
|
+
return { state: "exited", exit_code: code, ...(signal ? { signal } : {}) };
|
|
176
|
+
}
|
|
177
|
+
/** A state in words: `running`, `exited with code 0`, `ended by signal TERM (exit code 143)`, or lost. */
|
|
178
|
+
function stateWords(state, ended = false) {
|
|
179
|
+
const atEnd = ended ? ", at the run's end" : "";
|
|
180
|
+
if (state.state === "running")
|
|
181
|
+
return "is running";
|
|
182
|
+
if (state.state === "lost")
|
|
183
|
+
return "is gone: its box was replaced before it ended, so its exit code is not known";
|
|
184
|
+
return state.signal ? `ended by signal ${state.signal} (exit code ${state.exit_code})${atEnd}` : `exited with code ${state.exit_code}${atEnd}`;
|
|
185
|
+
}
|
|
186
|
+
const noSuch = (id) => new Error(`there is no background process ${id} in this run: bash_list shows them`);
|
|
187
|
+
/** The end of `bytes` that ends inside a UTF-8 character, left for the next page: how many bytes to keep (none, when the
|
|
188
|
+
* page holds only part of one character). */
|
|
189
|
+
function wholeChars(bytes) {
|
|
190
|
+
for (let back = 1; back <= Math.min(3, bytes.length); back++) {
|
|
191
|
+
const byte = bytes[bytes.length - back];
|
|
192
|
+
if ((byte & 0xc0) === 0x80)
|
|
193
|
+
continue;
|
|
194
|
+
const length = byte >= 0xf0 ? 4 : byte >= 0xe0 ? 3 : byte >= 0xc0 ? 2 : 1;
|
|
195
|
+
return length > back ? bytes.length - back : bytes.length;
|
|
196
|
+
}
|
|
197
|
+
return bytes.length;
|
|
198
|
+
}
|
|
199
|
+
const decode = (base64) => new Uint8Array(Buffer.from(base64.replace(/\s+/g, ""), "base64"));
|
|
200
|
+
const intArg = (value, name, min, max, fallback) => {
|
|
201
|
+
if (value === undefined || value === null)
|
|
202
|
+
return fallback;
|
|
203
|
+
if (typeof value !== "number" || !Number.isInteger(value) || value < min || value > max)
|
|
204
|
+
throw new Error(`${name} is a whole number from ${min.toLocaleString("en-US")} to ${max.toLocaleString("en-US")}`);
|
|
205
|
+
return value;
|
|
206
|
+
};
|
|
207
|
+
const ID_SCHEMA = { type: "string", pattern: "^[1-9][0-9]*$", description: "The process's id, as bash_background answered it (\"1\")." };
|
|
208
|
+
const schema = (properties, required) => ({ type: "object", additionalProperties: false, properties, required });
|
|
209
|
+
const bashBackground = defineTool({
|
|
210
|
+
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({ command: { type: "string", minLength: 1, description: "The command, as you would give bash." } }, ["command"]),
|
|
213
|
+
execute: async (args, api, context) => {
|
|
214
|
+
const command = String(args.command ?? "");
|
|
215
|
+
if (!command.trim())
|
|
216
|
+
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();
|
|
218
|
+
if (said === "linkdir")
|
|
219
|
+
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
|
+
const full = /^full (\d+)/.exec(said);
|
|
221
|
+
if (full)
|
|
222
|
+
throw new Error(`${full[1]} background processes already run in this run's box, the most at once (${BACKGROUND_MAX}): end one with bash_signal (bash_list shows them), or wait for one to exit, then start this again.`);
|
|
223
|
+
const started = /^started (\d+) ?(\d*)/.exec(said);
|
|
224
|
+
if (!started)
|
|
225
|
+
throw new Error(`the run's box did not start the process: ${said.slice(0, 300) || "it said nothing"}`);
|
|
226
|
+
const [, id, group] = started;
|
|
227
|
+
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) } : {}) } });
|
|
229
|
+
},
|
|
230
|
+
});
|
|
231
|
+
const bashOutput = defineTool({
|
|
232
|
+
name: "bash_output",
|
|
233
|
+
description: `Read a background process's output (stdout and stderr together) from a byte offset: at most max_bytes (default and most ${BACKGROUND_PAGE_BYTES / 1024} KiB), with whether it still runs, its exit code once it has ended, the log's size, and the offset to read from next. Start at 0; pass the next offset to read on, also to see what a running process wrote since.`,
|
|
234
|
+
parameters: schema({
|
|
235
|
+
id: ID_SCHEMA,
|
|
236
|
+
offset: { type: "integer", minimum: 0, description: "The byte to read from; 0, the start, when absent." },
|
|
237
|
+
max_bytes: { type: "integer", minimum: 1, maximum: BACKGROUND_PAGE_BYTES, description: `The most bytes to read; ${BACKGROUND_PAGE_BYTES.toLocaleString("en-US")} when absent.` },
|
|
238
|
+
}, ["id"]),
|
|
239
|
+
replay: "safe",
|
|
240
|
+
execute: async (args, api, context) => {
|
|
241
|
+
const given = args;
|
|
242
|
+
const id = idOf(given.id);
|
|
243
|
+
const offset = intArg(given.offset, "offset", 0, BACKGROUND_LOG_BYTES, 0);
|
|
244
|
+
const max = intArg(given.max_bytes, "max_bytes", 1, BACKGROUND_PAGE_BYTES, BACKGROUND_PAGE_BYTES);
|
|
245
|
+
const said = await inBox(api.env, "output", [id, String(offset), String(max)], context);
|
|
246
|
+
if (said.trim() === "none")
|
|
247
|
+
throw noSuch(id);
|
|
248
|
+
if (said.split("\n").includes("notlog"))
|
|
249
|
+
throw new Error(`background process ${id}'s log is not read: ${BACKGROUND_DIR}/${id}/output.log (or a directory on its way) is a link or not a regular file, so it could name another file (or this box has no /proc to check it by)`);
|
|
250
|
+
const at = said.indexOf("\n--\n");
|
|
251
|
+
const head = (at < 0 ? said : said.slice(0, at)).split("\n");
|
|
252
|
+
const fact = (key) => head.find((line) => line === key || line.startsWith(`${key} `))?.slice(key.length + 1);
|
|
253
|
+
const state = stateOf(fact("state") ?? "");
|
|
254
|
+
const size = Number(fact("size") ?? 0);
|
|
255
|
+
const capped = head.includes("capped");
|
|
256
|
+
const ended = head.includes("ended");
|
|
257
|
+
const read = at < 0 ? new Uint8Array() : decode(said.slice(at + 4));
|
|
258
|
+
// A character the page would split is left for the next one, unless the process has ended and nothing follows.
|
|
259
|
+
const keep = state.state === "running" || offset + read.length < size ? wholeChars(read) : read.length;
|
|
260
|
+
// A page too small for the one character it starts with reads nothing, and says what to ask instead.
|
|
261
|
+
if (keep === 0 && read.length > 0)
|
|
262
|
+
throw new Error(`the character at byte ${offset} of background process ${id}'s output is longer than max_bytes ${max}: ask again with max_bytes 4 or more`);
|
|
263
|
+
const bytes = read.subarray(0, keep);
|
|
264
|
+
const next = offset + bytes.length;
|
|
265
|
+
const more = next < size;
|
|
266
|
+
const where = offset >= size && bytes.length === 0
|
|
267
|
+
? `No output past byte ${offset.toLocaleString("en-US")} (the log has ${size.toLocaleString("en-US")} bytes)${state.state === "running" ? "; call again later with the same offset for what comes next" : ""}.`
|
|
268
|
+
: `Output bytes ${offset.toLocaleString("en-US")} to ${next.toLocaleString("en-US")} of ${size.toLocaleString("en-US")}${more ? `; call again with offset ${next} for the next part` : state.state === "running" ? `, all of it so far; call again with offset ${next} for what comes next` : ", the end"}.`;
|
|
269
|
+
const cap = capped ? ` Its log reached the ${MIB} MiB cap, so later output was dropped.` : "";
|
|
270
|
+
const text = new TextDecoder().decode(bytes);
|
|
271
|
+
return forModel(`Background process ${id} ${stateWords(state, ended)}. ${where}${cap}${text ? `\n\n${text}` : ""}`, { details: { id, ...state, offset, next_offset: next, size, ...(capped ? { capped } : {}), ...(ended ? { ended_at_run_end: true } : {}) } });
|
|
272
|
+
},
|
|
273
|
+
});
|
|
274
|
+
const bashInput = defineTool({
|
|
275
|
+
name: "bash_input",
|
|
276
|
+
description: `Write text to a running background process's stdin, as given (end a line with \\n), at most ${BACKGROUND_INPUT_BYTES / 1024} KiB a call. close: true closes its stdin after the text, so the process reads the end of its input. To interrupt it, send bash_signal INT; a control character written here is only a byte.`,
|
|
277
|
+
parameters: schema({
|
|
278
|
+
id: ID_SCHEMA,
|
|
279
|
+
text: { type: "string", description: "The bytes to write, as UTF-8; empty with close: true closes stdin alone." },
|
|
280
|
+
close: { type: "boolean", description: "Close the process's stdin after the text." },
|
|
281
|
+
}, ["id", "text"]),
|
|
282
|
+
execute: async (args, api, context) => {
|
|
283
|
+
const given = args;
|
|
284
|
+
const id = idOf(given.id);
|
|
285
|
+
const text = typeof given.text === "string" ? given.text : "";
|
|
286
|
+
if (Buffer.byteLength(text) > BACKGROUND_INPUT_BYTES)
|
|
287
|
+
throw new Error(`text is at most ${BACKGROUND_INPUT_BYTES / 1024} KiB a call; send it in parts`);
|
|
288
|
+
if (text.includes("\0"))
|
|
289
|
+
throw new Error("text holds a NUL byte, which no shell argument carries");
|
|
290
|
+
const close = given.close === true;
|
|
291
|
+
if (!text && !close)
|
|
292
|
+
throw new Error("text is what to write: give some, or close: true to close stdin");
|
|
293
|
+
const said = (await inBox(api.env, "input", [id, text, close ? "1" : "0"], context, 15).catch((error) => {
|
|
294
|
+
throw new Error(`process ${id} did not take its input: ${error.message} (a process that does not read its stdin fills its pipe at 64 KiB)`);
|
|
295
|
+
})).trim();
|
|
296
|
+
if (said === "none")
|
|
297
|
+
throw noSuch(id);
|
|
298
|
+
if (said === "closed")
|
|
299
|
+
throw new Error(`background process ${id}'s stdin is closed: an earlier bash_input closed it`);
|
|
300
|
+
if (said.startsWith("state "))
|
|
301
|
+
throw new Error(`background process ${id} ${stateWords(stateOf(said.slice(6)))}, so it takes no input`);
|
|
302
|
+
const bytes = Buffer.byteLength(text);
|
|
303
|
+
return forModel(`Wrote ${bytes.toLocaleString("en-US")} bytes to background process ${id}'s stdin${close ? ", then closed it" : ""}. Read what it answers with bash_output.`, { details: { id, bytes, ...(close ? { closed: true } : {}) } });
|
|
304
|
+
},
|
|
305
|
+
});
|
|
306
|
+
const bashSignal = defineTool({
|
|
307
|
+
name: "bash_signal",
|
|
308
|
+
description: "Send a signal to a background process's process group: TERM asks it to stop, INT is Ctrl-C, KILL ends it at once. Answers its state after up to 2 s: a process that handles the signal may run on.",
|
|
309
|
+
parameters: schema({ id: ID_SCHEMA, signal: { type: "string", enum: [...BACKGROUND_SIGNALS], description: "TERM, INT or KILL." } }, ["id", "signal"]),
|
|
310
|
+
execute: async (args, api, context) => {
|
|
311
|
+
const given = args;
|
|
312
|
+
const id = idOf(given.id);
|
|
313
|
+
const signal = typeof given.signal === "string" ? given.signal.trim().toUpperCase().replace(/^SIG/, "") : "";
|
|
314
|
+
if (!BACKGROUND_SIGNALS.includes(signal))
|
|
315
|
+
throw new Error(`signal is one of ${BACKGROUND_SIGNALS.join(", ")}`);
|
|
316
|
+
const said = (await inBox(api.env, "signal", [id, signal], context, 15)).trim();
|
|
317
|
+
if (said === "none")
|
|
318
|
+
throw noSuch(id);
|
|
319
|
+
if (said.startsWith("was "))
|
|
320
|
+
throw new Error(`background process ${id} ${stateWords(stateOf(said.slice(4)))}, so no signal was sent`);
|
|
321
|
+
const state = stateOf(said.replace(/^state /, ""));
|
|
322
|
+
return forModel(`Sent ${signal} to background process ${id}: it ${stateWords(state)}.${state.state === "running" ? " Send KILL to end it at once." : ""}`, { details: { id, signal, ...state } });
|
|
323
|
+
},
|
|
324
|
+
});
|
|
325
|
+
const bashList = defineTool({
|
|
326
|
+
name: "bash_list",
|
|
327
|
+
description: `List this run's background processes: each one's id, whether it runs or how it ended, its output's size and its command's start, every running one first, then the newest that ended (${BACKGROUND_LIST_MAX} at most).`,
|
|
328
|
+
parameters: schema({}, []),
|
|
329
|
+
replay: "safe",
|
|
330
|
+
execute: async (_args, api, context) => {
|
|
331
|
+
const said = await inBox(api.env, "list", [String(BACKGROUND_LIST_MAX)], context);
|
|
332
|
+
const [first = "", ...rows] = said.split("\n");
|
|
333
|
+
const total = Number(/^total (\d+)$/.exec(first)?.[1] ?? 0);
|
|
334
|
+
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)) };
|
|
337
|
+
}).sort((a, b) => Number(b.id) - Number(a.id));
|
|
338
|
+
const shown = [...all.filter((p) => p.state === "running"), ...all.filter((p) => p.state !== "running")].slice(0, BACKGROUND_LIST_MAX);
|
|
339
|
+
if (!shown.length)
|
|
340
|
+
return forModel("No background processes in this run's box: bash_background starts one.", { details: { processes: [] } });
|
|
341
|
+
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))}`);
|
|
343
|
+
const left = total - shown.length;
|
|
344
|
+
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
|
+
},
|
|
346
|
+
});
|
|
347
|
+
/** The background tools, each run on the environment its call gives (the box's, coding-tools.ts). */
|
|
348
|
+
export const backgroundTools = () => [bashBackground, bashOutput, bashInput, bashSignal, bashList];
|
|
349
|
+
/** The run's end: every background process still running in run `runId`'s box (found by `label`) is sent TERM, then
|
|
350
|
+
* KILL after 3 s, and marked as ended at the run's end, so its runner writes its exit code and its log is whole before
|
|
351
|
+
* the box is pulled and destroyed. How many it ended; a run with no box ends none. */
|
|
352
|
+
export async function endBackground(boxes, label, context) {
|
|
353
|
+
const { finish } = await import("@parcha/pi-transport");
|
|
354
|
+
const listed = await boxes.list(label, context);
|
|
355
|
+
if (!listed.ok)
|
|
356
|
+
throw listed.error;
|
|
357
|
+
let ended = 0;
|
|
358
|
+
for (const ref of listed.value) {
|
|
359
|
+
const attached = await boxes.attach(ref.id, context);
|
|
360
|
+
if (!attached.ok)
|
|
361
|
+
throw attached.error;
|
|
362
|
+
if (!attached.value)
|
|
363
|
+
continue;
|
|
364
|
+
const started = await attached.value.exec(["sh", "-c", SCRIPT, "agentrun-background", BOX_WORK, "end"], { timeoutMs: 20_000 }, context);
|
|
365
|
+
if (!started.ok)
|
|
366
|
+
throw started.error;
|
|
367
|
+
const done = await finish(started.value);
|
|
368
|
+
if (!done.ok)
|
|
369
|
+
throw done.error;
|
|
370
|
+
const said = new TextDecoder().decode(done.value.stdout).trim();
|
|
371
|
+
if (done.value.exitCode !== 0)
|
|
372
|
+
throw new Error(`ending the box's background processes failed (exit code ${done.value.exitCode}): ${done.value.stderr.trim().slice(0, 300)}`);
|
|
373
|
+
ended += Number(/^ended (\d+)/.exec(said)?.[1] ?? 0);
|
|
374
|
+
}
|
|
375
|
+
return ended;
|
|
376
|
+
}
|
|
@@ -0,0 +1,98 @@
|
|
|
1
|
+
/** Where the proxy's directory is in the box, read-only. */
|
|
2
|
+
export declare const EGRESS_MOUNT = "/run/agentrun/egress";
|
|
3
|
+
/** The proxy's socket, in its directory. */
|
|
4
|
+
export declare const EGRESS_SOCKET = "egress.sock";
|
|
5
|
+
/** The box's loopback port socat relays to the socket. */
|
|
6
|
+
export declare const EGRESS_PORT = 3128;
|
|
7
|
+
/** What a box with a list adds to its fixed environment (run-box.ts BOX_ENV): constants, no user, no password. */
|
|
8
|
+
export declare const PROXY_ENV: Readonly<Record<string, string>>;
|
|
9
|
+
/** The box's relay to the proxy's socket `socket` (in EGRESS_MOUNT), started once each time the run's box opens (made
|
|
10
|
+
* or found): a second one finds the port taken and exits. Each connection opens the socket by its path, so a proxy
|
|
11
|
+
* started again is reached with no new relay. */
|
|
12
|
+
export declare const forwarder: (socket?: string) => string[];
|
|
13
|
+
/** The relay's start, in the box, answered once it listens: exit 127 when the image has no socat, 126 when it has no
|
|
14
|
+
* setsid, and 124 when the relay is not listening within 3 s (its own error, if it gave one, on stderr). A relay a
|
|
15
|
+
* process before this one started still listens, so a second start finds the port taken and answers at once. */
|
|
16
|
+
export declare const forwarderStart: (socket?: string) => string[];
|
|
17
|
+
/** The timeline row of a box whose relay could not start, by the start's exit (forwarderStart): the box keeps no network. */
|
|
18
|
+
export declare function relayFailed(code: number, said: string): string;
|
|
19
|
+
/** The row for an image with no socat (relayFailed 127). */
|
|
20
|
+
export declare const NO_SOCAT: string;
|
|
21
|
+
/** What a refusal of a host off the list says to do, after its words. */
|
|
22
|
+
export declare const ALLOW_HINT = "use web_fetch for web pages, or ask the operator to add it to AGENTRUN_BOX_ALLOW";
|
|
23
|
+
/** Each run's proxy: connections open at once, and in all. */
|
|
24
|
+
export declare const MAX_TUNNELS = 64;
|
|
25
|
+
export declare const MAX_CONNECTIONS = 20000;
|
|
26
|
+
/** Under `--faux` only: a module whose default export gives the proxy's `lookup` and `dial` (the tests' fake network), so
|
|
27
|
+
* a test's box reaches no host but its fake. */
|
|
28
|
+
export declare const FAUX_EGRESS_ENV = "AGENTRUN_SERVE_FAUX_EGRESS";
|
|
29
|
+
/** The proxy's directory for run `runId`: short, so its socket's path stays within a Unix socket's limit (107 bytes)
|
|
30
|
+
* wherever the data root is, and the same each time the run's process starts, since the box keeps its bind. */
|
|
31
|
+
export declare function egressDir(runId: string, uid?: number): string;
|
|
32
|
+
/** Removes a run's proxy directory once its box is gone. */
|
|
33
|
+
export declare const removeEgressDir: (runId: string) => Promise<void>;
|
|
34
|
+
/** Where a run's box finds its proxy: what is mounted read-only at EGRESS_MOUNT, and the socket's name there. The one
|
|
35
|
+
* seam between the box's side, which is the same wherever the proxy runs (the mount, the relay, the proxy variables),
|
|
36
|
+
* and where the proxy runs. Today that is hostEgress: the run's own process, its socket's directory on this machine
|
|
37
|
+
* bound into the box. */
|
|
38
|
+
export type BoxEgress = (runId: string) => Promise<{
|
|
39
|
+
mount: {
|
|
40
|
+
source: string;
|
|
41
|
+
target: typeof EGRESS_MOUNT;
|
|
42
|
+
readOnly: true;
|
|
43
|
+
};
|
|
44
|
+
socket: string;
|
|
45
|
+
fresh: boolean;
|
|
46
|
+
}>;
|
|
47
|
+
/** The proxy in the run's process (hostEgress): for a Docker daemon on this Linux machine, which can reach a socket in a
|
|
48
|
+
* directory of this machine bound into a container. */
|
|
49
|
+
export declare const hostEgress: (allow: readonly string[], env: NodeJS.ProcessEnv, log: (line: string) => void) => BoxEgress;
|
|
50
|
+
/** How long `docker info` may take to say where this Docker runs. */
|
|
51
|
+
export declare const INFO_TIMEOUT_MS = 5000;
|
|
52
|
+
/** What `docker info` is asked: the daemon's OS type, its kernel, where it keeps its data, and the name it gives its OS. */
|
|
53
|
+
export declare const INFO_FORMAT = "{{.OSType}}|{{.KernelVersion}}|{{.DockerRootDir}}|{{.OperatingSystem}}";
|
|
54
|
+
/** `docker info`'s answer to INFO_FORMAT, its stdout, or a throw that says why there is none. */
|
|
55
|
+
export type DockerInfo = (env: NodeJS.ProcessEnv, timeoutMs: number) => Promise<string>;
|
|
56
|
+
/** `docker info` in the server's environment through the cut a run's takes, nothing on its stdin, within `timeoutMs`. */
|
|
57
|
+
export declare const dockerInfo: DockerInfo;
|
|
58
|
+
/**
|
|
59
|
+
* Why a Docker box on this server cannot reach its proxy, or undefined when it can. The proxy's socket is a file of this
|
|
60
|
+
* machine bound into the box, which only a Docker daemon on this same Linux machine can connect to: a daemon in a VM
|
|
61
|
+
* (Docker Desktop, OrbStack and their kin) sees the socket and is refused. So the network goes on only with positive
|
|
62
|
+
* evidence that the daemon is this machine's: this machine is Linux; the docker CLI's endpoint is a local unix socket
|
|
63
|
+
* (no DOCKER_HOST or a unix:// one, and the default context or a unix one: pi-transport's dockerDaemon); and `docker
|
|
64
|
+
* info` answers within its bound with OS type linux and this machine's own kernel (os.release()): the same kernel, so no
|
|
65
|
+
* VM in between; and the directory it keeps its data in (DockerRootDir) is one this server sees. A server in a container
|
|
66
|
+
* given the host's docker socket shares the host's kernel, but not its files: it would bind a directory the daemon does
|
|
67
|
+
* not have, so every box would fail to start. Anything else is no network, and the reason says what was seen, naming the
|
|
68
|
+
* runtime docker info gives. A kernel is the evidence, not a runtime's name: Docker Desktop on WSL2 runs the kernel this
|
|
69
|
+
* server does, but keeps its data outside the distro, so it passes only where the distro has that directory too.
|
|
70
|
+
*/
|
|
71
|
+
export declare function egressBlockedBy(env: NodeJS.ProcessEnv, options?: {
|
|
72
|
+
info?: DockerInfo;
|
|
73
|
+
platform?: string;
|
|
74
|
+
kernel?: string;
|
|
75
|
+
timeoutMs?: number;
|
|
76
|
+
sees?: (dir: string) => boolean;
|
|
77
|
+
}): Promise<string | undefined>;
|
|
78
|
+
/** How old a run's proxy directory with no live socket must be before the start's sweep takes it: one just made, whose
|
|
79
|
+
* proxy is still starting, is not yet listening. */
|
|
80
|
+
export declare const STALE_EGRESS_MS = 60000;
|
|
81
|
+
/**
|
|
82
|
+
* The start's sweep of this user's proxy directories (egressDir's parent): each whose socket refuses a connect (or has
|
|
83
|
+
* none) and that is older than STALE_EGRESS_MS is a run's process that is gone, and is removed, by its exact path. One
|
|
84
|
+
* whose socket answers is a live run's, this server's or another's under this uid, and stays. Returns how many went.
|
|
85
|
+
*/
|
|
86
|
+
export declare function sweepEgressDirs(uid?: number, now?: number): Promise<number>;
|
|
87
|
+
/** What a server's Docker boxes reach, resolved once at its start (run-box.ts boxNetworkOf): the list, or none and why. */
|
|
88
|
+
export type DockerNetwork = {
|
|
89
|
+
allow: readonly string[];
|
|
90
|
+
} | {
|
|
91
|
+
allow: "none";
|
|
92
|
+
reason: string;
|
|
93
|
+
};
|
|
94
|
+
/** What the server hands a run's process: the list its box reaches, resolved (boxNetworkOf), or `none`. The run's
|
|
95
|
+
* process reads this, never AGENTRUN_BOX_ALLOW: unset, it is none. */
|
|
96
|
+
export declare const RUN_BOX_ALLOW_ENV = "AGENTRUN_RUN_BOX_ALLOW";
|
|
97
|
+
/** The run's process's reading of RUN_BOX_ALLOW_ENV: the list, or `none` when it says so, is unset or is not a list. */
|
|
98
|
+
export declare function runBoxAllowOf(env: NodeJS.ProcessEnv): readonly string[] | "none";
|