@parcha/agentrun 0.4.10 → 0.4.12

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 CHANGED
@@ -13,10 +13,59 @@ It starts [AgentRun Server](https://github.com/Parcha-ai/agentrun/tree/main/pack
13
13
  it in your browser (`--no-open`, a CI run or a machine with no display skips that). The server,
14
14
  `@parcha/agentrun-serve`, is this package's one dependency, at its exact version, so `npx` installs the two together and
15
15
  each version of this command runs the server it was released with. Node.js 24.22.3 or newer, on macOS or Linux; an older
16
- Node gets one line naming that floor.
16
+ Node gets one line naming that floor and the installer's command.
17
17
 
18
- A start that fails says why on stderr: the step that failed, how the server ended (its exit code or signal, or still
19
- running after 60 seconds without printing its address, and stopped), its last lines on stderr and stdout, and
18
+ No Node.js on the machine, or an older one? The installer brings its own and keeps AgentRun running after the terminal
19
+ closes:
20
+
21
+ ```sh
22
+ curl -fsSL https://agentrun.ai/install | sh
23
+ ```
24
+
25
+ It downloads a Node.js from nodejs.org: the newest release on the major line of the version this AgentRun needs, so
26
+ later security releases come with the next install. It checks the download against that release's `SHASUMS256.txt`
27
+ and installs it and AgentRun under `~/.agentrun` (`node/`, `lib/`, `bin/agentrun`). Any other Node, nvm or Homebrew is
28
+ left alone, and nothing needs sudo. When no line in your shell's startup file puts `~/.agentrun/bin` on `PATH`, it adds
29
+ one and says so. Then it starts AgentRun as a service of your user, a LaunchAgent on macOS or a systemd user unit on
30
+ Linux, which starts again at login and after a crash and logs to `~/.agentrun/logs/agentrun.log`. Where there is no
31
+ service manager (a container, say), it starts AgentRun in the background, logging to the same file. It runs until the
32
+ machine or container stops, and `agentrun stop` stops it (a SIGTERM, then a SIGKILL to the launcher and its server
33
+ after 45 seconds). Either way the installer returns once AgentRun answers, and prints its address. `sh -s -- --no-start`
34
+ installs only, `--version V` installs another version, `--node-version V` installs that Node.js (at least the floor),
35
+ and `--service` fails where there is no service manager. Running it again upgrades, or installs an older version, and
36
+ first stops the AgentRun it started in the background itself, whatever version that is.
37
+
38
+ `agentrun service status` prints the console's address and its health. `start`, `stop`, `restart`, `logs [--follow]`,
39
+ `upgrade [--version V]` (the installer again, then a restart) and `uninstall` (the data stays) do what they say, and
40
+ `agentrun service install [--port N] [--data DIR]` installs the service after a `--no-start`. On Linux, a user's
41
+ services stop at logout unless lingering is on: `install` and `status` print the one `sudo loginctl enable-linger` line
42
+ when it is off. To remove AgentRun, run `agentrun service uninstall`, then delete `~/.agentrun/bin`, `~/.agentrun/lib`,
43
+ `~/.agentrun/node` and the `PATH` line; the rest of `~/.agentrun` is your data.
44
+
45
+ On a machine you keep on (a Mac mini, a home server, a VPS), `--tailscale` also puts AgentRun on your own
46
+ [Tailscale](https://tailscale.com) network, at `https://<machine>.<your tailnet's DNS name>/`, which only your devices
47
+ on that network open:
48
+
49
+ ```sh
50
+ curl -fsSL https://agentrun.ai/install | sh -s -- --tailscale
51
+ ```
52
+
53
+ It runs `tailscale serve --bg --https=443` to AgentRun's port, 7340, where AgentRun keeps listening on loopback only,
54
+ and prints the address from `tailscale status --json`. The server is told that address (`AGENTRUN_PUBLIC_URL`), so its
55
+ first visit asks for the setup code in `~/.agentrun/logs/agentrun.log`. It never uses Funnel, which would put the
56
+ address on the public internet: where Funnel is already on for that address, or the address serves something else, it
57
+ changes nothing and says so. It never installs Tailscale, logs in or runs sudo. With Tailscale missing or logged out,
58
+ it starts AgentRun on this machine and prints the steps and the command to run again. On Linux, where only root or
59
+ Tailscale's operator may change what it serves, it prints the one `sudo tailscale set --operator=$USER` line.
60
+ `agentrun service install --tailscale` does the same for an installed service, `--no-tailscale` takes it off, and
61
+ `agentrun service uninstall` removes the serve entry it added and no other. Tailscale's Personal plan is free: up to 6
62
+ users, unlimited user devices ([pricing](https://tailscale.com/pricing)). The https address needs MagicDNS and HTTPS
63
+ certificates turned on in your tailnet's [DNS settings](https://login.tailscale.com/admin/dns).
64
+
65
+ The server binds its port first and says so; this command then names the address on stderr at once, and prints its line
66
+ on stdout once the server is ready, which on a cold, slow disk can take minutes. A start that fails says why on stderr:
67
+ the step that failed, how the server ended (its exit code or signal, or still running after 60 seconds without printing
68
+ any address, and stopped; `AGENTRUN_START_TIMEOUT_MS` sets that wait, in milliseconds), its last lines on stderr and stdout, and
20
69
  `~/.agentrun/start.log` (in the folder `--data` names), which holds everything the server wrote in that start. Each start
21
70
  overwrites that log; it is private to you, as the server's first lines name its API token. A log the disk could not hold
22
71
  all of is named with the error that cut it short.
package/bin/agentrun.mjs CHANGED
@@ -2,17 +2,23 @@
2
2
  // npx @parcha/agentrun: AgentRun Server on this machine with one command. Node first, in syntax any Node reads, so an old one
3
3
  // gets one plain line rather than a parse error; the repository's own rule (scripts/check-node.mjs). The floor is read from
4
4
  // this package's own engines field (">=MAJOR.MINOR.PATCH"), so the line never names another version than npm's. The patch
5
- // counts: the floor is a security release, and the releases before it on the same minor lack its fixes.
5
+ // counts: the floor is a security release, and the releases before it on the same minor lack its fixes. The line ends
6
+ // with the installer's command, which brings a Node of its own (install.sh). `--version` alone prints this package's version.
6
7
  import { readFileSync } from "node:fs";
7
8
 
8
- const engines = JSON.parse(readFileSync(new URL("../package.json", import.meta.url), "utf8")).engines.node;
9
+ const own = JSON.parse(readFileSync(new URL("../package.json", import.meta.url), "utf8"));
10
+ const engines = own.engines.node;
9
11
  const floor = /^>=(\d+)\.(\d+)\.(\d+)$/.exec(engines);
10
12
  if (!floor) throw new Error(`this package's engines.node is ${JSON.stringify(engines)}, not ">=MAJOR.MINOR.PATCH"`);
11
13
  const need = floor.slice(1).map(Number);
12
14
  const have = process.versions.node.split(".").map(Number);
13
15
  const at = need.findIndex((part, i) => have[i] !== part);
14
16
  if (at !== -1 && !(have[at] > need[at])) {
15
- console.error(`AgentRun needs Node.js ${need.join(".")} or newer; you have v${process.versions.node}.`);
17
+ console.error(`AgentRun needs Node.js ${need.join(".")} or newer; you have v${process.versions.node}. To install AgentRun with a Node.js of its own, run: curl -fsSL https://agentrun.ai/install | sh`);
16
18
  process.exit(1);
17
19
  }
20
+ if (process.argv[2] === "--version" && process.argv.length === 3) {
21
+ console.log(own.version);
22
+ process.exit(0);
23
+ }
18
24
  import("../lib/start.mjs").then(({ main }) => main(process.argv.slice(2))).then((code) => { process.exitCode = code; });
package/install.sh ADDED
@@ -0,0 +1,482 @@
1
+ #!/bin/sh
2
+ # AgentRun's installer, for macOS and Linux (x64, arm64):
3
+ #
4
+ # curl -fsSL https://agentrun.ai/install | sh
5
+ # curl -fsSL https://agentrun.ai/install | sh -s -- --version 0.4.11 --no-start
6
+ #
7
+ # It brings its own Node.js: the newest release on the major line of the launcher's engines floor (`>=X.Y.Z`, the
8
+ # security release it needs) that is at least that floor, as nodejs.org/dist/index.json lists them, so later security
9
+ # releases come with the next install. It is downloaded from nodejs.org and checked against that release's
10
+ # SHASUMS256.txt, into ~/.agentrun/node/vX.Y.Z. It installs @parcha/agentrun, and the server it depends on, with that Node
11
+ # into ~/.agentrun/lib/<version>, and writes ~/.agentrun/bin/agentrun, which always runs that Node. The node, nvm,
12
+ # Homebrew or global npm on this machine are never used or changed. ~/.agentrun is also the server's data directory: the
13
+ # installer keeps to bin, lib, node, logs and agentrun.pid, names the server never uses. No sudo. Running it again
14
+ # installs the version asked for (the latest by default) and removes the others, except one a running server still uses.
15
+ #
16
+ # Then it starts AgentRun as a service of this user (`agentrun service install`: launchd on macOS, systemd --user on
17
+ # Linux), so it keeps running after the terminal closes and starts again at login. Where this user has no service
18
+ # manager (a container, say), it starts AgentRun in the background until the machine stops (`agentrun stop` stops it).
19
+ # Either way it returns once AgentRun answers, and prints its address. With --tailscale it is also served over HTTPS on
20
+ # the person's own tailnet (lib/tailscale.mjs), on port 7340. Options: --help.
21
+ #
22
+ # The whole script is one function, called on its last line, so a download cut short runs nothing.
23
+
24
+ set -eu
25
+
26
+ main() {
27
+ registry=https://registry.npmjs.org
28
+ package=@parcha/agentrun
29
+ version=latest
30
+ tarball=
31
+ node_version=
32
+ start=default
33
+ ts_flag=
34
+
35
+ while [ $# -gt 0 ]; do
36
+ case $1 in
37
+ --version) [ $# -ge 2 ] || fail "--version takes a version or npm tag"; version=$2; shift ;;
38
+ --version=*) version=${1#--version=} ;;
39
+ --package) [ $# -ge 2 ] || fail "--package takes a tarball"; tarball=$2; shift ;;
40
+ --package=*) tarball=${1#--package=} ;;
41
+ --node-version) [ $# -ge 2 ] || fail "--node-version takes a Node.js version"; node_version=${2#v}; shift ;;
42
+ --node-version=*) node_version=${1#--node-version=}; node_version=${node_version#v} ;;
43
+ --no-start) start=none ;;
44
+ --service) start=service ;;
45
+ --tailscale | --no-tailscale) ts_flag=$1 ;;
46
+ -h | --help) usage; return 0 ;;
47
+ *) fail "unknown option $1 (see --help)" ;;
48
+ esac
49
+ shift
50
+ done
51
+ if [ -n "$node_version" ] && ! printf '%s\n' "$node_version" | grep -qx '[0-9][0-9]*\.[0-9][0-9]*\.[0-9][0-9]*'; then
52
+ fail "--node-version $node_version is not a Node.js version (X.Y.Z)"
53
+ fi
54
+
55
+ [ -n "${HOME:-}" ] || fail "HOME is not set"
56
+ case $HOME in *"'"*) fail "HOME ($HOME) has a single quote in it, and the command this writes quotes paths with one" ;; esac
57
+ [ "$(id -u)" != 0 ] || say "Note: installing as root, into $HOME/.agentrun. AgentRun runs as the user who installs it."
58
+
59
+ detect_platform
60
+ need tar
61
+ need gzip
62
+ root=$HOME/.agentrun
63
+
64
+ # The launcher's version and its Node floor: from the tarball given, or from the registry's document for that version.
65
+ if [ -n "$tarball" ]; then
66
+ [ -f "$tarball" ] || fail "--package $tarball: no such file"
67
+ case $tarball in /*) ;; *) tarball=$(pwd)/$tarball ;; esac
68
+ manifest=$(tar -xzOf "$tarball" package/package.json | tr -d '\n\r\t ') || fail "--package $tarball is not an npm package tarball"
69
+ spec=$tarball
70
+ else
71
+ case $version in *[!A-Za-z0-9.+-]*) fail "--version $version is not a version or npm tag" ;; esac
72
+ manifest=$(download "$registry/@parcha%2fagentrun/$version") || fail "$package has no version or tag $version on $registry"
73
+ fi
74
+ # The manifest, one JSON member a line: enough to read its own name, version and engines.node without a JSON parser.
75
+ members=$(printf '%s' "$manifest" | tr ',{}' '\n\n\n')
76
+ printf '%s\n' "$members" | grep -qx "\"name\":\"$package\"" || fail "the package to install is not $package"
77
+ version=$(printf '%s\n' "$members" | sed -n 's/^"version":"\([^"]*\)"$/\1/p' | sort -u)
78
+ case $version in "" | *[!A-Za-z0-9.+-]*) fail "$package names no single version" ;; esac
79
+ floor=$(printf '%s\n' "$members" | awk 'engines && /^"node":/ { print; exit } { engines = ($0 == "\"engines\":") }' |
80
+ sed -n 's/^"node":">=\([0-9][0-9]*\.[0-9][0-9]*\.[0-9][0-9]*\)"$/\1/p')
81
+ [ -n "$floor" ] || fail "$package $version names no Node.js floor (engines.node \">=X.Y.Z\")"
82
+ [ -n "$tarball" ] || spec=$package@$version
83
+ if [ -n "$node_version" ] && ! at_least "$node_version" "$floor"; then
84
+ fail "--node-version $node_version is below the Node.js $package $version needs, $floor"
85
+ fi
86
+
87
+ mkdir -p "$root"
88
+ chmod 700 "$root"
89
+ # Every download and build lands in a folder beside its final place, so each one moves in whole, in one rename.
90
+ work=$(mktemp -d "$root/.install.XXXXXX")
91
+ trap 'rm -rf "$work"' EXIT
92
+ trap 'exit 130' INT
93
+ trap 'exit 143' TERM
94
+
95
+ if [ -n "$node_version" ]; then
96
+ say "Node.js $node_version, as --node-version asks"
97
+ else
98
+ resolve_node
99
+ say "Node.js $node_version, the newest ${floor%%.*}.x at or above $floor"
100
+ fi
101
+ node_dir=$root/node/v$node_version
102
+ if [ -x "$node_dir/bin/node" ] && [ "$("$node_dir/bin/node" --version 2>/dev/null)" = "v$node_version" ]; then
103
+ say "Node.js $node_version is already in $node_dir"
104
+ else
105
+ install_node
106
+ fi
107
+
108
+ lib_dir=$root/lib/$version
109
+ say "Installing $package $version with Node.js $node_version ..."
110
+ mkdir -p "$work/lib"
111
+ printf '{ "private": true }\n' > "$work/lib/package.json"
112
+ # npm from this Node, with this Node first on PATH for the packages' own scripts. The person's npm config (a registry
113
+ # mirror, a proxy) still applies, as it does to npx.
114
+ if ! PATH="$node_dir/bin:$PATH" "$node_dir/bin/npm" install --prefix "$work/lib" --no-audit --no-fund \
115
+ --update-notifier=false --loglevel=error "$spec" > "$work/npm.log" 2>&1; then
116
+ tail -n 20 "$work/npm.log" >&2
117
+ fail "npm could not install $spec (its last lines are above)"
118
+ fi
119
+ entry=$lib_dir/node_modules/@parcha/agentrun/bin/agentrun.mjs
120
+ [ -f "$work/lib/node_modules/@parcha/agentrun/bin/agentrun.mjs" ] || fail "npm installed $spec without its bin/agentrun.mjs"
121
+ printf '%s\n' "$node_dir" > "$work/lib/.node"
122
+ mkdir -p "$root/lib"
123
+ replace "$work/lib" "$lib_dir"
124
+
125
+ # The command: this version on this Node, whatever node is on PATH. Its Node goes first on PATH too, as the server's own
126
+ # launcher and the harness installs run `node` and `npm` by name.
127
+ shim=$root/bin/agentrun
128
+ mkdir -p "$root/bin" "$root/logs"
129
+ cat > "$work/agentrun" <<EOF
130
+ #!/bin/sh
131
+ # AgentRun $version on its own Node.js $node_version, written by the AgentRun installer (https://agentrun.ai/install).
132
+ PATH='$node_dir/bin':"\$PATH"
133
+ export PATH
134
+ exec '$node_dir/bin/node' '$entry' "\$@"
135
+ EOF
136
+ chmod 755 "$work/agentrun"
137
+ mv -f "$work/agentrun" "$shim"
138
+ "$shim" help > /dev/null 2>&1 || fail "$shim does not run: $("$shim" help 2>&1 | tail -n 3)"
139
+ say "Installed AgentRun $version (Node.js $node_version) in $root"
140
+ tailnet_js=$lib_dir/node_modules/@parcha/agentrun/lib/tailscale.mjs
141
+ [ -z "$ts_flag" ] || [ -f "$tailnet_js" ] || fail "AgentRun $version has no $ts_flag; install a newer version"
142
+
143
+ add_to_path
144
+
145
+ case $start in
146
+ none)
147
+ say ""
148
+ say "Start it: $(shown_shim) service install ${ts_flag:+$ts_flag } (keeps running, starts again at login)"
149
+ say " or: $(shown_shim) (in this terminal; Ctrl-C stops it)"
150
+ ;;
151
+ service)
152
+ stop_background
153
+ "$shim" service install ${ts_flag:+"$ts_flag"}
154
+ ;;
155
+ default)
156
+ # A service already installed restarts on this version, or installs again to go on or off the tailnet; status
157
+ # exits 3 when there is none.
158
+ code=0
159
+ "$shim" service status > /dev/null 2>&1 || code=$?
160
+ if { [ "$code" = 0 ] || [ "$code" = 1 ]; } && [ -z "$ts_flag" ]; then
161
+ "$shim" service restart
162
+ elif [ "$code" = 0 ] || [ "$code" = 1 ] || { [ "$code" = 3 ] && service_manager; }; then
163
+ stop_background
164
+ "$shim" service install ${ts_flag:+"$ts_flag"}
165
+ else
166
+ [ "$code" = 3 ] || why="AgentRun $version has no service command"
167
+ say "AgentRun can't run as a service here ($why), so it runs in the background."
168
+ start_background
169
+ fi
170
+ ;;
171
+ esac
172
+
173
+ # After the start, so the version a server ran until now goes once it has stopped.
174
+ prune
175
+ }
176
+
177
+ usage() {
178
+ cat <<'EOF'
179
+ AgentRun's installer, for macOS and Linux (x64, arm64):
180
+
181
+ curl -fsSL https://agentrun.ai/install | sh
182
+ curl -fsSL https://agentrun.ai/install | sh -s -- [options]
183
+
184
+ It installs AgentRun, with a Node.js of its own, in ~/.agentrun, and starts it as a service of this user (launchd on
185
+ macOS, systemd --user on Linux), which keeps running after the terminal closes. Where there is no service manager, it
186
+ starts AgentRun in the background until the machine stops; `agentrun stop` stops it. It returns once AgentRun answers.
187
+
188
+ Options:
189
+ --version V the @parcha/agentrun version or npm tag to install (default: latest)
190
+ --package FILE install the launcher from FILE, a tarball `npm pack` made, in place of the registry's
191
+ --node-version V the Node.js version to install, at least the launcher's floor (default: the newest release on the
192
+ floor's major line, from nodejs.org/dist/index.json)
193
+ --no-start install only
194
+ --service install the service, and fail if this machine can't run one
195
+ --tailscale also serve it over HTTPS on your own tailnet only, at this machine's name there
196
+ (tailscale serve, never Funnel; Tailscale must be installed and logged in, and it says how when
197
+ not); --no-tailscale takes it off
198
+ -h, --help this help
199
+ EOF
200
+ }
201
+
202
+ say() { printf '%s\n' "$*"; }
203
+ fail() {
204
+ printf 'agentrun install: %s\n' "$*" >&2
205
+ exit 1
206
+ }
207
+ need() { command -v "$1" > /dev/null 2>&1 || fail "it needs $1, which this machine does not have"; }
208
+
209
+ download() {
210
+ if command -v curl > /dev/null 2>&1; then
211
+ curl -fsSL --retry 3 --proto '=https' --tlsv1.2 "$1"
212
+ elif command -v wget > /dev/null 2>&1; then
213
+ wget -qO- "$1"
214
+ else
215
+ fail "it needs curl or wget, which this machine does not have"
216
+ fi
217
+ }
218
+
219
+ detect_platform() {
220
+ case $(uname -s) in
221
+ Darwin) os=darwin ;;
222
+ Linux) os=linux ;;
223
+ *) fail "AgentRun runs on macOS and Linux; this is $(uname -s). On Windows, use WSL." ;;
224
+ esac
225
+ case $(uname -m) in
226
+ x86_64 | amd64) arch=x64 ;;
227
+ arm64 | aarch64) arch=arm64 ;;
228
+ *) fail "AgentRun's Node.js comes for x64 and arm64; this is $(uname -m)" ;;
229
+ esac
230
+ # A shell under Rosetta on Apple silicon says x86_64: the machine is arm64.
231
+ if [ "$os" = darwin ] && [ "$arch" = x64 ] && [ "$(sysctl -n sysctl.proc_translated 2>/dev/null)" = 1 ]; then arch=arm64; fi
232
+ # On a musl libc (Alpine), Node's musl build: nodejs.org has one for x64 only.
233
+ suffix=
234
+ if [ "$os" = linux ] && { ls /lib/ld-musl-* > /dev/null 2>&1 || ldd --version 2>&1 | grep -qi musl; }; then
235
+ [ "$arch" = x64 ] || fail "nodejs.org has no Node.js for musl on $arch; use a glibc Linux"
236
+ suffix=-musl
237
+ fi
238
+ }
239
+
240
+ sha256() {
241
+ if command -v sha256sum > /dev/null 2>&1; then
242
+ sha256sum "$1" | cut -d ' ' -f 1
243
+ elif command -v shasum > /dev/null 2>&1; then
244
+ shasum -a 256 "$1" | cut -d ' ' -f 1
245
+ else
246
+ fail "it needs sha256sum or shasum to check Node.js, and this machine has neither"
247
+ fi
248
+ }
249
+
250
+ # Whether version $1 is at least version $2 (X.Y.Z each).
251
+ at_least() {
252
+ printf '%s %s\n' "$1" "$2" | awk '{ split($1, a, "."); split($2, b, ".")
253
+ for (i = 1; i <= 3; i++) if (a[i] + 0 != b[i] + 0) exit !(a[i] + 0 > b[i] + 0)
254
+ exit 0 }'
255
+ }
256
+
257
+ # Of the versions on stdin (X.Y.Z, one a line), the newest on $1's major line that is at least $1.
258
+ newest_from() {
259
+ awk -F. -v floor="$1" 'BEGIN { split(floor, f, ".") }
260
+ $1 + 0 == f[1] + 0 && ($2 + 0 > f[2] + 0 || ($2 + 0 == f[2] + 0 && $3 + 0 >= f[3] + 0)) &&
261
+ (best == "" || $2 + 0 > b2 || ($2 + 0 == b2 && $3 + 0 > b3)) { best = $0; b2 = $2 + 0; b3 = $3 + 0 }
262
+ END { if (best != "") print best }'
263
+ }
264
+
265
+ # $node_version: the newest release nodejs.org builds for this platform on the floor's major line, at least the floor.
266
+ # index.json holds one release a line, its version first and its builds in "files" ("linux-x64", "osx-arm64-tar").
267
+ resolve_node() {
268
+ case $os in darwin) build=osx-$arch-tar ;; *) build=$os-$arch$suffix ;; esac
269
+ download https://nodejs.org/dist/index.json > "$work/index.json" ||
270
+ fail "could not read https://nodejs.org/dist/index.json to choose a Node.js; --node-version V names one"
271
+ node_version=$(grep -F "\"$build\"" "$work/index.json" |
272
+ sed -n 's/^{"version":"v\([0-9][0-9]*\.[0-9][0-9]*\.[0-9][0-9]*\)".*/\1/p' | newest_from "$floor")
273
+ [ -n "$node_version" ] || fail "nodejs.org lists no Node.js ${floor%%.*}.x at or above $floor built for $build"
274
+ }
275
+
276
+ install_node() {
277
+ base=node-v$node_version-$os-$arch$suffix
278
+ say "Downloading Node.js $node_version ($os-$arch$suffix) from nodejs.org ..."
279
+ download "https://nodejs.org/dist/v$node_version/$base.tar.gz" > "$work/$base.tar.gz" || fail "could not download $base.tar.gz"
280
+ download "https://nodejs.org/dist/v$node_version/SHASUMS256.txt" > "$work/SHASUMS256.txt" || fail "could not download Node.js $node_version's SHASUMS256.txt"
281
+ expected=$(awk -v file="$base.tar.gz" '$2 == file { print $1 }' "$work/SHASUMS256.txt")
282
+ [ -n "$expected" ] || fail "SHASUMS256.txt of Node.js $node_version lists no $base.tar.gz"
283
+ [ "$(sha256 "$work/$base.tar.gz")" = "$expected" ] || fail "$base.tar.gz does not match its SHA-256 in SHASUMS256.txt; nothing was installed"
284
+ say "Checked $base.tar.gz against SHASUMS256.txt"
285
+ tar -xzf "$work/$base.tar.gz" -C "$work"
286
+ [ "$("$work/$base/bin/node" --version)" = "v$node_version" ] || fail "the Node.js downloaded does not run here"
287
+ mkdir -p "$root/node"
288
+ replace "$work/$base" "$node_dir"
289
+ }
290
+
291
+ # Moves the folder $1 to $2, in place of what $2 held.
292
+ replace() {
293
+ if [ -e "$2" ]; then
294
+ mv "$2" "$work/old"
295
+ mv "$1" "$2"
296
+ rm -rf "$work/old"
297
+ else
298
+ mv "$1" "$2"
299
+ fi
300
+ }
301
+
302
+ # Removes every other version in lib/ and node/ unless a running process names it on its command line: a server started
303
+ # before this install keeps its files until it stops, and its Node (lib/<version>/.node). Without ps, removes nothing.
304
+ prune() {
305
+ running=$(ps -A -o args= 2> /dev/null) || return 0
306
+ keep_node=$node_dir
307
+ for dir in "$root/lib"/*; do
308
+ [ -d "$dir" ] || continue
309
+ if [ "$dir" = "$lib_dir" ] || in_use "$dir"; then
310
+ [ -f "$dir/.node" ] && keep_node="$keep_node
311
+ $(cat "$dir/.node")"
312
+ else
313
+ rm -rf "$dir"
314
+ fi
315
+ done
316
+ for dir in "$root/node"/*; do
317
+ [ -d "$dir" ] || continue
318
+ printf '%s\n' "$keep_node" | grep -qxF "$dir" || in_use "$dir" || rm -rf "$dir"
319
+ done
320
+ }
321
+
322
+ in_use() {
323
+ case $running in *"$1/"*) return 0 ;; esac
324
+ return 1
325
+ }
326
+
327
+ # Whether this user has a service manager AgentRun can be a service of; $why says why not.
328
+ service_manager() {
329
+ why=
330
+ if [ "$os" = darwin ]; then
331
+ launchctl print "gui/$(id -u)" > /dev/null 2>&1 && return 0
332
+ why="no launchd session for this user: a login over ssh has none"
333
+ else
334
+ command -v systemctl > /dev/null 2>&1 || { why="no systemd"; return 1; }
335
+ systemctl --user show-environment > /dev/null 2>&1 && return 0
336
+ why="no systemd user session"
337
+ fi
338
+ return 1
339
+ }
340
+
341
+ # Whether process $1 runs, from /proc where there is one (a container may have no ps), else from ps. A process that ended
342
+ # is a zombie until its parent reaps it, and in a container with no init nothing does, so a zombie has ended. $args is
343
+ # its command line.
344
+ alive() {
345
+ case $1 in '' | *[!0-9]* | 0 | 1) return 1 ;; esac
346
+ if [ -r "/proc/$1/stat" ]; then
347
+ state=$(sed 's/.*) \(.\).*/\1/' "/proc/$1/stat" 2> /dev/null)
348
+ args=$(tr '\0' ' ' < "/proc/$1/cmdline" 2> /dev/null)
349
+ elif [ -e /proc/self/stat ]; then
350
+ return 1
351
+ else
352
+ state=$(ps -o stat= -p "$1" 2> /dev/null | tr -d ' ' | cut -c 1)
353
+ args=$(ps -o args= -p "$1" 2> /dev/null)
354
+ fi
355
+ [ -n "$state" ] && [ "$state" != Z ]
356
+ }
357
+
358
+ # Whether process $1 runs this install's files: a pid file outlives a crash, and its pid can be another process's by now.
359
+ ours() {
360
+ alive "$1" || return 1
361
+ case $args in *"$root/"*) return 0 ;; esac
362
+ return 1
363
+ }
364
+
365
+ # Stops the AgentRun an earlier install started in the background, so only one server uses ~/.agentrun. The installer does
366
+ # it itself, as `agentrun stop` does, since the version installed may have no `agentrun stop`: a SIGTERM to the launcher,
367
+ # which passes it on to its server (the pid in .server.json) and waits for it. Both get 45 s, past the server's 30 s
368
+ # drain, then a SIGKILL; one that outlives that fails the install, so a second server never starts beside it.
369
+ stop_background() {
370
+ bg=$(cat "$root/agentrun.pid" 2> /dev/null) || return 0
371
+ if ours "$bg"; then
372
+ server=$(sed -n 's/.*"pid": *\([0-9][0-9]*\).*/\1/p' "$root/.server.json" 2> /dev/null | head -n 1)
373
+ say "Stopping the AgentRun started in the background before (pid $bg) ..."
374
+ kill -s TERM "$bg" 2> /dev/null || true
375
+ waited=0
376
+ while { ours "$bg" || ours "$server"; } && [ "$waited" -lt 45 ]; do
377
+ sleep 1
378
+ waited=$((waited + 1))
379
+ done
380
+ if ours "$bg" || ours "$server"; then
381
+ # Started with setsid, it leads a process group, the server's too.
382
+ kill -s KILL -- "-$bg" 2> /dev/null || true
383
+ for each in $bg $server; do
384
+ if ours "$each"; then kill -s KILL "$each" 2> /dev/null || true; fi
385
+ done
386
+ sleep 1
387
+ if ours "$bg" || ours "$server"; then
388
+ fail "the AgentRun started before (pid $bg) still runs after a SIGKILL; stop it, then run the installer again"
389
+ fi
390
+ fi
391
+ fi
392
+ rm -f "$root/agentrun.pid"
393
+ }
394
+
395
+ # AgentRun in the background, where there is no service manager: in a session of its own where setsid is there (Linux),
396
+ # so neither the terminal closing nor a Ctrl-C to it stops AgentRun, with nothing open on this script's output, so a
397
+ # `curl | sh` returns. Its output goes to the service's log, its pid to agentrun.pid for `agentrun stop`. Waits for the
398
+ # launcher's "AgentRun is running at" line, or for it to end (it stops a server that never answers, after 60 s).
399
+ start_background() {
400
+ # On the tailnet (--tailscale, or an earlier install's entry kept): the server takes its https address and a fixed port.
401
+ # Checked before the AgentRun running now stops, so a refusal (Funnel on there) leaves it running.
402
+ [ -n "$ts_flag" ] || [ ! -f "$root/tailscale.json" ] || [ ! -f "$tailnet_js" ] || ts_flag=--tailscale
403
+ set -- --no-open
404
+ public=
405
+ if [ "$ts_flag" = --no-tailscale ]; then
406
+ tailnet off
407
+ elif [ "$ts_flag" = --tailscale ]; then
408
+ set -- --no-open --port 7340
409
+ public=$(tailnet address 7340) || exit 1
410
+ fi
411
+ stop_background
412
+ log=$root/logs/agentrun.log
413
+ touch "$log"
414
+ from=$(($(wc -c < "$log") + 1))
415
+ launch=
416
+ command -v setsid > /dev/null 2>&1 && launch=setsid
417
+ if [ -n "$public" ]; then
418
+ AGENTRUN_PUBLIC_URL=$public AGENTRUN_TRUSTED_PROXIES=loopback nohup $launch "$shim" "$@" < /dev/null >> "$log" 2>&1 &
419
+ else
420
+ nohup $launch "$shim" "$@" < /dev/null >> "$log" 2>&1 &
421
+ fi
422
+ pid=$!
423
+ printf '%s\n' "$pid" > "$root/agentrun.pid"
424
+ url=
425
+ waited=0
426
+ while [ "$waited" -lt 120 ]; do
427
+ url=$(tail -c "+$from" "$log" | sed -n 's/^AgentRun is running at //p' | head -n 1)
428
+ [ -z "$url" ] || break
429
+ alive "$pid" || break
430
+ sleep 1
431
+ waited=$((waited + 1))
432
+ done
433
+ if [ -z "$url" ]; then
434
+ tail -c "+$from" "$log" | tail -n 20 >&2
435
+ kill "$pid" 2> /dev/null || true
436
+ fail "AgentRun did not start (its last lines are above, and all of them are in $log)"
437
+ fi
438
+ # The launcher names the https address it was given; on this machine it is the loopback one, in the server's record,
439
+ # until serve is on.
440
+ here=
441
+ [ -z "$public" ] || here=$(sed -n 's/.*"base_url": *"\(http[^"]*\)\/v2".*/\1\//p' "$root/.server.json" 2> /dev/null | head -n 1)
442
+ [ -z "$here" ] || url=$here
443
+ say "AgentRun is running at $url"
444
+ say "It runs in the background until this machine or container stops. $(shown_shim) stop stops it; its log is $log."
445
+ [ -z "$public" ] || tailnet serve 7340 "$public" || exit 1
446
+ }
447
+
448
+ # tailscale.mjs of the version installed, on its Node: `address PORT`, `serve PORT URL` or `off`.
449
+ tailnet() {
450
+ verb=$1
451
+ shift
452
+ "$node_dir/bin/node" "$tailnet_js" "$verb" "${1:-}" "curl -fsSL https://agentrun.ai/install | sh -s -- $ts_flag" "${2:-}"
453
+ }
454
+
455
+ shown_shim() {
456
+ case ":$PATH:" in *":$root/bin:"*) printf agentrun ;; *) printf '%s' "$shim" ;; esac
457
+ }
458
+
459
+ # One line in the shell's startup file puts ~/.agentrun/bin on PATH, when no line there does yet.
460
+ add_to_path() {
461
+ case ":$PATH:" in *":$root/bin:"*) return 0 ;; esac
462
+ line='export PATH="$HOME/.agentrun/bin:$PATH"'
463
+ case $(basename "${SHELL:-sh}") in
464
+ zsh) rc=${ZDOTDIR:-$HOME}/.zshrc ;;
465
+ bash) if [ "$os" = darwin ]; then rc=$HOME/.bash_profile; else rc=$HOME/.bashrc; fi ;;
466
+ fish)
467
+ rc=${XDG_CONFIG_HOME:-$HOME/.config}/fish/conf.d/agentrun.fish
468
+ line='fish_add_path --global "$HOME/.agentrun/bin"'
469
+ mkdir -p "$(dirname "$rc")"
470
+ ;;
471
+ *) rc=$HOME/.profile ;;
472
+ esac
473
+ if grep -qs '\.agentrun/bin' "$rc"; then
474
+ say "$rc already puts ~/.agentrun/bin on PATH; a new terminal will have agentrun."
475
+ return 0
476
+ fi
477
+ printf '\n%s\n' "$line" >> "$rc"
478
+ say "Added ~/.agentrun/bin to PATH in $rc. A new terminal will have agentrun; in this one, run:"
479
+ say " $line"
480
+ }
481
+
482
+ main "$@"
package/lib/cli.mjs CHANGED
@@ -2,7 +2,9 @@
2
2
  // OpenAPI document, read from the generated table (commands.generated.mjs); nothing here is written per command. Waiting for
3
3
  // a run and following it live (runs start --wait, runs wait, runs timeline --follow) are follow.mjs's; the commands that
4
4
  // read a secret from stdin (login, logout) are login.mjs's, skill install is skill.mjs's, and template
5
- // validate and template eval, which check and run a package folder on this machine, are template.mjs's.
5
+ // validate and template eval, which check and run a package folder on this machine, are template.mjs's, and service
6
+ // (AgentRun as a launchd or systemd user service) and stop (the AgentRun the installer started in the background) are
7
+ // service.mjs's.
6
8
  import { closeSync, constants, fstatSync, openSync, readFileSync } from "node:fs";
7
9
  import { homedir } from "node:os";
8
10
  import { join, resolve } from "node:path";
@@ -10,11 +12,12 @@ import { parseArgs } from "node:util";
10
12
  import { COMMANDS, SECRET_ROUTES } from "./commands.generated.mjs";
11
13
  import { ALL, FLAGS, MAX_PAGES, REQUEST_MS, WAIT, followTimeline, timerMs, waitRun } from "./follow.mjs";
12
14
  import { SECRETS, guarded, refusalExit } from "./login.mjs";
15
+ import { SERVICE, STOP } from "./service.mjs";
13
16
  import { SKILL_INSTALL } from "./skill.mjs";
14
17
  import { TEMPLATE_EVAL, TEMPLATE_INSTALL, TEMPLATE_VALIDATE } from "./template.mjs";
15
18
 
16
19
  /** The hand-written commands that run as they are given (`run`), beside the table's. */
17
- const LOCAL = [...SECRETS, SKILL_INSTALL, TEMPLATE_VALIDATE, TEMPLATE_EVAL, TEMPLATE_INSTALL];
20
+ const LOCAL = [...SECRETS, SKILL_INSTALL, TEMPLATE_VALIDATE, TEMPLATE_EVAL, TEMPLATE_INSTALL, ...SERVICE, STOP];
18
21
  export const NOUNS = new Set(["api", "help", ...[...COMMANDS, ...LOCAL].map((entry) => entry.command[0])]);
19
22
  const DEFAULT_BASE_URL = "http://127.0.0.1:7340/v2"; // key-gate: loopback-rule the default address of the server started on this machine
20
23
  const START = "start it with `npx @parcha/agentrun`, or pass --base-url (or set AGENTRUN_BASE_URL) to the address the server printed";