From f39f1a4712a669f01d75f24298203bd21b0f1b22 Mon Sep 17 00:00:00 2001 From: Vincent Koc Date: Wed, 27 May 2026 22:34:08 +0200 Subject: [PATCH] fix(e2e): bound MCP channel harness buffers --- scripts/e2e/mcp-channels-harness.ts | 35 ++++++++++++++++++++++------- 1 file changed, 27 insertions(+), 8 deletions(-) diff --git a/scripts/e2e/mcp-channels-harness.ts b/scripts/e2e/mcp-channels-harness.ts index 8ba927d67b4d..715308dd064d 100644 --- a/scripts/e2e/mcp-channels-harness.ts +++ b/scripts/e2e/mcp-channels-harness.ts @@ -54,6 +54,14 @@ const MCP_CONNECT_TIMEOUT_MS = readPositiveInt( process.env.OPENCLAW_MCP_CHANNELS_CONNECT_TIMEOUT_MS, 60_000, ); +const GATEWAY_EVENT_RETAIN_LIMIT = readPositiveInt( + process.env.OPENCLAW_MCP_CHANNELS_GATEWAY_EVENT_RETAIN_LIMIT, + 2_000, +); +const MCP_RAW_MESSAGE_RETAIN_LIMIT = readPositiveInt( + process.env.OPENCLAW_MCP_CHANNELS_RAW_MESSAGE_RETAIN_LIMIT, + 2_000, +); export function assert(condition: unknown, message: string): asserts condition { if (!condition) { @@ -66,6 +74,13 @@ function readPositiveInt(raw: string | undefined, fallback: number) { return Number.isInteger(parsed) && parsed > 0 ? parsed : fallback; } +function pushBounded(items: T[], item: T, limit: number): void { + items.push(item); + if (items.length > limit) { + items.splice(0, items.length - limit); + } +} + export function extractTextFromGatewayPayload( payload: Record | undefined, ): string | undefined { @@ -164,13 +179,17 @@ async function connectGatewayOnce(params: { error?: { message?: unknown } | null; }; if (typed.type === "event" && typeof typed.event === "string") { - events.push({ - event: typed.event, - payload: - typed.payload && typeof typed.payload === "object" - ? (typed.payload as Record) - : {}, - }); + pushBounded( + events, + { + event: typed.event, + payload: + typed.payload && typeof typed.payload === "object" + ? (typed.payload as Record) + : {}, + }, + GATEWAY_EVENT_RETAIN_LIMIT, + ); return; } if (typed.type !== "res" || typeof typed.id !== "string") { @@ -336,7 +355,7 @@ export async function connectMcpClient(params: { // runtime, not an EventTarget-style addEventListener API. // oxlint-disable-next-line unicorn/prefer-add-event-listener transport.onmessage = (message) => { - rawMessages.push(message); + pushBounded(rawMessages, message, MCP_RAW_MESSAGE_RETAIN_LIMIT); }; const client = new Client({ name: "docker-mcp-channels", version: "1.0.0" });