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
//! How a compaction under way shows to clients: its start, and its end
//! under the id it started with.
use rho_protocol::{CompactionId, CompactionStatus, CompactionTrigger};
use rho_sdk::CompactionOutcome;
use super::LiveEvents;
use crate::app::session_events::WireEvent;
/// How a compaction ended, as [`LiveEvents::compaction_ended`] reports it.
pub(crate) enum CompactionEnd<'a> {
Completed(&'a CompactionOutcome),
Cancelled,
/// Failed with this error, which clients see: never a provider's raw
/// text, which may echo a credential.
Failed(String),
}
impl LiveEvents {
/// A compaction starting: the run's automatic one, or one a host runs
/// between turns, such as `/compact`, which emits no run events.
pub(crate) fn compaction_started(&mut self, trigger: CompactionTrigger) -> Vec<WireEvent> {
let id = fresh_compaction_id();
self.compaction = Some(id.clone());
// Called outside [`Self::map`] too: a compaction ends any message.
self.messages.interrupt();
vec![WireEvent::Compaction {
id,
status: CompactionStatus::Started {
trigger: Some(trigger),
},
}]
}
/// The compaction under way ending as `end`, under the id it started
/// with; a cancel or failure with no compaction under way reports
/// nothing.
pub(crate) fn compaction_ended(&mut self, end: CompactionEnd<'_>) -> Vec<WireEvent> {
let (id, status, usage) = match end {
CompactionEnd::Completed(outcome) => (
// A completion whose start was not seen still gets an id.
Some(self.compaction.take().unwrap_or_else(fresh_compaction_id)),
CompactionStatus::Completed {
counts: crate::compaction::outcome_counts(outcome),
},
// The compacted context's fill, as ACP reports it.
Some(outcome.current_tokens()),
),
CompactionEnd::Cancelled => (self.compaction.take(), CompactionStatus::Cancelled, None),
CompactionEnd::Failed(error) => (
self.compaction.take(),
CompactionStatus::Failed { error },
None,
),
};
let Some(id) = id else {
return Vec::new();
};
// Called outside [`Self::map`] too: a compaction ends any message.
self.messages.interrupt();
std::iter::once(WireEvent::Compaction { id, status })
.chain(usage.map(|used| self.fill(used)))
.collect()
}
}
/// Each live compaction gets a fresh random id: clients may keep a
/// session's events across daemon restarts, and an id that restarts per
/// process would update an earlier card. A replayed compaction's derives
/// from its saved node instead.
fn fresh_compaction_id() -> CompactionId {
CompactionId::new(format!("compaction-{}", uuid::Uuid::new_v4()))
}