@astrofoundry/astrogate 0.0.0-stage → 0.1.2

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 (101) hide show
  1. package/README.md +13 -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/cli/astrogate.sh +309 -0
  7. package/cli/astrogate.test.ts +113 -0
  8. package/cli/verify-release.sh +77 -0
  9. package/cli/verify-release.test.ts +140 -0
  10. package/dist/bridge.js +9950 -0
  11. package/dist/claude.js +7899 -0
  12. package/dist/extensions/bridge/index.js +9937 -0
  13. package/dist/extensions/gate/index.js +286 -0
  14. package/dist/extensions/guard/index.js +364 -0
  15. package/dist/extensions/subagents/index.js +8458 -0
  16. package/extensions/bridge/admin.ts +86 -0
  17. package/extensions/bridge/approvals.ts +147 -0
  18. package/extensions/bridge/attachments.ts +65 -0
  19. package/extensions/bridge/chunk.ts +50 -0
  20. package/extensions/bridge/claude-run.test.ts +62 -0
  21. package/extensions/bridge/claude-run.ts +301 -0
  22. package/extensions/bridge/commands.ts +89 -0
  23. package/extensions/bridge/config.ts +263 -0
  24. package/extensions/bridge/conversations.ts +144 -0
  25. package/extensions/bridge/daemon.ts +60 -0
  26. package/extensions/bridge/discord.test.ts +547 -0
  27. package/extensions/bridge/gateway.ts +222 -0
  28. package/extensions/bridge/index.ts +951 -0
  29. package/extensions/bridge/launcher.ts +36 -0
  30. package/extensions/bridge/lifecycle.test.ts +78 -0
  31. package/extensions/bridge/open-work.ts +74 -0
  32. package/extensions/bridge/plugin.ts +172 -0
  33. package/extensions/bridge/quote.ts +39 -0
  34. package/extensions/bridge/reports.ts +65 -0
  35. package/extensions/bridge/rest.ts +217 -0
  36. package/extensions/bridge/setup.ts +218 -0
  37. package/extensions/gate/approvals.test.ts +40 -0
  38. package/extensions/gate/config.ts +110 -0
  39. package/extensions/gate/core.ts +114 -0
  40. package/extensions/gate/index.test.ts +202 -0
  41. package/extensions/gate/index.ts +114 -0
  42. package/extensions/gate/risky.ts +14 -0
  43. package/extensions/gate/types.ts +19 -0
  44. package/extensions/guard/config.test.ts +107 -0
  45. package/extensions/guard/config.ts +120 -0
  46. package/extensions/guard/defaults.ts +40 -0
  47. package/extensions/guard/index.test.ts +313 -0
  48. package/extensions/guard/index.ts +183 -0
  49. package/extensions/guard/rules.test.ts +118 -0
  50. package/extensions/guard/rules.ts +101 -0
  51. package/extensions/guard/types.ts +12 -0
  52. package/extensions/subagents/agents.test.ts +137 -0
  53. package/extensions/subagents/agents.ts +207 -0
  54. package/extensions/subagents/bundled.test.ts +29 -0
  55. package/extensions/subagents/child.test.ts +202 -0
  56. package/extensions/subagents/child.ts +367 -0
  57. package/extensions/subagents/index.test.ts +123 -0
  58. package/extensions/subagents/index.ts +352 -0
  59. package/extensions/subagents/pi-paths.test.ts +39 -0
  60. package/extensions/subagents/pi-paths.ts +58 -0
  61. package/extensions/subagents/render.ts +221 -0
  62. package/extensions/subagents/sessions.test.ts +32 -0
  63. package/extensions/subagents/sessions.ts +34 -0
  64. package/extensions/subagents/ticker.test.ts +40 -0
  65. package/extensions/subagents/ticker.ts +36 -0
  66. package/harness/claude/agents.ts +93 -0
  67. package/harness/claude/claude.test.ts +171 -0
  68. package/harness/claude/hook.ts +77 -0
  69. package/harness/claude/main.ts +122 -0
  70. package/harness/claude/mcp.ts +174 -0
  71. package/harness/claude/settings.ts +63 -0
  72. package/package.json +71 -4
  73. package/release.sha256 +99 -0
  74. package/release.sha256.sig +6 -0
  75. package/specialists/AGENTS.md +67 -0
  76. package/specialists/README.md +81 -0
  77. package/specialists/entry/specialist-cli.sh +22 -0
  78. package/specialists/entry/specialist-install.sh +861 -0
  79. package/specialists/entry/specialist-install.test.ts +305 -0
  80. package/specialists/lib/argv.ts +19 -0
  81. package/specialists/lib/audit.ts +20 -0
  82. package/specialists/lib/config.ts +27 -0
  83. package/specialists/lib/errors.ts +8 -0
  84. package/specialists/lib/flush.test.ts +21 -0
  85. package/specialists/lib/lib.test.ts +87 -0
  86. package/specialists/lib/lock.test.ts +42 -0
  87. package/specialists/lib/lock.ts +67 -0
  88. package/specialists/lib/main.ts +40 -0
  89. package/specialists/lib/output.ts +11 -0
  90. package/specialists/lib/paths.ts +22 -0
  91. package/specialists/lib/pinned.test.ts +55 -0
  92. package/specialists/lib/pinned.ts +67 -0
  93. package/specialists/lib/proc.ts +49 -0
  94. package/specialists/lib/repo.ts +198 -0
  95. package/specialists/lib/runtime.test.ts +81 -0
  96. package/specialists/lib/sanitize.ts +43 -0
  97. package/specialists/lib/secrets.ts +28 -0
  98. package/specialists/lib/ssh.ts +38 -0
  99. package/specialists/lib/tunnel.test.ts +44 -0
  100. package/specialists/lib/tunnel.ts +76 -0
  101. package/specialists/provision/provision.sh +96 -0
package/cli/astro.sh ADDED
@@ -0,0 +1,449 @@
1
+ #!/bin/sh
2
+ # astro: check, update, and roll back the astro toolset on this machine, as the user that runs it.
3
+ #
4
+ # astro doctor read-only checks; exits 1 when one fails
5
+ # astro update pi-astro newest pi-astro (removes a rollback pin), then a bridge restart when idle
6
+ # astro update pack <x.y.z> the signed pack from /private/var/tmp/astro-pack, then a bridge restart when idle
7
+ # astro rollback pack the previous pack: wrappers, agents, skills, bridge plugin, gate entries
8
+ # astro rollback pi-astro <x.y.z> that pi-astro version, pinned until the next `astro update pi-astro`
9
+ # astro restart bridge restarts the Discord bridge once nothing is open
10
+ # astro harness claude the specialists in Claude Code too: subagents, the gate hook, herdr's integration;
11
+ # every later `astro update pack` refreshes them
12
+ # astro bridge daemon|pi the Discord bridge as its own process (daemon) or inside a headless Pi (pi)
13
+ # astro install <x.y.z> [<signers>] sets up the specialists on a new Mac from a signed pack (astro-install.sh):
14
+ # prints the plan, then applies it in a terminal after the owner's yes
15
+ #
16
+ # Root-owned files (the entry point, the root installer, the pack signing key) change only through the
17
+ # owner's sudo; `astro update pi-astro` prints the commands when pi-astro ships new copies.
18
+ # macOS only for now; Linux follows with a test host.
19
+ set -eu
20
+
21
+ usage() {
22
+ echo "usage: astro doctor | update pi-astro | update pack <x.y.z> | rollback pack | rollback pi-astro <x.y.z> | restart bridge | harness claude | bridge daemon|pi | install <x.y.z> [<allowed_signers>]" >&2
23
+ exit 2
24
+ }
25
+
26
+ fail() {
27
+ echo "astro: $*" >&2
28
+ exit 1
29
+ }
30
+
31
+ # x.y.z version $1 is older than $2.
32
+ version_lt() {
33
+ old_ifs=$IFS
34
+ IFS=.
35
+ set -- $1 $2
36
+ IFS=$old_ifs
37
+ [ "$1" -lt "$4" ] && return 0
38
+ [ "$1" -gt "$4" ] && return 1
39
+ [ "$2" -lt "$5" ] && return 0
40
+ [ "$2" -gt "$5" ] && return 1
41
+ [ "$3" -lt "$6" ]
42
+ }
43
+
44
+ valid_version() {
45
+ case "$1" in '' | *[!0-9.]*) return 1 ;; esac
46
+ printf '%s' "$1" | /usr/bin/grep -Eq '^[0-9]{1,6}\.[0-9]{1,6}\.[0-9]{1,6}$'
47
+ }
48
+
49
+ case "${1:-}" in
50
+ doctor) [ $# -eq 1 ] || usage ;;
51
+ update)
52
+ [ $# -ge 2 ] || usage
53
+ case "$2" in
54
+ pi-astro) [ $# -eq 2 ] || usage ;;
55
+ pack) [ $# -eq 3 ] && valid_version "$3" || usage ;;
56
+ *) usage ;;
57
+ esac
58
+ ;;
59
+ rollback)
60
+ [ $# -ge 2 ] || usage
61
+ case "$2" in
62
+ pack) [ $# -eq 2 ] || usage ;;
63
+ pi-astro) [ $# -eq 3 ] && valid_version "$3" || usage ;;
64
+ *) usage ;;
65
+ esac
66
+ ;;
67
+ restart) [ $# -eq 2 ] && [ "$2" = bridge ] || usage ;;
68
+ harness) [ $# -eq 2 ] && [ "$2" = claude ] || usage ;;
69
+ bridge) [ $# -eq 2 ] && { [ "$2" = daemon ] || [ "$2" = pi ]; } || usage ;;
70
+ install) { [ $# -eq 2 ] || [ $# -eq 3 ]; } && valid_version "$2" || usage ;;
71
+ *) usage ;;
72
+ esac
73
+
74
+ [ "$(/usr/bin/id -u)" != 0 ] || fail "run astro as the toolset's user, not as root"
75
+ [ "$(/usr/bin/uname -s)" = Darwin ] || fail "only macOS is supported yet"
76
+
77
+ # The first pi-astro that ships this command as an executable; an older one would leave no astro to go forward with.
78
+ FIRST_WITH_ASTRO=0.40.110
79
+ # Homebrew first, then the user's own programs (a native Claude Code install lives in ~/.local/bin).
80
+ PATH=/opt/homebrew/bin:$HOME/.local/bin:/usr/local/bin:/usr/bin:/bin:/usr/sbin:/sbin
81
+ export PATH
82
+ AGENT_DIR="$HOME/.pi/agent"
83
+ # This script's package: follow the ~/.local/bin/astro link to cli/astro.sh, then go one folder up.
84
+ self=$0
85
+ while [ -L "$self" ]; do
86
+ target=$(/usr/bin/readlink "$self")
87
+ case "$target" in /*) self=$target ;; *) self=$(/usr/bin/dirname "$self")/$target ;; esac
88
+ done
89
+ PACKAGE=$(cd "$(/usr/bin/dirname "$self")/.." && /bin/pwd -P)
90
+ PACKAGE_SOURCE=npm:@astrofoundry/pi-astro
91
+ BRIDGE_DIR="$HOME/.config/astro-discord"
92
+ BRIDGE_LABEL=com.astrofoundry.astro-discord
93
+ DROP=/private/var/tmp/astro-pack
94
+ SIGNERS=/usr/local/etc/astro/allowed_signers
95
+ INSTALLER=/usr/local/sbin/specialist-install
96
+ ENTRY=/usr/local/bin/specialist-cli
97
+ JQ=/usr/local/libexec/specialist-jq/current/jq
98
+ SPECIALIST=specialist
99
+ UID_NOW=$(/usr/bin/id -u)
100
+ CLAUDE_HARNESS="$PACKAGE/dist/claude.js"
101
+ BRIDGE_DAEMON="$PACKAGE/dist/bridge.js"
102
+ # Present when `astro harness claude` set up Claude Code; pack updates then refresh its agents.
103
+ CLAUDE_MARK="$HOME/.config/astro/harness-claude"
104
+
105
+ # One value from a JSON file (`-` reads stdin): `v` is the parsed document; fails when the value is missing.
106
+ json() {
107
+ node -e '
108
+ const fs = require("node:fs");
109
+ const v = JSON.parse(fs.readFileSync(process.argv[1] === "-" ? 0 : process.argv[1], "utf8"));
110
+ const r = new Function("v", `return (${process.argv[2]});`)(v);
111
+ if (r === undefined || r === null) process.exit(1);
112
+ process.stdout.write(typeof r === "string" ? r : JSON.stringify(r));
113
+ ' "$1" "$2"
114
+ }
115
+
116
+ sha() {
117
+ /usr/bin/shasum -a 256 "$1" | /usr/bin/cut -d' ' -f1
118
+ }
119
+
120
+ # The configured pi-astro source in Pi's settings: unversioned, or pinned by a rollback.
121
+ package_source() {
122
+ json "$AGENT_DIR/settings.json" 'v.packages && v.packages.find((p) => typeof p === "string" && (p === "npm:@astrofoundry/pi-astro" || p.startsWith("npm:@astrofoundry/pi-astro@")))' 2>/dev/null || true
123
+ }
124
+
125
+ launch_state() {
126
+ /bin/launchctl print "gui/$UID_NOW/$1" 2>/dev/null | /usr/bin/awk '/^\tstate = / { print $3; exit }'
127
+ }
128
+
129
+ # --- doctor ---------------------------------------------------------------
130
+
131
+ failed=0
132
+ ok() { printf 'ok %s\n' "$*"; }
133
+ warn() { printf 'warn %s\n' "$*"; }
134
+ bad() {
135
+ printf 'FAIL %s\n' "$*"
136
+ failed=1
137
+ }
138
+
139
+ check_root_file() {
140
+ file=$1 mode=$2 copy=$3
141
+ if [ ! -f "$file" ] || [ -L "$file" ]; then
142
+ bad "$file is missing"
143
+ return
144
+ fi
145
+ set -- $(/usr/bin/stat -f '%Su %Lp' "$file")
146
+ if [ "$1" != root ] || [ "$2" != "$mode" ]; then
147
+ bad "$file is owned by $1 with mode $2; it must be root with mode $mode"
148
+ elif [ -n "$copy" ] && [ -f "$copy" ] && [ "$(sha "$file")" != "$(sha "$copy")" ]; then
149
+ warn "$file differs from pi-astro's copy; astro update pi-astro prints the owner's install command"
150
+ else
151
+ ok "$file"
152
+ fi
153
+ }
154
+
155
+ check_service() {
156
+ label=$1 name=$2 required=$3
157
+ state=$(launch_state "$label")
158
+ if [ "$state" = running ]; then
159
+ ok "$name is running ($label)"
160
+ elif [ "$required" = yes ]; then
161
+ bad "$name is ${state:-not loaded} ($label)"
162
+ else
163
+ warn "$name is ${state:-not loaded} ($label)"
164
+ fi
165
+ }
166
+
167
+ doctor() {
168
+ printf 'astro doctor on %s, macOS %s on %s\n' "$(/bin/hostname -s)" "$(/usr/bin/sw_vers -productVersion)" "$(/usr/bin/uname -m)"
169
+
170
+ if node_version=$(node --version 2>/dev/null); then
171
+ major=${node_version#v}
172
+ major=${major%%.*}
173
+ if [ "$major" -ge 22 ]; then ok "node $node_version"; else bad "node $node_version; pi-astro needs 22 or later"; fi
174
+ else
175
+ bad "node is not on $PATH"
176
+ fi
177
+ if pi_version=$(pi --version 2>/dev/null); then ok "pi $pi_version"; else bad "pi is not on $PATH"; fi
178
+
179
+ if [ -f "$PACKAGE/package.json" ]; then
180
+ installed=$(json "$PACKAGE/package.json" v.version)
181
+ source=$(package_source)
182
+ latest=$(/usr/bin/curl -fsS --max-time 10 https://registry.npmjs.org/@astrofoundry/pi-astro/latest 2>/dev/null | json - v.version 2>/dev/null || true)
183
+ case "$source" in
184
+ "$PACKAGE_SOURCE")
185
+ if [ -n "$latest" ] && [ "$latest" != "$installed" ]; then warn "pi-astro $installed; $latest is published (astro update pi-astro)"; else ok "pi-astro $installed"; fi
186
+ ;;
187
+ "$PACKAGE_SOURCE"@*) warn "pi-astro $installed, pinned as $source by a rollback; astro update pi-astro removes the pin" ;;
188
+ *) bad "pi-astro $installed is installed but not listed in $AGENT_DIR/settings.json" ;;
189
+ esac
190
+ else
191
+ bad "pi-astro is not installed: pi install $PACKAGE_SOURCE"
192
+ fi
193
+
194
+ check_root_file "$ENTRY" 755 "$PACKAGE/specialists/entry/specialist-cli.sh"
195
+ check_root_file "$INSTALLER" 755 "$PACKAGE/specialists/entry/specialist-install.sh"
196
+ check_root_file "$SIGNERS" 644 ""
197
+
198
+ rules=$(/usr/bin/sudo -n -l 2>/dev/null || true)
199
+ case "$rules" in *"($SPECIALIST) NOPASSWD: $ENTRY"*) ok "sudo runs $ENTRY as $SPECIALIST" ;; *) bad "sudo has no NOPASSWD rule for $ENTRY as $SPECIALIST" ;; esac
200
+ case "$rules" in *"(root) NOPASSWD: $INSTALLER"*) ok "sudo runs $INSTALLER as root" ;; *) bad "sudo has no NOPASSWD rule for $INSTALLER as root" ;; esac
201
+
202
+ if /usr/bin/id -u "$SPECIALIST" >/dev/null 2>&1; then
203
+ home=$(/usr/bin/dscl . -read "/Users/$SPECIALIST" NFSHomeDirectory 2>/dev/null | /usr/bin/awk '{ print $2 }')
204
+ owner_mode=$(/usr/bin/stat -f '%Su %Lp' "$home" 2>/dev/null || echo "missing")
205
+ if [ "$owner_mode" = "$SPECIALIST 700" ]; then ok "user $SPECIALIST, home $home with mode 700"; else bad "user $SPECIALIST: home $home is $owner_mode; it must be $SPECIALIST with mode 700"; fi
206
+ else
207
+ bad "user $SPECIALIST does not exist"
208
+ fi
209
+
210
+ if status=$(/usr/bin/sudo -n "$INSTALLER" status 2>/dev/null); then
211
+ tools=$(printf '%s' "$status" | json - '[["pack", v.wrappers], ["node", v.node && v.node.current], ["jq", v.jq && v.jq.current], ["bw", v.bw && v.bw.current], ["python", v.python && v.python.current], ["gcloud", v.gcloud && v.gcloud.current], ["arcane-cli", v.arcaneCli], ["trendarr", v.trendarr]].filter(([, x]) => x).map(([n, x]) => `${n} ${x}`).join(", ")')
212
+ if printf '%s' "$status" | json - 'v.wrappers' >/dev/null 2>&1; then ok "installed: $tools"; else bad "no pack is installed (installed: $tools)"; fi
213
+ else
214
+ bad "$INSTALLER status failed"
215
+ fi
216
+
217
+ agents=$(/bin/ls "$AGENT_DIR/pack/agents" 2>/dev/null | /usr/bin/grep -c '\.md$' || true)
218
+ skills=$(/bin/ls "$AGENT_DIR/pack/skills" 2>/dev/null | /usr/bin/wc -l | /usr/bin/tr -d ' ')
219
+ if [ "$agents" -gt 0 ]; then ok "pack agents $agents, skills $skills in $AGENT_DIR/pack"; else bad "$AGENT_DIR/pack has no agents"; fi
220
+ if [ -f "$AGENT_DIR/pack/bridge/index.js" ]; then ok "bridge plugin $AGENT_DIR/pack/bridge/index.js"; else warn "the pack has no bridge plugin"; fi
221
+ [ -d "$AGENT_DIR/pack.previous" ] && ok "previous pack kept for astro rollback pack" || warn "no previous pack to roll back to"
222
+
223
+ if services=$(json "$AGENT_DIR/specialists.json" 'v.specialists.map((s) => s.service).join(" ")' 2>/dev/null); then
224
+ broken=""
225
+ for s in $services; do
226
+ /usr/bin/sudo -n -u "$SPECIALIST" -H "$ENTRY" "$s" --caller astro-doctor -- --help >/dev/null 2>&1 || broken="$broken $s"
227
+ done
228
+ if [ -z "$broken" ]; then ok "specialists answer --help: $services"; else bad "specialists failing --help:$broken"; fi
229
+ else
230
+ bad "$AGENT_DIR/specialists.json is missing or has no specialists"
231
+ fi
232
+
233
+ if [ -f "$HOME/Library/LaunchAgents/$BRIDGE_LABEL.plist" ]; then
234
+ check_service "$BRIDGE_LABEL" "Discord bridge" yes
235
+ for f in config.json token; do
236
+ mode=$(/usr/bin/stat -f '%Lp' "$BRIDGE_DIR/$f" 2>/dev/null || echo missing)
237
+ if [ "$mode" = 600 ]; then ok "$BRIDGE_DIR/$f with mode 600"; else bad "$BRIDGE_DIR/$f is $mode; it must exist with mode 600"; fi
238
+ done
239
+ if /usr/bin/grep -q 'dist/bridge.js' "$BRIDGE_DIR/run.sh" 2>/dev/null; then
240
+ if facts=$(node "$BRIDGE_DAEMON" check 2>/dev/null); then
241
+ ok "the bridge runs as its own process: runner $(printf '%s' "$facts" | json - v.runner), $(printf '%s' "$facts" | json - v.agents.length) agents, pi-astro $(printf '%s' "$facts" | json - v.version)"
242
+ else
243
+ bad "the bridge's self-check failed: node $BRIDGE_DAEMON check"
244
+ fi
245
+ else
246
+ ok "the bridge runs inside a headless Pi (astro bridge daemon makes it its own process)"
247
+ fi
248
+ log="$HOME/Library/Logs/astro-discord.log"
249
+ plugin=$(/usr/bin/grep 'connected as' "$log" 2>/dev/null | /usr/bin/tail -n 1 | /usr/bin/grep -oE 'plugin [a-z0-9-]+|no site plugin' || true)
250
+ case "$plugin" in
251
+ plugin*) ok "bridge runs with site $plugin" ;;
252
+ *) warn "the bridge's last start loaded no site plugin" ;;
253
+ esac
254
+ [ -f "$BRIDGE_DIR/busy.json" ] && warn "the bridge has open work now (busy.json)" || true
255
+ else
256
+ warn "no Discord bridge on this machine"
257
+ fi
258
+ check_service com.astrofoundry.herdr herdr no
259
+ check_service com.monadeo.astrogate Astrogate no
260
+
261
+ if [ -f "$CLAUDE_MARK" ]; then
262
+ expected=$(json "$AGENT_DIR/specialists.json" 'v.specialists.length' 2>/dev/null || echo 0)
263
+ generated=$(/usr/bin/grep -l '^generatedBy: astro$' "$HOME"/.claude/agents/astro-*.md 2>/dev/null | /usr/bin/wc -l | /usr/bin/tr -d ' ')
264
+ if [ "$generated" = "$expected" ]; then ok "Claude Code: $generated specialist subagents"; else bad "Claude Code: $generated specialist subagents for $expected specialists (astro harness claude)"; fi
265
+ if /usr/bin/grep -q "$CLAUDE_HARNESS hook" "$HOME/.claude/settings.json" 2>/dev/null; then ok "Claude Code: the specialist gate hook is in ~/.claude/settings.json"; else bad "Claude Code: no specialist gate hook in ~/.claude/settings.json (astro harness claude)"; fi
266
+ if command -v herdr >/dev/null 2>&1; then
267
+ case "$(herdr integration status 2>/dev/null | /usr/bin/grep '^claude:')" in
268
+ "claude: current"*) ok "herdr's Claude Code integration" ;;
269
+ *) warn "herdr's Claude Code integration is not current (herdr integration install claude)" ;;
270
+ esac
271
+ fi
272
+ fi
273
+
274
+ [ "$failed" = 0 ] || exit 1
275
+ }
276
+
277
+ # --- bridge restart ---------------------------------------------------------
278
+
279
+ restart_bridge() {
280
+ if [ -z "$(launch_state "$BRIDGE_LABEL")" ]; then
281
+ echo "no Discord bridge on this machine to restart"
282
+ return 0
283
+ fi
284
+ waited=0
285
+ while [ -f "$BRIDGE_DIR/busy.json" ]; do
286
+ [ "$waited" -lt 1800 ] || fail "the bridge still has open work after 30 minutes; run astro restart bridge later"
287
+ [ "$waited" -gt 0 ] || echo "the bridge has open work; waiting until it finishes"
288
+ /bin/sleep 10
289
+ waited=$((waited + 10))
290
+ done
291
+ /bin/launchctl kickstart -k "gui/$UID_NOW/$BRIDGE_LABEL"
292
+ echo "bridge restarted"
293
+ }
294
+
295
+ # --- pi-astro -------------------------------------------------------------
296
+
297
+ installed_package() {
298
+ json "$PACKAGE/package.json" v.version 2>/dev/null || echo none
299
+ }
300
+
301
+ # The owner's commands for root files that the new pi-astro ships changed.
302
+ root_file_commands() {
303
+ for pair in "specialist-cli.sh $ENTRY" "specialist-install.sh $INSTALLER"; do
304
+ set -- $pair
305
+ copy="$PACKAGE/specialists/entry/$1"
306
+ [ -f "$copy" ] || continue
307
+ if [ ! -f "$2" ] || [ "$(sha "$2")" != "$(sha "$copy")" ]; then
308
+ echo "pi-astro ships a new $2; the owner installs it (sha256 $(sha "$copy")):"
309
+ echo " sudo install -m 0755 -o root -g wheel $copy $2"
310
+ fi
311
+ done
312
+ }
313
+
314
+ update_pi_astro() {
315
+ before=$(installed_package)
316
+ case "$(package_source)" in
317
+ "$PACKAGE_SOURCE"@*) pi install "$PACKAGE_SOURCE" ;;
318
+ *) pi update "$PACKAGE_SOURCE" ;;
319
+ esac
320
+ after=$(installed_package)
321
+ [ "$after" != none ] || fail "pi-astro is not installed after the update"
322
+ echo "pi-astro $before -> $after"
323
+ root_file_commands
324
+ [ "$before" = "$after" ] || restart_bridge
325
+ }
326
+
327
+ rollback_pi_astro() {
328
+ ! version_lt "$1" "$FIRST_WITH_ASTRO" || fail "pi-astro $1 has no astro command, so nothing could update it again; roll back to $FIRST_WITH_ASTRO or later"
329
+ before=$(installed_package)
330
+ pi install "$PACKAGE_SOURCE@$1"
331
+ after=$(installed_package)
332
+ [ "$after" = "$1" ] || fail "pi-astro $after is installed, not $1"
333
+ echo "pi-astro $before -> $after, pinned until astro update pi-astro"
334
+ restart_bridge
335
+ }
336
+
337
+ # --- pack -----------------------------------------------------------------
338
+
339
+ update_pack() {
340
+ version=$1
341
+ work=$(/usr/bin/mktemp -d)
342
+ trap '/bin/rm -rf "$work"' EXIT
343
+ [ -x "$JQ" ] || fail "$JQ is missing: sudo $INSTALLER jq <version>"
344
+ /bin/cp "$DROP/pack-$version.tgz" "$work/pack.tgz"
345
+ /bin/cp "$DROP/pack-$version.tgz.sig" "$work/pack.sig"
346
+ /usr/bin/ssh-keygen -Y verify -f "$SIGNERS" -I astro-pack -n astro-pack -s "$work/pack.sig" <"$work/pack.tgz" >/dev/null ||
347
+ fail "pack-$version.tgz carries no valid signature from $SIGNERS"
348
+
349
+ /usr/bin/sudo -n "$INSTALLER" pack "$version"
350
+
351
+ /bin/mkdir "$work/pkg"
352
+ /usr/bin/tar -xzf "$work/pack.tgz" -C "$work/pkg"
353
+ [ "$("$JQ" -r .version "$work/pkg/pack.json")" = "$version" ] || fail "pack.json does not name version $version"
354
+ /bin/rm -rf "$AGENT_DIR/pack.new"
355
+ /bin/mkdir -p "$AGENT_DIR/pack.new"
356
+ for part in agents skills bridge site; do
357
+ [ ! -d "$work/pkg/$part" ] || /bin/mv "$work/pkg/$part" "$AGENT_DIR/pack.new/"
358
+ done
359
+ /bin/cp "$work/pkg/pack.json" "$AGENT_DIR/pack.new/pack.json"
360
+ # The pack before this one stays for `astro rollback pack`.
361
+ /bin/rm -rf "$AGENT_DIR/pack.previous"
362
+ [ ! -e "$AGENT_DIR/pack" ] || /bin/mv "$AGENT_DIR/pack" "$AGENT_DIR/pack.previous"
363
+ /bin/mv "$AGENT_DIR/pack.new" "$AGENT_DIR/pack"
364
+
365
+ # The gate keeps its entry point and user; the specialists, their timeouts, and their risky patterns come from the pack.
366
+ config="$AGENT_DIR/specialists.json"
367
+ /bin/cp "$config" "$config.before-pack"
368
+ "$JQ" --slurpfile pack "$work/pkg/pack.json" \
369
+ '.specialists = [$pack[0].specialists[] | {agent, tool, service, timeoutMs} + (if (.risky | length) > 0 then {risky} else {} end)]' \
370
+ "$config" >"$config.new"
371
+ /bin/mv "$config.new" "$config"
372
+ echo "pack $version: wrappers, agents, skills, bridge plugin, and gate entries installed; the previous pack is in $AGENT_DIR/pack.previous"
373
+ [ ! -f "$CLAUDE_MARK" ] || claude_harness
374
+ restart_bridge
375
+ }
376
+
377
+ # Swaps the pack and its gate entries with the kept previous ones, so a second rollback goes forward again.
378
+ rollback_pack() {
379
+ [ -d "$AGENT_DIR/pack.previous" ] || fail "no previous pack in $AGENT_DIR/pack.previous"
380
+ config="$AGENT_DIR/specialists.json"
381
+ [ -f "$config.before-pack" ] || fail "no previous gate file $config.before-pack"
382
+ /usr/bin/sudo -n "$INSTALLER" rollback wrappers
383
+ /bin/rm -rf "$AGENT_DIR/pack.swap"
384
+ /bin/mv "$AGENT_DIR/pack" "$AGENT_DIR/pack.swap"
385
+ /bin/mv "$AGENT_DIR/pack.previous" "$AGENT_DIR/pack"
386
+ /bin/mv "$AGENT_DIR/pack.swap" "$AGENT_DIR/pack.previous"
387
+ /bin/mv "$config" "$config.swap"
388
+ /bin/mv "$config.before-pack" "$config"
389
+ /bin/mv "$config.swap" "$config.before-pack"
390
+ echo "pack rolled back: wrappers, agents, skills, bridge plugin, and gate entries; astro rollback pack again goes forward"
391
+ [ ! -f "$CLAUDE_MARK" ] || [ ! -f "$CLAUDE_HARNESS" ] || claude_harness
392
+ restart_bridge
393
+ }
394
+
395
+ # --- bridge host --------------------------------------------------------------
396
+
397
+ # Rewrites the LaunchAgent's run.sh: the bridge as its own Node process, or inside a headless Pi on a FIFO.
398
+ bridge_host() {
399
+ run="$BRIDGE_DIR/run.sh"
400
+ [ -f "$run" ] || fail "no $run: set up the bridge first (setup.ts)"
401
+ node_link=$(command -v node) || fail "node is not on $PATH"
402
+ if [ "$1" = daemon ]; then
403
+ [ -f "$BRIDGE_DAEMON" ] || fail "pi-astro has no bridge daemon ($BRIDGE_DAEMON); astro update pi-astro"
404
+ body="# The Discord bridge as its own process; it starts Pi or Claude Code for each task (runner in config.json).
405
+ exec $node_link $BRIDGE_DAEMON"
406
+ else
407
+ pi_link=$(command -v pi) || fail "pi is not on $PATH"
408
+ body="# Headless Pi host for the Discord bridge. Pi's RPC mode exits when stdin closes; a FIFO opened read-write never closes.
409
+ export ASTRO_DISCORD=1
410
+ fifo=\"\$HOME/.config/astro-discord/stdin.fifo\"
411
+ [ -p \"\$fifo\" ] || mkfifo -m 600 \"\$fifo\"
412
+ exec $pi_link --mode rpc --no-session <> \"\$fifo\""
413
+ fi
414
+ printf '%s\n%s\n%s\n%s\n' '#!/bin/sh' 'export PATH="/opt/homebrew/bin:$HOME/.local/bin:/usr/bin:/bin"' 'cd "$HOME"' "$body" >"$run.new"
415
+ /bin/chmod 0700 "$run.new"
416
+ /bin/mv "$run.new" "$run"
417
+ echo "the bridge now starts $( [ "$1" = daemon ] && echo "as its own process" || echo "inside a headless Pi")"
418
+ restart_bridge
419
+ }
420
+
421
+ # --- Claude Code ------------------------------------------------------------
422
+
423
+ # Subagents from the installed pack, the gate hook with allow rules and time limits in ~/.claude/settings.json,
424
+ # and herdr's agent-state integration.
425
+ claude_harness() {
426
+ command -v claude >/dev/null 2>&1 || fail "claude is not on $PATH"
427
+ [ -f "$CLAUDE_HARNESS" ] || fail "pi-astro has no Claude Code harness ($CLAUDE_HARNESS); astro update pi-astro"
428
+ /bin/mkdir -p "$HOME/.claude/agents" "$HOME/.config/astro"
429
+ node_link=$(command -v node)
430
+ node "$CLAUDE_HARNESS" agents "$HOME/.claude/agents" "$node_link"
431
+ node "$CLAUDE_HARNESS" settings "$HOME/.claude/settings.json" "$node_link"
432
+ : >"$CLAUDE_MARK"
433
+ if command -v herdr >/dev/null 2>&1; then
434
+ herdr integration install claude >/dev/null && echo "herdr: Claude Code integration installed"
435
+ fi
436
+ }
437
+
438
+ case "$1" in
439
+ doctor) doctor ;;
440
+ update) if [ "$2" = pack ]; then update_pack "$3"; else update_pi_astro; fi ;;
441
+ rollback) if [ "$2" = pack ]; then rollback_pack; else rollback_pi_astro "$3"; fi ;;
442
+ restart) restart_bridge ;;
443
+ harness) claude_harness ;;
444
+ bridge) bridge_host "$2" ;;
445
+ install)
446
+ . "$PACKAGE/cli/astro-install.sh"
447
+ install_main "$2" "${3:-}"
448
+ ;;
449
+ esac
@@ -0,0 +1,65 @@
1
+ import { spawnSync } from "node:child_process";
2
+ import { readFileSync } from "node:fs";
3
+ import { join } from "node:path";
4
+ import { describe, expect, it } from "vitest";
5
+
6
+ const SCRIPT = join(import.meta.dirname, "astro.sh");
7
+
8
+ function astro(...args: string[]) {
9
+ return spawnSync("/bin/sh", [SCRIPT, ...args], { encoding: "utf-8" });
10
+ }
11
+
12
+ /** Runs one shell function of the script on its own, followed by `call`. */
13
+ function shellFunction(name: string, call: string) {
14
+ const match = new RegExp(`^${name}\\(\\) \\{\\n[\\s\\S]*?^\\}`, "m").exec(readFileSync(SCRIPT, "utf-8"));
15
+ if (!match) throw new Error(`${name} not found in the script`);
16
+ return spawnSync("/bin/sh", ["-c", `${match[0]}\n${call}`], { encoding: "utf-8" });
17
+ }
18
+
19
+ describe("astro", () => {
20
+ it.each([
21
+ ["0.40.109", "0.40.110", 0],
22
+ ["0.40.110", "0.40.110", 1],
23
+ ["0.41.0", "0.40.110", 1],
24
+ ["0.9.200", "0.40.110", 0],
25
+ ["1.0.0", "0.40.110", 1],
26
+ ])("version_lt %s %s exits %i", (a, b, code) => {
27
+ expect(shellFunction("version_lt", `version_lt ${a} ${b}`).status).toBe(code);
28
+ });
29
+
30
+ it("parses as POSIX sh", () => {
31
+ expect(spawnSync("/bin/sh", ["-n", SCRIPT]).status).toBe(0);
32
+ expect(spawnSync("/bin/sh", ["-n", join(import.meta.dirname, "astro-install.sh")]).status).toBe(0);
33
+ });
34
+
35
+ it.each([
36
+ [[]],
37
+ [["doctor", "now"]],
38
+ [["update"]],
39
+ [["update", "pi"]],
40
+ [["update", "pi-astro", "0.40.108"]],
41
+ [["update", "pack"]],
42
+ [["update", "pack", "1.1"]],
43
+ [["update", "pack", "1.1.1\nrm"]],
44
+ [["update", "pack", "../1.1.1"]],
45
+ [["rollback"]],
46
+ [["rollback", "pack", "1.1.0"]],
47
+ [["rollback", "pi-astro"]],
48
+ [["rollback", "pi-astro", "v0.40.107"]],
49
+ [["restart"]],
50
+ [["restart", "herdr"]],
51
+ [["install"]],
52
+ [["install", "1.2"]],
53
+ [["install", "latest"]],
54
+ [["install", "1.2.3", "signers", "extra"]],
55
+ [["harness"]],
56
+ [["harness", "codex"]],
57
+ [["harness", "claude", "now"]],
58
+ [["bridge"]],
59
+ [["bridge", "claude"]],
60
+ ])("refuses %j with a usage error before anything else", (args) => {
61
+ const result = astro(...args);
62
+ expect(result.status).toBe(2);
63
+ expect(result.stderr).toMatch(/^usage: astro/);
64
+ });
65
+ });