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(usage): read the Claude Code transcript store strictly so a damaged file is never overwritten
[._.]/ Adam Eivy48bdeb6e9662
diff --git a/server/services/claudeCodeTranscriptUsage.js b/server/services/claudeCodeTranscriptUsage.jsindex c2dd918650..ccfbefcee6 100644--- a/server/services/claudeCodeTranscriptUsage.js+++ b/server/services/claudeCodeTranscriptUsage.js@@ -150,12 +150,17 @@ function mergeDays(stored, scanned) {// MAX_DAYS x MAX_MODELS_PER_DAY rows per call.let storeMemo = null;-/** The persisted local store: `{ days, updatedAt }` (`updatedAt` null before the first scan). */+/**+ * The persisted local store: `{ days, updatedAt }` (`updatedAt` null before the+ * first scan). Throws when the file exists but is unreadable.+ */export async function readLocalTranscriptUsage() {const info = await stat(TRANSCRIPT_USAGE_FILE).catch(() => null);const key = info ? `${TRANSCRIPT_USAGE_FILE}|${info.mtimeMs}|${info.size}` : null;if (key && storeMemo?.key === key) return storeMemo.value;- const raw = await readJSONFile(TRANSCRIPT_USAGE_FILE, null);+ // Strict: a damaged store must stop the refresh, not read as empty and be+ // overwritten by a rescan of only the transcripts the CLI has not yet pruned.+ const raw = await readJSONFile(TRANSCRIPT_USAGE_FILE, null, { strict: true });const value = {days: sanitizeTranscriptDays(raw?.days),updatedAt: isNonBlankStr(raw?.updatedAt) ? raw.updatedAt : nulldiff --git a/server/services/claudeCodeTranscriptUsage.test.js b/server/services/claudeCodeTranscriptUsage.test.jsindex e0ca176a20..132d85e122 100644--- a/server/services/claudeCodeTranscriptUsage.test.js+++ b/server/services/claudeCodeTranscriptUsage.test.js@@ -1,6 +1,6 @@import { describe, it, expect, beforeEach, afterEach, afterAll, vi } from 'vitest';import { mockPathsDataRoot } from '../lib/mockPathsDataRoot.js';-import { mkdtemp, mkdir, writeFile, rm } from 'fs/promises';+import { mkdtemp, mkdir, writeFile, readFile, rm } from 'fs/promises';import { tmpdir } from 'os';import { join } from 'path';@@ -58,6 +58,12 @@ describe('claude code transcript usage', () => {expect(updatedAt).toBeTruthy();});+ it('refuses to overwrite a damaged store with a partial rescan', async () => {+ await writeFile(svc.TRANSCRIPT_USAGE_FILE, '{ not json');+ await expect(svc.refreshLocalTranscriptUsage({ root, force: true })).rejects.toThrow();+ expect(await readFile(svc.TRANSCRIPT_USAGE_FILE, 'utf8')).toBe('{ not json');+ });+it('rebuilds peer-supplied days to the fixed shape', () => {const out = svc.sanitizeTranscriptDays({ '2026-09-10': { m: { messages: 1, input: -5, evil: 9, output: 'x' } }, bad: { m: {} }, '2026-09-11': 'nope' });expect(out).toEqual({ '2026-09-10': { m: { messages: 1, input: 0, output: 0, cacheRead: 0, cacheWrite: 0 } } });diff --git a/server/services/peerUsage.js b/server/services/peerUsage.jsindex 26e2e117b2..0135e9931d 100644--- a/server/services/peerUsage.js+++ b/server/services/peerUsage.js@@ -230,8 +230,9 @@ async function buildSelfEntry() {if (!instanceId) return null;const usage = selfDigest(getUsage());const { quotas, capturedAt: quotasAt } = await readLocalQuotaCards();- const local = await readLocalTranscriptUsage();- const claudeCode = local.updatedAt && Object.keys(local.days).length > 0 ? local : null;+ // An unreadable store publishes no history rather than failing every sync read.+ const local = await readLocalTranscriptUsage().catch(() => null);+ const claudeCode = local?.updatedAt && Object.keys(local.days).length > 0 ? local : null;// capturedAt is the LWW stamp AND manifest fingerprint: the newest of every// component, so a transcript-store refresh alone still gets pulled by peers.const capturedAt = [usage.lastUpdated, quotasAt, claudeCode?.updatedAt].reduce(