Log
a367f30c9e54
test(feel-core): add descendant DAG de-duplication test
public
test(feel-core): add descendant DAG de-duplication test
Adds test verifying global visited-set de-duplicates shared acyclic host
sub-objects (diamond/DAG patterns) during descendant traversals. The test
demonstrates cycle-safety and exponential blowup prevention on diamond-heavy
DAGs by accepting per-path multiset semantics for shared host data.
Relates to ADR-46.
modified
DECISIONS.md
+13 −1
@@ -2199,13 +2199,25 @@
2199 2199 child) and `Foreign` (force one level, recurse). A null/error target propagates; a 2200 2200 non-navigable scalar is `null` (mirrors `.` ); otherwise the result is a list (possibly 2201 2201 empty). A `DESCENDANT_MAX_DEPTH = 1024` cap is a secondary backstop. 2202 - 4. **Cycle guard** — a `HashSet<usize>` visited-set keyed on a new 2202 + 4. **Cycle guard / DAG de-dup** — a `HashSet<usize>` visited-set keyed on a new 2203 2203 `ForeignObject::identity() -> usize` trait method (default = the Rust allocation 2204 2204 address). Only an `Arc` -backed `Value::Foreign` can form a cycle (pure-Rust values are 2205 2205 owned trees), and **only** `Foreign` nodes are keyed in the set, so the guard is a no-op 2206 2206 (empty set) for a pure-Rust embedder. The set stores plain integers — it owns nothing, 2207 2207 cannot extend any object's lifetime, and is dropped at scope exit (RAII; the workspace 2208 2208 uses `panic = unwind` , so also on panic): no leak. 2209 + 2210 + The set is **global** (threaded through the whole traversal, never cleared), so it does 2211 + double duty: it breaks cycles *and* de-duplicates shared *acyclic* host sub-objects (a 2212 + diamond/DAG reachable via two paths is collected once, not once per path). Pure-FEEL 2213 + contexts are owned value trees and never `Foreign` , so they are never keyed — the spec 2214 + domain keeps exact multiset semantics; only host-object sharing (out of DMN spec) is 2215 + de-duped. The alternative, a *path-scoped* set (insert on entry, remove on exit), would 2216 + match per-path multiset semantics and still terminate on cycles, but it risks 2217 + **exponential blowup** on diamond-heavy DAGs (a subgraph reachable via 2ᵏ paths is 2218 + traversed 2ᵏ times) — a real hazard for feel-py's ORM/object-graph target. The global set 2219 + is therefore the deliberate choice: trade per-path multiset semantics for shared host 2220 + data in exchange for cycle-safety *and* DAG-blow-up protection. 2209 2221 5. **Per-host identity overrides** — the default allocation address is sound only when one 2210 2222 `Arc` maps to one host object. Every host wrapper mints a *fresh* `Arc` per wrap 2211 2223 (`shallow_convert*` → `Arc::new` re-run on every `force` /`get` ), so two wrappers around
modified
crates/feel-core/src/eval.rs
+18 −7
@@ -1086,13 +1086,24 @@
1086 1086 /// collected *before* descending into its children, so 1087 1087 /// `{a:{b:{b:1}}} ... b` → `[{b:1}, 1]`. 1088 1088 /// 1089 -/// **Cycle guard.** Only an `Arc`-backed [`Value::Foreign`] can form a cycle 1090 -/// (pure-Rust values are owned trees). `seen` keys on 1091 -/// [`ForeignObject::identity`](crate::foreign::ForeignObject::identity) — the *host 1092 -/// object's* identity, not the Rust allocation address — so a host that mints a 1093 -/// fresh `Arc` per `force`/`get` (feel-py/lua/wasm) still has the back-edge broken 1094 -/// once its `identity()` override returns a stable value (feel-pg is exempt: 1095 -/// `jsonb` cannot cycle). [`DESCENDANT_MAX_DEPTH`] is a secondary backstop. 1089 +/// **Cycle guard / DAG de-dup.** `seen` is a *global* visited-set — threaded 1090 +/// through the whole traversal, never cleared — keyed on 1091 +/// [`ForeignObject::identity`](crate::foreign::ForeignObject::identity), the *host 1092 +/// object's* identity (not the Rust allocation address). Only an `Arc`-backed 1093 +/// [`Value::Foreign`] is keyed; pure-Rust values are owned trees and never 1094 +/// `Foreign`, so a pure-FEEL context is traversed in full (exact multiset 1095 +/// semantics, per spec). A host that mints a fresh `Arc` per `force`/`get` 1096 +/// (feel-py/lua/wasm) still has a back-edge broken once its `identity()` override 1097 +/// returns a stable value (feel-pg is exempt: `jsonb` cannot cycle). 1098 +/// 1099 +/// Because the set is global and never cleared, it also **de-duplicates shared 1100 +/// *acyclic* host sub-objects** (a diamond/DAG): a host object reachable via two 1101 +/// distinct paths is collected once, not once per path. Host object graphs are out 1102 +/// of DMN spec (spec defines descendant only over value-tree contexts), so there is 1103 +/// no spec answer; the global set is chosen over a path-scoped set (insert on entry, 1104 +/// remove on exit) to prevent *exponential blowup* on diamond-heavy DAGs, accepting 1105 +/// per-path multiset semantics for shared host data as the trade-off. See ADR-46. 1106 +/// [`DESCENDANT_MAX_DEPTH`] is a secondary backstop. 1096 1107 fn descendant_collect ( 1097 1108 root: & Value , 1098 1109 name: & str ,
modified
crates/feel-core/src/lib.rs
+28 −0
@@ -2440,6 +2440,34 @@
2440 2440 Value::List(vec! [Value::Number(9.0 ), Value::Number(9.0 )]) 2441 2441 ); 2442 2442 } 2443 + 2444 + #[ test ] 2445 + fn descendant_dedups_a_shared_acyclic_foreign_dag () { 2446 + // Pure-FEEL contrast first: two DISTINCT value contexts (owned, never 2447 + // Foreign) -> both traversed -> multiset [1, 1]. The spec domain is exact. 2448 + let ctx = Context::empty(); 2449 + assert_eq! ( 2450 + run(" {x:{v:1}, y:{v:1}} ... v " , & ctx), 2451 + Value::List(vec! [Value::Number(1.0 ), Value::Number(1.0 )]) 2452 + ); 2453 + 2454 + // Same shape but the inner object is ONE shared host object (a diamond/DAG, 2455 + // not a cycle): both `x` and `y` reference the same `a` (same Foreign 2456 + // identity). The GLOBAL visited-set collects `v` once, not once per path — 2457 + // the intentional DAG de-dup (ADR-46). Host object graphs are out of DMN 2458 + // spec; the global set is chosen over a path-scoped set to avoid exponential 2459 + // blowup on diamond-heavy DAGs, accepting per-path multiset semantics for 2460 + // shared host data. 2461 + let (gets, forces) = (Arc::new(AtomicUsize::new(0 )), Arc::new(AtomicUsize::new(0 ))); 2462 + let a = obj(& [(" v " , Value::Number(1.0 ))], & gets, & forces); 2463 + let g = obj(& [(" x " , a.clone()), (" y " , a.clone())], & gets, & forces); 2464 + let mut ctx = Context::empty(); 2465 + ctx.insert(" g " , g); 2466 + assert_eq! ( 2467 + run(" g ... v " , & ctx), 2468 + Value::List(vec! [Value::Number(1.0 )]) 2469 + ); 2470 + } 2443 2471 } 2444 2472 2445 2473 /// User-defined functions: native FEEL closures and host callables (ADR-23).