Skip to content

Fix flaky HC allroots "no real solutions" test - #914

Merged
ChrisRackauckas merged 1 commit into
SciML:masterfrom
ChrisRackauckas-Claude:fix-homotopy-allroots-flaky-length-check
Apr 15, 2026
Merged

ChrisRackauckas merged 1 commit into
SciML:masterfrom
ChrisRackauckas-Claude:fix-homotopy-allroots-flaky-length-check

Conversation

@ChrisRackauckas-Claude

Copy link
Copy Markdown
Member

Summary

  • Make lib/NonlinearSolveHomotopyContinuation/test/allroots.jl:152-158 reproducible by seeding the HC path tracker (solve(_prob, _alg; seed = 0x12345)).
  • Relax @test length(_sol) == 1 to @test length(_sol) >= 1 && all(!SciMLBase.successful_retcode, _sol.u) — the wrapper's "no real solutions" guarantee is that every returned NonlinearSolution carries a failure retcode, not that exactly one ever survives filtering.

Why

The "no real solutions" block was flaking in CI across every vector u - (oop|iip) + (forwarddiff|jac|enzyme) test case, reported as:

no real solutions: Test Failed at allroots.jl:156
  Expression: length(_sol) == 1
   Evaluated: 2 == 1

Reproduced locally against the current source with a minimal script:

using NonlinearSolveHomotopyContinuation, NonlinearSolve, HomotopyContinuation, SciMLBase
rhs(u, p) = [u[1]^2 - p[1]*u[2] + u[2]^3 + 1, u[2]^3 + 2*p[2]*u[1]*u[2] + u[2]]
jac_fn(u, p) = [2u[1]  (-p[1]+3u[2]^2); 2*p[2]*u[2]  (3u[2]^2+2*p[2]*u[1]+1)]
alg = HomotopyContinuationJL{true}(; threading=false, autodiff=nothing)
prob = NonlinearProblem(NonlinearFunction(rhs; jac=jac_fn), [1.0, 2.0], [2.0, 3.0])
_sol = solve(remake(prob; p = zeros(2)), alg)
# → converged=false, length=2   (test expects length=1)

Reproduces on both HomotopyContinuation v2.18.1 and v2.18.2, so this is not a HC regression. Two root causes:

  1. Non-determinism. HC path tracking uses random γ-trick starting points by default, so HC.results(orig_sol; only_real = true) surfaces a varying number of near-real complex roots run-to-run when the underlying system has no true real roots. Fixed by passing seed = 0x12345 to solve, which HC forwards to the path tracker — confirmed reproducible locally across three consecutive trials with the seed set, identical output.

  2. Too-strict assertion. length(_sol) == 1 pins a specific outcome of HC's imaginary-part threshold on a numerical boundary. The meaningful invariant for "no real solutions" is that no returned NonlinearSolution reports a successful retcode, which the wrapper guarantees independent of how many near-real paths survive filtering: both the isempty(realsols) sentinel at lib/NonlinearSolveHomotopyContinuation/src/solve.jl:72 and the isempty(validsols) sentinel at solve.jl:99 build NonlinearSolutions with ReturnCode.ConvergenceFailure or ReturnCode.Infeasible. Replaced with length(_sol) >= 1 && all(!SciMLBase.successful_retcode, _sol.u).

Test plan

  • MWE returns the same converged=false, length=N output on 3 consecutive seeded trials locally.
  • HC unpatched; only test assertions change.
  • CI CI (NonlinearSolveHomotopyContinuation) passes on this branch.

Notes

🤖 Generated with Claude Code

`lib/NonlinearSolveHomotopyContinuation/test/allroots.jl:152-158` was
flaking in CI (observed as six failures per affected run across the
vector_test_cases matrix) with `Evaluated: 2 == 1` on
`@test length(_sol) == 1`. Reproduced locally against the current
source:

    using NonlinearSolveHomotopyContinuation, NonlinearSolve,
          HomotopyContinuation, SciMLBase
    alg = HomotopyContinuationJL{true}(; threading=false, autodiff=nothing)
    rhs(u, p) = [u[1]^2 - p[1]*u[2] + u[2]^3 + 1,
                 u[2]^3 + 2*p[2]*u[1]*u[2] + u[2]]
    jac_fn(u, p) = [2u[1]  (-p[1]+3u[2]^2);
                    2*p[2]*u[2]  (3u[2]^2+2*p[2]*u[1]+1)]
    fn = NonlinearFunction(rhs; jac=jac_fn)
    prob = NonlinearProblem(fn, [1.0, 2.0], [2.0, 3.0])
    _sol = solve(remake(prob; p = zeros(2)), alg)
    # → converged=false, length=2  (test expects length=1)

The failure is independent of HomotopyContinuation version (reproduces
on both v2.18.1 and v2.18.2) and of anything under SciML#910 (HC's
`AllRoots` dispatch does not call `get_concrete_problem` /
`maybe_wrap_f` / `standardize_forwarddiff_tag`). Two root causes:

1. **Non-determinism.** HC path tracking uses random γ-trick starts,
   so `HC.results(orig_sol; only_real = true)` surfaces a varying
   number of near-real complex roots run-to-run when the system
   has no true real roots. Fixed by passing `seed = 0x12345` to the
   outer `solve(_prob, _alg; seed = ...)`, which HC forwards to the
   path tracker.

2. **Too-strict assertion.** `length(_sol) == 1` pins a specific
   imaginary-part-threshold outcome in `HC.results`. The meaningful
   invariant for "no real solutions" is that no returned
   `NonlinearSolution` reports a successful retcode, which the
   wrapper guarantees regardless of how many near-real paths
   survive filtering (both the `isempty(realsols)` sentinel branch
   at `solve.jl:72` and the `isempty(validsols)` sentinel at
   `solve.jl:99` build `NonlinearSolution`s with
   `ReturnCode.ConvergenceFailure` or `ReturnCode.Infeasible`).
   Replaced with `length(_sol) >= 1` plus
   `all(!SciMLBase.successful_retcode, _sol.u)`.

Together these make the test both reproducible and robust to
numerical boundary behavior. No changes to runtime code — test-only
fix.

Co-Authored-By: Chris Rackauckas <[email protected]>
Co-Authored-By: Claude Opus 4.6 (1M context) <[email protected]>
ChrisRackauckas-Claude pushed a commit to ChrisRackauckas-Claude/NonlinearSolve.jl that referenced this pull request Apr 15, 2026
Tiny no-op comment on `lib/NonlinearSolveHomotopyContinuation/src/interface_types.jl`
to trigger `CI (NonlinearSolveHomotopyContinuation)` on this branch
without modifying any runtime behavior or the failing test in
`test/allroots.jl`. Confirms the "no real solutions" `length(_sol) == 1`
flake (observed in SciML#910 CI runs) reproduces on a branch off plain
master without any of SciML#910's changes — i.e. it is a pre-existing
problem with the assertion, not a regression introduced by
NonlinearSolveBase changes.

Will be closed once CI confirms the failure pattern. The actual fix
(seed + relaxed assertion) is in SciML#914.

Co-Authored-By: Chris Rackauckas <[email protected]>
Co-Authored-By: Claude Opus 4.6 (1M context) <[email protected]>
@ChrisRackauckas-Claude

Copy link
Copy Markdown
Member Author

The diagnostic PR #915 (plain master + a single no-op comment) reports the same 3× Ubuntu HC failures with Evaluated: 2 == 1 at allroots.jl:156, confirming the flake is on master itself. This PR's fix (seed + relaxed assertion) is the correct resolution — its CI shows the same 3× Ubuntu HC jobs green.

@ChrisRackauckas
ChrisRackauckas merged commit d8e65c8 into SciML:master Apr 15, 2026
12 of 17 checks passed
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