Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -1092,6 +1092,94 @@ it.layer(McpProviderSessions.layer)("OpenCode2 adapter", (it) => {
event("session.execution.started", { sessionID: CHILD }),
];

it.effect("settles a background subagent that fails, so no work is left pending", () =>
Effect.gen(function* () {
const { runtime, thread } = yield* resumed([
...launchedAndRunning,
// The parent turn ends, and the background child fails: it reports
// nothing back, so its call has to end with the failure.
event("session.execution.succeeded", { sessionID: SESSION }),
event("session.execution.failed", {
sessionID: CHILD,
error: { type: "provider.quota", message: "Rate limit exceeded." },
}),
]);
const watch = yield* watchCalls(runtime, thread);
yield* runtime.startTurn(withLineage(thread));
yield* watch.until(
() => watch.calls.get("Sleep")?.status === "failed" && watch.state.roster?.length === 0,
);
assert.deepEqual(watch.state.roster, []);
assert.isFalse(yield* runtime.hasPendingBackgroundWorkForThread!(thread));
}).pipe(Effect.scoped),
);

it.effect("settles every call joined to a failed background subagent", () =>
Effect.gen(function* () {
const { runtime, thread } = yield* resumed([
...launchedAndRunning,
// A second call joins the running child, so its turn answers for both.
...callAgain({ background: true }),
event("session.execution.succeeded", { sessionID: SESSION }),
event("session.execution.failed", {
sessionID: CHILD,
error: { type: "provider.quota", message: "Rate limit exceeded." },
}),
]);
const watch = yield* watchCalls(runtime, thread);
yield* runtime.startTurn(withLineage(thread));
yield* watch.until(
() =>
watch.calls.get("Sleep")?.status === "failed" &&
watch.calls.get("Again")?.status === "failed" &&
watch.state.roster?.length === 0,
);
assert.deepEqual(watch.state.roster, []);
assert.isFalse(yield* runtime.hasPendingBackgroundWorkForThread!(thread));
}).pipe(Effect.scoped),
);

it.effect("settles the background call a foreground call joined to a failed child", () =>
Effect.gen(function* () {
const again = { sessionID: SESSION, assistantMessageID: "msg_assistant", id: "call-again" };
const { runtime, thread } = yield* resumed([
...launchedAndRunning,
// A foreground call joins the running background child. It is only
// marked background once its own tool call returns, so the child can
// fail while this call is still the foreground one.
event("session.tool.input.started", { ...again, name: "subagent" }),
event("session.tool.called", {
...again,
name: "subagent",
input: { description: "Again", prompt: "also this", sessionID: CHILD },
executed: false,
}),
event("session.tool.progress", {
...again,
metadata: { sessionID: CHILD, status: "running" },
}),
out("session.update", { sessionID: CHILD, permissions: "<any>" }),
reply("session.update", null),
// The child fails while the joined call is still the foreground one.
event("session.execution.failed", {
sessionID: CHILD,
error: { type: "provider.quota", message: "Rate limit exceeded." },
}),
event("session.execution.succeeded", { sessionID: SESSION }),
]);
const watch = yield* watchCalls(runtime, thread);
yield* runtime.startTurn(withLineage(thread));
yield* watch.until(
() =>
watch.calls.get("Sleep")?.status === "failed" &&
watch.calls.get("Again")?.status === "failed" &&
watch.state.roster?.length === 0,
);
assert.deepEqual(watch.state.roster, []);
assert.isFalse(yield* runtime.hasPendingBackgroundWorkForThread!(thread));
}).pipe(Effect.scoped),
);

it.effect("settles every call that joined a running subagent with its one report", () =>
Effect.gen(function* () {
const { runtime, thread } = yield* resumed([
Expand Down
21 changes: 21 additions & 0 deletions packages/provider-opencode/src/server/v2/adapter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -799,6 +799,16 @@ export const make = Effect.fn("OpenCode2Adapter.make")(function* (instanceId: Pr
const runningCalls = (thread: ThreadState): ReadonlyArray<SubagentCall> =>
sessionsOf(thread).flatMap((state) => [...state.calls.values()]);

/**
* The calls a subagent session answers for: the one that launched it and
* the ones that joined it while it ran.
*/
const callsAttachedTo = (child: ThreadState): ReadonlyArray<SubagentCall> => {
const parent = child.subagent?.call.state;
if (parent === undefined) return [];
return [...parent.calls.values()].filter((candidate) => candidate.child === child);
};

const callsAbove = (sessionId: string) => {
const calls: Array<SubagentCall> = [];
let current = threads.get(sessionId);
Expand Down Expand Up @@ -1557,6 +1567,17 @@ export const make = Effect.fn("OpenCode2Adapter.make")(function* (instanceId: Pr
if (!call.background && terminal.status !== "completed") {
yield* settleCall(call, terminal.status);
}
if (terminal.status === "failed") {
// A background call is settled by the report its parent reads, and a
// child that failed has no report to read: every call attached to it
// has to end with the failure, or the background ones keep the thread
// showing work that is gone. Which of them joined the child last
// does not matter: a foreground call is one until it is marked
// background, and the earlier background call outlives that window.
for (const attached of callsAttachedTo(state)) {
yield* settleCall(attached, terminal.status, terminal.failure.message);
}
}
return;
}
state.providerThread = {
Expand Down
Loading