Files
reverse-engineering-anything/tests/boundary/providers/cutterBridgeRequestLimit.test.ts
T
47353e494d feat(config): make the bridge request ceiling configurable (#6376)
* fix(reconstruction): dedupe residual unknown ids with uniqueSorted

Current main calls uniqueBy, which is not defined, so typecheck fails.
Residual unknown ids are strings, and the adjacent lists already use
uniqueSorted.

Co-authored-by: morluto <morluto@users.noreply.github.com>

* feat(config): make the bridge request ceiling configurable

REA_BRIDGE_MAX_REQUEST_BYTES replaces the hard-coded 16 MiB Hopper and
Cutter request payload ceiling. The default stays 16777216 bytes. Values
outside 7464 to 268435456 bytes are configuration_invalid.

Part of #6176.

Co-authored-by: morluto <morluto@users.noreply.github.com>

* fix(cutter): keep cleanup ownership when a sent command's reply is oversized

An authenticated command may already have changed the Cutter document
when its reply exceeds the frame ceiling. Return the existing unknown
execution, with the ceiling diagnostic in the message, so receipt
cleanup still waits. A frame that overflows before the command is sent
remains a resource constraint.

Co-authored-by: morluto <morluto@users.noreply.github.com>

* style: format the bridge request ceiling sources

oxfmt --check failed on the five files that set and test
REA_BRIDGE_MAX_REQUEST_BYTES. Formatting only; an oversized Cutter
reply still stays an unknown execution.

Co-authored-by: morluto <morluto@users.noreply.github.com>

* fix(cutter): exclude the LF from the response frame budget

REA_BRIDGE_MAX_REQUEST_BYTES counts payload bytes. The Cutter response
reader and authenticated encoder were reserving one byte for the
terminating LF, so an exact-limit response was rejected. A reply that
exceeds the ceiling after the command was sent still stays an unknown
execution.

Co-authored-by: morluto <morluto@users.noreply.github.com>

* fix(bridge): measure escaped JSON before allocating the request

A command of U+0001 characters can expand sixfold. JSON.stringify of
that expansion can throw past Node's string ceiling before the payload
check. Count the escaped size within REA_BRIDGE_MAX_REQUEST_BYTES
first and refuse it as a resource constraint.

Co-authored-by: morluto <morluto@users.noreply.github.com>

* fix(config): derive the bridge request maximum from JSON expansion

A 256 MiB ceiling still lets a shorter U+0001 command expand past Node's
536870888-character string limit. The maximum is now
floor((536870888 - 296) / 7) = 76695798, the longest such command whose
authenticated frame fits in one string. Requests are still measured
before JSON.stringify.

Co-authored-by: morluto <morluto@users.noreply.github.com>

* fix(bridge): size the Cutter frame deadline and send the measured Hopper payload

The authenticated Cutter command is encoded after the challenge, so a
one-second read rejected an in-budget frame. That read now allows one
second per mebibyte of the ceiling and never waits longer than 25
seconds. Hello stays at one second. Hopper writes the payload it already
measured instead of serializing the same request again.

Co-authored-by: morluto <morluto@users.noreply.github.com>

* fix(config): bound the bridge request maximum by the serialized string

The ceiling already measures escaped JSON, so dividing Node's string limit by the seven-byte expansion rejected encoded payloads that still fit. The maximum is 536870887, one byte under that string ceiling, leaving room for the framing LF.

Co-authored-by: morluto <morluto@users.noreply.github.com>

* fix(hopper): serialize a bridge request when it reaches the wire

The queue kept each encoded frame, plus the caller's params, until Hopper finished the active request. A raised ceiling made a few waiting calls enough to exhaust the heap. The FIFO now keeps the params, and the frame is encoded only for the request being written.

Co-authored-by: morluto <morluto@users.noreply.github.com>

* fix(cutter): hash the bridge proof prefix and payload separately

An exact-limit Cutter frame plus the HMAC prefix exceeds Node's single-string ceiling, so the client threw before sending an admitted command. The prefix and payload are now separate HMAC updates, including the response proof.

Co-authored-by: morluto <morluto@users.noreply.github.com>

* fix(cutter): keep recovery fields when a status path exceeds the frame

A long current_file made the status reply miss the ceiling, and the fallback then dropped session_id and the completion receipts. The status now omits the path and version and still returns the session, connection proof, and up to 64 receipts. That required recovery frame is smaller than the 7464 floor.

Co-authored-by: morluto <morluto@users.noreply.github.com>

* fix(cutter): report an unknown path when status identity does not fit

A bounded status kept the new document generation and omitted the path, so the client filled in the descriptor's older path. The observed path is sent when it fits. When it does not, current_file is null and is not replaced with that stale path.

Co-authored-by: morluto <morluto@users.noreply.github.com>

* fix(cutter): count structured replies by their encoded JSON size

The response guard charged 16 bytes per primitive and two bytes per list slot, so a dense array under the configured ceiling was replaced with output_truncated. Numbers, booleans, null, and container punctuation now use their JSON lengths.

Co-authored-by: morluto <morluto@users.noreply.github.com>

* fix(cutter): stream authenticated responses in bounded chunks

A fitting reply was signed from retained raw bytes, escaped bytes, and a
joined frame, so a ceiling near 512 MiB could exhaust Cutter before send.
Measure and sign JSON pieces, then write the same frame in 64 KiB chunks.

Co-authored-by: morluto <morluto@users.noreply.github.com>

* fix(bridge): keep Cutter admission from stalling or hiding sessions

Read an unauthenticated frame off the accept loop. A command reply that
does not fit keeps completion and generation and reports an oversized path
as null. Discovery reports a ceiling mismatch instead of an absent session.
The maximum HMAC proof is opt-in, and both bridges reject non-ASCII digits.

Co-authored-by: morluto <morluto@users.noreply.github.com>

* fix(cutter): bound frame scans and client response copies

Search only newly read bytes for the framing LF. Unescape each socket
chunk into one response string, check the proof, and then parse it.

Co-authored-by: morluto <morluto@users.noreply.github.com>

* fix(cutter): keep completion, admission, and dense replies bounded

Report completed_connection only through connections that have
terminated. Cap connection threads and keep the listener up if one
fails to start. Batch scalar JSON and fail discovery when any ceiling
mismatches.

Co-authored-by: morluto <morluto@users.noreply.github.com>

* fix(cutter): hash requests in parts and scan escapes once

Update the client-proof prefix and request separately. Stop the response
decoder at the first quote or backslash instead of rescanning the tail.

Co-authored-by: morluto <morluto@users.noreply.github.com>

* fix(cutter): confirm live ceiling mismatches and omitted output

A stale descriptor from a previous ceiling is not a discovery mismatch
until its hello proves a live authenticated bridge. A response discarded
for the frame ceiling after dispatch reports output_truncated.

Co-authored-by: morluto <morluto@users.noreply.github.com>

* test(cutter): close the unauthenticated ceiling-mismatch fixture

The listener that fails the hello proof is destroyed after its challenge
so discovery cleanup does not wait on that socket.

Co-authored-by: morluto <morluto@users.noreply.github.com>

---------

Co-authored-by: Cursor Agent <cursoragent@cursor.com>
Co-authored-by: morluto <morluto@users.noreply.github.com>
2026-10-23 06:11:21 +08:00

467 lines
14 KiB
TypeScript

import { createServer } from "node:net";
import { chmod, mkdtemp, rm } from "node:fs/promises";
import { tmpdir } from "node:os";
import { join } from "node:path";
import { expect, it, onTestFinished, vi } from "vitest";
import { CutterBridgeClient } from "../../../src/cutter/CutterBridgeClient.js";
import { CutterBridgeService } from "../../../src/cutter/CutterBridgeService.js";
import { ConfigurationError } from "../../../src/domain/configurationErrors.js";
import { AnalysisResourceConstraintError } from "../../../src/domain/analysisErrorCore.js";
import {
authenticatedServer,
writeDescriptor,
} from "../../fixtures/cutterBridgeProtocol.js";
const SESSION = "00000000-0000-4000-8000-000000000001";
const TOKEN = "bridge-token-0123456789abcdef";
const privateDirectory = async (): Promise<string> => {
const directory = await mkdtemp(join(tmpdir(), "rea-cutter-request-limit-"));
await chmod(directory, 0o700);
onTestFinished(async () => {
await rm(directory, { recursive: true, force: true });
});
return directory;
};
const listen = async (
server: ReturnType<typeof createServer>,
): Promise<number> => {
await new Promise<void>((resolve) => server.listen(0, "127.0.0.1", resolve));
const address = server.address();
if (address === null || typeof address === "string")
throw new Error("Expected an ephemeral IPv4 listener");
onTestFinished(
() =>
new Promise<void>((resolve) => {
server.close(() => resolve());
}),
);
return address.port;
};
const clientFor = (directory: string, ceiling: string): CutterBridgeClient => {
const client = new CutterBridgeClient({
REA_CUTTER_BRIDGE_DIR: directory,
REA_BRIDGE_MAX_REQUEST_BYTES: ceiling,
});
onTestFinished(() => client.close());
return client;
};
it("rejects a bridge ceiling below the floor at client construction", () => {
expect(
() =>
new CutterBridgeClient({
REA_BRIDGE_MAX_REQUEST_BYTES: "7463",
}),
).toThrow(ConfigurationError);
});
const closedPort = async (): Promise<number> => {
const server = createServer();
const port = await listen(server);
await new Promise<void>((resolve) => {
server.close(() => resolve());
});
return port;
};
const ceilingMismatch = (error: unknown): boolean => {
expect(error).toBeInstanceOf(AnalysisResourceConstraintError);
if (!(error instanceof AnalysisResourceConstraintError)) return false;
expect(error.operation).toBe("list_cutter_sessions");
expect(error.resource).toBe("transport");
expect(error.reportedLimits).toMatchObject({
bridge_max_request_bytes: 16_777_216,
client_max_request_bytes: 8192,
});
expect(error.reason).toContain(
"No status request was sent to the mismatched session.",
);
expect(error.remediationAction).toContain("Set REA_BRIDGE_MAX_REQUEST_BYTES");
return true;
};
it("ignores a stale Cutter descriptor whose advertised ceiling differs", async () => {
const directory = await privateDirectory();
await writeDescriptor(directory, SESSION, await closedPort(), TOKEN);
const client = clientFor(directory, "8192");
await expect(client.listSessions()).resolves.toMatchObject({
sessions: [],
discovery_status: "no_live_bridge_found",
});
});
it("returns the matching session when the other ceiling descriptor is stale", async () => {
const directory = await privateDirectory();
const server = authenticatedServer(TOKEN, SESSION, () => undefined);
const port = await listen(server);
const other = "00000000-0000-4000-8000-000000000002";
await writeDescriptor(directory, SESSION, port, TOKEN, {
maxRequestBytes: 8192,
});
await writeDescriptor(directory, other, await closedPort(), TOKEN);
const client = clientFor(directory, "8192");
await expect(client.listSessions()).resolves.toMatchObject({
discovery_status: "sessions_found",
sessions: [{ session_id: SESSION }],
});
});
it("fails discovery when a live authenticated session has a different ceiling", async () => {
const directory = await privateDirectory();
const server = authenticatedServer(TOKEN, SESSION, () => undefined);
const port = await listen(server);
const other = "00000000-0000-4000-8000-000000000002";
const requests: string[] = [];
let connections = 0;
const mismatched = authenticatedServer(
TOKEN,
other,
(request) => {
requests.push(String(request.kind));
},
true,
);
mismatched.on("connection", () => {
connections += 1;
});
const mismatchedPort = await listen(mismatched);
await writeDescriptor(directory, SESSION, port, TOKEN, {
maxRequestBytes: 8192,
});
await writeDescriptor(directory, other, mismatchedPort, TOKEN);
const client = clientFor(directory, "8192");
await expect(client.listSessions()).rejects.toSatisfy(ceilingMismatch);
expect(connections).toBe(1);
expect(requests).toEqual([]);
});
it("does not treat an unauthenticated listener as a Cutter ceiling mismatch", async () => {
const directory = await privateDirectory();
const server = authenticatedServer(TOKEN, SESSION, () => undefined);
const port = await listen(server);
const other = "00000000-0000-4000-8000-000000000002";
const stranger = createServer((socket) => {
socket.write(
`${JSON.stringify({
session_id: other,
nonce: "ab".repeat(32),
proof: "00".repeat(32),
})}\n`,
);
socket.destroy();
});
const strangerPort = await listen(stranger);
await writeDescriptor(directory, SESSION, port, TOKEN, {
maxRequestBytes: 8192,
});
await writeDescriptor(directory, other, strangerPort, TOKEN);
const client = clientFor(directory, "8192");
await expect(client.listSessions()).resolves.toMatchObject({
discovery_status: "sessions_found",
sessions: [{ session_id: SESSION }],
});
});
it("reports a Cutter ceiling mismatch before opening a command socket", async () => {
const directory = await privateDirectory();
await writeDescriptor(directory, SESSION, 1, TOKEN);
const client = clientFor(directory, "33554432");
await expect(
client.execute({
sessionId: SESSION,
expectedGeneration: 0,
command: "ij",
json: false,
}),
).rejects.toSatisfy((error: unknown) => {
expect(error).toBeInstanceOf(AnalysisResourceConstraintError);
if (!(error instanceof AnalysisResourceConstraintError)) return false;
expect(error.reason).toContain("REA_BRIDGE_MAX_REQUEST_BYTES");
expect(error.reason).toContain("33554432");
expect(error.reason).toContain("16777216");
expect(error.reason).toContain("No command was sent.");
expect(error.reason).not.toContain("no runtime override");
expect(error.remediationAction).toContain(
"Set REA_BRIDGE_MAX_REQUEST_BYTES",
);
return true;
});
});
it("lowers the Cutter command ceiling for both the refusal and a live command", async () => {
const directory = await privateDirectory();
let connections = 0;
const server = authenticatedServer(TOKEN, SESSION, (request, respond) => {
respond({
ok: true,
session_id: SESSION,
output: request.command,
execution_state: "complete",
document_generation: 0,
identity_status: "partial",
});
});
server.on("connection", () => {
connections += 1;
});
const port = await listen(server);
await writeDescriptor(directory, SESSION, port, TOKEN, {
maxRequestBytes: 8192,
});
const client = clientFor(directory, "8192");
await expect(
client.execute({
sessionId: SESSION,
expectedGeneration: 0,
command: "c".repeat(8000),
json: false,
}),
).rejects.toSatisfy((error: unknown) => {
expect(error).toBeInstanceOf(AnalysisResourceConstraintError);
if (!(error instanceof AnalysisResourceConstraintError)) return false;
expect(error.reason).toContain("authenticated frame limit of 8192 bytes");
expect(error.reason).toContain("no command was sent");
expect(error.reason).toContain("REA_BRIDGE_MAX_REQUEST_BYTES");
expect(error.reason).not.toContain("no runtime override");
return true;
});
expect(connections).toBe(0);
await expect(
client.execute({
sessionId: SESSION,
expectedGeneration: 0,
command: "ij",
json: false,
}),
).resolves.toMatchObject({
output: "ij",
executionState: "complete",
});
});
const responseEnvelopeBytes = (output: string): number => {
const response = JSON.stringify({
ok: true,
output,
execution_state: "complete",
document_generation: 0,
identity_status: "partial",
});
return Buffer.byteLength(JSON.stringify({ response, proof: "0".repeat(64) }));
};
const outputForEnvelope = (target: number): string => {
const count = target - responseEnvelopeBytes("");
if (responseEnvelopeBytes("A".repeat(count)) !== target)
throw new Error(`expected an exact ${String(target)}-byte response frame`);
return "A".repeat(count);
};
it("refuses an escaped Cutter command before serializing it", async () => {
const directory = await privateDirectory();
let connections = 0;
const server = authenticatedServer(TOKEN, SESSION, (_request, respond) => {
respond({
ok: true,
output: null,
execution_state: "complete",
document_generation: 0,
identity_status: "partial",
});
});
server.on("connection", () => {
connections += 1;
});
const port = await listen(server);
await writeDescriptor(directory, SESSION, port, TOKEN, {
maxRequestBytes: 8192,
});
const client = clientFor(directory, "8192");
const command = "\u0001".repeat(2000);
const stringify = vi.spyOn(JSON, "stringify");
try {
await expect(
client.execute({
sessionId: SESSION,
expectedGeneration: 0,
command,
json: false,
}),
).rejects.toSatisfy((error: unknown) => {
expect(error).toBeInstanceOf(AnalysisResourceConstraintError);
if (!(error instanceof AnalysisResourceConstraintError)) return false;
expect(error.reason).toContain("authenticated frame limit of 8192 bytes");
expect(error.reason).toContain("no command was sent");
expect(error.reason).toContain("REA_BRIDGE_MAX_REQUEST_BYTES");
return true;
});
expect(connections).toBe(0);
expect(
stringify.mock.calls.some((call) => {
const value: unknown = call[0];
return (
typeof value === "object" &&
value !== null &&
"command" in value &&
value.command === command
);
}),
).toBe(false);
} finally {
stringify.mockRestore();
}
});
it("accepts a Cutter response at the ceiling and excludes the terminating LF", async () => {
const directory = await privateDirectory();
const ceiling = 8192;
const output = outputForEnvelope(ceiling);
const server = authenticatedServer(TOKEN, SESSION, (request, respond) => {
if (request.kind === "ack") {
respond({ ok: true, acknowledged: true });
return;
}
const command = request.command;
const body = {
ok: true,
output: command === "over" ? `${output}A` : output,
execution_state: "complete",
document_generation: 0,
identity_status: "partial",
};
if (command === "split") respond(body, { chunkBytes: ceiling });
else respond(body);
});
const port = await listen(server);
await writeDescriptor(directory, SESSION, port, TOKEN, {
maxRequestBytes: ceiling,
});
const client = clientFor(directory, String(ceiling));
await expect(
client.execute({
sessionId: SESSION,
expectedGeneration: 0,
command: "exact",
json: false,
}),
).resolves.toMatchObject({
output,
executionState: "complete",
});
await expect(
client.execute({
sessionId: SESSION,
expectedGeneration: 0,
command: "split",
json: false,
}),
).resolves.toMatchObject({
output,
executionState: "complete",
});
const oversize = await new CutterBridgeService(client).execute({
session_id: SESSION,
expected_generation: 0,
command: "over",
json: false,
});
expect(oversize).toMatchObject({
ok: false,
error: {
_tag: "AnalysisOutputError",
partialObservation: {
normalized_result: {
execution_state: "unknown",
error: "transport-response-missing",
output_truncated: true,
message: expect.stringContaining("8192"),
},
limitations: expect.arrayContaining([
"The command completed or may have completed, but output exceeded the bridge transport limit and was omitted.",
]),
},
},
});
expect(oversize).toMatchObject({
error: {
partialObservation: {
normalized_result: {
message: expect.stringContaining("REA_BRIDGE_MAX_REQUEST_BYTES"),
},
},
},
});
expect(oversize).toMatchObject({
error: {
partialObservation: {
normalized_result: {
message: expect.stringContaining("may have completed"),
},
},
},
});
expect(oversize).toMatchObject({
error: {
partialObservation: {
normalized_result: {
message: expect.not.stringContaining("no runtime override"),
},
},
},
});
});
it("sends a Cutter command above 16 MiB when both sides raise the ceiling", async () => {
const directory = await privateDirectory();
const ceiling = 17 * 1024 * 1024;
const command = "h".repeat(16 * 1024 * 1024);
const server = authenticatedServer(TOKEN, SESSION, (request, respond) => {
respond({
ok: true,
session_id: SESSION,
output: typeof request.command === "string" ? request.command.length : 0,
execution_state: "complete",
document_generation: 0,
identity_status: "partial",
});
});
const port = await listen(server);
await writeDescriptor(directory, SESSION, port, TOKEN, {
maxRequestBytes: ceiling,
});
const raised = clientFor(directory, String(ceiling));
await expect(
raised.execute({
sessionId: SESSION,
expectedGeneration: 0,
command,
json: false,
}),
).resolves.toMatchObject({
output: command.length,
executionState: "complete",
});
const directoryForDefault = await privateDirectory();
await writeDescriptor(directoryForDefault, SESSION, port, TOKEN, {
maxRequestBytes: ceiling,
});
const defaults = new CutterBridgeClient({
REA_CUTTER_BRIDGE_DIR: directoryForDefault,
});
onTestFinished(() => defaults.close());
await expect(
defaults.execute({
sessionId: SESSION,
expectedGeneration: 0,
command: "ij",
json: false,
}),
).rejects.toBeInstanceOf(AnalysisResourceConstraintError);
});