Skip to content

Commit b99cd64

Browse files
fix(server): t3_thread_read results fit MCP clients' output limit
Fixes #33417 Co-Authored-By: Claude Opus 5.5 <[email protected]>
1 parent 82839de commit b99cd64

3 files changed

Lines changed: 240 additions & 52 deletions

File tree

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

Lines changed: 127 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import {
22
EnvironmentId,
33
NodeId,
4+
type OrchestratorMcpThreadReadResult,
45
type OrchestrationV2ThreadProjection,
56
type OrchestrationV2ThreadShell,
67
ProjectId,
@@ -390,6 +391,132 @@ it("readThread pages cut-off text without splitting an emoji across pages", asyn
390391
}).pipe(Effect.provide(layer), Effect.runPromise);
391392
});
392393

394+
it("readThread keeps an activity page within an MCP client's output limit and pages the rest", async () => {
395+
const projection = {
396+
thread: baseThread({
397+
threadId: parentThreadId,
398+
title: "Parent",
399+
instanceId: parentInstanceId,
400+
model: "gpt-5.4",
401+
}),
402+
runs: [makeRun({ id: activeRunId, ordinal: 1, status: "running" })],
403+
visibleTurnItems: [],
404+
providerTurns: [],
405+
runtimeRequests: [],
406+
messages: [],
407+
contextTransfers: [],
408+
subagents: [],
409+
updatedAt: now,
410+
} as unknown as OrchestrationV2ThreadProjection;
411+
const rows = Array.from({ length: 60 }, (_, position) => ({
412+
position,
413+
visibility: "visible",
414+
sourceThreadId: parentThreadId,
415+
sourceItemId: TurnItemId.make(`turn-item-mcp-reasoning-${position}`),
416+
item: {
417+
type: "reasoning",
418+
runId: activeRunId,
419+
status: "completed",
420+
// Command titles can carry a whole command.
421+
title: position === 1 ? "標".repeat(60_000) : null,
422+
text: position === 0 ? "😀a".repeat(30_000) : `${position} `.padEnd(3_000, "x"),
423+
updatedAt: now,
424+
},
425+
}));
426+
427+
const layer = layerOrchestratorService.pipe(
428+
Layer.provide(
429+
Layer.mergeAll(
430+
Layer.mock(ThreadManagementService.ThreadManagementService)({
431+
getTimelinePage: (_threadId, options) => {
432+
const after =
433+
options.itemId === undefined
434+
? rows.filter((row) => row.position > (options.afterPosition ?? -1))
435+
: rows.filter((row) => row.sourceItemId === options.itemId);
436+
return Effect.succeed({
437+
items: after.slice(
438+
0,
439+
options.limit,
440+
) as unknown as OrchestrationV2ThreadProjection["visibleTurnItems"],
441+
totalItems: rows.length,
442+
hasMore: after.length > options.limit,
443+
});
444+
},
445+
getThreadRecords: () => Effect.succeed(projection),
446+
getProjectThreadRecords: () => Effect.succeed(projection),
447+
getThreadShell: () =>
448+
Effect.succeed(projection.thread as unknown as OrchestrationV2ThreadShell),
449+
} satisfies Partial<ThreadManagementService.ThreadManagementService["Service"]>),
450+
ServerSettings.layerTest(),
451+
Layer.mock(ProviderRegistry.ProviderRegistry)({
452+
getProviders: Effect.succeed([]),
453+
} satisfies Partial<ProviderRegistry.ProviderRegistry["Service"]>),
454+
Layer.mock(ProjectService.ProjectService)({}),
455+
Layer.mock(SecretRequests.SecretRequests)({}),
456+
Layer.mock(RemoteDelegation.RemoteDelegation)({}),
457+
Layer.mock(ScheduledTaskService.ScheduledTaskService)({
458+
list: () => Effect.succeed({ tasks: [] }),
459+
} satisfies Partial<ScheduledTaskService.ScheduledTaskService["Service"]>),
460+
Layer.mock(ProviderAdapterRegistry.ProviderAdapterRegistryV2)({
461+
list: () => Effect.succeed([]),
462+
} satisfies Partial<ProviderAdapterRegistry.ProviderAdapterRegistryV2["Service"]>),
463+
NodeCrypto.layer,
464+
),
465+
),
466+
);
467+
468+
await Effect.gen(function* () {
469+
const service = yield* OrchestratorMcpService.OrchestratorMcpService;
470+
const positions: Array<number> = [];
471+
let afterPosition: number | undefined;
472+
for (let page = 0; page < rows.length; page++) {
473+
const result = yield* service.readThread(makeScope(), {
474+
threadId: parentThreadId,
475+
view: "activity",
476+
limit: 100,
477+
maxCharsPerItem: 1_500,
478+
...(afterPosition === undefined ? {} : { afterPosition }),
479+
});
480+
// Claude Code rejects MCP results over 25k tokens; CJK text is about a token per char.
481+
expect(Buffer.byteLength(JSON.stringify(result))).toBeLessThanOrEqual(50_000);
482+
if (page === 0) expect(result.hasMore).toBe(true);
483+
positions.push(...result.items.map((item) => item.position));
484+
if (!result.hasMore) break;
485+
afterPosition = result.nextPosition ?? undefined;
486+
}
487+
expect(positions).toEqual(rows.map((row) => row.position));
488+
489+
const longText = rows[0]!.item.text;
490+
const pages: Array<string> = [];
491+
let textOffset: number | null = 0;
492+
while (textOffset !== null) {
493+
const result: OrchestratorMcpThreadReadResult = yield* service.readThread(makeScope(), {
494+
threadId: parentThreadId,
495+
view: "activity",
496+
itemId: rows[0]!.sourceItemId,
497+
textOffset,
498+
maxCharsPerItem: 50_000,
499+
});
500+
expect(Buffer.byteLength(JSON.stringify(result))).toBeLessThanOrEqual(50_000);
501+
const [item] = result.items;
502+
expect(item?.text?.isWellFormed()).toBe(true);
503+
pages.push(item!.text!);
504+
textOffset = item!.nextTextOffset ?? null;
505+
}
506+
expect(pages.length).toBeGreaterThan(1);
507+
expect(pages.join("")).toBe(longText);
508+
509+
(projection.thread as { title: string }).title = "標".repeat(30_000);
510+
const underHugeTitle = yield* service.readThread(makeScope(), {
511+
threadId: parentThreadId,
512+
view: "activity",
513+
itemId: rows[0]!.sourceItemId,
514+
maxCharsPerItem: 50_000,
515+
});
516+
expect(underHugeTitle.items[0]?.text?.length).toBeGreaterThan(1_000);
517+
}).pipe(Effect.provide(layer), Effect.runPromise);
518+
});
519+
393520
it("taskStatus returns task.providerInstanceId rather than the driver kind", async () => {
394521
const parentProjection = {
395522
thread: baseThread({

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

Lines changed: 105 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -146,6 +146,8 @@ const DEFAULT_THREAD_LIST_LIMIT = 10;
146146
const DEFAULT_THREAD_READ_LIMIT = 10;
147147
const DEFAULT_THREAD_RUN_LIMIT = 10;
148148
const DEFAULT_THREAD_ITEM_MAX_CHARS = 500;
149+
/** Claude Code rejects MCP results over 25k tokens; CJK text runs near one token per char. */
150+
const THREAD_READ_RESULT_MAX_BYTES = 30_000;
149151

150152
interface ResolvedTarget {
151153
readonly modelSelection: ModelSelection;
@@ -631,14 +633,12 @@ function taskResultTransfer(
631633

632634
function pageIncludesTerminalTaskResult(input: {
633635
readonly parentThreadId: ThreadId;
634-
readonly page: ReadonlyArray<OrchestrationV2ThreadProjection["visibleTurnItems"][number]>;
636+
readonly items: ReadonlyArray<OrchestratorMcpThreadTimelineItem>;
635637
readonly task: OrchestrationV2Subagent;
636638
readonly target: Pick<
637639
OrchestrationV2ThreadProjection,
638640
"thread" | "runs" | "contextTransfers" | "messages" | "turnItems" | "providerTurns"
639641
>;
640-
readonly maxChars: number;
641-
readonly textOffset: number;
642642
}): boolean {
643643
const transfer = taskResultTransfer(input.parentThreadId, input.target, input.task);
644644
if (transfer === undefined) return false;
@@ -651,21 +651,14 @@ function pageIncludesTerminalTaskResult(input: {
651651
const result = subagentResultForRun(input.target, run);
652652
if (result.messageId === null && result.turnItemId === null) return false;
653653

654-
return input.page.some((row) => {
655-
if (row.sourceThreadId !== input.target.thread.id) return false;
654+
return input.items.some((item) => {
655+
if (item.sourceThreadId !== input.target.thread.id) return false;
656656
const matchesResult =
657-
(result.turnItemId !== null && row.sourceItemId === result.turnItemId) ||
657+
(result.turnItemId !== null && item.itemId === result.turnItemId) ||
658658
(result.messageId !== null &&
659-
row.item.type === "assistant_message" &&
660-
row.item.messageId === result.messageId);
661-
if (!matchesResult) return false;
662-
663-
const text = turnItemText(row.item);
664-
return (
665-
text !== null &&
666-
(input.textOffset === 0 || input.textOffset < text.length) &&
667-
text.length <= textPageEnd(text, input.textOffset, input.maxChars)
668-
);
659+
item.type === "assistant_message" &&
660+
item.messageId === result.messageId);
661+
return matchesResult && item.text !== null && !item.textTruncated;
669662
});
670663
}
671664

@@ -1098,6 +1091,72 @@ function timelineItem(input: {
10981091
};
10991092
}
11001093

1094+
const jsonBytes = (value: unknown) => Buffer.byteLength(JSON.stringify(value), "utf8");
1095+
1096+
/** The longest prefix length in [low, high] that `fits`, or `low - 1`. */
1097+
function longestFitting(low: number, high: number, fits: (length: number) => boolean): number {
1098+
let best = low - 1;
1099+
while (low <= high) {
1100+
const mid = Math.floor((low + high) / 2);
1101+
if (fits(mid)) {
1102+
best = mid;
1103+
low = mid + 1;
1104+
} else {
1105+
high = mid - 1;
1106+
}
1107+
}
1108+
return best;
1109+
}
1110+
1111+
/** The item with its text cut, and an oversized title too, until its JSON fits `maxBytes`. */
1112+
function timelineItemWithin(
1113+
input: Parameters<typeof timelineItem>[0],
1114+
maxBytes: number,
1115+
): OrchestratorMcpThreadTimelineItem {
1116+
const longestText = (title: string | null) => {
1117+
const withText = (maxChars: number) => ({ ...timelineItem({ ...input, maxChars }), title });
1118+
const chars = longestFitting(2, input.maxChars - 1, (n) => jsonBytes(withText(n)) <= maxBytes);
1119+
return withText(Math.max(chars, 1));
1120+
};
1121+
const item = longestText(input.row.item.title);
1122+
const title = item.title;
1123+
if (title === null || jsonBytes(item) <= maxBytes) return item;
1124+
// Half the budget for the title keeps `textOffset` pages of the text useful.
1125+
const cutTitle = (chars: number) =>
1126+
title.slice(0, chars === 0 ? 0 : textPageEnd(title, 0, chars));
1127+
const titleChars = longestFitting(
1128+
1,
1129+
title.length - 1,
1130+
(n) => jsonBytes({ ...item, title: cutTitle(n) }) <= maxBytes / 2,
1131+
);
1132+
return longestText(cutTitle(Math.max(titleChars, 0)));
1133+
}
1134+
1135+
/** Leading items whose result fits THREAD_READ_RESULT_MAX_BYTES; never empty for a non-empty page. */
1136+
function timelineItemsWithinBudget(
1137+
envelope: Omit<OrchestratorMcpThreadReadResult, "items">,
1138+
inputs: ReadonlyArray<Parameters<typeof timelineItem>[0]>,
1139+
): Array<OrchestratorMcpThreadTimelineItem> {
1140+
// An oversized envelope (say, a huge thread title) must not starve items to one char per read.
1141+
const budget = Math.max(
1142+
THREAD_READ_RESULT_MAX_BYTES - jsonBytes({ ...envelope, items: [] }),
1143+
THREAD_READ_RESULT_MAX_BYTES / 2,
1144+
);
1145+
const items: Array<OrchestratorMcpThreadTimelineItem> = [];
1146+
let bytes = 0;
1147+
for (const input of inputs) {
1148+
const item = timelineItem(input);
1149+
bytes += jsonBytes(item) + (items.length === 0 ? 0 : 1);
1150+
if (bytes <= budget) {
1151+
items.push(item);
1152+
continue;
1153+
}
1154+
if (items.length === 0) items.push(timelineItemWithin(input, budget));
1155+
break;
1156+
}
1157+
return items;
1158+
}
1159+
11011160
const make = Effect.gen(function* () {
11021161
const random = yield* SecureRandom.SecureRandom;
11031162
const threadManagement = yield* ThreadManagementService.ThreadManagementService;
@@ -3093,6 +3152,27 @@ const make = Effect.gen(function* () {
30933152
const returnedRunIds = new Set(
30943153
page.filter((row) => row.sourceThreadId === target.thread.id).map((row) => row.item.runId),
30953154
);
3155+
const thread = threadDetail(
3156+
scope,
3157+
target,
3158+
timeline.totalItems,
3159+
shell,
3160+
yield* Clock.currentTimeMillis,
3161+
input.afterPosition === undefined && input.itemId === undefined,
3162+
);
3163+
const recentRuns = target.runs
3164+
.toSorted((left, right) => right.ordinal - left.ordinal)
3165+
.slice(0, input.runLimit ?? DEFAULT_THREAD_RUN_LIMIT)
3166+
.map((run) => threadRun(target, run));
3167+
const items = timelineItemsWithinBudget(
3168+
{ thread, recentRuns, nextPosition: page.at(-1)?.position ?? null, hasMore: false },
3169+
page.map((row) => ({
3170+
row,
3171+
maxChars,
3172+
messagesByThreadId,
3173+
...(input.itemId === undefined ? {} : { textOffset: input.textOffset ?? 0 }),
3174+
})),
3175+
);
30963176
const tasks =
30973177
parent === undefined
30983178
? []
@@ -3107,7 +3187,9 @@ const make = Effect.gen(function* () {
31073187
target,
31083188
returnedRunIds,
31093189
);
3110-
if (parent !== undefined && scope.thread !== undefined && tasks.length > 0) {
3190+
// A read past the end of an item's text shows the agent nothing to acknowledge.
3191+
const readPastEnd = (input.textOffset ?? 0) > 0 && items.every((item) => !item.text);
3192+
if (parent !== undefined && scope.thread !== undefined && tasks.length > 0 && !readPastEnd) {
31113193
for (const task of tasks) {
31123194
const transfer = taskResultTransfer(parent.thread.id, target, task);
31133195
const resultRunId = transfer?.sourcePoint.runId ?? delegatedTaskRun(target, task)?.id;
@@ -3123,11 +3205,9 @@ const make = Effect.gen(function* () {
31233205
if (
31243206
pageIncludesTerminalTaskResult({
31253207
parentThreadId: parent.thread.id,
3126-
page,
3208+
items,
31273209
task,
31283210
target: { ...target, ...resultRecords },
3129-
maxChars,
3130-
textOffset: input.itemId === undefined ? 0 : (input.textOffset ?? 0),
31313211
})
31323212
) {
31333213
yield* readTask(
@@ -3141,28 +3221,11 @@ const make = Effect.gen(function* () {
31413221
}
31423222
}
31433223
return {
3144-
thread: threadDetail(
3145-
scope,
3146-
target,
3147-
timeline.totalItems,
3148-
shell,
3149-
yield* Clock.currentTimeMillis,
3150-
input.afterPosition === undefined && input.itemId === undefined,
3151-
),
3152-
recentRuns: target.runs
3153-
.toSorted((left, right) => right.ordinal - left.ordinal)
3154-
.slice(0, input.runLimit ?? DEFAULT_THREAD_RUN_LIMIT)
3155-
.map((run) => threadRun(target, run)),
3156-
items: page.map((row) =>
3157-
timelineItem({
3158-
row,
3159-
maxChars,
3160-
messagesByThreadId,
3161-
...(input.itemId === undefined ? {} : { textOffset: input.textOffset ?? 0 }),
3162-
}),
3163-
),
3164-
nextPosition: page.at(-1)?.position ?? null,
3165-
hasMore: timeline.hasMore,
3224+
thread,
3225+
recentRuns,
3226+
items,
3227+
nextPosition: items.at(-1)?.position ?? null,
3228+
hasMore: timeline.hasMore || items.length < page.length,
31663229
} satisfies OrchestratorMcpThreadReadResult;
31673230
}),
31683231
sendToThread: Effect.fn("OrchestratorMcpService.sendToThread")(function* (scope, input) {

0 commit comments

Comments
 (0)