Skip to content
Open
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
41 changes: 41 additions & 0 deletions apps/server/src/orchestration-v2/UsageLimitRecoveryWorker.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
import { assert, describe, it } from "@effect/vitest";
import { RunId, ThreadId } from "@t3tools/contracts";
import * as DateTime from "effect/DateTime";
import type * as ProjectionStore from "./ProjectionStore.ts";
import { limitRecoveryCommand } from "./UsageLimitRecoveryWorker.ts";

const HOUR_MS = 60 * 60 * 1000;
const nowMs = Date.parse("2026-10-19T12:00:00.000Z");

const failedThread = (failedAgoMs: number, resetAgoMs: number) => {
const failedAt = DateTime.makeUnsafe(nowMs - failedAgoMs);
const candidate: ProjectionStore.ProjectionLimitRecoveryCandidate = {
id: ThreadId.make("thread:limited"),
status: "failed",
lastErrorClass: "usage_limit",
usageLimitResetAt: DateTime.formatIso(DateTime.makeUnsafe(nowMs - resetAgoMs)),
latestRunId: RunId.make("run:limited"),
latestRunCompletedAt: failedAt,
updatedAt: failedAt,
archivedAt: null,
settledOverride: null,
pendingRuntimeRequest: null,
limitRecovery: null,
snoozedUntil: null,
};
return candidate;
};

describe("limitRecoveryCommand arming", () => {
it("does not freshly arm a failure whose reset passed days ago", () => {
const thread = failedThread(5 * 24 * HOUR_MS, 4 * 24 * HOUR_MS);
assert.isNull(limitRecoveryCommand(thread, true, nowMs));
assert.isNull(limitRecoveryCommand(thread, true, nowMs, true));
});

it("still arms a failure whose reset only just passed", () => {
const thread = failedThread(6 * HOUR_MS, HOUR_MS);
const arm = limitRecoveryCommand(thread, true, nowMs);
assert.equal(arm?.type, "thread.metadata.update");
});
});
3 changes: 3 additions & 0 deletions apps/server/src/orchestration-v2/UsageLimitRecoveryWorker.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@ import * as ServerSettings from "../serverSettings.ts";
import * as ProjectionStore from "./ProjectionStore.ts";
import * as ThreadManagement from "./ThreadManagementService.ts";

const MAX_OVERDUE_ARM_MS = 24 * 60 * 60 * 1000;

export function limitRecoveryCommand(
thread: ProjectionStore.ProjectionLimitRecoveryCandidate,
autoResume: boolean,
Expand All @@ -33,6 +35,7 @@ export function limitRecoveryCommand(
const identity = `${thread.id}:${thread.latestRunId}:${resetMs}`;
const recovery = thread.limitRecovery;
if (recovery?.runId !== thread.latestRunId || recovery.resetAt !== thread.usageLimitResetAt) {
if (nowMs - resetMs > MAX_OVERDUE_ARM_MS) return null;
if (!autoResume && (!snooze || resetMs <= nowMs)) return null;
return {
type: "thread.metadata.update",
Expand Down
Loading