@devries-cameron20 / t3code Mirror · read-only / commits / 6419422f94
Fork 0

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

Mirrored from https://github.com/Camo67/t3code.git

Synced Nov 21, 21:00 UTC

fix(server): a reset credit's limits refresh when the first read after it fails (#34780)

Julius Marminge committed Nov 11, 2026 at 22:35 UTC 6419422f947cbb6796675ee7dd4df8fe369c98e7
4 files changed +92 -42
apps/server/src/provider/Drivers/ClaudeDriver.ts
+18 -19
@@ -349,25 +349,24 @@ export const ClaudeDriver: ProviderDriver<ClaudeSettings, ClaudeDriverEnv, Path.
349 }),
350 ),
351 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 + ),
370 ),
371 );
372
apps/server/src/provider/Drivers/CodexDriver.ts
+16 -22
@@ -347,29 +347,23 @@ export const CodexDriver: ProviderDriver<
347 cause,
348 }),
349 ),
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.
350 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 + ),
367 ),
368 );
369
apps/server/src/provider/resetCreditCoordinator.test.ts
+26
@@ -3,6 +3,7 @@ import * as Deferred from "effect/Deferred";
3 import * as Effect from "effect/Effect";
4 import * as Fiber from "effect/Fiber";
5 import * as Ref from "effect/Ref";
6 +import * as TestClock from "effect/testing/TestClock";
7
8 import * as ResetCreditCoordinator from "./resetCreditCoordinator.ts";
9
@@ -100,4 +101,29 @@ describe("ResetCreditCoordinator", () => {
101 assert.strictEqual(yield* Ref.get(peak), 2);
102 }).pipe(Effect.provide(ResetCreditCoordinator.layerTest)),
103 );
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 + );
129 });
apps/server/src/provider/resetCreditCoordinator.ts
+32 -1
@@ -1,11 +1,12 @@
1 /** 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";
3 import * as Context from "effect/Context";
4 import * as SecureRandom from "effect/crypto/SecureRandom";
5 import * as Effect from "effect/Effect";
6 import * as Layer from "effect/Layer";
7 import type * as PlatformError from "effect/PlatformError";
8 import * as Ref from "effect/Ref";
9 +import * as Schedule from "effect/Schedule";
10 import * as Semaphore from "effect/Semaphore";
11
12 interface AccountRedemptionState {
@@ -70,6 +71,36 @@ export const make = Effect.gen(function* () {
71 return { redeem } satisfies ResetCreditCoordinator["Service"];
72 });
73
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 +
104 export const layer = Layer.effect(ResetCreditCoordinator, make);
105
106 export const layerTest = Layer.effect(