Property tests

Tests that try many random sequences of calls, approvals, crashes and retries, to catch cases nobody wrote by hand.

tests/test_properties.py checks rules that must hold for every input, not only for a few examples. Hypothesis makes the inputs and, when a rule breaks, shrinks the input to a short case. Hypothesis is a test-only dependency. The call4agents package itself still needs only the Python standard library.

pip install hypothesis
python -m tests.test_properties --require-hypothesis        # the CI run
python -m tests.test_properties --long                      # many more examples; prints its seed
python -m tests.test_properties --long --seed 1234          # repeat a long run
python -m tests.test_properties --only machine              # one group: machine, canonical, codec, files

Without Hypothesis the suite prints skip and passes; --require-hypothesis makes that a failure.

How many examples

group CI run (each push, Linux, macOS, Windows, Python 3.12) --long (weekly, Linux)
machine 40 runs of up to 20 steps, plus a check that each of 15 paths was taken 500 runs of up to 50 steps
canonical, codec 200 examples per property 3,000 per property
files 200 damaged copies 3,000 damaged copies

The CI run is derandomized: the same examples every time, so a failure in CI can be repeated on a laptop. The long run uses a new random seed each week and prints it. Measured on one Windows laptop: the CI run takes 20 seconds when the machine is idle, and 1.5 to 5 minutes when other heavy jobs run at the same time.

The properties

a. The state machine (--only machine)

One run record, one approved tool pay(item) with reconcile= and retry_on=, and a fake provider that counts real outside actions. Hypothesis chooses a sequence of steps: approve, deny, call (with or without c4a_key=, with retries=0 or 1), crash inside the action before or after the outside action (with or without the error being recorded), a retry_on error, another error before or after the action, resume, reconcile answers (truthful, or "lagging": c4a.UNKNOWN), two callers at once, a caller that comes while the first is still inside the action (Fix 17's case), and two callers resuming a call in doubt at once.

After every step:

The model also predicts the first decision of each call (replay, refuse, ask reconcile, or run) and checks it.

On 0.7.1 this machine finds the double payment that Fix 17 removed. Hypothesis shrinks it to one step: still_running_and_a_racer(item='a', key=None).

b. canonical() and effect_key() (--only canonical)

c. The codec (--only codec)

d. Damaged record files (--only files)

A record holds: A finished (result RA), B in doubt, C approved and unused, D never approved. Each example damages a copy: random bit flips, flips aimed near row text, a run of random bytes, a run of zeros, a cut at a random length, and sometimes a damaged, short, empty or missing secret file. Then it asks for A, B, C and D through the real API.

Checked: A, B and D never run, B never comes back as done, C runs at most once, and every error is LedgerError, sqlite3.DatabaseError, EffectInDoubt or ApprovalRequired.

Measured but not asserted: how often A came back with a different result (0 in our runs).

PF1 is a fixed check: four hand-damaged rows (an approval row that is not JSON, an effect_approved row without its field, a tool_done row without its result, a format version that is not a number) each give LedgerError.

Mutations

python -m tests.mutations.run_kit_mutations --suite tests.test_properties puts each bug back in a copy of the kit and runs the suite. A no-op control must be MISSED.

mutation caught by
a "yes" is never spent machine
a "no" is ignored machine
a "no" is ignored for a call in doubt machine
every error counts as "did not happen" machine
reconcile None is read as done machine
UNKNOWN is read as None machine
the result row is written twice machine
a finished call is not replayed machine
no owner check before reconcile (Fix 17) machine
the codec calls whatever a record names codec
a tuple comes back as a list codec
keys depend on dict order canonical
any object's str() is accepted in a key canonical
a row without its field is not explained (PF1) files
a damaged format version is not explained (PF1) files
approval rows read with json.loads (PF1) files

What this does NOT cover

Source: docs/PROPERTIES.md in the repository.