Skip to content

Commit c313504

Browse files
fix(opencode): surface resumable subagent errors (anomalyco#43657)
Co-authored-by: rekram1-node <[email protected]>
1 parent e49772a commit c313504

2 files changed

Lines changed: 65 additions & 4 deletions

File tree

‎packages/opencode/src/tool/task.ts‎

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -210,6 +210,15 @@ export const TaskTool = Tool.define(
210210
agent: next.name,
211211
parts,
212212
})
213+
if (result.info.role === "assistant" && result.info.error) {
214+
const message =
215+
"message" in result.info.error.data && typeof result.info.error.data.message === "string"
216+
? result.info.error.data.message
217+
: result.info.error.name
218+
return yield* Effect.fail(
219+
new Error(`Subagent failed (task_id: ${nextSession.id}): ${message}`),
220+
)
221+
}
213222
return result.parts.findLast((item) => item.type === "text")?.text ?? ""
214223
})
215224

‎packages/opencode/test/tool/task.test.ts‎

Lines changed: 56 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ import { SessionV1 } from "@opencode-ai/core/v1/session"
33
import { Database } from "@opencode-ai/core/database/database"
44
import { LayerNode } from "@opencode-ai/core/effect/layer-node"
55
import { SessionProjector } from "@opencode-ai/core/session/projector"
6-
import { Deferred, Effect, Exit, Fiber, Layer } from "effect"
6+
import { Cause, Deferred, Effect, Exit, Fiber, Layer } from "effect"
77
import { Agent } from "../../src/agent/agent"
88
import { BackgroundJob } from "@/background/job"
99
import { EventV2Bridge } from "@/event-v2-bridge"
@@ -96,19 +96,27 @@ const seed = Effect.fn("TaskToolTest.seed")(function* (title = "Pinned") {
9696
return { chat, assistant }
9797
})
9898

99-
function stubOps(opts?: { onPrompt?: (input: SessionPrompt.PromptInput) => void; text?: string }): TaskPromptOps {
99+
function stubOps(opts?: {
100+
onPrompt?: (input: SessionPrompt.PromptInput) => void
101+
text?: string
102+
error?: NonNullable<SessionV1.Assistant["error"]>
103+
}): TaskPromptOps {
100104
return {
101105
cancel: () => Effect.void,
102106
resolvePromptParts: (template) => Effect.succeed([{ type: "text" as const, text: template }]),
103107
prompt: (input) =>
104108
Effect.sync(() => {
105109
opts?.onPrompt?.(input)
106-
return reply(input, opts?.text ?? "done")
110+
return reply(input, opts?.text ?? "done", opts?.error)
107111
}),
108112
}
109113
}
110114

111-
function reply(input: SessionPrompt.PromptInput, text: string): SessionV1.WithParts {
115+
function reply(
116+
input: SessionPrompt.PromptInput,
117+
text: string,
118+
error?: NonNullable<SessionV1.Assistant["error"]>,
119+
): SessionV1.WithParts {
112120
const id = MessageID.ascending()
113121
return {
114122
info: {
@@ -125,6 +133,7 @@ function reply(input: SessionPrompt.PromptInput, text: string): SessionV1.WithPa
125133
providerID: input.model?.providerID ?? ref.providerID,
126134
time: { created: Date.now() },
127135
finish: "stop",
136+
error,
128137
},
129138
parts: [
130139
{
@@ -255,6 +264,49 @@ describe("tool.task", () => {
255264
}),
256265
)
257266

267+
it.instance("execute surfaces child errors with a resumable task_id", () =>
268+
Effect.gen(function* () {
269+
const sessions = yield* Session.Service
270+
const { chat, assistant } = yield* seed()
271+
const tool = yield* TaskTool
272+
const def = yield* tool.init()
273+
274+
const exit = yield* def
275+
.execute(
276+
{
277+
description: "inspect bug",
278+
prompt: "look into the cache key path",
279+
subagent_type: "general",
280+
},
281+
{
282+
sessionID: chat.id,
283+
messageID: assistant.id,
284+
agent: "build",
285+
abort: new AbortController().signal,
286+
extra: {
287+
promptOps: stubOps({
288+
text: "",
289+
error: new SessionV1.APIError({ message: "Network connection lost", isRetryable: false }).toObject(),
290+
}),
291+
},
292+
messages: [],
293+
metadata: () => Effect.void,
294+
ask: () => Effect.void,
295+
},
296+
)
297+
.pipe(Effect.exit)
298+
299+
expect(Exit.isFailure(exit)).toBe(true)
300+
if (Exit.isSuccess(exit)) throw new Error("expected task failure")
301+
const child = (yield* sessions.children(chat.id))[0]
302+
expect(child).toBeDefined()
303+
const failure = Cause.squash(exit.cause)
304+
expect(failure).toBeInstanceOf(Error)
305+
if (!(failure instanceof Error)) throw new Error("expected Error defect")
306+
expect(failure.message).toBe(`Subagent failed (task_id: ${child?.id}): Network connection lost`)
307+
}),
308+
)
309+
258310
it.instance("execute asks by default and skips checks when bypassed", () =>
259311
Effect.gen(function* () {
260312
const { chat, assistant } = yield* seed()

0 commit comments

Comments
 (0)