All docs

docs / use petri

Diagnostics

The 20 ways an experiment silently loses or skews its data, and the fix for each.

On this page

Petri checks every file that reads a flag or tracks an event against the live Flagsmith environment you selected. Each rule is a way an experiment silently loses or skews its data: the code runs, nothing throws, and the results end up empty or wrong.

Findings appear in the Problems panel with the source Petri and a code. Where a fix is safe and mechanical, a quick fix (💡, or Ctrl/Cmd+.) applies it. Fixes that change Flagsmith go through the same approval modal as every other write.

Code Severity What's wrong Quick fix
PETRI001 warning The flag name doesn't exist in this project Rename to the closest flag
PETRI002 error / info trackEvent name matches no metric (often a case mismatch) Rename to the metric's event
PETRI003 warning A running experiment's metric counts an event nothing in the workspace sends —
PETRI004 error A variant of a live experiment's flag has no key Set variant keys…
PETRI005 warning getValue()/hasFeature() on a flag in a running experiment records no exposure Switch to getExperimentFlag()
PETRI006 error / warning Experiment flag read without an identity —
PETRI007 error SDK initialised without enableEvents: true —
PETRI008 warning Server SDK never calls close()/flushEvents() —
PETRI009 error Results contain an arm that isn't a variant —
PETRI010 hint The experiment is finished; this branch is dead code Remove the losing code path…
PETRI011 error The environment has no warehouse connection, so events are rejected Store events in the Flagsmith database…
PETRI012 error Self-hosted Flagsmith, but the SDK sends events to Flagsmith Cloud —
PETRI013 error / warning What a running experiment serves changed since petri.lock.json recorded it Revert, or finish and restart
PETRI014 warning The conversion is tracked under a different identity than the exposure Use the same id
PETRI015 error The flag is read with a new random id each call Use a stable id
PETRI016 error The installed Flagsmith SDK has no experiment API Upgrade
PETRI017 warning A per-user variant in a cached or statically generated response Render per request
PETRI018 warning A browser event tracked right before navigating away await flagsmith.flush()
PETRI019 warning Python max_buffer_items=1 drops back-to-back events Leave the default
PETRI020 error An experiment flag read from an SDK with no experiment events Read it from a supported SDK

Methods recognised: getExperimentFlag, useExperiment, getValue, getFeatureValue, hasFeature, isFeatureEnabled, trackEvent and their snake_case and PascalCase forms, across JavaScript, TypeScript, Python, Go, Ruby, Java, Kotlin, Swift, Dart, PHP, C#, Rust, Vue and Svelte files (petri.include).


PETRI001: unknown flag

js
flagsmith.getValue("chekout_cta") // ⚠ No flag named "chekout_cta". Did you mean "checkout_cta"?

The SDK returns its default for a flag it doesn't know, so a typo looks like "the variant isn't showing".

PETRI002: event doesn't match a metric

js
flagsmith.trackEvent("Add_To_Cart", { identifier })
// ✖ Event "Add_To_Cart" doesn't match any metric. Metric "Add to cart" listens for "add_to_cart".

Metric event names are case-sensitive. The events are stored, but no metric ever counts them. If no metric is even close, this is reported as info: the event is stored but nothing reads it.

PETRI003: metric never tracked

A running experiment has a metric counting checkout_completed, but no file in the workspace calls trackEvent("checkout_completed"). That metric will read 0 for every arm.

PETRI004: variant without a key

Exposures and results are keyed by variant key. A multivariate option with no key can't be matched reliably. The quick fix proposes snake_case keys from the values and sends them only after you approve (a PUT, see Known issues).

PETRI005: read without exposure

python
label = flags.get_feature_value("checkout_cta")  # ⚠ no exposure is recorded

Only getExperimentFlag() sends $flag_exposure. Users who see a variant through getValue() are never counted, so the comparison only sees the subset that went through the experiment call. The quick fix asks the copilot to switch the call for your SDK, as a diff.

PETRI006: no identity

On a server SDK, getExperimentFlag("name") without an identifier enrols nobody (error). In a browser SDK file that never calls identify(), anonymous visitors aren't bucketed (warning).

PETRI007: events disabled

js
new Flagsmith({ environmentKey })   // ✖ without enableEvents: true, exposures and trackEvent are dropped

PETRI008: events never flushed

Server SDKs batch events in memory. A script, job or serverless function that exits without flagsmith.close() (or flushEvents()) loses the last batch, which skews short-lived processes toward zero conversions.

PETRI009: phantom arm

The results contain an arm like "Buy now · free shipping" next to free_shipping. Something sent $flag_exposure with the served value instead of the variant key. Those identities are split off into an arm that doesn't exist, and the real arms lose their data. The decision rule refuses to ship while a phantom arm exists.

PETRI010: decided flag

The experiment is complete and Flagsmith now serves one value to everyone. The if around this read has a dead branch. The quick fix asks the copilot to remove the losing path, keeping only the winning behaviour, as a diff.

PETRI011: no event storage

The environment has no warehouse connection, so the events endpoint rejects exposures and events (404) and the experiment never gets results. The quick fix enables storing events in the Flagsmith database for this environment, after approval.

PETRI012: self-hosted events sent to Cloud

js
new Flagsmith({
  environmentKey,
  apiUrl: "http://localhost:8000/api/v1/",
  enableEvents: true,
  // ✖ eventProcessorConfig.eventsApiUrl not set
})

On self-hosted Flagsmith, setting apiUrl is not enough: the SDK's event processor defaults to Flagsmith Cloud (events.api.flagsmith.com). Flags work and nothing errors, but the experiment on your server sees zero exposures and events. Fix:

js
eventProcessorConfig: { eventsApiUrl: "http://localhost:8000/api/" }

We hit this ourselves while building the demo shop. It's the most expensive silent failure in the list because everything else looks healthy.

PETRI013: treatment changed while running

An experiment only means something if each arm serves the same thing from start to end. Change the variant's code or its value in Flagsmith halfway through, and the results silently mix two treatments. Every number still looks fine.

When an experiment starts, Petri writes petri.lock.json: each running experiment's variant values and keys, split and metric events, plus a fingerprint of every code path that reads its flag (the enclosing function, with whitespace and comments ignored). Commit it. You can also create one with Petri: Lock Running Experiments or npx petri-check lock.

js
async function buyButtonLabel(shopperId) {
  const flag = await flagsmith.getExperimentFlag("checkout_cta", shopperId);   // ✖ PETRI013
  return flag.enabled && flag.value ? String(flag.value) : "Buy now!";          // edited while running
}
  • Code (error): a locked code path changed. It's flagged in the editor as you type, and in CI offline, with no Flagsmith key, because the lockfile is in the repo. A new code path that starts reading the flag mid-run is a warning.
  • Events, offline: the lock also records each running experiment's metric events, so CI catches a renamed event (PETRI002) or a metric event nothing sends any more (PETRI003) with no Flagsmith key.
  • Flagsmith (error, needs live data): a variant's value or key, the split, or the metrics changed since the lock.
  • In petri-check experiments and the copilot's report, the experiment gets a "treatment changed while running" line. The verdict itself is unchanged; you decide what to do with mixed data.

Completing the experiment removes its entry, after which the code is free to change (and PETRI010 offers to remove the losing path).

Decision memory. Completing also records the decision and its evidence in petri.lock.json: the winner, the value it serves, lift, interval, chance to win, expected loss and sample size, captured at completion. Wherever that value appears in code, a lens keeps the evidence above it:

text
✓ Decided by experiment “Delivery promise on the buy button” on 29 Nov 2026, free_shipping won: +50.4% checkout conversion [+0.6% … +100.3%], 98% to beat control, 2,028 users
return "Buy now · free shipping";

Six months later, whoever wants to "just change the button text" sees that it was tested and what it won by.

PETRI014: conversion tracked under a different identity

js
await flagsmith.getExperimentFlag("checkout_cta", user.id);
flagsmith.trackEvent("checkout_completed", { identifier: user.email });   // ⚠ PETRI014

Flagsmith joins conversions to exposures per identity. Enrol by user.id and track by user.email, and every conversion lands on an identity that was never exposed: the metric reads zero for every arm, with no error. Petri compares the identity expressions in the file, such as user.id vs user.email on the same object, or an email against an id. Different variable names for the same value (shopperId and id) aren't flagged.

PETRI015: a new random identity on every call

js
await flagsmith.getExperimentFlag("checkout_cta", randomUUID());   // ✖ PETRI015

A fresh randomUUID(), uuid4(), nanoid() or Math.random() per call makes every visit a new user. The same person is enrolled again and again, possibly in different arms, and their conversion can't be joined to their exposure. Keep a stable visitor id, for example in a cookie, as the demo shop does.

PETRI016: Flagsmith SDK too old for experiments

The code calls getExperimentFlag / trackEvent, but the SDK in package.json predates them, so no exposure or event is ever sent. From the published packages:

  • @flagsmith/nodejs added the experiment API in 9.1.0;
  • @flagsmith/flagsmith (browser and React) added it in 12.1.0;
  • the legacy flagsmith and flagsmith-nodejs packages never had it.

Petri prefers the version actually installed in node_modules, and otherwise uses the lowest version the range allows. The finding points at the dependency's line in package.json. It runs offline.

PETRI017: variant in a cached or static response

The variant is chosen per user, but the response is shared: Cache-Control: public/s-maxage, Next.js revalidate, force-static, getStaticProps or generateStaticParams. Many users then see one cached variant while each is recorded in the arm they were assigned, so the arms are mislabelled. Read the flag where the response isn't shared, or on the client.

PETRI018: event lost when the page navigates

The browser SDK sends events in batches. A trackEvent immediately followed by location.href = …, router.push(…), form.submit() or window.open(…) usually leaves with the page. Call await flagsmith.flush() first; the @flagsmith/flagsmith SDK exposes it.

PETRI019: Python max_buffer_items=1

A published Flagsmith docs prompt recommends it for short scripts. Measured by another Toggle Wars team (agent-ab), events tracked back to back (an exposure, then a conversion) can be dropped: every one of 197 programs that set it lost events. Leave the default buffer and call flagsmith.close() before exit.

PETRI020: an SDK that sends no experiment events

Per Flagsmith's docs, only the JS, React, Python, Java, Node, Go and Flutter SDKs send experiment events. An experiment flag read from .NET, Ruby, PHP, Rust, iOS or Elixir records no exposure, so those users are never counted.

Proposing a rule

Found another silent failure? Open a diagnostic proposal. Rules live in extension/src/insights.ts (analyse) and are unit-tested in extension/test/insights.test.ts.

docs/diagnostics.mdView or edit on GitHub →