From 9d1c3631b8deccb5fb91906130f38e3f24aa254e Mon Sep 17 00:00:00 2001 From: Rudy Mizrahi Celekli <47457359+rudycelekli@users.noreply.github.com> Date: Mon, 5 Oct 2026 02:50:05 -0400 Subject: [PATCH] fix(replay): preserve sparse array positions in result projection (#522) Signed-off-by: RudyCelekli <47457359+rudycelekli@users.noreply.github.com> --- src/replay/JavaScriptReplayWorker.ts | 11 +++++ .../replay/javascriptReplayWorker.test.ts | 45 +++++++++++++++++++ 2 files changed, 56 insertions(+) diff --git a/src/replay/JavaScriptReplayWorker.ts b/src/replay/JavaScriptReplayWorker.ts index e0056b7eb..2af759e8d 100644 --- a/src/replay/JavaScriptReplayWorker.ts +++ b/src/replay/JavaScriptReplayWorker.ts @@ -434,6 +434,17 @@ const projectComplexValue = ( ); } } + if (Array.isArray(candidate) && Array.isArray(output)) { + for (let index = 0; index < candidate.length; index += 1) { + if (Object.hasOwn(descriptors, String(index))) continue; + output[index] = projectValueRecursive( + null, + depth + 1, + ancestors, + context, + ); + } + } return output; }; diff --git a/tests/boundary/providers/replay/javascriptReplayWorker.test.ts b/tests/boundary/providers/replay/javascriptReplayWorker.test.ts index ee7263fe8..dce65a6d3 100644 --- a/tests/boundary/providers/replay/javascriptReplayWorker.test.ts +++ b/tests/boundary/providers/replay/javascriptReplayWorker.test.ts @@ -225,3 +225,48 @@ describe("disposable JavaScript replay worker", () => { }); }); }); + +describe("replay array projection", () => { + it.each([ + ["Array(3)", [null, null, null]], + [ + "(() => { const value = Array(3); value[1] = 42; return value; })()", + [null, 42, null], + ], + ["[1, null, 3]", [1, null, 3]], + ["[]", []], + ])("preserves JSON array positions for %s", async (expression, expected) => { + const workerRequest = await request("parser.mjs", "esm", "default", []); + workerRequest.left.modules[0] = { + alias: "entry", + format: "esm", + dependencies: {}, + source: `export default function () { return ${expression}; }`, + }; + const result = await runWorker(workerRequest); + expect(result.code).toBe(0); + expect(JSON.parse(result.stdout)).toMatchObject({ + left: [{ outcome: "return", value: expected }], + }); + }); + + it("accounts for holes in the existing result node budget", async () => { + const workerRequest = await request("parser.mjs", "esm", "default", []); + workerRequest.left.modules[0] = { + alias: "entry", + format: "esm", + dependencies: {}, + source: "export default function () { return Array(4); }", + }; + workerRequest.limits.resultNodes = 3; + const result = await runWorker(workerRequest); + expect(JSON.parse(result.stdout)).toMatchObject({ + left: [ + { + outcome: "serialization_error", + exception: { message: "Replay result projection limit exceeded" }, + }, + ], + }); + }); +});