- Commits: No
Co-Authored-Bylines. Single-line messages (no body). Same convention as OpenOutFind, OpenOutSend and OpenOutLearn. - Always work on
main. Never create or switch to a feature branch. - Every green push to
mainis a release..github/workflows/deploy.ymlruns the tests, then derives the version from the commit count and publishes to PyPI via trusted publishing (environmentpypi).needs: testis the whole gate. - No memory: never use the auto-memory system.
- No roadmap here. Work is tracked in
openoutreach-docs/roadmap/AgenticFSM/. The design, the milestones and what was weighed and not taken are inp1-e3-agentic-fsm-first-version.mdthere. Read it before changing the interface. - This is a general-purpose library. Nothing from OpenOutreach's domain (deals, leads,
batches) goes in the code, the docstrings, the README or the tests. The example everywhere is
a shop
Order. A machine for a real domain lives in the repo that uses the library, as models live in a Django project and not in Django. - The core imports no database. Anything a state backend needs (Django, a driver) is
imported inside that backend's package and is an optional extra in
pyproject.toml. Pydantic AI is the library's one dependency: a model deciding a move is part of the core, not an extra. - Every default is a setting. A default value is declared in
conf/global_settings.pyand read assettings.NAME, never written where it's used and never read fromos.environthere.
agentic_fsm keeps entity lifecycles: many rows that each sit in a state for days and are moved
by whoever knows something new. A machine is three plain Python things.
- States (
states.py): declared like Django model fields,pending = InitialState("...")._StatesMetareplaces each declaration with its stored value before the enum is built, so the class is a realstrenum (Order.paid == "paid", usable as a Pydantic type) whose members keep their description, kind, tags and handler. The description is required; it's the state's definition.handler=is the one handler that decides where an entity goes from the state (States.handler); a final state has none.tags=makes a state a queue (see Narrowing a run below). - Handlers (
handlers.py): a state has exactly one, and it has no name: the state identifies it.Handleris the abstract base, withrun(entity, targets)and three attributes the machine reads:reads_only(duemay run it),decided_by(who its moves are logged under, defaultcode) andeffect(run with the result before the move is written). A plain function of the entity is wrapped into aFunctionHandlerbyas_handlerwhere the state is declared (State.__init__), so the machine only ever callshandler.run.runreturns the next state (or aDecision),Nonefor "not now", or anotherHandlerto hand over to, which the machine then runs with the same targets (Machine._try); aDecideis one (see Decide).@checkmarks a function that only reads: it sets an attribute on the function, whichFunctionHandlerreads intoreads_only, and refuses aHandlerobject. - Edges: one list of
(from, to)pairs;frommay be a tuple. A state's handler may return only the states its state has an edge to (Machine._targets).
Machine(states, edges, load=, backend=, name=) (machine.py) checks the declaration and raises
MachineError naming the state or edge: one initial state, a handler on every state but a final
one and none on those, no edges out of a final state, no other state without one, every state
reachable. Then:
| Call | Does |
|---|---|
start(id) |
puts an entity in the initial state; AlreadyStarted if it has one |
start_all(ids) |
starts each id with no state yet (one all_current read, then a start each; a rival's AlreadyStarted is skipped), for entities another program makes |
fire(id) |
runs the handler of the entity's state, and each handler it hands over to; returns the logged Move, or None for "not now" |
fire(id, entity=) |
the same, on the entity already in hand instead of load(id) (brief takes it too) |
fire(id, answer=) |
decides in a Decide's place; no model call, the prompt doesn't run, its effect does |
brief(id) |
the Brief a Decide would give the model: state, instructions, prompt, choices, schema, tools |
progress(id) |
moves one entity until it rests: each round runs its state's handler on the entity loaded afresh, and commits its move; it stops when the handler doesn't move it, or after a move back to a state this call already left |
progress_all() |
per unfinished entity, runs its state's handler: at most one move per entity per call, each entity locked and committed on its own. An entity that raises is recorded and the rest tried; then ProgressError(moves, failures) |
due() |
the unfinished entities their state's handler could move now, writing nothing and taking no lock: a handler that only reads runs (and so does one it hands over to, if that only reads too); any other handler makes the entity due untried (_would_move) |
state_of(id), in_state(state), history(id) |
reads |
graph() |
states (each with its handler's label) and edges as data |
on_move(listener) |
adds a listener, called with the Move after every move and start; returns it, so it decorates |
Narrowing a run. due and progress_all take three narrowings, and the CLI passes the
first two as --tag and --limit:
tag: only the entities in a state carrying the tag, so one worker does one kind of work (Machine._tagged; a tag no state carries raisesMachineError).limit=(progress_allonly): stops after that many moves, oldest entity first; an entity that doesn't move or raises doesn't count.entities=: only those ids, oldest first (Machine._waiting), for a caller whose own data says which entities could move.
A listener is told once the move is committed. start and _apply hand the move to
_announce, which gives the backend's on_commit a callback: outside a lock it runs at once,
inside one when the outermost lock's writes commit, and never if they are undone (a savepoint
that rolls back drops its own). _tell calls the listeners in the order added; one that raises
is logged on agentic_fsm.machine and nothing is raised, since the move happened. A move
written straight through a backend (backend.move) tells no one. tests/test_on_move.py runs
the Django backend with transaction=True: in pytest-django's usual test transaction nothing
commits.
Every row in the log is a move that happened. fire raises TransitionError and writes
nothing when the move can't be made: an entity not started or in a final state, a result outside
the state's edges or not a state at all (a bug in the handler), an answer that doesn't validate or
that the state's code leaves no room for. A log row names no handler: its from_state says
which handler ran (a start has none) and decided_by who decided. Entity ids are stored as text,
so load receives a string.
An answer is taken at the end. fire(answer=) and brief find the Decide with
Machine._decide_for: the state's handler if it is one, else the handler is run and must hand over
to one. It is run only if it only reads (@check), since it runs again on every brief and
answer; one that doesn't raises TransitionError untried, and so does one that returns a state
("moves it itself") or None ("not now"). An answer never overrides what the code settles.
The entity is locked from read to write. fire (and each round of progress and
progress_all) runs inside the backend's lock(machine, entity): it reads the current state,
runs the handler and writes the move there, so a second caller waits and reads the state the first
left. A handler that raises undoes the move and, on the Django backend, the handler's own writes.
decide.py holds Decision (a Pydantic model whose one field is to), Decide(prompt=, agent= | schema=, effect=), a Handler, and Brief. The Agent is Pydantic AI's and
is the whole model-side definition; without one, Decide builds an agent for the DECIDE_MODEL
setting with the schema as its output type, with the model check deferred so no key is needed
until it runs. A Decide is a state's handler (handler=Decide(...)) or what a state's function
returns to hand over. A schema leaves to typed as States: a Decide given in a class body
can't name the class being built, and to is narrowed when it runs.
- Run:
Decide.run(entity, targets)calls the prompt function (falsy: "not now", no call), thenask(prompt, entity, <narrowed schema>), which isagent.run_sync(prompt, deps=entity, output_type=...); a subclass that calls its model another way overridesaskanddecided_by. The narrowed schema is a copy whosetois aLiteralof the state's targets, with each state's description in the field's description. The machine checks the result against the state's edges afterwards either way. A model that keeps answering outside them raises Pydantic AI'sUnexpectedModelBehavior, and nothing is logged. ADecidea function hands over to is offered every target of the state, those only the function's facts lead to included: the states' descriptions are what tell the model which is which. - Answer:
Decide.parse(answer, targets)validates a dict, a JSON object or a bare state value against the same narrowed schema. The machine raisesTransitionErroron an invalid one. - Effect:
Decide(effect=)is a function of the entity and the decision.Machine._applyruns the deciding handler'seffectafter the target is checked against the edges and beforebackend.move, for a run and for an answer alike (_answerloads the entity only when the handler has one). One that raises leaves nothing written. - Logged:
decided_byisllm:<provider>:<model>for a run,agentfor an answer andcodefor a function, andMove.decisionholds the wholeDecision. Brief.instructionsandBrief.toolsread the agent's plain-text instructions (a private attribute ofAgent) and its function tools.tests/test_decide.pyfails if Pydantic AI moves either.
Tests use Pydantic AI's TestModel; models.ALLOW_MODEL_REQUESTS = False keeps any test from
reaching a real model.
cli.py is the console script agfsm (main(argv) returns the exit code), on argparse and
nothing else. It adds no behaviour: each command loads the machine (--machine package.module:name, or the MACHINE setting) and calls one Machine method, show →
graph(), start, list → in_state, due, fire → fire / brief (--agent) /
fire(answer=) (--answer), progress, progress_all (on ProgressError: the moves on
stdout, each failure on stderr, exit 2). Each command returns a Result (the
data for --json, the text, the exit code: 0 a move made or "not now", 2 one that can't be made,
with the reason on stderr).
- An entity is
<machine name>:<id>, checked against the loaded machine; a state given tolistis its stored value. --machineand--jsongo before or after the command: the subcommands' copies default toargparse.SUPPRESSso they don't erase one given before.- The working directory goes on
sys.path, aspython -mwould put it. - With
DJANGO_SETTINGS_MODULEset, and Django and that settings module both installed (find_spec),backends/django/project.py:setup_projectruns before the machine's module is imported, since that module usually imports the project's models. A variable set for another program in a shared environment is left alone. - Pydantic AI's first-run banner goes to stderr, and never for a run an installed package
starts, so
--jsonoutput stays clean.
tests/test_cli.py drives main(argv) on both backends with machines it puts in
tests/machines.py, the module --machine tests.machines:orders names.
conf/global_settings.py lists every setting with its default (BACKEND, DECIDE_MODEL,
MACHINE, SQLALCHEMY_URL), and
conf/__init__.py holds settings, as Django's django.conf does. A read takes the first of:
settings.configure(...), the env var AGENTIC_FSM_<NAME>, the module named by
AGENTIC_FSM_SETTINGS_MODULE, the default. Nothing is cached, so import order doesn't matter. A
name global_settings doesn't declare raises.
Laid out and chosen as Django lays out its database backends: one package per backend, each with
a base module holding a StateBackend, picked by the package's dotted path (the BACKEND
setting, e.g. AGENTIC_FSM_BACKEND=agentic_fsm.backends.django, or Machine(..., backend=); a
machine with neither raises). backends/__init__.py:get_backend imports <path>.base on first use.
backends/base/base.py:BaseStateBackend(eight calls:start,lock,move,on_commit,current,in_state,all_current,history), plus theMoveandCurrentdataclasses.backends/locmem/: in memory, for tests;lockis a per-entityRLock. Nothing is undone there, soon_commitcallbacks run when the thread's outermost lock ends, raised or not.backends/django/: a Django app (INSTALLED_APPS = ["agentic_fsm.backends.django"], labelagentic_fsm) with two models,LogEntry(append-only) andCurrentState(one row per entity), and their migrations.lockistransaction.atomic()around aselect_for_updateof the entity's current-state row; SQLite ignores the row lock.on_commitistransaction.on_commit. It's the only place Django is imported.backends/sqlalchemy/: the same two tables as SQLAlchemyTables (tables.py; names, columns, indexes and constraint match the Django models, and the two store the same values: SQL NULL for a move with no decision, and on SQLite the same datetime text;test_backend_sqlalchemy.pychecks both), so the two backends share one database.StateBackend(engine=None)takes an engine, a function returning one, or nothing for one cached engine perSQLALCHEMY_URL; the last two resolve on first use (engineis acached_property), so a module-level machine imports with no database configured.create_tables()makes what's missing.lockisengine.begin()around aSELECT ... FOR UPDATE, and the open connection sits in aContextVarkeyed by engine:connection()hands it to a handler so its writes commit with the move, a lock inside it takes a savepoint, and reads inside it go through it. A secondContextVarholds theon_commitcallbacks of the open lock, run afterengine.begin()commits.UTCDateTimegives back aware UTC datetimes on SQLite too. It's the only place SQLAlchemy is imported.
Every backend keeps the same two things and the same rule: move, called inside lock,
rewrites the entity's current-state row and appends its log row, and both commit when the lock
is released.
The SQLAlchemy backend's create_tables() only makes what's missing and never alters a table, so
a column a Django migration drops from the log (0003_no_step_name's step) stays, nullable
and unused, in a database it made earlier, until whoever owns it drops it. An app whose own
migration writes the log's dropped applied, reason or version columns through the
historical models declares run_before = [("agentic_fsm", "0002_only_real_moves")].
uv venv && uv pip install -e ".[dev]"
pytesttests/conftest.py holds the example Order machine. Its backend fixture is parametrized, so
every test that takes orders or backend runs on locmem, on django (pytest-django,
in-memory SQLite, tests/settings.py) and on sqlalchemy (in-memory SQLite, StaticPool).
With AGENTIC_FSM_TEST_POSTGRES_URL set, the SQLAlchemy row lock is also tested on Postgres. A new backend is added to that fixture
and must pass the same tests. After changing a model, write the migration and check
DJANGO_SETTINGS_MODULE=tests.settings python -m django makemigrations --check --dry-run.