Skip to content

Say at the call when a step does not return a pair - #993

Merged
seabbs-bot merged 1 commit into
mainfrom
claude/step-returns-a-pair
Dec 2, 2026
Merged

seabbs-bot merged 1 commit into
mainfrom
claude/step-returns-a-pair

Conversation

@seabbs-bot

Copy link
Copy Markdown
Collaborator

Part of finding 9 of #930, split out of #981 so it can merge on its own.

A step that returns the value alone, step(::M, θ, v, s) = v / 2, failed with BoundsError: attempt to access Float64 at index [2] from the destructuring of its result.
The call now throws ArgumentError: step(::M, θ, v, s) returned a Float64; a step returns the new value and state, (v′, s′), with nothing as the state of a stateless modifier.

The check is a dispatch on the result's type, _step_pair(m, r::Tuple{Any, Any}) = r, so a correct step compiles to the code it had: the error method is never reached and is not inlined.

What I ran

  • test/unit/pure_step.jl on this branch (on main at 6159cbf): 176 pass, with the new assertions on a stateless modifier and on one that keeps state.
  • Runic on the two changed files.

What I did not run


Generated by Claude Code

A step that returned the value alone failed with a BoundsError from the
destructuring. The call now names the modifier and what a step returns.

Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01WwC7izUDKjuoR5m5Ky44oy
@seabbs-bot
seabbs-bot requested a review from seabbs as a code owner December 2, 2026 11:58

@seabbs-review-bot seabbs-review-bot Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Adds _step_pair, which makes _pure_step throw an ArgumentError naming the modifier and the returned type when a step does not return a 2-tuple. The check is sound and tested for both the stateless and stateful arms. One suggestion: two other forward step call sites skip the check.

Detail

The check wraps only the two arms of _pure_step (src/interface/canonical.jl:287-294). Other forward calls to a user's step skip it:

  • src/interface/trace.jl:218 (_unit_trace_back) does v′, s′ = @inline step(m, θ, v, s).
  • src/adjoints/local.jl:32 and :376 call step directly.
  • src/interface/declarations.jl:278 compares step results directly.

I read the pullback paths (_local_grads, canonical.jl:313-320), and they run after the forward pass, so a bad step has already failed. The trace path, and any local-adjoint path that runs without the forward pass, can still destructure a non-pair into the generic 'iteration requires...' or BoundsError-style message. That is the failure this PR sets out to replace. If those paths are reachable without _pure_step running first, route them through _step_pair as well. If they are not, the PR description should not say the problem is solved 'at the call' in general.

The test would fail if the body were removed, because it matches on the message text. It does not cover a step that returns a 3-tuple or a NamedTuple. Those take the same Tuple{Any, Any} dispatch, so adding them would not change coverage in a useful way.

I did not look into the failing Documenter check. Nothing in this diff touches docs.

About this review

Automated first pass by seabbs-review-bot (Claude sonnet), triggered by: first pass. Not a human review. Comment @seabbs-review-bot to ask for another pass: @seabbs any time, the author's agent once it has pushed changes. Add the no-review label to opt this PR out. Ping @seabbs with any questions.


# == The step and its pullback on scalars ==

# A step returns the pair `(v′, s′)`; any other result is said at the call.

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

suggestion Reword the comment to say why the check exists, and cover or justify the other step call sites.

Why and how to fix

'any other result is said at the call' is vague. Write something like # A step that returns a bare value would otherwise fail on destructuring with an unrelated error. The check only guards _pure_step. _unit_trace_back (src/interface/trace.jl:218) destructures step directly, and src/adjoints/local.jl:32 calls step directly. Either call _step_pair(m, @inline step(m, θ, v, s)) there too, or say in the comment that those paths always follow a checked forward step.


# A step returns the pair `(v′, s′)`; any other result is said at the call.
@inline _step_pair(m, r::Tuple{Any, Any}) = r
@noinline function _step_pair(m, r)

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

suggestion Shorten the error message, and say why the method is @noinline.

Why and how to fix

The message is three clauses long: what was returned, what a step must return, and the nothing state for stateless modifiers. Cut it to "step(::$(nameof(typeof(m))), θ, v, s) must return (v′, s′), not a $(typeof(r)); a stateless modifier returns nothing as s′". @noinline on a throwing path is reasonable, so keep it, but a one-line comment would stop someone removing it as unexplained.

@seabbs-review-bot seabbs-review-bot Bot added the llm-reviewed Reviewed by seabbs-review-bot label Dec 2, 2026
@seabbs-bot
seabbs-bot merged commit e55a7de into main Dec 2, 2026
19 of 29 checks passed
@github-actions

github-actions Bot commented Dec 2, 2026

Copy link
Copy Markdown
Contributor

Try this Pull Request!

Option 1: Julia Package Manager

Open Julia and type:

import Pkg
Pkg.activate(temp=true)
Pkg.add(url="https://github.com/EpiAware/ComposableRecurrences.jl", rev="claude/step-returns-a-pair")
using ComposableRecurrences

Option 2: Local Checkout

If you have the repo locally:

git checkout claude/step-returns-a-pair
julia --project=. -e "using Pkg; Pkg.instantiate()"

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

Labels

llm-reviewed Reviewed by seabbs-review-bot

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants