use std::collections::{BTreeMap, BTreeSet};
use serde_json::Value;
use super::refs::Checkout;
use crate::CloudError;
use crate::events::build;
use crate::events::hlc::{self, Stamp};
use crate::replica::Replica;
use crate::replica::fold::Graph;
use crate::review::{curate, maintain};
pub const WITHDRAWN_REASON: &str = "withdrawn by author";
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct OwnNode {
pub node_id: String,
pub category: String,
pub key: String,
pub value_digest: String,
}
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct StoreFact {
pub category: String,
pub key: String,
pub value_digest: String,
pub current: bool,
pub confirmed_here: bool,
}
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct NodeView {
pub live: bool,
pub marked: bool,
pub has_anchors: bool,
}
#[derive(Debug, Clone, PartialEq, Eq, PartialOrd, Ord)]
pub enum Action {
Supersede { old: String, new: String },
Retract { node: String },
Confirm { node: String },
}
#[derive(Debug, Default)]
pub struct Present {
pub supersedes: BTreeSet<(String, String)>,
pub retracting: BTreeSet<String>,
pub confirming: BTreeSet<String>,
pub adding: BTreeSet<String>,
}
impl Present {
pub fn view(&self, graph: &Graph, node_id: &str) -> Option<NodeView> {
node_view(graph, node_id).or_else(|| {
self.adding.contains(node_id).then_some(NodeView {
live: true,
marked: false,
has_anchors: false,
})
})
}
}
pub fn removed_identities(root: &str) -> BTreeSet<(String, String)> {
let Ok(dir) = lean_ctx::core::knowledge::knowledge_dir_of(root) else {
return BTreeSet::new();
};
lean_ctx::core::knowledge::journal::rows_since(&dir, 0, usize::MAX)
.into_iter()
.filter(|r| r.op == "removed")
.map(|r| (r.category, r.key))
.collect()
}
pub fn plan(
own: &[OwnNode],
store: &[StoreFact],
removed: &BTreeSet<(String, String)>,
node: impl Fn(&str) -> Option<NodeView>,
present: &Present,
) -> Vec<Action> {
if store.is_empty() && removed.is_empty() {
return Vec::new();
}
let mut by_identity: BTreeMap<(&str, &str), Vec<&StoreFact>> = BTreeMap::new();
for fact in store {
by_identity
.entry((fact.category.as_str(), fact.key.as_str()))
.or_default()
.push(fact);
}
let mut node_of: BTreeMap<(&str, &str, &str), &str> = BTreeMap::new();
for n in own {
if node(&n.node_id).is_some_and(|v| v.live) {
node_of.insert(
(n.category.as_str(), n.key.as_str(), n.value_digest.as_str()),
n.node_id.as_str(),
);
}
}
let mut actions = Vec::new();
for n in own {
let Some(view) = node(&n.node_id).filter(|v| v.live) else {
continue;
};
let facts = by_identity
.get(&(n.category.as_str(), n.key.as_str()))
.map(Vec::as_slice)
.unwrap_or_default();
let same = facts.iter().find(|f| f.value_digest == n.value_digest);
if let Some(fact) = same.filter(|f| f.current) {
if view.marked
&& view.has_anchors
&& fact.confirmed_here
&& !present.confirming.contains(&n.node_id)
{
actions.push(Action::Confirm {
node: n.node_id.clone(),
});
}
continue;
}
let successor = facts
.iter()
.filter(|f| f.current && f.value_digest != n.value_digest)
.find_map(|f| {
node_of.get(&(f.category.as_str(), f.key.as_str(), f.value_digest.as_str()))
});
if let Some(&new) = successor {
if !present
.supersedes
.contains(&(new.to_owned(), n.node_id.clone()))
{
actions.push(Action::Supersede {
old: n.node_id.clone(),
new: new.to_owned(),
});
}
} else {
let journaled = removed.contains(&(n.category.clone(), n.key.clone()));
if (store.is_empty() && !journaled) || present.retracting.contains(&n.node_id) {
continue;
}
actions.push(Action::Retract {
node: n.node_id.clone(),
});
}
}
actions.sort();
actions.dedup();
actions
}
pub fn own_nodes(replica: &Replica, project_hash: &str) -> Result<Vec<OwnNode>, CloudError> {
Ok(replica
.own_sources()?
.into_iter()
.filter(|(_, kind, _)| kind == "fact")
.filter_map(|(node_id, _, source)| {
if source.get("project_hash")?.as_str()? != project_hash {
return None;
}
Some(OwnNode {
node_id,
category: source.get("category")?.as_str()?.to_owned(),
key: source.get("key")?.as_str()?.to_owned(),
value_digest: source.get("value_digest")?.as_str()?.to_owned(),
})
})
.collect())
}
pub fn node_view(graph: &Graph, node_id: &str) -> Option<NodeView> {
let node = graph.nodes.get(node_id)?;
Some(NodeView {
live: !node.retracted && !node.purged,
marked: matches!(
node.fields.get("anchor_state").and_then(Value::as_str),
Some("to_verify" | "gone")
),
has_anchors: node
.fields
.get("anchors")
.and_then(Value::as_array)
.is_some_and(|a| !a.is_empty()),
})
}
pub fn present(graph: &Graph, replica: &Replica) -> Result<Present, CloudError> {
let mut out = Present::default();
for edge in graph.edges.values() {
if edge.kind == "supersedes" && !edge.retracted {
out.supersedes.insert((edge.from.clone(), edge.to.clone()));
}
}
for (_, bytes) in replica.pending()? {
let Ok(event) = serde_json::from_slice::<Value>(&bytes) else {
continue;
};
let target = event["target"].as_str().unwrap_or_default().to_owned();
match event["op"].as_str() {
Some("retract") => {
out.retracting.insert(target);
}
Some("field.set") if event["body"]["field"] == "anchors" => {
out.confirming.insert(target);
}
Some("node.add") => {
out.adding.insert(target);
}
Some("edge.add") if event["body"]["kind"] == "supersedes" => {
if let (Some(from), Some(to)) =
(event["body"]["from"].as_str(), event["body"]["to"].as_str())
{
out.supersedes.insert((from.to_owned(), to.to_owned()));
}
}
_ => {}
}
}
Ok(out)
}
#[derive(Debug, Clone, Copy, Default, PartialEq, Eq)]
pub struct Queued {
pub superseded: usize,
pub retracted: usize,
pub confirmed: usize,
pub clock: Stamp,
}
pub fn queue(
target: &maintain::Target<'_>,
graph: &Graph,
checkout: &mut Checkout,
actions: &[Action],
clock: Stamp,
now_ms: u64,
) -> Result<Queued, CloudError> {
let mut out = Queued {
clock,
..Queued::default()
};
for action in actions {
match action {
Action::Supersede { old, new } => {
out.clock = hlc::send(out.clock, now_ms);
let event = build::edge_add(
"supersedes",
new,
old,
"observed",
target.place,
out.clock,
target.signer,
)?;
maintain::queue(target, &event, out.clock)?;
out.superseded += 1;
}
Action::Retract { node } => {
out.clock = hlc::send(out.clock, now_ms);
let event = curate::retract_event(
target.place,
node,
WITHDRAWN_REASON,
out.clock,
target.signer,
)?;
maintain::queue(target, &event, out.clock)?;
out.retracted += 1;
}
Action::Confirm { node } => {
let Some(anchors) = graph
.nodes
.get(node)
.and_then(|n| n.fields.get("anchors"))
.and_then(Value::as_array)
else {
continue;
};
let Some(fresh) = maintain::reanchored(anchors, checkout, now_ms) else {
continue;
};
out.clock = hlc::send(out.clock, now_ms);
let event = maintain::field_set(
target.place,
node,
"anchors",
Value::Array(fresh),
Some(now_ms),
out.clock,
target.signer,
)?;
maintain::queue(target, &event, out.clock)?;
out.clock = hlc::send(out.clock, now_ms);
let event = maintain::field_set(
target.place,
node,
"anchor_state",
Value::from("fresh"),
None,
out.clock,
target.signer,
)?;
maintain::queue(target, &event, out.clock)?;
out.confirmed += 1;
}
}
}
Ok(out)
}
pub fn store_facts(replica: &Replica, project_root: &str) -> Option<Vec<StoreFact>> {
let knowledge =
lean_ctx::core::knowledge::ProjectKnowledge::load_for_checked_capture(project_root).ok()?;
let root = std::path::Path::new(project_root);
Some(
knowledge
.facts
.iter()
.map(|fact| {
let current = fact.valid_until.is_none();
let anchors_here = !fact.anchors.is_empty()
&& lean_ctx::core::knowledge::anchors::review(root, &fact.anchors)
.iter()
.all(|(_, state)| {
*state == lean_ctx::core::knowledge::anchors::AnchorState::Current
});
StoreFact {
category: fact.category.clone(),
key: fact.key.clone(),
value_digest: replica.value_digest(&fact.value),
current,
confirmed_here: current && fact.review.is_none() && anchors_here,
}
})
.collect(),
)
}
#[cfg(test)]
#[path = "lifecycle_tests.rs"]
mod tests;