Skip to content

Commit ae84fff

Browse files
author
engineer
committed
fix(session): retry only empty aborts (anomalyco#17717)
1 parent 334b30c commit ae84fff

4 files changed

Lines changed: 76 additions & 5 deletions

File tree

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

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -364,7 +364,10 @@ export namespace SessionProcessor {
364364
error,
365365
})
366366
} else {
367-
const retry = SessionRetry.retryable(error, input.abort)
367+
const retry = SessionRetry.retryable(error, {
368+
abort: input.abort,
369+
empty: (await MessageV2.parts(input.assistantMessage.id)).length === 0,
370+
})
368371
if (retry !== undefined) {
369372
attempt++
370373
const delay = SessionRetry.delay(attempt, error.name === "APIError" ? error : undefined)

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

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -58,11 +58,15 @@ export namespace SessionRetry {
5858
return Math.min(RETRY_INITIAL_DELAY * Math.pow(RETRY_BACKOFF_FACTOR, attempt - 1), RETRY_MAX_DELAY_NO_HEADERS)
5959
}
6060

61-
export function retryable(error: ReturnType<NamedError["toObject"]>, abort?: AbortSignal) {
61+
export function retryable(
62+
error: ReturnType<NamedError["toObject"]>,
63+
opts?: { abort?: AbortSignal; empty?: boolean },
64+
) {
6265
// context overflow errors should not be retried
6366
if (MessageV2.ContextOverflowError.isInstance(error)) return undefined
6467
if (MessageV2.AbortedError.isInstance(error)) {
65-
if (abort?.aborted) return undefined
68+
if (opts?.abort?.aborted) return undefined
69+
if (opts?.empty === false) return undefined
6670
return error.data.message
6771
}
6872
if (MessageV2.APIError.isInstance(error)) {

‎packages/opencode/test/session/processor.test.ts‎

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,15 @@ function output(values: unknown[]) {
7474
} as unknown as Stream
7575
}
7676

77+
function fail(values: unknown[], error: unknown) {
78+
return {
79+
fullStream: (async function* () {
80+
for (const value of values) yield value
81+
throw error
82+
})(),
83+
} as unknown as Stream
84+
}
85+
7786
async function run(
7887
fn: (input: {
7988
agent: Agent.Info
@@ -181,4 +190,52 @@ describe("session.processor", () => {
181190
}
182191
})
183192
})
193+
194+
test("does not retry aborts after assistant output has started", async () => {
195+
await run(async ({ agent, msg, session, usr }) => {
196+
const ctl = new AbortController()
197+
const stream = spyOn(LLMModule.LLM, "stream")
198+
const sleep = spyOn(RetryModule.SessionRetry, "sleep").mockResolvedValue()
199+
200+
try {
201+
stream.mockImplementation(async (): Promise<Stream> =>
202+
fail(
203+
[
204+
{ type: "start" },
205+
{ type: "text-start" },
206+
{ type: "text-delta", text: "partial" },
207+
],
208+
new DOMException("The operation was aborted.", "AbortError"),
209+
),
210+
)
211+
212+
const result = await SessionProcessor.create({
213+
assistantMessage: msg,
214+
sessionID: session.id,
215+
model: model(),
216+
abort: ctl.signal,
217+
}).process({
218+
user: usr,
219+
agent,
220+
abort: ctl.signal,
221+
sessionID: session.id,
222+
system: [],
223+
messages: [],
224+
tools: {},
225+
model: model(),
226+
})
227+
228+
const stored = await MessageV2.get({ sessionID: session.id, messageID: msg.id })
229+
if (stored.info.role !== "assistant") throw new Error("expected assistant")
230+
expect(result).toBe("stop")
231+
expect(stream).toHaveBeenCalledTimes(1)
232+
expect(sleep).not.toHaveBeenCalled()
233+
expect(stored.parts.some((part) => part.type === "text")).toBe(true)
234+
expect(stored.info.error?.name).toBe("MessageAbortedError")
235+
} finally {
236+
stream.mockRestore()
237+
sleep.mockRestore()
238+
}
239+
})
240+
})
184241
})

‎packages/opencode/test/session/retry.test.ts‎

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -130,15 +130,22 @@ describe("session.retry.retryable", () => {
130130
const ctl = new AbortController()
131131
const error = new MessageV2.AbortedError({ message: "The operation was aborted." }).toObject()
132132

133-
expect(SessionRetry.retryable(error, ctl.signal)).toBe("The operation was aborted.")
133+
expect(SessionRetry.retryable(error, { abort: ctl.signal, empty: true })).toBe("The operation was aborted.")
134134
})
135135

136136
test("does not retry aborted errors after session cancellation", () => {
137137
const ctl = new AbortController()
138138
ctl.abort()
139139
const error = new MessageV2.AbortedError({ message: "The operation was aborted." }).toObject()
140140

141-
expect(SessionRetry.retryable(error, ctl.signal)).toBeUndefined()
141+
expect(SessionRetry.retryable(error, { abort: ctl.signal, empty: true })).toBeUndefined()
142+
})
143+
144+
test("does not retry aborted errors after assistant output has started", () => {
145+
const ctl = new AbortController()
146+
const error = new MessageV2.AbortedError({ message: "The operation was aborted." }).toObject()
147+
148+
expect(SessionRetry.retryable(error, { abort: ctl.signal, empty: false })).toBeUndefined()
142149
})
143150
})
144151

0 commit comments

Comments
 (0)