@@ -8,7 +8,9 @@ import * as tar from "tar";
88import { describe , expect , it , vi } from "vitest" ;
99import { saveAuthProfileStore } from "../agents/auth-profiles/store.js" ;
1010import { backupVerifyCommand } from "../commands/backup-verify.js" ;
11+ import { isPathWithin } from "../commands/cleanup-utils.js" ;
1112import { CONFIG_AUDIT_MAX_ENTRIES , CONFIG_AUDIT_SCOPE } from "../config/io.audit.js" ;
13+ import { resolveGatewayLockDir } from "../config/paths.js" ;
1214import type { RuntimeEnv } from "../runtime.js" ;
1315import { closeOpenClawAgentDatabasesForTest } from "../state/openclaw-agent-db.js" ;
1416import {
@@ -33,6 +35,7 @@ import {
3335import { writeTarArchiveWithRetry } from "./backup-tar-retry.js" ;
3436import { isVolatileBackupPath } from "./backup-volatile-filter.js" ;
3537import { createBackupVolatileStatCache } from "./backup-volatile-stat-cache.js" ;
38+ import { acquireGatewayLock } from "./gateway-lock.js" ;
3639import { requireNodeSqlite } from "./node-sqlite.js" ;
3740import { createSqliteAuditRecordStore } from "./sqlite-audit-record-store.js" ;
3841import { detectLegacyAuditLogs , migrateLegacyAuditLogs } from "./state-migrations.audit-logs.js" ;
@@ -1659,7 +1662,7 @@ describe("createBackupArchive", () => {
16591662 ) ;
16601663 } ) ;
16611664
1662- it ( "snapshots nested live SQLite databases with transaction continuity" , async ( ) => {
1665+ it ( "snapshots lock-named plugin SQLite databases with transaction continuity" , async ( ) => {
16631666 await withOpenClawTestState (
16641667 {
16651668 layout : "state-only" ,
@@ -1669,7 +1672,7 @@ describe("createBackupArchive", () => {
16691672 async ( state ) => {
16701673 const outputDir = state . path ( "backups" ) ;
16711674 const extractDir = state . path ( "extract" ) ;
1672- const dbPath = state . statePath ( "plugins" , "dedicated" , "live .sqlite" ) ;
1675+ const dbPath = state . statePath ( "plugins" , "dedicated" , "cache.lock .sqlite" ) ;
16731676 await fs . mkdir ( path . dirname ( dbPath ) , { recursive : true } ) ;
16741677 await fs . mkdir ( outputDir , { recursive : true } ) ;
16751678 await fs . mkdir ( extractDir , { recursive : true } ) ;
@@ -1714,13 +1717,13 @@ describe("createBackupArchive", () => {
17141717 } ) ;
17151718 const entries = await listArchiveEntries ( result . archivePath ) ;
17161719 const archivedDbEntries = entries . filter ( ( entry ) =>
1717- entry . endsWith ( "/state/plugins/dedicated/live .sqlite" ) ,
1720+ entry . endsWith ( "/state/plugins/dedicated/cache.lock .sqlite" ) ,
17181721 ) ;
17191722 expect ( archivedDbEntries ) . toHaveLength ( 1 ) ;
17201723 for ( const suffix of [ "-wal" , "-shm" , "-journal" ] ) {
17211724 expect (
17221725 entries . some ( ( entry ) =>
1723- entry . endsWith ( `/state/plugins/dedicated/live .sqlite${ suffix } ` ) ,
1726+ entry . endsWith ( `/state/plugins/dedicated/cache.lock .sqlite${ suffix } ` ) ,
17241727 ) ,
17251728 suffix ,
17261729 ) . toBe ( false ) ;
@@ -2025,6 +2028,137 @@ describe("createBackupArchive", () => {
20252028 ) ;
20262029 } ) ;
20272030
2031+ it ( "backs up durable SQLite while live gateway coordinators remain held under state" , async ( ) => {
2032+ await withOpenClawTestState (
2033+ {
2034+ layout : "state-only" ,
2035+ prefix : "openclaw-backup-gateway-lock-sqlite-" ,
2036+ scenario : "minimal" ,
2037+ } ,
2038+ async ( state ) => {
2039+ const outputDir = state . path ( "backups" ) ;
2040+ const extractDir = state . path ( "extract" ) ;
2041+ const lockDir = resolveGatewayLockDir ( ( ) => state . statePath ( "tmp" ) ) ;
2042+ const pluginDbPath = state . statePath ( "plugins" , "dedicated" , "durable.sqlite" ) ;
2043+ const producerShapedDbPath = state . statePath (
2044+ "plugins" ,
2045+ "dedicated" ,
2046+ "gateway.12345678.lock.sqlite" ,
2047+ ) ;
2048+ const colocatedDbPath = path . join ( lockDir , "retained.sqlite" ) ;
2049+ await fs . mkdir ( outputDir , { recursive : true } ) ;
2050+ await fs . mkdir ( extractDir , { recursive : true } ) ;
2051+ await fs . mkdir ( path . dirname ( pluginDbPath ) , { recursive : true } ) ;
2052+ await fs . mkdir ( lockDir , { recursive : true } ) ;
2053+
2054+ const sqlite = requireNodeSqlite ( ) ;
2055+ for ( const [ databasePath , value ] of [
2056+ [ pluginDbPath , "plugin-state" ] ,
2057+ [ producerShapedDbPath , "producer-shaped-state" ] ,
2058+ [ colocatedDbPath , "colocated-state" ] ,
2059+ ] as const ) {
2060+ const database = new sqlite . DatabaseSync ( databasePath ) ;
2061+ try {
2062+ database . exec ( "CREATE TABLE durable_state (value TEXT NOT NULL)" ) ;
2063+ database . prepare ( "INSERT INTO durable_state (value) VALUES (?)" ) . run ( value ) ;
2064+ } finally {
2065+ database . close ( ) ;
2066+ }
2067+ }
2068+
2069+ const gatewayLock = await acquireGatewayLock ( {
2070+ allowInTests : true ,
2071+ env : state . env ,
2072+ lockDir,
2073+ timeoutMs : 100 ,
2074+ } ) ;
2075+ if ( ! gatewayLock ) {
2076+ throw new Error ( "expected test gateway lock" ) ;
2077+ }
2078+ expect ( isPathWithin ( resolveGatewayLockDir ( ) , state . stateDir ) ) . toBe ( false ) ;
2079+ const gatewayCoordinatorPaths = [
2080+ `${ gatewayLock . lockPath } .sqlite` ,
2081+ `${ gatewayLock . stateLockPath } .sqlite` ,
2082+ ] ;
2083+ const extraTransientPaths = [
2084+ path . join ( lockDir , "device-identity.12345678.lock.sqlite" ) ,
2085+ state . statePath ( "memory" , "main.sqlite.reindex-lock.sqlite" ) ,
2086+ ] ;
2087+
2088+ try {
2089+ for ( const transientPath of [ ...gatewayCoordinatorPaths , ...extraTransientPaths ] ) {
2090+ await fs . mkdir ( path . dirname ( transientPath ) , { recursive : true } ) ;
2091+ if ( ! gatewayCoordinatorPaths . includes ( transientPath ) ) {
2092+ await fs . writeFile ( transientPath , "transient coordinator database" ) ;
2093+ }
2094+ for ( const suffix of [ "-wal" , "-shm" , "-journal" ] ) {
2095+ await fs . writeFile ( `${ transientPath } ${ suffix } ` , "transient coordinator sidecar" ) ;
2096+ }
2097+ }
2098+
2099+ const result = await createBackupArchive ( {
2100+ output : outputDir ,
2101+ includeWorkspace : false ,
2102+ nowMs : Date . UTC ( 2026 , 7 , 5 , 12 , 0 , 0 ) ,
2103+ } ) ;
2104+ const entries = await listArchiveEntries ( result . archivePath ) ;
2105+ for ( const transientPath of [ ...gatewayCoordinatorPaths , ...extraTransientPaths ] ) {
2106+ const relativeTransientPath = path
2107+ . relative ( state . stateDir , transientPath )
2108+ . split ( path . sep )
2109+ . join ( "/" ) ;
2110+ for ( const suffix of [ "" , "-wal" , "-shm" , "-journal" ] ) {
2111+ expect (
2112+ entries . some ( ( entry ) => entry . endsWith ( `/state/${ relativeTransientPath } ${ suffix } ` ) ) ,
2113+ `${ relativeTransientPath } ${ suffix } ` ,
2114+ ) . toBe ( false ) ;
2115+ }
2116+ }
2117+ expect (
2118+ entries . some ( ( entry ) => entry . endsWith ( "/state/plugins/dedicated/durable.sqlite" ) ) ,
2119+ ) . toBe ( true ) ;
2120+ expect (
2121+ entries . some ( ( entry ) =>
2122+ entry . endsWith ( "/state/plugins/dedicated/gateway.12345678.lock.sqlite" ) ,
2123+ ) ,
2124+ ) . toBe ( true ) ;
2125+ expect (
2126+ entries . some ( ( entry ) => entry . endsWith ( `/${ path . basename ( lockDir ) } /retained.sqlite` ) ) ,
2127+ ) . toBe ( true ) ;
2128+
2129+ const runtime : RuntimeEnv = { log : vi . fn ( ) , error : vi . fn ( ) , exit : vi . fn ( ) } ;
2130+ await expect (
2131+ backupVerifyCommand ( runtime , { archive : result . archivePath } ) ,
2132+ ) . resolves . toMatchObject ( { ok : true } ) ;
2133+
2134+ await tar . x ( { file : result . archivePath , gzip : true , cwd : extractDir } ) ;
2135+ for ( const [ entrySuffix , value ] of [
2136+ [ "/state/plugins/dedicated/durable.sqlite" , "plugin-state" ] ,
2137+ [ "/state/plugins/dedicated/gateway.12345678.lock.sqlite" , "producer-shaped-state" ] ,
2138+ [ `/${ path . basename ( lockDir ) } /retained.sqlite` , "colocated-state" ] ,
2139+ ] as const ) {
2140+ const archivedEntry = expectDefined (
2141+ entries . find ( ( entry ) => entry . endsWith ( entrySuffix ) ) ,
2142+ `archive entry ending with ${ entrySuffix } ` ,
2143+ ) ;
2144+ const archivedDb = new sqlite . DatabaseSync ( path . join ( extractDir , archivedEntry ) , {
2145+ readOnly : true ,
2146+ } ) ;
2147+ try {
2148+ expect ( archivedDb . prepare ( "SELECT value FROM durable_state" ) . get ( ) ) . toEqual ( {
2149+ value,
2150+ } ) ;
2151+ } finally {
2152+ archivedDb . close ( ) ;
2153+ }
2154+ }
2155+ } finally {
2156+ await gatewayLock . release ( ) ;
2157+ }
2158+ } ,
2159+ ) ;
2160+ } ) ;
2161+
20282162 it ( "preserves noncanonical symlinked SQLite paths without dereferencing them" , async ( ) => {
20292163 if ( process . platform === "win32" ) {
20302164 return ;
0 commit comments