Skip to content

Commit 1334965

Browse files
maria-rcksvoltcrash
authored andcommitted
fix(server): acknowledge delegated results after text continuation
1 parent a4bceee commit 1334965

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
@@ -617,6 +617,7 @@ function pageIncludesTerminalTaskResult(input: {
617617
"thread" | "runs" | "contextTransfers" | "messages" | "turnItems"
618618
>;
619619
readonly maxChars: number;
620+
readonly textOffset: number;
620621
}): boolean {
621622
const transfer = taskResultTransfer(input.parent, input.target, input.task);
622623
if (transfer === undefined) return false;
@@ -639,7 +640,11 @@ function pageIncludesTerminalTaskResult(input: {
639640
if (!matchesResult) return false;
640641

641642
const text = turnItemText(row.item);
642-
return text !== null && text.length <= textPageEnd(text, 0, input.maxChars);
643+
return (
644+
text !== null &&
645+
(input.textOffset === 0 || input.textOffset < text.length) &&
646+
text.length <= textPageEnd(text, input.textOffset, input.maxChars)
647+
);
643648
});
644649
}
645650

@@ -3079,12 +3084,7 @@ const make = Effect.gen(function* () {
30793084
target,
30803085
returnedRunIds,
30813086
);
3082-
if (
3083-
parent !== undefined &&
3084-
scope.thread !== undefined &&
3085-
tasks.length > 0 &&
3086-
(input.textOffset ?? 0) === 0
3087-
) {
3087+
if (parent !== undefined && scope.thread !== undefined && tasks.length > 0) {
30883088
for (const task of tasks) {
30893089
const transfer = taskResultTransfer(parent, target, task);
30903090
const resultRunId = transfer?.sourcePoint.runId ?? delegatedTaskRun(target, task)?.id;
@@ -3104,6 +3104,7 @@ const make = Effect.gen(function* () {
31043104
task,
31053105
target: { ...target, ...resultRecords },
31063106
maxChars,
3107+
textOffset: input.itemId === undefined ? 0 : (input.textOffset ?? 0),
31073108
})
31083109
) {
31093110
yield* readTask(

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

Lines changed: 38 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ import {
1111
OrchestratorMcpDelegateTaskResult,
1212
OrchestratorMcpThreadInterruptResult,
1313
OrchestratorMcpThreadSendResult,
14+
OrchestratorMcpThreadReadResult,
1415
ProjectId,
1516
ProviderDriverKind,
1617
ProviderInstanceId,
@@ -79,6 +80,7 @@ const delegatedResult = "Delegated API boundary inspected.";
7980
const cancellationPrompt = "Remain active until the parent cancels this delegated task.";
8081
const queuedFollowupPrompt = "Complete the queued follow-up and return the final result.";
8182
const queuedFollowupResult = "Queued delegated follow-up completed.";
83+
const decodeThreadRead = Schema.decodeUnknownEffect(OrchestratorMcpThreadReadResult);
8284

8385
const decodeDelegateTaskResult = Schema.decodeUnknownEffect(OrchestratorMcpDelegateTaskResult);
8486
const decodeThreadInterruptResult = Schema.decodeUnknownEffect(
@@ -199,6 +201,11 @@ describe("orchestrator MCP toolkit", () => {
199201
{ label: "distinct", result: queuedFollowupResult, latestSummary: queuedFollowupResult },
200202
{ label: "identical", result: delegatedResult, latestSummary: null },
201203
{ label: "emoji", result: "😀", latestSummary: "😀" },
204+
{
205+
label: "paginated",
206+
result: "git safety ".repeat(120),
207+
latestSummary: "git safety ".repeat(120),
208+
},
202209
])("reports $label child results", ({ result, latestSummary }) =>
203210
Effect.scoped(
204211
Effect.gen(function* () {
@@ -682,11 +689,40 @@ describe("orchestrator MCP toolkit", () => {
682689
const read = yield* invoke("t3_thread_read", {
683690
threadId: delegated.childThreadId,
684691
limit: 100,
685-
maxCharsPerItem: result === "😀" ? 1 : 20_000,
692+
...(result === "😀" ? { maxCharsPerItem: 1 } : {}),
686693
}).pipe(Effect.withTracer(tracer));
687694
expect(read.isError).not.toBe(true);
688695
expect(transferReads.length).toBeGreaterThan(0);
689-
expect(read.structuredContent).toEqual(parentlessRead.structuredContent);
696+
const firstPage = yield* decodeThreadRead(read.structuredContent);
697+
const finalItem = firstPage.items.find((item) => item.text === result.slice(0, 500));
698+
expect(finalItem).toBeDefined();
699+
if (result.length > 500) {
700+
yield* invoke("t3_thread_read", {
701+
threadId: delegated.childThreadId,
702+
itemId: finalItem!.itemId,
703+
textOffset: result.length,
704+
});
705+
} else {
706+
expect(read.structuredContent).toEqual(parentlessRead.structuredContent);
707+
}
708+
let text = finalItem!.text ?? "";
709+
let nextTextOffset = finalItem?.nextTextOffset ?? null;
710+
while (nextTextOffset !== null) {
711+
const beforeContinuation = yield* orchestrator.getThreadProjection(parentThreadId);
712+
expect(
713+
beforeContinuation.subagents.find((task) => task.id === queuedFollowup.taskId)
714+
?.completionDelivery?.state,
715+
).toBe("claimed");
716+
const continuation = yield* invoke("t3_thread_read", {
717+
threadId: delegated.childThreadId,
718+
itemId: finalItem!.itemId,
719+
textOffset: nextTextOffset,
720+
});
721+
const nextPage = yield* decodeThreadRead(continuation.structuredContent);
722+
text += nextPage.items[0]?.text ?? "";
723+
nextTextOffset = nextPage.items[0]?.nextTextOffset ?? null;
724+
}
725+
expect(text).toBe(result);
690726
const afterRead = yield* orchestrator.getThreadProjection(parentThreadId);
691727
expect(
692728
afterRead.subagents.find((task) => task.id === queuedFollowup.taskId)

0 commit comments

Comments
 (0)