Repository navigation
Expand file tree
/
Copy pathinstall.sh
More file actions
executable file
·1489 lines (1339 loc) · 53.2 KB
/
Copy pathinstall.sh
File metadata and controls
executable file
·1489 lines (1339 loc) · 53.2 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#!/bin/bash
set -e
# Agent Relay Installer
# Usage: curl -fsSL https://raw.githubusercontent.com/AgentWorkforce/relay/main/install.sh | bash
#
# Options (set as environment variables):
# AGENT_RELAY_VERSION - Specific version to install (default: latest)
# AGENT_RELAY_INSTALL_DIR - Installation directory (default: ~/.agentworkforce/relay)
# AGENT_RELAY_BIN_DIR - Binary directory (default: ~/.local/bin)
# AGENT_RELAY_TELEMETRY_DISABLED - Disable anonymous install telemetry (default: false)
REPO_RELAY="AgentWorkforce/relay"
VERSION="${AGENT_RELAY_VERSION:-latest}"
INSTALL_DIR="${AGENT_RELAY_INSTALL_DIR:-$HOME/.agentworkforce/relay}"
BIN_DIR="${AGENT_RELAY_BIN_DIR:-$HOME/.local/bin}"
ORIGINAL_PATH="${PATH:-}"
STANDALONE_FAILURE_REASON=""
# Colors
RED='\033[0;31m'
GREEN='\033[0;32m'
YELLOW='\033[1;33m'
BLUE='\033[0;34m'
CYAN='\033[0;36m'
BOLD='\033[1m'
NC='\033[0m'
info() { echo -e "${BLUE}[info]${NC} $1"; }
success() { echo -e "${GREEN}[✓]${NC} $1"; }
warn() { echo -e "${YELLOW}[warn]${NC} $1"; }
error() {
echo -e "${RED}[error]${NC} $1"
# Track failure if telemetry is initialized
if [ -n "$INSTALL_ID" ]; then
# Escape special characters for JSON (newlines, quotes, backslashes)
local escaped_error
escaped_error=$(printf '%s' "$1" | sed 's/\\/\\\\/g; s/"/\\"/g' | tr '\n' ' ')
track_event "install_failed" ", \"error\": \"$escaped_error\""
fi
exit 1
}
step() { echo -e "\n${CYAN}${BOLD}$1${NC}"; }
# Telemetry (respects AGENT_RELAY_TELEMETRY_DISABLED)
POSTHOG_API_KEY="phc_2uDu01GtnLABJpVkWw4ri1OgScLU90aEmXmDjufGdqr"
POSTHOG_HOST="https://us.i.posthog.com"
INSTALL_ID=""
INSTALL_METHOD=""
telemetry_enabled() {
# Respect opt-out
if [ "${AGENT_RELAY_TELEMETRY_DISABLED:-}" = "1" ] || [ "${AGENT_RELAY_TELEMETRY_DISABLED:-}" = "true" ]; then
return 1
fi
# Also check DO_NOT_TRACK (standard env var)
if [ "${DO_NOT_TRACK:-}" = "1" ]; then
return 1
fi
return 0
}
generate_install_id() {
# Generate a random ID for this install session
if command -v uuidgen &> /dev/null; then
INSTALL_ID=$(uuidgen | tr '[:upper:]' '[:lower:]')
elif [ -f /proc/sys/kernel/random/uuid ]; then
INSTALL_ID=$(cat /proc/sys/kernel/random/uuid)
else
# Fallback: use timestamp + random
INSTALL_ID="install-$(date +%s)-$RANDOM"
fi
}
track_event() {
if ! telemetry_enabled; then
return 0
fi
local event="$1"
local extra_props="${2:-}"
# Send async (don't block install)
(curl -sS --max-time 5 -X POST "${POSTHOG_HOST}/capture/" \
-H "Content-Type: application/json" \
-d "{
\"api_key\": \"${POSTHOG_API_KEY}\",
\"event\": \"${event}\",
\"distinct_id\": \"${INSTALL_ID}\",
\"properties\": {
\"platform\": \"${PLATFORM:-unknown}\",
\"version\": \"${VERSION:-unknown}\",
\"method\": \"${INSTALL_METHOD:-unknown}\",
\"os\": \"${OS:-unknown}\",
\"arch\": \"${ARCH:-unknown}\",
\"has_node\": \"${HAS_NODE:-false}\"${extra_props}
}
}" > /dev/null 2>&1 &) || true
}
safe_remove_path() {
local target="$1"
if [ -L "$target" ] || [ -f "$target" ]; then
rm -f "$target"
fi
}
write_executable_file() {
local target="$1"
local temp="${target}.tmp.$$"
mkdir -p "$(dirname "$target")"
safe_remove_path "$temp"
cat > "$temp"
chmod +x "$temp"
mv -f "$temp" "$target"
}
install_binary_launcher() {
local target_binary="$1"
write_executable_file "$BIN_DIR/agent-relay" << WRAPPER
#!/usr/bin/env bash
exec "$target_binary" "\$@"
WRAPPER
}
install_node_launcher() {
local target_dir="$1"
write_executable_file "$BIN_DIR/agent-relay" << WRAPPER
#!/usr/bin/env bash
cd "$target_dir" && exec node dist/src/cli/index.js "\$@"
WRAPPER
}
prepend_bin_dir_to_path() {
case ":${PATH:-}:" in
*":$BIN_DIR:"*) ;;
*) export PATH="$BIN_DIR${PATH:+:$PATH}" ;;
esac
}
resolve_command_in_path() {
local command_name="$1"
local path_value="${2:-$PATH}"
PATH="$path_value" command -v "$command_name" 2>/dev/null || true
}
record_standalone_failure() {
local message="$1"
STANDALONE_FAILURE_REASON="$message"
warn "$message"
# The rejected binary only ever existed as a temp file, so any previously
# installed binary and launcher are still intact and are left alone.
}
# Detect OS and architecture
detect_platform() {
OS="$(uname -s)"
ARCH="$(uname -m)"
case "$OS" in
Linux*) OS="linux" ;;
Darwin*) OS="darwin" ;;
*) error "Unsupported OS: $OS" ;;
esac
case "$ARCH" in
x86_64|amd64) ARCH="x64" ;;
arm64|aarch64) ARCH="arm64" ;;
*) error "Unsupported architecture: $ARCH" ;;
esac
PLATFORM="${OS}-${ARCH}"
info "Detected platform: $PLATFORM"
}
# Get latest version from GitHub
get_latest_version() {
if [ "$VERSION" = "latest" ]; then
# Use GitHub token if available (avoids rate limiting)
local auth_header=""
if [ -n "${GITHUB_TOKEN:-}" ]; then
auth_header="-H \"Authorization: token $GITHUB_TOKEN\""
fi
VERSION=$(eval curl -fsSL $auth_header "https://api.github.com/repos/$REPO_RELAY/releases/latest" | sed -n 's/.*"tag_name"[[:space:]]*:[[:space:]]*"\([^"]*\)".*/\1/p')
if [ -z "$VERSION" ]; then
error "Failed to fetch latest version"
fi
fi
# Remove tag prefix (e.g., "openclaw-v3.1.10" -> "3.1.10", "v3.1.10" -> "3.1.10")
VERSION="${VERSION#openclaw-}"
VERSION="${VERSION#v}"
info "Installing version: $VERSION"
}
# Check if Node.js is available
check_node() {
if command -v node &> /dev/null; then
NODE_VERSION=$(node -v | cut -d'v' -f2 | cut -d'.' -f1)
if [ "$NODE_VERSION" -ge 22 ]; then
HAS_NODE=true
info "Node.js $(node -v) detected"
return 0
fi
fi
HAS_NODE=false
return 1
}
# ---------------------------------------------------------------------------
# Atomic, verified binary installation
#
# Rules (see https://github.com/AgentWorkforce/relay/issues/1885):
# * Never write to, sign or smoke-test a live destination path. Everything
# happens on a temp file in the destination directory; the final step is an
# atomic rename. A process running the old binary keeps its old inode.
# * Compare the downloaded bytes with the SHA-256 GitHub publishes per asset.
# * Re-hash after the rename and restore the previous binary on mismatch.
# * Run the broker for real (`init`) before accepting it; `--help` alone
# cannot see a damaged code page.
# ---------------------------------------------------------------------------
TEMP_FILES=""
SMOKE_PID=""
SMOKE_DIR=""
KEEP_PREV=0
DIGEST_VERIFIED=0 # set by verify_asset_digest when the last fetched asset matched its published digest
# One transaction covers the standalone CLI, the launcher and both broker
# copies. TXN_STATE lists the armed components ("cli", "broker"); the install
# commits with a SINGLE assignment (TXN_STATE=""), so an interrupt either rolls
# every component back or finds nothing to roll back, never a mixture.
TXN_STATE=""
TXN_SUFFIX=".prev.$$" # run-unique backup suffix: a stale .prev from an older run is never mistaken for ours
BACKUP_SUFFIX="" # empty = default ".prev"; the transactional installs set TXN_SUFFIX
BROKER_D1=""
BROKER_D2=""
BROKER_PRE1=0
BROKER_PRE2=0
CLI_PRE=0
LAUNCHER_PRE=0
LAUNCHER_BACKUP=""
RELEASE_JSON=""
RELEASE_JSON_STATE="" # "", "ok" or "failed"
register_temp() {
TEMP_FILES="${TEMP_FILES}${TEMP_FILES:+
}$1"
}
cleanup_temp_files() {
local f
if [ -n "$SMOKE_PID" ]; then
kill -9 "$SMOKE_PID" 2>/dev/null || true
SMOKE_PID=""
fi
if [ -n "$SMOKE_DIR" ]; then
rm -rf "$SMOKE_DIR" 2>/dev/null
SMOKE_DIR=""
fi
[ -n "$TEMP_FILES" ] || return 0
while IFS= read -r f; do
[ -n "$f" ] && rm -f "$f" 2>/dev/null
done <<EOT
$TEMP_FILES
EOT
TEMP_FILES=""
return 0
}
# Create a temp file in DIR (same filesystem as the destination, so that a
# rename is atomic). The path is returned in MADE_TEMP, NOT on stdout: it must
# run in the caller's shell so that register_temp updates the TEMP_FILES that
# the EXIT/INT/TERM cleanup reads (a $(...) subshell would lose the entry).
MADE_TEMP=""
make_temp_file() {
local dir="$1"
local label="$2"
MADE_TEMP=""
MADE_TEMP=$(mktemp "$dir/.${label}.XXXXXX") || return 1
register_temp "$MADE_TEMP"
}
# SHA-256 of a file (or of stdin when no file is given). Prints the lowercase
# hex digest; returns 1 when no hashing tool is available.
sha256_of() {
local out=""
if command -v shasum >/dev/null 2>&1; then
out=$(shasum -a 256 "$@" 2>/dev/null) || return 1
elif command -v sha256sum >/dev/null 2>&1; then
out=$(sha256sum "$@" 2>/dev/null) || return 1
elif command -v openssl >/dev/null 2>&1; then
# Output looks like "SHA256(file)= <hex>" or "(stdin)= <hex>"
out=$(openssl dgst -sha256 "$@" 2>/dev/null | awk '{print $NF}') || return 1
else
return 1
fi
out=$(printf '%s' "$out" | awk '{print tolower($1)}')
[ -n "$out" ] || return 1
printf '%s\n' "$out"
}
# Fetch (once) the release metadata for v$VERSION. Sets RELEASE_JSON and
# RELEASE_JSON_STATE in the current shell; never aborts the install.
load_release_metadata() {
[ -z "$RELEASE_JSON_STATE" ] || return 0
local url="https://api.github.com/repos/$REPO_RELAY/releases/tags/v${VERSION}"
local json=""
if [ -n "${GITHUB_TOKEN:-}" ]; then
json=$(curl -fsSL -H "Authorization: token $GITHUB_TOKEN" "$url" 2>/dev/null) || json=""
else
json=$(curl -fsSL "$url" 2>/dev/null) || json=""
fi
if [ -n "$json" ]; then
RELEASE_JSON="$json"
RELEASE_JSON_STATE="ok"
else
RELEASE_JSON=""
RELEASE_JSON_STATE="failed"
fi
return 0
}
# Structural parser (no jq): reads release JSON on stdin and prints the
# lowercase hex sha256 of asset $1. It walks the JSON character by character,
# tracks object/array depth and string state, and only looks at the DIRECT
# properties (name, digest) of each object in the top-level "assets" array, so
# nested objects (uploader, author, ...) with their own "name" keys, braces
# inside strings, and any key order are all handled.
parse_asset_digest_awk() {
awk -v asset="$1" 'BEGIN { RS = "\001" }
{
s = $0; n = length(s)
depth = 0; instr = 0; inassets = 0; ad = 0
for (i = 1; i <= n; i++) {
c = substr(s, i, 1)
if (instr) {
if (c == "\\") { i++; tok = tok substr(s, i, 1); continue }
if (c == "\"") {
instr = 0
j = i + 1
while (j <= n && substr(s, j, 1) ~ /[ \t\r\n]/) j++
if (substr(s, j, 1) == ":") { key[depth] = tok; haskey[depth] = 1 }
else if (haskey[depth]) {
k = key[depth]
if (inassets && depth == ad + 1) {
if (k == "name") cname = tok
else if (k == "digest") cdig = tok
}
haskey[depth] = 0
}
tok = ""
}
else tok = tok c
continue
}
if (c == "\"") { instr = 1; tok = ""; continue }
if (c == "{" || c == "[") {
if (c == "[" && depth == 1 && haskey[1] && key[1] == "assets") { inassets = 1; ad = depth + 1 }
depth++
haskey[depth] = 0
if (inassets && depth == ad + 1) { cname = ""; cdig = "" }
continue
}
if (c == "}" || c == "]") {
if (inassets && c == "}" && depth == ad + 1 && cname == asset && cdig ~ /^sha256:[0-9a-fA-F]+$/) {
d = substr(cdig, 8); print tolower(d); exit
}
if (inassets && c == "]" && depth == ad) inassets = 0
haskey[depth] = 0
depth--
haskey[depth] = 0
continue
}
if (c ~ /[ \t\r\n,:]/) continue
# bare literal (number, true, false, null): value of the current key
haskey[depth] = 0
}
}'
}
parse_asset_digest_jq() {
jq -r --arg n "$1" '[.assets[]? | select(.name == $n) | .digest // empty][0] // empty' 2>/dev/null \
| sed -n 's/^sha256:\([0-9a-fA-F][0-9a-fA-F]*\)$/\1/p' | tr 'A-F' 'a-f'
}
# Print the published sha256 (lowercase hex) of release asset $1, or nothing if
# the release lists no digest for it. Uses jq when installed, otherwise the
# structural awk parser above (both are tested against the same fixtures).
fetch_asset_expected_sha256() {
local asset="$1"
load_release_metadata
[ "$RELEASE_JSON_STATE" = "ok" ] || return 0
if has_command jq; then
printf '%s' "$RELEASE_JSON" | parse_asset_digest_jq "$asset"
else
printf '%s' "$RELEASE_JSON" | parse_asset_digest_awk "$asset"
fi
}
# Compare file $2 with the digest the release publishes for asset $1.
# 0 = verified, 1 = MISMATCH (reject), 2 = could not verify (warned, continue).
verify_asset_digest() {
local asset="$1"
local file="$2"
local expected actual
load_release_metadata
expected=$(fetch_asset_expected_sha256 "$asset")
if [ -z "$expected" ]; then
if [ "$RELEASE_JSON_STATE" = "failed" ]; then
warn "Could not fetch release metadata from the GitHub API (rate limit or network); cannot verify the SHA-256 of $asset. Continuing with the smoke test only."
else
warn "Release v${VERSION} publishes no SHA-256 digest for $asset; cannot verify its integrity. Continuing with the smoke test only."
fi
return 2
fi
if ! actual=$(sha256_of "$file"); then
warn "No SHA-256 tool found (shasum, sha256sum or openssl); cannot verify $asset. Continuing with the smoke test only."
return 2
fi
if [ "$actual" != "$expected" ]; then
warn "SHA-256 mismatch for $asset: expected $expected, got $actual. Refusing to install it."
return 1
fi
info "Verified SHA-256 of $asset ($expected)"
DIGEST_VERIFIED=1
return 0
}
# Download release asset $1 into a fresh temp file in directory $2. $3 is "raw"
# or "gz" (gunzip into a second temp file). On success FETCHED_TMP holds a
# digest-verified, not yet signed file. Returns 0 on success, 1 when the asset
# is unavailable or undecodable, 2 on an integrity failure.
fetch_release_asset() {
local asset="$1"
local dir="$2"
local mode="${3:-raw}"
local url="https://github.com/$REPO_RELAY/releases/download/v${VERSION}/${asset}"
local dl out rc
FETCHED_TMP=""
DIGEST_VERIFIED=0
make_temp_file "$dir" "download" || return 1
dl="$MADE_TEMP"
if ! curl -fsSL "$url" -o "$dl" 2>/dev/null; then
rm -f "$dl"
return 1
fi
if [ "$mode" = "gz" ]; then
# Reject error pages that are not gzip data
if ! head -c 2 "$dl" 2>/dev/null | od -An -tx1 | tr -d ' \n' | grep -q "^1f8b"; then
rm -f "$dl"
return 1
fi
fi
rc=0
verify_asset_digest "$asset" "$dl" || rc=$?
if [ "$rc" -eq 1 ]; then
rm -f "$dl"
return 2
fi
if [ "$mode" = "gz" ]; then
local h_file h_stream
make_temp_file "$dir" "decoded" || { rm -f "$dl"; return 1; }
out="$MADE_TEMP"
if ! gunzip -c "$dl" > "$out" 2>/dev/null; then
rm -f "$dl" "$out"
return 1
fi
# The bytes on disk must equal a clean second decode of the same archive.
h_file=$(sha256_of "$out" || true)
h_stream=$(gunzip -c "$dl" 2>/dev/null | sha256_of || true)
rm -f "$dl"
if [ -n "$h_file" ] && [ "$h_file" != "$h_stream" ]; then
warn "Decompressed $asset differs from a clean decode; refusing to install it."
rm -f "$out"
return 2
fi
dl="$out"
fi
FETCHED_TMP="$dl"
return 0
}
# Run the broker for real: `init` in a throwaway state dir and temp HOME with a
# scrubbed environment (no RELAY_*/AGENT_RELAY_* credentials). It must still be
# alive after a few seconds (or exit 0). Any crash or signal rejects it.
smoke_test_broker() {
# stderr is silenced so bash does not print "Terminated"/"Illegal
# instruction" job notices for the process we deliberately run and kill;
# the outcome is reported through info/warn (stdout).
# Resolve the duration here, outside the silenced call, so an invalid
# AGENT_RELAY_SMOKE_SECONDS warning (stderr) is actually shown.
local seconds
seconds=$(resolve_smoke_seconds)
smoke_test_broker_run "$1" "$seconds" 2>/dev/null
}
# AGENT_RELAY_SMOKE_SECONDS must be a positive integer; anything else (0,
# negative, non-numeric, empty) would skip the wait and let a crashing broker
# pass vacuously, so it falls back to the default with a warning. Clamped to 60.
resolve_smoke_seconds() {
local v="${AGENT_RELAY_SMOKE_SECONDS-}"
if [ -z "$v" ]; then
echo 4
return 0
fi
case "$v" in
*[!0-9]*|0|00*)
warn "Ignoring invalid AGENT_RELAY_SMOKE_SECONDS='$v' (need a positive integer); using 4" >&2
echo 4
;;
*)
if [ "${#v}" -gt 2 ] || [ "$v" -gt 60 ]; then
warn "AGENT_RELAY_SMOKE_SECONDS='$v' is too large; using 60" >&2
echo 60
else
echo "$v"
fi
;;
esac
}
smoke_test_broker_run() {
local bin="$1"
local seconds="$2"
local tmp pid rc=0 i=0 alive=1
tmp=$(mktemp -d "${TMPDIR:-/tmp}/agent-relay-smoke.XXXXXX") || {
warn "smoke test: could not create a temp dir"
return 1
}
SMOKE_DIR="$tmp"
mkdir -p "$tmp/home" "$tmp/state"
# Telemetry: the damaged page from #1885 was reachable from the telemetry
# path, so the smoke test must not opt out on the user's behalf; it only
# forwards an opt-out the user already made.
local optout_a="" optout_b=""
if ! telemetry_enabled; then
optout_a="AGENT_RELAY_TELEMETRY_DISABLED=1"
optout_b="DO_NOT_TRACK=1"
fi
# Run the real fleet-style startup (instance + channels + API port), not
# --local-only: --local-only skips code that the damaged build crashed in.
# The environment is scrubbed (env -i: no RELAY_*/AGENT_RELAY_* credentials)
# and RELAY_BASE_URL points at a closed local port, so the throwaway broker
# cannot create or join any workspace. A clean "failed to initialize
# relaycast session" exit is therefore the expected outcome for a healthy
# binary; a crash (signal) is not.
(
cd "$tmp" || exit 126
exec env -i HOME="$tmp/home" PATH="/usr/bin:/bin" TMPDIR="$tmp" \
RELAY_BASE_URL="http://127.0.0.1:9" \
${optout_a:+"$optout_a"} ${optout_b:+"$optout_b"} \
"$bin" init --instance-name "smoke-$$" --channels general --api-port 0 \
--state-dir "$tmp/state" \
< /dev/null > "$tmp/out.log" 2>&1
) &
pid=$!
SMOKE_PID="$pid"
while [ "$i" -lt "$seconds" ]; do
sleep 1
i=$((i + 1))
if ! kill -0 "$pid" 2>/dev/null; then
alive=0
break
fi
done
# Accepted-state rule: a broker that is still running after the window, or
# exits 0, or stops with the expected clean relaycast error, passes; a signal
# or any other failure rejects. The damaged build from #1885 died with
# SIGILL (132) within about a second in every run, well inside the window,
# and the digest / signature checks run before this, so a late crash or a
# stall is the residual risk this short check cannot cover.
if [ "$alive" -eq 1 ]; then
kill "$pid" 2>/dev/null || true
i=0
while kill -0 "$pid" 2>/dev/null && [ "$i" -lt 5 ]; do
sleep 1
i=$((i + 1))
done
kill -9 "$pid" 2>/dev/null || true
{ wait "$pid"; } 2>/dev/null || true
SMOKE_PID=""
rm -rf "$tmp"
info "Broker smoke test passed (init stayed up ${seconds}s)"
return 0
fi
{ wait "$pid"; } 2>/dev/null || rc=$?
SMOKE_PID=""
if [ "$rc" -eq 0 ]; then
rm -rf "$tmp"
info "Broker smoke test passed (init exited 0)"
return 0
fi
if [ "$rc" -lt 128 ] && grep -q "failed to initialize relaycast session" "$tmp/out.log" 2>/dev/null; then
rm -rf "$tmp"
info "Broker smoke test passed (init ran and stopped cleanly at the unreachable workspace service)"
return 0
fi
local detail=""
if [ "$rc" -gt 128 ]; then
detail=" (signal $((rc - 128)))"
fi
warn "Broker smoke test FAILED: init exited with status $rc${detail}"
tail -n 5 "$tmp/out.log" 2>/dev/null | sed 's/^/ /' || true
rm -rf "$tmp"
return 1
}
# Checks used with install_binary_atomic.
check_broker_binary() {
"$1" --help >/dev/null 2>&1 || { warn "broker binary failed --help"; return 1; }
smoke_test_broker "$1"
}
check_cli_binary() {
local log
log=$(mktemp "${TMPDIR:-/tmp}/agent-relay-verify.XXXXXX") || return 1
if "$1" --version >"$log" 2>&1; then
rm -f "$log"
return 0
fi
CLI_CHECK_OUTPUT=$(head -n 1 "$log" 2>/dev/null || true)
rm -f "$log"
return 1
}
check_help_binary() {
"$1" --help >/dev/null 2>&1
}
# Install prepared temp file $1 (in the destination directory) as $2.
# $3 check function run on the TEMP file (default: none)
# $4 "sign" (default) to strip quarantine / sign the temp file, "nosign" when
# the file was already signed and verified (a plain copy)
# The destination is only touched by one atomic `mv -f`. The previous binary is
# kept as "$2.prev" until the installed bytes are re-hashed and match; on any
# failure it is restored and 1 is returned. The temp file is always consumed.
install_binary_atomic() {
local tmp="$1"
local dest="$2"
local check_fn="${3:-true}"
local sign="${4:-sign}"
local prev="${dest}${BACKUP_SUFFIX:-.prev}"
local had_prev=0 signed_hash installed_hash
chmod +x "$tmp" || { rm -f "$tmp"; return 1; }
if [ "$sign" = "sign" ] && ! prepare_downloaded_binary "$tmp"; then
rm -f "$tmp"
return 1
fi
if ! "$check_fn" "$tmp"; then
warn "Verification of the new binary failed; $dest left untouched"
rm -f "$tmp"
return 1
fi
if ! signed_hash=$(sha256_of "$tmp"); then
signed_hash=""
warn "No SHA-256 tool found; skipping the post-install read-back check for $dest"
fi
local rotated=0
if [ -e "$dest" ] || [ -L "$dest" ]; then
# An existing backup (e.g. from an interrupted earlier upgrade) is only
# touched now that the new binary is verified and about to be committed,
# and it is rotated to .prev.1 rather than deleted.
if [ -e "$prev" ] || [ -L "$prev" ]; then
mv -f "$prev" "${prev}.1" && rotated=1
fi
if [ -L "$dest" ]; then
cp -pP "$dest" "$prev" 2>/dev/null && had_prev=1
else
{ ln "$dest" "$prev" 2>/dev/null || cp -p "$dest" "$prev" 2>/dev/null; } && had_prev=1
fi
if [ "$had_prev" -ne 1 ]; then
warn "Could not keep a backup of $dest; leaving it untouched"
[ "$rotated" -eq 1 ] && mv -f "${prev}.1" "$prev"
rm -f "$tmp"
return 1
fi
fi
if ! mv -f "$tmp" "$dest"; then
warn "Could not move the new binary into place at $dest"
rm -f "$tmp"
if [ "$had_prev" -eq 1 ]; then
rm -f "$prev"
[ "$rotated" -eq 1 ] && mv -f "${prev}.1" "$prev"
fi
return 1
fi
if [ -n "$signed_hash" ]; then
installed_hash=$(sha256_of "$dest" || true)
if [ "$installed_hash" != "$signed_hash" ]; then
warn "Installed $dest does not match the verified file (expected $signed_hash, got ${installed_hash:-unreadable}); restoring the previous binary"
if [ "$had_prev" -eq 1 ]; then
mv -f "$prev" "$dest"
[ "$rotated" -eq 1 ] && mv -f "${prev}.1" "$prev"
else
rm -f "$dest"
fi
return 1
fi
fi
# KEEP_PREV=1 lets a caller (the transactional install) roll back later if a
# dependent step fails; the caller then owns removing the backup.
if [ "$had_prev" -eq 1 ] && [ "${KEEP_PREV:-0}" != "1" ]; then
rm -f "$prev"
fi
return 0
}
# Copy an already verified+signed binary $1 to $2 through the same atomic path.
copy_binary_atomic() {
local src="$1"
local dest="$2"
local tmp src_hash tmp_hash
make_temp_file "$(dirname "$dest")" "copy" || return 1
tmp="$MADE_TEMP"
if ! cp "$src" "$tmp"; then
rm -f "$tmp"
return 1
fi
src_hash=$(sha256_of "$src" || true)
tmp_hash=$(sha256_of "$tmp" || true)
if [ "$src_hash" != "$tmp_hash" ]; then
warn "Copy of $src to $dest is corrupt; not installing it"
rm -f "$tmp"
return 1
fi
install_binary_atomic "$tmp" "$dest" true nosign
}
# Download broker binary (Rust broker for workflow/SDK agent spawning)
# Returns 0 on success, 1 when no binary is available for this platform, and 2
# when a binary was downloaded but REJECTED (integrity or smoke test failure);
# the existing broker is left untouched in both failure cases.
download_broker_binary() {
step "Downloading broker binary..."
local binary_name="agent-relay-broker-${PLATFORM}"
local target_path="$INSTALL_DIR/bin/agent-relay-broker"
local rc=0
mkdir -p "$INSTALL_DIR/bin"
mkdir -p "$BIN_DIR"
fetch_release_asset "$binary_name" "$INSTALL_DIR/bin" raw || rc=$?
if [ "$rc" -eq 1 ]; then
warn "No prebuilt broker binary for $PLATFORM"
return 1
elif [ "$rc" -ne 0 ]; then
warn "broker binary failed integrity verification"
return 2
fi
# Two destinations (INSTALL_DIR and BIN_DIR) commit together as part of the
# install transaction: both keep a run-unique backup until the whole install
# commits, and undo_brokers() restores BOTH (idempotently) on failure or
# interrupt. The transaction is armed before any destination is touched.
local d1="$target_path"
local d2="$BIN_DIR/agent-relay-broker"
BROKER_D1="$d1"; BROKER_D2="$d2"
BROKER_PRE1=0; BROKER_PRE2=0
if [ -e "$d1" ] || [ -L "$d1" ]; then BROKER_PRE1=1; fi
if [ -e "$d2" ] || [ -L "$d2" ]; then BROKER_PRE2=1; fi
local saved_keep="$KEEP_PREV" saved_suffix="$BACKUP_SUFFIX"
TXN_STATE="${TXN_STATE:+$TXN_STATE }broker"
KEEP_PREV=1
BACKUP_SUFFIX="$TXN_SUFFIX"
if install_binary_atomic "$FETCHED_TMP" "$d1" check_broker_binary \
&& copy_binary_atomic "$d1" "$d2"; then
KEEP_PREV="$saved_keep"; BACKUP_SUFFIX="$saved_suffix"
if [ "$TXN_STATE" = "broker" ]; then
# standalone-less (npm) install: this is the whole transaction
TXN_STATE=""
cleanup_broker_backups
fi
success "Downloaded broker binary (workflow agent spawning)"
return 0
fi
KEEP_PREV="$saved_keep"; BACKUP_SUFFIX="$saved_suffix"
warn "Could not install the verified broker into both $INSTALL_DIR/bin and $BIN_DIR; restoring the previous broker"
undo_brokers
if [ "$TXN_STATE" = "broker" ]; then TXN_STATE=""; else TXN_STATE="${TXN_STATE% broker}"; fi
warn "broker binary failed verification"
return 2
}
# The standalone CLI and the broker must be the same version. If the broker was
# rejected after the CLI was replaced, undo the CLI step (transactional
# install) and fail loudly instead of leaving a CLI/broker version skew.
abort_standalone_install() {
local broker="$INSTALL_DIR/bin/agent-relay-broker"
local old_broker="none installed"
if [ -x "$broker" ]; then
old_broker="$( { "$broker" --version 2>/dev/null || true; } | head -n 1)"
[ -n "$old_broker" ] || old_broker="unknown version"
fi
undo_brokers
undo_cli
TXN_STATE=""
error "The v${VERSION} broker was rejected (integrity or smoke test failed), so the v${VERSION} CLI was NOT kept: ${ROLLBACK_OUTCOME}. The existing broker (${old_broker}) is untouched. Nothing was upgraded; re-run the installer, or set AGENT_RELAY_VERSION to a known-good version."
}
# Idempotent undo of one destination: put this run's backup back; a destination
# that did not exist before is removed; otherwise leave it alone.
undo_destination() { # undo_destination <dest> <existed-before: 0|1>
local dest="$1" pre="$2" backup="$1${TXN_SUFFIX}"
if [ -e "$backup" ] || [ -L "$backup" ]; then
mv -f "$backup" "$dest"
elif [ "$pre" -eq 0 ]; then
rm -f "$dest"
fi
}
undo_brokers() {
[ -n "$BROKER_D1" ] || return 0
undo_destination "$BROKER_D1" "$BROKER_PRE1"
undo_destination "$BROKER_D2" "$BROKER_PRE2"
}
cleanup_broker_backups() {
[ -n "$BROKER_D1" ] || return 0
rm -f "${BROKER_D1}${TXN_SUFFIX}" "${BROKER_D2}${TXN_SUFFIX}"
}
# Undo the standalone CLI install: restore (or remove) the CLI binary and the
# launcher independently. Sets ROLLBACK_OUTCOME.
ROLLBACK_OUTCOME=""
undo_cli() {
local cli="$INSTALL_DIR/bin/agent-relay"
local launcher="$BIN_DIR/agent-relay"
undo_destination "$cli" "$CLI_PRE"
if [ "$CLI_PRE" -eq 1 ]; then
ROLLBACK_OUTCOME="the previous CLI ($( { "$cli" --version 2>/dev/null || true; } | head -n 1)) was restored"
else
ROLLBACK_OUTCOME="the new CLI was removed (there was no previous install)"
fi
# A launcher from an earlier npm/source install must survive byte-identical.
if [ -n "$LAUNCHER_BACKUP" ] && [ -e "$LAUNCHER_BACKUP" ]; then
mv -f "$LAUNCHER_BACKUP" "$launcher"
LAUNCHER_BACKUP=""
ROLLBACK_OUTCOME="${ROLLBACK_OUTCOME}; the existing $launcher launcher was restored"
elif [ "$LAUNCHER_PRE" -eq 0 ]; then
rm -f "$launcher"
fi
}
# INT/TERM/HUP: undo whatever is armed, then clean up and exit. TXN_STATE is
# read once, so a commit (TXN_STATE="") either happened before this point
# (nothing is rolled back) or did not (everything is).
handle_signal() {
trap '' INT TERM HUP
local code="$1"
local st="$TXN_STATE"
TXN_STATE=""
case " $st " in *" broker "*) undo_brokers ;; esac
case " $st " in
*" cli "*)
undo_cli
warn "Interrupted: ${ROLLBACK_OUTCOME}."
;;
esac
cleanup_temp_files
trap - EXIT
exit "$code"
}
# Check if a command exists
has_command() {
command -v "$1" &> /dev/null
}
# Prepare a downloaded temp binary (never a live path). On macOS this removes
# the quarantine flag and checks the code signature:
# * The published binaries ship an ad-hoc signature whose page hashes cover
# every page, so `codesign --verify --strict` is an OFFLINE integrity check
# (a zero-filled page fails it). A damaged download is rejected (return 1).
# * A valid shipped signature is kept as is, so the installed bytes equal the
# published bytes. Re-signing is only done for a binary that ships unsigned
# (Apple Silicon refuses to run unsigned code). Re-signing a damaged file is
# exactly what hid the corruption in #1885, so it is never done blindly.
prepare_downloaded_binary() {
local f="$1"
[ "$OS" = "darwin" ] || return 0
if has_command xattr; then
xattr -d com.apple.quarantine "$f" 2>/dev/null || true
fi
if ! has_command codesign; then
# Without codesign neither verification nor a required signature is
# possible. Only a verified published digest can vouch for the bytes.
if [ "$DIGEST_VERIFIED" = "1" ]; then
warn "codesign not found: cannot verify or apply a code signature. Continuing because the download matched its published SHA-256."
return 0
fi
warn "codesign not found and no published SHA-256 was verified: cannot establish the integrity of the downloaded binary. Refusing to install it."
return 1
fi
local out
if out=$(codesign --verify --strict "$f" 2>&1); then
return 0
fi
case "$out" in
*"not signed at all"*)
if codesign --force --sign - "$f" >/dev/null 2>&1; then
return 0
fi
warn "Could not apply an ad-hoc code signature to the downloaded binary; Apple Silicon will not run it unsigned. Refusing to install it."
return 1
;;
esac
warn "Code signature check failed for the downloaded binary (${out}); it is damaged or modified. Refusing to install it."
return 1
}
# Download relay-acp binary for Zed editor integration
download_relay_acp() {
step "Downloading relay-acp binary (Zed editor integration)..."
local binary_name="relay-acp-${PLATFORM}"
local target_path="$BIN_DIR/relay-acp"
local file_size
mkdir -p "$BIN_DIR"
# Try compressed binary first
if has_command gunzip; then
if fetch_release_asset "${binary_name}.gz" "$BIN_DIR" gz; then
if install_binary_atomic "$FETCHED_TMP" "$target_path" check_help_binary; then
success "Downloaded relay-acp binary (Zed ACP bridge)"
return 0
fi
warn "relay-acp binary failed verification, trying uncompressed..."
fi
fi
# Fall back to uncompressed binary
if fetch_release_asset "$binary_name" "$BIN_DIR" raw; then
file_size=$(stat -f%z "$FETCHED_TMP" 2>/dev/null || stat -c%s "$FETCHED_TMP" 2>/dev/null || echo "0")
if [ "$file_size" -gt 1000000 ]; then
if install_binary_atomic "$FETCHED_TMP" "$target_path" check_help_binary; then
success "Downloaded relay-acp binary (Zed ACP bridge)"
return 0
fi
else
rm -f "$FETCHED_TMP"
fi
fi
info "No relay-acp binary available for $PLATFORM"
return 1
}
# Install ACP bridge for Zed editor integration (fallback to npm if binary not available)
install_acp_bridge() {
# Try binary first
if download_relay_acp; then
return 0
fi
# Fall back to npm if Node.js is available
if check_node; then
info "Installing ACP bridge via npm..."
if npm install -g @agent-relay/acp-bridge@"$VERSION" 2>/dev/null || npm install -g @agent-relay/acp-bridge 2>/dev/null; then
success "Installed relay-acp via npm (Zed ACP bridge)"
return 0
fi
fi
warn "relay-acp not available (Zed editor integration won't work)"
return 1
}
# Download with progress indicator
download_with_progress() {
local url="$1"
local output="$2"