eidetic-engine 0.15.2

Durable, local-first, explainable memory for coding agents.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
use std::collections::{BTreeMap, HashMap, VecDeque};
use std::time::Duration;

use asupersync::Cx;
use fnx_classes::Graph;
use serde::{Serialize, Serializer};

use crate::core::degraded_aggregation::{
    AggregatedDegradation, DegradationAggregationInput, aggregate_degraded_entries,
};
use crate::graph::GraphResult;
use crate::graph::algorithms::{current_or_testing_cx, run_with_budget};
use crate::models::degradation::GRAPH_PROXIMITY_UNREACHABLE_CODE;

pub use crate::models::PROXIMITY_SCHEMA_V1;
pub const GOMORY_HU_WEIGHT_ATTR: &str = "weight";
const GOMORY_HU_BUILD_BUDGET: Duration = Duration::from_millis(10_000);

#[derive(Clone, Debug)]
pub struct GomoryHuTree {
    pub tree: Graph,
    query_index: TreeMinCutIndex,
    component_by_node: HashMap<String, usize>,
}

#[derive(Clone, Debug)]
struct TreeMinCutIndex {
    node_index: HashMap<String, usize>,
    component_by_index: Vec<usize>,
    depth_by_index: Vec<usize>,
    ancestor_by_power: Vec<Vec<usize>>,
    min_cut_by_power: Vec<Vec<f64>>,
}

#[derive(Clone, Debug, PartialEq, Serialize)]
#[serde(rename_all = "camelCase")]
pub struct ProximityReport {
    pub schema: &'static str,
    pub memory_a: String,
    pub memory_b: String,
    pub snapshot_version: u64,
    pub min_cut: Option<f64>,
    pub interpretation: String,
    pub tree_path: Option<Vec<String>>,
    #[serde(serialize_with = "serialize_proximity_degraded")]
    pub degraded: Vec<ProximityDegradation>,
}

#[derive(Clone, Debug, Eq, PartialEq, Serialize)]
#[serde(rename_all = "camelCase")]
pub struct ProximityDegradation {
    pub code: String,
    pub severity: String,
    pub message: String,
    pub repair: Option<String>,
}

fn serialize_proximity_degraded<S>(
    degraded: &[ProximityDegradation],
    serializer: S,
) -> Result<S::Ok, S::Error>
where
    S: Serializer,
{
    aggregate_proximity_degraded(degraded).serialize(serializer)
}

fn aggregate_proximity_degraded(degraded: &[ProximityDegradation]) -> Vec<AggregatedDegradation> {
    aggregate_degraded_entries(degraded.iter().map(|entry| {
        DegradationAggregationInput::new(
            "gomory_hu_proximity",
            entry.code.clone(),
            entry.severity.clone(),
            entry.message.clone(),
            entry
                .repair
                .clone()
                .unwrap_or_else(|| "Refresh graph proximity diagnostics.".to_owned()),
        )
    }))
}

pub fn build_gomory_hu_tree(graph: &Graph) -> GraphResult<GomoryHuTree> {
    let cx = current_or_testing_cx();
    build_gomory_hu_tree_with_cx(&cx, graph)
}

pub fn build_gomory_hu_tree_with_cx<Caps>(
    cx: &Cx<Caps>,
    graph: &Graph,
) -> GraphResult<GomoryHuTree> {
    let graph = graph.clone();
    run_with_budget(cx, "gomory_hu_tree", GOMORY_HU_BUILD_BUDGET, move || {
        let tree = fnx_algorithms::gomory_hu_tree(&graph, GOMORY_HU_WEIGHT_ATTR);
        let query_index = TreeMinCutIndex::new(&tree);
        let component_by_node = graph_components(&graph);
        GomoryHuTree {
            tree,
            query_index,
            component_by_node,
        }
    })
}

#[must_use]
pub fn query_min_cut(tree: &GomoryHuTree, left: &str, right: &str) -> Option<f64> {
    if !tree.tree.has_node(left) || !tree.tree.has_node(right) {
        return None;
    }
    if left == right {
        return Some(0.0);
    }
    tree.query_index.query_min_cut(left, right)
}

#[must_use]
pub fn query_proximity(
    tree: &GomoryHuTree,
    left: &str,
    right: &str,
    snapshot_version: u64,
) -> ProximityReport {
    if !tree.tree.has_node(left) || !tree.tree.has_node(right) {
        return ProximityReport {
            schema: PROXIMITY_SCHEMA_V1,
            memory_a: left.to_owned(),
            memory_b: right.to_owned(),
            snapshot_version,
            min_cut: None,
            interpretation: "missing_memory".to_owned(),
            tree_path: None,
            degraded: Vec::new(),
        };
    }

    if !same_original_component(tree, left, right) {
        return ProximityReport {
            schema: PROXIMITY_SCHEMA_V1,
            memory_a: left.to_owned(),
            memory_b: right.to_owned(),
            snapshot_version,
            min_cut: None,
            interpretation: "unreachable".to_owned(),
            tree_path: None,
            degraded: vec![proximity_unreachable_degradation(left, right)],
        };
    }

    let min_cut = query_min_cut(tree, left, right);
    ProximityReport {
        schema: PROXIMITY_SCHEMA_V1,
        memory_a: left.to_owned(),
        memory_b: right.to_owned(),
        snapshot_version,
        min_cut,
        interpretation: proximity_interpretation(left == right, min_cut).to_owned(),
        tree_path: tree_path_nodes(&tree.tree, left, right),
        degraded: Vec::new(),
    }
}

impl TreeMinCutIndex {
    fn new(tree: &Graph) -> Self {
        let nodes = tree
            .nodes_ordered()
            .into_iter()
            .map(ToOwned::to_owned)
            .collect::<Vec<_>>();
        let node_index = nodes
            .iter()
            .enumerate()
            .map(|(index, node)| (node.clone(), index))
            .collect::<HashMap<_, _>>();
        let node_count = nodes.len();
        let level_count = binary_lifting_level_count(node_count);
        let mut component_by_index = vec![usize::MAX; node_count];
        let mut depth_by_index = vec![0; node_count];
        let mut ancestor_by_power = vec![vec![0; node_count]; level_count];
        let mut min_cut_by_power = vec![vec![f64::INFINITY; node_count]; level_count];
        let mut component_index = 0;

        for root_index in 0..node_count {
            if component_by_index[root_index] != usize::MAX {
                continue;
            }
            component_by_index[root_index] = component_index;
            ancestor_by_power[0][root_index] = root_index;
            let mut queue = VecDeque::from([root_index]);

            while let Some(current_index) = queue.pop_front() {
                let current = &nodes[current_index];
                let Some(neighbors) = tree.neighbors_iter(current) else {
                    continue;
                };
                for neighbor in neighbors {
                    let Some(&neighbor_index) = node_index.get(neighbor) else {
                        continue;
                    };
                    if component_by_index[neighbor_index] != usize::MAX {
                        continue;
                    }
                    component_by_index[neighbor_index] = component_index;
                    depth_by_index[neighbor_index] = depth_by_index[current_index] + 1;
                    ancestor_by_power[0][neighbor_index] = current_index;
                    min_cut_by_power[0][neighbor_index] =
                        gomory_tree_edge_weight(tree, current, neighbor);
                    queue.push_back(neighbor_index);
                }
            }

            component_index += 1;
        }

        for level in 1..level_count {
            for node in 0..node_count {
                let mid_ancestor = ancestor_by_power[level - 1][node];
                ancestor_by_power[level][node] = ancestor_by_power[level - 1][mid_ancestor];
                min_cut_by_power[level][node] = min_cut_by_power[level - 1][node]
                    .min(min_cut_by_power[level - 1][mid_ancestor]);
            }
        }

        Self {
            node_index,
            component_by_index,
            depth_by_index,
            ancestor_by_power,
            min_cut_by_power,
        }
    }

    fn query_min_cut(&self, left: &str, right: &str) -> Option<f64> {
        let mut left_index = *self.node_index.get(left)?;
        let mut right_index = *self.node_index.get(right)?;
        if self.component_by_index[left_index] != self.component_by_index[right_index] {
            return None;
        }

        let mut min_cut = f64::INFINITY;
        if self.depth_by_index[left_index] < self.depth_by_index[right_index] {
            std::mem::swap(&mut left_index, &mut right_index);
        }

        let depth_delta = self.depth_by_index[left_index] - self.depth_by_index[right_index];
        for level in 0..self.ancestor_by_power.len() {
            if (depth_delta & (1_usize << level)) == 0 {
                continue;
            }
            min_cut = min_cut.min(self.min_cut_by_power[level][left_index]);
            left_index = self.ancestor_by_power[level][left_index];
        }

        if left_index == right_index {
            return Some(finite_path_min_cut(min_cut));
        }

        for level in (0..self.ancestor_by_power.len()).rev() {
            if self.ancestor_by_power[level][left_index]
                == self.ancestor_by_power[level][right_index]
            {
                continue;
            }
            min_cut = min_cut.min(self.min_cut_by_power[level][left_index]);
            min_cut = min_cut.min(self.min_cut_by_power[level][right_index]);
            left_index = self.ancestor_by_power[level][left_index];
            right_index = self.ancestor_by_power[level][right_index];
        }

        min_cut = min_cut.min(self.min_cut_by_power[0][left_index]);
        min_cut = min_cut.min(self.min_cut_by_power[0][right_index]);
        Some(finite_path_min_cut(min_cut))
    }
}

fn binary_lifting_level_count(node_count: usize) -> usize {
    usize::BITS
        .saturating_sub(node_count.max(1).leading_zeros())
        .max(1) as usize
}

fn finite_path_min_cut(min_cut: f64) -> f64 {
    if min_cut.is_finite() { min_cut } else { 0.0 }
}

fn graph_components(graph: &Graph) -> HashMap<String, usize> {
    let mut components = HashMap::new();
    let mut component_index = 0;
    for node in graph.nodes_ordered() {
        if components.contains_key(node) {
            continue;
        }

        let mut queue = VecDeque::from([node.to_owned()]);
        while let Some(current) = queue.pop_front() {
            if components
                .insert(current.clone(), component_index)
                .is_some()
            {
                continue;
            }
            let Some(neighbors) = graph.neighbors_iter(&current) else {
                continue;
            };
            for neighbor in neighbors {
                if !components.contains_key(neighbor) {
                    queue.push_back(neighbor.to_owned());
                }
            }
        }
        component_index += 1;
    }
    components
}

fn same_original_component(tree: &GomoryHuTree, left: &str, right: &str) -> bool {
    tree.component_by_node
        .get(left)
        .is_some_and(|left_component| {
            tree.component_by_node
                .get(right)
                .is_some_and(|right_component| left_component == right_component)
        })
}

fn proximity_unreachable_degradation(left: &str, right: &str) -> ProximityDegradation {
    ProximityDegradation {
        code: GRAPH_PROXIMITY_UNREACHABLE_CODE.to_owned(),
        severity: "info".to_owned(),
        message: format!(
            "Proximity query endpoints {left} and {right} are unreachable across different components."
        ),
        repair: None,
    }
}

fn proximity_interpretation(is_self: bool, min_cut: Option<f64>) -> &'static str {
    if is_self {
        return "self";
    }
    match min_cut {
        None => "unavailable",
        Some(cut) if cut < 1.0 => "weak",
        Some(cut) if cut < 3.0 => "moderate",
        Some(_) => "strong",
    }
}

fn tree_path_nodes(tree: &Graph, left: &str, right: &str) -> Option<Vec<String>> {
    if !tree.has_node(left) || !tree.has_node(right) {
        return None;
    }
    if left == right {
        return Some(vec![left.to_owned()]);
    }

    let mut queue = VecDeque::from([left.to_owned()]);
    let mut predecessor: BTreeMap<String, Option<String>> =
        BTreeMap::from([(left.to_owned(), None)]);

    while let Some(node) = queue.pop_front() {
        let Some(neighbors) = tree.neighbors_iter(&node) else {
            continue;
        };
        for neighbor in neighbors {
            if predecessor.contains_key(neighbor) {
                continue;
            }
            predecessor.insert(neighbor.to_owned(), Some(node.clone()));
            if neighbor == right {
                return reconstruct_tree_path(&predecessor, right);
            }
            queue.push_back(neighbor.to_owned());
        }
    }

    None
}

fn reconstruct_tree_path(
    predecessor: &BTreeMap<String, Option<String>>,
    right: &str,
) -> Option<Vec<String>> {
    let mut path = Vec::new();
    let mut current = right.to_owned();
    loop {
        path.push(current.clone());
        match predecessor.get(&current)? {
            Some(parent) => current = parent.clone(),
            None => break,
        }
    }
    path.reverse();
    Some(path)
}

fn gomory_tree_edge_weight(tree: &Graph, left: &str, right: &str) -> f64 {
    tree.edge_attrs(left, right)
        .and_then(|attrs| attrs.get(GOMORY_HU_WEIGHT_ATTR))
        .and_then(fnx_runtime::CgseValue::as_f64)
        .filter(|weight| weight.is_finite() && *weight >= 0.0)
        .unwrap_or(0.0)
}

#[cfg(test)]
mod tests {
    use super::*;
    use fnx_classes::AttrMap;
    use fnx_runtime::CgseValue;

    type TestResult = Result<(), String>;

    fn graph_result<T>(result: GraphResult<T>) -> Result<T, String> {
        result.map_err(|error| error.to_string())
    }

    fn weighted_edge(weight: f64) -> AttrMap {
        let mut attrs = AttrMap::new();
        attrs.insert(GOMORY_HU_WEIGHT_ATTR.to_owned(), CgseValue::Float(weight));
        attrs
    }

    fn add_weighted_edge(graph: &mut Graph, left: &str, right: &str, weight: f64) {
        let result = graph.add_edge_with_attrs(left, right, weighted_edge(weight));
        assert!(
            result.is_ok(),
            "test graph edge should be valid: {result:?}"
        );
    }

    #[test]
    fn gomory_hu_connected_path_uses_path_bottleneck() -> TestResult {
        let mut graph = Graph::strict();
        add_weighted_edge(&mut graph, "a", "b", 3.0);
        add_weighted_edge(&mut graph, "b", "c", 2.0);

        let tree = graph_result(build_gomory_hu_tree(&graph))?;

        assert_eq!(query_min_cut(&tree, "a", "c"), Some(2.0));
        assert_eq!(query_min_cut(&tree, "c", "a"), Some(2.0));
        Ok(())
    }

    #[test]
    fn gomory_hu_disconnected_graph_reports_zero_cut() -> TestResult {
        let mut graph = Graph::strict();
        graph.add_node("a");
        graph.add_node("b");

        let tree = graph_result(build_gomory_hu_tree(&graph))?;

        assert_eq!(query_min_cut(&tree, "a", "b"), Some(0.0));
        Ok(())
    }

    #[test]
    fn proximity_report_marks_different_components_unreachable() -> TestResult {
        let mut graph = Graph::strict();
        graph.add_node("a");
        graph.add_node("b");

        let tree = graph_result(build_gomory_hu_tree(&graph))?;
        let report = query_proximity(&tree, "a", "b", 7);

        assert_eq!(report.schema, PROXIMITY_SCHEMA_V1);
        assert_eq!(report.memory_a, "a");
        assert_eq!(report.memory_b, "b");
        assert_eq!(report.snapshot_version, 7);
        assert_eq!(report.min_cut, None);
        assert_eq!(report.interpretation, "unreachable");
        assert_eq!(report.tree_path, None);
        assert_eq!(report.degraded.len(), 1);
        let degraded = &report.degraded[0];
        assert_eq!(degraded.code, GRAPH_PROXIMITY_UNREACHABLE_CODE);
        assert_eq!(degraded.severity, "info");
        assert!(degraded.message.contains("unreachable"));
        assert!(degraded.message.contains("different components"));
        assert_eq!(degraded.repair, None);
        Ok(())
    }

    #[test]
    fn proximity_report_serializes_aggregated_degraded_entries() -> TestResult {
        let report = ProximityReport {
            schema: PROXIMITY_SCHEMA_V1,
            memory_a: "a".to_owned(),
            memory_b: "b".to_owned(),
            snapshot_version: 7,
            min_cut: None,
            interpretation: "unreachable".to_owned(),
            tree_path: None,
            degraded: vec![
                proximity_unreachable_degradation("a", "b"),
                proximity_unreachable_degradation("a", "b"),
            ],
        };

        let value = serde_json::to_value(report).map_err(|error| error.to_string())?;
        let degraded = value
            .get("degraded")
            .and_then(serde_json::Value::as_array)
            .ok_or_else(|| {
                "serialized proximity report should include degraded array".to_owned()
            })?;

        assert_eq!(degraded.len(), 1);
        assert_eq!(
            degraded[0].get("code"),
            Some(&serde_json::json!(GRAPH_PROXIMITY_UNREACHABLE_CODE))
        );
        assert_eq!(
            degraded[0].get("severity"),
            Some(&serde_json::json!("info"))
        );
        assert_eq!(
            degraded[0].get("repair"),
            Some(&serde_json::json!("Refresh graph proximity diagnostics."))
        );
        assert_eq!(
            degraded[0].get("sources"),
            Some(&serde_json::json!(["gomory_hu_proximity"]))
        );
        Ok(())
    }

    #[test]
    fn gomory_hu_single_node_has_zero_self_cut() -> TestResult {
        let mut graph = Graph::strict();
        graph.add_node("a");

        let tree = graph_result(build_gomory_hu_tree(&graph))?;

        assert_eq!(tree.tree.node_count(), 1);
        assert_eq!(query_min_cut(&tree, "a", "a"), Some(0.0));
        assert_eq!(query_min_cut(&tree, "a", "missing"), None);
        Ok(())
    }

    #[test]
    fn proximity_report_marks_same_memory_as_self() -> TestResult {
        let mut graph = Graph::strict();
        graph.add_node("a");

        let tree = graph_result(build_gomory_hu_tree(&graph))?;
        let report = query_proximity(&tree, "a", "a", 11);

        assert_eq!(report.schema, PROXIMITY_SCHEMA_V1);
        assert_eq!(report.memory_a, "a");
        assert_eq!(report.memory_b, "a");
        assert_eq!(report.snapshot_version, 11);
        assert_eq!(report.min_cut, Some(0.0));
        assert_eq!(report.interpretation, "self");
        assert_eq!(report.tree_path, Some(vec!["a".to_owned()]));
        assert!(report.degraded.is_empty());
        Ok(())
    }

    #[test]
    fn proximity_zero_cut_between_distinct_memories_is_weak_not_self() -> TestResult {
        let mut graph = Graph::strict();
        add_weighted_edge(&mut graph, "a", "b", 0.0);

        let tree = graph_result(build_gomory_hu_tree(&graph))?;
        let report = query_proximity(&tree, "a", "b", 12);

        assert_eq!(report.min_cut, Some(0.0));
        assert_eq!(report.interpretation, "weak");
        assert_eq!(report.tree_path, Some(vec!["a".to_owned(), "b".to_owned()]));
        assert!(report.degraded.is_empty());
        Ok(())
    }

    #[test]
    fn gomory_hu_complete_graph_has_three_way_cut() -> TestResult {
        let mut graph = Graph::strict();
        for left in ["a", "b", "c", "d"] {
            for right in ["a", "b", "c", "d"] {
                if left < right {
                    add_weighted_edge(&mut graph, left, right, 1.0);
                }
            }
        }

        let tree = graph_result(build_gomory_hu_tree(&graph))?;

        assert_eq!(query_min_cut(&tree, "a", "d"), Some(3.0));
        assert_eq!(tree.tree.edge_count(), 3);
        Ok(())
    }

    #[test]
    fn gomory_hu_weighted_graph_preserves_min_cut_capacity() -> TestResult {
        let mut graph = Graph::strict();
        add_weighted_edge(&mut graph, "a", "b", 5.0);
        add_weighted_edge(&mut graph, "b", "c", 1.5);
        add_weighted_edge(&mut graph, "a", "c", 2.0);

        let tree = graph_result(build_gomory_hu_tree(&graph))?;

        assert_eq!(query_min_cut(&tree, "a", "b"), Some(6.5));
        assert_eq!(query_min_cut(&tree, "b", "c"), Some(3.5));
        Ok(())
    }

    #[test]
    fn gomory_hu_build_is_deterministic_across_three_runs() -> TestResult {
        let mut graph = Graph::strict();
        add_weighted_edge(&mut graph, "b", "c", 2.0);
        add_weighted_edge(&mut graph, "a", "b", 3.0);
        add_weighted_edge(&mut graph, "a", "c", 4.0);
        add_weighted_edge(&mut graph, "c", "d", 1.0);

        let first = graph_result(build_gomory_hu_tree(&graph))?;
        let second = graph_result(build_gomory_hu_tree(&graph))?;
        let third = graph_result(build_gomory_hu_tree(&graph))?;

        assert_eq!(first.tree.edges_ordered(), second.tree.edges_ordered());
        assert_eq!(second.tree.edges_ordered(), third.tree.edges_ordered());
        for left in graph.nodes_ordered() {
            for right in graph.nodes_ordered() {
                assert_eq!(
                    query_min_cut(&first, left, right),
                    query_min_cut(&second, left, right)
                );
                assert_eq!(
                    query_min_cut(&second, left, right),
                    query_min_cut(&third, left, right)
                );
            }
        }
        Ok(())
    }
}