fix(mcp): bound retained doctor stderr while draining output

Retain a 64 KiB diagnostic prefix while counting and draining the complete stderr stream. Report truncation and avoid manufacturing a replacement character at a truncated UTF-8 boundary.

Validation: all nine doctor tests pass, including real noisy child, malformed bytes, canonical catalog and startup deadline controls.

Based on #3121.

Co-authored-by: 1052326311 <[email protected]>
This commit is contained in:
morlutoand1052326311 committed 2026-10-14 13:58:17 +08:00
1 parent ad9e22d05f
commit ae433f0983
3 files changed
+90 -3

No files matched your search

+15 -3
View File
@@ -3,6 +3,7 @@ import { StdioClientTransport } from "@modelcontextprotocol/client/stdio";
import { Formatter, Help, z } from "incur";
import { performance } from "node:perf_hooks";
import { resolve } from "node:path";
import { StringDecoder } from "node:string_decoder";
import { CATALOG_IDENTITY } from "./catalogIdentity.js";
import { PROMPT_CONTRACTS } from "./contracts/promptContracts.js";
@@ -483,19 +484,30 @@ const targetFreeEnvironment = (
};
const captureStderr = () => {
const chunks: Buffer[] = [];
const retained = Buffer.allocUnsafe(MCP_STARTUP_POLICY.doctorStderrBytes);
let retainedBytes = 0;
let bytes = 0;
let truncated = false;
return {
append(chunk: unknown) {
const buffer = Buffer.isBuffer(chunk)
? chunk
: Buffer.from(String(chunk), "utf8");
bytes += buffer.byteLength;
chunks.push(Buffer.from(buffer));
const remaining = retained.byteLength - retainedBytes;
const copied = Math.min(remaining, buffer.byteLength);
if (copied > 0) {
buffer.copy(retained, retainedBytes, 0, copied);
retainedBytes += copied;
}
if (copied < buffer.byteLength) truncated = true;
},
value: () => ({
stderr: Buffer.concat(chunks, bytes).toString("utf8"),
stderr: truncated
? new StringDecoder("utf8").write(retained.subarray(0, retainedBytes))
: retained.toString("utf8", 0, retainedBytes),
stderr_bytes: bytes,
stderr_truncated: truncated,
}),
};
};
+1
View File
@@ -13,4 +13,5 @@ export const MCP_STARTUP_POLICY = {
toolsListBudgetMs: 5_000,
firstCatalogBudgetMs: 20_000,
doctorDeadlineMs: 30_000,
doctorStderrBytes: 64 * 1024,
} as const;