Files
orca/src/main/git/remote-url-probe.ts
T
Jinjing ced4a2a959 fix(P1-D): bound hosted-review in-flight lookups so a wedged provider cannot pin a branch (#12030)
* fix(P1-D): bound hosted-review lookups with a detachable deadline

The `inflight` map in the hosted-review branch cache was only ever cleared
when the lookup settled, and nothing bounded how long that took. One wedged
provider call pinned its branch for the life of the process: every later poll
joined the same dead promise, so the card loaded forever with no in-session
recovery.

Each lookup now runs under a 120s deadline. Nothing below the funnel can be
cancelled, so the deadline detaches instead: the record is released, the
callers get the last known review (or a timeout error), and the branch enters
the existing failure backoff. The lookup keeps running and its answer is still
adopted if it lands, so a slow-but-alive host converges rather than failing
forever. A token identity keeps a detached lookup from evicting the record
that replaced it, and a wall-clock sweep expires records whose timer never
fired — main's timers are suspended across system sleep. `inflight` is capped
independently of the completed cache.

The failure backoff moves to its own module: it has a different lifetime from
the answer cache and is what a deadline records against.

* fix(P1-D): bound `git remote get-url` on the local/WSL path

`getRemoteUrlForRepo` ran the git child with no timeout, which is the one
unbounded step under the hosted-review lookup funnel: `git/runner.ts` only
arms its kill path when a timeout is passed, so a dead network mount or a
stalled WSL interop hangs the call and everything above it. The SSH branch is
already bounded by the relay mux's 30s request timeout, so it is unchanged.

* rm review doc

* rm review doc

* test(P1-D): add probe tests and transient-failure recovery verification

Add tests for coalesced-probe and remote-url-probe infrastructure. Add integration test verifying that transient Bitbucket API failures don't cache as a definitive no-review result, allowing recovery after cache TTL expiration.

* fix(P1-D): track lookups from start, prevent stale scope adoption

- Count unsettled lookups when they start, not after deadline expires: prevents multiple concurrent lookups for the same branch.
- Add evicted generation floor: prevents adopting stale results when scope is invalidated and evicted from the map.
- Consolidate duplicate repository reference cache logic into createRemoteRefProbeCache utility.
- Fix deadline wrapper in git config signature lookup: bound the caller's deadline only, not the coalesced probe itself.

* feat(P1-D): add remote-ref-probe-cache utility

Cache successful remote URL probes per repo/runtime to avoid duplicate work.
Skip caching transient errors and SSH failures so providers can retry on
reconnect, preventing stale scope adoption during the session.
2026-08-01 22:10:18 -07:00

102 lines
3.2 KiB
TypeScript

import {
getSshGitProvider,
SSH_GIT_PROVIDER_UNAVAILABLE_MESSAGE
} from '../providers/ssh-git-dispatch'
import { gitExecFileAsync } from './runner'
/**
* The `git remote get-url` probe every forge integration runs to decide whether
* a repo is theirs (P1-D).
*
* Why: it is a local config read, so the only way it outlasts this bound is a
* wedged host — a dead network mount or stalled WSL interop. Unbounded, the call
* never returns and every caller above it hangs with it. Passing a timeout is
* also what arms the runner's kill path; Node's own waits forever on a child
* that ignores signals. The SSH branch is bounded by the relay mux's own 30s
* request timeout.
*/
export const REMOTE_URL_PROBE_TIMEOUT_MS = 30_000
export type RemoteUrlProbeContext = {
repoPath: string
connectionId?: string | null
wslDistro?: string
}
/** Reads a remote URL, or null when the repo's SSH runtime is not connected. */
export async function readRemoteUrl(
context: RemoteUrlProbeContext,
remoteName: string
): Promise<string | null> {
if (context.connectionId) {
const provider = getSshGitProvider(context.connectionId)
if (!provider) {
return null
}
const { stdout } = await provider.exec(['remote', 'get-url', remoteName], context.repoPath)
return stdout
}
const { stdout } = await gitExecFileAsync(['remote', 'get-url', remoteName], {
cwd: context.repoPath,
timeout: REMOTE_URL_PROBE_TIMEOUT_MS,
...(context.wslDistro ? { wslDistro: context.wslDistro } : {})
})
return stdout
}
const TRANSIENT_PROBE_PATTERNS = [
/\btimed out\b/i,
/\bETIMEDOUT\b/,
/\bECONNRESET\b/,
/\bEPIPE\b/,
/connection (?:dropped|closed|refused|reset)/i
]
/**
* A probe that was killed on its deadline, or died with its transport, says
* nothing about the remote. Callers must neither cache it as an answer nor
* report it as "no review": it is an unavailable result, not a negative one.
*/
export function isTransientGitProbeError(error: unknown): boolean {
const parts: string[] = []
if (error instanceof Error) {
parts.push(error.message)
}
if (typeof error === 'object' && error !== null) {
const execLike = error as { stderr?: unknown; code?: unknown }
if (typeof execLike.stderr === 'string') {
parts.push(execLike.stderr)
}
if (typeof execLike.code === 'string') {
parts.push(execLike.code)
}
}
if (parts.length === 0) {
parts.push(String(error))
}
const text = parts.join('\n')
return TRANSIENT_PROBE_PATTERNS.some((pattern) => pattern.test(text))
}
/**
* Throws when the remote could not be read because the probe was wedged or its
* transport was gone, and returns normally for every answer — including a repo
* with no remote at all. Lets a caller that treats "nothing found" as a cacheable
* answer tell that apart from a lookup that never got to ask.
*/
export async function assertRemoteUrlReadable(
context: RemoteUrlProbeContext,
remoteName = 'origin'
): Promise<void> {
if (context.connectionId && !getSshGitProvider(context.connectionId)) {
throw new Error(SSH_GIT_PROVIDER_UNAVAILABLE_MESSAGE)
}
try {
await readRemoteUrl(context, remoteName)
} catch (error) {
if (isTransientGitProbeError(error)) {
throw error
}
}
}