{"record":{"id":"e5c50081436c90da","repo":"Hmbown/CodeWhale","slug":"saved-expression-version-does-not-match-its-checkpoint","errorCode":null,"errorMessage":"Saved expression version does not match its checkpoint.","messagePattern":"Saved expression version does not match its checkpoint\\.","errorType":"exception","errorClass":"Error","httpStatus":null,"severity":"error","filePath":"pet/src/ui/pet.ts","lineNumber":288,"sourceCode":"      world.step(1 / 30, { motion: !motion.checked, sensitivity: 1 }); seconds = world.frame.timeMs / 1000; play(world.voices); accumulator -= 1 / 30;\n    }\n    if ((mode.value === 'live' || !tape.length) && seconds >= duration()) seek.max = String(Math.ceil(seconds / 30) * 30 + 30);\n    draw();\n  }\n  last = now; requestAnimationFrame(animate);\n}\ntry {\n  const response = await fetch('./whale-points.tsv'); if (!response.ok) throw new Error('Whale point asset is unavailable.');\n  points = (await response.text()).trim().split('\\n').map(row => row.trim().split(/\\s+/).map(Number) as [number, number]);\n  if (points.length !== 980 || points.some(p => p.length !== 2 || !p.every(Number.isFinite))) throw new Error('Invalid whale point asset.');\n  try {\n    const saved = await library.getHabitat();\n    if (saved) {\n      savedRevision = saved.revision; const h = saved.habitat;\n      if (h.petPersistenceVersion !== 1 || !Number.isFinite(h.seconds) || h.seconds < 0 || h.seconds > PET_MAX_SECONDS\n        || !['wild', 'demo', 'replay'].includes(h.source) || typeof h.still !== 'boolean' || typeof h.sourceName !== 'string') throw new Error('Saved habitat is invalid. Save a replay file before replacing it.');\n      expressionVersion = h.expressionVersion === undefined ? 1 : h.expressionVersion;\n      if (![1, 2].includes(expressionVersion) || h.checkpoint && (h.checkpoint.sim?.expressionVersion ?? 1) !== expressionVersion) throw new Error('Saved expression version does not match its checkpoint.');\n      const savedWorld = PetWorld.fromRecording(points, { ...h, petReplayVersion: h.petReplayVersion === undefined ? 1 : h.petReplayVersion });\n      if (h.checkpoint && savedWorld.frame.timeMs / 1000 !== h.seconds) throw new Error('Saved pet clock does not match its checkpoint.');\n      tape = h.tape; interactions = [...h.interactions]; mode.value = h.source; motion.checked = h.still || media.matches;\n      source.textContent = h.sourceName;\n      seek.max = String(Math.max(h.source === 'demo' ? 80 : h.source === 'replay' ? 90 : 120, savedWorld.endTimeMs / 1000));\n      if (h.source === 'replay') {\n        mode.querySelector<HTMLOptionElement>('[value=\"replay\"]')!.disabled = false;\n      }\n      message.textContent = 'Restoring the saved habitat…';\n      if (h.checkpoint && h.still === motion.checked) adoptWorld(savedWorld);\n      else await rebuild(h.seconds, undefined, savedWorld.recording(false).start);\n      message.textContent = 'Habitat restored locally. Sound starts only when you enable it.';\n    } else await rebuild();\n    persistenceReady = true; void refreshArchives().catch(() => {});\n  } catch (error) { persistenceFailed = true; get('persistence').textContent = error instanceof Error ? error.message : 'Local persistence is unavailable.'; await rebuild(); }\n  void refreshArchives().catch(() => {});\n  setInterval(() => { void persist(); }, 5000); requestAnimationFrame(animate);\n} catch (error) { message.textContent = error instanceof Error ? error.message : 'Unable to start the habitat.'; }","sourceCodeStart":270,"sourceCodeEnd":306,"githubUrl":"https://github.com/Hmbown/CodeWhale/blob/433685b2024e7bc4c99e1e2e326bcad39b4d9d65/pet/src/ui/pet.ts#L270-L306","documentation":"Thrown when the restored habitat's `expressionVersion` is not 1 or 2, or when a checkpoint exists whose `sim.expressionVersion` (defaulting to 1) does not match the habitat-level `expressionVersion`. The expression state and its checkpoint must agree for a safe restore.","triggerScenarios":"A habitat saved by code that writes `expressionVersion` without updating the checkpoint's `sim.expressionVersion`, or vice versa; a value outside {1, 2}; an undefined checkpoint expression on a v2 habitat without the `?? 1` default applying.","commonSituations":"Partial migration between expression schema versions, concurrent writers (two tabs with different versions), hand-repaired saves.","solutions":["Re-save the habitat so both `expressionVersion` and `checkpoint.sim.expressionVersion` are written consistently.","Set `checkpoint.sim.expressionVersion` to match the habitat's `expressionVersion` when migrating checkpoints.","Downgrade/upgrade the saved record to expressionVersion 1 or 2; clear the habitat if unrecoverable."],"exampleFix":"// before\nh.expressionVersion = 2; // checkpoint still says 1\n// after\nh.expressionVersion = 2;\nh.checkpoint.sim.expressionVersion = 2;","handlingStrategy":"validation","validationCode":"if (![1,2].includes(h.expressionVersion) || (h.checkpoint && (h.checkpoint.sim?.expressionVersion ?? 1) !== h.expressionVersion)) { resaveHabitat(); return; }","typeGuard":"const versionsMatch = (h: any) => [1,2].includes(h.expressionVersion) && (!h.checkpoint || (h.checkpoint.sim?.expressionVersion ?? 1) === h.expressionVersion);","tryCatchPattern":"try { restoreHabitat(saved); } catch (e) { if (e.message.includes('expression version')) resetExpressionState(); }","preventionTips":["Write habitat.expressionVersion and checkpoint.sim.expressionVersion in the same transaction.","Default both to 1 when migrating older saves.","Test upgrades from every supported expressionVersion."],"tags":["persistence","versioning","indexeddb"],"backgroundTag":"incompatible-source-type","analyzedSha":"433685b2024e7bc4c99e1e2e326bcad39b4d9d65","analyzedAt":"2026-09-15T12:24:24.634Z","contentChangedAt":"2026-09-15T12:24:24.634Z","schemaVersion":2},"datasetVersion":"2026-09-23T08:17:48.524Z"}