fix(sync): ensure sync-branch worktree exists on fresh clone #1349
+397
−2
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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
EnsureWorktree()ininternal/syncbranch/worktree.go- creates the sync-branch worktree if configured but doesn't existEnsureWorktree()fromensureStoreActive()indirect_mode.go- guarantees worktree exists before any JSONL operationsgetWorktreeJSONLPath()falls back (should now be rare)The
EnsureWorktree()function is idempotent - safe to call multiple times. The underlyingCreateBeadsWorktreealso performs health checks and auto-repairs unhealthy worktrees.Root Cause Analysis
The issue was in the read path:
findJSONLPath()callsgetWorktreeJSONLPath()""causing fallback to main's JSONLbd sync --import-onlywhich uses the stale pathThe write path worked correctly - daemon creates worktree before committing. But reads happened before writes on fresh clone.
Tests
Test plan
go test ./internal/syncbranch/... -run TestEnsureWorktreego test ./internal/syncbranch/... -run TestFreshCloneScenariogo test ./...bdcommand🤖 Generated with Claude Code