fix(ai): adjusted usage ranking comparator for stable metric ordering

- Added a tolerance-aware `compareUsageRankingMetric` helper with finite-value handling.
- Replaced usage provider sorting comparisons with the new comparator for secondary and primary usage metrics.
- Kept existing tie-breaker fields while stabilizing ordering for nearly equal metric values.
This commit is contained in:
can1357
2026-06-06 21:36:39 +02:00
parent 3b5b182553
commit 5fc443f4af
+18 -6
View File
@@ -36,6 +36,8 @@ import { loginOpenAICodexDevice } from "./utils/oauth/openai-codex";
import type { OAuthController, OAuthCredentials, OAuthProvider, OAuthProviderId } from "./utils/oauth/types";
import { loginXiaomi, loginXiaomiTokenPlan } from "./utils/oauth/xiaomi";
const USAGE_RANKING_METRIC_EPSILON = 1e-9;
// ─────────────────────────────────────────────────────────────────────────────
// Credential Types
// ─────────────────────────────────────────────────────────────────────────────
@@ -606,6 +608,14 @@ function hasOpenAICodexProPlan(report: UsageReport | null): boolean {
return getUsagePlanType(report)?.includes("pro") === true;
}
function compareUsageRankingMetric(left: number, right: number): number {
if (left === right) return 0;
if (!Number.isFinite(left) || !Number.isFinite(right)) return left < right ? -1 : 1;
const delta = left - right;
const tolerance = Math.max(USAGE_RANKING_METRIC_EPSILON, Math.max(Math.abs(left), Math.abs(right)) * 0.000001);
return Math.abs(delta) <= tolerance ? 0 : delta;
}
function resolveDefaultUsageProvider(provider: Provider): UsageProvider | undefined {
return DEFAULT_USAGE_PROVIDER_MAP.get(provider);
}
@@ -2796,12 +2806,14 @@ export class AuthStorage {
return left.planPriority - right.planPriority;
}
if (left.hasPriorityBoost !== right.hasPriorityBoost) return left.hasPriorityBoost ? -1 : 1;
if (left.secondaryDrainRate !== right.secondaryDrainRate) {
return left.secondaryDrainRate - right.secondaryDrainRate;
}
if (left.secondaryUsed !== right.secondaryUsed) return left.secondaryUsed - right.secondaryUsed;
if (left.primaryDrainRate !== right.primaryDrainRate) return left.primaryDrainRate - right.primaryDrainRate;
if (left.primaryUsed !== right.primaryUsed) return left.primaryUsed - right.primaryUsed;
let metric = compareUsageRankingMetric(left.secondaryDrainRate, right.secondaryDrainRate);
if (metric !== 0) return metric;
metric = compareUsageRankingMetric(left.secondaryUsed, right.secondaryUsed);
if (metric !== 0) return metric;
metric = compareUsageRankingMetric(left.primaryDrainRate, right.primaryDrainRate);
if (metric !== 0) return metric;
metric = compareUsageRankingMetric(left.primaryUsed, right.primaryUsed);
if (metric !== 0) return metric;
return 0;
}