The best way to control Claude Code, Codex and more from any computer or phone
fix(server): tool calls a released session cuts off no longer keep the thread Waiting (#29036)
Co-authored-by: Spike <spike@example.com> Co-authored-by: maria-rcks <maria@kuuro.net> Co-authored-by: Julius Marminge <julius0216@outlook.com>
IamReliable committed
Nov 13, 2026 at 06:08 UTC
70833b2f558ee86d7a31d954126e282c8340ac22
6 files changed
+672
-122
apps/server/src/orchestration-v2/EventSink.ts
+65
-17
@@ -169,6 +169,17 @@ export interface EventSinkV2Shape {
169
},
170
EventSinkV2Error
171
>;
172
+ /** Commits only if the stored turn item still equals expected. */
173
+ readonly writeIfTurnItemUnchanged: (input: {
174
+ readonly expected: OrchestrationV2TurnItem;
175
+ readonly events: ReadonlyArray<OrchestrationV2DomainEvent>;
176
+ }) => Effect.Effect<
177
+ {
178
+ readonly committed: boolean;
179
+ readonly storedEvents: ReadonlyArray<OrchestrationV2StoredEvent>;
180
+ },
181
+ EventSinkV2Error
182
+ >;
183
readonly commitCommand: (input: {
184
readonly commandId: CommandId;
185
readonly threadId: ThreadId;
@@ -634,23 +645,15 @@ const layerBase: Layer.Layer<
645
);
646
});
647
637
- const writeIfProviderThreadUnchangedEffect = Effect.fn(
638
- "orchestrationV2.EventSink.writeIfProviderThreadUnchanged",
639
- )(function* (input: Parameters<EventSinkV2Shape["writeIfProviderThreadUnchanged"]>[0]) {
640
- yield* Effect.annotateCurrentSpan({
641
- "orchestration_v2.event_count": input.events.length,
642
- "orchestration_v2.provider_thread_id": input.expected.id,
643
- });
644
- const expectedJson = yield* ProjectionStore.encodeProviderThreadPayload(input.expected);
645
-
646
- return yield* commitThenPublish(
648
+ // Appends the events only if the stored payload still matches, in one transaction.
649
+ const writeIfStoredPayloadMatches = <E>(
650
+ readStoredPayload: Effect.Effect<ReadonlyArray<{ readonly payload_json: string }>, E>,
651
+ expectedJson: string,
652
+ events: ReadonlyArray<OrchestrationV2DomainEvent>,
653
+ ) =>
654
+ commitThenPublish(
655
Effect.gen(function* () {
648
- const rows = yield* sql<{ readonly payload_json: string }>`
649
- SELECT payload_json
650
- FROM orchestration_v2_projection_provider_threads
651
- WHERE provider_thread_id = ${input.expected.id}
652
- LIMIT 1
653
- `;
656
+ const rows = yield* readStoredPayload;
657
if (rows[0]?.payload_json !== expectedJson) {
658
return {
659
committed: false as const,
@@ -658,7 +661,7 @@ const layerBase: Layer.Layer<
661
};
662
}
663
661
- const normalized = yield* normalizeEvents(input.events);
664
+ const normalized = yield* normalizeEvents(events);
665
const storedEvents = yield* eventStore.append({ events: normalized });
666
const outcomes = yield* applyStoredEvents(storedEvents);
667
return { committed: true as const, storedEvents, outcomes };
@@ -668,6 +671,45 @@ const layerBase: Layer.Layer<
671
? publishStoredEvents(result.storedEvents, result.outcomes)
672
: Effect.void,
673
);
674
+
675
+ const writeIfProviderThreadUnchangedEffect = Effect.fn(
676
+ "orchestrationV2.EventSink.writeIfProviderThreadUnchanged",
677
+ )(function* (input: Parameters<EventSinkV2Shape["writeIfProviderThreadUnchanged"]>[0]) {
678
+ yield* Effect.annotateCurrentSpan({
679
+ "orchestration_v2.event_count": input.events.length,
680
+ "orchestration_v2.provider_thread_id": input.expected.id,
681
+ });
682
+ const expectedJson = yield* ProjectionStore.encodeProviderThreadPayload(input.expected);
683
+ return yield* writeIfStoredPayloadMatches(
684
+ sql<{ readonly payload_json: string }>`
685
+ SELECT payload_json
686
+ FROM orchestration_v2_projection_provider_threads
687
+ WHERE provider_thread_id = ${input.expected.id}
688
+ LIMIT 1
689
+ `,
690
+ expectedJson,
691
+ input.events,
692
+ );
693
+ });
694
+
695
+ const writeIfTurnItemUnchangedEffect = Effect.fn(
696
+ "orchestrationV2.EventSink.writeIfTurnItemUnchanged",
697
+ )(function* (input: Parameters<EventSinkV2Shape["writeIfTurnItemUnchanged"]>[0]) {
698
+ yield* Effect.annotateCurrentSpan({
699
+ "orchestration_v2.event_count": input.events.length,
700
+ "orchestration_v2.turn_item_id": input.expected.id,
701
+ });
702
+ const expectedJson = yield* ProjectionStore.encodeTurnItemPayload(input.expected);
703
+ return yield* writeIfStoredPayloadMatches(
704
+ sql<{ readonly payload_json: string }>`
705
+ SELECT payload_json
706
+ FROM orchestration_v2_projection_turn_items
707
+ WHERE turn_item_id = ${input.expected.id}
708
+ LIMIT 1
709
+ `,
710
+ expectedJson,
711
+ input.events,
712
+ );
713
});
714
715
const existingCommandResult =
@@ -1070,6 +1112,12 @@ const layerBase: Layer.Layer<
1112
(cause) => new EventSinkWriteError({ eventCount: input.events.length, cause }),
1113
),
1114
),
1115
+ writeIfTurnItemUnchanged: (input) =>
1116
+ writeIfTurnItemUnchangedEffect(input).pipe(
1117
+ Effect.mapError(
1118
+ (cause) => new EventSinkWriteError({ eventCount: input.events.length, cause }),
1119
+ ),
1120
+ ),
1121
commitCommand: (input) =>
1122
commitCommandEffect(input).pipe(
1123
Effect.mapError(
apps/server/src/orchestration-v2/ProjectionStore.ts
+1
-1
@@ -1192,7 +1192,7 @@ const encodeMessagePayload = Schema.encodeEffect(
1192
const encodePlanPayload = Schema.encodeEffect(
1193
Schema.fromJsonString(OrchestrationV2PlanArtifactSchema),
1194
);
1195
-const encodeTurnItemPayload = Schema.encodeEffect(
1195
+export const encodeTurnItemPayload = Schema.encodeEffect(
1196
Schema.fromJsonString(OrchestrationV2TurnItemJsonSchema),
1197
);
1198
// The codec every *Json payload schema uses for timestamps, so index columns match payload_json.
apps/server/src/orchestration-v2/ProviderSessionManager.test.ts
+257
-30
@@ -12,6 +12,7 @@ import {
12
type OrchestrationV2ProviderCapabilities,
13
type OrchestrationV2ProviderSession,
14
type OrchestrationV2ProviderThread,
15
+ type OrchestrationV2TurnItem,
16
type Project,
17
ProjectId,
18
ProviderDriverKind,
@@ -19,6 +20,7 @@ import {
20
type ProviderSessionId,
21
RunId,
22
ThreadId,
23
+ TurnItemId,
24
} from "@t3tools/contracts";
25
import * as Cause from "effect/Cause";
26
import * as DateTime from "effect/DateTime";
@@ -155,7 +157,11 @@ const layerPausingAttachEventSink = (pause: {
157
}),
158
).pipe(Layer.provide(layerTestEventSink));
159
158
-const layerBeforeUnchangedWriteEventSink = (before: Ref.Ref<Effect.Effect<void>>) =>
160
+const layerBeforeUnchangedWriteEventSink = (
161
+ before: Ref.Ref<Effect.Effect<void>>,
162
+ // One effect runs before each turn-item write, in order.
163
+ beforeTurnItems: Ref.Ref<ReadonlyArray<Effect.Effect<void>>> | undefined,
164
+) =>
165
Layer.effect(
166
EventSink.EventSinkV2,
167
Effect.gen(function* () {
@@ -167,6 +173,13 @@ const layerBeforeUnchangedWriteEventSink = (before: Ref.Ref<Effect.Effect<void>>
173
Effect.flatten,
174
Effect.andThen(delegate.writeIfProviderThreadUnchanged(input)),
175
),
176
+ writeIfTurnItemUnchanged: (input) =>
177
+ (beforeTurnItems === undefined
178
+ ? Effect.void
179
+ : Ref.modify(beforeTurnItems, ([next, ...rest]) => [next ?? Effect.void, rest]).pipe(
180
+ Effect.flatten,
181
+ )
182
+ ).pipe(Effect.andThen(delegate.writeIfTurnItemUnchanged(input))),
183
});
184
}),
185
).pipe(Layer.provide(layerTestEventSink));
@@ -619,6 +632,7 @@ function layerTest(input: {
632
readonly flakyReleaseWrites?: FlakyReleaseWrites;
633
readonly pauseAttachWrite?: Parameters<typeof layerPausingAttachEventSink>[0];
634
readonly beforeUnchangedWrite?: Ref.Ref<Effect.Effect<void>>;
635
+ readonly beforeTurnItemsWrite?: Ref.Ref<ReadonlyArray<Effect.Effect<void>>>;
636
readonly pauseResolve?: {
637
readonly armed: Ref.Ref<boolean>;
638
readonly paused: Deferred.Deferred<void>;
@@ -644,7 +658,10 @@ function layerTest(input: {
658
: input.pauseAttachWrite !== undefined
659
? layerPausingAttachEventSink(input.pauseAttachWrite)
660
: input.beforeUnchangedWrite !== undefined
647
- ? layerBeforeUnchangedWriteEventSink(input.beforeUnchangedWrite)
661
+ ? layerBeforeUnchangedWriteEventSink(
662
+ input.beforeUnchangedWrite,
663
+ input.beforeTurnItemsWrite,
664
+ )
665
: input.failReleaseEventWrites
666
? layerFailingReleaseEventSink
667
: layerTestEventSink;
@@ -3171,7 +3188,75 @@ const openSessionWithBackgroundCommand = (fixtureName: string) =>
3188
events: [yield* makeThreadCreatedEvent({ idAllocator, threadId, now })],
3189
});
3190
yield* Effect.forEach(providerThreads, writeProviderThread);
3191
+ const toolCall = (
3192
+ nativeItemId: string,
3193
+ providerThread: OrchestrationV2ProviderThread,
3194
+ status: "running" | "completed",
3195
+ startedAt: DateTime.Utc,
3196
+ ) =>
3197
+ ({
3198
+ id: idAllocator.derive.turnItemFromProviderItem({ driver: CODEX_DRIVER, nativeItemId }),
3199
+ threadId,
3200
+ runId: null,
3201
+ nodeId: null,
3202
+ providerThreadId: providerThread.id,
3203
+ providerTurnId: null,
3204
+ nativeItemRef: null,
3205
+ parentItemId: null,
3206
+ ordinal: 1,
3207
+ status,
3208
+ title: null,
3209
+ startedAt,
3210
+ completedAt: status === "completed" ? startedAt : null,
3211
+ updatedAt: startedAt,
3212
+ type: "dynamic_tool",
3213
+ toolName: "mcp__t3-code__t3_worktree_handoff",
3214
+ input: {},
3215
+ }) satisfies OrchestrationV2TurnItem;
3216
+ const seededToolCalls = {
3217
+ cutOff: toolCall("cut-off", providerThreads[0]!, "running", now),
3218
+ finished: toolCall("finished", providerThreads[0]!, "completed", now),
3219
+ otherSession: toolCall("other-session", providerThreads[1]!, "running", now),
3220
+ // Started by a replacement session after the release.
3221
+ replacement: toolCall(
3222
+ "replacement",
3223
+ providerThreads[0]!,
3224
+ "running",
3225
+ DateTime.add(now, { hours: 1 }),
3226
+ ),
3227
+ };
3228
+ const writeToolCall = (payload: OrchestrationV2TurnItem) =>
3229
+ Effect.gen(function* () {
3230
+ yield* eventSink.write({
3231
+ events: [
3232
+ {
3233
+ id: yield* idAllocator.allocate.event({ threadId }),
3234
+ type: "turn-item.updated",
3235
+ threadId,
3236
+ driver: CODEX_DRIVER,
3237
+ occurredAt: now,
3238
+ payload,
3239
+ },
3240
+ ],
3241
+ });
3242
+ });
3243
+ yield* Effect.forEach(Object.values(seededToolCalls), writeToolCall);
3244
yield* manager.open({ threadId, providerSessionId, modelSelection, runtimePolicy });
3245
+ const toolCallStatus = (id: OrchestrationV2TurnItem["id"]) =>
3246
+ Effect.gen(function* () {
3247
+ const projectionStore = yield* ProjectionStore.ProjectionStoreV2;
3248
+ const projection = yield* projectionStore.getThreadProjection(threadId);
3249
+ return projection.turnItems.find((item) => item.id === id)?.status;
3250
+ });
3251
+ const toolCalls = Effect.gen(function* () {
3252
+ const statusOf = (seeded: OrchestrationV2TurnItem) => toolCallStatus(seeded.id);
3253
+ return {
3254
+ cutOff: yield* statusOf(seededToolCalls.cutOff),
3255
+ finished: yield* statusOf(seededToolCalls.finished),
3256
+ otherSession: yield* statusOf(seededToolCalls.otherSession),
3257
+ replacement: yield* statusOf(seededToolCalls.replacement),
3258
+ };
3259
+ });
3260
const rosters = Effect.gen(function* () {
3261
const projectionStore = yield* ProjectionStore.ProjectionStoreV2;
3262
const projection = yield* projectionStore.getThreadProjection(threadId);
@@ -3182,25 +3267,95 @@ const openSessionWithBackgroundCommand = (fixtureName: string) =>
3267
).map((task) => task.taskId),
3268
);
3269
});
3185
- return { threadId, providerSessionId, providerThreads, writeProviderThread, rosters };
3270
+ return {
3271
+ threadId,
3272
+ providerSessionId,
3273
+ providerThreads,
3274
+ writeProviderThread,
3275
+ rosters,
3276
+ seededToolCalls,
3277
+ writeToolCall,
3278
+ toolCalls,
3279
+ toolCallStatus,
3280
+ };
3281
});
3282
3188
-it.effect(
3189
- "ProviderSessionManagerV2 clears the background roster of a session a detach releases",
3190
- () =>
3283
+// regression: #15993 a worktree handoff left its tool call running, so the thread stayed Waiting
3284
+it.effect.each(["detach", "stream failure", "provider stop", "early close"] as const)(
3285
+ "ProviderSessionManagerV2 clears background work after queued events drain on %s",
3286
+ (release) =>
3287
Effect.gen(function* () {
3288
const state = yield* Ref.make(emptyState);
3289
const effect = Effect.gen(function* () {
3290
const manager = yield* ProviderSessionManager.ProviderSessionManagerV2;
3195
- const { threadId, providerSessionId, rosters } = yield* openSessionWithBackgroundCommand(
3196
- "provider-session-manager-detach-roster",
3291
+ const { threadId, providerSessionId, rosters, toolCalls, seededToolCalls, writeToolCall } =
3292
+ yield* openSessionWithBackgroundCommand("provider-session-manager-detach-roster");
3293
+ const runtime = Option.getOrThrow(yield* manager.get(providerSessionId));
3294
+ const subscription = yield* runtime.subscribeEvents!;
3295
+ const consuming = yield* Deferred.make<void>();
3296
+ const resume = yield* Deferred.make<void>();
3297
+ const consumer = yield* subscription.events.pipe(
3298
+ Stream.tap(() =>
3299
+ Deferred.succeed(consuming, undefined).pipe(
3300
+ Effect.andThen(Deferred.await(resume)),
3301
+ Effect.andThen(writeToolCall(seededToolCalls.cutOff)),
3302
+ ),
3303
+ ),
3304
+ (events) => (release === "early close" ? events.pipe(Stream.take(1)) : events),
3305
+ Stream.runDrain,
3306
+ Effect.exit,
3307
+ Effect.forkScoped,
3308
);
3309
+ const queue = (yield* Ref.get(state)).eventQueues.get(String(providerSessionId))!;
3310
+ yield* Queue.offer(queue, {
3311
+ type: "provider_session.updated",
3312
+ driver: CODEX_DRIVER,
3313
+ providerSession: runtime.providerSession,
3314
+ });
3315
+ yield* Deferred.await(consuming);
3316
assert.deepStrictEqual(yield* rosters, [["dev-server"], ["dev-server"]]);
3317
3200
- yield* manager.detach({ providerSessionId, threadId, detail: "Thread settled." });
3318
+ const eventSink = yield* EventSink.EventSinkV2;
3319
+ const afterSequence = yield* eventSink.latestSequence({ threadId });
3320
+ if (release === "detach" || release === "early close") {
3321
+ yield* manager.detach({ providerSessionId, threadId, detail: "Workspace changed." });
3322
+ } else {
3323
+ if (release === "provider stop") {
3324
+ yield* Queue.offer(queue, {
3325
+ type: "provider_session.updated",
3326
+ driver: CODEX_DRIVER,
3327
+ providerSession: { ...runtime.providerSession, status: "stopped" },
3328
+ });
3329
+ }
3330
+ yield* Queue.end(queue);
3331
+ }
3332
+ const interrupts = (count: number) =>
3333
+ eventSink.stream({ threadId, afterSequence }).pipe(
3334
+ Stream.filter(
3335
+ ({ event }) =>
3336
+ event.type === "turn-item.updated" &&
3337
+ event.payload.id === seededToolCalls.cutOff.id &&
3338
+ event.payload.status === "interrupted",
3339
+ ),
3340
+ Stream.take(count),
3341
+ Stream.runDrain,
3342
+ );
3343
+ yield* interrupts(1);
3344
+ yield* Deferred.succeed(resume, undefined);
3345
+ assert.equal(
3346
+ Exit.isFailure(yield* Fiber.join(consumer)),
3347
+ release !== "provider stop" && release !== "early close",
3348
+ );
3349
+ yield* interrupts(2);
3350
3351
assert.isTrue(Option.isNone(yield* manager.get(providerSessionId)));
3352
assert.deepStrictEqual(yield* rosters, [[], ["dev-server"]]);
3353
+ assert.deepStrictEqual(yield* toolCalls, {
3354
+ cutOff: "interrupted",
3355
+ finished: "completed",
3356
+ otherSession: "running",
3357
+ replacement: "running",
3358
+ });
3359
});
3360
3361
yield* effect.pipe(
@@ -3211,37 +3366,94 @@ it.effect(
3366
}),
3367
);
3368
3214
-it.effect("ProviderSessionManagerV2 keeps the roster of a runtime whose tasks can outlive it", () =>
3215
- Effect.gen(function* () {
3216
- const state = yield* Ref.make(emptyState);
3217
- const effect = Effect.gen(function* () {
3218
- const manager = yield* ProviderSessionManager.ProviderSessionManagerV2;
3219
- const { providerSessionId, rosters } = yield* openSessionWithBackgroundCommand(
3220
- "provider-session-manager-outliving-roster",
3221
- );
3369
+it.effect.each(["a runtime whose tasks can outlive it", "server shutdown"] as const)(
3370
+ "ProviderSessionManagerV2 keeps the background work after the release of %s",
3371
+ (release) =>
3372
+ Effect.gen(function* () {
3373
+ const state = yield* Ref.make(emptyState);
3374
+ const effect = Effect.gen(function* () {
3375
+ const manager = yield* ProviderSessionManager.ProviderSessionManagerV2;
3376
+ const { providerSessionId, rosters, toolCalls } = yield* openSessionWithBackgroundCommand(
3377
+ "provider-session-manager-outliving-roster",
3378
+ );
3379
3223
- yield* manager.close(providerSessionId);
3380
+ yield* release === "server shutdown"
3381
+ ? manager.release({ providerSessionId, reason: "server_shutdown" })
3382
+ : manager.close(providerSessionId);
3383
3225
- assert.deepStrictEqual(yield* rosters, [["dev-server"], ["dev-server"]]);
3226
- });
3384
+ assert.deepStrictEqual(yield* rosters, [["dev-server"], ["dev-server"]]);
3385
+ assert.equal((yield* toolCalls).cutOff, "running");
3386
+ });
3387
3228
- yield* effect.pipe(
3229
- Effect.provide(
3230
- layerTest({ state, idleTimeoutMs: 60_000, backgroundTasksEndWithProcess: false }),
3231
- ),
3232
- );
3233
- }),
3388
+ yield* effect.pipe(
3389
+ Effect.provide(
3390
+ layerTest({
3391
+ state,
3392
+ idleTimeoutMs: 60_000,
3393
+ ...(release === "server shutdown" ? {} : { backgroundTasksEndWithProcess: false }),
3394
+ }),
3395
+ ),
3396
+ );
3397
+ }),
3398
);
3399
3400
// regression: #19355 settled and idle-released threads stayed Waiting
3237
-it.effect("ProviderSessionManagerV2 keeps a provider update that lands while release clears", () =>
3401
+it.effect("ProviderSessionManagerV2 keeps provider updates that land while release clears", () =>
3402
Effect.gen(function* () {
3403
const state = yield* Ref.make(emptyState);
3404
const beforeUnchangedWrite = yield* Ref.make<Effect.Effect<void>>(Effect.void);
3405
+ const beforeTurnItemsWrite = yield* Ref.make<ReadonlyArray<Effect.Effect<void>>>([]);
3406
const effect = Effect.gen(function* () {
3407
const manager = yield* ProviderSessionManager.ProviderSessionManagerV2;
3243
- const { providerSessionId, providerThreads, writeProviderThread, rosters } =
3244
- yield* openSessionWithBackgroundCommand("provider-session-manager-raced-roster");
3408
+ const {
3409
+ providerSessionId,
3410
+ providerThreads,
3411
+ writeProviderThread,
3412
+ rosters,
3413
+ seededToolCalls,
3414
+ writeToolCall,
3415
+ toolCallStatus,
3416
+ } = yield* openSessionWithBackgroundCommand("provider-session-manager-raced-roster");
3417
+ // Before each of the first four cleanup writes, one of these calls completes late. The
3418
+ // completion must win, and the conflicts must not stop the open calls from being interrupted.
3419
+ const lateCompleted = ["late-1", "late-2", "late-3", "late-4"].map((name) => ({
3420
+ ...seededToolCalls.cutOff,
3421
+ id: TurnItemId.make(`turn-item:${name}`),
3422
+ }));
3423
+ // Read after the late calls, so one write for all items would run out of tries first.
3424
+ const lastOpen = { ...seededToolCalls.cutOff, id: TurnItemId.make("turn-item:last-open") };
3425
+ yield* Effect.forEach([...lateCompleted, lastOpen], writeToolCall);
3426
+ const storedLate = {
3427
+ ...seededToolCalls.cutOff,
3428
+ id: TurnItemId.make("turn-item:stored-late"),
3429
+ };
3430
+ const storedInCleanPass = {
3431
+ ...seededToolCalls.cutOff,
3432
+ id: TurnItemId.make("turn-item:stored-in-clean-pass"),
3433
+ };
3434
+ const complete = (item: OrchestrationV2TurnItem) =>
3435
+ writeToolCall({ ...item, status: "completed" });
3436
+ // One hook runs before each cleanup write. The first pass writes the cut-off call, the
3437
+ // four late calls and the last open call; the second pass writes the cut-off call again
3438
+ // and the call stored late; the third pass writes the call stored in the clean pass.
3439
+ yield* Ref.set(
3440
+ beforeTurnItemsWrite,
3441
+ [
3442
+ // The cut-off call gets an update that keeps it open, so it needs a second pass, and
3443
+ // the dead session stores one more call.
3444
+ complete(lateCompleted[0]!).pipe(
3445
+ Effect.andThen(writeToolCall({ ...seededToolCalls.cutOff, title: "late output" })),
3446
+ Effect.andThen(writeToolCall(storedLate)),
3447
+ ),
3448
+ complete(lateCompleted[1]!),
3449
+ complete(lateCompleted[2]!),
3450
+ complete(lateCompleted[3]!),
3451
+ Effect.void,
3452
+ Effect.void,
3453
+ // The second pass has no conflict, but one more call lands during it.
3454
+ writeToolCall(storedInCleanPass),
3455
+ ].map(Effect.orDie),
3456
+ );
3457
const seeded = providerThreads[0]!;
3458
yield* Ref.set(
3459
beforeUnchangedWrite,
@@ -3257,10 +3469,25 @@ it.effect("ProviderSessionManagerV2 keeps a provider update that lands while rel
3469
yield* manager.close(providerSessionId);
3470
3471
assert.deepStrictEqual(yield* rosters, [["replacement-task"], ["dev-server"]]);
3472
+ const statuses = Object.fromEntries(
3473
+ yield* Effect.forEach(
3474
+ [seededToolCalls.cutOff, ...lateCompleted, lastOpen, storedLate, storedInCleanPass],
3475
+ (item) => toolCallStatus(item.id).pipe(Effect.map((status) => [item.id, status])),
3476
+ ),
3477
+ );
3478
+ assert.deepStrictEqual(statuses, {
3479
+ [seededToolCalls.cutOff.id]: "interrupted",
3480
+ ...Object.fromEntries(lateCompleted.map((item) => [item.id, "completed"])),
3481
+ [lastOpen.id]: "interrupted",
3482
+ [storedLate.id]: "interrupted",
3483
+ [storedInCleanPass.id]: "interrupted",
3484
+ });
3485
});
3486
3487
yield* effect.pipe(
3263
- Effect.provide(layerTest({ state, idleTimeoutMs: 60_000, beforeUnchangedWrite })),
3488
+ Effect.provide(
3489
+ layerTest({ state, idleTimeoutMs: 60_000, beforeUnchangedWrite, beforeTurnItemsWrite }),
3490
+ ),
3491
);
3492
}),
3493
);
apps/server/src/orchestration-v2/ProviderSessionManager.ts
+174
-70
@@ -121,6 +121,17 @@ export const ProviderSessionReleaseReason = Schema.Literals([
121
]);
122
export type ProviderSessionReleaseReason = typeof ProviderSessionReleaseReason.Type;
123
124
+// Tool calls that only a live provider process can end.
125
+const releasedToolCallTypes: ReadonlyArray<OrchestrationV2TurnItem["type"]> = [
126
+ "command_execution",
127
+ "dynamic_tool",
128
+];
129
+const activeTurnItemStatuses: ReadonlyArray<OrchestrationV2TurnItem["status"]> = [
130
+ "pending",
131
+ "running",
132
+ "waiting",
133
+];
134
+
135
export class ProviderSessionOpenError extends Schema.TaggedError<ProviderSessionOpenError>()(
136
"ProviderSessionOpenError",
137
{
@@ -606,6 +617,11 @@ export const layerWithOptions = (
617
),
618
);
619
620
+ const subscriberReleaseCleanup = new WeakMap<
621
+ Queue.Queue<ProviderSessionEventSignal, Cause.Done>,
622
+ Effect.Effect<void>
623
+ >();
624
+
625
const publishToSubscribers = (
626
subscribers: Ref.Ref<
627
ReadonlyMap<number, Queue.Queue<ProviderSessionEventSignal, Cause.Done>>
@@ -623,6 +639,8 @@ export const layerWithOptions = (
639
const failSubscribers = Effect.fn("ProviderSessionManager.failSubscribers")(function* (
640
entry: LiveSessionEntry,
641
detail: string,
642
+ afterDrain: Effect.Effect<void>,
643
+ cause?: Cause.Cause<ProviderAdapter.ProviderAdapterV2Error>,
644
) {
645
const error = new ProviderAdapter.ProviderAdapterEventStreamError({
646
driver: entry.runtime.driver,
@@ -632,11 +650,13 @@ export const layerWithOptions = (
650
const subscribers = yield* Ref.getAndSet(entry.eventSubscribers, new Map());
651
yield* Effect.forEach(
652
subscribers.values(),
635
- (queue) =>
636
- Queue.offer(queue, {
653
+ (queue) => {
654
+ subscriberReleaseCleanup.set(queue, afterDrain);
655
+ return Queue.offer(queue, {
656
type: "failure",
638
- cause: Cause.fail(error),
639
- }),
657
+ cause: cause ?? Cause.fail(error),
658
+ });
659
+ },
660
{ discard: true },
661
);
662
});
@@ -659,11 +679,17 @@ export const layerWithOptions = (
679
// must let consumers drain them before the stream completes.
680
const endSubscribers = Effect.fn("ProviderSessionManager.endSubscribers")(function* (
681
entry: LiveSessionEntry,
682
+ afterDrain: Effect.Effect<void>,
683
) {
684
const subscribers = yield* Ref.getAndSet(entry.eventSubscribers, new Map());
664
- yield* Effect.forEach(subscribers.values(), (queue) => Queue.end(queue), {
665
- discard: true,
666
- });
685
+ yield* Effect.forEach(
686
+ subscribers.values(),
687
+ (queue) => {
688
+ subscriberReleaseCleanup.set(queue, afterDrain);
689
+ return Queue.end(queue);
690
+ },
691
+ { discard: true },
692
+ );
693
});
694
695
const cancelIdleFiber = (fiber: Fiber.Fiber<void, never> | null) =>
@@ -854,10 +880,11 @@ export const layerWithOptions = (
880
}
881
});
882
857
- // Read tasks as soon as the session leaves the live map, before a replacement reports its own.
858
- // Keep OpenCode's roster: its background subagents share a server and can outlive the session.
859
- const readReleasedBackgroundTasks = Effect.fn(
860
- "ProviderSessionManager.readReleasedBackgroundTasks",
883
+ // Read the session's provider threads as soon as it leaves the live map, before a
884
+ // replacement reports its own tasks. Keep OpenCode's background work: its background
885
+ // subagents share a server and can outlive the session.
886
+ const readReleasedProviderThreads = Effect.fn(
887
+ "ProviderSessionManager.readReleasedProviderThreads",
888
)(
889
function* (input: {
890
readonly entry: LiveSessionEntry;
@@ -877,15 +904,15 @@ export const layerWithOptions = (
904
"providerThreads",
905
]);
906
for (const providerThread of projection.providerThreads) {
880
- const tasks = providerThread.pendingBackgroundTasks ?? [];
881
- if (providerThread.providerSessionId !== providerSessionId || tasks.length === 0) {
882
- continue;
883
- }
907
+ if (providerThread.providerSessionId !== providerSessionId) continue;
908
released.push({
909
threadId,
910
providerSessionId,
911
providerThreadId: providerThread.id,
888
- taskIds: new Set(tasks.map((task) => task.taskId)),
912
+ driver: providerThread.driver,
913
+ taskIds: new Set(
914
+ (providerThread.pendingBackgroundTasks ?? []).map((task) => task.taskId),
915
+ ),
916
});
917
}
918
}
@@ -901,57 +928,119 @@ export const layerWithOptions = (
928
),
929
),
930
);
904
- type ReleasedBackgroundTasks = Effect.Success<
905
- ReturnType<typeof readReleasedBackgroundTasks>
931
+ type ReleasedProviderThread = Effect.Success<
932
+ ReturnType<typeof readReleasedProviderThreads>
933
>[number];
934
935
// Dead processes cannot report task endings; stale rosters keep threads Waiting.
936
+ const writeReleasedRosterEvents = Effect.fn(
937
+ "ProviderSessionManager.writeReleasedRosterEvents",
938
+ )(function* ({
939
+ threadId,
940
+ providerSessionId,
941
+ providerThreadId,
942
+ taskIds,
943
+ }: ReleasedProviderThread) {
944
+ if (taskIds.size === 0) return;
945
+ for (let attempt = 0; attempt < 3; attempt++) {
946
+ const projection = yield* projectionStore.getThreadRecords(threadId, ["providerThreads"]);
947
+ const providerThread = projection.providerThreads.find(
948
+ (candidate) => candidate.id === providerThreadId,
949
+ );
950
+ const tasks = providerThread?.pendingBackgroundTasks ?? [];
951
+ const remaining = tasks.filter((task) => !taskIds.has(task.taskId));
952
+ if (providerThread === undefined || remaining.length === tasks.length) return;
953
+ const now = yield* DateTime.now;
954
+ const { committed } = yield* eventSink.writeIfProviderThreadUnchanged({
955
+ expected: providerThread,
956
+ events: [
957
+ {
958
+ id: yield* idAllocator.allocate.event({ threadId, providerSessionId }),
959
+ type: "provider-thread.updated",
960
+ threadId,
961
+ driver: providerThread.driver,
962
+ providerInstanceId: providerThread.providerInstanceId,
963
+ occurredAt: now,
964
+ payload: {
965
+ ...providerThread,
966
+ pendingBackgroundTasks: remaining,
967
+ updatedAt: now,
968
+ },
969
+ },
970
+ ],
971
+ });
972
+ if (committed) return;
973
+ }
974
+ yield* Effect.logWarning("orchestration-v2.provider-session-release-roster-conflict", {
975
+ providerSessionId,
976
+ providerThreadId,
977
+ });
978
+ });
979
+
980
+ // Tool calls the dead process started never report an end either, and run finalization
981
+ // keeps them open for a late result. Items started after the release belong to a
982
+ // replacement session. Subagent rows are left out: run finalization ends run-owned ones.
983
+ const writeReleasedToolCallEvents = Effect.fn(
984
+ "ProviderSessionManager.writeReleasedToolCallEvents",
985
+ )(function* (released: ReleasedProviderThread, releasedAt: DateTime.Utc) {
986
+ const { threadId, providerSessionId, providerThreadId, driver } = released;
987
+ const readCutOff = projectionStore
988
+ .getThreadRecords(threadId, ["turnItems"], {
989
+ turnItemTypes: releasedToolCallTypes,
990
+ turnItemStatuses: activeTurnItemStatuses,
991
+ })
992
+ .pipe(
993
+ Effect.map((projection) =>
994
+ projection.turnItems.filter(
995
+ (item) =>
996
+ item.providerThreadId === providerThreadId &&
997
+ DateTime.isLessThanOrEqualTo(item.startedAt ?? item.updatedAt, releasedAt),
998
+ ),
999
+ ),
1000
+ );
1001
+ // One write per item: a late completion from the dead session's queued events rejects
1002
+ // only the interrupt of its own item, and that item then is no longer open. Read again
1003
+ // after each pass: queued events can also store more cut-off items.
1004
+ let pending = yield* readCutOff;
1005
+ for (let pass = 0; pass < 3 && pending.length > 0; pass++) {
1006
+ for (const item of pending) {
1007
+ const now = yield* DateTime.now;
1008
+ yield* eventSink.writeIfTurnItemUnchanged({
1009
+ expected: item,
1010
+ events: [
1011
+ {
1012
+ id: yield* idAllocator.allocate.event({ threadId, providerSessionId }),
1013
+ type: "turn-item.updated",
1014
+ threadId,
1015
+ ...(item.runId === null ? {} : { runId: item.runId }),
1016
+ ...(item.nodeId === null ? {} : { nodeId: item.nodeId }),
1017
+ driver,
1018
+ occurredAt: now,
1019
+ payload: { ...item, status: "interrupted", completedAt: now, updatedAt: now },
1020
+ },
1021
+ ],
1022
+ });
1023
+ }
1024
+ pending = yield* readCutOff;
1025
+ }
1026
+ if (pending.length === 0) return;
1027
+ yield* Effect.logWarning("orchestration-v2.provider-session-release-tool-call-conflict", {
1028
+ providerSessionId,
1029
+ providerThreadId,
1030
+ openToolCalls: pending.length,
1031
+ });
1032
+ });
1033
+
1034
const writeReleasedBackgroundWorkEvents = (
910
- released: ReadonlyArray<ReleasedBackgroundTasks>,
1035
+ released: ReadonlyArray<ReleasedProviderThread>,
1036
+ releasedAt: DateTime.Utc,
1037
) =>
1038
Effect.forEach(
1039
released,
914
- ({ threadId, providerSessionId, providerThreadId, taskIds }) =>
915
- Effect.gen(function* () {
916
- for (let attempt = 0; attempt < 3; attempt++) {
917
- const projection = yield* projectionStore.getThreadRecords(threadId, [
918
- "providerThreads",
919
- ]);
920
- const providerThread = projection.providerThreads.find(
921
- (candidate) => candidate.id === providerThreadId,
922
- );
923
- const tasks = providerThread?.pendingBackgroundTasks ?? [];
924
- const remaining = tasks.filter((task) => !taskIds.has(task.taskId));
925
- if (providerThread === undefined || remaining.length === tasks.length) return;
926
- const now = yield* DateTime.now;
927
- const { committed } = yield* eventSink.writeIfProviderThreadUnchanged({
928
- expected: providerThread,
929
- events: [
930
- {
931
- id: yield* idAllocator.allocate.event({ threadId, providerSessionId }),
932
- type: "provider-thread.updated",
933
- threadId,
934
- driver: providerThread.driver,
935
- providerInstanceId: providerThread.providerInstanceId,
936
- occurredAt: now,
937
- payload: {
938
- ...providerThread,
939
- pendingBackgroundTasks: remaining,
940
- updatedAt: now,
941
- },
942
- },
943
- ],
944
- });
945
- if (committed) return;
946
- }
947
- yield* Effect.logWarning(
948
- "orchestration-v2.provider-session-release-roster-conflict",
949
- {
950
- providerSessionId,
951
- providerThreadId,
952
- },
953
- );
954
- }),
1040
+ (providerThread) =>
1041
+ writeReleasedRosterEvents(providerThread).pipe(
1042
+ Effect.andThen(writeReleasedToolCallEvents(providerThread, releasedAt)),
1043
+ ),
1044
{ discard: true },
1045
);
1046
@@ -959,7 +1048,7 @@ export const layerWithOptions = (
1048
readonly entry: LiveSessionEntry;
1049
readonly reason: ProviderSessionReleaseReason;
1050
readonly detail?: string;
962
- readonly releasedBackgroundTasks: ReadonlyArray<ReleasedBackgroundTasks>;
1051
+ readonly releasedProviderThreads: ReadonlyArray<ReleasedProviderThread>;
1052
readonly releasedAt: DateTime.Utc;
1053
readonly detachedThreadId?: ThreadId;
1054
readonly replaced: boolean;
@@ -974,7 +1063,9 @@ export const layerWithOptions = (
1063
input.entry.requestEventPermit.withPermits(1),
1064
),
1065
),
977
- Effect.exit(writeReleasedBackgroundWorkEvents(input.releasedBackgroundTasks)),
1066
+ Effect.exit(
1067
+ writeReleasedBackgroundWorkEvents(input.releasedProviderThreads, input.releasedAt),
1068
+ ),
1069
],
1070
{ concurrency: 1 },
1071
).pipe(Effect.flatMap(Exit.asVoidAll));
@@ -1130,6 +1221,7 @@ export const layerWithOptions = (
1221
readonly keepIfBusyOtherThan?: ReadonlyArray<ProviderThreadId>;
1222
readonly keepIf?: Effect.Effect<boolean>;
1223
readonly gracefulSubscribers?: boolean;
1224
+ readonly eventStreamCause?: Cause.Cause<ProviderAdapter.ProviderAdapterV2Error>;
1225
}) =>
1226
Effect.acquireUseRelease(
1227
removeLiveEntry(input),
@@ -1142,7 +1234,7 @@ export const layerWithOptions = (
1234
input.detachedThreadId === undefined
1235
? {}
1236
: { detachedThreadId: input.detachedThreadId };
1145
- const releasedBackgroundTasks = yield* readReleasedBackgroundTasks({
1237
+ const releasedProviderThreads = yield* readReleasedProviderThreads({
1238
entry,
1239
reason: input.reason,
1240
...detachedThread,
@@ -1150,14 +1242,22 @@ export const layerWithOptions = (
1242
if (input.cancelIdleFiber !== false) {
1243
yield* cancelIdleFiber(entry.idleFiber);
1244
}
1245
+ // Queued events can persist tool calls after the release-time scan.
1246
+ const afterDrain = Effect.forEach(
1247
+ releasedProviderThreads,
1248
+ (thread) => writeReleasedToolCallEvents(thread, releasedAt),
1249
+ { discard: true },
1250
+ ).pipe(logReleaseFailure(input.providerSessionId));
1251
if (input.gracefulSubscribers === true) {
1154
- yield* endSubscribers(entry);
1252
+ yield* endSubscribers(entry, afterDrain);
1253
} else if (input.reason === "server_shutdown") {
1254
yield* interruptSubscribers(entry);
1255
} else {
1256
yield* failSubscribers(
1257
entry,
1258
input.detail ?? `Provider session released: ${input.reason}.`,
1259
+ afterDrain,
1260
+ input.eventStreamCause,
1261
);
1262
}
1263
const closeExit = yield* closeScopeWithin(entry.scope, {
@@ -1168,7 +1268,7 @@ export const layerWithOptions = (
1268
entry,
1269
reason: input.reason,
1270
...(input.detail === undefined ? {} : { detail: input.detail }),
1171
- releasedBackgroundTasks,
1271
+ releasedProviderThreads,
1272
releasedAt,
1273
...detachedThread,
1274
};
@@ -2045,6 +2145,14 @@ export const layerWithOptions = (
2145
? Queue.clear(queue).pipe(Effect.andThen(Queue.end(queue)), Effect.asVoid)
2146
: Effect.void,
2147
),
2148
+ // Detached so the consumer's run failure is not ordered behind release cleanup.
2149
+ Effect.andThen(
2150
+ Effect.suspend(() => {
2151
+ const cleanup = subscriberReleaseCleanup.get(queue);
2152
+ subscriberReleaseCleanup.delete(queue);
2153
+ return cleanup === undefined ? Effect.void : Effect.forkDetach(cleanup);
2154
+ }),
2155
+ ),
2156
);
2157
const events = Stream.fromQueue(queue).pipe(
2158
Stream.mapEffect((signal) =>
@@ -2663,11 +2771,6 @@ export const layerWithOptions = (
2771
cause: "Provider event stream ended unexpectedly.",
2772
}),
2773
);
2666
- yield* publishToSubscribers(entry.eventSubscribers, {
2667
- type: "failure",
2668
- cause,
2669
- });
2670
- yield* Ref.set(entry.eventSubscribers, new Map());
2774
// lastError is the thread banner, so it gets the same bounded
2775
// message as a failed turn; the raw cause survives only in the log.
2776
yield* Effect.logWarning("orchestration-v2.provider-session-runtime-failed", {
@@ -2678,6 +2781,7 @@ export const layerWithOptions = (
2781
providerSessionId: entry.runtime.providerSessionId,
2782
reason: "runtime_error",
2783
detail: makeProviderFailure({ cause }).message,
2784
+ eventStreamCause: cause,
2785
}).pipe(logReleaseFailure(entry.runtime.providerSessionId));
2786
}),
2787
),
apps/server/src/orchestration-v2/testkit/ProviderSwitch.integration.test.ts
+170
-3
@@ -8,6 +8,7 @@ import {
8
MessageId,
9
type ModelSelection,
10
type OrchestrationV2Command,
11
+ type OrchestrationV2DomainEvent,
12
type OrchestrationV2ProviderCapabilities,
13
type OrchestrationV2ProviderSession,
14
type OrchestrationV2ProviderThread,
@@ -23,6 +24,7 @@ import {
24
TurnItemId,
25
ProviderDriverKind,
26
} from "@t3tools/contracts";
27
+import { derivePendingBackgroundWork } from "@t3tools/shared/orchestrationV2PendingBackgroundWork";
28
import * as DateTime from "effect/DateTime";
29
import * as Deferred from "effect/Deferred";
30
import * as Effect from "effect/Effect";
@@ -121,6 +123,12 @@ function makeTestAdapter(input: {
123
readonly subagentOnlyBackgroundWork?: boolean;
124
readonly holdFirstTurn?: Deferred.Deferred<void>;
125
readonly releaseFirstTurn?: Deferred.Deferred<void>;
126
+ /** A tool call the held turn reports running before it holds. */
127
+ readonly heldRunningToolCall?: {
128
+ readonly id: TurnItemId;
129
+ readonly toolName: string;
130
+ };
131
+ readonly backgroundTasksEndWithProcess?: true;
132
readonly backgroundWork?: Ref.Ref<boolean>;
133
readonly closedSessions?: Ref.Ref<ReadonlyArray<string>>;
134
}): ProviderAdapter.ProviderAdapterV2["Service"] {
@@ -158,6 +166,9 @@ function makeTestAdapter(input: {
166
providerSessionId: sessionInput.providerSessionId,
167
providerSession,
168
events: Stream.fromPubSub(events),
169
+ ...(input.backgroundTasksEndWithProcess === undefined
170
+ ? {}
171
+ : { backgroundTasksEndWithProcess: input.backgroundTasksEndWithProcess }),
172
...(input.pendingBackgroundWork === undefined
173
? {}
174
: {
@@ -281,18 +292,59 @@ function makeTestAdapter(input: {
292
: { nativeThreadHasTurns: turnInput.nativeThreadHasTurns }),
293
},
294
]);
295
+ const providerTurnId = ProviderTurnId.make(
296
+ `provider-turn:${input.driver}:${turnInput.threadId}:${turnInput.runOrdinal}`,
297
+ );
298
if (
299
turnInput.runOrdinal === (input.holdRunOrdinal ?? 1) &&
300
input.holdFirstTurn !== undefined
301
) {
302
+ if (input.heldRunningToolCall !== undefined) {
303
+ const startedAt = yield* DateTime.now;
304
+ yield* PubSub.publish(events, {
305
+ type: "provider_turn.updated",
306
+ driver: input.driver,
307
+ providerTurn: {
308
+ id: providerTurnId,
309
+ providerThreadId: turnInput.providerThread.id,
310
+ nodeId: turnInput.rootNodeId,
311
+ runAttemptId: turnInput.attemptId,
312
+ nativeTurnRef: null,
313
+ ordinal: turnInput.runOrdinal,
314
+ status: "running",
315
+ startedAt,
316
+ completedAt: null,
317
+ },
318
+ });
319
+ yield* PubSub.publish(events, {
320
+ type: "turn_item.updated",
321
+ driver: input.driver,
322
+ turnItem: {
323
+ id: input.heldRunningToolCall.id,
324
+ threadId: turnInput.threadId,
325
+ runId: turnInput.runId,
326
+ nodeId: turnInput.rootNodeId,
327
+ providerThreadId: turnInput.providerThread.id,
328
+ providerTurnId,
329
+ nativeItemRef: null,
330
+ parentItemId: null,
331
+ ordinal: turnInput.runOrdinal * 100 + 1,
332
+ status: "running",
333
+ title: null,
334
+ startedAt,
335
+ completedAt: null,
336
+ updatedAt: startedAt,
337
+ type: "dynamic_tool",
338
+ toolName: input.heldRunningToolCall.toolName,
339
+ input: {},
340
+ },
341
+ });
342
+ }
343
yield* Deferred.succeed(input.holdFirstTurn, undefined);
344
if (input.releaseFirstTurn === undefined) return;
345
yield* Deferred.await(input.releaseFirstTurn);
346
}
347
const eventTime = yield* DateTime.now;
293
- const providerTurnId = ProviderTurnId.make(
294
- `provider-turn:${input.driver}:${turnInput.threadId}:${turnInput.runOrdinal}`,
295
- );
348
const terminalStatus = input.failedRunOrdinals?.has(turnInput.runOrdinal)
349
? "failed"
350
: input.interruptedRunOrdinals?.has(turnInput.runOrdinal)
@@ -1896,6 +1948,121 @@ describe("orchestration v2 provider switching", () => {
1948
),
1949
);
1950
1951
+ // regression: #15993 t3_worktree_handoff releases the Claude session that runs it. Nothing
1952
+ // was left to report how the call ended, so it stayed running and the thread stayed Waiting.
1953
+ it.live("interrupts the handoff tool call that releases its own session", () =>
1954
+ Effect.scoped(
1955
+ Effect.gen(function* () {
1956
+ const key = "handoff-cuts-off-tool-call";
1957
+ const worktreePath = yield* checkpointWorkspace(key);
1958
+ const capturedTurns = yield* Ref.make<ReadonlyArray<CapturedTurn>>([]);
1959
+ const started = yield* Deferred.make<void>();
1960
+ const toolCallId = TurnItemId.make(`turn-item:${key}`);
1961
+ const scenarioThreadId = ThreadId.make(`thread:${key}`);
1962
+ const layerRegistry = ProviderAdapterRegistry.layerFromAdapters([
1963
+ makeTestAdapter({
1964
+ instanceId: CLAUDE_MODEL_SELECTION.instanceId,
1965
+ driver: CLAUDE_DRIVER,
1966
+ capabilities: ClaudeProviderCapabilitiesV2,
1967
+ modelSelection: CLAUDE_MODEL_SELECTION,
1968
+ responseByRunOrdinal: {},
1969
+ capturedTurns,
1970
+ holdFirstTurn: started,
1971
+ heldRunningToolCall: {
1972
+ id: toolCallId,
1973
+ toolName: "mcp__t3-code__t3_worktree_handoff",
1974
+ },
1975
+ backgroundTasksEndWithProcess: true,
1976
+ }),
1977
+ makeTestAdapter({
1978
+ instanceId: CODEX_MODEL_SELECTION.instanceId,
1979
+ driver: CODEX_DRIVER,
1980
+ capabilities: CodexProviderCapabilitiesV2,
1981
+ modelSelection: CODEX_MODEL_SELECTION,
1982
+ responseByRunOrdinal: {},
1983
+ capturedTurns,
1984
+ }),
1985
+ ]);
1986
+ yield* Effect.gen(function* () {
1987
+ const orchestrator = yield* Orchestrator.OrchestratorV2;
1988
+ const worker = yield* EffectWorker.OrchestrationEffectWorkerV2;
1989
+ const awaitEvent = (predicate: (event: OrchestrationV2DomainEvent) => boolean) =>
1990
+ orchestrator.streamDomainEvents.pipe(
1991
+ Stream.filter(predicate),
1992
+ Stream.take(1),
1993
+ Stream.runDrain,
1994
+ Effect.forkScoped,
1995
+ );
1996
+ yield* orchestrator.dispatch({
1997
+ type: "thread.create",
1998
+ createdBy: "user",
1999
+ creationSource: "web",
2000
+ commandId: CommandId.make(`command:${key}:create`),
2001
+ threadId: scenarioThreadId,
2002
+ projectId: ProjectId.make(`project:${key}`),
2003
+ title: "Tool call releases its session",
2004
+ modelSelection: CLAUDE_MODEL_SELECTION,
2005
+ runtimeMode: "full-access",
2006
+ interactionMode: "default",
2007
+ branch: null,
2008
+ worktreePath: null,
2009
+ });
2010
+ const toolCallRunning = yield* awaitEvent(
2011
+ (event) =>
2012
+ event.type === "turn-item.updated" &&
2013
+ event.payload.id === toolCallId &&
2014
+ event.payload.status === "running",
2015
+ );
2016
+ yield* orchestrator.dispatch({
2017
+ type: "message.dispatch",
2018
+ createdBy: "user",
2019
+ creationSource: "web",
2020
+ commandId: CommandId.make(`command:${key}:message`),
2021
+ threadId: scenarioThreadId,
2022
+ messageId: MessageId.make(`message:${key}`),
2023
+ text: "Release this session",
2024
+ attachments: [],
2025
+ modelSelection: CLAUDE_MODEL_SELECTION,
2026
+ dispatchMode: { type: "start_immediately" },
2027
+ });
2028
+ yield* Deferred.await(started);
2029
+ yield* Fiber.join(toolCallRunning);
2030
+
2031
+ const runFailed = yield* awaitEvent(
2032
+ (event) => event.type === "run.updated" && event.payload.status === "failed",
2033
+ );
2034
+ yield* orchestrator.dispatch({
2035
+ type: "thread.metadata.update",
2036
+ commandId: CommandId.make(`command:${key}:release`),
2037
+ threadId: scenarioThreadId,
2038
+ branch: `t3/${key}`,
2039
+ worktreePath,
2040
+ expectedWorktreePath: null,
2041
+ });
2042
+ yield* worker.drain();
2043
+ yield* Fiber.join(runFailed);
2044
+
2045
+ const projection = yield* orchestrator.getThreadProjection(scenarioThreadId);
2046
+ const toolCall = projection.turnItems.find((item) => item.id === toolCallId);
2047
+ assert.equal(toolCall?.status, "interrupted");
2048
+ assert.isNotNull(toolCall?.completedAt);
2049
+ assert.deepEqual(
2050
+ derivePendingBackgroundWork({
2051
+ latestRun: projection.runs.at(-1),
2052
+ providerThreads: projection.providerThreads,
2053
+ turnItems: projection.turnItems,
2054
+ activeProviderThreadId: projection.thread.activeProviderThreadId,
2055
+ runs: projection.runs,
2056
+ }),
2057
+ [],
2058
+ );
2059
+ }).pipe(
2060
+ Effect.provide(ProviderReplayHarness.layerWithRegistry({ name: key }, layerRegistry)),
2061
+ );
2062
+ }),
2063
+ ),
2064
+ );
2065
+
2066
it.live("checks the queued provider's capability while the current provider stays running", () =>
2067
Effect.scoped(
2068
Effect.gen(function* () {
packages/provider-core/src/server/ProviderAdapter.ts
+5
-1
@@ -672,7 +672,11 @@ export interface ProviderAdapterV2SessionRuntime {
672
providerThread: OrchestrationV2ProviderThread,
673
options?: { readonly includeSubagents?: boolean },
674
) => Effect.Effect<boolean>;
675
- /** Set only if all session background tasks end with its process; otherwise threads must keep Waiting. */
675
+ /**
676
+ * Set only if all session background tasks and tool calls end with its process. Releasing the
677
+ * session then clears its task roster and interrupts its open tool calls; otherwise threads
678
+ * must keep Waiting.
679
+ */
680
readonly backgroundTasksEndWithProcess?: true;
681
readonly getModelContextWindow?: (
682
modelSelection: ModelSelection,