|
| 1 | +# CLAUDE.md |
| 2 | + |
| 3 | +## Rules |
| 4 | + |
| 5 | +- **Commits**: No `Co-Authored-By` lines. Single-line messages (no body). Same convention as |
| 6 | + OpenOutFind, OpenOutSend and OpenOutLearn. |
| 7 | +- **Always work on `main`.** Never create or switch to a feature branch. |
| 8 | +- **Every green push to `main` is a release.** `.github/workflows/deploy.yml` runs the tests, then |
| 9 | + derives the version from the commit count and publishes to PyPI via trusted publishing |
| 10 | + (environment `pypi`). `needs: test` is the whole gate. |
| 11 | +- **No memory**: never use the auto-memory system. |
| 12 | +- **No roadmap here.** Work is tracked in `openoutreach-docs/roadmap/AgenticFSM/`. The design, |
| 13 | + the milestones and what was weighed and not taken are in |
| 14 | + `p1-e3-agentic-fsm-first-version.md` there. Read it before changing the interface. |
| 15 | +- **This is a general-purpose library.** Nothing from OpenOutreach's domain (deals, leads, |
| 16 | + batches) goes in the code, the docstrings, the README or the tests. The example everywhere is |
| 17 | + a shop `Order`. A machine for a real domain lives in the repo that uses the library, as models |
| 18 | + live in a Django project and not in Django. |
| 19 | +- **The core imports no database and no framework.** `states.py` and `machine.py` use the |
| 20 | + standard library only. Anything a backend needs is imported inside that backend's package and |
| 21 | + is an optional extra in `pyproject.toml`. |
| 22 | + |
| 23 | +## Project Overview |
| 24 | + |
| 25 | +`agentic_fsm` keeps entity lifecycles: many rows that each sit in a state for days and are moved |
| 26 | +by whoever knows something new. A machine is three plain Python things. |
| 27 | + |
| 28 | +- **States** (`states.py`): declared like Django model fields, `pending = InitialState("...")`. |
| 29 | + `_StatesMeta` replaces each declaration with its stored value before the enum is built, so the |
| 30 | + class is a real `str` enum (`Order.paid == "paid"`, usable as a Pydantic type) whose members |
| 31 | + keep their description and kind. The description is required; it's the state's definition. |
| 32 | +- **Steps**: a named function of the entity that returns the next state, or `None` for "not now". |
| 33 | +- **Transitions**: one list of `(from, to, step)` rows; `from` may be a tuple. A step's allowed |
| 34 | + targets out of a state are the rows it sits on there. |
| 35 | + |
| 36 | +`Machine(states, rows, load=, backend=, name=)` (`machine.py`) checks the declaration and raises |
| 37 | +`MachineError` naming the state or row. Then: |
| 38 | + |
| 39 | +| Call | Does | |
| 40 | +|---|---| |
| 41 | +| `start(id)` | puts an entity in the initial state; `AlreadyStarted` if it has one | |
| 42 | +| `fire(id, step)` | runs one step (the function or its name); returns the logged `Move`, or `None` for "not now" | |
| 43 | +| `advance()` | per unfinished entity, tries the steps out of its state in list order and stops at the first that doesn't return `None`: at most one move per entity per call | |
| 44 | +| `state_of(id)`, `in_state(state)`, `history(id)` | reads | |
| 45 | +| `graph()` | states and rows as data | |
| 46 | + |
| 47 | +A result outside the step's rows, or one that isn't a state, is logged as a refused `Move` |
| 48 | +(`applied=False`, with a `reason`) and never applied. `fire` raises `TransitionError` when the |
| 49 | +move can't even be tried (unknown step, entity not started, step doesn't leave the state). Entity |
| 50 | +ids are stored as text, so `load` receives a string. The machine never opens a transaction around |
| 51 | +a step: a step may send an email or call a model. |
| 52 | + |
| 53 | +## State backends |
| 54 | + |
| 55 | +Laid out and chosen as Django lays out its database backends: one package per backend, each with |
| 56 | +a `base` module holding a `StateBackend`, picked by the package's dotted path |
| 57 | +(`AGENTIC_FSM_BACKEND=agentic_fsm.backends.django`, or `Machine(..., backend=)`; a machine with |
| 58 | +neither raises). `backends/__init__.py:get_backend` imports `<path>.base` on first use. |
| 59 | + |
| 60 | +- `backends/base/base.py`: `BaseStateBackend` (seven calls: `start`, `move`, `refuse`, `current`, |
| 61 | + `in_state`, `all_current`, `history`), plus the `Move` and `Current` dataclasses. |
| 62 | +- `backends/locmem/`: in memory, for tests. |
| 63 | +- `backends/django/`: a Django app (`INSTALLED_APPS = ["agentic_fsm.backends.django"]`, label |
| 64 | + `agentic_fsm`) with two models, `LogEntry` (append-only) and `CurrentState` (one row per |
| 65 | + entity, with a `version`), and their migration. It's the only place Django is imported. |
| 66 | + |
| 67 | +Every backend keeps the same two things and the same rule: a move rewrites the entity's |
| 68 | +current-state row and appends its log row in one transaction, and only if the entity's version is |
| 69 | +still the one read before the step ran. A move that finds another version is logged as refused. |
| 70 | + |
| 71 | +## Tests |
| 72 | + |
| 73 | +```bash |
| 74 | +uv venv && uv pip install -e ".[dev]" |
| 75 | +pytest |
| 76 | +``` |
| 77 | + |
| 78 | +`tests/conftest.py` holds the example `Order` machine. Its `backend` fixture is parametrized, so |
| 79 | +every test that takes `orders` or `backend` runs once on `locmem` and once on `django` |
| 80 | +(pytest-django, in-memory SQLite, `tests/settings.py`). A new backend is added to that fixture |
| 81 | +and must pass the same tests. After changing a model, write the migration and check |
| 82 | +`DJANGO_SETTINGS_MODULE=tests.settings python -m django makemigrations --check --dry-run`. |
0 commit comments