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_root;
57pub mod workspace_store;
58
59use std::sync::Arc;
60
61use memstead_schema::{TypeDefinition, type_by_name};
62
63pub use anchor::{
64    ANCHOR_SIDECAR_PATH, Anchor, AnchorGrain, AnchorHashStability, AnchorInput,
65    AnchorProvenanceClass, AnchorSidecar, AnchorState, AnchorValidationError, AnchorVersion,
66    ArtifactObservation, EntityAnchorComposition, INVALID_ANCHOR_CODE, compose_entity_anchors,
67    resolve_anchor,
68};
69pub use backend::{BackendError, MemBackend};
70pub use engine::drift::{EngineHandle, OperationScope};
71pub use engine::{
72    BackendFactory, BootError, CreateEntityArgs, CreateEntityOutcome, DeleteEntityArgs,
73    DeleteEntityOutcome, DeleteReferrers, Engine, EngineError, EntityHistoryReport, EntityTouch,
74    FromArchiveBytesError, GitBranchBranchResetFn, GitBranchChangesSinceFn,
75    GitBranchCreateBranchAtFn, GitBranchDiffFn, GitBranchExportFn, GitBranchExportToBytesFn,
76    GitBranchFetchFn, GitBranchLsRemoteFn, GitBranchOps, GitBranchPullFn, GitBranchPushFn,
77    GitBranchReadConfigAtRefFn, GitBranchReadTreeFn, GitBranchResolveRefFn, HISTORY_PAGE_DEFAULT,
78    HISTORY_PAGE_MAX, INLINE_LIST_CAP, MutationClock, ReferrerInfo, RelateAction, RelateEntityArgs,
79    RelateEntityOutcome, RenameEntityArgs, RenameEntityOutcome, RetypeEdge, RetypeEdgeDirection,
80    RetypeEntityArgs, RetypeEntityOutcome, RetypeProblem, ReviewMarkStatus, SchemaSourceDiagnostic,
81    SchemaStaging, SetReviewMarkOutcome, StoryStart, UpdateEntityArgs, UpdateEntityOutcome,
82    format_inline_list_overflow,
83};
84pub use entity::id::{ENTITY_ID_MAX_LEN, SlugError, TITLE_GRAMMAR_RULE};
85pub use entity::parser::{
86    Frontmatter, body_after_frontmatter, frontmatter_parts, peek_type_from_frontmatter,
87    split_frontmatter_core,
88};
89pub use entity::{Entity, EntityId, MetadataValue, ParseResult, Relationship};
90pub use error::{FullEngineError, RecoveryAction};
91pub use graph::{ClusterInfo, LouvainOutput};
92pub use mem::{MEM_META_DIR, MemOrigin, MemRouterSnapshot};
93
94/// The engine crate SEMVER of the running binary. Version-carrying
95/// surfaces — the per-mem mutation stamp (`MemConfig.mutation_stamp`),
96/// the boot-time `ENGINE_VERSION_SKEW` comparison, the overview's
97/// `_engine_version`, CLI `--version`, MCP `serverInfo.version` — use
98/// [`build_info::full_version`] instead (semver plus the git build sha
99/// when present), so dev builds between releases stay distinguishable;
100/// this constant is the bare semver component.
101pub const ENGINE_VERSION: &str = env!("CARGO_PKG_VERSION");
102pub use mem_management::{CreateRuleSet, DeleteRuleSet, MatcherSet, MatcherSetError};
103pub use ops::{
104    BackendChanges, BatchEntry, BatchError, BatchResult, ChangeEnvelope, ChangesReport,
105    ContextResult, CreateArgs, CreateResult, DeleteResult, EMPTY_TREE_SHA, ExportResult, Facets,
106    HealthReport, HealthSummary, ListResult, MemExportResult, ModifiedMetadata, ModifiedSections,
107    Query, RENAME_SIMILARITY_DEFAULT, RENAME_SIMILARITY_MAX, RENAME_SIMILARITY_MIN, RelateArg,
108    RelateResult, ReloadReport, ReloadResult, RenameResult, SearchHit, SearchResult, SearchScope,
109    SetMemVersionOutcome, UpdateArgs, UpdateResult, WarningHint,
110};
111pub use pipeline::{IngestTrigger, MediumType, PatternEntry, PatternMode};
112pub use pipeline_edit::PipelineEditError;
113pub use pipeline_store::{BindingConfigs, MemPipelineRecord, load_pipeline_configs};
114pub use provenance::{Provenance, ProvenanceKind};
115pub use store::{Edge, EdgeSource, InEdge, Store};
116pub use workspace::{
117    CreateRuleSetting, DeleteRuleSetting, MEMSTEAD_CHECKS_REF_BRANCH, MEMSTEAD_REF_BRANCH, Mount,
118    MountCapability, MountLifecycle, MountStorage, SCHEMA_WILDCARD, Workspace, WorkspaceSettings,
119    branch_full_ref, branch_short_name, checks_ref_member_path,
120};
121pub use workspace_store::{
122    FileWorkspaceStore, InstantiateError, Layout, StoreError, WORKSPACE_STORE_DIR,
123    WorkspaceStoreAdapter, detect_layout, instantiate_local_backend, is_mem_repo_shaped,
124    is_workspace_root, standalone_workspace, workspace_shape_label,
125};
126
127/// Lowercase hex encoding of a digest. Byte-identical to the `{:x}`
128/// formatting that `sha2 < 0.11` digests carried; hash strings persisted
129/// under the old encoding must keep matching.
130pub fn hex_lower(bytes: &[u8]) -> String {
131    use std::fmt::Write;
132    bytes
133        .iter()
134        .fold(String::with_capacity(bytes.len() * 2), |mut s, b| {
135            let _ = write!(s, "{b:02x}");
136            s
137        })
138}
139
140/// Engine-wide sentinel type. Used by ops that need a shared reference
141/// type at the Engine level (search, health, list) — always resolves to
142/// `default@1.0.0::spec`. Per-entity resolution still goes through
143/// `type_by_name` + schema lookup.
144pub fn engine_fallback_type() -> Arc<TypeDefinition> {
145    type_by_name("spec").expect("spec type must exist in the default builtin schema")
146}