Skip to content

Commit 6e871c9

Browse files
fix(server): bound optional Claude profile lookup latency
1 parent bf52e93 commit 6e871c9

2 files changed

Lines changed: 37 additions & 27 deletions

File tree

‎apps/server/src/usage/cliproxyApi.test.ts‎

Lines changed: 32 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,8 @@ function fixture(
5151
options: {
5252
accounts?: Array<(typeof accounts)[number] & { disabled?: boolean }>;
5353
upstream?: (request: RequestBody) => { status: number; body: unknown };
54+
/** Upstream calls it matches never get an answer from the hub. */
55+
stalls?: (request: RequestBody) => boolean;
5456
cooldownStatus?: number;
5557
} = {},
5658
) {
@@ -76,6 +78,7 @@ function fixture(
7678
);
7779
expect(path).toMatch(/\/v0\/management\/api-call$/);
7880
expect(body?.header?.Authorization).toBe("Bearer $TOKEN$");
81+
if (options.stalls?.(body!)) return undefined;
7982
const upstream = options.upstream?.(body!) ?? {
8083
status: 200,
8184
body: body?.url?.endsWith("/consume")
@@ -104,7 +107,12 @@ function fixture(
104107
request,
105108
Response.json({ status_code: upstream.status, body: encodeJson(upstream.body) }),
106109
);
107-
}),
110+
}).pipe(
111+
Effect.filterOrElse(
112+
(response) => response !== undefined,
113+
() => Effect.never,
114+
),
115+
),
108116
);
109117
return {
110118
requests,
@@ -334,50 +342,48 @@ describe("CLIProxyAPI built-in management API", () => {
334342
}),
335343
);
336344

337-
it.effect("labels Claude accounts with the plan tier from their profile", () =>
345+
it.effect("labels a hub Claude account with its plan tier", () =>
338346
Effect.gen(function* () {
339-
const organizations: Record<string, { organization_type: string; rate_limit_tier: string }> =
340-
{
341-
a: { organization_type: "claude_max", rate_limit_tier: "default_claude_max_5x" },
342-
b: { organization_type: "claude_max", rate_limit_tier: "default_claude_max_20x" },
343-
c: { organization_type: "claude_pro", rate_limit_tier: "default_claude_ai" },
344-
};
345347
const test = fixture({
346-
accounts: [
347-
...accounts,
348-
{ ...accounts[0]!, id: "third.json", auth_index: "c", email: "[email protected]" },
349-
].map((account) => ({ ...account, provider: "claude" })),
348+
accounts: [{ ...accounts[0]!, provider: "claude" }],
350349
upstream: (request) => ({
351350
status: 200,
352351
body: request.url?.endsWith("/profile")
353-
? { organization: organizations[request.auth_index] }
352+
? {
353+
organization: {
354+
organization_type: "claude_max",
355+
rate_limit_tier: "default_claude_max_20x",
356+
},
357+
}
354358
: { seven_day: { utilization: 50, resets_at: null } },
355359
}),
356360
});
357361
const api = yield* test.api;
358-
const result = yield* api.readAccounts(config);
359-
expect(result.map((account) => account.plan)).toEqual([
360-
"Claude Max 5x Subscription",
361-
"Claude Max 20x Subscription",
362-
"Claude Pro Subscription",
363-
]);
362+
const [account] = yield* api.readAccounts(config);
363+
expect(account?.plan).toBe("Claude Max 20x Subscription");
364364
}),
365365
);
366366

367-
it.effect("keeps Claude usage when the profile cannot be read", () =>
367+
it.effect("does not hold Claude usage until the hub times out a stalled profile", () =>
368368
Effect.gen(function* () {
369369
const test = fixture({
370370
accounts: [{ ...accounts[0]!, provider: "claude" }],
371-
upstream: (request) =>
372-
request.url?.endsWith("/profile")
373-
? { status: 503, body: { token: "do-not-publish" } }
374-
: { status: 200, body: { seven_day: { utilization: 50, resets_at: null } } },
371+
upstream: () => ({
372+
status: 200,
373+
body: { seven_day: { utilization: 50, resets_at: null } },
374+
}),
375+
stalls: (request) => request.url?.endsWith("/profile") ?? false,
375376
});
376377
const api = yield* test.api;
377-
const [account] = yield* api.readAccounts(config);
378+
const pending = yield* api
379+
.readAccounts(config)
380+
.pipe(Effect.forkChild({ startImmediately: true }));
381+
yield* TestClock.adjust("14 seconds");
382+
const exit = pending.pollUnsafe();
383+
expect(exit?._tag).toBe("Success");
384+
const [account] = yield* Fiber.join(pending);
378385
expect(account?.plan).toBe("Claude Subscription");
379386
expect(account?.usageLimits.windows.map((window) => window.usedPercent)).toEqual([50]);
380-
expect(encodeJson(account)).not.toContain("do-not-publish");
381387
}),
382388
);
383389

‎apps/server/src/usage/cliproxyApi.ts‎

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -150,6 +150,7 @@ const CODEX_BASE = "https://chatgpt.com/backend-api/wham";
150150
const CREDIT_URL = `${CODEX_BASE}/rate-limit-reset-credits`;
151151
const CLAUDE_OAUTH_BASE = "https://api.anthropic.com/api/oauth";
152152
const CLAUDE_HUB_PLAN = "Claude Subscription";
153+
const CLAUDE_PROFILE_TIMEOUT = "5 seconds";
153154

154155
/**
155156
* Names a hub Claude account's plan the way a native sign-in does. Max plans
@@ -291,10 +292,13 @@ export const makeCliproxyApi = Effect.gen(function* () {
291292
const [body, plan] = yield* Effect.all(
292293
[
293294
apiCall(config, account, `${CLAUDE_OAUTH_BASE}/usage`),
294-
// A profile outage must not hide successfully fetched quota windows.
295+
// The profile only names the plan. A stalled or failed profile must not hold the
296+
// quota windows for the hub's 15-second management timeout, and readAccounts runs
297+
// four accounts at a time, so later batches would wait too.
295298
apiCall(config, account, `${CLAUDE_OAUTH_BASE}/profile`).pipe(
296299
Effect.flatMap(decodeClaudeProfile),
297300
Effect.map(claudeProfilePlanLabel),
301+
Effect.timeout(CLAUDE_PROFILE_TIMEOUT),
298302
Effect.orElseSucceed(() => CLAUDE_HUB_PLAN),
299303
),
300304
],

0 commit comments

Comments
 (0)