Public repository: timo / feel

Reader

An interpreter of FEEL, written in Rust, with integrations for PostgreSQL, WASM, Lua, and Python.

docs(adr): add ADR-46 for descendant expression `e ... name` (DMN §10.3.2.7)

public

docs(adr): add ADR-46 for descendant expression `e ... name` (DMN §10.3.2.7)

Documents the architectural decision to implement the descendant expression operator
`e ... name` with cycle-safe unbounded traversal across all language bindings.
Includes rationale for the five-part change: lexer token, grammar production,
AST/evaluation logic, cycle detection via `ForeignObject::identity()`, and
host-specific identity overrides for feel-py, feel-lua, feel-wasm, and feel-pg.
Changeset45fc1c031ced46cea2b83f3fc8fbef74b59391f7
Author (claimed)Timo Stolz <timo.stolz@gmail.com>,
Parentsa4a29f172176
Children
Pushlog
by timo via ssh

modified DECISIONS.md +62 −0

@@ -2170,3 +2170,65 @@
21702170
   `0034-drg-scopes` (compliance-level-3) — 2 cases, `exact`;
21712171
   `0070-feel-instance-of` (compliance-level-3) — 129 cases, `exact`.
21722172
   **Suite 3003 → 3145.**
2173
+
2174
+## ADR-46: Descendant expression `e ... name` + `ForeignObject::identity` (DMN §10.3.2.7, rule 68)
2175
+
2176
+- **Context:** Grammar rule 68 (`descendant expression = expression, "...", name`) was the
2177
+  one spec feature surfaced by reading the grammar rather than the TCK — no DMN-TCK case
2178
+  (level 2 or 3) exercises it, so it was documented as coverage gap #14. Semantics
2179
+  (§10.3.2.7, Table 68): where `e` is a context, `e ... name` returns a **list** of every
2180
+  value bound to `name` in `e` *and all nested contexts*, collected pre-order
2181
+  (`{a:{b:{b:1}}} ... b` → `[{b:1}, 1]`). The `.` operator already maps over a list of
2182
+  contexts; descendant recurses depth-first through the whole structure.
2183
+
2184
+  The design crux: descendant is the engine's **first unbounded collection traversal**.
2185
+  ADR-21's laziness-terminates guarantee (a written `.`-path is finite, so cyclic host
2186
+  graphs are harmless — `lib.rs` test `unbounded_graph_terminates_along_a_finite_path`)
2187
+  does **not** apply: descendant recurses through the whole reachable graph, so a cyclic
2188
+  host graph behind `Value::Foreign` would loop forever.
2189
+
2190
+- **Decision:** Five-part change.
2191
+
2192
+  1. **Lexer** — a new `...` token (`DotDotDot`). logos longest-match keeps it disjoint
2193
+     from `.`/`..`; no LR interaction.
2194
+  2. **Grammar** — one `Postfix` production `<e:Postfix> "..." <n:Name>` → `Expr::Descendant`,
2195
+     same precedence as the `.` path operator. `...` is a unique lookahead after a completed
2196
+     `Postfix`, so it is LR(1)-clean (the `build.rs` no-conflict gate is green).
2197
+  3. **AST/eval** — `Expr::Descendant(Box<Expr>, String)`; the evaluator's `descendant_collect`
2198
+     walks pre-order over `Context`/`List` (collect the match, then recurse into every
2199
+     child) and `Foreign` (force one level, recurse). A null/error target propagates; a
2200
+     non-navigable scalar is `null` (mirrors `.`); otherwise the result is a list (possibly
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
2203
+     `ForeignObject::identity() -> usize` trait method (default = the Rust allocation
2204
+     address). Only an `Arc`-backed `Value::Foreign` can form a cycle (pure-Rust values are
2205
+     owned trees), and **only** `Foreign` nodes are keyed in the set, so the guard is a no-op
2206
+     (empty set) for a pure-Rust embedder. The set stores plain integers — it owns nothing,
2207
+     cannot extend any object's lifetime, and is dropped at scope exit (RAII; the workspace
2208
+     uses `panic = unwind`, so also on panic): no leak.
2209
+  5. **Per-host identity overrides** — the default allocation address is sound only when one
2210
+     `Arc` maps to one host object. Every host wrapper mints a *fresh* `Arc` per wrap
2211
+     (`shallow_convert*` → `Arc::new` re-run on every `force`/`get`), so two wrappers around
2212
+     the *same* host object have distinct allocations. The correct identity is host-specific:
2213
+     - **feel-py** — `self.obj.as_ptr()` (= Python `id()`, stable across `Py::clone`, no GIL).
2214
+     - **feel-lua** — `self.table.to_pointer()` (mlua's Lua table pointer; stable across
2215
+       `Table::clone`).
2216
+     - **feel-wasm** — a process-wide `WeakMap<object, number>` (`js_identity`): JS exposes
2217
+       no cheap object address and a `JsValue` handle index is *not* object identity across
2218
+       different reads, so a WeakMap assigns each distinct object a stable integer. Its keys
2219
+       are weakly held → reclaimed by GC, so it neither pins objects nor grows unboundedly.
2220
+     - **feel-pg** — **exempt**: `jsonb` is a serialized value tree, so a `JsonbForeign`
2221
+       sub-graph is definitionally acyclic; no override, no cycle test.
2222
+
2223
+     This keeps feel-core's guard a plain `HashSet<usize>` (target-agnostic) while pushing
2224
+     "what is the same object" into each host — symmetric with the existing
2225
+     `ForeignObject::as_any` output-identity seam (ADR-21).
2226
+
2227
+- **Consequence:** The last spec grammar rule (68) is implemented; the engine's FEEL slice
2228
+  is now rule-1–68 complete (modulo the DRG-only out-of-scope forms). No TCK case exists, so
2229
+  the fixture suite is unchanged (still 3145 cases / 110 files). Tests added: feel-core
2230
+  (`foreign_tests`, incl. two cyclic-Foreign shapes — a fresh-`Arc`-per-access `Cycle` keyed
2231
+  on `id` and a single-shared-`Arc` `SharedCycle` keyed on the allocation address), feel-py
2232
+  (cyclic dict + cyclic class instance), feel-lua (cyclic table), feel-wasm (cyclic object),
2233
+  feel-pg (jsonb tree — terminates by construction). All non-pg suites pass; feel-pg is
2234
+  exercised via `cargo pgx test` (the pgrx harness, not runnable without a managed Postgres).