menger-notes 0.9.0

menger's notes service — notes on their own API and file, every write a revision, the organization's roles held by the gate
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
//! **The body's write** — a note's body is a Yjs CRDT (V2-29's
//! `pairing`): every writer's change is an update, merged by the CRDT, so
//! two hands in one paragraph lose nothing. This module is the one that
//! writes a body, replays it and squashes it.
//!
//! - **An update** (`notes.update`, the editor's): the Yjs v1 bytes the
//!   browser's `yjs` made, applied to the note's CRDT, appended to the
//!   log with who and when, said on the channel for every open watch.
//! - **A whole body** (`notes.save`, an agent's over synapse or the API): a
//!   diff of the text now against the body given, made into the CRDT's own
//!   inserts and deletes — an update like any other, so it merges with live
//!   typing instead of overwriting it.
//! - **Sync** (`notes.state`): what a client lacks, by its state vector.
//! - **Replay** (`notes.replay`): the log applied up to a moment, to a fresh
//!   CRDT — the note as it stood then.
//! - **Squash** (`notes.squash`): the log up to a moment collapsed into one
//!   snapshot row, who wrote what kept as its `authors`.
//!
//! The markdown is derived from the CRDT on every write (the table `docs`, `text`), so a
//! read never touches the CRDT. A loaded CRDT is kept in memory; a
//! snapshot of it is written every [`SNAPSHOT_EVERY`] changes so a load
//! applies only what came after. A write that fails after the CRDT took it
//! drops the loaded copy, so the next one loads the file's.

use crate::events::{Author, Note, HistoryRow};
use crate::quota::Reserved;
use crate::store::{live_doc, log, now, now_ms, Change, NotesStore, LogRow, StoreError};
use rusqlite::{params, Connection, OptionalExtension};
use std::sync::{Arc, Mutex};
use yrs::updates::decoder::Decode;
use yrs::updates::encoder::Encode;
use yrs::{GetString, OffsetKind, Options, ReadTxn, StateVector, Text, Transact, Update};

/// The CRDT's one text: the body (the browser's `ydoc.getText("body")`).
pub const BODY: &str = "body";

/// A snapshot of a loaded CRDT is kept every this many changes.
pub const SNAPSHOT_EVERY: u32 = 64;

/// A fresh CRDT: byte offsets, a random client id.
fn fresh() -> yrs::Doc {
    yrs::Doc::with_options(Options { offset_kind: OffsetKind::Bytes, ..Options::default() })
}

pub(crate) fn text_of(doc: &yrs::Doc) -> String {
    let body = doc.get_or_insert_text(BODY);
    let txn = doc.transact();
    body.get_string(&txn)
}

fn apply(doc: &yrs::Doc, bytes: &[u8]) -> Result<(), StoreError> {
    let update = Update::decode_v1(bytes).map_err(|e| StoreError::BadUpdate(e.to_string()))?;
    let mut txn = doc.transact_mut();
    txn.apply_update(update).map_err(|e| StoreError::BadUpdate(e.to_string()))
}

pub(crate) fn b64(bytes: &[u8]) -> String {
    use base64::Engine;
    base64::engine::general_purpose::STANDARD.encode(bytes)
}

/// Base64 to bytes, or why not.
pub fn unb64(text: &str) -> Result<Vec<u8>, StoreError> {
    use base64::Engine;
    base64::engine::general_purpose::STANDARD.decode(text.trim()).map_err(|e| StoreError::BadUpdate(format!("not base64: {e}")))
}

/// The edits that make `old` into `new`, as the CRDT's inserts and removes
/// on `body` in `txn` — a diff by characters, applied last first so every
/// byte offset stands.
fn rewrite(body: &yrs::TextRef, txn: &mut yrs::TransactionMut, old: &str, new: &str) {
    for e in edits(old, new).iter().rev() {
        if e.remove > 0 {
            body.remove_range(txn, e.at as u32, e.remove as u32);
        }
        if !e.insert.is_empty() {
            body.insert(txn, e.at as u32, &e.insert);
        }
    }
}

/// One edit of a text: at byte `at`, `remove` bytes go and `insert` comes.
#[derive(Debug, Clone, PartialEq, Eq)]
pub(crate) struct Edit {
    at: usize,
    remove: usize,
    insert: String,
}

/// The edits that make `old` into `new`, in the order of the text, none
/// over another. NOTES-12: where each falls is counted here, from the
/// order and the lengths of the diff's ops alone. `similar` 2.7.0 does not
/// keep an op's own indices true (an insert after a one-letter equal
/// carries the letter before it as its place in `old`; a delete's place in
/// `new` runs ahead), and a save that trusted them kept nine letters out
/// of place in a note rearranged on 2026-11-17. What the edits make is
/// made on a plain copy first: edits that do not make `new` are dropped
/// for one that replaces the whole text, so a save keeps the body given
/// or nothing.
pub(crate) fn edits(old: &str, new: &str) -> Vec<Edit> {
    let whole = || vec![Edit { at: 0, remove: old.len(), insert: new.to_string() }];
    let diff = similar::TextDiff::configure().timeout(std::time::Duration::from_millis(250)).diff_chars(old, new);
    let old_at: Vec<usize> = old.char_indices().map(|(i, _)| i).chain(std::iter::once(old.len())).collect();
    let new_at: Vec<usize> = new.char_indices().map(|(i, _)| i).chain(std::iter::once(new.len())).collect();
    // The characters of `old` and of `new` the ops before this one took.
    let (mut o, mut n) = (0usize, 0usize);
    let mut out = Vec::new();
    for op in diff.ops() {
        let (took, gave, same) = match *op {
            similar::DiffOp::Equal { len, .. } => (len, len, true),
            similar::DiffOp::Delete { old_len, .. } => (old_len, 0, false),
            similar::DiffOp::Insert { new_len, .. } => (0, new_len, false),
            similar::DiffOp::Replace { old_len, new_len, .. } => (old_len, new_len, false),
        };
        if o + took >= old_at.len() || n + gave >= new_at.len() {
            return whole();
        }
        if !same {
            out.push(Edit { at: old_at[o], remove: old_at[o + took] - old_at[o], insert: new[new_at[n]..new_at[n + gave]].to_string() });
        }
        o += took;
        n += gave;
    }
    if made(old, &out) == new {
        out
    } else {
        whole()
    }
}

/// What `edits` make of `old`: each applied last first, as `rewrite` applies them.
pub(crate) fn made(old: &str, edits: &[Edit]) -> String {
    let mut text = old.to_string();
    for e in edits.iter().rev() {
        text.replace_range(e.at..e.at + e.remove, &e.insert);
    }
    text
}

/// What a write of the body did.
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct Written {
    /// The note after it.
    pub doc: Note,
    /// The change's `seq` — `None` when the write changed nothing (no row).
    pub seq: Option<u32>,
    /// Whether the writer's `base` was not the revision it wrote over.
    pub stale: bool,
}

/// What a client lacks, and where the note stands.
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct Synced {
    /// The Yjs v1 update holding what the client's state vector lacks (base64).
    pub update: String,
    /// The note's state vector (base64), for the client's own diff back.
    pub vector: String,
    pub revision: u32,
}

/// `id`'s log applied to a fresh CRDT up to change `seq` (or the moment
/// `at`, unix milliseconds): the CRDT, the title then, and the change it
/// stands at, when and by whom. A moment before every change kept, or a
/// `seq` squashed away, is [`StoreError::NoRevision`].
fn replayed(conn: &Connection, id: &str, title_now: &str, seq: Option<u32>, at: Option<i64>) -> Result<(yrs::Doc, String, u32, i64, String), StoreError> {
    let mut stmt = conn.prepare("SELECT seq, kind, update_v1, title, at, by FROM doc_log WHERE id = ?1 ORDER BY seq")?;
    let rows = stmt.query_map([id], |r| Ok((r.get::<_, u32>(0)?, r.get::<_, String>(1)?, r.get::<_, Option<Vec<u8>>>(2)?, r.get::<_, Option<String>>(3)?, r.get::<_, i64>(4)?, r.get::<_, String>(5)?)))?;
    let crdt = fresh();
    let mut last: Option<(u32, i64, String)> = None;
    let mut title = title_now.to_string();
    for row in rows {
        let (n, kind, update, t, when, by) = row?;
        if seq.is_some_and(|s| n > s) || at.is_some_and(|a| when > a) {
            break;
        }
        if kind == "delete" {
            continue;
        }
        if let Some(u) = update {
            apply(&crdt, &u)?;
        }
        if let Some(t) = t {
            title = t;
        }
        last = Some((n, when, by));
    }
    let missing = || StoreError::NoRevision(id.to_string(), seq.unwrap_or_default());
    let (n, when, by) = last.ok_or_else(missing)?;
    if seq.is_some_and(|s| s != n) && at.is_none() {
        // A seq squashed away: the snapshot after it is the first moment kept.
        return Err(missing());
    }
    Ok((crdt, title, n, when, by))
}

impl NotesStore {
    /// The loaded CRDT of `id`, loading it from the file when it is not:
    /// its snapshot, then every change after it.
    pub(crate) fn crdt(&self, conn: &Connection, id: &str) -> Result<Arc<Mutex<yrs::Doc>>, StoreError> {
        if let Some(d) = self.loaded.lock().unwrap_or_else(|p| p.into_inner()).get(id) {
            return Ok(Arc::clone(d));
        }
        let (state, state_seq): (Vec<u8>, u32) = conn
            .query_row("SELECT state, state_seq FROM docs WHERE id = ?1 AND deleted_at IS NULL", [id], |r| Ok((r.get(0)?, r.get(1)?)))
            .optional()?
            .ok_or_else(|| StoreError::NotFound(id.to_string()))?;
        let doc = fresh();
        apply(&doc, &state)?;
        let mut stmt = conn.prepare("SELECT update_v1 FROM doc_log WHERE id = ?1 AND seq > ?2 AND update_v1 IS NOT NULL ORDER BY seq")?;
        for u in stmt.query_map(params![id, state_seq], |r| r.get::<_, Vec<u8>>(0))? {
            apply(&doc, &u?)?;
        }
        let doc = Arc::new(Mutex::new(doc));
        self.loaded.lock().unwrap_or_else(|p| p.into_inner()).insert(id.to_string(), Arc::clone(&doc));
        Ok(doc)
    }

    fn forget(&self, id: &str) {
        self.loaded.lock().unwrap_or_else(|p| p.into_inner()).remove(id);
    }

    /// Makes a note in `org`, titled `title`, its body `body`, by `by`
    /// (`principal`): change 1, its id fresh ([`crate::vocab::mint`]).
    pub async fn create(self: &Arc<Self>, org: &str, title: &str, body: &str, by: &str, principal: &str) -> Result<Note, StoreError> {
        let (org, title, body, by, principal) = (org.to_string(), title.to_string(), body.to_string(), by.to_string(), principal.to_string());
        let doc = self
            .blocking(move |s| {
                let quota = s.quota();
                let born = quota.reserve_count(&principal).map_err(StoreError::Quota)?;
                let crdt = fresh();
                let update = {
                    let text = crdt.get_or_insert_text(BODY);
                    let mut txn = crdt.transact_mut();
                    text.insert(&mut txn, 0, &body);
                    txn.encode_update_v1()
                };
                // V2-31: the bytes the change keeps (its update), as every later change counts.
                let reserved = quota.reserve(&principal, update.len() as u64).map_err(StoreError::Quota)?;
                let state = crdt.transact().encode_state_as_update_v1(&StateVector::default());
                let conn = s.lock();
                let tx = conn.unchecked_transaction()?;
                let id = crate::vocab::mint(|id| tx.query_row("SELECT 1 FROM docs WHERE id = ?1", [id], |_| Ok(())).optional().ok().flatten().is_some());
                let at = now();
                let doc = Note { id, org, title, bytes: body.len() as u64, body: Some(body), revision: 1, created_at: at, created_by: by.clone(), at, by };
                tx.execute(
                    "INSERT INTO docs (id, org, title, text, revision, created_at, created_by, at, by, state, state_seq) VALUES (?1, ?2, ?3, ?4, 1, ?5, ?6, ?5, ?6, ?7, 1)",
                    params![doc.id, doc.org, doc.title, doc.body.as_deref().unwrap_or_default(), doc.at, doc.by, state],
                )?;
                log(&tx, LogRow { id: &doc.id, seq: 1, kind: "create", update: Some(&update), title: Some(&doc.title), at: now_ms(), by: &doc.by, principal: &principal, authors: None })?;
                tx.commit()?;
                quota.commit(born);
                quota.commit(reserved);
                s.loaded.lock().unwrap_or_else(|p| p.into_inner()).insert(doc.id.clone(), Arc::new(Mutex::new(crdt)));
                Ok(doc)
            })
            .await?;
        self.said(Change::Created(doc.clone()));
        Ok(doc)
    }

    /// Appends one change made to the loaded CRDT of `id` — its update
    /// `bytes`, applied already — with who and when; writes the derived
    /// text, and a snapshot every [`SNAPSHOT_EVERY`]. The quota's one call
    /// site: the update's size reserved for `principal` before, committed
    /// after.
    fn append(&self, conn: &Connection, id: &str, crdt: &yrs::Doc, bytes: &[u8], kind: &str, by: &str, principal: &str) -> Result<(Note, u32), StoreError> {
        let quota = self.quota();
        let reserved: Reserved = quota.reserve(principal, bytes.len() as u64).map_err(StoreError::Quota)?;
        let tx = conn.unchecked_transaction()?;
        let was = live_doc(&tx, id)?;
        let text = text_of(crdt);
        let seq = was.revision + 1;
        let doc = Note { bytes: text.len() as u64, body: Some(text), revision: seq, at: now(), by: by.to_string(), ..was };
        tx.execute("UPDATE docs SET text = ?2, revision = ?3, at = ?4, by = ?5 WHERE id = ?1", params![doc.id, doc.body.as_deref().unwrap_or_default(), seq, doc.at, doc.by])?;
        log(&tx, LogRow { id, seq, kind, update: Some(bytes), title: None, at: now_ms(), by, principal, authors: None })?;
        let state_seq: u32 = tx.query_row("SELECT state_seq FROM docs WHERE id = ?1", [id], |r| r.get(0))?;
        if seq - state_seq >= SNAPSHOT_EVERY {
            let state = crdt.transact().encode_state_as_update_v1(&StateVector::default());
            tx.execute("UPDATE docs SET state = ?2, state_seq = ?3 WHERE id = ?1", params![id, state, seq])?;
        }
        tx.commit()?;
        quota.commit(reserved);
        Ok((doc, seq))
    }

    /// `notes.update`: a writer's Yjs v1 update (`update`, base64) applied to
    /// `id`'s CRDT and kept, by `by` (`principal`), from the presence
    /// `session` when it says.
    pub async fn update(self: &Arc<Self>, id: &str, update: &str, by: &str, principal: &str, session: Option<String>) -> Result<Written, StoreError> {
        let bytes = unb64(update)?;
        let (id, by, principal) = (id.to_string(), by.to_string(), principal.to_string());
        let said_update = update.trim().to_string();
        let w = self
            .blocking(move |s| {
                let conn = s.lock();
                let crdt = s.crdt(&conn, &id)?;
                let crdt = crdt.lock().unwrap_or_else(|p| p.into_inner());
                let written = apply(&crdt, &bytes).and_then(|()| s.append(&conn, &id, &crdt, &bytes, "update", &by, &principal));
                match written {
                    Ok((doc, seq)) => Ok(Written { doc, seq: Some(seq), stale: false }),
                    Err(e) => {
                        drop(crdt);
                        s.forget(&id);
                        Err(e)
                    }
                }
            })
            .await?;
        self.said(Change::Updated { doc: w.doc.clone(), update: said_update, seq: w.seq.unwrap_or_default(), session });
        Ok(w)
    }

    /// `notes.save`: `body` whole, by `by` — made into the CRDT's own edits by
    /// a diff of the text its writer read (`base`, the revision it read)
    /// against `body`, made on the CRDT as it stood then and merged into it
    /// now: what others wrote since stays. With no `base` (or one squashed
    /// away) the diff is against the text now. A body equal to the text
    /// writes nothing.
    pub async fn save(self: &Arc<Self>, id: &str, body: &str, base: Option<u32>, by: &str, principal: &str) -> Result<Written, StoreError> {
        let (id, body, by, principal) = (id.to_string(), body.to_string(), by.to_string(), principal.to_string());
        let (w, update) = self
            .blocking(move |s| {
                let conn = s.lock();
                let crdt = s.crdt(&conn, &id)?;
                let crdt = crdt.lock().unwrap_or_else(|p| p.into_inner());
                let was = live_doc(&conn, &id)?;
                let stale = base.is_some_and(|b| b != was.revision);
                if text_of(&crdt) == body {
                    return Ok((Written { doc: was, seq: None, stale }, None));
                }
                // The edits are made where the writer read: on the CRDT as it
                // stood at `base` when it is an earlier one still kept (so what
                // others wrote since is merged, not undone), else on it now.
                let at_base = base.filter(|_| stale).and_then(|b| replayed(&conn, &id, &was.title, Some(b), None).ok()).map(|(d, ..)| d);
                let update = {
                    let on = at_base.as_ref().unwrap_or(&*crdt);
                    let old = text_of(on);
                    let text = on.get_or_insert_text(BODY);
                    let mut txn = on.transact_mut();
                    rewrite(&text, &mut txn, &old, &body);
                    txn.encode_update_v1()
                };
                if at_base.is_some() {
                    if let Err(e) = apply(&crdt, &update) {
                        drop(crdt);
                        s.forget(&id);
                        return Err(e);
                    }
                }
                match s.append(&conn, &id, &crdt, &update, "save", &by, &principal) {
                    Ok((doc, seq)) => Ok((Written { doc, seq: Some(seq), stale }, Some(update))),
                    Err(e) => {
                        drop(crdt);
                        s.forget(&id);
                        Err(e)
                    }
                }
            })
            .await?;
        if let (Some(update), Some(seq)) = (update, w.seq) {
            self.said(Change::Updated { doc: w.doc.clone(), update: b64(&update), seq, session: None });
        }
        Ok(w)
    }

    /// `notes.state`: what a client whose state vector is `vector` (base64;
    /// none: it holds nothing) lacks of `id`, and the note's own vector.
    pub async fn state(self: &Arc<Self>, id: &str, vector: Option<String>) -> Result<Synced, StoreError> {
        let id = id.to_string();
        self.blocking(move |s| {
            let sv = match vector.as_deref().map(unb64).transpose()? {
                Some(b) => StateVector::decode_v1(&b).map_err(|e| StoreError::BadUpdate(e.to_string()))?,
                None => StateVector::default(),
            };
            let conn = s.lock();
            let revision = live_doc(&conn, &id)?.revision;
            let crdt = s.crdt(&conn, &id)?;
            let crdt = crdt.lock().unwrap_or_else(|p| p.into_inner());
            let txn = crdt.transact();
            Ok(Synced { update: b64(&txn.encode_state_as_update_v1(&sv)), vector: b64(&txn.state_vector().encode_v1()), revision })
        })
        .await
    }

    /// `notes.replay`: `id` as it stood after change `seq` — or, with `at`
    /// (unix milliseconds), after the last change made by then: the log to
    /// there applied to a fresh CRDT, the title as it was. A moment before
    /// the first change kept (or before a squash's snapshot) is
    /// [`StoreError::NoRevision`].
    pub async fn replay(self: &Arc<Self>, id: &str, seq: Option<u32>, at: Option<i64>) -> Result<Note, StoreError> {
        let id = id.to_string();
        self.blocking(move |s| {
            let conn = s.lock();
            let now = live_doc(&conn, &id)?;
            let (crdt, title, n, when, by) = replayed(&conn, &id, &now.title, seq, at)?;
            let text = text_of(&crdt);
            Ok(Note { title, bytes: text.len() as u64, body: Some(text), revision: n, at: when / 1000, by, ..now })
        })
        .await
    }

    /// `notes.restore`: `id`'s body put back as it stood after change `seq`,
    /// by `by` — a whole-body write over the CRDT ([`NotesStore::save`]), so a
    /// restore is a change like any other, merged with live typing.
    pub async fn restore(self: &Arc<Self>, id: &str, seq: u32, by: &str, principal: &str) -> Result<Written, StoreError> {
        let then = self.replay(id, Some(seq), None).await?;
        self.save(id, then.body.as_deref().unwrap_or_default(), None, by, principal).await
    }

    /// `notes.history`: `id`'s changes, newest first, at most `limit`, those
    /// before `before` (a `seq`) when it says.
    pub async fn history(self: &Arc<Self>, id: &str, limit: usize, before: Option<u32>) -> Result<Vec<HistoryRow>, StoreError> {
        let id = id.to_string();
        self.blocking(move |s| {
            let conn = s.lock();
            live_doc(&conn, &id)?;
            let mut stmt = conn.prepare("SELECT seq, kind, at, by, principal, bytes, title, authors FROM doc_log WHERE id = ?1 AND seq < ?2 ORDER BY seq DESC LIMIT ?3")?;
            let rows = stmt
                .query_map(params![id, before.map_or(i64::MAX, i64::from), limit as i64], |r| {
                    let authors: Option<String> = r.get(7)?;
                    Ok(HistoryRow {
                        id: id.clone(),
                        seq: r.get(0)?,
                        kind: r.get(1)?,
                        at: r.get(2)?,
                        by: r.get(3)?,
                        principal: r.get(4)?,
                        bytes: r.get::<_, i64>(5)? as u64,
                        title: r.get(6)?,
                        authors: authors.and_then(|a| serde_json::from_str(&a).ok()),
                    })
                })?
                .collect::<Result<Vec<_>, _>>()?;
            Ok(rows)
        })
        .await
    }

    /// `notes.squash`: `id`'s changes up to `until` (a `seq`) collapsed into
    /// one `snapshot` row at `until` — the CRDT as it stood then — its
    /// `authors` saying who wrote what in them (an earlier snapshot's authors
    /// folded in). Answers how many rows went. Nothing after `until` moves.
    pub async fn squash(self: &Arc<Self>, id: &str, until: u32, by: &str, principal: &str) -> Result<u32, StoreError> {
        let (id, by, principal) = (id.to_string(), by.to_string(), principal.to_string());
        let squasher = by.clone();
        let then = self.replay(&id, Some(until), None).await?;
        let (org, removed) = self
            .blocking(move |s| {
                let conn = s.lock();
                let tx = conn.unchecked_transaction()?;
                let doc = live_doc(&tx, &id)?;
                let rows: Vec<(u32, String, Option<Vec<u8>>, i64, String, String, i64, Option<String>)> = {
                    let mut stmt = tx.prepare("SELECT seq, kind, update_v1, at, by, principal, bytes, authors FROM doc_log WHERE id = ?1 AND seq <= ?2 ORDER BY seq")?;
                    let rows = stmt
                        .query_map(params![id, until], |r| Ok((r.get(0)?, r.get(1)?, r.get(2)?, r.get(3)?, r.get(4)?, r.get(5)?, r.get(6)?, r.get(7)?)))?
                        .collect::<Result<Vec<_>, _>>()?;
                    rows
                };
                if rows.len() < 2 {
                    return Ok((doc.org, 0));
                }
                let crdt = fresh();
                let mut authors: Vec<Author> = Vec::new();
                let mut add = |a: Author| match authors.iter_mut().find(|x| x.principal == a.principal) {
                    Some(x) => {
                        x.changes += a.changes;
                        x.bytes += a.bytes;
                        x.first = x.first.min(a.first);
                        x.last = x.last.max(a.last);
                    }
                    None => authors.push(a),
                };
                let mut last_at = 0;
                for (_, kind, update, at, who, p, bytes, folded) in &rows {
                    if let Some(u) = update {
                        apply(&crdt, u)?;
                    }
                    last_at = *at;
                    match (kind.as_str(), folded) {
                        ("snapshot", Some(f)) => {
                            for a in serde_json::from_str::<Vec<Author>>(f).unwrap_or_default() {
                                add(a);
                            }
                        }
                        _ => add(Author { by: who.clone(), principal: p.clone(), changes: 1, bytes: *bytes as u64, first: *at, last: *at }),
                    }
                }
                let state = crdt.transact().encode_state_as_update_v1(&StateVector::default());
                let authors = serde_json::to_string(&authors).map_err(|e| StoreError::Db(e.to_string()))?;
                // V2-31: what each author keeps moves from the rows folded to
                // their share of the snapshot (`crate::events::shares`).
                let mut moved: std::collections::BTreeMap<String, i64> = std::collections::BTreeMap::new();
                for (_, kind, _, _, _, p, bytes, folded) in &rows {
                    match (kind.as_str(), folded) {
                        ("snapshot", Some(f)) => {
                            for (who, n) in crate::events::shares(*bytes as u64, &serde_json::from_str::<Vec<Author>>(f).unwrap_or_default()) {
                                *moved.entry(who).or_default() -= n as i64;
                            }
                        }
                        _ => *moved.entry(p.clone()).or_default() -= *bytes,
                    }
                }
                let folded: Vec<Author> = serde_json::from_str(&authors).unwrap_or_default();
                for (who, n) in crate::events::shares(state.len() as u64, &folded) {
                    *moved.entry(who).or_default() += n as i64;
                }
                tx.execute("DELETE FROM doc_log WHERE id = ?1 AND seq <= ?2", params![id, until])?;
                log(&tx, LogRow { id: &id, seq: until, kind: "snapshot", update: Some(&state), title: Some(&then.title), at: last_at, by: &by, principal: &principal, authors: Some(&authors) })?;
                tx.commit()?;
                let quota = s.quota();
                for (who, delta) in moved.into_iter().filter(|(_, d)| *d != 0) {
                    quota.moved(&who, delta);
                }
                Ok((doc.org, rows.len() as u32 - 1))
            })
            .await?;
        if removed > 0 {
            self.said(Change::Squashed { id: then.id.clone(), org, until, removed, at: now(), by: squasher });
        }
        Ok(removed)
    }
}