Skip to content

Commit 2613ba5

Browse files
anandgupta42claude
andcommitted
fix(session): replay only completed tool results of a stalled step
Narrow the replay exception for a stalled step to messages where a tool actually completed, so partial text or tool input that never ran is not fed to the next turn. Also wait for the torn-down requests' close events in the stall tests before asserting on them. Co-Authored-By: Claude Opus 5.5 (1M context) <[email protected]>
1 parent fc5f189 commit 2613ba5

3 files changed

Lines changed: 49 additions & 18 deletions

File tree

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

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -763,15 +763,17 @@ export namespace MessageV2 {
763763
const differentModel = `${model.providerID}/${model.id}` !== `${msg.info.providerID}/${msg.info.modelID}`
764764
const media: Array<{ mime: string; url: string }> = []
765765

766-
// altimate_change start — a stalled step whose retry was refused because a tool had already run keeps its
767-
// tool history in replay (as an aborted step does), so the next turn knows those side effects happened
766+
// altimate_change start — a stalled step whose retry was refused keeps the results of tools that actually
767+
// completed in replay, so the next turn knows those side effects happened. Partial text or tool input that
768+
// never ran is not replayed.
768769
if (
769770
msg.info.error &&
770771
!(
771-
(MessageV2.AbortedError.isInstance(msg.info.error) ||
772-
(MessageV2.APIError.isInstance(msg.info.error) &&
773-
msg.info.error.data.message.startsWith(ProviderError.MODEL_STOPPED_RESPONDING))) &&
774-
msg.parts.some((part) => part.type !== "step-start" && part.type !== "reasoning")
772+
(MessageV2.AbortedError.isInstance(msg.info.error) &&
773+
msg.parts.some((part) => part.type !== "step-start" && part.type !== "reasoning")) ||
774+
(MessageV2.APIError.isInstance(msg.info.error) &&
775+
msg.info.error.data.message.startsWith(ProviderError.MODEL_STOPPED_RESPONDING) &&
776+
msg.parts.some((part) => part.type === "tool" && part.state.status === "completed"))
775777
)
776778
) {
777779
continue

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

Lines changed: 27 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1054,9 +1054,10 @@ describe("session.message-v2.toModelMessage", () => {
10541054
])
10551055
})
10561056

1057-
test("keeps the tool history of a stalled step whose retry was refused, but not a bare stall", async () => {
1057+
test("replays a stalled step only when a tool actually completed in it", async () => {
10581058
const withTool = "m-assistant-stalled"
1059-
const bare = "m-assistant-bare"
1059+
const textOnly = "m-assistant-text"
1060+
const pendingTool = "m-assistant-pending"
10601061
const stalled = new SessionV1.APIError({
10611062
message:
10621063
"The model stopped responding: no response headers within 300s (not retried: the attempt had already acted: echo)",
@@ -1084,13 +1085,34 @@ describe("session.message-v2.toModelMessage", () => {
10841085
] as SessionV1.Part[],
10851086
},
10861087
{
1087-
info: assistantInfo(bare, "m-parent", stalled),
1088-
parts: [{ ...basePart(bare, "b1"), type: "step-start" }] as SessionV1.Part[],
1088+
info: assistantInfo(textOnly, "m-parent", stalled),
1089+
parts: [{ ...basePart(textOnly, "b1"), type: "text", text: "half an answ" }] as SessionV1.Part[],
1090+
},
1091+
{
1092+
info: assistantInfo(pendingTool, "m-parent", stalled),
1093+
parts: [
1094+
{
1095+
...basePart(pendingTool, "c1"),
1096+
type: "tool",
1097+
callID: "call-2",
1098+
tool: "echo",
1099+
state: {
1100+
status: "error",
1101+
input: {},
1102+
error: "Tool execution aborted",
1103+
metadata: { interrupted: true },
1104+
time: { start: 0, end: 1 },
1105+
},
1106+
},
1107+
] as SessionV1.Part[],
10891108
},
10901109
]
10911110

10921111
const out = await MessageV2.toModelMessages(input as unknown as MessageV2.WithParts[], model)
1093-
expect(JSON.stringify(out)).toContain("ran rm")
1112+
const text = JSON.stringify(out)
1113+
expect(text).toContain("ran rm")
1114+
expect(text).not.toContain("half an answ")
1115+
expect(text).not.toContain("Tool execution aborted")
10941116
expect(out.filter((m) => m.role === "assistant")).toHaveLength(1)
10951117
})
10961118

‎packages/opencode/test/session/stall-recovery.test.ts‎

Lines changed: 14 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -321,13 +321,20 @@ function runSession(opts: RunOptions) {
321321
} as LLM.StreamInput),
322322
).pipe(Effect.ensuring(Effect.sync(clearTimers)))
323323
const elapsed = Date.now() - started
324-
// the server sees a client-side close asynchronously: wait (bounded) for it rather than asserting instantly
325-
if (opts.cancelAfterMs !== undefined) {
326-
yield* Effect.promise(async () => {
327-
const deadline = Date.now() + 2000
328-
while (!server.closedRequests.has(0) && Date.now() < deadline) await Bun.sleep(5)
329-
})
330-
}
324+
// The server sees a client-side close asynchronously. Every request but the last was torn down by the
325+
// client (timeout, reset or cancel): wait, bounded, for those close events before anyone asserts on them.
326+
yield* Effect.promise(async () => {
327+
const deadline = Date.now() + 2000
328+
const settled = () =>
329+
Array.from({ length: Math.max(0, server.requests - 1) }, (_, i) => i).every((i) =>
330+
server.closedRequests.has(i),
331+
)
332+
while (
333+
(!settled() || (opts.cancelAfterMs !== undefined && !server.closedRequests.has(0))) &&
334+
Date.now() < deadline
335+
)
336+
await Bun.sleep(5)
337+
})
331338
const parts = MessageV2.parts(msg.id)
332339
return { result, parts, message: handle.message, server, elapsed }
333340
}),

0 commit comments

Comments
 (0)