diff --git a/src/application/JavaScriptReplayPlanning.ts b/src/application/JavaScriptReplayPlanning.ts index 8492ce61f..f6214c99c 100644 --- a/src/application/JavaScriptReplayPlanning.ts +++ b/src/application/JavaScriptReplayPlanning.ts @@ -2,6 +2,10 @@ import canonicalize from "canonicalize"; import { createHash } from "node:crypto"; import type { JsonValue } from "../domain/jsonValue.js"; +import { + createSeededRandomUint, + NON_DEGENERATE_XORSHIFT32_SEED, +} from "../replay/seededRandom.js"; import type { ControlledReplayInput, ReplayExecutionResult, @@ -252,13 +256,9 @@ const replayCases = ( arguments: item.arguments, })); if (input.generator === undefined) return uniqueCases(explicit); - let state = input.generator.seed || 0x9e37_79b9; - const next = (): number => { - state ^= state << 13; - state ^= state >>> 17; - state ^= state << 5; - return state >>> 0; - }; + const next = createSeededRandomUint( + input.generator.seed || NON_DEGENERATE_XORSHIFT32_SEED, + ); const corpus = boundaryCorpus(input.generator.preset); const generated: { readonly case_id: string; diff --git a/src/replay/JavaScriptReplayWorker.ts b/src/replay/JavaScriptReplayWorker.ts index 715659e5b..10d7a130d 100644 --- a/src/replay/JavaScriptReplayWorker.ts +++ b/src/replay/JavaScriptReplayWorker.ts @@ -13,6 +13,7 @@ import type { WorkerRequest, WorkerSide, } from "./JavaScriptReplayWorkerTypes.js"; +import { createSeededRandom } from "./seededRandom.js"; const isRecord = (value: unknown): value is Record => typeof value === "object" && value !== null && !Array.isArray(value); @@ -272,13 +273,7 @@ const loadEntry = async ( }; const deterministicContext = (request: WorkerRequest, caseIndex: number) => { - let state = (request.determinism.randomSeed + caseIndex) >>> 0; - const random = (): number => { - state ^= state << 13; - state ^= state >>> 17; - state ^= state << 5; - return (state >>> 0) / 0x1_0000_0000; - }; + const random = createSeededRandom(request.determinism.randomSeed + caseIndex); const epoch = Date.parse(request.determinism.clockIso); class ReplayDate extends Date { constructor(...arguments_: unknown[]) { @@ -293,13 +288,25 @@ const deterministicContext = (request: WorkerRequest, caseIndex: number) => { return epoch; } } - const replayMath: Math = Object.create(Math); - Object.defineProperty(replayMath, "random", { value: random }); + // Observable identity must match the real builtin, or a bundle that feature- + // detects (`Date.name`, `Date.length`, `Object.getPrototypeOf(Math)`) takes a + // different branch in replay than in production. + Object.defineProperties(ReplayDate, { + name: { value: "Date", configurable: true }, + length: { value: Date.length, configurable: true }, + }); + const replayDate = new Proxy(ReplayDate, { + apply: () => new ReplayDate().toString(), + }); + // Intercept only `random`; everything else, including the prototype chain and + // property descriptors, is forwarded to the real Math. + const replayMath = new Proxy(Math, { + get: (target, property, receiver) => + property === "random" ? random : Reflect.get(target, property, receiver), + }); return createContext( { - Date: new Proxy(ReplayDate, { - apply: () => new ReplayDate().toString(), - }), + Date: replayDate, Math: replayMath, }, { diff --git a/src/replay/seededRandom.test.ts b/src/replay/seededRandom.test.ts new file mode 100644 index 000000000..36a705d6e --- /dev/null +++ b/src/replay/seededRandom.test.ts @@ -0,0 +1,43 @@ +import { describe, expect, it } from "vitest"; + +import { + createSeededRandom, + createSeededRandomUint, + normalizeXorshift32Seed, + NON_DEGENERATE_XORSHIFT32_SEED, +} from "./seededRandom.js"; + +describe("seeded replay randomness", () => { + it("does not collapse to a constant when seeded with zero", () => { + const random = createSeededRandom(0); + const values = [random(), random(), random()]; + expect(new Set(values).size).toBe(3); + expect(values.every((value) => value === 0)).toBe(false); + expect(values.every((value) => value >= 0 && value < 1)).toBe(true); + }); + + it("substitutes a fixed non-zero state for a zero seed", () => { + expect(normalizeXorshift32Seed(0)).toBe(NON_DEGENERATE_XORSHIFT32_SEED); + expect(normalizeXorshift32Seed(7)).toBe(7); + // Deterministic: the same seed must always give the same stream. + expect(Array.from({ length: 3 }, createSeededRandom(0))).toEqual( + Array.from({ length: 3 }, createSeededRandom(0)), + ); + }); + + it("produces the same stream regardless of which generator is used", () => { + const floats = createSeededRandom(11); + const uints = createSeededRandomUint(11); + expect([floats(), floats(), floats()]).toEqual([ + uints() / 0x1_0000_0000, + uints() / 0x1_0000_0000, + uints() / 0x1_0000_0000, + ]); + }); + + it("keeps distinct seeds on distinct streams", () => { + const first = Array.from({ length: 4 }, createSeededRandom(1)); + const second = Array.from({ length: 4 }, createSeededRandom(2)); + expect(first).not.toEqual(second); + }); +}); diff --git a/src/replay/seededRandom.ts b/src/replay/seededRandom.ts new file mode 100644 index 000000000..f74d3b977 --- /dev/null +++ b/src/replay/seededRandom.ts @@ -0,0 +1,53 @@ +/** + * Deterministic pseudo-random generation for controlled replay. + * + * The generator lives here so the replay worker and the case planner cannot + * drift: they previously each inlined the same xorshift32 loop, and a fix to + * one (such as the zero-seed collapse below) would silently not apply to the + * other. + */ + +/** One xorshift32 step. Returns the next unsigned 32-bit state. */ +export const nextXorshift32 = (state: number): number => { + let next = state >>> 0; + next ^= next << 13; + next ^= next >>> 17; + next ^= next << 5; + return next >>> 0; +}; + +/** + * xorshift32 has a fixed point at zero: seeding it with 0 yields 0 forever, so + * every "random" value collapses to the same constant and two runs look + * identical for the wrong reason. Substitute a fixed non-zero state instead. + */ +export const NON_DEGENERATE_XORSHIFT32_SEED = 0x9e37_79b9; + +export const normalizeXorshift32Seed = (seed: number): number => { + const state = seed >>> 0; + return state === 0 ? NON_DEGENERATE_XORSHIFT32_SEED : state; +}; + +/** A seeded generator returning floats in [0, 1). */ +export type SeededRandom = () => number; + +/** A seeded generator returning unsigned 32-bit integers. */ +export type SeededRandomUint = () => number; + +/** Build a float generator in [0, 1) whose values depend only on `seed`. */ +export const createSeededRandom = (seed: number): SeededRandom => { + let state = normalizeXorshift32Seed(seed); + return () => { + state = nextXorshift32(state); + return state / 0x1_0000_0000; + }; +}; + +/** Build an unsigned 32-bit generator whose values depend only on `seed`. */ +export const createSeededRandomUint = (seed: number): SeededRandomUint => { + let state = normalizeXorshift32Seed(seed); + return () => { + state = nextXorshift32(state); + return state; + }; +}; diff --git a/tests/boundary/providers/replay/javascriptReplayWorker.test.ts b/tests/boundary/providers/replay/javascriptReplayWorker.test.ts index 042911c06..7431d156c 100644 --- a/tests/boundary/providers/replay/javascriptReplayWorker.test.ts +++ b/tests/boundary/providers/replay/javascriptReplayWorker.test.ts @@ -497,6 +497,60 @@ describe("deterministic replay Date compatibility", () => { }); }); +describe("replay determinism and builtin identity", () => { + const evaluate = async ( + expression: string, + randomSeed: number, + ): Promise => { + const workerRequest = await request("parser.mjs", "esm", "default", []); + workerRequest.determinism.randomSeed = randomSeed; + 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); + return JSON.parse(result.stdout).left[0]; + }; + + it("varies Math.random across draws when seeded with zero", async () => { + const outcome = (await evaluate( + "[Math.random(), Math.random(), Math.random()]", + 0, + )) as { outcome: string; value?: number[] }; + expect(outcome.outcome).toBe("return"); + expect(new Set(outcome.value).size).toBe(3); + }); + + it("produces an identical stream for the same seed", async () => { + const first = await evaluate("[Math.random(), Math.random()]", 5); + const second = await evaluate("[Math.random(), Math.random()]", 5); + expect(first).toEqual(second); + }); + + it("presents Date with real builtin identity", async () => { + expect(await evaluate("[Date.name, Date.length]", 3)).toMatchObject({ + outcome: "return", + value: ["Date", 7], + }); + }); + + it("presents Math with real descriptors and unaffected members", async () => { + // Object identity is realm-specific inside the vm, so the meaningful checks + // are the descriptor and the untouched members. + const outcome = (await evaluate( + "[Object.getOwnPropertyDescriptor(Math, 'random').configurable, Object.getOwnPropertyDescriptor(Math, 'random').writable, Math.max(1, 2), Math.floor(1.7), Math.PI > 3]", + 3, + )) as { outcome: string; value?: unknown[] }; + expect(outcome).toMatchObject({ + outcome: "return", + value: [true, true, 2, 1, true], + }); + }); +}); + describe("replay array projection", () => { it.each([ ["Array(3)", [null, null, null]],