fix(session-history): start the versioned cache cold instead of snapshotting the old one in the scanner

This commit is contained in:
Kelvin Amoaba
2026-11-12 00:11:14 +00:00
parent 419705ca4a
commit 660c021ea8
5 changed files with 8 additions and 77 deletions
+1 -1
View File
@@ -16,7 +16,7 @@
1 src/main/agent-launch/trust/execution-host-workspace-trust.ts
1 src/main/ai-vault-search/worker/session-search-worker-client.ts
4 src/main/ai-vault/parse-cache/session-parse-cache-legacy-json.ts
7 src/main/ai-vault/parse-cache/session-parse-cache-version-files.ts # once per scanner-child database open, beside the legacy JSON import; not Electron main
2 src/main/ai-vault/parse-cache/session-parse-cache-version-files.ts # once per scanner-child database open: list and stat the few cache files; not Electron main
2 src/main/ai-vault/scan-service/session-scanner-service-entry-path.ts
1 src/main/ai-vault/scan-service/session-scanner-service-spawn.ts
3 src/main/ai-vault/scanners/omp/omp-session-root.ts # small fixed profile-dir probes mirroring OMP dirs.ts; one readdir + per-profile candidate checks, not a per-file sweep
@@ -252,38 +252,6 @@ describe('session parse cache persistence', () => {
}
})
it('adopts a pre-versioning file on this schema without disturbing a build still using it', async () => {
const root = await makeTempDir()
const cacheFile = join(root, 'session-parse-cache.json')
const transcript = await writeTranscript(root)
const candidate = await claudeCandidate(transcript)
const session = await parseAgentSessionFileCached(candidate, process.platform)
const unversioned = new SessionParseCacheDatabase(join(root, 'session-parse-cache.sqlite'))
unversioned.write(
[
[
transcript,
{
mtimeMs: candidate.file.mtimeMs,
sizeBytes: candidate.file.sizeBytes ?? null,
platform: process.platform,
session
}
]
],
// ast-grep-ignore: test-no-wall-clock -- Real database expiry uses current time for persisted row ages.
Date.now()
)
try {
simulateRestart(cacheFile)
expect(await parse(transcript)).toMatchObject({ reused: 1, fullParses: 0 })
expect(unversioned.read(transcript)).toBeDefined()
} finally {
unversioned.close()
}
})
it('scanAiVaultSessions reads the database and writes after parsing', async () => {
const root = await makeTempDir()
const cacheFile = join(root, 'session-parse-cache.json')
@@ -6,7 +6,6 @@ import { splitOpenCodeSqliteCandidate } from '../../../shared/opencode-sqlite-se
import { SessionParseCacheDatabase } from './session-parse-cache-database'
import { importLegacySessionParseCacheJson } from './session-parse-cache-legacy-json'
import {
adoptUnversionedSessionParseCache,
pruneOtherSessionParseCacheVersions,
sessionParseCacheDatabasePath
} from './session-parse-cache-version-files'
@@ -119,7 +118,6 @@ function openedDatabase(): SessionParseCacheDatabase | null {
try {
const directory = dirname(options.filePath)
const path = sessionParseCacheDatabasePath(directory)
adoptUnversionedSessionParseCache(directory, path)
database = new SessionParseCacheDatabase(path)
importLegacySessionParseCacheJson(options.filePath, database, Date.now())
pruneOtherSessionParseCacheVersions(directory, path)
@@ -1,15 +1,11 @@
import { existsSync, readdirSync, renameSync, rmSync, statSync } from 'node:fs'
import { readdirSync, statSync } from 'node:fs'
import { join } from 'node:path'
import { isTransientSqliteContention } from '../../sqlite/sqlite-read-failure'
import SyncDatabase from '../../sqlite/sync-database'
import {
removeVersionedCacheDatabase,
storedCacheSchemaVersion
} from '../../sqlite/versioned-cache-database'
import { removeVersionedCacheDatabase } from '../../sqlite/versioned-cache-database'
import { SESSION_PARSE_CACHE_SCHEMA_VERSION } from './session-parse-cache-database'
// The name before per-version files; older builds still write it.
const UNVERSIONED_NAME = 'session-parse-cache.sqlite'
// Without a version: the name before per-version files, which older builds still write.
const CACHE_FILE_NAME = /^session-parse-cache(?:\.v\d+)?\.sqlite$/
// Builds on other schema versions (a downgrade, or dev and installed builds) share userData; keep the latest one warm.
const RETAINED_OTHER_VERSIONS = 1
@@ -19,31 +15,6 @@ export function sessionParseCacheDatabasePath(directory: string): string {
return join(directory, `session-parse-cache.v${SESSION_PARSE_CACHE_SCHEMA_VERSION}.sqlite`)
}
/** Copy a pre-versioning file that is already on this schema, so upgrading reparses nothing. */
export function adoptUnversionedSessionParseCache(directory: string, path: string): void {
const unversioned = join(directory, UNVERSIONED_NAME)
if (existsSync(path) || !existsSync(unversioned)) {
return
}
// Snapshot into a private file first: a build still using the old file keeps it untouched.
const staging = `${path}.adopting`
try {
rmSync(staging, { force: true })
const db = new SyncDatabase(unversioned, { readonly: true })
try {
if (storedCacheSchemaVersion(db) === SESSION_PARSE_CACHE_SCHEMA_VERSION) {
db.prepare('VACUUM INTO ?').run(staging)
renameSync(staging, path)
}
} finally {
db.close()
}
} catch {
// Unreadable or busy: this version starts cold and the old file is pruned like any other.
rmSync(staging, { force: true })
}
}
/** Keep this version and the most recently used other one; the rest only cost disk. */
export function pruneOtherSessionParseCacheVersions(directory: string, path: string): void {
let others: { path: string; usedAt: number }[]
+4 -10
View File
@@ -126,20 +126,14 @@ export function removeVersionedCacheDatabase(
}
}
/** Null is a fresh database with no meta table; a missing or unparseable version row is NaN. */
export function storedCacheSchemaVersion(db: SyncDatabase): number | null {
/** No meta table is a fresh database; missing/unparseable version rows in existing meta indicate stale data and must rebuild rather than seed over it. */
function isStaleSchema(db: SyncDatabase, version: number): boolean {
const table = db
.prepare("SELECT name FROM sqlite_master WHERE type = 'table' AND name = 'meta'")
.get()
if (!table) {
return null
return false
}
const row = db.prepare("SELECT value FROM meta WHERE key = 'schema_version'").get()
return typeof row?.value === 'string' ? Number(row.value) : Number.NaN
}
/** Missing/unparseable version rows in existing meta indicate stale data and must rebuild rather than seed over it. */
function isStaleSchema(db: SyncDatabase, version: number): boolean {
const stored = storedCacheSchemaVersion(db)
return stored !== null && stored !== version
return (typeof row?.value === 'string' ? Number(row.value) : Number.NaN) !== version
}