All docs

docs / automate

Agent Skill: flagsmith-experiment-check

An Agent Skill that does the whole job through the Flagsmith CLI, behind approval gates.

On this page

What it does. Make a Flagsmith experiment trustworthy, then finish it. Checks the code that feeds the experiment for the bugs that make it collect nothing or skew its split (20 rules: events sent to Flagsmith Cloud from a self-hosted SDK, case-mismatched event names, reads that skip the exposure, missing identities, keyless variants, phantom arms…), reads every experiment with one fixed decision rule (chance to win, guardrails, sample-ratio check, expected loss), and on approval completes the winner, verifies what the SDK now serves, and removes the losing code. Use when asked whether an experiment is measuring correctly, "why does my experiment show no data", "is it safe to ship the winner", "finish / roll out the experiment", or to clean up after one.

Needs. Flagsmith CLI v2+ (flagsmith), Node.js 20+ (runs npx petri-check), git. Flagsmith Cloud or self-hosted with Experimentation. Read-only until an approval gate.

Install the skill

sh
# Claude Code (personal) — or copy into .claude/skills/ in a repo
mkdir -p ~/.claude/skills && cp -r skills/flagsmith-experiment-check ~/.claude/skills/

Needs the Flagsmith CLI v2 bound to the project (flagsmith init + flagsmith login; or, after node selfhost/bootstrap.mjs, set -a; . ./flagsmith.env; set +a in the shell that starts the agent) and Node.js 20+.

A recorded run, with its token count: DEMO.md.

Then ask: "Is the checkout experiment measuring correctly, and can we ship it?"

The skill (SKILL.md)

This is the file the agent reads, unchanged.

An experiment can run for a week, collect nothing and never raise an error. Your job: prove the measurement works, read the result with a fixed rule, and only then ship, with the person approving every change.

Keep output small (token count is judged): use the commands exactly as written. Don't read whole files or git history to re-derive what the checks already report; open a file only to edit the line a finding points at. petri-check prints one line per finding and one line per experiment; flagsmith calls use --jq to return only the field you need. Never print a whole JSON payload, and never read or print API keys.

Stop at every ⛔ and wait for an explicit "yes". A "yes" covers that one step only.

0. Context (read-only)

bash
flagsmith config --jq '"\(.environment.name) \(.environment.value) project=\(.project.value) \(.apiUrl.value)"'

One line: environment name, key, project, API. If it fails or the environment is wrong, stop and ask the person to run flagsmith init / flagsmith login. For a self-hosted Flagsmith set up with node selfhost/bootstrap.mjs, the person loads the key into the shell before starting the agent (set -a; . ./flagsmith.env; set +a): petri-check reads flagsmith.env itself, the flagsmith CLI only reads the environment. If the environment name contains prod, say so now: every write below is a production change.

1. Check the wiring (read-only)

bash
npx -y petri-check@latest . --format brief --fail-on never

With credentials (flagsmith.env, FLAGSMITH_API_KEY_<host>, or FLAGSMITH_API_KEY + flagsmith.json) all 20 rules run against the live flags, metrics and results; without, only the code-only rules (… offline … in the last line). Each line is file:line SEVERITY CODE what's wrong.

  • E (error) means the experiment's numbers are wrong or empty. Nothing that follows is trustworthy until it is fixed.
  • Fix code findings in the code, minimal diff, and show the diff. The common ones:
    • PETRI012: add eventProcessorConfig: { eventsApiUrl: "<your Flagsmith>/api/" } to the server SDK.
    • PETRI002: rename the event to the metric's exact name.
    • PETRI005: read the flag with getExperimentFlag(name, identity).
    • PETRI007: enableEvents: true.
    • PETRI006: pass the identity.
    • PETRI008: close() on shutdown. Explain one with npx -y petri-check@latest mcp → petri_rule, or https://github.com/Ivan825/petri/blob/main/docs/diagnostics.md.
  • PETRI011 (no event storage) is a Flagsmith write: ⛔ ask, then echo '{"warehouse_type":"postgres","name":"Flagsmith database","config":{"source":"flagsmith_database"}}' | flagsmith api api/v1/environments/<ENV>/warehouse-connections/ -X POST --input - --jq .id
  • Before a launch, if a development environment and a running local copy exist, prove it end to end: npx -y petri-check@latest rehearse . --url <local page> --convert form --visits 30 (a recount of what Flagsmith recorded; never against production).
  • Re-run step 1 until there are no E lines. If the experiment hasn't started yet, don't start it (and don't advise starting it) while any E line remains: it would enroll users into an experiment that records wrong or no data. Code fixes stay as local, reviewable changes (commit on a branch if the person wants).

2. Read the experiments (read-only)

bash
npx -y petri-check@latest experiments .

One line each: #<id> <flag> <status>: <VERDICT> | <arm> <lift> [<95% interval>] win <chance> loss <expected loss> n=<n> | srm p=<p> | health <n> pass · <n> warn. * after the SRM p-value means Petri computed it because Flagsmith returned none (Flagsmith#9128). Indented lines under it:

  • ✖ … is a failed health check or a Watch alert (an arm stopped converting or enrolling, or the split broke, with the commit that likely caused it). Report it first: data after the break is wrong, so fix the cause before reading the verdict.
  • ⚠ Results are fresh: computed … ago means the numbers are stale. npx -y petri-check@latest watch . --refresh recomputes them (one Flagsmith recompute, allowed every 5 minutes) and re-checks.

Need the experiment's metrics or stop plan? One read-only call, small output:

bash
flagsmith api api/v1/environments/<ENV>/experiments/<ID>/ --jq '{status,started_at,hypothesis,metrics:[.metrics[]|"\(.metric_name) (\(.expected_direction))"]}'

The verdict comes from one deterministic rule, the same one the Petri editor and copilot use. Report it; don't overrule it:

Verdict Meaning Do
SHIP <variant> (rollout_variant=<id>) ≥ 95% chance to beat control on the primary metric, no guardrail > 80% likely hurt, no SRM, no phantom arms go to step 3
KEEP RUNNING not enough evidence yet report the why: line and stop
KEEP CONTROL the variant lost, or a guardrail is likely hurt go to step 3 with rollout_decision=control
INVESTIGATE traffic split is broken (SRM) or exposures are mislabelled don't ship; go back to step 1

If a run was pre-registered with a stop date, don't finish before it, even on SHIP; say so.

A wait: the planned stop isn't reached line means the experiment was pre-registered to be decided later. Report the verdict as provisional, and don't offer step 3 until the stop has passed.

A ⚠ treatment changed while running (PETRI013) line means the arms didn't serve the same thing throughout (code or Flagsmith changed since petri.lock.json). Report it before the verdict: the numbers mix two treatments, so don't ship on them without the person's explicit decision. If the repo has no petri.lock.json and an experiment is running, suggest npx -y petri-check@latest lock . and committing it.

3. Finish: ⛔ one gate, the exact request

Show the person the one command and what it does, then wait for "yes":

bash
flagsmith api api/v1/environments/<ENV>/experiments/<ID>/complete/ -f rollout_decision=variant -F rollout_variant=<VARIANT_ID> --jq .status

(rollout_decision=control keeps control; disable turns the flag off.) On a production environment, also ask the person to type the environment name. Never batch this with anything else.

If the person wants a gradual rollout instead of everyone at once: complete with rollout_decision=control, then propose ramp entries in petri.plan.json ({"id":"ramp-<flag>-10","action":"ramp","flag":"<FLAG>","variant":"<KEY>","percent":10}, then 50, then 100) as pull requests; npx -y petri-check@latest apply . --dry-run shows the exact request. Each step is reviewed; nothing is applied by you.

Then verify what the SDK serves now, read-only (any new identity; without one you'd see the environment default):

bash
flagsmith evaluate <FLAG> --identity skill-check-1 --jq .value

It must print the winning variant's value. If not, stop and report; don't retry the write.

4. Remove the losing code: ⛔ diff first

The experiment is over, so the other branch is dead code (petri-check now reports PETRI010 on each read).

  1. Find every read: git grep -n "<FLAG>".
  2. Replace each read with the winning value, delete the losing branch and now-unused helpers, keep every trackEvent call. Show one diff for all files. ⛔ Apply only on "yes".
  3. npx -y petri-check@latest . --format brief --fail-on never must show no <FLAG> lines.
  4. If the repo has a GitHub remote, offer to ship the cleanup as a pull request instead of a local edit. ⛔ Ask first, then: git switch -c cleanup/<FLAG> && git commit -am "Remove <FLAG>: <winner> won experiment #<ID>" && git push -u origin HEAD && gh pr create --fill --body-file <file> Write the body file with: the experiment and the winner; the verdict line from step 2 verbatim (lift, interval, chance, expected loss, SRM); whether the traffic was real or simulated, if you know; and a checklist ([x] completed in Flagsmith, [x] SDK serves <winner> (flagsmith evaluate), [x] petri-check clean, [ ] flag archived after merge). Never merge it yourself.
  5. ⛔ Ask before archiving the flag (after the PR is merged, if there is one): flagsmith feature update <FLAG> --archive --jq .is_archived

Done

Report in ≤ 5 lines: findings fixed, the verdict and its numbers (lift, interval, chance, expected loss, SRM), what was completed, what the SDK serves, and the cleanup diff or PR link. Point to .petri/audit.jsonl if the Petri editor was used.

skills/flagsmith-experiment-check/SKILL.mdView or edit on GitHub →