Initial Tessera backup vault (M0–M2)

Scaffold Rust workspace, React canvas, vault engine, hub API,
providers, agent wormhole path, and Docker compose per SPEC.
This commit is contained in:
Tessera Agent
2026-11-09 09:59:42 +00:00
commit a5ffb2c1b4
54 changed files with 8379 additions and 0 deletions

View File

@@ -0,0 +1,119 @@
use chrono::{DateTime, Utc};
use serde::{Deserialize, Serialize};
use uuid::Uuid;
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct DecayPolicy {
pub keep_daily: usize,
pub keep_weekly: usize,
pub keep_monthly: usize,
}
impl DecayPolicy {
pub fn from_preset(name: &str) -> Self {
match name {
"week-of-dailies" => Self {
keep_daily: 7,
keep_weekly: 0,
keep_monthly: 0,
},
"month-of-weeklies" => Self {
keep_daily: 0,
keep_weekly: 4,
keep_monthly: 0,
},
"year-of-monthlies" => Self {
keep_daily: 0,
keep_weekly: 0,
keep_monthly: 12,
},
_ => Self {
keep_daily: 7,
keep_weekly: 4,
keep_monthly: 12,
},
}
}
}
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct SnapshotRef {
pub id: Uuid,
pub taken_at: DateTime<Utc>,
}
/// Returns snapshot IDs to forget (newest-first input).
pub fn evaluate_decay(policy: &DecayPolicy, mut snapshots: Vec<SnapshotRef>) -> Vec<Uuid> {
snapshots.sort_by(|a, b| b.taken_at.cmp(&a.taken_at));
let mut keep = std::collections::HashSet::new();
if policy.keep_daily > 0 {
for s in snapshots.iter().take(policy.keep_daily) {
keep.insert(s.id);
}
}
if policy.keep_weekly > 0 {
let mut weeks: Vec<(i64, SnapshotRef)> = Vec::new();
for s in &snapshots {
let week = s.taken_at.timestamp() / (7 * 86400);
if let Some(pos) = weeks.iter().position(|(w, _)| *w == week) {
if s.taken_at > weeks[pos].1.taken_at {
weeks[pos] = (week, s.clone());
}
} else {
weeks.push((week, s.clone()));
}
}
weeks.sort_by(|a, b| b.1.taken_at.cmp(&a.1.taken_at));
for (_, s) in weeks.into_iter().take(policy.keep_weekly) {
keep.insert(s.id);
}
}
if policy.keep_monthly > 0 {
let mut months: Vec<(i32, u32, SnapshotRef)> = Vec::new();
for s in &snapshots {
let key = (s.taken_at.year(), s.taken_at.month());
if let Some(pos) = months.iter().position(|(y, m, _)| *y == key.0 && *m == key.1) {
if s.taken_at > months[pos].2.taken_at {
months[pos] = (key.0, key.1, s.clone());
}
} else {
months.push((key.0, key.1, s.clone()));
}
}
months.sort_by(|a, b| b.2.taken_at.cmp(&a.2.taken_at));
for (_, _, s) in months.into_iter().take(policy.keep_monthly) {
keep.insert(s.id);
}
}
snapshots
.into_iter()
.filter(|s| !keep.contains(&s.id))
.map(|s| s.id)
.collect()
}
use chrono::Datelike;
#[cfg(test)]
mod tests {
use super::*;
use chrono::Duration;
#[test]
fn keeps_n_dailies() {
let policy = DecayPolicy::from_preset("week-of-dailies");
let now = Utc::now();
let snaps: Vec<SnapshotRef> = (0..10)
.map(|i| SnapshotRef {
id: Uuid::new_v4(),
taken_at: now - Duration::days(i),
})
.collect();
let forget = evaluate_decay(&policy, snaps);
assert_eq!(forget.len(), 3);
}
}