Skip to content

Commit 719703e

Browse files
MarcoDotIOclaude
andcommitted
fix(server): t3_thread_read no longer splits an emoji across text pages
When an item's text was longer than maxCharsPerItem, the page ended at a UTF-16 index. An emoji or other astral character on the boundary was cut in half: the page ended with a lone high surrogate and the next page, read from nextTextOffset, started with the lone low one. End the page before the pair instead, or take the whole pair when the page would otherwise be empty, so paging still makes progress. Co-Authored-By: Claude Opus 5.5 <[email protected]>
1 parent 812c0a7 commit 719703e

2 files changed

Lines changed: 101 additions & 1 deletion

File tree

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

Lines changed: 91 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,
@@ -9,6 +10,7 @@ import {
910
RunId,
1011
ScheduledTaskId,
1112
ThreadId,
13+
TurnItemId,
1214
} from "@t3tools/contracts";
1315
import * as DateTime from "effect/DateTime";
1416
import * as Effect from "effect/Effect";
@@ -183,6 +185,95 @@ it("readThread prefers activity-run status over a newer cancelled queued run", a
183185
}).pipe(Effect.provide(layer), Effect.runPromise);
184186
});
185187

188+
it("readThread pages cut-off text without splitting an emoji across pages", async () => {
189+
const text = "a😀bc";
190+
const itemId = TurnItemId.make("turn-item-mcp-reasoning");
191+
const projection = {
192+
thread: baseThread({
193+
threadId: parentThreadId,
194+
title: "Parent",
195+
instanceId: parentInstanceId,
196+
model: "gpt-5.4",
197+
}),
198+
runs: [makeRun({ id: activeRunId, ordinal: 1, status: "running" })],
199+
visibleTurnItems: [],
200+
runtimeRequests: [],
201+
messages: [],
202+
contextTransfers: [],
203+
subagents: [],
204+
updatedAt: now,
205+
} as unknown as OrchestrationV2ThreadProjection;
206+
const row = {
207+
position: 0,
208+
visibility: "visible",
209+
sourceThreadId: parentThreadId,
210+
sourceItemId: itemId,
211+
item: {
212+
type: "reasoning",
213+
runId: activeRunId,
214+
status: "completed",
215+
title: null,
216+
text,
217+
updatedAt: now,
218+
},
219+
};
220+
221+
const layer = layerOrchestratorService.pipe(
222+
Layer.provide(
223+
Layer.mergeAll(
224+
Layer.mock(ThreadManagementService.ThreadManagementService)({
225+
getTimelinePage: () =>
226+
Effect.succeed({
227+
items: [row] as unknown as OrchestrationV2ThreadProjection["visibleTurnItems"],
228+
totalItems: 1,
229+
hasMore: false,
230+
}),
231+
getThreadRecords: () => Effect.succeed(projection),
232+
getProjectThreadRecords: () => Effect.succeed(projection),
233+
getThreadShell: () =>
234+
Effect.succeed(projection.thread as unknown as OrchestrationV2ThreadShell),
235+
} satisfies Partial<ThreadManagementService.ThreadManagementService["Service"]>),
236+
ServerSettings.layerTest(),
237+
Layer.mock(ProviderRegistry.ProviderRegistry)({
238+
getProviders: Effect.succeed([]),
239+
} satisfies Partial<ProviderRegistry.ProviderRegistry["Service"]>),
240+
Layer.mock(ProjectService.ProjectService)({}),
241+
Layer.mock(SecretRequests.SecretRequests)({}),
242+
Layer.mock(RemoteDelegation.RemoteDelegation)({}),
243+
Layer.mock(ScheduledTaskService.ScheduledTaskService)({
244+
list: () => Effect.succeed({ tasks: [] }),
245+
} satisfies Partial<ScheduledTaskService.ScheduledTaskService["Service"]>),
246+
Layer.mock(ProviderAdapterRegistry.ProviderAdapterRegistryV2)({
247+
list: () => Effect.succeed([]),
248+
} satisfies Partial<ProviderAdapterRegistry.ProviderAdapterRegistryV2["Service"]>),
249+
NodeCrypto.layer,
250+
),
251+
),
252+
);
253+
254+
await Effect.gen(function* () {
255+
const service = yield* OrchestratorMcpService.OrchestratorMcpService;
256+
const pages = Effect.fn(function* (maxCharsPerItem: number) {
257+
const read: Array<string | null> = [];
258+
let textOffset: number | null = 0;
259+
while (textOffset !== null) {
260+
const result: OrchestratorMcpThreadReadResult = yield* service.readThread(makeScope(), {
261+
threadId: parentThreadId,
262+
view: "activity",
263+
itemId,
264+
textOffset,
265+
maxCharsPerItem,
266+
});
267+
read.push(result.items[0]!.text);
268+
textOffset = result.items[0]!.nextTextOffset ?? null;
269+
}
270+
return read;
271+
});
272+
expect(yield* pages(2)).toEqual(["a", "😀", "bc"]);
273+
expect(yield* pages(1)).toEqual(["a", "😀", "b", "c"]);
274+
}).pipe(Effect.provide(layer), Effect.runPromise);
275+
});
276+
186277
it("taskStatus returns task.providerInstanceId rather than the driver kind", async () => {
187278
const parentProjection = {
188279
thread: baseThread({

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

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -919,6 +919,15 @@ function turnItemText(item: OrchestrationV2TurnItem): string | null {
919919
}
920920
}
921921

922+
/** A cut inside a surrogate pair would leave half an emoji on each page. */
923+
function textPageEnd(text: string | null, offset: number, maxChars: number): number {
924+
const end = offset + maxChars;
925+
if (text === null || end >= text.length) return end;
926+
const last = text.charCodeAt(end - 1);
927+
if (last < 0xd800 || last > 0xdbff) return end;
928+
return end - 1 > offset ? end - 1 : end + 1;
929+
}
930+
922931
function timelineItem(input: {
923932
readonly row: OrchestrationV2ThreadProjection["visibleTurnItems"][number];
924933
readonly maxChars: number;
@@ -927,7 +936,7 @@ function timelineItem(input: {
927936
}): OrchestratorMcpThreadTimelineItem {
928937
const text = turnItemText(input.row.item);
929938
const offset = input.textOffset ?? 0;
930-
const end = offset + input.maxChars;
939+
const end = textPageEnd(text, offset, input.maxChars);
931940
const textTruncated = text !== null && text.length > end;
932941
const messageId =
933942
input.row.item.type === "user_message" || input.row.item.type === "assistant_message"

0 commit comments

Comments
 (0)