All docs

docs / use petri

Experiment Copilot

The Experiment Copilot: idea to running experiment to cleanup, with every change approved.

On this page

The copilot is an agent inside the Petri sidebar that takes an experiment from idea to cleanup:

text
idea / selection
  → plan (hypothesis, flag + variants, primary metric, guardrail, split)
  → create flag → create metrics → wire the code (diff) → create experiment → start
  → watch (diagnostics, sample-ratio mismatch, phantom arms)
  → judge with the decision rule → complete (ship / keep control / disable)
  → remove the losing code path (diff)

Other agents for Flagsmith stop at "experiment created". The copilot keeps going through the parts that usually get skipped: checking that measurement works, refusing to call a winner on noise, and deleting the dead branch afterwards.

You approve every change

The copilot has no write access of its own. Its tools call the same Operations layer as Petri's buttons, and every write opens the same modal:

  • Flagsmith changes show the exact HTTP request (method, path, JSON body) before anything is sent.
  • Code changes open a side-by-side diff. Apply change is a normal editor edit, so Undo works.
  • Declining stops that step. The agent is told not to retry, and asks you what to change instead.
  • Every decision, yours or the copilot's, approved or not, is appended to .petri/audit.jsonl in the workspace.

The one exception is asking Flagsmith to recompute results, which changes nothing users see.

Ways to start it

Where What it asks
Select code → right-click → Turn Selection into Experiment… Wire this code into a new experiment, optionally with your idea ("mention free shipping on the button")
Sidebar suggestions Is any running experiment measuring correctly? · How is the running experiment doing? Should we ship? · Ship the winner and remove the losing code
PETRI005 quick fix Switch a getValue() read to getExperimentFlag() for your SDK
PETRI010 quick fix / Remove Losing Code Path… Remove the dead branch of a finished experiment everywhere the flag is read
The chat box Anything else

Tools

Tool Writes? What it does
workspace_context — Environment, flags, variants, experiments, metrics, tracked events, the open file and selection, and Petri's diagnostics for it
read_file — A workspace file with line numbers
find_references — Every read of a flag or trackEvent of an event
experiment_report — Per-metric lift, 95% credible interval and chance to win, SRM, phantom arms and Petri's decision (optionally recomputed)
create_flag approval A multivariate flag with snake_case variant keys
create_metric approval A metric over an SDK event (occurrence, count, sum or mean); reuses an existing one
create_experiment approval An experiment with a primary metric and guardrails
start_experiment / pause_experiment approval Lifecycle
complete_experiment approval Ship a variant, keep control or disable; warns if it contradicts the decision rule
propose_code_edit approval (diff) New contents for one file
enable_event_storage approval Store events in the Flagsmith database for this environment

Verified edits

After you approve a code edit, such as the copilot removing a losing branch, Petri proves the project still works. It runs the project's own checks (petri.verifyCommand, or npm test when the nearest package.json defines a test script). If they fail, Petri puts the file back exactly as it was, tells you, and gives the copilot the failing output so it fixes its proposal instead of claiming success.

Launch gate

Before Petri starts an experiment, from the tree, the results panel or the copilot, it runs the diagnostics over the workspace as if the experiment were already running: events going to Cloud, events disabled, case-mismatched events, reads that skip the exposure, missing identities, keyless variants, no event storage. If any would make it record wrong or no data, the approval says so, names each file and line, and the button reads Start anyway. A measurement bug is caught before a single user is enrolled, not a week later.

It also checks the audience: another running experiment read in the same code (users would be in both and the effects interact), and segment overrides on the flag (users in those segments may get a fixed value instead of their arm).

Approvals are code, and tested

The approval isn't an instruction in the prompt. Every Flagsmith write goes through one function that shows the exact request and sends it only if you approve. Every file edit goes through a diff you accept. extension/test/policy.test.ts runs every copilot tool against a recording Flagsmith client:

  • When you decline, no tool sends anything that changes Flagsmith.
  • When you approve, every write sent is exactly the request you saw.
  • The read-only tools never write. Recompute only asks Flagsmith to refresh results.
  • A new tool fails the test until it is classified.

Remove the approval check from the code and the test fails.

The decision rule

The copilot doesn't judge results by feel. experiment_report returns the verdict of the same deterministic rule the UI shows (recommend in extension/src/insights.ts):

  1. Investigate if there's a phantom arm or a sample-ratio mismatch (p < 0.001). The data can't be trusted yet.
  2. Keep running until Flagsmith reports inference (at least 50 identities and 5 conversions per arm).
  3. Keep control if any guardrail is more than 80% likely hurt, or the best variant is at most 5% likely to win.
  4. Ship the best variant only at ≥ 95% chance to win on the primary metric. With several variants, the reasons also say whether the result clears a Bonferroni-corrected bar (97.5% for two variants), so a borderline win among many arms is labelled as weaker evidence. A ship verdict also states the expected loss: how much conversion you give up on average if the winner is actually worse.
  5. Otherwise keep running, and say what data would settle it.

Lift, credible intervals and chance to win come from Flagsmith's Experiments API. Petri adds only three things of its own: the sample-ratio chi-square when Flagsmith reports none (Flagsmith#9128), the multiple-variant note, and expected loss (from a normal approximation of Flagsmith's interval for the lift).

Model and key

  • Uses the official @anthropic-ai/sdk with streaming, prompt caching and a server-side refusal fallback. The model is petri.model (default claude-opus-5-5).
  • Run Petri: Set Anthropic API Key. The key goes into VS Code's SecretStorage. ANTHROPIC_API_KEY in the environment also works.
  • Only the copilot talks to the Anthropic API. CodeLens, hovers, diagnostics and the results panel work without a key and send nothing outside your Flagsmith.

What it sends

Per request: the system prompt, the tool definitions, your messages and the tool results it asked for (Flagsmith metadata, experiment results and the files it read). It reads files only through read_file, and only the ones it names in its tool calls, which you see as cards in the chat.