{"record":{"id":"01490fce75a16e45","repo":"Hmbown/CodeWhale","slug":"invalid-pet-recording-native","errorCode":null,"errorMessage":"Invalid pet recording.","messagePattern":"Invalid pet recording\\.","errorType":"validation","errorClass":"Error","httpStatus":null,"severity":"error","filePath":"pet/ios/Resources/pet-native.js","lineNumber":248,"sourceCode":"            random: this.random.state(), behaviour: this.behaviour, until: this.until,\n            targetX: this.targetX, targetY: this.targetY, x: this.x, y: this.y, flip: this.flip, lit: this.lit,\n            lastActivity: this.lastActivity, addressedAt: Number.isFinite(this.addressedAt) ? this.addressedAt : null,\n            waitSince: this.waitSince, food: this.food, members: [...this.members.values()], lastStill: this.lastStill,\n            frame: this.frame, voices: this.voices });\n    }\n    recording(withCheckpoint = true, completed = false) {\n        const tapeEnd = completed ? this.bucketIndex + 1 : this.tapeLog.length;\n        const inputEnd = completed ? this.interactionIndex : this.interactionLog.length;\n        const history = this.historyDigest(tapeEnd, inputEnd);\n        return { petReplayVersion: this.segmented ? 2 : 1, expressionVersion: this.sim.expressionVersion,\n            tape: this.tapeLog.slice(0, tapeEnd), interactions: this.interactionLog.slice(0, inputEnd).map(e => ({ ...e })),\n            ...(this.origin ? { start: { ...structuredClone(this.origin), hasTelemetry: this.hasTelemetry, history } } : {}),\n            ...(withCheckpoint ? { checkpoint: { ...this.checkpoint(), history } } : {}) };\n    }\n    static fromRecording(points, value) {\n        const r = value;\n        if (!r || ![1, 2].includes(r.petReplayVersion) || !Array.isArray(r.tape) || !Array.isArray(r.interactions))\n            throw new Error('Invalid pet recording.');\n        const version = r.expressionVersion === undefined ? 1 : r.expressionVersion;\n        if (![1, 2].includes(version))\n            throw new Error('Unsupported pet expression version.');\n        if (r.checkpoint !== undefined && !r.checkpoint || r.start !== undefined && !r.start)\n            throw new Error('Invalid pet checkpoint.');\n        for (const c of [r.start, r.checkpoint])\n            if (c && (c.sim?.expressionVersion ?? 1) !== version)\n                throw new Error('Pet expression version does not match its checkpoint.');\n        if (r.petReplayVersion === 1 && (r.start || r.checkpoint && r.checkpoint.petCheckpointVersion !== 1))\n            throw new Error('Invalid legacy pet recording.');\n        if (r.petReplayVersion === 2 && (!r.start || r.start.petCheckpointVersion !== 2 || r.start.historyStart !== r.start.tick))\n            throw new Error('The recording segment is missing its starting checkpoint.');\n        const first = r.start && PetWorld.restore(points, r.tape, r.interactions, r.start);\n        const world = r.checkpoint ? PetWorld.restore(points, r.tape, r.interactions, r.checkpoint) : first ?? new PetWorld(points, r.tape, r.interactions, version);\n        if (first) {\n            if (!world.segmented || world.tick < first.tick || r.checkpoint && r.checkpoint.historyStart !== first.tick)\n                throw new Error('Pet segment checkpoints do not agree.');\n            world.origin = first.checkpoint();","sourceCodeStart":230,"sourceCodeEnd":266,"githubUrl":"https://github.com/Hmbown/CodeWhale/blob/433685b2024e7bc4c99e1e2e326bcad39b4d9d65/pet/ios/Resources/pet-native.js#L230-L266","documentation":"PetWorld.fromRecording validates a serialized pet replay before restoring world state. This error means the value passed as the recording is null, or fails the most basic shape check: it must carry a petReplayVersion of 1 or 2 plus array-valued 'tape' and 'interactions' fields. The library throws early rather than restoring a world from corrupt or foreign data.","triggerScenarios":"Calling PetWorld.fromRecording(points, value) where value is null/undefined, value.petReplayVersion is missing or not 1 or 2, or value.tape / value.interactions is missing or not an array.","commonSituations":"Truncated or hand-edited export chunks; concatenating recordingChunk output in the wrong order so JSON.parse yields garbage; passing a recording produced by a different (older/newer) build whose top-level keys changed; passing a checkpoint object instead of the full recording.","solutions":["Log the parsed value and confirm petReplayVersion, tape, and interactions are present with the right types before calling fromRecording.","Re-export the recording via world.recording() / recordingChunk() and reassemble chunks 0..N in order (including the completed=true final chunk).","Check the code path that produced the JSON: it must be a full pet recording, not a checkpoint or partial slice.","If importing old data, verify the producer's petReplayVersion; recordings without petReplayVersion cannot be imported."],"exampleFix":"// before\nconst world = PetWorld.fromRecording(points, JSON.parse(savedText));\n// after\nconst r = JSON.parse(savedText);\nif (!r || ![1,2].includes(r.petReplayVersion) || !Array.isArray(r.tape) || !Array.isArray(r.interactions))\n  throw new Error('Not a valid pet recording: ' + JSON.stringify(Object.keys(r ?? {})));\nconst world = PetWorld.fromRecording(points, r);","handlingStrategy":"validation","validationCode":"function isPetRecording(v) { return !!v && [1,2].includes(v.petReplayVersion) && Array.isArray(v.tape) && Array.isArray(v.interactions); }","typeGuard":"const isPetRecording = (v: unknown): v is PetRecording =>\n  typeof v === 'object' && v !== null && [1,2].includes((v as any).petReplayVersion)\n  && Array.isArray((v as any).tape) && Array.isArray((v as any).interactions);","tryCatchPattern":"let world;\ntry { world = PetWorld.fromRecording(points, recording); }\ncatch (e) { if (e.message === 'Invalid pet recording.') throw new Error('Corrupt replay: re-export required'); throw e; }","preventionTips":["Only load recordings produced by world.recording()/recordingChunk(); never hand-assemble the object.","Validate with your own type guard before calling fromRecording.","Reassemble chunks strictly in order from index 0 and parse the concatenation once.","Store a checksum with saved recordings to detect truncation."],"tags":["validation","replay","schema"],"backgroundTag":"schema-validation-failed","analyzedSha":"433685b2024e7bc4c99e1e2e326bcad39b4d9d65","analyzedAt":"2026-09-15T12:24:24.634Z","contentChangedAt":"2026-09-15T12:24:24.634Z","schemaVersion":2},"datasetVersion":"2026-09-22T11:17:16.035Z"}