a3s-effect 0.1.0

Effect-style actor runtime for projecting an A3S Code harness from an immutable event log
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
//! Programs as values.
//!
//! An [`Effect`] describes a program: the value it produces, the expected
//! error it can return, and the services it needs. Constructing one does not
//! run it. [`Effect::run`] is the edge where a description becomes a result.
//!
//! The operators follow the Effect v4 onboarding split: typed expected
//! failures, retries as a schedule, structured concurrency that cancels the
//! sibling it no longer needs, resource release on every exit including
//! cancellation, services passed in the type, and spans recorded by the
//! runtime.

use std::future::Future;
use std::marker::PhantomData;
use std::pin::Pin;
use std::sync::{Arc, Mutex};
use std::time::Duration;

use tokio_util::sync::CancellationToken;

use crate::exit::Exit;

pub type BoxFuture<T> = Pin<Box<dyn Future<Output = T> + Send>>;

#[derive(Debug, Clone, PartialEq, Eq)]
pub struct TraceEvent {
    pub name: String,
    pub outcome: &'static str,
}

pub struct RunCtx<S> {
    pub services: Arc<S>,
    pub cancel: CancellationToken,
    trace: Arc<Mutex<Vec<TraceEvent>>>,
}

impl<S> Clone for RunCtx<S> {
    fn clone(&self) -> Self {
        Self {
            services: Arc::clone(&self.services),
            cancel: self.cancel.clone(),
            trace: Arc::clone(&self.trace),
        }
    }
}

impl<S> RunCtx<S> {
    pub fn child(&self) -> Self {
        Self {
            services: Arc::clone(&self.services),
            cancel: self.cancel.child_token(),
            trace: Arc::clone(&self.trace),
        }
    }

    fn record(&self, name: impl Into<String>, outcome: &'static str) {
        if let Ok(mut events) = self.trace.lock() {
            events.push(TraceEvent {
                name: name.into(),
                outcome,
            });
        }
    }
}

/// A description of a program. `A` is the success value, `E` is the expected
/// error, and `S` is the service environment required to run it.
pub struct Effect<A, E, S> {
    run: Arc<dyn Fn(RunCtx<S>) -> BoxFuture<Result<A, Exit<E>>> + Send + Sync>,
    _marker: PhantomData<fn() -> (A, E)>,
}

impl<A, E, S> Clone for Effect<A, E, S> {
    fn clone(&self) -> Self {
        Self {
            run: Arc::clone(&self.run),
            _marker: PhantomData,
        }
    }
}

impl<A, E, S> Effect<A, E, S>
where
    A: Send + Sync + 'static,
    E: Send + Sync + 'static,
    S: Send + Sync + 'static,
{
    fn new<F>(run: F) -> Self
    where
        F: Fn(RunCtx<S>) -> BoxFuture<Result<A, Exit<E>>> + Send + Sync + 'static,
    {
        Self {
            run: Arc::new(run),
            _marker: PhantomData,
        }
    }

    pub fn succeed(value: A) -> Self
    where
        A: Clone,
    {
        let value = Arc::new(value);
        Self::new(move |_ctx| {
            let value = Arc::clone(&value);
            Box::pin(async move { Ok((*value).clone()) })
        })
    }

    pub fn fail(error: E) -> Self
    where
        E: Clone,
    {
        let error = Arc::new(error);
        Self::new(move |_ctx| {
            let error = Arc::clone(&error);
            Box::pin(async move { Err(Exit::Fail((*error).clone())) })
        })
    }

    pub fn die(message: impl Into<String>) -> Self {
        let message = Arc::new(message.into());
        Self::new(move |_ctx| {
            let message = Arc::clone(&message);
            Box::pin(async move { Err(Exit::Die((*message).clone())) })
        })
    }

    /// Build a program from services and a cancellation token. The function
    /// runs only when [`Effect::run`] is called.
    pub fn from_async<F, Fut>(f: F) -> Self
    where
        F: Fn(Arc<S>, CancellationToken) -> Fut + Send + Sync + 'static,
        Fut: Future<Output = Result<A, Exit<E>>> + Send + 'static,
    {
        let f = Arc::new(f);
        Self::new(move |ctx| {
            if ctx.cancel.is_cancelled() {
                return Box::pin(async { Err(Exit::Interrupt) });
            }
            let fut = f(Arc::clone(&ctx.services), ctx.cancel.clone());
            Box::pin(fut)
        })
    }

    pub fn map<B, F>(&self, f: F) -> Effect<B, E, S>
    where
        B: Send + Sync + 'static,
        F: Fn(A) -> B + Send + Sync + 'static,
    {
        let run = Arc::clone(&self.run);
        let f = Arc::new(f);
        Effect::new(move |ctx| {
            let run = Arc::clone(&run);
            let f = Arc::clone(&f);
            Box::pin(async move { run(ctx).await.map(|value| f(value)) })
        })
    }

    pub fn and_then<B, F>(&self, f: F) -> Effect<B, E, S>
    where
        B: Send + Sync + 'static,
        F: Fn(A) -> Effect<B, E, S> + Send + Sync + 'static,
    {
        let run = Arc::clone(&self.run);
        let f = Arc::new(f);
        Effect::new(move |ctx| {
            let run = Arc::clone(&run);
            let f = Arc::clone(&f);
            Box::pin(async move {
                match run(ctx.clone()).await {
                    Ok(value) => f(value).run_in(ctx).await,
                    Err(error) => Err(error),
                }
            })
        })
    }

    /// Handle an expected failure. Defects and interruption pass through.
    pub fn catch_fail<F>(&self, f: F) -> Self
    where
        F: Fn(E) -> Effect<A, E, S> + Send + Sync + 'static,
    {
        let run = Arc::clone(&self.run);
        let f = Arc::new(f);
        Self::new(move |ctx| {
            let run = Arc::clone(&run);
            let f = Arc::clone(&f);
            Box::pin(async move {
                match run(ctx.clone()).await {
                    Err(Exit::Fail(error)) => f(error).run_in(ctx).await,
                    other => other,
                }
            })
        })
    }

    /// Retry expected failures. Interruption and defects are not retried.
    pub fn retry(&self, schedule: Schedule) -> Self
    where
        E: Clone,
    {
        let run = Arc::clone(&self.run);
        Self::new(move |ctx| {
            let run = Arc::clone(&run);
            let mut schedule = schedule;
            Box::pin(async move {
                loop {
                    if ctx.cancel.is_cancelled() {
                        return Err(Exit::Interrupt);
                    }
                    match run(ctx.clone()).await {
                        Ok(value) => return Ok(value),
                        Err(Exit::Fail(_error)) if schedule.remaining > 0 => {
                            schedule.remaining -= 1;
                            ctx.record("retry", "fail");
                            tokio::select! {
                                _ = ctx.cancel.cancelled() => return Err(Exit::Interrupt),
                                _ = tokio::time::sleep(schedule.delay) => {}
                            }
                        }
                        Err(error) => return Err(error),
                    }
                }
            })
        })
    }

    pub fn timeout(&self, duration: Duration) -> Self {
        let run = Arc::clone(&self.run);
        Self::new(move |ctx| {
            let run = Arc::clone(&run);
            let child = ctx.child();
            Box::pin(async move {
                let child_cancel = child.cancel.clone();
                let fut = run(child);
                match tokio::time::timeout(duration, fut).await {
                    Ok(result) => result,
                    Err(_elapsed) => {
                        child_cancel.cancel();
                        Err(Exit::Interrupt)
                    }
                }
            })
        })
    }

    pub fn with_span(&self, name: impl Into<String>) -> Self {
        let name = name.into();
        let run = Arc::clone(&self.run);
        Self::new(move |ctx| {
            let run = Arc::clone(&run);
            let name = name.clone();
            let fut = run(ctx.clone());
            Box::pin(async move {
                let result = fut.await;
                let outcome = match &result {
                    Ok(_) => "ok",
                    Err(Exit::Fail(_)) => "fail",
                    Err(Exit::Die(_)) => "die",
                    Err(Exit::Interrupt) => "interrupt",
                };
                ctx.record(name, outcome);
                result
            })
        })
    }

    /// Run `left` and `right` together. The first expected failure or defect
    /// cancels the sibling and waits for that sibling to finish.
    pub fn zip_par<B>(&self, right: &Effect<B, E, S>) -> Effect<(A, B), E, S>
    where
        B: Send + Sync + 'static,
    {
        let left_run = Arc::clone(&self.run);
        let right_run = Arc::clone(&right.run);
        Effect::new(move |ctx| {
            let left_run = Arc::clone(&left_run);
            let right_run = Arc::clone(&right_run);
            let left_ctx = ctx.child();
            let right_ctx = ctx.child();
            Box::pin(async move {
                let left = left_run(left_ctx.clone());
                let right = right_run(right_ctx.clone());
                tokio::pin!(left, right);
                tokio::select! {
                    left_result = &mut left => match left_result {
                        Ok(left_value) => match right.await {
                            Ok(right_value) => Ok((left_value, right_value)),
                            Err(error) => Err(error),
                        },
                        Err(error) => {
                            right_ctx.cancel.cancel();
                            let _ = right.await;
                            Err(error)
                        }
                    },
                    right_result = &mut right => match right_result {
                        Ok(right_value) => match left.await {
                            Ok(left_value) => Ok((left_value, right_value)),
                            Err(error) => Err(error),
                        },
                        Err(error) => {
                            left_ctx.cancel.cancel();
                            let _ = left.await;
                            Err(error)
                        }
                    },
                }
            })
        })
    }

    /// First success or failure wins. The other side is cancelled and awaited.
    pub fn race(&self, other: &Self) -> Self {
        let left_run = Arc::clone(&self.run);
        let right_run = Arc::clone(&other.run);
        Self::new(move |ctx| {
            let left_run = Arc::clone(&left_run);
            let right_run = Arc::clone(&right_run);
            let left_ctx = ctx.child();
            let right_ctx = ctx.child();
            Box::pin(async move {
                let left = left_run(left_ctx.clone());
                let right = right_run(right_ctx.clone());
                tokio::pin!(left, right);
                tokio::select! {
                    left_result = &mut left => {
                        right_ctx.cancel.cancel();
                        let _ = right.await;
                        left_result
                    }
                    right_result = &mut right => {
                        left_ctx.cancel.cancel();
                        let _ = left.await;
                        right_result
                    }
                }
            })
        })
    }

    /// Acquire a resource, use it, and release it on success, expected
    /// failure, and cancellation. Dropping the running future releases it too.
    pub fn bracket<B, F, R>(&self, release: R, body: F) -> Effect<B, E, S>
    where
        A: Clone,
        B: Send + Sync + 'static,
        F: Fn(A) -> Effect<B, E, S> + Send + Sync + 'static,
        R: Fn(A) + Send + Sync + 'static,
    {
        let acquire = Arc::clone(&self.run);
        let release: Arc<dyn Fn(A) + Send + Sync> = Arc::new(release);
        let body = Arc::new(body);
        Effect::new(move |ctx| {
            let acquire = Arc::clone(&acquire);
            let release = Arc::clone(&release);
            let body = Arc::clone(&body);
            Box::pin(async move {
                let resource = match acquire(ctx.clone()).await {
                    Ok(resource) => resource,
                    Err(error) => return Err(error),
                };
                let guard = ReleaseOnce::new(resource.clone(), Arc::clone(&release));
                let result = body(resource).run_in(ctx).await;
                guard.release_now();
                result
            })
        })
    }

    /// Replace the service environment. The resulting program no longer
    /// requires `S`; the caller supplies a different environment at the edge.
    pub fn provide<S2>(self, services: S) -> Effect<A, E, S2>
    where
        S2: Send + Sync + 'static,
    {
        let services = Arc::new(services);
        let run = self.run;
        Effect::new(move |ctx| {
            let run = Arc::clone(&run);
            let services = Arc::clone(&services);
            let ctx = RunCtx {
                services,
                cancel: ctx.cancel,
                trace: ctx.trace,
            };
            run(ctx)
        })
    }

    pub async fn run(self, services: Arc<S>) -> (Result<A, Exit<E>>, Vec<TraceEvent>) {
        let trace = Arc::new(Mutex::new(Vec::new()));
        let ctx = RunCtx {
            services,
            cancel: CancellationToken::new(),
            trace: Arc::clone(&trace),
        };
        let result = self.run_in(ctx).await;
        let events = trace
            .lock()
            .map(|events| events.clone())
            .unwrap_or_default();
        (result, events)
    }

    pub(crate) async fn run_in(&self, ctx: RunCtx<S>) -> Result<A, Exit<E>> {
        (self.run)(ctx).await
    }
}

#[derive(Debug, Clone, Copy)]
pub struct Schedule {
    pub remaining: u32,
    pub delay: Duration,
}

struct ReleaseOnce<T> {
    value: Mutex<Option<T>>,
    release: Arc<dyn Fn(T) + Send + Sync>,
}

impl<T> ReleaseOnce<T> {
    fn new(value: T, release: Arc<dyn Fn(T) + Send + Sync>) -> Self {
        Self {
            value: Mutex::new(Some(value)),
            release,
        }
    }

    fn release_now(&self) {
        if let Ok(mut slot) = self.value.lock() {
            if let Some(value) = slot.take() {
                (self.release)(value);
            }
        }
    }
}

impl<T> Drop for ReleaseOnce<T> {
    fn drop(&mut self) {
        self.release_now();
    }
}