euv-core 0.46.0

Core cross-platform runtime for euv featuring arena-based fine-grained reactivity, deterministic layout, flat scene graph, multi-mode rendering, and direct OS platform adapters.
Documentation
use super::*;

/// Client configuration controlling the background `UpdateWorker`, negotiation, verification, and budgets.
#[derive(Clone, Data, Debug, Eq, New, PartialEq)]
pub struct UpdateClientConfig {
    /// Base HTTP URL of the Hyperlane update server (e.g. `"http://127.0.0.1:8080"`).
    pub(crate) base_url: String,
    /// Logical project identifier.
    pub(crate) project: String,
    /// Release channel name (e.g. `"prod"`).
    pub(crate) channel: String,
    /// Trusted HMAC-SHA256 verification key bytes.
    pub(crate) verify_key: Vec<u8>,
    /// Running host ABI version.
    #[new(skip)]
    #[get(type(copy))]
    pub(crate) host_abi: u32,
    /// Allowed channel delivery mode (`WasmComponent` or `DeclarativeData`).
    #[new(skip)]
    #[get(type(copy))]
    pub(crate) delivery_mode: DeliveryChannelMode,
    /// Capabilities advertised by this host build.
    #[new(skip)]
    pub(crate) capabilities: Vec<String>,
    /// Optional preferred compatibility track override.
    #[new(skip)]
    #[get(skip)]
    pub(crate) preferred_track: Option<String>,
    /// Maximum size in bytes allowed for any single artifact.
    #[new(skip)]
    #[get(type(copy))]
    pub(crate) max_artifact_bytes: u64,
    /// Capacity of the bounded non-blocking `CandidateMailbox`.
    #[new(skip)]
    #[get(type(copy))]
    pub(crate) mailbox_capacity: usize,
    /// SSE reconnect backoff in milliseconds.
    #[new(skip)]
    #[get(type(copy))]
    pub(crate) reconnect_backoff_ms: u64,
    /// HTTP connect/read timeout in milliseconds.
    #[new(skip)]
    #[get(type(copy))]
    pub(crate) http_timeout_ms: u64,
    /// Wasm sandbox fuel, memory, and size limits applied during warm-up and mount.
    #[new(skip)]
    #[get(type(copy))]
    pub(crate) sandbox_config: WasmSandboxConfig,
}

/// Pure-std HTTP/1.1 release transport fetching signed manifests and range-resumable content-addressed blobs.
#[derive(Clone, Data, Debug, Eq, New, PartialEq)]
pub struct HttpReleaseTransport {
    /// Base HTTP URL (`http://host:port`).
    pub(crate) base_url: String,
    /// Connect and read timeout in milliseconds.
    #[get(type(copy))]
    pub(crate) timeout_ms: u64,
}

/// Fully verified, pre-compiled, and pre-migrated release candidate handed from `UpdateWorker` to the UI thread.
#[derive(Clone, Data, Debug)]
pub struct PreparedReleaseCandidate {
    /// Verified signed target `ReleaseManifest`.
    pub(crate) manifest: ReleaseManifest,
    /// SHA-256 digest of the verified manifest JSON.
    pub(crate) manifest_sha256: String,
    /// Pre-parsed / warmed artifacts keyed by `"module_id@version"`.
    pub(crate) warmed_artifacts: BTreeMap<String, WarmedArtifact>,
    /// Authoritative route paths whose pinned dependency graph changed relative to the previous manifest.
    pub(crate) affected_routes: Vec<String>,
    /// SHA-256 blob hashes downloaded over the wire during this preparation.
    pub(crate) newly_fetched_hashes: Vec<String>,
    /// SHA-256 blob hashes reused from the local verified content cache without re-downloading.
    pub(crate) reused_cached_hashes: Vec<String>,
    /// Pre-migrated nested component UI state snapshots keyed by canonical `StableAddress`.
    pub(crate) migrated_ui_states: BTreeMap<String, ComponentUiStateSnapshot>,
    /// Global authoritative business store revision `R` at snapshot capture time.
    #[get(type(copy))]
    pub(crate) snapshot_business_revision: u64,
    /// Per-entity baseline revisions captured at snapshot time `R` for `R -> R+n` catch-up.
    pub(crate) snapshot_entity_revisions: BTreeMap<String, u64>,
    /// OS thread name on which verification, compilation, and migration executed.
    pub(crate) prepared_on_thread: String,
}

/// Interior queue state for the bounded non-blocking `CandidateMailbox`.
#[derive(Debug)]
pub(crate) struct CandidateMailboxInner {
    /// Maximum number of queued candidates.
    pub(crate) capacity: usize,
    /// Highest release sequence accepted so far to reject stale candidates.
    pub(crate) highest_seen_seq: u64,
    /// Bounded FIFO queue of prepared candidates.
    pub(crate) queue: VecDeque<PreparedReleaseCandidate>,
}

/// Bounded non-blocking handoff mailbox decoupling the background `UpdateWorker` from the UI/render thread.
#[derive(Clone, Debug)]
pub struct CandidateMailbox {
    /// Shared mutex-guarded bounded queue accessed via `try_lock` on the UI thread.
    pub(crate) inner: Arc<Mutex<CandidateMailboxInner>>,
}

/// Shared worker state coordinating SSE cursor, verified blob cache, fetch telemetry, and shutdown.
#[derive(CustomDebug)]
pub(crate) struct UpdateWorkerShared {
    /// Client negotiation, verification, and budget configuration.
    pub(crate) config: UpdateClientConfig,
    /// Bounded non-blocking candidate delivery mailbox.
    pub(crate) mailbox: CandidateMailbox,
    /// Verified content-addressed blob cache keyed by 64-char SHA-256 hex digest.
    pub(crate) blob_cache: Mutex<HashMap<String, Arc<Vec<u8>>>>,
    /// Chronological list of SHA-256 hashes actually downloaded over the transport (for incremental verification).
    pub(crate) fetched_hashes_log: Mutex<Vec<String>>,
    /// Latest accepted SSE `Last-Event-ID` cursor.
    pub(crate) last_event_id: Mutex<Option<String>>,
    /// Latest manifest successfully prepared on the background worker.
    pub(crate) last_prepared_manifest: Mutex<Option<ReleaseManifest>>,
    /// Optional latest background error recorded without interrupting live UI.
    pub(crate) last_error: Mutex<Option<DynamicReleaseError>>,
    /// Pending route UI snapshot queued by the UI thread for background migration.
    pub(crate) pending_snapshot: Mutex<Option<NestedStateSnapshot>>,
    /// Pure background state migrator.
    #[debug(skip)]
    pub(crate) migrator: SharedStateMigrator,
    /// Cooperative stop flag terminating the worker thread cleanly.
    pub(crate) running: AtomicBool,
    /// One-shot flag requesting immediate manifest reconciliation.
    pub(crate) reconcile_requested: AtomicBool,
    /// Count of completed SSE reconnect cycles.
    pub(crate) sse_connect_count: AtomicU64,
}

/// Running handle to the dedicated background `UpdateWorker` thread.
#[derive(Debug)]
pub struct UpdateWorkerHandle {
    /// Shared worker coordination state.
    pub(crate) shared: Arc<UpdateWorkerShared>,
    /// Dedicated background worker OS thread handle.
    #[cfg(not(target_arch = "wasm32"))]
    pub(crate) worker_thread: Option<JoinHandle<()>>,
}

/// Active ref-counted release generation pinned by one or more open routes or in-flight operations.
#[derive(Clone, Data, Debug)]
pub struct ReleaseGeneration {
    /// Monotonic release generation identifier (`== manifest.release_seq`).
    #[get(type(copy))]
    pub(crate) generation_id: u64,
    /// Signed immutable manifest defining this generation.
    pub(crate) manifest: ReleaseManifest,
    /// Pre-warmed artifacts shared across routes pinned to this generation.
    pub(crate) warmed_artifacts: BTreeMap<String, WarmedArtifact>,
    /// Number of currently open route instances pinned to this generation.
    #[get(type(copy))]
    pub(crate) active_route_pins: usize,
    /// Number of active in-flight tasks still referencing this generation.
    #[get(type(copy))]
    pub(crate) active_in_flight_tasks: usize,
}

/// Per-route pinned dependency graph handle isolating open Route A from open Route B.
#[derive(Clone, Data, Debug)]
pub struct RoutePinHandle {
    /// Stable route instance identifier (e.g. `"A#tab-1"`).
    pub(crate) route_instance_id: String,
    /// Normalized route path (e.g. `"/route-a"`).
    pub(crate) route: String,
    /// Pinned release generation identifier.
    #[get(type(copy))]
    pub(crate) generation_id: u64,
    /// Pinned entry artifact key (`"page-a@1.0.0"`).
    pub(crate) entry_module: String,
    /// Pinned logical slot bindings (`"card"` -> `"card@1.0.0"` or `"card@2.0.0"`).
    pub(crate) slot_bindings: BTreeMap<String, String>,
    /// Cancellation token flipped when this pinned instance is superseded or closed.
    pub(crate) cancellation_token: Arc<AtomicBool>,
}

/// Host-mediated cross-route action preventing component version leakage between routes (Sec 11.2).
#[derive(Clone, Data, Debug, New)]
pub struct CrossRouteAction {
    /// Target route path to enter or activate.
    pub(crate) target_route: String,
    /// Logical action identifier validated against host permissions.
    pub(crate) action_id: String,
    /// Typed JSON action payload.
    pub(crate) payload: Value,
    /// Release generation active on the originating route.
    #[get(type(copy))]
    pub(crate) origin_generation: u64,
    /// Cancellation token tied to the originating route instance.
    pub(crate) cancellation_token: Arc<AtomicBool>,
}

/// Result summary emitted by `DynamicReleaseRuntime::activate_release`.
#[derive(Clone, Data, Debug, Default, Eq, New, PartialEq)]
pub struct ActivationReport {
    /// Target release sequence evaluated.
    #[get(type(copy))]
    pub(crate) release_seq: u64,
    /// Open route instance IDs atomically hot-swapped at this frame boundary.
    pub(crate) swapped_routes: Vec<String>,
    /// Open route instance IDs left untouched on their prior pinned generation.
    pub(crate) unchanged_pinned_routes: Vec<String>,
    /// Whether activation was safely deferred due to an unsafe interaction boundary.
    #[get(type(copy))]
    pub(crate) deferred: bool,
    /// Human-readable deferral reason when `deferred == true`.
    pub(crate) defer_reason: String,
    /// Number of business state deltas replayed from `R` to `R+n`.
    #[get(type(copy))]
    pub(crate) caught_up_deltas: usize,
    /// Number of unreachable old release generations reclaimed by GC.
    #[get(type(copy))]
    pub(crate) gc_reclaimed_generations: usize,
}

/// Client runtime orchestrator managing per-route pinned dependency graphs, safe swaps, business state, and GC.
#[derive(CustomDebug)]
pub struct DynamicReleaseRuntime {
    /// Ambient `DynamicUpdateHub` receiving surgical frame-boundary slot updates.
    pub(crate) hub: DynamicUpdateHub,
    /// Host-owned authoritative business state store surviving swaps and rollbacks.
    pub(crate) business_store: BusinessStateStore,
    /// Wasm sandbox execution configuration.
    pub(crate) sandbox_config: WasmSandboxConfig,
    /// Currently active latest release sequence (`0` before any release is installed).
    pub(crate) active_release_seq: u64,
    /// Live and ref-counted release generations keyed by `generation_id`.
    pub(crate) generations: BTreeMap<u64, ReleaseGeneration>,
    /// Currently open pinned route instances keyed by `route_instance_id`.
    pub(crate) open_routes: BTreeMap<String, RoutePinHandle>,
    /// Last migrated per-instance UI state snapshots keyed by canonical `StableAddress`.
    pub(crate) restored_ui_states: BTreeMap<String, ComponentUiStateSnapshot>,
    /// Candidate deferred at an unsafe interaction boundary awaiting the next safe frame.
    pub(crate) deferred_candidate: Option<PreparedReleaseCandidate>,
    /// Monotonic component revision counter applied onto `DynamicUpdateHub` slots.
    pub(crate) next_hub_revision: u64,
}