fix(javascript): resolve module-relative Worker URL references (#2104)

* fix(javascript): resolve module-relative Worker URL references

* fix(javascript): validate Worker URL paths independently of suffixes

* fix(javascript): preserve directory-form Worker URL paths

* fix(javascript): unwrap type assertions in Worker URL references

* fix(javascript): validate Worker bindings before URL resolution

* fix(javascript): leave drive-letter Worker URLs unresolved

Use WHATWG file URL parsing before pathname decoding to detect drive-letter
forms whose resource identity depends on the unmodeled source URL scheme.
Preserve encoded and ordinary relative pipe paths, and cover false matches
through the compiled CLI and resolver boundary tests.

* fix(javascript): preserve Worker URL identity boundaries

Recognize known Worker bindings before filtering callee spellings so ESM
aliases and transparent constructor wrappers reach URL analysis. Keep
root-relative module URLs unknown without a modeled runtime base root.

Index direct source-local global writes with the shared mutation collector
before promoting URL and browser Worker constructors. Preserve lexical
shadowing and unrelated property identities, and exercise all three review
regressions through the compiled CLI.

* fix(javascript): invalidate mutated import.meta URL bases

Track source-local import.meta writes alongside ambient global writes so
Worker URL expressions keep an unknown base after a possible URL mutation.
Tag the identity roots independently and cover direct mutation and unrelated
property cases through the compiled CLI.

* fix(javascript): close Worker URL identity and pathname gaps

Resolve ambient object identities before appending written properties, and
retain possible source-local aliases across rebinding. Reuse intrinsic
invocation handling for aliased and bound mutators, array spreads, and
transparent TypeScript wrappers while preserving Electron write semantics.

Keep malformed-percent nested Worker URL pathnames unknown because the base
scheme is unavailable and Node rejects these file URLs. Preserve tolerant
HTML URL handling, valid escapes, and query/fragment evidence.

Cover the new cases and unrelated writes through the compiled CLI with
existing-file decoys. All 591 targeted tests and npm run check pass.

* fix(javascript): bound alias expansion and classify Worker paths

Join ambient identity projections instead of enumerating alias histories.
Memoize repeated value and provenance queries while preserving capture,
depth, and cycle-dependent uncertainty.

Keep Node Worker string arguments unresolved without filesystem or cwd
context, and admit qualified global URL constructors through identity checks.

Validate three 40-level alias branch shapes through the compiled CLI,
925 source tests, 25 CLI/boundary tests, and the full npm run check gate.

* fix(server): preserve partial Evidence target-content provenance

Pass the exact named contract returnsTargetContent flag through the shared partial-Evidence error delivery path introduced by the shortlist integration.

Exercise successful and failed recording through in-memory MCP for marked and unmarked contracts, preserving source payload and original failures. Observe both shared-handler emission and wire delivery so the SDK error-marker fallback cannot hide omissions. Suppressing the flag fails exactly the two marked cases.

Validation: check:fast, check, 736 semantic tests, 29 MCP transport/error tests and six compiled static Worker CLI journeys pass. No live target/provider execution or hosted publication.

* fix(javascript): structurally share semantic dependency proofs

---------

Co-authored-by: baron <richstyles@gmail.com>
This commit is contained in:
Yann Roberto
2026-10-13 06:24:45 +08:00
committed by GitHub
parent 3b0ea590fc
commit 653067d799
19 changed files with 1946 additions and 171 deletions
@@ -0,0 +1,227 @@
import { expect, it, vi } from "vitest";
import {
analyzeJavaScriptSemantics,
indexJavaScriptBindingsSteps,
} from "./javascriptSemanticAnalysis.js";
import { SemanticDependencyProofs } from "./javascriptSemanticDependencies.js";
import type { JavaScriptSemanticValue } from "./javascriptSemanticValueTypes.js";
import {
clearSemanticValueCaches,
evaluateSemanticBinding,
evaluateSemanticProvenance,
} from "./javascriptSemanticValues.js";
import { parseJavaScriptSource } from "./javascriptSourceParser.js";
const property = (
value: JavaScriptSemanticValue,
name: string,
): JavaScriptSemanticValue => {
if (value.status !== "object")
throw new Error(`Expected object at ${name}, got ${value.status}`);
const result = value.properties.find((entry) => entry.name === name)?.value;
if (result === undefined) throw new Error(`Missing ${name}`);
return result;
};
it.each([false, true])(
"recomputes a shared cyclic value under a different active ancestor (prewarmed: %s)",
(prewarmed) => {
const parsed = parseJavaScriptSource(
`
const a = { shared: common, own: "a" };
const b = { shared: common, own: "b" };
const common = { a, b };
const result = { first: a, second: b };
`,
"cycles.js",
);
if (parsed === null) throw new Error("Expected parsed source");
const steps = indexJavaScriptBindingsSteps(parsed.program);
let step = steps.next();
while (step.done !== true) step = steps.next();
const state = step.value;
const binding = [...state.bindingsById.values()].find(
(entry) => entry.name === "result",
);
if (binding === undefined) throw new Error("Missing result binding");
if (prewarmed)
for (const entry of state.bindingsById.values())
if (entry.name === "a" || entry.name === "b")
evaluateSemanticBinding(entry, state);
const value = evaluateSemanticBinding(binding, state);
const firstCommon = property(property(value, "first"), "shared");
const secondCommon = property(property(value, "second"), "shared");
expect(property(firstCommon, "a").status).toBe("cycle");
expect(property(property(firstCommon, "b"), "own")).toEqual({
status: "literal",
value: "b",
});
expect(property(secondCommon, "b").status).toBe("cycle");
expect(property(property(secondCommon, "a"), "own")).toEqual({
status: "literal",
value: "a",
});
},
);
it("invalidates retained alias values at the producing mutation boundary", () => {
const parsed = parseJavaScriptSource(
"const target = { value: 1 }; const alias = target; const root = { alias };",
"mutation.js",
);
if (parsed === null) throw new Error("Expected parsed source");
const steps = indexJavaScriptBindingsSteps(parsed.program);
let step = steps.next();
while (step.done !== true) step = steps.next();
const state = step.value;
const target = [...state.bindingsById.values()].find(
({ name }) => name === "target",
);
const root = [...state.bindingsById.values()].find(
({ name }) => name === "root",
);
if (target === undefined || root === undefined)
throw new Error("Missing binding");
expect(
property(property(evaluateSemanticBinding(root, state), "alias"), "value"),
).toEqual({ status: "literal", value: 1 });
target.mutatedPaths.push(["value"]);
clearSemanticValueCaches(state);
expect(
property(property(evaluateSemanticBinding(root, state), "alias"), "value")
.status,
).toBe("unknown");
});
const balancedOr = (names: string[]): string => {
if (names.length === 1) return names[0]!;
const middle = Math.floor(names.length / 2);
return `(${balancedOr(names.slice(0, middle))} || ${balancedOr(names.slice(middle))})`;
};
it("shares dependency proofs across a shallow wide reconvergent graph", () => {
const measure = (width: number) => {
const imports = Array.from({ length: width }, (_, index) => `l${index}`);
const aliases = Array.from({ length: width }, (_, index) => `a${index}`);
const source = `${imports.map((name) => `import * as ${name} from "fs";`).join("\n")}
const shared = ${balancedOr(imports)};
function consumer() {
${aliases.map((name) => `const ${name} = shared;`).join("\n")}
const root = ${balancedOr(aliases)};
}`;
const parsed = parseJavaScriptSource(source, "wide.js");
if (parsed === null) throw new Error("Expected parsed source");
const steps = indexJavaScriptBindingsSteps(parsed.program);
let step = steps.next();
while (step.done !== true) step = steps.next();
const state = step.value;
const rootBinding = [...state.bindingsById.values()].find(
({ name }) => name === "root",
);
if (rootBinding === undefined) throw new Error("Missing root binding");
// One root query simultaneously retains every alias memo in this family.
// Separate per-binding provenance queries have baseline repeated traversal;
// they must not be confused with this cache's retained closure expansion.
// Count representation construction and union work, not elapsed time/heap.
// Binding-ID additions also discriminate the superseded flattened Sets.
let bindingMemberships = 0;
let proofBranches = 0;
let unionCalls = 0;
const add = Set.prototype.add;
const freeze = Object.freeze;
const union = SemanticDependencyProofs.prototype.union;
const addSpy = vi.spyOn(Set.prototype, "add").mockImplementation(function (
this: Set<unknown>,
value: unknown,
) {
if (typeof value === "string" && value.includes("binding:"))
bindingMemberships++;
return add.call(this, value);
});
const freezeSpy = vi.spyOn(Object, "freeze").mockImplementation((value) => {
if (
typeof value === "object" &&
value !== null &&
Object.hasOwn(value, "zero") &&
Object.hasOwn(value, "one") &&
Object.keys(value).length === 2
)
proofBranches++;
return freeze(value);
});
const unionSpy = vi
.spyOn(SemanticDependencyProofs.prototype, "union")
.mockImplementation(function (
this: SemanticDependencyProofs,
first,
second,
) {
unionCalls++;
return union.call(this, first, second);
});
try {
expect(evaluateSemanticBinding(rootBinding, state).status).toBe(
"ambiguous",
);
expect(evaluateSemanticProvenance(rootBinding, state).origins).toEqual([
{ specifier: "fs", importedPath: [] },
]);
} finally {
unionSpy.mockRestore();
freezeSpy.mockRestore();
addSpy.mockRestore();
}
const result = analyzeJavaScriptSemantics(source);
expect(result.coverage).toEqual({ status: "complete", omittedCount: 0 });
for (const name of [...imports, ...aliases, "shared", "root"])
expect(
result.bindings.filter((entry) => entry.name === name),
).toHaveLength(1);
const root = result.bindings.find((entry) => entry.name === "root");
expect(root?.value.status).toBe("ambiguous");
expect(root?.provenance.origins).toEqual([
{ specifier: "fs", importedPath: [] },
]);
return {
storage: bindingMemberships + proofBranches,
work: bindingMemberships + unionCalls,
};
};
// Balanced depths remain small; doubling both independent widths must not
// quadruple proof construction/work while all meaningful aliases survive.
const small = measure(32);
const large = measure(64);
expect(small.storage).toBeGreaterThan(0);
expect(small.work).toBeGreaterThan(0);
expect(large.storage).toBeLessThan(small.storage * 3);
expect(large.work).toBeLessThan(small.work * 3);
});
it("uses immutable exact identity proofs and shares unchanged union branches", () => {
const proofs = new SemanticDependencyProofs(["a", "b", "c", "d", "e"]);
const a = proofs.binding("a");
const b = proofs.binding("b");
const c = proofs.binding("c");
const ab = proofs.union(a, b);
const abc = proofs.union(ab, c);
expect(proofs.union(abc, ab)).toBe(abc);
expect(proofs.binding("a")).toBe(a);
expect(Object.isFrozen(abc)).toBe(true);
expect(["a", "b", "c", "d", "e"].map((id) => proofs.has(abc, id))).toEqual([
true,
true,
true,
false,
false,
]);
expect(proofs.has(undefined, "a")).toBe(false);
expect(proofs.intersects(abc, new Map([["d", 0]]))).toBe(false);
expect(proofs.intersects(abc, new Map([["c", 0]]))).toBe(true);
const single = new SemanticDependencyProofs(["only"]);
expect(single.has(single.binding("only"), "only")).toBe(true);
expect(single.has(undefined, "only")).toBe(false);
expect(() => proofs.binding("unindexed")).toThrow(
"Unindexed semantic dependency",
);
});