moire-web 2.0.0-rc.0

Web dashboard server for moire: REST API, MCP tools, and live entity graph viewer
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
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
use std::sync::Arc;

use facet::Facet;
use moire_trace_types::{BacktraceId, ModuleId, RelPc, RuntimeBase};
use moire_types::{ConnectionId, ProcessId};
use moire_wire::{BacktraceRecord, ModuleIdentity, ModuleManifestEntry};
use rusqlite_facet::{ConnectionFacetExt, StatementFacetExt};

use crate::db::Db;
use crate::util::time::now_nanos;

#[derive(Clone)]
pub struct BacktraceFramePersist {
    pub frame_index: u32,
    pub rel_pc: RelPc,
    pub module_path: String,
    pub module_identity: String,
}

#[derive(Clone)]
pub struct StoredModuleManifestEntry {
    pub module_id: ModuleId,
    pub module_path: String,
    pub module_identity: String,
    pub arch: String,
    pub runtime_base: RuntimeBase,
}

#[derive(Facet)]
struct ConnectionUpsertParams {
    conn_id: ConnectionId,
    process_id: ProcessId,
    process_name: String,
    pid: u32,
    connected_at_ns: i64,
}

#[derive(Facet)]
struct ConnectionClosedParams {
    conn_id: ConnectionId,
    disconnected_at_ns: i64,
}

#[derive(Facet)]
struct ProcessIdParams {
    process_id: ProcessId,
}

#[derive(Facet)]
struct ConnectionModuleInsertParams {
    process_id: ProcessId,
    module_id: ModuleId,
    module_index: i64,
    module_path: String,
    module_identity: String,
    arch: String,
    runtime_base: RuntimeBase,
}

#[derive(Facet)]
struct BacktraceInsertParams {
    process_id: ProcessId,
    backtrace_id: BacktraceId,
    frame_count: i64,
    received_at_ns: i64,
}

#[derive(Facet)]
struct BacktraceFrameInsertParams {
    process_id: ProcessId,
    backtrace_id: BacktraceId,
    frame_index: u32,
    module_path: String,
    module_identity: String,
    rel_pc: RelPc,
}

#[derive(Facet)]
struct CutRequestParams {
    cut_id: String,
    requested_at_ns: i64,
}

#[derive(Facet)]
struct CutAckParams {
    cut_id: String,
    process_id: ProcessId,
    next_seq_no: u64,
    received_at_ns: i64,
}

#[derive(Facet)]
struct DeltaBatchInsertParams {
    process_id: ProcessId,
    from_seq_no: u64,
    next_seq_no: u64,
    truncated: i64,
    compacted_before_seq_no: Option<u64>,
    change_count: u64,
    payload_json: String,
    received_at_ns: i64,
}

#[derive(Facet)]
struct UpsertEntityParams {
    process_id: ProcessId,
    entity_id: String,
    entity_json: String,
    updated_at_ns: i64,
}

#[derive(Facet)]
struct UpsertScopeParams {
    process_id: ProcessId,
    scope_id: String,
    scope_json: String,
    updated_at_ns: i64,
}

#[derive(Facet)]
struct UpsertEntityScopeLinkParams {
    process_id: ProcessId,
    entity_id: String,
    scope_id: String,
    updated_at_ns: i64,
}

#[derive(Facet)]
struct RemoveEntityParams {
    process_id: ProcessId,
    entity_id: String,
}

#[derive(Facet)]
struct RemoveScopeParams {
    process_id: ProcessId,
    scope_id: String,
}

#[derive(Facet)]
struct RemoveEntityScopeLinkParams {
    process_id: ProcessId,
    entity_id: String,
    scope_id: String,
}

#[derive(Facet)]
struct UpsertEdgeParams {
    process_id: ProcessId,
    src_id: String,
    dst_id: String,
    kind_json: String,
    edge_json: String,
    updated_at_ns: i64,
}

#[derive(Facet)]
struct RemoveEdgeParams {
    process_id: ProcessId,
    src_id: String,
    dst_id: String,
    kind_json: String,
}

#[derive(Facet)]
struct AppendEventParams {
    process_id: ProcessId,
    seq_no: u64,
    event_id: String,
    event_json: String,
    at_ms: u64,
}

#[derive(Facet)]
struct StreamCursorUpsertParams {
    process_id: ProcessId,
    next_seq_no: u64,
    updated_at_ns: i64,
}

pub fn backtrace_frames_for_store(
    module_manifest: &[StoredModuleManifestEntry],
    record: &BacktraceRecord,
) -> Result<Vec<BacktraceFramePersist>, String> {
    let modules_by_id = module_manifest
        .iter()
        .map(|module| (module.module_id, module))
        .collect::<std::collections::BTreeMap<_, _>>();
    let mut frames = Vec::with_capacity(record.frames.len());
    for (frame_index, frame) in record.frames.iter().enumerate() {
        let module_id = frame.module_id;
        let Some(module) = modules_by_id.get(&module_id) else {
            return Err(format!(
                "invariant violated: backtrace frame {frame_index} references module_id {} but handshake manifest for this connection has no matching module id ({} entries)",
                module_id,
                modules_by_id.len()
            ));
        };
        frames.push(BacktraceFramePersist {
            frame_index: frame_index as u32,
            rel_pc: frame.rel_pc,
            module_path: module.module_path.clone(),
            module_identity: module.module_identity.clone(),
        });
    }
    Ok(frames)
}

pub fn into_stored_module_manifest(
    module_manifest: Vec<ModuleManifestEntry>,
) -> Vec<StoredModuleManifestEntry> {
    module_manifest
        .into_iter()
        .map(|module| StoredModuleManifestEntry {
            module_id: module.module_id,
            module_path: module.module_path,
            module_identity: module_identity_key(&module.identity),
            arch: module.arch,
            runtime_base: module.runtime_base,
        })
        .collect()
}

fn module_identity_key(identity: &ModuleIdentity) -> String {
    match identity {
        ModuleIdentity::BuildId(build_id) => format!("build_id:{build_id}"),
        ModuleIdentity::DebugId(debug_id) => format!("debug_id:{debug_id}"),
    }
}

pub async fn persist_connection_upsert(
    db: Arc<Db>,
    conn_id: ConnectionId,
    process_id: ProcessId,
    process_name: String,
    pid: u32,
) -> Result<(), String> {
    tokio::task::spawn_blocking(move || {
        let conn = db.open()?;
        conn.facet_execute_ref(
            "INSERT INTO connections (conn_id, process_id, process_name, pid, connected_at_ns, disconnected_at_ns)
             VALUES (:conn_id, :process_id, :process_name, :pid, :connected_at_ns, NULL)
             ON CONFLICT(conn_id) DO UPDATE SET
               process_id = excluded.process_id,
               process_name = excluded.process_name,
               pid = excluded.pid",
            &ConnectionUpsertParams {
                conn_id,
                process_id,
                process_name,
                pid,
                connected_at_ns: now_nanos(),
            },
        )
        .map_err(|error| format!("upsert connection: {error}"))?;
        Ok::<(), String>(())
    })
    .await
    .map_err(|error| format!("join sqlite: {error}"))?
}

pub async fn persist_connection_closed(db: Arc<Db>, conn_id: ConnectionId) -> Result<(), String> {
    tokio::task::spawn_blocking(move || {
        let conn = db.open()?;
        conn.facet_execute_ref(
            "UPDATE connections
             SET disconnected_at_ns = :disconnected_at_ns
             WHERE conn_id = :conn_id",
            &ConnectionClosedParams {
                conn_id,
                disconnected_at_ns: now_nanos(),
            },
        )
        .map_err(|error| format!("close connection: {error}"))?;
        Ok::<(), String>(())
    })
    .await
    .map_err(|error| format!("join sqlite: {error}"))?
}

pub async fn persist_connection_module_manifest(
    db: Arc<Db>,
    process_id: ProcessId,
    module_manifest: Vec<StoredModuleManifestEntry>,
) -> Result<(), String> {
    tokio::task::spawn_blocking(move || {
        let mut conn = db.open()?;
        let tx = conn
            .transaction()
            .map_err(|error| format!("start transaction: {error}"))?;
        {
            let mut delete_stmt = tx
                .prepare("DELETE FROM connection_modules WHERE process_id = :process_id")
                .map_err(|error| format!("prepare delete connection_modules: {error}"))?;
            delete_stmt
                .facet_execute_ref(&ProcessIdParams {
                    process_id: process_id.clone(),
                })
                .map_err(|error| format!("delete connection_modules: {error}"))?;
        }

        {
            let mut insert_stmt = tx
                .prepare(
                    "INSERT INTO connection_modules (
                        process_id, module_id, module_index, module_path, module_identity, arch, runtime_base
                     ) VALUES (
                        :process_id, :module_id, :module_index, :module_path, :module_identity, :arch, :runtime_base
                     )",
                )
                .map_err(|error| format!("prepare insert connection_modules: {error}"))?;
            for (module_index, module) in module_manifest.iter().enumerate() {
                insert_stmt
                    .facet_execute_ref(&ConnectionModuleInsertParams {
                        process_id: process_id.clone(),
                        module_id: module.module_id,
                        module_index: module_index as i64,
                        module_path: module.module_path.clone(),
                        module_identity: module.module_identity.clone(),
                        arch: module.arch.clone(),
                        runtime_base: module.runtime_base,
                    })
                    .map_err(|error| format!("insert connection_module[{module_index}]: {error}"))?;
            }
        }
        tx.commit()
            .map_err(|error| format!("commit connection_modules: {error}"))?;
        Ok::<(), String>(())
    })
    .await
    .map_err(|error| format!("join sqlite: {error}"))?
}

// r[impl symbolicate.server-store]
pub async fn persist_backtrace_record(
    db: Arc<Db>,
    process_id: ProcessId,
    backtrace_id: BacktraceId,
    frames: Vec<BacktraceFramePersist>,
) -> Result<bool, String> {
    tokio::task::spawn_blocking(move || {
        let mut conn = db.open()?;
        let tx = conn
            .transaction()
            .map_err(|error| format!("start transaction: {error}"))?;
        let inserted = {
            let mut insert_backtrace_stmt = tx
                .prepare(
                    "INSERT INTO backtraces (process_id, backtrace_id, frame_count, received_at_ns)
                     VALUES (:process_id, :backtrace_id, :frame_count, :received_at_ns)
                     ON CONFLICT(backtrace_id) DO NOTHING",
                )
                .map_err(|error| format!("prepare insert backtrace: {error}"))?;
            insert_backtrace_stmt
                .facet_execute_ref(&BacktraceInsertParams {
                    process_id: process_id.clone(),
                    backtrace_id,
                    frame_count: frames.len() as i64,
                    received_at_ns: now_nanos(),
                })
                .map_err(|error| format!("insert backtrace: {error}"))?
                > 0
        };
        if inserted {
            {
                let mut insert_frame_stmt = tx
                    .prepare(
                        "INSERT INTO backtrace_frames (
                            process_id, backtrace_id, frame_index, module_path, module_identity, rel_pc
                         ) VALUES (
                            :process_id, :backtrace_id, :frame_index, :module_path, :module_identity, :rel_pc
                         )",
                    )
                    .map_err(|error| format!("prepare insert backtrace frames: {error}"))?;
                for frame in &frames {
                    insert_frame_stmt
                        .facet_execute_ref(&BacktraceFrameInsertParams {
                            process_id: process_id.clone(),
                            backtrace_id,
                            frame_index: frame.frame_index,
                            module_path: frame.module_path.clone(),
                            module_identity: frame.module_identity.clone(),
                            rel_pc: frame.rel_pc,
                        })
                        .map_err(|error| {
                            format!(
                                "insert backtrace frame {}/{}: {error}",
                                frame.frame_index,
                                backtrace_id
                            )
                        })?;
                }
            }
        }
        tx.commit()
            .map_err(|error| format!("commit backtrace record: {error}"))?;
        Ok::<bool, String>(inserted)
    })
    .await
    .map_err(|error| format!("join sqlite: {error}"))?
}

pub async fn persist_cut_request(
    db: Arc<Db>,
    cut_id: String,
    requested_at_ns: i64,
) -> Result<(), String> {
    tokio::task::spawn_blocking(move || {
        let conn = db.open()?;
        conn.facet_execute_ref(
            "INSERT INTO cuts (cut_id, requested_at_ns) VALUES (?1, ?2)
             ON CONFLICT(cut_id) DO UPDATE SET requested_at_ns = excluded.requested_at_ns",
            &CutRequestParams {
                cut_id,
                requested_at_ns,
            },
        )
        .map_err(|error| format!("upsert cut: {error}"))?;
        Ok::<(), String>(())
    })
    .await
    .map_err(|error| format!("join sqlite: {error}"))?
}

pub async fn persist_cut_ack(
    db: Arc<Db>,
    cut_id: String,
    process_id: ProcessId,
    stream_id: String,
    next_seq_no: u64,
) -> Result<(), String> {
    tokio::task::spawn_blocking(move || {
        if stream_id != process_id.as_str() {
            return Err(format!(
                "invariant violated: cut ack stream_id '{}' does not match process_id '{}'",
                stream_id,
                process_id.as_str()
            ));
        }
        let conn = db.open()?;
        conn.facet_execute_ref(
            "INSERT INTO cut_acks (cut_id, process_id, next_seq_no, received_at_ns)
             VALUES (?1, ?2, ?3, ?4)
             ON CONFLICT(cut_id, process_id) DO UPDATE SET
               next_seq_no = excluded.next_seq_no,
               received_at_ns = excluded.received_at_ns",
            &CutAckParams {
                cut_id,
                process_id,
                next_seq_no,
                received_at_ns: now_nanos(),
            },
        )
        .map_err(|error| format!("upsert cut ack: {error}"))?;
        Ok::<(), String>(())
    })
    .await
    .map_err(|error| format!("join sqlite: {error}"))?
}

pub async fn persist_delta_batch(
    db: Arc<Db>,
    process_id: ProcessId,
    batch: moire_types::PullChangesResponse,
) -> Result<(), String> {
    tokio::task::spawn_blocking(move || persist_delta_batch_blocking(&db, process_id, &batch))
        .await
        .map_err(|error| format!("join sqlite: {error}"))?
}

fn persist_delta_batch_blocking(
    db: &Db,
    process_id: ProcessId,
    batch: &moire_types::PullChangesResponse,
) -> Result<(), String> {
    use moire_types::Change;

    let mut conn = db.open()?;
    let tx = conn
        .transaction()
        .map_err(|error| format!("start transaction: {error}"))?;
    if batch.stream_id.0.as_str() != process_id.as_str() {
        return Err(format!(
            "invariant violated: delta batch stream_id '{}' does not match process_id '{}'",
            batch.stream_id.0,
            process_id.as_str()
        ));
    }
    let received_at_ns = now_nanos();
    let payload_json =
        facet_json::to_string(batch).map_err(|error| format!("encode batch: {error}"))?;

    {
        let mut insert_delta_batch_stmt = tx
            .prepare(
                "INSERT INTO delta_batches (
                process_id, from_seq_no, next_seq_no, truncated,
                compacted_before_seq_no, change_count, payload_json, received_at_ns
             ) VALUES (
                :process_id, :from_seq_no, :next_seq_no, :truncated,
                :compacted_before_seq_no, :change_count, :payload_json, :received_at_ns
             )",
            )
            .map_err(|error| format!("prepare delta batch insert: {error}"))?;
        insert_delta_batch_stmt
            .facet_execute_ref(&DeltaBatchInsertParams {
                process_id: process_id.clone(),
                from_seq_no: batch.from_seq_no.0,
                next_seq_no: batch.next_seq_no.0,
                truncated: if batch.truncated { 1 } else { 0 },
                compacted_before_seq_no: batch.compacted_before_seq_no.map(|seq_no| seq_no.0),
                change_count: batch.changes.len() as u64,
                payload_json,
                received_at_ns,
            })
            .map_err(|error| format!("insert delta batch: {error}"))?;

        let mut upsert_entity_stmt = tx
            .prepare(
                "INSERT INTO entities (process_id, entity_id, entity_json, updated_at_ns)
             VALUES (:process_id, :entity_id, :entity_json, :updated_at_ns)
             ON CONFLICT(entity_id) DO UPDATE SET
               process_id = excluded.process_id,
               entity_json = excluded.entity_json,
               updated_at_ns = excluded.updated_at_ns",
            )
            .map_err(|error| format!("prepare entity upsert: {error}"))?;
        let mut upsert_scope_stmt = tx
            .prepare(
                "INSERT INTO scopes (process_id, scope_id, scope_json, updated_at_ns)
             VALUES (:process_id, :scope_id, :scope_json, :updated_at_ns)
             ON CONFLICT(scope_id) DO UPDATE SET
               process_id = excluded.process_id,
               scope_json = excluded.scope_json,
               updated_at_ns = excluded.updated_at_ns",
            )
            .map_err(|error| format!("prepare scope upsert: {error}"))?;
        let mut upsert_entity_scope_link_stmt = tx
            .prepare(
                "INSERT INTO entity_scope_links (process_id, entity_id, scope_id, updated_at_ns)
             VALUES (:process_id, :entity_id, :scope_id, :updated_at_ns)
             ON CONFLICT(entity_id, scope_id) DO UPDATE SET
               process_id = excluded.process_id,
               updated_at_ns = excluded.updated_at_ns",
            )
            .map_err(|error| format!("prepare entity_scope_link upsert: {error}"))?;
        let mut delete_entity_stmt = tx
            .prepare(
                "DELETE FROM entities
             WHERE process_id = :process_id AND entity_id = :entity_id",
            )
            .map_err(|error| format!("prepare delete entity: {error}"))?;
        let mut delete_entity_scope_links_for_entity_stmt = tx
            .prepare(
                "DELETE FROM entity_scope_links
             WHERE process_id = :process_id AND entity_id = :entity_id",
            )
            .map_err(|error| format!("prepare delete entity_scope_links for entity: {error}"))?;
        let mut delete_incident_edges_stmt = tx
            .prepare(
                "DELETE FROM edges
             WHERE process_id = :process_id
               AND (src_id = :entity_id OR dst_id = :entity_id)",
            )
            .map_err(|error| format!("prepare delete incident edges: {error}"))?;
        let mut delete_scope_stmt = tx
            .prepare(
                "DELETE FROM scopes
             WHERE process_id = :process_id AND scope_id = :scope_id",
            )
            .map_err(|error| format!("prepare delete scope: {error}"))?;
        let mut delete_entity_scope_links_for_scope_stmt = tx
            .prepare(
                "DELETE FROM entity_scope_links
             WHERE process_id = :process_id AND scope_id = :scope_id",
            )
            .map_err(|error| format!("prepare delete entity_scope_links for scope: {error}"))?;
        let mut delete_entity_scope_link_stmt = tx
            .prepare(
                "DELETE FROM entity_scope_links
             WHERE process_id = :process_id
               AND entity_id = :entity_id AND scope_id = :scope_id",
            )
            .map_err(|error| format!("prepare delete entity_scope_link: {error}"))?;
        let mut upsert_edge_stmt = tx
        .prepare(
            "INSERT INTO edges (process_id, src_id, dst_id, kind_json, edge_json, updated_at_ns)
             VALUES (:process_id, :src_id, :dst_id, :kind_json, :edge_json, :updated_at_ns)
             ON CONFLICT(src_id, dst_id, kind_json) DO UPDATE SET
               process_id = excluded.process_id,
               edge_json = excluded.edge_json,
               updated_at_ns = excluded.updated_at_ns",
        )
        .map_err(|error| format!("prepare edge upsert: {error}"))?;
        let mut delete_edge_stmt = tx
            .prepare(
                "DELETE FROM edges
             WHERE process_id = :process_id
               AND src_id = :src_id AND dst_id = :dst_id AND kind_json = :kind_json",
            )
            .map_err(|error| format!("prepare delete edge: {error}"))?;
        let mut append_event_stmt = tx
            .prepare(
                "INSERT OR REPLACE INTO events (process_id, seq_no, event_id, event_json, at_ms)
             VALUES (:process_id, :seq_no, :event_id, :event_json, :at_ms)",
            )
            .map_err(|error| format!("prepare append event: {error}"))?;

        for stamped in &batch.changes {
            match &stamped.change {
                Change::UpsertEntity(entity) => {
                    let entity_json = facet_json::to_string(entity)
                        .map_err(|error| format!("encode entity: {error}"))?;
                    upsert_entity_stmt
                        .facet_execute_ref(&UpsertEntityParams {
                            process_id: process_id.clone(),
                            entity_id: entity.id.as_str().to_string(),
                            entity_json,
                            updated_at_ns: received_at_ns,
                        })
                        .map_err(|error| format!("upsert entity: {error}"))?;
                }
                Change::UpsertScope(scope) => {
                    let scope_json = facet_json::to_string(scope)
                        .map_err(|error| format!("encode scope: {error}"))?;
                    upsert_scope_stmt
                        .facet_execute_ref(&UpsertScopeParams {
                            process_id: process_id.clone(),
                            scope_id: scope.id.as_str().to_string(),
                            scope_json,
                            updated_at_ns: received_at_ns,
                        })
                        .map_err(|error| format!("upsert scope: {error}"))?;
                }
                Change::UpsertEntityScopeLink {
                    entity_id,
                    scope_id,
                } => {
                    upsert_entity_scope_link_stmt
                        .facet_execute_ref(&UpsertEntityScopeLinkParams {
                            process_id: process_id.clone(),
                            entity_id: entity_id.as_str().to_string(),
                            scope_id: scope_id.as_str().to_string(),
                            updated_at_ns: received_at_ns,
                        })
                        .map_err(|error| format!("upsert entity_scope_link: {error}"))?;
                }
                Change::RemoveEntity { id } => {
                    let params = RemoveEntityParams {
                        process_id: process_id.clone(),
                        entity_id: id.as_str().to_string(),
                    };
                    delete_entity_stmt
                        .facet_execute_ref(&params)
                        .map_err(|error| format!("delete entity: {error}"))?;
                    delete_entity_scope_links_for_entity_stmt
                        .facet_execute_ref(&params)
                        .map_err(|error| {
                            format!("delete entity_scope_links for entity: {error}")
                        })?;
                    delete_incident_edges_stmt
                        .facet_execute_ref(&params)
                        .map_err(|error| format!("delete incident edges: {error}"))?;
                }
                Change::RemoveScope { id } => {
                    let params = RemoveScopeParams {
                        process_id: process_id.clone(),
                        scope_id: id.as_str().to_string(),
                    };
                    delete_scope_stmt
                        .facet_execute_ref(&params)
                        .map_err(|error| format!("delete scope: {error}"))?;
                    delete_entity_scope_links_for_scope_stmt
                        .facet_execute_ref(&params)
                        .map_err(|error| format!("delete entity_scope_links for scope: {error}"))?;
                }
                Change::RemoveEntityScopeLink {
                    entity_id,
                    scope_id,
                } => {
                    delete_entity_scope_link_stmt
                        .facet_execute_ref(&RemoveEntityScopeLinkParams {
                            process_id: process_id.clone(),
                            entity_id: entity_id.as_str().to_string(),
                            scope_id: scope_id.as_str().to_string(),
                        })
                        .map_err(|error| format!("delete entity_scope_link: {error}"))?;
                }
                Change::UpsertEdge(edge) => {
                    let kind_json = facet_json::to_string(&edge.kind)
                        .map_err(|error| format!("encode edge kind: {error}"))?;
                    let edge_json = facet_json::to_string(edge)
                        .map_err(|error| format!("encode edge: {error}"))?;
                    upsert_edge_stmt
                        .facet_execute_ref(&UpsertEdgeParams {
                            process_id: process_id.clone(),
                            src_id: edge.src.as_str().to_string(),
                            dst_id: edge.dst.as_str().to_string(),
                            kind_json,
                            edge_json,
                            updated_at_ns: received_at_ns,
                        })
                        .map_err(|error| format!("upsert edge: {error}"))?;
                }
                Change::RemoveEdge { src, dst, kind } => {
                    let kind_json = facet_json::to_string(kind)
                        .map_err(|error| format!("encode edge kind: {error}"))?;
                    delete_edge_stmt
                        .facet_execute_ref(&RemoveEdgeParams {
                            process_id: process_id.clone(),
                            src_id: src.as_str().to_string(),
                            dst_id: dst.as_str().to_string(),
                            kind_json,
                        })
                        .map_err(|error| format!("delete edge: {error}"))?;
                }
                Change::AppendEvent(event) => {
                    let event_json = facet_json::to_string(event)
                        .map_err(|error| format!("encode event: {error}"))?;
                    append_event_stmt
                        .facet_execute_ref(&AppendEventParams {
                            process_id: process_id.clone(),
                            seq_no: stamped.seq_no.0,
                            event_id: event.id.as_str().to_string(),
                            event_json,
                            at_ms: event.at.as_millis(),
                        })
                        .map_err(|error| format!("append event: {error}"))?;
                }
            }
        }

        let mut upsert_stream_cursor_stmt = tx
            .prepare(
                "INSERT INTO stream_cursors (process_id, next_seq_no, updated_at_ns)
             VALUES (:process_id, :next_seq_no, :updated_at_ns)
             ON CONFLICT(process_id) DO UPDATE SET
               next_seq_no = excluded.next_seq_no,
               updated_at_ns = excluded.updated_at_ns",
            )
            .map_err(|error| format!("prepare stream cursor upsert: {error}"))?;
        upsert_stream_cursor_stmt
            .facet_execute_ref(&StreamCursorUpsertParams {
                process_id: process_id.clone(),
                next_seq_no: batch.next_seq_no.0,
                updated_at_ns: received_at_ns,
            })
            .map_err(|error| format!("upsert stream cursor: {error}"))?;
    }

    tx.commit()
        .map_err(|error| format!("commit transaction: {error}"))?;
    Ok(())
}