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",
"conformance",
"integrity",
"config",
"anchors",
"friction",
"open_questions",
"stale_derivations",
"checks",
];
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 = engine
.workspace_root()
.map(crate::check::CheckLedger::for_workspace);
let mut latest: std::collections::BTreeMap<String, crate::check::CheckRecord> =
std::collections::BTreeMap::new();
if let Some(l) = &ledger {
for rec in l.all() {
latest.insert(rec.entity.clone(), rec);
}
}
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 mut counts = std::collections::BTreeMap::from([
("never_checked", 0usize),
("checked_ok", 0usize),
("check_failed", 0usize),
("check_stale", 0usize),
]);
let self_checked: Vec<String> = Vec::new();
let confirmed_independent: Vec<String> = Vec::new();
let mut unconfirmable: Vec<String> = Vec::new();
for e in engine.store().all_entities().filter(|e| e.mem == mem) {
let id = e.id.0.clone();
let state = crate::check::derive_state(latest.get(&id), &e.content_hash);
*counts.entry(state.as_str()).or_insert(0) += 1;
if state != crate::check::CheckState::CheckedOk {
continue;
}
unconfirmable.push(id);
}
let mut m = serde_json::Map::new();
for (k, v) in counts {
m.insert(k.to_string(), serde_json::json!(v));
}
m.insert(
"independence".into(),
serde_json::json!({
"self_checked": capped(self_checked),
"confirmed_independent": capped(confirmed_independent),
"unconfirmable": capped(unconfirmable),
}),
);
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) = (Vec::new(), Vec::new());
if let Ok(report) = engine.verify_mem_anchors(mem) {
for a in &report.anchors {
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),
"unresolvable" => unresolvable.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": "dangling_link",
"id": d.from.to_string(),
"target": d.target_id.to_string(),
})
})
.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::ingest::resolve::ProcessMemResolution)> =
Vec::new();
if mem_bindings.is_empty() {
let r = crate::ingest::resolve::resolve_process_mem(engine, mem, "");
if r.declared {
resolutions.push((None, r));
}
} else {
for binding in &mem_bindings {
resolutions.push((
Some((*binding).clone()),
crate::ingest::resolve::resolve_process_mem(engine, mem, binding),
));
}
}
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);
}
}
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 total_open = stubs["count"].as_u64().unwrap_or(0)
+ recheck.len() as u64
+ unresolvable.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("unsatisfied_constraints".into(), constraints);
entry.insert("dangling_links".into(), dangling);
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) -> 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 {
let Ok(report) = engine.verify_mem_anchors(&mem) else {
continue;
};
out.insert(
mem,
serde_json::json!({
"resolved": report.resolved,
"drifted": report.drifted,
"recheck": report.recheck,
"unresolvable": report.unresolvable,
}),
);
}
serde_json::Value::Object(out)
}
pub fn compute_health(
store: &Store,
default_schema: &TypeDefinition,
mem_schemas: &HashMap<String, Arc<Schema>>,
) -> HealthSummary {
let mut missing_fields = Vec::new();
let mut stale_entities = Vec::new();
let today_days = days_since_epoch();
for entity in store.all_entities() {
if entity.stub {
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,
});
}
}
}
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).len();
let leaf_entities_by_type = query::leaf_population(store, mem_schemas);
let stub_count = query::find_stubs(store).len();
HealthSummary {
stale_entities,
missing_fields,
orphan_count,
stub_count,
warnings: Vec::new(),
quarantined: 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);
if target_missing || alias_orphan {
out.push(DanglingLink {
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 {
from: entity.id.clone(),
target_id: rel.target.clone(),
target_path: rel.target.path().to_string(),
section: None,
});
}
}
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| {
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,
})
.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,
rel_type: String,
tainted_by: String,
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, .. } => *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})"),
}
}
}
pub fn unsatisfied_constraints(
store: &Store,
entity: &crate::entity::Entity,
td: &TypeDefinition,
exclude: Option<&crate::entity::EntityId>,
) -> 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).any(|entry| entry == 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,
})
.collect()
}
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) -> impl Iterator<Item = &str> {
body.lines().filter_map(|line| {
let t = line.trim_start();
t.strip_prefix("- ")
.or_else(|| t.strip_prefix("* "))
.map(str::trim)
})
}
#[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>>,
) -> 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();
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);
}
}
if td.constraints.is_empty() {
continue;
}
let violations = unsatisfied_constraints(store, entity, td, None);
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,
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;
}
for tainted in reach_transitively(store, &entity.id, rel_type, *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(),
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_type: &str,
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| r.rel_type == rel_type && r.target == current)
})
.map(|e| e.id.clone())
.collect(),
PropagationDirection::Outgoing => store
.get(¤t)
.map(|e| {
e.relationships
.iter()
.filter(|r| r.rel_type == 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
}
#[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,
}
}
fn days_since_epoch() -> u64 {
#[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
}
}
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 {
use super::*;
use crate::entity::{Entity, EntityId, MetadataValue};
use crate::store::Store;
use indexmap::IndexMap;
use memstead_schema::type_by_name;
#[test]
fn declared_process_mem_pairs_and_missing_declaration_is_typed() {
use crate::engine::test_helpers::folder_mount;
let tmp = tempfile::TempDir::new().unwrap();
let dest_dir = tmp.path().join("dest");
let proc_dir = tmp.path().join("oddly-named-process");
std::fs::create_dir_all(dest_dir.join(".memstead")).unwrap();
std::fs::create_dir_all(&proc_dir).unwrap();
std::fs::write(
dest_dir.join(".memstead").join("config.json"),
r#"{ "schema": "default@1.0.0", "processMem": "oddly-named-process" }"#,
)
.unwrap();
let engine = crate::Engine::from_mounts(vec![
(
folder_mount("dest", dest_dir.clone()),
Box::new(crate::storage::FilesystemMemWriter::new(dest_dir.clone()))
as Box<dyn crate::backend::MemBackend>,
),
(
folder_mount("oddly-named-process", proc_dir.clone()),
Box::new(crate::storage::FilesystemMemWriter::new(proc_dir))
as Box<dyn crate::backend::MemBackend>,
),
])
.unwrap();
let r = crate::ingest::resolve::resolve_process_mem(&engine, "dest", "dest-derived");
assert!(r.declared && r.mounted);
assert_eq!(r.mem, "oddly-named-process");
let r =
crate::ingest::resolve::resolve_process_mem(&engine, "oddly-named-process", "whatever");
assert!(!r.declared && !r.mounted);
assert_eq!(r.mem, "whatever");
let axis = health_open_questions_axis(&engine, Some("dest"));
let process = &axis["dest"]["process"];
assert_eq!(process[0]["process_mem"], "oddly-named-process", "{axis}");
assert_eq!(process[0]["declared"], true, "{axis}");
assert_eq!(process[0]["resolvable"], true, "{axis}");
std::fs::write(
dest_dir.join(".memstead").join("config.json"),
r#"{ "schema": "default@1.0.0", "processMem": "nowhere" }"#,
)
.unwrap();
let engine2 = crate::Engine::from_mounts(vec![(
folder_mount("dest", dest_dir.clone()),
Box::new(crate::storage::FilesystemMemWriter::new(dest_dir))
as Box<dyn crate::backend::MemBackend>,
)])
.unwrap();
let axis = health_open_questions_axis(&engine2, Some("dest"));
let process = &axis["dest"]["process"];
assert_eq!(
process[0]["finding"], "DECLARED_PROCESS_MEM_MISSING",
"{axis}"
);
assert_eq!(process[0]["resolvable"], false, "{axis}");
}
fn make_entity(name: &str, has_required: bool) -> Entity {
let mut metadata = IndexMap::new();
metadata.insert("level".into(), MetadataValue::String("M0".into()));
metadata.insert("type".into(), MetadataValue::String("spec".into()));
metadata.insert(
"created_date".into(),
MetadataValue::String("2026-01-15".into()),
);
metadata.insert(
"last_modified".into(),
MetadataValue::String("2026-04-12".into()),
);
let mut sections = IndexMap::new();
if has_required {
sections.insert("identity".into(), "Has identity.".into());
sections.insert("purpose".into(), "Has purpose.".into());
}
Entity {
id: EntityId::new("specs", name),
title: name.into(),
entity_type: "spec".into(),
mem: "specs".into(),
file_path: format!("{name}.md"),
metadata,
sections,
relationships: Vec::new(),
content_hash: String::new(),
stub: false,
stub_kind: None,
heading_spans: std::collections::HashMap::new(),
raw_section_headings: Vec::new(),
}
}
fn violating_type() -> std::sync::Arc<TypeDefinition> {
let manifest = r#"name: debate
version: 0.1.0
description: sealed-violator fixture
when_to_use: health tests
types:
- question
relationships:
mode: strict
definitions:
- name: PART_OF
description: hier
default_weight: 3.0
- name: _default
description: fallback
default_weight: 1.0
community:
resolution: 1.0
seed: 42
"#;
let type_yaml = r#"name: question
description: t
when_to_use: tests
sections:
- key: answers
heading: Answers argued
required: true
search_weight: 10.0
write_rules: []
- key: notes
heading: Notes
required: false
search_weight: 3.0
catch_all: true
write_rules: []
metadata_fields: []
title_weight: 100.0
text_fields:
- answers
- notes
hierarchy_relationship: PART_OF
no_self_loop_relationships: []
updatable_fields:
- title
- answers
- notes
health_required_fields:
- answers
staleness_threshold_days: 90
write_rules: []
"#;
memstead_schema::load_schema_from_memory(
manifest,
&[("question".to_string(), type_yaml.to_string())],
)
.expect("violating schema still loads")
.get_type("question")
.expect("question type")
}
#[test]
fn health_distinguishes_heading_mismatch_from_missing_section() {
let schema = violating_type();
let md = "---\ntype: question\n---\n# Q\n\n## Answers argued\n\nTwo answers.\n";
let parsed = crate::entity::parser::parse_markdown(md, "q.md", &schema, "debate")
.expect("parses")
.entity;
let report = entity_health(&parsed, &schema);
let mismatch: Vec<_> = report
.issues
.iter()
.filter(|i| i.code == super::super::HealthIssueCode::SectionHeadingMismatch)
.collect();
assert_eq!(mismatch.len(), 1, "issues: {:?}", report.issues);
let msg = &mismatch[0].message;
assert!(
msg.contains("'Answers argued'") && msg.contains("'answers_argued'"),
"names found heading and derived key: {msg}"
);
assert!(
msg.contains("'notes'"),
"names the catch-all landing: {msg}"
);
assert!(
!report.issues.iter().any(|i| i.message.contains("is empty")),
"must not also report the section as missing: {:?}",
report.issues
);
let md_missing = "---\ntype: question\n---\n# Q2\n";
let parsed_missing =
crate::entity::parser::parse_markdown(md_missing, "q2.md", &schema, "debate")
.expect("parses")
.entity;
let report_missing = entity_health(&parsed_missing, &schema);
assert!(
report_missing
.issues
.iter()
.any(|i| i.code == super::super::HealthIssueCode::Missing
&& i.message == "required section 'answers' is empty"),
"absent section keeps the missing report (structured MISSING code): {:?}",
report_missing.issues
);
assert!(
!report_missing
.issues
.iter()
.any(|i| i.code == super::super::HealthIssueCode::SectionHeadingMismatch),
"no mismatch finding when the heading is not in the file"
);
let ok_type = crate::entity::parser::parse_markdown(
"---\ntype: question\n---\n# Q3\n\n## Answers\n\nfree.\n",
"q3.md",
&schema,
"debate",
)
.expect("parses")
.entity;
let report_ok = entity_health(&ok_type, &schema);
assert!(
!report_ok
.issues
.iter()
.any(|i| i.code == super::super::HealthIssueCode::SectionHeadingMismatch),
"mismatch fires only when the declared heading is present: {:?}",
report_ok.issues
);
}
fn make_concept_entity(name: &str, with_definition: bool) -> Entity {
let mut metadata = IndexMap::new();
metadata.insert("type".into(), MetadataValue::String("concept".into()));
metadata.insert("maturity".into(), MetadataValue::String("emerging".into()));
metadata.insert(
"abstraction_level".into(),
MetadataValue::String("concrete".into()),
);
metadata.insert(
"created_date".into(),
MetadataValue::String("2026-01-15".into()),
);
metadata.insert(
"last_modified".into(),
MetadataValue::String("2026-04-12".into()),
);
let mut sections = IndexMap::new();
if with_definition {
sections.insert("definition".into(), "Precise definition.".into());
}
sections.insert("explanation".into(), "How it works.".into());
Entity {
id: EntityId::new("concepts", name),
title: name.into(),
entity_type: "concept".into(),
mem: "concepts".into(),
file_path: format!("{name}.md"),
metadata,
sections,
relationships: Vec::new(),
content_hash: String::new(),
stub: false,
stub_kind: None,
heading_spans: std::collections::HashMap::new(),
raw_section_headings: Vec::new(),
}
}
#[test]
fn health_concept_missing_definition_reports_definition_field() {
let schema = &type_by_name("concept").unwrap();
let entity = make_concept_entity("clarity", false);
let report = entity_health(&entity, schema);
assert!(report.issues.iter().any(|i| i.field == "definition"));
assert!(!report.issues.iter().any(|i| i.field == "identity"));
assert!(!report.issues.iter().any(|i| i.field == "purpose"));
assert!(report.score < 1.0);
let healthy = make_concept_entity("clarity-ok", true);
let healthy_report = entity_health(&healthy, schema);
assert!(
!healthy_report
.issues
.iter()
.any(|i| i.field == "definition")
);
}
#[test]
fn health_detects_missing_sections() {
let schema = &type_by_name("spec").unwrap();
let entity = make_entity("incomplete", false);
let report = entity_health(&entity, schema);
assert!(!report.issues.is_empty());
assert!(report.score < 1.0);
}
#[test]
fn health_clean_entity() {
let schema = &type_by_name("spec").unwrap();
let entity = make_entity("complete", true);
let report = entity_health(&entity, schema);
let section_issues: Vec<_> = report
.issues
.iter()
.filter(|i| i.field == "identity" || i.field == "purpose")
.collect();
assert!(section_issues.is_empty());
}
#[test]
fn health_summary_counts() {
let mut store = Store::new();
let e1 = make_entity("healthy", true);
let e2 = make_entity("unhealthy", false);
store.upsert(e1.id.clone(), e1);
store.upsert(e2.id.clone(), e2);
let schema = &type_by_name("spec").unwrap();
let summary = compute_health(&store, schema, &HashMap::new());
assert_eq!(summary.orphan_count, 2); assert_eq!(summary.stub_count, 0);
}
#[test]
fn health_surfaces_invalid_rel_shape_on_existing_edges() {
use crate::entity::Relationship;
use memstead_schema::SchemaRegistry;
let registry = SchemaRegistry::builtin();
let software = registry
.get("software", &semver::Version::new(0, 2, 0))
.expect("software schema ships as a builtin");
let mut store = Store::new();
let mut bad = make_entity("bad-owns-source", true);
bad.entity_type = "spec".into();
bad.metadata
.insert("level".into(), MetadataValue::String("M0".into()));
bad.metadata
.insert("stability".into(), MetadataValue::String("evolving".into()));
bad.relationships.push(Relationship {
rel_type: "OWNS".into(),
target: EntityId::new("specs", "victim"),
description: None,
});
let mut victim = make_entity("victim", true);
victim.entity_type = "spec".into();
store.upsert(bad.id.clone(), bad);
store.upsert(victim.id.clone(), victim);
let mut mem_schemas = HashMap::new();
mem_schemas.insert("specs".to_string(), software);
let schema = &type_by_name("spec").unwrap();
let summary = compute_health(&store, schema, &mem_schemas);
let report = summary
.missing_fields
.iter()
.find(|r| r.id.as_ref() == "specs--bad-owns-source")
.expect("shape-violating entity must surface");
let issue = report
.issues
.iter()
.find(|i| i.field == "relationships" && i.message.contains("INVALID_REL_SHAPE"))
.expect("shape violation must produce an INVALID_REL_SHAPE issue");
assert!(
issue.message.contains("OWNS"),
"issue must name the offending rel_type: {}",
issue.message
);
assert!(
issue.message.contains("spec"),
"issue must name the actual source type: {}",
issue.message
);
assert!(
issue.message.contains("actor"),
"issue must name the allowed source type: {}",
issue.message
);
assert!(
issue.message.contains("remove=true"),
"issue must surface the recovery path: {}",
issue.message
);
}
#[test]
fn health_does_not_flag_shape_compliant_edges() {
use crate::entity::Relationship;
use memstead_schema::SchemaRegistry;
let registry = SchemaRegistry::builtin();
let software = registry
.get("software", &semver::Version::new(0, 2, 0))
.expect("software schema ships as a builtin");
let mut store = Store::new();
let mut owner = make_entity("owner", true);
owner.entity_type = "actor".into();
owner
.metadata
.insert("kind".into(), MetadataValue::String("team".into()));
owner
.metadata
.insert("active".into(), MetadataValue::Bool(true));
owner
.metadata
.insert("handle".into(), MetadataValue::String("owner".into()));
owner.relationships.push(Relationship {
rel_type: "OWNS".into(),
target: EntityId::new("specs", "owned"),
description: None,
});
let mut owned = make_entity("owned", true);
owned.entity_type = "spec".into();
store.upsert(owner.id.clone(), owner);
store.upsert(owned.id.clone(), owned);
let mut mem_schemas = HashMap::new();
mem_schemas.insert("specs".to_string(), software);
let schema = &type_by_name("spec").unwrap();
let summary = compute_health(&store, schema, &mem_schemas);
let shape_issue = summary
.missing_fields
.iter()
.flat_map(|r| r.issues.iter())
.find(|i| i.message.contains("INVALID_REL_SHAPE"));
assert!(
shape_issue.is_none(),
"shape-compliant edge must not surface a shape issue, got: {shape_issue:?}"
);
}
#[test]
fn health_warns_on_undeclared_relationship_in_existing_entity() {
use crate::entity::Relationship;
use memstead_schema::Schema;
let mut store = Store::new();
let mut entity = make_entity("with-bad-rel", true);
entity.relationships.push(Relationship {
rel_type: "CONJURES".into(),
target: EntityId::new("specs", "unknown"),
description: None,
});
store.upsert(entity.id.clone(), entity);
let mut mem_schemas = HashMap::new();
mem_schemas.insert("specs".to_string(), Schema::builtin_default());
let schema = &type_by_name("spec").unwrap();
let summary = compute_health(&store, schema, &mem_schemas);
let report = summary
.missing_fields
.iter()
.find(|r| r.id.as_ref() == "specs--with-bad-rel")
.expect("entity must surface in missing_fields");
let rel_issue = report
.issues
.iter()
.find(|i| i.field == "relationships")
.expect("undeclared relationship must produce an issue");
assert!(
rel_issue.message.contains("CONJURES"),
"issue message must name the offending relationship: {}",
rel_issue.message
);
assert!(
rel_issue.message.contains("default@1.0.0"),
"issue must name the schema pin: {}",
rel_issue.message
);
}
fn make_entity_with_body(name: &str, section_key: &str, body: &str) -> Entity {
let mut entity = make_entity(name, true);
entity.sections.insert(section_key.into(), body.to_string());
entity
}
#[test]
fn dangling_link_detected_after_delete() {
use crate::entity::store_builder::make_stub;
let mut store = Store::new();
let a = make_entity_with_body("a", "purpose", "Refers to [[b]] in prose.");
store.upsert(a.id.clone(), a.clone());
let b_id = EntityId::new("specs", "b");
store.upsert(b_id.clone(), make_stub(b_id.clone()));
let dangling = super::collect_dangling_links(&store, None);
assert_eq!(dangling.len(), 1, "exactly one dangling link expected");
let d = &dangling[0];
assert_eq!(d.from, a.id);
assert_eq!(d.target_id, b_id);
assert_eq!(d.target_path, "b");
assert_eq!(d.section.as_deref(), Some("purpose"));
}
#[test]
fn dangling_link_does_not_flag_stub_target_of_explicit_relationship() {
use crate::entity::Relationship;
use crate::entity::store_builder::make_stub;
let mut store = Store::new();
let mut a = make_entity("a", true);
let b_id = EntityId::new("specs", "b");
a.relationships.push(Relationship {
rel_type: "REFERENCES".into(),
target: b_id.clone(),
description: None,
});
store.upsert(a.id.clone(), a);
store.upsert(b_id.clone(), make_stub(b_id));
let dangling = super::collect_dangling_links(&store, None);
assert!(
dangling.is_empty(),
"explicit relationships to stubs are valid by design \
(stubs are first-class placeholders); only inline-body \
wiki-links to stubs must surface"
);
}
#[test]
fn dangling_link_does_not_flag_real_reference() {
use crate::entity::Relationship;
let mut store = Store::new();
let mut a = make_entity_with_body("a", "purpose", "Refers to [[b]] in prose.");
a.relationships.push(Relationship {
rel_type: "REFERENCES".into(),
target: EntityId::new("specs", "b"),
description: None,
});
let b = make_entity("b", true);
store.upsert(a.id.clone(), a);
store.upsert(b.id.clone(), b);
let dangling = super::collect_dangling_links(&store, None);
assert!(
dangling.is_empty(),
"real reference backed by relation — not dangling, not alias-orphan"
);
}
#[test]
fn dangling_link_relationship_section_target_absent() {
use crate::entity::Relationship;
let mut store = Store::new();
let mut a = make_entity("a", true);
a.relationships.push(Relationship {
rel_type: "DEPENDS_ON".into(),
target: EntityId::new("specs", "gone"),
description: None,
});
store.upsert(a.id.clone(), a.clone());
let dangling = super::collect_dangling_links(&store, None);
assert_eq!(
dangling.len(),
1,
"exactly one relationship-section dangler"
);
let d = &dangling[0];
assert_eq!(d.from, a.id);
assert_eq!(d.target_id, EntityId::new("specs", "gone"));
assert!(
d.section.is_none(),
"relationship-section danglers ship `section: None`, got {:?}",
d.section
);
}
#[test]
fn dangling_link_relationship_section_stub_target_not_flagged() {
use crate::entity::Relationship;
use crate::entity::store_builder::make_stub;
let mut store = Store::new();
let mut a = make_entity("a", true);
let b_id = EntityId::new("specs", "b");
a.relationships.push(Relationship {
rel_type: "DEPENDS_ON".into(),
target: b_id.clone(),
description: None,
});
store.upsert(a.id.clone(), a);
store.upsert(b_id.clone(), make_stub(b_id));
let dangling = super::collect_dangling_links(&store, None);
assert!(
dangling.is_empty(),
"relationship targets that resolve to stubs are forward-references, not corruption"
);
}
#[test]
fn dangling_link_dedups_across_body_and_relations() {
use crate::entity::Relationship;
use crate::entity::store_builder::make_stub;
let mut store = Store::new();
let mut a = make_entity_with_body("a", "purpose", "Refers to [[b]] in prose.");
let b_id = EntityId::new("specs", "b");
a.relationships.push(Relationship {
rel_type: "REFERENCES".into(),
target: b_id.clone(),
description: None,
});
store.upsert(a.id.clone(), a.clone());
store.upsert(b_id.clone(), make_stub(b_id.clone()));
let dangling = super::collect_dangling_links(&store, None);
assert_eq!(
dangling.len(),
1,
"body + relations both pointing at the same stub should dedup"
);
assert!(dangling[0].section.is_some(), "body axis wins the dedup");
}
#[test]
fn dangling_links_scope_to_mem_filter() {
use crate::entity::store_builder::make_stub;
let mut store = Store::new();
let a = make_entity_with_body("a", "purpose", "Refers to [[gone]] in prose.");
store.upsert(a.id.clone(), a);
let gone_specs = EntityId::new("specs", "gone");
store.upsert(gone_specs.clone(), make_stub(gone_specs));
let mut x = make_entity("x", true);
x.id = EntityId::new("web", "x");
x.mem = "web".into();
x.file_path = "x.md".into();
x.sections
.insert("purpose".into(), "Refers to [[gone]] in prose.".into());
store.upsert(x.id.clone(), x);
let gone_web = EntityId::new("web", "gone");
store.upsert(gone_web.clone(), make_stub(gone_web));
let all = super::collect_dangling_links(&store, None);
assert_eq!(all.len(), 2);
let specs_only = super::collect_dangling_links(&store, Some("specs"));
assert_eq!(specs_only.len(), 1);
assert_eq!(specs_only[0].from.mem(), "specs");
let web_only = super::collect_dangling_links(&store, Some("web"));
assert_eq!(web_only.len(), 1);
assert_eq!(web_only[0].from.mem(), "web");
}
#[test]
fn parse_iso_date() {
let days = parse_iso_to_days("2026-04-12").unwrap();
assert!(days > 0);
let days_with_time = parse_iso_to_days("2026-04-12T10:00:00Z").unwrap();
assert_eq!(days, days_with_time);
}
#[test]
fn ymd_roundtrip() {
let days = ymd_to_days(2026, 1, 1);
assert!(days > 20000); }
fn make_entity_with_tags(name: &str, mem: &str, entity_type: &str, tags: &str) -> Entity {
let mut e = make_entity(name, true);
e.id = EntityId::new(mem, name);
e.mem = mem.into();
e.entity_type = entity_type.into();
e.metadata
.insert("tags".into(), MetadataValue::String(tags.into()));
e
}
fn make_entity_no_tags(name: &str) -> Entity {
make_entity(name, true)
}
#[test]
fn tag_distribution_aggregates_across_entities() {
let mut store = Store::new();
let a = make_entity_with_tags("a", "specs", "spec", "decision, plan");
let b = make_entity_with_tags("b", "specs", "spec", "decision, plan");
let c = make_entity_with_tags("c", "specs", "spec", "plan");
store.upsert(a.id.clone(), a);
store.upsert(b.id.clone(), b);
store.upsert(c.id.clone(), c);
let (dist, _folded, untagged) = collect_tag_distribution(&store, None, 10);
assert_eq!(dist.len(), 2);
assert_eq!(dist[0].tag, "plan");
assert_eq!(dist[0].count, 3);
assert_eq!(dist[0].by_entity_type.get("spec"), Some(&3));
assert_eq!(dist[1].tag, "decision");
assert_eq!(dist[1].count, 2);
assert_eq!(untagged.total, 0);
}
#[test]
fn tag_distribution_case_sensitive() {
let mut store = Store::new();
let a = make_entity_with_tags("a", "specs", "spec", "Decision");
let b = make_entity_with_tags("b", "specs", "spec", "decision");
store.upsert(a.id.clone(), a);
store.upsert(b.id.clone(), b);
let (dist, folded, _untagged) = collect_tag_distribution(&store, None, 10);
assert_eq!(dist.len(), 2, "`decision` and `Decision` stay distinct");
let tags: std::collections::HashSet<&str> = dist.iter().map(|t| t.tag.as_str()).collect();
assert!(tags.contains("decision"));
assert!(tags.contains("Decision"));
assert_eq!(folded.len(), 1);
assert_eq!(folded[0].canonical, "decision");
assert_eq!(folded[0].total, 2);
assert_eq!(folded[0].variants.len(), 2);
}
#[test]
fn untagged_entities_counts_missing_and_empty() {
let mut store = Store::new();
let a = make_entity_no_tags("a"); let b = make_entity_with_tags("b", "specs", "spec", "");
let c = make_entity_with_tags("c", "specs", "spec", " , , ");
store.upsert(a.id.clone(), a);
store.upsert(b.id.clone(), b);
store.upsert(c.id.clone(), c);
let (dist, _folded, untagged) = collect_tag_distribution(&store, None, 10);
assert!(dist.is_empty(), "no effective tags → empty distribution");
assert_eq!(untagged.total, 3);
assert_eq!(untagged.by_entity_type.get("spec"), Some(&3));
}
#[test]
fn tag_distribution_respects_mem_filter() {
let mut store = Store::new();
let a = make_entity_with_tags("a", "specs", "spec", "decision");
let b = make_entity_with_tags("b", "memos", "memo", "observation");
let c = make_entity_no_tags("c");
store.upsert(a.id.clone(), a);
store.upsert(b.id.clone(), b);
store.upsert(c.id.clone(), c);
let (dist, _folded, untagged) = collect_tag_distribution(&store, Some("memos"), 10);
assert_eq!(dist.len(), 1);
assert_eq!(dist[0].tag, "observation");
assert_eq!(untagged.total, 0, "untagged scoped to filter mem");
}
#[test]
fn tag_distribution_respects_limit() {
let mut store = Store::new();
for (name, tag) in [
("a", "t-alpha"),
("b", "t-beta"),
("c", "t-gamma"),
("d", "t-delta"),
("e", "t-epsilon"),
] {
let e = make_entity_with_tags(name, "specs", "spec", tag);
store.upsert(e.id.clone(), e);
}
let (dist, _folded, _untagged) = collect_tag_distribution(&store, None, 3);
assert_eq!(dist.len(), 3);
assert_eq!(dist[0].tag, "t-alpha");
assert_eq!(dist[1].tag, "t-beta");
assert_eq!(dist[2].tag, "t-delta");
}
fn required_outgoing_fixture_schema() -> std::sync::Arc<memstead_schema::Schema> {
let manifest = r#"name: tests-ro-health
version: 0.1.0
description: required_outgoing health test schema
when_to_use: tests
types:
- decision
- note
relationships:
mode: strict
definitions:
- name: PART_OF
description: Hier
default_weight: 3.0
acyclic: true
- name: CHOSEN
description: ch
default_weight: 3.0
- name: REJECTED
description: rj
default_weight: 2.0
- name: REFERENCES
description: ref
default_weight: 0.5
- name: _default
description: Fallback
default_weight: 1.0
community:
resolution: 1.0
seed: 42
"#;
let body_section = "sections:\n - key: body\n heading: Body\n required: true\n search_weight: 10.0\n catch_all: true\n write_rules: []\nmetadata_fields: []\ntitle_weight: 100.0\ntext_fields:\n - body\nhierarchy_relationship: PART_OF\nno_self_loop_relationships: []\nupdatable_fields:\n - title\n - body\nhealth_required_fields:\n - body\nstaleness_threshold_days: 90\nwrite_rules: []\n";
let decision_yaml = format!(
"name: decision\ndescription: t\nwhen_to_use: Here\n{body_section}required_outgoing:\n - relationships: [CHOSEN]\n cardinality: at_least_one\n - relationships: [REJECTED]\n cardinality: at_least_one\n",
);
let note_yaml = format!("name: note\ndescription: t\nwhen_to_use: Here\n{body_section}",);
std::sync::Arc::new(
memstead_schema::load_schema_from_memory(
manifest,
&[
("decision".to_string(), decision_yaml),
("note".to_string(), note_yaml),
],
)
.expect("ro fixture schema must parse"),
)
}
fn make_typed_entity(mem: &str, slug: &str, entity_type: &str) -> crate::entity::Entity {
use crate::entity::MetadataValue;
let mut metadata = IndexMap::new();
metadata.insert("type".into(), MetadataValue::String(entity_type.into()));
let mut sections = IndexMap::new();
sections.insert("body".into(), "Body.".into());
crate::entity::Entity {
id: EntityId::new(mem, slug),
title: slug.to_string(),
entity_type: entity_type.into(),
mem: mem.into(),
file_path: format!("{slug}.md"),
metadata,
sections,
relationships: Vec::new(),
content_hash: String::new(),
stub: false,
stub_kind: None,
heading_spans: std::collections::HashMap::new(),
raw_section_headings: Vec::new(),
}
}
#[test]
fn missing_required_outgoing_collects_violators_only() {
let schema = required_outgoing_fixture_schema();
let mut store = Store::new();
let mut violator = make_typed_entity("plan", "stalled", "decision");
let mut satisfied = make_typed_entity("plan", "wired", "decision");
let opt_a = make_typed_entity("plan", "a", "note");
let opt_b = make_typed_entity("plan", "b", "note");
let happy_note = make_typed_entity("plan", "side", "note");
satisfied.relationships.push(crate::entity::Relationship {
rel_type: "CHOSEN".into(),
target: opt_a.id.clone(),
description: None,
});
satisfied.relationships.push(crate::entity::Relationship {
rel_type: "REJECTED".into(),
target: opt_b.id.clone(),
description: None,
});
for e in [violator.clone(), satisfied, opt_a, opt_b, happy_note] {
store.upsert(e.id.clone(), e);
}
let mut mem_schemas = HashMap::new();
mem_schemas.insert("plan".to_string(), schema);
let reports = collect_missing_required_outgoing(&store, None, &mem_schemas);
assert_eq!(
reports.len(),
1,
"exactly one violator (the empty decision); got {reports:?}"
);
let r = &reports[0];
assert_eq!(r.id, violator.id);
assert_eq!(r.entity_type, "decision");
assert_eq!(r.mem, "plan");
assert_eq!(r.missing.len(), 2);
let names: Vec<&str> = r
.missing
.iter()
.flat_map(|b| b.relationships.iter().map(String::as_str))
.collect();
assert!(names.contains(&"CHOSEN"));
assert!(names.contains(&"REJECTED"));
violator.relationships.push(crate::entity::Relationship {
rel_type: "CHOSEN".into(),
target: EntityId::new("plan", "x"),
description: None,
});
}
#[test]
fn missing_required_outgoing_respects_mem_filter() {
let schema = required_outgoing_fixture_schema();
let mut store = Store::new();
let v_a = make_typed_entity("alpha", "stalled", "decision");
let v_b = make_typed_entity("beta", "stalled", "decision");
store.upsert(v_a.id.clone(), v_a);
store.upsert(v_b.id.clone(), v_b.clone());
let mut mem_schemas = HashMap::new();
mem_schemas.insert("alpha".to_string(), schema.clone());
mem_schemas.insert("beta".to_string(), schema);
let alpha_only = collect_missing_required_outgoing(&store, Some("alpha"), &mem_schemas);
assert_eq!(alpha_only.len(), 1);
assert_eq!(alpha_only[0].mem, "alpha");
let both = collect_missing_required_outgoing(&store, None, &mem_schemas);
assert_eq!(both.len(), 2);
}
#[test]
fn missing_required_outgoing_skips_stubs_and_unschemaed_mems() {
let schema = required_outgoing_fixture_schema();
let mut store = Store::new();
let mut stub = make_typed_entity("plan", "ghost", "");
stub.stub = true;
stub.entity_type = String::new();
let other = make_typed_entity("uncharted", "lonely", "decision");
store.upsert(stub.id.clone(), stub);
store.upsert(other.id.clone(), other);
let mut mem_schemas = HashMap::new();
mem_schemas.insert("plan".to_string(), schema);
let reports = collect_missing_required_outgoing(&store, None, &mem_schemas);
assert!(
reports.is_empty(),
"stub (no schema lookup) and unschemaed mem must be skipped; got {reports:?}",
);
}
}