@astrofoundry/astrogate 0.0.0-stage → 0.1.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (97) hide show
  1. package/README.md +2 -2
  2. package/agents/foreman.md +27 -0
  3. package/cli/astro-install.sh +242 -0
  4. package/cli/astro.sh +449 -0
  5. package/cli/astro.test.ts +65 -0
  6. package/dist/bridge.js +9950 -0
  7. package/dist/claude.js +7899 -0
  8. package/dist/extensions/bridge/index.js +9937 -0
  9. package/dist/extensions/gate/index.js +286 -0
  10. package/dist/extensions/guard/index.js +364 -0
  11. package/dist/extensions/subagents/index.js +8458 -0
  12. package/extensions/bridge/admin.ts +86 -0
  13. package/extensions/bridge/approvals.ts +147 -0
  14. package/extensions/bridge/attachments.ts +65 -0
  15. package/extensions/bridge/chunk.ts +50 -0
  16. package/extensions/bridge/claude-run.test.ts +62 -0
  17. package/extensions/bridge/claude-run.ts +301 -0
  18. package/extensions/bridge/commands.ts +89 -0
  19. package/extensions/bridge/config.ts +263 -0
  20. package/extensions/bridge/conversations.ts +144 -0
  21. package/extensions/bridge/daemon.ts +60 -0
  22. package/extensions/bridge/discord.test.ts +547 -0
  23. package/extensions/bridge/gateway.ts +222 -0
  24. package/extensions/bridge/index.ts +951 -0
  25. package/extensions/bridge/launcher.ts +36 -0
  26. package/extensions/bridge/lifecycle.test.ts +78 -0
  27. package/extensions/bridge/open-work.ts +74 -0
  28. package/extensions/bridge/plugin.ts +172 -0
  29. package/extensions/bridge/quote.ts +39 -0
  30. package/extensions/bridge/reports.ts +65 -0
  31. package/extensions/bridge/rest.ts +217 -0
  32. package/extensions/bridge/setup.ts +218 -0
  33. package/extensions/gate/approvals.test.ts +40 -0
  34. package/extensions/gate/config.ts +110 -0
  35. package/extensions/gate/core.ts +114 -0
  36. package/extensions/gate/index.test.ts +202 -0
  37. package/extensions/gate/index.ts +114 -0
  38. package/extensions/gate/risky.ts +14 -0
  39. package/extensions/gate/types.ts +19 -0
  40. package/extensions/guard/config.test.ts +107 -0
  41. package/extensions/guard/config.ts +120 -0
  42. package/extensions/guard/defaults.ts +40 -0
  43. package/extensions/guard/index.test.ts +313 -0
  44. package/extensions/guard/index.ts +183 -0
  45. package/extensions/guard/rules.test.ts +118 -0
  46. package/extensions/guard/rules.ts +101 -0
  47. package/extensions/guard/types.ts +12 -0
  48. package/extensions/subagents/agents.test.ts +137 -0
  49. package/extensions/subagents/agents.ts +207 -0
  50. package/extensions/subagents/bundled.test.ts +29 -0
  51. package/extensions/subagents/child.test.ts +202 -0
  52. package/extensions/subagents/child.ts +367 -0
  53. package/extensions/subagents/index.test.ts +123 -0
  54. package/extensions/subagents/index.ts +352 -0
  55. package/extensions/subagents/pi-paths.test.ts +39 -0
  56. package/extensions/subagents/pi-paths.ts +58 -0
  57. package/extensions/subagents/render.ts +221 -0
  58. package/extensions/subagents/sessions.test.ts +32 -0
  59. package/extensions/subagents/sessions.ts +34 -0
  60. package/extensions/subagents/ticker.test.ts +40 -0
  61. package/extensions/subagents/ticker.ts +36 -0
  62. package/harness/claude/agents.ts +93 -0
  63. package/harness/claude/claude.test.ts +171 -0
  64. package/harness/claude/hook.ts +77 -0
  65. package/harness/claude/main.ts +122 -0
  66. package/harness/claude/mcp.ts +174 -0
  67. package/harness/claude/settings.ts +63 -0
  68. package/package.json +71 -4
  69. package/release.sha256 +95 -0
  70. package/release.sha256.sig +6 -0
  71. package/specialists/AGENTS.md +67 -0
  72. package/specialists/README.md +81 -0
  73. package/specialists/entry/specialist-cli.sh +22 -0
  74. package/specialists/entry/specialist-install.sh +861 -0
  75. package/specialists/entry/specialist-install.test.ts +305 -0
  76. package/specialists/lib/argv.ts +19 -0
  77. package/specialists/lib/audit.ts +20 -0
  78. package/specialists/lib/config.ts +27 -0
  79. package/specialists/lib/errors.ts +8 -0
  80. package/specialists/lib/flush.test.ts +21 -0
  81. package/specialists/lib/lib.test.ts +87 -0
  82. package/specialists/lib/lock.test.ts +42 -0
  83. package/specialists/lib/lock.ts +67 -0
  84. package/specialists/lib/main.ts +40 -0
  85. package/specialists/lib/output.ts +11 -0
  86. package/specialists/lib/paths.ts +22 -0
  87. package/specialists/lib/pinned.test.ts +55 -0
  88. package/specialists/lib/pinned.ts +67 -0
  89. package/specialists/lib/proc.ts +49 -0
  90. package/specialists/lib/repo.ts +198 -0
  91. package/specialists/lib/runtime.test.ts +81 -0
  92. package/specialists/lib/sanitize.ts +43 -0
  93. package/specialists/lib/secrets.ts +28 -0
  94. package/specialists/lib/ssh.ts +38 -0
  95. package/specialists/lib/tunnel.test.ts +44 -0
  96. package/specialists/lib/tunnel.ts +76 -0
  97. package/specialists/provision/provision.sh +96 -0
@@ -0,0 +1,861 @@
1
+ #!/bin/sh
2
+ # /usr/local/sbin/specialist-install: installs the specialist user's Node, wrappers,
3
+ # arcane-cli, trendarr, Python, gcloud, bw, and jq from downloads it checks itself, and
4
+ # signed specialist packs.
5
+ # Installed root-owned 0755.
6
+ # Runs as root through
7
+ # cortex ALL=(root) NOPASSWD: /usr/local/sbin/specialist-install
8
+ # It runs only system binaries and the Node it installed, never anything the cortex
9
+ # user can write (all of Homebrew), and it touches the specialist's home only as that
10
+ # user, so links the specialist plants there cannot redirect a root write.
11
+ #
12
+ # specialist-install node|wrappers|arcane-cli|trendarr|gcloud|bw|jq|pack <version>
13
+ # specialist-install python <x.y.z+YYYYMMDD>
14
+ # specialist-install rollback node|wrappers|arcane-cli|trendarr|python|gcloud|bw|jq
15
+ # specialist-install status
16
+ # `pack <version>` installs the wrappers of /private/var/tmp/astro-pack/pack-<version>.tgz
17
+ # only when pack-<version>.tgz.sig is an `astro-pack` signature from a key in
18
+ # /usr/local/etc/astro/allowed_signers, a file only root can change. The pack's
19
+ # site/config files replace the specialist's config files of the same name; `rollback
20
+ # wrappers` brings the replaced ones back.
21
+ set -eu
22
+ PATH=/usr/bin:/bin:/usr/sbin:/sbin
23
+ export PATH
24
+ umask 077
25
+ cd /
26
+
27
+ SPECIALIST=specialist
28
+ NODE_BASE=/usr/local/libexec/specialist-node
29
+ PYTHON_BASE=/usr/local/libexec/specialist-python
30
+ GCLOUD_BASE=/usr/local/libexec/specialist-gcloud
31
+ BW_BASE=/usr/local/libexec/specialist-bw
32
+ JQ_BASE=/usr/local/libexec/specialist-jq
33
+ SIGNERS_DIR=/usr/local/etc/astro
34
+ SIGNERS=$SIGNERS_DIR/allowed_signers
35
+ PACK_DROP=/private/var/tmp/astro-pack
36
+ # Components of the official darwin-arm gcloud archive that gcloud itself needs (bq,
37
+ # gsutil, and the empty macOS bundled-python component are left out).
38
+ GCLOUD_COMPONENTS="core core-nix gcloud-deps gcloud-deps-darwin-arm gcloud-crc32c-darwin-arm"
39
+ # Same list as the allowlist in specialist-cli; a new specialist needs both reinstalled.
40
+ SERVICES="arcane identity network dns edge security backup proxmox inference media research home"
41
+
42
+ usage() {
43
+ echo "usage: specialist-install node|wrappers|arcane-cli|trendarr|gcloud|bw|jq|pack <version> | python <x.y.z+YYYYMMDD> | rollback node|wrappers|arcane-cli|trendarr|python|gcloud|bw|jq | status" >&2
44
+ exit 2
45
+ }
46
+
47
+ fail() {
48
+ echo "specialist-install: $*" >&2
49
+ exit 1
50
+ }
51
+
52
+ # x.y.z with plain decimal parts; the character check also rules out newlines.
53
+ valid_version() {
54
+ case "$1" in '' | *[!0-9.]*) return 1 ;; esac
55
+ printf '%s\n' "$1" | /usr/bin/grep -Eq '^(0|[1-9][0-9]{0,5})\.(0|[1-9][0-9]{0,5})\.(0|[1-9][0-9]{0,5})$'
56
+ }
57
+
58
+ # True when version $1 is older than version $2.
59
+ version_lt() {
60
+ old_ifs=$IFS
61
+ IFS=.
62
+ set -- $1 $2
63
+ IFS=$old_ifs
64
+ [ "$1" -lt "$4" ] && return 0
65
+ [ "$1" -gt "$4" ] && return 1
66
+ [ "$2" -lt "$5" ] && return 0
67
+ [ "$2" -gt "$5" ] && return 1
68
+ [ "$3" -lt "$6" ]
69
+ }
70
+
71
+ # A python-build-standalone release: CPython x.y.z and the build date that names the release.
72
+ valid_python_version() {
73
+ case "$1" in '' | *[!0-9.+]*) return 1 ;; esac
74
+ printf '%s\n' "$1" | /usr/bin/grep -Eq '^(0|[1-9][0-9]{0,5})\.(0|[1-9][0-9]{0,5})\.(0|[1-9][0-9]{0,5})\+[0-9]{8}$'
75
+ }
76
+
77
+ # True when python release $1 is older than $2: CPython version first, then build date.
78
+ python_lt() {
79
+ version_lt "${1%+*}" "${2%+*}" && return 0
80
+ version_lt "${2%+*}" "${1%+*}" && return 1
81
+ [ "${1#*+}" -lt "${2#*+}" ]
82
+ }
83
+
84
+ component_ok() {
85
+ case "$1" in node | wrappers | arcane-cli | trendarr | python | gcloud | bw | jq) return 0 ;; esac
86
+ return 1
87
+ }
88
+
89
+ command=${1:-}
90
+ case "$command" in
91
+ node | wrappers | arcane-cli | trendarr | gcloud | bw | jq | pack)
92
+ [ $# -eq 2 ] || usage
93
+ valid_version "$2" || usage
94
+ ;;
95
+ python)
96
+ [ $# -eq 2 ] || usage
97
+ valid_python_version "$2" || usage
98
+ ;;
99
+ rollback)
100
+ [ $# -eq 2 ] || usage
101
+ component_ok "$2" || usage
102
+ ;;
103
+ status) [ $# -eq 1 ] || usage ;;
104
+ *) usage ;;
105
+ esac
106
+
107
+ [ "$(/usr/bin/id -u)" = 0 ] || fail "must run as root: sudo /usr/local/sbin/specialist-install $*"
108
+ [ "$(/usr/bin/uname -s)" = Darwin ] && [ "$(/usr/bin/uname -m)" = arm64 ] || fail "only macOS on arm64 is supported"
109
+ /usr/bin/id -u "$SPECIALIST" >/dev/null 2>&1 || fail "user $SPECIALIST does not exist"
110
+
111
+ work=$(/usr/bin/mktemp -d /private/var/tmp/specialist-install.XXXXXX)
112
+ trap '/bin/rm -rf "$work"' EXIT
113
+ trap 'exit 1' HUP INT TERM
114
+
115
+ # A directory only root can change: owned by uid 0, not a link, writable by nobody else.
116
+ check_root_dir() {
117
+ [ -d "$1" ] && [ ! -L "$1" ] || fail "$1 is not a directory"
118
+ set -- "$1" $(/usr/bin/stat -f '%u %Lp' "$1")
119
+ [ "$2" = 0 ] || fail "$1 is not owned by root"
120
+ case "$3" in *[2367] | *[2367]?) fail "$1 is writable by group or others" ;; esac
121
+ }
122
+
123
+ ensure_root_dir() {
124
+ [ -e "$1" ] || { /bin/mkdir -m 0755 "$1" && /usr/sbin/chown root:wheel "$1"; }
125
+ check_root_dir "$1"
126
+ }
127
+
128
+ check_node_base() {
129
+ check_root_dir /
130
+ check_root_dir /usr
131
+ check_root_dir /usr/local
132
+ ensure_root_dir /usr/local/libexec
133
+ ensure_root_dir "$NODE_BASE"
134
+ }
135
+
136
+ fetch() {
137
+ /usr/bin/curl --proto '=https' --proto-redir '=https' --tlsv1.2 -fsSL --retry 2 --max-time 900 -o "$2" "$1" || fail "download failed: $1"
138
+ }
139
+
140
+ json_field() {
141
+ /usr/bin/plutil -extract "$2" raw -o - "$1" 2>/dev/null
142
+ }
143
+
144
+ # $1 directory, $2 file name in it, $3 a "<sha256> <name>" list.
145
+ check_sha256() {
146
+ want=$(/usr/bin/awk -v f="$2" '$2 == f && length($1) == 64 { print $1; exit }' "$3")
147
+ [ -n "$want" ] || fail "no checksum for $2"
148
+ got=$(/usr/bin/shasum -a 256 "$1/$2" | /usr/bin/awk '{ print $1 }')
149
+ [ "$got" = "$want" ] || fail "checksum mismatch for $2"
150
+ }
151
+
152
+ # Downloads an npm package tarball after checking it against the registry's sha512 integrity.
153
+ fetch_npm() {
154
+ meta="$work/$3.meta.json"
155
+ fetch "https://registry.npmjs.org/$1/$2" "$meta"
156
+ [ "$(json_field "$meta" version)" = "$2" ] || fail "$1@$2 is not in the npm registry"
157
+ tarball=$(json_field "$meta" dist.tarball) || fail "$1@$2 has no tarball"
158
+ integrity=$(json_field "$meta" dist.integrity) || fail "$1@$2 has no integrity"
159
+ case "$tarball" in https://registry.npmjs.org/*) ;; *) fail "unexpected tarball location for $1@$2" ;; esac
160
+ fetch "$tarball" "$work/$3.tgz"
161
+ [ "sha512-$(/usr/bin/openssl dgst -sha512 -binary "$work/$3.tgz" | /usr/bin/base64)" = "$integrity" ] || fail "integrity mismatch for $1@$2"
162
+ }
163
+
164
+ # Runs a script as the specialist user with fixed arguments; the script never sees root's environment.
165
+ as_specialist() {
166
+ script=$1
167
+ shift
168
+ /usr/bin/sudo -n -u "$SPECIALIST" -H /bin/sh -c "set -eu; umask 077; PATH=/usr/bin:/bin:/usr/sbin:/sbin; cd \"\$HOME\"; $script" specialist-install "$@"
169
+ }
170
+
171
+ # Root-owned staging for files the specialist then copies into its home.
172
+ make_stage() {
173
+ /bin/chmod 0711 "$work"
174
+ stage="$work/stage"
175
+ /bin/mkdir -m 0700 "$stage"
176
+ }
177
+
178
+ hand_stage() {
179
+ /usr/sbin/chown -R "$SPECIALIST:$(/usr/bin/id -gn "$SPECIALIST")" "$stage"
180
+ }
181
+
182
+ node_link() {
183
+ target=$(/usr/bin/readlink "$NODE_BASE/$1" 2>/dev/null) || return 0
184
+ valid_version "$target" && [ -x "$NODE_BASE/$target/bin/node" ] && printf '%s' "$target"
185
+ return 0
186
+ }
187
+
188
+ switch_node_link() {
189
+ /bin/rm -f "$NODE_BASE/.$1.tmp"
190
+ (umask 022 && /bin/ln -s "$2" "$NODE_BASE/.$1.tmp")
191
+ /bin/mv -h "$NODE_BASE/.$1.tmp" "$NODE_BASE/$1"
192
+ }
193
+
194
+ current_node() {
195
+ node=$(node_link current)
196
+ [ -n "$node" ] || fail "no specialist Node yet: run specialist-install node <version> first"
197
+ printf '%s' "$NODE_BASE/$node/bin/node"
198
+ }
199
+
200
+ # Specialist side: every installed wrapper answers --help with Node $1; nothing to check before the first install.
201
+ SMOKE_INSTALLED='node=$1
202
+ bin="$HOME/.specialists/bin"
203
+ [ -d "$bin/lib" ] || exit 0
204
+ for r in "$bin"/*/run.ts; do
205
+ [ -f "$r" ] || continue
206
+ "$node" --disable-warning=ExperimentalWarning "$r" --caller specialist-install -- --help >/dev/null 2>&1 || { echo "$r --help failed with $node" >&2; exit 1; }
207
+ done'
208
+
209
+ smoke_installed_wrappers() {
210
+ as_specialist "$SMOKE_INSTALLED" "$1"
211
+ }
212
+
213
+ install_node() {
214
+ version=$1
215
+ check_node_base
216
+ current=$(node_link current)
217
+ if [ -n "$current" ] && version_lt "$version" "$current"; then fail "node $version is older than the installed $current; use rollback"; fi
218
+ [ "$current" != "$version" ] || { echo "node $version is already current"; return 0; }
219
+
220
+ fetch https://nodejs.org/dist/index.json "$work/index.json"
221
+ i=0
222
+ found=""
223
+ while [ "$i" -lt 400 ]; do
224
+ v=$(json_field "$work/index.json" "$i.version") || break
225
+ if [ "$v" = "v$version" ]; then found=$i; break; fi
226
+ i=$((i + 1))
227
+ done
228
+ [ -n "$found" ] || fail "node $version is not among the 400 newest releases"
229
+ [ "$(json_field "$work/index.json" "$found.lts")" != false ] || fail "node $version is not an LTS release"
230
+ /usr/bin/plutil -extract "$found.files" json -o - "$work/index.json" | /usr/bin/grep -q '"osx-arm64-tar"' || fail "node $version has no macOS arm64 build"
231
+
232
+ name="node-v$version-darwin-arm64.tar.gz"
233
+ fetch "https://nodejs.org/dist/v$version/$name" "$work/$name"
234
+ fetch "https://nodejs.org/dist/v$version/SHASUMS256.txt" "$work/SHASUMS256.txt"
235
+ check_sha256 "$work" "$name" "$work/SHASUMS256.txt"
236
+
237
+ dest="$NODE_BASE/$version"
238
+ /bin/rm -rf "$dest.partial"
239
+ /bin/mkdir -m 0755 "$dest.partial"
240
+ /usr/bin/tar -xzf "$work/$name" -C "$dest.partial" --strip-components 1
241
+ /usr/sbin/chown -R root:wheel "$dest.partial"
242
+ /bin/chmod -R u=rwX,go=rX "$dest.partial"
243
+ [ "$("$dest.partial/bin/node" --version)" = "v$version" ] || fail "node $version does not run"
244
+ /bin/rm -rf "$dest"
245
+ /bin/mv "$dest.partial" "$dest"
246
+ smoke_installed_wrappers "$dest/bin/node" || fail "the installed wrappers fail with node $version; current stays ${current:-unset}"
247
+
248
+ [ -z "$current" ] || switch_node_link previous "$current"
249
+ switch_node_link current "$version"
250
+ for d in "$NODE_BASE"/*; do
251
+ n=${d##*/}
252
+ case "$n" in current | previous | "$version" | "$current") continue ;; esac
253
+ /bin/rm -rf "$d"
254
+ done
255
+ echo "node $version is current${current:+; previous $current}"
256
+ }
257
+
258
+ rollback_node() {
259
+ check_node_base
260
+ current=$(node_link current)
261
+ previous=$(node_link previous)
262
+ [ -n "$current" ] && [ -n "$previous" ] || fail "no previous node to roll back to"
263
+ smoke_installed_wrappers "$NODE_BASE/$previous/bin/node" || fail "the installed wrappers fail with node $previous"
264
+ switch_node_link current "$previous"
265
+ switch_node_link previous "$current"
266
+ echo "node $previous is current; previous $current"
267
+ }
268
+
269
+ # Root-owned tools for the specialist: each version in its own folder under
270
+ # /usr/local/libexec/specialist-<tool>, behind the links current and previous.
271
+ check_tool_base() {
272
+ check_root_dir /
273
+ check_root_dir /usr
274
+ check_root_dir /usr/local
275
+ ensure_root_dir /usr/local/libexec
276
+ ensure_root_dir "$1"
277
+ }
278
+
279
+ # $1 base, $2 link name, $3 version check, $4 executable inside a version folder.
280
+ tool_link() {
281
+ target=$(/usr/bin/readlink "$1/$2" 2>/dev/null) || return 0
282
+ "$3" "$target" && [ -x "$1/$target/$4" ] && printf '%s' "$target"
283
+ return 0
284
+ }
285
+
286
+ switch_tool_link() {
287
+ /bin/rm -f "$1/.$2.tmp"
288
+ (umask 022 && /bin/ln -s "$3" "$1/.$2.tmp")
289
+ /bin/mv -h "$1/.$2.tmp" "$1/$2"
290
+ }
291
+
292
+ new_tool_folder() {
293
+ /bin/rm -rf "$1.partial"
294
+ /bin/mkdir -m 0755 "$1.partial"
295
+ }
296
+
297
+ seal_tool_folder() {
298
+ /usr/sbin/chown -R root:wheel "$1.partial"
299
+ /bin/chmod -R u=rwX,go=rX "$1.partial"
300
+ }
301
+
302
+ # Moves the checked $base/$version.partial into place, makes it current, keeps the old current as previous.
303
+ activate_tool() {
304
+ /bin/rm -rf "$1/$2"
305
+ /bin/mv "$1/$2.partial" "$1/$2"
306
+ [ -z "$3" ] || switch_tool_link "$1" previous "$3"
307
+ switch_tool_link "$1" current "$2"
308
+ for d in "$1"/*; do
309
+ n=${d##*/}
310
+ case "$n" in current | previous | "$2" | "$3") continue ;; esac
311
+ /bin/rm -rf "$d"
312
+ done
313
+ }
314
+
315
+ # $1 base, $2 label, $3 version check, $4 executable, $5 smoke function taking the executable's path.
316
+ rollback_tool() {
317
+ check_tool_base "$1"
318
+ current=$(tool_link "$1" current "$3" "$4")
319
+ previous=$(tool_link "$1" previous "$3" "$4")
320
+ [ -n "$current" ] && [ -n "$previous" ] || fail "no previous $2 to roll back to"
321
+ "$5" "$1/$previous/$4" "$previous" || fail "$2 $previous fails its check"
322
+ switch_tool_link "$1" current "$previous"
323
+ switch_tool_link "$1" previous "$current"
324
+ echo "$2 $previous is current; previous $current"
325
+ }
326
+
327
+ # Python from python-build-standalone: gcloud needs Python 3.10 to 3.15, and its macOS
328
+ # installer offers only Homebrew's (owned by cortex) or an unchecked python.org package.
329
+ SMOKE_PYTHON='"$1" -c "import ssl, sqlite3, sys; print(\"%d.%d.%d\" % sys.version_info[:3])"'
330
+ SMOKE_GCLOUD='cfg=$(/usr/bin/mktemp -d); trap "/bin/rm -rf \"\$cfg\"" EXIT
331
+ CLOUDSDK_PYTHON=$2 CLOUDSDK_CONFIG=$cfg CLOUDSDK_CORE_DISABLE_PROMPTS=1 "$1" version 2>/dev/null | /usr/bin/head -n 1'
332
+ SMOKE_BW='app=$(/usr/bin/mktemp -d); trap "/bin/rm -rf \"\$app\"" EXIT
333
+ BITWARDENCLI_APPDATA_DIR=$app "$1" --version 2>/dev/null'
334
+ SMOKE_JQ='"$1" --version'
335
+
336
+ smoke_python() {
337
+ [ "$(as_specialist "$SMOKE_PYTHON" "$1")" = "${2%+*}" ]
338
+ }
339
+
340
+ current_python() {
341
+ python=$(tool_link "$PYTHON_BASE" current valid_python_version bin/python3)
342
+ [ -n "$python" ] || fail "no specialist Python yet: run specialist-install python <x.y.z+YYYYMMDD> first"
343
+ printf '%s' "$PYTHON_BASE/$python/bin/python3"
344
+ }
345
+
346
+ # gcloud $1 (bin/gcloud) answers with version $2, run by Python $3.
347
+ gcloud_answers() {
348
+ [ "$(as_specialist "$SMOKE_GCLOUD" "$1" "$3")" = "Google Cloud SDK $2" ]
349
+ }
350
+
351
+ smoke_gcloud() {
352
+ gcloud_answers "$1" "$2" "$(current_python)"
353
+ }
354
+
355
+ smoke_bw() {
356
+ [ "$(as_specialist "$SMOKE_BW" "$1")" = "$2" ]
357
+ }
358
+
359
+ smoke_jq() {
360
+ [ "$(as_specialist "$SMOKE_JQ" "$1")" = "jq-$2" ]
361
+ }
362
+
363
+ install_python() {
364
+ version=$1
365
+ check_tool_base "$PYTHON_BASE"
366
+ current=$(tool_link "$PYTHON_BASE" current valid_python_version bin/python3)
367
+ if [ -n "$current" ] && python_lt "$version" "$current"; then fail "python $version is older than the installed $current; use rollback"; fi
368
+ [ "$current" != "$version" ] || { echo "python $version is already current"; return 0; }
369
+ tag=${version#*+}
370
+ name="cpython-$version-aarch64-apple-darwin-install_only_stripped.tar.gz"
371
+ base="https://github.com/astral-sh/python-build-standalone/releases/download/$tag"
372
+ fetch "$base/$name" "$work/$name"
373
+ fetch "$base/SHA256SUMS" "$work/SHA256SUMS"
374
+ check_sha256 "$work" "$name" "$work/SHA256SUMS"
375
+ dest="$PYTHON_BASE/$version"
376
+ new_tool_folder "$dest"
377
+ /usr/bin/tar -xzf "$work/$name" -C "$dest.partial" --strip-components 1
378
+ seal_tool_folder "$dest"
379
+ smoke_python "$dest.partial/bin/python3" "$version" || fail "python $version does not run"
380
+ gcloud=$(tool_link "$GCLOUD_BASE" current valid_version bin/gcloud)
381
+ if [ -n "$gcloud" ]; then
382
+ gcloud_answers "$GCLOUD_BASE/$gcloud/bin/gcloud" "$gcloud" "$dest.partial/bin/python3" || fail "gcloud $gcloud fails with python $version; current stays ${current:-unset}"
383
+ fi
384
+ activate_tool "$PYTHON_BASE" "$version" "$current"
385
+ echo "python $version is current${current:+; previous $current}"
386
+ }
387
+
388
+ # gcloud from the components of Google's release snapshot, each archive checked against
389
+ # the sha256 the snapshot lists; the snapshot and the archives come from dl.google.com.
390
+ install_gcloud() {
391
+ version=$1
392
+ python=$(current_python)
393
+ check_tool_base "$GCLOUD_BASE"
394
+ current=$(tool_link "$GCLOUD_BASE" current valid_version bin/gcloud)
395
+ if [ -n "$current" ] && version_lt "$version" "$current"; then fail "gcloud $version is older than the installed $current; use rollback"; fi
396
+ [ "$current" != "$version" ] || { echo "gcloud $version is already current"; return 0; }
397
+ channel=https://dl.google.com/dl/cloudsdk/channels/rapid
398
+ snapshot="$work/components.json"
399
+ fetch "$channel/components-v$version.json" "$snapshot"
400
+ [ "$(json_field "$snapshot" version)" = "$version" ] || fail "gcloud $version has no release snapshot"
401
+ dest="$GCLOUD_BASE/$version"
402
+ new_tool_folder "$dest"
403
+ for want in $GCLOUD_COMPONENTS; do
404
+ i=0
405
+ found=""
406
+ while [ "$i" -lt 1000 ]; do
407
+ id=$(json_field "$snapshot" "components.$i.id") || break
408
+ if [ "$id" = "$want" ]; then found=$i; break; fi
409
+ i=$((i + 1))
410
+ done
411
+ [ -n "$found" ] || fail "gcloud $version has no component $want"
412
+ source=$(json_field "$snapshot" "components.$found.data.source") || fail "component $want has no archive"
413
+ sum=$(json_field "$snapshot" "components.$found.data.checksum") || fail "component $want has no checksum"
414
+ printf '%s\n' "$source" | /usr/bin/grep -Eq '^components/google-cloud-sdk-[a-z0-9-]+-[0-9]{14}\.tar\.gz$' || fail "unexpected archive for component $want"
415
+ printf '%s\n' "$sum" | /usr/bin/grep -Eq '^[0-9a-f]{64}$' || fail "unexpected checksum for component $want"
416
+ fetch "$channel/$source" "$work/$want.tgz"
417
+ [ "$(/usr/bin/shasum -a 256 "$work/$want.tgz" | /usr/bin/awk '{ print $1 }')" = "$sum" ] || fail "checksum mismatch for component $want"
418
+ /usr/bin/tar -xzf "$work/$want.tgz" -C "$dest.partial"
419
+ done
420
+ seal_tool_folder "$dest"
421
+ gcloud_answers "$dest.partial/bin/gcloud" "$version" "$python" || fail "gcloud $version does not run"
422
+ activate_tool "$GCLOUD_BASE" "$version" "$current"
423
+ echo "gcloud $version is current${current:+; previous $current}"
424
+ }
425
+
426
+ # Bitwarden CLI: the native macOS arm64 build, checked against the sha256 digest GitHub publishes for the release asset.
427
+ install_bw() {
428
+ version=$1
429
+ check_tool_base "$BW_BASE"
430
+ current=$(tool_link "$BW_BASE" current valid_version bw)
431
+ if [ -n "$current" ] && version_lt "$version" "$current"; then fail "bw $version is older than the installed $current; use rollback"; fi
432
+ [ "$current" != "$version" ] || { echo "bw $version is already current"; return 0; }
433
+ name="bw-bit-macos-arm64-$version.zip"
434
+ # Bitwarden added the license prefix in 2026.11.0; older releases keep the old name.
435
+ if version_lt "$version" 2026.11.0; then name="bw-macos-arm64-$version.zip"; fi
436
+ release="$work/bw-release.json"
437
+ fetch "https://api.github.com/repos/bitwarden/clients/releases/tags/cli-v$version" "$release"
438
+ i=0
439
+ found=""
440
+ while [ "$i" -lt 100 ]; do
441
+ asset=$(json_field "$release" "assets.$i.name") || break
442
+ if [ "$asset" = "$name" ]; then found=$i; break; fi
443
+ i=$((i + 1))
444
+ done
445
+ [ -n "$found" ] || fail "bw $version has no $name"
446
+ digest=$(json_field "$release" "assets.$found.digest") || fail "$name has no published digest"
447
+ printf '%s\n' "$digest" | /usr/bin/grep -Eq '^sha256:[0-9a-f]{64}$' || fail "unexpected digest for $name"
448
+ url="https://github.com/bitwarden/clients/releases/download/cli-v$version/$name"
449
+ [ "$(json_field "$release" "assets.$found.browser_download_url")" = "$url" ] || fail "unexpected download location for $name"
450
+ fetch "$url" "$work/$name"
451
+ [ "sha256:$(/usr/bin/shasum -a 256 "$work/$name" | /usr/bin/awk '{ print $1 }')" = "$digest" ] || fail "checksum mismatch for $name"
452
+ dest="$BW_BASE/$version"
453
+ new_tool_folder "$dest"
454
+ /usr/bin/unzip -q "$work/$name" bw -d "$dest.partial" || fail "$name has no bw"
455
+ /bin/chmod 0755 "$dest.partial/bw"
456
+ seal_tool_folder "$dest"
457
+ smoke_bw "$dest.partial/bw" "$version" || fail "bw $version does not run"
458
+ activate_tool "$BW_BASE" "$version" "$current"
459
+ echo "bw $version is current${current:+; previous $current}"
460
+ }
461
+
462
+ # jq: the macOS arm64 binary, checked against the release's sha256sum.txt.
463
+ install_jq() {
464
+ version=$1
465
+ check_tool_base "$JQ_BASE"
466
+ current=$(tool_link "$JQ_BASE" current valid_version jq)
467
+ if [ -n "$current" ] && version_lt "$version" "$current"; then fail "jq $version is older than the installed $current; use rollback"; fi
468
+ [ "$current" != "$version" ] || { echo "jq $version is already current"; return 0; }
469
+ base="https://github.com/jqlang/jq/releases/download/jq-$version"
470
+ fetch "$base/jq-macos-arm64" "$work/jq-macos-arm64"
471
+ fetch "$base/sha256sum.txt" "$work/sha256sum.txt"
472
+ check_sha256 "$work" jq-macos-arm64 "$work/sha256sum.txt"
473
+ dest="$JQ_BASE/$version"
474
+ new_tool_folder "$dest"
475
+ /usr/bin/install -m 0755 "$work/jq-macos-arm64" "$dest.partial/jq"
476
+ seal_tool_folder "$dest"
477
+ smoke_jq "$dest.partial/jq" "$version" || fail "jq $version does not run"
478
+ activate_tool "$JQ_BASE" "$version" "$current"
479
+ echo "jq $version is current${current:+; previous $current}"
480
+ }
481
+
482
+ WRAPPER_VERSION='v=$(cat "$HOME/.specialists/bin/VERSION" 2>/dev/null) || exit 0; printf "%s" "$v"'
483
+
484
+ # Moves the new wrapper files in and the old ones aside, two renames per item, then
485
+ # checks every wrapper; on failure the old files come back.
486
+ SWAP_WRAPPERS='node=$1; stage=$2; shift 2
487
+ bin="$HOME/.specialists/bin"; back="$HOME/.specialists/rollback/wrappers"
488
+ items="lib provision.sh VERSION pack.json $*"
489
+ smoke() { d=$1; shift; for s in "$@"; do "$node" --disable-warning=ExperimentalWarning "$d/$s/run.ts" --caller specialist-install -- --help >/dev/null 2>&1 || { echo "$s --help failed" >&2; return 1; }; done; }
490
+ smoke "$stage" "$@" || exit 1
491
+ for f in "$stage/config"/*.json; do
492
+ [ ! -f "$f" ] || "$node" -e "JSON.parse(require(\"node:fs\").readFileSync(process.argv[1], \"utf8\"))" "$f" 2>/dev/null || { echo "config ${f##*/} is not valid JSON" >&2; exit 1; }
493
+ done
494
+ mkdir -p "$bin" "$HOME/.specialists/rollback"
495
+ rm -rf "$back.new"; mkdir "$back.new"
496
+ for i in $items; do rm -rf "$bin/.new-$i"; [ ! -e "$stage/$i" ] || cp -R "$stage/$i" "$bin/.new-$i"; done
497
+ for i in $items; do
498
+ [ ! -e "$bin/$i" ] || mv "$bin/$i" "$back.new/$i"
499
+ [ ! -e "$bin/.new-$i" ] || mv "$bin/.new-$i" "$bin/$i"
500
+ done
501
+ chmod -R go-rwx "$bin"
502
+ if ! smoke "$bin" "$@"; then
503
+ for i in $items; do rm -rf "$bin/$i"; [ ! -e "$back.new/$i" ] || mv "$back.new/$i" "$bin/$i"; done
504
+ rm -rf "$back.new"
505
+ echo "restored the previous wrappers" >&2
506
+ exit 1
507
+ fi
508
+ rm -rf "$back"; mv "$back.new" "$back"
509
+ conf="$HOME/.specialists/config"
510
+ for f in "$stage/config"/*; do
511
+ [ -f "$f" ] || continue
512
+ n=${f##*/}
513
+ mkdir -p "$conf" "$back/.config"
514
+ if [ -f "$conf/$n" ]; then
515
+ ! cmp -s "$f" "$conf/$n" || continue
516
+ cp "$conf/$n" "$back/.config/$n"
517
+ fi
518
+ cp "$f" "$conf/.new-$n"; mv "$conf/.new-$n" "$conf/$n"
519
+ echo "config $n written"
520
+ done'
521
+
522
+ install_wrappers() {
523
+ version=$1
524
+ node=$(current_node)
525
+ current=$(as_specialist "$WRAPPER_VERSION")
526
+ valid_version "$current" || current=""
527
+ if [ -n "$current" ] && version_lt "$version" "$current"; then fail "wrappers $version are older than the installed $current; use rollback"; fi
528
+
529
+ fetch_npm @astrofoundry/pi-astro "$version" pi-astro
530
+ /bin/mkdir "$work/pkg"
531
+ /usr/bin/tar -xzf "$work/pi-astro.tgz" -C "$work/pkg" --strip-components 1
532
+ src="$work/pkg/specialists"
533
+ [ -d "$src/lib" ] && [ -f "$src/provision/provision.sh" ] || fail "pi-astro $version has no specialists/lib or provision.sh"
534
+ make_stage
535
+ /bin/cp -R "$src/lib" "$stage/lib"
536
+ for s in $SERVICES; do
537
+ [ -f "$src/$s/run.ts" ] || fail "pi-astro $version has no $s wrapper"
538
+ /bin/mkdir "$stage/$s"
539
+ /bin/cp "$src/$s/run.ts" "$stage/$s/run.ts"
540
+ done
541
+ /bin/cp "$src/provision/provision.sh" "$stage/provision.sh"
542
+ /usr/bin/find "$stage" -type f -name '*.test.ts' -delete
543
+ printf '%s\n' "$version" > "$stage/VERSION"
544
+ hand_stage
545
+ as_specialist "$SWAP_WRAPPERS" "$node" "$stage" $SERVICES || fail "wrappers $version failed their check; ${current:-no} previous version kept"
546
+ echo "wrappers $version installed${current:+; previous $current}"
547
+ }
548
+
549
+ # A pack service name; the other names in the wrapper folder are taken.
550
+ valid_service() {
551
+ case "$1" in lib | provision | VERSION | rollback) return 1 ;; esac
552
+ printf '%s\n' "$1" | /usr/bin/grep -Eq '^[a-z][a-z0-9-]{0,30}$'
553
+ }
554
+
555
+ # The signing keys: a root-owned file nobody else can write, in folders only root can change.
556
+ check_signers() {
557
+ check_root_dir /
558
+ check_root_dir /usr
559
+ check_root_dir /usr/local
560
+ check_root_dir /usr/local/etc
561
+ check_root_dir "$SIGNERS_DIR"
562
+ [ -f "$SIGNERS" ] && [ ! -L "$SIGNERS" ] || fail "$SIGNERS is missing; install the pack signing key first"
563
+ set -- $(/usr/bin/stat -f '%u %Lp' "$SIGNERS")
564
+ [ "$1" = 0 ] || fail "$SIGNERS is not owned by root"
565
+ case "$2" in *[2367] | *[2367]?) fail "$SIGNERS is writable by group or others" ;; esac
566
+ }
567
+
568
+ # Copies pack $1 and its signature into root's work folder, then checks the signature there;
569
+ # nothing reads the tarball before that.
570
+ take_pack() {
571
+ for f in "$PACK_DROP/pack-$1.tgz" "$PACK_DROP/pack-$1.tgz.sig"; do
572
+ [ -f "$f" ] && [ ! -L "$f" ] || fail "$f is missing"
573
+ done
574
+ /bin/cp "$PACK_DROP/pack-$1.tgz" "$work/pack.tgz"
575
+ /bin/cp "$PACK_DROP/pack-$1.tgz.sig" "$work/pack.sig"
576
+ /usr/bin/ssh-keygen -Y verify -f "$SIGNERS" -I astro-pack -n astro-pack -s "$work/pack.sig" < "$work/pack.tgz" >/dev/null 2>&1 ||
577
+ fail "pack-$1.tgz carries no valid astro-pack signature from a key in $SIGNERS"
578
+ }
579
+
580
+ # A config file name in a pack's site/config folder: <service>.json, or a .yml a wrapper names.
581
+ valid_config_name() {
582
+ printf '%s\n' "$1" | /usr/bin/grep -Eq '^[a-z][a-z0-9-]{0,30}\.(json|yml)$'
583
+ }
584
+
585
+ # Copies the config files of the pack's site/config folder $1 into the stage; a pack without the folder has none.
586
+ stage_config() {
587
+ [ -e "$1" ] || [ -L "$1" ] || return 0
588
+ [ -d "$1" ] && [ ! -L "$1" ] || fail "the pack's site/config is not a folder"
589
+ /bin/mkdir "$stage/config"
590
+ for f in "$1"/* "$1"/.[!.]* "$1"/..?*; do
591
+ [ -e "$f" ] || [ -L "$f" ] || continue
592
+ name=${f##*/}
593
+ valid_config_name "$name" || fail "the pack's site/config holds a name that is no <service>.json or .yml"
594
+ [ -f "$f" ] && [ ! -L "$f" ] || fail "the pack's site/config/$name is not a plain file"
595
+ [ "$(/usr/bin/stat -f '%z' "$f")" -le 65536 ] || fail "the pack's site/config/$name is larger than 64 KiB"
596
+ /bin/cp "$f" "$stage/config/$name"
597
+ done
598
+ }
599
+
600
+ # The services pack.json $1 names, checked, one per line; fails on none, a bad name, or a repeat.
601
+ pack_services() {
602
+ i=0
603
+ found=" "
604
+ while s=$(json_field "$1" "specialists.$i.service"); do
605
+ valid_service "$s" || fail "pack.json names an invalid service"
606
+ case "$found" in *" $s "*) fail "pack.json names $s twice" ;; esac
607
+ found="$found$s "
608
+ printf '%s\n' "$s"
609
+ i=$((i + 1))
610
+ [ "$i" -lt 100 ] || fail "pack.json names more than 100 specialists"
611
+ done
612
+ [ "$i" -gt 0 ] || fail "pack.json names no specialists"
613
+ }
614
+
615
+ install_pack() {
616
+ version=$1
617
+ check_signers
618
+ node=$(current_node)
619
+ current=$(as_specialist "$WRAPPER_VERSION")
620
+ valid_version "$current" || current=""
621
+ if [ -n "$current" ] && version_lt "$version" "$current"; then fail "pack $version is older than the installed wrappers $current; use rollback"; fi
622
+
623
+ take_pack "$version"
624
+ /bin/mkdir "$work/pkg"
625
+ /usr/bin/tar -xzf "$work/pack.tgz" --no-same-owner -C "$work/pkg"
626
+ manifest="$work/pkg/pack.json"
627
+ [ -f "$manifest" ] && [ ! -L "$manifest" ] || fail "the pack has no pack.json"
628
+ [ "$(json_field "$manifest" version)" = "$version" ] || fail "pack.json does not name version $version"
629
+ src="$work/pkg/specialists"
630
+ [ -d "$src/lib" ] && [ -f "$src/provision/provision.sh" ] || fail "the pack has no specialists/lib or provision.sh"
631
+ services=$(pack_services "$manifest") || exit 1
632
+ make_stage
633
+ /bin/cp -R "$src/lib" "$stage/lib"
634
+ for s in $services; do
635
+ [ -f "$src/$s/run.ts" ] || fail "the pack has no $s wrapper"
636
+ /bin/mkdir "$stage/$s"
637
+ /bin/cp "$src/$s/run.ts" "$stage/$s/run.ts"
638
+ done
639
+ /bin/cp "$src/provision/provision.sh" "$stage/provision.sh"
640
+ /bin/cp "$manifest" "$stage/pack.json"
641
+ stage_config "$work/pkg/site/config"
642
+ /usr/bin/find "$stage" -type f -name '*.test.ts' -delete
643
+ printf '%s\n' "$version" > "$stage/VERSION"
644
+ hand_stage
645
+ as_specialist "$SWAP_WRAPPERS" "$node" "$stage" $services || fail "pack $version failed its check; ${current:-no} previous version kept"
646
+ echo "pack $version installed${current:+; previous $current}"
647
+ }
648
+
649
+ ROLLBACK_WRAPPERS='node=$1
650
+ bin="$HOME/.specialists/bin"; back="$HOME/.specialists/rollback/wrappers"
651
+ [ -f "$back/VERSION" ] || { echo "no previous wrappers to roll back to" >&2; exit 1; }
652
+ services=""
653
+ for r in "$bin"/*/run.ts "$back"/*/run.ts; do
654
+ [ -f "$r" ] || continue
655
+ s=${r%/run.ts}; s=${s##*/}
656
+ case " $services " in *" $s "*) ;; *) services="$services $s" ;; esac
657
+ done
658
+ set -- $services
659
+ items="lib provision.sh VERSION pack.json $*"
660
+ conf="$HOME/.specialists/config"
661
+ swap() { for i in $items; do
662
+ [ ! -e "$bin/$i" ] || mv "$bin/$i" "$bin/.old-$i"
663
+ [ ! -e "$back/$i" ] || mv "$back/$i" "$bin/$i"
664
+ [ ! -e "$bin/.old-$i" ] || mv "$bin/.old-$i" "$back/$i"
665
+ done
666
+ for f in "$back/.config"/*; do
667
+ [ -f "$f" ] || continue
668
+ n=${f##*/}
669
+ [ ! -f "$conf/$n" ] || mv "$conf/$n" "$conf/.old-$n"
670
+ mv "$f" "$conf/$n"
671
+ [ ! -f "$conf/.old-$n" ] || mv "$conf/.old-$n" "$back/.config/$n"
672
+ done; }
673
+ swap
674
+ for s in "$@"; do
675
+ [ -f "$bin/$s/run.ts" ] || continue
676
+ "$node" --disable-warning=ExperimentalWarning "$bin/$s/run.ts" --caller specialist-install -- --help >/dev/null 2>&1 || { swap; echo "$s --help failed; nothing changed" >&2; exit 1; }
677
+ done
678
+ cat "$bin/VERSION"'
679
+
680
+ rollback_wrappers() {
681
+ node=$(current_node)
682
+ restored=$(as_specialist "$ROLLBACK_WRAPPERS" "$node") || fail "wrapper rollback failed"
683
+ echo "wrappers $restored restored"
684
+ }
685
+
686
+ ARCANE_VERSION='out=$("$HOME/.specialists/bin/arcane-cli" --version 2>/dev/null) || exit 0
687
+ printf "%s\n" "$out" | awk '"'"'{ for (i = 1; i < NF; i++) if ($i == "version") { print $(i + 1); exit } }'"'"''
688
+
689
+ INSTALL_ARCANE='stage=$1; v=$2
690
+ bin="$HOME/.specialists/bin"
691
+ check() { "$1" --version 2>/dev/null | awk -v v="$v" '"'"'{ for (i = 1; i < NF; i++) if ($i == "version" && $(i + 1) == v) f = 1 } END { exit !f }'"'"'; }
692
+ mkdir -p "$bin"
693
+ cp "$stage/arcane-cli" "$bin/.arcane-cli.new"
694
+ chmod 0700 "$bin/.arcane-cli.new"
695
+ check "$bin/.arcane-cli.new" || { rm -f "$bin/.arcane-cli.new"; echo "the new arcane-cli does not report version $v" >&2; exit 1; }
696
+ had=no
697
+ if [ -e "$bin/arcane-cli" ]; then mv "$bin/arcane-cli" "$bin/arcane-cli.previous"; had=yes; fi
698
+ mv "$bin/.arcane-cli.new" "$bin/arcane-cli"
699
+ if ! check "$bin/arcane-cli"; then
700
+ rm -f "$bin/arcane-cli"
701
+ [ "$had" = no ] || mv "$bin/arcane-cli.previous" "$bin/arcane-cli"
702
+ echo "restored the previous arcane-cli" >&2
703
+ exit 1
704
+ fi'
705
+
706
+ install_arcane_cli() {
707
+ version=$1
708
+ current=$(as_specialist "$ARCANE_VERSION")
709
+ valid_version "$current" || current=""
710
+ if [ -n "$current" ] && version_lt "$version" "$current"; then fail "arcane-cli $version is older than the installed $current; use rollback"; fi
711
+
712
+ asset=arcane-cli_darwin_arm64.tar.gz
713
+ base="https://github.com/getarcaneapp/arcane/releases/download/v$version"
714
+ fetch "$base/$asset" "$work/$asset"
715
+ fetch "$base/arcane_${version}_checksums.txt" "$work/checksums.txt"
716
+ check_sha256 "$work" "$asset" "$work/checksums.txt"
717
+ /bin/mkdir "$work/acli"
718
+ /usr/bin/tar -xzf "$work/$asset" -C "$work/acli"
719
+ cli=$(/usr/bin/find "$work/acli" -type f -name arcane-cli | /usr/bin/head -n 1)
720
+ [ -n "$cli" ] || fail "no arcane-cli binary in $asset"
721
+ make_stage
722
+ /bin/cp "$cli" "$stage/arcane-cli"
723
+ hand_stage
724
+ as_specialist "$INSTALL_ARCANE" "$stage" "$version" || fail "arcane-cli $version failed its check"
725
+ echo "arcane-cli $version installed${current:+; previous $current}"
726
+ }
727
+
728
+ ROLLBACK_ARCANE='bin="$HOME/.specialists/bin"
729
+ [ -e "$bin/arcane-cli.previous" ] || { echo "no previous arcane-cli to roll back to" >&2; exit 1; }
730
+ mv "$bin/arcane-cli" "$bin/.arcane-cli.old"
731
+ mv "$bin/arcane-cli.previous" "$bin/arcane-cli"
732
+ mv "$bin/.arcane-cli.old" "$bin/arcane-cli.previous"
733
+ "$bin/arcane-cli" --version'
734
+
735
+ rollback_arcane_cli() {
736
+ out=$(as_specialist "$ROLLBACK_ARCANE") || fail "arcane-cli rollback failed"
737
+ echo "restored $out"
738
+ }
739
+
740
+ # trendarr lives in versioned directories behind the stable link bin/trendarr, so
741
+ # media.json's trendarrCli path never changes; bin/trendarr.previous keeps the last one.
742
+ TRENDARR_COMMON='bin="$HOME/.specialists/bin"
743
+ cli() { printf "%s/node_modules/@astrofoundry/trendarr/dist/cli.js" "$1"; }
744
+ version_of() { plutil -extract version raw -o - "$1/node_modules/@astrofoundry/trendarr/package.json" 2>/dev/null; }
745
+ link() { rm -f "$bin/.$1.tmp"; ln -s "$2" "$bin/.$1.tmp"; mv -h "$bin/.$1.tmp" "$bin/$1"; }'
746
+
747
+ TRENDARR_VERSION="$TRENDARR_COMMON"'
748
+ [ -e "$bin/trendarr" ] || exit 0
749
+ version_of "$bin/trendarr" || exit 0'
750
+
751
+ # A hand-installed bin/trendarr directory (the first media setup) becomes trendarr-<version>
752
+ # behind the link; one without a readable package moves aside to bin/trendarr.unknown.
753
+ INSTALL_TRENDARR="$TRENDARR_COMMON"'
754
+ node=$1; npm=$2; v=$3
755
+ if [ -d "$bin/trendarr" ] && [ ! -L "$bin/trendarr" ]; then
756
+ old=$(version_of "$bin/trendarr" || true)
757
+ case "$old" in
758
+ "" | *[!0-9.]*) rm -rf "$bin/trendarr.unknown"; mv "$bin/trendarr" "$bin/trendarr.unknown" ;;
759
+ *) rm -rf "$bin/trendarr-$old"; mv "$bin/trendarr" "$bin/trendarr-$old"; link trendarr "trendarr-$old" ;;
760
+ esac
761
+ fi
762
+ dir="$bin/trendarr-$v"
763
+ mkdir -p "$bin"
764
+ rm -rf "$dir.partial"
765
+ "$node" "$npm" install --prefix "$dir.partial" --omit=dev --ignore-scripts --no-audit --no-fund --no-update-notifier "@astrofoundry/trendarr@$v" >/dev/null
766
+ [ "$("$node" "$(cli "$dir.partial")" --version)" = "$v" ] || { rm -rf "$dir.partial"; echo "trendarr $v does not report its version" >&2; exit 1; }
767
+ before=$(readlink "$bin/trendarr" 2>/dev/null || true)
768
+ rm -rf "$dir"
769
+ mv "$dir.partial" "$dir"
770
+ link trendarr "trendarr-$v"
771
+ if [ "$("$node" "$(cli "$bin/trendarr")" --version)" != "$v" ]; then
772
+ if [ -n "$before" ]; then link trendarr "$before"; else rm -f "$bin/trendarr"; fi
773
+ echo "restored the previous trendarr" >&2
774
+ exit 1
775
+ fi
776
+ if [ -n "$before" ] && [ "$before" != "trendarr-$v" ]; then link trendarr.previous "$before"; fi
777
+ keep=$(readlink "$bin/trendarr.previous" 2>/dev/null || true)
778
+ for d in "$bin"/trendarr-*; do
779
+ [ -d "$d" ] || continue
780
+ n=${d##*/}
781
+ [ "$n" = "trendarr-$v" ] || [ "$n" = "$keep" ] || rm -rf "$d"
782
+ done'
783
+
784
+ install_trendarr() {
785
+ version=$1
786
+ node=$(current_node)
787
+ npm="${node%/bin/node}/lib/node_modules/npm/bin/npm-cli.js"
788
+ [ -f "$npm" ] || fail "the specialist Node has no npm"
789
+ current=$(as_specialist "$TRENDARR_VERSION")
790
+ valid_version "$current" || current=""
791
+ if [ -n "$current" ] && version_lt "$version" "$current"; then fail "trendarr $version is older than the installed $current; use rollback"; fi
792
+ [ "$current" != "$version" ] || { echo "trendarr $version is already current"; return 0; }
793
+ as_specialist "$INSTALL_TRENDARR" "$node" "$npm" "$version" || fail "trendarr $version failed its check"
794
+ echo "trendarr $version installed${current:+; previous $current}"
795
+ }
796
+
797
+ ROLLBACK_TRENDARR="$TRENDARR_COMMON"'
798
+ node=$1
799
+ previous=$(readlink "$bin/trendarr.previous" 2>/dev/null) || { echo "no previous trendarr to roll back to" >&2; exit 1; }
800
+ current=$(readlink "$bin/trendarr")
801
+ [ -d "$bin/$previous" ] || { echo "$previous is missing" >&2; exit 1; }
802
+ link trendarr "$previous"
803
+ link trendarr.previous "$current"
804
+ "$node" "$(cli "$bin/trendarr")" --version'
805
+
806
+ rollback_trendarr() {
807
+ node=$(current_node)
808
+ out=$(as_specialist "$ROLLBACK_TRENDARR" "$node") || fail "trendarr rollback failed"
809
+ echo "trendarr $out restored"
810
+ }
811
+
812
+ json_version() {
813
+ if valid_version "$1"; then printf '"%s"' "$1"; else printf 'null'; fi
814
+ }
815
+
816
+ json_python() {
817
+ if valid_python_version "$1"; then printf '"%s"' "$1"; else printf 'null'; fi
818
+ }
819
+
820
+ # {"current":..,"previous":..} of one root-owned tool: $1 base, $2 version check, $3 executable, $4 JSON writer.
821
+ json_tool() {
822
+ printf '{"current":%s,"previous":%s}' "$("$4" "$(tool_link "$1" current "$2" "$3")")" "$("$4" "$(tool_link "$1" previous "$2" "$3")")"
823
+ }
824
+
825
+ status() {
826
+ wrappers=$(as_specialist "$WRAPPER_VERSION" || true)
827
+ arcane=$(as_specialist "$ARCANE_VERSION" || true)
828
+ trendarr=$(as_specialist "$TRENDARR_VERSION" || true)
829
+ printf '{"node":{"current":%s,"previous":%s},"wrappers":%s,"arcaneCli":%s,"trendarr":%s,"python":%s,"gcloud":%s,"bw":%s,"jq":%s}\n' \
830
+ "$(json_version "$(node_link current)")" "$(json_version "$(node_link previous)")" \
831
+ "$(json_version "$wrappers")" "$(json_version "$arcane")" "$(json_version "$trendarr")" \
832
+ "$(json_tool "$PYTHON_BASE" valid_python_version bin/python3 json_python)" \
833
+ "$(json_tool "$GCLOUD_BASE" valid_version bin/gcloud json_version)" \
834
+ "$(json_tool "$BW_BASE" valid_version bw json_version)" \
835
+ "$(json_tool "$JQ_BASE" valid_version jq json_version)"
836
+ }
837
+
838
+ case "$command" in
839
+ node) install_node "$2" ;;
840
+ wrappers) install_wrappers "$2" ;;
841
+ pack) install_pack "$2" ;;
842
+ arcane-cli) install_arcane_cli "$2" ;;
843
+ trendarr) install_trendarr "$2" ;;
844
+ python) install_python "$2" ;;
845
+ gcloud) install_gcloud "$2" ;;
846
+ bw) install_bw "$2" ;;
847
+ jq) install_jq "$2" ;;
848
+ rollback)
849
+ case "$2" in
850
+ node) rollback_node ;;
851
+ wrappers) rollback_wrappers ;;
852
+ arcane-cli) rollback_arcane_cli ;;
853
+ trendarr) rollback_trendarr ;;
854
+ python) rollback_tool "$PYTHON_BASE" python valid_python_version bin/python3 smoke_python ;;
855
+ gcloud) rollback_tool "$GCLOUD_BASE" gcloud valid_version bin/gcloud smoke_gcloud ;;
856
+ bw) rollback_tool "$BW_BASE" bw valid_version bw smoke_bw ;;
857
+ jq) rollback_tool "$JQ_BASE" jq valid_version jq smoke_jq ;;
858
+ esac
859
+ ;;
860
+ status) status ;;
861
+ esac