Skip to main content

memstead_base/
lib.rs

1//! Backend-agnostic graph kernel for Memstead.
2//!
3//! Hosts the parts of the engine that do not assume a git-backed
4//! storage: token-budget chunking, workspace-root utilities, the
5//! entity types and markdown pipeline (parse / generate / write), the
6//! in-memory store, graph queries and community detection, the mem
7//! router, the operation request/response types (including the
8//! gix-free read paths in `ops::health` / `ops::search`), the search
9//! index, validators, and rendering.
10//!
11//! Consumers that need only graph operations against directory or
12//! archive storage depend on this crate alone — its dependency closure
13//! does not include `gix`. Code that needs git-backed mems depends on
14//! `memstead-git-branch`, which builds on `memstead-base`.
15
16pub mod anchor;
17pub mod backend;
18pub mod binding;
19pub mod binding_intent;
20pub mod binding_run;
21pub mod build_info;
22pub mod check;
23pub mod check_path;
24pub mod chunking;
25pub mod derivation;
26pub mod domain_authority_wire;
27pub mod engine;
28pub mod entity;
29pub mod error;
30pub mod filesystem;
31pub mod friction;
32pub mod graph;
33pub mod markdown;
34pub mod mem;
35pub mod mem_management;
36pub mod ops;
37pub mod overview;
38pub mod pipeline;
39pub mod pipeline_edit;
40pub mod pipeline_store;
41pub mod preparation;
42pub mod provenance;
43pub mod render;
44pub mod runtime_validator;
45pub mod schema_source;
46#[cfg(not(target_arch = "wasm32"))]
47pub mod search_index;
48pub mod section_format;
49pub mod source_scope;
50pub mod storage;
51pub mod store;
52pub mod validator;
53pub mod vcs;
54pub mod workspace;
55pub mod workspace_config_edit;
56pub mod workspace_create;
57pub mod workspace_root;
58pub mod workspace_store;
59
60use std::sync::Arc;
61
62use memstead_schema::{TypeDefinition, type_by_name};
63
64pub use anchor::{
65    ANCHOR_SIDECAR_PATH, Anchor, AnchorGrain, AnchorHashStability, AnchorInput,
66    AnchorProvenanceClass, AnchorSidecar, AnchorState, AnchorValidationError, AnchorVersion,
67    ArtifactObservation, EntityAnchorComposition, INVALID_ANCHOR_CODE, compose_entity_anchors,
68    resolve_anchor,
69};
70pub use backend::{BackendError, MemBackend};
71pub use engine::drift::{EngineHandle, OperationScope};
72pub use engine::{
73    BackendFactory, BootError, CreateEntityArgs, CreateEntityOutcome, DeleteEntityArgs,
74    DeleteEntityOutcome, DeleteReferrers, DisjointMerge, Engine, EngineError, EntityHistoryReport,
75    EntityTouch, ExpectedHash, FromArchiveBytesError, GitBranchBranchResetFn,
76    GitBranchChangesSinceFn, GitBranchCreateBranchAtFn, GitBranchDiffFn, GitBranchExportFn,
77    GitBranchExportToBytesFn, GitBranchFetchFn, GitBranchLsRemoteFn, GitBranchMergeDisjointFn,
78    GitBranchOps, GitBranchPullFn, GitBranchPushFn, GitBranchReadConfigAtRefFn,
79    GitBranchReadTreeFn, GitBranchResolveRefFn, HISTORY_PAGE_DEFAULT, HISTORY_PAGE_MAX,
80    HashOverride, INLINE_LIST_CAP, MutationClock, ReferrerInfo, RelateAction, RelateEntityArgs,
81    RelateEntityOutcome, RenameEntityArgs, RenameEntityOutcome, RetypeEdge, RetypeEdgeDirection,
82    RetypeEntityArgs, RetypeEntityOutcome, RetypeProblem, ReviewMarkStatus, SchemaSourceDiagnostic,
83    SchemaStaging, SetReviewMarkOutcome, StoryStart, UpdateEntityArgs, UpdateEntityOutcome,
84    format_inline_list_overflow,
85};
86pub use entity::id::{ENTITY_ID_MAX_LEN, SlugError, TITLE_GRAMMAR_RULE};
87pub use entity::parser::{
88    Frontmatter, body_after_frontmatter, frontmatter_parts, peek_type_from_frontmatter,
89    split_frontmatter_core,
90};
91pub use entity::{Entity, EntityId, MetadataValue, ParseResult, Relationship};
92pub use error::{FullEngineError, RecoveryAction};
93pub use graph::{ClusterInfo, LouvainOutput};
94pub use mem::{MEM_META_DIR, MemOrigin, MemRouterSnapshot};
95
96/// The engine crate SEMVER of the running binary. Version-carrying
97/// surfaces — the per-mem mutation stamp (`MemConfig.mutation_stamp`),
98/// the boot-time `ENGINE_VERSION_SKEW` comparison, the overview's
99/// `_engine_version`, CLI `--version`, MCP `serverInfo.version` — use
100/// [`build_info::full_version`] instead (semver plus the git build sha
101/// when present), so dev builds between releases stay distinguishable;
102/// this constant is the bare semver component.
103pub const ENGINE_VERSION: &str = env!("CARGO_PKG_VERSION");
104pub use mem_management::{CreateRuleSet, DeleteRuleSet, MatcherSet, MatcherSetError};
105pub use ops::{
106    BackendChanges, BatchEntry, BatchError, BatchResult, ChangeEnvelope, ChangesReport,
107    ContextResult, CreateArgs, CreateResult, DeleteResult, EMPTY_TREE_SHA, ExportResult, Facets,
108    HealthReport, HealthSummary, ListResult, MemExportResult, ModifiedMetadata, ModifiedSections,
109    Query, RENAME_SIMILARITY_DEFAULT, RENAME_SIMILARITY_MAX, RENAME_SIMILARITY_MIN, RelateArg,
110    RelateResult, ReloadReport, ReloadResult, RenameResult, SearchHit, SearchResult, SearchScope,
111    SetMemVersionOutcome, UpdateArgs, UpdateResult, WarningHint,
112};
113pub use pipeline::{IngestTrigger, MediumType, PatternEntry, PatternMode};
114pub use pipeline_edit::PipelineEditError;
115pub use pipeline_store::{BindingConfigs, MemPipelineRecord, load_pipeline_configs};
116pub use provenance::{Provenance, ProvenanceKind};
117pub use store::{Edge, EdgeSource, InEdge, Store};
118pub use workspace::{
119    CreateRuleSetting, DeleteRuleSetting, MEMSTEAD_CHECKS_REF_BRANCH, MEMSTEAD_REF_BRANCH, Mount,
120    MountCapability, MountLifecycle, MountStorage, SCHEMA_WILDCARD, Workspace, WorkspaceSettings,
121    branch_full_ref, branch_short_name, checks_ref_member_path,
122};
123pub use workspace_store::{
124    FileWorkspaceStore, InstantiateError, Layout, StoreError, WORKSPACE_STORE_DIR,
125    WorkspaceStoreAdapter, detect_layout, instantiate_local_backend, is_mem_repo_shaped,
126    is_workspace_root, standalone_workspace, workspace_shape_label,
127};
128
129/// Lowercase hex encoding of a digest. Byte-identical to the `{:x}`
130/// formatting that `sha2 < 0.11` digests carried; hash strings persisted
131/// under the old encoding must keep matching.
132pub fn hex_lower(bytes: &[u8]) -> String {
133    use std::fmt::Write;
134    bytes
135        .iter()
136        .fold(String::with_capacity(bytes.len() * 2), |mut s, b| {
137            let _ = write!(s, "{b:02x}");
138            s
139        })
140}
141
142/// Engine-wide sentinel type. Used by ops that need a shared reference
143/// type at the Engine level (search, health, list) — always resolves to
144/// `default@1.0.0::spec`. Per-entity resolution still goes through
145/// `type_by_name` + schema lookup.
146pub fn engine_fallback_type() -> Arc<TypeDefinition> {
147    type_by_name("spec").expect("spec type must exist in the default builtin schema")
148}