Skip to content

engine: VM and wasm getValue disagree after a run reaches its end #634

Open

Description

@bpowers

Once a run reaches its last saved row, the VM and the wasm backend rest at different times, so getValue after the run returns different numbers depending on the backend.
The VM evaluates every step up to the target and rests one step past it, so after a complete run its live values are at FINAL_TIME + DT, with flows and auxiliaries evaluated there.
The wasm blob stops at its last saved row and rests there, at FINAL_TIME.
We need to pick one behavior and make both backends follow it.

Example

Run from 0 to 10 with DT 1, then read a value:

call VM rests at wasm rests at
run_to(9) 10 10
run_to(10) 11 10
run_to(12) 11 10

With a save step of 4 (rows at 0, 4, 8) they already differ at run_to(8): the VM rests at 9, the blob at 8.
Before both backends reach the last saved row, they agree bit for bit.

Why it matters

@simlin/engine lets a host pick either backend, and reading a value with getValue after runToEnd is a natural thing to do.
A value at FINAL_TIME + DT is outside the simulated range, which is surprising.
Seen only in tests, not reported by a user.

Fix

Recommended: make the VM rest at its last saved row (FINAL_TIME) once the run is over, matching the blob.
The VM's current choice exists so the clock never moves backward between calls, so check that run_to followed by set_value and a resumed run still behave sensibly.
Alternative: make the blob step one past the end like the VM. That changes what the blob's exports mean to a host.

Pointers

  • src/simlin-engine/src/vm.rs: run_steps and the comment on the resting row after its loop
  • src/simlin-engine/src/wasmgen/module.rs: the resting-row comment in emit_run_to
  • src/simlin-engine/src/wasmgen/module_tests.rs: where_a_run_rests_on_each_backend tables both behaviors and should collapse to one column

Activity

  1. added
    engine
    bug
    on Jun 8, 2026
  2. changed the title [-]engine: VM vs wasm diverge on getValue after run_to(target) past FINAL_TIME[/-] [+]engine: VM and wasm getValue disagree after a run reaches its end[/+] on Oct 12, 2026
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

    bugengine

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions