Skip to content

Commit c4a46bd

Browse files
fix(opencode): a joined call's description matches its run's report
OpenCode labels a run's report with the description of whichever call registered the run's notification observer, which can be a call that joined the running child rather than the one that started it. Match the report against every call in a group, not only the group's root, so the earlier group keeps its result instead of being treated as lost. AI-assisted; verified with the tests and tools listed above. Co-Authored-By: Claude Opus 5.5 <[email protected]>
1 parent 1b462e1 commit c4a46bd

2 files changed

Lines changed: 43 additions & 8 deletions

File tree

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

Lines changed: 37 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1078,8 +1078,11 @@ it.layer(McpProviderSessions.layer)("OpenCode2 adapter", (it) => {
10781078
},
10791079
},
10801080
});
1081-
const callAgain = (input: Record<string, unknown>): ReadonlyArray<ProviderReplayEntry> => {
1082-
const tool = { sessionID: SESSION, assistantMessageID: "msg_assistant", id: "call-again" };
1081+
const callAgain = (
1082+
input: Record<string, unknown>,
1083+
{ id = "call-again", rules = true } = {},
1084+
): ReadonlyArray<ProviderReplayEntry> => {
1085+
const tool = { sessionID: SESSION, assistantMessageID: "msg_assistant", id };
10831086
return [
10841087
event("session.tool.input.started", { ...tool, name: "subagent" }),
10851088
event("session.tool.called", {
@@ -1092,8 +1095,13 @@ it.layer(McpProviderSessions.layer)("OpenCode2 adapter", (it) => {
10921095
...tool,
10931096
metadata: { sessionID: CHILD, status: "running" },
10941097
}),
1095-
out("session.update", { sessionID: CHILD, permissions: "<any>" }),
1096-
reply("session.update", null),
1098+
// The adapter skips the write when the child already has these rules.
1099+
...(rules
1100+
? [
1101+
out("session.update", { sessionID: CHILD, permissions: "<any>" }),
1102+
reply("session.update", null),
1103+
]
1104+
: []),
10971105
returnedRunning(tool.id),
10981106
];
10991107
};
@@ -1336,6 +1344,31 @@ it.layer(McpProviderSessions.layer)("OpenCode2 adapter", (it) => {
13361344
}).pipe(Effect.scoped),
13371345
);
13381346

1347+
it.effect("gives a run's report to its group when a joined call labels it", () =>
1348+
Effect.gen(function* () {
1349+
const { runtime, thread } = yield* resumed([
1350+
...launchedAndRunning,
1351+
// Again joins run 1, and OpenCode labels run 1's report with Again's description.
1352+
...callAgain({ background: true }),
1353+
event("session.execution.succeeded", { sessionID: CHILD }),
1354+
...callAgain({ background: true }, { id: "call-later", rules: false }),
1355+
event("session.execution.started", { sessionID: CHILD }),
1356+
event("session.execution.succeeded", { sessionID: CHILD }),
1357+
childReport("msg_report_1", "FIRST_OK", "Again"),
1358+
childReport("msg_report_2", "SECOND_OK", "Again"),
1359+
event("session.execution.succeeded", { sessionID: SESSION }),
1360+
]);
1361+
const watch = yield* watchCalls(runtime, thread);
1362+
yield* runtime.startTurn(withLineage(thread));
1363+
yield* watch.until(() => watch.state.terminal !== undefined);
1364+
assert.deepEqual(watch.state.roster, []);
1365+
assert.deepEqual(Object.fromEntries(watch.calls), {
1366+
Sleep: { status: "completed", result: "FIRST_OK" },
1367+
Again: { status: "completed", result: "SECOND_OK" },
1368+
});
1369+
}).pipe(Effect.scoped),
1370+
);
1371+
13391372
const MIDDLE = "ses_middle0000000000000000000";
13401373
const DEEP = "ses_grandchild00000000000000";
13411374
const toolOf = (session: string, id: string) => ({

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

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2553,14 +2553,16 @@ export const make = Effect.fn("OpenCode2Adapter.make")(function* (instanceId: Pr
25532553
const runs = [...state.calls.values()].filter(
25542554
(candidate) => candidate.child?.sessionId === childId && candidate.joined === undefined,
25552555
);
2556-
// OpenCode labels each run's report with the description of the call that started the run.
2557-
const call = runs.find((candidate) => candidate.title === payload.description) ?? runs[0];
2558-
// Runs are serial, so open calls before the reported one ran earlier and lost their reports.
2559-
const lost = runs.slice(0, call === undefined ? 0 : runs.indexOf(call));
25602556
const groupOf = (root: SubagentCall) =>
25612557
[...state.calls.values()].filter(
25622558
(candidate) => candidate === root || candidate.joined === root,
25632559
);
2560+
// OpenCode labels a run's report with the description of one of the calls in that run.
2561+
const call =
2562+
runs.find((root) => groupOf(root).some((each) => each.title === payload.description)) ??
2563+
runs[0];
2564+
// Runs are serial, so open calls before the reported one ran earlier and lost their reports.
2565+
const lost = runs.slice(0, call === undefined ? 0 : runs.indexOf(call));
25642566
const outcome = reportOutcome(stringField(payload.metadata, "state"));
25652567
state.reports.set(inboxId, {
25662568
inboxId,

0 commit comments

Comments
 (0)