Skip to content

Commit cf8578f

Browse files
committed
fix(server): acknowledge delegated results after text continuation
1 parent 501daf4 commit cf8578f

2 files changed

Lines changed: 46 additions & 9 deletions

File tree

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

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -609,6 +609,7 @@ function pageIncludesTerminalTaskResult(input: {
609609
"thread" | "runs" | "contextTransfers" | "messages" | "turnItems"
610610
>;
611611
readonly maxChars: number;
612+
readonly textOffset: number;
612613
}): boolean {
613614
const transfer = taskResultTransfer(input.parent, input.target, input.task);
614615
if (transfer === undefined) return false;
@@ -631,7 +632,11 @@ function pageIncludesTerminalTaskResult(input: {
631632
if (!matchesResult) return false;
632633

633634
const text = turnItemText(row.item);
634-
return text !== null && text.length <= textPageEnd(text, 0, input.maxChars);
635+
return (
636+
text !== null &&
637+
(input.textOffset === 0 || input.textOffset < text.length) &&
638+
text.length <= textPageEnd(text, input.textOffset, input.maxChars)
639+
);
635640
});
636641
}
637642

@@ -3002,12 +3007,7 @@ const make = Effect.gen(function* () {
30023007
target,
30033008
returnedRunIds,
30043009
);
3005-
if (
3006-
parent !== undefined &&
3007-
scope.thread !== undefined &&
3008-
tasks.length > 0 &&
3009-
(input.textOffset ?? 0) === 0
3010-
) {
3010+
if (parent !== undefined && scope.thread !== undefined && tasks.length > 0) {
30113011
for (const task of tasks) {
30123012
const transfer = taskResultTransfer(parent, target, task);
30133013
const resultRunId = transfer?.sourcePoint.runId ?? delegatedTaskRun(target, task)?.id;
@@ -3027,6 +3027,7 @@ const make = Effect.gen(function* () {
30273027
task,
30283028
target: { ...target, ...resultRecords },
30293029
maxChars,
3030+
textOffset: input.itemId === undefined ? 0 : (input.textOffset ?? 0),
30303031
})
30313032
) {
30323033
yield* readTask(

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

Lines changed: 38 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ import {
1010
OrchestratorMcpDelegateTaskResult,
1111
OrchestratorMcpThreadInterruptResult,
1212
OrchestratorMcpThreadSendResult,
13+
OrchestratorMcpThreadReadResult,
1314
ProjectId,
1415
ProviderDriverKind,
1516
ProviderInstanceId,
@@ -77,6 +78,7 @@ const delegatedResult = "Delegated API boundary inspected.";
7778
const cancellationPrompt = "Remain active until the parent cancels this delegated task.";
7879
const queuedFollowupPrompt = "Complete the queued follow-up and return the final result.";
7980
const queuedFollowupResult = "Queued delegated follow-up completed.";
81+
const decodeThreadRead = Schema.decodeUnknownEffect(OrchestratorMcpThreadReadResult);
8082

8183
const decodeDelegateTaskResult = Schema.decodeUnknownEffect(OrchestratorMcpDelegateTaskResult);
8284
const decodeThreadInterruptResult = Schema.decodeUnknownEffect(
@@ -196,6 +198,11 @@ describe("orchestrator MCP toolkit", () => {
196198
{ label: "distinct", result: queuedFollowupResult, latestSummary: queuedFollowupResult },
197199
{ label: "identical", result: delegatedResult, latestSummary: null },
198200
{ label: "emoji", result: "😀", latestSummary: "😀" },
201+
{
202+
label: "paginated",
203+
result: "git safety ".repeat(120),
204+
latestSummary: "git safety ".repeat(120),
205+
},
199206
])("reports $label child results", ({ result, latestSummary }) =>
200207
Effect.scoped(
201208
Effect.gen(function* () {
@@ -678,11 +685,40 @@ describe("orchestrator MCP toolkit", () => {
678685
const read = yield* invoke("t3_thread_read", {
679686
threadId: delegated.childThreadId,
680687
limit: 100,
681-
maxCharsPerItem: result === "😀" ? 1 : 20_000,
688+
...(result === "😀" ? { maxCharsPerItem: 1 } : {}),
682689
}).pipe(Effect.withTracer(tracer));
683690
expect(read.isError).not.toBe(true);
684691
expect(transferReads.length).toBeGreaterThan(0);
685-
expect(read.structuredContent).toEqual(parentlessRead.structuredContent);
692+
const firstPage = yield* decodeThreadRead(read.structuredContent);
693+
const finalItem = firstPage.items.find((item) => item.text === result.slice(0, 500));
694+
expect(finalItem).toBeDefined();
695+
if (result.length > 500) {
696+
yield* invoke("t3_thread_read", {
697+
threadId: delegated.childThreadId,
698+
itemId: finalItem!.itemId,
699+
textOffset: result.length,
700+
});
701+
} else {
702+
expect(read.structuredContent).toEqual(parentlessRead.structuredContent);
703+
}
704+
let text = finalItem!.text ?? "";
705+
let nextTextOffset = finalItem?.nextTextOffset ?? null;
706+
while (nextTextOffset !== null) {
707+
const beforeContinuation = yield* orchestrator.getThreadProjection(parentThreadId);
708+
expect(
709+
beforeContinuation.subagents.find((task) => task.id === queuedFollowup.taskId)
710+
?.completionDelivery?.state,
711+
).toBe("claimed");
712+
const continuation = yield* invoke("t3_thread_read", {
713+
threadId: delegated.childThreadId,
714+
itemId: finalItem!.itemId,
715+
textOffset: nextTextOffset,
716+
});
717+
const nextPage = yield* decodeThreadRead(continuation.structuredContent);
718+
text += nextPage.items[0]?.text ?? "";
719+
nextTextOffset = nextPage.items[0]?.nextTextOffset ?? null;
720+
}
721+
expect(text).toBe(result);
686722
const afterRead = yield* orchestrator.getThreadProjection(parentThreadId);
687723
expect(
688724
afterRead.subagents.find((task) => task.id === queuedFollowup.taskId)

0 commit comments

Comments
 (0)