fix(javascript): retain array identity after length mutations

Keep unknown element coverage on arrays whose length may have changed, preserving intrinsic length presence and subsequent slot mutations through semantic graph and export-return consumers.
This commit is contained in:
morluto
2026-10-10 06:20:28 +08:00
parent 4951a897c5
commit 4bcea1aa76
3 changed files with 65 additions and 32 deletions
@@ -761,37 +761,42 @@ it.each([
},
);
it("retains array length presence without inventing an exact length", () => {
const graph = graphFor("const root = [, 1]; const length = root.length;");
const slot = graph.nodes.find(
({ kind, properties }) =>
kind === "property-slot" && properties.property_pointer === "/length",
);
expect(slot?.properties).toMatchObject({
name: "length",
presence: "present",
value_status: "unknown",
});
const trace = queryJavaScriptSemanticGraph(graph, {
seed: { kind: "property", name: "length" },
direction: "forward-influence",
allowed_relations: ["reads-property"],
});
expect(trace.seed_node_ids).toEqual([slot?.node_id]);
expect(trace.relations).toContainEqual(
expect.objectContaining({
source_node_id: slot?.node_id,
relation: "reads-property",
resolution: "resolved",
}),
);
expect(
graph.relations.some(
({ target_node_id, relation }) =>
target_node_id === slot?.node_id && relation === "defines",
),
).toBe(false);
});
it.each(["", "root.length = 0;", "root.length = query(); root[0] = 9;"])(
"retains array length presence without inventing an exact length after %s",
(mutation) => {
const graph = graphFor(
`const root = [, 1]; ${mutation} const length = root.length;`,
);
const slot = graph.nodes.find(
({ kind, properties }) =>
kind === "property-slot" && properties.property_pointer === "/length",
);
expect(slot?.properties).toMatchObject({
name: "length",
presence: "present",
value_status: "unknown",
});
const trace = queryJavaScriptSemanticGraph(graph, {
seed: { kind: "property", name: "length" },
direction: "forward-influence",
allowed_relations: ["reads-property"],
});
expect(trace.seed_node_ids).toEqual([slot?.node_id]);
expect(trace.relations).toContainEqual(
expect.objectContaining({
source_node_id: slot?.node_id,
relation: "reads-property",
resolution: "resolved",
}),
);
expect(
graph.relations.some(
({ target_node_id, relation }) =>
target_node_id === slot?.node_id && relation === "defines",
),
).toBe(false);
},
);
it.each([
["object spread", 'const root = { known: "value", ...dynamic };'],
@@ -35,7 +35,13 @@ export const invalidateSemanticMutationPath = (
const name = String(key);
// Array length writes can remove every index; ordinary named properties
// and sparse indices affect only their own slot.
if (name === "length") return unknown;
if (name === "length")
return {
status: "array",
items: [],
unknownItems: true,
omittedItems: null,
};
const observed = value.items.some((item) => item.name === name);
const items = value.items.map((item) =>
item.name === name
@@ -232,6 +232,24 @@ it.each([
change: "unknown",
baseline: 'const result = {kind:"r",count:2};',
},
{
title: "array length mutation",
body: 'const items = [1]; items.length = query(); const result = {kind:"r", length: items.length, items};',
path: "/length",
presence: "present",
valueState: "unknown",
change: "unknown",
baseline: 'const result = {kind:"r",length:1,items:[2]};',
},
{
title: "array length mutation followed by another write",
body: 'const items = [1]; items.length = 0; items[0] = 9; const result = {kind:"r", length: items.length, items};',
path: "/length",
presence: "present",
valueState: "unknown",
change: "unknown",
baseline: 'const result = {kind:"r",length:1,items:[2]};',
},
{
title: "an array hole",
body: 'const result = {kind:"r",items:[,]};',
@@ -270,6 +288,10 @@ it.each([
expect(projection.static_return_shapes[0]?.fields).toContainEqual(
expect.objectContaining({ path, presence, state: valueState }),
);
if (path === "/length")
expect(
projection.static_return_shapes[0]?.property_coverage,
).toContainEqual({ path: "/items", status: "partial", omitted: null });
const { client, close } = await createApplicationMcpHarness();
onTestFinished(close);
const traced = await client.callTool({