Skip to content

fix(worktree): fetch trunk before creating worktree to avoid stale base#2324

Draft
richardsolomou wants to merge 1 commit into
mainfrom
posthog-code/fetch-trunk-before-worktree
Draft

fix(worktree): fetch trunk before creating worktree to avoid stale base#2324
richardsolomou wants to merge 1 commit into
mainfrom
posthog-code/fetch-trunk-before-worktree

Conversation

@richardsolomou
Copy link
Copy Markdown
Member

Problem

Cloud-agent worktrees can start off a stale local trunk. When the harness provisions a clone whose local main hasn't been refreshed, WorktreeManager.createWorktree bases the new detached worktree on the local ref — so the agent begins iterating on code that's already behind origin/main, surfacing user-visible "worktree starts behind master" reports.

Changes

Add an opt-in fetchBeforeCreate option on the trunk path:

  • WorktreeManager.createWorktree({ fetchBeforeCreate: true }) calls a new resolveFreshBaseRef helper that does a best-effort git fetch origin <baseBranch> and bases the worktree on origin/<base> when reachable. Falls back to the local base ref if the fetch fails (offline, no origin, ref doesn't exist on the remote) so existing local-only setups still work.
  • CreateWorktreeSaga gets the same option for parity, using this.git directly inside the existing write lock (the fetch query helper would re-enter the lock and deadlock).
  • apps/code/src/main/services/workspace/service.ts passes fetchBeforeCreate: true on the trunk path only. The occupied-branch fallback (which bases off a user-local branch they may have committed to) is intentionally left as-is — fetching there would silently bypass their local commits.

We base off origin/<base> rather than fast-forwarding the local branch so the user's local checkout is never mutated; only the worktree picks up the remote tip.

Two small helpers (hasRef, fetchRef) added to packages/git/src/queries.ts to keep the saga and WorktreeManager symmetric.

How did you test this?

  • pnpm --filter @posthog/git test src/worktree.test.ts — 3 new tests pass:
    • Without fetchBeforeCreate, worktree is based on the stale local ref (baseline that proves the bug).
    • With fetchBeforeCreate, worktree starts at the remote tip and local main is not mutated.
    • With fetchBeforeCreate and an unreachable remote, falls back to the local base.
  • pnpm --filter @posthog/git typecheck, pnpm --filter code typecheck, pnpm lint — all clean.
  • The 2 pre-existing failures in the git test suite (forceRemove EACCES, detectDefaultBranch > prefers remote HEAD) reproduce on main and are unrelated.

Publish to changelog?

no

When the worktree base is created off the local trunk ref, a clone
whose local default branch hasn't been refreshed produces a worktree that
starts behind `origin/main`. The agent then iterates on stale code,
which is the root cause behind the user-reported pattern of worktrees
starting "behind master".

Add an opt-in `fetchBeforeCreate` flag on the trunk path that fetches
`origin/<base>` first and bases the worktree on the remote tip when
reachable. Falls back to the local base ref if the fetch fails so
offline / local-only setups still work and the user's local checkout
stays untouched (we base off `origin/<branch>`, not a fast-forward of
local `<branch>`).

Wired up at the workspace-service trunk path; the occupied-branch
fallback keeps local-base behavior so user-local commits aren't silently
bypassed.

Generated-By: PostHog Code
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