Skip to content

Commit febd377

Browse files
MarcoDotIOclaudemaria-rcks
authored
fix(server): t3_thread_read pages no longer split an emoji in half (#27273)
Co-authored-by: Claude Opus 5.5 <[email protected]> Co-authored-by: maria-rcks <[email protected]>
1 parent e2ad07a commit febd377

3 files changed

Lines changed: 111 additions & 3 deletions

File tree

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

Lines changed: 93 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ import {
99
RunId,
1010
ScheduledTaskId,
1111
ThreadId,
12+
TurnItemId,
1213
} from "@t3tools/contracts";
1314
import * as DateTime from "effect/DateTime";
1415
import * as Effect from "effect/Effect";
@@ -205,6 +206,98 @@ it("readThread preserves caller access errors and prefers activity-run status ov
205206
}).pipe(Effect.provide(layer), Effect.runPromise);
206207
});
207208

209+
it("readThread pages cut-off text without splitting an emoji across pages", async () => {
210+
const text = "a😀bc";
211+
const itemId = TurnItemId.make("turn-item-mcp-reasoning");
212+
const projection = {
213+
thread: baseThread({
214+
threadId: parentThreadId,
215+
title: "Parent",
216+
instanceId: parentInstanceId,
217+
model: "gpt-5.4",
218+
}),
219+
runs: [makeRun({ id: activeRunId, ordinal: 1, status: "running" })],
220+
visibleTurnItems: [],
221+
runtimeRequests: [],
222+
messages: [],
223+
contextTransfers: [],
224+
subagents: [],
225+
updatedAt: now,
226+
} as unknown as OrchestrationV2ThreadProjection;
227+
const row = {
228+
position: 0,
229+
visibility: "visible",
230+
sourceThreadId: parentThreadId,
231+
sourceItemId: itemId,
232+
item: {
233+
type: "reasoning",
234+
runId: activeRunId,
235+
status: "completed",
236+
title: null,
237+
text,
238+
updatedAt: now,
239+
},
240+
};
241+
242+
const layer = layerOrchestratorService.pipe(
243+
Layer.provide(
244+
Layer.mergeAll(
245+
Layer.mock(ThreadManagementService.ThreadManagementService)({
246+
getTimelinePage: () =>
247+
Effect.succeed({
248+
items: [row] as unknown as OrchestrationV2ThreadProjection["visibleTurnItems"],
249+
totalItems: 1,
250+
hasMore: false,
251+
}),
252+
getThreadRecords: () => Effect.succeed(projection),
253+
getProjectThreadRecords: () => Effect.succeed(projection),
254+
getThreadShell: () =>
255+
Effect.succeed(projection.thread as unknown as OrchestrationV2ThreadShell),
256+
} satisfies Partial<ThreadManagementService.ThreadManagementService["Service"]>),
257+
ServerSettings.layerTest(),
258+
Layer.mock(ProviderRegistry.ProviderRegistry)({
259+
getProviders: Effect.succeed([]),
260+
} satisfies Partial<ProviderRegistry.ProviderRegistry["Service"]>),
261+
Layer.mock(ProjectService.ProjectService)({}),
262+
Layer.mock(SecretRequests.SecretRequests)({}),
263+
Layer.mock(RemoteDelegation.RemoteDelegation)({}),
264+
Layer.mock(ScheduledTaskService.ScheduledTaskService)({
265+
list: () => Effect.succeed({ tasks: [] }),
266+
} satisfies Partial<ScheduledTaskService.ScheduledTaskService["Service"]>),
267+
Layer.mock(ProviderAdapterRegistry.ProviderAdapterRegistryV2)({
268+
list: () => Effect.succeed([]),
269+
} satisfies Partial<ProviderAdapterRegistry.ProviderAdapterRegistryV2["Service"]>),
270+
NodeCrypto.layer,
271+
),
272+
),
273+
);
274+
275+
await Effect.gen(function* () {
276+
const service = yield* OrchestratorMcpService.OrchestratorMcpService;
277+
for (const [maxCharsPerItem, expected] of [
278+
[2, ["a", "😀", "bc"]],
279+
[1, ["a", "😀", "b", "c"]],
280+
] as const) {
281+
let textOffset = 0;
282+
for (const [index, text] of expected.entries()) {
283+
const result = yield* service.readThread(makeScope(), {
284+
threadId: parentThreadId,
285+
view: "activity",
286+
itemId,
287+
textOffset,
288+
maxCharsPerItem,
289+
});
290+
textOffset += text.length;
291+
expect(result.items[0]).toMatchObject({
292+
text,
293+
textTruncated: index < expected.length - 1,
294+
nextTextOffset: index < expected.length - 1 ? textOffset : null,
295+
});
296+
}
297+
}
298+
}).pipe(Effect.provide(layer), Effect.runPromise);
299+
});
300+
208301
it("taskStatus returns task.providerInstanceId rather than the driver kind", async () => {
209302
const parentProjection = {
210303
thread: baseThread({

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

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -631,7 +631,7 @@ function pageIncludesTerminalTaskResult(input: {
631631
if (!matchesResult) return false;
632632

633633
const text = turnItemText(row.item);
634-
return text !== null && text.length <= input.maxChars;
634+
return text !== null && text.length <= textPageEnd(text, 0, input.maxChars);
635635
});
636636
}
637637

@@ -948,6 +948,15 @@ function turnItemText(item: OrchestrationV2TurnItem): string | null {
948948
}
949949
}
950950

951+
/** A cut inside a surrogate pair would leave half an emoji on each page. */
952+
function textPageEnd(text: string | null, offset: number, maxChars: number): number {
953+
const end = offset + maxChars;
954+
if (text === null || end >= text.length) return end;
955+
const last = text.charCodeAt(end - 1);
956+
if (last < 0xd800 || last > 0xdbff) return end;
957+
return end - 1 > offset ? end - 1 : end + 1;
958+
}
959+
951960
function timelineItem(input: {
952961
readonly row: OrchestrationV2ThreadProjection["visibleTurnItems"][number];
953962
readonly maxChars: number;
@@ -956,7 +965,7 @@ function timelineItem(input: {
956965
}): OrchestratorMcpThreadTimelineItem {
957966
const text = turnItemText(input.row.item);
958967
const offset = input.textOffset ?? 0;
959-
const end = offset + input.maxChars;
968+
const end = textPageEnd(text, offset, input.maxChars);
960969
const textTruncated = text !== null && text.length > end;
961970
const messageId =
962971
input.row.item.type === "user_message" || input.row.item.type === "assistant_message"

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

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -195,6 +195,7 @@ describe("orchestrator MCP toolkit", () => {
195195
it.live.each([
196196
{ label: "distinct", result: queuedFollowupResult, latestSummary: queuedFollowupResult },
197197
{ label: "identical", result: delegatedResult, latestSummary: null },
198+
{ label: "emoji", result: "😀", latestSummary: "😀" },
198199
])("reports $label child results", ({ result, latestSummary }) =>
199200
Effect.scoped(
200201
Effect.gen(function* () {
@@ -660,7 +661,11 @@ describe("orchestrator MCP toolkit", () => {
660661
).toHaveLength(1);
661662
const parentlessRead = yield* invoke(
662663
"t3_thread_read",
663-
{ threadId: delegated.childThreadId, limit: 100 },
664+
{
665+
threadId: delegated.childThreadId,
666+
limit: 100,
667+
maxCharsPerItem: result === "😀" ? 1 : 20_000,
668+
},
664669
{ ...invocation, thread: undefined },
665670
).pipe(Effect.withTracer(tracer));
666671
expect(parentlessRead.isError).not.toBe(true);
@@ -674,6 +679,7 @@ describe("orchestrator MCP toolkit", () => {
674679
const read = yield* invoke("t3_thread_read", {
675680
threadId: delegated.childThreadId,
676681
limit: 100,
682+
maxCharsPerItem: result === "😀" ? 1 : 20_000,
677683
}).pipe(Effect.withTracer(tracer));
678684
expect(read.isError).not.toBe(true);
679685
expect(transferReads.length).toBeGreaterThan(0);

0 commit comments

Comments
 (0)