The best way to control Claude Code, Codex and more from any computer or phone
fix(server): a background OpenCode subagent's question survives its parent's turn end (#37300)
Julius Marminge committed
Nov 15, 2026 at 09:48 UTC
03e05f6235cfbf0d4c0df9b5650e6509772302e8
6 files changed
+71
-4
apps/server/src/orchestration-v2/Adapters/OpenCode2AdapterV2.test.ts
+31
@@ -4004,6 +4004,37 @@ it.layer(McpProviderSessions.layer)("OpenCode2 adapter", (it) => {
4004
}),
4005
);
4006
4007
+ it.effect("keeps a background child's question open past its parent's turn end", () =>
4008
+ Effect.gen(function* () {
4009
+ const form = { ...colorForm, sessionID: CHILD };
4010
+ const { runtime, thread } = yield* resumed([
4011
+ ...launchedAndRunning,
4012
+ event("form.created", { form }),
4013
+ event("session.execution.succeeded", { sessionID: SESSION }),
4014
+ ]);
4015
+ const requested = yield* requestOf(runtime).pipe(Effect.forkScoped);
4016
+ const ended = yield* runtime.events.pipe(
4017
+ Stream.filter(
4018
+ (event) =>
4019
+ event.type === "turn.terminal" ||
4020
+ (event.type === "provider_turn.updated" && event.providerTurn.status === "completed"),
4021
+ ),
4022
+ Stream.take(2),
4023
+ Stream.runCollect,
4024
+ Effect.forkScoped,
4025
+ );
4026
+ yield* runtime.startTurn(withLineage(thread));
4027
+ const request = yield* Fiber.join(requested);
4028
+ assert.equal(request?.nativeRequestRef?.nativeId, form.id);
4029
+ for (const terminal of yield* Fiber.join(ended)) {
4030
+ assert.deepEqual(
4031
+ "retainedRuntimeRequestIds" in terminal ? terminal.retainedRuntimeRequestIds : undefined,
4032
+ [request!.id],
4033
+ );
4034
+ }
4035
+ }),
4036
+ );
4037
+
4038
it.effect("keeps a background child's requests when its parent stops during reconciliation", () =>
4039
Effect.gen(function* () {
4040
const reading = yield* Deferred.make<void>();
apps/server/src/orchestration-v2/ProviderEventIngestor.test.ts
+10
-3
@@ -1247,6 +1247,7 @@ layer("ProviderEventIngestorV2", (it) => {
1247
{ key: "answered", responseCapability: { type: "live", providerSessionId } },
1248
{ key: "other-turn", responseCapability: { type: "live", providerSessionId } },
1249
{ key: "approval", responseCapability: { type: "live", providerSessionId } },
1250
+ { key: "retained", responseCapability: { type: "live", providerSessionId } },
1251
] as const;
1252
const fixtures = specs.map((spec, ordinal) => {
1253
const nodeId = NodeId.make(`${threadId}:${spec.key}`);
@@ -1321,6 +1322,7 @@ layer("ProviderEventIngestorV2", (it) => {
1322
}
1323
}
1324
yield* eventSink.write({ events: seedEvents });
1325
+ const retainedRuntimeRequestIds = [RuntimeRequestId.make(`${threadId}:retained`)];
1326
const input = {
1327
providerSessionId,
1328
providerInstanceId: modelSelection.instanceId,
@@ -1336,10 +1338,12 @@ layer("ProviderEventIngestorV2", (it) => {
1338
status: "completed" as const,
1339
failure: null,
1340
threadDisposition: "reusable" as const,
1341
+ retainedRuntimeRequestIds,
1342
}
1343
: {
1344
type: "provider_turn.updated" as const,
1345
driver: CODEX_DRIVER,
1346
+ retainedRuntimeRequestIds,
1347
providerTurn: {
1348
id: providerTurnId,
1349
providerThreadId,
@@ -1379,9 +1383,12 @@ layer("ProviderEventIngestorV2", (it) => {
1383
stored.filter((entry) => entry.event.type === "runtime-request.updated").length,
1384
2,
1385
);
1382
- assert.isEmpty(
1383
- (yield* projectionStore.getPendingNativeUserInputs(threadId, providerTurnId))
1384
- .runtimeRequests,
1386
+ assert.deepEqual(
1387
+ (yield* projectionStore.getPendingNativeUserInputs(
1388
+ threadId,
1389
+ providerTurnId,
1390
+ )).runtimeRequests.map((request) => request.id),
1391
+ retainedRuntimeRequestIds,
1392
);
1393
const replayed = yield* eventStore.read({ threadId }).pipe(Stream.runCollect);
1394
assert.equal(
apps/server/src/orchestration-v2/ProviderEventIngestor.ts
+13
-1
@@ -15,6 +15,7 @@ import {
15
RawEventId,
16
RunAttemptId,
17
RunId,
18
+ type RuntimeRequestId,
19
ThreadId,
20
} from "@t3tools/contracts";
21
import * as Context from "effect/Context";
@@ -300,11 +301,14 @@ export const layer: Layer.Layer<
301
input: ProviderEventIngestInput,
302
providerTurnId: ProviderTurnId,
303
threadId = input.threadId,
304
+ retained: ReadonlyArray<RuntimeRequestId> = [],
305
) {
306
const pending = yield* projections.getPendingNativeUserInputs(threadId, providerTurnId);
307
const now = yield* DateTime.now;
308
const events: Array<OrchestrationV2DomainEvent> = [];
309
+ const kept = new Set(retained);
310
for (const request of pending.runtimeRequests) {
311
+ if (kept.has(request.id)) continue;
312
events.push(
313
yield* makeDomainEvent(input, {
314
type: "runtime-request.updated",
@@ -315,6 +319,7 @@ export const layer: Layer.Layer<
319
);
320
}
321
for (const node of pending.nodes) {
322
+ if (node.runtimeRequestId !== null && kept.has(node.runtimeRequestId)) continue;
323
events.push(
324
yield* makeDomainEvent(input, {
325
type: "node.updated",
@@ -326,6 +331,7 @@ export const layer: Layer.Layer<
331
);
332
}
333
for (const item of pending.turnItems) {
334
+ if (item.type === "user_input_request" && kept.has(item.requestId)) continue;
335
events.push(
336
yield* makeDomainEvent(input, {
337
type: "turn-item.updated",
@@ -529,6 +535,7 @@ export const layer: Layer.Layer<
535
input,
536
input.event.providerTurn.id,
537
input.event.threadId,
538
+ input.event.retainedRuntimeRequestIds,
539
)
540
: []),
541
yield* makeDomainEvent(input, {
@@ -654,7 +661,12 @@ export const layer: Layer.Layer<
661
// The session manager serves these to clients before ingestion.
662
return [];
663
case "turn.terminal":
657
- const dismissed = yield* dismissNativeUserInputs(input, input.event.providerTurnId);
664
+ const dismissed = yield* dismissNativeUserInputs(
665
+ input,
666
+ input.event.providerTurnId,
667
+ undefined,
668
+ input.event.retainedRuntimeRequestIds,
669
+ );
670
if (input.event.status !== "failed") {
671
return dismissed;
672
}
apps/server/src/orchestration-v2/ProviderSessionManager.ts
+3
@@ -2710,6 +2710,9 @@ export const layerWithOptions = (
2710
retry: stalled.retry,
2711
...(stalled.startedAt === null ? {} : { retryStartedAt: stalled.startedAt }),
2712
threadDisposition: event.threadDisposition,
2713
+ ...(event.retainedRuntimeRequestIds === undefined
2714
+ ? {}
2715
+ : { retainedRuntimeRequestIds: event.retainedRuntimeRequestIds }),
2716
},
2717
};
2718
};
packages/provider-core/src/server/ProviderAdapter.ts
+4
@@ -107,6 +107,8 @@ export const ProviderAdapterV2Event = Schema.Union([
107
driver: ProviderDriverKind,
108
threadId: Schema.optional(ThreadId),
109
providerTurn: OrchestrationV2ProviderTurn,
110
+ /** Questions the adapter keeps open after this turn ends; the rest of the turn's are dismissed. */
111
+ retainedRuntimeRequestIds: Schema.optional(Schema.Array(RuntimeRequestId)),
112
}),
113
Schema.Struct({
114
type: Schema.Literal("node.updated"),
@@ -184,6 +186,7 @@ export const ProviderAdapterV2Event = Schema.Union([
186
failure: Schema.Null,
187
reason: Schema.optional(Schema.String),
188
threadDisposition: Schema.Literals(["reusable", "broken"]),
189
+ retainedRuntimeRequestIds: Schema.optional(Schema.Array(RuntimeRequestId)),
190
}),
191
Schema.Struct({
192
type: Schema.Literal("turn.terminal"),
@@ -199,6 +202,7 @@ export const ProviderAdapterV2Event = Schema.Union([
202
threadDisposition: Schema.Literals(["reusable", "broken"]),
203
/** Child turns outlive root failure and report their own terminals; otherwise open children settle. */
204
childTurnsContinue: Schema.optional(Schema.Literal(true)),
205
+ retainedRuntimeRequestIds: Schema.optional(Schema.Array(RuntimeRequestId)),
206
}),
207
]);
208
export type ProviderAdapterV2Event = typeof ProviderAdapterV2Event.Type;
packages/provider-opencode/src/server/v2/adapter.ts
+10
@@ -1516,6 +1516,14 @@ export const make = Effect.fn("OpenCode2Adapter.make")(function* (instanceId: Pr
1516
yield* emitRetry(state, turn, "running");
1517
});
1518
1519
+ // A background child's question outlives its parent's turn; keep the ingestor from dismissing it.
1520
+ const retainedRequestsOf = (turn: ActiveTurn) => {
1521
+ const ids = [...pending.values()].flatMap((entry) =>
1522
+ entry.turn === turn ? [entry.request.id] : [],
1523
+ );
1524
+ return ids.length === 0 ? {} : { retainedRuntimeRequestIds: ids };
1525
+ };
1526
+
1527
const emitProviderTurn = (
1528
state: ThreadState,
1529
turn: ActiveTurn,
@@ -1527,6 +1535,7 @@ export const make = Effect.fn("OpenCode2Adapter.make")(function* (instanceId: Pr
1535
driver,
1536
threadId: turn.input.threadId,
1537
providerTurn,
1538
+ ...(providerTurn.completedAt === null ? {} : retainedRequestsOf(turn)),
1539
});
1540
};
1541
@@ -2337,6 +2346,7 @@ export const make = Effect.fn("OpenCode2Adapter.make")(function* (instanceId: Pr
2346
providerTurnId: turn.providerTurn.id,
2347
runOrdinal: turn.input.runOrdinal,
2348
threadDisposition,
2349
+ ...retainedRequestsOf(turn),
2350
};
2351
yield* emit(
2352
terminal.status === "failed"