owpurge 0.2.0

Askar wallet pruning for multi-tenant SSI platforms: a single static binary that prunes stale exchange records straight from the wallet store
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
//! The per-category purges with their DidCommMessage cascade.
//!
//! A credential or proof exchange has 3-6 `DidCommMessageRecord` children (the raw
//! DIDComm protocol payloads) linked by the `associatedRecordId` tag. They are deleted
//! before the parent so no orphan rows are left behind, and the dry-run counts them
//! too, so the reported total reflects actual row reduction rather than parent count.
//!
//! Children are resolved a batch at a time rather than a parent at a time. Because
//! `associatedRecordId` is a real Askar tag, a whole batch of 100 parents goes in a
//! single `any_of` query — one round trip instead of 100, with no dependence on the
//! connection pool being wide enough to run them in parallel.

use std::collections::HashMap;

use anyhow::Result;
use aries_askar::entry::TagFilter;
use aries_askar::Store;

use crate::config::Config;
use crate::credo::{category, tag, RecordValue, OOB_STATE_AWAIT_RESPONSE, OOB_STATE_DONE, TERMINAL_STATES};
use crate::deletes::{delete_ids, delete_pairs, throttle};
use crate::scan;

/// Parents per cascade batch.
const BATCH_SIZE: usize = 100;

#[derive(Debug, Default, Clone, Copy)]
pub struct PurgeStats {
    pub parents: u64,
    pub children: u64,
}

impl PurgeStats {
    pub fn add(&mut self, other: PurgeStats) {
        self.parents += other.parents;
        self.children += other.children;
    }
}

fn mode(dry_run: bool) -> &'static str {
    if dry_run {
        "DRY"
    } else {
        "DEL"
    }
}

/// Scan one category (optionally filtered by state) and collect the ids of records
/// past the TTL cutoff.
#[allow(clippy::too_many_arguments)]
async fn eligible_ids(
    store: &Store,
    cfg: &Config,
    profile: Option<String>,
    label: &str,
    category: &str,
    category_label: &str,
    state: Option<&str>,
    cutoff: chrono::DateTime<chrono::Utc>,
    exclude_reusable: bool,
) -> Result<Vec<String>> {
    let tag_filter = state.map(|s| TagFilter::is_eq(tag::STATE, s));
    let mut ids = Vec::new();

    scan::for_each(
        store,
        profile,
        category,
        tag_filter,
        label,
        category_label,
        cfg.heartbeat_every,
        BATCH_SIZE,
        cfg.dry_run,
        |entry| {
            let value: RecordValue = serde_json::from_slice(entry.value.as_ref()).unwrap_or_default();
            if !value.older_than(cutoff) {
                return Ok(false);
            }
            // A reusable await-response invitation is a live invitation URL. Never
            // delete it. `reusable` is not a tag, so this can only be checked here.
            if exclude_reusable && value.reusable == Some(true) {
                return Ok(false);
            }
            ids.push(entry.name.clone());
            Ok(true)
        },
    )
    .await?;

    Ok(ids)
}

/// Fetch the DidCommMessage children of a whole batch of parents in one query, keyed
/// by parent id.
async fn children_for(
    store: &Store,
    profile: Option<String>,
    parent_ids: &[String],
) -> Result<HashMap<String, Vec<String>>> {
    let mut map: HashMap<String, Vec<String>> = HashMap::new();
    if parent_ids.is_empty() {
        return Ok(map);
    }

    let filter = TagFilter::any_of(
        parent_ids
            .iter()
            .map(|id| TagFilter::is_eq(tag::ASSOCIATED_RECORD_ID, id.as_str()))
            .collect(),
    );

    let mut session = store.session(profile).await?;
    let entries = session
        .fetch_all(
            Some(category::DIDCOMM_MESSAGE),
            Some(filter),
            None,
            None,
            false,
            false,
        )
        .await?;

    for entry in entries {
        // Prefer the tag; fall back to the value for a record that somehow lacks it.
        let parent = scan::tag_value(&entry, tag::ASSOCIATED_RECORD_ID)
            .map(|s| s.to_string())
            .or_else(|| {
                serde_json::from_slice::<RecordValue>(entry.value.as_ref())
                    .ok()
                    .and_then(|v| v.associated_record_id)
            });
        if let Some(parent) = parent {
            map.entry(parent).or_default().push(entry.name);
        }
    }
    Ok(map)
}

/// Delete terminal exchange records older than the TTL, cascading to their children.
///
/// Shared by credentials and proofs: the two flows differ only in the category, so
/// they run through one function rather than two near-identical copies.
async fn purge_exchange(
    store: &Store,
    cfg: &Config,
    profile: Option<String>,
    label: &str,
    category: &'static str,
    category_label: &str,
) -> Result<PurgeStats> {
    let mut stats = PurgeStats::default();

    for state in TERMINAL_STATES {
        println!("\n[{label}] {category_label} — state: {state}");

        let eligible = eligible_ids(
            store,
            cfg,
            profile.clone(),
            label,
            category,
            category_label,
            Some(state),
            cfg.delete_before,
            false,
        )
        .await?;

        let mut state_parents = 0u64;
        let mut state_children = 0u64;

        for (i, chunk) in eligible.chunks(BATCH_SIZE).enumerate() {
            let children = children_for(store, profile.clone(), chunk).await?;
            let batch_children: usize = children.values().map(|v| v.len()).sum();

            if !cfg.dry_run {
                // Children first, then the parent, so no orphan rows are left behind.
                let mut pairs: Vec<(&str, String)> = Vec::with_capacity(batch_children + chunk.len());
                for parent in chunk {
                    if let Some(kids) = children.get(parent) {
                        for kid in kids {
                            pairs.push((category::DIDCOMM_MESSAGE, kid.clone()));
                        }
                    }
                    pairs.push((category, parent.clone()));
                }
                let outcome = delete_pairs(
                    store,
                    profile.clone(),
                    &pairs,
                    &format!("[{label}] {category_label} [{state}]"),
                    false,
                )
                .await?;
                outcome.report(category_label);
            }

            let done = (i * BATCH_SIZE) + chunk.len();
            state_parents += chunk.len() as u64;
            state_children += batch_children as u64;
            stats.parents += chunk.len() as u64;
            stats.children += batch_children as u64;

            println!(
                "  [{}] {} parents + {} children ({}/{})",
                mode(cfg.dry_run),
                chunk.len(),
                batch_children,
                done,
                eligible.len()
            );

            throttle(cfg.throttle_ms).await;
        }

        println!("  → state '{state}': {state_parents} parents, {state_children} children");
    }

    Ok(stats)
}

pub async fn purge_credentials(
    store: &Store,
    cfg: &Config,
    profile: Option<String>,
    label: &str,
) -> Result<PurgeStats> {
    purge_exchange(
        store,
        cfg,
        profile,
        label,
        category::CREDENTIAL_EXCHANGE,
        "Credentials",
    )
    .await
}

pub async fn purge_proofs(
    store: &Store,
    cfg: &Config,
    profile: Option<String>,
    label: &str,
) -> Result<PurgeStats> {
    purge_exchange(store, cfg, profile, label, category::PROOF_EXCHANGE, "Proofs").await
}

/// OOB records, per solution design §4.1:
///   Track 1 — state=done, any reusability, older than TTL
///   Track 2 — state=await-response, non-reusable only, older than TTL
///             (a reusable await-response is a live invitation URL — never delete)
pub async fn purge_oob(
    store: &Store,
    cfg: &Config,
    profile: Option<String>,
    label: &str,
) -> Result<PurgeStats> {
    let mut stats = PurgeStats::default();
    let tracks: [(&str, &str, bool); 2] = [
        ("done (terminal)", OOB_STATE_DONE, false),
        (
            "await-response (stuck, non-reusable)",
            OOB_STATE_AWAIT_RESPONSE,
            true,
        ),
    ];

    for (track_label, state, exclude_reusable) in tracks {
        println!("\n[{label}] OOB — {track_label}");

        let eligible = eligible_ids(
            store,
            cfg,
            profile.clone(),
            label,
            category::OUT_OF_BAND,
            "OOB",
            Some(state),
            cfg.delete_before,
            exclude_reusable,
        )
        .await?;

        let mut track_count = 0u64;
        for (i, chunk) in eligible.chunks(BATCH_SIZE).enumerate() {
            if !cfg.dry_run {
                let outcome = delete_ids(
                    store,
                    profile.clone(),
                    category::OUT_OF_BAND,
                    chunk,
                    &format!("[{label}] OOB [{track_label}]"),
                    false,
                )
                .await?;
                outcome.report("OOB");
            }
            let done = (i * BATCH_SIZE) + chunk.len();
            track_count += chunk.len() as u64;
            stats.parents += chunk.len() as u64;
            println!(
                "  [{}] {} records ({}/{})",
                mode(cfg.dry_run),
                chunk.len(),
                done,
                eligible.len()
            );
            throttle(cfg.throttle_ms).await;
        }
        println!("  → '{track_label}': {track_count}");
    }

    Ok(stats)
}

/// Records eligible purely by age, with no state filter.
async fn purge_by_age(
    store: &Store,
    cfg: &Config,
    profile: Option<String>,
    label: &str,
    category: &'static str,
    category_label: &str,
) -> Result<PurgeStats> {
    let mut stats = PurgeStats::default();
    println!("\n[{label}] {category_label}");

    let eligible = eligible_ids(
        store,
        cfg,
        profile.clone(),
        label,
        category,
        category_label,
        None,
        cfg.delete_before,
        false,
    )
    .await?;

    for (i, chunk) in eligible.chunks(BATCH_SIZE).enumerate() {
        if !cfg.dry_run {
            let outcome = delete_ids(
                store,
                profile.clone(),
                category,
                chunk,
                &format!("[{label}] {category_label}"),
                false,
            )
            .await?;
            outcome.report(category_label);
        }
        let done = (i * BATCH_SIZE) + chunk.len();
        stats.parents += chunk.len() as u64;
        println!(
            "  [{}] {} records ({}/{})",
            mode(cfg.dry_run),
            chunk.len(),
            done,
            eligible.len()
        );
        throttle(cfg.throttle_ms).await;
    }

    Ok(stats)
}

pub async fn purge_basic_messages(
    store: &Store,
    cfg: &Config,
    profile: Option<String>,
    label: &str,
) -> Result<PurgeStats> {
    purge_by_age(
        store,
        cfg,
        profile,
        label,
        category::BASIC_MESSAGE,
        "Basic messages",
    )
    .await
}

pub async fn purge_question_answer(
    store: &Store,
    cfg: &Config,
    profile: Option<String>,
    label: &str,
) -> Result<PurgeStats> {
    purge_by_age(
        store,
        cfg,
        profile,
        label,
        category::QUESTION_ANSWER,
        "Question-answer",
    )
    .await
}