> Agent-ready spec from Diff of the Day (2026-12-01, idea 3). Give this file to your coding agent (Claude Code, Codex, Cursor) and ask it to build the P0 requirements first.

# Spec: State machines from code to TLA+

## Problem Statement
Code with timers, retries and concurrent writers has state machines that nobody wrote down. Tests try a few orders of events, and the bugs live in the orders nobody tried. Writing a formal model by hand was too slow for most teams; an agent can now draft one from the code.

## Goals
- List the implicit state machines of one service, each with links to the code that changes its state.
- Produce TLA+ models for the three most important ones that the TLC checker runs to completion.
- Turn every confirmed counterexample into a failing test in the repository.

## Non-Goals
- Proving the whole service correct: three models are enough to see if the method pays.
- Fixing the bugs in this spike: a failing test and a note are the output.
- Liveness properties: start with safety invariants, which are easier to state and check.

## User Stories
- As a maintainer, I want a list of the state machines in my service so that I can see what was never designed.
- As a maintainer, I want each counterexample as a short trace in plain words so that I can confirm it against the code.
- Edge case: as a maintainer, I want a counterexample that the real code cannot reach to be marked "model error" with the reason, so that false alarms improve the model and do not become bug reports.

## Requirements
### P0
- An inventory file: each state machine with its states, events and code locations.
  - [ ] Every entry links to at least one file and line.
- A TLA+ module and config for three machines, with at least one invariant each.
  - [ ] TLC runs on each and the output is saved.
- A triage file for the counterexamples.
  - [ ] Each one is marked real, model error or unclear, with a reason.

### P1
- A failing test for each real counterexample.
  - [ ] The test fails on the current code for the reason in the trace.

### P2
- A reusable prompt or skill that repeats the steps on another service.
  - [ ] It runs on a second service without edits.

## Success Metrics
- State machines found, models that check, counterexamples by type.
- Share of counterexamples confirmed as real.
- Failing tests added.

## Open Questions
- Which invariants does the team actually care about?
- How much of the environment (network, clock) must the model include?

## Timeline
One weekend: inventory and first model on day one, two more models and tests on day two.

## Agent Instructions
- Suggested stack: the TLA+ tools (TLC) run locally, your existing test framework, a coding agent with read access to the service.
- Build order: inventory, one model end to end, triage, then the other two models, then tests.
- Safety: read-only access to the code while modelling; write only new files for models, notes and tests; do not connect to production.
- Done when: three models run in TLC and every counterexample has a triage entry.

## Sources
- https://www.linkedin.com/feed/update/urn:li:activity:7533172089583747073/
