fix(plugin): return Promise tool throws as tool failures (#54913)

This commit is contained in:
Aiden Cline
2026-10-12 15:37:34 -05:00
committed by GitHub
Unverified
parent 80fb31ebb7
commit 1438073271
4 changed files with 118 additions and 13 deletions
+9 -7
View File
@@ -398,17 +398,19 @@ export const layer = Layer.effect(
retry: (event: Parameters<Prepared["retry"]>[0]) =>
hooks.trigger("session", "retry", event).pipe(Effect.asVoid),
// Permission.assert and the question tool throw declines as defects so tools cannot
// catch them and turn a "no" into model-visible output. Recover them here as failures.
// catch them and turn a "no" into model-visible output. Recover them here as failures,
// including declines a tool rethrew and its wrapper attached to a Tool.Error, as Promise
// plugin tools do.
executeTool: (call: Parameters<Prepared["executeTool"]>[0]) =>
tools.execute({ ...call, definitions: hooked }).pipe(
Effect.catchCauseFilter(
(cause) => {
const decline = cause.reasons.flatMap((r) =>
Cause.isDieReason(r) &&
(r.defect instanceof Permission.DeclinedError || r.defect instanceof QuestionTool.CancelledError)
? [r.defect]
: [],
)[0]
const decline = cause.reasons
.map((r) => (Cause.isDieReason(r) ? r.defect : Cause.isFailReason(r) ? r.error.error : undefined))
.find(
(value): value is Permission.DeclinedError | QuestionTool.CancelledError =>
value instanceof Permission.DeclinedError || value instanceof QuestionTool.CancelledError,
)
return decline ? Result.succeed(decline) : Result.fail(cause)
},
(decline) => Effect.fail(decline),
+84 -1
View File
@@ -1,12 +1,13 @@
import { expect } from "bun:test"
import { Agent } from "@opencode/core/agent"
import { Permission } from "@opencode/core/permission"
import { Plugin } from "@opencode/core/plugin"
import { PluginHost } from "@opencode/core/plugin/host"
import { PluginPromise } from "@opencode/core/plugin/promise"
import { Tool } from "@opencode/core/tool"
import { Session } from "@opencode/schema/session"
import { SessionMessage } from "@opencode/schema/session-message"
import { Cause, Deferred, Effect, Exit, Fiber } from "effect"
import { Cause, Deferred, Effect, Exit, Fiber, Option } from "effect"
import { testEffect } from "../lib/effect"
import { PluginTestLayer } from "./fixture"
@@ -53,3 +54,85 @@ it.live("Promise tool executors receive interruption through their AbortSignal",
expect(Exit.isFailure(exit) && Cause.hasInterruptsOnly(exit.cause)).toBe(true)
}),
)
const execute = (snapshot: Tool.Snapshot, name: string) =>
snapshot
.execute({
sessionID: Session.ID.make("ses_promise_tool_error"),
agent: Agent.ID.make("build"),
messageID: SessionMessage.ID.make("msg_promise_tool_error"),
call: { type: "tool-call", id: `call_${name}`, name, input: {} },
})
.pipe(Effect.exit)
it.live("Promise tool throws become tool failures", () =>
Effect.gen(function* () {
const plugins = yield* Plugin.Service
const tools = yield* Tool.Service
yield* PluginPromise.fromPromise({
id: "throwing-tools",
async setup(context) {
await context.tool.transform((editor) => {
editor.add({
name: "plain",
description: "Throw an Error",
input: { type: "object", properties: {}, additionalProperties: false },
options: { codemode: false },
execute: async () => {
throw new Error("plain failure")
},
})
editor.add({
name: "typed",
description: "Throw a Tool.Error",
input: { type: "object", properties: {}, additionalProperties: false },
options: { codemode: false },
execute: async () => {
throw new Tool.Error({ message: "typed failure", metadata: { code: 7 } })
},
})
})
},
}).effect(yield* PluginHost.make(plugins))
const snapshot = yield* tools.snapshot()
const plain = yield* execute(snapshot, "plain")
const typed = yield* execute(snapshot, "typed")
expect(Option.getOrUndefined(Exit.findErrorOption(plain))).toMatchObject({ message: "plain failure" })
expect(Option.getOrUndefined(Exit.findErrorOption(typed))).toMatchObject({
message: "typed failure",
metadata: { code: 7 },
})
}),
)
it.live("Promise tools that rethrow a decline attach it to the tool failure", () =>
Effect.gen(function* () {
const plugins = yield* Plugin.Service
const tools = yield* Tool.Service
const declined = new Permission.DeclinedError()
yield* tools.transform((editor) =>
editor.add({
name: "guarded",
description: "Decline every call",
input: { type: "object", properties: {}, additionalProperties: false },
options: { codemode: false },
execute: () => Effect.die(declined),
}),
)
yield* PluginPromise.fromPromise({
id: "wrapping-tool",
async setup(context) {
await context.tool.transform((editor) =>
editor.update("guarded", (tool) => {
const original = tool.execute
tool.execute = (input, context) => original(input, context)
}),
)
},
}).effect(yield* PluginHost.make(plugins))
const exit = yield* execute(yield* tools.snapshot(), "guarded")
expect(Option.getOrUndefined(Exit.findErrorOption(exit))?.error).toBe(declined)
}),
)
+13
View File
@@ -4922,6 +4922,14 @@ describe("SessionRunnerLLM", () => {
output: Schema.Struct({}),
execute: () => Effect.die(new Permission.DeclinedError()),
},
wrapped: {
name: "wrapped",
description: "Fail with a decline attached to a tool error",
input: Schema.Struct({}),
output: Schema.Struct({}),
execute: () =>
Effect.fail(new Tool.Error({ message: "Wrapped decline", error: new Permission.DeclinedError() })),
},
},
{ codemode: false },
)
@@ -4931,6 +4939,7 @@ describe("SessionRunnerLLM", () => {
TestLLM.toolCalls(
LLMEvent.toolCall({ id: "call-failed", name: "failed", input: {} }),
LLMEvent.toolCall({ id: "call-declined", name: "declined", input: {} }),
LLMEvent.toolCall({ id: "call-wrapped", name: "wrapped", input: {} }),
),
)
@@ -4947,6 +4956,10 @@ describe("SessionRunnerLLM", () => {
{ id: "call-declined" },
{ error: { type: "aborted", message: "The user declined this tool call" } },
),
Expected.failedTool(
{ id: "call-wrapped" },
{ error: { type: "aborted", message: "The user declined this tool call" } },
),
]),
])
})
+12 -5
View File
@@ -622,10 +622,17 @@ function attempt<A>(evaluate: (signal: AbortSignal) => PromiseLike<A>) {
type RuntimeSchema = Schema.Codec<unknown, unknown>
const executePromiseTool = (tool: Info, input: any, context: Tool.Context) =>
Effect.promise((signal) =>
tool.execute(input, {
...context,
signal,
progress: (update) => Effect.runPromise(context.progress(update), { signal }),
Effect.tryPromise({
try: (signal) =>
tool.execute(input, {
...context,
signal,
progress: (update) => Effect.runPromise(context.progress(update), { signal }),
}),
catch: (error) => error,
}).pipe(
Effect.catch((error) => {
if (error instanceof Tool.Error) return Effect.fail(error)
return Effect.fail(new Tool.Error({ message: error instanceof Error ? error.message : String(error), error }))
}),
)