kglite 0.18.0

Pure-Rust embedded Cypher knowledge graph engine with in-memory, mmap, and disk storage, and agent-facing schema introspection
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
//! Carrying relationship embedding stores between graphs: `.kgle` export and
//! import, and `copy_embeddings_with_relationships_from`.
//!
//! A relationship has no stable identity of its own — stores are keyed by
//! physical edge slots, which are graph-local. A carried vector is therefore
//! addressed by `(source type, source id, target type, target id, relationship
//! type)`. That address is unambiguous only when exactly one relationship of
//! the type connects those endpoints. A **parallel group** (two or more) is
//! carried only under a caller-named key property whose value is unique within
//! the group; without one the carry is refused by name — type, endpoints and
//! member count — and nothing is installed. Member order never enters the
//! identity, so a target graph that built the same group in another order
//! still receives each vector on the right member.
//!
//! Installation replaces the whole store, like a node import, through the same
//! WAL-capturing, undo-journalled steps the other store writes use, carrying
//! each vector's source-text hash and the store's model id.

use std::collections::{BTreeSet, HashMap};

use petgraph::graph::{EdgeIndex, NodeIndex};
use serde::{Deserialize, Serialize};

use super::{
    capture_wal_edge_embedding_bases, edge_store_key, live_edges_of_type,
    note_wal_edge_embedding_changes, validate_metric, EdgeEmbeddingStore,
};
use crate::datatypes::values::Value;
use crate::graph::embedding_validation::validate_finite_vector;
use crate::graph::embeddings::text_column_of;
use crate::graph::schema::{DirGraph, InternedKey};
use crate::graph::storage::GraphRead;

/// Caller-named key property per relationship type (`{'SUPPORTS': 'uid'}`).
pub type RelationshipKeys = HashMap<String, String>;

/// One relationship store as carried: portable addresses instead of slots.
/// This is the `.kgle` v4 record, so field order is the wire format.
#[derive(Debug, Clone, Serialize, Deserialize)]
pub(crate) struct CarriedEdgeStore {
    pub(crate) relationship_type: String,
    pub(crate) text_column: String,
    pub(crate) dimension: usize,
    pub(crate) metric: Option<String>,
    pub(crate) model_id: Option<String>,
    /// The key property the exporter used for this type, if any. An import
    /// uses it unless the caller names another.
    pub(crate) key_property: Option<String>,
    pub(crate) entries: Vec<CarriedEdgeEntry>,
}

#[derive(Debug, Clone, Serialize, Deserialize)]
pub(crate) struct CarriedEdgeEntry {
    pub(crate) source_type: String,
    pub(crate) source_id: Value,
    pub(crate) target_type: String,
    pub(crate) target_id: Value,
    /// The member's key value, when a key property was named for the type and
    /// the member carries it.
    pub(crate) key: Option<Value>,
    pub(crate) vector: Vec<f32>,
    pub(crate) text_hash: Option<u64>,
}

/// What a relationship carry did, summed over stores.
#[derive(Debug, Clone, Default, PartialEq, Eq)]
#[non_exhaustive]
pub struct RelationshipCarryStats {
    /// Stores installed on the target graph.
    pub stores: usize,
    /// Vectors installed.
    pub carried: usize,
    /// Vectors whose relationship the target graph does not have.
    pub skipped: usize,
    /// Stores that had vectors but matched no relationship, so were not
    /// installed.
    pub dropped_stores: usize,
}

/// A carried store resolved against the target graph, ready to install.
pub(crate) struct ResolvedEdgeStore {
    store: CarriedEdgeStore,
    members: Vec<(EdgeIndex, usize)>,
    skipped: usize,
}

pub(super) fn describe_group(
    graph: &DirGraph,
    relationship_type: &str,
    source: NodeIndex,
    target: NodeIndex,
    members: usize,
) -> String {
    format!(
        "{members} '{relationship_type}' relationships connect ({}) to ({})",
        super::describe_endpoint(graph, source),
        super::describe_endpoint(graph, target)
    )
}

fn refusal(store: &str, group: &str, reason: &str, relationship_type: &str) -> String {
    format!(
        "Relationship embedding store '{store}' cannot be carried: {group}, and {reason}. \
         A parallel group is carried only under a key property whose value is unique within \
         the group: pass relationship_keys={{'{relationship_type}': '<property>'}}."
    )
}

/// The live members of `relationship_type` from `source` to `target`, in slot
/// order.
///
/// Walks the source's outgoing and the target's incoming edges in lockstep
/// and answers from whichever list ends first, so the cost is twice the
/// smaller degree. `edges_connecting` walks the source's list alone, which
/// made resolving every relationship off one hub quadratic: 200k rows `Hub ->
/// Doc` took 31 s against 0.09 s for the node twin.
pub(super) fn group_members(
    graph: &DirGraph,
    source: NodeIndex,
    target: NodeIndex,
    relationship_type: &str,
) -> Vec<EdgeIndex> {
    use petgraph::Direction::{Incoming, Outgoing};
    let conn = InternedKey::from_str(relationship_type);
    let mut outgoing = graph
        .graph
        .edges_directed_filtered(source, Outgoing, Some(conn));
    let mut incoming = graph
        .graph
        .edges_directed_filtered(target, Incoming, Some(conn));
    let (mut from_source, mut from_target) = (Vec::new(), Vec::new());
    let mut members = loop {
        match outgoing.next() {
            None => break from_source,
            Some(edge) if edge.target() == target && edge.connection_type() == conn => {
                from_source.push(edge.id())
            }
            Some(_) => {}
        }
        match incoming.next() {
            None => break from_target,
            Some(edge) if edge.source() == source && edge.connection_type() == conn => {
                from_target.push(edge.id())
            }
            Some(_) => {}
        }
    };
    members.sort_unstable_by_key(|edge| edge.index());
    members
}

pub(super) fn key_value(graph: &DirGraph, edge: EdgeIndex, property: &str) -> Option<Value> {
    graph
        .graph
        .edge_weight(edge)?
        .get_property(property)
        .filter(|value| !matches!(value, Value::Null))
        .cloned()
}

/// Every member's key value, refusing a member without one or a repeated value.
pub(super) fn group_keys(
    graph: &DirGraph,
    members: &[EdgeIndex],
    property: &str,
) -> Result<Vec<(EdgeIndex, Value)>, String> {
    let mut keyed: Vec<(EdgeIndex, Value)> = Vec::with_capacity(members.len());
    for &edge in members {
        let value = key_value(graph, edge, property)
            .ok_or_else(|| format!("a member has no '{property}' value"))?;
        if keyed.iter().any(|(_, seen)| *seen == value) {
            return Err(format!(
                "'{property}' repeats the value {value} within the group"
            ));
        }
        keyed.push((edge, value));
    }
    Ok(keyed)
}

/// Extract every relationship store of `src` as carried stores, refusing any
/// store with a vector on a parallel-group member that no usable key names.
pub(crate) fn extract_edge_stores(
    src: &DirGraph,
    keys: &RelationshipKeys,
) -> Result<Vec<CarriedEdgeStore>, String> {
    let guard = src.graph.begin_query();
    let mut stores: Vec<_> = src.edge_embeddings.iter().collect();
    stores.sort_unstable_by(|a, b| a.0.cmp(b.0));
    let mut carried = Vec::with_capacity(stores.len());
    for ((relationship_type, name), store) in stores {
        carried.push(extract_one(src, relationship_type, name, store, keys)?);
    }
    drop(guard);
    Ok(carried)
}

fn extract_one(
    src: &DirGraph,
    relationship_type: &str,
    name: &str,
    store: &EdgeEmbeddingStore,
    keys: &RelationshipKeys,
) -> Result<CarriedEdgeStore, String> {
    let key_property = keys.get(relationship_type).cloned();
    let text_column = text_column_of(name).unwrap_or(name).to_string();
    let label = format!("{relationship_type}.{text_column}");
    let mut checked: BTreeSet<(usize, usize)> = BTreeSet::new();
    let mut entries = Vec::with_capacity(store.len());
    for edge in store.edges() {
        let Some((source, target)) = src.graph.edge_endpoints(edge) else {
            continue;
        };
        let members = group_members(src, source, target, relationship_type);
        if members.len() > 1 && checked.insert((source.index(), target.index())) {
            let group = describe_group(src, relationship_type, source, target, members.len());
            let Some(property) = key_property.as_deref() else {
                let reason = format!("relationship_keys names no key for '{relationship_type}'");
                return Err(refusal(&label, &group, &reason, relationship_type));
            };
            group_keys(src, &members, property)
                .map_err(|reason| refusal(&label, &group, &reason, relationship_type))?;
        }
        let (Some(source_view), Some(target_view)) =
            (src.graph.node_view(source), src.graph.node_view(target))
        else {
            continue;
        };
        let Some(vector) = store.get(edge) else {
            continue;
        };
        entries.push(CarriedEdgeEntry {
            source_type: source_view.node_type_str(&src.interner).to_string(),
            source_id: source_view.id().into_owned(),
            target_type: target_view.node_type_str(&src.interner).to_string(),
            target_id: target_view.id().into_owned(),
            key: key_property
                .as_deref()
                .and_then(|property| key_value(src, edge, property)),
            vector: vector.to_vec(),
            text_hash: store.text_hash(edge),
        });
    }
    Ok(CarriedEdgeStore {
        relationship_type: relationship_type.to_string(),
        text_column,
        dimension: store.dimension(),
        metric: store.metric().map(str::to_owned),
        model_id: store.model_id().map(str::to_owned),
        key_property,
        entries,
    })
}

/// Validate every carried store's shape before anything is installed.
pub(crate) fn validate_carried_edge_stores(stores: &[CarriedEdgeStore]) -> Result<(), String> {
    for store in stores {
        let label = format!("{}.{}", store.relationship_type, store.text_column);
        if store.dimension == 0 {
            return Err(format!(
                "Relationship embedding store '{label}' has dimension 0"
            ));
        }
        validate_metric(store.metric.as_deref())
            .map_err(|error| format!("Relationship embedding store '{label}': {error}"))?;
        for (index, entry) in store.entries.iter().enumerate() {
            if entry.vector.len() != store.dimension {
                return Err(format!(
                    "Invalid embedding in relationship store '{label}' at entry {index}: \
                     expected width {}, got {}",
                    store.dimension,
                    entry.vector.len()
                ));
            }
            validate_finite_vector(&entry.vector).map_err(|error| {
                format!(
                    "Invalid embedding in relationship store '{label}' at entry {index}: {error}"
                )
            })?;
        }
    }
    Ok(())
}

/// Resolve carried stores against `dst`. A parallel group on the target side
/// that cannot be told apart by the key is refused by name before anything is
/// installed; an entry whose relationship `dst` lacks is counted as skipped.
pub(crate) fn resolve_edge_stores(
    dst: &mut DirGraph,
    stores: Vec<CarriedEdgeStore>,
    keys: &RelationshipKeys,
) -> Result<Vec<ResolvedEdgeStore>, String> {
    validate_carried_edge_stores(&stores)?;
    let endpoint_types: BTreeSet<String> = stores
        .iter()
        .flat_map(|store| store.entries.iter())
        .flat_map(|entry| [entry.source_type.clone(), entry.target_type.clone()])
        .collect();
    for node_type in &endpoint_types {
        dst.build_id_index(node_type);
    }
    let dst: &DirGraph = dst;
    let guard = dst.graph.begin_query();
    let resolved = stores
        .into_iter()
        .map(|store| resolve_one(dst, store, keys))
        .collect::<Result<Vec<_>, _>>();
    drop(guard);
    resolved
}

fn resolve_one(
    dst: &DirGraph,
    store: CarriedEdgeStore,
    keys: &RelationshipKeys,
) -> Result<ResolvedEdgeStore, String> {
    let key_property = keys
        .get(&store.relationship_type)
        .cloned()
        .or_else(|| store.key_property.clone());
    let label = format!("{}.{}", store.relationship_type, store.text_column);
    let mut members = Vec::with_capacity(store.entries.len());
    let mut claimed: HashMap<usize, usize> = HashMap::new();
    let mut skipped = 0usize;
    let mut group_cache: HashMap<(usize, usize), Vec<(EdgeIndex, Value)>> = HashMap::new();
    for (index, entry) in store.entries.iter().enumerate() {
        let endpoints = (
            dst.lookup_by_id_readonly(&entry.source_type, &entry.source_id),
            dst.lookup_by_id_readonly(&entry.target_type, &entry.target_id),
        );
        let (Some(source), Some(target)) = endpoints else {
            skipped += 1;
            continue;
        };
        let group = group_members(dst, source, target, &store.relationship_type);
        let matched = match group.as_slice() {
            [] => None,
            [only] => match (&entry.key, key_property.as_deref()) {
                (Some(wanted), Some(property)) => match key_value(dst, *only, property) {
                    Some(found) if found != *wanted => None,
                    _ => Some(*only),
                },
                _ => Some(*only),
            },
            _ => {
                let described =
                    describe_group(dst, &store.relationship_type, source, target, group.len());
                let (Some(wanted), Some(property)) = (&entry.key, key_property.as_deref()) else {
                    let reason = "the carried vector names no key value to choose a member by";
                    return Err(refusal(
                        &label,
                        &described,
                        reason,
                        &store.relationship_type,
                    ));
                };
                let slot = (source.index(), target.index());
                if let std::collections::hash_map::Entry::Vacant(vacant) = group_cache.entry(slot) {
                    let keyed = group_keys(dst, &group, property).map_err(|reason| {
                        refusal(&label, &described, &reason, &store.relationship_type)
                    })?;
                    vacant.insert(keyed);
                }
                group_cache[&slot]
                    .iter()
                    .find(|(_, value)| value == wanted)
                    .map(|(edge, _)| *edge)
            }
        };
        let Some(edge) = matched else {
            skipped += 1;
            continue;
        };
        if claimed.insert(edge.index(), index).is_some() {
            return Err(format!(
                "Relationship embedding store '{label}' cannot be carried: two carried vectors \
                 resolve to the same relationship of this graph."
            ));
        }
        members.push((edge, index));
    }
    Ok(ResolvedEdgeStore {
        store,
        members,
        skipped,
    })
}

/// Install resolved stores, replacing any same-named relationship store.
/// A store whose vectors all missed is not installed (`dropped_stores`), as a
/// node import does not install an unmatched store.
pub(crate) fn install_edge_stores(
    dst: &mut DirGraph,
    resolved: Vec<ResolvedEdgeStore>,
) -> Result<RelationshipCarryStats, String> {
    let mut stats = RelationshipCarryStats::default();
    for resolved in resolved {
        stats.skipped += resolved.skipped;
        if resolved.members.is_empty() {
            if !resolved.store.entries.is_empty() {
                stats.dropped_stores += 1;
            }
            continue;
        }
        stats.carried += resolved.members.len();
        stats.stores += 1;
        install_one(dst, resolved)?;
    }
    if stats.stores > 0 {
        dst.bump_version();
    }
    Ok(stats)
}

fn install_one(dst: &mut DirGraph, resolved: ResolvedEdgeStore) -> Result<(), String> {
    let store = &resolved.store;
    let mut successor = EdgeEmbeddingStore::new(store.dimension, store.metric.as_deref());
    successor.set_wal_metadata(
        store.dimension,
        store.metric.clone(),
        store.model_id.clone(),
    )?;
    for &(edge, index) in &resolved.members {
        let entry = &store.entries[index];
        successor.install_wal_vector(edge, &entry.vector, entry.text_hash);
    }
    // The whole store is replaced, so every live member of the type is a
    // touched group: the WAL records each group's final state, prior members
    // included.
    let affected = live_edges_of_type(dst, &store.relationship_type)?;
    capture_wal_edge_embedding_bases(
        dst,
        &store.relationship_type,
        &store.text_column,
        affected.iter().copied(),
    )?;
    let key = edge_store_key(&store.relationship_type, &store.text_column);
    let prior = dst.edge_embeddings.insert(key.clone(), successor);
    if let Some(journal) = dst.graph.undo_journal_mut() {
        journal.note_edge_embedding_store_replaced(key, prior);
    }
    note_wal_edge_embedding_changes(
        dst,
        &store.relationship_type,
        &store.text_column,
        true,
        affected,
    );
    Ok(())
}

/// Relationship half of a graph-to-graph embedding copy.
pub(crate) fn resolve_copy(
    dst: &mut DirGraph,
    src: &DirGraph,
    keys: &RelationshipKeys,
) -> Result<Vec<ResolvedEdgeStore>, String> {
    let carried = extract_edge_stores(src, keys)?;
    resolve_edge_stores(dst, carried, keys)
}

/// `(stores_copied, vectors_copied, vectors_skipped)` for node stores plus the
/// relationship carry, as returned by
/// [`DirGraph::copy_embeddings_with_relationships_from`].
#[derive(Debug, Clone, PartialEq, Eq)]
#[non_exhaustive]
pub struct EmbeddingCopyReport {
    pub stores_copied: usize,
    pub vectors_copied: usize,
    pub vectors_skipped: usize,
    pub relationships: RelationshipCarryStats,
}

impl DirGraph {
    /// [`copy_embeddings_from`](Self::copy_embeddings_from) plus every
    /// relationship embedding store of `src`, addressed by endpoint ids and,
    /// for parallel groups, the key property `keys` names for the type.
    ///
    /// Atomic with respect to refusal: an ambiguous relationship (a parallel
    /// group without a usable key, on either side) is refused by name before
    /// any node or relationship store is written.
    pub fn copy_embeddings_with_relationships_from(
        &mut self,
        src: &DirGraph,
        keys: &RelationshipKeys,
    ) -> Result<EmbeddingCopyReport, String> {
        let resolved = resolve_copy(self, src, keys)?;
        let (stores_copied, vectors_copied, vectors_skipped) = self.copy_embeddings_from(src);
        let relationships = install_edge_stores(self, resolved)?;
        Ok(EmbeddingCopyReport {
            stores_copied,
            vectors_copied,
            vectors_skipped,
            relationships,
        })
    }
}

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

/// One relationship's stored vector, addressed by its endpoints — a row of
/// [`relationship_embeddings`].
#[derive(Debug, Clone, PartialEq)]
#[non_exhaustive]
pub struct RelationshipEmbedding {
    /// The source node's type.
    pub source_type: String,
    /// The source node's id.
    pub source_id: Value,
    /// The target node's type.
    pub target_type: String,
    /// The target node's id.
    pub target_id: Value,
    /// The relationship's value of the key property named for its type, when
    /// one was named and the relationship carries it.
    pub key: Option<Value>,
    /// The stored vector.
    pub vector: Vec<f32>,
}

/// Every vector in the `(relationship_type, text_column)` store, addressed by
/// endpoint ids — the relationship twin of reading a node store by node id,
/// and the shape an edge list (`edge_index`) plus an edge-feature matrix
/// (`edge_attr`) is built from.
///
/// Rows are ordered by source (type, id), then target (type, id), then key
/// (when one is named), then relationship slot, so the order is stable for a
/// given graph. Several relationships of the type between the same two nodes
/// (a parallel group) are all returned; naming a key property for the type in
/// `keys` tells them apart, and a named key that is missing on a member or
/// repeats within a group is refused by name rather than returned ambiguous.
/// Refused when there is no such store.
pub fn relationship_embeddings(
    graph: &DirGraph,
    relationship_type: &str,
    text_column: &str,
    keys: &RelationshipKeys,
) -> Result<Vec<RelationshipEmbedding>, String> {
    let label = format!("{relationship_type}.{text_column}");
    let store = graph
        .edge_embeddings
        .get(&edge_store_key(relationship_type, text_column))
        .ok_or_else(|| {
            crate::graph::embedding_hints::missing_store_error(
                graph,
                crate::graph::embedding_inventory::EmbeddingEntity::Relationship,
                relationship_type,
                text_column,
                crate::graph::embedding_hints::Surface::Method,
            )
        })?;
    let key_property = keys.get(relationship_type).map(String::as_str);
    let guard = graph.graph.begin_query();
    let mut checked: BTreeSet<(usize, usize)> = BTreeSet::new();
    let mut rows: Vec<(usize, RelationshipEmbedding)> = Vec::with_capacity(store.len());
    for edge in store.edges() {
        let Some((source, target)) = graph.graph.edge_endpoints(edge) else {
            continue;
        };
        if let Some(property) = key_property {
            if checked.insert((source.index(), target.index())) {
                let members = group_members(graph, source, target, relationship_type);
                if members.len() > 1 {
                    group_keys(graph, &members, property).map_err(|reason| {
                        let group =
                            describe_group(graph, relationship_type, source, target, members.len());
                        format!(
                            "Relationship embedding store '{label}': {group}, and {reason}; the \
                             key named in relationship_keys must be unique within each group"
                        )
                    })?;
                }
            }
        }
        let (Some(source_view), Some(target_view), Some(vector)) = (
            graph.graph.node_view(source),
            graph.graph.node_view(target),
            store.get(edge),
        ) else {
            continue;
        };
        rows.push((
            edge.index(),
            RelationshipEmbedding {
                source_type: source_view.node_type_str(&graph.interner).to_string(),
                source_id: source_view.id().into_owned(),
                target_type: target_view.node_type_str(&graph.interner).to_string(),
                target_id: target_view.id().into_owned(),
                key: key_property.and_then(|property| key_value(graph, edge, property)),
                vector: vector.to_vec(),
            },
        ));
    }
    drop(guard);
    rows.sort_by(|(left_slot, left), (right_slot, right)| {
        (
            &left.source_type,
            &left.source_id,
            &left.target_type,
            &left.target_id,
            &left.key,
        )
            .cmp(&(
                &right.source_type,
                &right.source_id,
                &right.target_type,
                &right.target_id,
                &right.key,
            ))
            .then(left_slot.cmp(right_slot))
    });
    Ok(rows.into_iter().map(|(_, row)| row).collect())
}