|
| 1 | +/** |
| 2 | + * Last-resort recovery for a data dir the sidecar can no longer open |
| 3 | + * (failed migration, corrupted SQLite, …): move the executor state aside |
| 4 | + * and start fresh. |
| 5 | + * |
| 6 | + * Strictly backup-then-move — nothing is ever deleted. data.db holds the |
| 7 | + * user's connections and secrets, so the old state lands in a timestamped |
| 8 | + * folder under ~/.executor/backups/ where it can be restored by copying |
| 9 | + * the files back. |
| 10 | + * |
| 11 | + * Scope: only the sidecar-owned state (data.db + SQLite sidecar files and |
| 12 | + * server-control/). The plugin manifest (executor.jsonc) is user-authored |
| 13 | + * config, not state — a reset shouldn't discard hand-written setup — and |
| 14 | + * desktop settings (port/auth) live in Electron's own store, unaffected. |
| 15 | + */ |
| 16 | + |
| 17 | +import { existsSync, mkdirSync, renameSync } from "node:fs"; |
| 18 | +import { homedir } from "node:os"; |
| 19 | +import { join } from "node:path"; |
| 20 | +import { dialog, shell } from "electron"; |
| 21 | +import log from "electron-log/main.js"; |
| 22 | + |
| 23 | +const STATE_ENTRIES = ["data.db", "data.db-wal", "data.db-shm", "server-control"]; |
| 24 | + |
| 25 | +const backupStamp = () => |
| 26 | + new Date() |
| 27 | + .toISOString() |
| 28 | + .replace(/[-:]/g, "") |
| 29 | + .replace(/\.\d+Z$/, "Z"); |
| 30 | + |
| 31 | +export interface ResetStateResult { |
| 32 | + readonly backupDir: string; |
| 33 | + readonly moved: ReadonlyArray<string>; |
| 34 | +} |
| 35 | + |
| 36 | +/** |
| 37 | + * Move the executor state into ~/.executor/backups/<stamp>/. The caller is |
| 38 | + * responsible for stopping the sidecar first and restarting it after. |
| 39 | + */ |
| 40 | +export const resetExecutorState = (): ResetStateResult => { |
| 41 | + const dataDir = join(homedir(), ".executor"); |
| 42 | + const backupDir = join(dataDir, "backups", backupStamp()); |
| 43 | + mkdirSync(backupDir, { recursive: true }); |
| 44 | + const moved: string[] = []; |
| 45 | + for (const entry of STATE_ENTRIES) { |
| 46 | + const from = join(dataDir, entry); |
| 47 | + if (!existsSync(from)) continue; |
| 48 | + renameSync(from, join(backupDir, entry)); |
| 49 | + moved.push(entry); |
| 50 | + } |
| 51 | + log.info("[reset-state] moved executor state to backup", { backupDir, moved }); |
| 52 | + return { backupDir, moved }; |
| 53 | +}; |
| 54 | + |
| 55 | +/** |
| 56 | + * Confirmation dialog shared by every surface that offers a reset. Returns |
| 57 | + * true when the user explicitly chose to reset. |
| 58 | + * |
| 59 | + * EXECUTOR_TEST_AUTO_CONFIRM_RESET=1 skips the dialog — native dialogs are |
| 60 | + * unreachable from Playwright, and the e2e crash-recovery scenario needs to |
| 61 | + * drive the full reset path. |
| 62 | + */ |
| 63 | +export const confirmResetState = async (): Promise<boolean> => { |
| 64 | + if (process.env.EXECUTOR_TEST_AUTO_CONFIRM_RESET === "1") return true; |
| 65 | + const { response } = await dialog.showMessageBox({ |
| 66 | + type: "warning", |
| 67 | + title: "Reset Executor data?", |
| 68 | + message: "Start over with a fresh data directory?", |
| 69 | + detail: |
| 70 | + "Your current data — integrations, connections, and history — will be moved to " + |
| 71 | + "~/.executor/backups (not deleted), and Executor will restart with a clean slate. " + |
| 72 | + "Use this when the app can't start because its data is damaged.", |
| 73 | + buttons: ["Reset and back up", "Cancel"], |
| 74 | + defaultId: 1, |
| 75 | + cancelId: 1, |
| 76 | + }); |
| 77 | + return response === 0; |
| 78 | +}; |
| 79 | + |
| 80 | +/** |
| 81 | + * Make the "your data is backed up" promise concrete after a reset: name the |
| 82 | + * exact folder and offer to open it. Without this the user is told their data |
| 83 | + * is safe somewhere but has no way to act on it. |
| 84 | + */ |
| 85 | +export const announceBackup = async (backupDir: string): Promise<void> => { |
| 86 | + // Same test seam as confirmResetState: a modal with no one to dismiss it |
| 87 | + // would hang the e2e reset path. |
| 88 | + if (process.env.EXECUTOR_TEST_AUTO_CONFIRM_RESET === "1") { |
| 89 | + log.info("[reset-state] backup announced (test mode, dialog skipped)", { backupDir }); |
| 90 | + return; |
| 91 | + } |
| 92 | + const { response } = await dialog.showMessageBox({ |
| 93 | + type: "info", |
| 94 | + title: "Executor data reset", |
| 95 | + message: "Your previous data has been backed up.", |
| 96 | + detail: |
| 97 | + `If you need anything from before the reset, it's all here:\n\n${backupDir}\n\n` + |
| 98 | + "You can ignore this folder otherwise — Executor is now running on a fresh data directory.", |
| 99 | + buttons: ["Show in folder", "OK"], |
| 100 | + defaultId: 1, |
| 101 | + cancelId: 1, |
| 102 | + }); |
| 103 | + if (response === 0) shell.showItemInFolder(backupDir); |
| 104 | +}; |
0 commit comments