aven-core 0.1.36

Core library for the Aven local-first task manager
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
use super::*;
use crate::sync::encrypted_tail::attachments::{
    self as images, Operation as ImageOperation, Reply as ImageReply, Ticket,
};
pub use aven_protocol::wire::images::PATH as IMAGES_PATH;
use std::{
    path::Path,
    time::{Duration, Instant},
};

/// Bounds serial append work in one round while allowing a large offline backlog
/// to clear well within the drain's round budget.
const PUSH_LIMIT: usize = 2048;

struct ImageRoundLimits {
    objects: usize,
    elapsed: Duration,
    metadata_pull_skips: usize,
}

/// A round transfers one image at a time until either bound is reached, and
/// always attempts at least one transfer so slow pushes, pulls or stale-context
/// refreshes earlier in the round cannot starve image progress. A complete
/// metadata observation may be reused only for this many intervening
/// image-only rounds before another pull is required.
const IMAGE_ROUND_LIMITS: ImageRoundLimits = ImageRoundLimits {
    objects: 16,
    elapsed: Duration::from_secs(2),
    metadata_pull_skips: 3,
};

#[derive(Debug, Clone, Copy, PartialEq, Eq, serde::Serialize)]
#[serde(rename_all = "kebab-case")]
pub enum ImageTransfer {
    Complete,
    Pending,
    Failed,
    Unavailable,
}

impl std::fmt::Display for ImageTransfer {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        f.write_str(match self {
            Self::Complete => "complete",
            Self::Pending => "pending",
            Self::Failed => "failed",
            Self::Unavailable => "unavailable",
        })
    }
}
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub struct Round {
    /// The server has no further page and no local change waits to upload.
    /// While publishing is blocked, withheld local changes do not count.
    pub metadata_caught_up: bool,
    pub images: ImageTransfer,
    /// Metadata records appended by this device during the round.
    pub sent_changes: usize,
    /// Metadata records applied from the server during the round.
    pub received_changes: usize,
    /// New encrypted content waits for a withdrawal rotation; this round
    /// uploaded nothing but still pulled and downloaded.
    pub publishing_blocked: bool,
}

pub struct DrainSnapshot {
    tail: TailSnapshot,
    pull: PullFreshness,
    batch_count: usize,
    /// Why this drain's withdrawal rotation failed, if it did.
    withdrawal: Option<anyhow::Error>,
}

#[derive(Default)]
struct PullFreshness {
    complete: bool,
    skipped_rounds: usize,
}
impl DrainSnapshot {
    /// The error reported for a drain that ended with publishing blocked.
    pub fn publishing_blocked_error(&mut self) -> anyhow::Error {
        match self.withdrawal.take() {
            Some(error) => error.context(PublishingBlocked),
            None => PublishingBlocked.into(),
        }
    }
}

async fn validated_tail_inputs(
    store: &ProtectedLocalKeyStore,
    db: &Database,
    locator: &str,
) -> Result<TailSnapshot> {
    let inputs = store.tail_inputs(db, locator).await?;
    db.validate_encrypted_attachment_integrity(&inputs.authority)
        .await?;
    Ok(inputs)
}

struct RoundProgress {
    pushes: usize,
    batch_collisions: usize,
    preflight: Option<tail::Preflight>,
    push_complete: bool,
    publishing_blocked: bool,
    page_complete: Option<bool>,
    pulled: bool,
    image_state: Option<ImageTransfer>,
    selected: bool,
    download: Option<images::Download>,
    image_started: Instant,
    image_transfers: usize,
}

impl Default for RoundProgress {
    fn default() -> Self {
        Self {
            pushes: 0,
            batch_collisions: 0,
            preflight: None,
            push_complete: false,
            publishing_blocked: false,
            page_complete: None,
            pulled: false,
            image_state: None,
            selected: false,
            download: None,
            image_started: Instant::now(),
            image_transfers: 0,
        }
    }
}

impl RoundProgress {
    fn image_budget_available(&self) -> bool {
        self.image_transfers == 0
            || (self.image_transfers < IMAGE_ROUND_LIMITS.objects
                && self.image_started.elapsed() < IMAGE_ROUND_LIMITS.elapsed)
    }

    fn transferred_image(&mut self) {
        self.image_transfers += 1;
    }
}
impl Client {
    pub async fn image_exchange(
        &self,
        context: &Context,
        bearer: &Secret,
        operation: ImageOperation,
    ) -> Result<ImageReply> {
        let request_limit = if matches!(operation, ImageOperation::Put { .. }) {
            images::HTTP_LIMIT
        } else {
            tail::CONTROL_LIMIT
        };
        let response_limit = if matches!(operation, ImageOperation::Read { .. }) {
            images::HTTP_LIMIT
        } else {
            tail::CONTROL_LIMIT
        };
        self.exchange_to(
            IMAGES_PATH,
            context,
            bearer,
            operation,
            request_limit,
            response_limit,
        )
        .await
    }
    pub async fn start_drain(
        &self,
        store: &ProtectedLocalKeyStore,
        db: &Database,
    ) -> Result<DrainSnapshot> {
        let enrollment = self.enrollment()?;
        enrollment.finish_pending_removal(store, db).await?;
        // A failed withdrawal keeps publishing blocked without stopping pulls.
        let withdrawal = Box::pin(enrollment.withdraw_expired_disclosure(store, db))
            .await
            .err();
        Ok(DrainSnapshot {
            tail: validated_tail_inputs(store, db, &self.locator).await?,
            pull: PullFreshness::default(),
            withdrawal,
            batch_count: 0,
        })
    }
    /// Pushes a bounded ordered prefix, applies at most one metadata page and
    /// transfers a bounded number of images serially. The caller owns the local
    /// blob root; committed metadata is independent of image transfer success.
    #[cfg(any(test, feature = "test-support"))]
    pub async fn round(
        &self,
        store: &ProtectedLocalKeyStore,
        db: &Database,
        blob_dir: &Path,
        lifecycle_policy: crate::attachments::LifecyclePolicy,
    ) -> Result<Round> {
        let mut drain = Box::pin(self.start_drain(store, db)).await?;
        Box::pin(self.round_in_drain(store, db, blob_dir, lifecycle_policy, &mut drain)).await
    }
    pub async fn round_in_drain(
        &self,
        store: &ProtectedLocalKeyStore,
        db: &Database,
        blob_dir: &Path,
        lifecycle_policy: crate::attachments::LifecyclePolicy,
        drain: &mut DrainSnapshot,
    ) -> Result<Round> {
        if !drain.tail.is_current(db).await? {
            drain.tail = validated_tail_inputs(store, db, &self.locator).await?;
        }
        let mut progress = RoundProgress::default();
        retry_stale!(
            self.round_once(
                &drain.tail,
                &mut drain.pull,
                db,
                blob_dir,
                lifecycle_policy,
                &mut progress,
                &mut drain.batch_count
            )
            .await,
            async {
                self.enrollment()?
                    .refresh_and_finish_pending_removal(store, db)
                    .await?;
                drain.tail = validated_tail_inputs(store, db, &self.locator).await?;
                drain.pull = PullFreshness::default();
                progress.preflight = None;
                progress.page_complete = None;
                progress.pulled = false;
                anyhow::Ok(())
            }
            .await,
        )
    }
    #[allow(clippy::too_many_arguments)]
    async fn round_once(
        &self,
        inputs: &TailSnapshot,
        pull: &mut PullFreshness,
        db: &Database,
        blob_dir: &Path,
        lifecycle_policy: crate::attachments::LifecyclePolicy,
        progress: &mut RoundProgress,
        batch_count: &mut usize,
    ) -> Result<Round> {
        let a = &inputs.authority;
        while !progress.push_complete && progress.pushes < PUSH_LIMIT {
            let (step, preflight) = match self
                .push_in_run(
                    inputs,
                    db,
                    blob_dir,
                    progress.preflight.clone(),
                    batch_count,
                    PUSH_LIMIT - progress.pushes,
                )
                .await
            {
                Err(error)
                    if super::super::errors::has_code(&error, "encrypted-tail-batch-known")
                        && progress.batch_collisions < 3 =>
                {
                    progress.batch_collisions += 1;
                    continue;
                }
                Err(error) if error.is::<PublishingBlocked>() => {
                    progress.publishing_blocked = true;
                    break;
                }
                result => result?,
            };
            progress.preflight = preflight;
            match step {
                PushStep::Appended => progress.pushes += 1,
                PushStep::BatchAppended(count) => progress.pushes += count,
                PushStep::Image(Some(state)) => {
                    progress.image_state = Some(state);
                    progress.push_complete = true;
                }
                PushStep::Image(None) => {
                    progress.transferred_image();
                    if !progress.image_budget_available() {
                        progress.push_complete = true;
                    }
                }
                PushStep::Empty => progress.push_complete = true,
            }
        }
        // Reaching the cap completes only this round's push phase. The next
        // bounded round resumes from the next ordered frozen group.
        progress.push_complete = true;
        let state_before_pull = db.encrypted_round_state(a).await?;
        let cursor_before_pull = state_before_pull.cursor;
        let image_work_waiting = state_before_pull.upload_pending
            || state_before_pull
                .downloads
                .is_some_and(|downloads| downloads.pending);
        let may_skip_pull = pull.complete
            && progress.pushes == 0
            && image_work_waiting
            && pull.skipped_rounds < IMAGE_ROUND_LIMITS.metadata_pull_skips;
        if progress.page_complete.is_none() && !may_skip_pull {
            progress.page_complete = Some(self.pull(a, &inputs.bearer, db).await?);
            progress.pulled = true;
            pull.complete = progress.page_complete == Some(true);
            pull.skipped_rounds = 0;
        } else if progress.page_complete.is_none() {
            progress.page_complete = Some(true);
            pull.skipped_rounds += 1;
        }
        let state = db.encrypted_round_state(a).await?;
        let mut downloaded = None;
        if progress.image_state.is_none() && state.downloads.is_some() {
            while progress.image_budget_available() {
                if !progress.selected {
                    progress.download = db.prepare_encrypted_image_download(a).await?;
                    progress.selected = true;
                }
                if progress.download.is_none() {
                    break;
                }
                match self
                    .download_image(
                        a,
                        &inputs.bearer,
                        db,
                        blob_dir,
                        progress.download.as_ref(),
                        lifecycle_policy,
                    )
                    .await
                {
                    Ok(true) => {
                        progress.transferred_image();
                        progress.selected = false;
                        progress.download = None;
                    }
                    Ok(false) => {
                        downloaded = Some(ImageTransfer::Unavailable);
                        break;
                    }
                    Err(error)
                        if is_stale(&error) || super::super::errors::is_hosting_refusal(&error) =>
                    {
                        return Err(error);
                    }
                    Err(_) => {
                        downloaded = Some(ImageTransfer::Failed);
                        break;
                    }
                }
            }
        }
        let final_state = db.encrypted_round_state(a).await?;
        let images =
            downloaded.unwrap_or_else(|| settled(&final_state, progress.publishing_blocked));
        Ok(Round {
            metadata_caught_up: progress.pulled
                && progress.page_complete == Some(true)
                && (final_state.idle || progress.publishing_blocked),
            // A failed push outranks later download outcomes in this round.
            images: progress.image_state.unwrap_or(images),
            sent_changes: progress.pushes,
            received_changes: final_state.cursor.saturating_sub(cursor_before_pull) as usize,
            publishing_blocked: progress.publishing_blocked,
        })
    }
    pub async fn upload_prepared_image(
        &self,
        inputs: &TailSnapshot,
        upload: images::Upload,
    ) -> Result<Ticket> {
        let (a, bearer) = (&inputs.authority, &inputs.bearer);
        inputs.require_publishing_ready()?;
        let ImageReply::Status(status) = self
            .image_exchange(
                &a.context,
                bearer,
                ImageOperation::Declare {
                    workspace: upload.workspace.clone(),
                    descriptor: upload.descriptor.clone(),
                },
            )
            .await?
        else {
            anyhow::bail!("error encrypted-image-reply")
        };
        let mut indices = std::collections::HashSet::new();
        ensure!(
            status.missing.len() <= upload.records.len()
                && (!status.complete || status.missing.is_empty())
                && status.expires_at.is_some(),
            "error encrypted-image-status"
        );
        ensure!(
            status
                .missing
                .iter()
                .all(|i| *i < upload.records.len() && indices.insert(*i)),
            "error encrypted-image-status"
        );
        let ticket = Ticket {
            reservation: status
                .reservation
                .ok_or_else(|| anyhow::anyhow!("error encrypted-image-ticket"))?,
        };
        for index in status.missing {
            inputs.require_publishing_ready()?;
            let record = upload
                .records
                .get(index)
                .ok_or_else(|| anyhow::anyhow!("error encrypted-image-index"))?
                .clone();
            ensure!(
                matches!(
                    self.image_exchange(
                        &a.context,
                        bearer,
                        ImageOperation::Put {
                            workspace: upload.workspace.clone(),
                            object: upload.object,
                            descriptor_commitment: upload.commitment,
                            reservation: ticket.reservation,
                            index,
                            record
                        }
                    )
                    .await?,
                    ImageReply::Done
                ),
                "error encrypted-image-reply"
            );
        }
        crate::sync::crash::Crash::Tail.at("image-put");
        inputs.require_publishing_ready()?;
        ensure!(
            matches!(
                self.image_exchange(
                    &a.context,
                    bearer,
                    ImageOperation::Complete {
                        workspace: upload.workspace,
                        object: upload.object,
                        descriptor_commitment: upload.commitment,
                        reservation: ticket.reservation
                    }
                )
                .await?,
                ImageReply::Done
            ),
            "error encrypted-image-reply"
        );
        Ok(ticket)
    }
    /// Repairs one known reference without changing its descriptor or metadata.
    pub async fn repair_attachment(
        &self,
        store: &ProtectedLocalKeyStore,
        db: &Database,
        blob_dir: &Path,
        workspace: &str,
        reference: &str,
    ) -> Result<()> {
        let enrollment = self.enrollment()?;
        enrollment.refresh(store, db).await?;
        retry_stale!(
            self.repair_attachment_once(store, db, blob_dir, workspace, reference)
                .await,
            enrollment.refresh(store, db).await,
        )
    }
    async fn repair_attachment_once(
        &self,
        store: &ProtectedLocalKeyStore,
        db: &Database,
        blob_dir: &Path,
        workspace: &str,
        reference: &str,
    ) -> Result<()> {
        let inputs = validated_tail_inputs(store, db, &self.locator).await?;
        let upload = db
            .repair_encrypted_image(&inputs.authority, blob_dir, workspace, reference)
            .await?;
        let object = upload.object;
        let commitment = upload.commitment;
        let ticket = self.upload_prepared_image(&inputs, upload).await?;
        ensure!(
            matches!(
                self.image_exchange(
                    &inputs.authority.context,
                    &inputs.bearer,
                    ImageOperation::Release {
                        workspace: workspace.into(),
                        object,
                        descriptor_commitment: commitment,
                        reservation: ticket.reservation
                    }
                )
                .await?,
                ImageReply::Done
            ),
            "error encrypted-image-release"
        );
        Ok(())
    }
    /// False means the server no longer holds the selected object's bytes.
    async fn download_image(
        &self,
        a: &tail::Authority,
        bearer: &Secret,
        db: &Database,
        blob_dir: &Path,
        selected: Option<&images::Download>,
        lifecycle_policy: crate::attachments::LifecyclePolicy,
    ) -> Result<bool> {
        if let Some(download) = selected
            && !db
                .complete_encrypted_image_from_local(a, blob_dir, download, lifecycle_policy)
                .await?
        {
            let mut records = Vec::new();
            let mut total = 0;
            for index in 0..download.chunk_count {
                match self
                    .image_exchange(
                        &a.context,
                        bearer,
                        ImageOperation::Read {
                            workspace: download.workspace.clone(),
                            object: download.object,
                            descriptor_commitment: download.descriptor_commitment,
                            index,
                        },
                    )
                    .await?
                {
                    ImageReply::Chunk(record) => {
                        total += record.len();
                        ensure!(
                            total <= images::TRANSFER_BYTES,
                            "error encrypted-image-limit"
                        );
                        records.push(record);
                    }
                    ImageReply::Unavailable => return Ok(false),
                    _ => anyhow::bail!("error encrypted-image-reply"),
                }
            }
            db.install_encrypted_image(a, blob_dir, download, &records, lifecycle_policy)
                .await?;
        }
        Ok(true)
    }
}
/// Image availability after this round's transfer step. Uploads withheld
/// while publishing is blocked do not keep the drain going.
fn settled(state: &tail::RoundState, publishing_blocked: bool) -> ImageTransfer {
    match state.downloads {
        Some(d) if d.pending => ImageTransfer::Pending,
        Some(d) if d.unavailable => ImageTransfer::Unavailable,
        Some(_) if !state.upload_pending || publishing_blocked => ImageTransfer::Complete,
        _ => ImageTransfer::Pending,
    }
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn exhausted_round_time_still_allows_the_first_image_transfer() {
        let mut progress = RoundProgress {
            image_started: Instant::now() - IMAGE_ROUND_LIMITS.elapsed,
            ..RoundProgress::default()
        };
        assert!(progress.image_budget_available());
        progress.transferred_image();
        assert!(!progress.image_budget_available());
    }
}