mirror of
https://github.com/morluto/rea.git
synced 2026-11-11 01:13:39 +00:00
* 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>
185 lines
8.3 KiB
TypeScript
185 lines
8.3 KiB
TypeScript
import { z } from "zod";
|
|
|
|
import { AnalysisResourceConstraintError } from "../domain/analysisErrorCore.js";
|
|
import { ConfigurationError } from "../domain/configurationErrors.js";
|
|
import { err, ok, type Result } from "../domain/result.js";
|
|
|
|
export const BRIDGE_MAX_REQUEST_BYTES_SETTING = "REA_BRIDGE_MAX_REQUEST_BYTES";
|
|
|
|
/** Request payload ceiling used when the setting is unset. Today's fixed constant. */
|
|
export const DEFAULT_BRIDGE_MAX_REQUEST_BYTES = 16 * 1024 * 1024;
|
|
|
|
/**
|
|
* Smallest ceiling that still admits the largest bounded bridge request.
|
|
* Hopper symbol names stop at 1024 UTF-16 units. A name of U+0001 repeated
|
|
* that many times JSON-escapes to six bytes per character. The Hopper
|
|
* `set_address_name` request for that name, a max-safe id, and a 64-hex token
|
|
* is 6322 bytes. The authenticated Cutter frame of a command carrying the same
|
|
* name is 7464 bytes. The required Cutter recovery status, 64 receipts with
|
|
* an unknown path, is smaller. A status path that fits is kept. A path that
|
|
* does not fit is null, so the client does not reuse the descriptor path with
|
|
* the new generation. Hello and a minimal command frame are smaller.
|
|
* Comments, search text, and Cutter command text are unbounded; this ceiling
|
|
* is what limits them. The floor stays below the 16 MiB default so a caller
|
|
* can lower the ceiling.
|
|
*/
|
|
export const MINIMUM_BRIDGE_MAX_REQUEST_BYTES = 7464;
|
|
|
|
/**
|
|
* Largest configurable request, in encoded payload bytes.
|
|
*
|
|
* The ceiling is the final JSON payload, not the raw command. Callers measure
|
|
* escaped JSON against it before `JSON.stringify`, so a U+0001 expansion is
|
|
* counted once. Node's single-string ceiling is 536870888 UTF-16 code units.
|
|
* The payload budget excludes the terminating LF, and that LF is the one code
|
|
* unit appended after the check. The maximum leaves room for it. Cutter signs
|
|
* that payload from bounded JSON pieces and writes it in 64 KiB chunks. The
|
|
* bridge looks for the framing LF only in the bytes just read. The client
|
|
* unescapes each socket chunk into one response string, checks the proof, and
|
|
* then parses that string. A fitting response does not keep the paged wire
|
|
* frame, a second concatenated copy, and an outer JSON string.
|
|
*/
|
|
const NODE_MAX_STRING_LENGTH = 536_870_888;
|
|
const FRAMING_LF_CODE_UNITS = 1;
|
|
export const MAXIMUM_BRIDGE_MAX_REQUEST_BYTES =
|
|
NODE_MAX_STRING_LENGTH - FRAMING_LF_CODE_UNITS;
|
|
|
|
/** Effective bridge request ceiling and where its value came from. */
|
|
export interface BridgeMaxRequestBytes {
|
|
readonly maximumBytes: number;
|
|
readonly source: "default" | "setting";
|
|
}
|
|
|
|
export const DEFAULT_BRIDGE_MAX_REQUEST: BridgeMaxRequestBytes = {
|
|
maximumBytes: DEFAULT_BRIDGE_MAX_REQUEST_BYTES,
|
|
source: "default",
|
|
};
|
|
|
|
export const bridgeMaxRequestBytesConstraint = `${BRIDGE_MAX_REQUEST_BYTES_SETTING} must be a positive decimal integer from ${String(MINIMUM_BRIDGE_MAX_REQUEST_BYTES)} to ${String(MAXIMUM_BRIDGE_MAX_REQUEST_BYTES)} bytes; the default is ${String(DEFAULT_BRIDGE_MAX_REQUEST_BYTES)} bytes (16 MiB). A value below ${String(MINIMUM_BRIDGE_MAX_REQUEST_BYTES)} bytes cannot carry the largest admitted symbol-name command, and a larger value is refused so the encoded payload plus its framing LF stays one string under Node's single-string ceiling.`;
|
|
|
|
const decimalInteger = /^[1-9]\d*$/u;
|
|
|
|
const withinRequestRange = (value: string): boolean => {
|
|
// The pattern check reports a non-decimal value. Refine only bounds integers.
|
|
if (!decimalInteger.test(value)) return true;
|
|
const parsed = BigInt(value);
|
|
return (
|
|
parsed >= BigInt(MINIMUM_BRIDGE_MAX_REQUEST_BYTES) &&
|
|
parsed <= BigInt(MAXIMUM_BRIDGE_MAX_REQUEST_BYTES)
|
|
);
|
|
};
|
|
|
|
/** Optional decimal byte count for the Hopper and Cutter request payload ceiling. */
|
|
export const bridgeMaxRequestBytesSchema = z
|
|
.string()
|
|
.regex(decimalInteger, { error: bridgeMaxRequestBytesConstraint })
|
|
.refine(withinRequestRange, { error: bridgeMaxRequestBytesConstraint })
|
|
.transform(Number);
|
|
|
|
/** Resolve the effective ceiling from one environment value. */
|
|
export const parseBridgeMaxRequestBytes = (
|
|
value: string | undefined,
|
|
): Result<BridgeMaxRequestBytes, ConfigurationError> => {
|
|
if (value === undefined) return ok(DEFAULT_BRIDGE_MAX_REQUEST);
|
|
const parsed = bridgeMaxRequestBytesSchema.safeParse(value);
|
|
return parsed.success
|
|
? ok({ maximumBytes: parsed.data, source: "setting" })
|
|
: err(
|
|
new ConfigurationError(bridgeMaxRequestBytesConstraint, {
|
|
settings: [
|
|
{
|
|
setting: BRIDGE_MAX_REQUEST_BYTES_SETTING,
|
|
constraint: bridgeMaxRequestBytesConstraint,
|
|
},
|
|
],
|
|
}),
|
|
);
|
|
};
|
|
|
|
/**
|
|
* Resolve the ceiling from a composition root's selected environment. An
|
|
* invalid setting is refused, not ignored. Callers that already resolved it
|
|
* at startup pass that value through instead of reading the environment again.
|
|
*/
|
|
export const resolveBridgeMaxRequestBytes = (
|
|
environment: Readonly<Record<string, string | undefined>>,
|
|
): BridgeMaxRequestBytes => {
|
|
const parsed = parseBridgeMaxRequestBytes(
|
|
environment[BRIDGE_MAX_REQUEST_BYTES_SETTING],
|
|
);
|
|
if (!parsed.ok) throw parsed.error;
|
|
return parsed.value;
|
|
};
|
|
|
|
/** How to read and change the effective bridge request ceiling. */
|
|
export const bridgeMaxRequestBytesRecovery = (
|
|
limit: BridgeMaxRequestBytes,
|
|
): string =>
|
|
[
|
|
`The effective ${BRIDGE_MAX_REQUEST_BYTES_SETTING} value is ${String(limit.maximumBytes)} bytes (source ${limit.source}; default ${String(DEFAULT_BRIDGE_MAX_REQUEST_BYTES)}, floor ${String(MINIMUM_BRIDGE_MAX_REQUEST_BYTES)}, maximum ${String(MAXIMUM_BRIDGE_MAX_REQUEST_BYTES)}).`,
|
|
`Set ${BRIDGE_MAX_REQUEST_BYTES_SETTING} in the environment that starts REA to a decimal integer from ${String(MINIMUM_BRIDGE_MAX_REQUEST_BYTES)} to ${String(MAXIMUM_BRIDGE_MAX_REQUEST_BYTES)}, then restart REA.`,
|
|
"Restart Hopper or Cutter from that same environment so the bridge uses this ceiling.",
|
|
].join(" ");
|
|
|
|
const limitFacts = (
|
|
limit: BridgeMaxRequestBytes,
|
|
): { readonly max_request_bytes: number } => ({
|
|
max_request_bytes: limit.maximumBytes,
|
|
});
|
|
|
|
/** Refuse one request whose payload is above the effective ceiling. */
|
|
export const bridgeRequestOversizeError = (
|
|
operation: string,
|
|
limit: BridgeMaxRequestBytes,
|
|
payloadBytes: number,
|
|
peer: "Hopper" | "Cutter",
|
|
): AnalysisResourceConstraintError => {
|
|
const recovery = bridgeMaxRequestBytesRecovery(limit);
|
|
const refusal =
|
|
peer === "Cutter"
|
|
? `Cutter request exceeds its authenticated frame limit of ${String(limit.maximumBytes)} bytes; no command was sent.`
|
|
: `Hopper request exceeds its ${String(limit.maximumBytes)}-byte payload limit; no request was sent.`;
|
|
return new AnalysisResourceConstraintError(
|
|
operation,
|
|
"transport",
|
|
`${refusal} ${recovery}`,
|
|
{ ...limitFacts(limit), payload_bytes: payloadBytes },
|
|
{ remediationAction: recovery, retryable: false },
|
|
);
|
|
};
|
|
|
|
/** Refuse a command when the bridge advertisement and the client ceiling differ. */
|
|
export const bridgeRequestLimitMismatchError = (
|
|
operation: string,
|
|
limit: BridgeMaxRequestBytes,
|
|
bridgeBytes: number,
|
|
peer: "Hopper" | "Cutter",
|
|
sent = peer === "Cutter" ? "No command was sent." : "No request was sent.",
|
|
): AnalysisResourceConstraintError => {
|
|
const recovery = bridgeMaxRequestBytesRecovery(limit);
|
|
return new AnalysisResourceConstraintError(
|
|
operation,
|
|
"transport",
|
|
`The ${peer} bridge ceiling is ${String(bridgeBytes)} bytes and the REA client ceiling is ${String(limit.maximumBytes)} bytes. ${sent} An out-of-band ${peer} process defaults to ${String(DEFAULT_BRIDGE_MAX_REQUEST_BYTES)} bytes when ${BRIDGE_MAX_REQUEST_BYTES_SETTING} is unset. ${recovery}`,
|
|
{
|
|
bridge_max_request_bytes: bridgeBytes,
|
|
client_max_request_bytes: limit.maximumBytes,
|
|
},
|
|
{ remediationAction: recovery, retryable: false },
|
|
);
|
|
};
|
|
|
|
/** Refuse a Cutter response that exceeds the same configured frame ceiling. */
|
|
export const bridgeResponseOversizeError = (
|
|
limit: BridgeMaxRequestBytes,
|
|
): AnalysisResourceConstraintError => {
|
|
const recovery = bridgeMaxRequestBytesRecovery(limit);
|
|
return new AnalysisResourceConstraintError(
|
|
"cutter_command",
|
|
"transport",
|
|
`Cutter bridge response exceeded its framed transport limit of ${String(limit.maximumBytes)} bytes. ${recovery}`,
|
|
limitFacts(limit),
|
|
{ remediationAction: recovery, retryable: false },
|
|
);
|
|
};
|