Skip to content

fix: gstack-brain-sync advances the artifacts worktree after push#1513

Open
alexputici wants to merge 1 commit into
garrytan:mainfrom
alexputici:fix/brain-sync-advance-worktree-after-push
Open

fix: gstack-brain-sync advances the artifacts worktree after push#1513
alexputici wants to merge 1 commit into
garrytan:mainfrom
alexputici:fix/brain-sync-advance-worktree-after-push

Conversation

@alexputici
Copy link
Copy Markdown

Summary

gstack-brain-sync --once commits + pushes artifacts to the remote, but never advances the local detached worktree at ~/.gstack-brain-worktree. gbrain indexes from that worktree path, so the worktree stays at the init commit forever, gbrain sources list reports 0 pages for the artifacts source, and recent CEO plans / test plans / reviews never become searchable locally.

Reproduction

  1. Enable artifacts sync (gstack-config set artifacts_sync_mode artifacts-only or full) and run gstack-artifacts-init.
  2. Use gstack normally — run /autoplan, /ship, etc. so brain-sync runs --discover-new then --once, producing committed artifacts under ~/.gstack/projects/*/.
  3. Inspect:
    git -C ~/.gstack worktree list
    # /Users/<you>/.gstack             <new SHA> [main]
    # /Users/<you>/.gstack-brain-worktree <init SHA> (detached HEAD)
    
    gbrain sources list | grep gstack-artifacts
    # ... 0 pages ...
    
    gbrain query "a phrase from your latest CEO plan"
    # No results.

The artifact files exist in ~/.gstack on main and on the GitHub remote. They just never reach the worktree gbrain scans.

Fix

Add a tiny advance_brain_worktree_to_head helper and call it on both successful-push paths in subcmd_once (the normal path and the fetch+merge retry path). It's a no-op when the worktree doesn't exist or the parent HEAD can't be read, so installs that haven't run gstack-gbrain-source-wireup yet are unaffected.

Why not call ensure_worktree from gstack-gbrain-source-wireup? That helper has additional side effects (source registration, jq probes, gbrain CLI calls) and lives in a script gstack-brain-sync doesn't currently source. A 12-line inline helper keeps the patch minimal and the push-success path side-effect-free on the gbrain CLI.

Test plan

  • New test '--once advances the brain worktree HEAD to match parent after push' in test/brain-sync.test.ts: sets up a real detached worktree at the init commit (mirroring gstack-gbrain-source-wireup), runs --once against it, asserts the worktree HEAD matches the parent HEAD afterwards AND that the synced file appears on the worktree path.
  • Verified the test fails without the fix (worktree stays at init SHA).
  • bun test test/brain-sync.test.ts — 28/28 pass.
  • bun test test/brain-sync.test.ts test/gbrain-sync-skip.test.ts test/gstack-gbrain-sync.test.ts — 53/53 pass.

Diff size

+17 in bin/gstack-brain-sync (helper + 2 call sites), +45 in test/brain-sync.test.ts (1 regression test). 2 files changed, 62 insertions.

🤖 Generated with Claude Code

Without this, the worktree at ~/.gstack-brain-worktree stays at the
init commit forever while the parent repo's main branch accumulates
artifact commits. gbrain indexes from the worktree path, so users
see `gbrain sources list` reporting 0 pages and recent CEO plans /
test plans / reviews never become searchable locally.

Reproduction: enable artifacts sync, run a few /autoplan or /ship
sessions to produce committed artifacts, then `gbrain query` for a
filename you just created — no results. Inspection shows
`git -C ~/.gstack worktree list` has the worktree pinned at the
init SHA while `git -C ~/.gstack rev-parse main` has moved ahead.

The fix adds an advance_brain_worktree_to_head helper and calls it
on both successful push paths in subcmd_once (normal + rebased
retry). It's a no-op when the worktree doesn't exist or the parent
HEAD can't be read, so installs that haven't run
gstack-gbrain-source-wireup yet are unaffected.

Regression test runs --once against a real detached worktree and
asserts the worktree HEAD matches the parent HEAD afterwards, plus
that the synced file actually appears at the worktree path. Verified
the test fails without the fix (worktree stuck at init SHA).

Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant