diff --git a/desktop/crates/radicle-tauri/src/commands/startup.rs b/desktop/crates/radicle-tauri/src/commands/startup.rs index 2e968387f..99077584f 100644 --- a/desktop/crates/radicle-tauri/src/commands/startup.rs +++ b/desktop/crates/radicle-tauri/src/commands/startup.rs @@ -77,6 +77,7 @@ pub(crate) fn startup(app: AppHandle) -> Result { crate::ref_updates::spawn( app.app_handle().clone(), Node::new(profile.home().socket_from_env()), + profile.storage.clone(), ); app.manage(state.clone()); diff --git a/desktop/crates/radicle-tauri/src/ref_updates.rs b/desktop/crates/radicle-tauri/src/ref_updates.rs index 36ed97d63..9f05bd84f 100644 --- a/desktop/crates/radicle-tauri/src/ref_updates.rs +++ b/desktop/crates/radicle-tauri/src/ref_updates.rs @@ -1,14 +1,21 @@ +use std::collections::{BTreeMap, HashMap}; +use std::path::Path; +use std::str::FromStr; use std::sync::Once; use std::sync::atomic::{AtomicBool, Ordering}; use std::time::Duration; +use radicle::Storage; +use radicle::git::Oid; use radicle::identity::RepoId; -use radicle::node::{self, Event, Handle, Node}; -use radicle::storage; +use radicle::node::{self, Event, Handle, Node, NodeId}; +use radicle::storage::refs::sigrefs::git::object::Reader as _; +use radicle::storage::refs::{REFS_BLOB_PATH, RefsAt}; +use radicle::storage::{self, ReadStorage}; use tauri::{AppHandle, Emitter}; -/// Refs that changed in a repository. `refs` is `None` when the node doesn't -/// say which refs changed, such as when the local user pushed. +/// Refs that changed in a repository. `refs` is `None` when it isn't known +/// which refs changed, such as on the first announcement after a restart. #[derive(Clone, serde::Serialize)] struct RefUpdate { rid: RepoId, @@ -26,62 +33,329 @@ pub(crate) fn is_live() -> bool { /// Starts forwarding the node's ref updates, once per app: `startup` runs again /// whenever the webview reloads. -pub(crate) fn spawn(app: AppHandle, node: Node) { +pub(crate) fn spawn(app: AppHandle, node: Node, storage: Storage) { static FORWARDING: Once = Once::new(); FORWARDING.call_once(|| { - std::thread::spawn(move || forward(app, node)); + std::thread::spawn(move || { + forward( + app, + node, + Resolver { + storage, + seen: HashMap::new(), + }, + ) + }); }); } /// Forwards the node's ref updates to the frontend as `ref_update` events. /// Every time the subscription (re)starts, the frontend gets `ref_resync`, /// because it may have missed events in between. -fn forward(app: AppHandle, node: Node) { +fn forward(app: AppHandle, node: Node, mut resolver: Resolver) { + let mut subscribed = true; loop { - if let Ok(events) = node.subscribe(Duration::from_secs(60 * 60 * 24)) { - LIVE.store(true, Ordering::Relaxed); - let _ = app.emit("ref_resync", ()); - for event in events { - match event { - Ok(event) => { - if let Some(update) = ref_update(event) { - let _ = app.emit("ref_update", update); + match node.subscribe(Duration::from_secs(60 * 60 * 24)) { + Ok(events) => { + subscribed = true; + let _live = Live::start(); + let _ = app.emit("ref_resync", ()); + for event in events { + match event { + Ok(event) => { + if let Some(update) = resolver.ref_update(event) { + let _ = app.emit("ref_update", update); + } + } + // An event this version can't read, such as a new kind + // from a newer node, doesn't end the subscription. + Err( + e @ (node::Error::InvalidJson { .. } | node::Error::Command { .. }), + ) => { + log::debug!("Skipping a node event: {e}"); + } + Err(e) => { + log::warn!("Node event subscription ended: {e}"); + break; } } - // An event this version can't read, such as a new kind - // from a newer node, doesn't end the subscription. - Err(node::Error::InvalidJson { .. } | node::Error::Command { .. }) => {} - Err(_) => break, } } + // Mostly the node not running; logged once until it works again. + Err(e) => { + if subscribed { + log::info!("Not subscribed to node events: {e}"); + } + subscribed = false; + } } - LIVE.store(false, Ordering::Relaxed); std::thread::sleep(Duration::from_secs(2)); } } -fn ref_update(event: Event) -> Option { - match event { - Event::RefsFetched { rid, updated, .. } => { - let refs: Vec = updated - .into_iter() - .filter_map(|update| match update { - storage::RefUpdate::Updated { name, .. } - | storage::RefUpdate::Created { name, .. } - | storage::RefUpdate::Deleted { name, .. } => Some(name.to_string()), - storage::RefUpdate::Skipped { .. } => None, +/// Marks the node's events as live while it exists, so that they stop +/// counting as live however the subscription ends, even by a panic. +struct Live; + +impl Live { + fn start() -> Self { + LIVE.store(true, Ordering::Relaxed); + Live + } +} + +impl Drop for Live { + fn drop(&mut self) { + LIVE.store(false, Ordering::Relaxed); + } +} + +/// Names the refs behind the node's events. +struct Resolver { + storage: Storage, + /// The last signed refs announced for each local namespace. + seen: HashMap<(RepoId, NodeId), Oid>, +} + +impl Resolver { + fn ref_update(&mut self, event: Event) -> Option { + match event { + Event::RefsFetched { rid, updated, .. } => { + let refs: Vec = updated + .into_iter() + .filter_map(|update| match update { + storage::RefUpdate::Updated { name, .. } + | storage::RefUpdate::Created { name, .. } + | storage::RefUpdate::Deleted { name, .. } => Some(name.to_string()), + storage::RefUpdate::Skipped { .. } => None, + }) + .collect(); + (!refs.is_empty()).then_some(RefUpdate { + rid, + refs: Some(refs), }) - .collect(); - (!refs.is_empty()).then_some(RefUpdate { + } + Event::CanonicalRefUpdated { rid, refname, .. } => Some(RefUpdate { rid, - refs: Some(refs), - }) + refs: Some(vec![refname.to_string()]), + }), + Event::LocalRefsAnnounced { rid, refs, .. } => { + let refs = self.announced(rid, refs); + match refs { + Some(refs) if refs.is_empty() => None, + refs => Some(RefUpdate { rid, refs }), + } + } + _ => None, } - Event::CanonicalRefUpdated { rid, refname, .. } => Some(RefUpdate { - rid, - refs: Some(vec![refname.to_string()]), - }), - Event::LocalRefsAnnounced { rid, .. } => Some(RefUpdate { rid, refs: None }), - _ => None, + } + + /// The refs that changed since the previous announcement of the same + /// namespace, found by comparing their signed refs. This also names + /// changes made outside the app, such as with the `rad` CLI, and several + /// writes announced at once. `None` when there is nothing to compare + /// with, such as the first announcement after a restart. + fn announced(&mut self, rid: RepoId, refs: RefsAt) -> Option> { + let storage = &self.storage; + // Verifying the refs would walk the whole history of older signed + // refs, so they are read unverified. They are the node's own, and only + // decide what the frontend refetches. + let load = |at: Oid| -> Option> { + let repo = storage.repository(rid).ok()?; + let blob = repo.read_blob(&at, Path::new(REFS_BLOB_PATH)).ok()??; + parse_refs(&blob.bytes) + }; + announced_refs(&mut self.seen, rid, refs, load) + } +} + +/// Compares an announcement with the previous one of the same namespace, +/// loading each one's refs with `load`. The announcement is remembered even if +/// its refs can't be loaded, so the next one compares against it. +fn announced_refs( + seen: &mut HashMap<(RepoId, NodeId), Oid>, + rid: RepoId, + refs: RefsAt, + load: impl Fn(Oid) -> Option>, +) -> Option> { + let previous = seen.insert((rid, refs.remote), refs.at)?; + if previous == refs.at { + return Some(Vec::new()); + } + Some(changed_refs(&load(previous)?, &load(refs.at)?)) +} + +/// Parses a signed refs blob: one ` ` line per ref. The link to the +/// previous signed refs isn't a ref of the repository, so it is left out. +fn parse_refs(bytes: &[u8]) -> Option> { + std::str::from_utf8(bytes) + .ok()? + .lines() + .map(|line| { + let (oid, name) = line.split_once(' ')?; + Some((name.to_string(), Oid::from_str(oid).ok()?)) + }) + .filter(|entry| !matches!(entry, Some((name, _)) if name == "refs/rad/sigrefs-parent")) + .collect() +} + +/// The refs that were added, changed or removed between two sets of refs. +fn changed_refs(before: &BTreeMap, after: &BTreeMap) -> Vec { + after + .iter() + .filter(|(name, oid)| before.get(*name) != Some(*oid)) + .map(|(name, _)| name.clone()) + .chain( + before + .keys() + .filter(|name| !after.contains_key(*name)) + .cloned(), + ) + .collect() +} + +#[cfg(test)] +mod tests { + use std::collections::{BTreeMap, HashMap}; + use std::str::FromStr; + + use radicle::git::Oid; + use radicle::identity::RepoId; + use radicle::node::NodeId; + use radicle::storage::refs::RefsAt; + + use super::{announced_refs, changed_refs, parse_refs}; + + fn oid(c: char) -> Oid { + Oid::from_str(&c.to_string().repeat(40)).unwrap() + } + + fn rid() -> RepoId { + RepoId::from_str("rad:z3gqcJUoA1n9HaHKufZs5FCSGazv5").unwrap() + } + + fn at(c: char) -> RefsAt { + RefsAt { + remote: NodeId::from_str("z6MkkfM3tPXNPrPevKr3uSiQtHPuwnNhu2yUVjgd2jXVsVz5").unwrap(), + at: oid(c), + } + } + + // Signed refs `a` hold one issue, `b` a changed issue and a new branch. + fn load(at: Oid) -> Option> { + let issue = "refs/cobs/xyz.radicle.issue/1".to_string(); + if at == oid('a') { + Some(BTreeMap::from([(issue, oid('1'))])) + } else if at == oid('b') { + Some(BTreeMap::from([ + (issue, oid('2')), + ("refs/heads/topic".to_string(), oid('1')), + ])) + } else { + None + } + } + + #[test] + fn the_first_announcement_is_broad() { + let mut seen = HashMap::new(); + assert_eq!(announced_refs(&mut seen, rid(), at('a'), load), None); + assert_eq!(seen.values().collect::>(), vec![&oid('a')]); + } + + #[test] + fn a_repeated_announcement_names_nothing() { + let mut seen = HashMap::new(); + announced_refs(&mut seen, rid(), at('a'), load); + assert_eq!( + announced_refs(&mut seen, rid(), at('a'), load), + Some(Vec::new()) + ); + } + + #[test] + fn an_announcement_names_the_refs_since_the_previous_one() { + let mut seen = HashMap::new(); + announced_refs(&mut seen, rid(), at('a'), load); + assert_eq!( + announced_refs(&mut seen, rid(), at('b'), load), + Some(vec![ + "refs/cobs/xyz.radicle.issue/1".to_string(), + "refs/heads/topic".to_string(), + ]) + ); + } + + #[test] + fn unreadable_refs_are_broad_but_remembered() { + let mut seen = HashMap::new(); + announced_refs(&mut seen, rid(), at('a'), load); + assert_eq!(announced_refs(&mut seen, rid(), at('c'), load), None); + // The next announcement compares against the unreadable one, which + // stays broad rather than naming changes it can't see. + assert_eq!(announced_refs(&mut seen, rid(), at('b'), load), None); + // From `b` back to `a` the issue changes again and the branch goes. + assert_eq!( + announced_refs(&mut seen, rid(), at('a'), load), + Some(vec![ + "refs/cobs/xyz.radicle.issue/1".to_string(), + "refs/heads/topic".to_string(), + ]) + ); + } + + fn refs(entries: &[(&str, &str)]) -> BTreeMap { + entries + .iter() + .map(|(name, oid)| (name.to_string(), Oid::from_str(oid).unwrap())) + .collect() + } + + #[test] + fn names_added_changed_and_removed_refs() { + let a = "a".repeat(40); + let b = "b".repeat(40); + let before = refs(&[ + ("refs/cobs/xyz.radicle.issue/1", &a), + ("refs/cobs/xyz.radicle.patch/2", &a), + ("refs/heads/main", &a), + ]); + let after = refs(&[ + ("refs/cobs/xyz.radicle.issue/1", &b), + ("refs/heads/main", &a), + ("refs/heads/topic", &a), + ]); + + assert_eq!( + changed_refs(&before, &after), + vec![ + "refs/cobs/xyz.radicle.issue/1", + "refs/heads/topic", + "refs/cobs/xyz.radicle.patch/2", + ] + ); + } + + #[test] + fn parses_signed_refs_without_the_parent_link() { + let a = "a".repeat(40); + let blob = format!( + "{a} refs/cobs/xyz.radicle.issue/1\n{a} refs/heads/main\n{a} refs/rad/sigrefs-parent\n" + ); + assert_eq!( + parse_refs(blob.as_bytes()), + Some(refs(&[ + ("refs/cobs/xyz.radicle.issue/1", &a), + ("refs/heads/main", &a), + ])) + ); + assert_eq!(parse_refs(b"not a ref line\n"), None); + } + + #[test] + fn names_nothing_when_unchanged() { + let a = "a".repeat(40); + let both = refs(&[("refs/heads/main", &a)]); + assert!(changed_refs(&both, &both).is_empty()); } } diff --git a/shared/core/src/keys.ts b/shared/core/src/keys.ts index 8a7f28d76..d6587a087 100644 --- a/shared/core/src/keys.ts +++ b/shared/core/src/keys.ts @@ -35,6 +35,8 @@ export const keys = { patchList: (rid: string, status: string) => ["repo", rid, "patches", status] as const, patch: (rid: string, id: string) => ["repo", rid, "patch", id] as const, + releases: (rid: string) => ["repo", rid, "releases"] as const, + boards: (rid: string) => ["repo", rid, "boards"] as const, jobs: (rid: string) => ["repo", rid, "jobs"] as const, repos: () => ["repos"] as const, notificationCount: () => ["notifications", "count"] as const, diff --git a/shared/core/src/refUpdates.ts b/shared/core/src/refUpdates.ts index a56574946..6d1110fa2 100644 --- a/shared/core/src/refUpdates.ts +++ b/shared/core/src/refUpdates.ts @@ -2,8 +2,8 @@ import type { Key } from "./keys"; import { keys } from "./keys"; -// Refs that changed in a repository. Without `refs`, the backend doesn't know -// which refs changed, such as when the local user pushed. +// Refs that changed in a repository. Without `refs`, it isn't known which refs +// changed, such as on the first announcement after a restart. export interface RefUpdate { rid: string; refs?: string[]; @@ -12,6 +12,9 @@ export interface RefUpdate { const ISSUE = "xyz.radicle.issue"; const PATCH = "xyz.radicle.patch"; const JOB = "xyz.radworks.job"; +// Releases are artifact COBs. +const RELEASE = "dev.radicle.artifact"; +const BOARD = "works.monadic.board"; export function affectedKeys({ rid, refs }: RefUpdate): Key[] { if (refs === undefined) { @@ -37,6 +40,10 @@ export function affectedKeys({ rid, refs }: RefUpdate): Key[] { // A job names its commit only in its contents, so every job list of // the repository is affected. add(keys.jobs(rid)); + } else if (cob && cob[1] === RELEASE) { + add(keys.releases(rid)); + } else if (cob && cob[1] === BOARD) { + add(keys.boards(rid)); } else if (/^refs\/(heads|tags)\//.test(name)) { add(keys.code(rid)); add(keys.repoInfo(rid)); diff --git a/shared/core/tests/refUpdates.test.ts b/shared/core/tests/refUpdates.test.ts index 77302dc89..9609beb5c 100644 --- a/shared/core/tests/refUpdates.test.ts +++ b/shared/core/tests/refUpdates.test.ts @@ -45,6 +45,18 @@ describe("affectedKeys", () => { ).toEqual([keys.jobs(rid)]); }); + test("releases and boards affect only their own keys", () => { + expect( + affectedKeys({ + rid, + refs: [ + `refs/namespaces/${nid}/refs/cobs/dev.radicle.artifact/${issue}`, + `refs/namespaces/${nid}/refs/cobs/works.monadic.board/${issue}`, + ], + }), + ).toEqual([keys.releases(rid), keys.boards(rid)]); + }); + test("an identity update affects the whole repository", () => { expect( affectedKeys({