relay-knowledge 1.1.17

Graph-database-based knowledge graph project.
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
790
791
792
793
794
795
796
//! Owns durable code-scope retention planning and pruning.

use std::collections::BTreeSet;

use rusqlite::{Connection, OptionalExtension, TransactionBehavior, params};

use crate::{
    clock::system_now_millis_or_zero,
    domain::{CodeIndexMode, CodeRepositoryRetentionJobStatus, CodeScopeRetentionSummary},
    storage::{CodeScopeRetentionRequest, StorageError},
};

use super::super::{lifecycle::commit_scope, workspace};
use super::retention_gc;
use super::retention_publications::{
    CommitReference, latest_successful_incremental_base, latest_successful_incremental_base_since,
    scope_is_queryable, successful_scopes_since,
};
use super::worktree::{
    active_worktree_base_scopes, compatible_non_retiring_scopes_for_commit,
    worktree_overlay_base_commit, worktree_task_base_commit,
};

mod task_history;

use task_history::finished_task_history_pending;
pub(super) use task_history::prune_finished_task_history;

pub(super) const RETAIN_SUCCEEDED_TASK_AUDIT_ROWS: usize = 128;
pub(super) const RETAIN_FAILED_TASK_AUDIT_ROWS: usize = 64;
const MAX_SCOPE_STATUS_ROWS: usize = 64;
const MAX_EXPLICIT_RETENTION_PINS: usize = 512;
const MAX_PUBLICATION_CANDIDATE_ROWS: usize = RETAIN_SUCCEEDED_TASK_AUDIT_ROWS + 1;

pub(in crate::storage::sqlite::code) fn retention_status(
    connection: &mut Connection,
    repository_id: &str,
) -> Result<CodeScopeRetentionSummary, StorageError> {
    let active_scope = connection
        .query_row(
            "SELECT last_indexed_scope_id FROM code_repositories WHERE repository_id = ?1",
            params![repository_id],
            |row| row.get::<_, Option<String>>(0),
        )
        .optional()?
        .flatten()
        .unwrap_or_default();
    let repository_retention = super::repository_retention_job(connection, repository_id)?;
    let plan = retention_plan(
        connection,
        repository_id,
        &active_scope,
        2,
        Vec::new(),
        repository_retention.as_ref(),
    )?;
    let retiring_jobs = retention_gc::jobs(connection, repository_id)?;
    let protected_aliases = protected_alias_commits(
        connection,
        repository_id,
        &plan.unfinished_tasks,
        plan.latest_incremental_base.as_ref(),
    )?;
    let audit_pending = finished_task_history_pending(connection, repository_id)?
        || commit_scope::repository_alias_pruning_pending(
            connection,
            repository_id,
            &protected_aliases,
        )?;
    Ok(summary_from_plan(
        repository_id,
        plan,
        Vec::new(),
        retiring_jobs,
        audit_pending,
        repository_retention,
    ))
}

pub(in crate::storage::sqlite::code) fn prune_scopes(
    connection: &mut Connection,
    request: CodeScopeRetentionRequest,
) -> Result<CodeScopeRetentionSummary, StorageError> {
    prune_scopes_with_retained(connection, request, Vec::new())
}

pub(in crate::storage::sqlite::code) fn prune_scopes_with_retained(
    connection: &mut Connection,
    request: CodeScopeRetentionRequest,
    extra_retained_scopes: Vec<String>,
) -> Result<CodeScopeRetentionSummary, StorageError> {
    let explicit_repository_retention = match (
        request.repository_retention_cutoff_ms,
        request.repository_retention_initial_scope.clone(),
    ) {
        (Some(cutoff_ms), Some(initial_scope)) => Some(CodeRepositoryRetentionJobStatus {
            repository_id: request.repository_id.clone(),
            initial_scope,
            cutoff_ms,
            cutoff_publication_generation: request
                .repository_retention_cutoff_generation
                .unwrap_or_default(),
            phase: "retiring_scopes".to_owned(),
            created_at_ms: cutoff_ms,
            updated_at_ms: cutoff_ms,
            last_error: None,
        }),
        (None, None) if request.repository_retention_cutoff_generation.is_none() => None,
        _ => {
            return Err(StorageError::InvalidInput(
                "repository retention cutoff and initial scope must be provided together"
                    .to_owned(),
            ));
        }
    };
    let persisted_repository_retention =
        super::repository_retention_job(connection, &request.repository_id)?;
    let complete_repository_retention =
        explicit_repository_retention.is_none() && persisted_repository_retention.is_some();
    let repository_retention = persisted_repository_retention.or(explicit_repository_retention);
    run_retention_pass(
        connection,
        &request.repository_id,
        &request.active_scope,
        request.retain_recent_successful_scopes,
        extra_retained_scopes,
        repository_retention,
        complete_repository_retention,
    )
}

fn retention_plan(
    connection: &Connection,
    repository_id: &str,
    active_scope: &str,
    retain_recent_successful_scopes: usize,
    extra_retained_scopes: Vec<String>,
    repository_retention: Option<&CodeRepositoryRetentionJobStatus>,
) -> Result<RetentionPlan, StorageError> {
    let mut retained = BTreeSet::new();
    let current_active_scope = connection
        .query_row(
            "SELECT last_indexed_scope_id FROM code_repositories WHERE repository_id = ?1",
            params![repository_id],
            |row| row.get::<_, Option<String>>(0),
        )
        .optional()?
        .flatten();
    let user_set_scopes =
        user_repository_set_member_scopes(connection, repository_id, MAX_EXPLICIT_RETENTION_PINS)?;
    let repository_set_protected =
        repository_retention.is_some() && !user_set_scopes.scopes.is_empty();
    let user_pins_truncated = user_set_scopes.truncated;
    retained.extend(user_set_scopes.scopes);

    let active_repository_retention = repository_retention.filter(|_| !repository_set_protected);
    let (latest_incremental_base, publication_history_incomplete) =
        if let Some(repository_retention) = active_repository_retention {
            let mut protected_publications = successful_scopes_since(
                connection,
                repository_id,
                repository_retention.cutoff_ms,
                repository_retention.cutoff_publication_generation,
                &repository_retention.initial_scope,
                MAX_SCOPE_STATUS_ROWS,
            )?;
            if let Some(current_active_scope) = current_active_scope
                .as_ref()
                .filter(|scope| *scope != &repository_retention.initial_scope)
            {
                protected_publications
                    .scopes
                    .push(current_active_scope.clone());
            }
            protected_publications.scopes.sort();
            protected_publications.scopes.dedup();
            for scope in &protected_publications.scopes {
                retained.insert(scope.clone());
                retained.extend(active_worktree_base_scopes(
                    connection,
                    repository_id,
                    scope,
                )?);
            }
            (
                latest_successful_incremental_base_since(
                    connection,
                    repository_id,
                    repository_retention.cutoff_ms,
                    repository_retention.cutoff_publication_generation,
                    &repository_retention.initial_scope,
                    current_active_scope
                        .as_deref()
                        .filter(|scope| *scope != repository_retention.initial_scope),
                )?,
                protected_publications.truncated,
            )
        } else {
            if let Some(current_active_scope) = &current_active_scope {
                retained.insert(current_active_scope.clone());
            }
            if !active_scope.is_empty() {
                retained.insert(active_scope.to_owned());
            }
            let mut active_scopes = BTreeSet::new();
            if !active_scope.is_empty() {
                active_scopes.insert(active_scope.to_owned());
            }
            if let Some(current_active_scope) = current_active_scope {
                active_scopes.insert(current_active_scope);
            }
            for scope in active_scopes {
                retained.extend(active_worktree_base_scopes(
                    connection,
                    repository_id,
                    &scope,
                )?);
            }
            let recent = recent_successful_scopes(
                connection,
                repository_id,
                retain_recent_successful_scopes,
            )?;
            for scope in recent.scopes {
                retained.insert(scope);
            }
            (
                latest_successful_incremental_base(connection, repository_id)?,
                recent.incomplete,
            )
        };
    let unfinished_tasks = unfinished_tasks(connection, repository_id)?;
    for scope in unfinished_task_scopes(connection, repository_id, &unfinished_tasks)? {
        retained.insert(scope);
    }
    if let Some(base) = &latest_incremental_base {
        retained.extend(scopes_for_commit(
            connection,
            repository_id,
            &base.resolved_commit_sha,
            &base.path_filters_json,
            &base.language_filters_json,
        )?);
    }
    for scope in extra_retained_scopes {
        retained.insert(scope);
    }
    let (mut prunable, scope_listing_truncated) =
        prunable_scopes(connection, repository_id, &retained, MAX_SCOPE_STATUS_ROWS)?;
    if publication_history_incomplete || repository_set_protected {
        // A legacy publication history can exceed the fixed candidate window.
        // Audit compaction still advances this pass, but scope retirement waits
        // until the newest distinct publications can be proven without a scan.
        prunable.clear();
    }
    Ok(RetentionPlan {
        retained,
        prunable,
        scope_listing_truncated: scope_listing_truncated
            || user_pins_truncated
            || publication_history_incomplete,
        publication_history_incomplete,
        repository_set_protected,
        unfinished_tasks,
        latest_incremental_base,
    })
}

fn run_retention_pass(
    connection: &mut Connection,
    repository_id: &str,
    active_scope: &str,
    retain_recent_successful_scopes: usize,
    extra_retained_scopes: Vec<String>,
    repository_retention: Option<CodeRepositoryRetentionJobStatus>,
    complete_repository_retention: bool,
) -> Result<CodeScopeRetentionSummary, StorageError> {
    let transaction = connection.transaction_with_behavior(TransactionBehavior::Immediate)?;
    let plan = retention_plan(
        &transaction,
        repository_id,
        active_scope,
        retain_recent_successful_scopes,
        extra_retained_scopes,
        repository_retention.as_ref(),
    )?;
    let now_ms = system_now_millis_or_zero();
    let had_job = !retention_gc::jobs(&transaction, repository_id)?.is_empty();
    let mut pruned = Vec::new();
    if had_job {
        if let Some(scope) = retention_gc::process_one(&transaction, repository_id, now_ms)? {
            pruned.push(scope);
        }
    } else if let Some(scope) = plan.prunable.first() {
        // The protected set and logical retirement are committed atomically under
        // BEGIN IMMEDIATE, so a concurrent writer cannot publish a new pin between them.
        retention_gc::schedule(&transaction, repository_id, scope, now_ms)?;
    }
    // Each maintenance category receives one fixed batch on every pass. Scope
    // retirement still advances at most one job phase, while a large scope can
    // no longer starve terminal-task or commit-alias audit retention.
    prune_finished_task_history(&transaction, repository_id, None)?;
    let protected = protected_alias_commits(
        &transaction,
        repository_id,
        &plan.unfinished_tasks,
        plan.latest_incremental_base.as_ref(),
    )?;
    commit_scope::prune_repository_aliases(&transaction, repository_id, &protected)?;
    let retiring_jobs = retention_gc::jobs(&transaction, repository_id)?;
    let protected_aliases = protected_alias_commits(
        &transaction,
        repository_id,
        &plan.unfinished_tasks,
        plan.latest_incremental_base.as_ref(),
    )?;
    let audit_pending = finished_task_history_pending(&transaction, repository_id)?
        || commit_scope::repository_alias_pruning_pending(
            &transaction,
            repository_id,
            &protected_aliases,
        )?;
    let repository_retention_complete = plan.repository_set_protected
        || (complete_repository_retention
            && !plan.publication_history_incomplete
            && plan.prunable.is_empty()
            && retiring_jobs.is_empty());
    let repository_retention = if repository_retention_complete {
        if let Some(job) = &repository_retention {
            super::complete_repository_retention(&transaction, repository_id, job.cutoff_ms)?;
        }
        None
    } else {
        repository_retention
            .map(|mut job| {
                let (phase, last_error) = retiring_jobs.first().map_or_else(
                    || ("retiring_scopes".to_owned(), None),
                    |scope_job| {
                        (
                            format!("scope_gc:{}", scope_job.phase),
                            scope_job.last_error.clone(),
                        )
                    },
                );
                super::update_repository_retention(
                    &transaction,
                    repository_id,
                    job.cutoff_ms,
                    &phase,
                    last_error.as_deref(),
                    now_ms,
                )?;
                job.phase = phase;
                job.updated_at_ms = now_ms;
                job.last_error = last_error;
                Ok::<_, StorageError>(job)
            })
            .transpose()?
    };
    let summary = summary_from_plan(
        repository_id,
        plan,
        pruned,
        retiring_jobs,
        audit_pending,
        repository_retention,
    );
    transaction.commit()?;
    Ok(summary)
}

fn summary_from_plan(
    repository_id: &str,
    plan: RetentionPlan,
    pruned: Vec<String>,
    retiring_jobs: Vec<crate::domain::CodeScopeRetirementJobStatus>,
    audit_pending: bool,
    repository_retention_job: Option<CodeRepositoryRetentionJobStatus>,
) -> CodeScopeRetentionSummary {
    let maintenance_pending = !plan.prunable.is_empty()
        || !retiring_jobs.is_empty()
        || audit_pending
        || repository_retention_job.is_some();
    let mut retained_scopes = plan.retained.into_iter().collect::<Vec<_>>();
    let retained_scope_count = retained_scopes.len();
    let retained_truncated = retained_scopes.len() > MAX_SCOPE_STATUS_ROWS;
    retained_scopes.truncate(MAX_SCOPE_STATUS_ROWS);
    CodeScopeRetentionSummary {
        repository_id: repository_id.to_owned(),
        retained_scope_count,
        prunable_scope_count: plan.prunable.len(),
        pruned_scope_count: pruned.len(),
        scope_listing_truncated: plan.scope_listing_truncated || retained_truncated,
        retiring_job_count: retiring_jobs.len(),
        maintenance_pending,
        retained_scopes,
        prunable_scopes: plan.prunable,
        pruned_scopes: pruned,
        retiring_jobs,
        repository_retention_job,
    }
}

struct RetentionPlan {
    retained: BTreeSet<String>,
    prunable: Vec<String>,
    scope_listing_truncated: bool,
    publication_history_incomplete: bool,
    repository_set_protected: bool,
    unfinished_tasks: Vec<UnfinishedTask>,
    latest_incremental_base: Option<CommitReference>,
}

pub(super) struct ScopePage {
    pub(super) scopes: Vec<String>,
    pub(super) truncated: bool,
}

struct PublishedScope {
    source_scope: String,
    publication_generation: u64,
    published_at_ms: u64,
}

struct PublishedScopePage {
    scopes: Vec<String>,
    incomplete: bool,
}

fn prunable_scopes(
    connection: &Connection,
    repository_id: &str,
    retained: &BTreeSet<String>,
    limit: usize,
) -> Result<(Vec<String>, bool), StorageError> {
    let auto_set_id = workspace::workspace_set_id(repository_id);
    let query_limit = limit.saturating_add(1);
    let mut scopes = prunable_scope_rows(
        connection,
        "code_repository_scopes",
        "AND candidate.retiring = 0",
        repository_id,
        &auto_set_id,
        retained,
        query_limit,
    )?;
    scopes.extend(prunable_scope_rows(
        connection,
        "code_repository_index_checkpoints",
        "AND NOT EXISTS (
             SELECT 1 FROM code_repository_scopes scope
             WHERE scope.source_scope = candidate.source_scope
         )",
        repository_id,
        &auto_set_id,
        retained,
        query_limit,
    )?);
    scopes.sort();
    scopes.dedup();
    let truncated = scopes.len() > limit;
    scopes.truncate(limit);
    Ok((scopes, truncated))
}

fn prunable_scope_rows(
    connection: &Connection,
    table: &'static str,
    table_filter: &'static str,
    repository_id: &str,
    auto_set_id: &str,
    retained: &BTreeSet<String>,
    limit: usize,
) -> Result<Vec<String>, StorageError> {
    use rusqlite::types::Value;

    let mut values = vec![
        Value::Text(repository_id.to_owned()),
        Value::Text(repository_id.to_owned()),
        Value::Text(auto_set_id.to_owned()),
    ];
    let retained_clause = if retained.is_empty() {
        String::new()
    } else {
        values.extend(retained.iter().cloned().map(Value::Text));
        let placeholders = std::iter::repeat_n("?", retained.len())
            .collect::<Vec<_>>()
            .join(", ");
        format!("AND candidate.source_scope NOT IN ({placeholders})")
    };
    values.push(Value::Integer(limit as i64));
    let query = format!(
        "SELECT candidate.source_scope
             FROM {table} candidate
             WHERE candidate.repository_id = ?
               {table_filter}
               AND NOT EXISTS (
                 SELECT 1 FROM code_repository_scope_gc_jobs job
                 WHERE job.source_scope = candidate.source_scope
             )
               AND NOT EXISTS (
                   SELECT 1
                   FROM code_repository_set_members member
                   WHERE member.repository_id = ?
                     AND member.source_scope = candidate.source_scope
                     AND member.set_id <> ?
               )
               {retained_clause}
             ORDER BY candidate.source_scope ASC
             LIMIT ?"
    );
    let mut statement = connection.prepare(&query)?;
    statement
        .query_map(rusqlite::params_from_iter(values), |row| {
            row.get::<_, String>(0)
        })?
        .collect::<Result<Vec<_>, _>>()
        .map_err(StorageError::from)
}

fn recent_successful_scopes(
    connection: &Connection,
    repository_id: &str,
    limit: usize,
) -> Result<PublishedScopePage, StorageError> {
    if limit > MAX_SCOPE_STATUS_ROWS {
        return Err(StorageError::InvalidInput(format!(
            "retain_recent_successful_scopes exceeds the bounded maximum of {MAX_SCOPE_STATUS_ROWS}"
        )));
    }
    if limit == 0 {
        return Ok(PublishedScopePage {
            scopes: Vec::new(),
            incomplete: false,
        });
    }
    let mut tasks = successful_task_candidates(connection, repository_id)?;
    let history_truncated = tasks.len() > MAX_PUBLICATION_CANDIDATE_ROWS;
    tasks.truncate(MAX_PUBLICATION_CANDIDATE_ROWS);
    tasks.extend(completed_checkpoint_candidates(
        connection,
        repository_id,
        limit.saturating_add(1),
    )?);
    let mut queryable = Vec::with_capacity(tasks.len());
    for candidate in tasks {
        if scope_is_queryable(connection, repository_id, &candidate.source_scope)? {
            queryable.push(candidate);
        }
    }
    let mut tasks = queryable;
    tasks.sort_by(|left, right| {
        (
            right.publication_generation > 0,
            right.publication_generation,
            right.published_at_ms,
            &right.source_scope,
        )
            .cmp(&(
                left.publication_generation > 0,
                left.publication_generation,
                left.published_at_ms,
                &left.source_scope,
            ))
    });
    let mut seen_scopes = BTreeSet::new();
    tasks.retain(|candidate| seen_scopes.insert(candidate.source_scope.clone()));
    let incomplete = history_truncated && tasks.len() < limit;
    tasks.truncate(limit);
    Ok(PublishedScopePage {
        scopes: tasks
            .into_iter()
            .map(|candidate| candidate.source_scope)
            .collect(),
        incomplete,
    })
}

fn successful_task_candidates(
    connection: &Connection,
    repository_id: &str,
) -> Result<Vec<PublishedScope>, StorageError> {
    let mut statement = connection.prepare(
        "SELECT source_scope, publication_generation, updated_at_ms
         FROM code_repository_index_tasks
              INDEXED BY code_repository_index_tasks_publication_retention
         WHERE repository_id = ?1 AND state = 'succeeded'
         ORDER BY publication_generation DESC, updated_at_ms DESC,
                  created_at_ms DESC, task_id DESC
         LIMIT ?2",
    )?;
    statement
        .query_map(
            params![repository_id, MAX_PUBLICATION_CANDIDATE_ROWS + 1],
            |row| {
                Ok(PublishedScope {
                    source_scope: row.get(0)?,
                    publication_generation: row.get(1)?,
                    published_at_ms: row.get(2)?,
                })
            },
        )?
        .collect::<Result<Vec<_>, _>>()
        .map_err(StorageError::from)
}

fn completed_checkpoint_candidates(
    connection: &Connection,
    repository_id: &str,
    limit: usize,
) -> Result<Vec<PublishedScope>, StorageError> {
    let mut statement = connection.prepare(
        "SELECT source_scope, updated_at_ms
         FROM code_repository_index_checkpoints
              INDEXED BY code_repository_index_checkpoints_publication_retention
         WHERE repository_id = ?1 AND state IN ('complete', 'completed')
         ORDER BY updated_at_ms DESC, source_scope DESC
         LIMIT ?2",
    )?;
    statement
        .query_map(params![repository_id, limit], |row| {
            Ok(PublishedScope {
                source_scope: row.get(0)?,
                publication_generation: 0,
                published_at_ms: row.get(1)?,
            })
        })?
        .collect::<Result<Vec<_>, _>>()
        .map_err(StorageError::from)
}

fn unfinished_tasks(
    connection: &Connection,
    repository_id: &str,
) -> Result<Vec<UnfinishedTask>, StorageError> {
    let mut statement = connection.prepare(
        "
        SELECT source_scope, ref_selector, resolved_commit_sha,
               path_filters_json, language_filters_json, mode_json
        FROM code_repository_index_tasks
        WHERE repository_id = ?1 AND state IN ('queued', 'running', 'retrying')
        ",
    )?;
    let rows = statement.query_map(params![repository_id], |row| {
        Ok(UnfinishedTask {
            source_scope: row.get(0)?,
            ref_selector: row.get(1)?,
            resolved_commit_sha: row.get(2)?,
            path_filters_json: row.get(3)?,
            language_filters_json: row.get(4)?,
            mode_json: row.get(5)?,
        })
    })?;
    rows.collect::<Result<Vec<_>, _>>()
        .map_err(StorageError::from)
}

fn unfinished_task_scopes(
    connection: &Connection,
    repository_id: &str,
    tasks: &[UnfinishedTask],
) -> Result<Vec<String>, StorageError> {
    let mut retained = BTreeSet::new();
    for task in tasks {
        retained.insert(task.source_scope.clone());
        for base_ref in task.base_refs()? {
            retained.extend(scopes_for_commit(
                connection,
                repository_id,
                &base_ref,
                &task.path_filters_json,
                &task.language_filters_json,
            )?);
        }
    }

    Ok(retained.into_iter().collect())
}

fn scopes_for_commit(
    connection: &Connection,
    repository_id: &str,
    resolved_commit_sha: &str,
    path_filters_json: &str,
    language_filters_json: &str,
) -> Result<Vec<String>, StorageError> {
    compatible_non_retiring_scopes_for_commit(
        connection,
        repository_id,
        resolved_commit_sha,
        path_filters_json,
        language_filters_json,
    )
}

fn protected_alias_commits(
    connection: &Connection,
    repository_id: &str,
    unfinished_tasks: &[UnfinishedTask],
    latest_incremental_base: Option<&CommitReference>,
) -> Result<BTreeSet<String>, StorageError> {
    let mut protected = BTreeSet::new();
    if let Some(active_commit) = connection
        .query_row(
            "SELECT last_indexed_commit FROM code_repositories WHERE repository_id = ?1",
            params![repository_id],
            |row| row.get::<_, Option<String>>(0),
        )
        .optional()?
        .flatten()
    {
        if let Some(base_commit) = worktree_overlay_base_commit(&active_commit) {
            protected.insert(base_commit.to_owned());
        }
        protected.insert(active_commit);
    }
    if let Some(base) = latest_incremental_base {
        protected.insert(base.resolved_commit_sha.clone());
    }
    for task in unfinished_tasks {
        protected.insert(task.resolved_commit_sha.clone());
        protected.extend(task.base_refs()?);
    }
    Ok(protected)
}

struct UnfinishedTask {
    source_scope: String,
    ref_selector: String,
    resolved_commit_sha: String,
    path_filters_json: String,
    language_filters_json: String,
    mode_json: String,
}

impl UnfinishedTask {
    fn base_refs(&self) -> Result<Vec<String>, StorageError> {
        let mode = serde_json::from_str::<CodeIndexMode>(&self.mode_json).map_err(|error| {
            StorageError::InvalidInput(format!(
                "unfinished code index task has invalid mode: {error}"
            ))
        })?;
        let mut refs = BTreeSet::new();
        match mode {
            CodeIndexMode::Incremental { base_ref, .. } => {
                refs.insert(base_ref);
            }
            CodeIndexMode::WorktreeOverlay => {
                refs.insert(self.ref_selector.clone());
                if let Some(base_ref) =
                    worktree_task_base_commit(&self.resolved_commit_sha, &self.ref_selector)
                {
                    refs.insert(base_ref.to_owned());
                }
            }
            CodeIndexMode::Full => {}
        }
        Ok(refs.into_iter().collect())
    }
}

fn user_repository_set_member_scopes(
    connection: &Connection,
    repository_id: &str,
    limit: usize,
) -> Result<ScopePage, StorageError> {
    let auto_set_id = workspace::workspace_set_id(repository_id);
    let mut statement = connection.prepare(
        "SELECT DISTINCT source_scope
         FROM code_repository_set_members
         WHERE repository_id = ?1 AND set_id <> ?2
         ORDER BY source_scope
         LIMIT ?3",
    )?;
    let mut scopes = statement
        .query_map(
            params![repository_id, auto_set_id, limit.saturating_add(1)],
            |row| row.get::<_, String>(0),
        )?
        .collect::<Result<Vec<_>, _>>()?;
    let truncated = scopes.len() > limit;
    scopes.truncate(limit);
    Ok(ScopePage { scopes, truncated })
}

#[cfg(test)]
#[path = "retention_tests.rs"]
mod tests;

#[cfg(test)]
#[path = "retention_fairness_tests.rs"]
mod fairness_tests;

#[cfg(test)]
#[path = "retention_lifecycle_tests.rs"]
mod lifecycle_tests;