From f1bbaea6328453660127934514a11f5b3deda8d5 Mon Sep 17 00:00:00 2001 From: Aiden Cline <63023139+rekram1-node@users.noreply.github.com> Date: Thu, 12 Nov 2026 17:43:40 -0600 Subject: [PATCH] fix(core): return empty structured output from patch (#63618) --- packages/core/src/tool/plugin/patch.ts | 53 +++++++++----------------- packages/core/test/tool-patch.test.ts | 32 +++++++--------- 2 files changed, 31 insertions(+), 54 deletions(-) diff --git a/packages/core/src/tool/plugin/patch.ts b/packages/core/src/tool/plugin/patch.ts index 58225b37b17..b8043dc4923 100644 --- a/packages/core/src/tool/plugin/patch.ts +++ b/packages/core/src/tool/plugin/patch.ts @@ -26,25 +26,7 @@ export const Input = Schema.Struct({ }), }) -export const Applied = Schema.Struct({ - type: Schema.Literals(["add", "update", "delete"]), - resource: Schema.String, - target: Schema.String, -}) - -export const Output = Schema.Struct({ - applied: Schema.Array(Applied), - files: Schema.Array(FileDiff.Info), -}) -export type Output = typeof Output.Type - -export const toModelContent = (output: Output) => - [ - "Success. Updated the following files:", - ...output.applied.map( - (item) => `${item.type === "add" ? "A" : item.type === "delete" ? "D" : "M"} ${item.resource}`, - ), - ].join("\n") +export const Output = Schema.Record(Schema.String, Schema.Never) type Prepared = | (Extract & { @@ -86,7 +68,7 @@ export const Plugin = { freeform: { type: "lark", grammar: GRAMMAR }, output: Output, execute: (input, context) => { - const applied: Array = [] + const completed: string[] = [] const parsed = Patch.parse(input.patchText) const lockTargets = Result.isSuccess(parsed) ? parsed.success.flatMap((hunk) => [ @@ -97,9 +79,8 @@ export const Plugin = { ]) : [] const fail = (operation: string, error: unknown) => { - const completed = applied.map((item) => item.resource).join(", ") return new ToolFailure({ - message: `${operation}: ${errorMessage(error)}${completed ? `. Completed before failure: ${completed}` : ""}`, + message: `${operation}: ${errorMessage(error)}${completed.length ? `. Completed before failure: ${completed.join(", ")}` : ""}`, }) } return Effect.gen(function* () { @@ -200,9 +181,7 @@ export const Plugin = { } else { updates.set(target.absolute, next) } - }).pipe( - Effect.mapError(toolFailure(`Unable to prepare patch at ${hunk.path}`)), - ) + }).pipe(Effect.mapError(toolFailure(`Unable to prepare patch at ${hunk.path}`))) } const patchFiles = prepared.map((change) => patchFile(change)) @@ -244,7 +223,7 @@ export const Plugin = { fail(`Wrote ${moveTarget.resource} but failed to remove ${change.target.resource}`, error), ), ) - applied.push({ type: change.type, resource: target.resource, target: target.absolute }) + completed.push(target.resource) }), { discard: true }, ) @@ -272,16 +251,18 @@ export const Plugin = { const target = change.type === "update" && change.moveTarget ? change.moveTarget : change.target return patchFile(change, formatted.get(target.absolute)) }) - return { applied, files } - }).pipe( - fileMutation.withLock(lockTargets), - Effect.map((output) => ({ - output, - content: toModelContent(output), - metadata: { files: output.files }, - })), - Effect.mapError(toolFailure("Unable to apply patch")), - ) + return { + // The multi-file patch contract intentionally leaves structured output empty. + output: {}, + content: [ + "Success. Updated the following files:", + ...files.map( + (file) => `${file.status === "added" ? "A" : file.status === "deleted" ? "D" : "M"} ${file.file}`, + ), + ].join("\n"), + metadata: { files }, + } + }).pipe(fileMutation.withLock(lockTargets), Effect.mapError(toolFailure("Unable to apply patch"))) }, }), ) diff --git a/packages/core/test/tool-patch.test.ts b/packages/core/test/tool-patch.test.ts index 9a0531880c0..2f64c3a079f 100644 --- a/packages/core/test/tool-patch.test.ts +++ b/packages/core/test/tool-patch.test.ts @@ -1,7 +1,7 @@ import fs from "fs/promises" import path from "path" import { describe, expect } from "bun:test" -import { Effect, Exit, Layer, Schema } from "effect" +import { Effect, Exit, Layer } from "effect" import { AppNodeBuilder } from "@opencode/core/effect/app-node-builder" import { LayerNode } from "@opencode/util/effect/layer-node" import { Environment } from "@opencode/core/environment/index" @@ -204,6 +204,7 @@ describe("PatchTool", () => { Effect.gen(function* () { const definitions = yield* toolDefinitions(registry) expect(definitions.map((tool) => tool.name)).toEqual(["patch", "execute"]) + expect(definitions[0]?.outputSchema).toEqual({ type: "object", additionalProperties: false }) expect(definitions[0]?.freeform).toEqual({ type: "lark", grammar: expect.stringContaining('"*** Begin Patch"'), @@ -224,12 +225,8 @@ describe("PatchTool", () => { ]) const modelText = settled.content?.[0]?.type === "text" ? settled.content[0].text : "" if (process.platform === "win32") expect(modelText).not.toContain("\\") - expect(settled.output).toMatchObject({ - applied: [ - { type: "add", resource: "nested/new.txt" }, - { type: "update", resource: "update.txt" }, - { type: "delete", resource: "remove.txt" }, - ], + expect(settled.output).toStrictEqual({}) + expect(settled.metadata).toMatchObject({ files: [ { file: "nested/new.txt", @@ -311,7 +308,7 @@ describe("PatchTool", () => { ) expect(settled.status).toBe("completed") if (settled.status !== "completed") return - expect(settled.output.files).toMatchObject([ + expect(settled.metadata?.files).toMatchObject([ { file: "trailing.txt", additions: 0, deletions: 1 }, { file: "unterminated.txt", additions: 0, deletions: 1 }, ]) @@ -371,7 +368,6 @@ describe("PatchTool", () => { ) expect(settled.status).toBe("completed") if (settled.status !== "completed") return - expect(settled.output.files[0]?.patch).toContain("+FORMATTED") expect(settled.metadata?.files?.[0]?.patch).toContain("+FORMATTED") expect(yield* Effect.promise(() => fs.readFile(target, "utf8"))).toBe("FORMATTED\n") }) @@ -483,7 +479,7 @@ describe("PatchTool", () => { ), ) - it.live("includes move file info in output and metadata", () => + it.live("includes move file info in metadata", () => withTempTool((directory, registry) => Effect.gen(function* () { const source = path.join(directory, "old", "name.txt") @@ -497,8 +493,7 @@ describe("PatchTool", () => { ) expect(settled.status).toBe("completed") if (settled.status !== "completed") return - expect(settled.output).toMatchObject({ - applied: [{ type: "update", resource: "renamed/dir/name.txt" }], + expect(settled.metadata).toMatchObject({ files: [ { file: "renamed/dir/name.txt", @@ -554,7 +549,7 @@ describe("PatchTool", () => { ) expect(settled).toMatchObject({ status: "completed", - output: { applied: [{ resource: "moved.txt" }] }, + metadata: { files: [{ file: "moved.txt" }] }, }) expect(assertions).toMatchObject([{ action: "edit", resources: ["old.txt", "moved.txt"] }]) }), @@ -747,10 +742,11 @@ describe("PatchTool", () => { ) expect(settled.status).toBe("completed") if (settled.status !== "completed") return - const output = Schema.decodeUnknownSync(PatchTool.Output)(settled.output) - expect(output.files[0]?.patch).not.toContain(bom) - expect(output.files[0]?.patch).not.toContain("-using System;") - expect(output.files[0]?.patch).not.toContain("+using System;") + const patch = settled.metadata?.files?.[0]?.patch + expect(patch).toContain("+class Next {}") + expect(patch).not.toContain(bom) + expect(patch).not.toContain("-using System;") + expect(patch).not.toContain("+using System;") expect(yield* Effect.promise(() => fs.readFile(target, "utf8"))).toBe( `${bom}using System;\n\nclass Test {}\nclass Next {}\n`, ) @@ -1110,7 +1106,7 @@ describe("PatchTool", () => { ) expect(settled).toMatchObject({ status: "completed", - output: { applied: [{ resource: destination.replaceAll("\\", "/") }] }, + metadata: { files: [{ file: destination.replaceAll("\\", "/") }] }, }) expect(assertions).toMatchObject([ {