memfork_core/lib.rs
1//! The MemFork engine: branchable in-memory state.
2//!
3//! An embedded database for the memory an agent accumulates while it works.
4//! Store and recall values, then **fork** the whole database before a risky
5//! step, **merge** the fork if the attempt worked, **discard** it if it did
6//! not, and **rewind** to how things were at any earlier point.
7//!
8//! Forking is constant time and constant space whatever the database holds,
9//! because branches share structure rather than copying it. Nothing here uses
10//! the operating system's `fork()`, copy-on-write pages or a filesystem
11//! snapshot: branching is persistent data structures and nothing else, which
12//! is why it behaves identically on Windows, macOS and Linux.
13//!
14//! This crate is the engine on its own — no I/O, no async, no network. The
15//! [`memfork`](https://crates.io/crates/memfork) crate builds an MCP server, a
16//! shared daemon and a command line on top of it. The project README has the
17//! wider picture: <https://github.com/memforkdb/memfork>.
18//!
19//! # Example
20//!
21//! ```
22//! use memfork_core::{Db, Value};
23//!
24//! let db = Db::new();
25//! db.put("main", "note:1", Value::new("the plan so far"))?;
26//!
27//! // Fork before doing something that might not work out.
28//! db.fork("main", "attempt")?;
29//! db.put("attempt", "note:1", Value::new("a risky rewrite"))?;
30//!
31//! // The parent branch cannot see the attempt.
32//! let on_main = db.get("main", "note:1")?;
33//! assert_eq!(on_main.map(|e| e.value.clone()), Some("the plan so far".into()));
34//!
35//! // It did not work out: throw the whole attempt away.
36//! db.discard("attempt")?;
37//! # Ok::<(), memfork_core::Error>(())
38//! ```
39//!
40//! Swap [`Db::discard`] for [`Db::merge`] and the change comes back to the
41//! parent instead, resolved key by key against the point the branches
42//! diverged.
43//!
44//! # What it guarantees
45//!
46//! - **Forking costs the same whatever it holds.** [`Db::fork`] copies a
47//! pointer, and the two branches share everything they have in common until
48//! one of them changes.
49//! - **A fork is invisible until it is merged.** Writes on a branch cannot be
50//! seen from the branch it came from, and [`Db::discard`] leaves that branch
51//! exactly as it was.
52//! - **You can read the past.** [`Db::at`] reads a branch as of any retained
53//! commit, and [`Db::fork_at`] branches from there.
54//! - **A dropped transaction leaves nothing behind.** A [`Txn`] that is not
55//! committed moves no head, writes no commit and advances no counter.
56//! - **The same operations give the same answers everywhere.** Commit ids are
57//! `blake3(parents ‖ message ‖ canonically encoded ops)`, and search breaks
58//! ties on the key, so ids and orderings match across machines and runs.
59//!
60//! # Where the data goes
61//!
62//! Nowhere, unless you ask. This crate keeps everything in memory and performs
63//! no I/O of its own; durability is a [`Journal`] the caller supplies, which is
64//! how the `memfork` binary writes a checksummed log without the engine ever
65//! touching a file. Long-lived memory also needs a limit, so
66//! [`Db::enforce_budget`] evicts by importance and recency together and records
67//! what it dropped as [`Op::Evict`] rather than as an ordinary delete.
68//!
69//! The design is documented in
70//! [`docs/DESIGN.md`](https://github.com/memforkdb/memfork/blob/main/docs/DESIGN.md).
71
72#![warn(missing_docs)]
73
74mod access;
75mod commit;
76mod db;
77mod encode;
78mod entry;
79mod error;
80mod evict;
81mod id;
82pub mod journal;
83mod merge;
84mod search;
85mod store;
86mod txn;
87mod view;
88
89pub use commit::{Commit, LogEntry};
90pub use db::{validate_branch_name, Db, Discarded, DEFAULT_BRANCH, DISCARDS_REMEMBERED};
91pub use entry::{validate_key, Entry, Op, Value, DEFAULT_IMPORTANCE, MAX_KEY_BYTES, WRITTEN_BY};
92pub use error::{Error, Result};
93pub use evict::{entry_size, score, Evicted, EvictionConfig, OnEvict};
94pub use id::CommitId;
95pub use journal::{DecodeError, Journal, JournalError, Record};
96pub use merge::{MergeKind, MergeOutcome, MergePolicy};
97pub use search::{cosine, SearchHit};
98pub use store::{Root, Store};
99pub use txn::{with_txn, Txn};
100pub use view::{BranchInfo, Change, ChangeKind, ReadView};
101
102/// The version of this crate.
103pub const VERSION: &str = env!("CARGO_PKG_VERSION");
104
105/// The README is a contract, so its examples are compiled
106/// and run as doctests rather than left to rot.
107#[cfg(doctest)]
108#[doc = include_str!("../README.md")]
109pub struct ReadmeExamples;
110
111/// And the same for the one at the root of the repository, which is the one
112/// most people read.
113#[cfg(doctest)]
114#[doc = include_str!("../../../README.md")]
115pub struct ProjectReadmeExamples;