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
@@ -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[] => {