@@ -1339,20 +1339,40 @@ export function runAllowedValidationCommandsWithBinding(
13391339 validationIdentityProofDeadlineAt ( deadlineAt ) ,
13401340 ) ;
13411341 }
1342- const restoreValidationCache = runtimeBuild
1343- ? prepareDisposableRuntimeBuildCache ( cwd , validationEnv , ignoredValidationInputs )
1344- : options . targetRepo === "openclaw/openclaw" && isChangedGateCommand ( parts , options )
1345- ? prepareDisposableRuntimeBuildCache (
1342+ const restoreChangedGateOutputs =
1343+ options . targetRepo === "openclaw/openclaw" &&
1344+ isChangedGateCommand ( parts , options ) &&
1345+ ! pendingRuntimeBuild
1346+ ? prepareDisposableChangedGateBuildOutputs (
13461347 cwd ,
13471348 validationEnv ,
13481349 ignoredValidationInputs ,
1349- "tsgo-cache" ,
1350- "changed-gate validation" ,
13511350 )
13521351 : null ;
1352+ let restoreValidationCache : ( ( ) => void ) | null ;
1353+ try {
1354+ restoreValidationCache = runtimeBuild
1355+ ? prepareDisposableRuntimeBuildCache ( cwd , validationEnv , ignoredValidationInputs )
1356+ : options . targetRepo === "openclaw/openclaw" && isChangedGateCommand ( parts , options )
1357+ ? prepareDisposableRuntimeBuildCache (
1358+ cwd ,
1359+ validationEnv ,
1360+ ignoredValidationInputs ,
1361+ "tsgo-cache" ,
1362+ "changed-gate validation" ,
1363+ )
1364+ : null ;
1365+ } catch ( error ) {
1366+ restoreChangedGateOutputs ?.( ) ;
1367+ throw error ;
1368+ }
13531369 const executionBudgetMs = remainingCommandBudget ( deadlineAt , identityReserveMs ) ;
13541370 if ( executionBudgetMs < MIN_VALIDATION_COMMAND_BUDGET_MS ) {
1355- restoreValidationCache ?.( ) ;
1371+ try {
1372+ restoreChangedGateOutputs ?.( ) ;
1373+ } finally {
1374+ restoreValidationCache ?.( ) ;
1375+ }
13561376 throw validationCommandBudgetError ( rendered ) ;
13571377 }
13581378 try {
@@ -1363,7 +1383,11 @@ export function runAllowedValidationCommandsWithBinding(
13631383 writableRoots : [ cwd , path . dirname ( String ( validationEnv . HOME ) ) ] ,
13641384 } ) ;
13651385 } finally {
1366- restoreValidationCache ?.( ) ;
1386+ try {
1387+ restoreChangedGateOutputs ?.( ) ;
1388+ } finally {
1389+ restoreValidationCache ?.( ) ;
1390+ }
13671391 }
13681392 if ( runtimeBuild ) {
13691393 assertGeneratedRuntimeBuildOutput ( cwd ) ;
@@ -1435,17 +1459,33 @@ export function runAllowedValidationCommandsWithBinding(
14351459 let fallbackError : Error | null = null ;
14361460 try {
14371461 resetValidationEnvironment ( deadlineAt - identityReserveMs ) ;
1438- const restoreFallbackValidationCache =
1462+ const restoreFallbackChangedGateOutputs =
14391463 options . targetRepo === "openclaw/openclaw" &&
1440- isChangedGateCommand ( fallbackParts , options )
1441- ? prepareDisposableRuntimeBuildCache (
1464+ isChangedGateCommand ( fallbackParts , options ) &&
1465+ ! pendingRuntimeBuild
1466+ ? prepareDisposableChangedGateBuildOutputs (
14421467 cwd ,
14431468 validationEnv ,
14441469 ignoredValidationInputs ,
1445- "tsgo-cache" ,
1446- "changed-gate validation" ,
14471470 )
14481471 : null ;
1472+ let restoreFallbackValidationCache : ( ( ) => void ) | null ;
1473+ try {
1474+ restoreFallbackValidationCache =
1475+ options . targetRepo === "openclaw/openclaw" &&
1476+ isChangedGateCommand ( fallbackParts , options )
1477+ ? prepareDisposableRuntimeBuildCache (
1478+ cwd ,
1479+ validationEnv ,
1480+ ignoredValidationInputs ,
1481+ "tsgo-cache" ,
1482+ "changed-gate validation" ,
1483+ )
1484+ : null ;
1485+ } catch ( error ) {
1486+ restoreFallbackChangedGateOutputs ?.( ) ;
1487+ throw error ;
1488+ }
14491489 try {
14501490 const fallbackBudgetMs = remainingCommandBudget ( deadlineAt , identityReserveMs ) ;
14511491 if ( fallbackBudgetMs < MIN_VALIDATION_COMMAND_BUDGET_MS ) {
@@ -1462,7 +1502,11 @@ export function runAllowedValidationCommandsWithBinding(
14621502 writableRoots : [ cwd , path . dirname ( String ( validationEnv . HOME ) ) ] ,
14631503 } ) ;
14641504 } finally {
1465- restoreFallbackValidationCache ?.( ) ;
1505+ try {
1506+ restoreFallbackChangedGateOutputs ?.( ) ;
1507+ } finally {
1508+ restoreFallbackValidationCache ?.( ) ;
1509+ }
14661510 }
14671511 } catch ( error ) {
14681512 fallbackError = error as Error ;
@@ -2798,6 +2842,113 @@ function runtimeArtifactBuildOutputRoots(cwd: string): string[] {
27982842 return roots ;
27992843}
28002844
2845+ function prepareDisposableChangedGateBuildOutputs (
2846+ cwd : string ,
2847+ validationEnv : NodeJS . ProcessEnv ,
2848+ ignoredValidationInputs : readonly string [ ] ,
2849+ ) {
2850+ const checkout = fs . realpathSync ( cwd ) ;
2851+ const backupRoot = fs . realpathSync (
2852+ fs . mkdtempSync ( path . join ( os . tmpdir ( ) , "clawsweeper-changed-gate-output-" ) ) ,
2853+ ) ;
2854+ const validationHomeRoot = fs . realpathSync ( path . dirname ( String ( validationEnv . HOME ) ) ) ;
2855+ if (
2856+ backupRoot === checkout ||
2857+ backupRoot . startsWith ( `${ checkout } ${ path . sep } ` ) ||
2858+ backupRoot === validationHomeRoot ||
2859+ backupRoot . startsWith ( `${ validationHomeRoot } ${ path . sep } ` )
2860+ ) {
2861+ fs . rmSync ( backupRoot , { recursive : true , force : true } ) ;
2862+ throw new Error ( "changed-gate validation backup must be outside writable sandbox roots" ) ;
2863+ }
2864+ const snapshots : Array < {
2865+ relativePath : string ;
2866+ existed : boolean ;
2867+ parentRealPath : string ;
2868+ parentDevice : number ;
2869+ parentInode : number ;
2870+ } > = [ ] ;
2871+ try {
2872+ for ( const relativePath of runtimeArtifactBuildOutputRoots ( cwd ) ) {
2873+ const output = path . join ( checkout , relativePath ) ;
2874+ const parent = path . dirname ( output ) ;
2875+ const parentStat = fs . lstatSync ( parent , { throwIfNoEntry : false } ) ;
2876+ if ( ! parentStat ) continue ;
2877+ if ( ! parentStat . isDirectory ( ) || parentStat . isSymbolicLink ( ) ) {
2878+ throw new Error ( `changed-gate validation has an unsafe output parent: ${ relativePath } ` ) ;
2879+ }
2880+ const parentRealPath = fs . realpathSync ( parent ) ;
2881+ assertPathWithin ( checkout , parentRealPath , relativePath ) ;
2882+ const outputStat = fs . lstatSync ( output , { throwIfNoEntry : false } ) ;
2883+ if (
2884+ outputStat &&
2885+ ( ! ignoredValidationInputs . includes ( relativePath ) ||
2886+ ! outputStat . isDirectory ( ) ||
2887+ outputStat . isSymbolicLink ( ) )
2888+ ) {
2889+ throw new Error ( `changed-gate validation has an unsafe existing output: ${ relativePath } ` ) ;
2890+ }
2891+ if ( outputStat ) {
2892+ const backup = path . join ( backupRoot , relativePath ) ;
2893+ fs . mkdirSync ( path . dirname ( backup ) , { recursive : true } ) ;
2894+ fs . cpSync ( output , backup , { recursive : true , verbatimSymlinks : true } ) ;
2895+ }
2896+ snapshots . push ( {
2897+ relativePath,
2898+ existed : Boolean ( outputStat ) ,
2899+ parentRealPath,
2900+ parentDevice : parentStat . dev ,
2901+ parentInode : parentStat . ino ,
2902+ } ) ;
2903+ }
2904+ } catch ( error ) {
2905+ fs . rmSync ( backupRoot , { recursive : true , force : true } ) ;
2906+ throw error ;
2907+ }
2908+ return ( ) => {
2909+ let restorationFailure : unknown = null ;
2910+ let preserveBackup = false ;
2911+ for ( const snapshot of snapshots ) {
2912+ try {
2913+ const output = path . join ( checkout , snapshot . relativePath ) ;
2914+ const parent = path . dirname ( output ) ;
2915+ const parentStat = fs . lstatSync ( parent , { throwIfNoEntry : false } ) ;
2916+ if (
2917+ ! parentStat ?. isDirectory ( ) ||
2918+ parentStat . isSymbolicLink ( ) ||
2919+ parentStat . dev !== snapshot . parentDevice ||
2920+ parentStat . ino !== snapshot . parentInode ||
2921+ fs . realpathSync ( parent ) !== snapshot . parentRealPath
2922+ ) {
2923+ throw new Error (
2924+ `changed-gate validation changed its protected output parent: ${ snapshot . relativePath } ` ,
2925+ ) ;
2926+ }
2927+ const outputStat = fs . lstatSync ( output , { throwIfNoEntry : false } ) ;
2928+ if ( outputStat && ( ! outputStat . isDirectory ( ) || outputStat . isSymbolicLink ( ) ) ) {
2929+ restorationFailure ??= new Error (
2930+ `changed-gate validation produced an unsafe output: ${ snapshot . relativePath } ` ,
2931+ ) ;
2932+ }
2933+ fs . rmSync ( output , { recursive : true , force : true } ) ;
2934+ if ( snapshot . existed ) {
2935+ fs . cpSync ( path . join ( backupRoot , snapshot . relativePath ) , output , {
2936+ recursive : true ,
2937+ verbatimSymlinks : true ,
2938+ } ) ;
2939+ }
2940+ } catch ( error ) {
2941+ restorationFailure ??= error ;
2942+ preserveBackup = true ;
2943+ }
2944+ }
2945+ if ( ! preserveBackup ) {
2946+ fs . rmSync ( backupRoot , { recursive : true , force : true } ) ;
2947+ }
2948+ if ( restorationFailure ) throw restorationFailure ;
2949+ } ;
2950+ }
2951+
28012952function prepareDisposableRuntimeBuildCache (
28022953 cwd : string ,
28032954 validationEnv : NodeJS . ProcessEnv ,
0 commit comments