-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsync.sh
More file actions
executable file
·330 lines (284 loc) · 10.5 KB
/
Copy pathsync.sh
File metadata and controls
executable file
·330 lines (284 loc) · 10.5 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
#!/bin/bash
# Reconcile this machine's agentic-coding config with the repo.
#
# Idempotent and non-interactive: safe to re-run any time config changes
# (a new skill, permission, global fragment, or settings edit). This is the
# "run more than once" half of setup. install.sh calls it, then layers the
# one-time interactive bootstrap (MCP servers, .zshrc, agy check) on top.
#
# What it does:
# - regenerate permission + global-instruction files from their sources
# - apply all symlinks (skip-if-correct; back up a real file, never clobber)
# - merge repo-managed keys into ~/.claude/settings.json (see merge_settings)
# - relink the Codex/Pi skill subset
#
# Usage: ./sync.sh [--autonomous | --normal]
# With no flag it uses the persisted profile (written by install.sh or a prior
# run), defaulting to "normal". An explicit flag overrides AND is persisted.
set -e
SCRIPT_DIR="$(cd "$(dirname "$0")" && pwd)"
PROFILE_FILE="${XDG_CONFIG_HOME:-$HOME/.config}/aiconf/profile"
# --- Profile resolution ---
EXPLICIT_PROFILE=""
while [[ $# -gt 0 ]]; do
case "$1" in
--autonomous) EXPLICIT_PROFILE="autonomous" ;;
--normal) EXPLICIT_PROFILE="normal" ;;
-h|--help)
echo "Usage: $0 [--autonomous | --normal]"
echo " Reconciles machine config with the repo. Uses the persisted"
echo " profile when no flag is given (default: normal)."
exit 0
;;
*) echo "Unknown option: $1" >&2; exit 1 ;;
esac
shift
done
if [[ -n "$EXPLICIT_PROFILE" ]]; then
PROFILE="$EXPLICIT_PROFILE"
mkdir -p "$(dirname "$PROFILE_FILE")"
echo "$PROFILE" > "$PROFILE_FILE" # persist explicit choice for future runs
elif [[ -f "$PROFILE_FILE" ]]; then
PROFILE="$(cat "$PROFILE_FILE")"
else
PROFILE="normal"
fi
if [[ "$PROFILE" == "autonomous" ]]; then
SETTINGS_SRC="$SCRIPT_DIR/claude/settings.autonomous.json"
CLAUDEMD_SRC="$SCRIPT_DIR/claude/CLAUDE.autonomous.md"
else
SETTINGS_SRC="$SCRIPT_DIR/claude/settings.json"
CLAUDEMD_SRC="$SCRIPT_DIR/claude/CLAUDE.md"
fi
# --- Symlinks: "source:destination" ---
# NOTE: ~/.claude/settings.json is intentionally NOT symlinked — Claude Code
# writes to it (survey state, etc.) and an atomic write would clobber the link.
# It is reconciled by merge_settings instead.
SYMLINKS=(
# Claude
"$SCRIPT_DIR/claude/skills:$HOME/.claude/skills"
"$SCRIPT_DIR/claude/hooks:$HOME/.claude/hooks"
"$CLAUDEMD_SRC:$HOME/.claude/CLAUDE.md"
# Codex
"$SCRIPT_DIR/codex/rules:$HOME/.codex/rules"
"$SCRIPT_DIR/global/AGENTS.md:$HOME/.codex/AGENTS.md"
# Antigravity (agy) — replaced Gemini CLI in May 2026
"$SCRIPT_DIR/antigravity/settings.json:$HOME/.gemini/antigravity-cli/settings.json"
# agy reads global instructions from ~/.gemini/GEMINI.md — same shared file as Codex
"$SCRIPT_DIR/global/AGENTS.md:$HOME/.gemini/GEMINI.md"
# OpenCode (reads global config from XDG ~/.config/opencode, not legacy ~/.opencode)
"$SCRIPT_DIR/opencode/opencode.json:$HOME/.config/opencode/opencode.json"
# Pi (pi-coding-agent) — settings.json holds the enabledModels allowlist;
# AGENTS.md is the same shared global-instructions file Codex/Antigravity use.
# Skills need no wiring: pi auto-discovers ~/.agents/skills (populated by
# install_codex_skills below). Pi has no shell-permission system, so it is not
# part of permissions/sync.py.
"$SCRIPT_DIR/pi/settings.json:$HOME/.pi/agent/settings.json"
"$SCRIPT_DIR/global/AGENTS.md:$HOME/.pi/agent/AGENTS.md"
# Shell
"$SCRIPT_DIR/.airc:$HOME/.airc"
)
# Skills shared with Codex (subset of claude/skills/). A name with a real dir
# in codex/skills/ uses that override; otherwise it links from claude/skills/.
CODEX_SKILLS=(
check-claude-projects check-notes commit
code-review debug-log deslop doc explain frontend-design guide ideation
library-docs optimize-seo pdf
perf-test read-docs review-architecture review-cleancode review-comments
review-history review-interfaces review-library-use review-perf review-plan review-product
review-security review-swift review-typescript research-general research-tech skill-creator
squash-commits temp test theme-factory
)
# Non-interactive symlink: correct link → skip; wrong link → silently relink
# (a symlink holds no data); a real file/dir where a link belongs → back it up
# (never rm -rf unattended), then link.
create_symlink() {
local source="$1"
local dest="$2"
if [[ ! -e "$source" ]]; then
echo "⚠️ Source does not exist: $source (skipping)"
return
fi
if [[ -L "$dest" ]]; then
if [[ "$(readlink "$dest")" == "$source" ]]; then
echo "✓ Already linked: $dest"
return
fi
rm -f "$dest" # wrong symlink — no data to lose
elif [[ -e "$dest" ]]; then
local bak="$dest.bak.$(date +%Y%m%d%H%M%S)"
mv "$dest" "$bak" # real file/dir — preserve it
echo "↩ Backed up existing $dest -> $bak"
fi
mkdir -p "$(dirname "$dest")"
ln -s "$source" "$dest"
echo "✓ Linked: $dest -> $source"
}
# Merge the repo's managed settings into the real ~/.claude/settings.json,
# preserving keys Claude Code writes itself (survey state, etc.). Repo keys win
# on conflict; keys only in the live file are kept. Writes a real file (breaking
# any leftover symlink once), so future app writes never clobber a link.
merge_settings() {
local overlay="$SETTINGS_SRC"
local dest="$HOME/.claude/settings.json"
if [[ ! -f "$overlay" ]]; then
echo "⚠️ Settings source not found: $overlay (skipping merge)"
return
fi
if ! command -v python3 &>/dev/null; then
echo "⚠️ python3 not found — settings merge skipped"
return
fi
python3 - "$overlay" "$dest" <<'PY'
import json, os, sys, tempfile
overlay_path, dest_path = sys.argv[1], sys.argv[2]
with open(overlay_path) as f:
overlay = json.load(f)
base = {}
if os.path.exists(dest_path):
try:
with open(dest_path) as f:
base = json.load(f)
except Exception:
base = {} # corrupt/empty live file → repo overlay becomes the file
def deep_merge(b, o):
if isinstance(b, dict) and isinstance(o, dict):
out = dict(b)
for k, v in o.items():
out[k] = deep_merge(b[k], v) if k in b else v
return out
return o # overlay wins for scalars and lists
merged = deep_merge(base, overlay)
d = os.path.dirname(dest_path)
os.makedirs(d, exist_ok=True)
fd, tmp = tempfile.mkstemp(dir=d)
with os.fdopen(fd, "w") as f:
json.dump(merged, f, indent=2, ensure_ascii=False)
f.write("\n")
os.replace(tmp, dest_path) # replaces a symlink with a real file, atomically
print(f"✓ Merged settings -> {dest_path} (repo keys applied, app keys preserved)")
PY
}
generate_permissions() {
local script="$SCRIPT_DIR/permissions/sync.py"
if [[ ! -f "$script" ]]; then
echo "⚠️ Permission generator not found: $script (skipping)"
return
fi
if ! command -v python3 &>/dev/null; then
echo "⚠️ python3 not found — skipping permission generation"
echo " (the committed permission files will be used as-is)"
return
fi
echo "Generating agent permission config from permissions/permissions.toml..."
if python3 "$script"; then
echo "✓ Permission config up to date"
else
echo "⚠️ Permission generation failed — using committed permission files"
fi
}
generate_global() {
local script="$SCRIPT_DIR/global/sync.py"
if [[ ! -f "$script" ]]; then
echo "⚠️ Global-instructions generator not found: $script (skipping)"
return
fi
if ! command -v python3 &>/dev/null; then
echo "⚠️ python3 not found — skipping global-instructions generation"
echo " (the committed instruction files will be used as-is)"
return
fi
echo "Generating global agent instructions from global/fragments/..."
if python3 "$script"; then
echo "✓ Global instructions up to date"
else
echo "⚠️ Global-instructions generation failed — using committed files"
fi
}
sync_codex_config() {
local script="$SCRIPT_DIR/codex/sync_config.py"
if [[ ! -f "$script" ]]; then
echo "⚠️ Codex config sync not found: $script (skipping)"
return
fi
if ! command -v python3 &>/dev/null; then
echo "⚠️ python3 not found — skipping Codex config sync"
return
fi
echo "Merging repo-managed Codex config..."
python3 "$script"
}
install_codex_skills() {
local dest_dir="$HOME/.agents/skills"
local claude_src="$SCRIPT_DIR/claude/skills"
local override_src="$SCRIPT_DIR/codex/skills"
mkdir -p "$dest_dir"
echo ""
echo "Installing Codex skills to $dest_dir..."
local wanted=" ${CODEX_SKILLS[*]} "
# Stale cleanup: only remove symlinks pointing into this repo whose target
# is gone or whose basename is no longer in CODEX_SKILLS. Links pointing
# outside this repo (e.g. find-skills from the npx skills CLI) are untouched.
for dest in "$dest_dir"/*; do
if [[ ! -L "$dest" ]]; then
continue
fi
local target
local name
target="$(readlink "$dest")"
name="$(basename "$dest")"
if [[ "$target" == "$SCRIPT_DIR/"* ]]; then
if [[ ! -e "$dest" ]] || [[ "$wanted" != *" $name "* ]]; then
rm -f "$dest"
echo "✓ Removed stale skill link: $dest"
fi
fi
done
for name in "${CODEX_SKILLS[@]}"; do
local source="$claude_src/$name"
# Prefer a real override dir in codex/skills/ (ignore lingering symlinks).
if [[ -d "$override_src/$name" && ! -L "$override_src/$name" ]]; then
source="$override_src/$name"
fi
if [[ ! -e "$source" ]]; then
echo "⚠️ Source for skill '$name' does not exist (skipping): $source"
continue
fi
create_symlink "$source" "$dest_dir/$name"
done
# Legacy cleanup: remove repo-pointing symlinks from the old ~/.codex/skills/
# (Codex now reads ~/.agents/skills/). Leaves .system/ and non-ours untouched.
local legacy_dir="$HOME/.codex/skills"
if [[ -d "$legacy_dir" ]]; then
for dest in "$legacy_dir"/*; do
if [[ ! -L "$dest" ]]; then
continue
fi
local target
target="$(readlink "$dest")"
if [[ "$target" == "$SCRIPT_DIR/"* ]]; then
rm -f "$dest"
echo "✓ Removed legacy Codex skill link: $dest"
fi
done
fi
}
# --- Reconcile ---
echo "Syncing agentic coding config... (${PROFILE} profile)"
echo ""
generate_permissions
echo ""
generate_global
echo ""
sync_codex_config
echo ""
for entry in "${SYMLINKS[@]}"; do
source="${entry%%:*}"
dest="${entry##*:}"
create_symlink "$source" "$dest"
done
echo ""
merge_settings
install_codex_skills
echo ""
echo "✓ Sync complete (${PROFILE} profile)."