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
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
use std::time::Duration;

use crate::MemoryError;

use super::catchup::ReplayProgress;

mod state;

use state::{ControllerState, StateStore};

const MAX_WINDOW: usize = 64;
const MAX_BUDGET: Duration = Duration::from_secs(24 * 60 * 60);
const RESUME_CATCH_UP: &str = "reopen source and resume catch-up";
const RECOVER_CUTOVER: &str = "complete or recover cutover before serving traffic";

#[derive(Debug, Clone, Copy, PartialEq, Eq, serde::Serialize, serde::Deserialize)]
pub(crate) struct ControllerConfig {
    pub(crate) observation_window: usize,
    pub(crate) pause_budget: Duration,
    pub(crate) verification_reserve: Duration,
}

impl ControllerConfig {
    pub(crate) fn validate(self) -> Result<Self, MemoryError> {
        if !(2..=MAX_WINDOW).contains(&self.observation_window) {
            return Err(capture("controller observation window must be in 2..=64"));
        }
        if self.pause_budget.is_zero() || self.pause_budget > MAX_BUDGET {
            return Err(capture("controller pause budget must be in 1ns..=24h"));
        }
        if self.verification_reserve > self.pause_budget {
            return Err(capture("verification reserve exceeds pause budget"));
        }
        Ok(self)
    }
}

#[derive(Debug, Clone, Copy, PartialEq, Eq, serde::Serialize, serde::Deserialize)]
pub(crate) struct ConvergenceSample {
    pub(crate) observed_at: Duration,
    pub(crate) input_watermark: u64,
    pub(crate) output_watermark: u64,
    pub(crate) distinct_dirty_facts: u64,
    pub(crate) distinct_edge_sources: u64,
    pub(crate) pending_journal_bytes: u64,
    pub(crate) replay_elapsed: Duration,
    pub(crate) largest_apply_latency: Duration,
}

#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub(crate) struct ConvergenceMetrics {
    pub(crate) input_watermark: u64,
    pub(crate) output_watermark: u64,
    pub(crate) backlog_records: u64,
    pub(crate) backlog_grew: bool,
    pub(crate) distinct_dirty_facts: u64,
    pub(crate) distinct_edge_sources: u64,
    pub(crate) pending_journal_bytes: u64,
    pub(crate) arrival_rate: MeasuredRate,
    pub(crate) replay_rate: MeasuredRate,
    pub(crate) window_elapsed: Duration,
    pub(crate) replay_elapsed: Duration,
    pub(crate) largest_apply_latency: Duration,
}

#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub(crate) struct MeasuredRate {
    pub(crate) records: u64,
    pub(crate) elapsed: Duration,
}

impl ConvergenceSample {
    pub(crate) fn from_replay(observed_at: Duration, progress: ReplayProgress) -> Self {
        Self {
            observed_at,
            input_watermark: progress.input_watermark,
            output_watermark: progress.output_watermark,
            distinct_dirty_facts: progress.distinct_dirty_facts,
            distinct_edge_sources: progress.distinct_edge_sources,
            pending_journal_bytes: progress.pending_journal_bytes,
            replay_elapsed: progress.elapsed,
            largest_apply_latency: progress.largest_apply_latency,
        }
    }
}

#[derive(Debug, Clone, Copy, PartialEq, Eq, serde::Serialize, serde::Deserialize)]
pub(crate) enum ConvergenceVerdict {
    CatchingUp,
    CutoverReady,
    NonConverging,
}

#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub(crate) struct ConvergenceObservation {
    pub(crate) metrics: ConvergenceMetrics,
    pub(crate) estimated_pause: Option<Duration>,
    pub(crate) verdict: ConvergenceVerdict,
}

#[derive(Debug, Clone, Copy, PartialEq, Eq, serde::Serialize, serde::Deserialize)]
pub(crate) enum ControllerPhase {
    CatchingUp,
    CutoverReady,
    NonConverging,
    Quiescing { deadline: Duration },
    Activated,
    Cancelled,
}

#[derive(Debug, Clone, PartialEq, Eq)]
pub(crate) struct CancellationPermit {
    epoch_id: String,
}

impl CancellationPermit {
    pub(crate) fn epoch_id(&self) -> &str {
        &self.epoch_id
    }
}

pub(crate) struct ConvergenceController {
    config: ControllerConfig,
    store: StateStore,
    state: ControllerState,
}

impl ConvergenceController {
    pub(crate) fn open(
        workspace: &std::path::Path,
        epoch_id: &str,
        config: ControllerConfig,
    ) -> Result<Self, MemoryError> {
        let config = config.validate()?;
        let (store, mut state, resumed) = StateStore::open(workspace, epoch_id, config)?;
        if resumed && prepare_resumed_state(&mut state) {
            store.save(&state)?;
        }
        Ok(Self {
            config,
            store,
            state,
        })
    }

    pub(crate) fn observe(
        &mut self,
        sample: ConvergenceSample,
    ) -> Result<ConvergenceObservation, MemoryError> {
        ensure_observable(self.state.phase)?;
        validate_sample(self.state.samples.last(), &sample)?;
        let mut next = self.state.clone();
        next.samples.push(sample);
        if next.samples.len() > self.config.observation_window {
            next.samples.remove(0);
        }
        let observation = assess(&next.samples, self.config);
        next.phase = phase_for(observation.verdict);
        next.last_observation = Some(sample);
        next.last_verdict = Some(observation.verdict);
        next.recovery_action = None;
        self.replace_state(next)?;
        Ok(observation)
    }

    pub(crate) fn begin_quiescing(&mut self, now: Duration) -> Result<(), MemoryError> {
        if self.state.phase != ControllerPhase::CutoverReady {
            return Err(capture("cutover is not ready for quiescing"));
        }
        ensure_clock_after_sample(&self.state, now)?;
        let deadline = now
            .checked_add(self.config.pause_budget)
            .ok_or_else(|| capture("cutover deadline overflow"))?;
        let mut next = self.state.clone();
        next.phase = ControllerPhase::Quiescing { deadline };
        self.replace_state(next)
    }

    pub(crate) fn activate(&mut self, now: Duration) -> Result<(), MemoryError> {
        self.ensure_cutover_start(now)?;
        let ControllerPhase::Quiescing { deadline } = self.state.phase else {
            return Err(capture("migration is not quiescing"));
        };
        let started = deadline
            .checked_sub(self.config.pause_budget)
            .ok_or_else(|| capture("cutover deadline predates its pause budget"))?;
        let elapsed = now
            .checked_sub(started)
            .ok_or_else(|| capture("activation time predates quiescing"))?;
        let mut next = self.state.clone();
        next.phase = ControllerPhase::Activated;
        next.recovery_action = None;
        next.measured_cutover = Some(elapsed);
        self.replace_state(next)
    }

    pub(crate) fn ensure_cutover_start(&mut self, now: Duration) -> Result<(), MemoryError> {
        if self.state.recovery_action.as_deref() == Some(RECOVER_CUTOVER) {
            return Err(capture("cutover recovery is required after restart"));
        }
        let ControllerPhase::Quiescing { deadline } = self.state.phase else {
            return Err(capture("migration is not quiescing"));
        };
        ensure_activation_time(now, deadline, self.config.pause_budget)?;
        if now > deadline {
            let mut next = self.state.clone();
            next.phase = ControllerPhase::CatchingUp;
            next.recovery_action = Some(RESUME_CATCH_UP.to_owned());
            self.replace_state(next)?;
            return Err(capture("cutover deadline expired"));
        }
        Ok(())
    }

    pub(crate) fn cancel(
        &mut self,
        source_authoritative: bool,
        epoch_id: &str,
    ) -> Result<CancellationPermit, MemoryError> {
        if epoch_id != self.state.epoch_id {
            return Err(capture("controller epoch ownership mismatch"));
        }
        if matches!(
            self.state.phase,
            ControllerPhase::Quiescing { .. } | ControllerPhase::Activated
        ) {
            let mut next = self.state.clone();
            next.recovery_action = Some(RECOVER_CUTOVER.to_owned());
            self.replace_state(next)?;
            return Err(capture("cutover recovery is required; rollback is unsafe"));
        }
        if !source_authoritative {
            return Err(capture("source is not authoritative; cancellation refused"));
        }
        let mut next = self.state.clone();
        next.phase = ControllerPhase::Cancelled;
        next.recovery_action = None;
        self.replace_state(next)?;
        Ok(CancellationPermit {
            epoch_id: epoch_id.to_owned(),
        })
    }

    pub(crate) fn phase(&self) -> ControllerPhase {
        self.state.phase
    }

    pub(crate) fn recovery_action(&self) -> Option<&str> {
        self.state.recovery_action.as_deref()
    }

    pub(crate) fn complete_source_recovery(&mut self) -> Result<(), MemoryError> {
        self.require_recovery_phase(false)?;
        let mut next = self.state.clone();
        next.phase = ControllerPhase::CatchingUp;
        next.samples.clear();
        next.last_observation = None;
        next.last_verdict = None;
        next.recovery_action = Some(RESUME_CATCH_UP.to_owned());
        self.replace_state(next)
    }

    pub(crate) fn complete_target_recovery(&mut self) -> Result<(), MemoryError> {
        self.require_recovery_phase(true)?;
        let mut next = self.state.clone();
        next.recovery_action = None;
        self.replace_state(next)
    }

    pub(crate) fn measured_cutover(&self) -> Option<Duration> {
        self.state.measured_cutover
    }

    pub(crate) fn epoch_id(&self) -> &str {
        &self.state.epoch_id
    }

    fn replace_state(&mut self, next: ControllerState) -> Result<(), MemoryError> {
        self.store.save(&next)?;
        self.state = next;
        Ok(())
    }

    fn require_recovery_phase(&self, activated: bool) -> Result<(), MemoryError> {
        let expected = if activated {
            matches!(self.state.phase, ControllerPhase::Activated)
        } else {
            matches!(self.state.phase, ControllerPhase::Quiescing { .. })
        };
        if expected && self.state.recovery_action.as_deref() == Some(RECOVER_CUTOVER) {
            return Ok(());
        }
        Err(capture(
            "controller is not in the requested cutover recovery phase",
        ))
    }

    #[cfg(test)]
    pub(super) fn retained_samples(&self) -> usize {
        self.state.samples.len()
    }

    #[cfg(test)]
    pub(super) fn last_observation(&self) -> Option<ConvergenceSample> {
        self.state.last_observation
    }
}

fn prepare_resumed_state(state: &mut ControllerState) -> bool {
    match state.phase {
        ControllerPhase::CatchingUp
        | ControllerPhase::CutoverReady
        | ControllerPhase::NonConverging => {
            state.samples.clear();
            state.phase = ControllerPhase::CatchingUp;
            state.recovery_action = Some(RESUME_CATCH_UP.to_owned());
            true
        }
        ControllerPhase::Quiescing { .. } | ControllerPhase::Activated => {
            state.recovery_action = Some(RECOVER_CUTOVER.to_owned());
            true
        }
        ControllerPhase::Cancelled => false,
    }
}

fn validate_sample(
    previous: Option<&ConvergenceSample>,
    sample: &ConvergenceSample,
) -> Result<(), MemoryError> {
    if sample.output_watermark > sample.input_watermark {
        return Err(capture("output watermark exceeds input watermark"));
    }
    if sample.largest_apply_latency > sample.replay_elapsed {
        return Err(capture("largest apply latency exceeds replay elapsed time"));
    }
    if let Some(previous) = previous {
        if sample.observed_at <= previous.observed_at {
            return Err(capture("controller observations must use monotonic time"));
        }
        if sample.input_watermark < previous.input_watermark
            || sample.output_watermark < previous.output_watermark
        {
            return Err(capture("controller watermarks must be monotonic"));
        }
    }
    Ok(())
}

fn assess(samples: &[ConvergenceSample], config: ControllerConfig) -> ConvergenceObservation {
    let metrics = metrics(samples);
    if samples.len() < config.observation_window {
        return observation(metrics, None, ConvergenceVerdict::CatchingUp);
    }
    let estimate = pause_estimate(metrics, config.verification_reserve);
    let verdict = verdict(metrics, estimate, config.pause_budget);
    observation(metrics, estimate, verdict)
}

fn metrics(samples: &[ConvergenceSample]) -> ConvergenceMetrics {
    let first = &samples[0];
    let last = &samples[samples.len() - 1];
    ConvergenceMetrics {
        input_watermark: last.input_watermark,
        output_watermark: last.output_watermark,
        backlog_records: last.input_watermark.saturating_sub(last.output_watermark),
        backlog_grew: last.input_watermark.saturating_sub(last.output_watermark)
            > first.input_watermark.saturating_sub(first.output_watermark),
        distinct_dirty_facts: last.distinct_dirty_facts,
        distinct_edge_sources: last.distinct_edge_sources,
        pending_journal_bytes: last.pending_journal_bytes,
        arrival_rate: MeasuredRate {
            records: last.input_watermark.saturating_sub(first.input_watermark),
            elapsed: last.observed_at.saturating_sub(first.observed_at),
        },
        replay_rate: MeasuredRate {
            records: last.output_watermark.saturating_sub(first.output_watermark),
            elapsed: last.observed_at.saturating_sub(first.observed_at),
        },
        window_elapsed: last.observed_at.saturating_sub(first.observed_at),
        replay_elapsed: last.replay_elapsed,
        largest_apply_latency: samples
            .iter()
            .map(|sample| sample.largest_apply_latency)
            .max()
            .unwrap_or_default(),
    }
}

fn pause_estimate(metrics: ConvergenceMetrics, reserve: Duration) -> Option<Duration> {
    if metrics.backlog_records == 0 {
        return metrics.largest_apply_latency.checked_add(reserve);
    }
    if metrics.replay_rate.records == 0 {
        return None;
    }
    let net_replay = metrics
        .replay_rate
        .records
        .checked_sub(metrics.arrival_rate.records)?;
    if net_replay == 0 || metrics.window_elapsed.is_zero() {
        return None;
    }
    let drain = ceil_duration_product(metrics.window_elapsed, metrics.backlog_records, net_replay);
    drain
        .checked_add(metrics.largest_apply_latency)?
        .checked_add(reserve)
}

fn verdict(
    metrics: ConvergenceMetrics,
    estimate: Option<Duration>,
    budget: Duration,
) -> ConvergenceVerdict {
    if metrics.backlog_grew
        || (metrics.arrival_rate.records > 0
            && metrics.arrival_rate.records >= metrics.replay_rate.records)
    {
        return ConvergenceVerdict::NonConverging;
    }
    match estimate {
        Some(duration) if duration <= budget => ConvergenceVerdict::CutoverReady,
        _ => ConvergenceVerdict::CatchingUp,
    }
}

fn ceil_duration_product(duration: Duration, count: u64, divisor: u64) -> Duration {
    let numerator = duration.as_nanos().saturating_mul(u128::from(count));
    let rounded =
        numerator.saturating_add(u128::from(divisor).saturating_sub(1)) / u128::from(divisor);
    Duration::from_nanos(u64::try_from(rounded).unwrap_or(u64::MAX))
}

fn observation(
    metrics: ConvergenceMetrics,
    estimated_pause: Option<Duration>,
    verdict: ConvergenceVerdict,
) -> ConvergenceObservation {
    ConvergenceObservation {
        metrics,
        estimated_pause,
        verdict,
    }
}

fn phase_for(verdict: ConvergenceVerdict) -> ControllerPhase {
    match verdict {
        ConvergenceVerdict::CatchingUp => ControllerPhase::CatchingUp,
        ConvergenceVerdict::CutoverReady => ControllerPhase::CutoverReady,
        ConvergenceVerdict::NonConverging => ControllerPhase::NonConverging,
    }
}

fn ensure_observable(phase: ControllerPhase) -> Result<(), MemoryError> {
    match phase {
        ControllerPhase::CatchingUp
        | ControllerPhase::CutoverReady
        | ControllerPhase::NonConverging => Ok(()),
        _ => Err(capture("controller phase does not accept observations")),
    }
}

fn ensure_clock_after_sample(state: &ControllerState, now: Duration) -> Result<(), MemoryError> {
    if state
        .samples
        .last()
        .is_some_and(|sample| now < sample.observed_at)
    {
        return Err(capture("cutover clock predates the latest observation"));
    }
    Ok(())
}

fn ensure_activation_time(
    now: Duration,
    deadline: Duration,
    budget: Duration,
) -> Result<(), MemoryError> {
    let started = deadline
        .checked_sub(budget)
        .ok_or_else(|| capture("invalid cutover deadline"))?;
    if now < started {
        return Err(capture("cutover activation time is not monotonic"));
    }
    Ok(())
}

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