fix: bind identity and coverage to the producing rule (#1513)
* fix: bind identity and coverage to the producing rule Logical paths, container families, examined bytes, path-segment conventions, destination collisions, file-id leases, Mach-O CPU selection, and firmware cleanup now follow the observation that owns each rule. * fix: fail unmatched Mach-O slices and keep firmware roots independent A fat target whose architecture lipo did not list now stops before slice tools run. Chained fixups still count unread LC_DYLD_INFO rebase bytes, and a firmware cleanup failure no longer blocks the next workspace. * test: cover path identity and runtime families at the projection boundary NFC joins and segment conventions are now asserted by the path resolver, the Apple projection, and a real APK inventory instead of helper unit tests. * test: expect unread dyld rebase bytes in the Apple dispatch fixture The legacy LC_DYLD_INFO fixture keeps a rebase range this reader does not decode, so the public CLI and MCP check now requires that partial coverage beside the bind opcodes. --------- Co-authored-by: Cursor Agent <cursoragent@cursor.com>
This commit is contained in:
@@ -56,4 +56,39 @@ describe("CDP execution-context retention", () => {
|
||||
});
|
||||
expect(electronFrames.size).toBe(0);
|
||||
});
|
||||
|
||||
it("keeps a script on the frame stored when it was parsed", () => {
|
||||
const request = inspectWebPageInputSchema.parse({
|
||||
cdp_endpoint: "http://127.0.0.1:9222",
|
||||
allowed_origins: ["https://app.example.test"],
|
||||
target_id: "page-1",
|
||||
});
|
||||
const browser = new CdpCaptureEvents(
|
||||
request,
|
||||
new Set(["https://app.example.test"]),
|
||||
);
|
||||
browser.beginAuthorizedFrame("frame-old");
|
||||
browser.beginAuthorizedFrame("frame-new");
|
||||
browser.ingest({
|
||||
method: "Runtime.executionContextCreated",
|
||||
params: { context: { id: 7, auxData: { frameId: "frame-old" } } },
|
||||
});
|
||||
browser.ingest({
|
||||
method: "Debugger.scriptParsed",
|
||||
params: {
|
||||
scriptId: "script-1",
|
||||
url: "https://app.example.test/app.js",
|
||||
executionContextId: 7,
|
||||
},
|
||||
});
|
||||
browser.ingest({
|
||||
method: "Runtime.executionContextCreated",
|
||||
params: { context: { id: 7, auxData: { frameId: "frame-new" } } },
|
||||
});
|
||||
const script = browser.scripts.get("script-1");
|
||||
if (script === undefined) throw new TypeError("Expected captured script");
|
||||
expect(
|
||||
browser.frameForScript(script, new Set(["frame-old", "frame-new"])),
|
||||
).toBe("frame-old");
|
||||
});
|
||||
});
|
||||
|
||||
@@ -198,6 +198,23 @@ describe("empty form destinations with a document base URL", () => {
|
||||
},
|
||||
);
|
||||
|
||||
it("keeps the first value when an attribute name repeats", () => {
|
||||
const value = snapshot();
|
||||
const later = `${origin}/later`;
|
||||
value.strings.push(later);
|
||||
const href = value.strings.indexOf("href");
|
||||
const guide = value.strings.indexOf("guide");
|
||||
const nodes = value.documents[0]?.nodes;
|
||||
if (nodes === undefined) throw new TypeError("Expected a DOM snapshot");
|
||||
nodes.attributes[2] = [href, guide, href, value.strings.length - 1];
|
||||
const result = capture(value);
|
||||
expect(result.nodes[2]?.attribute_names).toEqual(["href", "href"]);
|
||||
expect(result.urls.map(({ url }) => url)).toContain(
|
||||
`${origin}/screens/guide`,
|
||||
);
|
||||
expect(result.urls.map(({ url }) => url)).not.toContain(later);
|
||||
});
|
||||
|
||||
it("does not apply form semantics to an action attribute on another element", () => {
|
||||
const baseUrl = `${origin}/assets/`;
|
||||
const value = snapshot(baseUrl);
|
||||
|
||||
@@ -387,7 +387,8 @@ const domMetadata = (
|
||||
for (let index = 0; index + 1 < attributes.length; index += 2) {
|
||||
const name = indexedString(strings, attributes[index]).toLowerCase();
|
||||
const value = indexedString(strings, attributes[index + 1]);
|
||||
pairs.set(name, value);
|
||||
// HTML keeps the first declaration. A later repeat must not move dom_urls.
|
||||
if (!pairs.has(name)) pairs.set(name, value);
|
||||
}
|
||||
const urls: WebPageInspection["metadata"]["dom_urls"] = [];
|
||||
for (const attribute of domUrlAttributes) {
|
||||
|
||||
@@ -47,6 +47,7 @@ export const handleExecutionContextCreated = (
|
||||
)
|
||||
return;
|
||||
const key = String(identifier);
|
||||
// New contexts may reuse an id. Scripts already parsed keep the frame stored on them.
|
||||
state.executionContextFrames.set(key, frameId);
|
||||
};
|
||||
|
||||
@@ -80,6 +81,7 @@ export const handleScriptParsed = (
|
||||
state.completeness.exclude("scripts", exclusionReasonForUrl(rawUrl));
|
||||
return;
|
||||
}
|
||||
const executionContext = executionContextKey(params.executionContextId);
|
||||
const baseScript = {
|
||||
scriptId,
|
||||
rawUrl,
|
||||
@@ -87,7 +89,11 @@ export const handleScriptParsed = (
|
||||
origin: sanitized.origin,
|
||||
...scriptMetadataValues(params),
|
||||
language: cdpStringValue(params.scriptLanguage) ?? null,
|
||||
executionContextKey: executionContextKey(params.executionContextId),
|
||||
executionContextKey: executionContext,
|
||||
frameId:
|
||||
executionContext === null
|
||||
? null
|
||||
: (state.executionContextFrames.get(executionContext) ?? null),
|
||||
};
|
||||
const baseBytes = scriptMetadataStringBytes(baseScript);
|
||||
const sourceMapDeclared =
|
||||
@@ -131,6 +137,7 @@ const scriptMetadataStringBytes = (script: {
|
||||
readonly hash: string | null;
|
||||
readonly language: string | null;
|
||||
readonly executionContextKey: string | null;
|
||||
readonly frameId: string | null;
|
||||
}): number =>
|
||||
stringByteLength(script.scriptId) +
|
||||
stringByteLength(script.rawUrl) +
|
||||
@@ -138,7 +145,8 @@ const scriptMetadataStringBytes = (script: {
|
||||
stringByteLength(script.origin) +
|
||||
stringByteLength(script.hash) +
|
||||
stringByteLength(script.language) +
|
||||
stringByteLength(script.executionContextKey);
|
||||
stringByteLength(script.executionContextKey) +
|
||||
stringByteLength(script.frameId);
|
||||
|
||||
const stringByteLength = (value: string | null): number =>
|
||||
value === null ? 0 : Buffer.byteLength(value, "utf8");
|
||||
|
||||
@@ -12,6 +12,8 @@ export interface CapturedScript {
|
||||
readonly sourceMapUrl: string | null;
|
||||
readonly sourceMapRawUrl: string | null;
|
||||
readonly executionContextKey: string | null;
|
||||
/** Frame observed when this script was parsed. Later context reuse does not move it. */
|
||||
readonly frameId: string | null;
|
||||
}
|
||||
|
||||
export type NetworkState = WebPageInspection["network"]["requests"][number];
|
||||
|
||||
@@ -159,10 +159,8 @@ export class CdpCaptureEvents implements CdpCaptureEventsState {
|
||||
script: CapturedScript,
|
||||
allowedFrameIds: ReadonlySet<string>,
|
||||
): string | null {
|
||||
if (script.executionContextKey === null) return null;
|
||||
const frameId = this.executionContextFrames.get(script.executionContextKey);
|
||||
return frameId !== undefined && allowedFrameIds.has(frameId)
|
||||
? frameId
|
||||
return script.frameId !== null && allowedFrameIds.has(script.frameId)
|
||||
? script.frameId
|
||||
: null;
|
||||
}
|
||||
|
||||
|
||||
@@ -3,12 +3,22 @@ import type { Browser } from "playwright-core";
|
||||
|
||||
import { browserScenarioSchema } from "../domain/browserScenario.js";
|
||||
import {
|
||||
attachedScenarioLeaseKey,
|
||||
openPlaywrightScenarioBrowser,
|
||||
PlaywrightScenarioBrowserCleanupOwner,
|
||||
} from "./PlaywrightScenarioBrowser.js";
|
||||
|
||||
afterEach(() => vi.unstubAllEnvs());
|
||||
|
||||
it("locks one attached browser for spellings that discover the same endpoint", () => {
|
||||
expect(attachedScenarioLeaseKey("http://127.0.0.1:9222", "page-1")).toBe(
|
||||
attachedScenarioLeaseKey("http://127.0.0.1:9222/", "page-1"),
|
||||
);
|
||||
expect(attachedScenarioLeaseKey("http://127.0.0.1:9222", "page-1")).not.toBe(
|
||||
attachedScenarioLeaseKey("http://127.0.0.1:9222", "page-2"),
|
||||
);
|
||||
});
|
||||
|
||||
const deferred = <Value>() => {
|
||||
let resolve: (value: Value | PromiseLike<Value>) => void = () => undefined;
|
||||
const promise = new Promise<Value>((resolvePromise) => {
|
||||
|
||||
@@ -182,6 +182,13 @@ const createAttachedScenarioRelease = (
|
||||
};
|
||||
};
|
||||
|
||||
/** One browser admission. The key is the discovery URL this endpoint already parses. */
|
||||
export const attachedScenarioLeaseKey = (
|
||||
endpoint: string,
|
||||
targetId: string,
|
||||
): string =>
|
||||
JSON.stringify([new URL("/json/version", endpoint).href, targetId]);
|
||||
|
||||
const acquireAttachedScenario = (
|
||||
scenario: Extract<BrowserScenario["browser"], { readonly mode: "connect" }>,
|
||||
signal: AbortSignal | undefined,
|
||||
@@ -189,7 +196,10 @@ const acquireAttachedScenario = (
|
||||
if (signal?.aborted === true)
|
||||
return Promise.reject(new BrowserObservationError(OPERATION, "cancelled"));
|
||||
|
||||
const key = JSON.stringify([scenario.cdp_endpoint, scenario.target_id]);
|
||||
const key = attachedScenarioLeaseKey(
|
||||
scenario.cdp_endpoint,
|
||||
scenario.target_id,
|
||||
);
|
||||
const admission =
|
||||
attachedScenarioAdmissions.get(key) ??
|
||||
({
|
||||
|
||||
@@ -2,20 +2,26 @@ import { createHash } from "node:crypto";
|
||||
import { join } from "node:path";
|
||||
import { Readable } from "node:stream";
|
||||
|
||||
import { ArtifactReaderFailure } from "../../artifacts/ArtifactReader.js";
|
||||
import { DESTINATION_CASE_COLLISION_PREFIX } from "../../artifacts/ArtifactPaths.js";
|
||||
import { SafeOutputTree } from "../../artifacts/SafeOutputTree.js";
|
||||
import {
|
||||
webScriptExportManifestSchema,
|
||||
webScriptExportResultSchema,
|
||||
type ExportWebScriptsInput,
|
||||
type ExportedWebScript,
|
||||
type WebScriptExportResult,
|
||||
} from "../../domain/webScriptExport.js";
|
||||
import { planWebScriptExport } from "../../domain/webScriptExportPlan.js";
|
||||
import {
|
||||
planWebScriptExport,
|
||||
type PlannedWebScript,
|
||||
} from "../../domain/webScriptExportPlan.js";
|
||||
import { WebScriptExportError } from "../../domain/webScriptExportError.js";
|
||||
import type { SelectedScriptCapture } from "./ScriptCaptureAdapters.js";
|
||||
|
||||
const LIMITATIONS = [
|
||||
"Only script bytes retained in this capture are exported. Sources are not refetched or executed, and a response or Debugger source does not prove execution.",
|
||||
"Ordinary unambiguous URL paths preserve relative module layouts by origin. Query variants, collisions, inline sources, and unrepresentable paths are isolated; their browser module relationships remain unknown.",
|
||||
"Ordinary unambiguous URL paths preserve relative module layouts by origin, including case-distinct spellings. Query variants, exact path collisions, file/directory prefixes, inline sources, and unrepresentable paths are isolated. A case-distinct pair is isolated only when the destination filesystem cannot store both spellings; the record names that pair. Browser module relationships of isolated paths remain unknown.",
|
||||
"Local JavaScript analysis does not implement browser import maps, root-relative URL resolution, remote URL loading, or browser runtime behavior. It may strip import queries and fragments; isolated variants are not canonical targets.",
|
||||
"Source-map declarations are retained as metadata. No source maps or missing dependencies are fetched or reconstructed.",
|
||||
"Captured response bytes are browser-decoded bytes; Debugger sources are the exposed text encoded as UTF-8. Redacted bytes are exported exactly as retained, and may no longer parse as JavaScript.",
|
||||
@@ -32,7 +38,11 @@ export const publishWebScripts = async (
|
||||
const tree = await SafeOutputTree.create(input.output_directory);
|
||||
try {
|
||||
signal?.throwIfAborted();
|
||||
const records = planned.map(({ record }) => record);
|
||||
const records: ExportedWebScript[] = [];
|
||||
for (const [index, item] of planned.entries()) {
|
||||
signal?.throwIfAborted();
|
||||
records.push(await publishPlannedScript(tree, item, index, signal));
|
||||
}
|
||||
const manifest = webScriptExportManifestSchema.parse({
|
||||
capture_path: input.capture_path,
|
||||
capture_sha256: captureSha256,
|
||||
@@ -66,19 +76,6 @@ export const publishWebScripts = async (
|
||||
bytes: bytes.length,
|
||||
},
|
||||
});
|
||||
for (const { script, record } of planned) {
|
||||
signal?.throwIfAborted();
|
||||
if (
|
||||
script.content.state === "captured" &&
|
||||
record.content.state === "exported"
|
||||
)
|
||||
await tree.write(
|
||||
`files/${record.content.relative_path}`,
|
||||
Readable.from([script.content.bytes]),
|
||||
script.content.sha256,
|
||||
signal,
|
||||
);
|
||||
}
|
||||
await tree.write("manifest.json", Readable.from([bytes]), digest, signal);
|
||||
signal?.throwIfAborted();
|
||||
await tree.commit();
|
||||
@@ -98,5 +95,56 @@ export const publishWebScripts = async (
|
||||
}
|
||||
};
|
||||
|
||||
const publishPlannedScript = async (
|
||||
tree: SafeOutputTree,
|
||||
item: PlannedWebScript,
|
||||
index: number,
|
||||
signal: AbortSignal | undefined,
|
||||
): Promise<ExportedWebScript> => {
|
||||
const { script, record } = item;
|
||||
const content = script.content;
|
||||
if (content.state !== "captured" || record.content.state !== "exported")
|
||||
return record;
|
||||
const write = (relativePath: string): Promise<unknown> =>
|
||||
tree.write(
|
||||
`files/${relativePath}`,
|
||||
Readable.from([content.bytes]),
|
||||
content.sha256,
|
||||
signal,
|
||||
);
|
||||
try {
|
||||
await write(record.content.relative_path);
|
||||
return record;
|
||||
} catch (cause: unknown) {
|
||||
const collision = destinationCollisionReason(cause);
|
||||
if (collision === undefined || record.content.layout !== "url-path")
|
||||
throw cause;
|
||||
const relativePath = isolatedScriptPath(script, index);
|
||||
await write(relativePath);
|
||||
return {
|
||||
...record,
|
||||
content: {
|
||||
...record.content,
|
||||
relative_path: relativePath,
|
||||
layout: "isolated",
|
||||
layout_reason: collision,
|
||||
},
|
||||
};
|
||||
}
|
||||
};
|
||||
|
||||
const isolatedScriptPath = (
|
||||
script: PlannedWebScript["script"],
|
||||
index: number,
|
||||
): string =>
|
||||
`isolated/source-${index + 1}-${createHash("sha256").update(JSON.stringify(script.source)).digest("hex")}.js`;
|
||||
|
||||
const destinationCollisionReason = (cause: unknown): string | undefined =>
|
||||
cause instanceof ArtifactReaderFailure &&
|
||||
cause.reason === "path" &&
|
||||
cause.message.startsWith(DESTINATION_CASE_COLLISION_PREFIX)
|
||||
? cause.message
|
||||
: undefined;
|
||||
|
||||
const message = (cause: unknown): string =>
|
||||
cause instanceof Error ? cause.message : "Unknown publication failure";
|
||||
|
||||
Reference in New Issue
Block a user