Skip to content

Commit 755c9b0

Browse files
committed
fix(usage): identify Codex quotas by workspace account
1 parent 3e6f856 commit 755c9b0

9 files changed

Lines changed: 515 additions & 45 deletions

File tree

‎apps/server/src/provider/Layers/CodexProvider.test.ts‎

Lines changed: 80 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,85 @@
11
import { assert, it } from "@effect/vitest";
2+
import * as NodeFSP from "node:fs/promises";
3+
import * as NodeOS from "node:os";
4+
import * as NodePath from "node:path";
5+
import * as Effect from "effect/Effect";
26

3-
import { applyPreferredCodexDefaultModel, mapCodexModelCapabilities } from "./CodexProvider.ts";
7+
import {
8+
applyPreferredCodexDefaultModel,
9+
mapCodexModelCapabilities,
10+
readCodexAccountId,
11+
resolveCodexAuthHome,
12+
} from "./CodexProvider.ts";
13+
14+
it("resolves the selected or shadow home ahead of inherited credentials", () => {
15+
const inherited = { CODEX_HOME: "/shared", HOME: "/user" };
16+
assert.strictEqual(
17+
resolveCodexAuthHome(
18+
{ homePath: "/shadow", environment: { CODEX_HOME: "/instance" } },
19+
inherited,
20+
),
21+
"/shadow",
22+
);
23+
assert.strictEqual(
24+
resolveCodexAuthHome({ environment: { CODEX_HOME: "/instance" } }, inherited),
25+
"/instance",
26+
);
27+
assert.strictEqual(resolveCodexAuthHome({}, inherited), "/shared");
28+
assert.strictEqual(
29+
resolveCodexAuthHome({ environment: { HOME: "/instance-user" } }, { HOME: "/user" }),
30+
"/instance-user/.codex",
31+
);
32+
assert.strictEqual(resolveCodexAuthHome({}, { HOME: "/user" }), "/user/.codex");
33+
});
34+
35+
it.effect(
36+
"reads workspace identity from the selected home without treating API keys as subscriptions",
37+
() =>
38+
Effect.acquireUseRelease(
39+
Effect.promise(() =>
40+
NodeFSP.mkdtemp(NodePath.join(NodeOS.tmpdir(), "codex-account-identity-")),
41+
),
42+
(root) =>
43+
Effect.gen(function* () {
44+
for (const accountId of ["personal", "work"]) {
45+
const home = NodePath.join(root, accountId);
46+
yield* Effect.promise(() => NodeFSP.mkdir(home));
47+
yield* Effect.promise(() =>
48+
NodeFSP.writeFile(
49+
NodePath.join(home, "auth.json"),
50+
JSON.stringify({ tokens: { account_id: accountId } }),
51+
),
52+
);
53+
assert.strictEqual(yield* readCodexAccountId(home), accountId);
54+
}
55+
yield* Effect.promise(() =>
56+
NodeFSP.writeFile(
57+
NodePath.join(root, "auth.json"),
58+
JSON.stringify({ OPENAI_API_KEY: "local-proxy" }),
59+
),
60+
);
61+
assert.strictEqual(yield* readCodexAccountId(root), undefined);
62+
assert.strictEqual(yield* readCodexAccountId(NodePath.join(root, "missing")), undefined);
63+
const payload = Buffer.from(
64+
JSON.stringify({
65+
"https://api.openai.com/auth": { chatgpt_account_id: "token-workspace" },
66+
}),
67+
).toString("base64url");
68+
for (const [tokens, expected] of [
69+
[{ id_token: `header.${payload}.signature` }, "token-workspace"],
70+
[{ account_id: null, id_token: `header.${payload}.signature` }, "token-workspace"],
71+
[{ account_id: "explicit", id_token: `header.${payload}.signature` }, "explicit"],
72+
[{ id_token: "malformed" }, undefined],
73+
] as const) {
74+
yield* Effect.promise(() =>
75+
NodeFSP.writeFile(NodePath.join(root, "auth.json"), JSON.stringify({ tokens })),
76+
);
77+
assert.strictEqual(yield* readCodexAccountId(root), expected);
78+
}
79+
}),
80+
(root) => Effect.promise(() => NodeFSP.rm(root, { recursive: true, force: true })),
81+
),
82+
);
483

584
it("maps current Codex model capability fields", () => {
685
const capabilities = mapCodexModelCapabilities({

‎apps/server/src/provider/Layers/CodexProvider.ts‎

Lines changed: 77 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,6 @@
1+
import * as NodeFSP from "node:fs/promises";
2+
import * as NodeOS from "node:os";
3+
import * as NodePath from "node:path";
14
import * as DateTime from "effect/DateTime";
25
import * as Duration from "effect/Duration";
36
import * as Effect from "effect/Effect";
@@ -67,6 +70,7 @@ const CODEX_PRESENTATION = {
6770

6871
export interface CodexAppServerProviderSnapshot {
6972
readonly account: CodexSchema.V2GetAccountResponse;
73+
readonly accountId?: string;
7074
readonly rateLimits?: CodexRateLimitsProbe;
7175
readonly version: string | undefined;
7276
readonly models: ReadonlyArray<ServerProviderModel>;
@@ -404,6 +408,65 @@ export const withCodexAppServerClient = Effect.fn("withCodexAppServerClient")(fu
404408
return { client, initialize };
405409
});
406410

411+
const decodeCodexAccountIdentity = Schema.decodeUnknownEffect(
412+
Schema.fromJsonString(
413+
Schema.Struct({
414+
tokens: Schema.Struct({
415+
account_id: Schema.optionalKey(Schema.NullOr(Schema.String)),
416+
id_token: Schema.optionalKey(Schema.NullOr(Schema.String)),
417+
}),
418+
}),
419+
),
420+
);
421+
422+
const decodeCodexIdTokenIdentity = Schema.decodeUnknownEffect(
423+
Schema.fromJsonString(
424+
Schema.Struct({
425+
"https://api.openai.com/auth": Schema.Struct({
426+
chatgpt_account_id: Schema.optionalKey(Schema.NullOr(Schema.String)),
427+
}),
428+
}),
429+
),
430+
);
431+
432+
export function resolveCodexAuthHome(
433+
input: {
434+
readonly homePath?: string;
435+
readonly environment?: NodeJS.ProcessEnv;
436+
},
437+
inheritedEnvironment: NodeJS.ProcessEnv = process.env,
438+
) {
439+
return expandHomePath(
440+
input.homePath?.trim() ||
441+
input.environment?.CODEX_HOME ||
442+
inheritedEnvironment.CODEX_HOME ||
443+
NodePath.join(
444+
input.environment?.HOME || inheritedEnvironment.HOME || NodeOS.homedir(),
445+
".codex",
446+
),
447+
);
448+
}
449+
450+
export const readCodexAccountId = Effect.fn("readCodexAccountId")(function* (homePath: string) {
451+
return yield* Effect.tryPromise(() =>
452+
NodeFSP.readFile(NodePath.join(homePath, "auth.json"), "utf8"),
453+
).pipe(
454+
Effect.flatMap(decodeCodexAccountIdentity),
455+
Effect.flatMap((auth) => {
456+
const accountId = auth.tokens.account_id?.trim();
457+
if (accountId) return Effect.succeed(accountId);
458+
const payload = auth.tokens.id_token?.split(".")[1];
459+
if (!payload) return Effect.succeed(undefined);
460+
return decodeCodexIdTokenIdentity(Buffer.from(payload, "base64url").toString("utf8")).pipe(
461+
Effect.map(
462+
(claims) => claims["https://api.openai.com/auth"].chatgpt_account_id?.trim() || undefined,
463+
),
464+
);
465+
}),
466+
Effect.orElseSucceed(() => undefined),
467+
);
468+
});
469+
407470
const probeCodexAppServerProvider = Effect.fn("probeCodexAppServerProvider")(function* (input: {
408471
readonly binaryPath: string;
409472
readonly homePath?: string;
@@ -419,6 +482,12 @@ const probeCodexAppServerProvider = Effect.fn("probeCodexAppServerProvider")(fun
419482
const version = versionMatch ? versionMatch[1] : undefined;
420483

421484
const accountResponse = yield* client.request("account/read", {});
485+
// account/read omits workspace identity. File-backed logins keep it beside
486+
// the credentials in the same home used by this app-server instance.
487+
const accountId =
488+
accountResponse.account?.type === "chatgpt"
489+
? yield* readCodexAccountId(resolveCodexAuthHome(input))
490+
: undefined;
422491
if (!accountResponse.account && accountResponse.requiresOpenaiAuth) {
423492
return {
424493
account: accountResponse,
@@ -460,6 +529,7 @@ const probeCodexAppServerProvider = Effect.fn("probeCodexAppServerProvider")(fun
460529

461530
return {
462531
account: accountResponse,
532+
...(accountId ? { accountId } : {}),
463533
rateLimits,
464534
version,
465535
models: applyPreferredCodexDefaultModel(
@@ -524,7 +594,10 @@ const makePendingCodexProvider = (
524594
});
525595
});
526596

527-
function accountProbeStatus(account: CodexAppServerProviderSnapshot["account"]): {
597+
function accountProbeStatus(
598+
account: CodexAppServerProviderSnapshot["account"],
599+
accountId?: string,
600+
): {
528601
readonly status: Exclude<ServerProviderState, "disabled">;
529602
readonly auth: ServerProvider["auth"];
530603
readonly message?: string;
@@ -536,6 +609,7 @@ function accountProbeStatus(account: CodexAppServerProviderSnapshot["account"]):
536609
...(account.account?.type ? { type: account.account?.type } : {}),
537610
...(authLabel ? { label: authLabel } : {}),
538611
...(authEmail ? { email: authEmail } : {}),
612+
...(accountId ? { accountId } : {}),
539613
} satisfies ServerProvider["auth"];
540614

541615
if (account.account) {
@@ -646,9 +720,9 @@ export const checkCodexProviderStatus = Effect.fn("checkCodexProviderStatus")(fu
646720
}
647721

648722
const snapshot = probeResult.success.value;
649-
const accountStatus = accountProbeStatus(snapshot.account);
723+
const accountStatus = accountProbeStatus(snapshot.account, snapshot.accountId);
650724
const usageLimits =
651-
snapshot.account.account?.type === "apiKey"
725+
snapshot.account.account?.type === "apiKey" || !snapshot.account.requiresOpenaiAuth
652726
? makeUnavailableUsageLimits({ checkedAt, reason: "unsupported" })
653727
: snapshot.rateLimits === undefined || "failure" in snapshot.rateLimits
654728
? makeUnavailableUsageLimits({

‎apps/server/src/provider/Layers/ProviderRegistry.test.ts‎

Lines changed: 37 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -299,7 +299,7 @@ function makeCodexProbeSnapshot(
299299
300300
planType: "pro",
301301
},
302-
requiresOpenaiAuth: false,
302+
requiresOpenaiAuth: true,
303303
},
304304
models: [
305305
{
@@ -365,11 +365,44 @@ it.layer(Layer.mergeAll(NodeServices.layer, ServerSettingsModule.layerTest(), Te
365365
"ProviderRegistry",
366366
(it) => {
367367
describe("checkCodexProviderStatus", () => {
368+
it.effect("does not count proxy rate limits as a native subscription", () =>
369+
Effect.gen(function* () {
370+
for (const account of [
371+
null,
372+
{ type: "chatgpt" as const, email: "[email protected]", planType: "pro" as const },
373+
]) {
374+
const status = yield* checkCodexProviderStatus(defaultCodexSettings, () =>
375+
Effect.succeed(
376+
makeCodexProbeSnapshot({
377+
account: { account, requiresOpenaiAuth: false },
378+
rateLimits: {
379+
snapshot: {
380+
primary: { usedPercent: 90, windowDurationMins: 10080, resetsAt: 1789436313 },
381+
},
382+
rateLimitsByLimitId: null,
383+
resetCredits: null,
384+
},
385+
}),
386+
),
387+
);
388+
assert.strictEqual(status.usageLimits?.unavailable?.reason, "unsupported");
389+
assert.deepStrictEqual(status.usageLimits?.windows, []);
390+
}
391+
}),
392+
);
368393
it.effect("uses the app-server account and model list for provider status", () =>
369394
Effect.gen(function* () {
370395
const status = yield* checkCodexProviderStatus(defaultCodexSettings, () =>
371396
Effect.succeed(
372397
makeCodexProbeSnapshot({
398+
accountId: "workspace-a",
399+
rateLimits: {
400+
snapshot: {
401+
primary: { usedPercent: 42, windowDurationMins: 300, resetsAt: 1789436313 },
402+
},
403+
rateLimitsByLimitId: null,
404+
resetCredits: null,
405+
},
373406
skills: [
374407
{
375408
name: "github:gh-fix-ci",
@@ -389,6 +422,9 @@ it.layer(Layer.mergeAll(NodeServices.layer, ServerSettingsModule.layerTest(), Te
389422
assert.strictEqual(status.auth.type, "chatgpt");
390423
assert.strictEqual(status.auth.label, "ChatGPT Pro 20x Subscription");
391424
assert.strictEqual(status.auth.email, "[email protected]");
425+
assert.strictEqual(status.auth.accountId, "workspace-a");
426+
assert.strictEqual(status.usageLimits?.unavailable, undefined);
427+
assert.strictEqual(status.usageLimits?.windows[0]?.usedPercent, 42);
392428
assert.deepStrictEqual(status.models, [
393429
{
394430
slug: "gpt-live-codex",

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

Lines changed: 89 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,12 @@ const encodeJson = Schema.encodeSync(Schema.fromJsonString(Schema.Unknown));
4747

4848
function fixture(
4949
options: {
50-
accounts?: Array<(typeof accounts)[number] & { disabled?: boolean }>;
50+
accounts?: Array<
51+
Omit<(typeof accounts)[number], "id_token"> & {
52+
disabled?: boolean;
53+
id_token?: { chatgpt_account_id?: string; plan_type?: string; chatgpt_plan_type?: string };
54+
}
55+
>;
5156
upstream?: (request: RequestBody) => { status: number; body: unknown };
5257
cooldownStatus?: number;
5358
} = {},
@@ -119,6 +124,7 @@ describe("CLIProxyAPI built-in management API", () => {
119124
const test = fixture();
120125
const api = yield* test.api;
121126
const result = yield* api.readAccounts(config);
127+
expect(result.map((account) => account.accountId)).toEqual(["account-a", "account-b"]);
122128
expect(result.map((account) => account.usageLimits.resetCredits)).toEqual([
123129
{ availableCount: 2, nextCreditId: "first", nextExpiresAt: "2099-01-01T00:00:00.000Z" },
124130
{ availableCount: 2, nextCreditId: "first", nextExpiresAt: "2099-01-01T00:00:00.000Z" },
@@ -141,6 +147,88 @@ describe("CLIProxyAPI built-in management API", () => {
141147
}),
142148
);
143149

150+
it.effect("skips blank plan fields when falling back to account metadata", () =>
151+
Effect.gen(function* () {
152+
const test = fixture({
153+
accounts: accounts.map((account, index) => ({
154+
...account,
155+
id_token: {
156+
...account.id_token,
157+
plan_type: index === 0 ? " pro " : " ",
158+
chatgpt_plan_type: "business",
159+
},
160+
})),
161+
upstream: () => ({
162+
status: 200,
163+
body: { plan_type: " ", rate_limit: { primary_window: { used_percent: 12 } } },
164+
}),
165+
});
166+
const api = yield* test.api;
167+
const result = yield* api.readAccounts(config);
168+
expect(result.map((account) => account.plan)).toEqual([
169+
"ChatGPT Pro 20x Subscription",
170+
"ChatGPT Business Subscription",
171+
]);
172+
}),
173+
);
174+
175+
it.effect("uses fallback Free and Go plans for monthly quota windows", () =>
176+
Effect.gen(function* () {
177+
const test = fixture({
178+
accounts: accounts.map((account, index) => ({
179+
...account,
180+
id_token: {
181+
...account.id_token,
182+
plan_type: index === 0 ? " free " : " ",
183+
chatgpt_plan_type: " go ",
184+
},
185+
})),
186+
upstream: () => ({
187+
status: 200,
188+
body: { plan_type: " ", rate_limit: { primary_window: { used_percent: 12 } } },
189+
}),
190+
});
191+
const api = yield* test.api;
192+
const result = yield* api.readAccounts(config);
193+
expect(result.map((account) => account.plan)).toEqual([
194+
"ChatGPT Free Subscription",
195+
"ChatGPT Go Subscription",
196+
]);
197+
for (const account of result) {
198+
expect(account.usageLimits.windows).toMatchObject([
199+
{ kind: "monthly", windowDurationMins: 43200, usedPercent: 12 },
200+
]);
201+
}
202+
}),
203+
);
204+
205+
it.effect("uses trimmed account IDs for quota headers and omits empty IDs", () =>
206+
Effect.gen(function* () {
207+
const ids = [" account-a ", "", " ", undefined];
208+
const test = fixture({
209+
accounts: ids.map((accountId, index) => ({
210+
...accounts[0]!,
211+
id: `${index}.json`,
212+
auth_index: String(index),
213+
id_token: accountId === undefined ? {} : { chatgpt_account_id: accountId },
214+
})),
215+
});
216+
const api = yield* test.api;
217+
const result = yield* api.readAccounts(config);
218+
expect(result.map((account) => account.accountId)).toEqual([
219+
"account-a",
220+
undefined,
221+
undefined,
222+
undefined,
223+
]);
224+
for (const request of test.requests.filter((request) => request.body?.url)) {
225+
expect(request.body?.header?.["Chatgpt-Account-Id"]).toBe(
226+
request.body?.auth_index === "0" ? "account-a" : undefined,
227+
);
228+
}
229+
}),
230+
);
231+
144232
it.effect("keeps usage when the credits endpoint fails", () =>
145233
Effect.gen(function* () {
146234
const test = fixture({

0 commit comments

Comments
 (0)