aven-core 0.1.13

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
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
use std::path::Path;

use anyhow::{Result, bail};
use sqlx::Row;
use sqlx::SqliteConnection;

use crate::attachments::AttachmentBytesState;
use crate::attachments::decode::{ImageFacts, ValidatedImage, validate_image};
use crate::attachments::optimization::{ImageOptimizationPolicy, optimize_image_bytes};
use crate::attachments::storage::{blob_inventory_row, sha256_hex, store_validated_blob};
use crate::attachments::validation::{
    validate_alt_text, validate_attachment_id, validate_filename,
};
use crate::change_log::{ChangeEntity, ChangePayload, append_change, op_type};
use crate::db::{Database, begin_immediate};
use crate::ids::{TaskId, WorkspaceId, new_id, now};
use crate::types::TaskAttachment;
use crate::workspaces::Workspace;

#[derive(Debug, Clone, PartialEq, Eq)]
pub struct AttachmentAddInput {
    pub filename: Option<String>,
    pub alt_text: Option<String>,
    pub declared_media_type: Option<String>,
    pub bytes: Vec<u8>,
    pub optimization_policy: ImageOptimizationPolicy,
    pub dedupe_existing: bool,
}

#[derive(Debug, Clone, PartialEq, Eq)]
pub struct TaskAttachmentAddInput {
    pub attachment_id: String,
    pub input: AttachmentAddInput,
}

#[derive(Debug, Clone, PartialEq, Eq)]
pub struct PreparedAttachment {
    pub attachment_id: String,
    pub filename: Option<String>,
    pub alt_text: Option<String>,
    pub sha256: String,
    pub byte_size: i64,
    pub facts: ImageFacts,
    pub bytes: Vec<u8>,
    pub optimized: bool,
}

pub struct AttachmentAddOutcome {
    pub outcome: AttachmentOutcome,
    pub created: bool,
    pub optimized: bool,
}

pub struct AttachmentOutcome {
    pub attachment: TaskAttachment,
    pub has_blob: bool,
}

pub struct AttachmentReadLease {
    pub sha256: String,
    pub media_type: String,
    pub lease_id: String,
}

#[derive(Debug, Clone)]
pub struct AttachmentReadItem {
    pub attachment: TaskAttachment,
    pub bytes_state: AttachmentBytesState,
    pub has_blob: bool,
}

fn attachment_from_row(row: &sqlx::sqlite::SqliteRow) -> TaskAttachment {
    TaskAttachment {
        workspace_id: row.get("workspace_id"),
        attachment_id: row.get("attachment_id"),
        task_id: row.get("task_id"),
        sha256: row.get("sha256"),
        byte_size: row.get("byte_size"),
        media_type: row.get("media_type"),
        filename: row.get("filename"),
        alt_text: row.get("alt_text"),
        width: row.get("width"),
        height: row.get("height"),
        created_at: row.get("created_at"),
        created_by_change_id: row.get("created_by_change_id"),
        deleted: row.get::<i64, _>("deleted") != 0,
        deleted_at: row.get("deleted_at"),
        deleted_by_change_id: row.get("deleted_by_change_id"),
    }
}

async fn attachment_bytes_state(
    conn: &mut SqliteConnection,
    sha256: &str,
) -> Result<AttachmentBytesState> {
    Ok(match blob_inventory_row(conn, sha256).await? {
        Some(row) if row.available => AttachmentBytesState::Present,
        Some(_) => AttachmentBytesState::Unavailable,
        None => AttachmentBytesState::PendingDownload,
    })
}

async fn attachment_has_blob(conn: &mut SqliteConnection, sha256: &str) -> Result<bool> {
    Ok(attachment_bytes_state(conn, sha256).await? == AttachmentBytesState::Present)
}

async fn existing_live_attachments_by_sha(
    conn: &mut SqliteConnection,
    workspace_id: &crate::ids::WorkspaceId,
    task_id: &TaskId,
    sha256: &str,
) -> Result<Vec<TaskAttachment>> {
    let rows = sqlx::query(
        "SELECT ta.workspace_id, ta.attachment_id, ta.task_id, ta.sha256, ta.byte_size,
                ta.media_type, ta.filename, ta.alt_text, ta.width, ta.height,
                ta.created_at, ta.created_by_change_id, ta.deleted, ta.deleted_at, ta.deleted_by_change_id
         FROM task_attachments ta
         WHERE ta.workspace_id = ? AND ta.task_id = ? AND ta.sha256 = ? AND ta.deleted = 0
         ORDER BY ta.created_at, ta.attachment_id",
    )
    .bind(workspace_id)
    .bind(task_id)
    .bind(sha256)
    .fetch_all(&mut *conn)
    .await?;

    Ok(rows.iter().map(attachment_from_row).collect())
}

pub async fn prepare_task_attachment(input: TaskAttachmentAddInput) -> Result<PreparedAttachment> {
    validate_attachment_id(&input.attachment_id)?;
    validate_filename(input.input.filename.as_deref())?;
    validate_alt_text(input.input.alt_text.as_deref())?;

    let source = validate_image(input.input.bytes, input.input.declared_media_type).await?;
    let source_facts = source.facts.clone();
    let optimized = optimize_image_bytes(
        &source_facts.media_type,
        source.bytes,
        input.input.optimization_policy,
    )
    .await?;
    let optimized_flag = optimized.optimized;
    let stored_image = if optimized_flag {
        validate_image(optimized.bytes, None).await?
    } else {
        ValidatedImage {
            bytes: optimized.bytes,
            facts: source_facts,
        }
    };
    let sha256 = sha256_hex(&stored_image.bytes);
    let byte_size = i64::try_from(stored_image.bytes.len())?;
    Ok(PreparedAttachment {
        attachment_id: input.attachment_id,
        filename: input.input.filename,
        alt_text: input.input.alt_text,
        sha256,
        byte_size,
        facts: stored_image.facts,
        bytes: stored_image.bytes,
        optimized: optimized_flag,
    })
}

pub(super) async fn insert_prepared_attachment(
    conn: &mut SqliteConnection,
    workspace: &Workspace,
    task_id: &TaskId,
    prepared: &PreparedAttachment,
    created_at: &str,
) -> Result<String> {
    let change_id = append_change(
        conn,
        ChangeEntity::Task,
        task_id,
        Some("attachments"),
        op_type::ATTACHMENT_ADD,
        ChangePayload::workspace(workspace)
            .set("attachment_id", &prepared.attachment_id)
            .set("sha256", &prepared.sha256)
            .set("byte_size", prepared.byte_size)
            .set("media_type", &prepared.facts.media_type)
            .set("filename", &prepared.filename)
            .set("alt_text", &prepared.alt_text)
            .set("width", prepared.facts.width)
            .set("height", prepared.facts.height)
            .set("created_at", created_at),
    )
    .await?;
    sqlx::query(
        "INSERT INTO task_attachments(workspace_id, attachment_id, task_id, sha256, byte_size, media_type, filename, alt_text, width, height, created_at, created_by_change_id)
         VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)",
    )
    .bind(&workspace.id)
    .bind(&prepared.attachment_id)
    .bind(task_id)
    .bind(&prepared.sha256)
    .bind(prepared.byte_size)
    .bind(&prepared.facts.media_type)
    .bind(&prepared.filename)
    .bind(&prepared.alt_text)
    .bind(prepared.facts.width)
    .bind(prepared.facts.height)
    .bind(created_at)
    .bind(&change_id)
    .execute(&mut *conn)
    .await?;
    Ok(change_id)
}

struct AttachmentCommitIdentity {
    attachment_id: Option<String>,
    created_at: Option<String>,
}

pub async fn add_task_attachment(
    conn: &mut SqliteConnection,
    workspace: &Workspace,
    blob_dir: &Path,
    policy: crate::attachments::lifecycle::LifecyclePolicy,
    task_id: &TaskId,
    input: AttachmentAddInput,
) -> Result<AttachmentAddOutcome> {
    add_task_attachment_inner(
        conn,
        workspace,
        blob_dir,
        policy,
        task_id,
        AttachmentCommitIdentity {
            attachment_id: None,
            created_at: None,
        },
        input,
    )
    .await
}

pub(crate) async fn add_ordered_task_attachment(
    conn: &mut SqliteConnection,
    workspace: &Workspace,
    blob_dir: &Path,
    policy: crate::attachments::lifecycle::LifecyclePolicy,
    task_id: &TaskId,
    created_at: String,
    input: TaskAttachmentAddInput,
) -> Result<AttachmentAddOutcome> {
    add_task_attachment_inner(
        conn,
        workspace,
        blob_dir,
        policy,
        task_id,
        AttachmentCommitIdentity {
            attachment_id: Some(input.attachment_id),
            created_at: Some(created_at),
        },
        input.input,
    )
    .await
}

async fn add_task_attachment_inner(
    conn: &mut SqliteConnection,
    workspace: &Workspace,
    blob_dir: &Path,
    policy: crate::attachments::lifecycle::LifecyclePolicy,
    task_id: &TaskId,
    identity: AttachmentCommitIdentity,
    input: AttachmentAddInput,
) -> Result<AttachmentAddOutcome> {
    validate_filename(input.filename.as_deref())?;
    validate_alt_text(input.alt_text.as_deref())?;

    let source = validate_image(input.bytes, input.declared_media_type).await?;
    let source_facts = source.facts.clone();
    let optimized = optimize_image_bytes(
        &source_facts.media_type,
        source.bytes,
        input.optimization_policy,
    )
    .await?;
    let optimized_flag = optimized.optimized;
    let stored_image = if optimized_flag {
        validate_image(optimized.bytes, Some(source_facts.media_type.clone())).await?
    } else {
        ValidatedImage {
            bytes: optimized.bytes,
            facts: source_facts,
        }
    };
    let sha256 = sha256_hex(&stored_image.bytes);
    let byte_size = i64::try_from(stored_image.bytes.len())?;
    let capacity_reservation = crate::attachments::lifecycle::ensure_local_capacity(
        conn,
        blob_dir,
        &sha256,
        byte_size,
        policy,
        &crate::attachments::lifecycle::SystemClock,
    )
    .await?;
    let staging_lease = crate::attachments::lifecycle::acquire_lease(
        conn,
        &sha256,
        "staging",
        &crate::attachments::lifecycle::SystemClock,
    )
    .await?;
    let stored = match store_validated_blob(conn, blob_dir, stored_image).await {
        Ok(stored) => stored,
        Err(error) => {
            let _ = crate::attachments::lifecycle::release_lease(conn, &staging_lease).await;
            if let Some(reservation_id) = capacity_reservation.as_deref() {
                let _ =
                    crate::attachments::lifecycle::release_reservation(conn, reservation_id).await;
            }
            return Err(error);
        }
    };
    if let Some(reservation_id) = capacity_reservation {
        crate::attachments::lifecycle::release_reservation(conn, &reservation_id).await?;
    }
    let database_result = async {
        let mut tx = begin_immediate(conn).await?;
        let task_exists = sqlx::query_scalar::<_, bool>(
            "SELECT EXISTS(SELECT 1 FROM tasks WHERE workspace_id = ? AND id = ?)",
        )
        .bind(&workspace.id)
        .bind(task_id)
        .fetch_one(&mut *tx)
        .await?;
        if !task_exists {
            bail!("error task-not-found task_id={task_id}");
        }

        if input.dedupe_existing
            && let Some(existing) =
                existing_live_attachments_by_sha(&mut tx, &workspace.id, task_id, &stored.sha256)
                    .await?
                    .first()
        {
            let attachment_id = existing.attachment_id.clone();
            tx.commit().await?;
            return Ok::<_, anyhow::Error>((attachment_id, false));
        }

        let attachment_id = identity.attachment_id.unwrap_or_else(new_id);
        let created_at = identity.created_at.unwrap_or_else(now);
        let change_id = append_change(
            &mut tx,
            ChangeEntity::Task,
            task_id,
            Some("attachments"),
            op_type::ATTACHMENT_ADD,
            ChangePayload::workspace(workspace)
                .set("attachment_id", &attachment_id)
                .set("sha256", &stored.sha256)
                .set("byte_size", stored.byte_size)
                .set("media_type", &stored.facts.media_type)
                .set("filename", &input.filename)
                .set("alt_text", &input.alt_text)
                .set("width", stored.facts.width)
                .set("height", stored.facts.height)
                .set("created_at", &created_at),
        )
        .await?;

        sqlx::query(
            "INSERT INTO task_attachments(workspace_id, attachment_id, task_id, sha256, byte_size, media_type, filename, alt_text, width, height, created_at, created_by_change_id)
             VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)",
        )
        .bind(&workspace.id)
        .bind(&attachment_id)
        .bind(task_id)
        .bind(&stored.sha256)
        .bind(stored.byte_size)
        .bind(&stored.facts.media_type)
        .bind(&input.filename)
        .bind(&input.alt_text)
        .bind(stored.facts.width)
        .bind(stored.facts.height)
        .bind(&created_at)
        .bind(&change_id)
        .execute(&mut *tx)
        .await?;
        tx.commit().await?;
        Ok((attachment_id, true))
    }
    .await;
    let release_result = crate::attachments::lifecycle::release_lease(conn, &staging_lease).await;
    let (attachment_id, created) = match database_result {
        Ok(result) => result,
        Err(error) => {
            crate::attachments::storage::remove_staged_blob_if_unreferenced(
                conn,
                blob_dir,
                &stored.sha256,
            )
            .await;
            return Err(error);
        }
    };
    release_result?;
    crate::attachments::lifecycle::reconcile_liveness(
        conn,
        &crate::attachments::lifecycle::SystemClock,
    )
    .await?;
    let outcome = attachment_by_id(conn, workspace, &attachment_id).await?;
    Ok(AttachmentAddOutcome {
        outcome,
        created,
        optimized: optimized_flag,
    })
}

pub async fn attachment_by_id(
    conn: &mut SqliteConnection,
    workspace: &Workspace,
    attachment_id: &str,
) -> Result<AttachmentOutcome> {
    let row = sqlx::query(
        "SELECT ta.workspace_id, ta.attachment_id, ta.task_id, ta.sha256, ta.byte_size,
                ta.media_type, ta.filename, ta.alt_text, ta.width, ta.height,
                ta.created_at, ta.created_by_change_id, ta.deleted, ta.deleted_at, ta.deleted_by_change_id
         FROM task_attachments ta
         WHERE ta.workspace_id = ? AND ta.attachment_id = ?",
    )
    .bind(&workspace.id)
    .bind(attachment_id)
    .fetch_optional(&mut *conn)
    .await?;

    let Some(row) = row else {
        bail!("error attachment-not-found id={}", attachment_id);
    };

    let attachment = attachment_from_row(&row);
    let has_blob = attachment_has_blob(conn, &attachment.sha256).await?;
    Ok(AttachmentOutcome {
        attachment,
        has_blob,
    })
}

pub async fn delete_task_attachment(
    conn: &mut SqliteConnection,
    workspace: &Workspace,
    attachment_id: &str,
) -> Result<AttachmentOutcome> {
    let row = sqlx::query(
        "SELECT ta.workspace_id, ta.attachment_id, ta.task_id, ta.sha256, ta.byte_size,
                ta.media_type, ta.filename, ta.alt_text, ta.width, ta.height,
                ta.created_at, ta.created_by_change_id, ta.deleted, ta.deleted_at, ta.deleted_by_change_id
         FROM task_attachments ta
         WHERE ta.workspace_id = ? AND ta.attachment_id = ?",
    )
    .bind(&workspace.id)
    .bind(attachment_id)
    .fetch_optional(&mut *conn)
    .await?;

    let Some(row) = row else {
        bail!("error attachment-not-found id={}", attachment_id);
    };

    let deleted: bool = row.get::<i64, _>("deleted") != 0;
    if deleted {
        let attachment = attachment_from_row(&row);
        let has_blob = attachment_has_blob(conn, &attachment.sha256).await?;
        return Ok(AttachmentOutcome {
            attachment,
            has_blob,
        });
    }

    let task_id: String = row.get("task_id");
    let deleted_at = now();

    let mut tx = begin_immediate(conn).await?;

    let change_id = append_change(
        &mut tx,
        ChangeEntity::Task,
        &task_id,
        Some("attachments"),
        op_type::ATTACHMENT_DELETE,
        ChangePayload::workspace(workspace)
            .set("attachment_id", attachment_id)
            .set("deleted_at", &deleted_at),
    )
    .await?;

    sqlx::query(
        "UPDATE task_attachments SET deleted = 1, deleted_at = ?, deleted_by_change_id = ? WHERE workspace_id = ? AND attachment_id = ?",
    )
    .bind(&deleted_at)
    .bind(&change_id)
    .bind(&workspace.id)
    .bind(attachment_id)
    .execute(&mut *tx)
    .await?;

    tx.commit().await?;
    crate::attachments::lifecycle::reconcile_liveness(
        conn,
        &crate::attachments::lifecycle::SystemClock,
    )
    .await?;

    attachment_by_id(conn, workspace, attachment_id).await
}

pub async fn attachment_read_items_by_task(
    conn: &mut SqliteConnection,
    workspace_id: &str,
    task_id: &str,
    include_deleted: bool,
) -> Result<Vec<AttachmentReadItem>> {
    let attachments = attachments_by_task(conn, workspace_id, task_id, include_deleted).await?;
    let mut items = Vec::with_capacity(attachments.len());
    for attachment in attachments {
        let bytes_state = attachment_bytes_state(conn, &attachment.sha256).await?;
        items.push(AttachmentReadItem {
            attachment,
            has_blob: bytes_state == AttachmentBytesState::Present,
            bytes_state,
        });
    }
    Ok(items)
}

pub async fn attachments_by_task(
    conn: &mut SqliteConnection,
    workspace_id: &str,
    task_id: &str,
    include_deleted: bool,
) -> Result<Vec<TaskAttachment>> {
    let rows = if include_deleted {
        sqlx::query(
            "SELECT ta.workspace_id, ta.attachment_id, ta.task_id, ta.sha256, ta.byte_size,
                    ta.media_type, ta.filename, ta.alt_text, ta.width, ta.height,
                    ta.created_at, ta.created_by_change_id, ta.deleted, ta.deleted_at, ta.deleted_by_change_id
             FROM task_attachments ta
             WHERE ta.workspace_id = ? AND ta.task_id = ?
             ORDER BY ta.created_at, ta.attachment_id",
        )
        .bind(workspace_id)
        .bind(task_id)
        .fetch_all(&mut *conn)
        .await?
    } else {
        sqlx::query(
            "SELECT ta.workspace_id, ta.attachment_id, ta.task_id, ta.sha256, ta.byte_size,
                    ta.media_type, ta.filename, ta.alt_text, ta.width, ta.height,
                    ta.created_at, ta.created_by_change_id, ta.deleted, ta.deleted_at, ta.deleted_by_change_id
             FROM task_attachments ta
             WHERE ta.workspace_id = ? AND ta.task_id = ? AND ta.deleted = 0
             ORDER BY ta.created_at, ta.attachment_id",
        )
        .bind(workspace_id)
        .bind(task_id)
        .fetch_all(&mut *conn)
        .await?
    };

    let mut attachments = Vec::with_capacity(rows.len());
    for row in &rows {
        attachments.push(attachment_from_row(row));
    }
    Ok(attachments)
}

impl Database {
    pub async fn add_task_attachment(
        &self,
        workspace: &Workspace,
        blob_dir: &Path,
        policy: crate::attachments::lifecycle::LifecyclePolicy,
        task_id: &TaskId,
        input: AttachmentAddInput,
    ) -> Result<AttachmentAddOutcome> {
        let mut conn = self.acquire().await?;
        add_task_attachment(&mut conn, workspace, blob_dir, policy, task_id, input).await
    }

    pub async fn add_ordered_task_attachment(
        &self,
        workspace: &Workspace,
        blob_dir: &Path,
        policy: crate::attachments::lifecycle::LifecyclePolicy,
        task_id: &TaskId,
        created_at: String,
        input: TaskAttachmentAddInput,
    ) -> Result<AttachmentAddOutcome> {
        let mut conn = self.acquire().await?;
        add_ordered_task_attachment(
            &mut conn, workspace, blob_dir, policy, task_id, created_at, input,
        )
        .await
    }

    pub async fn attachment_by_id(
        &self,
        workspace: &Workspace,
        attachment_id: &str,
    ) -> Result<AttachmentOutcome> {
        let mut conn = self.acquire().await?;
        attachment_by_id(&mut conn, workspace, attachment_id).await
    }

    pub async fn delete_task_attachment(
        &self,
        workspace: &Workspace,
        attachment_id: &str,
    ) -> Result<AttachmentOutcome> {
        let mut conn = self.acquire().await?;
        delete_task_attachment(&mut conn, workspace, attachment_id).await
    }

    pub async fn attachment_read_items_by_task(
        &self,
        workspace_id: &WorkspaceId,
        task_id: &TaskId,
        include_deleted: bool,
    ) -> Result<Vec<AttachmentReadItem>> {
        let mut conn = self.acquire().await?;
        attachment_read_items_by_task(
            &mut conn,
            workspace_id.as_str(),
            task_id.as_str(),
            include_deleted,
        )
        .await
    }

    pub async fn prune_attachments(
        &self,
        blob_dir: &Path,
        policy: crate::attachments::lifecycle::LifecyclePolicy,
        apply: bool,
    ) -> Result<crate::attachments::lifecycle::PruneSummary> {
        let mut conn = self.acquire().await?;
        crate::attachments::lifecycle::prune(
            &mut conn,
            blob_dir,
            policy,
            apply,
            &crate::attachments::lifecycle::SystemClock,
        )
        .await
    }

    pub async fn acquire_attachment_lease(&self, sha256: &str, purpose: &str) -> Result<String> {
        let mut conn = self.acquire().await?;
        crate::attachments::lifecycle::acquire_lease(
            &mut conn,
            sha256,
            purpose,
            &crate::attachments::lifecycle::SystemClock,
        )
        .await
    }

    pub async fn acquire_live_attachment_read_lease(
        &self,
        workspace: &Workspace,
        attachment_id: &str,
    ) -> Result<AttachmentReadLease> {
        let mut conn = self.acquire().await?;
        let row = sqlx::query(
            "SELECT ta.sha256, ta.media_type, bi.available
             FROM task_attachments ta
             JOIN tasks t ON t.workspace_id = ta.workspace_id AND t.id = ta.task_id
             LEFT JOIN blob_inventory bi ON bi.sha256 = ta.sha256
             WHERE ta.workspace_id = ? AND ta.attachment_id = ?
               AND ta.deleted = 0 AND t.deleted = 0",
        )
        .bind(&workspace.id)
        .bind(attachment_id)
        .fetch_optional(&mut *conn)
        .await?
        .ok_or_else(|| anyhow::anyhow!("error attachment-invalidated"))?;
        if !row.try_get::<bool, _>("available").unwrap_or(false) {
            bail!("error attachment-blob-unavailable");
        }
        let sha256: String = row.get("sha256");
        let media_type: String = row.get("media_type");
        let lease_id = crate::attachments::lifecycle::acquire_lease(
            &mut conn,
            &sha256,
            "read",
            &crate::attachments::lifecycle::SystemClock,
        )
        .await?;
        Ok(AttachmentReadLease {
            sha256,
            media_type,
            lease_id,
        })
    }

    pub async fn release_attachment_lease(&self, lease_id: &str) -> Result<()> {
        let mut conn = self.acquire().await?;
        crate::attachments::lifecycle::release_lease(&mut conn, lease_id).await
    }

    pub async fn attachment_lifecycle_report(
        &self,
        blob_dir: &Path,
        policy: crate::attachments::lifecycle::LifecyclePolicy,
    ) -> Result<crate::attachments::lifecycle::LifecycleReport> {
        let mut conn = self.acquire().await?;
        crate::attachments::lifecycle::lifecycle_report(
            &mut conn,
            blob_dir,
            policy,
            &crate::attachments::lifecycle::SystemClock,
        )
        .await
    }
}