@@ -24,6 +24,7 @@ import {
2424import {
2525 clearRetainedManagedNpmInstallMarker ,
2626 markRetainedManagedNpmInstall ,
27+ RETAINED_MANAGED_NPM_KEEP_FILES_REASON ,
2728 resolveRetainedManagedNpmInstallPackageInfo ,
2829 resolveRetainedManagedNpmInstallMarkerPath ,
2930} from "../plugins/managed-npm-retention.js" ;
@@ -114,15 +115,19 @@ function resolveRetainedManagedNpmInstallMarkerTarget(params: {
114115 previousRecord ?: PluginInstallRecord ;
115116 nextRecord ?: PluginInstallRecord ;
116117} ) : string | null {
117- if ( params . previousRecord ?. source !== "npm" || params . nextRecord ?. source !== "npm" ) {
118+ if ( params . previousRecord ?. source !== "npm" ) {
118119 return null ;
119120 }
120121 const previousInstallPath = params . previousRecord . installPath ?. trim ( ) ;
121- const nextInstallPath = params . nextRecord . installPath ?. trim ( ) ;
122- if ( ! previousInstallPath || ! nextInstallPath ) {
122+ const nextInstallPath = params . nextRecord ? .installPath ?. trim ( ) ;
123+ if ( ! previousInstallPath || ( params . nextRecord && params . nextRecord . source !== "npm" ) ) {
123124 return null ;
124125 }
125- if ( installPathsOverlap ( previousInstallPath , nextInstallPath ) ) {
126+ if (
127+ params . nextRecord &&
128+ nextInstallPath &&
129+ installPathsOverlap ( previousInstallPath , nextInstallPath )
130+ ) {
126131 return null ;
127132 }
128133
@@ -145,7 +150,7 @@ function resolveRetainedManagedNpmInstallMarkerTarget(params: {
145150 ) {
146151 return null ;
147152 }
148- if ( installPathsOverlap ( plan . directoryRemoval . target , nextInstallPath ) ) {
153+ if ( nextInstallPath && installPathsOverlap ( plan . directoryRemoval . target , nextInstallPath ) ) {
149154 return null ;
150155 }
151156 return plan . directoryRemoval . target ;
@@ -174,17 +179,21 @@ function findReplacementNpmRecordForRemovedRecord(params: {
174179 return null ;
175180}
176181
177- async function markRetainedReplacedManagedNpmInstallRecords ( params : {
182+ async function markRetiredManagedNpmInstallRecords ( params : {
178183 previousInstallRecords : Record < string , PluginInstallRecord > ;
179184 nextInstallRecords : Record < string , PluginInstallRecord > ;
185+ retainRemovedNpmInstallRecord ?: string ;
180186 createdMarkerPaths : string [ ] ;
181187} ) : Promise < void > {
182188 const markedPreviousPluginIds = new Set < string > ( ) ;
183- const markReplacement = async (
189+ const markRetiredInstall = async (
184190 pluginId : string ,
185191 previousRecord : PluginInstallRecord | undefined ,
186192 nextRecord : PluginInstallRecord | undefined ,
187193 ) => {
194+ if ( ! nextRecord && pluginId !== params . retainRemovedNpmInstallRecord ) {
195+ return ;
196+ }
188197 const packageDir = resolveRetainedManagedNpmInstallMarkerTarget ( {
189198 pluginId,
190199 previousRecord,
@@ -198,7 +207,10 @@ async function markRetainedReplacedManagedNpmInstallRecords(params: {
198207 const marked = await markRetainedManagedNpmInstall ( {
199208 packageDir,
200209 pluginId,
201- reason : "replaced-by-managed-npm-generation-update" ,
210+ reason :
211+ nextRecord ?. source === "npm"
212+ ? "replaced-by-managed-npm-generation-update"
213+ : RETAINED_MANAGED_NPM_KEEP_FILES_REASON ,
202214 } ) ;
203215 if ( marked && ! markerAlreadyExisted ) {
204216 // Record each marker immediately so a later filesystem failure can roll it back.
@@ -208,13 +220,13 @@ async function markRetainedReplacedManagedNpmInstallRecords(params: {
208220 } ;
209221
210222 for ( const [ pluginId , nextRecord ] of Object . entries ( params . nextInstallRecords ) ) {
211- await markReplacement ( pluginId , params . previousInstallRecords [ pluginId ] , nextRecord ) ;
223+ await markRetiredInstall ( pluginId , params . previousInstallRecords [ pluginId ] , nextRecord ) ;
212224 }
213225 for ( const [ pluginId , previousRecord ] of Object . entries ( params . previousInstallRecords ) ) {
214226 if ( markedPreviousPluginIds . has ( pluginId ) || params . nextInstallRecords [ pluginId ] ) {
215227 continue ;
216228 }
217- await markReplacement (
229+ await markRetiredInstall (
218230 pluginId ,
219231 previousRecord ,
220232 findReplacementNpmRecordForRemovedRecord ( {
@@ -274,6 +286,7 @@ async function restoreClearedRetainedManagedNpmInstallMarkers(
274286async function commitPluginInstallRecordsWithWriter ( params : {
275287 previousInstallRecords ?: Record < string , PluginInstallRecord > ;
276288 nextInstallRecords : Record < string , PluginInstallRecord > ;
289+ retainRemovedNpmInstallRecord ?: string ;
277290 nextConfig : OpenClawConfig ;
278291 writeOptions ?: ConfigWriteOptions ;
279292 commit : ConfigCommit ;
@@ -285,9 +298,12 @@ async function commitPluginInstallRecordsWithWriter(params: {
285298 try {
286299 await writePersistedInstalledPluginIndexInstallRecords ( params . nextInstallRecords ) ;
287300 try {
288- await markRetainedReplacedManagedNpmInstallRecords ( {
301+ await markRetiredManagedNpmInstallRecords ( {
289302 previousInstallRecords,
290303 nextInstallRecords : params . nextInstallRecords ,
304+ ...( params . retainRemovedNpmInstallRecord
305+ ? { retainRemovedNpmInstallRecord : params . retainRemovedNpmInstallRecord }
306+ : { } ) ,
291307 // Keep partial progress visible to the outer rollback path.
292308 createdMarkerPaths : retainedMarkerPaths ,
293309 } ) ;
@@ -330,6 +346,7 @@ async function commitPluginInstallRecordsWithWriter(params: {
330346export async function commitPluginInstallRecordsWithConfig ( params : {
331347 previousInstallRecords ?: Record < string , PluginInstallRecord > ;
332348 nextInstallRecords : Record < string , PluginInstallRecord > ;
349+ retainRemovedNpmInstallRecord ?: string ;
333350 nextConfig : OpenClawConfig ;
334351 baseHash ?: string ;
335352 writeOptions ?: ConfigWriteOptions ;
0 commit comments