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>
192 lines
7.8 KiB
JavaScript
192 lines
7.8 KiB
JavaScript
const test = require("node:test");
|
|
const assert = require("node:assert/strict");
|
|
const fs = require("node:fs");
|
|
const os = require("node:os");
|
|
const path = require("node:path");
|
|
|
|
const bridge = require("./aiSessionsBackupBridge.cjs");
|
|
const session = { id: "s1", title: "Session", agentId: "agent", scope: { type: "global" }, messages: [], updatedAt: 1 };
|
|
|
|
function makeTempDir() {
|
|
return fs.mkdtempSync(path.join(os.tmpdir(), "netcatty-ai-backup-"));
|
|
}
|
|
|
|
test("writeGeneration writes atomically and rotates generations to the cap", async () => {
|
|
const dir = makeTempDir();
|
|
bridge.__internals._setBackupDir(dir);
|
|
try {
|
|
const { MAX_GENERATIONS } = bridge.__internals;
|
|
for (let i = 0; i < MAX_GENERATIONS + 2; i++) {
|
|
const result = await bridge.__internals.writeGeneration(JSON.stringify({ gen: i }));
|
|
assert.equal(result.ok, true);
|
|
// Small delay so timestamped names strictly increase.
|
|
const stamp = bridge.__internals.timestampToken();
|
|
while (bridge.__internals.timestampToken() === stamp) {
|
|
Atomics.wait(new Int32Array(new SharedArrayBuffer(4)), 0, 0, 5);
|
|
}
|
|
}
|
|
const generations = bridge.__internals.listGenerationFiles(dir);
|
|
assert.equal(generations.length, MAX_GENERATIONS);
|
|
// Newest generation wins the lexicographic sort.
|
|
assert.deepEqual(JSON.parse(fs.readFileSync(path.join(dir, generations[0]), "utf-8")), {
|
|
gen: MAX_GENERATIONS + 1,
|
|
});
|
|
// No temp files left behind by the atomic write.
|
|
assert.deepEqual(fs.readdirSync(dir).filter((f) => f.endsWith(".tmp")), []);
|
|
} finally {
|
|
bridge.__internals._setBackupDir(null);
|
|
fs.rmSync(dir, { recursive: true, force: true });
|
|
}
|
|
});
|
|
|
|
test("readLatestGeneration skips corrupt generations and reads the newest parseable one", () => {
|
|
const dir = makeTempDir();
|
|
bridge.__internals._setBackupDir(dir);
|
|
try {
|
|
fs.writeFileSync(path.join(dir, "ai-sessions-20260101T000000-000Z.json"), "not json");
|
|
fs.writeFileSync(
|
|
path.join(dir, "ai-sessions-20260102T000000-000Z.json"),
|
|
JSON.stringify([session]),
|
|
);
|
|
const latest = bridge.__internals.readLatestGeneration();
|
|
assert.equal(latest.ok, true);
|
|
assert.deepEqual(JSON.parse(latest.json), [session]);
|
|
// A corrupt-only directory reports failure instead of throwing.
|
|
const corruptOnly = makeTempDir();
|
|
bridge.__internals._setBackupDir(corruptOnly);
|
|
try {
|
|
fs.writeFileSync(path.join(corruptOnly, "ai-sessions-20260103T000000-000Z.json"), "{oops");
|
|
assert.equal(bridge.__internals.readLatestGeneration().ok, false);
|
|
} finally {
|
|
bridge.__internals._setBackupDir(dir);
|
|
fs.rmSync(corruptOnly, { recursive: true, force: true });
|
|
}
|
|
} finally {
|
|
bridge.__internals._setBackupDir(null);
|
|
fs.rmSync(dir, { recursive: true, force: true });
|
|
}
|
|
});
|
|
|
|
test("readLatestGeneration skips non-array generations", () => {
|
|
const dir = makeTempDir();
|
|
bridge.__internals._setBackupDir(dir);
|
|
try {
|
|
const olderFile = "ai-sessions-20260101T000000-000Z.json";
|
|
const sessions = [session];
|
|
fs.writeFileSync(path.join(dir, olderFile), JSON.stringify(sessions));
|
|
fs.writeFileSync(path.join(dir, "ai-sessions-20260102T000000-000Z.json"), JSON.stringify({ sessions }));
|
|
|
|
const latest = bridge.__internals.readLatestGeneration();
|
|
assert.equal(latest.ok, true);
|
|
assert.equal(latest.file, olderFile);
|
|
assert.deepEqual(JSON.parse(latest.json), sessions);
|
|
} finally {
|
|
bridge.__internals._setBackupDir(null);
|
|
fs.rmSync(dir, { recursive: true, force: true });
|
|
}
|
|
});
|
|
|
|
test("readLatestGeneration skips arrays without usable sessions", () => {
|
|
const dir = makeTempDir();
|
|
bridge.__internals._setBackupDir(dir);
|
|
try {
|
|
const olderFile = "ai-sessions-20260101T000000-000Z.json";
|
|
fs.writeFileSync(path.join(dir, olderFile), JSON.stringify([session]));
|
|
fs.writeFileSync(path.join(dir, "ai-sessions-20260102T000000-000Z.json"), JSON.stringify([{ id: "bad" }]));
|
|
|
|
const latest = bridge.__internals.readLatestGeneration();
|
|
assert.equal(latest.ok, true);
|
|
assert.equal(latest.file, olderFile);
|
|
assert.deepEqual(JSON.parse(latest.json), [session]);
|
|
} finally {
|
|
bridge.__internals._setBackupDir(null);
|
|
fs.rmSync(dir, { recursive: true, force: true });
|
|
}
|
|
});
|
|
|
|
test("readLatestGeneration treats an empty generation as an authoritative wipe", () => {
|
|
const dir = makeTempDir();
|
|
bridge.__internals._setBackupDir(dir);
|
|
try {
|
|
const newestFile = "ai-sessions-20260102T000000-000Z.json";
|
|
fs.writeFileSync(path.join(dir, "ai-sessions-20260101T000000-000Z.json"), JSON.stringify([session]));
|
|
fs.writeFileSync(path.join(dir, newestFile), "[]");
|
|
|
|
const latest = bridge.__internals.readLatestGeneration();
|
|
assert.equal(latest.ok, true);
|
|
assert.equal(latest.file, newestFile);
|
|
assert.deepEqual(JSON.parse(latest.json), []);
|
|
} finally {
|
|
bridge.__internals._setBackupDir(null);
|
|
fs.rmSync(dir, { recursive: true, force: true });
|
|
}
|
|
});
|
|
|
|
test("readLatestGeneration skips sessions with malformed messages", () => {
|
|
const dir = makeTempDir();
|
|
bridge.__internals._setBackupDir(dir);
|
|
try {
|
|
const olderFile = "ai-sessions-20260101T000000-000Z.json";
|
|
const newestFile = "ai-sessions-20260102T000000-000Z.json";
|
|
fs.writeFileSync(path.join(dir, olderFile), JSON.stringify([session]));
|
|
for (const message of [null, {}, { id: "m", role: "user", content: "hello", timestamp: null }]) {
|
|
fs.writeFileSync(path.join(dir, newestFile), JSON.stringify([{ ...session, messages: [message] }]));
|
|
const latest = bridge.__internals.readLatestGeneration();
|
|
assert.equal(latest.ok, true);
|
|
assert.equal(latest.file, olderFile);
|
|
assert.deepEqual(JSON.parse(latest.json), [session]);
|
|
}
|
|
} finally {
|
|
bridge.__internals._setBackupDir(null);
|
|
fs.rmSync(dir, { recursive: true, force: true });
|
|
}
|
|
});
|
|
|
|
test("writeGeneration rejects invalid or oversized payloads", async () => {
|
|
const dir = makeTempDir();
|
|
bridge.__internals._setBackupDir(dir);
|
|
try {
|
|
assert.equal((await bridge.__internals.writeGeneration("")).ok, false);
|
|
assert.equal((await bridge.__internals.writeGeneration(null)).ok, false);
|
|
const oversized = "x".repeat(bridge.__internals.MAX_BACKUP_JSON_BYTES + 1);
|
|
assert.equal((await bridge.__internals.writeGeneration(oversized)).ok, false);
|
|
assert.deepEqual(bridge.__internals.listGenerationFiles(dir), []);
|
|
} finally {
|
|
bridge.__internals._setBackupDir(null);
|
|
fs.rmSync(dir, { recursive: true, force: true });
|
|
}
|
|
});
|
|
|
|
test("export falls back to a fixed path when no dialog is available", async () => {
|
|
const dir = makeTempDir();
|
|
bridge.__internals._setBackupDir(dir);
|
|
try {
|
|
const result = await bridge.__internals.writeExportFixed(dir, JSON.stringify([{ id: "s1" }]));
|
|
assert.equal(result.success, true);
|
|
assert.ok(path.basename(result.filePath).startsWith("export-"));
|
|
assert.equal(fs.existsSync(result.filePath), true);
|
|
} finally {
|
|
bridge.__internals._setBackupDir(null);
|
|
fs.rmSync(dir, { recursive: true, force: true });
|
|
}
|
|
});
|
|
|
|
test("writeGeneration serializes concurrent writes without leaving temp files", async () => {
|
|
const dir = makeTempDir();
|
|
bridge.__internals._setBackupDir(dir);
|
|
try {
|
|
const results = await Promise.all([
|
|
bridge.__internals.writeGeneration(JSON.stringify({ gen: "a" })),
|
|
bridge.__internals.writeGeneration(""),
|
|
bridge.__internals.writeGeneration(JSON.stringify({ gen: "b" })),
|
|
]);
|
|
assert.deepEqual(results.map((result) => result.ok), [true, false, true]);
|
|
assert.deepEqual(fs.readdirSync(dir).filter((f) => f.endsWith(".tmp")), []);
|
|
const newest = bridge.__internals.listGenerationFiles(dir)[0];
|
|
assert.deepEqual(JSON.parse(fs.readFileSync(path.join(dir, newest), "utf-8")), { gen: "b" });
|
|
} finally {
|
|
bridge.__internals._setBackupDir(null);
|
|
fs.rmSync(dir, { recursive: true, force: true });
|
|
}
|
|
});
|