fix(worktrees): keep remote-owned repos out of local Git routes

Registrations owned by another host never get a local Git route, WSL
discovery notifies after the distro list is written and isolates its
listeners, and tests cover idle retirement on route-input and discovery
changes plus pruning that loses owner authority mid-helper.

Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
This commit is contained in:
Brennan Benson
2026-11-12 00:54:44 -08:00
co-authored by Claude Opus 5.5
parent a3d66c4733
commit 80712196d6
6 changed files with 76 additions and 6 deletions
@@ -30,9 +30,12 @@ vi.mock('./authoritative-local-worktree-metadata-pruning', () => ({
const {
DETECTED_WORKTREE_SCAN_CACHE_TTL_MS,
__resetDetectedWorktreeScanCacheForTests,
applyFreshDetectedWorktreeScanSideEffects,
invalidateDetectedWorktreeScanCache,
listDetectedGitWorktrees
} = await import('./detected-worktree-scan-cache')
const { setWorktreeCatalogRouteResolver } =
await import('../../../workspaces/catalog/repo-worktree-catalog-route')
const { invalidateLocalWorktreeMetadataPruneInputs } =
await import('../../../workspaces/catalog/local-worktree-metadata-prune-gate')
@@ -115,4 +118,35 @@ describe('detected worktree scan hygiene gate', () => {
advancePastListingTtl()
expect((await listDetectedGitWorktrees(store, repo)).hygieneDue).toBe(true)
})
it('stops deleting once the owner retires the route during the prune helper', async () => {
let live = true
setWorktreeCatalogRouteResolver(() =>
live ? { repoPath: repo.path, runtimeKey: 'local:default' } : null
)
try {
const scan = await listDetectedGitWorktrees(store, repo)
let helperSawCurrent: boolean | undefined
pruneMetadataMock.mockImplementation(async (args: { isCallerCurrent: () => boolean }) => {
live = false
helperSawCurrent = args.isCallerCurrent()
return { scanGenerationCurrent: true, preservedMetadataCandidateIds: new Set() }
})
const applied = await applyFreshDetectedWorktreeScanSideEffects(
store,
repo,
scan.gitWorktrees,
scan.metadataPrune,
{ sideEffectToken: scan.sideEffectToken, hygieneDue: scan.hygieneDue }
)
expect(helperSawCurrent).toBe(false)
expect(applied).toBe(false)
expect(pruneLineageMock).not.toHaveBeenCalled()
expect(scan.authorityCurrent?.()).toBe(false)
} finally {
setWorktreeCatalogRouteResolver(null)
}
})
})
@@ -1,4 +1,4 @@
import { getRepoExecutionHostId } from '../../../../shared/execution-host'
import { getRepoExecutionHostId, LOCAL_EXECUTION_HOST_ID } from '../../../../shared/execution-host'
import { isFolderRepo } from '../../../../shared/repo-kind'
import {
setWorktreeCatalogRouteResolver,
@@ -22,7 +22,8 @@ function buildRoutes(store: RuntimeStore): Map<string, WorktreeCatalogRoute> {
const projectRuntimeByRepoId = resolveLocalProjectRuntimesForRepos(store as never, repos)
const routes = new Map<string, WorktreeCatalogRoute>()
for (const repo of repos) {
if (isFolderRepo(repo)) {
// Only this host's repos run local Git; remote-owned registrations answer from their own host.
if (isFolderRepo(repo) || getRepoExecutionHostId(repo) !== LOCAL_EXECUTION_HOST_ID) {
continue
}
const { projectRuntime, runtimeKey, sshConnectionId } = resolveWorktreeScanRuntime(
@@ -3,7 +3,10 @@ import type { Repo } from '../../../shared/repo-types'
import type { GitWorktreeInfo } from '../../../shared/worktree/types'
import { bumpWorktreeScanGeneration } from '../../git/worktrees/catalog/worktree-scan-cache'
import { setWorktreeCatalogRouteResolver } from './repo-worktree-catalog-route'
import { markWorktreeRouteInputsChanged } from './local-worktree-scan-generation'
import { notifyWslDiscoveryChanged } from '../../wsl/discovery/wsl-discovery-change'
import {
_getWorktreeCatalogOwnerSizeForTests,
_resetWorktreeCatalogOwnerForTests,
retireObsoleteWorktreeCatalogs,
invalidateWorktreeCatalogForRepo,
@@ -181,6 +184,28 @@ describe('repo worktree catalog owner', () => {
expect(plan.calls).toBe(1)
})
it('retires an idle owner when a route input or WSL discovery answer changes, with no reader', async () => {
let live: { repoPath: string; runtimeKey: string } | null = {
repoPath: '/repos/app',
runtimeKey: 'local:default'
}
setWorktreeCatalogRouteResolver(() => live)
const plan = scanner([rows('/repos/app')])
await readWorktreeCatalog(repo, 'local:default', runtime, plan)
expect(_getWorktreeCatalogOwnerSizeForTests()).toBe(1)
live = { repoPath: '/repos/app', runtimeKey: 'local:wsl:Ubuntu' }
notifyWslDiscoveryChanged()
expect(_getWorktreeCatalogOwnerSizeForTests()).toBe(0)
live = { repoPath: '/repos/app', runtimeKey: 'local:default' }
await readWorktreeCatalog(repo, 'local:default', runtime, plan)
live = null
markWorktreeRouteInputsChanged()
await Promise.resolve()
expect(_getWorktreeCatalogOwnerSizeForTests()).toBe(0)
})
it('keeps a scan that outlived its repo path out of the live catalog', async () => {
let live = { repoPath: '/repos/app', runtimeKey: 'local:default' }
setWorktreeCatalogRouteResolver(() => live)
@@ -313,3 +313,7 @@ export function _resetWorktreeCatalogOwnerForTests(): void {
retireEntry(key, entry)
}
}
export function _getWorktreeCatalogOwnerSizeForTests(): number {
return entries.size
}
+4 -3
View File
@@ -77,12 +77,13 @@ function cacheWslDistroList(rawDistros: string[], probeSequence: number): string
if (userDistros.length === 0 && wslDistroCache !== null && wslDistroCache.length > 0) {
return wslDistroCache
}
if (userDistros.length > 0) {
dropStaleWslAvailabilityFailure()
}
wslDistroCacheSequence = probeSequence
const changed = wslDistroCache?.join('\0') !== userDistros.join('\0')
wslDistroCache = userDistros
// After the list is written, so route reconciliation never sees half-updated discovery.
if (userDistros.length > 0) {
dropStaleWslAvailabilityFailure()
}
if (changed) {
notifyWslDiscoveryChanged()
}
@@ -14,6 +14,11 @@ export function getWslDiscoveryRevision(): number {
export function notifyWslDiscoveryChanged(): void {
revision += 1
for (const listener of listeners) {
listener()
try {
listener()
} catch (error) {
// A listener failure must not break the discovery write that notified it.
console.warn('[wsl] discovery change listener failed:', error)
}
}
}