File size: 15,805 Bytes
ebcde1f
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
"""Single-archive, streaming normalization for the locked M8 producer.

This module intentionally has no all-calendar entry point.  A caller receives
one already authenticated :class:`AcquiredDailyArchive` and decides when the
CSV member may be opened.  In particular, the M8 producer supplies a
``before_member_open`` guard for held-out dates so the durable analysis lock is
revalidated at the lowest economic-data boundary.
"""

from __future__ import annotations

from collections.abc import Callable, Iterator, Mapping
from dataclasses import dataclass
from datetime import UTC, datetime
from pathlib import Path
from typing import Any, Literal, cast

import pyarrow as pa  # type: ignore[import-untyped]

from microstructure.data.binance_archive import (
    AcquiredDailyArchive,
    BinanceArchivePayloadError,
)
from microstructure.data.quality import IncrementalQualityValidator
from microstructure.data.storage import DatasetWriteResult, write_partitioned_parquet
from microstructure.m8_config import M8Period, M8StudyConfig
from microstructure.m8_manifest import M8ArchiveEntry, M8NormalizedPart, M8SymbolMetadata
from microstructure.provenance import read_json, sha256_file

_DEFAULT_BATCH_ROWS = 65_536


class M8NormalizationError(RuntimeError):
    """A system or caller-contract failure while normalizing one archive."""


M8NormalizationFailureKind = Literal[
    "PAYLOAD_OR_CONTINUITY",
    "QUALITY_GATE",
    "POSTWRITE_CONSISTENCY",
]
M8NormalizationEvidenceCompletion = Literal[
    "PARTIAL_STREAM",
    "COMPLETE_DATASET_AND_QUALITY",
]


class M8InsufficientDataError(M8NormalizationError):
    """A deterministic archive/data-quality failure in a declared date."""

    def __init__(
        self,
        symbol: str,
        study_date: str,
        reason: str,
        *,
        failure_kind: M8NormalizationFailureKind | None = None,
        evidence_completion: M8NormalizationEvidenceCompletion | None = None,
        completed_evidence: M8ArchiveEntry | None = None,
    ) -> None:
        super().__init__(f"{symbol}/{study_date}: {reason}")
        self.symbol = symbol
        self.study_date = study_date
        self.reason = reason
        self.failure_kind = failure_kind
        self.evidence_completion = evidence_completion
        self.completed_evidence = completed_evidence


@dataclass(frozen=True, slots=True)
class M8NormalizedArchive:
    """One complete archive entry plus its bounded normalization output."""

    entry: M8ArchiveEntry
    output_root: Path


def _object(value: object, label: str) -> Mapping[str, Any]:
    if not isinstance(value, Mapping):
        raise M8NormalizationError(f"{label} must be a JSON object")
    return cast(Mapping[str, Any], value)


def _downloaded_at(acquired: AcquiredDailyArchive) -> str:
    try:
        sidecar = _object(
            read_json(acquired.archive_artifact.manifest_path),
            "archive source sidecar",
        )
    except M8NormalizationError:
        raise
    except (OSError, UnicodeDecodeError, ValueError) as exc:
        raise M8NormalizationError(f"cannot parse archive source sidecar: {exc}") from exc
    value = sidecar.get("downloaded_at_utc")
    if type(value) is not str or not value:
        raise M8NormalizationError("archive source sidecar lacks downloaded_at_utc")
    return value


def _parts(storage: DatasetWriteResult) -> tuple[M8NormalizedPart, ...]:
    return tuple(
        M8NormalizedPart(
            data_path=item.data_path.resolve(),
            data_sha256=item.data_sha256,
            data_bytes=item.data_path.stat().st_size,
            sidecar_path=item.manifest_path.resolve(),
            sidecar_sha256=item.manifest_sha256,
            sidecar_bytes=item.manifest_path.stat().st_size,
            rows=item.rows,
            write_ordinal=item.write_ordinal,
            observed_start_ns=item.observed_start_ns,
            observed_end_inclusive_ns=item.observed_end_inclusive_ns,
        )
        for item in storage.artifacts
    )


def _validate_contract(
    config: M8StudyConfig,
    period: M8Period,
    metadata: M8SymbolMetadata,
    acquired: AcquiredDailyArchive,
    root: Path,
) -> None:
    request = acquired.request
    if request.symbol != metadata.symbol or request.date != period.date:
        raise M8NormalizationError("acquired archive identity disagrees with requested period")
    if request.tick_size != metadata.tick_size or request.lot_size != metadata.lot_size:
        raise M8NormalizationError("acquired archive scales disagree with frozen metadata")
    if metadata.status != "TRADING" or metadata.symbol not in config.study.symbols:
        raise M8NormalizationError("symbol metadata does not prove a frozen TRADING symbol")
    evidence = (
        acquired.archive_artifact.path.resolve(),
        acquired.archive_artifact.manifest_path.resolve(),
        acquired.checksum_artifact.path.resolve(),
        acquired.checksum_artifact.manifest_path.resolve(),
    )
    if len(set(evidence)) != len(evidence) or any(
        not path.is_relative_to(root) or not path.is_file() for path in evidence
    ):
        raise M8NormalizationError("stage-local raw evidence is missing, reused, or escapes root")
    if acquired.upstream_sha256 != acquired.archive_artifact.sha256:
        raise M8NormalizationError("official checksum does not authenticate the stage-local ZIP")
    for artifact in (acquired.archive_artifact, acquired.checksum_artifact):
        if artifact.path.stat().st_size != artifact.bytes:
            raise M8NormalizationError("stage-local raw artifact byte count changed")
        if sha256_file(artifact.path) != artifact.sha256:
            raise M8NormalizationError("stage-local raw artifact checksum changed")
        if sha256_file(artifact.manifest_path) != artifact.manifest_sha256:
            raise M8NormalizationError("stage-local raw sidecar checksum changed")


def _validate_frozen_period(config: M8StudyConfig, period: M8Period) -> None:
    """Reject a date/role reinterpretation before any economic member can open."""

    if period not in config.periods:
        raise M8NormalizationError("M8 period date/role is not an exact frozen configuration entry")


def normalize_m8_archive(
    config: M8StudyConfig,
    period: M8Period,
    metadata: M8SymbolMetadata,
    acquired: AcquiredDailyArchive,
    input_root: str | Path,
    *,
    output_root: str | Path | None = None,
    before_member_open: Callable[[], None] | None = None,
    batch_rows: int = _DEFAULT_BATCH_ROWS,
) -> M8NormalizedArchive:
    """Stream one authenticated archive through DQ and partitioned Parquet.

    ``input_root`` is the immutable raw-evidence authority used for containment
    checks.  ``output_root`` may isolate normalized and DQ artifacts elsewhere;
    omitting it preserves the legacy co-located layout.  The function never
    retains a full daily trade table.  Archive parsing, incremental quality
    validation, and Parquet writing are bounded by ``batch_rows``.  Any
    deterministic payload/continuity/DQ failure is exposed as
    :class:`M8InsufficientDataError` so the producer can publish a terminal
    failed-result bundle without selecting a replacement date.
    """

    if isinstance(batch_rows, bool) or not isinstance(batch_rows, int) or batch_rows < 1:
        raise ValueError("batch_rows must be a positive integer")
    raw_root = Path(input_root).resolve()
    if not raw_root.is_dir():
        raise M8NormalizationError(f"stage-local M8 input root is missing: {raw_root}")
    derived_root = raw_root if output_root is None else Path(output_root).resolve()
    if derived_root.exists() and not derived_root.is_dir():
        raise M8NormalizationError(
            f"stage-local M8 normalization output root is not a directory: {derived_root}"
        )
    _validate_frozen_period(config, period)
    if period.role in {"primary_test", "replication_test"} and before_member_open is None:
        raise M8NormalizationError(
            "held-out M8 archives require a lock-revalidation callback before member open"
        )
    _validate_contract(config, period, metadata, acquired, raw_root)
    symbol = metadata.symbol
    study_date = period.date.isoformat()
    base = derived_root / "normalized" / symbol / study_date
    if base.exists():
        raise M8NormalizationError(
            f"refusing to overwrite prior normalized evidence for {symbol}/{study_date}"
        )
    quality_root = derived_root / "quality" / symbol / study_date
    if quality_root.exists():
        raise M8NormalizationError(
            f"refusing to overwrite prior quality evidence for {symbol}/{study_date}"
        )
    quality_root.mkdir(parents=True, exist_ok=False)
    findings_path = quality_root / "findings.jsonl"
    report_path = quality_root / "report.json"
    day_start = datetime(period.date.year, period.date.month, period.date.day, tzinfo=UTC)
    day_start_ns = int(day_start.timestamp()) * 1_000_000_000
    day_end_ns = day_start_ns + 86_400 * 1_000_000_000
    stream = acquired.iter_normalized_batches(
        batch_rows=batch_rows,
        before_member_open=before_member_open,
    )
    try:
        with IncrementalQualityValidator(
            "trades",
            findings_jsonl_path=findings_path,
        ) as validator:

            def validated_batches() -> Iterator[pa.RecordBatch]:
                for batch in stream:
                    if batch.num_rows < 1 or batch.num_rows > batch_rows:
                        raise M8NormalizationError(
                            "archive stream violated its configured row bound"
                        )
                    validator.update(batch)
                    yield batch

            storage = write_partitioned_parquet(
                validated_batches(),
                root=base,
                dataset="trades",
                schema_name="trades",
                source=config.study.source,
                source_uri=acquired.archive_artifact.source_uri,
                downloaded_at_utc=_downloaded_at(acquired),
                source_checksum_sha256=acquired.archive_artifact.sha256,
                requested_start_ns=day_start_ns,
                requested_end_ns=day_end_ns,
                max_input_batch_rows=batch_rows,
                max_rows_per_file=250_000,
            )
            report = validator.finish()
        summary = stream.summary
        report.write_json(report_path)
    except BinanceArchivePayloadError as exc:
        stream.close()
        raise M8InsufficientDataError(
            symbol,
            study_date,
            str(exc),
            failure_kind="PAYLOAD_OR_CONTINUITY",
            evidence_completion="PARTIAL_STREAM",
        ) from exc
    except BaseException:
        stream.close()
        raise

    if summary.symbol != symbol or summary.date != study_date:
        raise M8InsufficientDataError(
            symbol,
            study_date,
            "archive summary identity disagrees",
            failure_kind="POSTWRITE_CONSISTENCY",
            evidence_completion="COMPLETE_DATASET_AND_QUALITY",
        )
    if summary.source_archive_sha256 != acquired.archive_artifact.sha256:
        raise M8InsufficientDataError(
            symbol,
            study_date,
            "archive summary checksum disagrees",
            failure_kind="POSTWRITE_CONSISTENCY",
            evidence_completion="COMPLETE_DATASET_AND_QUALITY",
        )
    if summary.expanded_bytes != acquired.declared_uncompressed_bytes:
        raise M8InsufficientDataError(
            symbol,
            study_date,
            "expanded byte count disagrees",
            failure_kind="POSTWRITE_CONSISTENCY",
            evidence_completion="COMPLETE_DATASET_AND_QUALITY",
        )
    if storage.rows != summary.rows or report.rows_checked != summary.rows:
        raise M8InsufficientDataError(
            symbol,
            study_date,
            "stream/storage/DQ row counts disagree",
            failure_kind="POSTWRITE_CONSISTENCY",
            evidence_completion="COMPLETE_DATASET_AND_QUALITY",
        )
    if summary.last_trade_id - summary.first_trade_id + 1 != summary.rows:
        raise M8InsufficientDataError(
            symbol,
            study_date,
            "aggregate trade IDs are noncontiguous",
            failure_kind="POSTWRITE_CONSISTENCY",
            evidence_completion="COMPLETE_DATASET_AND_QUALITY",
        )
    normalized_parts = _parts(storage)
    if not normalized_parts or sum(part.rows for part in normalized_parts) != summary.rows:
        raise M8InsufficientDataError(
            symbol,
            study_date,
            "normalized part accounting failed",
            failure_kind="POSTWRITE_CONSISTENCY",
            evidence_completion="COMPLETE_DATASET_AND_QUALITY",
        )
    entry = M8ArchiveEntry(
        symbol=symbol,
        date=period.date,
        role=period.role,
        complete=True,
        rows=summary.rows,
        first_trade_id=summary.first_trade_id,
        last_trade_id=summary.last_trade_id,
        observed_start_ns=summary.first_event_ts_ns,
        observed_end_inclusive_ns=summary.last_event_ts_ns,
        tick_size=metadata.tick_size,
        lot_size=metadata.lot_size,
        raw_zip_path=acquired.archive_artifact.path.resolve(),
        raw_zip_sha256=acquired.archive_artifact.sha256,
        raw_zip_bytes=acquired.archive_artifact.bytes,
        raw_uncompressed_bytes=summary.expanded_bytes,
        raw_source_uri=acquired.archive_artifact.source_uri,
        raw_source_manifest_path=acquired.archive_artifact.manifest_path.resolve(),
        raw_source_manifest_sha256=acquired.archive_artifact.manifest_sha256,
        raw_source_manifest_bytes=acquired.archive_artifact.manifest_path.stat().st_size,
        raw_checksum_path=acquired.checksum_artifact.path.resolve(),
        raw_checksum_sha256=acquired.checksum_artifact.sha256,
        raw_checksum_bytes=acquired.checksum_artifact.bytes,
        raw_checksum_source_uri=acquired.checksum_artifact.source_uri,
        raw_checksum_source_manifest_path=acquired.checksum_artifact.manifest_path.resolve(),
        raw_checksum_source_manifest_sha256=acquired.checksum_artifact.manifest_sha256,
        raw_checksum_source_manifest_bytes=acquired.checksum_artifact.manifest_path.stat().st_size,
        normalized_dataset_manifest_path=storage.manifest_path.resolve(),
        normalized_dataset_manifest_sha256=storage.manifest_sha256,
        normalized_dataset_manifest_bytes=storage.manifest_path.stat().st_size,
        normalized_parts=normalized_parts,
        quality_report_path=report_path.resolve(),
        quality_report_sha256=sha256_file(report_path),
        quality_report_bytes=report_path.stat().st_size,
        quality_findings_path=findings_path.resolve(),
        quality_findings_sha256=sha256_file(findings_path),
        quality_findings_bytes=findings_path.stat().st_size,
        quality_errors=report.error_count,
        quality_warnings=report.warning_count,
    )
    if report.error_count or (report.warning_count and not config.quality.allow_quality_warnings):
        raise M8InsufficientDataError(
            symbol,
            study_date,
            f"quality gate reported {report.error_count} errors and "
            f"{report.warning_count} warnings",
            failure_kind="QUALITY_GATE",
            evidence_completion="COMPLETE_DATASET_AND_QUALITY",
            completed_evidence=entry,
        )
    return M8NormalizedArchive(entry=entry, output_root=derived_root)


__all__ = [
    "M8InsufficientDataError",
    "M8NormalizationError",
    "M8NormalizationEvidenceCompletion",
    "M8NormalizationFailureKind",
    "M8NormalizedArchive",
    "normalize_m8_archive",
]