Skip to content
TypeScript

fix(server): stop shipping delegated task results twice (#19759)

Ben Davis3eaebac0f94d
diff --git a/apps/server/src/mcp/OrchestratorMcpService.ts b/apps/server/src/mcp/OrchestratorMcpService.ts
index b31c12d9ca2..b1aa1547950 100644
--- a/apps/server/src/mcp/OrchestratorMcpService.ts
+++ b/apps/server/src/mcp/OrchestratorMcpService.ts
@@ -1453,6 +1453,13 @@ const make = Effect.gen(function* () {
null);
const resultTransfer = taskResultTransfer(parentProjection, childProjection, task) ?? null;
const terminalStatus = terminalRun === undefined ? null : taskStatusForRun(terminalRun);
+ const latestTerminalSummary = canExposeTaskRunResult(terminalRun)
+ ? terminalRun.id === childRun?.id
+ ? derivedResult
+ : subagentResultForRun(childProjection, terminalRun).text
+ : null;
+ const resultContextTransferId = resultTransfer?.id ?? null;
+ const latestTerminalResultContextTransferId = resultTransferForRun(terminalRun)?.id ?? null;
const response = {
taskId: task.id,
childThreadId: task.childThreadId,
@@ -1464,16 +1471,16 @@ const make = Effect.gen(function* () {
providerInstanceId: task.providerInstanceId,
model: task.model,
summary: derivedResult,
- resultContextTransferId: resultTransfer?.id ?? null,
+ resultContextTransferId,
latestTerminalRunId: terminalRun?.id ?? null,
latestTerminalStatus:
terminalStatus !== null && isTerminalTaskStatus(terminalStatus) ? terminalStatus : null,
- latestTerminalSummary: canExposeTaskRunResult(terminalRun)
- ? terminalRun.id === childRun?.id
- ? derivedResult
- : subagentResultForRun(childProjection, terminalRun).text
- : null,
- latestTerminalResultContextTransferId: resultTransferForRun(terminalRun)?.id ?? null,
+ latestTerminalSummary:
+ latestTerminalSummary === derivedResult ? null : latestTerminalSummary,
+ latestTerminalResultContextTransferId:
+ latestTerminalResultContextTransferId === resultContextTransferId
+ ? null
+ : latestTerminalResultContextTransferId,
hasPendingQuestion:
!isTerminalTaskStatus(status) &&
childControls.runtimeRequests.some(
diff --git a/apps/server/src/mcp/OrchestratorMcpToolkit.integration.test.ts b/apps/server/src/mcp/OrchestratorMcpToolkit.integration.test.ts
index c7b5fc24509..eb639653bfb 100644
--- a/apps/server/src/mcp/OrchestratorMcpToolkit.integration.test.ts
+++ b/apps/server/src/mcp/OrchestratorMcpToolkit.integration.test.ts
@@ -136,16 +136,15 @@ const delegatedTaskStatusTranscriptFile = new URL(
import.meta.url,
);
-const readDelegatedTaskStatusTranscript = Effect.fn("readDelegatedTaskStatusTranscript")(
- function* () {
- const fs = yield* FileSystem.FileSystem;
- const text = yield* fs.readFileString(
- decodeURIComponent(delegatedTaskStatusTranscriptFile.pathname),
- );
- return yield* decodeProviderReplayNdjson(text);
- },
- Effect.provide(NodeServices.layer),
-);
+const readDelegatedTaskStatusTranscript = Effect.fn("readDelegatedTaskStatusTranscript")(function* (
+ followupResult: string,
+) {
+ const fs = yield* FileSystem.FileSystem;
+ const text = yield* fs.readFileString(
+ decodeURIComponent(delegatedTaskStatusTranscriptFile.pathname),
+ );
+ return yield* decodeProviderReplayNdjson(text.replaceAll(queuedFollowupResult, followupResult));
+}, Effect.provide(NodeServices.layer));
interface CapturedTurn {
readonly instanceId: ProviderInstanceId;
@@ -1757,10 +1756,10 @@ describe("orchestrator MCP toolkit", () => {
hasPendingChildRuns: false,
latestTerminalRunId: delegated.childRunId,
latestTerminalStatus: "completed",
- latestTerminalSummary: delegatedResult,
+ latestTerminalSummary: null,
+ latestTerminalResultContextTransferId: null,
});
expect(delegatedStatus.resultContextTransferId).not.toBeNull();
- expect(delegatedStatus.latestTerminalResultContextTransferId).not.toBeNull();
yield* expectOffersToStay(0);
@@ -3990,11 +3989,14 @@ describe("orchestrator MCP toolkit", () => {
),
);
- it.live("reports running and queued child follow-ups from a Codex replay transcript", () =>
+ it.live.each([
+ { label: "distinct", result: queuedFollowupResult, latestSummary: queuedFollowupResult },
+ { label: "identical", result: delegatedResult, latestSummary: null },
+ ])("reports $label child results", ({ result, latestSummary }) =>
Effect.scoped(
Effect.gen(function* () {
const cwd = yield* checkpointWorkspace("delegated-task-status");
- const rawTranscript = yield* readDelegatedTaskStatusTranscript();
+ const rawTranscript = yield* readDelegatedTaskStatusTranscript(result);
const transcript = yield* CodexOrchestratorReplayHarness.decodeTranscript(
materializeReplayTranscriptWorkspace(rawTranscript, cwd),
);
@@ -4163,12 +4165,10 @@ describe("orchestrator MCP toolkit", () => {
hasPendingChildRuns: false,
latestTerminalRunId: delegated.childRunId,
latestTerminalStatus: "completed",
- latestTerminalSummary: delegatedResult,
+ latestTerminalSummary: null,
+ latestTerminalResultContextTransferId: null,
});
expect(delegated.resultContextTransferId).not.toBeNull();
- expect(delegated.latestTerminalResultContextTransferId).toBe(
- delegated.resultContextTransferId,
- );
const delegatedChild = yield* orchestrator.getThreadProjection(delegated.childThreadId);
expect(delegatedChild.thread.lineage.relationshipToParent).toBe("subagent");
@@ -4262,8 +4262,8 @@ describe("orchestrator MCP toolkit", () => {
hasPendingChildRuns: true,
latestTerminalRunId: delegated.childRunId,
latestTerminalStatus: "completed",
- latestTerminalSummary: delegatedResult,
- latestTerminalResultContextTransferId: delegated.resultContextTransferId,
+ latestTerminalSummary: null,
+ latestTerminalResultContextTransferId: null,
});
const finalSequence = yield* orchestrator.getThreadEventSequence(delegated.childThreadId);
@@ -4336,7 +4336,7 @@ describe("orchestrator MCP toolkit", () => {
finishedParent.subagents.find((task) => task.id === followupTasks[1]!.id),
).toMatchObject({
status: "completed",
- result: queuedFollowupResult,
+ result,
});
expect(
finishedParent.contextTransfers.filter(
@@ -4363,9 +4363,18 @@ describe("orchestrator MCP toolkit", () => {
hasPendingChildRuns: false,
latestTerminalRunId: queuedFollowup.runId,
latestTerminalStatus: "completed",
- latestTerminalSummary: queuedFollowupResult,
+ latestTerminalSummary: latestSummary,
});
- expect(finalStatus.latestTerminalResultContextTransferId).not.toBeNull();
+ const followupTransfer = finishedParent.contextTransfers.find(
+ (transfer) =>
+ transfer.type === "subagent_result" &&
+ transfer.sourcePoint.runId === queuedFollowup.runId,
+ );
+ expect(followupTransfer).toBeDefined();
+ expect(finalStatus.latestTerminalResultContextTransferId).toBe(followupTransfer?.id);
+ expect(finalStatus.latestTerminalResultContextTransferId).not.toBe(
+ delegated.resultContextTransferId,
+ );
const read = yield* invoke("t3_thread_read", {
threadId: delegated.childThreadId,
limit: 100,
diff --git a/apps/server/src/mcp/toolkits/orchestrator/tools.ts b/apps/server/src/mcp/toolkits/orchestrator/tools.ts
index f0dabc0260e..bf2d2e01c41 100644
--- a/apps/server/src/mcp/toolkits/orchestrator/tools.ts
+++ b/apps/server/src/mcp/toolkits/orchestrator/tools.ts
@@ -78,7 +78,7 @@ export const DelegateTaskTool = Tool.make("delegate_task", {
const TaskStatusTool = Tool.make("task_status", {
description:
- "Needs an agent running inside a T3 thread. Read a T3-owned delegated task created by this parent thread. childRunId identifies the original delegated run. workState distinguishes working, waiting_for_children, and result_available; a completed turn with live nested work is not a completed task. summary is the final task result, including provider errors on failure, and remains stable after publication. hasPendingChildRuns reports later queued or executing turns in the backing child thread, even after the task is terminal; it does not reopen the task, and task_cancel stops those turns too. Turns held in a stopped queue wait for the user and do not count. latestTerminal* provides later non-monitor turn results. Reading a terminal result acknowledges its automatic parent delivery.",
+ "Needs an agent running inside a T3 thread. Read a T3-owned delegated task created by this parent thread. childRunId identifies the original delegated run. workState distinguishes working, waiting_for_children, and result_available; a completed turn with live nested work is not a completed task. summary is the final task result, including provider errors on failure, and remains stable after publication. hasPendingChildRuns reports later queued or executing turns in the backing child thread, even after the task is terminal; it does not reopen the task, and task_cancel stops those turns too. Turns held in a stopped queue wait for the user and do not count. latestTerminal* provides later non-monitor turn results; latestTerminalSummary and latestTerminalResultContextTransferId are independently null when they exactly repeat summary and resultContextTransferId, respectively. Reading a terminal result acknowledges its automatic parent delivery.",
parameters: OrchestratorMcpTaskStatusInput,
success: OrchestratorMcpDelegateTaskResult,
failure: OrchestratorMcpFailure,