fix(host-service): solve #5596 — stop attaching stale/wrong PRs to the sidebar
What
Fixes two related PR-attachment bugs surfaced by the July 2026 GitHub/PR stale-issue tracker (#5596): the default branch showing a spurious closed-PR icon, and a branch with multiple PRs showing the wrong (closed) one.
Bug 1 — default branch shows a stale closed/merged PR (#4508, #4260, #4513 item 1)
Root cause. In v2, PR→workspace linking lives in PullRequestRuntimeManager (packages/host-service/src/runtime/pull-requests/pull-requests.ts). performProjectRefresh looks up PRs by the workspace's upstream head branch (state=all) and links whatever comes back, with no state check.
When someone opens a throwaway PR from the default branch (e.g. "merge main into feature", so head=main) and closes it, the per-head lookup for the main workspace returns that closed PR and links it — even though main has long since advanced past the PR's head commit. The old v1 renderer path (pr-resolution.ts) guarded against exactly this (historical PRs only attach when the workspace HEAD still equals the PR head OID); the v2 host-service path dropped that guard.
Fix. Port the guard: closed/merged PRs are treated as historical and only stay attached while workspace.headSha still matches the PR's headRefOid. Otherwise the link is skipped (and any stale persisted link cleared). Open/draft/queued PRs are unaffected, and a merged/closed PR still shows while the workspace is sitting on that exact commit.
Bug 2 — branch with multiple PRs shows the closed one (#4513 item 2)
Root cause. normalizePullRequestCandidates (utils/github-query/github-query.ts) picked the first candidate matching the head branch. GitHub returns the list sorted by updated desc, so a recently-touched closed PR won out over the live open PR on the same branch.
Fix. Prefer the OPEN candidate among head-branch matches, falling back to the most-recently-updated only when none is open.
Tests
packages/host-service/src/runtime/pull-requests/:
pull-requests.test.ts— newdefault branch stale PR isolationblock: default branch does not link a closed PR it has advanced past; still links a closed PR while HEAD matches; clears a stale persisted link once HEAD moves on.utils/github-query/github-query.test.ts— prefers the open PR when a head branch has both closed and open PRs.
All three reproduction tests failed before the fix and pass after. Full src/runtime/pull-requests/ suite (28 tests) and tsc --noEmit are green.
Refs #5596
Closes #4508
Closes #4260
Closes #4513
Summary by cubic
Stops attaching stale or wrong PRs to the workspace sidebar by treating closed/merged PRs as historical and preferring open PRs when multiple exist for a branch. Addresses #5596 and prevents spurious PR icons on the default branch.
- Bug Fixes
- Default branch stale link: in
PullRequestRuntimeManager, closed/merged PRs only link ifworkspace.headShaequals the PRheadRefOid; otherwise we skip and clear any stale link. - Multiple PRs on the same head branch: in
utils/github-query/github-query.ts,normalizePullRequestCandidatesnow prefers theOPENcandidate, falling back to the latest only when none is open. - Tests: new cases in
pull-requests.test.tsandgithub-query.test.tscover default-branch isolation and open-over-closed selection.
- Default branch stale link: in