~/how-it-works
Catch it. Read it. Ship it.
Petri follows an experiment through its whole life inside the editor: it checks the code that feeds it, shows the results where the flag is read, decides with one fixed rule, runs the lifecycle, and deletes the losing code. It works through Flagsmith's Admin and Experiments APIs, on your own server or on Cloud.
01 · Catch
20 ways an experiment silently fails, flagged as you type.
Flags keep working when an experiment's measurement is broken, so nothing errors and nobody looks. Petri reads the code (flag reads, exposures, trackEvent calls, SDK setup) and the live Flagsmith project (flags, variants, metrics, results, event storage), and reports each mismatch in the Problems panel, on the line that causes it.
- Each finding has a code,
PETRI001–PETRI020, and a page in the docs. - Where a fix is mechanical there is a quick fix: rename the event, switch to
getExperimentFlag(), set variant keys, turn on event storage. - Fixes that change Flagsmith go through the same approval as everything else.
- Beyond the wiring: identity joins (
PETRI014,PETRI015), cached variants (PETRI017), events lost on navigation (PETRI018), SDKs too old or without experiment events (PETRI016,PETRI020), and a treatment changed mid-run (PETRI013). - The same rules run headless:
npx petri-check ., the GitHub Action and the MCP server.

| Code | What goes wrong |
|---|---|
| PETRI001 | A flag read uses a name that doesn't exist in the project, so it always returns the SDK default. |
| PETRI002 | trackEvent sends a name that no metric listens for (often a case mismatch), so it is never counted. |
| PETRI003 | A running experiment's metric counts an event that nothing in the code sends, so it reads 0 for every arm. |
| PETRI004 | A multivariate variant has no key, so exposures can't be matched to a variant. |
| PETRI005 | Code reads a flag that is in a running experiment without sending an exposure, so those users aren't counted. |
| PETRI006 | getExperimentFlag is called without an identity (server) or without identify() (client), so nobody is enrolled. |
| PETRI007 | The SDK is initialised without enableEvents: true, so exposures and events are dropped. |
| PETRI008 | A server SDK never flushes or closes, so the last batch of events is lost on exit. |
| PETRI009 | Results contain an arm that isn't a variant key: exposures were sent with the served value. The comparison is skewed. |
| PETRI010 | The experiment is finished; the losing code path is dead code. |
| PETRI011 | The environment has no event storage, so events are rejected (404) and the experiment never gets results. |
| PETRI012 | A self-hosted SDK sends events to Flagsmith Cloud because eventsApiUrl isn't set; the experiment gets no data and nothing errors. |
| PETRI013 | Treatment changed while the experiment runs: a code path serving it, or its variants, split or metrics in Flagsmith, differ from petri.lock.json. The results mix two treatments. |
| PETRI014 | A conversion is tracked under a different identity than the exposure (user.email vs user.id), so it is never joined to an arm. |
| PETRI015 | A new random identity on every call (randomUUID(), Math.random()…): each visit is a new user, enrolment repeats and conversions don't join. |
| PETRI016 | The installed Flagsmith SDK predates the experiment API (@flagsmith/nodejs < 9.1, @flagsmith/flagsmith < 12.1, or a legacy package): nothing is recorded. |
| PETRI017 | An experiment flag read in a cached or statically generated response: many users get one cached variant, so the arms are mislabelled. |
| PETRI018 | A browser trackEvent right before navigating away, without `await flagsmith.flush()`: the batched event is lost with the page. |
| PETRI019 | Python `max_buffer_items=1`: events tracked back to back (exposure, then conversion) can be dropped. |
| PETRI020 | An experiment flag read from an SDK that sends no experiment events (.NET, Ruby, PHP, Rust, iOS, Elixir): those users are never counted. |
Offline, without a Flagsmith key, PETRI007, PETRI008 and PETRI012–PETRI020 still run: they need only the code (and, for PETRI013, a committed petri.lock.json). Every rule, with examples and fixes →
02 · Show
Results above the line that reads the flag.
You shouldn't need a dashboard to know whether the code you're editing is in a running experiment, or who's winning.
Above every read
The experiment's status, the leading arm, its lift on the primary metric and its chance to win, refreshed every 60 seconds.
The live split
Every variant with its value and the experiment's split (not the environment default), the hypothesis, and per-arm results with 95% intervals.
Every arm, every metric
Lift with a plot of each 95% credible interval, chance to win, expected loss and Petri's verdict.


03 · Decide
One rule, the same everywhere.
The results panel, the CodeLens, the copilot, the CLI, the MCP server and the Agent Skill all call the same function. The copilot reports its verdict; it doesn't judge results by feel.
| Verdict | When |
|---|---|
| Investigate | A phantom arm in the results, or a sample-ratio mismatch (p < 0.001). Nothing else is decided on broken data. |
| Keep running | No inference yet (Flagsmith needs 50 identities and 5 conversions per arm), or the best variant is between 5% and 95% likely to win. |
| Keep control | A guardrail is more than 80% likely hurt, or the best variant is at most 5% likely to win. |
| Ship <variant> | At least 95% chance to beat control on the primary metric, with every check above passed. |
A split check that works
Flagsmith returns no sample-ratio p-value while an experiment runs (#9128). Petri runs its own chi-square against the configured split, so the guard still fires.
Many arms, stated
With two or more variants, a ship verdict also says whether it clears the corrected bar (97.5% for two), so a borderline win among many arms reads as weaker evidence.
The size of the downside
"98% to win" with how much conversion you'd give up on average if it's actually worse: 0.23% for the Petri Coffee winner.
Pre-registered, enforced
petri-check lock --stop-at … --stop-n … --mde … writes the stop into the lock. The CodeLens shows the progress and ETA; completing before it reads Complete early and is recorded that way.
Peek-safe, if you must
A mixture SPRT gives an always-valid p-value at every hourly look. In 2,000 simulated A/A tests, hourly peeking shipped a dud 36.5% of the time; the sequential test, 0.3%.
Users to a decision
While it's undecided, Petri estimates the users still needed for the leader to clear 95% at the current effect, and the hours at the current traffic.
The rule in full, with the formulas → · The A/A simulation →
04 · Watch
Catch the measurement breaking while it runs.
Wiring that was right at launch can break on day 3: a deploy, a renamed event, a cache rule. The totals still look plausible. Petri reads Flagsmith's hourly timelines per arm and checks the experiment's health on every refresh, in the editor, the CLI and CI.
Breaks, with the commit
An arm that stops enrolling or converting (a Poisson test on the hourly counts), or a split that drifts. Each alert names the hour it started and the commit deployed just before. petri-check watch exits 1, so it can gate a deploy.
One scorecard
Pass / warn / fail on the traffic split (α = 0.001), arms vs variants, conversions per arm, event storage, continuity, treatment, freshness and novelty (early lift that fades).
The treatment can't drift
petri.lock.json fingerprints the code serving each arm (whitespace and comments ignored), and the variants, split and metrics in Flagsmith. Any change while it runs is PETRI013, in the editor and in the PR.
05 · Act
Start, pause, complete, clean up, from the editor.
The lifecycle lives in the sidebar, the results panel and the command palette. Every step is a Flagsmith request you see before it's sent.
- Rehearse
petri-check rehearsesends N visitors through your real app in a non-production environment, converts some, and checks Flagsmith counted exactly that many. A gap names the likely rule. It refuses production environments. - StartThe launch gate runs the rules as if the experiment were already running, and the approval names any file and line that would record wrong data ("Start anyway"). It also checks the audience, then writes the treatment lock. CodeLens and hovers switch to the experiment's numbers.
- PauseHolds the experiment; the results so far are kept, and you can start it again.
- Complete & roll outShip a variant, keep control or turn the flag off. Petri marks the option the rule recommends, and warns in the approval if you go against it or there are no results yet.
- Ramp the winnerServe it to 10%, then 50%, then 100% of users, reviewed as entries in
petri.plan.jsonand applied on merge bypetri-check apply. Production changes need an explicit"production": true. - Remember the decisionThe outcome, evidence and whether it stopped early go into the lock's decision history, and a CodeLens above the decided code says what was learned.
- Remove the losing codeOnce it's finished, every read of the flag is dead code (
PETRI010). The copilot finds every read and proposes one diff that keeps only the winner and leaves thetrackEventcalls in place.
06 · Delegate
An agent that finishes the job, and asks first.
The Experiment Copilot (Claude, through the official Anthropic SDK) takes an experiment from a selected line of code to cleanup. In the recorded run it went from a one-line idea to a running experiment in 56 seconds, with 4 approvals, and fixed the self-hosted events bug on the way.
It has no write access of its own: its tools go through the same approvals as Petri's buttons. How approvals work → · The copilot's tools →

07 · Under the hood
Small pieces, one rule engine.
The diagnostics and the decision rule are pure functions with no editor dependency, so the extension, the CLI, the GitHub Action, the MCP server, the Agent Skill and this site's playground all run the same code.
Inputs
Core
Surfaces
Source: insights.ts (rules and decision) · api.ts (Flagsmith client) · copilot/ · 83 tests