fix(host-service): stop workspace↔PR/diff state drifting from GitHub

Merged
KI
4mo ago
+1145-318

Problem

Users report their workspace's main-branch tracking and PR link drifting from GitHub state — a branch that shows 146 files changed vs main, and a PR that never connects — most often on macOS's case-insensitive filesystem. Three distinct causes, all in the v2 host-service path:

Root causes & fixes

1. PR↔branch matching was case-sensitive on the branch. upstreamKey/headKey lowercased owner/repo but kept the branch verbatim, so a local roshvan/mcp-1525-… never matched a PR head Roshvan/mcp-1525-…, and every sync actively re-wrote pullRequestId = null. Now the branch is compared case-insensitively. Because GitHub's head= REST filter is itself case-sensitive server-side (verified against this repo), a per-head lookup for a drifted branch returns nothing — so a shallow open-PR sweep (one page, no checks/statuses, once per repo per 60s TTL, only when a per-head lookup missed) fills the gap and matches case-insensitively. A sweep failure marks keys failed so existing links are kept, never cleared.

2. Workspace creation could mint a case-twin branch. On a case-insensitive filesystem git rev-parse refs/heads/foo happily resolves the refs/heads/Foo file, so resolveRef's exact-case probe couldn't distinguish an exact match from case drift — and git worktree add -b would create a second branch sharing the existing one's loose-ref file (the mechanism that corrupts HEAD underneath the workspace). resolveRef now enumerates real refnames via for-each-ref: exact matches keep prior precedence (local > remote > tag); a case-insensitive match adopts the existing branch's canonical casing, which planBranchSource/create propagate.

3. The Changes panel never fetched its base ref. It diffs origin/<base>...HEAD (3-dot merge-base), but nothing in the status path fetched origin/<base> — so after a rebase onto a newer upstream, every replayed upstream commit was counted as a workspace change, ballooning the file count. getGitStatusSnapshot now schedules a fire-and-forget fetch of the base's remote branch (keyed by the repo common-dir, TTL'd, in-flight-deduped); GitWatcher picks up the ref change and re-queries.

Verification

  • Unit + integration tests, each mutation-checked (revert the fix → test fails).
  • End-to-end against real on-disk repos: resolveRef adopts canonical casing for local/remote/origin/-prefixed drift; diff balloons 4→heals to 1 file after the background fetch.
  • Live-GitHub: exact-head finds the PR, drifted-head misses server-side, the sweep matches it, a bogus branch stays unmatched.
  • Via CDP against the running host-service: getStatus balloon (4 files) → heal (1 file), background fetch fires (FETCH_HEAD + origin/main sha advance confirmed independently), TTL dedup holds on rapid calls.

Notes / prior art

  • Supersedes #4367 and closes the class in #4364 — that PR added a case-insensitive fallback after the exact-case probe, which on macOS never runs because the probe wrongly succeeds; this replaces the probes with enumeration so both are covered.
  • Mirrors GitHub Desktop's #21320 rename guard (enumerate branches, never trust a single-ref probe) and their list-and-match PR design.
  • The repo-wide sweep is deliberately narrow (shallow open-PR page, no checks) to avoid regressing the #4268/#4291 504-on-large-repos fix.
  • Follow-up worth considering: the base-ref fetch uses a 5-min TTL; a content-keyed dedup on (headSha, baseSha) + a short failure-only backoff would remove the arbitrary interval and make the heal immediate after a rebase. Left out of this PR to keep it focused.
  • The v1 desktop path (pr-resolution.ts) has the same case-sensitivity bug but is left untouched pending the v1 sunset.

🤖 Generated with Claude Code


Open in Stage

Summary by cubic

Fixes workspace↔GitHub drift that caused inflated file counts and PR links not attaching. Keeps branch casing and base refs accurate so diffs and links stay correct.

  • Bug Fixes
    • PR linking: keep upstreamKey and per-head cache keys case-sensitive to isolate case-variant branches; add a shallow state=open sweep (once per repo per TTL, only when a head lookup misses) that matches case-insensitively to recover drifted branches; cache failures for the TTL and preserve existing links if the sweep fails; tests now use a real, migrated in-memory SQLite DB to verify case-variant isolation across bypass and cached paths.
    • Branch ref resolution: enumerate branches via for-each-ref and adopt canonical casing; prevent case-twin creation on case-insensitive filesystems; propagate the canonical name during workspace creation; surface real for-each-ref failures instead of degrading to “no branches.”
    • Base-ref freshness: background-fetch the base remote branch during status (TTL, in-flight dedup keyed by repo common-dir, resolve common dir per call, never rejects) so origin/<base>...HEAD diffs don’t balloon after a rebase.

Summary by CodeRabbit

  • New Features

    • Workspace and branch reuse now adopts canonical casing from existing local or remote-tracking refs.
    • Added a repo-wide open-PR sweep fallback to recover missed PR lookups when branch names differ only by letter case.
    • Introduced TTL-based background refresh for remote-tracking base refs to keep comparisons up to date.
  • Bug Fixes

    • Improved Git ref resolution and PR linking for case-drifted branches/tags, including correct “no matches” vs real failures.
    • If the open-PR sweep fails transiently, existing workspace↔PR links are preserved (not cleared).
    • Prevented PR cross-linking between case-variant branch names.
  • Tests

    • Added/expanded regression coverage for these case-handling behaviors.