fuzzyworld

dm / commit

feat: add save version — discard sessions with mismatched format

bfd83c711a037de1e46696c83c3281337b6ed85f · Pavel Safronov · 2026-05-27 21:47:20 -0700 · parents 0a0dca6f5bf4

feat: add save version — discard sessions with mismatched format

SAVE_VERSION = 1. tryResumeSession drops any saved session whose
version field differs, forcing a fresh start. Increment SAVE_VERSION
on every future breaking change to gameState shape or session format.

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
 public/index.html | 10 ++++++++++
 1 file changed, 10 insertions(+)

diff --git a/public/index.html b/public/index.html
index 44c3bf3..7f28f9f 100644
--- a/public/index.html
+++ b/public/index.html
@@ -1597,12 +1597,16 @@ function buildDebugInfo(userDesc) {
 }
 
 // ── SESSION PERSISTENCE ───────────────────────────────
+// Increment SAVE_VERSION on any breaking change to gameState shape or
+// session format. tryResumeSession discards saves from older versions.
+const SAVE_VERSION = 1;
 
 function saveSession() {
   if (!gameState) return;
   const { apiKey: _, ...stateToSave } = gameState;
   try {
     localStorage.setItem('dm_session', JSON.stringify({
+      version: SAVE_VERSION,
       gameState: stateToSave,
       conversationHistory,
       messagesHTML: document.getElementById('messages').innerHTML,
@@ -1622,6 +1626,12 @@ async function tryResumeSession() {
   let session;
   try { session = JSON.parse(sessionRaw); } catch { return false; }
 
+  if (session.version !== SAVE_VERSION) {
+    console.log(`Save version mismatch (saved ${session.version ?? 'none'}, current ${SAVE_VERSION}) — discarding old session.`);
+    localStorage.removeItem('dm_session');
+    return false;
+  }
+
   gameState = { ...session.gameState, apiKey: currentApiKey };
   conversationHistory = session.conversationHistory || [];