~/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.

  1. 01 Catch
  2. 02 Show
  3. 03 Decide
  4. 04 Watch
  5. 05 Act
  6. 06 Delegate
  7. 07 Under the hood

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.
PETRI012 shown on the line that creates the Flagsmith client: events would go to Flagsmith Cloud
PETRI012 on the line that creates the client: a self-hosted SDK would send every event to Flagsmith Cloud.
CodeWhat goes wrong
PETRI001A flag read uses a name that doesn't exist in the project, so it always returns the SDK default.
PETRI002trackEvent sends a name that no metric listens for (often a case mismatch), so it is never counted.
PETRI003A running experiment's metric counts an event that nothing in the code sends, so it reads 0 for every arm.
PETRI004A multivariate variant has no key, so exposures can't be matched to a variant.
PETRI005Code reads a flag that is in a running experiment without sending an exposure, so those users aren't counted.
PETRI006getExperimentFlag is called without an identity (server) or without identify() (client), so nobody is enrolled.
PETRI007The SDK is initialised without enableEvents: true, so exposures and events are dropped.
PETRI008A server SDK never flushes or closes, so the last batch of events is lost on exit.
PETRI009Results contain an arm that isn't a variant key: exposures were sent with the served value. The comparison is skewed.
PETRI010The experiment is finished; the losing code path is dead code.
PETRI011The environment has no event storage, so events are rejected (404) and the experiment never gets results.
PETRI012A self-hosted SDK sends events to Flagsmith Cloud because eventsApiUrl isn't set; the experiment gets no data and nothing errors.
PETRI013Treatment 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.
PETRI014A conversion is tracked under a different identity than the exposure (user.email vs user.id), so it is never joined to an arm.
PETRI015A new random identity on every call (randomUUID(), Math.random()…): each visit is a new user, enrolment repeats and conversions don't join.
PETRI016The installed Flagsmith SDK predates the experiment API (@flagsmith/nodejs < 9.1, @flagsmith/flagsmith < 12.1, or a legacy package): nothing is recorded.
PETRI017An experiment flag read in a cached or statically generated response: many users get one cached variant, so the arms are mislabelled.
PETRI018A browser trackEvent right before navigating away, without `await flagsmith.flush()`: the batched event is lost with the page.
PETRI019Python `max_buffer_items=1`: events tracked back to back (exposure, then conversion) can be dropped.
PETRI020An 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.

CodeLens

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.

Hover

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.

Results panel

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.

VerdictWhen
InvestigateA phantom arm in the results, or a sample-ratio mismatch (p < 0.001). Nothing else is decided on broken data.
Keep runningNo inference yet (Flagsmith needs 50 identities and 5 conversions per arm), or the best variant is between 5% and 95% likely to win.
Keep controlA 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.
SRM

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.

Bonferroni

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.

Expected loss

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.

Planned stop

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.

Sequential

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%.

How much longer

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.

Watch

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.

Health

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).

Lock

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.

Petri Watch → · The health checks →

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.

  1. Rehearsepetri-check rehearse sends 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.
  2. 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.
  3. PauseHolds the experiment; the results so far are kept, and you can start it again.
  4. 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.
  5. Ramp the winnerServe it to 10%, then 50%, then 100% of users, reviewed as entries in petri.plan.json and applied on merge by petri-check apply. Production changes need an explicit "production": true.
  6. 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.
  7. 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 the trackEvent calls 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.

ideaplanflag + metricswire the codestartwatchjudgecompleteremove dead code

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 →

Approval modal showing the exact POST request the copilot wants to send
Every write: the exact request, approved by you.

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

Scannerflag reads, exposures, trackEvent, SDK setup
Storeflags, experiments, metrics, results, hourly timelines (Admin + Experiments API)
Lockpetri.lock.json fingerprints, planned stop, decisions · petri.plan.json

Core

Insights20 diagnostics · decision rule · SRM · expected loss · sequential test · Watch · health
Operationsevery write built as a request first
Approvalexact request or diff · .petri/audit.jsonl

Surfaces

VS CodeProblems · CodeLens · hover · results panel · tree · copilot
Headlesspetri-check · GitHub Action · MCP server · Agent Skill
Browserthe playground on this site

Source: insights.ts (rules and decision) · api.ts (Flagsmith client) · copilot/ · 83 tests

See it on your own code.