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): run a turn's tool calls one at a time so parallel writes all land

EtienneLescot07c082582759
diff --git a/electron/ai-edition/deep-agent/service.test.ts b/electron/ai-edition/deep-agent/service.test.ts
index fffbeb4c5..688ddc3ce 100644
--- a/electron/ai-edition/deep-agent/service.test.ts
+++ b/electron/ai-edition/deep-agent/service.test.ts
@@ -360,6 +360,38 @@ describe("the sink announces each call exactly once, with the real verdict", ()
expect(holder.current).not.toBe(before);
expect(holder.current.timeline.trimRanges).toHaveLength(2);
});
+
+ // The agent runs every tool call of one model step at once: each is its own
+ // `Send` to the tools node. Two writes that both start from the same snapshot
+ // both report success, and the one that finishes last keeps only its own edit.
+ // A zoom reads the cursor track first, so it finishes after the trims beside it:
+ // that is how a turn reported "added 3 trims" over a document holding none.
+ it("keeps every write of a step whose calls run at the same time", async () => {
+ const runtime = {
+ cursor: {
+ read: async ({ assetId }: { assetId: string }) => {
+ await new Promise((resolve) => setTimeout(resolve, 10));
+ return { status: "ok" as const, assetId, samples: [] };
+ },
+ },
+ };
+ const { sink, events } = recordingSink();
+ const holder = { current: fixtureDocument() };
+ const tools: BuiltTool[] = buildTools(holder, sink, true, runtime);
+ const zoom = tools.find((t) => t.name === "addZoom");
+ const trim = tools.find((t) => t.name === "addTrim");
+ if (!zoom || !trim) throw new Error("addZoom / addTrim are not built");
+
+ await Promise.all([
+ zoom.invoke({ startSec: 4, endSec: 6 }),
+ trim.invoke({ startSec: 1, endSec: 2 }),
+ trim.invoke({ startSec: 20, endSec: 21 }),
+ ]);
+
+ expect(events.filter((e) => e.kind === "toolEnd").every((e) => e.ok)).toBe(true);
+ expect(holder.current.zoomRanges).toHaveLength(1);
+ expect(holder.current.timeline.trimRanges).toHaveLength(3);
+ });
});
// ─── The surfaces that describe the tools ──────────────────────────────────
diff --git a/electron/ai-edition/deep-agent/service.ts b/electron/ai-edition/deep-agent/service.ts
index 69e0b85a8..00fc819ee 100644
--- a/electron/ai-edition/deep-agent/service.ts
+++ b/electron/ai-edition/deep-agent/service.ts
@@ -371,19 +371,42 @@ function documentTool<S extends z.ZodType>(
schema: S,
editsAllowed: boolean,
runtime: ToolRuntime,
+ inTurn: TurnQueue,
) {
return tool(
- async (args: z.infer<S>) => {
- sink.toolStart(name, args);
- const execution = await runDocumentTool(holder.current, name, args, editsAllowed, runtime);
- if (execution.document) holder.current = execution.document;
- sink.toolEnd(name, execution.ok, execution.summary);
- return execution.resultJson;
- },
+ (args: z.infer<S>) =>
+ inTurn(async () => {
+ sink.toolStart(name, args);
+ const execution = await runDocumentTool(holder.current, name, args, editsAllowed, runtime);
+ if (execution.document) holder.current = execution.document;
+ sink.toolEnd(name, execution.ok, execution.summary);
+ return execution.resultJson;
+ }),
{ name, description: TOOL_DESCRIPTIONS[name], schema },
);
}
+type TurnQueue = <T>(run: () => Promise<T>) => Promise<T>;
+
+/**
+ * Runs a turn's tool calls one at a time, in the order the agent starts them.
+ *
+ * The agent starts every call of one model step at once (each is its own `Send`
+ * to the tools node). Each call reads the holder, awaits, then writes the holder,
+ * so two at once both start from the same document and the last to finish keeps
+ * only its own edit, while every one of them reports success. A zoom reads the
+ * cursor track before it executes, so it finishes after the trims sent beside it:
+ * that is how a turn reported "added 3 trims" over a document holding none.
+ */
+function turnQueue(): TurnQueue {
+ let queue: Promise<unknown> = Promise.resolve();
+ return (run) => {
+ const next = queue.then(run, run);
+ queue = next.catch(() => undefined);
+ return next;
+ };
+}
+
/**
* Run one tool against a document: the cursor read it may need, then the shared
* executor. The in-app agent (`documentTool`) and the MCP server both call this,
@@ -499,8 +522,9 @@ export function buildTools(
editsAllowed = true,
runtime: ToolRuntime = {},
) {
+ const inTurn = turnQueue();
return TOOL_ARG_SCHEMAS.map(([name, schema]) =>
- documentTool(holder, sink, name, schema, editsAllowed, runtime),
+ documentTool(holder, sink, name, schema, editsAllowed, runtime, inTurn),
);
}