From e93cc92c2b889836d3277506b190ca23003617db Mon Sep 17 00:00:00 2001 From: "developing-today[bot]" <5454422+developing-today[bot]@users.noreply.github.com> Date: Thu, 5 Nov 2026 07:37:18 -0600 Subject: [PATCH] fix(quota-plane): publish over-limit Turso windows and preserve dated card metrics (#6227) --- pm-stack/aggregator/src/quota-current.ts | 17 ++++- pm-stack/aggregator/src/quota-gather-map.ts | 19 ++++-- .../aggregator/test/phase3-boundaries.test.ts | 13 +++- .../aggregator/test/quota-current.test.ts | 36 ++++++++++ .../aggregator/test/quota-gather-map.test.ts | 67 +++++++++++++++++++ 5 files changed, 144 insertions(+), 8 deletions(-) diff --git a/pm-stack/aggregator/src/quota-current.ts b/pm-stack/aggregator/src/quota-current.ts index 27db710f3f..6133afa5dc 100644 --- a/pm-stack/aggregator/src/quota-current.ts +++ b/pm-stack/aggregator/src/quota-current.ts @@ -423,7 +423,9 @@ const TURSO_WINDOW_META: Record m.id)); + for (const m of svc.metrics) { + if (freshIds.has(m.id)) continue; + metrics.push({ + ...m, + note: `stale: last measured ${svc.measuredAt}; absent from the fresh gatherer read at ${entry.measuredAt}`, + }); + } const resetsAt = entry.windows.find((w) => w.resetsAt)?.resetsAt ?? null; out[idx] = { ...svc, diff --git a/pm-stack/aggregator/src/quota-gather-map.ts b/pm-stack/aggregator/src/quota-gather-map.ts index abf41e5fe4..fff87b5439 100644 --- a/pm-stack/aggregator/src/quota-gather-map.ts +++ b/pm-stack/aggregator/src/quota-gather-map.ts @@ -391,7 +391,14 @@ export function entryFromResult(result: QuotaProviderResult, tickIso: string): Q const pct = isFiniteNumber(raw?.usedPercent) ? raw.usedPercent : null; // Percent is bounded by the window's own 100-point limit (#2571); // out-of-range values are dropped, not republished as live data. - if (pct !== null && !isPercentOf(100)(pct)) return; + // Turso is the exception (#6227): its windows are usage-vs-limit + // ratios over counts (storage bytes, rows read), which legitimately + // exceed 100 when the org is over its Starter limits — dropping + // them erased exactly the over-limit state the card exists to + // show (2026-11-05: storage ~147% and rows-read ~165% vanished + // from the live row while the under-limit windows survived). + const overLimitOk = result.providerId === "turso"; + if (pct !== null && !(overLimitOk ? isNonNegative(pct) : isPercentOf(100)(pct))) return; entry.windows.push({ name, value: pct, @@ -712,9 +719,13 @@ export function entryFromTurso( const rawPctOfLimit = (rawUsed / limit) * 100; const pctOfLimit = pct3(rawUsed, limit); // The raw count can be finite while scaling it to a percentage - // overflows, or the source can report a value outside its quota. - // Neither is a usable live window (#2579). - if (!isPercentOf(100)(rawPctOfLimit) || !isPercentOf(100)(pctOfLimit)) return; + // overflows; a non-finite ratio is not a usable live window + // (#2579). A ratio ABOVE 100 is different (#6227): these windows + // are usage-vs-limit ratios over counts, and an org over its + // Starter limits is a real, reportable state (2026-11-05 live: + // storage ~147%, rows-read ~165%) — the window is kept with its + // native value and its >100 pctOfLimit, never dropped. + if (!isNonNegative(rawPctOfLimit) || !isNonNegative(pctOfLimit)) return; entry.windows.push({ name, value, diff --git a/pm-stack/aggregator/test/phase3-boundaries.test.ts b/pm-stack/aggregator/test/phase3-boundaries.test.ts index 31c10bb614..d5a35ecdff 100644 --- a/pm-stack/aggregator/test/phase3-boundaries.test.ts +++ b/pm-stack/aggregator/test/phase3-boundaries.test.ts @@ -110,15 +110,22 @@ describe("entryFromTurso (#2553)", () => { expect(entry.windows.every((w) => (w.value ?? 0) >= 0)).toBe(true); }); - test("out-of-range derived percents are omitted while valid siblings survive", () => { + test("over-limit derived percents publish while valid siblings survive (#6227 supersedes the [0,100] drop)", () => { + // #2553's [0,100] drop treated a count over its limit as bad + // data; #6227 showed it is the normal over-limit state (live + // 2026-11-05: storage ~147%, rows-read ~165% of Starter limits) + // and dropping it erased exactly the state the card must show. + // The boundary that remains: every published pct is finite + // and >= 0 (negative counts are covered by the sibling test). const entry = entryFromTurso( { organization: { usage: { databases: 101, rows_read: 120 } } } as never, null, { measuredAt: "2026-10-29T15:00:00Z" }, ); expect(entry.state).toBe("ok"); - expect(entry.windows.map((w) => w.name)).toEqual(["rows-read"]); - expect(entry.windows.every((w) => w.pctOfLimit !== null && w.pctOfLimit >= 0 && w.pctOfLimit <= 100)).toBe(true); + expect(entry.windows.map((w) => w.name)).toEqual(["databases", "rows-read"]); + expect(entry.windows.find((w) => w.name === "databases")?.pctOfLimit).toBe(101); + expect(entry.windows.every((w) => w.pctOfLimit !== null && Number.isFinite(w.pctOfLimit) && w.pctOfLimit >= 0)).toBe(true); }); }); diff --git a/pm-stack/aggregator/test/quota-current.test.ts b/pm-stack/aggregator/test/quota-current.test.ts index bf641cbf5c..c2a856131a 100644 --- a/pm-stack/aggregator/test/quota-current.test.ts +++ b/pm-stack/aggregator/test/quota-current.test.ts @@ -439,6 +439,42 @@ describe("overlayTursoUsage (#1049)", () => { expect(out[0].conflicts).toHaveLength(1); expect(out[0].conflicts[0]).toContain("HTTP 401"); }); + test("a partial fresh entry preserves the metrics it omits, marked stale (#6227)", () => { + // The 2026-11-05 live shape from issue #6227: the fresh turso + // entry carried databases / rows-written / sync only, and the + // overlay erased the card's dated storage and rows-read values. + const svc = tursoSvc(); + svc.metrics = [ + { id: "databases", label: "Databases", used: 40, limit: 100, unit: "count", percent: 40, highlight: true }, + { id: "storage", label: "Storage", used: 7.358009, limit: 5, unit: "gb", highlight: true }, + { id: "rows-read", label: "Rows read / month", used: 821_437_321, limit: 500_000_000, unit: "rows", highlight: true }, + ]; + const at = iso(5 * 60_000); + const out = overlayTursoUsage( + [svc], + file([ + entry({ + serviceId: "turso", + providerId: "turso", + source: "opencode-quota", + measuredAt: at, + windows: [tursoWin("databases", 44, 44, at), tursoWin("rows-written", 1_672_071, 16.721, at), tursoWin("sync", 0, 0, at)], + }), + ]), + NOW, + ); + const byId = new Map(out[0].metrics.map((m) => [m.id, m])); + expect(byId.get("databases")?.used).toBe(44); + expect(byId.get("databases")?.note).toBeUndefined(); + expect(byId.get("rows-written")?.used).toBe(1_672_071); + // Omitted metrics keep their last dated values, flagged stale — + // never erased, never presented as fresh. + expect(byId.get("storage")?.used).toBe(7.358009); + expect(byId.get("storage")?.note).toContain("stale: last measured 2026-10-20T00:00:00.000Z"); + expect(byId.get("rows-read")?.used).toBe(821_437_321); + expect(byId.get("rows-read")?.note).toContain("stale:"); + expect(out[0].measuredAt).toBe(at); + }); test("no turso entry or no turso card passes through unchanged", () => { const services = [tursoSvc()]; expect(overlayTursoUsage(services, file([]), NOW)).toBe(services); diff --git a/pm-stack/aggregator/test/quota-gather-map.test.ts b/pm-stack/aggregator/test/quota-gather-map.test.ts index 4e08fd9c3f..0288b594e3 100644 --- a/pm-stack/aggregator/test/quota-gather-map.test.ts +++ b/pm-stack/aggregator/test/quota-gather-map.test.ts @@ -188,6 +188,40 @@ describe("entryFromResult", () => { ["zero", 0], ]); }); + test("turso windows keep over-limit percentages; other providers keep the [0,100] gate (#6227)", () => { + // The live path: the opencode quota subcommand's turso result + // reaches the gatherer through entryFromResult. Its storage and + // rows-read windows are usage-vs-limit ratios that legitimately + // exceed 100 over the Starter limits (see the entryFromTurso + // fixture above); negative and non-finite values still drop. + const entry = entryFromResult( + { + providerId: "turso", + providerName: "Turso", + ok: true, + configured: true, + usage: { + windows: { + databases: { usedPercent: 44 }, + storage: { usedPercent: 147.16 }, + "rows-read": { usedPercent: 164.287 }, + "rows-written": { usedPercent: 16.721 }, + sync: { usedPercent: 0 }, + negative: { usedPercent: -1 }, + }, + }, + }, + "2026-11-05T13:00:00.000Z", + ); + expect(entry.serviceId).toBe("turso"); + expect(entry.windows.map((w) => [w.name, w.pctOfLimit])).toEqual([ + ["databases", 44], + ["storage", 147.16], + ["rows-read", 164.287], + ["rows-written", 16.721], + ["sync", 0], + ]); + }); test("error and unconfigured states stay distinct", () => { const err = entryFromResult( { providerId: "claude", ok: false, configured: true, error: "session expired", usage: null }, @@ -640,6 +674,39 @@ describe("entryFromTurso (#1049)", () => { expect(empty.state).toBe("error"); expect(empty.windows).toEqual([]); }); + test("over-limit usage is kept with its native value and >100 pct (#6227)", () => { + // The 2026-11-05 live shape from issue #6227: storage and + // rows-read over their Starter limits — the exact windows the + // [0,100] percent gate used to drop, leaving a fresh-looking + // partial row that read as recovery. + const entry = entryFromTurso( + { + organization: { + usage: { + databases: 44, + storage_bytes: 7_358_009_000, + rows_read: 821_437_321, + rows_written: 1_672_071, + bytes_synced: 0, + }, + }, + }, + subDoc, + { measuredAt: at }, + ); + expect(entry.state).toBe("ok"); + const byName = new Map(entry.windows.map((w) => [w.name, w])); + expect(byName.get("storage")?.value).toBe(7.358009); + expect(byName.get("storage")?.pctOfLimit).toBe(147.16); + expect(byName.get("rows-read")?.value).toBe(821_437_321); + expect(byName.get("rows-read")?.pctOfLimit).toBe(164.287); + expect(byName.get("databases")?.pctOfLimit).toBe(44); + expect(byName.get("sync")?.pctOfLimit).toBe(0); + const { records } = recordsFromEntries([entry], new Map(), "quota-gatherer"); + const byMetric = new Map(records.map((r) => [r.metricId, r])); + expect(byMetric.get("storage")).toMatchObject({ value: 7.358009, unit: "gb", limit: 5 }); + expect(byMetric.get("rows-read")).toMatchObject({ value: 164.287, unit: "pct" }); + }); }); describe("entryFromTurso period stamps (#5091)", () => {