@@ -17,6 +17,7 @@ import type { InstalledPluginIndex } from "./installed-plugin-index.js";
1717import {
1818 hasRetainedManagedNpmInstallMarker ,
1919 markRetainedManagedNpmInstall ,
20+ resolveRetainedManagedNpmInstallMarkerPath ,
2021} from "./managed-npm-retention.js" ;
2122import { writeManagedNpmPlugin } from "./test-helpers/managed-npm-plugin.js" ;
2223
@@ -853,6 +854,67 @@ describe("commitConfigWithPendingPluginInstalls", () => {
853854 }
854855 } ) ;
855856
857+ it ( "restores earlier active markers when clearing a later marker fails" , async ( ) => {
858+ const stateDir = fs . mkdtempSync ( path . join ( os . tmpdir ( ) , "openclaw-record-commit-" ) ) ;
859+ const installPaths = [ "codex" , "voice-call" ] . map ( ( pluginId ) =>
860+ path . join (
861+ stateDir ,
862+ "npm" ,
863+ "projects" ,
864+ `${ pluginId } -v2` ,
865+ "node_modules" ,
866+ "@openclaw" ,
867+ pluginId ,
868+ ) ,
869+ ) ;
870+ for ( const [ index , installPath ] of installPaths . entries ( ) ) {
871+ fs . mkdirSync ( installPath , { recursive : true } ) ;
872+ await markRetainedManagedNpmInstall ( {
873+ packageDir : installPath ,
874+ pluginId : index === 0 ? "codex" : "voice-call" ,
875+ retainedAt : "2026-04-25T00:00:00.000Z" ,
876+ reason : "test-retained-generation" ,
877+ } ) ;
878+ }
879+ const laterMarkerPath = resolveRetainedManagedNpmInstallMarkerPath ( installPaths [ 1 ] ?? "" ) ;
880+ const realRm = fs . promises . rm . bind ( fs . promises ) ;
881+ const rmSpy = vi . spyOn ( fs . promises , "rm" ) . mockImplementation ( async ( target , options ) => {
882+ if ( String ( target ) === laterMarkerPath ) {
883+ const error = new Error ( "marker clear failed" ) as NodeJS . ErrnoException ;
884+ error . code = "EIO" ;
885+ throw error ;
886+ }
887+ return await realRm ( target , options ) ;
888+ } ) ;
889+
890+ try {
891+ await expect (
892+ commitPluginInstallRecordsWithConfig ( {
893+ previousInstallRecords : { } ,
894+ nextInstallRecords : Object . fromEntries (
895+ installPaths . map ( ( installPath , index ) => {
896+ const pluginId = index === 0 ? "codex" : "voice-call" ;
897+ return [
898+ pluginId ,
899+ {
900+ source : "npm" ,
901+ spec : `@openclaw/${ pluginId } @2.0.0` ,
902+ installPath,
903+ } ,
904+ ] ;
905+ } ) ,
906+ ) ,
907+ nextConfig : { } ,
908+ } ) ,
909+ ) . rejects . toThrow ( "marker clear failed" ) ;
910+
911+ expect ( installPaths . every ( hasRetainedManagedNpmInstallMarker ) ) . toBe ( true ) ;
912+ } finally {
913+ rmSpy . mockRestore ( ) ;
914+ fs . rmSync ( stateDir , { recursive : true , force : true } ) ;
915+ }
916+ } ) ;
917+
856918 it ( "rolls back plugin index writes when the config write fails" , async ( ) => {
857919 const existingRecords : Record < string , PluginInstallRecord > = {
858920 existing : {
0 commit comments