forked from ChelseaKR/outcome-receipts
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgenerate_workflow_compat_fixtures.py
More file actions
358 lines (325 loc) · 12.1 KB
/
Copy pathgenerate_workflow_compat_fixtures.py
File metadata and controls
358 lines (325 loc) · 12.1 KB
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
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
"""Generate stable version-1.0 fixtures for every evidence workflow artifact."""
from __future__ import annotations
import argparse
import io
import json
import tempfile
from contextlib import redirect_stdout
from pathlib import Path
from outcome_receipts.cli import EXIT_OK, main
from outcome_receipts.workflows import (
build_contract_evidence,
build_equity_review,
build_migration_check,
build_requirement_change,
build_restatement,
build_rollup,
)
ROOT = Path(__file__).resolve().parents[1]
OUTPUT = ROOT / "tests" / "fixtures" / "compat" / "v1" / "workflow-artifacts.json"
_BASIC_SPEC = """
schema_version = "1.0"
[data]
path = "data.csv"
[report]
title = "Compatibility fixture"
template = "People served: {served}."
[metrics.served]
description = "People served"
definition = "People represented by one row in the synthetic compatibility fixture."
kind = "output"
unit = "count"
value_sql = "SELECT COUNT(*) FROM data"
slice_sql = "SELECT synthetic_key FROM data"
""".lstrip()
_CONTRACT_SPEC = """
schema_version = "1.0"
[data]
path = "data.csv"
[report]
title = "Contract compatibility fixture"
template = "Observed {observed}; threshold {threshold}; finance {financial}."
[metrics.observed]
description = "Observed milestone"
definition = "Synthetic people represented by one row."
kind = "outcome"
unit = "count"
value_sql = "SELECT COUNT(*) FROM data"
slice_sql = "SELECT synthetic_key FROM data"
[metrics.threshold]
description = "Contract threshold"
definition = "Threshold copied into the synthetic controlling field."
kind = "output"
unit = "count"
value_sql = "SELECT MAX(CAST(threshold AS INTEGER)) FROM data"
slice_sql = "SELECT threshold FROM data"
[metrics.financial]
description = "Associated financial line"
definition = "Amount copied into the synthetic controlling field."
kind = "output"
unit = "money"
decimals = 0
value_sql = "SELECT MAX(CAST(amount AS INTEGER)) FROM data"
slice_sql = "SELECT amount FROM data"
""".lstrip()
# A spec whose second metric is a small cell, so migration equivalence has to
# classify it rather than compose a delta over it. Frozen as its own fixture so
# the `indeterminate` status is covered by the compatibility contract and not
# only by the unit tests.
_SMALL_CELL_SPEC = """
schema_version = "1.0"
[data]
path = "data.csv"
[report]
title = "Small-cell compatibility fixture"
template = "Total {served}; cohort A {small_group}."
[metrics.served]
description = "People served"
definition = "Synthetic people represented by one row."
kind = "output"
unit = "count"
value_sql = "SELECT COUNT(*) FROM data"
slice_sql = "SELECT synthetic_key FROM data"
[metrics.small_group]
description = "Cohort A"
definition = "Synthetic people represented by one row whose cohort is A."
kind = "output"
unit = "count"
value_sql = "SELECT COUNT(*) FROM data WHERE cohort = 'a'"
slice_sql = "SELECT synthetic_key FROM data WHERE cohort = 'a'"
""".lstrip()
_EQUITY_SPEC = """
schema_version = "1.0"
[data]
path = "data.csv"
[report]
title = "Equity compatibility fixture"
template = "Group A {group_a}; group B {group_b}; total {total}."
[metrics.group_a]
description = "Reviewed group A"
definition = "Synthetic records in operator-reviewed group A."
kind = "outcome"
unit = "count"
value_sql = "SELECT COUNT(*) FROM data WHERE category = 'A'"
slice_sql = "SELECT synthetic_key FROM data WHERE category = 'A'"
[metrics.group_b]
description = "Reviewed group B"
definition = "Synthetic records in operator-reviewed group B."
kind = "outcome"
unit = "count"
value_sql = "SELECT COUNT(*) FROM data WHERE category = 'B'"
slice_sql = "SELECT synthetic_key FROM data WHERE category = 'B'"
[metrics.total]
description = "All reviewed groups"
definition = "All synthetic records in the reviewed grouping."
kind = "outcome"
unit = "count"
value_sql = "SELECT COUNT(*) FROM data"
slice_sql = "SELECT synthetic_key FROM data"
""".lstrip()
def _write_basic(directory: Path, count: int) -> Path:
directory.mkdir()
rows = ["synthetic_key", *(f"fixture-{index}" for index in range(count))]
(directory / "data.csv").write_text("\n".join(rows) + "\n", encoding="utf-8")
config = directory / "report.toml"
config.write_text(_BASIC_SPEC, encoding="utf-8")
return config
def _write_small_cell(directory: Path, total: int, small: int) -> Path:
directory.mkdir()
rows = ["synthetic_key,cohort"]
rows.extend(f"fixture-{index},{'a' if index < small else 'b'}" for index in range(total))
(directory / "data.csv").write_text("\n".join(rows) + "\n", encoding="utf-8")
config = directory / "report.toml"
config.write_text(_SMALL_CELL_SPEC, encoding="utf-8")
return config
def _export_bundle(config: Path, bundle: Path, ledger: Path) -> None:
with redirect_stdout(io.StringIO()):
result = main(
[
"run",
"--config",
str(config),
"--out",
str(bundle),
"--ledger",
str(ledger),
"--approved-by",
"Compatibility fixture generator",
"--reproducible",
]
)
if result != EXIT_OK:
raise RuntimeError(f"fixture bundle export failed with exit code {result}")
def _write_json(path: Path, value: object) -> Path:
path.write_text(json.dumps(value, sort_keys=True) + "\n", encoding="utf-8")
return path
def _contract_files(directory: Path) -> tuple[Path, Path]:
directory.mkdir()
rows = ["synthetic_key,threshold,amount"]
rows.extend(f"fixture-{index},11,5000" for index in range(12))
(directory / "data.csv").write_text("\n".join(rows) + "\n", encoding="utf-8")
config = directory / "report.toml"
config.write_text(_CONTRACT_SPEC, encoding="utf-8")
contract = _write_json(
directory / "contract.json",
{
"contract_id": "synthetic-contract",
"controlling_text": "Synthetic compatibility fixture section A.",
"policy_citation": "Synthetic compatibility fixture section A.",
"milestones": [
{
"milestone_id": "synthetic-milestone",
"observed_metric_id": "observed",
"threshold_metric_id": "threshold",
"financial_metric_id": "financial",
"comparison": "gte",
}
],
},
)
return config, contract
def _equity_files(directory: Path) -> tuple[Path, Path]:
directory.mkdir()
rows = ["synthetic_key,category"]
rows.extend(f"a-{index},A" for index in range(5))
rows.extend(f"b-{index},B" for index in range(15))
(directory / "data.csv").write_text("\n".join(rows) + "\n", encoding="utf-8")
config = directory / "report.toml"
config.write_text(_EQUITY_SPEC, encoding="utf-8")
plan = _write_json(
directory / "plan.json",
{
"dimension": "synthetic operator-approved grouping",
"purpose": "Compatibility testing without interpretation.",
"controlling_policy": "CMS-modeled small-cell test policy.",
"consent_basis": "Synthetic data; no people represented.",
"category_provenance": "Generated compatibility fixture.",
"groups": [
{"label": "Group A", "metric_id": "group_a"},
{"label": "Group B", "metric_id": "group_b"},
],
},
)
return config, plan
def _artifacts(workspace: Path) -> list[dict[str, object]]:
prior = _write_basic(workspace / "prior", 12)
current = _write_basic(workspace / "current", 14)
prior_bundle = workspace / "prior-bundle"
_export_bundle(prior, prior_bundle, workspace / "prior-ledger.jsonl")
requirements_before = _write_json(
workspace / "requirements-before.json",
{"requirements": [{"requirement_id": "served", "definition": "People served."}]},
)
requirements_after = _write_json(
workspace / "requirements-after.json",
{
"requirements": [
{"requirement_id": "served", "definition": "Unduplicated people served."},
{"requirement_id": "exits", "definition": "Program exits."},
]
},
)
contract_config, contract = _contract_files(workspace / "contract")
partner_a = _write_basic(workspace / "partner-a", 12)
partner_b = _write_basic(workspace / "partner-b", 13)
bundle_a = workspace / "bundle-a"
bundle_b = workspace / "bundle-b"
_export_bundle(partner_a, bundle_a, workspace / "partner-a-ledger.jsonl")
_export_bundle(partner_b, bundle_b, workspace / "partner-b-ledger.jsonl")
rollup = _write_json(
workspace / "rollup.json",
{
"period": "2026-Q2",
"population_overlap": "not_deduplicated",
"suppression_policy_id": "cms-small-cell-v1",
"inputs": [
{
"partner": "Partner A",
"config": "partner-a/report.toml",
"bundle": "bundle-a",
"metric_id": "served",
"period": "2026-Q2",
"suppression_policy_id": "cms-small-cell-v1",
},
{
"partner": "Partner B",
"config": "partner-b/report.toml",
"bundle": "bundle-b",
"metric_id": "served",
"period": "2026-Q2",
"suppression_policy_id": "cms-small-cell-v1",
},
],
},
)
equity_config, equity_plan = _equity_files(workspace / "equity")
small_cell = _write_small_cell(workspace / "small-cell", 14, 4)
reproducible = True
return [
build_restatement(
prior_config=prior,
prior_bundle=prior_bundle,
current_config=current,
reason="Synthetic late records were accepted.",
approved_by="Compatibility fixture reviewer",
reproducible=reproducible,
),
build_migration_check(
before_config=prior,
after_config=current,
approved_by="Compatibility fixture reviewer",
reproducible=reproducible,
),
build_migration_check(
before_config=small_cell,
after_config=small_cell,
approved_by="Compatibility fixture reviewer",
reproducible=reproducible,
),
build_requirement_change(requirements_before, requirements_after),
build_contract_evidence(
config_path=contract_config,
contract_path=contract,
approved_by="Compatibility fixture reviewer",
reproducible=reproducible,
),
build_rollup(
plan_path=rollup,
approved_by="Compatibility fixture reviewer",
reproducible=reproducible,
),
build_equity_review(
config_path=equity_config,
plan_path=equity_plan,
approved_by="Compatibility fixture reviewer",
reproducible=reproducible,
),
]
def generate() -> str:
"""Generate the canonical compatibility-fixture document."""
with tempfile.TemporaryDirectory(prefix="outcome-receipts-compat-") as temporary:
artifacts = _artifacts(Path(temporary))
payload = {
"fixture_schema_version": "1.0",
"artifact_schema_version": "1.0",
"artifacts": artifacts,
}
return json.dumps(payload, indent=2, sort_keys=True) + "\n"
def main_cli() -> int:
parser = argparse.ArgumentParser()
parser.add_argument("--check", action="store_true")
args = parser.parse_args()
generated = generate()
if args.check:
current = OUTPUT.read_text(encoding="utf-8") if OUTPUT.is_file() else ""
if current != generated:
print(f"workflow compatibility fixtures are stale: {OUTPUT}")
return 1
print("workflow compatibility fixtures: current")
return 0
OUTPUT.parent.mkdir(parents=True, exist_ok=True)
OUTPUT.write_text(generated, encoding="utf-8")
print(f"wrote workflow compatibility fixtures: {OUTPUT}")
return 0
if __name__ == "__main__":
raise SystemExit(main_cli())