Skip to content

Commit 2d564d5

Browse files
committed
fix(durable): keep shared state as schema authority
1 parent 7cda8c7 commit 2d564d5

5 files changed

Lines changed: 33 additions & 180 deletions

File tree

‎src/durable/schema.ts‎

Lines changed: 2 additions & 75 deletions
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,6 @@ import { requireNodeSqlite } from "../infra/node-sqlite.js";
44
import { runSqliteImmediateTransactionSync } from "../infra/sqlite-transaction.js";
55
import { DURABLE_RUNTIME_SCHEMA_SQL } from "./schema.generated.js";
66

7-
export const DURABLE_RUNTIME_SCHEMA_VERSION = 1;
8-
export const DURABLE_RUNTIME_SCHEMA_META_KEY = "durable_runtime";
9-
107
const DURABLE_RUNTIME_TABLES = [
118
"durable_execution_records",
129
"durable_event_evidence",
@@ -20,10 +17,6 @@ const DURABLE_RUNTIME_TABLES = [
2017
"delivery_attempt_evidence",
2118
] as const;
2219

23-
type DurableSchemaMetadataRow = {
24-
schema_version?: unknown;
25-
};
26-
2720
function hasTable(db: DatabaseSync, tableName: string): boolean {
2821
return Boolean(
2922
db
@@ -32,46 +25,6 @@ function hasTable(db: DatabaseSync, tableName: string): boolean {
3225
);
3326
}
3427

35-
function readDurableSchemaVersion(db: DatabaseSync): number | undefined {
36-
if (!hasTable(db, "schema_meta")) {
37-
return undefined;
38-
}
39-
const row = db
40-
.prepare("SELECT schema_version FROM schema_meta WHERE meta_key = ?")
41-
.get(DURABLE_RUNTIME_SCHEMA_META_KEY) as DurableSchemaMetadataRow | undefined;
42-
if (!row) {
43-
return undefined;
44-
}
45-
const version = Number(row.schema_version);
46-
if (!Number.isSafeInteger(version) || version < 0) {
47-
throw new Error(`Durable runtime schema metadata is invalid: ${String(row.schema_version)}`);
48-
}
49-
return version;
50-
}
51-
52-
function assertSupportedDurableSchemaVersion(db: DatabaseSync, pathname: string): void {
53-
const version = readDurableSchemaVersion(db);
54-
if (version !== undefined && version > DURABLE_RUNTIME_SCHEMA_VERSION) {
55-
throw new Error(
56-
`Durable runtime database ${pathname} uses newer schema version ${version}; this OpenClaw build supports ${DURABLE_RUNTIME_SCHEMA_VERSION}.`,
57-
);
58-
}
59-
}
60-
61-
/** Fail before the shared-state owner can mutate a database from a newer durable schema. */
62-
export function assertDurableRuntimeSchemaVersionAtPath(pathname: string): void {
63-
if (!existsSync(pathname)) {
64-
return;
65-
}
66-
const { DatabaseSync } = requireNodeSqlite();
67-
const db = new DatabaseSync(pathname, { readOnly: true });
68-
try {
69-
assertSupportedDurableSchemaVersion(db, pathname);
70-
} finally {
71-
db.close();
72-
}
73-
}
74-
7528
/** Open an already-installed durable schema without creating or migrating state. */
7629
export function openDurableRuntimeSchemaReadOnly(pathname: string): DatabaseSync {
7730
if (!existsSync(pathname)) {
@@ -80,13 +33,6 @@ export function openDurableRuntimeSchemaReadOnly(pathname: string): DatabaseSync
8033
const { DatabaseSync } = requireNodeSqlite();
8134
const db = new DatabaseSync(pathname, { readOnly: true });
8235
try {
83-
assertSupportedDurableSchemaVersion(db, pathname);
84-
const version = readDurableSchemaVersion(db);
85-
if (version !== DURABLE_RUNTIME_SCHEMA_VERSION) {
86-
throw new Error(
87-
`Durable runtime database ${pathname} is not initialized at schema version ${DURABLE_RUNTIME_SCHEMA_VERSION}.`,
88-
);
89-
}
9036
const missingTable = DURABLE_RUNTIME_TABLES.find((tableName) => !hasTable(db, tableName));
9137
if (missingTable) {
9238
throw new Error(
@@ -100,28 +46,9 @@ export function openDurableRuntimeSchemaReadOnly(pathname: string): DatabaseSync
10046
}
10147
}
10248

103-
/** Install or migrate the optional durable schema inside the shared state database. */
104-
export function ensureDurableRuntimeSchema(db: DatabaseSync, pathname: string): void {
105-
assertSupportedDurableSchemaVersion(db, pathname);
49+
/** Lazily install the additive durable tables inside the shared state database. */
50+
export function ensureDurableRuntimeSchema(db: DatabaseSync): void {
10651
runSqliteImmediateTransactionSync(db, () => {
10752
db.exec(DURABLE_RUNTIME_SCHEMA_SQL);
108-
const now = Date.now();
109-
db.prepare(
110-
`INSERT INTO schema_meta (
111-
meta_key, role, schema_version, agent_id, app_version, created_at, updated_at
112-
) VALUES (?, ?, ?, NULL, NULL, ?, ?)
113-
ON CONFLICT(meta_key) DO UPDATE SET
114-
role = excluded.role,
115-
schema_version = excluded.schema_version,
116-
agent_id = NULL,
117-
app_version = NULL,
118-
updated_at = excluded.updated_at`,
119-
).run(
120-
DURABLE_RUNTIME_SCHEMA_META_KEY,
121-
"durable_runtime",
122-
DURABLE_RUNTIME_SCHEMA_VERSION,
123-
now,
124-
now,
125-
);
12653
});
12754
}

‎src/durable/sqlite-store.test.ts‎

Lines changed: 27 additions & 89 deletions
Original file line numberDiff line numberDiff line change
@@ -5,15 +5,12 @@ import { describe, expect, it } from "vitest";
55
import { requireNodeSqlite } from "../infra/node-sqlite.js";
66
import { resolveSqliteDatabaseFilePaths } from "../infra/sqlite-files.js";
77
import {
8+
OPENCLAW_STATE_SCHEMA_VERSION,
89
closeOpenClawStateDatabaseForPath,
910
openOpenClawStateDatabase,
1011
} from "../state/openclaw-state-db.js";
1112
import { resolveOpenClawStateSqlitePath } from "../state/openclaw-state-db.paths.js";
12-
import { DURABLE_RUNTIME_SCHEMA_META_KEY } from "./schema.js";
13-
import {
14-
DURABLE_RUNTIME_SQLITE_SCHEMA_VERSION,
15-
openDurableRuntimeSqliteStore,
16-
} from "./sqlite-store.js";
13+
import { openDurableRuntimeSqliteStore } from "./sqlite-store.js";
1714

1815
const DURABLE_TABLES = [
1916
"delivery_attempt_evidence",
@@ -65,27 +62,39 @@ describe("durable runtime sqlite store", () => {
6562
.all(...DURABLE_TABLES);
6663
expect(runtimeTables).toEqual([]);
6764
expect(
68-
state.db
69-
.prepare("SELECT 1 FROM schema_meta WHERE meta_key = ?")
70-
.get(DURABLE_RUNTIME_SCHEMA_META_KEY),
65+
state.db.prepare("SELECT 1 FROM schema_meta WHERE meta_key = ?").get("durable_runtime"),
7166
).toBeUndefined();
7267
} finally {
7368
closeOpenClawStateDatabaseForPath({ path: dbPath });
7469
fs.rmSync(dir, { recursive: true, force: true });
7570
}
7671
});
7772

78-
it("records the supported durable schema version", () => {
73+
it("installs additive durable tables without a separate schema marker", () => {
7974
const dir = fs.mkdtempSync(path.join(os.tmpdir(), "openclaw-durable-store-"));
75+
const dbPath = path.join(dir, "openclaw.sqlite");
8076
const store = openDurableRuntimeSqliteStore({
81-
path: path.join(dir, "openclaw.sqlite"),
77+
path: dbPath,
8278
});
8379
try {
84-
expect(store.getStats()).toMatchObject({
85-
schemaVersion: DURABLE_RUNTIME_SQLITE_SCHEMA_VERSION,
86-
});
80+
expect(store.getStats()).toMatchObject({ runs: 0, events: 0, steps: 0 });
8781
} finally {
8882
store.close();
83+
}
84+
const { DatabaseSync } = requireNodeSqlite();
85+
const db = new DatabaseSync(dbPath, { readOnly: true });
86+
try {
87+
expect(db.prepare("SELECT 1 FROM schema_meta WHERE meta_key = ?").get("primary")).toEqual({
88+
1: 1,
89+
});
90+
expect(
91+
db.prepare("SELECT 1 FROM schema_meta WHERE meta_key = ?").get("durable_runtime"),
92+
).toBeUndefined();
93+
expect(db.prepare("PRAGMA user_version").get()).toEqual({
94+
user_version: OPENCLAW_STATE_SCHEMA_VERSION,
95+
});
96+
} finally {
97+
db.close();
8998
fs.rmSync(dir, { recursive: true, force: true });
9099
}
91100
});
@@ -96,7 +105,7 @@ describe("durable runtime sqlite store", () => {
96105
const { DatabaseSync } = requireNodeSqlite();
97106
const db = new DatabaseSync(dbPath);
98107
try {
99-
db.exec(`PRAGMA user_version = ${DURABLE_RUNTIME_SQLITE_SCHEMA_VERSION + 1};`);
108+
db.exec(`PRAGMA user_version = ${OPENCLAW_STATE_SCHEMA_VERSION + 1};`);
100109
} finally {
101110
db.close();
102111
}
@@ -124,71 +133,6 @@ describe("durable runtime sqlite store", () => {
124133
}
125134
});
126135

127-
it("rejects a newer durable schema before mutating shared state", () => {
128-
const dir = fs.mkdtempSync(path.join(os.tmpdir(), "openclaw-durable-future-store-"));
129-
const dbPath = path.join(dir, "openclaw.sqlite");
130-
openOpenClawStateDatabase({ path: dbPath });
131-
closeOpenClawStateDatabaseForPath({ path: dbPath });
132-
const { DatabaseSync } = requireNodeSqlite();
133-
const db = new DatabaseSync(dbPath);
134-
try {
135-
db.prepare(
136-
`INSERT INTO schema_meta (
137-
meta_key, role, schema_version, agent_id, app_version, created_at, updated_at
138-
) VALUES (?, ?, ?, NULL, NULL, ?, ?)`,
139-
).run(
140-
DURABLE_RUNTIME_SCHEMA_META_KEY,
141-
"durable_runtime",
142-
DURABLE_RUNTIME_SQLITE_SCHEMA_VERSION + 1,
143-
123,
144-
123,
145-
);
146-
} finally {
147-
db.close();
148-
}
149-
150-
const snapshot = new DatabaseSync(dbPath, { readOnly: true });
151-
const before = (() => {
152-
try {
153-
return {
154-
schema: snapshot
155-
.prepare("SELECT type, name, sql FROM sqlite_schema ORDER BY type, name")
156-
.all(),
157-
metadata: snapshot.prepare("SELECT * FROM schema_meta ORDER BY meta_key").all(),
158-
};
159-
} finally {
160-
snapshot.close();
161-
}
162-
})();
163-
164-
try {
165-
expect(() => openDurableRuntimeSqliteStore({ path: dbPath })).toThrow(
166-
/Durable runtime database .* uses newer schema version .* supports/,
167-
);
168-
const verifyDb = new DatabaseSync(dbPath, { readOnly: true });
169-
try {
170-
expect({
171-
schema: verifyDb
172-
.prepare("SELECT type, name, sql FROM sqlite_schema ORDER BY type, name")
173-
.all(),
174-
metadata: verifyDb.prepare("SELECT * FROM schema_meta ORDER BY meta_key").all(),
175-
}).toEqual(before);
176-
const runtimeTables = verifyDb
177-
.prepare(
178-
`SELECT name FROM sqlite_master
179-
WHERE type = 'table'
180-
AND name IN (${DURABLE_TABLES.map(() => "?").join(", ")})`,
181-
)
182-
.all(...DURABLE_TABLES);
183-
expect(runtimeTables).toEqual([]);
184-
} finally {
185-
verifyDb.close();
186-
}
187-
} finally {
188-
fs.rmSync(dir, { recursive: true, force: true });
189-
}
190-
});
191-
192136
it("upgrades a canonical pre-durable shared state database without touching owner rows", () => {
193137
const dir = fs.mkdtempSync(path.join(os.tmpdir(), "openclaw-durable-upgrade-"));
194138
const dbPath = path.join(dir, "openclaw.sqlite");
@@ -216,9 +160,7 @@ describe("durable runtime sqlite store", () => {
216160

217161
const store = openDurableRuntimeSqliteStore({ path: dbPath });
218162
try {
219-
expect(store.getStats()).toMatchObject({
220-
schemaVersion: DURABLE_RUNTIME_SQLITE_SCHEMA_VERSION,
221-
});
163+
expect(store.getStats()).toMatchObject({ runs: 0, events: 0, steps: 0 });
222164
const run = store.createRun({
223165
operationKind: "openclaw.chat.send",
224166
idempotencyKey: "upgrade-smoke",
@@ -247,12 +189,8 @@ describe("durable runtime sqlite store", () => {
247189
expect(
248190
verifyDb
249191
.prepare("SELECT meta_key, role, schema_version FROM schema_meta WHERE meta_key = ?")
250-
.get(DURABLE_RUNTIME_SCHEMA_META_KEY),
251-
).toEqual({
252-
meta_key: DURABLE_RUNTIME_SCHEMA_META_KEY,
253-
role: "durable_runtime",
254-
schema_version: DURABLE_RUNTIME_SQLITE_SCHEMA_VERSION,
255-
});
192+
.get("durable_runtime"),
193+
).toBeUndefined();
256194
expect(
257195
verifyDb
258196
.prepare(
@@ -268,7 +206,7 @@ describe("durable runtime sqlite store", () => {
268206
},
269207
]);
270208
expect(verifyDb.prepare("PRAGMA user_version").get()).toEqual({
271-
user_version: DURABLE_RUNTIME_SQLITE_SCHEMA_VERSION,
209+
user_version: OPENCLAW_STATE_SCHEMA_VERSION,
272210
});
273211
const runtimeTables = verifyDb
274212
.prepare(

‎src/durable/sqlite-store.ts‎

Lines changed: 2 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -16,12 +16,7 @@ import {
1616
} from "../state/openclaw-state-db.js";
1717
import { resolveOpenClawStateSqlitePath } from "../state/openclaw-state-db.paths.js";
1818
import type { DB as DurableSchemaKyselyDatabase } from "./schema-db.generated.js";
19-
import {
20-
assertDurableRuntimeSchemaVersionAtPath,
21-
DURABLE_RUNTIME_SCHEMA_VERSION,
22-
ensureDurableRuntimeSchema,
23-
openDurableRuntimeSchemaReadOnly,
24-
} from "./schema.js";
19+
import { ensureDurableRuntimeSchema, openDurableRuntimeSchemaReadOnly } from "./schema.js";
2520
import type {
2621
AppendDurableRuntimeEventInput,
2722
ClaimNextWakeObligationInput,
@@ -862,9 +857,6 @@ function isSameSqlValue(
862857
return left === right;
863858
}
864859

865-
export const DURABLE_SQLITE_SCHEMA_VERSION = DURABLE_RUNTIME_SCHEMA_VERSION;
866-
export const DURABLE_RUNTIME_SQLITE_SCHEMA_VERSION = DURABLE_SQLITE_SCHEMA_VERSION;
867-
868860
export function openDurableRuntimeSqliteStore(storeOptions?: {
869861
path?: string;
870862
env?: NodeJS.ProcessEnv;
@@ -879,7 +871,6 @@ export function openDurableRuntimeSqliteStore(storeOptions?: {
879871
db = openDurableRuntimeSchemaReadOnly(pathname);
880872
releaseDatabase = () => db.close();
881873
} else {
882-
assertDurableRuntimeSchemaVersionAtPath(pathname);
883874
const stateDatabaseLease = acquireOpenClawStateDatabaseLease({ env, path: pathname });
884875
db = stateDatabaseLease.database.db;
885876
releaseDatabase = () => {
@@ -890,7 +881,7 @@ export function openDurableRuntimeSqliteStore(storeOptions?: {
890881
const durableDb = (() => {
891882
try {
892883
if (!readOnly) {
893-
ensureDurableRuntimeSchema(db, pathname);
884+
ensureDurableRuntimeSchema(db);
894885
}
895886
return getNodeSqliteKysely<DurableRuntimeDatabase>(db);
896887
} catch (err) {
@@ -3515,7 +3506,6 @@ export function openDurableRuntimeSqliteStore(storeOptions?: {
35153506
getStats(): DurableRuntimeStoreStats {
35163507
return {
35173508
path: pathname,
3518-
schemaVersion: DURABLE_RUNTIME_SCHEMA_VERSION,
35193509
runs: count(
35203510
db,
35213511
durableDb

‎src/durable/store-factory.test.ts‎

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@ import path from "node:path";
44
import { describe, expect, it } from "vitest";
55
import { requireNodeSqlite } from "../infra/node-sqlite.js";
66
import { resolveSqliteDatabaseFilePaths } from "../infra/sqlite-files.js";
7-
import { DURABLE_RUNTIME_SCHEMA_META_KEY } from "./schema.js";
87
import { openDurableRuntimeStore, openDurableRuntimeStoreReadOnly } from "./store-factory.js";
98

109
describe("durable runtime store factory", () => {
@@ -75,8 +74,8 @@ describe("durable runtime store factory", () => {
7574
const db = new DatabaseSync(dbPath, { readOnly: true });
7675
try {
7776
const row = db
78-
.prepare("SELECT updated_at FROM schema_meta WHERE meta_key = ?")
79-
.get(DURABLE_RUNTIME_SCHEMA_META_KEY) as { updated_at: number | bigint };
77+
.prepare("SELECT updated_at FROM schema_meta WHERE meta_key = 'primary'")
78+
.get() as { updated_at: number | bigint };
8079
return Number(row.updated_at);
8180
} finally {
8281
db.close();

‎src/durable/types.ts‎

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -743,7 +743,6 @@ export type ClaimDurableRuntimeStepInput = {
743743

744744
export type DurableRuntimeStoreStats = {
745745
path: string;
746-
schemaVersion: number;
747746
runs: number;
748747
events: number;
749748
steps: number;

0 commit comments

Comments
 (0)