printobserver-core 0.4.0

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
//! The spool ledger's commands and reads, through the one granted path.
//!
//! A spool command is an action of its own kind: the safety envelope grants it
//! per actor class exactly as it grants a print action, an ungranted one is
//! refused [`ActorMayNotRequest`](RejectionReason::ActorMayNotRequest), and
//! one the ledger cannot carry out is refused
//! [`SpoolRefused`](RejectionReason::SpoolRefused) naming why. Every refusal
//! writes a `spool_command_rejected`, and every accepted command writes its
//! spool events through one store write carrying who asked and why — so a
//! remaining figure can be traced to every print and correction behind it.
//!
//! Every command is carried out under the supervisor's one spool lock, so two
//! commands — or a command and a settlement — never compute from the same
//! stale figure.
//!
//! # A load mid-print, and a load after an unconfirmed change
//!
//! A load or unload during a print first charges what the tool fed up to the
//! position read now to the spool that was feeding it, and opens the next
//! segment for the spool loaded; the close-out charges that spool only the
//! remainder. After an unconfirmed filament change the open segment is not the
//! outgoing spool's: a load of another spool takes every pending charge on the
//! tool and the open post-signal segment to that point, and a load naming the
//! spool already there confirms it was never swapped — taking the pending
//! charges, clearing its filament-change marks and charging nothing for the
//! open segment, which the close-out charges to it as the remainder.

use printobserver_types::{EventRecord, EventSource, PrintId};

use crate::accounting::{
    AFTER_CHANGE, Figures, feed_to, holds_any_change_mark, holds_change_mark, spool_on_tool,
};
use crate::actions::source_of;
use crate::error::CoreError;
use crate::filament::{DEFAULT_DIAMETER_MM, MarkClearing, density_for, marks_cleared_by};
use crate::kinds::{
    SpoolAdjustedPayload, SpoolCommandRejectedPayload, SpoolLoadedPayload, SpoolRetiredPayload,
    SpoolUnloadedPayload,
};
use crate::records::{
    ClearedMark, PolicyDecision, RejectionReason, SpoolCommand, SpoolId, SpoolRecord, SpoolTrust,
};
use crate::store::{
    Charge, EventDraft, NewSpool, SpoolChange, SpoolMutation, StoreError, TimelineQuery,
};
use crate::supervisor::Supervisor;

/// What became of one spool command.
#[derive(Debug, Clone, PartialEq)]
pub struct SpoolCommandOutcome {
    /// The decision taken on it.
    pub decision: PolicyDecision,
    /// The spool it was about, as the command left it, when there is one.
    pub spool: Option<SpoolRecord>,
    /// Every event it wrote, in the order they were written.
    pub events: Vec<EventRecord>,
}

/// A refusal of a spool command, saying why and what to do instead.
fn refused(detail: impl Into<String>) -> RejectionReason {
    RejectionReason::SpoolRefused {
        detail: detail.into(),
    }
}

/// Whether a figure is a finite number at or above zero.
fn non_negative(value: f64) -> bool {
    value.is_finite() && value >= 0.0
}

/// Whether a figure is a finite number above zero.
fn positive(value: f64) -> bool {
    value.is_finite() && value > 0.0
}

/// The marks one spool loses to a clearing, named against it.
fn cleared_on(spool: &SpoolRecord, clearing: MarkClearing) -> Vec<ClearedMark> {
    let (_, removed) = spool.trust.clone().cleared(marks_cleared_by(clearing));
    removed
        .into_iter()
        .map(|reason| ClearedMark {
            spool_id: spool.id,
            reason,
        })
        .collect()
}

/// The mutation clearing one spool's marks of one clearing's kinds.
///
/// It writes no event of its own — the command's own event names the marks it
/// cleared — so it carries that event's draft, which the store does not write
/// for it.
fn clearing(spool_id: SpoolId, clearing: MarkClearing, command: &EventDraft) -> SpoolMutation {
    SpoolMutation {
        spool_id,
        change: SpoolChange::ClearMarks {
            kinds: marks_cleared_by(clearing).to_vec(),
        },
        at: command.received_at,
        event: command.clone(),
    }
}

/// What one accepted command asks the store to write.
#[derive(Debug, Default)]
struct Write {
    /// The spool mutations, in order.
    mutations: Vec<SpoolMutation>,
    /// The charges, in order.
    charges: Vec<Charge>,
}

impl Supervisor {
    /// Carry out one spool command, through the same grant every action passes.
    ///
    /// # Errors
    ///
    /// Returns the store's own error when the command or its refusal could not
    /// be written. A refusal is not an error: it is written down and answered
    /// in [`SpoolCommandOutcome::decision`].
    // llmlint: ignore[changed_behavior_has_e2e] suppressions.toml has the reason.
    pub async fn request_spool_command(
        &self,
        command: SpoolCommand,
    ) -> Result<SpoolCommandOutcome, CoreError> {
        let actor = command.actor().clone();
        let class = actor.class();
        let kind = command.kind();
        let granted = self
            .config()
            .envelope
            .actions
            .get(&class)
            .is_some_and(|kinds| kinds.contains(&kind));
        if !granted {
            let reason = RejectionReason::ActorMayNotRequest {
                actor_class: class,
                action: kind,
            };
            return self.refuse(command, reason).await;
        }
        let _writing = self.spool_writes().await;
        let during = self.print_during().await?.map(|print| print.id);
        let carried = match &command {
            SpoolCommand::RegisterSpool { .. } => self.register(&command).await?,
            SpoolCommand::LoadSpool { spool_id, tool, .. } => {
                self.load(&command, *spool_id, *tool, during).await?
            }
            SpoolCommand::UnloadSpool { spool_id, .. } => {
                self.unload(&command, *spool_id, during).await?
            }
            SpoolCommand::AdjustSpool {
                spool_id,
                remaining_g,
                gross_g,
                ..
            } => {
                self.adjust(&command, *spool_id, *remaining_g, *gross_g, during)
                    .await?
            }
            SpoolCommand::RetireSpool { spool_id, .. } => {
                self.retire(&command, *spool_id, during).await?
            }
        };
        match carried {
            Ok(outcome) => Ok(outcome),
            Err(reason) => self.refuse(command, reason).await,
        }
    }

    /// Every spool, retired ones only when asked for, most recently registered
    /// first.
    ///
    /// # Errors
    ///
    /// Returns the store's own error when the spools could not be read.
    // llmlint: ignore[invalid_states_unrepresentable] suppressions.toml has the reason.
    pub async fn spools(&self, include_retired: bool) -> Result<Vec<SpoolRecord>, CoreError> {
        Ok(self.stores().filament.spools(include_retired).await?)
    }

    /// One spool, retired or not.
    ///
    /// # Errors
    ///
    /// Returns the store's own error when the spool could not be read.
    pub async fn spool(&self, spool_id: SpoolId) -> Result<Option<SpoolRecord>, CoreError> {
        Ok(self.stores().filament.spool(spool_id).await?)
    }

    /// The printer timeline, newest first.
    ///
    /// # Errors
    ///
    /// Returns the store's own error when the timeline could not be read, a
    /// kind outside the timeline or a limit above the maximum included.
    pub async fn timeline(&self, query: TimelineQuery) -> Result<Vec<EventRecord>, CoreError> {
        Ok(self.stores().timeline.timeline(query).await?)
    }

    /// Every charge held after an unconfirmed filament change — on one tool,
    /// when one is named — oldest first.
    ///
    /// # Errors
    ///
    /// Returns the store's own error when the charges could not be read.
    pub async fn pending_charges(
        &self,
        tool: Option<i64>,
    ) -> Result<Vec<crate::records::PendingCharge>, CoreError> {
        Ok(self.stores().filament.pending_charges(tool).await?)
    }

    /// Write a refused command down and answer the refusal.
    async fn refuse(
        &self,
        command: SpoolCommand,
        reason: RejectionReason,
    ) -> Result<SpoolCommandOutcome, CoreError> {
        let decision = PolicyDecision::Rejected(reason);
        let source = source_of(command.actor());
        let during = self.print_during().await?.map(|print| print.id);
        let spool = match &command {
            SpoolCommand::RegisterSpool { .. } => None,
            SpoolCommand::LoadSpool { spool_id, .. }
            | SpoolCommand::UnloadSpool { spool_id, .. }
            | SpoolCommand::AdjustSpool { spool_id, .. }
            | SpoolCommand::RetireSpool { spool_id, .. } => {
                self.stores().filament.spool(*spool_id).await?
            }
        };
        let draft = self.draft_of(
            during,
            source,
            &SpoolCommandRejectedPayload {
                command,
                decision: decision.clone(),
            },
        )?;
        let event = self.stores().events.append_event(draft).await?;
        Ok(SpoolCommandOutcome {
            decision,
            spool,
            events: vec![event],
        })
    }

    /// One spool the command names, or the refusal that it cannot be acted on.
    async fn live_spool(
        &self,
        spool_id: SpoolId,
    ) -> Result<Result<SpoolRecord, RejectionReason>, CoreError> {
        Ok(match self.stores().filament.spool(spool_id).await? {
            None => Err(refused(format!(
                "there is no spool {spool_id}; list the spools to find the one meant"
            ))),
            Some(spool) if spool.retired_at.is_some() => Err(refused(format!(
                "spool {} is retired and takes no further command; register a new spool instead",
                spool.name
            ))),
            Some(spool) => Ok(spool),
        })
    }

    /// Write one accepted command's changes and answer them.
    async fn written(
        &self,
        spool_id: SpoolId,
        write: Write,
    ) -> Result<Result<SpoolCommandOutcome, RejectionReason>, CoreError> {
        let written = match self
            .stores()
            .filament
            .apply_spool_changes(write.mutations, write.charges, Vec::new())
            .await
        {
            Ok(written) => written,
            Err(StoreError::ConstraintRefused { constraint }) => {
                return Ok(Err(refused(format!(
                    "the ledger refused the change on {constraint}"
                ))));
            }
            Err(error) => return Err(error.into()),
        };
        let spool = written
            .spools
            .into_iter()
            .find(|spool| spool.id == spool_id);
        Ok(Ok(SpoolCommandOutcome {
            decision: PolicyDecision::Accepted,
            spool,
            events: written.events,
        }))
    }

    /// Register a spool.
    async fn register(
        &self,
        command: &SpoolCommand,
    ) -> Result<Result<SpoolCommandOutcome, RejectionReason>, CoreError> {
        let SpoolCommand::RegisterSpool {
            name,
            material,
            color,
            diameter_mm,
            density_g_cm3,
            initial_g,
            remaining_g,
            tare_g,
            reason,
            actor,
        } = command
        else {
            return Ok(Err(refused("not a registration")));
        };
        let name = name.trim();
        let material = material.trim();
        if name.is_empty() {
            return Ok(Err(refused("a spool needs a name to be known by")));
        }
        if material.is_empty() {
            return Ok(Err(refused("a spool needs its material, such as PLA")));
        }
        let diameter_mm = diameter_mm.unwrap_or(DEFAULT_DIAMETER_MM);
        if !positive(diameter_mm) {
            return Ok(Err(refused(format!(
                "a diameter of {diameter_mm}mm is not one a filament has; give it in millimetres, \
                 such as 1.75"
            ))));
        }
        let Some(density_g_cm3) = density_g_cm3.or_else(|| density_for(material)) else {
            return Ok(Err(refused(format!(
                "{material} has no known density, so the spool needs one in grams per cubic \
                 centimetre"
            ))));
        };
        if !positive(density_g_cm3) {
            return Ok(Err(refused(format!(
                "a density of {density_g_cm3}g/cm3 is not one a material has"
            ))));
        }
        if !positive(*initial_g) {
            return Ok(Err(refused(format!(
                "a full spool of {initial_g}g holds nothing; give the net filament it held full"
            ))));
        }
        let remaining_g = remaining_g.unwrap_or(*initial_g);
        if !remaining_g.is_finite() {
            return Ok(Err(refused("the remaining figure is not a number")));
        }
        if tare_g.is_some_and(|tare| !non_negative(tare)) {
            return Ok(Err(refused(
                "an empty spool's weight is at least zero grams",
            )));
        }
        let spool = NewSpool {
            name: name.to_owned(),
            material: material.to_owned(),
            color: color.clone(),
            diameter_mm,
            density_g_cm3,
            initial_g: *initial_g,
            remaining_g,
            tare_g: *tare_g,
            loaded_on_tool: None,
            trust: SpoolTrust::Verified,
            registered_at: self.clock().now(),
        };
        let (record, event) = self
            .stores()
            .filament
            .register_spool(spool, source_of(actor), actor.clone(), reason.clone())
            .await?;
        Ok(Ok(SpoolCommandOutcome {
            decision: PolicyDecision::Accepted,
            spool: Some(record),
            events: vec![event],
        }))
    }

    /// Load a spool onto a tool: a confirmation in place when it is already
    /// there, a swap when another is, and either way taking what that tool
    /// holds pending.
    async fn load(
        &self,
        command: &SpoolCommand,
        spool_id: SpoolId,
        tool: i64,
        during: Option<PrintId>,
    ) -> Result<Result<SpoolCommandOutcome, RejectionReason>, CoreError> {
        if tool < 0 {
            return Ok(Err(refused(format!(
                "there is no tool {tool}; tools are numbered from 0, as the printer numbers them"
            ))));
        }
        let incoming = match self.live_spool(spool_id).await? {
            Ok(spool) => spool,
            Err(reason) => return Ok(Err(reason)),
        };
        let source = source_of(command.actor());
        let pending = self.stores().filament.pending_charges(Some(tool)).await?;
        let mut figures = Figures::default();
        let already_there = incoming.loaded_on_tool == Some(tool);
        if already_there && pending.is_empty() && !holds_any_change_mark(&incoming) {
            return Ok(Err(refused(format!(
                "spool {} is already on tool {tool}, and nothing is pending there to confirm",
                incoming.name
            ))));
        }
        let spools = self.stores().filament.spools(true).await?;
        // The spool that was feeding the tool, retired or not — a spool retired
        // while it was loaded is still charged for what it fed — and the one
        // the load takes off it, which only an unretired spool can be.
        let feeding = spool_on_tool(&spools, tool)
            .filter(|spool| spool.id != incoming.id)
            .cloned();
        let outgoing = feeding.clone().filter(|spool| spool.retired_at.is_none());
        let (mut charges, took_pending) =
            self.take_pending(&pending, &incoming, &mut figures, &source)?;
        if !already_there && let Some(print_id) = during {
            charges.extend(
                self.close_segment(
                    print_id,
                    tool,
                    feeding.as_ref(),
                    Some(&incoming),
                    &mut figures,
                    &source,
                )
                .await?,
            );
        }
        let swapped = outgoing
            .as_ref()
            .map(|spool| cleared_on(spool, MarkClearing::SwappedOut))
            .unwrap_or_default();
        let confirmed = if already_there {
            cleared_on(&incoming, MarkClearing::ConfirmedInPlace)
        } else {
            Vec::new()
        };
        let loaded = self.draft_of(
            during,
            source.clone(),
            &SpoolLoadedPayload {
                spool_id: incoming.id,
                tool,
                replaced: outgoing.as_ref().map(|spool| spool.id),
                took_pending,
                cleared_marks: swapped.iter().chain(&confirmed).cloned().collect(),
                actor: command.actor().clone(),
                reason: command.reason().to_owned(),
            },
        )?;
        let mut write = Write {
            mutations: Vec::new(),
            charges,
        };
        if let Some(outgoing) = &outgoing {
            if !swapped.is_empty() {
                write
                    .mutations
                    .push(clearing(outgoing.id, MarkClearing::SwappedOut, &loaded));
            }
            write.mutations.push(self.mutation(
                outgoing.id,
                SpoolChange::Unload,
                during,
                &source,
                &SpoolUnloadedPayload {
                    spool_id: outgoing.id,
                    tool,
                    actor: command.actor().clone(),
                    reason: command.reason().to_owned(),
                },
            )?);
        }
        if !confirmed.is_empty() {
            write.mutations.push(clearing(
                incoming.id,
                MarkClearing::ConfirmedInPlace,
                &loaded,
            ));
        }
        write.mutations.push(SpoolMutation {
            spool_id: incoming.id,
            change: SpoolChange::Load { tool },
            at: loaded.received_at,
            event: loaded,
        });
        self.written(incoming.id, write).await
    }

    /// Unload a spool from its tool, charging it first for what it fed of a
    /// print running now.
    async fn unload(
        &self,
        command: &SpoolCommand,
        spool_id: SpoolId,
        during: Option<PrintId>,
    ) -> Result<Result<SpoolCommandOutcome, RejectionReason>, CoreError> {
        let spool = match self.live_spool(spool_id).await? {
            Ok(spool) => spool,
            Err(reason) => return Ok(Err(reason)),
        };
        let Some(tool) = spool.loaded_on_tool else {
            return Ok(Err(refused(format!(
                "spool {} is not loaded on any tool, so there is nothing to unload",
                spool.name
            ))));
        };
        let source = source_of(command.actor());
        let mut figures = Figures::default();
        let mut write = Write::default();
        if let Some(print_id) = during {
            write.charges = self
                .close_segment(print_id, tool, Some(&spool), None, &mut figures, &source)
                .await?;
        }
        write.mutations.push(self.mutation(
            spool.id,
            SpoolChange::Unload,
            during,
            &source,
            &SpoolUnloadedPayload {
                spool_id: spool.id,
                tool,
                actor: command.actor().clone(),
                reason: command.reason().to_owned(),
            },
        )?);
        self.written(spool.id, write).await
    }

    /// Correct a spool's figure, taking first what its tool holds pending.
    async fn adjust(
        &self,
        command: &SpoolCommand,
        spool_id: SpoolId,
        remaining_g: Option<f64>,
        gross_g: Option<f64>,
        during: Option<PrintId>,
    ) -> Result<Result<SpoolCommandOutcome, RejectionReason>, CoreError> {
        let spool = match self.live_spool(spool_id).await? {
            Ok(spool) => spool,
            Err(reason) => return Ok(Err(reason)),
        };
        let target = match (remaining_g, gross_g) {
            (Some(_), Some(_)) | (None, None) => {
                return Ok(Err(refused(
                    "a correction names exactly one of remaining_g and gross_g",
                )));
            }
            (Some(remaining), None) => remaining,
            (None, Some(gross)) => match spool.tare_g {
                Some(tare) => gross - tare,
                None => {
                    return Ok(Err(refused(format!(
                        "spool {} has no tare, so a scale reading cannot be turned into a \
                         figure; give remaining_g instead",
                        spool.name
                    ))));
                }
            },
        };
        if !target.is_finite() || gross_g.is_some_and(|gross| !non_negative(gross)) {
            return Ok(Err(refused("the figure given is not a weight in grams")));
        }
        let source = source_of(command.actor());
        let pending = match spool.loaded_on_tool {
            Some(tool) => self.stores().filament.pending_charges(Some(tool)).await?,
            None => Vec::new(),
        };
        let mut figures = Figures::default();
        let (charges, took_pending) = self.take_pending(&pending, &spool, &mut figures, &source)?;
        let adjusted = SpoolAdjustedPayload {
            spool_id: spool.id,
            remaining_before_g: figures.now(&spool),
            remaining_after_g: target,
            gross_g,
            took_pending,
            cleared_marks: cleared_on(&spool, MarkClearing::Adjusted),
            actor: command.actor().clone(),
            reason: command.reason().to_owned(),
        };
        let mutation = self.mutation(
            spool.id,
            SpoolChange::SetRemaining {
                remaining_g: target,
            },
            during,
            &source,
            &adjusted,
        )?;
        self.written(
            spool.id,
            Write {
                mutations: vec![mutation],
                charges,
            },
        )
        .await
    }

    /// Retire a spool, which takes nothing it fed and refuses every later
    /// command.
    async fn retire(
        &self,
        command: &SpoolCommand,
        spool_id: SpoolId,
        during: Option<PrintId>,
    ) -> Result<Result<SpoolCommandOutcome, RejectionReason>, CoreError> {
        let spool = match self.live_spool(spool_id).await? {
            Ok(spool) => spool,
            Err(reason) => return Ok(Err(reason)),
        };
        let source = source_of(command.actor());
        let mutation = self.mutation(
            spool.id,
            SpoolChange::Retire,
            during,
            &source,
            &SpoolRetiredPayload {
                spool_id: spool.id,
                remaining_g: spool.remaining_g,
                actor: command.actor().clone(),
                reason: command.reason().to_owned(),
            },
        )?;
        self.written(
            spool.id,
            Write {
                mutations: vec![mutation],
                charges: Vec::new(),
            },
        )
        .await
    }

    /// One mutation of one spool, with the event that says it happened.
    fn mutation<P: printobserver_types::EventPayload>(
        &self,
        spool_id: SpoolId,
        change: SpoolChange,
        during: Option<PrintId>,
        source: &EventSource,
        payload: &P,
    ) -> Result<SpoolMutation, CoreError> {
        let event = self.draft_of(during, source.clone(), payload)?;
        Ok(SpoolMutation {
            spool_id,
            change,
            at: event.received_at,
            event,
        })
    }

    /// Close a tool's open segment of a print for a spool loaded or unloaded
    /// during it: what the tool fed up to the position read now is charged to
    /// the spool that was feeding it — or, after an unconfirmed filament
    /// change, to the spool now loaded, and to nobody when none is.
    async fn close_segment(
        &self,
        print_id: PrintId,
        tool: i64,
        outgoing: Option<&SpoolRecord>,
        incoming: Option<&SpoolRecord>,
        figures: &mut Figures,
        source: &EventSource,
    ) -> Result<Vec<Charge>, CoreError> {
        let Some(print) = self.stores().prints.print(print_id).await? else {
            return Ok(Vec::new());
        };
        let Some((position, extrusion, analysis)) = self.extrusion_now(&print).await? else {
            return Ok(Vec::new());
        };
        let ledger = self.print_ledger(print_id).await?;
        let Some(mut feed) = feed_to(print_id, tool, &ledger, &extrusion, &position, &analysis)
        else {
            return Ok(Vec::new());
        };
        // The spool judged for the open segment: the one feeding the tool, or
        // — a tool emptied since — the one its last segment was charged to.
        let judged = match (outgoing, ledger.last_spool(tool)) {
            (Some(spool), _) => Some(spool.clone()),
            (None, Some(last)) => self.stores().filament.spool(last).await?,
            (None, None) => None,
        };
        let charged = if judged
            .as_ref()
            .is_some_and(|spool| holds_change_mark(spool, print_id))
        {
            let Some(incoming) = incoming else {
                // Unloading the spool an unconfirmed change left takes
                // nothing: the open segment is still the next spool's.
                return Ok(Vec::new());
            };
            feed.note = Some(match &feed.note {
                Some(note) => format!("{AFTER_CHANGE}; {note}"),
                None => AFTER_CHANGE.to_owned(),
            });
            Some(incoming)
        } else {
            outgoing
        };
        Ok(vec![self.charge_of(
            feed,
            charged,
            figures,
            source.clone(),
        )?])
    }
}