{"record":{"id":"ddeaadf6e95e7725","repo":"linshenkx/prompt-optimizer","slug":"cannot-delete-the-current-prompt-asset-version","errorCode":null,"errorMessage":"Cannot delete the current prompt asset version","messagePattern":"Cannot delete the current prompt asset version","errorType":"validation","errorClass":"FavoriteValidationError","httpStatus":null,"severity":"error","filePath":"packages/core/src/services/favorite/manager.ts","lineNumber":549,"sourceCode":"        const favoritesList = favorites || [];\n        const index = favoritesList.findIndex(f => f.id === id);\n        if (index === -1) {\n          throw new FavoriteNotFoundError(id);\n        }\n\n        const currentFavorite = favoritesList[index];\n        const metadata = currentFavorite.metadata && typeof currentFavorite.metadata === 'object'\n          ? { ...currentFavorite.metadata }\n          : {};\n        const promptAsset = isPromptAsset(metadata.promptAsset) ? metadata.promptAsset : null;\n        if (!promptAsset) {\n          throw new FavoriteValidationError('Prompt asset is not available for this favorite');\n        }\n        if (promptAsset.versions.length <= 1) {\n          throw new FavoriteValidationError('Cannot delete the last prompt asset version');\n        }\n        if (promptAsset.currentVersionId === versionId) {\n          throw new FavoriteValidationError('Cannot delete the current prompt asset version');\n        }\n\n        const now = Date.now();\n        const nextPromptAsset = deletePromptAssetVersion(promptAsset, versionId, now);\n        if (!nextPromptAsset) {\n          throw new FavoriteValidationError(`Prompt asset version not found: ${versionId}`);\n        }\n\n        const nextFavorite: FavoritePrompt = {\n          ...currentFavorite,\n          updatedAt: now,\n          metadata: {\n            ...metadata,\n            promptAsset: nextPromptAsset,\n          },\n        };\n        assertFavoriteFitsItemBudget(nextFavorite);\n","sourceCodeStart":531,"sourceCodeEnd":567,"githubUrl":"https://github.com/linshenkx/prompt-optimizer/blob/3e677b1d9f7e0493c142c175560531e7ae786dce/packages/core/src/services/favorite/manager.ts#L531-L567","documentation":"Thrown when deleteFavoritePromptAssetVersion is asked to remove the version currently marked as the prompt asset's active version (promptAsset.currentVersionId === versionId). The library keeps currentVersionId pointing at a live version, so the active version cannot be deleted directly; you must switch to another version first.","triggerScenarios":"Calling deleteFavoritePromptAssetVersion(favoriteId, versionId) where versionId equals the asset's currentVersionId — typically the version shown/selected in the UI is the one the user tries to delete, even when other versions exist.","commonSituations":"A version-history UI that offers delete on the currently selected/active row; state desync where the UI's selected version id is stale relative to the stored currentVersionId; restoring an old version then trying to delete the previous current one.","solutions":["Before deleting, switch the current version (e.g. via the manager's update/restore API so currentVersionId points elsewhere), then delete the old version","Guard the delete action with currentVersionId !== versionId in the UI","Catch FavoriteValidationError and inform the user the active version cannot be removed"],"exampleFix":"// before\nawait manager.deleteFavoritePromptAssetVersion(favId, versionId);\n\n// after\nconst fav = await manager.getFavorite(favId);\nconst asset = fav.metadata.promptAsset;\nif (asset && asset.currentVersionId === versionId) {\n  // switch to another version first, or block the action\n  throw new Error('Switch to another version before deleting the current one');\n}\nawait manager.deleteFavoritePromptAssetVersion(favId, versionId);","handlingStrategy":"validation","validationCode":"const fav = await manager.getFavorite(favId);\nconst asset = fav.metadata.promptAsset;\nif (asset?.currentVersionId === versionId) { /* block: switch version first */ }","typeGuard":"const isCurrentVersion = (asset: { currentVersionId: string }, id: string) => asset.currentVersionId === id;","tryCatchPattern":"try { await manager.deleteFavoritePromptAssetVersion(id, vId); } catch (e) { if (e instanceof FavoriteValidationError && /current/.test(e.message)) { /* prompt user to switch version */ } else throw e; }","preventionTips":["Mark the current version as non-deletable in the UI","Switch currentVersionId before issuing delete"],"tags":["favorites","prompt-asset","versioning","validation"],"backgroundTag":"delete-active-resource-version","analyzedSha":"3e677b1d9f7e0493c142c175560531e7ae786dce","analyzedAt":"2026-08-27T21:29:16.709Z","schemaVersion":2},"datasetVersion":"2026-08-28T00:17:15.603Z"}