diff --git a/src/browser/history/CaptureFormatError.ts b/src/browser/history/CaptureFormatError.ts index c63564e09e..41a49ac052 100644 --- a/src/browser/history/CaptureFormatError.ts +++ b/src/browser/history/CaptureFormatError.ts @@ -1,4 +1,6 @@ import { ParseFailureError } from "../../domain/parseFailure.js"; +import type { ArtifactLimitBoundary } from "../../domain/artifactOperationError.js"; +import type { ArtifactFixedLimit } from "../../resources/index.js"; /** The ParseFailure format of a historical web network capture. */ export const CAPTURE_FORMAT = "web-network-capture"; @@ -61,6 +63,16 @@ export const captureLimitForReply = ( ): CaptureReplyLimitName | undefined => CAPTURE_REPLY_LIMITS.find((name) => CAPTURE_LIMITS[name].reply === reason); +/** + * A fixed ceiling that the reader under a capture decoder named, and the + * resource it bounds. The capture keeps the reader's ceiling instead of + * attributing the refusal to one of its own budgets. + */ +export interface CaptureFixedCeiling { + readonly fixedAllowance: ArtifactFixedLimit; + readonly boundary: ArtifactLimitBoundary; +} + /** Expected malformed, unsupported or oversized producer data at a historical decoder boundary. */ export class CaptureFormatError extends ParseFailureError< "malformed" | "unsupported" | "limit" @@ -72,7 +84,9 @@ export class CaptureFormatError extends ParseFailureError< cause?: unknown, ); constructor( - limit: { readonly name: CaptureLimitName; readonly maximum: number }, + limit: + | { readonly name: CaptureLimitName; readonly maximum: number } + | CaptureFixedCeiling, message: string, pointer?: string, ); @@ -80,7 +94,8 @@ export class CaptureFormatError extends ParseFailureError< kind: | "malformed" | "unsupported" - | { readonly name: CaptureLimitName; readonly maximum: number }, + | { readonly name: CaptureLimitName; readonly maximum: number } + | CaptureFixedCeiling, message: string, pointer = "", cause?: unknown, @@ -101,24 +116,35 @@ export class CaptureFormatError extends ParseFailureError< kind: "limit", format: CAPTURE_FORMAT, detail: message, - limit: { name: kind.name, maximum: kind.maximum }, + limit: + "fixedAllowance" in kind + ? { + name: kind.fixedAllowance.name, + maximum: kind.fixedAllowance.limit, + } + : { name: kind.name, maximum: kind.maximum }, ...field, }, cause === undefined ? undefined : { cause }, ); this.name = "CaptureFormatError"; + this.ceiling = + typeof kind === "object" && "fixedAllowance" in kind ? kind : undefined; } + /** The reader-named fixed ceiling, for a limit that is not a capture budget. */ + readonly ceiling: CaptureFixedCeiling | undefined; + /** JSON pointer of the failing capture value; empty for the whole input. */ get pointer(): string { return this.failure.field ?? ""; } - /** The exhausted budget, for a `limit` failure. */ + /** The exhausted capture budget, for a `limit` failure that is not a fixed ceiling. */ get limit(): | { readonly name: CaptureLimitName; readonly maximum: number } | undefined { - return this.failure.kind === "limit" + return this.failure.kind === "limit" && this.ceiling === undefined ? { name: this.failure.limit.name as CaptureLimitName, maximum: this.failure.limit.maximum, @@ -132,7 +158,9 @@ export class CaptureFormatError extends ParseFailureError< | "unsupported" | "decoder" | "input-limit" - | "expanded-limit" { + | "expanded-limit" + | "fixed-limit" { + if (this.ceiling !== undefined) return "fixed-limit"; const { limit } = this; if (limit === undefined) return this.failure.kind === "unsupported" ? "unsupported" : "format"; diff --git a/src/browser/history/HistoricalCaptureDecoder.ts b/src/browser/history/HistoricalCaptureDecoder.ts index 5ac2159bf3..c957eccd7e 100644 --- a/src/browser/history/HistoricalCaptureDecoder.ts +++ b/src/browser/history/HistoricalCaptureDecoder.ts @@ -46,6 +46,7 @@ import { AnalysisOutputError, AnalysisResourceConstraintError, cancelledBy, + fixedCeilingConstraint, } from "../../domain/analysisErrorCore.js"; import { ProviderAdapterError } from "../../domain/providerAdapterError.js"; import { ProviderCleanupError } from "../../domain/providerCleanupError.js"; @@ -256,6 +257,14 @@ export class HistoricalCaptureDecoder { message: reply.message, }, ]); + // No capture budget raises a ceiling the decoder's reader named. + if (reply.reason === "fixed-limit") + throw fixedCeilingConstraint( + OPERATION, + reply.boundary, + reply.message, + reply.ceiling, + ); const limit = captureLimitForReply(reply.reason); if (limit !== undefined) { const { parameter, expected } = CAPTURE_LIMITS[limit]; diff --git a/src/browser/history/HistoricalCaptureReply.ts b/src/browser/history/HistoricalCaptureReply.ts index 918ca8f807..411d0ba431 100644 --- a/src/browser/history/HistoricalCaptureReply.ts +++ b/src/browser/history/HistoricalCaptureReply.ts @@ -19,26 +19,44 @@ import { import { projectAnalysisError } from "../../domain/analysisErrorProjection.js"; import { withCaptureReaderCleanup } from "./CaptureFailures.js"; import { webNetworkCapturePayloadSchema } from "../../domain/webNetworkCapture.js"; +import { FIXED_ALLOWANCE_KINDS } from "../../domain/fixedAllowanceRemediation.js"; + +const failureFields = { message: z.string(), pointer: z.string() }; const replySchema = z.discriminatedUnion("ok", [ z.strictObject({ ok: z.literal(true), value: webNetworkCapturePayloadSchema, }), - z.strictObject({ - ok: z.literal(false), - reason: z.enum([ - "format", - "unsupported", - "input-limit", - "expanded-limit", - "resource-limit", - "decoder", - "limit", - ]), - message: z.string(), - pointer: z.string(), - }), + z.discriminatedUnion("reason", [ + z.strictObject({ + ok: z.literal(false), + reason: z.enum([ + "format", + "unsupported", + "input-limit", + "expanded-limit", + "resource-limit", + "decoder", + "limit", + ]), + ...failureFields, + }), + // A fixed ceiling the decoder's reader named, carried with its value. + z.strictObject({ + ok: z.literal(false), + reason: z.literal("fixed-limit"), + ...failureFields, + ceiling: z.strictObject({ + name: z.string().min(1), + kind: z.enum(FIXED_ALLOWANCE_KINDS), + limit: z.number().nonnegative(), + unit: z.string().min(1), + narrowing: z.string().min(1).exactOptional(), + }), + boundary: z.enum(["file-size", "capacity", "memory"]), + }), + ]), ]); /** Decode a bounded reply while keeping its completed read and descriptor finalization distinct. */ diff --git a/src/browser/history/NodeCaptureProcess.ts b/src/browser/history/NodeCaptureProcess.ts index c5c8f9710b..cb3425f61a 100644 --- a/src/browser/history/NodeCaptureProcess.ts +++ b/src/browser/history/NodeCaptureProcess.ts @@ -52,6 +52,12 @@ const main = async (): Promise => { reason: cause.replyReason, message: cause.message, pointer: cause.pointer, + ...(cause.ceiling === undefined + ? {} + : { + ceiling: cause.ceiling.fixedAllowance, + boundary: cause.ceiling.boundary, + }), } : { ok: false, diff --git a/src/browser/history/SazCapture.ts b/src/browser/history/SazCapture.ts index d86abf73d4..9cae5c2899 100644 --- a/src/browser/history/SazCapture.ts +++ b/src/browser/history/SazCapture.ts @@ -191,11 +191,22 @@ export const decodeSazCapture = async ( }; } catch (cause: unknown) { if (cause instanceof CaptureFormatError) throw cause; - if (cause instanceof ArtifactReaderFailure && cause.reason === "limit") + if (cause instanceof ArtifactReaderFailure && cause.reason === "limit") { + // A ceiling the ZIP reader names stays its own; only the metadata read + // budget this decoder gave the reader is the expanded-input budget. + if (cause.fixedAllowance !== undefined) + throw new CaptureFormatError( + { + fixedAllowance: cause.fixedAllowance, + boundary: cause.boundary ?? "memory", + }, + cause.message, + ); throw new CaptureFormatError( { name: "expanded-input", maximum: captureBudgets().inputBytes }, "SAZ archive exceeds the ZIP reader's resource budget.", ); + } throw new CaptureFormatError( "malformed", "SAZ archive or session data is malformed or fails ZIP integrity validation.", diff --git a/src/browser/history/playwrightTrace/TraceFailure.ts b/src/browser/history/playwrightTrace/TraceFailure.ts index 521f16e24d..e5f7d35e03 100644 --- a/src/browser/history/playwrightTrace/TraceFailure.ts +++ b/src/browser/history/playwrightTrace/TraceFailure.ts @@ -5,6 +5,7 @@ import { AnalysisResourceConstraintError, cancelledBy, carriedReason, + fixedCeilingConstraint, } from "../../../domain/analysisErrorCore.js"; import { ArtifactOperationError } from "../../../domain/artifactOperationError.js"; import { PLAYWRIGHT_TRACE_OPERATION as operation } from "../../../domain/playwrightTrace/playwrightTrace.js"; @@ -28,19 +29,25 @@ export const traceFailure = (cause: unknown, path: string): AnalysisError => { if (cause.reason === "integrity") return readerIntegrityFailure(operation, path, cause); if (cause.reason === "limit") - return new AnalysisResourceConstraintError( - operation, - cause.boundary ?? "memory", - cause.message, - null, - cause.fixedAllowance === undefined - ? { + return cause.fixedAllowance === undefined + ? new AnalysisResourceConstraintError( + operation, + cause.boundary ?? "memory", + cause.message, + null, + { cause, remediationAction: "Select a smaller complete recorder chunk. Reply selection does not increase archive parsing allowances.", - } - : { cause, retryable: false, fixedAllowance: cause.fixedAllowance }, - ); + }, + ) + : fixedCeilingConstraint( + operation, + cause.boundary ?? "memory", + cause.message, + cause.fixedAllowance, + { cause }, + ); if (cause.reason === "format" || cause.reason === "path") return stableArtifactInputFailure(cause, operation); return new ArtifactOperationError( diff --git a/src/domain/analysisErrorCore.ts b/src/domain/analysisErrorCore.ts index 4071e6dd01..2e11b798ba 100644 --- a/src/domain/analysisErrorCore.ts +++ b/src/domain/analysisErrorCore.ts @@ -142,6 +142,24 @@ export const reattributedResourceConstraint = ( }, ); +/** + * Refuse at a fixed ceiling that its producer named. The ceiling carries its + * own value and recovery, so no other limit is reported beside it, and the + * unchanged request is not retryable. + */ +export const fixedCeilingConstraint = ( + operation: string, + resource: (typeof ANALYSIS_RESOURCE_KINDS)[number], + reason: string, + ceiling: FixedAllowance, + options?: AnalysisErrorOptions, +): AnalysisResourceConstraintError => + new AnalysisResourceConstraintError(operation, resource, reason, null, { + ...options, + retryable: false, + fixedAllowance: ceiling, + }); + /** Carry one refusal's recovery to an error re-attributed or redacted elsewhere. */ export const resourceConstraintRecovery = ( error: AnalysisResourceConstraintError, diff --git a/src/domain/fixedAllowanceRemediation.ts b/src/domain/fixedAllowanceRemediation.ts index aafd0fdefe..3666886c6a 100644 --- a/src/domain/fixedAllowanceRemediation.ts +++ b/src/domain/fixedAllowanceRemediation.ts @@ -2,15 +2,17 @@ * Recovery for a deterministic allowance. Host memory, worker limits, waiting, * and a different output channel do not raise it. */ -export type FixedAllowanceKind = - | "parser" - | "count" - | "bytes" - | "report" - | "input" - | "capture" - | "live" - | "work"; +export const FIXED_ALLOWANCE_KINDS = [ + "parser", + "count", + "bytes", + "report", + "input", + "capture", + "live", + "work", +] as const; +export type FixedAllowanceKind = (typeof FIXED_ALLOWANCE_KINDS)[number]; const unchangedRequest = "Repeating the unchanged request fails the same way."; diff --git a/src/native/elf/ElfNoteParser.ts b/src/native/elf/ElfNoteParser.ts index ebf3096af3..1918d91f4d 100644 --- a/src/native/elf/ElfNoteParser.ts +++ b/src/native/elf/ElfNoteParser.ts @@ -1,4 +1,3 @@ -import type { FixedAllowance } from "../../domain/fixedAllowanceRemediation.js"; import { observeElfStringTableEntry, elfStringTableText, @@ -10,6 +9,7 @@ import { readElfRawTableDeclarations } from "../../domain/elfRawTableDeclaration import { ArtifactReaderFailure, assertArtifactReadActive, + type ArtifactFixedLimit, } from "../../resources/index.js"; import { readElfFixedHeader, @@ -48,7 +48,7 @@ const isLinuxCoreProfile = (header: ElfFixedHeader): boolean => * The note text, hash, and scan ceiling. It equals the admitted image length * plus decoded section names, so host memory cannot raise it. */ -const noteTextAllowance = (budget: TextBudget): FixedAllowance => ({ +const noteTextAllowance = (budget: TextBudget): ArtifactFixedLimit => ({ name: "The ELF note text, hash, and scan allowance (derived from the image length and section names)", kind: "parser", limit: budget.limit, diff --git a/src/resources/ArtifactReader.ts b/src/resources/ArtifactReader.ts index 20d3127166..b1bdc0aa9b 100644 --- a/src/resources/ArtifactReader.ts +++ b/src/resources/ArtifactReader.ts @@ -1,7 +1,4 @@ -import type { - FixedAllowance, - FixedAllowanceKind, -} from "../domain/fixedAllowanceRemediation.js"; +import type { FixedAllowance } from "../domain/fixedAllowanceRemediation.js"; import type { ArtifactReaderLimitFact } from "../domain/artifactReaderLimits.js"; import type { ArtifactAdmissionLimit, @@ -45,7 +42,7 @@ export interface ArtifactReaderFailureOptions extends ErrorOptions { readonly admission?: ArtifactAdmissionLimit; readonly readerLimit?: ArtifactReaderLimitFact; /** For a `limit` failure, the release-fixed allowance that refused the input. */ - readonly fixedAllowance?: FixedAllowance; + readonly fixedAllowance?: ArtifactFixedLimit; /** For an `integrity` failure, the check that failed, when the reader names it. */ readonly integrityCheck?: ArtifactIntegrityCheck | undefined; } @@ -295,7 +292,7 @@ export class ArtifactReaderFailure extends Error { /** Named admission for a limit whose public recovery is owned by the producer. */ readonly admission: ArtifactAdmissionLimit | undefined; readonly readerLimit: ArtifactReaderLimitFact | undefined; - readonly fixedAllowance: FixedAllowance | undefined; + readonly fixedAllowance: ArtifactFixedLimit | undefined; readonly integrityCheck: ArtifactIntegrityCheck | undefined; /** An `integrity` failure names the check that failed. */ @@ -455,14 +452,14 @@ export class ArtifactReaderFailure extends Error { } } -/** A release-fixed reader ceiling and its value. */ -export interface ArtifactFixedLimit { - /** Display name, such as "The wxapkg member table allowance". */ - readonly name: string; - readonly kind: FixedAllowanceKind; +/** + * A release-fixed reader ceiling. The reader states its value, so a consumer + * that carries the ceiling to another process or projection keeps it. + */ +export type ArtifactFixedLimit = FixedAllowance & { readonly limit: number; readonly unit: string; -} +}; /** * Refuse input above a release-fixed reader ceiling. `outcome` states what diff --git a/src/resources/ZipArtifactReader.ts b/src/resources/ZipArtifactReader.ts index 33f12de7f9..1891139004 100644 --- a/src/resources/ZipArtifactReader.ts +++ b/src/resources/ZipArtifactReader.ts @@ -5,6 +5,7 @@ import { copyArtifactEntry, sameArtifactEntry, type ArtifactEntry, + type ArtifactFixedLimit, type ArtifactReader, type ArtifactReaderTraversal, type ArtifactTraversalOptions, @@ -55,7 +56,6 @@ import { readFileHandleRange } from "../filesystem/readFileHandleRange.js"; import { strictUtf8Decoder } from "../domain/strictUtf8.js"; import { decodeZipCp437 } from "../domain/zip/zipEntryText.js"; import { invariantViolation } from "../domain/internalInvariant.js"; -import type { FixedAllowance } from "../domain/fixedAllowanceRemediation.js"; const ZIP_UTF8_DECODER = strictUtf8Decoder("preserve"); @@ -68,7 +68,7 @@ const ZIP_ZSTANDARD_WINDOW_ALLOWANCE = `${String(2 ** (ZIP_ZSTANDARD_WINDOW_LOG_ const ZIP_ZSTANDARD_WINDOW_LIMIT = `ZIP Zstandard member requires a decoder window above REA's fixed ${ZIP_ZSTANDARD_WINDOW_ALLOWANCE} allowance; the archive is not necessarily corrupt.`; /** The fixed method-93 decoder window ceiling, with the request change that fits it. */ -export const ZIP_ZSTANDARD_WINDOW_CEILING: FixedAllowance = { +export const ZIP_ZSTANDARD_WINDOW_CEILING: ArtifactFixedLimit = { name: "The ZIP Zstandard decoder window allowance", kind: "parser", limit: 2 ** ZIP_ZSTANDARD_WINDOW_LOG_MAX, diff --git a/src/resources/index.ts b/src/resources/index.ts index de8b49b52b..fba423f0e0 100644 --- a/src/resources/index.ts +++ b/src/resources/index.ts @@ -56,6 +56,7 @@ export { export { artifactReaderLimitFailure } from "./ArtifactReaderLimits.js"; export type { ArtifactEntry, + ArtifactFixedLimit, ArtifactReader, ArtifactReaderFailureOptions, ArtifactReaderTraversal, diff --git a/tests/boundary/browser/sazWebCapture.test.ts b/tests/boundary/browser/sazWebCapture.test.ts index 62b09b5584..9f7a65abb0 100644 --- a/tests/boundary/browser/sazWebCapture.test.ts +++ b/tests/boundary/browser/sazWebCapture.test.ts @@ -26,6 +26,12 @@ import { createMcpSchemaValidation } from "../../fixtures/mcpSchemaValidation.js import { parseMcpToolError } from "../../fixtures/mcpToolError.js"; import { protobufFrame } from "../../fixtures/protobufCapture.js"; import { createTestTempDirectory } from "../../fixtures/temporaryDirectory.js"; +import { + zstandardWindowFrameFixture, + zstandardZipFixture, +} from "../../fixtures/zipZstandard.js"; +import { analysisRefusalRemediation } from "../../../src/domain/analysisRefusal.js"; +import { ZIP_ZSTANDARD_WINDOW_CEILING } from "../../../src/resources/ZipArtifactReader.js"; import { CAPTURE_DECODER_HAS_MEMORY_BOUND as hasMemoryBound, @@ -951,6 +957,51 @@ it.each([ expect(JSON.stringify(result)).not.toContain("malformed-private-input"); }); +it("names the ZIP decoder window instead of the expanded-input budget for a large-window member", async () => { + // Window descriptor 0x90 declares 256 MiB, above the ZIP reader's window. + const { content, compressed } = zstandardWindowFrameFixture(0x90); + const path = await fixture( + zstandardZipFixture(content, { compressed, name: "raw/1_c.txt" }), + ); + const result = await cliResponse(path); + expect(result.ok, result.stdout).toBe(false); + const error = JSON.parse(result.stdout); + expect(error).toMatchObject({ + code: "resource_constraint", + details: { + refusal: { + kind: "fixed_ceiling", + operation: "inspect_web_network_capture", + allowance: ZIP_ZSTANDARD_WINDOW_CEILING.name, + allowance_kind: ZIP_ZSTANDARD_WINDOW_CEILING.kind, + limit: 2 ** 27, + unit: "bytes", + narrowing: ZIP_ZSTANDARD_WINDOW_CEILING.narrowing, + }, + }, + // The recovery is the reader's ceiling through the refusal formatter. + remediation: { + action: analysisRefusalRemediation({ + kind: "fixed_ceiling", + operation: "inspect_web_network_capture", + ceiling: ZIP_ZSTANDARD_WINDOW_CEILING, + }), + }, + }); + expect(JSON.stringify(error)).not.toContain("max_input_bytes"); + const { call } = await connectSazMcp(); + const response = await call("inspect_web_network_capture", { + capture_path: path, + format: "saz", + }); + expect(response.isError).toBe(true); + expect(parseMcpToolError(response).error).toMatchObject({ + code: error.code, + details: { refusal: error.details.refusal }, + remediation: error.remediation, + }); +}); + runtimeTest.runIf(!hasMemoryBound && runtimeSupported)( "reports SAZ unavailable when its runtime cannot enforce decoder memory", async () => {