From a2baaad15ba0425a8f72bf927c7df92566943118 Mon Sep 17 00:00:00 2001 From: Alex Verkhovsky Date: Mon, 8 Dec 2025 08:43:35 -0700 Subject: [PATCH] fix: add error handling and rollback for temp directory cleanup --- tools/cli/installers/lib/core/installer.js | 38 +++++++++++++++++----- 1 file changed, 29 insertions(+), 9 deletions(-) diff --git a/tools/cli/installers/lib/core/installer.js b/tools/cli/installers/lib/core/installer.js index fb670d43..667cd9e9 100644 --- a/tools/cli/installers/lib/core/installer.js +++ b/tools/cli/installers/lib/core/installer.js @@ -972,19 +972,39 @@ If AgentVibes party mode is enabled, immediately trigger TTS with agent's voice: if (await fs.pathExists(customDir)) { // Move contents to module directory const items = await fs.readdir(customDir); - for (const item of items) { - const srcPath = path.join(customDir, item); - const destPath = path.join(moduleTargetPath, item); + const movedItems = []; + try { + for (const item of items) { + const srcPath = path.join(customDir, item); + const destPath = path.join(moduleTargetPath, item); - // If destination exists, remove it first (or we could merge) - if (await fs.pathExists(destPath)) { - await fs.remove(destPath); + // If destination exists, remove it first (or we could merge) + if (await fs.pathExists(destPath)) { + await fs.remove(destPath); + } + + await fs.move(srcPath, destPath); + movedItems.push({ src: srcPath, dest: destPath }); } - - await fs.move(srcPath, destPath); + } catch (moveError) { + // Rollback: restore any successfully moved items + for (const moved of movedItems) { + try { + await fs.move(moved.dest, moved.src); + } catch { + // Best-effort rollback - log if it fails + console.error(`Failed to rollback ${moved.dest} during cleanup`); + } + } + throw new Error(`Failed to move custom module files: ${moveError.message}`); } } - await fs.remove(tempCustomPath); + try { + await fs.remove(tempCustomPath); + } catch (cleanupError) { + // Non-fatal: temp directory cleanup failed but files were moved successfully + console.warn(`Warning: Could not clean up temp directory: ${cleanupError.message}`); + } } // Create module config (include collected config from module.yaml prompts)