nodedb-lite 0.1.0

Embedded edge client for NodeDB — offline vector search, graph traversal, and CRDT sync
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
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
//! Engine-level ack dispatch helpers — called from `dispatch_frame`.
//!
//! Each function handles one ack message type, applies `AckStatus` handling
//! (frontier-advance on Applied/Duplicate, fenced-flag on Fenced, warn on Gap),
//! then calls the appropriate `delegate.acknowledge_*` method.

use std::sync::Arc;

use nodedb_types::sync::wire::{AckStatus, EngineKind, SyncFrame, stream_id_for};

use crate::sync::client::SyncClient;
use crate::sync::delegate::SyncDelegate;

pub(super) async fn handle_columnar_insert_ack(
    client: &Arc<SyncClient>,
    delegate: &Arc<dyn SyncDelegate>,
    frame: &SyncFrame,
) {
    let Some(ack) = frame.decode_body::<nodedb_types::sync::wire::ColumnarInsertAckMsg>() else {
        return;
    };
    tracing::debug!(
        collection = %ack.collection,
        batch_id = ack.batch_id,
        accepted = ack.accepted,
        rejected = ack.rejected,
        "ColumnarInsertAck received from Origin"
    );
    match &ack.status {
        AckStatus::Applied | AckStatus::Duplicate => {
            let stream_id = stream_id_for(EngineKind::Columnar, &ack.collection);
            delegate.record_stream_ack(stream_id, ack.applied_seq).await;
            // Delete-on-ack: remove the in-flight record and delete the durable entry.
            delegate.ack_columnar_batch_in_flight(ack.batch_id).await;
        }
        AckStatus::Accepted => {
            // Provisional admission, not a terminal outcome: the entry
            // stays in flight until Origin reports whether it applied.
        }
        AckStatus::Fenced => {
            tracing::error!(
                collection = %ack.collection,
                batch_id = ack.batch_id,
                "ColumnarInsertAck: producer fenced by Origin; halting push"
            );
            client.set_fenced();
        }
        AckStatus::Gap { expected } => {
            tracing::warn!(
                collection = %ack.collection,
                batch_id = ack.batch_id,
                expected,
                applied_seq = ack.applied_seq,
                "ColumnarInsertAck: sequence gap detected by Origin; re-draining un-acked entries from expected"
            );
            // Re-drain by clearing all in-flight maps: the push loop will
            // re-send every durable un-acked entry starting from the oldest,
            // which is ≤ expected. Origin deduplicates already-applied entries
            // via its idempotent gate.
            delegate.clear_engine_in_flight().await;
        }
        AckStatus::Rejected { reason } => {
            // Terminal failure: per the wire contract this batch "will never
            // apply". Unlike `Fenced` and `Gap`, both of which are recoverable
            // by re-sending, there is nothing to retry — leaving it in flight
            // would stall the queue behind a batch Origin has permanently
            // refused. Retiring it DOES drop the write, so it is logged at
            // ERROR with Origin's reason rather than lost silently.
            tracing::error!(
                collection = %ack.collection,
                batch_id = ack.batch_id,
                reason = %reason,
                "ColumnarInsertAck: Origin permanently rejected this batch; \
                 dropping it — the write is LOST and will not retry"
            );
            delegate.ack_columnar_batch_in_flight(ack.batch_id).await;
        }
    }
}

pub(super) async fn handle_vector_insert_ack(
    client: &Arc<SyncClient>,
    delegate: &Arc<dyn SyncDelegate>,
    frame: &SyncFrame,
) {
    let Some(ack) = frame.decode_body::<nodedb_types::sync::wire::VectorInsertAckMsg>() else {
        return;
    };
    tracing::debug!(
        collection = %ack.collection,
        id = %ack.id,
        batch_id = ack.batch_id,
        accepted = ack.accepted,
        "VectorInsertAck received from Origin"
    );
    match &ack.status {
        AckStatus::Applied | AckStatus::Duplicate => {
            let stream_id = stream_id_for(EngineKind::Vector, &ack.collection);
            delegate.record_stream_ack(stream_id, ack.applied_seq).await;
            // Delete-on-ack: remove the in-flight record and delete the durable entry.
            delegate.ack_vector_insert_in_flight(ack.batch_id).await;
        }
        AckStatus::Accepted => {
            // Provisional admission, not a terminal outcome: the entry
            // stays in flight until Origin reports whether it applied.
        }
        AckStatus::Fenced => {
            tracing::error!(
                collection = %ack.collection,
                batch_id = ack.batch_id,
                "VectorInsertAck: producer fenced by Origin; halting push"
            );
            client.set_fenced();
        }
        AckStatus::Gap { expected } => {
            tracing::warn!(
                collection = %ack.collection,
                batch_id = ack.batch_id,
                expected,
                applied_seq = ack.applied_seq,
                "VectorInsertAck: sequence gap detected by Origin; re-draining un-acked entries from expected"
            );
            delegate.clear_engine_in_flight().await;
        }
        AckStatus::Rejected { reason } => {
            // Terminal failure: per the wire contract this insert "will never
            // apply". Unlike `Fenced` and `Gap`, both of which are recoverable
            // by re-sending, there is nothing to retry — leaving it in flight
            // would stall the queue behind an insert Origin has permanently
            // refused. Retiring it DOES drop the write, so it is logged at
            // ERROR with Origin's reason rather than lost silently.
            tracing::error!(
                collection = %ack.collection,
                batch_id = ack.batch_id,
                reason = %reason,
                "VectorInsertAck: Origin permanently rejected this insert; \
                 dropping it — the write is LOST and will not retry"
            );
            delegate.ack_vector_insert_in_flight(ack.batch_id).await;
        }
    }
    if !ack.accepted {
        tracing::warn!(
            collection = %ack.collection,
            id = %ack.id,
            reason = ?ack.reject_reason,
            "VectorInsert rejected by Origin"
        );
    }
}

pub(super) async fn handle_vector_delete_ack(
    client: &Arc<SyncClient>,
    delegate: &Arc<dyn SyncDelegate>,
    frame: &SyncFrame,
) {
    let Some(ack) = frame.decode_body::<nodedb_types::sync::wire::VectorDeleteAckMsg>() else {
        return;
    };
    tracing::debug!(
        collection = %ack.collection,
        id = %ack.id,
        batch_id = ack.batch_id,
        accepted = ack.accepted,
        "VectorDeleteAck received from Origin"
    );
    match &ack.status {
        AckStatus::Applied | AckStatus::Duplicate => {
            let stream_id = stream_id_for(EngineKind::Vector, &ack.collection);
            delegate.record_stream_ack(stream_id, ack.applied_seq).await;
            // Delete-on-ack: remove the in-flight record and delete the durable entry.
            delegate.ack_vector_delete_in_flight(ack.batch_id).await;
        }
        AckStatus::Accepted => {
            // Provisional admission, not a terminal outcome: the entry
            // stays in flight until Origin reports whether it applied.
        }
        AckStatus::Fenced => {
            tracing::error!(
                collection = %ack.collection,
                batch_id = ack.batch_id,
                "VectorDeleteAck: producer fenced by Origin; halting push"
            );
            client.set_fenced();
        }
        AckStatus::Gap { expected } => {
            tracing::warn!(
                collection = %ack.collection,
                batch_id = ack.batch_id,
                expected,
                applied_seq = ack.applied_seq,
                "VectorDeleteAck: sequence gap detected by Origin; re-draining un-acked entries from expected"
            );
            delegate.clear_engine_in_flight().await;
        }
        AckStatus::Rejected { reason } => {
            // Terminal failure: per the wire contract this delete "will never
            // apply". Unlike `Fenced` and `Gap`, both of which are recoverable
            // by re-sending, there is nothing to retry — leaving it in flight
            // would stall the queue behind a delete Origin has permanently
            // refused. Retiring it DOES drop the write, so it is logged at
            // ERROR with Origin's reason rather than lost silently.
            tracing::error!(
                collection = %ack.collection,
                batch_id = ack.batch_id,
                reason = %reason,
                "VectorDeleteAck: Origin permanently rejected this delete; \
                 dropping it — the write is LOST and will not retry"
            );
            delegate.ack_vector_delete_in_flight(ack.batch_id).await;
        }
    }
}

pub(super) async fn handle_fts_index_ack(
    client: &Arc<SyncClient>,
    delegate: &Arc<dyn SyncDelegate>,
    frame: &SyncFrame,
) {
    let Some(ack) = frame.decode_body::<nodedb_types::sync::wire::FtsIndexAckMsg>() else {
        return;
    };
    tracing::debug!(
        collection = %ack.collection,
        doc_id = %ack.doc_id,
        batch_id = ack.batch_id,
        accepted = ack.accepted,
        "FtsIndexAck received from Origin"
    );
    match &ack.status {
        AckStatus::Applied | AckStatus::Duplicate => {
            let stream_id = stream_id_for(EngineKind::Fts, &ack.collection);
            delegate.record_stream_ack(stream_id, ack.applied_seq).await;
            // Delete-on-ack: remove the in-flight record and delete the durable entry.
            delegate.ack_fts_index_in_flight(ack.batch_id).await;
        }
        AckStatus::Accepted => {
            // Provisional admission, not a terminal outcome: the entry
            // stays in flight until Origin reports whether it applied.
        }
        AckStatus::Fenced => {
            tracing::error!(
                collection = %ack.collection,
                batch_id = ack.batch_id,
                "FtsIndexAck: producer fenced by Origin; halting push"
            );
            client.set_fenced();
        }
        AckStatus::Gap { expected } => {
            tracing::warn!(
                collection = %ack.collection,
                batch_id = ack.batch_id,
                expected,
                applied_seq = ack.applied_seq,
                "FtsIndexAck: sequence gap detected by Origin; re-draining un-acked entries from expected"
            );
            delegate.clear_engine_in_flight().await;
        }
        AckStatus::Rejected { reason } => {
            // Terminal failure: per the wire contract this index op "will
            // never apply". Unlike `Fenced` and `Gap`, both of which are
            // recoverable by re-sending, there is nothing to retry — leaving
            // it in flight would stall the queue behind an entry Origin has
            // permanently refused. Retiring it DOES drop the write, so it is
            // logged at ERROR with Origin's reason rather than lost silently.
            tracing::error!(
                collection = %ack.collection,
                batch_id = ack.batch_id,
                reason = %reason,
                "FtsIndexAck: Origin permanently rejected this index op; \
                 dropping it — the write is LOST and will not retry"
            );
            delegate.ack_fts_index_in_flight(ack.batch_id).await;
        }
    }
}

pub(super) async fn handle_fts_delete_ack(
    client: &Arc<SyncClient>,
    delegate: &Arc<dyn SyncDelegate>,
    frame: &SyncFrame,
) {
    let Some(ack) = frame.decode_body::<nodedb_types::sync::wire::FtsDeleteAckMsg>() else {
        return;
    };
    tracing::debug!(
        collection = %ack.collection,
        doc_id = %ack.doc_id,
        batch_id = ack.batch_id,
        accepted = ack.accepted,
        "FtsDeleteAck received from Origin"
    );
    match &ack.status {
        AckStatus::Applied | AckStatus::Duplicate => {
            let stream_id = stream_id_for(EngineKind::Fts, &ack.collection);
            delegate.record_stream_ack(stream_id, ack.applied_seq).await;
            // Delete-on-ack: remove the in-flight record and delete the durable entry.
            delegate.ack_fts_delete_in_flight(ack.batch_id).await;
        }
        AckStatus::Accepted => {
            // Provisional admission, not a terminal outcome: the entry
            // stays in flight until Origin reports whether it applied.
        }
        AckStatus::Fenced => {
            tracing::error!(
                collection = %ack.collection,
                batch_id = ack.batch_id,
                "FtsDeleteAck: producer fenced by Origin; halting push"
            );
            client.set_fenced();
        }
        AckStatus::Gap { expected } => {
            tracing::warn!(
                collection = %ack.collection,
                batch_id = ack.batch_id,
                expected,
                applied_seq = ack.applied_seq,
                "FtsDeleteAck: sequence gap detected by Origin; re-draining un-acked entries from expected"
            );
            delegate.clear_engine_in_flight().await;
        }
        AckStatus::Rejected { reason } => {
            // Terminal failure: per the wire contract this delete op "will
            // never apply". Unlike `Fenced` and `Gap`, both of which are
            // recoverable by re-sending, there is nothing to retry — leaving
            // it in flight would stall the queue behind an entry Origin has
            // permanently refused. Retiring it DOES drop the write, so it is
            // logged at ERROR with Origin's reason rather than lost silently.
            tracing::error!(
                collection = %ack.collection,
                batch_id = ack.batch_id,
                reason = %reason,
                "FtsDeleteAck: Origin permanently rejected this delete op; \
                 dropping it — the write is LOST and will not retry"
            );
            delegate.ack_fts_delete_in_flight(ack.batch_id).await;
        }
    }
}

pub(super) async fn handle_spatial_insert_ack(
    client: &Arc<SyncClient>,
    delegate: &Arc<dyn SyncDelegate>,
    frame: &SyncFrame,
) {
    let Some(ack) = frame.decode_body::<nodedb_types::sync::wire::SpatialInsertAckMsg>() else {
        return;
    };
    tracing::debug!(
        collection = %ack.collection,
        field = %ack.field,
        doc_id = %ack.doc_id,
        batch_id = ack.batch_id,
        accepted = ack.accepted,
        "SpatialInsertAck received from Origin"
    );
    match &ack.status {
        AckStatus::Applied | AckStatus::Duplicate => {
            let stream_id = stream_id_for(EngineKind::Spatial, &ack.collection);
            delegate.record_stream_ack(stream_id, ack.applied_seq).await;
            // Delete-on-ack: remove the in-flight record and delete the durable entry.
            delegate.ack_spatial_insert_in_flight(ack.batch_id).await;
        }
        AckStatus::Accepted => {
            // Provisional admission, not a terminal outcome: the entry
            // stays in flight until Origin reports whether it applied.
        }
        AckStatus::Fenced => {
            tracing::error!(
                collection = %ack.collection,
                batch_id = ack.batch_id,
                "SpatialInsertAck: producer fenced by Origin; halting push"
            );
            client.set_fenced();
        }
        AckStatus::Gap { expected } => {
            tracing::warn!(
                collection = %ack.collection,
                batch_id = ack.batch_id,
                expected,
                applied_seq = ack.applied_seq,
                "SpatialInsertAck: sequence gap detected by Origin; re-draining un-acked entries from expected"
            );
            delegate.clear_engine_in_flight().await;
        }
        AckStatus::Rejected { reason } => {
            // Terminal failure: per the wire contract this insert "will never
            // apply". Unlike `Fenced` and `Gap`, both of which are recoverable
            // by re-sending, there is nothing to retry — leaving it in flight
            // would stall the queue behind an insert Origin has permanently
            // refused. Retiring it DOES drop the write, so it is logged at
            // ERROR with Origin's reason rather than lost silently.
            tracing::error!(
                collection = %ack.collection,
                batch_id = ack.batch_id,
                reason = %reason,
                "SpatialInsertAck: Origin permanently rejected this insert; \
                 dropping it — the write is LOST and will not retry"
            );
            delegate.ack_spatial_insert_in_flight(ack.batch_id).await;
        }
    }
}

pub(super) async fn handle_spatial_delete_ack(
    client: &Arc<SyncClient>,
    delegate: &Arc<dyn SyncDelegate>,
    frame: &SyncFrame,
) {
    let Some(ack) = frame.decode_body::<nodedb_types::sync::wire::SpatialDeleteAckMsg>() else {
        return;
    };
    tracing::debug!(
        collection = %ack.collection,
        field = %ack.field,
        doc_id = %ack.doc_id,
        batch_id = ack.batch_id,
        accepted = ack.accepted,
        "SpatialDeleteAck received from Origin"
    );
    match &ack.status {
        AckStatus::Applied | AckStatus::Duplicate => {
            let stream_id = stream_id_for(EngineKind::Spatial, &ack.collection);
            delegate.record_stream_ack(stream_id, ack.applied_seq).await;
            // Delete-on-ack: remove the in-flight record and delete the durable entry.
            delegate.ack_spatial_delete_in_flight(ack.batch_id).await;
        }
        AckStatus::Accepted => {
            // Provisional admission, not a terminal outcome: the entry
            // stays in flight until Origin reports whether it applied.
        }
        AckStatus::Fenced => {
            tracing::error!(
                collection = %ack.collection,
                batch_id = ack.batch_id,
                "SpatialDeleteAck: producer fenced by Origin; halting push"
            );
            client.set_fenced();
        }
        AckStatus::Gap { expected } => {
            tracing::warn!(
                collection = %ack.collection,
                batch_id = ack.batch_id,
                expected,
                applied_seq = ack.applied_seq,
                "SpatialDeleteAck: sequence gap detected by Origin; re-draining un-acked entries from expected"
            );
            delegate.clear_engine_in_flight().await;
        }
        AckStatus::Rejected { reason } => {
            // Terminal failure: per the wire contract this delete "will never
            // apply". Unlike `Fenced` and `Gap`, both of which are recoverable
            // by re-sending, there is nothing to retry — leaving it in flight
            // would stall the queue behind a delete Origin has permanently
            // refused. Retiring it DOES drop the write, so it is logged at
            // ERROR with Origin's reason rather than lost silently.
            tracing::error!(
                collection = %ack.collection,
                batch_id = ack.batch_id,
                reason = %reason,
                "SpatialDeleteAck: Origin permanently rejected this delete; \
                 dropping it — the write is LOST and will not retry"
            );
            delegate.ack_spatial_delete_in_flight(ack.batch_id).await;
        }
    }
}

pub(super) async fn handle_timeseries_ack(
    client: &Arc<SyncClient>,
    delegate: &Arc<dyn SyncDelegate>,
    frame: &SyncFrame,
) {
    let Some(ack) = frame.decode_body::<nodedb_types::sync::wire::TimeseriesAckMsg>() else {
        return;
    };
    tracing::debug!(
        collection = %ack.collection,
        accepted = ack.accepted,
        rejected = ack.rejected,
        lsn = ack.lsn,
        "TimeseriesAck received from Origin"
    );
    match &ack.status {
        AckStatus::Applied | AckStatus::Duplicate => {
            let stream_id = stream_id_for(EngineKind::Timeseries, &ack.collection);
            delegate.record_stream_ack(stream_id, ack.applied_seq).await;
            // Delete-on-ack: delete all durable entries whose seq ≤ applied_seq.
            delegate
                .ack_timeseries_batches_through_seq(ack.applied_seq)
                .await;
        }
        AckStatus::Accepted => {
            // Provisional admission, not a terminal outcome: the entry
            // stays in flight until Origin reports whether it applied.
        }
        AckStatus::Fenced => {
            tracing::error!(
                collection = %ack.collection,
                "TimeseriesAck: producer fenced by Origin; halting push"
            );
            client.set_fenced();
        }
        AckStatus::Gap { expected } => {
            tracing::warn!(
                collection = %ack.collection,
                expected,
                applied_seq = ack.applied_seq,
                "TimeseriesAck: sequence gap detected by Origin; re-draining un-acked entries from expected"
            );
            delegate.clear_engine_in_flight().await;
        }
        AckStatus::Rejected { reason } => {
            // Terminal failure: per the wire contract this batch "will never
            // apply". Unlike `Fenced` and `Gap`, both of which are recoverable
            // by re-sending, there is nothing to retry — so it is retired by
            // `batch_id`, naming exactly the batch Origin refused.
            //
            // The `applied_seq` sweep the success arm uses CANNOT serve this
            // case: a rejected batch never applied, so it never advances the
            // frontier and its seq sits above `applied_seq`. Retiring through
            // the frontier would skip it, and the push loop would re-send a
            // batch Origin has permanently refused, forever.
            //
            // Retiring DOES drop the write, so it is logged at ERROR with
            // Origin's reason rather than lost silently.
            tracing::error!(
                collection = %ack.collection,
                batch_id = ack.batch_id,
                applied_seq = ack.applied_seq,
                reason = %reason,
                "TimeseriesAck: Origin permanently rejected this batch; \
                 dropping it — the write is LOST and will not retry"
            );
            delegate.ack_timeseries_batch_by_id(ack.batch_id).await;
        }
    }
}