@@ -3,7 +3,7 @@ import { execFileSync, spawn } from "node:child_process";
33import fs from "node:fs" ;
44import os from "node:os" ;
55import path from "node:path" ;
6- import { afterAll , afterEach , describe , expect , it , vi } from "vitest" ;
6+ import { afterAll , afterEach , beforeAll , describe , expect , it , vi } from "vitest" ;
77import { cleanupTempDirs , makeTempDir } from "../../test/helpers/temp-dir.js" ;
88import {
99 beginAgentDeletion ,
@@ -70,6 +70,7 @@ type AgentDbTestDatabase = Pick<
7070
7171const agentDbTempDirs : string [ ] = [ ] ;
7272let sharedStateDatabaseTemplatePath : string | undefined ;
73+ let currentWorkerAgentDatabaseTemplatePath : string | undefined ;
7374
7475function createTempStateDir ( ) : string {
7576 return makeTempDir ( agentDbTempDirs , "openclaw-agent-db-" ) ;
@@ -100,6 +101,69 @@ function openOpenClawAgentDatabase(
100101 return openOpenClawAgentDatabaseRuntime ( options ) ;
101102}
102103
104+ function ensureCurrentWorkerAgentDatabaseTemplate ( ) : string {
105+ if ( currentWorkerAgentDatabaseTemplatePath ) {
106+ return currentWorkerAgentDatabaseTemplatePath ;
107+ }
108+ const stateDir = makeTempDir ( agentDbTempDirs , "openclaw-agent-db-current-worker-" ) ;
109+ const env = { OPENCLAW_STATE_DIR : stateDir } ;
110+ const template = openOpenClawAgentDatabase ( { agentId : "worker-1" , env } ) ;
111+ const templatePath = template . path ;
112+ template . db . exec ( "PRAGMA wal_checkpoint(TRUNCATE);" ) ;
113+ closeOpenClawAgentDatabasesForTest ( ) ;
114+ closeOpenClawStateDatabaseForTest ( ) ;
115+
116+ const walPath = `${ templatePath } -wal` ;
117+ if ( fs . existsSync ( walPath ) && fs . statSync ( walPath ) . size > 0 ) {
118+ throw new Error ( "current worker agent database template retained WAL content" ) ;
119+ }
120+ for ( const suffix of [ "-wal" , "-shm" , "-journal" ] ) {
121+ fs . rmSync ( `${ templatePath } ${ suffix } ` , { force : true } ) ;
122+ }
123+
124+ const { DatabaseSync } = requireNodeSqlite ( ) ;
125+ const verified = new DatabaseSync ( templatePath , { readOnly : true } ) ;
126+ try {
127+ const integrity = verified . prepare ( "PRAGMA integrity_check" ) . get ( ) as
128+ | { integrity_check ?: unknown }
129+ | undefined ;
130+ if ( integrity ?. integrity_check !== "ok" ) {
131+ throw new Error ( "current worker agent database template failed integrity check" ) ;
132+ }
133+ if ( verified . prepare ( "PRAGMA foreign_key_check" ) . all ( ) . length > 0 ) {
134+ throw new Error ( "current worker agent database template failed foreign key check" ) ;
135+ }
136+ if ( readSqliteNumberPragma ( verified , "user_version" ) !== OPENCLAW_AGENT_SCHEMA_VERSION ) {
137+ throw new Error ( "current worker agent database template has the wrong schema version" ) ;
138+ }
139+ const owner = verified
140+ . prepare ( "SELECT role, agent_id FROM schema_meta WHERE meta_key = 'primary'" )
141+ . get ( ) ;
142+ if ( ! owner || ( owner as { agent_id ?: unknown } ) . agent_id !== "worker-1" ) {
143+ throw new Error ( "current worker agent database template has the wrong owner" ) ;
144+ }
145+ } finally {
146+ verified . close ( ) ;
147+ }
148+ currentWorkerAgentDatabaseTemplatePath = templatePath ;
149+ return templatePath ;
150+ }
151+
152+ function materializeCurrentWorkerAgentDatabase ( stateDir : string ) : string {
153+ const options = {
154+ agentId : "worker-1" ,
155+ env : { OPENCLAW_STATE_DIR : stateDir } ,
156+ } as const ;
157+ const databasePath = resolveOpenClawAgentSqlitePath ( options ) ;
158+ fs . mkdirSync ( path . dirname ( databasePath ) , { recursive : true } ) ;
159+ fs . copyFileSync (
160+ ensureCurrentWorkerAgentDatabaseTemplate ( ) ,
161+ databasePath ,
162+ fs . constants . COPYFILE_EXCL ,
163+ ) ;
164+ return databasePath ;
165+ }
166+
103167function migrateAndOpenLegacyAgentDatabaseForTest (
104168 options : Parameters < typeof openOpenClawAgentDatabase > [ 0 ] ,
105169) {
@@ -671,6 +735,10 @@ function launchAgentSchemaOpener(params: {
671735 return { beginAttempt, child, ready, result } ;
672736}
673737
738+ beforeAll ( ( ) => {
739+ ensureCurrentWorkerAgentDatabaseTemplate ( ) ;
740+ } ) ;
741+
674742afterAll ( ( ) => {
675743 cleanupTempDirs ( agentDbTempDirs ) ;
676744} ) ;
@@ -1603,6 +1671,7 @@ describe("openclaw agent database", () => {
16031671
16041672 it ( "generates stable typed memory source identities" , ( ) => {
16051673 const stateDir = createTempStateDir ( ) ;
1674+ materializeCurrentWorkerAgentDatabase ( stateDir ) ;
16061675 const database = openOpenClawAgentDatabase ( {
16071676 agentId : "worker-1" ,
16081677 env : { OPENCLAW_STATE_DIR : stateDir } ,
@@ -2919,10 +2988,7 @@ describe("openclaw agent database", () => {
29192988 it ( "repairs a same-name transcript uniqueness index before accepting writes" , ( ) => {
29202989 const stateDir = createTempStateDir ( ) ;
29212990 const env = { OPENCLAW_STATE_DIR : stateDir } ;
2922- const created = openOpenClawAgentDatabase ( { agentId : "worker-1" , env } ) ;
2923- const databasePath = created . path ;
2924- closeOpenClawAgentDatabasesForTest ( ) ;
2925- closeOpenClawStateDatabaseForTest ( ) ;
2991+ const databasePath = materializeCurrentWorkerAgentDatabase ( stateDir ) ;
29262992 createTranscriptIdempotencyIndexDrift ( databasePath ) ;
29272993
29282994 const reopened = openOpenClawAgentDatabase ( { agentId : "worker-1" , env } ) ;
@@ -2950,10 +3016,7 @@ describe("openclaw agent database", () => {
29503016 it ( "repairs physical transcript index drift hidden behind canonical schema text" , ( ) => {
29513017 const stateDir = createTempStateDir ( ) ;
29523018 const env = { OPENCLAW_STATE_DIR : stateDir } ;
2953- const created = openOpenClawAgentDatabase ( { agentId : "worker-1" , env } ) ;
2954- const databasePath = created . path ;
2955- closeOpenClawAgentDatabasesForTest ( ) ;
2956- closeOpenClawStateDatabaseForTest ( ) ;
3019+ const databasePath = materializeCurrentWorkerAgentDatabase ( stateDir ) ;
29573020 createTranscriptIdempotencyIndexDrift ( databasePath , { hideWithCanonicalSql : true } ) ;
29583021
29593022 const reopened = openOpenClawAgentDatabase ( { agentId : "worker-1" , env } ) ;
@@ -2984,11 +3047,10 @@ describe("openclaw agent database", () => {
29843047 it ( "repairs every canonical agent-state named index" , ( ) => {
29853048 const stateDir = createTempStateDir ( ) ;
29863049 const env = { OPENCLAW_STATE_DIR : stateDir } ;
2987- const created = openOpenClawAgentDatabase ( { agentId : "worker-1" , env } ) ;
2988- const databasePath = created . path ;
2989- const canonicalShape = normalizeSqliteSchemaShapeSql ( collectSqliteSchemaShape ( created . db ) ) ;
2990- closeOpenClawAgentDatabasesForTest ( ) ;
2991- closeOpenClawStateDatabaseForTest ( ) ;
3050+ const databasePath = materializeCurrentWorkerAgentDatabase ( stateDir ) ;
3051+ const canonicalShape = normalizeSqliteSchemaShapeSql (
3052+ createSqliteSchemaShapeFromSql ( new URL ( "./openclaw-agent-schema.sql" , import . meta. url ) ) ,
3053+ ) ;
29923054
29933055 const { DatabaseSync } = requireNodeSqlite ( ) ;
29943056 const drifted = new DatabaseSync ( databasePath ) ;
@@ -3011,9 +3073,7 @@ describe("openclaw agent database", () => {
30113073 it ( "repairs physical ordinary-index drift before cold-open reads" , ( ) => {
30123074 const stateDir = createTempStateDir ( ) ;
30133075 const env = { OPENCLAW_STATE_DIR : stateDir } ;
3014- const databasePath = openOpenClawAgentDatabase ( { agentId : "worker-1" , env } ) . path ;
3015- closeOpenClawAgentDatabasesForTest ( ) ;
3016- closeOpenClawStateDatabaseForTest ( ) ;
3076+ const databasePath = materializeCurrentWorkerAgentDatabase ( stateDir ) ;
30173077 createCacheExpiryIndexPhysicalDrift ( databasePath ) ;
30183078
30193079 const reopened = openOpenClawAgentDatabase ( { agentId : "worker-1" , env } ) ;
@@ -3087,9 +3147,7 @@ describe("openclaw agent database", () => {
30873147 it ( "rejects a missing current-schema table instead of recreating it empty" , ( ) => {
30883148 const stateDir = createTempStateDir ( ) ;
30893149 const env = { OPENCLAW_STATE_DIR : stateDir } ;
3090- const databasePath = openOpenClawAgentDatabase ( { agentId : "worker-1" , env } ) . path ;
3091- closeOpenClawAgentDatabasesForTest ( ) ;
3092- closeOpenClawStateDatabaseForTest ( ) ;
3150+ const databasePath = materializeCurrentWorkerAgentDatabase ( stateDir ) ;
30933151
30943152 const { DatabaseSync } = requireNodeSqlite ( ) ;
30953153 const drifted = new DatabaseSync ( databasePath ) ;
@@ -3183,9 +3241,7 @@ describe("openclaw agent database", () => {
31833241 it ( "rejects an inline unique constraint hidden behind a SQLite autoindex" , ( ) => {
31843242 const stateDir = createTempStateDir ( ) ;
31853243 const env = { OPENCLAW_STATE_DIR : stateDir } ;
3186- const databasePath = openOpenClawAgentDatabase ( { agentId : "worker-1" , env } ) . path ;
3187- closeOpenClawAgentDatabasesForTest ( ) ;
3188- closeOpenClawStateDatabaseForTest ( ) ;
3244+ const databasePath = materializeCurrentWorkerAgentDatabase ( stateDir ) ;
31893245
31903246 const { DatabaseSync } = requireNodeSqlite ( ) ;
31913247 const drifted = new DatabaseSync ( databasePath ) ;
@@ -3235,9 +3291,7 @@ describe("openclaw agent database", () => {
32353291 it ( "rejects primary-key collation drift in a current-schema table" , ( ) => {
32363292 const stateDir = createTempStateDir ( ) ;
32373293 const env = { OPENCLAW_STATE_DIR : stateDir } ;
3238- const databasePath = openOpenClawAgentDatabase ( { agentId : "worker-1" , env } ) . path ;
3239- closeOpenClawAgentDatabasesForTest ( ) ;
3240- closeOpenClawStateDatabaseForTest ( ) ;
3294+ const databasePath = materializeCurrentWorkerAgentDatabase ( stateDir ) ;
32413295
32423296 const { DatabaseSync } = requireNodeSqlite ( ) ;
32433297 const drifted = new DatabaseSync ( databasePath ) ;
@@ -3259,9 +3313,7 @@ describe("openclaw agent database", () => {
32593313 it ( "rejects a partially missing lazy board schema" , ( ) => {
32603314 const stateDir = createTempStateDir ( ) ;
32613315 const env = { OPENCLAW_STATE_DIR : stateDir } ;
3262- const databasePath = openOpenClawAgentDatabase ( { agentId : "worker-1" , env } ) . path ;
3263- closeOpenClawAgentDatabasesForTest ( ) ;
3264- closeOpenClawStateDatabaseForTest ( ) ;
3316+ const databasePath = materializeCurrentWorkerAgentDatabase ( stateDir ) ;
32653317
32663318 const { DatabaseSync } = requireNodeSqlite ( ) ;
32673319 const drifted = new DatabaseSync ( databasePath ) ;
@@ -3276,10 +3328,7 @@ describe("openclaw agent database", () => {
32763328 it ( "rejects same-name transcript index drift when duplicate rows block repair" , ( ) => {
32773329 const stateDir = createTempStateDir ( ) ;
32783330 const env = { OPENCLAW_STATE_DIR : stateDir } ;
3279- const created = openOpenClawAgentDatabase ( { agentId : "worker-1" , env } ) ;
3280- const databasePath = created . path ;
3281- closeOpenClawAgentDatabasesForTest ( ) ;
3282- closeOpenClawStateDatabaseForTest ( ) ;
3331+ const databasePath = materializeCurrentWorkerAgentDatabase ( stateDir ) ;
32833332 createTranscriptIdempotencyIndexDrift ( databasePath , { duplicateRows : true } ) ;
32843333
32853334 expect ( ( ) => openOpenClawAgentDatabase ( { agentId : "worker-1" , env } ) ) . toThrow (
@@ -3826,9 +3875,7 @@ describe("openclaw agent database", () => {
38263875 it ( "rejects stale schema_meta indexes before writable initialization" , ( ) => {
38273876 const stateDir = createTempStateDir ( ) ;
38283877 const env = { OPENCLAW_STATE_DIR : stateDir } ;
3829- const databasePath = openOpenClawAgentDatabase ( { agentId : "worker-1" , env } ) . path ;
3830- closeOpenClawAgentDatabasesForTest ( ) ;
3831- closeOpenClawStateDatabaseForTest ( ) ;
3878+ const databasePath = materializeCurrentWorkerAgentDatabase ( stateDir ) ;
38323879 createUnsafeSchemaMetaIndexDrift ( databasePath ) ;
38333880
38343881 const { DatabaseSync } = requireNodeSqlite ( ) ;
@@ -3851,9 +3898,7 @@ describe("openclaw agent database", () => {
38513898 it ( "rejects unexpected unique indexes before writable initialization" , ( ) => {
38523899 const stateDir = createTempStateDir ( ) ;
38533900 const env = { OPENCLAW_STATE_DIR : stateDir } ;
3854- const databasePath = openOpenClawAgentDatabase ( { agentId : "worker-1" , env } ) . path ;
3855- closeOpenClawAgentDatabasesForTest ( ) ;
3856- closeOpenClawStateDatabaseForTest ( ) ;
3901+ const databasePath = materializeCurrentWorkerAgentDatabase ( stateDir ) ;
38573902
38583903 const { DatabaseSync } = requireNodeSqlite ( ) ;
38593904 const drifted = new DatabaseSync ( databasePath ) ;
@@ -3874,9 +3919,7 @@ describe("openclaw agent database", () => {
38743919 it ( "rejects unrelated current-schema index corruption before exposure" , ( ) => {
38753920 const stateDir = createTempStateDir ( ) ;
38763921 const env = { OPENCLAW_STATE_DIR : stateDir } ;
3877- const databasePath = openOpenClawAgentDatabase ( { agentId : "worker-1" , env } ) . path ;
3878- closeOpenClawAgentDatabasesForTest ( ) ;
3879- closeOpenClawStateDatabaseForTest ( ) ;
3922+ const databasePath = materializeCurrentWorkerAgentDatabase ( stateDir ) ;
38803923 createUnsafeIndexDrift ( databasePath ) ;
38813924
38823925 expect ( ( ) => openOpenClawAgentDatabase ( { agentId : "worker-1" , env } ) ) . toThrow (
@@ -3991,10 +4034,7 @@ describe("openclaw agent database", () => {
39914034 it ( "rejects current-schema foreign-key violations before exposure" , ( ) => {
39924035 const stateDir = createTempStateDir ( ) ;
39934036 const env = { OPENCLAW_STATE_DIR : stateDir } ;
3994- const created = openOpenClawAgentDatabase ( { agentId : "worker-1" , env } ) ;
3995- const databasePath = created . path ;
3996- closeOpenClawAgentDatabasesForTest ( ) ;
3997- closeOpenClawStateDatabaseForTest ( ) ;
4037+ const databasePath = materializeCurrentWorkerAgentDatabase ( stateDir ) ;
39984038
39994039 const { DatabaseSync } = requireNodeSqlite ( ) ;
40004040 const corrupted = new DatabaseSync ( databasePath ) ;
0 commit comments