fix(resilience): validate ISO reset dates and keep weekly provenance with its hint

- Reject an impossible calendar date in ISO reset prose: Date.parse rolls
  2026-11-31 over to Dec 1, and an exempt ISO reset would bypass the
  operator's maxCooldownMs cap for ~16 days.
- buildWeeklyQuotaFallback labels body_iso_reset only when the ISO instant
  is the selected hint; a "resets in N days" countdown stays capped.
- Split parseDetailedRetryFromErrorText into helpers to clear the
  cognitive-complexity ratchet (accountFallback.ts 2 -> 3 violations), and
  drop its unreachable second ISO check in the day-granularity branch.
- Add November 31 and mixed-hint regression tests.
This commit is contained in:
Rafa Martins
2026-11-18 21:58:06 -03:00
committed by diegosouzapw
parent 2d18601264
commit 42e9fdd4fc
4 changed files with 94 additions and 48 deletions

View File

@@ -1572,16 +1572,44 @@ export function parseRetryAfterFromBody(responseBody: unknown): {
// parseDelayString now lives in ./retryAfterJson.ts (shared with parseRetryHintFromJsonBody's
// Gemini RetryInfo.retryDelay parsing, #7940) — see the import at the top of this file.
type DetailedRetryHint = { retryAfterMs: number; provenance: RetryHintProvenance };
/**
* Relative "XhYmZs" countdown prose ("reset after 1h2m", "will reset after 30m",
* Antigravity / Cloud Code "Resets in 164h27m24s"). Returns `undefined` when no phrase
* matched, so the caller keeps looking; a matched phrase returns its duration (or null).
*/
function parseRelativeCountdownProseMs(msg: string): number | null | undefined {
for (const re of [RESETS_AFTER_RE, WILL_RESET_AFTER_RE, RESETS_IN_RE]) {
const match = re.exec(msg);
if (match?.[1] || match?.[2] || match?.[3]) return computeDurationMs(match);
}
return undefined;
}
/** Gemini phrasing: "Please retry in 54.472178091s" (fractional seconds). */
function parseRetryInSecondsMs(msg: string): number | null {
const retryInSecMatch = RETRY_IN_SEC_RE.exec(msg);
if (!retryInSecMatch?.[1]) return null;
const sec = Number.parseFloat(retryInSecMatch[1]);
if (!Number.isFinite(sec) || sec <= 0) return null;
return Math.min(Math.round(sec * 1000), MAX_PROVIDER_COOLDOWN_MS);
}
function bodyHint(retryAfterMs: number | null): DetailedRetryHint | null {
return retryAfterMs !== null ? { retryAfterMs, provenance: "body" } : null;
}
export function parseDetailedRetryFromErrorText(
errorText: unknown,
provider?: string | null,
nowMs: number = Date.now()
): { retryAfterMs: number; provenance: RetryHintProvenance } | null {
): DetailedRetryHint | null {
if (!errorText || typeof errorText !== "string") return null;
const msg: string = String(errorText);
// A Google PerDay quota violation outlives its seconds-scale RetryInfo.
if (hasGoogleDailyQuotaViolation(msg)) {
return { retryAfterMs: googleDailyQuotaCooldownMs({ nowMs }), provenance: "body" };
return bodyHint(googleDailyQuotaCooldownMs({ nowMs }));
}
const detailedJsonHint = parseDetailedRetryHintFromJsonBody(msg, MAX_PROVIDER_COOLDOWN_MS);
@@ -1600,10 +1628,7 @@ export function parseDetailedRetryFromErrorText(
// MAX_PROVIDER_COOLDOWN_MS, mirroring the JSON RetryInfo.retryDelay cap (#7940).
const pleaseRetryMs = parseDelayString(PLEASE_RETRY_RE.exec(msg)?.[1]);
if (pleaseRetryMs !== null && pleaseRetryMs > 0) {
return {
retryAfterMs: Math.min(pleaseRetryMs, MAX_SHORT_RETRY_HINT_MS),
provenance: "body",
};
return bodyHint(Math.min(pleaseRetryMs, MAX_SHORT_RETRY_HINT_MS));
}
// Issue #2321 / #14479 / #20281: parse embedded absolute ISO retry timestamps.
@@ -1613,48 +1638,15 @@ export function parseDetailedRetryFromErrorText(
return { retryAfterMs: isoMs, provenance: "body_iso_reset" };
}
const match = RESETS_AFTER_RE.exec(msg);
if (match?.[1] || match?.[2] || match?.[3]) {
const dur = computeDurationMs(match);
return dur !== null ? { retryAfterMs: dur, provenance: "body" } : null;
}
const countdownMs = parseRelativeCountdownProseMs(msg);
if (countdownMs !== undefined) return bodyHint(countdownMs);
// Variant without "reset after": "will reset after XhYmZs"
const altMatch = WILL_RESET_AFTER_RE.exec(msg);
if (altMatch?.[1] || altMatch?.[2] || altMatch?.[3]) {
const dur = computeDurationMs(altMatch);
return dur !== null ? { retryAfterMs: dur, provenance: "body" } : null;
}
const retryInSecMs = parseRetryInSecondsMs(msg);
if (retryInSecMs !== null) return bodyHint(retryInSecMs);
// Antigravity / Cloud Code phrasing: "Resets in 164h27m24s".
const resetsInMatch = RESETS_IN_RE.exec(msg);
if (resetsInMatch?.[1] || resetsInMatch?.[2] || resetsInMatch?.[3]) {
const dur = computeDurationMs(resetsInMatch);
return dur !== null ? { retryAfterMs: dur, provenance: "body" } : null;
}
// Gemini phrasing: "Please retry in 54.472178091s" (fractional seconds).
const retryInSecMatch = RETRY_IN_SEC_RE.exec(msg);
if (retryInSecMatch?.[1]) {
const sec = Number.parseFloat(retryInSecMatch[1]);
if (Number.isFinite(sec) && sec > 0) {
return {
retryAfterMs: Math.min(Math.round(sec * 1000), MAX_PROVIDER_COOLDOWN_MS),
provenance: "body",
};
}
}
const dayResetMs = parseDayGranularityResetMs(msg, MAX_PROVIDER_COOLDOWN_MS, nowMs, provider);
if (dayResetMs !== null) {
const isIso = parseIsoDateTimeResetMs(msg, MAX_PROVIDER_COOLDOWN_MS, nowMs, provider) !== null;
return {
retryAfterMs: dayResetMs,
provenance: isIso ? "body_iso_reset" : "body",
};
}
return null;
// The ISO branch above already returned for any absolute timestamp, so a day-granularity
// hint here is a relative countdown or a year-less date: capped `body` provenance.
return bodyHint(parseDayGranularityResetMs(msg, MAX_PROVIDER_COOLDOWN_MS, nowMs, provider));
}
// T07: parse retry time from error text body with combined "XhYmZs" format.

View File

@@ -87,6 +87,21 @@ export function parseDayGranularityResetMs(
return parseMonthDayResetMs(msg, maxMs, nowMs);
}
/**
* `Date.parse` rolls an impossible day over (2026-11-31 becomes 2026-12-01), so a
* malformed reset date would still yield a long wait. Accept only real calendar dates
* (#20281 review): an ISO reset can bypass the operator's maxCooldownMs cap.
*/
function isCalendarDate(stamp: string): boolean {
const [year, month, day] = stamp.slice(0, 10).split("-").map(Number);
const probe = new Date(Date.UTC(year, month - 1, day));
return (
probe.getUTCFullYear() === year &&
probe.getUTCMonth() === month - 1 &&
probe.getUTCDate() === day
);
}
/**
* Z.AI (GLM) reports an exhausted weekly/monthly cap with a FULL absolute
* datetime rather than a countdown:
@@ -118,6 +133,7 @@ export function parseIsoDateTimeResetMs(
);
if (!match) return null;
const stamp = match[1].replace(/[Tt ]/, "T");
if (!isCalendarDate(stamp)) return null;
// Normalize "+0200" to "+02:00": the bare-offset form is not part of the ES Date.parse grammar.
const rawZone = match[2] ? match[2].toUpperCase() : resolveNaiveResetZone(msg, providerOrZone);
const zone = /^[+-]\d{4}$/.test(rawZone) ? `${rawZone.slice(0, 3)}:${rawZone.slice(3)}` : rawZone;

View File

@@ -156,8 +156,15 @@ export function buildWeeklyQuotaFallback(
typeof parsedResetMs === "number" && parsedResetMs > 0
? parsedResetMs
: WEEKLY_QUOTA_COOLDOWN_MS;
const isIso =
parseIsoDateTimeResetMs(errorStr, MAX_WEEKLY_QUOTA_COOLDOWN_MS, nowMs, provider) !== null;
// Provenance follows the selected hint: a "resets in N days" countdown wins over an ISO
// instant in parseDayGranularityResetMs and must stay capped by maxCooldownMs.
const isoResetMs = parseIsoDateTimeResetMs(
errorStr,
MAX_WEEKLY_QUOTA_COOLDOWN_MS,
nowMs,
provider
);
const isIso = isoResetMs !== null && isoResetMs === parsedResetMs;
return {
shouldFallback: true,
cooldownMs,

View File

@@ -11,6 +11,7 @@ import {
getModelLockoutInfo,
clearAllModelLockouts,
} from "../../open-sse/services/accountFallback.ts";
import { buildWeeklyQuotaFallback } from "../../open-sse/services/quotaTextCooldowns.ts";
// Regression test for #20281:
// On subscription-quota 429s whose upstream reset time is stated in error body prose
@@ -177,3 +178,33 @@ test("recordModelLockoutFailure clamps relative prose countdown to maxCooldownMs
Date.now = realNow;
}
});
test("an impossible calendar date in ISO reset prose is rejected, not exempted (#20281)", () => {
// Date.parse rolls 2026-11-31 over to 2026-12-01: honoring it would exempt a
// ~16-day lock from the operator's maxCooldownMs cap.
const invalidDate =
"Usage limit reached for 5 hour. Your limit will reset at 2026-11-31T07:02:00.000Z";
const detailed = parseDetailedRetryFromErrorText(invalidDate, "zai", NOW);
assert.notEqual(detailed?.provenance, "body_iso_reset");
assert.equal(parseRetryFromErrorText(invalidDate, "zai", NOW), null);
});
test("weekly fallback keeps capped provenance when a relative day countdown is selected (#20281)", () => {
// parseDayGranularityResetMs prefers the "resets in N days" countdown; the ISO
// instant elsewhere in the text must not relabel that relative duration.
const mixed =
"Weekly quota exhausted. Resets in 7 days. Your limit will reset at 2026-11-15T07:02:00.000Z";
const fallback = buildWeeklyQuotaFallback(mixed, NOW, "zai");
assert.ok(fallback, "expected a weekly quota fallback");
assert.equal(fallback.quotaResetHintMs, 7 * 24 * 3600 * 1000);
assert.equal(fallback.retryHintSource, "body");
assert.equal(retryHintBypassesMaxCooldownMs(fallback.retryHintSource), false);
});
test("weekly fallback marks a selected ISO reset as body_iso_reset (#20281)", () => {
const isoOnly = "Weekly quota exhausted. Your limit will reset at 2026-11-15T07:02:00.000Z";
const fallback = buildWeeklyQuotaFallback(isoOnly, NOW, "zai");
assert.ok(fallback, "expected a weekly quota fallback");
assert.equal(fallback.quotaResetHintMs, EXPECTED_WAIT_MS);
assert.equal(fallback.retryHintSource, "body_iso_reset");
});