udb 0.3.7

Universal Data Broker — a Rust gRPC broker over multiple databases (Postgres, MySQL, SQLite, MongoDB, ClickHouse, Cassandra, MSSQL, Redis, Qdrant, S3, Neo4j, …) with per-tenant RLS, 2PC, sagas, and CDC.
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
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
//! Shared canonical `SystemStores` adapter for vector/search REST backends.
//!
//! Pinecone, Weaviate, and Elasticsearch already have native HTTP clients in
//! the runtime. This module gives them a real canonical system-state plane
//! instead of keeping them as projection-only islands. The adapter stores UDB
//! system records in a dedicated backend namespace/index/class per instance,
//! using deterministic IDs derived from logical record keys.

use std::time::{Duration, Instant};

use async_trait::async_trait;
use chrono::Utc;
use serde::{Deserialize, Serialize};
use serde_json::{Value as JsonValue, json};
use sha2::{Digest, Sha256};
use uuid::Uuid;

#[cfg(feature = "elasticsearch")]
use crate::runtime::executors::elasticsearch::ElasticsearchHttpClient;
#[cfg(feature = "pinecone")]
use crate::runtime::executors::pinecone::PineconeHttpClient;
#[cfg(feature = "weaviate")]
use crate::runtime::executors::weaviate::WeaviateHttpClient;

use super::system_store::{
    AdminAuditChainReport, AdminAuditInsert, AdminAuditListFilter, AdminAuditRow, AdvisoryLeaseRow,
    CompensationStatus, DeadLetterGroup, JsonProjectionTaskAdapter, JsonSystemRecordAdapter,
    MigrationAuditStore, MigrationOpInsert, MigrationOpRow, MigrationRunInsert, MigrationRunRow,
    MigrationRunState, MigrationRunsFilter, PendingTaskMetric, ProjectionClaimFilter,
    ProjectionTaskFailurePolicy, ProjectionTaskInsert, ProjectionTaskRow, ProjectionTaskStatus,
    ProjectionTaskStore, ProjectionTaskSummary, SagaInsert, SagaListFilter, SagaRow, SagaStatus,
    SagaStore, SagaSummary, SystemStoreError, SystemStoreResult, advisory_lease_can_acquire,
    advisory_lease_is_owned_by, append_json_admin_audit, claim_json_projection_tasks,
    claim_json_recoverable_sagas, enqueue_json_projection_task, get_json_migration_run,
    get_json_saga, increment_json_saga_recovery_attempts, latest_json_admin_audit_hash,
    list_json_admin_audit, list_json_migration_ops, list_json_migration_runs, list_json_sagas,
    mark_json_projection_task_completed, mark_json_projection_task_failed,
    mark_json_stale_sagas_indeterminate, new_advisory_lease_row,
    pending_json_projection_task_count, pending_projection_task_metrics,
    projection_dead_letter_groups, record_json_saga, requeue_json_dead_letter_by_source,
    requeue_json_dead_letter_tasks, reset_stale_json_in_progress_tasks, start_json_migration_run,
    summarize_json_sagas, summarize_projection_tasks, update_json_saga_status,
    verify_json_admin_audit_chain,
};
use super::{CanonicalStore, DurabilityToken};

const VECTOR_SYSTEM_DURABILITY_POLL_MS: u64 = 10;
const KV_MEMBERSHIP_MAX_ITEMS: usize = 10_000;

#[derive(Debug, Clone)]
enum VectorSystemClient {
    #[cfg(feature = "pinecone")]
    Pinecone(PineconeHttpClient),
    #[cfg(feature = "weaviate")]
    Weaviate(WeaviateHttpClient),
    #[cfg(feature = "elasticsearch")]
    Elasticsearch(ElasticsearchHttpClient),
}

impl VectorSystemClient {
    fn backend_label(&self) -> &'static str {
        match self {
            #[cfg(feature = "pinecone")]
            Self::Pinecone(_) => "pinecone",
            #[cfg(feature = "weaviate")]
            Self::Weaviate(_) => "weaviate",
            #[cfg(feature = "elasticsearch")]
            Self::Elasticsearch(_) => "elasticsearch",
        }
    }

    async fn request_json(
        &self,
        method: reqwest::Method,
        path: &str,
        body: &JsonValue,
    ) -> Result<JsonValue, tonic::Status> {
        match self {
            #[cfg(feature = "pinecone")]
            Self::Pinecone(client) => client.request_json(method, path, body).await,
            #[cfg(feature = "weaviate")]
            Self::Weaviate(client) => client.request_json(method, path, body).await,
            #[cfg(feature = "elasticsearch")]
            Self::Elasticsearch(client) => client.request_json(method, path, body).await,
        }
    }
}

pub struct VectorSystemCanonicalStore {
    client: VectorSystemClient,
    instance_name: String,
    resource_name: String,
    op_lock: tokio::sync::Mutex<()>,
}

impl VectorSystemCanonicalStore {
    #[cfg(feature = "pinecone")]
    pub(crate) fn new_pinecone(
        client: PineconeHttpClient,
        instance_name: impl Into<String>,
    ) -> Self {
        let instance_name = instance_name.into();
        Self {
            client: VectorSystemClient::Pinecone(client),
            resource_name: format!("udb_system_{}", sanitize_component(&instance_name)),
            instance_name,
            op_lock: tokio::sync::Mutex::new(()),
        }
    }

    #[cfg(feature = "weaviate")]
    pub(crate) fn new_weaviate(
        client: WeaviateHttpClient,
        instance_name: impl Into<String>,
    ) -> Self {
        let instance_name = instance_name.into();
        Self {
            client: VectorSystemClient::Weaviate(client),
            resource_name: format!("UdbSystem{}", pascal_component(&instance_name)),
            instance_name,
            op_lock: tokio::sync::Mutex::new(()),
        }
    }

    #[cfg(feature = "elasticsearch")]
    pub(crate) fn new_elasticsearch(
        client: ElasticsearchHttpClient,
        instance_name: impl Into<String>,
    ) -> Self {
        let instance_name = instance_name.into();
        Self {
            client: VectorSystemClient::Elasticsearch(client),
            resource_name: format!("udb-system-{}", sanitize_component(&instance_name)),
            instance_name,
            op_lock: tokio::sync::Mutex::new(()),
        }
    }

    fn backend_label_static(&self) -> &'static str {
        self.client.backend_label()
    }

    fn record_id(key: &str) -> String {
        let digest = Sha256::digest(key.as_bytes());
        let mut bytes = [0_u8; 16];
        bytes.copy_from_slice(&digest[..16]);
        bytes[6] = (bytes[6] & 0x0f) | 0x50;
        bytes[8] = (bytes[8] & 0x3f) | 0x80;
        Uuid::from_bytes(bytes).to_string()
    }

    fn kind_for_key(key: &str) -> &str {
        key.split(':').next().unwrap_or("record")
    }

    fn point_key(&self, suffix: &str) -> String {
        format!("{}:{suffix}", self.instance_name)
    }

    fn projection_task_key(&self, id: Uuid) -> String {
        self.point_key(&format!("projection_task:{id}"))
    }

    fn saga_key(&self, id: Uuid) -> String {
        self.point_key(&format!("saga:{id}"))
    }

    fn audit_key(&self, id: Uuid) -> String {
        self.point_key(&format!("admin_audit:{id}"))
    }

    fn migration_run_key(&self, id: Uuid) -> String {
        self.point_key(&format!("migration_run:{id}"))
    }

    fn migration_op_key(&self, id: i64) -> String {
        self.point_key(&format!("migration_op:{id}"))
    }

    async fn ensure_resource(&self) -> Result<(), String> {
        match &self.client {
            #[cfg(feature = "pinecone")]
            VectorSystemClient::Pinecone(client) => client.ping().await,
            #[cfg(feature = "weaviate")]
            VectorSystemClient::Weaviate(_) => {
                let path = format!("/v1/schema/{}", self.resource_name);
                match self
                    .client
                    .request_json(reqwest::Method::GET, &path, &JsonValue::Null)
                    .await
                {
                    Ok(_) => Ok(()),
                    Err(status) if status.code() == tonic::Code::NotFound => self
                        .client
                        .request_json(
                            reqwest::Method::POST,
                            "/v1/schema",
                            &json!({
                                "class": self.resource_name,
                                "vectorizer": "none",
                                "properties": [
                                    {"name": "record_key", "dataType": ["text"]},
                                    {"name": "record_kind", "dataType": ["text"]},
                                    {"name": "value_json", "dataType": ["text"]},
                                    {"name": "updated_at_ms", "dataType": ["int"]}
                                ]
                            }),
                        )
                        .await
                        .map(|_| ())
                        .map_err(|err| err.to_string()),
                    Err(status) => Err(status.to_string()),
                }
            }
            #[cfg(feature = "elasticsearch")]
            VectorSystemClient::Elasticsearch(_) => {
                let path = format!("/{}", self.resource_name);
                match self
                    .client
                    .request_json(reqwest::Method::GET, &path, &JsonValue::Null)
                    .await
                {
                    Ok(_) => Ok(()),
                    Err(status) if status.code() == tonic::Code::NotFound => self
                        .client
                        .request_json(
                            reqwest::Method::PUT,
                            &path,
                            &json!({
                                "mappings": {
                                    "dynamic": true,
                                    "properties": {
                                        "record_key": {"type": "keyword"},
                                        "record_kind": {"type": "keyword"},
                                        "updated_at_ms": {"type": "long"},
                                        "value": {"type": "object", "enabled": false}
                                    }
                                }
                            }),
                        )
                        .await
                        .map(|_| ())
                        .map_err(|err| err.to_string()),
                    Err(status) => Err(status.to_string()),
                }
            }
        }
    }

    async fn get_json<T>(&self, key: &str) -> SystemStoreResult<Option<T>>
    where
        T: for<'de> Deserialize<'de>,
    {
        let value = match &self.client {
            #[cfg(feature = "pinecone")]
            VectorSystemClient::Pinecone(_) => self.get_pinecone_value(key).await?,
            #[cfg(feature = "weaviate")]
            VectorSystemClient::Weaviate(_) => self.get_weaviate_value(key).await?,
            #[cfg(feature = "elasticsearch")]
            VectorSystemClient::Elasticsearch(_) => self.get_elasticsearch_value(key).await?,
        };
        value
            .map(|value| {
                serde_json::from_value(value).map_err(|err| {
                    SystemStoreError::InvalidInput(format!(
                        "decode {} system JSON {key}: {err}",
                        self.backend_label_static()
                    ))
                })
            })
            .transpose()
    }

    #[cfg(feature = "pinecone")]
    async fn get_pinecone_value(&self, key: &str) -> SystemStoreResult<Option<JsonValue>> {
        let path = format!(
            "/vectors/fetch?ids={}&namespace={}",
            Self::record_id(key),
            self.resource_name
        );
        let response = self
            .client
            .request_json(reqwest::Method::GET, &path, &JsonValue::Null)
            .await
            .map_err(|err| SystemStoreError::query("pinecone", "fetch vector", err))?;
        let Some(raw) = response
            .get("vectors")
            .and_then(|vectors| vectors.get(Self::record_id(key)))
            .and_then(|point| point.get("metadata"))
            .and_then(|metadata| metadata.get("value_json"))
            .and_then(JsonValue::as_str)
        else {
            return Ok(None);
        };
        serde_json::from_str(raw)
            .map(Some)
            .map_err(|err| SystemStoreError::InvalidInput(format!("decode pinecone JSON: {err}")))
    }

    #[cfg(feature = "weaviate")]
    async fn get_weaviate_value(&self, key: &str) -> SystemStoreResult<Option<JsonValue>> {
        let path = format!(
            "/v1/objects/{}/{}",
            self.resource_name,
            Self::record_id(key)
        );
        let response = match self
            .client
            .request_json(reqwest::Method::GET, &path, &JsonValue::Null)
            .await
        {
            Ok(response) => response,
            Err(status) if status.code() == tonic::Code::NotFound => return Ok(None),
            Err(status) => {
                return Err(SystemStoreError::query("weaviate", "get object", status));
            }
        };
        let Some(raw) = response
            .get("properties")
            .and_then(|properties| properties.get("value_json"))
            .and_then(JsonValue::as_str)
        else {
            return Ok(None);
        };
        serde_json::from_str(raw)
            .map(Some)
            .map_err(|err| SystemStoreError::InvalidInput(format!("decode weaviate JSON: {err}")))
    }

    #[cfg(feature = "elasticsearch")]
    async fn get_elasticsearch_value(&self, key: &str) -> SystemStoreResult<Option<JsonValue>> {
        let path = format!("/{}/_doc/{}", self.resource_name, Self::record_id(key));
        let response = match self
            .client
            .request_json(reqwest::Method::GET, &path, &JsonValue::Null)
            .await
        {
            Ok(response) => response,
            Err(status) if status.code() == tonic::Code::NotFound => return Ok(None),
            Err(status) => {
                return Err(SystemStoreError::query(
                    "elasticsearch",
                    "get document",
                    status,
                ));
            }
        };
        Ok(response
            .get("_source")
            .and_then(|source| source.get("value"))
            .cloned())
    }

    async fn set_json<T>(&self, key: &str, value: &T) -> SystemStoreResult<()>
    where
        T: Serialize,
    {
        let value = serde_json::to_value(value).map_err(|err| {
            SystemStoreError::InvalidInput(format!(
                "encode {} system JSON {key}: {err}",
                self.backend_label_static()
            ))
        })?;
        match &self.client {
            #[cfg(feature = "pinecone")]
            VectorSystemClient::Pinecone(_) => self.set_pinecone_value(key, value).await,
            #[cfg(feature = "weaviate")]
            VectorSystemClient::Weaviate(_) => self.set_weaviate_value(key, value).await,
            #[cfg(feature = "elasticsearch")]
            VectorSystemClient::Elasticsearch(_) => self.set_elasticsearch_value(key, value).await,
        }
    }

    #[cfg(feature = "pinecone")]
    async fn set_pinecone_value(&self, key: &str, value: JsonValue) -> SystemStoreResult<()> {
        let value_json = serde_json::to_string(&value).map_err(|err| {
            SystemStoreError::InvalidInput(format!("encode pinecone metadata JSON: {err}"))
        })?;
        self.client
            .request_json(
                reqwest::Method::POST,
                "/vectors/upsert",
                &json!({
                    "namespace": self.resource_name,
                    "vectors": [{
                        "id": Self::record_id(key),
                        "values": [0.0],
                        "metadata": {
                            "record_key": key,
                            "record_kind": Self::kind_for_key(key),
                            "value_json": value_json,
                            "updated_at_ms": Utc::now().timestamp_millis()
                        }
                    }]
                }),
            )
            .await
            .map(|_| ())
            .map_err(|err| SystemStoreError::query("pinecone", "upsert vector", err))
    }

    #[cfg(feature = "weaviate")]
    async fn set_weaviate_value(&self, key: &str, value: JsonValue) -> SystemStoreResult<()> {
        let value_json = serde_json::to_string(&value).map_err(|err| {
            SystemStoreError::InvalidInput(format!("encode weaviate property JSON: {err}"))
        })?;
        let path = format!(
            "/v1/objects/{}/{}",
            self.resource_name,
            Self::record_id(key)
        );
        self.client
            .request_json(
                reqwest::Method::PUT,
                &path,
                &json!({
                    "class": self.resource_name,
                    "id": Self::record_id(key),
                    "properties": {
                        "record_key": key,
                        "record_kind": Self::kind_for_key(key),
                        "value_json": value_json,
                        "updated_at_ms": Utc::now().timestamp_millis()
                    },
                    "vector": [0.0]
                }),
            )
            .await
            .map(|_| ())
            .map_err(|err| SystemStoreError::query("weaviate", "upsert object", err))
    }

    #[cfg(feature = "elasticsearch")]
    async fn set_elasticsearch_value(&self, key: &str, value: JsonValue) -> SystemStoreResult<()> {
        let path = format!(
            "/{}/_doc/{}?refresh=wait_for",
            self.resource_name,
            Self::record_id(key)
        );
        self.client
            .request_json(
                reqwest::Method::PUT,
                &path,
                &json!({
                    "record_key": key,
                    "record_kind": Self::kind_for_key(key),
                    "value": value,
                    "updated_at_ms": Utc::now().timestamp_millis()
                }),
            )
            .await
            .map(|_| ())
            .map_err(|err| SystemStoreError::query("elasticsearch", "index document", err))
    }

    async fn delete_key(&self, key: &str) -> SystemStoreResult<()> {
        match &self.client {
            #[cfg(feature = "pinecone")]
            VectorSystemClient::Pinecone(_) => self.delete_pinecone_key(key).await,
            #[cfg(feature = "weaviate")]
            VectorSystemClient::Weaviate(_) => self.delete_weaviate_key(key).await,
            #[cfg(feature = "elasticsearch")]
            VectorSystemClient::Elasticsearch(_) => self.delete_elasticsearch_key(key).await,
        }
    }

    #[cfg(feature = "pinecone")]
    async fn delete_pinecone_key(&self, key: &str) -> SystemStoreResult<()> {
        self.client
            .request_json(
                reqwest::Method::POST,
                "/vectors/delete",
                &json!({"namespace": self.resource_name, "ids": [Self::record_id(key)]}),
            )
            .await
            .map(|_| ())
            .map_err(|err| SystemStoreError::query("pinecone", "delete vector", err))
    }

    #[cfg(feature = "weaviate")]
    async fn delete_weaviate_key(&self, key: &str) -> SystemStoreResult<()> {
        let path = format!(
            "/v1/objects/{}/{}",
            self.resource_name,
            Self::record_id(key)
        );
        match self
            .client
            .request_json(reqwest::Method::DELETE, &path, &JsonValue::Null)
            .await
        {
            Ok(_) => Ok(()),
            Err(status) if status.code() == tonic::Code::NotFound => Ok(()),
            Err(status) => Err(SystemStoreError::query("weaviate", "delete object", status)),
        }
    }

    #[cfg(feature = "elasticsearch")]
    async fn delete_elasticsearch_key(&self, key: &str) -> SystemStoreResult<()> {
        let path = format!(
            "/{}/_doc/{}?refresh=wait_for",
            self.resource_name,
            Self::record_id(key)
        );
        match self
            .client
            .request_json(reqwest::Method::DELETE, &path, &JsonValue::Null)
            .await
        {
            Ok(_) => Ok(()),
            Err(status) if status.code() == tonic::Code::NotFound => Ok(()),
            Err(status) => Err(SystemStoreError::query(
                "elasticsearch",
                "delete document",
                status,
            )),
        }
    }

    async fn list_set(&self, set_key: &str) -> SystemStoreResult<Vec<String>> {
        Ok(self.get_json(set_key).await?.unwrap_or_default())
    }

    async fn add_to_set(&self, set_key: &str, item: String) -> SystemStoreResult<()> {
        let mut items = self.list_set(set_key).await?;
        if !items.iter().any(|existing| existing == &item) {
            items.push(item);
            self.set_json(set_key, &items).await?;
        }
        Ok(())
    }

    async fn add_to_capped_set(
        &self,
        set_key: &str,
        item: String,
        max_items: usize,
    ) -> SystemStoreResult<Vec<String>> {
        let mut items = self.list_set(set_key).await?;
        if items.iter().any(|existing| existing == &item) {
            return Ok(Vec::new());
        }
        items.push(item);
        let mut trimmed = Vec::new();
        if items.len() > max_items {
            let trim_count = items.len() - max_items;
            trimmed.extend(items.drain(0..trim_count));
            tracing::warn!(
                backend = self.backend_label_static(),
                set_key = %set_key,
                trimmed = trimmed.len(),
                max_items,
                "trimmed canonical KV membership set"
            );
        }
        self.set_json(set_key, &items).await?;
        Ok(trimmed)
    }

    async fn remove_from_set(&self, set_key: &str, item: &str) -> SystemStoreResult<bool> {
        let mut items = self.list_set(set_key).await?;
        let before = items.len();
        items.retain(|existing| existing != item);
        if items.len() != before {
            self.set_json(set_key, &items).await?;
            Ok(true)
        } else {
            Ok(false)
        }
    }

    async fn load_all<T>(&self, set_key: &str) -> SystemStoreResult<Vec<T>>
    where
        T: for<'de> Deserialize<'de>,
    {
        let keys = self.list_set(set_key).await?;
        let mut rows = Vec::with_capacity(keys.len());
        for key in keys {
            if let Some(row) = self.get_json(&key).await? {
                rows.push(row);
            }
        }
        Ok(rows)
    }

    async fn current_seq_value(&self) -> Result<i64, String> {
        self.get_json(&self.point_key("outbox_seq"))
            .await
            .map_err(|err| err.to_string())
            .map(|seq| seq.unwrap_or(0))
    }
}

fn sanitize_component(value: &str) -> String {
    let mut out = String::new();
    for ch in value.chars() {
        if ch.is_ascii_alphanumeric() {
            out.push(ch.to_ascii_lowercase());
        } else if ch == '_' || ch == '-' {
            out.push(ch);
        } else {
            out.push('_');
        }
    }
    out.trim_matches(['_', '-'])
        .chars()
        .collect::<String>()
        .if_empty("default")
}

fn pascal_component(value: &str) -> String {
    let mut out = String::new();
    let mut uppercase_next = true;
    for ch in value.chars() {
        if ch.is_ascii_alphanumeric() {
            if uppercase_next {
                out.push(ch.to_ascii_uppercase());
                uppercase_next = false;
            } else {
                out.push(ch);
            }
        } else {
            uppercase_next = true;
        }
    }
    if out.is_empty() {
        "Default".to_string()
    } else {
        out
    }
}

trait EmptyStringExt {
    fn if_empty(self, fallback: &str) -> String;
}

impl EmptyStringExt for String {
    fn if_empty(self, fallback: &str) -> String {
        if self.is_empty() {
            fallback.to_string()
        } else {
            self
        }
    }
}

#[async_trait]
impl CanonicalStore for VectorSystemCanonicalStore {
    fn backend_label(&self) -> &'static str {
        self.backend_label_static()
    }

    fn instance_name(&self) -> &str {
        &self.instance_name
    }

    async fn current_durability_token(&self) -> Result<DurabilityToken, String> {
        Ok(DurabilityToken::new(
            self.backend_label_static(),
            self.current_seq_value().await?.to_string(),
        ))
    }

    async fn wait_for_token(
        &self,
        token: &DurabilityToken,
        timeout: Duration,
    ) -> Result<bool, String> {
        if !token.is_for(self.backend_label_static()) {
            return Err(format!(
                "{} VectorSystemCanonicalStore cannot wait on a '{}' token",
                self.backend_label_static(),
                token.backend_label
            ));
        }
        let target: i64 = token.value.parse().map_err(|err| {
            format!(
                "invalid {} durability token '{}': {err}",
                self.backend_label_static(),
                token.value
            )
        })?;
        let started = Instant::now();
        let poll = super::durability_poll_interval(timeout, VECTOR_SYSTEM_DURABILITY_POLL_MS);
        loop {
            if self.current_seq_value().await? >= target {
                return Ok(true);
            }
            if started.elapsed() >= timeout {
                return Ok(false);
            }
            tokio::time::sleep(poll).await;
        }
    }

    async fn enqueue_outbox_event(
        &self,
        event_id: &str,
        topic: &str,
        partition_key: &str,
        payload: &serde_json::Value,
    ) -> Result<i64, String> {
        let _guard = self.op_lock.lock().await;
        let seq = self.current_seq_value().await?.saturating_add(1);
        self.set_json(&self.point_key("outbox_seq"), &seq)
            .await
            .map_err(|err| err.to_string())?;
        let event = super::OutboxEvent {
            event_seq: seq,
            event_id: event_id.to_string(),
            topic: topic.to_string(),
            partition_key: partition_key.to_string(),
            payload: payload.clone(),
            created_at_unix_ms: Utc::now().timestamp_millis(),
        };
        let key = self.point_key(&format!("outbox_event:{seq}"));
        self.set_json(&key, &event)
            .await
            .map_err(|err| err.to_string())?;
        let trimmed = self
            .add_to_capped_set(
                &self.point_key("outbox_events"),
                key,
                KV_MEMBERSHIP_MAX_ITEMS,
            )
            .await
            .map_err(|err| err.to_string())?;
        for old_key in trimmed {
            self.delete_key(&old_key)
                .await
                .map_err(|err| err.to_string())?;
        }
        Ok(seq)
    }

    async fn outbox_max_seq(&self) -> Result<i64, String> {
        self.current_seq_value().await
    }

    async fn ensure_system_tables(&self) -> Result<(), String> {
        self.ensure_resource().await
    }

    async fn ensure_advisory_lease_table(&self) -> Result<(), String> {
        self.ensure_system_tables().await
    }

    async fn try_acquire_advisory_lease(
        &self,
        lease_name: &str,
        owner_id: &str,
        ttl: Duration,
    ) -> Result<bool, String> {
        let _guard = self.op_lock.lock().await;
        let key = self.point_key(&format!("lease:{lease_name}"));
        let now = Utc::now().timestamp_millis();
        let current: Option<AdvisoryLeaseRow> =
            self.get_json(&key).await.map_err(|err| err.to_string())?;
        if !advisory_lease_can_acquire(current.as_ref(), owner_id, now) {
            return Ok(false);
        }
        self.set_json(&key, &new_advisory_lease_row(owner_id, now, ttl))
            .await
            .map_err(|err| err.to_string())?;
        Ok(true)
    }

    async fn release_advisory_lease(&self, lease_name: &str, owner_id: &str) -> Result<(), String> {
        let _guard = self.op_lock.lock().await;
        let key = self.point_key(&format!("lease:{lease_name}"));
        let current: Option<AdvisoryLeaseRow> =
            self.get_json(&key).await.map_err(|err| err.to_string())?;
        if advisory_lease_is_owned_by(current.as_ref(), owner_id) {
            self.delete_key(&key).await.map_err(|err| err.to_string())?;
        }
        Ok(())
    }
}

#[async_trait]
impl JsonProjectionTaskAdapter for VectorSystemCanonicalStore {
    fn projection_backend_label(&self) -> &'static str {
        self.backend_label_static()
    }

    fn projection_idem_key(&self, idempotency_key: &str) -> String {
        self.point_key(&format!("projection_idem:{idempotency_key}"))
    }

    fn projection_all_key(&self) -> String {
        self.point_key("projection_all")
    }

    fn projection_task_key(&self, task_id: Uuid) -> String {
        VectorSystemCanonicalStore::projection_task_key(self, task_id)
    }

    async fn get_projection_idem(&self, key: &str) -> SystemStoreResult<Option<String>> {
        self.get_json(key).await
    }

    async fn set_projection_idem(&self, key: &str, value: &String) -> SystemStoreResult<()> {
        self.set_json(key, value).await
    }

    async fn get_projection_row(&self, key: &str) -> SystemStoreResult<Option<ProjectionTaskRow>> {
        self.get_json(key).await
    }

    async fn set_projection_row(
        &self,
        key: &str,
        row: &ProjectionTaskRow,
    ) -> SystemStoreResult<()> {
        self.set_json(key, row).await
    }

    async fn load_projection_rows(
        &self,
        set_key: &str,
    ) -> SystemStoreResult<Vec<ProjectionTaskRow>> {
        self.load_all(set_key).await
    }

    async fn add_projection_row_key(
        &self,
        set_key: &str,
        row_key: String,
        max_items: usize,
    ) -> SystemStoreResult<Vec<String>> {
        self.add_to_capped_set(set_key, row_key, max_items).await
    }

    async fn remove_projection_row_key(
        &self,
        set_key: &str,
        row_key: &str,
    ) -> SystemStoreResult<bool> {
        self.remove_from_set(set_key, row_key).await
    }
}

/// FIX-79: raw typed KV primitives backing the shared saga / admin-audit /
/// migration-audit logic in `system_store.rs`.
#[async_trait]
impl JsonSystemRecordAdapter for VectorSystemCanonicalStore {
    fn record_backend_label(&self) -> &'static str {
        self.backend_label_static()
    }

    fn record_point_key(&self, suffix: &str) -> String {
        self.point_key(suffix)
    }

    fn saga_record_key(&self, saga_id: Uuid) -> String {
        self.saga_key(saga_id)
    }

    fn admin_audit_record_key(&self, audit_id: Uuid) -> String {
        self.audit_key(audit_id)
    }

    fn migration_run_record_key(&self, run_id: Uuid) -> String {
        self.migration_run_key(run_id)
    }

    async fn get_saga_row(&self, key: &str) -> SystemStoreResult<Option<SagaRow>> {
        self.get_json(key).await
    }

    async fn set_saga_row(&self, key: &str, row: &SagaRow) -> SystemStoreResult<()> {
        self.set_json(key, row).await
    }

    async fn load_saga_rows(&self, set_key: &str) -> SystemStoreResult<Vec<SagaRow>> {
        self.load_all(set_key).await
    }

    async fn get_admin_audit_row(&self, key: &str) -> SystemStoreResult<Option<AdminAuditRow>> {
        self.get_json(key).await
    }

    async fn set_admin_audit_row(&self, key: &str, row: &AdminAuditRow) -> SystemStoreResult<()> {
        self.set_json(key, row).await
    }

    async fn get_string_record(&self, key: &str) -> SystemStoreResult<Option<String>> {
        self.get_json(key).await
    }

    async fn set_string_record(&self, key: &str, value: &String) -> SystemStoreResult<()> {
        self.set_json(key, value).await
    }

    async fn get_migration_run_row(&self, key: &str) -> SystemStoreResult<Option<MigrationRunRow>> {
        self.get_json(key).await
    }

    async fn set_migration_run_row(
        &self,
        key: &str,
        row: &MigrationRunRow,
    ) -> SystemStoreResult<()> {
        self.set_json(key, row).await
    }

    async fn load_migration_run_rows(
        &self,
        set_key: &str,
    ) -> SystemStoreResult<Vec<MigrationRunRow>> {
        self.load_all(set_key).await
    }

    async fn load_migration_op_rows(
        &self,
        set_key: &str,
    ) -> SystemStoreResult<Vec<MigrationOpRow>> {
        self.load_all(set_key).await
    }

    async fn list_record_set(&self, set_key: &str) -> SystemStoreResult<Vec<String>> {
        self.list_set(set_key).await
    }

    async fn add_record_to_set(&self, set_key: &str, item: String) -> SystemStoreResult<()> {
        self.add_to_set(set_key, item).await
    }
}

#[async_trait]
impl ProjectionTaskStore for VectorSystemCanonicalStore {
    fn backend_label(&self) -> &'static str {
        self.backend_label_static()
    }

    async fn ensure_projection_tables(&self) -> SystemStoreResult<()> {
        self.ensure_system_tables()
            .await
            .map_err(|err| SystemStoreError::io(self.backend_label_static(), err))
    }

    async fn enqueue_projection_task(
        &self,
        task: &ProjectionTaskInsert,
    ) -> SystemStoreResult<Uuid> {
        let _guard = self.op_lock.lock().await;
        enqueue_json_projection_task(self, task, KV_MEMBERSHIP_MAX_ITEMS).await
    }

    async fn claim_projection_tasks(
        &self,
        filter: &ProjectionClaimFilter,
    ) -> SystemStoreResult<Vec<ProjectionTaskRow>> {
        let _guard = self.op_lock.lock().await;
        claim_json_projection_tasks(self, filter).await
    }

    async fn mark_projection_task_completed(&self, task_id: Uuid) -> SystemStoreResult<()> {
        let _guard = self.op_lock.lock().await;
        mark_json_projection_task_completed(self, task_id).await
    }

    async fn mark_projection_task_failed(
        &self,
        task_id: Uuid,
        new_retry_count: i32,
        new_status: ProjectionTaskStatus,
        error: &str,
    ) -> SystemStoreResult<()> {
        let _guard = self.op_lock.lock().await;
        mark_json_projection_task_failed(
            self,
            task_id,
            new_retry_count,
            new_status,
            error,
            ProjectionTaskFailurePolicy::LegacyAllowAnyStatusRemoveCompleted,
        )
        .await
    }

    async fn requeue_dead_letter_tasks(
        &self,
        target_backend: Option<&str>,
    ) -> SystemStoreResult<i64> {
        let _guard = self.op_lock.lock().await;
        requeue_json_dead_letter_tasks(self, target_backend).await
    }

    async fn reset_stale_in_progress_tasks(&self, stale_after: Duration) -> SystemStoreResult<i64> {
        let _guard = self.op_lock.lock().await;
        reset_stale_json_in_progress_tasks(self, stale_after).await
    }

    async fn projection_task_summary(&self) -> SystemStoreResult<ProjectionTaskSummary> {
        let rows: Vec<ProjectionTaskRow> = self.load_all(&self.point_key("projection_all")).await?;
        Ok(summarize_projection_tasks(rows))
    }

    async fn pending_task_metrics(&self, limit: i64) -> SystemStoreResult<Vec<PendingTaskMetric>> {
        let rows: Vec<ProjectionTaskRow> = self.load_all(&self.point_key("projection_all")).await?;
        Ok(pending_projection_task_metrics(rows, limit, Utc::now()))
    }

    async fn dead_letter_groups(&self, limit: i64) -> SystemStoreResult<Vec<DeadLetterGroup>> {
        let rows: Vec<ProjectionTaskRow> = self.load_all(&self.point_key("projection_all")).await?;
        Ok(projection_dead_letter_groups(rows, limit))
    }

    async fn requeue_dead_letter_by_source(
        &self,
        source_table: &str,
        target_backend: &str,
        target_instance: &str,
    ) -> SystemStoreResult<i64> {
        let _guard = self.op_lock.lock().await;
        requeue_json_dead_letter_by_source(self, source_table, target_backend, target_instance)
            .await
    }

    async fn pending_projection_task_count(
        &self,
        idempotency_keys: &[String],
    ) -> SystemStoreResult<i64> {
        pending_json_projection_task_count(self, idempotency_keys).await
    }
}

#[async_trait]
impl SagaStore for VectorSystemCanonicalStore {
    fn backend_label(&self) -> &'static str {
        self.backend_label_static()
    }

    async fn ensure_saga_tables(&self) -> SystemStoreResult<()> {
        self.ensure_system_tables()
            .await
            .map_err(|err| SystemStoreError::io(self.backend_label_static(), err))
    }

    async fn record_saga(&self, saga: &SagaInsert) -> SystemStoreResult<Uuid> {
        let _guard = self.op_lock.lock().await;
        record_json_saga(self, saga).await
    }

    async fn get_saga(&self, saga_id: Uuid) -> SystemStoreResult<Option<SagaRow>> {
        get_json_saga(self, saga_id).await
    }

    async fn list_sagas(&self, filter: &SagaListFilter) -> SystemStoreResult<Vec<SagaRow>> {
        list_json_sagas(self, filter).await
    }

    async fn update_saga_status(
        &self,
        saga_id: Uuid,
        status: SagaStatus,
        compensation_status: CompensationStatus,
    ) -> SystemStoreResult<()> {
        let _guard = self.op_lock.lock().await;
        update_json_saga_status(self, saga_id, status, compensation_status).await
    }

    async fn mark_saga_manual_review(&self, saga_id: Uuid) -> SystemStoreResult<()> {
        self.update_saga_status(
            saga_id,
            SagaStatus::ManualReview,
            CompensationStatus::ManualReview,
        )
        .await
    }

    async fn request_saga_recompensation(&self, saga_id: Uuid) -> SystemStoreResult<()> {
        let _guard = self.op_lock.lock().await;
        let key = self.saga_key(saga_id);
        let Some(mut row) = self.get_json::<SagaRow>(&key).await? else {
            return Ok(());
        };
        if !matches!(
            row.status,
            SagaStatus::FailedCompensation | SagaStatus::ManualReview
        ) {
            return Err(SystemStoreError::InvalidInput(format!(
                "saga {saga_id} is not eligible for recompensation"
            )));
        }
        row.compensation_status = CompensationStatus::RetryRequested;
        row.updated_at = Utc::now();
        self.set_json(&key, &row).await
    }

    async fn increment_recovery_attempts(
        &self,
        saga_id: Uuid,
        error: &str,
    ) -> SystemStoreResult<i64> {
        let _guard = self.op_lock.lock().await;
        increment_json_saga_recovery_attempts(self, saga_id, error).await
    }

    async fn claim_recoverable_sagas(
        &self,
        stale_after: Duration,
        limit: i64,
    ) -> SystemStoreResult<Vec<SagaRow>> {
        claim_json_recoverable_sagas(self, stale_after, limit).await
    }

    async fn mark_stale_in_progress_indeterminate(
        &self,
        stale_after: Duration,
    ) -> SystemStoreResult<i64> {
        let _guard = self.op_lock.lock().await;
        mark_json_stale_sagas_indeterminate(self, stale_after).await
    }

    async fn saga_summary(&self) -> SystemStoreResult<SagaSummary> {
        summarize_json_sagas(self).await
    }
}

#[async_trait]
impl super::system_store::AdminAuditStore for VectorSystemCanonicalStore {
    fn backend_label(&self) -> &'static str {
        self.backend_label_static()
    }

    async fn ensure_admin_audit_tables(&self) -> SystemStoreResult<()> {
        self.ensure_system_tables()
            .await
            .map_err(|err| SystemStoreError::io(self.backend_label_static(), err))
    }

    async fn latest_admin_audit_hash(&self) -> SystemStoreResult<String> {
        latest_json_admin_audit_hash(self).await
    }

    async fn append_admin_audit(&self, entry: &AdminAuditInsert) -> SystemStoreResult<Uuid> {
        append_json_admin_audit(self, entry).await
    }

    async fn list_admin_audit(
        &self,
        filter: &AdminAuditListFilter,
    ) -> SystemStoreResult<Vec<AdminAuditRow>> {
        list_json_admin_audit(self, filter).await
    }

    async fn verify_admin_audit_chain(
        &self,
        limit: Option<i64>,
    ) -> SystemStoreResult<AdminAuditChainReport> {
        verify_json_admin_audit_chain(self, limit).await
    }
}

#[async_trait]
impl MigrationAuditStore for VectorSystemCanonicalStore {
    fn backend_label(&self) -> &'static str {
        self.backend_label_static()
    }

    async fn ensure_migration_audit_tables(&self) -> SystemStoreResult<()> {
        self.ensure_system_tables()
            .await
            .map_err(|err| SystemStoreError::io(self.backend_label_static(), err))
    }

    async fn start_migration_run(&self, run: &MigrationRunInsert) -> SystemStoreResult<Uuid> {
        let _guard = self.op_lock.lock().await;
        start_json_migration_run(self, run).await
    }

    async fn record_migration_op(&self, op: &MigrationOpInsert) -> SystemStoreResult<i64> {
        let _guard = self.op_lock.lock().await;
        let seq_key = self.point_key("migration_op_seq");
        let id: i64 = self.get_json(&seq_key).await?.unwrap_or(0_i64) + 1;
        self.set_json(&seq_key, &id).await?;
        let row = MigrationOpRow {
            id,
            run_id: op.run_id,
            operation_index: op.operation_index,
            backend: op.backend.clone(),
            resource_uri: op.resource_uri.clone(),
            operation_kind: op.operation_kind.clone(),
            status: op.status,
            payload_json: op.payload_json.clone(),
            error: op.error.clone(),
            applied_at: Some(Utc::now()),
        };
        let key = self.migration_op_key(id);
        self.set_json(&key, &row).await?;
        self.add_to_set(
            &self.point_key(&format!("migration_ops:{}", op.run_id)),
            key.clone(),
        )
        .await?;
        self.add_to_set(&self.point_key("migration_ops_all"), key)
            .await?;
        Ok(id)
    }

    async fn finish_migration_run(
        &self,
        run_id: Uuid,
        new_state: MigrationRunState,
        error: &str,
    ) -> SystemStoreResult<()> {
        let _guard = self.op_lock.lock().await;
        let key = self.migration_run_key(run_id);
        let Some(mut row) = self.get_json::<MigrationRunRow>(&key).await? else {
            return Ok(());
        };
        row.state = new_state;
        row.error = error.to_string();
        if new_state.is_terminal() {
            row.finished_at = Some(Utc::now());
        }
        self.set_json(&key, &row).await
    }

    async fn get_migration_run(&self, run_id: Uuid) -> SystemStoreResult<Option<MigrationRunRow>> {
        get_json_migration_run(self, run_id).await
    }

    async fn list_migration_ops(&self, run_id: Uuid) -> SystemStoreResult<Vec<MigrationOpRow>> {
        list_json_migration_ops(self, run_id).await
    }

    async fn list_migration_runs(
        &self,
        filter: &MigrationRunsFilter,
    ) -> SystemStoreResult<Vec<MigrationRunRow>> {
        list_json_migration_runs(self, filter).await
    }
}