Files
orca/src/main/git/git-capability-state.ts
T
OrcaWinandm4air 4085e1cf60 fix(memory): release stale session registries (#21734)
* fix(memory): bound session and lifecycle registries

* fix(memory): bound transient filesystem registries

* fix(memory): cap path and locale caches

* fix(memory): bound runtime recovery registries

* fix(memory): bound host mirror gap verdicts

* fix(memory): bound shell startup env cache

* fix(memory): bound gitlab host context cache

* fix(memory): release removed ssh generations

* fix(memory): expire cloud refresh replay guards

* fix(memory): release retired plugin generations

* fix(memory): bound plugin log key retention

* fix(memory): bound automation authority generations

* fix(memory): bound native chat enrichment cache

* fix(memory): bound web session tracking generations

* fix(memory): bound codex credential absence paths

* fix(memory): bound WSL canonical path cache

* fix(memory): bound sparse checkout cache

* fix(memory): bound shared directory cache

* fix(memory): bound advertised URL scan snapshots

* fix(memory): bound automation manager cache

* fix(memory): bound web session reorder intents

* fix(memory): bound web session focus intents

* fix(memory): bound web session handoffs

* fix(memory): bound automation dispatch tokens

* fix(memory): bound host mirror waiters

* fix(memory): bound retained session activity

* fix(memory): bound retained session activity

* fix(memory): bound web session close intents

* fix(memory): bound cloud session cache

* fix(memory): bound WSL home cache

* fix(memory): bound SSH capability cache

* fix(memory): bound trust grant cooldowns

* fix(memory): bound WSL auth drain state

* fix(memory): bound Linear workspace credential cache

* fix(memory): bound local Git capability cache

* fix(memory): bound WSL Git environment cache

* fix(memory): bound WSL Git environment cache

* fix(memory): bound WSL preflight cache

* fix(memory): keep hot cache entries warm

* fix(memory): preserve generation fences across eviction

* fix(memory): close remaining eviction fences

* fix(memory): align evicted upstream generations

* fix(memory): trim successful capability probes

* fix(auth): retain expired refresh replay evidence

---------

Co-authored-by: m4air <m4air@Mac.localdomain>
2026-09-20 14:41:50 -07:00

82 lines
2.7 KiB
TypeScript

import { GitCapabilityCache } from '../../shared/git-capability-cache'
import type { SshGitProvider } from '../providers/ssh-git-provider'
import { parseWslUncPath } from '../../shared/wsl-paths'
import {
isWslLinkedWorktreeGitRoutingCandidate,
prepareWslLinkedWorktreeGitRouting
} from './wsl-linked-worktree-git-routing'
type LocalGitCapabilityTarget = {
cwd?: string
wslDistro?: string
signal?: AbortSignal
}
const localCapabilitiesByExecutionHost = new Map<string, GitCapabilityCache>()
const MAX_LOCAL_GIT_CAPABILITY_HOSTS = 128
// Why: reconnecting creates a new provider, while concurrent IPC/runtime users
// of one SSH connection must share the same remote Git capability results.
let sshCapabilitiesByProvider = new WeakMap<SshGitProvider, GitCapabilityCache>()
function getLocalGitExecutionHostKey(target: LocalGitCapabilityTarget): string {
const wslDistro =
target.wslDistro ?? (target.cwd ? parseWslUncPath(target.cwd)?.distro : undefined)
return wslDistro ? `wsl:${wslDistro}` : 'local'
}
export function getLocalGitCapabilityCache(
target: LocalGitCapabilityTarget = {}
): GitCapabilityCache {
const executionHost = getLocalGitExecutionHostKey(target)
let cache = localCapabilitiesByExecutionHost.get(executionHost)
if (!cache) {
cache = new GitCapabilityCache()
}
localCapabilitiesByExecutionHost.delete(executionHost)
localCapabilitiesByExecutionHost.set(executionHost, cache)
while (localCapabilitiesByExecutionHost.size > MAX_LOCAL_GIT_CAPABILITY_HOSTS) {
const oldest = localCapabilitiesByExecutionHost.keys().next().value
if (oldest === undefined) {
break
}
localCapabilitiesByExecutionHost.delete(oldest)
}
return cache
}
export function withLocalGitCapabilityCacheForExecution<T>(
target: LocalGitCapabilityTarget,
run: (capabilities: GitCapabilityCache) => Promise<T>
): Promise<T> {
if (!target.cwd || !isWslLinkedWorktreeGitRoutingCandidate(target.cwd, target.wslDistro)) {
try {
return run(getLocalGitCapabilityCache(target))
} catch (error) {
return Promise.reject(error)
}
}
return prepareWslLinkedWorktreeGitRouting(target.cwd, target.wslDistro, {
signal: target.signal
}).then((usesHostGit) =>
run(
getLocalGitCapabilityCache(
usesHostGit ? { cwd: target.cwd } : { cwd: target.cwd, wslDistro: target.wslDistro }
)
)
)
}
export function getSshGitCapabilityCache(provider: SshGitProvider): GitCapabilityCache {
let cache = sshCapabilitiesByProvider.get(provider)
if (!cache) {
cache = new GitCapabilityCache()
sshCapabilitiesByProvider.set(provider, cache)
}
return cache
}
export function clearGitCapabilityStateForTests(): void {
localCapabilitiesByExecutionHost.clear()
sshCapabilitiesByProvider = new WeakMap()
}