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:
16
crates/tessera-core/Cargo.toml
Normal file
16
crates/tessera-core/Cargo.toml
Normal file
@@ -0,0 +1,16 @@
|
||||
[package]
|
||||
name = "tessera-core"
|
||||
version.workspace = true
|
||||
edition.workspace = true
|
||||
license.workspace = true
|
||||
|
||||
[dependencies]
|
||||
anyhow.workspace = true
|
||||
chrono.workspace = true
|
||||
serde.workspace = true
|
||||
serde_json.workspace = true
|
||||
thiserror.workspace = true
|
||||
uuid.workspace = true
|
||||
hex.workspace = true
|
||||
sha2.workspace = true
|
||||
utoipa = { workspace = true }
|
||||
45
crates/tessera-core/src/config.rs
Normal file
45
crates/tessera-core/src/config.rs
Normal file
@@ -0,0 +1,45 @@
|
||||
use serde::{Deserialize, Serialize};
|
||||
use uuid::Uuid;
|
||||
|
||||
#[derive(Debug, Clone, Serialize, Deserialize)]
|
||||
pub struct VaultConfig {
|
||||
pub vault_id: Uuid,
|
||||
pub vault_path: String,
|
||||
pub created_at: chrono::DateTime<chrono::Utc>,
|
||||
}
|
||||
|
||||
#[derive(Debug, Clone, Serialize, Deserialize)]
|
||||
pub struct HubConfig {
|
||||
pub listen: String,
|
||||
pub data_dir: String,
|
||||
pub admin_password_hash: Option<String>,
|
||||
}
|
||||
|
||||
impl Default for HubConfig {
|
||||
fn default() -> Self {
|
||||
Self {
|
||||
listen: "0.0.0.0:8787".into(),
|
||||
data_dir: "./data".into(),
|
||||
admin_password_hash: None,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Debug, Clone, Serialize, Deserialize)]
|
||||
#[serde(rename_all = "snake_case")]
|
||||
pub enum ProviderKind {
|
||||
Filesystem,
|
||||
Postgres,
|
||||
Command,
|
||||
}
|
||||
|
||||
#[derive(Debug, Clone, Serialize, Deserialize)]
|
||||
pub struct OrbitSpec {
|
||||
pub id: Uuid,
|
||||
pub name: String,
|
||||
pub provider: ProviderKind,
|
||||
pub provider_config: serde_json::Value,
|
||||
pub schedule: Option<String>,
|
||||
pub decay_preset: String,
|
||||
pub dry_run: bool,
|
||||
}
|
||||
119
crates/tessera-core/src/decay.rs
Normal file
119
crates/tessera-core/src/decay.rs
Normal 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);
|
||||
}
|
||||
}
|
||||
33
crates/tessera-core/src/errors.rs
Normal file
33
crates/tessera-core/src/errors.rs
Normal file
@@ -0,0 +1,33 @@
|
||||
use thiserror::Error;
|
||||
|
||||
#[derive(Debug, Error)]
|
||||
pub enum TesseraError {
|
||||
#[error("vault is locked: {0}")]
|
||||
VaultLocked(String),
|
||||
#[error("wrong passphrase")]
|
||||
WrongPassphrase,
|
||||
#[error("provider unreachable: {0}")]
|
||||
ProviderUnreachable(String),
|
||||
#[error("disk full: need {needed} bytes")]
|
||||
DiskFull { needed: u64 },
|
||||
#[error("{0}")]
|
||||
UserMessage(String),
|
||||
}
|
||||
|
||||
impl TesseraError {
|
||||
pub fn user_message(&self) -> &str {
|
||||
match self {
|
||||
TesseraError::VaultLocked(_) => {
|
||||
"Another orbit is still circling the vault. Try again when it finishes."
|
||||
}
|
||||
TesseraError::WrongPassphrase => "That key doesn't unlock this vault.",
|
||||
TesseraError::ProviderUnreachable(_) => {
|
||||
"We couldn't reach the source at this outpost."
|
||||
}
|
||||
TesseraError::DiskFull { .. } => {
|
||||
"The vault is out of space—free room or widen the hold."
|
||||
}
|
||||
TesseraError::UserMessage(m) => m.as_str(),
|
||||
}
|
||||
}
|
||||
}
|
||||
15
crates/tessera-core/src/lib.rs
Normal file
15
crates/tessera-core/src/lib.rs
Normal file
@@ -0,0 +1,15 @@
|
||||
pub mod config;
|
||||
pub mod decay;
|
||||
pub mod errors;
|
||||
pub mod manifest;
|
||||
pub mod orbit;
|
||||
pub mod reconstitute;
|
||||
pub mod wormhole;
|
||||
|
||||
pub use config::*;
|
||||
pub use decay::*;
|
||||
pub use errors::*;
|
||||
pub use manifest::*;
|
||||
pub use orbit::*;
|
||||
pub use reconstitute::*;
|
||||
pub use wormhole::*;
|
||||
41
crates/tessera-core/src/manifest.rs
Normal file
41
crates/tessera-core/src/manifest.rs
Normal file
@@ -0,0 +1,41 @@
|
||||
use serde::{Deserialize, Serialize};
|
||||
use sha2::{Digest, Sha256};
|
||||
use uuid::Uuid;
|
||||
|
||||
#[derive(Debug, Clone, Serialize, Deserialize)]
|
||||
pub struct SnapshotManifest {
|
||||
pub snapshot_id: Uuid,
|
||||
pub orbit_id: Uuid,
|
||||
pub orbit_name: String,
|
||||
pub labels: Vec<String>,
|
||||
pub provider: String,
|
||||
pub paths: Vec<String>,
|
||||
pub restore_hints: serde_json::Value,
|
||||
}
|
||||
|
||||
pub fn manifest_hash(manifest: &SnapshotManifest) -> String {
|
||||
let json = serde_json::to_vec(manifest).expect("manifest serializes");
|
||||
let digest = Sha256::digest(&json);
|
||||
hex::encode(digest)
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::*;
|
||||
#[test]
|
||||
fn hash_is_stable() {
|
||||
let m = SnapshotManifest {
|
||||
snapshot_id: Uuid::nil(),
|
||||
orbit_id: Uuid::nil(),
|
||||
orbit_name: "nightly".into(),
|
||||
labels: vec!["filesystem".into()],
|
||||
provider: "filesystem".into(),
|
||||
paths: vec!["/tmp".into()],
|
||||
restore_hints: serde_json::json!({}),
|
||||
};
|
||||
let h1 = manifest_hash(&m);
|
||||
let h2 = manifest_hash(&m);
|
||||
assert_eq!(h1, h2);
|
||||
assert_eq!(h1.len(), 64);
|
||||
}
|
||||
}
|
||||
21
crates/tessera-core/src/orbit.rs
Normal file
21
crates/tessera-core/src/orbit.rs
Normal file
@@ -0,0 +1,21 @@
|
||||
use serde::{Deserialize, Serialize};
|
||||
|
||||
#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize, utoipa::ToSchema)]
|
||||
#[serde(rename_all = "snake_case")]
|
||||
pub enum OrbitRunState {
|
||||
Idle,
|
||||
Preparing,
|
||||
Capturing,
|
||||
Sealing,
|
||||
ApplyingDecay,
|
||||
Completed,
|
||||
Failed,
|
||||
Rehearsed,
|
||||
}
|
||||
|
||||
#[derive(Debug, Clone, Serialize, Deserialize)]
|
||||
pub struct OrbitRun {
|
||||
pub state: OrbitRunState,
|
||||
pub dry_run: bool,
|
||||
pub message: Option<String>,
|
||||
}
|
||||
33
crates/tessera-core/src/reconstitute.rs
Normal file
33
crates/tessera-core/src/reconstitute.rs
Normal file
@@ -0,0 +1,33 @@
|
||||
use serde::{Deserialize, Serialize};
|
||||
use uuid::Uuid;
|
||||
|
||||
pub const RECONSTITUTE_GRACE_SECS: u64 = 12;
|
||||
|
||||
#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize, utoipa::ToSchema)]
|
||||
#[serde(rename_all = "snake_case")]
|
||||
pub enum ReconstituteState {
|
||||
Idle,
|
||||
Confirming,
|
||||
Countdown,
|
||||
Restoring,
|
||||
Completed,
|
||||
Failed,
|
||||
Aborted,
|
||||
}
|
||||
|
||||
#[derive(Debug, Clone, Serialize, Deserialize, utoipa::ToSchema)]
|
||||
pub struct ReconstituteSession {
|
||||
pub snapshot_id: Uuid,
|
||||
pub target: String,
|
||||
pub state: ReconstituteState,
|
||||
pub seconds_remaining: u64,
|
||||
}
|
||||
|
||||
impl ReconstituteSession {
|
||||
pub fn countdown_copy(&self) -> String {
|
||||
format!(
|
||||
"Reconstituting in {}s — abort restores nothing.",
|
||||
self.seconds_remaining
|
||||
)
|
||||
}
|
||||
}
|
||||
26
crates/tessera-core/src/wormhole.rs
Normal file
26
crates/tessera-core/src/wormhole.rs
Normal file
@@ -0,0 +1,26 @@
|
||||
use serde::{Deserialize, Serialize};
|
||||
use uuid::Uuid;
|
||||
|
||||
#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
|
||||
#[serde(rename_all = "snake_case")]
|
||||
pub enum PairingState {
|
||||
Unpaired,
|
||||
CodeShown,
|
||||
Claimed,
|
||||
Active,
|
||||
Offline,
|
||||
}
|
||||
|
||||
#[derive(Debug, Clone, Serialize, Deserialize)]
|
||||
pub struct PairingCode {
|
||||
pub code: String,
|
||||
pub expires_at: chrono::DateTime<chrono::Utc>,
|
||||
pub hub_outpost_id: Option<Uuid>,
|
||||
}
|
||||
|
||||
#[derive(Debug, Clone, Serialize, Deserialize)]
|
||||
pub struct WormholeCredentials {
|
||||
pub outpost_id: Uuid,
|
||||
pub upload_token: String,
|
||||
pub hub_url: String,
|
||||
}
|
||||
Reference in New Issue
Block a user