Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 26 additions & 2 deletions packages/opencode/src/session/prompt.ts
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,15 @@ export namespace SessionPrompt {
},
async (current) => {
for (const item of Object.values(current)) {
// Reject all pending callbacks to prevent callers from hanging
const cancelError = new DOMException("Session cancelled", "AbortError")
for (const callback of item.callbacks) {
try {
callback.reject(cancelError)
} catch (e) {
log.error("failed to reject callback during dispose", { error: e })
}
}
item.abort.abort()
}
},
Expand Down Expand Up @@ -265,6 +274,15 @@ export namespace SessionPrompt {
SessionStatus.set(sessionID, { type: "idle" })
return
}
// Reject all pending callbacks to prevent callers from hanging
const cancelError = new DOMException("Session cancelled", "AbortError")
for (const callback of match.callbacks) {
try {
callback.reject(cancelError)
} catch (e) {
log.error("failed to reject callback during cancel", { error: e, sessionID })
}
}
match.abort.abort()
delete s[sessionID]
SessionStatus.set(sessionID, { type: "idle" })
Expand All @@ -281,8 +299,14 @@ export namespace SessionPrompt {
const abort = resume_existing ? resume(sessionID) : start(sessionID)
if (!abort) {
return new Promise<MessageV2.WithParts>((resolve, reject) => {
const callbacks = state()[sessionID].callbacks
callbacks.push({ resolve, reject })
const current = state()
const sessionState = current[sessionID]
// Check if session state exists to prevent hanging on race conditions
if (!sessionState) {
reject(new DOMException("Session state not found", "AbortError"))
return
}
sessionState.callbacks.push({ resolve, reject })
})
}

Expand Down
38 changes: 22 additions & 16 deletions packages/opencode/src/tool/task.ts
Original file line number Diff line number Diff line change
Expand Up @@ -127,22 +127,28 @@ export const TaskTool = Tool.define("task", async (ctx) => {
using _ = defer(() => ctx.abort.removeEventListener("abort", cancel))
const promptParts = await SessionPrompt.resolvePromptParts(params.prompt)

const result = await SessionPrompt.prompt({
messageID,
sessionID: session.id,
model: {
modelID: model.modelID,
providerID: model.providerID,
},
agent: agent.name,
tools: {
todowrite: false,
todoread: false,
...(hasTaskPermission ? {} : { task: false }),
...Object.fromEntries((config.experimental?.primary_tools ?? []).map((t) => [t, false])),
},
parts: promptParts,
})
let result: MessageV2.WithParts
try {
result = await SessionPrompt.prompt({
messageID,
sessionID: session.id,
model: {
modelID: model.modelID,
providerID: model.providerID,
},
agent: agent.name,
tools: {
todowrite: false,
todoread: false,
...(hasTaskPermission ? {} : { task: false }),
...Object.fromEntries((config.experimental?.primary_tools ?? []).map((t) => [t, false])),
},
parts: promptParts,
})
} catch (error) {
// Ensure proper error propagation to parent agent
throw new Error(`Subagent execution failed: ${error instanceof Error ? error.message : String(error)}`)
}

const text = result.parts.findLast((x) => x.type === "text")?.text ?? ""

Expand Down
Loading