@@ -2,7 +2,9 @@ import fs from "node:fs";
22import path from "node:path" ;
33import { afterEach , describe , expect , it , vi } from "vitest" ;
44import { useAutoCleanupTempDirTracker } from "../../test/helpers/temp-dir.js" ;
5+ import { runDoctorConfigPreflight } from "../commands/doctor-config-preflight.js" ;
56import { runDoctorStateSqliteCompact } from "../commands/doctor-state-sqlite-compact.js" ;
7+ import { planPristineStartupStateMigrations } from "../commands/doctor/shared/pristine-startup-state.js" ;
68import {
79 readConfigHealthStateFromStore ,
810 writeConfigHealthStateToStore ,
@@ -11,7 +13,8 @@ import { resolveGatewayLockDir } from "../config/paths.js";
1113import { resolvePathViaExistingAncestorSync } from "../infra/boundary-path.js" ;
1214import { sha256HexPrefixCore } from "../infra/crypto-digest.js" ;
1315import { requireNodeSqlite , resolveImmutableSqliteFileUri } from "../infra/node-sqlite.js" ;
14- import { withEnv } from "../test-utils/env.js" ;
16+ import * as sqliteReadonlyLocation from "../infra/sqlite-readonly-location.js" ;
17+ import { withEnv , withEnvAsync } from "../test-utils/env.js" ;
1518import { withOpenClawStateStartupMigrationCheckpointDatabase } from "./openclaw-state-db-startup-checkpoint.js" ;
1619import {
1720 closeOpenClawStateDatabaseForTest ,
@@ -25,6 +28,7 @@ import {
2528import { resolveOpenClawStateDirForDatabasePath } from "./openclaw-state-db.paths.js" ;
2629import { claimOpenClawStateOwnership } from "./openclaw-state-ownership-operations.js" ;
2730import {
31+ assertOpenClawStateWriteAllowedAtPath ,
2832 inspectOpenClawStateOwnershipAtPath ,
2933 OpenClawStateOwnershipError ,
3034 OpenClawStateOwnershipMetadataError ,
@@ -118,14 +122,41 @@ function mockCoordinatorRollbackFailure(onRollback?: () => void) {
118122}
119123
120124describe ( "external shared-state ownership" , ( ) => {
121- it ( "returns unowned for a missing path without creating its state tree" , ( ) => {
125+ it ( "returns unowned for a missing path without creating its state tree" , async ( ) => {
122126 const rootDir = tempDirs . make ( "openclaw-state-ownership-missing-" ) ;
123127 const missingStateDir = path . join ( rootDir , "missing-state" ) ;
124128 const databasePath = path . join ( missingStateDir , "state" , "openclaw.sqlite" ) ;
125129
126130 expect ( fs . existsSync ( missingStateDir ) ) . toBe ( false ) ;
127131 expect ( inspectOpenClawStateOwnershipAtPath ( databasePath ) ) . toBeNull ( ) ;
128132 expect ( fs . existsSync ( missingStateDir ) ) . toBe ( false ) ;
133+ await assertOpenClawStateWriteAllowedAtPath ( { databasePath } ) ;
134+ expect ( fs . existsSync ( missingStateDir ) ) . toBe ( false ) ;
135+ } ) ;
136+
137+ it ( "keeps missing-database admission eligible for pristine startup" , async ( ) => {
138+ const home = tempDirs . make ( "openclaw-state-ownership-pristine-" ) ;
139+ const stateDir = path . join ( home , "state" ) ;
140+ const configPath = path . join ( stateDir , "openclaw.json" ) ;
141+ const databasePath = path . join ( stateDir , "state" , "openclaw.sqlite" ) ;
142+ const env = {
143+ HOME : home ,
144+ OPENCLAW_CONFIG_PATH : configPath ,
145+ OPENCLAW_STATE_DIR : stateDir ,
146+ } ;
147+ fs . mkdirSync ( stateDir , { recursive : true } ) ;
148+ fs . writeFileSync ( configPath , "{}\n" ) ;
149+
150+ expect ( planPristineStartupStateMigrations ( env ) ) . toEqual ( {
151+ skipAllStateMigrations : true ,
152+ skipCoreStateMigrations : true ,
153+ } ) ;
154+ await assertOpenClawStateWriteAllowedAtPath ( { databasePath, env } ) ;
155+ expect ( fs . readdirSync ( stateDir ) ) . toEqual ( [ "openclaw.json" ] ) ;
156+ expect ( planPristineStartupStateMigrations ( env ) ) . toEqual ( {
157+ skipAllStateMigrations : true ,
158+ skipCoreStateMigrations : true ,
159+ } ) ;
129160 } ) ;
130161
131162 it ( "preserves ordinary unowned database behavior" , ( ) => {
@@ -135,6 +166,37 @@ describe("external shared-state ownership", () => {
135166 expect ( inspectOpenClawStateOwnershipAtPath ( database . path ) ) . toBeNull ( ) ;
136167 } ) ;
137168
169+ it ( "checks Doctor startup admission without staging a public snapshot" , async ( ) => {
170+ const fixture = claimFixture ( ) ;
171+ const home = tempDirs . make ( "openclaw-state-ownership-doctor-" ) ;
172+ const snapshotStaging = vi . spyOn ( sqliteReadonlyLocation , "prepareSqliteReadOnlyLocationSync" ) ;
173+ const runPreflight = async ( env : NodeJS . ProcessEnv ) =>
174+ await withEnvAsync (
175+ {
176+ HOME : home ,
177+ OPENCLAW_CONFIG_PATH : path . join ( home , "openclaw.json" ) ,
178+ OPENCLAW_PROFILE : undefined ,
179+ OPENCLAW_STATE_DIR : env . OPENCLAW_STATE_DIR ,
180+ OPENCLAW_SUPERVISOR_MODE : env . OPENCLAW_SUPERVISOR_MODE ,
181+ } ,
182+ async ( ) =>
183+ await runDoctorConfigPreflight ( {
184+ invalidConfigNote : false ,
185+ migrateLegacyConfig : false ,
186+ migrateState : true ,
187+ observe : false ,
188+ skipPristineStartupStateMigrations : true ,
189+ } ) ,
190+ ) ;
191+ try {
192+ await expect ( runPreflight ( fixture . unmarkedEnv ) ) . rejects . toThrow ( OpenClawStateOwnershipError ) ;
193+ await expect ( runPreflight ( fixture . externalEnv ) ) . resolves . toBeDefined ( ) ;
194+ expect ( snapshotStaging ) . not . toHaveBeenCalled ( ) ;
195+ } finally {
196+ snapshotStaging . mockRestore ( ) ;
197+ }
198+ } ) ;
199+
138200 it ( "reads ownership from a WAL when the SHM index is absent" , ( ) => {
139201 const env = createEnv ( true ) ;
140202 const databasePath = openOpenClawStateDatabase ( { env } ) . path ;
@@ -167,6 +229,44 @@ describe("external shared-state ownership", () => {
167229 }
168230 } ) ;
169231
232+ it ( "rejects unmarked WAL ownership without modifying the SQLite family" , async ( ) => {
233+ const env = createEnv ( true ) ;
234+ const databasePath = openOpenClawStateDatabase ( { env } ) . path ;
235+ closeOpenClawStateDatabaseForTest ( ) ;
236+ const { DatabaseSync } = requireNodeSqlite ( ) ;
237+ const writer = new DatabaseSync ( databasePath ) ;
238+ const ownership = {
239+ version : 1 ,
240+ mode : "external" ,
241+ managerId : "wal-only-manager" ,
242+ claimedAt : 1 ,
243+ } as const ;
244+ const copyDir = tempDirs . make ( "openclaw-state-ownership-wal-rejection-" ) ;
245+ const copyPath = path . join ( copyDir , "openclaw.sqlite" ) ;
246+ try {
247+ writer . exec ( "PRAGMA journal_mode = WAL; PRAGMA wal_autocheckpoint = 0;" ) ;
248+ writer
249+ . prepare (
250+ "INSERT INTO config_machine_state (state_key, value_json, updated_at_ms) VALUES (?, ?, ?)" ,
251+ )
252+ . run ( STATE_SUPERVISION_KEY , JSON . stringify ( ownership ) , ownership . claimedAt ) ;
253+ fs . copyFileSync ( databasePath , copyPath ) ;
254+ fs . copyFileSync ( `${ databasePath } -wal` , `${ copyPath } -wal` ) ;
255+ } finally {
256+ writer . close ( ) ;
257+ }
258+
259+ expect ( fs . existsSync ( `${ copyPath } -shm` ) ) . toBe ( false ) ;
260+ const before = snapshotSqliteFamily ( copyPath ) ;
261+ await expect (
262+ assertOpenClawStateWriteAllowedAtPath ( {
263+ databasePath : copyPath ,
264+ env : withoutExternalMarker ( env ) ,
265+ } ) ,
266+ ) . rejects . toThrow ( OpenClawStateOwnershipError ) ;
267+ expect ( snapshotSqliteFamily ( copyPath ) ) . toEqual ( before ) ;
268+ } ) ;
269+
170270 it ( "observes committed ownership that is still resident in the live WAL" , ( ) => {
171271 const env = createEnv ( ) ;
172272 const databasePath = openOpenClawStateDatabase ( { env } ) . path ;
0 commit comments