Repository navigation
Expand file tree
/
Copy pathinstall.sh
More file actions
executable file
·1068 lines (1016 loc) · 59.6 KB
/
Copy pathinstall.sh
File metadata and controls
executable file
·1068 lines (1016 loc) · 59.6 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
# install.sh — install deep-code-review (and optional overlays) into a project.
#
# Usage:
# ./install.sh [--minimal] [--with-codex] [TARGET_DIR]
# ./install.sh --with-delivery [--with-critic] [TARGET_DIR]
# ./install.sh --full [TARGET_DIR]
# ./install.sh --with-gates [TARGET_DIR]
# ./install.sh --with-delivery --with-operating-layer [TARGET_DIR]
# ./install.sh --recommend [TARGET_DIR]
# ./install.sh --dry-run [other flags] [TARGET_DIR] (writes nothing; lists what would be written)
#
# Default (agent-agnostic): copies the REVIEW skill into every common skill
# root the major hosts discover, and writes/refreshes a root AGENTS.md pointer:
# - .agents/skills/deep-code-review/ (Agent Skills / open standard)
# - .cursor/skills/deep-code-review/ (Cursor)
# - .claude/skills/deep-code-review/ (Claude Code; Cursor/Codex also load)
# Optional hosts:
# --with-codex also .codex/skills/
# --with-extra-hosts also .gemini .opencode .github .windsurf .hermes .kiro .cline .roo .factory .qwen .grok
# Overlay skills (opt-in; default stays review-only):
# --with-delivery also agentic-delivery
# --with-terse-replies with --with-delivery: write a Stop-hook snippet (terse_reply_check.py, opt-in)
# --with-critic also idea-critic
# --with-comms also communication-structure
# --with-contribution also contribution (prepare upstream PRs; not in --full)
# --with-discovery also product-discovery (worth-building / PMF / prioritization; not in --full)
# --with-ceo also agentic-ceo (suite conductor: routing + effort-sizing + chaos playbook; not in --full)
# --with-growth also growth-analytics (North Star + AARRR + event taxonomy; not in --full)
# --with-positioning also positioning (value prop / message house; a hypothesis, never fabricated market facts; not in --full)
# --with-business also business-ops (leads with the call on your pricing/unit-economics numbers; routes the binding legal/tax/securities part to a professional; not in --full)
# --with-output-safety also product-output-safety (govern harm from the product's own AI outputs; HITL on high-stakes actions; not in --full)
# --full review + delivery + critic + comms
# --recommend inspect TARGET, print a pack, install nothing
# CI enforcement (mechanism, not a skill; opt-in; never overwrites a file):
# --with-gates also write .github/workflows/dcr-gates.yml +
# scripts/dcr-gates.sh, wiring the INSTALLED skill's
# own gate scripts (fix_class_gate, binaries_gate)
# into the target's own CI; and .githooks/pre-push, a
# local hook that reruns the fast lint+unit tier
# before every push (opt in yourself: git config
# core.hooksPath .githooks -- never run by this
# script). Prints (does not write) a settings.json
# snippet for agentic-delivery's SubagentStop
# handback_cap hook and its SubagentStart
# subagent_start_inject hook.
# Narrow:
# --minimal only .claude/skills/ + AGENTS.md
# --claude-only only .claude/skills/ ; skip AGENTS.md
# --with-cursor no-op (Cursor path is now part of the default set)
#
# Source of truth in the upstream repo remains the trees under
# .claude/skills/<name>/ — install copies *from* there; never duplicates
# a skill inside the upstream repository itself.
#
# Backups land under <TARGET>/.<host>/skill-backups/ —
# never inside skills/, so backups are never loaded as duplicate skills.
# No network, no sudo, fully local and reversible.
set -euo pipefail
usage() {
cat >&2 <<'EOF'
install.sh — install deep-code-review for any coding agent.
Usage:
./install.sh [--minimal] [--with-codex] [--claude-only] [TARGET_DIR]
./install.sh --with-delivery [--with-critic] [TARGET_DIR]
./install.sh --full [TARGET_DIR]
./install.sh --recommend [TARGET_DIR]
Default: install REVIEW ONLY into .agents/skills/, .cursor/skills/, and
.claude/skills/, plus an AGENTS.md pointer (version-stamped; refreshed
on re-install). Overlay skills are opt-in.
--minimal Only .claude/skills/ + AGENTS.md
--policy dim=value[,dim=value] Write .perun/policy.json (e.g. local_cpu=maximize,github_actions=off,share_learnings=auto;
role=ceo warns and role=ceo-strict blocks when the MAIN session hand-edits source, never a subagent)
--with-codex Also .codex/skills/
--with-extra-hosts Also Gemini, OpenCode, Copilot, Windsurf, Hermes, Kiro, Cline, Roo Code, Factory Droid, Qwen Code, Grok Build
--claude-only Only .claude/skills/; skip AGENTS.md
--with-cursor Accepted as no-op (Cursor path is default now)
--with-delivery Also install agentic-delivery (gated delivery overlay)
--with-terse-replies With --with-delivery: write a Stop-hook snippet that asks for terser rewrites (opt-in)
--with-critic Also install idea-critic (pre-owner idea attack)
--with-comms Also install communication-structure (BLUF messages)
--with-contribution Also install contribution (prepare upstream PRs; default off, not in --full)
--with-discovery Also install product-discovery (worth-building / PMF / prioritization; default off, not in --full)
--with-ceo Also install agentic-ceo (suite conductor: routing, effort-sizing, chaos playbook; default off, not in --full)
--with-growth Also install growth-analytics (North Star + AARRR + event taxonomy; default off, not in --full)
--with-positioning Also install positioning (value prop / message house; a hypothesis, never fabricated market facts; default off, not in --full)
--with-business Also install business-ops (leads with the call on your pricing/unit-economics numbers; routes the binding legal/tax/securities part to a professional; default off, not in --full)
--tracker-project ID Write a <=12-line "Work tracking" block into AGENTS.md (idempotent; replayed by update-installed)
--tracker NAME Tracker for that block: linear (default), github or jira
--with-output-safety Also install product-output-safety (govern harm from the product's own AI outputs; HITL on high-stakes actions; default off, not in --full)
--full Review + delivery + critic + comms
--with-gates Write .github/workflows/dcr-gates.yml + scripts/dcr-gates.sh,
wiring the INSTALLED skill's own gate scripts (fix_class_gate,
binaries_gate) into the target's own CI, plus
.githooks/pre-push (reruns the fast lint+unit tier before every
push -- opt in yourself with `git config core.hooksPath
.githooks`, never run by this script); never overwrites an
existing file at any of these paths (writes .new instead).
See --with-operating-layer for the SubagentStart/SubagentStop
settings snippet. Mechanism, not a skill.
--no-operating-layer Opt out of the default-on operating layer (applied with --with-delivery/--full).
--with-operating-layer
Write .claude/settings.operating-layer.json.new: the
heavy_gate.py PreToolUse and session_brief.py SessionStart hooks, the
ceo_altitude_guard.py hook (an applied install writes policy role=ceo, warn, unless the policy has a role or PERUN_ROLE is set; role=off silences it, ceo-strict blocks), the
outcome_claim_guard.py Stop + UserPromptSubmit + PreToolUse hook (unmeasured "fixed/faster" claims; policy outcome_guard), the
stall_guard.py Stop hook (one nudge when a turn ends "stopped / waiting on you" with no lane launched; PERUN_STALL_GUARD=off), the
landing_watchdog.py UserPromptSubmit + PreToolUse(Agent|Task) hook (nothing merged for an hour while green PRs wait; policy landing_watchdog), the
SubagentStart house-default injector, the
SubagentStop handback_cap, and the subagent model pin
(agentic-delivery/references/operating-discipline.md).
Never overwrites an existing file at that path (writes
.new-1, .new-2, ... instead). Requires --with-delivery.
--apply-operating-layer
Instead of writing a .new snippet, jq-merge the operating-layer
hooks/env (plus model "sonnet" if unset) into
TARGET/.claude/settings.local.json idempotently (backup first;
fails closed if jq is missing) and write
.claude/agents/delivery-lane.md if absent. Implies
--with-operating-layer; requires --with-delivery.
--no-sandbox Leave the sandbox and deny rules OUT of the operating layer.
By default it sets sandbox.enabled=true,
sandbox.allowUnsandboxedCommands=false and denies
Bash(rm -rf *), (rm -fr *), (rm -r *), (rm -R *), (sudo *).
RISK: without the OS sandbox nothing stops an agent's
`rm -rf "$EMPTY"/*` from deleting your files; deny rules
match command text only (bypassable via /bin/rm, bash -c).
--no-auto-update Write auto_update: "off" to TARGET/.perun/policy.json. By default the
operating layer's SessionStart/UserPromptSubmit hook checks the newest
vX.Y.Z tag at most every 6h in the background and re-applies this
install from it (scripts/update-installed.sh; your settings win).
--global-rules Owner rules for EVERY repo: copy reply_cap, decision_gate, handback_cap and
subagent_start_inject to ~/.claude/perun-rules/ and idempotently merge their
hooks into ~/.claude/settings.json (backup first; needs jq). Writes nothing
else and ignores TARGET. Already in a repo's operating layer? Use one or the other.
--recommend Inspect TARGET and print a recommended pack; no writes
--dry-run Install into a throwaway empty directory and list the files it would write
(vendored vs generated); TARGET is never touched. Existing files in TARGET would
additionally be backed up or get a .new sibling.
-h, --help Show this help
TARGET_DIR defaults to the current working directory.
An agent should run --recommend, tell the owner the pack, and wait for a
yes before --with-delivery / --full. Default install stays review-only so
a second delivery OS already in the project is not doubled.
EOF
}
SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
REVIEW_NAME="deep-code-review"
WRITE_AGENTS=1
MINIMAL=0
WITH_CODEX=0
WITH_EXTRA=0
WITH_DELIVERY=0
NO_SANDBOX=0
WITH_CRITIC=0
WITH_COMMS=0
WITH_CONTRIBUTION=0
WITH_DISCOVERY=0
WITH_CEO=0
WITH_GROWTH=0
WITH_POSITIONING=0
WITH_BUSINESS=0
WITH_OUTPUT_SAFETY=0
WITH_GATES=0
WITH_TERSE=0
WITH_OPERATING_LAYER=0
APPLY_OPLAYER=0
NO_OPLAYER=0
NO_AUTO_UPDATE=0
GLOBAL_RULES=0
RECOMMEND_ONLY=0
DRY_RUN=0
POSITIONAL=()
TRACKER_PROJECT=""
TRACKER="linear"
POLICY=""
NEXT_OPT=""
for arg in "$@"; do
if [[ -n "${NEXT_OPT}" ]]; then
printf -v "${NEXT_OPT}" '%s' "${arg}"; NEXT_OPT=""; continue
fi
case "${arg}" in
--tracker-project) NEXT_OPT=TRACKER_PROJECT ;;
--tracker) NEXT_OPT=TRACKER ;;
--policy) NEXT_OPT=POLICY ;;
--policy=*) POLICY="${arg#*=}" ;;
--tracker-project=*) TRACKER_PROJECT="${arg#*=}" ;;
--tracker=*) TRACKER="${arg#*=}" ;;
--claude-only) WRITE_AGENTS=0; MINIMAL=1 ;;
--minimal) MINIMAL=1 ;;
--with-codex) WITH_CODEX=1 ;;
--with-extra-hosts) WITH_EXTRA=1 ;;
--with-delivery) WITH_DELIVERY=1 ;;
--with-critic) WITH_CRITIC=1 ;;
--with-comms) WITH_COMMS=1 ;;
--with-contribution) WITH_CONTRIBUTION=1 ;;
--with-discovery) WITH_DISCOVERY=1 ;;
--with-ceo) WITH_CEO=1 ;;
--with-growth) WITH_GROWTH=1 ;;
--with-positioning) WITH_POSITIONING=1 ;;
--with-business) WITH_BUSINESS=1 ;;
--with-output-safety) WITH_OUTPUT_SAFETY=1 ;;
--with-gates) WITH_GATES=1 ;;
--with-terse-replies) WITH_TERSE=1 ;;
--with-operating-layer) WITH_OPERATING_LAYER=1 ;;
--apply-operating-layer) WITH_OPERATING_LAYER=1; APPLY_OPLAYER=1 ;;
--no-sandbox) NO_SANDBOX=1 ;;
--no-operating-layer) NO_OPLAYER=1 ;;
--no-auto-update) NO_AUTO_UPDATE=1 ;;
--global-rules) GLOBAL_RULES=1 ;;
--full) WITH_DELIVERY=1; WITH_CRITIC=1; WITH_COMMS=1 ;;
--recommend) RECOMMEND_ONLY=1 ;;
--dry-run) DRY_RUN=1 ;;
--with-cursor) echo "note: --with-cursor is default now; ignoring." >&2 ;;
-p|--portable) echo "note: --portable is default; ignoring (use --minimal / --claude-only to narrow)." >&2 ;;
-h|--help) usage; exit 0 ;;
-*) echo "error: unknown option: ${arg}" >&2; usage; exit 2 ;;
*) POSITIONAL+=("${arg}") ;;
esac
done
if [[ -n "${NEXT_OPT}" ]]; then echo "error: --$(printf %s "${NEXT_OPT//_/-}" | tr A-Z a-z) needs a value" >&2; exit 2; fi
# values land in AGENTS.md: allow only ID-safe characters
if [[ -n "${TRACKER_PROJECT}" && ! "${TRACKER_PROJECT}" =~ ^[A-Za-z0-9._-]+$ ]]; then
echo "error: --tracker-project must match [A-Za-z0-9._-]+" >&2; exit 2
fi
case "${TRACKER}" in linear|github|jira) ;; *) echo "error: --tracker must be linear, github or jira" >&2; exit 2 ;; esac
# Global owner rules (reply cap, decision gate, subagent narration): scripts to ~/.claude/perun-rules, hooks merged into
# ~/.claude/settings.json. A template hook is added only when no existing hook of that event already runs a script of the same name.
if [[ "${GLOBAL_RULES}" -eq 1 ]]; then
command -v jq >/dev/null 2>&1 || { echo "error: --global-rules needs jq (fail closed)" >&2; exit 1; }
GR_SRC="${SCRIPT_DIR}/.claude/skills/agentic-delivery"
GR_DIR="${HOME}/.claude/perun-rules"; GR_SET="${HOME}/.claude/settings.json"
mkdir -p "${GR_DIR}"
for f in reply_cap decision_gate handback_cap subagent_start_inject lane_liveness; do cp "${GR_SRC}/scripts/${f}.py" "${GR_DIR}/${f}.py"; done
[[ -s "${GR_SET}" ]] || echo '{}' > "${GR_SET}" # missing or 0-byte settings.json = {}
GR_NEW="$(mktemp "${TMPDIR:-/tmp}/perun-gr.XXXXXX")"
jq --argjson t "$(cat "${GR_SRC}/templates/global-rules.settings.json")" '
def nm: .hooks[0].command | capture("(?<n>[a-z_]+\\.py)").n;
reduce ($t.hooks | to_entries[]) as $e (.;
.hooks[$e.key] = ((.hooks[$e.key] // []) as $a
| $a + [$e.value[] | select(nm as $n | [$a[]?.hooks[]?.command // empty | contains($n)] | any | not)]))' \
"${GR_SET}" > "${GR_NEW}" || { rm -f "${GR_NEW}"; echo "error: could not merge ${GR_SET} (left unchanged)" >&2; exit 1; }
if cmp -s "${GR_NEW}" "${GR_SET}" || [[ "$(jq -S . "${GR_NEW}")" == "$(jq -S . "${GR_SET}")" ]]; then
rm -f "${GR_NEW}"; echo "global rules already installed in ${GR_SET}; scripts refreshed in ${GR_DIR}"
else
cp "${GR_SET}" "${GR_SET}.bak.$(date +%Y%m%d%H%M%S)"; cat "${GR_NEW}" > "${GR_SET}"; rm -f "${GR_NEW}" # write through: keeps a symlinked settings.json
echo "global rules merged into ${GR_SET} (backup saved); restart open sessions to load the hooks"
fi
python3 "${GR_SRC}/scripts/host_installer.py" applied global-rules >/dev/null 2>&1 || true # clears the "Installer unapplied" alarm
exit 0
fi
if [[ "${WITH_OPERATING_LAYER}" -eq 1 && "${WITH_DELIVERY}" -eq 0 ]]; then
echo "error: --with-operating-layer requires --with-delivery (writes an agentic-delivery snippet)" >&2
exit 1
fi
if [[ "${WITH_TERSE}" -eq 1 && "${WITH_DELIVERY}" -eq 0 ]]; then
echo "error: --with-terse-replies requires --with-delivery (the hook script ships with agentic-delivery)" >&2
exit 1
fi
if [[ "${NO_OPLAYER}" -eq 1 && "${WITH_OPERATING_LAYER}" -eq 1 ]]; then
echo "error: --no-operating-layer conflicts with --with/--apply-operating-layer" >&2
exit 1
fi
# Default-on: delivery without its operating layer installs hooks that never run (field failure).
if [[ "${WITH_DELIVERY}" -eq 1 && "${WITH_OPERATING_LAYER}" -eq 0 && "${NO_OPLAYER}" -eq 0 ]]; then
WITH_OPERATING_LAYER=1
if command -v jq >/dev/null 2>&1; then APPLY_OPLAYER=1
else echo "note: jq not found; writing the operating-layer snippet to merge by hand instead of applying it" >&2; fi
fi
if [[ "${APPLY_OPLAYER}" -eq 1 ]] && ! command -v jq >/dev/null 2>&1; then
echo "error: --apply-operating-layer needs jq (fail closed); install jq or use --with-operating-layer" >&2
exit 1
fi
TARGET_DIR="${POSITIONAL[0]:-$(pwd)}"
POLICY_PY="${SCRIPT_DIR}/.claude/skills/agentic-delivery/scripts/perun_policy.py"
if [[ ! -d "${TARGET_DIR}" ]]; then
echo "error: target directory does not exist: ${TARGET_DIR}" >&2
exit 1
fi
if [[ -n "${POLICY}" ]]; then # shape-check before touching anything; values are validated when written
_seen=,
IFS=, read -ra _kv <<< "${POLICY}"
for kv in "${_kv[@]}"; do
if [[ ! "${kv}" =~ ^([A-Za-z_]+)=(.+)$ || "${_seen}" == *",${BASH_REMATCH[1]},"* ]]; then
echo "error: --policy wants dim=value pairs, one per dim (got '${kv}')" >&2; exit 2
fi
_seen="${_seen}${BASH_REMATCH[1]},"
python3 "${POLICY_PY}" check "${kv%%=*}" "${kv#*=}" || exit 2
done
fi
if [[ "$(cd "${TARGET_DIR}" && pwd)" == "${SCRIPT_DIR}" ]]; then
echo "error: target is the repository itself; choose another project directory." >&2
exit 1
fi
if [[ "${RECOMMEND_ONLY}" -eq 1 ]]; then
exec python3 "${SCRIPT_DIR}/scripts/recommend-overlays.py" "${TARGET_DIR}"
fi
# Files this run writes are those newer than STAMP; the summary at the end lists them. --dry-run installs into a
# throwaway empty dir (stdout muted, machine caches redirected into it) so the real TARGET is never touched.
STAMP="$(mktemp "${TMPDIR:-/tmp}/perun-stamp.XXXXXX")"
REAL_TARGET="${TARGET_DIR}"
if [[ "${DRY_RUN}" -eq 1 ]]; then
SIM="$(mktemp -d "${TMPDIR:-/tmp}/perun-dry.XXXXXX")"
export XDG_CACHE_HOME="${SIM}.cache" DCR_NO_PROBE=1 # the sandbox probe writes under $HOME and calls the network
TARGET_DIR="${SIM}"
exec 3>&1 1>/dev/null
fi
cleanup_dry() {
rm -f "${STAMP}"
if [[ -n "${SIM:-}" && "${SIM}" == */perun-dry.* ]]; then
find "${SIM}" "${SIM}.cache" -depth -delete 2>/dev/null || true
fi
}
trap cleanup_dry EXIT
SRC_REVIEW="${SCRIPT_DIR}/.claude/skills/${REVIEW_NAME}"
if [[ ! -f "${SRC_REVIEW}/SKILL.md" ]]; then
echo "error: cannot find ${SRC_REVIEW}/SKILL.md" >&2
echo "Run this script from inside a cloned deep-code-review repository." >&2
exit 1
fi
VERSION="unknown"
if [[ -f "${SRC_REVIEW}/VERSION" ]]; then
VERSION="$(tr -d '[:space:]' < "${SRC_REVIEW}/VERSION")"
fi
INSTALL_SHA="unknown"
if git -C "${SCRIPT_DIR}" rev-parse --is-inside-work-tree >/dev/null 2>&1; then
INSTALL_SHA="$(git -C "${SCRIPT_DIR}" rev-parse --short HEAD)"
fi
COMMAND_FILES=() # files install wrote fresh (commands, house-defaults.md, settings .bak): hashed into the uninstall record
BACKUP_DIRS=() # skill-backups dirs this run created and kept (recorded in the marker for uninstall)
install_skill_copy() {
local skill_name="$1"
local dest="$2"
local backup_root="$3"
local src="${SCRIPT_DIR}/.claude/skills/${skill_name}"
if [[ ! -f "${src}/SKILL.md" ]]; then
echo "error: cannot find ${src}/SKILL.md" >&2
exit 1
fi
mkdir -p "$(dirname "${dest}")"
# One canonical copy (.claude); every other host symlinks to it so copies cannot drift. DCR_COPY_HOSTS=1 (or a failed ln) falls back to a real copy that perun_doctor.py hash-checks.
local canon="${TARGET_DIR}/.claude/skills/${skill_name}" want_link=0 rel="../../.claude/skills/${skill_name}"
[[ "${dest}" != "${TARGET_DIR}/.claude/"* && -z "${DCR_COPY_HOSTS:-}" && -d "${canon}" ]] && want_link=1
if [[ "${want_link}" -eq 1 && -L "${dest}" && "$(readlink "${dest}")" == "${rel}" ]]; then
echo "linked: ${skill_name} (unchanged) -> ${dest}"; return
fi
# Our own host link is not user data: drop it instead of backing it up (copy mode would otherwise add a backup per re-run).
[[ -L "${dest}" && "$(readlink "${dest}")" == "${rel}" ]] && rm -f "${dest}"
local backup=""
if [[ -e "${dest}" || -L "${dest}" ]]; then
# Fail closed: a symlinked backup dir would move the user's skill (or later delete files) outside the target.
python3 "${SCRIPT_DIR}/scripts/perun_marker.py" backup-ok "${TARGET_DIR}" "${backup_root}" \
|| { echo "error: ${backup_root} (or a parent below ${TARGET_DIR}) is a symlink; refusing to back up into it. Replace it with a real directory and re-run." >&2; exit 1; }
mkdir -p "${backup_root}"
# Portable, collision-free backup name: a seconds-resolution timestamp plus
# an existence-checked numeric suffix, so rapid repeated installs within the
# same second never reuse a name. Avoids `date +%N` (unsupported on BSD/macOS
# date), which is why a plain timestamp alone would collide.
backup="${backup_root}/${skill_name}-$(date +%Y%m%d-%H%M%S)"
if [[ -e "${backup}" ]]; then
local n=1
while [[ -e "${backup}-${n}" ]]; do
n=$((n + 1))
done
backup="${backup}-${n}"
fi
echo "note: existing skill found -> backing up to ${backup}"
mv "${dest}" "${backup}"
fi
local linked=0 cmp_dir="${dest}"
if [[ "${want_link}" -eq 1 ]] && ln -s "${rel}" "${dest}" 2>/dev/null; then linked=1; cmp_dir="${canon}"; fi
[[ "${linked}" -eq 1 ]] || cp -R "${src}" "${dest}"
# Never ship local Python bytecode caches (created by running a script selftest).
[[ "${linked}" -eq 1 ]] || find "${dest}" -name __pycache__ -type d -prune -exec rm -rf {} +
# Support docs the review skill cites — must resolve after install.
if [[ "${linked}" -eq 0 && "${skill_name}" == "${REVIEW_NAME}" ]]; then
mkdir -p "${dest}/references"
if [[ -f "${SCRIPT_DIR}/docs/standards-index.md" ]]; then
cp "${SCRIPT_DIR}/docs/standards-index.md" "${dest}/references/standards-index.md"
fi
if [[ -f "${SCRIPT_DIR}/docs/example-review-report.md" ]]; then
cp "${SCRIPT_DIR}/docs/example-review-report.md" "${dest}/references/example-review-report.md"
fi
fi
if [[ -n "${backup}" ]]; then
# A backup byte-identical to the fresh copy is just the shipped files again, and one equal to what the marker says the last install wrote
# (still the pre-record() marker here) is an earlier release nobody edited: drop either so re-runs and version bumps add nothing.
# Any other backup (an edited copy, a pre-Perun copy, your own files) is never deleted by install.
python3 "${SCRIPT_DIR}/scripts/perun_marker.py" dedupe "${backup_root}" "${backup}" "${cmp_dir}" "${TARGET_DIR}" "${dest#"${TARGET_DIR}"/}"
[[ ! -d "${backup}" ]] || BACKUP_DIRS+=("${backup#"${TARGET_DIR}"/}")
fi
local skill_ver="unknown"
if [[ -f "${src}/VERSION" ]]; then
skill_ver="$(tr -d '[:space:]' < "${src}/VERSION")"
fi
echo "installed: ${skill_name} ${skill_ver} (@ ${INSTALL_SHA}) -> ${dest}$([[ "${linked}" -eq 1 ]] && echo " (symlink to .claude)")"
}
# Host roots that receive every selected skill.
HOSTS=()
HOSTS+=(".claude")
if [[ "${MINIMAL}" -eq 0 ]]; then
HOSTS+=(".cursor" ".agents")
fi
if [[ "${WITH_CODEX}" -eq 1 ]]; then
HOSTS+=(".codex")
fi
if [[ "${WITH_EXTRA}" -eq 1 ]]; then
HOSTS+=(".gemini" ".opencode" ".github" ".windsurf" ".hermes" ".kiro" ".cline" ".roo" ".factory" ".qwen" ".grok")
fi
SKILLS=("${REVIEW_NAME}")
if [[ "${WITH_DELIVERY}" -eq 1 ]]; then
SKILLS+=("agentic-delivery")
fi
if [[ "${WITH_CRITIC}" -eq 1 ]]; then
SKILLS+=("idea-critic")
fi
if [[ "${WITH_COMMS}" -eq 1 ]]; then
SKILLS+=("communication-structure")
fi
if [[ "${WITH_CONTRIBUTION}" -eq 1 ]]; then
SKILLS+=("contribution")
fi
if [[ "${WITH_DISCOVERY}" -eq 1 ]]; then
SKILLS+=("product-discovery")
fi
if [[ "${WITH_CEO}" -eq 1 ]]; then
SKILLS+=("agentic-ceo")
fi
if [[ "${WITH_GROWTH}" -eq 1 ]]; then
SKILLS+=("growth-analytics")
fi
if [[ "${WITH_POSITIONING}" -eq 1 ]]; then
SKILLS+=("positioning")
fi
if [[ "${WITH_BUSINESS}" -eq 1 ]]; then
SKILLS+=("business-ops")
fi
if [[ "${WITH_OUTPUT_SAFETY}" -eq 1 ]]; then
SKILLS+=("product-output-safety")
fi
INSTALLED_DIRS=()
for skill in "${SKILLS[@]}"; do
for host in "${HOSTS[@]}"; do
INSTALLED_DIRS+=("${host}/skills/${skill}")
install_skill_copy \
"${skill}" \
"${TARGET_DIR}/${host}/skills/${skill}" \
"${TARGET_DIR}/${host}/skill-backups"
done
# One canonical copy (the first host's); every other host's copy must be byte-identical, else fail naming it.
# Guards partial or failed copies (cp interrupted, pre-existing leftovers); later drift is perun_doctor's job.
for host in "${HOSTS[@]:1}"; do
diff -rq "${TARGET_DIR}/${HOSTS[0]}/skills/${skill}" "${TARGET_DIR}/${host}/skills/${skill}" >/dev/null \
|| { echo "error: ${TARGET_DIR}/${host}/skills/${skill} differs from ${TARGET_DIR}/${HOSTS[0]}/skills/${skill}; vendored copies drifted" >&2; exit 1; }
done
done
# The generated entry index (one line per skill, command and script): the host rules point agents at it.
# Copied beside the skills of every host; refreshed on re-install; only a file written fresh is recorded for uninstall.
if [[ -f "${SCRIPT_DIR}/.claude/skills/INDEX.md" ]]; then
for host in "${HOSTS[@]}"; do
dest="${TARGET_DIR}/${host}/skills/INDEX.md"
[[ -e "${dest}" ]] && cmp -s "${SCRIPT_DIR}/.claude/skills/INDEX.md" "${dest}" && continue
existed=0; [[ -e "${dest}" ]] && existed=1
cp "${SCRIPT_DIR}/.claude/skills/INDEX.md" "${dest}"
[[ "${existed}" -eq 1 ]] || COMMAND_FILES+=("${host}/skills/INDEX.md")
done
fi
# Host safety: one warning line per installed host, from the single-source table (docs/host-safety.md).
HS_TSV="${SCRIPT_DIR}/.claude/skills/agentic-delivery/templates/host-safety.tsv"
if [[ -f "${HS_TSV}" ]]; then
for host in "${HOSTS[@]}"; do
while IFS=$'\t' read -r hs_dir hs_name hs_sb hs_warn hs_url; do
[[ "${hs_dir}" == "${host}" ]] && echo "safety: ${hs_name}: ${hs_warn} ${hs_url}" >&2
done < "${HS_TSV}"
done
fi
# write_gate_file <src> <dest> <executable:0|1> — copy a CI-gates template or
# slash command into the target. NEVER overwrites a file you may have edited: a
# <dest> that differs gets <dest>.new (or .new-N on a further collision) instead
# and a note so the owner reviews/merges by hand. Writes nothing when <dest>, or
# an existing <dest>.new / .new-N, already holds <src>, so a re-run adds no
# sibling. One exception: a <dest> the install marker recorded (install wrote
# it) and nobody has edited since is refreshed in place (a release bump, via
# perun_marker.py refresh). Nothing is deleted: a .new already there stays.
# GATE_WROTE=1 tells the caller it wrote <dest> itself, fresh or refreshed, so
# it can record the new sha256. This is the only writer for --with-gates output
# and slash commands, so the never-overwrite rule cannot drift between them.
GATE_WROTE=0
write_gate_file() {
local src="$1" dest="$2" make_exec="$3" f re='^\.new(-[0-9]+)?$'
GATE_WROTE=0
mkdir -p "$(dirname "${dest}")"
if [[ ! -e "${dest}" ]]; then
cp "${src}" "${dest}"
[[ "${make_exec}" -eq 1 ]] && chmod +x "${dest}"
GATE_WROTE=1
echo "installed: ${dest}"
return 0
fi
cmp -s "${src}" "${dest}" && return 0 # already current
if python3 "${SCRIPT_DIR}/scripts/perun_marker.py" refresh "${TARGET_DIR}" "${dest#"${TARGET_DIR}"/}" "${src}"; then
[[ "${make_exec}" -eq 1 ]] && chmod +x "${dest}"
GATE_WROTE=1
echo "updated: ${dest} (unedited since install)"
return 0
fi
for f in "${dest}.new" "${dest}".new-[0-9]*; do
if [[ "${f#"${dest}"}" =~ ${re} && -f "${f}" && ! -L "${f}" ]] && cmp -s "${src}" "${f}"; then # exact .new / .new-N names only
echo "note: ${dest} differs from the shipped file; ${f} already holds the shipped one (review and merge by hand)"
return 0
fi
done
local newdest="${dest}.new"
if [[ -e "${newdest}" ]]; then
local n=1
while [[ -e "${newdest}-${n}" ]]; do
n=$((n + 1))
done
newdest="${newdest}-${n}"
fi
cp "${src}" "${newdest}"
[[ "${make_exec}" -eq 1 ]] && chmod +x "${newdest}"
echo "note: ${dest} already exists -> wrote ${newdest} instead (review and merge by hand)"
}
if [[ "${WITH_GATES}" -eq 1 ]]; then
GATES_SRC="${SCRIPT_DIR}/.claude/skills/${REVIEW_NAME}/templates"
if [[ ! -f "${GATES_SRC}/dcr-gates.yml" || ! -f "${GATES_SRC}/dcr-gates.sh" || ! -f "${GATES_SRC}/pre-push-verify.sh" || ! -f "${GATES_SRC}/integrator-gate.sh" ]]; then
echo "error: cannot find ${GATES_SRC}/{dcr-gates.yml,dcr-gates.sh,pre-push-verify.sh,integrator-gate.sh}" >&2
exit 1
fi
write_gate_file "${GATES_SRC}/dcr-gates.yml" "${TARGET_DIR}/.github/workflows/dcr-gates.yml" 0
write_gate_file "${GATES_SRC}/dcr-gates.sh" "${TARGET_DIR}/scripts/dcr-gates.sh" 1
write_gate_file "${GATES_SRC}/pre-push-verify.sh" "${TARGET_DIR}/.githooks/pre-push" 1
write_gate_file "${GATES_SRC}/integrator-gate.sh" "${TARGET_DIR}/scripts/integrator-gate.sh" 1
cat <<'EOF'
CI enforcement wired (--with-gates), MINIMUM-COST PROFILE by default:
.github/workflows/dcr-gates.yml PR-into-main ONLY (ready_for_review +
synchronize; drafts skipped); no push
trigger, no schedule; SHA-pinned checkout,
workflow-level concurrency cancels a
superseded run
scripts/dcr-gates.sh calls the INSTALLED skill's own gate scripts
(fix_class_gate, binaries_gate) by resolved
path -- no duplicated gate logic, no copies
.githooks/pre-push reruns the fast lint+unit tier (DCR_PREPUSH_CMD)
before every push -- catches an unverified rebase-
conflict-resolution commit locally, before CI. Not
wired yet: run this yourself (not run by install.sh)
to opt in:
git config core.hooksPath .githooks
Then set DCR_PREPUSH_CMD (e.g. "make lint test-unit")
in your shell profile. It is self-report, not a
substitute for CI -- see the hook's own header.
scripts/integrator-gate.sh run this on an integration branch's merge
result instead of adding hosted CI to it --
see its own header and
agentic-delivery/references/host-enforcement.md's
"Minimum-cost CI & token profile"
Edit the workflow's trigger branch if this repo's default branch isn't `main`.
MINIMUM-COST PROFILE CHECKLIST -- these are OWNER actions; install.sh never
automates them (they need forge/org privileges this script does not have):
[ ] If this repo has a separate integration branch (development, staging,
...): add branch protection/ruleset restricting merge AND push to ONE
integrator identity -- a deterministic script (never an LLM session),
run from launchd/cron with its gate token in the OS keychain, plus a
documented break-glass path for running it by hand. That identity runs
`scripts/integrator-gate.sh --target-branch <branch>`, which pushes the
merge result to a scratch ref, gates it, and only on a pass
fast-forwards the branch -- never `[skip ci]` on that branch. Do NOT
add hosted CI to it.
[ ] If a required status check is needed on the integration branch: have
integrator-gate.sh post it using a SEPARATE, low-privilege token from
whatever pushes -- never the same identity (it would be forgeable).
[ ] Set a soft spend budget (GitHub Settings > Billing) with alerts at
50/80/100% of your cap -- never a hard stop that could block real CI.
[ ] Confirm plan/ownership before relying on GitHub's merge queue: it needs
a public repo, or a private repo owned by an org on GitHub Enterprise
Cloud -- Team, Pro, and personal-account repos get none of it.
[ ] Run `python3 .claude/skills/deep-code-review/scripts/ci_cost_lint.py --gate .`
before merging any new/changed workflow file.
[ ] Run `python3 .claude/skills/deep-code-review/scripts/ci_cost_advisor.py .` to
price your workflows in minutes and dollars (public repos on standard runners
are free) and get a local-first patch to review; it never edits a workflow.
Optional, only useful alongside --with-delivery: --with-operating-layer WRITES the
SubagentStop hand-back cap, the SubagentStart house-default injector, and
the subagent model pin as a settings.operating-layer.json.new snippet to merge by
hand -- see agentic-delivery/references/operating-discipline.md (the always-on
operating layer's one entry point) and its "Install and self-check" section.
EOF
fi
if [[ "${WITH_TERSE}" -eq 1 ]]; then
# Opt-in Stop hook; written as a snippet to merge by hand, never applied automatically.
mkdir -p "${TARGET_DIR}/.claude"
cp "${SCRIPT_DIR}/.claude/skills/agentic-delivery/templates/terse-replies.settings.json" "${TARGET_DIR}/.claude/settings.terse-replies.json.new"
echo " terse-replies: .claude/settings.terse-replies.json.new written -- merge its Stop hook into settings (TERSE_MAX_RATIO tunes the 0.12 limit)"
fi
if [[ "${WITH_OPERATING_LAYER}" -eq 1 ]]; then
OPLAYER_SRC="${SCRIPT_DIR}/.claude/skills/agentic-delivery/templates/operating-layer.settings.json"
if [[ ! -f "${OPLAYER_SRC}" ]]; then
echo "error: cannot find ${OPLAYER_SRC}" >&2
exit 1
fi
OPLAYER_JSON="$(cat "${OPLAYER_SRC}")"
if [[ "${NO_SANDBOX}" -eq 1 ]]; then
command -v jq >/dev/null || { echo "error: --no-sandbox needs jq (fail closed)" >&2; exit 1; }
OPLAYER_JSON="$(jq -c 'del(.sandbox, .permissions)' "${OPLAYER_SRC}")"
echo "warning: --no-sandbox: operating layer has NO sandbox and NO rm/sudo deny rules; an agent can delete anything you can" >&2
fi
OPLAYER_DEST="${TARGET_DIR}/.claude/settings.operating-layer.json.new"
mkdir -p "$(dirname "${OPLAYER_DEST}")"
# Default house-defaults file the SubagentStart injector reads (it fails open when absent: lanes ran uncapped). Only if absent.
OPLAYER_HOUSE="${TARGET_DIR}/.claude/house-defaults.md"
if [[ "${APPLY_OPLAYER}" -eq 1 && -L "${OPLAYER_HOUSE}" ]]; then
echo "warning: ${OPLAYER_HOUSE} is a symlink; not writing a default house-defaults file (fail closed)" >&2
elif [[ "${APPLY_OPLAYER}" -eq 1 && ! -e "${OPLAYER_HOUSE}" ]]; then
cat > "${OPLAYER_HOUSE}" <<'HOUSE'
Lane budget: stay inside the lane token cap. Read only the line ranges you need, never re-read a file you just edited, and stop as soon as the task is done.
Optional chat voice: add your own line here; compressed assistant prose is available from the public caveman repository. Code, commits, PR bodies and docs stay normal prose.
HOUSE
COMMAND_FILES+=(".claude/house-defaults.md")
echo "wrote default ${OPLAYER_HOUSE} (edit freely; an existing file is never overwritten)"
fi
if [[ "${APPLY_OPLAYER}" -eq 1 ]]; then
OPLAYER_LOCAL="${TARGET_DIR}/.claude/settings.local.json"
[[ -f "${OPLAYER_LOCAL}" ]] || { echo '{}' > "${OPLAYER_LOCAL}"; OPLAYER_LOCAL_NEW=1; }
OPLAYER_PRE="$(mktemp "${TMPDIR:-/tmp}/perun.XXXXXX")"; cp "${OPLAYER_LOCAL}" "${OPLAYER_PRE}"
# Append template hook entries not already present; existing env/model win.
# dm: recursive merge, existing (right) values win, arrays unioned (existing order first).
# autoCompactWindow 200000: only when no user/project/local settings file sets it (local would shadow a user's value) and policy auto_compact is not off.
OPLAYER_AC=200000
[[ ",${POLICY}," == *,auto_compact=off,* ]] || grep -qs '"auto_compact"[[:space:]]*:[[:space:]]*"off"' "${TARGET_DIR}/.perun/policy.json" && OPLAYER_AC=""
# statusLine (cost, context and cache-health line from cost_statusline.py): same rule, a user's own statusLine always wins.
OPLAYER_SL=1
[[ ",${POLICY}," == *,statusline=off,* ]] || grep -qs '"statusline"[[:space:]]*:[[:space:]]*"off"' "${TARGET_DIR}/.perun/policy.json" && OPLAYER_SL=""
for _f in "${CLAUDE_CONFIG_DIR:-${HOME}/.claude}/settings.json" "${TARGET_DIR}/.claude/settings.json" "${OPLAYER_LOCAL}"; do
[[ -f "${_f}" ]] || continue
if ! jq -e 'type == "object"' "${_f}" >/dev/null 2>&1; then # invalid JSON/JSONC: cannot tell whether the user set it, so fail closed
[[ -n "${OPLAYER_AC}" ]] && echo "note: ${_f} is not plain JSON; autoCompactWindow not written (set it there yourself)" >&2
[[ -n "${OPLAYER_SL}" ]] && echo "note: ${_f} is not plain JSON; statusLine not written (set it there yourself)" >&2
OPLAYER_AC=""; OPLAYER_SL=""
else
jq -e 'has("autoCompactWindow")' "${_f}" >/dev/null 2>&1 && OPLAYER_AC=""
jq -e 'has("statusLine")' "${_f}" >/dev/null 2>&1 && OPLAYER_SL=""
fi
done
jq --argjson t "${OPLAYER_JSON}" --arg ac "${OPLAYER_AC}" --arg sl "${OPLAYER_SL}" '
def dm($a; $b): if ($a|type) == "object" and ($b|type) == "object"
then reduce ($a + $b | keys_unsorted[]) as $k ({}; .[$k] = (if ($a|has($k)) and ($b|has($k)) then dm($a[$k]; $b[$k]) elif ($b|has($k)) then $b[$k] else $a[$k] end))
elif ($a|type) == "array" and ($b|type) == "array" then $b + ($a - $b) else $b end;
# Upgrade old relative hook paths in place (cwd-dependent: break outside the repo root), so the union below sees no duplicate.
def up: sub("python3 (?<p>\\.claude/skills/[A-Za-z0-9_./-]+\\.py)"; "python3 \"$CLAUDE_PROJECT_DIR/\(.p)\"")
| sub("HOUSE_DEFAULTS_FILE=(?<p>\\.claude/[A-Za-z0-9_./-]+)"; "HOUSE_DEFAULTS_FILE=\"$CLAUDE_PROJECT_DIR/\(.p)\"");
(.hooks[]?[]?.hooks[]? | select((.command? | type) == "string") | .command) |= up
# The heavy_gate entry carries its own timeout, so an old copy (5s) would sit beside the template one and kill the in-hook wait: drop it unless it equals the template entry, the union below re-adds the current one.
| (if (.hooks.PreToolUse? | type) == "array" then .hooks.PreToolUse |= map(select((([.hooks[]?.command? // ""] | any(contains("heavy_gate.py"))) | not) or (. as $x | ($t.hooks.PreToolUse // []) | index([$x])))) else . end)
| reduce ($t.hooks | keys[]) as $e (.;
(.hooks[$e] // []) as $a
| .hooks[$e] = $a + ($t.hooks[$e] | map(select(. as $x | $a | index([$x]) | not))))
| .env = ($t.env + (.env // {}))
| (if $t.sandbox then .sandbox = dm($t.sandbox; .sandbox // {}) else . end)
| (if $t.permissions then .permissions = dm($t.permissions; .permissions // {}) else . end)
| .model //= "sonnet"
| (if $ac != "" then .autoCompactWindow = ($ac | tonumber) else . end)
| (if $sl != "" and $t.statusLine then .statusLine = $t.statusLine else . end)' "${OPLAYER_LOCAL}" > "${OPLAYER_LOCAL}.tmp"
if cmp -s "${OPLAYER_LOCAL}" "${OPLAYER_LOCAL}.tmp"; then
rm -f "${OPLAYER_LOCAL}.tmp"
else
OPLAYER_BAK="${OPLAYER_LOCAL}.bak.$(date +%Y%m%d-%H%M%S)" # timestamped: never overwrite an earlier backup
OPLAYER_N=1; while [[ -e "${OPLAYER_BAK}" ]]; do OPLAYER_BAK="${OPLAYER_LOCAL}.bak.$(date +%Y%m%d-%H%M%S)-${OPLAYER_N}"; OPLAYER_N=$((OPLAYER_N + 1)); done
cp "${OPLAYER_LOCAL}" "${OPLAYER_BAK}"
COMMAND_FILES+=("${OPLAYER_BAK#"${TARGET_DIR}"/}")
mv "${OPLAYER_LOCAL}.tmp" "${OPLAYER_LOCAL}"
fi
OPLAYER_AGENT="${TARGET_DIR}/.claude/agents/delivery-lane.md"
if [[ ! -e "${OPLAYER_AGENT}" ]]; then
OPLAYER_AGENT_NEW=1
mkdir -p "$(dirname "${OPLAYER_AGENT}")"
cat > "${OPLAYER_AGENT}" <<'AGENT'
---
name: delivery-lane
description: Default delivery lane for bounded implementation work. Terse, cheap, one-line hand-back.
model: sonnet
---
Do the assigned bounded task with the simplest change that works. Read only the line ranges you need. Run the project's own gates before claiming done. Final message is one line: status, evidence (sha/path/test), next step.
AGENT
fi
echo "applied operating layer to ${OPLAYER_LOCAL} (backup: .bak.<timestamp> if changed); verify with:"
if [[ "${NO_SANDBOX}" -eq 0 ]]; then
echo " what changed (sandbox): sandbox.enabled=true, sandbox.allowUnsandboxedCommands=false, autonomy-ready defaults (gh excluded with read-only gh subcommands allowed, common dev hosts, local binding, ~/.cache and ~/.npm writes; existing values win, lists unioned), deny Bash(rm -rf *), Bash(rm -fr *), Bash(rm -r *), Bash(rm -R *), Bash(sudo *) and gh alias/extension/repo delete/release delete; opt out with --no-sandbox"
echo " why each default: docs/host-safety.md#autonomy-ready-defaults"
echo " sandbox blocks gh (x509 -26276) or a dev server (EPERM)? see docs/host-safety.md#common-sandbox-errors"
fi
echo " python3 .claude/skills/agentic-delivery/scripts/operating_selfcheck.py --settings .claude/settings.local.json"
else
if [[ -e "${OPLAYER_DEST}" ]]; then
OPLAYER_N=1
while [[ -e "${OPLAYER_DEST}-${OPLAYER_N}" ]]; do
OPLAYER_N=$((OPLAYER_N + 1))
done
OPLAYER_DEST="${OPLAYER_DEST}-${OPLAYER_N}"
fi
printf '%s\n' "${OPLAYER_JSON}" > "${OPLAYER_DEST}"
echo "wrote ${OPLAYER_DEST} -- merge its \"hooks\"/\"env\" keys into ${TARGET_DIR}/.claude/settings.json by hand, then verify with:"
echo " python3 .claude/skills/agentic-delivery/scripts/operating_selfcheck.py"
fi
fi
# Slash commands: copy commands/*.md into .claude/commands/, but only those whose skill is installed.
# review, deliver always; cost-retro, perun, perun-run need agentic-ceo/agentic-delivery;
# critic, council need idea-critic; idea needs idea-critic and agentic-delivery.
# Never overwrites a file you edited (write_gate_file writes <dest>.new); a file this install wrote and you left alone is refreshed in place.
# Only files write_gate_file wrote itself (fresh or refreshed) go in the uninstall record, so a refresh updates the recorded sha256.
if [[ -d "${SCRIPT_DIR}/commands" ]]; then
for cmd in "${SCRIPT_DIR}"/commands/*.md; do
name="$(basename "${cmd}" .md)"
case "${name}" in
review|deliver) ;;
cost-retro|perun|perun-run) [[ "${WITH_CEO}" -eq 1 || "${WITH_DELIVERY}" -eq 1 ]] || continue ;;
critic|council) [[ "${WITH_CRITIC}" -eq 1 ]] || continue ;;
idea) [[ "${WITH_CRITIC}" -eq 1 && "${WITH_DELIVERY}" -eq 1 ]] || continue ;;
*) continue ;;
esac
dest="${TARGET_DIR}/.claude/commands/${name}.md"
write_gate_file "${cmd}" "${dest}" 0
[[ "${GATE_WROTE}" -eq 0 ]] || COMMAND_FILES+=(".claude/commands/${name}.md")
done
fi
# Marker: exactly what this install added, so perun_uninstall.py removes only that.
# Remote: PERUN_REMOTE, else this checkout's origin, else (tarball/plugin copy with no git origin) the canonical public repo, so auto-update always has one.
RECORD_REMOTE="${PERUN_REMOTE:-$(git -C "${SCRIPT_DIR}" remote get-url origin 2>/dev/null | sed -E 's#://[^/@]+@#://#' || true)}"
python3 "${SCRIPT_DIR}/scripts/perun_marker.py" "${TARGET_DIR}" --dirs "${INSTALLED_DIRS[@]}" ${COMMAND_FILES[@]+--files "${COMMAND_FILES[@]}"} \
${BACKUP_DIRS[@]+--backups "${BACKUP_DIRS[@]}"} ${OPLAYER_LOCAL_NEW:+--settings-created} \
${OPLAYER_PRE:+--pre "${OPLAYER_PRE}" --template "${SCRIPT_DIR}/.claude/skills/agentic-delivery/templates/operating-layer.settings.json"} \
${OPLAYER_AGENT_NEW:+--agent-created} --version "${VERSION}" \
--remote "${RECORD_REMOTE:-https://github.com/protocol/perun.git}"
if [[ "${NO_AUTO_UPDATE}" -eq 1 ]]; then
python3 - "${TARGET_DIR}/.perun/policy.json" <<'PY'
import json, sys
from pathlib import Path
p = Path(sys.argv[1])
pol = json.loads(p.read_text()) if p.is_file() else {}
pol["auto_update"] = "off"
p.parent.mkdir(parents=True, exist_ok=True)
p.write_text(json.dumps(pol, indent=2) + "\n")
PY
echo "auto-update: off (${TARGET_DIR}/.perun/policy.json)"
fi
if [[ "${WITH_OPERATING_LAYER}" -eq 1 && "${APPLY_OPLAYER}" -eq 1 ]]; then
OPLAYER_MODEL_NOTE="(model: sonnet, set only if you had none)"
if [[ -n "${OPLAYER_PRE:-}" ]] && jq -e 'has("model")' "${OPLAYER_PRE}" >/dev/null 2>&1; then
OPLAYER_MODEL_NOTE="(your existing model kept)"
fi
cat <<EOF
What changed in ${TARGET_DIR}:
1. Skills copied to .claude/skills (+ .cursor, .agents); any existing copy moved to <host>/skill-backups/.
2. .claude/settings.local.json: operating-layer hooks (PreToolUse incl. heavy_gate.py + lane_resume_nudge.py + landing_watchdog.py + ceo_altitude_guard.py + outcome_claim_guard.py, PostToolUse lane_cap.py, Stop outcome_claim_guard.py + stall_guard.py, SessionStart session_brief.py + handoff_inject.py + auto-update, UserPromptSubmit context_nudge.py + auto-update + ceo_altitude_guard.py + outcome_claim_guard.py + landing_watchdog.py, SubagentStart, SubagentStop), env (incl. LANE_TOOL_CAP=250), the model pin ${OPLAYER_MODEL_NOTE}, autoCompactWindow=200000 (only if no settings file sets it; policy auto_compact=off skips) and a cost statusLine (cost_statusline.py; only if no settings file sets one; policy statusline=off skips) merged in; previous file saved as .bak.<timestamp>.
Skills and permissions apply to open sessions without a restart; a hook change (new entries, or old relative paths upgraded to "\$CLAUDE_PROJECT_DIR" paths) and a model change reach already-running sessions only after a restart.
Auto-update: that hook runs release code from the Perun remote in the background (newest vX.Y.Z tag, at most every 6h).
Opt out: re-run install.sh with --no-auto-update, or set "auto_update": "off" in .perun/policy.json.
3. .claude/agents/delivery-lane.md added if absent; .claude/.perun-install.json and .dcr-install-flags record what was added.
4. Check it works: python3 ${SCRIPT_DIR}/scripts/perun_doctor.py ${TARGET_DIR}
5. Undo: python3 ${SCRIPT_DIR}/scripts/perun_uninstall.py ${TARGET_DIR} (opt out next time: --no-operating-layer)
EOF
fi
[[ -z "${OPLAYER_PRE:-}" ]] || rm -f "${OPLAYER_PRE}"
# Record the flags so scripts/update-installed.sh can replay this install. Flags that add capability or reduce
# safety (operating layer on, sandbox off) are machine-local and go to an untracked .dcr-install-flags.local so a committed record
# never replays one person's choice on another clone. Pack flags and the conservative opt-outs
# (--no-auto-update, --no-operating-layer) stay tracked so a team-wide opt-out travels.
mkdir -p "${TARGET_DIR}/.claude"
LOCAL_FLAGS="${TARGET_DIR}/.claude/.dcr-install-flags.local"
{ skip=0
for a in "$@"; do
if [[ "${skip}" -eq 1 ]]; then skip=0; continue; fi
case "${a}" in --tracker-project|--tracker|--tracker-project=*|--tracker=*) [[ "${a}" == *=* ]] || skip=1; continue ;; esac
case "${a}" in --with-operating-layer|--apply-operating-layer|--no-sandbox) continue ;; esac
[[ "${a}" == -* ]] && printf '%s\n' "${a}"
done
# tracker flags replay as single-token --opt=value lines (the marker is one arg per line)
[[ -n "${TRACKER_PROJECT}" ]] && printf -- '--tracker-project=%s\n--tracker=%s\n' "${TRACKER_PROJECT}" "${TRACKER}"
true; } > "${TARGET_DIR}/.claude/.dcr-install-flags"
for a in "$@"; do
case "${a}" in --with-operating-layer|--apply-operating-layer|--no-sandbox) printf '%s\n' "${a}" ;; esac
done > "${LOCAL_FLAGS}"
# Machine-local state never shows up as untracked in the target's git status: added to .git/info/exclude (idempotent),
# never to a tracked .gitignore. Backups of settings.local.json, the uninstall record and .perun/ are per machine.
git_exclude() {
local _ex _pre _pat
[[ "${DRY_RUN}" -eq 0 ]] || return 0 # the throwaway dir may sit inside some other repo: never touch its exclude file
_ex="$(git -C "${TARGET_DIR}" rev-parse --git-path info/exclude 2>/dev/null || true)"
[[ -z "${_ex}" || "${_ex}" == /* ]] || _ex="${TARGET_DIR}/${_ex}"
_pre="/$(git -C "${TARGET_DIR}" rev-parse --show-prefix 2>/dev/null || true)"
for _pat in "$@"; do
_pat="${_pre}${_pat}"
if [[ -n "${_ex}" ]] && ! grep -qxF "${_pat}" "${_ex}" 2>/dev/null; then
{ mkdir -p "$(dirname "${_ex}")" && printf '%s\n' "${_pat}" >> "${_ex}"; } 2>/dev/null || true
fi
done
}
git_exclude .claude/settings.local.json.bak.\* .claude/.perun-install.json .perun/
if [[ -s "${LOCAL_FLAGS}" ]]; then git_exclude .claude/.dcr-install-flags.local; else rm -f "${LOCAL_FLAGS}"; fi
# session_brief.py compares the installed version to this checkout's origin/main (machine-local, never committed).
if [[ "${APPLY_OPLAYER}" -eq 1 ]]; then
{ mkdir -p "${XDG_CACHE_HOME:-${HOME}/.cache}/perun" && printf '%s\n' "${SCRIPT_DIR}" > "${XDG_CACHE_HOME:-${HOME}/.cache}/perun/source"; } 2>/dev/null || true
fi
AGENTS_BLOCKS=(); AGENTS_CREATED=""
upsert_agents_block() {
local agents="$1"
local marker_begin="$2"
local marker_end="$3"
local block="$4"
AGENTS_BLOCKS+=("${marker_begin%:begin}")
if [[ -f "${agents}" ]] && grep -q "${marker_begin}" "${agents}"; then
local block_file out_file
block_file="$(mktemp "${TMPDIR:-/tmp}/perun.XXXXXX")"
out_file="$(mktemp "${TMPDIR:-/tmp}/perun.XXXXXX")"
printf '%s\n' "${block}" > "${block_file}"
# awk-v-ok: markers and a file path, single-line by construction
awk -v begin="<!-- ${marker_begin} -->" -v end="<!-- ${marker_end} -->" \
-v bf="${block_file}" '
BEGIN {
while ((getline line < bf) > 0) { newblock = newblock line ORS }
close(bf)
}
$0 == begin { printf "%s", newblock; skip=1; next }
skip && $0 == end { skip=0; next }
!skip { print }
' "${agents}" > "${out_file}"
mv "${out_file}" "${agents}"
rm -f "${block_file}"
echo "agents: refreshed ${marker_begin} (v${VERSION} @ ${INSTALL_SHA}) -> ${agents}"
elif [[ -f "${agents}" ]]; then
printf '\n%s\n' "${block}" >> "${agents}"
echo "agents: appended ${marker_begin} -> ${agents}"
else
printf '# AGENTS.md\n\n%s\n' "${block}" > "${agents}"
AGENTS_CREATED=1
echo "agents: created ${agents} with ${marker_begin}"
fi
}
if [[ "${WRITE_AGENTS}" -eq 1 ]]; then
AGENTS="${TARGET_DIR}/AGENTS.md"
LOCATIONS=""
if [[ "${MINIMAL}" -eq 0 ]]; then
LOCATIONS="also .cursor/skills/ and .agents/skills/"
fi
if [[ "${WITH_CODEX}" -eq 1 ]]; then
LOCATIONS="${LOCATIONS:+${LOCATIONS}; }.codex/skills/"
fi
if [[ "${WITH_EXTRA}" -eq 1 ]]; then
LOCATIONS="${LOCATIONS:+${LOCATIONS}; }extra hosts (.gemini .opencode .github .windsurf .hermes .kiro .cline .roo .factory .qwen .grok)"
fi
# Primary path is stated once; the parenthetical lists only the MIRROR roots
# (empty under --minimal, so no bare "()" and no path repeated).
if [[ -n "${LOCATIONS}" ]]; then
PRIMARY_PATH_LINE="Primary path: \`.claude/skills/${REVIEW_NAME}/SKILL.md\` (${LOCATIONS})."
else
PRIMARY_PATH_LINE="Primary path: \`.claude/skills/${REVIEW_NAME}/SKILL.md\`."
fi
REVIEW_BLOCK="$(cat <<EOF
<!-- deep-code-review:begin -->
## Code review — deep-code-review
Installed: **${VERSION}** (@ \`${INSTALL_SHA}\`). Agent-agnostic method.
${PRIMARY_PATH_LINE} Scope: \`FULL\` | \`DIFF <base-ref>\` | \`FILE <paths>\`; or say it in plain words, e.g. "run a deep code review FILE app.py" (\`/deep-code-review <scope>\` only where the host has slash commands).
Re-run upstream \`install.sh\` to refresh.
<!-- deep-code-review:end -->
EOF
)"
upsert_agents_block "${AGENTS}" "deep-code-review:begin" "deep-code-review:end" "${REVIEW_BLOCK}"
if [[ "${WITH_DELIVERY}" -eq 1 || "${WITH_CRITIC}" -eq 1 || "${WITH_COMMS}" -eq 1 || "${WITH_CONTRIBUTION}" -eq 1 || "${WITH_DISCOVERY}" -eq 1 || "${WITH_CEO}" -eq 1 || "${WITH_GROWTH}" -eq 1 || "${WITH_POSITIONING}" -eq 1 || "${WITH_BUSINESS}" -eq 1 || "${WITH_OUTPUT_SAFETY}" -eq 1 ]]; then
OVERLAY_LINES=""
if [[ "${WITH_DELIVERY}" -eq 1 ]]; then
OVERLAY_LINES="${OVERLAY_LINES} \`agentic-delivery\`"
fi
if [[ "${WITH_CRITIC}" -eq 1 ]]; then
OVERLAY_LINES="${OVERLAY_LINES} \`idea-critic\`"
fi
if [[ "${WITH_COMMS}" -eq 1 ]]; then
OVERLAY_LINES="${OVERLAY_LINES} \`communication-structure\`"
fi
if [[ "${WITH_CONTRIBUTION}" -eq 1 ]]; then
OVERLAY_LINES="${OVERLAY_LINES} \`contribution\`"
fi
if [[ "${WITH_DISCOVERY}" -eq 1 ]]; then
OVERLAY_LINES="${OVERLAY_LINES} \`product-discovery\`"
fi
if [[ "${WITH_CEO}" -eq 1 ]]; then
OVERLAY_LINES="${OVERLAY_LINES} \`agentic-ceo\`"
fi
if [[ "${WITH_GROWTH}" -eq 1 ]]; then
OVERLAY_LINES="${OVERLAY_LINES} \`growth-analytics\`"
fi
if [[ "${WITH_POSITIONING}" -eq 1 ]]; then
OVERLAY_LINES="${OVERLAY_LINES} \`positioning\`"
fi
if [[ "${WITH_BUSINESS}" -eq 1 ]]; then
OVERLAY_LINES="${OVERLAY_LINES} \`business-ops\`"
fi
if [[ "${WITH_OUTPUT_SAFETY}" -eq 1 ]]; then
OVERLAY_LINES="${OVERLAY_LINES} \`product-output-safety\`"
fi
OVERLAY_BLOCK="$(cat <<EOF
<!-- dcr-overlays:begin -->
## Optional overlays (deep-code-review ${VERSION} @ \`${INSTALL_SHA}\`)
Installed, each at \`.claude/skills/<name>/SKILL.md\`:${OVERLAY_LINES}. Persisted artifacts stay normal English.
Re-run upstream \`install.sh\` with the same \`--with-*\` flags (or \`--full\`) to refresh.
<!-- dcr-overlays:end -->
EOF
)"
upsert_agents_block "${AGENTS}" "dcr-overlays:begin" "dcr-overlays:end" "${OVERLAY_BLOCK}"
fi
if [[ -n "${TRACKER_PROJECT}" ]]; then
TRACKER_BLOCK="$(cat <<EOF
<!-- dcr-tracker:begin -->
## Work tracking (${TRACKER}, project \`${TRACKER_PROJECT}\`)
Every deliverable is a ${TRACKER} issue in this project; chores are PR checklist lines, not issues.
- Status moves: In Progress at start, In Review at PR open, Done only after the merge is confirmed.
- Put the issue ID in the title of only the PR that completes it. Slices, reverts, partial work and merge-train
members use \`Refs ID\` in the body, so the integration does not auto-close the issue.
- Write \`repo PR 123\`, never \`#123\`, in tracker text (it autolinks to a guessed repo).
- Set issue priority; leave project priority to its portfolio owner. Dependencies are blocks/blocked-by relations.
- Weekly: draft the update with \`tracker_weekly_update.py\`; hygiene: \`tracker_check.py\` (agentic-delivery scripts).
Details: \`.claude/skills/agentic-delivery/references/work-tracking.md\` (needs \`--with-delivery\`).
<!-- dcr-tracker:end -->
EOF
)"
upsert_agents_block "${AGENTS}" "dcr-tracker:begin" "dcr-tracker:end" "${TRACKER_BLOCK}"
fi
python3 "${SCRIPT_DIR}/scripts/perun_marker.py" "${TARGET_DIR}" --agents-block "${AGENTS_BLOCKS[@]}" ${AGENTS_CREATED:+--agents-created}
echo " works with Cursor, Claude Code, Codex, Copilot, Gemini, Aider, Windsurf, OpenCode, Hermes, Kiro, ..."
fi
echo "run it: tell your agent in plain words: run a deep code review FILE <a file in your project>"
echo " (/deep-code-review <scope> also works on hosts with slash commands, e.g. Claude Code)"
echo " scopes: FULL | DIFF <base-ref> | FILE <paths>"
echo " read: .claude/skills/${REVIEW_NAME}/SKILL.md (mirrors under other hosts when installed)"
if [[ "${WITH_DELIVERY}" -eq 1 ]]; then
echo " delivery: load agentic-delivery for gated multi-role work"
fi
if [[ "${WITH_CRITIC}" -eq 1 ]]; then
echo " critic: load idea-critic before a plan reaches the owner"
fi
if [[ "${WITH_COMMS}" -eq 1 ]]; then
echo " comms: load communication-structure before any human-facing message"
fi
if [[ "${WITH_GATES}" -eq 1 ]]; then
echo " gates: dcr-gates.yml + scripts/dcr-gates.sh wired -- push/open a PR to run them"
fi
if [[ "${WITH_OPERATING_LAYER}" -eq 1 ]]; then
[[ "${APPLY_OPLAYER}" -eq 1 ]] && echo " operating-layer: applied to .claude/settings.local.json" || echo " operating-layer: settings.operating-layer.json.new written -- merge it, then run operating_selfcheck.py"
fi
# First-run sandbox probe: harmless checks, one-line verdict, never fails the install.
PROBE="${SCRIPT_DIR}/.claude/skills/agentic-delivery/scripts/sandbox_probe.py"
if [[ -z "${DCR_NO_PROBE:-}" && -f "${PROBE}" ]] && command -v python3 >/dev/null; then
python3 "${PROBE}" --verdict-only "${TARGET_DIR}" 2>/dev/null || true