The best way to control Claude Code, Codex and more from any computer or phone
Mirrored from https://github.com/Camo67/t3code.git
Synced Nov 22, 05:45 UTC
fix(server): delegated children keep replies after their parent settles (#23456)
Co-authored-by: Claude Opus 5.5 <noreply@anthropic.com>
Julius Marminge committed
Nov 2, 2026 at 18:32 UTC
0ad3dc18644da1701e3932c59b0e3607d45b8629
2 files changed
+161
-31
apps/server/src/orchestration-v2/ProviderSessionManager.test.ts
+130
@@ -313,6 +313,102 @@ function makeProviderThread(input: {
313
};
314
}
315
316
+// A Codex native subagent's provider thread is owned by its child app thread
317
+// but reachable from the parent's projection through the subagent record.
318
+const makeNativeChildFixture = Effect.fnUntraced(function* () {
319
+ const idAllocator = yield* IdAllocator.IdAllocatorV2;
320
+ const eventSink = yield* EventSink.EventSinkV2;
321
+ const manager = yield* ProviderSessionManager.ProviderSessionManagerV2;
322
+ const now = yield* DateTime.now;
323
+ const parentThreadId = ThreadId.make("native-parent");
324
+ const childThreadId = ThreadId.make("native-child");
325
+ const providerSessionId = idAllocator.derive.providerSession({
326
+ providerInstanceId: modelSelection.instanceId,
327
+ });
328
+ const childThreadEvent = yield* makeThreadCreatedEvent({
329
+ idAllocator,
330
+ threadId: childThreadId,
331
+ now,
332
+ });
333
+ const childProviderThread = makeProviderThread({
334
+ idAllocator,
335
+ threadId: childThreadId,
336
+ providerSessionId,
337
+ nativeThreadId: "native-child-thread",
338
+ now,
339
+ });
340
+ const parentRunId = idAllocator.derive.run({ threadId: parentThreadId, ordinal: 1 });
341
+ yield* eventSink.write({
342
+ events: [
343
+ yield* makeThreadCreatedEvent({ idAllocator, threadId: parentThreadId, now }),
344
+ {
345
+ ...childThreadEvent,
346
+ payload: {
347
+ ...childThreadEvent.payload,
348
+ createdBy: "agent",
349
+ creationSource: "provider",
350
+ activeProviderThreadId: null,
351
+ lineage: {
352
+ parentThreadId,
353
+ relationshipToParent: "subagent",
354
+ rootThreadId: parentThreadId,
355
+ },
356
+ },
357
+ },
358
+ {
359
+ id: yield* idAllocator.allocate.event({ threadId: childThreadId }),
360
+ type: "provider-thread.updated",
361
+ threadId: childThreadId,
362
+ occurredAt: now,
363
+ payload: childProviderThread,
364
+ },
365
+ {
366
+ id: yield* idAllocator.allocate.event({ threadId: parentThreadId }),
367
+ type: "subagent.updated",
368
+ threadId: parentThreadId,
369
+ occurredAt: now,
370
+ payload: {
371
+ id: idAllocator.derive.nodeFromProviderItem({
372
+ driver: CODEX_DRIVER,
373
+ nativeItemId: "native-child-task",
374
+ }),
375
+ threadId: parentThreadId,
376
+ runId: parentRunId,
377
+ parentNodeId: idAllocator.derive.rootNode({ runId: parentRunId }),
378
+ origin: "provider_native",
379
+ createdBy: "agent",
380
+ driver: CODEX_DRIVER,
381
+ providerInstanceId: modelSelection.instanceId,
382
+ providerThreadId: childProviderThread.id,
383
+ childThreadId,
384
+ nativeTaskRef: {
385
+ driver: CODEX_DRIVER,
386
+ nativeId: "native-child-task",
387
+ strength: "strong",
388
+ },
389
+ prompt: "Reply READY",
390
+ title: "Native child",
391
+ model: null,
392
+ status: "completed",
393
+ result: "READY",
394
+ startedAt: now,
395
+ completedAt: now,
396
+ updatedAt: now,
397
+ },
398
+ },
399
+ ],
400
+ });
401
+ const parent = { threadId: parentThreadId, providerSessionId };
402
+ const runtime = yield* manager.open({ ...parent, modelSelection, runtimePolicy });
403
+ const resumeChild = runtime.resumeThread({
404
+ providerThread: childProviderThread,
405
+ threadId: childThreadId,
406
+ modelSelection,
407
+ runtimePolicy,
408
+ });
409
+ return { manager, parent, resumeChild };
410
+});
411
+
412
function unimplemented(detail: string) {
413
return Effect.fail(
414
new ProviderAdapter.ProviderAdapterProtocolError({
@@ -3533,6 +3629,40 @@ it.effect(
3629
}),
3630
);
3631
3632
+it.effect(
3633
+ "ProviderSessionManagerV2 keeps an attached native child loaded when its parent detaches",
3634
+ () =>
3635
+ Effect.gen(function* () {
3636
+ const state = yield* Ref.make(emptyState);
3637
+ yield* Effect.gen(function* () {
3638
+ const { manager, parent, resumeChild } = yield* makeNativeChildFixture();
3639
+ yield* resumeChild;
3640
+ yield* manager.detach(parent);
3641
+
3642
+ assert.deepEqual((yield* Ref.get(state)).unloadedNativeThreadIds, []);
3643
+ // The child's next turn trusts the manager's loaded cache, so the
3644
+ // native thread must still be loaded for that turn to be heard.
3645
+ yield* resumeChild;
3646
+ assert.equal((yield* Ref.get(state)).resumeCount, 1);
3647
+ assert.isTrue(yield* manager.isLive(parent.providerSessionId));
3648
+ }).pipe(Effect.provide(layerTest({ state, idleTimeoutMs: 1000 })), Effect.scoped);
3649
+ }),
3650
+);
3651
+
3652
+it.effect(
3653
+ "ProviderSessionManagerV2 unloads an unattached native child when its parent detaches",
3654
+ () =>
3655
+ Effect.gen(function* () {
3656
+ const state = yield* Ref.make(emptyState);
3657
+ yield* Effect.gen(function* () {
3658
+ const { manager, parent } = yield* makeNativeChildFixture();
3659
+ yield* manager.detach(parent);
3660
+
3661
+ assert.deepEqual((yield* Ref.get(state)).unloadedNativeThreadIds, ["native-child-thread"]);
3662
+ }).pipe(Effect.provide(layerTest({ state, idleTimeoutMs: 1000 })), Effect.scoped);
3663
+ }),
3664
+);
3665
+
3666
it.effect(
3667
"ProviderSessionManagerV2 re-attaching a thread waits for its in-flight unload, then reloads it",
3668
() =>
apps/server/src/orchestration-v2/ProviderSessionManager.ts
+31
-31
@@ -2509,38 +2509,38 @@ export const layerWithOptions = (
2509
}
2510
const unloadThread = detached.value.exposedRuntime.unloadThread;
2511
if (detached.value.supportsMultipleProviderThreads && unloadThread !== undefined) {
2512
- // Serialize unload with attachment so attached threads stay loaded and later attaches resume after unload.
2513
- yield* threadAttachment.withLock(
2514
- threadAttachmentKey(input),
2515
- Effect.gen(function* () {
2516
- const entry = (yield* Ref.get(sessions)).get(key);
2517
- if (
2518
- entry?.runtime !== detached.value.runtime ||
2519
- entry.attachedThreadIds.has(input.threadId)
2520
- ) {
2521
- return;
2522
- }
2523
- yield* Effect.forEach(
2524
- detachedProviderThreads.filter((thread) => thread.nativeThreadRef !== null),
2525
- (providerThread) =>
2526
- unloadThread({ providerThread }).pipe(
2527
- Effect.timeout(UNLOAD_THREAD_TIMEOUT_MS),
2528
- Effect.catchCause((cause) =>
2529
- Effect.logWarning(
2530
- "orchestration-v2.driver-session.detach-unload-failed",
2531
- {
2532
- providerSessionId: input.providerSessionId,
2533
- threadId: input.threadId,
2534
- providerThreadId: providerThread.id,
2535
- cause,
2536
- },
2537
- ),
2538
- ),
2512
+ // The projection includes native children owned by other app threads;
2513
+ // lock each owner so an attached child stays loaded and later attaches wait.
2514
+ for (const providerThread of detachedProviderThreads) {
2515
+ if (providerThread.nativeThreadRef === null) continue;
2516
+ const ownerThreadId = providerThread.appThreadId ?? input.threadId;
2517
+ yield* threadAttachment.withLock(
2518
+ threadAttachmentKey({
2519
+ providerSessionId: input.providerSessionId,
2520
+ threadId: ownerThreadId,
2521
+ }),
2522
+ Effect.gen(function* () {
2523
+ const entry = (yield* Ref.get(sessions)).get(key);
2524
+ if (
2525
+ entry?.runtime !== detached.value.runtime ||
2526
+ entry.attachedThreadIds.has(ownerThreadId)
2527
+ ) {
2528
+ return;
2529
+ }
2530
+ yield* unloadThread({ providerThread }).pipe(
2531
+ Effect.timeout(UNLOAD_THREAD_TIMEOUT_MS),
2532
+ Effect.catchCause((cause) =>
2533
+ Effect.logWarning("orchestration-v2.driver-session.detach-unload-failed", {
2534
+ providerSessionId: input.providerSessionId,
2535
+ threadId: ownerThreadId,
2536
+ providerThreadId: providerThread.id,
2537
+ cause,
2538
+ }),
2539
),
2540
- { concurrency: 1, discard: true },
2541
- );
2542
- }),
2543
- );
2540
+ );
2541
+ }),
2542
+ );
2543
+ }
2544
}
2545
yield* scheduleIdleRelease(input.providerSessionId);
2546
},