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
fix: replay a settled run only up to when it was reconciled (#12605)
[._.]/ Adam Eivy558bd226fdc5
diff --git a/server/services/usageBackfillWorker.js b/server/services/usageBackfillWorker.jsindex 9ecd3637a4..bd8ea52b08 100644--- a/server/services/usageBackfillWorker.js+++ b/server/services/usageBackfillWorker.js@@ -57,7 +57,10 @@ const workspaceAncestors = (path) => {* over in `claimsSeed` cannot carry that alone: it is empty after a restart* and prunes claims past its retention bound, while this scan reaches runs of* any age. Only settled runs that can overlap a pending one are re-read, and- * only for the families each settled pass actually covered.+ * only for the families each settled pass actually covered. Each re-read stops+ * at the moment that pass was reconciled: a message written later (inside the+ * trailing slack) was never seen, so it still belongs to whichever run owns it.+ * A run marked only in usage.json carries no such moment and is re-read whole.*/async function claimSettledUsage(runs, candidates, { home, providers }) {const below = new Map();@@ -82,15 +85,24 @@ async function claimSettledUsage(runs, candidates, { home, providers }) {];if (!pending.some((other) => other.from <= window.to && window.from <= other.to)) continue;const parentFamily = transcriptFamily(metadata);- const families = parentSettled && parentFamily ? [parentFamily] : [];+ const reads = parentSettled && parentFamily+ ? [{ family: parentFamily, seenUntil: metadata.usageReconciledAt }]+ : [];if (siblingSettled && canScanSiblings) {- families.push(...TRANSCRIPT_FAMILIES.filter((family) => (- family !== parentFamily && resolveFamilyProvider(providers, family)- )));+ const seenUntil = metadata.usageSiblingsReconciledAt ?? metadata.usageReconciledAt;+ for (const family of TRANSCRIPT_FAMILIES) {+ if (family !== parentFamily && resolveFamilyProvider(providers, family)) reads.push({ family, seenUntil });+ }}- for (const family of families) {- await readMeasuredUsage({ workspacePath, startTime: metadata.startTime, endTime: metadata.endTime, family, home })- .catch(() => null);+ for (const { family, seenUntil } of reads) {+ await readMeasuredUsage({+ workspacePath,+ startTime: metadata.startTime,+ endTime: metadata.endTime,+ family,+ home,+ seenUntil: typeof seenUntil === 'string' ? seenUntil : null+ }).catch(() => null);}}}@@ -141,7 +153,7 @@ export async function scanHistoricalUsage({for (const runId of await listRunIds(runsDir)) {const metadataPath = join(runsDir, runId, 'metadata.json');const metadata = await readJSONFile(metadataPath, null);- if (!metadata || !metadata.providerId || !metadata.workspacePath+ if (!metadata || !metadata.providerId || typeof metadata.workspacePath !== 'string' || !metadata.workspacePath|| typeof metadata.startTime !== 'string' || typeof metadata.endTime !== 'string'|| !Number.isFinite(Date.parse(metadata.startTime)) || !Number.isFinite(Date.parse(metadata.endTime))) continue;const parentPending = !reconciled.has(runId) && !metadata.usageReconcileddiff --git a/server/services/usageBackfillWorker.test.js b/server/services/usageBackfillWorker.test.jsindex 3b049d0bde..02718803d1 100644--- a/server/services/usageBackfillWorker.test.js+++ b/server/services/usageBackfillWorker.test.js@@ -16,17 +16,17 @@ const writeRun = async (id, metadata, output = 'estimated output') => {await writeFile(join(dir, 'output.txt'), output);};-const writeTranscript = async () => {+const writeTranscript = async ({ id = 'message-1', timestamp = '2026-07-01T10:05:00.000Z', file = 'session-example.jsonl' } = {}) => {const dir = join(home, '.claude', 'projects', '-work-example-repo');await mkdir(dir, { recursive: true });const line = {type: 'assistant',- uuid: 'uuid-message-1',+ uuid: `uuid-${id}`,sessionId: 'session-example',cwd: workspace,- timestamp: '2026-07-01T10:05:00.000Z',+ timestamp,message: {- id: 'message-1',+ id,model: 'claude-opus-5',usage: {input_tokens: 100,@@ -36,7 +36,7 @@ const writeTranscript = async () => {}}};- await writeFile(join(dir, 'session-example.jsonl'), JSON.stringify(line));+ await writeFile(join(dir, file), JSON.stringify(line));};beforeEach(async () => {@@ -141,6 +141,52 @@ describe('historical usage worker', () => {expect(result.total).toBe(1);expect(result.corrections).toEqual([]);});++ it('leaves a message written after the settled run reconciled to the run that owns it', async () => {+ // Inside the settled run's trailing slack, but written after it was read.+ await writeTranscript({ id: 'message-2', timestamp: '2026-07-01T10:10:30.000Z', file: 'session-later.jsonl' });+ await writeRun('run-settled-live', {+ providerId: 'claude-code-tui',+ model: 'claude-opus-5',+ workspacePath: workspace,+ startTime: '2026-07-01T10:00:00.000Z',+ endTime: '2026-07-01T10:10:00.000Z',+ usageReconciled: true,+ usageReconciledAt: '2026-07-01T10:10:05.000Z'+ });+ await writeRun('run-next-history', {+ providerId: 'claude-code-tui',+ model: 'claude-opus-5',+ workspacePath: workspace,+ startTime: '2026-07-01T10:10:10.000Z',+ endTime: '2026-07-01T10:20:00.000Z'+ });++ const result = await scanHistoricalUsage({ runsDir: root, home });+ expect(result.corrections).toHaveLength(1);+ expect(result.corrections[0]).toMatchObject({ runId: 'run-next-history' });+ expect(result.corrections[0].measured[0]).toMatchObject({ tokensOut: 200 });+ });++ it('skips a run whose workspace path is not a string instead of failing the scan', async () => {+ await writeRun('run-malformed', {+ providerId: 'claude-code-tui',+ workspacePath: 42,+ startTime: '2026-07-01T10:00:00.000Z',+ endTime: '2026-07-01T10:10:00.000Z',+ usageReconciled: true+ });+ await writeRun('run-example-1', {+ providerId: 'claude-code-tui',+ model: 'claude-opus-5',+ workspacePath: workspace,+ startTime: '2026-07-01T10:00:00.000Z',+ endTime: '2026-07-01T10:10:00.000Z'+ });++ const result = await scanHistoricalUsage({ runsDir: root, home });+ expect(result.corrections.map((correction) => correction.runId)).toEqual(['run-example-1']);+ });});// A nested `--review-with grok` pass leaves a grok session but never a PortOSdiff --git a/server/services/usageReconciler.js b/server/services/usageReconciler.jsindex f2600ff940..6675d22e85 100644--- a/server/services/usageReconciler.js+++ b/server/services/usageReconciler.js@@ -619,6 +619,8 @@ const FAMILY_READERS = Object.assign(Object.create(null), {* @param {string|null} run.endTime ISO* @param {'claude'|'codex'|'grok'|'agy'|'kimi'} run.family* @param {string} [run.home] override for tests+ * @param {string|null} [run.seenUntil] ISO; caps the window end at the moment a+ * transcript could last have been read, for replaying a read made back then* @returns {Promise<null|{ source: 'measured'|'estimate'|'mixed', family: string,* sessions: number, model: string|null, messages: number, tokensIn: number,* tokensOut: number, cacheReadTokens: number, cacheWriteTokens: number,@@ -627,13 +629,15 @@ const FAMILY_READERS = Object.assign(Object.create(null), {* died mid-turn, and Antigravity is always an estimate (it writes no token* counts anywhere).*/-export async function readMeasuredUsage({ workspacePath, startTime, endTime, family, home = homedir() } = {}) {+export async function readMeasuredUsage({ workspacePath, startTime, endTime, family, home = homedir(), seenUntil = null } = {}) {if (!workspacePath || !family) return null;const startMs = Date.parse(startTime || '');const endMs = Date.parse(endTime || '');+ const seenMs = Date.parse(seenUntil || '');const from = Number.isNaN(startMs) ? null : startMs - WINDOW_SLACK_MS;- const to = Number.isNaN(endMs) ? null : endMs + WINDOW_SLACK_MS;+ const slackTo = Number.isNaN(endMs) ? null : endMs + WINDOW_SLACK_MS;+ const to = slackTo != null && Number.isFinite(seenMs) ? Math.min(slackTo, seenMs) : slackTo;const totals = {source: 'measured',