ligase 0.4.0

Type-safe, reproducible computational biology pipelines (core engine)
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
//! A pinned remote: the same bytes forever, from the source.
//!
//! An SRA run, a PDB entry, HXB2 by accession. The source promises not to
//! change, the user pins the promise with a digest, and the engine checks it
//! **before any step runs** — because a digest that is only checked after a
//! result has been produced is a digest that explains a wrong answer rather
//! than preventing one.
//!
//! # What a digest is over
//!
//! What the file *means*, not what its bytes are: the canonical form of the
//! fetched file read as its declared kind, folded over the files in order
//! (D4). For a kind with no canonical form the fold is over the bytes, which
//! needs no special case because canonicalisation already falls back to them
//! (D21).
//!
//! This is the one choice here worth arguing about, and the argument is
//! `scripts/fetch_test_data.sh`'s, which M10 deletes: it pinned HXB2 by the
//! checksum of its *sequence* rather than of the file, because "NCBI may
//! reflow lines or reword a defline without the biology changing, and that
//! must not break the build". An alarm that fires on a cosmetic reformat
//! teaches people to bump pins without reading, and then the one that
//! matters gets bumped too. So:
//!
//! - the canonical digest differs from the pin → **the remote moved**, a
//!   loud failure naming both digests;
//! - the canonical digest matches and the bytes differ → the source rewrote
//!   the file and the biology is unchanged, which is a note (D26), because
//!   that is the honest statement and "this pipeline is broken" is not.
//!
//! Folding even a single file means a one-file dataset and a paired one are
//! described the same way, and that swapping a pair's mates is a different
//! digest — which it must be, since it is a different dataset.
//!
//! # Why resolution happens at graph-build time
//!
//! A remote's dimensions cannot be known until its bytes are in hand: you
//! cannot type-check data you have not got. So a remote resolves when the
//! pipeline declares it, and the result is an ordinary file with ordinary
//! dimensions, which is also why "a wrong digest fails before any step runs"
//! needs no arranging. On a warm store resolution touches no network at all.

use std::fmt;
use std::path::PathBuf;

use crate::cache::{Cache, CacheError, RemoteFile, RemoteRow};
use crate::canon::Form;
use crate::hash::ContentHash;
use crate::note::Note;
use crate::remote::fetch::{FetchError, Transport};
use crate::remote::locator::{Locator, LocatorError};
use crate::remote::request::Request;
use crate::store::{BlobStore, ObjectKey, StoreError};
use crate::types::{DataKind, Origin, RemoteVerdict};

/// Where a resolved remote's bytes came from on this run.
///
/// Which is also its audit verdict: a pinned remote served from the store is
/// as pinned as one just fetched, because the digest was checked either way.
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub enum Source {
    /// Already held, with a digest that satisfies the declaration. No
    /// network was touched.
    Store,
    /// Fetched on this run.
    Network,
}

impl fmt::Display for Source {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        f.write_str(match self {
            Self::Store => "from the store",
            Self::Network => "fetched",
        })
    }
}

/// A remote, resolved to files on this machine.
#[derive(Clone, Debug)]
pub struct Resolved {
    /// The files, in order, inside the content-addressed store.
    pub paths: Vec<PathBuf>,
    /// The digest of what they mean, which is what a pin is compared
    /// against.
    pub digest: ContentHash,
    /// What was recorded about the fetch.
    pub record: Record,
    /// Whether this run touched the network.
    pub from: Source,
    /// What resolving it noticed (D26).
    pub notes: Vec<Note>,
}

impl Resolved {
    /// The summary that travels with the loaded dataset.
    #[must_use]
    pub fn origin(&self) -> Origin {
        Origin {
            locator: self.record.locator.clone(),
            digest: self.record.digest,
            bytes: self.record.bytes,
            fetched_at: self.record.fetched_at,
            // Served from the store or fetched on this run, the digest was
            // checked either way, so the claim is the same.
            verdict: RemoteVerdict::Pinned,
        }
    }
}

/// What is known about a fetch, for provenance and the methods paragraph.
#[derive(Clone, Debug, PartialEq, Eq)]
pub struct Record {
    /// The locator as the user wrote it, which is what a reader can look up.
    pub locator: String,
    /// How the files were read.
    pub kind: DataKind,
    /// The digest of what they mean.
    pub digest: ContentHash,
    /// The digest of their bytes.
    pub bytes: ContentHash,
    /// The objects, in order.
    pub files: Vec<RemoteFile>,
    /// When they were fetched, as seconds since the epoch.
    pub fetched_at: i64,
}

/// Why a remote could not be resolved.
#[derive(Debug)]
pub enum RemoteError {
    /// The locator could not be read, or could not be turned into a URL.
    Locator(LocatorError),
    /// The source could not be reached, or did not answer with bytes.
    Fetch {
        /// What was asked for.
        locator: String,
        /// What went wrong.
        source: FetchError,
    },
    /// What arrived is not what the pin says it should be.
    ///
    /// The failure a pin exists to produce. Loud, before any step runs, and
    /// naming both digests so the two can be compared rather than merely
    /// disagreed about.
    Moved {
        /// What was asked for.
        locator: String,
        /// The digest the pipeline declared.
        expected: ContentHash,
        /// The digest of what arrived.
        found: ContentHash,
        /// Whether the bytes alone moved, with the meaning intact.
        ///
        /// False here by construction — a meaning-preserving rewrite is a
        /// note rather than this error — and kept so the message can say so
        /// where it is true in some future caller.
        cosmetic: bool,
    },
    /// What arrived cannot be read as the kind it was declared to be.
    ///
    /// NCBI answers a bad accession with a 200 and an HTML page. Nothing in
    /// the status says so, and with no digest declared there is nothing to
    /// compare against, so reading the file is the only thing between that
    /// page and a pipeline treating it as a reference.
    Unreadable {
        /// What was asked for.
        locator: String,
        /// How it was being read.
        kind: DataKind,
        /// Why it could not be.
        why: String,
    },
    /// The store or the cache metadata could not be used.
    Cache(CacheError),
    /// An object could not be held, or is not what its name says.
    Store(StoreError),
}

impl fmt::Display for RemoteError {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        match self {
            Self::Locator(error) => error.fmt(f),
            Self::Fetch { locator, source } => {
                write!(f, "`{locator}` could not be fetched: {source}")
            }
            Self::Moved {
                locator,
                expected,
                found,
                cosmetic,
            } => {
                write!(
                    f,
                    "the remote `{locator}` has moved: it was pinned to {} \
                     and it is now {}.",
                    expected.to_hex(),
                    found.to_hex()
                )?;
                if *cosmetic {
                    return f.write_str(
                        " Only its bytes differ; what it means is unchanged, \
                         so the pin can be updated without the result moving.",
                    );
                }
                f.write_str(
                    " This is a change in what the data means, not a \
                     reformatting, so check what changed at the source \
                     before updating the pin.",
                )
            }
            Self::Unreadable { locator, kind, why } => write!(
                f,
                "`{locator}` was fetched but cannot be read as {}: {why}. A \
                 200 is not a promise about the body -- an archive asked for \
                 an accession it does not have will answer with an error \
                 page",
                kind.name()
            ),
            Self::Cache(error) => error.fmt(f),
            Self::Store(error) => error.fmt(f),
        }
    }
}

impl std::error::Error for RemoteError {
    fn source(&self) -> Option<&(dyn std::error::Error + 'static)> {
        match self {
            Self::Locator(error) => Some(error),
            Self::Fetch { source, .. } => Some(source),
            Self::Cache(error) => Some(error),
            Self::Store(error) => Some(error),
            Self::Moved { .. } | Self::Unreadable { .. } => None,
        }
    }
}

impl From<CacheError> for RemoteError {
    fn from(error: CacheError) -> Self {
        Self::Cache(error)
    }
}

impl From<StoreError> for RemoteError {
    fn from(error: StoreError) -> Self {
        Self::Store(error)
    }
}

impl From<LocatorError> for RemoteError {
    fn from(error: LocatorError) -> Self {
        Self::Locator(error)
    }
}

/// Resolve a pinned remote to files on this machine.
///
/// A record whose objects are all still held and whose digest satisfies
/// `declared` is the answer, and no network is touched. Otherwise the source
/// is asked, what arrives is read as `kind`, and the digest is checked
/// before anything is stored or recorded — so a fetch that was not what the
/// pin promised leaves the cache exactly as it found it.
///
/// # Errors
///
/// [`RemoteError`] for a locator that cannot be read, a source that cannot
/// be reached, a body that cannot be read as `kind`, or a digest that does
/// not match `declared`.
pub fn resolve(
    locator: &Locator,
    kind: DataKind,
    declared: Option<ContentHash>,
    cache: &Cache,
    transport: &dyn Transport,
) -> Result<Resolved, RemoteError> {
    let written = locator.to_string();

    if let Some(resolved) = from_store(&written, kind, declared, cache)? {
        return Ok(resolved);
    }

    let previous = cache.remote(&written, kind)?;
    let fetched = fetch_all(locator, &written, kind, cache, transport)?;

    // Checked before anything is stored, so a fetch that is not what was
    // promised leaves nothing behind for a later run to find and trust.
    if let Some(expected) = declared
        && fetched.digest != expected
    {
        cache.forget_remote(&written, kind)?;
        return Err(RemoteError::Moved {
            locator: written,
            expected,
            found: fetched.digest,
            cosmetic: false,
        });
    }

    let mut notes = Vec::new();
    if let Some(previous) = previous
        && previous.digest == fetched.digest
        && previous.bytes != fetched.bytes
    {
        // The source rewrote the file and the biology is unchanged. Worth
        // saying, because a user who is told nothing will not know that the
        // archive has started serving different bytes; not worth failing
        // on, because nothing downstream moves.
        notes.push(Note::RemoteRewritten {
            locator: written.clone(),
        });
    }

    let row = RemoteRow {
        digest: fetched.digest,
        bytes: fetched.bytes,
        files: fetched.files.clone(),
        fetched_at: crate::prov::now(),
    };
    for (staged, file) in fetched.staged.iter().zip(&fetched.files) {
        cache.objects().put_file_as(staged, &file.key)?;
    }
    cache.record_remote(&written, kind, &row)?;

    Ok(Resolved {
        paths: paths_of(cache, &row)?,
        digest: row.digest,
        record: record_of(&written, kind, &row),
        from: Source::Network,
        notes,
    })
}

/// The answer from what is already held, if there is one.
fn from_store(
    written: &str,
    kind: DataKind,
    declared: Option<ContentHash>,
    cache: &Cache,
) -> Result<Option<Resolved>, RemoteError> {
    let Some(row) = cache.remote(written, kind)? else {
        return Ok(None);
    };
    // A record that does not satisfy the declaration is not the answer: the
    // user may have bumped the pin because the source legitimately changed,
    // and in that case the source is what to ask.
    if declared.is_some_and(|expected| expected != row.digest) {
        return Ok(None);
    }
    if row.files.is_empty() {
        return Ok(None);
    }
    let mut paths = Vec::with_capacity(row.files.len());
    for file in &row.files {
        // `local_path` rather than `checked_get`: the objects can be
        // gigabytes, and the cache's own prefilter is what decides when to
        // read them again. An object that has gone missing sends this back
        // to the source, which is the same answer a cold cache gives.
        match cache.objects().local_path(&file.key)? {
            Some(path) => paths.push(path),
            None => return Ok(None),
        }
    }
    Ok(Some(Resolved {
        paths,
        digest: row.digest,
        record: record_of(written, kind, &row),
        from: Source::Store,
        notes: Vec::new(),
    }))
}

/// What a fetch produced, before any of it is kept.
struct Fetched {
    staged: Vec<PathBuf>,
    files: Vec<RemoteFile>,
    digest: ContentHash,
    bytes: ContentHash,
}

/// Fetch every file a locator names, read each as `kind`, and fold the
/// digests in order.
fn fetch_all(
    locator: &Locator,
    written: &str,
    kind: DataKind,
    cache: &Cache,
    transport: &dyn Transport,
) -> Result<Fetched, RemoteError> {
    let urls = locator.urls()?;
    let suffixes = locator.suffixes();

    let mut staged = Vec::with_capacity(urls.len());
    let mut files = Vec::with_capacity(urls.len());
    let mut meanings = Vec::with_capacity(urls.len());
    let mut byte_hashes = Vec::with_capacity(urls.len());

    for (at, url) in urls.iter().enumerate() {
        let suffix = suffixes.get(at).cloned().unwrap_or_default();
        // Staged inside the cache directory, so moving it into the store is
        // a rename and a crash leaves its debris somewhere deletable.
        let into = cache.scratch(&format!(
            "fetch-{}-{at}{suffix}",
            ContentHash::of_bytes(written.as_bytes()).to_hex()
        ));
        // A pinned remote is a plain GET and is never authenticated: an
        // accession an archive undertakes not to change is one anybody can
        // ask for, and a pin that needed a token would not be a promise
        // anyone could hold the source to.
        let fetched = transport
            .fetch(&Request::get(url), &into)
            .map_err(|source| RemoteError::Fetch {
                locator: written.to_owned(),
                source,
            })?;

        let bytes = ContentHash::of_file(&into).map_err(|source| {
            RemoteError::Cache(CacheError::Io {
                path: into.clone(),
                source,
            })
        })?;
        byte_hashes.push(bytes);

        // What the file means, which is what the pin is over. Reading it is
        // also the only thing between an archive's error page and a
        // pipeline that treats it as data: see `Unreadable`.
        let canonical = cache
            .semantic(kind, &into)
            .map_err(|error| RemoteError::Unreadable {
                locator: written.to_owned(),
                kind,
                why: error.to_string(),
            })?;
        if canonical.form == Form::Raw {
            return Err(RemoteError::Unreadable {
                locator: written.to_owned(),
                kind,
                why: "it does not parse as that kind, so its bytes would \
                      have had to stand in for what it means"
                    .to_owned(),
            });
        }
        meanings.push(canonical.hash);

        files.push(RemoteFile {
            key: ObjectKey::new(bytes, &suffix),
            url: fetched.url,
            bytes: fetched.bytes,
        });
        staged.push(into);
    }

    Ok(Fetched {
        digest: ContentHash::of_hashes(&meanings),
        bytes: ContentHash::of_hashes(&byte_hashes),
        staged,
        files,
    })
}

/// Where the objects a record names sit on this filesystem.
fn paths_of(cache: &Cache, row: &RemoteRow) -> Result<Vec<PathBuf>, RemoteError> {
    row.files
        .iter()
        .map(|file| {
            cache
                .objects()
                .local_path(&file.key)
                .map_err(RemoteError::Store)
                .map(|found| found.unwrap_or_else(|| cache.objects().path_of(&file.key)))
        })
        .collect()
}

fn record_of(written: &str, kind: DataKind, row: &RemoteRow) -> Record {
    Record {
        locator: written.to_owned(),
        kind,
        digest: row.digest,
        bytes: row.bytes,
        files: row.files.clone(),
        fetched_at: row.fetched_at,
    }
}