@@ -1284,6 +1284,80 @@ describe("runGlobalPackageUpdateSteps", () => {
12841284 } ) ;
12851285 } ) ;
12861286
1287+ it ( "rejects staged recapture recovery roots inside the recorded live package path" , async ( ) => {
1288+ await withTempDir ( { prefix : "openclaw-package-update-local-swap-recovery-" } , async ( base ) => {
1289+ const prefix = path . join ( base , "prefix" ) ;
1290+ const globalRoot = path . join ( prefix , "lib" , "node_modules" ) ;
1291+ const packageRoot = path . join ( globalRoot , "openclaw" ) ;
1292+ const lateOverridePath = path . join ( packageRoot , "dist" , "late-local.js" ) ;
1293+ const stateDir = path . join ( packageRoot , "state" ) ;
1294+ await writePackageRoot ( packageRoot , "1.0.0" ) ;
1295+
1296+ const priorStateDir = process . env . OPENCLAW_STATE_DIR ;
1297+ process . env . OPENCLAW_STATE_DIR = stateDir ;
1298+ const realRename = fs . rename . bind ( fs ) ;
1299+ let lateOverrideCreated = false ;
1300+ const renameSpy = vi
1301+ . spyOn ( fs , "rename" )
1302+ . mockImplementation ( async ( ...args : Parameters < typeof fs . rename > ) => {
1303+ if ( ! lateOverrideCreated && String ( args [ 0 ] ) === packageRoot ) {
1304+ lateOverrideCreated = true ;
1305+ await fs . writeFile ( lateOverridePath , "export const late = true;\n" , "utf8" ) ;
1306+ }
1307+ return await realRename ( ...args ) ;
1308+ } ) ;
1309+
1310+ try {
1311+ const result = await runGlobalPackageUpdateSteps ( {
1312+ installTarget : createNpmTarget ( globalRoot ) ,
1313+ 1314+ packageName : "openclaw" ,
1315+ packageRoot,
1316+ runCommand : createRootRunner ( globalRoot ) ,
1317+ runStep : async ( { name, argv, cwd } ) => {
1318+ const stagePrefix = argv [ argv . indexOf ( "--prefix" ) + 1 ] ;
1319+ if ( ! stagePrefix ) {
1320+ throw new Error ( "missing staged prefix" ) ;
1321+ }
1322+ await writePackageRoot (
1323+ path . join ( stagePrefix , "lib" , "node_modules" , "openclaw" ) ,
1324+ "2.0.0" ,
1325+ ) ;
1326+ return {
1327+ name,
1328+ command : argv . join ( " " ) ,
1329+ cwd : cwd ?? process . cwd ( ) ,
1330+ durationMs : 1 ,
1331+ exitCode : 0 ,
1332+ } ;
1333+ } ,
1334+ timeoutMs : 1000 ,
1335+ } ) ;
1336+
1337+ expect ( lateOverrideCreated ) . toBe ( true ) ;
1338+ expect ( result . failedStep ?. name ) . toBe ( "local overrides" ) ;
1339+ expect ( result . failedStep ?. stderrTail ) . toContain (
1340+ "local override recovery root must be outside package root" ,
1341+ ) ;
1342+ expect ( result . afterVersion ) . toBe ( "1.0.0" ) ;
1343+ await expect (
1344+ fs . readFile ( path . join ( packageRoot , "package.json" ) , "utf8" ) ,
1345+ ) . resolves . toContain ( '"version":"1.0.0"' ) ;
1346+ await expect ( fs . readFile ( lateOverridePath , "utf8" ) ) . resolves . toBe (
1347+ "export const late = true;\n" ,
1348+ ) ;
1349+ await expectPathMissing ( stateDir ) ;
1350+ } finally {
1351+ renameSpy . mockRestore ( ) ;
1352+ if ( priorStateDir === undefined ) {
1353+ delete process . env . OPENCLAW_STATE_DIR ;
1354+ } else {
1355+ process . env . OPENCLAW_STATE_DIR = priorStateDir ;
1356+ }
1357+ }
1358+ } ) ;
1359+ } ) ;
1360+
12871361 it ( "preserves local dist overrides without reapplying by default" , async ( ) => {
12881362 await withTempDir ( { prefix : "openclaw-package-update-local-preserved-" } , async ( base ) => {
12891363 const prefix = path . join ( base , "prefix" ) ;
0 commit comments