Skip to content

Commit 7484747

Browse files
committed
fix(opencode): ignore replayed Task terminals
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. Keep recovered foreground Tasks settled when buffered terminal tool events replay. Preserve lifecycle handling for new and resumed Task work.
1 parent 518c12e commit 7484747

2 files changed

Lines changed: 52 additions & 7 deletions

File tree

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

Lines changed: 43 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1678,9 +1678,36 @@ it.layer(OpenCodeAdapterTestLayer)("OpenCodeAdapterLive", (it) => {
16781678
}),
16791679
),
16801680
];
1681+
runtimeMock.state.subscribedEvents = [
1682+
partUpdated(runtimeMock.state.messages[0]?.parts[0]),
1683+
partUpdated(runtimeMock.state.messages[1]?.parts[0]),
1684+
partUpdated({
1685+
id: "history-foreground-sentinel",
1686+
sessionID: OPEN_CODE_SESSION_ID,
1687+
messageID: "msg-history-foreground-sentinel",
1688+
type: "tool",
1689+
callID: "call-history-foreground-sentinel",
1690+
tool: "bash",
1691+
state: {
1692+
status: "completed",
1693+
input: {},
1694+
title: "Verify foreground history reconciliation",
1695+
output: "done",
1696+
metadata: {},
1697+
time: { start: 3, end: 4 },
1698+
},
1699+
}),
1700+
];
16811701
const eventsFiber = yield* adapter.streamEvents.pipe(
1682-
Stream.filter((event) => event.threadId === threadId && event.type === "task.completed"),
1683-
Stream.take(2),
1702+
Stream.filter(
1703+
(event) =>
1704+
event.threadId === threadId &&
1705+
(event.type === "task.completed" ||
1706+
event.type === "task.updated" ||
1707+
(event.type === "item.completed" &&
1708+
String(event.itemId) === "call-history-foreground-sentinel")),
1709+
),
1710+
Stream.take(3),
16841711
Stream.runCollect,
16851712
Effect.forkChild,
16861713
);
@@ -1694,15 +1721,25 @@ it.layer(OpenCodeAdapterTestLayer)("OpenCodeAdapterLive", (it) => {
16941721

16951722
const events = Array.from(yield* Fiber.join(eventsFiber).pipe(Effect.timeout("1 second")));
16961723
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-
}),
1724+
events.flatMap((event) =>
1725+
event.type === "task.completed"
1726+
? [[event.payload.taskId, event.payload.status, event.payload.summary]]
1727+
: [],
1728+
),
17011729
[
17021730
["task-history-completed", "completed", "Stored completed result."],
17031731
["task-history-failed", "failed", "Stored failure."],
17041732
],
17051733
);
1734+
NodeAssert.equal(events.filter((event) => event.type === "task.updated").length, 0);
1735+
NodeAssert.equal(
1736+
events.filter(
1737+
(event) =>
1738+
event.type === "item.completed" &&
1739+
String(event.itemId) === "call-history-foreground-sentinel",
1740+
).length,
1741+
1,
1742+
);
17061743
}),
17071744
);
17081745

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

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1258,7 +1258,15 @@ export function makeOpenCodeAdapter(
12581258
const task = taskLifecycleFromPart(part, previousTask);
12591259
const taskRemainsRunning =
12601260
part.state.status === "completed" && part.state.metadata["background"] === true;
1261-
if (task && previousTaskPart?.state.status !== part.state.status) {
1261+
const replaysKnownTerminal =
1262+
previousTask?.toolUseId === part.callID &&
1263+
((previousTask.status === "completed" && part.state.status === "completed") ||
1264+
(previousTask.status === "failed" && part.state.status === "error"));
1265+
if (
1266+
task &&
1267+
!replaysKnownTerminal &&
1268+
previousTaskPart?.state.status !== part.state.status
1269+
) {
12621270
const linkage = taskLinkage(task);
12631271
if (!previousTaskPart) {
12641272
const completedBeforeStart =

0 commit comments

Comments
 (0)