Skip to content

refactor(shared): extract DeterministicRNG/SplitMix64 → DeterminismKit, MonotonicClock → TimeKit (#446) - #451

Merged
wei18 merged 1 commit into
mainfrom
refactor/446-shared-determinism-primitives
Jun 9, 2026
Merged

wei18 merged 1 commit into
mainfrom
refactor/446-shared-determinism-primitives

Conversation

@wei18

@wei18 wei18 commented Jun 9, 2026

Copy link
Copy Markdown
Owner

Summary

Hoists the cross-engine deterministic primitives that were duplicated between SudokuCoreKit and MinesweeperCoreKit into shared Foundation-light leaf targets, eliminating the drift. The RNG drives puzzle/board generation, so the merged code is bit-identical to the Sudoku originals.

Refs #446 (code primitives part; audio assets deferred — see below).

Moves

Symbol From To
DeterministicRNG protocol (+ nextInt(upperBound:), shuffleInPlace) SudokuEngine, MinesweeperEngine (dupes) new leaf Packages/DeterminismKit
SplitMix64 SudokuEngine, MinesweeperEngine (dupes) Packages/DeterminismKit
MonotonicClock + LiveMonotonicClock GameState, MinesweeperGameState (dupes) existing Packages/TimeKit

6 duplicate source files deleted.

Divergence reconciled (Sudoku-vs-Minesweeper)

The pairs had diverged; the shared version is the Sudoku superset, with each MS copy being a lossy subset (not a behavioral variant):

  • shuffleInPlace — existed only in the Sudoku SplitMix64.swift. Kept in the shared leaf. Minesweeper never referenced it (MS does its own inline Fisher–Yates in MinesweeperEngine.swift), so this is purely additive for MS.
  • nextInt(upperBound:) — present in both copies (Sudoku's SplitMix64.swift extension, MS's DeterministicRNG.swift extension); identical body (same Lemire-style rejection threshold). Unified once.
  • SplitMix64.next() — identical constants in both; the MS file just carried fewer doc comments.
  • MonotonicClock / LiveMonotonicClock — byte-identical between the two copies (git detected the Sudoku move as a 73% rename into TimeKit).

The extension helpers (nextInt, shuffleInPlace) were internal in-module before; they are now public because the call sites live in a different module post-move. This is a required visibility change for the cross-module hoist; it does not alter behavior.

Re-export wiring (no call-site edits)

Mirroring the existing UTCDay @_exported import precedent so every consumer keeps compiling unedited:

  • SudokuEngine @_exported import DeterminismKit
  • GameState @_exported import TimeKit
  • MinesweeperEngine @_exported import DeterminismKit
  • MinesweeperGameState @_exported import TimeKit

Both CoreKits add a DeterminismKit path dependency (they already depended on TimeKit).

Determinism preserved

  • New DeterminismKitTests pins SplitMix64(seed: 0) to the frozen Phase-0 reference vector (meetings/2026-05-17_phase0-gates.md §0.1), independently proving the hoisted leaf is bit-identical.
  • No test files modified; no snapshot PNGs re-recorded.
Package Tests Result
DeterminismKit (new) 4 pass
SudokuCoreKit 131 pass (unchanged count)
MinesweeperCoreKit 81 pass (unchanged count)
SudokuKit (snapshots) 220 pass (unchanged count)
MinesweeperKit (snapshots) 127 pass (unchanged count)

All four swift builds (TimeKit, DeterminismKit, SudokuCoreKit, MinesweeperCoreKit) succeed.

Project.swift

No change needed — DeterminismKit is pulled in transitively via the umbrella packages (SudokuKit / MinesweeperKit), exactly as TimeKit and the CoreKits already are. The umbrella package builds above confirm auto-discovery.

Out of scope

Audio asset consolidation is deferred to #446 part-2 per the issue scope — this PR is code primitives only.

🤖 Generated with Claude Code

…t, MonotonicClock → TimeKit (#446)

Hoist the cross-engine deterministic primitives duplicated between
SudokuCoreKit and MinesweeperCoreKit into shared Foundation-light leaves,
eliminating the drift.

Moves:
- DeterministicRNG protocol (+ nextInt rejection sampling + shuffleInPlace
  Fisher-Yates) and SplitMix64 → new leaf Packages/DeterminismKit.
- MonotonicClock + LiveMonotonicClock → existing TimeKit leaf.

Reconciled the Sudoku-vs-Minesweeper divergence to the Sudoku superset:
- shuffleInPlace existed only in the Sudoku copy; kept it (MS never used it).
- nextInt(upperBound:) existed in Sudoku + MS DeterministicRNG; identical body.
- Core algorithm (SplitMix64.next, nextInt threshold, LiveMonotonicClock.now)
  is bit-identical to the Sudoku originals. The MS copies were lossy subsets,
  not variants.

Wiring: both CoreKits add a DeterminismKit dependency (already had TimeKit).
SudokuEngine re-exports DeterminismKit, GameState re-exports TimeKit,
MinesweeperEngine re-exports DeterminismKit, MinesweeperGameState re-exports
TimeKit — mirroring the existing UTCDay @_exported precedent so every call
site keeps compiling unedited. Deleted the 6 duplicate source files.

Determinism preserved: a new DeterminismKit test pins SplitMix64(seed:0) to the
frozen Phase-0 reference vector; all existing determinism/seed/snapshot tests
pass unchanged (SudokuCoreKit 131, MinesweeperCoreKit 81, SudokuKit 220,
MinesweeperKit 127). No snapshot PNGs re-recorded.

Project.swift needs no change — DeterminismKit is pulled transitively via the
umbrella packages, same as TimeKit and the CoreKits.

Audio asset consolidation is OUT of scope, deferred to #446 part-2.

Co-Authored-By: Claude Opus 4.8 <[email protected]>
@wei18
wei18 merged commit 0059cd7 into main Jun 9, 2026
4 checks passed
@wei18
wei18 deleted the refactor/446-shared-determinism-primitives branch June 9, 2026 10:58
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.

1 participant