diff --git a/pm-stack/aggregator/src/quota-current.ts b/pm-stack/aggregator/src/quota-current.ts index 7f084ed620..c382d6fe5d 100644 --- a/pm-stack/aggregator/src/quota-current.ts +++ b/pm-stack/aggregator/src/quota-current.ts @@ -211,21 +211,69 @@ function pctWindows(entry: QuotaCurrentEntry): QuotaWindow[] { })); } -function applyEntry(pool: QuotaPool, entry: QuotaCurrentEntry, state: FreshState): boolean { +/** + * The gatherer leg error for an entry's provider, from the tick's + * heartbeat (#6942): a retained entry is preserved verbatim with no + * error field (markRetained in quota-gather-map.ts), so when the leg + * that produced it errored, the heartbeat's perProvider record — + * keyed by run id, matched here on the entry's providerId with its + * serviceId as fallback — is the only place the error text survives. + */ +function legErrorFor(file: QuotaCurrentFile, entry: QuotaCurrentEntry): string | undefined { + const perProvider = file.heartbeat?.perProvider; + if (!perProvider) return undefined; + const leg = perProvider[entry.providerId] ?? perProvider[entry.serviceId]; + return leg && leg.state === "error" && leg.error ? safeProviderReason(leg.error) : undefined; +} + +/** + * #6942: a pool with no windows of its own (a shell pool — its dated + * windows exist only in the read entry) takes the entry's dated + * windows on the stale/error path, percentages identical to the + * read. A pool carrying its own dated windows is untouched: those + * win over the gatherer entry's, as pinned for spec pools. The + * shell pool's "no read" note is written against a read that never + * landed; when the entry does carry a dated read, the note + * contradicts the read and is suppressed. + */ +function adoptEntryWindows(pool: QuotaPool, entry: QuotaCurrentEntry): void { + if (pool.windows.length > 0) return; + const windows = pctWindows(entry); + if (windows.length === 0) return; + pool.windows = windows; + if (pool.note && /no (dated|current) read/i.test(pool.note)) delete pool.note; +} + +function applyEntry(pool: QuotaPool, entry: QuotaCurrentEntry, state: FreshState, legError?: string): boolean { // Returns true when the pool's windows were replaced by a fresh read. const freshness: PoolFreshness = { measuredAt: entry.measuredAt, source: entry.source, state, }; - if (state === "error") freshness.error = errorText(entry); + if (state === "error") freshness.error = !entry.error && legError ? legError : errorText(entry); pool.freshness = freshness; if (state === "error") { pool.state = "error"; + adoptEntryWindows(pool, entry); return false; } if (state === "stale") { + // #6942: a retained entry whose gatherer leg errored is that + // read error surfacing, not mere staleness — but only a pool + // with no dated windows of its own changes rendering for it. + // Spec pools keep the #1268 stale classification and their + // base dated windows exactly as pinned. + if (legError && entry.retainedFromFailedTick && pool.windows.length === 0) { + freshness.state = "error"; + freshness.error = legError; + pool.freshness = freshness; + pool.state = "error"; + adoptEntryWindows(pool, entry); + return false; + } pool.state = "stale"; + adoptEntryWindows(pool, entry); return false; } const windows = pctWindows(entry); @@ -371,7 +419,7 @@ export function overlayQuotaCurrent(view: QuotaView, file: QuotaCurrentFile, now return pool; } const newest = [...entries].sort(newestFirst(nowMs))[0]; - applyEntry(pool, newest, entryState(newest, nowMs)); + applyEntry(pool, newest, entryState(newest, nowMs), legErrorFor(file, newest)); return pool; }); @@ -405,7 +453,7 @@ export function overlayQuotaCurrent(view: QuotaView, file: QuotaCurrentFile, now const rows = credentialRows(entries, nowMs); if (rows) pool.credentials = rows; } else { - applyEntry(pool, newest, entryState(newest, nowMs)); + applyEntry(pool, newest, entryState(newest, nowMs), legErrorFor(file, newest)); } synthesized.push(pool); } diff --git a/pm-stack/aggregator/src/quota-guidance.ts b/pm-stack/aggregator/src/quota-guidance.ts index 0244919f5a..02d5cd435d 100644 --- a/pm-stack/aggregator/src/quota-guidance.ts +++ b/pm-stack/aggregator/src/quota-guidance.ts @@ -26,6 +26,13 @@ export function computeChoices(pools: QuotaPool[], now = Date.now()): QuotaView[ for (const p of pools) { if (p.windows.length === 0) continue; // no current read — no guidance + // #6942 (Codex finding on PR #7067): a pool whose gatherer read is + // stale or errored must not generate actionable guidance from its + // last-known windows — they stand for display only, and advertising + // a failed pool's retained 0% as headroom would route new sessions + // onto a pool whose card reports an error. Pools with no freshness + // stamp (dated-view rows) keep the historic behaviour. + if (p.freshness && p.freshness.state !== "ok") continue; const state = poolState(p); if (state === "walled" || state === "near") { doNotStart.push(p); diff --git a/pm-stack/aggregator/test/quota-current.test.ts b/pm-stack/aggregator/test/quota-current.test.ts index e94176d487..1187589994 100644 --- a/pm-stack/aggregator/test/quota-current.test.ts +++ b/pm-stack/aggregator/test/quota-current.test.ts @@ -1044,3 +1044,117 @@ describe("parseQuotaCurrent measuredAt gates (#4987)", () => { expect(parsed?.providers[0].windows).toHaveLength(1); }); }); + +describe("shell-pool stale/retained path (#6942)", () => { + const SHELL_NOTE = + "No dated read. Live windows arrive through the quota gatherer (the OpenCode opendesign credential, read by the OpenChamber quota module) and are overlaid onto this pool by overlayQuotaCurrent."; + const LEG_ERROR = "Open Design usage API returned HTTP 503"; + + function shellView(): QuotaView { + const view = baseView(); + view.pools.push({ + id: "opendesign", + provider: "Open Design", + plan: "Go", + state: "healthy", + windows: [], + note: SHELL_NOTE, + }); + return view; + } + + function legErrorFile(providers: QuotaCurrentEntry[]): QuotaCurrentFile { + const f = file(providers); + f.heartbeat = { + tickAt: iso(4 * 60_000), + ok: true, + durationMs: 900, + perProvider: { opendesign: { state: "error", ms: 12, rows: 1, error: LEG_ERROR } }, + }; + return f; + } + + const retainedEntry = () => + entry({ + serviceId: "opendesign", + source: "opencode-quota", + measuredAt: iso(45 * 60_000), + retainedFromFailedTick: iso(4 * 60_000), + windows: [win("5h", 0, iso(45 * 60_000)), win("7d", 0, iso(45 * 60_000))], + }); + + test("a retained entry whose leg errored renders the leg error as freshness, entry windows standing", () => { + const out = overlayQuotaCurrent(shellView(), legErrorFile([retainedEntry()]), NOW); + const od = out.pools.find((p) => p.id === "opendesign")!; + expect(od.state).toBe("error"); + expect(od.freshness).toEqual({ + measuredAt: iso(45 * 60_000), + source: "opencode-quota", + state: "error", + error: LEG_ERROR, + }); + // The read's dated fallback windows stand, percentages identical. + expect(od.windows.map((w) => [w.name, w.percent])).toEqual([ + ["5-hour", 0], + ["7d", 0], + ]); + // The read carries a dated read, so the shell note must not stand. + expect(od.note).toBeUndefined(); + }); + + test("a retained entry with no leg error stays stale, but its windows still stand and the shell note is suppressed", () => { + const out = overlayQuotaCurrent(shellView(), file([retainedEntry()]), NOW); + const od = out.pools.find((p) => p.id === "opendesign")!; + expect(od.state).toBe("stale"); + expect(od.freshness).toEqual({ measuredAt: iso(45 * 60_000), source: "opencode-quota", state: "stale" }); + expect(od.windows.map((w) => w.percent)).toEqual([0, 0]); + expect(od.note).toBeUndefined(); + }); + + test("an aged ok entry for a shell pool surfaces stale with the entry's windows standing", () => { + const at = iso(QUOTA_CURRENT_FRESH_MS + 10 * 60_000); + const out = overlayQuotaCurrent( + shellView(), + file([entry({ serviceId: "opendesign", source: "opencode-quota", measuredAt: at, windows: [win("5h", 12, at)] })]), + NOW, + ); + const od = out.pools.find((p) => p.id === "opendesign")!; + expect(od.state).toBe("stale"); + expect(od.windows.map((w) => [w.name, w.percent])).toEqual([["5-hour", 12]]); + expect(od.note).toBeUndefined(); + }); + + test("an errored shell pool with adopted windows is excluded from model-choice guidance", () => { + const out = overlayQuotaCurrent( + shellView(), + legErrorFile([ + retainedEntry(), + entry({ serviceId: "meta", windows: [win("5h", 1.33, iso(5 * 60_000))] }), + ]), + NOW, + ); + const od = out.pools.find((p) => p.id === "opendesign")!; + expect(od.windows.length).toBeGreaterThan(0); // windows stand for display + expect(out.choices.hasHeadroom).toContain("meta"); // the fresh read still guides + expect(out.choices.hasHeadroom).not.toContain("opendesign"); + expect(out.choices.burnBeforeReset).not.toContain("opendesign"); + expect(out.choices.doNotStart).not.toContain("opendesign"); + }); + + test("a spec pool with a leg error keeps the #1268 stale classification and its base dated windows", () => { + const f = legErrorFile([ + entry({ + serviceId: "cline-pass", + measuredAt: iso(5 * 60_000), + retainedFromFailedTick: iso(4 * 60_000), + windows: [win("monthly", 71, iso(5 * 60_000))], + }), + ]); + f.heartbeat.perProvider = { "cline-pass": { state: "error", ms: 9, rows: 1, error: "leg boom" } }; + const out = overlayQuotaCurrent(baseView(), f, NOW); + const cline = out.pools.find((p) => p.id === "cline-pass")!; + expect(cline.state).toBe("stale"); + expect(cline.freshness).toEqual({ measuredAt: iso(5 * 60_000), source: "openchamber-quota", state: "stale" }); + expect(cline.windows[0].percent).toBe(69); // dated read, not the retained gatherer value + }); +});