-
Notifications
You must be signed in to change notification settings - Fork 15
Expand file tree
/
Copy pathupdate.sh
More file actions
executable file
·443 lines (398 loc) · 15.4 KB
/
Copy pathupdate.sh
File metadata and controls
executable file
·443 lines (398 loc) · 15.4 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
#!/bin/bash
# Loci — Update Engine Files
# Downloads the latest engine files from GitHub without touching user data.
# Usage: ./update.sh [--check] [--rollback]
set -e
# ─── Colors ─────────────────────────────────────────────────────────────────
BOLD='\033[1m'
DIM='\033[2m'
GREEN='\033[0;32m'
YELLOW='\033[0;33m'
RED='\033[0;31m'
CYAN='\033[0;36m'
NC='\033[0m'
BRAIN_PATH="$(cd "$(dirname "$0")" && pwd)"
BRAIN_CONFIG_PATH="$BRAIN_PATH"
MANIFEST="$BRAIN_PATH/.loci/engine-files.yml"
BACKUP_DIR="$BRAIN_PATH/.loci/backups"
REPO="codesstar/loci"
BRANCH="main"
# ─── Safety: files that must NEVER be overwritten ───────────────────────────
NEVER_TOUCH=(
"plan.md"
"inbox.md"
"me/identity.md"
"me/values.md"
"me/wellbeing.md"
"me/learned.md"
"me/goals.md"
"me/evolution.md"
"me/insights.md"
"projects/side.md"
"projects/index.md"
"projects/*"
"tasks/tasks.json"
"tasks/active.md"
"tasks/calendar.json"
"tasks/journal/*"
"tasks/daily/*"
"tasks/plans/*"
"decisions/*.md"
"references/*.md"
"references/entries/*"
"notes/*"
"people/*.md"
"people/meetings/*"
"finance/*.md"
"content/*.md"
"archive/misc/*"
"archive/planning/*"
"archive/projects/*"
"archive/tasks/*"
".loci/config.yml"
".loci/status.yml"
".loci/activity/*"
)
# ─── Helpers ────────────────────────────────────────────────────────────────
print_ok() { echo -e " ${GREEN}✓${NC} $1"; }
print_warn() { echo -e " ${YELLOW}!${NC} $1"; }
print_fail() { echo -e " ${RED}✗${NC} $1"; }
node_fs_path() {
local value="$1"
local node_platform
node_platform=$(node -p 'process.platform' 2>/dev/null | tr -d '\r' || printf '')
if [ "$node_platform" = "win32" ] && command -v cygpath >/dev/null 2>&1; then
cygpath -m "$value"
elif [ "$node_platform" = "win32" ] && command -v wslpath >/dev/null 2>&1 && [[ "$value" == /mnt/* ]]; then
wslpath -m "$value"
else
printf '%s\n' "$value"
fi
}
is_protected() {
local file="$1"
for pattern in "${NEVER_TOUCH[@]}"; do
case "$file" in
$pattern) return 0 ;;
esac
done
return 1
}
get_local_version() {
grep '^version:' "$MANIFEST" 2>/dev/null | sed 's/version:[[:space:]]*"\{0,1\}\([^"]*\)"\{0,1\}/\1/'
}
get_remote_version() {
curl -fsSL "https://raw.githubusercontent.com/$REPO/$BRANCH/.loci/engine-files.yml" 2>/dev/null \
| grep '^version:' | sed 's/version:[[:space:]]*"\{0,1\}\([^"]*\)"\{0,1\}/\1/'
}
# Parse managed files from engine-files.yml (expand ** globs against remote)
get_managed_files() {
grep '^ *- ' "$1" | sed 's/^ *- //' | while read -r pattern; do
echo "$pattern"
done
}
# ─── Rollback ───────────────────────────────────────────────────────────────
do_rollback() {
local latest_backup
latest_backup=$(ls -1d "$BACKUP_DIR"/*/ 2>/dev/null | sort -r | head -1)
if [ -z "$latest_backup" ]; then
print_fail "No backups found in $BACKUP_DIR"
exit 1
fi
echo -e "${BOLD}Rolling back to backup: $(basename "$latest_backup")${NC}"
local count=0
while IFS= read -r -d '' file; do
local rel="${file#$latest_backup}"
local target="$BRAIN_PATH/$rel"
mkdir -p "$(dirname "$target")"
cp "$file" "$target"
count=$((count + 1))
done < <(find "$latest_backup" -type f -print0)
print_ok "Restored $count files"
echo -e "${GREEN}Rollback complete.${NC}"
}
# ─── Check only ─────────────────────────────────────────────────────────────
do_check() {
local local_ver remote_ver
local_ver=$(get_local_version)
remote_ver=$(get_remote_version)
if [ -z "$remote_ver" ]; then
print_fail "Could not reach GitHub. Check your network."
exit 1
fi
echo -e " Local: ${BOLD}${local_ver}${NC}"
echo -e " Remote: ${BOLD}${remote_ver}${NC}"
if [ "$local_ver" = "$remote_ver" ]; then
echo -e " ${GREEN}Already up to date.${NC}"
else
echo -e " ${CYAN}Update available: ${local_ver} → ${remote_ver}${NC}"
echo -e " Run ${BOLD}./update.sh${NC} to update."
fi
}
# ─── Main update flow ───────────────────────────────────────────────────────
do_update() {
echo ""
echo -e "${BOLD}Loci Update${NC}"
echo ""
# 1. Check versions
local local_ver remote_ver
local_ver=$(get_local_version)
remote_ver=$(get_remote_version)
if [ -z "$remote_ver" ]; then
print_fail "Could not reach GitHub. Check your network."
exit 1
fi
echo -e " Current: ${BOLD}${local_ver}${NC} → Latest: ${BOLD}${remote_ver}${NC}"
if [ "$local_ver" = "$remote_ver" ]; then
echo ""
print_ok "Already up to date."
# Even without engine downloads, heal an invalid/legacy pointer and refresh
# connected tool blocks. Moving a brain must not require a future release.
refresh_global_blocks
return
fi
# 2. Download latest to temp directory
echo ""
echo -e "${DIM}Downloading latest version...${NC}"
local tmp_dir
tmp_dir=$(mktemp -d)
trap "rm -rf $tmp_dir" EXIT
if command -v git &>/dev/null; then
git clone --depth 1 --quiet "https://github.com/$REPO.git" "$tmp_dir/repo" 2>/dev/null
else
curl -fsSL "https://github.com/$REPO/archive/refs/heads/$BRANCH.tar.gz" | tar xz -C "$tmp_dir"
mv "$tmp_dir/loci-$BRANCH" "$tmp_dir/repo"
fi
print_ok "Downloaded"
# 3. Read managed file list from NEW manifest (in case it changed)
local new_manifest="$tmp_dir/repo/.loci/engine-files.yml"
if [ ! -f "$new_manifest" ]; then
print_fail "Downloaded version has no engine-files.yml — aborting"
exit 1
fi
# 4. Expand file patterns and collect files to update
local files_to_update=()
local skipped=()
while IFS= read -r pattern; do
# Expand globs against the downloaded repo
while IFS= read -r -d '' file; do
local rel="${file#$tmp_dir/repo/}"
# Safety check
if is_protected "$rel"; then
skipped+=("$rel (PROTECTED)")
continue
fi
files_to_update+=("$rel")
done < <(find "$tmp_dir/repo" -path "$tmp_dir/repo/$pattern" -print0 2>/dev/null)
# Handle ** globs manually
if [[ "$pattern" == *"**"* ]]; then
local base_dir="${pattern%%/**}"
while IFS= read -r -d '' file; do
local rel="${file#$tmp_dir/repo/}"
if is_protected "$rel"; then
skipped+=("$rel (PROTECTED)")
continue
fi
# Avoid duplicates
local found=0
for existing in "${files_to_update[@]}"; do
[ "$existing" = "$rel" ] && found=1 && break
done
[ "$found" -eq 0 ] && files_to_update+=("$rel")
done < <(find "$tmp_dir/repo/$base_dir" -type f -print0 2>/dev/null)
fi
done < <(get_managed_files "$new_manifest")
echo ""
echo -e "${BOLD}Files to update (${#files_to_update[@]}):${NC}"
for f in "${files_to_update[@]}"; do
if [ -f "$BRAIN_PATH/$f" ]; then
if diff -q "$BRAIN_PATH/$f" "$tmp_dir/repo/$f" &>/dev/null; then
echo -e " ${DIM} $f (unchanged)${NC}"
else
echo -e " ${CYAN}↻${NC} $f"
fi
else
echo -e " ${GREEN}+${NC} $f (new)"
fi
done
if [ ${#skipped[@]} -gt 0 ]; then
echo ""
echo -e "${DIM}Skipped (protected user data):${NC}"
for f in "${skipped[@]}"; do
echo -e " ${DIM} $f${NC}"
done
fi
# 5. Confirm
echo ""
printf " Apply update ${local_ver} → ${remote_ver}? (Y/n): "
read -r confirm
if [[ "$confirm" =~ ^[nN] ]]; then
echo -e " ${DIM}Update cancelled.${NC}"
return
fi
# 6. Backup current engine files
local timestamp
timestamp=$(date +%Y%m%d-%H%M%S)
local backup_path="$BACKUP_DIR/$timestamp"
mkdir -p "$backup_path"
for f in "${files_to_update[@]}"; do
if [ -f "$BRAIN_PATH/$f" ]; then
mkdir -p "$backup_path/$(dirname "$f")"
cp "$BRAIN_PATH/$f" "$backup_path/$f"
fi
done
print_ok "Backed up to .loci/backups/$timestamp/"
# 7. Copy new files
local updated=0
for f in "${files_to_update[@]}"; do
if [ -f "$tmp_dir/repo/$f" ]; then
mkdir -p "$BRAIN_PATH/$(dirname "$f")"
cp "$tmp_dir/repo/$f" "$BRAIN_PATH/$f"
updated=$((updated + 1))
fi
done
print_ok "Updated $updated files"
# 7a. Remove engine files that no longer exist upstream (never user data —
# only files retired from the managed set in newer releases)
local OBSOLETE_ENGINE_FILES=(
".loci/dashboard/server.py"
".loci/dashboard/build.py"
".loci/dashboard/index-ink.html"
".loci/dashboard/index-clean.html"
".loci/dashboard/seahorse-logo.png"
".loci/dashboard/avatar-callum.png"
)
local removed=0
for f in "${OBSOLETE_ENGINE_FILES[@]}"; do
if [ -f "$BRAIN_PATH/$f" ] && [ ! -f "$tmp_dir/repo/$f" ]; then
mkdir -p "$backup_path/$(dirname "$f")"
cp "$BRAIN_PATH/$f" "$backup_path/$f" 2>/dev/null
rm -f "$BRAIN_PATH/$f"
removed=$((removed + 1))
fi
done
for d in ".loci/dashboard/pro" ".loci/dashboard/demos"; do
if [ -d "$BRAIN_PATH/$d" ] && [ ! -d "$tmp_dir/repo/$d" ]; then
mkdir -p "$backup_path/$d"
cp -R "$BRAIN_PATH/$d/." "$backup_path/$d/" 2>/dev/null
rm -rf "$BRAIN_PATH/$d"
removed=$((removed + 1))
fi
done
[ "$removed" -gt 0 ] && print_ok "Removed $removed retired engine files (backed up first)"
# 8. Re-run global config (idempotent)
refresh_global_blocks
# Hooks installed in ~/.claude are copies, not symlinks. Update the native
# Node hook and retain the legacy shell copy for older settings files.
if [ -f "$BRAIN_PATH/.claude/hooks/loci-context.js" ] \
&& { grep -q '<!-- loci:start' "$HOME/.claude/CLAUDE.md" 2>/dev/null \
|| grep -q 'loci-context' "$HOME/.claude/settings.json" 2>/dev/null; }; then
mkdir -p "$HOME/.claude/hooks"
cp "$BRAIN_PATH/.claude/hooks/loci-context.js" "$HOME/.claude/hooks/loci-context.js"
chmod +x "$HOME/.claude/hooks/loci-context.js" 2>/dev/null
if [ -f "$BRAIN_PATH/.claude/hooks/loci-context.sh" ]; then
cp "$BRAIN_PATH/.claude/hooks/loci-context.sh" "$HOME/.claude/hooks/loci-context.sh"
chmod +x "$HOME/.claude/hooks/loci-context.sh" 2>/dev/null
fi
if [ -f "$BRAIN_PATH/scripts/loci-claude-settings.js" ] && command -v node >/dev/null 2>&1; then
if node "$(node_fs_path "$BRAIN_PATH/scripts/loci-claude-settings.js")" install --home "$(node_fs_path "$HOME")" >/dev/null 2>&1; then
print_ok "Claude Code hook migrated to native Node"
else
print_warn "Claude settings.json was invalid or incompatible — left unchanged"
fi
fi
print_ok "Claude Code lightweight context hook updated"
fi
# Copy slash commands
if [ -d "$BRAIN_PATH/templates/commands" ]; then
cp "$BRAIN_PATH"/templates/commands/*.md "$HOME/.claude/commands/" 2>/dev/null
print_ok "Slash commands updated"
fi
# Ensure hooks executable
chmod +x "$BRAIN_PATH"/.loci/hooks/*.sh 2>/dev/null
chmod +x "$BRAIN_PATH"/.githooks/* 2>/dev/null
chmod +x "$BRAIN_PATH"/.claude/hooks/*.sh 2>/dev/null
# Keep the `loci` launcher wired up
if [ -f "$BRAIN_PATH/bin/loci" ]; then
chmod +x "$BRAIN_PATH/bin/loci" 2>/dev/null
mkdir -p "$HOME/.local/bin"
ln -sf "$BRAIN_PATH/bin/loci" "$HOME/.local/bin/loci"
fi
# 9. Show changelog if available
if [ -f "$BRAIN_PATH/CHANGELOG.md" ]; then
echo ""
echo -e "${BOLD}What's new in ${remote_ver}:${NC}"
# Show latest changelog section
awk '/^## /{if(found) exit; found=1} found' "$BRAIN_PATH/CHANGELOG.md" | head -15
fi
echo ""
echo -e "${GREEN}Updated to ${BOLD}${remote_ver}${GREEN}.${NC} Your data is untouched."
echo -e "${DIM}Rollback: ./update.sh --rollback${NC}"
}
# ─── Refresh injected loci blocks in every connected tool ───────────────────
# Claude Code (~/.claude/CLAUDE.md), Codex (~/.codex/AGENTS.md), and WorkBuddy
# (~/.workbuddy/MEMORY.md) all carry the same instruction block. Re-injecting
# it from the (freshly updated) template is how instruction fixes reach tools
# that have no hook mechanism. User content outside the markers is untouched.
refresh_registered_brain_path() {
BRAIN_CONFIG_PATH="$BRAIN_PATH"
if command -v node >/dev/null 2>&1 && [ -f "$BRAIN_PATH/scripts/loci-path.js" ]; then
local registered_brain node_brain node_home node_helper
node_brain=$(node_fs_path "$BRAIN_PATH")
node_home=$(node_fs_path "$HOME")
node_helper=$(node_fs_path "$BRAIN_PATH/scripts/loci-path.js")
if registered_brain=$(node "$node_helper" register --brain "$node_brain" --home "$node_home" 2>/dev/null); then
BRAIN_CONFIG_PATH="$registered_brain"
else
print_warn "Could not validate ~/.loci/brain-path — existing pointer left unchanged"
fi
fi
}
refresh_global_blocks() {
refresh_registered_brain_path
local tpl="$BRAIN_PATH/templates/global-claude-block.md"
if [ ! -f "$tpl" ]; then
print_warn "templates/global-claude-block.md missing — blocks not refreshed"
return 0
fi
local block escaped_brain
escaped_brain=$(printf '%s' "$BRAIN_CONFIG_PATH" | sed 's/[&|]/\\&/g')
block=$(sed "s|<brain-path>|${escaped_brain}|g" "$tpl")
local gf tmp_gf
for gf in "$HOME/.claude/CLAUDE.md" "$HOME/.codex/AGENTS.md" "$HOME/.workbuddy/MEMORY.md"; do
if [ -f "$gf" ] && grep -q '<!-- loci:start' "$gf"; then
tmp_gf=$(mktemp)
sed '/<!-- loci:start/,/<!-- loci:end -->/d' "$gf" > "$tmp_gf"
printf "\n%s\n" "$block" >> "$tmp_gf"
mv "$tmp_gf" "$gf"
print_ok "$(basename "$gf") loci block refreshed"
fi
done
# Refresh/dedupe the native Codex hook only for an already-connected user.
# Invalid JSON is never overwritten; the helper exits non-zero instead.
if command -v node >/dev/null 2>&1 \
&& [ -f "$BRAIN_PATH/scripts/loci-codex-hook.js" ] \
&& { grep -q '<!-- loci:start' "$HOME/.codex/AGENTS.md" 2>/dev/null \
|| grep -q 'loci-context' "$HOME/.codex/hooks.json" 2>/dev/null; }; then
if node "$(node_fs_path "$BRAIN_PATH/scripts/loci-codex-hook.js")" install --brain "$(node_fs_path "$BRAIN_CONFIG_PATH")" --home "$(node_fs_path "$HOME")" >/dev/null 2>&1; then
print_ok "Codex lightweight SessionStart hook updated"
else
print_warn "Codex hooks.json was invalid or incompatible — left unchanged"
fi
fi
}
# ─── Entry point ────────────────────────────────────────────────────────────
case "${1:-}" in
--check) do_check ;;
--rollback) do_rollback ;;
--refresh-blocks) refresh_global_blocks ;;
--help|-h)
echo "Usage: ./update.sh [--check] [--rollback] [--refresh-blocks] [--help]"
echo ""
echo " (no args) Update engine files to latest version"
echo " --check Check if updates are available"
echo " --rollback Restore from the most recent backup"
echo " --refresh-blocks Re-inject the loci block into Claude Code / Codex / WorkBuddy global files from the local template"
;;
*) do_update ;;
esac