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" ;
2322import { 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" ;
2524import { DEVIN_DEFAULT_API_SERVER , validateDevinApiBaseUrl } from "../../oauth/devin/api-base" ;
2625import { readBoundedResponseBytes } from "../../lib/bounded-body" ;
2726import { epochMillis , QUOTA_RESPONSE_MAX_BYTES , REQUEST_TIMEOUT_MS } from "../quota-wire" ;
@@ -60,12 +59,23 @@ export interface DevinUserStatus {
6059
6160type 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
8292function 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. */
99111export 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 } ` , {
0 commit comments