This repository was archived by the owner on Oct 8, 2026. It is now read-only.
Repository navigation
Expand file tree
/
Copy pathinstall.sh
More file actions
executable file
·846 lines (763 loc) · 35.3 KB
/
Copy pathinstall.sh
File metadata and controls
executable file
·846 lines (763 loc) · 35.3 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
#!/bin/bash
# Optimum Codegen CLI Installation Script
# Usage: install.sh [--harness=<list>]
# --harness=claude — comma-separated list of harnesses to install.
# When omitted, installs every harness (currently: claude).
# Invalid harness names cause a fail-fast exit with a helpful message.
#
# Environment variables (all optional):
# OCG_DEFAULT_AGENT=claude — skips agent-selection prompt; must match an installed harness.
# OCG_NONINTERACTIVE=1 — auto-trusts nested .mise.toml; hard-fails if a prompt would block.
# Requires OCG_DEFAULT_AGENT when multiple harnesses are installed.
# OCG_CODEGEN_DIR=<path> — dispatch fallback used by codegen-call when harnesses/ symlink unavailable.
#
# Examples:
# install.sh # installs claude
# install.sh --harness=claude # claude only
set -e
DRY_RUN=false
HARNESSES=()
for arg in "$@"; do
case "$arg" in
--dry-run)
DRY_RUN=true
;;
--harness=*)
list="${arg#--harness=}"
if [ -z "$list" ]; then
echo "❌ --harness= requires a value (one or more of: claude)" >&2
exit 1
fi
IFS=',' read -ra _tokens <<<"$list"
for tok in "${_tokens[@]}"; do
tok="$(echo "$tok" | tr -d '[:space:]')"
[ -z "$tok" ] && continue
case "$tok" in
claude)
# Deduplicate
already=false
for existing in "${HARNESSES[@]}"; do
[ "$existing" = "$tok" ] && already=true && break
done
[ "$already" = "false" ] && HARNESSES+=("$tok")
;;
*)
echo "❌ Unknown harness: '$tok' (expected one or more of: claude)" >&2
exit 1
;;
esac
done
;;
*)
echo "❌ Unknown argument: '$arg' (usage: install.sh [--harness=<list>])" >&2
exit 1
;;
esac
done
# Default = all harnesses when --harness flag omitted
if [ ${#HARNESSES[@]} -eq 0 ]; then
HARNESSES=("claude")
fi
harness_enabled() {
local target="$1"
for h in "${HARNESSES[@]}"; do
[ "$h" = "$target" ] && return 0
done
return 1
}
# content_stable_cp <src> <dst>
# Copies src to dst ONLY when dst doesn't exist or its bytes differ from src.
# Skipping identical files preserves mtime and keeps the Claude Code prompt
# cache valid (each write busts cache fleet-wide on the next session).
# Returns 0 in both cases; caller can inspect exit code of the underlying cp.
content_stable_cp() {
local src="$1"
local dst="$2"
if [ -f "$dst" ] && cmp -s "$src" "$dst"; then
return 0 # identical — skip write
fi
cp "$src" "$dst"
}
CODEGEN_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
# GENERATED_ROOT: overridable via OCG_GENERATED_DIR so concurrent installs (e.g. hermetic
# round-trip tests) can isolate their generator output instead of colliding on the shared
# repo-tracked path. Default is byte-identical to the pre-override behavior.
GENERATED_ROOT="${OCG_GENERATED_DIR:-$CODEGEN_DIR/templates/generated}"
export OCG_GENERATED_DIR="$GENERATED_ROOT"
INSTALL_DIR="$HOME/.local/bin"
SYMLINK_NAME="ocg"
CODEGEN_LOG_NAME="codegen-log"
echo "🚀 Installing Optimum Codegen CLI..."
echo " 📁 Codegen directory: $CODEGEN_DIR"
echo " 📁 Install directory: $INSTALL_DIR"
# Create install directory if it doesn't exist
if [ ! -d "$INSTALL_DIR" ]; then
echo " 📁 Creating install directory: $INSTALL_DIR"
mkdir -p "$INSTALL_DIR"
fi
# Create symlink for ocg
SYMLINK_PATH="$INSTALL_DIR/$SYMLINK_NAME"
if [ -L "$SYMLINK_PATH" ] || [ -f "$SYMLINK_PATH" ]; then
echo " 🔄 Removing existing $SYMLINK_NAME command"
rm -f "$SYMLINK_PATH"
fi
echo " 🔗 Creating symlink: $SYMLINK_PATH -> $CODEGEN_DIR/ocg"
ln -s "$CODEGEN_DIR/ocg" "$SYMLINK_PATH"
# Install codegen-log as a stable COPY, not a live-checkout symlink. A
# symlink dangles when the shared codegen checkout mutates mid-build (branch
# switch, worktree teardown); a copy is immune — it is a real file that keeps
# working regardless of what the source checkout is doing. Remove any
# pre-existing symlink/file first so a stale symlink from a prior install is
# replaced by a real copy (content_stable_cp only skips writes when dst is
# already an identical regular file — a symlink target never compares equal
# via `cmp`, so this always converts symlink -> copy on the next install).
CODEGEN_LOG_PATH="$INSTALL_DIR/$CODEGEN_LOG_NAME"
if [ -L "$CODEGEN_LOG_PATH" ]; then
echo " 🔄 Removing existing $CODEGEN_LOG_NAME symlink"
rm -f "$CODEGEN_LOG_PATH"
fi
echo " 📄 Copying: $CODEGEN_DIR/codegen-log -> $CODEGEN_LOG_PATH"
content_stable_cp "$CODEGEN_DIR/codegen-log" "$CODEGEN_LOG_PATH"
chmod +x "$CODEGEN_LOG_PATH"
# Wire dispatch path: codegen-call resolves $SCRIPT_DIR/harnesses at runtime.
# A sibling harnesses/ symlink in INSTALL_DIR makes tier-1 dispatch resolve without env-var fallback.
HARNESSES_SYMLINK="$INSTALL_DIR/harnesses"
rm -f "$HARNESSES_SYMLINK"
ln -sfn "$CODEGEN_DIR/harnesses" "$HARNESSES_SYMLINK"
if [ ! -d "$HARNESSES_SYMLINK/" ]; then
echo "❌ could not wire dispatch path: $HARNESSES_SYMLINK -> $CODEGEN_DIR/harnesses. Set fallback: export OCG_CODEGEN_DIR=$CODEGEN_DIR"
exit 1
fi
# Wire shared/ path: codegen-scaffold resolves $SCRIPT_DIR/shared at runtime.
# A sibling shared/ symlink in INSTALL_DIR makes it resolve without a full CODEGEN_DIR.
SHARED_SYMLINK="$INSTALL_DIR/shared"
rm -f "$SHARED_SYMLINK"
ln -sfn "$CODEGEN_DIR/shared" "$SHARED_SYMLINK"
# Wire templates/ + analysis/ paths: installed launchers are COPIES (not
# symlinks — a symlinked launcher would dangle if the checkout mutates
# mid-build), so each copy derives CODEGEN_DIR from its OWN location
# ($INSTALL_DIR) and cannot reach anything outside harnesses/ or shared/
# without a sibling link. codegen-document needs templates/ (INDEX
# regeneration script); codegen-analyze needs analysis/ (its Python
# package) — both currently fail from the installed copy without this.
TEMPLATES_SYMLINK="$INSTALL_DIR/templates"
rm -f "$TEMPLATES_SYMLINK"
ln -sfn "$CODEGEN_DIR/templates" "$TEMPLATES_SYMLINK"
if [ ! -d "$TEMPLATES_SYMLINK/" ]; then
echo "❌ could not wire templates path: $TEMPLATES_SYMLINK -> $CODEGEN_DIR/templates"
exit 1
fi
ANALYSIS_SYMLINK="$INSTALL_DIR/analysis"
rm -f "$ANALYSIS_SYMLINK"
ln -sfn "$CODEGEN_DIR/analysis" "$ANALYSIS_SYMLINK"
if [ ! -d "$ANALYSIS_SYMLINK/" ]; then
echo "❌ could not wire analysis path: $ANALYSIS_SYMLINK -> $CODEGEN_DIR/analysis"
exit 1
fi
# Check if ~/.local/bin is in PATH
if [[ ":$PATH:" != *":$INSTALL_DIR:"* ]]; then
echo ""
echo "⚠️ $INSTALL_DIR is not in your PATH"
echo " Add this line to your shell configuration file:"
if [ "$SHELL" = "/bin/zsh" ] || [ "$SHELL" = "/usr/bin/zsh" ]; then
RC_FILE="$HOME/.zshrc"
echo " echo 'export PATH=\"\$HOME/.local/bin:\$PATH\"' >> $RC_FILE"
else
RC_FILE="$HOME/.bashrc"
echo " echo 'export PATH=\"\$HOME/.local/bin:\$PATH\"' >> $RC_FILE"
fi
echo ""
echo " Or run this command to add it automatically:"
echo " echo 'export PATH=\"\$HOME/.local/bin:\$PATH\"' >> $RC_FILE"
echo ""
echo " Then restart your terminal or run: source $RC_FILE"
echo " export OCG_CODEGEN_DIR=$CODEGEN_DIR # dispatch fallback if harnesses/ symlink unavailable"
else
echo " ✅ $INSTALL_DIR is already in your PATH"
echo " export OCG_CODEGEN_DIR=$CODEGEN_DIR # dispatch fallback if harnesses/ symlink unavailable"
fi
echo ""
echo "🚀 Setting up recipes directory..."
# Context repository is now part of codegen as codegen/shared/ — no separate clone needed.
RECIPES_DIR="$CODEGEN_DIR/shared/recipes"
if [ ! -d "$RECIPES_DIR" ]; then
echo " 📚 Creating recipes directory at: $RECIPES_DIR"
mkdir -p "$RECIPES_DIR"
else
echo " ✅ Recipes directory already exists: $RECIPES_DIR"
fi
# Copy README only if it doesn't exist
if [ ! -f "$RECIPES_DIR/README.md" ] && [ -f "$CODEGEN_DIR/templates/recipes-README.md" ]; then
content_stable_cp "$CODEGEN_DIR/templates/recipes-README.md" "$RECIPES_DIR/README.md"
echo " ✅ Recipes README installed"
fi
echo ""
echo "🚀 Generating AI agent templates..."
# Platform symlinks: expose shared/ tables under codegen/ for agents resolving relative paths.
# Agents and tools resolve codegen/recipes/INDEX.md, codegen/rules/INDEX.md, etc. relative to
# the repo root; these symlinks make $CODEGEN_DIR/codegen/<x> -> $CODEGEN_DIR/shared/<x>.
mkdir -p "$CODEGEN_DIR/codegen"
for _link in rules recipes usage_rules subagents; do
ln -sfn "$CODEGEN_DIR/shared/$_link" "$CODEGEN_DIR/codegen/$_link" 2>/dev/null || true
if [ ! -e "$CODEGEN_DIR/codegen/$_link" ]; then
echo "❌ platform symlink unresolved: codegen/$_link -> shared/$_link"
exit 1
fi
done
# Install root node_modules (ajv, playwright, prettier) so schema-validate.js and render-check.js
# can resolve their dependencies from $CODEGEN_DIR/node_modules.
# Skip if node_modules already present and up-to-date (idempotent).
if [ -f "$CODEGEN_DIR/package.json" ]; then
if [ ! -d "$CODEGEN_DIR/node_modules/ajv" ]; then
echo ""
echo "📦 Installing root node deps (ajv, playwright, prettier)..."
# Use MISE_SKIP_CONFIG=1 so the npm shim runs without requiring mise trust on HOME-derived configs.
# This is safe: we only need npm here, not mise's tool version management.
_npm_install_failed=0
(cd "$CODEGEN_DIR" && MISE_SKIP_CONFIG=1 npm install) 2>&1 || _npm_install_failed=1
if [ "$_npm_install_failed" -eq 1 ]; then
echo "❌ root npm install failed — schema-validate/render-check need node_modules" >&2
exit 1
fi
else
echo " ✅ Root node_modules/ajv already present — skipping npm install"
fi
fi
# Install Chromium browser binary (required for static-site render verification).
# Only run if playwright is present (dev-dep); skip gracefully on production installs.
# advisory: chromium absence is fail-CLOSED-enforced at the GATE
# (static-site-build-check.sh chromium-launch-failed arm blocks the
# developer), NOT at install. Install-time is warn-only by design — a box
# may provision chromium after install. Do not make fatal (would break
# non-static + deferred-provision installs).
if command -v npm &>/dev/null && (cd "$CODEGEN_DIR" && npm list playwright >/dev/null 2>&1); then
echo "📦 Installing Chromium browser binary (playwright)..."
if (cd "$CODEGEN_DIR" && npx playwright install chromium) 2>&1; then
echo " ✅ Chromium browser binary installed"
else
echo " ⚠️ Chromium install failed — static-site render gate will warn until installed manually. Run: npx playwright install chromium" >&2
fi
else
echo " ⚠️ Skipping Chromium install (playwright not present; non-static install)"
fi
# Generate templates for the selected harnesses via unified manifest-driven generator.
# Source manifest helpers for launcher/completion iteration (used in harness install loop below).
# Upfront yq gate: generate.sh and manifest-lib.sh consume yq; fail fast before sourcing.
if ! command -v yq >/dev/null 2>&1; then
echo "❌ yq required but not found — install it: brew install yq (macOS) | https://github.com/mikefarah/yq"
exit 1
fi
if ! yq --version 2>&1 | grep -qi mikefarah; then
echo "❌ installed yq is not mikefarah/yq (apt python-yq is incompatible) — install from https://github.com/mikefarah/yq"
exit 1
fi
if [[ ! -f "$CODEGEN_DIR/templates/generator/manifest-lib.sh" ]]; then
echo "❌ manifest-lib.sh not found at $CODEGEN_DIR/templates/generator/manifest-lib.sh — corrupt or incomplete checkout" >&2
exit 1
fi
source "$CODEGEN_DIR/templates/generator/manifest-lib.sh"
# Pass all selected harnesses to generate.sh in one call.
bash "$CODEGEN_DIR/templates/generator/generate.sh" "${HARNESSES[@]}"
# Render user-app orchestrator AGENTS templates (.j2 -> .md) back into context repo.
# These .md files are symlinked into user-app workspaces (e.g. via a platform copy_agents_md step)
# at provision time, so they must exist as regenerable artifacts beside their .j2 source.
# Each template renders TWICE:
# agents render → AGENTS-{variant}.md (→ See pointers; harness-neutral)
# claude render → CLAUDE-{variant}.md (@ auto-load imports; consumed by Claude Code)
echo ""
echo "🚀 Rendering user-app AGENTS templates (.j2 -> .md)..."
# APPS_SRC_DIR: fixed — .j2 templates always live in the tracked repo source.
# APPS_OUT_DIR: overridable via OCG_RENDERED_APPS_DIR so isolated processes
# (e.g. hermetic round-trip tests) can render without writing tracked
# shared/apps/*.md bytes. Default is byte-identical to the pre-override
# behavior (mirrors the OCG_GENERATED_DIR convention above).
APPS_SRC_DIR="$CODEGEN_DIR/shared/apps"
APPS_OUT_DIR="${OCG_RENDERED_APPS_DIR:-$APPS_SRC_DIR}"
PROCESS_TEMPLATE="$CODEGEN_DIR/templates/generator/process_template.py"
if ! mkdir -p "$APPS_OUT_DIR"; then
echo "❌ Cannot create APPS_OUT_DIR: $APPS_OUT_DIR" >&2
exit 1
fi
render_to_md() {
local src="$1"
local tool_name="$2"
local dst="$3"
if [ -f "$src" ]; then
local tmp
tmp="$(mktemp)"
python3 "$PROCESS_TEMPLATE" "$src" "$tool_name" false >"$tmp"
if [ ! -f "$dst" ] || ! cmp -s "$tmp" "$dst"; then
mv "$tmp" "$dst"
echo " ✅ Rendered $(basename "$dst") (tool=$tool_name)"
else
rm -f "$tmp"
echo " ✅ $(basename "$dst") already up to date"
fi
# mktemp creates 0600. The idempotent branch above never reaches the
# mv, so a chmod placed after only the mv branch would never repair a
# $dst that is already stuck at 0600 from a prior install. Placed
# here (after the whole if/else, on every pass) it always converges
# $dst to 0644 regardless of which branch ran.
chmod 0644 "$dst"
else
echo "❌ Required app-doc template missing: $src" >&2
exit 1
fi
}
for base in AGENTS-phoenix AGENTS-static; do
src="$APPS_SRC_DIR/$base.md.j2"
# agents render → canonical AGENTS-{variant}.md
render_to_md "$src" agents "$APPS_OUT_DIR/$base.md"
# claude render → CLAUDE-{variant}.md (strip "AGENTS-" prefix, add "CLAUDE-")
variant="${base#AGENTS-}"
render_to_md "$src" claude "$APPS_OUT_DIR/CLAUDE-${variant}.md"
done
# Clean up generated templates after installation
cleanup_generated_templates() {
echo " 🧹 Cleaning up generated templates..."
rm -rf "$GENERATED_ROOT/"
echo " ✅ Generated templates cleaned up"
}
# ─── Manifest-driven harness install loop ─────────────────────────────────────
# Each harness declares its surface in its own manifest.yaml.
# Each harness's manifest.yaml declares its surface; this loop reads it.
#
# Zsh completions: install into first writable dir.
# Override search list via env: ZSH_COMPLETION_DIRS="/path1:/path2" (colon-separated).
# Default: /opt/homebrew/share/zsh/site-functions and $HOME/.zsh/completions.
IFS=':' read -ra ZSH_COMPLETION_DIRS <<<"${ZSH_COMPLETION_DIRS:-/opt/homebrew/share/zsh/site-functions:$HOME/.zsh/completions}"
ZSH_COMPLETION_DST=""
for _d in "${ZSH_COMPLETION_DIRS[@]}"; do
if [ -d "$_d" ] && [ -w "$_d" ]; then
ZSH_COMPLETION_DST="$_d"
break
fi
done
for _harness in "${HARNESSES[@]}"; do
echo ""
echo "🚀 Setting up $_harness harness..."
case "$_harness" in
claude)
# ── Claude-specific: settings, hooks, commands, agents, deps ──────────
CLAUDE_SETTINGS_DIR="$HOME/.claude"
CLAUDE_SETTINGS_FILE="$CLAUDE_SETTINGS_DIR/settings.json"
CLAUDE_COMMANDS_DIR="$CLAUDE_SETTINGS_DIR/commands"
mkdir -p "$CLAUDE_SETTINGS_DIR"
mkdir -p "$CLAUDE_COMMANDS_DIR"
# Install settings
if [ -f "$GENERATED_ROOT/claude-code/claude-code-settings.json" ]; then
content_stable_cp "$GENERATED_ROOT/claude-code/claude-code-settings.json" "$CLAUDE_SETTINGS_FILE"
echo " ✅ Claude Code settings installed at: $CLAUDE_SETTINGS_FILE"
fi
echo ""
echo "🚀 Setting up Claude Code hooks..."
if [ -d "$CODEGEN_DIR/harnesses/claude/hooks" ]; then
mkdir -p "$CLAUDE_SETTINGS_DIR/hooks"
for hook_file in "$CODEGEN_DIR/harnesses/claude/hooks"/*.sh; do
if [ -f "$hook_file" ]; then
hook_name=$(basename "$hook_file")
content_stable_cp "$hook_file" "$CLAUDE_SETTINGS_DIR/hooks/$hook_name"
chmod +x "$CLAUDE_SETTINGS_DIR/hooks/$hook_name"
echo " ✅ ${hook_name} hook installed at: $CLAUDE_SETTINGS_DIR/hooks/$hook_name"
fi
done
# Prune orphan hooks
if [ -d "$CLAUDE_SETTINGS_DIR/hooks" ]; then
for installed_hook in "$CLAUDE_SETTINGS_DIR/hooks"/*.sh; do
[ -f "$installed_hook" ] || continue
hook_basename=$(basename "$installed_hook")
if [ ! -f "$CODEGEN_DIR/harnesses/claude/hooks/$hook_basename" ]; then
rm -f "$installed_hook"
echo " Removed orphan hook: $hook_basename"
fi
done
fi
# Install hooks lib
if [ -d "$CODEGEN_DIR/harnesses/claude/hooks/lib" ]; then
mkdir -p "$CLAUDE_SETTINGS_DIR/hooks/lib"
for lib_file in "$CODEGEN_DIR/harnesses/claude/hooks/lib"/*; do
if [ -f "$lib_file" ]; then
content_stable_cp "$lib_file" "$CLAUDE_SETTINGS_DIR/hooks/lib/$(basename "$lib_file")"
fi
done
# stop-resume.sh sources the shared retryable-error taxonomy;
# install it beside the hook (single source of truth stays in
# harnesses/shared/) so the flat install dir has no ../.. reach.
if [ -f "$CODEGEN_DIR/harnesses/shared/retryable-errors.sh" ]; then
content_stable_cp "$CODEGEN_DIR/harnesses/shared/retryable-errors.sh" "$CLAUDE_SETTINGS_DIR/hooks/lib/retryable-errors.sh"
fi
echo " ✅ Hooks lib installed at: $CLAUDE_SETTINGS_DIR/hooks/lib/"
fi
fi
# Install custom Claude commands
echo " 📁 Installing custom Claude commands..."
COMMANDS_MANIFEST="$CLAUDE_COMMANDS_DIR/.installed-by-ocg"
CURRENT_COMMANDS=()
if [ -d "$CODEGEN_DIR/harnesses/claude/commands" ]; then
for cmd_file in "$CODEGEN_DIR/harnesses/claude/commands"/*.md; do
if [ -f "$cmd_file" ]; then
cmd_name=$(basename "$cmd_file")
content_stable_cp "$cmd_file" "$CLAUDE_COMMANDS_DIR/$cmd_name"
echo " ✅ Installed command: /${cmd_name%.md}"
CURRENT_COMMANDS+=("$cmd_name")
fi
done
fi
# Install generated commands (from .j2 templates rendered by generate.sh).
if [ -d "$GENERATED_ROOT/claude-code/commands" ]; then
for cmd_file in "$GENERATED_ROOT/claude-code/commands"/*.md; do
if [ -f "$cmd_file" ]; then
cmd_name=$(basename "$cmd_file")
content_stable_cp "$cmd_file" "$CLAUDE_COMMANDS_DIR/$cmd_name"
echo " ✅ Installed command (generated): /${cmd_name%.md}"
CURRENT_COMMANDS+=("$cmd_name")
fi
done
fi
for installed_cmd in "$CLAUDE_COMMANDS_DIR"/*.md; do
[ -f "$installed_cmd" ] || continue
cmd_basename=$(basename "$installed_cmd")
still_present=false
for cur in "${CURRENT_COMMANDS[@]}"; do
[ "$cur" = "$cmd_basename" ] && still_present=true && break
done
if [ "$still_present" = "false" ]; then
rm -f "$installed_cmd"
echo " 🗑️ Removed stale command: /${cmd_basename%.md}"
fi
done
printf '%s\n' "${CURRENT_COMMANDS[@]}" >"$COMMANDS_MANIFEST"
# Install Claude sub agents
echo " 🤖 Installing Claude sub agents..."
CLAUDE_AGENTS_DIR="$CLAUDE_SETTINGS_DIR/agents"
mkdir -p "$CLAUDE_AGENTS_DIR"
AGENTS_MANIFEST="$CLAUDE_AGENTS_DIR/.installed-by-ocg"
CURRENT_AGENTS=()
# One-time migration: union per-stack manifests into single manifest.
_any_stack_manifests=$(ls "$CLAUDE_AGENTS_DIR"/.installed-by-ocg.* 2>/dev/null || true)
if [ -n "$_any_stack_manifests" ]; then
# shellcheck disable=SC2086
sort -u $CLAUDE_AGENTS_DIR/.installed-by-ocg.* >"$AGENTS_MANIFEST"
# shellcheck disable=SC2086
rm -f $CLAUDE_AGENTS_DIR/.installed-by-ocg.*
fi
if [ -d "$GENERATED_ROOT/claude-code/agents" ]; then
for agent_file in "$GENERATED_ROOT/claude-code/agents"/*.md; do
if [ -f "$agent_file" ]; then
agent_name=$(basename "$agent_file")
content_stable_cp "$agent_file" "$CLAUDE_AGENTS_DIR/$agent_name"
echo " ✅ Installed Claude sub agent: ${agent_name%.md}"
CURRENT_AGENTS+=("$agent_name")
fi
done
fi
if [ ${#CURRENT_AGENTS[@]} -eq 0 ]; then
echo " ⚠️ Skipping Claude agent prune — install set is empty (generator may have failed)"
else
for installed_agent in "$CLAUDE_AGENTS_DIR"/*.md; do
[ -f "$installed_agent" ] || continue
agent_basename=$(basename "$installed_agent")
still_present=false
for cur in "${CURRENT_AGENTS[@]}"; do
[ "$cur" = "$agent_basename" ] && still_present=true && break
done
if [ "$still_present" = "false" ]; then
rm -f "$installed_agent"
echo " 🗑️ Removed stale agent: ${agent_basename%.md}"
fi
done
fi
printf '%s\n' "${CURRENT_AGENTS[@]+"${CURRENT_AGENTS[@]}"}" >"$AGENTS_MANIFEST"
# Build the codegen MCP server in place (no copy — call-dispatch.sh
# resolves harnesses/claude/mcp-server/dist/index.js from CODEGEN_DIR
# at dispatch time, so building here is sufficient; dispatch omits
# --mcp-config entirely when dist/index.js is absent, so a build
# failure here degrades gracefully rather than breaking `claude` calls).
echo ""
echo "🚀 Building codegen MCP server..."
MCP_SERVER_DIR="$CODEGEN_DIR/harnesses/claude/mcp-server"
if [ -f "$MCP_SERVER_DIR/package.json" ]; then
_mcp_build_failed=0
(cd "$MCP_SERVER_DIR" && npm install --prefer-offline && npm run build) || _mcp_build_failed=1
if [ "$_mcp_build_failed" -eq 1 ]; then
echo " ⚠️ codegen MCP server build failed — mcp__codegen__* tools will be unavailable this install (call-dispatch.sh omits --mcp-config when dist/ is absent)" >&2
else
echo " ✅ codegen MCP server built at: $MCP_SERVER_DIR/dist/index.js"
fi
fi
# Install required dependencies (claude-specific)
echo ""
echo "🚀 Installing required dependencies..."
if ! command -v jq >/dev/null 2>&1; then
echo " 📦 Installing jq with brew..."
if command -v brew >/dev/null 2>&1; then
brew install jq
echo " ✅ jq installed"
else
echo " ❌ Homebrew not found. Please install jq manually:"
echo " brew install jq"
exit 1
fi
else
echo " ✅ jq already installed"
fi
if ! command -v yq >/dev/null 2>&1; then
if [[ "$OSTYPE" == "darwin"* ]]; then
echo " 📦 Installing yq with brew..."
if command -v brew >/dev/null 2>&1; then
brew install yq
echo " ✅ yq installed"
else
echo " ❌ Homebrew not found. Please install yq manually:"
echo " brew install yq"
exit 1
fi
elif [[ "$OSTYPE" == "linux-gnu"* ]]; then
echo " 📦 Installing yq with apt..."
sudo apt-get update && sudo apt-get install -y yq
if ! yq --version 2>&1 | grep -qi mikefarah; then
echo " ❌ installed yq is not mikefarah/yq (apt python-yq is incompatible) — install from https://github.com/mikefarah/yq" >&2
exit 1
fi
echo " ✅ yq installed"
else
echo "Warning: yq installation not supported on this OS. Install manually: https://github.com/mikefarah/yq"
fi
else
echo " ✅ yq already installed"
fi
echo " 📦 Installing system ripgrep for Claude Code custom commands..."
if ! command -v rg >/dev/null 2>&1; then
if command -v brew >/dev/null 2>&1; then
brew install ripgrep
echo " ✅ System ripgrep installed"
else
echo " ❌ Homebrew not found. Please install ripgrep manually:"
echo " brew install ripgrep"
echo " ⚠️ Custom Claude Code commands may not work without system ripgrep"
fi
else
echo " ✅ System ripgrep already installed"
fi
# Determine shell rc file
if [ "$SHELL" = "/bin/zsh" ] || [ "$SHELL" = "/usr/bin/zsh" ]; then
RC_FILE="$HOME/.zshrc"
else
RC_FILE="$HOME/.bashrc"
fi
echo ""
echo "🚀 Setting up Claude Code custom command support..."
RIPGREP_VAR="export USE_BUILTIN_RIPGREP=0"
if grep -q "USE_BUILTIN_RIPGREP" "$RC_FILE" 2>/dev/null; then
echo " ✅ USE_BUILTIN_RIPGREP already set in $RC_FILE"
else
echo "" >>"$RC_FILE"
echo "# Claude Code custom command discovery (requires system ripgrep)" >>"$RC_FILE"
echo "$RIPGREP_VAR" >>"$RC_FILE"
echo " ✅ Added USE_BUILTIN_RIPGREP=0 to $RC_FILE"
fi
echo ""
echo "🚀 Setting up autocompletion..."
if grep -q "bash_completion.sh" "$RC_FILE" 2>/dev/null; then
echo " ⚠️ Completion already appears to be set up in $RC_FILE"
else
echo "" >>"$RC_FILE"
echo "# Optimum Codegen autocompletion" >>"$RC_FILE"
echo "source \"$CODEGEN_DIR/bash_completion.sh\"" >>"$RC_FILE"
echo " ✅ Added autocompletion to $RC_FILE"
fi
# De-register legacy aliases (idempotent)
for _legacy_alias in claude-build claude-design claude-debug; do
if grep -q "alias ${_legacy_alias}=" "$RC_FILE" 2>/dev/null; then
_TEMP_FILE=$(mktemp)
grep -v "^# Optimum Codegen ${_legacy_alias} alias$" "$RC_FILE" |
grep -v "^alias ${_legacy_alias}=" >"$_TEMP_FILE"
if ! cmp -s "$RC_FILE" "$_TEMP_FILE"; then
mv "$_TEMP_FILE" "$RC_FILE"
else
rm -f "$_TEMP_FILE"
fi
echo " 🗑️ Removed legacy ${_legacy_alias} alias from $RC_FILE"
fi
done
# Remove legacy claude-design binary (idempotent)
if [ -f "$INSTALL_DIR/claude-design" ]; then
rm -f "$INSTALL_DIR/claude-design"
echo " Removed legacy: $INSTALL_DIR/claude-design"
fi
echo ""
echo "🚀 Setting up Claude Code bash environment..."
echo "🤖 Installing Claude Code..."
hash -r 2>/dev/null || true
if command -v claude >/dev/null 2>&1; then
echo " ✅ Claude Code already installed"
else
curl -fsSL https://claude.ai/install.sh | bash
echo " ✅ Claude Code installed"
fi
;;
esac
# ── Manifest-driven: launchers + completions (all harnesses) ─────────────
echo ""
echo "🚀 Installing $_harness launchers..."
mkdir -p "$INSTALL_DIR"
CURRENT_LAUNCHERS=()
while IFS=' ' read -r _src _dest_name; do
_dest="$INSTALL_DIR/$_dest_name"
content_stable_cp "$CODEGEN_DIR/$_src" "$_dest"
chmod +x "$_dest"
echo " ✅ Installed launcher: $_dest_name → $_dest"
CURRENT_LAUNCHERS+=("$_dest_name")
done < <(manifest_launchers "$_harness")
# Prune stale launchers for this harness (prefix = harness name + dash)
if [ ${#CURRENT_LAUNCHERS[@]} -gt 0 ]; then
for _installed_launcher in "$INSTALL_DIR/${_harness}-"*; do
[ -f "$_installed_launcher" ] || continue
_launcher_basename=$(basename "$_installed_launcher")
_launcher_still_present=false
for _cur_launcher in "${CURRENT_LAUNCHERS[@]}"; do
[ "$_cur_launcher" = "$_launcher_basename" ] && _launcher_still_present=true && break
done
if [ "$_launcher_still_present" = "false" ]; then
rm -f "$_installed_launcher"
echo " 🗑️ Removed stale launcher: $_launcher_basename"
fi
done
fi
# Remove orphaned legacy codex-* launchers (removed from source; never in any
# manifest). Hardcoded known names.
for _codex in codex-build codex-inspector codex-refactor codex-shape; do
if [ -f "$INSTALL_DIR/$_codex" ]; then
rm -f "$INSTALL_DIR/$_codex"
echo " 🗑️ Removed orphaned legacy launcher: $_codex"
fi
done
echo ""
echo "🚀 Installing $_harness zsh completions..."
if [ -n "$ZSH_COMPLETION_DST" ]; then
while IFS= read -r _comp; do
content_stable_cp "$CODEGEN_DIR/harnesses/$_harness/$_comp" "$ZSH_COMPLETION_DST/$_comp"
echo " Installed zsh completion: $ZSH_COMPLETION_DST/$_comp"
done < <(manifest_completions "$_harness")
else
echo "WARNING: no writable zsh completion dir found (tried ${ZSH_COMPLETION_DIRS[*]}); skipping $_harness completion install" >&2
fi
done # harness loop
# Create OCG config directory
mkdir -p "$HOME/.ocg"
if [ ! -f "$HOME/.ocg/config.json" ]; then
# If only one harness was selected, use it as the default without prompting.
# Otherwise prompt the user among the selected harnesses.
if [ ${#HARNESSES[@]} -eq 1 ]; then
default_agent="${HARNESSES[0]}"
else
if [ -n "${OCG_DEFAULT_AGENT:-}" ]; then
if ! harness_enabled "$OCG_DEFAULT_AGENT"; then
echo "❌ OCG_DEFAULT_AGENT=$OCG_DEFAULT_AGENT not among installed harnesses (${HARNESSES[*]})"
exit 1
fi
default_agent="$OCG_DEFAULT_AGENT"
elif [ "${OCG_NONINTERACTIVE:-}" = "1" ]; then
echo "❌ OCG_NONINTERACTIVE=1 set but OCG_DEFAULT_AGENT unset and multiple harnesses installed (${HARNESSES[*]})"
exit 1
else
echo ""
echo "🤖 AI Agent Configuration"
echo " The following AI agents are installed: ${HARNESSES[*]}"
echo " Which should be your default AI agent?"
i=1
for h in "${HARNESSES[@]}"; do
echo " $i) $h"
i=$((i + 1))
done
echo ""
if [[ ! -t 0 ]]; then
echo "❌ install.sh needs a TTY for agent selection; set OCG_DEFAULT_AGENT=claude for non-interactive installs" >&2
exit 1
fi
read -p " Choose [1-${#HARNESSES[@]}]: " choice
if [[ "$choice" =~ ^[0-9]+$ ]] && [ "$choice" -ge 1 ] && [ "$choice" -le ${#HARNESSES[@]} ]; then
default_agent="${HARNESSES[$((choice - 1))]}"
else
default_agent="${HARNESSES[0]}"
fi
fi
fi
claude_enabled=false
harness_enabled claude && claude_enabled=true
cat >"$HOME/.ocg/config.json" <<EOF
{
"default_agent": "$default_agent",
"agents": {
"claude": { "enabled": $claude_enabled }
}
}
EOF
echo "✅ Default AI agent set to: $default_agent"
else
echo ""
echo "✅ AI agent configuration already exists"
fi
# Clean up generated templates now that everything is installed
cleanup_generated_templates
# Format codegen shared dir after install finishes regenerating files
# Resolve prettier's real CJS entry and run via a non-shim node binary.
# Resolving through the mise shim triggers trust checks when HOME is changed in tests;
# scanning PATH for a non-shim node entry avoids mise entirely.
# Non-fatal: formatting is cosmetic; installation succeeds even if prettier can't run.
if [ -d "$CODEGEN_DIR/shared" ]; then
_prettier_cjs="$CODEGEN_DIR/node_modules/prettier/bin/prettier.cjs"
if [ -f "$_prettier_cjs" ]; then
# Find a real (non-shim) node binary by scanning PATH entries.
_real_node=""
_old_IFS="$IFS"
IFS=:
for _dir in $PATH; do
IFS="$_old_IFS"
if [ -x "$_dir/node" ] && printf '%s' "$_dir" | grep -qv "shims"; then
_real_node="$_dir/node"
break
fi
IFS=:
done
IFS="$_old_IFS"
# Fallback: use the shim (may fail with trust error but non-fatal)
if [ -z "$_real_node" ]; then
_real_node="$(command -v node 2>/dev/null || true)"
fi
if [ -n "$_real_node" ] && [ -x "$_real_node" ]; then
_prettier_err="$(mktemp)"
if ! "$_real_node" "$_prettier_cjs" -w --log-level error "$CODEGEN_DIR/shared" 2>"$_prettier_err"; then
echo "⚠️ prettier failed to format shared/ (non-fatal) — output below:" >&2
cat "$_prettier_err" >&2
fi
rm -f "$_prettier_err"
fi
fi
fi
# Stamp install currency (sha + working-tree content hash of the source set
# `make install` consumes) so `make build-ready` can detect a stale/edited
# install on a remote box before a paid drain dispatches to it. See
# harnesses/shared/build-ready-currency.sh (single source of the source-set
# list + hash formula, shared with the reader in Makefile's build-ready
# target).
if [ -f "$CODEGEN_DIR/harnesses/shared/build-ready-currency.sh" ]; then
# shellcheck source=harnesses/shared/build-ready-currency.sh
source "$CODEGEN_DIR/harnesses/shared/build-ready-currency.sh"
build_ready_write_stamp "$CODEGEN_DIR" "$INSTALL_DIR/.ocg-install-stamp"
fi
echo ""
echo "✅ Installation complete!"
echo ""
echo "🎯 You can now use these commands from anywhere:"
echo " ocg new <feature-name>"
echo " ocg resume <feature-name>"
echo " ocg rm <feature-name>"
echo " ocg clean"
echo " ocg ls"
echo " ocg help"
echo ""
echo "💡 Restart your terminal or run 'source $RC_FILE' for autocompletion and Claude Code custom commands"
echo "💡 Test the installation by running: ocg help"
echo "💡 Test Claude Code custom commands by running: claude (then try /write-drop)"
echo "💡 Next: Run 'ocg setup' in your project directory to install project-specific tools"