fix(ai): gate 0% usage inference to active weekly periods

- Only infer 0% creditUsagePercent when end > now (active period).
- Reject expired weekly periods without explicit usage data so stale cache fallback is retained until rollover completes.
- Add unit test verifying expired periods without usage percent are rejected.
This commit is contained in:
bubua12
2026-08-12 16:05:56 +08:00
parent 1e8a44e272
commit bbd6c59d25
2 changed files with 28 additions and 5 deletions
+8 -5
View File
@@ -138,11 +138,14 @@ function parseWeeklyBillingConfig(raw: Record<string, unknown>): XaiWeeklyBillin
}
// Fresh weekly periods (or accounts with 0 usage) omit creditUsagePercent;
// default to 0 when a valid weekly period exists.
const creditUsagePercent =
raw.creditUsagePercent === undefined || raw.creditUsagePercent === null
? 0
: parsePercent(raw.creditUsagePercent);
// default to 0 only when the weekly period is active (end > now).
// Expired periods without explicit usage data are rejected to retain last good cache.
let creditUsagePercent: number | undefined;
if (raw.creditUsagePercent === undefined || raw.creditUsagePercent === null) {
creditUsagePercent = end > Date.now() ? 0 : undefined;
} else {
creditUsagePercent = parsePercent(raw.creditUsagePercent);
}
if (creditUsagePercent === undefined) return null;
const productUsage: XaiProductUsage[] = [];
+20
View File
@@ -224,6 +224,26 @@ describe("xai-oauth usage provider", () => {
expect(report?.limits[0]?.id).toBe("xai-oauth:credits:1w");
expect(report?.limits[0]?.window?.resetsAt).toBe(Date.parse(periodEnd));
});
it("rejects an expired weekly period when creditUsagePercent is omitted", async () => {
const periodEnd = new Date(Date.now() - 60_000).toISOString();
const periodStart = new Date(Date.now() - 8 * 24 * 60 * 60 * 1000).toISOString();
const report = await xaiOauthUsageProvider.fetchUsage(
{ provider: "xai-oauth", credential: makeCredential() },
{
fetch: capturingFetch({
config: {
currentPeriod: {
end: periodEnd,
start: periodStart,
type: "USAGE_PERIOD_TYPE_WEEKLY",
},
},
}).fetch,
},
);
expect(report).toBeNull();
});
it("reports zero usage when weekly period is active but creditUsagePercent is omitted (fresh reset)", async () => {
const periodEnd = new Date(Date.now() + 6 * 24 * 60 * 60 * 1000).toISOString();