I was turning single-player puzzles into a two-phone co-op mode: both players work on the same puzzle, each phone sends its moves to the other. The sync design was sound, but one game could never agree with itself. The cause was a rule inside the game, not the network code: the game refused moves that taught the player nothing, and whether a move teaches you anything depends on which moves came before it.
TL;DR — Don’t replay a partner’s moves through the same function that validates local input. Give the game a separate absorb(move) whose result does not depend on arrival order (sets, max/min), and serialise the state in a canonical order before comparing hashes.
The setup
Two phones on the same Wi-Fi. One is the host and holds the reference state; the other is the guest. Every move is sent as a small message and applied on both sides. Messages can arrive in a different order on each phone: two players tap at the same time, and each phone applies its own move first.
To detect drift, the guest periodically sends a hash of its state. If the host’s hash differs twice in a row, the host resends its full state as a list of moves:
fun fingerprint(snapshot: List<String>): Int = snapshot.joinToString("\n").hashCode()
The game in question is a « secret word » game: you guess any word, and the game says whether the secret word comes before or after it in alphabetical order. The known range (lower, upper) shrinks until you find it.
What I expected
Every guess is just an item in a list, and the range is « the closest guess below and the closest guess above ». That looks order-independent, so I expected applying the partner’s guesses with the normal guess() to work.
What actually happens
guess() had a sensible single-player rule: a guess outside the known range teaches nothing, so it is refused.
fun guess(guess: String): Result {
if (lower?.let { guess <= it } == true || upper?.let { guess >= it } == true) return Result.OUT_OF_RANGE
guesses += guess
// ... narrow lower or upper
}
With the secret MAISON and the four guesses ARBRE, LAPIN, ROSE, ZEBRE, one phone received them in that order and the other in reverse. Both ended with the same range (LAPIN … ROSE), but different guess lists. The phone that saw ROSE first refused ZEBRE; the one that saw ZEBRE first kept it and refused ARBRE.

The visible board looked identical, so nothing seemed wrong. But the hashes never matched. The host kept resending its state, and the guest replayed it through the same guess(), which refused the same moves again. The resync could not converge.
The fix
A second entry point for moves that come from the partner. It accepts every move and updates the range with max/min, which gives the same result whatever the order:
fun absorb(guess: String) {
if (guess in guesses) return
guesses += guess
if (guess < target && lower?.let { guess > it } != false) lower = guess
if (guess > target && upper?.let { guess < it } != false) upper = guess
}
The local player still goes through guess(), so the « useless guess » rule keeps working for the person typing. The snapshot used for the hash is built from sorted data, never from arrival order:
snapshot = { game.guesses.sorted().map { SecretOp(it) } + SecretOp(h = hints) }
The unit test that pins it, both directions:
val a = SecretWordGame("MAISON").apply { absorb("ARBRE"); absorb("LAPIN"); absorb("ZEBRE"); absorb("ROSE") }
val b = SecretWordGame("MAISON").apply { absorb("ROSE"); absorb("ZEBRE"); absorb("LAPIN"); absorb("ARBRE"); absorb("LAPIN") }
assertEquals(a.lower, b.lower); assertEquals(a.upper, b.upper)
assertEquals(a.guesses.sorted(), b.guesses.sorted())
val c = SecretWordGame("MAISON").apply { guess("ARBRE"); guess("LAPIN"); guess("ROSE"); guess("ZEBRE") }
val d = SecretWordGame("MAISON").apply { guess("ZEBRE"); guess("ROSE"); guess("LAPIN"); guess("ARBRE") }
assertEquals(listOf("ARBRE", "LAPIN", "ROSE"), c.guesses)
assertEquals(listOf("LAPIN", "ROSE", "ZEBRE"), d.guesses.sorted())
Why it works
Replaying moves on two machines only converges if applying them is commutative (order does not matter) and idempotent (applying one twice changes nothing; resyncs resend everything). Set union, max and min have both properties. A validation rule that reads the current state does not: whether it accepts a move depends on what came before.
The same pattern applied to the other games I wired up. Each message carries the full value it sets (row 3 = « PLANT », not « type the letter T »), and the snapshot lists items in a fixed order (sorted words, row by row), so resending it can only repair.
What I did not test
This was verified with unit tests and builds, not on two real phones with real network delays. Commutative merges do not fix genuine conflicts: if both players fill the same row at the same moment, the last message in the host’s order wins. I did not check games where the order of moves is itself part of the rules (turn-based games need a sequencer, not a merge).
Facts
context: Two-phone co-op puzzle; each phone applies both players' moves; host compares a hash of the state with the guest's
problem: Replaying partner moves through the local validator (refuse guesses outside the known range) left different move lists depending on arrival order, so hashes never matched and resync could not converge
solution: A separate absorb() for remote moves using set union and max/min (commutative, idempotent), and a snapshot serialised in sorted order before hashing
verified_on: 2026-10-28
applies_to: Any peer or host/guest state replication that replays moves and compares state hashes
does_not_apply_to: Turn-based games where move order is part of the rules