Skip to content

Commit 7d12eb1

Browse files
wtfsayoclaude
andcommitted
fix(quota): tighten Devin quota mapping and failure handling
- Publish the credit pool only for credit-billed plans (or an unknown strategy with no dated window); a quota-billed plan with a zero credit balance no longer reads monthly-exhausted. - Drop dated windows whose reset is not after now. - Read the body through the bounded byte reader and decode inside the try, so a truncated or oversized body keeps last-good instead of throwing. - Treat 409 and 499 as retryable alongside 408 and 429. - Cover flow credits (#5/#9/#13), hide_weekly (#37), and 403/404/429. Co-Authored-By: Claude Opus 5.5 (1M context) <[email protected]>
1 parent b493e0d commit 7d12eb1

3 files changed

Lines changed: 110 additions & 34 deletions

File tree

‎src/providers/quota/devin.ts‎

Lines changed: 30 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -23,11 +23,15 @@ import { randomUUID } from "node:crypto";
2323
import { buildMetadata } from "../../adapters/devin/cloud-direct/metadata";
2424
import { encodeMessage, iterFields } from "../../adapters/devin/cloud-direct/wire";
2525
import { DEVIN_DEFAULT_API_SERVER, validateDevinApiBaseUrl } from "../../oauth/devin/api-base";
26+
import { readBoundedResponseBytes } from "../../lib/bounded-body";
2627
import { epochMillis, QUOTA_RESPONSE_MAX_BYTES, REQUEST_TIMEOUT_MS } from "../quota-wire";
2728
import type { ProviderQuota, ProviderQuotaWindow } from "../quota-types";
2829
import { AUTHORITATIVE_EMPTY_QUOTA, report, TERMINAL_QUOTA_FAILURE, type ProviderQuotaProbeResult } from "./report-cache";
2930

3031
const GET_USER_STATUS_PATH = "/exa.seat_management_pb.SeatManagementService/GetUserStatus";
32+
const BILLING_STRATEGY_CREDITS = 1;
33+
/** Timeout, Connect `aborted`, throttle, client-closed: the account may be fine. */
34+
const RETRYABLE_STATUSES = new Set([408, 409, 429, 499]);
3135

3236
export interface DevinPlanInfo {
3337
teamsTier: number;
@@ -132,25 +136,34 @@ function remainingToUsed(remaining: number): number {
132136
}
133137

134138
/**
135-
* Only DATED percent windows are published. A credit-billed plan leaves the percent fields
136-
* at their zero default, which would otherwise read as a fully spent window and mark the
139+
* Only DATED percent windows whose reset is still ahead are published. A credit-billed plan
140+
* leaves the percent fields at their zero default, and a reset already in the past describes
141+
* a window that has rolled over; either would read as a fully spent window and mark the
137142
* account exhausted for routing.
138143
*
144+
* The credit pool is published only for a credit-billed plan, or for an unknown strategy
145+
* with no dated window. A quota-billed plan still reports credit balances, and a zero
146+
* balance there does not gate anything.
147+
*
139148
* Credits are measured against the server's own balance rather than the plan grant, so
140149
* top-ups count. Flex credits back prompt credits once those run out, so both share one
141150
* pool: an account with prompt credits spent and flex remaining can still serve.
142151
*/
143152
export function devinQuotaFromStatus(status: DevinUserStatus, now = Date.now()): ProviderQuota {
144153
const quota: ProviderQuota = { updatedAt: now };
145154
const customWindows: ProviderQuotaWindow[] = [];
146-
if (!status.plan.hideDailyQuota && status.dailyResetMs !== undefined) {
155+
const ahead = (resetMs: number | undefined): resetMs is number => resetMs !== undefined && resetMs > now;
156+
if (!status.plan.hideDailyQuota && ahead(status.dailyResetMs)) {
147157
customWindows.push({ label: "Daily", percent: remainingToUsed(status.dailyRemainingPercent), resetAt: status.dailyResetMs });
148158
}
149-
if (!status.plan.hideWeeklyQuota && status.weeklyResetMs !== undefined) {
159+
if (!status.plan.hideWeeklyQuota && ahead(status.weeklyResetMs)) {
150160
quota.weeklyPercent = remainingToUsed(status.weeklyRemainingPercent);
151161
quota.weeklyResetAt = status.weeklyResetMs;
152162
}
153-
const credits = status.availablePromptCredits < 0 || status.availableFlexCredits < 0
163+
const strategy = status.plan.billingStrategy;
164+
const creditBilled = strategy === BILLING_STRATEGY_CREDITS
165+
|| (strategy === 0 && !ahead(status.dailyResetMs) && !ahead(status.weeklyResetMs));
166+
const credits = !creditBilled || status.availablePromptCredits < 0 || status.availableFlexCredits < 0
154167
? undefined
155168
: usedPercent(status.usedPromptCredits + status.usedFlexCredits, status.availablePromptCredits + status.availableFlexCredits);
156169
if (credits !== undefined) {
@@ -178,7 +191,6 @@ export async function fetchDevinQuota(provider: string, apiKey: string, apiBaseU
178191
requestId: BigInt(Date.now()),
179192
triggerId: randomUUID(),
180193
});
181-
let buf: Buffer;
182194
try {
183195
const response = await fetch(`${base}${GET_USER_STATUS_PATH}`, {
184196
method: "POST",
@@ -191,23 +203,22 @@ export async function fetchDevinQuota(provider: string, apiKey: string, apiBaseU
191203
});
192204
if (!response.ok) {
193205
void response.body?.cancel().catch(() => undefined);
194-
return response.status >= 400 && response.status < 500 && response.status !== 408 && response.status !== 429
206+
return response.status >= 400 && response.status < 500 && !RETRYABLE_STATUSES.has(response.status)
195207
? TERMINAL_QUOTA_FAILURE
196208
: null;
197209
}
198-
const declared = Number(response.headers.get("content-length"));
199-
if (Number.isFinite(declared) && declared > QUOTA_RESPONSE_MAX_BYTES) {
200-
void response.body?.cancel().catch(() => undefined);
201-
return null;
202-
}
203-
buf = Buffer.from(await response.arrayBuffer());
210+
const body = await readBoundedResponseBytes(response, {
211+
maxBytes: QUOTA_RESPONSE_MAX_BYTES,
212+
inactivityTimeoutMs: REQUEST_TIMEOUT_MS,
213+
});
214+
if (body.oversized) return null;
215+
// Inside the try: a truncated varint throws, and a malformed body must keep last-good.
216+
const status = decodeDevinUserStatus(Buffer.from(body.bytes.buffer, body.bytes.byteOffset, body.bytes.byteLength));
217+
if (!status) return null;
218+
// A decoded status with nothing measurable (an unlimited plan, hidden windows) is an
219+
// authoritative answer, not a failed probe, so it must replace any last-good row.
220+
return report(provider, "devin:user-status", devinQuotaFromStatus(status)) ?? AUTHORITATIVE_EMPTY_QUOTA;
204221
} catch {
205222
return null;
206223
}
207-
if (buf.length > QUOTA_RESPONSE_MAX_BYTES) return null;
208-
const status = decodeDevinUserStatus(buf);
209-
if (!status) return null;
210-
// A decoded status with nothing measurable (an unlimited plan, hidden windows) is an
211-
// authoritative answer, not a failed probe, so it must replace any last-good row.
212-
return report(provider, "devin:user-status", devinQuotaFromStatus(status)) ?? AUTHORITATIVE_EMPTY_QUOTA;
213224
}

‎structure/providers-and-adapters.md‎

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -71,11 +71,14 @@ video quota rows are unrelated and omitted.
7171

7272
Devin account quota in `src/providers/quota/devin.ts` reads Cognition's unary
7373
`SeatManagementService/GetUserStatus` with the default cloud-direct Metadata, against the
74-
credential's allowlisted api-server host with redirects refused. It publishes only dated daily
75-
and weekly windows the plan does not hide, because a credit-billed plan leaves those percents at
76-
a zero default that would read as exhausted. Prompt plus flex credits form one monthly pool
77-
measured against the server balance, and a negative balance is the unlimited sentinel. A 4xx
78-
other than 408/429 is terminal; a decoded status with nothing measurable is authoritative-empty.
74+
credential's allowlisted api-server host with redirects refused and a bounded body read. It
75+
publishes only daily and weekly windows the plan does not hide whose reset is still ahead,
76+
because a credit-billed plan leaves those percents at a zero default and a past reset describes a
77+
rolled-over window; both would read as exhausted. Prompt plus flex credits form one monthly pool
78+
measured against the server balance, published only for a credit-billed plan (or an unknown
79+
strategy with no dated window); a negative balance is the unlimited sentinel. A 4xx other than
80+
408/409/429/499 is terminal; a malformed body keeps last-good; a decoded status with nothing
81+
measurable is authoritative-empty.
7982

8083
Kiro's account quota cache persists quota and an optional exhaustion verdict under one
8184
opaque account key and a non-secret login identity. Hydration admits only matching live

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

Lines changed: 72 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -4,27 +4,29 @@ import { tmpdir } from "node:os";
44
import { join } from "node:path";
55
import { encodeMessage, encodeString, encodeVarintField, iterFields } from "../../src/adapters/devin/cloud-direct/wire";
66
import { saveCredential } from "../../src/oauth/store";
7-
import { clearProviderQuotaCache, fetchProviderQuotaReports } from "../../src/providers/quota";
7+
import { clearProviderQuotaCache, fetchProviderQuotaReports, QUOTA_RESPONSE_MAX_BYTES } from "../../src/providers/quota";
88
import { decodeDevinUserStatus, devinQuotaFromStatus, fetchDevinQuota } from "../../src/providers/quota/devin";
99
import { AUTHORITATIVE_EMPTY_QUOTA, TERMINAL_QUOTA_FAILURE } from "../../src/providers/quota/report-cache";
1010
import type { OcxConfig } from "../../src/types";
1111
import { removeTreeWithRetry } from "../helpers/remove-tree";
1212

1313
const KEY = "devin-session-token$synthetic.fixture.key";
14-
const PLAN_END = 1_792_000_000;
15-
const DAILY_RESET = 1_790_500_000;
16-
const WEEKLY_RESET = 1_791_000_000;
14+
// Far enough ahead that the fetch-path tests, which use the real clock, see live windows.
15+
const PLAN_END = 4_000_000_000;
16+
const DAILY_RESET = 3_990_000_000;
17+
const WEEKLY_RESET = 3_995_000_000;
1718

1819
/** Signed ints ride the wire as 64-bit two's complement, exactly like the server sends -1. */
1920
const int = (num: number, value: number) => encodeVarintField(num, BigInt.asUintN(64, BigInt(value)));
2021

21-
function planInfo(p: { tier: number; name: string; billing: number; hideDaily?: boolean; monthlyFlow?: number }): Buffer {
22+
function planInfo(p: { tier: number; name: string; billing: number; hideDaily?: boolean; hideWeekly?: boolean; monthlyFlow?: number }): Buffer {
2223
return Buffer.concat([
2324
int(1, p.tier),
2425
encodeString(2, p.name),
2526
...(p.monthlyFlow ? [int(13, p.monthlyFlow)] : []),
2627
int(35, p.billing),
2728
...(p.hideDaily ? [int(36, 1)] : []),
29+
...(p.hideWeekly ? [int(37, 1)] : []),
2830
]);
2931
}
3032

@@ -52,6 +54,12 @@ const creditPlan = (status: Record<number, number>) => userStatusResponse(
5254
false,
5355
);
5456

57+
const creditPlanWithFlow = (monthlyFlow: number, status: Record<number, number>) => userStatusResponse(
58+
planInfo({ tier: 16, name: "Pro", billing: 1, monthlyFlow }),
59+
status,
60+
false,
61+
);
62+
5563
describe("Devin GetUserStatus decode and mapping", () => {
5664
test("quota-billed plan publishes the dated weekly window and nothing hidden or unlimited", () => {
5765
const status = decodeDevinUserStatus(quotaPlan());
@@ -85,6 +93,48 @@ describe("Devin GetUserStatus decode and mapping", () => {
8593
expect(spent.monthlyPercent).toBe(100);
8694
});
8795

96+
test("a quota-billed plan with a zero credit balance is not credit-exhausted", () => {
97+
const buf = userStatusResponse(planInfo({ tier: 17, name: "Max", billing: 2 }), { 6: 10, 8: 0, 14: 100, 15: 90 });
98+
const quota = devinQuotaFromStatus(decodeDevinUserStatus(buf)!, 1);
99+
expect(quota.weeklyPercent).toBe(10);
100+
expect(quota.monthlyPercent).toBeUndefined();
101+
});
102+
103+
test("an unknown billing strategy uses credits only when no window is dated", () => {
104+
const undated = userStatusResponse(planInfo({ tier: 16, name: "Pro", billing: 0 }), { 6: 50, 8: 50 }, false);
105+
expect(devinQuotaFromStatus(decodeDevinUserStatus(undated)!, 1).monthlyPercent).toBe(50);
106+
const dated = userStatusResponse(planInfo({ tier: 16, name: "Pro", billing: 0 }), { 6: 50, 8: 50, 14: 100, 15: 100 });
107+
expect(devinQuotaFromStatus(decodeDevinUserStatus(dated)!, 1).monthlyPercent).toBeUndefined();
108+
});
109+
110+
test("a window whose reset has already passed is dropped rather than read as spent", () => {
111+
const buf = userStatusResponse(planInfo({ tier: 17, name: "Max", billing: 2 }), { 8: -1, 14: 0, 15: 0 });
112+
const status = decodeDevinUserStatus(buf)!;
113+
expect(devinQuotaFromStatus(status, WEEKLY_RESET * 1000 + 1)).toEqual({ updatedAt: WEEKLY_RESET * 1000 + 1 });
114+
expect(devinQuotaFromStatus(status, DAILY_RESET * 1000 + 1)).toEqual({
115+
updatedAt: DAILY_RESET * 1000 + 1,
116+
weeklyPercent: 100,
117+
weeklyResetAt: WEEKLY_RESET * 1000,
118+
});
119+
});
120+
121+
test("hide_weekly_quota (#37) suppresses the weekly window", () => {
122+
const buf = userStatusResponse(planInfo({ tier: 17, name: "Max", billing: 2, hideWeekly: true }), { 8: -1, 14: 70, 15: 10 });
123+
expect(devinQuotaFromStatus(decodeDevinUserStatus(buf)!, 1)).toEqual({
124+
updatedAt: 1,
125+
customWindows: [{ label: "Daily", percent: 30, resetAt: DAILY_RESET * 1000 }],
126+
});
127+
});
128+
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();
136+
});
137+
88138
test("a response without PlanStatus is not a usable status", () => {
89139
expect(decodeDevinUserStatus(encodeMessage(1, encodeString(3, "Fixture User")))).toBeNull();
90140
expect(decodeDevinUserStatus(Buffer.alloc(0))).toBeNull();
@@ -112,15 +162,27 @@ describe("fetchDevinQuota transport", () => {
112162
expect(([...iterFields(metadata)].find(f => f.num === 3)?.value as Buffer).toString()).toBe(KEY);
113163
});
114164

115-
test("401 is terminal, 5xx and network faults keep the last-good row", async () => {
116-
globalThis.fetch = (async () => new Response("unauthenticated: " + KEY, { status: 401 })) as unknown as typeof fetch;
117-
expect(await fetchDevinQuota("devin", KEY, undefined)).toBe(TERMINAL_QUOTA_FAILURE);
118-
globalThis.fetch = (async () => new Response("", { status: 503 })) as unknown as typeof fetch;
119-
expect(await fetchDevinQuota("devin", KEY, undefined)).toBeNull();
165+
test("401/403/404 are terminal; 408/409/429/499, 5xx and network faults keep the last-good row", async () => {
166+
for (const status of [401, 403, 404]) {
167+
globalThis.fetch = (async () => new Response("unauthenticated: " + KEY, { status })) as unknown as typeof fetch;
168+
expect(await fetchDevinQuota("devin", KEY, undefined)).toBe(TERMINAL_QUOTA_FAILURE);
169+
}
170+
for (const status of [408, 409, 429, 499, 503]) {
171+
globalThis.fetch = (async () => new Response("", { status })) as unknown as typeof fetch;
172+
expect(await fetchDevinQuota("devin", KEY, undefined)).toBeNull();
173+
}
120174
globalThis.fetch = (async () => { throw new TypeError("network down"); }) as unknown as typeof fetch;
121175
expect(await fetchDevinQuota("devin", KEY, undefined)).toBeNull();
122176
});
123177

178+
test("a truncated or oversized body keeps the last-good row instead of throwing", async () => {
179+
// Tag for field 1 varint, then a continuation byte with nothing after it.
180+
globalThis.fetch = (async () => new Response(new Uint8Array([0x08, 0x80]), { status: 200 })) as unknown as typeof fetch;
181+
expect(await fetchDevinQuota("devin", KEY, undefined)).toBeNull();
182+
globalThis.fetch = (async () => new Response(new Uint8Array(QUOTA_RESPONSE_MAX_BYTES + 1), { status: 200 })) as unknown as typeof fetch;
183+
expect(await fetchDevinQuota("devin", KEY, undefined)).toBeNull();
184+
});
185+
124186
test("a decoded status with nothing measurable is authoritative-empty", async () => {
125187
const unlimited = userStatusResponse(planInfo({ tier: 17, name: "Max", billing: 2 }), { 8: -1 }, false);
126188
globalThis.fetch = (async () => new Response(new Uint8Array(unlimited), { status: 200 })) as unknown as typeof fetch;

0 commit comments

Comments
 (0)