Skip to content

Use Garry Tan's exact Claude Code setup: 23 opinionated tools that serve as CEO, Designer, Eng Manager, Release Manager, Doc Engineer, and QA

TypeScript

feat(v1.9.0.0): gbrain-sync — cross-machine gstack memory (#1151)

Garry Tan9dbaf906cffa
diff --git a/CHANGELOG.md b/CHANGELOG.md
index 4b5e43f4c..27c832caa 100644
--- a/CHANGELOG.md
+++ b/CHANGELOG.md
@@ -1,5 +1,80 @@
# Changelog
+## [1.9.0.0] - 2026-04-23
+
+## **Your gstack memory now travels with you. Cross-machine brain via a private git repo + optional GBrain indexing, no daemon, no credential leaks.**
+
+gstack session memory (learnings, plans, designs, retros, developer profile) used to die at the machine boundary. Now it doesn't. `gstack-brain-init` turns `~/.gstack/` into a git repo with an explicit allowlist, writer shims enqueue changed files at write-time, and a preamble-boundary sync pushes them to a private git remote of your choice. GBrain is the first consumer but the architecture is pluggable — Codex, OpenClaw, or anything else can be a reader later. No daemon, no background process, no new auth surface.
+
+The feature shipped after four plan reviews: /office-hours shaping, /plan-eng-review (6 issues → CLEAR), /plan-ceo-review (SELECTIVE EXPANSION, 2 cherry-picks accepted), /codex twice (16+16 findings applied, daemon model dropped in round 2), and /plan-devex-review (6/10 → 8/10, docs elevated to full treatment). The scope simplification from Codex round 2 alone removed ~1 week of daemon lifecycle surface.
+
+### What you can now do
+
+- **Initialize cross-machine sync:** `gstack-brain-init` creates a private git repo (GitHub via `gh`, or any git URL — GitLab, Gitea, self-hosted). 30-90 second TTHW.
+- **See yesterday's laptop on today's desktop:** copy `~/.gstack-brain-remote.txt` to the new machine, run `gstack-brain-restore`, and your learnings follow you.
+- **Control what syncs:** one-time privacy stop-gate on first run — `full` (everything allowlisted), `artifacts-only` (plans/designs/retros/learnings, skip behavioral), `off` (decline).
+- **Sleep through the conflict case:** two machines writing the same JSONL file the same day merge cleanly via a ts-sort-plus-hash-fallback merge driver registered automatically.
+- **Uninstall cleanly:** `gstack-brain-uninstall` removes the sync layer, leaves your data intact.
+- **Never push a secret:** AWS keys, GitHub tokens (`ghp_`/`gho_`/`ghu_`/`ghs_`/`ghr_`/`github_pat_`), OpenAI `sk-` keys, PEM blocks, JWTs, and bearer-token-in-JSON patterns are all blocked before push. `--skip-file <path>` gives you a single-command escape hatch for false positives.
+
+### The numbers that matter
+
+Source: integration smoke tests run during implementation, plus 27-test consolidated suite (`test/brain-sync.test.ts`). End-to-end round trip (init on machine A → write learning → restore on machine B → see the learning) verified inline.
+
+| Surface | Shape |
+|---|---|
+| New binaries | 8 (`gstack-brain-init`, `-enqueue`, `-sync`, `-consumer`, `-reader` alias, `-restore`, `-uninstall`, `gstack-jsonl-merge`) |
+| Config keys | 2 enum-validated (`gbrain_sync_mode`: off/artifacts-only/full; `gbrain_sync_mode_prompted`: bool) |
+| Writer shims modified | 4 (learnings-log, timeline-log, review-log, developer-profile on --migrate path) |
+| Writers deliberately NOT synced | 2 (question-log, question-preference — per-machine UX state, Codex v2 decision) |
+| Sync granularity | per-skill-boundary via `gstack-brain-sync --once` from preamble (no daemon) |
+| Privacy tiers | 3 (full / artifacts-only / off) |
+| Secret patterns blocked | 6 families (AWS, GH tokens, OpenAI, PEM, JWT, bearer-in-JSON) |
+| User-facing naming | `reader` (CLI); internal data model stays `consumer` per Codex-v2 DX decision |
+| New-machine discovery | auto via `~/.gstack-brain-remote.txt` file (URL-only, no secrets) |
+
+### What this means for you
+
+Work on the laptop Monday. Switch to the desktop Tuesday. Skill preamble sees the remote URL, offers `gstack-brain-restore`, your Monday learnings surface on Tuesday. The pattern scales to N consumers: today GBrain is the primary reader, tomorrow Codex or OpenClaw can subscribe without refactoring the sync.
+
+### Itemized changes
+
+#### Added
+
+- `bin/gstack-brain-init` — idempotent first-run setup. Turns `~/.gstack/` into a git repo with `.gitignore = *`, writes canonical `.brain-allowlist` + `.brain-privacy-map.json`, installs pre-commit secret-scan hook, registers JSONL merge driver, creates private remote via `gh repo create --private` (or accepts `--remote <url>`), writes `~/.gstack-brain-remote.txt` for new-machine discovery.
+- `bin/gstack-brain-sync` — core sync. Subcommands: `--once` (drain queue, secret-scan staged diff, commit with template message, push with fetch+merge retry), `--status`, `--skip-file <path>`, `--drop-queue --yes`, `--discover-new` (walks allowlist globs with mtime+size cursor).
+- `bin/gstack-brain-enqueue` — atomic-append shim called by writers. Silent no-op when feature disabled.
+- `bin/gstack-brain-consumer` + `bin/gstack-brain-reader` (symlink alias) — manage the consumer/reader registry in `consumers.json`. User-facing "reader", internal "consumer".
+- `bin/gstack-brain-restore` — new-machine bootstrap with safety gates (refuses dangerous clobber, re-registers merge drivers, prompts for per-consumer tokens since tokens stay machine-local).
+- `bin/gstack-brain-uninstall` — clean off-ramp. Removes `.git` + `.brain-*` files + `consumers.json` + config keys. Preserves user data (learnings etc). Optional `--delete-remote` for the GitHub repo.
+- `bin/gstack-jsonl-merge` — git merge driver. Concat-dedup-sort by ISO `ts` field; deterministic SHA-256 hash fallback when `ts` is missing.
+- `scripts/resolvers/preamble/generate-brain-sync-block.ts` — preamble bash block. New-machine restore hint, one-time privacy stop-gate, `--once` at skill start + end, once-daily auto-pull, `BRAIN_SYNC:` status line on every skill run.
+- `docs/gbrain-sync.md` — user guide (setup, first-use, restore, privacy modes, secret protection, uninstall).
+- `docs/gbrain-sync-errors.md` — error lookup index (problem / cause / fix for every user-visible error).
+- `test/brain-sync.test.ts` — 27-test consolidated suite: config isolation, enqueue atomicity, merge driver, secret scan across all 6 regex families, init+sync+restore round-trip, uninstall preserves data, `--discover-new` cursor idempotence, `--skip-file` remediation.
+
+#### Changed
+
+- `bin/gstack-config` — added 2 validated keys (`gbrain_sync_mode` enum, `gbrain_sync_mode_prompted` bool). Also accepts `GSTACK_HOME` env override alongside legacy `GSTACK_STATE_DIR` for test isolation (Codex v2 fix).
+- `bin/gstack-learnings-log`, `gstack-timeline-log`, `gstack-review-log`, `gstack-developer-profile` — each gains one backgrounded `gstack-brain-enqueue` call after its local write. Fire-and-forget, silent no-op when sync is off.
+- `bin/gstack-timeline-log` header comment — updated "local-only, never sent anywhere" to reflect the new privacy-gated sync contract (only applies when user explicitly opts into `full` mode).
+- `scripts/resolvers/preamble.ts` — composition root wires in the new `generateBrainSyncBlock`.
+- `README.md` — new "Cross-machine memory with GBrain sync" section near the top, plus docs-table entry linking to `docs/gbrain-sync.md` and `docs/gbrain-sync-errors.md`.
+
+#### For contributors
+
+- Sync respects `GSTACK_HOME=/tmp/test-$$` so tests never bleed into real `~/.gstack/config.yaml`. New test `test/brain-sync-env-isolation` logic baked into the consolidated suite.
+- The consumer registry lives in `consumers.json` (synced); tokens stay in `gstack-config` (local, never synced). Restore prompts for tokens on new machines.
+- Merge drivers require local `git config merge.<name>.driver=...` registration, not just `.gitattributes`. Both `init` and `restore` register them; uninstall clears them.
+- Pre-commit hook is defense-in-depth only. Primary secret scan runs in `gstack-brain-sync --once` BEFORE staging.
+- The fnmatch glob engine doesn't handle `**` the way git's gitignore does; allowlist uses explicit one- and two-level patterns instead.
+- GBrain HTTP ingest endpoint contract is a cross-project dependency (flagged as v1 blocker for real-world dogfooding). v1 of gbrain-sync ships on this branch regardless; GBrain-side work lands in a separate branch/repo.
+
+#### Known follow-ups
+
+- `test/brain-sync.test.ts` — 12 of 27 tests pass on first bun-test run; remaining 15 hit bun-test's 5s default timeout (spawnSync-heavy git operations). Behaviors verified via integration smokes during implementation. Test infrastructure needs a 30s per-test timeout wrapper.
+- Three unmerged team-sync branches (`garrytan/team-supabase-store`, `garrytan/fix-team-setup`, `garrytan/team-install-mode`) should be formally closed if team-sync isn't landing — flagged in the CEO plan.
+- Pre-existing golden-file regression test failure in `test/host-config.test.ts` (Codex ship skill baseline) exists on `main` too — unrelated to this PR, tracked separately.
## [1.6.4.0] - 2026-04-22
## **Sidebar prompt-injection defense got half as noisy, half as trusting of any single classifier.**
diff --git a/README.md b/README.md
index 05001dce2..1d63004d0 100644
--- a/README.md
+++ b/README.md
@@ -359,12 +359,42 @@ I open sourced how I build software. You can fork it and make it your own.
> Come work at YC — [ycombinator.com/software](https://ycombinator.com/software)
> Extremely competitive salary and equity. San Francisco, Dogpatch District.
+## Cross-machine memory with GBrain sync
+
+gstack accumulates a lot of useful state on your laptop: learnings, CEO
+plans, design docs, retros, developer profile. Today, all of that dies when
+you switch machines. **GBrain sync** optionally pushes a curated, secret-scanned
+subset to a private git repo so your memory follows you, and (if you use
+GBrain) becomes indexable there.
+
+One command to turn it on:
+
+```bash
+gstack-brain-init
+```
+
+That creates a private GitHub repo (or any git remote you prefer —
+GitLab, Gitea, self-hosted). Every skill run syncs the queue at its
+start and end boundaries. No daemon, no background process. A one-time
+privacy prompt asks how much you want to share (everything allowlisted /
+artifacts only / off). Secret-shaped content (AWS keys, GitHub tokens,
+PEM blocks, JWTs, etc.) is blocked from sync before it leaves your
+machine.
+
+New machine? Copy `~/.gstack-brain-remote.txt` over, run
+`gstack-brain-restore`, and yesterday's learnings surface on today's
+laptop.
+
+Full guide: [docs/gbrain-sync.md](docs/gbrain-sync.md) •
+Error index: [docs/gbrain-sync-errors.md](docs/gbrain-sync-errors.md)
+
## Docs
| Doc | What it covers |
|-----|---------------|
| [Skill Deep Dives](docs/skills.md) | Philosophy, examples, and workflow for every skill (includes Greptile integration) |
| [Builder Ethos](ETHOS.md) | Builder philosophy: Boil the Lake, Search Before Building, three layers of knowledge |
+| [GBrain Sync](docs/gbrain-sync.md) | Cross-machine memory setup, privacy modes, troubleshooting |
| [Architecture](ARCHITECTURE.md) | Design decisions and system internals |
| [Browser Reference](BROWSER.md) | Full command reference for `/browse` |
| [Contributing](CONTRIBUTING.md) | Dev setup, testing, contributor mode, and dev mode |
diff --git a/SKILL.md b/SKILL.md
index 95f22604b..ec9797154 100644
--- a/SKILL.md
+++ b/SKILL.md
@@ -349,6 +349,105 @@ AI orchestrator (e.g., OpenClaw). In spawned sessions:
- Focus on completing the task and reporting results via prose output.
- End with a completion report: what shipped, decisions made, anything uncertain.
+## GBrain Sync (skill start)
+
+```bash
+# gbrain-sync: drain pending writes, pull once per day. Silent no-op when
+# the feature isn't initialized or gbrain_sync_mode is "off". See
+# docs/gbrain-sync.md.
+
+_GSTACK_HOME="${GSTACK_HOME:-$HOME/.gstack}"
+_BRAIN_REMOTE_FILE="$HOME/.gstack-brain-remote.txt"
+_BRAIN_SYNC_BIN="~/.claude/skills/gstack/bin/gstack-brain-sync"
+_BRAIN_CONFIG_BIN="~/.claude/skills/gstack/bin/gstack-config"
+
+_BRAIN_SYNC_MODE=$("$_BRAIN_CONFIG_BIN" get gbrain_sync_mode 2>/dev/null || echo off)
+
+# New-machine hint: URL file present, local .git missing, sync not yet enabled.
+if [ -f "$_BRAIN_REMOTE_FILE" ] && [ ! -d "$_GSTACK_HOME/.git" ] && [ "$_BRAIN_SYNC_MODE" = "off" ]; then
+ _BRAIN_NEW_URL=$(head -1 "$_BRAIN_REMOTE_FILE" 2>/dev/null | tr -d '[:space:]')
+ if [ -n "$_BRAIN_NEW_URL" ]; then
+ echo "BRAIN_SYNC: brain repo detected: $_BRAIN_NEW_URL"
+ echo "BRAIN_SYNC: run 'gstack-brain-restore' to pull your cross-machine memory (or 'gstack-config set gbrain_sync_mode off' to dismiss forever)"
+ fi
+fi
+
+# Active-sync path.
+if [ -d "$_GSTACK_HOME/.git" ] && [ "$_BRAIN_SYNC_MODE" != "off" ]; then
+ # Once-per-day pull.
+ _BRAIN_LAST_PULL_FILE="$_GSTACK_HOME/.brain-last-pull"
+ _BRAIN_NOW=$(date +%s)
+ _BRAIN_DO_PULL=1
+ if [ -f "$_BRAIN_LAST_PULL_FILE" ]; then
+ _BRAIN_LAST=$(cat "$_BRAIN_LAST_PULL_FILE" 2>/dev/null || echo 0)
+ _BRAIN_AGE=$(( _BRAIN_NOW - _BRAIN_LAST ))
+ [ "$_BRAIN_AGE" -lt 86400 ] && _BRAIN_DO_PULL=0
+ fi
+ if [ "$_BRAIN_DO_PULL" = "1" ]; then
+ ( cd "$_GSTACK_HOME" && git fetch origin >/dev/null 2>&1 && git merge --ff-only "origin/$(git rev-parse --abbrev-ref HEAD)" >/dev/null 2>&1 ) || true
+ echo "$_BRAIN_NOW" > "$_BRAIN_LAST_PULL_FILE"
+ fi
+ # Drain pending queue, push.
+ "$_BRAIN_SYNC_BIN" --once 2>/dev/null || true
+fi
+
+# Status line — always emitted, easy to grep.
+if [ -d "$_GSTACK_HOME/.git" ] && [ "$_BRAIN_SYNC_MODE" != "off" ]; then
+ _BRAIN_QUEUE_DEPTH=0
+ [ -f "$_GSTACK_HOME/.brain-queue.jsonl" ] && _BRAIN_QUEUE_DEPTH=$(wc -l < "$_GSTACK_HOME/.brain-queue.jsonl" | tr -d ' ')
+ _BRAIN_LAST_PUSH="never"
+ [ -f "$_GSTACK_HOME/.brain-last-push" ] && _BRAIN_LAST_PUSH=$(cat "$_GSTACK_HOME/.brain-last-push" 2>/dev/null || echo never)
+ echo "BRAIN_SYNC: mode=$_BRAIN_SYNC_MODE | last_push=$_BRAIN_LAST_PUSH | queue=$_BRAIN_QUEUE_DEPTH"
+else
+ echo "BRAIN_SYNC: off"
+fi
+```
+
+
+
+**Privacy stop-gate (fires ONCE per machine).**
+
+If the bash output shows `BRAIN_SYNC: off` AND the config value
+`gbrain_sync_mode_prompted` is `false` AND gbrain is detected on this host
+(either `gbrain doctor --fast --json` succeeds or the `gbrain` binary is in PATH),
+fire a one-time privacy gate via AskUserQuestion:
+
+> gstack can publish your session memory (learnings, plans, designs, retros) to a
+> private GitHub repo that GBrain indexes across your machines. Higher tiers
+> include behavioral data (session timelines, developer profile). How much do you
+> want to sync?
+
+Options:
+- A) Everything allowlisted (recommended — maximum cross-machine memory)
+- B) Only artifacts (plans, designs, retros, learnings) — skip timelines and profile
+- C) Decline — keep everything local
+
+After the user answers, run (substituting the chosen value):
+
+```bash
+# Chosen mode: full | artifacts-only | off
+"$_BRAIN_CONFIG_BIN" set gbrain_sync_mode <choice>
+"$_BRAIN_CONFIG_BIN" set gbrain_sync_mode_prompted true
+```
+
+If A or B was chosen AND `~/.gstack/.git` doesn't exist, ask a follow-up:
+"Set up the GBrain sync repo now? (runs `gstack-brain-init`)"
+- A) Yes, run it now
+- B) Show me the command, I'll run it myself
+
+Do not block the skill. Emit the question, continue the skill workflow. The
+next skill run picks up wherever this left off.
+
+**At skill END (before the telemetry block),** run these bash commands to
+catch artifact writes (design docs, plans, retros) that skipped the writer
+shims, plus drain any still-pending queue entries:
+
+```bash
+"~/.claude/skills/gstack/bin/gstack-brain-sync" --discover-new 2>/dev/null || true
+"~/.claude/skills/gstack/bin/gstack-brain-sync" --once 2>/dev/null || true
+```
+
+
## Model-Specific Behavioral Patch (claude)
The following nudges are tuned for the claude model family. They are
diff --git a/VERSION b/VERSION
index fdd6f7a69..dcafa494c 100644
--- a/VERSION
+++ b/VERSION
@@ -1 +1 @@
-1.6.4.0
+1.9.0.0
diff --git a/autoplan/SKILL.md b/autoplan/SKILL.md
index 387c9902d..c4ceeee9d 100644
--- a/autoplan/SKILL.md
+++ b/autoplan/SKILL.md
@@ -358,6 +358,105 @@ AI orchestrator (e.g., OpenClaw). In spawned sessions:
- Focus on completing the task and reporting results via prose output.
- End with a completion report: what shipped, decisions made, anything uncertain.
+## GBrain Sync (skill start)
+
+```bash
+# gbrain-sync: drain pending writes, pull once per day. Silent no-op when
+# the feature isn't initialized or gbrain_sync_mode is "off". See
+# docs/gbrain-sync.md.
+
+_GSTACK_HOME="${GSTACK_HOME:-$HOME/.gstack}"
+_BRAIN_REMOTE_FILE="$HOME/.gstack-brain-remote.txt"
+_BRAIN_SYNC_BIN="~/.claude/skills/gstack/bin/gstack-brain-sync"
+_BRAIN_CONFIG_BIN="~/.claude/skills/gstack/bin/gstack-config"
+
+_BRAIN_SYNC_MODE=$("$_BRAIN_CONFIG_BIN" get gbrain_sync_mode 2>/dev/null || echo off)
+
+# New-machine hint: URL file present, local .git missing, sync not yet enabled.
+if [ -f "$_BRAIN_REMOTE_FILE" ] && [ ! -d "$_GSTACK_HOME/.git" ] && [ "$_BRAIN_SYNC_MODE" = "off" ]; then
+ _BRAIN_NEW_URL=$(head -1 "$_BRAIN_REMOTE_FILE" 2>/dev/null | tr -d '[:space:]')
+ if [ -n "$_BRAIN_NEW_URL" ]; then
+ echo "BRAIN_SYNC: brain repo detected: $_BRAIN_NEW_URL"
+ echo "BRAIN_SYNC: run 'gstack-brain-restore' to pull your cross-machine memory (or 'gstack-config set gbrain_sync_mode off' to dismiss forever)"
+ fi
+fi
+
+# Active-sync path.
+if [ -d "$_GSTACK_HOME/.git" ] && [ "$_BRAIN_SYNC_MODE" != "off" ]; then
+ # Once-per-day pull.
+ _BRAIN_LAST_PULL_FILE="$_GSTACK_HOME/.brain-last-pull"
+ _BRAIN_NOW=$(date +%s)
+ _BRAIN_DO_PULL=1
+ if [ -f "$_BRAIN_LAST_PULL_FILE" ]; then
+ _BRAIN_LAST=$(cat "$_BRAIN_LAST_PULL_FILE" 2>/dev/null || echo 0)
+ _BRAIN_AGE=$(( _BRAIN_NOW - _BRAIN_LAST ))
+ [ "$_BRAIN_AGE" -lt 86400 ] && _BRAIN_DO_PULL=0
+ fi
+ if [ "$_BRAIN_DO_PULL" = "1" ]; then
+ ( cd "$_GSTACK_HOME" && git fetch origin >/dev/null 2>&1 && git merge --ff-only "origin/$(git rev-parse --abbrev-ref HEAD)" >/dev/null 2>&1 ) || true
+ echo "$_BRAIN_NOW" > "$_BRAIN_LAST_PULL_FILE"
+ fi
+ # Drain pending queue, push.
+ "$_BRAIN_SYNC_BIN" --once 2>/dev/null || true
+fi
+
+# Status line — always emitted, easy to grep.
+if [ -d "$_GSTACK_HOME/.git" ] && [ "$_BRAIN_SYNC_MODE" != "off" ]; then
+ _BRAIN_QUEUE_DEPTH=0
+ [ -f "$_GSTACK_HOME/.brain-queue.jsonl" ] && _BRAIN_QUEUE_DEPTH=$(wc -l < "$_GSTACK_HOME/.brain-queue.jsonl" | tr -d ' ')
+ _BRAIN_LAST_PUSH="never"
+ [ -f "$_GSTACK_HOME/.brain-last-push" ] && _BRAIN_LAST_PUSH=$(cat "$_GSTACK_HOME/.brain-last-push" 2>/dev/null || echo never)
+ echo "BRAIN_SYNC: mode=$_BRAIN_SYNC_MODE | last_push=$_BRAIN_LAST_PUSH | queue=$_BRAIN_QUEUE_DEPTH"
+else
+ echo "BRAIN_SYNC: off"
+fi
+```
+
+
+
+**Privacy stop-gate (fires ONCE per machine).**
+
+If the bash output shows `BRAIN_SYNC: off` AND the config value
+`gbrain_sync_mode_prompted` is `false` AND gbrain is detected on this host
+(either `gbrain doctor --fast --json` succeeds or the `gbrain` binary is in PATH),
+fire a one-time privacy gate via AskUserQuestion:
+
+> gstack can publish your session memory (learnings, plans, designs, retros) to a
+> private GitHub repo that GBrain indexes across your machines. Higher tiers
+> include behavioral data (session timelines, developer profile). How much do you
+> want to sync?
+
+Options:
+- A) Everything allowlisted (recommended — maximum cross-machine memory)
+- B) Only artifacts (plans, designs, retros, learnings) — skip timelines and profile
+- C) Decline — keep everything local
+
+After the user answers, run (substituting the chosen value):
+
+```bash
+# Chosen mode: full | artifacts-only | off
+"$_BRAIN_CONFIG_BIN" set gbrain_sync_mode <choice>
+"$_BRAIN_CONFIG_BIN" set gbrain_sync_mode_prompted true
+```
+
+If A or B was chosen AND `~/.gstack/.git` doesn't exist, ask a follow-up:
+"Set up the GBrain sync repo now? (runs `gstack-brain-init`)"
+- A) Yes, run it now
+- B) Show me the command, I'll run it myself
+
+Do not block the skill. Emit the question, continue the skill workflow. The
+next skill run picks up wherever this left off.
+
+**At skill END (before the telemetry block),** run these bash commands to
+catch artifact writes (design docs, plans, retros) that skipped the writer
+shims, plus drain any still-pending queue entries:
+
+```bash
+"~/.claude/skills/gstack/bin/gstack-brain-sync" --discover-new 2>/dev/null || true
+"~/.claude/skills/gstack/bin/gstack-brain-sync" --once 2>/dev/null || true
+```
+
+
## Model-Specific Behavioral Patch (claude)
The following nudges are tuned for the claude model family. They are
diff --git a/benchmark-models/SKILL.md b/benchmark-models/SKILL.md
index 078c5c92e..516dc4bdc 100644
--- a/benchmark-models/SKILL.md
+++ b/benchmark-models/SKILL.md
@@ -351,6 +351,105 @@ AI orchestrator (e.g., OpenClaw). In spawned sessions:
- Focus on completing the task and reporting results via prose output.
- End with a completion report: what shipped, decisions made, anything uncertain.
+## GBrain Sync (skill start)
+
+```bash
+# gbrain-sync: drain pending writes, pull once per day. Silent no-op when
+# the feature isn't initialized or gbrain_sync_mode is "off". See
+# docs/gbrain-sync.md.
+
+_GSTACK_HOME="${GSTACK_HOME:-$HOME/.gstack}"
+_BRAIN_REMOTE_FILE="$HOME/.gstack-brain-remote.txt"
+_BRAIN_SYNC_BIN="~/.claude/skills/gstack/bin/gstack-brain-sync"
+_BRAIN_CONFIG_BIN="~/.claude/skills/gstack/bin/gstack-config"
+
+_BRAIN_SYNC_MODE=$("$_BRAIN_CONFIG_BIN" get gbrain_sync_mode 2>/dev/null || echo off)
+
+# New-machine hint: URL file present, local .git missing, sync not yet enabled.
+if [ -f "$_BRAIN_REMOTE_FILE" ] && [ ! -d "$_GSTACK_HOME/.git" ] && [ "$_BRAIN_SYNC_MODE" = "off" ]; then
+ _BRAIN_NEW_URL=$(head -1 "$_BRAIN_REMOTE_FILE" 2>/dev/null | tr -d '[:space:]')
+ if [ -n "$_BRAIN_NEW_URL" ]; then
+ echo "BRAIN_SYNC: brain repo detected: $_BRAIN_NEW_URL"
+ echo "BRAIN_SYNC: run 'gstack-brain-restore' to pull your cross-machine memory (or 'gstack-config set gbrain_sync_mode off' to dismiss forever)"
+ fi
+fi
+
+# Active-sync path.
+if [ -d "$_GSTACK_HOME/.git" ] && [ "$_BRAIN_SYNC_MODE" != "off" ]; then
+ # Once-per-day pull.
+ _BRAIN_LAST_PULL_FILE="$_GSTACK_HOME/.brain-last-pull"
+ _BRAIN_NOW=$(date +%s)
+ _BRAIN_DO_PULL=1
+ if [ -f "$_BRAIN_LAST_PULL_FILE" ]; then
+ _BRAIN_LAST=$(cat "$_BRAIN_LAST_PULL_FILE" 2>/dev/null || echo 0)
+ _BRAIN_AGE=$(( _BRAIN_NOW - _BRAIN_LAST ))
+ [ "$_BRAIN_AGE" -lt 86400 ] && _BRAIN_DO_PULL=0
+ fi
+ if [ "$_BRAIN_DO_PULL" = "1" ]; then
+ ( cd "$_GSTACK_HOME" && git fetch origin >/dev/null 2>&1 && git merge --ff-only "origin/$(git rev-parse --abbrev-ref HEAD)" >/dev/null 2>&1 ) || true
+ echo "$_BRAIN_NOW" > "$_BRAIN_LAST_PULL_FILE"
+ fi
+ # Drain pending queue, push.
+ "$_BRAIN_SYNC_BIN" --once 2>/dev/null || true
+fi
+
+# Status line — always emitted, easy to grep.
+if [ -d "$_GSTACK_HOME/.git" ] && [ "$_BRAIN_SYNC_MODE" != "off" ]; then
+ _BRAIN_QUEUE_DEPTH=0
+ [ -f "$_GSTACK_HOME/.brain-queue.jsonl" ] && _BRAIN_QUEUE_DEPTH=$(wc -l < "$_GSTACK_HOME/.brain-queue.jsonl" | tr -d ' ')
+ _BRAIN_LAST_PUSH="never"
+ [ -f "$_GSTACK_HOME/.brain-last-push" ] && _BRAIN_LAST_PUSH=$(cat "$_GSTACK_HOME/.brain-last-push" 2>/dev/null || echo never)
+ echo "BRAIN_SYNC: mode=$_BRAIN_SYNC_MODE | last_push=$_BRAIN_LAST_PUSH | queue=$_BRAIN_QUEUE_DEPTH"
+else
+ echo "BRAIN_SYNC: off"
+fi
+```
+
+
+
+**Privacy stop-gate (fires ONCE per machine).**
+
+If the bash output shows `BRAIN_SYNC: off` AND the config value
+`gbrain_sync_mode_prompted` is `false` AND gbrain is detected on this host
+(either `gbrain doctor --fast --json` succeeds or the `gbrain` binary is in PATH),
+fire a one-time privacy gate via AskUserQuestion:
+
+> gstack can publish your session memory (learnings, plans, designs, retros) to a
+> private GitHub repo that GBrain indexes across your machines. Higher tiers
+> include behavioral data (session timelines, developer profile). How much do you
+> want to sync?
+
+Options:
+- A) Everything allowlisted (recommended — maximum cross-machine memory)
+- B) Only artifacts (plans, designs, retros, learnings) — skip timelines and profile
+- C) Decline — keep everything local
+
+After the user answers, run (substituting the chosen value):
+
+```bash
+# Chosen mode: full | artifacts-only | off
+"$_BRAIN_CONFIG_BIN" set gbrain_sync_mode <choice>
+"$_BRAIN_CONFIG_BIN" set gbrain_sync_mode_prompted true
+```
+
+If A or B was chosen AND `~/.gstack/.git` doesn't exist, ask a follow-up:
+"Set up the GBrain sync repo now? (runs `gstack-brain-init`)"
+- A) Yes, run it now
+- B) Show me the command, I'll run it myself
+
+Do not block the skill. Emit the question, continue the skill workflow. The
+next skill run picks up wherever this left off.
+
+**At skill END (before the telemetry block),** run these bash commands to
+catch artifact writes (design docs, plans, retros) that skipped the writer
+shims, plus drain any still-pending queue entries:
+
+```bash
+"~/.claude/skills/gstack/bin/gstack-brain-sync" --discover-new 2>/dev/null || true
+"~/.claude/skills/gstack/bin/gstack-brain-sync" --once 2>/dev/null || true
+```
+
+
## Model-Specific Behavioral Patch (claude)
The following nudges are tuned for the claude model family. They are
diff --git a/benchmark/SKILL.md b/benchmark/SKILL.md
index ae22b5090..9e7f12ccb 100644
--- a/benchmark/SKILL.md
+++ b/benchmark/SKILL.md
@@ -351,6 +351,105 @@ AI orchestrator (e.g., OpenClaw). In spawned sessions:
- Focus on completing the task and reporting results via prose output.
- End with a completion report: what shipped, decisions made, anything uncertain.
+## GBrain Sync (skill start)
+
+```bash
+# gbrain-sync: drain pending writes, pull once per day. Silent no-op when
+# the feature isn't initialized or gbrain_sync_mode is "off". See
+# docs/gbrain-sync.md.
+
+_GSTACK_HOME="${GSTACK_HOME:-$HOME/.gstack}"
+_BRAIN_REMOTE_FILE="$HOME/.gstack-brain-remote.txt"
+_BRAIN_SYNC_BIN="~/.claude/skills/gstack/bin/gstack-brain-sync"
+_BRAIN_CONFIG_BIN="~/.claude/skills/gstack/bin/gstack-config"
+
+_BRAIN_SYNC_MODE=$("$_BRAIN_CONFIG_BIN" get gbrain_sync_mode 2>/dev/null || echo off)
+
+# New-machine hint: URL file present, local .git missing, sync not yet enabled.
+if [ -f "$_BRAIN_REMOTE_FILE" ] && [ ! -d "$_GSTACK_HOME/.git" ] && [ "$_BRAIN_SYNC_MODE" = "off" ]; then
+ _BRAIN_NEW_URL=$(head -1 "$_BRAIN_REMOTE_FILE" 2>/dev/null | tr -d '[:space:]')
+ if [ -n "$_BRAIN_NEW_URL" ]; then
+ echo "BRAIN_SYNC: brain repo detected: $_BRAIN_NEW_URL"
+ echo "BRAIN_SYNC: run 'gstack-brain-restore' to pull your cross-machine memory (or 'gstack-config set gbrain_sync_mode off' to dismiss forever)"
+ fi
+fi
+
+# Active-sync path.
+if [ -d "$_GSTACK_HOME/.git" ] && [ "$_BRAIN_SYNC_MODE" != "off" ]; then
+ # Once-per-day pull.
+ _BRAIN_LAST_PULL_FILE="$_GSTACK_HOME/.brain-last-pull"
+ _BRAIN_NOW=$(date +%s)
+ _BRAIN_DO_PULL=1
+ if [ -f "$_BRAIN_LAST_PULL_FILE" ]; then
+ _BRAIN_LAST=$(cat "$_BRAIN_LAST_PULL_FILE" 2>/dev/null || echo 0)
+ _BRAIN_AGE=$(( _BRAIN_NOW - _BRAIN_LAST ))
+ [ "$_BRAIN_AGE" -lt 86400 ] && _BRAIN_DO_PULL=0
+ fi
+ if [ "$_BRAIN_DO_PULL" = "1" ]; then
+ ( cd "$_GSTACK_HOME" && git fetch origin >/dev/null 2>&1 && git merge --ff-only "origin/$(git rev-parse --abbrev-ref HEAD)" >/dev/null 2>&1 ) || true
+ echo "$_BRAIN_NOW" > "$_BRAIN_LAST_PULL_FILE"
+ fi
+ # Drain pending queue, push.
+ "$_BRAIN_SYNC_BIN" --once 2>/dev/null || true
+fi
+
+# Status line — always emitted, easy to grep.
+if [ -d "$_GSTACK_HOME/.git" ] && [ "$_BRAIN_SYNC_MODE" != "off" ]; then
+ _BRAIN_QUEUE_DEPTH=0
+ [ -f "$_GSTACK_HOME/.brain-queue.jsonl" ] && _BRAIN_QUEUE_DEPTH=$(wc -l < "$_GSTACK_HOME/.brain-queue.jsonl" | tr -d ' ')
+ _BRAIN_LAST_PUSH="never"
+ [ -f "$_GSTACK_HOME/.brain-last-push" ] && _BRAIN_LAST_PUSH=$(cat "$_GSTACK_HOME/.brain-last-push" 2>/dev/null || echo never)
+ echo "BRAIN_SYNC: mode=$_BRAIN_SYNC_MODE | last_push=$_BRAIN_LAST_PUSH | queue=$_BRAIN_QUEUE_DEPTH"
+else
+ echo "BRAIN_SYNC: off"
+fi
+```
+
+
+
+**Privacy stop-gate (fires ONCE per machine).**
+
+If the bash output shows `BRAIN_SYNC: off` AND the config value
+`gbrain_sync_mode_prompted` is `false` AND gbrain is detected on this host
+(either `gbrain doctor --fast --json` succeeds or the `gbrain` binary is in PATH),
+fire a one-time privacy gate via AskUserQuestion:
+
+> gstack can publish your session memory (learnings, plans, designs, retros) to a
+> private GitHub repo that GBrain indexes across your machines. Higher tiers
+> include behavioral data (session timelines, developer profile). How much do you
+> want to sync?
+
+Options:
+- A) Everything allowlisted (recommended — maximum cross-machine memory)
+- B) Only artifacts (plans, designs, retros, learnings) — skip timelines and profile
+- C) Decline — keep everything local
+
+After the user answers, run (substituting the chosen value):
+
+```bash
+# Chosen mode: full | artifacts-only | off
+"$_BRAIN_CONFIG_BIN" set gbrain_sync_mode <choice>
+"$_BRAIN_CONFIG_BIN" set gbrain_sync_mode_prompted true
+```
+
+If A or B was chosen AND `~/.gstack/.git` doesn't exist, ask a follow-up:
+"Set up the GBrain sync repo now? (runs `gstack-brain-init`)"
+- A) Yes, run it now
+- B) Show me the command, I'll run it myself
+
+Do not block the skill. Emit the question, continue the skill workflow. The
+next skill run picks up wherever this left off.
+
+**At skill END (before the telemetry block),** run these bash commands to
+catch artifact writes (design docs, plans, retros) that skipped the writer
+shims, plus drain any still-pending queue entries:
+
+```bash
+"~/.claude/skills/gstack/bin/gstack-brain-sync" --discover-new 2>/dev/null || true
+"~/.claude/skills/gstack/bin/gstack-brain-sync" --once 2>/dev/null || true
+```
+
+
## Model-Specific Behavioral Patch (claude)
The following nudges are tuned for the claude model family. They are
diff --git a/bin/gstack-brain-consumer b/bin/gstack-brain-consumer
new file mode 100755
index 000000000..cf92ea3ed
--- /dev/null
+++ b/bin/gstack-brain-consumer
@@ -0,0 +1,196 @@
+#!/usr/bin/env bash
+# gstack-brain-consumer — manage the consumer (reader) registry.
+#
+# Consumer = a reader that ingests the gstack-brain git repo as a source of
+# session memory. v1 primary consumer is GBrain; later versions can register
+# Codex, OpenClaw, or third-party readers.
+#
+# NOTE ON NAMING: internally this helper uses "consumer" (correct data-model
+# term). User-facing copy and the alias `gstack-brain-reader` use "reader"
+# (matches user mental model: "what's reading my brain?").
+#
+# Usage:
+# gstack-brain-consumer add <name> --ingest-url <url> --token <token>
+# gstack-brain-consumer list
+# gstack-brain-consumer remove <name>
+# gstack-brain-consumer test <name>
+#
+# Env:
+# GSTACK_HOME — override ~/.gstack
+
+set -euo pipefail
+
+GSTACK_HOME="${GSTACK_HOME:-$HOME/.gstack}"
+CONSUMERS_FILE="$GSTACK_HOME/consumers.json"
+SCRIPT_DIR="$(cd "$(dirname "$0")" && pwd)"
+CONFIG_BIN="$SCRIPT_DIR/gstack-config"
+
+ensure_file() {
+ mkdir -p "$GSTACK_HOME"
+ if [ ! -f "$CONSUMERS_FILE" ]; then
+ echo '{"consumers": []}' > "$CONSUMERS_FILE"
+ fi
+}
+
+get_remote_url() {
+ git -C "$GSTACK_HOME" remote get-url origin 2>/dev/null || echo ""
+}
+
+sub_add() {
+ local name="" url="" token=""
+ local positional=""
+ while [ $# -gt 0 ]; do
+ case "$1" in
+ --ingest-url) url="$2"; shift 2 ;;
+ --token) token="$2"; shift 2 ;;
+ --) shift; break ;;
+ -*) echo "Unknown flag: $1" >&2; exit 1 ;;
+ *) positional="$1"; shift ;;
+ esac
+ done
+ name="$positional"
+ if [ -z "$name" ] || [ -z "$url" ]; then
+ echo "Usage: gstack-brain-consumer add <name> --ingest-url <url> [--token <token>]" >&2
+ exit 1
+ fi
+ ensure_file
+ # Upsert in consumers.json, store token in gstack-config under `<name>_token`.
+ python3 - "$CONSUMERS_FILE" "$name" "$url" <<'PYEOF'
+import sys, json
+path, name, url = sys.argv[1:4]
+try:
+ with open(path) as f:
+ data = json.load(f)
+except Exception:
+ data = {"consumers": []}
+entry = {"name": name, "ingest_url": url, "status": "unknown", "token_ref": f"{name}_token"}
+cs = data.setdefault("consumers", [])
+for i, c in enumerate(cs):
+ if c.get("name") == name:
+ cs[i] = entry
+ break
+else:
+ cs.append(entry)
+with open(path, "w") as f:
+ json.dump(data, f, indent=2)
+ f.write("\n")
+print(f"registered consumer: {name}")
+PYEOF
+ if [ -n "$token" ]; then
+ "$CONFIG_BIN" set "${name}_token" "$token"
+ echo "token stored: gstack-config get ${name}_token to retrieve"
+ fi
+ # Attempt registration with remote (HTTP POST).
+ sub_test "$name"
+}
+
+sub_list() {
+ if [ ! -f "$CONSUMERS_FILE" ]; then
+ echo '{"consumers": []}'
+ return 0
+ fi
+ cat "$CONSUMERS_FILE"
+}
+
+sub_remove() {
+ local name="${1:-}"
+ if [ -z "$name" ]; then
+ echo "Usage: gstack-brain-consumer remove <name>" >&2
+ exit 1
+ fi
+ ensure_file
+ python3 - "$CONSUMERS_FILE" "$name" <<'PYEOF'
+import sys, json
+path, name = sys.argv[1:3]
+try:
+ with open(path) as f:
+ data = json.load(f)
+except Exception:
+ data = {"consumers": []}
+before = len(data.get("consumers", []))
+data["consumers"] = [c for c in data.get("consumers", []) if c.get("name") != name]
+after = len(data["consumers"])
+with open(path, "w") as f:
+ json.dump(data, f, indent=2)
+ f.write("\n")
+print(f"removed: {before - after} entry(ies)")
+PYEOF
+}
+
+sub_test() {
+ local name="${1:-}"
+ if [ -z "$name" ]; then
+ echo "Usage: gstack-brain-consumer test <name>" >&2
+ exit 1
+ fi
+ ensure_file
+ # Look up the consumer by name.
+ local info
+ info=$(python3 - "$CONSUMERS_FILE" "$name" <<'PYEOF'
+import sys, json
+path, name = sys.argv[1:3]
+try:
+ with open(path) as f:
+ data = json.load(f)
+except Exception:
+ data = {"consumers": []}
+for c in data.get("consumers", []):
+ if c.get("name") == name:
+ print(c.get("ingest_url", ""))
+ sys.exit(0)
+sys.exit(1)
+PYEOF
+ ) || { echo "No such consumer: $name" >&2; exit 1; }
+
+ local url="$info"
+ local token
+ token=$("$CONFIG_BIN" get "${name}_token" 2>/dev/null || echo "")
+ if [ -z "$url" ] || [ -z "$token" ]; then
+ echo "consumer '$name': url or token missing; cannot test"
+ return 0
+ fi
+ local repo_url
+ repo_url=$(get_remote_url)
+ echo "Testing $name at ${url%/}/ingest-repo ..."
+ local resp
+ resp=$(curl -sS -X POST "${url%/}/ingest-repo" \
+ -H "Authorization: Bearer $token" \
+ -H "Content-Type: application/json" \
+ --data "{\"repo_url\":\"$repo_url\"}" \
+ -w "\n%{http_code}" 2>&1 || echo -e "\ncurl-error")
+ local code
+ code=$(echo "$resp" | tail -1)
+ if [ "$code" = "200" ] || [ "$code" = "201" ] || [ "$code" = "204" ]; then
+ echo "ok (HTTP $code)"
+ # Update status in consumers.json.
+ python3 - "$CONSUMERS_FILE" "$name" "ok" <<'PYEOF'
+import sys, json
+path, name, status = sys.argv[1:4]
+with open(path) as f: data = json.load(f)
+for c in data.get("consumers", []):
+ if c.get("name") == name:
+ c["status"] = status
+with open(path, "w") as f: json.dump(data, f, indent=2); f.write("\n")
+PYEOF
+ else
+ echo "failed (HTTP $code)"
+ python3 - "$CONSUMERS_FILE" "$name" "error" <<'PYEOF'
+import sys, json
+path, name, status = sys.argv[1:4]
+with open(path) as f: data = json.load(f)
+for c in data.get("consumers", []):
+ if c.get("name") == name:
+ c["status"] = status
+with open(path, "w") as f: json.dump(data, f, indent=2); f.write("\n")
+PYEOF
+ fi
+}
+
+case "${1:-}" in
+ add) shift; sub_add "$@" ;;
+ list) sub_list ;;
+ remove) shift; sub_remove "$@" ;;
+ test) shift; sub_test "$@" ;;
+ --help|-h|"") sed -n '2,20p' "$0" | sed 's/^# \{0,1\}//' ;;
+ *) echo "Unknown subcommand: $1" >&2; exit 1 ;;
+esac
diff --git a/bin/gstack-brain-enqueue b/bin/gstack-brain-enqueue
new file mode 100755
index 000000000..e37799d23
--- /dev/null
+++ b/bin/gstack-brain-enqueue
@@ -0,0 +1,55 @@
+#!/usr/bin/env bash
+# gstack-brain-enqueue — atomically append a path to the GBrain sync queue.
+#
+# Usage:
+# gstack-brain-enqueue <file-path>
+#
+# Called by writer scripts (gstack-learnings-log, gstack-timeline-log, etc.)
+# after their local write. Fire-and-forget; failures are silent (never blocks
+# the writer). Queue is drained by `gstack-brain-sync --once` invoked from the
+# preamble at skill START and END boundaries.
+#
+# No-op when:
+# - gbrain_sync_mode is off (the default)
+# - ~/.gstack/.git doesn't exist (feature not initialized)
+# - <file-path> matches a line in ~/.gstack/.brain-skip.txt
+#
+# Env:
+# GSTACK_HOME — override ~/.gstack state directory (aligns with writers).
+# Tests use GSTACK_HOME=/tmp/test-$$ for isolation.
+#
+# Concurrency: POSIX append is atomic up to PIPE_BUF (~4KB Linux, 512 BSD).
+# Queue lines are ~200 bytes, safe under concurrent callers.
+
+# No `-e` — writer shims rely on this never failing loudly.
+set -uo pipefail
+
+FILE="${1:-}"
+[ -z "$FILE" ] && exit 0
+
+GSTACK_HOME="${GSTACK_HOME:-$HOME/.gstack}"
+QUEUE="$GSTACK_HOME/.brain-queue.jsonl"
+SKIP_FILE="$GSTACK_HOME/.brain-skip.txt"
+
+# Fast exits: no git repo, no sync.
+[ ! -d "$GSTACK_HOME/.git" ] && exit 0
+
+# Check sync mode. off → silent no-op.
+SCRIPT_DIR="$(cd "$(dirname "$0")" 2>/dev/null && pwd)"
+MODE=$("$SCRIPT_DIR/gstack-config" get gbrain_sync_mode 2>/dev/null || echo off)
+[ "$MODE" = "off" ] && exit 0
+
+# User-maintained skip list (for secret-scan false positives).
+if [ -f "$SKIP_FILE" ]; then
+ if grep -Fxq "$FILE" "$SKIP_FILE" 2>/dev/null; then
+ exit 0
+ fi
+fi
+
+# JSON-escape the file path (backslash + quotes only; paths shouldn't have other specials).
+ESC_FILE=$(printf '%s' "$FILE" | sed 's/\\/\\\\/g; s/"/\\"/g')
+TS=$(date -u +%Y-%m-%dT%H:%M:%SZ 2>/dev/null || echo "")
+
+printf '{"file":"%s","ts":"%s"}\n' "$ESC_FILE" "$TS" >> "$QUEUE" 2>/dev/null
+
+exit 0
diff --git a/bin/gstack-brain-init b/bin/gstack-brain-init
new file mode 100755
index 000000000..6399c12c6
--- /dev/null
+++ b/bin/gstack-brain-init
@@ -0,0 +1,360 @@
+#!/usr/bin/env bash
+# gstack-brain-init — set up ~/.gstack/ as a git repo that syncs to GBrain.
+#
+# Usage:
+# gstack-brain-init [--remote <url>]
+#
+# Interactive by default. Pass --remote to skip the remote prompt.
+#
+# Idempotent: safe to re-run. If ~/.gstack/.git already exists AND points at
+# the same remote, reconfigures drivers/hooks/attributes without clobbering
+# history. If it points at a DIFFERENT remote, refuses and suggests
+# `gstack-brain-uninstall` first.
+#
+# What it does:
+# 1. git init ~/.gstack/ (or verify existing repo points at the right remote)
+# 2. Write .gitignore = "*" (ignore everything; allowlist is explicit)
+# 3. Write .brain-allowlist (canonical paths to sync)
+# 4. Write .brain-privacy-map.json (paths → privacy class)
+# 5. Write .gitattributes (register JSONL + union merge drivers)
+# 6. git config merge.jsonl-append.driver + merge.union.driver
+# 7. Install .git/hooks/pre-commit (defense-in-depth secret scan)
+# 8. Prompt for remote (default: gh repo create --private gstack-brain-$USER)
+# 9. Initial commit + push
+# 10. Write ~/.gstack-brain-remote.txt (URL-only, safe to share)
+# 11. Register GBrain consumer (HTTP POST if GBRAIN_URL set; else defer)
+#
+# Env:
+# GSTACK_HOME — override ~/.gstack
+# GBRAIN_URL — GBrain ingest endpoint base URL (for consumer registration)
+
+set -euo pipefail
+
+GSTACK_HOME="${GSTACK_HOME:-$HOME/.gstack}"
+SCRIPT_DIR="$(cd "$(dirname "$0")" && pwd)"
+CONFIG_BIN="$SCRIPT_DIR/gstack-config"
+REMOTE_FILE="$HOME/.gstack-brain-remote.txt"
+CONSUMERS_FILE="$GSTACK_HOME/consumers.json"
+
+REMOTE_URL=""
+while [ $# -gt 0 ]; do
+ case "$1" in
+ --remote) REMOTE_URL="$2"; shift 2 ;;
+ --help|-h) sed -n '2,32p' "$0" | sed 's/^# \{0,1\}//'; exit 0 ;;
+ *) echo "Unknown flag: $1" >&2; exit 1 ;;
+ esac
+done
+
+# ---- preconditions ----
+mkdir -p "$GSTACK_HOME"
+
+EXISTING_REMOTE=""
+if [ -d "$GSTACK_HOME/.git" ]; then
+ EXISTING_REMOTE=$(git -C "$GSTACK_HOME" remote get-url origin 2>/dev/null || echo "")
+ if [ -n "$EXISTING_REMOTE" ] && [ -n "$REMOTE_URL" ] && [ "$EXISTING_REMOTE" != "$REMOTE_URL" ]; then
+ cat >&2 <<EOF
+gstack-brain-init: ~/.gstack/ is already a git repo pointing at:
+ $EXISTING_REMOTE
+
+You asked to init with:
+ $REMOTE_URL
+
+Refusing to overwrite. To switch remotes, first run:
+ gstack-brain-uninstall
+
+(or edit the remote manually with: git -C ~/.gstack remote set-url origin <url>)
+EOF
+ exit 1
+ fi
+fi
+
+# ---- choose the remote ----
+if [ -z "$REMOTE_URL" ] && [ -n "$EXISTING_REMOTE" ]; then
+ REMOTE_URL="$EXISTING_REMOTE"
+ echo "Using existing remote: $REMOTE_URL"
+fi
+
+if [ -z "$REMOTE_URL" ]; then
+ # Interactive prompt. Default: gh repo create (if available).
+ echo "gstack-brain-init will create a private git repo that holds your"
+ echo "gstack session memory across machines and lets GBrain index it."
+ echo
+ if command -v gh >/dev/null 2>&1 && gh auth status >/dev/null 2>&1; then
+ DEFAULT_NAME="gstack-brain-${USER:-$(whoami)}"
+ echo "Default: gh will create a private repo named '$DEFAULT_NAME' under your account."
+ printf "Press Enter to accept, or paste a custom git URL: "
+ read -r REPLY || REPLY=""
+ if [ -z "$REPLY" ]; then
+ echo "Creating GitHub repo: $DEFAULT_NAME ..."
+ if ! gh repo create "$DEFAULT_NAME" --private --description "gstack session memory" --source "$GSTACK_HOME" 2>/dev/null; then
+ # Maybe the repo already exists; try to fetch its URL.
+ REMOTE_URL=$(gh repo view "$DEFAULT_NAME" --json sshUrl -q .sshUrl 2>/dev/null || echo "")
+ if [ -z "$REMOTE_URL" ]; then
+ echo "Failed to create or find '$DEFAULT_NAME'. Try --remote <url>." >&2
+ exit 1
+ fi
+ echo "Repo already exists; using $REMOTE_URL"
+ else
+ REMOTE_URL=$(gh repo view "$DEFAULT_NAME" --json sshUrl -q .sshUrl 2>/dev/null || echo "")
+ fi
+ else
+ REMOTE_URL="$REPLY"
+ fi
+ else
+ echo "(gh CLI not found or not authenticated; provide a git URL directly)"
+ printf "Paste a private git URL (e.g. [email protected]:you/gstack-brain.git): "
+ read -r REMOTE_URL || REMOTE_URL=""
+ if [ -z "$REMOTE_URL" ]; then
+ echo "No URL provided. Aborting." >&2
+ exit 1
+ fi
+ fi
+fi
+
+# ---- verify remote reachable ----
+echo "Verifying remote connectivity: $REMOTE_URL"
+if ! git ls-remote "$REMOTE_URL" >/dev/null 2>&1; then
+ cat >&2 <<EOF
+Remote not reachable: $REMOTE_URL
+This could mean:
+ - Wrong URL
+ - Not authenticated (GitHub: gh auth status; GitLab: glab auth status)
+ - Network issue
+Fix and re-run gstack-brain-init.
+EOF
+ exit 1
+fi
+
+# ---- git init ----
+if [ ! -d "$GSTACK_HOME/.git" ]; then
+ git -C "$GSTACK_HOME" init -q -b main 2>/dev/null || git -C "$GSTACK_HOME" init -q
+ # If -b main wasn't supported, rename.
+ git -C "$GSTACK_HOME" branch -M main 2>/dev/null || true
+fi
+
+if [ -z "$(git -C "$GSTACK_HOME" remote 2>/dev/null)" ]; then
+ git -C "$GSTACK_HOME" remote add origin "$REMOTE_URL"
+else
+ git -C "$GSTACK_HOME" remote set-url origin "$REMOTE_URL"
+fi
+
+# ---- write canonical files (idempotent) ----
+cat > "$GSTACK_HOME/.gitignore" <<'EOF'
+# gstack-brain sync: ignore-everything base. Paths are included explicitly via
+# .brain-allowlist and `git add -f` from gstack-brain-sync. Do not edit.
+*
+EOF
+
+cat > "$GSTACK_HOME/.brain-allowlist" <<'EOF'
+# Canonical allowlist of paths that gstack-brain-sync will publish.
+# One glob per line. Anything not matching stays local.
+# Do not edit directly; managed by gstack-brain-init. User additions go below
+# the marker and survive re-init.
+projects/*/learnings.jsonl
+projects/*/*-reviews.jsonl
+projects/*/ceo-plans/*.md
+projects/*/ceo-plans/*/*.md
+projects/*/designs/*.md
+projects/*/designs/*/*.md
+projects/*/timeline.jsonl
+retros/*.md
+developer-profile.json
+builder-journey.md
+builder-profile.jsonl
+# NOT synced (per Codex v2 review — machine-local UX state):
+# projects/*/question-preferences.json (per-machine UX preferences)
+# projects/*/question-log.jsonl (audit/derivation log stays with preferences)
+# projects/*/question-events.jsonl (same)
+# ---- USER ADDITIONS BELOW ---- (survives re-init; above is managed)
+EOF
+
+cat > "$GSTACK_HOME/.brain-privacy-map.json" <<'EOF'
+[
+ {"pattern": "projects/*/learnings.jsonl", "class": "artifact"},
+ {"pattern": "projects/*/*-reviews.jsonl", "class": "artifact"},
+ {"pattern": "projects/*/ceo-plans/*.md", "class": "artifact"},
+ {"pattern": "projects/*/ceo-plans/*/*.md", "class": "artifact"},
+ {"pattern": "projects/*/designs/*.md", "class": "artifact"},
+ {"pattern": "projects/*/designs/*/*.md", "class": "artifact"},
+ {"pattern": "retros/*.md", "class": "artifact"},
+ {"pattern": "builder-journey.md", "class": "artifact"},
+ {"pattern": "projects/*/timeline.jsonl", "class": "behavioral"},
+ {"pattern": "developer-profile.json", "class": "behavioral"},
+ {"pattern": "builder-profile.jsonl", "class": "behavioral"}
+]
+EOF
+
+cat > "$GSTACK_HOME/.gitattributes" <<'EOF'
+# gstack-brain: merge drivers for cross-machine sync conflicts.
+# Matching driver must be registered in local git config; gstack-brain-init
+# and gstack-brain-restore run `git config merge.<name>.driver ...` after init.
+*.jsonl merge=jsonl-append
+retros/*.md merge=union
+projects/*/designs/**/*.md merge=union
+projects/*/ceo-plans/**/*.md merge=union
+EOF
+
+# ---- register merge drivers in local git config ----
+git -C "$GSTACK_HOME" config merge.jsonl-append.driver "$SCRIPT_DIR/gstack-jsonl-merge %O %A %B"
+git -C "$GSTACK_HOME" config merge.jsonl-append.name "gstack JSONL append-only merger"
+git -C "$GSTACK_HOME" config merge.union.driver "cat %A %B > %A.merged && mv %A.merged %A"
+git -C "$GSTACK_HOME" config merge.union.name "union concat"
+
+# ---- install pre-commit hook (defense-in-depth) ----
+HOOK="$GSTACK_HOME/.git/hooks/pre-commit"
+mkdir -p "$(dirname "$HOOK")"
+cat > "$HOOK" <<'HOOK_EOF'
+#!/usr/bin/env bash
+# gstack-brain pre-commit hook — secret-scan defense-in-depth.
+# The primary scanner runs inside gstack-brain-sync BEFORE staging. This hook
+# catches any manual `git commit` a user might accidentally run against the
+# brain repo.
+set -uo pipefail
+
+python3 -c "
+import sys, re, subprocess
+try:
+ out = subprocess.check_output(['git', 'diff', '--cached'], stderr=subprocess.DEVNULL).decode('utf-8', 'replace')
+except Exception:
+ sys.exit(0)
+
+patterns = [
+ ('aws-access-key', re.compile(r'AKIA[0-9A-Z]{16}')),
+ ('github-token', re.compile(r'\b(gh[pousr]_[A-Za-z0-9]{20,}|github_pat_[A-Za-z0-9_]{20,})')),
+ ('openai-key', re.compile(r'\bsk-[A-Za-z0-9_-]{20,}')),
+ ('pem-block', re.compile(r'-----BEGIN [A-Z ]{3,}-----')),
+ ('jwt', re.compile(r'\beyJ[A-Za-z0-9_-]{10,}\.[A-Za-z0-9_-]{10,}\.[A-Za-z0-9_-]{10,}\b')),
+ ('bearer-token-json',
+ re.compile(r'\"(authorization|api[_-]?key|apikey|token|secret|password)\"\s*:\s*\"[A-Za-z0-9_./+=-]{16,}\"',
+ re.IGNORECASE)),
+]
+for name, rx in patterns:
+ if rx.search(out):
+ sys.stderr.write(f'gstack-brain pre-commit: refusing commit — {name} detected in staged diff.\n')
+ sys.stderr.write('Either edit the offending file, or if intentional, run:\n')
+ sys.stderr.write(' gstack-brain-sync --skip-file <path> (to permanently exclude)\n')
+ sys.exit(1)
+sys.exit(0)
+"
+HOOK_EOF
+chmod +x "$HOOK"
+
+# ---- initial commit (idempotent; skips if already committed) ----
+cd "$GSTACK_HOME"
+git add -f .gitignore .brain-allowlist .brain-privacy-map.json .gitattributes
+# Only commit if the index has changes from HEAD (if there is a HEAD).
+if git rev-parse HEAD >/dev/null 2>&1; then
+ if ! git diff --cached --quiet 2>/dev/null; then
+ git -c user.email="gstack@localhost" -c user.name="gstack-brain-init" \
+ commit -q -m "chore: gstack-brain-init (refresh sync config)"
+ fi
+else
+ # First commit ever.
+ git -c user.email="gstack@localhost" -c user.name="gstack-brain-init" \
+ commit -q -m "chore: gstack-brain-init"
+fi
+
+# ---- initial push ----
+if ! git push -q -u origin main 2>/dev/null; then
+ # Maybe the default branch is master, or the remote has existing content.
+ # Try to resolve: fetch + fast-forward merge + push.
+ CURRENT_BRANCH=$(git rev-parse --abbrev-ref HEAD)
+ if git fetch origin 2>/dev/null && git pull --ff-only origin "$CURRENT_BRANCH" 2>/dev/null; then
+ git push -q -u origin "$CURRENT_BRANCH" || {
+ echo "Push to $REMOTE_URL failed. The remote may have divergent content." >&2
+ echo "Try: cd ~/.gstack && git pull --rebase origin $CURRENT_BRANCH && git push origin $CURRENT_BRANCH" >&2
+ exit 1
+ }
+ else
+ # Couldn't fetch/merge; print what to do.
+ echo "Push to $REMOTE_URL failed and fetch/merge didn't help." >&2
+ echo "Manual recovery: cd ~/.gstack && git status, then push once conflicts are resolved." >&2
+ exit 1
+ fi
+fi
+
+# ---- write the remote-url helper file (outside ~/.gstack/, survives restore) ----
+echo "$REMOTE_URL" > "$REMOTE_FILE"
+chmod 600 "$REMOTE_FILE"
+
+# ---- register GBrain consumer ----
+mkdir -p "$GSTACK_HOME"
+CONSUMER_STATUS="pending"
+GBRAIN_URL_VAL="${GBRAIN_URL:-$("$CONFIG_BIN" get gbrain_url 2>/dev/null || echo "")}"
+GBRAIN_TOKEN_VAL="${GBRAIN_TOKEN:-$("$CONFIG_BIN" get gbrain_token 2>/dev/null || echo "")}"
+
+if [ -n "$GBRAIN_URL_VAL" ] && [ -n "$GBRAIN_TOKEN_VAL" ]; then
+ # Try the HTTP handoff.
+ HTTP_RESP=$(curl -sS -X POST "${GBRAIN_URL_VAL%/}/ingest-repo" \
+ -H "Authorization: Bearer $GBRAIN_TOKEN_VAL" \
+ -H "Content-Type: application/json" \
+ --data "{\"repo_url\":\"$REMOTE_URL\"}" \
+ -w "\n%{http_code}" 2>&1 || echo -e "\ncurl-error")
+ HTTP_CODE=$(echo "$HTTP_RESP" | tail -1)
+ if [ "$HTTP_CODE" = "200" ] || [ "$HTTP_CODE" = "201" ] || [ "$HTTP_CODE" = "204" ]; then
+ CONSUMER_STATUS="ok"
+ echo "GBrain consumer registered: $GBRAIN_URL_VAL"
+ else
+ echo "GBrain ingest endpoint returned HTTP $HTTP_CODE; will retry on next skill run."
+ fi
+elif [ -z "$GBRAIN_URL_VAL" ]; then
+ echo "(GBRAIN_URL not configured; skipping consumer registration. Set it with:"
+ echo " gstack-config set gbrain_url <url>"
+ echo " gstack-config set gbrain_token <token>"
+ echo " then run: gstack-brain-consumer add gbrain --ingest-url <url> --token <token>)"
+fi
+
+# Write consumers.json — the canonical registry. Tokens are NOT stored here;
+# they stay in gstack-config (machine-local). This file IS synced so a new
+# machine knows which consumers exist and can prompt for tokens.
+python3 - "$CONSUMERS_FILE" "$GBRAIN_URL_VAL" "$CONSUMER_STATUS" <<'PYEOF'
+import sys, json, os
+path, url, status = sys.argv[1:4]
+try:
+ with open(path) as f:
+ data = json.load(f)
+except (FileNotFoundError, json.JSONDecodeError):
+ data = {"consumers": []}
+# Upsert GBrain entry.
+entry = {"name": "gbrain", "ingest_url": url, "status": status, "token_ref": "gbrain_token"}
+updated = False
+for i, c in enumerate(data.get("consumers", [])):
+ if c.get("name") == "gbrain":
+ data["consumers"][i] = entry
+ updated = True
+ break
+if not updated:
+ data.setdefault("consumers", []).append(entry)
+with open(path, "w") as f:
+ json.dump(data, f, indent=2)
+ f.write("\n")
+PYEOF
+
+# Stage and commit consumers.json in the same session.
+cd "$GSTACK_HOME"
+git add -f consumers.json 2>/dev/null || true
+if ! git diff --cached --quiet 2>/dev/null; then
+ git -c user.email="gstack@localhost" -c user.name="gstack-brain-init" \
+ commit -q -m "chore: register GBrain consumer"
+ git push -q origin HEAD 2>/dev/null || true
+fi
+
+# ---- done ----
+cat <<EOF
+
+gstack-brain-init complete.
+Repo: $GSTACK_HOME (git)
+Remote: $REMOTE_URL
+Remote URL also saved at: $REMOTE_FILE
+
+Sync happens automatically at the start and end of each skill (no daemon).
+Check status anytime with:
+ gstack-brain-sync --status
+
+To activate sync, the next skill you run will ask you one question about
+privacy mode (sync everything / artifacts only / off).
+
+New machine? On the other laptop, put a copy of:
+ $REMOTE_FILE
+in that machine's home directory, then run: gstack-brain-restore
+EOF
diff --git a/bin/gstack-brain-reader b/bin/gstack-brain-reader
new file mode 120000
index 000000000..712ce87e6
--- /dev/null
+++ b/bin/gstack-brain-reader
@@ -0,0 +1 @@
+gstack-brain-consumer
\ No newline at end of file
diff --git a/bin/gstack-brain-restore b/bin/gstack-brain-restore
new file mode 100755
index 000000000..315b36f96
--- /dev/null
+++ b/bin/gstack-brain-restore
@@ -0,0 +1,235 @@
+#!/usr/bin/env bash
+# gstack-brain-restore — bootstrap a new machine from an existing brain repo.
+#
+# Usage:
+# gstack-brain-restore [<git-remote-url>]
+#
+# If no URL is given, reads from ~/.gstack-brain-remote.txt (written by
+# gstack-brain-init on the original machine). Copy that file to the new
+# machine before running this command.
+#
+# Safety gates (refuses with clear message):
+# - ~/.gstack/.git already exists with a DIFFERENT remote
+# - ~/.gstack/ contains non-allowlisted, non-gitignored user files
+# that would be clobbered by restore
+#
+# What it does:
+# 1. Clone the remote to a staging directory
+# 2. Validate the repo is gstack-brain-shaped (.brain-allowlist, .gitattributes)
+# 3. rsync-copy tracked files into ~/.gstack/ with skip-if-same-hash
+# 4. Move staging's .git into ~/.gstack/.git
+# 5. Register local git config merge drivers (they don't clone from remote)
+# 6. Rehydrate consumers.json endpoints; prompt for tokens
+#
+# Env:
+# GSTACK_HOME — override ~/.gstack
+
+set -euo pipefail
+
+GSTACK_HOME="${GSTACK_HOME:-$HOME/.gstack}"
+SCRIPT_DIR="$(cd "$(dirname "$0")" && pwd)"
+CONFIG_BIN="$SCRIPT_DIR/gstack-config"
+REMOTE_FILE="$HOME/.gstack-brain-remote.txt"
+
+REMOTE_URL="${1:-}"
+if [ -z "$REMOTE_URL" ]; then
+ if [ -f "$REMOTE_FILE" ]; then
+ REMOTE_URL=$(head -1 "$REMOTE_FILE" | tr -d '[:space:]')
+ fi
+fi
+
+if [ -z "$REMOTE_URL" ]; then
+ cat >&2 <<EOF
+gstack-brain-restore: no remote URL provided.
+
+Provide one of:
+ gstack-brain-restore <git-url>
+ or put the URL in $REMOTE_FILE (copy from the original machine)
+EOF
+ exit 1
+fi
+
+# ---- safety gates ----
+if [ -d "$GSTACK_HOME/.git" ]; then
+ EXISTING_REMOTE=$(git -C "$GSTACK_HOME" remote get-url origin 2>/dev/null || echo "")
+ if [ -n "$EXISTING_REMOTE" ] && [ "$EXISTING_REMOTE" != "$REMOTE_URL" ]; then
+ cat >&2 <<EOF
+gstack-brain-restore: ~/.gstack/.git already points at:
+ $EXISTING_REMOTE
+
+You asked to restore from:
+ $REMOTE_URL
+
+Refusing to overwrite. Run 'gstack-brain-uninstall' first or pass a matching URL.
+EOF
+ exit 1
+ fi
+fi
+
+# ---- clone to staging ----
+STAGING=$(mktemp -d "${TMPDIR:-/tmp}/gstack-brain-restore.XXXXXX")
+trap 'rm -rf "$STAGING" 2>/dev/null' EXIT
+
+echo "Cloning $REMOTE_URL to staging..."
+if ! git clone --quiet "$REMOTE_URL" "$STAGING/repo" 2>/dev/null; then
+ echo "Clone failed. Check:" >&2
+ echo " - URL is correct: $REMOTE_URL" >&2
+ echo " - Auth: gh auth status (github) / glab auth status (gitlab)" >&2
+ exit 1
+fi
+
+# ---- validate shape ----
+if [ ! -f "$STAGING/repo/.brain-allowlist" ] || [ ! -f "$STAGING/repo/.gitattributes" ]; then
+ cat >&2 <<EOF
+gstack-brain-restore: $REMOTE_URL does not look like a gstack-brain repo.
+Missing: .brain-allowlist and/or .gitattributes
+
+This command only works on repos created by gstack-brain-init.
+EOF
+ exit 1
+fi
+
+# ---- validate target ~/.gstack/ has no non-gitignored user files ----
+mkdir -p "$GSTACK_HOME"
+if [ ! -d "$GSTACK_HOME/.git" ]; then
+ # No existing git → check if we'd clobber anything allowlisted.
+ # Read the new allowlist globs and see if any existing files would collide.
+ CLOBBER_RISK=$(python3 - "$GSTACK_HOME" "$STAGING/repo/.brain-allowlist" <<'PYEOF'
+import sys, os, fnmatch
+home, allowlist_path = sys.argv[1:3]
+try:
+ with open(allowlist_path) as f:
+ globs = [l.strip() for l in f if l.strip() and not l.lstrip().startswith('#')]
+except FileNotFoundError:
+ globs = []
+risks = []
+for root, dirs, files in os.walk(home):
+ dirs[:] = [d for d in dirs if d != '.git']
+ for name in files:
+ full = os.path.join(root, name)
+ rel = os.path.relpath(full, home)
+ for g in globs:
+ if fnmatch.fnmatchcase(rel, g):
+ risks.append(rel)
+ break
+for r in risks[:5]:
+ print(r)
+if len(risks) > 5:
+ print(f"...and {len(risks) - 5} more")
+sys.exit(0 if not risks else 2)
+PYEOF
+ ) || true
+ if [ -n "$CLOBBER_RISK" ]; then
+ cat >&2 <<EOF
+gstack-brain-restore: ~/.gstack/ has existing allowlisted files that would
+be clobbered by restore:
+
+$CLOBBER_RISK
+
+Back these up first, or run this command on a machine with an empty
+~/.gstack/. If these files are from an earlier gstack session on THIS
+machine, you probably want to run gstack-brain-init instead (to create a
+new brain repo with this machine's state).
+EOF
+ exit 1
+ fi
+fi
+
+# ---- copy tracked files in ----
+echo "Copying tracked files into ~/.gstack/ ..."
+# Use git-ls-tree to get exact tracked file list (avoids staged/untracked files).
+cd "$STAGING/repo"
+git ls-tree -r --name-only HEAD | while IFS= read -r rel_path; do
+ src="$STAGING/repo/$rel_path"
+ dst="$GSTACK_HOME/$rel_path"
+ mkdir -p "$(dirname "$dst")"
+ # Skip if identical (content hash). Otherwise copy.
+ if [ -f "$dst" ] && cmp -s "$src" "$dst"; then
+ continue
+ fi
+ cp "$src" "$dst"
+done
+
+# ---- move .git into place ----
+if [ -d "$GSTACK_HOME/.git" ]; then
+ # Existing .git with matching remote — just fetch + fast-forward.
+ git -C "$GSTACK_HOME" fetch origin >/dev/null 2>&1 || true
+else
+ mv "$STAGING/repo/.git" "$GSTACK_HOME/.git"
+fi
+
+# ---- register merge drivers (local git config; don't survive clones) ----
+git -C "$GSTACK_HOME" config merge.jsonl-append.driver "$SCRIPT_DIR/gstack-jsonl-merge %O %A %B"
+git -C "$GSTACK_HOME" config merge.jsonl-append.name "gstack JSONL append-only merger"
+git -C "$GSTACK_HOME" config merge.union.driver "cat %A %B > %A.merged && mv %A.merged %A"
+git -C "$GSTACK_HOME" config merge.union.name "union concat"
+
+# ---- install pre-commit hook (same as init) ----
+HOOK="$GSTACK_HOME/.git/hooks/pre-commit"
+mkdir -p "$(dirname "$HOOK")"
+cat > "$HOOK" <<'HOOK_EOF'
+#!/usr/bin/env bash
+set -uo pipefail
+python3 -c "
+import sys, re, subprocess
+try:
+ out = subprocess.check_output(['git', 'diff', '--cached'], stderr=subprocess.DEVNULL).decode('utf-8', 'replace')
+except Exception:
+ sys.exit(0)
+patterns = [
+ ('aws-access-key', re.compile(r'AKIA[0-9A-Z]{16}')),
+ ('github-token', re.compile(r'\b(gh[pousr]_[A-Za-z0-9]{20,}|github_pat_[A-Za-z0-9_]{20,})')),
+ ('openai-key', re.compile(r'\bsk-[A-Za-z0-9_-]{20,}')),
+ ('pem-block', re.compile(r'-----BEGIN [A-Z ]{3,}-----')),
+ ('jwt', re.compile(r'\beyJ[A-Za-z0-9_-]{10,}\.[A-Za-z0-9_-]{10,}\.[A-Za-z0-9_-]{10,}\b')),
+ ('bearer-token-json',
+ re.compile(r'\"(authorization|api[_-]?key|apikey|token|secret|password)\"\s*:\s*\"[A-Za-z0-9_./+=-]{16,}\"',
+ re.IGNORECASE)),
+]
+for name, rx in patterns:
+ if rx.search(out):
+ sys.stderr.write(f'gstack-brain pre-commit: refusing commit — {name} detected.\n')
+ sys.exit(1)
+sys.exit(0)
+"
+HOOK_EOF
+chmod +x "$HOOK"
+
+# ---- rehydrate consumers, prompt for tokens ----
+if [ -f "$GSTACK_HOME/consumers.json" ]; then
+ echo ""
+ echo "Consumer registry restored. Tokens are machine-local and NOT synced."
+ echo "Run these for each consumer to re-enter tokens:"
+ python3 - "$GSTACK_HOME/consumers.json" <<'PYEOF'
+import sys, json
+try:
+ with open(sys.argv[1]) as f:
+ data = json.load(f)
+except Exception:
+ sys.exit(0)
+for c in data.get("consumers", []):
+ name = c.get("name", "")
+ token_ref = c.get("token_ref", f"{name}_token")
+ print(f" gstack-config set {token_ref} <your-token>")
+PYEOF
+fi
+
+# ---- write remote helper file if missing ----
+if [ ! -f "$REMOTE_FILE" ]; then
+ echo "$REMOTE_URL" > "$REMOTE_FILE"
+ chmod 600 "$REMOTE_FILE"
+ echo ""
+ echo "Wrote $REMOTE_FILE for future skill-run auto-detection."
+fi
+
+cat <<EOF
+
+gstack-brain-restore complete.
+Local: $GSTACK_HOME
+Remote: $REMOTE_URL
+
+Next skill run will ask about privacy mode (one-time question) and then
+sync automatically at skill boundaries.
+
+Status anytime: gstack-brain-sync --status
+EOF
diff --git a/bin/gstack-brain-sync b/bin/gstack-brain-sync
new file mode 100755
index 000000000..4adb330fa
--- /dev/null
+++ b/bin/gstack-brain-sync
@@ -0,0 +1,447 @@
+#!/usr/bin/env bash
+# gstack-brain-sync — drain queue, commit allowlisted paths, push to remote.
+#
+# Usage:
+# gstack-brain-sync --once drain queue, commit, push (default)
+# gstack-brain-sync --status print sync health as JSON
+# gstack-brain-sync --skip-file <p> add <p> to ~/.gstack/.brain-skip.txt
+# gstack-brain-sync --drop-queue --yes clear queue without committing
+# gstack-brain-sync --discover-new scan allowlist dirs, enqueue changed files
+#
+# Invoked by the preamble at skill START and END boundaries. No persistent
+# daemon. Typical run <1s when queue empty; ~200-800ms with network push.
+#
+# Singleton enforcement: flock on ~/.gstack/.brain-sync.lock. Concurrent
+# invocations queue and serialize.
+#
+# Env:
+# GSTACK_HOME — override ~/.gstack (aligns with writers).
+
+set -uo pipefail
+
+GSTACK_HOME="${GSTACK_HOME:-$HOME/.gstack}"
+QUEUE="$GSTACK_HOME/.brain-queue.jsonl"
+ALLOWLIST="$GSTACK_HOME/.brain-allowlist"
+PRIVACY_MAP="$GSTACK_HOME/.brain-privacy-map.json"
+SKIP_FILE="$GSTACK_HOME/.brain-skip.txt"
+STATUS_FILE="$GSTACK_HOME/.brain-sync-status.json"
+LAST_PUSH_FILE="$GSTACK_HOME/.brain-last-push"
+LOCK_FILE="$GSTACK_HOME/.brain-sync.lock"
+DISCOVER_CURSOR="$GSTACK_HOME/.brain-discover-cursor"
+
+SCRIPT_DIR="$(cd "$(dirname "$0")" && pwd)"
+CONFIG_BIN="$SCRIPT_DIR/gstack-config"
+
+# Remote-specific hint for auth errors (branch on origin URL).
+remote_auth_hint() {
+ local url
+ url=$(git -C "$GSTACK_HOME" remote get-url origin 2>/dev/null || echo "")
+ case "$url" in
+ *github.com*|*@github.*) echo "run: gh auth status (and gh auth refresh if needed)" ;;
+ *gitlab*) echo "run: glab auth status" ;;
+ *) echo "check 'git remote -v' and your credentials" ;;
+ esac
+}
+
+write_status() {
+ # args: status_code message [extra_json_blob]
+ local code="$1"
+ local msg="$2"
+ local extra="${3:-{\}}"
+ local ts
+ ts=$(date -u +%Y-%m-%dT%H:%M:%SZ 2>/dev/null || echo "")
+ python3 - "$STATUS_FILE" "$code" "$msg" "$ts" "$extra" <<'PYEOF' 2>/dev/null || true
+import json, sys
+path, code, msg, ts, extra = sys.argv[1:6]
+try:
+ extra_obj = json.loads(extra) if extra else {}
+except Exception:
+ extra_obj = {}
+data = {"status": code, "message": msg, "ts": ts, **extra_obj}
+with open(path, "w") as f:
+ json.dump(data, f)
+ f.write("\n")
+PYEOF
+}
+
+# Read config; return 0 if sync active, 1 otherwise.
+sync_active() {
+ if [ ! -d "$GSTACK_HOME/.git" ]; then
+ return 1
+ fi
+ local mode
+ mode=$("$CONFIG_BIN" get gbrain_sync_mode 2>/dev/null || echo off)
+ [ "$mode" = "off" ] && return 1
+ return 0
+}
+
+# Secret regex families — stdin scan. Exits 0 clean, 1 if hit.
+# Echoes the matching pattern family name on hit. Uses python3 -c (not
+# heredoc) so sys.stdin stays available for the diff content.
+secret_scan_stdin() {
+ python3 -c "
+import sys, re
+patterns = [
+ ('aws-access-key', re.compile(r'AKIA[0-9A-Z]{16}')),
+ ('github-token', re.compile(r'\\b(gh[pousr]_[A-Za-z0-9]{20,}|github_pat_[A-Za-z0-9_]{20,})')),
+ ('openai-key', re.compile(r'\\bsk-[A-Za-z0-9_-]{20,}')),
+ ('pem-block', re.compile(r'-----BEGIN [A-Z ]{3,}-----')),
+ ('jwt', re.compile(r'\\beyJ[A-Za-z0-9_-]{10,}\\.[A-Za-z0-9_-]{10,}\\.[A-Za-z0-9_-]{10,}\\b')),
+ ('bearer-token-json',
+ re.compile(r'\"(authorization|api[_-]?key|apikey|token|secret|password)\"\\s*:\\s*\"[A-Za-z0-9_./+=-]{16,}\"',
+ re.IGNORECASE)),
+]
+text = sys.stdin.read()
+for name, rx in patterns:
+ m = rx.search(text)
+ if m:
+ snippet = m.group(0)
+ if len(snippet) > 30:
+ snippet = snippet[:30] + '...'
+ print(name + ':' + snippet)
+ sys.exit(1)
+sys.exit(0)
+"
+}
+
+# Compute matched allowlisted, privacy-filtered path set from queue.
+# Output: newline-delimited relative paths that should be staged.
+compute_paths_to_stage() {
+ local mode="$1"
+ python3 - "$GSTACK_HOME" "$QUEUE" "$ALLOWLIST" "$PRIVACY_MAP" "$SKIP_FILE" "$mode" <<'PYEOF'
+import sys, json, os, fnmatch, glob
+
+gstack_home, queue, allowlist_path, privacy_path, skip_path, mode = sys.argv[1:7]
+
+def load_lines(path):
+ try:
+ with open(path) as f:
+ return [l.strip() for l in f if l.strip() and not l.lstrip().startswith("#")]
+ except FileNotFoundError:
+ return []
+
+def load_privacy_map(path):
+ try:
+ with open(path) as f:
+ data = json.load(f)
+ # Expected: [{"pattern": "glob", "class": "artifact" | "behavioral"}]
+ return data if isinstance(data, list) else []
+ except (FileNotFoundError, json.JSONDecodeError):
+ return []
+
+allowlist_globs = load_lines(allowlist_path)
+privacy_map = load_privacy_map(privacy_path)
+skip_lines = set(load_lines(skip_path))
+
+# Read queue; collect unique file paths.
+queue_paths = set()
+try:
+ with open(queue) as f:
+ for line in f:
+ line = line.strip()
+ if not line:
+ continue
+ try:
+ obj = json.loads(line)
+ p = obj.get("file")
+ if isinstance(p, str):
+ queue_paths.add(p)
+ except json.JSONDecodeError:
+ continue
+except FileNotFoundError:
+ pass
+
+def path_matches_any(path, globs):
+ for pattern in globs:
+ if fnmatch.fnmatchcase(path, pattern):
+ return True
+ return False
+
+def privacy_class(path, mapping):
+ for entry in mapping:
+ pat = entry.get("pattern")
+ if pat and fnmatch.fnmatchcase(path, pat):
+ return entry.get("class", "artifact")
+ # Default class when no pattern matches: artifact (safe default).
+ return "artifact"
+
+# mode filter: 'off' → nothing; 'artifacts-only' → only artifact class;
+# 'full' → both classes.
+def mode_allows(cls, mode):
+ if mode == "off":
+ return False
+ if mode == "artifacts-only":
+ return cls == "artifact"
+ return True # full
+
+final = []
+for p in sorted(queue_paths):
+ if p in skip_lines:
+ continue
+ # Must be under GSTACK_HOME root. Reject absolute + reject ../ escape.
+ if p.startswith("/") or ".." in p.split("/"):
+ continue
+ # Must match at least one allowlist glob.
+ if not path_matches_any(p, allowlist_globs):
+ continue
+ # Must survive privacy mode filter.
+ cls = privacy_class(p, privacy_map)
+ if not mode_allows(cls, mode):
+ continue
+ # Must exist on disk — can't stage what isn't there.
+ if not os.path.exists(os.path.join(gstack_home, p)):
+ continue
+ final.append(p)
+
+for p in final:
+ print(p)
+PYEOF
+}
+
+subcmd_once() {
+ if ! sync_active; then
+ # Silent no-op when feature not initialized / disabled.
+ exit 0
+ fi
+
+ # Singleton lock via atomic mkdir. `flock(1)` isn't on macOS by default;
+ # `mkdir` is atomic on every POSIX filesystem. If another --once is already
+ # running, skip (don't wait) — the next skill boundary will catch up.
+ local lock_dir="${LOCK_FILE}.d"
+ if ! mkdir "$lock_dir" 2>/dev/null; then
+ # Is the lock stale? Check the pidfile inside. If process is dead, clear it.
+ if [ -f "$lock_dir/pid" ]; then
+ local lock_pid
+ lock_pid=$(cat "$lock_dir/pid" 2>/dev/null || echo "")
+ if [ -n "$lock_pid" ] && ! kill -0 "$lock_pid" 2>/dev/null; then
+ # Stale lock — clear and retry once.
+ rm -rf "$lock_dir" 2>/dev/null || true
+ if ! mkdir "$lock_dir" 2>/dev/null; then
+ exit 0
+ fi
+ else
+ # Lock is held by a live process.
+ exit 0
+ fi
+ else
+ # Lock dir without pidfile — treat as held; don't touch.
+ exit 0
+ fi
+ fi
+ echo "$$" > "$lock_dir/pid" 2>/dev/null || true
+
+ local mode
+ mode=$("$CONFIG_BIN" get gbrain_sync_mode 2>/dev/null || echo off)
+
+ local paths_file
+ paths_file=$(mktemp /tmp/brain-sync-paths.XXXXXX) || { rm -rf "$lock_dir" 2>/dev/null; write_status "error" "mktemp failed"; exit 1; }
+ # Single trap covers both: lock cleanup AND tempfile cleanup.
+ trap 'rm -f "$paths_file" 2>/dev/null; rm -rf "$lock_dir" 2>/dev/null || true' EXIT INT TERM
+
+ compute_paths_to_stage "$mode" > "$paths_file"
+ if [ ! -s "$paths_file" ]; then
+ # Nothing to stage. Clear any stale queue entries and exit.
+ : > "$QUEUE"
+ write_status "idle" "no allowlisted changes in queue"
+ exit 0
+ fi
+
+ # Stage with git add -f (forces past .gitignore=*) explicit paths only.
+ while IFS= read -r p; do
+ [ -z "$p" ] && continue
+ git -C "$GSTACK_HOME" add -f -- "$p" 2>/dev/null || true
+ done < "$paths_file"
+
+ # Secret-scan staged diff.
+ local scan_out
+ scan_out=$(git -C "$GSTACK_HOME" diff --cached 2>/dev/null | secret_scan_stdin || true)
+ if [ -n "$scan_out" ]; then
+ # Hit — unstage, preserve queue, write loud status.
+ git -C "$GSTACK_HOME" reset HEAD -- . >/dev/null 2>&1 || true
+ local hint
+ hint="secret pattern detected ($scan_out). Remediation: review the staged file, then run: gstack-brain-sync --skip-file <path> OR edit the content."
+ write_status "blocked" "$hint"
+ echo "BRAIN_SYNC: blocked: $scan_out" >&2
+ exit 0
+ fi
+
+ # Commit with template message.
+ local n ts
+ n=$(wc -l < "$paths_file" | tr -d ' ')
+ ts=$(date -u +%Y-%m-%dT%H:%M:%SZ)
+ local msg="sync: $n file(s) | $ts"
+ git -C "$GSTACK_HOME" -c user.email="gstack@localhost" -c user.name="gstack-brain-sync" \
+ commit -q -m "$msg" 2>/dev/null || {
+ # Nothing to commit (e.g. all files already committed).
+ : > "$QUEUE"
+ write_status "idle" "queue drained but no new changes to commit"
+ exit 0
+ }
+
+ # Push. On reject, fetch + merge (merge driver handles JSONL) + retry once.
+ local push_err
+ push_err=$(git -C "$GSTACK_HOME" push origin HEAD 2>&1 >/dev/null) || {
+ # Check if this is an auth error first — no point retrying.
+ if echo "$push_err" | grep -qiE "auth|permission|403|401|forbidden"; then
+ local hint
+ hint=$(remote_auth_hint)
+ write_status "push_failed" "push failed: auth error. fix: $hint"
+ echo "BRAIN_SYNC: push failed: auth. fix: $hint" >&2
+ # Queue cleared because the commit exists locally; next push will send it.
+ : > "$QUEUE"
+ exit 0
+ fi
+
+ # Try a fetch-and-merge + retry.
+ if git -C "$GSTACK_HOME" fetch origin 2>/dev/null; then
+ local branch
+ branch=$(git -C "$GSTACK_HOME" rev-parse --abbrev-ref HEAD 2>/dev/null || echo main)
+ if git -C "$GSTACK_HOME" merge --no-edit "origin/$branch" >/dev/null 2>&1; then
+ if git -C "$GSTACK_HOME" push origin HEAD 2>/dev/null; then
+ : > "$QUEUE"
+ date -u +%Y-%m-%dT%H:%M:%SZ > "$LAST_PUSH_FILE"
+ write_status "ok" "pushed $n file(s) after rebase"
+ exit 0
+ fi
+ fi
+ fi
+ write_status "push_failed" "push failed: $(printf '%s' "$push_err" | head -1)"
+ : > "$QUEUE"
+ exit 0
+ }
+
+ # Success: clear queue, update last-push.
+ : > "$QUEUE"
+ date -u +%Y-%m-%dT%H:%M:%SZ > "$LAST_PUSH_FILE"
+ write_status "ok" "pushed $n file(s)"
+ exit 0
+}
+
+subcmd_status() {
+ if [ -f "$STATUS_FILE" ]; then
+ cat "$STATUS_FILE"
+ else
+ echo '{"status":"unknown","message":"no status file yet"}'
+ fi
+ # Supplemental info (not in status file).
+ local queue_depth=0
+ [ -f "$QUEUE" ] && queue_depth=$(wc -l < "$QUEUE" | tr -d ' ')
+ local last_push="never"
+ [ -f "$LAST_PUSH_FILE" ] && last_push=$(cat "$LAST_PUSH_FILE" 2>/dev/null || echo never)
+ local mode
+ mode=$("$CONFIG_BIN" get gbrain_sync_mode 2>/dev/null || echo off)
+ printf '{"queue_depth":%s,"last_push":"%s","mode":"%s"}\n' "$queue_depth" "$last_push" "$mode"
+}
+
+subcmd_skip_file() {
+ local path="${1:-}"
+ if [ -z "$path" ]; then
+ echo "Usage: gstack-brain-sync --skip-file <path>" >&2
+ exit 1
+ fi
+ mkdir -p "$GSTACK_HOME"
+ # Avoid duplicate entries.
+ if [ -f "$SKIP_FILE" ] && grep -Fxq "$path" "$SKIP_FILE"; then
+ echo "already in skip list: $path"
+ exit 0
+ fi
+ echo "$path" >> "$SKIP_FILE"
+ echo "added to skip list: $path"
+ echo "(future writers will not enqueue this path; existing queue entries ignored on next --once)"
+}
+
+subcmd_drop_queue() {
+ local force="${1:-}"
+ if [ "$force" != "--yes" ]; then
+ echo "Refusing: --drop-queue discards pending syncs. Pass --yes to confirm." >&2
+ exit 1
+ fi
+ if [ ! -f "$QUEUE" ]; then
+ echo "queue already empty"
+ exit 0
+ fi
+ local n
+ n=$(wc -l < "$QUEUE" | tr -d ' ')
+ : > "$QUEUE"
+ echo "dropped $n queue entries"
+}
+
+subcmd_discover_new() {
+ if ! sync_active; then
+ exit 0
+ fi
+ # Walk allowlist globs; enqueue any file where mtime+size differs from cursor.
+ python3 - "$GSTACK_HOME" "$ALLOWLIST" "$DISCOVER_CURSOR" "$SCRIPT_DIR/gstack-brain-enqueue" <<'PYEOF' 2>/dev/null || true
+import sys, os, json, glob, fnmatch, subprocess, hashlib
+
+gstack_home, allowlist_path, cursor_path, enqueue_bin = sys.argv[1:5]
+
+def load_lines(path):
+ try:
+ with open(path) as f:
+ return [l.strip() for l in f if l.strip() and not l.lstrip().startswith("#")]
+ except FileNotFoundError:
+ return []
+
+def load_cursor(path):
+ try:
+ with open(path) as f:
+ return json.load(f)
+ except (FileNotFoundError, json.JSONDecodeError):
+ return {}
+
+def save_cursor(path, data):
+ try:
+ with open(path, "w") as f:
+ json.dump(data, f)
+ except OSError:
+ pass
+
+allowlist = load_lines(allowlist_path)
+cursor = load_cursor(cursor_path)
+new_cursor = dict(cursor)
+
+# Walk all files under gstack_home, match against allowlist.
+for root, dirs, files in os.walk(gstack_home):
+ # Skip .git and .brain-* state files.
+ if ".git" in root.split(os.sep):
+ continue
+ for name in files:
+ full = os.path.join(root, name)
+ rel = os.path.relpath(full, gstack_home)
+ if rel.startswith(".brain-"):
+ continue
+ matched = any(fnmatch.fnmatchcase(rel, pat) for pat in allowlist)
+ if not matched:
+ continue
+ try:
+ st = os.stat(full)
+ key = f"{int(st.st_mtime)}:{st.st_size}"
+ except OSError:
+ continue
+ prev = cursor.get(rel)
+ if prev != key:
+ # Enqueue via the shim (respects sync mode + skip list).
+ subprocess.run([enqueue_bin, rel], check=False)
+ new_cursor[rel] = key
+
+save_cursor(cursor_path, new_cursor)
+PYEOF
+}
+
+# -------- dispatch --------
+case "${1:-}" in
+ --once|"") subcmd_once ;;
+ --status) subcmd_status ;;
+ --skip-file) shift; subcmd_skip_file "${1:-}" ;;
+ --drop-queue) shift; subcmd_drop_queue "${1:-}" ;;
+ --discover-new) subcmd_discover_new ;;
+ --help|-h)
+ sed -n '2,18p' "$0" | sed 's/^# \{0,1\}//'
+ ;;
+ *)
+ echo "Unknown subcommand: $1" >&2
+ echo "Run: gstack-brain-sync --help" >&2
+ exit 1
+ ;;
+esac
diff --git a/bin/gstack-brain-uninstall b/bin/gstack-brain-uninstall
new file mode 100755
index 000000000..e259f2889
--- /dev/null
+++ b/bin/gstack-brain-uninstall
@@ -0,0 +1,145 @@
+#!/usr/bin/env bash
+# gstack-brain-uninstall — clean off-ramp for gstack-brain sync.
+#
+# Usage:
+# gstack-brain-uninstall [--yes] [--delete-remote]
+#
+# Removes the git layer from ~/.gstack/ and clears sync config. Your local
+# gstack memory (learnings, timelines, etc.) is NOT touched — this is an
+# uninstall-sync command, not a delete-data command.
+#
+# Flags:
+# --yes Skip the confirmation prompt.
+# --delete-remote Also delete the GitHub repo via `gh repo delete`
+# (interactive unless --yes is also passed).
+#
+# What it removes (in ~/.gstack/):
+# .git/ — the sync repo's git data
+# .gitignore — canonical ignore-all marker
+# .gitattributes — merge driver declarations
+# .brain-allowlist — sync path list
+# .brain-privacy-map.json — sync privacy classifier
+# .brain-queue.jsonl — pending queue
+# .brain-discover-cursor — discover-new cursor
+# .brain-last-push — timestamp marker
+# .brain-skip.txt — user-maintained skip list
+# .brain-sync.lock.d/ — lock dir (if present)
+# .brain-sync-status.json — health status
+# consumers.json — consumer/reader registry
+#
+# What it clears (via gstack-config):
+# gbrain_sync_mode → off
+# gbrain_sync_mode_prompted → false (so user re-prompts on re-init)
+#
+# What it does NOT touch:
+# Project data (projects/*, retros/*, developer-profile.json, etc.)
+# Consumer tokens in gstack-config (<name>_token keys)
+# ~/.gstack-brain-remote.txt in your home directory
+# The actual remote git repo (unless --delete-remote)
+
+set -euo pipefail
+
+GSTACK_HOME="${GSTACK_HOME:-$HOME/.gstack}"
+SCRIPT_DIR="$(cd "$(dirname "$0")" && pwd)"
+CONFIG_BIN="$SCRIPT_DIR/gstack-config"
+REMOTE_FILE="$HOME/.gstack-brain-remote.txt"
+
+ASSUME_YES=0
+DELETE_REMOTE=0
+while [ $# -gt 0 ]; do
+ case "$1" in
+ --yes|-y) ASSUME_YES=1; shift ;;
+ --delete-remote) DELETE_REMOTE=1; shift ;;
+ --help|-h) sed -n '2,30p' "$0" | sed 's/^# \{0,1\}//'; exit 0 ;;
+ *) echo "Unknown flag: $1" >&2; exit 1 ;;
+ esac
+done
+
+if [ ! -d "$GSTACK_HOME/.git" ]; then
+ echo "gstack-brain-uninstall: nothing to do (~/.gstack/.git doesn't exist)."
+ exit 0
+fi
+
+REMOTE_URL=$(git -C "$GSTACK_HOME" remote get-url origin 2>/dev/null || echo "")
+
+# ---- confirmation ----
+if [ "$ASSUME_YES" != "1" ]; then
+ cat <<EOF
+This will remove gstack-brain sync from this machine:
+ - Remove ~/.gstack/.git and sync config files
+ - Clear gbrain_sync_mode in gstack-config
+ - Remote: $REMOTE_URL will be $([ "$DELETE_REMOTE" = "1" ] && echo "DELETED" || echo "kept")
+
+Local memory (learnings, plans, etc.) is NOT touched.
+
+EOF
+ printf "Proceed? [y/N] "
+ read -r reply
+ case "$reply" in
+ y|Y|yes|Yes) ;;
+ *) echo "Aborted."; exit 0 ;;
+ esac
+fi
+
+# ---- delete remote if requested ----
+if [ "$DELETE_REMOTE" = "1" ] && [ -n "$REMOTE_URL" ]; then
+ case "$REMOTE_URL" in
+ *github.com*|*@github*)
+ if command -v gh >/dev/null 2>&1; then
+ # Extract owner/repo from URL.
+ REPO_SLUG=$(echo "$REMOTE_URL" | sed -E 's#.*[:/]([^/:]+/[^/]+)(\.git)?$#\1#' | sed 's/\.git$//')
+ if [ -n "$REPO_SLUG" ]; then
+ echo "Deleting GitHub repo: $REPO_SLUG"
+ if [ "$ASSUME_YES" = "1" ]; then
+ gh repo delete "$REPO_SLUG" --yes 2>/dev/null || echo "gh repo delete failed; continuing local uninstall"
+ else
+ gh repo delete "$REPO_SLUG" 2>/dev/null || echo "gh repo delete failed; continuing local uninstall"
+ fi
+ fi
+ else
+ echo "--delete-remote requires the gh CLI. Skipping remote deletion."
+ fi
+ ;;
+ *)
+ echo "--delete-remote only supports github.com remotes. Delete manually if needed: $REMOTE_URL"
+ ;;
+ esac
+fi
+
+# ---- remove sync files ----
+echo "Removing git layer and sync config files..."
+rm -rf "$GSTACK_HOME/.git" 2>/dev/null || true
+rm -f "$GSTACK_HOME/.gitignore" 2>/dev/null || true
+rm -f "$GSTACK_HOME/.gitattributes" 2>/dev/null || true
+rm -f "$GSTACK_HOME/.brain-allowlist" 2>/dev/null || true
+rm -f "$GSTACK_HOME/.brain-privacy-map.json" 2>/dev/null || true
+rm -f "$GSTACK_HOME/.brain-queue.jsonl" 2>/dev/null || true
+rm -f "$GSTACK_HOME/.brain-discover-cursor" 2>/dev/null || true
+rm -f "$GSTACK_HOME/.brain-last-push" 2>/dev/null || true
+rm -f "$GSTACK_HOME/.brain-last-pull" 2>/dev/null || true
+rm -f "$GSTACK_HOME/.brain-skip.txt" 2>/dev/null || true
+rm -f "$GSTACK_HOME/.brain-sync-status.json" 2>/dev/null || true
+rm -rf "$GSTACK_HOME/.brain-sync.lock.d" 2>/dev/null || true
+rm -f "$GSTACK_HOME/consumers.json" 2>/dev/null || true
+
+# ---- clear config keys ----
+"$CONFIG_BIN" set gbrain_sync_mode off >/dev/null 2>&1 || true
+"$CONFIG_BIN" set gbrain_sync_mode_prompted false >/dev/null 2>&1 || true
+
+# ---- leave remote-helper file alone unless user asked to delete remote ----
+if [ "$DELETE_REMOTE" = "1" ]; then
+ rm -f "$REMOTE_FILE" 2>/dev/null || true
+else
+ if [ -f "$REMOTE_FILE" ]; then
+ echo "(keeping $REMOTE_FILE — remove manually if you want to forget the URL)"
+ fi
+fi
+
+cat <<EOF
+
+gstack-brain uninstall complete.
+Sync is off. ~/.gstack/ is a plain directory again.
+Your project data, learnings, and profile are untouched.
+
+To re-enable sync later: gstack-brain-init
+EOF
diff --git a/bin/gstack-config b/bin/gstack-config
index d715aee4b..967478b08 100755
--- a/bin/gstack-config
+++ b/bin/gstack-config
@@ -8,10 +8,11 @@
# gstack-config defaults — show just the defaults table
#
# Env overrides (for testing):
-# GSTACK_STATE_DIR — override ~/.gstack state directory
+# GSTACK_HOME — override ~/.gstack state directory (aligns with writer scripts)
+# GSTACK_STATE_DIR — legacy alias for GSTACK_HOME (kept for backwards compat)
set -euo pipefail
-STATE_DIR="${GSTACK_STATE_DIR:-$HOME/.gstack}"
+STATE_DIR="${GSTACK_HOME:-${GSTACK_STATE_DIR:-$HOME/.gstack}}"
CONFIG_FILE="$STATE_DIR/config.yaml"
# Annotated header for new config files. Written once on first `set`.
@@ -59,6 +60,19 @@ CONFIG_HEADER='# gstack configuration — edit freely, changes take effect on ne
# # Unknown values default to "default" with a warning.
# # See docs/designs/PLAN_TUNING_V1.md for rationale.
#
+# ─── GBrain sync (v1.7+) ─────────────────────────────────────────────
+# gbrain_sync_mode: off # off | artifacts-only | full
+# # off — no sync (default)
+# # artifacts-only — sync plans/designs/retros/learnings only
+# # (skip behavioral data: question-log,
+# # developer-profile, timeline)
+# # full — sync everything allowlisted
+# # Set by the first-run privacy stop-gate. See docs/gbrain-sync.md.
+#
+# gbrain_sync_mode_prompted: false
+# # Set to true once the privacy gate has asked the user.
+# # Flip back to false to be re-prompted.
+#
# ─── Advanced ────────────────────────────────────────────────────────
# codex_reviews: enabled # disabled = skip Codex adversarial reviews in /ship
# gstack_contributor: false # true = file field reports when gstack misbehaves
@@ -83,6 +97,8 @@ lookup_default() {
gstack_contributor) echo "false" ;;
skip_eng_review) echo "false" ;;
cross_project_learnings) echo "" ;; # intentionally empty → unset triggers first-time prompt
+ gbrain_sync_mode) echo "off" ;;
+ gbrain_sync_mode_prompted) echo "false" ;;
*) echo "" ;;
esac
}
@@ -114,6 +130,10 @@ case "${1:-}" in
echo "Warning: explain_level '$VALUE' not recognized. Valid values: default, terse. Using default." >&2
VALUE="default"
fi
+ if [ "$KEY" = "gbrain_sync_mode" ] && [ "$VALUE" != "off" ] && [ "$VALUE" != "artifacts-only" ] && [ "$VALUE" != "full" ]; then
+ echo "Warning: gbrain_sync_mode '$VALUE' not recognized. Valid values: off, artifacts-only, full. Using off." >&2
+ VALUE="off"
+ fi
mkdir -p "$STATE_DIR"
# Write annotated header on first creation
if [ ! -f "$CONFIG_FILE" ]; then
@@ -142,7 +162,8 @@ case "${1:-}" in
echo "# ─── Active values (including defaults for unset keys) ───"
for KEY in proactive routing_declined telemetry auto_upgrade update_check \
skill_prefix checkpoint_mode checkpoint_push codex_reviews \
- gstack_contributor skip_eng_review; do
+ gstack_contributor skip_eng_review gbrain_sync_mode \
+ gbrain_sync_mode_prompted; do
VALUE=$(grep -E "^${KEY}:" "$CONFIG_FILE" 2>/dev/null | tail -1 | awk '{print $2}' | tr -d '[:space:]' || true)
SOURCE="default"
if [ -n "$VALUE" ]; then
@@ -157,7 +178,8 @@ case "${1:-}" in
echo "# gstack-config defaults"
for KEY in proactive routing_declined telemetry auto_upgrade update_check \
skill_prefix checkpoint_mode checkpoint_push codex_reviews \
- gstack_contributor skip_eng_review; do
+ gstack_contributor skip_eng_review gbrain_sync_mode \
+ gbrain_sync_mode_prompted; do
printf ' %-24s %s\n' "$KEY:" "$(lookup_default "$KEY")"
done
;;
diff --git a/bin/gstack-developer-profile b/bin/gstack-developer-profile
index c4a3360cf..3e8ed0bd4 100755
--- a/bin/gstack-developer-profile
+++ b/bin/gstack-developer-profile
@@ -101,6 +101,10 @@ do_migrate() {
mv "$TMPOUT" "$PROFILE_FILE"
trap - EXIT
+ # gbrain-sync: enqueue the migrated file for cross-machine sync (no-op if off).
+ SCRIPT_DIR_E="$(cd "$(dirname "$0")" && pwd)"
+ "$SCRIPT_DIR_E/gstack-brain-enqueue" "developer-profile.json" 2>/dev/null &
+
# Archive the legacy file.
local TS
TS="$(date +%Y-%m-%d-%H%M%S)"
diff --git a/bin/gstack-jsonl-merge b/bin/gstack-jsonl-merge
new file mode 100755
index 000000000..2be0ea9d0
--- /dev/null
+++ b/bin/gstack-jsonl-merge
@@ -0,0 +1,88 @@
+#!/usr/bin/env bash
+# gstack-jsonl-merge — git merge driver for append-only JSONL files.
+#
+# Usage (called by git, not by users):
+# gstack-jsonl-merge <base> <ours> <theirs>
+#
+# Registered in local git config by bin/gstack-brain-init and
+# bin/gstack-brain-restore:
+# git config merge.jsonl-append.driver \
+# "$GSTACK_BIN/gstack-jsonl-merge %O %A %B"
+#
+# Behavior:
+# Concatenate base + ours + theirs, dedup exact-duplicate lines, sort by
+# ISO "ts" field when present, fall back to SHA-256 of the line for
+# deterministic order. Write result to <ours> (the %A file per the git
+# merge-driver contract).
+#
+# Two machines appending to the same JSONL file between pushes produces
+# a same-line conflict at the file tail. This driver resolves it cleanly:
+# both appends survive, ordered by wall-clock timestamp where available,
+# content hash otherwise.
+#
+# Exit codes:
+# 0 — merge succeeded, result written to <ours>
+# 1 — error; git treats as conflict and stops the merge
+
+set -uo pipefail
+
+if [ "$#" -lt 3 ]; then
+ echo "gstack-jsonl-merge: expected 3 args (base ours theirs), got $#" >&2
+ exit 1
+fi
+
+BASE="$1"
+OURS="$2"
+THEIRS="$3"
+
+TMP=$(mktemp /tmp/gstack-jsonl-merge.XXXXXX) || exit 1
+trap 'rm -f "$TMP" 2>/dev/null || true' EXIT
+
+python3 - "$BASE" "$OURS" "$THEIRS" > "$TMP" <<'PYEOF'
+import sys, json, hashlib
+
+paths = sys.argv[1:4] # base, ours, theirs
+seen = {} # line content -> sort_key
+
+for path in paths:
+ try:
+ with open(path, 'r', encoding='utf-8') as f:
+ for line in f:
+ line = line.rstrip('\n')
+ if not line:
+ continue
+ if line in seen:
+ continue
+ # Prefer ISO ts field for sort; fall back to SHA-256.
+ sort_key = None
+ try:
+ obj = json.loads(line)
+ ts = obj.get('ts') or obj.get('timestamp')
+ if isinstance(ts, str):
+ sort_key = (0, ts)
+ except (json.JSONDecodeError, ValueError, TypeError):
+ pass
+ if sort_key is None:
+ h = hashlib.sha256(line.encode('utf-8')).hexdigest()
+ sort_key = (1, h)
+ seen[line] = sort_key
+ except FileNotFoundError:
+ # Absent base / absent ours / absent theirs are all valid.
+ continue
+ except OSError:
+ # Permission / IO errors are fatal — caller sees non-zero exit.
+ sys.exit(1)
+
+# Timestamp-ordered entries first (group 0), then hash-ordered (group 1).
+for line, _ in sorted(seen.items(), key=lambda item: item[1]):
+ print(line)
+PYEOF
+
+_PYEXIT=$?
+if [ "$_PYEXIT" != "0" ]; then
+ exit 1
+fi
+
+mv "$TMP" "$OURS" || exit 1
+trap - EXIT
+exit 0
diff --git a/bin/gstack-learnings-log b/bin/gstack-learnings-log
index 6c528d3a8..5f53e1902 100755
--- a/bin/gstack-learnings-log
+++ b/bin/gstack-learnings-log
@@ -84,3 +84,6 @@ if [ $? -ne 0 ] || [ -z "$VALIDATED" ]; then
fi
echo "$VALIDATED" >> "$GSTACK_HOME/projects/$SLUG/learnings.jsonl"
+
+# gbrain-sync: enqueue for cross-machine sync (no-op if sync is off).
+"$SCRIPT_DIR/gstack-brain-enqueue" "projects/$SLUG/learnings.jsonl" 2>/dev/null &
diff --git a/bin/gstack-question-log b/bin/gstack-question-log
index 2aecb5361..4344843ef 100755
--- a/bin/gstack-question-log
+++ b/bin/gstack-question-log
@@ -165,3 +165,7 @@ if [ $VALIDATE_RC -ne 0 ] || [ -z "$VALIDATED" ]; then
fi
echo "$VALIDATED" >> "$GSTACK_HOME/projects/$SLUG/question-log.jsonl"
+
+# NOTE: question-log.jsonl is deliberately NOT enqueued for gbrain-sync.
+# Per Codex v2 review, audit/derivation data stays local alongside the
+# question-preferences.json it annotates.
diff --git a/bin/gstack-review-log b/bin/gstack-review-log
index 62c9e1719..fba2ee7d9 100755
--- a/bin/gstack-review-log
+++ b/bin/gstack-review-log
@@ -16,3 +16,6 @@ if ! printf '%s' "$INPUT" | bun -e "JSON.parse(await Bun.stdin.text())" 2>/dev/n
fi
echo "$INPUT" >> "$GSTACK_HOME/projects/$SLUG/$BRANCH-reviews.jsonl"
+
+# gbrain-sync: enqueue for cross-machine sync (no-op if sync is off).
+"$SCRIPT_DIR/gstack-brain-enqueue" "projects/$SLUG/$BRANCH-reviews.jsonl" 2>/dev/null &
diff --git a/bin/gstack-timeline-log b/bin/gstack-timeline-log
index 0167a1d00..9429b4769 100755
--- a/bin/gstack-timeline-log
+++ b/bin/gstack-timeline-log
@@ -2,7 +2,10 @@
# gstack-timeline-log — append a timeline event to the project timeline
# Usage: gstack-timeline-log '{"skill":"review","event":"started","branch":"main"}'
#
-# Session timeline: local-only, never sent anywhere.
+# Session timeline: local by default. If the user enables `gbrain_sync_mode`
+# with the `full` (not `artifacts-only`) privacy tier — via the first-run
+# stop-gate from `gstack-brain-init` or the preamble — timeline events are
+# published to the user's private GBrain sync repo. See docs/gbrain-sync.md.
# Required fields: skill, event (started|completed).
# Optional: branch, outcome, duration_s, session, ts.
# Validation failure → skip silently (non-blocking).
@@ -32,3 +35,6 @@ if ! printf '%s' "$INPUT" | bun -e "const j=JSON.parse(await Bun.stdin.text());
fi
echo "$INPUT" >> "$GSTACK_HOME/projects/$SLUG/timeline.jsonl"
+
+# gbrain-sync: enqueue for cross-machine sync (no-op if sync is off).
+"$SCRIPT_DIR/gstack-brain-enqueue" "projects/$SLUG/timeline.jsonl" 2>/dev/null &
diff --git a/browse/SKILL.md b/browse/SKILL.md
index 864644a09..64f68246d 100644
--- a/browse/SKILL.md
+++ b/browse/SKILL.md
@@ -350,6 +350,105 @@ AI orchestrator (e.g., OpenClaw). In spawned sessions:
- Focus on completing the task and reporting results via prose output.
- End with a completion report: what shipped, decisions made, anything uncertain.
+## GBrain Sync (skill start)
+
+```bash
+# gbrain-sync: drain pending writes, pull once per day. Silent no-op when
+# the feature isn't initialized or gbrain_sync_mode is "off". See
+# docs/gbrain-sync.md.
+
+_GSTACK_HOME="${GSTACK_HOME:-$HOME/.gstack}"
+_BRAIN_REMOTE_FILE="$HOME/.gstack-brain-remote.txt"
+_BRAIN_SYNC_BIN="~/.claude/skills/gstack/bin/gstack-brain-sync"
+_BRAIN_CONFIG_BIN="~/.claude/skills/gstack/bin/gstack-config"
+
+_BRAIN_SYNC_MODE=$("$_BRAIN_CONFIG_BIN" get gbrain_sync_mode 2>/dev/null || echo off)
+
+# New-machine hint: URL file present, local .git missing, sync not yet enabled.
+if [ -f "$_BRAIN_REMOTE_FILE" ] && [ ! -d "$_GSTACK_HOME/.git" ] && [ "$_BRAIN_SYNC_MODE" = "off" ]; then
+ _BRAIN_NEW_URL=$(head -1 "$_BRAIN_REMOTE_FILE" 2>/dev/null | tr -d '[:space:]')
+ if [ -n "$_BRAIN_NEW_URL" ]; then
+ echo "BRAIN_SYNC: brain repo detected: $_BRAIN_NEW_URL"
+ echo "BRAIN_SYNC: run 'gstack-brain-restore' to pull your cross-machine memory (or 'gstack-config set gbrain_sync_mode off' to dismiss forever)"
+ fi
+fi
+
+# Active-sync path.
+if [ -d "$_GSTACK_HOME/.git" ] && [ "$_BRAIN_SYNC_MODE" != "off" ]; then
+ # Once-per-day pull.
+ _BRAIN_LAST_PULL_FILE="$_GSTACK_HOME/.brain-last-pull"
+ _BRAIN_NOW=$(date +%s)
+ _BRAIN_DO_PULL=1
+ if [ -f "$_BRAIN_LAST_PULL_FILE" ]; then
+ _BRAIN_LAST=$(cat "$_BRAIN_LAST_PULL_FILE" 2>/dev/null || echo 0)
+ _BRAIN_AGE=$(( _BRAIN_NOW - _BRAIN_LAST ))
+ [ "$_BRAIN_AGE" -lt 86400 ] && _BRAIN_DO_PULL=0
+ fi
+ if [ "$_BRAIN_DO_PULL" = "1" ]; then
+ ( cd "$_GSTACK_HOME" && git fetch origin >/dev/null 2>&1 && git merge --ff-only "origin/$(git rev-parse --abbrev-ref HEAD)" >/dev/null 2>&1 ) || true
+ echo "$_BRAIN_NOW" > "$_BRAIN_LAST_PULL_FILE"
+ fi
+ # Drain pending queue, push.
+ "$_BRAIN_SYNC_BIN" --once 2>/dev/null || true
+fi
+
+# Status line — always emitted, easy to grep.
+if [ -d "$_GSTACK_HOME/.git" ] && [ "$_BRAIN_SYNC_MODE" != "off" ]; then
+ _BRAIN_QUEUE_DEPTH=0
+ [ -f "$_GSTACK_HOME/.brain-queue.jsonl" ] && _BRAIN_QUEUE_DEPTH=$(wc -l < "$_GSTACK_HOME/.brain-queue.jsonl" | tr -d ' ')
+ _BRAIN_LAST_PUSH="never"
+ [ -f "$_GSTACK_HOME/.brain-last-push" ] && _BRAIN_LAST_PUSH=$(cat "$_GSTACK_HOME/.brain-last-push" 2>/dev/null || echo never)
+ echo "BRAIN_SYNC: mode=$_BRAIN_SYNC_MODE | last_push=$_BRAIN_LAST_PUSH | queue=$_BRAIN_QUEUE_DEPTH"
+else
+ echo "BRAIN_SYNC: off"
+fi
+```
+
+
+
+**Privacy stop-gate (fires ONCE per machine).**
+
+If the bash output shows `BRAIN_SYNC: off` AND the config value
+`gbrain_sync_mode_prompted` is `false` AND gbrain is detected on this host
+(either `gbrain doctor --fast --json` succeeds or the `gbrain` binary is in PATH),
+fire a one-time privacy gate via AskUserQuestion:
+
+> gstack can publish your session memory (learnings, plans, designs, retros) to a
+> private GitHub repo that GBrain indexes across your machines. Higher tiers
+> include behavioral data (session timelines, developer profile). How much do you
+> want to sync?
+
+Options:
+- A) Everything allowlisted (recommended — maximum cross-machine memory)
+- B) Only artifacts (plans, designs, retros, learnings) — skip timelines and profile
+- C) Decline — keep everything local
+
+After the user answers, run (substituting the chosen value):
+
+```bash
+# Chosen mode: full | artifacts-only | off
+"$_BRAIN_CONFIG_BIN" set gbrain_sync_mode <choice>
+"$_BRAIN_CONFIG_BIN" set gbrain_sync_mode_prompted true
+```
+
+If A or B was chosen AND `~/.gstack/.git` doesn't exist, ask a follow-up:
+"Set up the GBrain sync repo now? (runs `gstack-brain-init`)"
+- A) Yes, run it now
+- B) Show me the command, I'll run it myself
+
+Do not block the skill. Emit the question, continue the skill workflow. The
+next skill run picks up wherever this left off.
+
+**At skill END (before the telemetry block),** run these bash commands to
+catch artifact writes (design docs, plans, retros) that skipped the writer
+shims, plus drain any still-pending queue entries:
+
+```bash
+"~/.claude/skills/gstack/bin/gstack-brain-sync" --discover-new 2>/dev/null || true
+"~/.claude/skills/gstack/bin/gstack-brain-sync" --once 2>/dev/null || true
+```
+
+
## Model-Specific Behavioral Patch (claude)
The following nudges are tuned for the claude model family. They are
diff --git a/canary/SKILL.md b/canary/SKILL.md
index 8cde8383f..d4b5d35b4 100644
--- a/canary/SKILL.md
+++ b/canary/SKILL.md
@@ -350,6 +350,105 @@ AI orchestrator (e.g., OpenClaw). In spawned sessions:
- Focus on completing the task and reporting results via prose output.
- End with a completion report: what shipped, decisions made, anything uncertain.
+## GBrain Sync (skill start)
+
+```bash
+# gbrain-sync: drain pending writes, pull once per day. Silent no-op when
+# the feature isn't initialized or gbrain_sync_mode is "off". See
+# docs/gbrain-sync.md.
+
+_GSTACK_HOME="${GSTACK_HOME:-$HOME/.gstack}"
+_BRAIN_REMOTE_FILE="$HOME/.gstack-brain-remote.txt"
+_BRAIN_SYNC_BIN="~/.claude/skills/gstack/bin/gstack-brain-sync"
+_BRAIN_CONFIG_BIN="~/.claude/skills/gstack/bin/gstack-config"
+
+_BRAIN_SYNC_MODE=$("$_BRAIN_CONFIG_BIN" get gbrain_sync_mode 2>/dev/null || echo off)
+
+# New-machine hint: URL file present, local .git missing, sync not yet enabled.
+if [ -f "$_BRAIN_REMOTE_FILE" ] && [ ! -d "$_GSTACK_HOME/.git" ] && [ "$_BRAIN_SYNC_MODE" = "off" ]; then
+ _BRAIN_NEW_URL=$(head -1 "$_BRAIN_REMOTE_FILE" 2>/dev/null | tr -d '[:space:]')
+ if [ -n "$_BRAIN_NEW_URL" ]; then
+ echo "BRAIN_SYNC: brain repo detected: $_BRAIN_NEW_URL"
+ echo "BRAIN_SYNC: run 'gstack-brain-restore' to pull your cross-machine memory (or 'gstack-config set gbrain_sync_mode off' to dismiss forever)"
+ fi
+fi
+
+# Active-sync path.
+if [ -d "$_GSTACK_HOME/.git" ] && [ "$_BRAIN_SYNC_MODE" != "off" ]; then
+ # Once-per-day pull.
+ _BRAIN_LAST_PULL_FILE="$_GSTACK_HOME/.brain-last-pull"
+ _BRAIN_NOW=$(date +%s)
+ _BRAIN_DO_PULL=1
+ if [ -f "$_BRAIN_LAST_PULL_FILE" ]; then
+ _BRAIN_LAST=$(cat "$_BRAIN_LAST_PULL_FILE" 2>/dev/null || echo 0)
+ _BRAIN_AGE=$(( _BRAIN_NOW - _BRAIN_LAST ))
+ [ "$_BRAIN_AGE" -lt 86400 ] && _BRAIN_DO_PULL=0
+ fi
+ if [ "$_BRAIN_DO_PULL" = "1" ]; then
+ ( cd "$_GSTACK_HOME" && git fetch origin >/dev/null 2>&1 && git merge --ff-only "origin/$(git rev-parse --abbrev-ref HEAD)" >/dev/null 2>&1 ) || true
+ echo "$_BRAIN_NOW" > "$_BRAIN_LAST_PULL_FILE"
+ fi
+ # Drain pending queue, push.
+ "$_BRAIN_SYNC_BIN" --once 2>/dev/null || true
+fi
+
+# Status line — always emitted, easy to grep.
+if [ -d "$_GSTACK_HOME/.git" ] && [ "$_BRAIN_SYNC_MODE" != "off" ]; then
+ _BRAIN_QUEUE_DEPTH=0
+ [ -f "$_GSTACK_HOME/.brain-queue.jsonl" ] && _BRAIN_QUEUE_DEPTH=$(wc -l < "$_GSTACK_HOME/.brain-queue.jsonl" | tr -d ' ')
+ _BRAIN_LAST_PUSH="never"
+ [ -f "$_GSTACK_HOME/.brain-last-push" ] && _BRAIN_LAST_PUSH=$(cat "$_GSTACK_HOME/.brain-last-push" 2>/dev/null || echo never)
+ echo "BRAIN_SYNC: mode=$_BRAIN_SYNC_MODE | last_push=$_BRAIN_LAST_PUSH | queue=$_BRAIN_QUEUE_DEPTH"
+else
+ echo "BRAIN_SYNC: off"
+fi
+```
+
+
+
+**Privacy stop-gate (fires ONCE per machine).**
+
+If the bash output shows `BRAIN_SYNC: off` AND the config value
+`gbrain_sync_mode_prompted` is `false` AND gbrain is detected on this host
+(either `gbrain doctor --fast --json` succeeds or the `gbrain` binary is in PATH),
+fire a one-time privacy gate via AskUserQuestion:
+
+> gstack can publish your session memory (learnings, plans, designs, retros) to a
+> private GitHub repo that GBrain indexes across your machines. Higher tiers
+> include behavioral data (session timelines, developer profile). How much do you
+> want to sync?
+
+Options:
+- A) Everything allowlisted (recommended — maximum cross-machine memory)
+- B) Only artifacts (plans, designs, retros, learnings) — skip timelines and profile
+- C) Decline — keep everything local
+
+After the user answers, run (substituting the chosen value):
+
+```bash
+# Chosen mode: full | artifacts-only | off
+"$_BRAIN_CONFIG_BIN" set gbrain_sync_mode <choice>
+"$_BRAIN_CONFIG_BIN" set gbrain_sync_mode_prompted true
+```
+
+If A or B was chosen AND `~/.gstack/.git` doesn't exist, ask a follow-up:
+"Set up the GBrain sync repo now? (runs `gstack-brain-init`)"
+- A) Yes, run it now
+- B) Show me the command, I'll run it myself
+
+Do not block the skill. Emit the question, continue the skill workflow. The
+next skill run picks up wherever this left off.
+
+**At skill END (before the telemetry block),** run these bash commands to
+catch artifact writes (design docs, plans, retros) that skipped the writer
+shims, plus drain any still-pending queue entries:
+
+```bash
+"~/.claude/skills/gstack/bin/gstack-brain-sync" --discover-new 2>/dev/null || true
+"~/.claude/skills/gstack/bin/gstack-brain-sync" --once 2>/dev/null || true
+```
+
+
## Model-Specific Behavioral Patch (claude)
The following nudges are tuned for the claude model family. They are
diff --git a/codex/SKILL.md b/codex/SKILL.md
index 8ae3cb13f..d752fd229 100644
--- a/codex/SKILL.md
+++ b/codex/SKILL.md
@@ -352,6 +352,105 @@ AI orchestrator (e.g., OpenClaw). In spawned sessions:
- Focus on completing the task and reporting results via prose output.
- End with a completion report: what shipped, decisions made, anything uncertain.
+## GBrain Sync (skill start)
+
+```bash
+# gbrain-sync: drain pending writes, pull once per day. Silent no-op when
+# the feature isn't initialized or gbrain_sync_mode is "off". See
+# docs/gbrain-sync.md.
+
+_GSTACK_HOME="${GSTACK_HOME:-$HOME/.gstack}"
+_BRAIN_REMOTE_FILE="$HOME/.gstack-brain-remote.txt"
+_BRAIN_SYNC_BIN="~/.claude/skills/gstack/bin/gstack-brain-sync"
+_BRAIN_CONFIG_BIN="~/.claude/skills/gstack/bin/gstack-config"
+
+_BRAIN_SYNC_MODE=$("$_BRAIN_CONFIG_BIN" get gbrain_sync_mode 2>/dev/null || echo off)
+
+# New-machine hint: URL file present, local .git missing, sync not yet enabled.
+if [ -f "$_BRAIN_REMOTE_FILE" ] && [ ! -d "$_GSTACK_HOME/.git" ] && [ "$_BRAIN_SYNC_MODE" = "off" ]; then
+ _BRAIN_NEW_URL=$(head -1 "$_BRAIN_REMOTE_FILE" 2>/dev/null | tr -d '[:space:]')
+ if [ -n "$_BRAIN_NEW_URL" ]; then
+ echo "BRAIN_SYNC: brain repo detected: $_BRAIN_NEW_URL"
+ echo "BRAIN_SYNC: run 'gstack-brain-restore' to pull your cross-machine memory (or 'gstack-config set gbrain_sync_mode off' to dismiss forever)"
+ fi
+fi
+
+# Active-sync path.
+if [ -d "$_GSTACK_HOME/.git" ] && [ "$_BRAIN_SYNC_MODE" != "off" ]; then
+ # Once-per-day pull.
+ _BRAIN_LAST_PULL_FILE="$_GSTACK_HOME/.brain-last-pull"
+ _BRAIN_NOW=$(date +%s)
+ _BRAIN_DO_PULL=1
+ if [ -f "$_BRAIN_LAST_PULL_FILE" ]; then
+ _BRAIN_LAST=$(cat "$_BRAIN_LAST_PULL_FILE" 2>/dev/null || echo 0)
+ _BRAIN_AGE=$(( _BRAIN_NOW - _BRAIN_LAST ))
+ [ "$_BRAIN_AGE" -lt 86400 ] && _BRAIN_DO_PULL=0
+ fi
+ if [ "$_BRAIN_DO_PULL" = "1" ]; then
+ ( cd "$_GSTACK_HOME" && git fetch origin >/dev/null 2>&1 && git merge --ff-only "origin/$(git rev-parse --abbrev-ref HEAD)" >/dev/null 2>&1 ) || true
+ echo "$_BRAIN_NOW" > "$_BRAIN_LAST_PULL_FILE"
+ fi
+ # Drain pending queue, push.
+ "$_BRAIN_SYNC_BIN" --once 2>/dev/null || true
+fi
+
+# Status line — always emitted, easy to grep.
+if [ -d "$_GSTACK_HOME/.git" ] && [ "$_BRAIN_SYNC_MODE" != "off" ]; then
+ _BRAIN_QUEUE_DEPTH=0
+ [ -f "$_GSTACK_HOME/.brain-queue.jsonl" ] && _BRAIN_QUEUE_DEPTH=$(wc -l < "$_GSTACK_HOME/.brain-queue.jsonl" | tr -d ' ')
+ _BRAIN_LAST_PUSH="never"
+ [ -f "$_GSTACK_HOME/.brain-last-push" ] && _BRAIN_LAST_PUSH=$(cat "$_GSTACK_HOME/.brain-last-push" 2>/dev/null || echo never)
+ echo "BRAIN_SYNC: mode=$_BRAIN_SYNC_MODE | last_push=$_BRAIN_LAST_PUSH | queue=$_BRAIN_QUEUE_DEPTH"
+else
+ echo "BRAIN_SYNC: off"
+fi
+```
+
+
+
+**Privacy stop-gate (fires ONCE per machine).**
+
+If the bash output shows `BRAIN_SYNC: off` AND the config value
+`gbrain_sync_mode_prompted` is `false` AND gbrain is detected on this host
+(either `gbrain doctor --fast --json` succeeds or the `gbrain` binary is in PATH),
+fire a one-time privacy gate via AskUserQuestion:
+
+> gstack can publish your session memory (learnings, plans, designs, retros) to a
+> private GitHub repo that GBrain indexes across your machines. Higher tiers
+> include behavioral data (session timelines, developer profile). How much do you
+> want to sync?
+
+Options:
+- A) Everything allowlisted (recommended — maximum cross-machine memory)
+- B) Only artifacts (plans, designs, retros, learnings) — skip timelines and profile
+- C) Decline — keep everything local
+
+After the user answers, run (substituting the chosen value):
+
+```bash
+# Chosen mode: full | artifacts-only | off
+"$_BRAIN_CONFIG_BIN" set gbrain_sync_mode <choice>
+"$_BRAIN_CONFIG_BIN" set gbrain_sync_mode_prompted true
+```
+
+If A or B was chosen AND `~/.gstack/.git` doesn't exist, ask a follow-up:
+"Set up the GBrain sync repo now? (runs `gstack-brain-init`)"
+- A) Yes, run it now
+- B) Show me the command, I'll run it myself
+
+Do not block the skill. Emit the question, continue the skill workflow. The
+next skill run picks up wherever this left off.
+
+**At skill END (before the telemetry block),** run these bash commands to
+catch artifact writes (design docs, plans, retros) that skipped the writer
+shims, plus drain any still-pending queue entries:
+
+```bash
+"~/.claude/skills/gstack/bin/gstack-brain-sync" --discover-new 2>/dev/null || true
+"~/.claude/skills/gstack/bin/gstack-brain-sync" --once 2>/dev/null || true
+```
+
+
## Model-Specific Behavioral Patch (claude)
The following nudges are tuned for the claude model family. They are
diff --git a/context-restore/SKILL.md b/context-restore/SKILL.md
index 6f44b45fe..cff29b864 100644
--- a/context-restore/SKILL.md
+++ b/context-restore/SKILL.md
@@ -354,6 +354,105 @@ AI orchestrator (e.g., OpenClaw). In spawned sessions:
- Focus on completing the task and reporting results via prose output.
- End with a completion report: what shipped, decisions made, anything uncertain.
+## GBrain Sync (skill start)
+
+```bash
+# gbrain-sync: drain pending writes, pull once per day. Silent no-op when
+# the feature isn't initialized or gbrain_sync_mode is "off". See
+# docs/gbrain-sync.md.
+
+_GSTACK_HOME="${GSTACK_HOME:-$HOME/.gstack}"
+_BRAIN_REMOTE_FILE="$HOME/.gstack-brain-remote.txt"
+_BRAIN_SYNC_BIN="~/.claude/skills/gstack/bin/gstack-brain-sync"
+_BRAIN_CONFIG_BIN="~/.claude/skills/gstack/bin/gstack-config"
+
+_BRAIN_SYNC_MODE=$("$_BRAIN_CONFIG_BIN" get gbrain_sync_mode 2>/dev/null || echo off)
+
+# New-machine hint: URL file present, local .git missing, sync not yet enabled.
+if [ -f "$_BRAIN_REMOTE_FILE" ] && [ ! -d "$_GSTACK_HOME/.git" ] && [ "$_BRAIN_SYNC_MODE" = "off" ]; then
+ _BRAIN_NEW_URL=$(head -1 "$_BRAIN_REMOTE_FILE" 2>/dev/null | tr -d '[:space:]')
+ if [ -n "$_BRAIN_NEW_URL" ]; then
+ echo "BRAIN_SYNC: brain repo detected: $_BRAIN_NEW_URL"
+ echo "BRAIN_SYNC: run 'gstack-brain-restore' to pull your cross-machine memory (or 'gstack-config set gbrain_sync_mode off' to dismiss forever)"
+ fi
+fi
+
+# Active-sync path.
+if [ -d "$_GSTACK_HOME/.git" ] && [ "$_BRAIN_SYNC_MODE" != "off" ]; then
+ # Once-per-day pull.
+ _BRAIN_LAST_PULL_FILE="$_GSTACK_HOME/.brain-last-pull"
+ _BRAIN_NOW=$(date +%s)
+ _BRAIN_DO_PULL=1
+ if [ -f "$_BRAIN_LAST_PULL_FILE" ]; then
+ _BRAIN_LAST=$(cat "$_BRAIN_LAST_PULL_FILE" 2>/dev/null || echo 0)
+ _BRAIN_AGE=$(( _BRAIN_NOW - _BRAIN_LAST ))
+ [ "$_BRAIN_AGE" -lt 86400 ] && _BRAIN_DO_PULL=0
+ fi
+ if [ "$_BRAIN_DO_PULL" = "1" ]; then
+ ( cd "$_GSTACK_HOME" && git fetch origin >/dev/null 2>&1 && git merge --ff-only "origin/$(git rev-parse --abbrev-ref HEAD)" >/dev/null 2>&1 ) || true
+ echo "$_BRAIN_NOW" > "$_BRAIN_LAST_PULL_FILE"
+ fi
+ # Drain pending queue, push.
+ "$_BRAIN_SYNC_BIN" --once 2>/dev/null || true
+fi
+
+# Status line — always emitted, easy to grep.
+if [ -d "$_GSTACK_HOME/.git" ] && [ "$_BRAIN_SYNC_MODE" != "off" ]; then
+ _BRAIN_QUEUE_DEPTH=0
+ [ -f "$_GSTACK_HOME/.brain-queue.jsonl" ] && _BRAIN_QUEUE_DEPTH=$(wc -l < "$_GSTACK_HOME/.brain-queue.jsonl" | tr -d ' ')
+ _BRAIN_LAST_PUSH="never"
+ [ -f "$_GSTACK_HOME/.brain-last-push" ] && _BRAIN_LAST_PUSH=$(cat "$_GSTACK_HOME/.brain-last-push" 2>/dev/null || echo never)
+ echo "BRAIN_SYNC: mode=$_BRAIN_SYNC_MODE | last_push=$_BRAIN_LAST_PUSH | queue=$_BRAIN_QUEUE_DEPTH"
+else
+ echo "BRAIN_SYNC: off"
+fi
+```
+
+
+
+**Privacy stop-gate (fires ONCE per machine).**
+
+If the bash output shows `BRAIN_SYNC: off` AND the config value
+`gbrain_sync_mode_prompted` is `false` AND gbrain is detected on this host
+(either `gbrain doctor --fast --json` succeeds or the `gbrain` binary is in PATH),
+fire a one-time privacy gate via AskUserQuestion:
+
+> gstack can publish your session memory (learnings, plans, designs, retros) to a
+> private GitHub repo that GBrain indexes across your machines. Higher tiers
+> include behavioral data (session timelines, developer profile). How much do you
+> want to sync?
+
+Options:
+- A) Everything allowlisted (recommended — maximum cross-machine memory)
+- B) Only artifacts (plans, designs, retros, learnings) — skip timelines and profile
+- C) Decline — keep everything local
+
+After the user answers, run (substituting the chosen value):
+
+```bash
+# Chosen mode: full | artifacts-only | off
+"$_BRAIN_CONFIG_BIN" set gbrain_sync_mode <choice>
+"$_BRAIN_CONFIG_BIN" set gbrain_sync_mode_prompted true
+```
+
+If A or B was chosen AND `~/.gstack/.git` doesn't exist, ask a follow-up:
+"Set up the GBrain sync repo now? (runs `gstack-brain-init`)"
+- A) Yes, run it now
+- B) Show me the command, I'll run it myself
+
+Do not block the skill. Emit the question, continue the skill workflow. The
+next skill run picks up wherever this left off.
+
+**At skill END (before the telemetry block),** run these bash commands to
+catch artifact writes (design docs, plans, retros) that skipped the writer
+shims, plus drain any still-pending queue entries:
+
+```bash
+"~/.claude/skills/gstack/bin/gstack-brain-sync" --discover-new 2>/dev/null || true
+"~/.claude/skills/gstack/bin/gstack-brain-sync" --once 2>/dev/null || true
+```
+
+
## Model-Specific Behavioral Patch (claude)
The following nudges are tuned for the claude model family. They are
diff --git a/context-save/SKILL.md b/context-save/SKILL.md
index c1cdadba5..5efcf1cf8 100644
--- a/context-save/SKILL.md
+++ b/context-save/SKILL.md
@@ -354,6 +354,105 @@ AI orchestrator (e.g., OpenClaw). In spawned sessions:
- Focus on completing the task and reporting results via prose output.
- End with a completion report: what shipped, decisions made, anything uncertain.
+## GBrain Sync (skill start)
+
+```bash
+# gbrain-sync: drain pending writes, pull once per day. Silent no-op when
+# the feature isn't initialized or gbrain_sync_mode is "off". See
+# docs/gbrain-sync.md.
+
+_GSTACK_HOME="${GSTACK_HOME:-$HOME/.gstack}"
+_BRAIN_REMOTE_FILE="$HOME/.gstack-brain-remote.txt"
+_BRAIN_SYNC_BIN="~/.claude/skills/gstack/bin/gstack-brain-sync"
+_BRAIN_CONFIG_BIN="~/.claude/skills/gstack/bin/gstack-config"
+
+_BRAIN_SYNC_MODE=$("$_BRAIN_CONFIG_BIN" get gbrain_sync_mode 2>/dev/null || echo off)
+
+# New-machine hint: URL file present, local .git missing, sync not yet enabled.
+if [ -f "$_BRAIN_REMOTE_FILE" ] && [ ! -d "$_GSTACK_HOME/.git" ] && [ "$_BRAIN_SYNC_MODE" = "off" ]; then
+ _BRAIN_NEW_URL=$(head -1 "$_BRAIN_REMOTE_FILE" 2>/dev/null | tr -d '[:space:]')
+ if [ -n "$_BRAIN_NEW_URL" ]; then
+ echo "BRAIN_SYNC: brain repo detected: $_BRAIN_NEW_URL"
+ echo "BRAIN_SYNC: run 'gstack-brain-restore' to pull your cross-machine memory (or 'gstack-config set gbrain_sync_mode off' to dismiss forever)"
+ fi
+fi
+
+# Active-sync path.
+if [ -d "$_GSTACK_HOME/.git" ] && [ "$_BRAIN_SYNC_MODE" != "off" ]; then
+ # Once-per-day pull.
+ _BRAIN_LAST_PULL_FILE="$_GSTACK_HOME/.brain-last-pull"
+ _BRAIN_NOW=$(date +%s)
+ _BRAIN_DO_PULL=1
+ if [ -f "$_BRAIN_LAST_PULL_FILE" ]; then
+ _BRAIN_LAST=$(cat "$_BRAIN_LAST_PULL_FILE" 2>/dev/null || echo 0)
+ _BRAIN_AGE=$(( _BRAIN_NOW - _BRAIN_LAST ))
+ [ "$_BRAIN_AGE" -lt 86400 ] && _BRAIN_DO_PULL=0
+ fi
+ if [ "$_BRAIN_DO_PULL" = "1" ]; then
+ ( cd "$_GSTACK_HOME" && git fetch origin >/dev/null 2>&1 && git merge --ff-only "origin/$(git rev-parse --abbrev-ref HEAD)" >/dev/null 2>&1 ) || true
+ echo "$_BRAIN_NOW" > "$_BRAIN_LAST_PULL_FILE"
+ fi
+ # Drain pending queue, push.
+ "$_BRAIN_SYNC_BIN" --once 2>/dev/null || true
+fi
+
+# Status line — always emitted, easy to grep.
+if [ -d "$_GSTACK_HOME/.git" ] && [ "$_BRAIN_SYNC_MODE" != "off" ]; then
+ _BRAIN_QUEUE_DEPTH=0
+ [ -f "$_GSTACK_HOME/.brain-queue.jsonl" ] && _BRAIN_QUEUE_DEPTH=$(wc -l < "$_GSTACK_HOME/.brain-queue.jsonl" | tr -d ' ')
+ _BRAIN_LAST_PUSH="never"
+ [ -f "$_GSTACK_HOME/.brain-last-push" ] && _BRAIN_LAST_PUSH=$(cat "$_GSTACK_HOME/.brain-last-push" 2>/dev/null || echo never)
+ echo "BRAIN_SYNC: mode=$_BRAIN_SYNC_MODE | last_push=$_BRAIN_LAST_PUSH | queue=$_BRAIN_QUEUE_DEPTH"
+else
+ echo "BRAIN_SYNC: off"
+fi
+```
+
+
+
+**Privacy stop-gate (fires ONCE per machine).**
+
+If the bash output shows `BRAIN_SYNC: off` AND the config value
+`gbrain_sync_mode_prompted` is `false` AND gbrain is detected on this host
+(either `gbrain doctor --fast --json` succeeds or the `gbrain` binary is in PATH),
+fire a one-time privacy gate via AskUserQuestion:
+
+> gstack can publish your session memory (learnings, plans, designs, retros) to a
+> private GitHub repo that GBrain indexes across your machines. Higher tiers
+> include behavioral data (session timelines, developer profile). How much do you
+> want to sync?
+
+Options:
+- A) Everything allowlisted (recommended — maximum cross-machine memory)
+- B) Only artifacts (plans, designs, retros, learnings) — skip timelines and profile
+- C) Decline — keep everything local
+
+After the user answers, run (substituting the chosen value):
+
+```bash
+# Chosen mode: full | artifacts-only | off
+"$_BRAIN_CONFIG_BIN" set gbrain_sync_mode <choice>
+"$_BRAIN_CONFIG_BIN" set gbrain_sync_mode_prompted true
+```
+
+If A or B was chosen AND `~/.gstack/.git` doesn't exist, ask a follow-up:
+"Set up the GBrain sync repo now? (runs `gstack-brain-init`)"
+- A) Yes, run it now
+- B) Show me the command, I'll run it myself
+
+Do not block the skill. Emit the question, continue the skill workflow. The
+next skill run picks up wherever this left off.
+
+**At skill END (before the telemetry block),** run these bash commands to
+catch artifact writes (design docs, plans, retros) that skipped the writer
+shims, plus drain any still-pending queue entries:
+
+```bash
+"~/.claude/skills/gstack/bin/gstack-brain-sync" --discover-new 2>/dev/null || true
+"~/.claude/skills/gstack/bin/gstack-brain-sync" --once 2>/dev/null || true
+```
+
+
## Model-Specific Behavioral Patch (claude)
The following nudges are tuned for the claude model family. They are
@@ -982,106 +1081,6 @@ Restore later with /context-restore.
---
-<<<<<<< HEAD:checkpoint/SKILL.md.tmpl
-## Resume flow
-
-### Step 1: Find checkpoints
-
-```bash
-eval "$(~/.claude/skills/gstack/bin/gstack-slug 2>/dev/null)" && mkdir -p ~/.gstack/projects/$SLUG
-CHECKPOINT_DIR="$HOME/.gstack/projects/$SLUG/checkpoints"
-if [ -d "$CHECKPOINT_DIR" ]; then
- find "$CHECKPOINT_DIR" -maxdepth 1 -name "*.md" -type f 2>/dev/null | xargs ls -1t 2>/dev/null | head -20
-else
- echo "NO_CHECKPOINTS"
-fi
-```
-
-List checkpoints from **all branches** (checkpoint files contain the branch name
-in their frontmatter, so all files in the directory are candidates). This enables
-Conductor workspace handoff — a checkpoint saved on one branch can be resumed from
-another.
-
-### Step 1.5: Check for WIP commit context (continuous checkpoint mode)
-
-If `CHECKPOINT_MODE` was `"continuous"` during prior work, the branch may have
-`WIP:` commits with structured `[gstack-context]` blocks in their bodies. These
-are a second recovery trail alongside the markdown checkpoint files.
-
-```bash
-_BRANCH=$(git branch --show-current 2>/dev/null)
-# Detect if this branch has any WIP commits against the nearest remote ancestor
-_BASE=$(git merge-base HEAD origin/main 2>/dev/null || git merge-base HEAD origin/master 2>/dev/null)
-if [ -n "$_BASE" ]; then
- WIP_COMMITS=$(git log "$_BASE"..HEAD --grep="^WIP:" --format="%H" 2>/dev/null | head -20)
- if [ -n "$WIP_COMMITS" ]; then
- echo "WIP_COMMITS_FOUND"
- # Extract [gstack-context] blocks from each WIP commit body
- for SHA in $WIP_COMMITS; do
- echo "--- commit $SHA ---"
- git log -1 "$SHA" --format="%s%n%n%b" 2>/dev/null | \
- awk '/\[gstack-context\]/,/\[\/gstack-context\]/ { print }'
- done
- else
- echo "NO_WIP_COMMITS"
- fi
-fi
-```
-
-If `WIP_COMMITS_FOUND`: Read the extracted `[gstack-context]` blocks. Each block
-represents a logical unit of prior work with Decisions/Remaining/Tried/Skill.
-Merge these with the markdown checkpoint file to reconstruct session state. The
-git history shows the chronological arc; the markdown checkpoint shows the
-intentional save points. Both matter.
-
-**Important:** Do NOT delete WIP commits during resume. They remain the recovery
-trail until /ship squashes them into clean commits during PR creation.
-
-### Step 2: Load checkpoint
-
-If the user specified a checkpoint (by number, title fragment, or date), find the
-matching file. Otherwise, load the **most recent** checkpoint.
-
-Read the checkpoint file and present a summary:
-
-```
-RESUMING CHECKPOINT
-════════════════════════════════════════
-Title: {title}
-Branch: {branch from checkpoint}
-Saved: {timestamp, human-readable}
-Duration: Last session was {formatted duration} (if available)
-Status: {status}
-════════════════════════════════════════
-
-### Summary
-{summary from checkpoint}
-
-### Remaining Work
-{remaining work items from checkpoint}
-
-### Notes
-{notes from checkpoint}
-```
-
-If the current branch differs from the checkpoint's branch, note this:
-"This checkpoint was saved on branch `{branch}`. You are currently on
-`{current branch}`. You may want to switch branches before continuing."
-
-### Step 3: Offer next steps
-
-After presenting the checkpoint, ask via AskUserQuestion:
-
-- A) Continue working on the remaining items
-- B) Show the full checkpoint file
-- C) Just needed the context, thanks
-
-If A, summarize the first remaining work item and suggest starting there.
-
----
-
-=======
->>>>>>> origin/main:context-save/SKILL.md.tmpl
## List flow
### Step 1: Gather saved contexts
diff --git a/context-save/SKILL.md.tmpl b/context-save/SKILL.md.tmpl
index 0854baf33..8343873f0 100644
--- a/context-save/SKILL.md.tmpl
+++ b/context-save/SKILL.md.tmpl
@@ -198,106 +198,6 @@ Restore later with /context-restore.
---
-<<<<<<< HEAD:checkpoint/SKILL.md.tmpl
-## Resume flow
-
-### Step 1: Find checkpoints
-
-```bash
-{{SLUG_SETUP}}
-CHECKPOINT_DIR="$HOME/.gstack/projects/$SLUG/checkpoints"
-if [ -d "$CHECKPOINT_DIR" ]; then
- find "$CHECKPOINT_DIR" -maxdepth 1 -name "*.md" -type f 2>/dev/null | xargs ls -1t 2>/dev/null | head -20
-else
- echo "NO_CHECKPOINTS"
-fi
-```
-
-List checkpoints from **all branches** (checkpoint files contain the branch name
-in their frontmatter, so all files in the directory are candidates). This enables
-Conductor workspace handoff — a checkpoint saved on one branch can be resumed from
-another.
-
-### Step 1.5: Check for WIP commit context (continuous checkpoint mode)
-
-If `CHECKPOINT_MODE` was `"continuous"` during prior work, the branch may have
-`WIP:` commits with structured `[gstack-context]` blocks in their bodies. These
-are a second recovery trail alongside the markdown checkpoint files.
-
-```bash
-_BRANCH=$(git branch --show-current 2>/dev/null)
-# Detect if this branch has any WIP commits against the nearest remote ancestor
-_BASE=$(git merge-base HEAD origin/main 2>/dev/null || git merge-base HEAD origin/master 2>/dev/null)
-if [ -n "$_BASE" ]; then
- WIP_COMMITS=$(git log "$_BASE"..HEAD --grep="^WIP:" --format="%H" 2>/dev/null | head -20)
- if [ -n "$WIP_COMMITS" ]; then
- echo "WIP_COMMITS_FOUND"
- # Extract [gstack-context] blocks from each WIP commit body
- for SHA in $WIP_COMMITS; do
- echo "--- commit $SHA ---"
- git log -1 "$SHA" --format="%s%n%n%b" 2>/dev/null | \
- awk '/\[gstack-context\]/,/\[\/gstack-context\]/ { print }'
- done
- else
- echo "NO_WIP_COMMITS"
- fi
-fi
-```
-
-If `WIP_COMMITS_FOUND`: Read the extracted `[gstack-context]` blocks. Each block
-represents a logical unit of prior work with Decisions/Remaining/Tried/Skill.
-Merge these with the markdown checkpoint file to reconstruct session state. The
-git history shows the chronological arc; the markdown checkpoint shows the
-intentional save points. Both matter.
-
-**Important:** Do NOT delete WIP commits during resume. They remain the recovery
-trail until /ship squashes them into clean commits during PR creation.
-
-### Step 2: Load checkpoint
-
-If the user specified a checkpoint (by number, title fragment, or date), find the
-matching file. Otherwise, load the **most recent** checkpoint.
-
-Read the checkpoint file and present a summary:
-
-```
-RESUMING CHECKPOINT
-════════════════════════════════════════
-Title: {title}
-Branch: {branch from checkpoint}
-Saved: {timestamp, human-readable}
-Duration: Last session was {formatted duration} (if available)
-Status: {status}
-════════════════════════════════════════
-
-### Summary
-{summary from checkpoint}
-
-### Remaining Work
-{remaining work items from checkpoint}
-
-### Notes
-{notes from checkpoint}
-```
-
-If the current branch differs from the checkpoint's branch, note this:
-"This checkpoint was saved on branch `{branch}`. You are currently on
-`{current branch}`. You may want to switch branches before continuing."
-
-### Step 3: Offer next steps
-
-After presenting the checkpoint, ask via AskUserQuestion:
-
-- A) Continue working on the remaining items
-- B) Show the full checkpoint file
-- C) Just needed the context, thanks
-
-If A, summarize the first remaining work item and suggest starting there.
-
----
-
-=======
->>>>>>> origin/main:context-save/SKILL.md.tmpl
## List flow
### Step 1: Gather saved contexts
diff --git a/cso/SKILL.md b/cso/SKILL.md
index 2aafca824..820c135be 100644
--- a/cso/SKILL.md
+++ b/cso/SKILL.md
@@ -355,6 +355,105 @@ AI orchestrator (e.g., OpenClaw). In spawned sessions:
- Focus on completing the task and reporting results via prose output.
- End with a completion report: what shipped, decisions made, anything uncertain.
+## GBrain Sync (skill start)
+
+```bash
+# gbrain-sync: drain pending writes, pull once per day. Silent no-op when
+# the feature isn't initialized or gbrain_sync_mode is "off". See
+# docs/gbrain-sync.md.
+
+_GSTACK_HOME="${GSTACK_HOME:-$HOME/.gstack}"
+_BRAIN_REMOTE_FILE="$HOME/.gstack-brain-remote.txt"
+_BRAIN_SYNC_BIN="~/.claude/skills/gstack/bin/gstack-brain-sync"
+_BRAIN_CONFIG_BIN="~/.claude/skills/gstack/bin/gstack-config"
+
+_BRAIN_SYNC_MODE=$("$_BRAIN_CONFIG_BIN" get gbrain_sync_mode 2>/dev/null || echo off)
+
+# New-machine hint: URL file present, local .git missing, sync not yet enabled.
+if [ -f "$_BRAIN_REMOTE_FILE" ] && [ ! -d "$_GSTACK_HOME/.git" ] && [ "$_BRAIN_SYNC_MODE" = "off" ]; then
+ _BRAIN_NEW_URL=$(head -1 "$_BRAIN_REMOTE_FILE" 2>/dev/null | tr -d '[:space:]')
+ if [ -n "$_BRAIN_NEW_URL" ]; then
+ echo "BRAIN_SYNC: brain repo detected: $_BRAIN_NEW_URL"
+ echo "BRAIN_SYNC: run 'gstack-brain-restore' to pull your cross-machine memory (or 'gstack-config set gbrain_sync_mode off' to dismiss forever)"
+ fi
+fi
+
+# Active-sync path.
+if [ -d "$_GSTACK_HOME/.git" ] && [ "$_BRAIN_SYNC_MODE" != "off" ]; then
+ # Once-per-day pull.
+ _BRAIN_LAST_PULL_FILE="$_GSTACK_HOME/.brain-last-pull"
+ _BRAIN_NOW=$(date +%s)
+ _BRAIN_DO_PULL=1
+ if [ -f "$_BRAIN_LAST_PULL_FILE" ]; then
+ _BRAIN_LAST=$(cat "$_BRAIN_LAST_PULL_FILE" 2>/dev/null || echo 0)
+ _BRAIN_AGE=$(( _BRAIN_NOW - _BRAIN_LAST ))
+ [ "$_BRAIN_AGE" -lt 86400 ] && _BRAIN_DO_PULL=0
+ fi
+ if [ "$_BRAIN_DO_PULL" = "1" ]; then
+ ( cd "$_GSTACK_HOME" && git fetch origin >/dev/null 2>&1 && git merge --ff-only "origin/$(git rev-parse --abbrev-ref HEAD)" >/dev/null 2>&1 ) || true
+ echo "$_BRAIN_NOW" > "$_BRAIN_LAST_PULL_FILE"
+ fi
+ # Drain pending queue, push.
+ "$_BRAIN_SYNC_BIN" --once 2>/dev/null || true
+fi
+
+# Status line — always emitted, easy to grep.
+if [ -d "$_GSTACK_HOME/.git" ] && [ "$_BRAIN_SYNC_MODE" != "off" ]; then
+ _BRAIN_QUEUE_DEPTH=0
+ [ -f "$_GSTACK_HOME/.brain-queue.jsonl" ] && _BRAIN_QUEUE_DEPTH=$(wc -l < "$_GSTACK_HOME/.brain-queue.jsonl" | tr -d ' ')
+ _BRAIN_LAST_PUSH="never"
+ [ -f "$_GSTACK_HOME/.brain-last-push" ] && _BRAIN_LAST_PUSH=$(cat "$_GSTACK_HOME/.brain-last-push" 2>/dev/null || echo never)
+ echo "BRAIN_SYNC: mode=$_BRAIN_SYNC_MODE | last_push=$_BRAIN_LAST_PUSH | queue=$_BRAIN_QUEUE_DEPTH"
+else
+ echo "BRAIN_SYNC: off"
+fi
+```
+
+
+
+**Privacy stop-gate (fires ONCE per machine).**
+
+If the bash output shows `BRAIN_SYNC: off` AND the config value
+`gbrain_sync_mode_prompted` is `false` AND gbrain is detected on this host
+(either `gbrain doctor --fast --json` succeeds or the `gbrain` binary is in PATH),
+fire a one-time privacy gate via AskUserQuestion:
+
+> gstack can publish your session memory (learnings, plans, designs, retros) to a
+> private GitHub repo that GBrain indexes across your machines. Higher tiers
+> include behavioral data (session timelines, developer profile). How much do you
+> want to sync?
+
+Options:
+- A) Everything allowlisted (recommended — maximum cross-machine memory)
+- B) Only artifacts (plans, designs, retros, learnings) — skip timelines and profile
+- C) Decline — keep everything local
+
+After the user answers, run (substituting the chosen value):
+
+```bash
+# Chosen mode: full | artifacts-only | off
+"$_BRAIN_CONFIG_BIN" set gbrain_sync_mode <choice>
+"$_BRAIN_CONFIG_BIN" set gbrain_sync_mode_prompted true
+```
+
+If A or B was chosen AND `~/.gstack/.git` doesn't exist, ask a follow-up:
+"Set up the GBrain sync repo now? (runs `gstack-brain-init`)"
+- A) Yes, run it now
+- B) Show me the command, I'll run it myself
+
+Do not block the skill. Emit the question, continue the skill workflow. The
+next skill run picks up wherever this left off.
+
+**At skill END (before the telemetry block),** run these bash commands to
+catch artifact writes (design docs, plans, retros) that skipped the writer
+shims, plus drain any still-pending queue entries:
+
+```bash
+"~/.claude/skills/gstack/bin/gstack-brain-sync" --discover-new 2>/dev/null || true
+"~/.claude/skills/gstack/bin/gstack-brain-sync" --once 2>/dev/null || true
+```
+
+
## Model-Specific Behavioral Patch (claude)
The following nudges are tuned for the claude model family. They are
diff --git a/design-consultation/SKILL.md b/design-consultation/SKILL.md
index 06a48adc5..c7703c7f7 100644
--- a/design-consultation/SKILL.md
+++ b/design-consultation/SKILL.md
@@ -355,6 +355,105 @@ AI orchestrator (e.g., OpenClaw). In spawned sessions:
- Focus on completing the task and reporting results via prose output.
- End with a completion report: what shipped, decisions made, anything uncertain.
+## GBrain Sync (skill start)
+
+```bash
+# gbrain-sync: drain pending writes, pull once per day. Silent no-op when
+# the feature isn't initialized or gbrain_sync_mode is "off". See
+# docs/gbrain-sync.md.
+
+_GSTACK_HOME="${GSTACK_HOME:-$HOME/.gstack}"
+_BRAIN_REMOTE_FILE="$HOME/.gstack-brain-remote.txt"
+_BRAIN_SYNC_BIN="~/.claude/skills/gstack/bin/gstack-brain-sync"
+_BRAIN_CONFIG_BIN="~/.claude/skills/gstack/bin/gstack-config"
+
+_BRAIN_SYNC_MODE=$("$_BRAIN_CONFIG_BIN" get gbrain_sync_mode 2>/dev/null || echo off)
+
+# New-machine hint: URL file present, local .git missing, sync not yet enabled.
+if [ -f "$_BRAIN_REMOTE_FILE" ] && [ ! -d "$_GSTACK_HOME/.git" ] && [ "$_BRAIN_SYNC_MODE" = "off" ]; then
+ _BRAIN_NEW_URL=$(head -1 "$_BRAIN_REMOTE_FILE" 2>/dev/null | tr -d '[:space:]')
+ if [ -n "$_BRAIN_NEW_URL" ]; then
+ echo "BRAIN_SYNC: brain repo detected: $_BRAIN_NEW_URL"
+ echo "BRAIN_SYNC: run 'gstack-brain-restore' to pull your cross-machine memory (or 'gstack-config set gbrain_sync_mode off' to dismiss forever)"
+ fi
+fi
+
+# Active-sync path.
+if [ -d "$_GSTACK_HOME/.git" ] && [ "$_BRAIN_SYNC_MODE" != "off" ]; then
+ # Once-per-day pull.
+ _BRAIN_LAST_PULL_FILE="$_GSTACK_HOME/.brain-last-pull"
+ _BRAIN_NOW=$(date +%s)
+ _BRAIN_DO_PULL=1
+ if [ -f "$_BRAIN_LAST_PULL_FILE" ]; then
+ _BRAIN_LAST=$(cat "$_BRAIN_LAST_PULL_FILE" 2>/dev/null || echo 0)
+ _BRAIN_AGE=$(( _BRAIN_NOW - _BRAIN_LAST ))
+ [ "$_BRAIN_AGE" -lt 86400 ] && _BRAIN_DO_PULL=0
+ fi
+ if [ "$_BRAIN_DO_PULL" = "1" ]; then
+ ( cd "$_GSTACK_HOME" && git fetch origin >/dev/null 2>&1 && git merge --ff-only "origin/$(git rev-parse --abbrev-ref HEAD)" >/dev/null 2>&1 ) || true
+ echo "$_BRAIN_NOW" > "$_BRAIN_LAST_PULL_FILE"
+ fi
+ # Drain pending queue, push.
+ "$_BRAIN_SYNC_BIN" --once 2>/dev/null || true
+fi
+
+# Status line — always emitted, easy to grep.
+if [ -d "$_GSTACK_HOME/.git" ] && [ "$_BRAIN_SYNC_MODE" != "off" ]; then
+ _BRAIN_QUEUE_DEPTH=0
+ [ -f "$_GSTACK_HOME/.brain-queue.jsonl" ] && _BRAIN_QUEUE_DEPTH=$(wc -l < "$_GSTACK_HOME/.brain-queue.jsonl" | tr -d ' ')
+ _BRAIN_LAST_PUSH="never"
+ [ -f "$_GSTACK_HOME/.brain-last-push" ] && _BRAIN_LAST_PUSH=$(cat "$_GSTACK_HOME/.brain-last-push" 2>/dev/null || echo never)
+ echo "BRAIN_SYNC: mode=$_BRAIN_SYNC_MODE | last_push=$_BRAIN_LAST_PUSH | queue=$_BRAIN_QUEUE_DEPTH"
+else
+ echo "BRAIN_SYNC: off"
+fi
+```
+
+
+
+**Privacy stop-gate (fires ONCE per machine).**
+
+If the bash output shows `BRAIN_SYNC: off` AND the config value
+`gbrain_sync_mode_prompted` is `false` AND gbrain is detected on this host
+(either `gbrain doctor --fast --json` succeeds or the `gbrain` binary is in PATH),
+fire a one-time privacy gate via AskUserQuestion:
+
+> gstack can publish your session memory (learnings, plans, designs, retros) to a
+> private GitHub repo that GBrain indexes across your machines. Higher tiers
+> include behavioral data (session timelines, developer profile). How much do you
+> want to sync?
+
+Options:
+- A) Everything allowlisted (recommended — maximum cross-machine memory)
+- B) Only artifacts (plans, designs, retros, learnings) — skip timelines and profile
+- C) Decline — keep everything local
+
+After the user answers, run (substituting the chosen value):
+
+```bash
+# Chosen mode: full | artifacts-only | off
+"$_BRAIN_CONFIG_BIN" set gbrain_sync_mode <choice>
+"$_BRAIN_CONFIG_BIN" set gbrain_sync_mode_prompted true
+```
+
+If A or B was chosen AND `~/.gstack/.git` doesn't exist, ask a follow-up:
+"Set up the GBrain sync repo now? (runs `gstack-brain-init`)"
+- A) Yes, run it now
+- B) Show me the command, I'll run it myself
+
+Do not block the skill. Emit the question, continue the skill workflow. The
+next skill run picks up wherever this left off.
+
+**At skill END (before the telemetry block),** run these bash commands to
+catch artifact writes (design docs, plans, retros) that skipped the writer
+shims, plus drain any still-pending queue entries:
+
+```bash
+"~/.claude/skills/gstack/bin/gstack-brain-sync" --discover-new 2>/dev/null || true
+"~/.claude/skills/gstack/bin/gstack-brain-sync" --once 2>/dev/null || true
+```
+
+
## Model-Specific Behavioral Patch (claude)
The following nudges are tuned for the claude model family. They are
diff --git a/design-html/SKILL.md b/design-html/SKILL.md
index 3b6ef550c..ba0e1e1a1 100644
--- a/design-html/SKILL.md
+++ b/design-html/SKILL.md
@@ -357,6 +357,105 @@ AI orchestrator (e.g., OpenClaw). In spawned sessions:
- Focus on completing the task and reporting results via prose output.
- End with a completion report: what shipped, decisions made, anything uncertain.
+## GBrain Sync (skill start)
+
+```bash
+# gbrain-sync: drain pending writes, pull once per day. Silent no-op when
+# the feature isn't initialized or gbrain_sync_mode is "off". See
+# docs/gbrain-sync.md.
+
+_GSTACK_HOME="${GSTACK_HOME:-$HOME/.gstack}"
+_BRAIN_REMOTE_FILE="$HOME/.gstack-brain-remote.txt"
+_BRAIN_SYNC_BIN="~/.claude/skills/gstack/bin/gstack-brain-sync"
+_BRAIN_CONFIG_BIN="~/.claude/skills/gstack/bin/gstack-config"
+
+_BRAIN_SYNC_MODE=$("$_BRAIN_CONFIG_BIN" get gbrain_sync_mode 2>/dev/null || echo off)
+
+# New-machine hint: URL file present, local .git missing, sync not yet enabled.
+if [ -f "$_BRAIN_REMOTE_FILE" ] && [ ! -d "$_GSTACK_HOME/.git" ] && [ "$_BRAIN_SYNC_MODE" = "off" ]; then
+ _BRAIN_NEW_URL=$(head -1 "$_BRAIN_REMOTE_FILE" 2>/dev/null | tr -d '[:space:]')
+ if [ -n "$_BRAIN_NEW_URL" ]; then
+ echo "BRAIN_SYNC: brain repo detected: $_BRAIN_NEW_URL"
+ echo "BRAIN_SYNC: run 'gstack-brain-restore' to pull your cross-machine memory (or 'gstack-config set gbrain_sync_mode off' to dismiss forever)"
+ fi
+fi
+
+# Active-sync path.
+if [ -d "$_GSTACK_HOME/.git" ] && [ "$_BRAIN_SYNC_MODE" != "off" ]; then
+ # Once-per-day pull.
+ _BRAIN_LAST_PULL_FILE="$_GSTACK_HOME/.brain-last-pull"
+ _BRAIN_NOW=$(date +%s)
+ _BRAIN_DO_PULL=1
+ if [ -f "$_BRAIN_LAST_PULL_FILE" ]; then
+ _BRAIN_LAST=$(cat "$_BRAIN_LAST_PULL_FILE" 2>/dev/null || echo 0)
+ _BRAIN_AGE=$(( _BRAIN_NOW - _BRAIN_LAST ))
+ [ "$_BRAIN_AGE" -lt 86400 ] && _BRAIN_DO_PULL=0
+ fi
+ if [ "$_BRAIN_DO_PULL" = "1" ]; then
+ ( cd "$_GSTACK_HOME" && git fetch origin >/dev/null 2>&1 && git merge --ff-only "origin/$(git rev-parse --abbrev-ref HEAD)" >/dev/null 2>&1 ) || true
+ echo "$_BRAIN_NOW" > "$_BRAIN_LAST_PULL_FILE"
+ fi
+ # Drain pending queue, push.
+ "$_BRAIN_SYNC_BIN" --once 2>/dev/null || true
+fi
+
+# Status line — always emitted, easy to grep.
+if [ -d "$_GSTACK_HOME/.git" ] && [ "$_BRAIN_SYNC_MODE" != "off" ]; then
+ _BRAIN_QUEUE_DEPTH=0
+ [ -f "$_GSTACK_HOME/.brain-queue.jsonl" ] && _BRAIN_QUEUE_DEPTH=$(wc -l < "$_GSTACK_HOME/.brain-queue.jsonl" | tr -d ' ')
+ _BRAIN_LAST_PUSH="never"
+ [ -f "$_GSTACK_HOME/.brain-last-push" ] && _BRAIN_LAST_PUSH=$(cat "$_GSTACK_HOME/.brain-last-push" 2>/dev/null || echo never)
+ echo "BRAIN_SYNC: mode=$_BRAIN_SYNC_MODE | last_push=$_BRAIN_LAST_PUSH | queue=$_BRAIN_QUEUE_DEPTH"
+else
+ echo "BRAIN_SYNC: off"
+fi
+```
+
+
+
+**Privacy stop-gate (fires ONCE per machine).**
+
+If the bash output shows `BRAIN_SYNC: off` AND the config value
+`gbrain_sync_mode_prompted` is `false` AND gbrain is detected on this host
+(either `gbrain doctor --fast --json` succeeds or the `gbrain` binary is in PATH),
+fire a one-time privacy gate via AskUserQuestion:
+
+> gstack can publish your session memory (learnings, plans, designs, retros) to a
+> private GitHub repo that GBrain indexes across your machines. Higher tiers
+> include behavioral data (session timelines, developer profile). How much do you
+> want to sync?
+
+Options:
+- A) Everything allowlisted (recommended — maximum cross-machine memory)
+- B) Only artifacts (plans, designs, retros, learnings) — skip timelines and profile
+- C) Decline — keep everything local
+
+After the user answers, run (substituting the chosen value):
+
+```bash
+# Chosen mode: full | artifacts-only | off
+"$_BRAIN_CONFIG_BIN" set gbrain_sync_mode <choice>
+"$_BRAIN_CONFIG_BIN" set gbrain_sync_mode_prompted true
+```
+
+If A or B was chosen AND `~/.gstack/.git` doesn't exist, ask a follow-up:
+"Set up the GBrain sync repo now? (runs `gstack-brain-init`)"
+- A) Yes, run it now
+- B) Show me the command, I'll run it myself
+
+Do not block the skill. Emit the question, continue the skill workflow. The
+next skill run picks up wherever this left off.
+
+**At skill END (before the telemetry block),** run these bash commands to
+catch artifact writes (design docs, plans, retros) that skipped the writer
+shims, plus drain any still-pending queue entries:
+
+```bash
+"~/.claude/skills/gstack/bin/gstack-brain-sync" --discover-new 2>/dev/null || true
+"~/.claude/skills/gstack/bin/gstack-brain-sync" --once 2>/dev/null || true
+```
+
+
## Model-Specific Behavioral Patch (claude)
The following nudges are tuned for the claude model family. They are
diff --git a/design-review/SKILL.md b/design-review/SKILL.md
index 0c8c092cd..4536de637 100644
--- a/design-review/SKILL.md
+++ b/design-review/SKILL.md
@@ -355,6 +355,105 @@ AI orchestrator (e.g., OpenClaw). In spawned sessions:
- Focus on completing the task and reporting results via prose output.
- End with a completion report: what shipped, decisions made, anything uncertain.
+## GBrain Sync (skill start)
+
+```bash
+# gbrain-sync: drain pending writes, pull once per day. Silent no-op when
+# the feature isn't initialized or gbrain_sync_mode is "off". See
+# docs/gbrain-sync.md.
+
+_GSTACK_HOME="${GSTACK_HOME:-$HOME/.gstack}"
+_BRAIN_REMOTE_FILE="$HOME/.gstack-brain-remote.txt"
+_BRAIN_SYNC_BIN="~/.claude/skills/gstack/bin/gstack-brain-sync"
+_BRAIN_CONFIG_BIN="~/.claude/skills/gstack/bin/gstack-config"
+
+_BRAIN_SYNC_MODE=$("$_BRAIN_CONFIG_BIN" get gbrain_sync_mode 2>/dev/null || echo off)
+
+# New-machine hint: URL file present, local .git missing, sync not yet enabled.
+if [ -f "$_BRAIN_REMOTE_FILE" ] && [ ! -d "$_GSTACK_HOME/.git" ] && [ "$_BRAIN_SYNC_MODE" = "off" ]; then
+ _BRAIN_NEW_URL=$(head -1 "$_BRAIN_REMOTE_FILE" 2>/dev/null | tr -d '[:space:]')
+ if [ -n "$_BRAIN_NEW_URL" ]; then
+ echo "BRAIN_SYNC: brain repo detected: $_BRAIN_NEW_URL"
+ echo "BRAIN_SYNC: run 'gstack-brain-restore' to pull your cross-machine memory (or 'gstack-config set gbrain_sync_mode off' to dismiss forever)"
+ fi
+fi
+
+# Active-sync path.
+if [ -d "$_GSTACK_HOME/.git" ] && [ "$_BRAIN_SYNC_MODE" != "off" ]; then
+ # Once-per-day pull.
+ _BRAIN_LAST_PULL_FILE="$_GSTACK_HOME/.brain-last-pull"
+ _BRAIN_NOW=$(date +%s)
+ _BRAIN_DO_PULL=1
+ if [ -f "$_BRAIN_LAST_PULL_FILE" ]; then
+ _BRAIN_LAST=$(cat "$_BRAIN_LAST_PULL_FILE" 2>/dev/null || echo 0)
+ _BRAIN_AGE=$(( _BRAIN_NOW - _BRAIN_LAST ))
+ [ "$_BRAIN_AGE" -lt 86400 ] && _BRAIN_DO_PULL=0
+ fi
+ if [ "$_BRAIN_DO_PULL" = "1" ]; then
+ ( cd "$_GSTACK_HOME" && git fetch origin >/dev/null 2>&1 && git merge --ff-only "origin/$(git rev-parse --abbrev-ref HEAD)" >/dev/null 2>&1 ) || true
+ echo "$_BRAIN_NOW" > "$_BRAIN_LAST_PULL_FILE"
+ fi
+ # Drain pending queue, push.
+ "$_BRAIN_SYNC_BIN" --once 2>/dev/null || true
+fi
+
+# Status line — always emitted, easy to grep.
+if [ -d "$_GSTACK_HOME/.git" ] && [ "$_BRAIN_SYNC_MODE" != "off" ]; then
+ _BRAIN_QUEUE_DEPTH=0
+ [ -f "$_GSTACK_HOME/.brain-queue.jsonl" ] && _BRAIN_QUEUE_DEPTH=$(wc -l < "$_GSTACK_HOME/.brain-queue.jsonl" | tr -d ' ')
+ _BRAIN_LAST_PUSH="never"
+ [ -f "$_GSTACK_HOME/.brain-last-push" ] && _BRAIN_LAST_PUSH=$(cat "$_GSTACK_HOME/.brain-last-push" 2>/dev/null || echo never)
+ echo "BRAIN_SYNC: mode=$_BRAIN_SYNC_MODE | last_push=$_BRAIN_LAST_PUSH | queue=$_BRAIN_QUEUE_DEPTH"
+else
+ echo "BRAIN_SYNC: off"
+fi
+```
+
+
+
+**Privacy stop-gate (fires ONCE per machine).**
+
+If the bash output shows `BRAIN_SYNC: off` AND the config value
+`gbrain_sync_mode_prompted` is `false` AND gbrain is detected on this host
+(either `gbrain doctor --fast --json` succeeds or the `gbrain` binary is in PATH),
+fire a one-time privacy gate via AskUserQuestion:
+
+> gstack can publish your session memory (learnings, plans, designs, retros) to a
+> private GitHub repo that GBrain indexes across your machines. Higher tiers
+> include behavioral data (session timelines, developer profile). How much do you
+> want to sync?
+
+Options:
+- A) Everything allowlisted (recommended — maximum cross-machine memory)
+- B) Only artifacts (plans, designs, retros, learnings) — skip timelines and profile
+- C) Decline — keep everything local
+
+After the user answers, run (substituting the chosen value):
+
+```bash
+# Chosen mode: full | artifacts-only | off
+"$_BRAIN_CONFIG_BIN" set gbrain_sync_mode <choice>
+"$_BRAIN_CONFIG_BIN" set gbrain_sync_mode_prompted true
+```
+
+If A or B was chosen AND `~/.gstack/.git` doesn't exist, ask a follow-up:
+"Set up the GBrain sync repo now? (runs `gstack-brain-init`)"
+- A) Yes, run it now
+- B) Show me the command, I'll run it myself
+
+Do not block the skill. Emit the question, continue the skill workflow. The
+next skill run picks up wherever this left off.
+
+**At skill END (before the telemetry block),** run these bash commands to
+catch artifact writes (design docs, plans, retros) that skipped the writer
+shims, plus drain any still-pending queue entries:
+
+```bash
+"~/.claude/skills/gstack/bin/gstack-brain-sync" --discover-new 2>/dev/null || true
+"~/.claude/skills/gstack/bin/gstack-brain-sync" --once 2>/dev/null || true
+```
+
+
## Model-Specific Behavioral Patch (claude)
The following nudges are tuned for the claude model family. They are
diff --git a/design-shotgun/SKILL.md b/design-shotgun/SKILL.md
index e1e45f0e5..8553af418 100644
--- a/design-shotgun/SKILL.md
+++ b/design-shotgun/SKILL.md
@@ -352,6 +352,105 @@ AI orchestrator (e.g., OpenClaw). In spawned sessions:
- Focus on completing the task and reporting results via prose output.
- End with a completion report: what shipped, decisions made, anything uncertain.
+## GBrain Sync (skill start)
+
+```bash
+# gbrain-sync: drain pending writes, pull once per day. Silent no-op when
+# the feature isn't initialized or gbrain_sync_mode is "off". See
+# docs/gbrain-sync.md.
+
+_GSTACK_HOME="${GSTACK_HOME:-$HOME/.gstack}"
+_BRAIN_REMOTE_FILE="$HOME/.gstack-brain-remote.txt"
+_BRAIN_SYNC_BIN="~/.claude/skills/gstack/bin/gstack-brain-sync"
+_BRAIN_CONFIG_BIN="~/.claude/skills/gstack/bin/gstack-config"
+
+_BRAIN_SYNC_MODE=$("$_BRAIN_CONFIG_BIN" get gbrain_sync_mode 2>/dev/null || echo off)
+
+# New-machine hint: URL file present, local .git missing, sync not yet enabled.
+if [ -f "$_BRAIN_REMOTE_FILE" ] && [ ! -d "$_GSTACK_HOME/.git" ] && [ "$_BRAIN_SYNC_MODE" = "off" ]; then
+ _BRAIN_NEW_URL=$(head -1 "$_BRAIN_REMOTE_FILE" 2>/dev/null | tr -d '[:space:]')
+ if [ -n "$_BRAIN_NEW_URL" ]; then
+ echo "BRAIN_SYNC: brain repo detected: $_BRAIN_NEW_URL"
+ echo "BRAIN_SYNC: run 'gstack-brain-restore' to pull your cross-machine memory (or 'gstack-config set gbrain_sync_mode off' to dismiss forever)"
+ fi
+fi
+
+# Active-sync path.
+if [ -d "$_GSTACK_HOME/.git" ] && [ "$_BRAIN_SYNC_MODE" != "off" ]; then
+ # Once-per-day pull.
+ _BRAIN_LAST_PULL_FILE="$_GSTACK_HOME/.brain-last-pull"
+ _BRAIN_NOW=$(date +%s)
+ _BRAIN_DO_PULL=1
+ if [ -f "$_BRAIN_LAST_PULL_FILE" ]; then
+ _BRAIN_LAST=$(cat "$_BRAIN_LAST_PULL_FILE" 2>/dev/null || echo 0)
+ _BRAIN_AGE=$(( _BRAIN_NOW - _BRAIN_LAST ))
+ [ "$_BRAIN_AGE" -lt 86400 ] && _BRAIN_DO_PULL=0
+ fi
+ if [ "$_BRAIN_DO_PULL" = "1" ]; then
+ ( cd "$_GSTACK_HOME" && git fetch origin >/dev/null 2>&1 && git merge --ff-only "origin/$(git rev-parse --abbrev-ref HEAD)" >/dev/null 2>&1 ) || true
+ echo "$_BRAIN_NOW" > "$_BRAIN_LAST_PULL_FILE"
+ fi
+ # Drain pending queue, push.
+ "$_BRAIN_SYNC_BIN" --once 2>/dev/null || true
+fi
+
+# Status line — always emitted, easy to grep.
+if [ -d "$_GSTACK_HOME/.git" ] && [ "$_BRAIN_SYNC_MODE" != "off" ]; then
+ _BRAIN_QUEUE_DEPTH=0
+ [ -f "$_GSTACK_HOME/.brain-queue.jsonl" ] && _BRAIN_QUEUE_DEPTH=$(wc -l < "$_GSTACK_HOME/.brain-queue.jsonl" | tr -d ' ')
+ _BRAIN_LAST_PUSH="never"
+ [ -f "$_GSTACK_HOME/.brain-last-push" ] && _BRAIN_LAST_PUSH=$(cat "$_GSTACK_HOME/.brain-last-push" 2>/dev/null || echo never)
+ echo "BRAIN_SYNC: mode=$_BRAIN_SYNC_MODE | last_push=$_BRAIN_LAST_PUSH | queue=$_BRAIN_QUEUE_DEPTH"
+else
+ echo "BRAIN_SYNC: off"
+fi
+```
+
+
+
+**Privacy stop-gate (fires ONCE per machine).**
+
+If the bash output shows `BRAIN_SYNC: off` AND the config value
+`gbrain_sync_mode_prompted` is `false` AND gbrain is detected on this host
+(either `gbrain doctor --fast --json` succeeds or the `gbrain` binary is in PATH),
+fire a one-time privacy gate via AskUserQuestion:
+
+> gstack can publish your session memory (learnings, plans, designs, retros) to a
+> private GitHub repo that GBrain indexes across your machines. Higher tiers
+> include behavioral data (session timelines, developer profile). How much do you
+> want to sync?
+
+Options:
+- A) Everything allowlisted (recommended — maximum cross-machine memory)
+- B) Only artifacts (plans, designs, retros, learnings) — skip timelines and profile
+- C) Decline — keep everything local
+
+After the user answers, run (substituting the chosen value):
+
+```bash
+# Chosen mode: full | artifacts-only | off
+"$_BRAIN_CONFIG_BIN" set gbrain_sync_mode <choice>
+"$_BRAIN_CONFIG_BIN" set gbrain_sync_mode_prompted true
+```
+
+If A or B was chosen AND `~/.gstack/.git` doesn't exist, ask a follow-up:
+"Set up the GBrain sync repo now? (runs `gstack-brain-init`)"
+- A) Yes, run it now
+- B) Show me the command, I'll run it myself
+
+Do not block the skill. Emit the question, continue the skill workflow. The
+next skill run picks up wherever this left off.
+
+**At skill END (before the telemetry block),** run these bash commands to
+catch artifact writes (design docs, plans, retros) that skipped the writer
+shims, plus drain any still-pending queue entries:
+
+```bash
+"~/.claude/skills/gstack/bin/gstack-brain-sync" --discover-new 2>/dev/null || true
+"~/.claude/skills/gstack/bin/gstack-brain-sync" --once 2>/dev/null || true
+```
+
+
## Model-Specific Behavioral Patch (claude)
The following nudges are tuned for the claude model family. They are
diff --git a/devex-review/SKILL.md b/devex-review/SKILL.md
index 32054c001..7c4c12ead 100644
--- a/devex-review/SKILL.md
+++ b/devex-review/SKILL.md
@@ -355,6 +355,105 @@ AI orchestrator (e.g., OpenClaw). In spawned sessions:
- Focus on completing the task and reporting results via prose output.
- End with a completion report: what shipped, decisions made, anything uncertain.
+## GBrain Sync (skill start)
+
+```bash
+# gbrain-sync: drain pending writes, pull once per day. Silent no-op when
+# the feature isn't initialized or gbrain_sync_mode is "off". See
+# docs/gbrain-sync.md.
+
+_GSTACK_HOME="${GSTACK_HOME:-$HOME/.gstack}"
+_BRAIN_REMOTE_FILE="$HOME/.gstack-brain-remote.txt"
+_BRAIN_SYNC_BIN="~/.claude/skills/gstack/bin/gstack-brain-sync"
+_BRAIN_CONFIG_BIN="~/.claude/skills/gstack/bin/gstack-config"
+
+_BRAIN_SYNC_MODE=$("$_BRAIN_CONFIG_BIN" get gbrain_sync_mode 2>/dev/null || echo off)
+
+# New-machine hint: URL file present, local .git missing, sync not yet enabled.
+if [ -f "$_BRAIN_REMOTE_FILE" ] && [ ! -d "$_GSTACK_HOME/.git" ] && [ "$_BRAIN_SYNC_MODE" = "off" ]; then
+ _BRAIN_NEW_URL=$(head -1 "$_BRAIN_REMOTE_FILE" 2>/dev/null | tr -d '[:space:]')
+ if [ -n "$_BRAIN_NEW_URL" ]; then
+ echo "BRAIN_SYNC: brain repo detected: $_BRAIN_NEW_URL"
+ echo "BRAIN_SYNC: run 'gstack-brain-restore' to pull your cross-machine memory (or 'gstack-config set gbrain_sync_mode off' to dismiss forever)"
+ fi
+fi
+
+# Active-sync path.
+if [ -d "$_GSTACK_HOME/.git" ] && [ "$_BRAIN_SYNC_MODE" != "off" ]; then
+ # Once-per-day pull.
+ _BRAIN_LAST_PULL_FILE="$_GSTACK_HOME/.brain-last-pull"
+ _BRAIN_NOW=$(date +%s)
+ _BRAIN_DO_PULL=1
+ if [ -f "$_BRAIN_LAST_PULL_FILE" ]; then
+ _BRAIN_LAST=$(cat "$_BRAIN_LAST_PULL_FILE" 2>/dev/null || echo 0)
+ _BRAIN_AGE=$(( _BRAIN_NOW - _BRAIN_LAST ))
+ [ "$_BRAIN_AGE" -lt 86400 ] && _BRAIN_DO_PULL=0
+ fi
+ if [ "$_BRAIN_DO_PULL" = "1" ]; then
+ ( cd "$_GSTACK_HOME" && git fetch origin >/dev/null 2>&1 && git merge --ff-only "origin/$(git rev-parse --abbrev-ref HEAD)" >/dev/null 2>&1 ) || true
+ echo "$_BRAIN_NOW" > "$_BRAIN_LAST_PULL_FILE"
+ fi
+ # Drain pending queue, push.
+ "$_BRAIN_SYNC_BIN" --once 2>/dev/null || true
+fi
+
+# Status line — always emitted, easy to grep.
+if [ -d "$_GSTACK_HOME/.git" ] && [ "$_BRAIN_SYNC_MODE" != "off" ]; then
+ _BRAIN_QUEUE_DEPTH=0
+ [ -f "$_GSTACK_HOME/.brain-queue.jsonl" ] && _BRAIN_QUEUE_DEPTH=$(wc -l < "$_GSTACK_HOME/.brain-queue.jsonl" | tr -d ' ')
+ _BRAIN_LAST_PUSH="never"
+ [ -f "$_GSTACK_HOME/.brain-last-push" ] && _BRAIN_LAST_PUSH=$(cat "$_GSTACK_HOME/.brain-last-push" 2>/dev/null || echo never)
+ echo "BRAIN_SYNC: mode=$_BRAIN_SYNC_MODE | last_push=$_BRAIN_LAST_PUSH | queue=$_BRAIN_QUEUE_DEPTH"
+else
+ echo "BRAIN_SYNC: off"
+fi
+```
+
+
+
+**Privacy stop-gate (fires ONCE per machine).**
+
+If the bash output shows `BRAIN_SYNC: off` AND the config value
+`gbrain_sync_mode_prompted` is `false` AND gbrain is detected on this host
+(either `gbrain doctor --fast --json` succeeds or the `gbrain` binary is in PATH),
+fire a one-time privacy gate via AskUserQuestion:
+
+> gstack can publish your session memory (learnings, plans, designs, retros) to a
+> private GitHub repo that GBrain indexes across your machines. Higher tiers
+> include behavioral data (session timelines, developer profile). How much do you
+> want to sync?
+
+Options:
+- A) Everything allowlisted (recommended — maximum cross-machine memory)
+- B) Only artifacts (plans, designs, retros, learnings) — skip timelines and profile
+- C) Decline — keep everything local
+
+After the user answers, run (substituting the chosen value):
+
+```bash
+# Chosen mode: full | artifacts-only | off
+"$_BRAIN_CONFIG_BIN" set gbrain_sync_mode <choice>
+"$_BRAIN_CONFIG_BIN" set gbrain_sync_mode_prompted true
+```
+
+If A or B was chosen AND `~/.gstack/.git` doesn't exist, ask a follow-up:
+"Set up the GBrain sync repo now? (runs `gstack-brain-init`)"
+- A) Yes, run it now
+- B) Show me the command, I'll run it myself
+
+Do not block the skill. Emit the question, continue the skill workflow. The
+next skill run picks up wherever this left off.
+
+**At skill END (before the telemetry block),** run these bash commands to
+catch artifact writes (design docs, plans, retros) that skipped the writer
+shims, plus drain any still-pending queue entries:
+
+```bash
+"~/.claude/skills/gstack/bin/gstack-brain-sync" --discover-new 2>/dev/null || true
+"~/.claude/skills/gstack/bin/gstack-brain-sync" --once 2>/dev/null || true
+```
+
+
## Model-Specific Behavioral Patch (claude)
The following nudges are tuned for the claude model family. They are
diff --git a/docs/gbrain-sync-errors.md b/docs/gbrain-sync-errors.md
new file mode 100644
index 000000000..52120a8b2
--- /dev/null
+++ b/docs/gbrain-sync-errors.md
@@ -0,0 +1,214 @@
+# gbrain-sync error lookup
+
+Every error message `gstack-brain-*` can print, with problem, cause, and fix.
+
+Search this file by the prefix after `BRAIN_SYNC:` or by the binary name in
+the command output.
+
+---
+
+## `BRAIN_SYNC: brain repo detected: <url>`
+
+**Problem.** You're on a machine that has `~/.gstack-brain-remote.txt` (copied
+from another machine) but no local git repo at `~/.gstack/.git`.
+
+**Cause.** You've set up GBrain sync elsewhere and your gstack hasn't been
+restored on this machine yet.
+
+**Fix.**
+```bash
+gstack-brain-restore
+```
+This pulls the repo into `~/.gstack/` and re-registers merge drivers.
+
+If you don't want to restore here, dismiss the hint with:
+```bash
+gstack-config set gbrain_sync_mode_prompted true
+```
+
+---
+
+## `BRAIN_SYNC: blocked: <pattern-family>:<snippet>`
+
+**Problem.** Sync stopped because the secret scanner detected credential-shaped
+content in a staged file. The queue is preserved; nothing was pushed.
+
+**Cause.** One of the pre-commit secret patterns matched the file contents —
+likely an AWS key, GitHub token, OpenAI key, PEM block, JWT, or bearer token
+embedded in JSON.
+
+**Fix (three options).**
+
+1. **If it's a real secret**: edit the offending file to remove the secret,
+ then re-run any skill to retry sync.
+
+2. **If the pattern is a false positive** (e.g., your learning contains a
+ GitHub token pattern in an example string that you *want* to publish):
+ ```bash
+ gstack-brain-sync --skip-file <path>
+ ```
+ This permanently excludes the path from future syncs.
+
+3. **If you want to abandon this sync batch entirely** (start fresh):
+ ```bash
+ gstack-brain-sync --drop-queue --yes
+ ```
+ This clears the queue without committing. Future writes will re-populate
+ it normally.
+
+---
+
+## `BRAIN_SYNC: push failed: auth.`
+
+**Problem.** Git push was rejected because your auth with the remote expired
+or is missing.
+
+**Cause.** The remote is unreachable with current credentials.
+
+**Fix.** Refresh auth based on your remote:
+
+- **GitHub**: `gh auth status` (then `gh auth refresh` if needed)
+- **GitLab**: `glab auth status`
+- **Other**: `git remote -v` + check SSH keys or credential helper
+
+After fixing auth, run any skill to retry sync automatically.
+
+---
+
+## `BRAIN_SYNC: push failed: <first-line-of-error>`
+
+**Problem.** Push failed for a reason other than auth. The first line of
+git's error appears after the colon.
+
+**Cause.** Could be network issue, rejected push (remote ahead), server 500,
+or repo access revoked.
+
+**Fix.** Look at `~/.gstack/.brain-sync-status.json` for more detail, or run:
+```bash
+cd ~/.gstack && git status && git push origin HEAD
+```
+to see git's full error. The queue is cleared after any push attempt, but
+your local commit still exists — the next skill run will retry the push.
+
+---
+
+## `gstack-brain-init: ~/.gstack/.git is already a git repo pointing at <url>`
+
+**Problem.** You tried to init with a remote URL that doesn't match the
+existing one.
+
+**Cause.** You already ran `gstack-brain-init` with a different remote.
+
+**Fix.** Either:
+
+- Use the existing remote: run `gstack-brain-init` without `--remote`, or
+ with the matching URL.
+- Switch remotes: `gstack-brain-uninstall` first, then re-init with the new
+ URL. This does not delete your data.
+
+---
+
+## `Remote not reachable: <url>`
+
+**Problem.** Init couldn't reach the git remote to verify connectivity.
+
+**Cause.** Wrong URL, missing auth, network issue.
+
+**Fix.** Test manually:
+```bash
+git ls-remote <url>
+```
+If that fails, check:
+- URL spelling
+- GitHub: `gh auth status`
+- GitLab: `glab auth status`
+- Private network / VPN / DNS
+
+---
+
+## `gstack-brain-init: failed to create or find '<name>'`
+
+**Problem.** Auto-repo-creation via `gh repo create` failed and the repo
+isn't discoverable via `gh repo view` either.
+
+**Cause.** `gh` is unauthenticated, a repo with that name already exists
+owned by someone else, or your GitHub account hit a quota.
+
+**Fix.**
+```bash
+gh auth status
+```
+If unauth'd, run `gh auth login`. If the repo name collides, pass a different
+name:
+```bash
+gstack-brain-init --remote [email protected]:YOURUSER/custom-name.git
+```
+
+---
+
+## `gstack-brain-restore: ~/.gstack/.git already points at <url>`
+
+**Problem.** You tried to restore from a URL that doesn't match the existing
+git config.
+
+**Cause.** Stale `.git` from a previous init with a different remote.
+
+**Fix.** `gstack-brain-uninstall`, then re-run `gstack-brain-restore <url>`.
+
+---
+
+## `gstack-brain-restore: ~/.gstack/ has existing allowlisted files that would be clobbered`
+
+**Problem.** You're trying to restore, but `~/.gstack/` already contains
+learnings or plans that would be overwritten.
+
+**Cause.** Either (a) this machine has accumulated state from a pre-sync
+gstack session, or (b) a previous failed restore left partial state.
+
+**Fix (three options).**
+
+1. **If this machine's state should become the new truth**: run
+ `gstack-brain-init` instead of restore — this creates a brand-new brain
+ repo from this machine's state.
+
+2. **If you want to adopt the remote and discard this machine's state**:
+ back up `~/.gstack/projects/` first, then remove the offending files and
+ re-run restore.
+
+3. **If you want to merge**: there's no automatic merge for this. Manually
+ copy learnings from `~/.gstack/` into your running gstack on a machine
+ with sync already on, then restore here.
+
+---
+
+## `gstack-brain-restore: <url> does not look like a gstack-brain repo`
+
+**Problem.** The clone succeeded but the repo is missing `.brain-allowlist`
+and `.gitattributes`.
+
+**Cause.** You pointed restore at a random git repo, or someone deleted the
+canonical config files from the brain repo.
+
+**Fix.** Verify the URL. If it's correct, run `gstack-brain-init --remote
+<url>` to re-seed the canonical config.
+
+---
+
+## Nothing is syncing but I expect it to
+
+**Not an error, but a common gotcha.** Check in order:
+
+1. `gstack-brain-sync --status` — is mode `off`?
+2. `~/.gstack/.git` exists?
+3. `gstack-config get gbrain_sync_mode` — should be `full` or `artifacts-only`.
+4. The file you expect to sync — is it in the allowlist?
+ `cat ~/.gstack/.brain-allowlist`
+5. Privacy class filter — if mode is `artifacts-only`, behavioral files
+ (timelines, developer-profile) are intentionally skipped.
+
+If all those look right, run:
+```bash
+gstack-brain-sync --discover-new
+gstack-brain-sync --once
+```
+to force a drain.
diff --git a/docs/gbrain-sync.md b/docs/gbrain-sync.md
new file mode 100644
index 000000000..02e9dd4c1
--- /dev/null
+++ b/docs/gbrain-sync.md
@@ -0,0 +1,188 @@
+# Cross-machine memory with GBrain sync
+
+gstack writes a lot of useful state to `~/.gstack/` — learnings, retros, CEO
+plans, design docs, developer profile. By default, all of that dies when you
+switch laptops. **GBrain sync** pushes a curated subset to a private git
+repo so your memory follows you across machines and becomes indexable by
+GBrain.
+
+## What you get
+
+- Work on machine A, pick up seamlessly on machine B.
+- Your learnings, plans, and designs are visible in GBrain (if you use it).
+- A clean off-ramp (`gstack-brain-uninstall`) that never touches your data.
+- No daemon, no system service, no background process.
+
+## What does NOT leave your machine
+
+By design, these stay local even when sync is on:
+
+- Credentials: `.auth.json`, `auth-token.json`, `sidebar-sessions/`,
+ `security/device-salt`, consumer tokens in `config.yaml`
+- Machine-specific state: Chromium profiles, ONNX model weights,
+ caches, eval-cache, CDP-profile, one-time prompt markers
+ (`.welcome-seen`, `.telemetry-prompted`, `.vendoring-warned-*`, etc.)
+- Question-preferences: per-machine UX preferences
+ (`question-preferences.json`, `question-log.jsonl`, `question-events.jsonl`).
+
+The exact allowlist lives in `~/.gstack/.brain-allowlist`. The CLI manages
+it; you can append your own entries below the marker line.
+
+## First-run setup (30–90 seconds)
+
+```bash
+gstack-brain-init
+```
+
+The command:
+
+1. Turns `~/.gstack/` into a git repo.
+2. Asks for a remote URL (default: `gh repo create --private
+ gstack-brain-$USER`). Any git remote works — GitHub, GitLab, Gitea,
+ self-hosted.
+3. Pushes an initial commit with just the config.
+4. Writes `~/.gstack-brain-remote.txt` (URL-only, no secrets —
+ safe to copy to another machine).
+5. Registers GBrain as a reader if `GBRAIN_URL` + `GBRAIN_TOKEN` are
+ configured. Otherwise you can add readers later with
+ `gstack-brain-reader add <name> --ingest-url <url> --token <token>`.
+
+After init, the **next skill you run** will ask you ONE question about
+privacy mode:
+
+- **Everything allowlisted (recommended)**: learnings, reviews, plans,
+ designs, retros, timelines, and developer profile all sync.
+- **Only artifacts**: plans, designs, retros, learnings — skip
+ behavioral data (timelines, developer profile).
+- **Decline**: keep everything local. You can turn sync on later with
+ `gstack-config set gbrain_sync_mode full`.
+
+Your answer is persisted. You won't be asked again.
+
+## Cross-machine workflow
+
+On machine A: run `gstack-brain-init` once. That's it — every skill
+invocation now drains the sync queue at its start and end boundaries
+(~200–800 ms network pause per skill).
+
+On machine B:
+
+1. Copy `~/.gstack-brain-remote.txt` from machine A to machine B
+ (password manager, dotfile repo, USB stick — your call).
+2. Run any gstack skill. The preamble sees the URL file and prints:
+ ```
+ BRAIN_SYNC: brain repo detected: <url>
+ BRAIN_SYNC: run 'gstack-brain-restore' to pull your cross-machine memory
+ ```
+3. Run `gstack-brain-restore`. That clones the repo, rehydrates your
+ learnings/plans/retros, and re-registers the git merge drivers.
+4. Re-enter consumer tokens (they're machine-local and NOT synced —
+ `gstack-config set gbrain_token <your-token>`).
+5. Next skill: your yesterday-on-machine-A learning surfaces. That's the
+ magical moment.
+
+## Status, health, and queue depth
+
+```bash
+gstack-brain-sync --status
+```
+
+Shows: last successful push, pending queue depth, any sync blocks, and the
+current privacy mode.
+
+Every skill run prints a `BRAIN_SYNC:` line near the top of the preamble
+output. Scan it for problems.
+
+## Privacy modes in detail
+
+| Mode | What syncs |
+|------|------------|
+| `off` | Nothing (default). |
+| `artifacts-only` | Plans, designs, retros, learnings, reviews. Skips timelines + developer-profile. |
+| `full` | Everything in the allowlist, including behavioral state. |
+
+Change anytime with:
+```bash
+gstack-config set gbrain_sync_mode full
+gstack-config set gbrain_sync_mode off
+```
+
+## Secret protection
+
+Every commit is scanned for credential-shaped content before it leaves
+your machine. Blocked patterns include:
+
+- AWS access keys (`AKIA…`)
+- GitHub tokens (`ghp_`, `gho_`, `ghu_`, `ghs_`, `ghr_`, `github_pat_`)
+- OpenAI keys (`sk-…`)
+- PEM blocks (`-----BEGIN …-----`)
+- JWTs (`eyJ…`)
+- Bearer tokens in JSON (`"authorization": "…"`, `"api_key": "…"`, etc.)
+
+If a scan hits, sync stops, the queue is preserved, and your preamble
+prints:
+
+```
+BRAIN_SYNC: blocked: <pattern-family>:<snippet>
+```
+
+To remediate:
+
+1. Review the offending file.
+2. If the match is a false positive on content you explicitly want to
+ sync, run `gstack-brain-sync --skip-file <path>` to permanently
+ exclude that path.
+3. Otherwise, edit the file to remove the secret and re-run any skill.
+
+There's a defense-in-depth hook at `~/.gstack/.git/hooks/pre-commit` that
+runs the same scan if you manually `git commit` against the repo.
+
+## Two-machine conflicts
+
+If you write on machine A and machine B the same day, both will push
+append commits. Git's default would conflict at the file tail, but the
+`.jsonl` and markdown files are registered with custom merge drivers:
+
+- JSONL files use a sort-and-dedup driver that orders appends by ISO
+ timestamp (falls back to SHA-256 hash of each line for determinism).
+- Markdown artifacts (retros, plans, designs) use a union merge driver
+ that concatenates both sides.
+
+You shouldn't see conflict prompts. If you do (a real semantic conflict,
+like two machines editing the same plan), git will stop and prompt.
+
+## Cross-machine pull cadence
+
+The preamble runs `git fetch` + `git merge --ff-only` once per 24 hours
+(cached via `~/.gstack/.brain-last-pull`). You don't need to think about
+this — it happens automatically at the first skill invocation each day.
+
+## Uninstall
+
+```bash
+gstack-brain-uninstall
+```
+
+This:
+
+- Removes `~/.gstack/.git/` and all `.brain-*` config files.
+- Clears `gbrain_sync_mode` in `gstack-config`.
+- Does NOT touch your learnings, plans, retros, or developer profile.
+
+Add `--delete-remote` to also delete the private GitHub repo (GitHub only,
+uses `gh repo delete`).
+
+Re-init anytime with `gstack-brain-init`.
+
+## Troubleshooting
+
+See [gbrain-sync-errors.md](gbrain-sync-errors.md) for an index of every
+error message gstack-brain may print, with problem / cause / fix for each.
+
+## Under the hood
+
+For the architectural decisions behind this feature (allowlist vs
+denylist, daemon vs preamble-boundary sync, JSONL merge driver, privacy
+stop-gate), see the
+[approved plan](../system-instruction-you-are-working-jaunty-kahn.md) in
+the gstack plans directory.
diff --git a/document-release/SKILL.md b/document-release/SKILL.md
index 589c495c6..711e10c38 100644
--- a/document-release/SKILL.md
+++ b/document-release/SKILL.md
@@ -352,6 +352,105 @@ AI orchestrator (e.g., OpenClaw). In spawned sessions:
- Focus on completing the task and reporting results via prose output.
- End with a completion report: what shipped, decisions made, anything uncertain.
+## GBrain Sync (skill start)
+
+```bash
+# gbrain-sync: drain pending writes, pull once per day. Silent no-op when
+# the feature isn't initialized or gbrain_sync_mode is "off". See
+# docs/gbrain-sync.md.
+
+_GSTACK_HOME="${GSTACK_HOME:-$HOME/.gstack}"
+_BRAIN_REMOTE_FILE="$HOME/.gstack-brain-remote.txt"
+_BRAIN_SYNC_BIN="~/.claude/skills/gstack/bin/gstack-brain-sync"
+_BRAIN_CONFIG_BIN="~/.claude/skills/gstack/bin/gstack-config"
+
+_BRAIN_SYNC_MODE=$("$_BRAIN_CONFIG_BIN" get gbrain_sync_mode 2>/dev/null || echo off)
+
+# New-machine hint: URL file present, local .git missing, sync not yet enabled.
+if [ -f "$_BRAIN_REMOTE_FILE" ] && [ ! -d "$_GSTACK_HOME/.git" ] && [ "$_BRAIN_SYNC_MODE" = "off" ]; then
+ _BRAIN_NEW_URL=$(head -1 "$_BRAIN_REMOTE_FILE" 2>/dev/null | tr -d '[:space:]')
+ if [ -n "$_BRAIN_NEW_URL" ]; then
+ echo "BRAIN_SYNC: brain repo detected: $_BRAIN_NEW_URL"
+ echo "BRAIN_SYNC: run 'gstack-brain-restore' to pull your cross-machine memory (or 'gstack-config set gbrain_sync_mode off' to dismiss forever)"
+ fi
+fi
+
+# Active-sync path.
+if [ -d "$_GSTACK_HOME/.git" ] && [ "$_BRAIN_SYNC_MODE" != "off" ]; then
+ # Once-per-day pull.
+ _BRAIN_LAST_PULL_FILE="$_GSTACK_HOME/.brain-last-pull"
+ _BRAIN_NOW=$(date +%s)
+ _BRAIN_DO_PULL=1
+ if [ -f "$_BRAIN_LAST_PULL_FILE" ]; then
+ _BRAIN_LAST=$(cat "$_BRAIN_LAST_PULL_FILE" 2>/dev/null || echo 0)
+ _BRAIN_AGE=$(( _BRAIN_NOW - _BRAIN_LAST ))
+ [ "$_BRAIN_AGE" -lt 86400 ] && _BRAIN_DO_PULL=0
+ fi
+ if [ "$_BRAIN_DO_PULL" = "1" ]; then
+ ( cd "$_GSTACK_HOME" && git fetch origin >/dev/null 2>&1 && git merge --ff-only "origin/$(git rev-parse --abbrev-ref HEAD)" >/dev/null 2>&1 ) || true
+ echo "$_BRAIN_NOW" > "$_BRAIN_LAST_PULL_FILE"
+ fi
+ # Drain pending queue, push.
+ "$_BRAIN_SYNC_BIN" --once 2>/dev/null || true
+fi
+
+# Status line — always emitted, easy to grep.
+if [ -d "$_GSTACK_HOME/.git" ] && [ "$_BRAIN_SYNC_MODE" != "off" ]; then
+ _BRAIN_QUEUE_DEPTH=0
+ [ -f "$_GSTACK_HOME/.brain-queue.jsonl" ] && _BRAIN_QUEUE_DEPTH=$(wc -l < "$_GSTACK_HOME/.brain-queue.jsonl" | tr -d ' ')
+ _BRAIN_LAST_PUSH="never"
+ [ -f "$_GSTACK_HOME/.brain-last-push" ] && _BRAIN_LAST_PUSH=$(cat "$_GSTACK_HOME/.brain-last-push" 2>/dev/null || echo never)
+ echo "BRAIN_SYNC: mode=$_BRAIN_SYNC_MODE | last_push=$_BRAIN_LAST_PUSH | queue=$_BRAIN_QUEUE_DEPTH"
+else
+ echo "BRAIN_SYNC: off"
+fi
+```
+
+
+
+**Privacy stop-gate (fires ONCE per machine).**
+
+If the bash output shows `BRAIN_SYNC: off` AND the config value
+`gbrain_sync_mode_prompted` is `false` AND gbrain is detected on this host
+(either `gbrain doctor --fast --json` succeeds or the `gbrain` binary is in PATH),
+fire a one-time privacy gate via AskUserQuestion:
+
+> gstack can publish your session memory (learnings, plans, designs, retros) to a
+> private GitHub repo that GBrain indexes across your machines. Higher tiers
+> include behavioral data (session timelines, developer profile). How much do you
+> want to sync?
+
+Options:
+- A) Everything allowlisted (recommended — maximum cross-machine memory)
+- B) Only artifacts (plans, designs, retros, learnings) — skip timelines and profile
+- C) Decline — keep everything local
+
+After the user answers, run (substituting the chosen value):
+
+```bash
+# Chosen mode: full | artifacts-only | off
+"$_BRAIN_CONFIG_BIN" set gbrain_sync_mode <choice>
+"$_BRAIN_CONFIG_BIN" set gbrain_sync_mode_prompted true
+```
+
+If A or B was chosen AND `~/.gstack/.git` doesn't exist, ask a follow-up:
+"Set up the GBrain sync repo now? (runs `gstack-brain-init`)"
+- A) Yes, run it now
+- B) Show me the command, I'll run it myself
+
+Do not block the skill. Emit the question, continue the skill workflow. The
+next skill run picks up wherever this left off.
+
+**At skill END (before the telemetry block),** run these bash commands to
+catch artifact writes (design docs, plans, retros) that skipped the writer
+shims, plus drain any still-pending queue entries:
+
+```bash
+"~/.claude/skills/gstack/bin/gstack-brain-sync" --discover-new 2>/dev/null || true
+"~/.claude/skills/gstack/bin/gstack-brain-sync" --once 2>/dev/null || true
+```
+
+
## Model-Specific Behavioral Patch (claude)
The following nudges are tuned for the claude model family. They are
diff --git a/health/SKILL.md b/health/SKILL.md
index 4027c31af..3fabd025a 100644
--- a/health/SKILL.md
+++ b/health/SKILL.md
@@ -352,6 +352,105 @@ AI orchestrator (e.g., OpenClaw). In spawned sessions:
- Focus on completing the task and reporting results via prose output.
- End with a completion report: what shipped, decisions made, anything uncertain.
+## GBrain Sync (skill start)
+
+```bash
+# gbrain-sync: drain pending writes, pull once per day. Silent no-op when
+# the feature isn't initialized or gbrain_sync_mode is "off". See
+# docs/gbrain-sync.md.
+
+_GSTACK_HOME="${GSTACK_HOME:-$HOME/.gstack}"
+_BRAIN_REMOTE_FILE="$HOME/.gstack-brain-remote.txt"
+_BRAIN_SYNC_BIN="~/.claude/skills/gstack/bin/gstack-brain-sync"
+_BRAIN_CONFIG_BIN="~/.claude/skills/gstack/bin/gstack-config"
+
+_BRAIN_SYNC_MODE=$("$_BRAIN_CONFIG_BIN" get gbrain_sync_mode 2>/dev/null || echo off)
+
+# New-machine hint: URL file present, local .git missing, sync not yet enabled.
+if [ -f "$_BRAIN_REMOTE_FILE" ] && [ ! -d "$_GSTACK_HOME/.git" ] && [ "$_BRAIN_SYNC_MODE" = "off" ]; then
+ _BRAIN_NEW_URL=$(head -1 "$_BRAIN_REMOTE_FILE" 2>/dev/null | tr -d '[:space:]')
+ if [ -n "$_BRAIN_NEW_URL" ]; then
+ echo "BRAIN_SYNC: brain repo detected: $_BRAIN_NEW_URL"
+ echo "BRAIN_SYNC: run 'gstack-brain-restore' to pull your cross-machine memory (or 'gstack-config set gbrain_sync_mode off' to dismiss forever)"
+ fi
+fi
+
+# Active-sync path.
+if [ -d "$_GSTACK_HOME/.git" ] && [ "$_BRAIN_SYNC_MODE" != "off" ]; then
+ # Once-per-day pull.
+ _BRAIN_LAST_PULL_FILE="$_GSTACK_HOME/.brain-last-pull"
+ _BRAIN_NOW=$(date +%s)
+ _BRAIN_DO_PULL=1
+ if [ -f "$_BRAIN_LAST_PULL_FILE" ]; then
+ _BRAIN_LAST=$(cat "$_BRAIN_LAST_PULL_FILE" 2>/dev/null || echo 0)
+ _BRAIN_AGE=$(( _BRAIN_NOW - _BRAIN_LAST ))
+ [ "$_BRAIN_AGE" -lt 86400 ] && _BRAIN_DO_PULL=0
+ fi
+ if [ "$_BRAIN_DO_PULL" = "1" ]; then
+ ( cd "$_GSTACK_HOME" && git fetch origin >/dev/null 2>&1 && git merge --ff-only "origin/$(git rev-parse --abbrev-ref HEAD)" >/dev/null 2>&1 ) || true
+ echo "$_BRAIN_NOW" > "$_BRAIN_LAST_PULL_FILE"
+ fi
+ # Drain pending queue, push.
+ "$_BRAIN_SYNC_BIN" --once 2>/dev/null || true
+fi
+
+# Status line — always emitted, easy to grep.
+if [ -d "$_GSTACK_HOME/.git" ] && [ "$_BRAIN_SYNC_MODE" != "off" ]; then
+ _BRAIN_QUEUE_DEPTH=0
+ [ -f "$_GSTACK_HOME/.brain-queue.jsonl" ] && _BRAIN_QUEUE_DEPTH=$(wc -l < "$_GSTACK_HOME/.brain-queue.jsonl" | tr -d ' ')
+ _BRAIN_LAST_PUSH="never"
+ [ -f "$_GSTACK_HOME/.brain-last-push" ] && _BRAIN_LAST_PUSH=$(cat "$_GSTACK_HOME/.brain-last-push" 2>/dev/null || echo never)
+ echo "BRAIN_SYNC: mode=$_BRAIN_SYNC_MODE | last_push=$_BRAIN_LAST_PUSH | queue=$_BRAIN_QUEUE_DEPTH"
+else
+ echo "BRAIN_SYNC: off"
+fi
+```
+
+
+
+**Privacy stop-gate (fires ONCE per machine).**
+
+If the bash output shows `BRAIN_SYNC: off` AND the config value
+`gbrain_sync_mode_prompted` is `false` AND gbrain is detected on this host
+(either `gbrain doctor --fast --json` succeeds or the `gbrain` binary is in PATH),
+fire a one-time privacy gate via AskUserQuestion:
+
+> gstack can publish your session memory (learnings, plans, designs, retros) to a
+> private GitHub repo that GBrain indexes across your machines. Higher tiers
+> include behavioral data (session timelines, developer profile). How much do you
+> want to sync?
+
+Options:
+- A) Everything allowlisted (recommended — maximum cross-machine memory)
+- B) Only artifacts (plans, designs, retros, learnings) — skip timelines and profile
+- C) Decline — keep everything local
+
+After the user answers, run (substituting the chosen value):
+
+```bash
+# Chosen mode: full | artifacts-only | off
+"$_BRAIN_CONFIG_BIN" set gbrain_sync_mode <choice>
+"$_BRAIN_CONFIG_BIN" set gbrain_sync_mode_prompted true
+```
+
+If A or B was chosen AND `~/.gstack/.git` doesn't exist, ask a follow-up:
+"Set up the GBrain sync repo now? (runs `gstack-brain-init`)"
+- A) Yes, run it now
+- B) Show me the command, I'll run it myself
+
+Do not block the skill. Emit the question, continue the skill workflow. The
+next skill run picks up wherever this left off.
+
+**At skill END (before the telemetry block),** run these bash commands to
+catch artifact writes (design docs, plans, retros) that skipped the writer
+shims, plus drain any still-pending queue entries:
+
+```bash
+"~/.claude/skills/gstack/bin/gstack-brain-sync" --discover-new 2>/dev/null || true
+"~/.claude/skills/gstack/bin/gstack-brain-sync" --once 2>/dev/null || true
+```
+
+
## Model-Specific Behavioral Patch (claude)
The following nudges are tuned for the claude model family. They are
diff --git a/investigate/SKILL.md b/investigate/SKILL.md
index 844605484..f1c974c70 100644
--- a/investigate/SKILL.md
+++ b/investigate/SKILL.md
@@ -369,6 +369,105 @@ AI orchestrator (e.g., OpenClaw). In spawned sessions:
- Focus on completing the task and reporting results via prose output.
- End with a completion report: what shipped, decisions made, anything uncertain.
+## GBrain Sync (skill start)
+
+```bash
+# gbrain-sync: drain pending writes, pull once per day. Silent no-op when
+# the feature isn't initialized or gbrain_sync_mode is "off". See
+# docs/gbrain-sync.md.
+
+_GSTACK_HOME="${GSTACK_HOME:-$HOME/.gstack}"
+_BRAIN_REMOTE_FILE="$HOME/.gstack-brain-remote.txt"
+_BRAIN_SYNC_BIN="~/.claude/skills/gstack/bin/gstack-brain-sync"
+_BRAIN_CONFIG_BIN="~/.claude/skills/gstack/bin/gstack-config"
+
+_BRAIN_SYNC_MODE=$("$_BRAIN_CONFIG_BIN" get gbrain_sync_mode 2>/dev/null || echo off)
+
+# New-machine hint: URL file present, local .git missing, sync not yet enabled.
+if [ -f "$_BRAIN_REMOTE_FILE" ] && [ ! -d "$_GSTACK_HOME/.git" ] && [ "$_BRAIN_SYNC_MODE" = "off" ]; then
+ _BRAIN_NEW_URL=$(head -1 "$_BRAIN_REMOTE_FILE" 2>/dev/null | tr -d '[:space:]')
+ if [ -n "$_BRAIN_NEW_URL" ]; then
+ echo "BRAIN_SYNC: brain repo detected: $_BRAIN_NEW_URL"
+ echo "BRAIN_SYNC: run 'gstack-brain-restore' to pull your cross-machine memory (or 'gstack-config set gbrain_sync_mode off' to dismiss forever)"
+ fi
+fi
+
+# Active-sync path.
+if [ -d "$_GSTACK_HOME/.git" ] && [ "$_BRAIN_SYNC_MODE" != "off" ]; then
+ # Once-per-day pull.
+ _BRAIN_LAST_PULL_FILE="$_GSTACK_HOME/.brain-last-pull"
+ _BRAIN_NOW=$(date +%s)
+ _BRAIN_DO_PULL=1
+ if [ -f "$_BRAIN_LAST_PULL_FILE" ]; then
+ _BRAIN_LAST=$(cat "$_BRAIN_LAST_PULL_FILE" 2>/dev/null || echo 0)
+ _BRAIN_AGE=$(( _BRAIN_NOW - _BRAIN_LAST ))
+ [ "$_BRAIN_AGE" -lt 86400 ] && _BRAIN_DO_PULL=0
+ fi
+ if [ "$_BRAIN_DO_PULL" = "1" ]; then
+ ( cd "$_GSTACK_HOME" && git fetch origin >/dev/null 2>&1 && git merge --ff-only "origin/$(git rev-parse --abbrev-ref HEAD)" >/dev/null 2>&1 ) || true
+ echo "$_BRAIN_NOW" > "$_BRAIN_LAST_PULL_FILE"
+ fi
+ # Drain pending queue, push.
+ "$_BRAIN_SYNC_BIN" --once 2>/dev/null || true
+fi
+
+# Status line — always emitted, easy to grep.
+if [ -d "$_GSTACK_HOME/.git" ] && [ "$_BRAIN_SYNC_MODE" != "off" ]; then
+ _BRAIN_QUEUE_DEPTH=0
+ [ -f "$_GSTACK_HOME/.brain-queue.jsonl" ] && _BRAIN_QUEUE_DEPTH=$(wc -l < "$_GSTACK_HOME/.brain-queue.jsonl" | tr -d ' ')
+ _BRAIN_LAST_PUSH="never"
+ [ -f "$_GSTACK_HOME/.brain-last-push" ] && _BRAIN_LAST_PUSH=$(cat "$_GSTACK_HOME/.brain-last-push" 2>/dev/null || echo never)
+ echo "BRAIN_SYNC: mode=$_BRAIN_SYNC_MODE | last_push=$_BRAIN_LAST_PUSH | queue=$_BRAIN_QUEUE_DEPTH"
+else
+ echo "BRAIN_SYNC: off"
+fi
+```
+
+
+
+**Privacy stop-gate (fires ONCE per machine).**
+
+If the bash output shows `BRAIN_SYNC: off` AND the config value
+`gbrain_sync_mode_prompted` is `false` AND gbrain is detected on this host
+(either `gbrain doctor --fast --json` succeeds or the `gbrain` binary is in PATH),
+fire a one-time privacy gate via AskUserQuestion:
+
+> gstack can publish your session memory (learnings, plans, designs, retros) to a
+> private GitHub repo that GBrain indexes across your machines. Higher tiers
+> include behavioral data (session timelines, developer profile). How much do you
+> want to sync?
+
+Options:
+- A) Everything allowlisted (recommended — maximum cross-machine memory)
+- B) Only artifacts (plans, designs, retros, learnings) — skip timelines and profile
+- C) Decline — keep everything local
+
+After the user answers, run (substituting the chosen value):
+
+```bash
+# Chosen mode: full | artifacts-only | off
+"$_BRAIN_CONFIG_BIN" set gbrain_sync_mode <choice>
+"$_BRAIN_CONFIG_BIN" set gbrain_sync_mode_prompted true
+```
+
+If A or B was chosen AND `~/.gstack/.git` doesn't exist, ask a follow-up:
+"Set up the GBrain sync repo now? (runs `gstack-brain-init`)"
+- A) Yes, run it now
+- B) Show me the command, I'll run it myself
+
+Do not block the skill. Emit the question, continue the skill workflow. The
+next skill run picks up wherever this left off.
+
+**At skill END (before the telemetry block),** run these bash commands to
+catch artifact writes (design docs, plans, retros) that skipped the writer
+shims, plus drain any still-pending queue entries:
+
+```bash
+"~/.claude/skills/gstack/bin/gstack-brain-sync" --discover-new 2>/dev/null || true
+"~/.claude/skills/gstack/bin/gstack-brain-sync" --once 2>/dev/null || true
+```
+
+
## Model-Specific Behavioral Patch (claude)
The following nudges are tuned for the claude model family. They are
diff --git a/land-and-deploy/SKILL.md b/land-and-deploy/SKILL.md
index 921c4d5de..55d138718 100644
--- a/land-and-deploy/SKILL.md
+++ b/land-and-deploy/SKILL.md
@@ -349,6 +349,105 @@ AI orchestrator (e.g., OpenClaw). In spawned sessions:
- Focus on completing the task and reporting results via prose output.
- End with a completion report: what shipped, decisions made, anything uncertain.
+## GBrain Sync (skill start)
+
+```bash
+# gbrain-sync: drain pending writes, pull once per day. Silent no-op when
+# the feature isn't initialized or gbrain_sync_mode is "off". See
+# docs/gbrain-sync.md.
+
+_GSTACK_HOME="${GSTACK_HOME:-$HOME/.gstack}"
+_BRAIN_REMOTE_FILE="$HOME/.gstack-brain-remote.txt"
+_BRAIN_SYNC_BIN="~/.claude/skills/gstack/bin/gstack-brain-sync"
+_BRAIN_CONFIG_BIN="~/.claude/skills/gstack/bin/gstack-config"
+
+_BRAIN_SYNC_MODE=$("$_BRAIN_CONFIG_BIN" get gbrain_sync_mode 2>/dev/null || echo off)
+
+# New-machine hint: URL file present, local .git missing, sync not yet enabled.
+if [ -f "$_BRAIN_REMOTE_FILE" ] && [ ! -d "$_GSTACK_HOME/.git" ] && [ "$_BRAIN_SYNC_MODE" = "off" ]; then
+ _BRAIN_NEW_URL=$(head -1 "$_BRAIN_REMOTE_FILE" 2>/dev/null | tr -d '[:space:]')
+ if [ -n "$_BRAIN_NEW_URL" ]; then
+ echo "BRAIN_SYNC: brain repo detected: $_BRAIN_NEW_URL"
+ echo "BRAIN_SYNC: run 'gstack-brain-restore' to pull your cross-machine memory (or 'gstack-config set gbrain_sync_mode off' to dismiss forever)"
+ fi
+fi
+
+# Active-sync path.
+if [ -d "$_GSTACK_HOME/.git" ] && [ "$_BRAIN_SYNC_MODE" != "off" ]; then
+ # Once-per-day pull.
+ _BRAIN_LAST_PULL_FILE="$_GSTACK_HOME/.brain-last-pull"
+ _BRAIN_NOW=$(date +%s)
+ _BRAIN_DO_PULL=1
+ if [ -f "$_BRAIN_LAST_PULL_FILE" ]; then
+ _BRAIN_LAST=$(cat "$_BRAIN_LAST_PULL_FILE" 2>/dev/null || echo 0)
+ _BRAIN_AGE=$(( _BRAIN_NOW - _BRAIN_LAST ))
+ [ "$_BRAIN_AGE" -lt 86400 ] && _BRAIN_DO_PULL=0
+ fi
+ if [ "$_BRAIN_DO_PULL" = "1" ]; then
+ ( cd "$_GSTACK_HOME" && git fetch origin >/dev/null 2>&1 && git merge --ff-only "origin/$(git rev-parse --abbrev-ref HEAD)" >/dev/null 2>&1 ) || true
+ echo "$_BRAIN_NOW" > "$_BRAIN_LAST_PULL_FILE"
+ fi
+ # Drain pending queue, push.
+ "$_BRAIN_SYNC_BIN" --once 2>/dev/null || true
+fi
+
+# Status line — always emitted, easy to grep.
+if [ -d "$_GSTACK_HOME/.git" ] && [ "$_BRAIN_SYNC_MODE" != "off" ]; then
+ _BRAIN_QUEUE_DEPTH=0
+ [ -f "$_GSTACK_HOME/.brain-queue.jsonl" ] && _BRAIN_QUEUE_DEPTH=$(wc -l < "$_GSTACK_HOME/.brain-queue.jsonl" | tr -d ' ')
+ _BRAIN_LAST_PUSH="never"
+ [ -f "$_GSTACK_HOME/.brain-last-push" ] && _BRAIN_LAST_PUSH=$(cat "$_GSTACK_HOME/.brain-last-push" 2>/dev/null || echo never)
+ echo "BRAIN_SYNC: mode=$_BRAIN_SYNC_MODE | last_push=$_BRAIN_LAST_PUSH | queue=$_BRAIN_QUEUE_DEPTH"
+else
+ echo "BRAIN_SYNC: off"
+fi
+```
+
+
+
+**Privacy stop-gate (fires ONCE per machine).**
+
+If the bash output shows `BRAIN_SYNC: off` AND the config value
+`gbrain_sync_mode_prompted` is `false` AND gbrain is detected on this host
+(either `gbrain doctor --fast --json` succeeds or the `gbrain` binary is in PATH),
+fire a one-time privacy gate via AskUserQuestion:
+
+> gstack can publish your session memory (learnings, plans, designs, retros) to a
+> private GitHub repo that GBrain indexes across your machines. Higher tiers
+> include behavioral data (session timelines, developer profile). How much do you
+> want to sync?
+
+Options:
+- A) Everything allowlisted (recommended — maximum cross-machine memory)
+- B) Only artifacts (plans, designs, retros, learnings) — skip timelines and profile
+- C) Decline — keep everything local
+
+After the user answers, run (substituting the chosen value):
+
+```bash
+# Chosen mode: full | artifacts-only | off
+"$_BRAIN_CONFIG_BIN" set gbrain_sync_mode <choice>
+"$_BRAIN_CONFIG_BIN" set gbrain_sync_mode_prompted true
+```
+
+If A or B was chosen AND `~/.gstack/.git` doesn't exist, ask a follow-up:
+"Set up the GBrain sync repo now? (runs `gstack-brain-init`)"
+- A) Yes, run it now
+- B) Show me the command, I'll run it myself
+
+Do not block the skill. Emit the question, continue the skill workflow. The
+next skill run picks up wherever this left off.
+
+**At skill END (before the telemetry block),** run these bash commands to
+catch artifact writes (design docs, plans, retros) that skipped the writer
+shims, plus drain any still-pending queue entries:
+
+```bash
+"~/.claude/skills/gstack/bin/gstack-brain-sync" --discover-new 2>/dev/null || true
+"~/.claude/skills/gstack/bin/gstack-brain-sync" --once 2>/dev/null || true
+```
+
+
## Model-Specific Behavioral Patch (claude)
The following nudges are tuned for the claude model family. They are
diff --git a/learn/SKILL.md b/learn/SKILL.md
index 8d55c3c1a..972e809c4 100644
--- a/learn/SKILL.md
+++ b/learn/SKILL.md
@@ -352,6 +352,105 @@ AI orchestrator (e.g., OpenClaw). In spawned sessions:
- Focus on completing the task and reporting results via prose output.
- End with a completion report: what shipped, decisions made, anything uncertain.
+## GBrain Sync (skill start)
+
+```bash
+# gbrain-sync: drain pending writes, pull once per day. Silent no-op when
+# the feature isn't initialized or gbrain_sync_mode is "off". See
+# docs/gbrain-sync.md.
+
+_GSTACK_HOME="${GSTACK_HOME:-$HOME/.gstack}"
+_BRAIN_REMOTE_FILE="$HOME/.gstack-brain-remote.txt"
+_BRAIN_SYNC_BIN="~/.claude/skills/gstack/bin/gstack-brain-sync"
+_BRAIN_CONFIG_BIN="~/.claude/skills/gstack/bin/gstack-config"
+
+_BRAIN_SYNC_MODE=$("$_BRAIN_CONFIG_BIN" get gbrain_sync_mode 2>/dev/null || echo off)
+
+# New-machine hint: URL file present, local .git missing, sync not yet enabled.
+if [ -f "$_BRAIN_REMOTE_FILE" ] && [ ! -d "$_GSTACK_HOME/.git" ] && [ "$_BRAIN_SYNC_MODE" = "off" ]; then
+ _BRAIN_NEW_URL=$(head -1 "$_BRAIN_REMOTE_FILE" 2>/dev/null | tr -d '[:space:]')
+ if [ -n "$_BRAIN_NEW_URL" ]; then
+ echo "BRAIN_SYNC: brain repo detected: $_BRAIN_NEW_URL"
+ echo "BRAIN_SYNC: run 'gstack-brain-restore' to pull your cross-machine memory (or 'gstack-config set gbrain_sync_mode off' to dismiss forever)"
+ fi
+fi
+
+# Active-sync path.
+if [ -d "$_GSTACK_HOME/.git" ] && [ "$_BRAIN_SYNC_MODE" != "off" ]; then
+ # Once-per-day pull.
+ _BRAIN_LAST_PULL_FILE="$_GSTACK_HOME/.brain-last-pull"
+ _BRAIN_NOW=$(date +%s)
+ _BRAIN_DO_PULL=1
+ if [ -f "$_BRAIN_LAST_PULL_FILE" ]; then
+ _BRAIN_LAST=$(cat "$_BRAIN_LAST_PULL_FILE" 2>/dev/null || echo 0)
+ _BRAIN_AGE=$(( _BRAIN_NOW - _BRAIN_LAST ))
+ [ "$_BRAIN_AGE" -lt 86400 ] && _BRAIN_DO_PULL=0
+ fi
+ if [ "$_BRAIN_DO_PULL" = "1" ]; then
+ ( cd "$_GSTACK_HOME" && git fetch origin >/dev/null 2>&1 && git merge --ff-only "origin/$(git rev-parse --abbrev-ref HEAD)" >/dev/null 2>&1 ) || true
+ echo "$_BRAIN_NOW" > "$_BRAIN_LAST_PULL_FILE"
+ fi
+ # Drain pending queue, push.
+ "$_BRAIN_SYNC_BIN" --once 2>/dev/null || true
+fi
+
+# Status line — always emitted, easy to grep.
+if [ -d "$_GSTACK_HOME/.git" ] && [ "$_BRAIN_SYNC_MODE" != "off" ]; then
+ _BRAIN_QUEUE_DEPTH=0
+ [ -f "$_GSTACK_HOME/.brain-queue.jsonl" ] && _BRAIN_QUEUE_DEPTH=$(wc -l < "$_GSTACK_HOME/.brain-queue.jsonl" | tr -d ' ')
+ _BRAIN_LAST_PUSH="never"
+ [ -f "$_GSTACK_HOME/.brain-last-push" ] && _BRAIN_LAST_PUSH=$(cat "$_GSTACK_HOME/.brain-last-push" 2>/dev/null || echo never)
+ echo "BRAIN_SYNC: mode=$_BRAIN_SYNC_MODE | last_push=$_BRAIN_LAST_PUSH | queue=$_BRAIN_QUEUE_DEPTH"
+else
+ echo "BRAIN_SYNC: off"
+fi
+```
+
+
+
+**Privacy stop-gate (fires ONCE per machine).**
+
+If the bash output shows `BRAIN_SYNC: off` AND the config value
+`gbrain_sync_mode_prompted` is `false` AND gbrain is detected on this host
+(either `gbrain doctor --fast --json` succeeds or the `gbrain` binary is in PATH),
+fire a one-time privacy gate via AskUserQuestion:
+
+> gstack can publish your session memory (learnings, plans, designs, retros) to a
+> private GitHub repo that GBrain indexes across your machines. Higher tiers
+> include behavioral data (session timelines, developer profile). How much do you
+> want to sync?
+
+Options:
+- A) Everything allowlisted (recommended — maximum cross-machine memory)
+- B) Only artifacts (plans, designs, retros, learnings) — skip timelines and profile
+- C) Decline — keep everything local
+
+After the user answers, run (substituting the chosen value):
+
+```bash
+# Chosen mode: full | artifacts-only | off
+"$_BRAIN_CONFIG_BIN" set gbrain_sync_mode <choice>
+"$_BRAIN_CONFIG_BIN" set gbrain_sync_mode_prompted true
+```
+
+If A or B was chosen AND `~/.gstack/.git` doesn't exist, ask a follow-up:
+"Set up the GBrain sync repo now? (runs `gstack-brain-init`)"
+- A) Yes, run it now
+- B) Show me the command, I'll run it myself
+
+Do not block the skill. Emit the question, continue the skill workflow. The
+next skill run picks up wherever this left off.
+
+**At skill END (before the telemetry block),** run these bash commands to
+catch artifact writes (design docs, plans, retros) that skipped the writer
+shims, plus drain any still-pending queue entries:
+
+```bash
+"~/.claude/skills/gstack/bin/gstack-brain-sync" --discover-new 2>/dev/null || true
+"~/.claude/skills/gstack/bin/gstack-brain-sync" --once 2>/dev/null || true
+```
+
+
## Model-Specific Behavioral Patch (claude)
The following nudges are tuned for the claude model family. They are
diff --git a/make-pdf/SKILL.md b/make-pdf/SKILL.md
index 8414a3460..0d74fb1ac 100644
--- a/make-pdf/SKILL.md
+++ b/make-pdf/SKILL.md
@@ -350,6 +350,105 @@ AI orchestrator (e.g., OpenClaw). In spawned sessions:
- Focus on completing the task and reporting results via prose output.
- End with a completion report: what shipped, decisions made, anything uncertain.
+## GBrain Sync (skill start)
+
+```bash
+# gbrain-sync: drain pending writes, pull once per day. Silent no-op when
+# the feature isn't initialized or gbrain_sync_mode is "off". See
+# docs/gbrain-sync.md.
+
+_GSTACK_HOME="${GSTACK_HOME:-$HOME/.gstack}"
+_BRAIN_REMOTE_FILE="$HOME/.gstack-brain-remote.txt"
+_BRAIN_SYNC_BIN="~/.claude/skills/gstack/bin/gstack-brain-sync"
+_BRAIN_CONFIG_BIN="~/.claude/skills/gstack/bin/gstack-config"
+
+_BRAIN_SYNC_MODE=$("$_BRAIN_CONFIG_BIN" get gbrain_sync_mode 2>/dev/null || echo off)
+
+# New-machine hint: URL file present, local .git missing, sync not yet enabled.
+if [ -f "$_BRAIN_REMOTE_FILE" ] && [ ! -d "$_GSTACK_HOME/.git" ] && [ "$_BRAIN_SYNC_MODE" = "off" ]; then
+ _BRAIN_NEW_URL=$(head -1 "$_BRAIN_REMOTE_FILE" 2>/dev/null | tr -d '[:space:]')
+ if [ -n "$_BRAIN_NEW_URL" ]; then
+ echo "BRAIN_SYNC: brain repo detected: $_BRAIN_NEW_URL"
+ echo "BRAIN_SYNC: run 'gstack-brain-restore' to pull your cross-machine memory (or 'gstack-config set gbrain_sync_mode off' to dismiss forever)"
+ fi
+fi
+
+# Active-sync path.
+if [ -d "$_GSTACK_HOME/.git" ] && [ "$_BRAIN_SYNC_MODE" != "off" ]; then
+ # Once-per-day pull.
+ _BRAIN_LAST_PULL_FILE="$_GSTACK_HOME/.brain-last-pull"
+ _BRAIN_NOW=$(date +%s)
+ _BRAIN_DO_PULL=1
+ if [ -f "$_BRAIN_LAST_PULL_FILE" ]; then
+ _BRAIN_LAST=$(cat "$_BRAIN_LAST_PULL_FILE" 2>/dev/null || echo 0)
+ _BRAIN_AGE=$(( _BRAIN_NOW - _BRAIN_LAST ))
+ [ "$_BRAIN_AGE" -lt 86400 ] && _BRAIN_DO_PULL=0
+ fi
+ if [ "$_BRAIN_DO_PULL" = "1" ]; then
+ ( cd "$_GSTACK_HOME" && git fetch origin >/dev/null 2>&1 && git merge --ff-only "origin/$(git rev-parse --abbrev-ref HEAD)" >/dev/null 2>&1 ) || true
+ echo "$_BRAIN_NOW" > "$_BRAIN_LAST_PULL_FILE"
+ fi
+ # Drain pending queue, push.
+ "$_BRAIN_SYNC_BIN" --once 2>/dev/null || true
+fi
+
+# Status line — always emitted, easy to grep.
+if [ -d "$_GSTACK_HOME/.git" ] && [ "$_BRAIN_SYNC_MODE" != "off" ]; then
+ _BRAIN_QUEUE_DEPTH=0
+ [ -f "$_GSTACK_HOME/.brain-queue.jsonl" ] && _BRAIN_QUEUE_DEPTH=$(wc -l < "$_GSTACK_HOME/.brain-queue.jsonl" | tr -d ' ')
+ _BRAIN_LAST_PUSH="never"
+ [ -f "$_GSTACK_HOME/.brain-last-push" ] && _BRAIN_LAST_PUSH=$(cat "$_GSTACK_HOME/.brain-last-push" 2>/dev/null || echo never)
+ echo "BRAIN_SYNC: mode=$_BRAIN_SYNC_MODE | last_push=$_BRAIN_LAST_PUSH | queue=$_BRAIN_QUEUE_DEPTH"
+else
+ echo "BRAIN_SYNC: off"
+fi
+```
+
+
+
+**Privacy stop-gate (fires ONCE per machine).**
+
+If the bash output shows `BRAIN_SYNC: off` AND the config value
+`gbrain_sync_mode_prompted` is `false` AND gbrain is detected on this host
+(either `gbrain doctor --fast --json` succeeds or the `gbrain` binary is in PATH),
+fire a one-time privacy gate via AskUserQuestion:
+
+> gstack can publish your session memory (learnings, plans, designs, retros) to a
+> private GitHub repo that GBrain indexes across your machines. Higher tiers
+> include behavioral data (session timelines, developer profile). How much do you
+> want to sync?
+
+Options:
+- A) Everything allowlisted (recommended — maximum cross-machine memory)
+- B) Only artifacts (plans, designs, retros, learnings) — skip timelines and profile
+- C) Decline — keep everything local
+
+After the user answers, run (substituting the chosen value):
+
+```bash
+# Chosen mode: full | artifacts-only | off
+"$_BRAIN_CONFIG_BIN" set gbrain_sync_mode <choice>
+"$_BRAIN_CONFIG_BIN" set gbrain_sync_mode_prompted true
+```
+
+If A or B was chosen AND `~/.gstack/.git` doesn't exist, ask a follow-up:
+"Set up the GBrain sync repo now? (runs `gstack-brain-init`)"
+- A) Yes, run it now
+- B) Show me the command, I'll run it myself
+
+Do not block the skill. Emit the question, continue the skill workflow. The
+next skill run picks up wherever this left off.
+
+**At skill END (before the telemetry block),** run these bash commands to
+catch artifact writes (design docs, plans, retros) that skipped the writer
+shims, plus drain any still-pending queue entries:
+
+```bash
+"~/.claude/skills/gstack/bin/gstack-brain-sync" --discover-new 2>/dev/null || true
+"~/.claude/skills/gstack/bin/gstack-brain-sync" --once 2>/dev/null || true
+```
+
+
## Model-Specific Behavioral Patch (claude)
The following nudges are tuned for the claude model family. They are
diff --git a/office-hours/SKILL.md b/office-hours/SKILL.md
index 82765c6ff..73a706b6e 100644
--- a/office-hours/SKILL.md
+++ b/office-hours/SKILL.md
@@ -360,6 +360,105 @@ AI orchestrator (e.g., OpenClaw). In spawned sessions:
- Focus on completing the task and reporting results via prose output.
- End with a completion report: what shipped, decisions made, anything uncertain.
+## GBrain Sync (skill start)
+
+```bash
+# gbrain-sync: drain pending writes, pull once per day. Silent no-op when
+# the feature isn't initialized or gbrain_sync_mode is "off". See
+# docs/gbrain-sync.md.
+
+_GSTACK_HOME="${GSTACK_HOME:-$HOME/.gstack}"
+_BRAIN_REMOTE_FILE="$HOME/.gstack-brain-remote.txt"
+_BRAIN_SYNC_BIN="~/.claude/skills/gstack/bin/gstack-brain-sync"
+_BRAIN_CONFIG_BIN="~/.claude/skills/gstack/bin/gstack-config"
+
+_BRAIN_SYNC_MODE=$("$_BRAIN_CONFIG_BIN" get gbrain_sync_mode 2>/dev/null || echo off)
+
+# New-machine hint: URL file present, local .git missing, sync not yet enabled.
+if [ -f "$_BRAIN_REMOTE_FILE" ] && [ ! -d "$_GSTACK_HOME/.git" ] && [ "$_BRAIN_SYNC_MODE" = "off" ]; then
+ _BRAIN_NEW_URL=$(head -1 "$_BRAIN_REMOTE_FILE" 2>/dev/null | tr -d '[:space:]')
+ if [ -n "$_BRAIN_NEW_URL" ]; then
+ echo "BRAIN_SYNC: brain repo detected: $_BRAIN_NEW_URL"
+ echo "BRAIN_SYNC: run 'gstack-brain-restore' to pull your cross-machine memory (or 'gstack-config set gbrain_sync_mode off' to dismiss forever)"
+ fi
+fi
+
+# Active-sync path.
+if [ -d "$_GSTACK_HOME/.git" ] && [ "$_BRAIN_SYNC_MODE" != "off" ]; then
+ # Once-per-day pull.
+ _BRAIN_LAST_PULL_FILE="$_GSTACK_HOME/.brain-last-pull"
+ _BRAIN_NOW=$(date +%s)
+ _BRAIN_DO_PULL=1
+ if [ -f "$_BRAIN_LAST_PULL_FILE" ]; then
+ _BRAIN_LAST=$(cat "$_BRAIN_LAST_PULL_FILE" 2>/dev/null || echo 0)
+ _BRAIN_AGE=$(( _BRAIN_NOW - _BRAIN_LAST ))
+ [ "$_BRAIN_AGE" -lt 86400 ] && _BRAIN_DO_PULL=0
+ fi
+ if [ "$_BRAIN_DO_PULL" = "1" ]; then
+ ( cd "$_GSTACK_HOME" && git fetch origin >/dev/null 2>&1 && git merge --ff-only "origin/$(git rev-parse --abbrev-ref HEAD)" >/dev/null 2>&1 ) || true
+ echo "$_BRAIN_NOW" > "$_BRAIN_LAST_PULL_FILE"
+ fi
+ # Drain pending queue, push.
+ "$_BRAIN_SYNC_BIN" --once 2>/dev/null || true
+fi
+
+# Status line — always emitted, easy to grep.
+if [ -d "$_GSTACK_HOME/.git" ] && [ "$_BRAIN_SYNC_MODE" != "off" ]; then
+ _BRAIN_QUEUE_DEPTH=0
+ [ -f "$_GSTACK_HOME/.brain-queue.jsonl" ] && _BRAIN_QUEUE_DEPTH=$(wc -l < "$_GSTACK_HOME/.brain-queue.jsonl" | tr -d ' ')
+ _BRAIN_LAST_PUSH="never"
+ [ -f "$_GSTACK_HOME/.brain-last-push" ] && _BRAIN_LAST_PUSH=$(cat "$_GSTACK_HOME/.brain-last-push" 2>/dev/null || echo never)
+ echo "BRAIN_SYNC: mode=$_BRAIN_SYNC_MODE | last_push=$_BRAIN_LAST_PUSH | queue=$_BRAIN_QUEUE_DEPTH"
+else
+ echo "BRAIN_SYNC: off"
+fi
+```
+
+
+
+**Privacy stop-gate (fires ONCE per machine).**
+
+If the bash output shows `BRAIN_SYNC: off` AND the config value
+`gbrain_sync_mode_prompted` is `false` AND gbrain is detected on this host
+(either `gbrain doctor --fast --json` succeeds or the `gbrain` binary is in PATH),
+fire a one-time privacy gate via AskUserQuestion:
+
+> gstack can publish your session memory (learnings, plans, designs, retros) to a
+> private GitHub repo that GBrain indexes across your machines. Higher tiers
+> include behavioral data (session timelines, developer profile). How much do you
+> want to sync?
+
+Options:
+- A) Everything allowlisted (recommended — maximum cross-machine memory)
+- B) Only artifacts (plans, designs, retros, learnings) — skip timelines and profile
+- C) Decline — keep everything local
+
+After the user answers, run (substituting the chosen value):
+
+```bash
+# Chosen mode: full | artifacts-only | off
+"$_BRAIN_CONFIG_BIN" set gbrain_sync_mode <choice>
+"$_BRAIN_CONFIG_BIN" set gbrain_sync_mode_prompted true
+```
+
+If A or B was chosen AND `~/.gstack/.git` doesn't exist, ask a follow-up:
+"Set up the GBrain sync repo now? (runs `gstack-brain-init`)"
+- A) Yes, run it now
+- B) Show me the command, I'll run it myself
+
+Do not block the skill. Emit the question, continue the skill workflow. The
+next skill run picks up wherever this left off.
+
+**At skill END (before the telemetry block),** run these bash commands to
+catch artifact writes (design docs, plans, retros) that skipped the writer
+shims, plus drain any still-pending queue entries:
+
+```bash
+"~/.claude/skills/gstack/bin/gstack-brain-sync" --discover-new 2>/dev/null || true
+"~/.claude/skills/gstack/bin/gstack-brain-sync" --once 2>/dev/null || true
+```
+
+
## Model-Specific Behavioral Patch (claude)
The following nudges are tuned for the claude model family. They are
diff --git a/open-gstack-browser/SKILL.md b/open-gstack-browser/SKILL.md
index d627c6809..7f880856a 100644
--- a/open-gstack-browser/SKILL.md
+++ b/open-gstack-browser/SKILL.md
@@ -349,6 +349,105 @@ AI orchestrator (e.g., OpenClaw). In spawned sessions:
- Focus on completing the task and reporting results via prose output.
- End with a completion report: what shipped, decisions made, anything uncertain.
+## GBrain Sync (skill start)
+
+```bash
+# gbrain-sync: drain pending writes, pull once per day. Silent no-op when
+# the feature isn't initialized or gbrain_sync_mode is "off". See
+# docs/gbrain-sync.md.
+
+_GSTACK_HOME="${GSTACK_HOME:-$HOME/.gstack}"
+_BRAIN_REMOTE_FILE="$HOME/.gstack-brain-remote.txt"
+_BRAIN_SYNC_BIN="~/.claude/skills/gstack/bin/gstack-brain-sync"
+_BRAIN_CONFIG_BIN="~/.claude/skills/gstack/bin/gstack-config"
+
+_BRAIN_SYNC_MODE=$("$_BRAIN_CONFIG_BIN" get gbrain_sync_mode 2>/dev/null || echo off)
+
+# New-machine hint: URL file present, local .git missing, sync not yet enabled.
+if [ -f "$_BRAIN_REMOTE_FILE" ] && [ ! -d "$_GSTACK_HOME/.git" ] && [ "$_BRAIN_SYNC_MODE" = "off" ]; then
+ _BRAIN_NEW_URL=$(head -1 "$_BRAIN_REMOTE_FILE" 2>/dev/null | tr -d '[:space:]')
+ if [ -n "$_BRAIN_NEW_URL" ]; then
+ echo "BRAIN_SYNC: brain repo detected: $_BRAIN_NEW_URL"
+ echo "BRAIN_SYNC: run 'gstack-brain-restore' to pull your cross-machine memory (or 'gstack-config set gbrain_sync_mode off' to dismiss forever)"
+ fi
+fi
+
+# Active-sync path.
+if [ -d "$_GSTACK_HOME/.git" ] && [ "$_BRAIN_SYNC_MODE" != "off" ]; then
+ # Once-per-day pull.
+ _BRAIN_LAST_PULL_FILE="$_GSTACK_HOME/.brain-last-pull"
+ _BRAIN_NOW=$(date +%s)
+ _BRAIN_DO_PULL=1
+ if [ -f "$_BRAIN_LAST_PULL_FILE" ]; then
+ _BRAIN_LAST=$(cat "$_BRAIN_LAST_PULL_FILE" 2>/dev/null || echo 0)
+ _BRAIN_AGE=$(( _BRAIN_NOW - _BRAIN_LAST ))
+ [ "$_BRAIN_AGE" -lt 86400 ] && _BRAIN_DO_PULL=0
+ fi
+ if [ "$_BRAIN_DO_PULL" = "1" ]; then
+ ( cd "$_GSTACK_HOME" && git fetch origin >/dev/null 2>&1 && git merge --ff-only "origin/$(git rev-parse --abbrev-ref HEAD)" >/dev/null 2>&1 ) || true
+ echo "$_BRAIN_NOW" > "$_BRAIN_LAST_PULL_FILE"
+ fi
+ # Drain pending queue, push.
+ "$_BRAIN_SYNC_BIN" --once 2>/dev/null || true
+fi
+
+# Status line — always emitted, easy to grep.
+if [ -d "$_GSTACK_HOME/.git" ] && [ "$_BRAIN_SYNC_MODE" != "off" ]; then
+ _BRAIN_QUEUE_DEPTH=0
+ [ -f "$_GSTACK_HOME/.brain-queue.jsonl" ] && _BRAIN_QUEUE_DEPTH=$(wc -l < "$_GSTACK_HOME/.brain-queue.jsonl" | tr -d ' ')
+ _BRAIN_LAST_PUSH="never"
+ [ -f "$_GSTACK_HOME/.brain-last-push" ] && _BRAIN_LAST_PUSH=$(cat "$_GSTACK_HOME/.brain-last-push" 2>/dev/null || echo never)
+ echo "BRAIN_SYNC: mode=$_BRAIN_SYNC_MODE | last_push=$_BRAIN_LAST_PUSH | queue=$_BRAIN_QUEUE_DEPTH"
+else
+ echo "BRAIN_SYNC: off"
+fi
+```
+
+
+
+**Privacy stop-gate (fires ONCE per machine).**
+
+If the bash output shows `BRAIN_SYNC: off` AND the config value
+`gbrain_sync_mode_prompted` is `false` AND gbrain is detected on this host
+(either `gbrain doctor --fast --json` succeeds or the `gbrain` binary is in PATH),
+fire a one-time privacy gate via AskUserQuestion:
+
+> gstack can publish your session memory (learnings, plans, designs, retros) to a
+> private GitHub repo that GBrain indexes across your machines. Higher tiers
+> include behavioral data (session timelines, developer profile). How much do you
+> want to sync?
+
+Options:
+- A) Everything allowlisted (recommended — maximum cross-machine memory)
+- B) Only artifacts (plans, designs, retros, learnings) — skip timelines and profile
+- C) Decline — keep everything local
+
+After the user answers, run (substituting the chosen value):
+
+```bash
+# Chosen mode: full | artifacts-only | off
+"$_BRAIN_CONFIG_BIN" set gbrain_sync_mode <choice>
+"$_BRAIN_CONFIG_BIN" set gbrain_sync_mode_prompted true
+```
+
+If A or B was chosen AND `~/.gstack/.git` doesn't exist, ask a follow-up:
+"Set up the GBrain sync repo now? (runs `gstack-brain-init`)"
+- A) Yes, run it now
+- B) Show me the command, I'll run it myself
+
+Do not block the skill. Emit the question, continue the skill workflow. The
+next skill run picks up wherever this left off.
+
+**At skill END (before the telemetry block),** run these bash commands to
+catch artifact writes (design docs, plans, retros) that skipped the writer
+shims, plus drain any still-pending queue entries:
+
+```bash
+"~/.claude/skills/gstack/bin/gstack-brain-sync" --discover-new 2>/dev/null || true
+"~/.claude/skills/gstack/bin/gstack-brain-sync" --once 2>/dev/null || true
+```
+
+
## Model-Specific Behavioral Patch (claude)
The following nudges are tuned for the claude model family. They are
diff --git a/package.json b/package.json
index b106a6566..701203e86 100644
--- a/package.json
+++ b/package.json
@@ -1,6 +1,6 @@
{
"name": "gstack",
- "version": "1.6.4.0",
+ "version": "1.9.0.0",
"description": "Garry's Stack — Claude Code skills + fast headless browser. One repo, one install, entire AI engineering workflow.",
"license": "MIT",
"type": "module",
diff --git a/pair-agent/SKILL.md b/pair-agent/SKILL.md
index e19bb1407..77806d8de 100644
--- a/pair-agent/SKILL.md
+++ b/pair-agent/SKILL.md
@@ -350,6 +350,105 @@ AI orchestrator (e.g., OpenClaw). In spawned sessions:
- Focus on completing the task and reporting results via prose output.
- End with a completion report: what shipped, decisions made, anything uncertain.
+## GBrain Sync (skill start)
+
+```bash
+# gbrain-sync: drain pending writes, pull once per day. Silent no-op when
+# the feature isn't initialized or gbrain_sync_mode is "off". See
+# docs/gbrain-sync.md.
+
+_GSTACK_HOME="${GSTACK_HOME:-$HOME/.gstack}"
+_BRAIN_REMOTE_FILE="$HOME/.gstack-brain-remote.txt"
+_BRAIN_SYNC_BIN="~/.claude/skills/gstack/bin/gstack-brain-sync"
+_BRAIN_CONFIG_BIN="~/.claude/skills/gstack/bin/gstack-config"
+
+_BRAIN_SYNC_MODE=$("$_BRAIN_CONFIG_BIN" get gbrain_sync_mode 2>/dev/null || echo off)
+
+# New-machine hint: URL file present, local .git missing, sync not yet enabled.
+if [ -f "$_BRAIN_REMOTE_FILE" ] && [ ! -d "$_GSTACK_HOME/.git" ] && [ "$_BRAIN_SYNC_MODE" = "off" ]; then
+ _BRAIN_NEW_URL=$(head -1 "$_BRAIN_REMOTE_FILE" 2>/dev/null | tr -d '[:space:]')
+ if [ -n "$_BRAIN_NEW_URL" ]; then
+ echo "BRAIN_SYNC: brain repo detected: $_BRAIN_NEW_URL"
+ echo "BRAIN_SYNC: run 'gstack-brain-restore' to pull your cross-machine memory (or 'gstack-config set gbrain_sync_mode off' to dismiss forever)"
+ fi
+fi
+
+# Active-sync path.
+if [ -d "$_GSTACK_HOME/.git" ] && [ "$_BRAIN_SYNC_MODE" != "off" ]; then
+ # Once-per-day pull.
+ _BRAIN_LAST_PULL_FILE="$_GSTACK_HOME/.brain-last-pull"
+ _BRAIN_NOW=$(date +%s)
+ _BRAIN_DO_PULL=1
+ if [ -f "$_BRAIN_LAST_PULL_FILE" ]; then
+ _BRAIN_LAST=$(cat "$_BRAIN_LAST_PULL_FILE" 2>/dev/null || echo 0)
+ _BRAIN_AGE=$(( _BRAIN_NOW - _BRAIN_LAST ))
+ [ "$_BRAIN_AGE" -lt 86400 ] && _BRAIN_DO_PULL=0
+ fi
+ if [ "$_BRAIN_DO_PULL" = "1" ]; then
+ ( cd "$_GSTACK_HOME" && git fetch origin >/dev/null 2>&1 && git merge --ff-only "origin/$(git rev-parse --abbrev-ref HEAD)" >/dev/null 2>&1 ) || true
+ echo "$_BRAIN_NOW" > "$_BRAIN_LAST_PULL_FILE"
+ fi
+ # Drain pending queue, push.
+ "$_BRAIN_SYNC_BIN" --once 2>/dev/null || true
+fi
+
+# Status line — always emitted, easy to grep.
+if [ -d "$_GSTACK_HOME/.git" ] && [ "$_BRAIN_SYNC_MODE" != "off" ]; then
+ _BRAIN_QUEUE_DEPTH=0
+ [ -f "$_GSTACK_HOME/.brain-queue.jsonl" ] && _BRAIN_QUEUE_DEPTH=$(wc -l < "$_GSTACK_HOME/.brain-queue.jsonl" | tr -d ' ')
+ _BRAIN_LAST_PUSH="never"
+ [ -f "$_GSTACK_HOME/.brain-last-push" ] && _BRAIN_LAST_PUSH=$(cat "$_GSTACK_HOME/.brain-last-push" 2>/dev/null || echo never)
+ echo "BRAIN_SYNC: mode=$_BRAIN_SYNC_MODE | last_push=$_BRAIN_LAST_PUSH | queue=$_BRAIN_QUEUE_DEPTH"
+else
+ echo "BRAIN_SYNC: off"
+fi
+```
+
+
+
+**Privacy stop-gate (fires ONCE per machine).**
+
+If the bash output shows `BRAIN_SYNC: off` AND the config value
+`gbrain_sync_mode_prompted` is `false` AND gbrain is detected on this host
+(either `gbrain doctor --fast --json` succeeds or the `gbrain` binary is in PATH),
+fire a one-time privacy gate via AskUserQuestion:
+
+> gstack can publish your session memory (learnings, plans, designs, retros) to a
+> private GitHub repo that GBrain indexes across your machines. Higher tiers
+> include behavioral data (session timelines, developer profile). How much do you
+> want to sync?
+
+Options:
+- A) Everything allowlisted (recommended — maximum cross-machine memory)
+- B) Only artifacts (plans, designs, retros, learnings) — skip timelines and profile
+- C) Decline — keep everything local
+
+After the user answers, run (substituting the chosen value):
+
+```bash
+# Chosen mode: full | artifacts-only | off
+"$_BRAIN_CONFIG_BIN" set gbrain_sync_mode <choice>
+"$_BRAIN_CONFIG_BIN" set gbrain_sync_mode_prompted true
+```
+
+If A or B was chosen AND `~/.gstack/.git` doesn't exist, ask a follow-up:
+"Set up the GBrain sync repo now? (runs `gstack-brain-init`)"
+- A) Yes, run it now
+- B) Show me the command, I'll run it myself
+
+Do not block the skill. Emit the question, continue the skill workflow. The
+next skill run picks up wherever this left off.
+
+**At skill END (before the telemetry block),** run these bash commands to
+catch artifact writes (design docs, plans, retros) that skipped the writer
+shims, plus drain any still-pending queue entries:
+
+```bash
+"~/.claude/skills/gstack/bin/gstack-brain-sync" --discover-new 2>/dev/null || true
+"~/.claude/skills/gstack/bin/gstack-brain-sync" --once 2>/dev/null || true
+```
+
+
## Model-Specific Behavioral Patch (claude)
The following nudges are tuned for the claude model family. They are
diff --git a/plan-ceo-review/SKILL.md b/plan-ceo-review/SKILL.md
index 495222ebe..d7e2cdf64 100644
--- a/plan-ceo-review/SKILL.md
+++ b/plan-ceo-review/SKILL.md
@@ -356,6 +356,105 @@ AI orchestrator (e.g., OpenClaw). In spawned sessions:
- Focus on completing the task and reporting results via prose output.
- End with a completion report: what shipped, decisions made, anything uncertain.
+## GBrain Sync (skill start)
+
+```bash
+# gbrain-sync: drain pending writes, pull once per day. Silent no-op when
+# the feature isn't initialized or gbrain_sync_mode is "off". See
+# docs/gbrain-sync.md.
+
+_GSTACK_HOME="${GSTACK_HOME:-$HOME/.gstack}"
+_BRAIN_REMOTE_FILE="$HOME/.gstack-brain-remote.txt"
+_BRAIN_SYNC_BIN="~/.claude/skills/gstack/bin/gstack-brain-sync"
+_BRAIN_CONFIG_BIN="~/.claude/skills/gstack/bin/gstack-config"
+
+_BRAIN_SYNC_MODE=$("$_BRAIN_CONFIG_BIN" get gbrain_sync_mode 2>/dev/null || echo off)
+
+# New-machine hint: URL file present, local .git missing, sync not yet enabled.
+if [ -f "$_BRAIN_REMOTE_FILE" ] && [ ! -d "$_GSTACK_HOME/.git" ] && [ "$_BRAIN_SYNC_MODE" = "off" ]; then
+ _BRAIN_NEW_URL=$(head -1 "$_BRAIN_REMOTE_FILE" 2>/dev/null | tr -d '[:space:]')
+ if [ -n "$_BRAIN_NEW_URL" ]; then
+ echo "BRAIN_SYNC: brain repo detected: $_BRAIN_NEW_URL"
+ echo "BRAIN_SYNC: run 'gstack-brain-restore' to pull your cross-machine memory (or 'gstack-config set gbrain_sync_mode off' to dismiss forever)"
+ fi
+fi
+
+# Active-sync path.
+if [ -d "$_GSTACK_HOME/.git" ] && [ "$_BRAIN_SYNC_MODE" != "off" ]; then
+ # Once-per-day pull.
+ _BRAIN_LAST_PULL_FILE="$_GSTACK_HOME/.brain-last-pull"
+ _BRAIN_NOW=$(date +%s)
+ _BRAIN_DO_PULL=1
+ if [ -f "$_BRAIN_LAST_PULL_FILE" ]; then
+ _BRAIN_LAST=$(cat "$_BRAIN_LAST_PULL_FILE" 2>/dev/null || echo 0)
+ _BRAIN_AGE=$(( _BRAIN_NOW - _BRAIN_LAST ))
+ [ "$_BRAIN_AGE" -lt 86400 ] && _BRAIN_DO_PULL=0
+ fi
+ if [ "$_BRAIN_DO_PULL" = "1" ]; then
+ ( cd "$_GSTACK_HOME" && git fetch origin >/dev/null 2>&1 && git merge --ff-only "origin/$(git rev-parse --abbrev-ref HEAD)" >/dev/null 2>&1 ) || true
+ echo "$_BRAIN_NOW" > "$_BRAIN_LAST_PULL_FILE"
+ fi
+ # Drain pending queue, push.
+ "$_BRAIN_SYNC_BIN" --once 2>/dev/null || true
+fi
+
+# Status line — always emitted, easy to grep.
+if [ -d "$_GSTACK_HOME/.git" ] && [ "$_BRAIN_SYNC_MODE" != "off" ]; then
+ _BRAIN_QUEUE_DEPTH=0
+ [ -f "$_GSTACK_HOME/.brain-queue.jsonl" ] && _BRAIN_QUEUE_DEPTH=$(wc -l < "$_GSTACK_HOME/.brain-queue.jsonl" | tr -d ' ')
+ _BRAIN_LAST_PUSH="never"
+ [ -f "$_GSTACK_HOME/.brain-last-push" ] && _BRAIN_LAST_PUSH=$(cat "$_GSTACK_HOME/.brain-last-push" 2>/dev/null || echo never)
+ echo "BRAIN_SYNC: mode=$_BRAIN_SYNC_MODE | last_push=$_BRAIN_LAST_PUSH | queue=$_BRAIN_QUEUE_DEPTH"
+else
+ echo "BRAIN_SYNC: off"
+fi
+```
+
+
+
+**Privacy stop-gate (fires ONCE per machine).**
+
+If the bash output shows `BRAIN_SYNC: off` AND the config value
+`gbrain_sync_mode_prompted` is `false` AND gbrain is detected on this host
+(either `gbrain doctor --fast --json` succeeds or the `gbrain` binary is in PATH),
+fire a one-time privacy gate via AskUserQuestion:
+
+> gstack can publish your session memory (learnings, plans, designs, retros) to a
+> private GitHub repo that GBrain indexes across your machines. Higher tiers
+> include behavioral data (session timelines, developer profile). How much do you
+> want to sync?
+
+Options:
+- A) Everything allowlisted (recommended — maximum cross-machine memory)
+- B) Only artifacts (plans, designs, retros, learnings) — skip timelines and profile
+- C) Decline — keep everything local
+
+After the user answers, run (substituting the chosen value):
+
+```bash
+# Chosen mode: full | artifacts-only | off
+"$_BRAIN_CONFIG_BIN" set gbrain_sync_mode <choice>
+"$_BRAIN_CONFIG_BIN" set gbrain_sync_mode_prompted true
+```
+
+If A or B was chosen AND `~/.gstack/.git` doesn't exist, ask a follow-up:
+"Set up the GBrain sync repo now? (runs `gstack-brain-init`)"
+- A) Yes, run it now
+- B) Show me the command, I'll run it myself
+
+Do not block the skill. Emit the question, continue the skill workflow. The
+next skill run picks up wherever this left off.
+
+**At skill END (before the telemetry block),** run these bash commands to
+catch artifact writes (design docs, plans, retros) that skipped the writer
+shims, plus drain any still-pending queue entries:
+
+```bash
+"~/.claude/skills/gstack/bin/gstack-brain-sync" --discover-new 2>/dev/null || true
+"~/.claude/skills/gstack/bin/gstack-brain-sync" --once 2>/dev/null || true
+```
+
+
## Model-Specific Behavioral Patch (claude)
The following nudges are tuned for the claude model family. They are
diff --git a/plan-design-review/SKILL.md b/plan-design-review/SKILL.md
index 676736f9d..d30f72230 100644
--- a/plan-design-review/SKILL.md
+++ b/plan-design-review/SKILL.md
@@ -353,6 +353,105 @@ AI orchestrator (e.g., OpenClaw). In spawned sessions:
- Focus on completing the task and reporting results via prose output.
- End with a completion report: what shipped, decisions made, anything uncertain.
+## GBrain Sync (skill start)
+
+```bash
+# gbrain-sync: drain pending writes, pull once per day. Silent no-op when
+# the feature isn't initialized or gbrain_sync_mode is "off". See
+# docs/gbrain-sync.md.
+
+_GSTACK_HOME="${GSTACK_HOME:-$HOME/.gstack}"
+_BRAIN_REMOTE_FILE="$HOME/.gstack-brain-remote.txt"
+_BRAIN_SYNC_BIN="~/.claude/skills/gstack/bin/gstack-brain-sync"
+_BRAIN_CONFIG_BIN="~/.claude/skills/gstack/bin/gstack-config"
+
+_BRAIN_SYNC_MODE=$("$_BRAIN_CONFIG_BIN" get gbrain_sync_mode 2>/dev/null || echo off)
+
+# New-machine hint: URL file present, local .git missing, sync not yet enabled.
+if [ -f "$_BRAIN_REMOTE_FILE" ] && [ ! -d "$_GSTACK_HOME/.git" ] && [ "$_BRAIN_SYNC_MODE" = "off" ]; then
+ _BRAIN_NEW_URL=$(head -1 "$_BRAIN_REMOTE_FILE" 2>/dev/null | tr -d '[:space:]')
+ if [ -n "$_BRAIN_NEW_URL" ]; then
+ echo "BRAIN_SYNC: brain repo detected: $_BRAIN_NEW_URL"
+ echo "BRAIN_SYNC: run 'gstack-brain-restore' to pull your cross-machine memory (or 'gstack-config set gbrain_sync_mode off' to dismiss forever)"
+ fi
+fi
+
+# Active-sync path.
+if [ -d "$_GSTACK_HOME/.git" ] && [ "$_BRAIN_SYNC_MODE" != "off" ]; then
+ # Once-per-day pull.
+ _BRAIN_LAST_PULL_FILE="$_GSTACK_HOME/.brain-last-pull"
+ _BRAIN_NOW=$(date +%s)
+ _BRAIN_DO_PULL=1
+ if [ -f "$_BRAIN_LAST_PULL_FILE" ]; then
+ _BRAIN_LAST=$(cat "$_BRAIN_LAST_PULL_FILE" 2>/dev/null || echo 0)
+ _BRAIN_AGE=$(( _BRAIN_NOW - _BRAIN_LAST ))
+ [ "$_BRAIN_AGE" -lt 86400 ] && _BRAIN_DO_PULL=0
+ fi
+ if [ "$_BRAIN_DO_PULL" = "1" ]; then
+ ( cd "$_GSTACK_HOME" && git fetch origin >/dev/null 2>&1 && git merge --ff-only "origin/$(git rev-parse --abbrev-ref HEAD)" >/dev/null 2>&1 ) || true
+ echo "$_BRAIN_NOW" > "$_BRAIN_LAST_PULL_FILE"
+ fi
+ # Drain pending queue, push.
+ "$_BRAIN_SYNC_BIN" --once 2>/dev/null || true
+fi
+
+# Status line — always emitted, easy to grep.
+if [ -d "$_GSTACK_HOME/.git" ] && [ "$_BRAIN_SYNC_MODE" != "off" ]; then
+ _BRAIN_QUEUE_DEPTH=0
+ [ -f "$_GSTACK_HOME/.brain-queue.jsonl" ] && _BRAIN_QUEUE_DEPTH=$(wc -l < "$_GSTACK_HOME/.brain-queue.jsonl" | tr -d ' ')
+ _BRAIN_LAST_PUSH="never"
+ [ -f "$_GSTACK_HOME/.brain-last-push" ] && _BRAIN_LAST_PUSH=$(cat "$_GSTACK_HOME/.brain-last-push" 2>/dev/null || echo never)
+ echo "BRAIN_SYNC: mode=$_BRAIN_SYNC_MODE | last_push=$_BRAIN_LAST_PUSH | queue=$_BRAIN_QUEUE_DEPTH"
+else
+ echo "BRAIN_SYNC: off"
+fi
+```
+
+
+
+**Privacy stop-gate (fires ONCE per machine).**
+
+If the bash output shows `BRAIN_SYNC: off` AND the config value
+`gbrain_sync_mode_prompted` is `false` AND gbrain is detected on this host
+(either `gbrain doctor --fast --json` succeeds or the `gbrain` binary is in PATH),
+fire a one-time privacy gate via AskUserQuestion:
+
+> gstack can publish your session memory (learnings, plans, designs, retros) to a
+> private GitHub repo that GBrain indexes across your machines. Higher tiers
+> include behavioral data (session timelines, developer profile). How much do you
+> want to sync?
+
+Options:
+- A) Everything allowlisted (recommended — maximum cross-machine memory)
+- B) Only artifacts (plans, designs, retros, learnings) — skip timelines and profile
+- C) Decline — keep everything local
+
+After the user answers, run (substituting the chosen value):
+
+```bash
+# Chosen mode: full | artifacts-only | off
+"$_BRAIN_CONFIG_BIN" set gbrain_sync_mode <choice>
+"$_BRAIN_CONFIG_BIN" set gbrain_sync_mode_prompted true
+```
+
+If A or B was chosen AND `~/.gstack/.git` doesn't exist, ask a follow-up:
+"Set up the GBrain sync repo now? (runs `gstack-brain-init`)"
+- A) Yes, run it now
+- B) Show me the command, I'll run it myself
+
+Do not block the skill. Emit the question, continue the skill workflow. The
+next skill run picks up wherever this left off.
+
+**At skill END (before the telemetry block),** run these bash commands to
+catch artifact writes (design docs, plans, retros) that skipped the writer
+shims, plus drain any still-pending queue entries:
+
+```bash
+"~/.claude/skills/gstack/bin/gstack-brain-sync" --discover-new 2>/dev/null || true
+"~/.claude/skills/gstack/bin/gstack-brain-sync" --once 2>/dev/null || true
+```
+
+
## Model-Specific Behavioral Patch (claude)
The following nudges are tuned for the claude model family. They are
diff --git a/plan-devex-review/SKILL.md b/plan-devex-review/SKILL.md
index 729c79192..3946711b5 100644
--- a/plan-devex-review/SKILL.md
+++ b/plan-devex-review/SKILL.md
@@ -357,6 +357,105 @@ AI orchestrator (e.g., OpenClaw). In spawned sessions:
- Focus on completing the task and reporting results via prose output.
- End with a completion report: what shipped, decisions made, anything uncertain.
+## GBrain Sync (skill start)
+
+```bash
+# gbrain-sync: drain pending writes, pull once per day. Silent no-op when
+# the feature isn't initialized or gbrain_sync_mode is "off". See
+# docs/gbrain-sync.md.
+
+_GSTACK_HOME="${GSTACK_HOME:-$HOME/.gstack}"
+_BRAIN_REMOTE_FILE="$HOME/.gstack-brain-remote.txt"
+_BRAIN_SYNC_BIN="~/.claude/skills/gstack/bin/gstack-brain-sync"
+_BRAIN_CONFIG_BIN="~/.claude/skills/gstack/bin/gstack-config"
+
+_BRAIN_SYNC_MODE=$("$_BRAIN_CONFIG_BIN" get gbrain_sync_mode 2>/dev/null || echo off)
+
+# New-machine hint: URL file present, local .git missing, sync not yet enabled.
+if [ -f "$_BRAIN_REMOTE_FILE" ] && [ ! -d "$_GSTACK_HOME/.git" ] && [ "$_BRAIN_SYNC_MODE" = "off" ]; then
+ _BRAIN_NEW_URL=$(head -1 "$_BRAIN_REMOTE_FILE" 2>/dev/null | tr -d '[:space:]')
+ if [ -n "$_BRAIN_NEW_URL" ]; then
+ echo "BRAIN_SYNC: brain repo detected: $_BRAIN_NEW_URL"
+ echo "BRAIN_SYNC: run 'gstack-brain-restore' to pull your cross-machine memory (or 'gstack-config set gbrain_sync_mode off' to dismiss forever)"
+ fi
+fi
+
+# Active-sync path.
+if [ -d "$_GSTACK_HOME/.git" ] && [ "$_BRAIN_SYNC_MODE" != "off" ]; then
+ # Once-per-day pull.
+ _BRAIN_LAST_PULL_FILE="$_GSTACK_HOME/.brain-last-pull"
+ _BRAIN_NOW=$(date +%s)
+ _BRAIN_DO_PULL=1
+ if [ -f "$_BRAIN_LAST_PULL_FILE" ]; then
+ _BRAIN_LAST=$(cat "$_BRAIN_LAST_PULL_FILE" 2>/dev/null || echo 0)
+ _BRAIN_AGE=$(( _BRAIN_NOW - _BRAIN_LAST ))
+ [ "$_BRAIN_AGE" -lt 86400 ] && _BRAIN_DO_PULL=0
+ fi
+ if [ "$_BRAIN_DO_PULL" = "1" ]; then
+ ( cd "$_GSTACK_HOME" && git fetch origin >/dev/null 2>&1 && git merge --ff-only "origin/$(git rev-parse --abbrev-ref HEAD)" >/dev/null 2>&1 ) || true
+ echo "$_BRAIN_NOW" > "$_BRAIN_LAST_PULL_FILE"
+ fi
+ # Drain pending queue, push.
+ "$_BRAIN_SYNC_BIN" --once 2>/dev/null || true
+fi
+
+# Status line — always emitted, easy to grep.
+if [ -d "$_GSTACK_HOME/.git" ] && [ "$_BRAIN_SYNC_MODE" != "off" ]; then
+ _BRAIN_QUEUE_DEPTH=0
+ [ -f "$_GSTACK_HOME/.brain-queue.jsonl" ] && _BRAIN_QUEUE_DEPTH=$(wc -l < "$_GSTACK_HOME/.brain-queue.jsonl" | tr -d ' ')
+ _BRAIN_LAST_PUSH="never"
+ [ -f "$_GSTACK_HOME/.brain-last-push" ] && _BRAIN_LAST_PUSH=$(cat "$_GSTACK_HOME/.brain-last-push" 2>/dev/null || echo never)
+ echo "BRAIN_SYNC: mode=$_BRAIN_SYNC_MODE | last_push=$_BRAIN_LAST_PUSH | queue=$_BRAIN_QUEUE_DEPTH"
+else
+ echo "BRAIN_SYNC: off"
+fi
+```
+
+
+
+**Privacy stop-gate (fires ONCE per machine).**
+
+If the bash output shows `BRAIN_SYNC: off` AND the config value
+`gbrain_sync_mode_prompted` is `false` AND gbrain is detected on this host
+(either `gbrain doctor --fast --json` succeeds or the `gbrain` binary is in PATH),
+fire a one-time privacy gate via AskUserQuestion:
+
+> gstack can publish your session memory (learnings, plans, designs, retros) to a
+> private GitHub repo that GBrain indexes across your machines. Higher tiers
+> include behavioral data (session timelines, developer profile). How much do you
+> want to sync?
+
+Options:
+- A) Everything allowlisted (recommended — maximum cross-machine memory)
+- B) Only artifacts (plans, designs, retros, learnings) — skip timelines and profile
+- C) Decline — keep everything local
+
+After the user answers, run (substituting the chosen value):
+
+```bash
+# Chosen mode: full | artifacts-only | off
+"$_BRAIN_CONFIG_BIN" set gbrain_sync_mode <choice>
+"$_BRAIN_CONFIG_BIN" set gbrain_sync_mode_prompted true
+```
+
+If A or B was chosen AND `~/.gstack/.git` doesn't exist, ask a follow-up:
+"Set up the GBrain sync repo now? (runs `gstack-brain-init`)"
+- A) Yes, run it now
+- B) Show me the command, I'll run it myself
+
+Do not block the skill. Emit the question, continue the skill workflow. The
+next skill run picks up wherever this left off.
+
+**At skill END (before the telemetry block),** run these bash commands to
+catch artifact writes (design docs, plans, retros) that skipped the writer
+shims, plus drain any still-pending queue entries:
+
+```bash
+"~/.claude/skills/gstack/bin/gstack-brain-sync" --discover-new 2>/dev/null || true
+"~/.claude/skills/gstack/bin/gstack-brain-sync" --once 2>/dev/null || true
+```
+
+
## Model-Specific Behavioral Patch (claude)
The following nudges are tuned for the claude model family. They are
diff --git a/plan-eng-review/SKILL.md b/plan-eng-review/SKILL.md
index 25741f58e..1b40c2eb3 100644
--- a/plan-eng-review/SKILL.md
+++ b/plan-eng-review/SKILL.md
@@ -355,6 +355,105 @@ AI orchestrator (e.g., OpenClaw). In spawned sessions:
- Focus on completing the task and reporting results via prose output.
- End with a completion report: what shipped, decisions made, anything uncertain.
+## GBrain Sync (skill start)
+
+```bash
+# gbrain-sync: drain pending writes, pull once per day. Silent no-op when
+# the feature isn't initialized or gbrain_sync_mode is "off". See
+# docs/gbrain-sync.md.
+
+_GSTACK_HOME="${GSTACK_HOME:-$HOME/.gstack}"
+_BRAIN_REMOTE_FILE="$HOME/.gstack-brain-remote.txt"
+_BRAIN_SYNC_BIN="~/.claude/skills/gstack/bin/gstack-brain-sync"
+_BRAIN_CONFIG_BIN="~/.claude/skills/gstack/bin/gstack-config"
+
+_BRAIN_SYNC_MODE=$("$_BRAIN_CONFIG_BIN" get gbrain_sync_mode 2>/dev/null || echo off)
+
+# New-machine hint: URL file present, local .git missing, sync not yet enabled.
+if [ -f "$_BRAIN_REMOTE_FILE" ] && [ ! -d "$_GSTACK_HOME/.git" ] && [ "$_BRAIN_SYNC_MODE" = "off" ]; then
+ _BRAIN_NEW_URL=$(head -1 "$_BRAIN_REMOTE_FILE" 2>/dev/null | tr -d '[:space:]')
+ if [ -n "$_BRAIN_NEW_URL" ]; then
+ echo "BRAIN_SYNC: brain repo detected: $_BRAIN_NEW_URL"
+ echo "BRAIN_SYNC: run 'gstack-brain-restore' to pull your cross-machine memory (or 'gstack-config set gbrain_sync_mode off' to dismiss forever)"
+ fi
+fi
+
+# Active-sync path.
+if [ -d "$_GSTACK_HOME/.git" ] && [ "$_BRAIN_SYNC_MODE" != "off" ]; then
+ # Once-per-day pull.
+ _BRAIN_LAST_PULL_FILE="$_GSTACK_HOME/.brain-last-pull"
+ _BRAIN_NOW=$(date +%s)
+ _BRAIN_DO_PULL=1
+ if [ -f "$_BRAIN_LAST_PULL_FILE" ]; then
+ _BRAIN_LAST=$(cat "$_BRAIN_LAST_PULL_FILE" 2>/dev/null || echo 0)
+ _BRAIN_AGE=$(( _BRAIN_NOW - _BRAIN_LAST ))
+ [ "$_BRAIN_AGE" -lt 86400 ] && _BRAIN_DO_PULL=0
+ fi
+ if [ "$_BRAIN_DO_PULL" = "1" ]; then
+ ( cd "$_GSTACK_HOME" && git fetch origin >/dev/null 2>&1 && git merge --ff-only "origin/$(git rev-parse --abbrev-ref HEAD)" >/dev/null 2>&1 ) || true
+ echo "$_BRAIN_NOW" > "$_BRAIN_LAST_PULL_FILE"
+ fi
+ # Drain pending queue, push.
+ "$_BRAIN_SYNC_BIN" --once 2>/dev/null || true
+fi
+
+# Status line — always emitted, easy to grep.
+if [ -d "$_GSTACK_HOME/.git" ] && [ "$_BRAIN_SYNC_MODE" != "off" ]; then
+ _BRAIN_QUEUE_DEPTH=0
+ [ -f "$_GSTACK_HOME/.brain-queue.jsonl" ] && _BRAIN_QUEUE_DEPTH=$(wc -l < "$_GSTACK_HOME/.brain-queue.jsonl" | tr -d ' ')
+ _BRAIN_LAST_PUSH="never"
+ [ -f "$_GSTACK_HOME/.brain-last-push" ] && _BRAIN_LAST_PUSH=$(cat "$_GSTACK_HOME/.brain-last-push" 2>/dev/null || echo never)
+ echo "BRAIN_SYNC: mode=$_BRAIN_SYNC_MODE | last_push=$_BRAIN_LAST_PUSH | queue=$_BRAIN_QUEUE_DEPTH"
+else
+ echo "BRAIN_SYNC: off"
+fi
+```
+
+
+
+**Privacy stop-gate (fires ONCE per machine).**
+
+If the bash output shows `BRAIN_SYNC: off` AND the config value
+`gbrain_sync_mode_prompted` is `false` AND gbrain is detected on this host
+(either `gbrain doctor --fast --json` succeeds or the `gbrain` binary is in PATH),
+fire a one-time privacy gate via AskUserQuestion:
+
+> gstack can publish your session memory (learnings, plans, designs, retros) to a
+> private GitHub repo that GBrain indexes across your machines. Higher tiers
+> include behavioral data (session timelines, developer profile). How much do you
+> want to sync?
+
+Options:
+- A) Everything allowlisted (recommended — maximum cross-machine memory)
+- B) Only artifacts (plans, designs, retros, learnings) — skip timelines and profile
+- C) Decline — keep everything local
+
+After the user answers, run (substituting the chosen value):
+
+```bash
+# Chosen mode: full | artifacts-only | off
+"$_BRAIN_CONFIG_BIN" set gbrain_sync_mode <choice>
+"$_BRAIN_CONFIG_BIN" set gbrain_sync_mode_prompted true
+```
+
+If A or B was chosen AND `~/.gstack/.git` doesn't exist, ask a follow-up:
+"Set up the GBrain sync repo now? (runs `gstack-brain-init`)"
+- A) Yes, run it now
+- B) Show me the command, I'll run it myself
+
+Do not block the skill. Emit the question, continue the skill workflow. The
+next skill run picks up wherever this left off.
+
+**At skill END (before the telemetry block),** run these bash commands to
+catch artifact writes (design docs, plans, retros) that skipped the writer
+shims, plus drain any still-pending queue entries:
+
+```bash
+"~/.claude/skills/gstack/bin/gstack-brain-sync" --discover-new 2>/dev/null || true
+"~/.claude/skills/gstack/bin/gstack-brain-sync" --once 2>/dev/null || true
+```
+
+
## Model-Specific Behavioral Patch (claude)
The following nudges are tuned for the claude model family. They are
diff --git a/plan-tune/SKILL.md b/plan-tune/SKILL.md
index 6d2c9bc37..988bbe7e9 100644
--- a/plan-tune/SKILL.md
+++ b/plan-tune/SKILL.md
@@ -363,6 +363,105 @@ AI orchestrator (e.g., OpenClaw). In spawned sessions:
- Focus on completing the task and reporting results via prose output.
- End with a completion report: what shipped, decisions made, anything uncertain.
+## GBrain Sync (skill start)
+
+```bash
+# gbrain-sync: drain pending writes, pull once per day. Silent no-op when
+# the feature isn't initialized or gbrain_sync_mode is "off". See
+# docs/gbrain-sync.md.
+
+_GSTACK_HOME="${GSTACK_HOME:-$HOME/.gstack}"
+_BRAIN_REMOTE_FILE="$HOME/.gstack-brain-remote.txt"
+_BRAIN_SYNC_BIN="~/.claude/skills/gstack/bin/gstack-brain-sync"
+_BRAIN_CONFIG_BIN="~/.claude/skills/gstack/bin/gstack-config"
+
+_BRAIN_SYNC_MODE=$("$_BRAIN_CONFIG_BIN" get gbrain_sync_mode 2>/dev/null || echo off)
+
+# New-machine hint: URL file present, local .git missing, sync not yet enabled.
+if [ -f "$_BRAIN_REMOTE_FILE" ] && [ ! -d "$_GSTACK_HOME/.git" ] && [ "$_BRAIN_SYNC_MODE" = "off" ]; then
+ _BRAIN_NEW_URL=$(head -1 "$_BRAIN_REMOTE_FILE" 2>/dev/null | tr -d '[:space:]')
+ if [ -n "$_BRAIN_NEW_URL" ]; then
+ echo "BRAIN_SYNC: brain repo detected: $_BRAIN_NEW_URL"
+ echo "BRAIN_SYNC: run 'gstack-brain-restore' to pull your cross-machine memory (or 'gstack-config set gbrain_sync_mode off' to dismiss forever)"
+ fi
+fi
+
+# Active-sync path.
+if [ -d "$_GSTACK_HOME/.git" ] && [ "$_BRAIN_SYNC_MODE" != "off" ]; then
+ # Once-per-day pull.
+ _BRAIN_LAST_PULL_FILE="$_GSTACK_HOME/.brain-last-pull"
+ _BRAIN_NOW=$(date +%s)
+ _BRAIN_DO_PULL=1
+ if [ -f "$_BRAIN_LAST_PULL_FILE" ]; then
+ _BRAIN_LAST=$(cat "$_BRAIN_LAST_PULL_FILE" 2>/dev/null || echo 0)
+ _BRAIN_AGE=$(( _BRAIN_NOW - _BRAIN_LAST ))
+ [ "$_BRAIN_AGE" -lt 86400 ] && _BRAIN_DO_PULL=0
+ fi
+ if [ "$_BRAIN_DO_PULL" = "1" ]; then
+ ( cd "$_GSTACK_HOME" && git fetch origin >/dev/null 2>&1 && git merge --ff-only "origin/$(git rev-parse --abbrev-ref HEAD)" >/dev/null 2>&1 ) || true
+ echo "$_BRAIN_NOW" > "$_BRAIN_LAST_PULL_FILE"
+ fi
+ # Drain pending queue, push.
+ "$_BRAIN_SYNC_BIN" --once 2>/dev/null || true
+fi
+
+# Status line — always emitted, easy to grep.
+if [ -d "$_GSTACK_HOME/.git" ] && [ "$_BRAIN_SYNC_MODE" != "off" ]; then
+ _BRAIN_QUEUE_DEPTH=0
+ [ -f "$_GSTACK_HOME/.brain-queue.jsonl" ] && _BRAIN_QUEUE_DEPTH=$(wc -l < "$_GSTACK_HOME/.brain-queue.jsonl" | tr -d ' ')
+ _BRAIN_LAST_PUSH="never"
+ [ -f "$_GSTACK_HOME/.brain-last-push" ] && _BRAIN_LAST_PUSH=$(cat "$_GSTACK_HOME/.brain-last-push" 2>/dev/null || echo never)
+ echo "BRAIN_SYNC: mode=$_BRAIN_SYNC_MODE | last_push=$_BRAIN_LAST_PUSH | queue=$_BRAIN_QUEUE_DEPTH"
+else
+ echo "BRAIN_SYNC: off"
+fi
+```
+
+
+
+**Privacy stop-gate (fires ONCE per machine).**
+
+If the bash output shows `BRAIN_SYNC: off` AND the config value
+`gbrain_sync_mode_prompted` is `false` AND gbrain is detected on this host
+(either `gbrain doctor --fast --json` succeeds or the `gbrain` binary is in PATH),
+fire a one-time privacy gate via AskUserQuestion:
+
+> gstack can publish your session memory (learnings, plans, designs, retros) to a
+> private GitHub repo that GBrain indexes across your machines. Higher tiers
+> include behavioral data (session timelines, developer profile). How much do you
+> want to sync?
+
+Options:
+- A) Everything allowlisted (recommended — maximum cross-machine memory)
+- B) Only artifacts (plans, designs, retros, learnings) — skip timelines and profile
+- C) Decline — keep everything local
+
+After the user answers, run (substituting the chosen value):
+
+```bash
+# Chosen mode: full | artifacts-only | off
+"$_BRAIN_CONFIG_BIN" set gbrain_sync_mode <choice>
+"$_BRAIN_CONFIG_BIN" set gbrain_sync_mode_prompted true
+```
+
+If A or B was chosen AND `~/.gstack/.git` doesn't exist, ask a follow-up:
+"Set up the GBrain sync repo now? (runs `gstack-brain-init`)"
+- A) Yes, run it now
+- B) Show me the command, I'll run it myself
+
+Do not block the skill. Emit the question, continue the skill workflow. The
+next skill run picks up wherever this left off.
+
+**At skill END (before the telemetry block),** run these bash commands to
+catch artifact writes (design docs, plans, retros) that skipped the writer
+shims, plus drain any still-pending queue entries:
+
+```bash
+"~/.claude/skills/gstack/bin/gstack-brain-sync" --discover-new 2>/dev/null || true
+"~/.claude/skills/gstack/bin/gstack-brain-sync" --once 2>/dev/null || true
+```
+
+
## Model-Specific Behavioral Patch (claude)
The following nudges are tuned for the claude model family. They are
diff --git a/qa-only/SKILL.md b/qa-only/SKILL.md
index a933c8372..2c83d1c63 100644
--- a/qa-only/SKILL.md
+++ b/qa-only/SKILL.md
@@ -351,6 +351,105 @@ AI orchestrator (e.g., OpenClaw). In spawned sessions:
- Focus on completing the task and reporting results via prose output.
- End with a completion report: what shipped, decisions made, anything uncertain.
+## GBrain Sync (skill start)
+
+```bash
+# gbrain-sync: drain pending writes, pull once per day. Silent no-op when
+# the feature isn't initialized or gbrain_sync_mode is "off". See
+# docs/gbrain-sync.md.
+
+_GSTACK_HOME="${GSTACK_HOME:-$HOME/.gstack}"
+_BRAIN_REMOTE_FILE="$HOME/.gstack-brain-remote.txt"
+_BRAIN_SYNC_BIN="~/.claude/skills/gstack/bin/gstack-brain-sync"
+_BRAIN_CONFIG_BIN="~/.claude/skills/gstack/bin/gstack-config"
+
+_BRAIN_SYNC_MODE=$("$_BRAIN_CONFIG_BIN" get gbrain_sync_mode 2>/dev/null || echo off)
+
+# New-machine hint: URL file present, local .git missing, sync not yet enabled.
+if [ -f "$_BRAIN_REMOTE_FILE" ] && [ ! -d "$_GSTACK_HOME/.git" ] && [ "$_BRAIN_SYNC_MODE" = "off" ]; then
+ _BRAIN_NEW_URL=$(head -1 "$_BRAIN_REMOTE_FILE" 2>/dev/null | tr -d '[:space:]')
+ if [ -n "$_BRAIN_NEW_URL" ]; then
+ echo "BRAIN_SYNC: brain repo detected: $_BRAIN_NEW_URL"
+ echo "BRAIN_SYNC: run 'gstack-brain-restore' to pull your cross-machine memory (or 'gstack-config set gbrain_sync_mode off' to dismiss forever)"
+ fi
+fi
+
+# Active-sync path.
+if [ -d "$_GSTACK_HOME/.git" ] && [ "$_BRAIN_SYNC_MODE" != "off" ]; then
+ # Once-per-day pull.
+ _BRAIN_LAST_PULL_FILE="$_GSTACK_HOME/.brain-last-pull"
+ _BRAIN_NOW=$(date +%s)
+ _BRAIN_DO_PULL=1
+ if [ -f "$_BRAIN_LAST_PULL_FILE" ]; then
+ _BRAIN_LAST=$(cat "$_BRAIN_LAST_PULL_FILE" 2>/dev/null || echo 0)
+ _BRAIN_AGE=$(( _BRAIN_NOW - _BRAIN_LAST ))
+ [ "$_BRAIN_AGE" -lt 86400 ] && _BRAIN_DO_PULL=0
+ fi
+ if [ "$_BRAIN_DO_PULL" = "1" ]; then
+ ( cd "$_GSTACK_HOME" && git fetch origin >/dev/null 2>&1 && git merge --ff-only "origin/$(git rev-parse --abbrev-ref HEAD)" >/dev/null 2>&1 ) || true
+ echo "$_BRAIN_NOW" > "$_BRAIN_LAST_PULL_FILE"
+ fi
+ # Drain pending queue, push.
+ "$_BRAIN_SYNC_BIN" --once 2>/dev/null || true
+fi
+
+# Status line — always emitted, easy to grep.
+if [ -d "$_GSTACK_HOME/.git" ] && [ "$_BRAIN_SYNC_MODE" != "off" ]; then
+ _BRAIN_QUEUE_DEPTH=0
+ [ -f "$_GSTACK_HOME/.brain-queue.jsonl" ] && _BRAIN_QUEUE_DEPTH=$(wc -l < "$_GSTACK_HOME/.brain-queue.jsonl" | tr -d ' ')
+ _BRAIN_LAST_PUSH="never"
+ [ -f "$_GSTACK_HOME/.brain-last-push" ] && _BRAIN_LAST_PUSH=$(cat "$_GSTACK_HOME/.brain-last-push" 2>/dev/null || echo never)
+ echo "BRAIN_SYNC: mode=$_BRAIN_SYNC_MODE | last_push=$_BRAIN_LAST_PUSH | queue=$_BRAIN_QUEUE_DEPTH"
+else
+ echo "BRAIN_SYNC: off"
+fi
+```
+
+
+
+**Privacy stop-gate (fires ONCE per machine).**
+
+If the bash output shows `BRAIN_SYNC: off` AND the config value
+`gbrain_sync_mode_prompted` is `false` AND gbrain is detected on this host
+(either `gbrain doctor --fast --json` succeeds or the `gbrain` binary is in PATH),
+fire a one-time privacy gate via AskUserQuestion:
+
+> gstack can publish your session memory (learnings, plans, designs, retros) to a
+> private GitHub repo that GBrain indexes across your machines. Higher tiers
+> include behavioral data (session timelines, developer profile). How much do you
+> want to sync?
+
+Options:
+- A) Everything allowlisted (recommended — maximum cross-machine memory)
+- B) Only artifacts (plans, designs, retros, learnings) — skip timelines and profile
+- C) Decline — keep everything local
+
+After the user answers, run (substituting the chosen value):
+
+```bash
+# Chosen mode: full | artifacts-only | off
+"$_BRAIN_CONFIG_BIN" set gbrain_sync_mode <choice>
+"$_BRAIN_CONFIG_BIN" set gbrain_sync_mode_prompted true
+```
+
+If A or B was chosen AND `~/.gstack/.git` doesn't exist, ask a follow-up:
+"Set up the GBrain sync repo now? (runs `gstack-brain-init`)"
+- A) Yes, run it now
+- B) Show me the command, I'll run it myself
+
+Do not block the skill. Emit the question, continue the skill workflow. The
+next skill run picks up wherever this left off.
+
+**At skill END (before the telemetry block),** run these bash commands to
+catch artifact writes (design docs, plans, retros) that skipped the writer
+shims, plus drain any still-pending queue entries:
+
+```bash
+"~/.claude/skills/gstack/bin/gstack-brain-sync" --discover-new 2>/dev/null || true
+"~/.claude/skills/gstack/bin/gstack-brain-sync" --once 2>/dev/null || true
+```
+
+
## Model-Specific Behavioral Patch (claude)
The following nudges are tuned for the claude model family. They are
diff --git a/qa/SKILL.md b/qa/SKILL.md
index 6c6330c2b..218c42646 100644
--- a/qa/SKILL.md
+++ b/qa/SKILL.md
@@ -357,6 +357,105 @@ AI orchestrator (e.g., OpenClaw). In spawned sessions:
- Focus on completing the task and reporting results via prose output.
- End with a completion report: what shipped, decisions made, anything uncertain.
+## GBrain Sync (skill start)
+
+```bash
+# gbrain-sync: drain pending writes, pull once per day. Silent no-op when
+# the feature isn't initialized or gbrain_sync_mode is "off". See
+# docs/gbrain-sync.md.
+
+_GSTACK_HOME="${GSTACK_HOME:-$HOME/.gstack}"
+_BRAIN_REMOTE_FILE="$HOME/.gstack-brain-remote.txt"
+_BRAIN_SYNC_BIN="~/.claude/skills/gstack/bin/gstack-brain-sync"
+_BRAIN_CONFIG_BIN="~/.claude/skills/gstack/bin/gstack-config"
+
+_BRAIN_SYNC_MODE=$("$_BRAIN_CONFIG_BIN" get gbrain_sync_mode 2>/dev/null || echo off)
+
+# New-machine hint: URL file present, local .git missing, sync not yet enabled.
+if [ -f "$_BRAIN_REMOTE_FILE" ] && [ ! -d "$_GSTACK_HOME/.git" ] && [ "$_BRAIN_SYNC_MODE" = "off" ]; then
+ _BRAIN_NEW_URL=$(head -1 "$_BRAIN_REMOTE_FILE" 2>/dev/null | tr -d '[:space:]')
+ if [ -n "$_BRAIN_NEW_URL" ]; then
+ echo "BRAIN_SYNC: brain repo detected: $_BRAIN_NEW_URL"
+ echo "BRAIN_SYNC: run 'gstack-brain-restore' to pull your cross-machine memory (or 'gstack-config set gbrain_sync_mode off' to dismiss forever)"
+ fi
+fi
+
+# Active-sync path.
+if [ -d "$_GSTACK_HOME/.git" ] && [ "$_BRAIN_SYNC_MODE" != "off" ]; then
+ # Once-per-day pull.
+ _BRAIN_LAST_PULL_FILE="$_GSTACK_HOME/.brain-last-pull"
+ _BRAIN_NOW=$(date +%s)
+ _BRAIN_DO_PULL=1
+ if [ -f "$_BRAIN_LAST_PULL_FILE" ]; then
+ _BRAIN_LAST=$(cat "$_BRAIN_LAST_PULL_FILE" 2>/dev/null || echo 0)
+ _BRAIN_AGE=$(( _BRAIN_NOW - _BRAIN_LAST ))
+ [ "$_BRAIN_AGE" -lt 86400 ] && _BRAIN_DO_PULL=0
+ fi
+ if [ "$_BRAIN_DO_PULL" = "1" ]; then
+ ( cd "$_GSTACK_HOME" && git fetch origin >/dev/null 2>&1 && git merge --ff-only "origin/$(git rev-parse --abbrev-ref HEAD)" >/dev/null 2>&1 ) || true
+ echo "$_BRAIN_NOW" > "$_BRAIN_LAST_PULL_FILE"
+ fi
+ # Drain pending queue, push.
+ "$_BRAIN_SYNC_BIN" --once 2>/dev/null || true
+fi
+
+# Status line — always emitted, easy to grep.
+if [ -d "$_GSTACK_HOME/.git" ] && [ "$_BRAIN_SYNC_MODE" != "off" ]; then
+ _BRAIN_QUEUE_DEPTH=0
+ [ -f "$_GSTACK_HOME/.brain-queue.jsonl" ] && _BRAIN_QUEUE_DEPTH=$(wc -l < "$_GSTACK_HOME/.brain-queue.jsonl" | tr -d ' ')
+ _BRAIN_LAST_PUSH="never"
+ [ -f "$_GSTACK_HOME/.brain-last-push" ] && _BRAIN_LAST_PUSH=$(cat "$_GSTACK_HOME/.brain-last-push" 2>/dev/null || echo never)
+ echo "BRAIN_SYNC: mode=$_BRAIN_SYNC_MODE | last_push=$_BRAIN_LAST_PUSH | queue=$_BRAIN_QUEUE_DEPTH"
+else
+ echo "BRAIN_SYNC: off"
+fi
+```
+
+
+
+**Privacy stop-gate (fires ONCE per machine).**
+
+If the bash output shows `BRAIN_SYNC: off` AND the config value
+`gbrain_sync_mode_prompted` is `false` AND gbrain is detected on this host
+(either `gbrain doctor --fast --json` succeeds or the `gbrain` binary is in PATH),
+fire a one-time privacy gate via AskUserQuestion:
+
+> gstack can publish your session memory (learnings, plans, designs, retros) to a
+> private GitHub repo that GBrain indexes across your machines. Higher tiers
+> include behavioral data (session timelines, developer profile). How much do you
+> want to sync?
+
+Options:
+- A) Everything allowlisted (recommended — maximum cross-machine memory)
+- B) Only artifacts (plans, designs, retros, learnings) — skip timelines and profile
+- C) Decline — keep everything local
+
+After the user answers, run (substituting the chosen value):
+
+```bash
+# Chosen mode: full | artifacts-only | off
+"$_BRAIN_CONFIG_BIN" set gbrain_sync_mode <choice>
+"$_BRAIN_CONFIG_BIN" set gbrain_sync_mode_prompted true
+```
+
+If A or B was chosen AND `~/.gstack/.git` doesn't exist, ask a follow-up:
+"Set up the GBrain sync repo now? (runs `gstack-brain-init`)"
+- A) Yes, run it now
+- B) Show me the command, I'll run it myself
+
+Do not block the skill. Emit the question, continue the skill workflow. The
+next skill run picks up wherever this left off.
+
+**At skill END (before the telemetry block),** run these bash commands to
+catch artifact writes (design docs, plans, retros) that skipped the writer
+shims, plus drain any still-pending queue entries:
+
+```bash
+"~/.claude/skills/gstack/bin/gstack-brain-sync" --discover-new 2>/dev/null || true
+"~/.claude/skills/gstack/bin/gstack-brain-sync" --once 2>/dev/null || true
+```
+
+
## Model-Specific Behavioral Patch (claude)
The following nudges are tuned for the claude model family. They are
diff --git a/retro/SKILL.md b/retro/SKILL.md
index 4dc9d4f41..59e4d8c62 100644
--- a/retro/SKILL.md
+++ b/retro/SKILL.md
@@ -350,6 +350,105 @@ AI orchestrator (e.g., OpenClaw). In spawned sessions:
- Focus on completing the task and reporting results via prose output.
- End with a completion report: what shipped, decisions made, anything uncertain.
+## GBrain Sync (skill start)
+
+```bash
+# gbrain-sync: drain pending writes, pull once per day. Silent no-op when
+# the feature isn't initialized or gbrain_sync_mode is "off". See
+# docs/gbrain-sync.md.
+
+_GSTACK_HOME="${GSTACK_HOME:-$HOME/.gstack}"
+_BRAIN_REMOTE_FILE="$HOME/.gstack-brain-remote.txt"
+_BRAIN_SYNC_BIN="~/.claude/skills/gstack/bin/gstack-brain-sync"
+_BRAIN_CONFIG_BIN="~/.claude/skills/gstack/bin/gstack-config"
+
+_BRAIN_SYNC_MODE=$("$_BRAIN_CONFIG_BIN" get gbrain_sync_mode 2>/dev/null || echo off)
+
+# New-machine hint: URL file present, local .git missing, sync not yet enabled.
+if [ -f "$_BRAIN_REMOTE_FILE" ] && [ ! -d "$_GSTACK_HOME/.git" ] && [ "$_BRAIN_SYNC_MODE" = "off" ]; then
+ _BRAIN_NEW_URL=$(head -1 "$_BRAIN_REMOTE_FILE" 2>/dev/null | tr -d '[:space:]')
+ if [ -n "$_BRAIN_NEW_URL" ]; then
+ echo "BRAIN_SYNC: brain repo detected: $_BRAIN_NEW_URL"
+ echo "BRAIN_SYNC: run 'gstack-brain-restore' to pull your cross-machine memory (or 'gstack-config set gbrain_sync_mode off' to dismiss forever)"
+ fi
+fi
+
+# Active-sync path.
+if [ -d "$_GSTACK_HOME/.git" ] && [ "$_BRAIN_SYNC_MODE" != "off" ]; then
+ # Once-per-day pull.
+ _BRAIN_LAST_PULL_FILE="$_GSTACK_HOME/.brain-last-pull"
+ _BRAIN_NOW=$(date +%s)
+ _BRAIN_DO_PULL=1
+ if [ -f "$_BRAIN_LAST_PULL_FILE" ]; then
+ _BRAIN_LAST=$(cat "$_BRAIN_LAST_PULL_FILE" 2>/dev/null || echo 0)
+ _BRAIN_AGE=$(( _BRAIN_NOW - _BRAIN_LAST ))
+ [ "$_BRAIN_AGE" -lt 86400 ] && _BRAIN_DO_PULL=0
+ fi
+ if [ "$_BRAIN_DO_PULL" = "1" ]; then
+ ( cd "$_GSTACK_HOME" && git fetch origin >/dev/null 2>&1 && git merge --ff-only "origin/$(git rev-parse --abbrev-ref HEAD)" >/dev/null 2>&1 ) || true
+ echo "$_BRAIN_NOW" > "$_BRAIN_LAST_PULL_FILE"
+ fi
+ # Drain pending queue, push.
+ "$_BRAIN_SYNC_BIN" --once 2>/dev/null || true
+fi
+
+# Status line — always emitted, easy to grep.
+if [ -d "$_GSTACK_HOME/.git" ] && [ "$_BRAIN_SYNC_MODE" != "off" ]; then
+ _BRAIN_QUEUE_DEPTH=0
+ [ -f "$_GSTACK_HOME/.brain-queue.jsonl" ] && _BRAIN_QUEUE_DEPTH=$(wc -l < "$_GSTACK_HOME/.brain-queue.jsonl" | tr -d ' ')
+ _BRAIN_LAST_PUSH="never"
+ [ -f "$_GSTACK_HOME/.brain-last-push" ] && _BRAIN_LAST_PUSH=$(cat "$_GSTACK_HOME/.brain-last-push" 2>/dev/null || echo never)
+ echo "BRAIN_SYNC: mode=$_BRAIN_SYNC_MODE | last_push=$_BRAIN_LAST_PUSH | queue=$_BRAIN_QUEUE_DEPTH"
+else
+ echo "BRAIN_SYNC: off"
+fi
+```
+
+
+
+**Privacy stop-gate (fires ONCE per machine).**
+
+If the bash output shows `BRAIN_SYNC: off` AND the config value
+`gbrain_sync_mode_prompted` is `false` AND gbrain is detected on this host
+(either `gbrain doctor --fast --json` succeeds or the `gbrain` binary is in PATH),
+fire a one-time privacy gate via AskUserQuestion:
+
+> gstack can publish your session memory (learnings, plans, designs, retros) to a
+> private GitHub repo that GBrain indexes across your machines. Higher tiers
+> include behavioral data (session timelines, developer profile). How much do you
+> want to sync?
+
+Options:
+- A) Everything allowlisted (recommended — maximum cross-machine memory)
+- B) Only artifacts (plans, designs, retros, learnings) — skip timelines and profile
+- C) Decline — keep everything local
+
+After the user answers, run (substituting the chosen value):
+
+```bash
+# Chosen mode: full | artifacts-only | off
+"$_BRAIN_CONFIG_BIN" set gbrain_sync_mode <choice>
+"$_BRAIN_CONFIG_BIN" set gbrain_sync_mode_prompted true
+```
+
+If A or B was chosen AND `~/.gstack/.git` doesn't exist, ask a follow-up:
+"Set up the GBrain sync repo now? (runs `gstack-brain-init`)"
+- A) Yes, run it now
+- B) Show me the command, I'll run it myself
+
+Do not block the skill. Emit the question, continue the skill workflow. The
+next skill run picks up wherever this left off.
+
+**At skill END (before the telemetry block),** run these bash commands to
+catch artifact writes (design docs, plans, retros) that skipped the writer
+shims, plus drain any still-pending queue entries:
+
+```bash
+"~/.claude/skills/gstack/bin/gstack-brain-sync" --discover-new 2>/dev/null || true
+"~/.claude/skills/gstack/bin/gstack-brain-sync" --once 2>/dev/null || true
+```
+
+
## Model-Specific Behavioral Patch (claude)
The following nudges are tuned for the claude model family. They are
diff --git a/review/SKILL.md b/review/SKILL.md
index 6b82d502c..6350e65f6 100644
--- a/review/SKILL.md
+++ b/review/SKILL.md
@@ -354,6 +354,105 @@ AI orchestrator (e.g., OpenClaw). In spawned sessions:
- Focus on completing the task and reporting results via prose output.
- End with a completion report: what shipped, decisions made, anything uncertain.
+## GBrain Sync (skill start)
+
+```bash
+# gbrain-sync: drain pending writes, pull once per day. Silent no-op when
+# the feature isn't initialized or gbrain_sync_mode is "off". See
+# docs/gbrain-sync.md.
+
+_GSTACK_HOME="${GSTACK_HOME:-$HOME/.gstack}"
+_BRAIN_REMOTE_FILE="$HOME/.gstack-brain-remote.txt"
+_BRAIN_SYNC_BIN="~/.claude/skills/gstack/bin/gstack-brain-sync"
+_BRAIN_CONFIG_BIN="~/.claude/skills/gstack/bin/gstack-config"
+
+_BRAIN_SYNC_MODE=$("$_BRAIN_CONFIG_BIN" get gbrain_sync_mode 2>/dev/null || echo off)
+
+# New-machine hint: URL file present, local .git missing, sync not yet enabled.
+if [ -f "$_BRAIN_REMOTE_FILE" ] && [ ! -d "$_GSTACK_HOME/.git" ] && [ "$_BRAIN_SYNC_MODE" = "off" ]; then
+ _BRAIN_NEW_URL=$(head -1 "$_BRAIN_REMOTE_FILE" 2>/dev/null | tr -d '[:space:]')
+ if [ -n "$_BRAIN_NEW_URL" ]; then
+ echo "BRAIN_SYNC: brain repo detected: $_BRAIN_NEW_URL"
+ echo "BRAIN_SYNC: run 'gstack-brain-restore' to pull your cross-machine memory (or 'gstack-config set gbrain_sync_mode off' to dismiss forever)"
+ fi
+fi
+
+# Active-sync path.
+if [ -d "$_GSTACK_HOME/.git" ] && [ "$_BRAIN_SYNC_MODE" != "off" ]; then
+ # Once-per-day pull.
+ _BRAIN_LAST_PULL_FILE="$_GSTACK_HOME/.brain-last-pull"
+ _BRAIN_NOW=$(date +%s)
+ _BRAIN_DO_PULL=1
+ if [ -f "$_BRAIN_LAST_PULL_FILE" ]; then
+ _BRAIN_LAST=$(cat "$_BRAIN_LAST_PULL_FILE" 2>/dev/null || echo 0)
+ _BRAIN_AGE=$(( _BRAIN_NOW - _BRAIN_LAST ))
+ [ "$_BRAIN_AGE" -lt 86400 ] && _BRAIN_DO_PULL=0
+ fi
+ if [ "$_BRAIN_DO_PULL" = "1" ]; then
+ ( cd "$_GSTACK_HOME" && git fetch origin >/dev/null 2>&1 && git merge --ff-only "origin/$(git rev-parse --abbrev-ref HEAD)" >/dev/null 2>&1 ) || true
+ echo "$_BRAIN_NOW" > "$_BRAIN_LAST_PULL_FILE"
+ fi
+ # Drain pending queue, push.
+ "$_BRAIN_SYNC_BIN" --once 2>/dev/null || true
+fi
+
+# Status line — always emitted, easy to grep.
+if [ -d "$_GSTACK_HOME/.git" ] && [ "$_BRAIN_SYNC_MODE" != "off" ]; then
+ _BRAIN_QUEUE_DEPTH=0
+ [ -f "$_GSTACK_HOME/.brain-queue.jsonl" ] && _BRAIN_QUEUE_DEPTH=$(wc -l < "$_GSTACK_HOME/.brain-queue.jsonl" | tr -d ' ')
+ _BRAIN_LAST_PUSH="never"
+ [ -f "$_GSTACK_HOME/.brain-last-push" ] && _BRAIN_LAST_PUSH=$(cat "$_GSTACK_HOME/.brain-last-push" 2>/dev/null || echo never)
+ echo "BRAIN_SYNC: mode=$_BRAIN_SYNC_MODE | last_push=$_BRAIN_LAST_PUSH | queue=$_BRAIN_QUEUE_DEPTH"
+else
+ echo "BRAIN_SYNC: off"
+fi
+```
+
+
+
+**Privacy stop-gate (fires ONCE per machine).**
+
+If the bash output shows `BRAIN_SYNC: off` AND the config value
+`gbrain_sync_mode_prompted` is `false` AND gbrain is detected on this host
+(either `gbrain doctor --fast --json` succeeds or the `gbrain` binary is in PATH),
+fire a one-time privacy gate via AskUserQuestion:
+
+> gstack can publish your session memory (learnings, plans, designs, retros) to a
+> private GitHub repo that GBrain indexes across your machines. Higher tiers
+> include behavioral data (session timelines, developer profile). How much do you
+> want to sync?
+
+Options:
+- A) Everything allowlisted (recommended — maximum cross-machine memory)
+- B) Only artifacts (plans, designs, retros, learnings) — skip timelines and profile
+- C) Decline — keep everything local
+
+After the user answers, run (substituting the chosen value):
+
+```bash
+# Chosen mode: full | artifacts-only | off
+"$_BRAIN_CONFIG_BIN" set gbrain_sync_mode <choice>
+"$_BRAIN_CONFIG_BIN" set gbrain_sync_mode_prompted true
+```
+
+If A or B was chosen AND `~/.gstack/.git` doesn't exist, ask a follow-up:
+"Set up the GBrain sync repo now? (runs `gstack-brain-init`)"
+- A) Yes, run it now
+- B) Show me the command, I'll run it myself
+
+Do not block the skill. Emit the question, continue the skill workflow. The
+next skill run picks up wherever this left off.
+
+**At skill END (before the telemetry block),** run these bash commands to
+catch artifact writes (design docs, plans, retros) that skipped the writer
+shims, plus drain any still-pending queue entries:
+
+```bash
+"~/.claude/skills/gstack/bin/gstack-brain-sync" --discover-new 2>/dev/null || true
+"~/.claude/skills/gstack/bin/gstack-brain-sync" --once 2>/dev/null || true
+```
+
+
## Model-Specific Behavioral Patch (claude)
The following nudges are tuned for the claude model family. They are
diff --git a/scripts/resolvers/preamble.ts b/scripts/resolvers/preamble.ts
index dd490202d..533864fcf 100644
--- a/scripts/resolvers/preamble.ts
+++ b/scripts/resolvers/preamble.ts
@@ -37,6 +37,9 @@ import { generateWritingStyleMigration } from './preamble/generate-writing-style
// Host-specific instructions
import { generateBrainHealthInstruction } from './preamble/generate-brain-health-instruction';
+// GBrain cross-machine sync (runs at skill start; end-side handled in completion-status)
+import { generateBrainSyncBlock } from './preamble/generate-brain-sync-block';
+
// Behavioral / voice
import { generateVoiceDirective } from './preamble/generate-voice-directive';
@@ -84,6 +87,7 @@ export function generatePreamble(ctx: TemplateContext): string {
generateVendoringDeprecation(ctx),
generateSpawnedSessionCheck(),
generateBrainHealthInstruction(ctx),
+ generateBrainSyncBlock(ctx),
generateModelOverlay(ctx),
generateVoiceDirective(tier),
...(tier >= 2 ? [
diff --git a/scripts/resolvers/preamble/generate-brain-sync-block.ts b/scripts/resolvers/preamble/generate-brain-sync-block.ts
new file mode 100644
index 000000000..6a378e588
--- /dev/null
+++ b/scripts/resolvers/preamble/generate-brain-sync-block.ts
@@ -0,0 +1,124 @@
+/**
+ * gbrain-sync preamble block.
+ *
+ * Emits bash that runs at every skill invocation:
+ * 1. If ~/.gstack-brain-remote.txt exists AND ~/.gstack/.git is missing,
+ * surface a restore-available hint (does NOT auto-run restore).
+ * 2. If sync is on, run `gstack-brain-sync --once` (drain + push).
+ * 3. On first skill of the day (24h cache via .brain-last-pull):
+ * `git fetch` + ff-only merge (JSONL merge driver handles conflicts).
+ * 4. Emit a `BRAIN_SYNC:` status line so every skill surfaces health.
+ *
+ * Also emits prose instructions for the host LLM to fire a one-time privacy
+ * stop-gate via AskUserQuestion when gbrain_sync_mode is unset and gbrain
+ * is available on the host.
+ *
+ * Block emitted across all tiers. Internal bash short-circuits when feature
+ * is disabled; cost is <5ms.
+ *
+ * Skill-end sync is handled by the completion-status generator via a call
+ * to `gstack-brain-sync --discover-new` + `--once`.
+ */
+import type { TemplateContext } from '../types';
+
+export function generateBrainSyncBlock(ctx: TemplateContext): string {
+ const isBrainHost = ctx.host === 'gbrain' || ctx.host === 'hermes';
+ return `## GBrain Sync (skill start)
+
+\`\`\`bash
+# gbrain-sync: drain pending writes, pull once per day. Silent no-op when
+# the feature isn't initialized or gbrain_sync_mode is "off". See
+# docs/gbrain-sync.md.
+
+_GSTACK_HOME="\${GSTACK_HOME:-$HOME/.gstack}"
+_BRAIN_REMOTE_FILE="$HOME/.gstack-brain-remote.txt"
+_BRAIN_SYNC_BIN="${ctx.paths.binDir}/gstack-brain-sync"
+_BRAIN_CONFIG_BIN="${ctx.paths.binDir}/gstack-config"
+
+_BRAIN_SYNC_MODE=$("$_BRAIN_CONFIG_BIN" get gbrain_sync_mode 2>/dev/null || echo off)
+
+# New-machine hint: URL file present, local .git missing, sync not yet enabled.
+if [ -f "$_BRAIN_REMOTE_FILE" ] && [ ! -d "$_GSTACK_HOME/.git" ] && [ "$_BRAIN_SYNC_MODE" = "off" ]; then
+ _BRAIN_NEW_URL=$(head -1 "$_BRAIN_REMOTE_FILE" 2>/dev/null | tr -d '[:space:]')
+ if [ -n "$_BRAIN_NEW_URL" ]; then
+ echo "BRAIN_SYNC: brain repo detected: $_BRAIN_NEW_URL"
+ echo "BRAIN_SYNC: run 'gstack-brain-restore' to pull your cross-machine memory (or 'gstack-config set gbrain_sync_mode off' to dismiss forever)"
+ fi
+fi
+
+# Active-sync path.
+if [ -d "$_GSTACK_HOME/.git" ] && [ "$_BRAIN_SYNC_MODE" != "off" ]; then
+ # Once-per-day pull.
+ _BRAIN_LAST_PULL_FILE="$_GSTACK_HOME/.brain-last-pull"
+ _BRAIN_NOW=$(date +%s)
+ _BRAIN_DO_PULL=1
+ if [ -f "$_BRAIN_LAST_PULL_FILE" ]; then
+ _BRAIN_LAST=$(cat "$_BRAIN_LAST_PULL_FILE" 2>/dev/null || echo 0)
+ _BRAIN_AGE=$(( _BRAIN_NOW - _BRAIN_LAST ))
+ [ "$_BRAIN_AGE" -lt 86400 ] && _BRAIN_DO_PULL=0
+ fi
+ if [ "$_BRAIN_DO_PULL" = "1" ]; then
+ ( cd "$_GSTACK_HOME" && git fetch origin >/dev/null 2>&1 && git merge --ff-only "origin/$(git rev-parse --abbrev-ref HEAD)" >/dev/null 2>&1 ) || true
+ echo "$_BRAIN_NOW" > "$_BRAIN_LAST_PULL_FILE"
+ fi
+ # Drain pending queue, push.
+ "$_BRAIN_SYNC_BIN" --once 2>/dev/null || true
+fi
+
+# Status line — always emitted, easy to grep.
+if [ -d "$_GSTACK_HOME/.git" ] && [ "$_BRAIN_SYNC_MODE" != "off" ]; then
+ _BRAIN_QUEUE_DEPTH=0
+ [ -f "$_GSTACK_HOME/.brain-queue.jsonl" ] && _BRAIN_QUEUE_DEPTH=$(wc -l < "$_GSTACK_HOME/.brain-queue.jsonl" | tr -d ' ')
+ _BRAIN_LAST_PUSH="never"
+ [ -f "$_GSTACK_HOME/.brain-last-push" ] && _BRAIN_LAST_PUSH=$(cat "$_GSTACK_HOME/.brain-last-push" 2>/dev/null || echo never)
+ echo "BRAIN_SYNC: mode=$_BRAIN_SYNC_MODE | last_push=$_BRAIN_LAST_PUSH | queue=$_BRAIN_QUEUE_DEPTH"
+else
+ echo "BRAIN_SYNC: off"
+fi
+\`\`\`
+
+${isBrainHost ? `If the bash output shows \`BRAIN_SYNC: brain repo detected\`, the user copied their remote URL file to this machine but hasn't restored yet. Offer to run \`gstack-brain-restore\` via AskUserQuestion. If the user agrees, run the command; otherwise continue without sync.` : ''}
+
+**Privacy stop-gate (fires ONCE per machine).**
+
+If the bash output shows \`BRAIN_SYNC: off\` AND the config value
+\`gbrain_sync_mode_prompted\` is \`false\` AND gbrain is detected on this host
+(either \`gbrain doctor --fast --json\` succeeds or the \`gbrain\` binary is in PATH),
+fire a one-time privacy gate via AskUserQuestion:
+
+> gstack can publish your session memory (learnings, plans, designs, retros) to a
+> private GitHub repo that GBrain indexes across your machines. Higher tiers
+> include behavioral data (session timelines, developer profile). How much do you
+> want to sync?
+
+Options:
+- A) Everything allowlisted (recommended — maximum cross-machine memory)
+- B) Only artifacts (plans, designs, retros, learnings) — skip timelines and profile
+- C) Decline — keep everything local
+
+After the user answers, run (substituting the chosen value):
+
+\`\`\`bash
+# Chosen mode: full | artifacts-only | off
+"$_BRAIN_CONFIG_BIN" set gbrain_sync_mode <choice>
+"$_BRAIN_CONFIG_BIN" set gbrain_sync_mode_prompted true
+\`\`\`
+
+If A or B was chosen AND \`~/.gstack/.git\` doesn't exist, ask a follow-up:
+"Set up the GBrain sync repo now? (runs \`gstack-brain-init\`)"
+- A) Yes, run it now
+- B) Show me the command, I'll run it myself
+
+Do not block the skill. Emit the question, continue the skill workflow. The
+next skill run picks up wherever this left off.
+
+**At skill END (before the telemetry block),** run these bash commands to
+catch artifact writes (design docs, plans, retros) that skipped the writer
+shims, plus drain any still-pending queue entries:
+
+\`\`\`bash
+"${ctx.paths.binDir}/gstack-brain-sync" --discover-new 2>/dev/null || true
+"${ctx.paths.binDir}/gstack-brain-sync" --once 2>/dev/null || true
+\`\`\`
+`;
+}
diff --git a/setup-browser-cookies/SKILL.md b/setup-browser-cookies/SKILL.md
index 3b0160e04..64890e098 100644
--- a/setup-browser-cookies/SKILL.md
+++ b/setup-browser-cookies/SKILL.md
@@ -347,6 +347,105 @@ AI orchestrator (e.g., OpenClaw). In spawned sessions:
- Focus on completing the task and reporting results via prose output.
- End with a completion report: what shipped, decisions made, anything uncertain.
+## GBrain Sync (skill start)
+
+```bash
+# gbrain-sync: drain pending writes, pull once per day. Silent no-op when
+# the feature isn't initialized or gbrain_sync_mode is "off". See
+# docs/gbrain-sync.md.
+
+_GSTACK_HOME="${GSTACK_HOME:-$HOME/.gstack}"
+_BRAIN_REMOTE_FILE="$HOME/.gstack-brain-remote.txt"
+_BRAIN_SYNC_BIN="~/.claude/skills/gstack/bin/gstack-brain-sync"
+_BRAIN_CONFIG_BIN="~/.claude/skills/gstack/bin/gstack-config"
+
+_BRAIN_SYNC_MODE=$("$_BRAIN_CONFIG_BIN" get gbrain_sync_mode 2>/dev/null || echo off)
+
+# New-machine hint: URL file present, local .git missing, sync not yet enabled.
+if [ -f "$_BRAIN_REMOTE_FILE" ] && [ ! -d "$_GSTACK_HOME/.git" ] && [ "$_BRAIN_SYNC_MODE" = "off" ]; then
+ _BRAIN_NEW_URL=$(head -1 "$_BRAIN_REMOTE_FILE" 2>/dev/null | tr -d '[:space:]')
+ if [ -n "$_BRAIN_NEW_URL" ]; then
+ echo "BRAIN_SYNC: brain repo detected: $_BRAIN_NEW_URL"
+ echo "BRAIN_SYNC: run 'gstack-brain-restore' to pull your cross-machine memory (or 'gstack-config set gbrain_sync_mode off' to dismiss forever)"
+ fi
+fi
+
+# Active-sync path.
+if [ -d "$_GSTACK_HOME/.git" ] && [ "$_BRAIN_SYNC_MODE" != "off" ]; then
+ # Once-per-day pull.
+ _BRAIN_LAST_PULL_FILE="$_GSTACK_HOME/.brain-last-pull"
+ _BRAIN_NOW=$(date +%s)
+ _BRAIN_DO_PULL=1
+ if [ -f "$_BRAIN_LAST_PULL_FILE" ]; then
+ _BRAIN_LAST=$(cat "$_BRAIN_LAST_PULL_FILE" 2>/dev/null || echo 0)
+ _BRAIN_AGE=$(( _BRAIN_NOW - _BRAIN_LAST ))
+ [ "$_BRAIN_AGE" -lt 86400 ] && _BRAIN_DO_PULL=0
+ fi
+ if [ "$_BRAIN_DO_PULL" = "1" ]; then
+ ( cd "$_GSTACK_HOME" && git fetch origin >/dev/null 2>&1 && git merge --ff-only "origin/$(git rev-parse --abbrev-ref HEAD)" >/dev/null 2>&1 ) || true
+ echo "$_BRAIN_NOW" > "$_BRAIN_LAST_PULL_FILE"
+ fi
+ # Drain pending queue, push.
+ "$_BRAIN_SYNC_BIN" --once 2>/dev/null || true
+fi
+
+# Status line — always emitted, easy to grep.
+if [ -d "$_GSTACK_HOME/.git" ] && [ "$_BRAIN_SYNC_MODE" != "off" ]; then
+ _BRAIN_QUEUE_DEPTH=0
+ [ -f "$_GSTACK_HOME/.brain-queue.jsonl" ] && _BRAIN_QUEUE_DEPTH=$(wc -l < "$_GSTACK_HOME/.brain-queue.jsonl" | tr -d ' ')
+ _BRAIN_LAST_PUSH="never"
+ [ -f "$_GSTACK_HOME/.brain-last-push" ] && _BRAIN_LAST_PUSH=$(cat "$_GSTACK_HOME/.brain-last-push" 2>/dev/null || echo never)
+ echo "BRAIN_SYNC: mode=$_BRAIN_SYNC_MODE | last_push=$_BRAIN_LAST_PUSH | queue=$_BRAIN_QUEUE_DEPTH"
+else
+ echo "BRAIN_SYNC: off"
+fi
+```
+
+
+
+**Privacy stop-gate (fires ONCE per machine).**
+
+If the bash output shows `BRAIN_SYNC: off` AND the config value
+`gbrain_sync_mode_prompted` is `false` AND gbrain is detected on this host
+(either `gbrain doctor --fast --json` succeeds or the `gbrain` binary is in PATH),
+fire a one-time privacy gate via AskUserQuestion:
+
+> gstack can publish your session memory (learnings, plans, designs, retros) to a
+> private GitHub repo that GBrain indexes across your machines. Higher tiers
+> include behavioral data (session timelines, developer profile). How much do you
+> want to sync?
+
+Options:
+- A) Everything allowlisted (recommended — maximum cross-machine memory)
+- B) Only artifacts (plans, designs, retros, learnings) — skip timelines and profile
+- C) Decline — keep everything local
+
+After the user answers, run (substituting the chosen value):
+
+```bash
+# Chosen mode: full | artifacts-only | off
+"$_BRAIN_CONFIG_BIN" set gbrain_sync_mode <choice>
+"$_BRAIN_CONFIG_BIN" set gbrain_sync_mode_prompted true
+```
+
+If A or B was chosen AND `~/.gstack/.git` doesn't exist, ask a follow-up:
+"Set up the GBrain sync repo now? (runs `gstack-brain-init`)"
+- A) Yes, run it now
+- B) Show me the command, I'll run it myself
+
+Do not block the skill. Emit the question, continue the skill workflow. The
+next skill run picks up wherever this left off.
+
+**At skill END (before the telemetry block),** run these bash commands to
+catch artifact writes (design docs, plans, retros) that skipped the writer
+shims, plus drain any still-pending queue entries:
+
+```bash
+"~/.claude/skills/gstack/bin/gstack-brain-sync" --discover-new 2>/dev/null || true
+"~/.claude/skills/gstack/bin/gstack-brain-sync" --once 2>/dev/null || true
+```
+
+
## Model-Specific Behavioral Patch (claude)
The following nudges are tuned for the claude model family. They are
diff --git a/setup-deploy/SKILL.md b/setup-deploy/SKILL.md
index 1cd1a507b..a1e52f744 100644
--- a/setup-deploy/SKILL.md
+++ b/setup-deploy/SKILL.md
@@ -353,6 +353,105 @@ AI orchestrator (e.g., OpenClaw). In spawned sessions:
- Focus on completing the task and reporting results via prose output.
- End with a completion report: what shipped, decisions made, anything uncertain.
+## GBrain Sync (skill start)
+
+```bash
+# gbrain-sync: drain pending writes, pull once per day. Silent no-op when
+# the feature isn't initialized or gbrain_sync_mode is "off". See
+# docs/gbrain-sync.md.
+
+_GSTACK_HOME="${GSTACK_HOME:-$HOME/.gstack}"
+_BRAIN_REMOTE_FILE="$HOME/.gstack-brain-remote.txt"
+_BRAIN_SYNC_BIN="~/.claude/skills/gstack/bin/gstack-brain-sync"
+_BRAIN_CONFIG_BIN="~/.claude/skills/gstack/bin/gstack-config"
+
+_BRAIN_SYNC_MODE=$("$_BRAIN_CONFIG_BIN" get gbrain_sync_mode 2>/dev/null || echo off)
+
+# New-machine hint: URL file present, local .git missing, sync not yet enabled.
+if [ -f "$_BRAIN_REMOTE_FILE" ] && [ ! -d "$_GSTACK_HOME/.git" ] && [ "$_BRAIN_SYNC_MODE" = "off" ]; then
+ _BRAIN_NEW_URL=$(head -1 "$_BRAIN_REMOTE_FILE" 2>/dev/null | tr -d '[:space:]')
+ if [ -n "$_BRAIN_NEW_URL" ]; then
+ echo "BRAIN_SYNC: brain repo detected: $_BRAIN_NEW_URL"
+ echo "BRAIN_SYNC: run 'gstack-brain-restore' to pull your cross-machine memory (or 'gstack-config set gbrain_sync_mode off' to dismiss forever)"
+ fi
+fi
+
+# Active-sync path.
+if [ -d "$_GSTACK_HOME/.git" ] && [ "$_BRAIN_SYNC_MODE" != "off" ]; then
+ # Once-per-day pull.
+ _BRAIN_LAST_PULL_FILE="$_GSTACK_HOME/.brain-last-pull"
+ _BRAIN_NOW=$(date +%s)
+ _BRAIN_DO_PULL=1
+ if [ -f "$_BRAIN_LAST_PULL_FILE" ]; then
+ _BRAIN_LAST=$(cat "$_BRAIN_LAST_PULL_FILE" 2>/dev/null || echo 0)
+ _BRAIN_AGE=$(( _BRAIN_NOW - _BRAIN_LAST ))
+ [ "$_BRAIN_AGE" -lt 86400 ] && _BRAIN_DO_PULL=0
+ fi
+ if [ "$_BRAIN_DO_PULL" = "1" ]; then
+ ( cd "$_GSTACK_HOME" && git fetch origin >/dev/null 2>&1 && git merge --ff-only "origin/$(git rev-parse --abbrev-ref HEAD)" >/dev/null 2>&1 ) || true
+ echo "$_BRAIN_NOW" > "$_BRAIN_LAST_PULL_FILE"
+ fi
+ # Drain pending queue, push.
+ "$_BRAIN_SYNC_BIN" --once 2>/dev/null || true
+fi
+
+# Status line — always emitted, easy to grep.
+if [ -d "$_GSTACK_HOME/.git" ] && [ "$_BRAIN_SYNC_MODE" != "off" ]; then
+ _BRAIN_QUEUE_DEPTH=0
+ [ -f "$_GSTACK_HOME/.brain-queue.jsonl" ] && _BRAIN_QUEUE_DEPTH=$(wc -l < "$_GSTACK_HOME/.brain-queue.jsonl" | tr -d ' ')
+ _BRAIN_LAST_PUSH="never"
+ [ -f "$_GSTACK_HOME/.brain-last-push" ] && _BRAIN_LAST_PUSH=$(cat "$_GSTACK_HOME/.brain-last-push" 2>/dev/null || echo never)
+ echo "BRAIN_SYNC: mode=$_BRAIN_SYNC_MODE | last_push=$_BRAIN_LAST_PUSH | queue=$_BRAIN_QUEUE_DEPTH"
+else
+ echo "BRAIN_SYNC: off"
+fi
+```
+
+
+
+**Privacy stop-gate (fires ONCE per machine).**
+
+If the bash output shows `BRAIN_SYNC: off` AND the config value
+`gbrain_sync_mode_prompted` is `false` AND gbrain is detected on this host
+(either `gbrain doctor --fast --json` succeeds or the `gbrain` binary is in PATH),
+fire a one-time privacy gate via AskUserQuestion:
+
+> gstack can publish your session memory (learnings, plans, designs, retros) to a
+> private GitHub repo that GBrain indexes across your machines. Higher tiers
+> include behavioral data (session timelines, developer profile). How much do you
+> want to sync?
+
+Options:
+- A) Everything allowlisted (recommended — maximum cross-machine memory)
+- B) Only artifacts (plans, designs, retros, learnings) — skip timelines and profile
+- C) Decline — keep everything local
+
+After the user answers, run (substituting the chosen value):
+
+```bash
+# Chosen mode: full | artifacts-only | off
+"$_BRAIN_CONFIG_BIN" set gbrain_sync_mode <choice>
+"$_BRAIN_CONFIG_BIN" set gbrain_sync_mode_prompted true
+```
+
+If A or B was chosen AND `~/.gstack/.git` doesn't exist, ask a follow-up:
+"Set up the GBrain sync repo now? (runs `gstack-brain-init`)"
+- A) Yes, run it now
+- B) Show me the command, I'll run it myself
+
+Do not block the skill. Emit the question, continue the skill workflow. The
+next skill run picks up wherever this left off.
+
+**At skill END (before the telemetry block),** run these bash commands to
+catch artifact writes (design docs, plans, retros) that skipped the writer
+shims, plus drain any still-pending queue entries:
+
+```bash
+"~/.claude/skills/gstack/bin/gstack-brain-sync" --discover-new 2>/dev/null || true
+"~/.claude/skills/gstack/bin/gstack-brain-sync" --once 2>/dev/null || true
+```
+
+
## Model-Specific Behavioral Patch (claude)
The following nudges are tuned for the claude model family. They are
diff --git a/ship/SKILL.md b/ship/SKILL.md
index e56262ed5..02a787831 100644
--- a/ship/SKILL.md
+++ b/ship/SKILL.md
@@ -355,6 +355,105 @@ AI orchestrator (e.g., OpenClaw). In spawned sessions:
- Focus on completing the task and reporting results via prose output.
- End with a completion report: what shipped, decisions made, anything uncertain.
+## GBrain Sync (skill start)
+
+```bash
+# gbrain-sync: drain pending writes, pull once per day. Silent no-op when
+# the feature isn't initialized or gbrain_sync_mode is "off". See
+# docs/gbrain-sync.md.
+
+_GSTACK_HOME="${GSTACK_HOME:-$HOME/.gstack}"
+_BRAIN_REMOTE_FILE="$HOME/.gstack-brain-remote.txt"
+_BRAIN_SYNC_BIN="~/.claude/skills/gstack/bin/gstack-brain-sync"
+_BRAIN_CONFIG_BIN="~/.claude/skills/gstack/bin/gstack-config"
+
+_BRAIN_SYNC_MODE=$("$_BRAIN_CONFIG_BIN" get gbrain_sync_mode 2>/dev/null || echo off)
+
+# New-machine hint: URL file present, local .git missing, sync not yet enabled.
+if [ -f "$_BRAIN_REMOTE_FILE" ] && [ ! -d "$_GSTACK_HOME/.git" ] && [ "$_BRAIN_SYNC_MODE" = "off" ]; then
+ _BRAIN_NEW_URL=$(head -1 "$_BRAIN_REMOTE_FILE" 2>/dev/null | tr -d '[:space:]')
+ if [ -n "$_BRAIN_NEW_URL" ]; then
+ echo "BRAIN_SYNC: brain repo detected: $_BRAIN_NEW_URL"
+ echo "BRAIN_SYNC: run 'gstack-brain-restore' to pull your cross-machine memory (or 'gstack-config set gbrain_sync_mode off' to dismiss forever)"
+ fi
+fi
+
+# Active-sync path.
+if [ -d "$_GSTACK_HOME/.git" ] && [ "$_BRAIN_SYNC_MODE" != "off" ]; then
+ # Once-per-day pull.
+ _BRAIN_LAST_PULL_FILE="$_GSTACK_HOME/.brain-last-pull"
+ _BRAIN_NOW=$(date +%s)
+ _BRAIN_DO_PULL=1
+ if [ -f "$_BRAIN_LAST_PULL_FILE" ]; then
+ _BRAIN_LAST=$(cat "$_BRAIN_LAST_PULL_FILE" 2>/dev/null || echo 0)
+ _BRAIN_AGE=$(( _BRAIN_NOW - _BRAIN_LAST ))
+ [ "$_BRAIN_AGE" -lt 86400 ] && _BRAIN_DO_PULL=0
+ fi
+ if [ "$_BRAIN_DO_PULL" = "1" ]; then
+ ( cd "$_GSTACK_HOME" && git fetch origin >/dev/null 2>&1 && git merge --ff-only "origin/$(git rev-parse --abbrev-ref HEAD)" >/dev/null 2>&1 ) || true
+ echo "$_BRAIN_NOW" > "$_BRAIN_LAST_PULL_FILE"
+ fi
+ # Drain pending queue, push.
+ "$_BRAIN_SYNC_BIN" --once 2>/dev/null || true
+fi
+
+# Status line — always emitted, easy to grep.
+if [ -d "$_GSTACK_HOME/.git" ] && [ "$_BRAIN_SYNC_MODE" != "off" ]; then
+ _BRAIN_QUEUE_DEPTH=0
+ [ -f "$_GSTACK_HOME/.brain-queue.jsonl" ] && _BRAIN_QUEUE_DEPTH=$(wc -l < "$_GSTACK_HOME/.brain-queue.jsonl" | tr -d ' ')
+ _BRAIN_LAST_PUSH="never"
+ [ -f "$_GSTACK_HOME/.brain-last-push" ] && _BRAIN_LAST_PUSH=$(cat "$_GSTACK_HOME/.brain-last-push" 2>/dev/null || echo never)
+ echo "BRAIN_SYNC: mode=$_BRAIN_SYNC_MODE | last_push=$_BRAIN_LAST_PUSH | queue=$_BRAIN_QUEUE_DEPTH"
+else
+ echo "BRAIN_SYNC: off"
+fi
+```
+
+
+
+**Privacy stop-gate (fires ONCE per machine).**
+
+If the bash output shows `BRAIN_SYNC: off` AND the config value
+`gbrain_sync_mode_prompted` is `false` AND gbrain is detected on this host
+(either `gbrain doctor --fast --json` succeeds or the `gbrain` binary is in PATH),
+fire a one-time privacy gate via AskUserQuestion:
+
+> gstack can publish your session memory (learnings, plans, designs, retros) to a
+> private GitHub repo that GBrain indexes across your machines. Higher tiers
+> include behavioral data (session timelines, developer profile). How much do you
+> want to sync?
+
+Options:
+- A) Everything allowlisted (recommended — maximum cross-machine memory)
+- B) Only artifacts (plans, designs, retros, learnings) — skip timelines and profile
+- C) Decline — keep everything local
+
+After the user answers, run (substituting the chosen value):
+
+```bash
+# Chosen mode: full | artifacts-only | off
+"$_BRAIN_CONFIG_BIN" set gbrain_sync_mode <choice>
+"$_BRAIN_CONFIG_BIN" set gbrain_sync_mode_prompted true
+```
+
+If A or B was chosen AND `~/.gstack/.git` doesn't exist, ask a follow-up:
+"Set up the GBrain sync repo now? (runs `gstack-brain-init`)"
+- A) Yes, run it now
+- B) Show me the command, I'll run it myself
+
+Do not block the skill. Emit the question, continue the skill workflow. The
+next skill run picks up wherever this left off.
+
+**At skill END (before the telemetry block),** run these bash commands to
+catch artifact writes (design docs, plans, retros) that skipped the writer
+shims, plus drain any still-pending queue entries:
+
+```bash
+"~/.claude/skills/gstack/bin/gstack-brain-sync" --discover-new 2>/dev/null || true
+"~/.claude/skills/gstack/bin/gstack-brain-sync" --once 2>/dev/null || true
+```
+
+
## Model-Specific Behavioral Patch (claude)
The following nudges are tuned for the claude model family. They are
diff --git a/test/brain-sync.test.ts b/test/brain-sync.test.ts
new file mode 100644
index 000000000..6ba8e95ca
--- /dev/null
+++ b/test/brain-sync.test.ts
@@ -0,0 +1,366 @@
+/**
+ * gbrain-sync integration tests.
+ *
+ * Covers the core cross-machine memory sync feature end-to-end:
+ * - bin/gstack-config gbrain keys (validation, isolation)
+ * - bin/gstack-brain-enqueue (atomicity, skip list, no-op gates)
+ * - bin/gstack-jsonl-merge (3-way, ts-sort, hash-fallback)
+ * - bin/gstack-brain-sync --once (drain, commit, push, secret-scan, skip-file)
+ * - bin/gstack-brain-init + --restore round-trip
+ * - bin/gstack-brain-uninstall preserves user data
+ * - env isolation (GSTACK_HOME never bleeds into real ~/.gstack/config.yaml)
+ *
+ * Runs each test against a temp GSTACK_HOME and a local bare git repo as
+ * a fake remote. No live GitHub, no live GBrain.
+ */
+
+import { describe, test as _test, expect, beforeEach, afterEach } from 'bun:test';
+
+// Boost timeout: brain-sync tests spawn git, network-ls-remote, and 10-way
+// parallel processes — 5s default is too tight.
+const test = (name: string, fn: any) => _test(name, fn, 30000);
+import * as fs from 'fs';
+import * as path from 'path';
+import * as os from 'os';
+import { spawnSync } from 'child_process';
+
+const ROOT = path.resolve(import.meta.dir, '..');
+const BIN = path.join(ROOT, 'bin');
+
+let tmpHome: string;
+let bareRemote: string;
+
+function run(argv: string[], opts: { env?: Record<string, string>; input?: string } = {}) {
+ const bin = argv[0];
+ const full = bin.startsWith('/') ? bin : path.join(BIN, bin);
+ const res = spawnSync(full, argv.slice(1), {
+ env: { ...process.env, GSTACK_HOME: tmpHome, ...(opts.env || {}) },
+ encoding: 'utf-8',
+ input: opts.input,
+ cwd: ROOT,
+ });
+ return { stdout: res.stdout || '', stderr: res.stderr || '', status: res.status ?? -1 };
+}
+
+function git(args: string[], cwd?: string) {
+ const res = spawnSync('git', args, { cwd: cwd || tmpHome, encoding: 'utf-8' });
+ return { stdout: res.stdout || '', stderr: res.stderr || '', status: res.status ?? -1 };
+}
+
+beforeEach(() => {
+ tmpHome = fs.mkdtempSync(path.join(os.tmpdir(), 'brain-sync-home-'));
+ bareRemote = fs.mkdtempSync(path.join(os.tmpdir(), 'brain-sync-remote-'));
+ spawnSync('git', ['init', '--bare', '-q', '-b', 'main', bareRemote]);
+});
+
+afterEach(() => {
+ fs.rmSync(tmpHome, { recursive: true, force: true });
+ fs.rmSync(bareRemote, { recursive: true, force: true });
+ // Clean up any remote-helper file init may have written.
+ const remoteFile = path.join(os.homedir(), '.gstack-brain-remote.txt');
+ // Only remove if it points at OUR bare remote (don't clobber a real user file).
+ try {
+ const contents = fs.readFileSync(remoteFile, 'utf-8').trim();
+ if (contents === bareRemote) fs.unlinkSync(remoteFile);
+ } catch {}
+});
+
+// ---------------------------------------------------------------
+// Config key validation + env isolation
+// ---------------------------------------------------------------
+describe('gstack-config gbrain keys', () => {
+ test('default gbrain_sync_mode is off', () => {
+ const r = run(['gstack-config', 'get', 'gbrain_sync_mode']);
+ expect(r.status).toBe(0);
+ expect(r.stdout.trim()).toBe('off');
+ });
+
+ test('default gbrain_sync_mode_prompted is false', () => {
+ const r = run(['gstack-config', 'get', 'gbrain_sync_mode_prompted']);
+ expect(r.stdout.trim()).toBe('false');
+ });
+
+ test('accepts full / artifacts-only / off', () => {
+ for (const val of ['full', 'artifacts-only', 'off']) {
+ const set = run(['gstack-config', 'set', 'gbrain_sync_mode', val]);
+ expect(set.status).toBe(0);
+ const get = run(['gstack-config', 'get', 'gbrain_sync_mode']);
+ expect(get.stdout.trim()).toBe(val);
+ }
+ });
+
+ test('invalid gbrain_sync_mode value warns + defaults', () => {
+ const r = run(['gstack-config', 'set', 'gbrain_sync_mode', 'bogus']);
+ expect(r.stderr).toContain('not recognized');
+ const get = run(['gstack-config', 'get', 'gbrain_sync_mode']);
+ expect(get.stdout.trim()).toBe('off');
+ });
+
+ test('GSTACK_HOME overrides real config dir', () => {
+ run(['gstack-config', 'set', 'gbrain_sync_mode', 'full']);
+ // Real ~/.gstack/config.yaml must NOT have been touched.
+ const realConfig = path.join(os.homedir(), '.gstack', 'config.yaml');
+ const real = fs.existsSync(realConfig) ? fs.readFileSync(realConfig, 'utf-8') : '';
+ expect(real).not.toContain('gbrain_sync_mode: full');
+ });
+});
+
+// ---------------------------------------------------------------
+// Enqueue behavior
+// ---------------------------------------------------------------
+describe('gstack-brain-enqueue', () => {
+ test('no-op when feature not initialized', () => {
+ const r = run(['gstack-brain-enqueue', 'projects/foo/learnings.jsonl']);
+ expect(r.status).toBe(0);
+ expect(fs.existsSync(path.join(tmpHome, '.brain-queue.jsonl'))).toBe(false);
+ });
+
+ test('no-op when mode is off (even if .git exists)', () => {
+ fs.mkdirSync(path.join(tmpHome, '.git'), { recursive: true });
+ const r = run(['gstack-brain-enqueue', 'projects/foo/learnings.jsonl']);
+ expect(r.status).toBe(0);
+ expect(fs.existsSync(path.join(tmpHome, '.brain-queue.jsonl'))).toBe(false);
+ });
+
+ test('enqueues when mode is full and .git exists', () => {
+ fs.mkdirSync(path.join(tmpHome, '.git'), { recursive: true });
+ run(['gstack-config', 'set', 'gbrain_sync_mode', 'full']);
+ run(['gstack-brain-enqueue', 'projects/foo/learnings.jsonl']);
+ const queue = fs.readFileSync(path.join(tmpHome, '.brain-queue.jsonl'), 'utf-8');
+ expect(queue).toContain('projects/foo/learnings.jsonl');
+ const obj = JSON.parse(queue.trim());
+ expect(obj.file).toBe('projects/foo/learnings.jsonl');
+ expect(obj.ts).toBeTruthy();
+ });
+
+ test('skip list honored', () => {
+ fs.mkdirSync(path.join(tmpHome, '.git'), { recursive: true });
+ run(['gstack-config', 'set', 'gbrain_sync_mode', 'full']);
+ fs.writeFileSync(path.join(tmpHome, '.brain-skip.txt'), 'projects/foo/secret.jsonl\n');
+ run(['gstack-brain-enqueue', 'projects/foo/secret.jsonl']);
+ run(['gstack-brain-enqueue', 'projects/foo/ok.jsonl']);
+ const queue = fs.readFileSync(path.join(tmpHome, '.brain-queue.jsonl'), 'utf-8');
+ expect(queue).not.toContain('secret.jsonl');
+ expect(queue).toContain('ok.jsonl');
+ });
+
+ test('concurrent enqueues all land (atomic append)', async () => {
+ fs.mkdirSync(path.join(tmpHome, '.git'), { recursive: true });
+ run(['gstack-config', 'set', 'gbrain_sync_mode', 'full']);
+ const procs = [];
+ for (let i = 0; i < 10; i++) {
+ procs.push(new Promise<void>((resolve) => {
+ const r = spawnSync(path.join(BIN, 'gstack-brain-enqueue'), [`file-${i}.jsonl`], {
+ env: { ...process.env, GSTACK_HOME: tmpHome },
+ encoding: 'utf-8',
+ });
+ resolve();
+ }));
+ }
+ await Promise.all(procs);
+ const queue = fs.readFileSync(path.join(tmpHome, '.brain-queue.jsonl'), 'utf-8');
+ const lines = queue.trim().split('\n').filter(Boolean);
+ expect(lines.length).toBe(10);
+ });
+
+ test('no args does not crash', () => {
+ const r = run(['gstack-brain-enqueue']);
+ expect(r.status).toBe(0);
+ });
+});
+
+// ---------------------------------------------------------------
+// JSONL merge driver
+// ---------------------------------------------------------------
+describe('gstack-jsonl-merge', () => {
+ test('3-way merge dedups + sorts by ts', () => {
+ const base = path.join(tmpHome, 'base.jsonl');
+ const ours = path.join(tmpHome, 'ours.jsonl');
+ const theirs = path.join(tmpHome, 'theirs.jsonl');
+ fs.writeFileSync(base, '');
+ fs.writeFileSync(ours, '{"x":1,"ts":"2026-01-01T10:00:00Z"}\n{"x":2,"ts":"2026-01-01T11:00:00Z"}\n');
+ fs.writeFileSync(theirs, '{"x":3,"ts":"2026-01-01T09:00:00Z"}\n{"x":2,"ts":"2026-01-01T11:00:00Z"}\n');
+ const r = run([path.join(BIN, 'gstack-jsonl-merge'), base, ours, theirs]);
+ expect(r.status).toBe(0);
+ const lines = fs.readFileSync(ours, 'utf-8').trim().split('\n');
+ expect(lines.length).toBe(3);
+ expect(lines[0]).toContain('"x":3'); // earliest ts
+ expect(lines[2]).toContain('"x":2'); // latest ts
+ });
+
+ test('falls back to hash order for lines without ts', () => {
+ const base = path.join(tmpHome, 'base.jsonl');
+ const ours = path.join(tmpHome, 'ours.jsonl');
+ const theirs = path.join(tmpHome, 'theirs.jsonl');
+ fs.writeFileSync(base, '');
+ fs.writeFileSync(ours, '{"a":1}\n{"a":2}\n');
+ fs.writeFileSync(theirs, '{"a":3}\n{"a":2}\n');
+ run([path.join(BIN, 'gstack-jsonl-merge'), base, ours, theirs]);
+ const lines = fs.readFileSync(ours, 'utf-8').trim().split('\n');
+ expect(lines.length).toBe(3);
+ // Order is deterministic (sha256 of each line).
+ const again = spawnSync(path.join(BIN, 'gstack-jsonl-merge'), [base, ours, theirs]);
+ // (re-running doesn't change the order since same input → same output)
+ });
+});
+
+// ---------------------------------------------------------------
+// Init + sync + restore round-trip
+// ---------------------------------------------------------------
+describe('init + sync + restore round-trip', () => {
+ test('init creates canonical files + registers drivers', () => {
+ const r = run(['gstack-brain-init', '--remote', bareRemote]);
+ expect(r.status).toBe(0);
+ expect(fs.existsSync(path.join(tmpHome, '.git'))).toBe(true);
+ expect(fs.existsSync(path.join(tmpHome, '.gitignore'))).toBe(true);
+ expect(fs.existsSync(path.join(tmpHome, '.brain-allowlist'))).toBe(true);
+ expect(fs.existsSync(path.join(tmpHome, '.brain-privacy-map.json'))).toBe(true);
+ expect(fs.existsSync(path.join(tmpHome, '.gitattributes'))).toBe(true);
+ expect(fs.existsSync(path.join(tmpHome, '.git/hooks/pre-commit'))).toBe(true);
+ // Merge driver registered in local git config.
+ const cfg = git(['config', '--get', 'merge.jsonl-append.driver']);
+ expect(cfg.stdout).toContain('gstack-jsonl-merge');
+ });
+
+ test('refuses init on different remote', () => {
+ run(['gstack-brain-init', '--remote', bareRemote]);
+ const otherRemote = fs.mkdtempSync(path.join(os.tmpdir(), 'brain-other-'));
+ spawnSync('git', ['init', '--bare', '-q', '-b', 'main', otherRemote]);
+ const r = run(['gstack-brain-init', '--remote', otherRemote]);
+ expect(r.status).not.toBe(0);
+ expect(r.stderr).toContain('already a git repo pointing at');
+ fs.rmSync(otherRemote, { recursive: true, force: true });
+ });
+
+ test('full sync: init → enqueue → --once → commit pushed', () => {
+ run(['gstack-brain-init', '--remote', bareRemote]);
+ run(['gstack-config', 'set', 'gbrain_sync_mode', 'full']);
+ fs.mkdirSync(path.join(tmpHome, 'projects', 'p'), { recursive: true });
+ fs.writeFileSync(path.join(tmpHome, 'projects/p/learnings.jsonl'),
+ '{"skill":"x","insight":"y","ts":"2026-04-22T10:00:00Z"}\n');
+ run(['gstack-brain-enqueue', 'projects/p/learnings.jsonl']);
+ const r = run(['gstack-brain-sync', '--once']);
+ expect(r.status).toBe(0);
+ // Check the remote got the commit.
+ const log = spawnSync('git', ['--git-dir=' + bareRemote, 'log', '--oneline'], { encoding: 'utf-8' });
+ expect(log.stdout).toMatch(/sync: 1 file/);
+ });
+
+ test('restore round-trip: writes on machine A visible on machine B', () => {
+ // Machine A.
+ run(['gstack-brain-init', '--remote', bareRemote]);
+ run(['gstack-config', 'set', 'gbrain_sync_mode', 'full']);
+ fs.mkdirSync(path.join(tmpHome, 'projects', 'myproj'), { recursive: true });
+ const aLearning = '{"skill":"x","insight":"machine A wisdom","ts":"2026-04-22T10:00:00Z"}\n';
+ fs.writeFileSync(path.join(tmpHome, 'projects/myproj/learnings.jsonl'), aLearning);
+ run(['gstack-brain-enqueue', 'projects/myproj/learnings.jsonl']);
+ run(['gstack-brain-sync', '--once']);
+
+ // Machine B (new temp home).
+ const machineB = fs.mkdtempSync(path.join(os.tmpdir(), 'brain-machineB-'));
+ const r = run(['gstack-brain-restore', bareRemote], {
+ env: { GSTACK_HOME: machineB },
+ });
+ expect(r.status).toBe(0);
+ const restored = fs.readFileSync(path.join(machineB, 'projects/myproj/learnings.jsonl'), 'utf-8');
+ expect(restored).toContain('machine A wisdom');
+ // Merge drivers re-registered on B.
+ const cfg = spawnSync('git', ['-C', machineB, 'config', '--get', 'merge.jsonl-append.driver'], { encoding: 'utf-8' });
+ expect(cfg.stdout).toContain('gstack-jsonl-merge');
+ fs.rmSync(machineB, { recursive: true, force: true });
+ });
+});
+
+// ---------------------------------------------------------------
+// Secret scan: all regex families block
+// ---------------------------------------------------------------
+describe('gstack-brain-sync secret scan', () => {
+ const SECRETS: [string, string][] = [
+ ['aws-access-key', 'AKIAABCDEFGHIJKLMNOP'],
+ ['github-token-ghp', 'ghp_abcdefghij1234567890abcdef1234567890'],
+ ['github-token-github-pat', 'github_pat_11ABCDEFG1234567890_abcdef'],
+ ['openai-key', 'sk-abcdefghij1234567890abcdef1234567890'],
+ ['pem-block', '-----BEGIN PRIVATE KEY-----'],
+ ['jwt', 'eyJ0eXAiOiJKV1QiLCJh.eyJzdWIiOiIxMjM0NTY3.SflKxwRJSMeKKF30oGTbU'],
+ ['bearer-json', '"authorization":"Bearer abcdef1234567890abcdef1234567890"'],
+ ];
+
+ for (const [name, content] of SECRETS) {
+ test(`blocks ${name}`, () => {
+ run(['gstack-brain-init', '--remote', bareRemote]);
+ run(['gstack-config', 'set', 'gbrain_sync_mode', 'full']);
+ fs.mkdirSync(path.join(tmpHome, 'projects', 'p'), { recursive: true });
+ fs.writeFileSync(path.join(tmpHome, 'projects/p/learnings.jsonl'),
+ `{"leaked":"${content}"}\n`);
+ run(['gstack-brain-enqueue', 'projects/p/learnings.jsonl']);
+ const r = run(['gstack-brain-sync', '--once']);
+ expect(r.status).toBe(0); // exits clean even when blocked
+ // No new commit should have been created.
+ const log = git(['log', '--oneline']);
+ expect(log.stdout.split('\n').filter(Boolean).length).toBeLessThanOrEqual(3);
+ // Status file should report blocked.
+ const status = JSON.parse(fs.readFileSync(path.join(tmpHome, '.brain-sync-status.json'), 'utf-8'));
+ expect(status.status).toBe('blocked');
+ });
+ }
+
+ test('--skip-file unblocks specific file', () => {
+ run(['gstack-brain-init', '--remote', bareRemote]);
+ run(['gstack-config', 'set', 'gbrain_sync_mode', 'full']);
+ fs.mkdirSync(path.join(tmpHome, 'projects', 'p'), { recursive: true });
+ const leakPath = 'projects/p/leaked.jsonl';
+ fs.writeFileSync(path.join(tmpHome, leakPath),
+ '{"gh":"ghp_abcdefghij1234567890abcdef1234567890"}\n');
+ run(['gstack-brain-enqueue', leakPath]);
+ run(['gstack-brain-sync', '--once']); // blocked
+ run(['gstack-brain-sync', '--skip-file', leakPath]);
+ // Any future enqueue of this path should no-op.
+ run(['gstack-brain-enqueue', leakPath]);
+ const skip = fs.readFileSync(path.join(tmpHome, '.brain-skip.txt'), 'utf-8');
+ expect(skip).toContain(leakPath);
+ });
+});
+
+// ---------------------------------------------------------------
+// Uninstall preserves user data
+// ---------------------------------------------------------------
+describe('gstack-brain-uninstall', () => {
+ test('removes sync config but preserves learnings/project data', () => {
+ run(['gstack-brain-init', '--remote', bareRemote]);
+ fs.mkdirSync(path.join(tmpHome, 'projects', 'user-data'), { recursive: true });
+ const preservedContent = '{"keep":"me","ts":"2026-04-22T12:00:00Z"}\n';
+ fs.writeFileSync(path.join(tmpHome, 'projects/user-data/learnings.jsonl'), preservedContent);
+ const r = run(['gstack-brain-uninstall', '--yes']);
+ expect(r.status).toBe(0);
+ expect(fs.existsSync(path.join(tmpHome, '.git'))).toBe(false);
+ expect(fs.existsSync(path.join(tmpHome, '.gitignore'))).toBe(false);
+ expect(fs.existsSync(path.join(tmpHome, '.brain-allowlist'))).toBe(false);
+ expect(fs.existsSync(path.join(tmpHome, 'consumers.json'))).toBe(false);
+ // Project data preserved.
+ const preserved = fs.readFileSync(path.join(tmpHome, 'projects/user-data/learnings.jsonl'), 'utf-8');
+ expect(preserved).toBe(preservedContent);
+ // Config key reset.
+ const mode = run(['gstack-config', 'get', 'gbrain_sync_mode']);
+ expect(mode.stdout.trim()).toBe('off');
+ });
+});
+
+// ---------------------------------------------------------------
+// --discover-new: cursor-based change detection
+// ---------------------------------------------------------------
+describe('gstack-brain-sync --discover-new', () => {
+ test('enqueues new allowlisted files; idempotent on re-run', () => {
+ run(['gstack-brain-init', '--remote', bareRemote]);
+ run(['gstack-config', 'set', 'gbrain_sync_mode', 'full']);
+ fs.mkdirSync(path.join(tmpHome, 'retros'), { recursive: true });
+ fs.writeFileSync(path.join(tmpHome, 'retros/week-1.md'), '# retro\n');
+ run(['gstack-brain-sync', '--discover-new']);
+ let queue = fs.readFileSync(path.join(tmpHome, '.brain-queue.jsonl'), 'utf-8');
+ expect(queue).toContain('retros/week-1.md');
+ // Clear queue, run again — idempotent (no new entries).
+ fs.writeFileSync(path.join(tmpHome, '.brain-queue.jsonl'), '');
+ run(['gstack-brain-sync', '--discover-new']);
+ queue = fs.readFileSync(path.join(tmpHome, '.brain-queue.jsonl'), 'utf-8');
+ expect(queue.trim()).toBe('');
+ });
+});
diff --git a/test/skill-e2e-bws.test.ts b/test/skill-e2e-bws.test.ts
index acbdf86c4..956174117 100644
--- a/test/skill-e2e-bws.test.ts
+++ b/test/skill-e2e-bws.test.ts
@@ -286,11 +286,38 @@ Log the operational learning now. Then say what you logged.`,
// Add a remote so the agent can derive a project name
run('git', ['remote', 'add', 'origin', 'https://github.com/acme/billing-app.git']);
- // Extract AskUserQuestion format instructions from generated SKILL.md
- const skillMd = fs.readFileSync(path.join(ROOT, 'SKILL.md'), 'utf-8');
+ // Extract AskUserQuestion format instructions from a generated SKILL.md.
+ // ROOT/SKILL.md is the browse skill (Tier 1) and does NOT contain the
+ // "## AskUserQuestion Format" section — that block is only emitted for
+ // Tier 2+ skills by scripts/resolvers/preamble.ts. Use office-hours/SKILL.md
+ // (Tier 3) which always has the format guidance baked in. Falls back to
+ // the first SKILL.md that contains the header so a future template move
+ // doesn't break this test again.
+ let skillMdPath = path.join(ROOT, 'office-hours', 'SKILL.md');
+ let skillMd = '';
+ if (fs.existsSync(skillMdPath)) {
+ skillMd = fs.readFileSync(skillMdPath, 'utf-8');
+ }
+ if (!skillMd.includes('## AskUserQuestion Format')) {
+ // Fallback: scan top-level skill dirs for the first match.
+ const skillDirs = fs.readdirSync(ROOT, { withFileTypes: true })
+ .filter(d => d.isDirectory())
+ .map(d => path.join(ROOT, d.name, 'SKILL.md'));
+ for (const candidate of skillDirs) {
+ if (!fs.existsSync(candidate)) continue;
+ const content = fs.readFileSync(candidate, 'utf-8');
+ if (content.includes('## AskUserQuestion Format')) {
+ skillMd = content;
+ skillMdPath = candidate;
+ break;
+ }
+ }
+ }
const aqStart = skillMd.indexOf('## AskUserQuestion Format');
const aqEnd = skillMd.indexOf('\n## ', aqStart + 1);
- const aqBlock = skillMd.slice(aqStart, aqEnd > 0 ? aqEnd : undefined);
+ const aqBlock = aqStart >= 0
+ ? skillMd.slice(aqStart, aqEnd > 0 ? aqEnd : undefined)
+ : '';
const outputPath = path.join(sessionDir, 'question-output.md');