fix(rust): admit valid reports at the configured byte limit (#13570)

This commit is contained in:
morluto authored and GitHub committed 2026-11-22 14:34:46 -05:00
1 parent c70d1af416
commit 0b1dcb413d
3 files changed
+65 -44

No files matched your search

+32
View File
@@ -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: [
+18 -24
View File
@@ -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();
}
};
+15 -20
View File
@@ -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 = (