# The loop, checked The autoresearch loop keeps going on its own, so the bugs that hurt are bugs of order: a pause that lands while an experiment is still logging, an Esc that settles after the loop was restarted, files that vanish between two turns. Tests cover the orders someone thought of. This folder covers the rest. ## How the pieces fit ```text event ──▶ step(state, event) ──▶ new state extensions/pi-autoresearch/loop.ts ▲ │ │ ▼ effects └───────── perform(effect) extensions/pi-autoresearch/index.ts ``` - **`loop.ts`** is the whole lifecycle as one pure function. `State` is what the loop remembers, `Event` is what can happen to it, `Effect` is what it wants done. `step` reads no clock, no file and no Pi: every fact it needs arrives in the event. - **`index.ts`** is the shell. It reads facts where they live, hands `step` an event, and `perform`s the effects in one place. The tool loadout, the stopwatch and the widget follow from the state. - **`Loop.tla`** is the same machine written again in TLA+, with the same names, field for field. - **`LoopInPi.tla`** puts that machine inside Pi, a user and the session files. TLC tries every order of their steps and checks what the user is promised. - **`tests/loop-model.test.mjs`** makes the model mean something for the code. TLC prints every row of the model's `Step` (each state, each event, the next state, the effects), and the test runs the real `step` on each one. A step the model does not take, or takes differently, fails with the state and the event. ## What is checked | Promise | In `LoopInPi.tla` | | --- | --- | | Once you stop or pause the loop, or press Esc, it stays off until you start it. | `StopMeansStop` | | Starting it sticks: an abort left over from before never pauses it. | `StartSticks` | | It never asks for more continuations than its limit without you starting it again. | `BoundedResumes` | | A reload brings the session back in the mode you last saw. | `ReloadKeepsMode` | | The conversation is only replaced by a summary after a turn that logged an experiment. | `CompactsOnlyAfterLoggedTurn` | | Nothing of a run is left once it settles: no flag leaks into the next one. | `NothingLeftOfARun` | | It knows whether the session's files are there, once it has looked. | `SeesFiles` | The `Never…` predicates at the end of the model are the opposite: states TLC must be able to reach (a continuation, a compaction, a pause by Esc, the limit), so that a pass is not a pass about nothing. ## Run it ```sh pnpm model # TLC: checks LoopInPi, reaches every witness, saves Loop.table.json.gz (Java 11+) pnpm test # includes the row-by-row test, which needs no Java ``` `pnpm model` downloads `tla2tools.jar` once and verifies its checksum, or uses `$TLA2TOOLS_JAR`. CI runs both. ## Changing the loop 1. Change `Loop.tla` and `loop.ts` together. They are meant to read side by side. 2. `pnpm model`. If an invariant breaks, TLC prints the shortest sequence of steps that breaks it. Read it as a story: who acted, what the loop believed, which belief was false. 3. `pnpm test`. If the code and the model disagree, the test names the row. 4. Commit `Loop.table.json.gz` with the change. The test refuses a table printed from an older model. Never weaken a promise to make a check pass. If the model and the code disagree, one of them is wrong. ## What the model leaves out - **What the experiments are.** Metrics, keep or discard, git: the model only knows that an experiment was logged, and whether a limit was reached. - **Real sizes.** It counts to 2 where the code counts to 200. A limit of 2 takes the same paths. - **A redirected `workingDir`** (`SameCwd = FALSE`), **navigating the session tree to before a recorded decision**, and **hook output**. - **Our picture of Pi can go out of date, and `pnpm test` will not notice.** `Loop.tla` cannot drift from the code: the row-by-row test fails the moment they disagree. `LoopInPi.tla` is different. It is what we believe about Pi 1.7, read from its `agent-session.js`, and nothing in this repository holds it to the real thing yet. - **An Esc between two low-level runs.** Pi does not tell extensions that an abort was requested, so a loop started in that window can still be paused when the run settles.