velesdb-memory 0.14.1

VelesDB-memory: local-first MCP memory server for AI agents (remember/recall/relate/forget/why + deterministic context compiler).
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
use std::fs::{File, OpenOptions};
use std::io::{Seek, SeekFrom, Write};
use std::path::{Path, PathBuf};

use parking_lot::Mutex;

use super::{DirtyKey, MutationObserver};
use crate::MemoryError;

mod format;
mod identity;
mod io;
mod platform;

use identity::validate_epoch_id;
pub(crate) use identity::{CutoverIdentity, EpochIdentity};

use format::{
    encode_record, read_header, read_records, recover_torn_tail, scan_records, write_header,
    EncodedRecord, JournalHeader, FORMAT_VERSION,
};
pub(super) use format::{JournalRecord, RECORD_BYTES};
use io::{append_synced, write_compacted};
use platform::{durability_barrier, promote};

pub(super) const JOURNAL_FILE: &str = "online-migration-dirty.journal";
const STAGING_FILE: &str = "online-migration-dirty.journal.tmp";
const MAX_READ_BATCH: usize = 4_096;

struct JournalInner {
    file: Option<File>,
    header: JournalHeader,
    header_bytes: u64,
    last_sequence: u64,
    poisoned: bool,
}

struct LoadedJournal {
    file: File,
    header: JournalHeader,
    header_bytes: u64,
    last_sequence: u64,
}

pub(crate) struct DirtyJournal {
    workspace: PathBuf,
    path: PathBuf,
    max_bytes: u64,
    inner: Mutex<JournalInner>,
    #[cfg(test)]
    fault: std::sync::atomic::AtomicU8,
}

impl DirtyJournal {
    pub(crate) fn open(
        workspace: &Path,
        identity: &EpochIdentity,
        max_bytes: u64,
    ) -> Result<Self, MemoryError> {
        let path = prepare_journal(workspace, identity)?;
        let loaded = load_journal(&path)?;
        verify_identity(&loaded.header, identity)?;
        validate_capacity(loaded.header_bytes, max_bytes)?;
        Ok(Self {
            workspace: workspace.to_owned(),
            path,
            max_bytes,
            inner: Mutex::new(JournalInner {
                file: Some(loaded.file),
                header: loaded.header,
                header_bytes: loaded.header_bytes,
                last_sequence: loaded.last_sequence,
                poisoned: false,
            }),
            #[cfg(test)]
            fault: std::sync::atomic::AtomicU8::new(0),
        })
    }

    pub(crate) fn last_sequence(&self) -> u64 {
        self.inner.lock().last_sequence
    }

    pub(crate) fn compacted_through(&self) -> u64 {
        self.inner.lock().header.compacted_through
    }

    pub(crate) fn verify_cutover_identity(
        &self,
        expected: &CutoverIdentity<'_>,
    ) -> Result<(), MemoryError> {
        let inner = self.inner.lock();
        let identity = &inner.header.identity;
        if identity.source_path() != expected.source
            || identity.destination_path() != expected.destination
            || identity.source_provenance() != expected.source_provenance
            || identity.target_model() != expected.target_model
            || identity.target_dimension() != expected.target_dimension
            || identity.target_witness() != expected.target_witness
            || identity.epoch_id() != expected.epoch_id
        {
            return Err(capture("cutover identity disagrees with journal epoch"));
        }
        Ok(())
    }

    pub(crate) fn workspace(&self) -> &Path {
        &self.workspace
    }

    pub(crate) fn records_after(
        &self,
        sequence: u64,
        limit: usize,
    ) -> Result<Vec<JournalRecord>, MemoryError> {
        let inner = self.inner.lock();
        ensure_healthy(&inner)?;
        let mut file =
            File::open(&self.path).map_err(|err| capture(format!("cannot read journal: {err}")))?;
        file.seek(SeekFrom::Start(inner.header_bytes))
            .map_err(|err| capture(format!("cannot seek journal: {err}")))?;
        read_records(&mut file, sequence, limit.min(MAX_READ_BATCH))
    }

    pub(crate) fn compact_through(&self, watermark: u64) -> Result<(), MemoryError> {
        let mut inner = self.inner.lock();
        ensure_healthy(&inner)?;
        if watermark < inner.header.compacted_through || watermark > inner.last_sequence {
            return Err(capture(format!("invalid compaction watermark {watermark}")));
        }
        if watermark == inner.header.compacted_through {
            return Ok(());
        }
        let result = self.compact_locked(&mut inner, watermark);
        if result.is_err() {
            inner.poisoned = true;
        }
        result
    }

    fn compact_locked(&self, inner: &mut JournalInner, watermark: u64) -> Result<(), MemoryError> {
        let next_header = next_header(&inner.header, watermark)?;
        let staging = self.workspace.join(STAGING_FILE);
        let header_bytes =
            write_compacted(&self.path, &staging, &next_header, watermark, |point| {
                self.maybe_fail(point)
            })?;
        self.publish_compaction(inner, &staging, next_header, header_bytes)
    }

    fn publish_compaction(
        &self,
        inner: &mut JournalInner,
        staging: &Path,
        next_header: JournalHeader,
        header_bytes: u64,
    ) -> Result<(), MemoryError> {
        self.maybe_fail(FaultPoint::BeforeCompactionReplace)?;
        inner.file.take();
        promote(staging, &self.path)
            .map_err(|err| capture(format!("cannot replace journal generation: {err}")))?;
        self.maybe_fail(FaultPoint::AfterCompactionReplace)?;
        self.maybe_fail(FaultPoint::BeforeDirectorySync)?;
        durability_barrier(&self.workspace)
            .map_err(|err| capture(format!("cannot sync journal directory: {err}")))?;
        self.maybe_fail(FaultPoint::AfterDirectorySync)?;
        let file = OpenOptions::new()
            .read(true)
            .write(true)
            .open(&self.path)
            .map_err(|err| capture(format!("cannot reopen compacted journal: {err}")))?;
        inner.file = Some(file);
        inner.header = next_header;
        inner.header_bytes = header_bytes;
        Ok(())
    }

    fn append(&self, key: DirtyKey) -> Result<(), MemoryError> {
        let mut inner = self.inner.lock();
        ensure_healthy(&inner)?;
        let (sequence, record) = prepare_append(&inner, self.max_bytes, key)?;
        let result = self.append_locked(&mut inner, &record);
        if let Err(error) = result {
            inner.poisoned = true;
            return Err(error);
        }
        inner.last_sequence = sequence;
        Ok(())
    }

    fn append_locked(&self, inner: &mut JournalInner, record: &[u8]) -> Result<(), MemoryError> {
        let file = inner
            .file
            .as_mut()
            .ok_or_else(|| capture("journal handle is closed"))?;
        append_synced(file, record, |point| self.maybe_fail(point))
    }

    #[cfg(test)]
    pub(super) fn header_bytes(&self) -> u64 {
        self.inner.lock().header_bytes
    }

    #[cfg(test)]
    pub(super) fn fail_once_at(&self, point: FaultPoint) {
        use std::sync::atomic::Ordering;
        self.fault.store(point as u8, Ordering::SeqCst);
    }

    #[cfg(test)]
    fn maybe_fail(&self, point: FaultPoint) -> Result<(), MemoryError> {
        use std::sync::atomic::Ordering;
        if self
            .fault
            .compare_exchange(point as u8, 0, Ordering::SeqCst, Ordering::SeqCst)
            .is_ok()
        {
            return Err(capture(format!("injected journal fault at {point:?}")));
        }
        Ok(())
    }

    #[cfg(not(test))]
    #[allow(clippy::unnecessary_wraps, clippy::unused_self)] // Mirrors the test fault seam exactly.
    fn maybe_fail(&self, _point: FaultPoint) -> Result<(), MemoryError> {
        Ok(())
    }
}

impl MutationObserver for DirtyJournal {
    fn before_mutation(&self, key: DirtyKey) -> Result<(), MemoryError> {
        self.append(key)
    }
}

#[derive(Debug, Clone, Copy)]
#[repr(u8)]
pub(super) enum FaultPoint {
    BeforeAppend = 1,
    AfterAppend = 2,
    BeforeAppendSync = 3,
    AfterAppendSync = 4,
    BeforeCompactionSync = 5,
    AfterCompactionSync = 6,
    BeforeCompactionReplace = 7,
    AfterCompactionReplace = 8,
    BeforeDirectorySync = 9,
    AfterDirectorySync = 10,
}

fn prepare_journal(workspace: &Path, identity: &EpochIdentity) -> Result<PathBuf, MemoryError> {
    validate_workspace(workspace)?;
    let path = workspace.join(JOURNAL_FILE);
    recover_staging(workspace, &path)?;
    if !path_entry_exists(&path)? {
        create_journal(workspace, identity)?;
    }
    validate_regular_file(&path)?;
    Ok(path)
}

fn load_journal(path: &Path) -> Result<LoadedJournal, MemoryError> {
    let mut file = OpenOptions::new()
        .read(true)
        .write(true)
        .open(path)
        .map_err(|err| capture(format!("cannot open {}: {err}", path.display())))?;
    let (header, header_bytes) = read_header(&mut file)?;
    let (last_sequence, valid_len) = scan_records(&mut file, &header, header_bytes)?;
    recover_torn_tail(&mut file, valid_len)?;
    Ok(LoadedJournal {
        file,
        header,
        header_bytes,
        last_sequence,
    })
}

fn verify_identity(header: &JournalHeader, identity: &EpochIdentity) -> Result<(), MemoryError> {
    if header.identity != *identity {
        return Err(capture("journal epoch identity mismatch"));
    }
    Ok(())
}

fn validate_capacity(header_bytes: u64, max_bytes: u64) -> Result<(), MemoryError> {
    if max_bytes < header_bytes + RECORD_BYTES {
        return Err(capture("journal byte cap cannot hold one record"));
    }
    Ok(())
}

fn next_header(header: &JournalHeader, watermark: u64) -> Result<JournalHeader, MemoryError> {
    let generation = header
        .generation
        .checked_add(1)
        .ok_or_else(|| capture("journal generation overflow"))?;
    Ok(JournalHeader {
        format_version: FORMAT_VERSION,
        generation,
        compacted_through: watermark,
        identity: header.identity.clone(),
    })
}

fn next_sequence(last: u64) -> Result<u64, MemoryError> {
    last.checked_add(1)
        .ok_or_else(|| capture("journal sequence overflow"))
}

fn prepare_append(
    inner: &JournalInner,
    max_bytes: u64,
    key: DirtyKey,
) -> Result<(u64, EncodedRecord), MemoryError> {
    let sequence = next_sequence(inner.last_sequence)?;
    let file = inner
        .file
        .as_ref()
        .ok_or_else(|| capture("journal handle is closed"))?;
    ensure_append_fits(file, max_bytes)?;
    Ok((sequence, encode_record(JournalRecord::new(sequence, key))))
}

fn ensure_append_fits(file: &File, max_bytes: u64) -> Result<(), MemoryError> {
    let length = file
        .metadata()
        .map_err(|err| capture(format!("cannot size journal: {err}")))?
        .len();
    if length.saturating_add(RECORD_BYTES) > max_bytes {
        return Err(capture(format!("journal byte cap {max_bytes} reached")));
    }
    Ok(())
}

fn create_journal(workspace: &Path, identity: &EpochIdentity) -> Result<(), MemoryError> {
    let staging = workspace.join(STAGING_FILE);
    let final_path = workspace.join(JOURNAL_FILE);
    let header = JournalHeader {
        format_version: FORMAT_VERSION,
        generation: 0,
        compacted_through: 0,
        identity: identity.clone(),
    };
    let mut file = OpenOptions::new()
        .write(true)
        .create_new(true)
        .open(&staging)
        .map_err(|err| capture(format!("cannot create journal staging file: {err}")))?;
    write_header(&mut file, &header)?;
    file.flush()
        .map_err(|err| capture(format!("cannot flush journal header: {err}")))?;
    file.sync_all()
        .map_err(|err| capture(format!("cannot sync journal header: {err}")))?;
    drop(file);
    promote(&staging, &final_path)
        .map_err(|err| capture(format!("cannot publish journal: {err}")))?;
    durability_barrier(workspace)
        .map_err(|err| capture(format!("cannot sync journal directory: {err}")))
}

fn validate_workspace(workspace: &Path) -> Result<(), MemoryError> {
    let metadata = std::fs::symlink_metadata(workspace)
        .map_err(|err| capture(format!("cannot inspect journal workspace: {err}")))?;
    if metadata.file_type().is_symlink() || !metadata.is_dir() {
        return Err(capture("journal workspace must be a real directory"));
    }
    Ok(())
}

fn validate_regular_file(path: &Path) -> Result<(), MemoryError> {
    let metadata = std::fs::symlink_metadata(path)
        .map_err(|err| capture(format!("cannot inspect journal file: {err}")))?;
    if metadata.file_type().is_symlink() || !metadata.is_file() {
        return Err(capture("journal path must be a regular file"));
    }
    Ok(())
}

fn recover_staging(workspace: &Path, final_path: &Path) -> Result<(), MemoryError> {
    let staging = workspace.join(STAGING_FILE);
    if !path_entry_exists(&staging)? {
        return Ok(());
    }
    if path_entry_exists(final_path)? {
        validate_regular_file(final_path)?;
    }
    validate_regular_file(&staging)?;
    std::fs::remove_file(&staging).map_err(|err| {
        capture(format!(
            "cannot remove interrupted compaction staging file: {err}"
        ))
    })
}

fn path_entry_exists(path: &Path) -> Result<bool, MemoryError> {
    match std::fs::symlink_metadata(path) {
        Ok(_) => Ok(true),
        Err(error) if error.kind() == std::io::ErrorKind::NotFound => Ok(false),
        Err(error) => Err(capture(format!(
            "cannot inspect {}: {error}",
            path.display()
        ))),
    }
}

fn ensure_healthy(inner: &JournalInner) -> Result<(), MemoryError> {
    if inner.poisoned {
        return Err(capture(
            "journal is poisoned after a durability failure; reopen it",
        ));
    }
    Ok(())
}

fn capture(message: impl Into<String>) -> MemoryError {
    MemoryError::MigrationCapture(message.into())
}