Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 24 additions & 1 deletion pm-stack/aggregator/src/quota-current.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 = {
Expand All @@ -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") {
Expand All @@ -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.
Expand Down
108 changes: 108 additions & 0 deletions pm-stack/aggregator/test/quota-current.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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");
});
});