Commit Graph
23 Commits
Author SHA1 Message Date
Stefan c694bfa904 feat(engine): pin the clock through options instead of the environment
The __ZEN_MOCK_UTC_TIME variable and the ZEN_EXPRESSION_CONFIG lane flag are
gone. Isolate takes with_now and with_lane, and the interpreted engine now
honours the now option in expressions, so tests pass the instant explicitly.
2026-11-21 14:42:04 +01:00
stefan-gorules e3d32a210d feat: trace format lane (#573)
* feat(trace): trace format v1 with TraceCollector on the compiled engine

Adds trace format v1 to the compiled engine, recorded by explicit trace
recorders and written natively. The interpreted engine keeps the legacy
trace.

- EvaluationOptions.trace is a TraceFormat (off, v1, legacy). Legacy
  traces are produced directly from the same recording.
- zen_engine::trace exposes convert, restore and inspect for
  stored documents, with redaction and truncation markers.
- The trace header takes evaluatedAt, timezone and seed from the
  evaluation context, so an evaluation replays from its own trace.
- Batch and columnar evaluations record events as columns and write one
  document per row.
- Bindings accept the trace format (true, 'v1', 'legacy'); the Node
  binding ships TypeScript types for the v1 document.
- Golden, fixture and damaged-document tests cover byte identity with the
  legacy trace, restore, interop with the reference fixtures and
  malformed input.

* perf(trace): build columnar request rows once with precomputed paths

* perf(trace): write a node's edits from its written columns

* perf(trace): capture node columns instead of row values in the columnar plan

* perf(trace): reuse a node's rendered body for rows with the same rule and cell values

* perf(trace): size the batch trace buffer up front, key node bodies on typed column words, share request rows as the input

* perf(trace): read the request from input columns and build request rows only on demand

* perf(trace): key node bodies on per-column codes and precompute node heads and result labels

* perf(trace): keep first-hit table facts and their referenced fields as columns

* perf(trace): write per-row expression results in the batch writer

* perf(columnar): write result rows as JSON straight from output columns

* perf(trace): keep collect-policy rule hits as columns and reuse hit text

* perf(trace): pack loop results from columns and resolve result column accessors once per batch

* perf(trace): share row-wise node outputs with the trace when nothing can alias them

* perf(trace): pack lists without a string per distinct item

* perf(trace): write new lists from columns and decide references by where a list came from

* perf(trace): write wide list items from columns while the list has never been built

* perf(trace): read list cells once while rendering

* feat(trace): list table inputs once per node and key rule cells by column id

* feat(trace): name matched policy table rows by rule id

* feat(trace): store loop results as one column per key

* build: optimise test builds
2026-11-21 13:38:43 +01:00
stefan-gorules cd6a3f3adb feat: evaluation context (#568)
* feat(engine): explicit evaluation context with now, timezone and seed

The compiled engine evaluates against an explicit EvaluationContext
instead of the host clock, host zone and an unseeded random source.

- zen_expression::context: EvaluationContext holds now (read once on
  first use, shared by every row of a batch), the time zone (supplied
  or the host zone cached per process), the locale, the seed (drawn on
  the first draw) and per-row and per-item random streams created only
  when a row or item draws. rows/picked/items/item/at narrow it with an
  Rc clone; at and item allocate nothing. RowContext is the borrowed
  per-row view builtins, kernels and function hosts read.
- the context is passed explicitly through the lane VM (LaneRunner,
  Session, kernels, cells, builtins), typed Program::run, graph state,
  rows, loops, sub-decisions, hosted rows, the columnar plan, policies
  and their plans. There are no thread-locals for context data.
- rand draws once per lane, row and item from the row's stream, so
  map(xs, rand(100)) draws per item. rand is never pure, folded,
  memoised or compiled into kernels as a constant; Math.random is no
  longer pure in the function purity analysis.
- function hosts (v1 and v2) install Date, Math.random and a minimal
  Intl.DateTimeFormat once per runtime and read a per-call slot:
  Date.now, new Date, local-time getters and setters, toString,
  toLocale*String and getTimezoneOffset use the context's now, zone and
  locale.
- EvaluationOptions and EvaluationSerializedOptions gain now, timezone,
  seed and locale (en-US by default). Workspace gains evaluate_pinned,
  evaluate_batch_pinned and evaluate_columns_pinned. In a batch, row i
  draws with row_seed(seed, i) and replays alone with that seed.
- bindings: Node and Python accept and validate now, timezone, seed and
  locale; C and uniffi pass them through and ignore invalid values.
- d('...') is folded at compile time only for texts with an explicit
  zone; an invalid __ZEN_MOCK_UTC_TIME is ignored by the context.
- the interpreted engine and the stack VM are unchanged and ignore the
  new options.

* refactor(engine): name evaluation context parameters ctx and context

* feat(engine): derive random draws from their location

A draw is now a pure function of (seed, stream, scope, field, site, n)
instead of a running counter per stream, so reordering, short-circuiting,
dead-code elimination or an extra rand elsewhere in a decision no longer
shifts other draws.

- site: occurrence index of rand( in source order, numbered at compile
  time and carried on the lane call op
- field: stable label from node/rule/column/key/statement/block ids,
  baked into multi-root programs at compile time or set on the context
  by per-source (session) paths
- scope: chained per sub-decision call (caller node + key) and per
  function node; JS Math.random uses a fixed site in that scope
- n: counter per (stream, address) for repeated evaluation only

Tables with draws run on the exact row path; fused cells and fused rules
refuse draws; object-literal expansion is skipped for drawing sources.

* refactor(expression): own the rand integer mapping

Map a draw to 0..=upper with Lemire multiply-shift and rejection, taking
further words from the same address on retry, so a dependency update can
never change rand(n) for a given seed. A negative upper bound now returns
"Invalid upper range" on both VMs instead of panicking, and the unseeded
root seed comes from a hashed counter, which drops fastrand.

* test(engine): keep evaluation context tests independent under cargo test

cargo test runs a crate's tests as threads of one process, so tests that
share process state raced:

- pinned.rs switched the global ZEN_CONFIG.compiled on and off; each
  decision now picks its engine with compiled_engine() / interpreted().
- clock_reads() counted reads from every thread; it now counts the
  current thread only.
- typed exact tests relied on __ZEN_MOCK_UTC_TIME, which is cached on the
  first clock read by any test; they now pin now in the context.

* ci: pin Rust 1.99 for the Windows Node and UniFFI builds

Rust 1.100 makes Cargo's new build-dir layout permanent: every
dependency gets its own directory and its own -L dependency= flag.
For zen-engine with --target x86_64-pc-windows-msvc the rustc command
line passes Windows' 32,767-character limit, and sccache fails to spawn
rustc with os error 206. Cargo itself falls back to an argument file,
but sccache does not.

Other matrix entries stay on stable. Remove the pin once sccache
handles long command lines.

* refactor(engine): remove locale from the evaluation context

The locale option did not make evaluations more reproducible: QuickJS has
no ICU, so function nodes never picked up a host locale. It only added
approximate locale-aware date formatting from a hand-written table, as new
public API in every binding.

Function nodes keep the time zone of the context. toLocaleString,
toLocaleDateString and toLocaleTimeString return QuickJS's fixed format in
that zone and ignore a locale argument, as before. Intl is reduced to
DateTimeFormat().resolvedOptions(), which reports the time zone.

Drops the icu_locale_core dependency.

* test(engine): compare interpreted draws with the pinned ones

The interpreted engine's Math.random is QuickJS's own, seeded from the
clock in microseconds when a context is created. On Windows the clock is
coarser, so two evaluations in a row can get the same seed and the same
numbers. Assert instead that the interpreted engine does not return the
compiled engine's draws for the same seed.
2026-11-17 17:37:10 +01:00
stefan-gorules fe43b4c1d7 perf: general performance improvements (#500)
* perf: general performance improvements

* remove unsafe usages, further improve perf

* perf: add table indexing
2026-08-04 11:12:37 +02:00
stefan-gorules 8d1910cc4e feat: graph workspace and static analysis (#483) 2026-07-16 23:42:13 +02:00
stefan-gorules 6c0ca51354 feat: policy engine (#450)
* feat: policy engine

* fix: cargo fmt

* fix: resolve issues with miri
2026-06-23 18:51:47 +02:00
bcrevar-gorulesandStefan 7bfebd90fe feat: precompile decision content (#401)
* precompile first

* test

* add Opcode cache through Option<T>

* Compiled support for decisionTable,
default compile of new DecisionContent for node.js and py bindings

* format

* DecisionContent refactored to model module

* fmt

* added back BaseDecisionContent

* tests and benchmark

---------

Co-authored-by: Stefan <stefan@gorules.io>
2025-11-21 14:44:24 +01:00
stefan-gorules 07040c8a96 feat: lazy load fn nodes (#389)
* feat: lazy load fn nodes

* fix rebase, fix $nodes

* fix

* miri ignore test
2025-11-07 10:53:21 +01:00
stefan-gorules 91509821be feat: refactor engine (#390)
* temp: refactor engine

* temp

* implement remaining nodes

* resolve snapshot tests

* improve decision graph implementation

* implement changes in bindings

* implement changes in bindings

* fix function impl gaps

* update cargo.toml

* fix issues with trace
2025-09-04 18:54:32 +02:00
stefan-gorulesandbojancrevar 80fe4021c2 feat: compact trace (#384)
* feat: compact trace

* align with changes

* add deserialization

* add tests and deserializer

* fix bindings

* first draft

* mock time; update snapshots

* fix

* Node.js test fix

* fix merge

* fix utc override

* fix cross-env

* fix workflow

* fix

---------

Co-authored-by: bojancrevar <bojan.crevar@gorules.io>
2025-08-25 12:33:07 +02:00
bcrevar-gorulesandStefan 4a59f222db feat: add snapshot tests (#385)
* first draft

* mock time; update snapshots

* fix

* Node.js test fix

---------

Co-authored-by: Stefan <stefan@gorules.io>
2025-08-20 16:30:28 +02:00
stefan-gorules 2b73856d55 feat: schema validation (#267)
* feat: schema validation

* fix prettier

* update implementation

* add test cases

* change validator to string

* update tests
2024-12-25 13:00:04 +01:00
stefan-gorulesandIvan Miletic 47812143a0 feat: passthrough nodes (#261)
* feat: passthrough nodes

* update bindings

* update model

* update exclusion

* fix merge strategy

* fix

* fix merge

* add tests

* fix walk

* fix merge

* fix

* fix: add tests

* fix types

---------

Co-authored-by: Ivan Miletic <vnmiletic@gmail.com>
2024-10-23 15:59:31 +02:00
stefan-gorules 91598166ce feat: rc variable (#246)
* feat: rc variable

* feat: implement variable in engine

* add exports

* fix bindings and improve clone accuracy

* fix tests, fix bindings

* fix fmt

* improve code
2024-09-30 18:34:54 +02:00
stefan-gorules 4a2312e956 fix: function v2 quickjs (#214)
* fix: function v2 quickjs

* upgrade rquickjs
2024-07-16 22:26:50 +02:00
stefan-gorules cc3d938b2f feat: function v2 (#212)
* feat: function v2

* expose evaluate

* remove async trait, add caching

* fix naming

* add reverse compatibility

* add http module

* update impl

* resolve issues with reqwest

* fix

* fix

* fix ssl

* remove unused deps

* fix: tests

* fix
2024-07-16 11:22:51 +02:00
stefan-gorules daecf901e6 feat: custom node (#138)
* feat: custom node

* add zen template, expose $nodes and $root

* add custom handler to go and nodejs

* add support for python, improve bindings, add error to zen template

* fix: correct binding exports for nodejs and python

* fix benchmark

* improve rust api, trim template in zen templates

* update cargo action

* update expression version

* compile action

* fix action format
2024-04-03 16:09:51 +02:00
stefan-gorules c281c55ef1 feat: replace v8 with quickjs (#119)
* feat: replace v8 with quickjs

* fix format; fix pipelines

* fix: workflow

* fix: remove incorrect target in node windows pipeline
2024-02-09 10:48:08 +01:00
stefan-gorules 5f423b7910 feat: zen expression rewrite (#107)
* temp: zen-expression rewrite

* rewrite standard parser

* stabilise parser, upgrade dependencies

* implement unary parser

* align builtin behavior

* fix tests and minor bugs

* refactor exports, update docs

* fix expression repl

* cleanup

* cleanup vm

* replace expression with expression-rewrite

* update d.ts file

* fix failing test
2023-12-12 08:50:25 +01:00
efcbe36c1f feat: switch node (#103)
* feat: switch node

* refactors

* add tests

* fix: change digraph to stable

* fix: test models

* fix: readme

* add aml test, disable miri for graph test due to v8

* Update README.md

* Update README.md

---------

Co-authored-by: Ivan Miletic <vnmiletic@gmail.com>
Co-authored-by: Ivan Miletic <60513195+ivanmiletic@users.noreply.github.com>
2023-11-30 13:00:34 +01:00
stefan-gorules f8dc07536f refactor: decision graph; move validation logic to core; (#78) 2023-08-29 15:38:49 +02:00
stefan-gorules a03978fbe7 feat: add bigjs to function node; (#77)
* feat: add bigjs to function node;

* fix test

* fix tests

* fix fmt
2023-08-28 17:21:31 +02:00
stefan-gorules fb2fb2e43e chore: move unit tests to integration tests; add more test cases; (#33)
* chore: move unit tests to integration tests; add more test cases;

* fix fmt errors
2023-05-03 01:09:53 +02:00