All docs

docs / automate

Experiments as pull requests

Experiments as pull requests: petri.plan.json, a dry run on the PR, apply on merge.

In Petri's editor, a person approves each change in a dialog. For teams, the review belongs where code reviews already happen. Experiment changes go in a file, petri.plan.json, and a pull request is the approval: branch protection gives you a second reviewer, and the history is in git.

json
{
  "changes": [
    { "id": "start-checkout-cta", "experiment": 2, "action": "start", "why": "Rehearsed 30/30, 15/15" },
    { "id": "ship-free-shipping", "experiment": 2, "action": "complete", "decision": "variant", "variant": "free_shipping",
      "production": true, "why": "98% to beat control, planned stop reached" }
  ]
}
  • npx petri-check apply . --dry-run prints the exact request each change would send, against the live state.
  • npx petri-check apply . sends them. It's idempotent: a change already in effect (an experiment already running, already completed) is skipped. A change that can't apply cleanly (an unknown variant key, a completed experiment) is refused and exits 1.
  • Production needs an explicit "production": true on the change, so an approval for a test environment can't land in production by accident.
  • Agents propose, people merge. The MCP tool petri_plan_change returns the entry plus a dry run, and changes nothing. The agent adds the entry to the file through its own tools, and the PR review decides.

The workflow

yaml
# .github/workflows/experiments.yml
on:
  pull_request:
    paths: [petri.plan.json]
  push:
    branches: [main]
    paths: [petri.plan.json]
jobs:
  plan:
    runs-on: ubuntu-latest
    steps:
      - uses: actions/checkout@v7
      - uses: Ivan825/petri@v0.1.5
        with:
          command: apply
          dry_run: ${{ github.event_name == 'pull_request' }}   # PRs: show what would happen
          environment: ${{ vars.FLAGSMITH_ENVIRONMENT }}
          api_url: https://flags.example.com/api/v1/
        env:
          FLAGSMITH_API_KEY: ${{ secrets.FLAGSMITH_API_KEY }}

A real run against a throwaway self-hosted Flagsmith:

text
would apply  pause-rehearsal: pause “Delivery promise (rehearsal)”  (POST /environments/…/experiments/2/pause/)
applied      pause-rehearsal: pause “Delivery promise (rehearsal)”  (POST /environments/…/experiments/2/pause/)
skip         pause-rehearsal: not running (paused)

Staged rollout of the winner: ramp

Shipping a winner to everyone at once is a big step. Instead, complete the experiment keeping control, then ramp the winner in reviewed steps, checking the health of what you're watching between them:

json
{ "id": "ramp-free-shipping-10", "action": "ramp", "flag": "checkout_cta", "variant": "free_shipping", "percent": 10, "production": true }

Each step is a pull request that changes percent (10 → 50 → 100). A ramp is refused while an experiment is still running on the flag (decide first), and skipped when the flag is already at that share. Measured on a throwaway self-hosted Flagsmith, sampling 200 users after each step: 10% → 12% got the winner; 50% → 44%.