feat: download lifecycle — stalled and failed downloads are given up and searched again; imported ones seed, then go (M502 #5523)
release / govulncheck (push) Successful in 17s
release / web (push) Failing after 17s
release / go (push) Successful in 1m11s
release / integration (push) Successful in 4m50s
release / android (push) Successful in 4m54s
release / Build signed APK (releases and dev) (push) Successful in 5m9s
release / Attach APK to the Release (tag releases only) (push) Skipped
release / Build + push container image (push) Skipped
release / Verify release artifacts (tag releases only) (push) Skipped

Ported from Fabled Librarian's System 104. A new lifecycle poller, every
ten minutes:
- a download the client fails is given up at once;
- one nobody shares, or still fetching its file list, past the stall
  setting (6 h) is asked for again three times, then given up;
- giving up removes the download with its files, holds the release back
  for a week (release_blocks.expires_at), ends the grab and searches the
  album again, with no limit per album;
- a running download the client stops listing for half an hour ends and is
  searched again, not held back; an empty list is not taken as gone;
- an imported download seeds to ratio 2.0 or 14 days, then is removed;
- a download the client would not remove is asked again every pass.

Every condition and act is one open row per (client, download, condition)
in download_interventions. Activity shows what is wrong with a download and
how often it was retried, why one ended, its seeding, and a banner for a
client that does not answer. A running download can be given up by hand
(set aside for good). The seed ratio, seed days and stall hours are on the
search settings card. The download client lookup moves to
acquire.DownloadClientFor, shared by the importer and the lifecycle.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
2026-10-10 15:27:22 -04:00
co-authored by Claude Opus 5.5
parent d9e00d8bc2
commit 8245f4406e
34 changed files with 2362 additions and 106 deletions
@@ -1,23 +1,32 @@
<script lang="ts">
import { Download as DownloadIcon } from 'lucide-svelte';
import { useQueryClient } from '@tanstack/svelte-query';
import { createDownloadsQuery, importAnyway, refuseDownload, retryImport } from '#lib/api/wanted.js';
import {
abandonDownload,
createDownloadsQuery,
importAnyway,
refuseDownload,
retryImport
} from '#lib/api/wanted.js';
import { errMessage } from '#lib/api/errors.js';
import { pushToast } from '#lib/stores/toast.svelte.js';
import { relativeTime } from '#lib/utils/relativeTime.js';
import ListContinuation from '#lib/components/ListContinuation.svelte';
import type { Download, ImportEvidence } from '#lib/api/types.js';
import type { Download, DownloadCondition, ImportEvidence } from '#lib/api/types.js';
// The Activity tab (M502 #5522): every download Minstrel took, running
// ones first. A finished download is checked against MusicBrainz before it
// is filed; one the check could not settle waits here for a person, who
// imports it anyway or refuses it. A refused release is set aside (the
// Blocked tab) and its album searched for again.
// Blocked tab) and its album searched for again. A download that fails, or
// that nobody shares, is retried and then given up the same way, held back
// for a week; an imported one seeds, then is removed (#5523).
const client = useQueryClient();
const downloadsStore = createDownloadsQuery();
const downloads = $derived($downloadsStore);
const items = $derived((downloads.data?.pages ?? []).flatMap((p) => p.items) as Download[]);
const clientsDown = $derived(downloads.data?.pages[0]?.clients_down ?? []);
let busy = $state<string | null>(null);
@@ -39,9 +48,25 @@
const onRetry = (d: Download) => act(d, retryImport, `${d.name} will be tried again within the minute.`);
const onRefuse = (d: Download) =>
act(d, refuseDownload, `Refused ${d.name}. It's set aside and the album will be searched for again.`);
const onAbandon = (d: Download) =>
act(d, abandonDownload, `Gave up on ${d.name}. It's set aside and the album will be searched for again.`);
// A download still downloading, or waiting to be: one a person may give up.
const running = (d: Download) =>
!d.ended_at && (d.import_state === '' || d.import_state === 'downloading' || d.import_state === 'waiting');
// How a download that ended without an import ended.
const endings: Record<string, string> = {
stalled: 'Given up',
failed: 'Failed',
abandoned: 'Given up by you',
gone: 'Left the client',
superseded: 'Replaced'
};
// The state of a download as a short label, and whether it needs a person.
function label(d: Download): string {
if (d.ended_at && endings[d.end_reason]) return endings[d.end_reason];
switch (d.import_state) {
case 'downloading':
case '':
@@ -68,7 +93,7 @@
function badge(d: Download): string {
if (needsYou(d)) return 'border-accent text-text-primary';
if (d.import_state === 'refused') return 'border-error-fg text-error-fg';
if (d.import_state === 'refused' || (d.ended_at && endings[d.end_reason])) return 'border-error-fg text-error-fg';
return 'border-border text-text-secondary';
}
@@ -82,6 +107,26 @@
return parts.join(' · ');
}
// What the lifecycle sees wrong with a running download, and what it did.
function condition(c: DownloadCondition): string {
let said = c.detail;
if (c.trigger === 'no_peers' || c.trigger === 'no_metadata') {
said += c.retries
? ` Asked for again ${c.retries} of 3 times; given up after that.`
: ' It will be asked for again three times, then given up.';
}
return said;
}
// What ended a download that was not imported, in the lifecycle's words.
const ending = (d: Download) => (d.ended_at && endings[d.end_reason] ? d.end_detail : '');
function seeded(d: Download): string | null {
if (d.seed_released_at) return `Seeded, then removed from the download client ${relativeTime(d.seed_released_at)}.`;
if (d.seeding) return `Seeding: share ratio ${d.ratio.toFixed(2)}.`;
return null;
}
function when(d: Download): string {
const who = d.origin === 'operator' ? 'by you' : 'by the schedule';
let said = `Taken ${who} ${relativeTime(d.grabbed_at)}`;
@@ -103,6 +148,17 @@
'rounded-md border border-border px-2.5 py-1 text-xs text-text-secondary hover:bg-surface-hover hover:text-text-primary disabled:opacity-50';
</script>
{#each clientsDown as c (c.client)}
<p
class="mb-3 rounded-lg border border-error-fg bg-surface p-3 text-sm text-text-primary"
role="status"
data-testid="client-down"
>
Can't reach the download client {c.client} since {relativeTime(c.since)}: {c.detail}. Its
downloads are left as they are until it answers.
</p>
{/each}
{#if downloads.isPending}
<p class="text-text-secondary">Reading the downloads…</p>
{:else if downloads.isError && !downloads.isFetchNextPageError}
@@ -147,6 +203,12 @@
</div>
{/if}
{#if running(d) && d.condition}
<p class="text-xs text-text-primary" data-testid="download-condition">{condition(d.condition)}</p>
{/if}
{#if ending(d)}
<p class="text-xs text-text-secondary" data-testid="download-ending">{ending(d)}</p>
{/if}
{#if d.import_reason}
<p class="text-xs {needsYou(d) ? 'text-text-primary' : 'text-text-secondary'}" data-testid="download-reason">
{d.import_reason}
@@ -158,12 +220,21 @@
{#if filed(d)}
<p class="truncate text-xs text-text-secondary" title={d.destination}>{filed(d)}</p>
{/if}
{#if seeded(d)}
<p class="text-xs text-text-muted" data-testid="download-seeding">{seeded(d)}</p>
{/if}
{#if d.rehomed_from}
<p class="text-xs text-text-muted">Taken for {d.rehomed_from}; it turned out to be this album.</p>
{/if}
<p class="text-xs text-text-muted">{when(d)}</p>
{#if needsYou(d)}
{#if running(d)}
<div class="flex flex-wrap gap-1.5 pt-1">
<button type="button" class={button} disabled={busy === d.id} onclick={() => onAbandon(d)}>
Give up
</button>
</div>
{:else if needsYou(d)}
<div class="flex flex-wrap gap-1.5 pt-1">
{#if d.import_state === 'review'}
<button type="button" class={button} disabled={busy === d.id} onclick={() => onImport(d)}>