harmos-runtime 0.1.4

Harmos runtime: managed state, undo, recovery, background work and typed streams.
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
/* ***
 *
 * Harmos
 *
 * SPDX-License-Identifier: Apache-2.0
 *
 * ***
 */

/* === Modules === */

/* === Re-Exports === */

/* === Imports === */

// Standard Library Imports
use std::collections::BTreeMap;
use std::future::Future;
use std::marker::PhantomData;
use std::pin::Pin;
use std::sync::{
    Arc, Mutex,
    atomic::{AtomicBool, Ordering},
};
// Third Party Imports
use futures_util::{StreamExt, stream::FuturesUnordered};
use tokio::sync::{mpsc, oneshot, watch};
use tokio::task::JoinHandle;
use wasmtime::AsContextMut;
// Local Imports
use super::{Active, Error};
use crate::Application;
use crate::operations::children::Children;
use crate::operations::{Definition, WireAnswer, WireChildCall, WireCommand};
use crate::plugin::PluginEndpoint;
use crate::supervision::executor;

/* === Constants === */

const CALLS: usize = 128;

/* === Macros === */

/* === Type Aliases === */

type Contexts = Arc<Mutex<BTreeMap<[u8; 32], Children>>>;
type Running<A> = JoinHandle<(Owned<A>, Result<(), Error>)>;

/* === Structs === */

struct Called<'a> {
    future: Pin<Box<dyn Future<Output = bool> + Send + 'a>>,
    admission: bool,
}

#[derive(Default)]
struct Loan {
    attempts: Vec<Retired>,
    background: Option<Arc<crate::supervision::instance::Instance>>,
}

pub(crate) struct Endpoint {
    definitions: Vec<Definition>,
    scope: Option<crate::plugin::PluginScope>,
    accepting: AtomicBool,
    requests: mpsc::Sender<Request>,
    ended: watch::Receiver<bool>,
    failure: Mutex<Option<Arc<wasmtime::Error>>>,
}

pub(super) struct Session<A: Application> {
    endpoint: Arc<Endpoint>,
    stopping: watch::Sender<bool>,
    running: Option<Running<A>>,
    ending: Option<Owned<A>>,
    instance: Option<PluginEndpoint>,
    closing: bool,
    _application: PhantomData<fn() -> A>,
}

struct Owned<A: Application> {
    guest: Option<Active<A>>,
    endpoint: Arc<Endpoint>,
    ended: watch::Sender<bool>,
    loans: Arc<Mutex<BTreeMap<u64, Loan>>>,
}

struct Request {
    call: Command,
    children: Option<Children>,
    answer: oneshot::Sender<Result<Reply, Error>>,
}

struct Retired {
    contexts: Contexts,
    token: [u8; 32],
}

/* === Enums === */

enum Command {
    Operation(WireCommand),
    Background { call: WireChildCall, submitted: tokio::time::Instant },
    Close(Option<PluginEndpoint>),
}

enum Reply {
    Operation(WireAnswer),
    Background(Vec<u8>),
}

enum Ending {
    Stopped,
    Closing(Option<PluginEndpoint>),
}

/* === Traits === */

/* === Implementations === */

impl Future for Called<'_> {
    type Output = bool;
    fn poll(mut self: Pin<&mut Self>, context: &mut std::task::Context<'_>) -> std::task::Poll<bool> {
        self.future.as_mut().poll(context)
    }
}

impl Endpoint {
    pub(crate) fn accepts(&self) -> bool {
        self.accepting.load(Ordering::Acquire)
    }

    pub(crate) fn scope(&self) -> Option<&crate::plugin::PluginScope> {
        self.scope.as_ref()
    }

    pub(crate) fn definitions(&self) -> &[Definition] {
        &self.definitions
    }

    pub(crate) async fn phase(&self, command: WireCommand, children: Option<Children>) -> Result<WireAnswer, Error> {
        let reply = self.request(Command::Operation(command), children).await?;
        match reply {
            Reply::Operation(answer) => Ok(answer),
            Reply::Background(_) => unreachable!("operation replies retain their request kind"),
        }
    }

    pub(super) async fn background(&self, call: WireChildCall, submitted: tokio::time::Instant) -> Result<Vec<u8>, Error> {
        if !call
            .generation_context
            .as_ref()
            .is_some_and(|token| self.scope.as_ref().is_some_and(|scope| scope.accepts_generation_context(token)))
        {
            return Err(Error::OperationCaller(crate::Error::Closing { domain: "guest generation context" }));
        }
        match self.request(Command::Background { call, submitted }, None).await? {
            Reply::Background(payload) => Ok(payload),
            Reply::Operation(_) => unreachable!("background replies retain their request kind"),
        }
    }

    async fn request(&self, call: Command, children: Option<Children>) -> Result<Reply, Error> {
        if !self.accepts() {
            return Err(unavailable());
        }
        let (answer, answered) = oneshot::channel();
        let request = Request { call, children, answer };
        self.requests.send(request).await.map_err(|_| unavailable())?;
        answered.await.map_err(|_| {
            self.failure.lock().expect("guest terminal failure lock").clone().map_or_else(unavailable, |source| Error::Execution { source })
        })?
    }

    pub(crate) async fn settle(&self) -> Result<(), Error> {
        let mut ended = self.ended.clone();
        ended.wait_for(|ended| *ended).await.map_err(|_| unavailable())?;
        Ok(())
    }
}

impl<A: Application> Session<A> {
    pub(super) fn start(mut guest: Active<A>) -> Self {
        let (requests, incoming) = mpsc::channel(CALLS);
        let (ended, receipt) = watch::channel(false);
        let definitions = guest.store.data().declaration.operations.clone();
        let endpoint = Arc::new(Endpoint {
            scope: guest.store.data().scope.clone(),
            definitions,
            accepting: AtomicBool::new(true),
            requests,
            ended: receipt,
            failure: Mutex::new(None),
        });
        guest.store.data_mut().session = Some(Arc::downgrade(&endpoint));
        let owned = Owned { guest: Some(guest), endpoint: Arc::clone(&endpoint), ended, loans: Arc::new(Mutex::new(BTreeMap::new())) };
        let (stopping, stopped) = watch::channel(false);
        let running = executor::task(async move {
            let mut owned = owned;
            let result = {
                let running = owned.run(incoming, stopped);
                let mut running = std::pin::pin!(running);
                std::future::poll_fn(|context| {
                    match std::panic::catch_unwind(std::panic::AssertUnwindSafe(|| running.as_mut().poll(context))) {
                        Ok(poll) => poll.map(Ok),
                        Err(_) => std::task::Poll::Ready(Err(())),
                    }
                })
                .await
            };
            let result = match result {
                Ok(result) => result,
                Err(()) => {
                    let source = Arc::new(wasmtime::Error::msg("guest session machinery panicked"));
                    *owned.endpoint.failure.lock().expect("guest terminal failure lock") = Some(Arc::clone(&source));
                    owned.endpoint.accepting.store(false, Ordering::Release);
                    // A Poisoned Store Cannot Be Re-Entered; Its Host Debts Stay Owned.
                    owned.destroy_store();
                    Err(Error::Execution { source })
                }
            };
            (owned, result)
        });
        Self { endpoint, stopping, running: Some(running), ending: None, instance: None, closing: false, _application: PhantomData }
    }

    pub(super) fn endpoint(&self) -> Arc<Endpoint> {
        Arc::clone(&self.endpoint)
    }

    pub(super) async fn close(&mut self, instance: Option<PluginEndpoint>) -> Result<(), Error> {
        self.endpoint.accepting.store(false, Ordering::Release);
        if !self.closing {
            self.instance = instance;
            if !*self.endpoint.ended.borrow() {
                let (answer, _answered) = oneshot::channel();
                let request = Request { call: Command::Close(self.instance.take()), children: None, answer };
                // A Terminal Session Returns The Same Instance With Its Failed Send.
                if let Err(error) = self.endpoint.requests.send(request).await
                    && let Command::Close(instance) = error.0.call
                {
                    self.instance = instance;
                }
            }
            self.closing = true;
        }
        if let Some(running) = &mut self.running {
            let (owned, result) = running.await.map_err(|source| Error::Guest { message: source.to_string() })?;
            self.running.take();
            let mut owned = owned;
            if let Some(guest) = &mut owned.guest
                && self.instance.is_some()
            {
                guest.routing = self.instance.take();
            }
            self.ending = Some(owned);
            // A Failed Cleanup Returns Its Exact Store Owner, Never Settlement.
            result?;
        }
        if let Some(ending) = &mut self.ending {
            ending.finish().await?;
        }
        self.ending.take();
        self.instance.take();
        Ok(())
    }
}

impl<A: Application> Drop for Session<A> {
    fn drop(&mut self) {
        self.endpoint.accepting.store(false, Ordering::Release);
        self.stopping.send_replace(true);
    }
}

impl Drop for Retired {
    fn drop(&mut self) {
        self.contexts.lock().expect("guest child context lock").remove(&self.token);
    }
}

impl<A: Application> Drop for Owned<A> {
    fn drop(&mut self) {
        // Destroy The Original Store Before Publishing Physical Settlement
        let guest = self.guest.take();
        drop(guest);
        let loans = std::mem::take(&mut *self.loans.lock().expect("guest invocation loan lock"));
        drop(loans);
        self.endpoint.accepting.store(false, Ordering::Release);
        self.ended.send_replace(true);
    }
}

impl<A: Application> Owned<A> {
    async fn run(&mut self, mut incoming: mpsc::Receiver<Request>, mut stopped: watch::Receiver<bool>) -> Result<(), Error> {
        let guest = self.guest.as_mut().expect("owned guest store");
        let operate = guest.sdk.func_operate();
        let contexts = Arc::clone(&guest.store.data().children);
        let loans = Arc::clone(&self.loans);
        let fuel = guest.fuel;
        let caller = guest.store.data().caller.clone();
        let scope = guest.store.data().scope.clone();
        // The Operation Manager Owns Operation Time Policy
        guest.store.set_epoch_deadline(super::UNBOUNDED);
        let ending = guest.store.run_concurrent(async |accessor| {
            let mut pending: FuturesUnordered<Called<'_>> = FuturesUnordered::new();
            let ending = loop {
                tokio::select! {
                    biased;
                    _ = stopped.wait_for(|stopped| *stopped) => break Ending::Stopped,
                    completed = pending.next(), if !pending.is_empty() => {
                        if completed == Some(true) { break Ending::Stopped; }
                    }
                    request = incoming.recv() => {
                        let Some(mut request) = request else { break Ending::Stopped; };
                        if let Command::Close(instance) = request.call { break Ending::Closing(instance); }
                        if request.answer.is_closed() { continue; }
                        let background_debts = loans.lock().expect("guest invocation loan lock").values().filter(|loan| loan.background.is_some()).count();
                        let finite = pending.iter().filter(|call| call.admission).count();
                        // Finalization Borrows An Already Admitted OperationInstance's Receipt.
                        // It Must Progress Even When Retained Background Debts Fill Admission.
                        let finalization = matches!(&request.call, Command::Operation(WireCommand::Finalize { .. }));
                        if !finalization && finite + background_debts >= CALLS {
                            let _ = request.answer.send(Err(crate::plugin::DispatchError::Busy.into()));
                            continue;
                        }
                        if let Command::Background { call, submitted } = request.call {
                            let admitted = if call.generation_context.as_ref().is_some_and(|token| scope.as_ref().is_some_and(|scope| scope.accepts_generation_context(token))) {
                                caller.as_ref().ok_or(crate::Error::Closing { domain: "guest generation context" })
                                    .and_then(|caller| caller.submit_encoded_at(&call.operation, call.payload, submitted, call.values))
                            } else { Err(crate::Error::Closing { domain: "guest generation context" }) };
                            let invocation = match admitted {
                                Ok(invocation) => invocation,
                                Err(error) => { let _ = request.answer.send(Err(Error::OperationCaller(error))); continue; }
                            };
                            let id = invocation.id().value();
                            let control = Arc::clone(&invocation.control);
                            loans.lock().expect("guest invocation loan lock").entry(id).or_default().background = Some(Arc::clone(&control));
                            let retained = Arc::clone(&loans);
                            let future = async move {
                                let (_cancel, cancelled) = oneshot::channel();
                                let result = tokio::select! {
                                    biased;
                                    _ = request.answer.closed() => None,
                                    result = crate::operations::children::lifecycle(invocation, cancelled) => Some(result),
                                };
                                if result.is_some()
                                    && !matches!(&result, Some(Err(crate::Error::OperationSettlement { .. })))
                                    && control.is_settled()
                                {
                                    let retired = {
                                        let mut retained = retained.lock().expect("guest invocation loan lock");
                                        let loan = retained.get_mut(&id).expect("original background receipt");
                                        let control = loan.background.take();
                                        if loan.attempts.is_empty() { retained.remove(&id); }
                                        control
                                    };
                                    drop(retired);
                                }
                                if let Some(result) = result {
                                    let _ = request.answer.send(result.map(Reply::Background).map_err(Error::OperationCaller));
                                }
                                false
                            };
                            pending.push(Called { future: Box::pin(future), admission: false });
                            continue;
                        }
                        if let Some(scope) = request.children.take() {
                            let Command::Operation(WireCommand::Initialize { invocation, .. } | WireCommand::Attempt { invocation, .. } | WireCommand::StreamAttempt { invocation, .. }) = &request.call else {
                                let _ = request.answer.send(Err(Error::Guest { message: "guest child context requires initialization or an attempt".to_owned() }));
                                continue;
                            };
                            let token = scope.context;
                            let mut scopes = contexts.lock().expect("guest child context lock");
                            if scopes.contains_key(&token) {
                                let _ = request.answer.send(Err(Error::Guest { message: "guest attempt context is already active".to_owned() }));
                                continue;
                            }
                            scopes.insert(token, scope);
                            drop(scopes);
                            loans.lock().expect("guest invocation loan lock")
                                .entry(*invocation).or_default().attempts
                                .push(Retired { contexts: Arc::clone(&contexts), token });
                        }
                        let context = accessor;
                        let refill = pending.is_empty();
                        let retained = Arc::clone(&loans);
                        let finalizing = match &request.call {
                            Command::Operation(WireCommand::Finalize { invocation }) => Some(*invocation),
                            _ => None,
                        };
                        let future = async move {
                            let called = async {
                                if refill {
                                    context.with(|mut access| access.as_context_mut().set_fuel(fuel)).map_err(|source| Error::Execution { source: Arc::new(source) })?;
                                }
                                match request.call {
                                    Command::Operation(command) => {
                                        let function = operate;
                                        let payload = command.encode().map_err(|source| Error::Malformed { method: "operate".to_owned(), message: source.to_string() })?;
                                        let (reply,) = function.call_concurrent(context, (payload,)).await.map_err(|source| Error::Execution { source: Arc::new(source) })?;
                                        let reply = reply.map_err(|refusal| Error::Operation { refusal: super::refused(refusal) })?;
                                        let answer = WireAnswer::decode(&reply).map_err(|source| Error::Malformed { method: "operate".to_owned(), message: source.to_string() })?;
                                        Ok(Reply::Operation(answer))
                                    }
                                    Command::Close(_) | Command::Background { .. } => unreachable!("control and background work belong to the session owner"),
                                }
                            };
                            let result = tokio::select! {
                                biased;
                                _ = request.answer.closed() => None,
                                result = called => Some(result),
                            };
                            // Retain The Original Loan Until SDK State Physically Finalizes
                            if matches!(&result, Some(Ok(Reply::Operation(WireAnswer::Ready { payload }))) if payload.is_empty())
                                && let Some(invocation) = finalizing
                            {
                                let retired = {
                                    let mut retained = retained.lock().expect("guest invocation loan lock");
                                    let loan = retained.get_mut(&invocation);
                                    let retired = loan.map(|loan| std::mem::take(&mut loan.attempts));
                                    if retained.get(&invocation).is_some_and(|loan| loan.background.is_none()) { retained.remove(&invocation); }
                                    retired
                                };
                                drop(retired);
                            }
                            let failed = result.is_none() || result.as_ref().is_some_and(|result| matches!(result, Err(Error::Execution { .. })));
                            if let Some(result) = result { let _ = request.answer.send(result); }
                            failed
                        };
                        pending.push(Called { future: Box::pin(future), admission: !finalization });
                    }
                }
            };
            // Cancel Calls And Drive Their Guest Tasks Through Physical Exit
            drop(pending);
            if matches!(ending, Ending::Stopped) { return ending; }
            tokio::select! {
                biased;
                _ = stopped.wait_for(|stopped| *stopped) => Ending::Stopped,
                _ = std::future::poll_fn(|context| accessor.poll_no_interesting_tasks(context)) => ending,
            }
        }).await;
        // Retain The Original Trap Before Closed Reply Waiters Resume
        let ending = match ending {
            Ok(ending) => ending,
            Err(source) => {
                *self.endpoint.failure.lock().expect("guest terminal failure lock") = Some(Arc::new(source));
                self.endpoint.accepting.store(false, Ordering::Release);
                Ending::Stopped
            }
        };
        self.endpoint.accepting.store(false, Ordering::Release);
        if let Ending::Closing(instance) = ending {
            guest.routing = instance;
        }
        self.finish().await
    }

    fn destroy_store(&mut self) {
        let guest = self.guest.take();
        drop(guest);
        let attempts: Vec<_> =
            self.loans.lock().expect("guest invocation loan lock").values_mut().map(|loan| std::mem::take(&mut loan.attempts)).collect();
        drop(attempts);
        // This Acknowledges Store Destruction, Not Host Background Settlement.
        self.ended.send_replace(true);
    }

    async fn finish(&mut self) -> Result<(), Error> {
        self.destroy_store();
        let controls: Vec<_> = self
            .loans
            .lock()
            .expect("guest invocation loan lock")
            .iter()
            .filter_map(|(id, loan)| loan.background.as_ref().map(|control| (*id, Arc::clone(control))))
            .collect();
        for (id, control) in controls {
            control.settle().await.map_err(|source| Error::OperationCaller(crate::Error::OperationSettlement { source }))?;
            let retired = self.loans.lock().expect("guest invocation loan lock").remove(&id);
            drop(retired);
        }
        self.loans.lock().expect("guest invocation loan lock").clear();
        Ok(())
    }
}

/* === Public Functions === */

/* === Private Functions === */

fn unavailable() -> Error {
    Error::Unavailable { method: "operate".to_owned(), discarded: "the original guest store is unavailable".to_owned() }
}

/* === Tests === */