| @@ -526,6 +526,53 @@ |
| 526 | 526 | cycle/depth guard feel-py uses: its recursion was previously unbounded, so a cyclic |
| 527 | 527 | JS object (`a.b = a`, legal in JS) could overflow the stack; a back-edge now maps to |
| 528 | 528 | `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). |
| 529 | 576 | |
| 530 | 577 | ## Revisable design decisions |
| 531 | 578 | |
| @@ -647,32 +694,30 @@ |
| 647 | 694 | - **If flipped:** return years-months durations as an ISO-8601 string or a small |
| 648 | 695 | module-level class; drop the `python-dateutil` dependency. |
| 649 | 696 | |
| 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. |
| 676 | 721 | |
| 677 | 722 | ## Roadmap: deferred scope (recorded, not yet built) |
| 678 | 723 | |