# FILE-SIZE-OK: Cohesive reconciliation engine (discover/repair/verify)
"""Kintsugi: Unified gap reconciliation for the ClickHouse cache (Issue #115, #238).

Named after the Japanese art of repairing broken pottery with gold lacquer
(金継ぎ). Interior gaps ("shards") and the trailing edge are discovered and
repaired autonomously, making the dataset stronger than before.

**Stathera-only anomaly detection.**  All anomalies detected via trade-ID
continuity (tid_delta != 0).  No time-based gap detection.  Both gaps
(tid_delta > 0) and overlaps (tid_delta < 0) are discovered and repaired.

**Day-recompute repair.**  When any Stathera anomaly is detected, the entire
Ouroboros day(s) containing it are deleted and reprocessed from scratch.
Vision-first (Binance Vision ZIPs), REST fallback via time-based fetch.
No Ariadne anchor needed — each day is independently processable.
This eliminates both missing-trade gaps and overlap artifacts from
concurrent sidecar + backfill writes.

**Trailing-edge fill (#238 Phase 2).**  After interior shard repairs, fills
the gap between the latest cached bar and now using Vision-first + REST
fallback (backfill_all_gaps).  Absorbed from the former gap-backfill daemon.

Architecture
------------
::

    Stathera audit (ClickHouse)  →  discover_shards()  →  day-recompute → verify
         (gaps + overlaps)              │
                            ┌───────────┘
                            ▼
                 Vision ZIP (if available) → _try_vision_day_replace()
                            │ fallback
                            ▼
                 REST time-based fetch → _process_gap_fallback_starttime()
                            │
    Trailing edge (latest bar → now)  →  backfill_all_gaps()  ◄─────┘

Usage
-----
Single pass::

    >>> from opendeviationbar.kintsugi import kintsugi_pass
    >>> results = kintsugi_pass()

Daemon mode::

    >>> from opendeviationbar.kintsugi import kintsugi_daemon
    >>> kintsugi_daemon()  # Long-running, adaptive polling
"""

from __future__ import annotations

# ruff: noqa: F401 — re-exports for backward compatibility (tests use @patch on these)
# Private symbols imported by tests and internal callers.
from ._helpers import (
    _get_worker_cache,
    _is_transient_error,
    _kintsugi_cfg,
    _thread_local,
)

# Re-export all public symbols so existing imports don't break.
from .daemon import (
    _periodic_rectification,
    _pressure_aware_sleep,
    _trailing_edge_fill,
    kintsugi_daemon,
)
from .dead_letter import replay_dead_letters
from .discovery import (
    KintsugiResult,
    Shard,
    _aletheia_purge,
    _discover_genesis_shards,
    _discover_shards_stathera,
    _enrich_shards,
    _get_dismissed_set,
    _get_rate_limited_set,
    _select_repair_candidates,
    _shard_has_vision_available,
    _shard_is_parquet_cached,
    _shard_sort_key,
    discover_shards,
)
from .orchestrator import kintsugi_pass
from .repair import (
    _notify_repair_failure,
    _repair,
    repair_shard,
)
from .telemetry import (
    _emit_telemetry,
    _log_repair,
    _maybe_log_repair,
    _notify_kintsugi_failures,
    _sd_notify,
)
from .verify import verify_repair

__all__ = [
    "KintsugiResult",
    "Shard",
    "discover_shards",
    "kintsugi_daemon",
    "kintsugi_pass",
    "repair_shard",
    "replay_dead_letters",
    "verify_repair",
]
