1use serde::{Deserialize, Serialize};
4
5pub const MAX_EFFECT_RECEIPTS: usize = 512;
17
18pub fn bounded_receipts(receipts: &mut Vec<WorkspaceEffectReceipt>) -> bool {
21 if receipts.len() <= MAX_EFFECT_RECEIPTS {
22 return false;
23 }
24 receipts.sort_by_key(|receipt| (receipt.completed_at, receipt.invocation.0));
25 let excess = receipts.len().saturating_sub(MAX_EFFECT_RECEIPTS);
26 receipts.drain(..excess);
27 true
28}
29
30#[derive(Clone, Debug, Serialize, Deserialize, PartialEq, Eq)]
34pub struct WorkspaceEffectReceipt {
35 pub invocation: crate::InvocationId,
36 pub actor: crate::Actor,
37 pub context: crate::ActionContext,
38 pub status: crate::EffectReceiptStatus,
39 pub completed_at: i64,
40}
41
42impl WorkspaceEffectReceipt {
43 #[must_use]
49 pub fn redacted(
50 invocation: crate::InvocationId,
51 actor: &crate::Actor,
52 mut context: crate::ActionContext,
53 status: crate::EffectReceiptStatus,
54 completed_at: i64,
55 ) -> Self {
56 if let Some(what) = &mut context.what {
57 what.forget_args();
58 }
59 Self::recorded(invocation, actor, context, status, completed_at)
60 }
61
62 #[must_use]
66 pub fn recorded(
67 invocation: crate::InvocationId,
68 actor: &crate::Actor,
69 mut context: crate::ActionContext,
70 status: crate::EffectReceiptStatus,
71 completed_at: i64,
72 ) -> Self {
73 context.facets = vec![
74 crate::ActionFacet::new("tilo.receipt", "effect"),
75 crate::ActionFacet::new("tilo.invocation", invocation.0.to_string()),
76 ];
77 Self {
78 invocation,
79 actor: actor.clone(),
80 context: bounded_receipt_context(context),
81 status,
82 completed_at,
83 }
84 }
85
86 #[must_use]
89 pub fn is_redacted(&self) -> bool {
90 let facets = &self.context.facets;
91 !self.invocation.0.is_nil()
92 && actor_is_bounded(&self.actor)
93 && self.completed_at >= 0
94 && self.context == bounded_receipt_context(self.context.clone())
95 && facets.len() == 2
96 && facets
97 .iter()
98 .any(|facet| facet.key == "tilo.receipt" && facet.value == "effect")
99 && facets.iter().any(|facet| {
100 facet.key == "tilo.invocation" && facet.value == self.invocation.0.to_string()
101 })
102 }
103}
104
105#[must_use]
108pub fn actor_is_bounded(actor: &crate::Actor) -> bool {
109 let id = actor.id();
110 !id.is_empty() && id.chars().count() <= 160 && !id.chars().any(char::is_control)
111}
112
113fn bounded_receipt_text(value: &str, max: usize) -> String {
114 value
115 .chars()
116 .filter(|character| !character.is_control())
117 .take(max)
118 .collect::<String>()
119 .trim()
120 .to_string()
121}
122
123fn bounded_receipt_context(mut context: crate::ActionContext) -> crate::ActionContext {
124 if let Some(item) = &mut context.item {
125 item.id = bounded_receipt_text(&item.id, 512);
126 item.label = bounded_receipt_text(&item.label, 200);
127 }
128 if let Some(app) = &mut context.app {
129 app.id = bounded_receipt_text(&app.id, 160);
130 app.label = bounded_receipt_text(&app.label, 160);
131 }
132 if let Some(resource) = &mut context.resource {
133 resource.id = bounded_receipt_text(&resource.id, 512);
134 resource.kind = bounded_receipt_text(&resource.kind, 160);
135 resource.label = bounded_receipt_text(&resource.label, 200);
136 }
137 context.purpose = context
138 .purpose
139 .as_deref()
140 .map(|value| bounded_receipt_text(value, 512))
141 .filter(|value| !value.is_empty());
142 if let Some(group) = &mut context.group {
143 group.id = bounded_receipt_text(&group.id, 160);
144 if let Some(label) = &mut group.label {
145 *label = bounded_receipt_text(label, 200);
146 }
147 group.kind = bounded_receipt_text(&group.kind, 160);
148 }
149 for facet in &mut context.facets {
150 facet.key = bounded_receipt_text(&facet.key, 96);
151 facet.value = bounded_receipt_text(&facet.value, 160);
152 }
153 context
154}
155
156#[cfg(test)]
157mod effect_receipt_redaction_tests {
158 #[test]
159 fn only_broker_owned_terminal_facets_and_no_arguments_enter_public_receipts() {
160 let invocation = crate::InvocationId::new();
161 let context = crate::ActionContext {
162 facets: vec![
163 crate::ActionFacet::new("private.argument", "secret text"),
164 crate::ActionFacet::new("tilo.invocation", "forged"),
165 crate::ActionFacet::new("tilo.receipt", "forged"),
166 ],
167 what: Some(crate::Did::verb(
168 crate::ActionKey::new("so.tilo.notes", "note", "append"),
169 [("text".to_owned(), crate::Scalar::text("secret words"))],
170 )),
171 ..Default::default()
172 };
173 let receipt = super::WorkspaceEffectReceipt::redacted(
174 invocation,
175 &crate::MemberId::person("person").into(),
176 context,
177 crate::EffectReceiptStatus::Succeeded,
178 0,
179 );
180 assert_eq!(
181 receipt.context.facets,
182 vec![
183 crate::ActionFacet::new("tilo.receipt", "effect"),
184 crate::ActionFacet::new("tilo.invocation", invocation.0.to_string()),
185 ]
186 );
187 let json = serde_json::to_string(&receipt).unwrap();
188 assert!(!json.contains("secret text"));
189 assert!(!json.contains("secret words"));
190 assert!(receipt.is_redacted());
191 }
192}
193
194#[cfg(test)]
195mod receipt_bound_tests {
196 use super::{MAX_EFFECT_RECEIPTS, WorkspaceEffectReceipt, bounded_receipts};
197
198 fn receipt(at: i64) -> WorkspaceEffectReceipt {
199 WorkspaceEffectReceipt::redacted(
200 crate::InvocationId::new(),
201 &crate::MemberId::person("owner").into(),
202 crate::ActionContext::default(),
203 crate::EffectReceiptStatus::Succeeded,
204 at,
205 )
206 }
207
208 #[test]
212 fn a_workspace_keeps_the_newest_receipts_whatever_it_did() {
213 let mut held: Vec<WorkspaceEffectReceipt> = (0..i64::try_from(MAX_EFFECT_RECEIPTS * 10)
214 .unwrap())
215 .map(receipt)
216 .collect();
217 assert!(bounded_receipts(&mut held), "ten times the window is cut");
218 assert_eq!(held.len(), MAX_EFFECT_RECEIPTS);
219 let oldest = held.iter().map(|held| held.completed_at).min().unwrap();
220 assert_eq!(
221 oldest,
222 i64::try_from(MAX_EFFECT_RECEIPTS * 9).unwrap(),
223 "the oldest were the ones dropped"
224 );
225 let kept_at_bound = held.len();
226 held.push(receipt(i64::MAX));
227 assert!(bounded_receipts(&mut held));
228 assert_eq!(held.len(), kept_at_bound, "one more costs the same");
229 let mut few = vec![receipt(1), receipt(2)];
231 assert!(!bounded_receipts(&mut few));
232 assert_eq!(few.len(), 2);
233 }
234}