nibkeep-node 0.1.0

The NibKeep engine: everything an app needs to work with a library
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
//! Purging what a move leaves behind ([technical design: spaces and moves]): the mover's replica purges, in
//! the old space, what its member's role lets it purge there, once the new entity counts and
//! another device holds it; a mark asks the old space's admins for the rest, and an admin's
//! replica lists those requests where the new entity counts with the same values.
//!
//! [technical design: spaces and moves]: ../../../../docs/Technical-design.md#131-spaces-and-moves

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

use nibkeep_base::{EntityId, Hash, SpaceId, StatementId};
use nibkeep_crypto::VerifyingKey;
use nibkeep_crypto::rand_core::CryptoRng;
use nibkeep_governance::Role as MemberRole;
use nibkeep_log::ChangeId;
use nibkeep_model::storage::Statements;
use nibkeep_model::{FieldName, Value, fields};
use nibkeep_sync::Synced;

use super::Library;
use super::members::users_and_owners;
use crate::NodeError;

/// An entity that a move left behind, whose mover asked that the rest of it be purged, as an
/// admin's replica lists it: where the new entity counts with the same values.
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub struct PurgeRequest {
	/// The entity left behind.
	pub entity: EntityId,
	/// The space it is in now, and the entity it is there.
	pub moved_into: (SpaceId, EntityId),
}

/// What a member may purge of an entity left behind, of one kind, secret or ordinary: the
/// statements, and the content that no statement that stays holds.
#[derive(Default)]
struct Part {
	statements: Vec<StatementId>,
	contents: Vec<Hash>,
}

/// A move of this replica's whose leftovers hold content, which waits for that content's
/// copies in the new space to be proven before it is purged.
pub(crate) struct WaitingMove {
	/// The old space, and the entity left there.
	pub(crate) space: SpaceId,
	pub(crate) entity: EntityId,
	/// The space it moved into.
	pub(crate) now_in: SpaceId,
	/// The contents that the leftovers hold which moved, each with whether it is secret.
	pub(crate) contents: Vec<(Hash, bool)>,
}

/// What a member may purge of an entity left behind: its ordinary values, and its secret
/// values, sealed or of a field secret in the space, each with their content.
#[derive(Default)]
struct Remains {
	ordinary: Part,
	secret: Part,
}

impl Remains {
	/// How many values it purges.
	fn len(&self) -> usize {
		self.ordinary.statements.len() + self.secret.statements.len()
	}

	/// Whether it purges no value.
	fn is_empty(&self) -> bool {
		self.len() == 0
	}
}

/// Leaves out of `contents`, ordinary and secret, what a statement that stays holds in a
/// field of the same kind, of the entity left behind or another: one that none of
/// `statements` is. A purge of content destroys only the keys of its kind, so a secret
/// file's content stays readable through the secrets, and the other way round, as a purge
/// of a value says.
fn keep_held<S: Statements>(
	storage: &S,
	secret_here: &BTreeSet<FieldName>,
	statements: &[BTreeSet<StatementId>; 2],
	contents: &mut [BTreeSet<Hash>; 2],
) -> Result<(), S::Error> {
	if contents.iter().all(BTreeSet::is_empty) {
		return Ok(());
	}
	for other in storage.described()? {
		for (field, ids) in storage.of_entity(other)? {
			for id in ids {
				if statements.iter().any(|kind| kind.contains(&id)) {
					continue;
				}
				// What a thumbnail was made of, it names without holding it.
				if let Some(recorded) = storage.get(id)?
					&& let Value::Blob(content) = &recorded.statement.value
					&& !nibkeep_std::names_content_only(&field)
				{
					let kind = secret_here.contains(&field) || storage.is_secret(id)?;
					contents[usize::from(kind)].remove(content);
				}
			}
		}
	}
	Ok(())
}

/// The fields whose statements a purge of what stays behind leaves: the marks.
pub(crate) fn is_mark(field: &FieldName) -> bool {
	*field == fields::MOVED_INTO || *field == fields::PURGE_ASKED || *field == fields::MERGED_INTO
}

/// Who purges: a member who may purge anything, as in the personal space or as an admin; an
/// editor, who purges only the changes their member's replicas signed; or nobody, as a
/// replica that its space does not list, or revoked.
enum Purger {
	All,
	Own(EntityId, BTreeMap<[u8; 32], EntityId>),
	Nobody,
}

impl Library {
	/// The members of `from` who are no members of `to`, by name, sorted, and whether this
	/// replica's member may purge all that a move from `from` leaves behind, as
	/// [`crate::MovePreview`] says.
	pub(crate) fn losing_and_purging(
		&self,
		(from, to): (SpaceId, SpaceId),
	) -> Result<(Vec<String>, bool), NodeError> {
		if from == self.info.personal || !self.has_members(from)? {
			return Ok((Vec::new(), true));
		}
		let users_of = |space: SpaceId| -> Result<BTreeSet<EntityId>, NodeError> {
			if space == self.info.personal || !self.has_members(space)? {
				return Ok(BTreeSet::from([self.info.user]));
			}
			Ok(self
				.members(space)?
				.into_iter()
				.filter(|member| member.role != MemberRole::None)
				.filter_map(|member| member.user)
				.collect())
		};
		let staying = users_of(to)?;
		let members = self.members(from)?;
		let mut losing: Vec<String> = members
			.iter()
			.filter(|member| member.role != MemberRole::None)
			.filter(|member| member.user.is_none_or(|user| !staying.contains(&user)))
			.map(|member| member.name.clone().unwrap_or_default())
			.collect();
		losing.sort();
		losing.dedup();
		let purges_all = members
			.iter()
			.any(|member| member.this_user && member.role == MemberRole::Admin);
		Ok((losing, purges_all))
	}

	/// Purges what moves that this replica made left behind, where a sync that started at
	/// `started`, after the move, just synced the new entity's space with another device,
	/// which then holds it: what this replica's member may purge in the old space, its own
	/// changes as an editor, everything but the marks as an admin or in the personal space.
	/// Leftovers that hold content, which the old space's replicas would lose, wait for the
	/// planner, which purges them once that content's copies in the new space are proven
	/// ([`Library::remove_proven`]). Called after each sync; returns the copies of this
	/// device's checkouts that it deleted.
	///
	/// # Errors
	///
	/// Returns an error if a space cannot be read, or a purge cannot be saved.
	pub fn settle_moves(
		&mut self,
		synced: &Synced,
		started: u64,
		wall_millis: u64,
		rng: &mut impl CryptoRng,
	) -> Result<Vec<PathBuf>, NodeError> {
		// Not where the other device was revoked or removed, which is sent nothing new, nor
		// in a space that this device dropped and deleted after the sync.
		let mut held = BTreeSet::new();
		for space in synced.spaces.iter().map(|space| space.space) {
			if !self.spaces.contains_key(&space) {
				continue;
			}
			let open = self.space(space)?;
			let log = open.stored.log();
			if !log.is_revoked(&synced.peer, &open.keys)?
				&& !log.is_removed(&synced.peer, &open.keys)?
			{
				held.insert(space);
			}
		}
		let synced = held;
		if synced.is_empty() || self.is_ai_agent()? {
			return Ok(Vec::new());
		}
		let own = self.replica.signing.verifying_key();
		let mut due = Vec::new();
		for space in self.spaces() {
			if self.check_not_revoked(space).is_err() {
				continue;
			}
			for entity in self.asked(space)? {
				// Only this replica's own moves, made before the sync started.
				if !self.asked_by(space, entity, (&own, started))? {
					continue;
				}
				if let Some((now_in, _)) = self.moved_into(space, entity)?
					&& synced.contains(&now_in)
				{
					due.push((space, entity, now_in));
				}
			}
		}
		let mut deleted = Vec::new();
		for (space, entity, now_in) in due {
			// Leftovers that hold content that moved wait for the planner, which purges them
			// once the copies of that content in the new space are proven.
			if let Some(remains) = self.remains(space, entity)?
				&& !remains.is_empty()
				&& self.moved_contents(now_in, &remains).is_empty()
			{
				deleted.extend(self.purge_remains(space, &remains, wall_millis, rng)?);
			}
		}
		Ok(deleted)
	}

	/// This replica's own moves whose leftovers hold content that moved, which wait for the
	/// planner: each with its old space and entity, the new space, and those contents, with
	/// whether each is secret ([technical design: the planner]).
	///
	/// [technical design: the planner]: ../../../../docs/Technical-design.md#99-the-planner
	pub(crate) fn moves_waiting_for_copies(&self) -> Result<Vec<WaitingMove>, NodeError> {
		if self.is_ai_agent()? {
			return Ok(Vec::new());
		}
		let own = self.replica.signing.verifying_key();
		let mut waiting = Vec::new();
		for space in self.spaces() {
			if self.check_not_revoked(space).is_err() {
				continue;
			}
			for entity in self.asked(space)? {
				if !self.asked_by(space, entity, (&own, u64::MAX))? {
					continue;
				}
				let Some((now_in, _)) = self.moved_into(space, entity)? else {
					continue;
				};
				if !self.spaces.contains_key(&now_in) {
					continue;
				}
				let Some(remains) = self.remains(space, entity)? else {
					continue;
				};
				if remains.is_empty() {
					continue;
				}
				let contents = self.moved_contents(now_in, &remains);
				if contents.is_empty() {
					continue;
				}
				waiting.push(WaitingMove {
					space,
					entity,
					now_in,
					contents,
				});
			}
		}
		Ok(waiting)
	}

	/// Adds to what a move left behind of a file, `taken`, where its plain copies were: its
	/// origins and checkouts that stayed, as those of other members' devices, deleted ones
	/// too, so that nothing that stays holds its content; those that moved are left behind
	/// themselves.
	fn with_plain_copies(
		&self,
		space: SpaceId,
		taken: &mut Vec<EntityId>,
	) -> Result<(), NodeError> {
		let files: BTreeSet<EntityId> = taken.iter().copied().collect();
		for (record, moved, _) in self.plain_copies_of(space, &files)? {
			if !moved {
				taken.push(record);
			}
		}
		Ok(())
	}

	/// The contents that leftovers hold which moved into `now_in`, as a key of a sealed copy
	/// there says, each with whether it is secret: those whose copies there are to be proven
	/// before the leftovers go. What did not move, as the old versions that a move without
	/// history leaves, goes as the mover chose.
	fn moved_contents(&self, now_in: SpaceId, remains: &Remains) -> Vec<(Hash, bool)> {
		let Some(there) = self.spaces.get(&now_in) else {
			return Vec::new();
		};
		let log = there.stored.log();
		remains
			.ordinary
			.contents
			.iter()
			.map(|content| (*content, false))
			.chain(
				remains
					.secret
					.contents
					.iter()
					.map(|content| (*content, true)),
			)
			.filter(|(content, secret)| {
				log.blob_keys(content)
					.any(|(sealed, _)| log.is_secret_blob(sealed) == *secret)
			})
			.collect()
	}

	/// Purges what this replica's move of an entity left behind, as [`Library::settle_moves`]
	/// does, once the planner has proven the copies in the new space of the content it holds;
	/// returns the copies of this device's checkouts that it deleted, or `None` if nothing
	/// was left to purge.
	pub(crate) fn purge_left_behind(
		&mut self,
		space: SpaceId,
		entity: EntityId,
		wall_millis: u64,
		rng: &mut impl CryptoRng,
	) -> Result<Option<Vec<PathBuf>>, NodeError> {
		match self.remains(space, entity)? {
			Some(remains) if !remains.is_empty() => self
				.purge_remains(space, &remains, wall_millis, rng)
				.map(Some),
			_ => Ok(None),
		}
	}

	/// Purges what a member may purge of an entity left behind, in a purge for each kind of
	/// content, secret or ordinary, as a purge destroys keys of one kind; returns the copies
	/// of this device's checkouts that it deleted. If the second purge fails, the first
	/// stays, and the next purge of what stays behind takes the rest.
	fn purge_remains(
		&mut self,
		space: SpaceId,
		remains: &Remains,
		wall_millis: u64,
		rng: &mut impl CryptoRng,
	) -> Result<Vec<PathBuf>, NodeError> {
		let mut deleted = Vec::new();
		for (part, of_secrets) in [(&remains.ordinary, false), (&remains.secret, true)] {
			if part.statements.is_empty() {
				continue;
			}
			deleted.extend(self.purge_statements(
				space,
				&part.statements,
				(&part.contents, of_secrets),
				wall_millis,
				rng,
			)?);
		}
		Ok(deleted)
	}

	/// The entities of a space that moves left behind and whose movers asked that the rest
	/// be purged, which this replica's member, an admin there, may purge: those whose new
	/// entity counts in a space this replica holds, with the same values, and that still hold
	/// more than their marks. Empty for a member who is no admin, and for the personal space,
	/// where the mover purges all.
	///
	/// # Errors
	///
	/// Returns an error if the space cannot be read.
	pub fn purge_requests(&self, space: SpaceId) -> Result<Vec<PurgeRequest>, NodeError> {
		if space == self.info.personal || !self.has_members(space)? {
			return Ok(Vec::new());
		}
		if self.check_settled_admin(space, "purge").is_err() {
			return Ok(Vec::new());
		}
		let mut requests = Vec::new();
		for entity in self.asked(space)? {
			if let Some(moved_into) = self.purgeable_behind(space, entity)?
				&& self
					.remains(space, entity)?
					.is_some_and(|remains| !remains.is_empty())
			{
				requests.push(PurgeRequest { entity, moved_into });
			}
		}
		Ok(requests)
	}

	/// How many values [`Library::purge_behind`] would purge of an entity that a move left
	/// behind: what this replica's member may purge there. `None` if no move asked for a
	/// purge of it.
	///
	/// # Errors
	///
	/// Returns an error if the space cannot be read.
	pub fn behind(&self, space: SpaceId, entity: EntityId) -> Result<Option<usize>, NodeError> {
		Ok(self.remains(space, entity)?.map(|remains| remains.len()))
	}

	/// Purges what a move left behind of an entity, as asked: what this replica's member may
	/// purge there, as [`Library::settle_moves`] says, once the new entity counts in a space
	/// this replica holds with the values that the old one shows; returns how many values it
	/// purged.
	///
	/// # Errors
	///
	/// Returns [`NodeError::NoPurgeAsked`] if no move asked for a purge of the entity,
	/// [`NodeError::MovedDiffers`] if what moved is not here or differs, and an error if the
	/// space cannot be read or the purge saved.
	pub fn purge_behind(
		&mut self,
		space: SpaceId,
		entity: EntityId,
		wall_millis: u64,
		rng: &mut impl CryptoRng,
	) -> Result<usize, NodeError> {
		self.check_not_revoked(space)?;
		self.check_not_ai_agent("purge")?;
		let Some(remains) = self.remains(space, entity)? else {
			return Err(NodeError::NoPurgeAsked(entity.to_string()));
		};
		if remains.is_empty() {
			return Ok(0);
		}
		if self.purgeable_behind(space, entity)?.is_none() {
			return Err(NodeError::MovedDiffers(entity.to_string()));
		}
		self.purge_remains(space, &remains, wall_millis, rng)?;
		Ok(remains.len())
	}

	/// Where the entity that a move made of one left behind counts, in a space this replica
	/// holds, if it counts there with the values that the one left behind shows.
	fn purgeable_behind(
		&self,
		space: SpaceId,
		entity: EntityId,
	) -> Result<Option<(SpaceId, EntityId)>, NodeError> {
		let Some(moved_into) = self.moved_into(space, entity)? else {
			return Ok(None);
		};
		Ok(self
			.same_values((space, entity), moved_into)?
			.then_some(moved_into))
	}

	/// The entities of a space whose moves asked that what stays of them be purged.
	fn asked(&self, space: SpaceId) -> Result<Vec<EntityId>, NodeError> {
		Ok(self
			.space(space)?
			.stored
			.find(&fields::PURGE_ASKED, &Value::Boolean(true))?)
	}

	/// The changes of the marks that ask for a purge of an entity, with when each was made.
	fn asking_changes(
		&self,
		space: SpaceId,
		entity: EntityId,
	) -> Result<Vec<(ChangeId, u64)>, NodeError> {
		let state = self.space(space)?.stored.log().state();
		let mut changes = Vec::new();
		for statement in state.current(entity, &fields::PURGE_ASKED)? {
			if let Some(recorded) = state.storage().get(statement.id)?
				&& let Some(change) = recorded.change
			{
				changes.push((ChangeId::from_hash(change), recorded.at.millis()));
			}
		}
		Ok(changes)
	}

	/// Whether a key signed a mark that asks for a purge of an entity, before `before`:
	/// whether its replica made the move, before a sync that started then.
	fn asked_by(
		&self,
		space: SpaceId,
		entity: EntityId,
		(key, before): (&VerifyingKey, u64),
	) -> Result<bool, NodeError> {
		let log = self.space(space)?.stored.log();
		for (change, at) in self.asking_changes(space, entity)? {
			if at < before
				&& log
					.change(change)?
					.is_some_and(|change| change.author() == key)
			{
				return Ok(true);
			}
		}
		Ok(false)
	}

	/// Who this replica's member is, as one who purges what moves leave in a space.
	fn purger(&self, space: SpaceId) -> Result<Purger, NodeError> {
		if space == self.info.personal || !self.has_members(space)? {
			return Ok(Purger::All);
		}
		if self.check_not_revoked(space).is_err() {
			return Ok(Purger::Nobody);
		}
		let Some(own) = self.member_in(space)? else {
			return Ok(Purger::Nobody);
		};
		let open = self.space(space)?;
		let roles = open.stored.log().member_roles(&open.keys)?;
		Ok(match roles.get(&own) {
			Some(MemberRole::Admin) => Purger::All,
			Some(MemberRole::Editor) => {
				Purger::Own(own, users_and_owners(&open.stored, &open.keys)?.1)
			}
			_ => Purger::Nobody,
		})
	}

	/// What this replica's member may purge of an entity that a move left behind, as asked:
	/// every statement, current or past, but the marks, of it and of the entities merged into
	/// it before the move, that the move's mark builds on; of a secret value, sealed or of a
	/// field secret in the space, only one that moved, as this replica sees the new entity
	/// hold the same value in the same field, which needs the secrets of both spaces unlocked,
	/// so that a value left behind, or one sealed out of sight, stays; of those, as an editor
	/// of a shared space, only the ones its member's replicas signed; and, but for an editor,
	/// the content of its files that no statement that stays holds. `None` if no move asked
	/// for a purge of it.
	fn remains(&self, space: SpaceId, entity: EntityId) -> Result<Option<Remains>, NodeError> {
		let marks: Vec<ChangeId> = self
			.asking_changes(space, entity)?
			.into_iter()
			.map(|(change, _)| change)
			.collect();
		if marks.is_empty() {
			return Ok(None);
		}
		let purger = self.purger(space)?;
		if matches!(purger, Purger::Nobody) {
			return Ok(Some(Remains::default()));
		}
		// What the new entity holds, read once a secret value is met.
		let mut moved: Option<Vec<(FieldName, Value)>> = None;
		let open = self.space(space)?;
		let secret_here = open.stored.secret_fields();
		let log = open.stored.log();
		let state = log.state();
		let storage = state.storage();
		let is_before = |change: ChangeId| -> Result<bool, NodeError> {
			for mark in &marks {
				if change != *mark && log.is_ancestor_of(change, *mark)? {
					return Ok(true);
				}
			}
			Ok(false)
		};
		// The entity, and those merged into it before the move, which the move took; never
		// one merged with it since, which the move did not take.
		let mut taken = vec![entity];
		for other in state.merged(state.canonical(entity)?)? {
			if other == entity {
				continue;
			}
			for merge in state.current(other, &fields::MERGED_INTO)? {
				if merge.value.as_reference() == Some(entity)
					&& let Some(recorded) = storage.get(merge.id)?
					&& let Some(change) = recorded.change
					&& is_before(ChangeId::from_hash(change))?
				{
					taken.push(other);
				}
			}
		}
		self.with_plain_copies(space, &mut taken)?;
		// The statements, and the contents, of each kind: ordinary, then secret.
		let mut statements = [BTreeSet::new(), BTreeSet::new()];
		let mut contents = [BTreeSet::new(), BTreeSet::new()];
		for &each in &taken {
			for (field, ids) in storage.of_entity(each)? {
				if is_mark(&field) {
					continue;
				}
				for id in ids {
					let Some(recorded) = storage.get(id)? else {
						continue;
					};
					let secret = secret_here.contains(&field) || storage.is_secret(id)?;
					// A secret value only where it moved, as far as this replica sees.
					let value = &recorded.statement.value;
					if secret && !self.secret_moved((space, entity), (&field, value), &mut moved)? {
						continue;
					}
					let kind = usize::from(secret);
					let Some(change) = recorded.change.map(ChangeId::from_hash) else {
						continue;
					};
					// An editor purges only the changes their own replicas signed, which is
					// cheaper to tell than whether the move took it.
					if let Purger::Own(own, owners) = &purger {
						let signer = log
							.change(change)?
							.and_then(|change| owners.get(change.author().as_bytes()).copied());
						if signer != Some(*own) {
							continue;
						}
					}
					// Only what the move took: what its mark builds on.
					if !is_before(change)? {
						continue;
					}
					// What a thumbnail was made of, it names without holding it.
					if matches!(purger, Purger::All)
						&& let Value::Blob(content) = &recorded.statement.value
						&& !nibkeep_std::names_content_only(&field)
					{
						contents[kind].insert(*content);
					}
					statements[kind].insert(id);
				}
			}
		}
		keep_held(storage, secret_here, &statements, &mut contents)?;
		self.take_thumbnails_staying(space, &mut statements, &mut contents)?;
		let [ordinary, secret] = statements;
		let [ordinary_contents, secret_contents] = contents;
		Ok(Some(Remains {
			ordinary: Part {
				statements: ordinary.into_iter().collect(),
				contents: ordinary_contents.into_iter().collect(),
			},
			secret: Part {
				statements: secret.into_iter().collect(),
				contents: secret_contents.into_iter().collect(),
			},
		}))
	}

	/// Adds to what a purge of what a move left behind takes, ordinary and secret, the
	/// thumbnails made of the ordinary content it takes that stayed, as of a version the move
	/// did not take: each one's statements, but its marks, and its own content, unless a
	/// statement that stays holds that. Those that moved are left behind themselves, and
	/// purged so.
	fn take_thumbnails_staying(
		&self,
		space: SpaceId,
		statements: &mut [BTreeSet<StatementId>; 2],
		contents: &mut [BTreeSet<Hash>; 2],
	) -> Result<(), NodeError> {
		let open = self.space(space)?;
		let state = open.stored.log().state();
		let storage = state.storage();
		let mut pictures = BTreeSet::new();
		for content in &contents[0] {
			for thumbnail in open
				.stored
				.find(&nibkeep_std::THUMBNAIL_OF, &Value::Blob(*content))?
			{
				if !state.current(thumbnail, &fields::MOVED_INTO)?.is_empty() {
					continue;
				}
				for (field, ids) in storage.of_entity(thumbnail)? {
					if is_mark(&field) {
						continue;
					}
					for id in ids {
						if field == nibkeep_std::CONTENT
							&& let Some(recorded) = storage.get(id)?
							&& let Value::Blob(picture) = recorded.statement.value
						{
							pictures.insert(picture);
						}
						statements[0].insert(id);
					}
				}
			}
		}
		if !pictures.is_empty() {
			let mut theirs = [pictures, BTreeSet::new()];
			keep_held(
				storage,
				open.stored.secret_fields(),
				statements,
				&mut theirs,
			)?;
			let [pictures, _] = theirs;
			contents[0].extend(pictures);
		}
		Ok(())
	}

	/// Whether a secret value of an entity that a move left behind moved, as this replica
	/// sees the new entity hold the same value in the same field, a reference leading where
	/// what it refers to moved, as a move carries it. Reads what the new entity holds into
	/// `moved` the first time.
	fn secret_moved(
		&self,
		(space, entity): (SpaceId, EntityId),
		(field, value): (&FieldName, &Value),
		moved: &mut Option<Vec<(FieldName, Value)>>,
	) -> Result<bool, NodeError> {
		if moved.is_none() {
			*moved = Some(self.moved_values(space, entity)?);
		}
		let carried = match value {
			Value::Reference(target) => {
				let target = self.space(space)?.stored.log().state().canonical(*target)?;
				self.moved_into(space, target)?
					.map_or_else(|| value.clone(), |(_, now)| Value::Reference(now))
			}
			_ => value.clone(),
		};
		Ok(moved
			.as_deref()
			.unwrap_or_default()
			.iter()
			.any(|(moved_field, moved_value)| {
				moved_field == field && (*moved_value == carried || moved_value == value)
			}))
	}

	/// The values, current or past, that the entity a move made of one left behind holds,
	/// each with its field, as far as this replica sees them: none where it does not hold the
	/// space of the new entity, and no secret value of it while that space's secrets are
	/// locked.
	fn moved_values(
		&self,
		space: SpaceId,
		entity: EntityId,
	) -> Result<Vec<(FieldName, Value)>, NodeError> {
		let Some((now_in, new)) = self.moved_into(space, entity)? else {
			return Ok(Vec::new());
		};
		let state = self.space(now_in)?.stored.log().state();
		let storage = state.storage();
		let mut values = Vec::new();
		for member in state.merged(state.canonical(new)?)? {
			for (field, ids) in storage.of_entity(member)? {
				for id in ids {
					if let Some(recorded) = storage.get(id)? {
						values.push((field.clone(), recorded.statement.into_owned().value));
					}
				}
			}
		}
		Ok(values)
	}

	/// Whether the entity a move made counts with the values that the one it left behind
	/// shows now: each of the old entity's fields that a move carries has, on the new one, a
	/// value equal to one of the old one's, a reference leading where the old one's moved, if
	/// it moved; one, as a field whose last writer wins carries only the value it shows.
	fn same_values(
		&self,
		(space, entity): (SpaceId, EntityId),
		(now_in, new): (SpaceId, EntityId),
	) -> Result<bool, NodeError> {
		let old_state = self.space(space)?.stored.log().state();
		let new_state = self.space(now_in)?.stored.log().state();
		let entity = old_state.canonical(entity)?;
		for field in old_state.storage().current_fields(entity)? {
			if is_mark(&field) || field == fields::DELETED {
				continue;
			}
			let theirs = new_state.current(new, &field)?;
			let (mut carried, mut matched) = (false, false);
			for statement in old_state.current(entity, &field)? {
				let value = match &statement.value {
					Value::Reference(target) => match self.moved_into(space, *target)? {
						Some((_, moved)) => Value::Reference(moved),
						// A folder that stays holds what moved through its mark alone.
						None if field == nibkeep_std::IN_FOLDER => continue,
						None => statement.value.clone(),
					},
					value => value.clone(),
				};
				carried = true;
				matched |= theirs.iter().any(|their| their.value == value);
			}
			if carried && !matched {
				return Ok(false);
			}
		}
		Ok(true)
	}
}