fix(ai): require evidence before inferred xAI weekly usage

This commit is contained in:
can1357
2026-08-13 00:51:32 +02:00
parent 87b28daea1
commit 165f93ecfe
2 changed files with 31 additions and 3 deletions
+6 -3
View File
@@ -202,9 +202,12 @@ function parseMonthlyBillingConfig(raw: Record<string, unknown>): XaiMonthlyBill
onDemandUsed: parseOnDemandAmount(raw.onDemandUsed),
};
}
function hasPositiveMonthlyLimit(raw: Record<string, unknown>): boolean {
function confirmsNoMonthlyQuota(raw: Record<string, unknown>): boolean {
const limit = parseOnDemandAmount(raw.monthlyLimit);
return limit !== undefined && limit > 0;
if (limit !== undefined) return limit === 0;
// Some weekly accounts return the credits shape from the default endpoint too.
return parseWeeklyBillingConfig(raw)?.inferredPercent === true;
}
function buildOnDemandLimit(
@@ -393,7 +396,7 @@ export const xaiOauthUsageProvider: UsageProvider = {
monthlyPayload && isRecord(monthlyPayload) && isRecord(monthlyPayload.config)
? monthlyPayload.config
: null;
if (!monthlyConfig || hasPositiveMonthlyLimit(monthlyConfig)) {
if (!monthlyConfig || !confirmsNoMonthlyQuota(monthlyConfig)) {
effectiveWeekly = null;
}
}
+25
View File
@@ -336,6 +336,31 @@ describe("xai-oauth usage provider", () => {
expect(report).toBeNull();
});
it("rejects inferred unified weekly usage when monthly quota evidence is absent", async () => {
const periodEnd = new Date(Date.now() + 6 * 24 * 60 * 60 * 1000).toISOString();
const periodStart = new Date(Date.now() - 24 * 60 * 60 * 1000).toISOString();
const report = await xaiOauthUsageProvider.fetchUsage(
{ provider: "xai-oauth", credential: makeCredential() },
{
fetch: dualBillingFetch(
{
config: {
currentPeriod: {
end: periodEnd,
start: periodStart,
type: "USAGE_PERIOD_TYPE_WEEKLY",
},
isUnifiedBillingUser: true,
},
},
{ config: { isUnifiedBillingUser: true } },
).fetch,
},
);
expect(report).toBeNull();
});
it("falls back to monthly included quota when credits has no percent fields", async () => {
const { fetch, calls } = dualBillingFetch(makeUnifiedCreditsPayload(), makeUnifiedMonthlyPayload());
const report = await xaiOauthUsageProvider.fetchUsage(