1- // Sandbox registry tests cover legacy registry migration, ordering,
2- // and race-safety for container/browser runtime records.
1+ // Sandbox registry tests cover SQLite ordering and race safety for container/browser runtime records.
32import fs from "node:fs/promises" ;
43import path from "node:path" ;
54import { afterAll , afterEach , describe , expect , it , vi } from "vitest" ;
65
7- const {
8- TEST_STATE_DIR ,
9- PREVIOUS_OPENCLAW_STATE_DIR ,
10- SANDBOX_REGISTRY_PATH ,
11- SANDBOX_BROWSER_REGISTRY_PATH ,
12- SANDBOX_CONTAINERS_DIR ,
13- SANDBOX_BROWSERS_DIR ,
14- } = vi . hoisted ( ( ) => {
6+ const { TEST_STATE_DIR , PREVIOUS_OPENCLAW_STATE_DIR , SANDBOX_REGISTRY_PATH } = vi . hoisted ( ( ) => {
157 const nodePath = require ( "node:path" ) ;
168 const { mkdtempSync } = require ( "node:fs" ) ;
179 const { tmpdir } = require ( "node:os" ) ;
@@ -23,25 +15,12 @@ const {
2315 TEST_STATE_DIR : baseDir ,
2416 PREVIOUS_OPENCLAW_STATE_DIR : previousStateDir ,
2517 SANDBOX_REGISTRY_PATH : nodePath . join ( baseDir , "containers.json" ) ,
26- SANDBOX_BROWSER_REGISTRY_PATH : nodePath . join ( baseDir , "browsers.json" ) ,
27- SANDBOX_CONTAINERS_DIR : nodePath . join ( baseDir , "containers" ) ,
28- SANDBOX_BROWSERS_DIR : nodePath . join ( baseDir , "browsers" ) ,
2918 } ;
3019} ) ;
3120
32- vi . mock ( "./constants.js" , ( ) => ( {
33- SANDBOX_STATE_DIR : TEST_STATE_DIR ,
34- SANDBOX_REGISTRY_PATH ,
35- SANDBOX_BROWSER_REGISTRY_PATH ,
36- SANDBOX_CONTAINERS_DIR ,
37- SANDBOX_BROWSERS_DIR ,
38- } ) ) ;
39-
4021import { closeOpenClawStateDatabaseForTest } from "../../state/openclaw-state-db.js" ;
4122import { deleteTestEnvValue , setTestEnvValue } from "../../test-utils/env.js" ;
42- import { hashTextSha256 } from "./hash.js" ;
4323import {
44- migrateLegacySandboxRegistryFiles ,
4524 readBrowserRegistry ,
4625 readRegisteredSandboxRuntimeIds ,
4726 readRegistry ,
@@ -54,25 +33,11 @@ import {
5433
5534type SandboxBrowserRegistryEntry = import ( "./registry.js" ) . SandboxBrowserRegistryEntry ;
5635type SandboxRegistryEntry = import ( "./registry.js" ) . SandboxRegistryEntry ;
57- type MigrationResult = Awaited < ReturnType < typeof migrateLegacySandboxRegistryFiles > > [ number ] ;
58-
59- async function seedMalformedContainerRegistry ( payload : string ) {
60- await fs . writeFile ( SANDBOX_REGISTRY_PATH , payload , "utf-8" ) ;
61- }
62-
63- async function seedMalformedBrowserRegistry ( payload : string ) {
64- await fs . writeFile ( SANDBOX_BROWSER_REGISTRY_PATH , payload , "utf-8" ) ;
65- }
6636
6737afterEach ( async ( ) => {
6838 closeOpenClawStateDatabaseForTest ( ) ;
6939 await fs . rm ( path . join ( TEST_STATE_DIR , "state" ) , { recursive : true , force : true } ) ;
70- await fs . rm ( SANDBOX_CONTAINERS_DIR , { recursive : true , force : true } ) ;
71- await fs . rm ( SANDBOX_BROWSERS_DIR , { recursive : true , force : true } ) ;
7240 await fs . rm ( SANDBOX_REGISTRY_PATH , { force : true } ) ;
73- await fs . rm ( SANDBOX_BROWSER_REGISTRY_PATH , { force : true } ) ;
74- await fs . rm ( `${ SANDBOX_REGISTRY_PATH } .lock` , { force : true } ) ;
75- await fs . rm ( `${ SANDBOX_BROWSER_REGISTRY_PATH } .lock` , { force : true } ) ;
7641} ) ;
7742
7843afterAll ( async ( ) => {
@@ -110,48 +75,6 @@ function containerEntry(overrides: Partial<SandboxRegistryEntry> = {}): SandboxR
11075 } ;
11176}
11277
113- async function seedContainerRegistry ( entries : SandboxRegistryEntry [ ] ) {
114- await fs . writeFile ( SANDBOX_REGISTRY_PATH , `${ JSON . stringify ( { entries } , null , 2 ) } \n` , "utf-8" ) ;
115- }
116-
117- async function seedBrowserRegistry ( entries : SandboxBrowserRegistryEntry [ ] ) {
118- await fs . writeFile (
119- SANDBOX_BROWSER_REGISTRY_PATH ,
120- `${ JSON . stringify ( { entries } , null , 2 ) } \n` ,
121- "utf-8" ,
122- ) ;
123- }
124-
125- async function seedShardedContainerRegistry ( entries : SandboxRegistryEntry [ ] ) {
126- await fs . mkdir ( SANDBOX_CONTAINERS_DIR , { recursive : true } ) ;
127- for ( const entry of entries ) {
128- await fs . writeFile (
129- path . join ( SANDBOX_CONTAINERS_DIR , `${ hashTextSha256 ( entry . containerName ) } .json` ) ,
130- `${ JSON . stringify ( entry , null , 2 ) } \n` ,
131- "utf-8" ,
132- ) ;
133- }
134- }
135-
136- async function seedShardedBrowserRegistry ( entries : SandboxBrowserRegistryEntry [ ] ) {
137- await fs . mkdir ( SANDBOX_BROWSERS_DIR , { recursive : true } ) ;
138- for ( const entry of entries ) {
139- await fs . writeFile (
140- path . join ( SANDBOX_BROWSERS_DIR , `${ hashTextSha256 ( entry . containerName ) } .json` ) ,
141- `${ JSON . stringify ( entry , null , 2 ) } \n` ,
142- "utf-8" ,
143- ) ;
144- }
145- }
146-
147- async function seedStaleLock ( lockPath : string ) {
148- await fs . writeFile (
149- lockPath ,
150- `${ JSON . stringify ( { pid : 999_999_999 , createdAt : "2000-01-01T00:00:00.000Z" } ) } \n` ,
151- "utf-8" ,
152- ) ;
153- }
154-
15578async function expectPathMissing ( targetPath : string ) : Promise < void > {
15679 try {
15780 await fs . access ( targetPath ) ;
@@ -162,175 +85,23 @@ async function expectPathMissing(targetPath: string): Promise<void> {
16285 }
16386}
16487
165- function requireMigrationResult (
166- results : readonly MigrationResult [ ] ,
167- kind : MigrationResult [ "kind" ] ,
168- ) : MigrationResult {
169- const result = results . find ( ( candidate ) => candidate . kind === kind ) ;
170- if ( ! result ) {
171- throw new Error ( `expected migration result for ${ kind } ` ) ;
172- }
173- return result ;
174- }
175-
17688describe ( "registry race safety" , ( ) => {
17789 it ( "does not migrate legacy registry files from runtime reads" , async ( ) => {
17890 // Runtime reads should ignore old monolithic files; explicit doctor/repair
17991 // owns migration so normal startup cannot mutate registry layout.
180- await seedContainerRegistry ( [ containerEntry ( { containerName : "legacy-container" } ) ] ) ;
92+ const legacyEntry = containerEntry ( { containerName : "legacy-container" } ) ;
93+ await fs . writeFile (
94+ SANDBOX_REGISTRY_PATH ,
95+ `${ JSON . stringify ( { entries : [ legacyEntry ] } , null , 2 ) } \n` ,
96+ "utf-8" ,
97+ ) ;
18198
18299 await expect ( readRegistry ( ) ) . resolves . toEqual ( { entries : [ ] } ) ;
183100 await expect ( readRegistryEntry ( "legacy-container" ) ) . resolves . toBeNull ( ) ;
184101 await expect ( fs . access ( SANDBOX_REGISTRY_PATH ) ) . resolves . toBeUndefined ( ) ;
185102 await expectPathMissing ( path . join ( TEST_STATE_DIR , "state" , "openclaw.sqlite" ) ) ;
186103 } ) ;
187104
188- it ( "normalizes legacy registry entries after explicit migration" , async ( ) => {
189- await seedContainerRegistry ( [
190- {
191- containerName : "legacy-container" ,
192- sessionKey : "agent:main" ,
193- createdAtMs : 1 ,
194- lastUsedAtMs : 1 ,
195- image : "openclaw-sandbox:test" ,
196- } ,
197- ] ) ;
198-
199- await migrateLegacySandboxRegistryFiles ( ) ;
200- const registry = await readRegistry ( ) ;
201- expect ( registry . entries ) . toHaveLength ( 1 ) ;
202- const [ entry ] = registry . entries ;
203- expect ( entry ?. containerName ) . toBe ( "legacy-container" ) ;
204- expect ( entry ?. backendId ) . toBe ( "docker" ) ;
205- expect ( entry ?. runtimeLabel ) . toBe ( "legacy-container" ) ;
206- expect ( entry ?. configLabelKind ) . toBe ( "Image" ) ;
207- } ) ;
208-
209- it ( "migrates legacy monolithic container and browser registry files after explicit repair" , async ( ) => {
210- await seedContainerRegistry ( [
211- containerEntry ( {
212- containerName : "legacy-container" ,
213- sessionKey : "agent:legacy" ,
214- lastUsedAtMs : 7 ,
215- configHash : "legacy-container-hash" ,
216- } ) ,
217- ] ) ;
218- await seedBrowserRegistry ( [
219- browserEntry ( {
220- containerName : "legacy-browser" ,
221- sessionKey : "agent:legacy" ,
222- cdpPort : 9333 ,
223- noVncPort : 6081 ,
224- configHash : "legacy-browser-hash" ,
225- } ) ,
226- ] ) ;
227- await seedStaleLock ( `${ SANDBOX_REGISTRY_PATH } .lock` ) ;
228- await seedStaleLock ( `${ SANDBOX_BROWSER_REGISTRY_PATH } .lock` ) ;
229-
230- const migrationResults = await migrateLegacySandboxRegistryFiles ( ) ;
231- const containerMigration = requireMigrationResult ( migrationResults , "containers" ) ;
232- const browserMigration = requireMigrationResult ( migrationResults , "browsers" ) ;
233- expect ( containerMigration . status ) . toBe ( "migrated" ) ;
234- expect ( containerMigration . entries ) . toBe ( 1 ) ;
235- expect ( browserMigration . status ) . toBe ( "migrated" ) ;
236- expect ( browserMigration . entries ) . toBe ( 1 ) ;
237-
238- await expectPathMissing ( SANDBOX_REGISTRY_PATH ) ;
239- await expectPathMissing ( SANDBOX_BROWSER_REGISTRY_PATH ) ;
240- await expectPathMissing ( `${ SANDBOX_REGISTRY_PATH } .lock` ) ;
241- await expectPathMissing ( `${ SANDBOX_BROWSER_REGISTRY_PATH } .lock` ) ;
242- const containerRegistry = await readRegistry ( ) ;
243- expect ( containerRegistry . entries ) . toHaveLength ( 1 ) ;
244- const [ container ] = containerRegistry . entries ;
245- expect ( container ?. containerName ) . toBe ( "legacy-container" ) ;
246- expect ( container ?. backendId ) . toBe ( "docker" ) ;
247- expect ( container ?. runtimeLabel ) . toBe ( "legacy-container" ) ;
248- expect ( container ?. sessionKey ) . toBe ( "agent:legacy" ) ;
249- expect ( container ?. configHash ) . toBe ( "legacy-container-hash" ) ;
250- const browserRegistry = await readBrowserRegistry ( ) ;
251- expect ( browserRegistry . entries ) . toHaveLength ( 1 ) ;
252- const [ browser ] = browserRegistry . entries ;
253- expect ( browser ?. containerName ) . toBe ( "legacy-browser" ) ;
254- expect ( browser ?. sessionKey ) . toBe ( "agent:legacy" ) ;
255- expect ( browser ?. cdpPort ) . toBe ( 9333 ) ;
256- expect ( browser ?. noVncPort ) . toBe ( 6081 ) ;
257- expect ( browser ?. configHash ) . toBe ( "legacy-browser-hash" ) ;
258- } ) ;
259-
260- it ( "migrates legacy sharded container and browser registry files after explicit repair" , async ( ) => {
261- await seedShardedContainerRegistry ( [
262- containerEntry ( {
263- containerName : "legacy-container" ,
264- sessionKey : "agent:legacy" ,
265- lastUsedAtMs : 7 ,
266- configHash : "legacy-container-hash" ,
267- } ) ,
268- ] ) ;
269- await seedShardedBrowserRegistry ( [
270- browserEntry ( {
271- containerName : "legacy-browser" ,
272- sessionKey : "agent:legacy" ,
273- cdpPort : 9333 ,
274- noVncPort : 6081 ,
275- configHash : "legacy-browser-hash" ,
276- } ) ,
277- ] ) ;
278-
279- const migrationResults = await migrateLegacySandboxRegistryFiles ( ) ;
280- expect ( requireMigrationResult ( migrationResults , "containers" ) . status ) . toBe ( "migrated" ) ;
281- expect ( requireMigrationResult ( migrationResults , "browsers" ) . status ) . toBe ( "migrated" ) ;
282- await expectPathMissing ( SANDBOX_CONTAINERS_DIR ) ;
283- await expectPathMissing ( SANDBOX_BROWSERS_DIR ) ;
284- expect ( ( await readRegistry ( ) ) . entries [ 0 ] ?. containerName ) . toBe ( "legacy-container" ) ;
285- expect ( ( await readBrowserRegistry ( ) ) . entries [ 0 ] ?. containerName ) . toBe ( "legacy-browser" ) ;
286- } ) ;
287-
288- it ( "does not overwrite newer SQLite entries during legacy migration" , async ( ) => {
289- await updateRegistry (
290- containerEntry ( {
291- containerName : "container-a" ,
292- sessionKey : "new-session" ,
293- lastUsedAtMs : 10 ,
294- } ) ,
295- ) ;
296- await seedContainerRegistry ( [
297- containerEntry ( {
298- containerName : "container-a" ,
299- sessionKey : "legacy-session" ,
300- lastUsedAtMs : 1 ,
301- } ) ,
302- ] ) ;
303-
304- await migrateLegacySandboxRegistryFiles ( ) ;
305-
306- const entry = await readRegistryEntry ( "container-a" ) ;
307- expect ( entry ?. sessionKey ) . toBe ( "new-session" ) ;
308- expect ( entry ?. lastUsedAtMs ) . toBe ( 10 ) ;
309- } ) ;
310-
311- it ( "prefers newer sharded entries over stale monolithic entries during legacy migration" , async ( ) => {
312- await seedContainerRegistry ( [
313- containerEntry ( {
314- containerName : "container-a" ,
315- sessionKey : "legacy-session" ,
316- lastUsedAtMs : 1 ,
317- } ) ,
318- ] ) ;
319- await seedShardedContainerRegistry ( [
320- containerEntry ( {
321- containerName : "container-a" ,
322- sessionKey : "sharded-session" ,
323- lastUsedAtMs : 10 ,
324- } ) ,
325- ] ) ;
326-
327- await migrateLegacySandboxRegistryFiles ( ) ;
328-
329- const entry = await readRegistryEntry ( "container-a" ) ;
330- expect ( entry ?. sessionKey ) . toBe ( "sharded-session" ) ;
331- expect ( entry ?. lastUsedAtMs ) . toBe ( 10 ) ;
332- } ) ;
333-
334105 it ( "reads a single SQLite entry without scanning the full registry" , async ( ) => {
335106 await updateRegistry ( containerEntry ( { containerName : "container-x" , sessionKey : "sess:x" } ) ) ;
336107 await updateRegistry ( containerEntry ( { containerName : "container-y" , sessionKey : "sess:y" } ) ) ;
@@ -491,52 +262,4 @@ describe("registry race safety", () => {
491262 const registry = await readBrowserRegistry ( ) ;
492263 expect ( registry . entries ) . toHaveLength ( 0 ) ;
493264 } ) ;
494-
495- it ( "quarantines malformed legacy registry files during migration" , async ( ) => {
496- await seedMalformedContainerRegistry ( "{bad json" ) ;
497- await seedMalformedBrowserRegistry ( "{bad json" ) ;
498- const results = await migrateLegacySandboxRegistryFiles ( ) ;
499-
500- await expectPathMissing ( SANDBOX_REGISTRY_PATH ) ;
501- await expectPathMissing ( SANDBOX_BROWSER_REGISTRY_PATH ) ;
502- expect ( results . map ( ( result ) => result . status ) ) . toEqual ( [
503- "quarantined-invalid" ,
504- "quarantined-invalid" ,
505- ] ) ;
506- } ) ;
507-
508- it ( "quarantines legacy registry files with invalid entries during migration" , async ( ) => {
509- const invalidEntries = `{"entries":[{"sessionKey":"agent:main"}]}` ;
510- await seedMalformedContainerRegistry ( invalidEntries ) ;
511- await seedMalformedBrowserRegistry ( invalidEntries ) ;
512- const migrationResults = await migrateLegacySandboxRegistryFiles ( ) ;
513- expect ( requireMigrationResult ( migrationResults , "containers" ) . status ) . toBe (
514- "quarantined-invalid" ,
515- ) ;
516- expect ( requireMigrationResult ( migrationResults , "browsers" ) . status ) . toBe ( "quarantined-invalid" ) ;
517- } ) ;
518-
519- it ( "quarantines malformed sharded registry directories during migration" , async ( ) => {
520- await fs . mkdir ( SANDBOX_CONTAINERS_DIR , { recursive : true } ) ;
521- await fs . mkdir ( SANDBOX_BROWSERS_DIR , { recursive : true } ) ;
522- await seedShardedContainerRegistry ( [
523- containerEntry ( { containerName : "valid-container" , sessionKey : "agent:valid" } ) ,
524- ] ) ;
525- await seedShardedBrowserRegistry ( [
526- browserEntry ( { containerName : "valid-browser" , sessionKey : "agent:valid" } ) ,
527- ] ) ;
528- await fs . writeFile ( path . join ( SANDBOX_CONTAINERS_DIR , "bad.json" ) , "{bad json" , "utf-8" ) ;
529- await fs . writeFile ( path . join ( SANDBOX_BROWSERS_DIR , "bad.json" ) , "{bad json" , "utf-8" ) ;
530-
531- const migrationResults = await migrateLegacySandboxRegistryFiles ( ) ;
532-
533- expect ( requireMigrationResult ( migrationResults , "containers" ) . status ) . toBe (
534- "quarantined-invalid" ,
535- ) ;
536- expect ( requireMigrationResult ( migrationResults , "browsers" ) . status ) . toBe ( "quarantined-invalid" ) ;
537- expect ( ( await readRegistry ( ) ) . entries [ 0 ] ?. containerName ) . toBe ( "valid-container" ) ;
538- expect ( ( await readBrowserRegistry ( ) ) . entries [ 0 ] ?. containerName ) . toBe ( "valid-browser" ) ;
539- await expectPathMissing ( SANDBOX_CONTAINERS_DIR ) ;
540- await expectPathMissing ( SANDBOX_BROWSERS_DIR ) ;
541- } ) ;
542265} ) ;
0 commit comments