Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 26 additions & 0 deletions src/invariant-judge.ts
Original file line number Diff line number Diff line change
Expand Up @@ -325,9 +325,29 @@ function armed(): boolean {
return invariantJudgeEnabled();
}

/** Bodies outside this size range are skipped by the judge — a full
* advisory-side JSON.parse of a #3128-scale multi-GiB body is an OOM/event-loop
* stall vector, and the judge's own precedent (SEAM_BODY_CAP, 512 KiB) already
* treats oversized bodies as "partial forensics: silently skip". Skips log
* at debug (visible when converging a bug), never warn: a skipped diagnostic
* is not a defect. Const by design — no knob until an operator asks (#4003). */
const JUDGE_BODY_CAP = 4 * 1024 * 1024;

/** Byte length of a judge input body — 0 when absent; strings measured as
* UTF-8 bytes so strings and Buffers compare like-for-like against the cap. */
function judgeBodyLen(body: string | Buffer | undefined): number {
if (!body) return 0;
return typeof body === "string" ? Buffer.byteLength(body) : body.length;
}

export function maybeRunInvariantJudge(input: InvariantJudgeInput): void {
if (!armed()) return;
try {
const bodyLen = judgeBodyLen(input.body);
if (bodyLen === 0 || bodyLen > JUDGE_BODY_CAP) {
loggerLog("debug", `[invariant-judge] body ${bodyLen}B outside judge range (≤${JUDGE_BODY_CAP}B) — skipping (advisory layer, #4003 cap)`);
return;
}
const entry = input.originalMessages ?? [];
const exitCores0 = parseExitCores(input.body, input.protocol);
if (!exitCores0) return; // unparseable/unknown wire → skip, never false-positive
Expand Down Expand Up @@ -357,6 +377,12 @@ export function maybeJudgeContinuity(
): void {
if (!armed() || !prev) return;
try {
const prevLen = judgeBodyLen(prev);
const bodyLen = judgeBodyLen(body);
if (prevLen === 0 || prevLen > JUDGE_BODY_CAP || bodyLen === 0 || bodyLen > JUDGE_BODY_CAP) {
loggerLog("debug", `[invariant-judge] continuity bodies ${prevLen}/${bodyLen}B outside judge range (≤${JUDGE_BODY_CAP}B) — skipping (advisory layer, #4003 cap)`);
return;
}
const entry = parseExitCores(prev, protocol);
const exitCores0 = parseExitCores(body, protocol);
if (!entry || !exitCores0) return;
Expand Down
47 changes: 47 additions & 0 deletions tests/invariant-judge.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -317,3 +317,50 @@ test("PR-D closure: a REAL mid-history rewrite still flags through the echo norm
assert.equal(v.ok, false);
assert.ok(v.violations.some((x) => x.kind === "mid-history-rewrite"));
});

test("#4003 cap: bodies over JUDGE_BODY_CAP (4 MiB) skip the judge silently — no parse, no verdict, no throw", () => {
const seen: JudgeVerdict[] = [];
const lines: Array<[string, string]> = [];
_setInvariantJudgeForTest(true, (v) => seen.push(v));
setLogCapture((level, msg) => lines.push([level, msg]));
try {
// 5 MiB of filler in ONE field: a would-be rewrite hides inside so a judge
// that (wrongly) parsed it would go red — the cap must skip instead.
const big = "x".repeat(5 * 1024 * 1024);
const body = JSON.stringify({ model: "m", messages: [
{ role: "user", content: big },
{ role: "assistant", content: "hi REWRITTEN BY DEFECT" },
] });
maybeRunInvariantJudge({ originalMessages: [], body, protocol: "openai", sessionId: "s-cap" });
assert.equal(seen.length, 0, "oversized body must not produce a verdict");
const hit = lines.find(([, m]) => m.includes("skipping"));
assert.ok(hit, "skip must be observable at debug");
assert.equal(hit![0], "debug");
// Buffer input takes the same path (relays hand Buffers too).
maybeRunInvariantJudge({ originalMessages: [], body: Buffer.from(body, "utf8"), protocol: "openai", sessionId: "s-cap" });
assert.equal(seen.length, 0);
// Absent body: the gate owns the empty case — zero verdicts, accurate log.
maybeRunInvariantJudge({ originalMessages: [], protocol: "openai", sessionId: "s-cap" });
assert.equal(seen.length, 0);
// Continuity lane: prev oversized OR next oversized → skip both.
const small = JSON.stringify({ model: "m", messages: [{ role: "user", content: "q" }] });
maybeJudgeContinuity(body, small, "openai", "s-cap");
maybeJudgeContinuity(small, body, "openai", "s-cap");
assert.equal(seen.length, 0);
// Continuity skips are observable at debug too (#4021 spec: both entries log).
const contSkips = lines.filter(([, m]) => m.includes("continuity") && m.includes("skipping"));
assert.ok(contSkips.length >= 2, "both continuity skips must be observable");
assert.ok(contSkips.every(([lvl]) => lvl === "debug"));
// Sanity: a small body still judges (cap did not brick the happy path).
const tiny = JSON.stringify({ model: "m", messages: [
{ role: "user", content: "hello" },
{ role: "assistant", content: "hi REWRITTEN BY DEFECT" },
] });
maybeRunInvariantJudge({ originalMessages: [mk("user", "text", "hello"), mk("assistant", "text", "hi")], body: tiny, protocol: "openai", sessionId: "s-cap" });
assert.equal(seen.length, 1);
assert.equal(seen[0].ok, false);
} finally {
_setInvariantJudgeForTest(false);
setLogCapture(null);
}
});
Loading