codeburn/src/cursor-cache.ts
AgentSeal 2afab5f71a fix: final review cleanup
- Remove unused vi import from cursor test
- Move LANG_DISPLAY_NAMES to module scope (was re-created per render)
- Remove redundant 'script' regex (scrip?t already covers it)
- Unexport getDbFingerprint (internal to cache module)
- Move beforeEach inside describe block (only cursor tests need it)
2026-04-15 05:35:11 -07:00

63 lines
1.6 KiB
TypeScript

import { readFile, writeFile, mkdir, stat } from 'fs/promises'
import { join } from 'path'
import { homedir } from 'os'
import type { ParsedProviderCall } from './providers/types.js'
type ResultCache = {
dbMtimeMs: number
dbSizeBytes: number
calls: ParsedProviderCall[]
}
const CACHE_FILE = 'cursor-results.json'
function getCacheDir(): string {
return join(homedir(), '.cache', 'codeburn')
}
function getCachePath(): string {
return join(getCacheDir(), CACHE_FILE)
}
async function getDbFingerprint(dbPath: string): Promise<{ mtimeMs: number; size: number } | null> {
try {
const s = await stat(dbPath)
return { mtimeMs: s.mtimeMs, size: s.size }
} catch {
return null
}
}
export async function readCachedResults(dbPath: string): Promise<ParsedProviderCall[] | null> {
try {
const fp = await getDbFingerprint(dbPath)
if (!fp) return null
const raw = await readFile(getCachePath(), 'utf-8')
const cache = JSON.parse(raw) as ResultCache
if (cache.dbMtimeMs === fp.mtimeMs && cache.dbSizeBytes === fp.size) {
return cache.calls
}
return null
} catch {
return null
}
}
export async function writeCachedResults(dbPath: string, calls: ParsedProviderCall[]): Promise<void> {
try {
const fp = await getDbFingerprint(dbPath)
if (!fp) return
const dir = getCacheDir()
await mkdir(dir, { recursive: true })
const cache: ResultCache = {
dbMtimeMs: fp.mtimeMs,
dbSizeBytes: fp.size,
calls,
}
await writeFile(getCachePath(), JSON.stringify(cache), 'utf-8')
} catch {}
}