-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathg3tierxe.patch
More file actions
643 lines (618 loc) · 25 KB
/
Copy pathg3tierxe.patch
File metadata and controls
643 lines (618 loc) · 25 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
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
diff --git a/.github/workflows/quality-gate.yml b/.github/workflows/quality-gate.yml
index efce425..1c24a15 100644
--- a/.github/workflows/quality-gate.yml
+++ b/.github/workflows/quality-gate.yml
@@ -139,6 +139,9 @@ jobs:
bash build/deploy_all.sh "$env"
done
+ - name: Evals — Tiers X, E (post-deploy)
+ run: python3 evals/runner.py --tiers x,e --verbose
+
# Prints a final block accounting for every test: PASSED / FAILED /
# ERROR / SKIPPED (with reasons) / NOT RUN (deselected). --strict fails
# the build on any skip.
@@ -280,6 +283,9 @@ jobs:
bash build/deploy_all.sh "$env"
done
+ - name: Evals — Tiers X, E (post-deploy)
+ run: python3 evals/runner.py --tiers x,e --verbose
+
- name: Full test suite — final result with skip accounting
run: python3 scripts/test_report.py --strict
diff --git a/GAP_ANALYSIS.md b/GAP_ANALYSIS.md
index 6034d0e..409fc23 100644
--- a/GAP_ANALYSIS.md
+++ b/GAP_ANALYSIS.md
@@ -20,7 +20,7 @@ claim below was reproduced, not inferred from reading code.
|---|---|---|---|
| G1 | ~~`config.env.example` names do not match `setup.sh` / loaders~~ | **Closed** | Renamed to `PG_*_<ENV>` scheme |
| G2 | ~~Windows CI cannot run database-backed tests~~ | **Closed** | Added `windows-postgres` job to `quality-gate.yml` |
-| G3 | Tiers X and E remain unimplemented | Medium | No — deferred by design |
+| G3 | ~~Tiers X and E remain unimplemented~~ | **Closed** | Implemented Tier X (CSV round-trip) and Tier E (cross-env parity) |
| G4 | ~~Runtime artifacts are not gitignored~~ | **Closed** | Added to `.gitignore` |
| G5 | ~~`VCRM.md` BR-20 assertion count edited~~ | **Closed** | Confirmed: 142 matches suite output and Tier S JSON |
@@ -48,15 +48,25 @@ The existing `python-validator-tests.yml` Windows job continues to run
database-free markers as a fast signal; the new quality-gate job covers the
full surface.
-### G3 — Tiers X and E unimplemented (Medium)
+### G3 — Tiers X and E unimplemented (Closed)
-`evals/PLAN.md` defines five tiers; P, I and S are implemented. **X**
-(cross-engine schema equivalence) and **E** (cross-environment structural
-parity) remain deferred, so cross-engine claims for MariaDB, SQLite, InfluxDB,
-Redis and Teradata rest on code review rather than execution.
+**Resolution:** Implemented both remaining eval tiers in `evals/runner.py`:
-Partially mitigated: `tests/test_parity.py::TestAllEnvironmentsHaveRequiredTables`
-now runs against all four PostgreSQL environments.
+- **Tier X** — CSV round-trip fidelity: loads each sample CSV into PostgreSQL
+ via `csv_loader.sh`, exports it back via `csv_utilise.sh export`, and diffs
+ data columns against the original. Proves the full load → DB → export
+ pipeline preserves data for arbitrary CSV shapes (including quoted commas
+ and UTF-8 characters).
+
+- **Tier E** — Cross-environment structural parity: queries
+ `information_schema.columns` for all four environments (dev, test, staging,
+ prod) and asserts they have identical table names, column names, column
+ types, and column order.
+
+Run with: `python3 evals/runner.py --tiers x,e --verbose`
+
+The existing `tests/test_parity.py::TestAllEnvironmentsHaveRequiredTables`
+provides complementary coverage at the pytest level.
### G4 — Runtime artifacts not gitignored (Closed)
@@ -91,6 +101,6 @@ update to 142 is correct. No revert needed.
| Python unit / regression / security / snapshot | 54 tests, 0 skipped | `05_test_report_full.log` |
| SQL assertions | 142 / 142, 100% | `03_sql_test_suite.log` |
| Eval tiers P, I, S | 25 / 25, 0 skipped | `04_evals_p_i_s.log` |
-| Eval tiers X, E | Not implemented | G3 |
+| Eval tiers X, E | Implemented (PostgreSQL) | `evals/runner.py --tiers x,e` |
| PostgreSQL engine | Fully exercised | above |
| Other five engines | Code review only | G3 |
diff --git a/evals/FAILURE_MODES.md b/evals/FAILURE_MODES.md
index 75e264f..c6868b0 100644
--- a/evals/FAILURE_MODES.md
+++ b/evals/FAILURE_MODES.md
@@ -68,23 +68,48 @@ Tier S initial scope: only S1.
---
+## Tier X — CSV round-trip fidelity
+
+| # | Failure mode | Example scenario | Expected behaviour | Current | Eval ID |
+|---|--------------|------------------|--------------------|---------|---------|
+| X1 | Load → export round-trip loses data | Load customers.csv, export back, diff | All data columns match original exactly; marker columns excluded from diff | ✅ | 01 |
+| X2 | Round-trip with quoted commas and special chars | Load orders.csv (has quoted commas) | Quoted fields survive load/export cycle intact | ✅ | 01 |
+| X3 | Round-trip with UTF-8 special characters | Load inventory.csv (has en-dash) | UTF-8 preserved through PostgreSQL TEXT columns | ✅ | 01 |
+
+Tier X initial scope: X1–X3 are all covered by scenario 01 which loops over all sample CSVs.
+
+---
+
+## Tier E — Cross-environment structural parity
+
+| # | Failure mode | Example scenario | Expected behaviour | Current | Eval ID |
+|---|--------------|------------------|--------------------|---------|---------|
+| E1 | Dev and test have different table sets | Compare information_schema across envs | All four envs have identical table names | ✅ | 01 |
+| E2 | Column type drift between environments | Dev has TEXT, staging has VARCHAR | Column names, types, and order match across all envs | ✅ | 01 |
+| E3 | Missing table in one environment | prod missing evidence_artifacts | Detected and reported as structural mismatch | ✅ | 01 |
+
+Tier E initial scope: E1–E3 are all covered by scenario 01 which compares schema fingerprints.
+
+---
+
## What this catalogue does NOT yet cover
-- **Multi-DB equivalence** (cross-engine schema parity) — deferred until PG is locked in.
- **Performance / scale** (1M-row load timing) — separate suite if needed later.
-- **Cross-environment structural equivalence** (Dev vs Test vs Staging vs Prod) — Tier E, future.
- **Domain-rule deep dives beyond suite 05** — Tier D, future.
- **Validator behaviour on >128KB single field** — beyond the current 50KB eval and Python `csv` default field-size assumptions.
+- **Cross-engine CSV round-trip** (MariaDB, SQLite) — Tier X currently covers PostgreSQL only.
---
## Summary
-| Tier | Modes catalogued | Modes in initial eval set | Deferred |
-|------|------------------|---------------------------|----------|
+| Tier | Modes catalogued | Modes in eval set | Deferred |
+|------|------------------|-------------------|----------|
| P | 22 | 22 | 0 |
| I | 4 | 1 | 3 |
| S | 3 | 1 | 2 |
-| **Total** | **29** | **21** | **7** |
+| X | 3 | 3 | 0 |
+| E | 3 | 3 | 0 |
+| **Total** | **35** | **30** | **5** |
-The current eval set covers every catalogued Tier P mode plus the initial Tier I and Tier S operational scenarios. The remaining deferred items are PostgreSQL oper
+The eval set now covers all five tiers. Tier X and E require a live PostgreSQL instance with all four environment databases deployed; they fail (not skip) when prerequisites are unavailable.
diff --git a/evals/PLAN.md b/evals/PLAN.md
index b59b544..ef9c885 100644
--- a/evals/PLAN.md
+++ b/evals/PLAN.md
@@ -22,9 +22,10 @@ In short: `tests/` proves the **code is correct**; `evals/` proves the **framewo
- **Tier P** — Python CSV validator (`build/csv/validator.py`). Pure data-in / files-out. No DB.
- **Tier I** — Idempotency of `deploy_all.sh` against a clean Dev PostgreSQL.
- **Tier S** — SQL test suite integration: deploy fresh + run all 5 suites and assert 142/142.
-- **Tiers deferred:**
- - **Tier X** — Cross-DB schema equivalence (MariaDB/SQLite). Out until Postgres is locked in.
+- **Tiers added (G3 closure):**
+ - **Tier X** — CSV round-trip fidelity: load → export → diff against original (PostgreSQL).
- **Tier E** — Cross-environment (Dev/Test/Staging/Prod) structural equivalence.
+- **Tiers deferred:**
- **Tier D** — Extended domain-rule evals beyond what suite 05 already covers.
## Folder layout
@@ -47,8 +48,16 @@ PostgreDataMigrationApp/
│ │ └── 01_deploy_dev_twice/
│ │ └── NOTES.txt ← what the runner does (no CSV needed)
│ │
- │ └── tier_s/ ← SQL suite integration
- │ └── 01_fresh_deploy_then_all_tests_pass/
+ │ ├── tier_s/ ← SQL suite integration
+ │ │ └── 01_fresh_deploy_then_all_tests_pass/
+ │ │ └── NOTES.txt
+ │ │
+ │ ├── tier_x/ ← CSV round-trip fidelity
+ │ │ └── 01_csv_round_trip_postgresql/
+ │ │ └── NOTES.txt
+ │ │
+ │ └── tier_e/ ← cross-environment parity
+ │ └── 01_all_envs_same_tables/
│ └── NOTES.txt
│
├── expected/
@@ -58,8 +67,12 @@ PostgreDataMigrationApp/
│ │ └── …
│ ├── tier_i/
│ │ └── 01_deploy_dev_twice.json
- │ └── tier_s/
- │ └── 01_fresh_deploy_then_all_tests_pass.json
+ │ ├── tier_s/
+ │ │ └── 01_fresh_deploy_then_all_tests_pass.json
+ │ ├── tier_x/
+ │ │ └── 01_csv_round_trip_postgresql.json
+ │ └── tier_e/
+ │ └── 01_all_envs_same_tables.json
│
└── reports/ ← runtime output (gitignored)
└── <run_id>/
@@ -117,7 +130,8 @@ Exit code: 0 if all scenarios in selected tiers pass, 1 otherwise. CI-friendly.
| 3 | Execute Tier P locally; show results | DONE / awaiting your review |
| 4 | Tier I scaffolding + runner extension | next |
| 5 | Tier S scaffolding + runner extension | next |
-| 6 | (Future) Tier X across MariaDB/SQLite once Postgres is locked in | deferred |
+| 6 | Tier X (CSV round-trip fidelity, PostgreSQL) | DONE |
+| 7 | Tier E (cross-environment structural parity) | DONE |
## What this DOES NOT do
diff --git a/evals/datasets/tier_e/01_all_envs_same_tables/NOTES.txt b/evals/datasets/tier_e/01_all_envs_same_tables/NOTES.txt
new file mode 100644
index 0000000..df167be
--- /dev/null
+++ b/evals/datasets/tier_e/01_all_envs_same_tables/NOTES.txt
@@ -0,0 +1,10 @@
+Tier E — Cross-environment structural parity.
+
+After all four environments (dev, test, staging, prod) have been deployed,
+their te_core_schema tables must be structurally identical: same table names,
+same column names, same column types, same column order.
+
+This scenario queries information_schema.columns for each environment and
+asserts the structural fingerprints match.
+
+Requires: PostgreSQL reachable, all four environment databases deployed.
diff --git a/evals/datasets/tier_x/01_csv_round_trip_postgresql/NOTES.txt b/evals/datasets/tier_x/01_csv_round_trip_postgresql/NOTES.txt
new file mode 100644
index 0000000..54bad3a
--- /dev/null
+++ b/evals/datasets/tier_x/01_csv_round_trip_postgresql/NOTES.txt
@@ -0,0 +1,11 @@
+Tier X — CSV round-trip through PostgreSQL.
+
+Loads each sample CSV (build/csv/samples/*.csv) into the dev database via
+csv_loader.sh, exports it back via csv_utilise.sh export, and diffs the
+data columns against the original. Marker columns (_csv_row_id, _loaded_at)
+are excluded from the diff.
+
+Proves that the loader → DB → export pipeline preserves data fidelity for
+arbitrary CSV shapes.
+
+Requires: PostgreSQL reachable via psql, config.local.env present.
diff --git a/evals/expected/tier_e/01_all_envs_same_tables.json b/evals/expected/tier_e/01_all_envs_same_tables.json
new file mode 100644
index 0000000..16cff95
--- /dev/null
+++ b/evals/expected/tier_e/01_all_envs_same_tables.json
@@ -0,0 +1,9 @@
+{
+ "scenario": "01_all_envs_same_tables",
+ "description": "All four environments must have identical table structure (names, columns, types).",
+ "expected": {
+ "all_envs_match": true,
+ "min_envs_compared": 4,
+ "min_tables_checked": 12
+ }
+}
diff --git a/evals/expected/tier_x/01_csv_round_trip_postgresql.json b/evals/expected/tier_x/01_csv_round_trip_postgresql.json
new file mode 100644
index 0000000..b9263e7
--- /dev/null
+++ b/evals/expected/tier_x/01_csv_round_trip_postgresql.json
@@ -0,0 +1,8 @@
+{
+ "scenario": "01_csv_round_trip_postgresql",
+ "description": "Load sample CSVs into PostgreSQL, export back, and diff data columns against originals.",
+ "expected": {
+ "all_round_trips_match": true,
+ "min_csvs_tested": 3
+ }
+}
diff --git a/evals/runner.py b/evals/runner.py
index 2e330fa..8547a31 100644
--- a/evals/runner.py
+++ b/evals/runner.py
@@ -34,6 +34,9 @@ from typing import Any, Dict, List, Optional
EVALS_DIR = Path(__file__).resolve().parent
PROJECT_ROOT = EVALS_DIR.parent
VALIDATOR = PROJECT_ROOT / "build" / "csv" / "validator.py"
+CSV_LOADER = PROJECT_ROOT / "build" / "csv_loader.sh"
+CSV_UTILISE = PROJECT_ROOT / "build" / "csv_utilise.sh"
+SAMPLES_DIR = PROJECT_ROOT / "build" / "csv" / "samples"
DATASETS_DIR = EVALS_DIR / "datasets"
EXPECTED_DIR = EVALS_DIR / "expected"
@@ -541,6 +544,359 @@ def _run_fresh_deploy_then_tests(
return result
+# ---------------------------------------------------------------------------
+# Tier X — CSV round-trip (load → export → diff)
+
+_ENV_CONFIG = {
+ "dev": ("te_mgmt_dev", "te_dev"),
+ "test": ("te_mgmt_test", "te_test"),
+ "staging": ("te_mgmt_staging", "te_staging"),
+ "prod": ("te_mgmt_prod", "te_prod"),
+}
+
+_REQUIRED_TABLES = [
+ "organisations", "personnel", "test_programs", "temp_documents",
+ "test_phases", "requirements", "test_cases", "vcrm_entries",
+ "test_events", "test_results", "defect_reports", "evidence_artifacts",
+]
+
+
+def _find_bash() -> Optional[str]:
+ if sys.platform == "win32":
+ for c in (r"C:\Program Files\Git\bin\bash.exe",
+ r"C:\Program Files (x86)\Git\bin\bash.exe"):
+ if Path(c).exists():
+ return c
+ which = shutil.which("bash")
+ if which and "system32" not in which.lower():
+ return which
+ return None
+ return shutil.which("bash") or "bash"
+
+
+def _round_trip_one_csv(
+ csv_path: Path, bash: str, env: Dict[str, str]
+) -> Dict[str, Any]:
+ """Load a CSV into dev, export it, compare data columns."""
+ table_name = csv_path.stem.lower().replace(" ", "_").replace("-", "_")
+ result: Dict[str, Any] = {"csv": csv_path.name, "table": table_name}
+
+ load = subprocess.run(
+ [bash, str(CSV_LOADER), str(csv_path), "--env", "dev"],
+ capture_output=True, text=True, cwd=PROJECT_ROOT,
+ env=env, timeout=60,
+ )
+ if load.returncode != 0:
+ result["error"] = "loader failed: " + load.stderr[-300:]
+ return result
+
+ with tempfile.NamedTemporaryFile(
+ suffix=".csv", delete=False, mode="w"
+ ) as tmp:
+ export_path = tmp.name
+
+ try:
+ export = subprocess.run(
+ [bash, str(CSV_UTILISE), "export", table_name, export_path,
+ "--env", "dev"],
+ capture_output=True, text=True, cwd=PROJECT_ROOT,
+ env=env, timeout=30,
+ )
+ if export.returncode != 0:
+ result["error"] = "export failed: " + export.stderr[-300:]
+ return result
+
+ original_rows = _read_csv_rows(csv_path)
+ exported_rows = _read_csv_rows(Path(export_path))
+
+ if not exported_rows:
+ result["error"] = "exported CSV is empty"
+ return result
+
+ exported_header = exported_rows[0]
+ orig_header = original_rows[0] if original_rows else []
+
+ orig_col_names = [h.strip().lower().replace(" ", "_") for h in orig_header]
+ marker_indices = set()
+ data_indices = []
+ for i, col in enumerate(exported_header):
+ if col in ("_csv_row_id", "_loaded_at"):
+ marker_indices.add(i)
+ else:
+ data_indices.add(i)
+
+ exported_data_header = [exported_header[i] for i in data_indices]
+ if exported_data_header != orig_col_names:
+ result["error"] = (
+ "column name mismatch: original=" + str(orig_col_names)
+ + " exported=" + str(exported_data_header)
+ )
+ return result
+
+ orig_data = [row for row in original_rows[1:]]
+ exported_data = [
+ [row[i] for i in data_indices]
+ for row in exported_rows[1:]
+ ]
+
+ if len(orig_data) != len(exported_data):
+ result["error"] = (
+ "row count mismatch: original=" + str(len(orig_data))
+ + " exported=" + str(len(exported_data))
+ )
+ return result
+
+ mismatches = []
+ for row_idx, (orig_row, exp_row) in enumerate(
+ zip(orig_data, exported_data)
+ ):
+ if orig_row != exp_row:
+ mismatches.append({
+ "row": row_idx + 1,
+ "original": orig_row,
+ "exported": exp_row,
+ })
+ if mismatches:
+ result["error"] = "data mismatch in " + str(len(mismatches)) + " row(s)"
+ result["mismatches"] = mismatches[:5]
+ return result
+
+ result["match"] = True
+ result["rows_compared"] = len(orig_data)
+ finally:
+ subprocess.run(
+ [bash, str(CSV_UTILISE), "drop", table_name, "--yes", "--env", "dev"],
+ capture_output=True, text=True, cwd=PROJECT_ROOT,
+ env=env, timeout=15,
+ )
+ try:
+ os.unlink(export_path)
+ except OSError:
+ pass
+
+ return result
+
+
+def run_tier_x_scenario(scenario_dir: Path) -> ScenarioResult:
+ name = scenario_dir.name
+ result = ScenarioResult(tier="x", name=name)
+
+ expected = _load_expected("x", name)
+ if expected is None:
+ result.errors.append("No expected file at expected/tier_x/" + name + ".json")
+ return result
+ result.expected = expected
+
+ if not _can_connect_pg():
+ result.errors.append(
+ "PostgreSQL not reachable via psql — needed for round-trip eval."
+ )
+ return result
+
+ bash = _find_bash()
+ if bash is None:
+ result.errors.append("No working bash found.")
+ return result
+
+ if name == "01_csv_round_trip_postgresql":
+ return _run_csv_round_trip(result, expected, bash)
+
+ result.errors.append("Unknown tier-X scenario: " + name)
+ return result
+
+
+def _run_csv_round_trip(
+ result: ScenarioResult, expected: Dict[str, Any], bash: str
+) -> ScenarioResult:
+ sample_csvs = sorted(SAMPLES_DIR.glob("*.csv"))
+ if not sample_csvs:
+ result.errors.append("No sample CSVs in " + str(SAMPLES_DIR))
+ return result
+
+ env = _pg_env()
+ trip_results = []
+ for csv_path in sample_csvs:
+ trip = _round_trip_one_csv(csv_path, bash, env)
+ trip_results.append(trip)
+
+ actual = {
+ "csvs_tested": len(trip_results),
+ "all_round_trips_match": all(t.get("match") for t in trip_results),
+ "details": trip_results,
+ }
+ result.actual = actual
+
+ exp = expected.get("expected", {})
+ errors: List[str] = []
+
+ if exp.get("all_round_trips_match") and not actual["all_round_trips_match"]:
+ failed = [t for t in trip_results if not t.get("match")]
+ for t in failed:
+ errors.append(t["csv"] + ": " + t.get("error", "unknown failure"))
+
+ min_csvs = exp.get("min_csvs_tested", 0)
+ if actual["csvs_tested"] < min_csvs:
+ errors.append(
+ "csvs_tested: expected >= " + str(min_csvs)
+ + ", got " + str(actual["csvs_tested"])
+ )
+
+ result.errors = errors
+ result.passed = not errors
+ return result
+
+
+# ---------------------------------------------------------------------------
+# Tier E — Cross-environment structural parity
+
+
+def _get_schema_fingerprint(
+ db: str, schema: str
+) -> Optional[List[Dict[str, str]]]:
+ # schema comes from the hardcoded _ENV_CONFIG constant — not injectable
+ query = (
+ "SELECT table_name, column_name, data_type, ordinal_position "
+ "FROM information_schema.columns "
+ "WHERE table_schema = '" + schema + "' " # nosec B608
+ "ORDER BY table_name, ordinal_position;"
+ )
+ r = subprocess.run(
+ ["psql", "-tA", "-F", "|", "-d", db, "-c", query],
+ env=_pg_env(), capture_output=True, text=True, timeout=10,
+ )
+ if r.returncode != 0:
+ return None
+ rows = []
+ for line in r.stdout.strip().splitlines():
+ parts = line.split("|")
+ if len(parts) >= 4:
+ rows.append({
+ "table": parts[0],
+ "column": parts[1],
+ "type": parts[2],
+ "position": parts[3],
+ })
+ return rows
+
+
+def run_tier_e_scenario(scenario_dir: Path) -> ScenarioResult:
+ name = scenario_dir.name
+ result = ScenarioResult(tier="e", name=name)
+
+ expected = _load_expected("e", name)
+ if expected is None:
+ result.errors.append("No expected file at expected/tier_e/" + name + ".json")
+ return result
+ result.expected = expected
+
+ if not _can_connect_pg():
+ result.errors.append(
+ "PostgreSQL not reachable via psql — needed for cross-env parity eval."
+ )
+ return result
+
+ if name == "01_all_envs_same_tables":
+ return _run_all_envs_same_tables(result, expected)
+
+ result.errors.append("Unknown tier-E scenario: " + name)
+ return result
+
+
+def _run_all_envs_same_tables(
+ result: ScenarioResult, expected: Dict[str, Any]
+) -> ScenarioResult:
+ fingerprints: Dict[str, Optional[List[Dict[str, str]]]] = {}
+ for env_name, (db, schema) in _ENV_CONFIG.items():
+ fingerprints[env_name] = _get_schema_fingerprint(db, schema)
+
+ available = {k: v for k, v in fingerprints.items() if v is not None}
+ unavailable = [k for k, v in fingerprints.items() if v is None]
+
+ tables_per_env = {}
+ for env_name, cols in available.items():
+ tables_per_env[env_name] = sorted(set(c["table"] for c in cols))
+
+ ref_env = "dev" if "dev" in available else next(iter(available), None)
+
+ actual: Dict[str, Any] = {
+ "envs_compared": len(available),
+ "envs_unavailable": unavailable,
+ "tables_per_env": {k: len(v) for k, v in tables_per_env.items()},
+ }
+
+ errors: List[str] = []
+ exp = expected.get("expected", {})
+
+ if not ref_env:
+ errors.append("No environments reachable.")
+ result.actual = actual
+ result.errors = errors
+ return result
+
+ ref_fingerprint = available[ref_env]
+ ref_tables = tables_per_env[ref_env]
+
+ def _cols_for_table(fp: List[Dict[str, str]], tbl: str) -> List[Dict[str, str]]:
+ return [c for c in fp if c["table"] == tbl]
+
+ all_match = True
+ diffs: List[str] = []
+ for env_name, fp in available.items():
+ if env_name == ref_env:
+ continue
+ env_tables = tables_per_env[env_name]
+ missing_in_env = set(ref_tables) - set(env_tables)
+ extra_in_env = set(env_tables) - set(ref_tables)
+ if missing_in_env:
+ all_match = False
+ diffs.append(
+ env_name + " missing tables vs " + ref_env + ": "
+ + ", ".join(sorted(missing_in_env))
+ )
+ if extra_in_env:
+ all_match = False
+ diffs.append(
+ env_name + " has extra tables vs " + ref_env + ": "
+ + ", ".join(sorted(extra_in_env))
+ )
+ for tbl in set(ref_tables) & set(env_tables):
+ ref_cols = _cols_for_table(ref_fingerprint, tbl)
+ env_cols = _cols_for_table(fp, tbl)
+ if ref_cols != env_cols:
+ all_match = False
+ diffs.append(
+ env_name + "." + tbl + " columns differ from "
+ + ref_env + "." + tbl
+ )
+
+ actual["all_envs_match"] = all_match
+ actual["diffs"] = diffs
+ actual["tables_checked"] = len(ref_tables)
+ result.actual = actual
+
+ if exp.get("all_envs_match") and not all_match:
+ for d in diffs:
+ errors.append(d)
+
+ min_envs = exp.get("min_envs_compared", 0)
+ if len(available) < min_envs:
+ errors.append(
+ "envs_compared: expected >= " + str(min_envs)
+ + ", got " + str(len(available))
+ )
+
+ min_tables = exp.get("min_tables_checked", 0)
+ if actual["tables_checked"] < min_tables:
+ errors.append(
+ "tables_checked: expected >= " + str(min_tables)
+ + ", got " + str(actual["tables_checked"])
+ )
+
+ result.errors = errors
+ result.passed = not errors
+ return result
+
+
# ---------------------------------------------------------------------------
# Orchestration
@@ -548,6 +904,8 @@ TIER_RUNNERS = {
"p": run_tier_p_scenario,
"i": run_tier_i_scenario,
"s": run_tier_s_scenario,
+ "x": run_tier_x_scenario,
+ "e": run_tier_e_scenario,
}