diff --git a/src/main/runtime/pty-process-liveness-broker-wait.ts b/src/main/runtime/pty-process-liveness-broker-wait.ts new file mode 100644 index 00000000000..ecb66209071 --- /dev/null +++ b/src/main/runtime/pty-process-liveness-broker-wait.ts @@ -0,0 +1,31 @@ +import type { PtyProcessEvidenceEntry } from './pty-process-liveness-broker-types' +import type { PtyProcessLivenessEvidence } from './pty-process-inspection' + +export function waitForPtyProcessProbe( + entries: ReadonlyMap, + ptyId: string, + entry: PtyProcessEvidenceEntry, + timeoutMs: number | null +): Promise { + const probe = entry.probe + if (!probe) { + return Promise.resolve( + entry.evidence ?? { status: 'unverifiable', reason: 'process inspection unavailable' } + ) + } + if (timeoutMs === null) { + return probe + } + return new Promise((resolve) => { + const timeout = setTimeout(() => { + if (entries.get(ptyId) === entry && entry.probe === probe) { + entry.timedOut = true + } + resolve({ status: 'unverifiable', reason: 'process inspection timed out' }) + }, timeoutMs) + void probe.then((evidence) => { + clearTimeout(timeout) + resolve(evidence) + }) + }) +} diff --git a/src/main/runtime/pty-process-liveness-broker.ts b/src/main/runtime/pty-process-liveness-broker.ts index 45aff70792e..2a788e46e2b 100644 --- a/src/main/runtime/pty-process-liveness-broker.ts +++ b/src/main/runtime/pty-process-liveness-broker.ts @@ -16,6 +16,7 @@ import type { PtyProcessEvidenceEntry, PtyProcessLivenessBrokerOptions } from './pty-process-liveness-broker-types' +import { waitForPtyProcessProbe } from './pty-process-liveness-broker-wait' export type { PtyProcessLivenessBrokerOptions } from './pty-process-liveness-broker-types' export type { PtyProcessInspectionSource, @@ -263,27 +264,7 @@ export class PtyProcessLivenessBroker { entry: PtyProcessEvidenceEntry, timeoutMs: number | null ): Promise { - const probe = entry.probe - if (!probe) { - return Promise.resolve( - entry.evidence ?? { status: 'unverifiable', reason: 'process inspection unavailable' } - ) - } - if (timeoutMs === null) { - return probe - } - return new Promise((resolve) => { - const timeout = setTimeout(() => { - if (this.entries.get(ptyId) === entry && entry.probe === probe) { - entry.timedOut = true - } - resolve({ status: 'unverifiable', reason: 'process inspection timed out' }) - }, timeoutMs) - void probe.then((evidence) => { - clearTimeout(timeout) - resolve(evidence) - }) - }) + return waitForPtyProcessProbe(this.entries, ptyId, entry, timeoutMs) } private unavailableBackoffMs(failureCount: number): number { const base = this.options.unavailableBackoffBaseMs ?? DEFAULT_UNAVAILABLE_BACKOFF_BASE_MS