Skip to main content

create_mem

Function create_mem 

Source
pub fn create_mem(
    engine: &mut Engine,
    params: MemCreateParams,
) -> Result<MemCreateResponse, FullEngineError>
Expand description

Create a new writable mem at runtime. Unified counterpart to full’s memstead_git_branch::mem_management::create_mem. Routes through the engine’s installed memstead_base::BackendFactory so the same call site materialises folder, archive, or git-branch backends transparently — production full consumers install memstead_git_branch::storage::instantiate_full_backend at boot via engine_from_workspace_root.

Pipeline: 0. Input validation (note length, optional path segments).

  • 0b. Schema canonicalization against the built-in catalogue. Workspace-authored schemas are resolved by the workspace’s schemas_dir, not yet here.
  1. Canonicalize location against engine.workspace_root() (relative paths) or take absolute paths as-is.
    • 1a. Allowlist match against the composed candidate (<path>/<name> when path is Some, else <name>).
    • 1b. Schema gate against matched rule’s schemas list. ["*"] wildcard admits any schema.
    • 1c. Basename invariant: params.name MUST equal the canonical location’s basename.
  2. Name collision probe against the current mem_router snapshot. The rich tree-walk collision detector (with colliding_paths envelope payload) is full-only; unified surfaces collisions through the snapshot probe with the same EngineError::MemNameCollision discriminant.
  3. Build memstead_schema::config::MemConfig bytes.
    • 3b. Pick the storage variant: params.storage when set (explicit StorageKind override — folder mems beside git-branch mems in one workspace), else by workspace shape (git-branch when <workspace_root>/mem-repo/.git/ exists, folder otherwise). The branch leaf composes as <path>/<name>.
  4. Write <location>/.memstead/config.json for folder mounts only. Git-branch mounts skip the on-disk write — the per-mem config travels in the workspace’s __MEMSTEAD registry ref.
  5. Materialise the backend via the engine’s memstead_base::BackendFactory, commit the seed (real sha for git-branch, synthetic id for folder), and register via memstead_base::Engine::register_writable_mem with memstead_base::MemOrigin::RuntimeCreated.