Skip to content

engine: fix nested array-producing builtin reductions in A2A - #368

Merged
bpowers merged 1 commit into
mainfrom
engine-364-nested-array-builtin-context
Mar 1, 2026
Merged

bpowers merged 1 commit into
mainfrom
engine-364-nested-array-builtin-context

Conversation

@bpowers

@bpowers bpowers commented Mar 1, 2026 •

Copy link
Copy Markdown
Owner

Summary

This PR fixes incorrect interpreter behavior when array-producing builtins are nested inside other builtins in A2A contexts.

The root cause was nested hoisting always rewriting array-producing builtins to TempArrayElement, even when the parent builtin expected an array argument (for example SUM(...)). That collapsed full-array semantics into element-local reads.

Changes

  • Made nested builtin rewriting argument-mode aware (ScalarElement vs ArrayValue).
  • Keep TempArray for array-consuming argument positions and TempArrayElement for scalar-consuming positions.
  • Added regression tests for nested:
    • SUM(VECTOR ELM MAP(...)) in array context
    • SUM(VECTOR SORT ORDER(...)) in array context
    • SUM(ALLOCATE AVAILABLE(...)) in array context

Validation

  • cargo test -p simlin-engine --test compiler_vector

Nested array-producing builtins in A2A expressions were always rewritten to TempArrayElement. That collapsed full-array arguments for reduction builtins like SUM into element-local reads, yielding incorrect interpreter results.

This change makes nested builtin rewriting argument-aware so array-consuming argument positions keep TempArray semantics while scalar positions still use TempArrayElement. Added regression coverage for nested VECTOR ELM MAP, VECTOR SORT ORDER, and ALLOCATE AVAILABLE reductions in array context.

@chatgpt-codex-connector chatgpt-codex-connector Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

💡 Codex Review

Here are some automated review suggestions for this pull request.

Reviewed commit: 386b966826

ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".

Comment on lines +1135 to +1136
BuiltinFn::Max(arg, None) => BuiltinFn::Max(
Box::new(replace_nested_builtins_for_element(

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P1 Badge Treat two-arg MAX/MIN children as scalar

replace_nested_builtins_for_element now marks array-reduction arguments as ArrayValue, but only Max(_, None)/Min(_, None) are explicitly handled; Max(_, Some(_)) and Min(_, Some(_)) fall through other.map(..., arg_mode). In expressions like sum(max(vector_elm_map(...), 15)), that rewrites the nested vector_elm_map to TempArray inside scalar MAX/MIN, and the interpreter later evaluates two-arg MAX/MIN in scalar mode, which cannot consume TempArray and panics (TempArray ... cannot be evaluated as scalar). This regression is introduced by this change and breaks nested reduction formulas that combine SUM with scalar MAX/MIN wrappers.

Useful? React with 👍 / 👎.

@claude

claude Bot commented Mar 1, 2026

Copy link
Copy Markdown

Code Review

Findings

[P3] Unreachable match arms for VectorElmMap, VectorSortOrder, and AllocateAvailable in the Expr::App handler

src/simlin-engine/src/compiler/mod.rs, lines 1256–1328

The explicit match arms for BuiltinFn::VectorElmMap, BuiltinFn::VectorSortOrder, and BuiltinFn::AllocateAvailable inside the Expr::App(builtin, loc) branch are unreachable dead code. The is_array_producing_builtin guard at line 1016 matches exactly these three builtins and returns early (as TempArray or TempArrayElement) before the match expr at line 1032 is ever reached. These arms can never execute, which creates a false sense of coverage and could mislead maintainers into thinking the per-argument ArrayValue/ScalarElement annotations in those arms actually govern how VEM/VSO/AA arguments are processed. Removing them (or adding a comment explaining they're defensive) would clarify the real control flow.


Overall Correctness Verdict

Correct. The core fix — making replace_nested_builtins_for_element argument-mode aware so that array-consuming builtins (SUM, Stddev, Size, single-arg Max/Min/Mean, Rank, VectorSelect) emit TempArray references instead of TempArrayElement — is sound. The is_array_producing_builtin early-return correctly branches on arg_mode, the other => fallback correctly propagates the parent's mode for builtins with uniformly scalar arguments, and all call sites pass ScalarElement as the initial mode. The regression tests cover the targeted patterns (SUM/MAX wrapping VEM/VSO/AA in both scalar and array contexts) with correct expected values. Existing tests and code paths are not broken.

@bpowers

bpowers commented Mar 1, 2026

Copy link
Copy Markdown
Owner Author

This change set fixes the root cause behind issue #364 by preserving full-array semantics when nested array-producing builtins are hoisted in A2A lowering. Previously, nested replacements always became element reads, which caused reductions like SUM(...) over VECTOR ELM MAP, VECTOR SORT ORDER, and ALLOCATE AVAILABLE to collapse incorrectly to per-element values. The compiler now distinguishes scalar-consuming vs array-consuming argument positions so array reducers receive full temp arrays.

I also added focused regression coverage in tests/compiler_vector.rs for these nested reduction patterns in array context, plus sanity checks for nested max/sum behavior. After the update, the simlin-engine compiler-vector test suite is green (cargo test -p simlin-engine --test compiler_vector). This review cycle completed in 1 Codex iteration (Claude sub-review intentionally skipped per request).

@codecov

codecov Bot commented Mar 1, 2026 •

Copy link
Copy Markdown

Codecov Report

❌ Patch coverage is 57.44681% with 60 lines in your changes missing coverage. Please review.
✅ Project coverage is 79.03%. Comparing base (614aa09) to head (386b966).
⚠️ Report is 1 commits behind head on main.

Files with missing lines Patch % Lines
src/simlin-engine/src/compiler/mod.rs 23.07% 60 Missing ⚠️
Additional details and impacted files
@@           Coverage Diff            @@
##             main     #368    +/-   ##
========================================
  Coverage   79.03%   79.03%            
========================================
  Files         142      142            
  Lines       37264    37396   +132     
========================================
+ Hits        29451    29556   +105     
- Misses       7813     7840    +27     

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.
  • 📦 JS Bundle Analysis: Save yourself from yourself by tracking and limiting bundle sizes in JS merges.

@bpowers
bpowers merged commit bb3e977 into main Mar 1, 2026
13 of 14 checks passed
@bpowers
bpowers deleted the engine-364-nested-array-builtin-context branch March 1, 2026 15:44
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.

1 participant