GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • snappea
  • Docs
  • Playground
  • GitHub
  • Your first model
  • Work with your agent
  • Python
  • Rust
  • Go
  • Java
  • Understand coverage
  • Run checks in CI
  • Command line
  • Coming from P

This view

  • Original page
  • New address

GeezerVison is showing snappea.dev — Overview · Snap Pea docs

Docs

Overview

What P is, and how it differs from the tests you already write.

Two clients each read a shared counter, add one and write it back. Your unit tests pass because they run the calls in one fixed order, but in production both clients can read 0, and one update is lost. P finds that ordering in milliseconds, from a short model of the design, before any service code exists.

A model, a spec and your code

P is a language for writing small, runnable descriptions of a system's design. You use it to check two kinds of thing:

You, or an agent, write a model of the design. snappea check runs it thousands of times, each with a different ordering of messages, and the spec flags any run that breaks a rule. None of your service code is involved.

Is P my code?
No. A model describes your design: the components, the messages they send, and what each does when a message arrives. It's usually a few hundred lines, and it runs only inside the checker.
Does it check my code?
The checker runs the model, not your code. A spec is a rule written in P, like "stock never goes negative". The same spec checks the model at design time and, through events your tests record, your code at test time.
How does a model help?
It finds bugs that depend on timing: races, reordered or lost messages, retries and crashes. Unit tests rarely hit these bugs, and they cost the most to fix once the code is written.

How it differs from a unit test

In a unit test, you pick one scenario and its order of events. In a P test, you describe the setup, and the checker picks the order: which message arrives first and which component runs next. It runs thousands of orderings and stops at the first one that breaks a rule.

counter0
A read–
B read–
  1. unit testA readA writeB readB write

Two clients each read the counter, then write back what they read plus one. The answer should be 2.

Simplified: each read here includes the counter's reply. The real model also lets each message wait in an inbox, so there are many more orderings.

Unit testP model check
Runsyour codea model of your design
Who picks the orderyou, oncethe checker, thousands of times
Findslogic bugs on the path you wrotebugs that need an unlucky ordering
Whenafter the code existsbefore it, and alongside it

Follow a counterexample

Your first model walks through the lost-update P code, its failure trace, and a design change that fixes it in the Playground.

The same rules, checking your code

A spec observes events and asserts rules about them. This one checks an inventory service. Specs check models in the same way.

inventory.p (excerpt)
spec InventoryCorrect observes eStocked, eReserved, eReleased {
var available: int;
state Ready {
  on eReserved do (p: (order: string, units: int)) {
    available = available - p.units;
    assert available >= 0, "reservation exceeds available stock";
  }
  // ...
}
}

Your code emits the events after each change succeeds, and your ordinary tests check them:

Python
def reserve(self, sku, order, units):
    ...
    emit("eReserved", key=sku, order=order, units=units)

def test_release_makes_stock_available_again(inventory_factory):
    with check_spec("InventoryCorrect"):
        inventory = inventory_factory()
        inventory.reserve("coffee", "order-1", 3)
        inventory.release("coffee", "order-1")

There are helpers for Python, Rust, Java and Go. To check events from running services, see live observation across processes and snappea observe. The same emit calls can write production logs for snappea observe: see testing and production.

Where P fits

↺ bug found: change the design, update the model, check again

Design. Write or update the design doc: components, messages, failure cases.

AI agents are well suited to writing models: they can turn a design doc into machines and specs quickly, and rerun the checker after every change. Review the model boundaries and assumptions as well as the specs. The specs are the short, plain statement of what must always be true, and the part of the model worth reviewing line by line.

What a passing check establishes

Checks explore the model you wrote, including its input bounds, atomic operations and failure assumptions. Complete exploration covers that bounded model. A timed-out search may miss a bug. Coverage and mutation testing help find weak checks, but cannot establish that every requirement is represented.

Application tests check the recorded events against the same specs; they do not automatically compare all implementation behavior with the model. Instrumentation and forced-interleaving tests need review too.

Next

  • Work with your agent: model a feature and test its implementation.
  • Run the lost-update model in the Playground, then try the bigger tutorials in its menu.
  • Integration examples: specs checking real code in four languages.
  • The P language manual, from the upstream project.
You are visitor 01402775 — GeezerVison does not store the pages you look up.
Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.