khive-runtime 0.24.0

Composable Service API: entity/note CRUD, graph traversal, hybrid search, curation.
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
//! Caller keys on entities (ADR-221 section 1).
//!
//! A key names one live entity per `(namespace, kind, key)`. A keyed create
//! writes when the key is free, answers the holder when the payload is the
//! same, and refuses with `key_conflict` when it differs. Deleting an entity
//! releases its keys; restoring it takes them all back or refuses.

use std::collections::BTreeSet;

use super::entity_create::EntityCreateWrite;
use super::*;
use khive_storage::EntityKeyHolder;

/// The refusal text for a key over 512 UTF-8 bytes or holding U+0000.
pub(crate) const ENTITY_KEY_REFUSAL: &str =
    "key must be at most 512 UTF-8 bytes and must not contain U+0000";

/// Claims lost to a concurrent writer before the create gives up. Each lost
/// claim re-reads the holder, so a second loss needs the key to be released
/// and taken again between two reads.
const KEYED_CREATE_ATTEMPTS: usize = 3;

/// Outcome of [`KhiveRuntime::create_keyed_entity_with_post_commit_report`].
#[derive(Debug)]
pub enum KeyedEntityCreate {
    /// No live entity held the key: this call wrote `entity` and claimed the
    /// key for it in the same transaction.
    Created {
        entity: Entity,
        embedding: crate::retrieval::EmbeddingTruncationReport,
        degradations: Vec<PostCommitDegradation>,
    },
    /// The live holder of the key carries the same payload. Nothing was
    /// written.
    Replayed { entity: Entity },
    /// A live entity with a different payload holds the key. Nothing was
    /// written; the holder is unchanged.
    Conflict { existing_id: Uuid },
}

/// Outcome of [`KhiveRuntime::reload_keyed_entity`].
#[derive(Debug)]
pub enum KeyedEntityReload {
    /// The holder now carries the request's payload.
    Updated {
        entity: Entity,
        report: crate::curation::EntityUpdateReport,
    },
    /// The holder already carried the request's payload. Nothing was written.
    Replayed { entity: Entity },
    /// The key no longer names the entity the caller was cleared to update:
    /// it was released, or another entity holds it now. Nothing was written.
    Moved,
}

/// A fresh id for the row a keyed bulk create will write (ADR-221 section 1).
///
/// The bulk create names the id before it builds the row, so the update gate
/// can judge a later item of the same request against that row. Only
/// [`PendingEntityId::mint`] makes one and building the plan spends it, so one
/// id names one new row.
#[derive(Debug)]
pub struct PendingEntityId(pub(super) Uuid);

impl PendingEntityId {
    /// A new random id.
    pub fn mint() -> Self {
        Self(Uuid::new_v4())
    }

    /// The id the row will carry.
    pub fn id(&self) -> Uuid {
        self.0
    }
}

/// A plan that writes nothing and holds only while `holder` is still the
/// live row it was read as: same id, `updated_at`, `deleted_at` and version.
///
/// A keyed bulk create that answers an item as an identical replay of a
/// stored holder joins this to its unit (ADR-221 section 1), so the replay
/// is judged inside the unit's transaction: a holder that changed after it
/// was read rolls the unit back and the request is judged again.
pub fn keyed_replay_assertion_plan(holder: &Entity) -> AtomicOpPlan {
    crate::canonical_apply::unchanged_entity_assertion_plan(holder, "keyed-replay-assertion")
}

/// Whether `holder` carries the payload `spec` asks for, as a keyed create
/// compares them.
pub fn entity_carries_create_payload(holder: &Entity, spec: &EntityCreateSpec) -> bool {
    same_entity_payload(
        holder,
        spec.entity_type.as_deref(),
        &spec.name,
        spec.description.as_deref(),
        spec.properties.as_ref(),
        &spec.tags,
        &spec_aliases(spec),
    )
}

/// The caller aliases a row written from `spec` holds: the requested ones
/// other than its name.
fn spec_aliases(spec: &EntityCreateSpec) -> BTreeSet<&str> {
    spec.aliases
        .iter()
        .map(String::as_str)
        .filter(|alias| *alias != spec.name)
        .collect()
}

/// Whether two keyed create payloads are the same, as
/// [`entity_carries_create_payload`] compares a holder with a request.
pub fn same_create_payload(first: &EntityCreateSpec, second: &EntityCreateSpec) -> bool {
    let mut held =
        Entity::new("", &first.kind, &first.name).with_entity_type(first.entity_type.as_deref());
    held.description = first.description.clone();
    held.properties = first.properties.clone();
    held.tags = first.tags.clone();
    held.aliases = spec_aliases(first)
        .into_iter()
        .map(|alias| khive_storage::entity::EntityAlias {
            alias: alias.to_owned(),
            origin: khive_storage::entity::EntityAliasOrigin::Caller,
        })
        .collect();
    entity_carries_create_payload(&held, second)
}

/// The caller property keys `holder` has and `wanted` leaves out: what a
/// reload removes so the holder's caller properties become `wanted`.
fn properties_to_remove(holder: &Entity, wanted: Option<&serde_json::Value>) -> Vec<String> {
    match caller_properties(holder.properties.as_ref()) {
        Some(serde_json::Value::Object(held)) => held
            .keys()
            .filter(|held_key| {
                !wanted
                    .and_then(serde_json::Value::as_object)
                    .is_some_and(|asked| asked.contains_key(*held_key))
            })
            .cloned()
            .collect(),
        _ => Vec::new(),
    }
}

/// The patch a reload writes: every caller field becomes the request's.
fn reload_patch(
    entity_type: Option<&str>,
    name: &str,
    description: Option<&str>,
    wanted: Option<serde_json::Value>,
    tags: &[String],
) -> crate::EntityPatch {
    crate::EntityPatch {
        name: Some(name.to_owned()),
        description: Some(description.map(str::to_owned)),
        properties: wanted,
        tags: Some(tags.to_vec()),
        entity_type: Some(entity_type.map(str::to_owned)),
    }
}

fn is_version_conflict(error: &RuntimeError) -> bool {
    matches!(
        error,
        RuntimeError::Khive(error)
            if error.details().and_then(|details| details.get("reason"))
                == Some("version_conflict")
    )
}

/// Validate a caller key and scan its exact bytes with the secret gate.
///
/// Runs before any holder lookup, so a refused key never reaches a read.
pub(crate) fn validate_entity_key(key: &str) -> RuntimeResult<()> {
    crate::note_validation::validate_key(key, ENTITY_KEY_REFUSAL)?;
    crate::secret_gate::check_at(key, "entity", "key")
}

/// The caller-visible part of a properties value: the runtime-owned
/// top-level keys removed, and `None` for an object left empty.
fn caller_properties(properties: Option<&serde_json::Value>) -> Option<serde_json::Value> {
    let value = properties?;
    let serde_json::Value::Object(map) = value else {
        return Some(value.clone());
    };
    let reserved = crate::secret_gate::RESERVED_PROPERTY_KEYS;
    let kept: serde_json::Map<String, serde_json::Value> = map
        .iter()
        .filter(|(key, _)| !reserved.contains(&key.as_str()))
        .map(|(key, value)| (key.clone(), value.clone()))
        .collect();
    (!kept.is_empty()).then_some(serde_json::Value::Object(kept))
}

/// Whether `holder` carries the payload a keyed create asked for: name,
/// description, `entity_type`, typed-equal properties, tags as a set and
/// caller-origin aliases as a set (ADR-221 section 1). `aliases` is the
/// set the request asks for, its name already left out. A requested alias
/// the holder holds under a rename or merge origin is left out of it too:
/// the write keeps that row as it is, so the request would not store it.
///
/// Both sides' properties are read through [`caller_properties`]: the
/// runtime-owned top-level keys are left out, because no caller can send
/// them, and absent properties and `{}` compare equal on either side, so a
/// holder created with `{}` and later stamped replays a request with `{}` or
/// with none.
fn same_entity_payload(
    holder: &Entity,
    entity_type: Option<&str>,
    name: &str,
    description: Option<&str>,
    properties: Option<&serde_json::Value>,
    tags: &[String],
    aliases: &BTreeSet<&str>,
) -> bool {
    let stored: BTreeSet<&str> = aliases
        .iter()
        .copied()
        .filter(|alias| {
            !holder.aliases.iter().any(|held| {
                held.alias == *alias
                    && held.origin != khive_storage::entity::EntityAliasOrigin::Caller
            })
        })
        .collect();
    if super::entity_aliases::caller_alias_set(holder) != stored {
        return false;
    }
    if holder.name != name
        || holder.description.as_deref() != description
        || holder.entity_type.as_deref() != entity_type
    {
        return false;
    }
    if caller_properties(holder.properties.as_ref()) != caller_properties(properties) {
        return false;
    }
    let held: BTreeSet<&str> = holder.tags.iter().map(String::as_str).collect();
    let asked: BTreeSet<&str> = tags.iter().map(String::as_str).collect();
    held == asked
}

/// `key_conflict` for a keyed create whose key a different payload holds.
///
/// `existing_id` is named only when the caller may list the holder; the
/// caller decides that and passes `None` otherwise.
pub fn entity_key_conflict(key: &str, existing_id: Option<Uuid>) -> RuntimeError {
    let mut details = vec![
        ("reason", "key_conflict".to_string()),
        ("key", key.to_owned()),
    ];
    if let Some(existing_id) = existing_id {
        details.push(("existing_id", existing_id.to_string()));
    }
    KhiveError::conflict(format!(
        "key_conflict: key {key:?} is already held by a live entity with a different payload"
    ))
    .with_details(khive_types::Details::new_owned(details))
    .into()
}

/// `restore_key_conflict` naming every key another live entity took while
/// entity `id` was deleted. The entity stays deleted.
///
/// A holder's id is named, in the message and in `taken_keys`, only for a
/// kind `disclose` admits, as `key_conflict` names `existing_id` only where
/// the caller may list the holder. The kind and key are always named.
fn restore_entity_key_conflict(
    id: Uuid,
    taken: &[EntityKeyHolder],
    disclose: &(dyn Fn(&str) -> bool + Sync),
) -> RuntimeError {
    let named: Vec<String> = taken
        .iter()
        .map(|taken| {
            if disclose(&taken.kind) {
                format!("{}:{:?} held by {}", taken.kind, taken.key, taken.holder_id)
            } else {
                format!("{}:{:?}", taken.kind, taken.key)
            }
        })
        .collect();
    let listed: Vec<serde_json::Value> = taken
        .iter()
        .map(|taken| {
            let mut entry = serde_json::json!({"kind": taken.kind, "key": taken.key});
            if disclose(&taken.kind) {
                entry["existing_id"] = serde_json::json!(taken.holder_id.to_string());
            }
            entry
        })
        .collect();
    KhiveError::conflict(format!(
        "restore_key_conflict: entity {id} stays deleted; keys taken by live entities: {}",
        named.join(", ")
    ))
    .with_details(khive_types::Details::new_owned([
        ("reason", "restore_key_conflict".to_string()),
        ("taken_keys", serde_json::Value::Array(listed).to_string()),
    ]))
    .into()
}

impl KhiveRuntime {
    /// Create an entity under caller key `key`, unique per namespace and
    /// base kind (ADR-221 section 1).
    ///
    /// The key is validated and secret-gated before any read. A free key is
    /// claimed in the transaction that writes the entity. A held key answers
    /// [`KeyedEntityCreate::Replayed`] when the holder carries the same
    /// payload and [`KeyedEntityCreate::Conflict`] when it does not; neither
    /// writes anything. `aliases` is part of the payload, compared as the set
    /// of the holder's caller-origin aliases.
    #[allow(clippy::too_many_arguments)]
    pub async fn create_keyed_entity_with_post_commit_report(
        &self,
        token: &NamespaceToken,
        kind: &str,
        key: &str,
        entity_type: Option<&str>,
        name: &str,
        description: Option<&str>,
        properties: Option<serde_json::Value>,
        tags: Vec<String>,
        aliases: &EntityAliasChange,
    ) -> RuntimeResult<KeyedEntityCreate> {
        validate_entity_key(key)?;
        self.validate_entity_kind(kind)?;
        crate::secret_gate::reject_reserved_secret_gate_property(properties.as_ref())?;
        let namespace = token.namespace().as_str();
        let wanted_aliases = aliases.wanted(name);
        for _ in 0..KEYED_CREATE_ATTEMPTS {
            let holder = self
                .entities(token)?
                .get_entity_by_key(namespace, kind, key)
                .await?;
            if let Some(holder) = holder {
                return Ok(
                    if same_entity_payload(
                        &holder,
                        entity_type,
                        name,
                        description,
                        properties.as_ref(),
                        &tags,
                        &wanted_aliases,
                    ) {
                        KeyedEntityCreate::Replayed { entity: holder }
                    } else {
                        KeyedEntityCreate::Conflict {
                            existing_id: holder.id,
                        }
                    },
                );
            }
            match self
                .create_entity_with_embedding_report_inner(
                    token,
                    kind,
                    entity_type,
                    name,
                    description,
                    properties.clone(),
                    tags.clone(),
                    Vec::new(),
                    Some(key),
                    aliases,
                )
                .await?
            {
                EntityCreateWrite::Written(entity, embedding, degradations) => {
                    return Ok(KeyedEntityCreate::Created {
                        entity: *entity,
                        embedding,
                        degradations,
                    });
                }
                EntityCreateWrite::KeyHeld => continue,
            }
        }
        // Every claim lost to a concurrent writer: answer from the winner.
        let holder = self
            .entities(token)?
            .get_entity_by_key(namespace, kind, key)
            .await?;
        match holder {
            Some(holder)
                if same_entity_payload(
                    &holder,
                    entity_type,
                    name,
                    description,
                    properties.as_ref(),
                    &tags,
                    &wanted_aliases,
                ) =>
            {
                Ok(KeyedEntityCreate::Replayed { entity: holder })
            }
            Some(holder) => Ok(KeyedEntityCreate::Conflict {
                existing_id: holder.id,
            }),
            None => Err(RuntimeError::Internal(format!(
                "keyed entity create: key {key:?} was claimed and released again during \
                 {KEYED_CREATE_ATTEMPTS} claims"
            ))),
        }
    }

    /// A keyed create under `on_key: "update"` whose key `holder_id` holds
    /// with a different payload (ADR-221 section 1).
    ///
    /// The holder's name, description, `entity_type`, tags and caller
    /// properties become the request's: a field the request leaves out is
    /// cleared, as the payload comparison reads it. The write is the
    /// ordinary entity update, bound to the version this call compared, so a
    /// holder that changed in between is read and compared again, and a
    /// holder carrying a runtime-owned property key is refused as that
    /// update refuses it: the key vouches for bytes this write would change.
    /// The caller has already been cleared to update `holder_id`; if the key
    /// no longer names it, nothing is written and the answer is
    /// [`KeyedEntityReload::Moved`]. The holder's caller-origin aliases
    /// become `aliases`; aliases a rename or a merge wrote stay.
    #[allow(clippy::too_many_arguments)]
    pub async fn reload_keyed_entity(
        &self,
        token: &NamespaceToken,
        kind: &str,
        key: &str,
        holder_id: Uuid,
        entity_type: Option<&str>,
        name: &str,
        description: Option<&str>,
        properties: Option<serde_json::Value>,
        tags: Vec<String>,
        aliases: &EntityAliasChange,
    ) -> RuntimeResult<KeyedEntityReload> {
        validate_entity_key(key)?;
        crate::secret_gate::reject_reserved_secret_gate_property(properties.as_ref())?;
        let namespace = token.namespace().as_str();
        let wanted = caller_properties(properties.as_ref());
        let wanted_aliases = aliases.wanted(name);
        for _ in 0..KEYED_CREATE_ATTEMPTS {
            let holder = self
                .entities(token)?
                .get_entity_by_key(namespace, kind, key)
                .await?;
            let Some(holder) = holder.filter(|holder| holder.id == holder_id) else {
                return Ok(KeyedEntityReload::Moved);
            };
            if same_entity_payload(
                &holder,
                entity_type,
                name,
                description,
                wanted.as_ref(),
                &tags,
                &wanted_aliases,
            ) {
                return Ok(KeyedEntityReload::Replayed { entity: holder });
            }
            let removed = properties_to_remove(&holder, wanted.as_ref());
            let removed: Vec<&str> = removed.iter().map(String::as_str).collect();
            let patch = reload_patch(entity_type, name, description, wanted.clone(), &tags);
            match self
                .update_entity_with_removals_and_index_report(
                    token,
                    holder.id,
                    patch,
                    &removed,
                    Some(holder.version),
                    aliases,
                )
                .await
            {
                Ok((entity, report)) => {
                    return Ok(KeyedEntityReload::Updated { entity, report });
                }
                Err(error) if is_version_conflict(&error) => continue,
                Err(error) => return Err(error),
            }
        }
        Err(RuntimeError::Internal(format!(
            "keyed entity reload: the holder of key {key:?} changed during \
             {KEYED_CREATE_ATTEMPTS} attempts"
        )))
    }

    /// The update plan for a bulk item under `on_key: "update"` whose key
    /// `holder` holds with a different payload (ADR-221 section 1).
    ///
    /// The holder's caller fields become `spec`'s, as
    /// [`Self::reload_keyed_entity`] writes them, and the plan is bound to the
    /// version `holder` carries, so a holder that changed after the caller
    /// read it rolls the unit back. A holder carrying a runtime-owned
    /// property key is refused as the ordinary update refuses it. The
    /// returned record is the projection; its version is the one read.
    pub async fn prepare_keyed_entity_reload_plan(
        &self,
        token: &NamespaceToken,
        holder: &Entity,
        spec: &EntityCreateSpec,
    ) -> RuntimeResult<(Entity, AtomicOpPlan)> {
        crate::secret_gate::reject_reserved_secret_gate_property(spec.properties.as_ref())?;
        let aliases = EntityAliasChange::replace(&spec.aliases)?;
        let wanted = caller_properties(spec.properties.as_ref());
        let removed = properties_to_remove(holder, wanted.as_ref());
        let removed: Vec<&str> = removed.iter().map(String::as_str).collect();
        let patch = reload_patch(
            spec.entity_type.as_deref(),
            &spec.name,
            spec.description.as_deref(),
            wanted,
            &spec.tags,
        );
        crate::atomic_prepare::prepare_keyed_reload_entity_record(
            self,
            token,
            holder.id,
            patch,
            &removed,
            holder.version,
            &aliases,
        )
        .await
    }

    /// The live entity holding `key` for `kind` in the caller's primary
    /// namespace, or `None` when no live entity holds it.
    pub async fn get_entity_by_key(
        &self,
        token: &NamespaceToken,
        kind: &str,
        key: &str,
    ) -> RuntimeResult<Option<Entity>> {
        validate_entity_key(key)?;
        Ok(self
            .entities(token)?
            .get_entity_by_key(token.namespace().as_str(), kind, key)
            .await?)
    }

    /// Refuse a restore of entity `id` while another live entity holds any
    /// key `id` released. `disclose` says, per kind, whether the refusal may
    /// name the holder's id.
    pub(super) async fn refuse_restore_over_taken_keys(
        &self,
        token: &NamespaceToken,
        id: Uuid,
        disclose: &(dyn Fn(&str) -> bool + Sync),
    ) -> RuntimeResult<()> {
        let taken = self.entities(token)?.taken_entity_keys(id).await?;
        if taken.is_empty() {
            Ok(())
        } else {
            Err(restore_entity_key_conflict(id, &taken, disclose))
        }
    }
}

#[cfg(test)]
#[path = "entity_keys_tests.rs"]
mod tests;