From 0808016955751d0a2b1396313ed4e82f65b4fce5 Mon Sep 17 00:00:00 2001 From: repo-worker-tmp-code-b Date: Mon, 16 Nov 2026 06:40:59 -0600 Subject: [PATCH] fix(quota): keep window state for intentionally uninstrumented pools (#10684) A failed or stale gatherer tick no longer relabels pools whose extraction is intentional/uninstrumented (opendesign, deepseek, mistral, openrouter) as error/stale over their real window state. Their pool state recomputes from the standing (or adopted) windows via poolState; the gatherer failure still rides honestly on freshness. Instrumented pools keep the existing error/stale surfacing, including the #6942/#7033 leg-error conversions. --- pm-stack/aggregator/src/quota-current.ts | 25 +++- .../aggregator/test/quota-current.test.ts | 108 ++++++++++++++++++ 2 files changed, 132 insertions(+), 1 deletion(-) diff --git a/pm-stack/aggregator/src/quota-current.ts b/pm-stack/aggregator/src/quota-current.ts index 9178531625..ca287a930b 100644 --- a/pm-stack/aggregator/src/quota-current.ts +++ b/pm-stack/aggregator/src/quota-current.ts @@ -262,6 +262,20 @@ function baseWindowsExpired(pool: QuotaPool, entry: QuotaCurrentEntry): boolean }); } +/** + * #10684: pools whose extraction is intentionally uninstrumented + * (opendesign, deepseek, mistral, openrouter — no dated-read + * extractor by design) carry no extractor samples, so a failed or + * stale gatherer tick must not relabel them "error"/"stale" over + * their real window state. Their pool state recomputes from the + * standing (or adopted) windows via poolState; the gatherer + * failure still rides honestly on freshness. Instrumented pools + * keep the error/stale surfacing below. + */ +function isIntentionallyUninstrumented(pool: QuotaPool): boolean { + return pool.extractionHealth?.status === "intentional" && pool.extractionHealth?.reason === "uninstrumented"; +} + 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 = { @@ -281,8 +295,8 @@ function applyEntry(pool: QuotaPool, entry: QuotaCurrentEntry, state: FreshState // the honest state until the next collector derivation. delete pool.exhaustion; if (state === "error") { - pool.state = "error"; adoptEntryWindows(pool, entry); + pool.state = isIntentionallyUninstrumented(pool) ? poolState(pool) : "error"; return false; } if (state === "lapsed") { @@ -295,6 +309,15 @@ function applyEntry(pool: QuotaPool, entry: QuotaCurrentEntry, state: FreshState return true; } if (state === "stale") { + // #10684: an uninstrumented pool never takes the error/stale + // label — its state recomputes from the standing (or adopted) + // windows while freshness keeps the stale stamp. The #6942 / + // #7033 leg-error conversions below stay for instrumented pools. + if (isIntentionallyUninstrumented(pool)) { + adoptEntryWindows(pool, entry); + pool.state = poolState(pool); + return false; + } // #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. diff --git a/pm-stack/aggregator/test/quota-current.test.ts b/pm-stack/aggregator/test/quota-current.test.ts index 65f310e65f..b879068b42 100644 --- a/pm-stack/aggregator/test/quota-current.test.ts +++ b/pm-stack/aggregator/test/quota-current.test.ts @@ -1416,3 +1416,111 @@ describe("non-pool registration (#10575)", () => { expect(out.gatherer?.providers.map((provider) => provider.serviceId)).toContain("groq"); }); }); + +describe("intentionally uninstrumented pools keep their window state (#10684)", () => { + // Oracle: a pool whose extraction is intentional/uninstrumented + // (no dated-read extractor by design — opendesign, deepseek, + // mistral, openrouter) carries its quota.json pool state, derived + // from its windows, instead of the gatherer's error/stale label; + // the gatherer failure still rides honestly on freshness. The + // control proves the exemption is scoped: an instrumented pool + // whose reading fails still surfaces "error". + const uninstrumented = { status: "intentional", matched: null, reason: "uninstrumented", count: 0 } as const; + + function walledUninstrumentedView(): QuotaView { + const view = baseView(); + view.pools.push({ + id: "opendesign", + provider: "Open Design", + plan: "Go", + state: "walled", + windows: [{ name: "7d", unit: "other", limit: 100, used: 100, percent: 100, resetsAt: "2026-11-17T11:44:41.000Z" }], + extractionHealth: { ...uninstrumented }, + }); + return view; + } + + test("an error entry leaves a walled uninstrumented pool walled, freshness keeping the error", () => { + const out = overlayQuotaCurrent( + walledUninstrumentedView(), + file([entry({ serviceId: "opendesign", state: "error", measuredAt: iso(2 * 60_000), error: "HTTP 503" })]), + NOW, + ); + const od = out.pools.find((p) => p.id === "opendesign")!; + expect(od.state).toBe("walled"); + expect(od.freshness).toEqual({ + measuredAt: iso(2 * 60_000), + source: "openchamber-quota", + state: "error", + error: "HTTP 503", + }); + expect(od.windows[0].percent).toBe(100); + }); + + test("a retained entry with a leg error keeps the adopted window state instead of converting to error", () => { + // Same shape as the #6942 shell-pool conversion, except the + // pool is marked intentionally uninstrumented: the read's dated + // fallback windows (7d at 100%) stand and the pool recomputes + // to walled instead of flipping to error. + const at = iso(45 * 60_000); + const view = baseView(); + view.pools.push({ + id: "opendesign", + provider: "Open Design", + plan: "Go", + state: "healthy", + windows: [], + extractionHealth: { ...uninstrumented }, + }); + const f = file([ + entry({ + serviceId: "opendesign", + source: "opencode-quota", + measuredAt: at, + retainedFromFailedTick: iso(4 * 60_000), + windows: [win("5h", 12, at), win("7d", 100, at)], + }), + ]); + f.heartbeat = { + tickAt: iso(4 * 60_000), + ok: true, + durationMs: 900, + perProvider: { opendesign: { state: "error", ms: 12, rows: 1, error: "Open Design usage API returned HTTP 503" } }, + }; + const out = overlayQuotaCurrent(view, f, NOW); + const od = out.pools.find((p) => p.id === "opendesign")!; + expect(od.state).toBe("walled"); + expect(od.freshness?.state).toBe("stale"); + expect(od.windows.map((w) => [w.name, w.percent])).toEqual([ + ["5-hour", 12], + ["7d", 100], + ]); + }); + + test("an aged (stale) entry leaves a walled uninstrumented pool walled", () => { + const at = iso(QUOTA_CURRENT_FRESH_MS + 10 * 60_000); + const out = overlayQuotaCurrent( + walledUninstrumentedView(), + file([entry({ serviceId: "opendesign", source: "opencode-quota", measuredAt: at, windows: [win("7d", 12, at)] })]), + NOW, + ); + const od = out.pools.find((p) => p.id === "opendesign")!; + expect(od.state).toBe("walled"); + expect(od.freshness).toEqual({ measuredAt: at, source: "opencode-quota", state: "stale" }); + expect(od.windows[0].percent).toBe(100); + }); + + test("control: an instrumented pool with a failed reading still surfaces error", () => { + const view = baseView(); + const claude = view.pools.find((p) => p.id === "anthropic:claude-pro")!; + claude.extractionHealth = { status: "matched", matched: true, count: 1 }; + const out = overlayQuotaCurrent( + view, + file([entry({ serviceId: "anthropic:claude-pro", state: "error", measuredAt: iso(2 * 60_000), error: "boom" })]), + NOW, + ); + const poolOut = out.pools.find((p) => p.id === "anthropic:claude-pro")!; + expect(poolOut.state).toBe("error"); + expect(poolOut.freshness?.state).toBe("error"); + }); +});