Hmbown/CodeWhale · error · Error

Invalid pet random stream.

Error message

Invalid pet random stream.

What it means

PetWorld uses a mulberry32 PRNG whose 32-bit state `a` can be snapshotted via `state()` and restored via `restore(state)`. `restore` throws when the given state is not a safe integer in the range [0, 0xffffffff] — i.e. anything outside what `state()` can ever produce.

Solutions

  1. Only restore values captured from `rng.state()`, stored verbatim (as an integer, not reformatted) — verify the saved value with `Number.isSafeInteger(s) && s >= 0 && s <= 0xffffffff` before calling restore.
  2. If the stored state is a string, convert with `Number(s)` and re-check the range before restoring.
  3. For a fresh stream, re-seed the PRNG by constructing it with a valid seed instead of restoring a fabricated state.

Example fix

// before
rng.restore(saved.rngState); // may be a string/null from JSON
// after
const s = Number(saved.rngState);
if (!Number.isSafeInteger(s) || s < 0 || s > 0xffffffff) {
  throw new Error('corrupt saved rng state: ' + saved.rngState);
}
rng.restore(s);
Defensive patterns

Strategy: type-guard

Validate before calling

function isRestorableRngState(s) { return Number.isSafeInteger(s) && s >= 0 && s <= 0xffffffff; }

Type guard

function isRngState(v) { return typeof v === 'number' && Number.isSafeInteger(v) && v >= 0 && v <= 0xffffffff; }

Try / catch

try { rng.restore(saved); } catch (e) { if (e.message.includes('Invalid pet random stream')) { rng = makeRng(DEFAULT_SEED); } else throw e; }

Prevention

When it happens

Trigger: Calling `rng.restore(x)` with a value that was not produced by `rng.state()` — e.g. a float from JSON parsing, a negative number, a value > 4294967295, a null/undefined placeholder, or a state saved from a different PRNG implementation.

Common situations: Persisting RNG state to JSON/storage where it round-trips as a string or loses exactness; hand-crafting seeds outside 32 bits; restoring from a save file written by an older version with a different state format; mixing up a seed value with a mid-stream state value.

Understand the failure class

Background: "value must be between 0 and 1" / "out of range" / "must not be negative" errors: fixing range-validation failures across open-source libraries — this error's family across 42 libraries.

Related errors


AI-assisted analysis of Hmbown/CodeWhale@433685b202 (2026-09-15). Data as JSON: /api/errors/db7188b9b1d56251. Report an issue: GitHub.

Appendix: source

Thrown at pet/ios/Resources/pet-native.js:728

        || ![s.roamX, s.roamY, s.flip].every(n => Number.isFinite(n) && Math.abs(n) <= 1))
        throw new Error('Invalid pet state.');
}
const hex2rgb = (h) => [parseInt(h.slice(1, 3), 16), parseInt(h.slice(3, 5), 16), parseInt(h.slice(5, 7), 16)];
const RGB = exports.CHANNELS.map(c => hex2rgb(c.color));
const UNKNOWN_RGB = hex2rgb('#738492');
const REST_RGB = [122, 214, 240];
// mulberry32 — a 32-bit seeded PRNG tiny enough to port by hand correctly.
function mulberry32(seed) {
    let a = seed >>> 0;
    return Object.assign(() => {
        a = (a + 0x6D2B79F5) >>> 0;
        let t = a;
        t = Math.imul(t ^ (t >>> 15), t | 1);
        t ^= t + Math.imul(t ^ (t >>> 7), t | 61);
        return ((t ^ (t >>> 14)) >>> 0) / 4294967296;
    }, { state: () => a, restore: (state) => {
            if (!Number.isSafeInteger(state) || state < 0 || state > 0xffffffff)
                throw new Error('Invalid pet random stream.');
            a = state;
        } });
}
/** Work reorganizes the same particles; no new random draws or invented facts.
 * These are expressive fields, not diagrams of unobserved network/file topology. */
function fieldTarget(q, t, act, att, key) {
    const u = q.s * 2 - 1, lane = q.pod - 2.5, a = q.s * Math.PI * 2;
    const flow = t * (.35 + act * .65);
    if (key === 'reasoning') {
        const ring = .34 + .105 * Math.cos(a * 3 + flow + lane * .18);
        return [ring * Math.cos(a * 2 + flow * .3), ring * Math.sin(a * 2 + flow * .3) * .7 + .10 * Math.sin(a * 3 + flow)];
    }
    if (key === 'memory')
        return [.46 * Math.cos(a + lane * .1 + flow * .25), lane * .082 + .052 * Math.sin(a * 2 + flow)];
    if (key === 'code')
        return [u * .57, lane * .066 + .12 * Math.sin(u * 7 + flow * 2 + q.pod * Math.PI / 3)];
    if (key === 'filesystem') {
        const branch = Math.max(0, (u + .3) / 1.3);

View on GitHub (pinned to 433685b202)