Commit Graph
59 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 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 4a378bafc4 feat(expression): lane VM and typed-mask engine for batch and columna evaluation (#553)
* feat(expression): lane VM and typed-mask engine for batch and columnar evaluation

Adds a register-based lane VM (core/expression/src/lane) and a typed-mask
engine (core/expression/src/typed) that evaluate ZEN expressions over
batches and columns, matching the stack VM's results. zen-types gains
shape-backed objects and dot-path helpers; existing public APIs only grow.

The lane VM has no expression size limit: deep and wide expressions are
compiled and run with the same stack growth the parser uses.

ZEN_EXPRESSION_CONFIG.lane (off by default) switches Isolate::run_standard
and run_unary to the lane VM at runtime. Lane internals and the typed
module are doc(hidden); the documented entry points are LaneProgram,
LaneRunner, Session, Columns and Output.

Includes an executable specification of the expression language
(core/expression/tests/spec, 28,852 cases) with differential tests that
run every lane path against the stack VM.

zen-expression and zen-types now declare rust-version 1.88.

* refactor(types): remove the shape transition cap

Shapes are freed when nothing uses them, so the 4,096-children cap per
shape no longer bounds memory. Shape::with, Shape::of and Shape::without
now always return a shape; objects only switch to a hash map past
SPILL_AT keys, as on master.

* fix(expression): debug-profile stack safety and Windows test build

CI runs tests in the debug profile, where frames are larger and tail
recursion is not optimised. Recursive lane, typed and parser helpers now
grow the stack like the rest (Const::number, the kernel builder, typed
lowering and lookup, lane compile helpers, Node::member_key).

The parser's closure depth counter is u32 instead of u8, so 256 nested
closures no longer wrap. median([]) and empty templates no longer
overflow in debug builds; release results are unchanged.

The deep/wide test runs on a 256 KiB stack so unprotected recursion
fails quickly in any profile. The conformance helper compares with
as_str(), which is unambiguous on Windows.

* fix(expression): stack checks in the lane executor, LF test data on Windows

In x86-64 debug builds Executor::cold has a ~93 KB frame, so nested
closures (execute -> cold -> body) could use more stack between two
checks than the stack-growth margin and overflow. execute and cold now
check the stack themselves; both run per instruction batch, not per row.

The expression test data is checked out with LF on every platform.
Windows runners converted it to CRLF, which broke multi-line spec cases.
2026-10-28 11:39:54 +01:00
stefan-gorules 39916c9d18 feat: date input (#529)
* feat: declared dates as date values

- graph format:date/date-time inputs and policy date properties arrive as
  dates that keep their original text for output, string() and string
  functions
- d() keeps the instant of offset/Z strings, resolves DST gaps and accepts
  ISO forms without offset, compact, slash-separated, YYYY-MM and YYYY
- dates compare with date strings in ==, !=, <, >, in and contains, and
  concatenate with strings; deprecated date helpers accept dates
- string(date), type() reports date, and date-to-string at string inputs

* fix: close review gaps in date inputs

- parse new date formats by exact shape; keep master formats first and
  guard DST-gap and extreme-year overflows
- text-carrying dates act as strings for object keys, in-object, index,
  slice, concatenation and bool
- deprecated helpers fall back to the date instant; time() accepts dates
- schema-less child inputs receive date text; dates satisfy string
  parameters and slicing in the type checker
- convert policy input before validation so dates are parsed once;
  share rewrite helpers between graph and policy conversion

* fix: align date text, unions, references and child inputs

- every valid date exposes a text form (original or RFC3339), so string
  functions and slicing on computed dates match the type checker
- date and string types merge to string; reference ids typed as dates
  resolve through their text
- input nodes convert declared dates and hand every other converted date
  back as its original text, following combinators only for structure
- date format checks no longer allocate
2026-09-29 09:59:44 +02:00
stefan-gorules ffdfd807d1 feat: implement slots instead of natural language (#527)
* feat: implement slots instead of natural language

Replace the natural-language projection (`nl`) with engine-owned slot
completion: for a cursor position the engine classifies the slot being
edited (operand, operator, literal, member access, ...) and returns typed
options, literal facts and the expected type, for policies and graphs.

- zen-expression: new `slot` module (classification, literals, operators)
  and a lenient lexer mode for incomplete input; `nl` removed
- Intellisense: typed diagnostic codes with arguments, richer inspect for
  hover, dedicated parser error variants (messages unchanged)
- Engine: `Workspace::cursor_scope`, `slot`, `facts`; `rename_from` and
  `references_from`; editor spans in UTF-16 units; renames reach imported
  policies, `$nodes` reads, child graphs and output column heads; no `$root`
  completion in policies
- Node bindings: `slot`, `facts`, `cursorScope`, `slotBatch` replace
  `nl`, `nlTokenize`, `nlEncodeString`, `nlTokenizeBatch`

No change to expression evaluation: the VM, compiler, functions and the
default lexer path are untouched, and parser error messages keep their text.

* fix: harden slot classification, scopes and graph expectations

- restore master execution order; slot scopes hide writes of the edited
  block and its dependents instead of relying on block ranking
- token-start editing gated by slot state; operator, list and operand
  spans no longer splice into neighbouring tokens
- interval-aware bracket pairing for half-open and reversed intervals
- lenient lexer recovers from unknown characters (slots only)
- graph expected type follows switch and pass-through nodes and merges
  compatible output schemas
- sibling inference caps distinct values and analyses lazily
- deterministic overload return type display

* fix: order policy blocks by entity reads through closures and relationships

- record reads for `#.field` in closures so entity fields read through
  lists become dependencies
- link reads through derived lists (filter/map results) to the entity
  fields they carry and order the list after those fields
- evaluate entity blocks on single relationships, and demand entity
  paths for plain reads through relationships
- slot scopes keep the top-level variable when hiding written fields
2026-09-28 21:47:40 +02:00
stefan-gorules 0b6c6a709a fix: nested scopes and any cascade (#513) 2026-08-20 17:39:45 +02:00
stefan-gorules 792e77a82e fix: panic hardening (#509) 2026-08-19 12:04:15 +02:00
stefan-gorules 32f7fb875c fix: end of week boundary (#508) 2026-08-19 11:15:21 +02:00
stefan-gorules 13722740a1 fix: number out of range panics (#506) 2026-08-19 11:01:36 +02:00
stefan-gorules ed0c286d9f fix: v1 behaviour hardening (#502) 2026-08-07 10:30:43 +02: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 1cb74e6f59 feat: add date natural language (#493)
* feat: add date natural language

* add coverage for date methods
2026-07-22 15:55:43 +02:00
stefan-gorules 8d1910cc4e feat: graph workspace and static analysis (#483) 2026-07-16 23:42:13 +02:00
stefan-gorules 6858c1bd04 feat: natural language (#477)
* feat: natural language

* fix cargo fmt
2026-07-07 13:12:46 +02:00
stefan-gorules 602d214b96 feat: expression static analysis (#449)
* feat: expression static analysis

* fix: cargo fmt

* fix: python workflow

* fix: python workflow

* fix: python workflow

* fix: ignore miri test bound by clock

* fix: memory leaks
2026-06-22 15:48:05 +02:00
stefan-gorules 53f3b5a430 feat: merge function (#438)
* feat: merge function

* add mergeDeep function

* default merge to return empty object instead of empty array
2026-03-15 14:44:32 +01:00
stefan-gorules 73d5d5c6cb feat: function scope alias (#426)
* feat: function scope alias

* fix types
2026-01-19 10:44:16 +01:00
stefan-gorules f2fcb95ad9 feat: assignment (#366)
* feat: assignment

* add tests
2025-06-18 10:43:01 +02:00
stefan-gorules bdf51dec11 fix: safe division (#365)
* fix: safe division

* add test
2025-06-16 09:47:35 +02:00
stefan-gorules 815729aca4 fix: update rounding strategy (#361)
* fix: update rounding strategy

* add test
2025-06-03 12:04:47 +02:00
stefan-gorules a911a623cb fix: align date diff with dayjs (#360) 2025-06-03 09:48:57 +02:00
stefan-gorules 29ce060ac7 feat: number scientific notation (#355)
* feat: number scientific notation

* add tests
2025-05-23 11:46:33 +02:00
stefan-gorules 34de232d3a feat: dates min/max (#353) 2025-05-23 11:18:50 +02:00
stefan-gorules d179552b3c feat: trunc function and decimal places for round (optional) (#349) 2025-05-14 08:21:29 +02:00
stefan-gorules 6027f5a6a1 fix: date endOf for a year and isAfter comparison for units (#348)
* fix: date endOf for a year and isAfter comparison for units

* fix test

* improve impl
2025-05-14 01:27:58 +02:00
stefan-gorules de6d13e1c7 feat: date v2 (#345)
* feat: date v2

* implement more date functions

* improve type safety

* add more methods for dates

* update utc to tz and add support

* add unary node behaviour for date methods

* add tests; fix errors in bindings

* add date operators

* fix types

* change set date signature

* update nightly
2025-05-12 22:39:08 +02:00
stefan-gorules 6535d44e03 feat: expression function system (#340)
* feat: expression function system

* cleanup

* improve parser

* revert test changes

* add more tests

* minor fixes
2025-04-28 00:40:32 +02:00
stefan-gorules 36c32814b8 feat: interval iterator (#336) 2025-04-24 12:00:15 +02:00
stefan-gorules ae40aff163 feat: compiled bytecode (#307)
* feat: compiled bytecode

* optimize bytecode

* add compiled expression struct

* add fetch fast and better abstraction

* fix

* rename evaluate_with for Expression<Unary>
2025-02-10 10:07:24 +01:00
stefan-gorules df2a44f8b3 fix: unary closure expression (#301) 2025-01-20 14:08:13 +01:00
kyle-mwnz 49814e1aa0 feat: add trim function to string data type (#279)
* feat: add trim function to string data type

* Linting

* Extra test
2024-11-28 10:15:22 +01: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 3e7cdfcde1 feat: intellisense (#244)
* local: fault tolerant parser

* fix tests

* add intellisense

* fix minor issues

* improve support

* fix fmt

* fix

* fix: improve error messages

* improve error handling and ast

* fix lint

* fix tests

* exclude zen-nodejs from rust tests
2024-09-23 09:23:48 +02:00
stefan-gorules a903982e17 fix: empty object (#241) 2024-09-10 15:48:56 +02:00
stefan-gorules c9d1e9cc32 feat: object support (#226)
* feat: object support

* add object tests

* fix fmt

* fix: fmt
2024-08-08 10:39:32 +02:00
stefan-gorules 7cb676d36a feat: template string (#225)
* feat: template string

* add tests

* fix cargo fmt
2024-08-07 09:50:49 +02:00
stefan-gorules e452df33e9 feat: implement new functions - part 1 (#205)
* feat: implement new functions - part 1

* add enumiter to BuiltInFunction

* fix ci

* remove centos test

* fix workflow
2024-07-04 16:39:58 +02:00
Brian ThorneandBrian Thorne 59371349c3 test: add a few basic tests around numerics (#192)
Co-authored-by: Brian Thorne <brian@partly.com>
2024-06-28 13:52:40 +02:00
stefan-gorules 6271d8ab1a feat: expression chain (#166)
* feat: expression chain

* fix implementation

* fix

* implement env update on expression execution

* fix error handling for serde

* rebase

* update deps

* add $ prefix for expression reference
2024-05-24 14:46:57 +02:00
stefan-gorules 7ce6ceb9e4 feat: fuzzy match (#168) 2024-05-23 12:00:58 +02:00
Ivan Miletic ae68b4a0dd feat: keys function (#135)
* feat: keys function
2024-03-25 16:30:59 +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
stefan-gorules 807537ad77 feat: is numeric function; (#95) 2023-10-13 17:10:42 +02:00
stefan-gorules 7b36527dc7 feat: type conversions; (#94) 2023-10-13 11:23:08 +02:00
stefan-gorules 615d554215 fix: string slices; remove int variable type; (#92) 2023-10-04 08:41:24 +02:00
stefan-gorules 78d8d2632a feat: date functions; (#91)
* feat: date functions;

* add tests
2023-09-21 16:19:43 +02:00
stefan-gorules 7b17575a66 fix: negative value checks (#89)
* fix: negative value checks;

* offset unary fix;

* disable unary csv test;
2023-09-19 11:23:31 +02:00
stefan-gorules 7283047f49 fix: remove missing date fns; (#71) 2023-08-14 01:00:19 +02:00
stefan-gorules e4b2691a06 feat: standardise operators; (#65) 2023-07-28 11:02:25 +02:00
stefan-gorules 404e91dc59 fix: dual arity argument parsing; (#55) 2023-07-18 18:58:04 +03:00