Skip to content

Commit dd2a9d8

Browse files
chledowskiclaude
andcommitted
fix(server): a redemption outlives its caller and marks only scheduled continuations
Second review round on the reset-credit follow-up. - The redemption runs on a fiber the service owns. A caller that disconnects while the provider is still re-reading limits no longer skips the follow-up after the credit is spent. - Only outcome `reset` ends waits. `alreadyRedeemed` does not say whether this request refilled anything. - A recovery with no scheduled continuation is woken but not marked, and a recovery choice made after the reset goes by the reset time the thread still shows. The existing metadata reducer is unchanged again. - A failed mark is logged as a warning. Co-Authored-By: Claude Fable 5.2 <[email protected]>
1 parent 99b4119 commit dd2a9d8

6 files changed

Lines changed: 195 additions & 160 deletions

File tree

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

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4068,7 +4068,6 @@ const makeOrchestrator = Effect.fn("orchestrationV2.Orchestrator.layer")(functio
40684068
autoResume:
40694069
command.limitRecovery.autoResume ?? previousRecovery?.autoResume ?? false,
40704070
snooze: command.limitRecovery.snooze ?? previousRecovery?.snooze ?? false,
4071-
...(previousRecovery?.resetEarly ? { resetEarly: true } : {}),
40724071
requestId: command.commandId,
40734072
};
40744073
return {
@@ -4756,7 +4755,8 @@ const makeOrchestrator = Effect.fn("orchestrationV2.Orchestrator.layer")(functio
47564755
limitRecovery: {
47574756
...recovery,
47584757
snooze: false,
4759-
resetEarly: true,
4758+
// Without a scheduled continuation the reset only ends the snooze.
4759+
...(recovery.autoResume ? { resetEarly: true } : {}),
47604760
requestId: command.commandId,
47614761
},
47624762
// Ends only the snooze this recovery set; one the user chose stays.
@@ -14662,7 +14662,10 @@ const makeOrchestrator = Effect.fn("orchestrationV2.Orchestrator.layer")(functio
1466214662
}).pipe(
1466314663
// A thread that moved on since the read is refused and keeps its own schedule.
1466414664
Effect.catchCause((cause) =>
14665-
Effect.logDebug("orchestration-v2.limit-recovery.reset-skipped", { threadId, cause }),
14665+
Effect.logWarning("orchestration-v2.limit-recovery.reset-failed", {
14666+
threadId,
14667+
cause,
14668+
}),
1466614669
),
1466714670
),
1466814671
{ discard: true },

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

Lines changed: 41 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -8410,6 +8410,9 @@ it.layer(layerTest)("usage-limit recovery", (it) => {
84108410
"early-reset",
84118411
"early-reset-snooze-only",
84128412
"early-reset-manual-snooze",
8413+
"early-reset-then-snooze",
8414+
"early-reset-then-cancel",
8415+
"early-reset-then-stop",
84138416
] as const)("guards a scheduled usage-limit continuation against %s", (scenario) =>
84148417
Effect.gen(function* () {
84158418
const orchestrator = yield* Orchestrator.OrchestratorV2;
@@ -8637,13 +8640,12 @@ it.layer(layerTest)("usage-limit recovery", (it) => {
86378640
});
86388641
if (snooze)
86398642
assert.equal(DateTime.toEpochMillis(armedShell.snoozedUntil!), Date.parse(resetAt));
8640-
if (
8641-
scenario === "early-reset" ||
8642-
scenario === "early-reset-snooze-only" ||
8643-
scenario === "early-reset-manual-snooze"
8644-
) {
8643+
if (scenario.startsWith("early-reset")) {
86458644
const manualWakeAt = DateTime.add(now, { seconds: 30 });
8646-
if (scenario === "early-reset-manual-snooze")
8645+
// Until a snooze the user set ends, the early reset is recorded and nothing continues.
8646+
const manualSnooze =
8647+
scenario === "early-reset-manual-snooze" || scenario.startsWith("early-reset-then");
8648+
if (manualSnooze)
86478649
yield* orchestrator.dispatch({
86488650
type: "thread.snooze",
86498651
commandId: CommandId.make(`recovery:manual-snooze:${scenario}`),
@@ -8687,9 +8689,11 @@ it.layer(layerTest)("usage-limit recovery", (it) => {
86878689
resetBefore: DateTime.add(DateTime.makeUnsafe(resetAt), { seconds: 1 }),
86888690
});
86898691
const reset = (yield* orchestrator.getThreadProjection(threadId)).thread;
8690-
assert.deepInclude(reset.limitRecovery, { autoResume, snooze: false, resetEarly: true });
8692+
assert.deepInclude(reset.limitRecovery, { autoResume, snooze: false });
8693+
// Without a scheduled continuation there is nothing to bring forward.
8694+
assert.equal(reset.limitRecovery?.resetEarly, autoResume ? true : undefined);
86918695
// The reset ends the snooze the recovery set, never one the user chose.
8692-
if (scenario === "early-reset-manual-snooze")
8696+
if (manualSnooze)
86938697
assert.equal(
86948698
DateTime.toEpochMillis(reset.snoozedUntil!),
86958699
DateTime.toEpochMillis(manualWakeAt),
@@ -8707,17 +8711,39 @@ it.layer(layerTest)("usage-limit recovery", (it) => {
87078711
? null
87088712
: limitRecoveryCommand(candidate, false, DateTime.toEpochMillis(sweptAt));
87098713
});
8710-
if (scenario !== "early-reset") {
8711-
assert.isNull(yield* sweep);
8712-
if (scenario === "early-reset-manual-snooze") yield* TestClock.adjust("30 seconds");
8713-
else
8714+
const choose = (
8715+
choice: string,
8716+
limitRecovery: { autoResume?: boolean; snooze?: boolean },
8717+
) =>
8718+
orchestrator.dispatch({
8719+
type: "thread.metadata.update",
8720+
commandId: CommandId.make(`recovery:${choice}:${scenario}`),
8721+
threadId,
8722+
limitRecovery: { runId: run.id, resetAt, ...limitRecovery },
8723+
});
8724+
if (scenario !== "early-reset") assert.isNull(yield* sweep);
8725+
if (scenario !== "early-reset" && scenario !== "early-reset-manual-snooze") {
8726+
// A choice made after the reset goes by the reset time the thread still shows.
8727+
if (scenario === "early-reset-then-snooze") yield* choose("snooze", { snooze: true });
8728+
if (scenario === "early-reset-then-cancel")
8729+
yield* choose("cancel", { autoResume: false });
8730+
if (scenario === "early-reset-then-stop")
87148731
yield* orchestrator.dispatch({
8715-
type: "thread.metadata.update",
8716-
commandId: CommandId.make(`recovery:resume-after-reset:${scenario}`),
8732+
type: "thread.stop",
8733+
commandId: CommandId.make(`recovery:stop:${scenario}`),
87178734
threadId,
8718-
limitRecovery: { runId: run.id, resetAt, autoResume: true },
87198735
});
8736+
if (scenario !== "early-reset-then-snooze") yield* choose("resume", { autoResume: true });
8737+
const chosen = (yield* orchestrator.getThreadProjection(threadId)).thread;
8738+
assert.deepInclude(chosen.limitRecovery, { autoResume: true });
8739+
assert.isUndefined(chosen.limitRecovery?.resetEarly);
8740+
if (scenario === "early-reset-then-snooze")
8741+
assert.equal(DateTime.toEpochMillis(chosen.snoozedUntil!), Date.parse(resetAt));
8742+
yield* TestClock.adjust("30 seconds");
8743+
assert.isNull(yield* sweep);
8744+
return;
87208745
}
8746+
if (manualSnooze) yield* TestClock.adjust("30 seconds");
87218747
const resume = yield* sweep;
87228748
assert(resume?.type === "message.dispatch");
87238749
assert.isBelow(DateTime.toEpochMillis(yield* DateTime.now), Date.parse(resetAt));

0 commit comments

Comments
 (0)