Skip to content

Commit f99cee7

Browse files
t3dotggclaude
andcommitted
fix(mcp): shed log lists before locators so the snapshot bound always holds
Per-field caps did not sum below the ceiling: three full log arrays of capped entries alone can pass 60 KB. Halve action timeline, network, and console lists first, then interactive elements, until the JSON fits. Co-Authored-By: Claude Fable 5.1 <[email protected]>
1 parent 135aa18 commit f99cee7

2 files changed

Lines changed: 110 additions & 11 deletions

File tree

‎apps/server/src/mcp/McpHttpServer.test.ts‎

Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -443,6 +443,72 @@ it.effect("bounds the snapshot text even when nothing but logs and the title are
443443
).pipe(Effect.provide(TestLayer)),
444444
);
445445

446+
it.effect("sheds log entries before locators when every list is full", () =>
447+
Effect.scoped(
448+
Effect.gen(function* () {
449+
const long = "x".repeat(2_000);
450+
const oversized = {
451+
...snapshotResult,
452+
interactiveElements: Array.from({ length: 20 }, (_, i) => ({
453+
tag: "button",
454+
role: "button",
455+
name: `Button ${i}`,
456+
selector: `#button-${i}`,
457+
x: 0,
458+
y: 0,
459+
width: 10,
460+
height: 10,
461+
})),
462+
consoleEntries: Array.from({ length: 200 }, () => ({
463+
level: long,
464+
text: long,
465+
timestamp: long,
466+
source: long,
467+
})),
468+
networkEntries: Array.from({ length: 200 }, () => ({
469+
url: long,
470+
method: long,
471+
status: 200,
472+
failed: false,
473+
errorText: long,
474+
timestamp: long,
475+
})),
476+
actionTimeline: Array.from({ length: 200 }, () => ({
477+
id: long,
478+
action: long,
479+
status: "succeeded",
480+
startedAt: long,
481+
completedAt: long,
482+
error: long,
483+
})),
484+
};
485+
yield* serveSnapshots("mcp-full-logs-client", oversized);
486+
487+
const snapshot = yield* callSnapshot({ includeImage: false });
488+
489+
const [text, notice] = snapshot.content;
490+
const body = text?.type === "text" ? text.text : "";
491+
expect(Buffer.byteLength(body, "utf8")).toBeLessThanOrEqual(
492+
McpHttpServer.MAX_SNAPSHOT_TEXT_BYTES,
493+
);
494+
const parsed = decodeJsonText(body) as {
495+
readonly interactiveElements: ReadonlyArray<unknown>;
496+
readonly consoleEntries: ReadonlyArray<unknown>;
497+
readonly networkEntries: ReadonlyArray<unknown>;
498+
readonly actionTimeline: ReadonlyArray<unknown>;
499+
};
500+
// Locators survive; the log lists take the cut.
501+
expect(parsed.interactiveElements).toHaveLength(20);
502+
expect(
503+
parsed.consoleEntries.length + parsed.networkEntries.length + parsed.actionTimeline.length,
504+
).toBeLessThan(120);
505+
const noticeText = notice?.type === "text" ? notice.text : "";
506+
expect(noticeText).toContain("40 of 40 actionTimeline");
507+
expect(noticeText).not.toMatch(/\d+ of \d+ interactiveElements/);
508+
}),
509+
).pipe(Effect.provide(TestLayer)),
510+
);
511+
446512
it.effect("terminates HTTP MCP sessions with DELETE", () =>
447513
Effect.scoped(
448514
Effect.gen(function* () {

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

Lines changed: 44 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -205,18 +205,51 @@ const boundSnapshotMetadata = (
205205
actionTimeline: tail(metadata.actionTimeline, "action timeline entries"),
206206
};
207207

208-
// Every field above has a fixed cap, so only the element list can still push
209-
// the text over the ceiling. Halve it until the JSON fits.
210-
let elements = bounded.interactiveElements;
211-
let text = encodeJsonText(bounded);
212-
while (utf8Length(text) > MAX_SNAPSHOT_TEXT_BYTES && elements.length > 0) {
213-
elements = elements.slice(0, Math.floor(elements.length / 2));
214-
text = encodeJsonText({ ...bounded, interactiveElements: elements });
208+
// Per-field caps do not sum below the ceiling: three log arrays of 40 capped
209+
// entries alone can pass 60 KB. Shed the least useful lists first, halving
210+
// one list per round, until the JSON fits. With every list empty the rest
211+
// is bounded by the identifier and visibleText caps, so this terminates.
212+
const shedOrder = [
213+
"actionTimeline",
214+
"networkEntries",
215+
"consoleEntries",
216+
"interactiveElements",
217+
] as const;
218+
const lists: Record<(typeof shedOrder)[number], ReadonlyArray<unknown>> = {
219+
interactiveElements: bounded.interactiveElements,
220+
consoleEntries: bounded.consoleEntries,
221+
networkEntries: bounded.networkEntries,
222+
actionTimeline: bounded.actionTimeline,
223+
};
224+
const dropped: Record<(typeof shedOrder)[number], number> = {
225+
interactiveElements: 0,
226+
consoleEntries: 0,
227+
networkEntries: 0,
228+
actionTimeline: 0,
229+
};
230+
let text = encodeJsonText({ ...bounded, ...lists });
231+
while (utf8Length(text) > MAX_SNAPSHOT_TEXT_BYTES) {
232+
// Elements carry the locators, so they go last; logs shed newest-last.
233+
const key =
234+
shedOrder.find(
235+
(candidate) => candidate !== "interactiveElements" && lists[candidate].length > 0,
236+
) ?? (lists.interactiveElements.length > 0 ? "interactiveElements" : undefined);
237+
if (key === undefined) break;
238+
const keep = Math.floor(lists[key].length / 2);
239+
dropped[key] += lists[key].length - keep;
240+
// slice(-0) keeps everything, so spell out the empty case.
241+
lists[key] =
242+
keep === 0
243+
? []
244+
: key === "interactiveElements"
245+
? lists[key].slice(0, keep)
246+
: lists[key].slice(-keep);
247+
text = encodeJsonText({ ...bounded, ...lists });
215248
}
216-
if (elements.length < bounded.interactiveElements.length) {
217-
omitted.push(
218-
`${bounded.interactiveElements.length - elements.length} of ${bounded.interactiveElements.length} interactive elements`,
219-
);
249+
for (const key of shedOrder) {
250+
if (dropped[key] > 0) {
251+
omitted.push(`${dropped[key]} of ${bounded[key].length} ${key}`);
252+
}
220253
}
221254
return { text, omitted };
222255
};

0 commit comments

Comments
 (0)