Repository navigation
Expand file tree
/
Copy pathinstall.sh
More file actions
executable file
·1333 lines (1252 loc) · 63.2 KB
/
Copy pathinstall.sh
File metadata and controls
executable file
·1333 lines (1252 loc) · 63.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
# dwarves-kit installer
# Merges hooks into ~/.claude/settings.json, symlinks commands and skills.
# Idempotent: safe to re-run.
#
# v1.1: Fixed jq merge (concat arrays, don't deduplicate by matcher).
# Added --uninstall flag. Backs up settings.json before modifying.
#
# v1.2 (ID-277 SG-04): layered install. The spine (safety-gate, ship-gate,
# spec-drift-guard, secrets-guard, commit-format, anti-rationalization) is always
# wired; everything else is an opt-in module via `--with <a,b,c>`, recorded in a
# `kit.toml [modules]` manifest. A re-run is additive (never un-wires a previously
# wired hook); `--prune --with <modules>` is the explicit trim path.
#
# v1.3: one module table (lib/config/modules.tsv) feeds this script and lib/adopt.sh. A FRESH
# install wires the spine plus the table's `core` modules; an existing install keeps its set.
# When the Claude Code plugin is installed the script refuses to also wire hooks (`--force-both`
# overrides), using the same detector as /kit:onboard (lib/onboard-detect.sh).
set -euo pipefail
# Fail fast on missing prerequisites (gauntlet round-1 finding: an unguarded
# jq call at the settings-merge step crashed MID-install on a jq-less machine;
# the front door must refuse before any write happens).
for prereq in jq git; do
if ! command -v "${prereq}" >/dev/null 2>&1; then
echo "dwarves-kit install requires '${prereq}' (docs/reference/install.md: Requirements)." >&2
echo "Install it first (brew/apt, or a static binary on PATH), then re-run." >&2
exit 1
fi
done
KIT_DIR="$(cd "$(dirname "$0")" && pwd)"
CLAUDE_DIR="${CLAUDE_DIR:-$HOME/.claude}" # overridable for fixture installs (SPEC-066)
SETTINGS_FILE="$CLAUDE_DIR/settings.json"
BACKUP_DIR="$CLAUDE_DIR/backups"
# Claude Code mods shipped in integrations/claude-code/<name>/. One list drives the install, the
# plugin-compat install and the uninstall; a mod's runtime parts are copied, its tests stay behind.
KIT_MOD_BASE="integrations/claude-code"
KIT_MODS="board-pane secrets-veil memory-lens loop-detective context-inspector"
KIT_MOD_PARTS=".claude-plugin hooks src types LICENSE VENDORED.md"
# Implementation playbooks referenced by agents/, commands/, hooks/, or lib/,
# plus sibling playbooks they point to (threat-modeling.md -> red-team-engagement.md).
# Out-of-place installs copy only these files instead of unreferenced playbooks.
KIT_IMPL_PLAYBOOKS="cloudflare.md coding-hygiene.md exploratory-testing.md financial-data-handling.md frontend-design-engineering.md red-team-engagement.md security.md test-case-design.md testing-strategy.md threat-modeling.md"
# kit_drop_mod_dirs <path>... -- remove each path from env.CLAUDE_CODE_PLUGIN_DIRS in settings.json
# (every other entry stays) and drop the key when it ends empty.
kit_drop_mod_dirs() {
[ -f "$SETTINGS_FILE" ] || return 0
local drop cleaned
drop=$(printf '%s\n' "$@" | jq -R . | jq -s . 2>/dev/null) || return 0
cleaned=$(jq --argjson d "$drop" '
if (.env.CLAUDE_CODE_PLUGIN_DIRS // null) == null then .
else
(.env.CLAUDE_CODE_PLUGIN_DIRS | split(":") | map(select(length > 0 and (. as $e | $d | index($e) | not)))) as $rest
| if ($rest | length) == 0 then del(.env.CLAUDE_CODE_PLUGIN_DIRS)
else .env.CLAUDE_CODE_PLUGIN_DIRS = ($rest | join(":")) end
end
' "$SETTINGS_FILE" 2>/dev/null || true)
[ -n "$cleaned" ] && echo "$cleaned" | jq '.' > "$SETTINGS_FILE"
return 0
}
# kit_mod_skip_dup <name> -- when another entry of env.CLAUDE_CODE_PLUGIN_DIRS already loads a plugin
# called <name> (its plugin.json name, else the folder name; a leading ~ is HOME), print one [skip]
# line, drop the kit's own entries for <name> so it never loads twice, and return 0. Else return 1.
kit_mod_skip_dup() {
local name="$1" own_copy="$CLAUDE_DIR/dwarves-kit/$KIT_MOD_BASE/$1" own_checkout="$KIT_DIR/$KIT_MOD_BASE/$1" entry resolved found
[ -f "$SETTINGS_FILE" ] || return 1
while IFS= read -r entry; do
[ -n "$entry" ] || continue
if [ "$entry" = "$own_copy" ] || [ "$entry" = "$own_checkout" ]; then continue; fi
case "$entry" in \~|\~/*) resolved="$HOME${entry#\~}" ;; *) resolved="$entry" ;; esac
resolved="${resolved%/}"
found=$(jq -r '.name // empty' "$resolved/.claude-plugin/plugin.json" 2>/dev/null || true)
[ -n "$found" ] || found="${resolved##*/}"
if [ "$found" = "$name" ]; then
echo "[skip] $name already loaded from $entry"
kit_drop_mod_dirs "$own_copy" "$own_checkout"
return 0
fi
done < <(jq -r '(.env.CLAUDE_CODE_PLUGIN_DIRS // "") | split(":")[]' "$SETTINGS_FILE" 2>/dev/null)
return 1
}
# ============================================================
# USAGE (--help / -h before any write; an unknown flag reuses it, exit 64)
# ============================================================
kit_usage() {
cat <<'USAGE'
usage: bash install.sh [--with <a,b,c>] [--prune] [--no-mods] [--force-both]
bash install.sh --uninstall
bash install.sh --help
--with <a,b,c> enable opt-in modules (also --with=a,b,c); additive on re-run
--prune with --with: trim the enabled set to exactly the named modules
--no-mods skip registering the Claude Code mods in settings.json
--force-both wire hooks even when the Claude Code plugin is installed
--uninstall remove kit hooks, commands, skills (must be the first argument)
-h, --help print this usage and exit 0
Installs into $CLAUDE_DIR (default ~/.claude) and ~/.local/bin. Idempotent.
USAGE
}
for _arg in "$@"; do
case "$_arg" in
-h|--help) kit_usage; exit 0 ;;
esac
done
unset _arg
# ============================================================
# UNINSTALL
# ============================================================
if [ "${1:-}" = "--uninstall" ]; then
echo "=== dwarves-kit uninstaller ==="
# Remove command symlinks
for CMD_FILE in "$KIT_DIR/commands/"*.md; do
CMD_NAME=$(basename "$CMD_FILE")
LINK="$CLAUDE_DIR/commands/$CMD_NAME"
if [ -L "$LINK" ]; then
rm "$LINK"
echo "[ok] Removed command: /${CMD_NAME%.md}"
fi
done
# Remove skill links pointing into the kit; never remove links or folders pointing elsewhere.
for SKILL_FILE in "$KIT_DIR/.agents/skills/"*/SKILL.md; do
[ -f "$SKILL_FILE" ] || continue
SKILL_NAME="$(basename "$(dirname "$SKILL_FILE")")"
SKILL_SRC="$(cd -P "$KIT_DIR/.agents/skills/$SKILL_NAME" && pwd)"
OLD_COPY="$CLAUDE_DIR/skills/$SKILL_NAME"
if [ -d "$OLD_COPY" ] && [ ! -L "$OLD_COPY" ] && [ "$(cd -P "$OLD_COPY" && pwd)" != "$SKILL_SRC" ] \
&& [ "$(ls -A "$OLD_COPY")" = "SKILL.md" ]; then
rm -rf "$OLD_COPY"; echo "[ok] Removed old skill copy: $SKILL_NAME"
fi
for SKILL_LINK in "$CLAUDE_DIR/skills/$SKILL_NAME" "$HOME/.agents/skills/$SKILL_NAME"; do
if [ -L "$SKILL_LINK" ] && [ "$(cd -P "$SKILL_LINK" 2>/dev/null && pwd)" = "$SKILL_SRC" ]; then
rm "$SKILL_LINK"; echo "[ok] Removed skill: $SKILL_NAME"
fi
done
done
# Remove output-style symlinks (only links that point at the kit; a real file stays)
for STYLE_FILE in "$KIT_DIR/output-styles/"*.md; do
[ -f "$STYLE_FILE" ] || continue
LINK="$CLAUDE_DIR/output-styles/$(basename "$STYLE_FILE")"
if [ -L "$LINK" ] && [ "$(readlink "$LINK")" = "$STYLE_FILE" ]; then
rm "$LINK"
echo "[ok] Removed output style: $(basename "${STYLE_FILE%.md}")"
fi
done
# Remove agents
for AGENT_FILE in "$KIT_DIR/agents/"*.md; do
AGENT_NAME=$(basename "$AGENT_FILE")
if [ -f "$CLAUDE_DIR/agents/$AGENT_NAME" ]; then
rm "$CLAUDE_DIR/agents/$AGENT_NAME"
echo "[ok] Removed agent: ${AGENT_NAME%.md}"
fi
done
# Detect an in-place install (checkout IS $CLAUDE_DIR/dwarves-kit, directly, via symlink,
# or through a case-variant path on case-insensitive filesystems). Compare by inode (-ef).
# Uninstall only removes what install created outside the checkout; checkout files stay intact.
UNINSTALL_IN_PLACE=0
if [ -e "$CLAUDE_DIR/dwarves-kit" ] && [ "$KIT_DIR" -ef "$CLAUDE_DIR/dwarves-kit" ]; then
UNINSTALL_IN_PLACE=1
fi
if [ "$UNINSTALL_IN_PLACE" -eq 1 ]; then
echo "[skip] In-place checkout ($KIT_DIR); keeping checkout hooks, lib, bin, docs, settings.json, and kit.toml"
else
# Remove the hook links/copies we created (and the dir if it is now empty).
HOOKS_DEST="$CLAUDE_DIR/dwarves-kit/hooks"
if [ -L "$HOOKS_DEST" ]; then
rm "$HOOKS_DEST"
echo "[ok] Removed hooks symlink: $HOOKS_DEST"
elif [ -d "$HOOKS_DEST" ]; then
for HOOK_FILE in "$KIT_DIR/hooks/"*.sh; do
[ -f "$HOOK_FILE" ] || continue
LINK="$HOOKS_DEST/$(basename "$HOOK_FILE")"
# symlinks (pre-SPEC-066) and copied files (SPEC-066) both belong to the kit
{ [ -L "$LINK" ] || [ -f "$LINK" ]; } && rm "$LINK"
done
# Companion *.py/*.json files installed alongside a hooks/*.sh shim (see install,
# step 1b); hooks.json (the plugin manifest) is excluded, matching install.
for AUX_FILE in "$KIT_DIR/hooks/"*.py "$KIT_DIR/hooks/"*.json; do
[ -f "$AUX_FILE" ] || continue
[ "$(basename "$AUX_FILE")" = "hooks.json" ] && continue
LINK="$HOOKS_DEST/$(basename "$AUX_FILE")"
{ [ -L "$LINK" ] || [ -f "$LINK" ]; } && rm "$LINK"
done
rmdir "$HOOKS_DEST" 2>/dev/null && echo "[ok] Removed hooks directory: $HOOKS_DEST" || true
fi
# Remove the bin/ stable-entrypoint dir we deployed (SPEC-184).
BIN_DEST="$CLAUDE_DIR/dwarves-kit/bin"
if [ -L "$BIN_DEST" ]; then
rm "$BIN_DEST" && echo "[ok] Removed bin symlink: $BIN_DEST"
elif [ -d "$BIN_DEST" ] && [ -f "$CLAUDE_DIR/dwarves-kit/INSTALL-STAMP" ]; then
rm -rf "$BIN_DEST" && echo "[ok] Removed copied bin dir: $BIN_DEST"
fi
# Remove the lib we deployed (SPEC-045; symlink pre-066, real dir post-066).
LIB_DEST="$CLAUDE_DIR/dwarves-kit/lib"
if [ -L "$LIB_DEST" ]; then
rm "$LIB_DEST" && echo "[ok] Removed lib symlink: $LIB_DEST"
elif [ -d "$LIB_DEST" ] && [ -f "$CLAUDE_DIR/dwarves-kit/INSTALL-STAMP" ]; then
rm -rf "$LIB_DEST" && echo "[ok] Removed copied lib dir: $LIB_DEST"
fi
# Remove the docs/impl-playbook/ dir we deployed (review-team agent reference rules).
PLAYBOOK_DEST="$CLAUDE_DIR/dwarves-kit/docs/impl-playbook"
if [ -L "$PLAYBOOK_DEST" ]; then
rm "$PLAYBOOK_DEST" && echo "[ok] Removed docs/impl-playbook symlink: $PLAYBOOK_DEST"
elif [ -d "$PLAYBOOK_DEST" ] && [ -f "$CLAUDE_DIR/dwarves-kit/INSTALL-STAMP" ]; then
rm -rf "$PLAYBOOK_DEST" && echo "[ok] Removed copied docs/impl-playbook dir: $PLAYBOOK_DEST"
fi
# Remove the settings.json template we deployed (compat symlink or out-of-place copy).
SETTINGS_TEMPLATE_DEST="$CLAUDE_DIR/dwarves-kit/settings.json"
if [ -L "$SETTINGS_TEMPLATE_DEST" ]; then
rm "$SETTINGS_TEMPLATE_DEST" && echo "[ok] Removed settings.json symlink: $SETTINGS_TEMPLATE_DEST"
elif [ -f "$SETTINGS_TEMPLATE_DEST" ] && [ -f "$CLAUDE_DIR/dwarves-kit/INSTALL-STAMP" ]; then
rm "$SETTINGS_TEMPLATE_DEST" && echo "[ok] Removed copied settings.json template: $SETTINGS_TEMPLATE_DEST"
fi
# Remove the operate-contract files (SPEC-049 symlinks, or SPEC-066 copies recorded in
# the stamp's managed= list; a user's own file is never in the list and never removed).
UNMANAGED="$(grep '^managed=' "$CLAUDE_DIR/dwarves-kit/INSTALL-STAMP" 2>/dev/null | cut -d= -f2- || true)"
for CONTRACT in AGENTS.md WORKFLOW.md docs/WORKFLOW.md; do
LINK="$CLAUDE_DIR/dwarves-kit/$CONTRACT"
if [ -L "$LINK" ]; then rm "$LINK" && echo "[ok] Removed $CONTRACT symlink: $LINK"
elif [ -f "$LINK" ] && printf '%s' " $UNMANAGED " | grep -q " $CONTRACT "; then
rm "$LINK" && echo "[ok] Removed kit-managed $CONTRACT: $LINK"
fi
done
[ -f "$CLAUDE_DIR/dwarves-kit/INSTALL-STAMP" ] && rm "$CLAUDE_DIR/dwarves-kit/INSTALL-STAMP" && echo "[ok] Removed install stamp"
fi
# Remove dwarves-kit hooks from settings.json
if [ -f "$SETTINGS_FILE" ] && command -v jq >/dev/null 2>&1; then
# Backup first
mkdir -p "$BACKUP_DIR"
cp "$SETTINGS_FILE" "$BACKUP_DIR/settings-pre-uninstall-$(date +%Y%m%d-%H%M%S).json"
# Remove any hook whose command path contains "dwarves-kit"
CLEANED=$(jq '
.hooks |= (
to_entries | map(
.value |= (
map(
.hooks |= map(select(.command | tostring | contains("dwarves-kit") | not))
) | map(select(.hooks | length > 0))
)
) | from_entries
)
' "$SETTINGS_FILE" 2>/dev/null || true)
if [ -n "$CLEANED" ]; then
echo "$CLEANED" | jq '.' > "$SETTINGS_FILE"
echo "[ok] Removed dwarves-kit hooks from settings.json"
else
echo "[warn] Could not clean settings.json automatically. Remove dwarves-kit entries manually."
fi
fi
# Claude Code mods: drop only OUR paths (copy and checkout forms) from env.CLAUDE_CODE_PLUGIN_DIRS
# (any other entry stays), drop the key when it ends empty, and remove the copied mod files.
# An in-place install (the clone IS ~/.claude/dwarves-kit) keeps its files: they are the repo's own.
_mod_paths=()
for _m in $KIT_MODS; do
_mod_paths+=("$CLAUDE_DIR/dwarves-kit/$KIT_MOD_BASE/$_m" "$KIT_DIR/$KIT_MOD_BASE/$_m")
done
if [ -f "$SETTINGS_FILE" ]; then
kit_drop_mod_dirs "${_mod_paths[@]}"
echo "[ok] Removed the Claude Code mod paths from settings.json env.CLAUDE_CODE_PLUGIN_DIRS"
fi
if [ "$UNINSTALL_IN_PLACE" -eq 0 ]; then
for _m in $KIT_MODS; do
if [ -d "$CLAUDE_DIR/dwarves-kit/$KIT_MOD_BASE/$_m" ]; then
rm -rf "$CLAUDE_DIR/dwarves-kit/$KIT_MOD_BASE/$_m"
echo "[ok] Removed copied mod files: $CLAUDE_DIR/dwarves-kit/$KIT_MOD_BASE/$_m"
fi
done
rmdir "$CLAUDE_DIR/dwarves-kit/$KIT_MOD_BASE" "$CLAUDE_DIR/dwarves-kit/integrations" 2>/dev/null || true
# Remove the module manifest (ID-277 SG-04); only for out-of-place installs.
if [ -L "$CLAUDE_DIR/dwarves-kit/kit.toml" ] || [ -f "$CLAUDE_DIR/dwarves-kit/kit.toml" ]; then
rm "$CLAUDE_DIR/dwarves-kit/kit.toml"
echo "[ok] Removed module manifest: kit.toml"
fi
fi
unset _m _mod_paths
echo ""
echo "=== Uninstall complete ==="
echo "Kit directory ($KIT_DIR) was NOT removed. Delete it manually if desired:"
echo " rm -rf $KIT_DIR"
exit 0
fi
# ============================================================
# INSTALL
# ============================================================
echo "=== dwarves-kit installer ==="
echo "Kit location: $KIT_DIR"
echo ""
# --- Layered install: spine + core modules + opt-in modules (ID-277 SG-04) --
# The spine (safety-gate, ship-gate, spec-drift-guard, secrets-guard, commit-format,
# anti-rationalization) is ALWAYS wired. A FRESH install (no kit.toml record yet) also wires
# the table's `core` modules. Every other module is wired only when named via `--with <a,b,c>`.
# A `kit.toml [modules]` manifest RECORDS the enabled set (for `--with`-less re-installs
# and future discovery) -- it is a shell-install RECORD, never a runtime feature-registry: no
# hook reads it (see tests/test-no-runtime-manifest-read.sh).
# The module table (names, hooks with their events, CLIs, core or opt-in) is ONE data file,
# lib/config/modules.tsv, read through lib/config/modules.sh. lib/adopt.sh reads the same file.
if [ ! -f "$KIT_DIR/lib/config/modules.sh" ] || [ ! -f "$KIT_DIR/lib/config/modules.tsv" ]; then
echo "[error] module table missing: $KIT_DIR/lib/config/modules.{sh,tsv}" >&2
exit 1
fi
# shellcheck source=lib/config/modules.sh
source "$KIT_DIR/lib/config/modules.sh"
KIT_KNOWN_MODULES="$(kit_modules_known)"
KIT_CORE_MODULES="$(kit_modules_core)"
KIT_SPINE_HOOKS="$(kit_module_hooks spine)"
# kit_write_cli_shim <name> <target> -- write ~/.local/bin/<name> as an exec-shim to
# <target>. A wrapper FILE, not a symlink: bin/ entrypoints resolve ../lib relative
# to BASH_SOURCE without realpath, so a symlink at ~/.local/bin would break them.
# Never clobbers a user-owned file: writes only when the path is absent, a symlink
# (e.g. a stale cc-elevation link, the exact thing this replaces), or a prior shim.
kit_write_cli_shim() {
local name="$1" target="$2" dst="$HOME/.local/bin/$1"
# A target outside $HOME means a fixture install (CLAUDE_DIR=<tmp>, HOME left
# real): the shim would point into a dir the fixture deletes, leaving a dangling
# CLI on the operator's PATH.
case "$target" in
"$HOME"/*) ;;
*) echo "[skip] $name shim: target $target is outside \$HOME (fixture install)"; return 0 ;;
esac
if [ -e "$dst" ] && [ ! -L "$dst" ] && ! grep -q "dwarves-kit CLI shim" "$dst" 2>/dev/null; then
echo "[warn] $dst exists and is not kit-managed; left untouched"
return 0
fi
mkdir -p "$HOME/.local/bin"
printf '#!/usr/bin/env bash\n# dwarves-kit CLI shim (installed by install.sh; re-run install.sh to refresh)\nexec "%s" "$@"\n' "$target" > "$dst.tmp.$$"
chmod +x "$dst.tmp.$$"
mv -f "$dst.tmp.$$" "$dst"
}
# kit_toml_modules_section_true <file> -- print the bare keys set `= true` WITHIN
# the [modules] section only (SPEC-183). Scoped on purpose: the full-schema kit.toml
# (repo-root default + its install-rendered copy) legitimately ships `= true`
# defaults in OTHER sections too ([ledger] telemetry, [mega] tier4_close, [gate]
# understanding_gate, [features] learning_ledger); a file-wide grep would
# misidentify those as "modules". INVARIANT: only ever call this on an
# INSTALL-RENDERED kit.toml (i.e. $KIT_TOML below), never the repo-root default --
# the match requires the line to end right after `true` with no trailing comment,
# which `kit_render_install_toml` guarantees for [modules] lines (it always emits
# a bare `key = true|false`), but the repo-root file's [modules] section has
# inline `#` comments on several keys and would silently under-match.
# `tests/test-install-modules.sh` keeps an identical `modules_section_true()`
# (can't source this file directly -- it's a full script, not a library); keep
# both in sync if this awk changes.
kit_toml_modules_section_true() {
awk '
/^\[modules\]/ { insec = 1; next }
/^\[/ { insec = 0 }
insec && /^[a-z_]+[[:space:]]*=[[:space:]]*true[[:space:]]*$/ {
key = $0; sub(/[[:space:]]*=.*/, "", key); print key
}
' "$1" 2>/dev/null
}
# kit_render_install_toml <src-default> <dst-install> -- render the install kit.toml
# (SPEC-183): copy the repo-root default VERBATIM (full schema, every section), then
# recompute ONLY the [modules] section's booleans from this run's actual enabled set
# (uses the caller's $KIT_ENABLED_MODULES). Every other section (`[ledger]`, `[mega]`,
# `[gate]`, `[features]`, `[team]`) rides through unchanged, so the install copy is
# the full schema, not just the old minimal manifest.
kit_render_install_toml() {
local src="$1" dst="$2" tmp mode=""
if [ -f "$dst" ] && [ ! -L "$dst" ]; then
mode="$(stat -c '%a' "$dst" 2>/dev/null || stat -f '%Lp' "$dst" 2>/dev/null || true)"
elif [ -f "$src" ]; then
mode="$(stat -c '%a' "$src" 2>/dev/null || stat -f '%Lp' "$src" 2>/dev/null || true)"
fi
case "$mode" in
[0-7][0-7][0-7]|[0-7][0-7][0-7][0-7]) ;;
*) mode="0644" ;;
esac
tmp="$(mktemp "$(dirname "$dst")/.kit.toml.XXXXXX")"
{
echo "# --- Rendered by install.sh from the repo-root kit.toml + --with (SPEC-183). ---"
echo "# Do not hand-edit the [modules] section; re-run \`install.sh --with <modules>\`"
echo "# (or \`--prune --with <modules>\` to trim) to change it. Every other section is"
echo "# copied verbatim from the repo-root default; edit that file upstream to change a"
echo "# default, or this project's .kit.toml to override just this project."
echo ""
awk -v mods=" $KIT_ENABLED_MODULES " '
/^\[/ { insec = ($0 ~ /^\[modules\]/); print; next }
insec && /^[a-z_]+[[:space:]]*=/ {
key = $0; sub(/[[:space:]]*=.*/, "", key)
if (key == "team_mode") { print "team_mode = false"; next }
val = (index(mods, " " key " ") > 0) ? "true" : "false"
print key " = " val
next
}
{ print }
' "$src"
} > "$tmp" && chmod "$mode" "$tmp" && mv -f "$tmp" "$dst" || { rm -f "$tmp"; return 1; }
}
KIT_WITH_ARG=""
KIT_PRUNE=0
KIT_NO_MODS=0
KIT_FORCE_BOTH=0
# KIT_FORCE_FULL=1 is the older spelling of --force-both; scripts and docs still use it.
[ -n "${KIT_FORCE_FULL:-}" ] && KIT_FORCE_BOTH=1
KIT_ARGS=("$@")
_i=0
while [ $_i -lt ${#KIT_ARGS[@]} ]; do
case "${KIT_ARGS[$_i]}" in
--with)
_i=$((_i + 1))
KIT_WITH_ARG="${KIT_ARGS[$_i]:-}"
;;
--with=*)
KIT_WITH_ARG="${KIT_ARGS[$_i]#--with=}"
;;
--prune)
KIT_PRUNE=1
;;
--no-mods)
KIT_NO_MODS=1
;;
--force-both)
KIT_FORCE_BOTH=1
;;
*)
echo "[error] unknown argument: '${KIT_ARGS[$_i]}' (--uninstall is only valid as the first argument)" >&2
kit_usage >&2
exit 64
;;
esac
_i=$((_i + 1))
done
unset _i
# Validate + normalize the requested module list (clean error on unknown/reserved).
KIT_REQUESTED_MODULES=""
if [ -n "$KIT_WITH_ARG" ]; then
IFS=',' read -ra _KIT_REQ <<< "$KIT_WITH_ARG"
for _m in "${_KIT_REQ[@]}"; do
_m="$(echo "$_m" | xargs)"
[ -z "$_m" ] && continue
if [ "$_m" = "team_mode" ]; then
echo "[error] 'team_mode' is a reserved, not-yet-installable module (parked; see DECISIONS.md Decision C)." >&2
exit 1
fi
case " $KIT_KNOWN_MODULES " in
*" $_m "*) KIT_REQUESTED_MODULES="$KIT_REQUESTED_MODULES $_m" ;;
*)
echo "[error] unknown module: '$_m'. Known modules: $KIT_KNOWN_MODULES" >&2
exit 1
;;
esac
done
unset _m
fi
KIT_REQUESTED_MODULES="$(echo "$KIT_REQUESTED_MODULES" | xargs)"
# --- One-time log migration -----------------------------------------------
# Old run and hook logs move into the durable ledger root here, once, and never from a hook or a
# resolver (lib/telemetry/kit-migrate.sh). Runs before the plugin-compat branch so both modes get it.
bash "$KIT_DIR/lib/telemetry/kit-migrate.sh" || true
# --- Plugin-aware compat mode --------------------------------------------
# If the kit is already installed as a Claude Code plugin, the plugin provides
# the hooks, commands, agents, and lib/ at runtime via ${CLAUDE_PLUGIN_ROOT}.
# Wiring hooks here too would DOUBLE-register them (settings.json AND the plugin) and
# could pin a different lib/ version, which is exactly the "don't run both paths" hazard
# the README warns about. So on a plugin machine this script REFUSES to wire hooks.
#
# It does a COMPAT-ONLY install instead: symlink the legacy
# ~/.claude/dwarves-kit paths (lib, hooks, WORKFLOW.md, AGENTS.md) that docs still
# call as plain bash (`bash ~/.claude/dwarves-kit/lib/<x>.sh`, where
# CLAUDE_PLUGIN_ROOT is not set). No settings.json hooks, no flat commands. The
# symlinks track this checkout (KIT_DIR), which is also the marketplace source,
# so a `git pull` updates them. `--force-both` wires the hooks anyway (they then fire twice).
#
# "Plugin installed" is lib/onboard-detect.sh's answer, the same one /kit:onboard shows:
# mode `plugin` (cache or roster, no bash hooks) or `both` (a plugin AND bash hooks).
if ! KIT_INSTALL_MODE="$(CLAUDE_DIR="$CLAUDE_DIR" bash "$KIT_DIR/lib/onboard-detect.sh" mode 2>/dev/null)"; then
echo "[warn] plugin detector ($KIT_DIR/lib/onboard-detect.sh) failed; assuming no kit plugin is installed"
KIT_INSTALL_MODE=none
fi
case "$KIT_INSTALL_MODE" in plugin|both) KIT_PLUGIN_PRESENT=1 ;; *) KIT_PLUGIN_PRESENT=0 ;; esac
# kit_symlink_hardened <target> <linkname> -- `ln -sfn` silently no-ops on macOS
# when LINKNAME already exists as a REAL (non-symlink) directory: -n only guards
# against LINKNAME being a symlink-to-dir, so ln instead treats LINKNAME as a
# directory and links TARGET's basename INSIDE it, leaving LINKNAME itself
# unchanged (observed: a stale real ~/.claude/dwarves-kit/hooks/ dir that never
# auto-updated). Move a real non-empty dir aside first, loudly, before linking.
kit_symlink_hardened() {
local target="$1" linkname="$2"
# Self-link guard: on a dev machine ~/.claude/dwarves-kit is itself a symlink
# to this checkout, so $CLAUDE_DIR/dwarves-kit/hooks and $KIT_DIR/hooks name
# the SAME path. Linking one to the other moves the real dir aside and leaves
# a symlink pointing at itself: every hook and bin then dies with ELOOP.
# Compare the resolved paths and skip.
local t_dir l_dir
t_dir="$(cd "$(dirname "$target")" 2>/dev/null && pwd -P)" || t_dir=""
l_dir="$(cd "$(dirname "$linkname")" 2>/dev/null && pwd -P)" || l_dir=""
if [ -n "$t_dir" ] && [ "$t_dir/$(basename "$target")" = "$l_dir/$(basename "$linkname")" ]; then
echo "[skip] $linkname already resolves to $target (symlinked checkout); not linking it to itself"
return 0
fi
if [ -e "$linkname" ] && [ ! -L "$linkname" ] && [ -d "$linkname" ] && [ -n "$(ls -A "$linkname" 2>/dev/null)" ]; then
local bak
bak="${linkname}.pre-symlink.bak.$(date +%Y%m%d-%H%M%S)"
echo "[warn] $linkname is a real non-empty directory (ln -sfn would silently no-op); moving it to $bak"
mv "$linkname" "$bak"
fi
mkdir -p "$(dirname "$linkname")"
ln -sfn "$target" "$linkname"
}
# Claude Code mods (integrations/claude-code/, list in KIT_MODS above): each loads in every
# session through env.CLAUDE_CODE_PLUGIN_DIRS in settings.json. `--no-mods` skips them all.
# kit_register_mod_dir <path> [<drop-path>] -- append <path> to env.CLAUDE_CODE_PLUGIN_DIRS
# (':' joined, never duplicated, every other entry kept). <drop-path>, the same mod's other
# location, leaves the list so one plugin dir never loads the mod twice. Both the full install
# (a pinned copy) and the plugin-compat install (the live checkout) call this one function.
kit_register_mod_dir() {
local path="$1" drop="${2:-}" merged
[ "$drop" = "$path" ] && drop=""
mkdir -p "$(dirname "$SETTINGS_FILE")"
[ -f "$SETTINGS_FILE" ] || echo '{}' > "$SETTINGS_FILE"
merged=$(jq --arg p "$path" --arg d "$drop" '
((.env.CLAUDE_CODE_PLUGIN_DIRS // "") | split(":") | map(select(length > 0 and . != $d))) as $dirs
| .env = ((.env // {}) + {CLAUDE_CODE_PLUGIN_DIRS: ((if ($dirs | index($p)) then $dirs else $dirs + [$p] end) | join(":"))})
' "$SETTINGS_FILE" 2>/dev/null || true)
if [ -n "$merged" ]; then
echo "$merged" | jq '.' > "$SETTINGS_FILE"
echo "[ok] Mod ${path##*/} registered in settings.json env.CLAUDE_CODE_PLUGIN_DIRS: $path (opt out: --no-mods)"
else
echo "[warn] Could not register the ${path##*/} mod; add $path to CLAUDE_CODE_PLUGIN_DIRS by hand"
fi
}
# The plugin's cache dir, when there is one (a roster-only plugin has none): the source of the
# agents the plugin serves.
PLUGIN_LIB="$(ls -d "$CLAUDE_DIR"/plugins/cache/dwarves-marketplace/kit/*/lib 2>/dev/null | sort -V | tail -1 || true)"
# kit_retire_bare_command_links -- the plugin serves every command under the kit: prefix, so a
# bare-name symlink a previous shell install left behind duplicates the slash menu and shadows
# same-named Anthropic built-ins (/design was the first real collision). Remove only our own links.
kit_retire_bare_command_links() {
local cmd_file cmd_name link
for cmd_file in "$KIT_DIR/commands/"*.md; do
cmd_name=$(basename "$cmd_file")
link="$CLAUDE_DIR/commands/$cmd_name"
if [ -L "$link" ] && [ "$(readlink "$link")" = "$cmd_file" ]; then
rm "$link"
echo "[ok] Removed bare command symlink (plugin serves kit:${cmd_name%.md}): /${cmd_name%.md}"
fi
done
echo "[ok] Plugin install detected: commands served as kit:*, no bare symlinks"
}
if [ "$KIT_PLUGIN_PRESENT" -eq 1 ] && [ "$KIT_FORCE_BOTH" -eq 0 ]; then
echo "[plugin detected] kit@dwarves-marketplace is installed (mode: $KIT_INSTALL_MODE); runtime comes from the plugin."
echo "[refused] Not wiring kit hooks into settings.json: the plugin already registers every kit hook,"
echo " so a second set would fire each hook twice. Doing a COMPAT-ONLY install instead"
echo " (legacy path shims). To wire the bash hooks anyway: bash install.sh --force-both"
if [ -n "$KIT_WITH_ARG" ]; then
echo "[refused] --with $KIT_WITH_ARG ignored: modules select hooks, and the plugin owns the hooks here."
fi
if [ "$KIT_INSTALL_MODE" = "both" ]; then
echo "[warn] settings.json ALREADY carries kit hooks next to the plugin's, so every hook fires twice right now."
echo " Keep one path: bash install.sh --uninstall, then bash install.sh (compat-only, the plugin's hooks stay)."
fi
kit_retire_bare_command_links
# Self-heal stale bare agent copies from a full install that predates this
# compat-mode branch (or ran with --force-both): the plugin already
# serves these under the kit: namespace, so a leftover bare copy is a
# duplicate, not a second install path. Same removal a full --uninstall
# does, run here so a plain re-install cleans it up too.
# The bare copy is unowned user data (it may have been edited), so it is
# moved aside, never rm'd, and only when the cached plugin really ships
# that agent; a checkout ahead of the plugin release leaves the copy alone.
PLUGIN_AGENTS=""
[ -n "${PLUGIN_LIB:-}" ] && PLUGIN_AGENTS="$(dirname "$PLUGIN_LIB")/agents"
RETIRED_AGENTS="$CLAUDE_DIR/agents.retired-$(date +%Y%m%d)"
if [ -n "$PLUGIN_AGENTS" ] && [ -d "$KIT_DIR/agents" ] && [ -d "$CLAUDE_DIR/agents" ] && [ ! -L "$CLAUDE_DIR/agents" ]; then
for AGENT_FILE in "$KIT_DIR/agents/"*.md; do
AGENT_NAME=$(basename "$AGENT_FILE")
[ -f "$CLAUDE_DIR/agents/$AGENT_NAME" ] || continue
[ -f "$PLUGIN_AGENTS/$AGENT_NAME" ] || continue
mkdir -p "$RETIRED_AGENTS"
if mv "$CLAUDE_DIR/agents/$AGENT_NAME" "$RETIRED_AGENTS/$AGENT_NAME"; then
echo "[ok] Retired stale bare agent copy to $RETIRED_AGENTS (plugin provides kit:${AGENT_NAME%.md}): $AGENT_NAME"
else
echo "[warn] Could not retire $CLAUDE_DIR/agents/$AGENT_NAME; left in place"
fi
done
fi
mkdir -p "$CLAUDE_DIR/dwarves-kit/docs"
for f in bin lib hooks WORKFLOW.md AGENTS.md kit.toml VERSION settings.json docs/WORKFLOW.md docs/impl-playbook; do
kit_symlink_hardened "$KIT_DIR/$f" "$CLAUDE_DIR/dwarves-kit/$f"
echo "[ok] compat symlink ~/.claude/dwarves-kit/$f -> $KIT_DIR/$f"
done
# CLI shims too: a plugin/compat machine is a dev machine, so expose every
# module's CLIs (module gating only applies to the full bash install, which is
# the only path that resolves an enabled set).
for _mod in $KIT_KNOWN_MODULES; do
for _cli in $(kit_module_clis "$_mod"); do
kit_write_cli_shim "$_cli" "$CLAUDE_DIR/dwarves-kit/bin/$_cli"
done
done
unset _mod _cli
echo "[ok] CLI shims written to ~/.local/bin"
echo ""
# The mods run live from this checkout, like the bin/lib symlinks above: no copy.
if [ "$KIT_NO_MODS" -eq 1 ]; then
echo "[skip] Claude Code mods not installed (--no-mods)"
elif [ -d "$KIT_DIR/$KIT_MOD_BASE" ]; then
[ -f "$SETTINGS_FILE" ] && { mkdir -p "$BACKUP_DIR"; cp "$SETTINGS_FILE" "$BACKUP_DIR/settings-pre-compat-$(date +%Y%m%d-%H%M%S).json"; }
for _m in $KIT_MODS; do
[ -d "$KIT_DIR/$KIT_MOD_BASE/$_m" ] || continue
kit_mod_skip_dup "$_m" && continue
kit_register_mod_dir "$KIT_DIR/$KIT_MOD_BASE/$_m" "$CLAUDE_DIR/dwarves-kit/$KIT_MOD_BASE/$_m"
done
unset _m
fi
# Link skills into ~/.agents/skills only (the plugin serves Claude's kit: skills,
# so linking ~/.claude/skills would double them). Never overwrite a folder or link the kit does not own.
mkdir -p "$HOME/.agents/skills"
for SKILL_FILE in "$KIT_DIR/.agents/skills/"*/SKILL.md; do
[ -f "$SKILL_FILE" ] || continue
SKILL_NAME="$(basename "$(dirname "$SKILL_FILE")")"
SKILL_SRC="$(cd -P "$KIT_DIR/.agents/skills/$SKILL_NAME" && pwd)"
SKILL_DST="$HOME/.agents/skills/$SKILL_NAME"
[ "$(cd -P "$SKILL_DST" 2>/dev/null && pwd)" = "$SKILL_SRC" ] && continue
if [ -L "$SKILL_DST" ]; then
case "$(readlink "$SKILL_DST")" in "$KIT_DIR"/*) ;; *) echo "[warn] $SKILL_DST links elsewhere; left as is"; continue ;; esac
elif [ -e "$SKILL_DST" ]; then
echo "[warn] $SKILL_DST is not the kit's; left as is"; continue
fi
ln -sfn "$KIT_DIR/.agents/skills/$SKILL_NAME" "$SKILL_DST"
echo "[ok] Linked skill: $SKILL_NAME"
done
echo ""
echo "Legacy doc paths (bash ~/.claude/dwarves-kit/lib/*.sh) now resolve."
echo "Full bash install anyway (hooks then fire twice): bash install.sh --force-both"
exit 0
fi
# --- end plugin-aware compat mode ----------------------------------------
# Ensure ~/.claude exists
mkdir -p "$CLAUDE_DIR/commands" "$CLAUDE_DIR/skills"
# 1. Make hook scripts executable
chmod +x "$KIT_DIR/hooks/"*.sh
echo "[ok] Hook scripts are executable"
# 1b. Ensure the hook scripts exist at the path settings.json references.
# settings.json hard-codes $HOME/.claude/dwarves-kit/hooks/<script>.sh, so the
# scripts must live there. Two layouts:
# in-place : the kit was cloned to ~/.claude/dwarves-kit (docs/reference/install.md, Option 3), so
# KIT_DIR already IS ~/.claude/dwarves-kit and the scripts are in
# place. Linking would point each script at itself, so skip it.
# elsewhere: a dev checkout, CI, or template dir. Link each hooks/*.sh into
# ~/.claude/dwarves-kit/hooks/ (per-file, like commands, so repo
# edits stay live). Without this every hook fails at runtime with
# "No such file or directory".
# NOTE: detection relies on ~/.claude/dwarves-kit NOT existing yet on a first
# out-of-place install. Do not mkdir "$CLAUDE_DIR/dwarves-kit" before this block,
# or the in-place branch could false-match and skip linking.
KIT_REAL="$(cd "$KIT_DIR" && pwd -P)"
DEST_REAL="$(cd "$CLAUDE_DIR/dwarves-kit" 2>/dev/null && pwd -P || true)"
if [ -n "$DEST_REAL" ] && [ "$KIT_REAL" = "$DEST_REAL" ]; then
echo "[ok] Kit is installed in place; hooks already at \$HOME/.claude/dwarves-kit/hooks/"
else
HOOKS_DEST="$CLAUDE_DIR/dwarves-kit/hooks"
# A previous run may have left a directory symlink here; drop it so we own a real dir.
[ -L "$HOOKS_DEST" ] && rm "$HOOKS_DEST"
mkdir -p "$HOOKS_DEST"
# SPEC-066: COPY, never symlink. A symlinked hook follows the CHECKED-OUT BRANCH of
# the clone, so switching branches silently swaps the live enforcement code (observed
# 2026-06-08: a fixed safety-gate regressed to the old build mid-session). Copies pin
# the installed version; upgrades are an explicit re-run of install.sh.
for HOOK_FILE in "$KIT_DIR/hooks/"*.sh; do
LINK="$HOOKS_DEST/$(basename "$HOOK_FILE")"
if [ -L "$LINK" ] || [ -f "$LINK" ]; then
rm "$LINK"
fi
cp "$HOOK_FILE" "$LINK" && chmod +x "$LINK"
done
# Companion files a hooks/*.sh shim `exec`s or reads by co-located path (e.g. the
# kit-foldin *.py ports + their JSON data files) must land alongside it in
# $HOOKS_DEST too, or the shim's realpath-relative lookup 404s post-install.
# hooks.json (the plugin manifest) is excluded: it is not consulted at this path.
for AUX_FILE in "$KIT_DIR/hooks/"*.py "$KIT_DIR/hooks/"*.json; do
[ -f "$AUX_FILE" ] || continue
[ "$(basename "$AUX_FILE")" = "hooks.json" ] && continue
LINK="$HOOKS_DEST/$(basename "$AUX_FILE")"
[ -L "$LINK" ] || [ -f "$LINK" ] && rm -f "$LINK"
cp "$AUX_FILE" "$LINK"
done
echo "[ok] Copied hook scripts into $HOOKS_DEST/ (pinned; re-run install.sh to upgrade)"
fi
# 1c. Deploy lib/ so the gates (proof-ledger, gate-ledger) resolve from the stable
# install path even when pushing a CONSUMER repo (SPEC-045). A consumer repo has no
# lib/, and bash-install mode has no CLAUDE_PLUGIN_ROOT, so without this the ship-gate
# fails open in every repo but dwarves-kit itself. Copied, not symlinked (SPEC-066).
if [ -n "$DEST_REAL" ] && [ "$KIT_REAL" = "$DEST_REAL" ]; then
echo "[ok] Kit is installed in place; lib already at \$HOME/.claude/dwarves-kit/lib/"
else
LIB_DEST="$CLAUDE_DIR/dwarves-kit/lib"
mkdir -p "$CLAUDE_DIR/dwarves-kit"
[ -L "$LIB_DEST" ] && rm "$LIB_DEST"
[ -d "$LIB_DEST" ] && [ ! -L "$LIB_DEST" ] && rm -rf "$LIB_DEST"
# Skip gitignored build output: a venv is not relocatable and `bin/stats` (`uv run --project`)
# rebuilds it on first use; the retired prose-rag Rust crate left target/ and bin/*-rs behind
# (419M + 5M) and nothing under bin/ or lib/ resolves them (bin/prose-rag never looks under lib/).
# Patterns mirror the .gitignore build-output rules.
(cd "$KIT_DIR" && tar -cf - \
--exclude='.venv' --exclude='__pycache__' --exclude='*.pyc' \
--exclude='lib/*/rust/target' --exclude='lib/*/bin/*-rs' \
--exclude='lib/*/tests' --exclude='lib/*/*/tests' lib) \
| tar -xpf - -C "$CLAUDE_DIR/dwarves-kit"
echo "[ok] Copied lib into $LIB_DEST (pinned, SPEC-066; build output and module tests skipped)"
fi
# 1c-bis. Deploy bin/ -- the STABLE consumer entrypoint dir (SPEC-184). Consumers (an
# adopted repo's board shims, the adopt-injected CLAUDE.md block) reference
# $DWARVES_KIT/bin/<name>, never a deep lib path, so an internal lib reorg never breaks
# them. Copied next to lib/ so each wrapper's `../lib/...` resolution holds in the install.
if [ -n "$DEST_REAL" ] && [ "$KIT_REAL" = "$DEST_REAL" ]; then
echo "[ok] Kit is installed in place; bin already at \$HOME/.claude/dwarves-kit/bin/"
else
BIN_DEST="$CLAUDE_DIR/dwarves-kit/bin"
mkdir -p "$CLAUDE_DIR/dwarves-kit"
[ -L "$BIN_DEST" ] && rm "$BIN_DEST"
[ -d "$BIN_DEST" ] && [ ! -L "$BIN_DEST" ] && rm -rf "$BIN_DEST"
cp -R "$KIT_DIR/bin" "$BIN_DEST"
echo "[ok] Copied bin into $BIN_DEST (stable entrypoint, SPEC-184)"
fi
# 1d. Deploy the operate-contract files so they resolve from the stable install path. adopt.sh
# needs a source AGENTS.md at $KIT_ROOT; gate-ledger reads the lane x phase matrix from
# $KIT_ROOT/docs/WORKFLOW.md (the bulk moved out of the root stub, SPEC-185). Without these,
# adopt + the lane gate are broken from the install (SPEC-049). Copied and version-pinned,
# mirroring hooks + lib (SPEC-066). docs/WORKFLOW.md is the CRITICAL one here: root WORKFLOW.md
# is now just a thin pointer stub, so skipping the docs/ copy would leave every installed
# consumer's gate machinery reading an empty/pointer-only file (404 in effect).
if [ -n "$DEST_REAL" ] && [ "$KIT_REAL" = "$DEST_REAL" ]; then
echo "[ok] Kit is installed in place; AGENTS.md + WORKFLOW.md + docs/WORKFLOW.md already at \$HOME/.claude/dwarves-kit/"
else
mkdir -p "$CLAUDE_DIR/dwarves-kit/docs"
# A real file is kit-managed ONLY if a prior run recorded it in the stamp's managed=
# list; presence of the stamp alone is not enough (review HIGH: the stamp is written by
# the same run that first sees the user's file, so stamp-presence destroys it on run 2).
PRIOR_MANAGED=""
[ -f "$CLAUDE_DIR/dwarves-kit/INSTALL-STAMP" ] \
&& PRIOR_MANAGED="$(grep '^managed=' "$CLAUDE_DIR/dwarves-kit/INSTALL-STAMP" 2>/dev/null | cut -d= -f2- || true)"
MANAGED_CONTRACTS=""
COPIED_CONTRACTS=""
for CONTRACT in AGENTS.md WORKFLOW.md docs/WORKFLOW.md; do
LINK="$CLAUDE_DIR/dwarves-kit/$CONTRACT"
if [ -L "$LINK" ]; then
rm "$LINK" # refresh a stale symlink
elif [ -e "$LINK" ] && ! printf '%s' " $PRIOR_MANAGED " | grep -q " $CONTRACT "; then
# A real file never recorded as kit-managed = the user's own file; leave it intact
# (clobbering would be silent data loss, the SPEC-049 review rule, made durable).
echo "[skip] $CONTRACT at $LINK is a user file (not in the stamp's managed list); leaving it untouched"
continue
fi
cp "$KIT_DIR/$CONTRACT" "$LINK"
MANAGED_CONTRACTS="$MANAGED_CONTRACTS $CONTRACT"
COPIED_CONTRACTS="$COPIED_CONTRACTS $CONTRACT"
done
if [ -n "$COPIED_CONTRACTS" ]; then
echo "[ok] Copied$COPIED_CONTRACTS into $CLAUDE_DIR/dwarves-kit/ (pinned, SPEC-066)"
else
echo "[ok] Contract files left as user files (none kit-managed)"
fi
fi
# 1d-bis. Deploy docs/impl-playbook/ -- the STABLE reference path the review-team agents
# (security-reviewer, code-reviewer, infra-reviewer, frontend-reviewer, test-writer) cite by
# absolute path (~/.claude/dwarves-kit/docs/impl-playbook/<file>.md). A bare repo-relative
# citation only resolves when the agent's cwd happens to be the kit's own checkout, never true
# when reviewing a CONSUMER repo, the actual real-world dispatch. Copied like lib/bin (SPEC-066:
# copy, never symlink, so a branch switch mid-session can't silently swap the rule content).
if [ -n "$DEST_REAL" ] && [ "$KIT_REAL" = "$DEST_REAL" ]; then
echo "[ok] Kit is installed in place; docs/impl-playbook already at \$HOME/.claude/dwarves-kit/docs/impl-playbook/"
else
PLAYBOOK_DEST="$CLAUDE_DIR/dwarves-kit/docs/impl-playbook"
mkdir -p "$CLAUDE_DIR/dwarves-kit/docs"
[ -L "$PLAYBOOK_DEST" ] && rm "$PLAYBOOK_DEST"
[ -d "$PLAYBOOK_DEST" ] && [ ! -L "$PLAYBOOK_DEST" ] && rm -rf "$PLAYBOOK_DEST"
mkdir -p "$PLAYBOOK_DEST"
for _pb in $KIT_IMPL_PLAYBOOKS; do
[ -f "$KIT_DIR/docs/impl-playbook/$_pb" ] || continue
cp "$KIT_DIR/docs/impl-playbook/$_pb" "$PLAYBOOK_DEST/$_pb"
done
unset _pb
echo "[ok] Copied referenced docs/impl-playbook files into $PLAYBOOK_DEST (pinned, SPEC-066)"
fi
# 1d-ter. Deploy root settings.json so lib/adopt.sh run from the installed path can wire
# enabled module hooks into a project's .claude/settings.json. Copied rather than symlinked;
# any prior compat symlink is dropped first so we never write through into the checkout.
if [ -n "$DEST_REAL" ] && [ "$KIT_REAL" = "$DEST_REAL" ]; then
echo "[ok] Kit is installed in place; settings.json already at \$HOME/.claude/dwarves-kit/settings.json"
elif [ -f "$KIT_DIR/settings.json" ]; then
SETTINGS_TEMPLATE_DEST="$CLAUDE_DIR/dwarves-kit/settings.json"
mkdir -p "$CLAUDE_DIR/dwarves-kit"
[ -L "$SETTINGS_TEMPLATE_DEST" ] && rm "$SETTINGS_TEMPLATE_DEST"
cp "$KIT_DIR/settings.json" "$SETTINGS_TEMPLATE_DEST"
echo "[ok] Copied settings.json into $SETTINGS_TEMPLATE_DEST (pinned)"
fi
# 1e. Version stamp (SPEC-066): records WHAT is installed so kit-health can flag a stale
# install (the upgrade path is: pull the repo, re-run install.sh). Skipped for in-place
# installs (the clone IS the install).
if [ -z "$DEST_REAL" ] || [ "$KIT_REAL" != "$DEST_REAL" ]; then
{
echo "version=$(cat "$KIT_DIR/VERSION" 2>/dev/null || echo unknown)"
echo "sha=$(git -C "$KIT_DIR" rev-parse --short HEAD 2>/dev/null || echo unknown)"
echo "date=$(date -u +%Y-%m-%dT%H:%M:%SZ)"
echo "managed=${MANAGED_CONTRACTS# }"
} > "$CLAUDE_DIR/dwarves-kit/INSTALL-STAMP"
echo "[ok] Stamped install: $(tr '\n' ' ' < "$CLAUDE_DIR/dwarves-kit/INSTALL-STAMP")"
fi
# 1f. Resolve the enabled module set (spine always; optionals additive across
# re-installs unless --prune). Four inputs feed the union so a re-run NEVER
# retroactively un-wires a hook a prior run (or a hand-edited settings.json) wired:
# (a) kit.toml's prior [modules] (this consumer's own record)
# (b) hooks ALREADY present in settings.json that map to a known module (covers
# a consumer who ran the OLD all-hooks installer, e.g. ops-toolkit/
# console-labs/family-office, before this manifest existed)
# (c) this run's --with request
# (d) the table's `core` modules, on a FRESH install only. Fresh = no install record and no
# module hook in settings.json. An existing install keeps what it has: a change to the
# core set reaches new installs, never a re-run on a machine that already chose.
# --prune drops (a)+(b)+(d) and trims to exactly --with (spine-only if --with is empty).
KIT_TOML="$CLAUDE_DIR/dwarves-kit/kit.toml"
KIT_PRIOR_MODULES=""
# A kit.toml that is a symlink is a compat install's link into the checkout, not an install record.
if [ -f "$KIT_TOML" ] && [ ! -L "$KIT_TOML" ]; then
KIT_PRIOR_MODULES="$( (kit_toml_modules_section_true "$KIT_TOML" | grep -v '^team_mode$' | tr '\n' ' ') || true)"
fi
KIT_EXISTING_WIRED_MODULES=""
if [ -f "$SETTINGS_FILE" ] && command -v jq >/dev/null 2>&1; then
KIT_EXISTING_CMDS="$(jq -r '(.hooks // {}) | to_entries[]? | .value[]? | .hooks[]? | .command // empty' "$SETTINGS_FILE" 2>/dev/null || true)"
for _mod in $KIT_KNOWN_MODULES; do
for _h in $(kit_module_hooks "$_mod"); do
if printf '%s\n' "$KIT_EXISTING_CMDS" | grep -q "dwarves-kit/hooks/${_h}"; then
KIT_EXISTING_WIRED_MODULES="$KIT_EXISTING_WIRED_MODULES $_mod"
break
fi
done
done
unset _mod _h
fi
KIT_DEFAULT_MODULES=""
if [ -z "$KIT_PRIOR_MODULES" ] && [ -z "$KIT_EXISTING_WIRED_MODULES" ] \
&& { [ ! -f "$KIT_TOML" ] || [ -L "$KIT_TOML" ] \
|| { [ -n "$DEST_REAL" ] && [ "$KIT_REAL" = "$DEST_REAL" ] && { [ ! -f "$SETTINGS_FILE" ] || ! grep -q 'dwarves-kit/hooks/' "$SETTINGS_FILE" 2>/dev/null; }; }; }; then
KIT_DEFAULT_MODULES="$KIT_CORE_MODULES"
fi
if [ "$KIT_PRUNE" -eq 1 ]; then
KIT_ENABLED_MODULES="$KIT_REQUESTED_MODULES"
echo "[ok] --prune: trimming enabled modules to exactly: ${KIT_ENABLED_MODULES:-<spine-only>}"
else
KIT_ENABLED_MODULES="$(printf '%s %s %s %s\n' "$KIT_PRIOR_MODULES" "$KIT_EXISTING_WIRED_MODULES" "$KIT_REQUESTED_MODULES" "$KIT_DEFAULT_MODULES" \
| tr ' ' '\n' | sed '/^$/d' | sort -u | tr '\n' ' ' | xargs)"
if [ -n "$KIT_DEFAULT_MODULES" ]; then
echo "[ok] Fresh install: core modules on by default: $KIT_DEFAULT_MODULES (the rest are opt-in: --with <a,b,c>)"
fi
fi
KIT_ENABLED_HOOK_NAMES="$KIT_SPINE_HOOKS"
for _mod in $KIT_ENABLED_MODULES; do
KIT_ENABLED_HOOK_NAMES="$KIT_ENABLED_HOOK_NAMES $(kit_module_hooks "$_mod")"
done
unset _mod
KIT_HOOK_RE=""
for _h in $KIT_ENABLED_HOOK_NAMES; do
_esc="$(printf '%s' "$_h" | sed 's/\./\\./g')"
if [ -z "$KIT_HOOK_RE" ]; then KIT_HOOK_RE="$_esc"; else KIT_HOOK_RE="$KIT_HOOK_RE|$_esc"; fi
done
unset _h _esc
# The filtered kit settings.json: spine hooks + only the opted-in modules' hooks.
# An un-installed module's hook is never present here, so it can never reach the
# consumer's settings.json below.
# statusLine ships with the `cosmetic` module (its command is statusline.sh); strip
# it here too, not just in step 7's gate, so a fresh (no-prior-settings.json) install
# doesn't smuggle it in via the blind first-time copy below.
KIT_STATUSLINE_ON=0
case " $KIT_ENABLED_HOOK_NAMES " in *" statusline.sh "*) KIT_STATUSLINE_ON=1 ;; esac
KIT_SETTINGS_FILTERED="$(mktemp "${TMPDIR:-/tmp}/kit.XXXXXX")"
jq --arg re "$KIT_HOOK_RE" --argjson sl "$KIT_STATUSLINE_ON" '
.hooks |= (
to_entries | map(
.value |= (
map(
.hooks |= map(select(.command | test($re)))
) | map(select(.hooks | length > 0))
)
) | from_entries
)
| if ($sl == 1) then . else del(.statusLine) end
' "$KIT_DIR/settings.json" > "$KIT_SETTINGS_FILTERED"
# 2. Merge settings.json
if [ ! -f "$SETTINGS_FILE" ]; then
# No existing settings, just copy the filtered (spine + opted-in) set
cp "$KIT_SETTINGS_FILTERED" "$SETTINGS_FILE"
echo "[ok] Created $SETTINGS_FILE"
else
if command -v jq >/dev/null 2>&1; then
# Backup existing settings
mkdir -p "$BACKUP_DIR"
cp "$SETTINGS_FILE" "$BACKUP_DIR/settings-pre-install-$(date +%Y%m%d-%H%M%S).json"
echo "[ok] Backed up existing settings.json"
# First, remove any existing dwarves-kit hooks (idempotent reinstall)
EXISTING_CLEAN=$(jq '
.hooks |= (
to_entries | map(
.value |= (
map(
.hooks |= map(select(.command | tostring | contains("dwarves-kit") | not))
) | map(select(.hooks | length > 0))
)
) | from_entries
)
' "$SETTINGS_FILE" 2>/dev/null || cat "$SETTINGS_FILE")
# Then merge: CONCAT arrays (don't deduplicate by matcher)
# This preserves the user's existing hooks alongside ours
MERGED=$(echo "$EXISTING_CLEAN" | jq --slurpfile kit "$KIT_SETTINGS_FILTERED" '
. as $existing |
$kit[0] as $new |
($new.hooks // {}) as $kh |
.hooks = (
((.hooks // {}) | to_entries) + ($kh | to_entries)
| group_by(.key)
| map({
key: .[0].key,
value: [.[].value[]] | unique_by(.hooks[0].command // "")
})
| from_entries
)
| .permissions = ((.permissions // {}) + {deny: (((.permissions.deny // []) + ($new.permissions.deny // [])) | unique)})
' 2>/dev/null)
if [ -n "$MERGED" ] && echo "$MERGED" | jq '.' >/dev/null 2>&1; then
echo "$MERGED" | jq '.' > "$SETTINGS_FILE"
echo "[ok] Merged hooks into $SETTINGS_FILE (existing hooks preserved)"
else
echo "[warn] jq merge failed. Manual merge needed."
echo " Copy hooks from $KIT_DIR/settings.json into $SETTINGS_FILE"
fi
else
echo "[warn] jq not installed. Cannot auto-merge settings.json."
echo " Install jq: brew install jq (macOS) or apt install jq (Linux)"
echo " Then re-run this script, or manually merge $KIT_DIR/settings.json"
fi
fi
rm -f "$KIT_SETTINGS_FILTERED"
# 2b. Render the install kit.toml (SPEC-183): the repo-root default (full,
# status-tagged schema) rendered into the live install, recomputing ONLY the
# [modules] section from this run's enabled set -- still a shell-install RECORD
# driving the wiring above, never a runtime registry (no hook reads this file;
# see the standing lint in tests/test-install-modules.sh). Every other section