fix(shared): redeem reset credits through the hub when it holds the account (#10462)

Co-authored-by: Claude Opus 5 (1M context) <[email protected]>
This commit is contained in:
Julius MarmingeandClaude Opus 5 authored and GitHub committed 2026-09-06 20:39:48 -07:00
1 parent f729e8fd83
commit 6abdf37a50
2 files changed
+117 -8

No files matched your search

+89
View File
@@ -433,6 +433,55 @@ describe("pools", () => {
expect(account?.environments).toEqual([{ environmentId: "env-a", label: "Laptop" }]);
});
it("redeems through the hub when it holds a credit, even with a fresher native read", () => {
const native = provider({
driver: claude,
instanceId: ProviderInstanceId.make("claude"),
auth: { status: "authenticated", email: "[email protected]" },
usageLimits: {
checkedAt: "2026-09-03T11:30:00.000Z",
windows: [{ ...window, usedPercent: 40 }],
resetCredits: { availableCount: 3, nextCreditId: "native-credit" },
},
});
const input = new Map([
[
EnvironmentId.make("env-a"),
{
...laptop,
serverConfig: {
providers: [native],
usageLimitSources: [
{
...source,
accounts: [
{
id: "[email protected]",
driver: claude,
email: "[email protected]",
usageLimits: {
checkedAt,
windows: [{ ...window, usedPercent: 55 }],
resetCredits: { availableCount: 2, nextCreditId: "hub-credit" },
},
},
],
},
],
},
},
],
]);
const [account] = collectLimitAccounts(input);
// Only the hub path clears the routing cooldown it holds for this account.
expect(account?.redeem).toEqual({
environmentId: "env-a",
input: { sourceId: "hub", accountId: "[email protected]", creditId: "hub-credit" },
});
// The fresher native balance is still the one shown.
expect(account?.limits.resetCredits?.availableCount).toBe(3);
});
it("redeems on the environment whose snapshot supplied the credits on show", () => {
const stale = provider({
auth: { status: "authenticated", email: "[email protected]" },
@@ -839,6 +888,46 @@ describe("/usage-limits", () => {
});
});
it("redeems a native duplicate through the hub even when the native snapshot is fresher", () => {
const fresher = provider({
usageLimits: {
checkedAt: "2026-09-03T11:30:00.000Z",
windows: [window],
resetCredits: { availableCount: 3, nextCreditId: "native-credit" },
},
auth: { status: "authenticated", email: "[email protected]" },
});
const stale = [
{
id: UsageLimitSourceId.make("hub"),
kind: "cliproxy" as const,
label: "Accounts",
checkedAt: limits.checkedAt,
accounts: [
{
id: "duplicate",
driver: fresher.driver,
email: "[email protected]",
usageLimits: {
...limits,
resetCredits: { availableCount: 2, nextCreditId: "hub-credit" },
},
},
],
},
];
const report = collectProviderUsageLimits(fresher.instanceId, [fresher], stale, now);
// Only redeeming through the hub clears the routing cooldown it holds for
// this account, so the hub wins the path even with a staler balance.
expect(report?.accounts[0]?.resetCreditInput).toEqual({
sourceId: "hub",
accountId: "duplicate",
creditId: "hub-credit",
});
// The fresher native balance is still the one shown.
expect(report?.accounts[0]?.limits.resetCredits?.availableCount).toBe(3);
});
it("keeps accounts and custom instances separate, filtering by driver", () => {
const report = collectProviderUsageLimits(
selected.instanceId,
+28 -8
View File
@@ -189,7 +189,21 @@ export function collectLimitAccounts(
): readonly LimitAccount[] {
const accounts = new Map<string, LimitAccount>();
const creditSources = new Map<string, LimitAccount>();
const hubRedeems = new Map<string, LimitAccount>();
const merge = (key: string, next: LimitAccount) => {
// Redeeming through a hub also clears the routing cooldown that hub holds
// for the account. Redeeming natively against the same subscription resets
// it upstream but leaves the hub refusing to route to the account until
// its own cooldown expires, so a hub target wins the redemption outright
// while the displayed balance still follows the freshest read.
const previousHub = hubRedeems.get(key);
if (
next.redeem &&
"sourceId" in next.redeem.input &&
(!previousHub || Date.parse(next.limits.checkedAt) > Date.parse(previousHub.limits.checkedAt))
) {
hubRedeems.set(key, next);
}
const previousCredit = creditSources.get(key);
if (
next.limits.resetCredits &&
@@ -224,9 +238,9 @@ export function collectLimitAccounts(
environments,
// A hub only names the account when no environment has it natively.
sourceLabel: environments.length > 0 ? null : (previous.sourceLabel ?? next.sourceLabel),
redeem: creditSource
? creditSource.redeem
: (winner.redeem ?? previous.redeem ?? next.redeem),
redeem:
hubRedeems.get(key)?.redeem ??
(creditSource ? creditSource.redeem : (winner.redeem ?? previous.redeem ?? next.redeem)),
limits: {
...winner.limits,
...(creditSource?.limits.resetCredits
@@ -645,14 +659,20 @@ export function collectProviderUsageLimits(
(a, b) =>
Date.parse(b.account.usageLimits.checkedAt) - Date.parse(a.account.usageLimits.checkedAt),
)[0];
const useHubCredits =
// Two independent decisions. Which balance to *display* follows whichever
// snapshot is fresher. Which path to *redeem through* always prefers the
// hub, because only the hub path clears the routing cooldown it holds for
// that account; redeeming natively against the same account resets the
// subscription upstream but leaves the hub refusing to route to it until
// its own cooldown expires. A hub credit id that a fresher native redeem
// already spent comes back as `alreadyRedeemed`, which still clears the
// cooldown, so preferring it is safe even when the hub snapshot is stale.
const hubCreditId = hubCredits?.account.usageLimits.resetCredits?.nextCreditId;
const showHubCredits =
hubCredits &&
(!provider.usageLimits.resetCredits ||
Date.parse(hubCredits.account.usageLimits.checkedAt) >
Date.parse(provider.usageLimits.checkedAt));
const hubCreditId = useHubCredits
? hubCredits.account.usageLimits.resetCredits?.nextCreditId
: undefined;
accounts.push({
id: provider.instanceId,
driver: provider.driver,
@@ -670,7 +690,7 @@ export function collectProviderUsageLimits(
...(provider.displayName ? { displayName: provider.displayName } : {}),
...(provider.accentColor ? { accentColor: provider.accentColor } : {}),
...(provider.auth.email ? { email: provider.auth.email } : {}),
limits: useHubCredits
limits: showHubCredits
? { ...provider.usageLimits, resetCredits: hubCredits.account.usageLimits.resetCredits }
: provider.usageLimits,
});