Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -2476,6 +2476,56 @@ it.layer(McpProviderSessions.layer)("OpenCode2 adapter", (it) => {
}).pipe(Effect.scoped),
);

it.effect("stores a shell command that exits non-zero as failed", () =>
Effect.gen(function* () {
const tool = { sessionID: SESSION, assistantMessageID: "msg_assistant", id: "call-exit" };
const { runtime, thread } = yield* resumed([
out("session.prompt", { sessionID: SESSION, text: "<any>" }),
promptAccepted,
event("session.tool.input.started", { ...tool, name: "shell" }),
event("session.tool.called", { ...tool, input: { command: "vp test" }, executed: false }),
{
type: "emit_inbound",
frame: {
type: "sdk.event",
event: {
id: "evt_toolsuccess_call-exit",
created: 1,
type: "session.tool.success",
data: {
...tool,
content: [{ type: "text", text: "1 failed\n" }],
metadata: { status: "completed", truncated: false, exit: 1 },
executed: false,
},
durable: { aggregateID: SESSION, seq: 1, version: 2 },
},
},
},
event("session.execution.succeeded", { sessionID: SESSION }),
]);
const events = yield* runtime.events.pipe(
Stream.takeUntil((event) => event.type === "turn.terminal"),
Stream.runCollect,
Effect.forkScoped,
);
yield* runtime.startTurn(turnInput(thread));
const collected = yield* Fiber.join(events);
const item = collected
.flatMap((event) => (event.type === "turn_item.updated" ? [event.turnItem] : []))
.findLast((item) => item.nativeItemRef?.nativeId?.endsWith(tool.id));
assert.equal(item?.type, "command_execution");
assert.equal(item?.status, "failed");
assert.equal(item?.type === "command_execution" ? item.exitCode : null, 1);
assert.equal(
collected
.flatMap((event) => (event.type === "node.updated" ? [event.node] : []))
.findLast((node) => node.id === item?.nodeId)?.status,
"failed",
);
}).pipe(Effect.scoped),
);

it.effect("re-registers T3 MCP each turn and drops its instructions when adding fails", () =>
Effect.gen(function* () {
const mcpSessions = yield* McpProviderSessions.McpProviderSessions;
Expand Down
35 changes: 33 additions & 2 deletions packages/provider-opencode/src/server/adapter.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -803,6 +803,30 @@ describe("OpenCodeAdapterV2", () => {
}),
);
}
yield* Effect.promise(() =>
nativeEvents.push({
type: "message.part.updated",
properties: {
sessionID: nativeSessionId,
part: {
id: "part-exit",
sessionID: nativeSessionId,
messageID: "late-assistant",
type: "tool",
callID: "call-exit",
tool: "bash",
state: {
status: "completed",
input: { command: "vp test" },
output: "1 failed\n",
title: "Run tests",
metadata: { output: "1 failed\n", exit: 1, truncated: false },
time: { start: 4, end: 5 },
},
},
},
}),
);
yield* Effect.promise(() =>
nativeEvents.push({
type: "message.part.updated",
Expand Down Expand Up @@ -877,14 +901,21 @@ describe("OpenCodeAdapterV2", () => {
const commands = items.filter((item) => item.type === "command_execution");
assert.deepEqual(
commands.map((item) => item.status),
["pending", "running", "completed", "failed"],
["pending", "running", "completed", "failed", "failed"],
);
assert.deepEqual(
commands.map((item) => item.input),
["pwd", "pwd", "pwd", "pwd"],
["pwd", "pwd", "pwd", "pwd", "vp test"],
);
assert.equal(commands[2]?.output, "/repo\n");
assert.equal(commands[3]?.output, "Command failed");
assert.equal(commands[4]?.exitCode, 1);
assert.equal(
events
.flatMap((event) => (event.type === "node.updated" ? [event.node] : []))
.findLast((node) => node.id === commands[4]?.nodeId)?.status,
"failed",
);
assert.isTrue(
events.some(
(event) =>
Expand Down
6 changes: 4 additions & 2 deletions packages/provider-opencode/src/server/adapter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@ import {
makeSubagentChildThread,
subagentThreadTitle,
} from "@t3tools/provider-core/server/subagentProjection";
import { openCodeToolTurnItem } from "./toolItems.ts";
import { openCodeCommandFailed, openCodeToolTurnItem } from "./toolItems.ts";

export const OPENCODE_PROVIDER = ProviderDriverKind.make("opencode");
export const OPENCODE_DEFAULT_INSTANCE_ID = defaultInstanceIdForDriver(OPENCODE_PROVIDER);
Expand Down Expand Up @@ -741,7 +741,9 @@ function toolStatus(part: ToolPart): {
case "running":
return { node: "running", item: "running" };
case "completed":
return { node: "completed", item: "completed" };
return openCodeCommandFailed(part.tool, part.state.metadata)
? { node: "failed", item: "failed" }
: { node: "completed", item: "completed" };
case "error":
return { node: "failed", item: "failed" };
}
Expand Down
10 changes: 10 additions & 0 deletions packages/provider-opencode/src/server/toolItems.ts
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,16 @@ function nativeReadOutput(output: string): string {
return envelope === null ? output : envelope[1] + body.slice(envelope[0].length);
}

/** OpenCode's shell tool succeeds when the command exits non-zero. */
export function openCodeCommandFailed(toolName: string, completedMetadata: unknown): boolean {
const exitCode = recordNumber(completedMetadata, "exit", "exitCode");
return (
openCodeToolProjectionKind(toolName) === "command_execution" &&
exitCode !== undefined &&
exitCode !== 0
);
}

export function openCodeToolTurnItem(
base: ToolItemBase,
tool: {
Expand Down
10 changes: 7 additions & 3 deletions packages/provider-opencode/src/server/v2/adapter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,7 @@ import {
import * as ProviderAdapter from "@t3tools/provider-core/server/ProviderAdapter";
import { turnScopedSelectionTransition } from "@t3tools/provider-core/server/selectionTransition";
import { OPENCODE_PROVIDER, openCodePermissionRequestKind } from "../adapter.ts";
import { openCodeToolTurnItem } from "../toolItems.ts";
import { openCodeCommandFailed, openCodeToolTurnItem } from "../toolItems.ts";

const OpenCode2ProviderCapabilities = {
sessions: {
Expand Down Expand Up @@ -998,12 +998,16 @@ export const make = Effect.fn("OpenCode2Adapter.make")(function* (instanceId: Pr
const startedAt = turn.startedAt.get(id) ?? updatedAt;
const completedAt = status === "running" ? null : updatedAt;
const nativeId = `${turn.scope}${id}`;
yield* emitNode(state, turn, nativeId, "tool_call", status, startedAt, completedAt);
const settled =
status === "completed" && openCodeCommandFailed(tool.name, result?.metadata)
? "failed"
: status;
yield* emitNode(state, turn, nativeId, "tool_call", settled, startedAt, completedAt);
yield* emit({
type: "turn_item.updated",
driver,
turnItem: openCodeToolTurnItem(
itemBase(state, turn, nativeId, status, startedAt, completedAt, updatedAt),
itemBase(state, turn, nativeId, settled, startedAt, completedAt, updatedAt),
{
name: tool.name,
input: tool.input,
Expand Down
Loading