| @@ -6,15 +6,28 @@ |
| 6 | 6 | |
| 7 | 7 | ## ADR-1: `feel-core` has no target-specific dependencies (Cardinal Rule) |
| 8 | 8 | |
| 9 | | -- **Context:** The engine must run identically inside PostgreSQL (pgrx) and in |
| 10 | | - the browser/Node (wasm-bindgen). If target glue leaks into the core, the two |
| 11 | | - builds drift and "same core, same result" stops being true. |
| 12 | | -- **Decision:** `feel-core` depends only on portable crates (today: `lalrpop`, |
| 13 | | - `lalrpop-util`). Anything pgrx- or wasm-bindgen-specific lives exclusively in |
| 14 | | - the wrapper crates `feel-pg` and `feel-wasm`. |
| 15 | | -- **Consequence:** The core is reusable and testable on the host toolchain. We |
| 16 | | - enforce this with `cargo tree -p feel-core` (must show no `pgrx` / |
| 17 | | - `wasm-bindgen`). Boundary concerns (JSON mapping) are pushed to the wrappers. |
| 9 | +- **Context:** The engine must run inside PostgreSQL (pgrx), the browser/Node |
| 10 | + (wasm-bindgen) and Python (pyo3) — and, planned, Lua (mlua) — from one core. Two |
| 11 | + *distinct* invariants make "one core" mean something, and conflating them has |
| 12 | + caused confusion (ADR-16 describes itself as a "relaxation of the cardinal rule"), |
| 13 | + so name them apart: |
| 14 | + - **I1 — Dependency purity (inviolable).** `feel-core` depends only on portable |
| 15 | + crates (today: `lalrpop`/`lalrpop-util`, `logos`, `unicode-ident`, `jiff`, and |
| 16 | + the optional `regex`); no *target* crate (`pgrx`/`wasm-bindgen`/`pyo3`/`mlua`) |
| 17 | + ever enters its tree. Never relaxed. |
| 18 | + - **I2 — Result determinism (relaxable, but only loudly).** The same expression |
| 19 | + yields the same FEEL *value* on every target. A strong default, not an |
| 20 | + absolute: ADR-16 (host regex dialect) and per-host temporal *representation* |
| 21 | + (RD-2/RD-5) consciously relax it — always feature-flagged and documented, never |
| 22 | + silently. The boundary *representation* of a value (jsonb vs native vs ISO) is a |
| 23 | + wrapper concern (ADR-4), not part of I2. |
| 24 | +- **Decision:** Keep I1 absolute and I2 the default. Anything target-specific — |
| 25 | + including the JSON/native boundary mapping — lives exclusively in the wrapper |
| 26 | + crates (`feel-pg`, `feel-wasm`, `feel-py`, and the planned `feel-lua`). |
| 27 | +- **Consequence:** The core is reusable and testable on the host toolchain. I1 is |
| 28 | + enforced mechanically with `cargo tree -p feel-core` (must show no |
| 29 | + `pgrx`/`wasm-bindgen`/`pyo3`/`mlua`). Each I2 relaxation is recorded as an ADR or |
| 30 | + RD, so a reader sees exactly where — and why — two targets may differ. |
| 18 | 31 | |
| 19 | 32 | ## ADR-2: Numbers are `f64` for now (deviation from spec `decimal128`) |
| 20 | 33 | |
| @@ -60,6 +73,16 @@ |
| 60 | 73 | `evaluate(expr, context_json) -> String` (JSON). Integral numbers are emitted |
| 61 | 74 | as JSON integers so `1 + 1` yields `2`, not `2.0`. `feel-core` stays free of |
| 62 | 75 | any serialization dependency in its non-dev tree. |
| 76 | +- **Status:** The *literal* "JSON at the boundary" framing is superseded. It held |
| 77 | + when feel-wasm exchanged JSON strings; the boundary has since become |
| 78 | + *language-neutral structured values* whose concrete representation is per-wrapper — |
| 79 | + `jsonb` (feel-pg) versus **native host values mapped lazily** through the |
| 80 | + `ForeignObject` seam (feel-wasm/feel-py, and the planned feel-lua), temporal values |
| 81 | + as native types or ISO-8601. See ADR-12 (Context), ADR-18 (temporal), ADR-20 |
| 82 | + (feel-py native mapping) and **ADR-21** (lazy host values). The durable part holds: |
| 83 | + the mapping lives in the wrappers, never in core (ADR-1/I1), and integral numbers |
| 84 | + stay integers (`1 + 1` → `2`). The per-target representation differences are the |
| 85 | + I2-representation kind (ADR-1), not value divergence. |
| 63 | 86 | |
| 64 | 87 | ## ADR-5: Temporal mapping convention (not exercised in this slice) |
| 65 | 88 | |
| @@ -129,6 +152,11 @@ |
| 129 | 152 | - **Consequence:** This is a visible behaviour change at the wrappers — |
| 130 | 153 | `feel_eval('1 + true')` now returns JSON `null` instead of raising an ERROR. |
| 131 | 154 | It is required for the three-valued TCK cases to pass and matches the spec. |
| 155 | +- **Status:** Refined by **ADR-14** — the error sources named here (type mismatch, |
| 156 | + division by zero, non-finite arithmetic, non-boolean logic operands) now produce an |
| 157 | + *error-null* (`Value::Error`) rather than a plain `Value::Null`, so provenance is |
| 158 | + tracked and the strict boundary can surface them. The three-valued *result* is |
| 159 | + unchanged (error-null behaves identically to null inside core). |
| 132 | 160 | |
| 133 | 161 | ## ADR-10: `Value` extended with String and List (map type superseded by ADR-12) |
| 134 | 162 | |
| @@ -311,9 +339,13 @@ |
| 311 | 339 | tables, ~0.6 MB of code plus `.rodata`) dominated the WASM bundle. It was pulled |
| 312 | 340 | by two consumers: lalrpop's built-in lexer and these three builtins. Removing |
| 313 | 341 | the lexer's copy (the logos addendum to ADR-3) is not enough — the builtins |
| 314 | | - still pull the whole stack. Meanwhile each host already ships a capable regex |
| 315 | | - engine (the browser/Node `RegExp`; PostgreSQL's POSIX ARE), and FEEL/XPath regex |
| 316 | | - is its own dialect that no single embedded engine reproduces exactly anyway. |
| 342 | + still pull the whole stack. Meanwhile *most* hosts already ship a capable regex |
| 343 | + engine (the browser/Node `RegExp`; Python's `re`; PostgreSQL's POSIX ARE), and |
| 344 | + FEEL/XPath regex is its own dialect that no single embedded engine reproduces |
| 345 | + exactly anyway. (Where a host has *no* regex engine — Lua offers only the weaker |
| 346 | + Lua *patterns*, not regex — the seam degrades gracefully: keep the bundled |
| 347 | + `RustRegex`, as feel-pg does (RD-1), rather than force a lossy host dialect. The |
| 348 | + provider is a per-target *choice*, not an obligation.) |
| 317 | 349 | - **Decision:** Abstract the three regex builtins behind a `FeelRegex` trait |
| 318 | 350 | (`is_match`/`replace`/`split`, each returning `None` → FEEL null for a |
| 319 | 351 | dialect/pattern error) and thread it through evaluation via an `Evaluator` |
| @@ -574,6 +606,89 @@ |
| 574 | 606 | pytest cases (a getter fires on access, an untouched sibling property does **not**, |
| 575 | 607 | identity pass-through, cyclic finite path), and feel-wasm smoke cases (`Reflect.get` |
| 576 | 608 | getter, nested navigation, pass-through identity, cyclic termination). |
| 609 | +- **Thread-safety & lifetime (deliberate, and Lua-relevant):** `ForeignObject: Debug` |
| 610 | + carries **no `Send`/`Sync` bound**, so `Value` and `Context` became `!Send + !Sync` |
| 611 | + the moment a `Foreign` can appear. This is intentional — host handles are themselves |
| 612 | + `!Send` (a `JsValue`; an mlua `Table`) — and costs the current hosts nothing, since |
| 613 | + each evaluates on a single thread (the pgrx backend, the WASM thread, under the |
| 614 | + Python GIL). The price: a `Value`/`Context` carrying a `Foreign` cannot cross |
| 615 | + threads; a cross-thread embedder must first materialise a `Foreign`-free `Value` |
| 616 | + (force the subtree) or take the eager-snapshot route (RD-6, "if flipped back"). The |
| 617 | + trait object is `Arc<dyn ForeignObject + 'static>`, so a wrapper's stored handle must |
| 618 | + be owned/`'static` — `Py<PyAny>`, `JsValue`, and mlua ≥ 0.10's lifetime-free `Table` |
| 619 | + all qualify (an older, lifetime-parameterised mlua would not). This is what makes the |
| 620 | + planned feel-lua (ADR-22) a clean fit rather than a fight with the borrow checker. |
| 621 | + |
| 622 | +## ADR-22 (planned): Lua bindings (`feel-lua`, mlua) |
| 623 | + |
| 624 | +- **Context:** feel-core already powers three wrappers (feel-pg, feel-wasm, feel-py). |
| 625 | + Lua is a common embedded-scripting and rules host (game logic, Redis, nginx/ |
| 626 | + OpenResty, Neovim, application plugins), so a fifth target is a natural next step. |
| 627 | + This entry records the decision and its constraints **before** the crate is built — |
| 628 | + in the spirit of ADR-5 — so the next increment starts from a decision, not a blank |
| 629 | + page. *Not yet built.* |
| 630 | +- **Decision (planned):** a new `crates/feel-lua`, a wrapper over the same core |
| 631 | + (ADR-1/I1 holds — `mlua` lives only here), built with **mlua ≥ 0.10**. The 0.10 line |
| 632 | + dropped the `'lua` lifetime parameter, so `Table`/`Value`/`Function` are owned and |
| 633 | + `'static` and fit the `Arc<dyn ForeignObject + 'static>` seam (ADR-21) directly; an |
| 634 | + older lifetime-parameterised mlua would not. Like feel-py (PyO3) and feel-pg (pgrx), |
| 635 | + feel-lua needs a Lua *link context*, so it goes in the workspace `members` but |
| 636 | + **not** `default-members` (a bare `cargo build` keeps building only the portable |
| 637 | + members). The three boundary sub-decisions are recorded ahead as **RD-7** (regex), |
| 638 | + **RD-8** (temporal) and **RD-9** (context mapping); the seven reusable seams are the |
| 639 | + "Adding a wrapper" contract below. |
| 640 | +- **Hazard to plan for:** Lua signals errors by `longjmp` (like PostgreSQL — the RD-1 |
| 641 | + hazard), but mlua already wraps every C-API call and surfaces them as |
| 642 | + `Result<_, mlua::Error>`. So `LuaForeign::get`/`force` map an `Err` (a raised |
| 643 | + `__index` metamethod, a bad access) to a value-null — the same defensive pattern |
| 644 | + feel-py uses around `getattr` (ADR-21). The longjmp never unwinds through Rust |
| 645 | + frames; mlua catches it at the boundary. |
| 646 | +- **Consequence (anticipated):** a fifth target on the same core. `Value` stays |
| 647 | + `!Send` (ADR-21), which *matches* mlua's non-`Send` values rather than fighting them. |
| 648 | + I2 (ADR-1) relaxes here exactly as it does elsewhere — no Lua regex (RD-7), no native |
| 649 | + Lua temporal (RD-8) — both loud and documented, neither a surprise. |
| 650 | + |
| 651 | +## Adding a wrapper — the host-binding contract |
| 652 | + |
| 653 | +The four wrappers (feel-pg, feel-wasm, feel-py, planned feel-lua) implement the same |
| 654 | +seven seams against feel-core. Writing them down once turns "add a target" into "fill a |
| 655 | +template" rather than re-deriving the boundary (as feel-py did, landing RD-4/5/6). Each |
| 656 | +seam links the ADR that defines it and the concrete core entry point. |
| 657 | + |
| 658 | +1. **Parse** — `feel_core::compile(expr) -> CompiledExpr`. Parse once, evaluate many |
| 659 | + (feel-pg caches per call-site via pgrx `fn_extra`; the others expose `compile`). |
| 660 | +2. **Evaluate** — `CompiledExpr::evaluate_with_env(&ctx, &EvalEnv)`: the *single* |
| 661 | + general entry the wrappers call (ADR-16/ADR-18; the regex-only `evaluate_with` was |
| 662 | + retired). Build the `Context` with `Context::insert`. |
| 663 | +3. **Input mapping (lazy)** — scalars and temporals convert eagerly; an object/dict/ |
| 664 | + table becomes `Value::Foreign` wrapping a host-specific `ForeignObject` (`get` = the |
| 665 | + FEEL dot, by name; `force` = materialise one level; `as_any` = recover the handle). |
| 666 | + Catch host errors inside `get`/`force` → value-null (ADR-21). |
| 667 | +4. **Output mapping** — `Value` → host value; a `Value::Foreign` that reaches the result |
| 668 | + is handed back **by identity** via `as_any` downcast (no re-traversal); temporal |
| 669 | + values map to native host types or ISO-8601 (ADR-18); error-null/value-null per the |
| 670 | + strict/lenient policy. |
| 671 | +5. **Regex provider** — implement `FeelRegex` over the host engine, *or* reuse the |
| 672 | + bundled `RustRegex` when the host has none (ADR-16; only `matches`/`replace`/`split` |
| 673 | + consult it). |
| 674 | +6. **Clock** — put a fixed `jiff::Zoned` instant in the `EvalEnv` so `now()`/`today()` |
| 675 | + are stable for the whole evaluation (ADR-18; e.g. feel-pg's transaction start). |
| 676 | +7. **Strict vs lenient** — after eval, `value.contains_error()` decides: raise/throw |
| 677 | + (strict) or `into_lenient()` → null (lenient default) (ADR-14). |
| 678 | + |
| 679 | +Per-wrapper choices at a glance (the detail, with revisit triggers, is in RD-1…9): |
| 680 | + |
| 681 | +| Seam | feel-pg | feel-wasm | feel-py | **feel-lua (planned)** | |
| 682 | +| --- | --- | --- | --- | --- | |
| 683 | +| Input mapping | eager `jsonb` | lazy `JsForeign` | lazy `PyForeign` | lazy `LuaForeign` (RD-9) | |
| 684 | +| Regex dialect | `RustRegex` (RD-1) | `JsRegex` (ADR-16) | `PyRegex`/`re` (RD-4) | `RustRegex` (RD-7) | |
| 685 | +| Temporal repr | ISO→native cast (RD-2) | JS `Date` / ISO (ADR-18) | `datetime`/`dateutil` (RD-5) | ISO-8601 string (RD-8) | |
| 686 | +| Clock | transaction start | `Date.now()` | system (`jiff::now()`) | system (`jiff::now()`) | |
| 687 | +| Strict surface | raise `ERROR` | throw | raise `FeelError` | Lua error | |
| 688 | +| Parse cache | `fn_extra` (auto) | `compile()` | `compile()` | `compile()` | |
| 689 | + |
| 690 | +(The matrix is a summary; RD-1…6 keep the full "weighed / decision / revisit trigger / |
| 691 | +if flipped" rationale a grid can't carry.) |
| 577 | 692 | |
| 578 | 693 | ## Revisable design decisions |
| 579 | 694 | |
| @@ -720,6 +835,56 @@ |
| 720 | 835 | (e.g. an `eager=`/`force=` parameter) while keeping the lazy default — the seam |
| 721 | 836 | (`ForeignObject` + `Value::forced`) already supports forcing a whole subtree. |
| 722 | 837 | |
| 838 | +### RD-7: feel-lua keeps the Rust regex dialect (no Lua-pattern provider) |
| 839 | + |
| 840 | +- **Weighed:** ADR-16 makes the three regex builtins host-pluggable. Unlike the |
| 841 | + browser (`RegExp`), Python (`re`) and Postgres (POSIX ARE), **Lua has no regex |
| 842 | + engine** — only Lua *patterns*, which lack alternation, full quantifiers and general |
| 843 | + grouping, so they cannot express FEEL/XPath regex. A faithful `LuaRegex` is therefore |
| 844 | + off the table; the realistic options are (a) keep the bundled `RustRegex` (parity with |
| 845 | + feel-core/TCK, like feel-pg RD-1) or (b) bind a C regex library (`lrexlib`/PCRE) — real |
| 846 | + FFI weight for a marginal gain. |
| 847 | +- **Decision (planned):** feel-lua keeps the default `RustRegex` provider — same dialect |
| 848 | + as feel-core and the TCK fixtures (the validated reference), and no native dependency. |
| 849 | + The `evaluate_with_env` seam leaves the door open. |
| 850 | +- **Revisit trigger:** a concrete need for FEEL regex in Lua to match a host PCRE binding |
| 851 | + users already author against. |
| 852 | +- **If flipped:** implement `struct LuaRegex` over `lrexlib`/PCRE as a |
| 853 | + `feel_core::FeelRegex`, mapping its errors to `None` → FEEL null, and pass it via |
| 854 | + `evaluate_with_env`; document PCRE as a per-target divergence under ADR-16. |
| 855 | + |
| 856 | +### RD-8: feel-lua temporal mapping is ISO-8601 strings |
| 857 | + |
| 858 | +- **Weighed:** ADR-18's boundary is canonical ISO-8601. Lua has **no native date/time |
| 859 | + type** — only numbers, strings and tables (`os.time`/`os.date` work in epoch seconds or |
| 860 | + `{year=, month=, …}` tables). Options for the five FEEL temporal types: ISO-8601 strings |
| 861 | + (as feel-wasm leaves durations), `os.date`-style tables, or userdata from a Lua datetime |
| 862 | + library. |
| 863 | +- **Decision (planned):** map every temporal type to/from a **canonical ISO-8601 string** — |
| 864 | + the portable boundary ADR-18 already produces, with zero extra dependency and no lossy |
| 865 | + conversion. A convenience `os.date`-style table form (`{year=, month=, day=, …}`) on input |
| 866 | + is a possible later addition, not the default. |
| 867 | +- **Revisit trigger:** users want first-class Lua date objects (integration with a specific |
| 868 | + Lua datetime lib) rather than ISO strings. |
| 869 | +- **If flipped:** add a table/userdata mapping in the wrapper boundary (`to_iso`/ |
| 870 | + `temporal_from_iso` already bridge the value side); keep ISO as the fallback. |
| 871 | + |
| 872 | +### RD-9: feel-lua context mapping is lazy (`LuaForeign`) |
| 873 | + |
| 874 | +- **Weighed:** ADR-21 made host context mapping lazy via `ForeignObject`, so an ORM row or |
| 875 | + deep table resolves one dot at a time. A Lua table can carry an `__index` metamethod (a |
| 876 | + computed/lazy field) exactly like a Python `@property`, so the lazy seam fits; an eager |
| 877 | + alternative would snapshot and miss metamethod semantics. |
| 878 | +- **Decision (planned):** wrap an `mlua::Table` (or userdata) in a `LuaForeign` implementing |
| 879 | + `ForeignObject`: `get` via `Table::get` (which fires `__index`, the on-demand load), an |
| 880 | + `Err` → value-null; `force` enumerates the table's own pairs one level; `as_any` recovers |
| 881 | + the handle for identity pass-through. Scalars and temporals (RD-8) stay eager. Mirrors |
| 882 | + feel-py's `PyForeign` and feel-wasm's `JsForeign`. |
| 883 | +- **Revisit trigger:** a need to forbid metamethod side effects entirely during evaluation |
| 884 | + (a hard "no `__index` runs" guarantee). |
| 885 | +- **If flipped:** offer an opt-in eager snapshot (force the whole subtree at the boundary) |
| 886 | + while keeping lazy the default — the same escape hatch RD-6 describes. |
| 887 | + |
| 723 | 888 | ## Roadmap: deferred scope (recorded, not yet built) |
| 724 | 889 | |
| 725 | 890 | These were considered and deliberately deferred to keep the slice thin. Listed so |