From cadaf7c8dc5536d3e66bb615490493c18379cbfc Mon Sep 17 00:00:00 2001 From: Atharva Padmakar Deshmukh <175204585+bablamon@users.noreply.github.com> Date: Thu, 3 Dec 2026 01:02:41 +0530 Subject: [PATCH 1/3] Raise the bridge's input limit to fit the server's tool schemas The bridge capped an input line at 8,192 bytes and exited on a longer one, so the agent lost every Proofline tool for the rest of its session. A full-length ask_codebase question (4,000 characters) in a CJK script is 12,000 bytes, so a call the server accepts never reached it. Raise the limit to 1 MiB, the same limit the CLI's bridge proposes for prooflinedev/cli#144. Past the limit nothing changes: the null-id error and the exit stay as designed. Collect a line's chunks and join them once at its newline. At 8 KiB the per-chunk copy didn't matter; at 1 MiB a line arriving in small chunks would be copied once per chunk. The test server now decodes the request body after joining its bytes, so multi-byte characters split across chunks stay intact. --- README.md | 2 +- src/bridge.js | 22 ++++++++++++++------- tests/bridge.test.js | 46 ++++++++++++++++++++++++++++++++++++++++---- 3 files changed, 58 insertions(+), 12 deletions(-) diff --git a/README.md b/README.md index 2d4e510..972bb9c 100644 --- a/README.md +++ b/README.md @@ -50,7 +50,7 @@ The server keeps model and repository text wrapped as `untrusted_text`. Treat it The bridge accepts newline-delimited JSON-RPC on stdin and writes only protocol responses to stdout. Diagnostics go to stderr. The bridge forwards one request at a time and never retries. A failed mutation may have committed; inspect its state before making another request. -The bridge supports Proofline's stateless JSON transport with protocol `2025-11-25`. Initialize, then send `notifications/initialized` before calling tools. The bridge limits input frames to 8 KiB, responses to 8 MiB, and requests to 35 seconds. A timed-out request returns a correlated JSON-RPC `-32000` error (`data.category: "timeout"`) without ending the bridge, so no restart is needed; because the call may still have run on the server, read state before repeating a write. It refuses redirects, SSE, and `MCP-Session-Id`. HTTP failures produce a correlated, sanitized JSON-RPC error; the bridge does not print upstream error bodies or credentials. HTTP refusals, timed-out notifications, and other transport failures exit 1. Clean input EOF exits 0. +The bridge supports Proofline's stateless JSON transport with protocol `2025-11-25`. Initialize, then send `notifications/initialized` before calling tools. The bridge limits input frames to 1 MiB, enough for any call the tool schemas accept, responses to 8 MiB, and requests to 35 seconds. A timed-out request returns a correlated JSON-RPC `-32000` error (`data.category: "timeout"`) without ending the bridge, so no restart is needed; because the call may still have run on the server, read state before repeating a write. It refuses redirects, SSE, and `MCP-Session-Id`. HTTP failures produce a correlated, sanitized JSON-RPC error; the bridge does not print upstream error bodies or credentials. HTTP refusals, timed-out notifications, and other transport failures exit 1. Clean input EOF exits 0. ## Development and releases diff --git a/src/bridge.js b/src/bridge.js index 577b6c4..0ba6f47 100644 --- a/src/bridge.js +++ b/src/bridge.js @@ -1,7 +1,10 @@ import { once } from "node:events"; export const PROTOCOL = "2025-11-25"; -const REQUEST_LIMIT = 8192; +// Same limit as the CLI's bridge (prooflinedev/cli#144). The longest bounded +// string in the server's tool schemas is 65,536 characters; escaped as surrogate +// pairs that is at most 768 KiB, so 1 MiB leaves room for the envelope. +const REQUEST_LIMIT = 1024 * 1024; const RESPONSE_LIMIT = 8 * 1024 * 1024; const REQUEST_TIMEOUT = 35_000; const TRANSPORT_TIMEOUTS = new Set(["UND_ERR_CONNECT_TIMEOUT", "UND_ERR_HEADERS_TIMEOUT", "UND_ERR_BODY_TIMEOUT", "ETIMEDOUT"]); @@ -35,21 +38,26 @@ export function configuration(env) { } async function* frames(input) { - let pending = Buffer.alloc(0); + // Collect a line's pieces and join them once at its newline, so a line that + // arrives in many small chunks is copied once rather than once per chunk. + let parts = []; + let size = 0; for await (const chunk of input) { let bytes = Buffer.isBuffer(chunk) ? chunk : Buffer.from(chunk); while (bytes.length) { const newline = bytes.indexOf(10); const end = newline === -1 ? bytes.length : newline; - if (pending.length + end > REQUEST_LIMIT) throw new BridgeError("input", -32600); - pending = Buffer.concat([pending, bytes.subarray(0, end)]); + if (size + end > REQUEST_LIMIT) throw new BridgeError("input", -32600); + parts.push(bytes.subarray(0, end)); + size += end; if (newline === -1) break; - yield pending; - pending = Buffer.alloc(0); + yield Buffer.concat(parts, size); + parts = []; + size = 0; bytes = bytes.subarray(newline + 1); } } - if (pending.length) throw new BridgeError("input", -32600); + if (size) throw new BridgeError("input", -32600); } async function send(output, message) { diff --git a/tests/bridge.test.js b/tests/bridge.test.js index 5abf266..17f9747 100644 --- a/tests/bridge.test.js +++ b/tests/bridge.test.js @@ -15,14 +15,22 @@ const notification = { jsonrpc: "2.0", method: "notifications/initialized" }; const call = { jsonrpc: "2.0", id: 7, method: "tools/call", params: { name: "request_reverification", arguments: { review_id: "review", commit_sha: "a".repeat(40), finding_ids: ["finding"] }, } }; +const inputLimit = 1024 * 1024; +const ping = (id) => ({ jsonrpc: "2.0", id, method: "ping" }); +const frame = (message) => JSON.stringify(message) + "\n"; +// A tools/call whose JSON line is exactly `bytes` long (newline excluded). +const callOfLength = (bytes) => { + const shaped = (padding) => ({ ...call, params: { ...call.params, arguments: { ...call.params.arguments, padding } } }); + return shaped("a".repeat(bytes - Buffer.byteLength(JSON.stringify(shaped(""))))); +}; const json = (response, value) => { response.writeHead(200, { "content-type": "application/json" }); response.end(JSON.stringify(value)); }; async function server(t, responder) { const requests = []; const http = createServer(async (request, response) => { - let raw = ""; - for await (const chunk of request) raw += chunk; - const body = JSON.parse(raw); + const chunks = []; + for await (const chunk of request) chunks.push(chunk); + const body = JSON.parse(Buffer.concat(chunks).toString("utf8")); requests.push(body); assert.equal(request.url, "/mcp"); assert.equal(request.headers.authorization, `Bearer ${token}`); @@ -98,7 +106,7 @@ test("HTTP refusals, redirects and malformed responses fail once without leaking test("invalid frames and lifecycle never reach the server", async (t) => { const fixture = await server(t, () => assert.fail("unexpected request")); - for (const input of ["{\n", " ".repeat(8193) + "\n", JSON.stringify(initialize), "[]\n"]) { + for (const input of ["{\n", " ".repeat(inputLimit + 1) + "\n", JSON.stringify(initialize), "[]\n"]) { const outcome = await run(fixture.origin, input); assert.equal(outcome.code, 1); assert.equal(outcome.responses.length, 1); @@ -115,6 +123,36 @@ test("invalid frames and lifecycle never reach the server", async (t) => { } }); +test("a full-length question in a CJK script is relayed and the session continues", async (t) => { + const fixture = await server(t, (body, response) => json(response, { jsonrpc: "2.0", id: body.id, result: {} })); + // ask_codebase allows 4,000 characters; in a CJK script that is 12,000 bytes. + const ask = { jsonrpc: "2.0", id: 7, method: "tools/call", params: { + name: "ask_codebase", arguments: { question: "代".repeat(4000) } } }; + const outcome = await run(fixture.origin, [initialize, notification, ask, ping(8)]); + assert.equal(outcome.code, 0); + assert.equal(outcome.stderr, ""); + assert.deepEqual(outcome.responses.map((response) => response.id), ["init", 7, 8]); + assert.deepEqual(fixture.requests.at(-2), ask); +}); + +test("an input line is bounded at 1 MiB", async (t) => { + const fixture = await server(t, (body, response) => json(response, { jsonrpc: "2.0", id: body.id, result: {} })); + const atLimit = callOfLength(inputLimit); + const accepted = await run(fixture.origin, [initialize, notification, atLimit, ping(8)]); + assert.equal(accepted.code, 0); + assert.deepEqual(accepted.responses.map((response) => response.id), ["init", 7, 8]); + assert.deepEqual(fixture.requests.at(-2), atLimit); + + const sent = fixture.requests.length; + const refused = await run(fixture.origin, + frame(initialize) + frame(notification) + frame(callOfLength(inputLimit + 1)) + frame(ping(8))); + assert.equal(refused.code, 1); + assert.deepEqual(refused.responses.map((response) => response.id), ["init", null]); + assert.equal(refused.responses[1].error.data.category, "input"); + assert.deepEqual(fixture.requests.slice(sent).map((request) => request.method), + ["initialize", "notifications/initialized"], "the oversized call never reaches the server"); +}); + test("pre-initialize probes are answered without ending the session", async (t) => { const tools = { tools: [] }; const fixture = await server(t, (body, response) => json(response, { jsonrpc: "2.0", id: body.id, result: tools })); From f73fe6bb10e78d2348e0f9d8ea9e0361f2b3b700 Mon Sep 17 00:00:00 2001 From: Atharva Padmakar Deshmukh <175204585+bablamon@users.noreply.github.com> Date: Thu, 3 Dec 2026 03:50:06 +0530 Subject: [PATCH 2/3] Keep no input chunks alive, and state the limit as raw bytes Review follow-up for the three findings on this pull request. frames() kept a view of every input chunk in a list until the line ended, and yielded the joined line before clearing that list. A finely split line held one view, and its parent chunk, per piece, and the list stayed alive for the whole HTTP request. Each piece is now copied into a buffer the bridge owns, grown by doubling so the copying stays linear, and the line is detached before it is yielded. The README and the limit's comment said 1 MiB fits any schema-valid call. It fits the longest bounded string, but a call with several large fields or an unbounded one can exceed it. Both now describe a raw-byte limit. Add a test that runs the bridge in-process on input split into pieces of a fixed size: 7-byte pieces that cut multi-byte characters are reassembled exactly, and an over-limit line in 8 KiB pieces is refused before any of it reaches the server. Unlike the stdio tests, it doesn't depend on how the pipe happens to chunk input. --- README.md | 2 +- src/bridge.js | 28 +++++++++++++++++++--------- tests/bridge.test.js | 38 +++++++++++++++++++++++++++++++++++++- 3 files changed, 57 insertions(+), 11 deletions(-) diff --git a/README.md b/README.md index 972bb9c..883fd08 100644 --- a/README.md +++ b/README.md @@ -50,7 +50,7 @@ The server keeps model and repository text wrapped as `untrusted_text`. Treat it The bridge accepts newline-delimited JSON-RPC on stdin and writes only protocol responses to stdout. Diagnostics go to stderr. The bridge forwards one request at a time and never retries. A failed mutation may have committed; inspect its state before making another request. -The bridge supports Proofline's stateless JSON transport with protocol `2025-11-25`. Initialize, then send `notifications/initialized` before calling tools. The bridge limits input frames to 1 MiB, enough for any call the tool schemas accept, responses to 8 MiB, and requests to 35 seconds. A timed-out request returns a correlated JSON-RPC `-32000` error (`data.category: "timeout"`) without ending the bridge, so no restart is needed; because the call may still have run on the server, read state before repeating a write. It refuses redirects, SSE, and `MCP-Session-Id`. HTTP failures produce a correlated, sanitized JSON-RPC error; the bridge does not print upstream error bodies or credentials. HTTP refusals, timed-out notifications, and other transport failures exit 1. Clean input EOF exits 0. +The bridge supports Proofline's stateless JSON transport with protocol `2025-11-25`. Initialize, then send `notifications/initialized` before calling tools. The bridge limits each input line to 1 MiB of raw bytes, whatever the call contains, responses to 8 MiB, and requests to 35 seconds. A timed-out request returns a correlated JSON-RPC `-32000` error (`data.category: "timeout"`) without ending the bridge, so no restart is needed; because the call may still have run on the server, read state before repeating a write. It refuses redirects, SSE, and `MCP-Session-Id`. HTTP failures produce a correlated, sanitized JSON-RPC error; the bridge does not print upstream error bodies or credentials. HTTP refusals, timed-out notifications, and other transport failures exit 1. Clean input EOF exits 0. ## Development and releases diff --git a/src/bridge.js b/src/bridge.js index 0ba6f47..241cc89 100644 --- a/src/bridge.js +++ b/src/bridge.js @@ -1,9 +1,11 @@ import { once } from "node:events"; export const PROTOCOL = "2025-11-25"; -// Same limit as the CLI's bridge (prooflinedev/cli#144). The longest bounded -// string in the server's tool schemas is 65,536 characters; escaped as surrogate -// pairs that is at most 768 KiB, so 1 MiB leaves room for the envelope. +// 1 MiB of raw bytes per input line, the limit proposed for the CLI's bridge +// (prooflinedev/cli#144). It fits the longest bounded string in the server's +// tool schemas (65,536 characters, at most 768 KiB escaped) with room for the +// envelope. A call with several large fields, or an unbounded one, can still +// exceed it. const REQUEST_LIMIT = 1024 * 1024; const RESPONSE_LIMIT = 8 * 1024 * 1024; const REQUEST_TIMEOUT = 35_000; @@ -38,9 +40,11 @@ export function configuration(env) { } async function* frames(input) { - // Collect a line's pieces and join them once at its newline, so a line that - // arrives in many small chunks is copied once rather than once per chunk. - let parts = []; + // Copy each piece into a buffer the bridge owns, so no view of an input chunk + // is kept however finely a line is split. Doubling the buffer keeps the total + // copying linear. The line is detached before it is yielded, so nothing else + // stays alive while its request is in flight. + let line = Buffer.alloc(0); let size = 0; for await (const chunk of input) { let bytes = Buffer.isBuffer(chunk) ? chunk : Buffer.from(chunk); @@ -48,12 +52,18 @@ async function* frames(input) { const newline = bytes.indexOf(10); const end = newline === -1 ? bytes.length : newline; if (size + end > REQUEST_LIMIT) throw new BridgeError("input", -32600); - parts.push(bytes.subarray(0, end)); + if (size + end > line.length) { + const grown = Buffer.allocUnsafe(Math.min(REQUEST_LIMIT, Math.max(size + end, line.length * 2))); + line.copy(grown, 0, 0, size); + line = grown; + } + bytes.copy(line, size, 0, end); size += end; if (newline === -1) break; - yield Buffer.concat(parts, size); - parts = []; + const frame = Buffer.from(line.subarray(0, size)); + line = Buffer.alloc(0); size = 0; + yield frame; bytes = bytes.subarray(newline + 1); } } diff --git a/tests/bridge.test.js b/tests/bridge.test.js index 17f9747..3f5f564 100644 --- a/tests/bridge.test.js +++ b/tests/bridge.test.js @@ -2,10 +2,13 @@ import assert from "node:assert/strict"; import { spawn } from "node:child_process"; import { once } from "node:events"; import { createServer } from "node:http"; +import { Readable, Writable } from "node:stream"; import test from "node:test"; -import { fileURLToPath } from "node:url"; +import { fileURLToPath, pathToFileURL } from "node:url"; const binary = process.env.PROOFLINE_MCP_TEST_BIN ?? fileURLToPath(new URL("../src/cli.js", import.meta.url)); +// The bridge module next to the binary under test, so packed runs test the packed copy. +const { bridge } = await import(new URL("bridge.js", pathToFileURL(binary)).href); const token = "test-token-never-print"; const protocol = "2025-11-25"; const initialize = { jsonrpc: "2.0", id: "init", method: "initialize", params: { @@ -153,6 +156,39 @@ test("an input line is bounded at 1 MiB", async (t) => { ["initialize", "notifications/initialized"], "the oversized call never reaches the server"); }); +// Runs the bridge in-process on input delivered in pieces of exactly `size` bytes, +// so the test, not the operating system's pipe, decides where lines are split. +async function relayInPieces(origin, input, size) { + const bytes = Buffer.from(input); + const pieces = []; + for (let offset = 0; offset < bytes.length; offset += size) pieces.push(bytes.subarray(offset, offset + size)); + let stdout = ""; + const output = new Writable({ write(chunk, _encoding, done) { stdout += chunk; done(); } }); + const diagnostic = new Writable({ write(_chunk, _encoding, done) { done(); } }); + const code = await bridge(Readable.from(pieces), output, diagnostic, { token, origin }); + return { code, responses: stdout.trim() ? stdout.trim().split("\n").map(JSON.parse) : [] }; +} + +test("a line split into small pieces is relayed whole, and the limit counts the whole line", async (t) => { + const fixture = await server(t, (body, response) => json(response, { jsonrpc: "2.0", id: body.id, result: {} })); + const ask = { jsonrpc: "2.0", id: 7, method: "tools/call", params: { + name: "ask_codebase", arguments: { question: "代".repeat(4000) } } }; + // 7-byte pieces split the 3-byte characters, and every line spans many pieces. + const relayed = await relayInPieces(fixture.origin, [initialize, notification, ask, ping(8)].map(frame).join(""), 7); + assert.equal(relayed.code, 0); + assert.deepEqual(relayed.responses.map((response) => response.id), ["init", 7, 8]); + assert.deepEqual(fixture.requests.at(-2), ask, "the line is reassembled byte for byte"); + + const sent = fixture.requests.length; + const refused = await relayInPieces(fixture.origin, + frame(initialize) + frame(notification) + frame(callOfLength(inputLimit + 1)) + frame(ping(8)), 8192); + assert.equal(refused.code, 1); + assert.deepEqual(refused.responses.map((response) => response.id), ["init", null]); + assert.equal(refused.responses[1].error.data.category, "input"); + assert.deepEqual(fixture.requests.slice(sent).map((request) => request.method), + ["initialize", "notifications/initialized"], "no piece of the oversized line reaches the server"); +}); + test("pre-initialize probes are answered without ending the session", async (t) => { const tools = { tools: [] }; const fixture = await server(t, (body, response) => json(response, { jsonrpc: "2.0", id: body.id, result: tools })); From 0e3cd2ce80f153d508e6918296a6b0985769a74b Mon Sep 17 00:00:00 2001 From: Atharva Padmakar Deshmukh <175204585+bablamon@users.noreply.github.com> Date: Thu, 3 Dec 2026 03:57:22 +0530 Subject: [PATCH 3/3] Load the bridge module beside the resolved test binary The in-process test imported bridge.js beside PROOFLINE_MCP_TEST_BIN as given. When that path is a symlink, such as node_modules/.bin/proofline-mcp on macOS and Linux, it looked in the link's folder and the whole suite failed to load. Spawning the binary had always followed the link, so this was a regression from the previous commit. Resolve the path first. --- tests/bridge.test.js | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/tests/bridge.test.js b/tests/bridge.test.js index 3f5f564..a884e82 100644 --- a/tests/bridge.test.js +++ b/tests/bridge.test.js @@ -1,6 +1,7 @@ import assert from "node:assert/strict"; import { spawn } from "node:child_process"; import { once } from "node:events"; +import { realpathSync } from "node:fs"; import { createServer } from "node:http"; import { Readable, Writable } from "node:stream"; import test from "node:test"; @@ -8,7 +9,8 @@ import { fileURLToPath, pathToFileURL } from "node:url"; const binary = process.env.PROOFLINE_MCP_TEST_BIN ?? fileURLToPath(new URL("../src/cli.js", import.meta.url)); // The bridge module next to the binary under test, so packed runs test the packed copy. -const { bridge } = await import(new URL("bridge.js", pathToFileURL(binary)).href); +// Resolve a symlinked binary (such as node_modules/.bin/proofline-mcp) to its target first. +const { bridge } = await import(new URL("bridge.js", pathToFileURL(realpathSync(binary))).href); const token = "test-token-never-print"; const protocol = "2025-11-25"; const initialize = { jsonrpc: "2.0", id: "init", method: "initialize", params: {