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(agent): let a malformed batch entry fall alone, like every other one

Etienne Lescot9bcfeee97342
diff --git a/electron/ai-edition/agent-tools.test.ts b/electron/ai-edition/agent-tools.test.ts
index ffed945e5..4fe706b87 100644
--- a/electron/ai-edition/agent-tools.test.ts
+++ b/electron/ai-edition/agent-tools.test.ts
@@ -322,6 +322,53 @@ describe("executeAgentTool", () => {
expect(result.summary).toMatch(/added 2 trims, 1 refused/);
});
+ it("addTrims refuses a MALFORMED range by itself, not the whole call", () => {
+ // The batch schema advertises the element shape without enforcing it, so a
+ // bad entry reaches the unitary executor and is refused at its index. If it
+ // were enforced at the container, one typo would cost every other cut —
+ // which is precisely what `applyBatch` says it exists to prevent.
+ const result = executeAgentTool(
+ fixtureDocument(),
+ "addTrims",
+ JSON.stringify({
+ ranges: [{ startSec: 1, endSec: 2 }, { startSec: "oops" }, { startSec: 40, endSec: 41 }],
+ }),
+ );
+
+ expect(result.ok).toBe(true);
+ const payload = JSON.parse(result.resultJson);
+ expect(payload.appliedCount).toBe(2);
+ expect(payload.refused).toEqual([{ index: 1, error: expect.stringMatching(/endSec/) }]);
+ expect(result.document?.timeline.trimRanges).toHaveLength(3);
+ });
+
+ it("addZooms refuses a MALFORMED region by itself, not the whole call", () => {
+ const result = executeAgentTool(
+ fixtureDocument(),
+ "addZooms",
+ JSON.stringify({
+ regions: [
+ { startSec: 1, endSec: 3, depth: 9 }, // depth is an ordinal 1–6
+ { startSec: 10, endSec: 12 },
+ ],
+ }),
+ );
+
+ expect(result.ok).toBe(true);
+ const payload = JSON.parse(result.resultJson);
+ expect(payload.appliedCount).toBe(1);
+ expect(payload.refused[0].index).toBe(0);
+ expect(result.document?.zoomRanges).toHaveLength(1);
+ });
+
+ it("addTrims still refuses a batch that is not a non-empty list", () => {
+ for (const args of ['{"ranges":[]}', '{"ranges":"1-2"}', "{}"]) {
+ const result = executeAgentTool(fixtureDocument(), "addTrims", args);
+ expect(result.ok).toBe(false);
+ expect(result.document).toBeUndefined();
+ }
+ });
+
it("addTrims reports a whole-batch refusal as a failure, not an empty success", () => {
const result = executeAgentTool(
fixtureDocument(),
diff --git a/electron/ai-edition/agent-tools.ts b/electron/ai-edition/agent-tools.ts
index f53d06598..67bf72a9b 100644
--- a/electron/ai-edition/agent-tools.ts
+++ b/electron/ai-edition/agent-tools.ts
@@ -356,13 +356,22 @@ export const addTrimArgs = z.object({
* different things. A separate element schema would be one more place to forget
* `clipId` the next time the unitary one grows a field.
*
+ * The `union(…, unknown)` is what makes "each item stands or falls alone" true for
+ * MALFORMED items too, not just unplaceable ones. A bare `z.array(addTrimArgs)`
+ * rejects the whole call the moment one entry is bad — and it rejects it in
+ * LangChain, before `applyBatch` runs — so nine good cuts would be thrown away
+ * with the tenth and `refused[index]` could never name it. Advertising the
+ * union keeps the element shape in the JSON schema the model reads (it shows up
+ * as `anyOf: [addTrim, {}]`) while letting a bad entry through to the unitary
+ * executor, which refuses it by itself with the wording it always uses.
+ *
* No cap on the array. A half-hour recording has hundreds of silences, and the
* point of this tool is precisely that it should not have to guess how many are
* too many. Picking a number here would repeat the mistake `getTranscript` made
* with its 800.
*/
export const addTrimsArgs = z.object({
- ranges: z.array(addTrimArgs).min(1),
+ ranges: z.array(z.union([addTrimArgs, z.unknown()])).min(1),
});
export const setTrimArgs = z.object({
@@ -427,9 +436,10 @@ export const addZoomArgs = z.object({
focus: focusSchema.default({ cx: 0.5, cy: 0.5 }),
});
-/** Same contract as `addTrimsArgs`: the element schema IS the unitary one. */
+/** Same contract as `addTrimsArgs`: the element schema IS the unitary one, and
+ * it is advertised rather than enforced so a bad region is refused by itself. */
export const addZoomsArgs = z.object({
- regions: z.array(addZoomArgs).min(1),
+ regions: z.array(z.union([addZoomArgs, z.unknown()])).min(1),
});
export const setZoomArgs = z.object({
diff --git a/electron/ai-edition/deep-agent/service.test.ts b/electron/ai-edition/deep-agent/service.test.ts
index 2a76fc8ef..43e963c22 100644
--- a/electron/ai-edition/deep-agent/service.test.ts
+++ b/electron/ai-edition/deep-agent/service.test.ts
@@ -175,7 +175,7 @@ function recordingSink(): { sink: OpenScreenAgentSink; events: SinkEvent[] } {
}
/** `buildTools` returns a tuple with a DISTINCT type per tool, one per zod
- * schema, so `tools.find(...)` is a 19-way union — and `.invoke` is generic, a
+ * schema, so `tools.find(...)` is a 21-way union — and `.invoke` is generic, a
* shape TypeScript will not call through a union (TS2349). Widening to the
* interface every one of them implements is what the model is handed anyway:
* `createAgent` takes them as `ClientTool`, i.e. exactly this. Nothing the
@@ -292,6 +292,23 @@ describe("the sink announces each call exactly once, with the real verdict", ()
expect(events[1]).toMatchObject({ kind: "toolEnd", name: "getTranscript", ok: false });
});
+ it("lets a malformed batch entry reach the executor instead of throwing at the schema", async () => {
+ // LangChain parses the tool's schema BEFORE calling us, so a batch schema
+ // that enforced its element shape would reject the whole call here — the
+ // per-item `refused[index]` that `addTrims` promises the model could never
+ // happen on the product path, only in a direct-executor test.
+ const { tools, holder } = toolsFor(fixtureDocument());
+ const tool = tools.find((t) => t.name === "addTrims");
+ if (!tool) throw new Error("addTrims is not built");
+
+ const result = JSON.parse(
+ String(await tool.invoke({ ranges: [{ startSec: 1, endSec: 2 }, { startSec: "oops" }] })),
+ );
+ expect(result.appliedCount).toBe(1);
+ expect(result.refused[0].index).toBe(1);
+ expect(holder.current.timeline.trimRanges).toHaveLength(2);
+ });
+
it("advances the holder on a write, and leaves it alone on a refusal", async () => {
const { tools, holder } = toolsFor(fixtureDocument());
const before = holder.current;
@@ -402,7 +419,7 @@ describe("the prompt when the user has turned project edits off", () => {
});
describe("the tools when the user has turned project edits off", () => {
- it("still builds all 20 — the model has to be able to NAME the edit", () => {
+ it("still builds all 21 — the model has to be able to NAME the edit", () => {
const { sink } = recordingSink();
const tools: BuiltTool[] = buildTools({ current: fixtureDocument() }, sink, false);
expect(tools.map((t) => t.name)).toEqual(OPENSCREEN_TOOLS);