Skip to content

Commit f3f686f

Browse files
fix(server): a reset credit's limits refresh when the first read after it fails
After a confirmed redemption the Codex and Claude drivers re-read usage once. A failed read republishes the pre-redemption windows, so the Usage tile kept the exhausted limits and the redemption reported a failure until the periodic provider check. Retry the re-probe a few times with a short backoff before giving up. Fixes #33770 AI-assisted; verified with the tests and tools listed above.
1 parent ddc4f57 commit f3f686f

4 files changed

Lines changed: 92 additions & 42 deletions

File tree

‎apps/server/src/provider/Drivers/ClaudeDriver.ts‎

Lines changed: 18 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -349,25 +349,24 @@ export const ClaudeDriver: ProviderDriver<ClaudeSettings, ClaudeDriverEnv, Path.
349349
}),
350350
),
351351
Effect.tap((outcome) =>
352-
Effect.gen(function* () {
353-
const before = (yield* snapshot.getSnapshot).usageLimits?.checkedAt;
354-
yield* Cache.invalidateAll(capabilitiesProbeCache);
355-
const refreshed = yield* snapshot.refresh;
356-
const after = refreshed.usageLimits?.checkedAt;
357-
if (
358-
outcome === "reset" &&
359-
(after === undefined ||
360-
after === before ||
361-
refreshed.usageLimits?.unavailable?.reason === "probeFailed")
362-
) {
363-
return yield* new ProviderDriverError({
364-
driver: DRIVER_KIND,
365-
instanceId,
366-
detail:
367-
"The reset was applied, but Claude could not confirm the new limits. Refresh to check.",
368-
});
369-
}
370-
}),
352+
ResetCreditCoordinator.refreshAfterRedemption({
353+
outcome,
354+
getSnapshot: snapshot.getSnapshot,
355+
refresh: Cache.invalidateAll(capabilitiesProbeCache).pipe(
356+
Effect.andThen(snapshot.refresh),
357+
),
358+
}).pipe(
359+
Effect.filterOrFail(
360+
(confirmed) => confirmed,
361+
() =>
362+
new ProviderDriverError({
363+
driver: DRIVER_KIND,
364+
instanceId,
365+
detail:
366+
"The reset was applied, but Claude could not confirm the new limits. Refresh to check.",
367+
}),
368+
),
369+
),
371370
),
372371
);
373372

‎apps/server/src/provider/Drivers/CodexDriver.ts‎

Lines changed: 16 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -347,29 +347,23 @@ export const CodexDriver: ProviderDriver<
347347
cause,
348348
}),
349349
),
350-
// The windows just changed; re-probe so the snapshot says so. A
351-
// failed probe republishes the pre-redemption limits rather than
352-
// marking them failed, so "confirmed" means `checkedAt` moved past
353-
// what was published before the redemption started.
354350
Effect.tap((outcome) =>
355-
Effect.gen(function* () {
356-
const before = (yield* snapshot.getSnapshot).usageLimits?.checkedAt;
357-
const refreshed = yield* snapshot.refresh;
358-
const after = refreshed.usageLimits?.checkedAt;
359-
if (
360-
outcome === "reset" &&
361-
(after === undefined ||
362-
after === before ||
363-
refreshed.usageLimits?.unavailable?.reason === "probeFailed")
364-
) {
365-
return yield* new ProviderDriverError({
366-
driver: DRIVER_KIND,
367-
instanceId,
368-
detail:
369-
"The reset was applied, but Codex could not confirm the new limits. Refresh to check.",
370-
});
371-
}
372-
}),
351+
ResetCreditCoordinator.refreshAfterRedemption({
352+
outcome,
353+
getSnapshot: snapshot.getSnapshot,
354+
refresh: snapshot.refresh,
355+
}).pipe(
356+
Effect.filterOrFail(
357+
(confirmed) => confirmed,
358+
() =>
359+
new ProviderDriverError({
360+
driver: DRIVER_KIND,
361+
instanceId,
362+
detail:
363+
"The reset was applied, but Codex could not confirm the new limits. Refresh to check.",
364+
}),
365+
),
366+
),
373367
),
374368
);
375369

‎apps/server/src/provider/resetCreditCoordinator.test.ts‎

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ import * as Deferred from "effect/Deferred";
33
import * as Effect from "effect/Effect";
44
import * as Fiber from "effect/Fiber";
55
import * as Ref from "effect/Ref";
6+
import * as TestClock from "effect/testing/TestClock";
67

78
import * as ResetCreditCoordinator from "./resetCreditCoordinator.ts";
89

@@ -100,4 +101,29 @@ describe("ResetCreditCoordinator", () => {
100101
assert.strictEqual(yield* Ref.get(peak), 2);
101102
}).pipe(Effect.provide(ResetCreditCoordinator.layerTest)),
102103
);
104+
105+
it.effect("re-probes after a reset until the limits are fresh, then gives up", () =>
106+
Effect.gen(function* () {
107+
const stale = { usageLimits: { checkedAt: "t0", windows: [] } };
108+
const run = (freshFrom: number) =>
109+
Effect.gen(function* () {
110+
const probes = yield* Ref.make(0);
111+
const refresh = Ref.updateAndGet(probes, (n) => n + 1).pipe(
112+
Effect.map((n) =>
113+
n >= freshFrom ? { usageLimits: { checkedAt: `t${n}`, windows: [] } } : stale,
114+
),
115+
);
116+
const fiber = yield* ResetCreditCoordinator.refreshAfterRedemption({
117+
outcome: "reset",
118+
getSnapshot: Effect.succeed(stale),
119+
refresh,
120+
}).pipe(Effect.forkChild);
121+
yield* TestClock.adjust("1 minute");
122+
return { confirmed: yield* Fiber.join(fiber), probes: yield* Ref.get(probes) };
123+
});
124+
125+
assert.deepStrictEqual(yield* run(3), { confirmed: true, probes: 3 });
126+
assert.deepStrictEqual(yield* run(Infinity), { confirmed: false, probes: 4 });
127+
}),
128+
);
103129
});

‎apps/server/src/provider/resetCreditCoordinator.ts‎

Lines changed: 32 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,12 @@
11
/** Instances sharing a login directory share credits; serialize redemptions on that directory. */
2-
import type { ProviderConsumeResetCreditOutcome } from "@t3tools/contracts";
2+
import type { ProviderConsumeResetCreditOutcome, ServerProvider } from "@t3tools/contracts";
33
import * as Context from "effect/Context";
44
import * as SecureRandom from "effect/crypto/SecureRandom";
55
import * as Effect from "effect/Effect";
66
import * as Layer from "effect/Layer";
77
import type * as PlatformError from "effect/PlatformError";
88
import * as Ref from "effect/Ref";
9+
import * as Schedule from "effect/Schedule";
910
import * as Semaphore from "effect/Semaphore";
1011

1112
interface AccountRedemptionState {
@@ -70,6 +71,36 @@ export const make = Effect.gen(function* () {
7071
return { redeem } satisfies ResetCreditCoordinator["Service"];
7172
});
7273

74+
type UsageSnapshot = Pick<ServerProvider, "usageLimits">;
75+
76+
/**
77+
* Re-probe after a redemption; true once a `reset` shows fresh limits. A failed
78+
* usage read republishes the pre-redemption windows, which are known stale after
79+
* a reset, so retry briefly instead of leaving them until the periodic check.
80+
*/
81+
export const refreshAfterRedemption = Effect.fn("resetCreditCoordinator.refreshAfterRedemption")(
82+
function* <R>(input: {
83+
readonly outcome: ProviderConsumeResetCreditOutcome;
84+
readonly getSnapshot: Effect.Effect<UsageSnapshot>;
85+
readonly refresh: Effect.Effect<UsageSnapshot, never, R>;
86+
}) {
87+
const before = (yield* input.getSnapshot).usageLimits?.checkedAt;
88+
const confirmed = ({ usageLimits }: UsageSnapshot) =>
89+
input.outcome !== "reset" ||
90+
(usageLimits !== undefined &&
91+
usageLimits.checkedAt !== before &&
92+
usageLimits.unavailable?.reason !== "probeFailed");
93+
return yield* input.refresh.pipe(
94+
Effect.map(confirmed),
95+
Effect.repeat({
96+
until: (ok) => ok,
97+
times: 3,
98+
schedule: Schedule.exponential("1 second"),
99+
}),
100+
);
101+
},
102+
);
103+
73104
export const layer = Layer.effect(ResetCreditCoordinator, make);
74105

75106
export const layerTest = Layer.effect(

0 commit comments

Comments
 (0)