Milestone 3: the hub

lunehub serves accounts, invites, pairing codes, memberships, devices
and vaults as plain bare repos (SPEC §8), checks device key and role on
every Git request, refuses secrets in a pre-receive hook (P3) and logs
every access change. Ships as one container (Dockerfile, compose.yaml).

lunesync gains join, login, pair, vault new/upload, invite, grant,
members and hub log; `run` picks up new vaults and roles from the hub.
Read members' edits stay local.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
2026-10-29 00:47:54 +07:00
parent 62bc3db416
commit 2b4ca84a6b
23 changed files with 2578 additions and 58 deletions

View File

@@ -37,6 +37,7 @@ pub struct Vault {
pub branch: String,
pub label: String,
repo: Repository,
last_refusal: RefCell<Option<String>>,
}
/// A burst of edits saved as one history entry.
@@ -75,6 +76,8 @@ pub struct SyncReport {
pub set_aside: Vec<(String, String)>,
/// A big change is held here, so nothing from this device was shared.
pub holding: bool,
/// Local changes not shared because this device's person only has read access.
pub read_only: bool,
}
impl Vault {
@@ -90,6 +93,7 @@ impl Vault {
branch: cfg.branch.clone(),
label: label.to_string(),
repo,
last_refusal: RefCell::new(None),
})
}
@@ -273,6 +277,48 @@ impl Vault {
Ok(kept_both)
}
/// Points the vault at a new remote (e.g. moving onto the hub). Its next sync shares everything there.
pub fn relink(&self, url: &str) -> Result<()> {
self.repo.remote_set_url("origin", url)?;
if let Ok(mut r) = self.repo.find_reference(&self.remote_ref()) {
r.delete()?;
}
Ok(())
}
/// Starts history over with just the current notes (the old history stays only on this device).
/// Returns where history was, to go back if needed.
pub fn fresh_start(&self, message: &str) -> Result<Oid> {
let head = self.head_commit()?.context("the vault is empty")?;
let sig = self.signature()?;
let id = self.repo.commit(None, &sig, &sig, message, &head.tree()?, &[])?;
self.repo.reference(&self.branch_ref(), id, true, "lune: fresh start")?;
Ok(head.id())
}
pub fn reset_history(&self, id: Oid) -> Result<()> {
self.repo.reference(&self.branch_ref(), id, true, "lune: put back")?;
Ok(())
}
/// After moving onto a remote whose history is unrelated (e.g. a fresh start on the hub), combine the
/// notes as one new entry on top of the remote's history, so this device's old history stays here.
pub fn join_unrelated(&self) -> Result<bool> {
self.fetch()?;
let (Some(local), Some(remote)) = (self.head_commit()?, self.remote_commit()?) else { return Ok(false) };
if self.repo.merge_base(local.id(), remote.id()).is_ok() {
return Ok(false);
}
let mut index = self.repo.merge_commits(&local, &remote, None)?;
let mut kept = vec![];
self.keep_both(&mut index, &mut kept)?;
let tree = self.repo.find_tree(index.write_tree_to(&self.repo)?)?;
let sig = self.signature()?;
let id = self.repo.commit(None, &sig, &sig, &format!("Joined the shared copy from {}", self.label), &tree, &[&remote])?;
self.move_to(id)?;
Ok(true)
}
/// A named checkpoint (S3). If everything is already saved, records the name on its own.
pub fn checkpoint(&self, message: &str) -> Result<Oid> {
if let Some(saved) = self.save_changes(Some(message), &HashSet::new())? {
@@ -293,10 +339,16 @@ impl Vault {
Ok(())
}
/// Pushes local history. Returns false if the remote moved on meanwhile (fetch and try again).
/// This device's role in the vault, as last told by the hub. None for plain Git remotes.
pub fn role(&self) -> Option<String> {
std::fs::read_to_string(state::lune_dir(&self.git_dir).join("role")).ok().map(|s| s.trim().to_string())
}
/// Pushes local history. Returns false if it was refused (the remote moved on, or said no).
fn push(&self) -> Result<bool> {
let mut remote = self.repo.find_remote("origin")?;
let rejected = RefCell::new(None::<String>);
let said = RefCell::new(String::new());
let mut cb = remote::callbacks();
cb.push_update_reference(|_, status| {
if let Some(s) = status {
@@ -304,6 +356,10 @@ impl Vault {
}
Ok(())
});
cb.sideband_progress(|text| {
said.borrow_mut().push_str(&String::from_utf8_lossy(text));
true
});
let mut opts = PushOptions::new();
opts.remote_callbacks(cb);
let spec = format!("{0}:{0}", self.branch_ref());
@@ -314,7 +370,9 @@ impl Vault {
return Err(e.into());
}
drop(opts);
if rejected.borrow().is_some() {
if let Some(status) = rejected.borrow().clone() {
let said: Vec<String> = said.borrow().lines().filter(|l| l.contains("lune:")).map(|l| l.replace("lune:", "").trim().to_string()).collect();
*self.last_refusal.borrow_mut() = Some(if said.is_empty() { status } else { said.join("; ") });
return Ok(false);
}
if let Some(head) = self.head_commit()? {
@@ -373,10 +431,21 @@ impl Vault {
report.holding = true;
return Ok(report);
}
if self.role().is_some_and(|r| !crate::hub::can_edit(&r)) {
report.read_only = true;
return Ok(report);
}
if self.push()? {
report.shared += ahead;
return Ok(report);
}
// Refused without the remote having moved on: the remote said no, so say why.
let before = self.remote_commit()?.map(|c| c.id());
self.fetch()?;
if self.remote_commit()?.map(|c| c.id()) == before {
let why = self.last_refusal.borrow().clone().unwrap_or_else(|| "no reason given".into());
bail!("the hub refused these changes: {why}");
}
}
bail!("the remote kept changing while sharing; will try again shortly")
}
@@ -672,7 +741,7 @@ pub fn add(name: &str, path: &Path, remote_url: &str, label: &str) -> Result<(Va
state::write_marker(&git_dir, name, !existed)?;
drop(repo);
let cfg = VaultConfig { name: name.into(), path: path.clone(), remote: remote_url.into(), branch };
let cfg = VaultConfig { name: name.into(), path: path.clone(), remote: remote_url.into(), branch, hub_vault: None };
let vault = Vault::open(&cfg, label)?;
let _op = state::op_lock(&git_dir)?;
vault.save_changes(Some(&format!("Added the vault on {label}")), &HashSet::new())?;