Skip to content

Commit 518c12e

Browse files
committed
fix(opencode): recover foreground Tasks after reconnect
Written by inayayousfi, typed by GPT-5.6 Sol running in OpenCode. Every call here is inayayousfi's, and no agent acted on its own. Reconstruct completed and failed foreground Task terminals from stored tool parts. Keep background Tasks running until their synthetic result arrives.
1 parent 15d828d commit 518c12e

2 files changed

Lines changed: 97 additions & 1 deletion

File tree

‎apps/server/src/provider/Layers/OpenCodeAdapter.test.ts‎

Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1636,6 +1636,76 @@ it.layer(OpenCodeAdapterTestLayer)("OpenCodeAdapterLive", (it) => {
16361636
}),
16371637
);
16381638

1639+
it.effect("recovers stored foreground Task terminals after reconnect", () =>
1640+
Effect.gen(function* () {
1641+
const adapter = yield* OpenCodeAdapter;
1642+
const threadId = asThreadId("thread-opencode-foreground-task-history");
1643+
runtimeMock.state.messages = [
1644+
messageEntry(
1645+
"msg-history-completed",
1646+
"assistant",
1647+
taskPart({
1648+
id: "history-completed",
1649+
taskId: "task-history-completed",
1650+
description: "Inspect completed history",
1651+
role: "explore",
1652+
status: "completed",
1653+
result: "Stored completed result.",
1654+
}),
1655+
),
1656+
messageEntry(
1657+
"msg-history-failed",
1658+
"assistant",
1659+
taskPart({
1660+
id: "history-failed",
1661+
taskId: "task-history-failed",
1662+
description: "Inspect failed history",
1663+
role: "general",
1664+
status: "error",
1665+
result: "Stored failure.",
1666+
}),
1667+
),
1668+
messageEntry(
1669+
"msg-history-background-running",
1670+
"assistant",
1671+
taskPart({
1672+
id: "history-background-running",
1673+
taskId: "task-history-background-running",
1674+
description: "Inspect background history",
1675+
role: "explore",
1676+
status: "completed",
1677+
background: true,
1678+
}),
1679+
),
1680+
];
1681+
const eventsFiber = yield* adapter.streamEvents.pipe(
1682+
Stream.filter((event) => event.threadId === threadId && event.type === "task.completed"),
1683+
Stream.take(2),
1684+
Stream.runCollect,
1685+
Effect.forkChild,
1686+
);
1687+
1688+
yield* adapter.startSession({
1689+
provider: ProviderDriverKind.make("opencode"),
1690+
threadId,
1691+
runtimeMode: "full-access",
1692+
resumeCursor: { schemaVersion: 1, sessionId: OPEN_CODE_SESSION_ID },
1693+
});
1694+
1695+
const events = Array.from(yield* Fiber.join(eventsFiber).pipe(Effect.timeout("1 second")));
1696+
NodeAssert.deepEqual(
1697+
events.map((event) => {
1698+
NodeAssert.ok(event.type === "task.completed");
1699+
return [event.payload.taskId, event.payload.status, event.payload.summary];
1700+
}),
1701+
[
1702+
["task-history-completed", "completed", "Stored completed result."],
1703+
["task-history-failed", "failed", "Stored failure."],
1704+
],
1705+
);
1706+
}),
1707+
);
1708+
16391709
it.effect("deduplicates a background terminal seen in stored and live events", () =>
16401710
Effect.gen(function* () {
16411711
const adapter = yield* OpenCodeAdapter;

‎apps/server/src/provider/Layers/OpenCodeAdapter.ts‎

Lines changed: 27 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -654,6 +654,30 @@ function taskNotificationFromPart(part: Part): OpenCodeTaskNotification | undefi
654654
};
655655
}
656656

657+
function taskNotificationFromToolPart(part: Part): OpenCodeTaskNotification | undefined {
658+
if (
659+
part.type !== "tool" ||
660+
(part.state.status !== "completed" && part.state.status !== "error")
661+
) {
662+
return undefined;
663+
}
664+
const taskId = taskIdFromPart(part);
665+
if (
666+
!taskId ||
667+
(part.state.status === "completed" && part.state.metadata["background"] === true)
668+
) {
669+
return undefined;
670+
}
671+
const summary = trimText(
672+
part.state.status === "error" ? part.state.error : openCodeTaskResult(part.state.output),
673+
);
674+
return {
675+
taskId,
676+
status: part.state.status === "error" ? "failed" : "completed",
677+
summary,
678+
};
679+
}
680+
657681
function taskLinkage(task: OpenCodeTaskLifecycle) {
658682
return {
659683
taskType: "subagent",
@@ -1024,7 +1048,9 @@ export function makeOpenCodeAdapter(
10241048
}
10251049
}
10261050

1027-
const terminal = taskNotificationFromPart(part);
1051+
const terminal =
1052+
taskNotificationFromPart(part) ??
1053+
(entry.info.role === "assistant" ? taskNotificationFromToolPart(part) : undefined);
10281054
if (!terminal) {
10291055
continue;
10301056
}

0 commit comments

Comments
 (0)