diff --git a/src/main/daemon/daemon-session-owner-resolution.ts b/src/main/daemon/daemon-session-owner-resolution.ts index 889c08686c1..d35d2795d56 100644 --- a/src/main/daemon/daemon-session-owner-resolution.ts +++ b/src/main/daemon/daemon-session-owner-resolution.ts @@ -11,10 +11,7 @@ export type DaemonSessionOwnerResolution = | { kind: 'absent' } | { kind: 'unknown' } -type ProviderInventory = { - provider: T - processes: PtyProcessInfo[] | null -} +type ProviderInventory = { provider: T; processes: PtyProcessInfo[] | null } type OwnerInventory = { candidatesBySessionId: Map @@ -26,6 +23,12 @@ const OWNER_RESOLUTION_TIMEOUT_MS = 2_000 const OWNER_INVENTORY_CACHE_MS = 1_000 const FAILED_PROVIDER_COOLDOWN_MS = 1_000 +function assertClientConnected(signal: PtySpawnOptions['signal']): void { + if (signal?.aborted) { + throw new Error('client_disconnected') + } +} + export class DaemonSessionOwnerResolver { private inventoryInFlight: Promise> | null = null private cachedInventory: { value: OwnerInventory; expiresAt: number } | null = null @@ -52,9 +55,7 @@ export class DaemonSessionOwnerResolver { } async spawnAttachOnly(opts: PtySpawnOptions & { sessionId: string }): Promise { - if (opts.signal?.aborted) { - throw new Error('client_disconnected') - } + assertClientConnected(opts.signal) const routed = this.providers.find((provider) => provider === this.routes.get(opts.sessionId)) const direct = routed ?? (this.providers.length === 1 ? this.providers[0] : undefined) const routedIncarnation = this.routeIncarnations.get(opts.sessionId) @@ -87,17 +88,13 @@ export class DaemonSessionOwnerResolver { } } - if (opts.signal?.aborted) { - throw new Error('client_disconnected') - } + assertClientConnected(opts.signal) const resolution = await this.resolve( opts.sessionId, opts.expectedIncarnationId, opts.expectedIncarnationIsAuthoritative ) - if (opts.signal?.aborted) { - throw new Error('client_disconnected') - } + assertClientConnected(opts.signal) if (resolution.kind === 'unknown') { throw new TerminalSessionOwnerUnverifiedError(opts.sessionId) } @@ -180,7 +177,6 @@ export class DaemonSessionOwnerResolver { async discoverRoutes(): Promise { await this.inventory() - // Startup inventory is advisory; restore must retry a daemon that becomes ready immediately after it. this.failedProviderCooldowns.clear() this.cachedInventory = null } @@ -245,7 +241,6 @@ export class DaemonSessionOwnerResolver { return Promise.resolve(this.cachedInventory.value) } this.cachedInventory = null - // One shared deadline bounds all owners without serial retries or per-pane sleep ladders. const deadlineMs = Date.now() + OWNER_RESOLUTION_TIMEOUT_MS const epoch = this.epoch const inventory = Promise.all(