GeezerVison is showing github.com — Apply PR #21598: fix: preserve interrupted bash output in tool results · anomalyco/opencode@5a4d663
File treeExpand file treeCollapse file tree Open diff view settings Expand file treeCollapse file tree Open diff view settings Collapse file Copy file name to clipboardExpand all lines: packages/opencode/src/session/message-v2.ts +26-10Lines changed: 26 additions & 10 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -751,16 +751,32 @@ export namespace MessageV2 { | 751 | 751 | ...(differentModel ? {} : { callProviderMetadata: providerMeta(part.metadata) }), | 752 | 752 | }) | 753 | 753 | } | 754 | | - if (part.state.status === "error") | 755 | | - assistantMessage.parts.push({ | 756 | | - type: ("tool-" + part.tool) as `tool-${string}`, | 757 | | - state: "output-error", | 758 | | - toolCallId: part.callID, | 759 | | - input: part.state.input, | 760 | | - errorText: part.state.error, | 761 | | - ...(part.metadata?.providerExecuted ? { providerExecuted: true } : {}), | 762 | | - ...(differentModel ? {} : { callProviderMetadata: providerMeta(part.metadata) }), | 763 | | - }) | | 754 | + if (part.state.status === "error") { | | 755 | + const output = part.state.metadata?.interrupted === true ? part.state.metadata.output : undefined | | 756 | + if (typeof output === "string") { | | 757 | + assistantMessage.parts.push({ | | 758 | + type: ("tool-" + part.tool) as `tool-${string}`, | | 759 | + state: "output-available", | | 760 | + toolCallId: part.callID, | | 761 | + input: part.state.input, | | 762 | + output, | | 763 | + ...(part.metadata?.providerExecuted ? { providerExecuted: true } : {}), | | 764 | + ...(differentModel ? {} : { callProviderMetadata: providerMeta(part.metadata) }), | | 765 | + }) | | 766 | + } else { | | 767 | + assistantMessage.parts.push({ | | 768 | + type: ("tool-" + part.tool) as `tool-${string}`, | | 769 | + state: "output-error", | | 770 | + toolCallId: part.callID, | | 771 | + input: part.state.input, | | 772 | + errorText: part.state.error, | | 773 | + ...(part.metadata?.providerExecuted ? { providerExecuted: true } : {}), | | 774 | + ...(differentModel ? {} : { callProviderMetadata: providerMeta(part.metadata) }), | | 775 | + }) | | 776 | + } | | 777 | + } | | 778 | + // Handle pending/running tool calls to prevent dangling tool_use blocks | | 779 | + // Anthropic/Claude APIs require every tool_use to have a corresponding tool_result | 764 | 780 | if (part.state.status === "pending" || part.state.status === "running") | 765 | 781 | assistantMessage.parts.push({ | 766 | 782 | type: ("tool-" + part.tool) as `tool-${string}`, | |
Collapse file Copy file name to clipboardExpand all lines: packages/opencode/src/session/processor.ts +7-4Lines changed: 7 additions & 4 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -18,6 +18,7 @@ import { SessionStatus } from "./status" | 18 | 18 | import { SessionSummary } from "./summary" | 19 | 19 | import type { Provider } from "@/provider/provider" | 20 | 20 | import { Question } from "@/question" | | 21 | +import { isRecord } from "@/util/record" | 21 | 22 |
| 22 | 23 | export namespace SessionProcessor { | 23 | 24 | const DOOM_LOOP_THRESHOLD = 3 | @@ -398,19 +399,21 @@ export namespace SessionProcessor { | 398 | 399 | } | 399 | 400 | ctx.reasoningMap = {} | 400 | 401 |
| 401 | | - const parts = MessageV2.parts(ctx.assistantMessage.id) | 402 | | - for (const part of parts) { | 403 | | - if (part.type !== "tool" || part.state.status === "completed" || part.state.status === "error") continue | | 402 | + for (const part of Object.values(ctx.toolcalls)) { | | 403 | + const end = Date.now() | | 404 | + const metadata = "metadata" in part.state && isRecord(part.state.metadata) ? part.state.metadata : {} | 404 | 405 | yield* session.updatePart({ | 405 | 406 | ...part, | 406 | 407 | state: { | 407 | 408 | ...part.state, | 408 | 409 | status: "error", | 409 | 410 | error: "Tool execution aborted", | 410 | | - time: { start: Date.now(), end: Date.now() }, | | 411 | + metadata: { ...metadata, interrupted: true }, | | 412 | + time: { start: "time" in part.state ? part.state.time.start : end, end }, | 411 | 413 | }, | 412 | 414 | }) | 413 | 415 | } | | 416 | + ctx.toolcalls = {} | 414 | 417 | ctx.assistantMessage.time.completed = Date.now() | 415 | 418 | yield* session.updateMessage(ctx.assistantMessage) | 416 | 419 | }) | |
Collapse file Copy file name to clipboardExpand all lines: packages/opencode/src/session/prompt.ts +1-1Lines changed: 1 addition & 1 deletion | Original file line number | Diff line number | Diff line change |
|---|
@@ -1505,7 +1505,7 @@ NOTE: At any point in time through this workflow you should feel free to ask the | 1505 | 1505 | Effect.promise(() => SystemPrompt.skills(agent)), | 1506 | 1506 | Effect.promise(() => SystemPrompt.environment(model)), | 1507 | 1507 | instruction.system().pipe(Effect.orDie), | 1508 | | - Effect.promise(() => MessageV2.toModelMessages(msgs, model)), | | 1508 | + MessageV2.toModelMessagesEffect(msgs, model), | 1509 | 1509 | ]) | 1510 | 1510 | const system = [...env, ...(skills ? [skills] : []), ...instructions] | 1511 | 1511 | const format = lastUser.format ?? { type: "text" as const } | |
Collapse file Copy file name to clipboardExpand all lines: packages/opencode/test/session/message-v2.test.ts +75Lines changed: 75 additions & 0 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -570,6 +570,81 @@ describe("session.message-v2.toModelMessage", () => { | 570 | 570 | ]) | 571 | 571 | }) | 572 | 572 |
| | 573 | + test("forwards partial bash output for aborted tool calls", async () => { | | 574 | + const userID = "m-user" | | 575 | + const assistantID = "m-assistant" | | 576 | + const output = [ | | 577 | + "31403", | | 578 | + "12179", | | 579 | + "4575", | | 580 | + "", | | 581 | + "<bash_metadata>", | | 582 | + "User aborted the command", | | 583 | + "</bash_metadata>", | | 584 | + ].join("\n") | | 585 | + | | 586 | + const input: MessageV2.WithParts[] = [ | | 587 | + { | | 588 | + info: userInfo(userID), | | 589 | + parts: [ | | 590 | + { | | 591 | + ...basePart(userID, "u1"), | | 592 | + type: "text", | | 593 | + text: "run tool", | | 594 | + }, | | 595 | + ] as MessageV2.Part[], | | 596 | + }, | | 597 | + { | | 598 | + info: assistantInfo(assistantID, userID), | | 599 | + parts: [ | | 600 | + { | | 601 | + ...basePart(assistantID, "a1"), | | 602 | + type: "tool", | | 603 | + callID: "call-1", | | 604 | + tool: "bash", | | 605 | + state: { | | 606 | + status: "error", | | 607 | + input: { command: "for i in {1..20}; do print -- $RANDOM; sleep 1; done" }, | | 608 | + error: "Tool execution aborted", | | 609 | + metadata: { interrupted: true, output }, | | 610 | + time: { start: 0, end: 1 }, | | 611 | + }, | | 612 | + }, | | 613 | + ] as MessageV2.Part[], | | 614 | + }, | | 615 | + ] | | 616 | + | | 617 | + expect(await MessageV2.toModelMessages(input, model)).toStrictEqual([ | | 618 | + { | | 619 | + role: "user", | | 620 | + content: [{ type: "text", text: "run tool" }], | | 621 | + }, | | 622 | + { | | 623 | + role: "assistant", | | 624 | + content: [ | | 625 | + { | | 626 | + type: "tool-call", | | 627 | + toolCallId: "call-1", | | 628 | + toolName: "bash", | | 629 | + input: { command: "for i in {1..20}; do print -- $RANDOM; sleep 1; done" }, | | 630 | + providerExecuted: undefined, | | 631 | + }, | | 632 | + ], | | 633 | + }, | | 634 | + { | | 635 | + role: "tool", | | 636 | + content: [ | | 637 | + { | | 638 | + type: "tool-result", | | 639 | + toolCallId: "call-1", | | 640 | + toolName: "bash", | | 641 | + output: { type: "text", value: output }, | | 642 | + }, | | 643 | + ], | | 644 | + }, | | 645 | + ]) | | 646 | + }) | | 647 | + | 573 | 648 | test("filters assistant messages with non-abort errors", async () => { | 574 | 649 | const assistantID = "m-assistant" | 575 | 650 |
| |
Collapse file Copy file name to clipboardExpand all lines: packages/opencode/test/session/processor-effect.test.ts +1Lines changed: 1 addition & 0 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -604,6 +604,7 @@ it.live("session.processor effect tests mark pending tools as aborted on cleanup | 604 | 604 | expect(call?.state.status).toBe("error") | 605 | 605 | if (call?.state.status === "error") { | 606 | 606 | expect(call.state.error).toBe("Tool execution aborted") | | 607 | + expect(call.state.metadata?.interrupted).toBe(true) | 607 | 608 | expect(call.state.time.end).toBeDefined() | 608 | 609 | } | 609 | 610 | }), | |
|