Skip to content

Commit a71f149

Browse files
wtfsayoclaude
andcommitted
fix(quota): reject truncated Devin quota protobufs and document the probe
- Require the decoded fields to account for every byte. iterFields stops quietly at a truncated length-delimited field, so half a response decoded to an empty status and was published as authoritative-empty, deleting the last-good row. - Use the Web Crypto randomUUID instead of node:crypto. - List Devin in the per-account quota section of the CLI accounts reference. Co-Authored-By: Claude Opus 5.5 (1M context) <[email protected]>
1 parent 7d12eb1 commit a71f149

3 files changed

Lines changed: 37 additions & 13 deletions

File tree

‎docs-site/src/content/docs/reference/cli/providers-accounts.md‎

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -351,7 +351,7 @@ returns:
351351
```
352352

353353
`--quota` adds a `QUOTA` column with each account's own usage, for providers that support a
354-
per-account probe (Anthropic, Kiro, and Google Antigravity today). It is opt-in because the proxy probes the upstream
354+
per-account probe (Anthropic, Kiro, Google Antigravity, and Devin today). It is opt-in because the proxy probes the upstream
355355
once per stored credential; the default listing stays a local read. `--refresh` bypasses the
356356
cached result. An account with no per-account quota shows `-`, and one whose probe failed shows
357357
`unavailable` — blank would read as "no usage" rather than "not measured". `--json` carries the
@@ -364,6 +364,10 @@ configured `baseUrl`: a custom base URL is a routing choice for requests, not a
364364
Google's accounting for a stored credential. An account without a project id, or one whose probe
365365
is redirected or fails, shows `unavailable`.
366366

367+
Devin rows come from Cognition's `GetUserStatus` for that account's own key, sent only to its
368+
allowlisted api-server host. They show the dated daily and weekly windows the plan exposes, and a
369+
monthly credit window only on a credit-billed plan; an unlimited balance shows no credit window.
370+
367371
```text
368372
$ ocx account list anthropic --quota
369373
PROVIDER TYPE ID PLAN/LABEL PRIORITY STATUS QUOTA

‎src/providers/quota/devin.ts‎

Lines changed: 26 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -19,9 +19,8 @@
1919
* PlanInfo { #1 teams_tier, #2 plan_name, #13 monthly_flow, #35 billing_strategy,
2020
* #36 hide_daily_quota, #37 hide_weekly_quota }
2121
*/
22-
import { randomUUID } from "node:crypto";
2322
import { buildMetadata } from "../../adapters/devin/cloud-direct/metadata";
24-
import { encodeMessage, iterFields } from "../../adapters/devin/cloud-direct/wire";
23+
import { encodeMessage, encodeTag, encodeVarint, iterFields } from "../../adapters/devin/cloud-direct/wire";
2524
import { DEVIN_DEFAULT_API_SERVER, validateDevinApiBaseUrl } from "../../oauth/devin/api-base";
2625
import { readBoundedResponseBytes } from "../../lib/bounded-body";
2726
import { epochMillis, QUOTA_RESPONSE_MAX_BYTES, REQUEST_TIMEOUT_MS } from "../quota-wire";
@@ -60,12 +59,23 @@ export interface DevinUserStatus {
6059

6160
type Fields = Map<number, bigint | Buffer>;
6261

63-
/** Last occurrence wins, as protobuf specifies for a non-repeated field. */
64-
function fieldsOf(buf: Buffer | undefined): Fields {
62+
/**
63+
* Last occurrence wins, as protobuf specifies for a non-repeated field. Null when the fields do
64+
* not account for every byte: iterFields stops quietly at a truncated field, and a status read
65+
* from half a response would otherwise be published as an authoritative empty quota.
66+
*/
67+
function fieldsOf(buf: Buffer | undefined): Fields | null {
6568
const out: Fields = new Map();
6669
if (!buf) return out;
67-
for (const field of iterFields(buf)) out.set(field.num, field.value);
68-
return out;
70+
let consumed = 0;
71+
for (const field of iterFields(buf)) {
72+
out.set(field.num, field.value);
73+
const body = typeof field.value === "bigint" ? encodeVarint(field.value).length
74+
: field.wire === 2 ? encodeVarint(field.value.length).length + field.value.length
75+
: field.value.length;
76+
consumed += encodeTag(field.num, field.wire).length + body;
77+
}
78+
return consumed === buf.length ? out : null;
6979
}
7080

7181
/** int32/int64 on the wire are two's-complement varints; -1 arrives as 2^64-1. */
@@ -80,11 +90,13 @@ function sub(fields: Fields, num: number): Buffer | undefined {
8090
}
8191

8292
function timestampMs(buf: Buffer | undefined): number | undefined {
83-
return buf ? epochMillis(int(fieldsOf(buf), 1)) : undefined;
93+
const f = buf ? fieldsOf(buf) : null;
94+
return f ? epochMillis(int(f, 1)) : undefined;
8495
}
8596

86-
function decodePlanInfo(buf: Buffer | undefined): DevinPlanInfo {
97+
function decodePlanInfo(buf: Buffer | undefined): DevinPlanInfo | null {
8798
const f = fieldsOf(buf);
99+
if (!f) return null;
88100
return {
89101
teamsTier: int(f, 1),
90102
planName: sub(f, 2)?.toString("utf8").trim() ?? "",
@@ -98,12 +110,14 @@ function decodePlanInfo(buf: Buffer | undefined): DevinPlanInfo {
98110
/** Null when the response carries no PlanStatus, which is not something a mapper can use. */
99111
export function decodeDevinUserStatus(buf: Buffer): DevinUserStatus | null {
100112
const response = fieldsOf(buf);
101-
const user = fieldsOf(sub(response, 1));
102-
const statusBuf = sub(user, 13);
113+
const user = response && fieldsOf(sub(response, 1));
114+
const statusBuf = user && sub(user, 13);
103115
if (!statusBuf) return null;
104116
const status = fieldsOf(statusBuf);
117+
if (!status) return null;
105118
// The top-level PlanInfo is authoritative; the nested copy covers servers that omit it.
106119
const plan = decodePlanInfo(sub(response, 2) ?? sub(status, 1));
120+
if (!plan) return null;
107121
if (!plan.teamsTier) plan.teamsTier = int(user, 10);
108122
const planEndMs = timestampMs(sub(status, 3));
109123
const dailyResetMs = epochMillis(int(status, 17));
@@ -187,9 +201,9 @@ export async function fetchDevinQuota(provider: string, apiKey: string, apiBaseU
187201
if (!base || !apiKey.trim()) return null;
188202
const metadata = buildMetadata({
189203
apiKey,
190-
sessionId: randomUUID(),
204+
sessionId: crypto.randomUUID(),
191205
requestId: BigInt(Date.now()),
192-
triggerId: randomUUID(),
206+
triggerId: crypto.randomUUID(),
193207
});
194208
try {
195209
const response = await fetch(`${base}${GET_USER_STATUS_PATH}`, {

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

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -183,6 +183,12 @@ describe("fetchDevinQuota transport", () => {
183183
expect(await fetchDevinQuota("devin", KEY, undefined)).toBeNull();
184184
});
185185

186+
test("a complete field followed by a truncated one is malformed, not authoritative-empty", async () => {
187+
// An empty UserStatus, then field 2 declaring 127 bytes with none present.
188+
globalThis.fetch = (async () => new Response(new Uint8Array([0x0a, 0x02, 0x6a, 0x00, 0x12, 0x7f]), { status: 200 })) as unknown as typeof fetch;
189+
expect(await fetchDevinQuota("devin", KEY, undefined)).toBeNull();
190+
});
191+
186192
test("a decoded status with nothing measurable is authoritative-empty", async () => {
187193
const unlimited = userStatusResponse(planInfo({ tier: 17, name: "Max", billing: 2 }), { 8: -1 }, false);
188194
globalThis.fetch = (async () => new Response(new Uint8Array(unlimited), { status: 200 })) as unknown as typeof fetch;

0 commit comments

Comments
 (0)