From a9e8601a1e4d4cb7ff0363a88e794289984f4c93 Mon Sep 17 00:00:00 2001 From: Navid Nadali Date: Sat, 21 Nov 2026 21:02:02 +0000 Subject: [PATCH 1/2] fix(mcp): t3_thread_wait no longer has parents poll a task's first run t3_thread_wait found the caller's task only through the run's delegatedTaskId, which only follow-up runs carry. During a delegated task's first run it never added the "end your turn" hint that task_status gives, so the parent was told to call again until the child finished. An untagged run now resolves to the task its thread was forked from, while that task has no result, as follow-up intake already treats it. Co-Authored-By: Claude Opus 5.5 --- .../src/mcp/OrchestratorMcpService.test.ts | 116 ++++++++++++++++++ apps/server/src/mcp/OrchestratorMcpService.ts | 9 +- 2 files changed, 123 insertions(+), 2 deletions(-) diff --git a/apps/server/src/mcp/OrchestratorMcpService.test.ts b/apps/server/src/mcp/OrchestratorMcpService.test.ts index 2dad1fe68ee6d..e40077f06359a 100644 --- a/apps/server/src/mcp/OrchestratorMcpService.test.ts +++ b/apps/server/src/mcp/OrchestratorMcpService.test.ts @@ -5691,4 +5691,120 @@ describe("OrchestratorMcpService thread stop", () => { }).pipe(Effect.provide(layerTest)); }), ); + + it.effect("t3_thread_wait tells the parent to end its turn while a task's first run works", () => + Effect.gen(function* () { + const cwd = yield* checkpointWorkspace("mcp-thread-wait-next"); + const layerAdapters = ProviderAdapterRegistry.layerSingle(liveAdapter(cwd)); + const layerDatabase = SqlitePersistence.layerMemory; + const layerOrchestrator = ProviderReplayHarness.layerWithRegistry( + { name: "mcp-thread-wait-next" }, + layerAdapters, + { databaseLayer: layerDatabase, runEffectWorker: false }, + ); + const layerThreads = ThreadManagementService.layer.pipe( + Layer.provideMerge(layerOrchestrator), + Layer.provide(ServerSettings.layerTest()), + Layer.provide(CommandReceiptStore.layer.pipe(Layer.provide(layerDatabase))), + ); + const layerTest = layerOrchestratorService.pipe( + Layer.provideMerge(layerThreads), + Layer.provide( + Layer.mergeAll( + NodeServices.layer, + NodeCrypto.layer, + layerAdapters, + ServerSettings.layerTest(), + Layer.mock(ProviderRegistry.ProviderRegistry)({ getProviders: Effect.succeed([]) }), + Layer.mock(ProjectService.ProjectService)({ getById: () => Effect.succeedNone }), + Layer.mock(SecretRequests.SecretRequests)({}), + Layer.mock(RemoteDelegation.RemoteDelegation)({}), + Layer.mock(ScheduledTaskService.ScheduledTaskService)({}), + ), + ), + ); + yield* Effect.gen(function* () { + const orchestrator = yield* Orchestrator.OrchestratorV2; + const worker = yield* EffectWorker.OrchestrationEffectWorkerV2; + const service = yield* OrchestratorMcpService.OrchestratorMcpService; + const turnRunning = (inParent: boolean) => + orchestrator.streamDomainEvents.pipe( + Stream.filter( + (event) => + event.type === "provider-turn.updated" && + (event.threadId === threadId) === inParent && + event.payload.status === "running", + ), + Stream.take(1), + Stream.runDrain, + Effect.forkScoped, + ); + yield* orchestrator.dispatch({ + type: "thread.create", + commandId: CommandId.make("create"), + threadId, + projectId: ProjectId.make("project:mcp-wait"), + title: "Parent", + modelSelection, + runtimeMode: "full-access", + interactionMode: "default", + branch: null, + worktreePath: cwd, + createdBy: "user", + creationSource: "web", + }); + const parentRunning = yield* turnRunning(true); + yield* orchestrator.dispatch({ + type: "message.dispatch", + commandId: CommandId.make("send:work"), + threadId, + messageId: MessageId.make("message:work"), + text: "work", + attachments: [], + dispatchMode: { type: "start_immediately" }, + createdBy: "user", + creationSource: "web", + }); + yield* worker.drain(); + yield* Fiber.join(parentRunning); + const parentRun = (yield* orchestrator.getThreadProjection(threadId)).runs[0]!; + const childRunning = yield* turnRunning(false); + yield* orchestrator.dispatch({ + type: "delegated_task.request", + commandId: CommandId.make("delegate"), + parentThreadId: threadId, + parentRunId: parentRun.id, + parentNodeId: parentRun.rootNodeId!, + task: "child work", + modelSelection, + runtimeMode: "full-access", + interactionMode: "default", + completionWake: "always", + createdBy: "agent", + creationSource: "mcp", + }); + yield* worker.drain(); + yield* Fiber.join(childRunning); + const childThreadId = (yield* orchestrator.getThreadProjection(threadId)).subagents[0]! + .childThreadId!; + + const parent: McpInvocationScope = { + ...client, + thread: { + threadId, + providerSessionId: "provider-session:mcp-wait", + providerInstanceId: instanceId, + }, + client: undefined, + }; + const waiting = yield* service + .waitForThread(parent, { threadId: childThreadId, timeoutMs: 1_000 }) + .pipe(Effect.forkScoped); + yield* TestClock.adjust("1 second"); + const waited = yield* Fiber.join(waiting); + assert.isTrue(waited.timedOut); + assert.equal(waited.next, ORCHESTRATOR_MCP_RUNNING_TASK_NEXT); + }).pipe(Effect.provide(layerTest)); + }), + ); }); diff --git a/apps/server/src/mcp/OrchestratorMcpService.ts b/apps/server/src/mcp/OrchestratorMcpService.ts index 07b137c61e354..415f16b646d76 100644 --- a/apps/server/src/mcp/OrchestratorMcpService.ts +++ b/apps/server/src/mcp/OrchestratorMcpService.ts @@ -3712,7 +3712,11 @@ const make = Effect.gen(function* () { }) .pipe(Effect.mapError(threadManagementFailure)); const status = result.run === null ? "idle" : runOutcome(result.run); - const taskId = result.run?.delegatedTaskId; + // A task's first run carries no task id; the child's untagged runs continue that task + // until it has a result. + const taskId = + result.run?.delegatedTaskId ?? + (target.thread.forkedFrom?.type === "node" ? target.thread.forkedFrom.nodeId : undefined); const ownedTask = result.timedOut && status !== "waiting" && @@ -3724,7 +3728,8 @@ const make = Effect.gen(function* () { task.origin === "app_owned" && task.threadId === parent.thread.id && task.childThreadId === input.threadId && - task.completionDelivery?.state !== "disposed", + task.completionDelivery?.state !== "disposed" && + (result.run?.delegatedTaskId !== undefined || task.result === null), ); // A child waiting on a question or approval needs an answer, not a wake. const childRequests = ownedTask From 2f6469ac807b5c4833eff4bec15123fdbf9aee58 Mon Sep 17 00:00:00 2001 From: Julius Marminge Date: Sun, 22 Nov 2026 00:41:43 +0000 Subject: [PATCH 2/2] test(mcp): cover a task's first run in the existing t3_thread_wait next case Replace the separate replay-harness case with assertions in the existing mocked test: an untagged first run gets the end-turn guidance, and an untagged run after the task has its result does not. Co-Authored-By: Claude Opus 5.5 --- .../src/mcp/OrchestratorMcpService.test.ts | 140 +++--------------- 1 file changed, 20 insertions(+), 120 deletions(-) diff --git a/apps/server/src/mcp/OrchestratorMcpService.test.ts b/apps/server/src/mcp/OrchestratorMcpService.test.ts index 24217aea655fc..ccd41144a5ea8 100644 --- a/apps/server/src/mcp/OrchestratorMcpService.test.ts +++ b/apps/server/src/mcp/OrchestratorMcpService.test.ts @@ -617,6 +617,7 @@ describe("OrchestratorMcpService", () => { const childThreadId = ThreadId.make("thread:mcp-next-child"); const taskId = NodeId.make("node:mcp-next-task"); let finished = false; + let firstRun = false; let pendingRequest: "live" | "message" | null = null; const parentProjection = () => ({ @@ -640,12 +641,17 @@ describe("OrchestratorMcpService", () => { const childRun = () => ({ id: RunId.make("run:mcp-next-child"), ordinal: 1, - status: finished ? ("completed" as const) : ("running" as const), - delegatedTaskId: taskId, + status: finished && !firstRun ? ("completed" as const) : ("running" as const), + // A task's first run is untagged; the child's fork node names the task. + ...(firstRun ? {} : { delegatedTaskId: taskId }), }); const childProjection = () => ({ - thread: { id: childThreadId, title: "Child task" }, + thread: { + id: childThreadId, + title: "Child task", + forkedFrom: { type: "node", nodeId: taskId }, + }, runs: [childRun()], contextTransfers: [], messages: [], @@ -677,7 +683,7 @@ describe("OrchestratorMcpService", () => { Effect.succeed({ threadId: childThreadId, run: childRun() as unknown as OrchestrationV2ThreadProjection["runs"][number], - timedOut: !finished, + timedOut: !finished || firstRun, }), dispatch: () => Effect.succeed({} as never), }), @@ -725,7 +731,17 @@ describe("OrchestratorMcpService", () => { assert.equal(working.next, ORCHESTRATOR_MCP_RUNNING_TASK_NEXT); pendingRequest = null; + firstRun = true; + const first = yield* service.waitForThread(scope, { threadId: childThreadId }); + assert.equal(first.next, ORCHESTRATOR_MCP_RUNNING_TASK_NEXT); + + // An untagged run after the task has its result is no longer that task. finished = true; + const followUp = yield* service.waitForThread(scope, { threadId: childThreadId }); + assert.isTrue(followUp.timedOut); + assert.notProperty(followUp, "next"); + firstRun = false; + const completed = yield* service.taskStatus(scope, taskId); assert.equal(completed.status, "completed"); assert.notProperty(completed, "next"); @@ -5705,120 +5721,4 @@ describe("OrchestratorMcpService thread stop", () => { }).pipe(Effect.provide(layerTest)); }), ); - - it.effect("t3_thread_wait tells the parent to end its turn while a task's first run works", () => - Effect.gen(function* () { - const cwd = yield* checkpointWorkspace("mcp-thread-wait-next"); - const layerAdapters = ProviderAdapterRegistry.layerSingle(liveAdapter(cwd)); - const layerDatabase = SqlitePersistence.layerMemory; - const layerOrchestrator = ProviderReplayHarness.layerWithRegistry( - { name: "mcp-thread-wait-next" }, - layerAdapters, - { databaseLayer: layerDatabase, runEffectWorker: false }, - ); - const layerThreads = ThreadManagementService.layer.pipe( - Layer.provideMerge(layerOrchestrator), - Layer.provide(ServerSettings.layerTest()), - Layer.provide(CommandReceiptStore.layer.pipe(Layer.provide(layerDatabase))), - ); - const layerTest = layerOrchestratorService.pipe( - Layer.provideMerge(layerThreads), - Layer.provide( - Layer.mergeAll( - NodeServices.layer, - NodeCrypto.layer, - layerAdapters, - ServerSettings.layerTest(), - Layer.mock(ProviderRegistry.ProviderRegistry)({ getProviders: Effect.succeed([]) }), - Layer.mock(ProjectService.ProjectService)({ getById: () => Effect.succeedNone }), - Layer.mock(SecretRequests.SecretRequests)({}), - Layer.mock(RemoteDelegation.RemoteDelegation)({}), - Layer.mock(ScheduledTaskService.ScheduledTaskService)({}), - ), - ), - ); - yield* Effect.gen(function* () { - const orchestrator = yield* Orchestrator.OrchestratorV2; - const worker = yield* EffectWorker.OrchestrationEffectWorkerV2; - const service = yield* OrchestratorMcpService.OrchestratorMcpService; - const turnRunning = (inParent: boolean) => - orchestrator.streamDomainEvents.pipe( - Stream.filter( - (event) => - event.type === "provider-turn.updated" && - (event.threadId === threadId) === inParent && - event.payload.status === "running", - ), - Stream.take(1), - Stream.runDrain, - Effect.forkScoped, - ); - yield* orchestrator.dispatch({ - type: "thread.create", - commandId: CommandId.make("create"), - threadId, - projectId: ProjectId.make("project:mcp-wait"), - title: "Parent", - modelSelection, - runtimeMode: "full-access", - interactionMode: "default", - branch: null, - worktreePath: cwd, - createdBy: "user", - creationSource: "web", - }); - const parentRunning = yield* turnRunning(true); - yield* orchestrator.dispatch({ - type: "message.dispatch", - commandId: CommandId.make("send:work"), - threadId, - messageId: MessageId.make("message:work"), - text: "work", - attachments: [], - dispatchMode: { type: "start_immediately" }, - createdBy: "user", - creationSource: "web", - }); - yield* worker.drain(); - yield* Fiber.join(parentRunning); - const parentRun = (yield* orchestrator.getThreadProjection(threadId)).runs[0]!; - const childRunning = yield* turnRunning(false); - yield* orchestrator.dispatch({ - type: "delegated_task.request", - commandId: CommandId.make("delegate"), - parentThreadId: threadId, - parentRunId: parentRun.id, - parentNodeId: parentRun.rootNodeId!, - task: "child work", - modelSelection, - runtimeMode: "full-access", - interactionMode: "default", - completionWake: "always", - createdBy: "agent", - creationSource: "mcp", - }); - yield* worker.drain(); - yield* Fiber.join(childRunning); - const childThreadId = (yield* orchestrator.getThreadProjection(threadId)).subagents[0]! - .childThreadId!; - - const parent: McpInvocationScope = { - ...client, - thread: { - threadId, - providerSessionId: "provider-session:mcp-wait", - providerInstanceId: instanceId, - }, - client: undefined, - }; - const waiting = yield* service - .waitForThread(parent, { threadId: childThreadId, timeoutMs: 1_000 }) - .pipe(Effect.forkScoped); - yield* TestClock.adjust("1 second"); - const waited = yield* Fiber.join(waiting); - assert.isTrue(waited.timedOut); - assert.equal(waited.next, ORCHESTRATOR_MCP_RUNNING_TASK_NEXT); - }).pipe(Effect.provide(layerTest)); - }), - ); });