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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
"""Pure snapshot-plus-delta order-book reconstruction.

Sequence order is authoritative.  Exchange timestamps are never used to sort
updates, so a timestamp reversal is reportable without concealing or inventing
book continuity.
"""

from __future__ import annotations

import math
from collections.abc import Mapping
from dataclasses import dataclass
from typing import Literal

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

from microstructure.data.schemas import SCHEMA_VERSION, table_from_records

BookLevel = tuple[int, int]
_MAX_BOOK_LEVELS_PER_SIDE = 10_000


class BookInvariantError(ValueError):
    """Raised when a snapshot or delta contains an impossible book level."""


class _BookCapacityError(BookInvariantError):
    """Raised before retained book state can exceed its hard memory bound."""


@dataclass(frozen=True, slots=True)
class BookSnapshot:
    venue: str
    symbol: str
    snapshot_id: str
    request_ts_ns: int
    received_ts_ns: int
    available_ts_ns: int
    continuity_id: str
    last_update_id: int
    depth_limit: int
    bids: tuple[BookLevel, ...]
    asks: tuple[BookLevel, ...]
    tick_size: float
    lot_size: float
    source_artifact_id: str

    def to_record(self) -> dict[str, object]:
        return {
            "schema_version": SCHEMA_VERSION,
            "venue": self.venue,
            "symbol": self.symbol,
            "snapshot_id": self.snapshot_id,
            "request_ts_ns": self.request_ts_ns,
            "received_ts_ns": self.received_ts_ns,
            "available_ts_ns": self.available_ts_ns,
            "continuity_id": self.continuity_id,
            "last_update_id": self.last_update_id,
            "depth_limit": self.depth_limit,
            "bids": [
                {"price_ticks": price, "quantity_lots": quantity} for price, quantity in self.bids
            ],
            "asks": [
                {"price_ticks": price, "quantity_lots": quantity} for price, quantity in self.asks
            ],
            "tick_size": self.tick_size,
            "lot_size": self.lot_size,
            "source_artifact_id": self.source_artifact_id,
        }


@dataclass(frozen=True, slots=True)
class DepthDelta:
    venue: str
    symbol: str
    event_ts_ns: int
    received_ts_ns: int | None
    available_ts_ns: int
    availability_basis: str
    capture_seq: int | None
    continuity_id: str
    first_update_id: int
    last_update_id: int
    previous_update_id: int | None
    bids: tuple[BookLevel, ...]
    asks: tuple[BookLevel, ...]
    tick_size: float
    lot_size: float
    source_artifact_id: str

    def to_record(self) -> dict[str, object]:
        return {
            "schema_version": SCHEMA_VERSION,
            "venue": self.venue,
            "symbol": self.symbol,
            "event_ts_ns": self.event_ts_ns,
            "received_ts_ns": self.received_ts_ns,
            "available_ts_ns": self.available_ts_ns,
            "availability_basis": self.availability_basis,
            "capture_seq": self.capture_seq,
            "continuity_id": self.continuity_id,
            "first_update_id": self.first_update_id,
            "last_update_id": self.last_update_id,
            "previous_update_id": self.previous_update_id,
            "bids": [
                {"price_ticks": price, "quantity_lots": quantity} for price, quantity in self.bids
            ],
            "asks": [
                {"price_ticks": price, "quantity_lots": quantity} for price, quantity in self.asks
            ],
            "tick_size": self.tick_size,
            "lot_size": self.lot_size,
            "source_artifact_id": self.source_artifact_id,
        }


@dataclass(frozen=True, slots=True)
class SequenceGap:
    venue: str
    symbol: str
    continuity_id: str
    expected_sequence: int
    observed_sequence_start: int
    observed_sequence_end: int
    missing_start: int
    missing_end: int
    detected_ts_ns: int
    reason: str
    source_artifact_id: str

    def to_record(self) -> dict[str, object]:
        return {
            "schema_version": SCHEMA_VERSION,
            "venue": self.venue,
            "symbol": self.symbol,
            "continuity_id": self.continuity_id,
            "expected_sequence": self.expected_sequence,
            "observed_sequence_start": self.observed_sequence_start,
            "observed_sequence_end": self.observed_sequence_end,
            "missing_start": self.missing_start,
            "missing_end": self.missing_end,
            "detected_ts_ns": self.detected_ts_ns,
            "reason": self.reason,
            "source_artifact_id": self.source_artifact_id,
        }


@dataclass(frozen=True, slots=True)
class ReconstructionResult:
    status: Literal["LIVE", "GAPPED", "INVALID"]
    observations: pa.Table
    gaps: pa.Table
    stale_events: int
    final_update_id: int


ReconstructionOutcome = Literal[
    "OBSERVED",
    "STALE",
    "GAP",
    "INVALID",
    "EXCLUDED_AFTER_TERMINAL",
]


@dataclass(frozen=True, slots=True)
class ReconstructionStep:
    """Bounded result of applying one delta to one continuity epoch."""

    outcome: ReconstructionOutcome
    observation: Mapping[str, object] | None
    gap: SequenceGap | None


def _levels_to_book(levels: tuple[BookLevel, ...], side: str) -> dict[int, int]:
    if len(levels) > _MAX_BOOK_LEVELS_PER_SIDE:
        raise _BookCapacityError(
            f"{side} snapshot exceeds {_MAX_BOOK_LEVELS_PER_SIDE} retained levels"
        )
    result: dict[int, int] = {}
    for price, quantity in levels:
        if price <= 0:
            raise BookInvariantError(f"{side} snapshot price must be positive: {price}")
        if quantity <= 0:
            raise BookInvariantError(f"{side} snapshot quantity must be positive: {quantity}")
        if price in result:
            raise BookInvariantError(f"duplicate {side} snapshot price: {price}")
        result[price] = quantity
    if not result:
        raise BookInvariantError(f"{side} snapshot must not be empty")
    return result


def _apply_side(book: dict[int, int], changes: tuple[BookLevel, ...], side: str) -> None:
    for price, quantity in changes:
        if price <= 0:
            raise BookInvariantError(f"{side} delta price must be positive: {price}")
        if quantity < 0:
            raise BookInvariantError(f"{side} delta quantity must not be negative: {quantity}")
        if quantity == 0:
            book.pop(price, None)
        else:
            book[price] = quantity
    if len(book) > _MAX_BOOK_LEVELS_PER_SIDE:
        raise _BookCapacityError(f"{side} book exceeds {_MAX_BOOK_LEVELS_PER_SIDE} retained levels")


def _depth(book: dict[int, int], *, bids: bool, levels: int, lot_size: float) -> float:
    ordered = sorted(book, reverse=bids)[:levels]
    return sum(book[price] for price in ordered) * lot_size


def _queue_imbalance(bid_depth: float, ask_depth: float) -> float:
    total = bid_depth + ask_depth
    return (bid_depth - ask_depth) / total if total > 0.0 else 0.0


def _observation(
    *,
    snapshot: BookSnapshot,
    delta: DepthDelta,
    bids: dict[int, int],
    asks: dict[int, int],
    valid: bool,
) -> dict[str, object]:
    if not bids or not asks:
        raise BookInvariantError("delta removed every price level from one side of the book")
    best_bid_ticks = max(bids)
    best_ask_ticks = min(asks)
    bid_quantity_lots = bids[best_bid_ticks]
    ask_quantity_lots = asks[best_ask_ticks]
    best_bid = best_bid_ticks * snapshot.tick_size
    best_ask = best_ask_ticks * snapshot.tick_size
    bid_quantity = bid_quantity_lots * snapshot.lot_size
    ask_quantity = ask_quantity_lots * snapshot.lot_size
    mid_price = (best_bid + best_ask) / 2.0
    microprice = (best_ask * bid_quantity + best_bid * ask_quantity) / (bid_quantity + ask_quantity)
    depth_bid_1 = _depth(bids, bids=True, levels=1, lot_size=snapshot.lot_size)
    depth_ask_1 = _depth(asks, bids=False, levels=1, lot_size=snapshot.lot_size)
    depth_bid_5 = _depth(bids, bids=True, levels=5, lot_size=snapshot.lot_size)
    depth_ask_5 = _depth(asks, bids=False, levels=5, lot_size=snapshot.lot_size)
    depth_bid_10 = _depth(bids, bids=True, levels=10, lot_size=snapshot.lot_size)
    depth_ask_10 = _depth(asks, bids=False, levels=10, lot_size=snapshot.lot_size)
    return {
        "schema_version": SCHEMA_VERSION,
        "venue": snapshot.venue,
        "symbol": snapshot.symbol,
        "event_ts_ns": delta.event_ts_ns,
        "received_ts_ns": delta.received_ts_ns,
        "available_ts_ns": max(snapshot.available_ts_ns, delta.available_ts_ns),
        "availability_basis": delta.availability_basis,
        "capture_seq": delta.capture_seq,
        "continuity_id": snapshot.continuity_id,
        "sequence_start": delta.first_update_id,
        "sequence_end": delta.last_update_id,
        "is_valid": valid,
        "best_bid_ticks": best_bid_ticks,
        "best_ask_ticks": best_ask_ticks,
        "bid_quantity_lots": bid_quantity_lots,
        "ask_quantity_lots": ask_quantity_lots,
        "tick_size": snapshot.tick_size,
        "lot_size": snapshot.lot_size,
        "best_bid": best_bid,
        "best_ask": best_ask,
        "bid_quantity": bid_quantity,
        "ask_quantity": ask_quantity,
        "spread": best_ask - best_bid,
        "mid_price": mid_price,
        "microprice": microprice,
        "depth_bid_1": depth_bid_1,
        "depth_ask_1": depth_ask_1,
        "depth_bid_5": depth_bid_5,
        "depth_ask_5": depth_ask_5,
        "depth_bid_10": depth_bid_10,
        "depth_ask_10": depth_ask_10,
        "queue_imbalance_1": _queue_imbalance(depth_bid_1, depth_ask_1),
        "queue_imbalance_5": _queue_imbalance(depth_bid_5, depth_ask_5),
        "queue_imbalance_10": _queue_imbalance(depth_bid_10, depth_ask_10),
        "source_artifact_id": delta.source_artifact_id,
    }


def _gap(snapshot: BookSnapshot, delta: DepthDelta, expected: int, reason: str) -> SequenceGap:
    missing_end = max(expected, delta.first_update_id - 1)
    return SequenceGap(
        venue=snapshot.venue,
        symbol=snapshot.symbol,
        continuity_id=snapshot.continuity_id,
        expected_sequence=expected,
        observed_sequence_start=delta.first_update_id,
        observed_sequence_end=delta.last_update_id,
        missing_start=expected,
        missing_end=missing_end,
        detected_ts_ns=delta.available_ts_ns,
        reason=reason,
        source_artifact_id=delta.source_artifact_id,
    )


class IncrementalBookReconstructor:
    """Stateful O(book-depth) snapshot-plus-delta reconstruction.

    The class retains only the current epoch's book.  Every input delta returns
    an explicit outcome; after a gap or invalidation, later deltas receive an
    ``EXCLUDED_AFTER_TERMINAL`` gap record rather than disappearing silently.
    """

    def __init__(self, snapshot: BookSnapshot) -> None:
        if snapshot.available_ts_ns < snapshot.received_ts_ns:
            raise BookInvariantError("snapshot cannot be available before it was received")
        if (
            not math.isfinite(snapshot.tick_size)
            or not math.isfinite(snapshot.lot_size)
            or snapshot.tick_size <= 0
            or snapshot.lot_size <= 0
        ):
            raise BookInvariantError("snapshot tick and lot sizes must be finite and positive")
        if snapshot.depth_limit < 1 or snapshot.depth_limit > _MAX_BOOK_LEVELS_PER_SIDE:
            raise BookInvariantError(
                f"snapshot depth_limit must be within 1..{_MAX_BOOK_LEVELS_PER_SIDE}"
            )
        bids = _levels_to_book(snapshot.bids, "bid")
        asks = _levels_to_book(snapshot.asks, "ask")
        if max(bids) >= min(asks):
            raise BookInvariantError("snapshot is crossed or locked")
        self.snapshot = snapshot
        self._bids = bids
        self._asks = asks
        self._last_update_id = snapshot.last_update_id
        self._stale_events = 0
        self._status: Literal["LIVE", "GAPPED", "INVALID"] = "LIVE"

    @property
    def status(self) -> Literal["LIVE", "GAPPED", "INVALID"]:
        return self._status

    @property
    def stale_events(self) -> int:
        return self._stale_events

    @property
    def final_update_id(self) -> int:
        return self._last_update_id

    def _validate_identity(self, delta: DepthDelta) -> None:
        if delta.venue != self.snapshot.venue or delta.symbol != self.snapshot.symbol:
            raise BookInvariantError("delta venue/symbol does not match snapshot")
        if delta.tick_size != self.snapshot.tick_size or delta.lot_size != self.snapshot.lot_size:
            raise BookInvariantError("delta tick/lot scales do not match snapshot metadata")

    def update(self, delta: DepthDelta) -> ReconstructionStep:
        """Apply exactly one delta and return its explicit reconstruction disposition."""
        self._validate_identity(delta)
        expected = self._last_update_id + 1
        if self._status != "LIVE":
            return ReconstructionStep(
                outcome="EXCLUDED_AFTER_TERMINAL",
                observation=None,
                gap=_gap(
                    self.snapshot,
                    delta,
                    expected,
                    f"epoch_already_{self._status.lower()}",
                ),
            )
        if delta.continuity_id != self.snapshot.continuity_id:
            self._status = "GAPPED"
            return ReconstructionStep(
                outcome="GAP",
                observation=None,
                gap=_gap(self.snapshot, delta, expected, "continuity_id_mismatch"),
            )
        if delta.last_update_id < delta.first_update_id:
            self._status = "INVALID"
            return ReconstructionStep(
                outcome="INVALID",
                observation=None,
                gap=_gap(self.snapshot, delta, expected, "invalid_sequence_range"),
            )
        if delta.last_update_id <= self._last_update_id:
            self._stale_events += 1
            return ReconstructionStep(outcome="STALE", observation=None, gap=None)
        if (
            delta.previous_update_id is not None
            and delta.previous_update_id != self._last_update_id
        ):
            self._status = "GAPPED"
            return ReconstructionStep(
                outcome="GAP",
                observation=None,
                gap=_gap(self.snapshot, delta, expected, "previous_update_id_mismatch"),
            )
        if delta.first_update_id > expected:
            self._status = "GAPPED"
            return ReconstructionStep(
                outcome="GAP",
                observation=None,
                gap=_gap(self.snapshot, delta, expected, "forward_sequence_gap"),
            )
        if delta.last_update_id < expected:
            self._stale_events += 1
            return ReconstructionStep(outcome="STALE", observation=None, gap=None)

        candidate_bids = self._bids.copy()
        candidate_asks = self._asks.copy()
        try:
            _apply_side(candidate_bids, delta.bids, "bid")
            _apply_side(candidate_asks, delta.asks, "ask")
            if not candidate_bids or not candidate_asks:
                raise BookInvariantError("delta emptied one side of the order book")
            crossed = max(candidate_bids) >= min(candidate_asks)
            observation = _observation(
                snapshot=self.snapshot,
                delta=delta,
                bids=candidate_bids,
                asks=candidate_asks,
                valid=not crossed,
            )
        except _BookCapacityError:
            self._status = "INVALID"
            return ReconstructionStep(
                outcome="INVALID",
                observation=None,
                gap=_gap(self.snapshot, delta, expected, "book_level_limit_exceeded"),
            )
        except BookInvariantError:
            self._status = "INVALID"
            return ReconstructionStep(
                outcome="INVALID",
                observation=None,
                gap=_gap(self.snapshot, delta, expected, "invalid_book_level"),
            )

        self._bids = candidate_bids
        self._asks = candidate_asks
        self._last_update_id = delta.last_update_id
        if crossed:
            self._status = "INVALID"
            return ReconstructionStep(
                outcome="INVALID",
                observation=observation,
                gap=_gap(self.snapshot, delta, expected, "crossed_or_locked_book"),
            )
        return ReconstructionStep(outcome="OBSERVED", observation=observation, gap=None)


def reconstruct_snapshot_and_deltas(
    snapshot: BookSnapshot, deltas: tuple[DepthDelta, ...] | list[DepthDelta]
) -> ReconstructionResult:
    """Apply buffered/live deltas until an explicit gap or invariant failure.

    Stale events (``u <= last_update_id``) are counted and ignored.  A usable
    event must cover the next expected update ID, allowing safe overlap.  A
    forward gap invalidates the continuity epoch; later events are not emitted.
    """
    reconstructor = IncrementalBookReconstructor(snapshot)
    observation_records: list[dict[str, object]] = []
    gaps: list[SequenceGap] = []

    for delta in deltas:
        step = reconstructor.update(delta)
        if step.observation is not None:
            observation_records.append(dict(step.observation))
        if step.gap is not None:
            gaps.append(step.gap)
        if step.outcome in {"GAP", "INVALID"}:
            break

    return ReconstructionResult(
        status=reconstructor.status,
        observations=table_from_records("book_observations", observation_records),
        gaps=table_from_records("sequence_gaps", [item.to_record() for item in gaps]),
        stale_events=reconstructor.stale_events,
        final_update_id=reconstructor.final_update_id,
    )


def snapshots_table(snapshots: list[BookSnapshot] | tuple[BookSnapshot, ...]) -> pa.Table:
    return table_from_records("book_snapshots", [snapshot.to_record() for snapshot in snapshots])


def deltas_table(deltas: list[DepthDelta] | tuple[DepthDelta, ...]) -> pa.Table:
    return table_from_records("depth_deltas", [delta.to_record() for delta in deltas])