Skip to content

Commit acd8871

Browse files
saphidclaude
andcommitted
fix(opencode): a command that exits non-zero is stored as failed, not completed
Co-Authored-By: Claude Opus 5.5 <[email protected]>
1 parent f6503f0 commit acd8871

5 files changed

Lines changed: 104 additions & 7 deletions

File tree

‎apps/server/src/orchestration-v2/Adapters/OpenCode2AdapterV2.test.ts‎

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2476,6 +2476,56 @@ it.layer(McpProviderSessions.layer)("OpenCode2 adapter", (it) => {
24762476
}).pipe(Effect.scoped),
24772477
);
24782478

2479+
it.effect("stores a shell command that exits non-zero as failed", () =>
2480+
Effect.gen(function* () {
2481+
const tool = { sessionID: SESSION, assistantMessageID: "msg_assistant", id: "call-exit" };
2482+
const { runtime, thread } = yield* resumed([
2483+
out("session.prompt", { sessionID: SESSION, text: "<any>" }),
2484+
promptAccepted,
2485+
event("session.tool.input.started", { ...tool, name: "shell" }),
2486+
event("session.tool.called", { ...tool, input: { command: "vp test" }, executed: false }),
2487+
{
2488+
type: "emit_inbound",
2489+
frame: {
2490+
type: "sdk.event",
2491+
event: {
2492+
id: "evt_toolsuccess_call-exit",
2493+
created: 1,
2494+
type: "session.tool.success",
2495+
data: {
2496+
...tool,
2497+
content: [{ type: "text", text: "1 failed\n" }],
2498+
metadata: { status: "completed", truncated: false, exit: 1 },
2499+
executed: false,
2500+
},
2501+
durable: { aggregateID: SESSION, seq: 1, version: 2 },
2502+
},
2503+
},
2504+
},
2505+
event("session.execution.succeeded", { sessionID: SESSION }),
2506+
]);
2507+
const events = yield* runtime.events.pipe(
2508+
Stream.takeUntil((event) => event.type === "turn.terminal"),
2509+
Stream.runCollect,
2510+
Effect.forkScoped,
2511+
);
2512+
yield* runtime.startTurn(turnInput(thread));
2513+
const collected = yield* Fiber.join(events);
2514+
const item = collected
2515+
.flatMap((event) => (event.type === "turn_item.updated" ? [event.turnItem] : []))
2516+
.findLast((item) => item.nativeItemRef?.nativeId?.endsWith(tool.id));
2517+
assert.equal(item?.type, "command_execution");
2518+
assert.equal(item?.status, "failed");
2519+
assert.equal(item?.type === "command_execution" ? item.exitCode : null, 1);
2520+
assert.equal(
2521+
collected
2522+
.flatMap((event) => (event.type === "node.updated" ? [event.node] : []))
2523+
.findLast((node) => node.id === item?.nodeId)?.status,
2524+
"failed",
2525+
);
2526+
}).pipe(Effect.scoped),
2527+
);
2528+
24792529
it.effect("re-registers T3 MCP each turn and drops its instructions when adding fails", () =>
24802530
Effect.gen(function* () {
24812531
const mcpSessions = yield* McpProviderSessions.McpProviderSessions;

‎packages/provider-opencode/src/server/adapter.test.ts‎

Lines changed: 33 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -803,6 +803,30 @@ describe("OpenCodeAdapterV2", () => {
803803
}),
804804
);
805805
}
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+
);
806830
yield* Effect.promise(() =>
807831
nativeEvents.push({
808832
type: "message.part.updated",
@@ -877,14 +901,21 @@ describe("OpenCodeAdapterV2", () => {
877901
const commands = items.filter((item) => item.type === "command_execution");
878902
assert.deepEqual(
879903
commands.map((item) => item.status),
880-
["pending", "running", "completed", "failed"],
904+
["pending", "running", "completed", "failed", "failed"],
881905
);
882906
assert.deepEqual(
883907
commands.map((item) => item.input),
884-
["pwd", "pwd", "pwd", "pwd"],
908+
["pwd", "pwd", "pwd", "pwd", "vp test"],
885909
);
886910
assert.equal(commands[2]?.output, "/repo\n");
887911
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+
);
888919
assert.isTrue(
889920
events.some(
890921
(event) =>

‎packages/provider-opencode/src/server/adapter.ts‎

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -80,7 +80,7 @@ import {
8080
makeSubagentChildThread,
8181
subagentThreadTitle,
8282
} from "@t3tools/provider-core/server/subagentProjection";
83-
import { openCodeToolTurnItem } from "./toolItems.ts";
83+
import { openCodeCommandFailed, openCodeToolTurnItem } from "./toolItems.ts";
8484

8585
export const OPENCODE_PROVIDER = ProviderDriverKind.make("opencode");
8686
export const OPENCODE_DEFAULT_INSTANCE_ID = defaultInstanceIdForDriver(OPENCODE_PROVIDER);
@@ -741,7 +741,9 @@ function toolStatus(part: ToolPart): {
741741
case "running":
742742
return { node: "running", item: "running" };
743743
case "completed":
744-
return { node: "completed", item: "completed" };
744+
return openCodeCommandFailed(part.tool, part.state.metadata)
745+
? { node: "failed", item: "failed" }
746+
: { node: "completed", item: "completed" };
745747
case "error":
746748
return { node: "failed", item: "failed" };
747749
}

‎packages/provider-opencode/src/server/toolItems.ts‎

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -81,6 +81,16 @@ function nativeReadOutput(output: string): string {
8181
return envelope === null ? output : envelope[1] + body.slice(envelope[0].length);
8282
}
8383

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+
8494
export function openCodeToolTurnItem(
8595
base: ToolItemBase,
8696
tool: {

‎packages/provider-opencode/src/server/v2/adapter.ts‎

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -87,7 +87,7 @@ import {
8787
import * as ProviderAdapter from "@t3tools/provider-core/server/ProviderAdapter";
8888
import { turnScopedSelectionTransition } from "@t3tools/provider-core/server/selectionTransition";
8989
import { OPENCODE_PROVIDER, openCodePermissionRequestKind } from "../adapter.ts";
90-
import { openCodeToolTurnItem } from "../toolItems.ts";
90+
import { openCodeCommandFailed, openCodeToolTurnItem } from "../toolItems.ts";
9191

9292
const OpenCode2ProviderCapabilities = {
9393
sessions: {
@@ -998,12 +998,16 @@ export const make = Effect.fn("OpenCode2Adapter.make")(function* (instanceId: Pr
998998
const startedAt = turn.startedAt.get(id) ?? updatedAt;
999999
const completedAt = status === "running" ? null : updatedAt;
10001000
const nativeId = `${turn.scope}${id}`;
1001-
yield* emitNode(state, turn, nativeId, "tool_call", status, startedAt, completedAt);
1001+
const settled =
1002+
status === "completed" && openCodeCommandFailed(tool.name, result?.metadata)
1003+
? "failed"
1004+
: status;
1005+
yield* emitNode(state, turn, nativeId, "tool_call", settled, startedAt, completedAt);
10021006
yield* emit({
10031007
type: "turn_item.updated",
10041008
driver,
10051009
turnItem: openCodeToolTurnItem(
1006-
itemBase(state, turn, nativeId, status, startedAt, completedAt, updatedAt),
1010+
itemBase(state, turn, nativeId, settled, startedAt, completedAt, updatedAt),
10071011
{
10081012
name: tool.name,
10091013
input: tool.input,

0 commit comments

Comments
 (0)