Introduce session persistence and artifact caching

This change implements a robust session management system allowing users to persist distance calculation samples and parameters across invocations. It includes concurrency-safe directory locking, deterministic parameter encoding, artifact integrity checks, and caching mechanisms to skip resampling when valid data is available.
This commit is contained in:
Eric Coissac committed 2026-09-12 07:46:55 +02:00
1 parent a8bcf5ffac
commit 7861e886e7
15 files changed
+746 -59

No files matched your search

+1
View File
@@ -24,6 +24,7 @@ obikstats = { path = "../obikstats" }
obikquery = { path = "../obikquery" }
obikidxcache = { path = "../obikidxcache" }
obikphylo = { path = "../obikphylo" }
obiksession = { path = "../obiksession" }
obikrope = { path = "../obikrope" }
obifastwrite = { path = "../obifastwrite" }
obiskbuilder = { path = "../obiskbuilder" }
+21
View File
@@ -224,6 +224,27 @@ pub struct PhyloArgs {
#[arg(long)]
pub entropy_sd: Option<f64>,
/// Persist the `snp-*` `--distance` sample (the pairwise substitution
/// tally) in `DIR` across separate `obikmer phylo` invocations: a later
/// run against the same index with the exact same
/// `--subsample`/`--free-loss`/`--no-ambiguity`/`--exclude-genome`/
/// `--min-shared-family`/`--entropy`/`--entropy-sd` restores it instead
/// of resampling. `DIR` is created if it doesn't exist. If it already
/// holds a sample built under *different* selection parameters, this
/// run is rejected with an error listing what changed, unless
/// `--session-force` is also given (which discards the old sample and
/// starts fresh under this run's parameters). Only covers a `snp-*`
/// `--distance` today — `--sankoff`/`--tnt`/`--phyg`/`--iqtree` don't
/// read or write a `--session` yet.
#[arg(long, value_name = "DIR")]
pub session: Option<PathBuf>,
/// With `--session DIR`: overwrite its saved selection parameters (and
/// discard its cached sample) instead of erroring out when this run's
/// parameters don't match. No effect without `--session`.
#[arg(long, requires = "session")]
pub session_force: bool,
/// Calibrate a 16-state Sankoff cost matrix (and its matching
/// pseudo-alignment) from an already-built sibling annex — requires
/// `--subsample <N>`, and shares `--free-loss`/`--no-ambiguity`/
+79
View File
@@ -24,6 +24,43 @@ use phylip::write_phylip_relaxed;
use sankoff::{write_sankoff_alignment_fasta, write_sankoff_matrix_csv, write_sankoff_params};
use tnt::write_sankoff_tnt;
/// Deterministic byte encoding of the `snp-*` `--distance` selection
/// parameters, for `obiksession::Session::open`'s params-conflict check.
/// `obiksession` treats this as an opaque blob (see its own crate docs) —
/// only this module needs to know the layout, and only well enough to
/// produce/compare it, never to pretty-print a per-field diff (v1 keeps
/// the conflict message generic rather than decoding both sides field by
/// field — see its use in `run` below).
fn session_params_blob(
n: Option<usize>,
free_loss: bool,
no_ambiguity: bool,
excluded: &[bool],
entropy_bias: Option<EntropyBias>,
) -> Vec<u8> {
let mut buf = Vec::new();
match n {
Some(v) => {
buf.push(1);
buf.extend_from_slice(&(v as u64).to_le_bytes());
}
None => buf.push(0),
}
buf.push(free_loss as u8);
buf.push(no_ambiguity as u8);
buf.extend_from_slice(&(excluded.len() as u64).to_le_bytes());
buf.extend(excluded.iter().map(|&b| b as u8));
match entropy_bias {
Some(EntropyBias { mu, sigma }) => {
buf.push(1);
buf.extend_from_slice(&mu.to_le_bytes());
buf.extend_from_slice(&sigma.to_le_bytes());
}
None => buf.push(0),
}
buf
}
pub use args::PhyloArgs;
pub fn run(args: PhyloArgs) {
@@ -236,6 +273,47 @@ pub fn run(args: PhyloArgs) {
None
};
// ── `--session`: persist the snp-* sample across separate invocations ──
// Only covers `snp-*` `--distance` today (see `--session`'s own docs);
// `None` when `--session` isn't given, matching today's always-resample
// default exactly.
let session = args.session.as_ref().map(|dir| {
let params = session_params_blob(
args.subsample,
args.free_loss,
args.no_ambiguity,
&snp_exclude_mask,
entropy_bias,
);
match obiksession::Session::open(dir, &params, args.session_force) {
Ok(Ok(session)) => {
match session.outcome() {
obiksession::SessionOutcome::Created => {
info!("--session {}: starting fresh", dir.display());
}
obiksession::SessionOutcome::Reused => {
info!("--session {}: found, will reuse its cached sample if present", dir.display());
}
}
session
}
Ok(Err(_conflict)) => {
eprintln!(
"error: --session {} was built with different selection parameters \
(--subsample/--free-loss/--no-ambiguity/--exclude-genome/--min-shared-family/\
--entropy/--entropy-sd) than this run — pass --session-force to discard its \
cached sample and start fresh, or point --session at a different directory",
dir.display()
);
std::process::exit(1);
}
Err(e) => {
eprintln!("error opening --session {}: {e}", dir.display());
std::process::exit(1);
}
}
});
// ── SNP pseudo-alignment (`--pseudo-alignment`) ─────────────────────────────
if args.pseudo_alignment {
let Some(subsample_n) = args.subsample else {
@@ -389,6 +467,7 @@ pub fn run(args: PhyloArgs) {
&snp_exclude_mask,
entropy_bias,
gamma_shape,
session.as_ref(),
)
}
}