Skip to content

feat: evaluation context - #568

Merged
stefan-gorules merged 8 commits into
release/2.2from
feat/evaluation-context
Nov 17, 2026
Merged

stefan-gorules merged 8 commits into
release/2.2from
feat/evaluation-context

Conversation

@stefan-gorules

Copy link
Copy Markdown
Contributor

No description provided.

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.
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.
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.
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.
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.
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.
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.
@stefan-gorules
stefan-gorules merged commit cd6a3f3 into release/2.2 Nov 17, 2026
60 checks passed
@stefan-gorules
stefan-gorules deleted the feat/evaluation-context branch November 17, 2026 16:37
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants