@devries-cameron20 / t3code-1 / commits / 0992f51696
Fork 0

The best way to control Claude Code, Codex and more from any computer or phone

fix(opencode): a command that exits non-zero is stored as failed, not completed (#29824)

Co-authored-by: Claude Opus 5.5 <noreply@anthropic.com>

Alex Southwell committed Nov 3, 2026 at 23:25 UTC 0992f51696834fda5fc1e880fde1f2ba2000f5ef
5 files changed +104 -7
apps/server/src/orchestration-v2/Adapters/OpenCode2AdapterV2.test.ts
+50
@@ -2759,6 +2759,56 @@ it.layer(McpProviderSessions.layer)("OpenCode2 adapter", (it) => {
2759 }).pipe(Effect.scoped),
2760 );
2761
2762 + it.effect("stores a shell command that exits non-zero as failed", () =>
2763 + Effect.gen(function* () {
2764 + const tool = { sessionID: SESSION, assistantMessageID: "msg_assistant", id: "call-exit" };
2765 + const { runtime, thread } = yield* resumed([
2766 + out("session.prompt", { sessionID: SESSION, text: "<any>" }),
2767 + promptAccepted,
2768 + event("session.tool.input.started", { ...tool, name: "shell" }),
2769 + event("session.tool.called", { ...tool, input: { command: "vp test" }, executed: false }),
2770 + {
2771 + type: "emit_inbound",
2772 + frame: {
2773 + type: "sdk.event",
2774 + event: {
2775 + id: "evt_toolsuccess_call-exit",
2776 + created: 1,
2777 + type: "session.tool.success",
2778 + data: {
2779 + ...tool,
2780 + content: [{ type: "text", text: "1 failed\n" }],
2781 + metadata: { status: "completed", truncated: false, exit: 1 },
2782 + executed: false,
2783 + },
2784 + durable: { aggregateID: SESSION, seq: 1, version: 2 },
2785 + },
2786 + },
2787 + },
2788 + event("session.execution.succeeded", { sessionID: SESSION }),
2789 + ]);
2790 + const events = yield* runtime.events.pipe(
2791 + Stream.takeUntil((event) => event.type === "turn.terminal"),
2792 + Stream.runCollect,
2793 + Effect.forkScoped,
2794 + );
2795 + yield* runtime.startTurn(turnInput(thread));
2796 + const collected = yield* Fiber.join(events);
2797 + const item = collected
2798 + .flatMap((event) => (event.type === "turn_item.updated" ? [event.turnItem] : []))
2799 + .findLast((item) => item.nativeItemRef?.nativeId?.endsWith(tool.id));
2800 + assert.equal(item?.type, "command_execution");
2801 + assert.equal(item?.status, "failed");
2802 + assert.equal(item?.type === "command_execution" ? item.exitCode : null, 1);
2803 + assert.equal(
2804 + collected
2805 + .flatMap((event) => (event.type === "node.updated" ? [event.node] : []))
2806 + .findLast((node) => node.id === item?.nodeId)?.status,
2807 + "failed",
2808 + );
2809 + }).pipe(Effect.scoped),
2810 + );
2811 +
2812 it.effect("re-registers T3 MCP each turn and drops its instructions when adding fails", () =>
2813 Effect.gen(function* () {
2814 const mcpSessions = yield* McpProviderSessions.McpProviderSessions;
packages/provider-opencode/src/server/adapter.test.ts
+33 -2
@@ -803,6 +803,30 @@ describe("OpenCodeAdapterV2", () => {
803 }),
804 );
805 }
806 + yield* Effect.promise(() =>
807 + nativeEvents.push({
808 + type: "message.part.updated",
809 + properties: {
810 + sessionID: nativeSessionId,
811 + part: {
812 + id: "part-exit",
813 + sessionID: nativeSessionId,
814 + messageID: "late-assistant",
815 + type: "tool",
816 + callID: "call-exit",
817 + tool: "bash",
818 + state: {
819 + status: "completed",
820 + input: { command: "vp test" },
821 + output: "1 failed\n",
822 + title: "Run tests",
823 + metadata: { output: "1 failed\n", exit: 1, truncated: false },
824 + time: { start: 4, end: 5 },
825 + },
826 + },
827 + },
828 + }),
829 + );
830 yield* Effect.promise(() =>
831 nativeEvents.push({
832 type: "message.part.updated",
@@ -877,14 +901,21 @@ describe("OpenCodeAdapterV2", () => {
901 const commands = items.filter((item) => item.type === "command_execution");
902 assert.deepEqual(
903 commands.map((item) => item.status),
880 - ["pending", "running", "completed", "failed"],
904 + ["pending", "running", "completed", "failed", "failed"],
905 );
906 assert.deepEqual(
907 commands.map((item) => item.input),
884 - ["pwd", "pwd", "pwd", "pwd"],
908 + ["pwd", "pwd", "pwd", "pwd", "vp test"],
909 );
910 assert.equal(commands[2]?.output, "/repo\n");
911 assert.equal(commands[3]?.output, "Command failed");
912 + assert.equal(commands[4]?.exitCode, 1);
913 + assert.equal(
914 + events
915 + .flatMap((event) => (event.type === "node.updated" ? [event.node] : []))
916 + .findLast((node) => node.id === commands[4]?.nodeId)?.status,
917 + "failed",
918 + );
919 assert.isTrue(
920 events.some(
921 (event) =>
packages/provider-opencode/src/server/adapter.ts
+4 -2
@@ -80,7 +80,7 @@ import {
80 makeSubagentChildThread,
81 subagentThreadTitle,
82 } from "@t3tools/provider-core/server/subagentProjection";
83 -import { openCodeToolTurnItem } from "./toolItems.ts";
83 +import { openCodeCommandFailed, openCodeToolTurnItem } from "./toolItems.ts";
84
85 export const OPENCODE_PROVIDER = ProviderDriverKind.make("opencode");
86 export const OPENCODE_DEFAULT_INSTANCE_ID = defaultInstanceIdForDriver(OPENCODE_PROVIDER);
@@ -742,7 +742,9 @@ function toolStatus(part: ToolPart): {
742 case "running":
743 return { node: "running", item: "running" };
744 case "completed":
745 - return { node: "completed", item: "completed" };
745 + return openCodeCommandFailed(part.tool, part.state.metadata)
746 + ? { node: "failed", item: "failed" }
747 + : { node: "completed", item: "completed" };
748 case "error":
749 return { node: "failed", item: "failed" };
750 }
packages/provider-opencode/src/server/toolItems.ts
+10
@@ -81,6 +81,16 @@ function nativeReadOutput(output: string): string {
81 return envelope === null ? output : envelope[1] + body.slice(envelope[0].length);
82 }
83
84 +/** OpenCode's shell tool succeeds when the command exits non-zero. */
85 +export function openCodeCommandFailed(toolName: string, completedMetadata: unknown): boolean {
86 + const exitCode = recordNumber(completedMetadata, "exit", "exitCode");
87 + return (
88 + openCodeToolProjectionKind(toolName) === "command_execution" &&
89 + exitCode !== undefined &&
90 + exitCode !== 0
91 + );
92 +}
93 +
94 export function openCodeToolTurnItem(
95 base: ToolItemBase,
96 tool: {
packages/provider-opencode/src/server/v2/adapter.ts
+7 -3
@@ -87,7 +87,7 @@ import {
87 import * as ProviderAdapter from "@t3tools/provider-core/server/ProviderAdapter";
88 import { turnScopedSelectionTransition } from "@t3tools/provider-core/server/selectionTransition";
89 import { OPENCODE_PROVIDER, openCodePermissionRequestKind } from "../adapter.ts";
90 -import { openCodeToolTurnItem } from "../toolItems.ts";
90 +import { openCodeCommandFailed, openCodeToolTurnItem } from "../toolItems.ts";
91
92 const OpenCode2ProviderCapabilities = {
93 sessions: {
@@ -1004,12 +1004,16 @@ export const make = Effect.fn("OpenCode2Adapter.make")(function* (instanceId: Pr
1004 const startedAt = turn.startedAt.get(id) ?? updatedAt;
1005 const completedAt = status === "running" ? null : updatedAt;
1006 const nativeId = `${turn.scope}${id}`;
1007 - yield* emitNode(state, turn, nativeId, "tool_call", status, startedAt, completedAt);
1007 + const settled =
1008 + status === "completed" && openCodeCommandFailed(tool.name, result?.metadata)
1009 + ? "failed"
1010 + : status;
1011 + yield* emitNode(state, turn, nativeId, "tool_call", settled, startedAt, completedAt);
1012 yield* emit({
1013 type: "turn_item.updated",
1014 driver,
1015 turnItem: openCodeToolTurnItem(
1012 - itemBase(state, turn, nativeId, status, startedAt, completedAt, updatedAt),
1016 + itemBase(state, turn, nativeId, settled, startedAt, completedAt, updatedAt),
1017 {
1018 name: tool.name,
1019 input: tool.input,