22import fs from "node:fs" ;
33import os from "node:os" ;
44import path from "node:path" ;
5- import { beforeEach , describe , expect , it , vi } from "vitest" ;
5+ import { afterEach , beforeEach , describe , expect , it , vi } from "vitest" ;
6+ import { useAutoCleanupTempDirTracker } from "../../test/helpers/temp-dir.js" ;
67import {
78 createPluginInstallRecordMap ,
89 getPluginInstallRecordMapEntry ,
@@ -11,11 +12,16 @@ import {
1112import type { OpenClawConfig } from "../config/types.openclaw.js" ;
1213import type { PluginInstallRecord } from "../config/types.plugins.js" ;
1314import { withEnvAsync } from "../test-utils/env.js" ;
15+ import { listRecoveredManagedNpmInstallCandidates } from "./installed-plugin-index-record-reader.js" ;
1416import type { InstalledPluginIndex } from "./installed-plugin-index.js" ;
1517import {
1618 hasRetainedManagedNpmInstallMarker ,
1719 markRetainedManagedNpmInstall ,
20+ resolveRetainedManagedNpmInstallMarkerPath ,
1821} from "./managed-npm-retention.js" ;
22+ import { writeManagedNpmPlugin } from "./test-helpers/managed-npm-plugin.js" ;
23+
24+ const retentionTempDirs = useAutoCleanupTempDirTracker ( afterEach ) ;
1925
2026const mocks = vi . hoisted ( ( ) => {
2127 const lease = {
@@ -480,6 +486,49 @@ describe("commitConfigWithPendingPluginInstalls", () => {
480486 }
481487 } ) ;
482488
489+ it ( "removes a new retirement marker when the leased config commit rolls back" , async ( ) => {
490+ const stateDir = retentionTempDirs . make ( "openclaw-record-commit-" ) ;
491+ const installPath = writeManagedNpmPlugin ( {
492+ stateDir,
493+ packageName : "@openclaw/retained-rollback" ,
494+ pluginId : "retained-rollback" ,
495+ version : "1.0.0" ,
496+ } ) ;
497+ const previousInstallRecords : Record < string , PluginInstallRecord > = {
498+ "retained-rollback" : {
499+ source : "npm" ,
500+ spec :
"@openclaw/[email protected] " , 501+ installPath,
502+ } ,
503+ } ;
504+ mocks . replaceConfigFile . mockRejectedValueOnce ( new Error ( "config changed" ) ) ;
505+
506+ try {
507+ await withEnvAsync ( { OPENCLAW_STATE_DIR : stateDir } , async ( ) => {
508+ await expect (
509+ commitPluginInstallRecordsWithConfig ( {
510+ previousInstallRecords,
511+ nextInstallRecords : { } ,
512+ nextConfig : { } ,
513+ } ) ,
514+ ) . rejects . toThrow ( "config changed" ) ;
515+
516+ expect ( hasRetainedManagedNpmInstallMarker ( installPath ) ) . toBe ( false ) ;
517+ expect (
518+ listRecoveredManagedNpmInstallCandidates ( { stateDir } ) . map (
519+ ( candidate ) => candidate . pluginId ,
520+ ) ,
521+ ) . toContain ( "retained-rollback" ) ;
522+ expect ( mocks . restorePersistedInstalledPluginIndexIfCurrent ) . toHaveBeenCalledWith ( null , 1 , {
523+ filePath : mocks . lease . databasePath ,
524+ lease : mocks . lease ,
525+ } ) ;
526+ } ) ;
527+ } finally {
528+ fs . rmSync ( stateDir , { recursive : true , force : true } ) ;
529+ }
530+ } ) ;
531+
483532 it ( "does not mark arbitrary npm paths outside the managed npm root" , async ( ) => {
484533 const stateDir = fs . mkdtempSync ( path . join ( os . tmpdir ( ) , "openclaw-record-commit-" ) ) ;
485534 const outsideRoot = fs . mkdtempSync ( path . join ( os . tmpdir ( ) , "openclaw-record-outside-" ) ) ;
@@ -805,6 +854,67 @@ describe("commitConfigWithPendingPluginInstalls", () => {
805854 }
806855 } ) ;
807856
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+
808918 it ( "rolls back plugin index writes when the config write fails" , async ( ) => {
809919 const existingRecords : Record < string , PluginInstallRecord > = {
810920 existing : {
0 commit comments