Self-hosted, Self-Improving, Federated Everything App — dev management, agent orchestration, persistent mind, digital twin, second brain, and creative pipeline from a single dashboard over Tailscale to port the user into the OS
JavaScript ai-agentsdashboarddeveloper-toolsdigital-twinquantified-selfsecond-brainself-hostedtailscale
address review (codex): retain residual and model usage
[._.]/ Adam Eivye47b75410e9c
diff --git a/server/services/usage.js b/server/services/usage.jsindex 78fbb0d3c6..4f7f901bee 100644--- a/server/services/usage.js+++ b/server/services/usage.js@@ -36,16 +36,10 @@ const normalizeUndefinedProviderBucket = (byProvider) => {}const stale = byProvider.undefined || {};const current = byProvider[UNKNOWN_PROVIDER_ID] || {};- byProvider[UNKNOWN_PROVIDER_ID] = {- ...stale,- ...current,- name: UNKNOWN_PROVIDER_NAME,- sessions: (stale.sessions || 0) + (current.sessions || 0),- messages: (stale.messages || 0) + (current.messages || 0),- tokens: (stale.tokens || 0) + (current.tokens || 0),- tokensIn: (stale.tokensIn || 0) + (current.tokensIn || 0),- tokensOut: (stale.tokensOut || 0) + (current.tokensOut || 0)- };+ const merged = {};+ deepSumInto(merged, stale);+ deepSumInto(merged, current);+ byProvider[UNKNOWN_PROVIDER_ID] = { ...merged, name: UNKNOWN_PROVIDER_NAME };delete byProvider.undefined;return true;};@@ -410,7 +404,7 @@ const roundCents = (n) => Math.round(n * 100) / 100;* is whole-month-granular: it is included whenever its month overlaps* `[from, to]` (rolled-up months are far older than any day-precise range).*/-export function buildUsageReport(dailyActivity, { from = null, to = null, providers = [], monthlyActivity = null } = {}) {+export function buildUsageReport(dailyActivity, { from = null, to = null, providers = [], monthlyActivity = null, totalTokens = null } = {}) {const configById = new Map((providers || []).map((p) => [p.id, p]));const agg = new Map(); // providerId -> { name, sessions, messages, tokensIn, tokensOut, byModel: Map }let breakdownSince = null;@@ -485,6 +479,23 @@ export function buildUsageReport(dailyActivity, { from = null, to = null, provid}}+ // The legacy POST /usage/tokens path historically updated only all-time+ // totals. On an unbounded report, retain any portion not already represented+ // by day/month buckets without double-counting normal recorded messages.+ if (totalTokens) {+ const represented = [...agg.values()].reduce((sum, provider) => ({+ input: sum.input + provider.tokensIn,+ output: sum.output + provider.tokensOut+ }), { input: 0, output: 0 });+ const residualIn = Math.max(0, (totalTokens.input || 0) - represented.input);+ const residualOut = Math.max(0, (totalTokens.output || 0) - represented.output);+ if (residualIn > 0 || residualOut > 0) {+ const legacy = ensureAggregate(LEGACY_PROVIDER_ID, LEGACY_PROVIDER_NAME);+ legacy.tokensIn += residualIn;+ legacy.tokensOut += residualOut;+ }+ }+const totals = { sessions: 0, messages: 0, tokensIn: 0, tokensOut: 0, estimatedCost: 0 };const providerRows = [];@@ -607,9 +618,19 @@ export function getUsageSummary({ from = null, to = null, providers = [] } = {})const currentStreak = calculateStreak(usageData.dailyActivity);const longestStreak = findLongestStreak(usageData.dailyActivity);- const report = buildUsageReport(usageData.dailyActivity, { from, to, providers, monthlyActivity: usageData.monthlyActivity });+ const report = buildUsageReport(usageData.dailyActivity, {+ from,+ to,+ providers,+ monthlyActivity: usageData.monthlyActivity,+ totalTokens: from || to ? null : usageData.totalTokens+ });const allTimeReport = from || to- ? buildUsageReport(usageData.dailyActivity, { providers, monthlyActivity: usageData.monthlyActivity })+ ? buildUsageReport(usageData.dailyActivity, {+ providers,+ monthlyActivity: usageData.monthlyActivity,+ totalTokens: usageData.totalTokens+ }): report;return {diff --git a/server/services/usage.test.js b/server/services/usage.test.jsindex 13bc5d810b..ae27c5106d 100644--- a/server/services/usage.test.js+++ b/server/services/usage.test.js@@ -362,6 +362,24 @@ describe('usage.js — streak calculations', () => {expect(atomicWrite).toHaveBeenCalled();});+ it('deep-merges model usage while normalizing undefined provider buckets', async () => {+ readJSONFile.mockResolvedValueOnce(makeUsage({+ [daysAgo(0)]: {+ byProvider: {+ unknown: { byModel: { current: { sessions: 2, tokensOut: 20 } } },+ undefined: { byModel: { legacy: { sessions: 1, tokensOut: 10 } } }+ }+ }+ }));++ await loadUsage();++ expect(getUsage().dailyActivity[daysAgo(0)].byProvider.unknown.byModel).toEqual({+ current: { sessions: 2, tokensOut: 20 },+ legacy: { sessions: 1, tokensOut: 10 }+ });+ });+it('recordMessages attributes input and output tokens to provider, model, and day', async () => {readJSONFile.mockResolvedValueOnce(makeUsage({}));await loadUsage();@@ -521,6 +539,30 @@ describe('usage.js — streak calculations', () => {]);});+ it('allocates historical direct-token residuals without double-counting buckets', () => {+ const report = buildUsageReport({+ '2025-06-01': {+ tokens: 500_000,+ byProvider: {+ codex: {+ tokensIn: 100_000,+ tokensOut: 500_000,+ byModel: {}+ }+ }+ }+ }, {+ totalTokens: { input: 1_000_000, output: 1_500_000 }+ });++ expect(report.totals.tokensIn).toBe(1_000_000);+ expect(report.totals.tokensOut).toBe(1_500_000);+ expect(report.providers.find((provider) => provider.id === 'legacy')).toMatchObject({+ tokensIn: 900_000,+ tokensOut: 1_000_000+ });+ });+it('prices provider-level tokens missing a model split at the provider default', () => {const daily = {[daysAgo(0)]: {