Skip to content

Commit f79b64d

Browse files
committed
Enhanced graph reasoning to handle RelayTo target validation. Updated error handling for non-existent nodes and added corresponding unit test.
Signed-off-by: Marvin Hansen <[email protected]>
1 parent 75735ff commit f79b64d

File tree

2 files changed

+56
-2
lines changed

2 files changed

+56
-2
lines changed

deep_causality/src/traits/causable_graph/graph_reasoning/mod.rs

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,7 @@ where
7171
/// * `Ok(PropagatingEffect)`: The final `PropagatingEffect` from the last successfully evaluated node
7272
/// in the main traversal path. `Deterministic(false)` now propagates and does not implicitly halt propagation.
7373
/// Only a `Causaloid` returning a `CausalityError` will abort the traversal.
74-
/// * `Err(CausalityError)` if the graph is not frozen, a node is missing, or an evaluation fails.
74+
/// * `Err(CausalityError)` if the graph is not frozen, a node is missing, a RelayTo target cannot be found or an evaluation fails.
7575
fn evaluate_subgraph_from_cause(
7676
&self,
7777
start_index: usize,
@@ -124,6 +124,14 @@ where
124124
// If a RelayTo effect is returned, clear the queue and add the target_index
125125
// with the inner_effect as the new starting point for traversal.
126126
queue.clear();
127+
128+
// Validate target_index before proceeding
129+
if !self.contains_causaloid(target_index) {
130+
return Err(CausalityError(format!(
131+
"RelayTo target causaloid with index {target_index} not found in graph."
132+
)));
133+
}
134+
127135
if !visited[target_index] {
128136
visited[target_index] = true;
129137
queue.push_back((target_index, *inner_effect));

deep_causality/tests/types/causal_types/causaloid_graph/causality_graph_reasoning_adaptive_tests.rs

Lines changed: 47 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
#![allow(clippy::too_many_arguments)]
2+
13
/*
24
* SPDX-License-Identifier: MIT
35
* Copyright (c) "2025" . The DeepCausality Authors and Contributors. All Rights Reserved.
@@ -22,7 +24,6 @@ fn test_evaluate_subgraph_from_cause_with_relay_to_simple() {
2224
|_effect: &PropagatingEffect| -> Result<PropagatingEffect, CausalityError> {
2325
Ok(PropagatingEffect::RelayTo(
2426
3,
25-
//
2627
Box::new(PropagatingEffect::Deterministic(false)),
2728
))
2829
};
@@ -189,3 +190,48 @@ fn test_evaluate_shortest_path_between_causes_with_relay_at_end() {
189190
PropagatingEffect::RelayTo(0, Box::new(PropagatingEffect::Numerical(50.0)))
190191
);
191192
}
193+
194+
#[test]
195+
fn test_evaluate_subgraph_from_cause_relay_to_non_existent_node() {
196+
// Graph: Root (0) -> A (1)
197+
// A will relay to a non-existent node (e.g., index 99)
198+
let mut g = CausaloidGraph::new(0);
199+
200+
let root_causaloid = test_utils::get_test_causaloid_deterministic_true();
201+
let root_index = g.add_root_causaloid(root_causaloid).unwrap();
202+
203+
// Causaloid A: Relays to a non-existent node (index 99)
204+
let non_existent_target_index = 124;
205+
let causaloid_a_id = 14;
206+
let causaloid_a_description = format!(
207+
"Causaloid A relays to non-existent node {}",
208+
non_existent_target_index
209+
);
210+
let causaloid_a_fn =
211+
|_effect: &PropagatingEffect| -> Result<PropagatingEffect, CausalityError> {
212+
Ok(PropagatingEffect::RelayTo(
213+
124, // non_existent_target_index
214+
Box::new(PropagatingEffect::Numerical(1.0)),
215+
))
216+
};
217+
218+
let causaloid_a = Causaloid::new(causaloid_a_id, causaloid_a_fn, &causaloid_a_description);
219+
let idx_a = g.add_causaloid(causaloid_a).unwrap();
220+
221+
// Link the graph: Root -> A
222+
g.add_edge(root_index, idx_a).unwrap();
223+
224+
g.freeze();
225+
226+
let initial_effect = PropagatingEffect::Deterministic(true);
227+
let res = g.evaluate_subgraph_from_cause(root_index, &initial_effect);
228+
229+
assert!(res.is_err());
230+
assert_eq!(
231+
res.unwrap_err().to_string(),
232+
format!(
233+
"CausalityError: RelayTo target causaloid with index {} not found in graph.",
234+
non_existent_target_index
235+
)
236+
);
237+
}

0 commit comments

Comments
 (0)