Public repository: timo / feel

Reader

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

docs: record ADR-21 (lazy Value::Foreign), flip RD-6, update README

public

docs: record ADR-21 (lazy Value::Foreign), flip RD-6, update README

Add ADR-21 (the lazy host-value seam) after ADR-20, note that it supersedes ADR-20's
eager boundary conversion and cycle guards, rewrite RD-6 to its flipped (lazy) state,
and update the feel-py README section to describe on-demand attribute resolution,
identity pass-through, and the absence of a cycle guard in lazy mode.

Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
Changeset60e90496826b9de1b5bd9c8de913da252b0152c5
Author (claimed)Timo Stolz <timo.stolz@gmail.com>,
Parentsff653f7c0c03
Children
Pushlog
by timo via ssh

modified DECISIONS.md +71 −26

@@ -526,6 +526,53 @@
526526
   cycle/depth guard feel-py uses: its recursion was previously unbounded, so a cyclic
527527
   JS object (`a.b = a`, legal in JS) could overflow the stack; a back-edge now maps to
528528
   `null` (no other behaviour change — own-enumerable getters still fire as before).
529
+  *(The eager boundary conversion this describes — and both cycle guards — were
530
+  superseded by **ADR-21**, which makes context mapping lazy.)*
531
+
532
+## ADR-21: Lazy host values (`Value::Foreign`, the `ForeignObject` seam)
533
+
534
+- **Context:** The wrappers (feel-py, feel-wasm) converted the *whole* host object
535
+  graph into a materialised `Value` tree **eagerly** at the boundary (RD-6). That
536
+  walks branches the expression never touches, snapshots data instead of reflecting
537
+  the live object, and needs an up-front cycle/depth guard. The recurring use case is
538
+  an ORM row or a deep object where `order.customer.address` should resolve one hop at
539
+  a time, loading relations on demand. RD-6 had rejected the lazy alternative, fearing
540
+  it would duck-type the evaluator and contaminate all targets; on revisiting, the
541
+  seam turned out far cleaner, so RD-6 is flipped.
542
+- **Decision (2026-06-17):** add a target-agnostic, std-only trait
543
+  `ForeignObject { get(key) -> Value; force() -> Value; as_any() }` and a
544
+  `Value::Foreign(Foreign)` variant, where `Foreign(Arc<dyn ForeignObject>)` is a
545
+  newtype that hand-implements `Debug`/`Clone`/`PartialEq` (the last as `Arc::ptr_eq`)
546
+  so `Value` keeps its derives. feel-core never constructs a `Foreign`; only the
547
+  wrappers do. **The rule:** the FEEL dot (`Expr::Path`) resolves a `Foreign` lazily,
548
+  by name, via `get` — the *one* place it is not forced; every other consumption
549
+  (arithmetic, comparison, three-valued logic, `if`, `in`/`between`, iteration,
550
+  builtin arguments) first calls `Value::forced`, an `#[inline]` tag test that
551
+  materialises one level via `force`. `get` uses host *attribute* access
552
+  (`getattr` / `Reflect.get`), so it fires `@property`/accessor getters — ORM
553
+  relations load exactly when the expression names them; `force` enumerates own
554
+  *instance data* only (no blanket property sweep). A `Foreign` that reaches the
555
+  result **passes through**: the wrapper recovers the original host object via
556
+  `as_any` and returns it untouched (identity, no re-traversal). Operators keep FEEL
557
+  semantics by construction — `force` only *converts* a host value into a `Value`,
558
+  then FEEL's own arithmetic/comparison runs; the host's `__add__`/`+` is never
559
+  invoked. The boundary sub-decision is **RD-6** (now flipped).
560
+- **Consequence:** lazy is the default mapping in both wrappers, replacing the eager
561
+  RD-6 conversion. Pure-Rust embedders are unaffected and pay nothing: `Value` does
562
+  not grow (the variant is a 16-byte fat pointer, under the 32-byte temporal variant —
563
+  pinned by a size test), and the `forced` checks fall through with a single predicted
564
+  branch when no `Foreign` exists. ADR-1 holds — the trait is std-only, like the
565
+  `FeelRegex` seam (ADR-16) — and feel-pg, which never builds a `Foreign`, gains one
566
+  defensive, unreachable match arm. Cyclic graphs need **no** engine guard: a finite
567
+  expression navigates a finite path, and output passes through rather than re-walking,
568
+  so the `js_to_value`/`py_to_value` guards added under ADR-20 are removed. The single
569
+  residual is structurally comparing two *distinct* cyclic foreign graphs, bounded by
570
+  an `Arc::ptr_eq` short-circuit plus a recursion cap in `feel_equal`. Verified by
571
+  feel-core foreign-seam unit tests (path is lazy → `get`, consume forces, an
572
+  unbounded graph terminates along a finite path, `Value` size pinned), 24 feel-py
573
+  pytest cases (a getter fires on access, an untouched sibling property does **not**,
574
+  identity pass-through, cyclic finite path), and feel-wasm smoke cases (`Reflect.get`
575
+  getter, nested navigation, pass-through identity, cyclic termination).
529576
 
530577
 ## Revisable design decisions
531578
 
@@ -647,32 +694,30 @@
647694
 - **If flipped:** return years-months durations as an ISO-8601 string or a small
648695
   module-level class; drop the `python-dateutil` dependency.
649696
 
650
-### RD-6: feel-py context mapping is eager, not lazy (no `Value::Foreign` in the core)
651
-
652
-- **Weighed:** FEEL has only the dot for member access (`Expr::Path`), resolved
653
-  **inside feel-core** against a fully materialised `Value` tree — there is no live
654
-  Python object at evaluation time. A *lazy* alternative (feel-core calling `getattr`
655
-  back into Python per path segment) was considered and **rejected**: it would need an
656
-  opaque `Value::Foreign` variant in the core, which duck-types the evaluator (every
657
-  type-inspecting op — `distinct`, arithmetic, comparison — would have to "force" the
658
-  value via the host) and lands the concept in *all* targets (pg/wasm) though only
659
-  Python would create such values — against the spirit of ADR-1. Lazy `getattr` would
660
-  also fire `@property`/descriptors, i.e. side effects / lazy DB loads on ORM rows.
661
-- **Decision (2026-06-16):** convert **eagerly** at the boundary. A `dict` maps by its
662
-  keys; any other object maps from its instance `__dict__` (`vars(obj)`) — instance
663
-  data only, so descriptors/properties/methods are **never** invoked (no side effects),
664
-  mirroring feel-wasm's `Object.keys` model. Keys beginning with `_` (dunders and
665
-  internal bookkeeping like `_sa_instance_state`) are skipped. A `__dict__`-less object
666
-  (`__slots__`) is not introspectable → null. An `id()`-based cycle guard plus a depth
667
-  cap break reference cycles (a back-edge → null), since loaded bidirectional ORM
668
-  graphs / parent-child trees can be cyclic; on acyclic dict data neither triggers.
669
-- **Revisit trigger:** a real need to evaluate against very large or genuinely live
670
-  object graphs where an eager snapshot is too costly — *and* a way to do it without a
671
-  foreign value in the core.
672
-- **If flipped:** add a `HostPath` resolver trait to `EvalEnv` plus a
673
-  `Value::Foreign(handle)` variant in feel-core (an ADR-16-style seam), with
674
-  force-on-consume rules for non-path operations — a feel-core change warranting its
675
-  own ADR.
697
+### RD-6: feel-py/feel-wasm context mapping is lazy (flipped 2026-06-17 → ADR-21)
698
+
699
+- **Weighed:** The original choice (2026-06-16) converted the whole host graph
700
+  **eagerly** and rejected a lazy `Value::Foreign`, fearing it would duck-type the
701
+  evaluator and land a foreign-value concept in *all* targets against ADR-1's spirit,
702
+  and that lazy `getattr` would fire `@property`/descriptors (side effects / DB hits on
703
+  ORM rows). On revisiting for ORM rows and large/live graphs, those concerns proved
704
+  manageable: the foreign value is *self-describing* (no `EvalEnv` resolver needed, just
705
+  a trait object that knows how to `get`/`force` itself), the force checks are a single
706
+  inlined tag test (pure-Rust embedders pay nothing, `Value` does not grow), and the
707
+  side-effect firing is now *desired and bounded* — only the attributes the expression
708
+  names fire, which is exactly the on-demand load users want.
709
+- **Decision (2026-06-17, flipped):** convert **lazily** — wrap a host object/dict
710
+  behind feel-core's `ForeignObject` seam, resolving each dot on demand via
711
+  `getattr`/`Reflect.get`; scalars and temporals stay eager; a foreign result passes
712
+  the original object back by identity. Full rationale and mechanics in **ADR-21**.
713
+  The previous eager rule (instance `__dict__`, no property firing, an `id()`/depth
714
+  cycle guard) is gone; bulk `force` still reads instance data only, so a *blanket*
715
+  materialisation never sweeps properties.
716
+- **Revisit trigger:** a need to forbid side effects entirely at the boundary (a hard
717
+  guarantee that no `@property`/getter ever runs during evaluation).
718
+- **If flipped (back):** reintroduce an eager `shallow→deep` snapshot as an **opt-in**
719
+  (e.g. an `eager=`/`force=` parameter) while keeping the lazy default — the seam
720
+  (`ForeignObject` + `Value::forced`) already supports forcing a whole subtree.
676721
 
677722
 ## Roadmap: deferred scope (recorded, not yet built)
678723
 

modified README.md +8 −4

@@ -252,10 +252,14 @@
252252
 prog.evaluate({"a": 41})                         # => 42
253253
 ```
254254
 
255
-Besides `dict`s, a context may be any object: its instance `__dict__` is read
256
-(plain data only — `@property`/methods are never invoked, so no side effects),
257
-and reference cycles are broken (back-edge → `None`). Temporal values map to
258
-`datetime`/`dateutil` types (years-months durations to `relativedelta`).
255
+Besides `dict`s, a context may be any object, mapped **lazily** (DECISIONS.md
256
+ADR-21): `order.customer.address` resolves one hop at a time via attribute access,
257
+so a `@property`/relation loads **on demand**, exactly when the expression names it
258
+(an untouched attribute never fires). A host object that comes back as the result is
259
+the *same* object, not a snapshot. Because a (finite) expression only navigates a
260
+finite path, cyclic graphs are fine — no cycle guard needed. Temporal values map to
261
+`datetime`/`dateutil` types (years-months durations to `relativedelta`). The same
262
+lazy mapping applies in feel-wasm (`Reflect.get`).
259263
 
260264
 The `matches`/`replace`/`split` builtins run on the **host** regex engine, so in
261265
 Python the **`re`** dialect applies (DECISIONS.md ADR-16/RD-4) — the FEEL/XPath