use serde_json::{Value, json};
use super::maintain::{Target, queue, reasons_hash};
use crate::core::hive::HiveError;
use crate::core::hive::capture::GATE_VERSION;
use crate::core::hive::events::build::{EVENT_SCHEMA, Place, Signer};
use crate::core::hive::events::hlc::{self, Stamp};
use crate::core::hive::events::{ids, sign};
use crate::core::hive::knowledge::screening::Screen;
use crate::core::hive::replica::fold::Graph;
const REASON_MIN: usize = 2;
const REASON_MAX: usize = 500;
fn local(e: impl std::fmt::Display) -> HiveError {
HiveError::Local(e.to_string())
}
fn event(
place: &Place,
op: &str,
target: &str,
body: &Value,
task: &str,
stamp: Stamp,
signer: &Signer<'_>,
) -> Result<Value, HiveError> {
let body_commit = sign::body_commit(&place.pk_id, body).map_err(local)?;
let mut event = json!({
"schema": EVENT_SCHEMA,
"event_id": ids::new_event_id(stamp.wall_ms)?,
"place": {"kind": place.kind, "id": place.id},
"op": op,
"target": target,
"body": body,
"body_commit": body_commit,
"hlc": {"wall_ms": stamp.wall_ms, "counter": stamp.counter, "node": signer.hlc_node},
"causal_base": place.causal_base,
"actor": {"kind": "worker"},
"worker_id": signer.worker_id,
"provenance_claim": "workspace",
"capture": {
"gate_version": GATE_VERSION,
"evidence": "observed",
"reasons_passed_hash": reasons_hash(task),
},
"screening": {
"counts": {"forbidden": 0, "local_path": 0, "pii": 0, "redaction": 0, "secret": 0},
"scanner_version": crate::core::hive::knowledge::screening::SCANNER_VERSION,
},
});
sign::sign(&mut event, signer.key).map_err(local)?;
Ok(event)
}
pub fn contradicts_edge(
place: &Place,
from: &str,
to: &str,
stamp: Stamp,
signer: &Signer<'_>,
) -> Result<Value, HiveError> {
let body = json!({
"body_nonce": sign::new_body_nonce()?,
"from": from,
"kind": "contradicts",
"to": to,
});
let edge_id = ids::new_random_id("e")?;
event(
place,
"edge.add",
&edge_id,
&body,
"contradicts",
stamp,
signer,
)
}
pub fn retract_event(
place: &Place,
target: &str,
reason: &str,
stamp: Stamp,
signer: &Signer<'_>,
) -> Result<Value, HiveError> {
let body = json!({"body_nonce": sign::new_body_nonce()?, "reason": reason});
event(place, "retract", target, &body, "retract", stamp, signer)
}
fn fold(target: &Target<'_>) -> Result<Graph, HiveError> {
let mut graph = Graph::default();
graph
.fold(&target.replica.fold_events()?)
.map_err(|e| HiveError::Protocol(e.to_string()))?;
Ok(graph)
}
fn live(graph: &Graph, node_id: &str) -> Result<(), HiveError> {
match graph.nodes.get(node_id) {
Some(n) if !n.retracted && !n.purged => Ok(()),
Some(_) => Err(HiveError::Config(format!(
"{node_id} is already retracted or purged in this vault"
))),
None => Err(HiveError::Config(format!(
"{node_id}: no such node in this vault's replica (run `lean-ctx hive sync` first)"
))),
}
}
fn pending(target: &Target<'_>) -> Result<Vec<Value>, HiveError> {
Ok(target
.replica
.pending()?
.iter()
.filter_map(|(_, bytes)| serde_json::from_slice::<Value>(bytes).ok())
.collect())
}
fn same_pair(body: &Value, a: &str, b: &str) -> bool {
let (from, to) = (body["from"].as_str(), body["to"].as_str());
(from == Some(a) && to == Some(b)) || (from == Some(b) && to == Some(a))
}
pub fn queue_contradiction(
target: &Target<'_>,
a: &str,
b: &str,
now_ms: u64,
) -> Result<bool, HiveError> {
if a == b {
return Err(HiveError::Config(
"a node does not contradict itself".into(),
));
}
let graph = fold(target)?;
live(&graph, a)?;
live(&graph, b)?;
let folded = graph.edges.values().any(|e| {
e.kind == "contradicts"
&& !e.retracted
&& ((e.from == a && e.to == b) || (e.from == b && e.to == a))
});
let queued = pending(target)?.iter().any(|e| {
e["op"] == "edge.add" && e["body"]["kind"] == "contradicts" && same_pair(&e["body"], a, b)
});
if folded || queued {
return Ok(false);
}
let stamp = hlc::send(Stamp::default(), now_ms);
let event = contradicts_edge(target.place, a, b, stamp, target.signer)?;
queue(target, event, stamp)?;
Ok(true)
}
pub fn queue_retraction(
target: &Target<'_>,
node: &str,
reason: &str,
screen: &mut Screen,
now_ms: u64,
) -> Result<bool, HiveError> {
let reason = reason.trim();
let chars = reason.chars().count();
if !(REASON_MIN..=REASON_MAX).contains(&chars) {
return Err(HiveError::Config(format!(
"--reason needs {REASON_MIN} to {REASON_MAX} characters"
)));
}
screen.check(reason).map_err(|why| {
HiveError::Refused(format!("screened: the reason was not queued ({why})"))
})?;
let graph = fold(target)?;
live(&graph, node)?;
if pending(target)?
.iter()
.any(|e| e["op"] == "retract" && e["target"] == node)
{
return Ok(false);
}
let stamp = hlc::send(Stamp::default(), now_ms);
let event = retract_event(target.place, node, reason, stamp, target.signer)?;
queue(target, event, stamp)?;
Ok(true)
}
#[cfg(test)]
#[path = "curate_tests.rs"]
mod tests;