Hmbown/CodeWhale · error · Error

Invalid pet random stream.

Error message

Invalid pet random stream.

What it means

mulberry32 creates a seeded PRNG used by the pet renderer and exposes a restore(state) method to resume a previous random stream. The stream state must be a single unsigned 32-bit safe integer; anything else would corrupt the PRNG, so restore throws 'Invalid pet random stream.'

Solutions

  1. Inspect the value passed to restore(); log it and confirm Number.isSafeInteger(v) && v >= 0 && v <= 0xffffffff.
  2. If the state came from serialization, coerce with `state >>> 0` before restoring, or clamp/reject at the save site.
  3. If the state was produced by a different RNG, re-seed with the original seed (mulberry32(seed)) instead of restoring foreign state.
  4. Regenerate the pet with a fresh seed if the previous stream state is unrecoverable.

Example fix

// before
pet.random.restore(parsed.state); // parsed.state is 4294967296.5 after JSON round-trip
// after
if (Number.isSafeInteger(parsed.state) && parsed.state >= 0 && parsed.state <= 0xffffffff) {
  pet.random.restore(parsed.state);
} else {
  pet.random.restore(0xC0FFEE); // re-seed fallback
}
Defensive patterns

Strategy: validation

Validate before calling

function validPrngState(v) { return Number.isSafeInteger(v) && v >= 0 && v <= 0xffffffff; }
if (!validPrngState(state)) state = state >>> 0;

Type guard

const isPrngState = (v) => Number.isSafeInteger(v) && v >= 0 && v <= 0xffffffff;

Try / catch

try {
  pet.random.restore(state);
} catch (e) {
  if (e.message === 'Invalid pet random stream.') pet.random.restore(seed);
  else throw e;
}

Prevention

When it happens

Trigger: Calling petRandom.restore(state) with a non-integer, negative number, or a value > 0xffffffff (e.g. a float captured by serializing state, a state from a different PRNG, or a state stored as a string).

Common situations: Restoring a checkpoint saved by an older build whose state width differed; passing mulberry32 state through JSON round-trips that turned integers into floats or strings; sharing state generated by another RNG like xorshift.

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@73e0f67d83 (2026-09-22). Data as JSON: /api/errors/7e1f1ca5b16f0550. Report an issue: GitHub.

Appendix: source

Thrown at crates/tui/src/tui/pet_watch/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 73e0f67d83)