Repository navigation
fix(server): stream the usage-scan cache to disk instead of one string - #34030
Merged
Merged
Conversation
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Rework item: the real-app run showed head no longer OOMs (it wrote a 361 MB cache and stayed reachable while base died) but kept all seven legacy cache files and the seeded orphan temp folder. Both are named in the issue's expected behaviour, so this commit adds that cleanup and nothing else. In UsageService, after the first successful persist of the current cache in each service lifetime, removeSupersededCacheFiles (1) removes every file in LEGACY_SCAN_CACHE_FILE_NAMES and (2) lists the state directory and recursively removes entries whose name starts with
<cache file name>.for the current or a legacy cache name, only when the entry is a directory whose mtime is more than an hour old (STALE_TEMP_DIRECTORY_MS). The age check leaves a write another server sharing the directory is still making untouched; the seeded 2020usage-scan-cache-v11.json.interruptedfolder qualifies. Every step ignores errors, so cleanup can never fail or retry a save. This reverses the earlier deliberate choice to keep legacy files (the doc comment on SCAN_CACHE_FILE_NAME and the upgrade test asserted it, and both are updated). The cost: after a downgrade, an older server finds no cache of its own and rescans, losing saved usage for deleted transcripts; the newer cache keeps everything. No regex was added; name matching is String.startsWith against a fixed list, linear in name length. The streaming write from the previous commit (the OOM fix, which the real-app run confirmed) is unchanged.Fixes #34018
Reproduction on main
Commit 5f2bb93 (origin/main). Generated synthetic Claude Code transcripts in a scratch CLAUDE_CONFIG_DIR (2,000 files x 1,000 assistant lines with unique message/request ids and usage, timestamps within the last 30 days), CODEX_HOME pointed at an empty dir. Ran
T3CODE_DEV_SEED=0 NODE_OPTIONS=--max-old-space-size=1536 vp run dev, paired a browser tab and opened /usage: the scan finished and wrote a 235 MB usage-scan-cache-v11.json (server RSS ~1.3 GB), no crash. Added 700 more transcripts (2.7M records total), restarted the dev server and opened /usage again: after ~25 s the server child loggedMark-Compact (reduce) 1483.9 (1627.1) -> 1483.9 (1594.4) MB ... last resort; GC in old space requested, thenFATAL ERROR: Ineffective mark-compacts near heap limit Allocation failed - JavaScript heap out of memory, with a native stack ending in v8::String::ValueView -> node::encoding_binding::BindingData::EncodeUtf8String, the same as the report. The server process died (Failed running 'src/bin.ts'), and an empty orphaned temp folderusage-scan-cache-v11.json.bV7rXg/was left next to the cache, as the reporter saw.Evidence
awaitRefresh=true; observe persistence and disk cleanup.EncodeUtf8String; WebSocket closes 1006. Legacy files and stale directories remain; another orphan appears.Seed script · Server driver · RPC driver · Recording script · Full procedure and limitations
AI-assisted; verified with the tests and tools listed above.
Not shown:
Tests
vp test run apps/server/src/usage/ packages/shared/src/atomicWrite.test.ts: 9 files, 159 tests passed. The extended 'upgrades a v%s cache' test (v4, v5, v6, v7, v8, v10) fails for every version with the UsageService.ts change stashed, because legacy files and the stale folder remain, and passes with it. vp run check:changed: format, lint, knip, typecheck (35 packages) and tests (4 files) all pass. The first run failed typecheck on
new Date(0)in the test (Effect globalDateInEffect rule); fixed by passing epoch seconds to utimes. The real-app 2.7M-record run was not repeated this round.Not in this change
<name>.XXXXXX/next to the target) are not swept at startup. The streaming write removes the crash that left them in the issue, but a generic cleanup of stale temp folders in writeFileStringAtomically is a separate change.AI-assisted; verified with the tests and tools listed above.