diff --git a/config/main-sync-fs-baseline.txt b/config/main-sync-fs-baseline.txt index 98cde8472a0..4b8ec76136d 100644 --- a/config/main-sync-fs-baseline.txt +++ b/config/main-sync-fs-baseline.txt @@ -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 diff --git a/src/main/ai-vault/parse-cache/session-parse-cache-persistence.test.ts b/src/main/ai-vault/parse-cache/session-parse-cache-persistence.test.ts index b3d3b5c8c65..f8e8e76a2a0 100644 --- a/src/main/ai-vault/parse-cache/session-parse-cache-persistence.test.ts +++ b/src/main/ai-vault/parse-cache/session-parse-cache-persistence.test.ts @@ -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') diff --git a/src/main/ai-vault/parse-cache/session-parse-cache-persistence.ts b/src/main/ai-vault/parse-cache/session-parse-cache-persistence.ts index 84f22ebee14..9d0bd170bda 100644 --- a/src/main/ai-vault/parse-cache/session-parse-cache-persistence.ts +++ b/src/main/ai-vault/parse-cache/session-parse-cache-persistence.ts @@ -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) diff --git a/src/main/ai-vault/parse-cache/session-parse-cache-version-files.ts b/src/main/ai-vault/parse-cache/session-parse-cache-version-files.ts index 02a185aaaae..5c2431af196 100644 --- a/src/main/ai-vault/parse-cache/session-parse-cache-version-files.ts +++ b/src/main/ai-vault/parse-cache/session-parse-cache-version-files.ts @@ -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 }[] diff --git a/src/main/sqlite/versioned-cache-database.ts b/src/main/sqlite/versioned-cache-database.ts index 837b23e622d..5fef05e7fa8 100644 --- a/src/main/sqlite/versioned-cache-database.ts +++ b/src/main/sqlite/versioned-cache-database.ts @@ -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 }