printobserver-core 0.15.2

Supervision logic for printobserver.
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
//! The supervisor, and the one call site every action passes through.
//!
//! # This module is the chokepoint
//!
//! Every action this crate takes at the printer port is issued from
//! [`Supervisor::issue_decided_action`] and from nowhere else, and that
//! function records the policy decision for the request **before** it issues
//! the call. That is structural rather than a convention, and two properties of
//! this module are what make it so:
//!
//! 1. **No other item of this crate names an action method.** A check over this
//!    crate's own sources enumerates every syntactic reference to an action
//!    method of the printer port — a call, a path taken as a value, a function
//!    pointer, an alias — and refuses one outside that function.
//! 2. **No item outside this module can obtain the handle to call one on.** The
//!    printer handle is declared once, as a private field here, and the
//!    identifier `PrinterPort` appears nowhere else in this crate. Nothing
//!    hands it out: no function anywhere returns it.
//!
//! **The decision gates actions, not port traffic.** Appending an event,
//! writing its image, reading a snapshot, running a supervision turn and
//! persisting its assessment are all port calls, none of them is an action
//! anyone requested, and none of them takes a decision — the event append in
//! particular is the *first* thing the loop does, before any decision could
//! exist. What the policy stands between is a requested
//! [`PrintAction`](crate::records::PrintAction) and the printer.
//!
//! The port's reads — `snapshot`, `job` and the three reads of the files the
//! printer holds — are outside the rule for that same reason, and are reached
//! through [`Supervisor::read_snapshot`], [`Supervisor::read_job`],
//! [`Supervisor::read_file_facts`], [`Supervisor::read_printed_files`] and
//! [`Supervisor::read_extruded_to`].
//!
//! # Nothing here sends a command string to a printer
//!
//! There is no method on the printer port that admits one, and the only
//! caller-supplied text that reaches the machine's own file API is the
//! [`FileName`](printobserver_types::FileName) newtype, which refuses anything
//! but a name.

use std::collections::{BTreeMap, BTreeSet};
use std::sync::{Arc, Mutex, MutexGuard, Weak};
use std::time::Duration;

use crate::records::{
    ActionId, ActionRecord, ActionRequest, ActorClass, ExecutionOutcome, PolicyDecision,
    PrintAction, RecoveryDecision, RecoveryRejection, ZTrust,
};
use printobserver_printer_api::{
    AxisPosition, FileFacts, JobSnapshot, PrintedExtent, PrinterCommand, PrinterError, PrinterPort,
    PrinterSnapshot, ToolFilament,
};
use printobserver_supervisor_api::SupervisorPort;
use printobserver_types::{EventPayload as _, EventRecord, FileName, PrintId, Timestamp};
use printobserver_vision_api::VisionPort;

use crate::store::{HistoryQuery, MAX_HISTORY_LIMIT, Stores};

use crate::block_on::block_on;
use crate::clock::{Clock, ClockReading};
use crate::config::CoreConfig;
use crate::decision::changes_the_machine;
use crate::detector::DetectorPauses;
use crate::error::CoreError;
use crate::expiry::Deadlines;
use crate::inbox::Inboxes;
use crate::kinds::{ActionExecutedPayload, ActionRequestedPayload, RecoveryRequestedPayload};
use crate::turn_access::{NoAccess, OpenedTurn, TurnAuthority};
use crate::turn_lock::TurnLocks;
use crate::watch::WatchState;

/// What became of one request that reached [`Supervisor::issue_decided_action`].
#[derive(Debug, Clone, PartialEq)]
pub struct Issued {
    /// The record of the request and the decision taken on it.
    pub record: ActionRecord,
    /// What the printer made of it, absent when the request was rejected.
    pub executed: Option<Result<(), PrinterError>>,
}

impl Issued {
    /// Whether the request was accepted and the printer took it.
    #[must_use]
    pub fn succeeded(&self) -> bool {
        matches!(self.executed, Some(Ok(())))
    }

    /// Why the printer refused it, when it reached the printer and was refused.
    #[must_use]
    pub fn printer_error(&self) -> Option<&PrinterError> {
        match &self.executed {
            Some(Err(error)) => Some(error),
            _ => None,
        }
    }
}

/// The status a printer port answers a method it does not implement with.
const NOT_SUPPORTED: u16 = 501;

/// What became of a recovery whose calls the printer answered so: carried
/// out, or failed at the printer — or, when its first call is one the printer
/// answers as not supported, which sent nothing, refused as one it cannot make.
fn recovery_decision_of(
    action: crate::records::RecoveryActionKind,
    executed: Result<(), PrinterError>,
    first: bool,
) -> RecoveryDecision {
    match executed {
        Ok(()) => RecoveryDecision::Executed(ExecutionOutcome::Succeeded),
        Err(PrinterError::Refused {
            status: NOT_SUPPORTED,
            detail,
        }) if first => {
            RecoveryDecision::Rejected(RecoveryRejection::PrinterCannot { action, detail })
        }
        failed @ Err(_) => RecoveryDecision::Executed(outcome_of(&failed)),
    }
}

/// What the printer made of one request, as its record keeps it.
fn outcome_of(executed: &Result<(), PrinterError>) -> ExecutionOutcome {
    match executed {
        Ok(()) => ExecutionOutcome::Succeeded,
        Err(error) => ExecutionOutcome::Failed {
            reason: error.to_string(),
        },
    }
}

/// One call a recovery makes at an action method of the printer port, which
/// only [`Supervisor::issue_decided_action`] turns into one.
#[derive(Debug, Clone, Copy, PartialEq)]
pub(crate) enum PortCall {
    /// One tool's target, in degrees Celsius.
    ToolTarget {
        /// The tool.
        tool: i64,
        /// The target.
        target_c: f64,
    },
    /// The bed's target, in degrees Celsius.
    BedTarget {
        /// The target.
        target_c: f64,
    },
    /// The part-cooling fan, in percent.
    Fan {
        /// The percentage.
        percent: f64,
    },
    /// One command of the port's closed vocabulary.
    Command(PrinterCommand),
}

/// What reaches the chokepoint: a print action and the decision policy took
/// on it, or a recovery and the calls it was decided to make.
#[derive(Debug, Clone)]
pub(crate) enum Decided {
    /// A print action.
    Action {
        /// The request.
        request: Box<ActionRequest>,
        /// The decision taken on it.
        decision: PolicyDecision,
        /// The print it was asked of, when it was asked of one.
        against: Option<PrintId>,
    },
    /// A recovery.
    Recovery {
        /// The request, as it is written down.
        requested: Box<RecoveryRequestedPayload>,
        /// The calls it makes, in order, or the refusal it met.
        calls: Result<Vec<PortCall>, RecoveryRejection>,
    },
}

/// What the chokepoint answers for what reached it.
#[derive(Debug, Clone, PartialEq)]
pub(crate) enum IssuedAs {
    /// A print action.
    Action(Issued),
    /// A recovery.
    Recovery(RecoveryIssued),
}

/// What became of one recovery request.
#[derive(Debug, Clone, PartialEq)]
pub struct RecoveryIssued {
    /// What became of it.
    pub decision: RecoveryDecision,
    /// The `recovery_requested` event.
    pub requested: EventRecord,
    /// The `recovery_executed` or `recovery_rejected` event.
    pub settled: EventRecord,
}

/// The supervision core: one printer, one store, one agent, one clock.
///
/// Built with [`Supervisor::new`], which answers a shared handle because the
/// expiry driver holds a weak reference to it: a bounded intervention expires
/// with nothing asking it to, and the driver is the thread that makes that so.
pub struct Supervisor {
    /// The printer. Private to this module, and named nowhere else.
    printer: Arc<dyn PrinterPort>,
    /// Durable state, one handle per aggregate.
    stores: Stores,
    /// External observations.
    vision: Arc<dyn VisionPort>,
    /// The supervising agent's harness.
    agent: Arc<dyn SupervisorPort>,
    /// The one time source.
    clock: Arc<dyn Clock>,
    /// The server configuration.
    config: CoreConfig,
    /// One supervision turn per print at a time.
    turns: TurnLocks,
    /// One resolution of which print a job or an alert is at a time.
    ///
    /// Finding the print the printer's job belongs to and finding the print an
    /// alert belongs to each read the open prints and may then write one, so
    /// two of them interleaved could each find none and each open one. They
    /// take this lock, under [`Supervisor::resolving_key`], for as long as that
    /// read and write take.
    resolving: TurnLocks,
    /// The one key [`Supervisor::resolving`] is held under.
    resolving_key: PrintId,
    /// One expiry of a print's interventions at a time.
    ///
    /// The expiry driver's sweep, a print's close-out and a start's
    /// reconciliation can each reach one intervention, and its restoration is
    /// issued before its outcome is settled, so two expiries interleaved would
    /// each put the value back.
    expiring: TurnLocks,
    /// One write to the spool ledger at a time.
    ///
    /// Every spool command, every settlement, every filament-change signal and
    /// every accounting of the printer's tallies reads the spools it charges
    /// and then writes them; two of them interleaved would each compute from
    /// the same figure and one of the two would be lost. They take this lock,
    /// under [`Supervisor::resolving_key`], for as long as that read and write
    /// take, and never take another lock while they hold it.
    spool_writes: TurnLocks,
    /// The prints this supervisor has itself seen printing or paused, or
    /// started: a print it closes out that is not among them ended while
    /// nothing was watching it.
    seen_running: Mutex<BTreeSet<PrintId>>,
    /// When, on the clock nothing steps, the agent last changed the machine
    /// on each print: absent until it is known, and `None` once it is known
    /// the agent never has.
    last_agent_action: Mutex<BTreeMap<PrintId, Option<AgentAction>>>,
    /// Both of the clock's readings as this supervisor started, which every
    /// instant a previous supervisor recorded is translated against once.
    started: ClockReading,
    /// The active interventions being counted down.
    deadlines: Deadlines,
    /// What reaches each print's running turn, and which prints have one.
    inboxes: Inboxes,
    /// The prints the detector paused and nobody has resumed yet.
    detector_pauses: Mutex<DetectorPauses>,
    /// Where each supervision turn's runs take their credentials from, once
    /// the composition root has installed it.
    turn_authority: std::sync::OnceLock<Arc<dyn TurnAuthority>>,
    /// What reads the failure detector's own record of the print, once the
    /// composition root has installed it.
    scoring_probe: std::sync::OnceLock<Arc<dyn crate::scoring::ScoringProbe>>,
    /// What makes the image every check is shown, once the composition root
    /// has installed it.
    pub(crate) check_images: std::sync::OnceLock<Arc<dyn crate::check_image::CheckImages>>,
    /// The prints whose running turn acknowledged its event by command, which
    /// is then not acknowledged again from the turn's answer.
    pub(crate) acknowledged_by_turn: Mutex<BTreeSet<PrintId>>,
    /// The prints whose running turn has asked for a pause or a cancel,
    /// shared with the access each turn's port is handed.
    pub(crate) stopped_by_turn: Arc<Mutex<BTreeSet<PrintId>>>,
    /// What the printer watch holds between its reads.
    pub(crate) watch: Mutex<WatchState>,
    /// Whether Z is trusted, which this supervisor starts out not doing.
    z_trust: Mutex<ZTrust>,
    /// What the last print's G-code laid down, by the print and the file
    /// position it was read to, so a recovery read does not read the file
    /// again while neither has moved.
    recovery_extents: Mutex<Option<RecoveryExtent>>,
    /// The prints a recovery moved the printer under while they were paused,
    /// and the `recovery_executed` event of the first that did.
    moved_while_paused: Mutex<BTreeMap<PrintId, printobserver_types::EventId>>,
    /// Every print's G-code index, built once per print for as long as this
    /// supervisor runs.
    gcode_indexes: crate::gcode_index::GcodeIndexes,
}

/// One print's G-code read to one file position, and what it laid down.
type RecoveryExtent = ((PrintId, i64), Option<PrintedExtent>);

/// When the agent last changed the machine on one print, as this supervisor
/// measures it.
#[derive(Debug, Clone, Copy)]
struct AgentAction {
    /// The reading of the clock nothing steps this supervisor counts on from.
    seen_at: Duration,
    /// How long before `seen_at` the change was made: nothing for one this
    /// supervisor made, and for one a previous supervisor recorded, how long
    /// it had been waited already as this one started.
    already: Duration,
}

impl core::fmt::Debug for Supervisor {
    fn fmt(&self, formatter: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
        formatter
            .debug_struct("Supervisor")
            .field("config", &self.config)
            .finish_non_exhaustive()
    }
}

impl Supervisor {
    /// Build a supervisor over the three ports and the stores, and start its
    /// expiry driver.
    ///
    /// The driver is a thread of core's own holding a weak reference, so it
    /// stops of its own accord when the last handle to the supervisor is
    /// dropped. It reads the injected clock and nothing else, which is what
    /// makes a bounded intervention expire with nothing asking it to.
    ///
    /// # Panics
    ///
    /// Panics when the operating system refuses to start that thread, which is
    /// a supervisor whose bounded interventions would never expire — the one
    /// state this crate must not come up in quietly.
    #[must_use]
    pub fn new(
        config: CoreConfig,
        printer: Arc<dyn PrinterPort>,
        stores: Stores,
        vision: Arc<dyn VisionPort>,
        agent: Arc<dyn SupervisorPort>,
        clock: Arc<dyn Clock>,
    ) -> Arc<Self> {
        let poll = config.expiry_poll;
        let started = ClockReading::of(clock.as_ref());
        let supervisor = Arc::new(Self {
            printer,
            stores,
            vision,
            agent,
            clock,
            config,
            turns: TurnLocks::default(),
            resolving: TurnLocks::default(),
            resolving_key: PrintId::new(),
            expiring: TurnLocks::default(),
            spool_writes: TurnLocks::default(),
            seen_running: Mutex::new(BTreeSet::new()),
            last_agent_action: Mutex::new(BTreeMap::new()),
            started,
            deadlines: Deadlines::default(),
            inboxes: Inboxes::default(),
            detector_pauses: Mutex::new(BTreeMap::new()),
            turn_authority: std::sync::OnceLock::new(),
            scoring_probe: std::sync::OnceLock::new(),
            check_images: std::sync::OnceLock::new(),
            acknowledged_by_turn: Mutex::new(BTreeSet::new()),
            stopped_by_turn: Arc::new(Mutex::new(BTreeSet::new())),
            watch: Mutex::new(WatchState::default()),
            z_trust: Mutex::new(ZTrust::Untrusted(
                crate::recovery::UNTRUSTED_AT_START.to_owned(),
            )),
            recovery_extents: Mutex::new(None),
            moved_while_paused: Mutex::new(BTreeMap::new()),
            gcode_indexes: crate::gcode_index::GcodeIndexes::default(),
        });
        let weak: Weak<Self> = Arc::downgrade(&supervisor);
        std::thread::Builder::new()
            .name("printobserver-expiry".to_owned())
            .spawn(move || {
                while let Some(supervisor) = weak.upgrade() {
                    let _ = block_on(supervisor.sweep_expired());
                    block_on(supervisor.resume_due_detector_pauses());
                    drop(supervisor);
                    std::thread::sleep(poll);
                }
            })
            .expect("the expiry driver thread starts");
        supervisor
    }

    /// Install where every supervision turn's runs take their credentials
    /// from. The first installation stands; answers whether this one did.
    pub fn install_turn_authority(&self, authority: Arc<dyn TurnAuthority>) -> bool {
        self.turn_authority.set(authority).is_ok()
    }

    /// Install what reads the failure detector's own record of the print,
    /// which every round of periodic observations asks first. The first
    /// installation stands; answers whether this one did.
    pub fn install_scoring_probe(&self, probe: Arc<dyn crate::scoring::ScoringProbe>) -> bool {
        self.scoring_probe.set(probe).is_ok()
    }

    /// What reads the failure detector's own record of the print, when one is
    /// installed.
    pub(crate) fn scoring_probe(&self) -> Option<Arc<dyn crate::scoring::ScoringProbe>> {
        self.scoring_probe.get().cloned()
    }

    /// Whether Z is trusted, held for one read or write.
    pub(crate) fn z_trust_held(&self) -> MutexGuard<'_, ZTrust> {
        self.z_trust
            .lock()
            .unwrap_or_else(std::sync::PoisonError::into_inner)
    }

    /// What the last print's G-code was last read to lay down, held for one
    /// read or write.
    pub(crate) fn recovery_extents(&self) -> MutexGuard<'_, Option<RecoveryExtent>> {
        self.recovery_extents
            .lock()
            .unwrap_or_else(std::sync::PoisonError::into_inner)
    }

    /// The prints a recovery moved the printer under while they were paused,
    /// held for one read or write.
    pub(crate) fn moved_while_paused(
        &self,
    ) -> MutexGuard<'_, BTreeMap<PrintId, printobserver_types::EventId>> {
        self.moved_while_paused
            .lock()
            .unwrap_or_else(std::sync::PoisonError::into_inner)
    }

    /// The issuer for one turn about one print, opened now, `handed_image`
    /// whether the turn is handed its event's image.
    pub(crate) fn open_turn(&self, print_id: PrintId, handed_image: bool) -> OpenedTurn {
        self.turn_authority
            .get()
            .map_or_else(NoAccess::opened, |authority| {
                authority.open(print_id, handed_image)
            })
    }

    /// Every print's G-code index.
    pub(crate) const fn gcode_indexes(&self) -> &crate::gcode_index::GcodeIndexes {
        &self.gcode_indexes
    }

    /// Durable state, one handle per aggregate.
    pub(crate) const fn stores(&self) -> &Stores {
        &self.stores
    }

    /// External observations.
    pub(crate) fn vision(&self) -> &Arc<dyn VisionPort> {
        &self.vision
    }

    /// The supervising agent's harness.
    pub(crate) fn agent(&self) -> &Arc<dyn SupervisorPort> {
        &self.agent
    }

    /// The one time source.
    pub(crate) fn clock(&self) -> &Arc<dyn Clock> {
        &self.clock
    }

    /// The server configuration this supervisor runs under.
    #[must_use]
    pub const fn config(&self) -> &CoreConfig {
        &self.config
    }

    /// One supervision turn per print at a time.
    pub(crate) const fn turns(&self) -> &TurnLocks {
        &self.turns
    }

    /// One expiry of a print's interventions at a time.
    pub(crate) const fn expiring(&self) -> &TurnLocks {
        &self.expiring
    }

    /// Both of the clock's readings as this supervisor started.
    pub(crate) const fn started(&self) -> ClockReading {
        self.started
    }

    /// The active interventions being counted down.
    pub(crate) const fn deadlines(&self) -> &Deadlines {
        &self.deadlines
    }

    /// What reaches each print's running turn.
    pub(crate) const fn inboxes(&self) -> &Inboxes {
        &self.inboxes
    }

    /// The prints the detector paused and nobody has resumed yet.
    pub(crate) fn detector_pauses(&self) -> MutexGuard<'_, DetectorPauses> {
        self.detector_pauses
            .lock()
            .unwrap_or_else(std::sync::PoisonError::into_inner)
    }

    /// Wait until no other resolution of a print is running, then hold it.
    pub(crate) async fn resolving(&self) -> crate::turn_lock::TurnGuard<'_> {
        self.resolving.acquire(self.resolving_key).await
    }

    /// Wait until no other write to the spool ledger is running, then hold it.
    pub(crate) async fn spool_writes(&self) -> crate::turn_lock::TurnGuard<'_> {
        self.spool_writes.acquire(self.resolving_key).await
    }

    /// Record that this supervisor saw one print's job printing or paused.
    pub(crate) fn note_seen_running(&self, print_id: PrintId) {
        self.seen_running
            .lock()
            .unwrap_or_else(std::sync::PoisonError::into_inner)
            .insert(print_id);
    }

    /// Whether this supervisor saw one print's job printing or paused.
    pub(crate) fn was_seen_running(&self, print_id: PrintId) -> bool {
        self.seen_running
            .lock()
            .unwrap_or_else(std::sync::PoisonError::into_inner)
            .contains(&print_id)
    }

    /// Record that the agent changed the machine on one print at a reading of
    /// the clock nothing steps.
    pub(crate) fn note_agent_action(&self, print_id: PrintId, at: Duration) {
        self.last_agents_actions().insert(
            print_id,
            Some(AgentAction {
                seen_at: at,
                already: Duration::ZERO,
            }),
        );
    }

    /// How long ago, on the clock nothing steps, the agent last changed the
    /// machine on one print, if it has.
    ///
    /// A print this supervisor has not seen the agent act on is read from its
    /// history once: the newest change the agent made that reached the machine,
    /// translated against the reading this supervisor took as it started, so
    /// that what is left of the interval when a supervisor restarts is waited
    /// out on the clock nothing steps. The time it had already been waited at
    /// that reading is kept as a span of its own rather than as a reading of
    /// that clock, whose origin may be later than the change: a host's
    /// monotonic clock starts near zero with the process. A change recorded
    /// after that reading — a wall clock stepped back across the restart — is
    /// taken as made the moment this supervisor started, which waits the whole
    /// interval rather than longer.
    ///
    /// # Errors
    ///
    /// Returns the store's own error when the print's history could not be
    /// read, which the next request reads again.
    pub(crate) async fn since_last_agent_action(
        &self,
        print_id: PrintId,
    ) -> Result<Option<Duration>, CoreError> {
        let known = self.last_agents_actions().get(&print_id).copied();
        let last = if let Some(last) = known {
            last
        } else {
            let started = self.started;
            let recorded = self
                .recorded_agent_action(print_id)
                .await?
                .map(|at| AgentAction {
                    seen_at: started.elapsed,
                    already: started
                        .wall
                        .as_utc()
                        .signed_duration_since(*at.as_utc())
                        .to_std()
                        .unwrap_or_default(),
                });
            *self
                .last_agents_actions()
                .entry(print_id)
                .or_insert(recorded)
        };
        Ok(last.map(|action| {
            self.clock()
                .paced()
                .saturating_sub(action.seen_at)
                .saturating_add(action.already)
        }))
    }

    /// When the newest change the agent made to the machine on one print was
    /// recorded, if its history holds one.
    async fn recorded_agent_action(
        &self,
        print_id: PrintId,
    ) -> Result<Option<Timestamp>, CoreError> {
        let history = self
            .stores()
            .events
            .history(HistoryQuery {
                print_id,
                kinds: vec![
                    ActionRequestedPayload::kind(),
                    ActionExecutedPayload::kind(),
                ],
                since: None,
                until: None,
                limit: Some(MAX_HISTORY_LIMIT),
            })
            .await?;
        let executed: BTreeSet<ActionId> = history
            .iter()
            .filter_map(EventRecord::payload_as::<ActionExecutedPayload>)
            .filter_map(Result::ok)
            .map(|payload| payload.action_id)
            .collect();
        // Newest first, so the first change found is the newest.
        Ok(history.iter().find_map(|event| {
            let requested = event.payload_as::<ActionRequestedPayload>()?.ok()?;
            (executed.contains(&requested.action_id)
                && requested.actor.class() == ActorClass::Agent
                && changes_the_machine(requested.action.kind()))
            .then_some(event.received_at)
        }))
    }

    /// The agent's last change on each print, held for one read or write.
    fn last_agents_actions(&self) -> MutexGuard<'_, BTreeMap<PrintId, Option<AgentAction>>> {
        self.last_agent_action
            .lock()
            .unwrap_or_else(std::sync::PoisonError::into_inner)
    }

    /// Read the printer's current state.
    ///
    /// A read rather than an action: it changes nothing at the machine, so it
    /// takes no policy decision and is outside the chokepoint's rule.
    pub(crate) async fn read_snapshot(&self) -> Result<PrinterSnapshot, PrinterError> {
        self.printer.snapshot().await
    }

    /// Read the job the printer reports it is running.
    ///
    /// A read, for the same reason [`Supervisor::read_snapshot`] is.
    pub(crate) async fn read_job(&self) -> Result<JobSnapshot, PrinterError> {
        self.printer.job().await
    }

    /// Read what the printer knows of one file it holds.
    ///
    /// A read, for the same reason [`Supervisor::read_snapshot`] is.
    pub(crate) async fn read_file_facts(
        &self,
        file_name: FileName,
    ) -> Result<FileFacts, PrinterError> {
        self.printer.file_facts(file_name).await
    }

    /// Read the material each tool of one file was sliced for, tool 0 first.
    // llmlint: ignore-block[invalid_states_unrepresentable] suppressions.toml has the reason.
    pub(crate) async fn read_filament_types(
        &self,
        file_name: FileName,
    ) -> Result<Vec<Option<String>>, PrinterError> {
        // llmlint: ignore-end[invalid_states_unrepresentable]
        self.printer.filament_types(file_name).await
    }

    /// Read every file the printer holds that carries a print tally.
    ///
    /// A read, for the same reason [`Supervisor::read_snapshot`] is.
    pub(crate) async fn read_printed_files(&self) -> Result<Vec<FileFacts>, PrinterError> {
        self.printer.printed_files().await
    }

    /// Read what one file printed up to a byte position in it.
    ///
    /// A read, for the same reason [`Supervisor::read_snapshot`] is.
    pub(crate) async fn read_printed_extent(
        &self,
        file_name: FileName,
        position_bytes: i64,
    ) -> Result<Option<PrintedExtent>, PrinterError> {
        self.printer.printed_extent(file_name, position_bytes).await
    }

    /// Stream one file's whole G-code into a sink.
    ///
    /// A read, for the same reason [`Supervisor::read_snapshot`] is.
    pub(crate) async fn read_gcode(
        &self,
        file_name: FileName,
        sink: &mut dyn printobserver_printer_api::GcodeSink,
    ) -> Result<(), PrinterError> {
        self.printer.read_gcode(file_name, sink).await
    }

    /// Read where the print head is.
    ///
    /// A read, for the same reason [`Supervisor::read_snapshot`] is.
    pub(crate) async fn read_position(&self) -> Result<AxisPosition, PrinterError> {
        self.printer.read_position().await
    }

    /// Read the filament each tool is fed by one file's own moves, up to a
    /// byte position in it.
    ///
    /// A read, for the same reason [`Supervisor::read_snapshot`] is.
    pub(crate) async fn read_extruded_to(
        &self,
        file_name: FileName,
        position_bytes: i64,
    ) -> Result<Vec<ToolFilament>, PrinterError> {
        self.printer.extruded_to(file_name, position_bytes).await
    }

    /// Record the decision for one request, and — if it was accepted — issue it.
    ///
    /// **This is the one function of this crate that calls an action method of
    /// the printer port.** Every action, from every actor — the agent, an
    /// operator, the command line, a client — arrives here, and the decision is
    /// recorded before anything reaches the machine. A rejected request reaches
    /// no action method at all: the record of the rejection is the only thing
    /// that happens.
    ///
    /// `against` is the print the request was asked of, when it was asked of
    /// one.
    ///
    /// # Errors
    ///
    /// Returns the store's own error when the decision or the execution could
    /// not be recorded. A failure at the printer is not an error here: it is
    /// recorded as [`ExecutionOutcome::Failed`] and answered in [`Issued`], so
    /// that a caller sees a request that was made and refused rather than a
    /// request that never happened.
    pub(crate) async fn issue_decided_action(
        &self,
        decided: Decided,
    ) -> Result<IssuedAs, CoreError> {
        let (request, decision, against) = match decided {
            Decided::Action {
                request,
                decision,
                against,
            } => (*request, decision, against),
            // The request is written down before anything reaches the machine.
            Decided::Recovery { requested, calls } => {
                let requested_event = self.write_recovery_request(&requested).await?;
                let decision = match calls {
                    Err(rejection) => RecoveryDecision::Rejected(rejection),
                    Ok(calls) => {
                        let (mut executed, mut first) = (Ok(()), true);
                        for call in calls {
                            executed = match call {
                                PortCall::ToolTarget { tool, target_c } => {
                                    self.printer.set_tool_target_c(tool, target_c).await
                                }
                                PortCall::BedTarget { target_c } => {
                                    self.printer.set_bed_target_c(target_c).await
                                }
                                PortCall::Fan { percent } => {
                                    self.printer.set_fan_percent(percent).await
                                }
                                PortCall::Command(command) => {
                                    self.printer.send_command(command).await
                                }
                            };
                            if executed.is_err() {
                                break;
                            }
                            first = false;
                        }
                        recovery_decision_of(requested.action.kind(), executed, first)
                    }
                };
                return self
                    .recovery_settled(*requested, requested_event, decision)
                    .await
                    .map(IssuedAs::Recovery);
            }
        };
        // A close is asked of the record it names, which may have ended or may
        // not be the newest open one, so it is recorded against that record;
        // every other request binds where the store binds it.
        let actions = &self.stores.actions;
        let record = match (&request.action, against) {
            (PrintAction::ClosePrint { .. }, Some(print_id)) => {
                actions
                    .record_action_against(print_id, request.clone(), decision.clone())
                    .await?
            }
            _ => {
                actions
                    .record_action(request.clone(), decision.clone())
                    .await?
            }
        };
        if decision != PolicyDecision::Accepted {
            return Ok(IssuedAs::Action(Issued {
                record,
                executed: None,
            }));
        }
        let executed = match &request.action {
            PrintAction::Pause { .. } => self.printer.pause().await,
            PrintAction::Resume { .. } => self.printer.resume().await,
            PrintAction::Cancel { .. } => self.printer.cancel().await,
            PrintAction::StartPrint { file_name, .. } => {
                self.printer.start(file_name.clone()).await
            }
            PrintAction::SetFeedrateFactor { factor, .. } => {
                self.printer.set_feedrate_factor(*factor).await
            }
            PrintAction::SetFlowrateFactor { factor, .. } => {
                self.printer.set_flowrate_factor(*factor).await
            }
            PrintAction::SetToolTargetC { tool, target_c, .. } => {
                self.printer.set_tool_target_c(*tool, *target_c).await
            }
            PrintAction::SetBedTargetC { target_c, .. } => {
                self.printer.set_bed_target_c(*target_c).await
            }
            PrintAction::SetFanPercent { percent, .. } => {
                self.printer.set_fan_percent(*percent).await
            }
            // Acknowledging a failure is a decision about the print written into
            // its record, and asks nothing of the machine whatever the
            // disposition. Stopping a print is cancelling it, which the policy
            // grants each actor on its own: an acknowledgement that cancelled
            // would let an actor granted acknowledgements and not cancellation
            // stop a print all the same. Closing a record writes that record
            // and asks nothing of the machine either: what it does is done once
            // its execution is recorded ([`crate::closing`]).
            PrintAction::AcknowledgeFailure { .. } | PrintAction::ClosePrint { .. } => Ok(()),
        };
        let outcome = outcome_of(&executed);
        let record = self
            .stores
            .actions
            .record_execution(record.id, outcome)
            .await?;
        Ok(IssuedAs::Action(Issued {
            record,
            executed: Some(executed),
        }))
    }

    /// Issue one print action through the chokepoint, answering what became
    /// of it.
    pub(crate) async fn issue_action(
        &self,
        request: ActionRequest,
        decision: PolicyDecision,
        against: Option<PrintId>,
    ) -> Result<Issued, CoreError> {
        match self
            .issue_decided_action(Decided::Action {
                request: Box::new(request),
                decision,
                against,
            })
            .await?
        {
            IssuedAs::Action(issued) => Ok(issued),
            // The chokepoint answers each request in its own kind, so this is
            // a chokepoint that has come apart from its callers.
            IssuedAs::Recovery(_) => Err(CoreError::Unrepresentable {
                detail: "a print action was answered as a recovery".to_owned(),
            }),
        }
    }
}