@@ -5,15 +5,12 @@ import { describe, expect, it } from "vitest";
55import { requireNodeSqlite } from "../infra/node-sqlite.js" ;
66import { resolveSqliteDatabaseFilePaths } from "../infra/sqlite-files.js" ;
77import {
8+ OPENCLAW_STATE_SCHEMA_VERSION ,
89 closeOpenClawStateDatabaseForPath ,
910 openOpenClawStateDatabase ,
1011} from "../state/openclaw-state-db.js" ;
1112import { 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
1815const 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- / D u r a b l e r u n t i m e d a t a b a s e .* u s e s n e w e r s c h e m a v e r s i o n .* s u p p o r t s / ,
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 (
0 commit comments