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