Skip to content

Commit 3450410

Browse files
committed
fix: preserve context after interrupts and propagate subagent interruption state
This commit addresses two critical interruption handling issues: 1. Context preservation after interruption (fixes anomalyco#2554): - Messages with AbortedError are preserved in context (already in prompt.ts) - filterSummarized() ignores errored summaries (message-v2.ts) - prune() ignores errored summaries when checking compaction (index.ts) 2. Subagent interruption propagation: - Task tool now checks result.info.error after subagent execution - Throws error when subagent was interrupted or errored - Includes partial output from interrupted subagents in error message - Parent agents correctly detect when delegated tasks are interrupted This ensures the model remembers work done before interruption and parent agents properly handle interrupted subagent tasks.
1 parent d947df3 commit 3450410

3 files changed

Lines changed: 16 additions & 3 deletions

File tree

‎packages/opencode/src/session/index.ts‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -301,7 +301,7 @@ export namespace Session {
301301
let sum = 0
302302
for (let msgIndex = msgs.length - 2; msgIndex >= 0; msgIndex--) {
303303
const msg = msgs[msgIndex]
304-
if (msg.info.role === "assistant" && msg.info.summary) return
304+
if (msg.info.role === "assistant" && msg.info.summary && !msg.info.error) return
305305
for (let partIndex = msg.parts.length - 1; partIndex >= 0; partIndex--) {
306306
const part = msg.parts[partIndex]
307307
if (part.type === "tool")

‎packages/opencode/src/session/message-v2.ts‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -569,7 +569,7 @@ export namespace MessageV2 {
569569
}
570570

571571
export function filterSummarized(msgs: { info: MessageV2.Info; parts: MessageV2.Part[] }[]) {
572-
const i = msgs.findLastIndex((m) => m.info.role === "assistant" && !!m.info.summary)
572+
const i = msgs.findLastIndex((m) => m.info.role === "assistant" && !!m.info.summary && !m.info.error)
573573
if (i === -1) return msgs.slice()
574574
return msgs.slice(i)
575575
}

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

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -75,12 +75,25 @@ export const TaskTool = Tool.define("task", async () => {
7575
],
7676
})
7777
unsub()
78+
const outputText = (result.parts.findLast((x: any) => x.type === "text") as any)?.text ?? ""
79+
const err = (result as any).info?.error
80+
if (err) {
81+
const isAborted = MessageV2.AbortedError.isInstance(err)
82+
const name = typeof err === "object" && err && "name" in err ? (err as any).name : "Error"
83+
const msg =
84+
typeof err === "object" && err && "data" in err && (err as any).data && "message" in (err as any).data
85+
? ((err as any).data as any).message
86+
: name
87+
const partial = outputText ? `\nPartial output from subagent before interruption:\n${outputText}` : ""
88+
const text = isAborted ? `Subagent task was interrupted by user.` : `Subagent encountered an error: ${msg}`
89+
throw new Error(`${text}${partial}`)
90+
}
7891
return {
7992
title: params.description,
8093
metadata: {
8194
summary: result.parts.filter((x: any) => x.type === "tool"),
8295
},
83-
output: (result.parts.findLast((x: any) => x.type === "text") as any)?.text ?? "",
96+
output: outputText,
8497
}
8598
},
8699
}

0 commit comments

Comments
 (0)