Skip to content

Record your screen, ship a demo. Free and open-source, GPU-accelerated, no watermarks, no subscriptions. Windows, macOS, Linux. Actively maintained.

fix(ai): keep a deleted project's chat out of the cache until its file is gone

EtienneLescot48ed345656d1
diff --git a/electron/ai-edition/chat-persistence.test.ts b/electron/ai-edition/chat-persistence.test.ts
index a3cf53550..dcb05265a 100644
--- a/electron/ai-edition/chat-persistence.test.ts
+++ b/electron/ai-edition/chat-persistence.test.ts
@@ -186,7 +186,10 @@ describe("local Agent chat history", () => {
expect(existsSync(file)).toBe(true);
renameSession("proj_removed", listSessions("proj_removed")[0]?.id ?? "", "Queued write");
// Queued behind that pending write, so the write cannot recreate the file.
- await deleteProjectChat("proj_removed");
+ const deleting = deleteProjectChat("proj_removed");
+ // The file still holds the deleted session until the queue reaches the delete.
+ expect(listSessions("proj_removed")).toEqual([]);
+ await deleting;
expect(existsSync(file)).toBe(false);
await restart();
expect(listSessions("proj_removed")).toEqual([]);
diff --git a/electron/ai-edition/chat-service.ts b/electron/ai-edition/chat-service.ts
index c82cc4748..b224565bc 100644
--- a/electron/ai-edition/chat-service.ts
+++ b/electron/ai-edition/chat-service.ts
@@ -58,12 +58,17 @@ function persistProject(projectId: string): void {
});
}
+/** Projects whose sidecar removal is still queued: their file holds deleted sessions. */
+const deletingProjects = new Set<string>();
+
export function deleteProjectChat(projectId: string): Promise<void> {
sessionsByProject.delete(projectId);
for (const key of messageCheckpointsBySession.keys()) {
if (key.startsWith(projectId + "::")) messageCheckpointsBySession.delete(key);
}
- return persistence?.delete(projectId) ?? Promise.resolve();
+ if (!persistence) return Promise.resolve();
+ deletingProjects.add(projectId);
+ return persistence.delete(projectId).finally(() => deletingProjects.delete(projectId));
}
/** Lets queued chat-history writes reach the disk before the app exits. */
@@ -215,7 +220,8 @@ function toSummary(s: ChatSession): ChatSessionSummary {
function getProjectSessions(projectId: string): Map<string, ChatSession> {
let m = sessionsByProject.get(projectId);
if (!m) {
- m = new Map(persistence?.read(projectId).map((session) => [session.id, session]) ?? []);
+ const stored = deletingProjects.has(projectId) ? [] : (persistence?.read(projectId) ?? []);
+ m = new Map(stored.map((session) => [session.id, session]));
sessionsByProject.set(projectId, m);
}
return m;
diff --git a/electron/main.ts b/electron/main.ts
index b036e908c..91b36b930 100644
--- a/electron/main.ts
+++ b/electron/main.ts
@@ -1110,7 +1110,7 @@ app.on("activate", () => {
}
});
-let sttShutdownPromise: Promise<unknown> | null = null;
+let sttShutdownPromise: Promise<void> | null = null;
let sttShutdownFinished = false;
// Electron does not wait for an async event listener. Hold the first quit long
@@ -1127,16 +1127,17 @@ app.on("before-quit", (event) => {
if (sttShutdownFinished) return;
event.preventDefault();
if (sttShutdownPromise) return;
- sttShutdownPromise = Promise.all([
- shutdownStt().catch((error) => {
+ sttShutdownPromise = shutdownStt()
+ .catch((error) => {
console.error("[stt] Failed to stop whisper helper during app quit:", error);
- }),
- // Chat history is written asynchronously; let a reply that just landed reach the disk.
- flushChatPersistence(),
- ]).finally(() => {
- sttShutdownFinished = true;
- app.quit();
- });
+ })
+ // Chat history is written asynchronously. Flushing after the helper stops also
+ // catches a reply that landed meanwhile; a turn still running is not waited for.
+ .then(flushChatPersistence)
+ .finally(() => {
+ sttShutdownFinished = true;
+ app.quit();
+ });
});
app.on("will-quit", () => {