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
refactor: keep the claim-ledger prune private to the reconciler (#12605)
[._.]/ Adam Eivy78d556114677
diff --git a/server/services/usageBackfillWorker.test.js b/server/services/usageBackfillWorker.test.jsindex 02718803d1..36dec79ce8 100644--- a/server/services/usageBackfillWorker.test.js+++ b/server/services/usageBackfillWorker.test.js@@ -2,7 +2,7 @@ import { afterEach, beforeEach, describe, expect, it } from 'vitest';import { mkdir, mkdtemp, rm, writeFile } from 'fs/promises';import { join } from 'path';import { tmpdir } from 'os';-import { __resetUsageClaims, pruneUsageClaims, readMeasuredUsage, snapshotUsageClaims } from './usageReconciler.js';+import { __resetUsageClaims, readMeasuredUsage, snapshotUsageClaims } from './usageReconciler.js';import { scanHistoricalUsage } from './usageBackfillWorker.js';let root;@@ -96,12 +96,10 @@ describe('historical usage worker', () => {expect(result.total).toBe(0);expect(result.corrections).toEqual([]);});- it('does not re-bill a message the live path claimed, even after a prune', async () => {+ it('does not re-bill a message the live path claimed', async () => {const window = { startTime: '2026-07-01T10:00:00.000Z', endTime: '2026-07-01T10:10:00.000Z' };const live = await readMeasuredUsage({ workspacePath: workspace, ...window, family: 'claude', home });expect(live?.tokensOut).toBe(200);- // A prune inside the retention bound keeps the claim for the hand-off.- pruneUsageClaims(Date.parse(window.endTime) + 60 * 60 * 1000);const claimsSeed = snapshotUsageClaims();// The worker thread starts from its own empty copy of the ledger.__resetUsageClaims();diff --git a/server/services/usageReconciler.js b/server/services/usageReconciler.jsindex 6675d22e85..8b99dc0132 100644--- a/server/services/usageReconciler.js+++ b/server/services/usageReconciler.js@@ -179,7 +179,7 @@ export function mergeUsageClaims({ claimedMessages: claimed = [], codexHighWater** @param {number} [nowMs]*/-export function pruneUsageClaims(nowMs = Date.now()) {+function pruneUsageClaims(nowMs = Date.now()) {const cutoff = nowMs - CLAIM_RETENTION_MS - WINDOW_SLACK_MS;for (const [fileKey, messages] of claimedMessages) {for (const [messageKey, stampMs] of messages) {diff --git a/server/services/usageReconciler.test.js b/server/services/usageReconciler.test.jsindex 6394874abc..3a34ffd6ab 100644--- a/server/services/usageReconciler.test.js+++ b/server/services/usageReconciler.test.js@@ -16,7 +16,6 @@ const {reconcileRunUsage,recordCompletedRunUsage,resolveFamilyProvider,- pruneUsageClaims,snapshotUsageClaims,CLAIM_RETENTION_MS,__resetUsageClaims@@ -790,6 +789,21 @@ describe('claim ledger retention', () => {vi.useRealTimers();});+ // Every completed run prunes the ledger; one with no transcript adds nothing.+ const completeUnrelatedRunAt = async (nowMs) => {+ vi.useFakeTimers({ toFake: ['Date'] });+ vi.setSystemTime(nowMs);+ await recordCompletedRunUsage({+ providerId: 'ollama',+ model: 'llama3',+ workspacePath: '/work/unrelated-repo',+ promptLength: 1,+ startTime: new Date(nowMs - 60_000).toISOString(),+ endTime: new Date(nowMs).toISOString()+ }, 'x', { home, providers: [] });+ vi.useRealTimers();+ };+it('stays bounded across many sequential completed runs', async () => {vi.useFakeTimers({ toFake: ['Date'] });const days = 12;@@ -843,7 +857,7 @@ describe('claim ledger retention', () => {const first = await readMeasuredUsage({ ...run, family: 'claude', home });// A prune between the two reads — the second run is still pending.- pruneUsageClaims(Date.parse(overlapping.endTime) + CLAIM_RETENTION_MS - HOUR);+ await completeUnrelatedRunAt(Date.parse(overlapping.endTime) + CLAIM_RETENTION_MS - HOUR);const second = await readMeasuredUsage({ ...overlapping, family: 'claude', home });expect(first.tokensOut).toBe(75);@@ -869,12 +883,12 @@ describe('claim ledger retention', () => {// Long past the claiming run's window, but the session stayed active// later, so a run overlapping its tail must still find it claimed.- pruneUsageClaims(lastActive + HOUR);+ await completeUnrelatedRunAt(lastActive + HOUR);expect(await readMeasuredUsage({...run, startTime: '2026-07-03T09:00:00.000Z', endTime: '2026-07-03T09:30:00.000Z', family: 'grok', home})).toBeNull();- pruneUsageClaims(lastActive + CLAIM_RETENTION_MS + 2 * HOUR);+ await completeUnrelatedRunAt(lastActive + CLAIM_RETENTION_MS + 2 * HOUR);expect(snapshotUsageClaims().claimedMessages).toEqual([]);});});