fix(host-service): refresh non-default base branches before forking new workspaces

Merged
KK
4mo ago
+203-68

Summary

resolveNewBranchStartPoint only upgraded local→remote-tracking + fetched the base ref when the base was the default branch. For any other base branch — sticky localStorage default, explicit picker choice, an AI-suggested branch — the local ref was used as-is with no fetch. A shared branch that hadn't been touched in weeks silently forked new workspaces from a stale tip.

Concrete case that motivated this: workspace created with baseBranch: mirror-flier; local mirror-flier was 44 days behind origin/mirror-flier, producing a new workspace ~1700 commits behind current work. Reproduced from the git reflog in the affected worktree (branch: Created from mirror-flier at a June 9 commit, worktree pointer touched July 23).

Change

  • Extract resolveNewBranchStartPoint from workspaces.ts into workspace-creation/utils/resolve-new-branch-start-point.ts so it can be unit-tested.
  • Drop the startPoint.shortName === defaultBranchName gate — the upgrade + fetch now applies to any local base branch that has a configured upstream. Default-branch behavior is unchanged.

Local branches without an upstream (workspace branches, purely-local dev branches) and cases where the remote-tracking ref doesn't exist locally continue to use the local ref unchanged — same behavior as before.

Test plan

  • New unit tests in resolve-new-branch-start-point.test.ts cover: non-default branch with upstream (regression), default branch (unchanged), no upstream, remote ref missing, silent fetch failure.
  • Manual: open a new workspace with a non-default base branch that's stale locally; verify the new worktree tip matches origin/<base> and not the pre-existing local ref.
  • bun test packages/host-service passes.
  • bun run lint and bun run typecheck clean.

Known follow-ups (not in this PR)

Adjacent causes surfaced during the same investigation, deliberately left as separate work:

  • Sticky base-branch default. apps/desktop/src/renderer/stores/v2-workspace-create-defaults.ts persists the last-picked base branch per project in localStorage indefinitely (baseBranchesByProjectId), and PromptGroup.tsx:96-98 pre-fills it silently. A one-time accidental pick of a non-default branch becomes the sticky default forever, with no UI signal. Fix has design choices (drop persistence, badge inherited defaults, add a reset affordance) worth discussing on its own PR. With this PR merged the resulting workspaces at least fork from a fresh tip of the stuck branch, so the symptom drops from "weeks behind" to "sometimes not main."
  • Picker source hint dropped. buildStartPointFromHint (packages/host-service/src/trpc/router/workspace-creation/shared/start-point.ts:12) is documented in apps/desktop/docs/V2_WORKSPACE_CREATION.md as the way the picker's explicit local / remote-tracking row choice reaches the server, but it's never called. createInputSchema has no baseBranchSource field and useSubmitWorkspace.ts:117 only sends baseBranch. So clicking the origin/main row can still resolve to local main on the server. Wiring the hint end-to-end is a small but cross-package change.

Summary by CodeRabbit

  • New Features

    • Improved new branch creation to recognize configured upstream branches and use available remote-tracking references.
    • Automatically refreshes remote branch information when resolving a branch’s starting point.
  • Bug Fixes

    • Branch creation now safely falls back to local references when upstream information is unavailable.
    • Fetch failures no longer prevent branch creation.