Follow-up to #9507, carrying over the reset-credit redemption from #9421. Codex grants a reset credit when it has rate-limited an account unfairly (`"Thanks for using Codex! You've been granted one free rate limit reset."`). Redeeming one clears the current 5h/weekly windows. The Limits tab now shows how many are banked and when the next expires, with a confirmed **Use a reset credit** action. ## How it works - `ServerProviderUsageLimits.resetCredits` carries the count and soonest expiry; the Codex probe reads it from the same `account/rateLimits/read` it already makes. - `ProviderInstance.consumeResetCredit` is a new optional hook — account-level, so it sits beside `refreshModels` rather than on the thread-routed adapter. The Codex driver implements it over a short-lived app-server (via `withCodexAppServerClient`, factored out of the status and skills probes which duplicated the setup), then re-probes. - Single-flight per instance with one idempotency key kept until Codex reports an outcome, so a retry after a timeout does not open a second attempt. - New `provider.consumeResetCredit` RPC under the operate scope; the outcome (`reset` / `nothingToReset` / `noCredit` / `alreadyRedeemed`) is shown inline. Only Codex reports credits today. A provider without the hook gets a clear "does not bank reset credits" error; one without credits shows nothing. ## Screenshots The local Codex row with one banked credit (the same account via the CLIProxyAPI hub above it shows no credit, as expected — the hub does not relay them):  Close-up of the row:  Clicking it opens the confirmation; nothing is sent until **Use credit**:  ## Verification - Mapper tests for the credit summary; provider, contract, and Usage page suites pass; typecheck clean. - Verified against a real Codex Pro account holding one credit: summary and expiry render, the confirm dialog opens. **Not redeemed** — that would spend the credit. Written by Claude Fable 5 via Claude Code; design and single-flight approach from @StiensWout's #9421. <!-- CURSOR_SUMMARY --> --- > [!NOTE] > **Medium Risk** > Redemption spends real account credits over a new RPC; correctness depends on per-account locking and idempotency, though disabled instances and non-Codex providers are rejected explicitly. > > **Overview** > Adds **end-to-end redemption of banked Codex rate-limit reset credits** from the Limits UI on web and mobile, backed by a new operate-scoped `provider.consumeResetCredit` RPC. > > **Contracts and server:** `ServerProviderUsageLimits` can include `resetCredits` (count + next expiry). Codex probes attach that from `account/rateLimits/read`. Optional `ProviderInstance.consumeResetCredit` is implemented for Codex via a scoped app-server call to `account/rateLimitResetCredit/consume`, then a limits refresh. `CodexResetCreditCoordinator` serializes redemptions per Codex account directory, reuses one idempotency key until Codex returns an outcome, and times out hung requests. `withCodexAppServerClient` is extracted so status, skills, and redemption share the same short-lived app-server setup. > > **Clients:** Limits rows show banked credits and a confirmed **Use a reset credit** action that calls `serverEnvironment.consumeResetCredit` and surfaces outcomes (`reset`, `nothingToReset`, etc.) or errors. > > **Web usage sources (same PR):** Adding/removing CLIProxyAPI hubs and the add dialog target a **selected connected environment** (with picker when several are connected), gated by operate access—not only the primary environment. > > <sup>Reviewed by [Cursor Bugbot](https://cursor.com/bugbot) for commit 98f32e60852dca1d38c94335b3ed7aac8c4b895e. Bugbot is set up for automated code reviews on this repo. Configure [here](https://www.cursor.com/dashboard/bugbot).</sup> <!-- /CURSOR_SUMMARY --> Co-authored-by: Wout Stiens <[email protected]> Co-authored-by: Claude Code <[email protected]>
87 lines
3.5 KiB
TypeScript
87 lines
3.5 KiB
TypeScript
import { assert, describe, it } from "@effect/vitest";
|
|
import * as Deferred from "effect/Deferred";
|
|
import * as Effect from "effect/Effect";
|
|
import * as Fiber from "effect/Fiber";
|
|
import * as Ref from "effect/Ref";
|
|
|
|
import { CodexResetCreditCoordinator, layerTest } from "./codexResetCredit.ts";
|
|
|
|
describe("CodexResetCreditCoordinator", () => {
|
|
it.effect("re-sends the same idempotency key after a failed attempt, then clears it", () =>
|
|
Effect.gen(function* () {
|
|
const { redeem } = yield* CodexResetCreditCoordinator;
|
|
const keys = yield* Ref.make<ReadonlyArray<string>>([]);
|
|
const attempts = yield* Ref.make(0);
|
|
const consume = (key: string) =>
|
|
Effect.gen(function* () {
|
|
yield* Ref.update(keys, (seen) => [...seen, key]);
|
|
const attempt = yield* Ref.updateAndGet(attempts, (n) => n + 1);
|
|
if (attempt === 1) return yield* Effect.fail("timed out" as const);
|
|
return "reset" as const;
|
|
});
|
|
|
|
const first = yield* redeem("acct", consume).pipe(Effect.result);
|
|
assert.isTrue(first._tag === "Failure");
|
|
const second = yield* redeem("acct", consume);
|
|
assert.strictEqual(second, "reset");
|
|
// A fresh redemption after success must be a fresh attempt.
|
|
yield* redeem("acct", consume);
|
|
|
|
const seen = yield* Ref.get(keys);
|
|
assert.strictEqual(seen.length, 3);
|
|
assert.strictEqual(seen[0], seen[1]);
|
|
assert.notStrictEqual(seen[1], seen[2]);
|
|
}).pipe(Effect.provide(layerTest)),
|
|
);
|
|
|
|
it.effect("serialises concurrent redemptions on the same account, not per caller", () =>
|
|
Effect.gen(function* () {
|
|
const { redeem } = yield* CodexResetCreditCoordinator;
|
|
const release = yield* Deferred.make<void>();
|
|
const inFlight = yield* Ref.make(0);
|
|
const peak = yield* Ref.make(0);
|
|
const consume = () =>
|
|
Effect.gen(function* () {
|
|
const now = yield* Ref.updateAndGet(inFlight, (n) => n + 1);
|
|
yield* Ref.update(peak, (p) => Math.max(p, now));
|
|
yield* Deferred.await(release);
|
|
yield* Ref.update(inFlight, (n) => n - 1);
|
|
return "reset" as const;
|
|
});
|
|
|
|
// Two instances of the same account redeem at once.
|
|
const a = yield* redeem("acct", consume).pipe(Effect.forkChild);
|
|
const b = yield* redeem("acct", consume).pipe(Effect.forkChild);
|
|
yield* Effect.yieldNow;
|
|
yield* Deferred.succeed(release, undefined);
|
|
yield* Fiber.join(a);
|
|
yield* Fiber.join(b);
|
|
|
|
assert.strictEqual(yield* Ref.get(peak), 1);
|
|
}).pipe(Effect.provide(layerTest)),
|
|
);
|
|
|
|
it.effect("keeps different accounts independent", () =>
|
|
Effect.gen(function* () {
|
|
const { redeem } = yield* CodexResetCreditCoordinator;
|
|
const release = yield* Deferred.make<void>();
|
|
const peak = yield* Ref.make(0);
|
|
const inFlight = yield* Ref.make(0);
|
|
const consume = () =>
|
|
Effect.gen(function* () {
|
|
const now = yield* Ref.updateAndGet(inFlight, (n) => n + 1);
|
|
yield* Ref.update(peak, (p) => Math.max(p, now));
|
|
yield* Deferred.await(release);
|
|
return "reset" as const;
|
|
});
|
|
const a = yield* redeem("acct-a", consume).pipe(Effect.forkChild);
|
|
const b = yield* redeem("acct-b", consume).pipe(Effect.forkChild);
|
|
yield* Effect.yieldNow;
|
|
yield* Deferred.succeed(release, undefined);
|
|
yield* Fiber.join(a);
|
|
yield* Fiber.join(b);
|
|
assert.strictEqual(yield* Ref.get(peak), 2);
|
|
}).pipe(Effect.provide(layerTest)),
|
|
);
|
|
});
|