-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsetup
More file actions
executable file
Β·725 lines (672 loc) Β· 25.5 KB
/
Copy pathsetup
File metadata and controls
executable file
Β·725 lines (672 loc) Β· 25.5 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
#!/usr/bin/env bash
# The Last Stack β setup / installer
#
# Registers each skill in skills/ into whatever agent harnesses you have
# installed (Claude Code, Codex, Factory/droid, OpenCode). Skills stay in this
# repo; the harness gets a real directory with a symlinked SKILL.md, so a later
# `git pull` updates every installed skill at once.
#
# Usage:
# ./setup # auto-detect harnesses, install where present
# ./setup --host claude # claude | codex | factory | opencode | auto
# ./setup --local # install into ./.claude/skills (project-vendored)
# ./setup --uninstall # remove installed skill links (see bin/last-stack-uninstall)
#
# Re-running is idempotent.
set -e
umask 077
ROOT="$(cd "$(dirname "$0")" && pwd -P)"
resolve_source_root() {
local candidate
for candidate in \
"${LAST_STACK_ROOT:-}" \
"${HOST_TRACK_INSTALL_ROOT:+$HOST_TRACK_INSTALL_ROOT/current}" \
"$HOME/.local/state/last-stack/artifacts/current" \
"$HOME/.last-stack/.artifacts/current" \
"$ROOT" \
"$HOME/.last-stack"
do
[ -n "$candidate" ] || continue
if [ -f "$candidate/VERSION" ] && [ -d "$candidate/skills" ]; then
cd "$candidate" && pwd -P
return 0
fi
done
printf '%s\n' "$ROOT"
}
SOURCE_ROOT="$(resolve_source_root)"
ARTIFACT_INSTALL=0
artifact_roots=()
[ -n "${HOST_TRACK_INSTALL_ROOT:-}" ] && artifact_roots+=("$HOST_TRACK_INSTALL_ROOT")
artifact_roots+=("$HOME/.local/state/last-stack/artifacts" "$HOME/.last-stack/.artifacts")
for artifact_root in "${artifact_roots[@]}"; do
[ -n "$artifact_root" ] || continue
artifact_root="$(cd "$artifact_root" 2>/dev/null && pwd -P || printf '%s\n' "$artifact_root")"
case "$SOURCE_ROOT" in
"$artifact_root"|"$artifact_root"/*) ARTIFACT_INSTALL=1 ;;
esac
done
SKILLS_SRC="$SOURCE_ROOT/skills"
HOOKS_SRC="$SOURCE_ROOT/hooks"
SOURCE_TOP_LOADED=0
SOURCE_TOP=""
SOURCE_WORKTREES=""
HOST="auto"
LOCAL_INSTALL=0
while [ $# -gt 0 ]; do
case "$1" in
--host) HOST="$2"; shift 2 ;;
--host=*) HOST="${1#*=}"; shift ;;
--local) LOCAL_INSTALL=1; shift ;;
--uninstall) exec "$SOURCE_ROOT/bin/last-stack-uninstall" ;;
-h|--help) sed -n '2,18p' "$0"; exit 0 ;;
*) echo "unknown flag: $1" >&2; exit 2 ;;
esac
done
# ββ Windows lacks reliable symlinks without Developer Mode; copy there instead β
IS_WINDOWS=0
case "$(uname -s)" in MINGW*|MSYS*|CYGWIN*|Windows_NT) IS_WINDOWS=1 ;; esac
_link_or_copy() {
# $1 = source file, $2 = destination path
if [ "$IS_WINDOWS" -eq 1 ] || is_transient_last_stack_target "$1"; then
cp -f "$1" "$2"
else
ln -snf "$1" "$2"
fi
}
is_transient_last_stack_target() {
local target="$1"
case "$target" in
"$HOME/.kanban/worktrees"/*|*/.kanban/worktrees/*) return 0 ;;
"$HOME/.fkanban/worktrees"/*|*/.fkanban/worktrees/*) return 0 ;;
esac
if [ "$SOURCE_TOP_LOADED" -eq 0 ]; then
SOURCE_TOP="$(git -C "$SOURCE_ROOT" rev-parse --show-toplevel 2>/dev/null || true)"
if [ -n "$SOURCE_TOP" ]; then
SOURCE_WORKTREES="$(git -C "$SOURCE_TOP" worktree list --porcelain 2>/dev/null | awk '/^worktree /{print substr($0, 10)}')"
fi
SOURCE_TOP_LOADED=1
fi
[ -n "$SOURCE_TOP" ] || return 1
local wt
while IFS= read -r wt; do
[ -n "$wt" ] || continue
[ "$wt" = "$SOURCE_TOP" ] && continue
case "$target" in
"$wt"/*) return 0 ;;
esac
done <<< "$SOURCE_WORKTREES"
return 1
}
assert_safe_skill_links() {
local target_dir="$1"
local link dest abs_dest
[ -d "$target_dir" ] || return 0
find "$target_dir" -type l -print | while IFS= read -r link; do
dest="$(readlink "$link" 2>/dev/null || true)"
[ -n "$dest" ] || continue
case "$dest" in
/*) abs_dest="$dest" ;;
*) abs_dest="$(cd "$(dirname "$link")" && cd "$(dirname "$dest")" && pwd -P)/$(basename "$dest")" ;;
esac
if is_transient_last_stack_target "$abs_dest"; then
echo "refusing transient Last Stack skill link: $link -> $abs_dest" >&2
echo "run setup from or with LAST_STACK_ROOT pointing at the canonical ~/.last-stack checkout" >&2
exit 1
fi
done
}
upsert_pretool_hook() {
local settings="$1"
local matcher="$2"
local command="$3"
local timeout="$4"
local tmp
tmp="$(mktemp)"
jq \
--arg matcher "$matcher" \
--arg command "$command" \
--argjson timeout "$timeout" '
.hooks //= {}
| .hooks.PreToolUse //= []
| .hooks.PreToolUse =
(if any(.hooks.PreToolUse[]?; .matcher == $matcher) then
[.hooks.PreToolUse[] |
if .matcher == $matcher then
.hooks = (((.hooks // []) | map(select(.command != $command))) + [{
type: "command",
command: $command,
timeout: $timeout
}])
else
.
end
]
else
.hooks.PreToolUse + [{
matcher: $matcher,
hooks: [{
type: "command",
command: $command,
timeout: $timeout
}]
}]
end)
' "$settings" > "$tmp"
mv "$tmp" "$settings"
}
remove_pretool_hook_by_script() {
# Strip every PreToolUse command entry whose command references the given
# script name, dropping matcher groups left empty.
local settings="$1"
local script="$2"
local tmp
tmp="$(mktemp)"
jq \
--arg script "$script" '
if (.hooks.PreToolUse // []) == [] then . else
.hooks.PreToolUse =
[.hooks.PreToolUse[]
| .hooks = ((.hooks // []) | map(select((.command // "") | contains($script) | not)))
| select((.hooks | length) > 0)]
end
' "$settings" > "$tmp"
mv "$tmp" "$settings"
}
upsert_permission_allow() {
local settings="$1"
local rule="$2"
local tmp
tmp="$(mktemp)"
jq \
--arg rule "$rule" '
.permissions //= {}
| .permissions.allow //= []
| if any(.permissions.allow[]?; . == $rule) then
.
else
.permissions.allow += [$rule]
end
' "$settings" > "$tmp"
mv "$tmp" "$settings"
}
install_claude_hooks() {
local hooks_dir="$HOME/.claude/hooks"
local settings="$HOME/.claude/settings.json"
mkdir -p "$hooks_dir" "$(dirname "$settings")"
for hook in unsafe-inline-json.sh; do
[ -f "$HOOKS_SRC/$hook" ] || continue
cp -f "$HOOKS_SRC/$hook" "$hooks_dir/$hook"
chmod 700 "$hooks_dir/$hook"
done
if ! command -v jq >/dev/null 2>&1; then
echo " claude hooks: installed scripts; skipped settings.json registration (jq not found)" >&2
return 0
fi
if [ ! -s "$settings" ]; then
printf '{}\n' > "$settings"
fi
# read-before-edit.sh is RETIRED and must never be re-registered here. Its
# deny ended the whole turn, freezing unattended runs mid-task; Tom removed
# it on 2026-07-28 and a later setup run silently re-armed it from this
# function on 2026-07-29. De-register so already-armed machines heal on the
# next run. History: brain papercut-read-before-edit-hook-denied-write-then-edit.
remove_pretool_hook_by_script "$settings" "read-before-edit.sh"
upsert_pretool_hook "$settings" "Bash" \
"$hooks_dir/unsafe-inline-json.sh # keep parsed JSON stdout clean; block brittle inline parsing and merged stderr" \
5
for tool in \
mcp__brain__brain_get \
mcp__brain__brain_search \
mcp__brain__brain_list \
mcp__brain__brain_ask \
mcp__brain__brain_put
do
upsert_permission_allow "$settings" "$tool"
done
echo " claude hooks: unsafe-inline-json.sh (inline parsing + merged JSON stderr guard; read-before-edit retired)"
echo " claude permissions: brain MCP get/search/list/ask/put"
}
# ββ Brain/Kanban wiring: usage instructions + Codex MCP registration ββββββββββ
# Managed block targets:
# Claude Code β ~/.claude/CLAUDE.md
# Codex β ~/.codex/AGENTS.md
# Factory β ~/.factory/AGENTS.md
# OpenCode β ~/.config/opencode/AGENTS.md
BK_START='<!-- last-stack:brain-kanban:start (managed by last-stack setup; edits inside are overwritten) -->'
BK_END='<!-- last-stack:brain-kanban:end -->'
upsert_brain_kanban_block() {
# $1 = harness instructions file (e.g. ~/.claude/CLAUDE.md, ~/.codex/AGENTS.md)
# $2 = label
local file="$1"
local label="$2"
local src="$SOURCE_ROOT/instructions/brain-kanban.md"
[ -f "$src" ] || return 0
mkdir -p "$(dirname "$file")"
[ -f "$file" ] || : > "$file"
local tmp
tmp="$(mktemp)"
# Drop any previous managed block (and trailing blank lines), keep user content.
awk -v s="$BK_START" -v e="$BK_END" '
$0 == s { ib = 1; next }
$0 == e { ib = 0; next }
!ib { lines[++n] = $0 }
END {
while (n > 0 && lines[n] == "") n--
for (i = 1; i <= n; i++) print lines[i]
}
' "$file" > "$tmp"
[ -s "$tmp" ] && echo "" >> "$tmp"
{
printf '%s\n' "$BK_START"
cat "$src"
printf '%s\n' "$BK_END"
} >> "$tmp"
mv "$tmp" "$file"
echo " ${label} instructions: brain-kanban block -> $file"
}
ensure_codex_mcp_servers() {
# brain and kanban are bun shims. GUI-launched MCP hosts (the Codex desktop
# app) spawn servers with a bare PATH that lacks ~/.bun/bin, so without an
# explicit env the server exits 127 and the client reports "transport closed".
local cfg="$HOME/.codex/config.toml"
local mcp_path="$HOME/.bun/bin:$HOME/.local/bin:/opt/homebrew/bin:/usr/local/bin:/usr/bin:/bin:/usr/sbin:/sbin"
local added=()
mkdir -p "$(dirname "$cfg")"
[ -f "$cfg" ] || : > "$cfg"
local name cmd
for name in brain kanban; do
case "$name" in
brain) cmd="$HOME/.bun/bin/brain" ;;
kanban) cmd="$HOME/.local/bin/kanban" ;;
esac
if [ ! -x "$cmd" ]; then
cmd="$(command -v "$name" 2>/dev/null || true)"
fi
if [ -z "$cmd" ]; then
echo " codex mcp: $name CLI not found; skipped its MCP registration" >&2
continue
fi
if ! grep -q "^\[mcp_servers\.$name\]" "$cfg"; then
{
echo ""
echo "[mcp_servers.$name]"
echo "args = [\"mcp\"]"
echo "command = \"$cmd\""
echo "startup_timeout_sec = 120"
} >> "$cfg"
added+=("$name")
fi
if ! grep -q "^\[mcp_servers\.$name\.env\]" "$cfg"; then
{
echo ""
echo "# $name is a bun shim; GUI-spawned MCP servers get a bare PATH (no ~/.bun/bin)"
echo "# and exit 127 -> 'transport closed' without this."
echo "[mcp_servers.$name.env]"
echo "PATH = \"$mcp_path\""
} >> "$cfg"
added+=("$name.env")
fi
done
if [ ${#added[@]} -gt 0 ]; then
echo " codex mcp: added ${added[*]} to $cfg"
else
echo " codex mcp: brain + kanban already registered"
fi
}
install_host_track_shim() {
local src="$SOURCE_ROOT/bin/host-track"
local dest="$HOME/.local/bin/host-track"
local registry_src="$SOURCE_ROOT/config/host-track/apps.json"
local registry_dest="$HOME/.local/share/last-stack/config/host-track/apps.json"
[ -f "$src" ] || return 0
mkdir -p "$(dirname "$dest")"
[ -L "$dest" ] && rm -f "$dest"
_link_or_copy "$src" "$dest"
chmod 700 "$dest" 2>/dev/null || true
if [ -f "$registry_src" ]; then
mkdir -p "$(dirname "$registry_dest")"
cp -f "$registry_src" "$registry_dest"
chmod 600 "$registry_dest" 2>/dev/null || true
fi
echo " host-track: $dest"
}
install_shared_checkout_guards() {
local local_bin="$HOME/.local/bin"
local name src dest
mkdir -p "$local_bin"
for name in last-stack-shared-checkout-guard last-stack-install-shared-checkout-guards; do
src="$SOURCE_ROOT/bin/$name"
dest="$local_bin/$name"
[ -f "$src" ] || continue
[ -L "$dest" ] && rm -f "$dest"
_link_or_copy "$src" "$dest"
chmod 700 "$dest" 2>/dev/null || true
done
if [ -x "$local_bin/last-stack-install-shared-checkout-guards" ]; then
"$local_bin/last-stack-install-shared-checkout-guards" \
"${LAST_STACK_SHARED_WORKSPACE:-$HOME/code/edgevector}" \
|| echo " shared-checkout guards: install failed" >&2
fi
}
xml_escape() {
sed \
-e 's/&/\&/g' \
-e 's/</\</g' \
-e 's/>/\>/g' \
-e 's/"/\"/g' \
-e "s/'/\'/g"
}
host_track_launchd_plist_path() {
printf '%s\n' "${LAST_STACK_HOST_TRACK_PLIST:-$HOME/.last-stack/launchd/com.edgevector.host-track-refresh.plist}"
}
host_track_real_home_matches() {
local real_home
real_home="$(eval "printf '%s' ~$(id -un)" 2>/dev/null || true)"
[ -n "$real_home" ] && [ "$HOME" = "$real_home" ]
}
host_track_refresh_watch_paths() {
local registry="$SOURCE_ROOT/config/host-track/apps.json"
local raw path git_dir
[ -f "$registry" ] || return 0
command -v jq >/dev/null 2>&1 || return 0
command -v git >/dev/null 2>&1 || return 0
jq -r '.apps[]? | select(.gate == "forgejo") | .host_track // empty' "$registry" |
while IFS= read -r raw; do
[ -n "$raw" ] || continue
case "$raw" in
'$HOME') path="$HOME" ;;
'$HOME'/*) path="$HOME/${raw#\$HOME/}" ;;
"~") path="$HOME" ;;
"~"/*) path="$HOME/${raw#~/}" ;;
*) path="$raw" ;;
esac
[ -d "$path" ] || continue
git_dir="$(git -C "$path" rev-parse --git-dir 2>/dev/null || true)"
[ -n "$git_dir" ] || continue
case "$git_dir" in
/*) ;;
*) git_dir="$path/$git_dir" ;;
esac
if [ -e "$git_dir/FETCH_HEAD" ]; then
printf '%s\n' "$git_dir/FETCH_HEAD"
fi
done
}
write_host_track_refresh_agent_plist() {
local plist="$1"
local program="$HOME/.local/bin/host-track"
local log_dir="$HOME/Library/Logs/last-stack"
local watch_paths
mkdir -p "$(dirname "$plist")" "$log_dir"
watch_paths="$(host_track_refresh_watch_paths | awk '!seen[$0]++')"
{
cat <<EOF
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<dict>
<key>Label</key>
<string>com.edgevector.host-track-refresh</string>
<key>ProgramArguments</key>
<array>
<string>$(printf '%s' "$program" | xml_escape)</string>
<string>refresh</string>
<string>--force-if-stale</string>
<string>--all</string>
</array>
<key>EnvironmentVariables</key>
<dict>
<key>PATH</key>
<string>$HOME/.local/bin:$HOME/.bun/bin:/opt/homebrew/bin:/usr/local/bin:/usr/bin:/bin:/usr/sbin:/sbin</string>
</dict>
<key>StartInterval</key>
<integer>1200</integer>
<key>StandardOutPath</key>
<string>$(printf '%s' "$log_dir/host-track-refresh.log" | xml_escape)</string>
<key>StandardErrorPath</key>
<string>$(printf '%s' "$log_dir/host-track-refresh.err.log" | xml_escape)</string>
EOF
if [ -n "$watch_paths" ]; then
cat <<'EOF'
<key>WatchPaths</key>
<array>
EOF
printf '%s\n' "$watch_paths" | while IFS= read -r watch_path; do
[ -n "$watch_path" ] || continue
printf ' <string>%s</string>\n' "$(printf '%s' "$watch_path" | xml_escape)"
done
cat <<'EOF'
</array>
EOF
fi
cat <<'EOF'
</dict>
</plist>
EOF
} > "$plist"
chmod 600 "$plist" 2>/dev/null || true
}
install_host_track_refresh_agent() {
[ "$(uname -s)" = "Darwin" ] || return 0
local plist label="com.edgevector.host-track-refresh"
plist="$(host_track_launchd_plist_path)"
write_host_track_refresh_agent_plist "$plist"
echo " host-track refresh agent: $plist"
if ! host_track_real_home_matches; then
echo " host-track refresh agent: launchctl skipped for non-login HOME"
return 0
fi
command -v launchctl >/dev/null 2>&1 || {
echo " host-track refresh agent: launchctl not found; plist written only" >&2
return 0
}
launchctl bootout "gui/$(id -u)/$label" >/dev/null 2>&1 || true
if launchctl bootstrap "gui/$(id -u)" "$plist" >/dev/null 2>&1; then
launchctl enable "gui/$(id -u)/$label" >/dev/null 2>&1 || true
echo " host-track refresh agent: loaded $label"
else
echo " host-track refresh agent: launchctl bootstrap failed; plist written only" >&2
fi
}
install_routines_cli() {
local routines_bin=""
if [ "${LAST_STACK_SKIP_ROUTINES_INSTALL:-0}" = "1" ]; then
echo " routines CLI: skipped (LAST_STACK_SKIP_ROUTINES_INSTALL=1)"
return 0
fi
routines_bin="$(command -v routines 2>/dev/null || true)"
[ -x "$routines_bin" ] || routines_bin="$HOME/.local/bin/routines"
if [ -x "$routines_bin" ] && "$routines_bin" --help >/dev/null 2>&1; then
echo " routines CLI: already installed ($routines_bin)"
return 0
fi
# Test/sandbox HOME values must never clone into or relink the login user's
# real app tree. Production callers can override only for a deliberate
# hermetic install proof.
if ! host_track_real_home_matches && [ "${LAST_STACK_ROUTINES_FORCE_INSTALL:-0}" != "1" ]; then
echo " routines CLI: install skipped for non-login HOME"
return 0
fi
local installer
installer="${LAST_STACK_ROUTINES_INSTALLER:-$SOURCE_ROOT/bin/last-stack-install-routines}"
[ -x "$installer" ] || {
echo "routines CLI installer is missing or not executable: $installer" >&2
return 1
}
echo " routines CLI: installing ..."
"$installer"
}
# ββ Register every skills/<name>/SKILL.md into a target skills dir ββββββββββββ
register_into() {
local skills_dir="$1"
local label="$2"
local linked=()
mkdir -p "$skills_dir"
for skill_dir in "$SKILLS_SRC"/*/; do
[ -f "$skill_dir/SKILL.md" ] || continue
local dir_name skill_name target
dir_name="$(basename "$skill_dir")"
[ "$dir_name" = "node_modules" ] && continue
# Prefer the frontmatter `name:` so the slash-command matches the skill name.
skill_name="$(grep -m1 '^name:' "$skill_dir/SKILL.md" 2>/dev/null | sed 's/^name:[[:space:]]*//' | tr -d '[:space:]')"
[ -z "$skill_name" ] && skill_name="$dir_name"
target="$skills_dir/$skill_name"
# Replace stale dir-symlinks from older layouts.
[ -L "$target" ] && rm -f "$target"
mkdir -p "$target"
[ -L "$target/SKILL.md" ] && rm -f "$target/SKILL.md"
_link_or_copy "${skill_dir%/}/SKILL.md" "$target/SKILL.md"
# Mirror any sidecar files (helper scripts, references/) so skills with more
# than a SKILL.md work fully from the source repo β not just their SKILL.md.
local base="${skill_dir%/}"
find "$base" -type f ! -name SKILL.md | while IFS= read -r src; do
rel="${src#"$base"/}"
dest="$target/$rel"
mkdir -p "$(dirname "$dest")"
[ -L "$dest" ] && rm -f "$dest"
_link_or_copy "$src" "$dest"
done
assert_safe_skill_links "$target"
linked+=("$skill_name")
done
if [ ${#linked[@]} -gt 0 ]; then
echo " ${label}: ${linked[*]}"
else
echo " ${label}: (no skills found)" >&2
fi
}
# ββ --local: vendor into the current project's .claude/skills βββββββββββββββββ
if [ "$LOCAL_INSTALL" -eq 1 ]; then
echo "Installing The Last Stack (v$(cat "$SOURCE_ROOT/VERSION")) into ./.claude/skills ..."
register_into "$(pwd)/.claude/skills" "claude (project)"
echo "Done."
exit 0
fi
# ββ Detect which harnesses are present ββββββββββββββββββββββββββββββββββββββββ
INSTALL_CLAUDE=0; INSTALL_CODEX=0; INSTALL_FACTORY=0; INSTALL_OPENCODE=0
case "$HOST" in
claude) INSTALL_CLAUDE=1 ;;
codex) INSTALL_CODEX=1 ;;
factory) INSTALL_FACTORY=1 ;;
opencode) INSTALL_OPENCODE=1 ;;
auto)
{ command -v claude >/dev/null 2>&1 || [ -d "$HOME/.claude" ]; } && INSTALL_CLAUDE=1
{ command -v codex >/dev/null 2>&1 || [ -d "$HOME/.codex" ]; } && INSTALL_CODEX=1
{ command -v droid >/dev/null 2>&1 || [ -d "$HOME/.factory" ]; } && INSTALL_FACTORY=1
{ command -v opencode >/dev/null 2>&1 || [ -d "$HOME/.config/opencode" ]; } && INSTALL_OPENCODE=1
# Nothing detected β default to Claude Code.
if [ $((INSTALL_CLAUDE+INSTALL_CODEX+INSTALL_FACTORY+INSTALL_OPENCODE)) -eq 0 ]; then
INSTALL_CLAUDE=1
fi
;;
*) echo "unknown --host: $HOST (use claude|codex|factory|opencode|auto)" >&2; exit 2 ;;
esac
echo "Installing The Last Stack (v$(cat "$SOURCE_ROOT/VERSION")) ..."
[ "$INSTALL_CLAUDE" -eq 1 ] && register_into "$HOME/.claude/skills" "claude"
[ "$INSTALL_CLAUDE" -eq 1 ] && install_claude_hooks
[ "$INSTALL_CLAUDE" -eq 1 ] && upsert_brain_kanban_block "$HOME/.claude/CLAUDE.md" "claude"
[ "$INSTALL_CODEX" -eq 1 ] && register_into "$HOME/.codex/skills" "codex"
[ "$INSTALL_CODEX" -eq 1 ] && upsert_brain_kanban_block "$HOME/.codex/AGENTS.md" "codex"
[ "$INSTALL_CODEX" -eq 1 ] && ensure_codex_mcp_servers
[ "$INSTALL_FACTORY" -eq 1 ] && register_into "$HOME/.factory/skills" "factory"
[ "$INSTALL_FACTORY" -eq 1 ] && upsert_brain_kanban_block "$HOME/.factory/AGENTS.md" "factory"
[ "$INSTALL_OPENCODE" -eq 1 ] && register_into "$HOME/.config/opencode/skills" "opencode"
[ "$INSTALL_OPENCODE" -eq 1 ] && upsert_brain_kanban_block "$HOME/.config/opencode/AGENTS.md" "opencode"
install_host_track_shim
install_routines_cli
install_shared_checkout_guards
install_host_track_refresh_agent
# Zero-LLM board closeout (merged PR/CR β done) β survives low-credit watch pause.
# Point LaunchAgents at the stable public root, not SOURCE_ROOT's version hash.
if [ -x "$SOURCE_ROOT/bin/last-stack-board-closeout-install" ]; then
LAST_STACK_PUBLIC_ROOT="${LAST_STACK_PUBLIC_ROOT:-$HOME/.last-stack}" \
"$SOURCE_ROOT/bin/last-stack-board-closeout-install" install || \
echo " board-closeout LaunchAgent: install failed (non-fatal)" >&2
fi
# Hourly factory health (bands + ra notify).
if [ -x "$SOURCE_ROOT/bin/last-stack-factory-health-install" ]; then
LAST_STACK_PUBLIC_ROOT="${LAST_STACK_PUBLIC_ROOT:-$HOME/.last-stack}" \
"$SOURCE_ROOT/bin/last-stack-factory-health-install" install || \
echo " factory-health LaunchAgent: install failed (non-fatal)" >&2
fi
# Seed missing registry TOMLs only. Existing ~/.routines/registry entries are
# the canonical live registry and are not rewritten on host-track refresh.
if [ -x "$SOURCE_ROOT/bin/last-stack-feature-prove-routine" ]; then
"$SOURCE_ROOT/bin/last-stack-feature-prove-routine" || \
echo " feature-prove registry: install failed (non-fatal)" >&2
fi
if [ -x "$SOURCE_ROOT/bin/last-stack-why-stopped-routine" ]; then
"$SOURCE_ROOT/bin/last-stack-why-stopped-routine" || \
echo " why-stopped registry: install failed (non-fatal)" >&2
fi
if [ -x "$SOURCE_ROOT/bin/last-stack-lastdb-ops-offenders-routine" ]; then
"$SOURCE_ROOT/bin/last-stack-lastdb-ops-offenders-routine" || \
echo " lastdb-ops-offenders registry: install failed (non-fatal)" >&2
fi
if [ -x "$SOURCE_ROOT/bin/last-stack-fleet-performance-routine" ]; then
"$SOURCE_ROOT/bin/last-stack-fleet-performance-routine" || \
echo " fleet-performance registry: install failed (non-fatal)" >&2
fi
# Zero-LLM self-upgrade healer (every 30m) β checkout installs only.
# Artifact runtime (one rule): host-track refresh agent is the only upgrader.
if [ -x "$SOURCE_ROOT/bin/last-stack-self-upgrade-install" ]; then
if [ "$ARTIFACT_INSTALL" -eq 1 ]; then
"$SOURCE_ROOT/bin/last-stack-self-upgrade-install" uninstall || \
echo " last-stack-self-upgrade LaunchAgent: uninstall failed (non-fatal)" >&2
else
"$SOURCE_ROOT/bin/last-stack-self-upgrade-install" install || \
echo " last-stack-self-upgrade LaunchAgent: install failed (non-fatal)" >&2
fi
fi
# One rule: when this setup is running from the Host Track artifact, ensure
# ~/.last-stack/{bin,skills,routines,...} are compatibility symlinks into
# .artifacts/current β never a second mutable copy agents can dirty.
# Nested call from last-stack-activate-artifact-layout sets SKIP=1.
if [ "$ARTIFACT_INSTALL" -eq 1 ] && \
[ "${LAST_STACK_SKIP_ARTIFACT_LAYOUT:-0}" != "1" ] && \
[ -x "$SOURCE_ROOT/bin/last-stack-activate-artifact-layout" ]; then
compat_root="${LAST_STACK_COMPAT_ROOT:-$HOME/.last-stack}"
need_layout=0
if [ ! -L "$compat_root/bin" ]; then
need_layout=1
else
link="$(readlink "$compat_root/bin" 2>/dev/null || true)"
case "$link" in
*/.artifacts/current/bin|*/.artifacts/current/bin/) ;;
*) need_layout=1 ;;
esac
fi
if [ "$need_layout" -eq 1 ]; then
echo " activating artifact compatibility layout under $compat_root ..."
# Always allow replacing a residual git-owned ~/.last-stack when setup is
# installing from the Host Track artifact (the live stack is not a checkout).
LAST_STACK_SKIP_ARTIFACT_LAYOUT=1 \
LAST_STACK_ARTIFACT_LAYOUT_ALLOW_GIT_WORKTREE=1 \
"$SOURCE_ROOT/bin/last-stack-activate-artifact-layout" || \
echo " artifact layout activation failed (non-fatal this run)" >&2
fi
fi
# ββ Guard against foreign installers (e.g. gstack) stomping our skill links ββββ
# Another installer that ships a same-named skill (gstack shipped a `diagram`
# skill colliding with our hand-drawn one) silently re-points that SKILL.md at
# its own tree. Registering above already re-points our links, but verify + emit
# any collisions so re-running setup AFTER gstack setup provably restores us.
guard="$SOURCE_ROOT/bin/last-stack-verify-skill-links"
if [ -x "$guard" ]; then
guard_dirs=()
[ "$INSTALL_CLAUDE" -eq 1 ] && guard_dirs+=("$HOME/.claude/skills")
[ "$INSTALL_CODEX" -eq 1 ] && guard_dirs+=("$HOME/.codex/skills")
[ "$INSTALL_FACTORY" -eq 1 ] && guard_dirs+=("$HOME/.factory/skills")
[ "$INSTALL_OPENCODE" -eq 1 ] && guard_dirs+=("$HOME/.config/opencode/skills")
[ ${#guard_dirs[@]} -gt 0 ] && "$guard" --quiet "${guard_dirs[@]}" || true
fi
echo
if [ "$ARTIFACT_INSTALL" -eq 1 ]; then
echo "Done. Upgrade later with: host-track refresh last-stack"
else
echo "Done. Upgrade later with: cd \"$SOURCE_ROOT\" && git pull && ./setup"
fi
echo " (run this AFTER any gstack ./setup β it re-points Last Stack skill links"
echo " that a colliding gstack skill name would otherwise stomp)"
echo "Uninstall with: \"$SOURCE_ROOT/setup\" --uninstall"