Skip to content

Commit 50b8308

Browse files
committed
fix review edge cases for usage pools
1 parent 6a1eca2 commit 50b8308

6 files changed

Lines changed: 160 additions & 10 deletions

File tree

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

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -569,7 +569,7 @@ const probeCodexAppServerProvider = Effect.fn("probeCodexAppServerProvider")(fun
569569
),
570570
// Usage is an enrichment: a failure or a slow answer degrades to "no
571571
// usage this probe" rather than costing the account and models.
572-
input.skipNativeUsage
572+
input.skipNativeUsage || !accountResponse.requiresOpenaiAuth
573573
? Effect.succeed(undefined)
574574
: withCodexDiscoveryDeadline(
575575
client.request("account/rateLimits/read", null),
@@ -717,7 +717,8 @@ export const checkCodexProviderStatus = Effect.fn("checkCodexProviderStatus")(fu
717717
const checkedAt = DateTime.formatIso(yield* DateTime.now);
718718
const emptyModels = emptyCodexModelsFromSettings(codexSettings);
719719
const launchArgs = resolveCodexLaunchArgs(codexSettings.launchArgs, resolvedEnvironment);
720-
const skipNativeUsage = managedAuth !== undefined || codexUsageProbeDisabled(launchArgs);
720+
const skipNativeUsageBeforeProbe =
721+
managedAuth !== undefined || codexUsageProbeDisabled(launchArgs);
721722

722723
if (!codexSettings.enabled) {
723724
return buildServerProvider({
@@ -742,7 +743,7 @@ export const checkCodexProviderStatus = Effect.fn("checkCodexProviderStatus")(fu
742743
launchArgs,
743744
cwd: process.cwd(),
744745
environment: resolvedEnvironment,
745-
...(skipNativeUsage ? { skipNativeUsage: true } : {}),
746+
...(skipNativeUsageBeforeProbe ? { skipNativeUsage: true } : {}),
746747
}).pipe(
747748
// Process teardown can outlast the work budget without discarding a completed snapshot.
748749
Effect.timeoutOption(Duration.millis(CODEX_AUTH_PROBE_TIMEOUT_MS)),
@@ -792,6 +793,7 @@ export const checkCodexProviderStatus = Effect.fn("checkCodexProviderStatus")(fu
792793
}
793794

794795
const snapshot = probeResult.success.value;
796+
const skipNativeUsage = skipNativeUsageBeforeProbe || !snapshot.account.requiresOpenaiAuth;
795797
const accountStatus = managedAuth
796798
? { status: "ready" as const, auth: managedAuth, message: undefined }
797799
: accountProbeStatus(snapshot);

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

Lines changed: 31 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -382,7 +382,8 @@ it.layer(
382382
let skipNativeUsage: boolean | undefined;
383383
const settings: CodexSettings = {
384384
...defaultCodexSettings,
385-
launchArgs: "-c model_providers.local_pool.requires_openai_auth=false",
385+
launchArgs:
386+
"-c model_provider=local_pool -c model_providers.local_pool.requires_openai_auth=false",
386387
};
387388
const status = yield* checkCodexProviderStatus(settings, (input) => {
388389
skipNativeUsage = input.skipNativeUsage;
@@ -394,6 +395,35 @@ it.layer(
394395
}),
395396
);
396397

398+
it.effect("does not skip native usage for an unselected external provider", () =>
399+
Effect.gen(function* () {
400+
let skipNativeUsage: boolean | undefined;
401+
const settings: CodexSettings = {
402+
...defaultCodexSettings,
403+
launchArgs:
404+
"-c model_provider=openai -c model_providers.local_pool.requires_openai_auth=false",
405+
};
406+
const status = yield* checkCodexProviderStatus(settings, (input) => {
407+
skipNativeUsage = input.skipNativeUsage;
408+
return Effect.succeed(
409+
makeCodexProbeSnapshot({
410+
account: {
411+
account: {
412+
type: "chatgpt",
413+
414+
planType: "pro",
415+
},
416+
requiresOpenaiAuth: true,
417+
},
418+
}),
419+
);
420+
});
421+
422+
assert.notStrictEqual(skipNativeUsage, true);
423+
assert.isDefined(status.usageLimits);
424+
}),
425+
);
426+
397427
it.effect("returns unauthenticated when app-server requires OpenAI auth", () =>
398428
Effect.gen(function* () {
399429
const status = yield* checkCodexProviderStatus(defaultCodexSettings, () =>

‎apps/server/src/provider/codexLaunchArgs.test.ts‎

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,12 +36,21 @@ describe("codexUsageProbeDisabled", () => {
3636
it("recognizes an external provider that does not use OpenAI auth", () => {
3737
NodeAssert.equal(
3838
codexUsageProbeDisabled(
39-
"-c model_providers.local_pool.requires_openai_auth=false --strict-config",
39+
"-c model_provider=local_pool -c model_providers.local_pool.requires_openai_auth=false --strict-config",
4040
),
4141
true,
4242
);
4343
});
4444

45+
it("ignores auth settings for providers that are not selected", () => {
46+
NodeAssert.equal(
47+
codexUsageProbeDisabled(
48+
"-c model_provider=openai -c model_providers.local_pool.requires_openai_auth=false",
49+
),
50+
false,
51+
);
52+
});
53+
4554
it("keeps native usage probing enabled by default", () => {
4655
NodeAssert.equal(codexUsageProbeDisabled('-c model="gpt-6.1-sol"'), false);
4756
});

‎apps/server/src/provider/codexLaunchArgs.ts‎

Lines changed: 21 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -24,16 +24,33 @@ function configValue(
2424
return undefined;
2525
}
2626

27+
function configAssignment(value: string | undefined): readonly [string, string] | undefined {
28+
if (!value) return undefined;
29+
const separator = value.indexOf("=");
30+
if (separator < 0) return undefined;
31+
const key = value.slice(0, separator).trim();
32+
const assignment = value.slice(separator + 1).trim();
33+
if (!key || !assignment) return undefined;
34+
return [key, assignment.replace(/^(['"])(.*)\1$/, "$2")];
35+
}
36+
2737
/**
2838
* External model providers do not implement Codex's OpenAI account quota RPC.
2939
* A provider can opt out explicitly with `requires_openai_auth=false`.
3040
*/
3141
export const codexUsageProbeDisabled = (launchArgs?: string): boolean => {
3242
const args = codexAppServerArgs(launchArgs);
33-
return args.some((argument, index) => {
34-
const value = configValue(argument, args[index + 1]);
35-
return /^model_providers\.[^.]+\.requires_openai_auth\s*=\s*false$/i.test(value ?? "");
36-
});
43+
const assignments = new Map<string, string>();
44+
for (let index = 0; index < args.length; index++) {
45+
const value = configValue(args[index], args[index + 1]);
46+
const assignment = configAssignment(value);
47+
if (assignment) assignments.set(assignment[0], assignment[1]);
48+
}
49+
const selectedProvider = assignments.get("model_provider") ?? "openai";
50+
return (
51+
assignments.get(`model_providers.${selectedProvider}.requires_openai_auth`)?.toLowerCase() ===
52+
"false"
53+
);
3754
};
3855

3956
export const codexExecLaunchArgs = (launchArgs?: string) => {

‎packages/shared/src/usageLimits.test.ts‎

Lines changed: 85 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -164,6 +164,91 @@ describe("pools", () => {
164164
});
165165
});
166166

167+
it("keeps a workspace-less hub credential apart from a different workspace credential", () => {
168+
const accounts = [
169+
{
170+
id: "workspace.json",
171+
driver: claude,
172+
173+
workspaceId: "workspace-a",
174+
usageLimits: {
175+
checkedAt,
176+
windows: [{ ...window, usedPercent: 20 }],
177+
},
178+
},
179+
{
180+
id: "personal.json",
181+
driver: claude,
182+
183+
usageLimits: {
184+
checkedAt,
185+
windows: [{ ...window, usedPercent: 70 }],
186+
},
187+
},
188+
];
189+
const input = new Map([
190+
[
191+
EnvironmentId.make("env-a"),
192+
{
193+
...laptop,
194+
serverConfig: { providers: [], usageLimitSources: [{ ...source, accounts }] },
195+
},
196+
],
197+
]);
198+
199+
const limitAccounts = collectLimitAccounts(input);
200+
expect(limitAccounts.map((account) => account.key)).toEqual([
201+
"hub:workspace.json:workspace-a",
202+
"hub:personal.json",
203+
]);
204+
expect(limitAccounts.map((account) => account.limits.windows[0]?.usedPercent)).toEqual([
205+
20, 70,
206+
]);
207+
});
208+
209+
it("deduplicates native reports without joining source credentials", () => {
210+
const native = provider({
211+
driver: claude,
212+
instanceId: ProviderInstanceId.make("claude"),
213+
auth: { status: "authenticated", email: "[email protected]" },
214+
usageLimits: { checkedAt, windows: [{ ...window, usedPercent: 30 }] },
215+
});
216+
const accounts = ["first.json", "second.json"].map((id, index) => ({
217+
id,
218+
driver: claude,
219+
220+
usageLimits: { checkedAt, windows: [{ ...window, usedPercent: index === 0 ? 10 : 90 }] },
221+
}));
222+
const input = new Map([
223+
[
224+
EnvironmentId.make("env-a"),
225+
{
226+
...laptop,
227+
serverConfig: { providers: [native], usageLimitSources: [{ ...source, accounts }] },
228+
},
229+
],
230+
[
231+
EnvironmentId.make("env-b"),
232+
{
233+
entry: { target: { label: "Desktop" } },
234+
serverConfig: {
235+
providers: [{ ...native, instanceId: ProviderInstanceId.make("codex-2") }],
236+
},
237+
},
238+
],
239+
]);
240+
241+
expect(collectLimitAccounts(input).map((account) => account.key)).toEqual([
242+
"env-a:claude",
243+
"hub:first.json",
244+
"hub:second.json",
245+
]);
246+
expect(collectLimitAccounts(input)[0]?.environments).toEqual([
247+
{ environmentId: "env-a", label: "Laptop" },
248+
{ environmentId: "env-b", label: "Desktop" },
249+
]);
250+
});
251+
167252
it("merges OpenCode Go limits from machines with the same API key", () => {
168253
const go = provider({
169254
driver: ProviderDriverKind.make("opencode"),

‎packages/shared/src/usageLimits.ts‎

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -192,9 +192,16 @@ function accountKeyResolver(reports: Iterable<AccountReport>) {
192192
}
193193
// Multiple source credentials share this email but are distinct accounts. Keep a
194194
// native provider without a workspace separate instead of attaching it to all of them.
195-
if (!sourceIdentity && (sourceAccounts.get(key)?.size ?? 0) > 1) return null;
195+
if (!sourceIdentity && (sourceAccounts.get(key)?.size ?? 0) > 1) return `${key}:native`;
196196
const known = workspaces.get(key);
197197
if (!known) return key;
198+
if (sourceIdentity && known.size === 1) {
199+
const workspaceId = [...known][0];
200+
const workspaceSources = sourceWorkspaceAccounts.get(`${key}:${workspaceId}`);
201+
if (workspaceSources && !workspaceSources.has(sourceIdentity)) {
202+
return `${key}:source:${sourceIdentity}`;
203+
}
204+
}
198205
return known.size === 1 ? `${key}:${[...known][0]}` : null;
199206
};
200207
}

0 commit comments

Comments
 (0)