Spielerando

Autosave Is On, but a Crash Loses More Than It Should

An autosave promises that nothing the player does stays unsaved for longer than its longest wait. If a crash right after a long save costs more than that, the clock probably started again at zero when the save finished, and forgot the changes made while it was being written. TLC found this in our own autosave blueprint.

What you see

Why does a change wait too long?

A player keeps playing while a save is being written. Our autosave blueprint marked those changes as still unsaved, which is right, because the running save started before them. But when the save finished, it set the clock of the oldest unsaved change back to zero. Those changes had been waiting since they were made, not since the save ended. So a busy player waited the save's whole time and then the whole longest wait on top.

TLC found the path while checking the rule that a change waits no longer than the longest wait unless a save it has to wait for is still running. Z3 then put numbers on the old clock: with a longest wait of 5 seconds, a change waited 10, although the save it waited for ended in time.

How to check your game for it

The test plays a busy player who changes something every frame, with a longest wait of 5 seconds and a save that takes 3. It follows the first change made while that save is being written and measures how long it waits for a save of its own. Two more tests make sure a quick save keeps the same promise and that nothing is written when nothing changed.

The test: fix.test.ts

TypeScript for Node's own test runner. The tests named below as the catch are the ones that fail on the bug.

import assert from "node:assert/strict";
import test from "node:test";
import { fresh, type Saver, tick } from "./fix.ts";

const dt = 0.1;
const longestWait = 5;

/** A busy player who changes something every frame, and writes that take `writeTime` seconds.
    Returns how long the first change made during the first write waited for a write of its own. */
function waitAfterASlowWrite(writeTime: number): number {
  let s: Saver = fresh;
  let frames = 0;
  // until the first write starts
  for (let r = tick(s, true, false, longestWait, dt); ; r = tick(s, true, false, longestWait, dt)) {
    s = r.saver;
    if (r.write || ++frames > 1000) break;
  }
  // the write runs while the player keeps changing things; we follow the change on its first frame
  let r = tick(s, true, false, longestWait, dt);
  let waited = 0;
  for (let i = 1; i < Math.round(writeTime / dt); i++) {
    r = tick(r.saver, true, false, longestWait, dt);
    waited += dt;
  }
  // the write reports that it worked
  r = tick(r.saver, true, true, longestWait, dt);
  waited += dt;
  // until the next write starts
  while (!r.write && waited < 60) {
    r = tick(r.saver, true, false, longestWait, dt);
    waited += dt;
  }
  return waited;
}

test("a change made during a slow write waits no longer than the longest wait", () => {
  const waited = waitAfterASlowWrite(3);
  assert.ok(waited <= longestWait + dt + 1e-9, `the change waited ${waited.toFixed(1)} s with a longest wait of ${longestWait} s`);
});

test("after a quick write, the next one comes within the longest wait", () => {
  const waited = waitAfterASlowWrite(0.1);
  assert.ok(waited <= longestWait + dt + 1e-9, `the change waited ${waited.toFixed(1)} s`);
});

test("nothing changed, nothing written", () => {
  let s = fresh;
  for (let i = 0; i < 200; i++) {
    const r = tick(s, false, false, longestWait, dt);
    assert.equal(r.write, false);
    s = r.saver;
  }
});

The catch:

What it said on the code with the bug put back

✖ a change made during a slow write waits no longer than the longest wait
    the change waited 8.0 s with a longest wait of 5 s
✔ after a quick write, the next one comes within the longest wait
✔ nothing changed, nothing written
3 tests: 2 passed, 1 failed

What it said on the fixed code

✔ a change made during a slow write waits no longer than the longest wait
✔ after a quick write, the next one comes within the longest wait
✔ nothing changed, nothing written
3 tests: 3 passed, 0 failed

Run by pnpm fixes:check on 2026-11-26 with Node v24.15.0. The text above is its output.

The fix

Keep a second clock that starts with the first change made during a save. When the save finishes, the oldest unsaved change is that one, and its age is that second clock, not zero. Then the next save starts on time.

One thing the fix cannot do, and Z3 says so plainly for our blueprint: a crash just before a save finishes also loses that save's own time. At our starting values, a longest wait of 60 seconds and a time limit of 10 for a write, a crash can cost 70 seconds of play, not 60. If your promise to players is a number, count the save's time into it.

What changed

Lines with - are the code with the bug, lines with + the fix.

    if (saving && writeOk) {
      saving = false;
-     unsavedFor = 0;
+     // A change made during the write has waited since it was made, not since the write ended.
+     unsavedFor = changedDuringWrite ? pendingFor : 0;
      dirty = changedDuringWrite;
      changedDuringWrite = false;

The fixed code: fix.ts

/* The longest wait of an autosave: however busy the player is, nothing they do stays unsaved for
   longer than `longestWait` seconds, as long as each write itself is quick. Here the player never
   pauses, so the longest wait is all that starts a write. From the autosave blueprint:
   https://spielerando.com/en/mechanics/autosave */

export interface Saver {
  /** Something changed that the last good write does not hold. */
  dirty: boolean;
  /** A write is running. */
  saving: boolean;
  /** The player changed something after the running write began. */
  changedDuringWrite: boolean;
  /** How long the oldest change the last good write does not hold has waited, s. */
  unsavedFor: number;
  /** How long the first change made during the running write has waited, s. */
  pendingFor: number;
}

export const fresh: Saver = { dirty: false, saving: false, changedDuringWrite: false, unsavedFor: 0, pendingFor: 0 };

/** One frame. `changed`: the player changed something worth saving. `writeOk`: the running write
    reported that it worked. Returns the saver, and `write` on the frame a write should start. */
export function tick(s: Saver, changed: boolean, writeOk: boolean, longestWait: number, dt: number): { saver: Saver; write: boolean } {
  let { dirty, saving, changedDuringWrite } = s;
  let unsavedFor = dirty ? s.unsavedFor + dt : 0;
  let pendingFor = changedDuringWrite ? s.pendingFor + dt : 0;
  if (saving && writeOk) {
    saving = false;
    // A change made during the write has waited since it was made, not since the write ended.
    unsavedFor = changedDuringWrite ? pendingFor : 0;
    dirty = changedDuringWrite;
    changedDuringWrite = false;
  }
  if (changed) {
    if (!dirty) unsavedFor = 0;
    dirty = true;
    if (saving && !changedDuringWrite) pendingFor = 0;
    if (saving) changedDuringWrite = true;
  }
  const write = dirty && !saving && unsavedFor >= longestWait - 1e-9;
  if (write) {
    saving = true;
    changedDuringWrite = false;
  }
  return { saver: { dirty, saving, changedDuringWrite, unsavedFor, pendingFor }, write };
}

Paste this to your AI

My game has an autosave that should never leave anything unsaved for longer than a set time. After a crash, players sometimes lose more than that, usually what they did while a save was being written. I think the clock of the oldest unsaved change starts at zero again when a save finishes.

1. Before you change anything, write down the states of my autosave (nothing to save, waiting, writing, retrying) and what moves it between them. Write down which clocks it keeps and when each one starts and stops. Next to it write the rule: while the player keeps playing and saves work, no change waits longer than the longest wait, unless a save it has to wait for is itself still running.
2. Write a test like the one below for my code: a player who changes something every frame, a save that takes 3 seconds, and a longest wait of 5. Measure how long the first change made during the save waits for a save of its own. Run it and paste me the real output.
3. If it waits too long, fix it the way the code below does: keep a second clock for the first change made during a save, and when the save finishes, the oldest unsaved change is that one, with its own age. Run the test again and paste the output.
4. Let the clock start at zero at the end of a save again for a moment, show me the test failing, and undo it.

Do not tell me it is fixed unless the test output shows it.

The button copies this text together with the fixed code and the test above, so your AI has everything.

Where we found it

The blueprint the bug was in, and the test that guards it there:

How we found them: the bugs our proofs found in our own code

Was this page helpful?

One click, no account. It shows us which pages help.

Next step

Fix by symptom

The Quest Is Done Before the Player Did Anything

A quest that counts as done the moment it starts usually has an empty list of goals: "every goal is met" says yes to an empty list. The test and the fix.

Last updated: 2026-11-26