Skip to content

Support successful_retcode on traced Reactant return codes - #1637

Merged
ChrisRackauckas merged 2 commits into
SciML:masterfrom
ChrisRackauckas-Claude:traced-successful-retcode
Oct 11, 2026
Merged

ChrisRackauckas merged 2 commits into
SciML:masterfrom
ChrisRackauckas-Claude:traced-successful-retcode

Conversation

@ChrisRackauckas-Claude

@ChrisRackauckas-Claude ChrisRackauckas-Claude commented Oct 8, 2026 •

Copy link
Copy Markdown
Member

Please ignore until reviewed by @ChrisRackauckas.

What changed and why

SciMLBase.successful_retcode throws on a return code produced inside Reactant.@jit. A compiled solver that selects its code with ifelse returns a Reactant.TracedEnum{ReturnCode.T}. The generic fallback then calls convert(ReturnCode.T, retcode), which throws MethodError: Cannot convert Reactant.TracedEnum{SciMLBase.ReturnCode.T} to SciMLBase.ReturnCode.T. The ReturnCode.T method uses short-circuit ||, which cannot branch on a traced Bool either.

This PR adds a SciMLBaseReactantExt extension. It defines successful_retcode(::Reactant.TracedEnum{ReturnCode.T}), which folds retcode == code over the successful codes with the non-short-circuiting | and returns a traced Bool. The successful codes for the traced method are a tuple inside the extension. The host method successful_retcode(::ReturnCode.T) is unchanged from master, keeping its short-circuit || expression and its speed. A test checks that the traced and host methods agree on every instances(ReturnCode.T), so the two lists cannot drift apart silently.

The concrete caller is the compiled RobustNonMonotoneLineSearch in SciML/LineSearch.jl#112. NonlinearSolve's DFSane checks !SciMLBase.successful_retcode(linesearch_sol.retcode) inside the compiled loop: https://github.com/SciML/NonlinearSolve.jl/blob/1caff340eef9f55895b03cb06c6849bacb37cce6/lib/NonlinearSolveSpectralMethods/src/solve.jl#L212-L230

Verification

All at head f78ac0858.

  • GROUP=Reactant: "Reactant specialization" 4/4 and "Reactant return codes" 28/28 pass. test/reactant_retcodes.jl covers:

    • Success, StalledSuccess, Failure and Default inside @jit, plus the after-return ConcreteEnum;
    • traced/host agreement on all 22 ReturnCode instances.
  • The agreement test catches drift: I dropped StalledSuccess from the extension's tuple in a scratch copy. Exactly one of the 22 cases fails, "agree on StalledSuccess" (false == true), and so does the existing StalledSuccess assertion.

  • GROUP=Core: passed.

  • ExplicitImports with Reactant loaded: all six checks pass on SciMLBaseReactantExt, using the round-1 reviewer's ei.jl. The extension references no SciMLBase internal, so test/qa/qa.jl's _ei_own_internals needs no new entry.

  • Formatting: Runic is clean on the changed files; the repo-wide --check already fails on master. typos is clean.

  • Host performance matches master. I used the reviewer's bulk_host.jl (@btime count(SciMLBase.successful_retcode, xs) over 22000 codes, 6000 successes) and bench_compare.jl (the original expression vs the package method, in the same process). Each project is a dev'd worktree of master 432e9856b or this head:

    Julia bulk_host.jl master bulk_host.jl head bench_compare.jl head: original/package, 22000 codes, ratio single call (Success/Failure)
    1.10.12 7.490 / 7.497 μs 7.482 / 7.490 μs 7497.5/7497.5 ns (1.000), 7497.5/7509.75 (1.002), 7500/7514.75 (1.002) 3.6 / 4.0 ns, 0 bytes
    1.12.4 7.747 / 7.755 μs 7.750 / 7.750 μs 7750/7754.75 ns (1.001), 7750/7752.5 (1.000), 7752.5/7752.5 (1.000) 3.6 / 4.0 ns, 0 bytes

    The previous head 65f835f2, which used tuple membership, measured 53.2 μs on Julia 1.10.12 in the round-1 review.

  • Downstream: the LineSearch branch https://github.com/ChrisRackauckas-Claude/LineSearch.jl/tree/agent/ls109x-traced-retcode tests successful_retcode(sol.retcode) inside @jit.

    • With SciMLBase 3.57.0: 53 pass, 3 errors (the convert MethodError above).
    • With the previous head of this branch dev'd: 62/62. It was not rerun against f78ac0858; only the extension's internals changed, and they are covered by the 28/28 above.
  • GROUP=QA: at 65f835f2 it gave 9 pass + 1 environmental CondaPkg/pixi error, the same as master on this machine. Not rerun at this head.

Not verified

  • GPU/TPU backends, and Julia 1.10 with Reactant loaded.
  • The QA group with Reactant loaded. QA does not load Reactant, so run_qa does not scan the new extension; the existing SciMLBaseReactantCoreExt is in the same position. The direct ExplicitImports checks above pass.

For the reviewer

  • Reactant moves from test-only to [weakdeps], and its compat floor rises from 0.2 to 0.2.289, the first release where Reactant.TracedEnum is public. Reactant is MIT licensed.
  • No version bump in this PR; a minor release (3.58.0) fits, since it adds a method and a weak dependency.

Risk assessment

🤖 Generated with Claude Code (model: claude-opus-5-5[1m]). The branch was written by a Claude Code Opus 5.5 worker session (~/sandbox/agent-jobs/LineSearch.jl/jobs/ls109x-claude on amdci2) and opened by the head session.

https://claude.ai/code/session_01592Hc7Y1ZjeqdXirDGww44

Independent review: Devin Fusion (fusion-claude-opus-5-5-high-sidekick-swe-2-medium) rated it low, verdict MERGE: #1637 (comment)

ChrisRackauckas and others added 2 commits October 8, 2026 08:52
Inside a Reactant compilation a ReturnCode can be a Reactant.TracedEnum
(public since Reactant 0.2.289). successful_retcode then hit the generic
convert(ReturnCode.T, retcode) fallback and threw a MethodError, and the
short-circuiting || cannot branch on a traced Bool. A Reactant weakdep
extension adds successful_retcode(::Reactant.TracedEnum{ReturnCode.T}),
which combines the comparisons with | into a traced Bool. The successful
codes move to one internal tuple used by both methods.

Co-Authored-By: Chris Rackauckas <[email protected]>
Co-Authored-By: Claude <[email protected]>
Agent-Harness: Claude Code 2.1.292
Agent-Model: opus
Agent-Session: local session, transcript at /home/crackauc/sandbox/agent-jobs/LineSearch.jl/jobs/ls109x-claude/log.txt on amdci2.julia.csail.mit.edu
… the extension

Membership in a tuple made bulk host classification about 7x slower than
the short-circuit expression. The host method is restored exactly; the
tuple lives in SciMLBaseReactantExt for the traced method only, and a test
checks the traced and host methods agree on every ReturnCode.

Co-Authored-By: Chris Rackauckas <[email protected]>
Co-Authored-By: Claude <[email protected]>
Agent-Harness: Claude Code 2.1.292
Agent-Model: opus
Agent-Session: local session, transcript at /home/crackauc/sandbox/agent-jobs/LineSearch.jl/jobs/ls109x-claude/log.txt on amdci2.julia.csail.mit.edu
@ChrisRackauckas

Copy link
Copy Markdown
Member

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

Independent review (Devin CLI 3000.11.3, model fusion-claude-opus-5-5-high-sidekick-swe-2-medium): MERGE, risk low.

Full review

VERDICT: MERGE
RISK: low

PR: #1637 (head f78ac08, merge-base 432e985; origin/master c8ea9ca differs only in a downstream test file).

Blocking findings

None.

Non-blocking findings

  1. Two copies of the successful-code list (suspected from reading, mitigated by a test I confirmed by running). src/retcodes.jl:490-495 (host || chain) and ext/SciMLBaseReactantExt.jl:8-11 (tuple) list the codes separately. The agreement test only runs in GROUP=Reactant (in_all = false, Julia 1 only). I confirmed that it catches drift in both directions: removing StalledSuccess from the tuple gave 2 failures, and adding Stalled gave 1 failure ("agree on Stalled", true == false). Someone who adds a new successful ReturnCode and runs only GROUP=Core will not see the failure until the Reactant CI job runs. One way to remove the second list is to generate both from a single const tuple in src/, for example an @eval-built || chain. That is a possible follow-up, not a requirement.
  2. The runner and the compat floor disagree. test/runtests.jl:4 still runs Pkg.add(PackageSpec(name = "Reactant", version = "0.2")), while [compat] now says 0.2.289. The resolver follows compat, so nothing breaks, but bumping the spec too would keep them consistent.
  3. The compat floor is correct but tightens resolution for existing users (confirmed by reading upstream source). Reactant moves from test-only extras into [weakdeps], and the floor rises from 0.2 to 0.2.289. I checked that floor: Reactant v0.2.288 has no src/Reactant.jl at that tag path (404). v0.2.289 marks TracedEnum as public, and its src/Enums.jl is byte-identical to v0.2.291, the version I tested. A user pinned to Reactant < 0.2.289 will be held at SciMLBase 3.57. That is acceptable for a minor release.

What I ran

All runs used Julia 1.12.7 and Reactant 0.2.291, with SciMLBase dev'd from the checkout. Logs are in ../scratch/.

  • The PR's test against the PR head (test/reactant_retcodes.jl): Pass 28/28. Confirmed by running.
  • The same test against origin/master (extracted with git archive): 24/24 error, each with MethodError: Cannot convert ... Reactant.TracedEnum{SciMLBase.ReturnCode.T} to ... SciMLBase.ReturnCode.T at src/retcodes.jl:494. Confirmed: the test fails before the fix and passes after it.
  • test/reactant_specialization.jl at head: Pass 4/4.
  • Drift mutations of the extension's tuple: removing StalledSuccess gave 26 pass / 2 fail, and adding Stalled gave 27 pass / 1 fail. Confirmed that the assertions can fail.
  • My own probes (scratch/probes.jl): 54/54 pass. Confirmed by running:
    • The agreement test's ifelse(flag > 0, code, code) really produces a Reactant.TracedEnum{ReturnCode.T}; it is not constant-folded back to a host enum. So the 22 agreement cases exercise the traced method, not the host one.
    • which resolves to SciMLBaseReactantExt.jl:15.
    • All 22 codes passed in as a ConcreteEnum jit input are correct inside @jit. The host fallback on the returned ConcreteEnum also works.
    • !successful_retcode(traced) works, which is the form DFSane uses. So does @trace if successful_retcode(rc), in both branches.
    • The solution-forwarding method works with a traced retcode field.
    • detect_ambiguities(SciMLBase; recursive=true) with Reactant loaded shows no ambiguities involving successful_retcode.
  • ExplicitImports on SciMLBaseReactantExt with Reactant loaded: all seven checks pass, including both checks that accesses are public. SciMLBase.successful_retcode and Reactant.TracedEnum are both ispublic == true.
  • Aqua test_stale_deps, test_deps_compat(check_extras=false) and test_project_extras: 5/5 pass.
  • Formatting and spelling: Runic --check on the 4 changed .jl files is clean, and typos on the changed files is clean.
  • Claims in the PR body that I checked against GitHub:
  • Interface fit: read Make RobustNonMonotoneLineSearch traceable under Reactant.@jit LineSearch.jl#112. Its compiled solve! returns TracedLineSearchSolution with a traced retcode, and callers use successful_retcode(sol.retcode). That is the exact signature this PR adds and that my probes exercise. The method has to live in a Reactant extension, not in SciMLBaseReactantCoreExt, because TracedEnum is defined in Reactant.
  • Rules review (by reading):
    • No new hard dependency; Reactant is MIT.
    • Only public API is used.
    • The docstring addition is in the present tense.
    • About 5 comment lines out of 54 added, none of them narrating.
    • The PR is single-concern.
    • The host method is byte-identical to master (diff shows no change to lines 490-498), so there is no host performance change to measure.

What I did not verify

  • Julia 1.10 (lts) with Reactant loaded. The extension uses only qualified access, so I expect no issue, but I did not run it.
  • The Reactant compat floor 0.2.289 at runtime. I checked it only by comparing Enums.jl source with the version I ran.
  • GPU/TPU backends.
  • The full GROUP=QA and GROUP=Core suites locally (CI QA is green), and the author's benchmark tables (unnecessary, since the host method is unchanged).
  • The downstream LineSearch branch run (62/62) claimed in the PR body.

Links


🤖 Posted by an AI agent — harness: Devin CLI 3000.11.3 (review), Claude Code 2.1.285 (posting) · model: fusion-claude-opus-5-5-high-sidekick-swe-2-medium
Conversation: local Claude session 3c311569-dda6-4687-bc9f-65febb212c33 (fleet master)

@ChrisRackauckas
ChrisRackauckas marked this pull request as ready for review October 11, 2026 10:31
@ChrisRackauckas
ChrisRackauckas merged commit 7940238 into SciML:master Oct 11, 2026
68 of 73 checks passed
ChrisRackauckas referenced this pull request Oct 13, 2026
* Add a specialization marker to `OptimizationFunction`

`OptimizationFunction` had no specialization marker: its second type parameter is
the AD type, and `specialization(::OptimizationFunction)` returned that instead of
an `AbstractSpecialization`. The marker is now stored as the last type parameter,
after the field types, so code that spells out the existing parameters keeps
working. It defaults to `FullSpecialize`, so existing functions are unchanged.

`OptimizationFunction{iip, specialize}(f, adtype; kwargs...)` selects it, and
`specialization` returns it. `remake`, `ConstructionBase.setproperties` (and with
it `@set`) and `OptimizationProblem` keep it. The spelling that passes the AD
type as the second type parameter (`OptimizationFunction{iip, AD}(f; adtype)`)
still builds a `FullSpecialize` function.

What `AutoSpecialize` and `AutoDespecialize` do is up to the solver packages,
as for the other SciML functions.

Co-Authored-By: Claude Opus 5.5 (1M context) <[email protected]>

* Convert parameter objects to `DespecializedParameters`

Storage typed for despecialized parameters could not be assigned a new parameter
object. For example, an optimization cache built from an `AutoDespecialize`
problem stores `p` as a `DespecializedParameters`, and `reinit!(cache; p = new_p)`
threw `Cannot convert an object of type ... to an object of type
SciMLBase.DespecializedParameters`. `convert` now wraps the object, and returns an
already wrapped one unchanged.

Co-Authored-By: Claude Opus 5.5 (1M context) <[email protected]>

* Bump version to 3.58.0

Co-Authored-By: Claude Opus 5.5 (1M context) <[email protected]>

---------

Co-authored-by: Claude Opus 5.5 (1M context) <[email protected]>
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