Source code for imednet_workflows.cached_loader

"""Workflow for loading study records with local SQLite caching for performance."""

from __future__ import annotations

import contextvars
import json
import sqlite3
import threading
import weakref
from collections.abc import Callable, Iterable, Iterator
from contextlib import contextmanager, suppress
from dataclasses import dataclass
from datetime import UTC, datetime
from enum import StrEnum
from pathlib import Path
from typing import TYPE_CHECKING, Any, TypeVar, cast
from unittest.mock import NonCallableMock

from filelock import FileLock
from pydantic import ValidationError as PydanticValidationError
from tenacity import Retrying, retry_if_exception, stop_after_attempt, wait_exponential

from imednet.spi.errors import (
    ApiError,
    AuthenticationError,
    AuthorizationError,
    BadRequestError,
    ClientError,
    ConflictError,
    NotFoundError,
    RateLimitError,
    ServerError,
    ValidationError,
)
from imednet.spi.models import Record
from imednet.spi.sink_base import is_record_deleted
from imednet.spi.utils import (
    build_filter_string,
    delegates_to_sdk_retry_policy,
    get_db_init_lock,
    redact_sensitive_text,
    resolve_sdk_base_url,
    validate_path_segment,
)
from imednet.spi.utils import get_sqlite_connection as _spi_get_sqlite_connection

from ._filter_bounds import build_delta_watermark_filter, plan_missing_record_id_bound
from .chunked_pipeline import DEFAULT_CHUNK_SIZE

if TYPE_CHECKING:
    from imednet.spi.facade import ImednetFacade

DEFAULT_CACHE_DIR = Path.home() / ".imednet" / "cache"
_EPOCH_FLOOR_WATERMARK = "1970-01-01T00:00:00Z"

_T = TypeVar("_T")


def _close_sqlite_quietly(conn: sqlite3.Connection | None) -> None:
    """Close a SQLite connection handle without raising teardown errors."""
    if conn is None:
        return
    with suppress(sqlite3.Error):
        conn.close()


[docs]class StudySyncStatus(StrEnum): """Lifecycle status for per-study SQLite cache synchronization.""" IDLE = "idle" SYNCING = "syncing" SYNCED = "synced" FAILED = "failed"
[docs]@dataclass(frozen=True, slots=True) class StudySyncState: """Snapshot of synchronization epoch and watermark metadata for a study.""" study_key: str sync_epoch: int = 0 high_water_mark: str | None = None initial_sync_complete: bool = False status: StudySyncStatus = StudySyncStatus.IDLE last_synced_at: str | None = None error_message: str | None = None
@dataclass(frozen=True, slots=True) class _LocalRecordMeta: """Lightweight metadata snapshot of a locally cached Record.""" date_modified: str | None form_key: str record_status: str last_updated: str | None def _serialize_date_modified(value: Any) -> str | None: """Serialize a record modification timestamp, storing missing values as SQL NULL.""" if value is None: return None serialized = value.isoformat() if hasattr(value, "isoformat") else str(value) return serialized if serialized.strip() else None def _normalize_status_token(value: Any) -> str: """Extract normalized string representation for a Record status field.""" if value is None: return "" raw = value.value if hasattr(value, "value") else value return str(raw).strip() def _is_projected_record_updated(projected: Record, local_meta: _LocalRecordMeta) -> bool: """Return True when upstream lightweight projection metadata supersedes cached metadata.""" proj_dm = _serialize_date_modified(projected.date_modified) if proj_dm is not None and proj_dm != local_meta.date_modified: return True proj_status = _normalize_status_token(projected.record_status) if proj_status and proj_status != local_meta.record_status: return True proj_form_key = (projected.form_key or "").strip() if proj_form_key and proj_form_key != local_meta.form_key: return True proj_last_updated = _serialize_date_modified(getattr(projected, "last_updated", None)) return bool(proj_last_updated is not None and proj_last_updated != local_meta.last_updated) def _is_retryable_loader_exception(exc: BaseException) -> bool: """Return True only for transient operational errors; never retry 4xx or validation errors.""" if not isinstance(exc, Exception): return False if isinstance( exc, ( ClientError, PydanticValidationError, ValueError, TypeError, KeyError, AttributeError, ), ): return False if isinstance( exc, ( BadRequestError, ValidationError, AuthenticationError, AuthorizationError, NotFoundError, ConflictError, ), ): return False if isinstance(exc, (RateLimitError, ServerError)): return True if isinstance(exc, ApiError): status = exc.status_code return status == 429 or (status is not None and 500 <= status < 600) return True def _timestamp_sort_key(timestamp: str) -> tuple[int, Any]: """Return a chronological sort key for ISO-8601 timestamps with string fallback.""" normalized = timestamp.strip().replace("Z", "+00:00") try: parsed = datetime.fromisoformat(normalized) utc_dt = parsed.replace(tzinfo=UTC) if parsed.tzinfo is None else parsed.astimezone(UTC) except ValueError: return (0, timestamp) return (1, utc_dt) def _max_timestamp(candidates: Iterable[str | None]) -> str | None: """Return the chronologically latest non-empty timestamp string while preserving formatting.""" valid = [ts for ts in candidates if ts is not None and ts.strip()] if not valid: return None return max(valid, key=_timestamp_sort_key) _active_lock_timeout_seconds: contextvars.ContextVar[float | None] = contextvars.ContextVar( "active_loader_lock_timeout_seconds", default=None, )
[docs]@contextmanager def bind_loader_lock_timeout(timeout_seconds: float | None) -> Iterator[None]: """Bind a per-cycle ``FileLock`` acquisition timeout without mutating ``loader.lock.timeout``.""" token = _active_lock_timeout_seconds.set(timeout_seconds) try: yield finally: _active_lock_timeout_seconds.reset(token)
[docs]def get_sqlite_connection(db_path: str | Path) -> sqlite3.Connection: """Return a SQLite connection configured for concurrent WAL cache access via SPI.""" return _spi_get_sqlite_connection(db_path)
[docs]class CachedRecordsLoader: """Load study records through a local SQLite cache with origin-scoped incremental sync."""
[docs] def __init__( self, sdk: ImednetFacade, *, cache_dir: str | Path | None = None, database_name: str = "records_cache.sqlite3", retry_attempts: int = 3, lock_timeout_seconds: int = 30, lock: FileLock | None = None, ) -> None: """Initialize the cached records loader. Args: sdk: An instance of the iMednet SDK facade. cache_dir: Directory to store the SQLite cache. Defaults to ~/.imednet/cache. database_name: Name of the SQLite database file. retry_attempts: Number of API retry attempts for facade implementations without SDK retry policy. lock_timeout_seconds: Number of seconds to wait when acquiring the cache file lock. lock: Optional FileLock instance for synchronizing write transactions. """ self._sdk = sdk self._base_url = resolve_sdk_base_url(sdk) self._tx_state = threading.local() base_dir = DEFAULT_CACHE_DIR if cache_dir is None else Path(cache_dir).expanduser() self.db_path = base_dir / database_name self._retry_attempts = retry_attempts lock_path = Path(f"{self.db_path}.lock") self._lock = ( lock if lock is not None else FileLock(str(lock_path), timeout=lock_timeout_seconds) ) self._anchor_lock = threading.Lock() self._wal_anchor_ino: int | None = None self._wal_anchor_finalizer: weakref.finalize[..., Any] | None = None self._initialise_cache() self._ensure_wal_anchor()
[docs] def close(self) -> None: """Close the loader's background WAL anchor connection if open.""" with self._anchor_lock: self._release_wal_anchor_unlocked()
def __enter__(self) -> CachedRecordsLoader: """Enter runtime context and return ``self``.""" return self def __exit__(self, *_exc_info: object) -> None: """Close the loader's background WAL anchor connection on context exit.""" self.close() def _release_wal_anchor_unlocked(self) -> None: """Detach and close any held WAL anchor connection under ``self._anchor_lock``.""" finalizer = self._wal_anchor_finalizer self._wal_anchor_finalizer = None self._wal_anchor_ino = None if finalizer is not None and finalizer.alive: finalizer() def _ensure_wal_anchor(self) -> None: """Keep an idle connection open so transient closes avoid WAL exclusive-unlink stalls.""" with self._anchor_lock: try: current_ino = self.db_path.stat().st_ino except OSError: current_ino = None if ( self._wal_anchor_finalizer is not None and self._wal_anchor_finalizer.alive and current_ino is not None and self._wal_anchor_ino == current_ino ): return self._release_wal_anchor_unlocked() if current_ino is None: return anchor: sqlite3.Connection | None = None try: anchor = _spi_get_sqlite_connection(self.db_path, check_same_thread=False) cursor = anchor.execute("SELECT 1 FROM sqlite_schema LIMIT 1") cursor.close() except sqlite3.Error: _close_sqlite_quietly(anchor) return self._wal_anchor_ino = current_ino self._wal_anchor_finalizer = weakref.finalize( self, _close_sqlite_quietly, anchor, ) @property def base_url(self) -> str: """Return the normalized SDK origin URL partitioning this loader's cache state.""" return self._base_url @property def lock(self) -> FileLock: """Return the inter-process file lock protecting cache write operations.""" return self._lock @contextmanager def _acquire_write_lock(self, timeout_seconds: float | None = None) -> Iterator[None]: """Acquire the cache FileLock with an optional timeout without mutating ``self._lock.timeout``.""" effective_timeout = ( timeout_seconds if timeout_seconds is not None else _active_lock_timeout_seconds.get() ) if effective_timeout is None: with self._lock: yield return self._lock.acquire(timeout=effective_timeout) try: yield finally: self._lock.release()
[docs] def load_records( self, study_key: str, *, reconcile: bool = True, lock_timeout_seconds: float | None = None, ) -> list[Record]: """Synchronise the cache for ``study_key`` and return cached records.""" self.sync_records( study_key, reconcile=reconcile, lock_timeout_seconds=lock_timeout_seconds, ) return self.get_cached_records(study_key)
[docs] def sync_records( self, study_key: str, *, reconcile: bool = True, lock_timeout_seconds: float | None = None, ) -> None: """Synchronise the cache for ``study_key`` in a single atomic commit after network I/O.""" self._ensure_wal_anchor() conn = get_sqlite_connection(self.db_path) try: sync_state = self.get_study_sync_state(study_key, conn=conn) cycle_timestamps: list[str | None] = [sync_state.high_water_mark] try: delta_records: list[Record] = [] if not sync_state.initial_sync_complete and sync_state.high_water_mark is None: delta_records = self._fetch_delta_records(study_key, None) elif sync_state.high_water_mark is not None: delta_records = self._fetch_delta_records(study_key, sync_state.high_water_mark) elif not reconcile: delta_records = self._fetch_delta_records(study_key, _EPOCH_FLOOR_WATERMARK) active_delta_records: list[Record] = [] voided_delta_ids: set[int] = set() for record in delta_records: cycle_timestamps.append(_serialize_date_modified(record.date_modified)) if is_record_deleted(record): if record.record_id is not None: voided_delta_ids.add(record.record_id) else: active_delta_records.append(record) pre_reconcile_epoch: int | None = None active_record_ids: set[int] = set() backfill_records: list[Record] = [] run_reconcile_commit = False if reconcile: pre_reconcile_epoch = self._get_sync_epoch(conn, study_key) skip_full_projection = False if ( sync_state.initial_sync_complete and not delta_records and self._count_local_untimestamped_drafts(conn, study_key) == 0 ): local_active_count = self.count_cached_records(study_key, conn=conn) remote_active_count = self._probe_remote_active_record_count(study_key) if remote_active_count is not None: if remote_active_count == local_active_count: skip_full_projection = True elif remote_active_count == 0: active_record_ids = set() run_reconcile_commit = True skip_full_projection = True if not skip_full_projection: run_reconcile_commit = True projected_records = self._fetch_active_records_projection(study_key) active_record_ids = { record.record_id for record in projected_records if record.record_id is not None and not is_record_deleted(record) } local_meta = self._get_local_record_metadata(conn, study_key) delta_active_ids = { record.record_id for record in active_delta_records if record.record_id is not None } missing_ids = active_record_ids - set(local_meta.keys()) - delta_active_ids stale_ids = { proj.record_id for proj in projected_records if proj.record_id is not None and not is_record_deleted(proj) and proj.record_id in local_meta and proj.record_id not in delta_active_ids and _is_projected_record_updated(proj, local_meta[proj.record_id]) } ids_to_fetch = missing_ids | stale_ids if ids_to_fetch: fetched_missing = self._fetch_missing_records(study_key, ids_to_fetch) for record in fetched_missing: cycle_timestamps.append( _serialize_date_modified(record.date_modified) ) if is_record_deleted(record): if record.record_id is not None: voided_delta_ids.add(record.record_id) else: backfill_records.append(record) prepared_delta = ( self._prepare_upsert_rows( active_delta_records, default_study_key=study_key, ) if active_delta_records else [] ) prepared_backfill = ( self._prepare_upsert_rows( backfill_records, default_study_key=study_key, ) if backfill_records else [] ) candidate_hwm = _max_timestamp(cycle_timestamps) completed_at_iso = datetime.now(UTC).isoformat() with self._acquire_write_lock(lock_timeout_seconds), conn: self._tx_state.in_atomic_commit = True try: if prepared_delta: self._upsert_prepared_rows_unlocked(conn, prepared_delta) if voided_delta_ids: self._delete_record_ids_unlocked(conn, study_key, voided_delta_ids) if prepared_backfill: self._upsert_prepared_rows_unlocked(conn, prepared_backfill) if run_reconcile_commit: self.reconcile_cache( conn, study_key, active_record_ids, max_epoch=pre_reconcile_epoch, ) self._complete_study_sync_unlocked( conn, study_key, (candidate_hwm,) if candidate_hwm is not None else (), now_iso=completed_at_iso, ) finally: self._tx_state.in_atomic_commit = False except Exception as exc: self._fail_study_sync( conn, study_key, exc, lock_timeout_seconds=lock_timeout_seconds, ) raise finally: conn.close()
[docs] def get_study_sync_state( self, study_key: str, *, conn: sqlite3.Connection | None = None ) -> StudySyncState: """Return the stored sync epoch, watermark, and status metadata for ``study_key``.""" close_conn = False if conn is None: conn = get_sqlite_connection(self.db_path) close_conn = True try: row = conn.execute( """ SELECT study_key, sync_epoch, high_water_mark, initial_sync_complete, status, last_synced_at, error_message FROM study_sync_state WHERE base_url = ? AND study_key = ? """, (self._base_url, study_key), ).fetchone() if row is None: return StudySyncState(study_key=study_key) raw_status = cast(str | None, row["status"]) or StudySyncStatus.IDLE.value try: parsed_status = StudySyncStatus(raw_status) except ValueError: parsed_status = StudySyncStatus.IDLE return StudySyncState( study_key=cast(str, row["study_key"]), sync_epoch=int(row["sync_epoch"]), high_water_mark=cast(str | None, row["high_water_mark"]), initial_sync_complete=bool(row["initial_sync_complete"]), status=parsed_status, last_synced_at=cast(str | None, row["last_synced_at"]), error_message=cast(str | None, row["error_message"]), ) finally: if close_conn: conn.close()
[docs] def count_cached_records( self, study_key: str, *, conn: sqlite3.Connection | None = None ) -> int: """Return the count of cached records for ``study_key`` without materialising rows.""" close_conn = False if conn is None: conn = get_sqlite_connection(self.db_path) close_conn = True try: row = conn.execute( """ SELECT COUNT(*) FROM record_cache WHERE base_url = ? AND study_key = ? """, (self._base_url, study_key), ).fetchone() if row is None: return 0 return int(row[0]) finally: if close_conn: conn.close()
[docs] def get_cached_records( self, study_key: str, *, conn: sqlite3.Connection | None = None ) -> list[Record]: """Return cached records for ``study_key`` without contacting the API.""" return list(self.iter_cached_records(study_key, conn=conn))
[docs] def iter_cached_records( self, study_key: str, *, conn: sqlite3.Connection | None = None, chunk_size: int = DEFAULT_CHUNK_SIZE, order_by_subject: bool = False, ) -> Iterator[Record]: """Yield cached records for ``study_key`` in bounded chunks.""" if chunk_size <= 0: raise ValueError("chunk_size must be greater than zero") close_conn = False if conn is None: conn = get_sqlite_connection(self.db_path) close_conn = True query = ( """ SELECT payload FROM record_cache WHERE base_url = ? AND study_key = ? ORDER BY COALESCE( NULLIF(TRIM(CAST(json_extract(payload, '$.recordData.USUBJID') AS TEXT)), ''), NULLIF(TRIM(CAST(json_extract(payload, '$.recordData.usubjid') AS TEXT)), ''), NULLIF(TRIM(CAST(json_extract(payload, '$.subjectKey') AS TEXT)), ''), NULLIF(TRIM(CAST(json_extract(payload, '$.subject_key') AS TEXT)), ''), '' ) ASC, record_id ASC """ if order_by_subject else """ SELECT payload FROM record_cache WHERE base_url = ? AND study_key = ? ORDER BY record_id """ ) try: cursor = conn.execute(query, (self._base_url, study_key)) while True: rows = cursor.fetchmany(chunk_size) if not rows: break for row in rows: record = Record.from_json(json.loads(cast(str, row["payload"]))) if not is_record_deleted(record): yield record finally: if close_conn: conn.close()
[docs] def reconcile_cache( self, conn: sqlite3.Connection, study_key: str, active_record_ids: set[int], *, max_epoch: int | None = None, ) -> None: """Prune records removed from the upstream EDC backend up to ``max_epoch``.""" if getattr(self._tx_state, "in_atomic_commit", False): self._reconcile_cache_unlocked( conn, study_key, active_record_ids, max_epoch=max_epoch, ) return with self._acquire_write_lock(), conn: self._reconcile_cache_unlocked( conn, study_key, active_record_ids, max_epoch=max_epoch, )
def _reconcile_cache_unlocked( self, conn: sqlite3.Connection, study_key: str, active_record_ids: set[int], *, max_epoch: int | None = None, ) -> None: """Execute epoch-fenced orphan deletion inside an active lock-held transaction.""" effective_epoch = ( max_epoch if max_epoch is not None else self._get_sync_epoch(conn, study_key) ) if not active_record_ids: conn.execute( """ DELETE FROM record_cache WHERE base_url = ? AND study_key = ? AND upserted_epoch <= ? """, (self._base_url, study_key, effective_epoch), ) return conn.execute( """ DELETE FROM record_cache WHERE base_url = ? AND study_key = ? AND upserted_epoch <= ? AND record_id NOT IN (SELECT CAST(value AS INTEGER) FROM json_each(?)) """, ( self._base_url, study_key, effective_epoch, json.dumps(sorted(active_record_ids)), ), ) def _delete_record_ids_unlocked( self, conn: sqlite3.Connection, study_key: str, record_ids: set[int], ) -> None: """Delete voided record IDs from ``record_cache`` inside an active transaction.""" if not record_ids: return conn.execute( """ DELETE FROM record_cache WHERE base_url = ? AND study_key = ? AND record_id IN (SELECT CAST(value AS INTEGER) FROM json_each(?)) """, (self._base_url, study_key, json.dumps(sorted(record_ids))), ) def _initialise_cache(self) -> None: """Ensure the cache database and origin-partitioned tables are created and migrated.""" with get_db_init_lock(self.db_path): conn = get_sqlite_connection(self.db_path) try: with self._acquire_write_lock(), conn: self._migrate_or_create_record_cache(conn) self._migrate_or_create_study_sync_state(conn) self._backfill_legacy_study_sync_state(conn) finally: conn.close() def _migrate_or_create_record_cache(self, conn: sqlite3.Connection) -> None: """Create ``record_cache`` or migrate legacy schemas in place without data loss.""" table_info = conn.execute("PRAGMA table_info(record_cache)").fetchall() if not table_info: conn.execute( """ CREATE TABLE IF NOT EXISTS record_cache ( base_url TEXT NOT NULL, study_key TEXT NOT NULL, record_id INTEGER NOT NULL, form_key TEXT NOT NULL, date_modified TEXT, payload TEXT NOT NULL, upserted_epoch INTEGER NOT NULL DEFAULT 0, PRIMARY KEY (base_url, study_key, record_id) ) """ ) else: columns = {cast(str, row["name"]): row for row in table_info} pk_cols = [ cast(str, row["name"]) for row in sorted(table_info, key=lambda r: int(r["pk"])) if int(row["pk"]) > 0 ] date_mod_col = columns.get("date_modified") needs_rebuild = ( "base_url" not in columns or "upserted_epoch" not in columns or pk_cols != ["base_url", "study_key", "record_id"] or (date_mod_col is not None and int(date_mod_col["notnull"]) != 0) ) if needs_rebuild: conn.execute("DROP TABLE IF EXISTS record_cache_migrated") conn.execute( """ CREATE TABLE record_cache_migrated ( base_url TEXT NOT NULL, study_key TEXT NOT NULL, record_id INTEGER NOT NULL, form_key TEXT NOT NULL, date_modified TEXT, payload TEXT NOT NULL, upserted_epoch INTEGER NOT NULL DEFAULT 0, PRIMARY KEY (base_url, study_key, record_id) ) """ ) if "base_url" in columns and "upserted_epoch" in columns: conn.execute( """ INSERT INTO record_cache_migrated ( base_url, study_key, record_id, form_key, date_modified, payload, upserted_epoch ) SELECT COALESCE(NULLIF(base_url, ''), ?), study_key, record_id, form_key, NULLIF(date_modified, ''), payload, COALESCE(upserted_epoch, 1) FROM record_cache """, (self._base_url,), ) elif "upserted_epoch" in columns: conn.execute( """ INSERT INTO record_cache_migrated ( base_url, study_key, record_id, form_key, date_modified, payload, upserted_epoch ) SELECT ?, study_key, record_id, form_key, NULLIF(date_modified, ''), payload, COALESCE(upserted_epoch, 1) FROM record_cache """, (self._base_url,), ) else: conn.execute( """ INSERT INTO record_cache_migrated ( base_url, study_key, record_id, form_key, date_modified, payload, upserted_epoch ) SELECT ?, study_key, record_id, form_key, NULLIF(date_modified, ''), payload, 1 FROM record_cache """, (self._base_url,), ) conn.execute("DROP TABLE record_cache") conn.execute("ALTER TABLE record_cache_migrated RENAME TO record_cache") conn.execute( """ CREATE INDEX IF NOT EXISTS idx_record_cache_origin_study_modified ON record_cache (base_url, study_key, date_modified) """ ) conn.execute( """ CREATE INDEX IF NOT EXISTS idx_record_cache_origin_study_epoch ON record_cache (base_url, study_key, upserted_epoch) """ ) def _migrate_or_create_study_sync_state(self, conn: sqlite3.Connection) -> None: """Create ``study_sync_state`` or migrate legacy unpartitioned rows in place.""" sync_info = conn.execute("PRAGMA table_info(study_sync_state)").fetchall() if not sync_info: conn.execute( """ CREATE TABLE IF NOT EXISTS study_sync_state ( base_url TEXT NOT NULL, study_key TEXT NOT NULL, sync_epoch INTEGER NOT NULL DEFAULT 0, high_water_mark TEXT, initial_sync_complete INTEGER NOT NULL DEFAULT 0, status TEXT NOT NULL DEFAULT 'idle', last_synced_at TEXT, error_message TEXT, PRIMARY KEY (base_url, study_key) ) """ ) return sync_cols = {cast(str, row["name"]): row for row in sync_info} sync_pk = [ cast(str, row["name"]) for row in sorted(sync_info, key=lambda r: int(r["pk"])) if int(row["pk"]) > 0 ] if "base_url" in sync_cols and sync_pk == ["base_url", "study_key"]: return conn.execute("DROP TABLE IF EXISTS study_sync_state_migrated") conn.execute( """ CREATE TABLE study_sync_state_migrated ( base_url TEXT NOT NULL, study_key TEXT NOT NULL, sync_epoch INTEGER NOT NULL DEFAULT 0, high_water_mark TEXT, initial_sync_complete INTEGER NOT NULL DEFAULT 0, status TEXT NOT NULL DEFAULT 'idle', last_synced_at TEXT, error_message TEXT, PRIMARY KEY (base_url, study_key) ) """ ) if "base_url" in sync_cols: conn.execute( """ INSERT INTO study_sync_state_migrated ( base_url, study_key, sync_epoch, high_water_mark, initial_sync_complete, status, last_synced_at, error_message ) SELECT COALESCE(NULLIF(base_url, ''), ?), study_key, COALESCE(sync_epoch, 0), NULLIF(high_water_mark, ''), COALESCE(initial_sync_complete, 0), COALESCE(status, 'idle'), last_synced_at, error_message FROM study_sync_state """, (self._base_url,), ) else: conn.execute( """ INSERT INTO study_sync_state_migrated ( base_url, study_key, sync_epoch, high_water_mark, initial_sync_complete, status, last_synced_at, error_message ) SELECT ?, study_key, COALESCE(sync_epoch, 0), NULLIF(high_water_mark, ''), COALESCE(initial_sync_complete, 0), COALESCE(status, 'idle'), last_synced_at, error_message FROM study_sync_state """, (self._base_url,), ) conn.execute("DROP TABLE study_sync_state") conn.execute("ALTER TABLE study_sync_state_migrated RENAME TO study_sync_state") def _backfill_legacy_study_sync_state(self, conn: sqlite3.Connection) -> None: """Synthesize missing ``study_sync_state`` rows from migrated ``record_cache`` rows.""" legacy_studies = conn.execute( """ SELECT DISTINCT r.base_url, r.study_key FROM record_cache AS r LEFT JOIN study_sync_state AS s ON s.base_url = r.base_url AND s.study_key = r.study_key WHERE s.study_key IS NULL """ ).fetchall() for study_row in legacy_studies: legacy_base_url = cast(str, study_row["base_url"]) legacy_study_key = cast(str, study_row["study_key"]) epoch_row = conn.execute( """ SELECT COALESCE(MAX(upserted_epoch), 1) FROM record_cache WHERE base_url = ? AND study_key = ? """, (legacy_base_url, legacy_study_key), ).fetchone() max_epoch = int(epoch_row[0]) if epoch_row is not None else 1 ts_rows = conn.execute( """ SELECT date_modified FROM record_cache WHERE base_url = ? AND study_key = ? AND date_modified IS NOT NULL """, (legacy_base_url, legacy_study_key), ).fetchall() hwm = _max_timestamp(cast(str | None, ts_row["date_modified"]) for ts_row in ts_rows) conn.execute( """ INSERT OR IGNORE INTO study_sync_state ( base_url, study_key, sync_epoch, high_water_mark, initial_sync_complete, status ) VALUES (?, ?, ?, ?, 1, ?) """, (legacy_base_url, legacy_study_key, max_epoch, hwm, StudySyncStatus.SYNCED.value), ) def _count_local_untimestamped_drafts(self, conn: sqlite3.Connection, study_key: str) -> int: """Return the number of cached records for ``study_key`` lacking ``date_modified``.""" row = conn.execute( """ SELECT COUNT(*) FROM record_cache WHERE base_url = ? AND study_key = ? AND date_modified IS NULL """, (self._base_url, study_key), ).fetchone() if row is None: return 0 return int(row[0]) def _probe_remote_active_record_count(self, study_key: str) -> int | None: """Execute a single-page lightweight count preflight when backed by an HTTP client.""" if isinstance(self._sdk, NonCallableMock): return None records_ep = getattr(self._sdk, "records", None) if isinstance(records_ep, NonCallableMock): return None client = ( getattr(records_ep, "_client", None) if records_ep is not None else getattr(self._sdk, "_client", None) ) if client is None: client = getattr(self._sdk, "_client", None) if client is None or isinstance(client, NonCallableMock): return None get_fn = getattr(client, "get", None) if not callable(get_fn) or isinstance(get_fn, NonCallableMock): return None safe_study_key = validate_path_segment(study_key, name="study_key") path = f"/api/v1/edc/studies/{safe_study_key}/records" params = { "filter": build_filter_string({"deleted": False}), "recordDataFilter": "none", "page": 0, "size": 1, } response = self._execute_with_retry_ownership(get_fn, path, params=params) json_fn = getattr(response, "json", None) if not callable(json_fn): return None payload = json_fn() if not isinstance(payload, dict): return None pagination = payload.get("pagination") if not isinstance(pagination, dict): return None total = pagination.get("totalElements") if isinstance(total, int) and not isinstance(total, bool) and total >= 0: return total return None def _get_sync_epoch(self, conn: sqlite3.Connection, study_key: str) -> int: """Return the current monotonic sync epoch for ``study_key``.""" row = conn.execute( "SELECT sync_epoch FROM study_sync_state WHERE base_url = ? AND study_key = ?", (self._base_url, study_key), ).fetchone() if row is None: return 0 return int(row["sync_epoch"]) def _get_local_record_metadata( self, conn: sqlite3.Connection, study_key: str ) -> dict[int, _LocalRecordMeta]: """Return lightweight cached metadata indexed by ``record_id`` for ``study_key``.""" rows = conn.execute( """ SELECT record_id, date_modified, form_key, COALESCE( json_extract(payload, '$.recordStatus'), json_extract(payload, '$.record_status') ) AS record_status, COALESCE( json_extract(payload, '$.lastUpdated'), json_extract(payload, '$.last_updated') ) AS last_updated FROM record_cache WHERE base_url = ? AND study_key = ? """, (self._base_url, study_key), ).fetchall() metadata: dict[int, _LocalRecordMeta] = {} for row in rows: rec_id = int(row["record_id"]) metadata[rec_id] = _LocalRecordMeta( date_modified=_serialize_date_modified(row["date_modified"]), form_key=str(row["form_key"] or "").strip(), record_status=_normalize_status_token(row["record_status"]), last_updated=_serialize_date_modified(row["last_updated"]), ) return metadata def _complete_study_sync_unlocked( self, conn: sqlite3.Connection, study_key: str, candidate_timestamps: Iterable[str | None], *, now_iso: str | None = None, ) -> None: """Advance ``high_water_mark`` monotonically inside an active lock-held transaction.""" effective_now_iso = now_iso if now_iso is not None else datetime.now(UTC).isoformat() candidates = list(candidate_timestamps) current_hwm = self._get_high_water_mark(conn, study_key) next_hwm = _max_timestamp([current_hwm, *candidates]) conn.execute( """ INSERT INTO study_sync_state ( base_url, study_key, sync_epoch, high_water_mark, initial_sync_complete, status, last_synced_at, error_message ) VALUES (?, ?, 0, ?, 1, ?, ?, NULL) ON CONFLICT(base_url, study_key) DO UPDATE SET high_water_mark = excluded.high_water_mark, initial_sync_complete = 1, status = excluded.status, last_synced_at = excluded.last_synced_at, error_message = NULL """, ( self._base_url, study_key, next_hwm, StudySyncStatus.SYNCED.value, effective_now_iso, ), ) def _fail_study_sync( self, conn: sqlite3.Connection, study_key: str, exc: Exception, *, lock_timeout_seconds: float | None = None, ) -> None: """Record sync failure metadata while preserving the existing ``high_water_mark``.""" safe_error = redact_sensitive_text(str(exc)) or type(exc).__name__ with self._acquire_write_lock(lock_timeout_seconds), conn: conn.execute( """ INSERT INTO study_sync_state (base_url, study_key, sync_epoch, status, error_message) VALUES (?, ?, 0, ?, ?) ON CONFLICT(base_url, study_key) DO UPDATE SET status = excluded.status, error_message = excluded.error_message """, (self._base_url, study_key, StudySyncStatus.FAILED.value, safe_error), ) def _get_high_water_mark(self, conn: sqlite3.Connection, study_key: str) -> str | None: """Get the stored high-water mark timestamp from ``study_sync_state``.""" row = conn.execute( "SELECT high_water_mark FROM study_sync_state WHERE base_url = ? AND study_key = ?", (self._base_url, study_key), ).fetchone() if row is None: return None value = cast(str | None, row["high_water_mark"]) return value if value is not None and value.strip() else None def _fetch_delta_records(self, study_key: str, high_water_mark: str | None) -> list[Record]: """Fetch records from the API that have been modified since the high water mark.""" if not high_water_mark or not high_water_mark.strip(): records = self._list_records( study_key=study_key, record_data_filter=None, deleted=False, ) return [record for record in records if not is_record_deleted(record)] # Use >= to avoid missing updates that share the high-water-mark timestamp. # _upsert_records_unlocked keeps refresh idempotent by deduplicating on (base_url, study_key, record_id). delta_filter = build_delta_watermark_filter(high_water_mark) return self._list_records_with_filter_override( study_key=study_key, filter_string=delta_filter, ) def _fetch_active_records_projection(self, study_key: str) -> list[Record]: """Fetch lightweight non-deleted Record projections for reconciliation and draft checks.""" records = self._list_records(study_key=study_key, record_data_filter="none", deleted=False) return [record for record in records if not is_record_deleted(record)] def _fetch_missing_records(self, study_key: str, missing_ids: set[int]) -> list[Record]: """Fetch full record payloads for upstream active IDs missing or stale in local cache.""" if not missing_ids: return [] records = self._list_records( study_key=study_key, record_id=plan_missing_record_id_bound(missing_ids), record_data_filter=None, ) return [record for record in records if record.record_id in missing_ids] def _execute_with_retry_ownership( self, target: Callable[..., _T], *args: Any, **kwargs: Any, ) -> _T: """Invoke *target*, delegating transient retries when backed by SDK DefaultRetryPolicy.""" if delegates_to_sdk_retry_policy(self._sdk, target): return target(*args, **kwargs) retryer = Retrying( stop=stop_after_attempt(max(1, self._retry_attempts)), wait=wait_exponential(multiplier=1, min=1, max=8), retry=retry_if_exception(_is_retryable_loader_exception), reraise=True, ) return retryer(target, *args, **kwargs) def _list_records(self, **filters: Any) -> list[Record]: """Call the SDK records list endpoint honoring retry ownership and permanent 4xx rules.""" return self._execute_with_retry_ownership(self._sdk.get_records, **filters) def _list_records_with_filter_override( self, *, study_key: str, filter_string: str ) -> list[Record]: """List records using an explicit raw ``filter`` query parameter. This bypasses automatic filter construction so incremental sync can send only the timestamp predicate (without ``studyKey``). """ endpoint = getattr(self._sdk, "records") # noqa: B009 list_fn = endpoint.list def _fetch_delta() -> list[Record]: return list( list_fn( study_key=study_key, filter=filter_string, record_data_filter=None, ) ) if delegates_to_sdk_retry_policy(self._sdk, list_fn): return _fetch_delta() return self._execute_with_retry_ownership(_fetch_delta) @staticmethod def _prepare_upsert_rows( records: Iterable[Record], *, default_study_key: str | None = None, ) -> list[tuple[str, int, str, str | None, str]]: """Serialize active records into SQLite row tuples outside any write lock.""" prepared: list[tuple[str, int, str, str | None, str]] = [] for record in records: if is_record_deleted(record): continue resolved_study_key = record.study_key or default_study_key if resolved_study_key is None or record.record_id is None: continue prepared.append( ( cast(str, resolved_study_key), cast(int, record.record_id), cast(str, record.form_key or ""), _serialize_date_modified(record.date_modified), json.dumps(record.model_dump(mode="json", by_alias=True), sort_keys=True), ) ) return prepared def _upsert_records_unlocked( self, conn: sqlite3.Connection, records: Iterable[Record], *, default_study_key: str | None = None, ) -> None: """Insert or update active records inside an active lock-held transaction.""" prepared = self._prepare_upsert_rows(records, default_study_key=default_study_key) self._upsert_prepared_rows_unlocked(conn, prepared) def _upsert_prepared_rows_unlocked( self, conn: sqlite3.Connection, prepared: list[tuple[str, int, str, str | None, str]], ) -> None: """Insert or update pre-serialized active records inside an active lock-held transaction.""" if not prepared: return distinct_studies = list(dict.fromkeys(item[0] for item in prepared)) epochs_by_study: dict[str, int] = {} for study_key in distinct_studies: epoch_row = conn.execute( """ INSERT INTO study_sync_state (base_url, study_key, sync_epoch, status) VALUES (?, ?, 1, ?) ON CONFLICT(base_url, study_key) DO UPDATE SET sync_epoch = study_sync_state.sync_epoch + 1 RETURNING sync_epoch """, (self._base_url, study_key, StudySyncStatus.IDLE.value), ).fetchone() epochs_by_study[study_key] = ( int(epoch_row[0]) if epoch_row is not None else self._get_sync_epoch(conn, study_key) ) payloads = [ ( self._base_url, study_key, record_id, form_key, date_modified, payload, epochs_by_study[study_key], ) for study_key, record_id, form_key, date_modified, payload in prepared ] conn.executemany( """ INSERT INTO record_cache ( base_url, study_key, record_id, form_key, date_modified, payload, upserted_epoch ) VALUES (?, ?, ?, ?, ?, ?, ?) ON CONFLICT(base_url, study_key, record_id) DO UPDATE SET form_key = excluded.form_key, date_modified = excluded.date_modified, payload = excluded.payload, upserted_epoch = excluded.upserted_epoch """, payloads, )