fix(host-service): link PR checkouts that track synthetic refs
What & why
Fixes #8775.
Creating a workspace for a merged PR whose head branch was deleted falls back to refs/pull/<number>/head. The PR runtime treats that ref as a branch name, so discovery finds nothing and the workspace never gets its PR link.
Resolve synthetic refs belonging to the project's base repository by PR number. Record the link and history only while the workspace HEAD matches the PR head, and respect an explicit unlink. If GitHub is unavailable, a known mismatch against the stored PR head clears only the current link; matching or unknown heads retain it and history is preserved. This also repairs existing workspaces on the next refresh. The lookup uses the existing cache, failure backoff, availability gate, and gh-to-Octokit fallback.
Related PRs #8935 and #9657 address recorded PR history; neither discovers this previously unlinked checkout. No API or database schema changes are needed.
How I tested it
- Added a regression test against the real migrated SQLite database: it fails on
6646c6399725537cda3de125b955465a8bc6d071and passes with this change. Coverage also checks changed HEAD, explicit unlink, foreign-repository refs, and both GitHub transports. - Earlier offline follow-up: 8 pass, 0 fail, including changed/matching/unknown HEAD while offline and the existing ordinary-branch lookup-failure case. The new changed-HEAD case fails before the follow-up fix.
- Earlier
bun test src/runtime/pull-requests: 124 pass, 1 fail. The failure is the unchanged history ordering test already tracked by #9713 (linkedAtties). bun run lint:fix, thenbun run lint: pass, 13,543 files.bun run typecheck: 42/42 tasks pass.- Full test run at the initial commit
99aa401012e9: host-service 2,912 pass, 0 fail; desktop 9,643 pass, 0 fail. The offline follow-up was checked with the targeted cases, PR runtime suite, lint, and all 42 typecheck tasks. bun run testis not fully green locally. The initial run encounters the unchanged CLI hyperlink tests underTERM=dumb(tracked by #9664). WithTERM=xterm-256color,bun run test --continue=alwayscompletes 27/30 tasks: unchanged shared date-format assertions fail on macOS (#9658), workspace-fs has a watcher timing failure, and agent-setup has shell/launch-report failures. The watcher file passes separately (5/5); the shared failure and two shell-wrapper failures reproduce separately. No CI or unrelated test changes are included.
Real dev-app reproduction
Used an isolated dev Electron instance, renderer http://localhost:3005/, CDP 9237, and its seeded local development account. Created a real workspace with the dev CLI for merged PR #9730, whose same-repository branch is gone. The host fetched refs/pull/9730/head; HEAD was 5ced4956be8e1ee9f2ed6b8bf7d1b76a718fcf84 throughout.
On the original runtime, the workspace had no PR link or history row. After rebuilding with the fix, that same workspace linked automatically to #9730 as merged. Opened the workspace and activity popover with CDP mouse input and captured both states. The screenshots show the same workspace; the stopped setup terminal is background context. Read-only database observations and reproduction metadata.
| Before | After |
|---|---|
![]() | ![]() |
Local setup needed PostgreSQL 18 and the Bun SQL migration driver: the stock setup selects the Neon driver for local migration, and migration 0122 uses named NOT NULL constraints unavailable in PostgreSQL 17. Those environment workarounds are outside this patch.
Implementation, diff inspection, tests, and UI automation were performed with Codex. No human review or manual testing is claimed.
First verification on November 18, 2026
Validated 446d1b1c90bea220f3f4b624568a1b6eb49d434a on c664bb1bca2ad754eaa9417e5a6b7ca76f32955a. The deferred-success regression proves that an older response cannot restore a link or write history after a newer scoped refresh sees a different checkout HEAD. The PR runtime suite passes all 127 tests, including the earlier failed-lookup regression. Repository lint and host-service typecheck pass. Screenshots above document the original UI verification.
Latest review verification
Rechecked on 2dc7120b954d860c26cc793972eb1f9769d837f8 after rebasing onto 6a487f050074afc93407b1125b3e116146473fd6. A checkout with unreadable HEAD retains its verified link. Successful matches use the SHA from their own lookup. A newer observation guards the shared PR row against late or cached older results. The three new regressions fail on the previous implementation. All 133 PR-runtime tests pass, along with repository lint and host-service typecheck.
On the latest head, a synthetic ref resolves through its tracking remote. The ref namespace separates ordinary branches with the same spelling. A superseded result checks the stored head before keeping an existing link. Git ref sync preserves a concurrently removed or replaced link. All three latest regressions fail on the preceding head and pass with the correction. The PR runtime suite has 136 passing tests. Repository lint and host-service typecheck pass.
Checklist
- PR title follows conventional commits (
type(scope): subject) -
bun run lintandbun run typecheckpass (CI fails on lint warnings too) - "Allow edits from maintainers" is checked on fork PRs
- I have signed the CLA (the bot asks on your first PR)
Summary by cubic
Fixes workspaces tracking a merged PR whose head branch was deleted never getting their PR link. When the head branch is gone, checkout falls back to refs/pull/<number>/head, which the PR runtime treated as a branch name, so discovery found nothing.
The lookup now resolves synthetic pull refs belonging to the project's base repository by PR number, linking the checkout and recording its history. Pull refs are distinguished from ordinary branches whose short names look like pull refs, so the tracking config stays correct. The link is recorded only while the workspace HEAD matches the PR head, and an explicit unlink is respected. When GitHub is unavailable, a known mismatch against the stored PR head clears only the current link; matching or unknown heads retain it and history is preserved. Lookup results that arrive after the checkout moved to a different HEAD are ignored, so an older refresh—whether it failed or succeeded—cannot drop or overwrite a link set by a newer, scoped refresh. When different refs identify the same PR, only the newest observation is kept, so a stale branch-head cache response cannot replace a newer numbered lookup. Existing affected workspaces self-repair on the next refresh. No API or database schema changes.
Summary by CodeRabbit
- Bug Fixes
- Improved recognition of pull-request checkouts that use numbered GitHub refs, while ensuring they match the correct repository.
- Prevented delayed or overlapping refreshes from replacing newer pull-request information.
- Preserved existing pull-request links when lookups fail, unless the checkout’s commit has changed.
- Ensured explicit unlinking remains in effect and retained pull-request history when a link is removed.
Latest review follow-up (64e3fb63a356, base d31b92a172d4): PR runtime: 126 tests pass; stale full refresh cannot erase a newer scoped link. Repository lint and host-service typecheck pass. Original UI evidence and full-suite results above remain from their stated commits.

