@devries-cameron20 / t3code-1 / commits / 63b0e3d82e
Fork 0

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

fix(server): do not freshly arm days-old usage-limit recoveries (#21735)

fixfon committed Oct 31, 2026 at 11:39 UTC 63b0e3d82ee1c1a3bc1d0bc4950d4dfe03b402c9
2 files changed +53 -2
apps/server/src/orchestration-v2/UsageLimitRecoveryWorker.test.ts
+50 -2
@@ -1,5 +1,5 @@
1 -import { assert, it } from "@effect/vitest";
2 -import type { OrchestrationV2DomainEvent } from "@t3tools/contracts";
1 +import { assert, describe, it } from "@effect/vitest";
2 +import { RunId, ThreadId, type OrchestrationV2DomainEvent } from "@t3tools/contracts";
3 import * as Clock from "effect/Clock";
4 import * as DateTime from "effect/DateTime";
5 import * as Effect from "effect/Effect";
@@ -14,6 +14,7 @@ import * as Orchestrator from "./Orchestrator.ts";
14 import * as ProjectionStore from "./ProjectionStore.ts";
15 import * as ThreadManagement from "./ThreadManagementService.ts";
16 import * as UsageLimitRecoveryWorker from "./UsageLimitRecoveryWorker.ts";
17 +import { limitRecoveryCommand } from "./UsageLimitRecoveryWorker.ts";
18
19 it.effect("idle sweeps skip SQL until the next wake time or a relevant event", () =>
20 Effect.gen(function* () {
@@ -68,3 +69,50 @@ it.effect("idle sweeps skip SQL until the next wake time or a relevant event", (
69 }).pipe(Effect.provide(layer));
70 }),
71 );
72 +
73 +const HOUR_MS = 60 * 60 * 1000;
74 +const nowMs = Date.parse("2026-10-19T12:00:00.000Z");
75 +
76 +const failedThread = (failedAgoMs: number, resetAgoMs: number) => {
77 + const failedAt = DateTime.makeUnsafe(nowMs - failedAgoMs);
78 + const candidate: ProjectionStore.ProjectionLimitRecoveryCandidate = {
79 + id: ThreadId.make("thread:limited"),
80 + status: "failed",
81 + lastErrorClass: "usage_limit",
82 + usageLimitResetAt: DateTime.formatIso(DateTime.makeUnsafe(nowMs - resetAgoMs)),
83 + latestRunId: RunId.make("run:limited"),
84 + latestRunCompletedAt: failedAt,
85 + updatedAt: failedAt,
86 + archivedAt: null,
87 + settledOverride: null,
88 + pendingRuntimeRequest: null,
89 + limitRecovery: null,
90 + snoozedUntil: null,
91 + };
92 + return candidate;
93 +};
94 +
95 +describe("limitRecoveryCommand arming", () => {
96 + it("does not freshly arm a failure whose reset passed days ago", () => {
97 + const thread = failedThread(5 * 24 * HOUR_MS, 4 * 24 * HOUR_MS);
98 + assert.isNull(limitRecoveryCommand(thread, true, nowMs));
99 + assert.isNull(limitRecoveryCommand(thread, true, nowMs, true));
100 + });
101 +
102 + it("still arms a failure whose reset only just passed", () => {
103 + const thread = failedThread(6 * HOUR_MS, HOUR_MS);
104 + const arm = limitRecoveryCommand(thread, true, nowMs);
105 + assert.equal(arm?.type, "thread.metadata.update");
106 + });
107 +
108 + it("arms a reset that is exactly 24 hours overdue", () => {
109 + const thread = failedThread(30 * HOUR_MS, 24 * HOUR_MS);
110 + const arm = limitRecoveryCommand(thread, true, nowMs);
111 + assert.equal(arm?.type, "thread.metadata.update");
112 + });
113 +
114 + it("does not arm a reset that is 24 hours and 1ms overdue", () => {
115 + const thread = failedThread(30 * HOUR_MS, 24 * HOUR_MS + 1);
116 + assert.isNull(limitRecoveryCommand(thread, true, nowMs));
117 + });
118 +});
apps/server/src/orchestration-v2/UsageLimitRecoveryWorker.ts
+3
@@ -12,6 +12,8 @@ import * as ProjectionStore from "./ProjectionStore.ts";
12 import * as Orchestrator from "./Orchestrator.ts";
13 import * as ThreadManagement from "./ThreadManagementService.ts";
14
15 +const MAX_OVERDUE_ARM_MS = 24 * 60 * 60 * 1000;
16 +
17 export function limitRecoveryCommand(
18 thread: ProjectionStore.ProjectionLimitRecoveryCandidate,
19 autoResume: boolean,
@@ -38,6 +40,7 @@ export function limitRecoveryCommand(
40 const identity = `${thread.id}:${thread.latestRunId}:${resetMs}`;
41 const recovery = thread.limitRecovery;
42 if (recovery?.runId !== thread.latestRunId || recovery.resetAt !== thread.usageLimitResetAt) {
43 + if (nowMs - resetMs > MAX_OVERDUE_ARM_MS) return null;
44 if (!autoResume && (!snooze || resetMs <= nowMs)) return null;
45 return {
46 type: "thread.metadata.update",