refactor(sse): move preflight window warnings into a helper

preflightQuota grew past the 80-line function cap once the expired-window
skip landed, which tripped the new-code complexity ratchet. The per-window
warning loop now lives in warnApproachingWindowCutoffs; behavior is unchanged.
This commit is contained in:
Tomas Fecko
2026-11-19 06:34:43 -03:00
committed by diegosouzapw
parent cfbad7a0e3
commit c4a4f5e4d6

View File

@@ -564,6 +564,29 @@ export function resolveDynamicQuotaFetcher(
return fetchNewApiAggregatorQuota;
}
/** Warn for each live window whose remaining quota is at or below its warn threshold. */
function warnApproachingWindowCutoffs(
label: string,
windows: NonNullable<QuotaInfo["windows"]>,
thresholds?: PreflightQuotaThresholds
): void {
for (const [windowName, windowInfo] of Object.entries(windows)) {
// A window whose reset already passed is stale: it no longer approaches any cutoff.
if (hasQuotaWindowReset(windowInfo.resetAt)) continue;
const warnRemainingPercent = resolveOrDefault(
thresholds?.resolveWarnRemainingPercent,
windowName,
DEFAULT_WARN_REMAINING_PERCENT
);
const remainingPercent = remainingPercentFrom(windowInfo.percentUsed);
if (isRemainingAtOrBelowThreshold(remainingPercent, warnRemainingPercent)) {
console.warn(
`[QuotaPreflight] ${label} ${windowName}: ${remainingPercent.toFixed(1)}% remaining — approaching cutoff`
);
}
}
}
export async function preflightQuota(
provider: string,
connectionId: string,
@@ -603,21 +626,11 @@ export async function preflightQuota(
const windows = quota.windows;
if (windows && Object.keys(windows).length > 0) {
const scopedWindows = windowsForScope(windows, scope);
for (const [windowName, windowInfo] of Object.entries(scopedWindows)) {
if (hasQuotaWindowReset(windowInfo.resetAt)) continue;
const warnRemainingPercent = resolveOrDefault(
thresholds?.resolveWarnRemainingPercent,
windowName,
DEFAULT_WARN_REMAINING_PERCENT
);
const remainingPercent = remainingPercentFrom(windowInfo.percentUsed);
if (isRemainingAtOrBelowThreshold(remainingPercent, warnRemainingPercent)) {
console.warn(
`[QuotaPreflight] ${provider}/${connectionId} ${windowName}: ${remainingPercent.toFixed(1)}% remaining — approaching cutoff`
);
}
}
warnApproachingWindowCutoffs(
`${provider}/${connectionId}`,
windowsForScope(windows, scope),
thresholds
);
}
const decision = evaluateQuotaCutoff(quota, thresholds, scope);