fix(host-service): refresh past PRs in a workspace's PR history

HA
5d ago
+297-2

Problem

A customer (Slack, ext-trainloop; SUPER-3429) saw #1512 in the Workspace activity panel still shown as open after it was closed. Clicking Refresh pull request did not fix it.

PostHog shows the sequence on 2026-11-11 around 18:58 UTC: they clicked the #1512 TRA-1741 … row in the Workspace activity panel, clicked Refresh pull request in the PR pane that opened, then reopened the row.

Root cause

  • The panel's rows come from pullRequests.historyByWorkspaces. That query lists every PR the workspace has ever been linked to, read from the host-service DB.
  • PullRequestRuntimeManager only refreshes PRs through a head lookup on each workspace's current upstream ref. When a workspace moves on (checking out another stack layer through useOpenStackLayer, or switching branch), the earlier PR becomes history. Its row is never fetched again, so a later close or merge never reaches the DB. The upsertPullRequestRow comment already says so: "fetches cover heads of unarchived active workspaces, so other rows keep what they have."
  • Refresh pull request (useRefreshPullRequest) only invalidates the GitHub-backed detail, diff, threads and git.getPullRequest queries. It does not trigger a host sync and does not invalidate historyByWorkspaces. Even if the row had been fixed, the button would not have shown it.

Fix

  • performProjectRefresh now also refetches, by number, linked history PRs for the in-scope workspaces when they are still open/draft/queued and the head lookup did not just refresh them. Once a PR is closed or merged it drops out of the set, so the cost is bounded by the open PRs in a workspace's history. Calls use the same TTL cache (60 s, with failure backoff), availability gate, explicit-refresh probe and gh→Octokit fallback as the other runtime lookups. New helpers: fetchPullRequestByNumber{,FromGh}.
  • The PR pane's refresh button also calls pullRequests.refreshByWorkspaces for the workspace and then invalidates historyByWorkspaces, so the panel updates at once instead of on the next 5-minute sweep.

No tRPC procedure or schema changes.

Testing

  • New regression test: a workspace links #101, moves to a branch with #102, then #101 is closed on GitHub. History must show [102 open, 101 closed], and a further refresh must not look up #101 again. The test fails without the fix and passes with it.
  • bun test src/runtime/pull-requests (host-service): 113 pass.
  • PullRequestPane tests (desktop): 14 pass.
  • bun run typecheck passes for host-service and desktop. Biome is clean.
  • Not verified end-to-end in the running app.

Summary by cubic

Fixes the Workspace activity panel showing past pull requests as open even after they're closed or merged. The runtime only refreshed PRs on a workspace's current branch, so once a workspace moved on, earlier PRs kept stale state forever, and the Refresh button couldn't update the panel.

Changes

  • Project refresh now fetches open/draft/queued history PRs by number, dropping them once GitHub reports them closed or merged, using the same TTL cache, availability gate, and gh→Octokit fallback as other runtime lookups.
  • Number lookups run in batches of 8 and don't overwrite rows a newer lookup already refreshed.
  • A past PR that left the merge queue but is still open now stores "open" instead of a stale "queued".
  • The PR pane's Refresh button now syncs the workspace's PRs and invalidates the history and git.getPullRequest queries so the panel updates immediately; its spinner also covers the sync.

Summary by CodeRabbit

  • Bug Fixes
    • Refreshing a workspace’s pull-request pane now syncs its pull requests and updates the displayed history after the sync completes. The refresh indicator remains active while both the detail refresh and workspace sync are in progress.
    • Previously linked pull requests can have their status refreshed even after the workspace switches branches, helping history reflect whether requests remain open or have closed. Requests found on the current branch are not refreshed a second time.