macrame/graph/subgraph.rs
1//! The in-memory graph loaded from `links_current`, and its loader (§5.4).
2//!
3//! A `Subgraph` is derivative state (Doctrine VI): every field is re-derivable
4//! from the ledger, nothing here is authoritative, and dropping one loses
5//! nothing. That is what lets analytics run on a snapshot without a third clock
6//! — the graph is the topology as of one instant, and the instant is the
7//! caller's `now_ts`, not a property of the structure.
8
9use std::collections::BTreeMap;
10
11use crate::connection::{Annotation, Database};
12use crate::error::{DbError, Result};
13
14/// Edges returned to a caller asking for a node with no edges in that direction.
15const NO_EDGES: &[EdgeRef] = &[];
16
17/// A transient, in-memory graph loaded from `links_current`.
18///
19/// The maps are `BTreeMap`, not `HashMap`, so iteration follows node id order.
20/// Every algorithm in [`super::algorithms`] inherits its determinism from that
21/// choice, and Louvain in particular returns a different partition under a
22/// randomised iteration order.
23///
24/// # Closure
25///
26/// **Every id appearing in `out_adj` or `in_adj` — as a key or as an
27/// [`EdgeRef::node`] — is a key of `nodes`.** `drop_dangling_adjacency` — private,
28/// and named here because it is the sole establisher — establishes it and
29/// [`Subgraph::is_closed`] checks it; every algorithm in
30/// [`super::algorithms`] is written assuming it and none of them re-checks.
31///
32/// It did not hold before Wave 1 (defect Z), and the way it failed is the reason
33/// it is now stated on the type rather than left to the loader. Adjacency comes
34/// from `links_current`, which carries edges to retired concepts; `hydrate`
35/// filters `retired = 0`. So a retired neighbour left an `EdgeRef` pointing at
36/// an id with no `NodeData`, and the five algorithms each met that differently:
37/// `louvain` panicked on the missing map entry, `scc` emitted the absent node as
38/// a phantom component of its own, `k_core` counted a degree of 2 where one edge
39/// was in the graph, and `dijkstra` returned a finite distance to a node the
40/// caller could not then look up. Four handlings of one violated invariant, none
41/// of them chosen — and the panic was the least damaging, because the other
42/// three answer.
43///
44/// Dangling entries are **dropped** rather than admitted with a tombstone node.
45/// A retired concept is not visible (§4.1), analytics over a graph is analytics
46/// over what is visible, and the alternative pushes a three-state node onto
47/// every present and future algorithm to preserve edges whose endpoint the
48/// caller is not entitled to read. Retirement is the supported path — concepts
49/// are never deleted (D-022) — so this is ordinary use, not a corner.
50///
51/// # Why the fields are private (0.8.0, B1, D-114)
52///
53/// They were `pub` through 0.7.0, and the three maps were the crate's most
54/// widely read data structure. That made **every detail of the representation
55/// part of the public API** — the `BTreeMap`, the `String` keys, the fact that
56/// adjacency is stored as two maps at all — none of which was ever a promise
57/// anyone intended to make.
58///
59/// The immediate reason is D-087: interning the keys to `u32` cannot be done
60/// at all while `EdgeRef::node` is a public `String`. The break is taken **once**, here, with the representation
61/// unchanged, so that anything depending on the old shape fails against code
62/// that still behaves identically.
63///
64/// Accessors return borrowed views, so nothing here costs an allocation that
65/// field access did not.
66#[derive(Debug, Clone, Default)]
67pub struct Subgraph {
68 nodes: BTreeMap<String, NodeData>,
69 out_adj: BTreeMap<String, Vec<EdgeRef>>,
70 in_adj: BTreeMap<String, Vec<EdgeRef>>,
71 /// Every string an `EdgeRef` carries. See [`Interner`].
72 pool: Interner,
73}
74
75/// The attributes of one node, as of the instant the graph was loaded.
76///
77/// Fields are private for the reason given on [`Subgraph`]; `content` is the
78/// one whose type is expected to move.
79#[derive(Debug, Clone, PartialEq)]
80pub struct NodeData {
81 title: String,
82 /// **`None` means "not loaded", not "empty" (0.8.0, B3, D-116).**
83 ///
84 /// Document text is not loaded unless a caller asks. No algorithm reads it
85 /// — `dijkstra`, `astar`, `scc`, `k_core`, `louvain` and `modularity` touch
86 /// topology and weight only — and at realistic document sizes it is most of
87 /// the byte budget, so the default load spent the budget on bytes nothing
88 /// would look at.
89 ///
90 /// An `Option` rather than an empty `String` because a sentinel that is a
91 /// *valid value of the type* cannot be told apart from the real thing: a
92 /// concept with genuinely empty content and one whose content was not
93 /// requested are different facts, and they differ exactly when a caller is
94 /// deciding whether to go back to the database. Same refusal
95 /// [D-096](../../docs/architecture/s13-decision-register.md) made for the
96 /// open interval.
97 content: Option<String>,
98 embedding_model: Option<String>,
99 valid_from: String,
100 valid_to: String,
101}
102
103impl NodeData {
104 /// A node with no content and no embedding model — what the default load
105 /// produces. Use [`Self::with_content`] and [`Self::with_embedding_model`]
106 /// to add either.
107 pub fn new(
108 title: impl Into<String>,
109 valid_from: impl Into<String>,
110 valid_to: impl Into<String>,
111 ) -> Self {
112 Self {
113 title: title.into(),
114 content: None,
115 embedding_model: None,
116 valid_from: valid_from.into(),
117 valid_to: valid_to.into(),
118 }
119 }
120
121 #[must_use]
122 pub fn with_content(mut self, content: impl Into<String>) -> Self {
123 self.content = Some(content.into());
124 self
125 }
126
127 #[must_use]
128 pub fn with_embedding_model(mut self, model: Option<String>) -> Self {
129 self.embedding_model = model;
130 self
131 }
132
133 pub fn title(&self) -> &str {
134 &self.title
135 }
136
137 /// The document text, or `None` when it was not requested.
138 ///
139 /// **`None` is not an empty document.** See the field's own note: the
140 /// default load does not fetch content, so a caller that did not ask gets
141 /// `None` and can tell that apart from a concept whose content really is
142 /// `""`.
143 pub fn content(&self) -> Option<&str> {
144 self.content.as_deref()
145 }
146
147 pub fn embedding_model(&self) -> Option<&str> {
148 self.embedding_model.as_deref()
149 }
150
151 pub fn valid_from(&self) -> &str {
152 &self.valid_from
153 }
154
155 pub fn valid_to(&self) -> &str {
156 &self.valid_to
157 }
158}
159
160/// The string pool an interned [`EdgeRef`] indexes into (0.8.0, B2, D-115).
161///
162/// One pool for every string an edge carries — node ids, edge types and the two
163/// timestamps — because they dedupe against each other for free and the whole
164/// point is that the cost is per **distinct string** rather than per edge.
165///
166/// Indices are handed out first-seen. **Nothing observable depends on them**:
167/// node order comes from `nodes`, which is still a `BTreeMap` keyed by id, and
168/// adjacency order is the order edges were added, exactly as before. That is
169/// the deliberate answer to D-063's warning that "determinism stops being
170/// structural and becomes procedural" — it does not, because the node map was
171/// never what needed interning. `node_order_does_not_depend_on_construction_order`
172/// is the gate that holds it.
173#[derive(Debug, Clone, Default)]
174struct Interner {
175 strings: Vec<String>,
176 index: BTreeMap<String, u32>,
177 /// Running payload total, maintained on insert.
178 ///
179 /// **Not recomputed.** The first version of the loader called
180 /// `estimated_bytes()` before and after every edge to charge the marginal
181 /// pool cost, which is O(pool) per row and made loading quadratic — the
182 /// exact defect [D-047](../../docs/architecture/s13-decision-register.md)
183 /// diagnosed and fixed, re-introduced by the change that was supposed to
184 /// make loading *cheaper*. `loading_scales_linearly_in_the_number_of_edges`
185 /// caught it, which is what that test is for.
186 bytes: usize,
187}
188
189impl Interner {
190 /// Intern `s`, returning its index and **how many bytes that cost** — zero
191 /// when the string was already pooled.
192 ///
193 /// The caller needs the marginal figure to charge the byte budget as it
194 /// loads, and it has to be O(1) or the budget check is quadratic again.
195 fn intern(&mut self, s: &str) -> (u32, usize) {
196 if let Some(&i) = self.index.get(s) {
197 return (i, 0);
198 }
199 let i = u32::try_from(self.strings.len())
200 .expect("a subgraph cannot hold 2^32 distinct strings within any byte budget");
201 self.strings.push(s.to_string());
202 self.index.insert(s.to_string(), i);
203 let cost = Self::entry_bytes(s);
204 self.bytes += cost;
205 (i, cost)
206 }
207
208 /// Once in `strings`, once as the key of `index`, plus both containers'
209 /// per-entry overhead.
210 fn entry_bytes(s: &str) -> usize {
211 2 * s.len() + std::mem::size_of::<String>() + std::mem::size_of::<u32>()
212 }
213
214 fn get(&self, i: u32) -> &str {
215 &self.strings[i as usize]
216 }
217
218 /// Payload bytes held by the pool, counted the way [`Subgraph::node_bytes`]
219 /// counts: string bytes plus per-item overhead.
220 ///
221 /// **This is the arithmetic D-063 asked for.** Its objection to interning
222 /// was that an id table "stores every id a second time, partly cancelling
223 /// the memory win". It is counted here rather than argued about: the
224 /// duplication is per distinct string, the saving is per edge entry, and
225 /// `estimated_bytes()` reports the sum so a caller can see both.
226 fn estimated_bytes(&self) -> usize {
227 self.bytes
228 }
229}
230
231/// One end of an edge in an adjacency list — **interned** (0.8.0, B2, D-115).
232///
233/// Five fields, no heap payload, `size_of` 24 bytes against 104 bytes of struct
234/// plus around 250 of strings before. Every field but the weight is an index
235/// into its [`Subgraph`]'s pool, so reading one needs the graph:
236///
237/// ```ignore
238/// for e in graph.out_edges("a") {
239/// println!("{} {} {}", e.node(&graph), e.edge_type(&graph), e.weight());
240/// }
241/// ```
242///
243/// That is the visible cost of the change, and it is the reason B1 had to
244/// privatise these fields first: a public `node: String` cannot become a `u32`.
245/// The win is **reachability**, not speed ([D-073](../../docs/architecture/s13-decision-register.md)'s
246/// category): graphs that did not fit the byte budget start fitting.
247///
248/// # Invariants
249///
250/// An `EdgeRef` is tied to the specific [`Subgraph`] it was retrieved from.
251/// Querying it against a different one — via an accessor like [`Self::node`],
252/// or via derived `PartialEq` — is a **logic error** that will silently return
253/// incorrect data or report equality where none exists. Because the handle is
254/// `Copy` it can be stored in a struct that outlives the graph; it stays
255/// well-formed and becomes meaningless without its pool.
256///
257/// `PartialEq` is the sharp edge, and it is kept rather than removed: *within*
258/// one graph, index equality is exactly the comparison a caller wants, and it
259/// is cheaper and stricter than comparing five strings. Across two graphs it
260/// compares indices that mean different things — a wrong answer that needs no
261/// accessor call at all, so it sits outside the mental model of "querying".
262/// Before interning, `==` compared the strings and could not be wrong this way.
263///
264/// This logic error does not result in undefined behaviour — every index goes
265/// through bounds-checked slice indexing and there is no `unsafe` here — but
266/// the results are otherwise unspecified.
267///
268/// The handle is intentionally **not** lifetime-branded, which would make the
269/// invariant a compile error, because that propagates a generic parameter
270/// through every algorithm and every signature that mentions a `Subgraph`. See
271/// D-115 for the argument and for what to do if this is ever hit in practice.
272#[derive(Clone, Copy, PartialEq)]
273pub struct EdgeRef {
274 node: u32,
275 edge_type: u32,
276 weight: f64,
277 valid_from: u32,
278 valid_to: u32,
279}
280
281/// Written by hand so a failing `assert_eq!` cannot be mistaken for one about
282/// strings.
283///
284/// The derived form printed `EdgeRef { node: 3, edge_type: 1, .. }`, which
285/// reads as data and is not: those are pool indices, meaningless without the
286/// graph. The `#` is there to say so at a glance.
287impl std::fmt::Debug for EdgeRef {
288 fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
289 write!(
290 f,
291 "EdgeRef(node=#{}, type=#{}, w={}, from=#{}, to=#{})",
292 self.node, self.edge_type, self.weight, self.valid_from, self.valid_to
293 )
294 }
295}
296
297impl EdgeRef {
298 /// The far end of the edge: the target in `out_edges`, the source in
299 /// `in_edges`.
300 ///
301 /// Takes the graph because the string lives in its pool. `graph` must be
302 /// the one this edge came from; passing another is a programming error and
303 /// will panic or answer nonsense, exactly as indexing the wrong slice would.
304 pub fn node<'a>(&self, graph: &'a Subgraph) -> &'a str {
305 graph.pool.get(self.node)
306 }
307
308 pub fn edge_type<'a>(&self, graph: &'a Subgraph) -> &'a str {
309 graph.pool.get(self.edge_type)
310 }
311
312 /// The only field that is not interned, because an `f64` is already 8 bytes
313 /// and a pool of them would cost more than it saved.
314 pub fn weight(&self) -> f64 {
315 self.weight
316 }
317
318 pub fn valid_from<'a>(&self, graph: &'a Subgraph) -> &'a str {
319 graph.pool.get(self.valid_from)
320 }
321
322 pub fn valid_to<'a>(&self, graph: &'a Subgraph) -> &'a str {
323 graph.pool.get(self.valid_to)
324 }
325}
326
327impl Subgraph {
328 /// Whether `id` is a hydrated node of this graph.
329 ///
330 /// By the closure invariant this is also the answer to "may an algorithm
331 /// look this id up", which is why every algorithm asks it rather than
332 /// probing adjacency.
333 pub fn contains_node(&self, id: &str) -> bool {
334 self.nodes.contains_key(id)
335 }
336
337 /// The attributes of `id`, or `None` when it is not in the graph.
338 pub fn node(&self, id: &str) -> Option<&NodeData> {
339 self.nodes.get(id)
340 }
341
342 /// Node ids in ascending order.
343 ///
344 /// The order is `BTreeMap`'s and is load-bearing rather than incidental:
345 /// Louvain breaks ties by first-seen community and returns a different
346 /// partition under a randomised order.
347 pub fn node_ids(&self) -> impl ExactSizeIterator<Item = &str> + '_ {
348 self.nodes.keys().map(String::as_str)
349 }
350
351 pub fn node_count(&self) -> usize {
352 self.nodes.len()
353 }
354
355 /// Every node with its attributes, in id order.
356 pub fn nodes(&self) -> impl ExactSizeIterator<Item = (&str, &NodeData)> + '_ {
357 self.nodes.iter().map(|(id, d)| (id.as_str(), d))
358 }
359
360 /// The outgoing index: each node that has outgoing edges, with them.
361 ///
362 /// For one node prefer [`Self::out_edges`]. This exists for callers that
363 /// must walk the whole index — the Python `to_dict`, and the diagnostics.
364 pub fn out_adjacency(&self) -> impl Iterator<Item = (&str, &[EdgeRef])> + '_ {
365 self.out_adj.iter().map(|(id, e)| (id.as_str(), e.as_slice()))
366 }
367
368 /// The incoming index. See [`Self::out_adjacency`].
369 pub fn in_adjacency(&self) -> impl Iterator<Item = (&str, &[EdgeRef])> + '_ {
370 self.in_adj.iter().map(|(id, e)| (id.as_str(), e.as_slice()))
371 }
372
373 /// Add or replace a node, returning what was there before.
374 ///
375 /// Public so that callers who build a graph by hand — the test fixtures,
376 /// the diagnostics — can still do so now the fields are private. It does
377 /// **not** establish the closure invariant on its own: adjacency naming an
378 /// id never inserted is still dangling, exactly as before.
379 pub fn insert_node(&mut self, id: impl Into<String>, data: NodeData) -> Option<NodeData> {
380 self.nodes.insert(id.into(), data)
381 }
382
383 /// Outgoing edges of `node`, empty when it has none or is absent.
384 pub fn out_edges(&self, node: &str) -> &[EdgeRef] {
385 self.out_adj.get(node).map_or(NO_EDGES, Vec::as_slice)
386 }
387
388 /// Incoming edges of `node`, empty when it has none or is absent.
389 pub fn in_edges(&self, node: &str) -> &[EdgeRef] {
390 self.in_adj.get(node).map_or(NO_EDGES, Vec::as_slice)
391 }
392
393 /// Undirected edge count incident to `node`, counting parallel edges once
394 /// each and a self-loop twice.
395 pub fn degree(&self, node: &str) -> usize {
396 self.out_edges(node).len() + self.in_edges(node).len()
397 }
398
399 /// Undirected weight incident to `node`. Summed over both directions, so
400 /// summing this over all nodes gives `2 * total_weight`.
401 pub fn weighted_degree(&self, node: &str) -> f64 {
402 self.out_edges(node).iter().map(|e| e.weight).sum::<f64>()
403 + self.in_edges(node).iter().map(|e| e.weight).sum::<f64>()
404 }
405
406 /// Total edge weight, each edge counted once — the `m` of the modularity
407 /// formulas.
408 pub fn total_weight(&self) -> f64 {
409 self.out_adj
410 .values()
411 .flat_map(|edges| edges.iter().map(|e| e.weight))
412 .sum()
413 }
414
415 pub fn edge_count(&self) -> usize {
416 self.out_adj.values().map(Vec::len).sum()
417 }
418
419 /// Remove adjacency entries whose endpoint is not a hydrated node.
420 ///
421 /// This is what establishes the closure invariant on the type's docs, and it
422 /// runs after `hydrate` because that is the first moment the set of visible
423 /// nodes is known — the walk is over `links_current`, which does not record
424 /// retirement.
425 ///
426 /// A node left with no edges keeps its (now empty) entry only if it had one;
427 /// entries emptied by the prune are removed outright, so `out_adj` and
428 /// `in_adj` do not accumulate keys for nodes that turned out to have nothing.
429 /// Keys that are themselves not hydrated go too, which covers the case where
430 /// the *source* is the retired concept rather than the target.
431 ///
432 /// The byte accounting is deliberately not rewound. `bytes` bounded the load
433 /// as it ran and refused early on that basis, so a graph that would have fit
434 /// after pruning can still be refused before it. That is conservative in the
435 /// safe direction — the budget exists to stop an allocation, and the
436 /// allocation happens during the walk, not after it.
437 fn drop_dangling_adjacency(&mut self) {
438 // Destructured so `nodes` is borrowed separately from the two maps being
439 // mutated — the same borrow through `self` inside the closure would not
440 // compile.
441 let Subgraph {
442 nodes,
443 out_adj,
444 in_adj,
445 pool,
446 } = self;
447
448 for adj in [out_adj, in_adj] {
449 adj.retain(|id, edges| {
450 if !nodes.contains_key(id) {
451 return false;
452 }
453 edges.retain(|e| nodes.contains_key(pool.get(e.node)));
454 !edges.is_empty()
455 });
456 }
457 }
458
459 /// Whether the closure invariant holds. Used by tests and `debug_assert`s.
460 ///
461 /// Cheap enough to call in a test and O(V + E), so not on any hot path.
462 ///
463 /// **The `debug_assert`s were only a claim until 0.10.0** (W4.8). This
464 /// sentence shipped in 0.6.0 and none existed in `src/`; they now sit at the
465 /// entry of `dijkstra`, `astar`, `scc`, `k_core` and `louvain`
466 /// (`algorithms::CLOSURE`). Writing them was the fix rather than weakening
467 /// the sentence: the type docs above say every algorithm assumes closure and
468 /// none re-checks it, and an assert is the auditable form of that.
469 pub fn is_closed(&self) -> bool {
470 self.out_adj
471 .iter()
472 .chain(self.in_adj.iter())
473 .all(|(id, edges)| {
474 self.nodes.contains_key(id)
475 && edges
476 .iter()
477 .all(|e| self.nodes.contains_key(self.pool.get(e.node)))
478 })
479 }
480
481 /// Record an edge in both directions.
482 ///
483 /// Both indices are maintained together because every undirected quantity
484 /// here — degree, k-core peeling, Louvain's `k_i` — reads them as a pair. An
485 /// `in_adj` that lags `out_adj` would not fail loudly; it would return a
486 /// plausible wrong number.
487 /// **Public since 0.8.0.** The callers that used to push into both maps by
488 /// hand cannot now the fields are private, and routing them through the one
489 /// function that maintains the pair is the point rather than a consolation:
490 /// hand-written adjacency was two chances to get the reverse edge wrong,
491 /// and every such call site was already doing the `back.node = source`
492 /// dance itself. `edge.node` is expected to be `target`; the reverse entry
493 /// is derived here.
494 /// Returns the bytes this edge added to [`Self::estimated_bytes`] — the two
495 /// fixed-size entries plus whatever strings were genuinely new. The loader
496 /// charges its budget with it, and it is O(1) by construction.
497 pub fn add_edge(
498 &mut self,
499 source: &str,
500 target: &str,
501 edge_type: &str,
502 weight: f64,
503 valid_from: &str,
504 valid_to: &str,
505 ) -> usize {
506 let (src, b1) = self.pool.intern(source);
507 let (tgt, b2) = self.pool.intern(target);
508 let (ty, b3) = self.pool.intern(edge_type);
509 let (from, b4) = self.pool.intern(valid_from);
510 let (to, b5) = self.pool.intern(valid_to);
511 let pooled = b1 + b2 + b3 + b4 + b5;
512
513 self.out_adj.entry(source.to_string()).or_default().push(EdgeRef {
514 node: tgt,
515 edge_type: ty,
516 weight,
517 valid_from: from,
518 valid_to: to,
519 });
520 self.in_adj.entry(target.to_string()).or_default().push(EdgeRef {
521 node: src,
522 edge_type: ty,
523 weight,
524 valid_from: from,
525 valid_to: to,
526 });
527 2 * std::mem::size_of::<EdgeRef>() + pooled
528 }
529
530 /// Estimated payload bytes for one node, keyed by `id`.
531 ///
532 /// The per-item functions are the single definition of the estimate.
533 /// [`Self::estimated_bytes`] sums them over a whole graph; the loader adds
534 /// them as it inserts, so the running total it checks against the budget and
535 /// the total a caller can compute are the same arithmetic rather than two
536 /// descriptions of it. `load_subgraph_totals_agree_with_the_derivation`
537 /// pins that they stay equal.
538 fn node_bytes(id: &str, d: &NodeData) -> usize {
539 id.len()
540 + d.title.len()
541 + d.content.as_ref().map_or(0, String::len)
542 + d.embedding_model.as_ref().map_or(0, String::len)
543 + d.valid_from.len()
544 + d.valid_to.len()
545 + std::mem::size_of::<NodeData>()
546 }
547
548 /// Estimated payload bytes for one adjacency entry.
549 ///
550 /// An edge occupies two of these — one in `out_adj`, one in `in_adj` — so a
551 /// caller accounting for a newly added edge counts it twice.
552 /// **24 bytes, and nothing else** since B2 (D-115).
553 ///
554 /// Before interning this summed four string lengths as well, around 189
555 /// bytes for a ULID-keyed edge. The strings did not disappear — they moved
556 /// into the pool, where they are counted once per *distinct* value by
557 /// [`Interner::estimated_bytes`] rather than once per edge entry.
558 fn edge_bytes(_e: &EdgeRef) -> usize {
559 std::mem::size_of::<EdgeRef>()
560 }
561
562 /// Estimated heap footprint (D-007).
563 ///
564 /// Deliberately an estimate of the *payload*, not a precise `size_of` walk:
565 /// the budget exists to stop a dense neighbourhood exhausting memory, and a
566 /// figure that tracks string bytes and per-item overhead is accurate enough
567 /// for that.
568 ///
569 /// **O(V + E), and therefore not for use inside a loop over rows.** The
570 /// loader used to call this per row, which made loading O(E²): 500 edges in
571 /// 26 ms, 1,000 in 76 ms, 2,000 in 231 ms — time tripling for each doubling.
572 /// The byte budget is what bounds a load, and the budget *check* was the
573 /// thing that did not scale (D-047).
574 pub fn estimated_bytes(&self) -> usize {
575 let nodes: usize = self
576 .nodes
577 .iter()
578 .map(|(id, d)| Self::node_bytes(id, d))
579 .sum();
580 let edges: usize = self
581 .out_adj
582 .values()
583 .chain(self.in_adj.values())
584 .flat_map(|v| v.iter())
585 .map(Self::edge_bytes)
586 .sum();
587 nodes + edges + self.pool.estimated_bytes()
588 }
589
590 /// Write one derived result per node under `label` (§5.4, D-041).
591 ///
592 /// Goes through [`Database::write_analytics_annotations`], which chunks at
593 /// [`crate::connection::chunk_rows::ANNOTATIONS`] and sends on the
594 /// low-priority channel,
595 /// so a community assignment over a large subgraph cannot starve interactive
596 /// writes.
597 ///
598 /// Rows land in `analytics_annotations`, which carries no log trigger.
599 /// Before 0.5.4 this method built a `ConceptUpsert` per node and put the
600 /// value in `content`, so writing back a partition **overwrote every
601 /// annotated concept's document text** — and, because the write went through
602 /// the ledger, recorded each rerun of the algorithm as a fresh version of a
603 /// world that had not changed. The old doc comment defended that as "a
604 /// normal bitemporal write," which was true of the mechanism and false of
605 /// the intent: it is the right mechanism for a domain fact, and a community
606 /// label is not one.
607 ///
608 /// `values` is keyed by node id; nodes absent from it are not annotated.
609 pub async fn write_back_annotations(
610 &self,
611 db: &Database,
612 label: &str,
613 values: &BTreeMap<String, String>,
614 ) -> Result<usize> {
615 let rows: Vec<Annotation> = self
616 .nodes
617 .keys()
618 .filter_map(|id| {
619 values
620 .get(id)
621 .map(|value| Annotation::new(id.clone(), label, value.clone()))
622 })
623 .collect();
624
625 db.write_analytics_annotations(rows).await
626 }
627}
628
629impl Database {
630 /// Load the topology reachable from `start_node` within `max_hops` (§5.4).
631 ///
632 /// Runs on the read connection, so it cannot contend with the write actor.
633 /// `byte_budget` bounds the result: a hub node in a dense graph can reach
634 /// most of the database in three hops, and the budget is what turns that
635 /// into [`DbError::SubgraphTooLarge`] rather than into an allocation
636 /// failure.
637 ///
638 /// Unfiltered: every edge type, **every weight**. See
639 /// [`Self::load_subgraph_with`] for the filtered form, which this delegates
640 /// to.
641 ///
642 /// `min_weight` is `NEG_INFINITY` rather than
643 /// [`TraversalBuilder`](super::TraversalBuilder)'s default
644 /// of `0.0`, and the difference is load-bearing. A floor of `0.0` silently
645 /// drops negative-weight edges — which is precisely the input
646 /// [`DbError::NegativeEdgeWeight`] exists to *report*, since Dijkstra and A*
647 /// are unsound over them and D-039 chose to refuse at the boundary rather
648 /// than return a shortest path that is merely a path. Delegating with the
649 /// builder default turned that typed refusal into a graph quietly missing
650 /// edges; `a_negative_edge_weight_is_refused_at_load` caught it.
651 ///
652 /// So the two mechanisms are made to agree instead of overlapping: an edge a
653 /// caller has **not** filtered out reaches the weight guard, and an edge they
654 /// have is theirs to exclude. See [`Self::load_subgraph_with`] for what that
655 /// means when a caller passes a default builder.
656 pub async fn load_subgraph(
657 &self,
658 start_node: &str,
659 max_hops: u32,
660 now_ts: &str,
661 byte_budget: usize,
662 ) -> Result<Subgraph> {
663 self.load_subgraph_with(
664 &super::TraversalBuilder::new(start_node)
665 .max_depth(max_hops as usize)
666 .min_weight(f64::NEG_INFINITY),
667 now_ts,
668 byte_budget,
669 )
670 .await
671 }
672
673 /// Load the topology a [`TraversalBuilder`](super::TraversalBuilder)
674 /// describes, as a [`Subgraph`]
675 /// (§5.4, D-073).
676 ///
677 /// `load_subgraph` took neither `edge_types` nor `min_weight` while
678 /// `TraversalBuilder` took both — the same walk over the same table with two
679 /// fewer knobs. That was a **reachability** limit rather than a convenience
680 /// one: the byte budget bounds the *unfiltered* neighbourhood, so a caller
681 /// wanting one edge type out of a hub got [`DbError::SubgraphTooLarge`] for a
682 /// graph whose filtered form would have fitted easily, and filtering the
683 /// returned `Subgraph` afterwards cannot help because the refusal happens
684 /// during the walk.
685 ///
686 /// # The filters apply to the walk *and* to the returned edges
687 ///
688 /// This is the decision the change turned on, and the two are separable.
689 /// `TraversalBuilder` applies its filters to the **recursive step** — which
690 /// edges are followed — while this loader's final projection returns every
691 /// edge of every node it reached. Wiring the two together naively gives a
692 /// caller who asked for `CITES` a graph reached via `CITES` and populated
693 /// with `KNOWS` edges as well, which is surprising enough to be read as a
694 /// bug.
695 ///
696 /// So both halves filter. If a caller names edge types or a minimum weight,
697 /// they are asking for a subgraph **of those edges**: the walk uses them to
698 /// bound which nodes are reached, and the projection uses them to decide
699 /// which adjacency lands in the result. `load_subgraph` passes a default
700 /// builder — no types, weight ≥ 0 — so its behaviour is unchanged.
701 ///
702 /// # `min_weight` and the negative-weight guard
703 ///
704 /// [`TraversalBuilder`](super::TraversalBuilder) defaults `min_weight` to
705 /// `0.0`, so a **default
706 /// builder passed here filters negative-weight edges out** rather than
707 /// letting them reach [`DbError::NegativeEdgeWeight`]. That is a real
708 /// difference from [`Self::load_subgraph`], which passes `NEG_INFINITY`.
709 ///
710 /// It is deliberate and it is the coherent reading: a caller who states a
711 /// weight floor has asked to exclude what falls below it, and excluding it
712 /// is not an error. A caller who states none should be told, because
713 /// Dijkstra and A* are unsound over negative weights. Pass
714 /// `.min_weight(f64::NEG_INFINITY)` to get the guard with a filtered builder.
715 ///
716 /// `attribute_mode` is ignored: hydration here is always the live concept
717 /// row, which is what a `Subgraph` has always carried.
718 pub async fn load_subgraph_with(
719 &self,
720 traversal: &super::TraversalBuilder,
721 now_ts: &str,
722 byte_budget: usize,
723 ) -> Result<Subgraph> {
724 let start_node = traversal.start_node.as_str();
725 let max_hops = traversal.max_depth as u32;
726 let conn = self.read_conn();
727 let mut graph = Subgraph::default();
728 // Running payload total, carried through the load and into `hydrate`.
729 // See `estimated_bytes` for why this is not recomputed per row (D-047).
730 let mut bytes = 0usize;
731
732 // `?1..?4` are start, depth, ts and min_weight; edge types take `?5`
733 // onwards. Bound, never spliced — an edge type is a value, and the only
734 // validation in the crate runs on the *write* path (D-039), so a
735 // traversal never passes through it.
736 let edge_filter = traversal.edge_filter_sql();
737
738 // Topology first. The recursion itself is `TraversalBuilder::walk_cte`
739 // and is **not** duplicated here (T0.1): this file and `builder.rs` held
740 // byte-identical copies, and they had already drifted once — D-073 found
741 // this loader taking neither `edge_types` nor `min_weight` while the
742 // builder took both.
743 let sql = format!(
744 "{}{}",
745 traversal.walk_cte(),
746 format_args!(
747 r#"
748-- **The `DISTINCT` is why this query is superlinear, and it is not removable.**
749--
750-- Wave 3 measured `load_subgraph` at 12.5x for 10x the nodes and could not say
751-- why; Wave 4 answered it from the plan. `EXPLAIN` reports
752-- `USE TEMP B-TREE FOR DISTINCT`: an O(E log E) sort over the output, and
753-- n log n predicts ~13.3x for 10x, against the 12.5x measured. That is the term.
754--
755-- It is load-bearing: two branches can reach the same node, so a node appears in
756-- `walk` at more than one depth and the join would otherwise emit its edges once
757-- per depth. Without `DISTINCT` a caller gets duplicate edges.
758--
759-- **Corrected in 0.6.0 (T0.1), and the correction is not that the analysis was
760-- wrong.** Everything above holds, and D-070's two rejected fixes were measured
761-- honestly. What was wrong was the fixture: `benches/` seeds a chain of stars,
762-- which is a *tree*, and in a tree there is exactly one path to each node — so
763-- the term that actually dominated was identically 1 and invisible. D-070
764-- concluded the growth was "inherent to producing a deduplicated result", which
765-- is true of trees and false of graphs. The real cost was the walk enumerating
766-- **paths** rather than nodes; see `walk_cte`. On a 328-edge layered graph at
767-- depth 6 that was 299,593 walk rows and 428 ms, against 49 rows and 0.1 ms now.
768-- The `DISTINCT` stays, and it is no longer the leading term.
769--
770-- The filters appear **twice**, and that is the contract (D-073). The walk uses
771-- them to bound which nodes are reached; the projection uses them to decide
772-- which adjacency lands in the result. Filtering only the walk would hand a
773-- caller who asked for `CITES` a graph reached via `CITES` and populated with
774-- every other edge type those nodes happen to have.
775SELECT DISTINCT l.source_id, l.target_id, l.edge_type, l.weight, l.valid_from, l.valid_to
776FROM walk w
777JOIN links_current l ON l.source_id = w.node_id
778WHERE l.valid_from <= ?3 AND ?3 < l.valid_to
779 AND l.weight >= ?4
780 {edge_filter}
781ORDER BY l.source_id, l.target_id, l.edge_type
782"#
783 )
784 );
785
786 let mut params: Vec<libsql::Value> = vec![
787 start_node.into(),
788 (max_hops as i64).into(),
789 now_ts.into(),
790 traversal.min_weight.into(),
791 ];
792 params.extend(traversal.edge_types.iter().map(|t| t.as_str().into()));
793
794 let mut rows = conn.query(&sql, params).await?;
795
796 while let Some(row) = rows.next().await? {
797 let source: String = row.get(0)?;
798 let target: String = row.get(1)?;
799 let weight: f64 = row.get(3)?;
800
801 // Dijkstra and A* are only correct for non-negative weights, and the
802 // schema does not constrain the column. Refusing here keeps the
803 // wrongness at the boundary: the alternative is a shortest path that
804 // is merely a path, returned with no indication of it.
805 //
806 // **The `is_nan()` arm is unreachable on a file this schema created
807 // (T0.3, D-078).** SQLite stores a NaN double as NULL, so
808 // `weight REAL NOT NULL` refuses it — measured on libSQL 0.9.30
809 // through `assert_edge`, through a raw `INSERT` binding NaN, and
810 // through a raw `INSERT` computing `0.0/0.0` in the engine; all three
811 // fail with `NOT NULL constraint failed`. §4.7 used to list NaN as a
812 // gap this loader covered, which had it backwards.
813 //
814 // Kept anyway, as defence rather than decoration: a future engine
815 // that stores NaN as a real double would make it live again, and the
816 // cost of a comparison per edge against reading a shortest path
817 // computed over NaN is not a close call. `storage_boundary_tests`
818 // pins the engine's current behaviour, so that change would arrive
819 // as a failing test rather than as a silent answer.
820 if weight < 0.0 || weight.is_nan() {
821 return Err(DbError::NegativeEdgeWeight {
822 source_id: source,
823 target_id: target,
824 weight,
825 });
826 }
827
828 let edge_type: String = row.get(2)?;
829 let valid_from: String = row.get(4)?;
830 let valid_to: String = row.get(5)?;
831
832 // Accounted before the insert, and the arithmetic is far simpler
833 // than it was: an interned entry is a fixed 24 bytes whichever
834 // endpoint it names, so the two entries `add_edge` writes cost the
835 // same and there is no id-length asymmetry to get wrong.
836 //
837 // The strings have not vanished, they have moved into the pool, so
838 // what a *new* distinct string costs is charged here too. Only the
839 // ones actually new: `intern` dedupes, and charging every edge for
840 // its type and timestamps would re-introduce exactly the per-edge
841 // cost B2 removes.
842 bytes += graph.add_edge(&source, &target, &edge_type, weight, &valid_from, &valid_to);
843
844 if bytes > byte_budget {
845 return Err(DbError::SubgraphTooLarge {
846 n: bytes,
847 budget: byte_budget,
848 });
849 }
850 }
851
852 // Every endpoint is a node, plus the start itself so a lone node still
853 // loads as a one-node graph rather than an empty one.
854 let mut ids: Vec<String> = graph
855 .out_adj
856 .keys()
857 .chain(graph.in_adj.keys())
858 .cloned()
859 .collect();
860 ids.push(start_node.to_string());
861 ids.sort();
862 ids.dedup();
863
864 hydrate(conn, &mut graph, &ids, bytes, byte_budget, traversal.content).await?;
865 graph.drop_dangling_adjacency();
866 Ok(graph)
867 }
868}
869
870use crate::util::limits::HYDRATE_CHUNK;
871
872/// Fill in `nodes` from `concepts` for the ids the topology touched.
873/// Attach node attributes, continuing the caller's byte accounting.
874///
875/// `bytes_so_far` is the topology's payload total; this adds each node as it
876/// lands and refuses as soon as the running total passes the budget rather than
877/// after the whole set is in hand. Checking once at the end would allocate the
878/// whole oversized result before declining to return it, which is the failure
879/// the budget exists to prevent rather than to report.
880///
881/// **One query per [`HYDRATE_CHUNK`] ids, not one per node (defect AE).** The
882/// previous version issued a round trip per id: 400 nodes cost 400 of them and
883/// 13.2 ms, essentially all of it latency rather than work, and linear in node
884/// count on a path whose whole purpose is to bound the result by *bytes*.
885async fn hydrate(
886 conn: &libsql::Connection,
887 graph: &mut Subgraph,
888 ids: &[String],
889 bytes_so_far: usize,
890 byte_budget: usize,
891 with_content: bool,
892) -> Result<()> {
893 let mut bytes = bytes_so_far;
894
895 for chunk in ids.chunks(HYDRATE_CHUNK) {
896 // Only the placeholders are built; the ids themselves are bound.
897 let list = (1..=chunk.len())
898 .map(|i| format!("?{i}"))
899 .collect::<Vec<_>>()
900 .join(", ");
901 let sql = format!(
902 "SELECT id, title, content, embedding_model, valid_from, valid_to \
903 FROM concepts WHERE retired = 0 AND id IN ({list})"
904 );
905 let params: Vec<libsql::Value> = chunk
906 .iter()
907 .map(|id| libsql::Value::Text(id.clone()))
908 .collect();
909
910 let mut rows = conn.query(&sql, params).await?;
911 while let Some(row) = rows.next().await? {
912 let id: String = row.get(0)?;
913 let data = NodeData {
914 title: row.get(1)?,
915 content: if with_content { row.get(2).ok() } else { None },
916 embedding_model: row.get(3).ok(),
917 valid_from: row.get(4)?,
918 valid_to: row.get(5)?,
919 };
920 bytes += Subgraph::node_bytes(&id, &data);
921 graph.nodes.insert(id, data);
922
923 if bytes > byte_budget {
924 return Err(DbError::SubgraphTooLarge {
925 n: bytes,
926 budget: byte_budget,
927 });
928 }
929 }
930 }
931
932 Ok(())
933}
934
935#[cfg(test)]
936mod tests {
937 use super::*;
938
939 #[test]
940 fn adding_an_edge_indexes_it_in_both_directions() {
941 let mut g = Subgraph::default();
942 g.add_edge("A", "B", "KNOWS", 0.5, "2026-01-01T00:00:00.000000Z", "9999-12-31T23:59:59.999999Z");
943
944 assert_eq!(g.out_edges("A").len(), 1);
945 assert_eq!(g.out_edges("A")[0].node(&g), "B");
946 assert_eq!(g.in_edges("B").len(), 1);
947 assert_eq!(g.in_edges("B")[0].node(&g), "A", "in_adj holds the source");
948
949 // The undirected view has to agree with itself: total degree is twice
950 // the edge weight total, which is the identity every undirected
951 // quantity in `algorithms` is derived from.
952 assert_eq!(g.degree("A") + g.degree("B"), 2);
953 assert_eq!(g.weighted_degree("A") + g.weighted_degree("B"), 1.0);
954 assert_eq!(g.total_weight(), 0.5);
955 }
956
957 #[test]
958 fn a_missing_node_has_no_edges_rather_than_panicking() {
959 let g = Subgraph::default();
960 assert!(g.out_edges("nobody").is_empty());
961 assert!(g.in_edges("nobody").is_empty());
962 assert_eq!(g.degree("nobody"), 0);
963 assert_eq!(g.weighted_degree("nobody"), 0.0);
964 }
965}