import * as Predicate from "effect/Predicate"; const MAX_PARSED_BYTES = 16_384; const MAX_METADATA_BYTES = 8_192; const MAX_ID_LENGTH = 256; const MAX_THREADS = 100; const MAX_CONTENT_BLOCKS = 32; const MAX_ENVELOPE_DEPTH = 4; const MAX_ENVELOPE_NODES = 128; const encoder = new TextEncoder(); interface ResultReadBudget { remainingBytes: number; remainingNodes: number; exceeded: boolean; } interface ResultEnvelope { data?: Record; failed: boolean; } interface CompactToolOutput { isError?: true; threadId?: string; messageId?: string; taskId?: string; scheduledTaskId?: string; status?: "rolled_back"; thread?: { threadId: string }; threads?: Array<{ threadId?: string; status?: "rolled_back" }>; } function readResult(value: unknown, budget: ResultReadBudget, depth = 0): ResultEnvelope { if (depth > MAX_ENVELOPE_DEPTH || budget.remainingNodes-- <= 0) { budget.exceeded = true; return { failed: false }; } if (typeof value === "string") { if (value.length > budget.remainingBytes) { budget.exceeded = true; return { failed: false }; } const bytes = encoder.encode(value).byteLength; if (bytes > budget.remainingBytes) { budget.exceeded = true; return { failed: false }; } budget.remainingBytes -= bytes; try { return readResult(JSON.parse(value), budget, depth + 1); } catch { return { failed: false }; } } if (Array.isArray(value)) { if (value.length > MAX_CONTENT_BLOCKS) { budget.exceeded = true; return { failed: false }; } let data: ResultEnvelope["data"]; let failed = false; for (const block of value) { const text = Predicate.isObject(block) ? block.text : undefined; const result = readResult( Predicate.isObject(text) ? (text.text ?? text) : text, budget, depth + 1, ); data ??= result.data; failed ||= result.failed; if (budget.exceeded) break; } return { ...(data === undefined ? {} : { data }), failed }; } if (!Predicate.isObject(value)) return { failed: false }; const failed = value.isError === true || value.is_error === true || value._tag === "OrchestratorMcpFailure" || value.error != null; const content = value.structuredContent ?? value.content; if (content !== undefined) { const nested = readResult(content, budget, depth + 1); return { ...nested, failed: failed || nested.failed }; } return { data: value, failed }; } function boundedId(value: unknown): string | undefined { if (typeof value !== "string" || value.length > MAX_ID_LENGTH || value.trim() === "") { return undefined; } return Array.from(value).join(""); } /** Keeps only IDs and failure metadata used by T3's grouped tool summaries. */ export function compactDynamicToolOutput(value: unknown): CompactToolOutput | undefined { const budget: ResultReadBudget = { remainingBytes: MAX_PARSED_BYTES, remainingNodes: MAX_ENVELOPE_NODES, exceeded: false, }; const result = readResult(value, budget); const output: CompactToolOutput = result.failed ? { isError: true } : {}; const data = budget.exceeded ? undefined : result.data; if (data !== undefined) { for (const key of ["threadId", "messageId", "taskId", "scheduledTaskId"] as const) { const id = boundedId(data[key]); if (id !== undefined) output[key] = id; } if (data.status === "rolled_back") output.status = "rolled_back"; const nestedThreadId = Predicate.isObject(data.thread) ? boundedId(data.thread.threadId) : undefined; if (nestedThreadId !== undefined) output.thread = { threadId: nestedThreadId }; if (Array.isArray(data.threads)) { let complete = data.threads.length <= MAX_THREADS; const threads: NonNullable = []; if (complete) { for (const entry of data.threads) { if (!Predicate.isObject(entry)) { complete = false; break; } const threadId = boundedId(entry.threadId); const rolledBack = entry.status === "rolled_back"; if (threadId === undefined && !rolledBack) { complete = false; break; } threads.push({ ...(threadId === undefined ? {} : { threadId }), ...(rolledBack ? { status: "rolled_back" as const } : {}), }); } } if (complete) output.threads = threads; else { // A partial batch or a top-level ID would turn an unknown creation // count into a confidently wrong one in the existing summary parser. delete output.threadId; delete output.status; } } } if (encoder.encode(JSON.stringify(output)).byteLength > MAX_METADATA_BYTES) { delete output.threads; delete output.threadId; delete output.status; } return Object.keys(output).length === 0 ? undefined : output; } /** Some providers report completion even when command output describes a failure. */ export function toolOutputIndicatesFailure(text: string): boolean { return ( /file not found|no files found|enoent|no such file|commandnotfoundexception|command not found|is not recognized as the name of a cmdlet|a parameter cannot be found that matches parameter name/i.test( text, ) || (/cannot find path/i.test(text) && /because it does not exist/i.test(text)) || (/is not recognized/i.test(text) && /the term '/i.test(text)) || //i.test(text) || /exit(?:ed)? with exit code\s+[1-9]\d*/i.test(text) || /exit code\s*[:\s]\s*[1-9]\d*\b/i.test(text) ); }