|
| 1 | +import type { GatewayRecoveryRuntime } from "../../gateway/server-instance-runtime.types.js"; |
| 2 | +import type { AgentRunRequest } from "../../gateway/server-methods/agent-request-types.js"; |
| 3 | + |
| 4 | +const RESTART_RECOVERY_EXECUTION_START_TIMEOUT_MS = 10_000; |
| 5 | +const RESTART_RECOVERY_ABORT_TIMEOUT_MS = 2_000; |
| 6 | + |
| 7 | +type RestartRecoveryDispatchResult = { |
| 8 | + runId: string; |
| 9 | + status?: unknown; |
| 10 | +}; |
| 11 | + |
| 12 | +type RestartRecoveryDispatchObservation = { |
| 13 | + dispatchAccepted: boolean; |
| 14 | + executionStarted: boolean; |
| 15 | + preStartAbortAttempted: boolean; |
| 16 | + preStartAbortConfirmed: boolean; |
| 17 | +}; |
| 18 | + |
| 19 | +export type RestartRecoveryDispatchStartOutcome = |
| 20 | + | { |
| 21 | + kind: "started"; |
| 22 | + observation: RestartRecoveryDispatchObservation; |
| 23 | + } |
| 24 | + | { |
| 25 | + kind: "terminal"; |
| 26 | + observation: RestartRecoveryDispatchObservation; |
| 27 | + result: RestartRecoveryDispatchResult; |
| 28 | + } |
| 29 | + | { |
| 30 | + kind: "failed"; |
| 31 | + error: unknown; |
| 32 | + observation: RestartRecoveryDispatchObservation; |
| 33 | + }; |
| 34 | + |
| 35 | +export async function dispatchRestartRecoveryUntilStarted(params: { |
| 36 | + agentId: string; |
| 37 | + agentParams: AgentRunRequest; |
| 38 | + gatewayRuntime: GatewayRecoveryRuntime; |
| 39 | + recoveryRunId: string; |
| 40 | + sessionKey: string; |
| 41 | +}): Promise<RestartRecoveryDispatchStartOutcome> { |
| 42 | + let dispatchAccepted = false; |
| 43 | + let executionStarted = false; |
| 44 | + let executionStartTimedOut = false; |
| 45 | + let preStartAbortAttempted = false; |
| 46 | + let preStartAbortConfirmed = false; |
| 47 | + let preStartAbort: Promise<void> | undefined; |
| 48 | + const observe = (): RestartRecoveryDispatchObservation => ({ |
| 49 | + dispatchAccepted, |
| 50 | + executionStarted, |
| 51 | + preStartAbortAttempted, |
| 52 | + preStartAbortConfirmed, |
| 53 | + }); |
| 54 | + let resolveExecutionStarted!: () => void; |
| 55 | + const executionStartedPromise = new Promise<void>((resolve) => { |
| 56 | + resolveExecutionStarted = resolve; |
| 57 | + }); |
| 58 | + const executionStartAbort = new AbortController(); |
| 59 | + const abortBeforeStart = () => { |
| 60 | + if (!dispatchAccepted || executionStarted) { |
| 61 | + return Promise.resolve(); |
| 62 | + } |
| 63 | + return (preStartAbort ??= (async () => { |
| 64 | + preStartAbortAttempted = true; |
| 65 | + const aborted = await params.gatewayRuntime.abortAgent( |
| 66 | + { |
| 67 | + agentId: params.agentId, |
| 68 | + runId: params.recoveryRunId, |
| 69 | + sessionKey: params.sessionKey, |
| 70 | + }, |
| 71 | + RESTART_RECOVERY_ABORT_TIMEOUT_MS, |
| 72 | + ); |
| 73 | + preStartAbortConfirmed = aborted.aborted === true; |
| 74 | + })()); |
| 75 | + }; |
| 76 | + let resolveExecutionStartTimeout!: (outcome: RestartRecoveryDispatchStartOutcome) => void; |
| 77 | + const executionStartTimeoutPromise = new Promise<RestartRecoveryDispatchStartOutcome>( |
| 78 | + (resolve) => { |
| 79 | + resolveExecutionStartTimeout = resolve; |
| 80 | + }, |
| 81 | + ); |
| 82 | + let executionStartTimer: ReturnType<typeof setTimeout> | undefined; |
| 83 | + const clearExecutionStartTimer = () => { |
| 84 | + if (executionStartTimer) { |
| 85 | + clearTimeout(executionStartTimer); |
| 86 | + executionStartTimer = undefined; |
| 87 | + } |
| 88 | + }; |
| 89 | + executionStartTimer = setTimeout(() => { |
| 90 | + if (!executionStarted) { |
| 91 | + executionStartTimedOut = true; |
| 92 | + const error = new Error("restart recovery execution start timeout"); |
| 93 | + void abortBeforeStart() |
| 94 | + .catch(() => undefined) |
| 95 | + .then(() => { |
| 96 | + executionStartAbort.abort(error); |
| 97 | + resolveExecutionStartTimeout({ kind: "failed", error, observation: observe() }); |
| 98 | + }); |
| 99 | + } |
| 100 | + }, RESTART_RECOVERY_EXECUTION_START_TIMEOUT_MS); |
| 101 | + executionStartTimer.unref?.(); |
| 102 | + let dispatchPromise: Promise<RestartRecoveryDispatchResult>; |
| 103 | + try { |
| 104 | + dispatchPromise = params.gatewayRuntime.dispatchAgent<RestartRecoveryDispatchResult>( |
| 105 | + params.agentParams, |
| 106 | + undefined, |
| 107 | + { |
| 108 | + expectFinal: true, |
| 109 | + onAccepted: () => { |
| 110 | + dispatchAccepted = true; |
| 111 | + if (executionStartTimedOut) { |
| 112 | + void abortBeforeStart().catch(() => undefined); |
| 113 | + } |
| 114 | + }, |
| 115 | + onExecutionStarted: () => { |
| 116 | + if (executionStartTimedOut) { |
| 117 | + return; |
| 118 | + } |
| 119 | + executionStarted = true; |
| 120 | + clearExecutionStartTimer(); |
| 121 | + resolveExecutionStarted(); |
| 122 | + }, |
| 123 | + onSignalAbort: abortBeforeStart, |
| 124 | + signal: executionStartAbort.signal, |
| 125 | + }, |
| 126 | + ); |
| 127 | + } catch (error) { |
| 128 | + clearExecutionStartTimer(); |
| 129 | + return { kind: "failed", error, observation: observe() }; |
| 130 | + } |
| 131 | + const terminalDispatchOutcome = dispatchPromise.then< |
| 132 | + RestartRecoveryDispatchStartOutcome, |
| 133 | + RestartRecoveryDispatchStartOutcome |
| 134 | + >( |
| 135 | + (result) => { |
| 136 | + if (result.status === "in_flight") { |
| 137 | + // Cached acceptance is still queued work. Keep its exact claim under the |
| 138 | + // same start deadline as a newly accepted run. |
| 139 | + dispatchAccepted = true; |
| 140 | + return executionStartTimeoutPromise; |
| 141 | + } |
| 142 | + clearExecutionStartTimer(); |
| 143 | + return { kind: "terminal", observation: observe(), result }; |
| 144 | + }, |
| 145 | + (error: unknown) => { |
| 146 | + clearExecutionStartTimer(); |
| 147 | + return { kind: "failed", error, observation: observe() }; |
| 148 | + }, |
| 149 | + ); |
| 150 | + return await Promise.race([ |
| 151 | + terminalDispatchOutcome, |
| 152 | + executionStartTimeoutPromise, |
| 153 | + executionStartedPromise.then( |
| 154 | + (): RestartRecoveryDispatchStartOutcome => ({ |
| 155 | + kind: "started", |
| 156 | + observation: observe(), |
| 157 | + }), |
| 158 | + ), |
| 159 | + ]); |
| 160 | +} |
0 commit comments