Skip to content

refactor: migrate OnnxStrategy onto dicechess-engine-scala's JvmApi - #18

Merged
rabestro merged 1 commit into
mainfrom
refactor/17-migrate-onnxstrategy-to-jvmapi
Aug 4, 2026
Merged

refactor: migrate OnnxStrategy onto dicechess-engine-scala's JvmApi#18
rabestro merged 1 commit into
mainfrom
refactor/17-migrate-onnxstrategy-to-jvmapi

Conversation

@rabestro

@rabestro rabestro commented Aug 4, 2026

Copy link
Copy Markdown
Owner

Context

Bumps dicechess.engine.version to 1.12.0 (the release that ships dicechess.engine.jvmapi.JvmApi, dicechess-engine-scala#558) and rewrites OnnxStrategy against it.

OnnxStrategy previously reached into surface never meant for Java consumption:

  • Reflection in a static initializer to find GameState.makeMove(Move) — an @targetName-disambiguated extension method that compiles onto a synthetic Position$package class, with no ordinary Java entry point.
  • An unchecked cast through Object to unwrap TurnGenerator.generateAllLegalTurnPaths's List[List[Move]]Move is an opaque Int, erased on the JVM, so the nested Scala collection looked like List[List[Object]] of boxed integers from Java.
  • Manual Either unwrapping for FenParser.parse.
  • A hand-rolled moveToNotation reimplementing UCI encoding with raw bit masks.

Changes

OnnxStrategy.chooseMoves now:

  • JvmApi.parseDfen(dfen) — throws IllegalArgumentException on a bad DFEN instead of an Either to unwrap.
  • JvmApi.activeColor(state) — a real static entry point, no reflection.
  • JvmApi.legalTurns(state) — each legal turn as its UCI micro-move sequence and the resulting GameState in one call, so scoring a candidate turn no longer needs a separate apply-loop or an unchecked cast.

Net: ~120 fewer lines, the static initializer and its reflection are gone entirely, no more hand-rolled bit-twiddling for notation.

Verification

  • mise run check (Spotless + unit tests + shaded-jar package) against the real published dicechess-engine-scala_3:1.12.0 from GitHub Packages (not a local snapshot): BUILD SUCCESS, 4/4 tests pass (OnnxStrategyTest ×3, WebhookIntegrationTest ×1), unmodified.
  • OnnxStrategyTest's three cases are black-box against chooseMoves() (empty DFEN → empty list; single pawn die → 1 micro-move; triple dice pool → ≤3 micro-moves) — passing unmodified is exactly the regression signal that the rewrite preserves behavior.

Out of scope

OnnxEvaluator — unaffected, it already takes GameState/int color directly (opaque Color erases to int, so no interop issue existed there to begin with). Main/Strategy — unaffected.

Closes #17

OnnxStrategy reached into the Scala engine through surface never meant
for Java: reflection in a static initializer to find the
@targetName-disambiguated makeMove(Move) extension method (which
compiles onto a synthetic Position$package class), an unchecked cast
through Object to unwrap TurnGenerator's List[List[Move]] (Move is an
opaque Int, erased on the JVM), manual Either unwrapping for
FenParser.parse, and a hand-rolled moveToNotation reimplementing UCI
encoding with raw bit masks.

dicechess-engine-scala v1.12.0 ships JvmApi specifically to remove all
of that: parseDfen (Either -> exception), activeColor, and legalTurns
(each legal turn as its UCI sequence plus the resulting GameState,
replacing the reflective apply loop and the encoder in one call).

Existing OnnxStrategyTest cases are black-box against chooseMoves and
pass unmodified -- same behavior, ~120 fewer lines, zero reflection.

Closes #17
@github-actions github-actions Bot added refactoring Code restructuring without behavioral changes infrastructure Docker, Koyeb, and container runtime java-bot labels Aug 4, 2026
@coderabbitai

coderabbitai Bot commented Aug 4, 2026

Copy link
Copy Markdown

Review Change Stack

No actionable comments were generated in the recent review. 🎉

ℹ️ Recent review info
⚙️ Run configuration

Configuration used: Organization UI

Review profile: CHILL

Plan: Pro Plus

Run ID: f0f57d95-7487-4bca-aa8f-d5f6d3b443aa

📥 Commits

Reviewing files that changed from the base of the PR and between eb0073e and 1441181.

📒 Files selected for processing (2)
  • pom.xml
  • src/main/java/dicechess/bot/OnnxStrategy.java

📝 Walkthrough

Walkthrough

The Maven dependency updates to engine version 1.12.0. OnnxStrategy replaces Scala interop and manual move handling with JvmApi parsing, legal-turn generation, final-state evaluation, and UCI move selection.

Changes

OnnxStrategy JvmApi migration

Layer / File(s) Summary
Engine API upgrade
pom.xml, src/main/java/dicechess/bot/OnnxStrategy.java
The engine version changes to 1.12.0. OnnxStrategy now uses JvmApi instead of Scala-specific integrations.
JvmApi turn selection
src/main/java/dicechess/bot/OnnxStrategy.java
chooseMoves parses DFENs, obtains legal turns, evaluates final states from the active color’s perspective, selects the highest score, and returns the turn’s UCI sequence.

Estimated code review effort: 3 (Moderate) | ~20 minutes

Sequence Diagram(s)

sequenceDiagram
  participant OnnxStrategy
  participant JvmApi
  participant OnnxEvaluator
  OnnxStrategy->>JvmApi: Parse DFEN and read active color
  OnnxStrategy->>JvmApi: Request legal turns
  JvmApi-->>OnnxStrategy: Return turns with final states and UCI sequences
  OnnxStrategy->>OnnxEvaluator: Evaluate each final state
  OnnxEvaluator-->>OnnxStrategy: Return scores
  OnnxStrategy-->>OnnxStrategy: Return highest-scoring UCI sequence
Loading

Possibly related issues

  • rabestro/dicechess-engine-scala issue 557: The change adopts the JvmApi facade introduced for Java engine integration.
🚥 Pre-merge checks | ✅ 5
✅ Passed checks (5 passed)
Check name Status Explanation
Title check ✅ Passed The title clearly and concisely describes the migration of OnnxStrategy to dicechess-engine-scala's JvmApi.
Description check ✅ Passed The description explains the JvmApi migration, dependency upgrade, removed interop workarounds, verification, and unchanged scope.
Linked Issues check ✅ Passed The changes satisfy issue #17 by using JvmApi, upgrading to 1.12.0, removing legacy interop code, and preserving the stated scope.
Out of Scope Changes check ✅ Passed The changes are limited to the requested dependency upgrade and OnnxStrategy rewrite; OnnxEvaluator, Main, and Strategy remain unchanged.
Docstring Coverage ✅ Passed No functions found in the changed files to evaluate docstring coverage. Skipping docstring coverage check.
✨ Finishing Touches
📝 Generate docstrings
  • Create stacked PR
  • Commit on current branch

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

Comment @coderabbitai help to get the list of available commands.

@sonarqubecloud

sonarqubecloud Bot commented Aug 4, 2026

Copy link
Copy Markdown

@rabestro rabestro self-assigned this Aug 4, 2026
@rabestro
rabestro merged commit 7e5bbcb into main Aug 4, 2026
9 checks passed
@rabestro
rabestro deleted the refactor/17-migrate-onnxstrategy-to-jvmapi branch August 4, 2026 17:42
@codecov

codecov Bot commented Aug 4, 2026

Copy link
Copy Markdown

Welcome to Codecov 🎉

Once you merge this PR into your default branch, you're all set! Codecov will compare coverage reports and display results in all future pull requests.

Thanks for integrating Codecov - We've got you covered ☂️

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

Labels

infrastructure Docker, Koyeb, and container runtime java-bot refactoring Code restructuring without behavioral changes

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Migrate OnnxStrategy onto dicechess-engine-scala's JvmApi facade

1 participant