Skip to content

Conversation

@jdelfino
Copy link

@jdelfino jdelfino commented Jan 27, 2026

Summary

On fresh clone, findJSONLPath() would silently fall back to main's stale JSONL because the sync-branch worktree didn't exist yet. The worktree was only created on first daemon sync (write operation), but import operations (reads) would use the stale data.

This caused sync failures when containers were rebuilt - issues accumulated on beads-sync branch were not imported after rebuild because main's stale JSONL was used instead. The data still existed on beads-sync, but the local database was populated with outdated state.

The Fix

  1. Add EnsureWorktree() in internal/syncbranch/worktree.go - creates the sync-branch worktree if configured but doesn't exist
  2. Call EnsureWorktree() from ensureStoreActive() in direct_mode.go - guarantees worktree exists before any JSONL operations
  3. Add debug logging when getWorktreeJSONLPath() falls back (should now be rare)

The EnsureWorktree() function is idempotent - safe to call multiple times. The underlying CreateBeadsWorktree also performs health checks and auto-repairs unhealthy worktrees.

Root Cause Analysis

The issue was in the read path:

  • findJSONLPath() calls getWorktreeJSONLPath()
  • If worktree doesn't exist, it returns "" causing fallback to main's JSONL
  • On fresh clone, worktree doesn't exist yet
  • Post-checkout hook runs bd sync --import-only which uses the stale path

The write path worked correctly - daemon creates worktree before committing. But reads happened before writes on fresh clone.

Tests

  • Worktree not created when sync-branch not configured
  • Worktree created when sync-branch configured via env
  • Worktree created when remote branch exists
  • Idempotent behavior on repeated calls
  • Fresh clone scenario end-to-end test - simulates the exact broken scenario

Test plan

  • Run go test ./internal/syncbranch/... -run TestEnsureWorktree
  • Run go test ./internal/syncbranch/... -run TestFreshCloneScenario
  • Run full test suite: go test ./...
  • Manual test: clone repo with sync-branch configured, verify worktree created on first bd command

🤖 Generated with Claude Code

@jdelfino jdelfino force-pushed the fix/ensure-worktree-on-fresh-clone branch from c59f213 to 8867da4 Compare January 27, 2026 01:25
@jdelfino jdelfino marked this pull request as ready for review January 27, 2026 01:33
@jdelfino jdelfino force-pushed the fix/ensure-worktree-on-fresh-clone branch from 8867da4 to f191402 Compare January 27, 2026 01:33
On fresh clone, findJSONLPath() would silently fall back to main's stale
JSONL because the sync-branch worktree didn't exist yet. The worktree
was only created on first daemon sync (write operation), but import
operations (reads) would use the stale data.

This caused sync failures when containers were rebuilt - issues
accumulated on beads-sync branch were not imported after rebuild because
main's stale JSONL was used instead. The data still existed on
beads-sync, but the local database was populated with outdated state.

The fix:
1. Add EnsureWorktree() in internal/syncbranch/worktree.go - creates
   the sync-branch worktree if configured but doesn't exist
2. Call EnsureWorktree() from ensureStoreActive() in direct_mode.go -
   guarantees worktree exists before any JSONL operations
3. Add debug logging when getWorktreeJSONLPath() falls back (now rare)

The EnsureWorktree() function is idempotent - safe to call multiple
times. The underlying CreateBeadsWorktree also performs health checks
and auto-repairs unhealthy worktrees.

Includes comprehensive tests for:
- Worktree not created when sync-branch not configured
- Worktree created when sync-branch configured via env
- Worktree created when remote branch exists
- Idempotent behavior on repeated calls
- Fresh clone scenario end-to-end test

Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
@jdelfino jdelfino force-pushed the fix/ensure-worktree-on-fresh-clone branch from f191402 to 6bd28a5 Compare January 27, 2026 01:37
@jdelfino jdelfino changed the title fix: ensure sync-branch worktree exists on fresh clone fix(sync): ensure sync-branch worktree exists on fresh clone Jan 27, 2026
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