@@ -260,23 +260,29 @@ export const make = Effect.gen(function* () {
260260 fingerprint : fingerprintStatusPart ( local ) ,
261261 value : local ,
262262 } satisfies CachedValue < VcsStatusLocalResult > ;
263- const shouldPublish = yield * Ref . modify ( cacheRef , ( cache ) => {
263+ const change = yield * Ref . modify ( cacheRef , ( cache ) => {
264264 const previous = cache . get ( cwd ) ?? { local : null , remote : null } ;
265+ const branchChanged =
266+ previous . local !== null && previous . local . value . refName !== local . refName ;
265267 const nextCache = new Map ( cache ) ;
266268 nextCache . set ( cwd , {
267- ...previous ,
268269 local : nextLocal ,
270+ remote : branchChanged
271+ ? { fingerprint : fingerprintStatusPart ( null ) , value : null }
272+ : previous . remote ,
269273 } ) ;
270- return [ previous . local ?. fingerprint !== nextLocal . fingerprint , nextCache ] as const ;
274+ return [
275+ { changed : previous . local ?. fingerprint !== nextLocal . fingerprint , branchChanged } ,
276+ nextCache ,
277+ ] as const ;
271278 } ) ;
272279
273- if ( options ?. publish && shouldPublish ) {
280+ if ( options ?. publish && change . changed ) {
274281 yield * PubSub . publish ( changesPubSub , {
275282 cwd,
276- event : {
277- _tag : "localUpdated" ,
278- local,
279- } ,
283+ event : change . branchChanged
284+ ? { _tag : "snapshot" , local, remote : null }
285+ : { _tag : "localUpdated" , local } ,
280286 } ) ;
281287 }
282288
@@ -318,18 +324,33 @@ export const make = Effect.gen(function* () {
318324 cwd : string ,
319325 local : VcsStatusLocalResult ,
320326 remote : VcsStatusRemoteResult | null ,
321- options ?: { publish ?: boolean } ,
327+ options ?: { publish ?: boolean ; expectedLocal ?: CachedValue < VcsStatusLocalResult > | null } ,
322328 ) {
323- const nextLocal = {
329+ let nextLocal = {
324330 fingerprint : fingerprintStatusPart ( local ) ,
325331 value : local ,
326332 } satisfies CachedValue < VcsStatusLocalResult > ;
327- const nextRemote = {
333+ let nextRemote = {
328334 fingerprint : fingerprintStatusPart ( remote ) ,
329335 value : remote ,
330336 } satisfies CachedValue < VcsStatusRemoteResult | null > ;
331337 const shouldPublish = yield * Ref . modify ( cacheRef , ( cache ) => {
332338 const previous = cache . get ( cwd ) ?? { local : null , remote : null } ;
339+ if (
340+ options &&
341+ "expectedLocal" in options &&
342+ previous . local !== options . expectedLocal &&
343+ previous . local
344+ ) {
345+ // A local refresh completed while this remote read was in flight.
346+ // Preserve its local state, and never attach an old branch's remote result.
347+ if ( previous . local . value . refName !== local . refName ) {
348+ remote = previous . remote ?. value ?? null ;
349+ nextRemote = { fingerprint : fingerprintStatusPart ( remote ) , value : remote } ;
350+ }
351+ local = previous . local . value ;
352+ nextLocal = previous . local ;
353+ }
333354 const nextCache = new Map ( cache ) ;
334355 nextCache . set ( cwd , {
335356 local : nextLocal ,
@@ -483,20 +504,22 @@ export const make = Effect.gen(function* () {
483504 const refresh = withRemoteWriteLock (
484505 cwd ,
485506 Effect . gen ( function * ( ) {
507+ const expectedLocal = ( yield * getCachedStatus ( cwd ) ) ?. local ?? null ;
486508 yield * workflow . invalidateStatus ( cwd ) ;
487509 const [ local , remote ] = yield * Effect . all (
488510 [ workflow . localStatus ( { cwd } ) , workflow . remoteStatus ( { cwd } ) ] ,
489511 { concurrency : "unbounded" } ,
490512 ) ;
491- const pulled = yield * maybeAutoPull ( cwd , remote , [ rawCwd ] ) ;
513+ const unchanged = ( ( yield * getCachedStatus ( cwd ) ) ?. local ?? null ) === expectedLocal ;
514+ const pulled = unchanged ? yield * maybeAutoPull ( cwd , remote , [ rawCwd ] ) : null ;
492515 if ( pulled !== null ) return mergeGitStatusParts ( pulled . local , pulled . remote ) ;
493- return yield * updateCachedStatus ( cwd , local , remote , { publish : true } ) ;
516+ return yield * updateCachedStatus ( cwd , local , remote , { publish : true , expectedLocal } ) ;
494517 } ) ,
495518 ) ;
496519 if ( options ?. waitForRemote !== false ) return yield * refresh ;
497520
498- const cached = yield * getCachedStatus ( cwd ) ;
499521 const local = yield * refreshLocalStatusCore ( cwd ) ;
522+ const cached = yield * getCachedStatus ( cwd ) ;
500523 // The UI already subscribes to status updates. A slow fetch or PR lookup
501524 // must not hold its refresh request open or queue duplicate remote reads.
502525 yield * Effect . uninterruptibleMask ( ( restore ) =>
@@ -522,7 +545,7 @@ export const make = Effect.gen(function* () {
522545 cwd ,
523546 Effect . gen ( function * ( ) {
524547 const cached = yield * getCachedStatus ( cwd ) ;
525- if ( cached ?. remote ?. value == null ) return null ;
548+ if ( cached ?. remote == null || ! cached . local ?. value . isRepo ) return null ;
526549 const poller = ( yield * SynchronizedRef . get ( pollersRef ) ) . get ( cwd ) ;
527550 const demandCwds = poller ? [ ...( yield * Ref . get ( poller . demandCwds ) ) . keys ( ) ] : [ rawCwd ] ;
528551 const shouldRefresh = ( yield * Effect . forEach (
0 commit comments