refactor: consolidate boundary semantics and prune redundant tests

Share parser modes and collision-free composite keys, enforce architecture guards, and consolidate redundant unit assertions into filesystem, CLI, MCP, and producer-backed workflows. Preserve concurrent upstream boundary fixes and exclude legacy Objective-C class-name bookkeeping labels.

Validation: source/composition suites (3358 passing), focused canonical digest and MCP checks, typecheck/lint, Knip, formatting, architecture guards, and real Foundation archive and Apple legacy simulator CLI/MCP verification. No real Hopper, Ghidra, or IDA verification claim.
This commit is contained in:
morluto
2026-10-10 05:56:01 +08:00
parent 077d6c407b
commit 392f1f3107
78 changed files with 1065 additions and 2020 deletions
-15
View File
@@ -2,7 +2,6 @@ import { describe, expect, it } from "vitest";
import {
analysisProfileSchema,
analysisProfilesEqual,
createAnalysisProfile,
} from "./analysisProfile.js";
import type { JsonValue } from "./jsonValue.js";
@@ -14,20 +13,6 @@ const PROVIDER = {
} as const;
describe("analysis profile commitments", () => {
it("canonicalizes parameter key order and validates its digest", () => {
const first = createAnalysisProfile(PROVIDER, {
architecture: "arm64",
analyzers: { strings: true, functions: true },
});
const reordered = createAnalysisProfile(PROVIDER, {
analyzers: { functions: true, strings: true },
architecture: "arm64",
});
expect(reordered.digest).toBe(first.digest);
expect(analysisProfilesEqual(first, reordered)).toBe(true);
expect(analysisProfileSchema.parse(first)).toEqual(first);
});
it("separates provider builds and semantic parameters", () => {
const baseline = createAnalysisProfile(PROVIDER, { loader: "default" });
const changedBuild = createAnalysisProfile(
+1 -27
View File
@@ -2,33 +2,7 @@ import { expect, it } from "vitest";
import { projectPlistValue } from "./plistValue.js";
it("projects plist data, dates, and non-finite reals as typed values", () => {
expect(
projectPlistValue({
blob: new Uint8Array([0, 1, 2]),
when: new Date("2020-01-01T00:00:00Z"),
nested: [{ ratio: 0.5, missing: Number.NaN, unbounded: -Infinity }],
plain: { "": "empty key", flag: true, count: 42 },
}),
).toEqual({
value: {
blob: { $plist_type: "data", base64: "AAEC" },
when: { $plist_type: "date", iso8601: "2020-01-01T00:00:00.000Z" },
nested: [
{
ratio: 0.5,
missing: { $plist_type: "real", value: null },
unbounded: { $plist_type: "real", value: null },
},
],
plain: { "": "empty key", flag: true, count: 42 },
},
unknownRealCount: 2,
});
});
it("rejects values a plist decoder cannot produce", () => {
expect(() => projectPlistValue({ value: Symbol("x") })).toThrow();
it("rejects excessive plist nesting without exhausting the stack", () => {
const deep: unknown[] = [];
let cursor = deep;
for (let depth = 0; depth < 200; depth += 1) {
-191
View File
@@ -1,191 +0,0 @@
import { expect, it } from "vitest";
import {
artifactContradictionId,
artifactEdgeId,
artifactGraphDigest,
artifactIdForContent,
artifactManifestId,
occurrenceIdForLocation,
} from "./artifactIdentity.js";
import { appleProjectionId } from "./apple/appleApplication.js";
it("keeps content identity separate from occurrence location", () => {
const root = artifactIdForContent("a".repeat(64));
expect(root).toBe(
"art_91d55c997599a1b301a52f2ad8559a9f2bfa28927d7cb2c391f3cedf6072ccdd",
);
expect(artifactIdForContent("a".repeat(64))).toBe(root);
expect(
occurrenceIdForLocation({
rootArtifactId: "art_root",
logicalPath: "one.bin",
entryKind: "file",
}),
).toBe(
"occ_e6b5ac5042e4f050a3cb25f5af8419660c01f19523189470161f44113b4c16ec",
);
expect(
occurrenceIdForLocation({
rootArtifactId: root,
logicalPath: "one.bin",
entryKind: "file",
}),
).not.toBe(
occurrenceIdForLocation({
rootArtifactId: root,
logicalPath: "two.bin",
entryKind: "file",
}),
);
expect(
occurrenceIdForLocation({
rootArtifactId: root,
logicalPath: "one.bin",
entryKind: "file",
}),
).not.toBe(
occurrenceIdForLocation({
rootArtifactId: artifactIdForContent("b".repeat(64)),
logicalPath: "one.bin",
entryKind: "file",
}),
);
});
it("commits to graph arrays in the order established by their owner", () => {
const nodes = [
{ artifact_id: "art-a", sha256: "a" },
{ artifact_id: "art-b", sha256: "b" },
] as never;
const occurrences = [
{ occurrence_id: "occ-a", logical_path: "a" },
{ occurrence_id: "occ-b", logical_path: "b" },
] as never;
const edges = [{ edge_id: "edge-a" }] as never;
const contradictions = [] as never;
const graph = artifactGraphDigest({
nodes,
occurrences,
edges,
contradictions,
});
expect(
artifactGraphDigest({
nodes: [],
occurrences: [],
edges: [],
contradictions: [],
}),
).toBe("d9b34ff5b7099912cce6c39343453ebc7318febd2ed75205ce21caf27113e98f");
expect(
artifactGraphDigest({
nodes: [
{ artifact_id: "art-a", sha256: "changed-bytes" },
nodes[1],
] as never,
occurrences,
edges,
contradictions,
}),
).not.toBe(graph);
expect(
artifactGraphDigest({
nodes,
occurrences,
edges,
contradictions: [{ contradiction_id: "ic-a" }] as never,
}),
).not.toBe(graph);
const orderedContradictions = [
{
contradiction_id: "ic-z",
logical_path: "z",
observed_sha256: "a",
},
{
contradiction_id: "ic-a",
logical_path: "a",
observed_sha256: "b",
},
] as never;
const contradictionGraph = artifactGraphDigest({
nodes: [],
occurrences: [],
edges: [],
contradictions: orderedContradictions,
});
const reorderedContradictionGraph = artifactGraphDigest({
nodes: [],
occurrences: [],
edges: [],
contradictions: [...orderedContradictions].reverse(),
});
expect(contradictionGraph).toBe(
"121f28fb3c5f104e357d502c29994961aea68be22f7f8bcae6c90e540904ddc5",
);
expect(reorderedContradictionGraph).toBe(
"f7ba200f36804975789ee85fab32c4f79922c8a9e603b076d20bab09ba87377e",
);
expect(reorderedContradictionGraph).not.toBe(contradictionGraph);
expect(artifactManifestId("art_root", contradictionGraph)).toBe(
"agm_e25e6ed72612cab4a39efbbcf17e35f119ef944955ad87c0cbecce1a1c126342",
);
expect(artifactManifestId("art_root", reorderedContradictionGraph)).toBe(
"agm_a7631939867608767e773992a4d8fc672202dd1a4a234239eb9d2e50a625fedb",
);
expect(
artifactContradictionId({
rootArtifactId: "root",
logicalPath: "entry",
declaredSha256: "a",
observedSha256: "b",
}),
).not.toBe(
artifactContradictionId({
rootArtifactId: "root",
logicalPath: "entry",
declaredSha256: "a",
observedSha256: "c",
}),
);
expect(
artifactContradictionId({
rootArtifactId: "art_root",
logicalPath: "one.bin",
declaredSha256: "a",
observedSha256: "b",
}),
).toBe("ic_69ee65b6c79c597ca02c91777882edec17064e15a3abeb4ed652adc7781ce4d2");
expect(artifactManifestId("art_root", "b".repeat(64))).toBe(
"agm_3940382b43202a6700426217151879f6274e4c362c3728368c84af993fa1f9a6",
);
expect(
artifactEdgeId({
parent_artifact_id: "art_parent",
child_artifact_id: "art_child",
relation: "contains",
occurrence_id: "occ_child",
logical_path: "child",
}),
).toMatch(/^edge_[a-f0-9]{64}$/u);
});
it("makes Apple projection identity depend on projected facts and evidence", () => {
const projection = {
source_evidence_ids: ["ev-a", "ev-b"],
classification: ["javascript", "native"],
};
expect(appleProjectionId(projection)).toBe(
"aap_eb45cb11ec77d3c98268fa90ce32d3060760b8f70a534fc9255cdb92de316bf9",
);
expect(
appleProjectionId({ ...projection, classification: ["native"] }),
).not.toBe(appleProjectionId(projection));
expect(
appleProjectionId({
...projection,
source_evidence_ids: ["ev-a", "ev-c"],
}),
).not.toBe(appleProjectionId(projection));
});
-26
View File
@@ -2,7 +2,6 @@ import { createHash } from "node:crypto";
import { expect, it } from "vitest";
import { browserNetworkBodySchema } from "./browserNetworkEvidence.js";
import { browserScenarioSchema } from "./browserScenario.js";
const bytes = Buffer.from([0xff, 0, 0xfe]);
const retained = {
@@ -27,28 +26,3 @@ it("validates the actual retained bytes against canonical base64, count, and dig
browserNetworkBodySchema.safeParse({ ...retained, ...mutation }).success,
).toBe(false);
});
it("defaults content selection independently and rejects unsupported controls", () => {
const input = {
browser: { mode: "launch", executable_path: "/fixture/chrome" },
start_url: { url: "https://example.test" },
actions: [{ step_id: "done", action: "wait_for_timeout", duration_ms: 1 }],
};
expect(browserScenarioSchema.parse(input).capture.network).toEqual({
request_body: false,
response_body: false,
header_values: false,
});
expect(
browserScenarioSchema.parse({
...input,
capture: { network: { response_body: true } },
}).capture.network,
).toEqual({ request_body: false, response_body: true, header_values: false });
expect(
browserScenarioSchema.safeParse({
...input,
capture: { network: { refetch: true } },
}).success,
).toBe(false);
});
-77
View File
@@ -3,13 +3,9 @@ import { describe, expect, it } from "vitest";
import {
browserEndpointSchema,
browserOriginSchema,
inspectWebPageInputSchema,
isLiteralLoopbackHostname,
listBrowserTargetsInputSchema,
sanitizeEndpointCandidate,
sanitizeBrowserUrl,
} from "./browserObservation.js";
import { captureWebScreenshotInputSchema } from "./webScreenshot.js";
describe("browser observation contracts", () => {
it("normalizes exact HTTP origins without accepting broader URL scopes", () => {
@@ -45,82 +41,9 @@ describe("browser observation contracts", () => {
])
expect(browserEndpointSchema.safeParse(value).success, value).toBe(false);
});
it("recognizes bracketed and normalized IPv6 loopback hostnames", () => {
for (const hostname of ["127.0.0.1", "[::1]", "::1"])
expect(isLiteralLoopbackHostname(hostname), hostname).toBe(true);
for (const hostname of ["localhost", "127.0.0.2", "::2"])
expect(isLiteralLoopbackHostname(hostname), hostname).toBe(false);
});
it("applies defaults and rejects unknown public observation input fields", () => {
expect(
listBrowserTargetsInputSchema.parse({
cdp_endpoint: "http://127.0.0.1:9222",
allowed_origins: ["https://app.example.test"],
}),
).toEqual({
cdp_endpoint: "http://127.0.0.1:9222",
allowed_origins: ["https://app.example.test"],
});
expect(
inspectWebPageInputSchema.parse({
cdp_endpoint: "http://127.0.0.1:9222",
allowed_origins: ["https://app.example.test"],
target_id: "page-1",
}),
).toMatchObject({
observation_ms: 500,
include_accessibility_text: false,
include_console_text: false,
include_json_body_shapes: false,
include_websocket_shapes: false,
include_script_sources: false,
include_storage_keys: false,
});
expect(
inspectWebPageInputSchema.safeParse({
cdp_endpoint: "http://127.0.0.1:9222",
allowed_origins: ["https://app.example.test"],
target_id: "page-1",
unknown_field: true,
}).success,
).toBe(false);
});
});
describe("browser observation sensitive surfaces and retention", () => {
it("accepts selected sensitive capture surfaces without extra flags", () => {
const base = {
cdp_endpoint: "http://127.0.0.1:9222",
allowed_origins: ["https://app.example.test"],
target_id: "page-1",
};
for (const input of [
{ include_console_text: true },
{ include_json_body_shapes: true },
{ include_websocket_shapes: true },
])
expect(
inspectWebPageInputSchema.safeParse({ ...base, ...input }).success,
).toBe(true);
expect(
inspectWebPageInputSchema.safeParse({
...base,
include_storage_fingerprints: true,
}).success,
).toBe(false);
});
it("accepts screenshot capture without an extra approval flag", () => {
const input = {
cdp_endpoint: "http://127.0.0.1:9222",
allowed_origins: ["https://app.example.test"],
target_id: "page-1",
};
expect(captureWebScreenshotInputSchema.safeParse(input).success).toBe(true);
});
it("removes only URL userinfo and preserves the original query and fragment", () => {
expect(
sanitizeBrowserUrl(
+13 -16
View File
@@ -6,7 +6,7 @@ import { describe, expect } from "vitest";
import { digestCanonicalValue } from "./canonicalDigest.js";
const canonicalizeDigest = (value: unknown): string => {
const legacyDigest = (value: unknown): string => {
const encoded = canonicalize(value);
if (encoded === undefined) throw new TypeError("Unserializable fixture");
return createHash("sha256").update(encoded).digest("hex");
@@ -16,7 +16,7 @@ describe("incremental canonical digest", () => {
it.prop([fc.jsonValue()])(
"preserves canonicalize digests for JSON values",
(value) => {
expect(digestCanonicalValue(value)).toBe(canonicalizeDigest(value));
expect(digestCanonicalValue(value)).toBe(legacyDigest(value));
},
);
@@ -25,7 +25,7 @@ describe("incremental canonical digest", () => {
const sparse: unknown[] = [undefined, Symbol("omit"), () => undefined];
sparse.length = 6;
const fixtures: unknown[] = [
{ "\ue000": "bmp", 𐀀: "supplementary" },
{ "\uE000": "BMP", "\u{10000}": "supplementary" },
{
"€": "\ud800",
"😀": "\udc00",
@@ -41,7 +41,7 @@ describe("incremental canonical digest", () => {
{ nested: { toJSON: () => undefined } },
];
for (const value of fixtures)
expect(digestCanonicalValue(value)).toBe(canonicalizeDigest(value));
expect(digestCanonicalValue(value)).toBe(legacyDigest(value));
});
it("rejects unsupported roots, non-finite numbers, BigInt and ancestor cycles", () => {
@@ -56,18 +56,15 @@ describe("incremental canonical digest", () => {
expect(() => digestCanonicalValue(value)).toThrow();
});
it("hashes repeated large leaves with the same byte sequence", () => {
const value = {
payload: Array.from({ length: 64 }, () => "x".repeat(16_384)),
};
expect(digestCanonicalValue(value)).toBe(canonicalizeDigest(value));
});
it("keeps digests identical when buffered parts cross flush boundaries", () => {
it("preserves canonical bytes across large keys, repeated leaves and buffer flushes", () => {
const manyKeys: Record<string, unknown> = {};
for (let i = 0; i < 2000; i += 1) manyKeys[`k${i}`] = i;
const value = { keys: manyKeys, tail: ["€", "😀", "x".repeat(9000)] };
expect(digestCanonicalValue(value)).toBe(canonicalizeDigest(value));
const value = {
keys: manyKeys,
payload: Array.from({ length: 64 }, () => "x".repeat(16_384)),
tail: ["€", "😀", "x".repeat(9000)],
};
expect(digestCanonicalValue(value)).toBe(legacyDigest(value));
});
it("preserves canonicalize property access and toJSON cycle behavior", () => {
@@ -81,11 +78,11 @@ describe("incremental canonical digest", () => {
};
};
expect(digestCanonicalValue(createAccessor())).toBe(
canonicalizeDigest(createAccessor()),
legacyDigest(createAccessor()),
);
const shared = { toJSON: () => ({ answer: 42 }) };
expect(digestCanonicalValue([shared, shared])).toBe(
canonicalizeDigest([shared, shared]),
legacyDigest([shared, shared]),
);
});
});
+7 -1
View File
@@ -1,6 +1,11 @@
import { createHash } from "node:crypto";
/** Hash canonicalize-compatible JSON without assembling an aggregate string. */
/**
* Streaming SHA-256 over canonical JSON. Hashing owner (avoids assembling
* aggregate strings); `comparisonSemantics.canonicalJson` (canonicalize lib)
* is the string-comparison owner. JSON object keys follow RFC 8785's UTF-16
* code-unit order in both; entity-list ordering is a separate concern.
*/
export const digestCanonicalValue = (
value: unknown,
context = "Comparison",
@@ -64,6 +69,7 @@ const emitCanonical = (
} else {
emit("{");
let first = true;
// Match canonicalize/RFC 8785, including supplementary-plane keys.
for (const key of Object.keys(value).sort()) {
if (
Reflect.get(value, key) === undefined ||
+6 -5
View File
@@ -2,7 +2,10 @@ import { z } from "zod";
import { artifactComparisonResultSchema } from "./artifactComparison.js";
import { uniqueSorted } from "./canonicalOrdering.js";
import { compareUnicodeCodePoints } from "./unicodeCodePointOrder.js";
import {
compositeKey,
compareUnicodeCodePoints,
} from "./unicodeCodePointOrder.js";
import { comparisonSourceEvidenceIds } from "./comparisonSourceEvidence.js";
import { evidenceSchema, parseEvidence, type Evidence } from "./evidence.js";
import { functionComparisonResultSchema } from "./functionComparisonSchemas.js";
@@ -389,11 +392,9 @@ const count = (
.length;
const compareFindings = (left: Finding, right: Finding): number =>
compareUnicodeCodePoints(left.scope, right.scope) ||
compareUnicodeCodePoints(left.dimension, right.dimension) ||
compareUnicodeCodePoints(
left.source_comparison_id,
right.source_comparison_id,
compositeKey([left.scope, left.dimension, left.source_comparison_id]),
compositeKey([right.scope, right.dimension, right.source_comparison_id]),
);
const assertUnique = (values: readonly string[], label: string): void => {
-61
View File
@@ -1,61 +0,0 @@
import { describe, expect, it } from "vitest";
import {
digestSchema,
isDigest,
isPrefixedDigest,
prefixedDigestSchema,
} from "./digests.js";
const DIGEST = "a".repeat(64);
const OTHER_DIGEST = "b".repeat(64);
describe("digest shapes", () => {
it("accepts a lowercase hex sha256", () => {
expect(digestSchema.parse(DIGEST)).toBe(DIGEST);
expect(isDigest(DIGEST)).toBe(true);
});
it.each([
["uppercase", DIGEST.toUpperCase()],
["too short", "a".repeat(63)],
["too long", "a".repeat(65)],
["non-hex", "g".repeat(64)],
["prefixed", `ev_${DIGEST}`],
])("rejects a %s digest", (_label, value) => {
expect(digestSchema.safeParse(value).success).toBe(false);
expect(isDigest(value)).toBe(false);
});
it("rejects non-string values", () => {
for (const value of [null, undefined, 42, {}, []])
expect(isDigest(value)).toBe(false);
});
it("keeps identifier families apart", () => {
expect(prefixedDigestSchema("ev").parse(`ev_${DIGEST}`)).toBe(
`ev_${DIGEST}`,
);
// The point of the prefix: an identifier from one family must not satisfy
// another, or a mis-pasted id would resolve to the wrong object.
expect(prefixedDigestSchema("ev").safeParse(`art_${DIGEST}`).success).toBe(
false,
);
expect(prefixedDigestSchema("ev").safeParse(DIGEST).success).toBe(false);
expect(isPrefixedDigest(`ev_${DIGEST}`, "ev")).toBe(true);
expect(isPrefixedDigest(`ev_${DIGEST}`, "art")).toBe(false);
});
it("distinguishes nested prefixes from their common ancestor", () => {
// `jag_node_` must not validate as `jag_` even though it starts with it.
expect(
prefixedDigestSchema("jag").safeParse(`jag_node_${DIGEST}`).success,
).toBe(false);
expect(prefixedDigestSchema("jag_node").parse(`jag_node_${DIGEST}`)).toBe(
`jag_node_${DIGEST}`,
);
expect(prefixedDigestSchema("jag").parse(`jag_${OTHER_DIGEST}`)).toBe(
`jag_${OTHER_DIGEST}`,
);
});
});
-5
View File
@@ -35,8 +35,3 @@ export const prefixedDigestSchema = (prefix: string): z.ZodString =>
/** True when `value` is a bare digest. */
export const isDigest = (value: unknown): value is string =>
typeof value === "string" && SHA256_PATTERN.test(value);
/** True when `value` is a digest carrying `prefix`. */
export const isPrefixedDigest = (value: unknown, prefix: string): boolean =>
typeof value === "string" &&
prefixedDigestSchema(prefix).safeParse(value).success;
-18
View File
@@ -1,18 +0,0 @@
import { describe, expect, it } from "vitest";
import { executableFormatHintSchema, validateDosComLength } from "./dosCom.js";
describe("explicit DOS COM interpretation", () => {
it.each([1, 0xff00])("admits the segment extent %i", (length) => {
expect(validateDosComLength(length).ok).toBe(true);
});
it.each([0, -1, 0xff01, 1.5, NaN, Infinity])(
"rejects invalid extent %i",
(length) => {
expect(validateDosComLength(length).ok).toBe(false);
},
);
it("does not turn arbitrary format names into raw COM", () => {
expect(executableFormatHintSchema.safeParse("dos-com").success).toBe(true);
expect(executableFormatHintSchema.safeParse("raw").success).toBe(false);
});
});
-5
View File
@@ -2,11 +2,6 @@ import { expect, it } from "vitest";
import { evmInterfaceSchema } from "./evmInterface.js";
import { evmInterfaceFixture } from "../../../tests/fixtures/evm/interface.js";
it("preserves original carrier and decoded byte identity as distinct observations", () => {
const fixture = evmInterfaceFixture();
expect(evmInterfaceSchema.parse(fixture)).toEqual(fixture);
expect(fixture.artifact.sha256).not.toBe(fixture.bytecode.sha256);
});
it.each(["byte-count", "body-offset", "selector", "authenticity"])(
"rejects false interface evidence: %s",
(problem) => {
+1 -1
View File
@@ -143,7 +143,7 @@ describe("function comparison normalized identity", () => {
},
);
it.each(["sub_deadbeef", "fcn.00401000", "FUN_00401000", "FUN_deadBEEF"])(
it.each(["sub_deadbeef", "fcn.00401000", "FUN_deadBEEF"])(
"keeps %s as an address-derived name",
(name) => {
const generated = (base: "0x1000" | "0x2000") =>
@@ -17,6 +17,7 @@ import {
range,
} from "./javascriptStaticAnalysisHelpers.js";
import { semanticStaticPropertyName } from "./javascriptAstValues.js";
import { compositeKey } from "../unicodeCodePointOrder.js";
import type { JavaScriptFindingContext } from "./javascriptStaticAnalysisState.js";
/** Inspect Electron IPC operations and validation candidates. */
@@ -161,7 +162,14 @@ const addValidation = (
};
addLocatedFinding(context, {
collection: context.accumulator.senderValidations,
key: `electron-validation\0${input.subject}\0${input.mechanism}\0${input.expected.status === "literal" ? String(input.expected.value) : input.expected.expression}`,
key: compositeKey([
"electron-validation",
input.subject,
input.mechanism,
input.expected.status === "literal"
? String(input.expected.value)
: input.expected.expression,
]),
node,
value: finding,
});
@@ -1,6 +1,9 @@
import * as t from "@babel/types";
import { compareUnicodeCodePoints } from "../unicodeCodePointOrder.js";
import {
compareUnicodeCodePoints,
compositeKey,
} from "../unicodeCodePointOrder.js";
import { stripQueryAndFragment } from "../artifactPathSyntax.js";
import type { ElectronNativeAddonBindingFinding } from "./electronStaticAnalysisTypes.js";
import { addLocatedFinding } from "./javascriptStaticAnalysisFindings.js";
@@ -141,7 +144,9 @@ const addBinding = (input: NativeBindingInput): void => {
const members = unique.length === 0 ? ["*"] : unique;
addLocatedFinding(context, {
collection: context.accumulator.nativeAddonBindings,
key: `native-addon-binding\0${kind}\0${specifier}\0${members.join("\0")}`,
// Members are a variable-length source-derived list: compositeKey keeps
// ["a\0b"] distinct from ["a", "b"] instead of collapsing one binding.
key: compositeKey(["native-addon-binding", kind, specifier, members]),
node,
value: {
specifier,
@@ -1,5 +1,8 @@
import { canonicalJson } from "../comparisonSemantics.js";
import { compareUnicodeCodePoints } from "../unicodeCodePointOrder.js";
import {
compareUnicodeCodePoints,
compositeKey,
} from "../unicodeCodePointOrder.js";
import type { ApplicationNode } from "./javascriptApplicationGraphSchemas.js";
import type { JsonValue } from "../jsonValue.js";
@@ -295,7 +298,7 @@ const uniqueStringProperty = (
const joined = (...values: readonly unknown[]): string | null => {
const strings = values.map(nonEmptyString);
return strings.some((value) => value === null) ? null : strings.join("\0");
return strings.some((value) => value === null) ? null : compositeKey(strings);
};
const nonEmptyString = (value: unknown): string | null =>
@@ -40,101 +40,116 @@ const matchingModes = [
describe.each(memberFields)(
"$kind seeds for $nodeKind / $field",
({ kind, nodeKind, field }) => {
it.each(matchingModes)(
"matches scalar and array values with $match / $value / case-sensitive $case_sensitive",
(mode) => {
const values: readonly [JsonValue, string][] = [
["openProject", ""],
[["closeProject", "openProject"], "[1]"],
];
for (const [value, suffix] of values) {
const node = memberNode(nodeKind, { [field]: value });
expect(
findApplicationFeatureSeeds([node], { kind, ...mode }),
).toEqual([propertyMatch(node, `${field}${suffix}`)]);
}
},
);
it("keeps exact, case-sensitive, and non-string exclusions", () => {
const node = memberNode(nodeKind, {
[field]: [null, 42, true, "openProject"],
});
for (const value of ["Project", "OPENPROJECT", "42", "true", "null"])
expect(findApplicationFeatureSeeds([node], seed(kind, value))).toEqual(
[],
);
expect(
findApplicationFeatureSeeds([node], {
...seed(kind, "Project"),
match: null,
}),
).toEqual([]);
});
it("retains actual indexes through nested arrays and object properties", () => {
const properties = {
nested: [{ [field]: [null, 42, false, [["openProject"]]] }],
};
const node = memberNode(nodeKind, properties);
const before = structuredClone(node);
expect(findApplicationFeatureSeeds([node], seed(kind))).toEqual([
propertyMatch(node, `nested[0].${field}[3][0][0]`),
]);
expect(node).toEqual(before);
expect(properties.nested[0]?.[field]).toEqual([
null,
42,
false,
[["openProject"]],
]);
});
it("preserves literal dotted-field suffixes for scalars and arrays", () => {
it("retains scalar and array member locations", () => {
const values: readonly [JsonValue, string][] = [
["openProject", ""],
[["openProject"], "[0]"],
[["closeProject", "openProject"], "[1]"],
];
for (const [value, suffix] of values) {
const node = memberNode(nodeKind, { [`nested.${field}`]: value });
const node = memberNode(nodeKind, { [field]: value });
expect(findApplicationFeatureSeeds([node], seed(kind))).toEqual([
propertyMatch(node, `nested.${field}${suffix}`),
]);
}
});
it("does not select near-fields, literal bracket keys, or object descendants", () => {
const cases = [
{
properties: { [`other_${field}`]: "openProject" },
path: `other_${field}`,
},
{ properties: { [`${field}[0]`]: "openProject" }, path: `${field}[0]` },
{
properties: { [`nested.${field}[0]`]: ["openProject"] },
path: `nested.${field}[0][0]`,
},
{
properties: { [field]: { name: "openProject" } },
path: `${field}.name`,
},
{
properties: { [field]: [{ name: "openProject" }] },
path: `${field}[0].name`,
},
];
for (const { properties, path } of cases) {
const node = memberNode(nodeKind, properties);
expect(findApplicationFeatureSeeds([node], seed(kind))).toEqual([]);
expect(findApplicationFeatureSeeds([node], seed("string"))).toEqual([
propertyMatch(node, path),
propertyMatch(node, `${field}${suffix}`),
]);
}
});
},
);
describe("shared array-member matching semantics", () => {
const kind = "api";
const nodeKind = "context-bridge-api";
const field = "members";
it.each(matchingModes)(
"matches $match / $value / case-sensitive $case_sensitive",
(mode) => {
const node = memberNode(nodeKind, {
members: ["closeProject", "openProject"],
});
expect(findApplicationFeatureSeeds([node], { kind, ...mode })).toEqual([
propertyMatch(node, "members[1]"),
]);
},
);
it("keeps exact, case-sensitive, and non-string exclusions", () => {
const node = memberNode(nodeKind, {
[field]: [null, 42, true, "openProject"],
});
for (const value of ["Project", "OPENPROJECT", "42", "true", "null"])
expect(findApplicationFeatureSeeds([node], seed(kind, value))).toEqual(
[],
);
expect(
findApplicationFeatureSeeds([node], {
...seed(kind, "Project"),
match: null,
}),
).toEqual([]);
});
it("retains actual indexes through nested arrays and object properties", () => {
const properties = {
nested: [{ [field]: [null, 42, false, [["openProject"]]] }],
};
const node = memberNode(nodeKind, properties);
const before = structuredClone(node);
expect(findApplicationFeatureSeeds([node], seed(kind))).toEqual([
propertyMatch(node, `nested[0].${field}[3][0][0]`),
]);
expect(node).toEqual(before);
expect(properties.nested[0]?.[field]).toEqual([
null,
42,
false,
[["openProject"]],
]);
});
it("preserves literal dotted-field suffixes for scalars and arrays", () => {
const values: readonly [JsonValue, string][] = [
["openProject", ""],
[["openProject"], "[0]"],
];
for (const [value, suffix] of values) {
const node = memberNode(nodeKind, { [`nested.${field}`]: value });
expect(findApplicationFeatureSeeds([node], seed(kind))).toEqual([
propertyMatch(node, `nested.${field}${suffix}`),
]);
}
});
it("does not select near-fields, literal bracket keys, or object descendants", () => {
const cases = [
{
properties: { [`other_${field}`]: "openProject" },
path: `other_${field}`,
},
{ properties: { [`${field}[0]`]: "openProject" }, path: `${field}[0]` },
{
properties: { [`nested.${field}[0]`]: ["openProject"] },
path: `nested.${field}[0][0]`,
},
{
properties: { [field]: { name: "openProject" } },
path: `${field}.name`,
},
{
properties: { [field]: [{ name: "openProject" }] },
path: `${field}[0].name`,
},
];
for (const { properties, path } of cases) {
const node = memberNode(nodeKind, properties);
expect(findApplicationFeatureSeeds([node], seed(kind))).toEqual([]);
expect(findApplicationFeatureSeeds([node], seed("string"))).toEqual([
propertyMatch(node, path),
]);
}
});
});
describe("feature seed selection controls", () => {
it.each([
{ kind: "api", nodeKind: "context-bridge-api", field: "label" },
@@ -1,21 +0,0 @@
import { describe, expect, it } from "vitest";
import { javascriptRecoveryInputSchema } from "./javascriptRecovery.js";
describe("JavaScript recovery requests", () => {
it.each([
{ path: "", output_directory: "/tmp/recovered" },
{ path: "/tmp/bundle.js" },
{
path: "/tmp/bundle.js",
output_directory: "/tmp/recovered",
approve: true,
},
{
path: "/tmp/bundle.js",
output_directory: "/tmp/recovered",
extraction_mode: "invented",
},
])("rejects malformed input %j", (input) => {
expect(javascriptRecoveryInputSchema.safeParse(input).success).toBe(false);
});
});
@@ -52,6 +52,8 @@ const resolveCallables = (
node: t.Node,
context: CallResolutionContext,
): LocalCallableResolution => {
// `unwrapJavaScriptExpression` strips all transparent layers iteratively,
// so a single recursion step suffices (no per-layer stack growth).
const unwrapped = unwrapJavaScriptExpression(node).node;
if (unwrapped !== node) return resolveCallables(unwrapped, context);
const direct = semanticCallableIdForNode(node);
@@ -1,13 +0,0 @@
import { expect, it } from "vitest";
import { javaScriptSemanticQueryInputSchema } from "./javascriptSemanticQuerySchemas.js";
it("rejects a source-map authority setting that the query does not consume", () => {
expect(
javaScriptSemanticQueryInputSchema.safeParse({
seed: { kind: "endpoint", value: "/api" },
direction: "forward-influence",
source_map_authority: { authority: "none" },
}).success,
).toBe(false);
});
@@ -50,7 +50,11 @@ interface TraversalFrame {
nextIndex: number;
}
/** Return child Babel nodes in visitor-key order. */
/**
* Child Babel nodes in `VISITOR_KEYS` order. Single owner for AST child
* expansion; bespoke `Object.values` / duplicated `childNodes` walkers must
* not be reintroduced (they diverge on loc/comment fields and ordering).
*/
export const childNodes = (node: t.Node): t.Node[] => {
const keys = t.VISITOR_KEYS[node.type];
if (keys === undefined) return [];
+35 -19
View File
@@ -3,35 +3,51 @@ import { parse, type ParserPlugin } from "@babel/parser";
/** Babel AST produced by REA's inert JavaScript parser boundary. */
export type ParsedJavaScriptSource = ReturnType<typeof parse>;
/**
* Babel plugins for one source path. Single owner for TypeScript/JSX
* mode selection (tsc + Babel `typescript` plugin semantics).
*
* - `dts:true` only for `.d.ts/.d.mts/.d.cts` ambient declarations;
* ordinary `.ts` keeps the missing-initializer diagnostic.
* - JSX enabled for `.tsx`, `.jsx`, `.js`, and unknown paths; disabled for
* plain `.ts/.cts/.mts` (angle-bracket assertions, not JSX) and for all
* declaration files. `.mts` (including `.d.mts`) sets
* `disallowAmbiguousJSXLike`, matching tsc's `.mts` mode.
* - `decorators-legacy` always present: without it decorated sources fail
* hard instead of reporting recovered syntax, losing every derived fact.
* - Unknown paths retain the existing JSX-capable contract (best-effort
* parse; failures surface via `errorRecovery`, never silent misparse).
*/
export const parserPluginsForPath = (sourcePath?: string): ParserPlugin[] => {
const path = sourcePath ?? "";
const dts = /\.d\.(?:ts|mts|cts)$/iu.test(path);
const isTsLike = /\.(?:ts|cts|mts)$/iu.test(path);
const isTsx = /\.tsx$/iu.test(path);
const isMts = /\.mts$/iu.test(path);
const typescript: ParserPlugin = [
"typescript",
{
dts,
...(isMts ? { disallowAmbiguousJSXLike: true } : {}),
},
];
const jsxCapable = !dts && (isTsx || !isTsLike);
return jsxCapable
? ["decorators-legacy", "jsx", typescript]
: ["decorators-legacy", typescript];
};
/** Parse JavaScript or TypeScript once without attaching comments to AST nodes. */
export const parseJavaScriptSource = (
source: string,
sourcePath?: string,
): ParsedJavaScriptSource | null => {
const path = sourcePath ?? "";
const isMts = /\.mts$/iu.test(path);
const typescript: ParserPlugin = [
"typescript",
{
dts: /\.d\.(?:ts|mts|cts)$/iu.test(path),
...(isMts ? { disallowAmbiguousJSXLike: true } : {}),
},
];
try {
return parse(source, {
sourceType: "unambiguous",
errorRecovery: true,
attachComment: false,
// "decorators-legacy" admits both the standard and the legacy decorator
// forms, including parameter decorators. Without it a decorated
// TypeScript source fails to parse at all rather than reporting
// recovered syntax, which loses every fact derived from that source.
// TypeScript's .mts mode disables JSX and rejects ambiguous angle syntax.
// Plain .ts and .cts artifacts admit angle-bracket type assertions instead of JSX.
// Unknown paths retain the existing JSX-capable parser contract.
plugins: /\.(?:ts|cts|mts)$/iu.test(path)
? ["decorators-legacy", typescript]
: ["decorators-legacy", "jsx", typescript],
plugins: parserPluginsForPath(sourcePath),
});
} catch (cause: unknown) {
// Unparseable source is represented by the null return.
@@ -16,7 +16,10 @@ import {
semanticStaticPropertyName,
unwrapJavaScriptExpression,
} from "./javascriptAstValues.js";
import { compareUnicodeCodePoints } from "../unicodeCodePointOrder.js";
import {
compareUnicodeCodePoints,
compositeKey,
} from "../unicodeCodePointOrder.js";
/** Explicit result for source text that Babel cannot parse. */
export const failedJavaScriptStaticAnalysis = (): JavaScriptStaticAnalysis => ({
@@ -674,7 +677,17 @@ export const detectVendors = (source: string): string[] =>
export const registrationKey = (
registration: JavaScriptBundlerRegistration,
): string =>
`${registration.runtime}\0${registration.chunk_keys.join("\0")}\0${String(registration.unknown_chunk_keys)}\0${registration.modules.map(({ module_key: key, source_sha256: digest }) => `${key}:${digest}`).join("\0")}`;
// Collision-free tuple: chunk/module keys are variable-length source-derived
// lists, so NUL-joins would collapse ["a\0b"] with ["a", "b"].
compositeKey([
registration.runtime,
registration.chunk_keys,
String(registration.unknown_chunk_keys),
registration.modules.map(({ module_key: key, source_sha256: digest }) => [
key,
digest,
]),
]);
/** Sort values by a deterministic unique semantic key. */
export const sortedUnique = <Value>(
@@ -14,16 +14,10 @@ const absoluteParts = [
["/app", "/shared/preload.js"],
["ignored", "/app", "preload.js"],
["/app", "first", "/shared", "nested", "../preload.js"],
["/app", "preload/"],
["/app", "preload///"],
["/app/", "./sub", "../preload.js"],
["/app", ".."],
["/app", "../../.."],
["/"],
["///"],
["/app", "/", ""],
["", "/app", "", "preload.js", ""],
["/app", "..", "child", "..", "preload.js"],
];
const anchoredExpressions = [
@@ -38,21 +32,12 @@ const anchoredExpressions = [
'path.resolve("ignored", path.dirname(__filename), "preload.js")',
"preload.js",
],
['path.resolve(__dirname, "sub", "../preload.js")', "preload.js"],
['path.resolve(__dirname, "../preload.js")', "../preload.js"],
['path.resolve(__dirname, "preload/")', "preload"],
['path.resolve(__dirname, "sub", __dirname, "preload.js")', "preload.js"],
['path.resolve(__dirname, "/shared", "preload.js")', "/shared/preload.js"],
[
'path.resolve(dirname(fileURLToPath(import.meta.url)), "/shared/preload.js")',
"/shared/preload.js",
],
["path.resolve(__dirname)", "."],
['path.resolve(__dirname, "")', "."],
['path.resolve(__dirname, "sub/../")', "."],
['path.posix.resolve(`/app`, `/shared`, "preload.js")', "/shared/preload.js"],
['path.posix.resolve("" + "/app", "preload.js")', "/app/preload.js"],
['path.posix.resolve(`/` + `app`, "preload.js")', "/app/preload.js"],
] as const;
const unknownExpressions = [
+10 -7
View File
@@ -1,6 +1,9 @@
import { posix } from "node:path";
import { compareUnicodeCodePoints } from "../unicodeCodePointOrder.js";
import {
compareUnicodeCodePoints,
compositeKey,
} from "../unicodeCodePointOrder.js";
import type { ApplicationNode } from "./javascriptApplicationGraphSchemas.js";
import type { HistoricalSourceGraph } from "../referenceSourceGraph.js";
import {
@@ -322,15 +325,15 @@ const uniquePaths = (paths: readonly CurrentPath[]): CurrentPath[] =>
[
...new Map(
paths.map((path) => [
`${path.kind}\0${path.value}\0${String(path.allowSuffix)}`,
compositeKey([path.kind, path.value, String(path.allowSuffix)]),
path,
]),
).values(),
].sort(
(left, right) =>
compareUnicodeCodePoints(left.kind, right.kind) ||
compareUnicodeCodePoints(left.value, right.value) ||
Number(left.allowSuffix) - Number(right.allowSuffix),
].sort((left, right) =>
compareUnicodeCodePoints(
compositeKey([left.kind, left.value]),
compositeKey([right.kind, right.value]),
),
);
const pathSuffixes = (path: string): string[] => {
+22 -49
View File
@@ -39,45 +39,19 @@ describe("inferJsonShape", () => {
expect(inferJsonShape("not-json")).toBeNull();
});
it.each([
["é", "e\u0301", "e\u0341"],
["é", "e\u0341", "e\u0301"],
["e\u0301", "é", "e\u0341"],
["e\u0301", "e\u0341", "é"],
["e\u0341", "é", "e\u0301"],
["e\u0341", "e\u0301", "é"],
])("orders distinct collation-equal paths from %j, %j, %j", (...names) => {
for (const name of names) expect(name.localeCompare("é")).toBe(0);
const values: Readonly<Record<string, unknown>> = {
é: 1,
"e\u0301": "value",
"e\u0341": null,
};
const shape = inferJsonShape(
JSON.stringify(
Object.fromEntries(names.map((name) => [name, values[name]])),
),
);
expect(shape).toEqual({
root_type: "object",
node_count: 4,
max_depth_observed: 1,
properties: [
{ path: "/e\u0301", types: ["string"], observations: 1 },
{ path: "/e\u0341", types: ["null"], observations: 1 },
{ path: "/é", types: ["number"], observations: 1 },
],
});
});
it.each([false, true])(
"preserves nested types, counts, depth and pointer names with reversed keys: %s",
(reverse) => {
it("preserves pointer escaping and code-point ordering independently of key insertion order", () => {
for (const reverse of [false, true]) {
const object = (number: number, value: unknown) => {
const entries = [
["é/~", number],
["e\u0301/~", value],
["e\u0341/~", true],
["A", false],
["_", false],
["a", false],
["z", false],
["\u{10000}", false],
["\uE000", false],
];
return Object.fromEntries(reverse ? entries.reverse() : entries);
};
@@ -87,31 +61,30 @@ describe("inferJsonShape", () => {
expect(shape).toEqual({
root_type: "object",
node_count: 9,
node_count: 23,
max_depth_observed: 4,
properties: [
{ path: "/nested", types: ["array"], observations: 1 },
{ path: "/nested/*/A", types: ["boolean"], observations: 2 },
{ path: "/nested/*/_", types: ["boolean"], observations: 2 },
{ path: "/nested/*/a", types: ["boolean"], observations: 2 },
{
path: "/nested/*/e\u0301~1~0",
types: ["array", "null"],
observations: 2,
},
{
path: "/nested/*/e\u0341~1~0",
types: ["boolean"],
observations: 2,
},
{ path: "/nested/*/z", types: ["boolean"], observations: 2 },
{ path: "/nested/*/é~1~0", types: ["number"], observations: 2 },
{ path: "/nested/*/\uE000", types: ["boolean"], observations: 2 },
{ path: "/nested/*/\u{10000}", types: ["boolean"], observations: 2 },
],
});
},
);
it("orders paths deterministically by Unicode code point", () => {
const names = ["z", "é", "A", "_", "a"];
// Code-point order (locale-independent): A < _ < a < z < é.
// `localeCompare` order varies by host ICU and must not leak into evidence.
const expected = ["/A", "/_", "/a", "/z", "/é"];
const shape = inferJsonShape(
JSON.stringify(Object.fromEntries(names.map((name) => [name, true]))),
);
expect(shape?.properties.map(({ path }) => path)).toEqual(expected);
}
});
it("retains every parsed property beyond the former shape-node limit", () => {
+8 -3
View File
@@ -1,6 +1,9 @@
import { z } from "zod";
import { compareUnicodeCodePoints } from "../unicodeCodePointOrder.js";
import {
compareUnicodeCodePoints,
compositeKey,
} from "../unicodeCodePointOrder.js";
/** Objective-C method type. */
export const objcMethodTypeSchema = z.enum([
@@ -397,8 +400,10 @@ export const inspectNativeDispatchMetadata = (
}));
const decodedImplementations = [...implementations.values()].sort(
(left, right) =>
compareUnicodeCodePoints(left.class_name, right.class_name) ||
compareUnicodeCodePoints(left.selector, right.selector),
compareUnicodeCodePoints(
compositeKey([left.class_name, left.selector]),
compositeKey([right.class_name, right.selector]),
),
);
const swift = swiftSymbols.length > 0;
+1 -8
View File
@@ -1,10 +1,6 @@
import { expect, it } from "vitest";
import {
omitPrototypeKeys,
omittedPrototypeKeysLimitation,
parseXmlPropertyList,
} from "./propertyListKeys.js";
import { omitPrototypeKeys, parseXmlPropertyList } from "./propertyListKeys.js";
const plist = (body: string) =>
`<?xml version="1.0" encoding="UTF-8"?><plist version="1.0">${body}</plist>`;
@@ -67,7 +63,4 @@ it("omits own __proto__ entries that JSON decoding creates", () => {
value: { a: [{ b: 3 }] },
omittedPrototypeKeys: 2,
});
expect(omittedPrototypeKeysLimitation(1)).toBe(
"1 dictionary entry keyed __proto__ was omitted because REA results cannot represent that key.",
);
});
+8 -3
View File
@@ -3,7 +3,10 @@ import { createHash } from "node:crypto";
import canonicalize from "canonicalize";
import { z } from "zod";
import { compareUnicodeCodePoints } from "./unicodeCodePointOrder.js";
import {
compareUnicodeCodePoints,
compositeKey,
} from "./unicodeCodePointOrder.js";
import { evidenceBundleSchema, parseEvidenceBundle } from "./evidenceBundle.js";
import {
@@ -405,8 +408,10 @@ const closureStatus = (
};
const reasonOrder = (left: ClosureReason, right: ClosureReason): number =>
compareUnicodeCodePoints(left.code, right.code) ||
compareUnicodeCodePoints(left.subject_id, right.subject_id);
compareUnicodeCodePoints(
compositeKey([left.code, left.subject_id]),
compositeKey([right.code, right.subject_id]),
);
const digest = (value: unknown): string => {
const encoded = canonicalize(value);
+10 -6
View File
@@ -1,5 +1,8 @@
import { uniqueSorted } from "./canonicalOrdering.js";
import { compareUnicodeCodePoints } from "./unicodeCodePointOrder.js";
import {
compareUnicodeCodePoints,
compositeKey,
} from "./unicodeCodePointOrder.js";
import type {
ReconstructionClaim,
ReconstructionClaimResult,
@@ -90,7 +93,7 @@ export const reconstructionProbes = (
const probes = unknowns.flatMap(({ recommended_probes: items }) => items);
const candidates = probes.length > 0 ? probes : [defaultProbe(result.kind)];
for (const probe of candidates) {
const key = `${probe.operation}\0${probe.rationale}`;
const key = compositeKey([probe.operation, probe.rationale]);
const current = output.get(key) ?? {
...probe,
claim_ids: [],
@@ -104,10 +107,11 @@ export const reconstructionProbes = (
output.set(key, current);
}
}
const sorted = [...output.values()].sort(
(left, right) =>
compareUnicodeCodePoints(left.operation, right.operation) ||
compareUnicodeCodePoints(left.rationale, right.rationale),
const sorted = [...output.values()].sort((left, right) =>
compareUnicodeCodePoints(
compositeKey([left.operation, left.rationale]),
compositeKey([right.operation, right.rationale]),
),
);
return sorted;
};
@@ -1,81 +0,0 @@
import { describe, expect, it } from "vitest";
import { parseReferenceSourceImports } from "./referenceSourceImportParsing.js";
const parse = (source: string) =>
parseReferenceSourceImports(
"main.ts",
new TextEncoder().encode(source),
"TypeScript",
);
describe("historical source dynamic imports", () => {
it.each([
'import("./lazy.js");',
'const module = import("./lazy.js");',
'async function load() { return await import("./lazy.js"); }',
'import("./lazy.js", { with: { type: "json" } });',
])("retains the literal dependency in %s", (source) => {
expect(parse(source)).toEqual({
relationships: [
{
from_path: "main.ts",
to: "./lazy.js",
kind: "imports",
resolution: "internal",
parse_state: "parsed",
},
],
parse_failures: [],
});
});
it.each([
"import(moduleName);",
"async function load(name: string) { return await import(name); }",
"import(`./${moduleName}.js`);",
])("keeps a computed dependency unknown in %s", (source) => {
expect(parse(source)).toEqual({
relationships: [
{
from_path: "main.ts",
to: "<dynamic-import>",
kind: "imports",
resolution: "unknown",
parse_state: "partial",
},
],
parse_failures: [],
});
});
it("retains static import and require relationships alongside dynamic imports", () => {
const result = parse(
'import "./static.js"; require("./common.cjs"); import("node:fs");',
);
expect(result.parse_failures).toEqual([]);
expect(result.relationships).toEqual([
{
from_path: "main.ts",
to: "./static.js",
kind: "imports",
resolution: "internal",
parse_state: "parsed",
},
{
from_path: "main.ts",
to: "./common.cjs",
kind: "requires",
resolution: "internal",
parse_state: "parsed",
},
{
from_path: "main.ts",
to: "node:fs",
kind: "imports",
resolution: "external",
parse_state: "parsed",
},
]);
});
});
+10 -13
View File
@@ -1,4 +1,4 @@
import { parse, type ParserPlugin } from "@babel/parser";
import { parse } from "@babel/parser";
import type {
CallExpression,
File,
@@ -7,6 +7,7 @@ import type {
StringLiteral,
} from "@babel/types";
import { traverseJavaScriptAst } from "./javascript/javascriptSemanticTraversal.js";
import { parserPluginsForPath } from "./javascript/javascriptSourceParser.js";
import {
isCallExpression,
isExportAllDeclaration,
@@ -206,6 +207,9 @@ const extractRequireAndDynamicImports = (
from_path: string,
relationships: ReferenceSourceImportRelationship[],
): void => {
// Single traversal owner: iterative, VISITOR_KEYS-gated, no recursion over
// loc/comment objects. Survives deeply nested generated member chains that
// overflowed the previous hand-rolled Object.values walker.
const expressions: Array<CallExpression | ImportExpression> = [];
for (const statement of body)
traverseJavaScriptAst(statement, {
@@ -263,18 +267,11 @@ const parseWithBabel = (
ast: File | undefined;
reasons: readonly string[];
} => {
const plugins: ParserPlugin[] = ["jsx"];
if (language === "TypeScript" || language === "TSX") {
plugins.push([
"typescript",
{
dts:
path.endsWith(".d.ts") ||
path.endsWith(".d.mts") ||
path.endsWith(".d.cts"),
},
]);
}
// Single parser-mode owner: path-driven plugins (dts/JSX/mts) shared with
// the semantic pipeline. `language` only gates non-JS/TS callers upstream;
// TypeScript syntax must parse identically here and in semantic analysis.
void language;
const plugins = parserPluginsForPath(path);
try {
const ast = parse(source, {
@@ -1,150 +0,0 @@
import { describe, expect, it } from "vitest";
import { parseReferenceSourceImports } from "./referenceSourceImportParsing.js";
const recoverySource = [
"const missing;",
"let duplicate;",
"let duplicate;",
"let duplicate;",
].join("\n");
const recoveryReasons = [
"Missing initializer in const declaration. (1:13)",
"Identifier 'duplicate' has already been declared. (3:4)",
"Identifier 'duplicate' has already been declared. (4:4)",
];
const moduleSource = [
'import "./static.js";',
'export { value } from "./named.js";',
'export * from "./all.js";',
'require("./common.cjs");',
'require.resolve("node:fs");',
'import("./lazy.js");',
"import(moduleName);",
].join("\n");
const moduleRelationships = [
["./static.js", "imports", "internal", "parsed"],
["./named.js", "imports", "internal", "parsed"],
["./all.js", "imports", "internal", "parsed"],
["./common.cjs", "requires", "internal", "parsed"],
["node:fs", "requires", "external", "parsed"],
["./lazy.js", "imports", "internal", "parsed"],
["<dynamic-import>", "imports", "unknown", "partial"],
].map(([to, kind, resolution, parse_state]) => ({
to,
kind,
resolution,
parse_state,
}));
const parseSource = (path: string, source: string, language: string | null) =>
parseReferenceSourceImports(path, new TextEncoder().encode(source), language);
describe.each([
["main.js", "JavaScript"],
["main.jsx", "JSX"],
["main.ts", "TypeScript"],
["main.tsx", "TSX"],
])("recovered reference imports in %s", (path, language) => {
it("retains every diagnostic and marks recovered relationships partial", () => {
const result = parseSource(
path,
`${recoverySource}\n${moduleSource}`,
language,
);
expect(result.parse_failures).toEqual(
recoveryReasons.map((reason) => ({ path, parser: "babel", reason })),
);
expect(result.relationships).toEqual(
moduleRelationships.map((relationship) => ({
...relationship,
from_path: path,
parse_state: "partial",
})),
);
});
it("preserves valid and computed relationships without diagnostics", () => {
expect(parseSource(path, moduleSource, language)).toEqual({
relationships: moduleRelationships.map((relationship) => ({
...relationship,
from_path: path,
})),
parse_failures: [],
});
});
it("reports recovery diagnostics when there are no relationships", () => {
expect(parseSource(path, recoverySource, language)).toEqual({
relationships: [],
parse_failures: recoveryReasons.map((reason) => ({
path,
parser: "babel",
reason,
})),
});
});
it("returns no relationships when parsing throws", () => {
expect(
parseSource(path, 'import "./before.js";\nconst = ;', language),
).toEqual({
relationships: [],
parse_failures: [
{ path, parser: "babel", reason: "Unexpected token (2:6)" },
],
});
});
});
it.each(["main.ts", "types.d.ts", "types.d.mts", "types.d.cts"])(
"marks recovered TypeScript declarations partial in %s",
(path) => {
const result = parseSource(
path,
[
"let duplicate: number;",
"let duplicate: number;",
'import dep = require("node:path");',
'declare module "ambient-package" {}',
].join("\n"),
"TypeScript",
);
expect(result).toEqual({
relationships: [
{
from_path: path,
to: "node:path",
kind: "requires",
resolution: "external",
parse_state: "partial",
},
{
from_path: path,
to: "ambient-package",
kind: "declares-module",
resolution: "unknown",
parse_state: "partial",
},
],
parse_failures: [
{
path,
parser: "babel",
reason: "Identifier 'duplicate' has already been declared. (2:4)",
},
],
});
},
);
it("keeps unsupported source languages outside Babel parsing", () => {
expect(parseSource("main.py", recoverySource, "Python")).toEqual({
relationships: [],
parse_failures: [],
});
});
@@ -1,104 +0,0 @@
import { describe, expect, it } from "vitest";
import { parseReferenceSourceImports } from "./referenceSourceImportParsing.js";
const parseRequire = (source: string) =>
parseReferenceSourceImports(
"main.cjs",
new TextEncoder().encode(source),
"JavaScript",
);
const knownCallees = [
"require",
"require.resolve",
'require["resolve"]',
"require.main",
'require["main"]',
];
describe("historical source require member names", () => {
it.each([
...knownCallees,
'require[("resolve")]',
'require["res\\u006flve"]',
])(
"retains a literal dependency for %s beside computed members",
(callee) => {
expect(
parseRequire(
`require[resolve]("./ignored.js"); ${callee}("./dep.js");`,
),
).toEqual({
relationships: [
{
from_path: "main.cjs",
to: "./dep.js",
kind: "requires",
resolution: "internal",
parse_state: "parsed",
},
],
parse_failures: [],
});
},
);
it.each([
'require[resolve]("./dep.js");',
'require[main]("./dep.js");',
'const resolve = "toString"; require[resolve]("./dep.js");',
'const main = "toString"; require[main]("./dep.js");',
'const resolve = "resolve"; require[resolve]("./dep.js");',
'const main = "main"; require[main]("./dep.js");',
'const method = "resolve"; require[method]("./dep.js");',
'require[`resolve`]("./dep.js");',
'require["re" + "solve"]("./dep.js");',
])("does not infer computed member values in %s", (source) => {
expect(parseRequire(source)).toEqual({
relationships: [],
parse_failures: [],
});
});
it.each([
"require?.",
"require.resolve?.",
'require["resolve"]?.',
"require.main?.",
'require["main"]?.',
"require?.resolve",
'require?.["resolve"]',
"require?.[resolve]",
"require?.main",
'require?.["main"]',
"require?.[main]",
"(require?.resolve)",
"require.resolve.call",
"require.main.require",
"module.require",
"other.resolve",
"other.require",
"require.toString",
'require["toString"]',
])("leaves the unrecognized callee %s unchanged", (callee) => {
expect(parseRequire(`${callee}("./dep.js");`)).toEqual({
relationships: [],
parse_failures: [],
});
});
it.each(knownCallees)("does not guess unknown arguments to %s", (callee) => {
expect(
parseRequire(
[
'const target = "./dep.js";',
`${callee}(target);`,
`${callee}(\`./dep.js\`);`,
`${callee}(getTarget());`,
`${callee}();`,
].join("\n"),
),
).toEqual({ relationships: [], parse_failures: [] });
});
});
+9 -5
View File
@@ -2,7 +2,10 @@ import { createHash } from "node:crypto";
import canonicalize from "canonicalize";
import { z } from "zod";
import { compareUnicodeCodePoints } from "./unicodeCodePointOrder.js";
import {
compareUnicodeCodePoints,
compositeKey,
} from "./unicodeCodePointOrder.js";
import { prefixedDigestSchema } from "./../domain/digests.js";
const evidenceIdSchema = prefixedDigestSchema("ev");
@@ -293,8 +296,9 @@ const sortedUnique = (values: readonly string[]): string[] =>
const sortedRelationships = (
values: readonly z.infer<typeof relationshipSchema>[],
): z.infer<typeof relationshipSchema>[] =>
[...values].sort(
(left, right) =>
compareUnicodeCodePoints(left.type, right.type) ||
compareUnicodeCodePoints(left.unknown_id, right.unknown_id),
[...values].sort((left, right) =>
compareUnicodeCodePoints(
compositeKey([left.type, left.unknown_id]),
compositeKey([right.type, right.unknown_id]),
),
);
-11
View File
@@ -1,11 +0,0 @@
import { expect, it } from "vitest";
import { safeParseJson } from "./safeJson.js";
it("keeps the original JSON parse failure as the cause", () => {
const parsed = safeParseJson("{");
if (parsed.ok) throw new Error("expected invalid JSON to fail");
expect(parsed.error).toContain("JSON");
expect(parsed.cause).toBeInstanceOf(SyntaxError);
});
+1 -182
View File
@@ -1,160 +1,9 @@
import { describe, expect, it } from "vitest";
import {
categorizeSwiftTypes,
discoverObjcClasses,
discoverObjcProtocols,
} from "./symbolAnalysis.js";
describe("symbol analysis", () => {
it("deduplicates Objective-C classes and protocols by symbol name", () => {
const names = [
{ address: "0x1", name: "_OBJC_CLASS_$_App" },
{ address: "0x2", name: "_OBJC_CLASS_$_App" },
{ address: "0x3", name: "_OBJC_PROTOCOL_$_Delegate" },
{ address: "0x4", name: "__OBJC_PROTOCOL_REFERENCE_$_Delegate" },
{ address: "0x4", name: "_OBJC_PROTOCOL_$_Delegate" },
];
expect(discoverObjcClasses(names, "App")).toMatchObject({ count: 1 });
expect(discoverObjcProtocols(names)).toMatchObject({ count: 1 });
});
it("excludes Objective-C ivars, metaclasses, properties, and protocols from classes", () => {
const result = discoverObjcClasses(
[
{ address: "0x1", name: "_OBJC_IVAR_$_Fixture.value" },
{ address: "0x2", name: "_OBJC_METACLASS_$_Fixture" },
{ address: "0x3", name: "_OBJC_PROP_$_Fixture.value" },
{ address: "0x4", name: "_OBJC_PROTOCOL_$_FixtureProtocol" },
{ address: "0x5", name: "_OBJC_CLASS_$_Fixture" },
],
"",
);
expect(result).toMatchObject({
count: 1,
classes: [{ address: "0x5", name: "_OBJC_CLASS_$_Fixture" }],
});
});
it("recognizes fragile Apple Objective-C class records without alias duplication", () => {
// Apple clang -target i386-apple-macosx10.5
// -fobjc-runtime=macosx-fragile-10.5 emits these local class records.
// The `.objc_class_name_*` absolute symbols alias the same class and are
// deliberately not selected.
const names = [
{ address: "0x1ac", name: "l_OBJC_CLASS_ProbeClass" },
{ address: "0x0", name: ".objc_class_name_ProbeClass" },
// A class named `NAME_` produces this exact legacy local symbol.
{ address: "0x0", name: "l_OBJC_CLASS_NAME_" },
];
expect(discoverObjcClasses(names, "")).toEqual({
count: 2,
classes: [
{ address: "0x1ac", name: "l_OBJC_CLASS_ProbeClass" },
{ address: "0x0", name: "l_OBJC_CLASS_NAME_" },
],
});
});
it("discovers modern Swift protocol descriptors without treating methods as declarations", () => {
// Current swiftc emits `…Mp` descriptors without the nominal `P` marker.
const names = [
{ address: "0xf0", name: "_$s4main7ScoringMp" },
{ address: "0x100", name: "_$s4main7ScoringTL" },
{ address: "0x110", name: "_$s4main7ScoringP5scoreSiyFTq" },
];
expect(discoverObjcProtocols(names)).toEqual({
count: 1,
protocols: [{ address: "0xf0", name: "_$s4main7ScoringMp" }],
});
});
it("categorizes every Swift mangling family and deduplicates names", () => {
const result = categorizeSwiftTypes([
{ address: "1", name: "_TtCClass" },
{ address: "2", name: "_TtVStruct" },
{ address: "3", name: "_TtOEnum" },
{ address: "4", name: "_TtPProtocol" },
{ address: "5", name: "_TtEExtension" },
{ address: "6", name: "prefix_TtOther" },
{ address: "7", name: "_TtCClass" },
]);
expect(result).toMatchObject({
total: 6,
categories: {
classes: { count: 1 },
structs: { count: 1 },
enums: { count: 1 },
protocols: { count: 1 },
extensions: { count: 1 },
other: { count: 1 },
},
});
});
it("filters Swift categories by a case-sensitive literal name", () => {
const result = categorizeSwiftTypes(
[
{ address: "0x1", name: "_TtCAccount" },
{ address: "0x2", name: "_TtCSession" },
{ address: "0x3", name: "_TtVAccountState" },
],
{ category: "classes", pattern: "Account" },
);
expect(result).toMatchObject({
total: 1,
categories: {
classes: {
count: 1,
items: [{ address: "0x1", name: "_TtCAccount" }],
},
structs: { count: 0, items: [] },
},
});
});
it("classifies modern Swift manglings reported by current providers", () => {
const result = categorizeSwiftTypes([
{ address: "0x1000009f8", name: "$s4main4PairVMa" },
{ address: "0x100000ac4", name: "$s4main6ScorerVMa" },
{ address: "0x100000ab4", name: "_$s4main4PairVMa" },
{ address: "0x1", name: "_TtCClass" },
{ address: "0x1000009f8", name: "$s4main4PairVMa" },
]);
expect(result).toMatchObject({
total: 4,
categories: {
classes: { count: 1 },
structs: {
count: 3,
items: [
{ address: "0x1000009f8", name: "$s4main4PairVMa" },
{ address: "0x100000ac4", name: "$s4main6ScorerVMa" },
{ address: "0x100000ab4", name: "_$s4main4PairVMa" },
],
},
},
});
});
it("keeps non-Swift procedure names out of the Swift inventory", () => {
const result = categorizeSwiftTypes([
{ address: "0x1", name: "printf" },
{ address: "0x2", name: "_OBJC_CLASS_$_App" },
{ address: "0x3", name: "std::vector<int>::push_back" },
]);
expect(result).toMatchObject({ total: 0 });
});
});
import { categorizeSwiftTypes } from "./symbolAnalysis.js";
describe("modern Swift category evidence", () => {
it.each([
["$s4main4PairVMa", "structs"],
["_$s4main6ScorerVMa", "structs"],
["$s4main7ServiceCMa", "classes"],
["$s4main5StateOMa", "enums"],
["$s4main7ScoringPMp", "protocols"],
@@ -203,36 +52,6 @@ describe("modern Swift category evidence", () => {
).toMatchObject({ total: 0, unclassified: [] });
});
it("joins Swift aliases only at the exact procedure address", () => {
const result = categorizeSwiftTypes(
[
{ address: "0x1", name: "main::Pair::typeMetadataAccessor" },
{ address: "0x2", name: "std::vector<int>::push_back" },
{ address: "0x3", name: "main::Pair::other" },
],
{ category: "structs", pattern: "Pair" },
[
{ address: "0x1", name: "_$s4main4PairVMa" },
{ address: "0x4", name: "$s4main4PairVMa" },
],
);
expect(result).toMatchObject({
total: 1,
categories: {
structs: {
items: [
{
address: "0x1",
name: "main::Pair::typeMetadataAccessor",
mangled_names: ["_$s4main4PairVMa"],
},
],
},
},
unclassified: [],
});
});
it("deduplicates display names after selecting a category", () => {
expect(
categorizeSwiftTypes(
+2 -1
View File
@@ -28,7 +28,8 @@ export const discoverObjcClasses = (
({ name }) =>
name.includes(OBJC_CLASS_MARKER) ||
(name.startsWith(LEGACY_OBJC_CLASS_PREFIX) &&
name.length > LEGACY_OBJC_CLASS_PREFIX.length),
name.length > LEGACY_OBJC_CLASS_PREFIX.length &&
!/^l_OBJC_CLASS_NAME_(?:\.\d+)?$/u.test(name)),
)
.filter(({ name }) => pattern.length === 0 || name.includes(pattern)),
);
+8
View File
@@ -16,3 +16,11 @@ export const compareUnicodeCodePoints = (
if (leftIndex === left.length && rightIndex === right.length) return 0;
return leftIndex === left.length ? -1 : 1;
};
/**
* Collision-free composite key for deduplication/sorting. `\0`-joins
* collide when any component contains NUL (valid in JS property names);
* a JSON tuple escapes every component. Single owner for composite keys.
*/
export const compositeKey = (parts: readonly unknown[]): string =>
JSON.stringify(parts);
+9 -1
View File
@@ -2,6 +2,7 @@ import { parse } from "@babel/parser";
import * as t from "@babel/types";
import { sanitizeEndpointCandidate } from "./browserObservation.js";
import { compositeKey } from "./unicodeCodePointOrder.js";
import type { WebPageInspection } from "./browserObservationSchemas.js";
import type { WebBundleAnalysis } from "./webBundleAnalysis.js";
import { traverseJavaScriptAst } from "./javascript/javascriptSemanticTraversal.js";
@@ -271,7 +272,14 @@ const addWebMcpDeclaration = (
: [],
)
: [];
const key = `webmcp\0${script.script_key}\0${name ?? ""}\0${schemaPropertyNames.join("\0")}`;
// Property names are a variable-length source-derived list: NUL-joins
// would collapse ["a\0b"] with ["a", "b"] and drop a declaration.
const key = compositeKey([
"webmcp",
script.script_key,
name ?? "",
schemaPropertyNames,
]);
addUnique(accumulator, key, () =>
accumulator.webMcp.push({
name: name ?? null,
+1 -24
View File
@@ -12,15 +12,6 @@ import {
} from "./webContentArtifact.js";
describe("web content artifacts", () => {
it("binds UTF-8 content, size, and digest inline", () => {
const artifact = createWebTextArtifact(
"const marker = '😀';",
"text/javascript",
);
expect(webTextArtifactSchema.parse(artifact)).toEqual(artifact);
expect(artifact.bytes).toBe(Buffer.byteLength(artifact.text));
});
it("rejects altered content that reuses an artifact digest", () => {
const artifact = createWebTextArtifact("original", "text/javascript");
expect(
@@ -83,7 +74,7 @@ describe("stable web inventory", () => {
content_size: 1_024,
},
];
const forward = stableWebResources(inputs);
const forward = stableWebResources([...inputs, ...inputs.slice(0, 1)]);
const reverse = stableWebResources([...inputs].reverse());
expect(reverse).toEqual(forward);
@@ -117,20 +108,6 @@ describe("stable web inventory", () => {
});
});
it("deduplicates identical resource observations by stable identity", () => {
const duplicate = {
url: script.url,
origin: "https://app.example.test",
type: "Script",
mime_type: "text/javascript",
content_size: 1_024,
};
expect(stableWebResources([duplicate, duplicate])).toEqual(
stableWebResources([duplicate]),
);
});
it("reconciles raw URLs independently of stable inventory sort order", () => {
const app = {
url: script.url,
-37
View File
@@ -1,37 +0,0 @@
import { describe, expect, it } from "vitest";
import { z } from "zod";
import {
webRuntimeLocationSchema,
webRuntimeSourceSchema,
} from "./webRuntime.js";
const properties = (schema: z.ZodType) =>
z
.object({ properties: z.record(z.string(), z.unknown()) })
.parse(z.toJSONSchema(schema, { io: "output" })).properties;
describe("web runtime coordinate contract", () => {
it("advertises zero-based resource-relative CDP lines and UTF-16 columns", () => {
const location = properties(webRuntimeLocationSchema);
expect(location.line_number).toMatchObject({
description: expect.stringContaining(
"Zero-based line in the resource named by url",
),
});
expect(location.script_id).toMatchObject({
description: expect.stringContaining("no session-script association"),
});
expect(location.column_number).toMatchObject({
description: expect.stringContaining("Zero-based UTF-16 column"),
});
const start = properties(
webRuntimeSourceSchema.shape.resource_start.unwrap(),
);
expect(start.line_number).toMatchObject({
description: expect.stringContaining(
"Zero-based line where the script text starts",
),
});
});
});
-40
View File
@@ -1,40 +0,0 @@
import { describe, expect, it } from "vitest";
import {
captureWebScreenshotInputSchema,
compareWebScreenshotsInputSchema,
createWebScreenshotArtifact,
} from "./webScreenshot.js";
describe("inline web screenshots", () => {
it("rejects removed screenshot size and pixel controls", () => {
expect(
captureWebScreenshotInputSchema.safeParse({
cdp_endpoint: "http://127.0.0.1:9222",
allowed_origins: ["https://app.example.test"],
target_id: "page-1",
maximum_image_bytes: 1,
}).success,
).toBe(false);
const artifact = createWebScreenshotArtifact(Buffer.from("image"));
expect(
compareWebScreenshotsInputSchema.safeParse({
before: artifact,
after: artifact,
maximum_pixels: 1,
}).success,
).toBe(false);
});
it("accepts a caller-supplied target ID without a length ceiling", () => {
const targetId = "target-".repeat(100);
expect(
captureWebScreenshotInputSchema.parse({
cdp_endpoint: "http://127.0.0.1:9222",
allowed_origins: ["https://app.example.test"],
target_id: targetId,
}).target_id,
).toBe(targetId);
});
});