Skip to main content

memstead_base/engine/
query.rs

1//! Engine read paths — accessors and queries.
2//!
3//! Read-only methods on `Engine`: store / schema / mount accessors,
4//! per-mem path helpers (`gitdir_for` / `worktree_for`), aggregated
5//! views (`communities`, `orphans`, `stubs`, `most_connected`,
6//! `missing_required_outgoing`), per-mem summaries (`health`,
7//! `status`, `context`), search (`list`, `search`,
8//! `search_indexes`), and the bytes-level read wrappers
9//! (`list_entities`, `read_entity`, `read_provenance`). Capability and
10//! cross-mem link gating live here too — they're consulted by
11//! handlers before any mutation reaches the backend.
12
13use std::cell::OnceCell;
14use std::collections::HashMap;
15use std::path::{Path, PathBuf};
16use std::sync::Arc;
17
18use memstead_schema::Schema;
19
20use crate::engine_fallback_type;
21use crate::entity::{Entity, EntityId};
22use crate::graph::{LouvainOutput, community::detect_communities};
23use crate::mem::MemRouterSnapshot;
24use crate::ops::{ContextResult, Direction, NeighborInfo, SearchResult, SearchScope, WarningHint};
25use crate::provenance::Provenance;
26#[cfg(not(target_arch = "wasm32"))]
27use crate::search_index::{MemIndex, build_all};
28use crate::store::Store;
29use crate::workspace::{MountCapability, MountStorage, WorkspaceSettings};
30
31use super::{BackendFactory, Engine, EngineError, MountedBackend};
32
33impl Engine {
34    /// In-memory store populated at construction time from every
35    /// mount's backend. Read-only at this point in the rebuild —
36    /// mutation paths land in a later session.
37    pub fn store(&self) -> &Store {
38        &self.store
39    }
40
41    /// Per-mem schema, keyed by mount's mem name. Each entry is the
42    /// schema resolved from that mount's pin at boot, so the map holds
43    /// genuinely heterogeneous schemas in a multi-schema workspace.
44    pub fn schemas(&self) -> &HashMap<String, Arc<Schema>> {
45        &self.schemas
46    }
47
48    /// Workspace-authored schemas loaded from
49    /// `WorkspaceSettings.schemas_dir` at construction. Distinct from
50    /// [`Self::schemas`] (per-mem, only schemas pinned by a mount):
51    /// this slice carries every workspace-loaded schema regardless of
52    /// whether a mem pins it. Used by `memstead_overview` to enumerate
53    /// schemas referenced by `mem_create_rules.schemas[]` but not
54    /// pinned by any mem — agents see what could be pinned without
55    /// looking up the workspace.toml directly.
56    pub fn workspace_schemas(&self) -> &[Arc<Schema>] {
57        &self.workspace_schemas
58    }
59
60    /// Embedded built-in schemas loaded once at boot. Handlers
61    /// resolving a schema pin by `<name>@<version>` (MCP's `memstead_schema`,
62    /// `memstead_overview` rendering) walk mem-pinned, workspace, and
63    /// built-in catalogues in order — built-ins are the catch-all when
64    /// no mem or workspace dir pins the schema. Workspace schemas
65    /// shadow built-ins on `(name, version)` collision; resolve from
66    /// `workspace_schemas()` first.
67    pub fn builtin_schemas(&self) -> &[Arc<Schema>] {
68        &self.builtin_schemas
69    }
70
71    /// Classify a schema's trust origin — the single authority every read
72    /// surface consults before serving a schema's instruction-prose.
73    ///
74    /// A schema is [`OriginClass::FirstParty`] iff it is an engine built-in
75    /// **or** pinned by a writable mount in this workspace. Built-ins are
76    /// compiled into the binary — unforgeable. A non-built-in schema earns
77    /// first-party status only once the operator *adopts* it by writably
78    /// mounting a mem that pins it: writing into a mem is the act that
79    /// legitimately needs a schema's authoring prose (`system_message`,
80    /// `write_rules`, …), and the mount's writable posture is set by the
81    /// consumer's own config — a publisher cannot forge it.
82    ///
83    /// Everything else is [`OriginClass::ThirdParty`]: a schema present in
84    /// the catalogue but pinned only by read-only mounts (a registry-
85    /// installed read-mem or an adopted foreign folder/clone), or one the
86    /// engine cannot vouch for at all. Its prose is served structural-only
87    /// so a stranger's free-text never reaches a consuming agent as
88    /// instructions. This classifies by the mount graph — never by scanning
89    /// the schema's content, which a publisher controls — and `ThirdParty`
90    /// is the safe default for any ambiguous origin.
91    ///
92    /// Note a read-only mount pinning a *built-in* schema (e.g. a registry
93    /// mem on `default@1.0.0`) resolves to the consumer's own clean copy
94    /// and stays first-party — the de-framing targets only foreign,
95    /// non-built-in schemas that no writable mem has adopted.
96    pub fn schema_origin(&self, schema: &Arc<Schema>) -> crate::render::OriginClass {
97        use crate::render::OriginClass;
98        let (name, version) = schema.id();
99        // Built-in schemas are compiled in — first-party, unforgeable.
100        let is_builtin = self.builtin_schemas.iter().any(|s| {
101            let id = s.id();
102            id.0 == name && id.1 == version
103        });
104        if is_builtin {
105            return OriginClass::FirstParty;
106        }
107        // Adoption signal: some writable mount pins this exact schema, so
108        // the operator authors against it here.
109        let canon = format!("{name}@{version}");
110        let pinned_by_writable = self.mounts().iter().any(|m| {
111            m.schema.as_ref().map(|s| s.to_string()).as_deref() == Some(canon.as_str())
112                && self.mem_router().is_writable(&m.mem)
113        });
114        if pinned_by_writable {
115            OriginClass::FirstParty
116        } else {
117            OriginClass::ThirdParty
118        }
119    }
120
121    /// Classify a mem's *data* trust origin — the authority every read
122    /// surface consults before serving an entity's content (bodies,
123    /// snippets, titles). A writable mount is [`OriginClass::FirstParty`]:
124    /// its content is authored in this workspace. Anything else — a
125    /// read-only mount (a registry-installed read-mem or an adopted
126    /// foreign folder/clone) or an unknown mem — is
127    /// [`OriginClass::ThirdParty`], so the consuming agent/host treats the
128    /// content as quoted, untrusted data.
129    ///
130    /// This reads the deployment's declaration when one exists (see
131    /// [`Self::declare_mem_origin`]), else the mount's already-decided
132    /// writable/read-only posture (fixed at adopt/mount time) — it never
133    /// scans content, and both levers are consumer-side config, so a
134    /// publisher cannot forge first-party. Distinct from
135    /// [`Self::schema_origin`], which governs a schema's
136    /// instruction-prose: the data channel and the instruction channel
137    /// are separate vectors with separate authorities.
138    pub fn mem_origin_class(&self, mem: &str) -> crate::render::OriginClass {
139        if let Some(declared) = self.declared_origins.get(mem) {
140            return *declared;
141        }
142        if self.mem_router().is_writable(mem) {
143            crate::render::OriginClass::FirstParty
144        } else {
145            crate::render::OriginClass::ThirdParty
146        }
147    }
148
149    /// Declare a mem's data-trust origin as a deployment fact — the
150    /// embedding process (a curated hosted read tier, an app that vouches
151    /// for a bundled mem) overrides the writability inference for one mem.
152    /// Composition-layer-only by design: not persisted, not reachable over
153    /// MCP, never derived from mem content — the operator running the
154    /// process is the only authority that can set it, so a served mem the
155    /// deployment does *not* vouch for keeps reporting third-party on
156    /// every surface. (Deliberately absent from the CLI: that surface
157    /// operates a workspace, not a deployment; the CLI counterpart would be
158    /// a workspace-config knob no use case demands yet.)
159    pub fn declare_mem_origin(
160        &mut self,
161        mem: impl Into<String>,
162        origin: crate::render::OriginClass,
163    ) {
164        self.declared_origins.insert(mem.into(), origin);
165    }
166
167    /// Per-file errors collected during load. Non-fatal: the engine
168    /// continues with whatever did parse. Empty when every backend's
169    /// content parses cleanly.
170    pub fn load_errors(&self) -> &[(PathBuf, String)] {
171        &self.load_errors
172    }
173
174    /// Resolve a VISIBLE mem to its folder-storage root on disk.
175    ///
176    /// Unknown or quarantined names refuse `UNKNOWN_MEM` (the same
177    /// visibility gate `search` and the conflicts door apply); a
178    /// visible mount whose storage is not folder-backed returns
179    /// `Ok(None)` so callers branch on backend applicability without
180    /// inventing a typed error. Read-only mounts resolve too — this is
181    /// a read accessor, not a write gate. Generic by design: any
182    /// consumer that needs a folder mem's disk root (per-mem changelog
183    /// readers, export tooling, future doors) gets the same answer.
184    pub fn folder_mem_root(&self, mem: &str) -> Result<Option<PathBuf>, EngineError> {
185        let mount = self
186            .mounts
187            .iter()
188            .find(|m| m.mount.mem == mem)
189            .ok_or_else(|| self.unknown_mem_error(mem))?;
190        if self.quarantine_reason(mem).is_some() {
191            return Err(self.unknown_mem_error(mem));
192        }
193        match &mount.mount.storage {
194            crate::workspace::MountStorage::Folder { path } => Ok(Some(path.clone())),
195            _ => Ok(None),
196        }
197    }
198
199    /// Workspace-level operator policy (mem create/delete rules,
200    /// cross-mem links). Defaults to empty; populated via
201    /// [`Engine::set_settings`] after construction. Surfaced for MCP
202    /// handlers (`memstead_health { include_config: true }`,
203    /// `memstead_overview`'s lifecycle-namespaces section) and other
204    /// consumers that need to read workspace policy.
205    pub fn settings(&self) -> &WorkspaceSettings {
206        &self.settings
207    }
208
209    /// The pipeline configs — the v2 single-record binding store — loaded
210    /// from the workspace at boot: the read-only queryable surface the
211    /// loader exposes. Empty for engines not booted from a workspace root,
212    /// or for a workspace that declares no pipelines. The ingest skill
213    /// and future MCP tools consume this structured form
214    /// rather than re-reading the JSON folders.
215    pub fn pipeline_configs(&self) -> &crate::pipeline_store::BindingConfigs {
216        &self.pipeline_configs
217    }
218
219    /// The pipeline configs serialized as a JSON string — the read
220    /// counterpart of the `add_projection_json` edit entry point.
221    /// Serialization-boundary callers (where serde does not live)
222    /// get the store in one call and deserialize on their side.
223    ///
224    /// Shape: `{ "bindings": [{ mem, name, config }] }` — the v2
225    /// single-record store (`config` carries the whole binding: inline
226    /// `sources`, `operations`, everything). The `mediums` / `facets` /
227    /// `ingests` keys are **gone** with their record kinds. This reads the
228    /// live binding store fresh (like the brief path) rather than the
229    /// in-memory snapshot, so an edit shows back immediately. A missing
230    /// root or a legacy/unreadable store yields the fallback empty object.
231    pub fn pipeline_configs_json(&self) -> String {
232        let empty = || "{\"bindings\":[]}".to_string();
233        let Some(root) = self.workspace_root() else {
234            return empty();
235        };
236        match crate::pipeline_store::load_pipeline_configs(root) {
237            Ok(configs) => serde_json::to_string(&configs).unwrap_or_else(|_| empty()),
238            Err(_) => empty(),
239        }
240    }
241
242    /// Overwrite the in-memory pipeline configs. The workspace-root boot
243    /// paths call this after [`crate::pipeline_store::load_pipeline_configs`];
244    /// exposed so the full boot helper (a separate crate) can populate the
245    /// same surface.
246    pub fn set_pipeline_configs(&mut self, configs: crate::pipeline_store::BindingConfigs) {
247        self.pipeline_configs = configs;
248    }
249
250    /// Build a [`WarningHint::NoteMissing`] when the workspace has
251    /// `[mutations].require_notes = true` and the caller omitted (or
252    /// passed a blank/whitespace-only) `note`; `None` otherwise.
253    ///
254    /// This is the single enforcement point for the `require_notes`
255    /// provenance nudge. Every mutation that accepts a `note` calls it
256    /// on its commit-landing path and pushes the result onto the
257    /// outcome's `warnings`, so both the CLI and the MCP transports
258    /// inherit identical behaviour from the engine response rather than
259    /// each re-deriving the policy at its own boundary (the drift that
260    /// left the policy decorative on the CLI). `tool` becomes the
261    /// warning's `details.tool` — callers pass the engine-level verb
262    /// (`create_entity`, `update_entity`, `relate_entity`,
263    /// `delete_entity`, `rename_entity`, `create_mem`,
264    /// `delete_mem`), matching the commit `Tool:` provenance trailer.
265    /// The mutation still commits — the policy nudges, it never blocks.
266    pub fn note_missing_warning(&self, tool: &str, note: Option<&str>) -> Option<WarningHint> {
267        if !self.settings.mutations.require_notes.unwrap_or(false) {
268            return None;
269        }
270        let has_note = note.map(|n| !n.trim().is_empty()).unwrap_or(false);
271        if has_note {
272            return None;
273        }
274        Some(WarningHint::NoteMissing {
275            tool: tool.to_string(),
276        })
277    }
278
279    /// Backend factory currently installed on this engine. Returned by
280    /// value because [`BackendFactory`] is a function pointer (`Copy`).
281    /// Used by [`crate::mem_management::create_mem`] to materialise
282    /// the backend for a freshly-registered mount; consumers that need
283    /// to instantiate a backend ad-hoc can call this directly.
284    pub fn backend_factory(&self) -> BackendFactory {
285        self.backend_factory
286    }
287
288    /// Git-branch ops bundle currently installed on this engine.
289    /// `None` on lean-flavor engines that don't see mem-repo
290    /// mounts. Returned by value because [`super::GitBranchOps`] is
291    /// `Copy`. `create_mem` reaches for
292    /// the bundle to drive `prune_residue` against an unmounted
293    /// gitdir when the `ForceOverwrite` recovery action is selected.
294    pub fn git_branch_ops(&self) -> Option<super::GitBranchOps> {
295        self.git_branch_ops
296    }
297
298    /// Convenience: look up a parsed entity by id. Returns `None` for
299    /// unknown ids, including stub entries created for unresolved
300    /// inline-link targets — callers that want to distinguish real
301    /// from stub branch on `Entity::stub`.
302    pub fn get_entity(&self, id: &EntityId) -> Option<&Entity> {
303        self.store.get(id)
304    }
305
306    /// The stored provenance anchors for `id`, read from its mem's
307    /// anchors sidecar. Empty for an entity with none, an unknown mem, or
308    /// a backend that does not persist anchors (a pre-anchor archive / any
309    /// sealed read-only mount). Additive read surface (E3a): the
310    /// resolution *model* lives in [`crate::anchor`]
311    /// ([`crate::anchor::resolve_anchor`] / [`crate::anchor::compose_entity_anchors`]);
312    /// the live per-anchor *state* (which requires observing the source
313    /// artifacts through the medium/preparation pipeline) is E3b's concern.
314    /// The anchors sidecar's parse error for `mem`, if it has one.
315    ///
316    /// The anchor readers below degrade a malformed sidecar to "no anchors",
317    /// which keeps a read path alive but makes a corrupt file
318    /// indistinguishable from an empty one. For a *reader* that is the right
319    /// trade; for anything that draws a conclusion from the absence of
320    /// anchors it is not — a fidelity pass would report every artifact
321    /// uncovered and call it a finding, when the truth is that it could not
322    /// read the file. Callers that need that distinction ask here first and
323    /// refuse. `None` means the sidecar is absent (legitimately no anchors
324    /// yet) or parses cleanly; the binding store draws the same distinction
325    /// with its quarantine path.
326    pub fn anchors_sidecar_error(&self, mem: &str) -> Option<String> {
327        let mount = self.mounts.iter().find(|m| m.mount.mem == mem)?;
328        // Three distinct ways to be unreadable, and only one of them is a
329        // parse error. `.ok().flatten()` would collapse the first into
330        // "absent", which is the very confusion this exists to prevent.
331        let bytes = match mount.backend.read_anchors_sidecar() {
332            // A backend error — permission denied, an IO fault. The file may
333            // be perfectly well-formed; we simply could not look at it.
334            Err(e) => return Some(format!("could not read the sidecar: {e}")),
335            // Genuinely absent: a mem with no anchors yet. Not an error.
336            Ok(None) => return None,
337            Ok(Some(b)) => b,
338        };
339        // An empty or whitespace-only file parses as "no anchors" by a
340        // deliberate tolerance in `from_bytes`. That tolerance is right for a
341        // reader and wrong here: a sidecar truncated to zero by an
342        // interrupted write is not a mem that never had anchors.
343        if bytes.iter().all(|b| b.is_ascii_whitespace()) {
344            return Some(
345                "the sidecar file is empty — an interrupted write leaves this state, and it is                  not the same as having no anchors; remove the file if the mem genuinely has none"
346                    .to_string(),
347            );
348        }
349        match crate::anchor::AnchorSidecar::from_bytes(&bytes) {
350            Ok(_) => None,
351            Err(e) => Some(e.to_string()),
352        }
353    }
354
355    pub fn entity_anchors(&self, id: &EntityId) -> Vec<crate::anchor::Anchor> {
356        let Some(mount) = self.mounts.iter().find(|m| m.mount.mem == id.mem()) else {
357            return Vec::new();
358        };
359        let Ok(Some(bytes)) = mount.backend.read_anchors_sidecar() else {
360            return Vec::new();
361        };
362        match crate::anchor::AnchorSidecar::from_bytes(&bytes) {
363            Ok(sc) => sc.get(id.as_ref()).to_vec(),
364            // Deliberate degrade-to-empty for the read path; a caller that
365            // must not confuse "unreadable" with "none" checks
366            // [`Self::anchors_sidecar_error`] first.
367            Err(_) => Vec::new(),
368        }
369    }
370
371    /// The stored anchors for `id`, each paired with its **live** resolution
372    /// state when the engine could observe the source artifact this pass.
373    ///
374    /// Additive over [`Self::entity_anchors`]: the durable data is unchanged;
375    /// `state` is the [`crate::anchor::resolve_anchor`] outcome against an
376    /// observation the engine produces here. A `path`-namespace anchor
377    /// (codebase / filesystem / git) is observed against the working tree at
378    /// the current HEAD; an `entity`-namespace anchor is observed against the
379    /// live graph by [`Self::observe_entity_anchor`]:
380    ///
381    /// - artifact absent ⇒ [`AnchorState::Orphaned`](crate::anchor::AnchorState::Orphaned);
382    /// - artifact present, non-hash class (`authored` / `informed-by`) ⇒
383    ///   [`Resolves`](crate::anchor::AnchorState::Resolves);
384    /// - artifact present, hash-bearing class (`anchored` / `derived`) ⇒ the
385    ///   prepared-content hash comparison decides:
386    ///   [`Resolves`](crate::anchor::AnchorState::Resolves) on a match,
387    ///   [`Drifted`](crate::anchor::AnchorState::Drifted) on a stable-medium
388    ///   mismatch, [`Recheck`](crate::anchor::AnchorState::Recheck) on an
389    ///   unstable medium or when a hash is unavailable on either side (a
390    ///   hash-less anchor, a `tree` grain, an unreadable artifact).
391    ///
392    /// For an `entity` grain the same table applies, read off the store
393    /// rather than the filesystem: the entity missing (or present only as a
394    /// stub) is `Orphaned`, and a hash-bearing class compares the canonical
395    /// rendered markdown.
396    ///
397    /// `state` is `None` (unobserved — never a fabricated state) when there is
398    /// no workspace root, when the grain is `url` (the engine never fetches;
399    /// a url anchor's hash is the registry's prepared form of the content
400    /// its observer supplied at write time), when an `entity` anchor's
401    /// source declares a preparation the registry does not know (the form
402    /// cannot be computed), or when an `entity` anchor's mem is **not
403    /// mounted** — an unmounted mem is not a mem of deleted entities, and
404    /// saying so would route a deletion proposal to prune.
405    pub fn entity_anchors_resolved(&self, id: &EntityId) -> Vec<ResolvedAnchor> {
406        let anchors = self.entity_anchors(id);
407        let source_roots = self.anchor_source_roots(id.mem());
408        let none = SuppliedObservations::new();
409        anchors
410            .into_iter()
411            .map(|anchor| self.resolve_one(anchor, &source_roots, &none))
412            .collect()
413    }
414
415    /// Observe one stored anchor and pair it with its resolution — the
416    /// shared step behind every anchor read.
417    fn resolve_one(
418        &self,
419        anchor: crate::anchor::Anchor,
420        source_roots: &std::collections::BTreeMap<String, AnchorSourceJoin>,
421        supplied: &SuppliedObservations,
422    ) -> ResolvedAnchor {
423        let observed = self.observe_anchor(&anchor, source_roots, supplied);
424        let (state, observed_hash, observed_at) = match observed {
425            Some(o) => (Some(o.state), o.hash, o.at),
426            None => (None, None, None),
427        };
428        ResolvedAnchor {
429            anchor,
430            state,
431            observed_hash,
432            observed_at,
433        }
434    }
435
436    /// Per-anchor observation — THE one resolution mechanism, shared by
437    /// binding-backed verify (`mem_anchors_resolved`, which the ingest
438    /// render/report/prune/findings paths consume), the per-entity read
439    /// (`entity_anchors_resolved`), and the standalone
440    /// `verify_mem_anchors` operation. Path-shaped grains
441    /// (`span`/`file`/`tree`) observe under the **decision-29 candidate
442    /// priority** (backlog-sweep plan 03a): the anchor's artifact path is
443    /// SOURCE-relative first — when its `source` name resolves through
444    /// `source_roots` to a declared pointer, the pointer-joined path is
445    /// authoritative — and workspace-relative only as the fallback, tried
446    /// when the source-join does not resolve. A path resolving under both
447    /// joins is decided by that priority, deterministically. An anchor
448    /// without a `source` (a hand-authored mem, a binding-less write)
449    /// observes workspace-relative exactly as before. A `url`
450    /// grain has no engine-side observation and returns `None` (the report
451    /// vocabulary's `unresolvable`) — the engine never fetches; its hash is
452    /// recorded from observation-supplied content at write time — as does a
453    /// workspace-root-less engine. An `entity`
454    /// grain is not a filesystem path but is not unobservable either — it
455    /// resolves against the live graph via
456    /// [`Self::observe_entity_anchor`], under the preparation its source
457    /// declares (touchpoint A of [`crate::preparation`]: the registry
458    /// decides the prepared form the artifact hashes as). This replaces the retired `single_path_medium_root` gate,
459    /// whose single-source assumption nulled every anchor of a mem with
460    /// zero or several bindings — the honest per-anchor answer supersedes
461    /// the all-or-nothing mem-level one.
462    ///
463    /// A `url` grain is the one grain the engine cannot observe itself: it
464    /// resolves from a SUPPLIED observation when the caller passed one for
465    /// its artifact (`memstead verify-anchors --observations`), else from
466    /// the row's recorded `last_observed` (sidecar version 2) — an
467    /// observation that was made, carrying its own date so the row ages
468    /// visibly — else it is unobserved (`None`), never fabricated. A
469    /// supplied `absent` resolves `recheck`, not `orphaned`: an observer
470    /// failing to retrieve a web resource is not the medium saying the
471    /// artifact is gone, and prune must never act on it.
472    fn observe_anchor(
473        &self,
474        anchor: &crate::anchor::Anchor,
475        source_roots: &std::collections::BTreeMap<String, AnchorSourceJoin>,
476        supplied: &SuppliedObservations,
477    ) -> Option<Observed> {
478        if anchor.grain == crate::anchor::AnchorGrain::Url {
479            if let Some(obs) = supplied.get(&anchor.artifact) {
480                return Some(match &obs.outcome {
481                    crate::anchor::SuppliedOutcome::Absent => Observed {
482                        state: crate::anchor::AnchorState::Recheck,
483                        hash: None,
484                        at: Some(obs.at.clone()),
485                    },
486                    crate::anchor::SuppliedOutcome::Present { hash } => Observed {
487                        state: crate::anchor::resolve_anchor(
488                            anchor,
489                            &crate::anchor::ArtifactObservation::Present {
490                                current_hash: Some(hash.clone()),
491                            },
492                        ),
493                        hash: Some(hash.clone()),
494                        at: Some(obs.at.clone()),
495                    },
496                });
497            }
498            return anchor.last_observed.as_ref().map(|rec| Observed {
499                state: rec.state,
500                hash: rec.hash.clone(),
501                at: Some(rec.at.clone()),
502            });
503        }
504        let join = anchor
505            .source
506            .as_deref()
507            .and_then(|name| source_roots.get(name));
508        // An `entity`-grain anchor points into a mem's graph, not a file
509        // tree. It has always returned `None` here — "unobserved this pass" —
510        // which meant it could never be drifted, never be orphaned, and always
511        // blocked prune. That is the bail the S1b pilot demonstrated: a
512        // deliberately stale anchor over a changed source entity went unflagged
513        // while the capability matrix claimed full parity.
514        if anchor.grain == crate::anchor::AnchorGrain::Entity {
515            return self
516                .observe_entity_anchor(anchor, join.and_then(|j| j.preparation.as_deref()))
517                .map(Observed::live);
518        }
519        let root = self.workspace_root.as_deref()?;
520        observe_path_anchor(root, anchor, join).map(Observed::live)
521    }
522
523    /// Observe an `entity`-grain anchor against the live graph — the entity-
524    /// namespace counterpart of [`observe_path_anchor`], and the mechanism
525    /// that makes a graph-medium binding's drift real.
526    ///
527    /// The artifact is an entity id. Present/absent comes from the store, so
528    /// this works uniformly across backends — a git-branch mem has no
529    /// working-tree file to stat, which is exactly why observation cannot go
530    /// through the filesystem here. The compared form is the preparation
531    /// registry's **prepared form** for `preparation` (the anchor's source's
532    /// declared preparation, [`crate::preparation::entity_prepared_hash`]):
533    /// the **canonical rendered markdown** when the source declares none —
534    /// byte-for-byte today's form — or the load-bearing serialization under
535    /// `entity-load-bearing`; hashed with the same `prepared_content_hash`
536    /// the path arm uses, so an anchor's recorded hash means the same thing
537    /// in both namespaces. An identifier the registry does not know cannot
538    /// be prepared: the anchor is reported unobserved (`None`), never hashed
539    /// under a fabricated form.
540    ///
541    /// A stub is treated as absent: a stub is the engine's placeholder for an
542    /// unresolved reference, not the entity the anchor claims to pin. Scoring
543    /// it as present would let a dangling anchor resolve clean.
544    fn observe_entity_anchor(
545        &self,
546        anchor: &crate::anchor::Anchor,
547        preparation: Option<&str>,
548    ) -> Option<(crate::anchor::AnchorState, Option<String>)> {
549        let id = EntityId::canonical(&anchor.artifact);
550
551        // The mem the anchor points into must be MOUNTED before a store miss
552        // can mean anything. If it is not, every entity in it is missing from
553        // the store, and reporting `Orphaned` would say "the source deleted
554        // these" about entities sitting untouched on disk. `None` — genuinely
555        // unobserved — is the honest answer.
556        //
557        // This guard lives here, at the one observation site, and not at the
558        // callers. An earlier fix put it in `run_verify` alone; `prune` reaches
559        // anchor resolution by its own path (`mem_anchors_resolved`), so the
560        // sync brief went on proposing the deletion of every destination
561        // entity — a data-loss suggestion routed to the graph's only
562        // maintenance writer, from a mem merely being unmounted. A guard that
563        // protects one caller is not a guard on the behaviour.
564        if !self.mounts.iter().any(|m| m.mount.mem == id.mem()) {
565            return None;
566        }
567
568        let entity = self.store.get(&id).filter(|e| !e.stub);
569        let Some(entity) = entity else {
570            return Some((
571                crate::anchor::resolve_anchor(anchor, &crate::anchor::ArtifactObservation::Absent),
572                None,
573            ));
574        };
575        let current_hash = if anchor.class.is_hash_bearing() {
576            let type_def = self
577                .schema_for(id.mem())
578                .and_then(|schema| schema.get_type(&entity.entity_type));
579            Some(crate::preparation::entity_prepared_hash(
580                entity,
581                type_def.as_deref(),
582                preparation,
583            )?)
584        } else {
585            None
586        };
587        let observation = crate::anchor::ArtifactObservation::Present {
588            current_hash: current_hash.clone(),
589        };
590        Some((
591            crate::anchor::resolve_anchor(anchor, &observation),
592            current_hash,
593        ))
594    }
595
596    /// The `source name → join` map for `mem`'s bindings: the filesystem
597    /// roots that anchors written in the source dialect join onto (decision
598    /// 26: anchor artifact paths are source-relative first) and the
599    /// preparation each source declares (touchpoint A: what the registry
600    /// prepares the artifact as before hashing). Empty when the workspace
601    /// has no root, the pipeline store does not load, or `mem` has no
602    /// bindings — resolution then degrades to the workspace-relative dialect
603    /// alone with no preparation, which is exactly the hand-authored-mem
604    /// posture.
605    pub(crate) fn anchor_source_roots(
606        &self,
607        mem: &str,
608    ) -> std::collections::BTreeMap<String, AnchorSourceJoin> {
609        let mut roots = std::collections::BTreeMap::new();
610        let Some(root) = self.workspace_root.as_deref() else {
611            return roots;
612        };
613        let Ok(configs) = crate::pipeline_store::load_pipeline_configs(root) else {
614            return roots;
615        };
616        for record in configs.bindings.iter().filter(|r| r.mem == mem) {
617            for source in &record.config.sources {
618                roots
619                    .entry(source.name.clone())
620                    .or_insert_with(|| AnchorSourceJoin {
621                        pointer: source.pointer.clone(),
622                        preparation: source.preparation.clone(),
623                        source: source.clone(),
624                        deny_paths: record.config.deny_paths.clone(),
625                    });
626            }
627        }
628        roots
629    }
630
631    /// Reverse anchor lookup: every `(entity_id, anchor)` across all mems
632    /// whose anchor references `artifact_path`. This is the query the
633    /// rebuilt check-realization hook consumes — given the file an agent
634    /// just edited, which entities anchored to it. A `span`/`file`/`tree`
635    /// anchor references the path when its base path (locator suffix
636    /// `@commit` / `#span` stripped) equals the path, or — for a `tree`
637    /// grain — when the path lies under the tree. Path-shaped grains only;
638    /// `url` / `entity` anchors are matched by exact base equality.
639    pub fn anchors_referencing_artifact(
640        &self,
641        artifact_path: &str,
642    ) -> Vec<(EntityId, crate::anchor::Anchor)> {
643        let mut out = Vec::new();
644        for mount in &self.mounts {
645            let Ok(Some(bytes)) = mount.backend.read_anchors_sidecar() else {
646                continue;
647            };
648            let Ok(sc) = crate::anchor::AnchorSidecar::from_bytes(&bytes) else {
649                continue;
650            };
651            // Source-dialect anchors (decision 26) reference the same
652            // artifact under its pointer-joined workspace form — match both.
653            let source_roots = self.anchor_source_roots(&mount.mount.mem);
654            for (eid, anchors) in &sc.entities {
655                for a in anchors {
656                    // The shared decision-29 candidate rule: the join
657                    // candidate exists only where the rule produces one (a
658                    // climbing `../…` artifact never joins); the
659                    // workspace-relative form is the `anchor_references_path`
660                    // arm below.
661                    let joined = a
662                        .source
663                        .as_deref()
664                        .and_then(|name| source_roots.get(name))
665                        .map(|join| {
666                            artifact_candidates(&join.pointer, anchor_base_path(&a.artifact))
667                        })
668                        .and_then(|mut c| (c.len() > 1).then(|| c.remove(0)));
669                    if anchor_references_path(a, artifact_path)
670                        || joined.is_some_and(|j| {
671                            path_references(
672                                &j,
673                                a.grain == crate::anchor::AnchorGrain::Tree,
674                                artifact_path,
675                            )
676                        })
677                    {
678                        out.push((EntityId(eid.clone()), a.clone()));
679                    }
680                }
681            }
682        }
683        out
684    }
685
686    /// Every `(entity_id, resolved anchor)` in `mem`, read from its anchors
687    /// sidecar once and each paired with its **live** resolution state (the
688    /// same observation [`Self::entity_anchors_resolved`] produces per entity,
689    /// computed here mem-wide in a single sidecar read). Empty for an unknown
690    /// mem, a backend that persists no anchors, or a mem with none.
691    ///
692    /// Additive read surface: the durable data is unchanged; `state` is the
693    /// [`crate::anchor::resolve_anchor`] outcome against an observation the
694    /// engine produces — the working tree for a `path`-namespace anchor, the
695    /// live graph for an `entity` one — or `None` when unobserved (never
696    /// fabricated). The verify pipeline consumes it to adjudicate a mem's
697    /// anchors against the source; audit/health can reuse it.
698    /// Whether `mem`'s anchor sidecar records ANY anchor row — the cheap
699    /// existence check [`Self::mem_anchors_resolved`] cannot serve: that
700    /// walk OBSERVES every anchor (hashing live source artifacts, and for
701    /// path anchors enumerating the facet's file scope) when a caller only
702    /// needs to know the sidecar is non-empty. Parses the sidecar and stops
703    /// there; false for an unknown mem, a backend without anchors, or an
704    /// empty sidecar — the same population `mem_anchors_resolved` would
705    /// report empty for, minus the observation cost.
706    pub fn mem_has_anchors(&self, mem: &str) -> bool {
707        let Some(mount) = self.mounts.iter().find(|m| m.mount.mem == mem) else {
708            return false;
709        };
710        let Ok(Some(bytes)) = mount.backend.read_anchors_sidecar() else {
711            return false;
712        };
713        let Ok(sc) = crate::anchor::AnchorSidecar::from_bytes(&bytes) else {
714            return false;
715        };
716        sc.entities.values().any(|anchors| !anchors.is_empty())
717    }
718
719    pub fn mem_anchors_resolved(&self, mem: &str) -> Vec<(EntityId, ResolvedAnchor)> {
720        self.mem_anchors_resolved_with(mem, &SuppliedObservations::new())
721    }
722
723    /// [`Self::mem_anchors_resolved`] with observer-supplied observations
724    /// for the grains the engine cannot observe itself (`url`), keyed by
725    /// artifact. Rows without a supplied observation resolve as they would
726    /// without the map.
727    pub fn mem_anchors_resolved_with(
728        &self,
729        mem: &str,
730        supplied: &SuppliedObservations,
731    ) -> Vec<(EntityId, ResolvedAnchor)> {
732        let Some(mount) = self.mounts.iter().find(|m| m.mount.mem == mem) else {
733            return Vec::new();
734        };
735        let Ok(Some(bytes)) = mount.backend.read_anchors_sidecar() else {
736            return Vec::new();
737        };
738        let Ok(sc) = crate::anchor::AnchorSidecar::from_bytes(&bytes) else {
739            return Vec::new();
740        };
741        let mut out = Vec::new();
742        let source_roots = self.anchor_source_roots(mem);
743        for (eid, anchors) in &sc.entities {
744            for anchor in anchors {
745                out.push((
746                    EntityId(eid.clone()),
747                    self.resolve_one(anchor.clone(), &source_roots, supplied),
748                ));
749            }
750        }
751        out
752    }
753
754    /// Standalone anchor verification — "do my sources still say what I
755    /// recorded?" for one mem, regardless of how it was built. Walks the
756    /// mem's anchor sidecar through the shared per-anchor mechanism
757    /// ([`Self::observe_anchor`] via [`Self::mem_anchors_resolved`]) and
758    /// classifies every anchor into the report vocabulary: `resolved`
759    /// (source present, hash matches or non-hash class), `drifted`
760    /// (present, hash differs, stability `stable`), `recheck` (hash
761    /// differs under `unstable`, or a hash is missing on either side),
762    /// `unresolvable` (source absent, or a grain/medium the mechanism
763    /// does not reach — never fabricated into drift). Read-only on mem
764    /// content: pure sidecar read + filesystem observation, no commit on
765    /// any backend. A mem with no anchors returns an empty report.
766    pub fn verify_mem_anchors(&self, mem: &str) -> Result<MemAnchorVerification, EngineError> {
767        self.verify_mem_anchors_with(mem, &SuppliedObservations::new())
768    }
769
770    /// [`Self::verify_mem_anchors`] with observer-supplied observations
771    /// (the `--observations` file of `memstead verify-anchors`). A `url`
772    /// row whose artifact has a supplied observation adjudicates through
773    /// the shared funnel exactly like a file row; a supplied row matching
774    /// no `url` anchor of the mem is reported in `unmatched_observations`
775    /// and changes nothing. The report's `recordable_observations` are the
776    /// `last_observed` records the caller commits with
777    /// [`Self::record_anchor_observations`] — the verification itself
778    /// writes nothing.
779    pub fn verify_mem_anchors_with(
780        &self,
781        mem: &str,
782        supplied: &SuppliedObservations,
783    ) -> Result<MemAnchorVerification, EngineError> {
784        if !self.mem_router.is_visible(mem) {
785            return Err(self.unknown_mem_error(mem));
786        }
787        let now = self.now_iso();
788        let mut report = MemAnchorVerification {
789            mem: mem.to_string(),
790            unreconciled: self
791                .entity_set_is_reconcilable(mem)
792                .err()
793                .map(str::to_string),
794            ..Default::default()
795        };
796        let reconciled = report.unreconciled.is_none();
797        let mut matched: std::collections::BTreeSet<String> = std::collections::BTreeSet::new();
798        for (eid, resolved) in self.mem_anchors_resolved_with(mem, supplied) {
799            let is_url = resolved.anchor.grain == crate::anchor::AnchorGrain::Url;
800            let supplied_here = is_url && supplied.contains_key(&resolved.anchor.artifact);
801            if supplied_here {
802                matched.insert(resolved.anchor.artifact.clone());
803            }
804            let observed_at = resolved.observed_at.clone();
805            let unobserved_for_days = observed_at
806                .as_deref()
807                .and_then(|at| crate::anchor::days_between(at, &now));
808            if let (true, Some(state), Some(at)) = (supplied_here, resolved.state, &observed_at) {
809                report.recordable_observations.push(RecordedObservation {
810                    entity: eid.to_string(),
811                    artifact: resolved.anchor.artifact.clone(),
812                    observation: crate::anchor::AnchorObservation {
813                        at: at.clone(),
814                        hash: resolved.observed_hash.clone(),
815                        state,
816                    },
817                });
818            }
819            // The entity end first: a row whose holder is gone is adjudicated
820            // against its artifact alone otherwise, and a matching hash then
821            // reports it as `resolved` for an entity that does not exist.
822            if reconciled && self.entity_is_absent(&eid) {
823                report.dangling += 1;
824                report.anchors.push(VerifiedAnchor {
825                    entity_id: eid.to_string(),
826                    artifact: resolved.anchor.artifact.clone(),
827                    grain: resolved.anchor.grain.as_wire().to_string(),
828                    class: resolved.anchor.class.as_wire().to_string(),
829                    state: "dangling".to_string(),
830                    observed_hash: resolved.observed_hash,
831                    observed_at,
832                    unobserved_for_days,
833                    observation_supplied: supplied_here,
834                });
835                continue;
836            }
837            let state = match resolved.state {
838                Some(crate::anchor::AnchorState::Resolves) => {
839                    report.resolved += 1;
840                    "resolved"
841                }
842                Some(crate::anchor::AnchorState::Drifted) => {
843                    report.drifted += 1;
844                    "drifted"
845                }
846                Some(crate::anchor::AnchorState::Recheck) => {
847                    report.recheck += 1;
848                    "recheck"
849                }
850                Some(crate::anchor::AnchorState::Orphaned) => {
851                    report.unresolvable += 1;
852                    "unresolvable"
853                }
854                // Split from `unresolvable` (03/05, criterion 2): the artifact
855                // being GONE is a measurement; the pass not reaching the
856                // artifact at all is the absence of one, and the repairs
857                // differ.
858                None => {
859                    report.unobserved += 1;
860                    "unobserved"
861                }
862            };
863            report.anchors.push(VerifiedAnchor {
864                entity_id: eid.to_string(),
865                artifact: resolved.anchor.artifact.clone(),
866                grain: resolved.anchor.grain.as_wire().to_string(),
867                class: resolved.anchor.class.as_wire().to_string(),
868                state: state.to_string(),
869                observed_hash: resolved.observed_hash,
870                observed_at,
871                unobserved_for_days,
872                observation_supplied: supplied_here,
873            });
874        }
875        report.unmatched_observations = supplied
876            .keys()
877            .filter(|artifact| !matched.contains(*artifact))
878            .cloned()
879            .collect();
880        Ok(report)
881    }
882
883    /// Mem names the engine knows about, in declaration order.
884    /// Cheap; useful for callers that need to enumerate before
885    /// dispatching by mem.
886    pub fn mem_names(&self) -> Vec<&str> {
887        self.mounts.iter().map(|m| m.mount.mem.as_str()).collect()
888    }
889
890    /// Derivation-staleness report for one mem (agent-trust plan 12):
891    /// every EXPLICIT edge whose rel-type the mem's schema declares
892    /// `derivation: true`, compared against its recorded baseline.
893    /// Baseline differs from the target's current hash → `stale`;
894    /// no baseline recorded (edge predates the declaration, or was
895    /// load-derived) → `unbaselined`, distinctly — never fabricated
896    /// as fresh or stale. Fresh edges are not reported. A mem whose
897    /// schema declares no derivation rel-types returns the empty
898    /// report; an unreadable sidecar reads as empty (every edge
899    /// unbaselined) rather than an error.
900    pub fn derivation_report(
901        &self,
902        mem: &str,
903    ) -> Result<Vec<crate::ops::health::DerivationFinding>, EngineError> {
904        if !self.mem_router.is_visible(mem) {
905            return Err(self.unknown_mem_error(mem));
906        }
907        let Some(schema) = self.schemas.get(mem) else {
908            return Ok(Vec::new());
909        };
910        let declared: std::collections::HashSet<&str> = schema
911            .manifest
912            .relationships
913            .definitions
914            .iter()
915            .filter(|d| d.derivation)
916            .map(|d| d.name.as_str())
917            .collect();
918        if declared.is_empty() {
919            return Ok(Vec::new());
920        }
921        let sidecar = self
922            .mounts
923            .iter()
924            .find(|m| m.mount.mem == mem)
925            .and_then(|m| {
926                m.backend
927                    .read_entity(Path::new(crate::derivation::DERIVATION_SIDECAR_PATH))
928                    .ok()
929                    .flatten()
930            })
931            .and_then(|bytes| crate::derivation::DerivationSidecar::from_bytes(&bytes).ok())
932            .unwrap_or_default();
933
934        let mut out = Vec::new();
935        let mut sources: Vec<&crate::entity::Entity> = self
936            .store
937            .all_entities()
938            .filter(|e| !e.stub && e.id.mem() == mem)
939            .collect();
940        sources.sort_by(|a, b| a.id.as_ref().cmp(b.id.as_ref()));
941        for entity in sources {
942            for edge in self.store.outgoing(&entity.id) {
943                if !declared.contains(edge.rel_type.as_str())
944                    || edge.source != crate::store::EdgeSource::Explicit
945                {
946                    continue;
947                }
948                let current = self
949                    .store
950                    .get(&edge.target)
951                    .map(|t| t.content_hash.clone())
952                    .unwrap_or_default();
953                match sidecar.get(entity.id.as_ref(), &edge.rel_type, edge.target.as_ref()) {
954                    None => out.push(crate::ops::health::DerivationFinding {
955                        source: entity.id.clone(),
956                        rel_type: edge.rel_type.clone(),
957                        target: edge.target.clone(),
958                        state: "unbaselined".to_string(),
959                        baseline: None,
960                        current,
961                    }),
962                    Some(baseline) if baseline != current => {
963                        out.push(crate::ops::health::DerivationFinding {
964                            source: entity.id.clone(),
965                            rel_type: edge.rel_type.clone(),
966                            target: edge.target.clone(),
967                            state: "stale".to_string(),
968                            baseline: Some(baseline.to_string()),
969                            current,
970                        })
971                    }
972                    Some(_) => {}
973                }
974            }
975        }
976        Ok(out)
977    }
978
979    /// Public-shape mount record for `mem`, or `None` for an unknown
980    /// mem.
981    ///
982    /// Surfaces the operator-facing
983    /// [`crate::workspace::Mount`] (mem name, schema pin, storage
984    /// reference, capability, lifecycle, cross_linkable) so MCP / CLI
985    /// handlers can branch on backend-specific shapes via
986    /// [`crate::workspace::MountStorage`] when they need accessors
987    /// that don't make sense on every backend (e.g. gitdir / branch
988    /// for `memstead_health { include_config: true }`'s git-class
989    /// payload). Backends that want the equivalent of full's
990    /// `engine.gitdir_for(mem)` match
991    /// `engine.mount(mem).map(|m| &m.storage)` against
992    /// `MountStorage::GitBranch { gitdir, branch }` and walk
993    /// directly — keeps the engine surface backend-neutral.
994    ///
995    /// Counterpart to [`Self::mem_names`] which lists every mount.
996    pub fn mount(&self, mem: &str) -> Option<&crate::workspace::Mount> {
997        self.mounts
998            .iter()
999            .find(|m| m.mount.mem == mem)
1000            .map(|m| &m.mount)
1001    }
1002
1003    /// Orphan count attributed to each mem's pinned schema, over the
1004    /// given `orphan_ids` (the caller pre-filters them by any mem scope).
1005    /// Lets a health surface show that ingest-mem isolates (orphans by
1006    /// design) and code-mem debt land in different schema buckets rather
1007    /// than one blended, misleading total. Mems with no settled pin
1008    /// bucket under the empty string.
1009    pub fn orphans_by_schema(
1010        &self,
1011        orphan_ids: &[EntityId],
1012    ) -> std::collections::BTreeMap<String, usize> {
1013        let mut by_schema = std::collections::BTreeMap::new();
1014        for id in orphan_ids {
1015            let schema = self
1016                .store()
1017                .get(id)
1018                .and_then(|e| self.mount(&e.mem))
1019                .and_then(|m| m.schema.as_ref().map(|s| s.as_display()))
1020                .unwrap_or_default();
1021            *by_schema.entry(schema).or_insert(0) += 1;
1022        }
1023        by_schema
1024    }
1025
1026    /// Community count attributed to each schema across `mems`: a cluster
1027    /// counts toward every schema whose mems it touches, so these figures
1028    /// can sum above the global community count — the same "touches"
1029    /// semantic as the mem-scoped count. Per-schema dedup keeps a cluster
1030    /// touching two mems of one schema from being counted twice.
1031    pub fn communities_by_schema(
1032        &self,
1033        mems: &[String],
1034    ) -> std::collections::BTreeMap<String, usize> {
1035        let louvain = self.communities();
1036        let mut buckets: std::collections::BTreeMap<String, std::collections::BTreeSet<String>> =
1037            std::collections::BTreeMap::new();
1038        for name in mems {
1039            let schema = self
1040                .mount(name)
1041                .and_then(|m| m.schema.as_ref().map(|s| s.as_display()))
1042                .unwrap_or_default();
1043            let clusters = crate::graph::community::clusters_in_mem(self.store(), louvain, name);
1044            buckets.entry(schema).or_default().extend(clusters);
1045        }
1046        buckets
1047            .into_iter()
1048            .map(|(schema, set)| (schema, set.len()))
1049            .collect()
1050    }
1051
1052    /// All mounts the engine knows about, in declaration order.
1053    /// Counterpart to [`Self::mem_names`] when the caller needs
1054    /// the full mount shape (e.g. to enumerate by storage variant).
1055    pub fn mounts(&self) -> Vec<&crate::workspace::Mount> {
1056        self.mounts.iter().map(|m| &m.mount).collect()
1057    }
1058
1059    /// Names of mems whose mount declares
1060    /// [`crate::workspace::MountCapability::Write`], in declaration
1061    /// order. Convenience over `mounts().iter().filter(...).map(...)`
1062    /// for handlers that gate by writable status (`memstead_health`,
1063    /// `memstead_overview`'s mem roster, the lifecycle tools'
1064    /// candidate list). Read-only mounts (archive backends) are
1065    /// excluded.
1066    pub fn writable_mem_names(&self) -> Vec<&str> {
1067        self.mounts
1068            .iter()
1069            .filter(|m| m.mount.capability == MountCapability::Write)
1070            .map(|m| m.mount.mem.as_str())
1071            .collect()
1072    }
1073
1074    /// The default writable mem — the target a mutation lands in when
1075    /// it omits `mem`. `None` when no writable mem is mounted.
1076    ///
1077    /// Defined as the **first writable mount in declaration order**, i.e.
1078    /// the seed / earliest-created writable mem. This is a *stable*
1079    /// designation, not a function of the current name set: new mems
1080    /// register via `register_writable_mem`, which pushes onto the end
1081    /// of the mount list (and `mounts.json` preserves that order across
1082    /// reboots), so creating an additional mem never moves the default
1083    /// — even one whose name sorts ahead alphabetically. Deleting the
1084    /// current default promotes the next-earliest writable mem; that is
1085    /// the only thing that shifts it. Both the MCP `resolve_mem` and the
1086    /// CLI's omitted-`--mem` path resolve through here so the two
1087    /// surfaces always agree (the
1088    /// pre-fix MCP path read `writable_mems().iter().next()` off an
1089    /// unordered `HashSet`, which silently retargeted writes when a second
1090    /// mem appeared).
1091    pub fn default_writable_mem(&self) -> Option<&str> {
1092        self.mounts
1093            .iter()
1094            .find(|m| m.mount.capability == MountCapability::Write)
1095            .map(|m| m.mount.mem.as_str())
1096    }
1097
1098    /// On-disk folder path for a folder-backed mount, or `None` for
1099    /// any other backend (git-branch, archive) or unknown mem.
1100    /// Convenience over `engine.mount(mem).map(|m| &m.storage)` +
1101    /// matching on `MountStorage::Folder { path }`. Used by
1102    /// handlers that need a filesystem path for a folder mem
1103    /// (e.g. `memstead_health { include_config: true }`'s
1104    /// `mems[].vcs.worktree` field for folder mounts).
1105    pub fn folder_path_for_mem(&self, mem: &str) -> Option<&Path> {
1106        match self.mount(mem).map(|m| &m.storage) {
1107            Some(crate::workspace::MountStorage::Folder { path }) => Some(path.as_path()),
1108            _ => None,
1109        }
1110    }
1111
1112    /// Runtime snapshot of writable / visible mems. Handlers that
1113    /// need the writable roster (`memstead_health`'s `writable_mems` /
1114    /// `read_mems`), per-mem origin tag (`include_config:
1115    /// true`'s `mems[].origin`), or visibility check
1116    /// (`memstead_overview`'s mem list, the lifecycle tools' collision
1117    /// guard) consume the router here. Returned by reference — the
1118    /// `Arc` is held on the engine; callers that need a clonable
1119    /// handle can `Arc::clone` the engine's field directly when that
1120    /// surface arrives.
1121    pub fn mem_router(&self) -> &MemRouterSnapshot {
1122        &self.mem_router
1123    }
1124
1125    /// Resolve the gitdir for a writable mem. Used by `memstead_health
1126    /// { include_config: true }` to surface per-mem `vcs.gitdir`
1127    /// so outer-repo bookkeeping clients can `git -C <gitdir>` per
1128    /// mem without hardcoding the layout.
1129    ///
1130    /// - `EngineError::UnknownMem` when the name does not resolve.
1131    /// - `EngineError::Mem` when the mount's storage is not
1132    ///   git-branch-backed (folder, archive — they have no gitdir).
1133    pub fn gitdir_for(&self, mem_name: &str) -> Result<PathBuf, EngineError> {
1134        let m = self
1135            .mount(mem_name)
1136            .ok_or_else(|| self.unknown_mem_error(mem_name))?;
1137        match &m.storage {
1138            MountStorage::GitBranch { gitdir, .. } => Ok(gitdir.clone()),
1139            MountStorage::Folder { .. } | MountStorage::Archive { .. } | MountStorage::InMemory => {
1140                Err(EngineError::Mem(format!(
1141                    "mem '{mem_name}' has no resolved gitdir"
1142                )))
1143            }
1144        }
1145    }
1146
1147    /// Resolve the worktree for a writable mem. Used by
1148    /// `memstead_health { include_config: true }` to surface per-mem
1149    /// `vcs.worktree`.
1150    ///
1151    /// - `EngineError::UnknownMem` when the name does not resolve.
1152    /// - `EngineError::Mem` when the mount's backend has no
1153    ///   worktree concept (git-branch with no working tree, archive).
1154    ///
1155    /// Folder mounts surface their on-disk path. Git-branch mounts
1156    /// follow the `dir: Some(...)` composition pattern: when the
1157    /// workspace root contains a folder named after the mem with a
1158    /// `.memstead/config.json` marker, that folder is the worktree
1159    /// (disk-shape composition). Otherwise — pure mem-repo-backed
1160    /// — return Err.
1161    pub fn worktree_for(&self, mem_name: &str) -> Result<PathBuf, EngineError> {
1162        let m = self
1163            .mount(mem_name)
1164            .ok_or_else(|| self.unknown_mem_error(mem_name))?;
1165        match &m.storage {
1166            MountStorage::Folder { path } => Ok(path.clone()),
1167            MountStorage::GitBranch { .. } => {
1168                if let Some(root) = self.workspace_root.as_deref() {
1169                    let candidate = root.join(mem_name);
1170                    if candidate
1171                        .join(crate::mem::MEM_META_DIR)
1172                        .join("config.json")
1173                        .is_file()
1174                    {
1175                        return Ok(candidate.canonicalize().unwrap_or(candidate));
1176                    }
1177                }
1178                Err(EngineError::Mem(format!(
1179                    "mem '{mem_name}' has no working tree (mem-repo-backed)"
1180                )))
1181            }
1182            MountStorage::Archive { .. } => Err(EngineError::Mem(format!(
1183                "mem '{mem_name}' is archive-backed and has no worktree"
1184            ))),
1185            MountStorage::InMemory => Err(EngineError::Mem(format!(
1186                "mem '{mem_name}' is in-memory and has no worktree"
1187            ))),
1188        }
1189    }
1190
1191    /// Per-mem `.memstead/config.json` payload, when available. Used
1192    /// by `memstead_health { include_config: true }` to surface the
1193    /// opaque `write_guidance` map and the catch-all `extra` fields
1194    /// per mem.
1195    ///
1196    /// Folder-backed mounts return `Some(&MemConfig)` when
1197    /// `<path>/.memstead/config.json` parsed cleanly at construction.
1198    /// Git-branch and archive backends return `None` until the
1199    /// read-from-storage-backend path lifts (the V1 unified engine
1200    /// loads configs only from folder layouts; the file lives
1201    /// inside the gitdir / archive for the other backends and
1202    /// needs a backend-level read primitive).
1203    ///
1204    /// Unknown mem names return `None` (no error variant — the
1205    /// accessor is intentionally lenient because memstead_health emits
1206    /// an empty detail block per missing config rather than
1207    /// aborting the call).
1208    pub fn mem_config_for(&self, mem: &str) -> Option<&memstead_schema::config::MemConfig> {
1209        self.mounts
1210            .iter()
1211            .find(|m| m.mount.mem == mem)
1212            .and_then(|m| m.mem_config.as_ref())
1213    }
1214
1215    /// The authoring-provenance payload an installed mem carries, read
1216    /// from the archive's `.memstead/provenance.json` at construction.
1217    /// `None` when the mem carries none (a pre-provenance archive, a
1218    /// runtime-created mem, or a backend that does not surface one) —
1219    /// the read path reports provenance as absent. Unknown mem names
1220    /// return `None`.
1221    pub fn archive_provenance_for(&self, mem: &str) -> Option<&memstead_schema::ArchiveProvenance> {
1222        self.mounts
1223            .iter()
1224            .find(|m| m.mount.mem == mem)
1225            .and_then(|m| m.archive_provenance.as_ref())
1226    }
1227
1228    /// Iterate `(mem_name, &MemConfig)` for every mount whose
1229    /// mem-config payload loaded at construction. Used by callers
1230    /// that walk every writable mount's config (`memstead health`'s
1231    /// per-mem dump, the workspace-dump CLI). The yielded `&str` is
1232    /// the authoritative mem leaf from the mount record.
1233    ///
1234    /// Folder-backed mounts yield when their `.memstead/config.json`
1235    /// parsed cleanly. Git-branch and archive backends are silent in
1236    /// V1 (the same deferred-read-from-storage gap that
1237    /// [`Self::mem_config_for`] documents).
1238    pub fn mem_configs_named(
1239        &self,
1240    ) -> impl Iterator<Item = (&str, &memstead_schema::config::MemConfig)> {
1241        self.mounts
1242            .iter()
1243            .filter_map(|m| m.mem_config.as_ref().map(|c| (m.mount.mem.as_str(), c)))
1244    }
1245
1246    /// Every configured mount, with its config when one was readable.
1247    ///
1248    /// The counterpart to [`Self::mem_configs_named`], whose contract is "mems
1249    /// WITH config" and which is therefore right to omit the rest. The trouble
1250    /// was that callers wanting to enumerate mounts reached for it anyway: a
1251    /// folder mount whose directory is gone returns no config rather than an
1252    /// error, boot stores none, and the mount became invisible to every one of
1253    /// them. Nine call sites shared that blind spot, and the omission was a
1254    /// side effect of config readability rather than anything about the mount
1255    /// (04/05, criteria 1 and 8).
1256    ///
1257    /// Callers that genuinely want only configured mems keep using the other
1258    /// one; drivers that want every mount ask for every mount.
1259    pub fn mounts_with_optional_config(
1260        &self,
1261    ) -> impl Iterator<Item = (&str, Option<&memstead_schema::config::MemConfig>)> {
1262        self.mounts
1263            .iter()
1264            .map(|m| (m.mount.mem.as_str(), m.mem_config.as_ref()))
1265    }
1266
1267    /// Resolved `Arc<Schema>` for a writable mem by name. `None`
1268    /// when the name is not a registered mount.
1269    ///
1270    /// Cheap — `Arc::clone` over the per-mem schema map. Resolved
1271    /// schemas are stored in `HashMap<String, Arc<Schema>>` so the
1272    /// lookup is a single hash hit + clone.
1273    pub fn schema_for(&self, mem: &str) -> Option<std::sync::Arc<memstead_schema::Schema>> {
1274        self.schemas.get(mem).cloned()
1275    }
1276
1277    /// Cached current branch-tip cursor (typically a 40-char hex
1278    /// SHA for git-branch backends; `None` for fresh mems or
1279    /// backends that don't track a head — folder / archive).
1280    ///
1281    /// The value is the per-mount `last_known_head`, seeded at
1282    /// construction by `backend.current_head()` and refreshed by
1283    /// [`Self::reload_if_stale`] / mutation paths after a
1284    /// successful commit.
1285    ///
1286    /// - `EngineError::UnknownMem` when the name does not resolve.
1287    pub fn mem_head_sha(&self, mem_name: &str) -> Result<Option<String>, EngineError> {
1288        let m = self
1289            .mounts
1290            .iter()
1291            .find(|m| m.mount.mem == mem_name)
1292            .ok_or_else(|| self.unknown_mem_error(mem_name))?;
1293        Ok(m.last_known_head.clone())
1294    }
1295
1296    /// Whether a sibling writer has advanced this mem's backend past
1297    /// the engine's cached `last_known_head` — a read-only drift probe
1298    /// that does **not** reload (unlike [`Self::reload_if_stale`]). One
1299    /// `backend.current_head()` read compared against the cached cursor;
1300    /// the comparison clears once the engine re-reads (a `reload` /
1301    /// `reload_if_stale` refreshes `last_known_head` to the live tip).
1302    ///
1303    /// Only git-branch backends track a head, so folder / archive /
1304    /// in-memory mounts always report `false`. A backend that errors on
1305    /// the probe (transient refdb hiccup) reports `false` rather than
1306    /// surfacing the error — drift is advisory, and the next real
1307    /// operation's reload path is the authoritative sync.
1308    ///
1309    /// - `EngineError::UnknownMem` when the name does not resolve.
1310    pub fn mem_drifted(&self, mem_name: &str) -> Result<bool, EngineError> {
1311        let m = self
1312            .mounts
1313            .iter()
1314            .find(|m| m.mount.mem == mem_name)
1315            .ok_or_else(|| self.unknown_mem_error(mem_name))?;
1316        let live = m.backend.current_head().ok().flatten();
1317        Ok(live != m.last_known_head)
1318    }
1319
1320    /// Workspace root the engine booted from, when one is known.
1321    /// `None` for engines built directly from a mount list (tests,
1322    /// ad-hoc consumers). Set by [`Self::from_workspace_root`] and
1323    /// the full counterpart.
1324    pub fn workspace_root(&self) -> Option<&Path> {
1325        self.workspace_root.as_deref()
1326    }
1327
1328    /// Typed warnings surfaced during mem load — drift findings
1329    /// the loader pipeline collects per entity. Empty for V1; the
1330    /// accessor surfaces them so handlers can merge into health
1331    /// summaries uniformly.
1332    pub fn load_warnings(&self) -> &[WarningHint] {
1333        &self.load_warnings
1334    }
1335
1336    /// The quarantine roster: mems that failed their mem-level boot
1337    /// step and serve nothing until repaired + reloaded. Empty on a
1338    /// fully healthy workspace. Surfaced on overview and health.
1339    pub fn quarantined_mems(&self) -> &[crate::engine::QuarantinedMem] {
1340        &self.quarantined
1341    }
1342
1343    /// The quarantine entry for `mem`, when it is quarantined.
1344    pub fn quarantine_reason(&self, mem: &str) -> Option<&crate::engine::QuarantinedMem> {
1345        self.quarantined.iter().find(|q| q.mount.mem == mem)
1346    }
1347
1348    /// Whether the store's entity set for `mem` can be trusted to answer the
1349    /// question "does this entity still exist?".
1350    ///
1351    /// WHY this is a question and not an assumption: an anchor sidecar is
1352    /// keyed by entity id, and a key with no entity behind it is a dangling
1353    /// row (consistency-sweep 03/02). Detecting one means reading a NEGATIVE
1354    /// from the store, and a negative is only evidence when the store is known
1355    /// to hold everything the mem has. Four states break that, and each of
1356    /// them would otherwise turn every anchor in the mem into a false dangling
1357    /// report: the mem is not mounted at all, it is quarantined (serving
1358    /// nothing), its lazy load has not run yet (mounted, entities absent), or
1359    /// a file in it failed to parse, in which case an id missing from the
1360    /// store may be a load failure rather than a deleted entity.
1361    ///
1362    /// The last case is deliberately COARSE for non-folder mounts: load-error
1363    /// paths are normalized to absolute only for folder mounts, so a
1364    /// git-branch mem's errors carry mem-relative paths that two mems can
1365    /// spell identically. Attributing them by name would be a guess, and a
1366    /// wrong guess here fabricates dangling rows. Any load error at all
1367    /// therefore blocks reconciliation for a non-folder mem. The caller states
1368    /// the block rather than skipping silently, which is the honest direction.
1369    pub fn entity_set_is_reconcilable(&self, mem: &str) -> Result<(), &'static str> {
1370        if self.quarantine_reason(mem).is_some() {
1371            return Err("the mem is quarantined and serves no entities");
1372        }
1373        let Some(mount) = self.mounts.iter().find(|m| m.mount.mem == mem) else {
1374            return Err("the mem is not mounted here");
1375        };
1376        if mount.deferred {
1377            return Err("the mem's lazy entity load has not run this session");
1378        }
1379        if self.load_errors.is_empty() {
1380            return Ok(());
1381        }
1382        match &mount.mount.storage {
1383            crate::workspace::MountStorage::Folder { path } => {
1384                if self.load_errors.iter().any(|(p, _)| p.starts_with(path)) {
1385                    Err(
1386                        "a file in this mem failed to parse, so an id missing from the store may be a load failure rather than a deleted entity",
1387                    )
1388                } else {
1389                    Ok(())
1390                }
1391            }
1392            _ => Err(
1393                "this workspace has files that failed to parse, and their paths cannot be attributed to one mem",
1394            ),
1395        }
1396    }
1397
1398    /// Whether `id` names an entity this mem no longer holds. A STUB counts
1399    /// as missing: a stub is the placeholder an unresolved wiki-link target
1400    /// leaves behind, not an entity anyone wrote, so an anchor keyed to one
1401    /// is dangling exactly as if nothing were there.
1402    ///
1403    /// Only meaningful once [`Self::entity_set_is_reconcilable`] says yes.
1404    pub fn entity_is_absent(&self, id: &EntityId) -> bool {
1405        self.store.get(id).is_none_or(|e| e.stub)
1406    }
1407
1408    /// Mems whose lazy entity load is still DEFERRED — on the mount
1409    /// roster with a resolved schema pin, but with no entities in the
1410    /// store yet. Read surfaces that render per-mem counts or
1411    /// distributions consult this: a count over a deferred mem's slice
1412    /// of the store is a count over nothing, and rendering it as a
1413    /// bare zero is the silent absence the lazy-mount contract forbids.
1414    /// Either trigger the load ([`Self::ensure_mems_loaded`]) or render
1415    /// the load state explicitly.
1416    pub fn deferred_mems(&self) -> Vec<&str> {
1417        self.mounts
1418            .iter()
1419            .filter(|m| m.deferred)
1420            .map(|m| m.mount.mem.as_str())
1421            .collect()
1422    }
1423
1424    /// Whether `mem` is a lazy mount whose entity load has not run yet.
1425    pub fn mem_is_deferred(&self, mem: &str) -> bool {
1426        self.mounts.iter().any(|m| m.deferred && m.mount.mem == mem)
1427    }
1428
1429    /// The typed error for a mem name that did not resolve to a
1430    /// serving mount: `MEM_QUARANTINED` (carrying the underlying boot
1431    /// failure and its repair command) when the mem is on the
1432    /// quarantine roster, `UNKNOWN_MEM` otherwise. Every lookup site
1433    /// that fails to find a mem routes here so a quarantined mem is
1434    /// never misreported as unknown — honest absence, with the reason.
1435    pub fn unknown_mem_error(&self, mem: &str) -> EngineError {
1436        match self.quarantine_reason(mem) {
1437            Some(q) => EngineError::MemQuarantined {
1438                mem: mem.to_string(),
1439                reason_code: q.reason_code.clone(),
1440                reason_message: q.reason_message.clone(),
1441            },
1442            None => EngineError::UnknownMem(mem.to_string()),
1443        }
1444    }
1445
1446    /// The workspace-level boot diagnosis a diagnostic-shell engine
1447    /// carries (`None` on ordinarily booted engines).
1448    pub fn boot_diagnosis(&self) -> Option<(&str, &str)> {
1449        self.boot_diagnosis
1450            .as_ref()
1451            .map(|(c, m)| (c.as_str(), m.as_str()))
1452    }
1453
1454    /// Build a mem-less diagnostic-shell engine for a workspace whose
1455    /// boot failed at the WORKSPACE level (nothing loadable — e.g. an
1456    /// unparseable store). It serves no mems and no entities; its one
1457    /// job is answering overview/health with the typed boot diagnosis
1458    /// so a session can always ask WHY the graph is gone — the MCP
1459    /// server serves this instead of exiting into `-32000 Connection
1460    /// closed` (degrade, never disappear).
1461    pub fn diagnostic_shell(reason_code: String, reason_message: String) -> Engine {
1462        let mut engine =
1463            Engine::from_mounts(Vec::new()).expect("an empty mount list always constructs");
1464        engine.boot_diagnosis = Some((reason_code, reason_message));
1465        engine
1466    }
1467
1468    /// Append boot-path quarantine entries recorded outside
1469    /// `from_mounts_inner` (backend-instantiation failures happen
1470    /// before the mount list reaches the engine constructor). Boot
1471    /// paths only — quarantine is a boot judgment, never a runtime
1472    /// mutation.
1473    pub fn extend_quarantine(&mut self, entries: Vec<crate::engine::QuarantinedMem>) {
1474        self.quarantined.extend(entries);
1475    }
1476
1477    // ---------------------------------------------------------------
1478    // Read-side delegates onto the kernel ops/graph functions.
1479    //
1480    // The mem-router engine exposed each of these directly so the
1481    // MCP layer could call them without reaching into the store. The
1482    // unified engine mirrors that surface so the MCP migration is a
1483    // straight rename rather than a re-architecture.
1484    //
1485    // Multi-mem cache strategy: per-mem community detection and
1486    // per-mem search indexes are unnecessary at this layer — the
1487    // engine-wide store already carries every mount's edges; Louvain
1488    // and tantivy run once across the union. `mem_schemas` for
1489    // health/search is the engine's existing `schemas` field as-is.
1490    // ---------------------------------------------------------------
1491
1492    /// Lazy community-detection cache. First call runs Louvain
1493    /// against the current store using one pinned schema for
1494    /// `community.{resolution, seed}` and the per-rel weights.
1495    /// Subsequent calls return the cached result. Mutations invalidate
1496    /// the cache via [`Self::invalidate_communities`].
1497    ///
1498    /// One detection run per engine. The partition is workspace-global,
1499    /// so it needs a single source for the Louvain parameters; that
1500    /// source is the schema of the lexicographically-first mem name —
1501    /// a stable key, so the partition is deterministic across processes
1502    /// even when mounts pin heterogeneous schemas. For a single-schema
1503    /// workspace every mem's schema is identical, so the choice of
1504    /// key is immaterial there.
1505    pub fn communities(&self) -> &LouvainOutput {
1506        // Generation sanity: a stored memo must match the live store
1507        // generation — the mutation hooks clear stale memos before any
1508        // read can arrive here. A mismatch would mean a mutation path
1509        // skipped its invalidation call (the exact silent-staleness
1510        // bug the generation exists to catch).
1511        if let Some((memo_key, _)) = self.community_memo.get() {
1512            debug_assert_eq!(
1513                *memo_key,
1514                self.derived_key(),
1515                "community memo key lags the engine — a mutation path missed invalidate_communities"
1516            );
1517        }
1518        &self
1519            .community_memo
1520            .get_or_init(|| (self.derived_key(), self.compute_communities()))
1521            .1
1522    }
1523
1524    /// The current validity key for derived-structure memos: store
1525    /// generation plus schemas epoch (see [`super::DerivedKey`]).
1526    pub fn derived_key(&self) -> super::DerivedKey {
1527        super::DerivedKey {
1528            store_generation: self.store.generation(),
1529            schemas_epoch: self.schemas_epoch,
1530        }
1531    }
1532
1533    fn compute_communities(&self) -> LouvainOutput {
1534        {
1535            // Select the parameter schema by a stable key (smallest
1536            // mem name) rather than unordered-map iteration, so the
1537            // partition does not vary between processes. Fall back to
1538            // the builtin default for the empty-mounts case (caller
1539            // still gets a valid empty Louvain result against an empty
1540            // store).
1541            let schema = self
1542                .schemas
1543                .iter()
1544                .min_by(|a, b| a.0.cmp(b.0))
1545                .map(|(_, s)| s.clone())
1546                .unwrap_or_else(Schema::builtin_default);
1547            let manifest = &schema.manifest;
1548            let resolution = manifest.community.resolution;
1549            let seed = manifest.community.seed;
1550            let schema_for_weights = schema.clone();
1551            detect_communities(&self.store, resolution, seed, move |rel_type| {
1552                schema_for_weights
1553                    .manifest
1554                    .relationships
1555                    .definitions
1556                    .iter()
1557                    .find(|d| d.name == rel_type)
1558                    .map(|d| d.default_weight as f64)
1559                    .unwrap_or(1.0)
1560            })
1561        }
1562    }
1563
1564    /// Drop the cached community detection result and the grounded
1565    /// labelling memo — unless the store still sits at the generation
1566    /// each memo was computed from (flywheel W8/01). The keep case is
1567    /// exactly the batch-rollback path: the restored snapshot restored
1568    /// the generation with it, so the memo describes the live state
1569    /// and recomputing it would be pure waste. Every real mutation
1570    /// bumps the generation first, so those clears behave as before.
1571    /// Coupling the labelling reset here means every site that already
1572    /// invalidates communities (all mutation paths, drift reload,
1573    /// quarantine attach/detach, apply-commit) invalidates the
1574    /// labelling too, so a stale label can never outlive the state
1575    /// change that moved it.
1576    pub fn invalidate_communities(&mut self) {
1577        let key = self.derived_key();
1578        if !matches!(self.community_memo.get(), Some((k, _)) if *k == key) {
1579            self.community_memo = OnceCell::new();
1580        }
1581        if !matches!(self.labelling_memo.get(), Some((k, _)) if *k == key) {
1582            self.labelling_memo = OnceCell::new();
1583        }
1584    }
1585
1586    /// The grounded labelling of one mem — `None` when its pinned
1587    /// schema declares no `relationships.labelling`. Computed on
1588    /// first access for every declaring mem, memoised until the next
1589    /// invalidation; generation-keyed like `community_memo`.
1590    pub fn mem_labelling(&self, mem: &str) -> Option<&crate::ops::labelling::MemLabelling> {
1591        // Generation sanity, same contract as `communities()`: a
1592        // stored memo must match the live derived key — a mismatch
1593        // means a mutation path missed invalidate_communities.
1594        if let Some((memo_key, _)) = self.labelling_memo.get() {
1595            debug_assert_eq!(
1596                *memo_key,
1597                self.derived_key(),
1598                "labelling memo key lags the engine — a mutation path missed invalidate_communities"
1599            );
1600        }
1601        let (_, map) = self.labelling_memo.get_or_init(|| {
1602            let mut out = std::collections::HashMap::new();
1603            for (mem_name, schema) in &self.schemas {
1604                if let Some(lab) = crate::ops::labelling::labelling_of(schema) {
1605                    out.insert(
1606                        mem_name.clone(),
1607                        crate::ops::labelling::compute_mem_labelling(
1608                            &self.store,
1609                            mem_name,
1610                            &lab.attack,
1611                        ),
1612                    );
1613                }
1614            }
1615            (self.derived_key(), out)
1616        });
1617        map.get(mem)
1618    }
1619
1620    /// One entity's served labelling view — `None` when the entity is
1621    /// a stub or its mem's schema declares no labelling; serving
1622    /// surfaces then keep their byte-identical payloads. The shape
1623    /// block is present exactly when the declaration carries a
1624    /// `support` walk.
1625    pub fn computed_labelling(
1626        &self,
1627        entity: &Entity,
1628    ) -> Option<crate::ops::labelling::LabellingView> {
1629        use crate::ops::labelling::{Label, compute_shape, labelling_of};
1630        if entity.stub {
1631            return None;
1632        }
1633        let schema = self.schemas.get(entity.mem.as_str())?;
1634        let lab = labelling_of(schema)?;
1635        let mem_lab = self.mem_labelling(entity.mem.as_str())?;
1636        let label = *mem_lab.labels.get(entity.id.0.as_str())?;
1637        let defeated_by = if label == Label::Defeated {
1638            mem_lab.accepted_attackers_of(entity.id.0.as_str())
1639        } else {
1640            Vec::new()
1641        };
1642        let undecided_by = if label == Label::Undecided {
1643            mem_lab.undecided_attackers_of(entity.id.0.as_str())
1644        } else {
1645            Vec::new()
1646        };
1647        let shape = lab.support.as_ref().map(|walk| {
1648            let label_of = |id: &EntityId| -> Option<Label> {
1649                self.mem_labelling(id.mem())
1650                    .and_then(|ml| ml.labels.get(id.0.as_str()).copied())
1651            };
1652            compute_shape(&self.store, &entity.id, walk, &label_of)
1653        });
1654        Some(crate::ops::labelling::LabellingView {
1655            label,
1656            defeated_by,
1657            undecided_by,
1658            shape,
1659        })
1660    }
1661
1662    /// The `labelling` health axis payload — per declaring mem:
1663    /// counts per label, the defeated list with its accepted
1664    /// attackers, the undecided list with its open attacker set, and
1665    /// the excluded cross-mem attack-edge count. One composer shared
1666    /// by the CLI health command and both MCP flavours.
1667    pub fn health_labelling_axis(&self, mem_filter: Option<&str>) -> serde_json::Value {
1668        use crate::ops::labelling::Label;
1669        let mut mems = serde_json::Map::new();
1670        let mut mem_names: Vec<&String> = self.schemas.keys().collect();
1671        mem_names.sort();
1672        for mem in mem_names {
1673            if let Some(v) = mem_filter
1674                && mem != v
1675            {
1676                continue;
1677            }
1678            let Some(ml) = self.mem_labelling(mem) else {
1679                continue;
1680            };
1681            let mut accepted = 0usize;
1682            let mut defeated: Vec<serde_json::Value> = Vec::new();
1683            let mut undecided: Vec<serde_json::Value> = Vec::new();
1684            for (id, label) in &ml.labels {
1685                match label {
1686                    Label::Accepted => accepted += 1,
1687                    Label::Defeated => defeated.push(serde_json::json!({
1688                        "id": id,
1689                        "defeated_by": ml.accepted_attackers_of(id),
1690                    })),
1691                    Label::Undecided => undecided.push(serde_json::json!({
1692                        "id": id,
1693                        "undecided_by": ml.undecided_attackers_of(id),
1694                    })),
1695                }
1696            }
1697            mems.insert(
1698                mem.clone(),
1699                serde_json::json!({
1700                    "counts": {
1701                        "accepted": accepted,
1702                        "defeated": defeated.len(),
1703                        "undecided": undecided.len(),
1704                    },
1705                    "defeated": defeated,
1706                    "undecided": undecided,
1707                    "cross_mem_edges_excluded": ml.cross_mem_edges_excluded,
1708                }),
1709            );
1710        }
1711        serde_json::Value::Object(mems)
1712    }
1713
1714    /// Real entities with no incoming or outgoing edges — leaf-declared
1715    /// types exempt (their edge-less entities are terminal by
1716    /// construction; see [`Self::leaf_population`]).
1717    pub fn orphans(&self) -> Vec<EntityId> {
1718        crate::graph::query::find_orphans_with_schemas(&self.store, &self.schemas)
1719    }
1720
1721    /// Count of real entities per leaf-declared type, keyed
1722    /// `<schema_ref>:<type>` — the visible population the orphan
1723    /// exemption covers.
1724    pub fn leaf_population(&self) -> std::collections::BTreeMap<String, usize> {
1725        crate::graph::query::leaf_population(&self.store, &self.schemas)
1726    }
1727
1728    /// Stub entities with their referencer ids.
1729    pub fn stubs(&self) -> Vec<(EntityId, Vec<EntityId>)> {
1730        crate::graph::query::find_stubs(&self.store)
1731    }
1732
1733    /// Top `limit` entities by total degree.
1734    pub fn most_connected(&self, limit: usize) -> Vec<crate::graph::query::Connectivity> {
1735        crate::graph::query::most_connected(&self.store, limit)
1736    }
1737
1738    /// Entities whose type's `required_outgoing` blocks are not yet
1739    /// satisfied. `mem_filter = None` scans every mem; `Some(v)`
1740    /// scans only that mem.
1741    pub fn missing_required_outgoing(
1742        &self,
1743        mem_filter: Option<&str>,
1744    ) -> Vec<crate::ops::health::MissingRequiredOutgoingReport> {
1745        crate::ops::health::collect_missing_required_outgoing(
1746            &self.store,
1747            mem_filter,
1748            &self.schemas,
1749        )
1750    }
1751
1752    /// Standing violations of declared `constraints` (the health
1753    /// `constraints` include) — every non-stub entity whose type
1754    /// declares constraints its current state violates, in
1755    /// deterministic `(mem, id)` order.
1756    pub fn constraint_findings(
1757        &self,
1758        mem_filter: Option<&str>,
1759    ) -> Vec<crate::ops::health::ConstraintFindingReport> {
1760        let check_provider = self.check_state_provider();
1761        crate::ops::health::collect_constraint_findings(
1762            &self.store,
1763            mem_filter,
1764            &self.schemas,
1765            Some(&check_provider),
1766        )
1767    }
1768
1769    /// The evaluated aggregate signals for one entity — `None` when
1770    /// the mem has no schema, the type is unknown or a stub, or the
1771    /// type declares no signals; serving surfaces then keep their
1772    /// byte-identical payloads.
1773    pub fn computed_signals(
1774        &self,
1775        entity: &Entity,
1776    ) -> Option<Vec<crate::ops::signals::ComputedSignal>> {
1777        if entity.stub {
1778            return None;
1779        }
1780        let schema = self.schemas.get(entity.mem.as_str())?;
1781        let td = schema.types.get(entity.entity_type.as_str())?;
1782        if td.signals.is_empty() {
1783            return None;
1784        }
1785        Some(crate::ops::signals::compute_signals(
1786            &self.store,
1787            td,
1788            &entity.id,
1789        ))
1790    }
1791
1792    /// Every entity carrying at least one signal above `none` — the
1793    /// include-gated `signals` health axis.
1794    pub fn signal_reports(
1795        &self,
1796        mem_filter: Option<&str>,
1797    ) -> Vec<crate::ops::health::SignalReport> {
1798        crate::ops::health::collect_signal_reports(&self.store, mem_filter, &self.schemas)
1799    }
1800
1801    /// The `signals` health axis payload — the entity roster plus
1802    /// per-level counts. One composer shared by the CLI health
1803    /// command and both MCP flavours so the axis cannot drift
1804    /// between surfaces.
1805    pub fn health_signals_axis(&self, mem_filter: Option<&str>) -> serde_json::Value {
1806        use memstead_schema::SignalLevel;
1807        let reports = self.signal_reports(mem_filter);
1808        let mut notice = 0usize;
1809        let mut warn = 0usize;
1810        for r in &reports {
1811            for s in &r.signals {
1812                match s.level {
1813                    Some(SignalLevel::Notice) => notice += 1,
1814                    Some(SignalLevel::Warn) => warn += 1,
1815                    None => {}
1816                }
1817            }
1818        }
1819        serde_json::json!({
1820            "entities": reports,
1821            "counts": { "notice": notice, "warn": warn },
1822        })
1823    }
1824
1825    /// Defective section-format declarations the loaded schemas carry
1826    /// (lenient boot recorded them; install would have refused).
1827    pub fn schema_format_defects(&self) -> Vec<crate::ops::health::SchemaFormatDefect> {
1828        crate::ops::health::collect_schema_format_defects(&self.schemas)
1829    }
1830
1831    /// Conformance-axis integrity findings for one mem — which
1832    /// entities a write would refuse under the effective schema, and
1833    /// why. `target_schema = None` lints against the mem's current
1834    /// pin; `Some(ref)` lints against that schema instead (resolved
1835    /// among mem-pinned, workspace, and built-in schemas).
1836    pub fn conformance_findings(
1837        &self,
1838        mem: &str,
1839        target_schema: Option<&memstead_schema::SchemaRef>,
1840    ) -> Result<Vec<crate::ops::integrity::IntegrityFinding>, EngineError> {
1841        let pinned = self
1842            .schemas
1843            .get(mem)
1844            .ok_or_else(|| self.unknown_mem_error(mem))?;
1845        let effective: Arc<Schema> = match target_schema {
1846            None => pinned.clone(),
1847            Some(target) => self.resolve_schema_by_ref(target).ok_or_else(|| {
1848                let consulted: Vec<_> = self
1849                    .workspace_schemas
1850                    .iter()
1851                    .chain(self.builtin_schemas.iter())
1852                    .cloned()
1853                    .collect();
1854                EngineError::SchemaNotFound {
1855                    mem: mem.to_string(),
1856                    pin: target.as_display(),
1857                    sources: crate::engine::error::SchemaSourceDiagnostic::for_failed_pin(
1858                        &target.name,
1859                        &target.version,
1860                        &consulted,
1861                    ),
1862                    install_hint: None,
1863                }
1864                .with_schema_install_probe(self.workspace_root())
1865            })?,
1866        };
1867        Ok(crate::ops::integrity::conformance_findings(
1868            &self.store,
1869            mem,
1870            &effective,
1871            &self.schemas,
1872        ))
1873    }
1874
1875    /// What `mem`'s entity BODIES carry that their types do not declare
1876    /// (consistency-sweep 04/01): headings absorbed into the catch-all,
1877    /// headings repeated so that later bodies were not kept, and frontmatter
1878    /// keys the next write will drop.
1879    ///
1880    /// A folder mem's ledger set against its file set, per mem.
1881    ///
1882    /// **Folder mems only, and that is the point** (04/04, criterion 4). On a
1883    /// git-branch mem the change set is a real two-tree diff against the
1884    /// committed tree, so ledger-versus-files divergence is structurally
1885    /// impossible; emitting an always-clean version of this check there would
1886    /// be a surface asserting something it never had to establish, which is
1887    /// the failure class this bundle exists to remove. Such a mem is absent
1888    /// from the map rather than present and empty.
1889    pub fn ledger_reconciliation(
1890        &self,
1891    ) -> std::collections::BTreeMap<String, crate::filesystem::changelog::LedgerReconciliation>
1892    {
1893        let mut out = std::collections::BTreeMap::new();
1894        for m in &self.mounts {
1895            let crate::workspace::MountStorage::Folder { path } = &m.mount.storage else {
1896                continue;
1897            };
1898            if let Ok(r) = crate::filesystem::changelog::reconcile_ledger(path) {
1899                out.insert(m.mount.mem.clone(), r);
1900            }
1901        }
1902        out
1903    }
1904
1905    /// Separate from [`Self::conformance_findings`] on purpose. These are
1906    /// observations, not violations: absorbing an undeclared heading is the
1907    /// catch-all working as designed, and reporting it as a finding would fail
1908    /// every mem that uses the feature. What a reader needs here is whether the
1909    /// content SURVIVES, which each observation states.
1910    pub fn body_observations(
1911        &self,
1912        mem: &str,
1913        target_schema: Option<&memstead_schema::SchemaRef>,
1914    ) -> Result<Vec<crate::ops::integrity::BodyObservation>, EngineError> {
1915        let pinned = self
1916            .schemas
1917            .get(mem)
1918            .ok_or_else(|| self.unknown_mem_error(mem))?;
1919        let effective: Arc<Schema> = match target_schema {
1920            None => pinned.clone(),
1921            Some(target) => {
1922                self.resolve_schema_by_ref(target)
1923                    .ok_or_else(|| EngineError::SchemaNotFound {
1924                        mem: mem.to_string(),
1925                        pin: target.as_display(),
1926                        sources: Vec::new(),
1927                        install_hint: None,
1928                    })?
1929            }
1930        };
1931        Ok(crate::ops::integrity::body_observations(
1932            &self.store,
1933            mem,
1934            &effective,
1935        ))
1936    }
1937
1938    /// Resolve an exact `name@version` ref against every schema this
1939    /// engine can see: mem-pinned, workspace-authored, built-in.
1940    /// `None` when no loaded schema matches.
1941    pub(crate) fn resolve_schema_by_ref(
1942        &self,
1943        target: &memstead_schema::SchemaRef,
1944    ) -> Option<Arc<Schema>> {
1945        self.schemas
1946            .values()
1947            .chain(self.workspace_schemas.iter())
1948            .chain(self.builtin_schemas.iter())
1949            .find(|s| {
1950                let (name, version) = s.id();
1951                name == target.name && version == target.version
1952            })
1953            .cloned()
1954    }
1955
1956    /// The mem's `Mount.schema` expectation assertion, when set.
1957    /// `None` for unknown mems *and* for mems whose mount carries no
1958    /// assertion (the authoritative pin then lives in the backend
1959    /// config; the resolved active schema, not this, is the effective pin).
1960    pub fn schema_pin(&self, mem: &str) -> Option<memstead_schema::SchemaRef> {
1961        self.mounts
1962            .iter()
1963            .find(|m| m.mount.mem == mem)
1964            .and_then(|m| m.mount.schema.clone())
1965    }
1966
1967    /// The mem's in-flight migration target, when dual-pin state is
1968    /// active. `None` for settled or unknown mems.
1969    pub fn migration_target(&self, mem: &str) -> Option<memstead_schema::SchemaRef> {
1970        self.mounts
1971            .iter()
1972            .find(|m| m.mount.mem == mem)
1973            .and_then(|m| m.mount.migration_target.clone())
1974    }
1975
1976    /// Consistency-axis integrity findings for one mem — the
1977    /// pre-existing graph-coherence categories (dangling links, stubs)
1978    /// plus cross-mem edges the workspace grant table no longer permits,
1979    /// projected into the `{ id, axis, code, detail }` finding shape.
1980    pub fn consistency_findings(
1981        &self,
1982        mem: &str,
1983    ) -> Result<Vec<crate::ops::integrity::IntegrityFinding>, EngineError> {
1984        if !self.schemas.contains_key(mem) {
1985            return Err(self.unknown_mem_error(mem));
1986        }
1987        Ok(crate::ops::integrity::consistency_findings(
1988            &self.store,
1989            mem,
1990            // The one grant resolver, the same one the write gate calls. Every
1991            // consumer of the axis reaches it through this funnel, so there is
1992            // no site where a second answer could be written (04/07).
1993            &|from, to| self.cross_mem_link_allowed(from, to),
1994        ))
1995    }
1996
1997    /// Every cross-mem edge the workspace's current grant resolution does
1998    /// not permit, across every visible mem.
1999    ///
2000    /// A projection of [`Self::consistency_findings`] filtered to the one
2001    /// code, not a second scan: the revoke path and the health axis must
2002    /// never be able to answer differently about the same edge, and the
2003    /// surest way to guarantee that is for one of them to BE the other
2004    /// (04/07, criterion 8).
2005    ///
2006    /// Call it after the grant edit has landed and the settings have been
2007    /// reloaded — the answer is "what does the CURRENT policy leave
2008    /// unbacked", which is what an operator revoking a grant wants to know.
2009    ///
2010    /// Takes `&mut self` because it must load lazily-deferred mems first. A
2011    /// deferred mem's entities are not in the store, so scanning without the
2012    /// load would report zero ungranted edges for it and call that clean —
2013    /// which is precisely the silent all-clear this whole axis exists to
2014    /// prevent. The long-lived server engines carry lazy mounts; the CLI's
2015    /// fresh boot does not, so this only bites on the surface where it is
2016    /// hardest to notice.
2017    pub fn ungranted_cross_mem_edges(&mut self) -> Vec<crate::ops::integrity::IntegrityFinding> {
2018        self.ensure_mems_loaded(None);
2019        let mut mems: Vec<&String> = self.schemas.keys().collect();
2020        mems.sort();
2021        mems.into_iter()
2022            .filter_map(|mem| self.consistency_findings(mem).ok())
2023            .flatten()
2024            .filter(|f| f.code == "CROSS_MEM_EDGE_UNGRANTED")
2025            .collect()
2026    }
2027
2028    /// The edges that went from permitted to unpermitted between two
2029    /// readings of [`Self::ungranted_cross_mem_edges`] — what a policy edit
2030    /// just orphaned, as opposed to what was already orphaned before it.
2031    ///
2032    /// A revocation that reported the whole standing set would blame this
2033    /// edit for every edge some earlier unrelated revocation left behind,
2034    /// which is a different and less useful claim (04/07, criterion 5).
2035    pub fn newly_ungranted(
2036        before: &[crate::ops::integrity::IntegrityFinding],
2037        after: Vec<crate::ops::integrity::IntegrityFinding>,
2038    ) -> Vec<crate::ops::integrity::IntegrityFinding> {
2039        let seen: std::collections::BTreeSet<(String, String)> = before
2040            .iter()
2041            .map(|f| (f.id.clone(), f.detail["target_id"].to_string()))
2042            .collect();
2043        after
2044            .into_iter()
2045            .filter(|f| !seen.contains(&(f.id.clone(), f.detail["target_id"].to_string())))
2046            .collect()
2047    }
2048
2049    /// Engine-wide health summary across every mount.
2050    pub fn health(&self) -> crate::ops::HealthSummary {
2051        self.health_inner(None)
2052    }
2053
2054    /// Health summary scoped to one visible mem. The scans and
2055    /// structural counts narrow to that mem; workspace-level facts
2056    /// (quarantine roster, boot diagnosis, workspace-scoped warnings)
2057    /// stay global — an agent scoping to one mem must still see them.
2058    /// A name that is quarantined or not on the visible roster refuses
2059    /// `UNKNOWN_MEM` — the same gate `search` applies to its `mem`
2060    /// filter, so "no such mem" and "healthy mem, nothing to report"
2061    /// can never be confused. `None` is the engine-wide sweep.
2062    pub fn health_scoped(
2063        &self,
2064        mem: Option<&str>,
2065    ) -> Result<crate::ops::HealthSummary, crate::EngineError> {
2066        if let Some(name) = mem
2067            && (self.quarantine_reason(name).is_some() || !self.mem_router.is_visible(name))
2068        {
2069            return Err(self.unknown_mem_error(name));
2070        }
2071        Ok(self.health_inner(mem))
2072    }
2073
2074    fn health_inner(&self, mem: Option<&str>) -> crate::ops::HealthSummary {
2075        let fallback = engine_fallback_type();
2076        let mut summary =
2077            crate::ops::health::compute_health(&self.store, fallback.as_ref(), &self.schemas, mem);
2078        // Merge in load-time drift warnings so every caller of
2079        // Engine::health — MCP handler, Swift FFI, direct CLI —
2080        // sees the SuspiciousNestedPrefix / DuplicateSectionHeading
2081        // findings without reaching into private engine state. The
2082        // MCP handler further appends request-scoped warnings on
2083        // top. Mirrors full's merge.
2084        if !self.load_warnings.is_empty() {
2085            let mut merged = self.load_warnings.clone();
2086            merged.append(&mut summary.warnings);
2087            summary.warnings = merged;
2088        }
2089        // A standing property, reported here rather than on every boot: a
2090        // folder mem's drift cursor is its own ledger, which only the engine
2091        // writes, so an edit made to its files by anything else is invisible
2092        // and reads keep serving the pre-edit content. Silence about that is
2093        // the one outcome 04/04's criterion 3 forbids. Git-branch mems are
2094        // absent: their change set is a real two-tree diff, so the condition
2095        // cannot arise (criterion 4).
2096        for m in &self.mounts {
2097            if matches!(
2098                m.mount.storage,
2099                crate::workspace::MountStorage::Folder { .. }
2100            ) && mem.is_none_or(|scope| scope == m.mount.mem)
2101            {
2102                summary
2103                    .warnings
2104                    .push(crate::ops::WarningHint::OutOfBandEditsUndetected {
2105                        mem: m.mount.mem.clone(),
2106                    });
2107            }
2108        }
2109        // Quarantine roster — a boot-honesty fact, present whenever
2110        // non-empty, never behind an include gate. Empty (and omitted
2111        // from the wire) on a healthy workspace.
2112        summary.quarantined = self
2113            .quarantined
2114            .iter()
2115            .map(|q| crate::ops::QuarantinedMemReport {
2116                mem: q.mount.mem.clone(),
2117                reason_code: q.reason_code.clone(),
2118                reason_message: q.reason_message.clone(),
2119            })
2120            .collect();
2121        // Per-file load failures ride the report unconditionally, like
2122        // the quarantine roster — each entry's message names the remedy
2123        // (the merge-conflict refusal names `memstead conflicts
2124        // resolve`), and a remedy only a library accessor carries is a
2125        // capability nobody finds at the moment it is needed.
2126        summary.load_errors = self
2127            .load_errors
2128            .iter()
2129            .map(|(path, msg)| crate::ops::LoadErrorReport {
2130                file: path.display().to_string(),
2131                error: msg.clone(),
2132            })
2133            .collect();
2134        summary.boot_diagnosis = self
2135            .boot_diagnosis
2136            .as_ref()
2137            .map(|(code, message)| serde_json::json!({ "code": code, "message": message }));
2138        // Surface OUTER_REPO_NOT_IGNORING_MEM_REPO when the
2139        // workspace is embedded inside a git repository whose
2140        // .gitignore does not list `mem-repo/`. Skipped when
2141        // workspace_root is unset (engine built ad-hoc from a mount
2142        // list).
2143        if let Some(root) = self.workspace_root.as_deref()
2144            && let Some(outer) = crate::workspace_root::find_enclosing_git_repo(root)
2145            && !crate::workspace_root::outer_repo_ignores_mem_repo(&outer, root)
2146        {
2147            summary
2148                .warnings
2149                .push(WarningHint::OuterRepoNotIgnoringMemRepo {
2150                    outer_repo_root: outer.display().to_string(),
2151                    workspace_root: root.display().to_string(),
2152                });
2153        }
2154        // Authoring-drift axis: for every pinned schema whose sealed
2155        // copy carries an install-provenance stamp, report a MISSING
2156        // authoring package (stamped path gone) or a DIVERGED one
2157        // (present but no longer parsed-equivalent to the seal).
2158        // Unstamped schemas — sealed pre-stamp, built-ins, archive
2159        // installs — produce no finding. Read-only on both copies.
2160        summary.warnings.extend(self.authoring_drift_findings());
2161        // Rot axis for the pins the drift axis skips: an UNSTAMPED
2162        // sealed package whose content no longer passes current-
2163        // language authoring validation gets its own low-tier hint —
2164        // the holding runs fine on the tolerant seal, but the package
2165        // (and the unlocatable authoring source it came from) is no
2166        // longer installable, and nothing else would say so before the
2167        // next install attempt. A parsing unstamped package stays
2168        // silent; stamped pins are the drift axis's business.
2169        summary.warnings.extend(self.unstamped_rot_findings());
2170        // Under a mem scope, mem-attributable warnings narrow to the
2171        // scoped mem; workspace- and request-scoped warnings return
2172        // `None` from `source_mem()` and stay visible regardless.
2173        // Mirrors the full flavour's compose filter.
2174        if let Some(v) = mem {
2175            summary
2176                .warnings
2177                .retain(|w| w.source_mem().is_none_or(|wv| wv == v));
2178        }
2179        summary
2180    }
2181
2182    /// Compute the authoring-drift findings for every stamped pinned
2183    /// schema. See the call site in [`Self::health`] for the axis
2184    /// contract; returns an empty list when no workspace root is set
2185    /// (ad-hoc mount-list engines have no authoring tree to check).
2186    fn authoring_drift_findings(&self) -> Vec<WarningHint> {
2187        let Some(root) = self.workspace_root.as_deref() else {
2188            return Vec::new();
2189        };
2190        // Group pinning mems by (name, version) — BTreeMap for a
2191        // deterministic finding order.
2192        let mut pins: std::collections::BTreeMap<(String, String), Vec<String>> =
2193            std::collections::BTreeMap::new();
2194        for (mem, schema) in &self.schemas {
2195            let (name, version) = schema.id();
2196            pins.entry((name.to_string(), version.to_string()))
2197                .or_default()
2198                .push(mem.clone());
2199        }
2200        let mut out = Vec::new();
2201        for ((name, version), mut mems) in pins {
2202            mems.sort();
2203            let Some(stamped_path) = self.read_install_provenance(root, &name, &version) else {
2204                continue;
2205            };
2206            let schema_ref = format!("{name}@{version}");
2207            // A workspace-relative stamp (the portable form the installer
2208            // writes for in-workspace authoring dirs) resolves against
2209            // THIS workspace root, so the axis checks real drift on every
2210            // clone instead of reporting another machine's absolute path
2211            // as missing. Absolute stamps stay machine-pinned as-is.
2212            let stamped = std::path::Path::new(&stamped_path);
2213            let resolved: std::path::PathBuf = if stamped.is_absolute() {
2214                stamped.to_path_buf()
2215            } else {
2216                root.join(stamped)
2217            };
2218            let authoring = resolved.as_path();
2219            if !authoring.is_dir() {
2220                out.push(WarningHint::SchemaAuthoringSourceMissing {
2221                    schema_ref,
2222                    stamped_path,
2223                    mems,
2224                });
2225                continue;
2226            }
2227            let sealed = self
2228                .schemas
2229                .get(&mems[0])
2230                .expect("mems collected from self.schemas keys")
2231                .clone();
2232            match memstead_schema::load_schema_from_dir(authoring) {
2233                Err(e) => out.push(WarningHint::SchemaAuthoringSourceDiverged {
2234                    schema_ref,
2235                    stamped_path,
2236                    mems,
2237                    detail: format!("the authoring package no longer loads: {e}"),
2238                }),
2239                Ok(authored) => {
2240                    if schema_parsed_fingerprint(&authored) != schema_parsed_fingerprint(&sealed) {
2241                        out.push(WarningHint::SchemaAuthoringSourceDiverged {
2242                            schema_ref,
2243                            stamped_path,
2244                            mems,
2245                            detail: "the parsed authoring package differs from the sealed copy \
2246                                     the engine runs on"
2247                                .to_string(),
2248                        });
2249                    }
2250                }
2251            }
2252        }
2253        out
2254    }
2255
2256    /// Compute the rot findings for every UNSTAMPED pinned schema: read
2257    /// the sealed package's content back (folder seal directory, or the
2258    /// `__MEMSTEAD:schemas/` ref via the ops bundle) and run the
2259    /// authoring-tier check over it. A pin with a stamp is skipped (the
2260    /// divergence axis owns it); a pin with no readable sealed package
2261    /// — built-ins resolving from the embedded catalogue — is skipped
2262    /// too (nothing on disk can rot). See the call site in
2263    /// [`Self::health`] for the axis contract.
2264    fn unstamped_rot_findings(&self) -> Vec<WarningHint> {
2265        let Some(root) = self.workspace_root.as_deref() else {
2266            return Vec::new();
2267        };
2268        let mut pins: std::collections::BTreeMap<(String, String), Vec<String>> =
2269            std::collections::BTreeMap::new();
2270        for (mem, schema) in &self.schemas {
2271            let (name, version) = schema.id();
2272            pins.entry((name.to_string(), version.to_string()))
2273                .or_default()
2274                .push(mem.clone());
2275        }
2276        let mut out = Vec::new();
2277        for ((name, version), mut mems) in pins {
2278            mems.sort();
2279            if self
2280                .read_install_provenance(root, &name, &version)
2281                .is_some()
2282            {
2283                continue; // stamped — the divergence axis checks it
2284            }
2285            let schema_ref = format!("{name}@{version}");
2286            // Folder seal: the sealed package is a real directory the
2287            // authoring loader can probe directly.
2288            let sealed_dir = root.join(".memstead").join("schemas").join(&schema_ref);
2289            let detail: Option<String> = if sealed_dir.join("schema.yaml").is_file() {
2290                memstead_schema::load_schema_from_dir(&sealed_dir)
2291                    .err()
2292                    .map(|e| e.to_string())
2293            } else if let Some((manifest, types)) = self.read_sealed_package_yamls(&name, &version)
2294            {
2295                memstead_schema::loader::check_package_reauthorable(&manifest, &types)
2296                    .err()
2297                    .map(|e| e.to_string())
2298            } else {
2299                None // no sealed copy anywhere — embedded builtin
2300            };
2301            if let Some(detail) = detail {
2302                out.push(WarningHint::SchemaUnstampedSourceRot {
2303                    schema_ref,
2304                    mems,
2305                    detail,
2306                });
2307            }
2308        }
2309        out
2310    }
2311
2312    /// Read a sealed package's `schema.yaml` + `types/*.yaml` back from
2313    /// the `__MEMSTEAD:schemas/` ref, reconstructing the type-file names
2314    /// from the pinned parsed schema's type roster (seal-time authoring
2315    /// enforces stem == declared type name). `None` when the ops bundle
2316    /// or the package is absent — the embedded-builtin state.
2317    fn read_sealed_package_yamls(
2318        &self,
2319        name: &str,
2320        version: &str,
2321    ) -> Option<(String, Vec<(String, String)>)> {
2322        let ops = self.git_branch_ops()?;
2323        let root = self.workspace_root.as_deref()?;
2324        let gitdir = self
2325            .mounts
2326            .iter()
2327            .find_map(|m| match &m.mount.storage {
2328                crate::workspace::MountStorage::GitBranch { gitdir, .. } => Some(gitdir.clone()),
2329                _ => None,
2330            })
2331            .or_else(|| {
2332                let g = root.join("mem-repo").join(".git");
2333                g.is_dir().then_some(g)
2334            })?;
2335        let read = |rel: &str| -> Option<String> {
2336            (ops.read_schema_file)(&gitdir, name, version, rel)
2337                .ok()
2338                .flatten()
2339                .and_then(|bytes| String::from_utf8(bytes).ok())
2340        };
2341        let manifest = read("schema.yaml")?;
2342        let schema = self
2343            .schemas
2344            .values()
2345            .find(|s| {
2346                let (n, v) = s.id();
2347                n == name && v.to_string() == version
2348            })?
2349            .clone();
2350        let mut type_names: Vec<String> = schema.types.keys().cloned().collect();
2351        type_names.sort();
2352        let mut types = Vec::new();
2353        for t in type_names {
2354            if let Some(body) = read(&format!("types/{t}.yaml")) {
2355                types.push((t, body));
2356            }
2357        }
2358        Some((manifest, types))
2359    }
2360
2361    /// Read the install-provenance stamp for a sealed schema package,
2362    /// checking the folder location first
2363    /// (`.memstead/schemas/<name>@<version>/`) and falling back to the
2364    /// `__MEMSTEAD:schemas/` ref via the git-branch ops bundle when
2365    /// wired. `None` when no stamp exists anywhere — the normal state
2366    /// for pre-stamp seals, built-ins, and archive installs.
2367    fn read_install_provenance(&self, root: &Path, name: &str, version: &str) -> Option<String> {
2368        let folder_stamp = root
2369            .join(".memstead")
2370            .join("schemas")
2371            .join(format!("{name}@{version}"))
2372            .join(memstead_schema::INSTALL_PROVENANCE_FILE);
2373        let bytes = if folder_stamp.is_file() {
2374            std::fs::read(&folder_stamp).ok()
2375        } else {
2376            let ops = self.git_branch_ops()?;
2377            let gitdir = self
2378                .mounts
2379                .iter()
2380                .find_map(|m| match &m.mount.storage {
2381                    crate::workspace::MountStorage::GitBranch { gitdir, .. } => {
2382                        Some(gitdir.clone())
2383                    }
2384                    _ => None,
2385                })
2386                .or_else(|| {
2387                    let g = root.join("mem-repo").join(".git");
2388                    g.is_dir().then_some(g)
2389                })?;
2390            (ops.read_schema_file)(
2391                &gitdir,
2392                name,
2393                version,
2394                memstead_schema::INSTALL_PROVENANCE_FILE,
2395            )
2396            .ok()
2397            .flatten()
2398        }?;
2399        let v: serde_json::Value = serde_json::from_slice(&bytes).ok()?;
2400        v.get("authoring_path")?.as_str().map(String::from)
2401    }
2402
2403    /// Engine-wide [`crate::ops::Status`] across every mount — the graph
2404    /// counts behind `memstead status` (renamed from `stats` with the
2405    /// command, D11; fields unchanged).
2406    pub fn status(&self) -> crate::ops::Status {
2407        let mut types_in_use: Vec<String> = self
2408            .store
2409            .all_entities()
2410            .filter(|e| !e.stub && !e.entity_type.is_empty())
2411            .map(|e| e.entity_type.clone())
2412            .collect();
2413        types_in_use.sort();
2414        types_in_use.dedup();
2415
2416        let mut edge_types: HashMap<String, usize> = HashMap::new();
2417        for id in self.store.all_ids() {
2418            for edge in self.store.outgoing(id) {
2419                *edge_types.entry(edge.rel_type.clone()).or_insert(0) += 1;
2420            }
2421        }
2422
2423        crate::ops::Status {
2424            entity_count: self.store.all_entities().filter(|e| !e.stub).count(),
2425            edge_count: self.store.edge_count(),
2426            edge_types,
2427            community_count: self.communities().count,
2428            mem_count: self.mounts.len(),
2429            types_in_use,
2430        }
2431    }
2432
2433    /// Build a [`ContextResult`] for `id`: the community cluster id
2434    /// (or `None` when the entity is a stub or not present), plus the
2435    /// outgoing + incoming neighbour lists.
2436    pub fn context(&self, id: &EntityId) -> Option<ContextResult> {
2437        let entity = self.store.get(id)?;
2438        let community = self
2439            .communities()
2440            .entity_cluster_map
2441            .get(id.as_ref())
2442            .cloned();
2443        let mut neighbors = Vec::new();
2444        for edge in self.store.outgoing(id) {
2445            if let Some(target) = self.store.get(&edge.target) {
2446                neighbors.push(NeighborInfo {
2447                    id: target.id.clone(),
2448                    title: target.title.clone(),
2449                    relationship: edge.rel_type.clone(),
2450                    direction: Direction::Outgoing,
2451                });
2452            }
2453        }
2454        for edge in self.store.incoming(id) {
2455            if let Some(source) = self.store.get(&edge.from) {
2456                neighbors.push(NeighborInfo {
2457                    id: source.id.clone(),
2458                    title: source.title.clone(),
2459                    relationship: edge.rel_type.clone(),
2460                    direction: Direction::Incoming,
2461                });
2462            }
2463        }
2464        Some(ContextResult {
2465            entity_id: entity.id.clone(),
2466            community,
2467            neighbors,
2468        })
2469    }
2470
2471    /// Lazily-built per-mem search index map. The map carries one
2472    /// entry per writable mem. Build cost scales with entity count;
2473    /// expect hundreds-of-ms for thousand-entity workspaces. Not
2474    /// available on `wasm32` targets — search lives behind the bridge
2475    /// (see [`Self::search`] for the typed refuse).
2476    #[cfg(not(target_arch = "wasm32"))]
2477    pub fn search_indexes(&self) -> &HashMap<String, MemIndex> {
2478        if let Some((memo_key, _)) = self.search_indexes_memo.get() {
2479            debug_assert_eq!(
2480                *memo_key,
2481                self.derived_key(),
2482                "search memo key lags the engine — a mutation path missed invalidate_search_indexes"
2483            );
2484        }
2485        &self
2486            .search_indexes_memo
2487            .get_or_init(|| (self.derived_key(), build_all(&self.store, &self.schemas)))
2488            .1
2489    }
2490
2491    /// Drop the cached per-mem search index map. No-op on `wasm32`
2492    /// where no index exists; the method stays present so mutation
2493    /// hooks can call it unconditionally.
2494    /// Incrementally maintain the search-index memo for a known
2495    /// touched-id set (flywheel W8/01, criterion 1): replace or remove
2496    /// exactly the touched documents in place and advance the memo's
2497    /// key, instead of dropping the whole map. Semantics:
2498    ///
2499    /// - Memo empty → nothing to maintain; the next read builds fresh.
2500    /// - Memo current (key matches) → no-op (rollback already landed).
2501    /// - Schemas epoch moved → the index FIELD SET may have changed:
2502    ///   the named, scoped fallback — drop the memo for a full
2503    ///   rebuild. Never a silent widening: this is the one case the
2504    ///   plan names (schema-shape change).
2505    /// - Otherwise: per touched id, a real non-stub entity in the
2506    ///   store is re-indexed (delete-then-add on the id term), and an
2507    ///   absent or stub entry is removed — stubs stay excluded exactly
2508    ///   as the bulk build excludes them. Touched mems' writers
2509    ///   commit; any tantivy error falls back to dropping the memo
2510    ///   (warn-logged), never to serving a stale index.
2511    #[cfg(not(target_arch = "wasm32"))]
2512    pub(crate) fn maintain_search_indexes(&mut self, touched: &[crate::EntityId]) {
2513        let current = super::DerivedKey {
2514            store_generation: self.store.generation(),
2515            schemas_epoch: self.schemas_epoch,
2516        };
2517        let Some((memo_key, indexes)) = self.search_indexes_memo.get_mut() else {
2518            return;
2519        };
2520        if *memo_key == current {
2521            return;
2522        }
2523        if memo_key.schemas_epoch != current.schemas_epoch {
2524            self.search_indexes_memo = OnceCell::new();
2525            return;
2526        }
2527        let mut touched_mems: std::collections::HashSet<&str> = std::collections::HashSet::new();
2528        for id in touched {
2529            let Some(idx) = indexes.get_mut(id.mem()) else {
2530                continue;
2531            };
2532            let result = match self.store.get(id) {
2533                Some(entity) if !entity.stub => idx.index_entity(entity),
2534                _ => idx.remove_entity(id),
2535            };
2536            if let Err(e) = result {
2537                tracing::warn!(
2538                    id = id.as_ref(),
2539                    error = %e,
2540                    "incremental index maintenance failed; dropping the memo for a full rebuild"
2541                );
2542                self.search_indexes_memo = OnceCell::new();
2543                return;
2544            }
2545            touched_mems.insert(id.mem());
2546        }
2547        for (mem, idx) in indexes.iter_mut() {
2548            if !touched_mems.contains(mem.as_str()) {
2549                continue;
2550            }
2551            if let Err(e) = idx.commit() {
2552                tracing::warn!(
2553                    mem = mem.as_str(),
2554                    error = %e,
2555                    "incremental index commit failed; dropping the memo for a full rebuild"
2556                );
2557                self.search_indexes_memo = OnceCell::new();
2558                return;
2559            }
2560        }
2561        *memo_key = current;
2562    }
2563
2564    /// No-op shim on `wasm32`, mirroring `invalidate_search_indexes`
2565    /// so mutation paths call it unconditionally.
2566    #[cfg(target_arch = "wasm32")]
2567    pub(crate) fn maintain_search_indexes(&mut self, _touched: &[crate::EntityId]) {}
2568
2569    /// Unconditionally drop the search-index memo, regardless of its
2570    /// generation. The forced variant exists for embedders that want
2571    /// to release the index's memory in a long-lived process (or force
2572    /// a from-scratch rebuild for verification); the generation-checked
2573    /// [`Self::invalidate_search_indexes`] stays the mutation-path
2574    /// hook.
2575    pub fn drop_search_indexes(&mut self) {
2576        #[cfg(not(target_arch = "wasm32"))]
2577        {
2578            self.search_indexes_memo = OnceCell::new();
2579        }
2580    }
2581
2582    pub fn invalidate_search_indexes(&mut self) {
2583        #[cfg(not(target_arch = "wasm32"))]
2584        {
2585            // Same generation check as `invalidate_communities`: keep
2586            // the memo when the store still sits at its generation
2587            // (the batch-rollback case), clear otherwise.
2588            if let Some((memo_key, _)) = self.search_indexes_memo.get()
2589                && *memo_key == self.derived_key()
2590            {
2591                return;
2592            }
2593            self.search_indexes_memo = OnceCell::new();
2594        }
2595    }
2596
2597    /// Filter the in-memory store by metadata only (no text match).
2598    #[cfg(not(target_arch = "wasm32"))]
2599    pub fn list(&self, scope: &SearchScope) -> crate::ops::ListResult {
2600        let fallback = engine_fallback_type();
2601        crate::ops::search::list(&self.store, scope, fallback.as_ref(), &self.schemas)
2602    }
2603
2604    /// Run a search against the lazily-built index map. Returns
2605    /// [`EngineError::SearchUnavailable`] on `wasm32` targets — browser
2606    /// consumers route search to the bridge; the local
2607    /// engine never builds a tantivy index in WASM. Native targets get
2608    /// the same shape as before, wrapped in `Ok`.
2609    pub fn search(&self, scope: &SearchScope) -> Result<SearchResult, EngineError> {
2610        // A mem filter naming a quarantined OR nonexistent mem refuses
2611        // typed `UNKNOWN_MEM`, matching every other mem-naming surface. A
2612        // success with 0 hits (the old nonexistent-mem behaviour, with a
2613        // missing-index warning) is indistinguishable from a true empty
2614        // result — the one thing a typed surface must never be.
2615        if let Some(mem) = scope.mem.as_deref()
2616            && (self.quarantine_reason(mem).is_some() || !self.mem_router.is_visible(mem))
2617        {
2618            return Err(self.unknown_mem_error(mem));
2619        }
2620        #[cfg(target_arch = "wasm32")]
2621        {
2622            let _ = scope;
2623            return Err(EngineError::SearchUnavailable);
2624        }
2625        #[cfg(not(target_arch = "wasm32"))]
2626        {
2627            let fallback = engine_fallback_type();
2628            Ok(crate::ops::search::search(
2629                &self.store,
2630                scope,
2631                fallback.as_ref(),
2632                self.search_indexes(),
2633                &self.schemas,
2634            ))
2635        }
2636    }
2637
2638    /// All mem-relative entity paths under `mem`. Delegates to
2639    /// the backend's `list_entities`. Order is backend-defined.
2640    pub fn list_entities(&self, mem: &str) -> Result<Vec<PathBuf>, EngineError> {
2641        let m = self.find_mount(mem)?;
2642        m.backend.list_entities().map_err(EngineError::Backend)
2643    }
2644
2645    /// Raw bytes for a single entity (`Ok(None)` if absent).
2646    pub fn read_entity(&self, mem: &str, rel_path: &Path) -> Result<Option<Vec<u8>>, EngineError> {
2647        let m = self.find_mount(mem)?;
2648        m.backend
2649            .read_entity(rel_path)
2650            .map_err(EngineError::Backend)
2651    }
2652
2653    /// Provenance entries for `mem` since `cursor`. Cursor shape is
2654    /// backend-specific (RFC-3339 timestamp for folder, commit SHA for
2655    /// git-branch); `None` means "from the beginning".
2656    pub fn read_provenance(
2657        &self,
2658        mem: &str,
2659        cursor: Option<&str>,
2660    ) -> Result<Vec<Provenance>, EngineError> {
2661        let m = self.find_mount(mem)?;
2662        m.backend
2663            .read_provenance(cursor)
2664            .map_err(EngineError::Backend)
2665    }
2666
2667    /// Capability declared on the mount for `mem`. Surfaced for
2668    /// callers that need to gate before dispatching a write — the
2669    /// engine itself does not yet enforce capability (mutation paths
2670    /// land in a later session).
2671    pub fn capability(&self, mem: &str) -> Result<crate::workspace::MountCapability, EngineError> {
2672        let m = self.find_mount(mem)?;
2673        Ok(m.mount.capability)
2674    }
2675
2676    /// Returns `true` when `from`'s source mem is mounted with
2677    /// [`crate::workspace::MountCapability::ReadOnly`]. Returns
2678    /// `false` for Write-Mems and for mems whose mount is absent
2679    /// from the router (no mount → no ReadOnly assertion can be
2680    /// made; the absence is treated as not-ReadOnly so consumers
2681    /// don't trip on transient lookup misses).
2682    ///
2683    /// Plan body §"Single edge source in the store" specifies this
2684    /// helper as the derived-on-demand alternative to adding a new
2685    /// field on [`crate::store::Edge`]. Strict-invariant validators
2686    /// and surfaces that want to highlight cross-mount references
2687    /// call this rather than pattern-matching on a per-edge marker.
2688    /// The information is fully derivable from the current mount
2689    /// roster, so no new state needs to live on the edge itself.
2690    pub fn edge_is_from_readonly(&self, from: &EntityId) -> bool {
2691        match self.capability(from.mem()) {
2692            Ok(crate::workspace::MountCapability::ReadOnly) => true,
2693            Ok(crate::workspace::MountCapability::Write) | Err(_) => false,
2694        }
2695    }
2696
2697    /// Whether a cross-mem edge from `from_mem` to `to_mem` is
2698    /// permitted under the current [`crate::WorkspaceSettings`]
2699    /// cross-mem link policy.
2700    ///
2701    /// Resolution rules (matches full's `mem_router` semantics):
2702    /// 1. Same-mem edge (`from_mem == to_mem`) → always
2703    ///    allowed; the policy gates *cross*-mem edges only.
2704    /// 2. Explicit `cross_mem_links[from_mem]`:
2705    ///    - `"*"` (wildcard) → allowed regardless of target.
2706    ///    - `["a", ...]` (allowlist) → allowed iff `to_mem` is in
2707    ///      the list.
2708    /// 3. Per-create-rule `default_cross_links` synthesis — if
2709    ///    rule (1) didn't grant permission and `from_mem` matches
2710    ///    a `[[mem_management.create]]` rule whose
2711    ///    `default_cross_links` is set, the synthesised value
2712    ///    contributes:
2713    ///    - `"*"` → allowed regardless of target.
2714    ///    - `["a", ...]` → allowed iff `to_mem` is in the list.
2715    /// 4. Otherwise → denied (default-deny posture).
2716    ///
2717    /// The synthesis layer compiles a [`crate::mem_management::CreateRuleSet`]
2718    /// lazily on first call and caches it; [`Self::set_settings`]
2719    /// invalidates the cache. Compilation failure (malformed glob
2720    /// in a rule) logs a warning and the synthesis layer is silently
2721    /// skipped — the resolver still returns `true` from explicit
2722    /// policy alone, so a half-broken config doesn't lock out edges
2723    /// the operator did intend to allow. Operators who want hard
2724    /// validation pre-compile via
2725    /// [`crate::mem_management::CreateRuleSet::new`] before
2726    /// calling [`Self::set_settings`].
2727    ///
2728    /// The MCP `memstead_relate` handler's cross-mem gate consumes
2729    /// this method directly.
2730    pub fn cross_mem_link_allowed(&self, from_mem: &str, to_mem: &str) -> bool {
2731        use memstead_schema::workspace_config::CrossLinkValue;
2732        if from_mem == to_mem {
2733            return true;
2734        }
2735
2736        // Step 1: explicit cross_mem_links policy.
2737        if let Some(value) = self.settings.cross_mem_links.get(from_mem) {
2738            match value {
2739                CrossLinkValue::Wildcard => return true,
2740                CrossLinkValue::List(targets) => {
2741                    if targets.iter().any(|t| t == to_mem) {
2742                        return true;
2743                    }
2744                    // Fall through to synthesis check — a List that
2745                    // doesn't include the target may still allow it
2746                    // via per-rule default_cross_links union.
2747                }
2748            }
2749        }
2750
2751        // Step 2: per-create-rule default_cross_links synthesis.
2752        let rule_set = self.create_rule_set_memo.get_or_init(|| {
2753            crate::mem_management::CreateRuleSet::new(
2754                self.settings.mem_create_rules.clone(),
2755            )
2756            .unwrap_or_else(|err| {
2757                tracing::warn!(
2758                    error = %err,
2759                    "cross_mem_link_allowed: failed to compile mem_create_rules — synthesis disabled (resolver falls back to explicit-policy-only)"
2760                );
2761                crate::mem_management::CreateRuleSet::default()
2762            })
2763        });
2764
2765        // Compose the same `<mem_path>/<name>` candidate the create-rule
2766        // composer matched against. The rule globs are keyed on the composed
2767        // lifecycle path (e.g. `memstead/project`, compiled with
2768        // `literal_separator`), not the bare leaf name — matching
2769        // `from_mem` alone silently misses, so synthesis denied a link
2770        // that `memstead_overview` rendered as rule-granted (the
2771        // leaf-vs-composed-path divergence). Flat-layout mems (no
2772        // hierarchical path) keep the bare leaf, matching their bare rule.
2773        let candidate = match self.mount(from_mem).and_then(|m| m.mem_path()) {
2774            Some(path) => format!("{path}/{from_mem}"),
2775            None => from_mem.to_string(),
2776        };
2777        if let Some(matched) = rule_set.first_match(std::path::Path::new(&candidate))
2778            && let Some(synth) = matched.default_cross_links.as_ref()
2779        {
2780            return match synth {
2781                CrossLinkValue::Wildcard => true,
2782                CrossLinkValue::List(targets) => targets.iter().any(|t| t == to_mem),
2783            };
2784        }
2785
2786        false
2787    }
2788
2789    pub(super) fn find_mount(&self, mem: &str) -> Result<&MountedBackend, EngineError> {
2790        self.mounts
2791            .iter()
2792            .find(|m| m.mount.mem == mem)
2793            .ok_or_else(|| self.unknown_mem_error(mem))
2794    }
2795}
2796
2797/// The base path of an anchor artifact ref — the locator suffixes a
2798/// medium may append (`@<commit>`, `#<span>`) stripped so the reverse
2799/// lookup compares paths, not versioned/located refs.
2800pub(crate) fn anchor_base_path(artifact: &str) -> &str {
2801    let cut = artifact.find(['@', '#']).unwrap_or(artifact.len());
2802    &artifact[..cut]
2803}
2804
2805/// One mem's standalone anchor-verification report — the counts plus
2806/// the per-anchor rows, in sidecar order.
2807#[derive(Debug, Clone, Default, serde::Serialize)]
2808pub struct MemAnchorVerification {
2809    pub mem: String,
2810    /// Source present, hash matches (or a non-hash class whose source
2811    /// exists).
2812    pub resolved: usize,
2813    /// Source present, hash differs, stability `stable` — real drift.
2814    pub drifted: usize,
2815    /// Hash differs under `unstable` stability, or a hash is missing on
2816    /// either side — flagged for re-examination, never called drift.
2817    pub recheck: usize,
2818    /// Source absent: a MEASURED failure. The artifact the anchor names is
2819    /// not there.
2820    pub unresolvable: usize,
2821    /// The anchor could not be observed at all this pass, so nothing about it
2822    /// was measured (consistency-sweep 03/05, criterion 2). Its own count,
2823    /// because `unresolvable` used to swallow it: a reader on the surface you
2824    /// reach WITHOUT a binding could not tell a measured failure from an
2825    /// absent measurement, which is the one distinction that surface exists to
2826    /// make.
2827    pub unobserved: usize,
2828    /// Rows whose ENTITY is gone (consistency-sweep 03/02). Its own class,
2829    /// counted apart from the states above: those describe the artifact end,
2830    /// and a vanished entity says nothing about the source. Folding it into
2831    /// `unresolvable` would name the wrong repair.
2832    pub dangling: usize,
2833    /// Why the entity end could not be reconciled this pass, when it could
2834    /// not. `dangling: 0` means "none found" only when this is `None`.
2835    pub unreconciled: Option<String>,
2836    pub anchors: Vec<VerifiedAnchor>,
2837    /// Supplied observations whose artifact matched no `url` anchor of the
2838    /// mem — reported, never silently dropped, never applied to anything.
2839    #[serde(default, skip_serializing_if = "Vec::is_empty")]
2840    pub unmatched_observations: Vec<String>,
2841    /// The `last_observed` records this verification produced from supplied
2842    /// observations, for the caller to commit through
2843    /// [`Engine::record_anchor_observations`]. Engine-internal.
2844    #[serde(skip)]
2845    pub recordable_observations: Vec<RecordedObservation>,
2846}
2847
2848/// One observation to record onto a sidecar row (the `last_observed`
2849/// field), addressed by the `(entity, artifact)` pair.
2850#[derive(Debug, Clone, PartialEq, Eq)]
2851pub struct RecordedObservation {
2852    pub entity: String,
2853    pub artifact: String,
2854    pub observation: crate::anchor::AnchorObservation,
2855}
2856
2857/// Observer-supplied observations keyed by artifact — the input of
2858/// [`Engine::verify_mem_anchors_with`].
2859pub type SuppliedObservations =
2860    std::collections::BTreeMap<String, crate::anchor::SuppliedObservation>;
2861
2862/// What one observation of an anchor yielded: the resolved state, the
2863/// hash it saw (when any), and — for an observation that was not made
2864/// live this pass — when it was made.
2865#[derive(Debug, Clone, PartialEq, Eq)]
2866struct Observed {
2867    state: crate::anchor::AnchorState,
2868    hash: Option<String>,
2869    at: Option<String>,
2870}
2871
2872impl Observed {
2873    /// A live observation made this pass (path and entity grains).
2874    fn live((state, hash): (crate::anchor::AnchorState, Option<String>)) -> Self {
2875        Observed {
2876            state,
2877            hash,
2878            at: None,
2879        }
2880    }
2881}
2882
2883impl MemAnchorVerification {
2884    /// The population statement that must accompany this report's figures
2885    /// (consistency-sweep 03/05, criteria 1 and 3): what the figures were
2886    /// computed over, and how much of it the pass could not adjudicate.
2887    ///
2888    /// A resolution figure alone is read as health. Every W3 finding made that
2889    /// figure mean less than a reader assumes, and none of them made it wrong
2890    /// in a way anyone could see. Rendering the figure and its population as
2891    /// ONE unit is what stops the next such finding being invisible: a surface
2892    /// cannot show the number and omit the caveat, because it gets both from
2893    /// here or neither.
2894    pub fn population_statement(&self) -> String {
2895        // `recheck` belongs on ONE side of this sentence. A first version put
2896        // it in both: counted as adjudicated and then reported as not, so the
2897        // same rows appeared twice and the two numbers could not be reconciled
2898        // by a reader. A recheck row is a row whose drift could NOT be
2899        // asserted, which is the definition of unadjudicated.
2900        let adjudicated = self.resolved + self.drifted + self.unresolvable;
2901        let unadjudicated = self.recheck + self.unobserved;
2902        let mut s = format!(
2903            "over {} counted row(s): {adjudicated} adjudicated, {unadjudicated} not (recheck {}, unobserved {})",
2904            adjudicated + unadjudicated,
2905            self.recheck,
2906            self.unobserved
2907        );
2908        if self.dangling > 0 {
2909            s.push_str(&format!(
2910                "; {} row(s) excluded, naming an entity the mem no longer holds",
2911                self.dangling
2912            ));
2913        }
2914        if let Some(why) = &self.unreconciled {
2915            s.push_str(&format!(
2916                "; the entity end was NOT reconciled ({why}), so dangling rows would not have been detected"
2917            ));
2918        }
2919        s
2920    }
2921
2922    /// Whether this axis adjudicated everything it counted. False means the
2923    /// figures above rest on an incomplete measurement, which is not the same
2924    /// as a failed one.
2925    pub fn fully_adjudicated(&self) -> bool {
2926        self.recheck == 0 && self.unobserved == 0 && self.unreconciled.is_none()
2927    }
2928}
2929
2930/// One anchor's verification row.
2931#[derive(Debug, Clone, serde::Serialize)]
2932pub struct VerifiedAnchor {
2933    pub entity_id: String,
2934    pub artifact: String,
2935    pub grain: String,
2936    pub class: String,
2937    /// `resolved` | `drifted` | `recheck` | `unresolvable` (artifact gone) |
2938    /// `unobserved` (not measured this pass) | `dangling` (the entity is
2939    /// gone). The wire vocabulary of this field, which is NOT the engine's
2940    /// `AnchorState` enum: that has four variants describing the artifact
2941    /// end, and the last two here are conditions beside them.
2942    pub state: String,
2943    #[serde(skip_serializing_if = "Option::is_none")]
2944    pub observed_hash: Option<String>,
2945    /// When the observation this row's state rests on was made — present
2946    /// only for a row resolved from a supplied or recorded observation (a
2947    /// `url` row); live observations carry none.
2948    #[serde(skip_serializing_if = "Option::is_none")]
2949    pub observed_at: Option<String>,
2950    /// Whole days between `observed_at` and this run — how long the row has
2951    /// gone unobserved. `Some(0)` for an observation made today.
2952    #[serde(skip_serializing_if = "Option::is_none")]
2953    pub unobserved_for_days: Option<u64>,
2954    /// Whether this row's state came from an observation supplied to this
2955    /// run (as opposed to a live or a previously recorded one).
2956    #[serde(default, skip_serializing_if = "std::ops::Not::not")]
2957    pub observation_supplied: bool,
2958}
2959
2960/// Whether `anchor` references `path`. `tree`-grain anchors match `path`
2961/// itself and anything beneath the tree; every other grain matches by
2962/// exact base-path equality.
2963/// A stored anchor paired with its live resolution state, when observable.
2964/// See [`Engine::entity_anchors_resolved`] for how `state` is produced and
2965/// when it is `None` (unobserved, never fabricated).
2966#[derive(Debug, Clone, PartialEq, Eq, serde::Serialize)]
2967pub struct ResolvedAnchor {
2968    /// The durable anchor record (flattened on the wire so the resolved shape
2969    /// is the stored anchor plus a `state` field).
2970    #[serde(flatten)]
2971    pub anchor: crate::anchor::Anchor,
2972    /// The live resolution state, or `None` when the engine could not observe
2973    /// the source artifact this pass: a `url` grain, no workspace root, an
2974    /// ambiguous or absent path medium, or an `entity` grain whose mem is not
2975    /// mounted. That last case is load-bearing — an unmounted mem is not a mem
2976    /// of deleted entities, so it must read as unobserved rather than
2977    /// `Orphaned`, which prune would act on.
2978    #[serde(skip_serializing_if = "Option::is_none")]
2979    pub state: Option<crate::anchor::AnchorState>,
2980    /// The prepared-content hash the observation computed this pass —
2981    /// present only for a hash-bearing (`anchored` / `derived`) anchor whose
2982    /// artifact could be read: a `file` / `span` anchor resolving to a
2983    /// readable file, or an `entity` anchor whose mem is mounted (hashed over
2984    /// the canonical rendered markdown, so the value means the same thing in
2985    /// both namespaces). The verify pass's backfill leg records it onto a
2986    /// hash-less anchor. Engine-internal
2987    /// observation detail, deliberately not serialized: the wire shape stays
2988    /// the stored anchor plus `state`.
2989    #[serde(skip)]
2990    pub observed_hash: Option<String>,
2991    /// When the observation `state` rests on was made, for a row whose
2992    /// observation is supplied or recorded rather than live (a `url` row).
2993    /// Additive: absent on every live-observed row.
2994    #[serde(skip_serializing_if = "Option::is_none")]
2995    pub observed_at: Option<String>,
2996}
2997
2998/// What an anchor's `source` name resolves to in its mem's bindings: the
2999/// declared pointer (the filesystem root a source-dialect artifact path
3000/// joins onto, decision 26) and the declared preparation (what the
3001/// preparation registry prepares the artifact as before hashing —
3002/// touchpoint A of [`crate::preparation`]).
3003#[derive(Debug, Clone, PartialEq, Eq)]
3004pub(crate) struct AnchorSourceJoin {
3005    /// The source's declared `pointer`.
3006    pub(crate) pointer: String,
3007    /// The source's declared `preparation`, if any.
3008    pub(crate) preparation: Option<String>,
3009    /// The declaring source itself — its scope is what a `tree` anchor's
3010    /// prepared form enumerates under a code-map preparation.
3011    pub(crate) source: crate::pipeline::Source,
3012    /// The binding's `deny_paths`, applied on top of the source scope.
3013    pub(crate) deny_paths: Vec<String>,
3014}
3015
3016/// Observe a single path-namespace anchor against `root` (its medium's
3017/// filesystem root) and resolve its live state plus — for a present
3018/// hash-bearing (`anchored` / `derived`) `file` / `span` anchor — the
3019/// artifact's **prepared-content hash**
3020/// ([`crate::anchor::prepared_content_hash`]). `None` when the anchor's
3021/// grain does not reference a filesystem path.
3022///
3023/// The computed hash is what lets [`crate::anchor::resolve_anchor`]
3024/// adjudicate `drifted` vs `resolves` deterministically against the recorded
3025/// hash. The prepared form is the registry's rule for the anchor's
3026/// source's preparation ([`crate::preparation::path_prepared_hash`]): a
3027/// `span` anchor hashes its whole containing file (the span locator selects
3028/// within it; the file is the hashed unit), except under a **delivery
3029/// preparation**, where a `<path>#<key>` span names one delivery unit (the
3030/// unit's own text is the hashed unit, and a key the file no longer yields
3031/// is an absent artifact); under a **code-map** preparation a file or span
3032/// hashes the interface digest, and a `tree` hashes the code map of every
3033/// scoped file under it. A `tree` under any other preparation hashes the
3034/// plain per-file prepared-content map of its scoped files, so tree anchors
3035/// adjudicate deterministically like file anchors. A `tree` with no
3036/// resolvable source-join (the enumeration scope is undefined without one),
3037/// a partial enumeration, and a read failure observe no hash — those
3038/// resolve `recheck`, never a fabricated `drifted`. Non-hash classes
3039/// (`authored` / `informed-by`) skip the read entirely, so an anchor-less or
3040/// hash-free mem pays no observation cost.
3041fn observe_path_anchor(
3042    root: &Path,
3043    anchor: &crate::anchor::Anchor,
3044    join: Option<&AnchorSourceJoin>,
3045) -> Option<(crate::anchor::AnchorState, Option<String>)> {
3046    use crate::anchor::AnchorGrain;
3047    match anchor.grain {
3048        AnchorGrain::Span | AnchorGrain::File | AnchorGrain::Tree => {}
3049        AnchorGrain::Url | AnchorGrain::Entity => return None,
3050    }
3051    let source_pointer = join.map(|j| j.pointer.as_str());
3052    let preparation = join.and_then(|j| j.preparation.as_deref());
3053    let base = anchor_base_path(&anchor.artifact);
3054    // Decision 29: the source-join is authoritative — an artifact path is
3055    // source-relative first (joined onto the declaring source's pointer,
3056    // which may deliberately leave the workspace root for out-of-root
3057    // pointers); the workspace-relative form is tried only when the
3058    // source-join does not resolve. The candidate set is the shared
3059    // [`artifact_candidates`] rule, so resolution, the write gate, and the
3060    // population matcher read one artifact the same way.
3061    let path = {
3062        let candidates = artifact_candidates(source_pointer.unwrap_or(""), base);
3063        candidates
3064            .iter()
3065            .map(|c| root.join(c))
3066            .find(|p| p.exists())
3067            .unwrap_or_else(|| root.join(base))
3068    };
3069    if !path.exists() {
3070        return Some((
3071            crate::anchor::resolve_anchor(anchor, &crate::anchor::ArtifactObservation::Absent),
3072            None,
3073        ));
3074    }
3075    let current_hash = if !anchor.class.is_hash_bearing() {
3076        None
3077    } else if matches!(anchor.grain, AnchorGrain::File | AnchorGrain::Span) && path.is_file() {
3078        match std::fs::read(&path).ok().map(|bytes| {
3079            crate::preparation::path_prepared_hash(
3080                preparation,
3081                &anchor.artifact,
3082                anchor.grain,
3083                &bytes,
3084            )
3085        }) {
3086            Some(crate::preparation::PathPrepared::Hash(h)) => Some(h),
3087            Some(crate::preparation::PathPrepared::UnitAbsent) => {
3088                return Some((
3089                    crate::anchor::resolve_anchor(
3090                        anchor,
3091                        &crate::anchor::ArtifactObservation::Absent,
3092                    ),
3093                    None,
3094                ));
3095            }
3096            Some(crate::preparation::PathPrepared::NoHash) | None => None,
3097        }
3098    } else if anchor.grain == AnchorGrain::Tree
3099        && path.is_dir()
3100        && let Some(join) = join
3101    {
3102        // The tree's prepared form: a digest over every scoped file under
3103        // the tree, by the declaring source's own scope and the binding's
3104        // deny paths — the code map under a code-map preparation, the plain
3105        // per-file prepared-content map otherwise, so a tree anchor
3106        // adjudicates deterministically instead of resting in `recheck`
3107        // forever. The path the anchor names is workspace-relative or
3108        // source-relative; the enumeration is workspace-relative, so compare
3109        // the resolved absolute paths. A PARTIAL enumeration (malformed or
3110        // retired-dialect scope pattern) observes no hash — a digest over a
3111        // set that is not the population would silently change a stored
3112        // tree-anchor hash; no-hash resolves `recheck`, the same posture as
3113        // a failed read.
3114        let enumeration = crate::ingest::cursor::enumerate_facet_files_reported(
3115            &join.source,
3116            &join.deny_paths,
3117            root,
3118        );
3119        if enumeration.is_partial() {
3120            None
3121        } else if preparation == Some(crate::preparation::CODE_MAP) {
3122            let files: Vec<(String, String)> = enumeration
3123                .files
3124                .into_iter()
3125                .filter(|f| root.join(f).starts_with(&path))
3126                .filter_map(|f| {
3127                    std::fs::read(root.join(&f))
3128                        .ok()
3129                        .map(|bytes| (f, String::from_utf8_lossy(&bytes).into_owned()))
3130                })
3131                .collect();
3132            Some(crate::anchor::prepared_content_hash(
3133                crate::preparation::code_map_tree_digest(&files).as_bytes(),
3134            ))
3135        } else {
3136            let files: Vec<(String, Vec<u8>)> = enumeration
3137                .files
3138                .into_iter()
3139                .filter(|f| root.join(f).starts_with(&path))
3140                .filter_map(|f| std::fs::read(root.join(&f)).ok().map(|bytes| (f, bytes)))
3141                .collect();
3142            Some(crate::anchor::prepared_content_hash(
3143                crate::preparation::plain_tree_digest(&files).as_bytes(),
3144            ))
3145        }
3146    } else {
3147        None
3148    };
3149    let observation = crate::anchor::ArtifactObservation::Present {
3150        current_hash: current_hash.clone(),
3151    };
3152    Some((
3153        crate::anchor::resolve_anchor(anchor, &observation),
3154        current_hash,
3155    ))
3156}
3157
3158/// Deterministic fingerprint of a PARSED schema for the
3159/// authoring-drift equivalence check. Compares semantic content, never
3160/// raw bytes: YAML comments (the CLI-injected editor-header lines) and
3161/// whitespace vanish at parse time, and `Schema.types` — a `HashMap`
3162/// with nondeterministic iteration order — is rendered sorted by type
3163/// name so two loads of equivalent packages always fingerprint alike.
3164fn schema_parsed_fingerprint(schema: &memstead_schema::Schema) -> String {
3165    let mut keys: Vec<&String> = schema.types.keys().collect();
3166    keys.sort();
3167    let types: Vec<String> = keys
3168        .iter()
3169        .map(|k| format!("{k}={:?}", schema.types[k.as_str()]))
3170        .collect();
3171    format!(
3172        "{:?}|{}|{}",
3173        schema.manifest,
3174        schema.version,
3175        types.join(";")
3176    )
3177}
3178
3179fn anchor_references_path(anchor: &crate::anchor::Anchor, path: &str) -> bool {
3180    let base = anchor_base_path(&anchor.artifact);
3181    path_references(base, anchor.grain == crate::anchor::AnchorGrain::Tree, path)
3182}
3183
3184/// Whether `base` (a file path, or a tree root when `is_tree`) references
3185/// `path` — exact match, or containment for a tree.
3186fn path_references(base: &str, is_tree: bool, path: &str) -> bool {
3187    if base == path {
3188        return true;
3189    }
3190    if is_tree {
3191        let prefix = base.strip_suffix('/').unwrap_or(base);
3192        return path.starts_with(&format!("{prefix}/"));
3193    }
3194    false
3195}
3196
3197/// Join a source pointer and a source-relative artifact path into the
3198/// pointer-joined (workspace-relative) form — the decision-26 dialect
3199/// bridge. Plain string concatenation with a separator: the pointer is
3200/// workspace-relative (and may climb out via `..`), the artifact is
3201/// source-relative; no canonicalization here, the filesystem resolves it.
3202pub(crate) fn join_pointer(pointer: &str, base: &str) -> String {
3203    let pointer = pointer.trim_end_matches('/');
3204    if pointer.is_empty() || pointer == "." {
3205        base.to_string()
3206    } else {
3207        format!("{pointer}/{base}")
3208    }
3209}
3210
3211/// The candidate workspace-relative forms an anchor artifact could denote
3212/// under its declaring source's pointer, in the ratified priority (bundle
3213/// decision 29): the source-join first, the workspace-relative form as the
3214/// fallback. **The single implementation of that rule** — resolution, the
3215/// write-time gate, and the population scope matcher all construct their
3216/// candidate set here, so one artifact cannot read differently across the
3217/// three (each site then applies its own predicate: existence for
3218/// resolution and the gate, glob membership for the matcher).
3219///
3220/// One lexical clarification rides with the rule: an artifact that climbs
3221/// (`../…`) never joins. An artifact of a source never climbs OUT of that
3222/// source, so such a path is already the workspace-relative form; joining
3223/// anyway fabricates `<ptr>/../…`, which the filesystem then resolves into a
3224/// sibling tree — a false resolution for an existence test and a false
3225/// in-scope for a `**` glob. An artifact already carrying the pointer prefix
3226/// is NOT suppressed: on a self-nested layout both readings exist and the
3227/// decision's priority — source-join wins — settles it deterministically.
3228pub(crate) fn artifact_candidates(pointer: &str, base: &str) -> Vec<String> {
3229    let pointer = pointer.trim_end_matches('/');
3230    if pointer.is_empty() || pointer == "." {
3231        return vec![base.to_string()];
3232    }
3233    if base.starts_with("../") || base == ".." {
3234        return vec![base.to_string()];
3235    }
3236    vec![format!("{pointer}/{base}"), base.to_string()]
3237}
3238
3239#[cfg(test)]
3240mod tests {
3241    use std::path::Path;
3242
3243    use tempfile::TempDir;
3244
3245    use crate::backend::{BackendError, MemBackend};
3246    use crate::engine::test_helpers::*;
3247    use crate::engine::{Engine, EngineError, RelateEntityArgs};
3248    use crate::entity::EntityId;
3249    use crate::ops::{Direction, SearchScope, WarningHint};
3250    use crate::provenance::Provenance;
3251    use crate::storage::{ArchiveBackend, FilesystemMemWriter, MemWriter};
3252
3253    use crate::vcs::CommitContext;
3254    use crate::workspace::{Mount, MountCapability, MountLifecycle, MountStorage};
3255
3256    /// The shared decision-29 candidate rule: source-join first,
3257    /// workspace-relative fallback; a pointer-less (or `.`) source has one
3258    /// reading; a climbing `../…` artifact never joins (the fabricated
3259    /// `<ptr>/../…` would resolve into a sibling tree).
3260    #[test]
3261    fn artifact_candidates_follow_decision_29_priority() {
3262        use super::artifact_candidates;
3263        assert_eq!(artifact_candidates("", "a/b.rs"), vec!["a/b.rs"]);
3264        assert_eq!(artifact_candidates(".", "a/b.rs"), vec!["a/b.rs"]);
3265        assert_eq!(artifact_candidates("./", "a/b.rs"), vec!["a/b.rs"]);
3266        assert_eq!(
3267            artifact_candidates("sub", "a/b.rs"),
3268            vec!["sub/a/b.rs", "a/b.rs"]
3269        );
3270        assert_eq!(
3271            artifact_candidates("sub/", "a/b.rs"),
3272            vec!["sub/a/b.rs", "a/b.rs"]
3273        );
3274        // Self-nesting is settled by priority, not suppression: the artifact
3275        // already carrying the pointer prefix still offers the join first.
3276        assert_eq!(
3277            artifact_candidates("sub", "sub/x.rs"),
3278            vec!["sub/sub/x.rs", "sub/x.rs"]
3279        );
3280        // A climbing artifact is already the workspace-relative form.
3281        assert_eq!(
3282            artifact_candidates("sub", "../dev/x.md"),
3283            vec!["../dev/x.md"]
3284        );
3285        assert_eq!(
3286            artifact_candidates("../dev", "../dev/x.md"),
3287            vec!["../dev/x.md"]
3288        );
3289        assert_eq!(artifact_candidates("sub", ".."), vec![".."]);
3290    }
3291
3292    /// `schema_origin` is the trust-classification authority: a built-in
3293    /// (or workspace-authored) schema is first-party; a schema whose
3294    /// `(name, version)` is in neither catalogue is third-party — the safe
3295    /// default for an origin the engine cannot vouch for.
3296    #[test]
3297    fn schema_origin_classifies_builtin_first_party_and_unknown_third_party() {
3298        use std::sync::Arc;
3299
3300        use crate::render::OriginClass;
3301
3302        let tmp = TempDir::new().unwrap();
3303        let engine = Engine::from_mounts(vec![(
3304            folder_mount("specs", tmp.path().to_path_buf()),
3305            Box::new(FilesystemMemWriter::new(tmp.path().to_path_buf())) as Box<dyn MemBackend>,
3306        )])
3307        .unwrap();
3308
3309        // A built-in schema (the catalogue the engine resolved against).
3310        let builtin = engine.builtin_schemas()[0].clone();
3311        assert_eq!(
3312            engine.schema_origin(&builtin),
3313            OriginClass::FirstParty,
3314            "a built-in schema is first-party"
3315        );
3316
3317        // A schema whose version is in no catalogue — a stand-in for a
3318        // schema that entered from outside the workspace. Same name, a
3319        // version the engine never loaded.
3320        let foreign = Arc::new(memstead_schema::Schema {
3321            manifest: builtin.manifest.clone(),
3322            version: semver::Version::new(99, 0, 0),
3323            types: builtin.types.clone(),
3324        });
3325        assert_eq!(
3326            engine.schema_origin(&foreign),
3327            OriginClass::ThirdParty,
3328            "a schema in neither catalogue classifies third-party (safe default)"
3329        );
3330    }
3331
3332    /// `mem_origin_class` classifies a writable mount first-party (its
3333    /// content is authored in this workspace) and a read-only mount
3334    /// third-party (registry-installed read-mem or adopted foreign
3335    /// folder/clone — quoted, untrusted data). An unknown mem is
3336    /// third-party (the safe default).
3337    #[test]
3338    fn mem_origin_class_writable_first_party_readonly_third_party() {
3339        use crate::render::OriginClass;
3340
3341        let tmp = TempDir::new().unwrap();
3342        // Writable folder mem.
3343        let writable_dir = tmp.path().join("writable");
3344        std::fs::create_dir_all(&writable_dir).unwrap();
3345        let writer = FilesystemMemWriter::new(writable_dir.clone());
3346
3347        // Read-only archive mem.
3348        let body = "---\ntype: spec\n---\n# Ext\n\n## Identity\n\nFrom an archive.\n";
3349        let archive_path = build_archive(tmp.path(), "ext", &[("ext.md", body.as_bytes())]);
3350
3351        let engine = Engine::from_mounts(vec![
3352            (
3353                folder_mount("local", writable_dir),
3354                Box::new(writer) as Box<dyn MemBackend>,
3355            ),
3356            (
3357                archive_mount("external", archive_path.clone()),
3358                Box::new(ArchiveBackend::new(archive_path)) as Box<dyn MemBackend>,
3359            ),
3360        ])
3361        .unwrap();
3362
3363        assert_eq!(
3364            engine.mem_origin_class("local"),
3365            OriginClass::FirstParty,
3366            "a writable mount is first-party"
3367        );
3368        assert_eq!(
3369            engine.mem_origin_class("external"),
3370            OriginClass::ThirdParty,
3371            "a read-only mount is third-party"
3372        );
3373        assert_eq!(
3374            engine.mem_origin_class("no-such-mem"),
3375            OriginClass::ThirdParty,
3376            "an unknown mem is third-party (safe default)"
3377        );
3378    }
3379
3380    /// `declare_mem_origin` lets the embedding deployment vouch for one
3381    /// read-only mount as first-party (the curated hosted read tier),
3382    /// overriding the writability inference for that mem only — sibling
3383    /// read-only mounts keep the safe third-party default.
3384    #[test]
3385    fn declared_origin_overrides_inference_per_mem() {
3386        use crate::render::OriginClass;
3387
3388        let tmp = TempDir::new().unwrap();
3389        let body = "---\ntype: spec\n---\n# Ext\n\n## Identity\n\nFrom an archive.\n";
3390        let vouched_path = build_archive(tmp.path(), "vouched", &[("v.md", body.as_bytes())]);
3391        let other_path = build_archive(tmp.path(), "other", &[("o.md", body.as_bytes())]);
3392
3393        let mut engine = Engine::from_mounts(vec![
3394            (
3395                archive_mount("vouched", vouched_path.clone()),
3396                Box::new(ArchiveBackend::new(vouched_path)) as Box<dyn MemBackend>,
3397            ),
3398            (
3399                archive_mount("other", other_path.clone()),
3400                Box::new(ArchiveBackend::new(other_path)) as Box<dyn MemBackend>,
3401            ),
3402        ])
3403        .unwrap();
3404
3405        engine.declare_mem_origin("vouched", OriginClass::FirstParty);
3406
3407        assert_eq!(
3408            engine.mem_origin_class("vouched"),
3409            OriginClass::FirstParty,
3410            "the deployment's declaration wins over the read-only inference"
3411        );
3412        assert_eq!(
3413            engine.mem_origin_class("other"),
3414            OriginClass::ThirdParty,
3415            "an undeclared sibling mount keeps the safe default"
3416        );
3417    }
3418
3419    /// The adopt-gate: a non-built-in schema is first-party only once a
3420    /// writable mount pins it (the operator authors against it here).
3421    /// Pinned only by a read-only mount — a registry read-mem or an
3422    /// adopted foreign folder/clone — it stays third-party, so
3423    /// `memstead_schema` serves it structural-only.
3424    #[test]
3425    fn schema_origin_third_party_until_pinned_by_a_writable_mount() {
3426        use memstead_schema::SchemaRef;
3427
3428        use crate::render::OriginClass;
3429
3430        let manifest = r#"name: trust-test
3431version: 0.1.0
3432description: adopt-gate test schema
3433when_to_use: tests
3434types:
3435  - doc
3436relationships:
3437  mode: strict
3438  definitions:
3439    - name: _default
3440      description: fallback
3441      default_weight: 1.0
3442community:
3443  resolution: 1.0
3444  seed: 42
3445"#;
3446        let pin = SchemaRef::new("trust-test", semver::Version::new(0, 1, 0));
3447
3448        let mk_engine = |cap: MountCapability| -> Engine {
3449            let tmp = TempDir::new().unwrap();
3450            let schemas_dir = tmp.path().join("schemas");
3451            std::fs::create_dir_all(&schemas_dir).unwrap();
3452            write_schema_files_with_default_type(&schemas_dir, "trust-test", manifest, &["doc"]);
3453            let mem_dir = tmp.path().join("mem");
3454            std::fs::create_dir_all(&mem_dir).unwrap();
3455            let mount = Mount {
3456                mem: "v".to_string(),
3457                schema: Some(pin.clone()),
3458                storage: MountStorage::Folder {
3459                    path: mem_dir.clone(),
3460                },
3461                capability: cap,
3462                lifecycle: MountLifecycle::Eager,
3463                cross_linkable: true,
3464                migration_target: None,
3465            };
3466            let backend = Box::new(FilesystemMemWriter::new(mem_dir)) as Box<dyn MemBackend>;
3467            // Keep `tmp` alive for the engine's lifetime by leaking it —
3468            // the test process is short-lived and the folder must outlast
3469            // the closure.
3470            std::mem::forget(tmp);
3471            Engine::from_mounts_with_schemas_dir(vec![(mount, backend)], Some(&schemas_dir))
3472                .unwrap()
3473        };
3474
3475        // Read-only mount: the foreign schema is never adopted → third-party.
3476        let ro = mk_engine(MountCapability::ReadOnly);
3477        let schema = ro.schemas().get("v").expect("schema resolved").clone();
3478        assert_eq!(
3479            ro.schema_origin(&schema),
3480            OriginClass::ThirdParty,
3481            "a non-built-in schema pinned only by a read-only mount is third-party"
3482        );
3483
3484        // Writable mount pinning the same schema: adopted → first-party.
3485        let rw = mk_engine(MountCapability::Write);
3486        let schema = rw.schemas().get("v").expect("schema resolved").clone();
3487        assert_eq!(
3488            rw.schema_origin(&schema),
3489            OriginClass::FirstParty,
3490            "a writable mount pinning the schema adopts it → first-party"
3491        );
3492    }
3493
3494    /// Consumer read path: an installed (archive-backed) mem that ships
3495    /// a `.memstead/provenance.json` payload surfaces per-entity authoring
3496    /// provenance through `archive_provenance_for`. A noted entity carries
3497    /// its rationale; an entity authored without a note is absent from the
3498    /// payload and reads as provenance-absent (no fabricated value); the
3499    /// `history` disposition records that full history is not shipped.
3500    #[test]
3501    fn archive_provenance_surfaces_per_entity_and_reports_absence() {
3502        use memstead_schema::History;
3503
3504        let tmp = TempDir::new().unwrap();
3505        let config = br#"{"format":3,"name":"seed","version":"0.1.0","schema":"default@1.0.0"}"#;
3506        let alpha = b"---\ntype: spec\n---\n# Alpha\n\n## Identity\n\na\n\n## Purpose\n\np\n";
3507        let beta = b"---\ntype: spec\n---\n# Beta\n\n## Identity\n\nb\n\n## Purpose\n\np\n";
3508        // alpha noted; beta deliberately absent from the payload.
3509        let provenance = br#"{"format":1,"history":"summarised","entities":{"alpha":{"rationale":"why alpha exists","kind":"create","timestamp":"2026-06-24T00:00:00Z","actor":"agent"}}}"#;
3510        let archive = build_archive(
3511            tmp.path(),
3512            "seed",
3513            &[
3514                (".memstead/config.json", config),
3515                ("alpha.md", alpha),
3516                ("beta.md", beta),
3517                (".memstead/provenance.json", provenance),
3518            ],
3519        );
3520        let engine = Engine::from_mounts(vec![(
3521            archive_mount("seed", archive.clone()),
3522            Box::new(ArchiveBackend::new(archive)) as Box<dyn MemBackend>,
3523        )])
3524        .unwrap();
3525
3526        let prov = engine
3527            .archive_provenance_for("seed")
3528            .expect("provenance payload read from the archive");
3529        assert_eq!(
3530            prov.history,
3531            History::Summarised,
3532            "history-not-shipped is observable"
3533        );
3534        assert_eq!(
3535            prov.entity("alpha").and_then(|r| r.rationale.as_deref()),
3536            Some("why alpha exists"),
3537            "noted entity surfaces its rationale"
3538        );
3539        assert!(
3540            prov.entity("beta").is_none(),
3541            "unnoted entity is absent (reported absent, not fabricated)"
3542        );
3543    }
3544
3545    /// A pre-provenance archive (no `.memstead/provenance.json`) reads as
3546    /// provenance uniformly absent — the additive contract: a newer engine
3547    /// installing an old archive reports no provenance, never an error.
3548    #[test]
3549    fn archive_without_provenance_reports_absent() {
3550        let tmp = TempDir::new().unwrap();
3551        let config = br#"{"format":3,"name":"seed","version":"0.1.0","schema":"default@1.0.0"}"#;
3552        let alpha = b"---\ntype: spec\n---\n# Alpha\n\n## Identity\n\na\n\n## Purpose\n\np\n";
3553        let archive = build_archive(
3554            tmp.path(),
3555            "seed",
3556            &[(".memstead/config.json", config), ("alpha.md", alpha)],
3557        );
3558        let engine = Engine::from_mounts(vec![(
3559            archive_mount("seed", archive.clone()),
3560            Box::new(ArchiveBackend::new(archive)) as Box<dyn MemBackend>,
3561        )])
3562        .unwrap();
3563        assert!(
3564            engine.archive_provenance_for("seed").is_none(),
3565            "an archive without a provenance payload reports provenance absent"
3566        );
3567    }
3568
3569    #[test]
3570    fn folder_mount_routes_reads_to_filesystem_backend() {
3571        let tmp = TempDir::new().unwrap();
3572        let mem_dir = tmp.path().to_path_buf();
3573        let writer = FilesystemMemWriter::new(mem_dir.clone());
3574        // MemWriter and MemBackend share method names; the
3575        // module-top `use` brings both into scope. Seed via fully-
3576        // qualified MemWriter calls so dot-syntax stays unambiguous.
3577        <FilesystemMemWriter as MemWriter>::write_entity(&writer, Path::new("a.md"), b"alpha")
3578            .unwrap();
3579        <FilesystemMemWriter as MemWriter>::commit(&writer, "seed", &CommitContext::internal())
3580            .unwrap();
3581
3582        let engine = Engine::from_mounts(vec![(
3583            folder_mount("specs", mem_dir),
3584            Box::new(writer) as Box<dyn MemBackend>,
3585        )])
3586        .unwrap();
3587
3588        let mut paths: Vec<String> = engine
3589            .list_entities("specs")
3590            .unwrap()
3591            .into_iter()
3592            .map(|p| p.to_string_lossy().into_owned())
3593            .collect();
3594        paths.sort();
3595        assert_eq!(paths, vec!["a.md".to_string()]);
3596
3597        assert_eq!(
3598            engine.read_entity("specs", Path::new("a.md")).unwrap(),
3599            Some(b"alpha".to_vec())
3600        );
3601    }
3602
3603    #[test]
3604    fn heterogeneous_mounts_route_to_correct_backend() {
3605        let tmp = TempDir::new().unwrap();
3606
3607        // Folder mem.
3608        let folder_dir = tmp.path().join("folder-mem");
3609        std::fs::create_dir_all(&folder_dir).unwrap();
3610        let folder_writer = FilesystemMemWriter::new(folder_dir.clone());
3611        <FilesystemMemWriter as MemWriter>::write_entity(
3612            &folder_writer,
3613            Path::new("local.md"),
3614            b"local",
3615        )
3616        .unwrap();
3617        <FilesystemMemWriter as MemWriter>::commit(
3618            &folder_writer,
3619            "seed",
3620            &CommitContext::internal(),
3621        )
3622        .unwrap();
3623
3624        // Archive mem.
3625        let archive_path = build_archive(
3626            tmp.path(),
3627            "external",
3628            &[("ext.md", b"external"), ("dir/nested.md", b"nested")],
3629        );
3630
3631        let engine = Engine::from_mounts(vec![
3632            (
3633                folder_mount("local", folder_dir),
3634                Box::new(folder_writer) as Box<dyn MemBackend>,
3635            ),
3636            (
3637                archive_mount("external", archive_path.clone()),
3638                Box::new(ArchiveBackend::new(archive_path)),
3639            ),
3640        ])
3641        .unwrap();
3642
3643        // Routes correctly by mem name.
3644        assert_eq!(engine.mem_names(), vec!["local", "external"]);
3645        assert_eq!(
3646            engine.read_entity("local", Path::new("local.md")).unwrap(),
3647            Some(b"local".to_vec())
3648        );
3649        assert_eq!(
3650            engine.read_entity("external", Path::new("ext.md")).unwrap(),
3651            Some(b"external".to_vec())
3652        );
3653        assert_eq!(
3654            engine
3655                .read_entity("external", Path::new("dir/nested.md"))
3656                .unwrap(),
3657            Some(b"nested".to_vec())
3658        );
3659        // Cross-routing: reading a path from the wrong mem → None
3660        // (the backend doesn't have it), not an error.
3661        assert_eq!(
3662            engine.read_entity("local", Path::new("ext.md")).unwrap(),
3663            None
3664        );
3665        assert_eq!(
3666            engine
3667                .read_entity("external", Path::new("local.md"))
3668                .unwrap(),
3669            None
3670        );
3671    }
3672
3673    #[test]
3674    fn edge_is_from_readonly_classifies_every_edge_by_source_mount_capability() {
3675        // `engine.edge_is_from_readonly` is the derived-on-demand
3676        // alternative to adding a per-edge marker: construct a mixed
3677        // workspace (one Write-Mem + one ReadOnly archive with
3678        // cross-mem wiki-links) and walk every edge in the store,
3679        // asserting each edge's source-mount capability.
3680        let tmp = TempDir::new().unwrap();
3681
3682        // Write folder mem `local` with a spec-shaped entity that
3683        // declares an explicit cross-mem relation into the archive
3684        // (under the alias model edges originate from `## Relationships`).
3685        let folder_dir = tmp.path().join("local-mem");
3686        std::fs::create_dir_all(&folder_dir).unwrap();
3687        let folder_writer = FilesystemMemWriter::new(folder_dir.clone());
3688        let local_md = b"---\ntype: spec\n---\n# Note\n\n## Identity\n\nsee [[external:archived]] for prior context.\n\n## Relationships\n\n- **REFERENCES**: [[external:archived]]\n";
3689        <FilesystemMemWriter as MemWriter>::write_entity(
3690            &folder_writer,
3691            Path::new("note.md"),
3692            local_md,
3693        )
3694        .unwrap();
3695        <FilesystemMemWriter as MemWriter>::commit(
3696            &folder_writer,
3697            "seed",
3698            &CommitContext::internal(),
3699        )
3700        .unwrap();
3701
3702        // ReadOnly archive mem `external` with a spec-shaped entity
3703        // declaring an explicit cross-mem relation back to the local
3704        // note.
3705        let archive_md = b"---\ntype: spec\n---\n# Archived\n\n## Identity\n\nrefers back to [[local:note]] for the current revision.\n\n## Relationships\n\n- **REFERENCES**: [[local:note]]\n";
3706        let archive_path = build_archive(tmp.path(), "external", &[("archived.md", archive_md)]);
3707
3708        let engine = Engine::from_mounts(vec![
3709            (
3710                folder_mount("local", folder_dir),
3711                Box::new(folder_writer) as Box<dyn MemBackend>,
3712            ),
3713            (
3714                archive_mount("external", archive_path.clone()),
3715                Box::new(ArchiveBackend::new(archive_path)),
3716            ),
3717        ])
3718        .unwrap();
3719
3720        // Sanity: both entities are real, both mems are mounted.
3721        let local_id = EntityId::new("local", "note");
3722        let archived_id = EntityId::new("external", "archived");
3723        assert!(engine.get_entity(&local_id).is_some());
3724        assert!(engine.get_entity(&archived_id).is_some());
3725        assert!(matches!(
3726            engine.capability("local").unwrap(),
3727            MountCapability::Write
3728        ));
3729        assert!(matches!(
3730            engine.capability("external").unwrap(),
3731            MountCapability::ReadOnly
3732        ));
3733
3734        // Walk every edge in the store. For each (from, edge) pair,
3735        // `edge_is_from_readonly(from)` must return true iff the
3736        // source mount's capability is ReadOnly. The fixture's two
3737        // wiki-links produce one edge from each mem — both halves
3738        // exercise both branches of the helper.
3739        let mut seen_write_edge = false;
3740        let mut seen_readonly_edge = false;
3741        for from in engine.store().all_ids().cloned().collect::<Vec<_>>() {
3742            for _edge in engine.store().outgoing(&from) {
3743                let is_ro = engine.edge_is_from_readonly(&from);
3744                match engine.capability(from.mem()).unwrap() {
3745                    MountCapability::Write => {
3746                        assert!(
3747                            !is_ro,
3748                            "edge from write mem {} reported as ReadOnly",
3749                            from.mem()
3750                        );
3751                        seen_write_edge = true;
3752                    }
3753                    MountCapability::ReadOnly => {
3754                        assert!(
3755                            is_ro,
3756                            "edge from readonly mem {} reported as Write",
3757                            from.mem()
3758                        );
3759                        seen_readonly_edge = true;
3760                    }
3761                }
3762            }
3763        }
3764        assert!(
3765            seen_write_edge,
3766            "fixture must produce at least one edge from a write mem"
3767        );
3768        assert!(
3769            seen_readonly_edge,
3770            "fixture must produce at least one edge from a readonly mem"
3771        );
3772
3773        // Helper also reports `false` for mems absent from the
3774        // router — no mount → no ReadOnly assertion can be made.
3775        let phantom = EntityId::new("missing-mem", "phantom");
3776        assert!(
3777            !engine.edge_is_from_readonly(&phantom),
3778            "absent mount must not be reported as ReadOnly"
3779        );
3780    }
3781
3782    // ---- Engine::changes_since wrapper ------------------------------
3783
3784    #[test]
3785    fn cross_mem_link_allowed_same_mem_always_true() {
3786        // Self-edges (from == to) bypass the cross-mem policy
3787        // entirely — the policy gates *cross*-mem edges only.
3788        let tmp = TempDir::new().unwrap();
3789        let engine = build_demo_engine(&tmp);
3790        assert!(engine.cross_mem_link_allowed("specs", "specs"));
3791        // Even when the mem doesn't exist (not enrolled in
3792        // settings.cross_mem_links), same-mem returns true —
3793        // the engine doesn't validate mem existence here, just the
3794        // policy.
3795        assert!(engine.cross_mem_link_allowed("anywhere", "anywhere"));
3796    }
3797
3798    #[test]
3799    fn cross_mem_link_allowed_absent_denies_by_default() {
3800        // No entry in cross_mem_links for `from_mem` → denied.
3801        // Default-deny is the V1 posture; operators opt in.
3802        let tmp = TempDir::new().unwrap();
3803        let engine = build_demo_engine(&tmp);
3804        assert!(!engine.cross_mem_link_allowed("specs", "engine"));
3805        assert!(!engine.cross_mem_link_allowed("missing", "anywhere"));
3806    }
3807
3808    #[test]
3809    fn cross_mem_link_allowed_wildcard_admits_any_target() {
3810        use memstead_schema::workspace_config::CrossLinkValue;
3811        let tmp = TempDir::new().unwrap();
3812        let mut engine = build_demo_engine(&tmp);
3813        let mut settings = crate::workspace::WorkspaceSettings::default();
3814        settings
3815            .cross_mem_links
3816            .insert("specs".to_string(), CrossLinkValue::Wildcard);
3817        engine.set_settings(settings);
3818        assert!(engine.cross_mem_link_allowed("specs", "engine"));
3819        assert!(engine.cross_mem_link_allowed("specs", "macos"));
3820        assert!(engine.cross_mem_link_allowed("specs", "any-other"));
3821        // Reverse direction is independent — no policy entry for
3822        // engine→specs means denied.
3823        assert!(!engine.cross_mem_link_allowed("engine", "specs"));
3824    }
3825
3826    #[test]
3827    fn cross_mem_link_allowed_allowlist_enforces_membership() {
3828        use memstead_schema::workspace_config::CrossLinkValue;
3829        let tmp = TempDir::new().unwrap();
3830        let mut engine = build_demo_engine(&tmp);
3831        let mut settings = crate::workspace::WorkspaceSettings::default();
3832        settings.cross_mem_links.insert(
3833            "specs".to_string(),
3834            CrossLinkValue::List(vec!["engine".to_string(), "macos".to_string()]),
3835        );
3836        engine.set_settings(settings);
3837        assert!(engine.cross_mem_link_allowed("specs", "engine"));
3838        assert!(engine.cross_mem_link_allowed("specs", "macos"));
3839        assert!(!engine.cross_mem_link_allowed("specs", "external"));
3840    }
3841
3842    #[test]
3843    fn cross_mem_link_allowed_synthesises_from_matching_create_rule_wildcard() {
3844        // No explicit cross_mem_links entry, but a create rule
3845        // matches `from_mem` and carries default_cross_links = "*".
3846        // Synthesis grants permission to any target.
3847        use memstead_schema::workspace_config::CrossLinkValue;
3848        let tmp = TempDir::new().unwrap();
3849        let mut engine = build_demo_engine(&tmp);
3850        let mut settings = crate::workspace::WorkspaceSettings::default();
3851        settings
3852            .mem_create_rules
3853            .push(crate::workspace::CreateRuleSetting {
3854                pattern: "exec-*".to_string(),
3855                schemas: vec!["default".to_string()],
3856                default_cross_links: Some(CrossLinkValue::Wildcard),
3857            });
3858        engine.set_settings(settings);
3859        // No explicit policy; synthesis grants permission for any
3860        // target because the rule's value is Wildcard.
3861        assert!(engine.cross_mem_link_allowed("exec-foo", "specs"));
3862        assert!(engine.cross_mem_link_allowed("exec-foo", "engine"));
3863        // Mem that doesn't match any rule → still denied.
3864        assert!(!engine.cross_mem_link_allowed("orphan", "specs"));
3865    }
3866
3867    /// #42: synthesis matches a hierarchical mem by composing the same
3868    /// `<mem_path>/<name>` candidate the create-rule glob is keyed on,
3869    /// not the bare leaf. Before the fix, `from_mem = "project"` could
3870    /// never match a `memstead/*` rule (the leaf-vs-composed-path
3871    /// divergence), so enforcement denied a link `memstead_overview`
3872    /// rendered as rule-granted.
3873    #[test]
3874    fn cross_mem_link_allowed_synthesises_for_hierarchical_mem() {
3875        use memstead_schema::workspace_config::CrossLinkValue;
3876        let tmp = TempDir::new().unwrap();
3877        let mem_dir = tmp.path().to_path_buf();
3878        // Mount `project` with a hierarchical branch so its `mem_path()`
3879        // is "memstead" and the composed candidate is "memstead/project".
3880        // The Folder backend handles loading; only the Mount's storage
3881        // feeds `mem_path()`.
3882        let mount = Mount {
3883            mem: "project".into(),
3884            schema: Some(pin("default")),
3885            storage: MountStorage::GitBranch {
3886                gitdir: mem_dir.join(".git"),
3887                branch: "memstead/project".into(),
3888            },
3889            capability: MountCapability::Write,
3890            lifecycle: MountLifecycle::Eager,
3891            cross_linkable: true,
3892            migration_target: None,
3893        };
3894        let writer = FilesystemMemWriter::new(mem_dir.clone());
3895        let mut engine =
3896            Engine::from_mounts(vec![(mount, Box::new(writer) as Box<dyn MemBackend>)]).unwrap();
3897        let mut settings = crate::workspace::WorkspaceSettings::default();
3898        settings
3899            .mem_create_rules
3900            .push(crate::workspace::CreateRuleSetting {
3901                pattern: "memstead/*".to_string(),
3902                schemas: vec!["default".to_string()],
3903                default_cross_links: Some(CrossLinkValue::List(vec!["engine".to_string()])),
3904            });
3905        engine.set_settings(settings);
3906        assert!(
3907            engine.cross_mem_link_allowed("project", "engine"),
3908            "synthesis must match via the composed `memstead/project` candidate"
3909        );
3910        assert!(
3911            !engine.cross_mem_link_allowed("project", "macos"),
3912            "a target outside the rule's default_cross_links is still denied"
3913        );
3914    }
3915
3916    #[test]
3917    fn cross_mem_link_allowed_synthesises_from_matching_create_rule_list() {
3918        // Create rule's default_cross_links is a list — synthesis
3919        // grants permission to listed targets only.
3920        use memstead_schema::workspace_config::CrossLinkValue;
3921        let tmp = TempDir::new().unwrap();
3922        let mut engine = build_demo_engine(&tmp);
3923        let mut settings = crate::workspace::WorkspaceSettings::default();
3924        settings
3925            .mem_create_rules
3926            .push(crate::workspace::CreateRuleSetting {
3927                pattern: "exec-*".to_string(),
3928                schemas: vec!["default".to_string()],
3929                default_cross_links: Some(CrossLinkValue::List(vec!["specs".to_string()])),
3930            });
3931        engine.set_settings(settings);
3932        assert!(engine.cross_mem_link_allowed("exec-foo", "specs"));
3933        // Target not in the synthesised list → denied.
3934        assert!(!engine.cross_mem_link_allowed("exec-foo", "engine"));
3935    }
3936
3937    #[test]
3938    fn cross_mem_link_allowed_explicit_policy_wins_over_synthesis() {
3939        // Explicit cross_mem_links wildcard fires first; the
3940        // synthesis layer is never consulted (and would deny).
3941        use memstead_schema::workspace_config::CrossLinkValue;
3942        let tmp = TempDir::new().unwrap();
3943        let mut engine = build_demo_engine(&tmp);
3944        let mut settings = crate::workspace::WorkspaceSettings::default();
3945        settings
3946            .cross_mem_links
3947            .insert("exec-foo".to_string(), CrossLinkValue::Wildcard);
3948        // The synthesis layer would deny `exec-foo → engine` (no
3949        // matching rule), but explicit policy returns true first.
3950        engine.set_settings(settings);
3951        assert!(engine.cross_mem_link_allowed("exec-foo", "engine"));
3952    }
3953
3954    #[test]
3955    fn cross_mem_link_allowed_synthesis_unions_into_explicit_list() {
3956        // Explicit list = ["specs"]; create rule synthesises = ["macos"].
3957        // Effective allowed targets: union ({specs, macos}).
3958        use memstead_schema::workspace_config::CrossLinkValue;
3959        let tmp = TempDir::new().unwrap();
3960        let mut engine = build_demo_engine(&tmp);
3961        let mut settings = crate::workspace::WorkspaceSettings::default();
3962        settings.cross_mem_links.insert(
3963            "exec-foo".to_string(),
3964            CrossLinkValue::List(vec!["specs".to_string()]),
3965        );
3966        settings
3967            .mem_create_rules
3968            .push(crate::workspace::CreateRuleSetting {
3969                pattern: "exec-*".to_string(),
3970                schemas: vec!["default".to_string()],
3971                default_cross_links: Some(CrossLinkValue::List(vec!["macos".to_string()])),
3972            });
3973        engine.set_settings(settings);
3974        // Explicit allowlist contains specs → allowed.
3975        assert!(engine.cross_mem_link_allowed("exec-foo", "specs"));
3976        // Synthesis layer adds macos → allowed.
3977        assert!(engine.cross_mem_link_allowed("exec-foo", "macos"));
3978        // Neither layer allows engine → denied.
3979        assert!(!engine.cross_mem_link_allowed("exec-foo", "engine"));
3980    }
3981
3982    #[test]
3983    fn cross_mem_link_allowed_set_settings_invalidates_compiled_rule_cache() {
3984        // After set_settings, a fresh policy must be reflected on the
3985        // next call — the lazy memo can't return stale rules.
3986        use memstead_schema::workspace_config::CrossLinkValue;
3987        let tmp = TempDir::new().unwrap();
3988        let mut engine = build_demo_engine(&tmp);
3989
3990        // First settings: a rule allows exec-* → specs via synthesis.
3991        let mut s1 = crate::workspace::WorkspaceSettings::default();
3992        s1.mem_create_rules
3993            .push(crate::workspace::CreateRuleSetting {
3994                pattern: "exec-*".to_string(),
3995                schemas: vec!["default".to_string()],
3996                default_cross_links: Some(CrossLinkValue::List(vec!["specs".to_string()])),
3997            });
3998        engine.set_settings(s1);
3999        assert!(engine.cross_mem_link_allowed("exec-foo", "specs"));
4000
4001        // Replace settings: the rule no longer carries
4002        // default_cross_links. Cache must invalidate so the next
4003        // call sees the new policy.
4004        let mut s2 = crate::workspace::WorkspaceSettings::default();
4005        s2.mem_create_rules
4006            .push(crate::workspace::CreateRuleSetting {
4007                pattern: "exec-*".to_string(),
4008                schemas: vec!["default".to_string()],
4009                default_cross_links: None,
4010            });
4011        engine.set_settings(s2);
4012        assert!(!engine.cross_mem_link_allowed("exec-foo", "specs"));
4013    }
4014
4015    #[test]
4016    fn cross_mem_link_allowed_malformed_glob_falls_back_to_explicit_policy() {
4017        // Malformed pattern in a create rule causes CreateRuleSet
4018        // compilation to fail; the resolver logs and disables
4019        // synthesis, but explicit cross_mem_links still works.
4020        use memstead_schema::workspace_config::CrossLinkValue;
4021        let tmp = TempDir::new().unwrap();
4022        let mut engine = build_demo_engine(&tmp);
4023        let mut settings = crate::workspace::WorkspaceSettings::default();
4024        settings
4025            .mem_create_rules
4026            .push(crate::workspace::CreateRuleSetting {
4027                pattern: "[unclosed".to_string(),
4028                schemas: vec!["default".to_string()],
4029                default_cross_links: Some(CrossLinkValue::Wildcard),
4030            });
4031        // Explicit policy still works.
4032        settings
4033            .cross_mem_links
4034            .insert("specs".to_string(), CrossLinkValue::Wildcard);
4035        engine.set_settings(settings);
4036        // Explicit policy: specs → engine allowed.
4037        assert!(engine.cross_mem_link_allowed("specs", "engine"));
4038        // Synthesis disabled (compilation failed); rule's would-be
4039        // wildcard doesn't apply.
4040        assert!(!engine.cross_mem_link_allowed("orphan", "anything"));
4041    }
4042
4043    #[test]
4044    fn cross_mem_link_allowed_empty_list_denies_all_cross_mem_targets() {
4045        // [cross_mem_links] specs = [] is the explicit
4046        // "intentionally locked down" shape — same effect as
4047        // default-deny but operator-acknowledged.
4048        use memstead_schema::workspace_config::CrossLinkValue;
4049        let tmp = TempDir::new().unwrap();
4050        let mut engine = build_demo_engine(&tmp);
4051        let mut settings = crate::workspace::WorkspaceSettings::default();
4052        settings
4053            .cross_mem_links
4054            .insert("specs".to_string(), CrossLinkValue::List(Vec::new()));
4055        engine.set_settings(settings);
4056        // Same-mem still passes — policy only gates cross-mem.
4057        assert!(engine.cross_mem_link_allowed("specs", "specs"));
4058        // Cross-mem denied to every target.
4059        assert!(!engine.cross_mem_link_allowed("specs", "engine"));
4060        assert!(!engine.cross_mem_link_allowed("specs", "anything"));
4061    }
4062
4063    #[test]
4064    fn from_mounts_load_warnings_merge_into_health_summary() {
4065        let tmp = TempDir::new().unwrap();
4066        let mem_dir = tmp.path().to_path_buf();
4067        let body = "---\ntype: spec\n---\n# Dup2\n\n## Identity\n\na.\n\n## Identity\n\nb.\n";
4068        std::fs::write(mem_dir.join("dup2.md"), body).unwrap();
4069
4070        let writer = FilesystemMemWriter::new(mem_dir.clone());
4071        let engine = Engine::from_mounts(vec![(
4072            folder_mount("specs", mem_dir),
4073            Box::new(writer) as Box<dyn MemBackend>,
4074        )])
4075        .unwrap();
4076
4077        let summary = engine.health();
4078        assert!(
4079            summary
4080                .warnings
4081                .iter()
4082                .any(|w| matches!(w, WarningHint::DuplicateSectionHeading { .. })),
4083            "health() must merge load_warnings into summary.warnings: {:?}",
4084            summary.warnings,
4085        );
4086    }
4087
4088    #[test]
4089    fn workspace_root_accessor_is_none_for_engine_built_from_mounts() {
4090        let tmp = TempDir::new().unwrap();
4091        let mem_dir = tmp.path().to_path_buf();
4092        let writer = FilesystemMemWriter::new(mem_dir.clone());
4093        // Newest default generation so the clean-boot assertion below
4094        // isn't tripped by the SCHEMA_GENERATIONS_BEHIND hint.
4095        let mut mount = folder_mount("specs", mem_dir);
4096        mount.schema = Some("default@1.3.0".parse().unwrap());
4097        let engine =
4098            Engine::from_mounts(vec![(mount, Box::new(writer) as Box<dyn MemBackend>)]).unwrap();
4099        assert!(
4100            engine.workspace_root().is_none(),
4101            "from_mounts has no workspace path",
4102        );
4103        // An entity-less mount reports itself empty; nothing else.
4104        assert!(
4105            engine
4106                .load_warnings()
4107                .iter()
4108                .all(|w| w.code() == "MOUNT_UNBACKED"),
4109            "{:?}",
4110            engine.load_warnings()
4111        );
4112    }
4113
4114    #[test]
4115    fn health_omits_outer_repo_warning_when_workspace_root_unset() {
4116        let tmp = TempDir::new().unwrap();
4117        let mem_dir = tmp.path().to_path_buf();
4118        let writer = FilesystemMemWriter::new(mem_dir.clone());
4119        let engine = Engine::from_mounts(vec![(
4120            folder_mount("specs", mem_dir),
4121            Box::new(writer) as Box<dyn MemBackend>,
4122        )])
4123        .unwrap();
4124        let health = engine.health();
4125        assert!(
4126            !health
4127                .warnings
4128                .iter()
4129                .any(|w| matches!(w, WarningHint::OuterRepoNotIgnoringMemRepo { .. })),
4130            "outer-repo check must skip when workspace_root is None",
4131        );
4132    }
4133
4134    #[test]
4135    fn writable_mem_names_filters_by_capability() {
4136        let tmp = TempDir::new().unwrap();
4137        let mem_dir = tmp.path().to_path_buf();
4138        let writer = FilesystemMemWriter::new(mem_dir.clone());
4139        let archive_path = build_archive(tmp.path(), "ext", &[("a.md", b"a")]);
4140
4141        let engine = Engine::from_mounts(vec![
4142            (
4143                folder_mount("writable", mem_dir),
4144                Box::new(writer) as Box<dyn MemBackend>,
4145            ),
4146            (
4147                archive_mount("sealed", archive_path.clone()),
4148                Box::new(ArchiveBackend::new(archive_path)),
4149            ),
4150        ])
4151        .unwrap();
4152
4153        // Only the writable mount surfaces; the archive (read-only)
4154        // is filtered out.
4155        let names = engine.writable_mem_names();
4156        assert_eq!(names, vec!["writable"]);
4157    }
4158
4159    /// The default writable mem is
4160    /// the FIRST writable mount in declaration order — the stable seed,
4161    /// not the alphabetically-first name. `test` is declared first;
4162    /// `other` sorts ahead alphabetically but is declared second, so it
4163    /// is NOT the default. This is the invariant that stops a second
4164    /// mem from silently retargeting omitted-`mem` writes.
4165    #[test]
4166    fn default_writable_mem_is_declaration_first_not_alphabetical() {
4167        let tmp = TempDir::new().unwrap();
4168        let test_dir = tmp.path().join("test");
4169        let other_dir = tmp.path().join("other");
4170        std::fs::create_dir_all(&test_dir).unwrap();
4171        std::fs::create_dir_all(&other_dir).unwrap();
4172
4173        let engine = Engine::from_mounts(vec![
4174            (
4175                folder_mount("test", test_dir.clone()),
4176                Box::new(FilesystemMemWriter::new(test_dir)) as Box<dyn MemBackend>,
4177            ),
4178            (
4179                folder_mount("other", other_dir.clone()),
4180                Box::new(FilesystemMemWriter::new(other_dir)) as Box<dyn MemBackend>,
4181            ),
4182        ])
4183        .unwrap();
4184
4185        assert_eq!(
4186            engine.default_writable_mem(),
4187            Some("test"),
4188            "default must be the declaration-first writable mem, not the alphabetically-first",
4189        );
4190    }
4191
4192    /// Reverse declaration order to prove the default tracks declaration
4193    /// order rather than a fixed name: with `other` declared first it
4194    /// becomes the default. Together with the test above this pins the
4195    /// lean as mount order, not name sort.
4196    #[test]
4197    fn default_writable_mem_follows_declaration_order() {
4198        let tmp = TempDir::new().unwrap();
4199        let other_dir = tmp.path().join("other");
4200        let test_dir = tmp.path().join("test");
4201        std::fs::create_dir_all(&other_dir).unwrap();
4202        std::fs::create_dir_all(&test_dir).unwrap();
4203
4204        let engine = Engine::from_mounts(vec![
4205            (
4206                folder_mount("other", other_dir.clone()),
4207                Box::new(FilesystemMemWriter::new(other_dir)) as Box<dyn MemBackend>,
4208            ),
4209            (
4210                folder_mount("test", test_dir.clone()),
4211                Box::new(FilesystemMemWriter::new(test_dir)) as Box<dyn MemBackend>,
4212            ),
4213        ])
4214        .unwrap();
4215
4216        assert_eq!(engine.default_writable_mem(), Some("other"));
4217    }
4218
4219    /// A read-only-only workspace has no default writable mem.
4220    #[test]
4221    fn default_writable_mem_none_without_writable_mount() {
4222        let tmp = TempDir::new().unwrap();
4223        let archive_path = build_archive(tmp.path(), "ext", &[("a.md", b"a")]);
4224        let engine = Engine::from_mounts(vec![(
4225            archive_mount("sealed", archive_path.clone()),
4226            Box::new(ArchiveBackend::new(archive_path)) as Box<dyn MemBackend>,
4227        )])
4228        .unwrap();
4229        assert_eq!(engine.default_writable_mem(), None);
4230    }
4231
4232    #[test]
4233    fn folder_path_for_mem_returns_path_for_folder_mounts_only() {
4234        let tmp = TempDir::new().unwrap();
4235        let mem_dir = tmp.path().join("specs");
4236        std::fs::create_dir_all(&mem_dir).unwrap();
4237        let writer = FilesystemMemWriter::new(mem_dir.clone());
4238        let archive_path = build_archive(tmp.path(), "ext", &[("a.md", b"a")]);
4239
4240        let engine = Engine::from_mounts(vec![
4241            (
4242                folder_mount("specs", mem_dir.clone()),
4243                Box::new(writer) as Box<dyn MemBackend>,
4244            ),
4245            (
4246                archive_mount("sealed", archive_path.clone()),
4247                Box::new(ArchiveBackend::new(archive_path)),
4248            ),
4249        ])
4250        .unwrap();
4251
4252        // Folder mount returns its path.
4253        assert_eq!(engine.folder_path_for_mem("specs"), Some(mem_dir.as_path()),);
4254        // Archive mount returns None — caller branches on storage type.
4255        assert_eq!(engine.folder_path_for_mem("sealed"), None);
4256        // Unknown mem returns None — same as Engine::mount.
4257        assert_eq!(engine.folder_path_for_mem("missing"), None);
4258    }
4259
4260    #[test]
4261    fn mount_accessor_returns_public_mount_shape() {
4262        // Build a heterogeneous engine and verify Engine::mount /
4263        // Engine::mounts surface the operator-facing Mount records.
4264        // Handlers branch on MountStorage variants through this
4265        // accessor (replacing full's gitdir_for / worktree_for /
4266        // mem_head_sha / mem_config_for direct-engine
4267        // accessors).
4268        let tmp = TempDir::new().unwrap();
4269        let mem_dir = tmp.path().to_path_buf();
4270        let writer = FilesystemMemWriter::new(mem_dir.clone());
4271        let archive_path = build_archive(tmp.path(), "ext", &[("a.md", b"a")]);
4272
4273        let engine = Engine::from_mounts(vec![
4274            (
4275                folder_mount("writable", mem_dir.clone()),
4276                Box::new(writer) as Box<dyn MemBackend>,
4277            ),
4278            (
4279                archive_mount("sealed", archive_path.clone()),
4280                Box::new(ArchiveBackend::new(archive_path.clone())),
4281            ),
4282        ])
4283        .unwrap();
4284
4285        // Known mems: each returns a Mount whose storage variant
4286        // matches what the caller passed at construction.
4287        let folder = engine.mount("writable").expect("known mem");
4288        assert!(matches!(folder.storage, MountStorage::Folder { .. }));
4289        assert_eq!(folder.capability, MountCapability::Write);
4290
4291        let archive = engine.mount("sealed").expect("known mem");
4292        match &archive.storage {
4293            MountStorage::Archive { path } => assert_eq!(path, &archive_path),
4294            other => panic!("expected Archive storage, got {other:?}"),
4295        }
4296        assert_eq!(archive.capability, MountCapability::ReadOnly);
4297
4298        // Unknown mem — None, no panic, no error.
4299        assert!(engine.mount("missing").is_none());
4300
4301        // Engine::mounts enumerates every mount in declaration order.
4302        let mounts = engine.mounts();
4303        assert_eq!(mounts.len(), 2);
4304        assert_eq!(mounts[0].mem, "writable");
4305        assert_eq!(mounts[1].mem, "sealed");
4306    }
4307
4308    #[test]
4309    fn mem_router_writable_set_matches_writable_mount_capability() {
4310        // Build an engine with one writable folder mount and one
4311        // read-only archive mount; the router's writable set must
4312        // equal the writable mount's name only.
4313        let tmp = TempDir::new().unwrap();
4314        let mem_dir = tmp.path().join("specs");
4315        std::fs::create_dir_all(&mem_dir).unwrap();
4316        let writer = FilesystemMemWriter::new(mem_dir.clone());
4317        let archive_path = build_archive(tmp.path(), "ext", &[("a.md", b"a")]);
4318
4319        let engine = Engine::from_mounts(vec![
4320            (
4321                folder_mount("specs", mem_dir.clone()),
4322                Box::new(writer) as Box<dyn MemBackend>,
4323            ),
4324            (
4325                archive_mount("ext", archive_path.clone()),
4326                Box::new(ArchiveBackend::new(archive_path)),
4327            ),
4328        ])
4329        .unwrap();
4330
4331        let router = engine.mem_router();
4332        assert!(router.is_writable("specs"));
4333        assert!(!router.is_writable("ext"));
4334        assert!(router.is_visible("specs"));
4335        assert!(router.is_visible("ext"));
4336        let writable: std::collections::HashSet<&String> = router.writable_mems().iter().collect();
4337        assert_eq!(writable.len(), 1);
4338        assert!(writable.contains(&"specs".to_string()));
4339    }
4340
4341    #[test]
4342    fn mem_router_origin_is_explicit_toml_for_workspace_mounts() {
4343        // Every mount built via `from_mounts` lands as
4344        // `MemOrigin::ExplicitToml` — the file-adapter origin.
4345        // `RuntimeCreated` is reserved for `memstead_mem_create`
4346        // runtime registrations once that handler migrates onto
4347        // the unified engine.
4348        let tmp = TempDir::new().unwrap();
4349        let mem_dir = tmp.path().join("specs");
4350        std::fs::create_dir_all(&mem_dir).unwrap();
4351        let writer = FilesystemMemWriter::new(mem_dir.clone());
4352
4353        let engine = Engine::from_mounts(vec![(
4354            folder_mount("specs", mem_dir),
4355            Box::new(writer) as Box<dyn MemBackend>,
4356        )])
4357        .unwrap();
4358
4359        let origin = engine
4360            .mem_router()
4361            .origin_for_mem("specs")
4362            .expect("known mem");
4363        assert_eq!(origin.kind(), "explicit");
4364    }
4365
4366    #[test]
4367    fn mem_router_dir_for_writable_folder_mount_matches_storage_path() {
4368        // Folder-backed writable mounts surface the storage path
4369        // via `dir_for_mem`. Handlers consuming the router for
4370        // per-mem path resolution rely on this.
4371        let tmp = TempDir::new().unwrap();
4372        let mem_dir = tmp.path().join("specs");
4373        std::fs::create_dir_all(&mem_dir).unwrap();
4374        let writer = FilesystemMemWriter::new(mem_dir.clone());
4375
4376        let engine = Engine::from_mounts(vec![(
4377            folder_mount("specs", mem_dir.clone()),
4378            Box::new(writer) as Box<dyn MemBackend>,
4379        )])
4380        .unwrap();
4381
4382        assert_eq!(
4383            engine.mem_router().dir_for_mem("specs"),
4384            Some(mem_dir.as_path()),
4385        );
4386        assert_eq!(engine.mem_router().dir_for_mem("unknown"), None);
4387    }
4388
4389    #[test]
4390    fn mem_router_archive_path_for_read_only_archive_mount() {
4391        // Read-only archive mounts register via `add_read_only` so
4392        // `archive_path_for_mem` resolves the archive's on-disk
4393        // location.
4394        let tmp = TempDir::new().unwrap();
4395        let mem_dir = tmp.path().join("specs");
4396        std::fs::create_dir_all(&mem_dir).unwrap();
4397        let writer = FilesystemMemWriter::new(mem_dir.clone());
4398        let archive_path = build_archive(tmp.path(), "ext", &[("a.md", b"a")]);
4399
4400        let engine = Engine::from_mounts(vec![
4401            (
4402                folder_mount("specs", mem_dir),
4403                Box::new(writer) as Box<dyn MemBackend>,
4404            ),
4405            (
4406                archive_mount("ext", archive_path.clone()),
4407                Box::new(ArchiveBackend::new(archive_path.clone())),
4408            ),
4409        ])
4410        .unwrap();
4411
4412        let router = engine.mem_router();
4413        assert_eq!(
4414            router.archive_path_for_mem("ext"),
4415            Some(archive_path.as_path()),
4416        );
4417        // Writable folder mount has no archive path.
4418        assert_eq!(router.archive_path_for_mem("specs"), None);
4419    }
4420
4421    #[test]
4422    fn read_mem_config_via_backend_trait_folder_reads_bytes() {
4423        // Direct trait call against FilesystemMemWriter. Verifies
4424        // the backend-side primitive returns the raw bytes the
4425        // engine then parses.
4426        let tmp = TempDir::new().unwrap();
4427        let mem_dir = tmp.path().to_path_buf();
4428        std::fs::create_dir_all(mem_dir.join(".memstead")).unwrap();
4429        let body = br#"{
4430            "format": 1,
4431            "schema": "default@1.0.0",
4432            "writeGuidance": { "tone": "neutral" }
4433        }"#;
4434        std::fs::write(mem_dir.join(".memstead").join("config.json"), body).unwrap();
4435
4436        let writer = FilesystemMemWriter::new(mem_dir);
4437        let result = MemBackend::read_mem_config(&writer).unwrap();
4438        let bytes = result.expect("config bytes must surface");
4439        let parsed: serde_json::Value = serde_json::from_slice(&bytes).unwrap();
4440        assert_eq!(parsed["schema"], "default@1.0.0");
4441    }
4442
4443    #[test]
4444    fn read_mem_config_via_backend_trait_folder_missing_returns_none() {
4445        let tmp = TempDir::new().unwrap();
4446        let mem_dir = tmp.path().to_path_buf();
4447        let writer = FilesystemMemWriter::new(mem_dir);
4448        let result = MemBackend::read_mem_config(&writer).unwrap();
4449        assert!(result.is_none());
4450    }
4451
4452    #[test]
4453    fn read_mem_config_via_backend_trait_archive_reads_bytes() {
4454        // Build an archive containing .memstead/config.json and verify
4455        // the ArchiveBackend impl returns its bytes.
4456        let tmp = TempDir::new().unwrap();
4457        let archive_path = tmp.path().join("seed.mem");
4458        let body = br#"{
4459            "format": 1,
4460            "schema": "default@1.0.0",
4461            "writeGuidance": { "tone": "archive" }
4462        }"#;
4463        {
4464            let file = std::fs::File::create(&archive_path).unwrap();
4465            let mut writer = zip::ZipWriter::new(file);
4466            writer
4467                .start_file(
4468                    ".memstead/config.json",
4469                    zip::write::SimpleFileOptions::default(),
4470                )
4471                .unwrap();
4472            use std::io::Write;
4473            writer.write_all(body).unwrap();
4474            writer.finish().unwrap();
4475        }
4476
4477        let backend = ArchiveBackend::new(archive_path);
4478        let result = MemBackend::read_mem_config(&backend).unwrap();
4479        let bytes = result.expect("config bytes must surface");
4480        let parsed: serde_json::Value = serde_json::from_slice(&bytes).unwrap();
4481        assert_eq!(parsed["writeGuidance"]["tone"], "archive");
4482    }
4483
4484    #[test]
4485    fn mem_config_for_returns_none_when_no_config_file_present() {
4486        // Folder backend without a `.memstead/config.json` file. The
4487        // accessor must lenient — return None, not error.
4488        let tmp = TempDir::new().unwrap();
4489        let mem_dir = tmp.path().to_path_buf();
4490        let writer = FilesystemMemWriter::new(mem_dir.clone());
4491        let engine = Engine::from_mounts(vec![(
4492            folder_mount("specs", mem_dir),
4493            Box::new(writer) as Box<dyn MemBackend>,
4494        )])
4495        .unwrap();
4496        assert!(engine.mem_config_for("specs").is_none());
4497    }
4498
4499    #[test]
4500    fn mem_config_for_returns_some_when_config_file_present() {
4501        // Drop a valid `.memstead/config.json` into the mem dir,
4502        // build the engine, and assert the accessor surfaces a
4503        // MemConfig with the right shape (write_guidance entries
4504        // round-trip).
4505        let tmp = TempDir::new().unwrap();
4506        let mem_dir = tmp.path().to_path_buf();
4507        std::fs::create_dir_all(mem_dir.join(".memstead")).unwrap();
4508        let config_body = r#"{
4509            "format": 1,
4510            "schema": "default@1.0.0",
4511            "writeGuidance": {
4512                "tone": "neutral",
4513                "voice": "active"
4514            }
4515        }"#;
4516        std::fs::write(mem_dir.join(".memstead").join("config.json"), config_body).unwrap();
4517
4518        let writer = FilesystemMemWriter::new(mem_dir.clone());
4519        let engine = Engine::from_mounts(vec![(
4520            folder_mount("specs", mem_dir),
4521            Box::new(writer) as Box<dyn MemBackend>,
4522        )])
4523        .unwrap();
4524
4525        let cfg = engine
4526            .mem_config_for("specs")
4527            .expect("mem_config should load");
4528        assert_eq!(cfg.write_guidance.len(), 2);
4529        assert_eq!(
4530            cfg.write_guidance.get("tone").and_then(|v| v.as_str()),
4531            Some("neutral"),
4532        );
4533        assert_eq!(
4534            cfg.write_guidance.get("voice").and_then(|v| v.as_str()),
4535            Some("active"),
4536        );
4537    }
4538
4539    #[test]
4540    fn mem_config_for_unknown_mem_returns_none() {
4541        // Lenient accessor — unknown names get None, not Err.
4542        let tmp = TempDir::new().unwrap();
4543        let mem_dir = tmp.path().to_path_buf();
4544        let writer = FilesystemMemWriter::new(mem_dir.clone());
4545        let engine = Engine::from_mounts(vec![(
4546            folder_mount("specs", mem_dir),
4547            Box::new(writer) as Box<dyn MemBackend>,
4548        )])
4549        .unwrap();
4550        assert!(engine.mem_config_for("missing").is_none());
4551    }
4552
4553    #[test]
4554    fn mem_config_for_archive_mount_returns_none() {
4555        // Archive backends carry mem_config = None in V1 (the
4556        // read-from-storage path is deferred to a follow-up).
4557        let tmp = TempDir::new().unwrap();
4558        let archive_path = build_archive(tmp.path(), "ext", &[("a.md", b"a")]);
4559        let engine = Engine::from_mounts(vec![(
4560            archive_mount("ext", archive_path.clone()),
4561            Box::new(ArchiveBackend::new(archive_path)) as Box<dyn MemBackend>,
4562        )])
4563        .unwrap();
4564        assert!(engine.mem_config_for("ext").is_none());
4565    }
4566
4567    #[test]
4568    fn mem_configs_named_iterates_only_mounts_with_config() {
4569        // Two folder mounts; one has a config file, one doesn't.
4570        // The iterator yields exactly the configured one — verifies
4571        // the filter_map shape and that the name comes from the
4572        // mount record (authoritative), not the config body.
4573        let tmp = TempDir::new().unwrap();
4574        let with_config = tmp.path().join("specs");
4575        let without_config = tmp.path().join("memos");
4576        std::fs::create_dir_all(with_config.join(".memstead")).unwrap();
4577        std::fs::create_dir_all(&without_config).unwrap();
4578        let config_body = r#"{
4579            "format": 1,
4580            "schema": "default@1.0.0",
4581            "writeGuidance": { "tone": "neutral" }
4582        }"#;
4583        std::fs::write(
4584            with_config.join(".memstead").join("config.json"),
4585            config_body,
4586        )
4587        .unwrap();
4588
4589        let engine = Engine::from_mounts(vec![
4590            (
4591                folder_mount("specs", with_config.clone()),
4592                Box::new(FilesystemMemWriter::new(with_config)) as Box<dyn MemBackend>,
4593            ),
4594            (
4595                folder_mount("memos", without_config.clone()),
4596                Box::new(FilesystemMemWriter::new(without_config)) as Box<dyn MemBackend>,
4597            ),
4598        ])
4599        .unwrap();
4600
4601        let yielded: Vec<(&str, usize)> = engine
4602            .mem_configs_named()
4603            .map(|(name, cfg)| (name, cfg.write_guidance.len()))
4604            .collect();
4605        assert_eq!(yielded, vec![("specs", 1)]);
4606    }
4607
4608    #[test]
4609    fn schema_for_returns_some_for_known_mem_and_none_for_unknown() {
4610        // Every mount registers a schema (resolved from its pin at
4611        // boot). Lookup by mem name surfaces the same Arc that
4612        // mutations resolve internally; unknown names return None.
4613        let tmp = TempDir::new().unwrap();
4614        let mem_dir = tmp.path().to_path_buf();
4615        let writer = FilesystemMemWriter::new(mem_dir.clone());
4616        let engine = Engine::from_mounts(vec![(
4617            folder_mount("specs", mem_dir),
4618            Box::new(writer) as Box<dyn MemBackend>,
4619        )])
4620        .unwrap();
4621        assert!(engine.schema_for("specs").is_some());
4622        assert!(engine.schema_for("missing").is_none());
4623    }
4624
4625    #[test]
4626    fn gitdir_for_unknown_mem_returns_unknown_mem() {
4627        let tmp = TempDir::new().unwrap();
4628        let mem_dir = tmp.path().to_path_buf();
4629        let writer = FilesystemMemWriter::new(mem_dir.clone());
4630        let engine = Engine::from_mounts(vec![(
4631            folder_mount("specs", mem_dir),
4632            Box::new(writer) as Box<dyn MemBackend>,
4633        )])
4634        .unwrap();
4635        let err = engine.gitdir_for("missing").unwrap_err();
4636        assert!(matches!(err, EngineError::UnknownMem(v) if v == "missing"));
4637    }
4638
4639    #[test]
4640    fn gitdir_for_folder_mount_returns_no_gitdir_error() {
4641        // Folder mounts do not have a gitdir — full's contract surfaces
4642        // a mem-level error, not UnknownMem. Mirror that here.
4643        let tmp = TempDir::new().unwrap();
4644        let mem_dir = tmp.path().to_path_buf();
4645        let writer = FilesystemMemWriter::new(mem_dir.clone());
4646        let engine = Engine::from_mounts(vec![(
4647            folder_mount("specs", mem_dir),
4648            Box::new(writer) as Box<dyn MemBackend>,
4649        )])
4650        .unwrap();
4651        let err = engine.gitdir_for("specs").unwrap_err();
4652        match err {
4653            EngineError::Mem(msg) => assert!(msg.contains("no resolved gitdir")),
4654            other => panic!("expected EngineError::Mem, got {other:?}"),
4655        }
4656    }
4657
4658    #[test]
4659    fn worktree_for_folder_mount_returns_storage_path() {
4660        let tmp = TempDir::new().unwrap();
4661        let mem_dir = tmp.path().to_path_buf();
4662        let writer = FilesystemMemWriter::new(mem_dir.clone());
4663        let engine = Engine::from_mounts(vec![(
4664            folder_mount("specs", mem_dir.clone()),
4665            Box::new(writer) as Box<dyn MemBackend>,
4666        )])
4667        .unwrap();
4668        let worktree = engine.worktree_for("specs").unwrap();
4669        assert_eq!(worktree, mem_dir);
4670    }
4671
4672    #[test]
4673    fn worktree_for_unknown_mem_returns_unknown_mem() {
4674        let tmp = TempDir::new().unwrap();
4675        let mem_dir = tmp.path().to_path_buf();
4676        let writer = FilesystemMemWriter::new(mem_dir.clone());
4677        let engine = Engine::from_mounts(vec![(
4678            folder_mount("specs", mem_dir),
4679            Box::new(writer) as Box<dyn MemBackend>,
4680        )])
4681        .unwrap();
4682        let err = engine.worktree_for("missing").unwrap_err();
4683        assert!(matches!(err, EngineError::UnknownMem(v) if v == "missing"));
4684    }
4685
4686    #[test]
4687    fn worktree_for_archive_mount_returns_archive_backed_error() {
4688        let tmp = TempDir::new().unwrap();
4689        let archive_path = build_archive(tmp.path(), "ext", &[("a.md", b"a")]);
4690        let engine = Engine::from_mounts(vec![(
4691            archive_mount("ext", archive_path.clone()),
4692            Box::new(ArchiveBackend::new(archive_path)) as Box<dyn MemBackend>,
4693        )])
4694        .unwrap();
4695        let err = engine.worktree_for("ext").unwrap_err();
4696        match err {
4697            EngineError::Mem(msg) => assert!(msg.contains("archive-backed")),
4698            other => panic!("expected EngineError::Mem, got {other:?}"),
4699        }
4700    }
4701
4702    #[test]
4703    fn mem_head_sha_for_folder_mount_is_none() {
4704        // Folder backend doesn't track a head; current_head() returns
4705        // Ok(None) at construction; mem_head_sha returns Ok(None).
4706        let tmp = TempDir::new().unwrap();
4707        let mem_dir = tmp.path().to_path_buf();
4708        let writer = FilesystemMemWriter::new(mem_dir.clone());
4709        let engine = Engine::from_mounts(vec![(
4710            folder_mount("specs", mem_dir),
4711            Box::new(writer) as Box<dyn MemBackend>,
4712        )])
4713        .unwrap();
4714        let head = engine.mem_head_sha("specs").unwrap();
4715        assert_eq!(head, None);
4716    }
4717
4718    #[test]
4719    fn mem_head_sha_unknown_mem_returns_unknown_mem() {
4720        let tmp = TempDir::new().unwrap();
4721        let mem_dir = tmp.path().to_path_buf();
4722        let writer = FilesystemMemWriter::new(mem_dir.clone());
4723        let engine = Engine::from_mounts(vec![(
4724            folder_mount("specs", mem_dir),
4725            Box::new(writer) as Box<dyn MemBackend>,
4726        )])
4727        .unwrap();
4728        let err = engine.mem_head_sha("missing").unwrap_err();
4729        assert!(matches!(err, EngineError::UnknownMem(v) if v == "missing"));
4730    }
4731
4732    #[test]
4733    fn capability_surfaces_per_mount() {
4734        let tmp = TempDir::new().unwrap();
4735        let mem_dir = tmp.path().to_path_buf();
4736        let writer = FilesystemMemWriter::new(mem_dir.clone());
4737        let archive_path = build_archive(tmp.path(), "ext", &[("a.md", b"a")]);
4738
4739        let engine = Engine::from_mounts(vec![
4740            (
4741                folder_mount("writable", mem_dir),
4742                Box::new(writer) as Box<dyn MemBackend>,
4743            ),
4744            (
4745                archive_mount("read-only", archive_path.clone()),
4746                Box::new(ArchiveBackend::new(archive_path)),
4747            ),
4748        ])
4749        .unwrap();
4750
4751        assert_eq!(
4752            engine.capability("writable").unwrap(),
4753            MountCapability::Write
4754        );
4755        assert_eq!(
4756            engine.capability("read-only").unwrap(),
4757            MountCapability::ReadOnly
4758        );
4759        assert!(matches!(
4760            engine.capability("missing"),
4761            Err(EngineError::UnknownMem(_))
4762        ));
4763    }
4764
4765    #[test]
4766    fn read_provenance_routes_through_backend() {
4767        let tmp = TempDir::new().unwrap();
4768        let mem_dir = tmp.path().to_path_buf();
4769        let writer = FilesystemMemWriter::new(mem_dir.clone());
4770
4771        // Append a provenance record via the backend trait directly,
4772        // then read it back through the engine.
4773        let backend_handle: &dyn MemBackend = &writer;
4774        backend_handle
4775            .append_provenance(&Provenance::new(
4776                std::time::UNIX_EPOCH + std::time::Duration::from_secs(1_700_000_000),
4777                crate::ProvenanceKind::Create,
4778                Some("v:e".into()),
4779                crate::vcs::Actor::Cli,
4780                None,
4781                Some("first".into()),
4782            ))
4783            .unwrap();
4784
4785        let engine = Engine::from_mounts(vec![(
4786            folder_mount("specs", mem_dir),
4787            Box::new(writer) as Box<dyn MemBackend>,
4788        )])
4789        .unwrap();
4790
4791        let records = engine.read_provenance("specs", None).unwrap();
4792        assert_eq!(records.len(), 1);
4793        assert_eq!(records[0].kind, crate::ProvenanceKind::Create);
4794        assert_eq!(records[0].entity.as_deref(), Some("v:e"));
4795        assert_eq!(records[0].note.as_deref(), Some("first"));
4796    }
4797
4798    #[test]
4799    fn archive_mount_returns_sealed_indirectly_through_backend_layer() {
4800        // The engine doesn't yet expose mutation methods, but an
4801        // archive backend held on a Mount with ReadOnly capability is
4802        // still a `&dyn MemBackend` whose write methods return
4803        // Sealed. This test locks the trait routing — when the engine
4804        // gains write methods in a later session, capability gating +
4805        // backend Sealed errors must agree.
4806        let tmp = TempDir::new().unwrap();
4807        let archive_path = build_archive(tmp.path(), "ext", &[("a.md", b"a")]);
4808        let backend = ArchiveBackend::new(archive_path);
4809        match MemBackend::write_entity(&backend, Path::new("x.md"), b"x") {
4810            Err(BackendError::Sealed) => {}
4811            other => panic!("expected Sealed, got {other:?}"),
4812        }
4813    }
4814
4815    // ---- Read-side delegates ----------------------------------------
4816    //
4817    // These tests pin the surface that the MCP migration consumes
4818    // (stats, health, context, communities, search, list, orphans,
4819    // stubs, most_connected, missing_required_outgoing). They run
4820    // against a folder-mount engine with a small fixture of created
4821    // entities and one relate edge — enough to exercise both the
4822    // graph-query path and the cache-invalidation hooks.
4823
4824    /// Generation-keyed memos (flywheel W8/01). Four claims: repeated
4825    /// reads serve the memo; a REFUSED engine batch leaves the memo
4826    /// untouched (the refusal path calls no hook — pinned so it stays
4827    /// cheap); a memo computed from an INTERIM (mid-batch) state never
4828    /// survives a rollback as fresh — the store-carried generation is
4829    /// what lets the invalidation hook adjudicate that correctly; and
4830    /// a real mutation invalidates, with the recomputation identical
4831    /// to a from-scratch detection (the criterion-3 identity oracle
4832    /// for the mechanism).
4833    #[test]
4834    fn generation_keyed_memos_survive_rollback_and_track_mutations() {
4835        use indexmap::IndexMap;
4836
4837        let tmp = TempDir::new().unwrap();
4838        let mut engine = build_demo_engine(&tmp);
4839
4840        // 1. Repeated reads: cell filled once, generation stable.
4841        let _ = engine.communities();
4842        let memo_gen_before = engine.community_memo.get().expect("memo filled").0;
4843        let _ = engine.communities();
4844        assert_eq!(
4845            engine.community_memo.get().expect("still filled").0,
4846            memo_gen_before,
4847            "repeated reads must serve the memo"
4848        );
4849
4850        // 2. A REFUSED engine batch rolls the store back and leaves
4851        // the memo untouched — refusal stays recompute-free.
4852        let bare = |id: crate::EntityId| crate::engine::UpdateEntityArgs {
4853            anchors: Vec::new(),
4854            anchors_unset: Vec::new(),
4855            id,
4856            expected_hash: None,
4857            sections: IndexMap::new(),
4858            append_sections: IndexMap::new(),
4859            patch_sections: IndexMap::new(),
4860            sections_unset: Vec::new(),
4861            metadata: IndexMap::new(),
4862            metadata_unset: Vec::new(),
4863            declare_relations: Vec::new(),
4864            dry_run: false,
4865            relations_unset: Vec::new(),
4866        };
4867        let mut real = bare(crate::EntityId::new("specs", "source-one"));
4868        real.append_sections
4869            .insert("identity".to_string(), "appended line".to_string());
4870        let missing = bare(crate::EntityId::new("specs", "does-not-exist"));
4871        let (actor, client) = cli_actor();
4872        let result = engine
4873            .batch_update(
4874                vec![(real, None), (missing, None)],
4875                actor,
4876                Some(&client),
4877                false,
4878            )
4879            .expect("refused batch returns a report-all envelope");
4880        assert!(
4881            !result.applied,
4882            "the missing target refuses the whole batch"
4883        );
4884        assert_eq!(
4885            engine.community_memo.get().map(|m| m.0),
4886            Some(memo_gen_before),
4887            "a refused batch must leave the pre-batch memo standing"
4888        );
4889        assert_eq!(
4890            engine.store().generation(),
4891            memo_gen_before.store_generation,
4892            "rollback restored the store to the memo's generation"
4893        );
4894
4895        // 3. The dangerous direction: a memo computed from an INTERIM
4896        // state (simulated batch staging) must not survive the
4897        // rollback as fresh. The store-carried generation is what the
4898        // invalidation hook adjudicates with.
4899        let snapshot = engine.store.clone();
4900        let interim_id = crate::EntityId::new("specs", "interim-only");
4901        let mut interim = engine
4902            .store
4903            .get(&crate::EntityId::new("specs", "source-one"))
4904            .expect("demo entity present")
4905            .clone();
4906        interim.id = interim_id.clone();
4907        interim.title = "Interim Only".to_string();
4908        engine.store.upsert(interim_id, interim);
4909        engine.invalidate_communities();
4910        let _ = engine.communities();
4911        let interim_gen = engine.community_memo.get().expect("interim memo").0;
4912        assert_ne!(interim_gen, memo_gen_before);
4913        engine.store = snapshot; // rollback, generation restored with it
4914        engine.invalidate_communities();
4915        engine.invalidate_search_indexes();
4916        if let Some((g, _)) = engine.community_memo.get() {
4917            assert_ne!(
4918                *g, interim_gen,
4919                "a rolled-back interim state must never be served as fresh"
4920            );
4921        }
4922        assert!(
4923            !engine
4924                .communities()
4925                .entity_cluster_map
4926                .keys()
4927                .any(|id| id.contains("interim-only")),
4928            "the partition served after rollback reflects the restored store, not the interim one"
4929        );
4930
4931        // 4. A real mutation invalidates; the recomputation equals a
4932        // from-scratch detection and sees the new entity.
4933        let (actor, client) = cli_actor();
4934        engine
4935            .create_entity(
4936                empty_create_args("specs", "Fourth Entity"),
4937                actor,
4938                Some(&client),
4939                None,
4940            )
4941            .unwrap();
4942        assert!(
4943            engine
4944                .communities()
4945                .entity_cluster_map
4946                .keys()
4947                .any(|id| id.contains("fourth-entity")),
4948            "recomputed partition sees the new entity"
4949        );
4950        let fresh = {
4951            let schema = engine
4952                .schemas
4953                .iter()
4954                .min_by(|a, b| a.0.cmp(b.0))
4955                .map(|(_, s)| s.clone())
4956                .expect("demo engine has a schema");
4957            let schema_for_weights = schema.clone();
4958            crate::graph::community::detect_communities(
4959                engine.store(),
4960                schema.manifest.community.resolution,
4961                schema.manifest.community.seed,
4962                move |rel_type| {
4963                    schema_for_weights
4964                        .manifest
4965                        .relationships
4966                        .definitions
4967                        .iter()
4968                        .find(|d| d.name == rel_type)
4969                        .map(|d| d.default_weight as f64)
4970                        .unwrap_or(1.0)
4971                },
4972            )
4973        };
4974        assert_eq!(
4975            engine.communities().entity_cluster_map,
4976            fresh.entity_cluster_map,
4977            "memo must equal a from-scratch detection over the current store"
4978        );
4979    }
4980
4981    fn build_demo_engine(tmp: &TempDir) -> Engine {
4982        let mem_dir = tmp.path().to_path_buf();
4983        let writer = FilesystemMemWriter::new(mem_dir.clone());
4984        let mut engine = Engine::from_mounts(vec![(
4985            folder_mount("specs", mem_dir),
4986            Box::new(writer) as Box<dyn MemBackend>,
4987        )])
4988        .unwrap();
4989        let (actor, client) = cli_actor();
4990        let source = engine
4991            .create_entity(
4992                empty_create_args("specs", "Source One"),
4993                actor,
4994                Some(&client),
4995                None,
4996            )
4997            .unwrap();
4998        let target = engine
4999            .create_entity(
5000                empty_create_args("specs", "Target Two"),
5001                actor,
5002                Some(&client),
5003                None,
5004            )
5005            .unwrap();
5006        engine
5007            .create_entity(
5008                empty_create_args("specs", "Lonely Three"),
5009                actor,
5010                Some(&client),
5011                None,
5012            )
5013            .unwrap();
5014        engine
5015            .relate_entity(
5016                RelateEntityArgs {
5017                    source: source.id.clone(),
5018                    expected_hash: Some(source.content_hash.clone()),
5019                    rel_type: "USES".to_string(),
5020                    target: target.id.clone(),
5021                    remove: false,
5022                    description: None,
5023                    dry_run: false,
5024                },
5025                actor,
5026                Some(&client),
5027                None,
5028            )
5029            .unwrap();
5030        engine
5031    }
5032
5033    #[test]
5034    fn status_reports_per_engine_counts() {
5035        let tmp = TempDir::new().unwrap();
5036        let engine = build_demo_engine(&tmp);
5037        let stats = engine.status();
5038        assert_eq!(stats.entity_count, 3);
5039        assert_eq!(stats.edge_count, 1);
5040        assert_eq!(stats.mem_count, 1);
5041        assert_eq!(stats.types_in_use, vec!["spec".to_string()]);
5042        assert_eq!(stats.edge_types.get("USES"), Some(&1));
5043    }
5044
5045    #[test]
5046    fn orphans_lists_unconnected_real_entities() {
5047        let tmp = TempDir::new().unwrap();
5048        let engine = build_demo_engine(&tmp);
5049        let orphans = engine.orphans();
5050        assert_eq!(orphans.len(), 1);
5051        assert_eq!(orphans[0].as_ref(), "specs--lonely-three");
5052    }
5053
5054    /// #49: the orphan/community headlines can be attributed per pinned
5055    /// schema. Single-mem here, so one bucket — but it proves the
5056    /// attribution keys by `schema_of(mem)` and that the per-schema
5057    /// counts sum to the raw total (which a health surface keeps verbatim).
5058    #[test]
5059    fn schema_breakdowns_attribute_to_mem_pin() {
5060        let tmp = TempDir::new().unwrap();
5061        let engine = build_demo_engine(&tmp);
5062
5063        let orphans = engine.orphans();
5064        let orphans_by_schema = engine.orphans_by_schema(&orphans);
5065        assert_eq!(
5066            orphans_by_schema.values().sum::<usize>(),
5067            orphans.len(),
5068            "per-schema orphan counts must sum to the raw total"
5069        );
5070        assert_eq!(orphans_by_schema.len(), 1, "one mem ⇒ one schema bucket");
5071        let (schema, count) = orphans_by_schema.iter().next().unwrap();
5072        assert!(!schema.is_empty(), "specs mem is pinned: {schema:?}");
5073        assert_eq!(*count, 1);
5074
5075        // communities_by_schema buckets the demo mem's clusters under the
5076        // same pin; with one schema, its values sum to the global count.
5077        let mems: Vec<String> = engine.mounts().iter().map(|m| m.mem.clone()).collect();
5078        let communities_by_schema = engine.communities_by_schema(&mems);
5079        assert_eq!(communities_by_schema.len(), 1);
5080        assert_eq!(
5081            communities_by_schema.values().sum::<usize>(),
5082            engine.communities().count,
5083        );
5084    }
5085
5086    #[test]
5087    fn stubs_lists_unresolved_link_targets() {
5088        let tmp = TempDir::new().unwrap();
5089        let mem_dir = tmp.path().to_path_buf();
5090        let writer = FilesystemMemWriter::new(mem_dir.clone());
5091        let mut engine = Engine::from_mounts(vec![(
5092            folder_mount("specs", mem_dir),
5093            Box::new(writer) as Box<dyn MemBackend>,
5094        )])
5095        .unwrap();
5096        let (actor, client) = cli_actor();
5097        let source = engine
5098            .create_entity(
5099                empty_create_args("specs", "Holder"),
5100                actor,
5101                Some(&client),
5102                None,
5103            )
5104            .unwrap();
5105        // Relate to a non-existent target — relate_entity creates a
5106        // stub for the target so the edge can land.
5107        engine
5108            .relate_entity(
5109                RelateEntityArgs {
5110                    source: source.id.clone(),
5111                    expected_hash: Some(source.content_hash.clone()),
5112                    rel_type: "USES".to_string(),
5113                    target: EntityId::new("specs", "ghost"),
5114                    remove: false,
5115                    description: None,
5116                    dry_run: false,
5117                },
5118                actor,
5119                Some(&client),
5120                None,
5121            )
5122            .unwrap();
5123        let stubs = engine.stubs();
5124        assert!(
5125            stubs.iter().any(|(id, _)| id.as_ref() == "specs--ghost"),
5126            "expected ghost stub: {stubs:?}"
5127        );
5128    }
5129
5130    #[test]
5131    fn most_connected_orders_by_degree() {
5132        let tmp = TempDir::new().unwrap();
5133        let engine = build_demo_engine(&tmp);
5134        let top = engine.most_connected(5);
5135        assert_eq!(top.len(), 3);
5136        // Source and Target each have one edge; Lonely has zero.
5137        let zero_degree: Vec<_> = top
5138            .iter()
5139            .filter(|c| c.total == 0)
5140            .map(|c| c.id.as_ref().to_string())
5141            .collect();
5142        assert_eq!(zero_degree, vec!["specs--lonely-three".to_string()]);
5143    }
5144
5145    #[test]
5146    fn health_returns_per_engine_summary() {
5147        let tmp = TempDir::new().unwrap();
5148        let engine = build_demo_engine(&tmp);
5149        let health = engine.health();
5150        // `memstead_create` refuses on missing required sections, so
5151        // entities built through `empty_create_args` carry the
5152        // helper-seeded `identity` + `purpose` bodies and no longer
5153        // surface as missing-fields. Health remains the read-side
5154        // tolerance surface for legacy on-disk drift — covered by
5155        // the loader-tolerance tests that hand-craft pre-strict
5156        // markdown files.
5157        assert!(
5158            health
5159                .missing_fields
5160                .iter()
5161                .all(|r| r.id.as_ref() != "specs--source-one"),
5162            "post-strict-create fixture must not surface as missing-fields; got {:?}",
5163            health.missing_fields,
5164        );
5165    }
5166
5167    #[test]
5168    fn context_carries_neighbors_and_community() {
5169        let tmp = TempDir::new().unwrap();
5170        let engine = build_demo_engine(&tmp);
5171        let source_id = EntityId::new("specs", "source-one");
5172        let ctx = engine.context(&source_id).unwrap();
5173        assert_eq!(ctx.entity_id, source_id);
5174        assert_eq!(ctx.neighbors.len(), 1);
5175        assert_eq!(ctx.neighbors[0].relationship, "USES");
5176        assert!(matches!(ctx.neighbors[0].direction, Direction::Outgoing));
5177    }
5178
5179    #[test]
5180    fn communities_caches_louvain_until_invalidated() {
5181        let tmp = TempDir::new().unwrap();
5182        let mut engine = build_demo_engine(&tmp);
5183        // Population reflects the current store at first call.
5184        let entities_before = engine.communities().entity_cluster_map.len();
5185        // Cache hit — repeat call returns same data.
5186        assert_eq!(
5187            engine.communities().entity_cluster_map.len(),
5188            entities_before
5189        );
5190        // Mutation invalidates the cache; next call re-runs against
5191        // the post-mutation store and includes the new entity.
5192        let (actor, client) = cli_actor();
5193        engine
5194            .create_entity(
5195                empty_create_args("specs", "Disturber"),
5196                actor,
5197                Some(&client),
5198                None,
5199            )
5200            .unwrap();
5201        let entities_after = engine.communities().entity_cluster_map.len();
5202        assert_eq!(
5203            entities_after,
5204            entities_before + 1,
5205            "create_entity should have invalidated community cache and added the new entity"
5206        );
5207    }
5208
5209    #[test]
5210    fn list_filters_by_metadata_only() {
5211        let tmp = TempDir::new().unwrap();
5212        let engine = build_demo_engine(&tmp);
5213        let scope = SearchScope {
5214            entity_type: Some("spec".to_string()),
5215            ..Default::default()
5216        };
5217        let result = engine.list(&scope);
5218        // Three real spec entities created; stubs / non-spec types absent.
5219        assert_eq!(result.hits.len(), 3);
5220    }
5221
5222    #[test]
5223    fn list_applies_schema_declared_filter_on_non_default_schema_mem() {
5224        // A mem pinned to `planning` (non-default schema). The
5225        // `decision` type declares `status` with `filterable: equality`.
5226        // Pre-fix, filter dispatch consulted only the built-in default
5227        // schema via `type_by_name`, missed `status`, silently bypassed
5228        // the filter, and emitted the misleading "unknown filter key"
5229        // warning. Post-fix, the filter is honored and no warning fires.
5230        let tmp = TempDir::new().unwrap();
5231        let mem_dir = tmp.path().to_path_buf();
5232        let writer = FilesystemMemWriter::new(mem_dir.clone());
5233        let mount = Mount {
5234            mem: "planning".to_string(),
5235            schema: Some(memstead_schema::SchemaRef::new(
5236                "planning",
5237                semver::Version::new(0, 1, 0),
5238            )),
5239            storage: MountStorage::Folder { path: mem_dir },
5240            capability: MountCapability::Write,
5241            lifecycle: MountLifecycle::Eager,
5242            cross_linkable: true,
5243            migration_target: None,
5244        };
5245        let mut engine =
5246            Engine::from_mounts(vec![(mount, Box::new(writer) as Box<dyn MemBackend>)]).unwrap();
5247        let (actor, client) = cli_actor();
5248
5249        // Two decisions with different status values; required fields
5250        // (decision/context/consequences sections, decided_on, deciders)
5251        // get placeholder defaults — the test only cares about the
5252        // status field's filterability.
5253        for (title, status) in &[("Skip Postgres", "accepted"), ("Use SQLite", "proposed")] {
5254            let mut metadata = indexmap::IndexMap::new();
5255            metadata.insert("status".to_string(), status.to_string());
5256            metadata.insert("deciders".to_string(), "alice".to_string());
5257            metadata.insert("decided_on".to_string(), "2026-05-19".to_string());
5258            let args = crate::engine::CreateEntityArgs {
5259                anchors: Vec::new(),
5260                mem: "planning".to_string(),
5261                title: title.to_string(),
5262                entity_type: "decision".to_string(),
5263                sections: indexmap::IndexMap::from_iter([
5264                    ("decision".to_string(), "We chose this.".to_string()),
5265                    ("context".to_string(), "Single-user dev.".to_string()),
5266                    ("consequences".to_string(), "Lose multi-writer.".to_string()),
5267                ]),
5268                metadata,
5269                relations: Vec::new(),
5270                dry_run: false,
5271            };
5272            engine
5273                .create_entity(args, actor, Some(&client), None)
5274                .unwrap();
5275        }
5276
5277        // Filter on the schema-declared filterable field.
5278        let scope = SearchScope {
5279            entity_type: Some("decision".to_string()),
5280            filters: std::collections::HashMap::from([(
5281                "status".to_string(),
5282                "accepted".to_string(),
5283            )]),
5284            ..Default::default()
5285        };
5286        let result = engine.list(&scope);
5287        assert_eq!(
5288            result.hits.len(),
5289            1,
5290            "filter on schema-declared field must select only matching entities"
5291        );
5292        assert_eq!(result.hits[0].title, "Skip Postgres");
5293        assert!(
5294            result.warnings.is_empty(),
5295            "no warning should fire when the filter is declared by the mem's pinned schema: {:?}",
5296            result.warnings
5297        );
5298    }
5299
5300    #[test]
5301    fn search_returns_results_against_built_index() {
5302        let tmp = TempDir::new().unwrap();
5303        let engine = build_demo_engine(&tmp);
5304        let scope = SearchScope {
5305            query: Some(crate::ops::Query {
5306                any: vec!["source".to_string()],
5307                ..Default::default()
5308            }),
5309            ..Default::default()
5310        };
5311        let result = engine.search(&scope).expect("native search returns Ok");
5312        assert!(result.total >= 1, "expected ≥1 hit for source: {result:?}");
5313        assert!(
5314            result
5315                .hits
5316                .iter()
5317                .any(|h| h.id.as_ref() == "specs--source-one"),
5318            "expected source-one in hits: {result:?}"
5319        );
5320    }
5321
5322    // ---- Engine::from_workspace_root (lean boot path) --------------
5323}