printobserver-core 0.15.5

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
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
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
//! The printer timeline, and what every print takes from the spools that fed it.
//!
//! # A print is opened, charged in segments, and settled once
//!
//! Every print this system opens after spool tracking was installed — by a
//! start through it, by a read adopting a job somebody started at the printer,
//! or by an alert about a print it had not seen — gets a `print_started`
//! naming the spool on each tool. A print without one was already open when
//! tracking was installed, is never charged, and its close-out says so with one
//! `print_untracked`.
//!
//! A tool's filament is charged in **segments**, each under its own
//! [`ChargeKey`]: segment 0 opens at the start, and a spool loaded or unloaded
//! mid-print, or a filament-change signal, charges the open segment up to the
//! position read then and opens the next. Which segment is open is read back
//! from the store — one past the highest the print has charged or held on that
//! tool — and the closing segment is charged the tool's total less every
//! length already charged or held for that print and tool. Both are read from
//! what the store holds, never from memory, so a supervisor built after a
//! restart charges exactly what is left.
//!
//! Close-out works the print's remaining charges out, then settles it through
//! [`FilamentStore::settle_print`](crate::store::FilamentStore::settle_print)
//! — its charges, pends, marks, `print_ended` and settlement in one write —
//! and only then ends the print record. A close-out that finds the print
//! settled only ends it, so whatever is replayed after a crash debits nothing
//! twice.
//!
//! # After an unconfirmed filament change, the remainder is held
//!
//! A filament-change signal charges the loaded spool up to the stop point and
//! marks it as one whose change nobody confirmed. From then until a spool is
//! loaded onto that tool or that spool is confirmed in place, what the tool
//! feeds is not charged to the spool still recorded there: the open segment is
//! **pended**. Whether a segment is post-signal is read from the marks alone —
//! the spool judged for the tool still carries that print's filament-change
//! mark — so a restart between a confirmation and the close-out changes
//! nothing.

use std::collections::{BTreeMap, BTreeSet};

use printobserver_printer_api::{ErrorSource, FileFacts, JobSnapshot, PrinterState, ToolFilament};
use printobserver_types::{
    EventBody, EventKind, EventPayload, EventSource, FileName, PrintId, Timestamp,
};
use printobserver_vision_api::PrinterSignal;

use crate::error::CoreError;
use crate::filament::grams_for_length;
use crate::kinds::{
    ActionExecutedPayload, ActionRequestedPayload, ChargePendedPayload,
    FilamentChangeSignalledPayload, FilamentConsumedPayload, PositionFreshness, PrintEndedPayload,
    PrintStartedPayload, PrintUntrackedPayload, SignalPosition, SpoolUnverifiedPayload,
    system_source,
};
use crate::records::{
    ActionId, ChargeKey, ChargeMethod, ChargedFilament, FilePosition, LoadedSpool, PendingCharge,
    PendingTaken, Preflight, PrintAction, PrintOutcome, PrintRecord, SpoolId, SpoolRecord,
    SpoolTrust, UnverifiedReason,
};
use crate::store::{
    Charge, ChargeIdentity, ChargeRecord, EventDraft, HistoryQuery, MAX_HISTORY_LIMIT, Pend,
    SpoolChange, SpoolMutation,
};
use crate::supervisor::Supervisor;
use crate::watch::PushFacts;

/// Below this many millimetres a length is nothing, and no charge is written
/// for it: a zero-length charge would be a record of nothing having happened.
pub(crate) const NOTHING_MM: f64 = 1e-6;

/// Why a print that predates spool tracking carries no charge.
pub const UNTRACKED_REASON: &str =
    "the print was already open when spool tracking was installed, so it is not charged";

/// The note a charge taken from a pending charge carries.
const TAKEN_NOTE: &str = "fed after a filament change nobody confirmed, and taken by this spool";

/// The note a post-signal segment charged to the spool loaded after it carries.
pub(crate) const AFTER_CHANGE: &str =
    "fed after a filament change nobody confirmed, and charged to the spool loaded after it";

/// What one print has had charged and held so far, as the store holds it.
#[derive(Debug, Clone, Default)]
pub(crate) struct PrintLedger {
    /// Every charge recorded against the print.
    charges: Vec<ChargeRecord>,
    /// Every charge still held for the print.
    pending: Vec<PendingCharge>,
}

/// The segment a print key names, when it is one.
const fn segment_of(key: &ChargeKey) -> Option<u32> {
    match key {
        ChargeKey::Print { segment, .. } => Some(*segment),
        ChargeKey::Unobserved { .. } => None,
    }
}

impl PrintLedger {
    /// Every length charged or held for the print on one tool, in millimetres.
    pub(crate) fn already_mm(&self, tool: i64) -> f64 {
        let charged: f64 = self
            .charges
            .iter()
            .filter(|charge| charge.identity.tool() == tool)
            .map(|charge| charge.length_mm)
            .sum();
        let held: f64 = self
            .pending
            .iter()
            .filter(|pending| pending.tool() == tool)
            .map(|pending| pending.length_mm)
            .sum();
        charged + held
    }

    /// The segment open on one tool: one past the highest charged or held.
    pub(crate) fn open_segment(&self, tool: i64) -> u32 {
        let charged = self
            .charges
            .iter()
            .filter(|charge| charge.identity.tool() == tool)
            .filter_map(|charge| segment_of(charge.identity.key()));
        let held = self
            .pending
            .iter()
            .filter(|pending| pending.tool() == tool)
            .map(|pending| pending.charge_key.segment);
        charged.chain(held).max().map_or(0, |highest| highest + 1)
    }

    /// The spool the latest charged segment on one tool was charged to.
    pub(crate) fn last_spool(&self, tool: i64) -> Option<SpoolId> {
        self.charges
            .iter()
            .filter(|charge| charge.identity.tool() == tool)
            .max_by_key(|charge| segment_of(charge.identity.key()))
            .and_then(ChargeRecord::spool_id)
    }

    /// Every tool the print has charged or held anything on.
    fn tools(&self) -> BTreeSet<i64> {
        self.charges
            .iter()
            .map(|charge| charge.identity.tool())
            .chain(self.pending.iter().map(PendingCharge::tool))
            .collect()
    }
}

/// The figure each spool stands at as one write's charges are worked out, so
/// that two charges against one spool in one write chain rather than both
/// starting from the figure read.
#[derive(Debug, Default)]
pub(crate) struct Figures {
    /// Each spool's figure so far, once it has been charged in this write.
    remaining: BTreeMap<SpoolId, f64>,
}

impl Figures {
    /// One spool's figure before and after a debit of so many grams.
    fn debit(&mut self, spool: &SpoolRecord, grams: f64) -> (f64, f64) {
        let before = *self.remaining.get(&spool.id).unwrap_or(&spool.remaining_g);
        let after = before - grams;
        self.remaining.insert(spool.id, after);
        (before, after)
    }

    /// One spool's figure once every debit so far has been taken.
    pub(crate) fn now(&self, spool: &SpoolRecord) -> f64 {
        *self.remaining.get(&spool.id).unwrap_or(&spool.remaining_g)
    }
}

/// One stretch of filament a tool fed, before it is charged or held.
#[derive(Debug, Clone)]
pub(crate) struct Feed {
    /// The durable identity of the debit, which names the tool that fed it
    /// and, for a print this system watched, the print.
    pub(crate) key: ChargeKey,
    /// How the length was worked out.
    pub(crate) method: ChargeMethod,
    /// The length fed, in millimetres.
    pub(crate) length_mm: f64,
    /// The file's analysed length for the tool, when it had one.
    pub(crate) analysed_length_mm: Option<f64>,
    /// How far into the file the stretch runs, when that was read.
    pub(crate) position: Option<FilePosition>,
    /// Anything a reader needs to make sense of the figure.
    pub(crate) note: Option<String>,
}

/// Where a print is, and how that is known.
#[derive(Debug, Clone, PartialEq, Eq)]
pub(crate) enum Whereabouts {
    /// Read from the job just now.
    ReadNow(FilePosition),
    /// The last position recorded, the job reporting none now.
    Recorded(FilePosition),
    /// No position has been read for it at all.
    Unknown,
}

impl Whereabouts {
    /// The position, however it is known.
    const fn position(&self) -> Option<&FilePosition> {
        match self {
            Self::ReadNow(position) | Self::Recorded(position) => Some(position),
            Self::Unknown => None,
        }
    }
}

/// The filament each tool fed up to a position, and how that was worked out.
#[derive(Debug, Clone)]
pub(crate) struct Extrusion {
    /// The length per tool, in millimetres.
    pub(crate) lengths: BTreeMap<i64, f64>,
    /// How the lengths were worked out.
    pub(crate) method: ChargeMethod,
    /// Why, when the lengths are a fallback.
    pub(crate) note: Option<String>,
}

/// What the printer says of a print's file: its facts and its analysis.
#[derive(Debug, Clone, Default)]
pub(crate) struct FileAnalysis {
    /// The file's facts, when they could be read.
    pub(crate) facts: Option<FileFacts>,
    /// The analysed length per tool, when the file has an analysis.
    pub(crate) lengths: Option<BTreeMap<i64, f64>>,
}

impl FileAnalysis {
    /// The analysed length for one tool, when the file has an analysis.
    pub(crate) fn length(&self, tool: i64) -> Option<f64> {
        self.lengths
            .as_ref()
            .map(|lengths| lengths.get(&tool).copied().unwrap_or(0.0))
    }

    /// The file's size in bytes, when it is known.
    fn size_bytes(&self) -> Option<i64> {
        self.facts.as_ref().and_then(|facts| facts.size_bytes)
    }
}

/// The lengths per tool one list of tool filament adds up to.
///
/// A printer's answer is read at this boundary: a tool numbered below zero, or
/// a length that is negative or not a number, is no filament any tool fed, and
/// is left out rather than charged.
pub(crate) fn lengths_of(filament: &[ToolFilament]) -> BTreeMap<i64, f64> {
    let mut lengths = BTreeMap::new();
    for each in filament
        .iter()
        .filter(|each| each.tool >= 0 && each.length_mm.is_finite() && each.length_mm >= 0.0)
    {
        *lengths.entry(each.tool).or_insert(0.0) += each.length_mm;
    }
    // Lengths that are each finite can still sum past what a figure holds;
    // such a tool's total is no length either.
    lengths.retain(|_, total| total.is_finite());
    lengths
}

/// What a tool fed of a print's open segment, up to a position, when it fed
/// anything since what the print has already charged or held on it.
pub(crate) fn feed_to(
    print_id: PrintId,
    tool: i64,
    ledger: &PrintLedger,
    extrusion: &Extrusion,
    position: &FilePosition,
    analysis: &FileAnalysis,
) -> Option<Feed> {
    let length = extrusion.lengths.get(&tool).copied().unwrap_or(0.0) - ledger.already_mm(tool);
    (length > NOTHING_MM).then(|| Feed {
        key: ChargeKey::Print {
            print_id,
            tool,
            segment: ledger.open_segment(tool),
        },
        method: extrusion.method,
        length_mm: length,
        analysed_length_mm: analysis.length(tool),
        position: Some(position.clone()),
        note: extrusion.note.clone(),
    })
}

/// A print's file, as a name a file API takes, when it has one.
fn file_of(print: &PrintRecord) -> Option<FileName> {
    print
        .file_name
        .as_deref()
        .and_then(|name| FileName::new(name).ok())
}

/// Whether a spool holds one print's filament-change mark, of either kind.
pub(crate) fn holds_change_mark(spool: &SpoolRecord, print_id: PrintId) -> bool {
    marks_of(spool).iter().any(|reason| {
        matches!(
            reason,
            UnverifiedReason::FilamentChangeUnconfirmed { print_id: held }
            | UnverifiedReason::FilamentChangeUnconfirmedPosition { print_id: held }
            if *held == print_id
        )
    })
}

/// Whether a spool holds any filament-change mark at all.
pub(crate) fn holds_any_change_mark(spool: &SpoolRecord) -> bool {
    marks_of(spool).iter().any(|reason| {
        matches!(
            reason,
            UnverifiedReason::FilamentChangeUnconfirmed { .. }
                | UnverifiedReason::FilamentChangeUnconfirmedPosition { .. }
        )
    })
}

/// Every reason a spool's figure is in doubt, oldest first.
pub(crate) fn marks_of(spool: &SpoolRecord) -> Vec<UnverifiedReason> {
    match &spool.trust {
        SpoolTrust::Verified => Vec::new(),
        SpoolTrust::Unverified { marks } => marks.iter().map(|mark| mark.reason.clone()).collect(),
    }
}

/// The spool loaded on one tool, when one is. A retired spool is on no tool:
/// retiring one loaded mid-print charged it for what it fed up to then.
pub(crate) fn spool_on_tool(spools: &[SpoolRecord], tool: i64) -> Option<&SpoolRecord> {
    spools
        .iter()
        .find(|spool| spool.loaded_tool() == Some(tool))
}

/// The open position a job read reports for a print, when it reports one.
pub(crate) fn position_from(
    job: &JobSnapshot,
    print: &PrintRecord,
    observed_at: Timestamp,
) -> Option<FilePosition> {
    let active = matches!(job.state, PrinterState::Printing | PrinterState::Paused);
    let same_file = print.file_name.is_none() || job.file_name == print.file_name;
    if !active || !same_file {
        return None;
    }
    // A position before the file's start is no position, and a size of
    // nothing is no size: neither is kept.
    job.file_position_bytes
        .filter(|bytes| *bytes >= 0)
        .map(|bytes| FilePosition {
            bytes,
            size_bytes: job.size_bytes.filter(|size| *size > 0),
            observed_at,
        })
}

/// What a tracked print's close-out writes: its charges, its pends and its
/// marks, in that order.
type ClosingWrites = (Vec<Charge>, Vec<Pend>, Vec<SpoolMutation>);

/// What a tracked print's close-out read.
#[derive(Debug, Clone)]
struct Closing {
    /// How the print ended.
    outcome: PrintOutcome,
    /// Whether anything was watching as it ended: unfinished and unwatched,
    /// what it fed after the last position recorded is unknown.
    watch: Watch,
    /// What each tool fed by the outcome's rule, when that could be worked out.
    totals: Option<Extrusion>,
    /// What the printer says of the print's file.
    analysis: FileAnalysis,
    /// The last position recorded.
    position: Option<FilePosition>,
}

/// How a print ended, from everything its close-out read.
#[derive(Debug, Clone, Copy)]
struct Ending<'a> {
    /// The state the printer was in as it ended.
    state: &'a PrinterState,
    /// The last job read, when one was made.
    job: Option<&'a JobSnapshot>,
    /// The print's file, when it is known as a name a file API takes.
    file_name: Option<&'a FileName>,
    /// The file's size, when it is known.
    size_bytes: Option<i64>,
    /// What the file's tally says of a print since this one was opened.
    tally: Tally,
    /// Who cancelled it, as far as this system knows.
    cancelled_by: Canceller,
    /// What the printer pushed during the stop.
    pushes: PushFacts,
}

/// How a print's closing charge is worked out, by the outcome it ended with.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum ChargeRule {
    /// The length the file's analysis gives: the print ran to its end.
    SlicerEstimate,
    /// What the file's moves extruded up to the last file position seen.
    ExtrudedToLastPosition,
}

/// The rule one outcome's closing charge is worked out by.
///
/// Only a completed print is charged its whole analysed length. Every other
/// outcome stopped somewhere short of the end, so it is charged what the file
/// extruded up to where it was last seen — an interrupted print exactly as a
/// failed one.
#[must_use]
pub const fn charge_rule(outcome: PrintOutcome) -> ChargeRule {
    match outcome {
        PrintOutcome::Completed => ChargeRule::SlicerEstimate,
        PrintOutcome::Cancelled | PrintOutcome::Failed | PrintOutcome::Interrupted => {
            ChargeRule::ExtrudedToLastPosition
        }
    }
}

/// Whether a print was watched to its end.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
enum Watch {
    /// This supervisor saw it running, or it completed: what it fed is known.
    Watched,
    /// It ended unfinished while nothing was watching.
    Unwatched,
}

/// What a file's tally says of a print since one was opened.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
enum Tally {
    /// A successful print of the file was recorded since, and this
    /// supervisor did not observe the stop: the one way a print is completed
    /// without a job read showing the finish.
    CompletedSince,
    /// Nothing the tally says shows this print completed, or the stop was
    /// observed and its job reads decide it.
    NoCompletion,
}

/// Who cancelled a print, as far as this system knows.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
enum Canceller {
    /// This system's own cancel was executed for it.
    Here,
    /// No cancel of this system's was executed for it.
    NotHere,
}

impl Ending<'_> {
    /// The outcome these say the print ended with.
    fn outcome(&self) -> PrintOutcome {
        if self.read_finished() || self.tally == Tally::CompletedSince {
            return PrintOutcome::Completed;
        }
        if self.cancelled_by == Canceller::Here {
            return PrintOutcome::Cancelled;
        }
        match self.state {
            PrinterState::Error | PrinterState::Offline => {
                if self.printer_errored() {
                    PrintOutcome::Failed
                } else {
                    PrintOutcome::Interrupted
                }
            }
            _ if self.pushes.reset => PrintOutcome::Interrupted,
            _ => PrintOutcome::Cancelled,
        }
    }

    /// Whether the job read at the stop shows the print finished: one read of
    /// the print's own file reporting both completion 100.0 and a file
    /// position equal to the file's size.
    ///
    /// Neither alone is a finish. A position at the end is where a print
    /// stands while its last moves still run, and a completion of 100.0 is
    /// a rounding of a print short of its end; only the two in one read are
    /// what `OctoPrint` reports of a print it finished.
    fn read_finished(&self) -> bool {
        self.job.is_some_and(|job| {
            let same_file = self
                .file_name
                .is_none_or(|file| job.file_name.as_deref() == Some(file.as_str()));
            let complete = job
                .completion
                .is_some_and(|completion| completion.value() >= 1.0);
            let size = job.size_bytes.or(self.size_bytes).filter(|size| *size > 0);
            let at_the_end = job
                .file_position_bytes
                .zip(size)
                .is_some_and(|(bytes, size)| bytes == size);
            same_file && complete && at_the_end
        })
    }

    /// Whether a stop that went offline or into `error` was the printer's own
    /// error.
    ///
    /// The push channel says so outright: the printer pushes an error of its
    /// own for exactly that stop, and for no serial drop or reset. Only when
    /// the channel was down during the stop is the job's error read instead,
    /// by whose its adapter says it is.
    fn printer_errored(&self) -> bool {
        if self.pushes.firmware_error {
            return true;
        }
        self.pushes.gap
            && self
                .job
                .and_then(|job| job.error.as_ref())
                .is_some_and(|report| report.source == ErrorSource::Printer)
    }
}

impl Supervisor {
    /// One event draft this system writes now, about a print or about none.
    pub(crate) fn draft_of<P: EventPayload>(
        &self,
        print_id: Option<PrintId>,
        source: EventSource,
        payload: &P,
    ) -> Result<EventDraft, CoreError> {
        Ok(EventDraft {
            print_id,
            source,
            received_at: self.clock().now(),
            body: EventBody::of(payload)?,
            raw: None,
        })
    }

    /// The charge one feed makes against a spool, or records uncharged with no
    /// spool, carrying its `filament_consumed` event.
    pub(crate) fn charge_of(
        &self,
        feed: Feed,
        spool: Option<&SpoolRecord>,
        figures: &mut Figures,
        source: EventSource,
    ) -> Result<Charge, CoreError> {
        let charged = spool.map(|spool| {
            let grams = grams_for_length(feed.length_mm, spool.diameter_mm, spool.density_g_cm3);
            let (remaining_before_g, remaining_after_g) = figures.debit(spool, grams);
            ChargedFilament {
                spool_id: spool.id,
                grams,
                diameter_mm: spool.diameter_mm,
                density_g_cm3: spool.density_g_cm3,
                remaining_before_g,
                remaining_after_g,
            }
        });
        let payload = FilamentConsumedPayload {
            charge_key: feed.key.clone(),
            tool: feed.key.tool(),
            method: feed.method,
            length_mm: feed.length_mm,
            charged: charged.clone(),
            analysed_length_mm: feed.analysed_length_mm,
            position: feed.position.clone(),
            note: feed.note.clone(),
        };
        let event = self.draft_of(feed.key.watched_print(), source, &payload)?;
        Ok(Charge {
            identity: ChargeIdentity::of(feed.key),
            length_mm: feed.length_mm,
            charged,
            at: event.received_at,
            event,
        })
    }

    /// The pend one feed makes, held for the next spool loaded onto its tool,
    /// carrying its `charge_pended` event.
    pub(crate) fn pend_of(&self, feed: Feed) -> Result<Pend, CoreError> {
        let Some(charge_key) = feed.key.print_segment() else {
            return Err(CoreError::Unrepresentable {
                detail: format!(
                    "{} is no print segment, and only a watched print's filament is held",
                    feed.key.canonical()
                ),
            });
        };
        let print_id = charge_key.print_id;
        let pending = PendingCharge {
            charge_key,
            length_mm: feed.length_mm,
            method: feed.method,
            analysed_length_mm: feed.analysed_length_mm,
            position: feed.position,
            note: feed.note,
            pended_at: self.clock().now(),
        };
        let event = self.draft_of(
            Some(print_id),
            system_source(),
            &ChargePendedPayload {
                pending: pending.clone(),
            },
        )?;
        Ok(Pend { pending, event })
    }

    /// The mutation that marks one spool's figure as in doubt, with its
    /// `spool_unverified` event.
    pub(crate) fn mark_of(
        &self,
        spool_id: SpoolId,
        reason: UnverifiedReason,
        print_id: Option<PrintId>,
    ) -> Result<SpoolMutation, CoreError> {
        let event = self.draft_of(
            print_id,
            system_source(),
            &SpoolUnverifiedPayload {
                spool_id,
                reason: reason.clone(),
            },
        )?;
        Ok(SpoolMutation {
            spool_id,
            at: event.received_at,
            change: SpoolChange::MarkUnverified { reason, event },
        })
    }

    /// Every pending charge a spool takes, each charged to it under its own
    /// key and named for the command's own event.
    pub(crate) fn take_pending(
        &self,
        pending: &[PendingCharge],
        spool: &SpoolRecord,
        figures: &mut Figures,
        source: &EventSource,
    ) -> Result<(Vec<Charge>, Vec<PendingTaken>), CoreError> {
        let mut charges = Vec::new();
        let mut taken = Vec::new();
        for held in pending {
            let note = match &held.note {
                Some(note) => format!("{TAKEN_NOTE}; {note}"),
                None => TAKEN_NOTE.to_owned(),
            };
            charges.push(self.charge_of(
                Feed {
                    key: held.charge_key.charge_key(),
                    method: held.method,
                    length_mm: held.length_mm,
                    analysed_length_mm: held.analysed_length_mm,
                    position: held.position.clone(),
                    note: Some(note),
                },
                Some(spool),
                figures,
                source.clone(),
            )?);
            taken.push(PendingTaken {
                charge_key: held.charge_key.charge_key(),
                print_id: held.print_id(),
            });
        }
        Ok((charges, taken))
    }

    /// What one print has had charged and held so far.
    pub(crate) async fn print_ledger(&self, print_id: PrintId) -> Result<PrintLedger, CoreError> {
        let charges = self.stores().filament.charges(print_id).await?;
        let pending = self
            .stores()
            .filament
            .pending_charges(None)
            .await?
            .into_iter()
            .filter(|held| held.print_id() == print_id)
            .collect();
        Ok(PrintLedger { charges, pending })
    }

    /// The `print_started` a print was opened with, when it has one.
    pub(crate) async fn print_started_of(
        &self,
        print_id: PrintId,
    ) -> Result<Option<PrintStartedPayload>, CoreError> {
        let found = self
            .stores()
            .events
            .history(HistoryQuery {
                print_id,
                kinds: vec![PrintStartedPayload::kind()],
                since: None,
                until: None,
                limit: Some(1),
            })
            .await?;
        Ok(found
            .first()
            .and_then(printobserver_types::EventRecord::payload_as::<PrintStartedPayload>)
            .transpose()?)
    }

    /// Write a print's `print_started`, naming the spool on each tool now.
    // llmlint: ignore[invalid_states_unrepresentable] suppressions.toml has the reason.
    pub(crate) async fn record_print_started(
        &self,
        print_id: PrintId,
        file_name: Option<String>,
        started_by: Option<ActionId>,
        preflight: Option<Preflight>,
    ) -> Result<(), CoreError> {
        self.forget_pushes();
        let loaded = self
            .stores()
            .filament
            .loaded_spools()
            .await?
            .into_iter()
            .filter_map(|spool| {
                spool.loaded_tool().map(|tool| LoadedSpool {
                    tool,
                    spool_id: spool.id,
                })
            })
            .collect();
        let draft = self.draft_of(
            Some(print_id),
            system_source(),
            &PrintStartedPayload {
                file_name,
                started_by,
                loaded,
                preflight,
            },
        )?;
        self.stores().events.append_event(draft).await?;
        Ok(())
    }

    /// Record how far into its file a print had got, from one job read.
    pub(crate) async fn record_position_from(
        &self,
        print: &PrintRecord,
        job: &JobSnapshot,
        observed_at: Timestamp,
    ) -> Result<Option<FilePosition>, CoreError> {
        let Some(position) = position_from(job, print, observed_at) else {
            return Ok(None);
        };
        self.stores()
            .filament
            .record_file_position(print.id, position.clone())
            .await?;
        Ok(Some(position))
    }

    /// What the printer says of one file: its facts, and the analysed length
    /// per tool from the job read when it carries one, otherwise from the
    /// file's own facts.
    pub(crate) async fn analysis_of(
        &self,
        file_name: &FileName,
        job: Option<&JobSnapshot>,
    ) -> FileAnalysis {
        let facts = self.read_file_facts(file_name.clone()).await.ok();
        let from_job = job
            .filter(|job| {
                job.file_name.as_deref() == Some(file_name.as_str()) && !job.filament.is_empty()
            })
            .map(|job| lengths_of(&job.filament));
        let lengths = from_job.or_else(|| {
            facts
                .as_ref()
                .and_then(|facts| facts.filament.as_deref().map(lengths_of))
        });
        FileAnalysis { facts, lengths }
    }

    /// The filament each tool fed up to a position: summed from the file's own
    /// moves, or — when the file could not be read — the analysed length
    /// scaled by how far into the file the position is, with a note saying
    /// why. Nothing when neither can be worked out.
    pub(crate) async fn extrusion_to(
        &self,
        file_name: &FileName,
        position: &FilePosition,
        analysis: &FileAnalysis,
    ) -> Option<Extrusion> {
        let refusal = match self
            .read_extruded_to(file_name.clone(), position.bytes)
            .await
        {
            Ok(filament) => {
                return Some(Extrusion {
                    lengths: lengths_of(&filament),
                    method: ChargeMethod::GcodeExtrusion,
                    note: None,
                });
            }
            Err(error) => error.to_string(),
        };
        let analysed = analysis.lengths.as_ref()?;
        let size = position
            .size_bytes
            .or_else(|| analysis.size_bytes())
            .filter(|size| *size > 0)?;
        #[expect(clippy::cast_precision_loss)]
        let fraction = (position.bytes as f64 / size as f64).clamp(0.0, 1.0);
        Some(Extrusion {
            lengths: analysed
                .iter()
                .map(|(tool, length)| (*tool, length * fraction))
                .collect(),
            method: ChargeMethod::FileFraction,
            note: Some(format!(
                "the file could not be read ({refusal}), so the analysed length was scaled by \
                 how far into the file the print got"
            )),
        })
    }

    /// Whether this system's own cancel was executed for a print.
    async fn cancelled_here(&self, print_id: PrintId) -> Result<bool, CoreError> {
        let events = 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> = events
            .iter()
            .filter_map(printobserver_types::EventRecord::payload_as::<ActionExecutedPayload>)
            .filter_map(Result::ok)
            .map(|payload| payload.action_id)
            .collect();
        Ok(events
            .iter()
            .filter_map(printobserver_types::EventRecord::payload_as::<ActionRequestedPayload>)
            .filter_map(Result::ok)
            .any(|payload| {
                matches!(payload.action, PrintAction::Cancel { .. })
                    && executed.contains(&payload.action_id)
            }))
    }

    /// Settle one print's filament as it is closed out, unless it is settled
    /// already.
    ///
    /// A print with no `print_started` predates spool tracking: it is settled
    /// with no charge and one `print_untracked` — when a job was ever seen for
    /// it at all; a print a refused start opened and nothing ever ran is not
    /// a print of the timeline. Any other print is charged by the rule of its
    /// outcome, as this module states.
    pub(crate) async fn settle_filament(
        &self,
        print: &PrintRecord,
        state: &PrinterState,
        reason: &str,
        closing_job: Option<&JobSnapshot>,
    ) -> Result<(), CoreError> {
        if self
            .stores()
            .filament
            .print_settlement(print.id)
            .await?
            .is_some()
        {
            return Ok(());
        }
        let Some(started) = self.print_started_of(print.id).await? else {
            return self
                .settle_untracked(print, state, reason, closing_job)
                .await;
        };
        let closing = self.closing(print, &started, state, closing_job).await?;
        let ended = self.draft_of(
            Some(print.id),
            system_source(),
            &PrintEndedPayload {
                outcome: closing.outcome,
                state: state.clone(),
                reason: reason.to_owned(),
                last_position: closing.position.clone(),
            },
        )?;
        let _writing = self.spool_writes().await;
        let spools = self.stores().filament.spools(true).await?;
        let ledger = self.print_ledger(print.id).await?;
        let mut used: BTreeSet<i64> = ledger.tools();
        used.extend(started.loaded.iter().map(|loaded| loaded.tool));
        let (charges, pends, marks) =
            self.closing_writes(print.id, used, &closing, &spools, &ledger)?;
        self.stores()
            .filament
            .settle_print(print.id, closing.outcome, ended, charges, pends, marks)
            .await?;
        Ok(())
    }

    /// Everything a tracked print's close-out reads before it works out what
    /// to charge: how it ended, and what each tool fed by that outcome's rule.
    async fn closing(
        &self,
        print: &PrintRecord,
        started: &PrintStartedPayload,
        state: &PrinterState,
        closing_job: Option<&JobSnapshot>,
    ) -> Result<Closing, CoreError> {
        let read = if closing_job.is_none() {
            self.read_job().await.ok()
        } else {
            None
        };
        let job = closing_job.or(read.as_ref());
        let file_name = print
            .file_name
            .as_deref()
            .or(started.file_name.as_deref())
            .and_then(|name| FileName::new(name).ok());
        let analysis = match &file_name {
            Some(file) => self.analysis_of(file, job).await,
            None => FileAnalysis::default(),
        };
        let position = self.stores().filament.file_position(print.id).await?;
        // A print completes after it was opened, whoever opened it; the job's
        // own start, as a read puts it, can lie before that, and a success
        // recorded between the two is an earlier print's.
        //
        // That record is read only for a stop this supervisor did not observe
        // — it was not running when the print ended, so no job read of the
        // finish was made. A stop it watched is completed by its job reads
        // alone.
        let began = print.opened_at;
        let unobserved = !self.was_seen_running(print.id);
        let tally = if unobserved
            && analysis
                .facts
                .as_ref()
                .and_then(|facts| facts.last_print.as_ref())
                .is_some_and(|last| last.success && last.at >= began)
        {
            Tally::CompletedSince
        } else {
            Tally::NoCompletion
        };
        let cancelled_by = if self.cancelled_here(print.id).await? {
            Canceller::Here
        } else {
            Canceller::NotHere
        };
        let ending = |pushes| {
            Ending {
                state,
                job,
                file_name: file_name.as_ref(),
                size_bytes: analysis.size_bytes(),
                tally,
                cancelled_by,
                pushes,
            }
            .outcome()
        };
        let mut outcome = ending(self.push_facts());
        if outcome == PrintOutcome::Cancelled && cancelled_by == Canceller::NotHere {
            outcome = ending(self.pushes_after_a_reset_could_arrive().await);
        }
        let rule = charge_rule(outcome);
        let totals = match (rule, &file_name, &position) {
            (ChargeRule::SlicerEstimate, _, _) => {
                analysis.lengths.clone().map(|lengths| Extrusion {
                    lengths,
                    method: ChargeMethod::SlicerEstimate,
                    note: None,
                })
            }
            (ChargeRule::ExtrudedToLastPosition, Some(file), Some(position)) => {
                self.extrusion_to(file, position, &analysis).await
            }
            (ChargeRule::ExtrudedToLastPosition, _, _) => None,
        };
        Ok(Closing {
            outcome,
            watch: if self.was_seen_running(print.id) || rule == ChargeRule::SlicerEstimate {
                Watch::Watched
            } else {
                Watch::Unwatched
            },
            totals,
            analysis,
            position,
        })
    }

    /// What a tracked print's close-out writes: each tool's remainder charged
    /// to the spool on it, or held when the tool's last change is unconfirmed,
    /// and every spool whose figure the print leaves in doubt marked.
    fn closing_writes(
        &self,
        print_id: PrintId,
        mut used: BTreeSet<i64>,
        closing: &Closing,
        spools: &[SpoolRecord],
        ledger: &PrintLedger,
    ) -> Result<ClosingWrites, CoreError> {
        let mut figures = Figures::default();
        let mut charges = Vec::new();
        let mut pends = Vec::new();
        let mut marks = Vec::new();
        let uncharged = UnverifiedReason::UnchargedPrint { print_id };
        let Some(totals) = &closing.totals else {
            for tool in used {
                if let Some(spool) = spool_on_tool(spools, tool) {
                    marks.push(self.mark_of(spool.id, uncharged.clone(), Some(print_id))?);
                }
            }
            return Ok((charges, pends, marks));
        };
        used.extend(totals.lengths.keys().copied());
        for tool in used {
            let on_tool = spool_on_tool(spools, tool);
            if closing.watch == Watch::Unwatched
                && let Some(spool) = on_tool
            {
                marks.push(self.mark_of(spool.id, uncharged.clone(), Some(print_id))?);
            }
            let total = totals.lengths.get(&tool).copied().unwrap_or(0.0);
            let remainder = total - ledger.already_mm(tool);
            if remainder <= NOTHING_MM {
                continue;
            }
            let feed = Feed {
                key: ChargeKey::Print {
                    print_id,
                    tool,
                    segment: ledger.open_segment(tool),
                },
                method: totals.method,
                length_mm: remainder,
                analysed_length_mm: closing.analysis.length(tool),
                position: closing.position.clone(),
                note: totals.note.clone(),
            };
            let judged = on_tool.or_else(|| {
                ledger
                    .last_spool(tool)
                    .and_then(|id| spools.iter().find(|spool| spool.id == id))
            });
            if judged.is_some_and(|spool| holds_change_mark(spool, print_id)) {
                pends.push(self.pend_of(feed)?);
            } else {
                charges.push(self.charge_of(feed, on_tool, &mut figures, system_source())?);
            }
        }
        Ok((charges, pends, marks))
    }

    /// Settle a print with no `print_started`: no charge, one `print_untracked`
    /// and one `print_ended`, when a job was ever seen for it.
    async fn settle_untracked(
        &self,
        print: &PrintRecord,
        state: &PrinterState,
        reason: &str,
        closing_job: Option<&JobSnapshot>,
    ) -> Result<(), CoreError> {
        let ran = print.provider_print_id.is_some()
            || print.job_started_at.is_some()
            || print.job_print_time_s.is_some();
        if !ran {
            return Ok(());
        }
        let named = print
            .file_name
            .as_deref()
            .and_then(|name| FileName::new(name).ok());
        let ending = |pushes| {
            Ending {
                state,
                job: closing_job,
                file_name: named.as_ref(),
                size_bytes: None,
                tally: Tally::NoCompletion,
                cancelled_by: Canceller::NotHere,
                pushes,
            }
            .outcome()
        };
        let mut outcome = ending(self.push_facts());
        if outcome == PrintOutcome::Cancelled {
            outcome = ending(self.pushes_after_a_reset_could_arrive().await);
        }
        // The `print_untracked` goes first, once: a close-out replayed after
        // it — the settlement refused, or committed with its answer lost —
        // finds it written and writes no second, and one replayed after the
        // settlement finds that and writes nothing at all.
        let written = self
            .stores()
            .events
            .history(HistoryQuery {
                print_id: print.id,
                kinds: vec![PrintUntrackedPayload::kind()],
                since: None,
                until: None,
                limit: Some(1),
            })
            .await?;
        if written.is_empty() {
            let draft = self.draft_of(
                Some(print.id),
                system_source(),
                &PrintUntrackedPayload {
                    reason: UNTRACKED_REASON.to_owned(),
                },
            )?;
            self.stores().events.append_event(draft).await?;
        }
        let ended = self.draft_of(
            Some(print.id),
            system_source(),
            &PrintEndedPayload {
                outcome,
                state: state.clone(),
                reason: reason.to_owned(),
                last_position: None,
            },
        )?;
        self.stores()
            .filament
            .settle_print(print.id, outcome, ended, Vec::new(), Vec::new(), Vec::new())
            .await?;
        Ok(())
    }

    /// The print a spool command made now happens during, when one is: the
    /// most recently opened open print that is tracked and not yet settled.
    pub(crate) async fn print_during(&self) -> Result<Option<PrintRecord>, CoreError> {
        for print in self.stores().prints.open_prints().await? {
            if self
                .stores()
                .filament
                .print_settlement(print.id)
                .await?
                .is_none()
                && self.print_started_of(print.id).await?.is_some()
            {
                return Ok(Some(print));
            }
        }
        Ok(None)
    }

    /// Where a print is now: read afresh from the job when it reports one,
    /// recorded as it is read, and otherwise the last position recorded —
    /// with the job the read found, when it found one.
    pub(crate) async fn position_now(
        &self,
        print: &PrintRecord,
    ) -> Result<(Whereabouts, Option<JobSnapshot>), CoreError> {
        let job = self.read_job().await.ok();
        let observed_at = self.clock().now();
        if let Some(job) = &job
            && let Some(position) = self.record_position_from(print, job, observed_at).await?
        {
            self.note_seen_running(print.id);
            return Ok((Whereabouts::ReadNow(position), Some(job.clone())));
        }
        let whereabouts = match self.stores().filament.file_position(print.id).await? {
            Some(recorded) => Whereabouts::Recorded(recorded),
            None => Whereabouts::Unknown,
        };
        Ok((whereabouts, job))
    }

    /// The filament each tool fed up to where a print is now, and that
    /// position — what a spool command made during it charges against.
    pub(crate) async fn extrusion_now(
        &self,
        print: &PrintRecord,
    ) -> Result<Option<(FilePosition, Extrusion, FileAnalysis)>, CoreError> {
        let Some(file_name) = file_of(print) else {
            return Ok(None);
        };
        let (whereabouts, job) = self.position_now(print).await?;
        let Some(position) = whereabouts.position().cloned() else {
            return Ok(None);
        };
        let analysis = self.analysis_of(&file_name, job.as_ref()).await;
        Ok(self
            .extrusion_to(&file_name, &position, &analysis)
            .await
            .map(|extrusion| (position, extrusion, analysis)))
    }

    /// Handle what the printer itself signalled, as an alert relayed it.
    ///
    /// A filament change reads the job afresh — the printer is paused at the
    /// change, so that is where the spool stopped feeding — charges the spool
    /// on each tool up to it, writes `filament_change_signalled`, and marks
    /// the spool as one whose change nobody has confirmed. When no fresh
    /// position can be read it charges up to the last position recorded and
    /// says so, with the mark that says its figure may be overstated. A spool
    /// already holding the print's mark — a second signal before any load —
    /// has its open segment pended instead, and is marked again.
    pub(crate) async fn handle_signal(
        &self,
        print: &PrintRecord,
        signal: PrinterSignal,
        source_kind: &EventKind,
    ) -> Result<(), CoreError> {
        let PrinterSignal::FilamentChange = signal;
        if print.ended_at.is_some()
            || self
                .stores()
                .filament
                .print_settlement(print.id)
                .await?
                .is_some()
            || self.print_started_of(print.id).await?.is_none()
        {
            return Ok(());
        }
        let (whereabouts, job) = self.position_now(print).await?;
        let read_now = matches!(whereabouts, Whereabouts::ReadNow(_));
        let position = whereabouts.position().cloned();
        let signal_position = position.clone().map(|position| SignalPosition {
            position,
            freshness: if read_now {
                PositionFreshness::ReadAtSignal
            } else {
                PositionFreshness::LastRecorded
            },
        });
        let file_name = file_of(print);
        let analysis = match &file_name {
            Some(file) => self.analysis_of(file, job.as_ref()).await,
            None => FileAnalysis::default(),
        };
        let extrusion = match (&file_name, &position) {
            (Some(file), Some(position)) => self.extrusion_to(file, position, &analysis).await,
            _ => None,
        };
        let signalled = |tool: Option<i64>, spool_id: Option<SpoolId>| {
            self.draft_of(
                Some(print.id),
                system_source(),
                &FilamentChangeSignalledPayload {
                    tool,
                    spool_id,
                    source_kind: source_kind.clone(),
                    position: signal_position.clone(),
                },
            )
        };
        let reason = if read_now {
            UnverifiedReason::FilamentChangeUnconfirmed { print_id: print.id }
        } else {
            UnverifiedReason::FilamentChangeUnconfirmedPosition { print_id: print.id }
        };

        let _writing = self.spool_writes().await;
        let loaded = self.stores().filament.loaded_spools().await?;
        if loaded.is_empty() {
            self.stores()
                .events
                .append_event(signalled(None, None)?)
                .await?;
            return Ok(());
        }
        let ledger = self.print_ledger(print.id).await?;
        let mut figures = Figures::default();
        let mut charges = Vec::new();
        let mut pends = Vec::new();
        let mut marks = Vec::new();
        for spool in &loaded {
            let Some(tool) = spool.loaded_tool() else {
                continue;
            };
            self.stores()
                .events
                .append_event(signalled(Some(tool), Some(spool.id))?)
                .await?;
            let feed = match (&extrusion, &position) {
                (Some(extrusion), Some(position)) => {
                    feed_to(print.id, tool, &ledger, extrusion, position, &analysis)
                }
                _ => None,
            };
            match feed {
                Some(feed) if holds_change_mark(spool, print.id) => {
                    pends.push(self.pend_of(feed)?);
                }
                Some(feed) => {
                    charges.push(self.charge_of(
                        feed,
                        Some(spool),
                        &mut figures,
                        system_source(),
                    )?);
                }
                None => {}
            }
            marks.push(self.mark_of(spool.id, reason.clone(), Some(print.id))?);
        }
        self.stores()
            .filament
            .apply_spool_changes(marks, charges, pends)
            .await?;
        Ok(())
    }
}