@devries-cameron20 / t3code Mirror · read-only / commits / a624256ff3
Fork 0

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, 03:15 UTC

test(server): retain delegated child events when detaching a parent (#29622)

benjamin-di committed Nov 5, 2026 at 12:20 UTC a624256ff3981a54ede06e2ee6da444c0cadf164
1 file changed +147
apps/server/src/orchestration-v2/ProviderSessionManager.test.ts
+147
@@ -3507,6 +3507,153 @@ it.effect(
3507 }),
3508 );
3509
3510 +it.effect(
3511 + "ProviderSessionManagerV2 detaching a parent keeps an attached delegated child's thread loaded",
3512 + () =>
3513 + Effect.gen(function* () {
3514 + const state = yield* Ref.make(emptyState);
3515 + const effect = Effect.gen(function* () {
3516 + const eventSink = yield* EventSink.EventSinkV2;
3517 + const idAllocator = yield* IdAllocator.IdAllocatorV2;
3518 + const manager = yield* ProviderSessionManager.ProviderSessionManagerV2;
3519 + const now = yield* DateTime.now;
3520 + const projectId = yield* idAllocator.allocate.project({
3521 + fixtureName: "provider-session-manager-delegated-child",
3522 + });
3523 + const parentThreadId = yield* idAllocator.allocate.thread({
3524 + fixtureName: "provider-session-manager-delegated-child-parent",
3525 + projectId,
3526 + });
3527 + const childThreadId = yield* idAllocator.allocate.thread({
3528 + fixtureName: "provider-session-manager-delegated-child-child",
3529 + projectId,
3530 + });
3531 + const providerSessionId = idAllocator.derive.providerSession({
3532 + providerInstanceId: modelSelection.instanceId,
3533 + });
3534 + const parentProviderThread = makeProviderThread({
3535 + idAllocator,
3536 + threadId: parentThreadId,
3537 + providerSessionId,
3538 + now,
3539 + nativeThreadId: "native-parent",
3540 + });
3541 + const childProviderThread = makeProviderThread({
3542 + idAllocator,
3543 + threadId: childThreadId,
3544 + providerSessionId,
3545 + now,
3546 + nativeThreadId: "native-child",
3547 + });
3548 + const childRunId = idAllocator.derive.run({ threadId: childThreadId, ordinal: 2 });
3549 + const parentRunId = idAllocator.derive.run({ threadId: parentThreadId, ordinal: 1 });
3550 + const subagentId = idAllocator.derive.rootNode({ runId: childRunId });
3551 +
3552 + yield* eventSink.write({
3553 + events: [
3554 + yield* makeThreadCreatedEvent({ idAllocator, threadId: parentThreadId, now }),
3555 + yield* makeThreadCreatedEvent({ idAllocator, threadId: childThreadId, now }),
3556 + {
3557 + id: yield* idAllocator.allocate.event({ threadId: parentThreadId }),
3558 + type: "provider-thread.updated",
3559 + threadId: parentThreadId,
3560 + driver: CODEX_DRIVER,
3561 + occurredAt: now,
3562 + payload: parentProviderThread,
3563 + },
3564 + {
3565 + id: yield* idAllocator.allocate.event({ threadId: childThreadId }),
3566 + type: "provider-thread.updated",
3567 + threadId: childThreadId,
3568 + driver: CODEX_DRIVER,
3569 + occurredAt: now,
3570 + payload: childProviderThread,
3571 + },
3572 + // The delegated task record already finished; the child was then sent a
3573 + // follow-up and is running a later turn of its own.
3574 + {
3575 + id: yield* idAllocator.allocate.event({ threadId: parentThreadId }),
3576 + type: "subagent.updated",
3577 + threadId: parentThreadId,
3578 + nodeId: subagentId,
3579 + providerInstanceId: modelSelection.instanceId,
3580 + occurredAt: now,
3581 + payload: {
3582 + id: subagentId,
3583 + threadId: parentThreadId,
3584 + runId: null,
3585 + parentNodeId: idAllocator.derive.rootNode({ runId: parentRunId }),
3586 + origin: "app_owned",
3587 + createdBy: "agent",
3588 + driver: CODEX_DRIVER,
3589 + providerInstanceId: modelSelection.instanceId,
3590 + providerThreadId: childProviderThread.id,
3591 + childThreadId,
3592 + nativeTaskRef: null,
3593 + prompt: "Delegated work",
3594 + title: "Delegated child",
3595 + model: modelSelection.model,
3596 + status: "completed",
3597 + result: "first result",
3598 + startedAt: now,
3599 + completedAt: now,
3600 + updatedAt: now,
3601 + },
3602 + },
3603 + {
3604 + id: yield* idAllocator.allocate.event({ threadId: childThreadId }),
3605 + type: "provider-turn.updated",
3606 + threadId: childThreadId,
3607 + runId: childRunId,
3608 + driver: CODEX_DRIVER,
3609 + occurredAt: now,
3610 + payload: {
3611 + id: idAllocator.derive.providerTurn({
3612 + driver: CODEX_DRIVER,
3613 + nativeTurnId: "native-child-turn",
3614 + }),
3615 + providerThreadId: childProviderThread.id,
3616 + nodeId: idAllocator.derive.rootNode({ runId: childRunId }),
3617 + runAttemptId: null,
3618 + nativeTurnRef: null,
3619 + ordinal: 2,
3620 + status: "running",
3621 + startedAt: now,
3622 + completedAt: null,
3623 + },
3624 + },
3625 + ],
3626 + });
3627 + yield* manager.open({
3628 + threadId: parentThreadId,
3629 + providerSessionId,
3630 + modelSelection,
3631 + runtimePolicy,
3632 + });
3633 + yield* manager.open({
3634 + threadId: childThreadId,
3635 + providerSessionId,
3636 + modelSelection,
3637 + runtimePolicy,
3638 + });
3639 +
3640 + yield* manager.detach({ providerSessionId, threadId: parentThreadId });
3641 +
3642 + const afterParentDetach = yield* Ref.get(state);
3643 + assert.equal(afterParentDetach.interruptCount, 0);
3644 + assert.deepEqual(afterParentDetach.unloadedNativeThreadIds, ["native-parent"]);
3645 +
3646 + yield* manager.detach({ providerSessionId, threadId: childThreadId });
3647 + assert.deepEqual((yield* Ref.get(state)).unloadedNativeThreadIds, [
3648 + "native-parent",
3649 + "native-child",
3650 + ]);
3651 + });
3652 +
3653 + yield* effect.pipe(Effect.provide(layerTest({ state, idleTimeoutMs: 1000 })));
3654 + }),
3655 +);
3656 +
3657 it.effect(
3658 "ProviderSessionManagerV2 invalidates only errored threads and keeps reusable session errors cached",
3659 () =>