@@ -32,6 +32,7 @@ import {
3232import {
3333 detectSourceControlProviderFromGitRemoteUrl ,
3434 mergeGitStatusParts ,
35+ normalizeGitRemoteUrl ,
3536 resolveAutoFeatureBranchName ,
3637 sanitizeBranchFragment ,
3738 sanitizeFeatureBranchName ,
@@ -145,6 +146,7 @@ interface BranchHeadContext {
145146 headSelectors : ReadonlyArray < string > ;
146147 preferredHeadSelector : string ;
147148 remoteName : string | null ;
149+ headRemoteUrlKey : string | null ;
148150 headRepositoryNameWithOwner : string | null ;
149151 headRepositoryOwnerLogin : string | null ;
150152 isCrossRepository : boolean ;
@@ -792,10 +794,17 @@ export const make = Effect.gen(function* () {
792794 const prLookupCache = yield * Cache . makeWith (
793795 ( key : string ) => {
794796 const [ cwd = "" , branch = "" , upstreamRef = "" ] = key . split ( "\u0000" ) ;
795- return findLatestPr ( cwd , {
797+ const details = {
796798 branch,
797799 upstreamRef : upstreamRef . length > 0 ? upstreamRef : null ,
798- } ) ;
800+ } ;
801+ return resolveBranchHeadContext ( cwd , details ) . pipe (
802+ Effect . flatMap ( ( headContext ) =>
803+ findLatestPrForHeadContext ( cwd , headContext ) . pipe (
804+ Effect . map ( ( latest ) => ( { latest, headContext } ) ) ,
805+ ) ,
806+ ) ,
807+ ) ;
799808 } ,
800809 {
801810 capacity : PR_LOOKUP_CACHE_CAPACITY ,
@@ -804,9 +813,17 @@ export const make = Effect.gen(function* () {
804813 ) ;
805814 // A transient lookup failure (rate limit, network blip) must not clear an
806815 // already-known PR badge, so the last successful answer per branch sticks
807- // around as the fallback.
808- const lastKnownPrByBranchKey = new Map < string , ReturnType < typeof toStatusPr > | null > ( ) ;
809- const rememberLastKnownPr = ( branchKey : string , pr : ReturnType < typeof toStatusPr > | null ) => {
816+ // around as the fallback. Keep the resolved head context with it so a
817+ // branch retargeted to another remote/fork cannot inherit the old badge.
818+ interface LastKnownPr {
819+ readonly pr : ReturnType < typeof toStatusPr > | null ;
820+ readonly upstreamRef : string | null ;
821+ readonly headBranch : string ;
822+ readonly remoteName : string | null ;
823+ readonly headRemoteUrlKey : string | null ;
824+ }
825+ const lastKnownPrByBranchKey = new Map < string , LastKnownPr > ( ) ;
826+ const rememberLastKnownPr = ( branchKey : string , entry : LastKnownPr ) => {
810827 if (
811828 ! lastKnownPrByBranchKey . has ( branchKey ) &&
812829 lastKnownPrByBranchKey . size >= PR_LOOKUP_CACHE_CAPACITY
@@ -816,7 +833,32 @@ export const make = Effect.gen(function* () {
816833 lastKnownPrByBranchKey . delete ( oldestKey ) ;
817834 }
818835 }
819- lastKnownPrByBranchKey . set ( branchKey , pr ) ;
836+ lastKnownPrByBranchKey . set ( branchKey , entry ) ;
837+ } ;
838+ const resolveLastKnownPr = (
839+ branchKey : string ,
840+ current : Pick < LastKnownPr , "upstreamRef" | "headBranch" | "remoteName" | "headRemoteUrlKey" > ,
841+ ) : ReturnType < typeof toStatusPr > | null => {
842+ const lastKnown = lastKnownPrByBranchKey . get ( branchKey ) ;
843+ if ( ! lastKnown ) return null ;
844+ if ( lastKnown . headBranch !== current . headBranch ) {
845+ return null ;
846+ }
847+
848+ // The normalized URL catches both remote-alias changes and an existing
849+ // alias being repointed. It also lets an upstream appear after `push -u`
850+ // without invalidating the fallback when it still targets the same repo.
851+ if ( lastKnown . headRemoteUrlKey !== null || current . headRemoteUrlKey !== null ) {
852+ return lastKnown . headRemoteUrlKey === current . headRemoteUrlKey ? lastKnown . pr : null ;
853+ }
854+
855+ // If neither remote URL is available, fall back to the remote identity
856+ // encoded by tracked branches. A null-to-non-null transition is allowed
857+ // because that is the expected first-push case.
858+ if ( lastKnown . upstreamRef !== null && current . upstreamRef !== null ) {
859+ return lastKnown . remoteName === current . remoteName ? lastKnown . pr : null ;
860+ }
861+ return lastKnown . pr ;
820862 } ;
821863 const lookupStatusPr = Effect . fn ( "lookupStatusPr" ) ( function * (
822864 cwd : string ,
@@ -826,14 +868,27 @@ export const make = Effect.gen(function* () {
826868 // `push -u`) must not orphan the fallback value for the same branch.
827869 const branchKey = `${ cwd } \u0000${ details . branch } ` ;
828870 return yield * Cache . get ( prLookupCache , prLookupCacheKey ( cwd , details ) ) . pipe (
829- Effect . map ( ( latest ) => {
830- if ( ! latest ) return null ;
871+ Effect . map ( ( { latest, headContext } ) => {
872+ if ( ! latest ) return { pr : null , headContext } ;
831873 // On the default branch, only surface open PRs.
832874 // Merged/closed matches are usually reverse-merge history, not the thread's PR context.
833- if ( details . isDefaultBranch && latest . state !== "open" ) return null ;
834- return toStatusPr ( latest ) ;
875+ if ( details . isDefaultBranch && latest . state !== "open" ) {
876+ return { pr : null , headContext } ;
877+ }
878+ return { pr : toStatusPr ( latest ) , headContext } ;
835879 } ) ,
836- Effect . tap ( ( pr ) => Effect . sync ( ( ) => rememberLastKnownPr ( branchKey , pr ) ) ) ,
880+ Effect . tap ( ( { pr, headContext } ) =>
881+ Effect . sync ( ( ) =>
882+ rememberLastKnownPr ( branchKey , {
883+ pr,
884+ upstreamRef : details . upstreamRef ,
885+ headBranch : headContext . headBranch ,
886+ remoteName : headContext . remoteName ,
887+ headRemoteUrlKey : headContext . headRemoteUrlKey ,
888+ } ) ,
889+ ) ,
890+ ) ,
891+ Effect . map ( ( { pr } ) => pr ) ,
837892 Effect . catch ( ( error ) =>
838893 Effect . logWarning ( "PR lookup failed; keeping last known PR state." ) . pipe (
839894 Effect . annotateLogs ( {
@@ -844,7 +899,15 @@ export const make = Effect.gen(function* () {
844899 ? String ( error . _tag )
845900 : typeof error ,
846901 } ) ,
847- Effect . as ( lastKnownPrByBranchKey . get ( branchKey ) ?? null ) ,
902+ Effect . andThen ( resolveBranchHeadContext ( cwd , details ) ) ,
903+ Effect . map ( ( headContext ) =>
904+ resolveLastKnownPr ( branchKey , {
905+ upstreamRef : details . upstreamRef ,
906+ headBranch : headContext . headBranch ,
907+ remoteName : headContext . remoteName ,
908+ headRemoteUrlKey : headContext . headRemoteUrlKey ,
909+ } ) ,
910+ ) ,
848911 ) ,
849912 ) ,
850913 ) ;
@@ -910,6 +973,7 @@ export const make = Effect.gen(function* () {
910973 ) {
911974 if ( ! remoteName ) {
912975 return {
976+ remoteUrlKey : null ,
913977 repositoryNameWithOwner : null ,
914978 ownerLogin : null ,
915979 } ;
@@ -918,6 +982,7 @@ export const make = Effect.gen(function* () {
918982 const remoteUrl = yield * readConfigValueNullable ( cwd , `remote.${ remoteName } .url` ) ;
919983 const repositoryNameWithOwner = parseGitHubRepositoryNameWithOwnerFromRemoteUrl ( remoteUrl ) ;
920984 return {
985+ remoteUrlKey : remoteUrl ? normalizeGitRemoteUrl ( remoteUrl ) : null ,
921986 repositoryNameWithOwner,
922987 ownerLogin : parseRepositoryOwnerLogin ( repositoryNameWithOwner ) ,
923988 } ;
@@ -988,6 +1053,9 @@ export const make = Effect.gen(function* () {
9881053 preferredHeadSelector :
9891054 ownerHeadSelector && isCrossRepository ? ownerHeadSelector : headBranch ,
9901055 remoteName,
1056+ headRemoteUrlKey :
1057+ remoteRepository . remoteUrlKey ??
1058+ ( remoteName === null ? originRepository . remoteUrlKey : null ) ,
9911059 headRepositoryNameWithOwner : remoteRepository . repositoryNameWithOwner ,
9921060 headRepositoryOwnerLogin : remoteRepository . ownerLogin ,
9931061 isCrossRepository,
@@ -1033,11 +1101,10 @@ export const make = Effect.gen(function* () {
10331101 return null ;
10341102 } ) ;
10351103
1036- const findLatestPr = Effect . fn ( "findLatestPr " ) ( function * (
1104+ const findLatestPrForHeadContext = Effect . fn ( "findLatestPrForHeadContext " ) ( function * (
10371105 cwd : string ,
1038- details : { branch : string ; upstreamRef : string | null } ,
1106+ headContext : BranchHeadContext ,
10391107 ) {
1040- const headContext = yield * resolveBranchHeadContext ( cwd , details ) ;
10411108 const parsedByNumber = new Map < number , PullRequestInfo > ( ) ;
10421109
10431110 for ( const headSelector of headContext . headSelectors ) {
@@ -1064,7 +1131,6 @@ export const make = Effect.gen(function* () {
10641131 }
10651132 return parsed [ 0 ] ?? null ;
10661133 } ) ;
1067-
10681134 const buildCompletionToast = Effect . fn ( "buildCompletionToast" ) ( function * (
10691135 cwd : string ,
10701136 result : Pick < GitRunStackedActionResult , "action" | "branch" | "commit" | "push" | "pr" > ,
0 commit comments