Never stop coding. Free MIT AI gateway: one endpoint, 375 providers (150+ free), 1,400+ models Kimi, Claude, GPT, Gemini, GLM, DeepSeek, MiniMax. Works with Claude Code, Codex, Cursor, OpenCode, Cline & Copilot. Quota-aware auto-fallback, RTK+Caveman compression saves 15-95% tokens, MCP/A2A, Desktop/PWA. Built by 900+ contributors
TypeScript a2aai-agentsai-gatewayanthropicclaudeclaude-codeclinecodexcopilotcursordeepseekfree-aigeminikimillm-gatewaymcpopenaiopenai-proxyqwentoken-saver
fix(requestLogger): exempt tools field from array truncation for full debug visibility (#2234)
Anton46df8470a906
diff --git a/open-sse/utils/requestLogger.ts b/open-sse/utils/requestLogger.tsindex 6e17e101b8..b145bcb3e6 100644--- a/open-sse/utils/requestLogger.ts+++ b/open-sse/utils/requestLogger.ts@@ -50,7 +50,7 @@ type RequestLoggerOptions = {const DEFAULT_MAX_STREAM_CHUNK_BYTES = 128 * 1024;const DEFAULT_MAX_STREAM_CHUNK_ITEMS = 512;const MAX_LOG_STRING_LENGTH = 64 * 1024;-const MAX_LOG_ARRAY_ITEMS = 24;+export const MAX_LOG_ARRAY_ITEMS = 24;const MAX_LOG_OBJECT_KEYS = 80;function maskSensitiveHeaders(headers: HeaderInput): Record<string, unknown> {@@ -98,16 +98,35 @@ function truncateLogString(value: string, maxLength = MAX_LOG_STRING_LENGTH): streturn `${value.slice(0, Math.floor(maxLength / 2))}\n[...truncated ${value.length - maxLength} chars...]\n${value.slice(-Math.ceil(maxLength / 2))}`;}-function cloneBoundedForLog(value: unknown, depth = 0): unknown {+/**+ * Recursively clone `value` for logging, with size bounds applied:+ * - Arrays longer than MAX_LOG_ARRAY_ITEMS are truncated to the tail with a+ * sentinel marker prepended.+ * - The `tools` field is exempt from array truncation: the full tool inventory+ * is debug-critical for understanding which tools the model had access to,+ * and individual tool descriptions are independently bounded by+ * truncateLogString, so the total size remains naturally capped.+ *+ * The optional `key` parameter carries the parent object's field name when+ * recursing into an object's values, enabling the per-field exemption above.+ * Top-level arrays (no key context) remain subject to truncation.+ */+export function cloneBoundedForLog(+ value: unknown,+ depth = 0,+ key: string | null = null+): unknown {if (value === null || value === undefined) return value;if (typeof value === "string") return truncateLogString(value);if (typeof value !== "object") return value;if (depth >= 6) return "[MaxDepth]";if (Array.isArray(value)) {- const source = value.length > MAX_LOG_ARRAY_ITEMS ? value.slice(-MAX_LOG_ARRAY_ITEMS) : value;+ const exempt = key === "tools";+ const shouldTruncate = !exempt && value.length > MAX_LOG_ARRAY_ITEMS;+ const source = shouldTruncate ? value.slice(-MAX_LOG_ARRAY_ITEMS) : value;const mapped = source.map((item) => cloneBoundedForLog(item, depth + 1));- if (value.length > MAX_LOG_ARRAY_ITEMS) {+ if (shouldTruncate) {return [{_omniroute_truncated_array: true,@@ -122,8 +141,8 @@ function cloneBoundedForLog(value: unknown, depth = 0): unknown {const result: JsonRecord = {};const entries = Object.entries(value as JsonRecord);- for (const [key, item] of entries.slice(0, MAX_LOG_OBJECT_KEYS)) {- result[key] = cloneBoundedForLog(item, depth + 1);+ for (const [k, item] of entries.slice(0, MAX_LOG_OBJECT_KEYS)) {+ result[k] = cloneBoundedForLog(item, depth + 1, k);}if (entries.length > MAX_LOG_OBJECT_KEYS) {result._omniroute_truncated_keys = entries.length - MAX_LOG_OBJECT_KEYS;diff --git a/tests/unit/request-logger-bounded-clone.test.ts b/tests/unit/request-logger-bounded-clone.test.tsnew file mode 100644index 0000000000..5603479a0d--- /dev/null+++ b/tests/unit/request-logger-bounded-clone.test.ts@@ -0,0 +1,52 @@+import test from "node:test";+import assert from "node:assert/strict";++const { cloneBoundedForLog, MAX_LOG_ARRAY_ITEMS } = await import(+ "../../open-sse/utils/requestLogger.ts"+);++test("cloneBoundedForLog: tools array is exempt from truncation (debug-critical)", () => {+ const tools = Array.from({ length: 45 }, (_, i) => ({+ name: `tool_${String(i).padStart(2, "0")}`,+ description: `Tool ${i} description`,+ }));+ const result = cloneBoundedForLog({ tools }) as { tools: Array<Record<string, unknown>> };+ assert.equal(result.tools.length, 45);+ assert.equal(result.tools[0].name, "tool_00");+ assert.equal(result.tools[44].name, "tool_44");+ // No truncation marker should appear inside tools+ assert.ok(+ !("_omniroute_truncated_array" in result.tools[0]),+ "tools array should NOT have truncation marker"+ );+});++test("cloneBoundedForLog: other large arrays still truncated to MAX_LOG_ARRAY_ITEMS", () => {+ const messages = Array.from({ length: 45 }, (_, i) => ({ role: "user", content: `msg ${i}` }));+ const result = cloneBoundedForLog({ messages }) as { messages: Array<Record<string, unknown>> };+ assert.equal(result.messages.length, MAX_LOG_ARRAY_ITEMS + 1, "1 marker + tail items");+ const marker = result.messages[0];+ assert.equal(marker._omniroute_truncated_array, true);+ assert.equal(marker.originalLength, 45);+ assert.equal(marker.retainedTailItems, MAX_LOG_ARRAY_ITEMS);+});++test("cloneBoundedForLog: nested tools field still exempt", () => {+ const body = {+ body: { tools: Array.from({ length: 30 }, (_, i) => ({ name: `t_${i}` })) },+ };+ const result = cloneBoundedForLog(body) as { body: { tools: unknown[] } };+ assert.equal(result.body.tools.length, 30);+});++test("cloneBoundedForLog: top-level array without key context still truncated", () => {+ const arr = Array.from({ length: 45 }, (_, i) => i);+ const result = cloneBoundedForLog(arr) as unknown[];+ assert.equal(result.length, MAX_LOG_ARRAY_ITEMS + 1);+});++test("cloneBoundedForLog: small tools array (<=MAX) passes through unchanged", () => {+ const tools = Array.from({ length: 10 }, (_, i) => ({ name: `t_${i}` }));+ const result = cloneBoundedForLog({ tools }) as { tools: unknown[] };+ assert.equal(result.tools.length, 10);+});