Skip to main content

euv_core/dynamic/engine/
struct.rs

1use super::*;
2
3/// Proxy-aware on-device hot-update connection configuration bridging direct, reverse-proxy, and HTTP forward-proxy transports.
4#[derive(Clone, Data, Debug, Deserialize, Eq, PartialEq, Serialize)]
5pub struct HotUpdateProxyConfig {
6    /// Target update server base URL (direct `http://127.0.0.1:8787` or reverse proxy `https://proxy.host/euv`).
7    pub(crate) server_url: String,
8    /// Optional explicit HTTP forward proxy URL (`http://proxy.local:7890`).
9    #[get(skip)]
10    #[serde(default)]
11    pub(crate) proxy_url: Option<String>,
12    /// Optional proxy credentials (`"user:pass"`, `"Basic ..."`, or `"Bearer ..."`).
13    #[get(skip)]
14    #[serde(default)]
15    pub(crate) proxy_auth: Option<String>,
16    /// Release channel name (e.g. `"prod"` or `"dev"`).
17    pub(crate) channel: String,
18    /// Whether verified incoming candidates apply automatically without manual prompt.
19    #[get(type(copy))]
20    pub(crate) auto_apply: bool,
21    /// Fallback manifest polling interval in seconds when SSE is unavailable.
22    #[get(type(copy))]
23    pub(crate) poll_fallback_secs: u64,
24}
25
26/// Client configuration controlling the background `UpdateWorker`, negotiation, verification, and budgets.
27#[derive(Clone, Data, Debug, Eq, New, PartialEq)]
28pub struct UpdateClientConfig {
29    /// Base HTTP URL of the Hyperlane update server (e.g. `"http://127.0.0.1:8080"`).
30    pub(crate) base_url: String,
31    /// Logical project identifier.
32    pub(crate) project: String,
33    /// Release channel name (e.g. `"prod"`).
34    pub(crate) channel: String,
35    /// Trusted HMAC-SHA256 verification key bytes.
36    pub(crate) verify_key: Vec<u8>,
37    /// Optional explicit HTTP forward proxy URL (`http://proxy:port`).
38    #[new(skip)]
39    #[get(skip)]
40    pub(crate) proxy_url: Option<String>,
41    /// Optional forward-proxy `Proxy-Authorization` credential (`"user:pass"` or `"Basic ..."`).
42    #[new(skip)]
43    #[get(skip)]
44    pub(crate) proxy_auth: Option<String>,
45    /// Running host ABI version.
46    #[new(skip)]
47    #[get(type(copy))]
48    pub(crate) host_abi: u32,
49    /// Allowed channel delivery mode (`WasmComponent` or `DeclarativeData`).
50    #[new(skip)]
51    #[get(type(copy))]
52    pub(crate) delivery_mode: DeliveryChannelMode,
53    /// Capabilities advertised by this host build.
54    #[new(skip)]
55    pub(crate) capabilities: Vec<String>,
56    /// Optional preferred compatibility track override.
57    #[new(skip)]
58    #[get(skip)]
59    pub(crate) preferred_track: Option<String>,
60    /// Maximum size in bytes allowed for any single artifact.
61    #[new(skip)]
62    #[get(type(copy))]
63    pub(crate) max_artifact_bytes: u64,
64    /// Capacity of the bounded non-blocking `CandidateMailbox`.
65    #[new(skip)]
66    #[get(type(copy))]
67    pub(crate) mailbox_capacity: usize,
68    /// SSE reconnect backoff in milliseconds.
69    #[new(skip)]
70    #[get(type(copy))]
71    pub(crate) reconnect_backoff_ms: u64,
72    /// HTTP connect/read timeout in milliseconds.
73    #[new(skip)]
74    #[get(type(copy))]
75    pub(crate) http_timeout_ms: u64,
76    /// Wasm sandbox fuel, memory, and size limits applied during warm-up and mount.
77    #[new(skip)]
78    #[get(type(copy))]
79    pub(crate) sandbox_config: WasmSandboxConfig,
80}
81
82/// Pure-std HTTP/1.1 release transport fetching signed manifests and range-resumable content-addressed blobs.
83#[derive(Clone, Data, Debug, Eq, New, PartialEq)]
84pub struct HttpReleaseTransport {
85    /// Base HTTP URL (`http://host:port` or reverse-proxy prefix `http://proxy:port/prefix`).
86    pub(crate) base_url: String,
87    /// Connect and read timeout in milliseconds.
88    #[get(type(copy))]
89    pub(crate) timeout_ms: u64,
90    /// Optional explicit HTTP forward proxy URL (`http://proxy:port`).
91    #[new(skip)]
92    #[get(skip)]
93    pub(crate) proxy_url: Option<String>,
94    /// Optional forward-proxy `Proxy-Authorization` credential.
95    #[new(skip)]
96    #[get(skip)]
97    pub(crate) proxy_auth: Option<String>,
98}
99
100/// Fully verified, pre-compiled, and pre-migrated release candidate handed from `UpdateWorker` to the UI thread.
101#[derive(Clone, Data, Debug)]
102pub struct PreparedReleaseCandidate {
103    /// Verified signed target `ReleaseManifest`.
104    pub(crate) manifest: ReleaseManifest,
105    /// SHA-256 digest of the verified manifest JSON.
106    pub(crate) manifest_sha256: String,
107    /// Pre-parsed / warmed artifacts keyed by `"module_id@version"`.
108    pub(crate) warmed_artifacts: BTreeMap<String, WarmedArtifact>,
109    /// Authoritative route paths whose pinned dependency graph changed relative to the previous manifest.
110    pub(crate) affected_routes: Vec<String>,
111    /// SHA-256 blob hashes downloaded over the wire during this preparation.
112    pub(crate) newly_fetched_hashes: Vec<String>,
113    /// SHA-256 blob hashes reused from the local verified content cache without re-downloading.
114    pub(crate) reused_cached_hashes: Vec<String>,
115    /// Pre-migrated nested component UI state snapshots keyed by canonical `StableAddress`.
116    pub(crate) migrated_ui_states: BTreeMap<String, ComponentUiStateSnapshot>,
117    /// Global authoritative business store revision `R` at snapshot capture time.
118    #[get(type(copy))]
119    pub(crate) snapshot_business_revision: u64,
120    /// Per-entity baseline revisions captured at snapshot time `R` for `R -> R+n` catch-up.
121    pub(crate) snapshot_entity_revisions: BTreeMap<String, u64>,
122    /// OS thread name on which verification, compilation, and migration executed.
123    pub(crate) prepared_on_thread: String,
124}
125
126/// Interior queue state for the bounded non-blocking `CandidateMailbox`.
127#[derive(Debug)]
128pub(crate) struct CandidateMailboxInner {
129    /// Maximum number of queued candidates.
130    pub(crate) capacity: usize,
131    /// Highest release sequence accepted so far to reject stale candidates.
132    pub(crate) highest_seen_seq: u64,
133    /// Bounded FIFO queue of prepared candidates.
134    pub(crate) queue: VecDeque<PreparedReleaseCandidate>,
135}
136
137/// Bounded non-blocking handoff mailbox decoupling the background `UpdateWorker` from the UI/render thread.
138#[derive(Clone, Debug)]
139pub struct CandidateMailbox {
140    /// Shared mutex-guarded bounded queue accessed via `try_lock` on the UI thread.
141    pub(crate) inner: Arc<Mutex<CandidateMailboxInner>>,
142}
143
144/// Shared worker state coordinating SSE cursor, verified blob cache, fetch telemetry, and shutdown.
145#[derive(CustomDebug)]
146pub(crate) struct UpdateWorkerShared {
147    /// Client negotiation, verification, and budget configuration.
148    pub(crate) config: UpdateClientConfig,
149    /// Bounded non-blocking candidate delivery mailbox.
150    pub(crate) mailbox: CandidateMailbox,
151    /// Verified content-addressed blob cache keyed by 64-char SHA-256 hex digest.
152    pub(crate) blob_cache: Mutex<HashMap<String, Arc<Vec<u8>>>>,
153    /// Chronological list of SHA-256 hashes actually downloaded over the transport (for incremental verification).
154    pub(crate) fetched_hashes_log: Mutex<Vec<String>>,
155    /// Latest accepted SSE `Last-Event-ID` cursor.
156    pub(crate) last_event_id: Mutex<Option<String>>,
157    /// Latest manifest successfully prepared on the background worker.
158    pub(crate) last_prepared_manifest: Mutex<Option<ReleaseManifest>>,
159    /// Optional latest background error recorded without interrupting live UI.
160    pub(crate) last_error: Mutex<Option<DynamicReleaseError>>,
161    /// Pending route UI snapshot queued by the UI thread for background migration.
162    pub(crate) pending_snapshot: Mutex<Option<NestedStateSnapshot>>,
163    /// Pure background state migrator.
164    #[debug(skip)]
165    pub(crate) migrator: Arc<dyn StateMigrator>,
166    /// Cooperative stop flag terminating the worker thread cleanly.
167    pub(crate) running: AtomicBool,
168    /// One-shot flag requesting immediate manifest reconciliation.
169    pub(crate) reconcile_requested: AtomicBool,
170    /// Count of completed SSE reconnect cycles.
171    pub(crate) sse_connect_count: AtomicU64,
172}
173
174/// Running handle to the dedicated background `UpdateWorker` thread.
175#[derive(Debug)]
176pub struct UpdateWorkerHandle {
177    /// Shared worker coordination state.
178    pub(crate) shared: Arc<UpdateWorkerShared>,
179    /// Dedicated background worker OS thread handle.
180    #[cfg(not(target_arch = "wasm32"))]
181    pub(crate) worker_thread: Option<JoinHandle<()>>,
182}
183
184/// Active ref-counted release generation pinned by one or more open routes or in-flight operations.
185#[derive(Clone, Data, Debug)]
186pub struct ReleaseGeneration {
187    /// Monotonic release generation identifier (`== manifest.release_seq`).
188    #[get(type(copy))]
189    pub(crate) generation_id: u64,
190    /// Signed immutable manifest defining this generation.
191    pub(crate) manifest: ReleaseManifest,
192    /// Pre-warmed artifacts shared across routes pinned to this generation.
193    pub(crate) warmed_artifacts: BTreeMap<String, WarmedArtifact>,
194    /// Number of currently open route instances pinned to this generation.
195    #[get(type(copy))]
196    pub(crate) active_route_pins: usize,
197    /// Number of active in-flight tasks still referencing this generation.
198    #[get(type(copy))]
199    pub(crate) active_in_flight_tasks: usize,
200}
201
202/// Per-route pinned dependency graph handle isolating open Route A from open Route B.
203#[derive(Clone, Data, Debug)]
204pub struct RoutePinHandle {
205    /// Stable route instance identifier (e.g. `"A#tab-1"`).
206    pub(crate) route_instance_id: String,
207    /// Normalized route path (e.g. `"/route-a"`).
208    pub(crate) route: String,
209    /// Pinned release generation identifier.
210    #[get(type(copy))]
211    pub(crate) generation_id: u64,
212    /// Pinned entry artifact key (`"page-a@1.0.0"`).
213    pub(crate) entry_module: String,
214    /// Pinned logical slot bindings (`"card"` -> `"card@1.0.0"` or `"card@2.0.0"`).
215    pub(crate) slot_bindings: BTreeMap<String, String>,
216    /// Cancellation token flipped when this pinned instance is superseded or closed.
217    pub(crate) cancellation_token: Arc<AtomicBool>,
218}
219
220/// Host-mediated cross-route action preventing component version leakage between routes (Sec 11.2).
221#[derive(Clone, Data, Debug, New)]
222pub struct CrossRouteAction {
223    /// Target route path to enter or activate.
224    pub(crate) target_route: String,
225    /// Logical action identifier validated against host permissions.
226    pub(crate) action_id: String,
227    /// Typed JSON action payload.
228    pub(crate) payload: Value,
229    /// Release generation active on the originating route.
230    #[get(type(copy))]
231    pub(crate) origin_generation: u64,
232    /// Cancellation token tied to the originating route instance.
233    pub(crate) cancellation_token: Arc<AtomicBool>,
234}
235
236/// Result summary emitted by `DynamicReleaseRuntime::activate_release`.
237#[derive(Clone, Data, Debug, Default, Eq, New, PartialEq)]
238pub struct ActivationReport {
239    /// Target release sequence evaluated.
240    #[get(type(copy))]
241    pub(crate) release_seq: u64,
242    /// Open route instance IDs atomically hot-swapped at this frame boundary.
243    pub(crate) swapped_routes: Vec<String>,
244    /// Open route instance IDs left untouched on their prior pinned generation.
245    pub(crate) unchanged_pinned_routes: Vec<String>,
246    /// Whether activation was safely deferred due to an unsafe interaction boundary.
247    #[get(type(copy))]
248    pub(crate) deferred: bool,
249    /// Human-readable deferral reason when `deferred == true`.
250    pub(crate) defer_reason: String,
251    /// Number of business state deltas replayed from `R` to `R+n`.
252    #[get(type(copy))]
253    pub(crate) caught_up_deltas: usize,
254    /// Number of unreachable old release generations reclaimed by GC.
255    #[get(type(copy))]
256    pub(crate) gc_reclaimed_generations: usize,
257}
258
259/// Client runtime orchestrator managing per-route pinned dependency graphs, safe swaps, business state, and GC.
260#[derive(CustomDebug)]
261pub struct DynamicReleaseRuntime {
262    /// Ambient `DynamicUpdateHub` receiving surgical frame-boundary slot updates.
263    pub(crate) hub: DynamicUpdateHub,
264    /// Host-owned authoritative business state store surviving swaps and rollbacks.
265    pub(crate) business_store: BusinessStateStore,
266    /// Wasm sandbox execution configuration.
267    pub(crate) sandbox_config: WasmSandboxConfig,
268    /// Currently active latest release sequence (`0` before any release is installed).
269    pub(crate) active_release_seq: u64,
270    /// Live and ref-counted release generations keyed by `generation_id`.
271    pub(crate) generations: BTreeMap<u64, ReleaseGeneration>,
272    /// Currently open pinned route instances keyed by `route_instance_id`.
273    pub(crate) open_routes: BTreeMap<String, RoutePinHandle>,
274    /// Last migrated per-instance UI state snapshots keyed by canonical `StableAddress`.
275    pub(crate) restored_ui_states: BTreeMap<String, ComponentUiStateSnapshot>,
276    /// Candidate deferred at an unsafe interaction boundary awaiting the next safe frame.
277    pub(crate) deferred_candidate: Option<PreparedReleaseCandidate>,
278    /// Monotonic component revision counter applied onto `DynamicUpdateHub` slots.
279    pub(crate) next_hub_revision: u64,
280}