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(mcp): count the open-file dialog's read of a project file

EtienneLescotbcc40ccac8ea
diff --git a/electron/ai-edition/document-service.test.ts b/electron/ai-edition/document-service.test.ts
index 5f8d3f5ff..6abb18653 100644
--- a/electron/ai-edition/document-service.test.ts
+++ b/electron/ai-edition/document-service.test.ts
@@ -860,6 +860,21 @@ describe("DocumentService", () => {
expect((await service.getProject(created.project.id)).project.title).toBe("Synced");
});
+ it("counts the open-file dialog reading the project's own file as a read", async () => {
+ const created = await service.createProject("Browsed");
+ const { document, version } = await service.getProjectForUpdate(created.project.id);
+ await service.beforeProjectFileRead(path.join(tempDir, `${created.project.id}.openscreen`));
+ expect(await service.saveProjectIfUnchanged(renamed(document, "Edited"), version)).toBeNull();
+ });
+
+ it("ignores the open-file dialog reading a file outside the projects folder", async () => {
+ const created = await service.createProject("Elsewhere");
+ const { document, version } = await service.getProjectForUpdate(created.project.id);
+ await service.beforeProjectFileRead(path.join(mediaDir, `${created.project.id}.openscreen`));
+ const saved = await service.saveProjectIfUnchanged(renamed(document, "Edited"), version);
+ expect(saved?.project.title).toBe("Edited");
+ });
+
it("writes nothing over a project deleted since", async () => {
const created = await service.createProject("Deleted");
const { document, version } = await service.getProjectForUpdate(created.project.id);
diff --git a/electron/ai-edition/document-service.ts b/electron/ai-edition/document-service.ts
index 8045b0cba..1e3cf1c81 100644
--- a/electron/ai-edition/document-service.ts
+++ b/electron/ai-edition/document-service.ts
@@ -329,6 +329,24 @@ export class DocumentService {
return { document, version: { generation, raw } };
}
+ /**
+ * For a read of a project file that bypasses getProject: the open-file dialog,
+ * which then saves back what it read. If the file is one of this service's
+ * projects, that counts as a read of it, behind its queued saves, exactly as in
+ * getProject. Any other file is not this service's business.
+ */
+ async beforeProjectFileRead(filePath: string): Promise<void> {
+ const resolved = path.resolve(filePath);
+ if (path.relative(path.resolve(this.projectsRoot), path.dirname(resolved)) !== "") return;
+ const extension = [PROJECT_FILE_EXTENSION, LEGACY_PROJECT_FILE_EXTENSION].find((ext) =>
+ resolved.endsWith(ext),
+ );
+ if (!extension) return;
+ const projectId = path.basename(resolved, extension);
+ this.touch(projectId);
+ await this.writeQueues.get(projectId);
+ }
+
private touch(projectId: string): number {
const generation = (this.generations.get(projectId) ?? 0) + 1;
this.generations.set(projectId, generation);
diff --git a/electron/ipc/handlers.ts b/electron/ipc/handlers.ts
index 07c2141c9..78a370d95 100644
--- a/electron/ipc/handlers.ts
+++ b/electron/ipc/handlers.ts
@@ -4718,6 +4718,8 @@ export function registerIpcHandlers(
}
const filePath = result.filePaths[0];
+ // The editor saves back what it opened here: see beforeProjectFileRead.
+ await aiEditionDocuments.beforeProjectFileRead(filePath);
const content = await fs.readFile(filePath, "utf-8");
const project = await relinkProjectMedia(JSON.parse(content), DEFAULT_RECORDINGS_DIR);
currentProjectPath = filePath;
@@ -4764,6 +4766,7 @@ export function registerIpcHandlers(
if (!stats?.isFile()) {
return { success: false, message: "File not found" };
}
+ await aiEditionDocuments.beforeProjectFileRead(filePath);
const content = await fs.readFile(filePath, "utf-8");
const project = await relinkProjectMedia(JSON.parse(content), DEFAULT_RECORDINGS_DIR);
currentProjectPath = filePath;
diff --git a/technical-documentation/architecture/mcp-server.md b/technical-documentation/architecture/mcp-server.md
index 510635b98..e06c06cac 100644
--- a/technical-documentation/architecture/mcp-server.md
+++ b/technical-documentation/architecture/mcp-server.md
@@ -52,7 +52,7 @@ Only the webContents that registered on `ai-edition.mcp-host` is asked, and only
Read with `DocumentService.getProjectForUpdate` (`getProject`, migrated and relinked like any open, plus a version) and, if the tool changed it, saved with `saveProjectIfUnchanged` — the same instance every other save in the app goes through, so its per-project write queue still holds. There is no revision to guard with, so the save is refused with "NOT applied … re-read, then retry" when, since the read:
-1. **Anything read the project.** This is the editor opening it: from that read on it holds a copy without the MCP edit, and its next save would drop the edit without a word. A snapshot cannot see this in time, since the editor installs what it read only after the read returns. `DocumentService` counts every read and save of a project instead. The client's retry, with the same `projectId`, then goes through the editor.
+1. **Anything read the project.** This is the editor opening it: from that read on it holds a copy without the MCP edit, and its next save would drop the edit without a word. A snapshot cannot see this in time, since the editor installs what it read only after the read returns. `DocumentService` counts every read and save of a project instead, the open-file dialog's direct read of a project file included (`beforeProjectFileRead`). The client's retry, with the same `projectId`, then goes through the editor.
2. **Anything saved it, or its file changed.** The file must still be, byte for byte, the one the tool ran against. This also covers a writer outside the app (a sync tool, a restored copy) and a delete.
The check runs inside the project's write queue, and `getProject` waits behind that queue, so nothing in the app reads or saves the project between the check and the write.