Skip to content

Commit c141e54

Browse files
Afloat16thedotmackclaude
authored
fix(restore): refuse newer-schema snapshots before swapping live data (#5649)
* fix(restore): refuse newer-schema snapshots before swapping live data * build: synchronize verified runtime artifacts * chore: drop generated bundles (rebuilt at release) Co-Authored-By: Claude Opus 5.5 <[email protected]> Claude-Session: https://claude.ai/code/session_019FbY8a4P6RjaWFnrGVZXnd --------- Co-authored-by: Alex Newman <[email protected]> Co-authored-by: Claude Opus 5.5 <[email protected]>
1 parent 74def38 commit c141e54

3 files changed

Lines changed: 131 additions & 4 deletions

File tree

‎src/services/backup/restore-swap.ts‎

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import { Database } from 'bun:sqlite';
55
import { logger } from '../../utils/logger.js';
66
import { renameWithWindowsRetry } from '../../shared/atomic-json.js';
77
import { runSqliteFileJob } from './sqlite-file-job.js';
8+
import { LATEST_SCHEMA_VERSION } from '../sqlite/connection.js';
89
import { ChromaSyncState } from '../sync/ChromaSyncState.js';
910
import {
1011
clearPendingRestore,
@@ -167,7 +168,10 @@ export async function swapDatabaseFromSnapshot(
167168
try {
168169
await copyFile(snapshotPath, stagedDb);
169170
if (existsSync(`${snapshotPath}-wal`)) await copyFile(`${snapshotPath}-wal`, `${stagedDb}-wal`);
170-
await runSqliteFileJob({ kind: 'standalone', target: stagedDb });
171+
// Refuse snapshots this binary cannot open before checkpointing or
172+
// replacing the healthy live database. The startup writer guard would
173+
// otherwise discover the incompatibility only after the swap committed.
174+
await runSqliteFileJob({ kind: 'standalone', target: stagedDb, maxSchemaVersion: LATEST_SCHEMA_VERSION });
171175

172176
if (existsSync(dbPath)) {
173177
// Keep a byte-for-byte copy of the live db and its -wal before anything

‎src/services/backup/sqlite-file-job.ts‎

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ export type SqliteFileJob =
1515
/** VACUUM INTO `target` from `source`, then make it standalone. */
1616
| { kind: 'snapshot'; source: string; target: string }
1717
/** Fold any `-wal` into `target`, switch it to rollback-journal mode, require quick_check ok. */
18-
| { kind: 'standalone'; target: string }
18+
| { kind: 'standalone'; target: string; maxSchemaVersion?: number }
1919
/** PRAGMA wal_checkpoint(TRUNCATE) on `target`; reports whether another connection blocked it. */
2020
| { kind: 'checkpoint'; target: string };
2121

@@ -27,9 +27,15 @@ const CHILD_SCRIPT = String.raw`
2727
const { Database } = require('bun:sqlite');
2828
const fs = require('fs');
2929
function rm(p) { try { fs.unlinkSync(p); } catch (e) { if (e.code !== 'ENOENT') throw e; } }
30-
function standalone(p) {
30+
function standalone(p, maxSchemaVersion) {
3131
const db = new Database(p, { readwrite: true, create: false });
3232
try {
33+
if (maxSchemaVersion !== undefined) {
34+
const stamp = db.query('PRAGMA user_version').get().user_version;
35+
if (stamp > maxSchemaVersion) {
36+
throw new Error('Snapshot has schema v' + stamp + '; this claude-mem knows up to schema v' + maxSchemaVersion + '. Upgrade claude-mem before restoring this snapshot.');
37+
}
38+
}
3339
db.run('PRAGMA journal_mode = DELETE');
3440
const r = db.query('PRAGMA quick_check').get();
3541
if (!r || r.quick_check !== 'ok') throw new Error('database failed quick_check: ' + (r ? r.quick_check : 'no result'));
@@ -54,7 +60,7 @@ try {
5460
try { src.run("VACUUM INTO '" + job.target.replace(/'/g, "''") + "'"); }
5561
finally { src.close(); }
5662
}
57-
standalone(job.target);
63+
standalone(job.target, job.maxSchemaVersion);
5864
}
5965
process.stdout.write(JSON.stringify(result) + '\n');
6066
} catch (e) {
Lines changed: 117 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,117 @@
1+
import { describe, expect, it, spyOn } from 'bun:test';
2+
import { Database } from 'bun:sqlite';
3+
import { copyFileSync, existsSync, mkdirSync, mkdtempSync, readFileSync, readdirSync, rmSync } from 'node:fs';
4+
import { tmpdir } from 'node:os';
5+
import { join } from 'node:path';
6+
import { applyPendingRestore, swapDatabaseFromSnapshot } from '../../src/services/backup/restore-swap.js';
7+
import { readLastRestoreResult, stagePendingRestore } from '../../src/services/backup/snapshot-files.js';
8+
import { LATEST_SCHEMA_VERSION } from '../../src/services/sqlite/connection.js';
9+
import { ChromaSyncState } from '../../src/services/sync/ChromaSyncState.js';
10+
import { paths } from '../../src/shared/paths.js';
11+
12+
function createDatabase(file: string, value: string, schemaVersion: number) {
13+
const db = new Database(file);
14+
db.run('CREATE TABLE memories (value TEXT)');
15+
db.run('INSERT INTO memories VALUES (?)', [value]);
16+
db.run(`PRAGMA user_version = ${schemaVersion}`);
17+
db.close();
18+
}
19+
20+
function fixture(schemaVersion: number) {
21+
const dir = mkdtempSync(join(tmpdir(), 'restore-schema-'));
22+
const dbPath = join(dir, 'live.db');
23+
const backupsDir = join(dir, 'backups');
24+
mkdirSync(backupsDir);
25+
const snapshotName = 'claude-mem-2026-01-01T00-00-00-000Z.db';
26+
const snapshotPath = join(backupsDir, snapshotName);
27+
createDatabase(dbPath, 'healthy current database', LATEST_SCHEMA_VERSION);
28+
createDatabase(snapshotPath, 'snapshot database', schemaVersion);
29+
return { dir, dbPath, backupsDir, snapshotName, snapshotPath };
30+
}
31+
32+
describe('restore schema compatibility before live changes', () => {
33+
it('rejects a newer-schema snapshot before replacing or backing up the healthy live database', async () => {
34+
const { dir, dbPath, backupsDir, snapshotName, snapshotPath } = fixture(LATEST_SCHEMA_VERSION + 1);
35+
const originalLive = readFileSync(dbPath);
36+
const originalSnapshot = readFileSync(snapshotPath);
37+
try {
38+
let failure: unknown;
39+
try { await swapDatabaseFromSnapshot(dbPath, snapshotPath, { preRestoreDir: backupsDir }); }
40+
catch (error) { failure = error; }
41+
expect(failure instanceof Error).toBe(true);
42+
expect((failure as Error).message).toContain(`schema v${LATEST_SCHEMA_VERSION + 1}`);
43+
expect(readFileSync(dbPath)).toEqual(originalLive);
44+
expect(readFileSync(snapshotPath)).toEqual(originalSnapshot);
45+
expect(readdirSync(backupsDir)).toEqual([snapshotName]);
46+
expect(readdirSync(dir).filter(name => name.includes('.restore-'))).toEqual([]);
47+
} finally { rmSync(dir, { recursive: true, force: true }); }
48+
});
49+
50+
it('records a failed pending restore and leaves the worker able to use its existing database', async () => {
51+
const { dir, dbPath, backupsDir, snapshotName } = fixture(LATEST_SCHEMA_VERSION + 1);
52+
const pendingFile = join(dir, 'restore-pending.json');
53+
const resultFile = join(dir, 'last-restore.json');
54+
const pendingSpy = spyOn(paths, 'pendingRestore').mockReturnValue(pendingFile);
55+
const resultSpy = spyOn(paths, 'lastRestore').mockReturnValue(resultFile);
56+
let semanticRebuilds = 0;
57+
const rebuildSpy = spyOn(ChromaSyncState, 'markAllForRebuild').mockImplementation(() => { semanticRebuilds++; });
58+
try {
59+
stagePendingRestore(snapshotName);
60+
const result = await applyPendingRestore(dbPath, backupsDir);
61+
expect(result?.ok).toBe(false);
62+
expect(result?.error).toContain(`schema v${LATEST_SCHEMA_VERSION + 1}`);
63+
expect(result?.preRestoreCopy).toBeNull();
64+
expect(readLastRestoreResult()).toEqual(result);
65+
expect(existsSync(pendingFile)).toBe(false);
66+
expect(semanticRebuilds).toBe(0);
67+
const db = new Database(dbPath);
68+
try {
69+
expect(db.query('SELECT value FROM memories').get()).toEqual({ value: 'healthy current database' });
70+
expect(db.query('PRAGMA user_version').get()).toEqual({ user_version: LATEST_SCHEMA_VERSION });
71+
} finally { db.close(); }
72+
} finally {
73+
rebuildSpy.mockRestore(); resultSpy.mockRestore(); pendingSpy.mockRestore();
74+
rmSync(dir, { recursive: true, force: true });
75+
}
76+
});
77+
78+
it('also refuses a newer schema stamp committed only in a snapshot WAL', async () => {
79+
const { dir, dbPath, backupsDir, snapshotPath } = fixture(LATEST_SCHEMA_VERSION);
80+
const writer = new Database(snapshotPath);
81+
try {
82+
writer.run('PRAGMA journal_mode = WAL');
83+
writer.run('PRAGMA wal_autocheckpoint = 0');
84+
writer.run(`PRAGMA user_version = ${LATEST_SCHEMA_VERSION + 1}`);
85+
// The main file alone still has the compatible stamp; the restore must
86+
// validate the actual staged main + WAL state that it would install.
87+
const mainOnly = join(dir, 'main-only.db');
88+
copyFileSync(snapshotPath, mainOnly);
89+
const probe = new Database(mainOnly);
90+
try { expect(probe.query('PRAGMA user_version').get()).toEqual({ user_version: LATEST_SCHEMA_VERSION }); }
91+
finally { probe.close(); }
92+
const originalLive = readFileSync(dbPath);
93+
let failure: unknown;
94+
try { await swapDatabaseFromSnapshot(dbPath, snapshotPath, { preRestoreDir: backupsDir }); }
95+
catch (error) { failure = error; }
96+
expect(failure instanceof Error).toBe(true);
97+
expect((failure as Error).message).toContain(`schema v${LATEST_SCHEMA_VERSION + 1}`);
98+
expect(readFileSync(dbPath)).toEqual(originalLive);
99+
} finally { writer.close(); rmSync(dir, { recursive: true, force: true }); }
100+
});
101+
102+
it('continues restoring unstamped, older, and current-schema snapshots', async () => {
103+
for (const schemaVersion of [0, LATEST_SCHEMA_VERSION - 1, LATEST_SCHEMA_VERSION]) {
104+
const { dir, dbPath, backupsDir, snapshotPath } = fixture(schemaVersion);
105+
try {
106+
const result = await swapDatabaseFromSnapshot(dbPath, snapshotPath, { preRestoreDir: backupsDir });
107+
expect(result.preRestoreCopy).not.toBeNull();
108+
const db = new Database(dbPath);
109+
try {
110+
expect(db.query('SELECT value FROM memories').get()).toEqual({ value: 'snapshot database' });
111+
expect(db.query('PRAGMA user_version').get()).toEqual({ user_version: schemaVersion });
112+
expect(db.query('PRAGMA quick_check').get()).toEqual({ quick_check: 'ok' });
113+
} finally { db.close(); }
114+
} finally { rmSync(dir, { recursive: true, force: true }); }
115+
}
116+
});
117+
});

0 commit comments

Comments
 (0)