feat(panel): add 3-tier weekly quota capacity and token estimates

- Add a 3-tier pool burn-rate estimator (rolling median calibration, Theil-Sen net-burn slope, and solo cycle ratio) with cross-reset priced token density tracking to derive full-pool USD and token capacities plus account-wide weeklyCapacityEstimate
- Render the Est. Weekly Quota summary banner, preliminary/partial badges, full-pool token equivalents, supplementary weekly fallback rows, and pool header capacity tooltips in QuotaPoolsWidget
- Coalesce multi-pool state persistence across 5h/weekly buckets, suppress post-refund idle replica rebounds, and clamp externalUsedPct to physical local-spend bounds
This commit is contained in:
fish
2026-11-24 11:24:45 +09:00
parent 7e6d11d7aa
commit 4fb7ec4958
13 changed files with 2415 additions and 230 deletions
File diff suppressed because it is too large Load Diff
+127 -4
View File
@@ -2249,6 +2249,16 @@ export async function buildTelemetryPayloadInternal(
});
}
const sortedQuotaPools = Array.from(quotaPoolsMap.values()).sort((a, b) => {
const order = { Gemini: 0, Argon: 1, Carbon: 2, 'Claude & GPT': 3 };
return (order[a.family] ?? 99) - (order[b.family] ?? 99);
});
const weeklyCapacityEstimate = buildWeeklyCapacityEstimate({
sortedPools: sortedQuotaPools,
isCustomWeeklyMode,
hasOfficialData: Boolean(officialQuotaInfo?.data),
});
const finalPayload = {
timestamp: new Date().toISOString(),
hostPlatform: process.platform || 'unknown',
@@ -2356,11 +2366,9 @@ export async function buildTelemetryPayloadInternal(
errorMessage: officialQuotaInfo?.error || null,
},
activePool: activePoolQuota,
pools: Array.from(quotaPoolsMap.values()).sort((a, b) => {
const order = { Gemini: 0, Argon: 1, Carbon: 2, 'Claude & GPT': 3 };
return (order[a.family] ?? 99) - (order[b.family] ?? 99);
}),
pools: sortedQuotaPools,
weekly: weeklyQuota,
weeklyCapacityEstimate,
},
modelBreakdown,
};
@@ -2556,6 +2564,7 @@ function tryBuildFastConversationPayload(requestedConvId, filterOpts = {}) {
quotas: {
activePool: activePoolQuota,
weekly: weeklyQuota,
weeklyCapacityEstimate: snap.basePayload.quotas?.weeklyCapacityEstimate || null,
},
consumption: {
session: sessionUsage.finalizedSelectedSession,
@@ -2565,6 +2574,120 @@ function tryBuildFastConversationPayload(requestedConvId, filterOpts = {}) {
};
}
function buildWeeklyCapacityEstimate({
sortedPools = [],
isCustomWeeklyMode = false,
hasOfficialData = false,
} = {}) {
if (isCustomWeeklyMode) {
return {
status: 'custom_mode',
totalEqWeeklyUsd: null,
totalEqWeeklyTokens: null,
readyPoolCount: 0,
totalPoolCount: 0,
hasPreliminary: false,
pools: [],
};
}
const rawPools = Array.isArray(sortedPools) ? sortedPools : [];
const candidatePools = rawPools.filter(
(p) =>
p &&
(p.hasWeekly ||
p.weeklyUsedPct != null ||
Number.isFinite(p.rateEstimate?.eqFullWeeklyUsd))
);
const targetPools = candidatePools.length > 0 ? candidatePools : rawPools;
if (!hasOfficialData && targetPools.length === 0) {
return {
status: 'unavailable',
totalEqWeeklyUsd: null,
totalEqWeeklyTokens: null,
readyPoolCount: 0,
totalPoolCount: 0,
hasPreliminary: false,
pools: [],
};
}
let sumUsd = 0;
let sumTokens = 0;
let readyPoolCount = 0;
let tokenReadyPoolCount = 0;
let hasPreliminary = false;
const pools = [];
for (const p of targetPools) {
const est = p?.rateEstimate && typeof p.rateEstimate === 'object' ? p.rateEstimate : null;
let eqUsd =
Number.isFinite(est?.eqFullWeeklyUsd) && est.eqFullWeeklyUsd > 0
? Number(est.eqFullWeeklyUsd)
: null;
let eqTok =
Number.isFinite(est?.eqFullWeeklyTokens) && est.eqFullWeeklyTokens > 0
? Math.round(Number(est.eqFullWeeklyTokens))
: null;
if (eqUsd === null && Array.isArray(est?.models) && est.models.length > 0) {
const wkModels = est.models.filter(
(m) => Number.isFinite(m?.eqFullWeeklyUsd) && m.eqFullWeeklyUsd > 0
);
if (wkModels.length > 0) {
eqUsd =
wkModels.reduce((acc, m) => acc + Number(m.eqFullWeeklyUsd), 0) / wkModels.length;
const tokModels = wkModels.filter(
(m) => Number.isFinite(m?.eqFullWeeklyTokens) && m.eqFullWeeklyTokens > 0
);
if (tokModels.length === wkModels.length) {
eqTok = Math.round(
tokModels.reduce((acc, m) => acc + Number(m.eqFullWeeklyTokens), 0) / tokModels.length
);
}
}
}
const isPrelim = Boolean(est?.isPreliminaryWeekly);
if (eqUsd !== null) {
readyPoolCount += 1;
sumUsd += eqUsd;
if (isPrelim) hasPreliminary = true;
if (eqTok !== null) {
tokenReadyPoolCount += 1;
sumTokens += eqTok;
}
}
pools.push({
poolKey: p.poolKey || p.family || '',
poolLabel: p.poolLabel || p.family || p.poolKey || 'Pool',
usedWeeklyPct: Number.isFinite(Number(p.weeklyUsedPct)) ? +Number(p.weeklyUsedPct).toFixed(1) : null,
eqFullWeeklyUsd: eqUsd !== null ? +eqUsd.toFixed(1) : null,
eqFullWeeklyTokens: eqTok,
source: est?.sourceWeekly || null,
isPreliminary: isPrelim,
});
}
const totalPoolCount = pools.length;
const status =
readyPoolCount === 0
? 'collecting'
: readyPoolCount < totalPoolCount
? 'partial'
: 'ready';
return {
status,
totalEqWeeklyUsd: readyPoolCount > 0 ? +sumUsd.toFixed(1) : null,
totalEqWeeklyTokens:
readyPoolCount > 0 && tokenReadyPoolCount === readyPoolCount ? Math.round(sumTokens) : null,
readyPoolCount,
totalPoolCount,
hasPreliminary,
pools,
};
}
function enrichTopConversationTitles(topList, convSummaries) {
if (!Array.isArray(topList) || topList.length === 0) return [];
const titleMap = new Map();
@@ -794,8 +794,12 @@ function buildQuotaPoolCycleMap(quotaPoolsMap, nowMs) {
startWkMs,
b5h: createUsageBucket(),
bWk: createUsageBucket(),
pricedTokens5h: 0,
pricedTokensWk: 0,
modelCosts5h: new Map(),
modelCostsWk: new Map(),
modelTokens5h: new Map(),
modelTokensWk: new Map(),
lastActivityMs: 0,
recent10mSessions: new Map(),
});
@@ -1610,20 +1614,38 @@ export function aggregateMultiWindowUsage({
const quotaModelKey = mInfo.baseLabel || mInfo.label || '';
if (qMs >= poolBucketEntry.start5hMs) {
accumulateStepIntoBucket(poolBucketEntry.b5h, stepUsage, cascadeId);
if (stepUsage.resolvedCost > 0 && stepTotalTokens > 0) {
poolBucketEntry.pricedTokens5h += stepTotalTokens;
}
if (quotaModelKey && stepUsage.resolvedCost > 0) {
poolBucketEntry.modelCosts5h.set(
quotaModelKey,
(poolBucketEntry.modelCosts5h.get(quotaModelKey) || 0) + stepUsage.resolvedCost
);
if (stepTotalTokens > 0) {
poolBucketEntry.modelTokens5h.set(
quotaModelKey,
(poolBucketEntry.modelTokens5h.get(quotaModelKey) || 0) + stepTotalTokens
);
}
}
}
if (qMs >= poolBucketEntry.startWkMs) {
accumulateStepIntoBucket(poolBucketEntry.bWk, stepUsage, cascadeId);
if (stepUsage.resolvedCost > 0 && stepTotalTokens > 0) {
poolBucketEntry.pricedTokensWk += stepTotalTokens;
}
if (quotaModelKey && stepUsage.resolvedCost > 0) {
poolBucketEntry.modelCostsWk.set(
quotaModelKey,
(poolBucketEntry.modelCostsWk.get(quotaModelKey) || 0) + stepUsage.resolvedCost
);
if (stepTotalTokens > 0) {
poolBucketEntry.modelTokensWk.set(
quotaModelKey,
(poolBucketEntry.modelTokensWk.get(quotaModelKey) || 0) + stepTotalTokens
);
}
}
}
}
@@ -1836,6 +1858,16 @@ export function aggregateMultiWindowUsage({
}
return out;
};
const serializeModelTokens = (mMap) => {
const out = {};
if (!(mMap instanceof Map)) return out;
for (const [mKey, tokVal] of mMap.entries()) {
if (mKey && Number.isFinite(tokVal) && tokVal > 0) {
out[mKey] = Math.round(tokVal);
}
}
return out;
};
for (const [poolKey, entry] of poolCycleBuckets.entries()) {
let recent10mCostUsd = 0;
for (const sStat of entry.recent10mSessions.values()) {
@@ -1844,10 +1876,14 @@ export function aggregateMultiWindowUsage({
poolCycleStats.set(poolKey, {
cycle5h: formatCycleSummary(entry.b5h),
cycleWeekly: formatCycleSummary(entry.bWk),
pricedTokens5h: Math.round(entry.pricedTokens5h || 0),
pricedTokensWeekly: Math.round(entry.pricedTokensWk || 0),
exactCost5hUsd: +entry.b5h.costUsd.toFixed(6),
exactCostWeeklyUsd: +entry.bWk.costUsd.toFixed(6),
modelCosts5h: serializeModelCosts(entry.modelCosts5h),
modelCostsWeekly: serializeModelCosts(entry.modelCostsWk),
modelTokens5h: serializeModelTokens(entry.modelTokens5h),
modelTokensWeekly: serializeModelTokens(entry.modelTokensWk),
recent10m: {
lastActivityMs: entry.lastActivityMs,
activeSessionCount: entry.recent10mSessions.size,