From a05b91d80b9014ad4c2bb17c217bbb20f8efb67f Mon Sep 17 00:00:00 2001 From: NeoNix <112125692+NeoNix-Lab@users.noreply.github.com> Date: Sun, 25 Oct 2026 10:12:31 +0100 Subject: [PATCH] feat(data): R01 - superseding reassessment writer for a live revision (ADR-0074 A2.3) (#519) Appends one new quality report (a fail that retires a valid revision, or a pass that restores it) for the current live revision of one natural partition, then runs the existing A16 lifecycle seam. The report is filed under the same suite and profile as the assessment it supersedes, copies its bound evidence unchanged, carries metrics.supersedes_assessment_signature = the exact signature of the current leaf, and must pass current_partition_report before anything is written. Idempotent; refuses a superseded, writing or closed revision, foreign evidence, a different dataset or key. Two short transactions (report, then the seam); a crash between them converges on a repeat. Tests: unit (argument rules), real-PostgreSQL gate (retire/restore, supersession link, evidence copy, reads exclude the retired revision under every policy, bytes/manifests/reports untouched, refusals write nothing, a concurrent S13 publication waits for the writer, no lock held after, crash window), a CI job, mutation: 22 mutants, 0 survivors. Co-Authored-By: Claude Sonnet 5.5 --- .github/workflows/integrity.yml | 38 ++ .../data/quality_reassessment.py | 216 +++++++++++ ...tegration_quality_reassessment_postgres.py | 351 ++++++++++++++++++ tests/test_package_boundaries_v1.py | 1 + tests/test_quality_reassessment_v1.py | 74 ++++ 5 files changed, 680 insertions(+) create mode 100644 src/quant_platform/data/quality_reassessment.py create mode 100644 tests/integration_quality_reassessment_postgres.py create mode 100644 tests/test_quality_reassessment_v1.py diff --git a/.github/workflows/integrity.yml b/.github/workflows/integrity.yml index 0a2ef51d..47042dc1 100644 --- a/.github/workflows/integrity.yml +++ b/.github/workflows/integrity.yml @@ -379,6 +379,44 @@ jobs: DATA_GATEWAY_TEST_DSN: postgresql://postgres:ci-postgres-password@127.0.0.1:5432/market_catalog_live_session run: python tests/integration_live_session_postgres.py + quality-reassessment-postgres: + name: Disposable PostgreSQL 17 quality reassessment test + runs-on: ubuntu-latest + services: + postgres: + image: postgres:17 + env: + POSTGRES_USER: postgres + POSTGRES_PASSWORD: ci-postgres-password + POSTGRES_DB: market_catalog_quality_reassessment + ports: + - 5432:5432 + options: >- + --health-cmd "pg_isready -U postgres -d market_catalog_quality_reassessment" + --health-interval 5s + --health-timeout 5s + --health-retries 20 + steps: + - uses: actions/checkout@v4 + - uses: actions/setup-python@v5 + with: + python-version: '3.11' + - name: Install quality reassessment test dependencies + run: python -m pip install --disable-pip-version-check -r tests/requirements.txt + - name: Initialize catalog schema + env: + POSTGRES_USER: postgres + POSTGRES_DB: market_catalog_quality_reassessment + PGPASSWORD: ci-postgres-password + PGHOST: 127.0.0.1 + PGPORT: '5432' + run: | + docker run --rm --network host -v "$GITHUB_WORKSPACE:/workspace:ro" -e POSTGRES_USER -e POSTGRES_DB -e PGPASSWORD -e PGHOST -e PGPORT postgres:17 psql -U "$POSTGRES_USER" -d "$POSTGRES_DB" -v ON_ERROR_STOP=1 -f /workspace/db/init/001_catalog.sql + - name: Run quality reassessment PostgreSQL integration + env: + DATA_GATEWAY_TEST_DSN: postgresql://postgres:ci-postgres-password@127.0.0.1:5432/market_catalog_quality_reassessment + run: python tests/integration_quality_reassessment_postgres.py + adversarial-refusal-postgres: name: Disposable PostgreSQL 17 adversarial publication refusal test runs-on: ubuntu-latest diff --git a/src/quant_platform/data/quality_reassessment.py b/src/quant_platform/data/quality_reassessment.py new file mode 100644 index 00000000..1c23388b --- /dev/null +++ b/src/quant_platform/data/quality_reassessment.py @@ -0,0 +1,216 @@ +"""R01 (DG-R): a superseding reassessment of the current live revision of one natural partition (ADR-0074 Amendment 2, A2.3). + +``QualityLifecycleCatalog.apply_partition_lifecycle`` (A16) accepts ``valid`` as a source state and maps a current ``fail`` assessment +to ``invalid``, but nothing could write that assessment for a revision that is not ``closed``: ``record_quality_report`` refuses every +other state and the repair path writes reports only for candidates inside its own cutover. This module is the missing writer. It +appends one new report (a ``fail`` that retires the revision from reads, or a ``pass`` that restores it) whose bound evidence is the +superseded assessment's, unchanged, and whose ``metrics.supersedes_assessment_signature`` is the exact semantic signature of the +current leaf; then it runs the existing lifecycle seam. Nothing is deleted or rewritten: the bytes, the manifests and every earlier +report stay as they were, and the old state is recoverable by a later ``pass``. + +The writer does not widen the meaning of the lifecycle: it files the report under the same check suite and certification profile as +the assessment it supersedes, it must pass ``current_partition_report`` before anything is written, and it refuses a revision that is +superseded, foreign (another dataset, key or revision), not the live one of its key, ``writing`` or ``closed`` (ordinary S13 owns +``closed``). The report says the partition is incomplete for its day, not that it is corrupt. +""" + +from __future__ import annotations + +from copy import deepcopy +from dataclasses import dataclass +from pathlib import Path +from typing import Any, Mapping, Sequence + +from .models import Instant +from .quality_lifecycle import ( + QUALITY_LIFECYCLE_CODE_ID, + SUPERSEDES_ASSESSMENT_SIGNATURE, + QualityLifecycleCatalog, + QualityLifecycleRefusal, + QualityLifecycleResult, + _identity, + _identity_from_row, + _live_row, + current_partition_report, + select_current_quality_assessment, + semantic_assessment_signature, +) + +REASON_LEGACY_INCOMPLETE_DAY = "legacy_publication_incomplete_day" +REASSESSMENT_STATUSES = frozenset({"pass", "fail"}) +# ``closed`` is S13's (``record_quality_report``); ``writing`` and ``superseded`` are never reassessed. +REASSESSABLE_STATES = frozenset({"valid", "degraded", "invalid"}) + + +class QualityReassessmentRefusal(QualityLifecycleRefusal): + """The reassessment was refused; nothing was written.""" + + +@dataclass(frozen=True, slots=True) +class LifecycleInputs: + """The durable evidence of one partition, as the lifecycle seam wants it (read from the three manifest kinds).""" + + dataset: dict[str, Any] + dataset_sha256: str + partition: dict[str, Any] + partition_sha256: str + coverage_ids: tuple[str, ...] + assertion_ids: tuple[str, ...] + coverage_sha256: tuple[str, ...] + + +@dataclass(frozen=True, slots=True) +class ReassessmentResult: + """``report_id`` is the report this call wrote; ``None`` (``inserted`` False) when the current assessment already says it.""" + + report_id: str | None + inserted: bool + superseded_signature: str + new_signature: str + lifecycle: QualityLifecycleResult + + +def load_lifecycle_inputs( + dataset_manifest_path: str | Path, partition_manifest_path: str | Path, coverage_manifest_paths: Sequence[str | Path], +) -> LifecycleInputs: + """Read and validate the durable manifests of one partition (the same loader and hashes S13 uses).""" + + from .publication import SealedPartitionEvidence, _load_evidence + + paths = tuple(Path(item) for item in coverage_manifest_paths) + loaded = _load_evidence(SealedPartitionEvidence( + Path(dataset_manifest_path), Path(partition_manifest_path), paths, Path(partition_manifest_path), "unused", + )) + return LifecycleInputs( + dataset=loaded.dataset, + dataset_sha256=loaded.dataset_sha256, + partition=loaded.partition, + partition_sha256=loaded.partition_sha256, + coverage_ids=tuple(item.get("coverage_id") for item in loaded.coverage), + assertion_ids=tuple( + assertion.get("assertion_id") for document in loaded.coverage for assertion in document.get("assertions") or () + ), + coverage_sha256=tuple(loaded.coverage_sha256), + ) + + +def reassess_live_partition( + connection: Any, + *, + inputs: LifecycleInputs, + coverage_start: Instant, + coverage_end: Instant, + storage_root_id: str, + expected_profile: str, + expected_check_suite: str, + status: str, + code_ref: str, + archive_identity: Mapping[str, Any] | None = None, + lifecycle_code_ref: str = QUALITY_LIFECYCLE_CODE_ID, +) -> ReassessmentResult: + """Append the reassessment ``status`` (``fail`` retires, ``pass`` restores) for the live revision named by ``inputs``. + + Two short transactions, each committed on its own: the first locks the dataset row and then the key's topology (the order of the + lifecycle application), checks the target and writes the report; the second is the existing lifecycle seam. A crash between the + two leaves a current ``fail`` with the state still ``valid``; running the same call again converges (the report is not written + twice and the seam applies the state). ``archive_identity`` names the archive file the incompleteness is measured against + (required for ``fail``).""" + + if status not in REASSESSMENT_STATUSES: + raise QualityReassessmentRefusal(f"a reassessment is a pass or a fail, not {status!r}") + if not isinstance(code_ref, str) or not code_ref.strip(): + raise QualityReassessmentRefusal("a reassessment requires the producing commit as code_ref") + if status == "fail" and not archive_identity: + raise QualityReassessmentRefusal("a failing reassessment must name the archive identity it is measured against") + code_ref = code_ref.strip() + violations: list[dict[str, Any]] = [] if status == "pass" else [{ + "category": "coverage", + "message": REASON_LEGACY_INCOMPLETE_DAY, + "detail": "the live publication of this partition is incomplete for its day; the archive holds the whole day", + "archive": dict(archive_identity or {}), + }] + try: + with connection.cursor() as cursor: + inserted_id, superseded, new_signature = _write_report( + cursor, inputs, coverage_start, coverage_end, storage_root_id, expected_profile, expected_check_suite, + status, code_ref, violations, + ) + connection.commit() + except Exception: + connection.rollback() + raise + lifecycle = QualityLifecycleCatalog(connection).apply_partition_lifecycle( + dataset=inputs.dataset, dataset_sha256=inputs.dataset_sha256, + partition=inputs.partition, partition_sha256=inputs.partition_sha256, + coverage_start=coverage_start, coverage_end=coverage_end, + coverage_ids=inputs.coverage_ids, assertion_ids=inputs.assertion_ids, coverage_sha256=inputs.coverage_sha256, + storage_root_id=storage_root_id, expected_profile=expected_profile, expected_check_suite=expected_check_suite, + lifecycle_code_ref=lifecycle_code_ref, + ) + return ReassessmentResult(inserted_id, inserted_id is not None, superseded, new_signature, lifecycle) + + +def _write_report( + cursor: Any, inputs: LifecycleInputs, start: Instant, end: Instant, storage_root_id: str, profile: str, suite: str, + status: str, code_ref: str, violations: list[dict[str, Any]], +) -> tuple[str | None, str, str]: + identity = _identity(inputs.dataset) + partition = inputs.partition + dataset_row = QualityLifecycleCatalog._resolve_dataset(cursor, identity, inputs.dataset, inputs.dataset_sha256) + topology = QualityLifecycleCatalog._lock_partition_topology(cursor, dataset_row[0], partition["partition_key"]) + target = next((row for row in topology if int(row[3]) == int(partition["revision"])), None) + if target is None: + raise QualityReassessmentRefusal("target revision is missing") + if target[4] == "superseded": + raise QualityReassessmentRefusal("a superseded revision is never reassessed") + if target[4] == "writing": + raise QualityReassessmentRefusal("a writing revision is not quality-eligible") + _live_row(topology) # exactly one live revision per key; a target that is not superseded is that one + if target[4] not in REASSESSABLE_STATES: + raise QualityReassessmentRefusal( + "only a valid, degraded or invalid revision is reassessed; a closed one is recorded by the ordinary S13 path") + QualityLifecycleCatalog._verify_partition(target, dataset_row, partition, inputs.partition_sha256, start, end, storage_root_id) + + reports = QualityLifecycleCatalog._quality_reports(cursor, target[0], suite) + leaf = select_current_quality_assessment( + reports, expected_profile=profile, expected_check_suite=suite, identity=_identity_from_row(dataset_row), + partition=partition, dataset_sha256=inputs.dataset_sha256, partition_sha256=inputs.partition_sha256, + coverage_ids=inputs.coverage_ids, assertion_ids=inputs.assertion_ids, coverage_sha256=inputs.coverage_sha256, + target=target, + ) + if leaf.status == status and (status == "pass" or (leaf.violations == violations and leaf.code_ref == code_ref)): + return None, leaf.signature, leaf.signature # the current assessment already says it: nothing to write + + metrics = deepcopy(dict(leaf.metrics)) + evidence = deepcopy(dict(metrics["evidence"])) + evidence["coverage"] = dict(evidence["coverage"], status=status) + metrics["evidence"] = evidence + metrics[SUPERSEDES_ASSESSMENT_SIGNATURE] = leaf.signature + if not current_partition_report( + metrics, status, violations, code_ref, profile, suite, _identity_from_row(dataset_row), partition, + inputs.dataset_sha256, inputs.partition_sha256, inputs.coverage_ids, inputs.assertion_ids, inputs.coverage_sha256, target, + ): + raise QualityReassessmentRefusal( + f"a {status} reassessment cannot be built over the current assessment ({leaf.status}): its evidence does not allow it" + ) + signature = semantic_assessment_signature(suite, status, metrics, violations, code_ref) + from psycopg.types.json import Jsonb + + cursor.execute( + "INSERT INTO catalog.quality_reports (partition_id, check_suite, status, metrics, violations, code_ref) " + "VALUES (%s, %s, %s, %s, %s, %s) RETURNING report_id::text", + (target[0], suite, status, Jsonb(metrics), Jsonb(violations), code_ref), + ) + return str(cursor.fetchone()[0]), leaf.signature, signature + + +__all__ = [ + "LifecycleInputs", + "QualityReassessmentRefusal", + "REASON_LEGACY_INCOMPLETE_DAY", + "REASSESSABLE_STATES", + "REASSESSMENT_STATUSES", + "ReassessmentResult", + "load_lifecycle_inputs", + "reassess_live_partition", +] diff --git a/tests/integration_quality_reassessment_postgres.py b/tests/integration_quality_reassessment_postgres.py new file mode 100644 index 00000000..06e49a75 --- /dev/null +++ b/tests/integration_quality_reassessment_postgres.py @@ -0,0 +1,351 @@ +#!/usr/bin/env python3 +"""Real PostgreSQL proof of the superseding reassessment (DG-R R01, ADR-0074 Amendment 2 A2.3). + +A partial ``ETHUSDT`` day is imported through the ordinary S13/S14 path (a ``valid`` revision with a ``pass`` assessment, the shape of a +damaged legacy live day). The reassessment writer retires it (``valid -> invalid``) with a ``fail`` that supersedes the exact +signature of the previous leaf and copies its bound evidence unchanged, then restores it with a superseding ``pass``. Reads exclude +the retired revision; the bytes, the manifests and every earlier report are untouched; the call is idempotent; each refusal writes +nothing; a concurrent S13 publication on the same dataset waits for the writer's short transaction; a crash between the report and +the lifecycle application converges when the call is repeated. Requires ``DATA_GATEWAY_TEST_DSN`` and a POSIX storage root. +""" + +from __future__ import annotations + +from dataclasses import replace +import gzip +import hashlib +import json +import os +from pathlib import Path +import sys +import tempfile +import threading +import time +from unittest import mock + +ROOT = Path(__file__).resolve().parents[1] +sys.path.insert(0, str(ROOT / "src")) +import psycopg # noqa: E402 +import quant_platform.data.quality_reassessment as reassessment # noqa: E402 +from quant_platform.access.catalog import Catalog # noqa: E402 +from quant_platform.access.gateway import DataGateway # noqa: E402 +from quant_platform.access.models import DataRequest, LifecyclePolicy # noqa: E402 +from quant_platform.application.bybit_archive_import import import_bybit_archive_day # noqa: E402 +from quant_platform.data import DatasetIdentity, Instant # noqa: E402 +from quant_platform.data.models import NoCoverage # noqa: E402 +from quant_platform.data.quality_lifecycle import ( # noqa: E402 + QualityLifecycleCatalog, + QualityLifecycleRefusal, + semantic_assessment_signature, +) +from quant_platform.data.quality_reassessment import ( # noqa: E402 + REASON_LEGACY_INCOMPLETE_DAY, + load_lifecycle_inputs, + reassess_live_partition, +) +from quant_platform.source_adapters.bybit import ( # noqa: E402 + BYBIT_ORDERING_PROVIDER, + BYBIT_TRADE_V1_CERTIFICATION_PROFILE, + BYBIT_TRADE_V1_CHECK_SUITE, + BYBIT_TRADE_V1_ORDERING_POLICY, +) + +HEADER = "timestamp,symbol,side,size,price,tickDirection,trdMatchID,grossValue,homeNotional,foreignNotional" +STORAGE_ROOT_ID = "reassess-hot" +INSTRUMENT = "ETHUSDT" +DAY = "2024-01-15" +START = Instant.parse("2024-01-15T00:00:00Z") +END = Instant.parse("2024-01-16T00:00:00Z") +ARCHIVE = {"url": "https://public.bybit.com/trading/ETHUSDT/ETHUSDT2024-01-15.csv.gz", "sha256": "a" * 64, "rows": 1568861} + + +def archive(directory: Path, day: str, rows: list[tuple[int, str, str]]) -> Path: + base = int(Instant.parse(f"{day}T00:00:00Z").epoch_ns // 1_000_000_000) + path = directory / f"{INSTRUMENT}{day}.csv.gz" + lines = [HEADER] + [f"{base + offset}.5,{INSTRUMENT},Buy,1,2500,PlusTick,{trade_id},1,1,1" for offset, trade_id, _ in rows] + path.write_bytes(gzip.compress(("\n".join(lines) + "\n").encode("utf-8"))) + return path + + +def sql(connection, query, params=()): + with connection.cursor() as cursor: + cursor.execute(query, params) + rows = cursor.fetchall() if cursor.description else None + connection.commit() + return rows + + +def reports(connection, key=f"dt={DAY}"): + return sql(connection, "SELECT r.status, r.metrics, r.violations, r.code_ref, r.report_id::text FROM catalog.quality_reports r " + "JOIN catalog.partitions p ON p.partition_id = r.partition_id JOIN catalog.datasets d ON d.dataset_id = p.dataset_id " + "WHERE d.instrument = %s AND p.partition_key = %s ORDER BY r.ran_at, r.report_id", (INSTRUMENT, key)) + + +def state(connection, key=f"dt={DAY}"): + return sql(connection, "SELECT p.state, p.revision FROM catalog.partitions p JOIN catalog.datasets d ON d.dataset_id = p.dataset_id " + "WHERE d.instrument = %s AND p.partition_key = %s ORDER BY p.revision", (INSTRUMENT, key)) + + +def signature(row) -> str: + return semantic_assessment_signature(BYBIT_TRADE_V1_CHECK_SUITE, row[0], row[1], row[2], row[3]) + + +def read(connection, policy, coverage="allow-partial"): + identity = DatasetIdentity("canonical", "trades", "bybit", INSTRUMENT, "trade-v1") + request = DataRequest(identity, START, END, schema_requirement="trade-v1", lifecycle_policy=policy, coverage_policy=coverage, + ordering_policy=BYBIT_TRADE_V1_ORDERING_POLICY) + try: + return [r.trade_id for r in DataGateway(Catalog(connection=connection), ordering_providers=(BYBIT_ORDERING_PROVIDER,)).read(request).records] + except NoCoverage: + return None + + +def call(connection, inputs, status, **kwargs): + arguments = dict(inputs=inputs, coverage_start=START, coverage_end=END, storage_root_id=STORAGE_ROOT_ID, + expected_profile=BYBIT_TRADE_V1_CERTIFICATION_PROFILE, expected_check_suite=BYBIT_TRADE_V1_CHECK_SUITE, + status=status, code_ref="r01-commit", archive_identity=ARCHIVE if status == "fail" else None) + arguments.update(kwargs) + return reassess_live_partition(connection, **arguments) + + +def refused(connection, inputs, status, message, **kwargs): + before = (reports(connection), state(connection)) + try: + call(connection, inputs, status, **kwargs) + except QualityLifecycleRefusal as exc: + assert message in str(exc), (message, str(exc)) + else: + raise AssertionError(f"expected a refusal containing {message!r}") + assert (reports(connection), state(connection)) == before, "a refused reassessment must write nothing" + + +def main() -> int: + dsn = os.environ.get("DATA_GATEWAY_TEST_DSN") + if not dsn: + print("POSTGRESQL QUALITY REASSESSMENT GATE NOT EXECUTED LOCALLY") + return 0 + with tempfile.TemporaryDirectory() as holder: + base = Path(holder) + storage, staging = base / "storage", base / "staging" + storage.mkdir() + staging.mkdir() + dataset_root = storage / "canonical" / "trades" / "bybit" / INSTRUMENT / "trade-v1" + with psycopg.connect(dsn) as connection: + sql(connection, "INSERT INTO catalog.schema_registry (schema_id,name,version,json_sha256,body) VALUES ('trade-v1','trade',1,%s,%s::jsonb) ON CONFLICT DO NOTHING", ("0" * 64, "{}")) + sql(connection, "INSERT INTO catalog.storage_roots (storage_root_id,abs_path,tier) VALUES (%s,%s,'hot')", (STORAGE_ROOT_ID, str(storage))) + + # the damaged legacy day: a partial day, valid, with one pass assessment + partial = archive(staging, DAY, [(10, "a", ""), (20, "b", ""), (30, "c", ""), (40, "d", "")]) + imported = import_bybit_archive_day(connection, instrument=INSTRUMENT, day=DAY, archive_path=partial, + storage_root_id=STORAGE_ROOT_ID, code_ref="import-commit") + assert imported.state == "valid", imported + key = f"dt={DAY}" + inputs = load_lifecycle_inputs(dataset_root / "dataset-manifest.json", dataset_root / key / "partition-manifest.json", + [dataset_root / key / "coverage-manifest.json"]) + parquet = dataset_root / key / "part-001.parquet" + manifest = dataset_root / key / "partition-manifest.json" + frozen = (hashlib.sha256(parquet.read_bytes()).hexdigest(), hashlib.sha256(manifest.read_bytes()).hexdigest(), + sql(connection, "SELECT p.content_sha256, p.manifest_sha256, p.created_at, p.closed_at FROM catalog.partitions p " + "JOIN catalog.datasets d ON d.dataset_id=p.dataset_id WHERE d.instrument=%s", (INSTRUMENT,))) + first = reports(connection) + assert len(first) == 1 and first[0][0] == "pass" and state(connection) == [("valid", 1)] + original = signature(first[0]) + assert read(connection, LifecyclePolicy.VALID_ONLY) == ["a", "b", "c", "d"] + + # 1. retire: valid -> invalid, a fail that supersedes the exact signature of the previous leaf + retired = call(connection, inputs, "fail") + assert retired.inserted and retired.superseded_signature == original, retired + assert (retired.lifecycle.prior_state, retired.lifecycle.resulting_state, retired.lifecycle.mutated) == ("valid", "invalid", True) + assert state(connection) == [("invalid", 1)] + after = reports(connection) + assert len(after) == 2 and after[0] == first[0], "the earlier report is untouched" + fail = after[1] + assert fail[0] == "fail" and fail[3] == "r01-commit" and fail[4] == retired.report_id + assert fail[1]["supersedes_assessment_signature"] == original + # the bound evidence is the previous report's, unchanged + for name in ("certification_profile", "natural_partition_identity", "dataset_manifest_sha256", "partition_manifest_sha256", + "physical_artifact_hash", "canonical_content_hash_v1", "coverage_manifest_id", "coverage_assertion_id", + "coverage_manifest_ids", "coverage_assertion_ids", "coverage_manifest_sha256"): + assert fail[1][name] == first[0][1][name], name + for category in ("source", "canonical", "physical", "manifests", "publication"): + assert fail[1]["evidence"][category] == first[0][1]["evidence"][category], category + assert fail[1]["evidence"]["coverage"]["status"] == "fail" + assert first[0][1]["evidence"]["coverage"]["status"] == "pass" + (violation,) = fail[2] + assert violation["message"] == REASON_LEGACY_INCOMPLETE_DAY and violation["archive"] == ARCHIVE, violation + + # 2. the retired revision is excluded by every lifecycle policy; the bytes and manifests are untouched + for policy in LifecyclePolicy: + assert not read(connection, policy), policy + assert read(connection, LifecyclePolicy.VALID_ONLY, "strict") is None + now = (hashlib.sha256(parquet.read_bytes()).hexdigest(), hashlib.sha256(manifest.read_bytes()).hexdigest(), + sql(connection, "SELECT p.content_sha256, p.manifest_sha256, p.created_at, p.closed_at FROM catalog.partitions p " + "JOIN catalog.datasets d ON d.dataset_id=p.dataset_id WHERE d.instrument=%s", (INSTRUMENT,))) + assert now == frozen, "retiring a revision must not change its bytes, its manifests or its catalog row" + + # 3. idempotent: the same call adds no leaf and changes no state + again = call(connection, inputs, "fail") + assert not again.inserted and again.report_id is None and not again.lifecycle.mutated, again + assert reports(connection) == after and state(connection) == [("invalid", 1)] + + # 4. restore: a superseding pass returns the revision to valid, through the same seam + restored = call(connection, inputs, "pass", code_ref="r01-restore") + assert restored.inserted and restored.superseded_signature == signature(fail), restored + assert (restored.lifecycle.prior_state, restored.lifecycle.resulting_state) == ("invalid", "valid") + third = reports(connection) + assert len(third) == 3 and third[:2] == after and third[2][1]["supersedes_assessment_signature"] == signature(fail) + assert third[2][1]["evidence"]["coverage"]["status"] == "pass" and not third[2][2] + assert read(connection, LifecyclePolicy.VALID_ONLY) == ["a", "b", "c", "d"] + assert not call(connection, inputs, "pass", code_ref="r01-restore").inserted + assert len(reports(connection)) == 3 + retired_again = call(connection, inputs, "fail") # a later retire supersedes the restore, not the first fail + assert retired_again.inserted and retired_again.superseded_signature == signature(third[2]) + assert state(connection) == [("invalid", 1)] + call(connection, inputs, "pass", code_ref="r01-restore-2") + assert state(connection) == [("valid", 1)] + + # 5. refusals write nothing + refused(connection, inputs, "warn", "a pass or a fail") + refused(connection, inputs, "fail", "producing commit", code_ref=" ") + refused(connection, inputs, "fail", "archive identity", archive_identity=None) + for field in ("partition_sha256", "dataset_sha256"): + refused(connection, replace(inputs, **{field: "0" * 64}), "fail", "") + refused(connection, inputs, "fail", "does not equal", coverage_end=Instant.parse("2024-01-15T12:00:00Z")) + refused(connection, inputs, "fail", "storage identity", storage_root_id="elsewhere") + wrong_revision = replace(inputs, partition={**inputs.partition, "revision": 3}) + refused(connection, wrong_revision, "fail", "target revision is missing") + for forced, message in (("writing", "not quality-eligible"), ("closed", "ordinary S13")): + sql(connection, "UPDATE catalog.partitions SET state=%s WHERE partition_key=%s", (forced, key)) + try: + refused(connection, inputs, "fail", message) + finally: + sql(connection, "UPDATE catalog.partitions SET state='valid' WHERE partition_key=%s", (key,)) + # a superseded revision, and a key whose live revision is another + sql(connection, "UPDATE catalog.partitions SET state='superseded' WHERE partition_key=%s", (key,)) + sql(connection, "INSERT INTO catalog.partitions (dataset_id, partition_key, revision, storage_root_id, rel_path, ts_start, ts_end, " + "row_count, byte_size, content_sha256, state, manifest_sha256, created_at, closed_at, producer, code_ref) " + "SELECT dataset_id, partition_key, 2, storage_root_id, rel_path, ts_start, ts_end, row_count, byte_size, " + "content_sha256, 'valid', manifest_sha256, created_at, closed_at, producer, code_ref FROM catalog.partitions " + "WHERE partition_key=%s AND revision=1", (key,)) + try: + refused(connection, inputs, "fail", "superseded") + finally: + sql(connection, "DELETE FROM catalog.partitions WHERE partition_key=%s AND revision=2", (key,)) + sql(connection, "UPDATE catalog.partitions SET state='valid' WHERE partition_key=%s", (key,)) + assert state(connection) == [("valid", 1)] + # a foreign dataset (the dataset manifest of another instrument names no catalog dataset) + foreign = replace(inputs, dataset={**inputs.dataset, "instrument": "SOLUSDT"}) + refused(connection, foreign, "fail", "dataset natural identity") + + # 6. the writer holds no lock beyond its transaction, and a concurrent S13 publication on the dataset waits for it + gate, entered, released = threading.Event(), threading.Event(), threading.Event() + real_select = reassessment.select_current_quality_assessment + + def gated(*args, **kwargs): + entered.set() + assert gate.wait(30), "the test did not release the writer" + return real_select(*args, **kwargs) + + outcome: dict = {} + + def writer(): + with psycopg.connect(dsn) as other: + outcome["writer"] = call(other, inputs, "fail") + + def publisher(): + with psycopg.connect(dsn) as other: + other_day = archive(staging, "2024-01-16", [(5, "e", "")]) + outcome["published"] = import_bybit_archive_day( + other, instrument=INSTRUMENT, day="2024-01-16", archive_path=other_day, storage_root_id=STORAGE_ROOT_ID, + code_ref="import-commit-2") + released.set() + + with mock.patch.object(reassessment, "select_current_quality_assessment", gated): + first_thread = threading.Thread(target=writer, daemon=True) + first_thread.start() + assert entered.wait(30), "the writer did not reach its report" + second_thread = threading.Thread(target=publisher, daemon=True) + second_thread.start() + time.sleep(2.0) + assert not released.is_set(), "a publication on the same dataset must wait for the writer's transaction" + gate.set() + first_thread.join(60) + second_thread.join(60) + assert outcome["writer"].inserted and outcome["published"].state == "valid", outcome + assert state(connection) == [("invalid", 1)] and state(connection, "dt=2024-01-16") == [("valid", 1)] + with psycopg.connect(dsn) as probe, probe.cursor() as cursor: # no lock is held by the writer any more + cursor.execute("SELECT 1 FROM catalog.datasets WHERE instrument = %s FOR UPDATE NOWAIT", (INSTRUMENT,)) + cursor.execute("SELECT 1 FROM catalog.partitions WHERE partition_key = %s FOR UPDATE NOWAIT", (key,)) + + # 7. a crash between the report and the lifecycle application leaves a current fail with the state still valid; the + # same call again converges without a second report + call(connection, inputs, "pass", code_ref="r01-restore-3") + assert state(connection) == [("valid", 1)] + count = len(reports(connection)) + with mock.patch.object(QualityLifecycleCatalog, "apply_partition_lifecycle", side_effect=RuntimeError("crash")): + try: + call(connection, inputs, "fail", code_ref="r01-commit-2") + except RuntimeError: + pass + else: + raise AssertionError("the crash was not raised") + with psycopg.connect(dsn) as other: # the report was committed on its own, before the seam + assert len(reports(other)) == count + 1 and state(other) == [("valid", 1)] + healed = call(connection, inputs, "fail", code_ref="r01-commit-2") + assert not healed.inserted and healed.lifecycle.mutated and state(connection) == [("invalid", 1)] + assert len(reports(connection)) == count + 1 + + # a fail that names another archive identity, or comes from another commit, is a different assessment: it supersedes the + # current fail instead of being taken for a repeat (the state stays invalid) + count = len(reports(connection)) + other_archive = call(connection, inputs, "fail", code_ref="r01-commit-2", archive_identity={**ARCHIVE, "sha256": "b" * 64}) + assert other_archive.inserted and len(reports(connection)) == count + 1 and state(connection) == [("invalid", 1)] + other_commit = call(connection, inputs, "fail", code_ref="r01-commit-3", archive_identity={**ARCHIVE, "sha256": "b" * 64}) + assert other_commit.inserted and len(reports(connection)) == count + 2 and state(connection) == [("invalid", 1)] + assert not call(connection, inputs, "fail", code_ref="r01-commit-3", archive_identity={**ARCHIVE, "sha256": "b" * 64}).inserted + + # a leaf that is a warn (a degraded source) cannot be turned into a pass by this writer: the copied evidence would not allow + # it; a fail over it is fine + warn_day = archive(staging, "2024-01-17", [(7, "w", "")]) + import_bybit_archive_day(connection, instrument=INSTRUMENT, day="2024-01-17", archive_path=warn_day, + storage_root_id=STORAGE_ROOT_ID, code_ref="import-commit-3") + warn_key = "dt=2024-01-17" + warn_inputs = load_lifecycle_inputs(dataset_root / "dataset-manifest.json", dataset_root / warn_key / "partition-manifest.json", + [dataset_root / warn_key / "coverage-manifest.json"]) + (row,) = sql(connection, "SELECT r.report_id::text, r.metrics FROM catalog.quality_reports r JOIN catalog.partitions p ON " + "p.partition_id = r.partition_id WHERE p.partition_key = %s", (warn_key,)) + warn_metrics = row[1] + warn_metrics["evidence"]["source"]["status"] = "warn" + sql(connection, "UPDATE catalog.quality_reports SET status='warn', metrics=%s::jsonb, violations=%s::jsonb WHERE report_id=%s", + (json.dumps(warn_metrics), '[{"category": "source", "message": "warn"}]', row[0])) + warn_args = dict(coverage_start=Instant.parse("2024-01-17T00:00:00Z"), coverage_end=Instant.parse("2024-01-18T00:00:00Z")) + before = reports(connection, warn_key) + try: + call(connection, warn_inputs, "pass", **warn_args) + except QualityLifecycleRefusal as exc: + assert "evidence does not allow it" in str(exc), str(exc) + else: + raise AssertionError("a pass over a warn leaf must be refused") + assert reports(connection, warn_key) == before + over_warn = call(connection, warn_inputs, "fail", **warn_args) + assert over_warn.inserted and state(connection, warn_key) == [("invalid", 1)], over_warn + + # 8. the ordinary path still refuses to record a report for a partition that is not closed + from quant_platform.data.publication_catalog import CatalogPublicationConflict, CatalogPublicationWriter + + partition_id = sql(connection, "SELECT p.partition_id::text FROM catalog.partitions p WHERE p.partition_key = %s", (key,))[0][0] + try: + CatalogPublicationWriter(connection).record_quality_report( + partition_id=partition_id, check_suite=BYBIT_TRADE_V1_CHECK_SUITE, status="fail", metrics={}, violations=[], + code_ref="x") + except CatalogPublicationConflict as exc: + assert "closed partition" in str(exc) + else: + raise AssertionError("record_quality_report must keep refusing a partition that is not closed") + connection.rollback() + print("POSTGRESQL QUALITY REASSESSMENT GATE PASSED") + return 0 + + +if __name__ == "__main__": + raise SystemExit(main()) diff --git a/tests/test_package_boundaries_v1.py b/tests/test_package_boundaries_v1.py index a2b646aa..69b3fcb5 100644 --- a/tests/test_package_boundaries_v1.py +++ b/tests/test_package_boundaries_v1.py @@ -38,6 +38,7 @@ "quant_platform.data.publication_eligibility": "producer", "quant_platform.data.publication_eligibility_catalog": "producer", "quant_platform.data.quality_lifecycle": "producer", + "quant_platform.data.quality_reassessment": "producer", "quant_platform.data.repair": "producer", "quant_platform.access": "access", "quant_platform.access.models": "access", diff --git a/tests/test_quality_reassessment_v1.py b/tests/test_quality_reassessment_v1.py new file mode 100644 index 00000000..d33221fc --- /dev/null +++ b/tests/test_quality_reassessment_v1.py @@ -0,0 +1,74 @@ +"""DG-R R01: the argument rules of the reassessment writer (the catalog behaviour is the PostgreSQL gate).""" + +from __future__ import annotations + +from pathlib import Path +import sys +import unittest + +ROOT = Path(__file__).resolve().parents[1] +sys.path.insert(0, str(ROOT / "src")) + +from quant_platform.data import Instant # noqa: E402 +from quant_platform.data.quality_lifecycle import QualityLifecycleRefusal # noqa: E402 +from quant_platform.data.quality_reassessment import ( # noqa: E402 + REASSESSABLE_STATES, + REASSESSMENT_STATUSES, + LifecycleInputs, + QualityReassessmentRefusal, + reassess_live_partition, +) + + +class Untouchable: + """A connection that must not be used: a refused argument never reaches the catalog.""" + + def cursor(self): + raise AssertionError("the catalog was touched") + + def commit(self): + raise AssertionError("the catalog was touched") + + def rollback(self): + raise AssertionError("the catalog was touched") + + +INPUTS = LifecycleInputs({}, "d" * 64, {}, "p" * 64, ("c",), ("a",), ("s",)) +NOW = Instant.parse("2024-01-15T00:00:00Z") + + +def call(**overrides): + arguments = dict(inputs=INPUTS, coverage_start=NOW, coverage_end=NOW, storage_root_id="hot", expected_profile="profile", + expected_check_suite="suite", status="fail", code_ref="commit", archive_identity={"url": "u"}) + arguments.update(overrides) + return reassess_live_partition(Untouchable(), **arguments) + + +class ArgumentRuleTests(unittest.TestCase): + def test_only_a_pass_or_a_fail_is_a_reassessment(self) -> None: + self.assertEqual({"pass", "fail"}, set(REASSESSMENT_STATUSES)) + for status in ("warn", "closed", "", None): + with self.assertRaises(QualityReassessmentRefusal): + call(status=status) + + def test_the_producing_commit_is_required(self) -> None: + for code_ref in ("", " ", None): + with self.assertRaises(QualityReassessmentRefusal): + call(code_ref=code_ref) + + def test_a_fail_names_the_archive_it_is_measured_against_a_pass_does_not_need_one(self) -> None: + for identity in (None, {}): + with self.assertRaises(QualityReassessmentRefusal): + call(archive_identity=identity) + with self.assertRaises(AssertionError): # a valid pass gets as far as the catalog + call(status="pass", archive_identity=None) + + def test_the_states_a_reassessment_may_touch_exclude_closed_writing_and_superseded(self) -> None: + self.assertEqual({"valid", "degraded", "invalid"}, set(REASSESSABLE_STATES)) + + def test_a_refusal_is_a_lifecycle_refusal(self) -> None: + self.assertTrue(issubclass(QualityReassessmentRefusal, QualityLifecycleRefusal)) + + +if __name__ == "__main__": + unittest.main()