Skip to content

Commit 9e23686

Browse files
wtfsayoclaude
andcommitted
fix(quota): stop flow credits gating Devin accounts; key the cache by tenant host
- Flow credits are decoded but no longer published. They meter agent tool actions, not chat turns, and ranking reads the fullest custom window as the account's limit, so an empty flow balance retired accounts that could still serve. - A credit-billed plan with nothing used and nothing left reads 100% instead of unmeasured, so ranking no longer prefers it as untested. - The account quota identity includes the credential's own api base URL when it has one, so a tenant host change invalidates the cached reading. Credentials without one keep their existing key. Co-Authored-By: Claude Opus 5.5 (1M context) <[email protected]>
1 parent a71f149 commit 9e23686

3 files changed

Lines changed: 37 additions & 12 deletions

File tree

‎src/providers/quota/account-cache.ts‎

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -466,6 +466,9 @@ export function quotaCredentialIdentity(provider: string, accountId: string, cre
466466
provider, accountId, credential.access, credential.refresh, credential.expires,
467467
credential.accountId, credential.projectId, credential.source,
468468
target.adapter, target.baseUrl, target.authMode, target.disabled === true,
469+
// Only credentials that carry their own endpoint (Devin tenants) extend the identity, so
470+
// every other provider's existing cache keys stay valid.
471+
...(credential.apiBaseUrl ? [credential.apiBaseUrl] : []),
469472
])).digest("hex");
470473
}
471474

‎src/providers/quota/devin.ts‎

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -159,6 +159,10 @@ function remainingToUsed(remaining: number): number {
159159
* with no dated window. A quota-billed plan still reports credit balances, and a zero
160160
* balance there does not gate anything.
161161
*
162+
* Flow credits are decoded but not published. They meter agent tool actions, not chat
163+
* turns, and account ranking reads the fullest custom window as the account's limit, so an
164+
* empty flow balance would retire an account that can still serve chat.
165+
*
162166
* Credits are measured against the server's own balance rather than the plan grant, so
163167
* top-ups count. Flex credits back prompt credits once those run out, so both share one
164168
* pool: an account with prompt credits spent and flex remaining can still serve.
@@ -177,17 +181,17 @@ export function devinQuotaFromStatus(status: DevinUserStatus, now = Date.now()):
177181
const strategy = status.plan.billingStrategy;
178182
const creditBilled = strategy === BILLING_STRATEGY_CREDITS
179183
|| (strategy === 0 && !ahead(status.dailyResetMs) && !ahead(status.weeklyResetMs));
184+
const available = status.availablePromptCredits + status.availableFlexCredits;
180185
const credits = !creditBilled || status.availablePromptCredits < 0 || status.availableFlexCredits < 0
181186
? undefined
182-
: usedPercent(status.usedPromptCredits + status.usedFlexCredits, status.availablePromptCredits + status.availableFlexCredits);
187+
// A credit-billed plan with nothing used and nothing left has no balance to serve from;
188+
// leaving it unmeasured would rank it as untested headroom.
189+
: available === 0 ? 100
190+
: usedPercent(status.usedPromptCredits + status.usedFlexCredits, available);
183191
if (credits !== undefined) {
184192
quota.monthlyPercent = credits;
185193
if (status.planEndMs !== undefined) quota.monthlyResetAt = status.planEndMs;
186194
}
187-
const flow = status.plan.monthlyFlowCredits > 0 ? usedPercent(status.usedFlowCredits, status.availableFlowCredits) : undefined;
188-
if (flow !== undefined) {
189-
customWindows.push({ label: "Flow credits", percent: flow, ...(status.planEndMs !== undefined ? { resetAt: status.planEndMs } : {}) });
190-
}
191195
if (customWindows.length > 0) quota.customWindows = customWindows;
192196
return quota;
193197
}

‎tests/providers/devin-quota.test.ts‎

Lines changed: 25 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -126,13 +126,19 @@ describe("Devin GetUserStatus decode and mapping", () => {
126126
});
127127
});
128128

129-
test("flow credits read used #5 against available #9 only when the plan grants them (#13)", () => {
130-
const granted = creditPlanWithFlow(100, { 5: 30, 9: 70 });
131-
expect(devinQuotaFromStatus(decodeDevinUserStatus(granted)!, 1).customWindows).toEqual([
132-
{ label: "Flow credits", percent: 30, resetAt: PLAN_END * 1000 },
133-
]);
134-
const ungranted = creditPlanWithFlow(0, { 5: 30, 9: 70 });
135-
expect(devinQuotaFromStatus(decodeDevinUserStatus(ungranted)!, 1).customWindows).toBeUndefined();
129+
test("flow credits decode from #5/#9/#13 but never gate the account", () => {
130+
const status = decodeDevinUserStatus(creditPlanWithFlow(100, { 5: 30, 9: 70 }))!;
131+
expect(status.usedFlowCredits).toBe(30);
132+
expect(status.availableFlowCredits).toBe(70);
133+
expect(status.plan.monthlyFlowCredits).toBe(100);
134+
const exhausted = decodeDevinUserStatus(creditPlanWithFlow(100, { 5: 100, 9: 0, 6: 10, 8: 90 }))!;
135+
const quota = devinQuotaFromStatus(exhausted, 1);
136+
expect(quota.customWindows).toBeUndefined();
137+
expect(quota.monthlyPercent).toBe(10);
138+
});
139+
140+
test("a credit-billed plan with nothing used and nothing left reads exhausted, not unmeasured", () => {
141+
expect(devinQuotaFromStatus(decodeDevinUserStatus(creditPlan({ 6: 0, 8: 0 }))!, 1).monthlyPercent).toBe(100);
136142
});
137143

138144
test("a response without PlanStatus is not a usable status", () => {
@@ -237,3 +243,15 @@ describe("Devin provider quota through the aggregator", () => {
237243
expect(result.reports.filter(r => r.provider === "devin")).toEqual([]);
238244
});
239245
});
246+
247+
describe("Devin quota cache identity", () => {
248+
test("a tenant host change invalidates the cached reading; a credential without one keeps its key", async () => {
249+
const { quotaCredentialIdentity } = await import("../../src/providers/quota/account-cache");
250+
const target = { adapter: "devin", authMode: "oauth" } as any;
251+
const base = { access: KEY, refresh: "", expires: Number.MAX_SAFE_INTEGER } as any;
252+
const us = quotaCredentialIdentity("devin", "a1", { ...base, apiBaseUrl: "https://server.codeium.com" }, target);
253+
const eu = quotaCredentialIdentity("devin", "a1", { ...base, apiBaseUrl: "https://eu.windsurf.com/_route/api_server" }, target);
254+
expect(us).not.toBe(eu);
255+
expect(quotaCredentialIdentity("devin", "a1", base, target)).toBe(quotaCredentialIdentity("devin", "a1", { ...base, apiBaseUrl: undefined }, target));
256+
});
257+
});

0 commit comments

Comments
 (0)