mirror of
https://github.com/binaricat/Netcatty.git
synced 2026-11-23 16:42:19 +00:00
Both paths ran synchronous I/O on the Electron main process, which freezes
every window and the terminal keystroke relay while it runs:
- Agent discovery (opening the AI panel or Settings > AI) probed auth with
execFileSync: `security find-generic-password` (4 s timeout), `gh auth
status` (network, 5 s), `which cursor-agent` (4 s) and `cursor-agent status`
(8 s), one after another. The probes now use a non-rejecting async execFile
helper with the same result shape; discovery awaits them.
- Cursor SDK detection evaluated `import("@cursor/sdk")` (~4 MB of ESM plus
its transport stack) only to check that it is installed, and kept it
resident afterwards. It now checks for the ESM entry that the dynamic import
loads; the SDK itself still loads on first use. The package does not export
package.json and its CJS build is stripped from releases, so the check walks
require.resolve.paths() instead of using require.resolve.
- The AI chat backup wrote the whole history (often several MB) with
writeFileSync on every debounced change. Writes now use fs.promises and are
serialized so generations cannot interleave.
Co-authored-by: Claude Opus 5.5 <noreply@anthropic.com>
282 lines
9.3 KiB
JavaScript
282 lines
9.3 KiB
JavaScript
/**
|
|
* AI Sessions Backup Bridge - durable disk mirror of the renderer's AI chat
|
|
* session store (`netcatty_ai_sessions_v1` in localStorage).
|
|
*
|
|
* localStorage lives inside the Electron profile directory, so reinstalling,
|
|
* moving or recreating the app takes the chat history with it, and in-app
|
|
* cleanup sweeps / cap eviction can also shrink the live store. This bridge
|
|
* keeps timestamped JSON generations under
|
|
* `{userData}/ai-sessions-backup/` so the newest store can be restored on
|
|
* boot when the live store is missing or empty (never on top of a non-empty
|
|
* store — see useAIState).
|
|
*
|
|
* Generations are capped (default 5) and each write is atomic
|
|
* (temp file + rename). All handlers never throw: a backup failure must not
|
|
* break chat persistence.
|
|
*/
|
|
|
|
const fs = require("node:fs");
|
|
const path = require("node:path");
|
|
|
|
let backupDir = null;
|
|
let electronDialog = null;
|
|
|
|
const BACKUP_DIR_NAME = "ai-sessions-backup";
|
|
const FILE_PREFIX = "ai-sessions-";
|
|
const EXPORT_PREFIX = "export-";
|
|
const FILE_SUFFIX = ".json";
|
|
/** Keep at most this many rolling generations (plus dated export files). */
|
|
const MAX_GENERATIONS = 5;
|
|
/**
|
|
* Hard cap for a mirrored payload. localStorage itself targets ~2 MB, but when
|
|
* cloud AI-session sync is enabled the renderer persists the full unpruned
|
|
* history for the disk mirror — that can exceed 4 MB. Keep a generous disk
|
|
* ceiling so synced histories still get rolling backups, while still rejecting
|
|
* runaway payloads.
|
|
*/
|
|
const MAX_BACKUP_JSON_BYTES = 64 * 1024 * 1024;
|
|
|
|
/**
|
|
* The automatic mirror recovers from in-profile localStorage loss or corruption
|
|
* (quota, empty or corrupt JSON, or accidental clear). Profile deletion or
|
|
* recreation is out of scope; use the manual export/import buttons for that.
|
|
*/
|
|
function ensureBackupDir() {
|
|
if (backupDir) return backupDir;
|
|
try {
|
|
let userDataPath = null;
|
|
try {
|
|
const { app } = require("electron");
|
|
userDataPath = app?.getPath?.("userData") ?? null;
|
|
} catch {
|
|
userDataPath = null;
|
|
}
|
|
if (!userDataPath) return null;
|
|
backupDir = path.join(userDataPath, BACKUP_DIR_NAME);
|
|
if (!fs.existsSync(backupDir)) {
|
|
fs.mkdirSync(backupDir, { recursive: true });
|
|
}
|
|
return backupDir;
|
|
} catch {
|
|
return null;
|
|
}
|
|
}
|
|
|
|
function listGenerationFiles(dir) {
|
|
try {
|
|
return fs
|
|
.readdirSync(dir)
|
|
.filter((file) => file.startsWith(FILE_PREFIX) && file.endsWith(FILE_SUFFIX))
|
|
.sort()
|
|
.reverse(); // timestamped names sort lexicographically newest-first
|
|
} catch {
|
|
return [];
|
|
}
|
|
}
|
|
|
|
function listExportFiles(dir) {
|
|
try {
|
|
return fs
|
|
.readdirSync(dir)
|
|
.filter((file) => file.startsWith(EXPORT_PREFIX) && file.endsWith(FILE_SUFFIX))
|
|
.sort()
|
|
.reverse();
|
|
} catch {
|
|
return [];
|
|
}
|
|
}
|
|
|
|
function timestampToken(now = new Date()) {
|
|
return `${now.toISOString().replace(/[-:]/g, "").replace(".", "-")}`;
|
|
}
|
|
|
|
let writeQueue = Promise.resolve();
|
|
|
|
/**
|
|
* Write one generation atomically (temp + rename) and prune older ones.
|
|
* Resolves { ok, file } and never rejects. Writes are serialized and use
|
|
* fs.promises: the history can be several MB and is written on every
|
|
* debounced chat change, which used to block the main thread (all windows).
|
|
*/
|
|
function writeGeneration(json) {
|
|
const run = () => writeGenerationNow(json);
|
|
const result = writeQueue.then(run, run);
|
|
writeQueue = result.catch(() => {});
|
|
return result;
|
|
}
|
|
|
|
async function writeGenerationNow(json) {
|
|
const dir = ensureBackupDir();
|
|
if (!dir) return { ok: false, error: "no-backup-dir" };
|
|
if (typeof json !== "string" || json.length === 0) {
|
|
return { ok: false, error: "invalid-json" };
|
|
}
|
|
if (Buffer.byteLength(json, "utf-8") > MAX_BACKUP_JSON_BYTES) {
|
|
return { ok: false, error: "payload-too-large" };
|
|
}
|
|
|
|
const file = `${FILE_PREFIX}${timestampToken()}${FILE_SUFFIX}`;
|
|
const filePath = path.join(dir, file);
|
|
const tmpPath = `${filePath}.tmp`;
|
|
try {
|
|
await fs.promises.writeFile(tmpPath, json, "utf-8");
|
|
await fs.promises.rename(tmpPath, filePath);
|
|
} catch (error) {
|
|
await fs.promises.unlink(tmpPath).catch(() => {});
|
|
return { ok: false, error: String(error) };
|
|
}
|
|
|
|
// Keep only the newest MAX_GENERATIONS generations.
|
|
const generations = listGenerationFiles(dir);
|
|
for (const stale of generations.slice(MAX_GENERATIONS)) {
|
|
await fs.promises.unlink(path.join(dir, stale)).catch(() => {});
|
|
}
|
|
return { ok: true, file };
|
|
}
|
|
|
|
/**
|
|
* Read the newest usable session array. Invalid generations are skipped and
|
|
* older ones are tried, so one bad write never blocks a restore.
|
|
*/
|
|
function readLatestGeneration() {
|
|
const dir = ensureBackupDir();
|
|
if (!dir) return { ok: false };
|
|
for (const file of listGenerationFiles(dir)) {
|
|
try {
|
|
const json = fs.readFileSync(path.join(dir, file), "utf-8");
|
|
const parsed = JSON.parse(json);
|
|
// Structurally unusable payloads can fall back to an older session array.
|
|
if (!Array.isArray(parsed)) continue;
|
|
// An empty array is an intentional wipe and must not resurrect older chats.
|
|
if (parsed.length === 0) return { ok: true, json, file };
|
|
if (!parsed.some((entry) => (
|
|
entry !== null && typeof entry === "object"
|
|
&& typeof entry.id === "string"
|
|
&& typeof entry.title === "string"
|
|
&& typeof entry.agentId === "string"
|
|
&& entry.scope !== null && typeof entry.scope === "object"
|
|
&& typeof entry.scope.type === "string"
|
|
&& Array.isArray(entry.messages)
|
|
&& entry.messages.every((message) => (
|
|
message !== null && typeof message === "object"
|
|
&& typeof message.id === "string"
|
|
&& typeof message.role === "string"
|
|
&& typeof message.content === "string"
|
|
&& typeof message.timestamp === "number" && Number.isFinite(message.timestamp)
|
|
))
|
|
&& typeof entry.updatedAt === "number" && Number.isFinite(entry.updatedAt)
|
|
))) continue;
|
|
return { ok: true, json, file };
|
|
} catch {
|
|
// try the next older generation
|
|
}
|
|
}
|
|
return { ok: false };
|
|
}
|
|
|
|
async function exportToFile(json) {
|
|
const dir = ensureBackupDir();
|
|
if (!dir) return { success: false, error: "no-backup-dir" };
|
|
if (typeof json !== "string" || json.length === 0) {
|
|
return { success: false, error: "invalid-json" };
|
|
}
|
|
try {
|
|
let result;
|
|
if (electronDialog?.showSaveDialog) {
|
|
result = await electronDialog.showSaveDialog({
|
|
defaultPath: `${EXPORT_PREFIX}${timestampToken()}${FILE_SUFFIX}`,
|
|
filters: [
|
|
{ name: "JSON", extensions: ["json"] },
|
|
{ name: "All Files", extensions: ["*"] },
|
|
],
|
|
});
|
|
if (result.canceled || !result.filePath) {
|
|
return { success: false, canceled: true };
|
|
}
|
|
} else {
|
|
// Fallback when no dialog is available (tests / unusual hosts): fixed
|
|
// path next to the rolling generations.
|
|
return await writeExportFixed(dir, json);
|
|
}
|
|
await fs.promises.writeFile(result.filePath, json, "utf-8");
|
|
return { success: true, filePath: result.filePath };
|
|
} catch (error) {
|
|
return { success: false, error: String(error) };
|
|
}
|
|
}
|
|
|
|
async function writeExportFixed(dir, json) {
|
|
const filePath = path.join(dir, `${EXPORT_PREFIX}${timestampToken()}${FILE_SUFFIX}`);
|
|
try {
|
|
await fs.promises.writeFile(filePath, json, "utf-8");
|
|
return { success: true, filePath };
|
|
} catch (error) {
|
|
return { success: false, error: String(error) };
|
|
}
|
|
}
|
|
|
|
async function importFromFile() {
|
|
try {
|
|
if (electronDialog?.showOpenDialog) {
|
|
const result = await electronDialog.showOpenDialog({
|
|
properties: ["openFile"],
|
|
filters: [
|
|
{ name: "JSON", extensions: ["json"] },
|
|
{ name: "All Files", extensions: ["*"] },
|
|
],
|
|
});
|
|
if (result.canceled || !result.filePaths || result.filePaths.length === 0) {
|
|
return { success: false, canceled: true };
|
|
}
|
|
const json = await fs.promises.readFile(result.filePaths[0], "utf-8");
|
|
return { success: true, json };
|
|
}
|
|
// No dialog available: import the newest fixed-path export instead of
|
|
// failing outright.
|
|
const dir = ensureBackupDir();
|
|
if (!dir) return { success: false, error: "no-backup-dir" };
|
|
const exports = listExportFiles(dir);
|
|
if (exports.length === 0) return { success: false, error: "no-export-file" };
|
|
const json = await fs.promises.readFile(path.join(dir, exports[0]), "utf-8");
|
|
return { success: true, json };
|
|
} catch (error) {
|
|
return { success: false, error: String(error) };
|
|
}
|
|
}
|
|
|
|
function registerHandlers(ipcMain) {
|
|
ipcMain.handle("netcatty:aiSessionsBackup:write", (_event, payload) =>
|
|
writeGeneration(payload?.json));
|
|
ipcMain.handle("netcatty:aiSessionsBackup:readLatest", () => readLatestGeneration());
|
|
ipcMain.handle("netcatty:aiSessionsBackup:export", (_event, payload) =>
|
|
exportToFile(payload?.json));
|
|
ipcMain.handle("netcatty:aiSessionsBackup:import", () => importFromFile());
|
|
}
|
|
|
|
const __internals = {
|
|
BACKUP_DIR_NAME,
|
|
FILE_PREFIX,
|
|
EXPORT_PREFIX,
|
|
MAX_GENERATIONS,
|
|
MAX_BACKUP_JSON_BYTES,
|
|
listGenerationFiles,
|
|
listExportFiles,
|
|
timestampToken,
|
|
writeGeneration,
|
|
readLatestGeneration,
|
|
writeExportFixed,
|
|
// Tests point the bridge at a temp directory without Electron.
|
|
_setBackupDir(dir) {
|
|
backupDir = dir;
|
|
},
|
|
};
|
|
|
|
module.exports = {
|
|
init(deps) {
|
|
electronDialog = deps?.electronModule?.dialog ?? null;
|
|
ensureBackupDir();
|
|
},
|
|
registerHandlers,
|
|
__internals,
|
|
};
|