File size: 10,331 Bytes
bf3cbf1
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
"""Integration tests for real functionality: persistence, CSV import, and statistical attribution."""

from __future__ import annotations

import os
import tempfile
from io import StringIO
from pathlib import Path

import pytest

from spinor_os import ExperimentationOS, PersistenceManager, import_events_csv
from spinor_os.config import AttributionMethod, EventType


def test_csv_import_allocates_missions_and_records_events():
    os = ExperimentationOS()
    os.register_employee("import-001", "field-scientist", "northeast")
    hypothesis = os.propose_hypothesis(
        statement="Imported outreach increases appointments.",
        causal_claim="Tailored outreach causes higher appointment rate.",
        predicted_effect={
            "metric": "appointment_rate",
            "direction": "increase",
            "magnitude": 0.1,
            "unit": "percentage_point",
            "timing": "7d",
        },
        employee_owner="import-001",
        falsification_criteria=["No increase observed."],
        customer_segment="enterprise",
        territory="northeast",
        modification="imported_outreach",
    )
    experiment = os.start_experiment(hypothesis.hypothesis_id)

    csv_text = f"""experiment_id,actor_id,event_type,outcome_value,metric,execution_quality,actor_role
{experiment.experiment_id},import-001,OUTCOME_OBSERVED,0.15,appointment_rate,0.88,field-scientist
{experiment.experiment_id},import-001,OUTCOME_OBSERVED,0.18,appointment_rate,0.90,field-scientist
"""
    summary = import_events_csv(os, StringIO(csv_text), experiment_id=experiment.experiment_id)
    assert summary["rows_read"] == 2
    assert summary["rows_ingested"] == 2
    assert len(summary["event_ids"]) == 2
    assert not summary["errors"]
    assert len(os.events) == 2


def test_real_attribution_ols_and_diff_in_diff():
    os = ExperimentationOS()
    os.register_employee("alice", "field-scientist", "northeast")
    os.register_employee("bob", "field-scientist", "southwest")

    hypothesis = os.propose_hypothesis(
        statement="X lifts Y in enterprise segment",
        causal_claim="X causes Y",
        predicted_effect={
            "metric": "y_rate",
            "direction": "increase",
            "magnitude": 0.1,
            "unit": "pp",
            "timing": "7d",
        },
        employee_owner="alice",
        falsification_criteria=["no effect"],
        customer_segment="enterprise",
        territory="northeast",
        modification="treatment_a",
    )

    treat = os.start_experiment(hypothesis.hypothesis_id)
    control = os.start_experiment(hypothesis.hypothesis_id)

    m_t = os.allocate_mission(
        treat.experiment_id,
        "alice",
        [
            {
                "hypothesis_id": hypothesis.hypothesis_id,
                "modification": "treatment_a",
                "customer_segment": "enterprise",
                "territory": "northeast",
                "timing": "2026-W31",
                "resource_allocation": 1.0,
            }
        ],
    )
    m_c = os.allocate_mission(
        control.experiment_id,
        "bob",
        [
            {
                "hypothesis_id": hypothesis.hypothesis_id,
                "modification": "control",
                "customer_segment": "enterprise",
                "territory": "southwest",
                "timing": "2026-W31",
                "resource_allocation": 1.0,
            }
        ],
    )

    os.record_event(
        treat.experiment_id,
        m_t.mission_id,
        EventType.OUTCOME_OBSERVED,
        "alice",
        outcome_value=0.05,
        metric="y_rate",
        execution_quality=0.9,
    )
    os.record_event(
        control.experiment_id,
        m_c.mission_id,
        EventType.OUTCOME_OBSERVED,
        "bob",
        outcome_value=0.07,
        metric="y_rate",
        execution_quality=0.9,
    )
    os.record_event(
        treat.experiment_id,
        m_t.mission_id,
        EventType.OUTCOME_OBSERVED,
        "alice",
        outcome_value=0.35,
        metric="y_rate",
        execution_quality=0.9,
    )
    os.record_event(
        control.experiment_id,
        m_c.mission_id,
        EventType.OUTCOME_OBSERVED,
        "bob",
        outcome_value=0.07,
        metric="y_rate",
        execution_quality=0.85,
    )

    claim_ols = os.attribute(
        treat.experiment_id,
        "y_rate",
        0.35,
        0.05,
        AttributionMethod.OLS,
        0.95,
        falsification_survived=True,
    )
    assert claim_ols.real_attribution is not None
    assert claim_ols.real_attribution["method"] == "ols_regression"
    assert claim_ols.real_attribution["estimated_effect"] > 0
    assert claim_ols.real_attribution["n_observations"] >= 4

    claim_did = os.attribute(
        treat.experiment_id,
        "y_rate",
        0.35,
        0.05,
        AttributionMethod.DIFF_IN_DIFF,
        0.95,
        falsification_survived=True,
    )
    assert claim_did.real_attribution is not None
    assert claim_did.real_attribution["method"] == "difference_in_differences"
    assert claim_did.real_attribution["estimated_effect"] > 0
    assert claim_did.real_attribution["n_observations"] >= 4


def test_persistence_reloads_imported_data():
    with tempfile.TemporaryDirectory() as tmp:
        db = Path(tmp) / "spinor_real.sqlite"
        os1 = ExperimentationOS(persistence=PersistenceManager(db))
        os1.register_employee("persist-001", "field-scientist", "northeast")
        hypothesis = os1.propose_hypothesis(
            statement="Persistent outreach works.",
            causal_claim="Outreach causes lift.",
            predicted_effect={
                "metric": "lift",
                "direction": "increase",
                "magnitude": 0.1,
                "unit": "pp",
                "timing": "7d",
            },
            employee_owner="persist-001",
            falsification_criteria=["no lift"],
            customer_segment="enterprise",
            territory="northeast",
            modification="outreach",
        )
        experiment = os1.start_experiment(hypothesis.hypothesis_id)

        csv_text = f"""experiment_id,actor_id,event_type,outcome_value,metric,execution_quality
{experiment.experiment_id},persist-001,OUTCOME_OBSERVED,0.12,lift,0.88
"""
        summary = import_events_csv(os1, StringIO(csv_text), experiment_id=experiment.experiment_id)
        assert summary["rows_ingested"] == 1

        # Reopen from the same SQLite file.
        os2 = ExperimentationOS(persistence=PersistenceManager(db))
        assert "persist-001" in os2.employees
        assert experiment.experiment_id in os2.experiments
        assert len(os2.events) == 1


def test_server_import_and_real_attribution(client):
    """Live-server style verification using the FastAPI TestClient."""
    # Register employee and hypothesis.
    r = client.post("/employees", json={
        "employee_id": "real-api-001",
        "role": "field-scientist",
        "territory": "northeast",
    })
    assert r.status_code == 200

    r = client.post("/hypotheses", json={
        "statement": "Real outreach increases conversions",
        "causal_claim": "Outreach causes conversions",
        "predicted_effect": {
            "metric": "real_conversion_rate",
            "direction": "increase",
            "magnitude": 0.1,
            "unit": "pp",
            "timing": "7d",
        },
        "employee_owner": "real-api-001",
        "falsification_criteria": ["No lift"],
        "customer_segment": "enterprise",
        "territory": "northeast",
        "modification": "real_outreach",
    })
    assert r.status_code == 200
    hypothesis_id = r.json()["hypothesis_id"]

    # Create treatment and control experiments.
    treat = client.post("/experiments", json={"hypothesis_id": hypothesis_id}).json()
    control = client.post("/experiments", json={"hypothesis_id": hypothesis_id}).json()
    treat_id = treat["experiment_id"]
    control_id = control["experiment_id"]

    # Allocate missions.
    m_t = client.post(f"/experiments/{treat_id}/missions", json={
        "employee_id": "real-api-001",
        "candidates": [{
            "hypothesis_id": hypothesis_id,
            "modification": "real_outreach",
            "customer_segment": "enterprise",
            "territory": "northeast",
            "timing": "2026-W31",
            "resource_allocation": 1.0,
        }],
    }).json()
    m_c = client.post(f"/experiments/{control_id}/missions", json={
        "employee_id": "real-api-001",
        "candidates": [{
            "hypothesis_id": hypothesis_id,
            "modification": "control",
            "customer_segment": "enterprise",
            "territory": "northeast",
            "timing": "2026-W31",
            "resource_allocation": 1.0,
        }],
    }).json()

    # Record pre and post outcomes.
    for (exp, mid, val, eq) in [
        (treat_id, m_t["mission_id"], 0.05, 0.9),
        (control_id, m_c["mission_id"], 0.06, 0.9),
        (treat_id, m_t["mission_id"], 0.35, 0.9),
        (control_id, m_c["mission_id"], 0.07, 0.85),
    ]:
        r = client.post(f"/experiments/{exp}/events", json={
            "mission_id": mid,
            "event_type": "outcome_observed",
            "actor_id": "real-api-001",
            "outcome_value": val,
            "metric": "real_conversion_rate",
            "execution_quality": eq,
        })
        assert r.status_code == 200

    # Compute real OLS attribution.
    r = client.post(f"/experiments/{treat_id}/attributes", json={
        "outcome_metric": "real_conversion_rate",
        "outcome_value": 0.35,
        "counterfactual_estimate": 0.05,
        "method": "ols",
        "confidence": 0.95,
        "falsification_survived": True,
    })
    assert r.status_code == 200
    data = r.json()
    assert data["real_attribution"] is not None
    assert data["real_attribution"]["method"] == "ols_regression"
    assert data["real_attribution"]["estimated_effect"] > 0

    # Force a save and check persistence status.
    r = client.post("/persistence/save")
    assert r.status_code == 200
    r = client.get("/persistence/status")
    assert r.status_code == 200
    status = r.json()
    assert status["persistence_enabled"] is True
    assert status["experiments"] >= 2