Skip to content

fix(host-service): solve #5596 — stop attaching stale/wrong PRs to the sidebar - #5619

Draft
github-actions[bot] wants to merge 1 commit into
mainfrom
triage/issue-5596-29170829580
Draft

github-actions[bot] wants to merge 1 commit into
mainfrom
triage/issue-5596-29170829580

Conversation

@github-actions

@github-actions github-actions Bot commented Jul 11, 2026 •

Copy link
Copy Markdown
Contributor

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 — new default branch stale PR isolation block: 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


Open in Stage

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 if workspace.headSha equals the PR headRefOid; otherwise we skip and clear any stale link.
    • Multiple PRs on the same head branch: in utils/github-query/github-query.ts, normalizePullRequestCandidates now prefers the OPEN candidate, falling back to the latest only when none is open.
    • Tests: new cases in pull-requests.test.ts and github-query.test.ts cover default-branch isolation and open-over-closed selection.

Written for commit eab4ae8. Summary will update on new commits.

Review in cubic

@ghost

ghost commented Jul 11, 2026

Copy link
Copy Markdown

Ready to review this PR? Stage has broken it down into 4 individual chapters for you:

Title
1 Prioritize open PRs during branch resolution
2 Define helpers for historical PR state detection
3 Track PR state and SHA during refresh
4 Prevent linking workspaces to stale historical PRs
Open in Stage

Chapters generated by Stage for commit eab4ae8 on Jul 11, 2026 10:45pm UTC.

This branch has not been deployed

No deployments
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.

Incorrect pull requests shown [bug] default branch should not indicate an associated pull request Main thinks its a merged PR

0 participants