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): queue chat history writes off the main thread

EtienneLescot64b58472554c
diff --git a/electron/ai-edition/chat-persistence.test.ts b/electron/ai-edition/chat-persistence.test.ts
index a617d92b9..a3cf53550 100644
--- a/electron/ai-edition/chat-persistence.test.ts
+++ b/electron/ai-edition/chat-persistence.test.ts
@@ -10,12 +10,17 @@ vi.mock("./deep-agent/chat-model", () => ({
messageContentToText: (content: unknown) => String(content),
}));
+import {
+ AiEditionService,
+ type AiEditionServiceOptions,
+} from "../native-bridge/services/aiEditionService";
import {
compactSessionNow,
configureChatPersistence,
createSession,
deleteProjectChat,
deleteSession,
+ flushChatPersistence,
listSessions,
renameSession,
rewindToMessage,
@@ -39,13 +44,14 @@ function config(): LlmConfigStore {
} as unknown as LlmConfigStore;
}
-function restart() {
+async function restart() {
+ await flushChatPersistence();
configureChatPersistence(root);
}
beforeEach(() => {
root = mkdtempSync(path.join(tmpdir(), "openscreen-chat-"));
- restart();
+ configureChatPersistence(root);
invokeMock.mockReset();
modelMock.mockReset();
invokeMock.mockImplementation(async (args) => ({
@@ -55,19 +61,20 @@ beforeEach(() => {
}));
});
-afterEach(() => {
+afterEach(async () => {
+ await flushChatPersistence();
rmSync(root, { recursive: true, force: true });
});
describe("local Agent chat history", () => {
- it("restores multiple sessions and persists rename and delete per project", () => {
+ it("restores multiple sessions and persists rename and delete per project", async () => {
const first = createSession("proj_a", "First");
const deleted = createSession("proj_a", "Deleted");
const other = createSession("proj_b", "Other");
renameSession("proj_a", first.id, "Renamed");
expect(deleteSession("proj_a", deleted.id)).toBe(true);
- restart();
+ await restart();
expect(listSessions("proj_a")).toEqual([{ ...first, title: "Renamed" }]);
expect(selectSession("proj_a", deleted.id)).toBeNull();
expect(listSessions("proj_b")).toEqual([other]);
@@ -79,11 +86,12 @@ describe("local Agent chat history", () => {
const document = createEmptyDocument({ title: "Test", projectId: "proj_chat" });
await runChat("proj_chat", session.id, "first question", config(), document);
expect(selectSession("proj_chat", session.id)?.messages[0]?.checkpointId).toBeTruthy();
+ await flushChatPersistence();
const file = path.join(root, "proj_chat.chat.json");
expect(readFileSync(file, "utf8")).not.toContain(key);
expect(readFileSync(file, "utf8")).not.toContain("checkpointId");
- restart();
+ await restart();
const restored = selectSession("proj_chat", session.id);
expect(restored?.messages.map((m) => m.content)).toEqual(["first question", "reply"]);
expect(restored?.messages[0]?.checkpointId).toBeNull();
@@ -111,7 +119,7 @@ describe("local Agent chat history", () => {
}
const compacted = await compactSessionNow("proj_compact", session.id, config());
expect(compacted?.summary).toBe("Earlier goals and decisions");
- restart();
+ await restart();
expect(selectSession("proj_compact", session.id)?.messages).toHaveLength(8);
await runChat("proj_compact", session.id, "continue", config());
const history = invokeMock.mock.lastCall?.[0].history ?? [];
@@ -130,7 +138,7 @@ describe("local Agent chat history", () => {
JSON.stringify({ version: 1, projectId: "proj_other", sessions: [] }),
]) {
writeFileSync(file, bad);
- restart();
+ await restart();
expect(listSessions(doc.project.id)).toEqual([]);
await expect(documents.getProject(doc.project.id)).resolves.toMatchObject({
project: { id: doc.project.id },
@@ -138,8 +146,9 @@ describe("local Agent chat history", () => {
}
});
- it("does not trust a checkpoint id found in a saved transcript", () => {
+ it("does not trust a checkpoint id found in a saved transcript", async () => {
const session = createSession("proj_false_rewind");
+ await flushChatPersistence();
const file = path.join(root, "proj_false_rewind.chat.json");
const saved = JSON.parse(readFileSync(file, "utf8"));
saved.sessions[0].messages = [
@@ -152,28 +161,65 @@ describe("local Agent chat history", () => {
},
];
writeFileSync(file, JSON.stringify(saved));
- restart();
+ await restart();
expect(selectSession("proj_false_rewind", session.id)?.messages[0]?.checkpointId).toBeNull();
expect(rewindToMessage("proj_false_rewind", session.id, "user_1").success).toBe(false);
});
- it("skips a damaged session while retaining valid siblings", () => {
+ it("skips a damaged session while retaining valid siblings", async () => {
const good = createSession("proj_partial", "Good");
+ await flushChatPersistence();
const file = path.join(root, "proj_partial.chat.json");
const saved = JSON.parse(readFileSync(file, "utf8"));
saved.sessions.push({ id: "broken", projectId: "proj_partial", messages: "wrong" });
writeFileSync(file, JSON.stringify(saved));
- restart();
+ await restart();
expect(listSessions("proj_partial")).toEqual([good]);
});
- it("removes chat history when the owning project is deleted", () => {
+ it("removes chat history when the owning project is deleted", async () => {
createSession("proj_removed", "Disposable");
const file = path.join(root, "proj_removed.chat.json");
+ // Written off the event loop: nothing is on disk until the queue runs.
+ expect(existsSync(file)).toBe(false);
+ await flushChatPersistence();
expect(existsSync(file)).toBe(true);
- deleteProjectChat("proj_removed");
+ 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");
expect(existsSync(file)).toBe(false);
- restart();
+ await restart();
expect(listSessions("proj_removed")).toEqual([]);
});
+
+ it("does not recreate the history of a project deleted while the agent replies", async () => {
+ const session = createSession("proj_mid_turn");
+ let reply: (value: Awaited<ReturnType<typeof invokeOpenScreenAgent>>) => void = () => undefined;
+ invokeMock.mockImplementation(() => new Promise((resolve) => (reply = resolve)));
+ const turn = runChat("proj_mid_turn", session.id, "question", config());
+ await vi.waitFor(() => expect(invokeMock).toHaveBeenCalled());
+ await deleteProjectChat("proj_mid_turn");
+ const document = createEmptyDocument({ title: "x", projectId: "proj_mid_turn" });
+ reply({ text: "late reply", document, mutated: false });
+
+ expect(await turn).toMatchObject({ success: false });
+ await restart();
+ expect(existsSync(path.join(root, "proj_mid_turn.chat.json"))).toBe(false);
+ expect(listSessions("proj_mid_turn")).toEqual([]);
+ });
+
+ it("reports a project deleted even when its chat history cannot be removed", async () => {
+ const documents = new DocumentService(root, root);
+ const doc = await documents.createProject("Doomed");
+ const service = new AiEditionService({
+ documents,
+ deleteChatHistory: async () => {
+ throw new Error("disk says no");
+ },
+ } as unknown as AiEditionServiceOptions);
+ const warn = vi.spyOn(console, "warn").mockImplementation(() => undefined);
+ await expect(service.deleteProject(doc.project.id)).resolves.toEqual({ success: true });
+ expect(warn).toHaveBeenCalled();
+ warn.mockRestore();
+ });
});
diff --git a/electron/ai-edition/chat-persistence.ts b/electron/ai-edition/chat-persistence.ts
index 82fa16a18..eb5ee144b 100644
--- a/electron/ai-edition/chat-persistence.ts
+++ b/electron/ai-edition/chat-persistence.ts
@@ -3,19 +3,11 @@
// must never stop the project itself from opening.
import { randomUUID } from "node:crypto";
-import {
- closeSync,
- existsSync,
- fsyncSync,
- mkdirSync,
- openSync,
- readFileSync,
- renameSync,
- rmSync,
- writeFileSync,
-} from "node:fs";
+import { existsSync, readFileSync } from "node:fs";
+import fs from "node:fs/promises";
import path from "node:path";
import { z } from "zod";
+import { renameWithRetry } from "./document-service";
const toolCallSchema = z.object({ name: z.string(), summary: z.string() });
const messageSchema = z.object({
@@ -47,6 +39,9 @@ const fileSchema = z.object({
export type StoredChatSession = z.infer<typeof sessionSchema>;
export class ChatPersistence {
+ /** Tail of the pending write/delete chain per project id — see `enqueue`. */
+ private readonly queues = new Map<string, Promise<void>>();
+
constructor(private readonly projectsRoot: string) {}
fileFor(projectId: string): string {
@@ -79,29 +74,51 @@ export class ChatPersistence {
}
}
- write(projectId: string, sessions: StoredChatSession[]): void {
+ /** Snapshots `sessions` now; the disk write waits its turn in the project's queue. */
+ async write(projectId: string, sessions: StoredChatSession[]): Promise<void> {
const destination = this.fileFor(projectId);
- mkdirSync(this.projectsRoot, { recursive: true });
- const temporary = `${destination}.tmp-${process.pid}-${randomUUID()}`;
// Explicitly choose fields; provider configuration and API keys never enter
// the chat format. Checkpoints remain process-local and are never serialized.
const sanitized = sessions.map((session) => sessionSchema.parse(session));
const json = JSON.stringify({ version: 1, projectId, sessions: sanitized });
- let fd: number | undefined;
- try {
- fd = openSync(temporary, "w");
- writeFileSync(fd, json, "utf8");
- fsyncSync(fd);
- closeSync(fd);
- fd = undefined;
- renameSync(temporary, destination);
- } finally {
- if (fd !== undefined) closeSync(fd);
- rmSync(temporary, { force: true });
- }
+ await this.enqueue(projectId, async () => {
+ await fs.mkdir(this.projectsRoot, { recursive: true });
+ const temporary = `${destination}.tmp-${process.pid}-${randomUUID()}`;
+ try {
+ const handle = await fs.open(temporary, "w");
+ try {
+ await handle.writeFile(json, "utf8");
+ await handle.sync();
+ } finally {
+ await handle.close();
+ }
+ await renameWithRetry(temporary, destination);
+ } finally {
+ await fs.rm(temporary, { force: true });
+ }
+ });
+ }
+
+ /** Queued behind the project's pending writes, so none of them can recreate the file. */
+ async delete(projectId: string): Promise<void> {
+ const file = this.fileFor(projectId);
+ await this.enqueue(projectId, () => fs.rm(file, { force: true }));
+ }
+
+ /** Settles once every write and delete queued so far has. */
+ async flush(): Promise<void> {
+ await Promise.all(this.queues.values());
}
- delete(projectId: string): void {
- rmSync(this.fileFor(projectId), { force: true });
+ // Same chain as DocumentService.writeProject: one queue per project, run on
+ // both settlements so a failed write does not cancel the next one.
+ private enqueue(projectId: string, task: () => Promise<void>): Promise<void> {
+ const run = (this.queues.get(projectId) ?? Promise.resolve()).then(task, task);
+ const settled = run.catch(() => undefined);
+ this.queues.set(projectId, settled);
+ void settled.then(() => {
+ if (this.queues.get(projectId) === settled) this.queues.delete(projectId);
+ });
+ return run;
}
}
diff --git a/electron/ai-edition/chat-service.ts b/electron/ai-edition/chat-service.ts
index 41ffe9bef..c82cc4748 100644
--- a/electron/ai-edition/chat-service.ts
+++ b/electron/ai-edition/chat-service.ts
@@ -47,15 +47,28 @@ export function configureChatPersistence(projectsRoot: string): void {
}
function persistProject(projectId: string): void {
- if (persistence) persistence.write(projectId, [...getProjectSessions(projectId).values()]);
+ const sessions = sessionsByProject.get(projectId);
+ // No cache entry means the project was deleted, possibly mid-turn: writing
+ // now would recreate the history its deletion just removed.
+ if (!persistence || !sessions) return;
+ // Off the event loop and best effort: the cache stays the source of truth
+ // while the app runs, and a chat file must never fail a chat turn.
+ persistence.write(projectId, [...sessions.values()]).catch((error) => {
+ console.warn(`[ai-edition] could not save chat history for ${projectId}:`, error);
+ });
}
-export function deleteProjectChat(projectId: string): void {
- persistence?.delete(projectId);
+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();
+}
+
+/** Lets queued chat-history writes reach the disk before the app exits. */
+export function flushChatPersistence(): Promise<void> {
+ return persistence?.flush() ?? Promise.resolve();
}
// ponytail: per-message checkpoints, stored as an ordered list per session
@@ -447,6 +460,12 @@ export async function runChat(
cursor: env.cursor,
});
+ // The project was deleted while the agent ran: its sessions are gone, so a
+ // reply recorded now would be reported as sent and then never be seen again.
+ if (sessionsByProject.get(projectId) !== sessions) {
+ return { success: false, error: "The project was deleted while the agent was replying." };
+ }
+
if (!result.text) {
// ponytail: surface the deep-agent's diagnostic so the user can see
// *why* the model produced no text (e.g. MiniMax streaming only
diff --git a/electron/ai-edition/document-service.ts b/electron/ai-edition/document-service.ts
index 6314bd0bf..59818d839 100644
--- a/electron/ai-edition/document-service.ts
+++ b/electron/ai-edition/document-service.ts
@@ -138,7 +138,7 @@ function parseLoadedDocument(raw: string): AxcutDocument {
* than surface a save error the user cannot act on. POSIX renames don't hit this
* and take the first attempt.
*/
-async function renameWithRetry(from: string, to: string): Promise<void> {
+export async function renameWithRetry(from: string, to: string): Promise<void> {
const RETRYABLE = new Set(["EPERM", "EACCES", "EBUSY"]);
for (let attempt = 0; ; attempt++) {
try {
diff --git a/electron/main.ts b/electron/main.ts
index e8e983044..b036e908c 100644
--- a/electron/main.ts
+++ b/electron/main.ts
@@ -21,6 +21,7 @@ import {
PRODUCT_NAME,
usesNativeAboutPanel,
} from "./about";
+import { flushChatPersistence } from "./ai-edition/chat-service";
import { AppSettingsStore } from "./app-settings";
import {
blockedFromInstalling,
@@ -1109,7 +1110,7 @@ app.on("activate", () => {
}
});
-let sttShutdownPromise: Promise<void> | null = null;
+let sttShutdownPromise: Promise<unknown> | null = null;
let sttShutdownFinished = false;
// Electron does not wait for an async event listener. Hold the first quit long
@@ -1126,14 +1127,16 @@ app.on("before-quit", (event) => {
if (sttShutdownFinished) return;
event.preventDefault();
if (sttShutdownPromise) return;
- sttShutdownPromise = shutdownStt()
- .catch((error) => {
+ sttShutdownPromise = Promise.all([
+ shutdownStt().catch((error) => {
console.error("[stt] Failed to stop whisper helper during app quit:", error);
- })
- .finally(() => {
- sttShutdownFinished = true;
- app.quit();
- });
+ }),
+ // Chat history is written asynchronously; let a reply that just landed reach the disk.
+ flushChatPersistence(),
+ ]).finally(() => {
+ sttShutdownFinished = true;
+ app.quit();
+ });
});
app.on("will-quit", () => {
diff --git a/electron/native-bridge/services/aiEditionService.ts b/electron/native-bridge/services/aiEditionService.ts
index f06d79c67..01e880a3e 100644
--- a/electron/native-bridge/services/aiEditionService.ts
+++ b/electron/native-bridge/services/aiEditionService.ts
@@ -36,7 +36,7 @@ import type { McpController } from "../../mcp/mcp-controller";
export interface AiEditionServiceOptions {
documents: DocumentService;
- deleteChatHistory?: (projectId: string) => void;
+ deleteChatHistory?: (projectId: string) => Promise<void>;
/**
* A factory, not an instance: building `LlmConfigStore` does two sync
* readFileSync plus a `safeStorage` decrypt, and on macOS that decrypt is
@@ -147,14 +147,20 @@ export class AiEditionService {
async deleteProject(projectId: string): Promise<AiEditionDocumentResult> {
try {
await this.options.documents.deleteProject(projectId);
- this.options.deleteChatHistory?.(projectId);
- return { success: true };
} catch (error) {
return {
success: false,
error: error instanceof Error ? error.message : String(error),
};
}
+ try {
+ await this.options.deleteChatHistory?.(projectId);
+ } catch (error) {
+ // The project is already gone: a chat file left behind is orphaned data,
+ // not a failed delete.
+ console.warn(`[ai-edition] could not remove chat history for ${projectId}:`, error);
+ }
+ return { success: true };
}
async addAsset(