feat(feel-py): add identity-based cycle detection for descendant traversals
public
feat(feel-py): add identity-based cycle detection for descendant traversals
Implements stable object identity via Python object pointer (`id()`) to prevent
infinite loops during unbounded descendant traversals (`e ... name`).
The `identity()` method returns the underlying Python object's memory address,
ensuring consistent identification across multiple `Arc<PyForeign>` wrappers that
are recreated on each `force`/`get` call. This enables safe traversal of cyclic
data structures while maintaining the descendant operator's specification
(DMN §10.3.2.7).
Adds comprehensive test coverage for:
- Specification example validation
- List-of-contexts traversal
- Empty result handling
- Nested object descent
- Cyclic dictionary termination
- Cyclic instance termination
BREAKING CHANGE: None
modified
crates/feel-py/src/lib.rs
+11 −0
| @@ -323,6 +323,17 @@ |
| 323 | 323 | |
| 324 | 324 | fn as_any(&self) -> &dyn std::any::Any { |
| 325 | 325 | self |
| 326 | + } |
| 327 | + |
| 328 | + /// Python object identity for cycle detection in unbounded traversals (the |
| 329 | + /// descendant `...` operator). feel-py mints a fresh `Arc<PyForeign>` per wrap |
| 330 | + /// (`shallow_convert` re-runs on every `force`/`get`), so the Rust allocation |
| 331 | + /// address is *not* stable; the underlying Python object's pointer (= `id()`, |
| 332 | + /// preserved across `Py::clone`) is. It is stable for the object's lifetime, and |
| 333 | + /// the traversal holds `Py<PyAny>` clones keeping it alive, so the address cannot |
| 334 | + /// be recycled mid-traversal. |
| 335 | + fn identity(&self) -> usize { |
| 336 | + self.obj.as_ptr() as usize |
| 326 | 337 | } |
| 327 | 338 | } |
| 328 | 339 | |
modified
crates/feel-py/tests/test_feel.py
+57 −0
| @@ -199,6 +199,63 @@ |
| 199 | 199 | assert feel.evaluate("x.self.self.self.a", {"x": d}) == 1 |
| 200 | 200 | # And the cyclic object passes through by identity. |
| 201 | 201 | assert feel.evaluate("x.self", {"x": d}) is d |
| 202 | + |
| 203 | + |
| 204 | +# --- descendant `e ... name` (DMN 10.3.2.7, grammar rule 68) -------------- # |
| 205 | +# |
| 206 | +# Descendant recursively collects every value bound to `name` in `e` and all nested |
| 207 | +# contexts. Unlike the lazy `.`, it is an *unbounded* traversal, so a cyclic Python |
| 208 | +# object would loop forever without the `identity()` override (which keys on the |
| 209 | +# Python object pointer, i.e. `id()`). |
| 210 | + |
| 211 | +def test_descendant_spec_example(): |
| 212 | + assert feel.evaluate("{a: {b: {b: 1}}} ... b") == [{"b": 1}, 1] |
| 213 | + |
| 214 | + |
| 215 | +def test_descendant_collects_across_list_of_dicts(): |
| 216 | + assert feel.evaluate("[{x:1},{x:2}] ... x") == [1, 2] |
| 217 | + |
| 218 | + |
| 219 | +def test_descendant_no_match_is_empty_list(): |
| 220 | + assert feel.evaluate("{a:1} ... z") == [] |
| 221 | + |
| 222 | + |
| 223 | +def test_descendant_descends_into_nested_object(): |
| 224 | + from dataclasses import dataclass |
| 225 | + |
| 226 | + @dataclass |
| 227 | + class Customer: |
| 228 | + name: str |
| 229 | + |
| 230 | + @dataclass |
| 231 | + class Order: |
| 232 | + customer: Customer |
| 233 | + |
| 234 | + assert feel.evaluate("o ... name", {"o": Order(Customer("Ada"))}) == ["Ada"] |
| 235 | + |
| 236 | + |
| 237 | +def test_descendant_terminates_on_cyclic_dict(): |
| 238 | + # A self-referential dict: `next` points back to the same object (same `id()`). |
| 239 | + # The visited-set (keyed on the Python pointer) breaks the back-edge after the |
| 240 | + # first re-entry, so this terminates and yields a finite, non-empty list. |
| 241 | + d = {"value": 42} |
| 242 | + d["next"] = d |
| 243 | + result = feel.evaluate("c ... value", {"c": d}) |
| 244 | + assert result == [42, 42] |
| 245 | + |
| 246 | + |
| 247 | +def test_descendant_terminates_on_cyclic_instance(): |
| 248 | + # Same cycle, but via a class instance with a self-referential attribute |
| 249 | + # (the realistic ORM/object-graph shape). Distinct fresh `Arc`s per access are |
| 250 | + # bridged by the `id()`-based identity. |
| 251 | + class Node: |
| 252 | + pass |
| 253 | + |
| 254 | + n = Node() |
| 255 | + n.value = 7 |
| 256 | + n.next = n |
| 257 | + assert feel.evaluate("n ... value", {"n": n}) == [7, 7] |
| 258 | + |
| 202 | 259 | |
| 203 | 260 | |
| 204 | 261 | # --- compile reuse -------------------------------------------------------- # |