diff --git a/pm-stack/aggregator/scripts/gather-quota.ts b/pm-stack/aggregator/scripts/gather-quota.ts index d9a010fa18..adf7347b7c 100755 --- a/pm-stack/aggregator/scripts/gather-quota.ts +++ b/pm-stack/aggregator/scripts/gather-quota.ts @@ -387,7 +387,13 @@ function finalizeRun(id: string, entries: QuotaCurrentEntry[], t0: number): Prov state: anyOk ? "ok" : onlyLapsed ? "lapsed" : "error", ms: Date.now() - t0, entries, - ...(!anyOk && !onlyLapsed && firstFailure?.error ? { error: firstFailure.error } : {}), + // A partially-failed run still surfaces its first failure: a + // folded provider (codex answers one row per OpenAI credential) + // must not let one credential's ok row hide a sibling credential's + // error from the heartbeat — 2026-11-10 the codex run read "ok" + // with no error while the openai.main and _openai credentials were + // both failing token_expired at the provider. + ...(firstFailure?.error ? { error: firstFailure.error } : {}), }; } diff --git a/pm-stack/aggregator/src/quota-gather-map.ts b/pm-stack/aggregator/src/quota-gather-map.ts index 6297b77c4b..ce5fc1db79 100644 --- a/pm-stack/aggregator/src/quota-gather-map.ts +++ b/pm-stack/aggregator/src/quota-gather-map.ts @@ -162,6 +162,41 @@ export const ANTIGRAVITY_CLAUDE_GPT_SERVICE_ID = "_antigravity.claude-gpt"; export const ANTIGRAVITY_LEGACY_GEMINI_SERVICE_ID = "antigravity-direct.gemini"; export const ANTIGRAVITY_LEGACY_CLAUDE_GPT_SERVICE_ID = "antigravity-direct.claude-gpt"; +export const OPENAI_MAIN_SERVICE_ID = "openai.main"; +export const OPENAI_ALTERNATE_SERVICE_ID = "openai.alternate"; +export const OPENAI_SELF_SERVICE_ID = "_openai"; + +/** + * Which OpenAI pool a folded `codex` entry belongs to, from the + * credential's human label / the instance name the fork stamps on the + * folded result. The fork's codex provider folds every OpenAI instance + * credential (openai.main, openai.alternate, _openai) into its + * per-credential answer under the bare provider id "codex" + * (foldInstanceCredentials), and serviceIdFor historically filed ALL + * of those rows under "openai.main" — so the alternate account's 5h + * window landed in the snapshot store as openai.main|5h (its values + * mirror openai.alternate|5h tick-for-tick in usage-2026-11.jsonl) + * and its weekly competed under main's key in the #1624 max-collapse + * (2026-11-04: main|weekly published 100 with the ALTERNATE window's + * reset label while main's own read was ~17). Like goPoolServiceId, + * the label is the only stable gatherer-side signal: instance ids are + * config names, credential ids are opaque row ids the mapping would + * have to hardcode. Unmatched entries yield null and keep the + * historical fallback (serviceIdFor: openai.main, the base provider's + * own pool) — never a fabricated pool row. + */ +export function codexPoolServiceId(entry: { + credentialLabel?: string | null; + providerName?: string | null; +}): string | null { + const text = `${entry.credentialLabel ?? ""} ${entry.providerName ?? ""}`.toLowerCase(); + if (text.includes("alternate")) return OPENAI_ALTERNATE_SERVICE_ID; + if (text.includes("_openai") || text.includes("self-contained") || text.includes("self contained")) + return OPENAI_SELF_SERVICE_ID; + if (text.includes("main")) return OPENAI_MAIN_SERVICE_ID; + return null; +} + /** * Which split Go pool a per-workspace entry belongs to, from the * credential's human label. OpenCode credential labels are user-set; on @@ -427,10 +462,19 @@ export function entryFromResult(result: QuotaProviderResult, tickIso: string): Q ? "error" : "unconfigured"; const providerId = canonicalProviderId(result.providerId); + // Folded codex rows carry one credential per OpenAI instance; file + // each under its own pool (codexPoolServiceId) instead of the base + // provider's pool, or a sibling account's windows contaminate + // openai.main's rows and snapshot series. + const serviceId = + providerId === "codex" + ? (codexPoolServiceId({ credentialLabel: result.credentialLabel, providerName: result.providerName }) ?? + serviceIdFor(providerId)) + : serviceIdFor(providerId); const entry: QuotaCurrentEntry = { providerId, providerName: result.providerName ?? providerId, - serviceId: serviceIdFor(providerId), + serviceId, state, measuredAt, source: GATHERER_SOURCE, diff --git a/pm-stack/aggregator/test/quota-gather-map.test.ts b/pm-stack/aggregator/test/quota-gather-map.test.ts index ee607219a0..a407147ac6 100644 --- a/pm-stack/aggregator/test/quota-gather-map.test.ts +++ b/pm-stack/aggregator/test/quota-gather-map.test.ts @@ -4,11 +4,15 @@ import { ANTIGRAVITY_GEMINI_SERVICE_ID, GO_POPESDREW_SERVICE_ID, GO_DREWRYPOPE_SERVICE_ID, + OPENAI_ALTERNATE_SERVICE_ID, + OPENAI_MAIN_SERVICE_ID, + OPENAI_SELF_SERVICE_ID, canonicalProviderId, entriesWithPoolSplits, entryFromFileExport, entryFromTurso, entryFromResult, + codexPoolServiceId, goPoolServiceId, lastKnownEntries, mapWithConcurrency, @@ -1228,3 +1232,86 @@ describe("canonicalProviderId (config-id alignment, #1823)", () => { expect(retained[0].retainedFromFailedTick).toBe("2026-10-27T06:00:00.000Z"); }); }); + +describe("codexPoolServiceId — folded codex rows file under their own OpenAI pool", () => { + const TICK = "2026-11-10T21:30:00.000Z"; + const win = (usedPercent: number, windowSeconds: number) => ({ + usedPercent, + remainingPercent: 100 - usedPercent, + windowSeconds, + resetAt: 1794364348000, + }); + const codexResult = ( + credentialLabel: string | undefined, + providerName: string, + windows: Record>, + ): QuotaProviderResult => ({ + providerId: "codex", + providerName, + ok: true, + configured: true, + fetchedAt: Date.parse(TICK), + credentialId: "cred_x", + credentialLabel, + usage: { windows }, + }); + + test("each instance credential lands on its own serviceId", () => { + const alt = entryFromResult( + codexResult("OpenAI Alternate (Student - popesdrew)", "OpenAI Alternate (Student - popesdrew)", { + "5h": win(42, 18_000), + weekly: win(65, 604_800), + }), + TICK, + ); + expect(alt.serviceId).toBe(OPENAI_ALTERNATE_SERVICE_ID); + const main = entryFromResult( + codexResult("OpenAI Main (drewrypope)", "OpenAI Main (drewrypope)", { weekly: win(100, 604_800) }), + TICK, + ); + expect(main.serviceId).toBe(OPENAI_MAIN_SERVICE_ID); + const self = entryFromResult( + codexResult("OpenAI (_openai, drewrypope) — self-contained", "OpenAI Self-Contained (drewrypope, no Codex)", { + weekly: win(100, 604_800), + }), + TICK, + ); + expect(self.serviceId).toBe(OPENAI_SELF_SERVICE_ID); + }); + + test("unmatched folded rows keep the historical openai.main fallback; codexPoolServiceId is null", () => { + expect(codexPoolServiceId({ credentialLabel: undefined, providerName: "Codex" })).toBeNull(); + expect(codexPoolServiceId({ credentialLabel: "some future account", providerName: "Codex" })).toBeNull(); + const bare = entryFromResult(codexResult(undefined, "Codex", { weekly: win(10, 604_800) }), TICK); + expect(bare.serviceId).toBe("openai.main"); + }); + + test("recordsFromEntries: the alternate account's 5h never enters openai.main's series", () => { + const alt = entryFromResult( + codexResult("OpenAI Alternate (Student - popesdrew)", "OpenAI Alternate (Student - popesdrew)", { + "5h": win(42, 18_000), + weekly: win(65, 604_800), + }), + TICK, + ); + const mainDedicated = entryFromResult( + { + providerId: "openai.main", + providerName: "OpenAI Main (drewrypope)", + ok: true, + configured: true, + fetchedAt: Date.parse(TICK), + credentialId: "cred_main", + credentialLabel: "OpenAI Main (drewrypope)", + usage: { windows: { weekly: win(100, 604_800) } }, + }, + TICK, + ); + const { records } = recordsFromEntries([alt, mainDedicated], new Map(), "quota-gatherer"); + const byKey = new Map(records.map((r) => [`${r.serviceId}|${r.metricId}`, r.value])); + expect(byKey.get("openai.alternate|5h")).toBe(42); + expect(byKey.has("openai.main|5h")).toBe(false); + expect(byKey.get("openai.main|weekly")).toBe(100); + expect(byKey.get("openai.alternate|weekly")).toBe(65); + }); +});