Skip to content

Commit 62addc8

Browse files
committed
fix(opencode): propagate image tasks to parent threads
1 parent 80401f6 commit 62addc8

2 files changed

Lines changed: 95 additions & 81 deletions

File tree

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

Lines changed: 80 additions & 68 deletions
Original file line numberDiff line numberDiff line change
@@ -3226,75 +3226,87 @@ it.layer(McpProviderSessions.layer)("OpenCode2 adapter", (it) => {
32263226
}).pipe(Effect.scoped, Effect.provide(NodeServices.layer)),
32273227
);
32283228

3229-
it.effect("settles native image tasks when reconnect attempts fail", () =>
3230-
Effect.gen(function* () {
3231-
const attempts = yield* Effect.forEach([0, 1, 2, 3, 4], () => Deferred.make<void>());
3232-
let count = 0;
3233-
let failures = 0;
3234-
const borrowers = {
3235-
get current() {
3236-
return count;
3237-
},
3238-
set current(next: number) {
3239-
if (next < count && count === 2) {
3240-
const attempt = attempts[failures++];
3241-
if (attempt !== undefined) Deferred.doneUnsafe(attempt, Effect.void);
3242-
}
3243-
count = next;
3244-
},
3245-
};
3246-
const tool = {
3247-
sessionID: SESSION,
3248-
assistantMessageID: "msg_assistant",
3249-
id: "call-image-lost",
3250-
};
3251-
const { runtime, thread } = yield* resumed(
3252-
[
3253-
out("session.prompt", { sessionID: SESSION, text: "<any>" }),
3254-
promptAccepted,
3255-
event("session.tool.input.started", { ...tool, name: "experimental_image_generate" }),
3256-
event(
3257-
"session.tool.success",
3258-
{
3259-
...tool,
3260-
content: [{ type: "text", text: "Launched" }],
3261-
metadata: { jobID: "image_lost", path: "/tmp/image", model: "test/image" },
3262-
executed: false,
3263-
},
3264-
undefined,
3265-
2,
3229+
it.effect.each(["root", "child"] as const)(
3230+
"settles %s native image tasks when reconnect attempts fail",
3231+
(owner) =>
3232+
Effect.gen(function* () {
3233+
const attempts = yield* Effect.forEach([0, 1, 2, 3, 4], () => Deferred.make<void>());
3234+
let count = 0;
3235+
let failures = 0;
3236+
const borrowers = {
3237+
get current() {
3238+
return count;
3239+
},
3240+
set current(next: number) {
3241+
if (next < count && count === 2) {
3242+
const attempt = attempts[failures++];
3243+
if (attempt !== undefined) Deferred.doneUnsafe(attempt, Effect.void);
3244+
}
3245+
count = next;
3246+
},
3247+
};
3248+
const tool = {
3249+
sessionID: owner === "root" ? SESSION : CHILD,
3250+
assistantMessageID: "msg_assistant",
3251+
id: "call-image-lost",
3252+
};
3253+
const { runtime, thread } = yield* resumed(
3254+
[
3255+
...(owner === "root"
3256+
? [out("session.prompt", { sessionID: SESSION, text: "<any>" }), promptAccepted]
3257+
: [
3258+
...backgroundLaunch(CHILD),
3259+
event("session.execution.started", { sessionID: CHILD }),
3260+
]),
3261+
event("session.tool.input.started", { ...tool, name: "experimental_image_generate" }),
3262+
event(
3263+
"session.tool.success",
3264+
{
3265+
...tool,
3266+
content: [{ type: "text", text: "Launched" }],
3267+
metadata: { jobID: "image_lost", path: "/tmp/image", model: "test/image" },
3268+
executed: false,
3269+
},
3270+
undefined,
3271+
2,
3272+
),
3273+
...(owner === "child"
3274+
? [event("session.execution.succeeded", { sessionID: CHILD })]
3275+
: []),
3276+
event("session.execution.succeeded", { sessionID: SESSION }),
3277+
{ type: "runtime_exit", status: "success" },
3278+
],
3279+
{ borrowers },
3280+
);
3281+
const events = yield* runtime.events.pipe(Stream.runCollect, Effect.forkScoped);
3282+
yield* runtime.startTurn(withLineage(thread));
3283+
yield* Deferred.await(attempts[0]!);
3284+
for (const attempt of attempts.slice(1)) {
3285+
yield* TestClock.adjust("2 seconds");
3286+
yield* Deferred.await(attempt);
3287+
}
3288+
const collected = yield* Fiber.join(events);
3289+
assert.isFalse(yield* runtime.hasPendingBackgroundWorkForThread!(thread));
3290+
assert.isTrue(
3291+
collected.some(
3292+
(e) =>
3293+
e.type === "turn_item.updated" &&
3294+
e.turnItem.nativeItemRef?.nativeId ===
3295+
(owner === "root" ? tool.id : `${CHILD}:${tool.id}`) &&
3296+
e.turnItem.status === "failed",
32663297
),
3267-
event("session.execution.succeeded", { sessionID: SESSION }),
3268-
{ type: "runtime_exit", status: "success" },
3269-
],
3270-
{ borrowers },
3271-
);
3272-
const events = yield* runtime.events.pipe(Stream.runCollect, Effect.forkScoped);
3273-
yield* runtime.startTurn(turnInput(thread));
3274-
yield* Deferred.await(attempts[0]!);
3275-
for (const attempt of attempts.slice(1)) {
3276-
yield* TestClock.adjust("2 seconds");
3277-
yield* Deferred.await(attempt);
3278-
}
3279-
const collected = yield* Fiber.join(events);
3280-
assert.isFalse(yield* runtime.hasPendingBackgroundWorkForThread!(thread));
3281-
assert.isTrue(
3282-
collected.some(
3283-
(e) =>
3284-
e.type === "turn_item.updated" &&
3285-
e.turnItem.nativeItemRef?.nativeId === tool.id &&
3286-
e.turnItem.status === "failed",
3287-
),
3288-
);
3289-
assert.deepEqual(
3290-
collected
3291-
.flatMap((e) =>
3292-
e.type === "provider_thread.updated" ? [e.providerThread.pendingBackgroundTasks] : [],
3293-
)
3294-
.at(-1),
3295-
[],
3296-
);
3297-
}).pipe(Effect.scoped),
3298+
);
3299+
const rootSnapshots = collected.flatMap((e) =>
3300+
e.type === "provider_thread.updated" && e.providerThread.id === thread.id ? [e] : [],
3301+
);
3302+
assert.isFalse(rootSnapshots.at(-1)?.pendingBackgroundWork);
3303+
assert.deepEqual(rootSnapshots.at(-1)?.providerThread.pendingBackgroundTasks, []);
3304+
assert.isTrue(
3305+
rootSnapshots.some((e) =>
3306+
e.providerThread.pendingBackgroundTasks?.some((task) => task.taskId === "image_lost"),
3307+
),
3308+
);
3309+
}).pipe(Effect.scoped),
32983310
);
32993311

33003312
it.effect("projects successful native todo updates into one checklist", () =>

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

Lines changed: 15 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1204,7 +1204,7 @@ export const make = Effect.fn("OpenCode2Adapter.make")(function* (instanceId: Pr
12041204
status: "running",
12051205
});
12061206
status = "running";
1207-
yield* syncRoster(state);
1207+
yield* syncRosterAncestors(state);
12081208
}
12091209
}
12101210
const updatedAt = yield* DateTime.now;
@@ -1471,16 +1471,18 @@ export const make = Effect.fn("OpenCode2Adapter.make")(function* (instanceId: Pr
14711471
? {}
14721472
: { childThreadId: call.child.subagent.appThread.id }),
14731473
}));
1474-
const imageTasks = [...state.imageJobs.entries()].flatMap(([taskId, job]) =>
1475-
job.status === "running"
1476-
? [
1477-
{
1478-
taskId,
1479-
kind: "background_task" as const,
1480-
description: `Image generation (${job.model})`,
1481-
},
1482-
]
1483-
: [],
1474+
const imageTasks = sessionsOf(state).flatMap((session) =>
1475+
[...session.imageJobs.entries()].flatMap(([taskId, job]) =>
1476+
job.status === "running"
1477+
? [
1478+
{
1479+
taskId,
1480+
kind: "background_task" as const,
1481+
description: `Image generation (${job.model})`,
1482+
},
1483+
]
1484+
: [],
1485+
),
14841486
);
14851487
const allTasks = [...roster, ...imageTasks];
14861488
const current = state.providerThread.pendingBackgroundTasks ?? [];
@@ -2527,7 +2529,7 @@ export const make = Effect.fn("OpenCode2Adapter.make")(function* (instanceId: Pr
25272529
{ output: payload.text, metadata: undefined, files },
25282530
);
25292531
job.turn.tools.delete(job.toolId);
2530-
yield* syncRoster(state);
2532+
yield* syncRosterAncestors(state);
25312533
}),
25322534
),
25332535
);
@@ -2836,7 +2838,7 @@ export const make = Effect.fn("OpenCode2Adapter.make")(function* (instanceId: Pr
28362838
}
28372839
for (const wake of state.wakes.splice(0)) wake.dropped = true;
28382840
state.reports.clear();
2839-
yield* syncRoster(state);
2841+
yield* syncRosterAncestors(state);
28402842
}
28412843
yield* setSessionStatus("error", message);
28422844
yield* Queue.end(events);

0 commit comments

Comments
 (0)