memstead_base/entity/store_builder.rs
1//! Shared helper for turning `ParseResult`s into a populated `Store`.
2//!
3//! The runtime engine calls this during `Engine::init` + `reload` and
4//! `attach_read_mem`. The strict validator calls it during V1 graph
5//! construction. Having one implementation guarantees both paths use
6//! identical stub + edge semantics.
7
8use indexmap::IndexMap;
9use memstead_schema::TypeDefinition;
10
11use super::parser::extract_inline_links_lenient;
12use super::{Entity, EntityId, ParseResult};
13use crate::ops::WarningHint;
14use crate::store::{Edge, EdgeSource, Store};
15
16/// Context passed to `push_entities_into_store` for load-time drift
17/// detection. Load-path call sites pass `Some(LoadCollector { .. })` so
18/// authored nested-prefix wiki-links (the classic mem-rename drift
19/// footprint) emit a `SuspiciousNestedPrefix` warning — mutation-path
20/// call sites pass `None` to stay silent (an author editing an entity
21/// that still has a drifted link should not see the warning refire on
22/// every save; load already caught it).
23pub struct LoadCollector<'a> {
24 /// Target for emitted warnings — typically `&mut engine.load_warnings`.
25 pub warnings: &'a mut Vec<WarningHint>,
26 /// Known-mem last-segment suffixes, derived from the mem roster
27 /// (e.g. `test-mem-plugin` → `plugin`). A nested-prefix link
28 /// is detected when a target id has the shape
29 /// `<current-mem>--<suffix>--<rest>` where `<suffix>` is in this
30 /// set and `<suffix>` is not the entity's own mem's last segment.
31 pub known_suffixes: &'a [String],
32 /// Full mem-name roster (writable + read mems). Used by the
33 /// two-pass candidate resolver to probe cross-mem matches.
34 pub mem_names: &'a [String],
35}
36
37/// Upsert parse results into the store, adding explicit relationship
38/// edges and auto-stubbing any unknown targets. Body wiki-links are
39/// not edge sources — under the alias model every edge originates
40/// from the auto-managed `## Relationships` section.
41///
42/// The fallback schema parameter is retained for call-site compatibility
43/// but no longer consulted for edge emission.
44///
45/// `load_ctx` is `Some` at load/reload/attach sites (drift-warning
46/// emission enabled) and `None` at mutation/validator sites (silent —
47/// warnings fire once at load, not on every edit).
48pub fn push_entities_into_store(
49 store: &mut Store,
50 parse_results: Vec<ParseResult>,
51 _fallback_schema: &TypeDefinition,
52 mut load_ctx: Option<LoadCollector<'_>>,
53) {
54 // Stash id + mem + sections per entity for a post-upsert drift
55 // scan. We can't scan before upsert: the two-pass resolver needs
56 // ALL entities in the batch to be present so a bare-slug fallback
57 // can find intra-batch targets regardless of filesystem iteration
58 // order (e.g. `drifted.md` loaded before its `foo.md` sibling).
59 let mut drift_scan_inputs: Vec<(EntityId, String, IndexMap<String, String>)> = Vec::new();
60
61 for parse_result in parse_results {
62 let entity_id = parse_result.entity.id.clone();
63 let entity_mem = parse_result.entity.mem.clone();
64
65 // Surface parse-time warnings (e.g. duplicate section headings) at
66 // load / reload / attach sites. Mutation paths build their own
67 // `ParseResult`s without `load_ctx` and ignore these.
68 if let Some(ctx) = load_ctx.as_mut()
69 && !parse_result.parse_warnings.is_empty()
70 {
71 ctx.warnings
72 .extend(parse_result.parse_warnings.iter().cloned());
73 }
74
75 if load_ctx.is_some() {
76 drift_scan_inputs.push((
77 entity_id.clone(),
78 entity_mem,
79 parse_result.entity.sections.clone(),
80 ));
81 }
82
83 // Clear pre-existing out-edges before upserting so the store
84 // reflects exactly the new entity's relationships. Without this,
85 // a mutation that drops a relation leaks the stale edge:
86 // `add_edge` is idempotent on (from, to, rel_type), so it never
87 // removes edges that the post-parse pass no longer emits.
88 store.remove_edges_from(&entity_id);
89
90 store.upsert(entity_id.clone(), parse_result.entity);
91
92 let relationships: Vec<_> = store
93 .get(&entity_id)
94 .map(|e| e.relationships.clone())
95 .unwrap_or_default();
96 for rel in &relationships {
97 if !store.contains(&rel.target) {
98 store.upsert(rel.target.clone(), make_stub(rel.target.clone()));
99 }
100 store.add_edge(
101 entity_id.clone(),
102 Edge {
103 rel_type: rel.rel_type.clone(),
104 target: rel.target.clone(),
105 source: EdgeSource::Explicit,
106 },
107 );
108 }
109 }
110
111 // Post-upsert drift scan — every batch entity is now in the store,
112 // so pass-2 (same-mem bare-slug) finds intra-batch targets too.
113 if let Some(ctx) = load_ctx.as_mut() {
114 for (id, mem, sections) in &drift_scan_inputs {
115 scan_nested_prefix_drift(id, mem, sections, ctx, store);
116 }
117 }
118}
119
120/// Re-add edges that point INTO `reloaded_mem` from entities living in
121/// other mems, after a per-mem reload of `reloaded_mem`.
122///
123/// The per-mem removal cascade ([`Store::remove`] via
124/// [`Store::remove_entities_by_mem`]) drops every incoming mirror of the
125/// reloaded mem's nodes — including cross-mem edges sourced from an
126/// un-reloaded mem — and the re-push ([`push_entities_into_store`]) only
127/// rebuilds edges authored by the reloaded mem's own entities. So a
128/// cross-mem edge `A→B` (A in another mem) survives in A's record and
129/// on disk but vanishes from the in-memory adjacency until a workspace-wide
130/// reload rebuilds A's side. This pass restores it from the authoritative
131/// source records, so a per-mem reload of B and a workspace-wide reload
132/// converge to the same incoming adjacency for B.
133///
134/// Mirrors `push_entities_into_store`'s edge construction exactly: auto-stub
135/// a missing target and add the edge as `EdgeSource::Explicit`. A following
136/// [`remap_alias_target_edge_sources`] reclassifies alias-derived sources
137/// (the same post-pass the reload already runs over the re-pushed mem),
138/// so an alias/body-link cross-mem edge keeps its `BodyLink` source. The
139/// scan is over in-memory records only — it never re-reads or re-parses
140/// another mem's backend, preserving the cheap-per-mem-reload property.
141pub fn reconstruct_incoming_cross_mem_edges(store: &mut Store, reloaded_mem: &str) {
142 let mut to_add: Vec<(EntityId, Edge)> = Vec::new();
143 for entity in store.all_entities() {
144 if entity.mem == reloaded_mem {
145 continue;
146 }
147 for rel in &entity.relationships {
148 if rel.target.mem() == reloaded_mem {
149 to_add.push((
150 entity.id.clone(),
151 Edge {
152 rel_type: rel.rel_type.clone(),
153 target: rel.target.clone(),
154 source: EdgeSource::Explicit,
155 },
156 ));
157 }
158 }
159 }
160 for (from, edge) in to_add {
161 if !store.contains(&edge.target) {
162 store.upsert(edge.target.clone(), make_stub(edge.target.clone()));
163 }
164 store.add_edge(from, edge);
165 }
166}
167
168/// Extract the last `-`-separated segment of a mem name (e.g.
169/// `test-mem-plugin` → `plugin`). Used to derive the
170/// known-mem-suffix set from the roster.
171pub fn last_segment_suffix(mem_name: &str) -> &str {
172 mem_name.rsplit('-').next().unwrap_or(mem_name)
173}
174
175/// Scan an entity's section bodies for wiki-links whose mem prefix
176/// matches a known mem last-segment but is NOT the full mem name —
177/// i.e. the author wrote the short-form (`[[plugin--foo]]`) instead of
178/// the bare-slug form (same-mem target) or the canonical
179/// fully-qualified form. Each hit produces a `SuspiciousNestedPrefix`
180/// warning with a two-pass resolved candidate.
181///
182/// Tier-0 `<mem>--<slug>` recognition resolves the body link to the
183/// named mem directly. A known short-name being used where a bare
184/// slug or a fully-qualified id was intended is the canonical drift
185/// pattern; the detector matches on the resolved target's mem.
186/// Runs before the entity is upserted so the candidate probe reflects
187/// the store state *before* this entity's own auto-stub would mask a
188/// real intra-mem match.
189fn scan_nested_prefix_drift(
190 from: &EntityId,
191 current_mem: &str,
192 sections: &IndexMap<String, String>,
193 ctx: &mut LoadCollector<'_>,
194 store: &Store,
195) {
196 for (section, body) in sections {
197 // Reuse the same extractor DanglingLink uses so semantics stay
198 // aligned (code-block masking, inline-code skipping, alias handling).
199 for target_id in extract_inline_links_lenient(body, current_mem) {
200 let target_mem = target_id.mem();
201 // Skip when the body link resolves into the current mem —
202 // bare-slug authoring is the canonical same-mem form, no
203 // drift to surface.
204 if target_mem == current_mem {
205 continue;
206 }
207 // A colon/dash link whose target mem is itself a full
208 // roster member AND whose target entity actually exists is a
209 // legitimate cross-mem reference, not drift: pass-1 of the
210 // two-pass resolver would just rediscover the same id, so the
211 // warning's "did you mean" candidate equals the already-
212 // resolved target — a self-contradicting false positive (the
213 // macos→engine case). Skip it. A real-mem target whose
214 // entity is *missing* is left to fire (it may be genuine
215 // rename-drift where a suffix-sibling mem holds the real
216 // entity — see `suffix_collision_resolves_first_match`).
217 if ctx.mem_names.iter().any(|v| v.as_str() == target_mem)
218 && store.get(&target_id).is_some_and(|e| !e.stub)
219 {
220 continue;
221 }
222 // Fire when the target's mem matches a known last-segment
223 // suffix of some mem in the roster. Self-suffix is NOT
224 // excluded — `[[plugin--x]]` inside `test-mem-plugin`
225 // (suffix `plugin`, with no `plugin` mem) remains the
226 // empirically-dominant drift pattern.
227 for suffix in ctx.known_suffixes.iter() {
228 if target_mem == suffix {
229 let candidate_target =
230 resolve_two_pass(target_id.path(), current_mem, ctx.mem_names, store);
231 ctx.warnings.push(WarningHint::SuspiciousNestedPrefix {
232 from: from.clone(),
233 resolved_id: target_id.clone(),
234 candidate_target,
235 section: section.clone(),
236 });
237 break;
238 }
239 }
240 }
241 }
242}
243
244/// Two-pass resolver for a stripped slug (the `<rest>` part of a
245/// nested-prefix drift hit).
246///
247/// Pass 1 (cross-mem-first): probe `<V>--<rest>` against every
248/// non-current mem in the roster. If exactly one match resolves to a
249/// real entity, the author probably meant that cross-mem entity.
250///
251/// Pass 2 (same-mem bare-slug): if no unique cross-mem match,
252/// probe `<current_mem>--<rest>`. If that resolves to a real entity,
253/// the author probably meant the bare slug form in the current mem.
254///
255/// Returns `None` on zero hits, multiple cross-mem hits (ambiguous),
256/// or when the matched candidate is a stub. Callers surface the
257/// `None` case so the author can disambiguate by hand — the warning
258/// still fires.
259fn resolve_two_pass(
260 rest: &str,
261 current_mem: &str,
262 mem_names: &[String],
263 store: &Store,
264) -> Option<EntityId> {
265 let mut hits: Vec<EntityId> = Vec::new();
266 for mem in mem_names {
267 if mem == current_mem {
268 continue;
269 }
270 let candidate = EntityId::new(mem, rest);
271 if let Some(e) = store.get(&candidate)
272 && !e.stub
273 {
274 hits.push(candidate);
275 }
276 }
277 match hits.len() {
278 1 => hits.pop(),
279 0 => {
280 // Pass 2: same-mem bare-slug fallback.
281 let candidate = EntityId::new(current_mem, rest);
282 if let Some(e) = store.get(&candidate)
283 && !e.stub
284 {
285 Some(candidate)
286 } else {
287 None
288 }
289 }
290 _ => None, // ambiguous cross-mem match
291 }
292}
293
294/// Validate every loaded entity's `## Relationships` entries against
295/// the source mem's schema and the wiki-link grammar. Invalid
296/// relations are dropped from both the store's edge index and the
297/// entity's in-memory `relationships` list; each drop emits a
298/// `PARSED_RELATION_INVALID` warning naming the offending entity,
299/// rel-type, target, and reason.
300///
301/// Four reasons fire today:
302/// - `grammar` — the target id's path does not match the wiki-link
303/// grammar (`^[a-z0-9-]+(/[a-z0-9-]+)*$`).
304/// - `unknown_rel_type` — the rel-type is not declared in the mem's
305/// schema and the schema is in `strict` mode. Open-mode schemas
306/// admit the relation without a warning (mirrors the mutation
307/// surface).
308/// - `shape` — the `(source_type, target_type)` pair is not allowed
309/// by the declared `source_types` / `target_types`. `target_type`
310/// is looked up from the store post-load, so the check sees the
311/// real type for any target — including cross-mem targets
312/// loaded from another mount. Stub targets (no `entity_type`) skip
313/// the target-side check; the relation lands and the shape will be
314/// re-verified when the stub is promoted to a real entity.
315/// - `cycle` — the relation closes a cycle in an `acyclic: true`
316/// rel-type's subgraph. Emitted by the second pass after grammar /
317/// rel-type / shape drops; the two-pass structure runs cycle
318/// detection after the initial relation-load so loading order
319/// doesn't determine which edge
320/// gets blamed. Each cycle drops exactly one back-edge per DFS
321/// visit; multiple independent cycles each lose one edge.
322///
323/// Runs once at boot after every mount's entities are pushed into the
324/// store. Mutation paths do not call this — they pre-validate via
325/// `validate_rel_type` + `validate_rel_shape` before the write and
326/// the existing same-call `would_cycle` check guards acyclic adds.
327pub fn validate_loaded_relations(
328 store: &mut Store,
329 schemas: &std::collections::HashMap<String, std::sync::Arc<memstead_schema::Schema>>,
330 mount_caps: &std::collections::HashMap<String, crate::workspace::MountCapability>,
331 warnings: &mut Vec<WarningHint>,
332) {
333 use crate::entity::Relationship;
334 use crate::entity::id::validate_id_path_grammar;
335 use crate::runtime_validator::{
336 CrossMemRelCheck, validate_cross_mem_edge, validate_rel_shape, validate_rel_type,
337 };
338 use crate::workspace::MountCapability;
339 use memstead_schema::SchemaRef;
340
341 let origin_for = |mem: &str| -> &'static str {
342 match mount_caps.get(mem) {
343 Some(MountCapability::ReadOnly) => "readonly",
344 _ => "writable",
345 }
346 };
347
348 // Pass 1: schema-shape + grammar + rel-type-known drops.
349 let mut to_drop: Vec<(EntityId, Relationship, &'static str)> = Vec::new();
350 for entity in store.all_entities() {
351 if entity.stub {
352 continue;
353 }
354 let Some(schema) = schemas.get(entity.mem.as_str()) else {
355 continue;
356 };
357 for rel in &entity.relationships {
358 if validate_id_path_grammar(rel.target.path()).is_err() {
359 to_drop.push((entity.id.clone(), rel.clone(), "grammar"));
360 continue;
361 }
362 // Cross-mem-different edges validate against the
363 // source schema's `cross_mem_relationships:` section,
364 // not its intra-mem `relationships.definitions`. Same-
365 // schema cross-mem and same-mem fall through to the
366 // intra-mem path — matching the runtime relate flow's
367 // routing rule.
368 let target_mem = rel.target.mem();
369 let target_schema = if entity.mem.as_str() == target_mem {
370 None
371 } else {
372 schemas.get(target_mem).cloned()
373 };
374 let target_schema_ref: Option<SchemaRef> = target_schema.as_ref().map(|s| {
375 let (name, version) = s.id();
376 SchemaRef::new(name, version)
377 });
378 let cross_mem_different = match (&target_schema_ref, schema.id()) {
379 (Some(target), (src_name, _)) => target.name != src_name,
380 (None, _) => false,
381 };
382 let target_type = store
383 .get(&rel.target)
384 .map(|e| e.entity_type.clone())
385 .filter(|t| !t.is_empty());
386 if cross_mem_different {
387 let target_ref = target_schema_ref.as_ref().expect("present when different");
388 match validate_cross_mem_edge(
389 &rel.rel_type,
390 entity.entity_type.as_str(),
391 target_type.as_deref(),
392 schema.as_ref(),
393 target_ref,
394 ) {
395 CrossMemRelCheck::Ok => {}
396 CrossMemRelCheck::EdgeNotDeclared => {
397 to_drop.push((entity.id.clone(), rel.clone(), "cross_mem_not_declared"));
398 continue;
399 }
400 CrossMemRelCheck::Invalid(_) => {
401 // Same drop semantics as the intra-mem
402 // shape/vocabulary branch — boot is silent
403 // best-effort cleanup.
404 to_drop.push((entity.id.clone(), rel.clone(), "cross_mem_shape"));
405 continue;
406 }
407 }
408 } else {
409 if validate_rel_type(&rel.rel_type, schema.as_ref()).is_err() {
410 to_drop.push((entity.id.clone(), rel.clone(), "unknown_rel_type"));
411 continue;
412 }
413 if validate_rel_shape(
414 &rel.rel_type,
415 entity.entity_type.as_str(),
416 target_type.as_deref(),
417 schema.as_ref(),
418 )
419 .is_err()
420 {
421 to_drop.push((entity.id.clone(), rel.clone(), "shape"));
422 continue;
423 }
424 }
425 }
426 }
427 for (from_id, rel, reason) in to_drop {
428 let origin = origin_for(from_id.mem()).to_string();
429 store.remove_edge(&from_id, &rel.target, &rel.rel_type);
430 if let Some(entity) = store.get_mut(&from_id) {
431 entity
432 .relationships
433 .retain(|r| !(r.rel_type == rel.rel_type && r.target == rel.target));
434 }
435 let recovery = if origin == "writable" {
436 Some(
437 crate::ops::ParsedRelationRecovery::remove_explicit_relation(
438 from_id.clone(),
439 rel.target.clone(),
440 rel.rel_type.clone(),
441 ),
442 )
443 } else {
444 None
445 };
446 warnings.push(WarningHint::ParsedRelationInvalid {
447 entity_id: from_id,
448 rel_type: rel.rel_type,
449 target: rel.target,
450 reason: reason.to_string(),
451 origin,
452 recovery,
453 });
454 }
455
456 // Pass 1b: per-edge description posture against the rel-type's
457 // schema declaration. Forbidden + description present → drop the
458 // description in-memory and warn; the next render normalises the
459 // row to the simple form. Required + description absent → warn
460 // and leave the relation intact; the operator's follow-up
461 // mutation (or a hand-edit using the em-dash delimiter) supplies
462 // the text. Runs after the shape drops so the surviving
463 // relationships have known-valid rel-types in this schema.
464 {
465 use memstead_schema::PerEdgeDescription;
466 let mut posture_warnings: Vec<WarningHint> = Vec::new();
467 let mut to_strip_description: Vec<(EntityId, String, EntityId)> = Vec::new();
468 for entity in store.all_entities() {
469 if entity.stub {
470 continue;
471 }
472 let Some(schema) = schemas.get(entity.mem.as_str()) else {
473 continue;
474 };
475 for rel in &entity.relationships {
476 // Look up the posture in the routing-appropriate
477 // definition. Cross-mem-different routes through
478 // the source schema's cross_mem_relationships entry
479 // for the target schema; intra-mem and same-schema
480 // cross-mem fall through to the intra-mem
481 // relationships.definitions.
482 let target_mem = rel.target.mem();
483 let target_schema = if entity.mem.as_str() == target_mem {
484 None
485 } else {
486 schemas.get(target_mem).cloned()
487 };
488 let target_schema_ref: Option<SchemaRef> = target_schema.as_ref().map(|s| {
489 let (name, version) = s.id();
490 SchemaRef::new(name, version)
491 });
492 let cross_mem_different = match (&target_schema_ref, schema.id()) {
493 (Some(target), (src_name, _)) => target.name != src_name,
494 (None, _) => false,
495 };
496 let posture = if cross_mem_different {
497 let target_ref = target_schema_ref
498 .as_ref()
499 .expect("target_schema_ref is Some when cross_mem_different");
500 schema
501 .cross_mem_entry(&target_ref.name)
502 .and_then(|entry| entry.definitions.iter().find(|d| d.name == rel.rel_type))
503 .map(|d| d.per_edge_description)
504 } else {
505 schema
506 .relationship_def(&rel.rel_type)
507 .map(|d| d.per_edge_description)
508 };
509 match posture {
510 Some(PerEdgeDescription::Required) if rel.description.is_none() => {
511 posture_warnings.push(WarningHint::ParseMissingRequiredDescription {
512 from: entity.id.clone(),
513 rel_type: rel.rel_type.clone(),
514 target: rel.target.clone(),
515 });
516 }
517 Some(PerEdgeDescription::Forbidden) if rel.description.is_some() => {
518 posture_warnings.push(WarningHint::ParseDescriptionNotPermitted {
519 from: entity.id.clone(),
520 rel_type: rel.rel_type.clone(),
521 target: rel.target.clone(),
522 });
523 to_strip_description.push((
524 entity.id.clone(),
525 rel.rel_type.clone(),
526 rel.target.clone(),
527 ));
528 }
529 _ => {}
530 }
531 }
532 }
533 // Apply the description-strip in a second pass to avoid
534 // borrowing the store mutably while iterating it.
535 for (from_id, rel_type, target) in to_strip_description {
536 if let Some(entity) = store.get_mut(&from_id) {
537 for rel in entity.relationships.iter_mut() {
538 if rel.rel_type == rel_type && rel.target == target {
539 rel.description = None;
540 }
541 }
542 }
543 }
544 warnings.extend(posture_warnings);
545 }
546
547 // Pass 2: cycle detection per acyclic rel-type. Runs after the
548 // schema-shape drops above so the input subgraph is already
549 // schema-clean; cycles closed by edges that pass shape are the
550 // residual hazard hand-edits can produce. Single pass per
551 // rel-type — for each acyclic rel-type, build the workspace-wide
552 // adjacency list of edges whose source mem declares that
553 // rel-type as acyclic, then DFS with three-color marking
554 // (white / gray / black). On encountering a gray node from a
555 // gray parent, the traversing edge is a back-edge — drop it and
556 // continue. The chosen back-edge is the *latest-visited* edge
557 // in the cycle, not the "earliest" or "structural" one. That's
558 // intentionally stable: DFS order is determined by `EntityId`
559 // hash iteration (`HashMap` keys), which is consistent within a
560 // process. Different processes may pick different back-edges;
561 // either way the cycle is broken and the agent sees a typed
562 // warning naming the dropped relation.
563
564 // Collect the union of acyclic rel-types declared by any schema
565 // in this workspace.
566 let mut acyclic_rel_types: Vec<String> = Vec::new();
567 for schema in schemas.values() {
568 for def in &schema.manifest.relationships.definitions {
569 if def.acyclic && !acyclic_rel_types.contains(&def.name) {
570 acyclic_rel_types.push(def.name.clone());
571 }
572 }
573 }
574
575 let mut cycle_drops: Vec<(EntityId, EntityId, String)> = Vec::new();
576 for rel_type in &acyclic_rel_types {
577 // Adjacency list scoped to this rel-type. Includes edges
578 // whose source mem's schema declares the rel-type as
579 // acyclic — a mem whose schema doesn't declare the type
580 // acyclic shouldn't have its edges dropped just because a
581 // sibling mem does.
582 let mut adj: std::collections::HashMap<EntityId, Vec<EntityId>> =
583 std::collections::HashMap::new();
584 for entity in store.all_entities() {
585 let Some(schema) = schemas.get(entity.mem.as_str()) else {
586 continue;
587 };
588 if !schema.relationship_acyclic(rel_type) {
589 continue;
590 }
591 for edge in store.outgoing(&entity.id) {
592 if &edge.rel_type == rel_type {
593 adj.entry(entity.id.clone())
594 .or_default()
595 .push(edge.target.clone());
596 }
597 }
598 }
599
600 // Three-color DFS. Each entity is white initially. Push to
601 // gray on entry; demote to black on full descent. A gray
602 // child reached from a gray parent is a back-edge.
603 #[derive(Clone, Copy, PartialEq, Eq)]
604 enum Color {
605 White,
606 Gray,
607 Black,
608 }
609 let mut color: std::collections::HashMap<EntityId, Color> =
610 adj.keys().map(|k| (k.clone(), Color::White)).collect();
611 // Stable iteration order — sort the seeds so the dropped
612 // edge depends only on the workspace's id set, not on hash
613 // iteration order.
614 let mut seeds: Vec<EntityId> = adj.keys().cloned().collect();
615 seeds.sort_by(|a, b| a.as_ref().cmp(b.as_ref()));
616 for seed in seeds {
617 if color.get(&seed).copied() != Some(Color::White) {
618 continue;
619 }
620 // Iterative DFS to avoid stack blow-ups on deep graphs.
621 // Stack entry: (node, sorted-adjacency-index, sorted-adjacency-snapshot).
622 let mut stack: Vec<(EntityId, usize, Vec<EntityId>)> = Vec::new();
623 let mut start_targets: Vec<EntityId> = adj.get(&seed).cloned().unwrap_or_default();
624 start_targets.sort_by(|a, b| a.as_ref().cmp(b.as_ref()));
625 color.insert(seed.clone(), Color::Gray);
626 stack.push((seed.clone(), 0, start_targets));
627 while let Some((node, idx, targets)) = stack.last_mut() {
628 if *idx >= targets.len() {
629 let done = node.clone();
630 color.insert(done, Color::Black);
631 stack.pop();
632 continue;
633 }
634 let target = targets[*idx].clone();
635 *idx += 1;
636 let node_id = node.clone();
637 match color.get(&target).copied() {
638 Some(Color::White) => {
639 let mut next_targets: Vec<EntityId> =
640 adj.get(&target).cloned().unwrap_or_default();
641 next_targets.sort_by(|a, b| a.as_ref().cmp(b.as_ref()));
642 color.insert(target.clone(), Color::Gray);
643 stack.push((target, 0, next_targets));
644 }
645 Some(Color::Gray) => {
646 // Back-edge — closes a cycle. Drop it.
647 cycle_drops.push((node_id, target, rel_type.clone()));
648 }
649 Some(Color::Black) | None => {
650 // Already fully explored or not in the
651 // subgraph — no cycle through this edge.
652 }
653 }
654 }
655 }
656 }
657
658 for (from_id, target, rel_type) in cycle_drops {
659 let origin = origin_for(from_id.mem()).to_string();
660 store.remove_edge(&from_id, &target, &rel_type);
661 if let Some(entity) = store.get_mut(&from_id) {
662 entity
663 .relationships
664 .retain(|r| !(r.rel_type == rel_type && r.target == target));
665 }
666 let recovery = if origin == "writable" {
667 Some(
668 crate::ops::ParsedRelationRecovery::remove_explicit_relation(
669 from_id.clone(),
670 target.clone(),
671 rel_type.clone(),
672 ),
673 )
674 } else {
675 None
676 };
677 warnings.push(WarningHint::ParsedRelationInvalid {
678 entity_id: from_id,
679 rel_type,
680 target,
681 reason: "cycle".to_string(),
682 origin,
683 recovery,
684 });
685 }
686}
687
688/// Remap edge sources to reflect each source mem's
689/// `alias_target_rel_type` schema pointer: edges whose `rel_type`
690/// equals the pointer are flipped from `Explicit` to `BodyLink`.
691/// Idempotent — running it repeatedly produces the same result.
692///
693/// The discriminator is store-side only (no entity-side field). Under
694/// the schema-load coupling (Option C), the pointer rel-type is also
695/// `manual_authoring: forbidden`, so the only path to an edge of that
696/// rel-type is via the alias-synthesis pass — making this remap
697/// uniform across the workspace once the test sweep completes.
698///
699/// During the transitional window (synthesis pass landed but the 5
700/// built-ins not yet flipped to `manual_authoring: forbidden`),
701/// explicit `memstead_relate type=REFERENCES` still works for tests, and
702/// those edges will also be remapped to `BodyLink` here. The wire
703/// shape distinguishes synthesised vs. explicit only through this
704/// label, so the relabel is observable but harmless — no test
705/// asserts the legacy `"explicit"` string for REFERENCES.
706pub fn remap_alias_target_edge_sources(
707 store: &mut Store,
708 schemas: &std::collections::HashMap<String, std::sync::Arc<memstead_schema::Schema>>,
709) {
710 let mut remaps: Vec<(EntityId, EntityId, String)> = Vec::new();
711 for entity in store.all_entities() {
712 let Some(schema) = schemas.get(entity.mem.as_str()) else {
713 continue;
714 };
715 let Some(pointer) = schema.alias_target_rel_type() else {
716 continue;
717 };
718 for edge in store.outgoing(&entity.id) {
719 if edge.rel_type == pointer && edge.source != EdgeSource::BodyLink {
720 remaps.push((
721 entity.id.clone(),
722 edge.target.clone(),
723 edge.rel_type.clone(),
724 ));
725 }
726 }
727 }
728 for (from, to, rel_type) in remaps {
729 store.add_edge(
730 from,
731 Edge {
732 rel_type,
733 target: to,
734 source: EdgeSource::BodyLink,
735 },
736 );
737 }
738}
739
740/// Minimal placeholder entity for a wiki-link target that has no
741/// markdown file. Tagged `StubKind::LoadTime` — this constructor
742/// fires from parser-driven paths (boot, reload, attach) where the
743/// stub is auto-emitted from a wiki-link to a not-yet-present
744/// target. Mutation paths that need `ForwardReference` /
745/// `Residual` use the engine-internal `make_stub` in
746/// `engine/mutation/mod.rs` which takes an explicit kind.
747pub fn make_stub(id: EntityId) -> Entity {
748 Entity {
749 title: id.name().to_string(),
750 entity_type: String::new(),
751 mem: id.mem().to_string(),
752 file_path: String::new(),
753 metadata: IndexMap::new(),
754 sections: IndexMap::new(),
755 relationships: Vec::new(),
756 content_hash: String::new(),
757 stub: true,
758 stub_kind: Some(crate::entity::StubKind::LoadTime),
759 id,
760 heading_spans: std::collections::HashMap::new(),
761 }
762}
763
764#[cfg(test)]
765mod tests {
766 use super::*;
767 use crate::entity::Entity;
768 use memstead_schema::type_by_name;
769
770 fn default_fallback() -> std::sync::Arc<TypeDefinition> {
771 type_by_name("spec").expect("spec type must exist")
772 }
773
774 fn real_entity(id_str: &str, sections: &[(&str, &str)]) -> ParseResult {
775 let id = EntityId(id_str.to_string());
776 let mem = id.mem().to_string();
777 let mut sec = IndexMap::new();
778 for (k, v) in sections {
779 sec.insert(k.to_string(), v.to_string());
780 }
781 ParseResult {
782 entity: Entity {
783 title: id.name().to_string(),
784 entity_type: "spec".to_string(),
785 mem,
786 file_path: format!("{}.md", id.name()),
787 metadata: IndexMap::new(),
788 sections: sec,
789 relationships: Vec::new(),
790 content_hash: "deadbeef00000000".to_string(),
791 stub: false,
792 stub_kind: None,
793 id,
794 heading_spans: std::collections::HashMap::new(),
795 },
796 inline_links: Vec::new(),
797 parse_warnings: Vec::new(),
798 }
799 }
800
801 /// Plugin-mem entity with `[[plugin--foo]]` in a section and a
802 /// real `test-mem-plugin--foo` already in the store → warning
803 /// fires with a populated `candidate_target` (same-mem bare-slug
804 /// resolution, pass 2 of the two-pass resolver).
805 #[test]
806 fn nested_prefix_emits_warning_with_candidate() {
807 let fallback = default_fallback();
808 let mut store = Store::new();
809
810 let target = real_entity("test-mem-plugin--foo", &[]);
811 push_entities_into_store(&mut store, vec![target], &fallback, None);
812
813 let author = real_entity(
814 "test-mem-plugin--author",
815 &[("constraints", "See [[plugin--foo]] for details.")],
816 );
817 let mut warnings = Vec::new();
818 let mem_names = vec!["test-mem-plugin".to_string()];
819 let known_suffixes = vec!["plugin".to_string()];
820 push_entities_into_store(
821 &mut store,
822 vec![author],
823 &fallback,
824 Some(LoadCollector {
825 warnings: &mut warnings,
826 known_suffixes: &known_suffixes,
827 mem_names: &mem_names,
828 }),
829 );
830
831 assert_eq!(warnings.len(), 1, "one nested-prefix warning expected");
832 match &warnings[0] {
833 WarningHint::SuspiciousNestedPrefix {
834 from,
835 resolved_id,
836 candidate_target,
837 section,
838 } => {
839 assert_eq!(from.as_ref(), "test-mem-plugin--author");
840 // Tier-0 resolves `[[plugin--foo]]` to `plugin--foo`
841 // directly (not a phantom
842 // `test-mem-plugin--plugin--foo`).
843 assert_eq!(resolved_id.as_ref(), "plugin--foo");
844 assert_eq!(
845 candidate_target.as_ref().map(|c| c.as_ref()),
846 Some("test-mem-plugin--foo")
847 );
848 assert_eq!(section, "constraints");
849 }
850 other => panic!("unexpected variant: {other:?}"),
851 }
852 }
853
854 /// #41 narrowing: a colon/dash cross-mem link whose target mem
855 /// is itself a full roster member is legitimate — no nested-prefix
856 /// warning, even though that mem name also appears as a known
857 /// suffix. This is the macos→engine false positive the heuristic
858 /// used to emit (the "did you mean" candidate equalled the resolved
859 /// target — self-contradicting).
860 #[test]
861 fn nested_prefix_skips_when_target_is_a_real_mem() {
862 let fallback = default_fallback();
863 let mut store = Store::new();
864
865 let target = real_entity("engine--foo", &[]);
866 push_entities_into_store(&mut store, vec![target], &fallback, None);
867
868 let author = real_entity(
869 "macos--author",
870 &[("constraints", "See [[engine--foo]] for details.")],
871 );
872 let mut warnings = Vec::new();
873 let mem_names = vec!["macos".to_string(), "engine".to_string()];
874 // `engine` is both a real mem AND its own last-segment suffix.
875 let known_suffixes = vec!["macos".to_string(), "engine".to_string()];
876 push_entities_into_store(
877 &mut store,
878 vec![author],
879 &fallback,
880 Some(LoadCollector {
881 warnings: &mut warnings,
882 known_suffixes: &known_suffixes,
883 mem_names: &mem_names,
884 }),
885 );
886
887 assert!(
888 warnings.is_empty(),
889 "a cross-mem link to a real mem must not warn: {warnings:?}"
890 );
891 }
892
893 /// Same scenario but the candidate is missing — the warning still
894 /// fires so the author sees drift, with `candidate_target: None`.
895 #[test]
896 fn nested_prefix_emits_warning_without_candidate() {
897 let fallback = default_fallback();
898 let mut store = Store::new();
899
900 let author = real_entity(
901 "test-mem-plugin--author",
902 &[("constraints", "[[plugin--ghost]]")],
903 );
904 let mut warnings = Vec::new();
905 let mem_names = vec!["test-mem-plugin".to_string()];
906 let known_suffixes = vec!["plugin".to_string()];
907 push_entities_into_store(
908 &mut store,
909 vec![author],
910 &fallback,
911 Some(LoadCollector {
912 warnings: &mut warnings,
913 known_suffixes: &known_suffixes,
914 mem_names: &mem_names,
915 }),
916 );
917
918 assert_eq!(warnings.len(), 1);
919 match &warnings[0] {
920 WarningHint::SuspiciousNestedPrefix {
921 candidate_target, ..
922 } => assert!(candidate_target.is_none()),
923 other => panic!("unexpected variant: {other:?}"),
924 }
925 }
926
927 /// Bare-slug link (`[[foo]]`) resolves to `<current-mem>--foo` —
928 /// no nested prefix, no warning.
929 #[test]
930 fn non_nested_link_no_warning() {
931 let fallback = default_fallback();
932 let mut store = Store::new();
933
934 let author = real_entity("test-mem-plugin--author", &[("constraints", "[[foo]]")]);
935 let mut warnings = Vec::new();
936 let mem_names = vec!["test-mem-plugin".to_string()];
937 let known_suffixes = vec!["plugin".to_string()];
938 push_entities_into_store(
939 &mut store,
940 vec![author],
941 &fallback,
942 Some(LoadCollector {
943 warnings: &mut warnings,
944 known_suffixes: &known_suffixes,
945 mem_names: &mem_names,
946 }),
947 );
948 assert!(warnings.is_empty());
949 }
950
951 /// Fully-qualified cross-mem link resolves to a different mem's
952 /// id, not `<current-mem>--<suffix>--...`, so no nested prefix.
953 /// Note: `[[<mem>--slug]]` in the section body literally resolves
954 /// via wiki_link_to_id to `<current>--<mem>--slug` (nested), so
955 /// this pattern is ambiguous by construction — the detector fires
956 /// with a candidate that points at the fully-qualified target.
957 /// Callers should write the full id or bare slug, not
958 /// `<mem>--slug` from outside that mem.
959 #[test]
960 fn cross_mem_qualified_fires_with_cross_mem_candidate() {
961 let fallback = default_fallback();
962 let mut store = Store::new();
963
964 // Real entity in the engine mem.
965 let target = real_entity("test-mem-engine--health", &[]);
966 push_entities_into_store(&mut store, vec![target], &fallback, None);
967
968 // Plugin-mem author writes `[[engine--health]]`.
969 let author = real_entity(
970 "test-mem-plugin--author",
971 &[("purpose", "See [[engine--health]].")],
972 );
973 let mut warnings = Vec::new();
974 let mem_names = vec!["test-mem-engine".to_string(), "test-mem-plugin".to_string()];
975 let known_suffixes = vec!["engine".to_string(), "plugin".to_string()];
976 push_entities_into_store(
977 &mut store,
978 vec![author],
979 &fallback,
980 Some(LoadCollector {
981 warnings: &mut warnings,
982 known_suffixes: &known_suffixes,
983 mem_names: &mem_names,
984 }),
985 );
986 assert_eq!(warnings.len(), 1);
987 match &warnings[0] {
988 WarningHint::SuspiciousNestedPrefix {
989 candidate_target, ..
990 } => {
991 assert_eq!(
992 candidate_target.as_ref().map(|c| c.as_ref()),
993 Some("test-mem-engine--health"),
994 "cross-mem pass-1 must find the engine mem candidate"
995 );
996 }
997 other => panic!("unexpected variant: {other:?}"),
998 }
999 }
1000
1001 /// Two mems sharing a last-segment suffix: both contribute to the
1002 /// known-suffix set, the warning fires, and `candidate_target` is
1003 /// the one that has a real entity. Locks the suffix-collision
1004 /// resolution semantics.
1005 #[test]
1006 fn suffix_collision_resolves_first_match() {
1007 let fallback = default_fallback();
1008 let mut store = Store::new();
1009
1010 // Mem A = `alpha`, Mem B = `beta-alpha`, both have suffix "alpha".
1011 // Real entity lives in `beta-alpha--target`.
1012 let target = real_entity("beta-alpha--target", &[]);
1013 push_entities_into_store(&mut store, vec![target], &fallback, None);
1014
1015 // An author in `beta-alpha` writes `[[alpha--target]]`.
1016 let author = real_entity("beta-alpha--author", &[("purpose", "[[alpha--target]]")]);
1017 let mut warnings = Vec::new();
1018 let mem_names = vec!["alpha".to_string(), "beta-alpha".to_string()];
1019 let known_suffixes = vec!["alpha".to_string(), "alpha".to_string()]; // collision
1020 push_entities_into_store(
1021 &mut store,
1022 vec![author],
1023 &fallback,
1024 Some(LoadCollector {
1025 warnings: &mut warnings,
1026 known_suffixes: &known_suffixes,
1027 mem_names: &mem_names,
1028 }),
1029 );
1030 assert_eq!(
1031 warnings.len(),
1032 1,
1033 "collision must not duplicate the warning"
1034 );
1035 match &warnings[0] {
1036 WarningHint::SuspiciousNestedPrefix {
1037 candidate_target, ..
1038 } => {
1039 // Pass 1 cross-mem probe excludes `beta-alpha` (self),
1040 // probes `alpha` — no real entity there, so pass 2
1041 // falls back to same-mem bare-slug `beta-alpha--target`
1042 // which is real.
1043 assert_eq!(
1044 candidate_target.as_ref().map(|c| c.as_ref()),
1045 Some("beta-alpha--target")
1046 );
1047 }
1048 other => panic!("unexpected variant: {other:?}"),
1049 }
1050 }
1051}