Repository navigation
Expand file tree
/
Copy pathinstall.sh
More file actions
executable file
·3544 lines (3328 loc) · 149 KB
/
Copy pathinstall.sh
File metadata and controls
executable file
·3544 lines (3328 loc) · 149 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
# brainstack installer for the global brain at ~/.agent/.
#
# Modes:
# ./install.sh -- install fresh ~/.agent/ if missing, else show status.
# Prints the full plan first and asks for
# confirmation. Non-interactive runs without
# --yes fall back to --minimal.
# ./install.sh --yes -- explicit consent for the full default install
# (no prompt, works without a TTY)
# ./install.sh --minimal -- brain + recall CLI only; touch NO host
# configs, install NO scheduler. Prints the
# commands to enable everything else later.
# ./install.sh --dry-run -- global plan mode: combine with ANY mode to
# print what would change and touch nothing
# ./install.sh --upgrade -- refresh tools + hooks; leave memory/ untouched
# ./install.sh --verify -- self-check: confirm brain is healthy
# ./install.sh --setup-digests
# -- check LLM providers, wire digest sync into
# LaunchAgent, optionally run initial backfill
# ./install.sh --setup-systemd / --remove-systemd
# -- Linux scheduler parity: install/remove the
# systemd user units (sync/dream/auto-migrate)
# ./install.sh --setup-daemon / --remove-daemon
# -- (macOS) install/remove the recall query
# daemon LaunchAgent (com.brainstack.recall-
# daemon). --no-daemon skips it on the
# default full install.
# ./install.sh --migrate <flat-memory-dir>
# -- run tools/migrate.py against the given dir
#
# Plug-in flags (work with any mode that creates a brain):
# --brain-remote <url> -- after install, init the brain as a git repo
# with this remote as origin (HTTPS or SSH).
# Also reads $BRAIN_REMOTE_URL if set.
# --push-initial-commit -- after --brain-remote, also push the first
# commit to the remote (skipped by default
# so the user can review locally first).
# --brain-root <path> -- override $BRAIN_ROOT for this run only.
# --install-scanner [name] -- attempt to install a secret scanner
# (default: trufflehog; pass `gitleaks` to
# use that instead). Skipped if already on
# PATH or no supported package manager.
# Currently supports macOS (brew) and Linux
# with apt/dnf/pacman.
# --symlink-native -- (DEFAULT, --migrate only) after migrating,
# replace the source dir with a symlink to
# $BRAIN_ROOT/memory so Claude Code's native
# writes flow into the brain. Original content
# is moved to <source>.bak.<unix-ts>.
# --no-symlink -- (--migrate only) leave the source dir in
# place after migration. Native writes will
# NOT reach the brain; you must set up your
# own forwarding to capture them.
#
# Honesty note: the FULL default install (consented via the prompt or --yes)
# DOES edit user settings: it registers Claude Code hooks in
# ~/.claude/settings.json and writes sentinel-bracketed blocks into
# ~/.claude/CLAUDE.md, ~/.codex/AGENTS.md, and ~/.cursor/.cursorrules.
# The full plan is printed BEFORE anything executes, confirmation is asked
# first (non-interactive runs without --yes fall back to --minimal), and
# every edit is reversible via ./uninstall.sh.
# Absolute path to this script, so recursive sub-invocations work no matter
# how the user launched it (./install.sh, bash install.sh, absolute path).
SELF="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)/$(basename "${BASH_SOURCE[0]}")"
set -euo pipefail
# Keep helper-Python invocations from writing bytecode caches into $HOME.
# The macOS CommandLineTools Python (the typical `python3` probed before
# walking to a newer interpreter) writes ~/Library/Caches/com.apple.python
# on every -c invocation, which would break --dry-run's nothing-changed
# guarantee.
export PYTHONDONTWRITEBYTECODE=1
REPO_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
BRAIN_ROOT="${BRAIN_ROOT:-$HOME/.agent}"
BRAIN_REMOTE="${BRAIN_REMOTE_URL:-}"
PUSH_INITIAL_COMMIT=0
INSTALL_SCANNER=""
MODE="install"
MIGRATE_SOURCE=""
# After a successful --migrate, replace the source dir with a symlink to
# $BRAIN_ROOT/memory so Claude Code's ongoing native writes flow into the
# brain instead of drifting away from it. Default ON; opt out with
# --no-symlink for users who want to keep their native dir untouched.
SYMLINK_NATIVE=1
# Track explicit user intent so we can refuse mutually exclusive flag pairs.
SYMLINK_NATIVE_FLAG_COUNT=0
# --dry-run is a GLOBAL plan mode: combined with any invocation it prints
# what would change and touches nothing on disk.
DRY_RUN=0
# --minimal: brain + recall CLI only (no host configs, no scheduler). Also
# the automatic fallback for non-interactive default installs without --yes.
MINIMAL=0
# Default-on setup modes (v0.6.0+): fresh install runs all six automatically.
# Each --no-X flag disables exactly one. Only takes effect in fresh-install
# mode; ignored when explicit --setup-X / --upgrade is passed.
NO_AUTO_MIGRATE=0
NO_LAUNCHD=0
NO_RECALL_FIRST=0
NO_AUTO_RECALL=0
NO_DAEMON=0
SKIP_MIGRATE=0
# --yes accepts all interactive migrate-discovery prompts non-interactively.
# --no-prompt declines all of them (silent skip). Either makes the install
# script safe to run from CI / wrappers without stdin attached.
ASSUME_YES=0
NO_PROMPT=0
# Captures any extra args after a mode flag so they can be forwarded to
# the relevant helper. Used for --setup-auto-migrate / --remove-auto-migrate
# which delegate to the Python helper for everything past the mode.
EXTRA_ARGS=()
while [ $# -gt 0 ]; do
case "$1" in
--upgrade) MODE="upgrade"; shift ;;
--verify) MODE="verify"; shift ;;
--setup-auto-migrate)
MODE="setup-auto-migrate"
shift
# Everything else is forwarded to auto_migrate_install.py setup.
# Note: global flags (--dry-run, --brain-root) parsed earlier
# are also forwarded — see the call site for the merge logic.
while [ $# -gt 0 ]; do EXTRA_ARGS+=("$1"); shift; done
break
;;
--remove-auto-migrate)
MODE="remove-auto-migrate"
shift
while [ $# -gt 0 ]; do EXTRA_ARGS+=("$1"); shift; done
break
;;
--setup-claude-extras)
MODE="setup-claude-extras"
shift
;;
--remove-claude-extras)
MODE="remove-claude-extras"
shift
;;
--setup-cursor-rules)
MODE="setup-cursor-rules"
shift
;;
--remove-cursor-rules)
MODE="remove-cursor-rules"
shift
;;
--setup-shell-banner)
MODE="setup-shell-banner"
shift
;;
--remove-shell-banner)
MODE="remove-shell-banner"
shift
;;
--setup-pending-hook)
MODE="setup-pending-hook"
shift
;;
--remove-pending-hook)
MODE="remove-pending-hook"
shift
;;
--setup-digests)
MODE="setup-digests"
shift
;;
--setup-statusline)
MODE="setup-statusline"
shift
;;
--remove-statusline)
MODE="remove-statusline"
shift
;;
--setup-codex-agents-md)
MODE="setup-codex-agents-md"
shift
;;
--remove-codex-agents-md)
MODE="remove-codex-agents-md"
shift
;;
--setup-pending-review-all)
MODE="setup-pending-review-all"
shift
;;
--remove-pending-review-all)
MODE="remove-pending-review-all"
shift
;;
--setup-recall-first-claude)
MODE="setup-recall-first-claude"
shift
;;
--remove-recall-first-claude)
MODE="remove-recall-first-claude"
shift
;;
--setup-recall-first-codex)
MODE="setup-recall-first-codex"
shift
;;
--remove-recall-first-codex)
MODE="remove-recall-first-codex"
shift
;;
--setup-recall-first-cursor)
MODE="setup-recall-first-cursor"
shift
;;
--remove-recall-first-cursor)
MODE="remove-recall-first-cursor"
shift
;;
--setup-recall-first-all)
MODE="setup-recall-first-all"
shift
;;
--remove-recall-first-all)
MODE="remove-recall-first-all"
shift
;;
--uninstall)
MODE="uninstall"
shift
;;
--setup-launchd)
MODE="setup-launchd"
shift
;;
--remove-launchd)
MODE="remove-launchd"
shift
;;
--setup-systemd)
MODE="setup-systemd"
shift
;;
--remove-systemd)
MODE="remove-systemd"
shift
;;
--setup-daemon)
MODE="setup-daemon"
shift
;;
--remove-daemon)
MODE="remove-daemon"
shift
;;
--purge-data)
UNINSTALL_PURGE_DATA=1
shift
;;
-y|--yes)
# Dual-purpose: in uninstall mode, auto-confirms removal.
# In install mode, auto-accepts migrate-discovery prompts.
UNINSTALL_YES=1
ASSUME_YES=1
shift
;;
--enable-auto-recall)
MODE="enable-auto-recall"
shift
;;
--disable-auto-recall)
MODE="disable-auto-recall"
shift
;;
--add-source)
MODE="add-source"
# Required: SRC path. Optional: --as DST_SUB on a later flag.
if [ $# -lt 2 ] || [[ "$2" == --* ]]; then
echo "install: --add-source requires a path" >&2
exit 2
fi
ADD_SOURCE_PATH="$2"
shift 2
;;
--as)
ADD_SOURCE_DST="${2:-}"
if [ -z "$ADD_SOURCE_DST" ]; then
echo "install: --as requires a destination subpath" >&2
exit 2
fi
shift 2
;;
--remove-source)
MODE="remove-source"
if [ $# -lt 2 ] || [[ "$2" == --* ]]; then
echo "install: --remove-source requires a path or destination" >&2
exit 2
fi
REMOVE_SOURCE_KEY="$2"
shift 2
;;
--list-sources)
MODE="list-sources"
shift
;;
--migrate)
MODE="migrate"
# Consume the source path only if the next arg is NOT another
# flag (handles `--migrate <path>`, `--migrate <path> --dry-run`,
# `--migrate --dry-run`, and bare `--migrate` correctly).
if [ $# -ge 2 ] && [[ "$2" != --* ]]; then
MIGRATE_SOURCE="$2"
shift 2
else
shift
fi
;;
--dry-run) DRY_RUN=1; shift ;;
--symlink-native)
SYMLINK_NATIVE=1
SYMLINK_NATIVE_FLAG_COUNT=$((SYMLINK_NATIVE_FLAG_COUNT + 1))
shift
;;
--no-symlink)
SYMLINK_NATIVE=0
SYMLINK_NATIVE_FLAG_COUNT=$((SYMLINK_NATIVE_FLAG_COUNT + 1))
shift
;;
--brain-remote)
BRAIN_REMOTE="${2:-}"
if [ -z "$BRAIN_REMOTE" ]; then
echo "install: --brain-remote requires a URL" >&2
exit 2
fi
shift 2
;;
--push-initial-commit)
PUSH_INITIAL_COMMIT=1
shift
;;
--brain-root)
BRAIN_ROOT="${2:-}"
if [ -z "$BRAIN_ROOT" ]; then
echo "install: --brain-root requires a path" >&2
exit 2
fi
shift 2
;;
--install-scanner)
# Optional name argument: --install-scanner trufflehog | gitleaks
# If next arg is missing or starts with `--`, default to trufflehog.
if [ $# -ge 2 ] && [[ "$2" != --* ]]; then
INSTALL_SCANNER="$2"
shift 2
else
INSTALL_SCANNER="trufflehog"
shift
fi
case "$INSTALL_SCANNER" in
trufflehog|gitleaks) : ;;
*)
echo "install: --install-scanner accepts only 'trufflehog' or 'gitleaks' (got: $INSTALL_SCANNER)" >&2
exit 2
;;
esac
;;
--no-auto-migrate)
NO_AUTO_MIGRATE=1
shift
;;
--no-launchd)
NO_LAUNCHD=1
shift
;;
--no-daemon)
# Fresh-install only opt-out (Default 6). Explicit
# --setup-daemon ignores it, same as the other --no-X flags.
NO_DAEMON=1
shift
;;
--no-recall-first)
NO_RECALL_FIRST=1
shift
;;
--no-auto-recall)
NO_AUTO_RECALL=1
shift
;;
--skip-migrate)
SKIP_MIGRATE=1
shift
;;
--minimal)
# Trust-building entry point: brain + recall CLI only. Skips all
# five defaults, the scanner offer, and migrate discovery; never
# touches host configs. The summary teaches the opt-in commands.
MINIMAL=1
shift
;;
--no-prompt)
NO_PROMPT=1
shift
;;
--help|-h)
# Print the full header comment block (everything between the
# shebang and the first non-comment line) so the help text can't
# drift out of sync with a hard-coded line range. Single awk
# process: a tail|sed pipeline here dies of SIGPIPE under
# `set -o pipefail` when sed quits early.
/usr/bin/awk 'NR == 1 { next } /^#/ { sub(/^# ?/, ""); print; next } { exit }' "$SELF"
exit 0
;;
*)
echo "install: unknown argument: $1" >&2
echo "see ./install.sh --help" >&2
exit 2
;;
esac
done
# Mutually exclusive: passing both --symlink-native and --no-symlink is
# almost always a wrapper-script bug. Refuse rather than silently using
# whichever appeared last (per API persona finding #12).
if [ "$SYMLINK_NATIVE_FLAG_COUNT" -gt 1 ]; then
echo "install: --symlink-native and --no-symlink are mutually exclusive" >&2
exit 2
fi
# BRAIN_ROOT is settled: `--brain-root` has been parsed, and nothing
# below reassigns it. Export once so every helper, sub-invocation of this
# script, and scheduled unit it renders sees the SAME brain. This used to
# be two dozen hand-written per-command assignment prefixes, and the ones
# nobody remembered to add silently wrote to ~/.agent on a custom
# --brain-root install.
export BRAIN_ROOT
# ----- Helper: install a secret scanner via the local package manager -----
# launchctl, unless BRAINSTACK_SKIP_LAUNCHCTL=1. launchd labels are per-USER,
# not per-HOME: `launchctl unload <plist in a sandbox HOME>` unloads the real
# user's job of that Label. Two full-day QA sandboxes (2026-09-04, 2026-09-08)
# ran --uninstall this way and silently stopped the live nightly dream and
# hourly sync for days. New launchctl calls go through here; the older
# sites keep their inline `BRAINSTACK_SKIP_LAUNCHCTL` checks.
_launchctl() {
if [ "${BRAINSTACK_SKIP_LAUNCHCTL:-0}" = "1" ]; then
return 0
fi
launchctl "$@"
}
maybe_install_scanner() {
local name="$1"
[ -z "$name" ] && return 0
if command -v "$name" >/dev/null 2>&1; then
echo "==> $name already on PATH; skipping install"
return 0
fi
echo "==> Installing $name (this may take a minute)..."
if command -v brew >/dev/null 2>&1; then
# Homebrew works on macOS and Linuxbrew
if [ "$name" = "trufflehog" ]; then
brew install trufflesecurity/trufflehog/trufflehog
else
brew install "$name"
fi
elif command -v apt-get >/dev/null 2>&1; then
# apt only carries gitleaks reliably; trufflehog needs a binary install
if [ "$name" = "gitleaks" ]; then
sudo apt-get update && sudo apt-get install -y gitleaks
else
echo "install: trufflehog isn't in apt; install via:" >&2
echo " curl -sSfL https://raw.githubusercontent.com/trufflesecurity/trufflehog/main/scripts/install.sh | sh -s -- -b /usr/local/bin" >&2
return 1
fi
elif command -v dnf >/dev/null 2>&1; then
sudo dnf install -y "$name" || {
echo "install: $name not found via dnf; install manually" >&2
return 1
}
elif command -v pacman >/dev/null 2>&1; then
sudo pacman -S --noconfirm "$name" || {
echo "install: $name not found via pacman; install manually" >&2
return 1
}
else
echo "install: no supported package manager (brew/apt/dnf/pacman); install $name manually" >&2
return 1
fi
if command -v "$name" >/dev/null 2>&1; then
echo "==> $name installed: $(command -v "$name")"
else
echo "install: $name installation finished but binary not on PATH; check your shell rc" >&2
return 1
fi
}
# ----- Helper: append missing gitignore rules from a template (S5 R4) -----
# append_missing_gitignore_rules <template-path> <live-gitignore-path>
#
# For each non-blank, non-comment line in the template that is not already
# present in the live file (exact match via `grep -qxF`), append it once
# under a header naming this upgrade. Never removes a line the user wrote
# by hand; running twice against the same template is a no-op.
#
# The template is the single source of ignore rules: adding one there is
# all a future change needs to do, and every existing brain picks it up on
# the next --upgrade. Replaces the ad-hoc per-rule grep/append blocks this
# file used to carry, which drifted from the template every time.
#
# See tests/test_brain_gitignore_upgrade.py.
append_missing_gitignore_rules() {
local template="$1"
local live="$2"
if [ ! -f "$template" ] || [ ! -f "$live" ]; then
return 0
fi
local missing=()
local rule
while IFS= read -r rule || [ -n "$rule" ]; do
# Comments are documentation, not rules; blanks are layout. Neither
# is copied, so a re-run never re-appends them.
case "$rule" in
''|\#*) continue ;;
esac
if ! grep -qxF -- "$rule" "$live"; then
missing+=("$rule")
fi
done < "$template"
if [ "${#missing[@]}" -eq 0 ]; then
return 0
fi
{
printf '\n# Added by brainstack --upgrade from templates/brain.gitignore.\n'
printf '# Append-only: nothing you wrote above was removed.\n'
printf '%s\n' "${missing[@]}"
} >> "$live"
echo "==> .gitignore: appended ${#missing[@]} rule(s) from the brainstack template"
}
# ----- Helper: print the install plan -----
# Shared by --dry-run (print + exit) and the consent gate (print + confirm).
# Enumerates every path the install would create or modify, based on the
# parsed flags, the platform, and the current disk state. READ-ONLY: this
# function must never touch the filesystem.
print_install_plan() {
local kind="$1" # "full" or "minimal"
local plan_platform="${BRAINSTACK_PLATFORM_OVERRIDE:-$(uname -s)}"
echo ""
echo "==> Install plan ($kind install); nothing has been changed yet:"
echo ""
echo " Brain (created if missing):"
echo " $BRAIN_ROOT/ memory tree + tools/ + harness/"
echo " $BRAIN_ROOT/.gitignore default ignore rules"
echo " $BRAIN_ROOT/redact-private.txt private redaction patterns stub"
if [ -n "$BRAIN_REMOTE" ]; then
echo " git init + remote origin = $BRAIN_REMOTE"
if [ "$PUSH_INITIAL_COMMIT" -eq 1 ]; then
echo " initial commit pushed to origin (--push-initial-commit)"
else
echo " initial commit created locally (not pushed)"
fi
fi
echo ""
echo " Recall CLI:"
echo " $REPO_DIR/.venv package venv (created if missing)"
echo " $HOME/.local/bin/recall symlink onto your PATH"
if [ "$kind" = "minimal" ]; then
echo ""
echo " Nothing else: no host configs are touched, no scheduler is"
echo " installed. The summary lists the opt-in commands."
return 0
fi
echo ""
echo " Scheduler (hourly sync + nightly dream + hourly auto-migrate):"
if [ "$NO_LAUNCHD" = "1" ]; then
echo " skipped (--no-launchd)"
elif [ "$plan_platform" = "Darwin" ]; then
echo " $HOME/Library/LaunchAgents/com.user.agent-sync.plist"
echo " $HOME/Library/LaunchAgents/com.user.agent-dream.plist"
echo " $HOME/Library/LaunchAgents/com.brainstack.auto-migrate.plist"
elif [ "$plan_platform" = "Linux" ] && command -v systemctl >/dev/null 2>&1; then
echo " ${XDG_CONFIG_HOME:-$HOME/.config}/systemd/user/brainstack-{sync,dream,auto-migrate}.{service,timer}"
else
echo " NONE: no launchd or systemd on this platform ($plan_platform);"
echo " set up cron manually or run ./install.sh --setup-systemd where available"
fi
echo ""
echo " Warm recall daemon (keeps the retriever and index resident):"
if [ "$NO_DAEMON" = "1" ]; then
echo " skipped (--no-daemon)"
elif [ "$plan_platform" = "Darwin" ]; then
echo " $HOME/Library/LaunchAgents/com.brainstack.recall-daemon.plist"
echo " socket at $BRAIN_ROOT/runtime/recall.sock"
else
echo " NONE: launchd-only today ($plan_platform); run 'recall serve'"
echo " under your own supervisor instead"
fi
echo ""
echo " Host configs (sentinel-bracketed edits, reversible via ./uninstall.sh):"
echo " $HOME/.claude/settings.json Claude Code UserPromptSubmit hook (auto-recall)"
echo " $HOME/.claude/CLAUDE.md recall-first directive block"
echo " $HOME/.codex/AGENTS.md recall-first directive block"
echo " $HOME/.cursor/.cursorrules recall-first directive block"
echo " $BRAIN_ROOT/runtime/pyproject.toml enable_auto_recall flag"
# Migrate discovery: list the candidate dirs that WOULD be offered.
local plan_candidates=()
if [ "$SKIP_MIGRATE" != "1" ]; then
if [ -d "$HOME/.claude/projects" ]; then
local d
for d in "$HOME/.claude/projects"/*/memory; do
[ -d "$d" ] || continue
plan_candidates+=("$d")
done
fi
[ -d "$HOME/.codex" ] && plan_candidates+=("$HOME/.codex")
[ -d "$HOME/.cursor" ] && plan_candidates+=("$HOME/.cursor")
fi
echo ""
echo " Migrate discovery (mirror-only; sources are never replaced):"
if [ "$SKIP_MIGRATE" = "1" ]; then
echo " skipped (--skip-migrate)"
elif [ "${#plan_candidates[@]}" -eq 0 ]; then
echo " no candidate memory dirs found"
else
local c
for c in "${plan_candidates[@]}"; do
echo " candidate: $c"
done
fi
# Scanner offer
if [ -z "$INSTALL_SCANNER" ] \
&& ! command -v trufflehog >/dev/null 2>&1 \
&& ! command -v gitleaks >/dev/null 2>&1; then
echo ""
echo " Secret scanner: none found on PATH; the install will offer to"
echo " install trufflehog (sync refuses to push without a scanner)."
elif [ -n "$INSTALL_SCANNER" ]; then
echo ""
echo " Secret scanner: $INSTALL_SCANNER will be installed if missing (--install-scanner)"
fi
echo ""
echo " Everything above is reversible with ./uninstall.sh"
}
# ----- Python version check -----
# Auto-detect: if the default `python3` isn't >= 3.10, walk preferred
# versions (matches sync.sh's behavior). Codex QA caught that
# `--verify` on macOS hit the bundled CommandLineTools python 3.9
# and aborted before even reaching the digest checks.
PYTHON_BIN="${PYTHON_BIN:-python3}"
if ! "$PYTHON_BIN" -c 'import sys; sys.exit(0 if sys.version_info >= (3, 10) else 1)' 2>/dev/null; then
for cand in python3.13 python3.12 python3.11 python3.10; do
if command -v "$cand" >/dev/null 2>&1; then
PYTHON_BIN="$cand"
break
fi
done
fi
if ! command -v "$PYTHON_BIN" >/dev/null 2>&1; then
echo "install: $PYTHON_BIN not found in PATH" >&2
exit 1
fi
PY_VER="$("$PYTHON_BIN" -c 'import sys; print(f"{sys.version_info.major}.{sys.version_info.minor}")')"
PY_MAJOR="$("$PYTHON_BIN" -c 'import sys; print(sys.version_info.major)')"
PY_MINOR="$("$PYTHON_BIN" -c 'import sys; print(sys.version_info.minor)')"
if [ "$PY_MAJOR" -lt 3 ] || { [ "$PY_MAJOR" -eq 3 ] && [ "$PY_MINOR" -lt 10 ]; }; then
echo "install: Python >= 3.10 required (found $PY_VER at $($PYTHON_BIN -c 'import sys;print(sys.executable)'))" >&2
echo ""
echo "On macOS: brew install python@3.13" >&2
echo "Then: PYTHON_BIN=python3.13 ./install.sh" >&2
exit 1
fi
# Resolve absolute path so launchd plists / hook commands don't depend on PATH.
PYTHON_ABS="$("$PYTHON_BIN" -c 'import sys; print(sys.executable)')"
# ----- Secret scanner check / optional install -----
# sync.sh fails closed without trufflehog or gitleaks. If --install-scanner
# was passed, attempt installation now; otherwise warn so the user can
# install before first sync.
if [ -n "$INSTALL_SCANNER" ] && [ "$DRY_RUN" = "1" ]; then
# Global plan mode: never invoke the package manager. Without this gate,
# `--dry-run --install-scanner` REALLY installed the scanner.
echo "==> DRY RUN: would install $INSTALL_SCANNER via the local package manager (skipped)"
elif [ -n "$INSTALL_SCANNER" ]; then
maybe_install_scanner "$INSTALL_SCANNER" || {
echo "install: scanner install failed; sync.sh will refuse to push" >&2
echo " Set SYNC_ALLOW_NO_SCANNER=1 to bypass (NOT RECOMMENDED)." >&2
}
elif ! command -v trufflehog >/dev/null 2>&1 && ! command -v gitleaks >/dev/null 2>&1; then
echo "" >&2
echo "install: WARNING - no secret scanner found on PATH." >&2
echo " sync.sh requires trufflehog or gitleaks to push." >&2
echo "" >&2
echo " Install via this script:" >&2
echo " ./install.sh --install-scanner # default: trufflehog" >&2
echo " ./install.sh --install-scanner gitleaks # alternative" >&2
echo "" >&2
echo " Or manually:" >&2
echo " brew install trufflehog # or" >&2
echo " brew install gitleaks" >&2
echo "" >&2
fi
# ----- Mode: migrate -----
if [ "$MODE" = "migrate" ]; then
# No source path → drop into discovery + interactive flow.
if [ -z "$MIGRATE_SOURCE" ]; then
if [ "$DRY_RUN" = "1" ]; then
echo "install: --dry-run requires a source path; pass --migrate <path> --dry-run, or omit --dry-run to run interactive discovery." >&2
exit 2
fi
# Brain root must exist before discovery — discovery itself doesn't
# write, but it's pointless without a target to migrate into.
if [ ! -d "$BRAIN_ROOT" ]; then
echo "install: $BRAIN_ROOT does not exist; run install first" >&2
exit 2
fi
if [ ! -f "$BRAIN_ROOT/tools/migrate_dispatcher.py" ]; then
echo "install: $BRAIN_ROOT/tools/migrate_dispatcher.py is missing" >&2
echo " run: ./install.sh --upgrade to refresh tools" >&2
exit 2
fi
"$PYTHON_BIN" "$BRAIN_ROOT/tools/migrate_dispatcher.py" interactive
exit $?
fi
# --dry-run with a source: run plan, write nothing.
if [ "$DRY_RUN" = "1" ]; then
if [ ! -e "$MIGRATE_SOURCE" ] && [ ! -L "$MIGRATE_SOURCE" ]; then
echo "install: migrate source not found: $MIGRATE_SOURCE" >&2
exit 2
fi
if [ ! -d "$BRAIN_ROOT" ]; then
echo "install: $BRAIN_ROOT does not exist; run install first" >&2
exit 2
fi
if [ ! -f "$BRAIN_ROOT/tools/migrate_dispatcher.py" ]; then
echo "install: $BRAIN_ROOT/tools/migrate_dispatcher.py is missing" >&2
echo " run: ./install.sh --upgrade to refresh tools" >&2
exit 2
fi
"$PYTHON_BIN" "$BRAIN_ROOT/tools/migrate_dispatcher.py" plan "$MIGRATE_SOURCE" "$BRAIN_ROOT"
exit $?
fi
# Strip trailing slash. Shell completion happily appends one when the
# arg is a directory; later `mv tmp_link "$MIGRATE_SOURCE"` would treat
# a trailing-slash destination as "must be an existing dir" and fail
# after we've already moved the source to backup.
MIGRATE_SOURCE="${MIGRATE_SOURCE%/}"
# Use -e (existence) so symlinks pass too; we distinguish symlink vs
# regular dir below. The pair `-e + later -d` is intentional — see comment
# at the bottom of this block.
if [ ! -e "$MIGRATE_SOURCE" ] && [ ! -L "$MIGRATE_SOURCE" ]; then
echo "install: migrate source not found: $MIGRATE_SOURCE" >&2
exit 2
fi
if [ ! -d "$BRAIN_ROOT" ]; then
echo "install: $BRAIN_ROOT does not exist; run install first" >&2
exit 2
fi
# The brain memory dir must exist before we can compare a symlink against
# it, AND `migrate.py` writes into it. Without this guard the idempotency
# check could spuriously fall through (per reliability/security review).
if [ ! -d "$BRAIN_ROOT/memory" ]; then
echo "install: $BRAIN_ROOT/memory does not exist; run install first" >&2
exit 2
fi
# Helper: canonical absolute path via Python's os.path.realpath. Handles
# absolute and relative symlink targets, broken symlinks (returns the
# input path unchanged for non-existent targets) and macOS BSD vs GNU
# readlink differences. Echoes the resolved path to stdout, empty on
# any error.
resolve_real() {
"$PYTHON_BIN" -c 'import os, sys
try:
print(os.path.realpath(sys.argv[1]))
except Exception:
pass' "$1" 2>/dev/null
}
brain_resolved="$(resolve_real "$BRAIN_ROOT/memory")"
if [ -z "$brain_resolved" ]; then
# `brain_resolved` is what we'll write into the symlink (when the
# swap runs). It must be absolute regardless of how the user passed
# `--brain-root` / `BRAIN_ROOT` (relative paths would create a
# broken symlink resolved relative to the symlink's parent dir).
echo "install: could not resolve $BRAIN_ROOT/memory to an absolute path" >&2
exit 2
fi
if [ -L "$MIGRATE_SOURCE" ]; then
link_resolved="$(resolve_real "$MIGRATE_SOURCE")"
if [ -n "$link_resolved" ] && [ "$link_resolved" = "$brain_resolved" ]; then
# Already pointing at the right place — true no-op.
echo "==> $MIGRATE_SOURCE is already symlinked to $BRAIN_ROOT/memory — no-op"
exit 0
fi
# Symlink exists but points elsewhere — refuse to silently overwrite
# user-owned topology (per reliability persona finding #4 + security
# finding #11). The user must remove the existing symlink themselves
# if they really want to retarget it.
echo "install: $MIGRATE_SOURCE is a symlink, but its target ($link_resolved)" >&2
echo " is not $BRAIN_ROOT/memory ($brain_resolved)." >&2
echo " Refusing to overwrite. Remove the symlink and re-run if intended." >&2
exit 2
fi
# Not a symlink — must be a real directory at this point.
if [ ! -d "$MIGRATE_SOURCE" ]; then
echo "install: migrate source must be a directory: $MIGRATE_SOURCE" >&2
exit 2
fi
echo "==> Migrating $MIGRATE_SOURCE -> $BRAIN_ROOT"
# Detect format up front so we can route through the right path.
# Per codex review of PR-B: previously this branch always invoked
# migrate.py directly, which is the Claude-Code adapter — running it
# on a Cursor or Codex source produced garbled output (Cursor plans
# routed to personal/notes/ as generic Claude misc) AND would have
# symlinked the native dir to the brain (wrong for Cursor/Codex,
# whose tools keep writing to their own dirs).
src_format="$("$PYTHON_BIN" "$BRAIN_ROOT/tools/migrate_dispatcher.py" plan "$MIGRATE_SOURCE" "$BRAIN_ROOT" 2>&1 | grep -E "^ Detected format:" | sed 's/^ Detected format: *//' | head -1)"
case "$src_format" in
claude-code-flat|claude-code-nested|claude-code-mixed)
# Claude Code path: the legacy migrate.py invocation. After
# success, the symlink swap below installs the native symlink
# so Claude Code's ongoing auto-memory writes flow into the brain.
if ! "$PYTHON_BIN" "$BRAIN_ROOT/tools/migrate.py" "$MIGRATE_SOURCE" "$BRAIN_ROOT"; then
echo "install: migrate.py failed; not symlinking native dir" >&2
echo " migration data (if any wrote successfully) is in $BRAIN_ROOT/memory" >&2
echo " your source dir at $MIGRATE_SOURCE is unchanged" >&2
echo " tip: if this is a Python version error, retry with" >&2
echo " PYTHON_BIN=python3.13 ./install.sh --migrate $MIGRATE_SOURCE" >&2
exit 1
fi
;;
*)
# Non-Claude path: dispatcher routes to the right adapter
# (Cursor today; PR-C's Codex adapter next). These tools
# keep writing to their native dirs, so SYMLINK_NATIVE is
# ignored — we ingest a snapshot only.
if ! "$PYTHON_BIN" "$BRAIN_ROOT/tools/migrate_dispatcher.py" execute "$MIGRATE_SOURCE" "$BRAIN_ROOT"; then
echo "install: dispatcher migration failed for format=$src_format" >&2
echo " your source dir at $MIGRATE_SOURCE is unchanged" >&2
exit 1
fi
# Suppress the symlink swap below — it's Claude-only.
SYMLINK_NATIVE=0
echo "==> Non-Claude source ($src_format) migrated as a snapshot."
echo " Source dir at $MIGRATE_SOURCE left untouched. Re-run when"
echo " you want to import newer entries."
;;
esac
if [ "$SYMLINK_NATIVE" = "1" ]; then
# Replace native source with a symlink to brain/memory so future
# native writes (Claude Code, Cursor, etc.) flow into the brain.
#
# Atomic-ish symlink swap (per reliability + security review):
# 1. Create the new symlink at a sibling temp name.
# 2. Move the original source dir to the timestamped backup.
# 3. Rename the temp symlink into the original source path.
# If step 1 or 2 fails, the source is intact. If step 3 fails, the
# backup retains the original data and the temp symlink is left for
# the user to inspect — no silent data loss.
#
# `mktemp -d` would be cleaner for the backup name, but we want the
# backup to live as a sibling of the source, and `mktemp -d` doesn't
# accept a custom parent on all macOS versions reliably. Use a high-
# resolution timestamp to avoid same-second collisions.
ts="$(date +%s)"
# Append PID + a short random tag to avoid same-second collisions
# whether the backup-naming attacker is human, scripted, or just two
# parallel migrations of separate dirs.
rand="$RANDOM-$$"
backup="${MIGRATE_SOURCE%/}.bak.$ts.$rand"
if [ -e "$backup" ] || [ -L "$backup" ]; then
echo "install: backup target $backup already exists; aborting" >&2
exit 1
fi
tmp_link="${MIGRATE_SOURCE%/}.symlink-tmp.$ts.$rand"
if [ -e "$tmp_link" ] || [ -L "$tmp_link" ]; then
echo "install: temp symlink path $tmp_link already exists; aborting" >&2
exit 1
fi
# Step 1 — make the new symlink at the temp name. Use the resolved
# absolute path (computed above) so a relative `--brain-root` doesn't
# produce a symlink whose target dangles relative to the source's
# parent dir.
if ! ln -s "$brain_resolved" "$tmp_link"; then
echo "install: failed to create temp symlink at $tmp_link" >&2
echo " source dir at $MIGRATE_SOURCE is unchanged" >&2
echo " migration data is in $BRAIN_ROOT/memory" >&2
exit 1
fi
# Step 2 — move the original source to the backup.
if ! mv "$MIGRATE_SOURCE" "$backup"; then
echo "install: failed to back up $MIGRATE_SOURCE -> $backup" >&2
echo " migration data is in $BRAIN_ROOT/memory; source is intact" >&2
echo " remove $tmp_link and re-run --migrate to retry the symlink swap" >&2
rm -f "$tmp_link" 2>/dev/null || true
exit 1
fi
# Step 3 — rename the temp symlink into the source position. If this
# fails, the user is left with no source dir and no symlink; surface
# the recovery path explicitly.
if ! mv "$tmp_link" "$MIGRATE_SOURCE"; then
echo "install: failed to install symlink at $MIGRATE_SOURCE" >&2
echo " source data is preserved at $backup" >&2
echo " temp symlink left at $tmp_link" >&2
echo " to recover manually: mv \"$backup\" \"$MIGRATE_SOURCE\"" >&2
exit 1
fi
echo "==> Backed up original source -> $backup"
echo "==> Symlinked $MIGRATE_SOURCE -> $BRAIN_ROOT/memory"
echo " Native auto-memory writes now flow into the brain."
else
echo "==> --no-symlink set; leaving $MIGRATE_SOURCE in place" >&2
echo "==> WARNING: native writes after this point will NOT reach the brain." >&2
echo " Re-run with --symlink-native (or symlink manually) to capture them." >&2
fi
exit 0
fi
# ----- Mode: setup-auto-migrate / remove-auto-migrate -----
# Delegate to the Python helper. Forwards everything after the mode flag
# (--enable, --disable, --all, --none, --dry-run, --print-plist, etc.).
if [ "$MODE" = "setup-auto-migrate" ] || [ "$MODE" = "remove-auto-migrate" ]; then
helper="$BRAIN_ROOT/tools/auto_migrate_install.py"
if [ ! -f "$helper" ]; then
echo "install: $helper is missing" >&2
echo " run: ./install.sh --upgrade to refresh tools" >&2
exit 2
fi
# Forward globally-parsed flags to the helper. `--dry-run` (parsed by
# this wrapper) is the most important one — codex review P2 caught
# `./install.sh --dry-run --setup-auto-migrate --all` performing a
# real install because DRY_RUN never reached argparse.
forwarded_args=()
if [ "$DRY_RUN" = "1" ]; then
forwarded_args+=(--dry-run)
fi
forwarded_args+=(--brain-root "$BRAIN_ROOT")
# If the user ALSO passed --brain-root in the post-flag args, the
# helper's argparse takes the LAST one, which is what they intended.
# `${EXTRA_ARGS[@]+"${EXTRA_ARGS[@]}"}` guards against `set -u` tripping
# on an empty array under macOS bash 3.2 — `--remove-auto-migrate` with
# no trailing flags is the common case and used to error with
# "EXTRA_ARGS[@]: unbound variable" before this guard.
if [ "$MODE" = "setup-auto-migrate" ]; then
"$PYTHON_BIN" "$helper" setup "${forwarded_args[@]}" ${EXTRA_ARGS[@]+"${EXTRA_ARGS[@]}"}
else
"$PYTHON_BIN" "$helper" remove "${forwarded_args[@]}" ${EXTRA_ARGS[@]+"${EXTRA_ARGS[@]}"}
fi
exit $?
fi
# ----- Mode: verify -----
if [ "$MODE" = "verify" ]; then
echo "==> Verifying brain health at $BRAIN_ROOT"
fail=0
check() {
local desc="$1"
local cmd="$2"
if eval "$cmd" >/dev/null 2>&1; then
printf " ✓ %s\n" "$desc"
else
printf " ✗ %s\n" "$desc"
fail=1
fi
}
check "BRAIN_ROOT exists" "test -d '$BRAIN_ROOT'"
check "memory/ layout present" "test -d '$BRAIN_ROOT/memory/episodic' -a -d '$BRAIN_ROOT/memory/working' -a -d '$BRAIN_ROOT/memory/candidates' -a -d '$BRAIN_ROOT/memory/semantic'"
check "AGENT_LEARNINGS.jsonl present" "test -f '$BRAIN_ROOT/memory/episodic/AGENT_LEARNINGS.jsonl'"
check "redact.py executable" "test -x '$BRAIN_ROOT/tools/redact.py' -o -f '$BRAIN_ROOT/tools/redact.py'"
check "redact_jsonl.py present" "test -f '$BRAIN_ROOT/tools/redact_jsonl.py'"
# sync.sh refuses to push without this, so a partial install is a
# silently non-syncing brain unless we surface it here.
check "scan_gate.py present" "test -f '$BRAIN_ROOT/tools/scan_gate.py'"
check "dream_runner.py present" "test -f '$BRAIN_ROOT/tools/dream_runner.py'"
check "atomic helper present" "test -f '$BRAIN_ROOT/memory/_atomic.py'"
check "global wrapper hook present" "test -f '$BRAIN_ROOT/harness/hooks/agentic_post_tool_global.py'"
check "vendored hook present" "test -f '$BRAIN_ROOT/harness/hooks/claude_code_post_tool.py'"