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

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

fix(server): running task results tell the agent to end its turn (#24782)

Co-authored-by: Claude Opus 5.5 <noreply@anthropic.com>

Theo Browne committed Oct 25, 2026 at 01:07 UTC a96cf0818eff4a113c3ff52ae702683e51a5803b
3 files changed +146 -3
apps/server/src/mcp/OrchestratorMcpService.test.ts
+109
@@ -5,6 +5,7 @@ import {
5 EnvironmentId,
6 MessageId,
7 NodeId,
8 + ORCHESTRATOR_MCP_RUNNING_TASK_NEXT,
9 type OrchestratorMcpCapabilitiesResult,
10 OrchestratorMcpTarget,
11 type OrchestrationV2ThreadShell,
@@ -292,6 +293,114 @@ describe("OrchestratorMcpService", () => {
293 }),
294 );
295
296 + it.effect("tells the parent to end its turn only while its task still runs", () =>
297 + Effect.gen(function* () {
298 + const parentThreadId = ThreadId.make("thread:mcp-next-parent");
299 + const childThreadId = ThreadId.make("thread:mcp-next-child");
300 + const taskId = NodeId.make("node:mcp-next-task");
301 + let finished = false;
302 + let pendingRequest = false;
303 + const parentProjection = () =>
304 + ({
305 + thread: { id: parentThreadId },
306 + runs: [],
307 + contextTransfers: [],
308 + subagents: [
309 + {
310 + id: taskId,
311 + threadId: parentThreadId,
312 + origin: "app_owned",
313 + childThreadId,
314 + driver: "codex",
315 + model: "gpt-5.6-terra",
316 + status: finished ? "completed" : "running",
317 + result: finished ? "done" : null,
318 + completionDelivery: { state: "pending" },
319 + },
320 + ],
321 + }) as unknown as OrchestrationV2ThreadProjection;
322 + const childRun = () => ({
323 + id: RunId.make("run:mcp-next-child"),
324 + ordinal: 1,
325 + status: finished ? ("completed" as const) : ("running" as const),
326 + delegatedTaskId: taskId,
327 + });
328 + const childProjection = () =>
329 + ({
330 + thread: { id: childThreadId, title: "Child task" },
331 + runs: [childRun()],
332 + contextTransfers: [],
333 + messages: [],
334 + subagents: [],
335 + providerThreads: [],
336 + runtimeRequests: pendingRequest ? [{ status: "pending" }] : [],
337 + nodes: [],
338 + turnItems: [],
339 + }) as unknown as OrchestrationV2ThreadProjection;
340 + const layerDependencies = Layer.mergeAll(
341 + NodeServices.layer,
342 + Layer.mock(ThreadManagementService.ThreadManagementService)({
343 + getThreadRecords: (threadId) =>
344 + Effect.succeed(threadId === parentThreadId ? parentProjection() : childProjection()),
345 + getThreadShell: (threadId) => Effect.succeed(liveThreadShell(threadId)),
346 + getProjectThreadRecords: () => Effect.succeed(childProjection()),
347 + waitForThread: () =>
348 + Effect.succeed({
349 + threadId: childThreadId,
350 + run: childRun() as unknown as OrchestrationV2ThreadProjection["runs"][number],
351 + timedOut: !finished,
352 + }),
353 + dispatch: () => Effect.succeed({} as never),
354 + }),
355 + ServerSettings.layerTest(),
356 + Layer.mock(ProviderRegistry.ProviderRegistry)({ getProviders: Effect.succeed([]) }),
357 + Layer.mock(ProviderAdapterRegistry.ProviderAdapterRegistryV2)({
358 + list: () => Effect.succeed([]),
359 + }),
360 + Layer.mock(ProjectService.ProjectService)({}),
361 + Layer.mock(SecretRequests.SecretRequests)({}),
362 + Layer.mock(ScheduledTaskService.ScheduledTaskService)({}),
363 + );
364 + const scope: McpInvocationScope = {
365 + environmentId: EnvironmentId.make("environment:mcp-next"),
366 + requestNamespace: "provider-session:mcp-next",
367 + thread: {
368 + threadId: parentThreadId,
369 + providerSessionId: "provider-session:mcp-next",
370 + providerInstanceId: ProviderInstanceId.make("codex"),
371 + },
372 + client: undefined,
373 + capabilities: new Set(["orchestration"]),
374 + issuedAt: 1,
375 + };
376 +
377 + yield* Effect.gen(function* () {
378 + const service = yield* OrchestratorMcpService.OrchestratorMcpService;
379 + const running = yield* service.taskStatus(scope, taskId);
380 + assert.equal(running.status, "running");
381 + assert.equal(running.next, ORCHESTRATOR_MCP_RUNNING_TASK_NEXT);
382 + const waiting = yield* service.waitForThread(scope, { threadId: childThreadId });
383 + assert.isTrue(waiting.timedOut);
384 + assert.equal(waiting.next, ORCHESTRATOR_MCP_RUNNING_TASK_NEXT);
385 +
386 + // A child waiting on a question or approval needs an answer, not a wake.
387 + pendingRequest = true;
388 + const asking = yield* service.waitForThread(scope, { threadId: childThreadId });
389 + assert.isTrue(asking.timedOut);
390 + assert.notProperty(asking, "next");
391 + pendingRequest = false;
392 +
393 + finished = true;
394 + const completed = yield* service.taskStatus(scope, taskId);
395 + assert.equal(completed.status, "completed");
396 + assert.notProperty(completed, "next");
397 + const waited = yield* service.waitForThread(scope, { threadId: childThreadId });
398 + assert.equal(waited.status, "completed");
399 + assert.notProperty(waited, "next");
400 + }).pipe(Effect.provide(layerOrchestratorService.pipe(Layer.provide(layerDependencies))));
401 + }),
402 + );
403 +
404 it.effect("does not dispose delivery when a nonterminal task has no active child run", () =>
405 Effect.gen(function* () {
406 const parentThreadId = ThreadId.make("thread:mcp-cancel-parent");
apps/server/src/mcp/OrchestratorMcpService.ts
+30 -3
@@ -5,6 +5,7 @@ import {
5 MessageId,
6 type ModelSelection,
7 NodeId,
8 + ORCHESTRATOR_MCP_RUNNING_TASK_NEXT,
9 TurnItemId,
10 type OrchestrationV2Run,
11 type OrchestrationV2Subagent,
@@ -1597,7 +1598,7 @@ const make = Effect.gen(function* () {
1598 instanceId: task.providerInstanceId,
1599 model: task.model,
1600 };
1600 - const response = {
1601 + const result = {
1602 taskId: task.id,
1603 childThreadId: task.childThreadId,
1604 link: formatThreadLink(task.childThreadId, childControls.thread.title),
@@ -1633,6 +1634,10 @@ const make = Effect.gen(function* () {
1634 : pendingTaskApproval(childControls, task.id, childRun),
1635 waitTimedOut,
1636 } satisfies OrchestratorMcpDelegateTaskResult;
1637 + const response: OrchestratorMcpDelegateTaskResult =
1638 + endsTaskWait(result) || task.completionDelivery?.state === "disposed"
1639 + ? result
1640 + : { ...result, next: ORCHESTRATOR_MCP_RUNNING_TASK_NEXT };
1641 if (
1642 acknowledgeTerminal &&
1643 isTerminalTaskStatus(status) &&
@@ -2853,7 +2858,7 @@ const make = Effect.gen(function* () {
2858 } satisfies OrchestratorMcpThreadSendResult;
2859 }),
2860 waitForThread: Effect.fn("OrchestratorMcpService.waitForThread")(function* (scope, input) {
2856 - const { target } = yield* loadScopedThread(scope, input.threadId);
2861 + const { parent, target } = yield* loadScopedThread(scope, input.threadId);
2862 const result = yield* threadManagement
2863 .waitForThread({
2864 projectId: target.thread.projectId,
@@ -2865,11 +2870,33 @@ const make = Effect.gen(function* () {
2870 ),
2871 })
2872 .pipe(Effect.mapError(threadManagementFailure));
2873 + const status = result.run === null ? "idle" : runOutcome(result.run);
2874 + const taskId = result.run?.delegatedTaskId;
2875 + const ownedTask =
2876 + result.timedOut &&
2877 + status !== "waiting" &&
2878 + parent?.subagents.some(
2879 + (task) =>
2880 + task.id === taskId &&
2881 + task.origin === "app_owned" &&
2882 + task.threadId === parent.thread.id &&
2883 + task.childThreadId === input.threadId &&
2884 + task.completionDelivery?.state !== "disposed",
2885 + ) === true;
2886 + // A child waiting on a question or approval needs an answer, not a wake.
2887 + const childRequests = ownedTask
2888 + ? (yield* threadManagement
2889 + .getThreadRecords(input.threadId, ["runtimeRequests"])
2890 + .pipe(Effect.mapError(threadManagementFailure))).runtimeRequests
2891 + : [];
2892 + const wakesCaller =
2893 + ownedTask && !childRequests.some((request) => request.status === "pending");
2894 return {
2895 threadId: input.threadId,
2896 runId: result.run?.id ?? null,
2871 - status: result.run === null ? "idle" : runOutcome(result.run),
2897 + status,
2898 timedOut: result.timedOut,
2899 + ...(wakesCaller ? { next: ORCHESTRATOR_MCP_RUNNING_TASK_NEXT } : {}),
2900 } satisfies OrchestratorMcpThreadWaitResult;
2901 }),
2902 interruptThread: Effect.fn("OrchestratorMcpService.interruptThread")(function* (scope, input) {
packages/contracts/src/orchestratorMcp.ts
+7
@@ -187,6 +187,11 @@ export const OrchestratorMcpDelegateTaskInput = Schema.Struct({
187 });
188 export type OrchestratorMcpDelegateTaskInput = typeof OrchestratorMcpDelegateTaskInput.Type;
189
190 +// Set only on results for still-running tasks whose completion will wake the parent.
191 +export const ORCHESTRATOR_MCP_RUNNING_TASK_NEXT =
192 + "Completion wakes this thread. End your turn unless you need this result now.";
193 +const OrchestratorMcpRunningTaskNext = Schema.Literal(ORCHESTRATOR_MCP_RUNNING_TASK_NEXT);
194 +
195 export const OrchestratorMcpDelegateTaskResult = Schema.Struct({
196 taskId: NodeId,
197 childThreadId: ThreadId,
@@ -224,6 +229,7 @@ export const OrchestratorMcpDelegateTaskResult = Schema.Struct({
229 description:
230 "True only on that mode=wait call when timeoutMs elapsed. The timeout does not cancel the child. Later task_status reads return false and use status for liveness.",
231 }),
232 + next: Schema.optional(OrchestratorMcpRunningTaskNext),
233 });
234 export type OrchestratorMcpDelegateTaskResult = typeof OrchestratorMcpDelegateTaskResult.Type;
235
@@ -500,6 +506,7 @@ export const OrchestratorMcpThreadWaitResult = Schema.Struct({
506 runId: Schema.NullOr(RunId),
507 status: OrchestratorMcpThreadStatus,
508 timedOut: Schema.Boolean,
509 + next: Schema.optional(OrchestratorMcpRunningTaskNext),
510 });
511 export type OrchestratorMcpThreadWaitResult = typeof OrchestratorMcpThreadWaitResult.Type;
512