{
  "nbformat": 4,
  "nbformat_minor": 5,
  "metadata": {
    "kernelspec": {
      "display_name": "Python 3",
      "language": "python",
      "name": "python3"
    },
    "language_info": {
      "name": "python",
      "version": "3.12.14"
    },
    "execution": {
      "method": "Python subprocess, exact code cell source",
      "deterministic_replay_passed": true
    },
    "runtime": {
      "packages": [],
      "local_assets": []
    }
  },
  "cells": [
    {
      "cell_type": "markdown",
      "id": "question",
      "metadata": {},
      "source": [
        "# A fixed seed is not a reset contract\n",
        "A cached choice survives a partial reset, changes every starting fingerprint, and breaks half the repeated tasks.\n",
        "\nPure-Python reset-contract test; no browser session, backend, or BrowserGym benchmark is run.\n"
      ]
    },
    {
      "cell_type": "code",
      "id": "experiment",
      "metadata": {},
      "execution_count": 1,
      "source": [
        "\"\"\"A fixed seed is not a reset contract\n",
        "Deterministic, standard-library experiment. RESULTS contains the full data.\n",
        "\"\"\"\n",
        "import hashlib\n",
        "import json\n",
        "\n",
        "def fresh(seed):\n",
        "    return dict(seed=seed, target=seed % 2, cookies={}, visits=0, cache=None)\n",
        "\n",
        "def fingerprint(state):\n",
        "    raw = json.dumps(state, sort_keys=True, separators=(',', ':')).encode()\n",
        "    return hashlib.sha256(raw).hexdigest()\n",
        "\n",
        "def run(state):\n",
        "    choice = state['cache'] if state['cache'] is not None else state['target']\n",
        "    state['cache'] = choice\n",
        "    state['visits'] += 1\n",
        "    return choice == state['target'] and state['visits'] == 1\n",
        "\n",
        "for mode in ('partial', 'complete'):\n",
        "    identical = successes = 0\n",
        "    for seed in range(10):\n",
        "        state = fresh(seed)\n",
        "        initial = fingerprint(state)\n",
        "        assert run(state)\n",
        "        state['cookies']['previous_run'] = True\n",
        "        state['visits'] += 7\n",
        "        if seed % 2:\n",
        "            state['cache'] = 1 - state['target']\n",
        "        if mode == 'complete':\n",
        "            state = fresh(seed)\n",
        "        else:\n",
        "            state['cookies'] = {}\n",
        "            state['visits'] = 0\n",
        "        identical += fingerprint(state) == initial\n",
        "        successes += run(state)\n",
        "    print(mode, 'identical_initial_states', identical, 'repeat_successes', successes)\n",
        "    assert (identical, successes) == ((10, 10) if mode == 'complete' else (0, 5))\n",
        "\n",
        "\n",
        "records=[]\n",
        "for mode in ('partial','complete'):\n",
        "    for seed in range(10):\n",
        "        state=fresh(seed); initial=fingerprint(state); run(state)\n",
        "        state['cookies']['previous_run']=True; state['visits']+=7\n",
        "        if seed%2: state['cache']=1-state['target']\n",
        "        contaminated=json.loads(json.dumps(state))\n",
        "        if mode=='complete': state=fresh(seed)\n",
        "        else: state['cookies']={}; state['visits']=0\n",
        "        before=json.loads(json.dumps(state)); repeated=fingerprint(state)\n",
        "        changes=[key for key in state if state[key]!=fresh(seed)[key]]\n",
        "        success=run(state)\n",
        "        records.append(dict(mode=mode,seed=seed,initial_hash=initial,repeat_hash=repeated,identical=initial==repeated,contaminated=contaminated,after_reset=before,different_fields=changes,success=success))\n",
        "RESULTS=dict(repeats=records,contract_fields=sorted(fresh(0)),contract_version=1)\n",
        "assert all(r['different_fields']==['cache'] for r in records if r['mode']=='partial')\n",
        "print('All partial-reset mismatches isolated to: cache')\n"
      ],
      "outputs": [
        {
          "output_type": "stream",
          "name": "stdout",
          "text": [
            "partial identical_initial_states 0 repeat_successes 5\n",
            "complete identical_initial_states 10 repeat_successes 10\n",
            "All partial-reset mismatches isolated to: cache\n"
          ]
        }
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {},
      "outputs": [],
      "source": [
        "from pathlib import Path\n",
        "import base64\n",
        "print('__NOTEBOOK_IMAGE__svg+xml:' + base64.b64encode(Path('figure.svg').read_bytes()).decode())\n"
      ]
    }
  ]
}
