Skip to main content

memstead_base/engine/
lifecycle.rs

1//! Engine lifecycle — settings/workspace-root setters, runtime
2//! mem add/remove, reload, and export.
3//!
4//! `register_writable_mem` / `unregister_writable_mem` are the
5//! engine-level primitives the `memstead_mem_create` / `memstead_mem_delete`
6//! handlers build on. `reload_one_mem*` re-reads a mount's backend
7//! and refreshes the in-memory store; `reload_each_writable_mem*`
8//! sweeps every writable mount. `export_markdown` regenerates entity
9//! markdown for folder mounts; `export_mem` produces a portable
10//! `.mem` archive via the backend-aware dispatch in
11//! [`crate::ops::export`].
12
13use std::cell::OnceCell;
14use std::collections::HashMap;
15use std::path::PathBuf;
16use std::sync::Arc;
17
18use crate::backend::{BackendError, MemBackend};
19use crate::engine_fallback_type;
20use crate::entity::EntityId;
21use crate::entity::generator::generate_markdown;
22use crate::entity::loader::parse_entries;
23use crate::entity::store_builder::push_entities_into_store;
24use crate::mem::MemOrigin;
25use crate::ops::WarningHint;
26use crate::workspace::{Mount, MountStorage, WorkspaceSettings};
27
28use super::boot::collect_source_entries;
29use super::{BackendFactory, Engine, EngineError, GitBranchOps, MountedBackend};
30
31/// What [`Engine::stage_sealed_schema`] did.
32#[derive(Debug, Clone, Copy, PartialEq, Eq)]
33pub enum SchemaStaging {
34    /// The pin already resolves in this workspace — nothing written.
35    /// Re-installing a mem, and installing a second mem that pins the
36    /// same schema, both land here.
37    AlreadyResolvable,
38    /// The package was written into the workspace's local schema
39    /// storage and is resolvable in this process from now on.
40    Staged,
41    /// The archive carries no embedded schema tree. Nothing to stage;
42    /// the pin must resolve on its own or the mount refuses.
43    NoEmbeddedSchema,
44}
45
46impl Engine {
47    /// Replace the workspace-level settings. Called by
48    /// [`Self::from_workspace_root`] (and the full counterpart) after
49    /// reading `.memstead/workspace.toml`. Tests / direct callers leave
50    /// the default empty value in place. Cheap clone — settings
51    /// carry only data shapes (raw rule lists, link policy map),
52    /// no compiled matchers. Invalidates the lazy
53    /// `create_rule_set_memo` so the next synthesis call rebuilds
54    /// from the new policy.
55    pub fn set_settings(&mut self, settings: WorkspaceSettings) {
56        self.settings = settings;
57        self.create_rule_set_memo = OnceCell::new();
58    }
59
60    /// Replace the backend factory. Full consumers call this once at boot
61    /// (`engine_from_workspace_root`) to install
62    /// `memstead_git_branch::storage::instantiate_full_backend` so the engine
63    /// can materialise git-branch backends on top of folder + archive.
64    /// Lean consumers leave the default in place.
65    pub fn set_backend_factory(&mut self, factory: BackendFactory) {
66        self.backend_factory = factory;
67    }
68
69    /// Insert into the engine's schema map, bumping the schemas epoch
70    /// (the second half of the derived-memo key — flywheel W8/01):
71    /// derived structures depend on schemas, so any change here must
72    /// be visible to the memo-invalidation hooks even when the store
73    /// generation did not move.
74    pub(crate) fn schemas_insert(
75        &mut self,
76        mem: String,
77        schema: std::sync::Arc<memstead_schema::Schema>,
78    ) {
79        self.schemas_epoch += 1;
80        self.schemas.insert(mem, schema);
81    }
82
83    /// Remove from the engine's schema map, bumping the schemas epoch
84    /// — see [`Self::schemas_insert`].
85    pub(crate) fn schemas_remove(&mut self, mem: &str) {
86        self.schemas_epoch += 1;
87        self.schemas.remove(mem);
88    }
89
90    /// Install the unmounted-mem storage discovery hook (flywheel
91    /// W7/02). Full boot sets it; without one, writes referencing
92    /// unmounted mems keep the forward-reference mechanic unchanged.
93    pub fn set_unmounted_storage_prober(&mut self, prober: super::UnmountedStorageProber) {
94        self.unmounted_storage_prober = Some(prober);
95    }
96
97    /// Replace the mutation-timestamp clock — the source every
98    /// engine-stamped metadata field (`init_timestamp` /
99    /// `auto_timestamp` schema flags: `created_date`, `last_modified`)
100    /// reads. A testing affordance for suites that assert over
101    /// canonical entity bytes (e.g. cross-surface hash parity):
102    /// pin both engines to the same constant and byte-level
103    /// nondeterminism from wall-clock seconds disappears. Production
104    /// code never calls this — the default installed at construction
105    /// is the system clock, and the stamped format is unchanged
106    /// either way.
107    pub fn set_mutation_clock(&mut self, clock: crate::engine::MutationClock) {
108        self.mutation_clock = clock;
109    }
110
111    /// Set the caller-declared role for subsequent mutations
112    /// (agent-trust plan 13). The surface calls this before every
113    /// mutation with the per-call parameter resolved against its
114    /// session default (per-call wins); `Role::Unspecified` records
115    /// as absence.
116    pub fn set_role(&mut self, role: crate::vcs::Role) {
117        self.current_role = role;
118    }
119
120    /// The currently declared role — what the next mutation records.
121    pub fn current_role(&self) -> crate::vcs::Role {
122        self.current_role
123    }
124
125    /// Set the caller-declared identity for subsequent mutations and
126    /// checks (agent-trust plan 15). The surface calls this before
127    /// every operation with the per-call parameter resolved against
128    /// its session default (per-call wins); `None` records as
129    /// absence. Callers pass an already-normalised value
130    /// ([`crate::vcs::normalise_identity`]).
131    pub fn set_identity(&mut self, identity: Option<String>) {
132        self.current_identity = identity;
133    }
134
135    /// The currently declared identity — what the next mutation or
136    /// check records.
137    pub fn current_identity(&self) -> Option<&str> {
138        self.current_identity.as_deref()
139    }
140
141    /// Current mutation timestamp as the second-granularity ISO form
142    /// the stamping paths write. Reads [`Self::mutation_clock`] — the
143    /// system clock unless a test pinned it.
144    pub(crate) fn now_iso(&self) -> String {
145        crate::engine::mutation::iso_from_system_time((self.mutation_clock)())
146    }
147
148    /// Install the git-branch ops bundle. Full boot
149    /// (`memstead_git_branch::engine_from_workspace_root`) calls this once
150    /// at construction. Lean consumers leave it unset and the
151    /// git-branch dispatch branches collapse to typed errors / empty
152    /// reports — lean has no git-branch mounts.
153    pub fn set_git_branch_ops(&mut self, ops: GitBranchOps) {
154        self.git_branch_ops = Some(ops);
155    }
156
157    /// Install a schema package onto the workspace's git-branch backend —
158    /// the unified `__MEMSTEAD:schemas/<name>@<version>/` ref. `files`
159    /// are `(relative-path, bytes)` pairs (`schema.yaml`,
160    /// `types/<t>.yaml`, optional `mem-template.json`). Returns the
161    /// resulting commit sha; idempotent at the storage layer (an
162    /// identical re-install produces no new commit).
163    ///
164    /// Folder workspaces install schemas by writing under
165    /// `<workspace>/.memstead/schemas/` directly; this is the git-branch
166    /// path, where the engine owns the mem-repo and the write must
167    /// route through it. Errors when no git-branch ops are wired (lean
168    /// flavour) or no git-branch mount exists to resolve the shared
169    /// mem-repo gitdir from. The caller reloads (or restarts) to pick
170    /// the new schema into the resolution catalogue.
171    pub fn install_schema(
172        &self,
173        name: &str,
174        version: &str,
175        files: &[(String, Vec<u8>)],
176    ) -> Result<String, EngineError> {
177        // Validation gate: the engine refuses to seal a package that the
178        // loader would reject or whose section headings cannot round-trip
179        // to their keys. Install time is the last moment the author can
180        // act — sealed schemas keep loading even when a later rule would
181        // refuse them, so nothing invalid may pass this point.
182        Self::validate_schema_package(name, version, files)?;
183        // Resolve the shared mem-repo gitdir: prefer a live git-branch
184        // mount's gitdir (authoritative — that is where the engine reads
185        // schemas from), falling back to the workspace's `mem-repo/.git`
186        // so a schema can be installed into an empty mem-repo *before*
187        // any mem pins it.
188        let gitdir = self
189            .mounts
190            .iter()
191            .find_map(|m| match &m.mount.storage {
192                crate::workspace::MountStorage::GitBranch { gitdir, .. } => Some(gitdir.clone()),
193                _ => None,
194            })
195            .or_else(|| {
196                self.workspace_root()
197                    .map(|r| r.join("mem-repo").join(".git"))
198            })
199            .ok_or_else(|| {
200                EngineError::Mem(
201                    "schema install requires a mem-repo workspace (no git-branch mount and \
202                     no workspace root to resolve the mem-repo gitdir)"
203                        .to_string(),
204                )
205            })?;
206        let ops = self.git_branch_ops.as_ref().ok_or_else(|| {
207            EngineError::Mem("git-branch ops are not wired on this engine".to_string())
208        })?;
209        // Seal the package AS-GIVEN: the format marker is a generation
210        // claim only the resolver can make (an authored directory source
211        // arrives already stamped; a legacy builtin or sealed source
212        // arrives unmarked, and absence IS its legacy claim). Stamping
213        // here mis-labelled legacy content as current and flipped every
214        // bare field's meaning in the sealed copy — the manufacturing
215        // defect backlog-sweep plan 05 removed.
216        (ops.write_schema)(&gitdir, name, version, files).map_err(EngineError::Backend)
217    }
218
219    /// Make a **sealed third-party** schema package resolvable in this
220    /// workspace — the install-time half of "a published mem installs
221    /// on the strength of the schema it carries".
222    ///
223    /// The package (package-relative files: `schema.yaml`,
224    /// `types/<t>.yaml`, `schema-format.json`) is written into the
225    /// workspace's local schema storage, the same storage
226    /// [`Self::install_schema`] writes to and the pin resolver reads —
227    /// so the mount that follows resolves without a fourth mechanism
228    /// and a second mem pinning the same schema finds it already
229    /// there. The loaded schema also lands in this engine's live
230    /// catalogue, so the caller mounts in the same process without a
231    /// reload.
232    ///
233    /// Two things it deliberately does NOT do. It does not run the
234    /// authoring gate ([`Self::validate_schema_package`]): these are a
235    /// third party's sealed bytes, the installing user cannot fix
236    /// them, and the archive validator has already admitted them under
237    /// the sealed reading — applying the authoring gate here would
238    /// make an archive that is valid to publish invalid to install.
239    /// And it does not append the format marker: presence of the
240    /// marker IS the package's metadata-polarity generation, so
241    /// injecting one would rewrite the meaning of bytes the publisher
242    /// sealed.
243    ///
244    /// Idempotent: a pin the engine can already resolve returns
245    /// [`SchemaStaging::AlreadyResolvable`] with nothing written.
246    pub fn stage_sealed_schema(
247        &mut self,
248        mem: &str,
249        pin: &memstead_schema::SchemaRef,
250        files: &[(String, Vec<u8>)],
251    ) -> Result<SchemaStaging, EngineError> {
252        let catalogue: Vec<std::sync::Arc<memstead_schema::Schema>> = self
253            .workspace_schemas
254            .iter()
255            .chain(self.builtin_schemas.iter())
256            .cloned()
257            .collect();
258        if crate::engine::SchemaResolver::new(&catalogue)
259            .resolve(pin)
260            .is_ok()
261        {
262            return Ok(SchemaStaging::AlreadyResolvable);
263        }
264        if files.is_empty() {
265            // Nothing embedded to stage. The caller's mount attempt
266            // reports the unresolved pin with its own source trail —
267            // that IS the actionable error here.
268            return Ok(SchemaStaging::NoEmbeddedSchema);
269        }
270
271        let unloadable = |reason: String| EngineError::EmbeddedSchemaInvalid {
272            mem: mem.to_string(),
273            pin: pin.as_display(),
274            reason,
275        };
276        // Read it exactly as the local schema source will read it back
277        // — one sealed reader, so admission and re-read cannot diverge.
278        let schema =
279            memstead_schema::load_sealed_package(files).map_err(|e| unloadable(e.to_string()))?;
280        let (name, version) = schema.id();
281        if name != pin.name || version != pin.version {
282            return Err(unloadable(format!(
283                "the package declares '{name}@{version}' but the mem pins {}",
284                pin.as_display()
285            )));
286        }
287
288        self.write_to_local_schema_source(&name, &version.to_string(), files)?;
289        self.workspace_schemas.push(std::sync::Arc::new(schema));
290        Ok(SchemaStaging::Staged)
291    }
292
293    /// Write a schema package into the workspace's local schema
294    /// storage — the git-branch `__MEMSTEAD:schemas/` ref when the
295    /// workspace is mem-repo-shaped. Shares
296    /// [`Self::install_schema`]'s gitdir resolution so authored and
297    /// staged packages land in one place.
298    fn write_to_local_schema_source(
299        &self,
300        name: &str,
301        version: &str,
302        files: &[(String, Vec<u8>)],
303    ) -> Result<(), EngineError> {
304        let gitdir = self
305            .mounts
306            .iter()
307            .find_map(|m| match &m.mount.storage {
308                crate::workspace::MountStorage::GitBranch { gitdir, .. } => Some(gitdir.clone()),
309                _ => None,
310            })
311            .or_else(|| {
312                self.workspace_root()
313                    .map(|r| r.join("mem-repo").join(".git"))
314            })
315            .filter(|g| g.is_dir())
316            .ok_or_else(|| {
317                EngineError::Mem(
318                    "staging a schema requires a mem-repo workspace — the folder workspace's \
319                     `.memstead/schemas/` is the authoring tier and never holds sealed \
320                     third-party packages"
321                        .to_string(),
322                )
323            })?;
324        let ops = self.git_branch_ops.as_ref().ok_or_else(|| {
325            EngineError::Mem("git-branch ops are not wired on this engine".to_string())
326        })?;
327        (ops.write_schema)(&gitdir, name, version, files).map_err(EngineError::Backend)?;
328        Ok(())
329    }
330
331    /// Validate a schema package's files before they are sealed. Runs
332    /// the full loader (structural + semantic) plus the section-heading
333    /// round-trip gate, and checks the manifest's declared identity
334    /// matches the `(name, version)` the package is being installed
335    /// under — a mismatch would seal the schema under a ref its own
336    /// manifest contradicts.
337    ///
338    /// `pub` so the below-boot install path (memstead-git-branch's
339    /// repair surface) runs the SAME gate as this booted path — the
340    /// two must never fork into separate validation regimes.
341    pub fn validate_schema_package(
342        name: &str,
343        version: &str,
344        files: &[(String, Vec<u8>)],
345    ) -> Result<(), EngineError> {
346        let invalid = |message: String| EngineError::SchemaPackageInvalid {
347            name: name.to_string(),
348            version: version.to_string(),
349            message,
350        };
351        let manifest_yaml = files
352            .iter()
353            .find(|(rel, _)| rel == "schema.yaml")
354            .map(|(_, bytes)| String::from_utf8_lossy(bytes).into_owned())
355            .ok_or_else(|| invalid("package has no schema.yaml".to_string()))?;
356        let types: Vec<(String, String)> = files
357            .iter()
358            .filter_map(|(rel, bytes)| {
359                rel.strip_prefix("types/")
360                    .and_then(|f| f.strip_suffix(".yaml"))
361                    .map(|stem| {
362                        (
363                            stem.to_string(),
364                            String::from_utf8_lossy(bytes).into_owned(),
365                        )
366                    })
367            })
368            .collect();
369        // Install validates the CURRENT language (the author acts
370        // now) — the retired `optional:` key refuses here, and absent
371        // required keys mean optional.
372        let schema = memstead_schema::load_schema_from_memory_with_format(
373            &manifest_yaml,
374            &types,
375            memstead_schema::loader::MetadataPolarityFormat::RequiredOptIn,
376        )
377        .map_err(|e| invalid(e.to_string()))?;
378        memstead_schema::check_section_heading_roundtrip(&schema)
379            .map_err(|e| invalid(e.to_string()))?;
380        memstead_schema::check_reserved_metadata_keys(&schema)
381            .map_err(|e| invalid(e.to_string()))?;
382        memstead_schema::check_section_formats(&schema).map_err(|e| invalid(e.to_string()))?;
383        let (declared_name, declared_version) =
384            (schema.manifest.name.as_str(), schema.version.to_string());
385        if declared_name != name || declared_version != version {
386            return Err(invalid(format!(
387                "manifest declares '{declared_name}@{declared_version}' but the package is \
388                 being installed as '{name}@{version}'"
389            )));
390        }
391        Self::validate_schema_exemplars(&std::sync::Arc::new(schema)).map_err(invalid)?;
392        Ok(())
393    }
394
395    /// Validate every type exemplar a schema carries by running it
396    /// through the REAL create validation stage (agent-trust plan 09):
397    /// an in-memory engine is booted with the candidate schema pinned
398    /// on a virtual mem, and each exemplar is submitted as a
399    /// `dry_run` create — the same gates a real write runs (sections,
400    /// metadata + enums, rel-type vocabulary, edge shape, description
401    /// posture), commit-free by construction. Placeholder relation
402    /// targets are bare slugs scoped to the virtual mem, so target
403    /// existence is never checked (an absent target is the legal
404    /// would-be-stub path).
405    ///
406    /// Returns the defect as a message naming the type — the caller
407    /// wraps it in its own typed envelope (`SchemaPackageInvalid` on
408    /// the install path). There is deliberately no warn-and-carry
409    /// mode: a non-conformant exemplar refuses, because the whole
410    /// value of an exemplar is the impossibility of drift.
411    ///
412    /// `pub` so the built-in suite gates every shipped exemplar
413    /// through the SAME validator (a broken built-in exemplar fails
414    /// CI), and the below-boot install path shares the gate via
415    /// [`Self::validate_schema_package`].
416    pub fn validate_schema_exemplars(
417        schema: &std::sync::Arc<memstead_schema::Schema>,
418    ) -> Result<(), String> {
419        let with_exemplars: Vec<&str> = schema
420            .manifest
421            .types
422            .iter()
423            .filter(|t| {
424                schema
425                    .types
426                    .get(t.as_str())
427                    .is_some_and(|td| td.exemplar.is_some())
428            })
429            .map(String::as_str)
430            .collect();
431        if with_exemplars.is_empty() {
432            return Ok(());
433        }
434
435        let (name, version) = schema.id();
436        let mem = "exemplar";
437        let mount = crate::workspace::Mount {
438            mem: mem.to_string(),
439            schema: Some(memstead_schema::SchemaRef::new(name, version)),
440            storage: crate::workspace::MountStorage::InMemory,
441            capability: crate::workspace::MountCapability::Write,
442            lifecycle: crate::workspace::MountLifecycle::Eager,
443            cross_linkable: true,
444            migration_target: None,
445        };
446        let backend = Box::new(crate::storage::InMemoryBackend::new()) as Box<dyn MemBackend>;
447        let mut engine = Engine::from_mounts_with_schemas_dir_and_extra(
448            vec![(mount, backend)],
449            None,
450            vec![schema.clone()],
451        )
452        .map_err(|e| format!("exemplar validation could not boot: {e}"))?;
453
454        for type_name in with_exemplars {
455            let td = schema
456                .types
457                .get(type_name)
458                .expect("filtered on presence above");
459            let ex = td.exemplar.as_ref().expect("filtered on presence above");
460            let mut relations = Vec::with_capacity(ex.relations.len());
461            for r in &ex.relations {
462                let target = r.target_slug();
463                if target.contains("--") || target.trim().is_empty() {
464                    return Err(format!(
465                        "type '{type_name}' exemplar relation target '{target}' must be a bare \
466                         placeholder slug (no `--`, non-empty) — exemplars live outside \
467                         any mem",
468                    ));
469                }
470                relations.push(crate::ops::RelateArg {
471                    target: crate::entity::EntityId::new(mem, target),
472                    rel_type: r.rel_type_name().to_string(),
473                    description: r.description.clone(),
474                });
475            }
476            let args = crate::engine::CreateEntityArgs {
477                anchors: Vec::new(),
478                mem: mem.to_string(),
479                title: ex.title.clone(),
480                entity_type: type_name.to_string(),
481                sections: ex.sections.clone(),
482                metadata: ex.metadata.clone(),
483                relations,
484                dry_run: true,
485            };
486            if let Err(e) = engine.create_entity(args, crate::vcs::Actor::Cli, None, None) {
487                return Err(format!(
488                    "type '{type_name}' exemplar does not conform: [{}] {e}",
489                    e.code()
490                ));
491            }
492        }
493        Ok(())
494    }
495    /// Unregister a writable mem at runtime. Engine-level
496    /// primitive that `memstead_mem_delete` builds on.
497    ///
498    /// Removes the named mount from [`Self::mounts`], drops the
499    /// mem's entities from the store, refreshes the
500    /// [`MemRouterSnapshot`] via `Arc::make_mut` (COW swap so
501    /// readers holding a pre-swap snapshot see the pre-state for
502    /// their lifetime), and invalidates the community + search
503    /// memos. Does NOT touch the backend's on-disk state — the
504    /// caller (`delete_mem` orchestrator) decides whether to
505    /// remove the directory / gitdir after this returns.
506    ///
507    /// Returns `Ok(Some(backend))` when the mem was present and
508    /// unregistered — the caller can drive any backend-specific
509    /// follow-up cleanup (`backend.delete_artifacts()` for the
510    /// mem-repo branch + `__MEMSTEAD` config when `delete_files=true`).
511    /// Returns `Ok(None)` when no mount named the mem (idempotent —
512    /// repeated calls are safe).
513    pub fn unregister_writable_mem(
514        &mut self,
515        mem_name: &str,
516    ) -> Result<Option<Box<dyn MemBackend>>, EngineError> {
517        let pos = self.mounts.iter().position(|m| m.mount.mem == mem_name);
518        let Some(idx) = pos else {
519            return Ok(None);
520        };
521
522        // Drop the mount first — releases all engine-side state that
523        // referenced the backend. The `Box<dyn MemBackend>` itself
524        // travels back to the caller so backend-side cleanup
525        // (`delete_artifacts`) can run after the engine snapshot
526        // settled.
527        let mount = self.mounts.remove(idx);
528
529        // Drop the schema entry for this mem (kept in lockstep
530        // with `self.mounts`).
531        self.schemas_remove(&mount.mount.mem);
532
533        // Drop entities. The store's mem index is the
534        // authoritative count; the return value (number of
535        // entities removed) is informational only — the caller
536        // already knows the mem and doesn't need the count.
537        let _removed = self.store.remove_entities_by_mem(mem_name);
538
539        // Purge load-time warnings attributed to the removed mem.
540        // `health()` merges `self.load_warnings` unconditionally, so
541        // a skipped purge leaves phantom warnings citing entities the
542        // store no longer holds — for the whole engine lifetime, since
543        // nothing else clears the accumulator on the MCP path.
544        // Attribution is by SOURCE mem only (`WarningHint::source_mem`):
545        // a warning whose source entity lives in a surviving mem stays
546        // even when its target pointed into the deleted mem — the
547        // invalid row still exists in that survivor's markdown and
548        // remains visible drift (recover-worthy), not stale state.
549        self.load_warnings
550            .retain(|w| w.source_mem() != Some(mem_name));
551
552        // COW snapshot swap on the mem_router. `Arc::make_mut`
553        // clones the inner snapshot when other Arcs exist; if this
554        // is the only handle (typical for the engine's lifetime),
555        // it returns the existing inner directly without cloning.
556        // Readers that captured an `Arc` before this call observe
557        // the pre-swap state — the in-flight handler's
558        // `mem_router()` borrow is unaffected by this mutation.
559        Arc::make_mut(&mut self.mem_router).remove_writable(mem_name);
560
561        // Invalidate dependent memos — community detection + search
562        // indexes were computed over the pre-removal store and are
563        // now stale. Mutation paths already invalidate; this
564        // matches the contract.
565        self.invalidate_communities();
566        self.invalidate_search_indexes();
567
568        Ok(Some(mount.backend))
569    }
570
571    /// Register a read-only mount at runtime — the install path's
572    /// engine primitive. Same registration pipeline as
573    /// [`Self::register_writable_mem`] (collision probe, config read,
574    /// schema resolution, entity load, router swap); the router branch
575    /// lands the mount in the read-only slot for
576    /// `capability: ReadOnly` + `Archive` storage, so `is_writable`
577    /// stays false and `archive_path_for_mem` resolves.
578    pub fn register_read_mount(
579        &mut self,
580        mount: Mount,
581        backend: Box<dyn MemBackend>,
582        origin: MemOrigin,
583    ) -> Result<(), EngineError> {
584        self.register_writable_mem_inner(mount, backend, origin, true)
585    }
586
587    /// Unregister a read-only mount at runtime — the uninstall path's
588    /// engine primitive, mirroring [`Self::unregister_writable_mem`]
589    /// for the read-only slot. Returns `Ok(None)` when the name is
590    /// not a registered read-only mount (writable mems are the
591    /// delete/unregister verbs' business, deliberately not this
592    /// one's). Registration removal only — the backing archive file
593    /// (global cache) is never touched.
594    pub fn unregister_read_mount(
595        &mut self,
596        mem_name: &str,
597    ) -> Result<Option<Box<dyn MemBackend>>, EngineError> {
598        let pos = self.mounts.iter().position(|m| {
599            m.mount.mem == mem_name
600                && m.mount.capability == crate::workspace::MountCapability::ReadOnly
601        });
602        let Some(idx) = pos else {
603            return Ok(None);
604        };
605        let mount = self.mounts.remove(idx);
606        self.schemas_remove(&mount.mount.mem);
607        let _removed = self.store.remove_entities_by_mem(mem_name);
608        self.load_warnings
609            .retain(|w| w.source_mem() != Some(mem_name));
610        Arc::make_mut(&mut self.mem_router).remove_read_only(mem_name);
611        self.invalidate_communities();
612        self.invalidate_search_indexes();
613        Ok(Some(mount.backend))
614    }
615
616    /// Append a typed load-time warning from outside the engine's own
617    /// load pipeline — the boot orchestrators (which live in the full
618    /// crate) use this to surface one-time migrations they perform
619    /// around engine construction.
620    pub fn push_load_warning(&mut self, warning: crate::ops::WarningHint) {
621        self.load_warnings.push(warning);
622    }
623
624    /// Register a writable mem at runtime. Engine-level primitive
625    /// that `memstead_mem_create` builds on.
626    ///
627    /// Steps:
628    /// 1. Name collision probe against the current `mem_router`
629    ///    snapshot. Writable AND read-only entries collide; the
630    ///    error surfaces the colliding source so the orchestrator
631    ///    can render a recovery hint.
632    /// 2. Schema resolution via the built-in catalogue (mirrors
633    ///    [`Self::from_mounts`]; workspace-authored schema
634    ///    resolution lifts later).
635    /// 3. Per-mem config load (folder backends only; git-branch /
636    ///    archive return None — same contract as
637    ///    [`Self::from_mounts`]).
638    /// 4. Entity load via the backend, parse, push into the engine's
639    ///    store with a `LoadCollector` so drift warnings forward to
640    ///    `self.load_warnings`.
641    /// 5. Insert schema into [`Self::schemas`].
642    /// 6. Push the [`MountedBackend`] into [`Self::mounts`].
643    /// 7. COW snapshot swap on [`Self::mem_router`] via
644    ///    `Arc::make_mut` + `add_writable(name, dir, origin, mem_path)`.
645    ///    Folder mounts surface their on-disk path; other backends
646    ///    register with `dir: None` (matches full's contract).
647    ///    `mem_path` carries the create-time organisational `path`
648    ///    component (mirrors `MemCreateParams.path`) — the
649    ///    delete-side lifecycle composer reads it back to rebuild the
650    ///    `<mem_path>/<name>` candidate the create-side composer
651    ///    matched against. Caller threads `None` for flat-layout
652    ///    registrations and `Some(p)` for hierarchical ones.
653    /// 8. Invalidate community + search memos.
654    ///
655    /// Returns `Err(EngineError::MemNameCollision)` when the name
656    /// is already registered. Other failures (schema-not-found,
657    /// backend read errors) propagate as their typed variants. On
658    /// failure no engine mutation happens: every potentially-
659    /// mutating step runs only after the collision probe succeeds,
660    /// and intermediate failures propagate before the mount /
661    /// router are touched.
662    pub fn register_writable_mem(
663        &mut self,
664        mount: Mount,
665        backend: Box<dyn MemBackend>,
666        origin: MemOrigin,
667    ) -> Result<(), EngineError> {
668        self.register_writable_mem_inner(mount, backend, origin, true)
669    }
670
671    /// [`Self::register_writable_mem`] with the workspace-global
672    /// passes (relation validation, alias remap, memo invalidation)
673    /// made optional: `run_global_passes: false` lets a batch caller
674    /// ([`Self::full_refresh`]) attach N mounts and run the global
675    /// passes ONCE afterwards instead of N times under the engine
676    /// lock. A `false` caller MUST run
677    /// [`Self::finish_batched_registrations`] after its loop, or
678    /// loaded relations skip validation and alias edges stay
679    /// unmapped.
680    fn register_writable_mem_inner(
681        &mut self,
682        mount: Mount,
683        backend: Box<dyn MemBackend>,
684        origin: MemOrigin,
685        run_global_passes: bool,
686    ) -> Result<(), EngineError> {
687        // Step 1: name collision probe.
688        if let Some(existing) = self.mem_router.origin_for_mem(&mount.mem) {
689            return Err(EngineError::MemNameCollision {
690                name: mount.mem.clone(),
691                source_origin: existing.render_source(),
692            });
693        }
694        if self.mem_router.archive_path_for_mem(&mount.mem).is_some() {
695            return Err(EngineError::MemNameCollision {
696                name: mount.mem.clone(),
697                source_origin: "attached read mem".to_string(),
698            });
699        }
700
701        // Step 2: per-mem config load via the backend trait. Read
702        // before resolving the schema — the mem's own config carries
703        // the authoritative pin (mirrors the boot path), so a mem
704        // re-registered or mounted from another machine resolves from
705        // its own backend, not this workspace's mount expectation.
706        let mem_config = backend.read_mem_config().ok().flatten().and_then(|bytes| {
707            let value: serde_json::Value = serde_json::from_slice(&bytes).ok()?;
708            memstead_schema::config::parse_mem_config(&value).ok()
709        });
710
711        // Step 3: schema resolution. `MemConfig.schema` is the
712        // authoritative settled pin; `Mount.schema` is the fallback when
713        // the config carries none, and an expectation assertion when it
714        // does — a disagreement surfaces `SchemaPinMismatch` (config
715        // wins, neither silently dropped). Mirrors `from_mounts_inner`.
716        // Resolve against the engine's full loaded catalogue (already-
717        // loaded workspace/local-storage schemas layered over built-ins)
718        // so a mem registered against a backend-installed (e.g.
719        // git-branch `__MEMSTEAD:schemas/` ref) schema resolves.
720        let mut builtin_schemas: Vec<std::sync::Arc<memstead_schema::Schema>> =
721            self.workspace_schemas().to_vec();
722        builtin_schemas.extend(
723            memstead_schema::builtins::load_builtin_schemas()
724                .map_err(|e| EngineError::SchemaResolverInit(e.to_string()))?,
725        );
726        let config_pin = mem_config.as_ref().and_then(|c| c.schema.as_ref());
727        let mount_pin = mount.schema.as_ref();
728        if let (Some(cfg), Some(mp)) = (config_pin, mount_pin)
729            && cfg != mp
730        {
731            self.load_warnings
732                .push(crate::ops::WarningHint::SchemaPinMismatch {
733                    mem: mount.mem.clone(),
734                    config_pin: cfg.as_display(),
735                    mount_pin: mp.as_display(),
736                });
737        }
738        let settled_pin = config_pin.or(mount_pin);
739        let effective_pin = mount
740            .migration_target
741            .as_ref()
742            .or(settled_pin)
743            .ok_or_else(|| EngineError::MemConfigIncomplete {
744                mem: mount.mem.clone(),
745                missing_fields: vec!["schema".to_string()],
746            })?
747            .clone();
748        let schema = crate::engine::SchemaResolver::new(&builtin_schemas)
749            .resolve(&effective_pin)
750            .map_err(|sources| {
751                EngineError::SchemaNotFound {
752                    mem: mount.mem.clone(),
753                    pin: effective_pin.as_display(),
754                    sources,
755                    install_hint: None,
756                }
757                .with_schema_install_probe(self.workspace_root())
758            })?;
759
760        // Step 4: load entities via the backend, push into the
761        // engine's store with a LoadCollector so drift warnings
762        // forward into `self.load_warnings`. Derive the mem
763        // roster + last-segment suffixes from the POST-registration
764        // view (new mem included) so cross-mem references
765        // targeting the new mem resolve correctly during this
766        // load.
767        let (entries, read_errors) = collect_source_entries(backend.as_ref())?;
768        if let Some(w) =
769            super::boot::unbacked_mount_warning(&mount, backend.as_ref(), Some(entries.len()))
770        {
771            self.load_warnings.push(w);
772        }
773        let load_result = parse_entries(entries, read_errors, &mount.mem, schema.as_ref());
774
775        let mut mem_names: Vec<String> = self.mounts.iter().map(|m| m.mount.mem.clone()).collect();
776        mem_names.push(mount.mem.clone());
777        let known_suffixes: Vec<String> = mem_names
778            .iter()
779            .map(|n| crate::entity::store_builder::last_segment_suffix(n).to_string())
780            .collect();
781        let fallback = engine_fallback_type();
782        push_entities_into_store(
783            &mut self.store,
784            load_result.entities,
785            fallback.as_ref(),
786            Some(crate::entity::store_builder::LoadCollector {
787                warnings: &mut self.load_warnings,
788                known_suffixes: &known_suffixes,
789                mem_names: &mem_names,
790            }),
791        );
792        self.load_errors.extend(load_result.errors);
793
794        // Step 5: insert schema (kept in lockstep with `self.mounts`).
795        self.schemas_insert(mount.mem.clone(), schema);
796
797        // Re-run the parse-time relation validator now that the new
798        // mem's schema is in `self.schemas`. Mirrors the boot path
799        // (`Engine::from_mounts_inner`) — hand-edited or externally-
800        // generated markdown in the newly-attached mount goes through
801        // the same gauntlet (grammar / unknown_rel_type / shape /
802        // cycle) and offending relations are dropped with typed
803        // `PARSED_RELATION_INVALID` warnings on `self.load_warnings`.
804        // The newly-pushed mount isn't in `self.mounts` yet (that's
805        // Step 6 below), so build the map from `self.mounts` plus the
806        // about-to-be-attached mount we're still holding.
807        if run_global_passes {
808            let mut mount_caps: std::collections::HashMap<
809                String,
810                crate::workspace::MountCapability,
811            > = self
812                .mounts
813                .iter()
814                .map(|m| (m.mount.mem.clone(), m.mount.capability))
815                .collect();
816            mount_caps.insert(mount.mem.clone(), mount.capability);
817            crate::entity::store_builder::validate_loaded_relations(
818                &mut self.store,
819                &self.schemas,
820                &mount_caps,
821                &mut self.load_warnings,
822            );
823            crate::entity::store_builder::remap_alias_target_edge_sources(
824                &mut self.store,
825                &self.schemas,
826            );
827        }
828
829        // Step 6: push the MountedBackend.
830        let last_known_head = backend.current_head().ok().flatten();
831        let mem_name_for_router = mount.mem.clone();
832        let storage_for_router = mount.storage.clone();
833        let mount_capability_for_router = mount.capability;
834        self.mounts.push(MountedBackend {
835            mount,
836            backend,
837            last_known_head,
838            mem_config,
839            // A runtime-created mem is authored live, not installed from
840            // an archive — it carries no archive-borne provenance payload.
841            archive_provenance: None,
842            // Registered live and loaded in this call — never deferred.
843            deferred: false,
844        });
845
846        // Step 7: COW snapshot swap on mem_router, branched on the
847        // mount's capability — a read-only archive mount registers in
848        // the router's read-only slot (so `is_writable` stays false
849        // and `archive_path_for_mem` resolves), everything else in the
850        // writable slot. Folder mounts surface their on-disk path;
851        // other backends register with `dir: None` (mem-repo-backed
852        // mounts have no working tree).
853        match (&mount_capability_for_router, &storage_for_router) {
854            (crate::workspace::MountCapability::ReadOnly, MountStorage::Archive { path }) => {
855                Arc::make_mut(&mut self.mem_router)
856                    .add_read_only(mem_name_for_router, path.clone());
857            }
858            _ => {
859                let dir: Option<PathBuf> = match &storage_for_router {
860                    MountStorage::Folder { path } => Some(path.clone()),
861                    MountStorage::GitBranch { .. }
862                    | MountStorage::Archive { .. }
863                    | MountStorage::InMemory => None,
864                };
865                Arc::make_mut(&mut self.mem_router).add_writable(mem_name_for_router, dir, origin);
866            }
867        }
868
869        // Step 8: invalidate dependent memos.
870        if run_global_passes {
871            self.invalidate_communities();
872            self.invalidate_search_indexes();
873        }
874
875        Ok(())
876    }
877
878    /// The batched tail of `register_writable_mem_inner(...,
879    /// run_global_passes: false)`: one workspace-global relation
880    /// validation, one alias remap, one memo invalidation for the
881    /// whole batch of registrations.
882    fn finish_batched_registrations(&mut self) {
883        let mount_caps: std::collections::HashMap<String, crate::workspace::MountCapability> = self
884            .mounts
885            .iter()
886            .map(|m| (m.mount.mem.clone(), m.mount.capability))
887            .collect();
888        crate::entity::store_builder::validate_loaded_relations(
889            &mut self.store,
890            &self.schemas,
891            &mount_caps,
892            &mut self.load_warnings,
893        );
894        crate::entity::store_builder::remap_alias_target_edge_sources(
895            &mut self.store,
896            &self.schemas,
897        );
898        self.invalidate_communities();
899        self.invalidate_search_indexes();
900    }
901
902    /// Additive full refresh — the warm-server half of "restart the
903    /// process": re-scan the schema sources and the mount manifest,
904    /// making newly installed schema versions resolvable and newly
905    /// registered mems usable, WITHOUT applying removals. The
906    /// asymmetry is deliberate and is the whole safety argument:
907    /// adding extends what the in-memory store can answer, while
908    /// removing can strand entities, in-flight handles, and cached
909    /// hashes the process is still serving. Removals are skipped and
910    /// reported; a restart applies them.
911    ///
912    /// Failure model is per-item: a schema source or a mount that
913    /// fails to refresh lands in `failures` and never surfaces as
914    /// newly available; the others proceed. Each mount registration
915    /// is all-or-nothing (every fallible step runs before the store
916    /// is touched), so a failed item leaves no half-updated state.
917    /// The workspace-global passes (relation validation, alias remap,
918    /// memo invalidation) run ONCE per refresh regardless of how many
919    /// mounts attached.
920    ///
921    /// A newly mounted mem starts cold and loads like any other
922    /// mount. Content reload of pre-existing mems is NOT part of this
923    /// method — callers that want both (the `memstead_reload
924    /// full=true` surface) run the existing content-reload sweep
925    /// alongside.
926    pub fn full_refresh(&mut self) -> crate::ops::FullRefreshReport {
927        let started = std::time::Instant::now();
928        let mut report = crate::ops::FullRefreshReport::default();
929
930        let Some(root) = self.workspace_root.clone() else {
931            report.failures.push(crate::ops::RefreshFailure {
932                item: "workspace".to_string(),
933                error: "engine has no workspace root (ad-hoc mount-list construction) — \
934                        nothing to re-scan"
935                    .to_string(),
936            });
937            report.elapsed_ms = started.elapsed().as_millis() as u64;
938            return report;
939        };
940
941        // Workspace policy — same best-effort refresh the
942        // workspace-wide content reload performs.
943        self.refresh_workspace_settings_if_possible();
944
945        // --- Schema sources, additively. ---
946        use crate::schema_source::SchemaSource as _;
947        let mut fresh: Vec<std::sync::Arc<memstead_schema::Schema>> = Vec::new();
948        let mut sources_complete = true;
949        match crate::schema_source::FolderSchemaSource::for_workspace(&root).read_schemas() {
950            Ok(mut s) => fresh.append(&mut s),
951            Err(e) => {
952                sources_complete = false;
953                report.failures.push(crate::ops::RefreshFailure {
954                    item: "schema-source:folder".to_string(),
955                    error: e.to_string(),
956                });
957            }
958        }
959        if let Some(ops) = self.git_branch_ops() {
960            match (ops.read_ref_schemas)(&root) {
961                Ok(mut s) => fresh.append(&mut s),
962                Err(e) => {
963                    sources_complete = false;
964                    report.failures.push(crate::ops::RefreshFailure {
965                        item: "schema-source:memstead-ref".to_string(),
966                        error: e.to_string(),
967                    });
968                }
969            }
970        }
971        let key = |s: &memstead_schema::Schema| {
972            let (name, version) = s.id();
973            format!("{name}@{version}")
974        };
975        let existing: std::collections::HashSet<String> =
976            self.workspace_schemas.iter().map(|s| key(s)).collect();
977        let fresh_keys: std::collections::HashSet<String> = fresh.iter().map(|s| key(s)).collect();
978        for schema in fresh {
979            let k = key(&schema);
980            if !existing.contains(&k) && !report.schemas_added.contains(&k) {
981                report.schemas_added.push(k);
982                self.workspace_schemas.push(schema);
983            }
984        }
985        report.schemas_added.sort();
986        // Removal detection is only meaningful when every source was
987        // actually readable — otherwise an unreadable source would
988        // masquerade as a mass removal.
989        if sources_complete {
990            report.schema_removals_skipped = existing
991                .difference(&fresh_keys)
992                .cloned()
993                .collect::<Vec<_>>();
994            report.schema_removals_skipped.sort();
995        }
996
997        // --- Mount manifest, additively. ---
998        let store = crate::workspace_store::FileWorkspaceStore::new();
999        match crate::workspace_store::WorkspaceStoreAdapter::load(&store, &root) {
1000            Err(e) => {
1001                report.failures.push(crate::ops::RefreshFailure {
1002                    item: "mount-manifest".to_string(),
1003                    error: e.to_string(),
1004                });
1005            }
1006            Ok(workspace) => {
1007                let manifest_names: std::collections::HashSet<String> =
1008                    workspace.mounts.iter().map(|m| m.mem.clone()).collect();
1009                let mut any_mounted = false;
1010                for mount in workspace.mounts {
1011                    if self.mounts.iter().any(|m| m.mount.mem == mount.mem) {
1012                        continue;
1013                    }
1014                    let name = mount.mem.clone();
1015                    if mount.capability != crate::workspace::MountCapability::Write {
1016                        report.failures.push(crate::ops::RefreshFailure {
1017                            item: format!("mount:{name}"),
1018                            error: "only writable mounts attach on a warm refresh — \
1019                                    restart the process to attach this mount"
1020                                .to_string(),
1021                        });
1022                        continue;
1023                    }
1024                    let backend = match (self.backend_factory)(&mount) {
1025                        Ok(b) => b,
1026                        Err(e) => {
1027                            report.failures.push(crate::ops::RefreshFailure {
1028                                item: format!("mount:{name}"),
1029                                error: e.to_string(),
1030                            });
1031                            continue;
1032                        }
1033                    };
1034                    match self.register_writable_mem_inner(
1035                        mount,
1036                        backend,
1037                        crate::mem::MemOrigin::ExplicitToml,
1038                        false,
1039                    ) {
1040                        Ok(()) => {
1041                            any_mounted = true;
1042                            report.mems_mounted.push(name);
1043                        }
1044                        Err(e) => report.failures.push(crate::ops::RefreshFailure {
1045                            item: format!("mount:{name}"),
1046                            error: e.to_string(),
1047                        }),
1048                    }
1049                }
1050                // Removals: a currently-mounted WRITABLE mem absent
1051                // from the manifest. Read-only attachments (archive
1052                // read-mems hydrated from per-mem configs) are not
1053                // manifest entries and never count as removals.
1054                report.mem_removals_skipped = self
1055                    .mounts
1056                    .iter()
1057                    .filter(|m| {
1058                        m.mount.capability == crate::workspace::MountCapability::Write
1059                            && !manifest_names.contains(&m.mount.mem)
1060                    })
1061                    .map(|m| m.mount.mem.clone())
1062                    .collect();
1063                report.mem_removals_skipped.sort();
1064                if any_mounted {
1065                    // One workspace-global pass for the whole batch.
1066                    self.finish_batched_registrations();
1067                }
1068            }
1069        }
1070
1071        report.elapsed_ms = started.elapsed().as_millis() as u64;
1072        report
1073    }
1074
1075    /// Override the workspace root after construction. The full
1076    /// boot helper `memstead_git_branch::engine_from_workspace_root`
1077    /// calls this so the engine knows the path even when the boot
1078    /// route runs through the full adapter rather than
1079    /// [`Self::from_workspace_root`].
1080    pub fn set_workspace_root(&mut self, root: PathBuf) {
1081        self.workspace_root = Some(root);
1082    }
1083
1084    /// Persist the engine's current mount list to the workspace
1085    /// store so a freshly-booted sibling process observes the same
1086    /// mem membership. Called by
1087    /// [`crate::mem_management::create_mem`] /
1088    /// [`crate::mem_management::delete_mem`] after the in-memory
1089    /// router mutation lands — without this, the per-mem content
1090    /// (branch + `__MEMSTEAD` config blob, or folder + `.memstead/config.json`)
1091    /// is already on disk, but the next process boot reads an empty
1092    /// `.memstead/state/mounts.json` and the engine starts with zero
1093    /// writable mems.
1094    ///
1095    /// No-op when `workspace_root` is unset (tests / ad-hoc
1096    /// consumers that build the engine directly from a mount list).
1097    /// Production boot paths (`Engine::from_workspace_root` and the
1098    /// full counterpart) always set the root, so the engine-side
1099    /// fix covers every caller — in-process embedders included
1100    /// — by construction.
1101    ///
1102    /// Hardcoded against [`crate::FileWorkspaceStore`] because that
1103    /// is the only V1 adapter; a future SQLite or remote adapter
1104    /// would install through a setter mirroring
1105    /// [`Self::set_backend_factory`].
1106    pub fn persist_state(&self) -> Result<(), EngineError> {
1107        let Some(root) = self.workspace_root.as_ref() else {
1108            return Ok(());
1109        };
1110        use crate::workspace_store::WorkspaceStoreAdapter as _;
1111        let store = crate::FileWorkspaceStore::new();
1112        let map = |e: crate::workspace_store::StoreError| {
1113            EngineError::Mem(format!("persist workspace state: {e}"))
1114        };
1115
1116        // Publish THIS engine's changes, not its whole cached view. An
1117        // earlier version serialized `self.mounts` wholesale, so a
1118        // long-lived process silently dropped every mount a sibling
1119        // process had registered since the cache was taken — the same
1120        // condition the mem-config writers close by re-reading, reaching
1121        // the workspace roster through the one writer they did not cover.
1122        // The delta is computed against `mounts_baseline` (what this
1123        // engine last read or wrote) rather than against a clock: a
1124        // single-writer workspace has an identical on-disk roster, so
1125        // the merge is a no-op there.
1126        // The roster this engine speaks for is the attached mounts PLUS
1127        // the quarantined ones: a quarantined mem's retained Mount is
1128        // what lets `reload` re-attempt the attach after a repair, and
1129        // dropping it from the file is how "degrade, never disappear"
1130        // turns into "disappear". It is also the record the
1131        // quarantine-repair path repins before asking for a state write.
1132        let ours: Vec<crate::workspace::Mount> = self
1133            .mounts
1134            .iter()
1135            .map(|m| m.mount.clone())
1136            .chain(self.quarantined.iter().map(|q| q.mount.clone()))
1137            .collect();
1138
1139        for attempt in 0..8 {
1140            let expected = store.read_state_bytes(root).map_err(map)?;
1141            let on_disk: Vec<crate::workspace::Mount> = match expected.as_deref() {
1142                Some(bytes) => store.parse_state_bytes(root, bytes).map_err(map)?,
1143                None => Vec::new(),
1144            };
1145
1146            let merged = {
1147                let baseline = self.mounts_baseline.borrow();
1148                merge_mount_rosters(&baseline, &ours, on_disk)
1149            };
1150            let workspace = crate::workspace::Workspace {
1151                mounts: merged,
1152                settings: self.settings.clone(),
1153            };
1154
1155            if store
1156                .save_state_cas(root, &workspace, expected.as_deref())
1157                .map_err(map)?
1158            {
1159                *self.mounts_baseline.borrow_mut() = ours;
1160                return Ok(());
1161            }
1162            if attempt == 7 {
1163                return Err(EngineError::Mem(
1164                    "workspace state is being written concurrently: eight compare-and-set \
1165                     attempts all lost the race. Retry, or find the writer that is not \
1166                     backing off."
1167                        .to_string(),
1168                ));
1169            }
1170        }
1171        unreachable!("the loop returns on success and on exhaustion")
1172    }
1173    /// Set a mem's schema pin — the conformance-gated schema-migration
1174    /// trigger. Behaviour per the pinned contract:
1175    ///
1176    /// - requested == current pin → `Noop`, no state change.
1177    /// - requested != pin, mem integral against the target →
1178    ///   atomic switch (`schema_pin = target`, migration state
1179    ///   cleared) in one workspace-store write → `Switched`.
1180    /// - requested != pin, mem NOT integral → enter (or stay in)
1181    ///   dual-pin: `migration_target = target`, writes validate
1182    ///   against the target from this call on, `findings` carries
1183    ///   the non-integral entities → `MigrationStarted`
1184    ///   (first call) / `MigrationPending` (same target re-issued).
1185    /// - re-issued with the in-flight target once every entity is
1186    ///   integral → atomic switch → `Switched`.
1187    ///
1188    /// The trigger is a label change gated by the conformance check —
1189    /// no content hashing. The response hands the agent findings and
1190    /// nothing else (no migration scripts, no hints); each repair
1191    /// write is validated strictly against the target.
1192    pub fn set_mem_schema(
1193        &mut self,
1194        mem: &str,
1195        target: &memstead_schema::SchemaRef,
1196    ) -> Result<crate::engine::SetSchemaOutcome, EngineError> {
1197        use crate::engine::{SetSchemaOutcome, SetSchemaResult};
1198        // A quarantined mem's set-schema IS the repair path (an
1199        // unresolvable pin is the commonest quarantine cause): repin
1200        // the retained mount, then re-attempt the attach — the same
1201        // in-process recovery `reload` performs after an external
1202        // repair. Ordinary mounts proceed below unchanged.
1203        if self.quarantine_reason(mem).is_some() {
1204            return self.set_schema_on_quarantined(mem, target);
1205        }
1206        let mount_idx = self
1207            .mounts
1208            .iter()
1209            .position(|m| m.mount.mem == mem)
1210            .ok_or_else(|| self.unknown_mem_error(mem))?;
1211        // Capability gate, identical in shape and position to the six
1212        // sibling setters (`set_mem_version` … `set_mem_sync_state`):
1213        // a schema-pin change starts a migration — the one lifecycle
1214        // mutation a read-only mount must be able to refuse like any
1215        // other.
1216        if self.mounts[mount_idx].mount.capability != crate::workspace::MountCapability::Write {
1217            return Err(EngineError::ReadOnlyMount(mem.to_string()));
1218        }
1219
1220        // The requested target must resolve before anything else —
1221        // an unknown ref is an error, not a migration into nowhere.
1222        let target_schema = self.resolve_schema_by_ref(target).ok_or_else(|| {
1223            // The migration resolver consulted workspace-authored
1224            // schemas layered over the built-ins (`resolve_schema_by_ref`).
1225            let consulted: Vec<_> = self
1226                .workspace_schemas
1227                .iter()
1228                .chain(self.builtin_schemas.iter())
1229                .cloned()
1230                .collect();
1231            EngineError::SchemaNotFound {
1232                mem: mem.to_string(),
1233                pin: target.as_display(),
1234                sources: crate::engine::error::SchemaSourceDiagnostic::for_failed_pin(
1235                    &target.name,
1236                    &target.version,
1237                    &consulted,
1238                ),
1239                install_hint: None,
1240            }
1241            .with_schema_install_probe(self.workspace_root())
1242        })?;
1243
1244        // `Mount.schema` is now the optional assertion; for a mem the
1245        // operator is actively re-pinning it is normally `Some` (and kept
1246        // in sync with the config by the switch below). `<unset>` covers a
1247        // mount that carried no assertion.
1248        let current_pin = self.mounts[mount_idx].mount.schema.clone();
1249        let current_pin_display = current_pin
1250            .as_ref()
1251            .map(|p| p.as_display())
1252            .unwrap_or_else(|| "<unset>".to_string());
1253        let in_flight = self.mounts[mount_idx].mount.migration_target.clone();
1254
1255        // The noop question is asked of the pin the engine actually
1256        // serves (the loaded schema, resolved from the authoritative
1257        // backend config at boot), never of the mount's expectation
1258        // alone. With a `SCHEMA_PIN_MISMATCH` (mount says 0.4.0, config
1259        // says 0.1.0) the old comparison against the mount answered
1260        // "noop" for a target of 0.4.0 and the config stayed at 0.1.0
1261        // forever: the one command meant to repair the mismatch could
1262        // not. When the served pin already equals the target and only
1263        // the mount expectation lags, the expectation is aligned in
1264        // place and reported as switched.
1265        let served_pin: Option<memstead_schema::SchemaRef> = self.schemas.get(mem).map(|s| {
1266            let (name, version) = s.id();
1267            memstead_schema::SchemaRef::new(name, version)
1268        });
1269        // During a dual-pin migration the served schema IS the target
1270        // (writes validate against it) while the config still pins the
1271        // old generation, so the shortcut applies only with no migration
1272        // in flight; an in-flight target falls through to the
1273        // conformance gate that completes it.
1274        if served_pin.as_ref() == Some(target) && in_flight.is_none() {
1275            if current_pin.as_ref() == Some(target) {
1276                return Ok(SetSchemaOutcome {
1277                    mem: mem.to_string(),
1278                    schema_pin: current_pin_display,
1279                    migration_target: in_flight.map(|t| t.as_display()),
1280                    outcome: SetSchemaResult::Noop,
1281                    findings: Vec::new(),
1282                });
1283            }
1284            self.mounts[mount_idx].mount.schema = Some(target.clone());
1285            self.mounts[mount_idx].mount.migration_target = None;
1286            self.persist_state()?;
1287            return Ok(SetSchemaOutcome {
1288                mem: mem.to_string(),
1289                schema_pin: target.as_display(),
1290                migration_target: None,
1291                outcome: SetSchemaResult::Switched,
1292                findings: Vec::new(),
1293            });
1294        }
1295
1296        // Conformance gate against the requested target. The full
1297        // integrity definition includes the consistency axis, but the
1298        // schema-switch gate is conformance: consistency breaks are
1299        // schema-independent (they neither block nor are caused by a
1300        // pin change) and keep their always-available repair paths.
1301        let findings = crate::ops::integrity::conformance_findings(
1302            &self.store,
1303            mem,
1304            target_schema.as_ref(),
1305            &self.schemas,
1306        );
1307
1308        if findings.is_empty() {
1309            // Atomic switch. The pin's authoritative home is the backend
1310            // config (boot resolution prefers it over `Mount.schema`), so
1311            // persist there FIRST — if that write fails, every other piece
1312            // of state stays untouched and the switch is a clean no-op.
1313            // Without this the new pin landed only in `mounts.json` and was
1314            // silently reverted on the next process boot for any
1315            // config-present mem.
1316            self.persist_mem_schema_pin(mount_idx, target)?;
1317            self.mounts[mount_idx].mount.schema = Some(target.clone());
1318            self.mounts[mount_idx].mount.migration_target = None;
1319            self.schemas_insert(mem.to_string(), target_schema);
1320            self.invalidate_communities();
1321            // The index field set derives from the pinned schema — the
1322            // schema-switch staleness the whole-map drop used to mask
1323            // (flywheel W8/01, criterion 2): this mem's index must be
1324            // rebuilt against the new field set.
1325            self.invalidate_search_indexes();
1326            self.persist_state()?;
1327            return Ok(SetSchemaOutcome {
1328                mem: mem.to_string(),
1329                schema_pin: target.as_display(),
1330                migration_target: None,
1331                outcome: SetSchemaResult::Switched,
1332                findings: Vec::new(),
1333            });
1334        }
1335
1336        let outcome = if in_flight.as_ref() == Some(target) {
1337            SetSchemaResult::MigrationPending
1338        } else {
1339            SetSchemaResult::MigrationStarted
1340        };
1341        self.mounts[mount_idx].mount.migration_target = Some(target.clone());
1342        // Writes validate against the target from this point on —
1343        // the load-bearing dual-pin semantic.
1344        self.schemas_insert(mem.to_string(), target_schema);
1345        self.invalidate_communities();
1346        // Same field-set dependency as the atomic-switch branch above.
1347        self.invalidate_search_indexes();
1348        self.persist_state()?;
1349        Ok(SetSchemaOutcome {
1350            mem: mem.to_string(),
1351            schema_pin: current_pin_display,
1352            migration_target: Some(target.as_display()),
1353            outcome,
1354            findings,
1355        })
1356    }
1357
1358    /// Persist a mem's new schema pin into the authoritative backend
1359    /// config (`.memstead/config.json` for folder, the `__MEMSTEAD`
1360    /// mem-config blob for git-branch).
1361    ///
1362    /// Boot resolution treats the backend config as the authoritative
1363    /// settled pin and `Mount.schema` (the `mounts.json` copy) as a
1364    /// cross-checked assertion. A schema switch that updated only
1365    /// `mounts.json` would therefore be silently reverted on the next
1366    /// process boot — the config still names the old pin. This keeps the
1367    /// authoritative home in sync at switch time.
1368    ///
1369    /// Value-level field bump: only the `"schema"` string is rewritten;
1370    /// every other config field (`readMems`, write guidance, …) is
1371    /// preserved verbatim. Config-absent mems (no `config.json`) keep
1372    /// `Mount.schema` as their settled pin, so there is nothing to update
1373    /// — a clean no-op.
1374    /// **The one way this engine writes a mem config.** Every config-writing
1375    /// operation goes through it; none serializes its cached struct
1376    /// (consistency-sweep 04/03, criteria 1 and 2).
1377    ///
1378    /// The defect it removes: a long-lived MCP server reads each mem's config
1379    /// once at boot and holds it for days. Eight operations used to clone that
1380    /// cached struct, set one field, and write the whole thing back, so
1381    /// anything a sibling process changed in between was gone. The
1382    /// reload-before-operation invariant did not save them, because the
1383    /// staleness probe watches the entity branch (git-branch) or the change
1384    /// log (folder) and a config-only write advances neither.
1385    ///
1386    /// What this does instead is what the schema-pin writer next door already
1387    /// did: read the config the backend HAS, mutate the one field on that
1388    /// JSON, write it back. A field this call did not set cannot be reverted,
1389    /// because this call never had an opinion about it.
1390    ///
1391    /// `apply` receives the freshly-read config, PARSED. It deliberately does
1392    /// not receive raw JSON: an earlier draft handed out the `Value` and each
1393    /// closure wrote its own key name, which put `review_mark` in the file
1394    /// where the struct reads `reviewMark` and lost the field on the next
1395    /// read. Serde owns the wire names; the closures must not restate them.
1396    /// Unknown fields survive because `MemConfig` flattens them into `extra`.
1397    ///
1398    /// `apply` runs again on a re-read if the file moved between the read and
1399    /// the write, so it must be a pure function of the config it is handed,
1400    /// never of the engine's cache.
1401    ///
1402    /// Returns the parsed new config plus, when the stored config had moved on
1403    /// from what this engine last observed, the fields the intervening writer
1404    /// had changed. The caller rides that on its own response as
1405    /// `CONFIG_WRITE_INTERVENED`.
1406    pub(crate) fn write_mem_config_merged(
1407        &mut self,
1408        mount_idx: usize,
1409        mem_name: &str,
1410        note: Option<&str>,
1411        apply: &dyn Fn(&mut memstead_schema::config::MemConfig),
1412    ) -> Result<(memstead_schema::config::MemConfig, Vec<String>), EngineError> {
1413        let backend = self.mounts[mount_idx].backend.as_ref();
1414        let read = |b: &dyn crate::backend::MemBackend| -> Result<Vec<u8>, EngineError> {
1415            b.read_mem_config()
1416                .map_err(|e| EngineError::Mem(format!("read mem config for update: {e}")))?
1417                .ok_or_else(|| {
1418                    EngineError::InvalidInput(format!(
1419                        "mem '{mem_name}' has no stored MemConfig (initialize the mem via \
1420                         `memstead init` or `memstead mem create` first)"
1421                    ))
1422                })
1423        };
1424
1425        let stored = read(backend)?;
1426        // What the intervening writer changed, if anyone did. Compared against
1427        // the engine's cached copy, never against a clock: criterion 6 forbids
1428        // reacting to cache age, and a single-writer workspace has an
1429        // identical cache, so this is empty there (criterion 4).
1430        let intervened = match self.mounts[mount_idx].mem_config.as_ref() {
1431            Some(cached) => changed_config_fields(cached, &stored),
1432            None => Vec::new(),
1433        };
1434
1435        let render =
1436            |raw: &[u8]| -> Result<(memstead_schema::config::MemConfig, Vec<u8>), EngineError> {
1437                let value: serde_json::Value = serde_json::from_slice(raw)
1438                    .map_err(|e| EngineError::Mem(format!("parse mem config for update: {e}")))?;
1439                let mut cfg = memstead_schema::config::parse_mem_config(&value)
1440                    .map_err(|e| EngineError::Mem(format!("parse mem config for update: {e}")))?;
1441                apply(&mut cfg);
1442                let mut bytes = serde_json::to_vec_pretty(&cfg)
1443                    .map_err(|e| EngineError::Mem(format!("serialize mem config: {e}")))?;
1444                bytes.push(b'\n');
1445                Ok((cfg, bytes))
1446            };
1447        let (mut parsed, mut bytes) = render(&stored)?;
1448
1449        // Compare-and-set, done by the backend so the check and the write are
1450        // one step. An earlier draft re-read here and then wrote, which is
1451        // check-then-write and leaves exactly the window criterion 5 names
1452        // open. On a mismatch the loop re-reads, re-applies onto what is
1453        // there, and retries: the intervening writer's change is merged, never
1454        // overwritten. Bounded, because an unbounded retry against a hot
1455        // writer is a hang, and reaching the bound is a real contention
1456        // problem the caller should hear about rather than a state to spin in.
1457        let mut expected = stored;
1458        for attempt in 0..8 {
1459            let wrote = self.mounts[mount_idx].backend.write_mem_config_cas(
1460                Some(&expected),
1461                &bytes,
1462                note,
1463            )?;
1464            if wrote {
1465                break;
1466            }
1467            if attempt == 7 {
1468                return Err(EngineError::Mem(format!(
1469                    "mem '{mem_name}' config is being written concurrently: eight \
1470                     compare-and-set attempts all lost the race. Retry, or find the \
1471                     writer that is not backing off."
1472                )));
1473            }
1474            expected = read(self.mounts[mount_idx].backend.as_ref())?;
1475            let rendered = render(&expected)?;
1476            parsed = rendered.0;
1477            bytes = rendered.1;
1478        }
1479
1480        let mounted = &mut self.mounts[mount_idx];
1481        mounted.mem_config = Some(parsed.clone());
1482        // Refresh the head cursor so the next drift probe does not surface
1483        // MEM_RELOADED for the commit this call just produced.
1484        if let Some(sha) = mounted.backend.current_head().ok().flatten() {
1485            mounted.last_known_head = Some(sha);
1486        }
1487        Ok((parsed, intervened))
1488    }
1489
1490    fn persist_mem_schema_pin(
1491        &mut self,
1492        mount_idx: usize,
1493        target: &memstead_schema::SchemaRef,
1494    ) -> Result<(), EngineError> {
1495        let value = bump_backend_schema_pin(self.mounts[mount_idx].backend.as_ref(), target)?;
1496        // Refresh the cached parsed config so in-session reads observe the
1497        // new pin without a reload.
1498        if let Some(value) = value
1499            && let Ok(cfg) = memstead_schema::config::parse_mem_config(&value)
1500        {
1501            self.mounts[mount_idx].mem_config = Some(cfg);
1502        }
1503        Ok(())
1504    }
1505
1506    /// Regenerate entity markdown files from the in-memory store.
1507    ///
1508    /// Dispatch:
1509    /// - When `mem_filter` is `Some(name)`, only that mem's mount
1510    ///   is considered. If its active backend doesn't support markdown
1511    ///   regeneration in place (today: anything other than
1512    ///   `MountStorage::Folder`), the call refuses with
1513    ///   [`EngineError::MarkdownExportUnsupportedBackend`] carrying
1514    ///   the active backend's id and the supported-backend list.
1515    /// - When `mem_filter` is `None`, every mount is iterated.
1516    ///   Folder mounts regenerate as today; non-folder mounts are
1517    ///   recorded in [`crate::ops::ExportResult::skipped_mounts`] so
1518    ///   the caller can surface the partial-success shape.
1519    ///
1520    /// Per-folder-mount behaviour: iterate the store, regenerate each
1521    /// non-stub entity belonging to the mount's mem, compare to the
1522    /// on-disk file, write if changed.
1523    ///
1524    /// `schema_filter` narrows the per-entity-type subset: when
1525    /// `Some(name)`, only entities whose `entity_type` matches are
1526    /// regenerated. `None` exports every type.
1527    ///
1528    /// Pre-fix this returned
1529    /// `ExportResult { written: 0, unchanged: 0 }` for git-branch /
1530    /// archive mounts — a successful-looking no-op that masked the
1531    /// backend-incompatibility. The typed refusal (per-mem) and the
1532    /// `skipped_mounts` channel (workspace-wide) give the caller an
1533    /// agent-actionable signal in one round-trip.
1534    pub fn export_markdown(
1535        &self,
1536        mem_filter: Option<&str>,
1537        schema_filter: Option<&str>,
1538    ) -> Result<crate::ops::ExportResult, EngineError> {
1539        use crate::workspace::MountStorage;
1540        let fallback = engine_fallback_type();
1541        let supported_backends = vec!["folder".to_string()];
1542
1543        if let Some(name) = mem_filter {
1544            let mount = self
1545                .mounts
1546                .iter()
1547                .find(|m| m.mount.mem == name)
1548                .ok_or_else(|| self.unknown_mem_error(name))?;
1549            if !matches!(mount.mount.storage, MountStorage::Folder { .. }) {
1550                return Err(EngineError::MarkdownExportUnsupportedBackend {
1551                    mem: name.to_string(),
1552                    active_backend: mount.mount.storage.backend_id().to_string(),
1553                    supported_backends,
1554                });
1555            }
1556        }
1557
1558        let mut total_written = 0;
1559        let mut refused: Vec<crate::ops::RefusedEntity> = Vec::new();
1560        let mut total_unchanged = 0;
1561        let mut skipped_mounts: Vec<crate::ops::SkippedMount> = Vec::new();
1562
1563        for mount in &self.mounts {
1564            let mem_name = mount.mount.mem.as_str();
1565            if let Some(filter) = mem_filter
1566                && mem_name != filter
1567            {
1568                continue;
1569            }
1570            let MountStorage::Folder { path: mem_dir } = &mount.mount.storage else {
1571                skipped_mounts.push(crate::ops::SkippedMount {
1572                    mem: mem_name.to_string(),
1573                    active_backend: mount.mount.storage.backend_id().to_string(),
1574                    reason: "backend_does_not_support_markdown_export".to_string(),
1575                });
1576                continue;
1577            };
1578            let schema = match self.schemas.get(mem_name) {
1579                Some(s) => s,
1580                None => continue,
1581            };
1582
1583            for entity in self.store.all_entities() {
1584                if entity.stub || entity.file_path.is_empty() {
1585                    continue;
1586                }
1587                if entity.id.mem() != mem_name {
1588                    continue;
1589                }
1590                if let Some(filter) = schema_filter
1591                    && entity.entity_type != filter
1592                {
1593                    continue;
1594                }
1595                let type_def = schema
1596                    .get_type(&entity.entity_type)
1597                    .unwrap_or_else(|| fallback.clone());
1598                let generated = generate_markdown(entity, type_def.as_ref());
1599
1600                let full_path = mem_dir.join(&entity.file_path);
1601                let needs_write = match std::fs::read_to_string(&full_path) {
1602                    Ok(existing) => existing != generated,
1603                    Err(_) => true,
1604                };
1605                if needs_write {
1606                    match crate::entity::writer::write_entity(entity, mem_dir, type_def.as_ref()) {
1607                        Ok(_) => total_written += 1,
1608                        // The one refusal the export must not swallow: writing
1609                        // this entity would bury the sections its open fence
1610                        // absorbed. Name it and carry on — an export that
1611                        // aborted here would strand every other entity.
1612                        Err(e @ crate::entity::writer::WriteError::UnterminatedFence { .. }) => {
1613                            refused.push(crate::ops::RefusedEntity {
1614                                id: entity.id.to_string(),
1615                                reason: "UNTERMINATED_FENCE_IN_STORED_BODY".to_string(),
1616                                detail: e.to_string(),
1617                            });
1618                        }
1619                        Err(_) => {}
1620                    }
1621                } else {
1622                    total_unchanged += 1;
1623                }
1624            }
1625        }
1626
1627        Ok(crate::ops::ExportResult {
1628            refused_entities: refused,
1629            written: total_written,
1630            unchanged: total_unchanged,
1631            skipped_mounts,
1632        })
1633    }
1634
1635    /// Export a mem as a portable `.mem` archive.
1636    ///
1637    /// Dispatch is internal: the engine looks up the mount whose mem
1638    /// name matches and branches on its `MountStorage`. Folder mounts
1639    /// produce a snapshot archive (current `.md` files + config);
1640    /// git-branch mounts invoke the registered [`GitBranchOps::export`]
1641    /// hook to produce a history archive (the per-mem branch tip's
1642    /// tree); archive mounts reject with `BackendError::Sealed`
1643    /// (already-an-archive — no meaningful re-export).
1644    ///
1645    /// The mem's `MemConfig` is looked up via
1646    /// [`Self::mem_config_for`]; unloaded configs (folder mounts
1647    /// without a `.memstead/config.json`, git-branch mounts without a
1648    /// `__MEMSTEAD:mems/<mem>/config.json`) surface as
1649    /// `EngineError::InvalidInput`. Workspace-level schema dir is
1650    /// threaded from `self.settings.schemas_dir` for the
1651    /// schema-source resolution chain.
1652    pub fn export_mem(
1653        &self,
1654        mem_name: &str,
1655        output_path: &std::path::Path,
1656    ) -> Result<crate::ops::MemExportResult, EngineError> {
1657        let mount = self
1658            .mounts
1659            .iter()
1660            .find(|m| m.mount.mem == mem_name)
1661            .ok_or_else(|| self.unknown_mem_error(mem_name))?;
1662        let config = self.mem_config_for(mem_name).ok_or_else(|| {
1663            EngineError::InvalidInput(format!(
1664                "mem '{mem_name}' has no loaded MemConfig — cannot export"
1665            ))
1666        })?;
1667        // F1: surface the missing-version case as a typed
1668        // `MEM_CONFIG_INCOMPLETE` envelope with structured recovery
1669        // details, rather than letting it bubble through as the
1670        // backend's `INTERNAL` collapse pointing at the wrong path
1671        // (`.memstead/config.json` is the folder-backend layout — the
1672        // mem-repo backend keeps the blob under `__MEMSTEAD:mems/`).
1673        // The check fires for both backends symmetrically.
1674        if config.version.is_none() {
1675            return Err(EngineError::MemConfigIncomplete {
1676                mem: mem_name.to_string(),
1677                missing_fields: vec!["version".to_string()],
1678            });
1679        }
1680        // Collected before the backend split so both storage flavours report
1681        // it. `install` refuses the archive for each of these; naming them at
1682        // export time is the same courtesy the dangling cross-mem edges get,
1683        // and for the same reason: an operator who learns at install time has
1684        // already shared the archive.
1685        let fenced: Vec<String> = self
1686            .store
1687            .all_entities()
1688            .filter(|e| e.mem == mem_name && !e.stub)
1689            .filter(|e| {
1690                e.sections
1691                    .values()
1692                    .any(|v| crate::markdown::closing_fence_if_unterminated(v.trim()).is_some())
1693            })
1694            .map(|e| e.id.to_string())
1695            .collect();
1696        let workspace_root = self.workspace_root.as_deref();
1697        // Authored schemas live at the fixed `<workspace>/.memstead/schemas/`
1698        // location (the `schemas_dir` key is retired). Absent dir → the
1699        // schema-source chain falls through to cache/built-in, as before.
1700        let fixed_schemas_dir = workspace_root.map(|r| r.join(".memstead").join("schemas"));
1701        let workspace_schemas_dir = fixed_schemas_dir.as_deref();
1702        let exported = match &mount.mount.storage {
1703            MountStorage::Folder { path } => crate::ops::export::export_mem(
1704                path,
1705                config,
1706                output_path,
1707                workspace_root,
1708                workspace_schemas_dir,
1709            )
1710            .map_err(|e| EngineError::Backend(BackendError::Other(format!("export_mem: {e}")))),
1711            MountStorage::GitBranch { gitdir, branch } => {
1712                let hook = self.git_branch_ops.as_ref().ok_or_else(|| {
1713                    EngineError::Backend(BackendError::Other(
1714                        "git-branch export hook not installed (full flavour not loaded)"
1715                            .to_string(),
1716                    ))
1717                })?;
1718                // Source per-entity provenance from the git-branch mutation
1719                // log (commit trailers) and hand the serialised payload to
1720                // the export hook to embed — symmetric with the bytes path.
1721                let provenance_bytes = mount
1722                    .backend
1723                    .read_provenance(None)
1724                    .ok()
1725                    .and_then(|records| crate::ops::export::build_archive_provenance(&records))
1726                    .and_then(|prov| prov.to_archive_bytes().ok());
1727                // Source the anchors sidecar from the branch tip — symmetric
1728                // with the bytes-export path so the disk `.mem` carries anchors.
1729                let anchors_bytes = mount.backend.read_anchors_sidecar().ok().flatten();
1730                (hook.export)(
1731                    gitdir,
1732                    branch,
1733                    mem_name,
1734                    config,
1735                    output_path,
1736                    workspace_root,
1737                    workspace_schemas_dir,
1738                    provenance_bytes.as_deref(),
1739                    anchors_bytes.as_deref(),
1740                )
1741                .map_err(EngineError::Backend)
1742            }
1743            MountStorage::Archive { .. } => Err(EngineError::Backend(BackendError::Sealed)),
1744            // `.mem` export from an in-memory mem lands with the
1745            // writable-session-server plan (it needs a backend-level
1746            // archive builder); this plan adds the backend, not the
1747            // export path, so refuse explicitly rather than silently.
1748            MountStorage::InMemory => Err(EngineError::Backend(BackendError::Other(
1749                "export not yet supported for in-memory backend".to_string(),
1750            ))),
1751        };
1752        exported.map(|mut r| {
1753            r.unterminated_fence_entities = fenced;
1754            r
1755        })
1756    }
1757
1758    /// Update a mem's `version` field in its per-mem config and
1759    /// persist it through the backend. Backend-symmetric: folder
1760    /// backends rewrite `.memstead/config.json`; git-branch backends
1761    /// commit `__MEMSTEAD:mems/<mem>/config.json`. Archive mounts
1762    /// reject with `BackendError::Sealed`.
1763    ///
1764    /// Returns the (mem, old_version, new_version) triple so
1765    /// callers can surface the change without an extra read. Reads
1766    /// the current value from the in-memory `MemConfig` and
1767    /// updates it on success, keeping the next call free of a
1768    /// stale-version read.
1769    ///
1770    /// `EngineError::UnknownMem` when the name resolves to no
1771    /// mount; `EngineError::ReadOnlyMount` when the mount is sealed
1772    /// for writes; `EngineError::InvalidInput` when the mount has no
1773    /// loaded `MemConfig` (folder mount with no
1774    /// `.memstead/config.json`; the residual missing-config path is
1775    /// distinct from the missing-version path). F1.
1776    /// Record pipeline-edit provenance through `mem`'s backend — the
1777    /// bridge the pipeline-edit block (outside the engine module) uses
1778    /// to reach a mount's backend. A mem that isn't currently mounted
1779    /// is a successful no-op: pipeline configs may reference unmounted
1780    /// mems, and provenance is recorded against the mounted set.
1781    pub fn record_pipeline_edit_provenance(
1782        &self,
1783        mem: &str,
1784        kind: &str,
1785        edits: &[(String, Option<Vec<u8>>)],
1786        note: Option<&str>,
1787        verb: &str,
1788    ) -> Result<(), crate::backend::BackendError> {
1789        match self.mounts.iter().find(|m| m.mount.mem == mem) {
1790            Some(m) => m.backend.record_pipeline_edit(kind, edits, note, verb),
1791            None => Ok(()),
1792        }
1793    }
1794
1795    pub fn set_mem_version(
1796        &mut self,
1797        mem_name: &str,
1798        new_version: semver::Version,
1799        note: Option<&str>,
1800    ) -> Result<crate::ops::SetMemVersionOutcome, EngineError> {
1801        // Resolve the mount up-front so an unknown-mem name refuses
1802        // before any drift-probe side effect lands.
1803        let mount_idx = self
1804            .mounts
1805            .iter()
1806            .position(|m| m.mount.mem == mem_name)
1807            .ok_or_else(|| self.unknown_mem_error(mem_name))?;
1808        if self.mounts[mount_idx].mount.capability != crate::workspace::MountCapability::Write {
1809            return Err(EngineError::ReadOnlyMount(mem_name.to_string()));
1810        }
1811
1812        // Probe for concurrent-drift before the write — a sibling
1813        // engine that committed between our last snapshot and now
1814        // surfaces `MEM_RELOADED` on the response so callers see
1815        // the drift without a separate read round-trip. Drift
1816        // warnings ride alongside the success outcome; an
1817        // unreachable-backend probe collapses to no warnings (the
1818        // existing accessor warn-logs internally and skips).
1819        let mut warnings = self.reload_if_stale(Some(mem_name));
1820        // Provenance nudge — same posture as every other commit-
1821        // producing mutation: when `require_notes` is set and no note
1822        // was supplied, ride a non-blocking `NOTE_MISSING` warning.
1823        // The version bump still commits.
1824        if let Some(w) = self.note_missing_warning("set_mem_version", note) {
1825            warnings.push(w);
1826        }
1827
1828        // The old value is read from the STORED config, not the cache: the
1829        // cache may be days stale, and reporting a version the file has not
1830        // held since boot would be its own small lie.
1831        let old_version = self.mounts[mount_idx]
1832            .mem_config
1833            .as_ref()
1834            .and_then(|c| c.version.clone());
1835        let target = new_version.clone();
1836        let (_, intervened) = self.write_mem_config_merged(
1837            mount_idx,
1838            mem_name,
1839            note,
1840            &move |c: &mut memstead_schema::config::MemConfig| {
1841                c.version = Some(target.clone());
1842            },
1843        )?;
1844        if !intervened.is_empty() {
1845            warnings.push(crate::ops::WarningHint::ConfigWriteIntervened {
1846                mem: mem_name.to_string(),
1847                fields: intervened,
1848            });
1849        }
1850
1851        Ok(crate::ops::SetMemVersionOutcome {
1852            mem: mem_name.to_string(),
1853            old_version,
1854            new_version,
1855            warnings,
1856        })
1857    }
1858
1859    /// Update a mem's `description` field in its per-mem config and
1860    /// persist it through the backend — the one-line text mem-archive
1861    /// export embeds and the registry card surfaces. `None` clears the
1862    /// field. Same backend symmetry, drift probe, and provenance-note
1863    /// posture as [`Self::set_mem_version`]; archive mounts reject with
1864    /// `BackendError::Sealed`.
1865    pub fn set_mem_description(
1866        &mut self,
1867        mem_name: &str,
1868        new_description: Option<String>,
1869        note: Option<&str>,
1870    ) -> Result<crate::ops::SetMemDescriptionOutcome, EngineError> {
1871        let mount_idx = self
1872            .mounts
1873            .iter()
1874            .position(|m| m.mount.mem == mem_name)
1875            .ok_or_else(|| self.unknown_mem_error(mem_name))?;
1876        if self.mounts[mount_idx].mount.capability != crate::workspace::MountCapability::Write {
1877            return Err(EngineError::ReadOnlyMount(mem_name.to_string()));
1878        }
1879
1880        let mut warnings = self.reload_if_stale(Some(mem_name));
1881        if let Some(w) = self.note_missing_warning("set_mem_description", note) {
1882            warnings.push(w);
1883        }
1884
1885        let old_description = self.mounts[mount_idx]
1886            .mem_config
1887            .as_ref()
1888            .and_then(|c| c.description.clone());
1889        let target = new_description.clone();
1890        let (_, intervened) = self.write_mem_config_merged(
1891            mount_idx,
1892            mem_name,
1893            note,
1894            &move |c: &mut memstead_schema::config::MemConfig| {
1895                c.description = target.clone();
1896            },
1897        )?;
1898        if !intervened.is_empty() {
1899            warnings.push(crate::ops::WarningHint::ConfigWriteIntervened {
1900                mem: mem_name.to_string(),
1901                fields: intervened,
1902            });
1903        }
1904
1905        Ok(crate::ops::SetMemDescriptionOutcome {
1906            mem: mem_name.to_string(),
1907            old_description,
1908            new_description,
1909            warnings,
1910        })
1911    }
1912
1913    /// Update a mem's display `title` — free text, NOT identity: the
1914    /// mem name stays the sole handle everywhere. `None` clears it.
1915    /// Mirrors [`Self::set_mem_description`] in backend symmetry,
1916    /// drift probe, and provenance-note posture.
1917    pub fn set_mem_title(
1918        &mut self,
1919        mem_name: &str,
1920        new_title: Option<String>,
1921        note: Option<&str>,
1922    ) -> Result<crate::ops::SetMemTitleOutcome, EngineError> {
1923        let mount_idx = self
1924            .mounts
1925            .iter()
1926            .position(|m| m.mount.mem == mem_name)
1927            .ok_or_else(|| self.unknown_mem_error(mem_name))?;
1928        if self.mounts[mount_idx].mount.capability != crate::workspace::MountCapability::Write {
1929            return Err(EngineError::ReadOnlyMount(mem_name.to_string()));
1930        }
1931
1932        let mut warnings = self.reload_if_stale(Some(mem_name));
1933        if let Some(w) = self.note_missing_warning("set_mem_title", note) {
1934            warnings.push(w);
1935        }
1936
1937        let old_title = self.mounts[mount_idx]
1938            .mem_config
1939            .as_ref()
1940            .and_then(|c| c.title.clone());
1941        let target = new_title.clone();
1942        let (_, intervened) = self.write_mem_config_merged(
1943            mount_idx,
1944            mem_name,
1945            note,
1946            &move |c: &mut memstead_schema::config::MemConfig| {
1947                c.title = target.clone();
1948            },
1949        )?;
1950        if !intervened.is_empty() {
1951            warnings.push(crate::ops::WarningHint::ConfigWriteIntervened {
1952                mem: mem_name.to_string(),
1953                fields: intervened,
1954            });
1955        }
1956
1957        Ok(crate::ops::SetMemTitleOutcome {
1958            mem: mem_name.to_string(),
1959            old_title,
1960            new_title,
1961            warnings,
1962        })
1963    }
1964
1965    /// Update a mem's `subject` block — scope, method, deliberate
1966    /// exclusions, published verbatim. `None` clears the block AS A
1967    /// UNIT. Mirrors [`Self::set_mem_description`].
1968    pub fn set_mem_subject(
1969        &mut self,
1970        mem_name: &str,
1971        new_subject: Option<memstead_schema::MemSubject>,
1972        note: Option<&str>,
1973    ) -> Result<crate::ops::SetMemSubjectOutcome, EngineError> {
1974        let mount_idx = self
1975            .mounts
1976            .iter()
1977            .position(|m| m.mount.mem == mem_name)
1978            .ok_or_else(|| self.unknown_mem_error(mem_name))?;
1979        if self.mounts[mount_idx].mount.capability != crate::workspace::MountCapability::Write {
1980            return Err(EngineError::ReadOnlyMount(mem_name.to_string()));
1981        }
1982
1983        let mut warnings = self.reload_if_stale(Some(mem_name));
1984        if let Some(w) = self.note_missing_warning("set_mem_subject", note) {
1985            warnings.push(w);
1986        }
1987
1988        let old_subject = self.mounts[mount_idx]
1989            .mem_config
1990            .as_ref()
1991            .and_then(|c| c.subject.clone());
1992        let target = new_subject.clone();
1993        let (_, intervened) = self.write_mem_config_merged(
1994            mount_idx,
1995            mem_name,
1996            note,
1997            &move |c: &mut memstead_schema::config::MemConfig| {
1998                c.subject = target.clone();
1999            },
2000        )?;
2001        if !intervened.is_empty() {
2002            warnings.push(crate::ops::WarningHint::ConfigWriteIntervened {
2003                mem: mem_name.to_string(),
2004                fields: intervened,
2005            });
2006        }
2007
2008        Ok(crate::ops::SetMemSubjectOutcome {
2009            mem: mem_name.to_string(),
2010            old_subject,
2011            new_subject,
2012            warnings,
2013        })
2014    }
2015
2016    /// Mark (or unmark) a mem as **internal** — hidden from the default
2017    /// `memstead_overview` roster and public projections, while remaining a
2018    /// real, schema-validated, diffable mem (inspectable when explicitly
2019    /// scoped by name, and deletable). The ingest process-state redesign
2020    /// (candidate (b)) flags each `ingest/<name>` process mem this way so it
2021    /// does not clutter the roster alongside real content.
2022    ///
2023    /// Stored as the top-level `internal` config field (captured by the
2024    /// flattened `extra` map). Backend-symmetric like
2025    /// [`Self::set_mem_description`]; `EngineError::UnknownMem` /
2026    /// `ReadOnlyMount` / `InvalidInput` on the usual failures.
2027    pub fn set_mem_internal(
2028        &mut self,
2029        mem_name: &str,
2030        internal: bool,
2031        note: Option<&str>,
2032    ) -> Result<crate::ops::SetMemInternalOutcome, EngineError> {
2033        let mount_idx = self
2034            .mounts
2035            .iter()
2036            .position(|m| m.mount.mem == mem_name)
2037            .ok_or_else(|| self.unknown_mem_error(mem_name))?;
2038        if self.mounts[mount_idx].mount.capability != crate::workspace::MountCapability::Write {
2039            return Err(EngineError::ReadOnlyMount(mem_name.to_string()));
2040        }
2041
2042        let _ = self.reload_if_stale(Some(mem_name));
2043
2044        let (_, intervened) = self.write_mem_config_merged(
2045            mount_idx,
2046            mem_name,
2047            note,
2048            &move |c: &mut memstead_schema::config::MemConfig| {
2049                if internal {
2050                    c.extra
2051                        .insert("internal".to_string(), serde_json::Value::Bool(true));
2052                } else {
2053                    c.extra.remove("internal");
2054                }
2055            },
2056        )?;
2057        // This one used to return a bare `bool` and so had nowhere to put the
2058        // intervention report: it was dropped, not even logged (04/03,
2059        // criterion 3, found by the plan's grade). A writer whose signature
2060        // cannot carry a warning is a writer that silently will not.
2061        let mut warnings = Vec::new();
2062        if !intervened.is_empty() {
2063            warnings.push(crate::ops::WarningHint::ConfigWriteIntervened {
2064                mem: mem_name.to_string(),
2065                fields: intervened,
2066            });
2067        }
2068
2069        Ok(crate::ops::SetMemInternalOutcome {
2070            mem: mem_name.to_string(),
2071            internal,
2072            warnings,
2073        })
2074    }
2075
2076    /// Set (or clear) one opaque sync-state token in a mem's per-mem
2077    /// config and persist it through the backend. The ingest layer calls
2078    /// this after a successful pass over a source's changed slice to
2079    /// record "the source state the graph was last synced against".
2080    ///
2081    /// `key` and `token` are both opaque to the engine: the key is
2082    /// conventionally `"<ingest>/<facet>"` but the engine treats it as an
2083    /// arbitrary string; the token's meaning belongs to the medium-type
2084    /// layer (git → commit id, graph → snapshot token, filesystem → a
2085    /// JSON-stringified stat digest). The engine never parses either.
2086    /// An **empty** `token` removes the key — the surface for clearing a
2087    /// baseline (which the next ingest pass re-seeds at the current
2088    /// source state).
2089    ///
2090    /// Backend-symmetric like [`Self::set_mem_version`]: folder backends
2091    /// rewrite `.memstead/config.json`; git-branch backends commit
2092    /// `__MEMSTEAD:mems/<mem>/config.json`. Archive mounts reject with
2093    /// `BackendError::Sealed`.
2094    ///
2095    /// Returns the (mem, key, previous-token) triple so callers can
2096    /// surface the change without an extra read. `EngineError::UnknownMem`
2097    /// when the name resolves to no mount; `EngineError::ReadOnlyMount`
2098    /// when the mount is sealed for writes; `EngineError::InvalidInput`
2099    /// when the mount has no loaded `MemConfig`.
2100    pub fn set_mem_sync_state(
2101        &mut self,
2102        mem_name: &str,
2103        key: &str,
2104        token: &str,
2105        note: Option<&str>,
2106    ) -> Result<crate::ops::SetMemSyncStateOutcome, EngineError> {
2107        // Resolve the mount up-front so an unknown-mem name refuses
2108        // before any drift-probe side effect lands.
2109        let mount_idx = self
2110            .mounts
2111            .iter()
2112            .position(|m| m.mount.mem == mem_name)
2113            .ok_or_else(|| self.unknown_mem_error(mem_name))?;
2114        if self.mounts[mount_idx].mount.capability != crate::workspace::MountCapability::Write {
2115            return Err(EngineError::ReadOnlyMount(mem_name.to_string()));
2116        }
2117
2118        // Probe for concurrent-drift before the write — same posture as
2119        // every other commit-producing mutation; a sibling engine that
2120        // committed since our last snapshot surfaces `MEM_RELOADED`.
2121        let mut warnings = self.reload_if_stale(Some(mem_name));
2122        if let Some(w) = self.note_missing_warning("set_mem_sync_state", note) {
2123            warnings.push(w);
2124        }
2125
2126        // `previous` has to come from the config this write actually lands
2127        // on, not from the cache: a sibling ingest pass may have moved the
2128        // very token this call is replacing, and reporting the cached value
2129        // would name a baseline that has not been current since boot. The
2130        // cell is written by each `apply` pass, so after a compare-and-set
2131        // retry it holds what was really overwritten.
2132        let seen: std::cell::RefCell<Option<String>> = std::cell::RefCell::new(None);
2133        let key_owned = key.to_string();
2134        let token_owned = token.to_string();
2135        let (_, intervened) = self.write_mem_config_merged(
2136            mount_idx,
2137            mem_name,
2138            note,
2139            &|c: &mut memstead_schema::config::MemConfig| {
2140                *seen.borrow_mut() = if token_owned.is_empty() {
2141                    c.sync_state.remove(&key_owned)
2142                } else {
2143                    c.sync_state.insert(key_owned.clone(), token_owned.clone())
2144                };
2145            },
2146        )?;
2147        let previous = seen.into_inner();
2148        // `removed` distinguishes a no-op clear (key absent) from a real one
2149        // so the outcome is honest.
2150        let removed = token.is_empty() && previous.is_some();
2151        if !intervened.is_empty() {
2152            warnings.push(crate::ops::WarningHint::ConfigWriteIntervened {
2153                mem: mem_name.to_string(),
2154                fields: intervened,
2155            });
2156        }
2157
2158        Ok(crate::ops::SetMemSyncStateOutcome {
2159            mem: mem_name.to_string(),
2160            key: key.to_string(),
2161            previous,
2162            removed,
2163            warnings,
2164        })
2165    }
2166
2167    /// Re-read the named mount's backend entities and refresh the
2168    /// in-memory store for that mem. Returns the diff against the
2169    /// pre-reload snapshot — `added` (ids newly present), `removed`
2170    /// (ids no longer present), `changed` (same id, different
2171    /// `content_hash`).
2172    ///
2173    /// Operator-triggered: useful when an external writer modified
2174    /// disk while this engine instance was alive (the lean flavour
2175    /// assumes single-writer; this primitive is the escape hatch when
2176    /// that assumption breaks). On the happy path the diff is empty.
2177    ///
2178    /// Drift detection (whether disk *did* change) is not part of this
2179    /// surface — callers that want to short-circuit on "nothing
2180    /// changed" must compare `added.is_empty() && changed.is_empty()
2181    /// && removed.is_empty()` against the result. Backend-specific
2182    /// drift signals (git HEAD comparison, mtime check) live in the
2183    /// full-flavour engine where they have meaning.
2184    ///
2185    /// Invalidates community + search-index memos on success.
2186    /// Load every DEFERRED (lazy, not-yet-loaded) mem matching `mem`
2187    /// (`None` = all) into the store — the first-read trigger of the
2188    /// lazy-mount lifecycle. Runs before the staleness probes on every
2189    /// operation ([`Self::reload_if_stale`] calls it first), so an
2190    /// operation scoped to one mem loads exactly that mem, and a
2191    /// workspace-scoped operation (search, overview, health) loads
2192    /// whatever it needs to answer over a complete store — a count
2193    /// computed over a partial store is never presented as truth.
2194    ///
2195    /// Loading rides the ordinary per-mem reload (entity walk, store
2196    /// push, the workspace-global validation passes, memo
2197    /// invalidation), so a lazily-loaded mem gets the same refusals and
2198    /// warnings an eager boot produces for the same content — deferral
2199    /// changes WHEN the gauntlet runs, never whether. After each load,
2200    /// pending `SuspiciousNestedPrefix` warnings whose target arrived
2201    /// with this mem are dropped — the same legitimate-cross-mem
2202    /// exemption the eager boot applies once every mount is loaded.
2203    ///
2204    /// A deferred load that FAILS quarantines the mem with the same
2205    /// typed reporting an eager boot failure produces, at this first
2206    /// read: the mount leaves the serving roster, the quarantine roster
2207    /// gains the typed reason, and the existing reattach contract
2208    /// applies. Deferral never converts a load failure into an
2209    /// empty-mem impression.
2210    ///
2211    /// No-op for workspaces without lazy mounts, for already-loaded
2212    /// mems, and for a filter naming no deferred mem.
2213    pub fn ensure_mems_loaded(&mut self, mem: Option<&str>) {
2214        let pending: Vec<String> = self
2215            .mounts
2216            .iter()
2217            .filter(|m| m.deferred && mem.is_none_or(|v| m.mount.mem == v))
2218            .map(|m| m.mount.mem.clone())
2219            .collect();
2220        for name in pending {
2221            match self.reload_one_mem(&name) {
2222                Ok(_) => {
2223                    if let Some(state) = self.mounts.iter_mut().find(|m| m.mount.mem == name) {
2224                        state.deferred = false;
2225                    }
2226                    // Cross-mem links INTO this mem were scanned by the
2227                    // nested-prefix detector against a store that did
2228                    // not yet carry it; now that its entities exist,
2229                    // drop any hit the arrival resolves (mirrors the
2230                    // boot-time retain over the complete store).
2231                    let store = &self.store;
2232                    self.load_warnings.retain(|w| match w {
2233                        crate::ops::WarningHint::SuspiciousNestedPrefix { resolved_id, .. } => {
2234                            store.get(resolved_id).is_none_or(|e| e.stub)
2235                        }
2236                        _ => true,
2237                    });
2238                }
2239                Err(e) => {
2240                    // Quarantine at the moment of first read — mirror
2241                    // the eager boot failure path byte-for-byte in
2242                    // consequence: out of the serving roster, onto the
2243                    // quarantine roster with the typed reason.
2244                    let Some(idx) = self.mounts.iter().position(|m| m.mount.mem == name) else {
2245                        continue;
2246                    };
2247                    let removed = self.mounts.remove(idx);
2248                    self.schemas_remove(&name);
2249                    self.quarantined.push(crate::engine::QuarantinedMem {
2250                        mount: removed.mount,
2251                        reason_code: e.code().to_string(),
2252                        reason_message: e.to_string(),
2253                    });
2254                    self.mem_router = std::sync::Arc::new(
2255                        crate::engine::boot::build_mem_router_from_mounts(&self.mounts),
2256                    );
2257                    self.invalidate_communities();
2258                    // The schemas epoch just moved (`schemas_remove`),
2259                    // so a filled search memo is stale-keyed — clear it
2260                    // here or the next search trips the memo-key
2261                    // assert (the first W8/01 grade demonstrated
2262                    // exactly that on this branch).
2263                    self.invalidate_search_indexes();
2264                }
2265            }
2266        }
2267    }
2268
2269    pub fn reload_one_mem(&mut self, mem: &str) -> Result<crate::ops::ReloadResult, EngineError> {
2270        // Per-mem reload refreshes THIS mem's slice of the engine-wide
2271        // `load_warnings` accumulator: stale boot-time warnings for the
2272        // mem drop, fresh re-parse warnings take their place, other
2273        // mems' entries stay untouched. (The earlier "intentionally
2274        // silent" contract let a reload heal drift on disk while
2275        // `health()` kept reporting the healed warning forever — the
2276        // same class of stale-state lie the mem-delete purge closes.)
2277        //
2278        // The sink is filtered by source-mem attribution before it
2279        // merges: `validate_loaded_relations` scans the whole store, so
2280        // in principle the sink can carry other mems' warnings. In the
2281        // common case those mems' invalid rows were already dropped
2282        // from the in-memory store at their own load, so the filter is
2283        // a no-op guard against cross-mem duplicates, not a routine
2284        // trim. Failure leaves the accumulator untouched (`?` fires
2285        // before the merge), matching the inner fn's no-mutation-on-
2286        // failed-read fence. Drift events still surface as
2287        // `MemReloaded` warnings via `reload_if_stale`.
2288        // A quarantined mem's reload is the way back into service:
2289        // re-attempt the whole attach (backend, schema resolution,
2290        // entity load). On success the roster entry disappears; on
2291        // failure the mem stays quarantined with a refreshed reason.
2292        if self.quarantine_reason(mem).is_some() {
2293            return self.reattach_quarantined_mem(mem);
2294        }
2295        let mut sink: Vec<WarningHint> = Vec::new();
2296        let result = self.reload_one_mem_inner(mem, &mut sink)?;
2297        self.load_warnings.retain(|w| w.source_mem() != Some(mem));
2298        self.load_warnings
2299            .extend(sink.into_iter().filter(|w| w.source_mem() == Some(mem)));
2300        Ok(result)
2301    }
2302
2303    /// The quarantine branch of [`Self::set_mem_schema`]: repin the
2304    /// retained mount (target must resolve — the same booted resolver;
2305    /// repair never force-writes a pin that resolves nowhere), bump
2306    /// the backend config through the shared value-level writer,
2307    /// persist the mount state, then re-attempt the attach. A reattach
2308    /// that still fails (some second cause) leaves the mem quarantined
2309    /// with its refreshed reason — the pin switch itself is durable
2310    /// either way. The booted path's conformance gate cannot run over
2311    /// an unloaded mem; findings surface on the post-reattach health.
2312    fn set_schema_on_quarantined(
2313        &mut self,
2314        mem: &str,
2315        target: &memstead_schema::SchemaRef,
2316    ) -> Result<crate::engine::SetSchemaOutcome, EngineError> {
2317        use crate::engine::{SetSchemaOutcome, SetSchemaResult};
2318        // Same target-ref validation as the ordinary branch.
2319        if self.resolve_schema_by_ref(target).is_none() {
2320            let consulted: Vec<_> = self
2321                .workspace_schemas
2322                .iter()
2323                .chain(self.builtin_schemas.iter())
2324                .cloned()
2325                .collect();
2326            return Err(EngineError::SchemaNotFound {
2327                mem: mem.to_string(),
2328                pin: target.as_display(),
2329                sources: crate::engine::error::SchemaSourceDiagnostic::for_failed_pin(
2330                    &target.name,
2331                    &target.version,
2332                    &consulted,
2333                ),
2334                install_hint: None,
2335            }
2336            .with_schema_install_probe(self.workspace_root()));
2337        }
2338        let Some(q_idx) = self.quarantined.iter().position(|q| q.mount.mem == mem) else {
2339            return Err(self.unknown_mem_error(mem));
2340        };
2341        // Repin the retained mount and, where a backend can be
2342        // instantiated, the authoritative backend config (shared
2343        // value-level bump — same writer as the ordinary branch).
2344        self.quarantined[q_idx].mount.schema = Some(target.clone());
2345        self.quarantined[q_idx].mount.migration_target = None;
2346        if let Ok(backend) = (self.backend_factory)(&self.quarantined[q_idx].mount) {
2347            let _ = bump_backend_schema_pin(backend.as_ref(), target);
2348        }
2349        self.persist_state()?;
2350        // Re-attempt the attach. Failure keeps the quarantine (fresh
2351        // reason on the roster) but the pin switch stands — the
2352        // outcome reports the switch, the roster reports any
2353        // remaining cause.
2354        let _ = self.reattach_quarantined_mem(mem);
2355        Ok(SetSchemaOutcome {
2356            mem: mem.to_string(),
2357            schema_pin: target.as_display(),
2358            migration_target: None,
2359            outcome: SetSchemaResult::Switched,
2360            findings: Vec::new(),
2361        })
2362    }
2363
2364    /// Re-attempt the boot-time attach of a quarantined mem —
2365    /// backend instantiation, schema resolution (same resolver and
2366    /// catalogue layering as boot: workspace-authored schemas over
2367    /// built-ins), then a per-mem entity load. Success removes the
2368    /// roster entry and the mem serves again in the same process;
2369    /// any failure keeps (re-)quarantining with the fresh typed
2370    /// reason, so the roster never goes stale against the live state.
2371    fn reattach_quarantined_mem(
2372        &mut self,
2373        mem: &str,
2374    ) -> Result<crate::ops::ReloadResult, EngineError> {
2375        let Some(q_idx) = self.quarantined.iter().position(|q| q.mount.mem == mem) else {
2376            return Err(self.unknown_mem_error(mem));
2377        };
2378        let mount = self.quarantined[q_idx].mount.clone();
2379
2380        let requarantine = |this: &mut Self, e: &EngineError| {
2381            this.quarantined[q_idx].reason_code = e.code().to_string();
2382            this.quarantined[q_idx].reason_message = e.to_string();
2383        };
2384
2385        let backend = match (self.backend_factory)(&mount) {
2386            Ok(b) => b,
2387            Err(e) => {
2388                let err = EngineError::Mem(e.to_string());
2389                requarantine(self, &err);
2390                return Err(self.unknown_mem_error(mem));
2391            }
2392        };
2393
2394        // Same config / pin-authority reads as the boot loop.
2395        let last_known_head = backend.current_head().ok().flatten();
2396        let mem_config = backend.read_mem_config().ok().flatten().and_then(|bytes| {
2397            let value: serde_json::Value = serde_json::from_slice(&bytes).ok()?;
2398            memstead_schema::config::parse_mem_config(&value).ok()
2399        });
2400        let archive_provenance = backend
2401            .read_archive_provenance()
2402            .ok()
2403            .flatten()
2404            .and_then(|bytes| memstead_schema::ArchiveProvenance::from_archive_bytes(&bytes).ok());
2405        let config_pin = mem_config.as_ref().and_then(|c| c.schema.clone());
2406        let effective_pin = mount
2407            .migration_target
2408            .clone()
2409            .or(config_pin)
2410            .or(mount.schema.clone());
2411        let Some(effective_pin) = effective_pin else {
2412            let err = EngineError::MemConfigIncomplete {
2413                mem: mem.to_string(),
2414                missing_fields: vec!["schema".to_string()],
2415            };
2416            requarantine(self, &err);
2417            return Err(self.unknown_mem_error(mem));
2418        };
2419        let catalogue: Vec<std::sync::Arc<memstead_schema::Schema>> = self
2420            .workspace_schemas
2421            .iter()
2422            .chain(self.builtin_schemas.iter())
2423            .cloned()
2424            .collect();
2425        let schema = match crate::engine::SchemaResolver::new(&catalogue).resolve(&effective_pin) {
2426            Ok(s) => s,
2427            Err(sources) => {
2428                let err = EngineError::SchemaNotFound {
2429                    mem: mem.to_string(),
2430                    pin: effective_pin.as_display(),
2431                    sources,
2432                    install_hint: None,
2433                }
2434                .with_schema_install_probe(self.workspace_root());
2435                requarantine(self, &err);
2436                return Err(self.unknown_mem_error(mem));
2437            }
2438        };
2439
2440        // Attach, then load entities through the ordinary per-mem
2441        // reload. An entity-load failure re-quarantines (the mount is
2442        // detached again) — quarantine is not tolerance.
2443        self.quarantined.remove(q_idx);
2444        self.schemas_insert(mem.to_string(), schema);
2445        self.mounts.push(crate::engine::MountedBackend {
2446            mount,
2447            backend,
2448            last_known_head,
2449            mem_config,
2450            archive_provenance,
2451            // The reattach loads entities immediately below — a
2452            // quarantine return-to-service is never deferred.
2453            deferred: false,
2454        });
2455        self.mem_router = std::sync::Arc::new(crate::engine::boot::build_mem_router_from_mounts(
2456            &self.mounts,
2457        ));
2458        let mut sink: Vec<WarningHint> = Vec::new();
2459        match self.reload_one_mem_inner(mem, &mut sink) {
2460            Ok(result) => {
2461                self.load_warnings.retain(|w| w.source_mem() != Some(mem));
2462                self.load_warnings
2463                    .extend(sink.into_iter().filter(|w| w.source_mem() == Some(mem)));
2464                self.invalidate_communities();
2465                Ok(result)
2466            }
2467            Err(e) => {
2468                let mount_idx = self.mounts.len() - 1;
2469                let mounted = self.mounts.remove(mount_idx);
2470                self.schemas_remove(mem);
2471                self.mem_router = std::sync::Arc::new(
2472                    crate::engine::boot::build_mem_router_from_mounts(&self.mounts),
2473                );
2474                self.quarantined.push(crate::engine::QuarantinedMem {
2475                    mount: mounted.mount,
2476                    reason_code: e.code().to_string(),
2477                    reason_message: e.to_string(),
2478                });
2479                // Same epoch-moved staleness as the deferred-load
2480                // quarantine branch: `schemas_remove` bumped the
2481                // epoch, so both memos must clear.
2482                self.invalidate_communities();
2483                self.invalidate_search_indexes();
2484                Err(e)
2485            }
2486        }
2487    }
2488
2489    /// Inner per-mem body shared by [`Self::reload_one_mem`]
2490    /// and [`Self::reload_each_writable_mem`]. The caller passes
2491    /// a warning sink so the workspace-wide reload can forward
2492    /// warnings into `self.load_warnings` while the single-mem
2493    /// path keeps the accumulator pristine.
2494    fn reload_one_mem_inner(
2495        &mut self,
2496        mem: &str,
2497        warnings_sink: &mut Vec<WarningHint>,
2498    ) -> Result<crate::ops::ReloadResult, EngineError> {
2499        // Locate the target mount + schema. Unknown mem short-
2500        // circuits before any store mutation.
2501        let mount_idx = self
2502            .mounts
2503            .iter()
2504            .position(|m| m.mount.mem == mem)
2505            .ok_or_else(|| self.unknown_mem_error(mem))?;
2506        let schema = self
2507            .schemas
2508            .get(mem)
2509            .cloned()
2510            .ok_or_else(|| self.unknown_mem_error(mem))?;
2511
2512        // Snapshot pre-reload (id, content_hash) for this mem.
2513        let pre: HashMap<EntityId, String> = self
2514            .store
2515            .all_entities()
2516            .filter(|e| !e.stub && e.mem == mem)
2517            .map(|e| (e.id.clone(), e.content_hash.clone()))
2518            .collect();
2519        let pre_ids: std::collections::HashSet<EntityId> = pre.keys().cloned().collect();
2520
2521        // Walk the backend; surface read-time errors instead of
2522        // mutating the store on a failed reload.
2523        let backend = self.mounts[mount_idx].backend.as_ref();
2524        let (entries, read_errors) = collect_source_entries(backend)?;
2525        // The unbacked-mount probe rides the reload like the other
2526        // load-time warnings: a branch that appeared (or vanished) since
2527        // boot changes the answer, and the sink's per-mem replace below
2528        // drops the boot-time one.
2529        let unbacked = super::boot::unbacked_mount_warning(
2530            &self.mounts[mount_idx].mount,
2531            backend,
2532            Some(entries.len()),
2533        );
2534        let load_result = parse_entries(entries, read_errors, mem, schema.as_ref());
2535
2536        // Build the LoadCollector inputs — mem roster + last-
2537        // segment suffixes — so the parser pipeline can emit
2538        // typed drift warnings into the caller's sink.
2539        let mem_names: Vec<String> = self.mounts.iter().map(|m| m.mount.mem.clone()).collect();
2540        let known_suffixes: Vec<String> = mem_names
2541            .iter()
2542            .map(|n| crate::entity::store_builder::last_segment_suffix(n).to_string())
2543            .collect();
2544
2545        // Failure fence above; below this point the store is mutated.
2546        self.store.remove_entities_by_mem(mem);
2547        if let Some(w) = unbacked {
2548            warnings_sink.push(w);
2549        }
2550        let fallback = engine_fallback_type();
2551        push_entities_into_store(
2552            &mut self.store,
2553            load_result.entities,
2554            fallback.as_ref(),
2555            Some(crate::entity::store_builder::LoadCollector {
2556                warnings: warnings_sink,
2557                known_suffixes: &known_suffixes,
2558                mem_names: &mem_names,
2559            }),
2560        );
2561        // Re-run parse-time relation validation across the workspace.
2562        // A reload re-parses one mem but the validator's cycle pass
2563        // is global (acyclic-rel-type subgraphs span mems), so the
2564        // scan runs against the whole store. Hand-edits arriving via
2565        // sibling-writer commits get the same gauntlet boot enforces
2566        // (grammar / unknown_rel_type / shape / cycle).
2567        let mount_caps: std::collections::HashMap<String, crate::workspace::MountCapability> = self
2568            .mounts
2569            .iter()
2570            .map(|m| (m.mount.mem.clone(), m.mount.capability))
2571            .collect();
2572        // Restore cross-mem edges that point INTO this mem. The
2573        // removal cascade above dropped their incoming mirrors and the
2574        // re-push only rebuilt edges authored by this mem's own
2575        // entities, so a cross-mem `A→B` would silently vanish from the
2576        // index until a workspace-wide reload. Reconstruct from the
2577        // authoritative source records (in-memory only — no other mem is
2578        // re-read), then let the remap pass below reclassify alias sources.
2579        crate::entity::store_builder::reconstruct_incoming_cross_mem_edges(&mut self.store, mem);
2580        crate::entity::store_builder::validate_loaded_relations(
2581            &mut self.store,
2582            &self.schemas,
2583            &mount_caps,
2584            warnings_sink,
2585        );
2586        crate::entity::store_builder::remap_alias_target_edge_sources(
2587            &mut self.store,
2588            &self.schemas,
2589        );
2590        // Surface load errors back through the engine's accumulator
2591        // so subsequent `load_errors()` calls reflect the latest read.
2592        // THIS mem's stale entries are replaced, not accumulated — a
2593        // repaired file (e.g. a resolved merge conflict) must stop
2594        // reporting its old refusal, and a re-read of a still-broken
2595        // file must not duplicate its entry. Other mems' entries are
2596        // untouched (their reloads own them). Folder mounts key their
2597        // entries by absolute source path under the mem root; other
2598        // backends have no path-attributable entries to replace.
2599        if let crate::workspace::MountStorage::Folder { path } =
2600            &self.mounts[mount_idx].mount.storage
2601        {
2602            let root = path.clone();
2603            self.load_errors.retain(|(p, _)| !p.starts_with(&root));
2604            // The backend walk yields mem-relative paths while the boot
2605            // loader yields absolute ones — normalize to absolute so the
2606            // replace-on-reload key stays uniform across both origins.
2607            self.load_errors
2608                .extend(load_result.errors.into_iter().map(|(p, m)| {
2609                    let abs = if p.is_relative() { root.join(&p) } else { p };
2610                    (abs, m)
2611                }));
2612        } else {
2613            self.load_errors.extend(load_result.errors);
2614        }
2615
2616        // Refresh the mem's config from the backend too (D13). `sync_state`
2617        // (the projection baselines) and the schema pin / write guidance are
2618        // mem-scoped state that rides the mem branch, so an out-of-band write
2619        // — a sibling `projection advance` / `mem set-sync-state` — must become
2620        // visible after a per-mem reload, not only entity changes. A missing or
2621        // unparseable config leaves the cached value untouched (best-effort:
2622        // the reload never fails on a config read hiccup).
2623        if let Ok(Some(bytes)) = self.mounts[mount_idx].backend.read_mem_config()
2624            && let Ok(value) = serde_json::from_slice::<serde_json::Value>(&bytes)
2625            && let Ok(cfg) = memstead_schema::config::parse_mem_config(&value)
2626        {
2627            self.mounts[mount_idx].mem_config = Some(cfg);
2628        }
2629
2630        // Diff post-reload against the snapshot.
2631        let mut added: Vec<EntityId> = Vec::new();
2632        let mut changed: Vec<EntityId> = Vec::new();
2633        for entity in self.store.all_entities() {
2634            if entity.stub || entity.mem != mem {
2635                continue;
2636            }
2637            match pre.get(&entity.id) {
2638                None => added.push(entity.id.clone()),
2639                Some(prev_hash) if prev_hash != &entity.content_hash => {
2640                    changed.push(entity.id.clone());
2641                }
2642                Some(_) => {}
2643            }
2644        }
2645        let post_ids: std::collections::HashSet<EntityId> = self
2646            .store
2647            .all_entities()
2648            .filter(|e| !e.stub && e.mem == mem)
2649            .map(|e| e.id.clone())
2650            .collect();
2651        let mut removed: Vec<EntityId> = pre_ids.difference(&post_ids).cloned().collect();
2652        added.sort_by(|a, b| a.0.cmp(&b.0));
2653        changed.sort_by(|a, b| a.0.cmp(&b.0));
2654        removed.sort_by(|a, b| a.0.cmp(&b.0));
2655
2656        self.invalidate_communities();
2657        self.invalidate_search_indexes();
2658
2659        Ok(crate::ops::ReloadResult {
2660            added,
2661            changed,
2662            removed,
2663        })
2664    }
2665
2666    /// Rich-shape variant of [`Self::reload_one_mem`] that returns a
2667    /// [`crate::ops::ReloadReport`] (mem + head_before + head_after +
2668    /// entities_loaded + changed_entity_ids) instead of the slim
2669    /// [`crate::ops::ReloadResult`]. Handler-facing wrapper consumed
2670    /// by the `memstead_reload` MCP tool — the rich shape is the wire
2671    /// contract MCP callers depend on; the slim form stays for
2672    /// programmatic consumers that just want the diff lists.
2673    ///
2674    /// `head_before` is the engine's **prior cursor** for this mem
2675    /// (its cached `last_known_head`), *not* the current on-disk tip:
2676    /// when a sibling has committed since, the tip has already advanced,
2677    /// so reporting it would make the advertised
2678    /// `changes_since(since=head_before)` recipe span an empty range.
2679    /// `head_after` is the freshly-peeled tip from
2680    /// [`crate::backend::MemBackend::current_head`]; the reload also
2681    /// advances the cursor to it, so a follow-up staleness probe does
2682    /// not re-reload the same window. Backends without history (folder,
2683    /// archive) carry no cursor and return `Ok(None)`; both fields fall
2684    /// back to [`crate::ops::EMPTY_TREE_SHA`] for wire-shape stability.
2685    ///
2686    /// `entities_loaded` is the post-reload non-stub count for the
2687    /// mem — same semantic as full's report.
2688    ///
2689    /// `changed_entity_ids` is the union of `added ∪ changed ∪
2690    /// removed` from the underlying [`crate::ops::ReloadResult`]
2691    /// so callers don't have to merge three lists themselves —
2692    /// matches full's bundled wire shape.
2693    pub fn reload_one_mem_report(
2694        &mut self,
2695        mem: &str,
2696    ) -> Result<crate::ops::ReloadReport, EngineError> {
2697        // `head_before` is the engine's PRIOR cursor — the SHA it last
2698        // knew for this mem — not the current (possibly already
2699        // drifted) on-disk tip. Reporting the tip would collapse the
2700        // `changes_since(since=head_before)` range to empty in exactly
2701        // the sibling-drift case the recipe targets. `current_head` is
2702        // backend-defined: git-branch serves a git cursor, the folder
2703        // backend serves its change-ledger watermark, and a backend
2704        // without a head signal returns None — for those, `head_before`
2705        // stays the empty-tree sentinel that pairs with the
2706        // equally-empty `head_after` below.
2707        let tracks_head = self
2708            .mounts
2709            .iter()
2710            .find(|m| m.mount.mem == mem)
2711            .and_then(|m| m.backend.current_head().ok().flatten())
2712            .is_some();
2713        let head_before = if tracks_head {
2714            self.mounts
2715                .iter()
2716                .find(|m| m.mount.mem == mem)
2717                .and_then(|m| m.last_known_head.clone())
2718                .unwrap_or_else(|| crate::ops::EMPTY_TREE_SHA.to_string())
2719        } else {
2720            crate::ops::EMPTY_TREE_SHA.to_string()
2721        };
2722
2723        let result = self.reload_one_mem(mem)?;
2724
2725        // Capture head_after = the freshly-peeled tip, and advance the
2726        // engine's cursor to it. Without this advance the next
2727        // operation's `reload_if_stale` would compare the stale cursor
2728        // against the same tip and re-reload the identical window,
2729        // re-emitting a spurious `MEM_RELOADED`. Only history-backed
2730        // mounts (current_head → Some) carry a cursor to advance.
2731        let head_after_raw = self
2732            .mounts
2733            .iter()
2734            .find(|m| m.mount.mem == mem)
2735            .and_then(|m| m.backend.current_head().ok().flatten());
2736        if let Some(new_head) = head_after_raw.clone()
2737            && let Some(m) = self.mounts.iter_mut().find(|m| m.mount.mem == mem)
2738        {
2739            m.last_known_head = Some(new_head);
2740        }
2741        let head_after = head_after_raw.unwrap_or_else(|| crate::ops::EMPTY_TREE_SHA.to_string());
2742
2743        let entities_loaded = self
2744            .store
2745            .all_entities()
2746            .filter(|e| !e.stub && e.mem == mem)
2747            .count();
2748
2749        // Union of added + changed + removed, sorted lexicographically
2750        // for deterministic wire output. Matches full's "single
2751        // changed_entity_ids list" contract — saves callers from
2752        // merging three slices themselves.
2753        let mut changed_entity_ids: Vec<EntityId> = result
2754            .added
2755            .into_iter()
2756            .chain(result.changed)
2757            .chain(result.removed)
2758            .collect();
2759        changed_entity_ids.sort_by(|a, b| a.0.cmp(&b.0));
2760
2761        Ok(crate::ops::ReloadReport {
2762            mem: mem.to_string(),
2763            head_before,
2764            head_after,
2765            entities_loaded,
2766            changed_entity_ids,
2767        })
2768    }
2769
2770    /// Batched rich-shape variant — returns one
2771    /// [`crate::ops::ReloadReport`] per mounted mem in declaration
2772    /// order. Counterpart to [`Self::reload_each_writable_mem`]
2773    /// (slim) that the `memstead_reload` MCP tool's no-mem path
2774    /// consumes.
2775    ///
2776    /// `load_warnings` semantics ride on the per-mem contract: each
2777    /// [`Self::reload_one_mem`] in the sweep refreshes its own mem's
2778    /// slice of the engine-wide accumulator, so a full sweep leaves
2779    /// the accumulator equivalent to a fresh boot. (Earlier this
2780    /// variant discarded every reload warning while its slim
2781    /// counterpart repopulated — the MCP workspace-wide reload could
2782    /// never clear a stale warning.) On first-error-abort, mems
2783    /// reloaded before the failure carry refreshed slices and the
2784    /// rest keep their boot-time entries — no slice is lost.
2785    ///
2786    /// Also re-reads `.memstead/workspace.toml` and refreshes
2787    /// [`crate::workspace::WorkspaceSettings`] before sweeping the
2788    /// mems — this is the pairing with the CLI's
2789    /// `memstead workspace allow-create / grant-cross-link / set-mutations`
2790    /// family. Without this re-read, a CLI write would land on disk but
2791    /// the running MCP would still serve the engine's boot-time policy
2792    /// snapshot; every subsequent `memstead_mem_create` against the new
2793    /// allowlist would fail with `MEM_PATH_NOT_ALLOWED` until process
2794    /// restart. The workspace-wide form runs the heavier path; the
2795    /// per-mem form (`reload_one_mem_report`) intentionally skips
2796    /// the workspace re-read — content drift doesn't imply policy
2797    /// drift.
2798    ///
2799    /// Reload of `workspace.toml` is best-effort: a missing or
2800    /// unparseable file leaves the existing settings untouched. The
2801    /// per-mem sweep is the primary contract — settings refresh is
2802    /// the additive bonus.
2803    ///
2804    /// First-error-aborts: if any mem's reload fails, the loop
2805    /// stops and the error propagates. Mems reloaded before the
2806    /// failing one are already mutated in the store; the returned
2807    /// error has no rollback. Operators run the per-mem form to
2808    /// retry the failing mem explicitly.
2809    pub fn reload_each_writable_mem_reports(
2810        &mut self,
2811    ) -> Result<Vec<crate::ops::ReloadReport>, EngineError> {
2812        self.refresh_workspace_settings_if_possible();
2813        let names: Vec<String> = self.mounts.iter().map(|m| m.mount.mem.clone()).collect();
2814        let mut out = Vec::with_capacity(names.len());
2815        for name in names {
2816            let report = self.reload_one_mem_report(&name)?;
2817            out.push(report);
2818        }
2819        Ok(out)
2820    }
2821
2822    /// Best-effort refresh of [`crate::workspace::WorkspaceSettings`]
2823    /// from the workspace's `.memstead/workspace.toml`. Called by the
2824    /// workspace-wide reload sweep so CLI-driven policy edits become
2825    /// visible to a live engine without process restart.
2826    ///
2827    /// Silent no-op when the engine has no `workspace_root` (legacy
2828    /// in-memory constructions) or when the on-disk file is missing /
2829    /// unparseable. The per-mem reload contract stays the canonical
2830    /// failure surface; settings refresh failures are intentionally
2831    /// non-fatal so a malformed workspace.toml doesn't break content
2832    /// drift detection.
2833    fn refresh_workspace_settings_if_possible(&mut self) {
2834        let Some(root) = self.workspace_root.clone() else {
2835            return;
2836        };
2837        let store = crate::workspace_store::FileWorkspaceStore::new();
2838        let workspace = match crate::workspace_store::WorkspaceStoreAdapter::load(&store, &root) {
2839            Ok(w) => w,
2840            Err(_) => return,
2841        };
2842        self.set_settings(workspace.settings);
2843    }
2844
2845    /// Reload every mounted mem in declaration order; returns one
2846    /// `(mem, ReloadResult)` per mount.
2847    ///
2848    /// Failure model is **first-error-aborts**: if any mem's reload
2849    /// fails, the loop stops and the error propagates. Mems reloaded
2850    /// before the failing one are already mutated in the store; the
2851    /// returned error has no rollback. Operators run the per-mem
2852    /// form to retry the failing mem explicitly.
2853    ///
2854    /// Caller-friendly batching wrapper around [`Self::reload_one_mem`];
2855    /// internal cache invalidation happens once per mem (the inner
2856    /// call invalidates) so an N-mem batch invalidates the memos
2857    /// N times. That's wasteful for large workspaces; once the
2858    /// `memstead_reload` MCP handler migrates we can tighten this to one
2859    /// invalidation at the end.
2860    pub fn reload_each_writable_mem(
2861        &mut self,
2862    ) -> Result<Vec<(String, crate::ops::ReloadResult)>, EngineError> {
2863        let names: Vec<String> = self.mounts.iter().map(|m| m.mount.mem.clone()).collect();
2864        // Workspace-wide reload semantics: take the engine-wide
2865        // sink, clear it, route per-mem inner reloads through it,
2866        // put it back. The result is `self.load_warnings` carries
2867        // every typed drift warning the reload sweep produced (so
2868        // the next `engine.health()` call surfaces them).
2869        let mut sink = std::mem::take(&mut self.load_warnings);
2870        sink.clear();
2871        let mut out = Vec::with_capacity(names.len());
2872        let mut loop_err = None;
2873        for name in names {
2874            match self.reload_one_mem_inner(&name, &mut sink) {
2875                Ok(result) => out.push((name, result)),
2876                Err(e) => {
2877                    loop_err = Some(e);
2878                    break;
2879                }
2880            }
2881        }
2882        self.load_warnings = sink;
2883        if let Some(e) = loop_err {
2884            return Err(e);
2885        }
2886        Ok(out)
2887    }
2888}
2889
2890/// Value-level schema-pin bump on a backend's mem config: read the
2891/// config blob, rewrite ONLY the `"schema"` string, write it back —
2892/// every other field (`readMems`, write guidance, sync state, …) is
2893/// preserved verbatim. Config-absent backends (no `config.json`) are a
2894/// clean no-op returning `None`; the caller's `Mount.schema` then
2895/// stays the settled pin. Returns the updated JSON value on a write so
2896/// callers can refresh caches.
2897///
2898/// One shared implementation for the booted path
2899/// (`Engine::persist_mem_schema_pin`) and the below-boot repair path
2900/// (memstead-git-branch) — the two must never fork: a pin written by
2901/// repair must be byte-shaped exactly as one written by the engine.
2902/// Field names on which the stored config differs from `cached`.
2903///
2904/// Compared as JSON so the comparison covers every field the struct models
2905/// without a hand-written field list that a new field would silently escape.
2906/// A parse failure yields no fields rather than a false report: the write
2907/// itself will surface the malformed config.
2908fn changed_config_fields(
2909    cached: &memstead_schema::config::MemConfig,
2910    stored_bytes: &[u8],
2911) -> Vec<String> {
2912    let (Ok(a), Ok(b)) = (
2913        serde_json::to_value(cached),
2914        serde_json::from_slice::<serde_json::Value>(stored_bytes),
2915    ) else {
2916        return Vec::new();
2917    };
2918    let (Some(a), Some(b)) = (a.as_object(), b.as_object()) else {
2919        return Vec::new();
2920    };
2921    let mut keys: std::collections::BTreeSet<&String> = a.keys().collect();
2922    keys.extend(b.keys());
2923    keys.into_iter()
2924        .filter(|k| a.get(*k) != b.get(*k))
2925        .map(|k| k.to_string())
2926        .collect()
2927}
2928
2929pub fn bump_backend_schema_pin(
2930    backend: &dyn crate::backend::MemBackend,
2931    target: &memstead_schema::SchemaRef,
2932) -> Result<Option<serde_json::Value>, EngineError> {
2933    let Some(bytes) = backend
2934        .read_mem_config()
2935        .map_err(|e| EngineError::Mem(format!("read mem config for pin update: {e}")))?
2936    else {
2937        return Ok(None);
2938    };
2939    let mut value: serde_json::Value = serde_json::from_slice(&bytes)
2940        .map_err(|e| EngineError::Mem(format!("parse mem config for pin update: {e}")))?;
2941    value["schema"] = serde_json::Value::String(target.as_display());
2942    let new_bytes = serde_json::to_vec_pretty(&value)
2943        .map_err(|e| EngineError::Mem(format!("serialize mem config for pin update: {e}")))?;
2944    backend
2945        .write_mem_config(&new_bytes)
2946        .map_err(|e| EngineError::Mem(format!("write mem config for pin update: {e}")))?;
2947    Ok(Some(value))
2948}
2949
2950/// Three-way merge of a mount roster for a state write.
2951///
2952/// `baseline` is what the writing engine last read or wrote, `ours` is
2953/// its roster now, `on_disk` is what the file holds at this instant.
2954/// The result keeps every on-disk mount the writer did not touch (so a
2955/// sibling's registration survives), drops the ones the writer removed
2956/// since its baseline, and applies the ones it added or changed.
2957///
2958/// A mount present in `ours` unchanged since the baseline does NOT
2959/// overwrite the on-disk record of the same name: if a sibling edited
2960/// it and we did not, the sibling's edit is the newer statement about
2961/// it, and republishing our stale copy is exactly the loss this merge
2962/// exists to prevent.
2963fn merge_mount_rosters(
2964    baseline: &[crate::workspace::Mount],
2965    ours: &[crate::workspace::Mount],
2966    on_disk: Vec<crate::workspace::Mount>,
2967) -> Vec<crate::workspace::Mount> {
2968    use std::collections::{HashMap, HashSet};
2969
2970    let ours_names: HashSet<&str> = ours.iter().map(|m| m.mem.as_str()).collect();
2971    let removed_by_us: HashSet<&str> = baseline
2972        .iter()
2973        .map(|m| m.mem.as_str())
2974        .filter(|n| !ours_names.contains(n))
2975        .collect();
2976    let baseline_by_name: HashMap<&str, &crate::workspace::Mount> =
2977        baseline.iter().map(|m| (m.mem.as_str(), m)).collect();
2978
2979    let mut merged: Vec<crate::workspace::Mount> = on_disk
2980        .into_iter()
2981        .filter(|m| !removed_by_us.contains(m.mem.as_str()))
2982        .collect();
2983
2984    for mount in ours {
2985        let untouched_by_us = baseline_by_name
2986            .get(mount.mem.as_str())
2987            .is_some_and(|b| *b == mount);
2988        match merged.iter_mut().find(|d| d.mem == mount.mem) {
2989            Some(slot) => {
2990                if !untouched_by_us {
2991                    *slot = mount.clone();
2992                }
2993            }
2994            None => merged.push(mount.clone()),
2995        }
2996    }
2997    merged
2998}
2999
3000#[cfg(test)]
3001mod tests {
3002
3003    use tempfile::TempDir;
3004
3005    use crate::backend::{BackendError, MemBackend};
3006    use crate::engine::test_helpers::*;
3007    use crate::engine::{Engine, EngineError};
3008    use crate::mem::MemOrigin;
3009    use crate::ops::WarningHint;
3010    use crate::storage::{ArchiveBackend, FilesystemMemWriter};
3011
3012    fn schema_package_files(heading: &str, manifest_name: &str) -> Vec<(String, Vec<u8>)> {
3013        let manifest = format!(
3014            r#"name: {manifest_name}
3015version: 1.0.0
3016description: Install-gate test schema
3017when_to_use: Tests
3018types:
3019  - sample
3020relationships:
3021  mode: strict
3022  definitions:
3023    - name: PART_OF
3024      description: hier
3025      default_weight: 3.0
3026    - name: _default
3027      description: fallback
3028      default_weight: 1.0
3029community:
3030  resolution: 1.0
3031  seed: 42
3032"#
3033        );
3034        let type_yaml = format!(
3035            r#"name: sample
3036description: t
3037when_to_use: tests
3038sections:
3039  - key: body
3040    heading: {heading}
3041    required: true
3042    search_weight: 10.0
3043    catch_all: true
3044    write_rules: []
3045metadata_fields: []
3046title_weight: 100.0
3047text_fields:
3048  - body
3049hierarchy_relationship: PART_OF
3050no_self_loop_relationships: []
3051updatable_fields:
3052  - title
3053  - body
3054health_required_fields:
3055  - body
3056staleness_threshold_days: 90
3057write_rules: []
3058"#
3059        );
3060        vec![
3061            ("schema.yaml".to_string(), manifest.into_bytes()),
3062            ("types/sample.yaml".to_string(), type_yaml.into_bytes()),
3063        ]
3064    }
3065
3066    /// The install gate accepts a conforming package and refuses one
3067    /// whose section heading cannot round-trip to its key — the last
3068    /// moment the author can act, since sealed schemas keep loading.
3069    #[test]
3070    fn install_gate_refuses_non_roundtrip_heading() {
3071        let ok =
3072            Engine::validate_schema_package("gate", "1.0.0", &schema_package_files("Body", "gate"));
3073        assert!(ok.is_ok(), "conforming package passes: {ok:?}");
3074
3075        let err = Engine::validate_schema_package(
3076            "gate",
3077            "1.0.0",
3078            &schema_package_files("Body Text", "gate"),
3079        )
3080        .expect_err("non-deriving heading must refuse install");
3081        match &err {
3082            EngineError::SchemaPackageInvalid { name, message, .. } => {
3083                assert_eq!(name, "gate");
3084                assert!(
3085                    message.contains("'body'") && message.contains("'Body Text'"),
3086                    "message names the offending tuple: {message}"
3087                );
3088            }
3089            other => panic!("expected SchemaPackageInvalid, got {other:?}"),
3090        }
3091    }
3092
3093    /// Build a package whose single type carries an exemplar assembled
3094    /// from the given pieces — the fixture for the exemplar-gate
3095    /// tests. The type declares a required `body` section, a `status`
3096    /// enum field, PART_OF (unpinned), and REFINES pinned to
3097    /// `source_types: [other]` so a REFINES exemplar edge from
3098    /// `sample` violates shape.
3099    fn exemplar_package_files(
3100        section_key: &str,
3101        status_value: &str,
3102        rel_type: &str,
3103    ) -> Vec<(String, Vec<u8>)> {
3104        let manifest = r#"name: gate
3105version: 1.0.0
3106description: exemplar gate fixture
3107when_to_use: tests
3108types:
3109  - sample
3110  - other
3111relationships:
3112  mode: strict
3113  definitions:
3114    - name: PART_OF
3115      description: hier
3116      default_weight: 3.0
3117    - name: REFINES
3118      description: pinned
3119      default_weight: 1.0
3120      source_types: [other]
3121    - name: _default
3122      description: fallback
3123      default_weight: 1.0
3124community:
3125  resolution: 1.0
3126  seed: 42
3127"#
3128        .to_string();
3129        let type_yaml = format!(
3130            r#"name: sample
3131description: t
3132when_to_use: tests
3133sections:
3134  - key: body
3135    heading: Body
3136    required: true
3137    search_weight: 10.0
3138    catch_all: true
3139    write_rules: []
3140metadata_fields:
3141  - key: status
3142    description: workflow state
3143    field_type: string
3144    enum_values: [draft, final]
3145title_weight: 100.0
3146text_fields:
3147  - body
3148hierarchy_relationship: PART_OF
3149no_self_loop_relationships: []
3150updatable_fields:
3151  - title
3152  - body
3153health_required_fields:
3154  - body
3155staleness_threshold_days: 90
3156write_rules: []
3157exemplar:
3158  title: A Conforming Sample
3159  metadata:
3160    status: "{status_value}"
3161  sections:
3162    {section_key}: "One canonical body paragraph."
3163  relations:
3164    - to: parent-placeholder
3165      type: {rel_type}
3166"#
3167        );
3168        let other_yaml = r#"name: other
3169description: shape-pin partner
3170when_to_use: tests
3171sections:
3172  - key: body
3173    heading: Body
3174    required: true
3175    search_weight: 10.0
3176    catch_all: true
3177    write_rules: []
3178metadata_fields: []
3179title_weight: 100.0
3180text_fields:
3181  - body
3182hierarchy_relationship: PART_OF
3183no_self_loop_relationships: []
3184updatable_fields:
3185  - title
3186  - body
3187health_required_fields:
3188  - body
3189staleness_threshold_days: 90
3190write_rules: []
3191"#
3192        .to_string();
3193        vec![
3194            ("schema.yaml".to_string(), manifest.into_bytes()),
3195            ("types/sample.yaml".to_string(), type_yaml.into_bytes()),
3196            ("types/other.yaml".to_string(), other_yaml.into_bytes()),
3197        ]
3198    }
3199
3200    /// The exemplar gate (agent-trust plan 09): a package whose type
3201    /// carries a CONFORMANT exemplar installs; the same package broken
3202    /// three ways — wrong section key, illegal enum value, relationship
3203    /// shape violation — refuses with a typed error naming the type
3204    /// and the defect. No warn-and-carry path exists: the refusal is
3205    /// `SchemaPackageInvalid`, same as every other install-gate class.
3206    #[test]
3207    fn install_gate_validates_exemplars_through_the_real_create_path() {
3208        // Conformant exemplar → the package installs.
3209        let ok = Engine::validate_schema_package(
3210            "gate",
3211            "1.0.0",
3212            &exemplar_package_files("body", "draft", "PART_OF"),
3213        );
3214        assert!(ok.is_ok(), "conformant exemplar passes: {ok:?}");
3215
3216        // Variant 1 — wrong section key.
3217        let err = Engine::validate_schema_package(
3218            "gate",
3219            "1.0.0",
3220            &exemplar_package_files("bogus_section", "draft", "PART_OF"),
3221        )
3222        .expect_err("wrong section key must refuse");
3223        match &err {
3224            EngineError::SchemaPackageInvalid { message, .. } => {
3225                assert!(
3226                    message.contains("'sample'") && message.contains("exemplar"),
3227                    "names type and calls out the exemplar: {message}"
3228                );
3229                assert!(
3230                    message.contains("UNKNOWN_SECTION")
3231                        || message.contains("MISSING_REQUIRED_SECTION"),
3232                    "carries the typed defect code: {message}"
3233                );
3234            }
3235            other => panic!("expected SchemaPackageInvalid, got {other:?}"),
3236        }
3237
3238        // Variant 2 — illegal enum value.
3239        let err = Engine::validate_schema_package(
3240            "gate",
3241            "1.0.0",
3242            &exemplar_package_files("body", "not-a-legal-status", "PART_OF"),
3243        )
3244        .expect_err("illegal enum value must refuse");
3245        assert!(
3246            matches!(&err, EngineError::SchemaPackageInvalid { message, .. }
3247                if message.contains("'sample'") && message.contains("INVALID_ENUM_VALUE")),
3248            "got {err:?}"
3249        );
3250
3251        // Variant 3 — relationship shape violation (REFINES is pinned
3252        // to source_types [other]; the exemplar's type is `sample`).
3253        let err = Engine::validate_schema_package(
3254            "gate",
3255            "1.0.0",
3256            &exemplar_package_files("body", "draft", "REFINES"),
3257        )
3258        .expect_err("relationship shape violation must refuse");
3259        assert!(
3260            matches!(&err, EngineError::SchemaPackageInvalid { message, .. }
3261                if message.contains("'sample'") && message.contains("INVALID_REL_SHAPE")),
3262            "got {err:?}"
3263        );
3264    }
3265
3266    /// The worked-example teaching package (`memstead-schema/examples/
3267    /// minimal`) models the exemplar practice — its exemplars validate
3268    /// through the same gate, so the material that teaches schema
3269    /// authoring can never itself teach a non-conformant shape.
3270    #[test]
3271    fn worked_example_package_exemplars_validate() {
3272        let pkg = std::path::Path::new(env!("CARGO_MANIFEST_DIR"))
3273            .join("../memstead-schema/examples/minimal");
3274        let schema = std::sync::Arc::new(
3275            memstead_schema::load_schema_from_dir(&pkg).expect("worked example loads"),
3276        );
3277        assert!(
3278            schema.types.values().all(|td| td.exemplar.is_some()),
3279            "every worked-example type models the exemplar practice"
3280        );
3281        Engine::validate_schema_exemplars(&schema).expect("worked-example exemplars conform");
3282    }
3283
3284    /// Every built-in schema's exemplars validate through the SAME
3285    /// gate the install path runs — a built-in exemplar broken by a
3286    /// future edit fails CI here. Completeness rides the same walk:
3287    /// the NEWEST version of every built-in name carries an exemplar
3288    /// on every type (older versions are sealed as shipped and may
3289    /// predate the field).
3290    #[test]
3291    fn builtin_exemplars_validate_through_the_install_gate() {
3292        let schemas = memstead_schema::builtins::load_builtin_schemas()
3293            .expect("built-in schemas always load");
3294        // Validity: every exemplar anywhere in the catalogue conforms.
3295        for schema in &schemas {
3296            if let Err(defect) = Engine::validate_schema_exemplars(schema) {
3297                let (name, version) = schema.id();
3298                panic!("built-in {name}@{version}: {defect}");
3299            }
3300        }
3301        // Completeness: the newest version per name is exemplar-complete.
3302        let mut newest: std::collections::HashMap<
3303            String,
3304            &std::sync::Arc<memstead_schema::Schema>,
3305        > = std::collections::HashMap::new();
3306        for schema in &schemas {
3307            let name = schema.manifest.name.clone();
3308            match newest.get(&name) {
3309                Some(cur) if cur.version >= schema.version => {}
3310                _ => {
3311                    newest.insert(name, schema);
3312                }
3313            }
3314        }
3315        for (name, schema) in &newest {
3316            for (type_name, td) in &schema.types {
3317                assert!(
3318                    td.exemplar.is_some(),
3319                    "built-in {name}@{} type '{type_name}' has no exemplar — the \
3320                     reference schemas model the practice completely",
3321                    schema.version
3322                );
3323            }
3324        }
3325    }
3326
3327    /// Exemplar relation targets are PLACEHOLDERS: a bare slug is
3328    /// legal (target existence is never checked — the absent target
3329    /// is the would-be-stub path), while a mem-prefixed target
3330    /// refuses with the placeholder rule named.
3331    #[test]
3332    fn exemplar_relation_targets_are_bare_placeholder_slugs() {
3333        let mut files = exemplar_package_files("body", "draft", "PART_OF");
3334        let patched = String::from_utf8(files[1].1.clone())
3335            .unwrap()
3336            .replace("to: parent-placeholder", "to: other--real-entity");
3337        files[1].1 = patched.into_bytes();
3338        let err = Engine::validate_schema_package("gate", "1.0.0", &files)
3339            .expect_err("mem-prefixed exemplar target must refuse");
3340        assert!(
3341            matches!(&err, EngineError::SchemaPackageInvalid { message, .. }
3342                if message.contains("bare") && message.contains("'sample'")),
3343            "got {err:?}"
3344        );
3345    }
3346
3347    /// A manifest whose declared identity contradicts the install ref
3348    /// is refused — the schema would otherwise seal under a ref its
3349    /// own manifest disagrees with.
3350    #[test]
3351    fn install_gate_refuses_manifest_identity_mismatch() {
3352        let err = Engine::validate_schema_package(
3353            "gate",
3354            "1.0.0",
3355            &schema_package_files("Body", "other"),
3356        )
3357        .expect_err("identity mismatch must refuse install");
3358        assert!(
3359            matches!(&err, EngineError::SchemaPackageInvalid { message, .. }
3360                if message.contains("other@1.0.0")),
3361            "got {err:?}"
3362        );
3363    }
3364
3365    #[test]
3366    fn reload_each_writable_mem_repopulates_load_warnings() {
3367        // Boot with a clean mem, then mid-flight write a file
3368        // with a duplicate heading, then call reload_each_writable_mem.
3369        // The accumulator should pick up the new typed warning.
3370        let tmp = TempDir::new().unwrap();
3371        let mem_dir = tmp.path().to_path_buf();
3372        let writer = FilesystemMemWriter::new(mem_dir.clone());
3373        // Newest default generation so the clean-boot baseline isn't
3374        // tripped by the SCHEMA_GENERATIONS_BEHIND hint.
3375        let mut mount = folder_mount("specs", mem_dir.clone());
3376        mount.schema = Some("default@1.3.0".parse().unwrap());
3377        let mut engine =
3378            Engine::from_mounts(vec![(mount, Box::new(writer) as Box<dyn MemBackend>)]).unwrap();
3379        // The only thing a clean, entity-less mount says about itself
3380        // is that it is empty (`MOUNT_UNBACKED` / `empty`).
3381        assert!(
3382            engine
3383                .load_warnings()
3384                .iter()
3385                .all(|w| w.code() == "MOUNT_UNBACKED"),
3386            "clean boot has no warnings beyond the empty-mount one: {:?}",
3387            engine.load_warnings()
3388        );
3389
3390        // Drop a markdown file with two `## Identity` headings.
3391        let body =
3392            "---\ntype: spec\n---\n# Dup\n\n## Identity\n\nfirst.\n\n## Identity\n\nsecond.\n";
3393        std::fs::write(mem_dir.join("dup.md"), body).unwrap();
3394
3395        engine.reload_each_writable_mem().unwrap();
3396        let warnings = engine.load_warnings();
3397        assert!(
3398            warnings
3399                .iter()
3400                .any(|w| matches!(w, WarningHint::DuplicateSectionHeading { .. })),
3401            "workspace-wide reload must repopulate load_warnings: {warnings:?}",
3402        );
3403    }
3404
3405    /// `validate_loaded_relations` runs on the reload path too — a
3406    /// sibling-writer commit that injects a markdown file carrying a
3407    /// schema-undeclared rel-type must surface as a typed
3408    /// `PARSED_RELATION_INVALID` warning after `reload_each_writable_mem`.
3409    /// Without the reload-path wiring this drift would slip past the
3410    /// validator (boot only catches what existed at startup).
3411    #[test]
3412    fn reload_picks_up_parse_time_relation_drift_from_sibling_writer() {
3413        let tmp = TempDir::new().unwrap();
3414        let mem_dir = tmp.path().to_path_buf();
3415        // Seed a clean target entity at boot.
3416        let target_body = "---\ntype: spec\n---\n# Target\n\n## Identity\n\nThe target.\n";
3417        std::fs::write(mem_dir.join("target.md"), target_body).unwrap();
3418        let writer = FilesystemMemWriter::new(mem_dir.clone());
3419        let mut engine = Engine::from_mounts(vec![(
3420            folder_mount("specs", mem_dir.clone()),
3421            Box::new(writer) as Box<dyn MemBackend>,
3422        )])
3423        .unwrap();
3424        // Clean boot — no parse-time relation warnings yet.
3425        assert!(
3426            !engine
3427                .load_warnings()
3428                .iter()
3429                .any(|w| matches!(w, WarningHint::ParsedRelationInvalid { .. })),
3430            "clean boot must not emit ParsedRelationInvalid; got: {:?}",
3431            engine.load_warnings()
3432        );
3433
3434        // Sibling-writer drops a new file with an unknown rel-type.
3435        let drift_body = "---\ntype: spec\n---\n# Source\n\n## Identity\n\nThe source.\n\n## Relationships\n\n- **MADE_UP_TYPE**: [[specs--target]]\n";
3436        std::fs::write(mem_dir.join("source.md"), drift_body).unwrap();
3437
3438        engine.reload_each_writable_mem().unwrap();
3439
3440        let invalid: Vec<_> = engine
3441            .load_warnings()
3442            .iter()
3443            .filter_map(|w| match w {
3444                WarningHint::ParsedRelationInvalid {
3445                    rel_type,
3446                    reason,
3447                    origin,
3448                    ..
3449                } => Some((rel_type.clone(), reason.clone(), origin.clone())),
3450                _ => None,
3451            })
3452            .collect();
3453        assert_eq!(
3454            invalid.len(),
3455            1,
3456            "reload must surface the parse-time drift, got: {invalid:?}",
3457        );
3458        assert_eq!(invalid[0].0, "MADE_UP_TYPE");
3459        assert_eq!(invalid[0].1, "unknown_rel_type");
3460        assert_eq!(invalid[0].2, "writable");
3461    }
3462
3463    #[test]
3464    fn reload_one_mem_refreshes_own_slice_and_keeps_other_mems() {
3465        // Boot two mems, each with a duplicate-heading file, so the
3466        // accumulator carries one warning per mem. Fix alpha's file on
3467        // disk, reload ONLY alpha: alpha's stale warning must drop
3468        // (reload heals drift — health() must stop reporting it) while
3469        // beta's untouched warning survives (per-mem reload never
3470        // clears other mems' slices).
3471        let tmp = TempDir::new().unwrap();
3472        let dup_body = "---\ntype: spec\n---\n# Dup\n\n## Identity\n\na.\n\n## Identity\n\nb.\n";
3473        let a_dir = tmp.path().join("a");
3474        std::fs::create_dir_all(&a_dir).unwrap();
3475        std::fs::write(a_dir.join("dup.md"), dup_body).unwrap();
3476        let b_dir = tmp.path().join("b");
3477        std::fs::create_dir_all(&b_dir).unwrap();
3478        std::fs::write(b_dir.join("dup.md"), dup_body).unwrap();
3479        let mut engine = Engine::from_mounts(vec![
3480            (
3481                folder_mount("alpha", a_dir.clone()),
3482                Box::new(FilesystemMemWriter::new(a_dir.clone())) as Box<dyn MemBackend>,
3483            ),
3484            (
3485                folder_mount("beta", b_dir.clone()),
3486                Box::new(FilesystemMemWriter::new(b_dir.clone())) as Box<dyn MemBackend>,
3487            ),
3488        ])
3489        .unwrap();
3490        let mem_of = |w: &WarningHint| w.source_mem().map(str::to_string);
3491        let pre: Vec<_> = engine.load_warnings().iter().filter_map(mem_of).collect();
3492        assert!(
3493            pre.contains(&"alpha".to_string()) && pre.contains(&"beta".to_string()),
3494            "boot must populate one warning per mem: {pre:?}"
3495        );
3496
3497        // Heal alpha's file on disk, then reload only alpha.
3498        let clean_body = "---\ntype: spec\n---\n# Dup\n\n## Identity\n\na.\n";
3499        std::fs::write(a_dir.join("dup.md"), clean_body).unwrap();
3500        engine.reload_one_mem("alpha").unwrap();
3501
3502        let post: Vec<_> = engine.load_warnings().iter().filter_map(mem_of).collect();
3503        assert!(
3504            !post.contains(&"alpha".to_string()),
3505            "reload must drop the healed mem's stale warning: {post:?}"
3506        );
3507        assert!(
3508            post.contains(&"beta".to_string()),
3509            "reload of alpha must not clear beta's slice: {post:?}"
3510        );
3511    }
3512
3513    #[test]
3514    fn unregister_writable_mem_purges_load_warnings_for_that_mem_only() {
3515        // Two mems, each contributing a boot-time warning. Deleting
3516        // alpha must purge alpha's warnings from the accumulator
3517        // (health() merges it unconditionally — leftovers would cite
3518        // entities the store no longer holds) while beta's survive.
3519        let tmp = TempDir::new().unwrap();
3520        let dup_body = "---\ntype: spec\n---\n# Dup\n\n## Identity\n\na.\n\n## Identity\n\nb.\n";
3521        let a_dir = tmp.path().join("a");
3522        std::fs::create_dir_all(&a_dir).unwrap();
3523        std::fs::write(a_dir.join("dup.md"), dup_body).unwrap();
3524        let b_dir = tmp.path().join("b");
3525        std::fs::create_dir_all(&b_dir).unwrap();
3526        std::fs::write(b_dir.join("dup.md"), dup_body).unwrap();
3527        let mut engine = Engine::from_mounts(vec![
3528            (
3529                folder_mount("alpha", a_dir.clone()),
3530                Box::new(FilesystemMemWriter::new(a_dir)) as Box<dyn MemBackend>,
3531            ),
3532            (
3533                folder_mount("beta", b_dir.clone()),
3534                Box::new(FilesystemMemWriter::new(b_dir)) as Box<dyn MemBackend>,
3535            ),
3536        ])
3537        .unwrap();
3538        assert!(
3539            engine
3540                .load_warnings()
3541                .iter()
3542                .any(|w| w.source_mem() == Some("alpha")),
3543            "boot must carry alpha-sourced warnings"
3544        );
3545
3546        engine.unregister_writable_mem("alpha").unwrap();
3547
3548        let post = engine.load_warnings();
3549        assert!(
3550            !post.iter().any(|w| w.source_mem() == Some("alpha")),
3551            "delete must purge the removed mem's warnings: {post:?}"
3552        );
3553        assert!(
3554            post.iter().any(|w| w.source_mem() == Some("beta")),
3555            "delete of alpha must keep beta's warnings: {post:?}"
3556        );
3557    }
3558
3559    #[test]
3560    fn unregister_writable_mem_keeps_warnings_sourced_in_surviving_mems() {
3561        // Complement to the purge: a warning SOURCED in a surviving
3562        // mem whose TARGET pointed into the deleted mem must survive.
3563        // The invalid row still exists in the survivor's markdown —
3564        // it is live drift (recover-worthy), not stale state, so
3565        // purging by target would hide a real finding.
3566        let tmp = TempDir::new().unwrap();
3567        let a_dir = tmp.path().join("a");
3568        std::fs::create_dir_all(&a_dir).unwrap();
3569        let source_body = "---\ntype: spec\n---\n# Source\n\n## Identity\n\nThe source.\n\n## Relationships\n\n- **MADE_UP_TYPE**: [[beta--b1]]\n";
3570        std::fs::write(a_dir.join("source.md"), source_body).unwrap();
3571        let b_dir = tmp.path().join("b");
3572        std::fs::create_dir_all(&b_dir).unwrap();
3573        let target_body = "---\ntype: spec\n---\n# B1\n\n## Identity\n\nThe target.\n";
3574        std::fs::write(b_dir.join("b1.md"), target_body).unwrap();
3575        let mut engine = Engine::from_mounts(vec![
3576            (
3577                folder_mount("alpha", a_dir.clone()),
3578                Box::new(FilesystemMemWriter::new(a_dir)) as Box<dyn MemBackend>,
3579            ),
3580            (
3581                folder_mount("beta", b_dir.clone()),
3582                Box::new(FilesystemMemWriter::new(b_dir)) as Box<dyn MemBackend>,
3583            ),
3584        ])
3585        .unwrap();
3586        let alpha_sourced = |engine: &Engine| {
3587            engine
3588                .load_warnings()
3589                .iter()
3590                .any(|w| matches!(w, WarningHint::ParsedRelationInvalid { entity_id, .. } if entity_id.mem() == "alpha"))
3591        };
3592        assert!(
3593            alpha_sourced(&engine),
3594            "boot must flag alpha's invalid row: {:?}",
3595            engine.load_warnings()
3596        );
3597
3598        engine.unregister_writable_mem("beta").unwrap();
3599
3600        assert!(
3601            alpha_sourced(&engine),
3602            "deleting the TARGET mem must not purge the survivor-sourced warning: {:?}",
3603            engine.load_warnings()
3604        );
3605    }
3606
3607    /// The `memstead_reload` MCP tool's no-mem path consumes the
3608    /// reports variant — it must refresh `load_warnings` like its slim
3609    /// counterpart, not discard the sweep's warnings. Regression for
3610    /// the split-brain where the slim variant repopulated and the
3611    /// reports variant silently kept the boot-time snapshot forever
3612    /// (observed live 2026-07-11: warnings for deleted mems survived a
3613    /// workspace-wide MCP reload).
3614    #[test]
3615    fn reload_each_writable_mem_reports_refreshes_load_warnings() {
3616        let tmp = TempDir::new().unwrap();
3617        let mem_dir = tmp.path().to_path_buf();
3618        let dup_body = "---\ntype: spec\n---\n# Dup\n\n## Identity\n\na.\n\n## Identity\n\nb.\n";
3619        std::fs::write(mem_dir.join("dup.md"), dup_body).unwrap();
3620        let writer = FilesystemMemWriter::new(mem_dir.clone());
3621        let mut engine = Engine::from_mounts(vec![(
3622            folder_mount("specs", mem_dir.clone()),
3623            Box::new(writer) as Box<dyn MemBackend>,
3624        )])
3625        .unwrap();
3626        assert!(
3627            !engine.load_warnings().is_empty(),
3628            "boot must populate load_warnings"
3629        );
3630
3631        // Heal the file on disk; the reports sweep must clear the
3632        // stale warning.
3633        let clean_body = "---\ntype: spec\n---\n# Dup\n\n## Identity\n\na.\n";
3634        std::fs::write(mem_dir.join("dup.md"), clean_body).unwrap();
3635        engine.reload_each_writable_mem_reports().unwrap();
3636        assert!(
3637            engine.load_warnings().is_empty(),
3638            "reports sweep must drop healed warnings: {:?}",
3639            engine.load_warnings()
3640        );
3641
3642        // And the inverse: fresh drift surfaces through the same sweep.
3643        std::fs::write(mem_dir.join("dup.md"), dup_body).unwrap();
3644        engine.reload_each_writable_mem_reports().unwrap();
3645        assert!(
3646            engine
3647                .load_warnings()
3648                .iter()
3649                .any(|w| matches!(w, WarningHint::DuplicateSectionHeading { .. })),
3650            "reports sweep must surface fresh drift: {:?}",
3651            engine.load_warnings()
3652        );
3653    }
3654
3655    /// A cross-mem edge `A→B` must survive a
3656    /// per-mem reload of the TARGET mem B. The removal cascade drops
3657    /// B's incoming mirrors (including the cross-mem one sourced from A)
3658    /// and the re-push only rebuilds edges authored by B, so without the
3659    /// reconstruction pass the edge silently vanishes from the in-memory
3660    /// index while staying intact in A's record and on disk — under-
3661    /// reporting topology until a workspace-wide reload heals it.
3662    #[test]
3663    fn per_mem_reload_of_target_preserves_incoming_cross_mem_edge() {
3664        let tmp = TempDir::new().unwrap();
3665        let a_dir = tmp.path().join("a");
3666        let b_dir = tmp.path().join("b");
3667        std::fs::create_dir_all(&a_dir).unwrap();
3668        std::fs::create_dir_all(&b_dir).unwrap();
3669        let a_writer = FilesystemMemWriter::new(a_dir.clone());
3670        let b_writer = FilesystemMemWriter::new(b_dir.clone());
3671        let mut engine = Engine::from_mounts(vec![
3672            (
3673                folder_mount("specs", a_dir),
3674                Box::new(a_writer) as Box<dyn MemBackend>,
3675            ),
3676            (
3677                folder_mount("memos", b_dir),
3678                Box::new(b_writer) as Box<dyn MemBackend>,
3679            ),
3680        ])
3681        .unwrap();
3682
3683        // Grant the cross-mem link specs → memos so the relate lands.
3684        let mut settings = crate::workspace::WorkspaceSettings::default();
3685        settings.cross_mem_links.insert(
3686            "specs".to_string(),
3687            memstead_schema::workspace_config::CrossLinkValue::Wildcard,
3688        );
3689        engine.set_settings(settings);
3690
3691        let (actor, client) = cli_actor();
3692        let source = engine
3693            .create_entity(
3694                empty_create_args("specs", "Source"),
3695                actor,
3696                Some(&client),
3697                None,
3698            )
3699            .unwrap();
3700        let target = engine
3701            .create_entity(
3702                empty_create_args("memos", "Target"),
3703                actor,
3704                Some(&client),
3705                None,
3706            )
3707            .unwrap();
3708        engine
3709            .relate_entity(
3710                crate::engine::RelateEntityArgs {
3711                    source: source.id.clone(),
3712                    expected_hash: Some(source.content_hash.clone()),
3713                    rel_type: "USES".to_string(),
3714                    target: target.id.clone(),
3715                    remove: false,
3716                    description: None,
3717                    dry_run: false,
3718                },
3719                actor,
3720                Some(&client),
3721                None,
3722            )
3723            .unwrap();
3724
3725        // (outgoing-present, incoming-present) for the A→B edge.
3726        let has_edge = |e: &Engine| {
3727            let out = e
3728                .store()
3729                .outgoing(&source.id)
3730                .iter()
3731                .any(|edge| edge.target == target.id);
3732            let inc = e
3733                .store()
3734                .incoming(&target.id)
3735                .iter()
3736                .any(|edge| edge.from == source.id);
3737            (out, inc)
3738        };
3739
3740        assert_eq!(
3741            has_edge(&engine),
3742            (true, true),
3743            "edge must be indexed in both directions after relate",
3744        );
3745
3746        // Per-mem reload of the TARGET mem — the bug trigger.
3747        engine.reload_one_mem("memos").unwrap();
3748        assert_eq!(
3749            has_edge(&engine),
3750            (true, true),
3751            "cross-mem edge into B must survive a per-mem reload of B",
3752        );
3753
3754        // Convergence: a workspace-wide reload yields the same incoming
3755        // adjacency for the target — no path-dependent difference.
3756        engine.reload_each_writable_mem().unwrap();
3757        assert_eq!(
3758            has_edge(&engine),
3759            (true, true),
3760            "per-mem and workspace reload converge on the same edge",
3761        );
3762
3763        // Complement: the edge stayed in the source record throughout —
3764        // the bug and the fix are about the index, not the records.
3765        assert!(
3766            engine
3767                .store()
3768                .get(&source.id)
3769                .unwrap()
3770                .relationships
3771                .iter()
3772                .any(|r| r.target == target.id),
3773            "source record must retain the relationship throughout",
3774        );
3775    }
3776
3777    /// A per-mem reload of the SOURCE
3778    /// mem leaves the cross-mem edge intact too — the source's own
3779    /// outgoing edges are rebuilt by the re-push, and the reconstruction
3780    /// pass for the OTHER mem is not needed here. Guards against a fix
3781    /// that fixates on the target case and perturbs the source case.
3782    #[test]
3783    fn per_mem_reload_of_source_preserves_outgoing_cross_mem_edge() {
3784        let tmp = TempDir::new().unwrap();
3785        let a_dir = tmp.path().join("a");
3786        let b_dir = tmp.path().join("b");
3787        std::fs::create_dir_all(&a_dir).unwrap();
3788        std::fs::create_dir_all(&b_dir).unwrap();
3789        let a_writer = FilesystemMemWriter::new(a_dir.clone());
3790        let b_writer = FilesystemMemWriter::new(b_dir.clone());
3791        let mut engine = Engine::from_mounts(vec![
3792            (
3793                folder_mount("specs", a_dir),
3794                Box::new(a_writer) as Box<dyn MemBackend>,
3795            ),
3796            (
3797                folder_mount("memos", b_dir),
3798                Box::new(b_writer) as Box<dyn MemBackend>,
3799            ),
3800        ])
3801        .unwrap();
3802        let mut settings = crate::workspace::WorkspaceSettings::default();
3803        settings.cross_mem_links.insert(
3804            "specs".to_string(),
3805            memstead_schema::workspace_config::CrossLinkValue::Wildcard,
3806        );
3807        engine.set_settings(settings);
3808
3809        let (actor, client) = cli_actor();
3810        let source = engine
3811            .create_entity(
3812                empty_create_args("specs", "Source"),
3813                actor,
3814                Some(&client),
3815                None,
3816            )
3817            .unwrap();
3818        let target = engine
3819            .create_entity(
3820                empty_create_args("memos", "Target"),
3821                actor,
3822                Some(&client),
3823                None,
3824            )
3825            .unwrap();
3826        engine
3827            .relate_entity(
3828                crate::engine::RelateEntityArgs {
3829                    source: source.id.clone(),
3830                    expected_hash: Some(source.content_hash.clone()),
3831                    rel_type: "USES".to_string(),
3832                    target: target.id.clone(),
3833                    remove: false,
3834                    description: None,
3835                    dry_run: false,
3836                },
3837                actor,
3838                Some(&client),
3839                None,
3840            )
3841            .unwrap();
3842
3843        engine.reload_one_mem("specs").unwrap();
3844
3845        let out = engine
3846            .store()
3847            .outgoing(&source.id)
3848            .iter()
3849            .any(|edge| edge.target == target.id);
3850        let inc = engine
3851            .store()
3852            .incoming(&target.id)
3853            .iter()
3854            .any(|edge| edge.from == source.id);
3855        assert!(
3856            out && inc,
3857            "outgoing cross-mem edge must survive a source-mem reload"
3858        );
3859    }
3860
3861    #[test]
3862    fn workspace_root_setter_round_trips() {
3863        let tmp = TempDir::new().unwrap();
3864        let mem_dir = tmp.path().to_path_buf();
3865        let writer = FilesystemMemWriter::new(mem_dir.clone());
3866        let mut engine = Engine::from_mounts(vec![(
3867            folder_mount("specs", mem_dir),
3868            Box::new(writer) as Box<dyn MemBackend>,
3869        )])
3870        .unwrap();
3871        let root = tmp.path().to_path_buf();
3872        engine.set_workspace_root(root.clone());
3873        assert_eq!(engine.workspace_root(), Some(root.as_path()));
3874    }
3875
3876    #[test]
3877    fn export_mem_folder_backend_produces_archive() {
3878        // Folder-backed mem with config + one entity. The
3879        // export_mem dispatcher routes to the folder backend's
3880        // override which produces a deterministic .memstead archive.
3881        let tmp = TempDir::new().unwrap();
3882        let mem_dir = tmp.path().join("specs");
3883        std::fs::create_dir_all(mem_dir.join(".memstead")).unwrap();
3884        let config_body = r#"{
3885            "format": 1,
3886            "schema": "default@1.0.0",
3887            "version": "1.0.0"
3888        }"#;
3889        std::fs::write(mem_dir.join(".memstead").join("config.json"), config_body).unwrap();
3890
3891        let writer = FilesystemMemWriter::new(mem_dir.clone());
3892        let engine = Engine::from_mounts(vec![(
3893            folder_mount("specs", mem_dir.clone()),
3894            Box::new(writer) as Box<dyn MemBackend>,
3895        )])
3896        .unwrap();
3897
3898        let archive_path = tmp.path().join("specs.mem");
3899        let result = engine.export_mem("specs", &archive_path).unwrap();
3900        assert!(archive_path.exists(), "archive must exist on disk");
3901        assert!(result.size_bytes > 0);
3902        // entity_count is 0 here (no .md files seeded); the function
3903        // still produces an archive carrying the config + schema.
3904        assert_eq!(result.entity_count, 0);
3905    }
3906
3907    #[test]
3908    fn export_mem_unknown_mem_returns_unknown_mem() {
3909        let tmp = TempDir::new().unwrap();
3910        let mem_dir = tmp.path().to_path_buf();
3911        let writer = FilesystemMemWriter::new(mem_dir.clone());
3912        let engine = Engine::from_mounts(vec![(
3913            folder_mount("specs", mem_dir),
3914            Box::new(writer) as Box<dyn MemBackend>,
3915        )])
3916        .unwrap();
3917        let output = tmp.path().join("out.mem");
3918        let err = engine.export_mem("missing", &output).unwrap_err();
3919        assert!(matches!(err, EngineError::UnknownMem(v) if v == "missing"));
3920    }
3921
3922    #[test]
3923    fn export_mem_missing_config_returns_invalid_input() {
3924        // Folder mount with no .memstead/config.json — `mem_config_for`
3925        // returns None and `export_mem` surfaces InvalidInput
3926        // rather than reaching the backend.
3927        let tmp = TempDir::new().unwrap();
3928        let mem_dir = tmp.path().to_path_buf();
3929        let writer = FilesystemMemWriter::new(mem_dir.clone());
3930        let engine = Engine::from_mounts(vec![(
3931            folder_mount("specs", mem_dir),
3932            Box::new(writer) as Box<dyn MemBackend>,
3933        )])
3934        .unwrap();
3935        let output = tmp.path().join("out.mem");
3936        let err = engine.export_mem("specs", &output).unwrap_err();
3937        assert!(matches!(err, EngineError::InvalidInput(_)));
3938    }
3939
3940    #[test]
3941    fn export_mem_archive_backend_returns_sealed() {
3942        // Archive backends are already-an-archive — re-export is
3943        // intentionally rejected via BackendError::Sealed.
3944        let tmp = TempDir::new().unwrap();
3945        let archive_path = build_archive(
3946            tmp.path(),
3947            "ext",
3948            &[(
3949                ".memstead/config.json",
3950                b"{\"format\":1,\"schema\":\"default@1.0.0\",\"version\":\"1.0.0\"}",
3951            )],
3952        );
3953        let engine = Engine::from_mounts(vec![(
3954            archive_mount("ext", archive_path.clone()),
3955            Box::new(ArchiveBackend::new(archive_path)) as Box<dyn MemBackend>,
3956        )])
3957        .unwrap();
3958        let output = tmp.path().join("out.mem");
3959        let err = engine.export_mem("ext", &output).unwrap_err();
3960        assert!(matches!(err, EngineError::Backend(BackendError::Sealed)));
3961    }
3962
3963    #[test]
3964    fn export_markdown_writes_unchanged_files_zero_writes() {
3965        // Seed a folder-backed mem with one entity, then call
3966        // export_markdown. The entity's file already matches the
3967        // generated content (engine wrote it via create_entity), so
3968        // export reports `unchanged: 1, written: 0`.
3969        let tmp = TempDir::new().unwrap();
3970        let (engine, _seeded) = engine_with_seed(&tmp, "Sample");
3971        let result = engine.export_markdown(None, None).unwrap();
3972        assert_eq!(
3973            result.written, 0,
3974            "freshly-created entity's file already matches generated markdown"
3975        );
3976        assert_eq!(
3977            result.unchanged, 1,
3978            "the one seeded entity counts as unchanged"
3979        );
3980        assert!(
3981            result.skipped_mounts.is_empty(),
3982            "folder-only workspace has no skipped mounts"
3983        );
3984    }
3985
3986    #[test]
3987    fn export_markdown_skips_non_folder_mounts() {
3988        // Archive-mounted mem has no working tree — workspace-wide
3989        // export records it under skipped_mounts and reports zero
3990        // writes / zero unchanged for the rest.
3991        let tmp = TempDir::new().unwrap();
3992        let archive_path = build_archive(tmp.path(), "ext", &[("a.md", b"# Title: Foo\n")]);
3993        let engine = Engine::from_mounts(vec![(
3994            archive_mount("ext", archive_path.clone()),
3995            Box::new(ArchiveBackend::new(archive_path)) as Box<dyn MemBackend>,
3996        )])
3997        .unwrap();
3998        let result = engine.export_markdown(None, None).unwrap();
3999        assert_eq!(result.written, 0);
4000        assert_eq!(result.unchanged, 0);
4001        assert_eq!(
4002            result.skipped_mounts.len(),
4003            1,
4004            "archive mount is in the skipped list"
4005        );
4006        let entry = &result.skipped_mounts[0];
4007        assert_eq!(entry.mem, "ext");
4008        assert_eq!(entry.active_backend, "archive");
4009        assert_eq!(entry.reason, "backend_does_not_support_markdown_export");
4010    }
4011
4012    #[test]
4013    fn export_markdown_per_mem_refuses_on_incompatible_backend() {
4014        // Per-mem export against an archive-backed mem returns
4015        // the typed `MARKDOWN_EXPORT_UNSUPPORTED_BACKEND` refusal
4016        // naming the active backend and the supported-backend list.
4017        let tmp = TempDir::new().unwrap();
4018        let archive_path = build_archive(tmp.path(), "ext", &[("a.md", b"# Title: Foo\n")]);
4019        let engine = Engine::from_mounts(vec![(
4020            archive_mount("ext", archive_path.clone()),
4021            Box::new(ArchiveBackend::new(archive_path)) as Box<dyn MemBackend>,
4022        )])
4023        .unwrap();
4024        let err = engine.export_markdown(Some("ext"), None).unwrap_err();
4025        assert_eq!(err.code(), "MARKDOWN_EXPORT_UNSUPPORTED_BACKEND");
4026        let details = err.details();
4027        assert_eq!(details["mem"], "ext");
4028        assert_eq!(details["active_backend"], "archive");
4029        assert_eq!(details["supported_backends"], serde_json::json!(["folder"]));
4030    }
4031
4032    #[test]
4033    fn register_writable_mem_adds_mount_and_router_entry() {
4034        // Start with one mem; register a second at runtime. Both
4035        // should be visible afterwards.
4036        let tmp = TempDir::new().unwrap();
4037        let mem_a = tmp.path().join("a");
4038        std::fs::create_dir_all(&mem_a).unwrap();
4039        let writer_a = FilesystemMemWriter::new(mem_a.clone());
4040
4041        let mut engine = Engine::from_mounts(vec![(
4042            folder_mount("alpha", mem_a),
4043            Box::new(writer_a) as Box<dyn MemBackend>,
4044        )])
4045        .unwrap();
4046        assert!(engine.mem_router().is_writable("alpha"));
4047
4048        let mem_b = tmp.path().join("b");
4049        std::fs::create_dir_all(&mem_b).unwrap();
4050        let writer_b = FilesystemMemWriter::new(mem_b.clone());
4051
4052        engine
4053            .register_writable_mem(
4054                folder_mount("beta", mem_b.clone()),
4055                Box::new(writer_b) as Box<dyn MemBackend>,
4056                MemOrigin::ExplicitToml,
4057            )
4058            .unwrap();
4059
4060        // Both mems are now writable + visible.
4061        assert!(engine.mem_router().is_writable("alpha"));
4062        assert!(engine.mem_router().is_writable("beta"));
4063        assert!(engine.mem_router().is_visible("beta"));
4064
4065        // Mount + schema lookups resolve.
4066        assert!(engine.mount("beta").is_some());
4067        assert!(engine.schemas().contains_key("beta"));
4068
4069        // Folder path surfaces via mem_router.
4070        assert_eq!(
4071            engine.mem_router().dir_for_mem("beta"),
4072            Some(mem_b.as_path()),
4073        );
4074    }
4075
4076    /// Schema-pin authority on the runtime-register path (symmetric with
4077    /// the boot path): a mem registered at runtime resolves its schema
4078    /// from its own config (`software@0.1.0`) even though the mount
4079    /// expects an unresolvable pin — register succeeds, and the
4080    /// disagreement surfaces a `SchemaPinMismatch` warning.
4081    #[test]
4082    fn register_writable_mem_resolves_schema_from_mem_config() {
4083        let tmp = TempDir::new().unwrap();
4084        let mem_a = tmp.path().join("a");
4085        std::fs::create_dir_all(&mem_a).unwrap();
4086        let mut engine = Engine::from_mounts(vec![(
4087            folder_mount("alpha", mem_a.clone()),
4088            Box::new(FilesystemMemWriter::new(mem_a)) as Box<dyn MemBackend>,
4089        )])
4090        .unwrap();
4091
4092        let mem_b = tmp.path().join("b");
4093        std::fs::create_dir_all(mem_b.join(".memstead")).unwrap();
4094        std::fs::write(
4095            mem_b.join(".memstead").join("config.json"),
4096            r#"{"schema":"software@0.1.0"}"#,
4097        )
4098        .unwrap();
4099        let mount_b = crate::workspace::Mount {
4100            mem: "beta".to_string(),
4101            schema: Some(memstead_schema::SchemaRef::new(
4102                "totally-not-a-schema",
4103                semver::Version::new(9, 9, 9),
4104            )),
4105            storage: crate::workspace::MountStorage::Folder {
4106                path: mem_b.clone(),
4107            },
4108            capability: crate::workspace::MountCapability::Write,
4109            lifecycle: crate::workspace::MountLifecycle::Eager,
4110            cross_linkable: true,
4111            migration_target: None,
4112        };
4113        engine
4114            .register_writable_mem(
4115                mount_b,
4116                Box::new(FilesystemMemWriter::new(mem_b)) as Box<dyn MemBackend>,
4117                MemOrigin::ExplicitToml,
4118            )
4119            .expect("config pin software@0.1.0 is authoritative — register must succeed despite the unresolvable mount pin");
4120
4121        assert!(engine.schemas().contains_key("beta"));
4122        let surfaced = engine.load_warnings().iter().any(|w| {
4123            matches!(
4124                w,
4125                WarningHint::SchemaPinMismatch { mem, config_pin, mount_pin }
4126                    if mem == "beta"
4127                        && config_pin == "software@0.1.0"
4128                        && mount_pin == "totally-not-a-schema@9.9.9"
4129            )
4130        });
4131        assert!(
4132            surfaced,
4133            "SchemaPinMismatch must surface for beta: {:?}",
4134            engine.load_warnings(),
4135        );
4136    }
4137
4138    #[test]
4139    fn register_writable_mem_rejects_existing_name() {
4140        // Re-registering an already-writable mem must fail with
4141        // MemNameCollision and not mutate the engine.
4142        let tmp = TempDir::new().unwrap();
4143        let mem_a = tmp.path().join("a");
4144        std::fs::create_dir_all(&mem_a).unwrap();
4145        let writer_a = FilesystemMemWriter::new(mem_a.clone());
4146
4147        let mut engine = Engine::from_mounts(vec![(
4148            folder_mount("alpha", mem_a),
4149            Box::new(writer_a) as Box<dyn MemBackend>,
4150        )])
4151        .unwrap();
4152        let mount_count_pre = engine.mounts().len();
4153
4154        let mem_collide = tmp.path().join("alpha-2");
4155        std::fs::create_dir_all(&mem_collide).unwrap();
4156        let writer_collide = FilesystemMemWriter::new(mem_collide.clone());
4157
4158        let err = engine
4159            .register_writable_mem(
4160                folder_mount("alpha", mem_collide),
4161                Box::new(writer_collide) as Box<dyn MemBackend>,
4162                MemOrigin::ExplicitToml,
4163            )
4164            .unwrap_err();
4165        match err {
4166            EngineError::MemNameCollision {
4167                name,
4168                source_origin,
4169            } => {
4170                assert_eq!(name, "alpha");
4171                // post-restructure source_origin references
4172                // `.memstead/workspace.toml`; the assertion stays
4173                // permissive (substring OR non-empty) so the test
4174                // doesn't lock the exact wording.
4175                assert!(
4176                    source_origin.contains(".memstead/workspace.toml") || !source_origin.is_empty()
4177                );
4178            }
4179            other => panic!("expected MemNameCollision, got {other:?}"),
4180        }
4181
4182        // Engine state unchanged.
4183        assert_eq!(engine.mounts().len(), mount_count_pre);
4184    }
4185
4186    #[test]
4187    fn register_writable_mem_loads_entities_into_store() {
4188        // The newly-registered mem's entities should surface in
4189        // the engine's store after registration.
4190        let tmp = TempDir::new().unwrap();
4191        let mem_a = tmp.path().join("a");
4192        std::fs::create_dir_all(&mem_a).unwrap();
4193        let writer_a = FilesystemMemWriter::new(mem_a.clone());
4194
4195        let mut engine = Engine::from_mounts(vec![(
4196            folder_mount("alpha", mem_a),
4197            Box::new(writer_a) as Box<dyn MemBackend>,
4198        )])
4199        .unwrap();
4200        let pre_count = engine.store().all_entities().count();
4201
4202        // Build mem_b with a markdown entity on disk.
4203        let mem_b = tmp.path().join("b");
4204        std::fs::create_dir_all(&mem_b).unwrap();
4205        std::fs::write(
4206            mem_b.join("b1.md"),
4207            "---\ntype: spec\n---\n# B1\n\n## Identity\n\nseed.\n",
4208        )
4209        .unwrap();
4210        let writer_b = FilesystemMemWriter::new(mem_b.clone());
4211
4212        engine
4213            .register_writable_mem(
4214                folder_mount("beta", mem_b),
4215                Box::new(writer_b) as Box<dyn MemBackend>,
4216                MemOrigin::ExplicitToml,
4217            )
4218            .unwrap();
4219
4220        let post_count = engine.store().all_entities().count();
4221        assert!(post_count > pre_count, "register must load entities");
4222        let beta_count = engine
4223            .store()
4224            .all_entities()
4225            .filter(|e| e.mem == "beta")
4226            .count();
4227        assert_eq!(beta_count, 1);
4228    }
4229
4230    #[test]
4231    fn register_then_unregister_round_trips() {
4232        // End-to-end check: register a mem, then unregister it,
4233        // and confirm the engine returns to the pre-registration
4234        // state.
4235        let tmp = TempDir::new().unwrap();
4236        let mem_a = tmp.path().join("a");
4237        std::fs::create_dir_all(&mem_a).unwrap();
4238        let writer_a = FilesystemMemWriter::new(mem_a.clone());
4239
4240        let mut engine = Engine::from_mounts(vec![(
4241            folder_mount("alpha", mem_a),
4242            Box::new(writer_a) as Box<dyn MemBackend>,
4243        )])
4244        .unwrap();
4245        let pre_mounts = engine.mounts().len();
4246
4247        let mem_b = tmp.path().join("b");
4248        std::fs::create_dir_all(&mem_b).unwrap();
4249        let writer_b = FilesystemMemWriter::new(mem_b);
4250
4251        engine
4252            .register_writable_mem(
4253                folder_mount("beta", tmp.path().join("b")),
4254                Box::new(writer_b) as Box<dyn MemBackend>,
4255                MemOrigin::ExplicitToml,
4256            )
4257            .unwrap();
4258        assert_eq!(engine.mounts().len(), pre_mounts + 1);
4259
4260        let removed = engine.unregister_writable_mem("beta").unwrap();
4261        assert!(removed.is_some());
4262        assert_eq!(engine.mounts().len(), pre_mounts);
4263        assert!(!engine.mem_router().is_writable("beta"));
4264    }
4265
4266    #[test]
4267    fn unregister_writable_mem_returns_false_for_unknown_name() {
4268        // Idempotent contract: repeated calls / unknown names are
4269        // not errors — return false so callers can branch without
4270        // a typed error envelope for the common "already gone" case.
4271        let tmp = TempDir::new().unwrap();
4272        let mem_dir = tmp.path().to_path_buf();
4273        let writer = FilesystemMemWriter::new(mem_dir.clone());
4274        let mut engine = Engine::from_mounts(vec![(
4275            folder_mount("specs", mem_dir),
4276            Box::new(writer) as Box<dyn MemBackend>,
4277        )])
4278        .unwrap();
4279        let removed = engine.unregister_writable_mem("missing").unwrap();
4280        assert!(removed.is_none(), "unknown mem returns Ok(None)");
4281        // The original mem is still present and readable.
4282        assert!(engine.mem_router().is_writable("specs"));
4283    }
4284
4285    #[test]
4286    fn unregister_writable_mem_drops_mount_and_router_entry() {
4287        // Heterogeneous engine: two mounts. Unregister one and
4288        // assert (a) it's gone from the mount list, (b) gone from
4289        // the mem_router's writable set, (c) the OTHER mount is
4290        // untouched.
4291        let tmp = TempDir::new().unwrap();
4292        let mem_a = tmp.path().join("a");
4293        std::fs::create_dir_all(&mem_a).unwrap();
4294        let writer_a = FilesystemMemWriter::new(mem_a.clone());
4295        let mem_b = tmp.path().join("b");
4296        std::fs::create_dir_all(&mem_b).unwrap();
4297        let writer_b = FilesystemMemWriter::new(mem_b.clone());
4298
4299        let mut engine = Engine::from_mounts(vec![
4300            (
4301                folder_mount("alpha", mem_a),
4302                Box::new(writer_a) as Box<dyn MemBackend>,
4303            ),
4304            (
4305                folder_mount("beta", mem_b),
4306                Box::new(writer_b) as Box<dyn MemBackend>,
4307            ),
4308        ])
4309        .unwrap();
4310
4311        let removed = engine.unregister_writable_mem("alpha").unwrap();
4312        assert!(removed.is_some());
4313
4314        // alpha is gone from every surface.
4315        assert!(!engine.mem_router().is_writable("alpha"));
4316        assert!(!engine.mem_router().is_visible("alpha"));
4317        assert!(engine.mount("alpha").is_none());
4318
4319        // beta survives unchanged.
4320        assert!(engine.mem_router().is_writable("beta"));
4321        assert!(engine.mount("beta").is_some());
4322    }
4323
4324    #[test]
4325    fn unregister_writable_mem_drops_entities_for_that_mem_only() {
4326        // Build an engine with two mems, write one entity to each
4327        // backend, build the engine (loads both), unregister one,
4328        // assert the store still has the other mem's entity.
4329        let tmp = TempDir::new().unwrap();
4330        let mem_a = tmp.path().join("a");
4331        std::fs::create_dir_all(&mem_a).unwrap();
4332        std::fs::write(
4333            mem_a.join("a1.md"),
4334            "---\ntype: spec\n---\n# A1\n\n## Identity\n\nseed.\n",
4335        )
4336        .unwrap();
4337        let writer_a = FilesystemMemWriter::new(mem_a.clone());
4338
4339        let mem_b = tmp.path().join("b");
4340        std::fs::create_dir_all(&mem_b).unwrap();
4341        std::fs::write(
4342            mem_b.join("b1.md"),
4343            "---\ntype: spec\n---\n# B1\n\n## Identity\n\nseed.\n",
4344        )
4345        .unwrap();
4346        let writer_b = FilesystemMemWriter::new(mem_b.clone());
4347
4348        let mut engine = Engine::from_mounts(vec![
4349            (
4350                folder_mount("alpha", mem_a),
4351                Box::new(writer_a) as Box<dyn MemBackend>,
4352            ),
4353            (
4354                folder_mount("beta", mem_b),
4355                Box::new(writer_b) as Box<dyn MemBackend>,
4356            ),
4357        ])
4358        .unwrap();
4359
4360        let pre_total = engine.store().all_entities().count();
4361        assert!(pre_total >= 2, "both mems must load entities");
4362
4363        engine.unregister_writable_mem("alpha").unwrap();
4364
4365        // alpha's entities are gone.
4366        let alpha_remaining = engine
4367            .store()
4368            .all_entities()
4369            .filter(|e| e.mem == "alpha")
4370            .count();
4371        assert_eq!(alpha_remaining, 0);
4372
4373        // beta's entities survive.
4374        let beta_remaining = engine
4375            .store()
4376            .all_entities()
4377            .filter(|e| e.mem == "beta")
4378            .count();
4379        assert!(beta_remaining > 0, "beta entities must survive");
4380    }
4381    #[test]
4382    fn reload_one_mem_returns_empty_diff_when_disk_is_unchanged() {
4383        let tmp = TempDir::new().unwrap();
4384        let mut engine = build_demo_engine(&tmp);
4385        let result = engine
4386            .reload_one_mem("specs")
4387            .expect("reload on stable disk must succeed");
4388        assert!(result.added.is_empty(), "added: {:?}", result.added);
4389        assert!(result.changed.is_empty(), "changed: {:?}", result.changed);
4390        assert!(result.removed.is_empty(), "removed: {:?}", result.removed);
4391    }
4392
4393    #[test]
4394    fn reload_one_mem_picks_up_external_addition() {
4395        let tmp = TempDir::new().unwrap();
4396        let mut engine = build_demo_engine(&tmp);
4397        // Simulate an external writer dropping a new entity on disk
4398        // without going through the engine.
4399        std::fs::write(
4400            tmp.path().join("external.md"),
4401            "---\ntype: spec\n---\n# External\n\n## Identity\n\nE.\n",
4402        )
4403        .unwrap();
4404        let result = engine.reload_one_mem("specs").unwrap();
4405        assert_eq!(
4406            result.added.iter().map(|i| i.as_ref()).collect::<Vec<_>>(),
4407            vec!["specs--external"]
4408        );
4409        assert!(result.changed.is_empty());
4410        assert!(result.removed.is_empty());
4411        // The new entity is now reachable through the engine.
4412        assert!(
4413            engine
4414                .get_entity(&crate::EntityId::new("specs", "external"))
4415                .is_some()
4416        );
4417    }
4418
4419    #[test]
4420    fn reload_one_mem_picks_up_external_removal() {
4421        let tmp = TempDir::new().unwrap();
4422        let mut engine = build_demo_engine(&tmp);
4423        // Lonely Three exists from the demo fixture; remove it
4424        // off-engine and reload.
4425        std::fs::remove_file(tmp.path().join("lonely-three.md")).unwrap();
4426        let result = engine.reload_one_mem("specs").unwrap();
4427        assert!(result.added.is_empty());
4428        assert!(result.changed.is_empty());
4429        assert_eq!(
4430            result
4431                .removed
4432                .iter()
4433                .map(|i| i.as_ref())
4434                .collect::<Vec<_>>(),
4435            vec!["specs--lonely-three"]
4436        );
4437    }
4438
4439    #[test]
4440    fn reload_one_mem_picks_up_external_change() {
4441        let tmp = TempDir::new().unwrap();
4442        let mut engine = build_demo_engine(&tmp);
4443        // Overwrite an existing entity's content; the new
4444        // `content_hash` must surface in the `changed` diff.
4445        std::fs::write(
4446            tmp.path().join("source-one.md"),
4447            "---\ntype: spec\n---\n# Source One Edited\n\n## Identity\n\nNew body.\n",
4448        )
4449        .unwrap();
4450        let result = engine.reload_one_mem("specs").unwrap();
4451        assert!(result.added.is_empty());
4452        assert_eq!(
4453            result
4454                .changed
4455                .iter()
4456                .map(|i| i.as_ref())
4457                .collect::<Vec<_>>(),
4458            vec!["specs--source-one"]
4459        );
4460        assert!(result.removed.is_empty());
4461    }
4462
4463    #[test]
4464    fn reload_one_mem_rejects_unknown_mem() {
4465        let tmp = TempDir::new().unwrap();
4466        let mut engine = build_demo_engine(&tmp);
4467        let err = engine.reload_one_mem("nope").unwrap_err();
4468        match err {
4469            EngineError::UnknownMem(name) => assert_eq!(name, "nope"),
4470            other => panic!("expected UnknownMem, got {other:?}"),
4471        }
4472    }
4473
4474    #[test]
4475    fn reload_each_writable_mem_returns_one_entry_per_mount() {
4476        let tmp = TempDir::new().unwrap();
4477        let mut engine = build_demo_engine(&tmp);
4478        let reports = engine
4479            .reload_each_writable_mem()
4480            .expect("batch reload on stable disk must succeed");
4481        assert_eq!(reports.len(), 1);
4482        assert_eq!(reports[0].0, "specs");
4483        assert!(reports[0].1.added.is_empty());
4484        assert!(reports[0].1.changed.is_empty());
4485        assert!(reports[0].1.removed.is_empty());
4486    }
4487
4488    // ---- Engine::settings -------------------------------------------
4489
4490    #[test]
4491    fn settings_default_to_empty_on_fresh_engine() {
4492        let tmp = TempDir::new().unwrap();
4493        let engine = build_demo_engine(&tmp);
4494        let s = engine.settings();
4495        assert!(s.mem_create_rules.is_empty());
4496        assert!(s.mem_delete_rules.is_empty());
4497        assert!(s.cross_mem_links.is_empty());
4498    }
4499
4500    #[test]
4501    fn set_settings_replaces_workspace_policy() {
4502        use crate::workspace::{CreateRuleSetting, DeleteRuleSetting, WorkspaceSettings};
4503        let tmp = TempDir::new().unwrap();
4504        let mut engine = build_demo_engine(&tmp);
4505        let mut settings = WorkspaceSettings::default();
4506        settings.mem_create_rules.push(CreateRuleSetting {
4507            pattern: "exec-*".to_string(),
4508            schemas: vec!["default@1.0.0".to_string()],
4509            default_cross_links: None,
4510        });
4511        settings.mem_delete_rules.push(DeleteRuleSetting {
4512            pattern: "exec-*".to_string(),
4513        });
4514        engine.set_settings(settings);
4515        assert_eq!(engine.settings().mem_create_rules.len(), 1);
4516        assert_eq!(engine.settings().mem_create_rules[0].pattern, "exec-*");
4517        assert_eq!(engine.settings().mem_delete_rules.len(), 1);
4518        assert_eq!(engine.settings().mem_delete_rules[0].pattern, "exec-*");
4519    }
4520
4521    // ---- Engine::reload_each_writable_mem (continued) -------------
4522
4523    #[test]
4524    fn reload_each_writable_mem_picks_up_external_changes_per_mem() {
4525        let tmp = TempDir::new().unwrap();
4526        let mut engine = build_demo_engine(&tmp);
4527        // Mutate disk: add one entity, remove another, change a third.
4528        std::fs::write(
4529            tmp.path().join("new-via-disk.md"),
4530            "---\ntype: spec\n---\n# New Via Disk\n\n## Identity\n\nN.\n",
4531        )
4532        .unwrap();
4533        std::fs::remove_file(tmp.path().join("lonely-three.md")).unwrap();
4534        std::fs::write(
4535            tmp.path().join("source-one.md"),
4536            "---\ntype: spec\n---\n# Source One\n\n## Identity\n\nDifferent body.\n",
4537        )
4538        .unwrap();
4539
4540        let reports = engine.reload_each_writable_mem().unwrap();
4541        assert_eq!(reports.len(), 1);
4542        let (mem, result) = &reports[0];
4543        assert_eq!(mem, "specs");
4544        assert_eq!(
4545            result.added.iter().map(|i| i.as_ref()).collect::<Vec<_>>(),
4546            vec!["specs--new-via-disk"]
4547        );
4548        assert_eq!(
4549            result
4550                .removed
4551                .iter()
4552                .map(|i| i.as_ref())
4553                .collect::<Vec<_>>(),
4554            vec!["specs--lonely-three"]
4555        );
4556        assert_eq!(
4557            result
4558                .changed
4559                .iter()
4560                .map(|i| i.as_ref())
4561                .collect::<Vec<_>>(),
4562            vec!["specs--source-one"]
4563        );
4564    }
4565
4566    // ---- Engine::reload_one_mem_report (rich-shape wrapper) -------
4567
4568    #[test]
4569    fn reload_one_mem_report_returns_rich_shape_for_folder_default() {
4570        // The folder backend's drift cursor is the changelog's
4571        // last-line timestamp (RFC3339-millis) — the same dialect
4572        // `folder_changes_since` accepts. With the demo engine's
4573        // creates already logged, both heads carry that cursor and,
4574        // with the disk unchanged between init and reload, they are
4575        // equal. entities_loaded reflects the post-reload count;
4576        // changed_entity_ids is empty when the disk is unchanged.
4577        let tmp = TempDir::new().unwrap();
4578        let mut engine = build_demo_engine(&tmp);
4579        let report = engine.reload_one_mem_report("specs").unwrap();
4580        assert_eq!(report.mem, "specs");
4581        assert_eq!(
4582            report.head_before, report.head_after,
4583            "unchanged disk → stable cursor"
4584        );
4585        assert!(
4586            crate::filesystem::changelog::parse_rfc3339_utc(&report.head_after).is_some(),
4587            "folder heads carry the changelog-ts cursor, got {}",
4588            report.head_after
4589        );
4590        // build_demo_engine seeds 3 entities (Source One, Target Two,
4591        // Lonely Three) — all real, no stubs from those creates.
4592        assert_eq!(report.entities_loaded, 3);
4593        // No external disk changes between init and reload → empty diff.
4594        assert!(report.changed_entity_ids.is_empty());
4595    }
4596
4597    #[test]
4598    fn reload_one_mem_report_unions_added_changed_removed_into_one_list() {
4599        // Mutate disk: add one, remove one, change one. The report's
4600        // changed_entity_ids unions the slim ReloadResult's three
4601        // diff lists into a single sorted vec — matches full's
4602        // wire contract.
4603        let tmp = TempDir::new().unwrap();
4604        let mut engine = build_demo_engine(&tmp);
4605        std::fs::write(
4606            tmp.path().join("new-via-disk.md"),
4607            "---\ntype: spec\n---\n# New Via Disk\n\n## Identity\n\nN.\n",
4608        )
4609        .unwrap();
4610        std::fs::remove_file(tmp.path().join("lonely-three.md")).unwrap();
4611        std::fs::write(
4612            tmp.path().join("source-one.md"),
4613            "---\ntype: spec\n---\n# Source One\n\n## Identity\n\nDifferent body.\n",
4614        )
4615        .unwrap();
4616
4617        let report = engine.reload_one_mem_report("specs").unwrap();
4618        assert_eq!(report.mem, "specs");
4619        let ids: Vec<&str> = report
4620            .changed_entity_ids
4621            .iter()
4622            .map(|id| id.as_ref())
4623            .collect();
4624        // Sorted lexicographically: lonely-three < new-via-disk < source-one
4625        assert_eq!(
4626            ids,
4627            vec![
4628                "specs--lonely-three",
4629                "specs--new-via-disk",
4630                "specs--source-one",
4631            ]
4632        );
4633    }
4634
4635    #[test]
4636    fn reload_one_mem_report_rejects_unknown_mem() {
4637        let tmp = TempDir::new().unwrap();
4638        let mut engine = build_demo_engine(&tmp);
4639        let err = engine.reload_one_mem_report("missing").unwrap_err();
4640        assert!(matches!(err, EngineError::UnknownMem(_)));
4641    }
4642
4643    #[test]
4644    fn reload_each_writable_mem_reports_returns_one_entry_per_mount() {
4645        let tmp = TempDir::new().unwrap();
4646        let mut engine = build_demo_engine(&tmp);
4647        let reports = engine.reload_each_writable_mem_reports().unwrap();
4648        assert_eq!(reports.len(), 1);
4649        assert_eq!(reports[0].mem, "specs");
4650        assert_eq!(reports[0].entities_loaded, 3);
4651    }
4652
4653    /// Workspace-wide reload re-reads `.memstead/workspace.toml` and
4654    /// refreshes [`WorkspaceSettings`]. This is the pairing with the
4655    /// CLI's `memstead workspace allow-create / grant-cross-link /
4656    /// set-mutations` family — without it, a CLI write lands on disk
4657    /// but the running engine keeps serving the boot-time policy
4658    /// snapshot until process restart.
4659    #[test]
4660    fn reload_each_writable_mem_reports_refreshes_workspace_settings() {
4661        let tmp = TempDir::new().unwrap();
4662
4663        // Minimum-viable workspace.toml (no rules) + one writable
4664        // folder-backed mem.
4665        let memstead_dir = tmp.path().join(".memstead");
4666        std::fs::create_dir_all(&memstead_dir).unwrap();
4667        let workspace_toml = memstead_dir.join("workspace.toml");
4668        std::fs::write(
4669            &workspace_toml,
4670            "format = \"memstead-git-branch-2\"\n\n[persistence_adapter]\nname = \"file-two-layer\"\n",
4671        )
4672        .unwrap();
4673        let mounts_json = memstead_dir.join("state").join("mounts.json");
4674        std::fs::create_dir_all(mounts_json.parent().unwrap()).unwrap();
4675        let mem_dir = tmp.path().join("specs");
4676        std::fs::create_dir_all(&mem_dir).unwrap();
4677        let mounts_body = format!(
4678            r#"{{ "format": "memstead-mounts-3", "mounts": [{{ "mem": "specs", "schema": "default@1.0.0", "storage": {{ "type": "folder", "path": "{}" }}, "capability": "write", "lifecycle": "eager", "cross_linkable": true }}] }}"#,
4679            mem_dir.display(),
4680        );
4681        std::fs::write(&mounts_json, mounts_body).unwrap();
4682
4683        let mut engine = Engine::from_workspace_root(tmp.path()).unwrap();
4684        assert!(
4685            engine.settings().mem_create_rules.is_empty(),
4686            "boot-time settings carry no create rules"
4687        );
4688
4689        // Simulate an out-of-band CLI write to workspace.toml.
4690        std::fs::write(
4691            &workspace_toml,
4692            "format = \"memstead-git-branch-2\"\n\n[persistence_adapter]\nname = \"file-two-layer\"\n\n[[mem_management.create]]\npattern = \"exec-*\"\nschemas = [\"default@1.0.0\"]\n",
4693        )
4694        .unwrap();
4695
4696        engine.reload_each_writable_mem_reports().unwrap();
4697
4698        let rules = &engine.settings().mem_create_rules;
4699        assert_eq!(
4700            rules.len(),
4701            1,
4702            "workspace-wide reload must refresh the policy"
4703        );
4704        assert_eq!(rules[0].pattern, "exec-*");
4705    }
4706
4707    // ---- Engine::reload_if_stale ------------------------------
4708
4709    // ---- set_mem_schema / dual-pin migration ----
4710
4711    const MIG_TYPE_TAIL: &str = r#"sections:
4712  - key: body
4713    heading: Body
4714    required: true
4715    search_weight: 10.0
4716    catch_all: true
4717    write_rules: []
4718title_weight: 100.0
4719text_fields:
4720  - body
4721hierarchy_relationship: _default
4722no_self_loop_relationships: []
4723updatable_fields: []
4724health_required_fields: []
4725staleness_threshold_days: 90
4726write_rules: []
4727"#;
4728
4729    /// Schema manifest for the migration tests: `name@version` with a
4730    /// `doc` type. `with_status = true` adds a required, no-default
4731    /// enum field `status` — entities created without it are
4732    /// non-conformant against that schema.
4733    fn mig_manifest(name: &str, version: &str) -> String {
4734        format!(
4735            r#"name: {name}
4736version: {version}
4737description: migration test schema
4738when_to_use: tests
4739types:
4740  - doc
4741relationships:
4742  mode: strict
4743  definitions:
4744    - name: USES
4745      description: link
4746      default_weight: 1.0
4747    - name: _default
4748      description: fallback
4749      default_weight: 1.0
4750community:
4751  resolution: 1.0
4752  seed: 42
4753"#
4754        )
4755    }
4756
4757    fn mig_type_yaml(with_status: bool) -> String {
4758        let metadata = if with_status {
4759            "metadata_fields:\n  - key: status\n    description: Lifecycle state\n    field_type: string\n    required: true\n    enum_values:\n      - open\n      - closed\n"
4760        } else {
4761            "metadata_fields: []\n"
4762        };
4763        format!("name: doc\ndescription: t\nwhen_to_use: tests\n{metadata}{MIG_TYPE_TAIL}")
4764    }
4765
4766    fn write_mig_schema(
4767        root: &std::path::Path,
4768        dir: &str,
4769        name: &str,
4770        version: &str,
4771        with_status: bool,
4772    ) {
4773        let d = root.join(dir);
4774        std::fs::create_dir_all(d.join("types")).unwrap();
4775        std::fs::write(d.join("schema.yaml"), mig_manifest(name, version)).unwrap();
4776        std::fs::write(d.join("types").join("doc.yaml"), mig_type_yaml(with_status)).unwrap();
4777    }
4778
4779    /// Engine with one mem pinned `mig-a@0.1.0` (no required
4780    /// metadata) plus loadable `mig-a@0.2.0` (identical shape) and
4781    /// `mig-b@0.1.0` (required enum `status`) in the workspace
4782    /// schemas dir. Two conformant-under-A entities are created.
4783    /// The criterion-4 property test (flywheel W8/01): a
4784    /// deterministic, seeded, hand-rolled generator (xorshift64 — the
4785    /// house discipline, no dependency) drives mutation sequences
4786    /// across EVERY kind — create, update, relate, delete, rename,
4787    /// batch update (applied AND refused/rolled-back), reload, and a
4788    /// schema switch — asserting at checkpoints and at sequence end
4789    /// that the maintained derived structures are identical to a
4790    /// from-scratch rebuild over the current store. Coverage is
4791    /// guaranteed by construction (the first pass cycles every kind
4792    /// once before the random tail), and asserted, so a silently
4793    /// narrowed generator fails the suite. A failing sequence
4794    /// reproduces from the seed printed in the panic message alone.
4795    #[test]
4796    fn derived_structures_match_rebuild_across_random_mutation_sequences() {
4797        for seed in [0x5eed_0001_u64, 0x5eed_0002, 0x5eed_0003] {
4798            run_mutation_sequence(seed);
4799        }
4800    }
4801
4802    struct Xorshift(u64);
4803    impl Xorshift {
4804        fn next(&mut self) -> u64 {
4805            let mut x = self.0;
4806            x ^= x << 13;
4807            x ^= x >> 7;
4808            x ^= x << 17;
4809            self.0 = x;
4810            x
4811        }
4812        fn pick(&mut self, n: usize) -> usize {
4813            (self.next() % n as u64) as usize
4814        }
4815    }
4816
4817    fn assert_derived_oracles(engine: &Engine, seed: u64, label: &str) {
4818        // Search oracle: the maintained per-mem index holds exactly
4819        // the ids a from-scratch build over the current store holds.
4820        let fresh = crate::search_index::build_all(engine.store(), &engine.schemas);
4821        let live = engine.search_indexes();
4822        let mut live_mems: Vec<&String> = live.keys().collect();
4823        let mut fresh_mems: Vec<&String> = fresh.keys().collect();
4824        live_mems.sort();
4825        fresh_mems.sort();
4826        assert_eq!(
4827            live_mems, fresh_mems,
4828            "seed {seed:#x} @ {label}: index mem set diverged from rebuild"
4829        );
4830        for (mem, idx) in live {
4831            let mut got = idx.stored_ids().unwrap();
4832            let mut want = fresh[mem].stored_ids().unwrap();
4833            got.sort();
4834            want.sort();
4835            assert_eq!(
4836                got, want,
4837                "seed {seed:#x} @ {label}: mem `{mem}` index contents diverged from rebuild"
4838            );
4839        }
4840        // Community oracle: the memoised partition equals a fresh
4841        // detection with the same parameter source (smallest mem name).
4842        let schema = engine
4843            .schemas
4844            .iter()
4845            .min_by(|a, b| a.0.cmp(b.0))
4846            .map(|(_, s)| s.clone())
4847            .expect("schema present");
4848        let weights_schema = schema.clone();
4849        let fresh_partition = crate::graph::community::detect_communities(
4850            engine.store(),
4851            schema.manifest.community.resolution,
4852            schema.manifest.community.seed,
4853            move |rel_type| {
4854                weights_schema
4855                    .manifest
4856                    .relationships
4857                    .definitions
4858                    .iter()
4859                    .find(|d| d.name == rel_type)
4860                    .map(|d| d.default_weight as f64)
4861                    .unwrap_or(1.0)
4862            },
4863        );
4864        assert_eq!(
4865            engine.communities().entity_cluster_map,
4866            fresh_partition.entity_cluster_map,
4867            "seed {seed:#x} @ {label}: partition diverged from a fresh detection"
4868        );
4869    }
4870
4871    fn run_mutation_sequence(seed: u64) {
4872        use indexmap::IndexMap;
4873
4874        let (_tmp, mut engine) = migration_engine();
4875        let mut rng = Xorshift(seed);
4876        let mut live: Vec<crate::EntityId> = vec![
4877            crate::EntityId::new("specs", "one"),
4878            crate::EntityId::new("specs", "two"),
4879        ];
4880        let mut counter = 0usize;
4881        let mut kinds_hit: std::collections::HashSet<&'static str> =
4882            std::collections::HashSet::new();
4883
4884        const KINDS: [&str; 7] = [
4885            "create",
4886            "update",
4887            "relate",
4888            "delete",
4889            "rename",
4890            "batch_applied",
4891            "batch_refused",
4892        ];
4893
4894        let bare_update = |id: crate::EntityId| crate::engine::UpdateEntityArgs {
4895            anchors: Vec::new(),
4896            anchors_unset: Vec::new(),
4897            id,
4898            expected_hash: None,
4899            sections: IndexMap::new(),
4900            append_sections: IndexMap::new(),
4901            patch_sections: IndexMap::new(),
4902            sections_unset: Vec::new(),
4903            metadata: IndexMap::new(),
4904            metadata_unset: Vec::new(),
4905            declare_relations: Vec::new(),
4906            dry_run: false,
4907            relations_unset: Vec::new(),
4908        };
4909
4910        for op_i in 0..30usize {
4911            // First pass cycles every kind once (coverage by
4912            // construction); the tail is seed-driven.
4913            let kind = *KINDS
4914                .get(op_i)
4915                .unwrap_or_else(|| &KINDS[rng.pick(KINDS.len())]);
4916            match kind {
4917                "create" => {
4918                    counter += 1;
4919                    let mut args = empty_create_args("specs", &format!("Gen {counter}"));
4920                    args.entity_type = "doc".to_string();
4921                    args.sections = IndexMap::from_iter([(
4922                        "body".to_string(),
4923                        format!("generated body {counter}"),
4924                    )]);
4925                    let out = engine
4926                        .create_entity(args, crate::vcs::Actor::Cli, None, None)
4927                        .expect("generated create is conformant");
4928                    live.push(out.id);
4929                    kinds_hit.insert("create");
4930                }
4931                "update" => {
4932                    let id = live[rng.pick(live.len())].clone();
4933                    let mut args = bare_update(id);
4934                    args.append_sections
4935                        .insert("body".to_string(), format!("appended at op {op_i}"));
4936                    engine
4937                        .update_entity(args, crate::vcs::Actor::Cli, None, None)
4938                        .expect("append update is conformant");
4939                    kinds_hit.insert("update");
4940                }
4941                "relate" => {
4942                    if live.len() >= 2 {
4943                        let a = rng.pick(live.len());
4944                        let mut b = rng.pick(live.len());
4945                        if a == b {
4946                            b = (b + 1) % live.len();
4947                        }
4948                        engine
4949                            .relate_entity(
4950                                crate::engine::RelateEntityArgs {
4951                                    source: live[a].clone(),
4952                                    expected_hash: None,
4953                                    rel_type: "USES".to_string(),
4954                                    target: live[b].clone(),
4955                                    remove: false,
4956                                    description: None,
4957                                    dry_run: false,
4958                                },
4959                                crate::vcs::Actor::Cli,
4960                                None,
4961                                None,
4962                            )
4963                            .expect("USES relate is legal under mig-a");
4964                        kinds_hit.insert("relate");
4965                    }
4966                }
4967                "delete" => {
4968                    // Only reference-free entities delete cleanly; keep
4969                    // at least two so relate stays possible.
4970                    if live.len() > 2
4971                        && let Some(pos) = (0..live.len()).find(|&i| {
4972                            engine.store().incoming(&live[i]).is_empty()
4973                                && engine
4974                                    .store()
4975                                    .get(&live[i])
4976                                    .is_some_and(|e| e.relationships.is_empty())
4977                        })
4978                    {
4979                        let id = live.remove(pos);
4980                        engine
4981                            .delete_entity(
4982                                crate::engine::DeleteEntityArgs {
4983                                    id: id.clone(),
4984                                    expected_hash: None,
4985                                },
4986                                crate::vcs::Actor::Cli,
4987                                None,
4988                                None,
4989                            )
4990                            .expect("reference-free delete lands");
4991                        kinds_hit.insert("delete");
4992                    }
4993                }
4994                "rename" => {
4995                    counter += 1;
4996                    let pos = rng.pick(live.len());
4997                    let old = live[pos].clone();
4998                    let out = engine
4999                        .rename_entity(
5000                            crate::engine::RenameEntityArgs {
5001                                id: old,
5002                                new_title: format!("Renamed {counter}"),
5003                                expected_hash: None,
5004                            },
5005                            crate::vcs::Actor::Cli,
5006                            None,
5007                            None,
5008                        )
5009                        .expect("fresh-slug rename lands");
5010                    live[pos] = out.new_id;
5011                    kinds_hit.insert("rename");
5012                }
5013                "batch_applied" => {
5014                    let id_a = live[rng.pick(live.len())].clone();
5015                    let mut a = bare_update(id_a);
5016                    a.append_sections
5017                        .insert("body".to_string(), format!("batch line {op_i}"));
5018                    let result = engine
5019                        .batch_update(vec![(a, None)], crate::vcs::Actor::Cli, None, false)
5020                        .expect("batch envelope");
5021                    assert!(result.applied, "single-entry append batch applies");
5022                    kinds_hit.insert("batch_applied");
5023                }
5024                "batch_refused" => {
5025                    let id_a = live[rng.pick(live.len())].clone();
5026                    let mut a = bare_update(id_a);
5027                    a.append_sections
5028                        .insert("body".to_string(), "doomed".to_string());
5029                    let missing = bare_update(crate::EntityId::new("specs", "no-such-entity"));
5030                    let result = engine
5031                        .batch_update(
5032                            vec![(a, None), (missing, None)],
5033                            crate::vcs::Actor::Cli,
5034                            None,
5035                            false,
5036                        )
5037                        .expect("refused batch returns a report-all envelope");
5038                    assert!(!result.applied, "the missing target refuses the batch");
5039                    kinds_hit.insert("batch_refused");
5040                }
5041                _ => unreachable!(),
5042            }
5043
5044            if op_i == 14 {
5045                // The at-least-one schema switch the criterion demands
5046                // (identical field shape, so the index rebuild is
5047                // exercised through the epoch path).
5048                engine
5049                    .set_mem_schema("specs", &sref("mig-a@0.2.0"))
5050                    .expect("integral switch");
5051                kinds_hit.insert("schema_switch");
5052            }
5053            if op_i == 19 {
5054                engine.reload_one_mem("specs").expect("reload lands");
5055                kinds_hit.insert("reload");
5056            }
5057
5058            if op_i % 10 == 9 {
5059                assert_derived_oracles(&engine, seed, &format!("checkpoint op {op_i}"));
5060            }
5061        }
5062
5063        assert_derived_oracles(&engine, seed, "sequence end");
5064
5065        for kind in KINDS.iter().copied().chain(["schema_switch", "reload"]) {
5066            assert!(
5067                kinds_hit.contains(kind),
5068                "seed {seed:#x}: generator coverage narrowed — kind `{kind}` never executed"
5069            );
5070        }
5071    }
5072
5073    fn migration_engine() -> (tempfile::TempDir, Engine) {
5074        let tmp = tempfile::TempDir::new().unwrap();
5075        let schemas_dir = tmp.path().join("schemas");
5076        write_mig_schema(&schemas_dir, "mig-a-1", "mig-a", "0.1.0", false);
5077        write_mig_schema(&schemas_dir, "mig-a-2", "mig-a", "0.2.0", false);
5078        write_mig_schema(&schemas_dir, "mig-b-1", "mig-b", "0.1.0", true);
5079        let mem_dir = tmp.path().join("mem");
5080        std::fs::create_dir_all(&mem_dir).unwrap();
5081        let writer = crate::storage::FilesystemMemWriter::new(mem_dir.clone());
5082        let mut mount = folder_mount("specs", mem_dir);
5083        mount.schema = Some("mig-a@0.1.0".parse().unwrap());
5084        let mut engine = Engine::from_mounts_with_schemas_dir(
5085            vec![(
5086                mount,
5087                Box::new(writer) as Box<dyn crate::backend::MemBackend>,
5088            )],
5089            Some(&schemas_dir),
5090        )
5091        .unwrap();
5092        for title in ["One", "Two"] {
5093            let mut args = empty_create_args("specs", title);
5094            args.entity_type = "doc".to_string();
5095            args.sections =
5096                indexmap::IndexMap::from_iter([("body".to_string(), "content".to_string())]);
5097            engine
5098                .create_entity(args, crate::vcs::Actor::Cli, None, None)
5099                .expect("conformant create under mig-a");
5100        }
5101        (tmp, engine)
5102    }
5103
5104    fn sref(s: &str) -> memstead_schema::SchemaRef {
5105        s.parse().unwrap()
5106    }
5107
5108    /// A `SCHEMA_PIN_MISMATCH` state (the mount expects the target, the
5109    /// served config pins an older generation) is exactly what
5110    /// `set-schema` must repair: the switch persists the config pin and
5111    /// reports `switched`, never `noop`. Complement: with both in
5112    /// agreement the same call is a noop.
5113    #[test]
5114    fn set_schema_repairs_a_mount_expectation_ahead_of_the_served_pin() {
5115        let (_tmp, mut engine) = migration_engine();
5116        // Fabricate the mismatch: the mount expectation says mig-b while
5117        // the engine still serves mig-a from the config.
5118        let idx = engine
5119            .mounts
5120            .iter()
5121            .position(|m| m.mount.mem == "specs")
5122            .unwrap();
5123        engine.mounts[idx].mount.schema = Some(sref("mig-b@0.1.0"));
5124        assert_eq!(engine.schemas.get("specs").unwrap().id().0, "mig-a");
5125
5126        let out = engine
5127            .set_mem_schema("specs", &sref("mig-b@0.1.0"))
5128            .unwrap();
5129        // The fixture's entities are not integral against mig-b, so the
5130        // honest answer is a started migration; the point is that it is
5131        // NOT the noop the stale mount expectation used to produce.
5132        assert_eq!(
5133            out.outcome,
5134            crate::engine::SetSchemaResult::MigrationStarted,
5135            "a served pin behind the target enters the switch path, never a noop: {out:?}"
5136        );
5137        assert!(!out.findings.is_empty());
5138        assert_eq!(
5139            engine.schemas.get("specs").unwrap().id().0,
5140            "mig-b",
5141            "writes now validate against the target"
5142        );
5143
5144        // Complement: served pin and expectation both at the target (no
5145        // migration in flight) is a noop.
5146        let (_tmp2, mut clean) = migration_engine();
5147        let again = clean.set_mem_schema("specs", &sref("mig-a@0.1.0")).unwrap();
5148        assert_eq!(again.outcome, crate::engine::SetSchemaResult::Noop);
5149    }
5150
5151    #[test]
5152    fn set_schema_noop_on_current_pin() {
5153        let (_tmp, mut engine) = migration_engine();
5154        let out = engine
5155            .set_mem_schema("specs", &sref("mig-a@0.1.0"))
5156            .unwrap();
5157        assert_eq!(out.outcome, crate::engine::SetSchemaResult::Noop);
5158        assert_eq!(out.schema_pin, "mig-a@0.1.0");
5159        assert_eq!(out.migration_target, None);
5160        assert!(out.findings.is_empty());
5161    }
5162
5163    /// Schema-switch invalidation (flywheel W8/01, criterion 2): a
5164    /// schema switch changes NO store content — the store generation
5165    /// stays put — yet both derived memos depend on the schema
5166    /// (community weights, the index field set), so the switch must
5167    /// clear them. The schemas EPOCH is what carries that dependency
5168    /// into the memo key; without it the generation-checked hooks
5169    /// would keep both memos and serve results computed against the
5170    /// old schema (the staleness the whole-map drop used to mask).
5171    #[test]
5172    fn schema_switch_invalidates_both_memos_despite_unchanged_store() {
5173        let (_tmp, mut engine) = migration_engine();
5174
5175        let _ = engine.communities();
5176        let _ = engine.search_indexes();
5177        assert!(engine.community_memo.get().is_some());
5178        assert!(engine.search_indexes_memo.get().is_some());
5179        let store_gen_before = engine.store().generation();
5180
5181        engine
5182            .set_mem_schema("specs", &sref("mig-a@0.2.0"))
5183            .unwrap();
5184
5185        assert_eq!(
5186            engine.store().generation(),
5187            store_gen_before,
5188            "a schema switch mutates no store content"
5189        );
5190        assert!(
5191            engine.community_memo.get().is_none(),
5192            "the community memo must clear on a schema switch (weights derive from the schema)"
5193        );
5194        assert!(
5195            engine.search_indexes_memo.get().is_none(),
5196            "the search memo must clear on a schema switch (the field set derives from the schema)"
5197        );
5198    }
5199
5200    #[test]
5201    fn set_schema_switches_immediately_when_integral() {
5202        // Version bump within the same domain; entities conform to
5203        // the identical-shape 0.2.0, so the switch is immediate.
5204        let (_tmp, mut engine) = migration_engine();
5205        let out = engine
5206            .set_mem_schema("specs", &sref("mig-a@0.2.0"))
5207            .unwrap();
5208        assert_eq!(out.outcome, crate::engine::SetSchemaResult::Switched);
5209        assert_eq!(out.schema_pin, "mig-a@0.2.0");
5210        assert_eq!(out.migration_target, None);
5211        assert!(out.findings.is_empty());
5212        assert_eq!(
5213            engine.schema_pin("specs").unwrap().as_display(),
5214            "mig-a@0.2.0"
5215        );
5216        assert!(engine.migration_target("specs").is_none());
5217    }
5218
5219    /// Regression: an atomic switch must persist the new pin into the
5220    /// **authoritative** backend config, not just `mounts.json`. Boot
5221    /// resolution prefers the backend config's pin over `Mount.schema`,
5222    /// so before this fix the switch evaporated on the next process boot
5223    /// for any config-present mem (every `create_mem`-made mem).
5224    #[test]
5225    fn set_schema_switch_persists_pin_into_backend_config() {
5226        let tmp = tempfile::TempDir::new().unwrap();
5227        let schemas_dir = tmp.path().join("schemas");
5228        write_mig_schema(&schemas_dir, "mig-a-1", "mig-a", "0.1.0", false);
5229        write_mig_schema(&schemas_dir, "mig-a-2", "mig-a", "0.2.0", false);
5230        let mem_dir = tmp.path().join("mem");
5231        std::fs::create_dir_all(mem_dir.join(".memstead")).unwrap();
5232        // Config-present mem: the authoritative pin lives here.
5233        std::fs::write(
5234            mem_dir.join(".memstead").join("config.json"),
5235            br#"{"schema":"mig-a@0.1.0"}"#,
5236        )
5237        .unwrap();
5238        let writer = crate::storage::FilesystemMemWriter::new(mem_dir.clone());
5239        let mut mount = folder_mount("specs", mem_dir.clone());
5240        mount.schema = Some("mig-a@0.1.0".parse().unwrap());
5241        let mut engine = Engine::from_mounts_with_schemas_dir(
5242            vec![(
5243                mount,
5244                Box::new(writer) as Box<dyn crate::backend::MemBackend>,
5245            )],
5246            Some(&schemas_dir),
5247        )
5248        .unwrap();
5249
5250        let out = engine
5251            .set_mem_schema("specs", &sref("mig-a@0.2.0"))
5252            .unwrap();
5253        assert_eq!(out.outcome, crate::engine::SetSchemaResult::Switched);
5254
5255        // The authoritative backend config now carries the new pin —
5256        // otherwise the switch would evaporate on reboot.
5257        let cfg_bytes = std::fs::read(mem_dir.join(".memstead").join("config.json")).unwrap();
5258        let cfg: serde_json::Value = serde_json::from_slice(&cfg_bytes).unwrap();
5259        assert_eq!(
5260            cfg["schema"], "mig-a@0.2.0",
5261            "atomic switch must update the authoritative backend config"
5262        );
5263    }
5264
5265    #[test]
5266    fn set_schema_unknown_target_refuses_schema_not_found() {
5267        let (_tmp, mut engine) = migration_engine();
5268        let err = engine
5269            .set_mem_schema("specs", &sref("nope@9.9.9"))
5270            .unwrap_err();
5271        assert_eq!(err.code(), "SCHEMA_NOT_FOUND");
5272        // No state change.
5273        assert!(engine.migration_target("specs").is_none());
5274    }
5275
5276    #[test]
5277    fn set_schema_migration_lifecycle_end_to_end() {
5278        let (_tmp, mut engine) = migration_engine();
5279        let target = sref("mig-b@0.1.0");
5280
5281        // 1. Non-integral target → migration starts; pin unchanged.
5282        let out = engine.set_mem_schema("specs", &target).unwrap();
5283        assert_eq!(
5284            out.outcome,
5285            crate::engine::SetSchemaResult::MigrationStarted
5286        );
5287        assert_eq!(out.schema_pin, "mig-a@0.1.0");
5288        assert_eq!(out.migration_target.as_deref(), Some("mig-b@0.1.0"));
5289        assert_eq!(out.findings.len(), 2, "both entities lack `status`");
5290        assert!(
5291            out.findings
5292                .iter()
5293                .all(|f| f.code == "REQUIRED_FIELD_UNSET")
5294        );
5295
5296        // 2. Reads of not-yet-repaired entities stay permissive.
5297        let one = crate::entity::EntityId::new("specs", "one");
5298        assert!(engine.store().get(&one).is_some());
5299
5300        // 3. Re-issue while unrepaired → pending, full remaining set.
5301        let out = engine.set_mem_schema("specs", &target).unwrap();
5302        assert_eq!(
5303            out.outcome,
5304            crate::engine::SetSchemaResult::MigrationPending
5305        );
5306        assert_eq!(out.findings.len(), 2);
5307
5308        // 4. Writes validate against the TARGET: `status` is unknown
5309        //    to the pinned mig-a but declared by mig-b — setting it
5310        //    must commit; an invalid enum value must refuse.
5311        let mut bad = crate::engine::UpdateEntityArgs {
5312            anchors: Vec::new(),
5313            id: one.clone(),
5314            expected_hash: None,
5315            sections: indexmap::IndexMap::new(),
5316            append_sections: indexmap::IndexMap::new(),
5317            patch_sections: indexmap::IndexMap::new(),
5318            sections_unset: Vec::new(),
5319            metadata: indexmap::IndexMap::from_iter([("status".to_string(), "banana".to_string())]),
5320            metadata_unset: Vec::new(),
5321            declare_relations: Vec::new(),
5322            dry_run: false,
5323            relations_unset: Vec::new(),
5324            anchors_unset: Vec::new(),
5325        };
5326        let err = engine
5327            .update_entity(bad.clone(), crate::vcs::Actor::Cli, None, None)
5328            .unwrap_err();
5329        assert_eq!(err.code(), "INVALID_ENUM_VALUE", "strict against target");
5330        bad.metadata = indexmap::IndexMap::from_iter([("status".to_string(), "open".to_string())]);
5331        engine
5332            .update_entity(bad, crate::vcs::Actor::Cli, None, None)
5333            .expect("repair write validated against the migration target");
5334
5335        // 5. One entity repaired → still pending, findings shrink.
5336        let out = engine.set_mem_schema("specs", &target).unwrap();
5337        assert_eq!(
5338            out.outcome,
5339            crate::engine::SetSchemaResult::MigrationPending
5340        );
5341        assert_eq!(out.findings.len(), 1, "only `two` remains non-integral");
5342
5343        // 6. Repair the second entity, re-issue → atomic switch.
5344        let two = crate::entity::EntityId::new("specs", "two");
5345        let repair = crate::engine::UpdateEntityArgs {
5346            anchors: Vec::new(),
5347            id: two.clone(),
5348            expected_hash: None,
5349            sections: indexmap::IndexMap::new(),
5350            append_sections: indexmap::IndexMap::new(),
5351            patch_sections: indexmap::IndexMap::new(),
5352            sections_unset: Vec::new(),
5353            metadata: indexmap::IndexMap::from_iter([("status".to_string(), "closed".to_string())]),
5354            metadata_unset: Vec::new(),
5355            declare_relations: Vec::new(),
5356            dry_run: false,
5357            relations_unset: Vec::new(),
5358            anchors_unset: Vec::new(),
5359        };
5360        engine
5361            .update_entity(repair, crate::vcs::Actor::Cli, None, None)
5362            .unwrap();
5363        let out = engine.set_mem_schema("specs", &target).unwrap();
5364        assert_eq!(out.outcome, crate::engine::SetSchemaResult::Switched);
5365        assert_eq!(out.schema_pin, "mig-b@0.1.0");
5366        assert_eq!(out.migration_target, None);
5367        assert!(out.findings.is_empty());
5368        assert_eq!(
5369            engine.schema_pin("specs").unwrap().as_display(),
5370            "mig-b@0.1.0"
5371        );
5372        assert!(engine.migration_target("specs").is_none());
5373    }
5374
5375    /// During migration every not-yet-repaired entity is
5376    /// non-conformant against the target, so `relations_unset` works
5377    /// on exactly those entities with no mode flag — and the same
5378    /// update can complete the entity's repair.
5379    #[test]
5380    fn relations_unset_works_during_migration_without_mode_flag() {
5381        let (_tmp, mut engine) = migration_engine();
5382        let one = crate::entity::EntityId::new("specs", "one");
5383        let two = crate::entity::EntityId::new("specs", "two");
5384        engine
5385            .relate_entity(
5386                crate::engine::RelateEntityArgs {
5387                    source: one.clone(),
5388                    expected_hash: None,
5389                    rel_type: "USES".to_string(),
5390                    target: two.clone(),
5391                    remove: false,
5392                    description: None,
5393                    dry_run: false,
5394                },
5395                crate::vcs::Actor::Cli,
5396                None,
5397                None,
5398            )
5399            .unwrap();
5400        // Conformant under the pin → the repair gate is shut.
5401        let shut = engine
5402            .update_entity(
5403                crate::engine::UpdateEntityArgs {
5404                    anchors: Vec::new(),
5405                    id: one.clone(),
5406                    expected_hash: None,
5407                    sections: indexmap::IndexMap::new(),
5408                    append_sections: indexmap::IndexMap::new(),
5409                    patch_sections: indexmap::IndexMap::new(),
5410                    sections_unset: Vec::new(),
5411                    metadata: indexmap::IndexMap::new(),
5412                    metadata_unset: Vec::new(),
5413                    declare_relations: Vec::new(),
5414                    dry_run: false,
5415                    relations_unset: vec![crate::ops::RelationUnsetArg {
5416                        rel_type: "USES".to_string(),
5417                        target: two.clone(),
5418                    }],
5419                    anchors_unset: Vec::new(),
5420                },
5421                crate::vcs::Actor::Cli,
5422                None,
5423                None,
5424            )
5425            .unwrap_err();
5426        assert_eq!(shut.code(), "REPAIR_NOT_NEEDED");
5427
5428        // Enter migration → `one` is now non-conformant against the
5429        // target; the same call opens, removes the relation, and the
5430        // bundled `status` set makes the entity integral-against-target.
5431        engine
5432            .set_mem_schema("specs", &sref("mig-b@0.1.0"))
5433            .unwrap();
5434        engine
5435            .update_entity(
5436                crate::engine::UpdateEntityArgs {
5437                    anchors: Vec::new(),
5438                    id: one.clone(),
5439                    expected_hash: None,
5440                    sections: indexmap::IndexMap::new(),
5441                    append_sections: indexmap::IndexMap::new(),
5442                    patch_sections: indexmap::IndexMap::new(),
5443                    sections_unset: Vec::new(),
5444                    metadata: indexmap::IndexMap::from_iter([(
5445                        "status".to_string(),
5446                        "open".to_string(),
5447                    )]),
5448                    metadata_unset: Vec::new(),
5449                    declare_relations: Vec::new(),
5450                    dry_run: false,
5451                    relations_unset: vec![crate::ops::RelationUnsetArg {
5452                        rel_type: "USES".to_string(),
5453                        target: two.clone(),
5454                    }],
5455                    anchors_unset: Vec::new(),
5456                },
5457                crate::vcs::Actor::Cli,
5458                None,
5459                None,
5460            )
5461            .expect("repair-shaped update lands during migration without a flag");
5462        let entity = engine.store().get(&one).unwrap();
5463        assert!(entity.relationships.is_empty());
5464    }
5465
5466    /// Boot honors a persisted in-flight migration: a mount carrying
5467    /// `migration_target` validates writes against the target from
5468    /// the first call of the new process — the resumability half of
5469    /// the dual-pin contract.
5470    #[test]
5471    fn boot_resumes_dual_pin_validation_against_target() {
5472        let (tmp, engine) = migration_engine();
5473        drop(engine);
5474        let schemas_dir = tmp.path().join("schemas");
5475        let mem_dir = tmp.path().join("mem");
5476        let writer = crate::storage::FilesystemMemWriter::new(mem_dir.clone());
5477        let mut mount = folder_mount("specs", mem_dir);
5478        mount.schema = Some("mig-a@0.1.0".parse().unwrap());
5479        mount.migration_target = Some("mig-b@0.1.0".parse().unwrap());
5480        let engine = Engine::from_mounts_with_schemas_dir(
5481            vec![(
5482                mount,
5483                Box::new(writer) as Box<dyn crate::backend::MemBackend>,
5484            )],
5485            Some(&schemas_dir),
5486        )
5487        .unwrap();
5488        // Effective validation schema is the target...
5489        let (name, version) = {
5490            let s = engine.schema_for("specs").unwrap();
5491            let (n, v) = s.id();
5492            (n.to_string(), v.to_string())
5493        };
5494        assert_eq!((name.as_str(), version.as_str()), ("mig-b", "0.1.0"));
5495        // ...while the settled pin and the in-flight target read back
5496        // distinctly.
5497        assert_eq!(
5498            engine.schema_pin("specs").unwrap().as_display(),
5499            "mig-a@0.1.0"
5500        );
5501        assert_eq!(
5502            engine.migration_target("specs").unwrap().as_display(),
5503            "mig-b@0.1.0"
5504        );
5505    }
5506
5507    /// Every lifecycle setter refuses `READ_ONLY_MOUNT` on a read-only
5508    /// mount — the family, not an instance. `set_mem_schema` was the
5509    /// one ungated sibling (a schema-pin change starts a migration —
5510    /// the last mutation a sealed mount should accept); this test
5511    /// enumerates all seven current setters — extend it when adding an
5512    /// eighth (the enumeration is manual, not reflective). Refusal complement: the same calls succeed (or fail
5513    /// for their own non-capability reasons) against a writable mount —
5514    /// covered by the existing per-setter tests; `set_mem_schema`'s
5515    /// writable-mount behaviour is pinned by the migration tests above.
5516    #[test]
5517    fn every_lifecycle_setter_refuses_on_read_only_mount() {
5518        let tmp = TempDir::new().unwrap();
5519        let archive_path = build_archive(tmp.path(), "ext", &[("a.md", b"# Title: Foo\n")]);
5520        let mut engine = Engine::from_mounts(vec![(
5521            archive_mount("ext", archive_path.clone()),
5522            Box::new(ArchiveBackend::new(archive_path)) as Box<dyn MemBackend>,
5523        )])
5524        .unwrap();
5525
5526        let default_pin: memstead_schema::SchemaRef = "default@1.0.0".parse().unwrap();
5527        let attempts: Vec<(&str, EngineError)> = vec![
5528            (
5529                "set_mem_schema",
5530                engine.set_mem_schema("ext", &default_pin).unwrap_err(),
5531            ),
5532            (
5533                "set_mem_version",
5534                engine
5535                    .set_mem_version("ext", semver::Version::new(9, 9, 9), None)
5536                    .unwrap_err(),
5537            ),
5538            (
5539                "set_mem_description",
5540                engine
5541                    .set_mem_description("ext", Some("x".into()), None)
5542                    .unwrap_err(),
5543            ),
5544            (
5545                "set_mem_title",
5546                engine
5547                    .set_mem_title("ext", Some("x".into()), None)
5548                    .unwrap_err(),
5549            ),
5550            (
5551                "set_mem_subject",
5552                engine.set_mem_subject("ext", None, None).unwrap_err(),
5553            ),
5554            (
5555                "set_mem_internal",
5556                engine.set_mem_internal("ext", true, None).unwrap_err(),
5557            ),
5558            (
5559                "set_mem_sync_state",
5560                engine
5561                    .set_mem_sync_state("ext", "k", "t", None)
5562                    .unwrap_err(),
5563            ),
5564        ];
5565        for (setter, err) in attempts {
5566            match err {
5567                EngineError::ReadOnlyMount(v) => {
5568                    assert_eq!(v, "ext", "{setter} must name the refused mem")
5569                }
5570                other => panic!("{setter} must refuse ReadOnlyMount, got {other:?}"),
5571            }
5572        }
5573    }
5574
5575    /// 04/03, criteria 1 and 2, on the folder backend. Every one of the
5576    /// lifecycle setters, each against a config a sibling moved after boot.
5577    /// The loop is the point: the criterion is the whole set behind one
5578    /// implementation, so a test that exercised one setter would pass while
5579    /// the other six stayed broken.
5580    #[test]
5581    fn no_config_setter_reverts_a_siblings_write() {
5582        type Setter = fn(&mut Engine) -> Result<(), EngineError>;
5583        let setters: Vec<(&str, Setter)> = vec![
5584            ("version", |e| {
5585                e.set_mem_version("specs", semver::Version::new(9, 0, 0), None)
5586                    .map(|_| ())
5587            }),
5588            ("description", |e| {
5589                e.set_mem_description("specs", Some("mine".into()), None)
5590                    .map(|_| ())
5591            }),
5592            ("title", |e| {
5593                e.set_mem_title("specs", Some("Mine".into()), None)
5594                    .map(|_| ())
5595            }),
5596            ("internal", |e| {
5597                e.set_mem_internal("specs", true, None).map(|_| ())
5598            }),
5599            ("sync_state", |e| {
5600                e.set_mem_sync_state("specs", "src/facet", "tok", None)
5601                    .map(|_| ())
5602            }),
5603            // Cleared rather than set: the mark validates against a real
5604            // commit cursor, and the clear path writes config just the same,
5605            // which is what this test is about.
5606            ("review_mark", |e| {
5607                e.set_review_mark("specs", None, None).map(|_| ())
5608            }),
5609        ];
5610
5611        for (name, set) in setters {
5612            let tmp = TempDir::new().unwrap();
5613            let mem_dir = tmp.path().to_path_buf();
5614            let meta = mem_dir.join(memstead_schema::MEM_META_DIR);
5615            std::fs::create_dir_all(&meta).unwrap();
5616            let path = meta.join("config.json");
5617            std::fs::write(
5618                &path,
5619                br#"{"schema": "default@1.0.0", "version": "0.1.0"}"#.as_slice(),
5620            )
5621            .unwrap();
5622
5623            let writer = FilesystemMemWriter::new(mem_dir.clone());
5624            let mut engine = Engine::from_mounts(vec![(
5625                folder_mount("specs", mem_dir.clone()),
5626                Box::new(writer) as Box<dyn MemBackend>,
5627            )])
5628            .unwrap();
5629            // The review-mark setter validates its cursor against a real
5630            // entity, so seed one before the sibling write.
5631            engine
5632                .create_entity(
5633                    crate::engine::test_helpers::empty_create_args("specs", "Seed"),
5634                    crate::vcs::Actor::Cli,
5635                    None,
5636                    None,
5637                )
5638                .unwrap();
5639
5640            // A sibling writes a field this engine has never seen.
5641            let mut sibling: memstead_schema::MemConfig =
5642                serde_json::from_slice(&std::fs::read(&path).unwrap()).unwrap();
5643            sibling
5644                .extra
5645                .insert("siblingMark".into(), serde_json::json!("kept"));
5646            std::fs::write(&path, serde_json::to_vec_pretty(&sibling).unwrap()).unwrap();
5647
5648            set(&mut engine).unwrap_or_else(|e| panic!("{name} setter failed: {e}"));
5649
5650            let after: memstead_schema::MemConfig =
5651                serde_json::from_slice(&std::fs::read(&path).unwrap()).unwrap();
5652            assert_eq!(
5653                after.extra.get("siblingMark"),
5654                Some(&serde_json::json!("kept")),
5655                "the {name} setter reverted a field it never set"
5656            );
5657        }
5658    }
5659
5660    /// Criterion 3, and its complement 4: the intervention is reported on the
5661    /// operation's own response, and only when there was one.
5662    #[test]
5663    fn intervention_is_reported_on_the_response_and_only_when_real() {
5664        let tmp = TempDir::new().unwrap();
5665        let mem_dir = tmp.path().to_path_buf();
5666        let meta = mem_dir.join(memstead_schema::MEM_META_DIR);
5667        std::fs::create_dir_all(&meta).unwrap();
5668        let path = meta.join("config.json");
5669        std::fs::write(&path, br#"{"schema": "default@1.0.0"}"#.as_slice()).unwrap();
5670        let writer = FilesystemMemWriter::new(mem_dir.clone());
5671        let mut engine = Engine::from_mounts(vec![(
5672            folder_mount("specs", mem_dir.clone()),
5673            Box::new(writer) as Box<dyn MemBackend>,
5674        )])
5675        .unwrap();
5676
5677        // Single writer: no report. This is the ordinary path, and a fix that
5678        // cried intervention here would be worse than the bug.
5679        let quiet = engine
5680            .set_mem_description("specs", Some("first".into()), None)
5681            .unwrap();
5682        assert!(
5683            !quiet
5684                .warnings
5685                .iter()
5686                .any(|w| w.code() == "CONFIG_WRITE_INTERVENED"),
5687            "single-writer workspace must stay silent: {:?}",
5688            quiet.warnings
5689        );
5690
5691        // A sibling intervenes; the next write says so, naming the field.
5692        let mut sibling: memstead_schema::MemConfig =
5693            serde_json::from_slice(&std::fs::read(&path).unwrap()).unwrap();
5694        sibling.title = Some("theirs".into());
5695        std::fs::write(&path, serde_json::to_vec_pretty(&sibling).unwrap()).unwrap();
5696
5697        let loud = engine
5698            .set_mem_description("specs", Some("second".into()), None)
5699            .unwrap();
5700        let hint = loud
5701            .warnings
5702            .iter()
5703            .find(|w| w.code() == "CONFIG_WRITE_INTERVENED")
5704            .expect("intervention must be reported on the response");
5705        assert!(
5706            format!("{hint}").contains("title"),
5707            "the report names what they changed: {hint}"
5708        );
5709        // And theirs survived, which is the point of reporting rather than refusing.
5710        let after: memstead_schema::MemConfig =
5711            serde_json::from_slice(&std::fs::read(&path).unwrap()).unwrap();
5712        assert_eq!(after.title.as_deref(), Some("theirs"));
5713        assert_eq!(after.description.as_deref(), Some("second"));
5714    }
5715}