use std::collections::HashMap;
use std::sync::Arc;
use memstead_schema::{Schema, TypeDefinition, type_by_name};
use super::{
DanglingLink, FoldedTag, HealthIssue, HealthReport, HealthSummary, StaleEntity,
TagDistribution, TagVariant, UntaggedStats,
};
use crate::entity::MetadataValue;
use crate::graph::query;
use crate::store::Store;
pub const HEALTH_INCLUDE_KEYS: &[&str] = &[
"orphans",
"stubs",
"most_connected",
"missing_fields",
"stale",
"dangling_links",
"tags",
"missing_required_outgoing",
"constraints",
"signals",
"labelling",
"conformance",
"integrity",
"config",
"anchors",
"friction",
"open_questions",
"stale_derivations",
"checks",
"ledger",
"vital_signs",
"due",
];
pub const VITAL_SIGNS_ITEM_CAP: usize = 20;
pub fn health_vital_signs_axis(
engine: &crate::engine::Engine,
mem_filter: Option<&str>,
) -> serde_json::Value {
let cap = VITAL_SIGNS_ITEM_CAP;
let capped = |mut items: Vec<serde_json::Value>| -> serde_json::Value {
let count = items.len();
let more = count.saturating_sub(cap);
items.truncate(cap);
let mut o = serde_json::Map::new();
o.insert("count".into(), serde_json::json!(count));
o.insert("items".into(), serde_json::Value::Array(items));
if more > 0 {
o.insert("more".into(), serde_json::json!(more));
}
serde_json::Value::Object(o)
};
let mut mems: Vec<String> = engine.mem_names().iter().map(|s| s.to_string()).collect();
mems.sort();
let communities = engine.communities();
let mut out = serde_json::Map::new();
for mem in &mems {
if let Some(f) = mem_filter
&& f != mem
{
continue;
}
let entities: Vec<&crate::entity::Entity> = engine
.store()
.all_entities()
.filter(|e| !e.stub && e.id.mem() == mem)
.collect();
let schema = engine.schema_for(mem);
let last_resort: Option<String> = schema.as_ref().and_then(|s| {
s.types
.values()
.find(|t| t.last_resort)
.map(|t| t.name.clone())
});
let type_share = match &last_resort {
None => serde_json::json!({ "status": "not_declared" }),
Some(lr) => {
let mut per: std::collections::BTreeMap<String, (usize, usize)> =
std::collections::BTreeMap::new();
for e in &entities {
let cluster = communities
.entity_cluster_map
.get(&e.id.0)
.cloned()
.unwrap_or_else(|| "unplaced".to_string());
let slot = per.entry(cluster).or_insert((0, 0));
slot.0 += 1;
if e.entity_type == *lr {
slot.1 += 1;
}
}
let mut rows: Vec<serde_json::Value> = per
.into_iter()
.map(|(community, (total, on_last_resort))| {
serde_json::json!({
"community": community,
"entities": total,
"on_last_resort_type": on_last_resort,
})
})
.collect();
rows.sort_by(|a, b| {
let share = |v: &serde_json::Value| {
let t = v["entities"].as_u64().unwrap_or(1).max(1) as f64;
v["on_last_resort_type"].as_u64().unwrap_or(0) as f64 / t
};
share(b)
.partial_cmp(&share(a))
.unwrap_or(std::cmp::Ordering::Equal)
.then_with(|| a["community"].as_str().cmp(&b["community"].as_str()))
});
let mut v = capped(rows);
v["status"] = serde_json::json!("declared");
v["last_resort_type"] = serde_json::json!(lr);
v
}
};
let mut claims: std::collections::BTreeMap<
String,
(std::collections::BTreeSet<String>, bool),
> = std::collections::BTreeMap::new();
for e in &entities {
for a in engine.entity_anchors(&e.id) {
let slot = claims.entry(a.artifact.clone()).or_default();
slot.0.insert(e.id.0.clone());
if a.class == crate::anchor::AnchorProvenanceClass::Anchored {
slot.1 = true;
}
}
}
let roots = engine.anchor_source_roots(mem);
let mut unclaimed: Vec<serde_json::Value> = Vec::new();
let mut sources_enumerated = 0usize;
if let Some(ws) = engine.workspace_root() {
let mut seen: std::collections::BTreeSet<String> = std::collections::BTreeSet::new();
for join in roots.values() {
sources_enumerated += 1;
for file in crate::source_scope::enumerate_source_artifacts(
engine,
&join.source,
&join.deny_paths,
ws,
) {
if !seen.insert(file.clone()) || claims.contains_key(&file) {
continue;
}
let size = std::fs::metadata(ws.join(&file))
.map(|m| m.len())
.unwrap_or(0);
unclaimed.push(serde_json::json!({ "artifact": file, "bytes": size }));
}
}
}
unclaimed.sort_by(|a, b| {
b["bytes"]
.as_u64()
.cmp(&a["bytes"].as_u64())
.then_with(|| a["artifact"].as_str().cmp(&b["artifact"].as_str()))
});
let unclaimed_v = if sources_enumerated == 0 {
serde_json::json!({ "status": "no_bound_source" })
} else {
let mut v = capped(unclaimed);
v["status"] = serde_json::json!("enumerated");
v
};
let contested: Vec<serde_json::Value> = claims
.iter()
.filter(|(_, (who, owned))| who.len() >= 2 && !owned)
.map(|(artifact, (who, _))| {
serde_json::json!({
"artifact": artifact,
"claimed_by": who.iter().cloned().collect::<Vec<_>>(),
})
})
.collect();
let cluster_size = |c: &str| -> usize {
communities
.clusters
.get(c)
.map(|ci| ci.entities.len())
.unwrap_or(0)
};
let mut by_community: std::collections::BTreeMap<String, Vec<String>> =
std::collections::BTreeMap::new();
for e in &entities {
if !engine.store().outgoing(&e.id).is_empty() {
continue;
}
let own = communities.entity_cluster_map.get(&e.id.0).cloned();
let community = match own {
Some(c) if cluster_size(&c) > 1 => c,
_ => engine
.store()
.incoming(&e.id)
.iter()
.find_map(|edge| communities.entity_cluster_map.get(&edge.from.0).cloned())
.unwrap_or_else(|| "unplaced".to_string()),
};
by_community
.entry(community)
.or_default()
.push(e.id.0.clone());
}
let zero_total: usize = by_community.values().map(Vec::len).sum();
let zero_rows: Vec<serde_json::Value> = by_community
.into_iter()
.map(|(community, mut ids)| {
ids.sort();
let count = ids.len();
let more = count.saturating_sub(cap);
ids.truncate(cap);
let mut o = serde_json::json!({
"community": community,
"count": count,
"items": ids,
});
if more > 0 {
o["more"] = serde_json::json!(more);
}
o
})
.collect();
let mut zero_v = capped(zero_rows);
zero_v["entities"] = serde_json::json!(zero_total);
let mut empty_sections: Vec<serde_json::Value> = Vec::new();
if let Some(s) = &schema {
for e in &entities {
let Some(td) = s.types.get(&e.entity_type) else {
continue;
};
for sec in &td.sections {
if e.sections
.get(&sec.key)
.is_some_and(|body| body.trim().is_empty())
{
empty_sections.push(serde_json::json!({
"id": e.id.0,
"section": sec.key,
}));
}
}
}
}
out.insert(
mem.clone(),
serde_json::json!({
"type_share_by_community": type_share,
"unclaimed_source_files": unclaimed_v,
"contested_unowned_files": capped(contested),
"zero_outgoing_entities": zero_v,
"empty_declared_sections": capped(empty_sections),
}),
);
}
let mut top = serde_json::Map::new();
top.insert("_item_cap".into(), serde_json::json!(cap));
for (k, v) in out {
top.insert(k, v);
}
serde_json::Value::Object(top)
}
#[derive(Default)]
struct CheckMaps {
latest: std::collections::BTreeMap<String, crate::check::CheckRecord>,
latest_conformance: std::collections::BTreeMap<String, crate::check::CheckRecord>,
foreign_by_entity: std::collections::BTreeMap<String, Vec<String>>,
newest_any: std::collections::BTreeMap<String, crate::check::CheckRecord>,
all_verification: std::collections::BTreeMap<String, Vec<crate::check::CheckRecord>>,
}
impl CheckMaps {
fn from_records<'a>(records: impl Iterator<Item = &'a crate::check::CheckRecord>) -> Self {
let mut maps = Self::default();
for rec in records {
maps.newest_any.insert(rec.entity.clone(), rec.clone());
match rec.resolved_kind() {
Some(crate::check::CheckKind::Verification) => {
maps.all_verification
.entry(rec.entity.clone())
.or_default()
.push(rec.clone());
maps.latest.insert(rec.entity.clone(), rec.clone());
}
Some(crate::check::CheckKind::Conformance) => {
maps.latest_conformance
.insert(rec.entity.clone(), rec.clone());
}
None => {
if let Some(k) = rec.foreign_kind() {
maps.foreign_by_entity
.entry(rec.entity.clone())
.or_default()
.push(k.to_string());
}
}
}
}
maps
}
}
pub fn health_checks_axis(
engine: &crate::engine::Engine,
mem_filter: Option<&str>,
) -> serde_json::Value {
let cap = OPEN_QUESTIONS_ITEM_CAP;
let capped = |mut items: Vec<String>| -> serde_json::Value {
items.sort();
let count = items.len();
let more = count.saturating_sub(cap);
items.truncate(cap);
let mut o = serde_json::Map::new();
o.insert("count".into(), serde_json::json!(count));
o.insert("items".into(), serde_json::json!(items));
if more > 0 {
o.insert("more".into(), serde_json::json!(more));
}
serde_json::Value::Object(o)
};
let ledger_records: Vec<crate::check::CheckRecord> = engine
.workspace_root()
.map(|root| crate::check::CheckLedger::for_workspace(root).all())
.unwrap_or_default();
let from_ledger = CheckMaps::from_records(ledger_records.iter());
let mut mems: Vec<String> = engine.mem_names().iter().map(|s| s.to_string()).collect();
mems.sort();
let mut out = serde_json::Map::new();
for mem in mems {
if let Some(f) = mem_filter
&& f != mem
{
continue;
}
let sealed_maps;
let (maps, sealed_note) = if engine.is_archive_mount(&mem) {
match engine.archive_checks_for(&mem) {
Some(sealed) => {
let records: Vec<crate::check::CheckRecord> = sealed
.entities
.iter()
.flat_map(|(path, kinds)| {
let id = crate::EntityId::new(&mem, path).0;
kinds.iter().map(move |(kind, sc)| sc.to_record(&id, kind))
})
.collect();
sealed_maps = CheckMaps::from_records(records.iter());
(
&sealed_maps,
Some(serde_json::json!({
"carried": true,
"records": records.len(),
})),
)
}
None => {
sealed_maps = CheckMaps::default();
(
&sealed_maps,
Some(serde_json::json!({
"carried": false,
"reason": "the archive carries no check records; every state reads never_checked",
})),
)
}
}
} else {
(&from_ledger, None)
};
let CheckMaps {
latest,
latest_conformance,
foreign_by_entity,
newest_any,
all_verification,
} = maps;
let mut counts = std::collections::BTreeMap::from([
("never_checked", 0usize),
("checked_ok", 0usize),
("check_failed", 0usize),
("check_stale", 0usize),
]);
let current_pin = engine
.mount(&mem)
.and_then(|m| m.schema.as_ref())
.map(|s| s.as_display());
let mut conformance_counts = std::collections::BTreeMap::from([
("never_checked", 0usize),
("checked_ok", 0usize),
("check_failed", 0usize),
("check_stale", 0usize),
]);
let mut self_checked: Vec<String> = Vec::new();
let mut confirmed_independent: Vec<String> = Vec::new();
let mut unconfirmable: Vec<String> = Vec::new();
let mut executors = serde_json::Map::new();
let mut readings = serde_json::Map::new();
let touches = engine.mem_touches(&mem);
let mut foreign_kinds: std::collections::BTreeMap<String, usize> =
std::collections::BTreeMap::new();
let mut findings = serde_json::Map::new();
for e in engine.store().all_entities().filter(|e| e.mem == mem) {
let id = e.id.0.clone();
if let Some(kinds) = foreign_by_entity.get(&id) {
for k in kinds {
*foreign_kinds.entry(k.clone()).or_insert(0) += 1;
}
}
if let Some(rec) = newest_any.get(&id)
&& let Some(f) = &rec.finding
{
findings.insert(
id.clone(),
serde_json::json!({
"verdict": rec.verdict,
"kind": rec.kind.as_deref().unwrap_or("verification"),
"ts": rec.ts,
"identity": rec.identity,
"origin": engine.check_row_prose_origin(&mem, &id, rec).as_wire(),
"finding": f,
}),
);
}
let state = crate::check::derive_state(latest.get(&id), &e.content_hash);
*counts.entry(state.as_str()).or_insert(0) += 1;
if let Some(records) = all_verification.get(&id) {
let rows: Vec<serde_json::Value> = records
.iter()
.map(|rec| {
let reading = if rec.verdict == "ok" {
engine.independence_of(e, rec, &touches).0.as_str()
} else {
"failed"
};
let role_confirms = matches!(
crate::vcs::Role::from_wire(&rec.role).unwrap_or_default(),
crate::vcs::Role::Checker | crate::vcs::Role::Verifier
);
serde_json::json!({
"ts": rec.ts,
"identity": rec.identity,
"verdict": rec.verdict,
"reading": reading,
"role": rec.role,
"role_confirms": role_confirms,
})
})
.collect();
readings.insert(id.clone(), serde_json::Value::Array(rows));
}
let cstate = crate::check::derive_state_pinned(
latest_conformance.get(&id),
&e.content_hash,
current_pin.as_deref(),
);
*conformance_counts.entry(cstate.as_str()).or_insert(0) += 1;
if state != crate::check::CheckState::CheckedOk {
continue;
}
let check = latest.get(&id).expect("checked_ok implies a record");
let (reading, execs) = engine.independence_of(e, check, &touches);
if let Some(execs) = execs {
executors.insert(id.clone(), serde_json::json!(execs.identities));
}
match reading {
crate::engine::independence::Independence::SelfChecked => self_checked.push(id),
crate::engine::independence::Independence::ConfirmedIndependent => {
confirmed_independent.push(id)
}
crate::engine::independence::Independence::Unconfirmable => unconfirmable.push(id),
}
}
let mut m = serde_json::Map::new();
for (k, v) in counts {
m.insert(k.to_string(), serde_json::json!(v));
}
let mut c = serde_json::Map::new();
for (k, v) in conformance_counts {
c.insert(k.to_string(), serde_json::json!(v));
}
m.insert("conformance".into(), serde_json::Value::Object(c));
m.insert(
"foreign_kinds".into(),
serde_json::to_value(&foreign_kinds).unwrap_or(serde_json::json!({})),
);
m.insert("findings".into(), serde_json::Value::Object(findings));
if let Some(note) = sealed_note {
m.insert("sealed".into(), note);
}
m.insert(
"independence".into(),
serde_json::json!({
"self_checked": capped(self_checked),
"confirmed_independent": capped(confirmed_independent),
"unconfirmable": capped(unconfirmable),
"comparator": "every identity that mutated the verified plan, its criteria or its session-log notes since the criterion was written; a non-criterion compares against its own author",
"executors": serde_json::Value::Object(executors),
"readings": serde_json::Value::Object(readings),
}),
);
out.insert(mem, serde_json::Value::Object(m));
}
serde_json::Value::Object(out)
}
#[derive(Debug, Clone, serde::Serialize)]
pub struct DerivationFinding {
pub source: crate::entity::EntityId,
pub rel_type: String,
pub target: crate::entity::EntityId,
pub state: String,
#[serde(skip_serializing_if = "Option::is_none")]
pub baseline: Option<String>,
pub current: String,
}
pub fn health_stale_derivations_axis(
engine: &crate::engine::Engine,
mem_filter: Option<&str>,
) -> serde_json::Value {
let mut mems: Vec<String> = engine.mem_names().iter().map(|s| s.to_string()).collect();
mems.sort();
let mut out = serde_json::Map::new();
for mem in mems {
if let Some(f) = mem_filter
&& f != mem
{
continue;
}
let findings = engine.derivation_report(&mem).unwrap_or_default();
out.insert(
mem,
serde_json::to_value(&findings).unwrap_or(serde_json::Value::Array(Vec::new())),
);
}
serde_json::Value::Object(out)
}
pub const OPEN_QUESTIONS_ITEM_CAP: usize = 20;
pub fn health_open_questions_axis(
engine: &crate::engine::Engine,
mem_filter: Option<&str>,
) -> serde_json::Value {
let cap = OPEN_QUESTIONS_ITEM_CAP;
let capped = |mut items: Vec<serde_json::Value>| -> serde_json::Value {
let count = items.len();
let more = count.saturating_sub(cap);
items.truncate(cap);
let mut o = serde_json::Map::new();
o.insert("count".into(), serde_json::json!(count));
o.insert("items".into(), serde_json::Value::Array(items));
if more > 0 {
o.insert("more".into(), serde_json::json!(more));
}
serde_json::Value::Object(o)
};
let bindings: Vec<(String, String)> = engine
.workspace_root()
.and_then(|root| crate::pipeline_store::load_pipeline_configs(root).ok())
.map(|c| {
c.bindings
.iter()
.map(|r| (r.config.destination_mem.clone(), r.name.clone()))
.collect()
})
.unwrap_or_default();
let mounted: Vec<String> = engine.mem_names().iter().map(|s| s.to_string()).collect();
let mut mems: Vec<String> = mounted.clone();
mems.sort();
let mut out = serde_json::Map::new();
for mem in &mems {
if let Some(f) = mem_filter
&& f != mem
{
continue;
}
let stubs = capped(
engine
.store()
.all_entities()
.filter(|e| e.stub && e.id.mem() == mem)
.map(|e| serde_json::json!({ "kind": "stub", "id": e.id.to_string() }))
.collect(),
);
let (mut recheck, mut unresolvable, mut unobserved, mut dangling_rows) =
(Vec::new(), Vec::new(), Vec::new(), Vec::new());
let mut span_absent = Vec::new();
let mut aging = Vec::new();
let mut entity_end_unreconciled: Option<String> = None;
if let Ok(report) = engine.verify_mem_anchors(mem) {
entity_end_unreconciled = report.unreconciled.clone();
for a in &report.anchors {
if let Some(days) = a.unobserved_for_days
&& days > 0
{
aging.push(serde_json::json!({
"kind": "anchor_aging",
"id": a.entity_id,
"artifact": a.artifact,
"state": a.state,
"observed_at": a.observed_at,
"unobserved_for_days": days,
"note": format!("unobserved for {days} days"),
}));
}
let item = serde_json::json!({
"kind": format!("anchor_{}", a.state),
"id": a.entity_id,
"artifact": a.artifact,
});
match a.state.as_str() {
"recheck" => recheck.push(item),
"orphaned" => unresolvable.push(item),
"span_absent" => span_absent.push(item),
"unobserved" => unobserved.push(item),
"dangling" => dangling_rows.push(item),
_ => {}
}
}
}
let constraints = capped(
engine
.constraint_findings(Some(mem))
.iter()
.map(|r| {
serde_json::json!({
"kind": "unsatisfied_constraint",
"id": r.id.to_string(),
"violations": r.violations.len(),
})
})
.collect(),
);
let dangling = capped(
collect_dangling_links(engine.store(), Some(mem))
.iter()
.map(|d| {
serde_json::json!({
"kind": d.kind.code(),
"id": d.from.to_string(),
"target": d.target_id.to_string(),
"repair": d.kind.repair(),
})
})
.collect(),
);
let mut process = Vec::new();
let mem_bindings: Vec<&String> = bindings
.iter()
.filter(|(d, _)| d == mem)
.map(|(_, b)| b)
.collect();
let mut resolutions: Vec<(Option<String>, crate::binding_run::ProcessMemResolution)> =
Vec::new();
if mem_bindings.is_empty() {
let r = crate::binding_run::resolve_process_mem(engine, mem, "");
if r.declared {
resolutions.push((None, r));
}
} else {
for binding in &mem_bindings {
let derived =
crate::binding_run::derived_process_mem_name(&format!("{mem}/{binding}"));
resolutions.push((
Some((*binding).clone()),
crate::binding_run::resolve_process_mem(engine, mem, &derived),
));
}
}
for (binding, r) in resolutions {
if r.mounted {
let mut open = Vec::new();
let mut searched = Vec::new();
for e in engine
.store()
.all_entities()
.filter(|e| !e.stub && e.id.mem() == r.mem.as_str())
{
let item = serde_json::json!({
"kind": e.entity_type,
"id": e.id.to_string(),
"title": e.title,
});
if e.entity_type == "negative_finding" {
searched.push(item);
} else {
open.push(item);
}
}
let by_id = |a: &serde_json::Value, b: &serde_json::Value| {
a["id"].as_str().cmp(&b["id"].as_str())
};
open.sort_by(by_id);
searched.sort_by(by_id);
process.push(serde_json::json!({
"binding": binding,
"process_mem": r.mem,
"declared": r.declared,
"resolvable": true,
"open_entries": capped(open),
"already_searched": capped(searched),
}));
} else if r.declared {
process.push(serde_json::json!({
"binding": binding,
"process_mem": r.mem,
"declared": true,
"resolvable": false,
"finding": "DECLARED_PROCESS_MEM_MISSING",
}));
} else {
process.push(serde_json::json!({
"binding": binding,
"resolvable": false,
}));
}
}
let (mut missing, mut unchecked) = (Vec::new(), Vec::new());
if let Some(schema) = engine.schema_for(mem) {
for entity in engine
.store()
.all_entities()
.filter(|e| !e.stub && e.id.mem() == mem)
{
let Some(td) = schema.types.get(&entity.entity_type) else {
continue;
};
let Some(res) = &td.resolution else { continue };
if let Some(field) = &res.status_field {
let open = match entity.metadata.get(field) {
Some(crate::entity::MetadataValue::String(s)) => {
res.open_values.contains(s)
}
_ => false,
};
if !open {
continue;
}
}
let has_condition = entity
.sections
.get(&res.condition_section)
.is_some_and(|body| !body.trim().is_empty());
if !has_condition {
missing.push(serde_json::json!({
"kind": "resolution_missing",
"id": entity.id.to_string(),
"section": res.condition_section,
}));
continue;
}
let kind = res.check_kind.as_deref().unwrap_or("verification");
let checked = crate::check::RecordKind::from_wire(kind).is_some()
&& engine
.latest_check_record(mem, &entity.id.0, kind)
.is_some_and(|r| r.verdict == "ok" && r.entity_hash == entity.content_hash);
if !checked {
unchecked.push(serde_json::json!({
"kind": "resolution_unchecked",
"id": entity.id.to_string(),
"section": res.condition_section,
"check_kind": kind,
}));
}
}
}
let resolution_missing = capped(missing);
let resolution_unchecked = capped(unchecked);
let total_open = stubs["count"].as_u64().unwrap_or(0)
+ resolution_missing["count"].as_u64().unwrap_or(0)
+ resolution_unchecked["count"].as_u64().unwrap_or(0)
+ recheck.len() as u64
+ unresolvable.len() as u64
+ span_absent.len() as u64
+ unobserved.len() as u64
+ dangling_rows.len() as u64
+ aging.len() as u64
+ constraints["count"].as_u64().unwrap_or(0)
+ dangling["count"].as_u64().unwrap_or(0)
+ process
.iter()
.filter_map(|p| p["open_entries"]["count"].as_u64())
.sum::<u64>();
let mut entry = serde_json::Map::new();
entry.insert("stubs".into(), stubs);
entry.insert("anchors_recheck".into(), capped(recheck));
entry.insert("anchors_unresolvable".into(), capped(unresolvable));
entry.insert("anchors_span_absent".into(), capped(span_absent));
entry.insert("anchors_unobserved".into(), capped(unobserved));
entry.insert("anchors_dangling".into(), capped(dangling_rows));
entry.insert("anchors_aging".into(), capped(aging));
if let Some(why) = entity_end_unreconciled {
entry.insert("entity_end_unreconciled".into(), serde_json::json!(why));
}
entry.insert("unsatisfied_constraints".into(), constraints);
entry.insert("dangling_links".into(), dangling);
entry.insert("resolution_missing".into(), resolution_missing);
entry.insert("resolution_unchecked".into(), resolution_unchecked);
if !process.is_empty() {
entry.insert("process".into(), serde_json::Value::Array(process));
} else {
entry.insert("process_mem_resolvable".into(), serde_json::json!(false));
}
entry.insert("total_open".into(), serde_json::json!(total_open));
out.insert(mem.clone(), serde_json::Value::Object(entry));
}
let mut top = serde_json::Map::new();
top.insert("_item_cap".into(), serde_json::json!(cap));
for (k, v) in out {
top.insert(k, v);
}
serde_json::Value::Object(top)
}
pub fn health_anchors_axis(
engine: &crate::engine::Engine,
mem_filter: Option<&str>,
) -> serde_json::Value {
let mut mems: Vec<String> = engine.mem_names().iter().map(|s| s.to_string()).collect();
mems.retain(|m| mem_filter.is_none_or(|v| m == v));
mems.sort();
let mut out = serde_json::Map::new();
for mem in mems {
let Ok(report) = engine.verify_mem_anchors(&mem) else {
continue;
};
let condition = report.sidecar_error.as_ref().map(|why| {
serde_json::json!({
"code": "ANCHORS_SIDECAR_UNREADABLE",
"mem": mem,
"reason": why,
})
});
let mut row = serde_json::json!({
"condition": condition,
"drifted": report.drifted,
"recheck": report.recheck,
"unresolvable": report.unresolvable,
"span_absent": report.span_absent,
"unobserved": report.unobserved,
"dangling": report.dangling,
"entity_end_unreconciled": report.unreconciled,
"aging": report
.anchors
.iter()
.filter(|a| a.observed_at.is_some())
.map(|a| {
let days = a.unobserved_for_days.unwrap_or(0);
serde_json::json!({
"id": a.entity_id,
"artifact": a.artifact,
"state": a.state,
"observed_at": a.observed_at,
"unobserved_for_days": days,
"note": format!("unobserved for {days} days"),
})
})
.collect::<Vec<_>>(),
});
if let (Some(o), Ok(serde_json::Value::Object(f))) =
(row.as_object_mut(), serde_json::to_value(&report.figure))
{
o.extend(f);
}
out.insert(mem, row);
}
serde_json::Value::Object(out)
}
pub fn compute_health(
store: &Store,
default_schema: &TypeDefinition,
mem_schemas: &HashMap<String, Arc<Schema>>,
mem_filter: Option<&str>,
) -> HealthSummary {
let mut missing_fields = Vec::new();
let mut stale_entities = Vec::new();
let today_days = days_since_epoch();
let in_scope = |mem: &str| mem_filter.is_none_or(|v| mem == v);
for entity in store.all_entities() {
if entity.stub || !in_scope(&entity.mem) {
continue;
}
let resolved = mem_schemas
.get(entity.mem.as_str())
.and_then(|s| s.types.get(entity.entity_type.as_str()).cloned())
.or_else(|| type_by_name(&entity.entity_type));
let schema: &TypeDefinition = resolved.as_deref().unwrap_or(default_schema);
let mut issues = Vec::new();
for field in &schema.health_required_fields {
if schema.section(field).is_some() {
let content = entity.sections.get(field.as_str());
if content.is_none_or(|c| c.trim().is_empty()) {
if let Some(issue) = section_heading_mismatch_issue(entity, schema, field) {
issues.push(issue);
} else {
issues.push(HealthIssue {
field: field.clone(),
code: super::HealthIssueCode::Missing,
message: format!("required section '{field}' is empty"),
});
}
}
} else {
let value = entity.metadata.get(field.as_str());
let is_empty = match value {
None => true,
Some(v) => v.to_frontmatter_string().trim().is_empty(),
};
if is_empty {
issues.push(HealthIssue {
field: field.clone(),
code: super::HealthIssueCode::Missing,
message: format!("required field '{field}' is missing"),
});
}
}
}
for s in schema.sections.iter().filter(|s| !s.catch_all) {
if schema.health_required_fields.contains(&s.key) {
continue; }
let content = entity.sections.get(s.key.as_str());
if content.is_none_or(|c| c.trim().is_empty())
&& let Some(issue) = section_heading_mismatch_issue(entity, schema, &s.key)
{
issues.push(issue);
}
}
if let Some(mem_schema) = mem_schemas.get(entity.mem.as_str()) {
let mut seen_unknown = std::collections::HashSet::new();
for rel in &entity.relationships {
if !mem_schema.relationship_known(&rel.rel_type) {
if seen_unknown.insert(rel.rel_type.clone()) {
let suggestion = mem_schema
.suggest_relationship(&rel.rel_type)
.map(|s| format!(" Did you mean '{s}'?"))
.unwrap_or_default();
let (schema_name, schema_version) = mem_schema.id();
issues.push(HealthIssue {
field: "relationships".to_string(),
code: super::HealthIssueCode::UndeclaredRelationship,
message: format!(
"relationship '{}' is not declared in schema \
'{schema_name}@{schema_version}'.{suggestion}",
rel.rel_type
),
});
}
continue;
}
let target_type = store
.get(&rel.target)
.map(|t| t.entity_type.clone())
.filter(|t| !t.is_empty());
if let Err(crate::runtime_validator::ValidationError::InvalidRelationshipShape {
rel_type,
from_type,
to_type,
allowed_source_types,
allowed_target_types,
..
}) = crate::runtime_validator::validate_rel_shape(
&rel.rel_type,
entity.entity_type.as_str(),
target_type.as_deref(),
mem_schema.as_ref(),
) {
let allowed_src = if allowed_source_types.is_empty() {
"<any>".to_string()
} else {
allowed_source_types.join(", ")
};
let allowed_tgt = if allowed_target_types.is_empty() {
"<any>".to_string()
} else {
allowed_target_types.join(", ")
};
issues.push(HealthIssue {
field: "relationships".to_string(),
code: super::HealthIssueCode::InvalidRelShape,
message: format!(
"INVALID_REL_SHAPE: edge '{rel_type}' from \
'{from_type}' to '{to_type}' (target {target}) \
violates declared shape — allowed_source_types: \
[{allowed_src}], allowed_target_types: \
[{allowed_tgt}]. Remove via \
`memstead_relate from={from_id} to={target} \
type={rel_type} remove=true`.",
target = rel.target,
from_id = entity.id,
),
});
}
}
}
let auto_ts_field = schema.metadata_fields.iter().find(|f| f.auto_timestamp);
if let Some(ts_field) = auto_ts_field
&& let Some(val) = entity.metadata.get(ts_field.key.as_str())
{
let date_str = val.to_frontmatter_string();
if let Some(modified_days) = parse_iso_to_days(&date_str) {
let days_since = today_days.saturating_sub(modified_days);
if days_since > schema.staleness_threshold_days as u64 {
stale_entities.push(StaleEntity {
id: entity.id.clone(),
title: entity.title.clone(),
days_since_modified: days_since,
anchor_state: None,
});
}
}
}
if !issues.is_empty() {
let total = schema.health_required_fields.len();
let score = if total > 0 {
(total.saturating_sub(issues.len()) as f32) / (total as f32)
} else {
1.0
};
missing_fields.push(HealthReport {
id: entity.id.clone(),
title: entity.title.clone(),
score,
issues,
});
}
}
stale_entities.sort_by_key(|e| std::cmp::Reverse(e.days_since_modified));
let orphan_count = query::find_orphans_with_schemas(store, mem_schemas)
.into_iter()
.filter(|id| store.get(id).is_some_and(|e| in_scope(&e.mem)))
.count();
let leaf_entities_by_type = match mem_filter {
None => query::leaf_population(store, mem_schemas),
Some(v) => {
let scoped: HashMap<String, Arc<Schema>> = mem_schemas
.iter()
.filter(|(mem, _)| mem.as_str() == v)
.map(|(mem, s)| (mem.clone(), s.clone()))
.collect();
query::leaf_population(store, &scoped)
}
};
let stub_count = query::find_stubs(store)
.iter()
.filter(|(id, _)| store.get(id).is_some_and(|e| in_scope(&e.mem)))
.count();
stale_entities.sort_by(|a, b| a.id.0.cmp(&b.id.0));
missing_fields.sort_by(|a, b| a.id.0.cmp(&b.id.0));
HealthSummary {
stale_entities,
anchor_fresh: Vec::new(),
missing_fields,
orphan_count,
stub_count,
warnings: Vec::new(),
quarantined: Vec::new(),
load_errors: Vec::new(),
boot_diagnosis: None,
leaf_entities_by_type,
dangling_links: None,
findings: None,
tag_distribution: None,
tag_distribution_folded: None,
untagged_entities: None,
}
}
pub fn collect_tag_distribution(
store: &Store,
mem_filter: Option<&str>,
limit: usize,
) -> (Vec<TagDistribution>, Vec<FoldedTag>, UntaggedStats) {
let mut counts: HashMap<String, (usize, HashMap<String, usize>)> = HashMap::new();
let mut untagged = UntaggedStats {
total: 0,
by_entity_type: HashMap::new(),
};
for entity in store.all_entities() {
if entity.stub {
continue;
}
if let Some(v) = mem_filter
&& entity.mem != v
{
continue;
}
let tags_raw = entity
.metadata
.get("tags")
.and_then(|v| match v {
MetadataValue::String(s) => Some(s.as_str()),
_ => None,
})
.unwrap_or("");
let mut any_tag = false;
for tag in tags_raw.split(',').map(str::trim).filter(|s| !s.is_empty()) {
any_tag = true;
let entry = counts
.entry(tag.to_string())
.or_insert_with(|| (0, HashMap::new()));
entry.0 += 1;
*entry.1.entry(entity.entity_type.clone()).or_insert(0) += 1;
}
if !any_tag {
untagged.total += 1;
*untagged
.by_entity_type
.entry(entity.entity_type.clone())
.or_insert(0) += 1;
}
}
let mut entries: Vec<TagDistribution> = counts
.iter()
.map(|(tag, (count, by_type))| TagDistribution {
tag: tag.clone(),
count: *count,
by_entity_type: by_type.clone(),
})
.collect();
entries.sort_by(|a, b| b.count.cmp(&a.count).then_with(|| a.tag.cmp(&b.tag)));
entries.truncate(limit);
let mut by_canonical: HashMap<String, Vec<(String, usize)>> = HashMap::new();
for (tag, (count, _)) in counts.iter() {
by_canonical
.entry(tag.to_lowercase())
.or_default()
.push((tag.clone(), *count));
}
let mut folded: Vec<FoldedTag> = by_canonical
.into_iter()
.filter(|(_, v)| v.len() > 1)
.map(|(canonical, mut variants)| {
variants.sort_by(|a, b| b.1.cmp(&a.1).then_with(|| a.0.cmp(&b.0)));
let total = variants.iter().map(|(_, c)| *c).sum();
FoldedTag {
canonical,
total,
variants: variants
.into_iter()
.map(|(tag, count)| TagVariant { tag, count })
.collect(),
}
})
.collect();
folded.sort_by(|a, b| {
b.total
.cmp(&a.total)
.then_with(|| a.canonical.cmp(&b.canonical))
});
(entries, folded, untagged)
}
pub fn collect_dangling_links(store: &Store, mem_filter: Option<&str>) -> Vec<DanglingLink> {
use crate::entity::parser::extract_inline_links_lenient;
use std::collections::HashSet;
let mut out = Vec::new();
for entity in store.all_entities() {
if entity.stub {
continue;
}
if let Some(v) = mem_filter
&& entity.mem != v
{
continue;
}
let explicit_targets: HashSet<_> = entity
.relationships
.iter()
.map(|r| r.target.clone())
.collect();
for (section_key, section_body) in &entity.sections {
for target_id in extract_inline_links_lenient(section_body, &entity.mem) {
let target_missing = store.get(&target_id).map(|e| e.stub).unwrap_or(true);
let alias_orphan = !target_missing && !explicit_targets.contains(&target_id);
let kind = if target_missing {
crate::ops::DanglingLinkKind::LinkTargetMissing
} else {
crate::ops::DanglingLinkKind::LinkNotRelated
};
if target_missing || alias_orphan {
out.push(DanglingLink {
kind,
from: entity.id.clone(),
target_id: target_id.clone(),
target_path: target_id.path().to_string(),
section: Some(section_key.clone()),
});
}
}
}
for rel in &entity.relationships {
if store.get(&rel.target).is_some() {
continue;
}
let already_reported = out
.iter()
.any(|d| d.from == entity.id && d.target_id == rel.target);
if already_reported {
continue;
}
out.push(DanglingLink {
kind: crate::ops::DanglingLinkKind::RelationTargetMissing,
from: entity.id.clone(),
target_id: rel.target.clone(),
target_path: rel.target.path().to_string(),
section: None,
});
}
}
out.sort_by(|a, b| {
(&a.from.0, &a.target_id.0, &a.section).cmp(&(&b.from.0, &b.target_id.0, &b.section))
});
out
}
pub fn collect_missing_required_outgoing(
store: &Store,
mem_filter: Option<&str>,
mem_schemas: &HashMap<String, Arc<memstead_schema::Schema>>,
) -> Vec<MissingRequiredOutgoingReport> {
let mut out = Vec::new();
for entity in store.all_entities() {
if entity.stub {
continue;
}
if let Some(v) = mem_filter
&& entity.mem != v
{
continue;
}
let Some(mem_schema) = mem_schemas.get(entity.mem.as_str()) else {
continue;
};
let Some(td) = mem_schema.types.get(entity.entity_type.as_str()) else {
continue;
};
if td.required_outgoing.is_empty() {
continue;
}
let unsatisfied = unsatisfied_required_outgoing(entity, td);
if unsatisfied.is_empty() {
continue;
}
out.push(MissingRequiredOutgoingReport {
id: entity.id.clone(),
title: entity.title.clone(),
entity_type: entity.entity_type.clone(),
mem: entity.mem.clone(),
missing: unsatisfied,
});
}
out.sort_by(|a, b| a.mem.cmp(&b.mem).then_with(|| a.id.0.cmp(&b.id.0)));
out
}
pub fn unsatisfied_required_outgoing(
entity: &crate::entity::Entity,
td: &TypeDefinition,
) -> Vec<super::MissingRequiredOutgoingBlock> {
td.required_outgoing
.iter()
.filter(|block| {
if let (Some(when_field), Some(when_value)) = (&block.when_field, &block.when_value) {
let armed = entity
.metadata
.get(when_field.as_str())
.is_some_and(|v| v.to_frontmatter_string() == *when_value);
if !armed {
return false;
}
}
let count = entity
.relationships
.iter()
.filter(|rel| block.relationships.iter().any(|name| name == &rel.rel_type))
.count();
!block.admits(count)
})
.map(|block| super::MissingRequiredOutgoingBlock {
relationships: block.relationships.clone(),
cardinality: block.cardinality.to_string(),
severity: block.severity,
when_field: block.when_field.clone(),
when_value: block.when_value.clone(),
})
.collect()
}
#[derive(Debug, Clone, serde::Serialize)]
#[serde(tag = "kind", rename_all = "snake_case")]
pub enum UnsatisfiedConstraint {
RequiresWhen {
field: String,
when_field: String,
when_value: String,
severity: memstead_schema::ConstraintSeverity,
},
Unique {
fields: Vec<String>,
values: Vec<String>,
colliding: String,
severity: memstead_schema::ConstraintSeverity,
},
EnumFromNeighbour {
field: String,
value: String,
rel_type: String,
section: String,
severity: memstead_schema::ConstraintSeverity,
},
StatusPropagation {
field: String,
value: String,
#[serde(skip_serializing_if = "Option::is_none")]
rel_type: Option<String>,
#[serde(skip_serializing_if = "Option::is_none")]
rel_types: Option<Vec<String>>,
tainted_by: String,
severity: memstead_schema::ConstraintSeverity,
},
TransitionRequiresChecks {
field: String,
to_value: String,
relationships: Vec<String>,
direction: memstead_schema::PropagationDirection,
unchecked: Vec<UncheckedRelated>,
related: usize,
min_related: usize,
severity: memstead_schema::ConstraintSeverity,
},
TransitionRequiresSelfCheck {
field: String,
to_value: String,
check_kind: String,
state: String,
severity: memstead_schema::ConstraintSeverity,
},
MustReach {
relationships: Vec<String>,
direction: memstead_schema::ReachDirection,
terminal_types: Vec<String>,
#[serde(skip_serializing_if = "Option::is_none")]
max_depth: Option<u32>,
severity: memstead_schema::ConstraintSeverity,
},
}
impl UnsatisfiedConstraint {
pub fn severity(&self) -> memstead_schema::ConstraintSeverity {
match self {
Self::RequiresWhen { severity, .. }
| Self::Unique { severity, .. }
| Self::EnumFromNeighbour { severity, .. }
| Self::StatusPropagation { severity, .. }
| Self::MustReach { severity, .. }
| Self::TransitionRequiresChecks { severity, .. }
| Self::TransitionRequiresSelfCheck { severity, .. } => *severity,
}
}
pub fn describe(&self) -> String {
match self {
Self::RequiresWhen {
field,
when_field,
when_value,
..
} => format!(
"requires_when: '{field}' is required when {when_field}={when_value} and is unset"
),
Self::Unique {
fields, colliding, ..
} => format!(
"unique: tuple ({}) collides with '{colliding}'",
fields.join(", ")
),
Self::EnumFromNeighbour {
field,
value,
rel_type,
section,
..
} => format!(
"enum_from_neighbour: '{field}' value '{value}' has no backing entry in any \
`{section}` section reached via {rel_type}"
),
Self::StatusPropagation {
field,
value,
tainted_by,
..
} => {
format!("status_propagation: tainted by '{tainted_by}' ({field}={value})")
}
Self::MustReach {
relationships,
direction,
terminal_types,
max_depth,
..
} => {
let depth = match max_depth {
Some(d) => format!(" within {d} hop(s)"),
None => String::new(),
};
format!(
"must_reach: no path via [{}] ({direction}) reaches a [{}] entity{depth}",
relationships.join(", "),
terminal_types.join(", ")
)
}
Self::TransitionRequiresChecks {
field,
to_value,
relationships,
unchecked,
related,
min_related,
..
} => {
if related < min_related {
return format!(
"transition_requires_checks: {field}={to_value} requires at least \
{min_related} related entit{} via [{}] — found {related}",
if *min_related == 1 { "y" } else { "ies" },
relationships.join(", ")
);
}
let listed: Vec<String> = unchecked
.iter()
.map(|u| format!("'{}' ({})", u.id, u.state))
.collect();
format!(
"transition_requires_checks: {field}={to_value} requires a fresh confirming \
check record on every entity related via [{}] — unconfirmed: {}",
relationships.join(", "),
listed.join(", ")
)
}
Self::TransitionRequiresSelfCheck {
field,
to_value,
check_kind,
state,
..
} => format!(
"transition_requires_self_check: {field}={to_value} requires a fresh confirming \
`{check_kind}` check record on this entity under an identity other than its \
author — derived: {state}"
),
}
}
}
#[derive(Debug, Clone, PartialEq, Eq, serde::Serialize)]
pub struct UncheckedRelated {
pub id: String,
pub state: String,
}
pub type CheckStateProvider<'a> =
&'a dyn Fn(&crate::entity::Entity, &str) -> crate::engine::independence::CheckStanding;
pub fn unsatisfied_constraints(
store: &Store,
entity: &crate::entity::Entity,
td: &TypeDefinition,
exclude: Option<&crate::entity::EntityId>,
checks: Option<CheckStateProvider<'_>>,
) -> Vec<UnsatisfiedConstraint> {
use memstead_schema::ConstraintDef;
td.constraints
.iter()
.filter_map(|c| match c {
ConstraintDef::RequiresWhen {
field,
when_field,
when_value,
severity,
} => {
let triggered = entity
.metadata
.get(when_field.as_str())
.is_some_and(|v| v.to_frontmatter_string() == *when_value);
if !triggered {
return None;
}
let satisfied = entity
.metadata
.get(field.as_str())
.is_some_and(|v| !v.to_frontmatter_string().trim().is_empty())
|| entity
.sections
.get(field.as_str())
.is_some_and(|body| !body.trim().is_empty());
if satisfied {
return None;
}
Some(UnsatisfiedConstraint::RequiresWhen {
field: field.clone(),
when_field: when_field.clone(),
when_value: when_value.clone(),
severity: *severity,
})
}
ConstraintDef::Unique { fields, severity } => {
let tuple = tuple_of(entity, fields)?;
let mut colliding: Vec<&str> = store
.all_entities()
.filter(|other| {
!other.stub
&& other.mem == entity.mem
&& other.entity_type == entity.entity_type
&& Some(&other.id) != exclude
&& other.id != entity.id
&& tuple_of(other, fields).as_ref() == Some(&tuple)
})
.map(|other| other.id.0.as_str())
.collect();
colliding.sort_unstable();
let first = colliding.first()?;
Some(UnsatisfiedConstraint::Unique {
fields: fields.clone(),
values: tuple,
colliding: first.to_string(),
severity: *severity,
})
}
ConstraintDef::EnumFromNeighbour {
field,
rel_type,
section,
severity,
} => {
let value = entity
.metadata
.get(field.as_str())
.map(|v| v.to_frontmatter_string())
.filter(|v| !v.trim().is_empty())?;
let backed = entity
.relationships
.iter()
.filter(|rel| rel.rel_type == *rel_type)
.filter_map(|rel| store.get(&rel.target))
.filter_map(|neighbour| neighbour.sections.get(section.as_str()))
.any(|body| bullet_entries(body).contains(&value));
if backed {
return None;
}
Some(UnsatisfiedConstraint::EnumFromNeighbour {
field: field.clone(),
value,
rel_type: rel_type.clone(),
section: section.clone(),
severity: *severity,
})
}
ConstraintDef::StatusPropagation { .. } => None,
ConstraintDef::TransitionRequiresChecks {
field,
to_value,
relationships,
direction,
min_related,
severity,
} => {
let triggered = entity
.metadata
.get(field.as_str())
.is_some_and(|v| v.to_frontmatter_string() == *to_value);
if !triggered {
return None;
}
let (related, unchecked) = transition_gate_standing(
store,
entity,
relationships,
*direction,
exclude,
checks,
);
if unchecked.is_empty() && related >= *min_related {
return None;
}
Some(UnsatisfiedConstraint::TransitionRequiresChecks {
field: field.clone(),
to_value: to_value.clone(),
relationships: relationships.clone(),
direction: *direction,
unchecked,
related,
min_related: *min_related,
severity: *severity,
})
}
ConstraintDef::TransitionRequiresSelfCheck {
field,
to_value,
check_kind,
severity,
} => {
let triggered = entity
.metadata
.get(field.as_str())
.is_some_and(|v| v.to_frontmatter_string() == *to_value);
if !triggered {
return None;
}
let standing = match checks {
Some(provider) => provider(entity, check_kind),
None => crate::engine::independence::CheckStanding {
state: crate::check::CheckState::NeverChecked,
independence: None,
role: crate::vcs::Role::Unspecified,
},
};
if standing.confirms() {
return None;
}
Some(UnsatisfiedConstraint::TransitionRequiresSelfCheck {
field: field.clone(),
to_value: to_value.clone(),
check_kind: check_kind.clone(),
state: standing.label().to_string(),
severity: *severity,
})
}
})
.collect()
}
pub fn transition_gate_standing(
store: &Store,
entity: &crate::entity::Entity,
relationships: &[String],
direction: memstead_schema::PropagationDirection,
exclude: Option<&crate::entity::EntityId>,
checks: Option<CheckStateProvider<'_>>,
) -> (usize, Vec<UncheckedRelated>) {
let related: Vec<&crate::entity::Entity> = match direction {
memstead_schema::PropagationDirection::Outgoing => entity
.relationships
.iter()
.filter(|rel| relationships.contains(&rel.rel_type))
.filter_map(|rel| store.get(&rel.target))
.collect(),
memstead_schema::PropagationDirection::Incoming => store
.all_entities()
.filter(|other| {
other.id != entity.id
&& Some(&other.id) != exclude
&& other
.relationships
.iter()
.any(|rel| rel.target == entity.id && relationships.contains(&rel.rel_type))
})
.collect(),
};
let total = related.len();
let mut unchecked: Vec<UncheckedRelated> = related
.into_iter()
.filter_map(|rel_entity| {
let standing = match checks {
Some(provider) => {
provider(rel_entity, crate::check::CheckKind::Verification.as_str())
}
None => crate::engine::independence::CheckStanding {
state: crate::check::CheckState::NeverChecked,
independence: None,
role: crate::vcs::Role::Unspecified,
},
};
if standing.confirms() {
None
} else {
Some(UncheckedRelated {
id: rel_entity.id.0.clone(),
state: standing.label().to_string(),
})
}
})
.collect();
unchecked.sort_by(|a, b| a.id.cmp(&b.id));
(total, unchecked)
}
fn tuple_of(entity: &crate::entity::Entity, fields: &[String]) -> Option<Vec<String>> {
fields
.iter()
.map(|f| {
entity
.metadata
.get(f.as_str())
.map(|v| v.to_frontmatter_string())
.filter(|v| !v.trim().is_empty())
})
.collect()
}
fn bullet_entries(body: &str) -> Vec<String> {
let masked = crate::markdown::mask_code_blocks_and_spans(body);
body.lines()
.zip(masked.lines())
.filter_map(|(line, masked_line)| {
let m = masked_line.trim_start();
if m.starts_with("- ") || m.starts_with("* ") {
let t = line.trim_start();
t.strip_prefix("- ")
.or_else(|| t.strip_prefix("* "))
.map(|e| e.trim().to_string())
} else {
None
}
})
.collect()
}
#[derive(Debug, Clone, serde::Serialize)]
pub struct ConstraintFindingReport {
pub id: crate::entity::EntityId,
pub title: String,
pub entity_type: String,
pub mem: String,
pub violations: Vec<UnsatisfiedConstraint>,
#[serde(skip_serializing_if = "Vec::is_empty")]
pub format_violations: Vec<crate::section_format::SectionFormatViolation>,
}
pub fn collect_constraint_findings(
store: &Store,
mem_filter: Option<&str>,
mem_schemas: &HashMap<String, Arc<memstead_schema::Schema>>,
checks: Option<CheckStateProvider<'_>>,
) -> Vec<ConstraintFindingReport> {
use memstead_schema::ConstraintDef;
type Bucket = (
Vec<UnsatisfiedConstraint>,
Vec<crate::section_format::SectionFormatViolation>,
);
let mut by_entity: std::collections::BTreeMap<String, Bucket> = Default::default();
let needs_reverse = mem_schemas.values().any(|s| {
s.types.values().any(|t| {
t.must_reach
.iter()
.any(|ob| ob.direction == memstead_schema::ReachDirection::In)
})
});
let reverse: ReverseIndex = if needs_reverse {
build_reverse_index(store)
} else {
ReverseIndex::default()
};
for entity in store.all_entities() {
if entity.stub {
continue;
}
if let Some(v) = mem_filter
&& entity.mem != v
{
continue;
}
let Some(mem_schema) = mem_schemas.get(entity.mem.as_str()) else {
continue;
};
let Some(td) = mem_schema.types.get(entity.entity_type.as_str()) else {
continue;
};
for def in &td.sections {
if def.compiled_content.is_none() {
continue;
}
let Some(body) = entity.sections.get(def.key.as_str()) else {
continue;
};
let violations = crate::section_format::check_section_format(def, body);
if !violations.is_empty() {
by_entity
.entry(entity.id.0.clone())
.or_default()
.1
.extend(violations);
}
}
for ob in &td.must_reach {
if !reaches_terminal(store, &reverse, &entity.id, ob) {
by_entity.entry(entity.id.0.clone()).or_default().0.push(
UnsatisfiedConstraint::MustReach {
relationships: ob.relationships.clone(),
direction: ob.direction,
terminal_types: ob.terminal_types.clone(),
max_depth: ob.max_depth,
severity: ob.severity,
},
);
}
}
if td.constraints.is_empty() {
continue;
}
let violations = unsatisfied_constraints(store, entity, td, None, checks);
if !violations.is_empty() {
by_entity
.entry(entity.id.0.clone())
.or_default()
.0
.extend(violations);
}
for c in &td.constraints {
let ConstraintDef::StatusPropagation {
field,
value,
rel_type,
rel_types,
direction,
severity,
} = c
else {
continue;
};
let terminal = entity
.metadata
.get(field.as_str())
.is_some_and(|v| v.to_frontmatter_string() == *value);
if !terminal {
continue;
}
let set = c
.propagation_rel_types()
.expect("StatusPropagation always yields a set");
for tainted in reach_transitively(store, &entity.id, &set, *direction) {
if let Some(v) = mem_filter
&& tainted.mem() != v
{
continue;
}
by_entity.entry(tainted.0.clone()).or_default().0.push(
UnsatisfiedConstraint::StatusPropagation {
field: field.clone(),
value: value.clone(),
rel_type: rel_type.clone(),
rel_types: rel_types.clone(),
tainted_by: entity.id.to_string(),
severity: *severity,
},
);
}
}
}
let mut out: Vec<ConstraintFindingReport> = by_entity
.into_iter()
.filter_map(|(id, (violations, format_violations))| {
let id = crate::entity::EntityId(id);
let entity = store.get(&id)?;
Some(ConstraintFindingReport {
id,
title: entity.title.clone(),
entity_type: entity.entity_type.clone(),
mem: entity.mem.clone(),
violations,
format_violations,
})
})
.collect();
out.sort_by(|a, b| a.mem.cmp(&b.mem).then_with(|| a.id.0.cmp(&b.id.0)));
out
}
fn reach_transitively(
store: &Store,
start: &crate::entity::EntityId,
rel_types: &[String],
direction: memstead_schema::PropagationDirection,
) -> Vec<crate::entity::EntityId> {
use memstead_schema::PropagationDirection;
let mut seen: std::collections::HashSet<crate::entity::EntityId> =
std::iter::once(start.clone()).collect();
let mut frontier = vec![start.clone()];
let mut reached = Vec::new();
while let Some(current) = frontier.pop() {
let next: Vec<crate::entity::EntityId> = match direction {
PropagationDirection::Incoming => store
.all_entities()
.filter(|e| {
e.relationships
.iter()
.any(|r| rel_types.iter().any(|n| n == &r.rel_type) && r.target == current)
})
.map(|e| e.id.clone())
.collect(),
PropagationDirection::Outgoing => store
.get(¤t)
.map(|e| {
e.relationships
.iter()
.filter(|r| rel_types.iter().any(|n| n == &r.rel_type))
.map(|r| r.target.clone())
.collect()
})
.unwrap_or_default(),
};
for id in next {
if seen.insert(id.clone()) {
if store.get(&id).is_some_and(|e| !e.stub) {
reached.push(id.clone());
}
frontier.push(id);
}
}
}
reached
}
type ReverseIndex =
std::collections::HashMap<crate::entity::EntityId, Vec<(String, crate::entity::EntityId)>>;
fn build_reverse_index(store: &Store) -> ReverseIndex {
let mut idx = ReverseIndex::default();
for entity in store.all_entities() {
for rel in &entity.relationships {
idx.entry(rel.target.clone())
.or_default()
.push((rel.rel_type.clone(), entity.id.clone()));
}
}
idx
}
fn reaches_terminal(
store: &Store,
reverse: &ReverseIndex,
start: &crate::entity::EntityId,
ob: &memstead_schema::MustReach,
) -> bool {
use memstead_schema::ReachDirection;
let mut seen: std::collections::HashSet<crate::entity::EntityId> =
std::iter::once(start.clone()).collect();
let mut frontier = vec![start.clone()];
let mut depth: u32 = 0;
while !frontier.is_empty() {
if let Some(max) = ob.max_depth
&& depth >= max
{
return false;
}
depth += 1;
let mut next_frontier = Vec::new();
for current in frontier {
let next: Vec<crate::entity::EntityId> = match ob.direction {
ReachDirection::Out => store
.get(¤t)
.map(|e| {
e.relationships
.iter()
.filter(|r| ob.relationships.iter().any(|n| n == &r.rel_type))
.map(|r| r.target.clone())
.collect()
})
.unwrap_or_default(),
ReachDirection::In => reverse
.get(¤t)
.map(|sources| {
sources
.iter()
.filter(|(rel, _)| ob.relationships.iter().any(|n| n == rel))
.map(|(_, src)| src.clone())
.collect()
})
.unwrap_or_default(),
};
for id in next {
if seen.insert(id.clone()) {
if store.get(&id).is_some_and(|e| {
!e.stub && ob.terminal_types.iter().any(|t| t == &e.entity_type)
}) {
return true;
}
next_frontier.push(id);
}
}
}
frontier = next_frontier;
}
false
}
#[derive(Debug, Clone, serde::Serialize)]
pub struct SignalReport {
pub id: crate::entity::EntityId,
pub title: String,
pub entity_type: String,
pub mem: String,
pub signals: Vec<super::signals::ComputedSignal>,
}
impl SignalReport {
pub fn has_warn(&self) -> bool {
self.signals
.iter()
.any(|s| s.level == Some(memstead_schema::SignalLevel::Warn))
}
}
pub fn collect_signal_reports(
store: &Store,
mem_filter: Option<&str>,
mem_schemas: &HashMap<String, Arc<memstead_schema::Schema>>,
) -> Vec<SignalReport> {
let mut out = Vec::new();
for entity in store.all_entities() {
if entity.stub {
continue;
}
if let Some(v) = mem_filter
&& entity.mem != v
{
continue;
}
let Some(mem_schema) = mem_schemas.get(entity.mem.as_str()) else {
continue;
};
let Some(td) = mem_schema.types.get(entity.entity_type.as_str()) else {
continue;
};
if td.signals.is_empty() {
continue;
}
let above: Vec<super::signals::ComputedSignal> =
super::signals::compute_signals(store, td, &entity.id)
.into_iter()
.filter(|s| s.level.is_some())
.collect();
if above.is_empty() {
continue;
}
out.push(SignalReport {
id: entity.id.clone(),
title: entity.title.clone(),
entity_type: entity.entity_type.clone(),
mem: entity.mem.clone(),
signals: above,
});
}
out.sort_by(|a, b| a.mem.cmp(&b.mem).then_with(|| a.id.0.cmp(&b.id.0)));
out
}
#[derive(Debug, Clone, serde::Serialize)]
pub struct SchemaFormatDefect {
pub schema: String,
pub type_name: String,
pub section: String,
pub problems: Vec<String>,
}
pub fn collect_schema_format_defects(
mem_schemas: &HashMap<String, Arc<memstead_schema::Schema>>,
) -> Vec<SchemaFormatDefect> {
let mut seen: std::collections::BTreeSet<String> = Default::default();
let mut out = Vec::new();
let mut schemas: Vec<&Arc<memstead_schema::Schema>> = mem_schemas.values().collect();
schemas.sort_by_key(|s| (s.manifest.name.clone(), s.version.clone()));
for schema in schemas {
let schema_ref = format!("{}@{}", schema.manifest.name, schema.version);
if !seen.insert(schema_ref.clone()) {
continue;
}
for td in schema.types.values() {
for section in &td.sections {
if !section.format_problems.is_empty() {
out.push(SchemaFormatDefect {
schema: schema_ref.clone(),
type_name: td.name.clone(),
section: section.key.clone(),
problems: section.format_problems.clone(),
});
}
}
}
}
out.sort_by(|a, b| {
(&a.schema, &a.type_name, &a.section).cmp(&(&b.schema, &b.type_name, &b.section))
});
out
}
#[derive(Debug, Clone, serde::Serialize)]
pub struct MissingRequiredOutgoingReport {
pub id: crate::entity::EntityId,
pub title: String,
pub entity_type: String,
pub mem: String,
pub missing: Vec<super::MissingRequiredOutgoingBlock>,
}
pub fn config_projection(
engine: &crate::Engine,
writable_mems: &[String],
mutations: serde_json::Value,
plugin: serde_json::Value,
) -> serde_json::Map<String, serde_json::Value> {
let backend_by_mem: std::collections::HashMap<&str, (&'static str, bool)> = engine
.mounts()
.iter()
.map(|m| {
(
m.mem.as_str(),
(m.storage.backend_id(), m.storage.is_durable()),
)
})
.collect();
let mems_detail: Vec<serde_json::Value> = writable_mems
.iter()
.map(|name| {
let origin = engine
.mem_router()
.origin_for_mem(name)
.map(|o| o.kind())
.unwrap_or("explicit");
let mut entry = serde_json::Map::new();
entry.insert("name".into(), serde_json::json!(name));
entry.insert("origin".into(), serde_json::json!(origin));
if let Some((storage, durable)) = backend_by_mem.get(name.as_str()).copied() {
entry.insert("storage".into(), serde_json::json!(storage));
entry.insert("durable".into(), serde_json::json!(durable));
}
let mut vcs_obj = serde_json::Map::new();
if let Ok(gitdir) = engine.gitdir_for(name) {
vcs_obj.insert("gitdir".into(), serde_json::json!(gitdir));
}
if let Ok(worktree) = engine.worktree_for(name) {
vcs_obj.insert("worktree".into(), serde_json::json!(worktree));
}
if let Some(sha) = engine.mem_head_sha(name).ok().flatten() {
vcs_obj.insert("head".into(), serde_json::json!(sha));
}
if !vcs_obj.is_empty() {
entry.insert("vcs".into(), serde_json::Value::Object(vcs_obj));
}
if let Some(cfg) = engine.mem_config_for(name) {
if let Some(title) = &cfg.title {
entry.insert("title".into(), serde_json::json!(title));
}
if let Some(subject) = &cfg.subject {
entry.insert("subject".into(), serde_json::json!(subject));
}
let guidance = serde_json::Map::from_iter(
cfg.write_guidance
.iter()
.map(|(k, v)| (k.clone(), v.clone())),
);
entry.insert("write_guidance".into(), serde_json::Value::Object(guidance));
let extra = serde_json::Map::from_iter(
cfg.extra.iter().map(|(k, v)| (k.clone(), v.clone())),
);
entry.insert("extra".into(), serde_json::Value::Object(extra));
}
serde_json::Value::Object(entry)
})
.collect();
let mut out = serde_json::Map::new();
out.insert("mems".into(), serde_json::json!(mems_detail));
out.insert("mutations".into(), mutations);
out.insert("plugin".into(), plugin);
out
}
pub fn config_projection_from_settings(
settings: &crate::workspace::WorkspaceSettings,
) -> (serde_json::Value, serde_json::Value) {
let mutations = serde_json::json!({ "require_notes": settings.mutations.require_notes });
let plugin_map: serde_json::Map<String, serde_json::Value> = settings
.plugin
.iter()
.map(|(k, v)| {
(
k.clone(),
serde_json::to_value(v).unwrap_or(serde_json::Value::Null),
)
})
.collect();
(mutations, serde_json::Value::Object(plugin_map))
}
pub(crate) fn section_heading_mismatch_issue(
entity: &crate::entity::Entity,
schema: &TypeDefinition,
key: &str,
) -> Option<HealthIssue> {
let def = schema.section(key)?;
let derived = memstead_schema::derive_section_key(&def.heading);
if derived == key {
return None;
}
if !entity
.raw_section_headings
.iter()
.any(|h| h == &def.heading)
{
return None;
}
let landing = match schema.catch_all_section() {
Some(c) => format!(
"the content was absorbed into catch-all section '{}'",
c.key
),
None => "the content is unreachable under any declared key".to_string(),
};
Some(HealthIssue {
field: key.to_string(),
code: super::HealthIssueCode::SectionHeadingMismatch,
message: format!(
"SECTION_HEADING_MISMATCH: section '{key}' is not missing — its content sits \
under heading '{found}', which derives to '{derived}', not '{key}'; {landing}. \
The schema's declared heading cannot round-trip to its key (expected a heading \
that derives to '{key}'); fix the schema's heading/key pair — new installs of \
such a schema are refused",
found = def.heading,
),
})
}
pub fn entity_health(entity: &crate::entity::Entity, schema: &TypeDefinition) -> HealthReport {
let mut issues = Vec::new();
for field in &schema.health_required_fields {
if schema.section(field).is_some() {
let content = entity.sections.get(field.as_str());
if content.is_none_or(|c| c.trim().is_empty()) {
if let Some(issue) = section_heading_mismatch_issue(entity, schema, field) {
issues.push(issue);
} else {
issues.push(HealthIssue {
field: field.clone(),
code: super::HealthIssueCode::Missing,
message: format!("required section '{field}' is empty"),
});
}
}
} else {
let value = entity.metadata.get(field.as_str());
if value.is_none() {
issues.push(HealthIssue {
field: field.clone(),
code: super::HealthIssueCode::Missing,
message: format!("required field '{field}' is missing"),
});
}
}
}
for s in schema.sections.iter().filter(|s| !s.catch_all) {
if schema.health_required_fields.contains(&s.key) {
continue; }
let content = entity.sections.get(s.key.as_str());
if content.is_none_or(|c| c.trim().is_empty())
&& let Some(issue) = section_heading_mismatch_issue(entity, schema, &s.key)
{
issues.push(issue);
}
}
let total = schema.health_required_fields.len();
let score = if total > 0 {
(total.saturating_sub(issues.len()) as f32) / (total as f32)
} else {
1.0
};
HealthReport {
id: entity.id.clone(),
title: entity.title.clone(),
score,
issues,
}
}
pub fn days_since_epoch() -> u64 {
if let Some(pinned) = std::env::var("MEMSTEAD_TODAY")
.ok()
.and_then(|s| crate::engine::due::pinned_days_since_epoch(&s))
{
return pinned;
}
#[cfg(target_arch = "wasm32")]
{
(js_sys::Date::now() / 1000.0) as u64 / 86400
}
#[cfg(not(target_arch = "wasm32"))]
{
std::time::SystemTime::now()
.duration_since(std::time::UNIX_EPOCH)
.unwrap_or_default()
.as_secs()
/ 86400
}
}
pub fn parse_iso_to_days(date: &str) -> Option<u64> {
let date_part = date.split('T').next()?;
let parts: Vec<&str> = date_part.split('-').collect();
if parts.len() != 3 {
return None;
}
let year: u64 = parts[0].parse().ok()?;
let month: u64 = parts[1].parse().ok()?;
let day: u64 = parts[2].parse().ok()?;
Some(ymd_to_days(year, month, day))
}
fn ymd_to_days(year: u64, month: u64, day: u64) -> u64 {
let y = if month <= 2 { year - 1 } else { year };
let m = if month <= 2 { month + 9 } else { month - 3 };
let era = y / 400;
let yoe = y - era * 400;
let doy = (153 * m + 2) / 5 + day - 1;
let doe = yoe * 365 + yoe / 4 - yoe / 100 + doy;
let days = era * 146097 + doe;
days - 719468
}
#[cfg(test)]
mod tests;