codeburn/tests/fixtures/session-cache-io.ts
Resham Joshi e5dff62fa1
perf: store the session cache in day pieces (#1550)
* Store the session cache in day pieces with a per-provider index

Layout v10: a provider's files live in pieces by the UTC day of their
newest call, each with a key file, and an index row per file (piece,
fingerprint, call span, v9 month) read on every load. A ranged load reads
the index, the key files and only the members it reports on; a save
rewrites only the pieces it touched. Rows keep v9's full-load order and
month scope, so orphans, the first-paint snapshot and dedup see what they
saw on v9. The v9 month shards re-lay out once, streaming, and are removed
after v10 publishes.

* Skip cached transcripts outside a narrow query's months when unchanged

v9 could not see them, so every narrow query re-parsed each older Claude
transcript it discovered and republished the cache. The index knows their
fingerprint; an unchanged one needs nothing, a changed one still re-parses
as if uncached.

* Read piece members in batches through one buffer, and full loads concurrently

* Write piece key files without a sync; rebuild lost keys from the piece

* Changelog: day pieces

* Seed dedup with the keys of unchanged transcripts outside the query's months before a whole-file parse

* Upgrade-path check: verify the v10 envelope, indexes, pieces and key files
2026-09-24 20:45:35 +02:00

53 lines
2.2 KiB
TypeScript

// Test-side IO for the sharded session cache: the on-disk form is a directory
// (envelope + one shard per provider), so tests read and write it through the
// real load/save path instead of touching a single JSON file.
import { readFile, readdir } from 'fs/promises'
import { join } from 'path'
import {
clearLoadCacheMemo,
loadCache,
markCacheDirty,
saveCache,
sessionCacheDir,
type CachedFile,
type SessionCache,
} from '../../src/session-cache.js'
/** The cache exactly as it is on disk, bypassing the in-process memo. */
export async function readCacheOnDisk(): Promise<SessionCache> {
clearLoadCacheMemo()
return loadCache()
}
/** Publish `cache`, rewriting every provider's shard. */
export async function writeCacheOnDisk(cache: SessionCache): Promise<void> {
for (const provider of Object.keys(cache.providers)) markCacheDirty(cache, provider)
await saveCache(cache)
clearLoadCacheMemo()
}
/** Byte-level snapshot of the whole cache directory (names + contents). */
export async function cacheDirSnapshot(): Promise<string> {
const dir = sessionCacheDir()
const names = (await readdir(dir)).sort()
const parts = await Promise.all(names.map(async name => `${name}:${await readFile(join(dir, name), 'utf-8')}`))
return parts.join('\n')
}
/** The published pieces of `provider` (UTC day -> file name), straight off disk. */
export async function publishedPieces(provider: string): Promise<Record<string, string>> {
const dir = sessionCacheDir()
const envelope = JSON.parse(await readFile(join(dir, 'envelope.json'), 'utf-8')) as { providers: Record<string, { index: string }> }
const index = envelope.providers[provider]?.index
if (!index) return {}
return (JSON.parse(await readFile(join(dir, index), 'utf-8')) as { pieces: Record<string, string> }).pieces
}
/** Every member the published pieces of `provider` hold, straight off disk. */
export async function publishedMembers(provider: string): Promise<Record<string, CachedFile>> {
const dir = sessionCacheDir()
const files: Record<string, CachedFile> = {}
for (const name of Object.values(await publishedPieces(provider))) Object.assign(files, JSON.parse(await readFile(join(dir, name), 'utf-8')))
return files
}