Skip to content

Bug: ExpressionVisitor cross-submission capture crash in Polyglot Notebooks (.NET Interactive) #2

Closed

Description

@jsboige

Context

Discovered while teaching with Z3.Linq inside Polyglot Notebooks (.NET Interactive), where each notebook cell compiles as a separate "submission". The crash does not reproduce in a console app or the xUnit test suite (single assembly / single scope) — which is why it has stayed invisible.

Repro

A Theorem<T> over a nested int[][] model where the bounds are declared in one notebook cell and .Solve() is called in another cell (i.e. the constraint lambdas capture variables from a different submission) raises:

System.ArgumentException: Field 'X' defined on type 'Submission#N' is not a field
on the target object of type 'Submission#M'

Two int[][] solves in the same cell work fine — the crash only appears on cross-submission capture.

Root cause

ExpressionVisitor.VisitMember (Z3.Linq ExpressionVisitor.cs, ~L461) resolves host-captured constants by reflection: field.GetValue(target). When a constraint lambda captures a variable declared in a different .NET Interactive submission (or a different closure scope), the target object does not carry that field → ArgumentException.

Proposed fix (direction)

PartialEvaluator.PartialEval the constraint bodies inside Theorem.AssertConstraints before the Z3 visit — fold the captured constants into literals in the expression tree. This neutralizes the reflective cross-scope resolution. Needs validation against the full endjin test suite + the fork's own tests.

Happy to open a PR for this.

Workaround (current, in the pedagogical notebooks)

Keep the bounds declaration, the Where(...) constraint lambdas, and .Solve() in the same notebook cell — so no constant is captured across submissions.

Where this is exercised

The pedagogical series using the fork (CoursIA SMT/Z3/ notebooks) hits this pattern in the hierarchical meal-planner notebooks (int[][] multi-day plans). The same notebooks are candidates for upstream endjin/Z3.Linq samples once cleaned up (flagged in the ongoing conversation on endjin#29).

/cc @jsboige

Activity

  1. jsboige commented on Jun 24, 2026

    @jsboige
    Author

    Update — root-cause investigation (2026-06-24)

    Investigated the fork source firsthand. The root cause is narrower and more structural than the original report.

    SimplifyLambda is vestigial

    ExpressionVisitor.VisitMember (L439-488) has this comment at L447:

    // We only ever get here if SimplifyLambda is set to false, otherwise partial evaluation does it earlier

    But SimplifyLambda does not exist as a property or mechanism anywhere in the current fork — it's a stale comment carried over from an earlier endjin revision. So the reflective branch (field.GetValue(target) / property.GetValue(target), L461/L465) is always active whenever a MemberExpression resolves to a captured constant outside the theorem's parameter.

    Why the test suite is green but notebooks crash

    The xUnit suite (Z3.Linq.Tests, 6/6 passing — verified local baseline) captures loop indices in locals (int i1 = i; theorem = theorem.Where(g => g.Cells[i1][j1]...), see CollectionHandlingTests.cs). In a single assembly, field.GetValue(target) succeeds: the compiler-generated closure field lives on a type in the same assembly, so reflection finds it.

    In Polyglot Notebooks (.NET Interactive), each cell compiles to a separate dynamic assembly (Submission#0, Submission#1, ...). When a constraint lambda in cell N captures a variable declared in cell M (M != N), the target object is a closure instance typed as Submission#M, but the FieldInfo was resolved against Submission#N → field.GetValue(target) throws ArgumentException: Field 'X' defined on type 'Submission#N' is not a field on the target object of type 'Submission#M'.

    PartialEval is already used in the visitor — just not on constraint bodies

    PartialEvaluator.PartialEval is invoked at ExpressionVisitor.cs:177, :229, :344 — but only on array-index sub-expressions and a substitution case, never on the whole constraint body. Theorem.AssertConstraints (Theorem.cs:149-190) calls ExpressionVisitor.Visit(..., constraint.Body, ...) directly at L176 with no prior partial evaluation.

    Proposed fix (concrete, surgical)

    In Theorem.AssertConstraints, fold captured constants before visiting:

    // Theorem.cs, inside the foreach at L174-189
    var body = PartialEvaluator.PartialEval(constraint.Body, ExpressionInterpreter.Instance);
    BoolExpr expression = (BoolExpr)ExpressionVisitor.Visit(context, environment, body, constraint.Parameters[0]);

    This rewrites captured-constant member accesses into literal ConstantExpressions before VisitMember's reflective branch ever runs — neutralizing the cross-assembly reflection. PartialEvaluator/ExpressionInterpreter are already referenced in the same file's namespace, so no new dependency.

    Repro challenge

    The crash is specific to .NET Interactive's per-cell dynamic-assembly model — it does not reproduce in a single-assembly xUnit test (which is why it stayed invisible). Faithfully reproducing it requires either a Polyglot Notebook execution or a dynamically-compiled multi-assembly repro. The fix can be validated by: (1) the existing 6/6 suite stays green (PartialEval is a no-op on same-assembly captures — same result), and (2) a Polyglot Notebook cross-cell repro stops crashing.

    Happy to open the PR with the one-line AssertConstraints change + a notebook repro if you want it.

  2. jsboige commented on Jun 24, 2026

    @jsboige
    Author

    Closed by #3 (merged fc44dfa).

    Fix summary: Theorem.AssertConstraints now calls PartialEvaluator.PartialEval(constraint.Body, ExpressionInterpreter.Instance) before ExpressionVisitor.Visit, folding host-captured constants into literal ConstantExpressions. This sidesteps the reflective field.GetValue(target) resolution in ExpressionVisitor.VisitMember (~L461) that crashed across .NET Interactive's dynamic-assembly boundaries.

    Validation:

    • dotnet test Z3.Linq.Tests: 6/6 passing (no regression — PartialEval is a no-op for same-assembly captures, which is why the xUnit suite never caught the bug).
    • Polyglot regression test solutions/polyglot-repro/CrossSubmissionCaptureRepro.ipynb: cross-submission capture now solves 7,8,9 instead of raising ArgumentException: Field 'b0' ... Submission#N ... Submission#M.

    Root-cause analysis + fix direction originally posted in this issue (comment 2026-06-24); the fix was implemented and empirically validated in PR #3. CoursIA submodule pointer bumped in jsboige/CoursIA#4124.

    Thanks to the root-cause note about SimplifyLambda being vestigial (only a stale comment at ExpressionVisitor.cs:447) — confirmed firsthand: the reflective path is always active, which is exactly what PartialEval neutralizes.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions