From b41399d8a61bb86586a2bfbd1b6fb11799ae325e Mon Sep 17 00:00:00 2001 From: morluto <76467478+morluto@users.noreply.github.com> Date: Wed, 25 Nov 2026 23:19:09 -0500 Subject: [PATCH] fix(artifacts): omit required_value where reader limits know only a lower bound (#14646) * fix(artifacts): omit required_value where reader limits know only a lower bound A reader-limit refusal reports required_value only when a retry at that value is admitted. Deno specifier text and remote body totals, VFS entry and name-unit counts, Deno and wxapkg plaintext totals, and the shared path registry's part and node counts stop at the first crossing, so those refusals now omit it. Declared lengths and table counts keep it. * test(resources): assert path budget refusals omit required_value Capture the path_nodes and path_parts refusals and check readerLimit has no required_value, so restoring the lower-bound value fails. --- src/artifacts/DenoStandaloneArtifactReader.ts | 2 +- src/artifacts/WxapkgArtifactReader.ts | 3 +- src/artifacts/deno/DenoSectionLayout.ts | 10 +++-- src/artifacts/deno/DenoVirtualFs.test.ts | 5 ++- src/artifacts/deno/DenoVirtualFs.ts | 12 +++--- src/resources/ArtifactPaths.test.ts | 43 +++++++++++++++---- src/resources/ArtifactPaths.ts | 4 +- 7 files changed, 55 insertions(+), 24 deletions(-) diff --git a/src/artifacts/DenoStandaloneArtifactReader.ts b/src/artifacts/DenoStandaloneArtifactReader.ts index 41e25cf140e..c87e0661396 100644 --- a/src/artifacts/DenoStandaloneArtifactReader.ts +++ b/src/artifacts/DenoStandaloneArtifactReader.ts @@ -156,7 +156,7 @@ export class DenoStandaloneArtifactReader extends PositionalArtifactReader< "artifact_limits.expanded_bytes", Number(expansionBudget), "bytes", - declaredPlaintext, + // Entries after this one are not counted: a lower bound only. ); } paths.reservePath(entry.path); diff --git a/src/artifacts/WxapkgArtifactReader.ts b/src/artifacts/WxapkgArtifactReader.ts index 69370ed0835..32f75d4a848 100644 --- a/src/artifacts/WxapkgArtifactReader.ts +++ b/src/artifacts/WxapkgArtifactReader.ts @@ -205,7 +205,8 @@ export class WxapkgArtifactReader implements ArtifactReader { "artifact_limits.expanded_bytes", budget, "bytes", - declaredBytes, + // Later members are not counted, so the total is a lower bound. + undefined, "memory", ); const adapterKey = `wxapkg-entry:${String(index)}`; diff --git a/src/artifacts/deno/DenoSectionLayout.ts b/src/artifacts/deno/DenoSectionLayout.ts index 7af532434ea..70819feb4fd 100644 --- a/src/artifacts/deno/DenoSectionLayout.ts +++ b/src/artifacts/deno/DenoSectionLayout.ts @@ -215,7 +215,7 @@ export const parseDenoSection = async ( const range = takeRange(BigInt(length), "specifier text"); textBytes += length; if (textBytes > limits.specifier_text_bytes) - throw denoByteLimit("specifier_text_bytes", textBytes, limits); + throw denoByteLimit("specifier_text_bytes", undefined, limits); const bytes = await readExact(range.sectionOffset, range.length); const id = await u32(); if (specifierText.has(id)) @@ -377,7 +377,7 @@ const addRemoteBody = ( ): bigint => { const next = total + BigInt(length); if (next > BigInt(limits.remote_body_bytes)) - throw denoByteLimit("remote_body_bytes", next, limits); + throw denoByteLimit("remote_body_bytes", undefined, limits); return next; }; @@ -540,7 +540,11 @@ const denoByteLimit = ( | "vfs_header_json_bytes" | "specifier_text_bytes" | "remote_body_bytes", - claimed: number | bigint, + /** + * A declared length is the complete need. A running total stops at the + * first crossing, a lower bound, and is passed as undefined. + */ + claimed: number | bigint | undefined, limits: DenoReaderLimits, ): ArtifactReaderFailure => artifactReaderLimitFailure( diff --git a/src/artifacts/deno/DenoVirtualFs.test.ts b/src/artifacts/deno/DenoVirtualFs.test.ts index b43998b36c8..f584a77a478 100644 --- a/src/artifacts/deno/DenoVirtualFs.test.ts +++ b/src/artifacts/deno/DenoVirtualFs.test.ts @@ -182,12 +182,15 @@ it("refuses an entry count above the selected allowance", () => { readerLimit: { parameter: "artifact_limits.deno.vfs_entries", effective_value: 2, - required_value: 3, can_increase: true, }, }); } expect(failure).toBeInstanceOf(ArtifactReaderFailure); + // The walk stops at the first entry past the allowance; the total is unknown. + expect((failure as ArtifactReaderFailure).readerLimit).not.toHaveProperty( + "required_value", + ); expect( walkDenoVirtualFs(entries, 0, undefined, { ...limits, diff --git a/src/artifacts/deno/DenoVirtualFs.ts b/src/artifacts/deno/DenoVirtualFs.ts index 93b367966a0..448e7b0dfdd 100644 --- a/src/artifacts/deno/DenoVirtualFs.ts +++ b/src/artifacts/deno/DenoVirtualFs.ts @@ -212,7 +212,7 @@ const walkEntries = ( if (tagged === undefined) throw new UnsupportedVfsShape(); const index = state.count; if (index >= state.limits.vfs_entries) - throw denoVfsEntryLimit(state.limits, index + 1); + throw denoVfsEntryLimit(state.limits); state.count += 1; const name = tagged.body.n; if (typeof name !== "string") throw new UnsupportedVfsShape(); @@ -456,7 +456,8 @@ const retainText = (state: WalkState, text: string | null): string | null => { "artifact_limits.deno.vfs_name_units", state.limits.vfs_name_units, "count", - next, + // The walk stops here, so the units counted so far are only a lower bound. + undefined, "memory", ); state.retainedUnits = next; @@ -513,15 +514,12 @@ const asRecord = (value: unknown): Record | undefined => { return Object.fromEntries(Object.entries(value)); }; -const denoVfsEntryLimit = ( - limits: DenoReaderLimits, - count: number, -): ArtifactReaderFailure => +/** The walk stops at the first entry past the allowance; the total is unknown. */ +const denoVfsEntryLimit = (limits: DenoReaderLimits): ArtifactReaderFailure => artifactReaderLimitFailure( "artifact_limits.deno.vfs_entries", limits.vfs_entries, "count", - count, ); const denoVfsDepthLimit = (depth: number): ArtifactReaderFailure => diff --git a/src/resources/ArtifactPaths.test.ts b/src/resources/ArtifactPaths.test.ts index f9bec1b125e..e6360c6c529 100644 --- a/src/resources/ArtifactPaths.test.ts +++ b/src/resources/ArtifactPaths.test.ts @@ -1,4 +1,5 @@ import { describe, expect, it } from "vitest"; +import { ArtifactReaderFailure } from "./ArtifactReader.js"; import { ArtifactPathRegistry, @@ -191,6 +192,32 @@ it.each([["app.js", "App.js"], ["other.js"]])( }, ); +/** The reader failure a registry call throws. */ +const pathFailure = (run: () => void): ArtifactReaderFailure => { + try { + run(); + } catch (cause) { + if (cause instanceof ArtifactReaderFailure) return cause; + throw cause; + } + throw new Error("Expected a path budget refusal"); +}; + +it("refuses a deeper path at path_parts without claiming a required value", () => { + const registry = new ArtifactPathRegistry({ + group: "deno", + maxParts: 2, + maxNodes: 100, + }); + // A later path may be deeper still, so this one's depth is a lower bound. + const failure = pathFailure(() => registry.reservePath("a/b/c")); + expect(failure.readerLimit).toMatchObject({ + parameter: "artifact_limits.deno.path_parts", + effective_value: 2, + }); + expect(failure.readerLimit).not.toHaveProperty("required_value"); +}); + it("reserves prefix capacity atomically without declaring destination kinds", () => { const registry = new ArtifactPathRegistry({ group: "deno", @@ -198,15 +225,13 @@ it("reserves prefix capacity atomically without declaring destination kinds", () maxNodes: 3, }); registry.reservePath("a/b"); - expect(() => registry.reservePath("a/c/d")).toThrow( - expect.objectContaining({ - readerLimit: expect.objectContaining({ - parameter: "artifact_limits.deno.path_nodes", - effective_value: 3, - required_value: 4, - }), - }), - ); + // Unvisited paths may add nodes, so no required value is claimed. + const failure = pathFailure(() => registry.reservePath("a/c/d")); + expect(failure.readerLimit).toMatchObject({ + parameter: "artifact_limits.deno.path_nodes", + effective_value: 3, + }); + expect(failure.readerLimit).not.toHaveProperty("required_value"); expect(() => registry.reservePath("z")).not.toThrow(); expect(() => registry.reservePath("a/b")).not.toThrow(); registry.add("a/b", "file"); diff --git a/src/resources/ArtifactPaths.ts b/src/resources/ArtifactPaths.ts index 34195a23633..7e985820f30 100644 --- a/src/resources/ArtifactPaths.ts +++ b/src/resources/ArtifactPaths.ts @@ -143,7 +143,7 @@ export class ArtifactPathRegistry { `artifact_limits.${budget.group}.path_parts`, budget.maxParts, "count", - parts.length, + // A later, deeper path may need more; this path is a lower bound. ); if (budget !== undefined) { let observed = this.#root; @@ -155,7 +155,7 @@ export class ArtifactPathRegistry { `artifact_limits.${budget.group}.path_nodes`, budget.maxNodes, "count", - this.#nodeCount + parts.length - index, + // Paths not yet visited add nodes; the count is a lower bound. ); break; }