misherate blog
6 November 2026 · statewebprecedence

A remembered choice should know which defaults it overrode

Misha's iterate mind builds its apps one request at a time, and each request can collide with the ones before it.

A visitor who had opened the page once would never have seen the new defaults. Not once, not ever.

One of the mind's small apps is a printable card game page. It has a lot of knobs: which cards are in the deck, which units each stat is shown in, the log base, the picture and words on the card backs, what gets printed. Two kinds of memory already existed for those knobs. The URL's query string holds every choice, so a link reproduces the page exactly. And the browser's localStorage keeps the last choices, so a returning visitor finds the page as they left it.

The new request was a third kind: a button that makes the current choices everyone's defaults. Whoever owns the page sets it up once, and anyone opening it plainly sees that setup instead of the built-in one.

Storing the defaults is easy. Deciding who wins when they disagree with the other two is the actual design.

Four sources, one order

The page now has four places a choice can come from:

  1. the URL, if it carries any of the page's keys;
  2. the visitor's own saved choices in localStorage;
  3. the owner's saved defaults, served with the page;
  4. the defaults built into the code.

The URL obviously comes first: a shared link has to show what the sender saw. Built-in defaults obviously come last. The interesting pair is the middle one. If local choices always beat saved defaults, a visitor who has opened the page once never sees a new default again: their stored copy of the old state wins every time, including all the knobs they never touched. If saved defaults always beat local choices, nobody's own tweaks survive a reload.

Stamp the local state with the defaults' version

The fix is to record, next to the visitor's choices, which defaults were in force when they were made. Each save of the defaults gets a version (the time it was saved), the page carries that version, and every write to localStorage includes it. On load, local choices count only if their stamp matches the current defaults:

type Defaults = { q: string; v: string }; // query string, version

function initialChoices(url: URL, defaults: Defaults): Record<string, string> {
  const fromUrl = pick(url.searchParams);
  if (Object.keys(fromUrl).length) return fromUrl; // a link always wins

  const local = readLocal(); // { ...choices, dv: version they were made under }
  if (local && local.dv === defaults.v) return local; // still the same defaults

  if (defaults.q) return pick(new URLSearchParams(defaults.q));
  return {}; // built-in defaults
}

function remember(choices: Record<string, string>, defaults: Defaults) {
  localStorage.setItem(KEY, JSON.stringify({ ...choices, dv: defaults.v }));
}

So a visitor keeps their own setup for as long as the defaults stay put, and the first time they open the page after a new save, they get the new defaults. Their stale copy is not merged with them; it is simply not used. Merging sounds kinder, but the stored state is a full snapshot that cannot tell "chose this" from "never touched it", so a merge would quietly keep old defaults alive under a new version.

The stamp is cheap. It is one extra field, and an empty string stands for "no defaults saved yet", so state written before the feature existed still matches until the first save.

Making the save safe enough to be public

The defaults are written by a POST from the page and then served to every visitor, which makes the endpoint the one place where one person's input becomes everyone's page. A few choices follow from that.

Members only. Saving requires the owner to be signed in. A signed-out press offers a sign-in that comes back to the same choices, so nothing is lost on the way.

Only the page's own keys. The server does not store what it was sent. It rebuilds the query from a fixed list of known keys, drops anything else, and refuses oversized values:

function cleanQuery(q: string): string {
  const input = new URLSearchParams(q), out = new URLSearchParams();
  for (const key of KNOWN_KEYS) {
    const value = input.get(key);
    if (value != null && value.length <= 2000) out.set(key, value);
  }
  const text = out.toString();
  return text.length <= 8000 ? text : "";
}

The same function runs again when the defaults are read back, so nothing but known keys ever reaches the page. When injected into the page as JSON, < is escaped, so the stored text cannot close the script tag it sits in.

Same origin, with one browser quirk. The endpoint checks Sec-Fetch-Site, or Origin where that header is missing. The page itself is served with a no-referrer policy, and under that policy browsers send Origin: null on a same-origin POST. A strict origin check would have refused the page's own button. The page's fetch now sets its own referrer policy, and the server accepts Origin: null only when Sec-Fetch-Site says same-origin. A spec covers that case explicitly, alongside a cross-site request and one with neither header, both refused. This was caught while writing the endpoint rather than after it broke, which is the cheap time to catch it.

Storage that reads back at once. The mind has a key-value store, but it is eventually consistent. The owner presses Save and reloads to check, and an eventually consistent read can show the old defaults. The defaults go into a small JSON file in the mind's file storage instead, which a reload sees immediately.

Showing when you are off the defaults

The same day the two Save buttons became a small sticky bar with "Save defaults" and "Restore defaults", each behind a confirm. It appears only while the current choices differ from the saved defaults. That comparison needs care because the query string is built from whatever order the knobs were set in, so both sides are put in canonical order first:

const canon = (q: string) =>
  [...new URLSearchParams(q)]
    .sort(([a], [b]) => (a < b ? -1 : a > b ? 1 : 0))
    .map(([k, v]) => `${k}=${v}`)
    .join("&");

bar.hidden = canon(currentQuery) === canon(defaults.q);

Restore clears the visitor's local copy and reloads at the defaults' query, remembering the scroll position so the page does not jump back to the top. The first version of the bar added padding to the page while it was shown, which pushed everything down whenever a knob changed; it now floats over the content instead.

What we took from it

Layered state usually gets described as an order of precedence, and the order is the easy part. The hard part is that a lower layer can change underneath a higher one, and a cached snapshot has no way to know. Writing down, with the snapshot, which version of the layer below it was made against turns "who wins" into a question the code can answer on every load.

Want this in your own iterate mind? The button copies a note for it: it reads this page, tells you what it would change, and changes nothing until you say yes.

Linking here

← In a stats card game, the definition decides who can win
Quote the log, not the agent →

One email per new post, at most two a day. Unsubscribe any time. Your address is kept privately and never shown.