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
use serde::{Deserialize, Serialize};
use uuid::Uuid;
use crate::ids::OwnerKind;
use crate::ids::TenantId;
use crate::model::{Cardinality, Nullability, TypeState};
use crate::occurrence::{EmbeddingBinding, EmbeddingSpaceInfo, OccurrenceRow};
/// Current deterministic command format. Only this version is accepted by
/// the state-machine expander; it is independent of the platform wire protocol.
/// Reset to 1 for the first release (no earlier format was ever shipped);
/// bump it whenever a command type changes.
pub const COMMAND_VERSION: u32 = 1;
/// Deterministic mutation set applied atomically by the replicated state
/// machine. Contains allocated IDs and encoded values; apply never reads a
/// clock, RNG, network or parser (plan documents 05/07).
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
pub struct CommitBatch {
/// Command format version used to interpret the batch.
pub command_version: u32,
/// Tenant whose keyspace the batch mutates.
pub tenant: TenantId,
/// Client-visible transaction identifier.
pub transaction_id: String,
/// Caller-chosen idempotency key; replays discover the stored outcome.
pub commit_key: Uuid,
/// Commit index the batch is expected to be applied at.
pub start_index: u64,
/// Isolation level under which the batch was planned.
pub isolation: IsolationMode,
/// Conditions that must hold before any mutation is applied.
pub preconditions: Vec<Precondition>,
/// Ordered mutations applied atomically.
pub mutations: Vec<Mutation>,
}
/// Isolation level used when planning a batch.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
pub enum IsolationMode {
/// Observe reads committed before the batch was planned.
ReadCommitted,
/// Read a consistent snapshot taken at planning time.
Snapshot,
/// Fully serializable execution.
Serializable,
}
/// A single key condition guarding a batch.
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
pub struct Precondition {
/// Key the condition inspects.
pub key: Vec<u8>,
/// Condition evaluated against the key.
pub kind: PreconditionKind,
}
/// The two supported precondition checks.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
pub enum PreconditionKind {
/// The key must exist.
Exists,
/// The key must be absent.
Absent,
}
impl Precondition {
/// Require `key` to exist.
#[must_use]
pub fn exists(key: Vec<u8>) -> Self {
Precondition {
key,
kind: PreconditionKind::Exists,
}
}
/// Require `key` to be absent.
#[must_use]
pub fn absent(key: Vec<u8>) -> Self {
Precondition {
key,
kind: PreconditionKind::Absent,
}
}
}
/// A single deterministic mutation applied by the state machine.
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
pub enum Mutation {
/// Publish a tenant policy, using the applying CI as its generation.
SetSecurityPolicy {
/// Policy payload to publish.
policy: crate::security::DataAccessPolicy,
/// Enforcement strength for the policy.
mode: crate::security::AuthorizationMode,
},
/// Create a record header with the given id, ordinal and label.
CreateRecordHeader {
/// Public id of the new record.
record_id: [u8; 16],
/// Dense ordinal assigned to the record.
ordinal: u64,
/// Primary label of the record.
label: String,
},
/// Delete a record header by id, ordinal and label.
DeleteRecordHeader {
/// Dense ordinal of the record.
ordinal: u64,
/// Public id of the record.
record_id: [u8; 16],
/// Primary label of the record.
label: String,
},
/// Move a record between its single primary-label membership sets while
/// preserving its stable public id, ordinal, fields, and relationships.
RetypeRecordHeader {
/// Public id of the record.
record_id: [u8; 16],
/// Dense ordinal of the record.
ordinal: u64,
/// Label the record is leaving.
from_label: String,
/// Label the record is moving to.
to_label: String,
},
/// Create a relationship header between two records.
CreateRelationshipHeader {
/// Public id of the new relationship.
relationship_id: [u8; 16],
/// Dense ordinal assigned to the relationship.
ordinal: u64,
/// Relationship type.
rel_type: String,
/// Ordinal of the source record.
source_ord: u64,
/// Ordinal of the target record.
target_ord: u64,
/// Public id of the source record.
source_id: [u8; 16],
/// Public id of the target record.
target_id: [u8; 16],
},
/// Delete a relationship header by id, ordinal and type.
DeleteRelationshipHeader {
/// Public id of the relationship.
relationship_id: [u8; 16],
/// Dense ordinal of the relationship.
ordinal: u64,
/// Relationship type.
rel_type: String,
/// Ordinal of the source record.
source_ord: u64,
/// Ordinal of the target record.
target_ord: u64,
},
/// Create a property family if it does not already exist.
EnsureFamily {
/// Dense id of the family.
family_id: u64,
/// Namespace of the property.
namespace: String,
/// Whether the family is owned by records or relationships.
owner_scope: OwnerKind,
/// Normalized name used for lookup.
normalized_name: String,
/// Human-readable display name.
display_name: String,
},
/// Create a property definition within a family.
EnsureDefinition {
/// Dense id of the property.
property_id: u64,
/// Family the property belongs to.
family_id: u64,
/// Current type resolution state.
type_state: TypeState,
/// Allowed value cardinality.
cardinality: Cardinality,
/// Nullability constraint.
nullability: Nullability,
},
/// Atomically remove a property family: every occurrence of the family
/// (records and relationships), the family row/signature, and all property
/// definitions. Used by the protocol `deleteProperty` operation.
DeleteFamily {
/// Dense id of the family.
family_id: u64,
/// Namespace of the property.
namespace: String,
/// Whether the family is owned by records or relationships.
owner_scope: OwnerKind,
/// Normalized name used for lookup.
normalized_name: String,
/// Human-readable display name.
display_name: String,
},
/// Insert or update one property occurrence.
PutOccurrence {
/// Occurrence row to persist.
row: OccurrenceRow,
/// Canonical encoded payload, or `None` for payload-free states.
canonical_value_bytes: Option<Vec<u8>>,
},
/// Delete one property occurrence.
DeleteOccurrence {
/// Occurrence row to remove.
row: OccurrenceRow,
},
/// Create or update an embedding space.
EnsureEmbeddingSpace {
/// Alias the space is reachable under.
alias: String,
/// Space definition to ensure.
info: EmbeddingSpaceInfo,
/// Whether this creates a new space.
new: bool,
},
/// Insert or update an embedding binding.
UpsertEmbeddingBinding {
/// Binding to persist.
binding: EmbeddingBinding,
/// Vector payload to store, if any.
vector: Option<Vec<f32>>,
/// When set, statuses are recomputed against this expected live hash.
expect_live_hash: bool,
},
/// v3: resolve and remove canonical binary vector keys at apply time.
/// The alias must still identify this space; counters and the immutable
/// space definition remain so a recreated alias receives a fresh space ID.
ClearEmbeddingSpace {
/// Alias of the space to clear.
alias: String,
/// Dense id of the space.
space_id: u64,
},
/// Replicated meta bookkeeping (projection watermarks and similar);
/// value semantics are opaque to the state machine.
SetMetaValue {
/// Logical meta key.
key: String,
/// New integer value.
value: u64,
},
/// Replicated opaque blob (policies, configuration); replayed verbatim
/// by the deterministic state machine like any other mutation.
SetMetaBlob {
/// Logical meta key.
key: String,
/// New blob value.
value: Vec<u8>,
},
/// Deterministic deletion of a replicated meta blob (index registry rows).
DeleteMetaBlob {
/// Logical meta key.
key: String,
},
/// Delete one tenant-scoped raw key during an explicit maintenance repair.
/// The maintenance caller must validate the key belongs to its tenant.
DeleteRawKey {
/// Canonical tenant-scoped key to remove.
key: Vec<u8>,
},
/// Put one tenant-scoped raw key during an explicit maintenance fixture
/// or rebuild operation.
PutRawKey {
/// Canonical tenant-scoped key to write.
key: Vec<u8>,
/// Value bytes to store.
value: Vec<u8>,
},
/// One bounded, resumable purge step: delete up to `max_keys` of the
/// smallest remaining purgeable keys (tenant keyspace excluding the
/// security subrange, then vector/ANN metadata), without retaining MVCC
/// history for erased keys. The driver loops commits until nothing
/// remains; expansion is a pure function of (state, batch), so replay
/// reproduces the same sequence.
PurgeTenantChunk {
/// Maximum number of keys this step may delete.
max_keys: u64,
},
/// Register a tenant scope in the directory: assign the next
/// ordinal unless the scope already has an active one. Expansion reads
/// and bumps the directory sequence, so replay reproduces the ordinal.
RegisterTenant {
/// SHA-256 digest of the scope value (the scope string is never
/// stored).
scope: crate::ids::TenantScope,
},
/// Retire the batch tenant's ordinal after its keyspace is purged: both
/// directory rows move to `Purged`, and the ordinal is never reassigned.
RetireTenant,
/// Reclaim embedding vector slots whose superseding binding version is at
/// or below `through_ci` (the GC watermark). A re-binding allocates a fresh
/// slot and records the old one in the reclamation index; the old slot's
/// bytes stay readable by pinned views until the history floor passes it.
/// Expansion deletes a bounded batch of the oldest reclaimable slots plus
/// their reclamation rows; the driver loops commits until none remain.
ReclaimEmbeddingSlots {
/// Highest superseding commit index safe to reclaim (inclusive).
through_ci: u64,
},
/// Declare an index: allocate the next id and store the
/// definition as `Building { cursor: 0 }`. From this commit on, the
/// deriver maintains entries of covered records.
DefineIndex {
/// Tenant-unique name.
name: String,
/// Record label.
label: String,
/// Property name (normalized at expansion).
property: String,
/// Index kind.
kind: crate::index::IndexKind,
},
/// Advance a building index's cursor to `through` (the deriver backfills
/// the records it newly covers from their current values), or mark it
/// `Ready` when `through` is `None`.
IndexBackfill {
/// Index id.
id: u32,
/// New cursor, or `None` for `Ready`.
through: Option<u64>,
},
/// Start dropping an index by name: `Dropping { cursor: 0 }`.
DropIndex {
/// Index name.
name: String,
},
/// Advance a dropping index's cursor to `through` (the deriver removes
/// the entries it no longer covers), or remove the definition when
/// `through` is `None`.
IndexDrop {
/// Index id.
id: u32,
/// New cursor, or `None` to remove the definition.
through: Option<u64>,
},
/// An index applier (text or structural) processed `owners` of index
/// `index` as of commit `upto`: remove each owner's pending row
/// unless a later commit re-marked it.
IndexApplied {
/// Index id.
index: u32,
/// Snapshot the applier read.
upto: u64,
/// Owners whose current text was applied.
owners: Vec<u64>,
},
/// [`Mutation::SetSecurityPolicy`] with a small-count threshold.
/// Appended last so earlier logs keep their variant indexes; written only
/// when the threshold is set, so a log without one stays readable by
/// binaries that predate it.
SetSecurityPolicyThreshold {
/// Policy payload to publish.
policy: crate::security::DataAccessPolicy,
/// Enforcement strength for the policy.
mode: crate::security::AuthorizationMode,
/// Smallest group a catalog read may report.
min_count: u64,
},
}