"""Immutable shared snapshot service for one cloud subscription.""" from __future__ import annotations from datetime import datetime from sqlalchemy.ext.asyncio import AsyncSession from app.intelligence.cloud_scope_facts import ( ScopeAnomalyIndex, collect_cloud_scope_facts, ) from app.intelligence.cloud_scope_scoring import CLOUD_SCOPE_SCORE_VERSION from app.intelligence.provider_registry import BUSINESS_AUDIENCE_KEY from app.intelligence.snapshot_service import ( SnapshotBuildResult, SnapshotSubject, persist_compiled_snapshot, ) async def build_cloud_scope_snapshot( db: AsyncSession, scope_id: str, *, now: datetime | None = None, anomaly_index: ScopeAnomalyIndex | None = None, ) -> SnapshotBuildResult: """Collect and persist one shared business-basis subscription snapshot. ``anomaly_index`` threads the fleet refresh's per-integration memo through; single-scope callers omit it. See ``ScopeAnomalyIndex``. """ if db.new or db.dirty or db.deleted: raise RuntimeError("build_cloud_scope_snapshot requires a clean, dedicated DB session") facts = await collect_cloud_scope_facts( db, scope_id, now=now, anomaly_index=anomaly_index, ) return await persist_compiled_snapshot( db, subject=SnapshotSubject("cloud_scope", scope_id, BUSINESS_AUDIENCE_KEY), score_version=CLOUD_SCOPE_SCORE_VERSION, health=facts.health, aggregate_facts=facts.aggregate_facts, reference_map=facts.reference_map, data_as_of=facts.data_as_of, ) __all__ = ["build_cloud_scope_snapshot"]