@devries-cameron20 / t3code Mirror · read-only / commits / d7454f7e39
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 21, 19:45 UTC

fix(server): finished Claude delegates release their process (#34934)

Julius Marminge committed Nov 12, 2026 at 00:15 UTC d7454f7e39beddfe9f63c5949b6772bf90dfa202
2 files changed +177 -87
apps/server/src/orchestration-v2/DelegatedCompletionDelivery.test.ts
+131 -87
@@ -17,6 +17,7 @@ import {
17 ProjectId,
18 ProviderDriverKind,
19 ProviderInstanceId,
20 + ProviderSessionId,
21 ProviderThreadId,
22 RunId,
23 RuntimeRequestId,
@@ -3706,96 +3707,139 @@ it.layer(Layer.merge(layerTest, ProviderContinuationRequests.layer))(
3707 );
3708
3709 it.layer(layerTest)("delegated tasks across a server restart", (it) => {
3709 - it.effect("queues a child result without creating a provider history handoff", () =>
3710 - Effect.gen(function* () {
3711 - const orchestrator = yield* Orchestrator.OrchestratorV2;
3712 - const eventSink = yield* EventSink.EventSinkV2;
3713 - const now = yield* DateTime.now;
3714 - const threadId = ThreadId.make("thread:result-message-parent");
3715 - const projectId = ProjectId.make("project:result-message");
3716 - const runId = RunId.make("run:result-message-parent");
3717 - const rootNodeId = NodeId.make("node:result-message-parent");
3718 - yield* seedParentWithTerminalTask({
3719 - threadId,
3720 - projectId,
3721 - runId,
3722 - rootNodeId,
3723 - taskId: NodeId.make("node:result-message-seed"),
3724 - deliveryState: "acknowledged",
3725 - now,
3726 - });
3727 - const child = yield* seedRestartCancelledChild({
3728 - parentThreadId: threadId,
3729 - projectId,
3730 - parentRunId: runId,
3731 - rootNodeId,
3732 - name: "result-message-child",
3733 - completionWake: "always",
3734 - continuationPending: false,
3735 - runStatus: "completed",
3736 - now,
3737 - });
3738 - const parent = yield* orchestrator.getThreadProjection(threadId);
3739 - const childProjection = yield* orchestrator.getThreadProjection(child.childThreadId);
3740 - const providerThreadId = ProviderThreadId.make("provider-thread:result-message-child");
3741 - yield* eventSink.write({
3742 - commandId: CommandId.make("command:result-message-child-provider"),
3743 - events: [
3744 - {
3745 - id: EventId.make("event:result-message-child-provider"),
3746 - type: "provider-thread.updated",
3747 - threadId: child.childThreadId,
3748 - occurredAt: now,
3749 - payload: {
3750 - ...parent.providerThreads[0]!,
3751 - id: providerThreadId,
3752 - appThreadId: child.childThreadId,
3710 + it.effect.each([
3711 + { name: "releases", key: "", pendingBackgroundTasks: [], released: true },
3712 + {
3713 + name: "keeps while an unawaited background command runs",
3714 + key: "-background",
3715 + pendingBackgroundTasks: [{ taskId: "dev-server", kind: "command" as const }],
3716 + released: false,
3717 + },
3718 + ])(
3719 + "queues a child result without creating a provider history handoff and $name its process",
3720 + ({ key, pendingBackgroundTasks, released }) =>
3721 + Effect.gen(function* () {
3722 + const orchestrator = yield* Orchestrator.OrchestratorV2;
3723 + const eventSink = yield* EventSink.EventSinkV2;
3724 + const now = yield* DateTime.now;
3725 + const threadId = ThreadId.make(`thread:result-message${key}-parent`);
3726 + const projectId = ProjectId.make(`project:result-message${key}`);
3727 + const runId = RunId.make(`run:result-message${key}-parent`);
3728 + const rootNodeId = NodeId.make(`node:result-message${key}-parent`);
3729 + yield* seedParentWithTerminalTask({
3730 + threadId,
3731 + projectId,
3732 + runId,
3733 + rootNodeId,
3734 + taskId: NodeId.make(`node:result-message${key}-seed`),
3735 + deliveryState: "acknowledged",
3736 + now,
3737 + });
3738 + const child = yield* seedRestartCancelledChild({
3739 + parentThreadId: threadId,
3740 + projectId,
3741 + parentRunId: runId,
3742 + rootNodeId,
3743 + name: `result-message${key}-child`,
3744 + completionWake: "always",
3745 + continuationPending: false,
3746 + runStatus: "completed",
3747 + now,
3748 + });
3749 + const parent = yield* orchestrator.getThreadProjection(threadId);
3750 + const childProjection = yield* orchestrator.getThreadProjection(child.childThreadId);
3751 + const providerThreadId = ProviderThreadId.make(
3752 + `provider-thread:result-message${key}-child`,
3753 + );
3754 + yield* eventSink.write({
3755 + commandId: CommandId.make(`command:result-message${key}-child-provider`),
3756 + events: [
3757 + {
3758 + id: EventId.make(`event:result-message${key}-child-session`),
3759 + type: "provider-session.attached",
3760 + threadId: child.childThreadId,
3761 + occurredAt: now,
3762 + payload: {
3763 + id: ProviderSessionId.make(`provider-session:result-message${key}-child`),
3764 + driver,
3765 + providerInstanceId: modelSelection.instanceId,
3766 + status: "ready",
3767 + cwd: "/repo",
3768 + model: modelSelection.model,
3769 + capabilities: {
3770 + ...CodexProviderCapabilitiesV2,
3771 + sessions: {
3772 + ...CodexProviderCapabilitiesV2.sessions,
3773 + supportsMultipleProviderThreadsPerSession: false,
3774 + },
3775 + },
3776 + createdAt: now,
3777 + updatedAt: now,
3778 + lastError: null,
3779 + },
3780 },
3754 - },
3755 - {
3756 - id: EventId.make("event:result-message-child-run"),
3757 - type: "run.updated",
3758 - threadId: child.childThreadId,
3759 - runId: child.childRunId,
3760 - occurredAt: now,
3761 - payload: { ...childProjection.runs[0]!, providerThreadId },
3762 - },
3763 - ],
3764 - });
3781 + {
3782 + id: EventId.make(`event:result-message${key}-child-provider`),
3783 + type: "provider-thread.updated",
3784 + threadId: child.childThreadId,
3785 + occurredAt: now,
3786 + payload: {
3787 + ...parent.providerThreads[0]!,
3788 + id: providerThreadId,
3789 + appThreadId: child.childThreadId,
3790 + pendingBackgroundTasks,
3791 + },
3792 + },
3793 + {
3794 + id: EventId.make(`event:result-message${key}-child-run`),
3795 + type: "run.updated",
3796 + threadId: child.childThreadId,
3797 + runId: child.childRunId,
3798 + occurredAt: now,
3799 + payload: { ...childProjection.runs[0]!, providerThreadId },
3800 + },
3801 + ],
3802 + });
3803
3766 - yield* orchestrator.recoverDelegatedTasks;
3767 - const recovered = yield* orchestrator.getThreadProjection(threadId);
3768 - const task = recovered.subagents.find((candidate) => candidate.id === child.taskId)!;
3769 - assert.equal(task.status, "completed");
3770 - assert.isNotNull(task.result);
3771 - assert.isEmpty(recovered.contextHandoffs);
3772 - assert.equal(recovered.contextTransfers.length, 1);
3804 + yield* orchestrator.recoverDelegatedTasks;
3805 + const recovered = yield* orchestrator.getThreadProjection(threadId);
3806 + const task = recovered.subagents.find((candidate) => candidate.id === child.taskId)!;
3807 + assert.equal(task.status, "completed");
3808 + assert.isNotNull(task.result);
3809 + assert.isEmpty(recovered.contextHandoffs);
3810 + assert.equal(recovered.contextTransfers.length, 1);
3811 + // A one-process-per-thread child releases its process once its result is recorded,
3812 + // unless background work it did not await would die with it.
3813 + assert.equal(
3814 + (yield* orchestrator.getThreadProjection(child.childThreadId)).providerSessions.length,
3815 + released ? 0 : 1,
3816 + );
3817
3774 - const delivery = recovered.runs.find((candidate) => candidate.id === runId)!
3775 - .delegatedCompletion!.delivery!;
3776 - const messageId = delivery.messageId;
3777 - yield* orchestrator.dispatch({
3778 - type: "message.dispatch",
3779 - commandId: CommandId.make("command:result-message-delivery"),
3780 - threadId,
3781 - messageId,
3782 - text: "Delegated task finished.",
3783 - attachments: [],
3784 - dispatchMode: { type: "queue_after_active" },
3785 - createdBy: "agent",
3786 - creationSource: "server",
3787 - delegatedCompletion: {
3788 - parentRunId: runId,
3789 - generation: delivery.generation,
3790 - taskIds: [child.taskId],
3791 - },
3792 - });
3793 - const queued = yield* orchestrator.getThreadProjection(threadId);
3794 - assert.include(
3795 - queued.messages.find((message) => message.id === messageId)?.text ?? "",
3796 - task.result!,
3797 - );
3798 - }),
3818 + const delivery = recovered.runs.find((candidate) => candidate.id === runId)!
3819 + .delegatedCompletion!.delivery!;
3820 + const messageId = delivery.messageId;
3821 + yield* orchestrator.dispatch({
3822 + type: "message.dispatch",
3823 + commandId: CommandId.make(`command:result-message${key}-delivery`),
3824 + threadId,
3825 + messageId,
3826 + text: "Delegated task finished.",
3827 + attachments: [],
3828 + dispatchMode: { type: "queue_after_active" },
3829 + createdBy: "agent",
3830 + creationSource: "server",
3831 + delegatedCompletion: {
3832 + parentRunId: runId,
3833 + generation: delivery.generation,
3834 + taskIds: [child.taskId],
3835 + },
3836 + });
3837 + const queued = yield* orchestrator.getThreadProjection(threadId);
3838 + assert.include(
3839 + queued.messages.find((message) => message.id === messageId)?.text ?? "",
3840 + task.result!,
3841 + );
3842 + }),
3843 );
3844
3845 it.effect("holds a restart-cancelled child for its continuation's result", () =>
apps/server/src/orchestration-v2/Orchestrator.ts
+46
@@ -13484,6 +13484,51 @@ const makeOrchestrator = Effect.fn("orchestrationV2.Orchestrator.layer")(functio
13484 };
13485 });
13486
13487 + // A finished delegate otherwise keeps its provider process until the idle timeout. Callers hold
13488 + // the parent lock; the child lock orders this after any follow-up that started a run meanwhile.
13489 + const releaseFinishedDelegateSessions = Effect.fn("Orchestrator.releaseFinishedDelegateSessions")(
13490 + function* (childThreadId: ThreadId, resultRunId: RunId) {
13491 + yield* threadDispatch.withLock(
13492 + childThreadId,
13493 + Effect.gen(function* () {
13494 + const child = yield* projectionStore.getThreadRecords(
13495 + childThreadId,
13496 + ["runs", "messages", "subagents", "providerThreads", "providerSessions"],
13497 + { messageRoles: ["user"] },
13498 + );
13499 + // Unawaited background tasks still allow a result but die with the process; the idle
13500 + // timer keeps their process pinned.
13501 + if (
13502 + delegatedTaskProgress(child).state !== "result_available" ||
13503 + child.providerThreads.some((thread) => (thread.pendingBackgroundTasks ?? []).length > 0)
13504 + )
13505 + return;
13506 + for (const session of child.providerSessions) {
13507 + // Shared processes unload their threads themselves; only a per-thread process lingers.
13508 + if (
13509 + session.status === "stopped" ||
13510 + session.status === "error" ||
13511 + session.capabilities.sessions.supportsMultipleProviderThreadsPerSession
13512 + )
13513 + continue;
13514 + yield* dispatchWithReceiptEffect({
13515 + type: "provider-session.detach",
13516 + commandId: CommandId.make(
13517 + `command:system:delegated-task-release:${resultRunId}:${session.id}`,
13518 + ),
13519 + threadId: childThreadId,
13520 + providerSessionId: session.id,
13521 + reason: "Delegated task finished.",
13522 + });
13523 + }
13524 + }),
13525 + );
13526 + },
13527 + Effect.catchCause((cause) =>
13528 + Effect.logWarning("Failed to release a finished delegate's provider session", { cause }),
13529 + ),
13530 + );
13531 +
13532 /**
13533 * Callers must hold the parent thread's dispatch lock: wake-policy updates the same
13534 * subagent row with full-row payloads, so unserialized writers clobber policy or terminal status.
@@ -13762,6 +13807,7 @@ const makeOrchestrator = Effect.fn("orchestrationV2.Orchestrator.layer")(functio
13807 },
13808 ];
13809 yield* writeSystemEvents(completionEvents, [], { updateDelegatedCompletionOnly: true });
13810 + yield* releaseFinishedDelegateSessions(childThreadId, childRun.id);
13811
13812 if (completionPlan.offer && completionPlan.parentRun !== undefined) {
13813 yield* offerDelegatedCompletionDelivery(parentThreadId, completionPlan.parentRun.id);