Skip to content

fix(github): reject cached same-branch PRs with foreign heads - #32060

Merged
nwparker merged 2 commits into
mainfrom
nwparker/github-cached-pr-head-identity
Oct 23, 2026
Merged

nwparker merged 2 commits into
mainfrom
nwparker/github-cached-pr-head-identity

Conversation

@nwparker

@nwparker nwparker commented Oct 22, 2026 •

Copy link
Copy Markdown
Contributor
Files Added Deleted Net
Test 1 $\color{#1a7f37}{\Huge{\mathbf{+}}}$​196 0 $\color{#1a7f37}{\Huge{\mathbf{+}}}$​196
Prod 1 $\color{#1a7f37}{\Huge{\mathbf{+}}}$​30 $\color{#cf222e}{\Huge{\mathbf{−}}}$​2 $\color{#1a7f37}{\Huge{\mathbf{+}}}$​28

ELI5

If you open a worktree on main in a public repo, Orca could show somebody else's pull request on it: an outside contributor's PR opened from their fork's main branch. You'd see their failing checks, conflicts and review comments as if they were yours. Deleting the cache didn't help, because Orca remembers the PR number and fetches that PR straight back by number.

Orca's normal branch search already rejects PRs from other forks. The "fetch by remembered number" path skipped that check. Now it runs the same check: a PR whose branch name matches yours only sticks if it comes from your origin, from the fork your branch tracks, or from the base repository itself (a same-repo PR). If it's rejected, Orca also clears that remembered PR, so it doesn't come back on the next refresh.

What stays the same:

  • PRs you linked explicitly (including Orca's "start from PR" worktrees for fork PRs) still show.
  • Contributors who work from their own fork (origin = fork) still see their own PR.
  • Renamed local review branches, and checkouts where Orca can't tell which repo origin is, behave the same as before.
  • Renamed or transferred repos (an old origin URL like zeit/next.js that now redirects to vercel/next.js) still show their own same-repo PR. The first version of this PR broke that case; it's fixed in 2b626a6.

Related: #27811 (this fixes the cached-number path, which is the "it comes back after refresh" part of that report). Prior contributor attempt: #29330 by @AmoabaKelvin (closed, a broader rewrite of push-head resolution across providers). It proposed the same rule for remembered PR numbers; no code was taken from it.

Verification

Live against real GitHub, calling the real getPRForBranchOutcome with real gh and real scratch git repos (no mocks):

Scenario main This PR
origin stablyai/orca, branch main, remembered #28356 (from RandomPixelStudios's fork main) found #28356 (bug) no-pr, rejected URL .../pull/28356
origin RandomPixelStudios/orcaupdates + upstream stablyai/orca, branch main, #28356 (the contributor's own view) found found
origin stablyai/orca, branch of this PR, #32060 (your own same-repo PR) found found
origin uses the old zeit/next.js URL, branch fleet/prune-stale-dev-types, #100612 (same-repo PR in renamed vercel/next.js) found found (the original commit returned no-pr and cleared the cache; fixed in 2b626a6)

Unit tests: src/main/github/client-pr-foreign-head-cache.test.ts has 11 cases. The foreign-fork, same-owner-other-repo and branch-result-revival cases fail on main, and the renamed-remote case fails on the original commit. Together with the GitHub suites (src/main/github, src/renderer/src/store/github), 113 files / 1340 tests pass. check:code-quality:changed, oxlint and oxfmt pass.

Visual proof: N/A. No layout change; the effect is that the foreign PR stops showing, which the live table above covers.

Risks / tradeoffs

  • A cross-fork PR with your branch's exact name, found only by remembered number, with no explicit link and no tracked fork remote, is now hidden. Example: a fork's own repo was renamed while its origin URL is stale. Linking the PR explicitly brings it back.
  • No RPC, persistence or Git command changes, and no new fields, so mixed client/server versions are unaffected. SSH/WSL/Windows use the same main-process policy. This is platform-agnostic logic and wasn't separately run on remote hosts. GitLab and other providers are unchanged. Folder (non-git) workspaces never reach this path.

@coderabbitai

coderabbitai Bot commented Oct 22, 2026 •

Copy link
Copy Markdown
Contributor

Review in Change Stack →

📝 Walkthrough

The acceptance policy now rejects a pull request when its head branch matches the current branch, an expected head repository is known, and neither expected repository matches the pull request’s head repository. The policy retains the same-repository URL exception and the existing implicit-default-branch check. New tests cover cached lookup rejection and retention across repository identity, fork, branch rename, metadata, and checkout identity scenarios.

Priority: ⬇️ Low

Merge Risk: 🟡 Moderate · up to 2b626

A checkout can show an unrelated upstream pull request as its own. Restrict the exception before merging.

Pre-merge checks: Passed 4 · Failed 1

Failed Failed checks: 1 warning

Check name Status Explanation Resolution
Docstring Coverage Warning Docstring coverage is 25.00% which is insufficient. The required threshold is 80.00%. Docstring coverage is scoped to functions touched by this diff. Analyzed 4 functions across 2 files. Write docstrings for the functions missing them to satisfy the coverage threshold.
Passed Passed checks: 4 passed
Check name Status Explanation
Title check Passed The title clearly and concisely describes the main change: rejecting cached same-branch pull requests whose heads are from foreign repositories.
Description check Passed The description explains the user impact, implementation, rationale, linked issue, verification results, visual impact, risks, and platform considerations. It does not reproduce every template heading…
Linked Issues check Passed Check skipped because no linked issues were found for this pull request.
Out of Scope Changes check Passed Check skipped because no linked issues were found for this pull request.
Full details: Description check

Explanation

The description explains the user impact, implementation, rationale, linked issue, verification results, visual impact, risks, and platform considerations. It does not reproduce every template heading or checklist item, but the required substantive information is mostly present.

✨ Finishing Touches: 💡 1
📝 Generate docstrings: 💡
  • Commit to this branch
  • Create a new PR
  • Autofix · Keep fixing CodeRabbit findings and required CI, and resolving merge conflicts

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

Comment @coderabbitai help to get the list of available commands.

A stale origin URL after a repository rename or transfer made the
foreign-head check reject the checkout's own same-repo PR and clear its
cache. Treat a PR whose head matches its canonical base URL as local.

Co-Authored-By: Claude <noreply@anthropic.com>

@coderabbitai coderabbitai Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actionable comments posted: 1


ℹ️ Review info
⚙️ Run configuration
  • Source mode: Auto
  • Repository: Unavailable
  • Branch: Unavailable
  • File: No configuration file from this repository applied
  • Commit: Unavailable
  • Layers: Repository UI
  • Review profile: CHILL
  • Plan: Advanced
  • Run ID: a2191403-70cb-4fe3-a1e8-5667c6eef380
📥 Commits

Reviewing files that changed from the base of the PR and between b0ba672 and 2b626a6.

📒 Files selected for processing: 2
  • src/main/github/client-pr-foreign-head-cache.test.ts
  • src/main/github/client/lookup/implicit-default-branch-pr.ts

Included review availability: This review used your included allowance. Your plan provides up to 10 included reviews per hour; 0 remain after this review.

Comment on lines +72 to +73
if (headRepo && baseRepo && githubRepoIdentityKey(headRepo) === githubRepoIdentityKey(baseRepo)) {
return false

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🎯 Functional Correctness | 🟠 Major | ⚡ Quick win

🔎 Supported by static analysis

🏁 Script executed:

sed -n '95,140p' src/main/github/github-api-repository.ts
sed -n '195,245p' src/main/github/client/lookup/branch-lookup-resolution.ts
sed -n '85,145p' src/main/github/client/lookup/pr-number-lookup.ts
sed -n '1,110p' src/main/github/client/lookup/implicit-default-branch-pr.ts
rg -n 'originHeadRepo|trackedUpstream|lookupPRByNumber|pre-rename|same-repository' src/main/github/client/lookup src/main/github/client-pr-foreign-head-cache.test.ts

Repository: stablyai/orca

Length of output: 14428


🏁 Script executed:

sed -n '1,245p' src/main/github/client/lookup/branch-lookup-resolution.ts
printf '\n--- candidate resolver declaration and callers ---\n'
rg -n -F --glob '*.ts' -e 'resolveGitHubApiRepositoryCandidates' -e 'createImplicitDefaultBranchPRPolicy' -e 'originHeadRepo:' -e 'trackedUpstream =' src/main/github
printf '\n--- relevant tests ---\n'
sed -n '130,250p' src/main/github/client-pr-foreign-head-cache.test.ts
printf '\n--- policy tests and references ---\n'
rg -n -F --glob '*.{ts,tsx}' -e 'isForeignSameNamedHead' -e 'same-repository' -e 'fallbackPRNumber' -e 'upstream' src/main/github/client src/main/github/*test.ts

Repository: stablyai/orca

Length of output: 41457


🏁 Script executed:

sed -n '1,225p' src/main/github/client-pr-foreign-head-cache.test.ts
printf '\n--- test fixture and lookup mocks ---\n'
rg -n -F --glob 'src/main/github/client-pr-foreign-head-cache.test.ts' -e 'function primeLookup' -e 'const upstream' -e 'const origin' -e 'const foreign' -e 'trackedFork' -e 'checkout:' -e 'head:' -e 'expected:'
printf '\n--- policy data contracts ---\n'
sed -n '120,180p' src/main/github/client/lookup/pull-request-lookup-data.ts
sed -n '1,125p' src/main/github/client/lookup/tracked-upstream-cache.ts

Repository: stablyai/orca

Length of output: 14767


Restrict the same-repository exception to trusted candidate repositories.

lookupPRByNumber checks every candidate from resolveGitHubApiRepositoryCandidates. The current exception accepts a candidate when the PR head repository matches the repository in the PR URL. It does not require that candidate to match originHeadRepo or trackedUpstream.headRepo.

An untracked upstream candidate can therefore pass the fallback lookup for a same-named PR. Reject that candidate. Compare repository identities so renamed origin URLs retain the existing behavior.

🐛 Suggested fix
--- "a/src/main/github/client/lookup/implicit-default-branch-pr.ts"
+++ "b/src/main/github/client/lookup/implicit-default-branch-pr.ts"
@@ -66,12 +66,20 @@
   if (data.headRefName !== context.branchName || expectedHeads.length === 0) {
     return false
   }
   const headRepo = pullRequestHeadRepository(data, prRepo)
   const baseRepo = data.url ? ownerRepoFromPullRequestUrl(data.url) : null
+  const trustedPRRepo =
+    prRepo !== null &&
+    expectedHeads.some((head) => githubRepoIdentityKey(head) === githubRepoIdentityKey(prRepo))
   // Why: a same-repo PR lives in a checkout remote; its canonical URL survives stale renamed-remote URLs.
-  if (headRepo && baseRepo && githubRepoIdentityKey(headRepo) === githubRepoIdentityKey(baseRepo)) {
+  if (
+    trustedPRRepo &&
+    headRepo &&
+    baseRepo &&
+    githubRepoIdentityKey(headRepo) === githubRepoIdentityKey(baseRepo)
+  ) {
     return false
   }
   return expectedHeads.every((head) => !pullRequestMatchesHeadRepository(data, prRepo, head))
 }
 

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.

1 participant