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,54 @@
use anyhow::{Context, Result};
use std::io::{Read, Write};
use std::net::TcpStream;
pub fn get(url: &str) -> Result<String> {
request("GET", url, None, None)
}
pub fn post_json(url: &str, body: &serde_json::Value) -> Result<String> {
let payload = serde_json::to_string(body)?;
request("POST", url, Some(&payload), Some("application/json"))
}
fn request(method: &str, url: &str, body: Option<&str>, content_type: Option<&str>) -> Result<String> {
let (host, port, path) = parse_url(url)?;
let mut stream = TcpStream::connect((host.as_str(), port)).context("connect")?;
let mut req = format!("{method} {path} HTTP/1.1\r\nHost: {host}\r\nConnection: close\r\n");
if let Some(b) = body {
req.push_str(&format!("Content-Length: {}\r\n", b.len()));
if let Some(ct) = content_type {
req.push_str(&format!("Content-Type: {ct}\r\n"));
}
req.push_str("\r\n");
req.push_str(b);
} else {
req.push_str("\r\n");
}
stream.write_all(req.as_bytes())?;
let mut buf = String::new();
stream.read_to_string(&mut buf)?;
let body_start = buf.find("\r\n\r\n").map(|i| i + 4).unwrap_or(0);
Ok(buf[body_start..].trim().to_string())
}
fn parse_url(url: &str) -> Result<(String, u16, String)> {
let url = url.strip_prefix("http://").context("only http supported")?;
let (host_port, path) = url.split_once('/').unwrap_or((url, ""));
let path = if path.is_empty() {
"/".to_string()
} else {
format!("/{path}")
};
let (host, port) = if let Some((h, p)) = host_port.split_once(':') {
(h.to_string(), p.parse()?)
} else {
(host_port.to_string(), 80)
};
Ok((host, port, path))
}
pub fn post_empty(url: &str) -> Result<()> {
request("POST", url, None, None)?;
Ok(())
}

View File

@@ -0,0 +1,202 @@
mod http;
use anyhow::{Context, Result};
use clap::{Parser, Subcommand};
use tessera_store::Vault;
use uuid::Uuid;
#[derive(Parser, Clone)]
#[command(name = "tessera", about = "Tessera backup vault CLI")]
struct Cli {
#[command(subcommand)]
command: Commands,
#[arg(long, env = "TESSERA_DATA_DIR", default_value = "./data")]
data_dir: String,
#[arg(long, env = "TESSERA_VAULT_PASSPHRASE", default_value = "dev-vault-key")]
vault_pass: String,
#[arg(long, env = "TESSERA_HUB_URL", default_value = "http://127.0.0.1:8787")]
hub_url: String,
}
#[derive(Subcommand, Clone)]
enum Commands {
Init,
Status,
Orbit {
name: String,
#[arg(long, default_value = "filesystem")]
provider: String,
#[arg(long)]
path: Option<String>,
#[arg(long)]
schedule: Option<String>,
#[arg(long)]
dry_run: bool,
},
Snap {
#[arg(long)]
orbit: Option<String>,
},
List,
Reconstitute {
snapshot_id: Uuid,
target: String,
},
Decay {
#[arg(long, default_value = "week-of-dailies")]
preset: String,
},
Wormhole {
#[command(subcommand)]
cmd: WormholeCmd,
},
}
#[derive(Subcommand, Clone)]
enum WormholeCmd {
Pair {
code: String,
#[arg(long, default_value = "outpost")]
name: String,
},
}
fn main() -> Result<()> {
let cli = Cli::parse();
let command = cli.command.clone();
match command {
Commands::Init => cmd_init(&cli)?,
Commands::Status => cmd_status(&cli)?,
Commands::Orbit {
name,
provider,
path,
schedule,
dry_run,
} => cmd_orbit(&cli, name, provider, path, schedule, dry_run)?,
Commands::Snap { orbit } => cmd_snap(&cli, orbit)?,
Commands::List => cmd_list(&cli)?,
Commands::Reconstitute {
snapshot_id,
target,
} => cmd_reconstitute(&cli, snapshot_id, target)?,
Commands::Decay { preset } => cmd_decay(&cli, &preset)?,
Commands::Wormhole { ref cmd } => match cmd {
WormholeCmd::Pair { code, name } => wormhole_pair(&cli, code, name)?,
},
}
Ok(())
}
fn vault_path(data_dir: &str) -> std::path::PathBuf {
std::path::PathBuf::from(data_dir).join("vault")
}
fn cmd_init(cli: &Cli) -> Result<()> {
let vp = vault_path(&cli.data_dir);
if vp.join("config").exists() {
println!("Vault already initialized.");
return Ok(());
}
Vault::init(&vp, &cli.vault_pass).context("init vault")?;
println!("Vault initialized at {}", vp.display());
Ok(())
}
fn cmd_status(cli: &Cli) -> Result<()> {
match http::get(&format!("{}/health", cli.hub_url)) {
Ok(body) => println!("Hub: {body}"),
Err(_) => println!("Hub: offline (is tessera-hub running?)"),
}
let vp = vault_path(&cli.data_dir);
if vp.join("config").exists() {
let vault = Vault::open(&vp, &cli.vault_pass)?;
let n = vault.list_snapshots()?.len();
println!("Vault: online — {n} snapshots");
} else {
println!("Vault: not initialized (run tessera init)");
}
Ok(())
}
fn cmd_orbit(
cli: &Cli,
name: String,
provider: String,
path: Option<String>,
schedule: Option<String>,
dry_run: bool,
) -> Result<()> {
let config = match provider.as_str() {
"filesystem" => serde_json::json!({ "paths": [path.unwrap_or_else(|| ".".into())] }),
"postgres" => serde_json::json!({ "connection_url": path.context("--path or connection url required")? }),
_ => anyhow::bail!("unknown provider"),
};
let body = serde_json::json!({
"name": name,
"provider": provider,
"provider_config": config,
"schedule": schedule,
"dry_run": dry_run,
});
let raw = http::post_json(&format!("{}/api/v1/orbits", cli.hub_url), &body)?;
let orbit: serde_json::Value = serde_json::from_str(&raw)?;
println!("Orbit registered: {}", orbit["id"]);
Ok(())
}
fn cmd_snap(cli: &Cli, orbit_name: Option<String>) -> Result<()> {
let raw = http::get(&format!("{}/api/v1/orbits", cli.hub_url))?;
let orbits: serde_json::Value = serde_json::from_str(&raw)?;
let arr = orbits.as_array().context("orbits response")?;
let id = if let Some(name) = orbit_name {
arr.iter()
.find(|o| o["name"] == name)
.and_then(|o| o["id"].as_str())
.context("orbit not found")?
} else {
arr.first()
.and_then(|o| o["id"].as_str())
.context("no orbits defined")?
};
http::post_empty(&format!("{}/api/v1/orbits/{id}/run", cli.hub_url))?;
println!("Orbit run triggered.");
Ok(())
}
fn cmd_list(cli: &Cli) -> Result<()> {
let vault = Vault::open(&vault_path(&cli.data_dir), &cli.vault_pass)?;
for s in vault.list_snapshots()? {
println!(
"{} {} {} bytes orbit={}",
s.taken_at.to_rfc3339(),
s.id,
s.size_bytes,
s.manifest.orbit_name
);
}
Ok(())
}
fn cmd_reconstitute(cli: &Cli, snapshot_id: Uuid, target: String) -> Result<()> {
let vault = Vault::open(&vault_path(&cli.data_dir), &cli.vault_pass)?;
let n = vault.restore_snapshot(snapshot_id, std::path::Path::new(&target))?;
println!("Reconstituted {n} bytes to {target}");
Ok(())
}
fn cmd_decay(cli: &Cli, preset: &str) -> Result<()> {
let mut vault = Vault::open(&vault_path(&cli.data_dir), &cli.vault_pass)?;
let pruned = vault.prune_with_decay(preset)?;
println!("{} snapshots decayed.", pruned.len());
Ok(())
}
fn wormhole_pair(cli: &Cli, code: &str, name: &str) -> Result<()> {
let body = serde_json::json!({ "code": code, "outpost_name": name });
let raw = http::post_json(&format!("{}/api/v1/wormhole/claim", cli.hub_url), &body)?;
let res: serde_json::Value = serde_json::from_str(&raw)?;
println!("Paired outpost {}", res["outpost_id"]);
println!("Set TESSERA_UPLOAD_TOKEN on the agent.");
Ok(())
}