statifier 0.0.0

An SCXML statechart interpreter core, a conformant sibling of the Elixir statifier package.
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
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
//! The in-memory driver: what a host calls to execute a chart.
//!
//! The interpreter takes one external event at a time and answers its
//! effects as data; the driver is the small loop around it that a session
//! process is in the reference (`Statifier.Session` and
//! `Statifier.Session.Effects` in statifier-ex), with no process, no thread,
//! no real clock and no I/O. It keeps the external queue, routes every
//! `<send>` of the SCXML Event I/O Processor as the reference's session
//! routes it, keeps each delayed one as a pending timer on a virtual clock,
//! and answers every send of a type the host registered as an effect for
//! the host to deliver: it never calls host code.
//!
//! Every call takes a [`State`] by reference and answers a new one with the
//! effects the call produced, in the order they were produced ([`Moved`]);
//! the state it was given is unchanged.
//!
//! Routing, as the reference's session routes a send of the SCXML Event I/O
//! Processor: no target, and the session's own `#_scxml_` address, join the
//! session's external queue; `#_internal` is delivered onto the internal
//! queue and the chart runs to a stable configuration again, after the
//! effects of the batch that sent it, as the reference defers it.
//! `#_parent` from an invoked child, and `#_` followed by the id of a live
//! invocation, deliver as external events (below). Another session's
//! address, a parent a session does not have and an invocation that is not
//! live are unreachable: the interpreter refuses an immediate send to one
//! with `error.communication`, and a delayed one raises the same event when
//! its timer fires.
//!
//! Invocations, as the reference's session and its built-in `scxml`
//! handler run them (`Statifier.Session`, `Statifier.Session.Invocations`
//! and `Statifier.Invoke.Source` in statifier-ex), with a tree of session
//! states in place of processes. An [`Effect::Invoke`] of the SCXML type
//! whose content is markup compiles it, a root with no namespace accepted
//! as inline content is read, and starts it as a child session. Its session
//! id is its parent's, a dot and the invoke id; its datamodel is seeded with
//! the params a root `<data>` of the child names, the rest dropped; it
//! inherits its parent's registered send types, and starts with the
//! default round budget and no trace, as the reference starts a child its
//! parent passes neither. A content that is not markup or does not compile, and an
//! invocation with no content (`src` is never fetched), raise
//! `error.communication`; any other type raises `error.execution`. A child
//! runs inside its parent's call on its parent's virtual clock: starting
//! it, an event sent to it and an event forwarded to it
//! ([`Effect::Autoforward`]) each run it to a stable configuration at once,
//! and its timers fire with its parent's, in due-then-scheduled order
//! across the tree. Every effect a child answers is among the call's
//! effects, in the order made, as an [`Effect::Child`] naming its session
//! id.
//!
//! What a child sends its parent (an event to `#_parent`, carrying the
//! invoke id; a notice that it reached a top-level final, ahead of the
//! batch that stops it; and its `done.invoke.` event with its donedata)
//! waits in the parent's mailbox, taken one entry at a time once the
//! parent's external queue is empty. An entry whose invocation is no longer
//! live is discarded, and the done event retires its invocation once taken.
//! An [`Effect::CancelInvoke`] retires the invocation and stops its child,
//! running its `<onexit>` handlers, unless the child already said it
//! completed; a cancelled child returns no done event.
//!
//! The virtual clock starts at 0. A delayed send becomes a pending timer due
//! at the clock's time plus its delay; [`State::advance`] moves the clock and
//! fires every timer due by the new time, earliest first and, at one due
//! time, in the order they were scheduled, each run to completion before the
//! next. A `<cancel>` removes every pending timer under its send id. When the
//! chart stops, its pending timers and its queued external events are
//! discarded.
//!
//! A macrostep that spends its round budget halts the driver as the
//! reference halts its session: the chart stays where the macrostep left it
//! and is never run again. Later external events are queued and not taken.
//! A pending timer still fires: a send to the session itself joins the
//! external queue, untaken, and a send to the internal queue or to a target
//! the driver cannot reach is dropped, with nothing run and nothing raised,
//! as the reference's halted session drops a fired delayed send.
//!
//! One call also spends the budget on three counts the reference's
//! session, which never returns, has no need of, each kept at one virtual
//! time and started over whenever [`State::advance`] moves the clock on:
//! the events it takes that an immediate send to the session itself queued
//! in the same call, the immediate sends it delivers onto the internal
//! queue, and the timers it fires that were scheduled at that same virtual
//! time in the same call. A call that would count one more than the budget
//! on any of them halts the same way, answering [`Effect::BudgetExhausted`].
//! Under a bounded budget every call therefore returns, a chart that keeps
//! rescheduling a timer due at once included, while timers with a delay, a
//! chain or a fan-out, are bounded by the duration advanced and never halt
//! on these counts, however the host splits time. Under
//! [`RoundBudget::Unbounded`] no count ever halts the driver, and such a
//! call may never return. A timer scheduled by an earlier call, or at an
//! earlier virtual time, is never counted.

mod invocations;
mod position;
#[cfg(test)]
mod tests;

pub use position::{ActiveInvocation, ExportError, ImportError, Position, Status};

use crate::datamodel::system_variables::{EventType, SCXML_EVENT_PROCESSOR, scxml_location};
use crate::effect::Effect;
use crate::interpreter::{self, Event, MachineState, Route, Routes, StartOptions};
use crate::machine::Machine;
use predicator_rs::{Map, Value};
use std::collections::{BTreeMap, BTreeSet, VecDeque};
use std::fmt;
use std::num::NonZeroU64;

/// The round budget when the host sets none, the reference's
/// (`max_macrostep_rounds` in `Statifier.MachineState`).
const DEFAULT_ROUNDS: u64 = 10_000;

/// The most rounds one macrostep may take.
///
/// The interpreter's loop runs a macrostep until its configuration is
/// stable; a chart whose eventless transitions or raised events never let
/// it settle would run forever, so each macrostep spends at most this many
/// rounds and then stops where the last round left it, answering
/// [`Effect::BudgetExhausted`].
#[non_exhaustive]
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
pub enum RoundBudget {
    /// At most this many rounds.
    Rounds(NonZeroU64),
    /// No bound: a macrostep that never reaches a stable configuration
    /// never returns, as the specification's loop never does, and nor does
    /// the call that started it.
    Unbounded,
}

impl Default for RoundBudget {
    /// 10000 rounds, the reference's budget.
    fn default() -> Self {
        RoundBudget::Rounds(NonZeroU64::new(DEFAULT_ROUNDS).unwrap_or(NonZeroU64::MIN))
    }
}

impl RoundBudget {
    fn interpreter(self) -> interpreter::RoundBudget {
        match self {
            RoundBudget::Rounds(rounds) => interpreter::RoundBudget::Rounds(rounds.get()),
            RoundBudget::Unbounded => interpreter::RoundBudget::Unbounded,
        }
    }

    /// Whether `count` is within the budget: always, for no bound.
    fn admits(self, count: u64) -> bool {
        match self {
            RoundBudget::Rounds(rounds) => count <= rounds.get(),
            RoundBudget::Unbounded => true,
        }
    }

    /// The budget as [`Effect::BudgetExhausted`] carries it; only a bounded
    /// budget is ever spent.
    fn spent(self) -> u64 {
        match self {
            RoundBudget::Rounds(rounds) => rounds.get(),
            RoundBudget::Unbounded => u64::MAX,
        }
    }
}

/// What [`Machine::start`] takes.
///
/// The struct is `#[non_exhaustive]`: a host builds one with
/// [`Options::new`] and sets the fields it wants.
///
/// ```
/// use statifier::{Map, Options, Value};
///
/// let mut options = Options::new("session-1");
/// let mut datamodel = Map::new();
/// datamodel.insert("limit", Value::from(3));
/// options.datamodel = datamodel;
/// options.send_types = ["x-example"].into_iter().map(String::from).collect();
/// # let _ = options;
/// ```
#[non_exhaustive]
#[derive(Debug, Clone, PartialEq)]
pub struct Options {
    /// The session's id, minted by the host: `_sessionid` reads it, and
    /// `#_scxml_` followed by it is the session's own address.
    pub session_id: String,
    /// Starting datamodel values, under the system variables. A root
    /// `<data>` whose id is here keeps the host's value.
    pub datamodel: Map,
    /// The send types the host delivers itself. A `<send>` of one of them
    /// is answered as an effect and never routed or scheduled here, delayed
    /// or not; `_ioprocessors` holds an empty entry for each. A built-in
    /// spelling, `scxml` or the SCXML Event I/O Processor's URI, is refused.
    pub send_types: BTreeSet<String>,
    /// The round budget of each macrostep, and of the two counts one call
    /// spends it on.
    pub round_budget: RoundBudget,
    /// Whether the chart answers trace effects. This version emits none,
    /// whatever the flag says; the state keeps it, and a position carries
    /// it.
    pub trace: bool,
}

impl Options {
    /// The options for a session with this id, and every other field at its
    /// default: no starting values, no registered send type, the default
    /// round budget and no trace.
    pub fn new(session_id: impl Into<String>) -> Self {
        Options {
            session_id: session_id.into(),
            datamodel: Map::new(),
            send_types: BTreeSet::new(),
            round_budget: RoundBudget::default(),
            trace: false,
        }
    }
}

/// Why a driver call was refused.
#[non_exhaustive]
#[derive(Debug, Clone, PartialEq, Eq)]
pub enum Error {
    /// The chart has stopped: it takes no more events.
    NotRunning,
    /// [`State::advance`] was given a duration that moves the virtual clock
    /// past its largest value.
    #[non_exhaustive]
    InvalidDuration {
        /// The duration given, in milliseconds.
        ms: u64,
    },
    /// [`Options::send_types`] names a built-in type, which is never handed
    /// to a host.
    #[non_exhaustive]
    BuiltInSendTypes {
        /// The built-in spellings named, sorted.
        types: Vec<String>,
    },
}

impl fmt::Display for Error {
    fn fmt(&self, formatter: &mut fmt::Formatter<'_>) -> fmt::Result {
        match self {
            Error::NotRunning => formatter.write_str("the chart has stopped"),
            Error::InvalidDuration { ms } => write!(
                formatter,
                "a duration of {ms} ms moves the clock past its largest value"
            ),
            Error::BuiltInSendTypes { types } => write!(
                formatter,
                "built-in send types cannot be registered: {}",
                types.join(", ")
            ),
        }
    }
}

impl std::error::Error for Error {}

/// What a call that moves a chart answers: the new state, and the effects
/// the call produced, in the order they were produced.
#[non_exhaustive]
#[derive(Debug, Clone)]
pub struct Moved {
    /// The new state.
    pub state: State,
    /// The effects, in order.
    pub effects: Vec<Effect>,
}

/// What a stopped chart answers ([`State::done`]).
#[non_exhaustive]
#[derive(Debug, Clone, PartialEq)]
pub struct Done {
    /// The top-level final state's `<donedata>`: [`Value::Undefined`] when
    /// it has none, or when evaluating it failed.
    pub donedata: Value,
    /// The ids of the states that were active when the chart stopped, root
    /// excluded, sorted; a state with no id is left out.
    pub configuration: Vec<String>,
    /// The ids of the leaf states of the configuration the chart stopped
    /// in, the states in it with no child state, sorted; a state with no id
    /// is left out. A stopped chart has no active state, so
    /// [`State::active_leaf_states`] answers none; this is the set the
    /// reference's conformance harness compares once it restores the
    /// configuration its done effect carries.
    pub active_leaf_states: Vec<String>,
    /// How many leaf states the configuration the chart stopped in holds,
    /// a state with no id counted too: more than
    /// [`active_leaf_states`](Done::active_leaf_states) holds when a leaf it
    /// stopped in has no id. It is [`State::active_leaf_count`] for a
    /// stopped chart.
    pub active_leaf_count: usize,
}

/// A delayed send waiting on the virtual clock.
#[derive(Debug, Clone)]
struct Timer {
    /// The send id a `<cancel>` names it by.
    send_id: String,
    /// The virtual time it fires at, in milliseconds.
    due_ms: u64,
    /// The order it was scheduled in: two timers due together fire in this
    /// order.
    sequence: u64,
    /// The send it delivers.
    send: Delivery,
}

/// What the driver keeps of a send to route it: the target it names and
/// the event a delivery carries.
#[derive(Debug, Clone)]
struct Delivery {
    target: Option<Value>,
    event: Event,
}

/// A chart in execution: an immutable value, as cheap to clone as its
/// datamodel. Each call that moves it answers a new state and leaves this
/// one unchanged.
///
/// ```
/// use statifier::{Options, Value};
///
/// let machine = statifier::compile(
///     r#"<scxml xmlns="http://www.w3.org/2005/07/scxml" version="1.0" initial="a">
///          <state id="a"><transition event="e.go" target="b"/></state>
///          <final id="b"/>
///        </scxml>"#,
/// )
/// .map_err(|refusals| format!("{refusals:?}"))?;
/// let started = machine.start(Options::new("s-1"))?;
/// assert_eq!(started.state.configuration(), ["a"]);
///
/// let moved = started.state.step("e.go", Value::Undefined)?;
/// assert_eq!(started.state.configuration(), ["a"]);
/// assert_eq!(moved.state.done().map(|done| done.configuration.clone()), Some(vec!["b".to_owned()]));
/// # Ok::<(), Box<dyn std::error::Error>>(())
/// ```
#[derive(Debug, Clone)]
pub struct State {
    core: MachineState,
    session_id: String,
    round_budget: RoundBudget,
    trace: bool,
    now_ms: u64,
    timers: Vec<Timer>,
    sequence: u64,
    external_queue: VecDeque<Event>,
    halted: bool,
    done: Option<Done>,
    /// The live invocations' child sessions, by invoke id.
    invocations: BTreeMap<String, Invocation>,
    /// What the session's children sent it and it has not taken.
    mailbox: VecDeque<Mail>,
    /// The invoke id this session was started under, `None` for a session
    /// the host started.
    invoked_as: Option<String>,
    /// Whether the session's parent cancelled it.
    cancelled: bool,
}

/// A live invocation of a child session, as its parent holds it.
#[derive(Debug, Clone)]
struct Invocation {
    /// Whether the child said it reached a top-level final.
    completed: bool,
    /// The child's state; its clock and timer sequence are its parent's as
    /// they stood when it last ran.
    child: State,
}

/// What a child sent its parent and the parent has not taken.
#[derive(Debug, Clone)]
enum Mail {
    /// The child reached a top-level final.
    Completed { invoke_id: String },
    /// An event the child sent to `#_parent`.
    Event { invoke_id: String, event: Event },
    /// The child stopped on its own, with this donedata.
    Done { invoke_id: String, donedata: Value },
}

impl Mail {
    /// The invocation it came from.
    fn invoke_id(&self) -> &str {
        match self {
            Mail::Completed { invoke_id }
            | Mail::Event { invoke_id, .. }
            | Mail::Done { invoke_id, .. } => invoke_id,
        }
    }
}

impl State {
    /// A session over a core that has just started, at virtual time
    /// `now_ms` with the timer sequence at `sequence`.
    fn fresh(
        core: MachineState,
        session_id: String,
        round_budget: RoundBudget,
        trace: bool,
    ) -> Self {
        State {
            core,
            session_id,
            round_budget,
            trace,
            now_ms: 0,
            timers: Vec::new(),
            sequence: 0,
            external_queue: VecDeque::new(),
            halted: false,
            done: None,
            invocations: BTreeMap::new(),
            mailbox: VecDeque::new(),
            invoked_as: None,
            cancelled: false,
        }
    }
}

impl Machine {
    /// Starts the chart: its datamodel bound, its `<scxml>` scripts run, its
    /// initial states entered, and the whole run to a stable configuration,
    /// with every event it sent itself taken.
    ///
    /// Refused with [`Error::BuiltInSendTypes`] when `options` registers a
    /// built-in send type.
    pub fn start(&self, options: Options) -> Result<Moved, Error> {
        let built_in: Vec<String> = options
            .send_types
            .iter()
            .filter(|send_type| is_built_in(send_type))
            .cloned()
            .collect();
        if !built_in.is_empty() {
            return Err(Error::BuiltInSendTypes { types: built_in });
        }
        let ioprocessors = options
            .send_types
            .iter()
            .map(|send_type| (send_type.as_str(), Value::Map(Map::new())))
            .collect();
        let routes = Routes {
            sessions: BTreeSet::from([options.session_id.clone()]),
            ..Routes::default()
        };
        let started = interpreter::initialize(
            self,
            StartOptions {
                session_id: options.session_id.clone(),
                datamodel: options.datamodel,
                send_types: options.send_types,
                ioprocessors,
                routes: Some(routes),
                max_macrostep_rounds: options.round_budget.interpreter(),
            },
        );
        let mut call = Call::open(State::fresh(
            started.state,
            options.session_id,
            options.round_budget,
            options.trace,
        ));
        call.perform(started.effects);
        call.drain();
        Ok(call.close())
    }
}

impl State {
    /// Sends the chart one external event named `name`, carrying `data`
    /// ([`Value::Undefined`] for none), and runs it, and every event it
    /// sends itself, to a stable configuration. A driver that a spent
    /// budget halted queues the event without taking it.
    ///
    /// Refused with [`Error::NotRunning`] once the chart has stopped.
    pub fn step(&self, name: &str, data: Value) -> Result<Moved, Error> {
        if !self.core.running {
            return Err(Error::NotRunning);
        }
        let mut call = Call::open(self.clone());
        call.external.push_back(Queued {
            event: Event {
                name: name.to_owned(),
                event_type: EventType::External,
                sendid: None,
                origin: None,
                origintype: None,
                invokeid: None,
                data,
            },
            counted: false,
        });
        call.drain();
        Ok(call.close())
    }

    /// Moves the virtual clock forward `ms` milliseconds, firing every
    /// pending timer due by then, earliest first and, at one due time, in
    /// the order they were scheduled, each run to completion before the
    /// next. A timer an invoked child scheduled fires the same way, in the
    /// child. A stopped chart has no timer, so only its clock moves.
    ///
    /// Refused with [`Error::InvalidDuration`] when the clock would pass its
    /// largest value.
    pub fn advance(&self, ms: u64) -> Result<Moved, Error> {
        let until = self
            .now_ms
            .checked_add(ms)
            .ok_or(Error::InvalidDuration { ms })?;
        let mut call = Call::open(self.clone());
        while let Some((path, due_ms, sequence)) = call.next_due(until) {
            if due_ms > call.state.now_ms {
                call.next_instant();
            }
            call.state.now_ms = due_ms;
            call.fire_at(&path, sequence);
        }
        call.state.now_ms = until;
        Ok(call.close())
    }

    /// The ids of the active states, root excluded, sorted; a state with no
    /// id is left out. A stopped chart has none.
    pub fn configuration(&self) -> Vec<String> {
        ids(&self.core, self.core.configuration.iter().copied())
    }

    /// The ids of the active leaf states, the active states with no child
    /// state, sorted; a state with no id is left out. It is the reference's
    /// `Statifier.active_leaf_states/1`, the set its conformance harness
    /// compares with a case's expected configuration. A final state is a
    /// leaf, and a history state is never active.
    ///
    /// A stopped chart has no active state, so it answers none, as the
    /// reference does; the leaves it stopped in are
    /// [`Done::active_leaf_states`].
    ///
    /// ```
    /// use statifier::{Options, Value};
    ///
    /// let machine = statifier::compile(
    ///     r#"<scxml xmlns="http://www.w3.org/2005/07/scxml" version="1.0" initial="p">
    ///          <parallel id="p">
    ///            <state id="a"><transition event="e.go" target="f"/></state>
    ///            <state id="b"/>
    ///          </parallel>
    ///          <final id="f"/>
    ///        </scxml>"#,
    /// )
    /// .map_err(|refusals| format!("{refusals:?}"))?;
    /// let started = machine.start(Options::new("s-1"))?;
    /// assert_eq!(started.state.configuration(), ["a", "b", "p"]);
    /// assert_eq!(started.state.active_leaf_states(), ["a", "b"]);
    ///
    /// let stopped = started.state.step("e.go", Value::Undefined)?;
    /// assert!(stopped.state.configuration().is_empty());
    /// assert!(stopped.state.active_leaf_states().is_empty());
    /// let done = stopped.state.done().ok_or("the chart has stopped")?;
    /// assert_eq!(done.active_leaf_states, ["f"]);
    /// # Ok::<(), Box<dyn std::error::Error>>(())
    /// ```
    pub fn active_leaf_states(&self) -> Vec<String> {
        leaf_ids(&self.core, self.core.configuration.iter().copied())
    }

    /// How many active leaf states there are, a state with no id counted
    /// too: more than [`active_leaf_states`](State::active_leaf_states)
    /// holds when an active leaf has no id, which no expected configuration
    /// can name. The reference's conformance harness compares the two
    /// counts so that an anonymous active leaf fails a case rather than
    /// passing unseen. A stopped chart has none; the leaves it stopped in
    /// are counted in [`Done::active_leaf_count`].
    ///
    /// ```
    /// use statifier::Options;
    ///
    /// let machine = statifier::compile(
    ///     r#"<scxml xmlns="http://www.w3.org/2005/07/scxml" version="1.0" initial="p">
    ///          <parallel id="p">
    ///            <state id="a"/>
    ///            <state/>
    ///          </parallel>
    ///        </scxml>"#,
    /// )
    /// .map_err(|refusals| format!("{refusals:?}"))?;
    /// let started = machine.start(Options::new("s-1"))?;
    /// assert_eq!(started.state.active_leaf_states(), ["a"]);
    /// assert_eq!(started.state.active_leaf_count(), 2);
    /// # Ok::<(), Box<dyn std::error::Error>>(())
    /// ```
    pub fn active_leaf_count(&self) -> usize {
        leaf_count(&self.core, self.core.configuration.iter().copied())
    }

    /// `None` while the chart runs; once it has stopped, its donedata, the
    /// configuration it stopped in and that configuration's leaf states.
    pub fn done(&self) -> Option<&Done> {
        self.done.as_ref()
    }

    /// The virtual clock, in milliseconds since the chart started.
    pub fn now_ms(&self) -> u64 {
        self.now_ms
    }

    /// The virtual time the earliest pending timer fires at, an invoked
    /// child's included, `None` when no timer is pending.
    pub fn next_due_ms(&self) -> Option<u64> {
        self.timers
            .iter()
            .map(|timer| timer.due_ms)
            .chain(
                self.invocations
                    .values()
                    .filter_map(|invocation| invocation.child.next_due_ms()),
            )
            .min()
    }
}

/// Whether `send_type` is a built-in spelling of the SCXML Event I/O
/// Processor, which a host never registers.
fn is_built_in(send_type: &str) -> bool {
    send_type == "scxml" || send_type == SCXML_EVENT_PROCESSOR
}

/// The ids of `indexes`, sorted; a state with no id, the root among them,
/// is left out.
fn ids(core: &MachineState, indexes: impl Iterator<Item = usize>) -> Vec<String> {
    let chart = core.chart();
    let mut ids: Vec<String> = indexes
        .filter_map(|index| chart.states.get(index).and_then(|state| state.id.clone()))
        .collect();
    ids.sort();
    ids
}

/// The ids of the atomic states among `indexes`, sorted; a state with no id
/// is left out.
fn leaf_ids(core: &MachineState, indexes: impl Iterator<Item = usize>) -> Vec<String> {
    let chart = core.chart();
    ids(
        core,
        indexes.filter(|&index| index < chart.states.len() && chart.is_atomic(index)),
    )
}

/// How many of `indexes` are atomic states, a state with no id counted too.
fn leaf_count(core: &MachineState, indexes: impl Iterator<Item = usize>) -> usize {
    let chart = core.chart();
    indexes
        .filter(|&index| index < chart.states.len() && chart.is_atomic(index))
        .count()
}

/// An external event waiting, and whether this call counts it against the
/// budget when it takes it.
#[derive(Debug)]
struct Queued {
    event: Event,
    counted: bool,
}

/// One call's working form: the state being moved, the effects answered so
/// far, and the call's own counts.
struct Call {
    state: State,
    external: VecDeque<Queued>,
    /// The mailbox, each entry with whether this call counts it when it
    /// takes it.
    mailbox: VecDeque<(Mail, bool)>,
    /// What this session sent its parent in this call, each with whether
    /// the parent counts it.
    outbox: Vec<(Mail, bool)>,
    /// The external event the call is taking, which an autoforward
    /// delivers.
    taking: Option<Event>,
    effects: Vec<Effect>,
    /// Counted events taken in this call at the current virtual time.
    taken: u64,
    /// Immediate internal sends delivered in this call at the current
    /// virtual time.
    sent_internal: u64,
    /// Whether this call has spent its budget: nothing more runs the chart.
    spent: bool,
    /// The sequence of the first timer this call schedules at the current
    /// virtual time: a timer at or past it was scheduled at this time in
    /// this call, as a host that advanced only to this time would see it.
    first_sequence: u64,
    /// Timers scheduled in this call that it fired at the current virtual
    /// time.
    fired: u64,
}

impl Call {
    /// A call over `state`: every event already queued is one an earlier
    /// call queued, and is not counted.
    fn open(mut state: State) -> Self {
        let first_sequence = state.sequence;
        let external = std::mem::take(&mut state.external_queue)
            .into_iter()
            .map(|event| Queued {
                event,
                counted: false,
            })
            .collect();
        let mailbox = std::mem::take(&mut state.mailbox)
            .into_iter()
            .map(|mail| (mail, false))
            .collect();
        Call {
            state,
            external,
            mailbox,
            outbox: Vec::new(),
            taking: None,
            effects: Vec::new(),
            taken: 0,
            sent_internal: 0,
            spent: false,
            first_sequence,
            fired: 0,
        }
    }

    /// The state the call leaves, and its effects.
    fn close(self) -> Moved {
        let (state, effects, _) = self.finish();
        Moved { state, effects }
    }

    /// The state the call leaves, its effects, and what it sent its parent.
    fn finish(mut self) -> (State, Vec<Effect>, Vec<(Mail, bool)>) {
        self.state.external_queue = self.external.into_iter().map(|q| q.event).collect();
        self.state.mailbox = self.mailbox.into_iter().map(|(mail, _)| mail).collect();
        (self.state, self.effects, self.outbox)
    }

    /// The earliest timer due by `until` anywhere in the tree of sessions,
    /// the first scheduled among equals: the invoke ids from this session
    /// down to the one holding it, its due time and its sequence.
    fn next_due(&self, until: u64) -> Option<(Vec<String>, u64, u64)> {
        let mut pending = Vec::new();
        pending_timers(&self.state, &mut Vec::new(), &mut pending);
        pending
            .into_iter()
            .filter(|(_, due_ms, _)| *due_ms <= until)
            .min_by_key(|(_, due_ms, sequence)| (*due_ms, *sequence))
    }

    /// Fires the timer scheduled at `sequence` in the session `path` names,
    /// each session on the way drained after it.
    fn fire_at(&mut self, path: &[String], sequence: u64) {
        let Some((invoke_id, rest)) = path.split_first() else {
            let Some(position) = self
                .state
                .timers
                .iter()
                .position(|timer| timer.sequence == sequence)
            else {
                return;
            };
            let timer = self.state.timers.remove(position);
            self.count_fire(&timer);
            let effects = self.route(timer.send, false);
            self.perform(effects);
            self.drain();
            return;
        };
        self.within(invoke_id, |child| child.fire_at(rest, sequence));
        self.drain();
    }

    /// Takes the external queue one event per macrostep while the chart
    /// runs and the driver is not halted, and, once it is empty, the
    /// mailbox one entry at a time. A counted entry the budget has no room
    /// for is left where it waits, and the driver halts.
    fn drain(&mut self) {
        while self.state.core.running && !self.state.halted {
            if let Some(queued) = self.external.pop_front() {
                if queued.counted && !self.count_taken() {
                    self.external.push_front(queued);
                    self.halt();
                    return;
                }
                self.take(queued.event);
                continue;
            }
            let Some((mail, counted)) = self.mailbox.pop_front() else {
                return;
            };
            if counted && !self.count_taken() {
                self.mailbox.push_front((mail, counted));
                self.halt();
                return;
            }
            self.take_mail(mail);
        }
    }

    /// Counts one more counted entry taken, answering whether the budget
    /// had room for it.
    fn count_taken(&mut self) -> bool {
        let taken = self.taken.saturating_add(1);
        if !self.state.round_budget.admits(taken) {
            return false;
        }
        self.taken = taken;
        true
    }

    /// One external event, one macrostep, with the routes the session can
    /// reach declared to the interpreter as the event arrives.
    fn take(&mut self, event: Event) {
        self.stamp();
        if let Ok(stepped) = interpreter::handle_event(&self.state.core, &event) {
            self.state.core = stepped.state;
            self.taking = Some(event);
            self.perform(stepped.effects);
            self.taking = None;
        }
    }

    /// The routes the session can reach now, declared to the interpreter:
    /// itself, its parent when it has one, and its live invocations.
    fn stamp(&mut self) {
        self.state.core.routes = Some(Routes {
            sessions: BTreeSet::from([self.state.session_id.clone()]),
            parent: self.state.invoked_as.is_some(),
            invokes: self.state.invocations.keys().cloned().collect(),
        });
    }

    /// The clock has moved on to a later virtual time: the call's three
    /// counts start over, so each bounds only what happens at one time, and
    /// a chain of timers with a delay is bounded by the duration advanced.
    /// A timer scheduled at an earlier time is no longer counted when it
    /// fires, as a call that started at this time would not count it.
    fn next_instant(&mut self) {
        self.first_sequence = self.state.sequence;
        self.taken = 0;
        self.sent_internal = 0;
        self.fired = 0;
    }

    /// Counts a timer scheduled at the current virtual time in this call
    /// against the budget as it fires. Halts the driver when the budget has
    /// no room for it; a halted driver runs nothing, so its fires are not
    /// counted.
    fn count_fire(&mut self, timer: &Timer) {
        if self.state.halted || timer.sequence < self.first_sequence {
            return;
        }
        self.fired = self.fired.saturating_add(1);
        if !self.state.round_budget.admits(self.fired) {
            self.halt();
        }
    }

    /// Halts the driver for the call's budget, answering the spent-budget
    /// effect unless a macrostep already halted it.
    fn halt(&mut self) {
        self.spent = true;
        if self.state.halted {
            return;
        }
        let budget = self.state.round_budget.spent();
        self.perform(vec![interpreter::budget_exhausted(
            &self.state.core,
            budget,
        )]);
    }

    /// Acts on a batch of effects in order, each answered as it is acted
    /// on. A delivery onto the internal queue runs the chart at once, and
    /// the effects that run answers are acted on after the batch, as the
    /// reference defers them.
    fn perform(&mut self, effects: Vec<Effect>) {
        let mut batch = effects;
        while !batch.is_empty() {
            self.announce(&batch);
            let mut deferred = Vec::new();
            for effect in batch {
                self.effects.push(self.report(effect.clone()));
                deferred.extend(self.perform_one(&effect));
            }
            batch = deferred;
        }
    }

    /// `effect` as the call answers it: an invoked child's wrapped in an
    /// [`Effect::Child`] naming the child.
    fn report(&self, effect: Effect) -> Effect {
        match self.state.invoked_as {
            Some(_) => Effect::Child {
                session_id: self.state.session_id.clone(),
                effect: Box::new(effect),
            },
            None => effect,
        }
    }

    fn perform_one(&mut self, effect: &Effect) -> Vec<Effect> {
        match effect {
            Effect::Send {
                event,
                target,
                send_type,
                data,
                send_id,
                id_from_author,
                ..
            } if send_type.is_none() || !self.registered(send_type.as_ref()) => {
                let delivery = self.delivery(event, target, data, send_id, *id_from_author);
                self.route(delivery, true)
            }
            Effect::SendDelayed {
                event,
                target,
                send_type,
                data,
                send_id,
                id_from_author,
                delay_ms,
                ..
            } if send_type.is_none() || !self.registered(send_type.as_ref()) => {
                let send = self.delivery(event, target, data, send_id, *id_from_author);
                let delay = u64::try_from(*delay_ms).unwrap_or(0);
                self.state.timers.push(Timer {
                    send_id: send_id.clone(),
                    due_ms: self.state.now_ms.saturating_add(delay),
                    sequence: self.state.sequence,
                    send,
                });
                self.state.sequence = self.state.sequence.saturating_add(1);
                Vec::new()
            }
            Effect::Cancel { send_id, .. } => {
                if let Value::String(send_id) = send_id {
                    self.state.timers.retain(|timer| timer.send_id != *send_id);
                }
                Vec::new()
            }
            Effect::Done {
                donedata,
                configuration,
                ..
            } => {
                self.state.done = Some(Done {
                    donedata: donedata.clone().unwrap_or(Value::Undefined),
                    configuration: ids(&self.state.core, configuration.iter().copied()),
                    active_leaf_states: leaf_ids(&self.state.core, configuration.iter().copied()),
                    active_leaf_count: leaf_count(&self.state.core, configuration.iter().copied()),
                });
                self.state.timers.clear();
                self.external.clear();
                self.mailbox.clear();
                self.state.invocations.clear();
                self.return_done(donedata.clone().unwrap_or(Value::Undefined));
                Vec::new()
            }
            Effect::Invoke {
                invoke_id,
                invoke_type,
                params,
                content,
                ..
            } => self.start_child(invoke_id, invoke_type.as_ref(), params, content.as_ref()),
            Effect::CancelInvoke { invoke_id, .. } => {
                self.cancel_child(invoke_id);
                Vec::new()
            }
            Effect::Autoforward { invoke_id, .. } => {
                if let Some(event) = self.taking.clone() {
                    self.deliver(invoke_id, event);
                }
                Vec::new()
            }
            Effect::BudgetExhausted { .. } => {
                self.state.halted = true;
                Vec::new()
            }
            _ => Vec::new(),
        }
    }

    /// Whether `send_type` names a type the host registered.
    fn registered(&self, send_type: Option<&Value>) -> bool {
        matches!(send_type, Some(Value::String(send_type)) if self.state.core.send_types.contains(send_type))
    }

    /// The event a delivered send carries, the reference's
    /// `Statifier.Send.Event.build/3`: from the session's own address through
    /// the SCXML Event I/O Processor, with the send id only when the
    /// document named it.
    fn delivery(
        &self,
        event: &Option<Value>,
        target: &Option<Value>,
        data: &Value,
        send_id: &str,
        id_from_author: bool,
    ) -> Delivery {
        let name = match event {
            Some(Value::String(name)) => name.clone(),
            _ => String::new(),
        };
        Delivery {
            target: target.clone(),
            event: Event {
                name,
                event_type: EventType::External,
                sendid: id_from_author.then(|| send_id.to_owned()),
                origin: Some(scxml_location(&self.state.session_id)),
                origintype: Some(SCXML_EVENT_PROCESSOR.to_owned()),
                invokeid: None,
                data: data.clone(),
            },
        }
    }

    /// Routes a send of the SCXML Event I/O Processor; `immediate` when it
    /// was sent with no delay, which counts what it queues for the session
    /// itself and what it delivers onto the internal queue.
    fn route(&mut self, send: Delivery, immediate: bool) -> Vec<Effect> {
        match interpreter::parse_target(send.target.as_ref()) {
            Route::Own => self.enqueue_own(send.event, immediate),
            Route::Session(id) if id == self.state.session_id => {
                self.enqueue_own(send.event, immediate)
            }
            Route::Parent if self.state.invoked_as.is_some() => {
                if !self.state.halted || immediate {
                    self.post_to_parent(send.event, immediate);
                }
                Vec::new()
            }
            Route::Invoke(id) if self.state.invocations.contains_key(&id) => {
                if !self.state.halted || immediate {
                    self.deliver(&id, send.event);
                }
                Vec::new()
            }
            Route::Internal => {
                let mut event = send.event;
                event.event_type = EventType::Internal;
                event.origin = None;
                event.origintype = None;
                if !immediate {
                    return self.fired_internal(event);
                }
                self.sent_internal = self.sent_internal.saturating_add(1);
                if self.state.round_budget.admits(self.sent_internal) {
                    self.deliver_internal(event)
                } else {
                    self.spend(event)
                }
            }
            _ => {
                let mut event = Event::platform("error.communication", Value::Undefined);
                event.sendid = send.event.sendid;
                self.fired_internal(event)
            }
        }
    }

    /// A fired timer's delivery onto the internal queue, its own event or
    /// the `error.communication` an unreachable target raises: dropped by a
    /// halted driver, with nothing run and nothing raised, as the
    /// reference's halted session drops a fired delayed send; delivered
    /// otherwise.
    fn fired_internal(&mut self, event: Event) -> Vec<Effect> {
        if self.state.halted {
            return Vec::new();
        }
        self.deliver_internal(event)
    }

    /// An event for the session itself joins its external queue, counted
    /// when an immediate send queued it.
    fn enqueue_own(&mut self, event: Event, counted: bool) -> Vec<Effect> {
        self.external.push_back(Queued { event, counted });
        Vec::new()
    }

    /// The reference's `Interpreter.deliver_internal/5`: the event joins the
    /// internal queue and the chart runs to a stable configuration,
    /// answering the effects that run left. A stopped chart takes nothing,
    /// and once the call has spent its budget the event waits there unrun.
    fn deliver_internal(&mut self, event: Event) -> Vec<Effect> {
        if !self.state.core.running {
            return Vec::new();
        }
        if self.spent {
            self.state.core.enqueue(event);
            return Vec::new();
        }
        self.stamp();
        match interpreter::deliver_internal(&self.state.core, event) {
            Ok(stepped) => {
                self.state.core = stepped.state;
                stepped.effects
            }
            Err(_) => Vec::new(),
        }
    }

    /// An internal send past the call's budget: queued on the internal
    /// queue unrun, and the driver halted, answering the spent-budget effect
    /// when it was not halted already.
    fn spend(&mut self, event: Event) -> Vec<Effect> {
        if !self.state.core.running {
            return Vec::new();
        }
        self.spent = true;
        self.state.core.enqueue(event);
        if self.state.halted {
            return Vec::new();
        }
        self.state.halted = true;
        vec![interpreter::budget_exhausted(
            &self.state.core,
            self.state.round_budget.spent(),
        )]
    }
}

/// Every pending timer of the tree under `state`, each with the path to its
/// session, its due time and its sequence.
fn pending_timers(state: &State, path: &mut Vec<String>, out: &mut Vec<(Vec<String>, u64, u64)>) {
    out.extend(
        state
            .timers
            .iter()
            .map(|timer| (path.clone(), timer.due_ms, timer.sequence)),
    );
    for (invoke_id, invocation) in &state.invocations {
        path.push(invoke_id.clone());
        pending_timers(&invocation.child, path, out);
        path.pop();
    }
}