Skip to content

Commit cbe52d2

Browse files
authored
fix(core): show Code Mode programs the same failure text as direct calls (anomalyco#60109)
1 parent c45aae7 commit cbe52d2

3 files changed

Lines changed: 27 additions & 18 deletions

File tree

‎packages/core/src/codemode/tool.ts‎

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
export * as CodeModeTool from "./tool.js"
22

3+
import { ToolFailure } from "@opencode/ai/effect"
34
import { CodeMode, Namespace, Tool } from "@opencode/codemode"
45
import type {
56
Content,
@@ -11,6 +12,7 @@ import type {
1112
Result,
1213
} from "@opencode/schema/tool"
1314
import { Clock, Context, Effect, Ref, Schema, Semaphore } from "effect"
15+
import { toSessionError } from "../session/to-session-error.js"
1416
import { definition, normalizedName } from "../tool/runtime.js"
1517
import { CodeModeCatalog } from "./catalog.js"
1618
import { CodeModeWeb } from "./web.js"
@@ -120,7 +122,10 @@ export const create = (
120122
executeTool: (name, tool, input) =>
121123
Effect.gen(function* () {
122124
const index = yield* Ref.getAndUpdate(callIndex, (index) => index + 1)
123-
const executed = yield* executeTool(name, tool, input, context)
125+
// The program sees the same failure text the model gets for a direct call.
126+
const executed = yield* executeTool(name, tool, input, context).pipe(
127+
Effect.mapError((error) => new ToolFailure({ message: toSessionError(error).message, error })),
128+
)
124129
const content = resultContent(executed)
125130
const outputFileParts = outputFiles(content)
126131
if (outputFileParts.length > 0)

‎packages/core/test/mcp.test.ts‎

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1619,7 +1619,6 @@ it.live("discovers and reads MCP resources through Code Mode", () =>
16191619
'return await tools.opencode.read_mcp_resource({ server: "resources", uri: "docs://gone" })',
16201620
)
16211621
expect(gone.metadata?.error).toBe(true)
1622-
expect(gone.output.output).toContain("Unable to read MCP resource resources:docs://gone")
16231622
expect(gone.output.output).toContain("Resource not found")
16241623
const missing = yield* run(
16251624
'return await tools.opencode.read_mcp_resource({ server: "missing", uri: "docs://readme" })',

‎packages/core/test/tool-opencode.test.ts‎

Lines changed: 21 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -144,17 +144,15 @@ it.effect("groups available models by provider with paging", () =>
144144
}),
145145
)
146146

147-
const approvals = { assertions: [] as Permission.AssertInput[], denyEdit: false }
147+
const approvals = { assertions: [] as Permission.AssertInput[], rejectEdit: false }
148148
const worktreeIt = testEffect(
149149
makePluginTestLayer(
150150
permissionLayer({
151151
assert: (input) =>
152152
Effect.sync(() => approvals.assertions.push(input)).pipe(
153153
Effect.andThen(
154-
approvals.denyEdit && input.action === "edit"
155-
? Effect.fail(
156-
new Permission.BlockedError({ rules: [], permission: input.action, resources: input.resources }),
157-
)
154+
approvals.rejectEdit && input.action === "edit"
155+
? Effect.fail(new Permission.CorrectedError({ feedback: "Keep this worktree" }))
158156
: Effect.void,
159157
),
160158
),
@@ -193,17 +191,14 @@ worktreeIt.live("creates, lists, and removes worktrees for the plugin's project"
193191
yield* OpenCodeTools.Plugin.effect(yield* PluginHost.make(plugins))
194192
const session = yield* sessions.create({ location: Location.Ref.make({ directory: location.directory }) })
195193
const registry = yield* Tool.Service
196-
const call = (name: string, input: Record<string, unknown>) =>
194+
const program = (code: string) =>
197195
executeTool(registry, {
198196
sessionID: session.id,
199197
...toolIdentity,
200-
call: {
201-
type: "tool-call",
202-
id: `call-${name}-${JSON.stringify(input)}`,
203-
name: "execute",
204-
input: { code: `return await tools.opencode.${name}(${JSON.stringify(input)})` },
205-
},
198+
call: { type: "tool-call", id: `call-${code}`, name: "execute", input: { code } },
206199
})
200+
const call = (name: string, input: Record<string, unknown>) =>
201+
program(`return await tools.opencode.${name}(${JSON.stringify(input)})`)
207202
const run = (name: string, input: Record<string, unknown>) =>
208203
call(name, input).pipe(
209204
Effect.map((result) => JSON.parse(result.content?.[0]?.type === "text" ? result.content[0].text : "")),
@@ -234,12 +229,21 @@ worktreeIt.live("creates, lists, and removes worktrees for the plugin's project"
234229
expect(yield* Effect.promise(() => Bun.file(path.join(directory, "draft.txt")).exists())).toBe(true)
235230
yield* Effect.promise(() => fs.rm(path.join(directory, "draft.txt")))
236231

237-
// Removal requires the same approval as editing files in the worktree.
232+
// Removal requires the same approval as editing files in the worktree. Rejection feedback reaches the
233+
// program and the execute result, as it would reach the model from a direct call.
238234
approvals.assertions.length = 0
239-
approvals.denyEdit = true
240-
expect(yield* call("worktree_remove", { directory })).toMatchObject({ metadata: { error: true } })
235+
approvals.rejectEdit = true
236+
expect(yield* call("worktree_remove", { directory })).toMatchObject({
237+
content: [{ type: "text", text: "Keep this worktree" }],
238+
metadata: { error: true },
239+
})
240+
expect(
241+
yield* program(
242+
`try { await tools.opencode.worktree_remove(${JSON.stringify({ directory })}) } catch (error) { return error.message }`,
243+
),
244+
).toMatchObject({ content: [{ type: "text", text: "Keep this worktree" }] })
241245
expect(yield* Effect.promise(() => Bun.file(path.join(directory, ".git")).exists())).toBe(true)
242-
approvals.denyEdit = false
246+
approvals.rejectEdit = false
243247

244248
// Relative paths resolve from the session's directory for both approval and removal.
245249
expect(yield* run("worktree_remove", { directory: path.relative(location.directory, directory) })).toEqual({
@@ -252,6 +256,7 @@ worktreeIt.live("creates, lists, and removes worktrees for the plugin's project"
252256
expect(approvals.assertions).toMatchObject([
253257
{ action: "edit", resources: [resource], save: ["*"], sessionID: session.id },
254258
{ action: "edit", resources: [resource], save: ["*"], sessionID: session.id },
259+
{ action: "edit", resources: [resource], save: ["*"], sessionID: session.id },
255260
])
256261
expect((yield* run("worktree_list", {})).worktrees).not.toContainEqual({ directory, strategy: "git" })
257262
}),

0 commit comments

Comments
 (0)