@devries-cameron20 / t3code-1 / commits / e367ef0da1
Fork 0

The best way to control Claude Code, Codex and more from any computer or phone

fix(server): a delegated child's turn end re-offers only its own parent run's wake (#37399)

Julius Marminge committed Nov 15, 2026 at 15:53 UTC e367ef0da11e11a814911c93d812e2681c51294b
2 files changed +121 -1
apps/server/src/orchestration-v2/Orchestrator.control-reads.test.ts
+105
@@ -2855,3 +2855,108 @@ it.effect("a running provider-turn update reads only the thread's queued and run
2855 );
2856 }),
2857 );
2858 +
2859 +it.effect(
2860 + "a delegated child's run end reads the parent's runs no matter how many deliveries it has open",
2861 + () =>
2862 + Effect.gen(function* () {
2863 + const orchestrator = yield* Orchestrator.OrchestratorV2;
2864 + const projections = yield* ProjectionStore.ProjectionStoreV2;
2865 + const parentThreadId = ThreadId.make("thread:busy-coordinator");
2866 + const now = yield* DateTime.now;
2867 + yield* orchestrator.dispatch({
2868 + type: "thread.create",
2869 + commandId: CommandId.make("create-busy-coordinator"),
2870 + threadId: parentThreadId,
2871 + projectId: ProjectId.make("project:busy-coordinator"),
2872 + title: "Coordinator",
2873 + modelSelection,
2874 + runtimeMode: "full-access",
2875 + interactionMode: "default",
2876 + branch: null,
2877 + worktreePath: null,
2878 + createdBy: "user",
2879 + creationSource: "web",
2880 + });
2881 + yield* orchestrator.dispatch({
2882 + type: "message.dispatch",
2883 + commandId: CommandId.make("start-busy-coordinator"),
2884 + threadId: parentThreadId,
2885 + messageId: MessageId.make("message:busy-coordinator"),
2886 + text: "Delegate the work",
2887 + attachments: [],
2888 + dispatchMode: { type: "start_immediately" },
2889 + createdBy: "user",
2890 + creationSource: "web",
2891 + });
2892 + const parentRun = (yield* projections.getThreadRecords(parentThreadId, ["runs"])).runs[0]!;
2893 + yield* orchestrator.dispatch({
2894 + type: "delegated_task.request",
2895 + commandId: CommandId.make("delegate-busy-coordinator"),
2896 + parentThreadId,
2897 + parentRunId: parentRun.id,
2898 + parentNodeId: parentRun.rootNodeId!,
2899 + task: "Do the work",
2900 + completionWake: "always",
2901 + modelSelection,
2902 + runtimeMode: "full-access",
2903 + interactionMode: "default",
2904 + createdBy: "agent",
2905 + creationSource: "mcp",
2906 + });
2907 + const task = (yield* projections.getThreadRecords(parentThreadId, ["subagents"]))
2908 + .subagents[0]!;
2909 + const childThreadId = task.childThreadId!;
2910 + const childRun = (yield* projections.getThreadRecords(childThreadId, ["runs"])).runs[0]!;
2911 +
2912 + const runReads: Array<string> = [];
2913 + const recordRunReads = Effect.provideService(
2914 + Statement.CurrentTransformer,
2915 + (statement, _make, _fiber, _span) => {
2916 + const [query, params] = statement.compile();
2917 + if (
2918 + query.includes("FROM orchestration_v2_projection_runs") &&
2919 + params.includes(parentThreadId)
2920 + )
2921 + runReads.push(query);
2922 + return Effect.succeed(statement);
2923 + },
2924 + );
2925 + const captureReads = Effect.gen(function* () {
2926 + runReads.length = 0;
2927 + yield* orchestrator
2928 + .recoverDelegatedTask(childThreadId, { completedRunId: childRun.id })
2929 + .pipe(recordRunReads);
2930 + return runReads.length;
2931 + });
2932 + const quietParent = yield* captureReads;
2933 +
2934 + // Completion wakes queued behind the coordinator's own work, each with a delivery still open.
2935 + for (let index = 0; index < 40; index++) {
2936 + const runId = RunId.make(`run:busy-coordinator:wake:${index}`);
2937 + yield* projections.apply({
2938 + id: EventId.make(`event:busy-coordinator:wake:${index}`),
2939 + type: "run.updated",
2940 + threadId: parentThreadId,
2941 + runId,
2942 + occurredAt: now,
2943 + payload: {
2944 + ...parentRun,
2945 + id: runId,
2946 + ordinal: parentRun.ordinal + index + 1,
2947 + status: "queued",
2948 + delegatedCompletion: {
2949 + disposition: "open",
2950 + nextGeneration: 2,
2951 + delivery: {
2952 + generation: 1,
2953 + messageId: MessageId.make(`message:busy-coordinator:wake:${index}`),
2954 + taskIds: [NodeId.make(`task:busy-coordinator:other:${index}`)],
2955 + },
2956 + },
2957 + },
2958 + });
2959 + }
2960 + assert.equal(yield* captureReads, quietParent);
2961 + }).pipe(Effect.provide(layerTest)),
2962 +);
apps/server/src/orchestration-v2/Orchestrator.ts
+16 -1
@@ -16075,7 +16075,22 @@ const makeOrchestrator = Effect.fn("orchestrationV2.Orchestrator.layer")(functio
16075 return;
16076 yield* threadDispatch.withLock(
16077 parentThreadId,
16078 - offerDelegatedCompletionDeliveries(parentThreadId).pipe(
16078 + Effect.gen(function* () {
16079 + // A task's wake lives on the run that requested it; the parent's other wakes are not this child's.
16080 + const { subagents } = yield* projectionStore.getThreadRecords(
16081 + parentThreadId,
16082 + ["subagents"],
16083 + { subagentChildThreadIds: [threadId] },
16084 + );
16085 + const parentRunIds = new Set(
16086 + subagents.flatMap((task) =>
16087 + task.childThreadId === threadId && task.runId !== null ? [task.runId] : [],
16088 + ),
16089 + );
16090 + for (const parentRunId of parentRunIds) {
16091 + yield* offerDelegatedCompletionDelivery(parentThreadId, parentRunId);
16092 + }
16093 + }).pipe(
16094 Effect.andThen(
16095 finalizeAppOwnedSubagent(
16096 threadId,