Skip to content

Commit 7078c91

Browse files
fix(server): never hold new work behind a usage limit with no reset time
A usage-limit failure without a reset time cannot arm auto-resume, so a held arrival had nothing to drain it. Hold only until a known reset, or while its continuation is pending. AI-assisted; verified with the tests and tools listed above. Co-Authored-By: Claude Opus 5.5 <[email protected]>
1 parent b0019e2 commit 7078c91

2 files changed

Lines changed: 20 additions & 10 deletions

File tree

‎apps/server/src/orchestration-v2/Orchestrator.ts‎

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -6011,12 +6011,11 @@ const makeOrchestrator = Effect.fn("orchestrationV2.Orchestrator.layer")(functio
60116011
projection.thread.settledOverride !== "settled" &&
60126012
projection.thread.providerInstanceId === limitedRun.providerInstanceId &&
60136013
!projection.runtimeRequests.some((request) => request.status === "pending");
6014-
// After a known reset, only a valid pending continuation takes priority
6015-
// over new work. Unknown reset times keep the existing queue protection.
6014+
// Hold only until a known reset, or while its continuation is pending. With no
6015+
// reset time nothing can drain the hold, so new work starts as before.
60166016
if (
6017-
Number.isFinite(resetMs) &&
6018-
resetMs <= DateTime.toEpochMillis(yield* DateTime.now) &&
6019-
!autoResumePending
6017+
!Number.isFinite(resetMs) ||
6018+
(resetMs <= DateTime.toEpochMillis(yield* DateTime.now) && !autoResumePending)
60206019
) {
60216020
limitedRun = null;
60226021
}

‎apps/server/src/orchestration-v2/runtimeLayer.test.ts‎

Lines changed: 16 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -9038,7 +9038,14 @@ it.layer(layerTest)("usage-limit recovery", (it) => {
90389038
}),
90399039
);
90409040

9041-
it.effect.each(["snoozed", "snoozed, other provider", "settled", "settled after reset"] as const)(
9041+
const heldWakeScenarios = [
9042+
"snoozed",
9043+
"snoozed, other provider",
9044+
"settled",
9045+
"settled after reset",
9046+
"settled, unknown reset",
9047+
] as const;
9048+
it.effect.each(heldWakeScenarios)(
90429049
"handles background wakes on a %s usage-limited thread",
90439050
(scenario) =>
90449051
Effect.gen(function* () {
@@ -9117,7 +9124,7 @@ it.layer(layerTest)("usage-limit recovery", (it) => {
91179124
message: "Plan limit reached.",
91189125
code: "usageLimitExceeded",
91199126
retryable: null,
9120-
resetAt,
9127+
resetAt: scenario === "settled, unknown reset" ? null : resetAt,
91219128
},
91229129
},
91239130
},
@@ -9127,7 +9134,7 @@ it.layer(layerTest)("usage-limit recovery", (it) => {
91279134
snapshot.threads.find((thread) => thread.id === threadId),
91289135
);
91299136
const snoozed = scenario === "snoozed" || scenario === "snoozed, other provider";
9130-
if (scenario !== "settled after reset")
9137+
if (scenario !== "settled after reset" && scenario !== "settled, unknown reset")
91319138
yield* orchestrator.dispatch(
91329139
limitRecoveryCommand((yield* shellOf)!, true, DateTime.toEpochMillis(now), true)!,
91339140
);
@@ -9164,8 +9171,12 @@ it.layer(layerTest)("usage-limit recovery", (it) => {
91649171
creationSource: "provider",
91659172
});
91669173
const held = yield* orchestrator.getThreadProjection(threadId);
9167-
// Another provider's work is not held behind this provider's limit.
9168-
if (scenario === "settled after reset" || scenario === "snoozed, other provider") {
9174+
// Nothing holds another provider's work, or work after a passed or unknown reset.
9175+
if (
9176+
scenario === "settled after reset" ||
9177+
scenario === "settled, unknown reset" ||
9178+
scenario === "snoozed, other provider"
9179+
) {
91699180
assert.deepEqual(
91709181
held.runs.map((run) => run.status),
91719182
["failed", "starting"],

0 commit comments

Comments
 (0)