@@ -4,7 +4,6 @@ import os from "node:os";
44import path from "node:path" ;
55import type { DatabaseSync } from "node:sqlite" ;
66import { toStringifiedError } from "@openclaw/normalization-core/error-coercion" ;
7- import { readStringValue } from "@openclaw/normalization-core/string-coerce" ;
87import * as tar from "tar" ;
98import { loadSqliteVecExtension } from "../../packages/memory-host-sdk/src/engine-storage.js" ;
109import {
@@ -18,45 +17,20 @@ import { formatDiskSpaceBytes, tryReadDiskSpace } from "../infra/disk-space.js";
1817import { openNodeSqliteDatabase } from "../infra/node-sqlite.js" ;
1918import { assertSqliteIntegrity } from "../infra/sqlite-integrity.js" ;
2019import { type RuntimeEnv , writeRuntimeJson } from "../runtime.js" ;
21- import { isRecord , resolveUserPath } from "../utils.js" ;
20+ import { resolveUserPath } from "../utils.js" ;
2221import { BACKUP_MAX_DECOMPRESSION_RATIO , buildBackupArchivePath } from "./backup-shared.js" ;
22+ import {
23+ type BackupManifest ,
24+ isRootBackupManifestEntry ,
25+ parseBackupManifest ,
26+ verifyBackupManifestEntries ,
27+ } from "./backup-verify-manifest.js" ;
2328
2429const MAX_MANIFEST_BYTES = 1024 * 1024 ;
2530const MAX_SQLITE_SNAPSHOT_EXTRACT_BYTES = 64 * 1024 * 1024 * 1024 ;
2631const SQLITE_SNAPSHOT_FREE_SPACE_RESERVE_BYTES = 256 * 1024 * 1024 ;
2732const SQLITE_SNAPSHOT_SIDECAR_SUFFIXES = [ "-wal" , "-shm" , "-journal" ] as const ;
2833
29- type BackupManifestAsset = {
30- kind : string ;
31- sourcePath : string ;
32- archivePath : string ;
33- } ;
34-
35- type BackupManifest = {
36- schemaVersion : number ;
37- createdAt : string ;
38- archiveRoot : string ;
39- runtimeVersion : string ;
40- platform : string ;
41- nodeVersion : string ;
42- options ?: {
43- includeWorkspace ?: boolean ;
44- } ;
45- paths ?: {
46- stateDir ?: string ;
47- configPath ?: string ;
48- oauthDir ?: string ;
49- workspaceDirs ?: string [ ] ;
50- } ;
51- assets : BackupManifestAsset [ ] ;
52- skipped ?: Array < {
53- kind ?: string ;
54- sourcePath ?: string ;
55- reason ?: string ;
56- coveredBy ?: string ;
57- } > ;
58- } ;
59-
6034type BackupVerifyOptions = {
6135 archive : string ;
6236 json ?: boolean ;
@@ -93,81 +67,6 @@ type SqliteSnapshotEntry = NormalizedArchiveEntry & {
9367
9468type ExpectedSqliteRole = "agent" | "global" ;
9569
96- function parseManifest ( raw : string ) : BackupManifest {
97- let parsed : unknown ;
98- try {
99- parsed = JSON . parse ( raw ) ;
100- } catch ( err ) {
101- throw new Error ( "Backup manifest is not valid JSON." , { cause : err } ) ;
102- }
103-
104- if ( ! isRecord ( parsed ) ) {
105- throw new Error ( "Backup manifest must be an object." ) ;
106- }
107- if ( parsed . schemaVersion !== 1 ) {
108- throw new Error ( `Unsupported backup manifest schemaVersion: ${ String ( parsed . schemaVersion ) } ` ) ;
109- }
110- if ( typeof parsed . archiveRoot !== "string" || ! parsed . archiveRoot . trim ( ) ) {
111- throw new Error ( "Backup manifest is missing archiveRoot." ) ;
112- }
113- if ( typeof parsed . createdAt !== "string" || ! parsed . createdAt . trim ( ) ) {
114- throw new Error ( "Backup manifest is missing createdAt." ) ;
115- }
116- if ( ! Array . isArray ( parsed . assets ) ) {
117- throw new Error ( "Backup manifest is missing assets." ) ;
118- }
119-
120- const assets : BackupManifestAsset [ ] = [ ] ;
121- for ( const asset of parsed . assets ) {
122- if ( ! isRecord ( asset ) ) {
123- throw new Error ( "Backup manifest contains a non-object asset." ) ;
124- }
125- if ( typeof asset . kind !== "string" || ! asset . kind . trim ( ) ) {
126- throw new Error ( "Backup manifest asset is missing kind." ) ;
127- }
128- if ( typeof asset . sourcePath !== "string" || ! asset . sourcePath . trim ( ) ) {
129- throw new Error ( "Backup manifest asset is missing sourcePath." ) ;
130- }
131- if ( typeof asset . archivePath !== "string" || ! asset . archivePath . trim ( ) ) {
132- throw new Error ( "Backup manifest asset is missing archivePath." ) ;
133- }
134- assets . push ( {
135- kind : asset . kind ,
136- sourcePath : asset . sourcePath ,
137- archivePath : asset . archivePath ,
138- } ) ;
139- }
140-
141- return {
142- schemaVersion : 1 ,
143- archiveRoot : parsed . archiveRoot ,
144- createdAt : parsed . createdAt ,
145- runtimeVersion :
146- typeof parsed . runtimeVersion === "string" && parsed . runtimeVersion . trim ( )
147- ? parsed . runtimeVersion
148- : "unknown" ,
149- platform : typeof parsed . platform === "string" ? parsed . platform : "unknown" ,
150- nodeVersion : typeof parsed . nodeVersion === "string" ? parsed . nodeVersion : "unknown" ,
151- options : isRecord ( parsed . options )
152- ? { includeWorkspace : parsed . options . includeWorkspace as boolean | undefined }
153- : undefined ,
154- paths : isRecord ( parsed . paths )
155- ? {
156- stateDir : readStringValue ( parsed . paths . stateDir ) ,
157- configPath : readStringValue ( parsed . paths . configPath ) ,
158- oauthDir : readStringValue ( parsed . paths . oauthDir ) ,
159- workspaceDirs : Array . isArray ( parsed . paths . workspaceDirs )
160- ? parsed . paths . workspaceDirs . filter (
161- ( entry ) : entry is string => typeof entry === "string" ,
162- )
163- : undefined ,
164- }
165- : undefined ,
166- assets,
167- skipped : Array . isArray ( parsed . skipped ) ? parsed . skipped : undefined ,
168- } ;
169- }
170-
17170async function listArchiveEntries ( archivePath : string ) : Promise < ArchiveEntry [ ] > {
17271 const entries : ArchiveEntry [ ] = [ ] ;
17372 await tar . t ( {
@@ -215,43 +114,6 @@ async function extractManifest(params: {
215114 return content . toString ( "utf8" ) ;
216115}
217116
218- function isRootManifestEntry ( entryPath : string ) : boolean {
219- const parts = entryPath . split ( "/" ) ;
220- return parts . length === 2 && parts [ 0 ] !== "" && parts [ 1 ] === "manifest.json" ;
221- }
222-
223- function verifyManifestAgainstEntries ( manifest : BackupManifest , entries : Set < string > ) : void {
224- const archiveRoot = normalizeArchiveRoot ( manifest . archiveRoot ) ;
225- const manifestEntryPath = path . posix . join ( archiveRoot , "manifest.json" ) ;
226- const normalizedEntries = [ ...entries ] ;
227- const normalizedEntrySet = new Set ( normalizedEntries ) ;
228-
229- if ( ! normalizedEntrySet . has ( manifestEntryPath ) ) {
230- throw new Error ( `Archive is missing manifest entry: ${ manifestEntryPath } ` ) ;
231- }
232-
233- for ( const entry of normalizedEntries ) {
234- if ( ! isArchivePathWithin ( entry , archiveRoot ) ) {
235- throw new Error ( `Archive entry is outside the declared archive root: ${ entry } ` ) ;
236- }
237- }
238-
239- const payloadRoot = path . posix . join ( archiveRoot , "payload" ) ;
240- for ( const asset of manifest . assets ) {
241- const assetArchivePath = normalizeArchivePath ( asset . archivePath , "Backup manifest asset path" ) ;
242- if ( ! isArchivePathWithin ( assetArchivePath , payloadRoot ) ) {
243- throw new Error ( `Manifest asset path is outside payload root: ${ asset . archivePath } ` ) ;
244- }
245- const exact = normalizedEntrySet . has ( assetArchivePath ) ;
246- const nested = normalizedEntries . some (
247- ( entry ) => entry !== assetArchivePath && isArchivePathWithin ( entry , assetArchivePath ) ,
248- ) ;
249- if ( ! exact && ! nested ) {
250- throw new Error ( `Archive is missing payload for manifest asset: ${ assetArchivePath } ` ) ;
251- }
252- }
253- }
254-
255117function verifyHardlinkTargetsAgainstArchiveRoot (
256118 hardlinkTargets : Array < { entryPath : string ; normalized : string } > ,
257119 archiveRoot : string ,
@@ -705,7 +567,7 @@ export async function verifyBackupArchive(archive: string): Promise<BackupVerify
705567 . map ( ( entry ) => ( { entryPath : entry . path , linkpath : entry . linkpath } ) ) ;
706568 const normalizedEntrySet = new Set ( entries . map ( ( entry ) => entry . normalized ) ) ;
707569
708- const manifestMatches = entries . filter ( ( entry ) => isRootManifestEntry ( entry . normalized ) ) ;
570+ const manifestMatches = entries . filter ( ( entry ) => isRootBackupManifestEntry ( entry . normalized ) ) ;
709571 if ( manifestMatches . length !== 1 ) {
710572 throw new Error ( `Expected exactly one backup manifest entry, found ${ manifestMatches . length } .` ) ;
711573 }
@@ -725,8 +587,8 @@ export async function verifyBackupArchive(archive: string): Promise<BackupVerify
725587 }
726588
727589 const manifestRaw = await extractManifest ( { archivePath, manifestEntryPath } ) ;
728- const manifest = parseManifest ( manifestRaw ) ;
729- verifyManifestAgainstEntries ( manifest , normalizedEntrySet ) ;
590+ const manifest = parseBackupManifest ( manifestRaw ) ;
591+ verifyBackupManifestEntries ( manifest , normalizedEntrySet ) ;
730592 verifyHardlinkTargetsAgainstArchiveRoot (
731593 hardlinkTargets ,
732594 manifest . archiveRoot ,
0 commit comments