flagsmith-second-opinion 0.1.0__py3-none-any.whl
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- flagsmith_second_opinion-0.1.0.dist-info/METADATA +262 -0
- flagsmith_second_opinion-0.1.0.dist-info/RECORD +40 -0
- flagsmith_second_opinion-0.1.0.dist-info/WHEEL +5 -0
- flagsmith_second_opinion-0.1.0.dist-info/entry_points.txt +2 -0
- flagsmith_second_opinion-0.1.0.dist-info/licenses/LICENSE +11 -0
- flagsmith_second_opinion-0.1.0.dist-info/top_level.txt +1 -0
- second_opinion/__init__.py +5 -0
- second_opinion/checks/__init__.py +23 -0
- second_opinion/checks/base.py +22 -0
- second_opinion/checks/c1_srm.py +43 -0
- second_opinion/checks/c2_primary.py +166 -0
- second_opinion/checks/c3_guardrails.py +127 -0
- second_opinion/checks/c4_segments.py +186 -0
- second_opinion/checks/c5_novelty.py +147 -0
- second_opinion/checks/c6_sequential.py +101 -0
- second_opinion/checks/c7_event_quality.py +156 -0
- second_opinion/cli.py +544 -0
- second_opinion/config.py +134 -0
- second_opinion/flagsmith_api.py +68 -0
- second_opinion/report/__init__.py +17 -0
- second_opinion/report/html_reporter.py +257 -0
- second_opinion/report/json_reporter.py +14 -0
- second_opinion/report/markdown_reporter.py +104 -0
- second_opinion/sim/__init__.py +8 -0
- second_opinion/sim/generator.py +231 -0
- second_opinion/sim/proxy.py +135 -0
- second_opinion/sim/scenarios.py +78 -0
- second_opinion/sources/__init__.py +42 -0
- second_opinion/sources/base.py +92 -0
- second_opinion/sources/clickhouse.py +68 -0
- second_opinion/sources/csv_source.py +310 -0
- second_opinion/sources/postgres.py +180 -0
- second_opinion/stats/__init__.py +41 -0
- second_opinion/stats/bayes.py +188 -0
- second_opinion/stats/flagsmith_replica.py +65 -0
- second_opinion/stats/freq.py +142 -0
- second_opinion/stats/math_utils.py +236 -0
- second_opinion/stats/sequential.py +124 -0
- second_opinion/stats/srm.py +137 -0
- second_opinion/verdict.py +254 -0
|
@@ -0,0 +1,262 @@
|
|
|
1
|
+
Metadata-Version: 2.4
|
|
2
|
+
Name: flagsmith-second-opinion
|
|
3
|
+
Version: 0.1.0
|
|
4
|
+
Summary: A read-only trust layer and deploy gate for Flagsmith experimentation
|
|
5
|
+
Author: Toggle Wars Track 03 Team
|
|
6
|
+
License: BSD-3-Clause
|
|
7
|
+
Classifier: Development Status :: 4 - Beta
|
|
8
|
+
Classifier: Intended Audience :: Developers
|
|
9
|
+
Classifier: License :: OSI Approved :: BSD License
|
|
10
|
+
Classifier: Programming Language :: Python :: 3
|
|
11
|
+
Classifier: Programming Language :: Python :: 3.10
|
|
12
|
+
Classifier: Programming Language :: Python :: 3.11
|
|
13
|
+
Classifier: Programming Language :: Python :: 3.12
|
|
14
|
+
Classifier: Programming Language :: Python :: 3.13
|
|
15
|
+
Requires-Python: >=3.10
|
|
16
|
+
Description-Content-Type: text/markdown
|
|
17
|
+
License-File: LICENSE
|
|
18
|
+
Requires-Dist: pyyaml>=6.0
|
|
19
|
+
Provides-Extra: full
|
|
20
|
+
Requires-Dist: numpy>=1.26.0; extra == "full"
|
|
21
|
+
Requires-Dist: scipy>=1.12.0; extra == "full"
|
|
22
|
+
Requires-Dist: typer>=0.12.0; extra == "full"
|
|
23
|
+
Requires-Dist: rich>=13.7.0; extra == "full"
|
|
24
|
+
Requires-Dist: jinja2>=3.1.0; extra == "full"
|
|
25
|
+
Requires-Dist: httpx>=0.27.0; extra == "full"
|
|
26
|
+
Requires-Dist: duckdb>=0.10.0; extra == "full"
|
|
27
|
+
Provides-Extra: dev
|
|
28
|
+
Requires-Dist: pytest>=8.0.0; extra == "dev"
|
|
29
|
+
Requires-Dist: hypothesis>=6.100.0; extra == "dev"
|
|
30
|
+
Requires-Dist: ruff>=0.4.0; extra == "dev"
|
|
31
|
+
Requires-Dist: mypy>=1.10.0; extra == "dev"
|
|
32
|
+
Requires-Dist: numpy>=1.26.0; extra == "dev"
|
|
33
|
+
Requires-Dist: scipy>=1.12.0; extra == "dev"
|
|
34
|
+
Dynamic: license-file
|
|
35
|
+
|
|
36
|
+
<p align="center">
|
|
37
|
+
<picture>
|
|
38
|
+
<source media="(prefers-color-scheme: dark)" srcset="docs/wemakedevs_flagsmith_logo.svg">
|
|
39
|
+
<source media="(prefers-color-scheme: light)" srcset="docs/wemakedevs_flagsmith_logo_light.svg">
|
|
40
|
+
<img src="docs/wemakedevs_flagsmith_logo.png" alt="WeMakeDevs x Flagsmith Experimentation" width="480"/>
|
|
41
|
+
</picture>
|
|
42
|
+
</p>
|
|
43
|
+
|
|
44
|
+
# Second Opinion
|
|
45
|
+
|
|
46
|
+
> **A read-only trust layer and automated deploy gate for self-hosted Flagsmith Experimentation.**
|
|
47
|
+
> *Toggle Wars Track 03: Build on Open Source Flagsmith*
|
|
48
|
+
|
|
49
|
+
[](https://github.com/ankit-cybertron/Second_Opinion/actions/workflows/ci.yml)
|
|
50
|
+
[](LICENSE)
|
|
51
|
+
[](pyproject.toml)
|
|
52
|
+
|
|
53
|
+
---
|
|
54
|
+
|
|
55
|
+
## What is Second Opinion?
|
|
56
|
+
|
|
57
|
+
Flagsmith makes it easy to run feature-flag experiments. But in production, shipping code whenever a dashboard card turns green can be dangerous:
|
|
58
|
+
|
|
59
|
+
- **Simpson's Paradox:** Overall conversion looks positive (+13.3%), but mobile users suffer a severe -16.2% drop.
|
|
60
|
+
- **Guardrail Regressions:** Purchases increase, but refund requests quietly spike by +81.8%.
|
|
61
|
+
- **Peeking Bias:** Checking daily and stopping early inflates false positive rates up to about 19-20% (varies slightly with Python version).
|
|
62
|
+
|
|
63
|
+
**Second Opinion connects to your self-hosted Flagsmith stack as a read-only companion.** It runs 7 automated integrity checks on your event stream and outputs a clean CI exit code (`Exit 20 VETO` or `Exit 0 SHIP`) to protect your releases.
|
|
64
|
+
|
|
65
|
+
---
|
|
66
|
+
|
|
67
|
+
## System Architecture
|
|
68
|
+
|
|
69
|
+
```mermaid
|
|
70
|
+
flowchart TD
|
|
71
|
+
subgraph Flagsmith["Self-Hosted Flagsmith"]
|
|
72
|
+
Flags["Multivariate Flags (Control vs Treatment)"]
|
|
73
|
+
SDK["Flagsmith SDKs (Client & Server)"]
|
|
74
|
+
Flags --> SDK
|
|
75
|
+
SDK -->|"Emits exposures & events"| PG[("PostgreSQL\n(experimentation_sdkexperimentationevent)")]
|
|
76
|
+
end
|
|
77
|
+
|
|
78
|
+
subgraph SecondOpinion["Second Opinion Trust Layer (Read-Only)"]
|
|
79
|
+
API_Client["Flagsmith API Client\n(Reads configs & baselines)"]
|
|
80
|
+
PG_Reader["PostgreSQL Reader\n(Direct read-only event stream)"]
|
|
81
|
+
|
|
82
|
+
Engine{"Statistical Engine\n(7 Verification Checks)"}
|
|
83
|
+
|
|
84
|
+
API_Client --> Engine
|
|
85
|
+
PG_Reader --> Engine
|
|
86
|
+
|
|
87
|
+
C1["C1: Sample Ratio (Chi-Square)"]
|
|
88
|
+
C2["C2: Primary Effect (Delta Method & Bayes)"]
|
|
89
|
+
C3["C3: Guardrails (Tolerance Loss)"]
|
|
90
|
+
C4["C4: Subgroups (Simpson's Paradox)"]
|
|
91
|
+
C5["C5: Novelty Effect (Decay Tracking)"]
|
|
92
|
+
C6["C6: Peeking (mSPRT Always-Valid)"]
|
|
93
|
+
C7["C7: Data Hygiene (Event Linter)"]
|
|
94
|
+
|
|
95
|
+
Engine --- C1
|
|
96
|
+
Engine --- C2
|
|
97
|
+
Engine --- C3
|
|
98
|
+
Engine --- C4
|
|
99
|
+
Engine --- C5
|
|
100
|
+
Engine --- C6
|
|
101
|
+
Engine --- C7
|
|
102
|
+
end
|
|
103
|
+
|
|
104
|
+
subgraph CICD["Deployment Pipeline (CI/CD)"]
|
|
105
|
+
Engine -->|"Exit 0"| SHIP["🟢 SHIP: Merge PR / Rollout Winner"]
|
|
106
|
+
Engine -->|"Exit 10"| HOLD["🟡 HOLD: Continue Experimentation"]
|
|
107
|
+
Engine -->|"Exit 20"| VETO["🔴 DON'T SHIP: Block Deploy & Alert"]
|
|
108
|
+
end
|
|
109
|
+
```
|
|
110
|
+
|
|
111
|
+
---
|
|
112
|
+
|
|
113
|
+
## How Flagsmith Experimentation Works with Second Opinion
|
|
114
|
+
|
|
115
|
+
1. **Round 01 (Create Flag):** Discovers multivariate flags and baseline controls via Flagsmith's API.
|
|
116
|
+
2. **Round 02 (Pick Metric):** Ingests conversion and guardrail events from the PostgreSQL event table.
|
|
117
|
+
3. **Round 03 (Split Traffic):** Audits `$flag_exposure` events to verify traffic ratios without tracking bias.
|
|
118
|
+
4. **Round 04 (Ship Winner):** Evaluates 7 statistical checks to return a binary CI exit code before rollout.
|
|
119
|
+
|
|
120
|
+
---
|
|
121
|
+
|
|
122
|
+
## The Verdict Card
|
|
123
|
+
|
|
124
|
+
Here is what happens when Second Opinion evaluates an experiment where aggregate conversions look positive, but mobile users are being harmed:
|
|
125
|
+
|
|
126
|
+
```text
|
|
127
|
+
checkout_button environment: Development 24,000 identities
|
|
128
|
+
Flagsmith: Winning, 99.7% chance to win, +13.3% lift
|
|
129
|
+
Second Opinion: DON'T SHIP (Exit 20)
|
|
130
|
+
|
|
131
|
+
VETO VETO: Credible harm detected in subgroup: Segment 'platform=mobile': lift -16.2%, 95% interval (-26.4%, -4.5%), 6000 identities/arm
|
|
132
|
+
info Variant 'treatment' not safe to stop (mSPRT p=0.0716 > 0.05).
|
|
133
|
+
ok Sample ratio matches expected split (chi2=0.00, df=1, p=1.0000).
|
|
134
|
+
ok Second Opinion's Bayesian estimate: chance to win 99.8%, frequentist p=0.0030, lift +13.3%.
|
|
135
|
+
ok No guardrail metrics configured.
|
|
136
|
+
ok Event data quality verified: clean schemas, zero naming anomalies, high integrity.
|
|
137
|
+
```
|
|
138
|
+
|
|
139
|
+
---
|
|
140
|
+
|
|
141
|
+
## 30-Second Quickstart (Zero Setup Needed)
|
|
142
|
+
|
|
143
|
+
Second Opinion includes 7 built-in test fixtures with planted traps so you can test it immediately without needing a database:
|
|
144
|
+
|
|
145
|
+
```bash
|
|
146
|
+
# 1. Clone the repository
|
|
147
|
+
git clone https://github.com/ankit-cybertron/Second_Opinion.git
|
|
148
|
+
cd Second_Opinion
|
|
149
|
+
|
|
150
|
+
# 2. Install
|
|
151
|
+
pip install -e .
|
|
152
|
+
|
|
153
|
+
# 3. Catch Simpson's Paradox / Mobile harm (exits with code 20)
|
|
154
|
+
so analyze --demo hidden-harm
|
|
155
|
+
|
|
156
|
+
# 4. Generate an interactive visual HTML report
|
|
157
|
+
so analyze --demo hidden-harm --format html -o report.html
|
|
158
|
+
open report.html
|
|
159
|
+
```
|
|
160
|
+
|
|
161
|
+
---
|
|
162
|
+
|
|
163
|
+
## The Three Real Traps Caught
|
|
164
|
+
|
|
165
|
+
| Experiment Trap | Flagsmith Dashboard Alone | Second Opinion Trust Layer | Verdict & Action |
|
|
166
|
+
| :--- | :--- | :--- | :--- |
|
|
167
|
+
| **1. Mobile Harm**<br>*Simpson's Paradox* | **Winning, 99.7% chance to win, +13.3% lift**<br>Looks green and invites rollout. | **VETO (C4)**: Slices traits and catches a credible **-16.2%** drop on mobile. | 🔴 **`DON'T SHIP` (Exit 20)**<br>Protects mobile revenue. |
|
|
168
|
+
| **2. Refund Spike**<br>*Guardrail metric* | **Winning (+26.0% lift)**<br>High purchase conversion. | **VETO (C3)**: Catches a **+81.8%** spike in refund requests exceeding tolerance. | 🔴 **`DON'T SHIP` (Exit 20)**<br>Halts costly rollouts. |
|
|
169
|
+
| **3. Peeking Inflation**<br>*A/A test false wins* | **Claims 95%+ win**<br>Early random noise looks real. | **HOLD (C6)**: Always-valid mSPRT sequential test prevents premature stopping. | 🟡 **`HOLD` (Exit 10)**<br>Suppresses the about 19-20% (varies slightly with Python version) naive false win rate. |
|
|
170
|
+
|
|
171
|
+
---
|
|
172
|
+
|
|
173
|
+
## The Seven Planted-Trap Scenarios
|
|
174
|
+
|
|
175
|
+
Every claim is backed by reproducible test runs on golden fixtures:
|
|
176
|
+
|
|
177
|
+
| Scenario | Planted Trap | Flagsmith View | Second Opinion Verdict | Exit Code |
|
|
178
|
+
| :--- | :--- | :--- | :---: | :---: |
|
|
179
|
+
| `honest-win` | Authentic lift (8% $\to$ 10%, 16k users) | Winning (+29.9% lift) | 🟢 **SHIP** | `0` |
|
|
180
|
+
| `srm` | Assignment bug (70/30 split on 50/50 flag) | Unreliable split ($p < 10^{-15}$) | 🔴 **DON'T SHIP** (C1) | `20` |
|
|
181
|
+
| `hidden-harm` | Simpson's Paradox: mobile harmed (-16.2%) | Winning (99.7% win prob, +13.3% lift) | 🔴 **DON'T SHIP** (C4) | `20` |
|
|
182
|
+
| `novelty` | Day 1 surge (+82.7%) collapses by Days 2–3 | Winning overall from Day 1 spike | 🔴 **DON'T SHIP** (C5) | `20` |
|
|
183
|
+
| `guardrail` | Purchases up (+26.0%), but refunds spike +81.8% | Winning on purchase metric (99.5% win) | 🔴 **DON'T SHIP** (C3) | `20` |
|
|
184
|
+
| `dirty-events` | 15% conversions sent as `Signup_Completed` | Instrumentation case defect | 🟡 **HOLD** (C7) | `10` |
|
|
185
|
+
| `peeking-aa` | True A/A test under daily peeking | False win spikes at early looks | 🟡 **HOLD** (C6) | `10` |
|
|
186
|
+
|
|
187
|
+
---
|
|
188
|
+
|
|
189
|
+
## Empirical Statistical Validation
|
|
190
|
+
|
|
191
|
+
From `scripts/validate.py` (2,000 A/A Monte Carlo simulations with an 8% base rate):
|
|
192
|
+
|
|
193
|
+
```text
|
|
194
|
+
Results across 2,000 A/A replicates:
|
|
195
|
+
Naive peeking false win rate (14 daily looks): 19.60% (about 19-20% (varies slightly with Python version))
|
|
196
|
+
Second Opinion mSPRT false alarm rate (20 looks): 1.80% (about 1-2%)
|
|
197
|
+
Full Engine false SHIP rate (completed tests): 0.15% (Strictly <= 5.0%)
|
|
198
|
+
```
|
|
199
|
+
|
|
200
|
+
---
|
|
201
|
+
|
|
202
|
+
## The Seven Checks
|
|
203
|
+
|
|
204
|
+
Second Opinion evaluates seven safety checks before clearing an experiment:
|
|
205
|
+
|
|
206
|
+
- **C1: Sample Ratio (SRM):** Verifies traffic split matches configuration ($p < 0.001$ = Blocker).
|
|
207
|
+
- **C2: Primary Effect:** Computes delta-method lift & Bayesian win probability (matches Flagsmith $< 10^{-6}$).
|
|
208
|
+
- **C3: Guardrail Protection:** Ensures secondary metrics (e.g. refunds, latency) do not exceed tolerance.
|
|
209
|
+
- **C4: Subgroup Harm:** Checks segments for Simpson's paradox with Benjamini-Hochberg FDR control.
|
|
210
|
+
- **C5: Novelty Decay:** Compares Day 1 lift against Days 2–3 to catch early novelty surges that fade.
|
|
211
|
+
- **C6: Sequential Stopping:** mSPRT always-valid $p$-value prevents false wins from daily peeking.
|
|
212
|
+
- **C7: Data Hygiene:** Lints for casing mismatches (e.g. `Signup_Completed` vs `signup_completed`) and dropped events.
|
|
213
|
+
|
|
214
|
+
---
|
|
215
|
+
|
|
216
|
+
## CI/CD Deploy Gate
|
|
217
|
+
|
|
218
|
+
Add Second Opinion directly to your deployment pipeline to fail pull requests on `Exit 20`:
|
|
219
|
+
|
|
220
|
+
```yaml
|
|
221
|
+
name: experiment-gate
|
|
222
|
+
on: pull_request
|
|
223
|
+
|
|
224
|
+
jobs:
|
|
225
|
+
gate:
|
|
226
|
+
runs-on: ubuntu-latest
|
|
227
|
+
steps:
|
|
228
|
+
- uses: actions/checkout@v4
|
|
229
|
+
- uses: ./
|
|
230
|
+
with:
|
|
231
|
+
experiment: checkout_button
|
|
232
|
+
fail-on: dont-ship
|
|
233
|
+
env:
|
|
234
|
+
SO_DATABASE_URL: ${{ secrets.SO_DATABASE_URL }}
|
|
235
|
+
```
|
|
236
|
+
|
|
237
|
+
| Verdict | Exit Code | Action in CI |
|
|
238
|
+
| :--- | :---: | :--- |
|
|
239
|
+
| `SHIP` | `0` | 🟢 Automatically merge or deploy winner |
|
|
240
|
+
| `HOLD` | `10` | 🟡 Keep experiment running; data not ready |
|
|
241
|
+
| `DON'T SHIP` | `20` | 🔴 Block rollout; notify engineering team |
|
|
242
|
+
|
|
243
|
+
---
|
|
244
|
+
|
|
245
|
+
## Track 03: Build on Open Source Flagsmith
|
|
246
|
+
|
|
247
|
+
<p align="center">
|
|
248
|
+
<img src="docs/wemakedevs_flagsmith_logo.png" alt="WeMakeDevs x Flagsmith Experiment" width="360"/>
|
|
249
|
+
</p>
|
|
250
|
+
|
|
251
|
+
Second Opinion is designed specifically for **Track 03** of the WeMakeDevs Flagsmith Hackathon:
|
|
252
|
+
|
|
253
|
+
- **Self-Hosted First:** Connects directly to self-hosted Flagsmith API and PostgreSQL event stream.
|
|
254
|
+
- **Zero-Dependency Quickstart:** Evaluates offline in 15 seconds with 7 frozen golden fixtures.
|
|
255
|
+
- **Non-Invasive:** 100% read-only. Zero database migrations, zero schema changes.
|
|
256
|
+
- **Mathematical Parity:** Verified to match Flagsmith's server-side delta-method calculations within $|\Delta| < 10^{-6}$.
|
|
257
|
+
|
|
258
|
+
---
|
|
259
|
+
|
|
260
|
+
## License
|
|
261
|
+
|
|
262
|
+
BSD-3-Clause License. Open source and free for enterprise or community use.
|
|
@@ -0,0 +1,40 @@
|
|
|
1
|
+
flagsmith_second_opinion-0.1.0.dist-info/licenses/LICENSE,sha256=qs4QKQkiwXii4mcrs07Ye-B_19-mJ7hiiG-P8TtbizM,1463
|
|
2
|
+
second_opinion/__init__.py,sha256=dwNiGqEEZUSMRcb9igt6fbHm3X0NzOpkWR1QWRgKodU,118
|
|
3
|
+
second_opinion/cli.py,sha256=0yMRa3K5skJpv4Lx_k_e-z3AIJr5I4GssHTI67JFgyY,19705
|
|
4
|
+
second_opinion/config.py,sha256=KFJ6C-Etbi0daxv3UAT491pEZauM7NBdj8kvVDQOoJg,4378
|
|
5
|
+
second_opinion/flagsmith_api.py,sha256=V7ZFXF_WBYqEy6DbdVXtAhGq4-yg8AAnT4bHTsG70qw,2518
|
|
6
|
+
second_opinion/verdict.py,sha256=3eohA8JGknhR2Ogchy81fe7TGL70wjG7PAo6eV6WKtc,9288
|
|
7
|
+
second_opinion/checks/__init__.py,sha256=PwYskupzjSZURKtbyRI7HHxSpDg9avrUoXoZaE4Ynjc,788
|
|
8
|
+
second_opinion/checks/base.py,sha256=wiQR_2iLbU2IDI0bxqZf5iqePyYaR_atPOMqIfzd5jo,530
|
|
9
|
+
second_opinion/checks/c1_srm.py,sha256=G_ZRBKcZbhYEM2oDgNfdwnwuHGp0lPG2ROWyejHK7gE,1226
|
|
10
|
+
second_opinion/checks/c2_primary.py,sha256=hhLtXnN7cnQpbIFIqFXUScjDhSGOmdzaOKiRBukcFCM,5796
|
|
11
|
+
second_opinion/checks/c3_guardrails.py,sha256=2oJmxnj40UypiOLx7pc2GjUllu6B0aP5h-U9B7lNTW8,4355
|
|
12
|
+
second_opinion/checks/c4_segments.py,sha256=Ujvy6DCfYfYCv_PwYVC2DRt3kB9RAQp80nop-2D5QQw,7031
|
|
13
|
+
second_opinion/checks/c5_novelty.py,sha256=hafrNCBydtA4RZFt7AKu8lv7OwS3T3qWkrhei4Q4Y4I,5582
|
|
14
|
+
second_opinion/checks/c6_sequential.py,sha256=TrLIRj1LTQxqnB4VGI0Ku-7TpAG0hhjqj504pwxQyqU,3349
|
|
15
|
+
second_opinion/checks/c7_event_quality.py,sha256=QnKIFZt7rwgqisLh7v7txuYwLieudAs3-_Ypzmv2rwM,6562
|
|
16
|
+
second_opinion/report/__init__.py,sha256=0Dbs6oMBn8_g1ZDYKk8WJn0AHrQ7WiwWsu90IQ6hQPc,414
|
|
17
|
+
second_opinion/report/html_reporter.py,sha256=vfOB1Lz4fiMlH3yAbNemM6uYwfkS7j-Soqmw_5FlEzA,10891
|
|
18
|
+
second_opinion/report/json_reporter.py,sha256=G0PrD_-n3qatrDhOf44gS5rvyhmeHMFQO2wtTPR2Bdk,337
|
|
19
|
+
second_opinion/report/markdown_reporter.py,sha256=ldaTprqDrHsRGM8FTML2MAx5eE-W6ogd_QYBzHuaTcA,4629
|
|
20
|
+
second_opinion/sim/__init__.py,sha256=BCHbU2Pr0ChT5pn5HtY6IkyaV-fTnJTzT_XRZgwAAQc,287
|
|
21
|
+
second_opinion/sim/generator.py,sha256=58hpFTwlUOqIeeOjrNSgkyxgIaASp-OMSDYEabtZIew,9878
|
|
22
|
+
second_opinion/sim/proxy.py,sha256=I98ZfM30OiyJ9_x5DkSycLb-D5KJsBmCQKm-Sh6pofo,4755
|
|
23
|
+
second_opinion/sim/scenarios.py,sha256=bzy0U3qyxOKfS3gvoKYk-LB5h0ooWIQlvnJG5arSFI0,3056
|
|
24
|
+
second_opinion/sources/__init__.py,sha256=MVXGIe9IEQH0jTScplsyaz_q124kElEFl2iHXKSjtNE,1252
|
|
25
|
+
second_opinion/sources/base.py,sha256=dAt9uhTiH8va5gyQh9zsMJ0mVs9nHz-M9FnYkHzBcgk,2924
|
|
26
|
+
second_opinion/sources/clickhouse.py,sha256=Qdk8UUgACn2Zz22dyXryDRkgvPF5NNtmyAOcd7kaVgA,2142
|
|
27
|
+
second_opinion/sources/csv_source.py,sha256=FeimFvEYO_adHU5357Vcq03VTJH9hs6e8nvoZCfljiQ,11921
|
|
28
|
+
second_opinion/sources/postgres.py,sha256=qOA6zkASyidGtc8PlxbJFUB5OyygV8v_J12p7NjrGbo,7629
|
|
29
|
+
second_opinion/stats/__init__.py,sha256=zEw3buRbONIw-X1aT3mfLzoYufrfiKaeSBUUTqUpHsk,938
|
|
30
|
+
second_opinion/stats/bayes.py,sha256=AljEUbiCf0Q093cUMHrA6q9KFKBtxtnrvjPKk9vBBnU,6582
|
|
31
|
+
second_opinion/stats/flagsmith_replica.py,sha256=ULN8ucSIz0EvY_DBSXzjgMewJIwmvr8mqzCqLtDa_b0,1867
|
|
32
|
+
second_opinion/stats/freq.py,sha256=mr-fr5Frp-kJtjOntJ4EOQEgC2tzjUG3z6BAxrkBR4A,3723
|
|
33
|
+
second_opinion/stats/math_utils.py,sha256=hvZvCCRtR0886ogF5lv2cxgza4EaCpGos9K_6s5csW0,7156
|
|
34
|
+
second_opinion/stats/sequential.py,sha256=E-CdixGElgivRYwuXzl8rseUDTCkJoOLzJ-Dud6DBuE,3796
|
|
35
|
+
second_opinion/stats/srm.py,sha256=lWFQ-_eH3O5i_jeuxIIMfMik3Q0FSdiXEgSUoqZvAkU,3991
|
|
36
|
+
flagsmith_second_opinion-0.1.0.dist-info/METADATA,sha256=f7iIzSVV5GfVRY6LKnQStvdXxeT9XViPHMgSXA8hXdc,11294
|
|
37
|
+
flagsmith_second_opinion-0.1.0.dist-info/WHEEL,sha256=mOdXR9C3wMVov7cSY8PY-XCzo3SmmWCHvGQr5csJoao,91
|
|
38
|
+
flagsmith_second_opinion-0.1.0.dist-info/entry_points.txt,sha256=b4uKBZGJ1BHlVApbn7B6GF-ZVPg6WmSu1TLW9loe4-I,47
|
|
39
|
+
flagsmith_second_opinion-0.1.0.dist-info/top_level.txt,sha256=ak0oO3zyp2nMLSMT7mFfx31yXFZBSjQVO7JYnlhf-2g,15
|
|
40
|
+
flagsmith_second_opinion-0.1.0.dist-info/RECORD,,
|
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
Copyright 2026 Ankit Kumar Tiwari
|
|
2
|
+
|
|
3
|
+
Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met:
|
|
4
|
+
|
|
5
|
+
1. Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer.
|
|
6
|
+
|
|
7
|
+
2. Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution.
|
|
8
|
+
|
|
9
|
+
3. Neither the name of the copyright holder nor the names of its contributors may be used to endorse or promote products derived from this software without specific prior written permission.
|
|
10
|
+
|
|
11
|
+
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
second_opinion
|
|
@@ -0,0 +1,23 @@
|
|
|
1
|
+
"""
|
|
2
|
+
Second Opinion experiment checks (C1 to C7).
|
|
3
|
+
"""
|
|
4
|
+
|
|
5
|
+
from second_opinion.checks.base import CheckResult
|
|
6
|
+
from second_opinion.checks.c1_srm import check_sample_ratio
|
|
7
|
+
from second_opinion.checks.c2_primary import check_primary_effect
|
|
8
|
+
from second_opinion.checks.c3_guardrails import check_guardrails
|
|
9
|
+
from second_opinion.checks.c4_segments import check_segments
|
|
10
|
+
from second_opinion.checks.c5_novelty import check_novelty_decay
|
|
11
|
+
from second_opinion.checks.c6_sequential import check_sequential_validity
|
|
12
|
+
from second_opinion.checks.c7_event_quality import check_event_quality
|
|
13
|
+
|
|
14
|
+
__all__ = [
|
|
15
|
+
"CheckResult",
|
|
16
|
+
"check_event_quality",
|
|
17
|
+
"check_guardrails",
|
|
18
|
+
"check_novelty_decay",
|
|
19
|
+
"check_primary_effect",
|
|
20
|
+
"check_sample_ratio",
|
|
21
|
+
"check_segments",
|
|
22
|
+
"check_sequential_validity",
|
|
23
|
+
]
|
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
"""
|
|
2
|
+
Base classes for Second Opinion experiment checks.
|
|
3
|
+
"""
|
|
4
|
+
|
|
5
|
+
from __future__ import annotations
|
|
6
|
+
|
|
7
|
+
from dataclasses import dataclass, field
|
|
8
|
+
from typing import Any
|
|
9
|
+
|
|
10
|
+
|
|
11
|
+
@dataclass
|
|
12
|
+
class CheckResult:
|
|
13
|
+
check_id: str
|
|
14
|
+
name: str
|
|
15
|
+
status: str # "PASS", "WARNING", "VETO", "BLOCKER", "SKIPPED"
|
|
16
|
+
severity: str # "NONE", "INFO", "WARNING", "HIGH", "CRITICAL"
|
|
17
|
+
message: str
|
|
18
|
+
evidence: dict[str, Any] = field(default_factory=dict)
|
|
19
|
+
|
|
20
|
+
@property
|
|
21
|
+
def is_veto(self) -> bool:
|
|
22
|
+
return self.status in ("VETO", "BLOCKER")
|
|
@@ -0,0 +1,43 @@
|
|
|
1
|
+
"""
|
|
2
|
+
Check C1: Sample Ratio Mismatch (SRM).
|
|
3
|
+
Validates that traffic was split according to configured weights.
|
|
4
|
+
"""
|
|
5
|
+
|
|
6
|
+
from __future__ import annotations
|
|
7
|
+
|
|
8
|
+
from second_opinion.checks.base import CheckResult
|
|
9
|
+
from second_opinion.sources.base import ExperimentData
|
|
10
|
+
from second_opinion.stats.srm import check_srm
|
|
11
|
+
|
|
12
|
+
|
|
13
|
+
def check_sample_ratio(
|
|
14
|
+
data: ExperimentData,
|
|
15
|
+
expected_split: dict[str, float] | None = None,
|
|
16
|
+
) -> CheckResult:
|
|
17
|
+
observed_counts = {v: data.arms[v].identity_count for v in data.variants if v in data.arms}
|
|
18
|
+
|
|
19
|
+
srm_res = check_srm(observed_counts, expected_split=expected_split)
|
|
20
|
+
|
|
21
|
+
if srm_res.status == "BLOCKER":
|
|
22
|
+
severity = "CRITICAL"
|
|
23
|
+
elif srm_res.status == "WARNING":
|
|
24
|
+
severity = "WARNING"
|
|
25
|
+
else:
|
|
26
|
+
severity = "NONE"
|
|
27
|
+
|
|
28
|
+
evidence = {
|
|
29
|
+
"observed_counts": observed_counts,
|
|
30
|
+
"expected_counts": srm_res.expected,
|
|
31
|
+
"chi2_statistic": srm_res.statistic,
|
|
32
|
+
"degrees_of_freedom": srm_res.degrees_of_freedom,
|
|
33
|
+
"p_value": srm_res.p_value,
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
return CheckResult(
|
|
37
|
+
check_id="C1",
|
|
38
|
+
name="Sample Ratio Mismatch",
|
|
39
|
+
status=srm_res.status,
|
|
40
|
+
severity=severity,
|
|
41
|
+
message=srm_res.message,
|
|
42
|
+
evidence=evidence,
|
|
43
|
+
)
|
|
@@ -0,0 +1,166 @@
|
|
|
1
|
+
"""
|
|
2
|
+
Check C2: Primary Effect Evaluation.
|
|
3
|
+
Combines Bayesian Beta-Binomial and Frequentist z-test cross-validation.
|
|
4
|
+
"""
|
|
5
|
+
|
|
6
|
+
from __future__ import annotations
|
|
7
|
+
|
|
8
|
+
from second_opinion.checks.base import CheckResult
|
|
9
|
+
from second_opinion.sources.base import ExperimentData
|
|
10
|
+
from second_opinion.stats.bayes import analyze_beta_binomial
|
|
11
|
+
from second_opinion.stats.freq import two_proportion_z_test
|
|
12
|
+
|
|
13
|
+
|
|
14
|
+
def check_primary_effect(
|
|
15
|
+
data: ExperimentData,
|
|
16
|
+
primary_metric: str,
|
|
17
|
+
control_variant: str | None = None,
|
|
18
|
+
expected_loss_cap: float = 0.001,
|
|
19
|
+
) -> CheckResult:
|
|
20
|
+
variants = [v for v in data.variants if v in data.arms]
|
|
21
|
+
if len(variants) < 2:
|
|
22
|
+
return CheckResult(
|
|
23
|
+
check_id="C2",
|
|
24
|
+
name="Primary Effect",
|
|
25
|
+
status="SKIPPED",
|
|
26
|
+
severity="NONE",
|
|
27
|
+
message="Not enough variants to analyze primary effect.",
|
|
28
|
+
evidence={},
|
|
29
|
+
)
|
|
30
|
+
|
|
31
|
+
# Determine control variant
|
|
32
|
+
ctrl_v = control_variant
|
|
33
|
+
if not ctrl_v or ctrl_v not in data.arms:
|
|
34
|
+
# Default: variant containing 'control', 'baseline', or first alphabetically
|
|
35
|
+
candidates = [v for v in variants if "control" in v.lower() or "base" in v.lower()]
|
|
36
|
+
ctrl_v = candidates[0] if candidates else variants[0]
|
|
37
|
+
|
|
38
|
+
treatment_variants = [v for v in variants if v != ctrl_v]
|
|
39
|
+
if not treatment_variants:
|
|
40
|
+
return CheckResult(
|
|
41
|
+
check_id="C2",
|
|
42
|
+
name="Primary Effect",
|
|
43
|
+
status="SKIPPED",
|
|
44
|
+
severity="NONE",
|
|
45
|
+
message="No treatment variant found distinct from control.",
|
|
46
|
+
evidence={"control": ctrl_v},
|
|
47
|
+
)
|
|
48
|
+
|
|
49
|
+
ctrl_arm = data.arms[ctrl_v]
|
|
50
|
+
ctrl_n = ctrl_arm.identity_count
|
|
51
|
+
ctrl_conv = ctrl_arm.conversions.get(primary_metric, 0)
|
|
52
|
+
|
|
53
|
+
# For multi-armed tests, analyze each treatment vs control
|
|
54
|
+
evidence_variants = {}
|
|
55
|
+
any_veto = False
|
|
56
|
+
any_win = False
|
|
57
|
+
disagreements = []
|
|
58
|
+
messages = []
|
|
59
|
+
|
|
60
|
+
for trt_v in treatment_variants:
|
|
61
|
+
trt_arm = data.arms[trt_v]
|
|
62
|
+
trt_n = trt_arm.identity_count
|
|
63
|
+
trt_conv = trt_arm.conversions.get(primary_metric, 0)
|
|
64
|
+
|
|
65
|
+
# Bayesian analysis
|
|
66
|
+
bayes_res = analyze_beta_binomial(
|
|
67
|
+
control_conversions=ctrl_conv,
|
|
68
|
+
control_total=ctrl_n,
|
|
69
|
+
variant_conversions=trt_conv,
|
|
70
|
+
variant_total=trt_n,
|
|
71
|
+
variant_name=trt_v,
|
|
72
|
+
control_name=ctrl_v,
|
|
73
|
+
)
|
|
74
|
+
|
|
75
|
+
# Frequentist analysis
|
|
76
|
+
freq_res = two_proportion_z_test(
|
|
77
|
+
control_conversions=ctrl_conv,
|
|
78
|
+
control_total=ctrl_n,
|
|
79
|
+
variant_conversions=trt_conv,
|
|
80
|
+
variant_total=trt_n,
|
|
81
|
+
)
|
|
82
|
+
|
|
83
|
+
evidence_variants[trt_v] = {
|
|
84
|
+
"control_variant": ctrl_v,
|
|
85
|
+
"control_n": ctrl_n,
|
|
86
|
+
"control_conversions": ctrl_conv,
|
|
87
|
+
"control_rate": bayes_res.control_rate,
|
|
88
|
+
"treatment_n": trt_n,
|
|
89
|
+
"treatment_conversions": trt_conv,
|
|
90
|
+
"treatment_rate": bayes_res.variant_rate,
|
|
91
|
+
"relative_lift": bayes_res.relative_lift,
|
|
92
|
+
"absolute_lift": bayes_res.absolute_lift,
|
|
93
|
+
"chance_to_win": bayes_res.chance_to_win,
|
|
94
|
+
"expected_loss": bayes_res.expected_loss,
|
|
95
|
+
"credible_interval_95": bayes_res.credible_interval_95,
|
|
96
|
+
"frequentist_p_value": freq_res.p_value,
|
|
97
|
+
"frequentist_z_stat": freq_res.z_statistic,
|
|
98
|
+
"frequentist_ci_diff": freq_res.confidence_interval,
|
|
99
|
+
}
|
|
100
|
+
|
|
101
|
+
# Check conditions
|
|
102
|
+
# 1. Credibly worse -> VETO
|
|
103
|
+
if bayes_res.chance_to_win <= 0.05 or (freq_res.p_value < 0.05 and bayes_res.absolute_lift < 0):
|
|
104
|
+
any_veto = True
|
|
105
|
+
messages.append(
|
|
106
|
+
f"Variant '{trt_v}' is credibly worse than control (chance to win {bayes_res.chance_to_win*100:.1f}%, lift {bayes_res.relative_lift*100:.1f}%)."
|
|
107
|
+
)
|
|
108
|
+
continue
|
|
109
|
+
|
|
110
|
+
# 2. Check for win: chance to win >= 95% AND p < 0.05
|
|
111
|
+
bayes_win = bayes_res.chance_to_win >= 0.95
|
|
112
|
+
freq_win = freq_res.p_value < 0.05 and bayes_res.absolute_lift > 0
|
|
113
|
+
|
|
114
|
+
if bayes_win and freq_win:
|
|
115
|
+
any_win = True
|
|
116
|
+
messages.append(
|
|
117
|
+
f"Second Opinion's Bayesian estimate: chance to win {bayes_res.chance_to_win*100:.1f}%, frequentist p={freq_res.p_value:.4f}, lift +{bayes_res.relative_lift*100:.1f}%."
|
|
118
|
+
)
|
|
119
|
+
elif bayes_win != freq_win:
|
|
120
|
+
disagreements.append(trt_v)
|
|
121
|
+
messages.append(
|
|
122
|
+
f"Variant '{trt_v}' has estimator disagreement: Bayesian chance to win {bayes_res.chance_to_win*100:.1f}%, but frequentist p={freq_res.p_value:.4f}."
|
|
123
|
+
)
|
|
124
|
+
else:
|
|
125
|
+
messages.append(
|
|
126
|
+
f"Variant '{trt_v}' is inconclusive: chance to win {bayes_res.chance_to_win*100:.1f}%, p={freq_res.p_value:.4f}."
|
|
127
|
+
)
|
|
128
|
+
|
|
129
|
+
if any_veto:
|
|
130
|
+
return CheckResult(
|
|
131
|
+
check_id="C2",
|
|
132
|
+
name="Primary Effect",
|
|
133
|
+
status="VETO",
|
|
134
|
+
severity="CRITICAL",
|
|
135
|
+
message="; ".join(messages),
|
|
136
|
+
evidence=evidence_variants,
|
|
137
|
+
)
|
|
138
|
+
|
|
139
|
+
if disagreements:
|
|
140
|
+
return CheckResult(
|
|
141
|
+
check_id="C2",
|
|
142
|
+
name="Primary Effect",
|
|
143
|
+
status="WARNING",
|
|
144
|
+
severity="WARNING",
|
|
145
|
+
message="; ".join(messages),
|
|
146
|
+
evidence=evidence_variants,
|
|
147
|
+
)
|
|
148
|
+
|
|
149
|
+
if any_win:
|
|
150
|
+
return CheckResult(
|
|
151
|
+
check_id="C2",
|
|
152
|
+
name="Primary Effect",
|
|
153
|
+
status="PASS",
|
|
154
|
+
severity="NONE",
|
|
155
|
+
message="; ".join(messages),
|
|
156
|
+
evidence=evidence_variants,
|
|
157
|
+
)
|
|
158
|
+
|
|
159
|
+
return CheckResult(
|
|
160
|
+
check_id="C2",
|
|
161
|
+
name="Primary Effect",
|
|
162
|
+
status="PASS",
|
|
163
|
+
severity="NONE",
|
|
164
|
+
message="; ".join(messages),
|
|
165
|
+
evidence=evidence_variants,
|
|
166
|
+
)
|