Skip to content

Commit 3eaebac

Browse files
fix(server): stop shipping delegated task results twice (pingdotgg#19759)
Co-authored-by: Julius Marminge <[email protected]>
1 parent 7208335 commit 3eaebac

3 files changed

Lines changed: 47 additions & 31 deletions

File tree

‎apps/server/src/mcp/OrchestratorMcpService.ts‎

Lines changed: 14 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1453,6 +1453,13 @@ const make = Effect.gen(function* () {
14531453
null);
14541454
const resultTransfer = taskResultTransfer(parentProjection, childProjection, task) ?? null;
14551455
const terminalStatus = terminalRun === undefined ? null : taskStatusForRun(terminalRun);
1456+
const latestTerminalSummary = canExposeTaskRunResult(terminalRun)
1457+
? terminalRun.id === childRun?.id
1458+
? derivedResult
1459+
: subagentResultForRun(childProjection, terminalRun).text
1460+
: null;
1461+
const resultContextTransferId = resultTransfer?.id ?? null;
1462+
const latestTerminalResultContextTransferId = resultTransferForRun(terminalRun)?.id ?? null;
14561463
const response = {
14571464
taskId: task.id,
14581465
childThreadId: task.childThreadId,
@@ -1464,16 +1471,16 @@ const make = Effect.gen(function* () {
14641471
providerInstanceId: task.providerInstanceId,
14651472
model: task.model,
14661473
summary: derivedResult,
1467-
resultContextTransferId: resultTransfer?.id ?? null,
1474+
resultContextTransferId,
14681475
latestTerminalRunId: terminalRun?.id ?? null,
14691476
latestTerminalStatus:
14701477
terminalStatus !== null && isTerminalTaskStatus(terminalStatus) ? terminalStatus : null,
1471-
latestTerminalSummary: canExposeTaskRunResult(terminalRun)
1472-
? terminalRun.id === childRun?.id
1473-
? derivedResult
1474-
: subagentResultForRun(childProjection, terminalRun).text
1475-
: null,
1476-
latestTerminalResultContextTransferId: resultTransferForRun(terminalRun)?.id ?? null,
1478+
latestTerminalSummary:
1479+
latestTerminalSummary === derivedResult ? null : latestTerminalSummary,
1480+
latestTerminalResultContextTransferId:
1481+
latestTerminalResultContextTransferId === resultContextTransferId
1482+
? null
1483+
: latestTerminalResultContextTransferId,
14771484
hasPendingQuestion:
14781485
!isTerminalTaskStatus(status) &&
14791486
childControls.runtimeRequests.some(

‎apps/server/src/mcp/OrchestratorMcpToolkit.integration.test.ts‎

Lines changed: 32 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -136,16 +136,15 @@ const delegatedTaskStatusTranscriptFile = new URL(
136136
import.meta.url,
137137
);
138138

139-
const readDelegatedTaskStatusTranscript = Effect.fn("readDelegatedTaskStatusTranscript")(
140-
function* () {
141-
const fs = yield* FileSystem.FileSystem;
142-
const text = yield* fs.readFileString(
143-
decodeURIComponent(delegatedTaskStatusTranscriptFile.pathname),
144-
);
145-
return yield* decodeProviderReplayNdjson(text);
146-
},
147-
Effect.provide(NodeServices.layer),
148-
);
139+
const readDelegatedTaskStatusTranscript = Effect.fn("readDelegatedTaskStatusTranscript")(function* (
140+
followupResult: string,
141+
) {
142+
const fs = yield* FileSystem.FileSystem;
143+
const text = yield* fs.readFileString(
144+
decodeURIComponent(delegatedTaskStatusTranscriptFile.pathname),
145+
);
146+
return yield* decodeProviderReplayNdjson(text.replaceAll(queuedFollowupResult, followupResult));
147+
}, Effect.provide(NodeServices.layer));
149148

150149
interface CapturedTurn {
151150
readonly instanceId: ProviderInstanceId;
@@ -1757,10 +1756,10 @@ describe("orchestrator MCP toolkit", () => {
17571756
hasPendingChildRuns: false,
17581757
latestTerminalRunId: delegated.childRunId,
17591758
latestTerminalStatus: "completed",
1760-
latestTerminalSummary: delegatedResult,
1759+
latestTerminalSummary: null,
1760+
latestTerminalResultContextTransferId: null,
17611761
});
17621762
expect(delegatedStatus.resultContextTransferId).not.toBeNull();
1763-
expect(delegatedStatus.latestTerminalResultContextTransferId).not.toBeNull();
17641763

17651764
yield* expectOffersToStay(0);
17661765

@@ -3990,11 +3989,14 @@ describe("orchestrator MCP toolkit", () => {
39903989
),
39913990
);
39923991

3993-
it.live("reports running and queued child follow-ups from a Codex replay transcript", () =>
3992+
it.live.each([
3993+
{ label: "distinct", result: queuedFollowupResult, latestSummary: queuedFollowupResult },
3994+
{ label: "identical", result: delegatedResult, latestSummary: null },
3995+
])("reports $label child results", ({ result, latestSummary }) =>
39943996
Effect.scoped(
39953997
Effect.gen(function* () {
39963998
const cwd = yield* checkpointWorkspace("delegated-task-status");
3997-
const rawTranscript = yield* readDelegatedTaskStatusTranscript();
3999+
const rawTranscript = yield* readDelegatedTaskStatusTranscript(result);
39984000
const transcript = yield* CodexOrchestratorReplayHarness.decodeTranscript(
39994001
materializeReplayTranscriptWorkspace(rawTranscript, cwd),
40004002
);
@@ -4163,12 +4165,10 @@ describe("orchestrator MCP toolkit", () => {
41634165
hasPendingChildRuns: false,
41644166
latestTerminalRunId: delegated.childRunId,
41654167
latestTerminalStatus: "completed",
4166-
latestTerminalSummary: delegatedResult,
4168+
latestTerminalSummary: null,
4169+
latestTerminalResultContextTransferId: null,
41674170
});
41684171
expect(delegated.resultContextTransferId).not.toBeNull();
4169-
expect(delegated.latestTerminalResultContextTransferId).toBe(
4170-
delegated.resultContextTransferId,
4171-
);
41724172

41734173
const delegatedChild = yield* orchestrator.getThreadProjection(delegated.childThreadId);
41744174
expect(delegatedChild.thread.lineage.relationshipToParent).toBe("subagent");
@@ -4262,8 +4262,8 @@ describe("orchestrator MCP toolkit", () => {
42624262
hasPendingChildRuns: true,
42634263
latestTerminalRunId: delegated.childRunId,
42644264
latestTerminalStatus: "completed",
4265-
latestTerminalSummary: delegatedResult,
4266-
latestTerminalResultContextTransferId: delegated.resultContextTransferId,
4265+
latestTerminalSummary: null,
4266+
latestTerminalResultContextTransferId: null,
42674267
});
42684268

42694269
const finalSequence = yield* orchestrator.getThreadEventSequence(delegated.childThreadId);
@@ -4336,7 +4336,7 @@ describe("orchestrator MCP toolkit", () => {
43364336
finishedParent.subagents.find((task) => task.id === followupTasks[1]!.id),
43374337
).toMatchObject({
43384338
status: "completed",
4339-
result: queuedFollowupResult,
4339+
result,
43404340
});
43414341
expect(
43424342
finishedParent.contextTransfers.filter(
@@ -4363,9 +4363,18 @@ describe("orchestrator MCP toolkit", () => {
43634363
hasPendingChildRuns: false,
43644364
latestTerminalRunId: queuedFollowup.runId,
43654365
latestTerminalStatus: "completed",
4366-
latestTerminalSummary: queuedFollowupResult,
4366+
latestTerminalSummary: latestSummary,
43674367
});
4368-
expect(finalStatus.latestTerminalResultContextTransferId).not.toBeNull();
4368+
const followupTransfer = finishedParent.contextTransfers.find(
4369+
(transfer) =>
4370+
transfer.type === "subagent_result" &&
4371+
transfer.sourcePoint.runId === queuedFollowup.runId,
4372+
);
4373+
expect(followupTransfer).toBeDefined();
4374+
expect(finalStatus.latestTerminalResultContextTransferId).toBe(followupTransfer?.id);
4375+
expect(finalStatus.latestTerminalResultContextTransferId).not.toBe(
4376+
delegated.resultContextTransferId,
4377+
);
43694378
const read = yield* invoke("t3_thread_read", {
43704379
threadId: delegated.childThreadId,
43714380
limit: 100,

‎apps/server/src/mcp/toolkits/orchestrator/tools.ts‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -78,7 +78,7 @@ export const DelegateTaskTool = Tool.make("delegate_task", {
7878

7979
const TaskStatusTool = Tool.make("task_status", {
8080
description:
81-
"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.",
81+
"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.",
8282
parameters: OrchestratorMcpTaskStatusInput,
8383
success: OrchestratorMcpDelegateTaskResult,
8484
failure: OrchestratorMcpFailure,

0 commit comments

Comments
 (0)