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): OpenCode 1.x background subagents no longer show as done at launch (#36523)
Co-authored-by: Julius Marminge <julius0216@outlook.com>
Maciej Pruchnik committed
Nov 14, 2026 at 02:30 UTC
83d238218d86a035ab3674efb09e51fd68437712
2 files changed
+154
-12
packages/provider-opencode/src/server/adapter.test.ts
+60
-10
@@ -1387,7 +1387,13 @@ describe("OpenCodeAdapterV2", () => {
1387
);
1388
1389
// OpenCode 1.18.32 background tasks complete at launch; children stay busy after root settlement.
1390
- it.effect("reports a background task child as pending work after the root turn settles", () =>
1390
+ // Event order from a live 1.19.5 run: the child reports busy only after the launch completes.
1391
+ it.effect.each([
1392
+ { case: "finishes", ending: "idle", outcome: "completed" },
1393
+ { case: "is aborted", ending: "abort", outcome: "interrupted" },
1394
+ { case: "fails before its first turn", ending: "error", outcome: "failed" },
1395
+ { case: "never starts and the thread is stopped", ending: "stop", outcome: "interrupted" },
1396
+ ])("keeps a background task's subagent running until its child $case", ({ ending, outcome }) =>
1397
Effect.gen(function* () {
1398
const nativeEvents = asyncEventStream();
1399
const push = (event: unknown) => Effect.promise(() => nativeEvents.push(event));
@@ -1415,9 +1421,14 @@ describe("OpenCodeAdapterV2", () => {
1421
return yield* Effect.die("OpenCode runtime must expose hasPendingBackgroundWork.");
1422
}
1423
yield* harness.startTurn();
1418
- const terminal = yield* harness.runtime.events.pipe(
1419
- Stream.filter((event) => event.type === "turn.terminal"),
1420
- Stream.runHead,
1424
+ const received = yield* harness.runtime.events.pipe(
1425
+ Stream.takeUntil(
1426
+ (event) =>
1427
+ event.type === "turn_item.updated" &&
1428
+ event.turnItem.type === "subagent" &&
1429
+ event.turnItem.status !== "running",
1430
+ ),
1431
+ Stream.runCollect,
1432
Effect.forkScoped,
1433
);
1434
const taskPart = (status: "running" | "completed") => ({
@@ -1459,7 +1470,6 @@ describe("OpenCodeAdapterV2", () => {
1470
},
1471
});
1472
yield* push(status(root, "busy"));
1462
- yield* push(taskPart("running"));
1473
yield* push({
1474
type: "session.created",
1475
properties: {
@@ -1467,15 +1477,55 @@ describe("OpenCodeAdapterV2", () => {
1477
info: { id: child, parentID: root, time: { created: 2, updated: 2 } },
1478
},
1479
});
1470
- yield* push(status(child, "busy"));
1480
+ yield* push(taskPart("running"));
1481
+ const starts = ending !== "stop" && ending !== "error";
1482
+ if (starts) {
1483
+ yield* push({
1484
+ type: "message.updated",
1485
+ properties: {
1486
+ sessionID: child,
1487
+ info: { id: "msg_child_prompt", sessionID: child, role: "user", time: { created: 2 } },
1488
+ },
1489
+ });
1490
+ }
1491
yield* push(taskPart("completed"));
1492
+ if (starts) yield* push(status(child, "busy"));
1493
yield* push(status(root, "idle"));
1473
- assert.equal(Option.getOrUndefined(yield* Fiber.join(terminal))?.status, "completed");
1474
- assert.isTrue(yield* hasPendingBackgroundWork, "the running child must pin idle release");
1494
+ assert.equal(yield* hasPendingBackgroundWork, starts, "a running child pins idle release");
1495
1476
- yield* push(status(child, "idle"));
1477
- yield* push({ type: "session.idle", properties: { sessionID: child } });
1496
+ if (ending === "stop") {
1497
+ yield* harness.runtime.interruptTurn({
1498
+ providerThread: harness.providerThread,
1499
+ providerTurnId: ProviderTurnId.make("provider-turn:settled"),
1500
+ requestRuntimeRestart: true,
1501
+ });
1502
+ } else {
1503
+ if (ending !== "idle") {
1504
+ yield* push({
1505
+ type: "session.error",
1506
+ properties: {
1507
+ sessionID: child,
1508
+ error:
1509
+ ending === "abort"
1510
+ ? { name: "MessageAbortedError", data: {} }
1511
+ : { name: "UnknownError", data: { message: "model unavailable" } },
1512
+ },
1513
+ });
1514
+ }
1515
+ yield* push(status(child, "idle"));
1516
+ yield* push({ type: "session.idle", properties: { sessionID: child } });
1517
+ }
1518
assert.isFalse(yield* hasPendingBackgroundWork, "an idle child must not pin idle release");
1519
+ assert.deepEqual(
1520
+ (yield* Fiber.join(received)).flatMap((event) =>
1521
+ event.type === "subagent.updated"
1522
+ ? [`subagent ${event.subagent.status}`]
1523
+ : event.type === "turn.terminal"
1524
+ ? [`root turn ${event.status}`]
1525
+ : [],
1526
+ ),
1527
+ ["subagent running", "subagent running", "root turn completed", `subagent ${outcome}`],
1528
+ );
1529
}).pipe(Effect.provide(Layer.merge(IdAllocator.layer, McpProviderSessions.layer))),
1530
);
1531
packages/provider-opencode/src/server/adapter.ts
+94
-2
@@ -396,6 +396,8 @@ interface OpenCodeSubagentContext {
396
childProviderThreadId: OrchestrationV2ProviderThread["id"] | null;
397
model: string | null;
398
result: string | null;
399
+ // A background task call returns at launch; held here until its child session stops.
400
+ launch: ToolPart | null;
401
}
402
403
interface OpenCodeThreadState {
@@ -1066,6 +1068,10 @@ export const makeOpenCodeAdapterV2 = Effect.fn("makeOpenCodeAdapterV2")(function
1068
const relatedSessionOwners = new Map<string, OpenCodeThreadState>();
1069
// Busy children outlive parent settlement; keep their server open.
1070
const busySessionIds = new Set<string>();
1071
+ // A background task returns before its new child session reports busy.
1072
+ const unstartedSessionIds = new Set<string>();
1073
+ // A child can error before any turn of it is projected; its next idle settles the launch.
1074
+ const sessionErrorOutcomes = new Map<string, TerminalTurnStatus>();
1075
// Remember settled requests across routing retries to prevent resurrection; only recent IDs matter.
1076
const settledNativeRequestIds = new Set<string>();
1077
const pendingChildRequestRoutes = new Set<string>();
@@ -1386,6 +1392,7 @@ export const makeOpenCodeAdapterV2 = Effect.fn("makeOpenCodeAdapterV2")(function
1392
state: OpenCodeThreadState,
1393
turn: ActiveOpenCodeTurn,
1394
part: ToolPart,
1395
+ childOutcome?: TerminalTurnStatus,
1396
) {
1397
const now = yield* DateTime.now;
1398
const nativeItemRef = providerRef(part.id);
@@ -1416,6 +1423,7 @@ export const makeOpenCodeAdapterV2 = Effect.fn("makeOpenCodeAdapterV2")(function
1423
childProviderThreadId: null,
1424
model: null,
1425
result: null,
1426
+ launch: null,
1427
};
1428
subagentsByNativeItemId.set(part.id, context);
1429
}
@@ -1527,8 +1535,27 @@ export const makeOpenCodeAdapterV2 = Effect.fn("makeOpenCodeAdapterV2")(function
1535
}
1536
const output = toolOutput(part);
1537
if (part.state.status === "completed" && output !== undefined) context.result = output;
1530
- const status = toolStatus(part);
1531
- const completedAt = toolCompletedAt(part, now);
1538
+ const launchStatus = toolStatus(part);
1539
+ context.launch =
1540
+ childOutcome === undefined &&
1541
+ launchStatus.item === "completed" &&
1542
+ context.childSessionId !== null &&
1543
+ (busySessionIds.has(context.childSessionId) ||
1544
+ unstartedSessionIds.has(context.childSessionId))
1545
+ ? part
1546
+ : null;
1547
+ const status =
1548
+ context.launch !== null
1549
+ ? ({ node: "running", item: "running" } as const)
1550
+ : childOutcome === undefined
1551
+ ? launchStatus
1552
+ : { node: childOutcome, item: childOutcome };
1553
+ const completedAt =
1554
+ context.launch !== null
1555
+ ? null
1556
+ : childOutcome === undefined
1557
+ ? toolCompletedAt(part, now)
1558
+ : now;
1559
const subagentStatus: OrchestrationV2Subagent["status"] = status.item;
1560
const subagent: OrchestrationV2Subagent = {
1561
id: nodeId,
@@ -1607,6 +1634,49 @@ export const makeOpenCodeAdapterV2 = Effect.fn("makeOpenCodeAdapterV2")(function
1634
});
1635
});
1636
1637
+ const settleLaunchedSubagents = Effect.fnUntraced(function* (
1638
+ childSessionId: string,
1639
+ outcome?: TerminalTurnStatus,
1640
+ ) {
1641
+ const lastTurn = Array.from(threads.get(childSessionId)?.providerTurns.values() ?? []).at(
1642
+ -1,
1643
+ )?.status;
1644
+ const errorOutcome = sessionErrorOutcomes.get(childSessionId);
1645
+ sessionErrorOutcomes.delete(childSessionId);
1646
+ const childOutcome =
1647
+ outcome ??
1648
+ errorOutcome ??
1649
+ (lastTurn === "interrupted" || lastTurn === "failed" || lastTurn === "cancelled"
1650
+ ? lastTurn
1651
+ : "completed");
1652
+ for (const context of subagentsByNativeItemId.values()) {
1653
+ if (context.childSessionId === childSessionId && context.launch !== null) {
1654
+ yield* emitSubagent(
1655
+ context.parentState,
1656
+ context.parentTurn,
1657
+ context.launch,
1658
+ childOutcome,
1659
+ );
1660
+ }
1661
+ }
1662
+ });
1663
+
1664
+ // No idle event settles a launched child that Stop aborts before it reports busy.
1665
+ const interruptLaunchedSubagents = Effect.fnUntraced(function* (rootSessionId: string) {
1666
+ for (const context of subagentsByNativeItemId.values()) {
1667
+ let owner = context.parentState;
1668
+ while (owner.parentSubagent !== null) owner = owner.parentSubagent.parentState;
1669
+ if (owner.nativeSessionId === rootSessionId && context.launch !== null) {
1670
+ yield* emitSubagent(
1671
+ context.parentState,
1672
+ context.parentTurn,
1673
+ context.launch,
1674
+ "interrupted",
1675
+ );
1676
+ }
1677
+ }
1678
+ });
1679
+
1680
const emitToolPart = Effect.fnUntraced(function* (
1681
state: OpenCodeThreadState,
1682
turn: ActiveOpenCodeTurn,
@@ -2712,6 +2782,9 @@ export const makeOpenCodeAdapterV2 = Effect.fn("makeOpenCodeAdapterV2")(function
2782
case "session.created":
2783
case "session.updated": {
2784
const info = event.properties.info;
2785
+ if (event.type === "session.created" && info.parentID !== undefined) {
2786
+ unstartedSessionIds.add(info.id);
2787
+ }
2788
if (info.parentID !== undefined && !threads.has(info.id)) {
2789
const owner = threads.get(info.parentID) ?? relatedSessionOwners.get(info.parentID);
2790
if (owner !== undefined) {
@@ -2723,9 +2796,12 @@ export const makeOpenCodeAdapterV2 = Effect.fn("makeOpenCodeAdapterV2")(function
2796
case "session.deleted":
2797
relatedSessionOwners.delete(event.properties.info.id);
2798
busySessionIds.delete(event.properties.info.id);
2799
+ unstartedSessionIds.delete(event.properties.info.id);
2800
+ yield* settleLaunchedSubagents(event.properties.info.id, "interrupted");
2801
return;
2802
case "session.status": {
2803
const sessionId = event.properties.sessionID;
2804
+ unstartedSessionIds.delete(sessionId);
2805
switch (event.properties.status.type) {
2806
case "busy":
2807
case "retry":
@@ -2757,10 +2833,14 @@ export const makeOpenCodeAdapterV2 = Effect.fn("makeOpenCodeAdapterV2")(function
2833
state.activeTurn.interrupted ? "interrupted" : "completed",
2834
);
2835
}
2836
+ if (event.properties.status.type === "idle") {
2837
+ yield* settleLaunchedSubagents(sessionId);
2838
+ }
2839
return;
2840
}
2841
case "session.idle": {
2842
busySessionIds.delete(event.properties.sessionID);
2843
+ unstartedSessionIds.delete(event.properties.sessionID);
2844
const state = threads.get(event.properties.sessionID);
2845
if (state?.activeTurn !== null && state?.activeTurn !== undefined) {
2846
if (state.activeTurn.admissionPending) {
@@ -2774,6 +2854,7 @@ export const makeOpenCodeAdapterV2 = Effect.fn("makeOpenCodeAdapterV2")(function
2854
state.activeTurn.interrupted ? "interrupted" : "completed",
2855
);
2856
}
2857
+ yield* settleLaunchedSubagents(event.properties.sessionID);
2858
return;
2859
}
2860
case "session.error": {
@@ -2791,6 +2872,12 @@ export const makeOpenCodeAdapterV2 = Effect.fn("makeOpenCodeAdapterV2")(function
2872
});
2873
return;
2874
}
2875
+ if (event.properties.sessionID !== undefined) {
2876
+ sessionErrorOutcomes.set(
2877
+ event.properties.sessionID,
2878
+ isMessageAbortedError(event) ? "interrupted" : "failed",
2879
+ );
2880
+ }
2881
const states =
2882
event.properties.sessionID === undefined
2883
? Array.from(threads.values()).filter((state) => state.activeTurn !== null)
@@ -2855,6 +2942,8 @@ export const makeOpenCodeAdapterV2 = Effect.fn("makeOpenCodeAdapterV2")(function
2942
Effect.flatMap((exit) =>
2943
Effect.gen(function* () {
2944
busySessionIds.clear();
2945
+ unstartedSessionIds.clear();
2946
+ sessionErrorOutcomes.clear();
2947
if (closing || abortController.signal.aborted) return;
2948
const detail = Exit.isSuccess(exit)
2949
? "OpenCode event stream ended unexpectedly."
@@ -2870,6 +2959,7 @@ export const makeOpenCodeAdapterV2 = Effect.fn("makeOpenCodeAdapterV2")(function
2959
failure: makeProviderFailure({ message: detail, class: "transport_error" }),
2960
threadDisposition: "broken",
2961
});
2962
+ yield* settleLaunchedSubagents(state.nativeSessionId, "failed");
2963
}
2964
yield* Queue.end(events);
2965
}),
@@ -3641,6 +3731,7 @@ export const makeOpenCodeAdapterV2 = Effect.fn("makeOpenCodeAdapterV2")(function
3731
for (const controller of commandControllers.get(sessionId) ?? [])
3732
controller.abort();
3733
yield* abortDescendants(sessionId);
3734
+ yield* interruptLaunchedSubagents(sessionId);
3735
return;
3736
}
3737
if (
@@ -3679,6 +3770,7 @@ export const makeOpenCodeAdapterV2 = Effect.fn("makeOpenCodeAdapterV2")(function
3770
);
3771
// Root abort leaves children running; report incomplete cleanup even after root termination.
3772
yield* abortDescendants(sessionId);
3773
+ yield* interruptLaunchedSubagents(sessionId);
3774
},
3775
(effect, interruptInput) =>
3776
effect.pipe(