@devries-cameron20 / t3code Mirror · read-only / commits / 892cdaf4df
Fork 0

The best way to control Claude Code, Codex and more from any computer or phone

Mirrored from https://github.com/Camo67/t3code.git

Synced Nov 22, 23:15 UTC

fix(server): limit restart loops within a five-minute window (#20306)

Co-authored-by: Julius Marminge <julius0216@outlook.com>

ObliviousObserver committed Oct 20, 2026 at 00:15 UTC 892cdaf4df9a794c23e55bb0148a70eba0188f12
2 files changed +27 -2
apps/server/src/orchestration-v2/RestartContinuation.test.ts
+22 -2
@@ -56,6 +56,8 @@ function makeProjection() {
56 providerThreadId,
57 activeAttemptId: attemptId,
58 status: "running",
59 + requestedAt: now,
60 + completedAt: null,
61 },
62 ],
63 providerThreads: [
@@ -945,6 +947,7 @@ const cutMidTurn = (extra: Record<string, unknown> = {}) => {
947 {
948 ...base.runs[0]!,
949 status: "cancelled",
950 + completedAt: now,
951 userMessageId: MessageId.make("message:user"),
952 ...extra,
953 },
@@ -1142,7 +1145,7 @@ it.effect("prepares later threads' continuations when one thread fails", () =>
1145 }),
1146 );
1147
1145 -it.effect("stops continuing after three automatic restart continuations in a row", () =>
1148 +it.effect("stops rapid restart loops but allows repeated restarts after normal work", () =>
1149 Effect.gen(function* () {
1150 const first = RunId.make("run:first-continuation");
1151 const second = RunId.make("run:second-continuation");
@@ -1163,7 +1166,24 @@ it.effect("stops continuing after three automatic restart continuations in a row
1166 run.id === first ? { ...run, restartContinuationOfRunId: RunId.make("run:root") } : run,
1167 ),
1168 } as unknown as OrchestrationV2ThreadProjection;
1166 - assert.lengthOf(yield* continuationTexts(withThreeLinks), 0);
1169 + for (const { completedAt, oldestRequestAge, expected } of [
1170 + { completedAt: null, oldestRequestAge: { minutes: 6 }, expected: 0 },
1171 + { completedAt: null, oldestRequestAge: { minutes: 6, milliseconds: 1 }, expected: 1 },
1172 + { completedAt: now, oldestRequestAge: { minutes: 6 }, expected: 1 },
1173 + ]) {
1174 + const projection = {
1175 + ...withThreeLinks,
1176 + runs: withThreeLinks.runs.map((run, index) => ({
1177 + ...run,
1178 + completedAt,
1179 + requestedAt: DateTime.subtract(
1180 + now,
1181 + run.id === first ? oldestRequestAge : { minutes: index + 1 },
1182 + ),
1183 + })),
1184 + };
1185 + assert.lengthOf(yield* continuationTexts(projection), expected);
1186 + }
1187 }),
1188 );
1189
apps/server/src/orchestration-v2/RestartContinuation.ts
+5
@@ -31,6 +31,7 @@ import {
31 const CONTINUE_PROMPT = "Continue where you left off.";
32
33 const MAX_CONTINUATION_CHAIN = 3;
34 +const CONTINUATION_LOOP_WINDOW_MS = 5 * 60 * 1_000;
35
36 function restartContinuationBlocked(
37 projection: Pick<
@@ -228,11 +229,15 @@ export const continueRestartedRun = Effect.fn("RestartContinuation.continueResta
229 if (projection.thread.providerInstanceId !== source.providerInstanceId) return;
230 // A turn that keeps crashing the server would otherwise restart it forever.
231 let chainLength = 0;
232 + const loopWindowStart =
233 + DateTime.toEpochMillis(source.completedAt ?? source.requestedAt) -
234 + CONTINUATION_LOOP_WINDOW_MS;
235 for (
236 let run: OrchestrationV2Run | undefined = source;
237 run?.restartContinuationOfRunId !== undefined && chainLength < MAX_CONTINUATION_CHAIN;
238 run = projection.runs.find((candidate) => candidate.id === run?.restartContinuationOfRunId)
239 ) {
240 + if (DateTime.toEpochMillis(run.requestedAt) < loopWindowStart) break;
241 chainLength += 1;
242 }
243 if (chainLength >= MAX_CONTINUATION_CHAIN) {