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
17 changes: 16 additions & 1 deletion pm-stack/aggregator/src/quota-current.ts
Original file line number Diff line number Diff line change
Expand Up @@ -423,7 +423,9 @@ const TURSO_WINDOW_META: Record<string, { label: string; unit: UsageUnit; limit:
* Apply the gatherer's Turso entry (#1049) to the SaaS services —
* the usage() counterpart of overlayQuotaCurrent, with the same
* semantics: a fresh ok read replaces the Turso card's metrics,
* measuredAt, and cycle end; an error or stale read leaves the
* measuredAt, and cycle end — except metrics the fresh read does
* not carry, which stay as stale-marked dated values (#6227); an
* error or stale read leaves the
* card's dated values standing and adds a dated conflict flag, so
* the failure is loud without faking a fresh zero. Services without
* a Turso card, and files without a Turso entry, pass through
Expand Down Expand Up @@ -456,6 +458,19 @@ export function overlayTursoUsage(services: SaasService[], file: QuotaCurrentFil
});
}
if (metrics.length === 0) return services;
// A partial fresh read must not silently erase the card's last
// dated values (#6227): metrics the fresh entry does not carry
// stay on the card, marked stale with the stamp they were read
// at, instead of vanishing until a complete read returns. They
// are evidence of the last measured state, never of recovery.
const freshIds = new Set(metrics.map((m) => 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,
Expand Down
19 changes: 15 additions & 4 deletions pm-stack/aggregator/src/quota-gather-map.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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,
Expand Down
13 changes: 10 additions & 3 deletions pm-stack/aggregator/test/phase3-boundaries.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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);
});
});

Expand Down
36 changes: 36 additions & 0 deletions pm-stack/aggregator/test/quota-current.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
67 changes: 67 additions & 0 deletions pm-stack/aggregator/test/quota-gather-map.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 },
Expand Down Expand Up @@ -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)", () => {
Expand Down