mirror of
https://github.com/openclaw/openclaw.git
synced 2026-05-08 02:11:23 +00:00
refactor(auth-profiles): centralize active-window logic + strengthen regression coverage
This commit is contained in:
@@ -256,6 +256,17 @@ export function resolveProfileUnusableUntilForDisplay(
|
||||
return resolveProfileUnusableUntil(stats);
|
||||
}
|
||||
|
||||
function keepActiveWindowOrRecompute(params: {
|
||||
existingUntil: number | undefined;
|
||||
now: number;
|
||||
recomputedUntil: number;
|
||||
}): number {
|
||||
const { existingUntil, now, recomputedUntil } = params;
|
||||
const hasActiveWindow =
|
||||
typeof existingUntil === "number" && Number.isFinite(existingUntil) && existingUntil > now;
|
||||
return hasActiveWindow ? existingUntil : recomputedUntil;
|
||||
}
|
||||
|
||||
function computeNextProfileUsageStats(params: {
|
||||
existing: ProfileUsageStats;
|
||||
now: number;
|
||||
@@ -287,29 +298,23 @@ function computeNextProfileUsageStats(params: {
|
||||
baseMs: params.cfgResolved.billingBackoffMs,
|
||||
maxMs: params.cfgResolved.billingMaxMs,
|
||||
});
|
||||
const existingDisabledUntil = params.existing.disabledUntil;
|
||||
const hasActiveDisabledWindow =
|
||||
typeof existingDisabledUntil === "number" &&
|
||||
Number.isFinite(existingDisabledUntil) &&
|
||||
existingDisabledUntil > params.now;
|
||||
// Keep active disable windows immutable so retries within the window cannot
|
||||
// extend recovery time indefinitely.
|
||||
updatedStats.disabledUntil = hasActiveDisabledWindow
|
||||
? existingDisabledUntil
|
||||
: params.now + backoffMs;
|
||||
updatedStats.disabledUntil = keepActiveWindowOrRecompute({
|
||||
existingUntil: params.existing.disabledUntil,
|
||||
now: params.now,
|
||||
recomputedUntil: params.now + backoffMs,
|
||||
});
|
||||
updatedStats.disabledReason = "billing";
|
||||
} else {
|
||||
const backoffMs = calculateAuthProfileCooldownMs(nextErrorCount);
|
||||
const existingCooldownUntil = params.existing.cooldownUntil;
|
||||
const hasActiveCooldownWindow =
|
||||
typeof existingCooldownUntil === "number" &&
|
||||
Number.isFinite(existingCooldownUntil) &&
|
||||
existingCooldownUntil > params.now;
|
||||
// Keep active cooldown windows immutable so retries within the window
|
||||
// cannot push recovery further out.
|
||||
updatedStats.cooldownUntil = hasActiveCooldownWindow
|
||||
? existingCooldownUntil
|
||||
: params.now + backoffMs;
|
||||
updatedStats.cooldownUntil = keepActiveWindowOrRecompute({
|
||||
existingUntil: params.existing.cooldownUntil,
|
||||
now: params.now,
|
||||
recomputedUntil: params.now + backoffMs,
|
||||
});
|
||||
}
|
||||
|
||||
return updatedStats;
|
||||
|
||||
Reference in New Issue
Block a user