pulsehive-db 0.6.0

Embedded database for agentic AI systems — collective memory for multi-agent coordination
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
//! Sync manager — orchestrates sync lifecycle between PulseDB instances.
//!
//! [`SyncManager`] is the public API for sync. It manages:
//! - Handshake negotiation with remote peer
//! - Background push/pull loops on configured intervals
//! - Manual one-shot sync via [`sync_once()`](SyncManager::sync_once)
//! - Initial catchup sync with progress callback
//! - Error recovery with exponential backoff
//! - Graceful shutdown

use std::sync::{Arc, RwLock};

use tokio::sync::Notify;
use tokio::task::JoinHandle;
use tracing::{debug, error, info, instrument, warn};

use crate::db::PulseDB;

use super::applier::RemoteChangeApplier;
use super::config::{SyncConfig, SyncDirection};
use super::error::SyncError;
use super::progress::SyncProgressCallback;
use super::pusher::LocalChangePusher;
use super::transport::SyncTransport;
use super::types::{HandshakeRequest, InstanceId, PullRequest, SyncCursor, SyncStatus};
use super::{SYNC_CAPABILITY_GCOUNTER_APPLICATIONS, SYNC_PROTOCOL_VERSION};

/// Orchestrator for sync operations between two PulseDB instances.
///
/// The SyncManager is a **sidecar** — it holds `Arc<PulseDB>` but doesn't
/// wrap it. Local database operations are completely unaffected by sync state.
///
/// # Example
///
/// ```rust,no_run
/// use std::sync::Arc;
/// use pulsedb::{PulseDB, Config};
/// use pulsedb::sync::{SyncManager, SyncConfig, InMemorySyncTransport};
///
/// # async fn example() -> Result<(), Box<dyn std::error::Error>> {
/// let db = Arc::new(PulseDB::open("my.db", Config::default())?);
/// let (local_transport, _remote) = InMemorySyncTransport::new_pair();
/// let mut manager = SyncManager::new(db, Box::new(local_transport), SyncConfig::default());
/// manager.start().await?;
/// // ... sync runs in background ...
/// manager.stop().await?;
/// # Ok(())
/// # }
/// ```
pub struct SyncManager {
    db: Arc<PulseDB>,
    transport: Arc<dyn SyncTransport>,
    config: SyncConfig,
    local_instance_id: InstanceId,
    peer_instance_id: Option<InstanceId>,
    status: Arc<RwLock<SyncStatus>>,
    shutdown: Arc<Notify>,
    task_handle: Option<JoinHandle<()>>,
}

impl SyncManager {
    /// Creates a new SyncManager.
    ///
    /// Does NOT start sync — call [`start()`](Self::start) or
    /// [`sync_once()`](Self::sync_once) to begin.
    pub fn new(db: Arc<PulseDB>, transport: Box<dyn SyncTransport>, config: SyncConfig) -> Self {
        let local_instance_id = db.storage_for_test().instance_id();
        Self {
            db,
            transport: Arc::from(transport),
            config,
            local_instance_id,
            peer_instance_id: None,
            status: Arc::new(RwLock::new(SyncStatus::Idle)),
            shutdown: Arc::new(Notify::new()),
            task_handle: None,
        }
    }

    /// Starts the background sync loop.
    ///
    /// Performs a handshake with the remote peer, then spawns a background
    /// tokio task that pushes and pulls on the configured intervals.
    #[instrument(skip(self), fields(instance_id = %self.local_instance_id))]
    pub async fn start(&mut self) -> Result<(), SyncError> {
        if self.task_handle.is_some() {
            return Err(SyncError::transport("SyncManager already started"));
        }

        // Perform handshake
        let peer_id = self.perform_handshake().await?;
        self.peer_instance_id = Some(peer_id);

        self.set_status(SyncStatus::Syncing);

        // Clone everything needed for the background task
        let db = Arc::clone(&self.db);
        let transport = Arc::clone(&self.transport);
        let config = self.config.clone();
        let local_id = self.local_instance_id;
        let status = Arc::clone(&self.status);
        let shutdown = Arc::clone(&self.shutdown);

        let handle = tokio::spawn(async move {
            Self::background_loop(db, transport, config, local_id, peer_id, status, shutdown).await;
        });

        self.task_handle = Some(handle);
        info!("SyncManager started");
        Ok(())
    }

    /// Stops the background sync loop.
    #[instrument(skip(self))]
    pub async fn stop(&mut self) -> Result<(), SyncError> {
        if let Some(handle) = self.task_handle.take() {
            self.shutdown.notify_one();
            handle
                .await
                .map_err(|e| SyncError::transport(format!("Background task panicked: {}", e)))?;
            self.set_status(SyncStatus::Idle);
            info!("SyncManager stopped");
        }
        Ok(())
    }

    /// Performs a single push+pull sync cycle (no background task needed).
    ///
    /// Useful for testing or manual sync triggers.
    #[instrument(skip(self))]
    pub async fn sync_once(&mut self) -> Result<SyncStatus, SyncError> {
        // Handshake if we haven't yet
        if self.peer_instance_id.is_none() {
            let peer_id = self.perform_handshake().await?;
            self.peer_instance_id = Some(peer_id);
        }
        let peer_id = self.peer_instance_id.unwrap();

        self.set_status(SyncStatus::Syncing);

        // Load saved push cursor
        let push_seq = self.load_cursor_sequence(peer_id)?;
        let mut pusher = LocalChangePusher::new(
            Arc::clone(&self.db),
            Arc::clone(&self.transport),
            self.config.clone(),
            self.local_instance_id,
            peer_id,
            push_seq,
        );

        let applier = RemoteChangeApplier::new(Arc::clone(&self.db), self.config.clone());

        // Push if enabled
        let pushed = if matches!(
            self.config.direction,
            SyncDirection::PushOnly | SyncDirection::Bidirectional
        ) {
            pusher.push_pending().await?
        } else {
            0
        };

        // Pull if enabled
        let pulled = if matches!(
            self.config.direction,
            SyncDirection::PullOnly | SyncDirection::Bidirectional
        ) {
            self.pull_and_apply(&applier, peer_id).await?
        } else {
            0
        };

        self.set_status(SyncStatus::Idle);

        debug!(pushed, pulled, "sync_once complete");
        Ok(SyncStatus::Idle)
    }

    /// Performs initial sync — pulls all remote changes in batches.
    ///
    /// Call this before `start()` to catch up from a cold start.
    #[instrument(skip(self, progress))]
    pub async fn initial_sync(
        &mut self,
        progress: Option<Box<dyn SyncProgressCallback>>,
    ) -> Result<(), SyncError> {
        // Handshake if needed
        if self.peer_instance_id.is_none() {
            let peer_id = self.perform_handshake().await?;
            self.peer_instance_id = Some(peer_id);
        }
        let peer_id = self.peer_instance_id.unwrap();

        self.set_status(SyncStatus::Syncing);

        let applier = RemoteChangeApplier::new(Arc::clone(&self.db), self.config.clone());

        let mut total_pulled = 0usize;
        let mut cursor = SyncCursor {
            instance_id: peer_id,
            last_sequence: self.load_cursor_sequence(peer_id)?,
        };

        loop {
            let pull_request = PullRequest {
                cursor: cursor.clone(),
                batch_size: self.config.batch_size,
                collectives: self.config.collectives.clone(),
            };

            let response = self.transport.pull_changes(pull_request).await?;
            let batch_size = response.changes.len();

            if batch_size > 0 {
                applier.apply_batch(response.changes)?;
            }

            total_pulled += batch_size;
            cursor = response.new_cursor;

            // Save cursor after each batch (crash-safe)
            self.save_cursor(&cursor)?;

            if let Some(ref cb) = progress {
                cb.on_progress(batch_size, total_pulled, response.has_more);
            }

            if !response.has_more {
                break;
            }
        }

        self.set_status(SyncStatus::Idle);
        info!(total_pulled, "Initial sync complete");
        Ok(())
    }

    /// Returns the current sync status.
    pub fn status(&self) -> SyncStatus {
        self.status
            .read()
            .unwrap_or_else(|e| e.into_inner())
            .clone()
    }

    // ─── Internal helpers ────────────────────────────────────────────

    #[instrument(skip(self))]
    async fn perform_handshake(&self) -> Result<InstanceId, SyncError> {
        let request = HandshakeRequest {
            instance_id: self.local_instance_id,
            protocol_version: SYNC_PROTOCOL_VERSION,
            capabilities: vec![
                "push".into(),
                "pull".into(),
                SYNC_CAPABILITY_GCOUNTER_APPLICATIONS.into(),
            ],
        };

        let response = self.transport.handshake(request).await?;

        if !response.accepted {
            return Err(SyncError::handshake(
                response.reason.unwrap_or_else(|| "rejected".into()),
            ));
        }

        if response.protocol_version != SYNC_PROTOCOL_VERSION {
            return Err(SyncError::ProtocolVersion {
                local: SYNC_PROTOCOL_VERSION,
                remote: response.protocol_version,
            });
        }

        debug!(peer = %response.instance_id, "Handshake accepted");
        Ok(response.instance_id)
    }

    fn set_status(&self, status: SyncStatus) {
        if let Ok(mut s) = self.status.write() {
            *s = status;
        }
    }

    fn load_cursor_sequence(&self, peer_id: InstanceId) -> Result<u64, SyncError> {
        self.db
            .storage_for_test()
            .load_sync_cursor(&peer_id)
            .map_err(|e| SyncError::transport(format!("Failed to load cursor: {}", e)))
            .map(|opt| opt.map_or(0, |c| c.last_sequence))
    }

    fn save_cursor(&self, cursor: &SyncCursor) -> Result<(), SyncError> {
        self.db
            .storage_for_test()
            .save_sync_cursor(cursor)
            .map_err(|e| SyncError::transport(format!("Failed to save cursor: {}", e)))
    }

    /// Pull changes from remote and apply them locally.
    async fn pull_and_apply(
        &self,
        applier: &RemoteChangeApplier,
        peer_id: InstanceId,
    ) -> Result<usize, SyncError> {
        let cursor_seq = self.load_cursor_sequence(peer_id)?;
        let pull_request = PullRequest {
            cursor: SyncCursor {
                instance_id: peer_id,
                last_sequence: cursor_seq,
            },
            batch_size: self.config.batch_size,
            collectives: self.config.collectives.clone(),
        };

        let response = self.transport.pull_changes(pull_request).await?;
        let count = response.changes.len();

        if count > 0 {
            applier.apply_batch(response.changes)?;
            self.save_cursor(&response.new_cursor)?;
        }

        Ok(count)
    }

    /// Background loop that runs push+pull on configured intervals.
    async fn background_loop(
        db: Arc<PulseDB>,
        transport: Arc<dyn SyncTransport>,
        config: SyncConfig,
        local_id: InstanceId,
        peer_id: InstanceId,
        status: Arc<RwLock<SyncStatus>>,
        shutdown: Arc<Notify>,
    ) {
        let interval_ms = std::cmp::max(config.push_interval_ms, config.pull_interval_ms);
        let interval = tokio::time::Duration::from_millis(interval_ms);

        let mut consecutive_failures = 0u32;
        let max_retries = config.retry.max_retries;
        let initial_backoff = config.retry.initial_backoff_ms;
        let max_backoff = config.retry.max_backoff_ms;
        let multiplier = config.retry.backoff_multiplier;

        loop {
            let sleep_duration = if consecutive_failures > 0 {
                // Exponential backoff
                let backoff = (initial_backoff as f64)
                    * multiplier.powi(consecutive_failures.saturating_sub(1) as i32);
                let backoff_ms = (backoff as u64).min(max_backoff);
                tokio::time::Duration::from_millis(backoff_ms)
            } else {
                interval
            };

            tokio::select! {
                _ = shutdown.notified() => {
                    debug!("Sync background loop shutting down");
                    break;
                }
                _ = tokio::time::sleep(sleep_duration) => {
                    // Build push cursor from saved state
                    let push_seq = db
                        .storage_for_test()
                        .load_sync_cursor(&peer_id)
                        .unwrap_or(None)
                        .map_or(0, |c| c.last_sequence);

                    let mut pusher = LocalChangePusher::new(
                        Arc::clone(&db),
                        Arc::clone(&transport),
                        config.clone(),
                        local_id,
                        peer_id,
                        push_seq,
                    );
                    let applier = RemoteChangeApplier::new(Arc::clone(&db), config.clone());

                    let result = Self::run_sync_cycle(&mut pusher, &applier, &transport, &db, &config, peer_id).await;

                    match result {
                        Ok(_) => {
                            if consecutive_failures > 0 {
                                info!("Sync recovered after {} failures", consecutive_failures);
                            }
                            consecutive_failures = 0;
                            if let Ok(mut s) = status.write() {
                                *s = SyncStatus::Syncing;
                            }
                        }
                        Err(e) => {
                            consecutive_failures += 1;
                            if consecutive_failures > max_retries {
                                warn!(
                                    failures = consecutive_failures,
                                    "Sync errors exceed max_retries, continuing with backoff"
                                );
                            }
                            error!("Sync cycle failed: {}", e);
                            if let Ok(mut s) = status.write() {
                                *s = SyncStatus::Error(e.to_string());
                            }
                        }
                    }
                }
            }
        }

        if let Ok(mut s) = status.write() {
            *s = SyncStatus::Idle;
        }
    }

    /// Execute one push+pull cycle. Used by background loop.
    async fn run_sync_cycle(
        pusher: &mut LocalChangePusher,
        applier: &RemoteChangeApplier,
        transport: &Arc<dyn SyncTransport>,
        db: &Arc<PulseDB>,
        config: &SyncConfig,
        peer_id: InstanceId,
    ) -> Result<(), SyncError> {
        // Push
        if matches!(
            config.direction,
            SyncDirection::PushOnly | SyncDirection::Bidirectional
        ) {
            pusher.push_pending().await?;
        }

        // Pull
        if matches!(
            config.direction,
            SyncDirection::PullOnly | SyncDirection::Bidirectional
        ) {
            let cursor_seq = db
                .storage_for_test()
                .load_sync_cursor(&peer_id)
                .map_err(|e| SyncError::transport(format!("cursor load: {}", e)))?
                .map_or(0, |c| c.last_sequence);

            let pull_request = PullRequest {
                cursor: SyncCursor {
                    instance_id: peer_id,
                    last_sequence: cursor_seq,
                },
                batch_size: config.batch_size,
                collectives: config.collectives.clone(),
            };

            let response = transport.pull_changes(pull_request).await?;
            let count = response.changes.len();

            if count > 0 {
                applier.apply_batch(response.changes)?;
                let cursor = response.new_cursor;
                db.storage_for_test()
                    .save_sync_cursor(&cursor)
                    .map_err(|e| SyncError::transport(format!("cursor save: {}", e)))?;
            }
        }

        Ok(())
    }
}