TypeScript
fix(server): limit restart loops within a five-minute window (#20306)
ObliviousObserver892cdaf4df9a
diff --git a/apps/server/src/orchestration-v2/RestartContinuation.test.ts b/apps/server/src/orchestration-v2/RestartContinuation.test.tsindex 60f55d8689e..80d9aadeced 100644--- a/apps/server/src/orchestration-v2/RestartContinuation.test.ts+++ b/apps/server/src/orchestration-v2/RestartContinuation.test.ts@@ -56,6 +56,8 @@ function makeProjection() {providerThreadId,activeAttemptId: attemptId,status: "running",+ requestedAt: now,+ completedAt: null,},],providerThreads: [@@ -945,6 +947,7 @@ const cutMidTurn = (extra: Record<string, unknown> = {}) => {{...base.runs[0]!,status: "cancelled",+ completedAt: now,userMessageId: MessageId.make("message:user"),...extra,},@@ -1142,7 +1145,7 @@ it.effect("prepares later threads' continuations when one thread fails", () =>}),);-it.effect("stops continuing after three automatic restart continuations in a row", () =>+it.effect("stops rapid restart loops but allows repeated restarts after normal work", () =>Effect.gen(function* () {const first = RunId.make("run:first-continuation");const second = RunId.make("run:second-continuation");@@ -1163,7 +1166,24 @@ it.effect("stops continuing after three automatic restart continuations in a rowrun.id === first ? { ...run, restartContinuationOfRunId: RunId.make("run:root") } : run,),} as unknown as OrchestrationV2ThreadProjection;- assert.lengthOf(yield* continuationTexts(withThreeLinks), 0);+ for (const { completedAt, oldestRequestAge, expected } of [+ { completedAt: null, oldestRequestAge: { minutes: 6 }, expected: 0 },+ { completedAt: null, oldestRequestAge: { minutes: 6, milliseconds: 1 }, expected: 1 },+ { completedAt: now, oldestRequestAge: { minutes: 6 }, expected: 1 },+ ]) {+ const projection = {+ ...withThreeLinks,+ runs: withThreeLinks.runs.map((run, index) => ({+ ...run,+ completedAt,+ requestedAt: DateTime.subtract(+ now,+ run.id === first ? oldestRequestAge : { minutes: index + 1 },+ ),+ })),+ };+ assert.lengthOf(yield* continuationTexts(projection), expected);+ }}),);diff --git a/apps/server/src/orchestration-v2/RestartContinuation.ts b/apps/server/src/orchestration-v2/RestartContinuation.tsindex 3339133c9a9..1343fed42c5 100644--- a/apps/server/src/orchestration-v2/RestartContinuation.ts+++ b/apps/server/src/orchestration-v2/RestartContinuation.ts@@ -31,6 +31,7 @@ import {const CONTINUE_PROMPT = "Continue where you left off.";const MAX_CONTINUATION_CHAIN = 3;+const CONTINUATION_LOOP_WINDOW_MS = 5 * 60 * 1_000;function restartContinuationBlocked(projection: Pick<@@ -228,11 +229,15 @@ export const continueRestartedRun = Effect.fn("RestartContinuation.continueRestaif (projection.thread.providerInstanceId !== source.providerInstanceId) return;// A turn that keeps crashing the server would otherwise restart it forever.let chainLength = 0;+ const loopWindowStart =+ DateTime.toEpochMillis(source.completedAt ?? source.requestedAt) -+ CONTINUATION_LOOP_WINDOW_MS;for (let run: OrchestrationV2Run | undefined = source;run?.restartContinuationOfRunId !== undefined && chainLength < MAX_CONTINUATION_CHAIN;run = projection.runs.find((candidate) => candidate.id === run?.restartContinuationOfRunId)) {+ if (DateTime.toEpochMillis(run.requestedAt) < loopWindowStart) break;chainLength += 1;}if (chainLength >= MAX_CONTINUATION_CHAIN) {