From 0b1dcb413d8c1a7f64f3bebbf664c4f2f419718f Mon Sep 17 00:00:00 2001 From: morluto <76467478+morluto@users.noreply.github.com> Date: Sun, 22 Nov 2026 14:34:46 -0500 Subject: [PATCH] fix(rust): admit valid reports at the configured byte limit (#13570) --- src/rust/RustAuditableReader.test.ts | 32 +++++++++++++++++++++ src/rust/RustAuditableReader.ts | 42 ++++++++++++---------------- src/rust/RustBinaryProvider.ts | 35 ++++++++++------------- 3 files changed, 65 insertions(+), 44 deletions(-) diff --git a/src/rust/RustAuditableReader.test.ts b/src/rust/RustAuditableReader.test.ts index c4e9f511c5..c47276352e 100644 --- a/src/rust/RustAuditableReader.test.ts +++ b/src/rust/RustAuditableReader.test.ts @@ -382,6 +382,38 @@ it("rejects the first malformed package before schema issues accumulate", () => ); }); +it("admits an explicit empty feature list at its exact normalized graph size", () => { + const pkg = { + name: "a", + version: "1", + source: "s", + features: [], + root: true, + }; + const expected = { ...pkg, kind: null, dependencies: [] }; + const maxOutputBytes = Buffer.byteLength(JSON.stringify(expected)); + const payload = deflateSync( + Buffer.from(JSON.stringify({ format: 0, packages: [pkg] })), + ); + expect( + readAuditableSection(".dep-v0", 0, payload, { maxOutputBytes }).packages, + ).toEqual([expected]); + expect(() => + readAuditableSection(".dep-v0", 0, payload, { + maxOutputBytes: maxOutputBytes - 1, + }), + ).toThrow( + expect.objectContaining({ + failure: expect.objectContaining({ + kind: "limit", + limit: expect.objectContaining({ + name: "dependency graph output bytes", + maximum: maxOutputBytes - 1, + }), + }), + }), + ); +}); it("admits dependency expansion before allocating normalized or reverse edges", () => { const document = { packages: [ diff --git a/src/rust/RustAuditableReader.ts b/src/rust/RustAuditableReader.ts index b9969d42f0..29e6176f65 100644 --- a/src/rust/RustAuditableReader.ts +++ b/src/rust/RustAuditableReader.ts @@ -255,7 +255,7 @@ const MIN_PACKAGE_OUTPUT_BYTES = JSON.stringify({ source: "a", kind: null, dependencies: [], - features: null, + features: [], root: true, }).length; const MIN_EDGE_OUTPUT_BYTES = JSON.stringify({ @@ -290,32 +290,26 @@ const admitGraphExpansion = (value: unknown, maxOutputBytes: number): void => { admit(); for (const pkg of packages) { // Shape and index validity remain owned by documentSchema/rootIndex. - if ( - typeof pkg === "object" && - pkg !== null && - "features" in pkg && - Array.isArray(pkg.features) && - pkg.features.every((feature) => typeof feature === "string") + if (typeof pkg !== "object" || pkg === null) continue; + // An explicit empty array is the smallest feature record. Omission + // becomes null; valid explicit arrays only add to the initial bound. + const features = "features" in pkg ? pkg.features : undefined; + if (features === undefined) { + minimumBytes += 2; // "null" is two bytes longer than "[]". + } else if ( + Array.isArray(features) && + features.every((feature) => typeof feature === "string") ) { const featureBudget = - maxOutputBytes - minimumBytes - currentEdgeBytes() + 4; - const featureBytes = jsonValueByteLength( - pkg.features as string[], - featureBudget, - ); - minimumBytes += featureBytes - 4; - admit(); + maxOutputBytes - minimumBytes - currentEdgeBytes() + 2; + minimumBytes += + jsonValueByteLength(features as string[], featureBudget) - 2; + } + if ("dependencies" in pkg && Array.isArray(pkg.dependencies)) { + const count = pkg.dependencies.length; + edges += count; + minimumBytes += Math.max(0, count * 2 - 1); } - if ( - typeof pkg !== "object" || - pkg === null || - !("dependencies" in pkg) || - !Array.isArray(pkg.dependencies) - ) - continue; - const count = pkg.dependencies.length; - edges += count; - minimumBytes += Math.max(0, count * 2 - 1); admit(); } }; diff --git a/src/rust/RustBinaryProvider.ts b/src/rust/RustBinaryProvider.ts index 11df5765b2..d320cb3d87 100644 --- a/src/rust/RustBinaryProvider.ts +++ b/src/rust/RustBinaryProvider.ts @@ -159,14 +159,12 @@ export class RustBinaryProvider implements RustBinaryPort { ...selected.limitations, ], }; - retainCompilerPathsWithinReport( - report, - limits.output_bytes, - rustcLimitation(image.rustc_commit, image.format), - ); if ( - Buffer.byteLength(JSON.stringify(report), "utf8") > - limits.output_bytes + !fitCompilerPathsWithinReport( + report, + limits.output_bytes, + rustcLimitation(image.rustc_commit, image.format), + ) ) throw outputLimit(input.path, limits.output_bytes); const parsed = rustBinarySchema.safeParse(report); @@ -324,23 +322,19 @@ const rustcLimitation = ( * directly because no compiler record may survive. Byte trimming is reported * only when it removes a path. */ -const retainCompilerPathsWithinReport = ( +const fitCompilerPathsWithinReport = ( report: RustBinary, maxOutputBytes: number, compilerGuidance: readonly string[], -): void => { +): boolean => { const commit = report.rustc_commit; - const withGuidance = [...report.limitations, ...compilerGuidance]; - if ( - commit === null || - Buffer.byteLength( - JSON.stringify({ ...report, limitations: withGuidance }), - ) <= maxOutputBytes - ) { - report.limitations = withGuidance; - return; - } - if (Buffer.byteLength(JSON.stringify(report)) <= maxOutputBytes) return; + const originalLimitations = report.limitations; + report.limitations = [...originalLimitations, ...compilerGuidance]; + if (Buffer.byteLength(JSON.stringify(report)) <= maxOutputBytes) return true; + if (commit === null) return false; + + report.limitations = originalLimitations; + if (Buffer.byteLength(JSON.stringify(report)) <= maxOutputBytes) return true; const limitation = "Compiler paths were omitted to preserve dependency facts within the complete-report byte allowance; compiler-path coverage is partial. Increase producer_limits.output_bytes on the next call; no restart is required. Increasing max_rustc_hashes alone does not raise this allowance."; const baseline = { @@ -378,6 +372,7 @@ const retainCompilerPathsWithinReport = ( } else report.rustc_commit = { ...empty, hashes: commit.hashes.slice(0, count) }; report.limitations = baseline.limitations; + return Buffer.byteLength(JSON.stringify(report)) <= maxOutputBytes; }; const failureForPrimary = (