diff --git a/src/browser/execution/CdpRuntimeRequests.test.ts b/src/browser/execution/CdpRuntimeRequests.test.ts index 4e055f337e..3f51ed60da 100644 --- a/src/browser/execution/CdpRuntimeRequests.test.ts +++ b/src/browser/execution/CdpRuntimeRequests.test.ts @@ -17,7 +17,7 @@ const sources = { }; const event = ( extraRequest: Record, - extraInitiator: Record = {}, + extraInitiator: Record = {}, ) => ({ method: "Network.requestWillBeSent", params: { @@ -47,6 +47,26 @@ it("budgets retained request facts independently of discarded POST bodies", () = expect(JSON.stringify(retained)).not.toContain("postData"); }); +it("keeps retained initiator facts independent of a returned result", () => { + const requests = new CdpRuntimeRequests(sources); + requests.ingest( + receiveCdpEvent(event({}, { extension: { label: "producer" } })), + ); + const initiator = requests.result()[0]?.reported_initiator; + const extension = initiator?.extension; + if ( + typeof extension !== "object" || + extension === null || + Array.isArray(extension) + ) + throw new Error("Missing producer extension"); + extension.label = "changed by caller"; + expect(requests.result()[0]?.reported_initiator).toEqual({ + type: "parser", + extension: { label: "producer" }, + }); +}); + it("preserves a typed resource refusal for oversized retained initiator facts", () => { const requests = new CdpRuntimeRequests(sources); requests.ingest( diff --git a/src/browser/execution/CdpRuntimeRequests.ts b/src/browser/execution/CdpRuntimeRequests.ts index 0f7af4c247..2765d6bc31 100644 --- a/src/browser/execution/CdpRuntimeRequests.ts +++ b/src/browser/execution/CdpRuntimeRequests.ts @@ -126,7 +126,7 @@ export class CdpRuntimeRequests { const result = this.items.map((request) => ({ ...request, reported_initiator: sanitizeRuntimeInitiator( - jsonObjectSchema.parse(request.reported_initiator), + structuredClone(request.reported_initiator), this.sources, ), callsites: request.callsites.map((site) => diff --git a/src/domain/javascript/javascriptExportShapeVariants.ts b/src/domain/javascript/javascriptExportShapeVariants.ts index e8b0e229ee..6fc22d2a17 100644 --- a/src/domain/javascript/javascriptExportShapeVariants.ts +++ b/src/domain/javascript/javascriptExportShapeVariants.ts @@ -160,7 +160,7 @@ const inventoryFor = ( source_range: shape.source_range, properties: uniqueSorted([ ...shape.fields - .filter((field) => fieldPresence(shape, field, field.path) === "present") + .filter((field) => field.presence === "present") .map(({ path }) => path), ...shape.property_coverage.map(({ path }) => path), ]).filter((path) => path !== ""), @@ -177,10 +177,7 @@ export const hasPartialJavaScriptExportPropertyCoverage = ( shapes.some( (shape) => shape.property_coverage.some(({ status }) => status === "partial") || - shape.fields.some( - (field) => - fieldPresence(shape, field, field.path) === "unknown-coverage", - ), + shape.fields.some((field) => field.presence === "unknown-coverage"), ); interface DiscriminantOccurrence { @@ -194,7 +191,7 @@ const discriminantOccurrences = ( const output = new Map(); shapes.forEach((shape, index) => { for (const field of shape.fields) { - if (fieldPresence(shape, field, field.path) !== "present") continue; + if (field.presence !== "present") continue; if (field.state !== "literal") continue; const discriminant = { path: field.path, value: field.value }; const key = canonicalJson( @@ -305,19 +302,39 @@ const diffPair = ( discriminant: Discriminant, evidenceLinks: [string, string], ): JavaScriptExportShapeComparisonChange[] => { - const leftFields = new Map(left.fields.map((field) => [field.path, field])); - const rightFields = new Map(right.fields.map((field) => [field.path, field])); + const leftFields = new Map(); + for (const field of left.fields) + if (!leftFields.has(field.path)) leftFields.set(field.path, field); + const rightFields = new Map(); + for (const field of right.fields) + if (!rightFields.has(field.path)) rightFields.set(field.path, field); + const leftCoverage = new Map(); + for (const entry of left.property_coverage) + if (!leftCoverage.has(entry.path)) leftCoverage.set(entry.path, entry); + const rightCoverage = new Map(); + for (const entry of right.property_coverage) + if (!rightCoverage.has(entry.path)) rightCoverage.set(entry.path, entry); const paths = uniqueSorted([ ...leftFields.keys(), ...rightFields.keys(), - ...left.property_coverage.map(({ path }) => path), - ...right.property_coverage.map(({ path }) => path), + ...leftCoverage.keys(), + ...rightCoverage.keys(), ]); return paths.flatMap((path) => { - const leftField = fieldAtPath(left, leftFields, path); - const rightField = fieldAtPath(right, rightFields, path); - const leftPresence = fieldPresence(left, leftField, path); - const rightPresence = fieldPresence(right, rightField, path); + const leftField = fieldAtPath(leftFields, leftCoverage, path); + const rightField = fieldAtPath(rightFields, rightCoverage, path); + const leftPresence = fieldPresence( + leftFields, + leftCoverage, + leftField, + path, + ); + const rightPresence = fieldPresence( + rightFields, + rightCoverage, + rightField, + path, + ); const status = fieldChangeStatus({ leftPresence, rightPresence, @@ -351,14 +368,13 @@ const diffPair = ( }; const fieldAtPath = ( - shape: Shape, fields: ReadonlyMap, + coverageByPath: ReadonlyMap, path: string, ): Field | undefined => { const field = fields.get(path); if (field !== undefined) return field; - if (!shape.property_coverage.some((coverage) => coverage.path === path)) - return undefined; + if (!coverageByPath.has(path)) return undefined; return { path, presence: "present", @@ -405,7 +421,8 @@ const fieldChangeStatus = ({ }; const fieldPresence = ( - shape: Shape, + fields: ReadonlyMap, + coverageByPath: ReadonlyMap, field: Field | undefined, path: string, ): PropertyPresence => { @@ -415,11 +432,9 @@ const fieldPresence = ( parent !== null; parent = parentPointer(parent) ) { - const coverage = shape.property_coverage.find( - (entry) => entry.path === parent, - ); + const coverage = coverageByPath.get(parent); if (coverage?.status === "complete") return "absent"; - const ancestor = shape.fields.find((entry) => entry.path === parent); + const ancestor = fields.get(parent); if (ancestor !== undefined) return ancestor.presence === "absent" ? "absent" : "unknown-coverage"; if (coverage !== undefined) return "unknown-coverage"; diff --git a/src/domain/javascript/javascriptSemanticMutationInitializers.ts b/src/domain/javascript/javascriptSemanticMutationInitializers.ts index f03fb3badd..c498ff1867 100644 --- a/src/domain/javascript/javascriptSemanticMutationInitializers.ts +++ b/src/domain/javascript/javascriptSemanticMutationInitializers.ts @@ -2146,6 +2146,7 @@ export const semanticBindingHasPriorValueWrite = ( ): boolean => { if (binding.definitions.some(({ kind }) => kind === "function")) return true; const writes = state.bindingWrites.get(binding.bindingId) ?? []; + const writeSet = new Set(writes); const read = semanticBindingAtRead(binding, site, state); const retained = read.status === "ordered" @@ -2161,7 +2162,7 @@ export const semanticBindingHasPriorValueWrite = ( ...binding.referenceInitializers, ]) { let owner: t.Node | undefined = source.node; - while (owner !== undefined && !writes.includes(owner)) + while (owner !== undefined && !writeSet.has(owner)) owner = state.parentsByNode.get(owner); if (owner !== undefined) { representedWrites.add(owner); diff --git a/src/uefi/UefiLzmaWorker.ts b/src/uefi/UefiLzmaWorker.ts index 0adb9fc581..6fb719da32 100644 --- a/src/uefi/UefiLzmaWorker.ts +++ b/src/uefi/UefiLzmaWorker.ts @@ -201,17 +201,19 @@ const decodeStreamPayload = ( } }; -const serializeReply = (reply: UefiLzmaReply): string => { +const serializeReply = ( + reply: UefiLzmaReply, +): { readonly text: string; readonly ok: boolean } => { const encoded = JSON.stringify(reply); if (Buffer.byteLength(encoded) <= UEFI_FIRMWARE_LIMITS.diagnosticBytes) { - return encoded; + return { text: encoded, ok: reply.ok }; } const bounded: UefiLzmaReply = { ok: false, reason: reply.ok ? "decoder" : reply.reason, message: "LZMA reply exceeds the diagnostic budget.", }; - return JSON.stringify(bounded); + return { text: JSON.stringify(bounded), ok: false }; }; class FramedByteReader { @@ -313,11 +315,10 @@ const runStdioWorker = async (): Promise => { request.format, request.max_output_bytes, ); - const replyText = serializeReply(reply); - const parsedReply = JSON.parse(replyText) as UefiLzmaReply; + const serialized = serializeReply(reply); const body = - parsedReply.ok && decoded !== undefined ? decoded : Buffer.alloc(0); - await writeFramed(process.stdout, encodeLzmaFrame(replyText, body)); + serialized.ok && decoded !== undefined ? decoded : Buffer.alloc(0); + await writeFramed(process.stdout, encodeLzmaFrame(serialized.text, body)); } }; @@ -355,7 +356,7 @@ const runSingleFileWorker = async (requestPath: string): Promise => { message: errorMessage(cause), }; } - await writeFile(request.reply_path, serializeReply(reply), { + await writeFile(request.reply_path, serializeReply(reply).text, { flag: "wx", mode: 0o600, });