Skip to content

Hotfix: reject array unknowns in JumpProblem, which gave silently incorrect results - #5429

Open
isaacsas wants to merge 5 commits into
SciML:masterfrom
isaacsas:mtkbase/jumps-reject-array-unknowns
Open

isaacsas wants to merge 5 commits into
SciML:masterfrom
isaacsas:mtkbase/jumps-reject-array-unknowns

Conversation

@isaacsas

@isaacsas isaacsas commented Nov 21, 2026 •

Copy link
Copy Markdown
Member

#5131 (ModelingToolkitBase 1.72.0, September) allowed unscalarized array unknowns in completed systems for every problem type, but did not update or test the jump handling for that case. Since then, JumpProblems of systems with an array unknown have given silently incorrect results whenever the array comes before other unknowns: the jump dependency maps are indexed by position in unknowns(sys) instead of in the flat state, so aggregators that use them, such as NRM and SortingDirect, miss rate updates, and jump affects on an unknown after the array change the wrong state entry, even with Direct.

As a hotfix, JumpProblem now rejects systems with unscalarized array unknowns, as every problem constructor did before 1.72.0. The error message suggests scalarizing them when building the system, for example by passing [A; collect(X)] instead of [A, X] as the unknowns (mtkcompile keeps the array unknowns of a pure jump system, so it doesn't help there), and writing any use of a whole array in a jump rate or affect in terms of its elements, such as X[1] + X[2] for sum(X): a rate using sum(X) gets no dependency on the scalarized elements, so it could still give stale rates (#5428). The check runs even with check_compatibility = false, since it guards against wrong results. Systems it accepts are handled exactly as on master.

It also replaces the @test_nowarn in the testset "Proper substitution in JumpSysMajParamWrapper" with a check of the mass action rate the substitution gives (exp(p)). The @test_nowarn fails the InterfaceII tests on master: JumpProblem(::System, ...) passes the dep_graph, vartojumps_map and jumptovars_map keywords, which JumpProcesses 10.2 deprecates, and the deprecation warning trips it. It came with a fix to the then-symbolic substitution of mass action rates (ae72b4a), which made building the problem fail; the rates are now compiled, so warnings no longer say anything about it. JumpProcesses compat stays at "10", and moving to JumpDependencies is left for later.

Other parts are still broken and need more work; #5428 has the details and reproducers. In particular, event affects and conditions give wrong results in completed systems with array unknowns, also without jumps, and some jump rates get no dependencies, such as rates using an array as a whole (sum(X)) or an observed variable. Supporting array unknowns in jump systems, which would let this check go, is also left for the follow-up there.

Supersedes #5425.

Tests

The new testset checks that JumpProblem rejects a pure jump system and a hybrid system with an array unknown, also with check_compatibility = false. With the unknowns scalarized, it checks over three runs per aggregator (the default, Direct, NRM, SortingDirect, RDirect, DirectCR, CCNRM, RSSA, RSSACR and Coevolve) that when two mass action jumps compete for the single X[2], the losing jump's rate and that of a constant rate jump with rate k * (X[1] + X[2]) drop to zero. A second new testset checks the three dependency maps directly against hand-derived ones for array unknowns scalarized by hand, with elements of a vector and of a matrix before and after a scalar unknown.

jumpsystem.jl passes locally on Julia 1.13.3 with JumpProcesses 10.2.0 and --depwarn=yes (as under Pkg.test).

🤖 Generated with Claude Code 2.1.328 (model: claude-opus-5-5)

Since SciML#5131 (ModelingToolkitBase 1.72.0) a `complete`d system may have
unscalarized array unknowns, but the jump dependency maps and the compiled
jump affects index the state by position in `unknowns(sys)` instead of the
flat state, which gave silently wrong results (SciML#5428). As a hotfix,
`JumpProblem` rejects such systems again, as before 1.72.0.

Jump rates are now scalarized before their dependencies are collected, so a
rate using an array as a whole, e.g. `sum(X)`, depends on its scalarized
elements.

The `@test_nowarn` in "Proper substitution in `JumpSysMajParamWrapper`",
which caught JumpProcesses 10.2's deprecation warning for the dependency map
keywords, is replaced with a check of the substituted rate.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@ChrisRackauckas-Claude

Copy link
Copy Markdown
Member

🤖 Automated comment from an AI agent running as @ChrisRackauckas — not written or reviewed by Chris.

PR: #5429

Reviewed head: 2052975
Raw SHA-256: 6503bbd8bb1ce65910ea78dd632856079d80e1189a7179d98aafdc68838bd258
Source SHA-256: 419ef4c7772a5fb6cee8bc5a78726495da87510d1800a98c9fbeed01680ae0ca
Source: standard-review-v1 (harness-log)
Reviewer session: 01a1ff2c-14c1-79e2-b7ce-0a4b7d50e892
Slot: 1
Confidence: unknown
Own risk fixed before author rating/discussion: yes
Redaction: ran (substitutions=0)

Risk assessment

  • Risk: high (H1)
  • Verdict: CHANGES
  • Reviewed head: 2052975

Review report

VERDICT: CHANGES
Round: 1
Head: 2052975
Slot: 1
CI: noregress
Session: 01a1ff2c-14c1-79e2-b7ce-0a4b7d50e892
Reviewer: codex-cli 0.170.0; model: gpt-6.2-sol

Risk

high (H1: the public constructor now rejects an array-state model that solved correctly on the base)

Blocking findings

  • Blocking priorities: P1, P2, P2
  • [P1] lib/ModelingToolkitBase/src/problems/jumpproblem.jl:20 rejects a working graph-free path. With unknowns [A, X], X(t)[1:2], one mass-action jump consuming X[2] and producing A, and Direct(), the base solved from [0, 0, 1] to the expected [1, 0, 0] with Success. The head throws the new ArgumentError. Preserve and regression-test the event-free, mass-action-only path that uses flattened stoichiometry without dependency maps. If dropping that path is intentional, document the restriction and migration in the public API and release notes and ship it as a SemVer major change. ModelingToolkitBase remains at 1.81.0 in this diff.
  • [P2] lib/ModelingToolkitBase/test/jumpsystem.jl:646 drops the warning assertion while the warning persists. With the package runner's --depwarn=yes and JumpProcesses 10.2.0, the original mapper test failed on the base. The head's rate assertion passed while emitting the same warning about the legacy dependency-map keywords forwarded at lib/ModelingToolkitBase/src/problems/jumpproblem.jl:157. The original test passed with 10.1.1 in an otherwise identical base environment. Keep the warning check and address the deprecation through the public JumpDependencies API in a separate compatibility change.
  • [P2] lib/ModelingToolkitBase/src/problems/jumpproblem.jl:169 adds a call to a private dependency API. With Symbolics 7.49.0, Base.ispublic(Symbolics, :isarraysymbolic) returned false. Use the owning package's public SymbolicUtils.symtype(u) <: AbstractArray for the symbolic unknowns.

Non-blocking findings

  • lib/ModelingToolkitBase/src/problems/jumpproblem.jl:256 still misses whole-array dependencies hidden behind a callable parameter. With scalarized X, an NRM jump with rate h(X) and h => sum incremented A from 0 to 1 after the only particle was consumed, when the rate should have been zero. Both revisions returned an empty dependency list for that rate and solved with Success. Conservatively map a whole-array dependency to its scalar elements to cover this case.
  • lib/ModelingToolkitBase/src/problems/jumpproblem.jl:164 records version and issue history in the source comment. Keep the indexing invariant. The added lines include 13 comment lines out of 66.
  • lib/ModelingToolkitBase/test/jumpsystem.jl:649 reads the aggregator's internal maj_rates field. Prefer testing the parameterized rate through public behavior when strengthening this mapper test.

What I ran

  • Julia 1.12.4 with the same resolved dependencies for the base and head comparisons. GROUP=ModelingToolkitBase_InterfaceII julia --project=lib/ModelingToolkitBase --startup-file=no -e 'using Pkg; Pkg.test()' exited 0 on the head. JumpSystem passed 4,403/4,403, Poissonians 123/123, and Extend SDE/Jump 1,917/1,917. The group reported two existing broken assertions in Optimal Control. The standalone jumpsystem.jl run passed 4,401/4,401.
  • Ran the verbatim new array testset with a fresh StableRNG(12345). The base passed 102/123, with three missing throws and 18 failures of the assertion that B stays fixed after depletion. The head passed 123/123.
  • In a bounded one-jump probe with [A, X, B], rate k * X[2] * B, and affect B ~ 0, the base returned Success with final flat state [0, 0, 0, 1]. It zeroed X[2] instead of B. The head rejected the raw array. Both revisions with scalarized unknowns returned the expected [0, 0, 1, 0].
  • Constructor probes confirmed rejection before the ODE and SDE branches. A compiled scalar hybrid, an array-parameter model after remake, and a manually indexed numeric DiscreteProblem jump passed on the head. Catalyst 17.0.1's scalar A → B network ended at A=0, B=1. A model with array-valued observed Y constructed on the head, but solving that fixture raised the same invalid-index error on both revisions.
  • Ran the extracted mapper test with --check-bounds=yes --depwarn=yes. The base failed 1/1 with JumpProcesses 10.2.0 and passed 1/1 with 10.1.1 in the dependency bisection. The head passed 1/1 with 10.2.0 while emitting the warning.
  • git diff --check, Runic in check mode and typos passed. uv run --locked python --version printed Python 3.13.13 and warned that --locked has no effect outside a project. The checkout has no uv.lock.

What I did not verify

  • Additional Julia versions, the full downstream suites, and Catalyst array-species models.
  • A larger raw-array NRM stress probe was interrupted after it exceeded 18 GiB of resident memory. The bounded reproducer and the regression testset both ran to completion.

AUTHOR_GRADE: {"task":"bugfix","score":3,"difficulty":3,"rounds":1,"author_harness":"unknown","author_model":"unknown","note":"The new testset discriminates both failure modes and the guard stops a reproduced wrong state write. The guard also rejects a correct mass-action model and the test replacement drops a warning assertion while the warning persists."}

Head link: 2052975


🤖 Posted by an AI agent — harness: codex-cli 0.170.0 · model: gpt-6.2-sol
Conversation: local session 01a1ff2c-14c1-79e2-b7ce-0a4b7d50e892 (no session URL)

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@isaacsas

Copy link
Copy Markdown
Member Author

On the points of the automated review above:

  • Rejecting array-unknown models that happened to give correct results (P1). This is intended. Which of these models are correct depends on details a user can't see: the aggregator (the default switches from Direct to RSSA at 20 jumps, and then uses the mis-indexed dependency maps), and whether any constant or variable rate jump, or any event, touches an unknown that comes after the array (cases 1 to 4 in Since #5131, unscalarized array unknowns give silently wrong jump and event results #5428). A narrower check would have to enumerate the safe paths, and any path it missed would stay silently wrong. The error message gives the workaround, scalarizing the unknowns, and the follow-up in Since #5131, unscalarized array unknowns give silently wrong jump and event results #5428 restores support.
  • Keeping the @test_nowarn (P2). Keeping it leaves InterfaceII failing until JumpProblem(::System, ...) passes its maps in a JumpDependencies, which needs JumpProcesses 10.2 as the lower bound. This PR keeps the lower bound at 10 so that the fix reaches users on any 10.x. The warning only appears with deprecation warnings enabled, and the testset is about the substitution of the mass action rate, not warnings, so it now checks the substituted rate.
  • Symbolics.isarraysymbolic is not public (P2). ModelingToolkitBase uses it in 11 other places, so the check follows existing practice there.
  • A rate that applies a function to a whole array, such as h(X) with a function-valued parameter h, still gets no dependency on the elements of X, since scalarize can't expand it. I reproduced it (NRM and SortingDirect keep the stale rate). It isn't caused by Support array unknowns on a completed system for every problem type #5131, and fixing it means mapping a whole-array dependency to all of its elements, so I added it to Since #5131, unscalarized array unknowns give silently wrong jump and event results #5428 for the follow-up.
  • Version history in the code comment. Dropped in 94f6ef1; the reference to Since #5131, unscalarized array unknowns give silently wrong jump and event results #5428 stays.
  • The test reads maj_rates. jumpsystem.jl reads discrete_jump_aggregation.maj_rates in 11 other places, so the test follows the file's convention.

🤖 Generated with Claude Code 2.1.328 (model: claude-opus-5-5)

@isaacsas isaacsas changed the title Hotfix: reject array unknowns in JumpProblem, which gave silently incorrect results WIP: Hotfix: reject array unknowns in JumpProblem, which gave silently incorrect results Nov 21, 2026
isaacsas and others added 2 commits November 21, 2026 15:30
Scalarizing the whole rate also expanded array parameters, so a rate such
as `p[i] * A` with a parameter index `i` failed to build. Go back to
master's dependency collection; the test's constant rate jump now names
both elements of `X`.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@isaacsas isaacsas changed the title WIP: Hotfix: reject array unknowns in JumpProblem, which gave silently incorrect results Hotfix: reject array unknowns in JumpProblem, which gave silently incorrect results Nov 21, 2026
…ment

A rate such as `sum(X)` gets no dependency on the scalarized elements of
`X`, so it could still give stale rates with NRM after the suggested
scalarization.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@ChrisRackauckas-Claude

Copy link
Copy Markdown
Member

🤖 Automated comment from an AI agent running as @ChrisRackauckas — not written or reviewed by Chris.

PR: #5429

Reviewed head: 9c90bc4
Raw SHA-256: f2d27e9a2182de1824ece062abd5a80ae6b833c75af82a7807508447a894ba21
Source SHA-256: ab9de61efcd5d8c13f636df4b84d47e253b7e76b1f2a08f61eb8b04bb2226226
Source: standard-review-v1 (harness-log)
Reviewer session: 01a20078-3f16-7010-9ce2-57ab986ef3a8
Slot: 1
Confidence: unknown
Own risk fixed before author rating/discussion: yes
Redaction: ran (substitutions=0)

Risk assessment

  • Risk: high (H1)
  • Verdict: CHANGES
  • Reviewed head: 9c90bc4

Review report

VERDICT: CHANGES
Round: 2
Head: 9c90bc4
Slot: 1
CI: noregress
Session: 01a20078-3f16-7010-9ce2-57ab986ef3a8
Reviewer: codex-cli 0.170.0; model: gpt-6.2-sol

Risk

high (H1: the public JumpProblem constructor rejects a previously working array-state model)

Blocking findings

  • Blocking priorities: P1, P1, P2, P2
  • [P1] lib/ModelingToolkitBase/src/problems/jumpproblem.jl:256 drops the dependencies of a whole-array rate even when the unknowns are scalarized. With [A; collect(X)], X = [0, 1], and StableRNG(12345), an NRM model consumed its only particle and then incremented A at rate k * sum(X), which should have been zero. The head returned Success with A going from 0.0 at depletion to 1.0. The round-one head extracted dependencies [2, 3] and kept A at 0.0. The base and current head produced an empty dependency list. The delta also switches the test at lib/ModelingToolkitBase/test/jumpsystem.jl:321 to explicit element addition, which passes on both the base and head. Recover scalar-element dependencies for supported whole-array reductions and keep a solve regression that uses sum(X).
  • [P1] lib/ModelingToolkitBase/src/problems/jumpproblem.jl:20 still rejects an event-free mass-action model whose aggregator requires neither dependency map. With unknowns [A, X] and a single Direct() reaction consuming X[2] and producing A, the base returned Success and final state [1.0, 0.0, 0.0]. Both review heads threw ArgumentError during construction. Restrict rejection to configurations that depend on the incorrect indexing and regression-test the working mass-action path. Removing the working path would require a documented breaking release.
  • [P2] lib/ModelingToolkitBase/test/jumpsystem.jl:677 removes the warning check from the mapper test. With --depwarn=yes and JumpProcesses 10.2.0, the original @test_nowarn failed on both the base and head. The replacement passed 1/1 while emitting the same warning about the dependency-map keywords passed at lib/ModelingToolkitBase/src/problems/jumpproblem.jl:158. Keep the warning assertion and handle the public JumpDependencies migration in a separate compatibility change.
  • [P2] lib/ModelingToolkitBase/src/problems/jumpproblem.jl:169 calls a private dependency API. With Symbolics 7.49.0, Base.ispublic(Symbolics, :isarraysymbolic) returned false. Use the public SymbolicUtils.symtype accessor and test whether the symbolic unknown's type is an AbstractArray.

Non-blocking findings

  • lib/ModelingToolkitBase/test/jumpsystem.jl:680 checks the internal discrete_jump_aggregation.maj_rates field. Verify the mapped rate through the first firing time under a fixed RNG, using an independently constructed numeric mass-action jump with rate exp(1.0) as the reference.
  • lib/ModelingToolkitBase/src/problems/jumpproblem.jl:164 still includes historical wording and an issue pointer. Keep the present-tense indexing invariant that explains why the check is needed.

What I ran

  • GROUP=InterfaceII JULIA_PKG_PRECOMPILE_AUTO=0 julia --project -e 'using Pkg; Pkg.test()' from a scratch copy of lib/ModelingToolkitBase with Julia 1.12.4 exited 0. JumpSystem passed 4,407/4,407, Poissonians passed 123/123, and Extend SDE/Jump passed 1,917/1,917. Optimal Control reported 31 passes and two existing broken assertions.
  • The standalone jumpsystem.jl suite also passed 4,407/4,407 with --check-bounds=yes --depwarn=yes. The two new testsets passed 127/127 on the head and 124/127 on the base. The three base failures were missing constructor throws. The explicit-element dependency graph test passed 4/4 on both.
  • Ran the bounded constructor, NRM rate, and mapper probes with the test runner's resolved dependency versions across the compared revisions, including Symbolics 7.49.0 and JumpProcesses 10.2.0. In a separate Direct() affect probe on the base, B ~ 0 incorrectly zeroed X[2], yielding Success and [0.0, 0.0, 0.0, 1.0] where [0.0, 0.0, 1.0, 0.0] was expected. The current head rejected that raw-array model.
  • git diff --check, Runic 1.10.0 in check mode, and typos on the changed files exited 0. The supplied delta matched the local Git diff. The checkout was clean after testing.
  • uv run --locked python --version reported Python 3.13.13 and warned that --locked has no effect outside a project. The checkout has no uv.lock.
  • codex --version reported codex-cli 0.170.0. The session in the header comes from the running Codex harness.

What I did not verify

  • Additional Julia versions, downstream suites, and remote CI.
  • The callable-parameter probe failed before solving on all three revisions with MethodError: no method matching allocatedinline(::Core.TypeofVararg).

AUTHOR_GRADE: {"task": "bugfix", "score": 2, "difficulty": 3, "rounds": 2, "author_harness": "unknown", "author_model": "unknown", "note": "The new tests exercise the rejection guard and explicit-element graphs. A working Direct input is rejected and the round-two delta restores a stale NRM whole-array rate."}

Head link: 9c90bc4


🤖 Posted by an AI agent — harness: codex-cli 0.170.0 · model: gpt-6.2-sol
Conversation: local session 01a20078-3f16-7010-9ce2-57ab986ef3a8 (no session URL)

This branch has not been deployed

No deployments
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