The best way to control Claude Code, Codex and more from any computer or phone
Mirrored from https://github.com/Camo67/t3code.git
Synced Nov 21, 16:00 UTC
fix(opencode): a lost subagent report no longer leaves a resumed call running (#36386)
Julius Marminge committed
Nov 13, 2026 at 13:25 UTC
b35819caca83199aad1769f3f93f1c51888aaae4
2 files changed
+174
-21
apps/server/src/orchestration-v2/Adapters/OpenCode2AdapterV2.test.ts
+136
-8
@@ -1561,8 +1561,11 @@ it.layer(McpProviderSessions.layer)("OpenCode2 adapter", (it) => {
1561
},
1562
},
1563
});
1564
- const callAgain = (input: Record<string, unknown>): ReadonlyArray<ProviderReplayEntry> => {
1565
- const tool = { sessionID: SESSION, assistantMessageID: "msg_assistant", id: "call-again" };
1564
+ const callAgain = (
1565
+ input: Record<string, unknown>,
1566
+ { id = "call-again", writesRules = true } = {},
1567
+ ): ReadonlyArray<ProviderReplayEntry> => {
1568
+ const tool = { sessionID: SESSION, assistantMessageID: "msg_assistant", id };
1569
return [
1570
event("session.tool.input.started", { ...tool, name: "subagent" }),
1571
event("session.tool.called", {
@@ -1575,20 +1578,25 @@ it.layer(McpProviderSessions.layer)("OpenCode2 adapter", (it) => {
1578
...tool,
1579
metadata: { sessionID: CHILD, status: "running" },
1580
}),
1578
- out("session.update", { sessionID: CHILD, permissions: "<any>" }),
1579
- reply("session.update", null),
1581
+ // The adapter writes a child's rules only when they changed.
1582
+ ...(writesRules
1583
+ ? [
1584
+ out("session.update", { sessionID: CHILD, permissions: "<any>" }),
1585
+ reply("session.update", null),
1586
+ ]
1587
+ : []),
1588
returnedRunning(tool.id),
1589
];
1590
};
1583
- const childReport = (inboxID: string, text: string) =>
1591
+ const childReport = (inboxID: string, text: string, description = "Sleep") =>
1592
event("session.inbox.enqueued", {
1593
inboxID,
1594
sessionID: SESSION,
1595
item: {
1596
type: "synthetic",
1597
payload: {
1590
- text: `<subagent sessionID="${CHILD}" state="completed" description="Sleep">\n${text}\n</subagent>`,
1591
- description: "Sleep",
1598
+ text: `<subagent sessionID="${CHILD}" state="completed" description="${description}">\n${text}\n</subagent>`,
1599
+ description,
1600
metadata: { source: "subagent", childID: CHILD, agent: "General", state: "completed" },
1601
},
1602
delivery: "steer",
@@ -1600,6 +1608,7 @@ it.layer(McpProviderSessions.layer)("OpenCode2 adapter", (it) => {
1608
) =>
1609
Effect.gen(function* () {
1610
const calls = new Map<string, { status: string; result: string | null }>();
1611
+ const callsById = new Map<string, { status: string; result: string | null }>();
1612
const childTurns = new Map<string, string>();
1613
const callsAtChildTurnStart = new Map<
1614
string,
@@ -1610,6 +1619,12 @@ it.layer(McpProviderSessions.layer)("OpenCode2 adapter", (it) => {
1619
yield* runtime.events.pipe(
1620
Stream.tap((event) =>
1621
Effect.gen(function* () {
1622
+ if (event.type === "subagent.updated") {
1623
+ callsById.set(event.subagent.nativeTaskRef?.nativeId ?? "", {
1624
+ status: event.subagent.status,
1625
+ result: event.subagent.result,
1626
+ });
1627
+ }
1628
if (event.type === "subagent.updated" && event.subagent.title !== null) {
1629
calls.set(event.subagent.title, {
1630
status: event.subagent.status,
@@ -1648,7 +1663,7 @@ it.layer(McpProviderSessions.layer)("OpenCode2 adapter", (it) => {
1663
yield* until(check);
1664
});
1665
});
1651
- return { calls, callsAtChildTurnStart, childTurns, state, until };
1666
+ return { calls, callsById, callsAtChildTurnStart, childTurns, state, until };
1667
});
1668
const launchedAndRunning = [
1669
...backgroundLaunch(CHILD),
@@ -1861,6 +1876,119 @@ it.layer(McpProviderSessions.layer)("OpenCode2 adapter", (it) => {
1876
}),
1877
);
1878
1879
+ const childSaid = (text: string, run = 1) =>
1880
+ event("session.text.ended", {
1881
+ sessionID: CHILD,
1882
+ assistantMessageID: `msg_child_${run}`,
1883
+ ordinal: 0,
1884
+ text,
1885
+ });
1886
+ it.effect.each([
1887
+ {
1888
+ // regression: #26319 the report settled the older run and left the resumed call running
1889
+ name: "an earlier run's report never arrived",
1890
+ replay: [
1891
+ ...launchedAndRunning,
1892
+ ...callAgain({ background: true }),
1893
+ childSaid("FIRST_OK"),
1894
+ event("session.execution.succeeded", { sessionID: CHILD }),
1895
+ ...callAgain({ background: true }, { id: "call-later", writesRules: false }),
1896
+ event("session.execution.started", { sessionID: CHILD }),
1897
+ childSaid("SECOND_OK", 2),
1898
+ event("session.execution.succeeded", { sessionID: CHILD }),
1899
+ event("session.execution.succeeded", { sessionID: SESSION }),
1900
+ childReport("msg_report_2", "SECOND_OK", "Again"),
1901
+ ],
1902
+ expected: {
1903
+ "call-background": { status: "completed", result: "FIRST_OK" },
1904
+ "call-again": { status: "completed", result: "FIRST_OK" },
1905
+ "call-later": { status: "completed", result: "SECOND_OK" },
1906
+ },
1907
+ },
1908
+ {
1909
+ name: "both runs' reports arrive after the runs ended",
1910
+ replay: [
1911
+ ...launchedAndRunning,
1912
+ event("session.execution.succeeded", { sessionID: CHILD }),
1913
+ ...callAgain({ background: true, description: "Sleep" }),
1914
+ event("session.execution.started", { sessionID: CHILD }),
1915
+ event("session.execution.succeeded", { sessionID: CHILD }),
1916
+ childReport("msg_report_1", "FIRST_OK"),
1917
+ childReport("msg_report_2", "SECOND_OK"),
1918
+ event("session.execution.succeeded", { sessionID: SESSION }),
1919
+ ],
1920
+ expected: {
1921
+ "call-background": { status: "completed", result: "FIRST_OK" },
1922
+ "call-again": { status: "completed", result: "SECOND_OK" },
1923
+ },
1924
+ },
1925
+ {
1926
+ name: "two runs with the same answer report late",
1927
+ replay: [
1928
+ ...launchedAndRunning,
1929
+ childSaid("DONE"),
1930
+ event("session.execution.succeeded", { sessionID: CHILD }),
1931
+ ...callAgain({ background: true }),
1932
+ event("session.execution.started", { sessionID: CHILD }),
1933
+ childSaid("DONE", 2),
1934
+ event("session.execution.succeeded", { sessionID: CHILD }),
1935
+ childReport("msg_report_1", "DONE"),
1936
+ ...callAgain({ background: true }, { id: "call-later", writesRules: false }),
1937
+ event("session.execution.started", { sessionID: CHILD }),
1938
+ childReport("msg_report_2", "DONE", "Again"),
1939
+ childSaid("THIRD_OK", 3),
1940
+ event("session.execution.succeeded", { sessionID: CHILD }),
1941
+ event("session.execution.succeeded", { sessionID: SESSION }),
1942
+ childReport("msg_report_3", "THIRD_OK", "Again"),
1943
+ ],
1944
+ expected: {
1945
+ "call-background": { status: "completed", result: "DONE" },
1946
+ "call-again": { status: "completed", result: "DONE" },
1947
+ "call-later": { status: "completed", result: "THIRD_OK" },
1948
+ },
1949
+ },
1950
+ {
1951
+ name: "the call it joined already returned",
1952
+ replay: [
1953
+ ...backgroundLaunch(CHILD),
1954
+ event("session.execution.started", { sessionID: CHILD }),
1955
+ ...callAgain({ background: true }),
1956
+ event("session.execution.succeeded", { sessionID: CHILD }),
1957
+ event(
1958
+ "session.tool.success",
1959
+ {
1960
+ sessionID: SESSION,
1961
+ assistantMessageID: "msg_assistant",
1962
+ id: "call-background",
1963
+ content: [{ type: "text", text: "FIRST_OK" }],
1964
+ metadata: { sessionID: CHILD, status: "completed" },
1965
+ executed: true,
1966
+ },
1967
+ undefined,
1968
+ 2,
1969
+ ),
1970
+ event("session.execution.succeeded", { sessionID: SESSION }),
1971
+ childReport("msg_report", "CHILD_OK", "Again"),
1972
+ ],
1973
+ expected: {
1974
+ "call-background": { status: "completed", result: "FIRST_OK" },
1975
+ "call-again": { status: "completed", result: "CHILD_OK" },
1976
+ },
1977
+ },
1978
+ ])("settles a resumed subagent's calls when $name", ({ replay, expected }) =>
1979
+ Effect.gen(function* () {
1980
+ const { runtime, thread } = yield* resumed(replay);
1981
+ const watch = yield* watchCalls(runtime, thread);
1982
+ yield* runtime.startTurn(withLineage(thread));
1983
+ yield* watch.until(
1984
+ () =>
1985
+ watch.state.roster?.length === 0 &&
1986
+ Object.keys(expected).every((id) => watch.callsById.get(id)?.status === "completed"),
1987
+ );
1988
+ assert.deepEqual(Object.fromEntries(watch.callsById), expected);
1989
+ }),
1990
+ );
1991
+
1992
const MIDDLE = "ses_middle0000000000000000000";
1993
const DEEP = "ses_grandchild00000000000000";
1994
const toolOf = (session: string, id: string) => ({
packages/provider-opencode/src/server/v2/adapter.ts
+38
-13
@@ -228,6 +228,8 @@ interface ActiveTurn {
228
readonly input: TurnOwner;
229
providerTurn: OrchestrationV2ProviderTurn;
230
readonly texts: Map<string, OpenBlock>;
231
+ /** The last assistant text this turn completed. */
232
+ answer: string | undefined;
233
readonly tools: Map<string, { readonly name: string; input: Record<string, unknown> }>;
234
readonly startedAt: Map<string, DateTime.Utc>;
235
readonly ordinals: Map<string, number>;
@@ -280,7 +282,10 @@ interface SubagentCall {
282
agent: string | undefined;
283
model: string | null;
284
background: boolean;
283
- joined: SubagentCall | undefined;
285
+ /** The child's turn this call prompted or steered; unset until that turn starts. */
286
+ run: number | undefined;
287
+ /** That turn's last text once it ended, which OpenCode sends as the run's report. */
288
+ answer: string | undefined;
289
child: ThreadState | undefined;
290
status: OrchestrationV2Subagent["status"];
291
result: string | null;
@@ -1132,6 +1137,7 @@ export const make = Effect.fn("OpenCode2Adapter.make")(function* (instanceId: Pr
1137
if (completed) turn.texts.delete(nativeId);
1138
else turn.texts.set(nativeId, entry);
1139
if (entry.text.length === 0) return;
1140
+ if (completed && kind === "text") turn.answer = entry.text;
1141
const status = completed ? "completed" : "running";
1142
const completedAt = completed ? updatedAt : null;
1143
const nodeKind = kind === "text" ? "assistant_message" : "reasoning";
@@ -1488,6 +1494,7 @@ export const make = Effect.fn("OpenCode2Adapter.make")(function* (instanceId: Pr
1494
input: owner,
1495
providerTurn,
1496
texts: new Map(),
1497
+ answer: undefined,
1498
tools: new Map(),
1499
startedAt: new Map(),
1500
ordinals: new Map(),
@@ -1987,13 +1994,8 @@ export const make = Effect.fn("OpenCode2Adapter.make")(function* (instanceId: Pr
1994
updatedAt: now,
1995
};
1996
const previous = threads.get(childId);
1990
- // Repeated subagent calls steer the same run; OpenCode reports it once for every joined call.
1991
- if (previous?.active !== undefined) {
1992
- call.joined = [...call.state.calls.values()].findLast(
1993
- (candidate) =>
1994
- candidate !== call && candidate.child === previous && candidate.joined === undefined,
1995
- );
1996
- }
1997
+ // A call to a running child steers that run; OpenCode reports it once for all its calls.
1998
+ if (previous?.active !== undefined) call.run = previous.subagent?.turns;
1999
const subagent = {
2000
call,
2001
appThread,
@@ -2059,6 +2061,7 @@ export const make = Effect.fn("OpenCode2Adapter.make")(function* (instanceId: Pr
2061
const subagent = child.subagent;
2062
if (subagent === undefined || child.active !== undefined) return;
2063
subagent.turns += 1;
2064
+ for (const call of callsAttachedTo(child)) call.run ??= subagent.turns;
2065
const startedAt = yield* DateTime.now;
2066
const nativeTurnId = `${child.sessionId}:turn:${subagent.turns}`;
2067
const rootNodeId = idAllocator.derive.nodeFromProviderItem({
@@ -2249,6 +2252,9 @@ export const make = Effect.fn("OpenCode2Adapter.make")(function* (instanceId: Pr
2252
// Subagent turns are runless; their calls own settlement.
2253
if (state.subagent !== undefined) {
2254
const call = state.subagent.call;
2255
+ for (const attached of callsAttachedTo(state)) {
2256
+ if (attached.run === state.subagent.turns) attached.answer = turn.answer;
2257
+ }
2258
if (!call.background && terminal.status !== "completed") {
2259
yield* settleCall(call, terminal.status);
2260
}
@@ -2637,7 +2643,8 @@ export const make = Effect.fn("OpenCode2Adapter.make")(function* (instanceId: Pr
2643
agent: undefined,
2644
model: null,
2645
background: false,
2640
- joined: undefined,
2646
+ run: undefined,
2647
+ answer: undefined,
2648
child: undefined,
2649
status: "running",
2650
result: null,
@@ -3019,11 +3026,28 @@ export const make = Effect.fn("OpenCode2Adapter.make")(function* (instanceId: Pr
3026
const childId = stringField(payload.metadata, "childID");
3027
if (stringField(payload.metadata, "source") !== "subagent" || childId === undefined) return;
3028
strays.delete(childId);
3022
- const call = [...state.calls.values()].find(
3029
+ const open = [...state.calls.values()].filter(
3030
(candidate) => candidate.child?.sessionId === childId,
3031
);
3025
- const calls = [...state.calls.values()].filter(
3026
- (candidate) => candidate === call || (call !== undefined && candidate.joined === call),
3032
+ const output = subagentOutput(payload.text);
3033
+ const started = open
3034
+ .flatMap((candidate) =>
3035
+ candidate.run === undefined ? [] : [{ call: candidate, run: candidate.run }],
3036
+ )
3037
+ .sort((a, b) => a.run - b.run);
3038
+ // The report names only the child, and reports arrive in order. So it belongs to the oldest
3039
+ // open run, unless that run ended with a different text: then its own report is lost.
3040
+ const next = started.find(
3041
+ (candidate) => candidate.call.answer === undefined || candidate.call.answer === output,
3042
+ );
3043
+ const written = next?.call.answer === output ? next.run : undefined;
3044
+ const call = written === undefined ? open[0] : next?.call;
3045
+ const calls =
3046
+ call?.run === undefined
3047
+ ? open.slice(0, 1)
3048
+ : open.filter((candidate) => candidate.run === call.run);
3049
+ const lost = started.flatMap((candidate) =>
3050
+ written !== undefined && candidate.run < written ? [candidate.call] : [],
3051
);
3052
const outcome = reportOutcome(stringField(payload.metadata, "state"));
3053
state.reports.set(inboxId, {
@@ -3044,7 +3068,8 @@ export const make = Effect.fn("OpenCode2Adapter.make")(function* (instanceId: Pr
3068
: outcome === "cancelled"
3069
? "cancelled"
3070
: "completed";
3047
- for (const each of calls) yield* settleCall(each, status, subagentOutput(payload.text));
3071
+ for (const each of lost) yield* settleCall(each, "completed", each.answer);
3072
+ for (const each of calls) yield* settleCall(each, status, output);
3073
});
3074
3075
const holderOf = (sessionId: string) => {