-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathinstall.sh
More file actions
executable file
·2626 lines (2294 loc) · 86.3 KB
/
Copy pathinstall.sh
File metadata and controls
executable file
·2626 lines (2294 loc) · 86.3 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
#!/usr/bin/env bash
# Spine installer — AI coding setup for Cursor, Claude Code, Codex, and OpenCode.
# https://github.com/kenoxa/spine
#
# Usage:
# curl -fsSL https://raw.githubusercontent.com/kenoxa/spine/main/install.sh | bash
#
# Or inspect first:
# curl -fsSL https://raw.githubusercontent.com/kenoxa/spine/main/install.sh -o install.sh
# less install.sh
# bash install.sh
set -euo pipefail
# --- Constants ---
REPO="kenoxa/spine"
BRANCH="main"
GLOBAL_SKILLS=(
"obra/superpowers -s brainstorming"
"nicobailon/visual-explainer -s visual-explainer"
"jeffallan/claude-skills -s security-reviewer"
"anthropics/claude-code -s frontend-design"
"wshobson/agents -s wcag-audit-patterns"
"softaworks/agent-toolkit -s reducing-entropy"
"mcollina/skills -s typescript-magician"
"trailofbits/skills -s fp-check"
"mattpocock/skills -s ubiquitous-language -s tdd"
"GoogleChrome/modern-web-guidance -s modern-web-guidance"
)
# Pins the npx CLI version invoked inside modern-web-guidance's SKILL.md body.
# Upstream is early-preview and churns daily — pin to a known-good version.
MODERN_WEB_GUIDANCE_VERSION="0.0.169"
# MCP servers previously installed by Spine — removed on next run.
# Add server names here when replacing or dropping an MCP server.
RETIRED_MCP_SERVERS=()
# Agent names previously used by Spine — cleaned up on next run.
# Add names here when renaming an agent to ensure cross-provider cleanup.
RETIRED_AGENT_NAMES=("worker" "second-opinion" "qwen-code" "github-copilot")
# Skill names previously used by Spine — cleaned up on next run.
# Add names here when renaming a skill to ensure cross-provider cleanup.
RETIRED_SKILL_NAMES=("do-analyze" "do-consult" "run-queue")
# --- Terminal UI ---
# Capability detection: colors always (unless NO_COLOR), cursor movement stricter
_C_RED='\033[0;31m' _C_GREEN='\033[0;32m' _C_YELLOW='\033[0;33m'
_C_BLUE='\033[0;34m' _C_DIM='\033[2m' _C_BOLD='\033[1m'
_C_RESET='\033[0m'
# shellcheck disable=SC2034
[ -n "${NO_COLOR:-}" ] && _C_RED='' _C_GREEN='' _C_YELLOW='' _C_BLUE='' _C_DIM='' _C_BOLD='' _C_RESET=''
# Cursor movement: only on real TTY, not dumb terminal, not CI, not NO_COLOR
_UI_CAN_MOVE=false
[ -t 2 ] && [ "${TERM:-dumb}" != "dumb" ] && [ -z "${NO_COLOR:-}" ] && [ -z "${CI:-}" ] && _UI_CAN_MOVE=true
# Live section state
_UI_LIVE_LINES=0
_UI_WARNINGS=()
_UI_LIVE_FAILURES=0
# Per-tool feature accumulator (replaces tool_add)
_TOOL_FEATURES=""
_TOOL_MCP=0
# Global counters for final summary
_TOTAL_TOOLS=0
_TOTAL_DEPS=0
_TOTAL_DEPS_INSTALLED=0
_TOTAL_HOOKS=0
_SPINE_SKILL_COUNT=0
_GLOBAL_SKILL_COUNT=0
_ERROR_COUNT=0
# --- UI functions ---
warn() {
if [ "$_UI_LIVE_LINES" -gt 0 ]; then
_UI_WARNINGS+=("$*")
else
printf " ${_C_YELLOW}⚠${_C_RESET} %s\n" "$*" >&2
fi
}
error() {
_ERROR_COUNT=$((_ERROR_COUNT + 1))
if [ "$_UI_LIVE_LINES" -gt 0 ]; then
_UI_WARNINGS+=("ERROR: $*")
else
printf " ${_C_RED}✗${_C_RESET} %s\n" "$*" >&2
fi
}
_ui_step_n=0
_ui_step_total=0
ui_init() { _ui_step_total="$1"; }
ui_step() {
_ui_step_n=$((_ui_step_n + 1))
printf "\n${_C_DIM}[%d/%d]${_C_RESET} %s\n" "$_ui_step_n" "$_ui_step_total" "$*" >&2
}
ui_ok() {
if [ -n "${2:-}" ]; then
printf " ${_C_GREEN}✓${_C_RESET} %s ${_C_DIM}%s${_C_RESET}\n" "$1" "$2" >&2
else
printf " ${_C_GREEN}✓${_C_RESET} %s\n" "$1" >&2
fi
}
ui_fail() {
if [ -n "${2:-}" ]; then
printf " ${_C_RED}✗${_C_RESET} %s ${_C_DIM}%s${_C_RESET}\n" "$1" "$2" >&2
else
printf " ${_C_RED}✗${_C_RESET} %s\n" "$1" >&2
fi
}
ui_live_start() {
_UI_LIVE_LINES=0
_UI_WARNINGS=()
_UI_LIVE_FAILURES=0
if $_UI_CAN_MOVE && [ -n "${1:-}" ]; then
printf " %b▸%b %s\n" "${_C_BLUE}" "${_C_RESET}" "$1" >&2
_UI_LIVE_LINES=1
fi
}
ui_live_item() {
if $_UI_CAN_MOVE; then
printf " %b▸ %s%b\n" "${_C_DIM}" "$*" "${_C_RESET}" >&2
_UI_LIVE_LINES=$((_UI_LIVE_LINES + 1))
fi
}
ui_live_done() {
local summary="$1" detail="${2:-}"
# Track failures for collapse summary
if [[ "${detail}" == *failed* ]]; then
_UI_LIVE_FAILURES=$((_UI_LIVE_FAILURES + 1))
fi
if $_UI_CAN_MOVE; then
if [[ "${detail}" == *failed* ]]; then
printf "\033[1A\r\033[K %b⚠%b %b%s" "${_C_YELLOW}" "${_C_RESET}" "${_C_DIM}" "$summary" >&2
else
printf "\033[1A\r\033[K %b✓%b %b%s" "${_C_GREEN}" "${_C_RESET}" "${_C_DIM}" "$summary" >&2
fi
[ -n "$detail" ] && printf " %s" "$detail" >&2
printf '%b\n' "${_C_RESET}" >&2
fi
}
_ui_erase_live() {
if $_UI_CAN_MOVE && [ "$_UI_LIVE_LINES" -gt 0 ]; then
printf "\033[%dA\033[J" "$_UI_LIVE_LINES" >&2
fi
_UI_LIVE_LINES=0
}
_ui_flush_warnings() {
local w
for w in "${_UI_WARNINGS[@]+"${_UI_WARNINGS[@]}"}"; do
if [[ "$w" == ERROR:* ]]; then
printf " ${_C_RED}✗${_C_RESET} %s\n" "${w#ERROR: }" >&2
else
printf " ${_C_YELLOW}⚠${_C_RESET} %s\n" "$w" >&2
fi
done
_UI_WARNINGS=()
}
ui_live_collapse() {
_ui_erase_live
if [ "$_UI_LIVE_FAILURES" -gt 0 ]; then
# Yellow summary when some subtasks failed
local detail="${2:-}"
[ -n "$detail" ] && detail="$detail · ${_UI_LIVE_FAILURES} failed"
printf " ${_C_YELLOW}⚠${_C_RESET} %s ${_C_DIM}%s${_C_RESET}\n" "$1" "$detail" >&2
else
ui_ok "$@"
fi
_ui_flush_warnings
}
ui_live_fail() {
_ui_erase_live
ui_fail "$@"
_ui_flush_warnings
}
ui_done() {
printf '\n%b─────────────────────────────────────%b\n' "${_C_DIM}" "${_C_RESET}" >&2
printf '%b✓%b %b%s%b\n\n' "${_C_GREEN}" "${_C_RESET}" "${_C_BOLD}" "$*" "${_C_RESET}" >&2
}
# Feature accumulator for per-tool tracking
_feature() {
if [[ "$1" == MCP:* ]]; then
_TOOL_MCP=$((_TOOL_MCP + 1))
else
_TOOL_FEATURES="${_TOOL_FEATURES:+${_TOOL_FEATURES} · }$1"
fi
}
_tool_summary() {
local s="$_TOOL_FEATURES"
[ "$_TOOL_MCP" -gt 0 ] && s="${s:+$s · }MCP×$_TOOL_MCP"
printf '%s' "$s"
}
# Cleanup trap: erase live section on abnormal exit + clean up downloaded source
_SPINE_CLEANUP_DIR=""
_ui_cleanup() {
if [ "$_UI_LIVE_LINES" -gt 0 ] && $_UI_CAN_MOVE; then
printf "\033[%dA\033[J" "$_UI_LIVE_LINES" >&2
printf ' %b✗%b Interrupted\n' "${_C_RED}" "${_C_RESET}" >&2
_UI_LIVE_LINES=0
fi
[ -n "${_SPINE_CLEANUP_DIR:-}" ] && rm -rf "$_SPINE_CLEANUP_DIR" || true
}
trap '_ui_cleanup' EXIT
# --- Core helpers ---
# Run a command silently; on failure show captured output then return 1.
# During live sections: suppress output (callers handle failure via warn).
quiet() {
local out rc
# Close stdin so wrapped CLIs can never block on an interactive prompt
# (UI live lines would hide the prompt, producing a silent hang).
# 60s cap bounds runaway network stalls.
out=$(timeout 60 "$@" </dev/null 2>&1)
rc=$?
if [ "$rc" -ne 0 ]; then
if [ "$_UI_LIVE_LINES" -eq 0 ]; then echo "$out" >&2; fi
return "$rc"
fi
}
# --- Backup helper ---
backup_if_exists() {
local file="$1"
if [ -e "$file" ] || [ -L "$file" ]; then
# Skip backup for spine-managed files — we own them and will overwrite
if [ -f "$file" ] && head -3 "$file" | grep -q 'spine:managed' 2>/dev/null; then
return 0
fi
cp -L "$file" "${file}.bak" 2>/dev/null || true
fi
}
# Compare semver: returns 0 if $1 >= $2
version_gte() {
[ "$(printf '%s\n' "$1" "$2" | sort -V | head -1)" = "$2" ]
}
# --- Download source into temp dir ---
download_source() {
local tmpdir
tmpdir=$(mktemp -d)
if command -v git &>/dev/null && quiet git clone --depth 1 --branch "$BRANCH" "https://github.com/$REPO.git" "$tmpdir/spine"; then
echo "$tmpdir/spine"
elif command -v curl &>/dev/null && command -v tar &>/dev/null; then
curl -fsSL "https://github.com/$REPO/archive/refs/heads/$BRANCH.tar.gz" | tar -xz -C "$tmpdir"
echo "$tmpdir/spine-$BRANCH"
else
error "Neither git nor curl+tar found. Install one of them and retry."
rm -rf "$tmpdir"
return 1
fi
}
# --- Dependency detection + install ---
prepend_path_if_dir() {
[ -d "$1" ] || return 0
case ":$PATH:" in
*":$1:"*) ;;
*) PATH="$1:$PATH" ;;
esac
}
ensure_installer_tool_paths() {
prepend_path_if_dir "$HOME/.cargo/bin"
prepend_path_if_dir "/home/linuxbrew/.linuxbrew/bin"
prepend_path_if_dir "/home/linuxbrew/.linuxbrew/opt/node/bin"
prepend_path_if_dir "/usr/local/bin"
prepend_path_if_dir "/usr/local/opt/node/bin"
prepend_path_if_dir "/opt/homebrew/bin"
prepend_path_if_dir "/opt/homebrew/opt/node/bin"
prepend_path_if_dir "$HOME/.local/bin"
prepend_path_if_dir "$HOME/.bun/bin"
export PATH
}
# Check whether a python command reports Python >= 3.9.
python39_plus() {
local python_cmd="$1"
local version major minor
command -v "$python_cmd" >/dev/null 2>&1 || return 1
version=$("$python_cmd" -c 'import sys; print(f"{sys.version_info[0]}.{sys.version_info[1]}")' 2>/dev/null) || return 1
major=${version%%.*}
minor=${version#*.}
[ "$major" -gt 3 ] || { [ "$major" -eq 3 ] && [ "$minor" -ge 9 ]; }
}
has_brew() { command -v brew >/dev/null 2>&1; }
node_present() {
command -v node >/dev/null 2>&1 && return 0
command -v nodejs >/dev/null 2>&1 && return 0
[ -x /opt/homebrew/opt/node/bin/node ] && return 0
[ -x /usr/local/opt/node/bin/node ] && return 0
[ -x /home/linuxbrew/.linuxbrew/opt/node/bin/node ] && return 0
return 1
}
# Check if a tool binary is on PATH.
# Handles formula-to-binary name differences (e.g., ripgrep → rg).
dep_present() {
case "$1" in
python3) python39_plus python3 || python39_plus python ;;
ripgrep) command -v rg >/dev/null 2>&1 ;;
ast-grep) command -v ast-grep >/dev/null 2>&1 || command -v sg >/dev/null 2>&1 ;;
node) node_present ;;
coreutils) command -v gtimeout >/dev/null 2>&1 || command -v timeout >/dev/null 2>&1 ;;
*) command -v "$1" >/dev/null 2>&1 ;;
esac
}
brew_formula_name() {
case "$1" in
python3) echo "python" ;;
tokenizer) echo "zahidcakici/tap/tokenizer" ;;
*) echo "$1" ;;
esac
}
# Install a Homebrew formula if not already on PATH or installed via brew.
brew_install_if_missing() {
local formula="$1"
local brew_formula
brew_formula=$(brew_formula_name "$formula")
dep_present "$formula" && return 0
brew list --formula "$brew_formula" >/dev/null 2>&1 && return 0
quiet brew install "$brew_formula" </dev/null
}
# Install probe (direct binary, no Homebrew formula)
install_probe() {
local PROBE_REPO="probelabs/probe"
local INSTALL_DIR="$HOME/.local/bin"
local MANIFEST="$HOME/.config/spine/tool-versions"
# Platform guard — mac only
local os; os="$(uname -s)"
if [ "$os" != "Darwin" ]; then
warn "probe: unsupported OS $os — skipping"
return 0
fi
local arch; arch="$(uname -m)"
local PLATFORM
case "$arch" in
arm64|aarch64) PLATFORM="aarch64-apple-darwin" ;;
x86_64) PLATFORM="x86_64-apple-darwin" ;;
*) warn "probe: unsupported architecture $arch — skipping"; return 0 ;;
esac
mkdir -p "$INSTALL_DIR"
# Get latest release tag — gh CLI primary, curl fallback
local latest_tag=""
if command -v gh &>/dev/null && gh auth status &>/dev/null; then
latest_tag=$(gh release view --repo "$PROBE_REPO" --json tagName --jq '.tagName' 2>/dev/null) || true
fi
if [ -z "$latest_tag" ]; then
local api_response
api_response=$(curl -sS "https://api.github.com/repos/$PROBE_REPO/releases/latest" 2>/dev/null) || true
if [ -n "$api_response" ]; then
latest_tag=$(echo "$api_response" | grep '"tag_name":' | sed -E 's/.*"([^"]+)".*/\1/')
fi
fi
if [ -z "$latest_tag" ]; then
warn "probe: could not determine latest version — skipping"
return 0
fi
# Version skip — check manifest + binary existence
local installed_tag
installed_tag=$(grep "^probe=" "$MANIFEST" 2>/dev/null | cut -d= -f2 | head -1) || true
if [ "${installed_tag:-}" = "$latest_tag" ] && [ -x "$INSTALL_DIR/probe" ]; then
return 0
fi
# Download
local tmpdir
tmpdir=$(mktemp -d)
local download_ok=false
if command -v gh &>/dev/null && gh auth status &>/dev/null; then
if quiet gh release download "$latest_tag" --repo "$PROBE_REPO" \
--pattern "probe-*-${PLATFORM}.tar.gz" \
--pattern "probe-*-${PLATFORM}.tar.gz.sha256" \
--dir "$tmpdir"; then
download_ok=true
fi
fi
if ! $download_ok; then
# Discover asset URLs from release API
local release_json asset_url sha_url
release_json=$(curl -sS "https://api.github.com/repos/$PROBE_REPO/releases/tags/$latest_tag" 2>/dev/null) || true
if [ -n "$release_json" ]; then
asset_url=$(echo "$release_json" | grep "browser_download_url" | grep "${PLATFORM}.tar.gz\"" | grep -v '.sha256"' | sed -E 's/.*"(https[^"]+)".*/\1/' | head -1)
sha_url=$(echo "$release_json" | grep "browser_download_url" | grep "${PLATFORM}.tar.gz.sha256\"" | sed -E 's/.*"(https[^"]+)".*/\1/' | head -1)
fi
if [ -n "${asset_url:-}" ] && [ -n "${sha_url:-}" ]; then
if curl -fsSL -o "$tmpdir/$(basename "$asset_url")" "$asset_url" && \
curl -fsSL -o "$tmpdir/$(basename "$sha_url")" "$sha_url"; then
download_ok=true
fi
fi
fi
if ! $download_ok; then
warn "probe: download failed — skipping"
rm -rf "$tmpdir"
return 0
fi
# Checksum verification
if ! (cd "$tmpdir" && shasum -a 256 -c ./*.sha256 >/dev/null 2>&1); then
warn "probe: checksum verification failed — skipping"
rm -rf "$tmpdir"
return 0
fi
# Extract and install
tar -xzf "$tmpdir"/probe-*-"${PLATFORM}".tar.gz -C "$tmpdir"
local extracted
extracted=$(find "$tmpdir" -name probe -type f -not -name '*.gz' | head -1)
if [ -z "$extracted" ]; then
warn "probe: binary not found in archive — skipping"
rm -rf "$tmpdir"
return 0
fi
mv "$extracted" "$INSTALL_DIR/probe"
chmod +x "$INSTALL_DIR/probe"
# Write manifest — atomic tmpfile+mv
local manifest_tmp
manifest_tmp=$(mktemp)
[ -f "$MANIFEST" ] && grep -v "^probe=" "$MANIFEST" > "$manifest_tmp" || true
echo "probe=$latest_tag" >> "$manifest_tmp"
mv "$manifest_tmp" "$MANIFEST"
# Migration: remove old probe from /usr/local/bin
if [ -f /usr/local/bin/probe ]; then
if [ -O /usr/local/bin/probe ]; then
rm /usr/local/bin/probe
else
warn "Old probe at /usr/local/bin/probe not user-owned — remove manually: sudo rm /usr/local/bin/probe"
fi
fi
rm -rf "$tmpdir"
ui_ok "probe" "$latest_tag"
}
# Compose a one-line progress string from the dev-browser/Playwright install log.
# Pure: reads the log, holds no state. Playwright prints `NN% of XXX.X MiB` lines.
_provision_line() {
local logf="$1" elapsed="$2" slow_after="$3" attempt="$4" max="$5"
local pct mmss activity note="" retry=""
pct=$(grep -oE '[0-9]+% of [0-9.]+ [KMG]i?B' "$logf" 2>/dev/null | tail -1 || true)
mmss=$(printf '%d:%02d' $((elapsed / 60)) $((elapsed % 60)))
if [ -z "$pct" ]; then
activity="provisioning browser"
elif [ "${pct%%%*}" = "100" ]; then
activity="extracting browser" # download done; Playwright is unpacking (silent)
else
activity="downloading Chromium ${pct}"
fi
[ "$elapsed" -ge "$slow_after" ] && note=" — taking longer than expected"
[ "$attempt" -gt 1 ] && retry=" (retry ${attempt}/${max})"
printf '%s · %s%s%s' "$activity" "$mmss" "$note" "$retry"
}
# Erase the in-place provisioning line (TTY only).
_provision_clear() {
$_UI_CAN_MOVE && printf "\r\033[K" >&2 || true
}
# Kill a process and ALL its descendants, deepest first. The Playwright download
# runs three levels below `dev-browser install` (npm → playwright → oopDownload),
# and the deepest child holds the registry lock — killing only the parent orphans
# it and blocks the next attempt. Walk descendants while the tree is still intact.
_kill_tree() {
local p="$1" child
for child in $(pgrep -P "$p" 2>/dev/null); do
_kill_tree "$child"
done
kill "$p" 2>/dev/null || true
}
# Provision Playwright Chromium + dev-browser runtime assets. The slowest install
# step: a cold cache downloads ~160MB+ of browser binaries. Stream live progress,
# escalate to a "taking longer" notice past slow_after, and retry a stalled download
# (no log growth for stall_secs) so a dropped connection can't wedge the installer.
# Returns: 0 = provisioned (work done), 2 = nothing to do (all cached), 1 = failed.
provision_browser_assets() {
local bin="$1"
local logf; logf=$(mktemp)
local stall_secs=120 slow_after=45 poll=2 print_every=15
local max_attempts=3 attempt=0 rc=1 stalled=false
while [ "$attempt" -lt "$max_attempts" ]; do
attempt=$((attempt + 1)); stalled=false
: > "$logf"
"$bin" install </dev/null >"$logf" 2>&1 &
local pid=$!
local start=$SECONDS last_size=0 last_change=$SECONDS last_print=0
while kill -0 "$pid" 2>/dev/null; do
sleep "$poll"
local now=$SECONDS elapsed=$((SECONDS - start)) size
size=$(wc -c <"$logf" 2>/dev/null || echo 0)
if [ "$size" -ne "$last_size" ]; then
last_size=$size; last_change=$now
elif [ $((now - last_change)) -ge "$stall_secs" ]; then
stalled=true
_kill_tree "$pid"
break
fi
local line; line=$(_provision_line "$logf" "$elapsed" "$slow_after" "$attempt" "$max_attempts")
if $_UI_CAN_MOVE; then
printf "\r\033[K ${_C_BLUE}▸${_C_RESET} dev-browser ${_C_DIM}%s${_C_RESET}" "$line" >&2
elif [ $((elapsed - last_print)) -ge "$print_every" ]; then
last_print=$elapsed
printf " dev-browser: %s\n" "$line" >&2
fi
done
set +e; wait "$pid" 2>/dev/null; local wrc=$?; set -e
if $stalled; then
_provision_clear
if [ "$attempt" -lt "$max_attempts" ]; then
warn "dev-browser: download stalled — retrying (${attempt}/${max_attempts})"
fi
continue
fi
rc=$wrc
break
done
_provision_clear
if [ "$rc" -ne 0 ]; then
warn "dev-browser: Chromium install failed after ${attempt} attempt(s) — run 'dev-browser install' manually"
tail -8 "$logf" >&2 2>/dev/null || true
rm -f "$logf"
return 1
fi
local did_work=2
if grep -q "Downloading" "$logf" 2>/dev/null; then did_work=0; fi
rm -f "$logf"
return "$did_work"
}
# Install dev-browser (direct binary, no Homebrew formula)
install_dev_browser() {
local DEV_BROWSER_REPO="SawyerHood/dev-browser"
local INSTALL_DIR="$HOME/.local/bin"
local MANIFEST="$HOME/.config/spine/tool-versions"
local os; os="$(uname -s)"
local arch; arch="$(uname -m)"
local ASSET_NAME
case "${os}-${arch}" in
Darwin-arm64|Darwin-aarch64) ASSET_NAME="dev-browser-darwin-arm64" ;;
Darwin-x86_64) ASSET_NAME="dev-browser-darwin-x64" ;;
Linux-x86_64) ASSET_NAME="dev-browser-linux-x64" ;;
Linux-arm64|Linux-aarch64) ASSET_NAME="dev-browser-linux-arm64" ;;
*) warn "dev-browser: unsupported platform ${os}-${arch} — skipping"; return 0 ;;
esac
mkdir -p "$INSTALL_DIR"
# Get latest release tag — gh CLI primary, curl fallback
local latest_tag=""
if command -v gh &>/dev/null && gh auth status &>/dev/null; then
latest_tag=$(gh release view --repo "$DEV_BROWSER_REPO" --json tagName --jq '.tagName' 2>/dev/null) || true
fi
if [ -z "$latest_tag" ]; then
local api_response
api_response=$(curl -sS "https://api.github.com/repos/$DEV_BROWSER_REPO/releases/latest" 2>/dev/null) || true
if [ -n "$api_response" ]; then
latest_tag=$(echo "$api_response" | grep '"tag_name":' | sed -E 's/.*"([^"]+)".*/\1/')
fi
fi
if [ -z "$latest_tag" ]; then
warn "dev-browser: could not determine latest version — skipping"
return 0
fi
# Version skip — check manifest + binary existence. Runtime install still runs
# below because it repairs missing Playwright browsers and runtime files.
local installed_tag
installed_tag=$(grep "^dev-browser=" "$MANIFEST" 2>/dev/null | cut -d= -f2 | head -1) || true
local binary_changed=false
if ! { [ "${installed_tag:-}" = "$latest_tag" ] && [ -x "$INSTALL_DIR/dev-browser" ]; }; then
# Download binary directly (no archive — single binary asset)
local tmpfile
tmpfile=$(mktemp)
local download_ok=false
if command -v gh &>/dev/null && gh auth status &>/dev/null; then
if quiet gh release download "$latest_tag" --repo "$DEV_BROWSER_REPO" \
--pattern "$ASSET_NAME" \
--output "$tmpfile" --clobber; then
download_ok=true
fi
fi
if ! $download_ok; then
local download_url="https://github.com/$DEV_BROWSER_REPO/releases/download/$latest_tag/$ASSET_NAME"
if curl -fsSL -o "$tmpfile" "$download_url"; then
download_ok=true
fi
fi
if ! $download_ok; then
warn "dev-browser: download failed — skipping"
rm -f "$tmpfile"
return 0
fi
mv "$tmpfile" "$INSTALL_DIR/dev-browser"
chmod +x "$INSTALL_DIR/dev-browser"
binary_changed=true
fi
# Install/repair Playwright Chromium + dev-browser runtime. First run pulls
# ~160MB+ of browser binaries; stream progress and retry a stalled download so a
# dropped connection can't wedge the installer (see install.sh git history).
local provision_rc=0
provision_browser_assets "$INSTALL_DIR/dev-browser" || provision_rc=$?
if $binary_changed; then
# Write manifest — atomic tmpfile+mv
local manifest_tmp
manifest_tmp=$(mktemp)
[ -f "$MANIFEST" ] && grep -v "^dev-browser=" "$MANIFEST" > "$manifest_tmp" || true
echo "dev-browser=$latest_tag" >> "$manifest_tmp"
mv "$manifest_tmp" "$MANIFEST"
fi
# One status line. Show ✓ when the binary changed or browsers were provisioned;
# stay silent when everything was already cached (provision_rc=2, binary unchanged).
local tag="$latest_tag"
$binary_changed || tag="${installed_tag:-$latest_tag}"
case "$provision_rc" in
0) ui_ok "dev-browser" "$tag" ;;
2) $binary_changed && ui_ok "dev-browser" "$tag" || true ;;
*) ui_fail "dev-browser" "Chromium incomplete — run 'dev-browser install' manually" ;;
esac
}
# Ensure system deps are available. Attempts brew install on macOS; prints hints otherwise.
ensure_system_deps() {
local os missing=()
os="$(uname -s)"
# Installed tools Spine manages — keep machine-keyed and sync docs when changing these.
local -a installed_tools=(
git
jq
node
python3
uv
ast-grep
bun
coreutils
fd
ni
ripgrep
sd
shellcheck
shfmt
yq
tokenizer
rtk
)
local use_brew=false
if has_brew; then
use_brew=true
elif [ "$os" = "Darwin" ]; then
warn "Homebrew not found — install it from https://brew.sh"
fi
# Direct binary installs to ~/.local/bin (no Homebrew formula)
install_probe
install_dev_browser
# Collect missing deps (brew-managed tools only)
for dep in "${installed_tools[@]}"; do
dep_present "$dep" || missing+=("$dep")
done
# +2 for probe and dev-browser (managed separately above)
_TOTAL_DEPS=$(( ${#installed_tools[@]} + 2 ))
if [ ${#missing[@]} -eq 0 ]; then
ui_ok "All found" "${_TOTAL_DEPS} deps"
return 0
fi
if $use_brew; then
ui_live_start "Installing packages"
local installed_names=""
for dep in "${missing[@]}"; do
ui_live_item "$dep"
if brew_install_if_missing "$dep"; then
_TOTAL_DEPS_INSTALLED=$((_TOTAL_DEPS_INSTALLED + 1))
ui_live_done "$dep"
case "$dep" in
ripgrep) installed_names="${installed_names:+$installed_names · }rg" ;;
ast-grep) installed_names="${installed_names:+$installed_names · }sg" ;;
*) installed_names="${installed_names:+$installed_names · }$dep" ;;
esac
else
ui_live_done "$dep" "failed"
fi
done
ui_live_collapse "Packages installed" "$installed_names"
else
warn "Missing tools: ${missing[*]}"
if [ "$os" = "Darwin" ]; then
local brew_missing=()
for dep in "${missing[@]}"; do
brew_missing+=("$(brew_formula_name "$dep")")
done
printf " After installing Homebrew, run:\n" >&2
printf " brew install %s\n" "${brew_missing[*]}" >&2
else
printf " Install via your package manager, e.g.:\n" >&2
printf " sudo apt install %s\n" "${missing[*]}" >&2
fi
fi
}
# --- Tool detection ---
detect_tools() {
local tools=()
# Cursor: config dir is sufficient (no standalone CLI binary)
[ -d "$HOME/.cursor" ] && tools+=("cursor")
# Claude Code / Codex / OpenCode: require CLI binary on PATH
for tool in claude codex opencode; do
if command -v "$tool" >/dev/null 2>&1; then
tools+=("$tool")
elif [ -d "$HOME/.$tool" ]; then
warn "$tool: config directory exists but CLI not found on PATH — skipping"
fi
done
if [ ${#tools[@]} -eq 0 ]; then
warn "No AI coding tools found (cursor, claude, codex, opencode)"
warn "Install at least one, then re-run this installer"
fi
echo "${tools[@]}"
}
# --- Central directory setup ---
setup_central_dir() {
local src="$1"
local spine_dir="$HOME/.config/spine"
mkdir -p "$spine_dir/agents"
mkdir -p "$spine_dir/logs"
# Copy guardrails
backup_if_exists "$spine_dir/SPINE.md"
cp "$src/SPINE.md" "$spine_dir/SPINE.md"
# Create empty AGENTS.md for user customizations (never overwritten)
[ -f "$spine_dir/AGENTS.md" ] || touch "$spine_dir/AGENTS.md"
# Always sync .env.example; seed .env from it if absent
if [ -f "$src/env.example" ]; then
cp "$src/env.example" "$spine_dir/.env.example"
if [ ! -f "$spine_dir/.env" ]; then
cp "$spine_dir/.env.example" "$spine_dir/.env"
ui_ok "Created .env" "edit ~/.config/spine/.env to configure"
fi
fi
# Copy agents
for agent in "$src/agents/"*.md; do
[ -f "$agent" ] || continue
cp "$agent" "$spine_dir/agents/"
done
# Remove stale agents no longer in source
for existing in "$spine_dir/agents/"*.md; do
[ -f "$existing" ] || continue
local name
name=$(basename "$existing")
if [ ! -f "$src/agents/$name" ]; then
backup_if_exists "$existing"
rm "$existing"
fi
done
# shellcheck disable=SC2088 # display string, not path expansion
ui_ok "Central dir" "~/.config/spine/"
}
# --- Hook file setup ---
# Copy hook files from source to ~/.config/spine/hooks/ with shebang rewriting.
# .sh hooks: #!/bin/sh → #!/path/to/_env.sh (env bootstrap via shebang)
# .ts hooks: shebang → #!/path/to/_ts.sh (runtime resolver)
# Helper scripts (_env.sh, _ts.sh, etc.) keep original shebangs.
# Subdirectories: e.g. hooks/inject-types/ (modules imported by inject-types-on-read.ts) are copied wholesale.
setup_hooks() {
local src="$1"
local spine_dir="$HOME/.config/spine"
local count=0
mkdir -p "$spine_dir/hooks"
for hook in "$src/hooks/"*.sh "$src/hooks/"*.ts "$src/hooks/"*.prompt; do
[ -f "$hook" ] || continue
local hook_name
hook_name=$(basename "$hook")
cp "$hook" "$spine_dir/hooks/$hook_name"
count=$((count + 1))
# .prompt files are plain text — don't make executable
case "$hook_name" in
*.prompt) ;;
*) chmod +x "$spine_dir/hooks/$hook_name" ;;
esac
# Rewrite shebangs at install time — makes hooks directly executable
# Skip _env.sh, _ts.sh, _nlx.sh, _project.sh themselves (helper scripts keep original shebangs)
case "$hook_name" in
_*) ;; # skip helper scripts
*.sh)
if head -1 "$spine_dir/hooks/$hook_name" | grep -q '^#!/bin/sh'; then
local tmp_hook
tmp_hook=$(mktemp)
echo "#!$spine_dir/hooks/_env.sh" > "$tmp_hook"
tail -n +2 "$spine_dir/hooks/$hook_name" >> "$tmp_hook"
mv "$tmp_hook" "$spine_dir/hooks/$hook_name"
chmod +x "$spine_dir/hooks/$hook_name"
fi
;;
*.ts)
local tmp_hook
tmp_hook=$(mktemp)
echo "#!$spine_dir/hooks/_ts.sh" > "$tmp_hook"
# Strip existing shebang if present
if head -1 "$spine_dir/hooks/$hook_name" | grep -q '^#!'; then
tail -n +2 "$spine_dir/hooks/$hook_name" >> "$tmp_hook"
else
cat "$spine_dir/hooks/$hook_name" >> "$tmp_hook"
fi
mv "$tmp_hook" "$spine_dir/hooks/$hook_name"
chmod +x "$spine_dir/hooks/$hook_name"
;;
esac
done
# TS support modules (not counted in hook total — only top-level *.sh/*.ts/*.prompt are "hooks")
if [ -d "$src/hooks/inject-types" ]; then
rm -rf "$spine_dir/hooks/inject-types"
cp -R "$src/hooks/inject-types" "$spine_dir/hooks/inject-types"
fi
# Post-install smoke test: verify _env.sh restores tool access
if [ -f "$spine_dir/hooks/_env.sh" ]; then
SPINE_ENV_VERIFY=1 sh "$spine_dir/hooks/_env.sh" true 2>&1 | while IFS= read -r line; do
case "$line" in
*"NOT FOUND"*) warn "$line" ;;
esac
done
fi
# shellcheck disable=SC2088 # display string, not path expansion
ui_ok "Hooks" "$count files → ~/.config/spine/hooks/"
}
# --- Hook capability matrix ---
# Which hooks fire on which provider events. Used by generators to silently omit unsupported hooks.
# Providers: claude, codex, cursor, opencode
# Events: SessionStart, PreToolUse, PostToolUse, PreCompact, Stop
#
# Codex PostToolUse is Bash-only — inject-types-on-read and check-on-edit deferred (TODO.md).
# OpenCode uses in-process TS plugin — shell hooks delegated via execFileSync.
hook_supports() {
local hook="$1" event="$2" provider="$3"
case "$hook:$event:$provider" in
inject-agents-md:SessionStart:claude) return 0 ;; # only Claude — others load AGENTS.md natively
inject-compact-essentials:SessionStart:claude) return 0 ;;
guard-shell:PreToolUse:claude) return 0 ;;
guard-shell:PreToolUse:codex) return 0 ;;
guard-shell:PreToolUse:cursor) return 0 ;;
guard-shell:PreToolUse:opencode) return 0 ;;
guard-read-large:PreToolUse:claude) return 0 ;;
guard-read-large:PreToolUse:codex) return 0 ;;
guard-read-large:PreToolUse:cursor) return 0 ;;
guard-read-large:PreToolUse:opencode) return 0 ;;
inject-types-on-read:PostToolUse:claude) return 0 ;;
inject-types-on-read:PostToolUse:cursor) return 0 ;;
inject-types-on-read:PostToolUse:opencode) return 0 ;;
check-on-edit:PostToolUse:claude) return 0 ;;
check-on-edit:PostToolUse:cursor) return 0 ;;
check-on-edit:PostToolUse:opencode) return 0 ;;
pre-compact:PreCompact:claude) return 0 ;;
pre-compact:PreCompact:cursor) return 0 ;;
*) return 1 ;;
esac
}
# Count hooks supported for a provider. Sets _TOOL_HOOKS.
_count_provider_hooks() {
local provider="$1"
_TOOL_HOOKS=0
local hook event
for hook in inject-agents-md inject-compact-essentials guard-shell guard-read-large inject-types-on-read check-on-edit pre-compact; do
for event in SessionStart PreToolUse PostToolUse PreCompact; do
if hook_supports "$hook" "$event" "$provider" 2>/dev/null; then
_TOOL_HOOKS=$((_TOOL_HOOKS + 1))
fi
done
done
}
# --- Hook config generation ---
# Generate Claude Code hooks in settings.json (fallback when plugin not available).
# Uses spine:managed strip-and-append pattern.
# Usage: generate_claude_hooks <spine-dir>
generate_claude_hooks() {
local spine_dir="$1"
local hooks_dir="$spine_dir/hooks"
local settings="$HOME/.claude/settings.json"
[ -d "$hooks_dir" ] || return 0
command -v jq &>/dev/null || { warn "jq not found — cannot generate Claude hook config"; return 0; }
[ -f "$settings" ] || echo '{}' > "$settings"
backup_if_exists "$settings"
# Build hook entries using absolute paths (resolved at install time)
local tmp
tmp=$(mktemp)
# Start from existing settings, strip any spine-managed hooks
# Identified by spine/hooks path in hook commands
jq '
.hooks //= {} |
.hooks |= with_entries(
.value |= map(select(
(.hooks // []) | all(.command // "" | test("spine/hooks") | not)
))
)
' "$settings" > "$tmp" 2>/dev/null || cp "$settings" "$tmp"
# SessionStart hooks
local ss_hooks="[]"
if hook_supports inject-agents-md SessionStart claude; then
ss_hooks=$(echo "$ss_hooks" | jq --arg cmd "$hooks_dir/inject-agents-md.sh" \
'. + [{"hooks":[{"type":"command","command":$cmd}]}]')
fi
if hook_supports inject-compact-essentials SessionStart claude; then
ss_hooks=$(echo "$ss_hooks" | jq --arg cmd "$hooks_dir/inject-compact-essentials.sh" \
'. + [{"matcher":"compact","hooks":[{"type":"command","command":$cmd}]}]')
fi
# PreToolUse hooks
local ptu_hooks="[]"
if hook_supports guard-shell PreToolUse claude; then
ptu_hooks=$(echo "$ptu_hooks" | jq --arg cmd "$hooks_dir/guard-shell.sh" \
'. + [{"matcher":"Bash","hooks":[{"type":"command","command":$cmd}]}]')
fi
if hook_supports guard-read-large PreToolUse claude; then
ptu_hooks=$(echo "$ptu_hooks" | jq --arg cmd "$hooks_dir/guard-read-large.sh" \
'. + [{"matcher":"Read","hooks":[{"type":"command","command":$cmd,"timeout":10}]}]')
fi