Modern Apps git

← Modern-Apps.git

games/logicgate: restored level pins can no longer duplicate a node id

Commit
2db5fc672bb87f0764766460361f2c664f85a15d
Author
Vayun Mathur <[email protected]>
When
2026-11-30 23:42 UTC (5 days ago)
Parents
5f2cad2
Circuit.withLevelIo minted restored input/output pins from
the persisted nextId, trusting it — but a stale save can
carry a nextId already taken by an existing node. The
sanitized circuit then held two nodes with NodeId(1): the
NAND gate plus the restored level output. Editor layout,
hit-testing, wiring and the simulator are all keyed by
NodeId, so the restored output shadowed the gate and a
working saved circuit restored broken. The end of sanitized
already computed maxId+1 — allocation simply ran before it.

Restored pins now allocate from maxOf(nextId,
maxExistingNodeId+1). Simulation (SCC order, feedback,
latches, RAM/CPU), the verifier, all 28 level definitions,
wiring rules and the debounced stores were audited and
cleared. New regression test failed pre-fix (3 nodes, 2
unique ids). Verified: :games:logicgate:testDevUnitTest
BUILD SUCCESSFUL; lint 0 errors.

Diff

games/logicgate/src/main/java/com/vayunmathur/games/logicgate/domain/model/Circuit.kt
diff --git a/games/logicgate/src/main/java/com/vayunmathur/games/logicgate/domain/model/Circuit.kt b/games/logicgate/src/main/java/com/vayunmathur/games/logicgate/domain/model/Circuit.ktindex 55f81c0a52..332501e63d 100644--- a/games/logicgate/src/main/java/com/vayunmathur/games/logicgate/domain/model/Circuit.kt+++ b/games/logicgate/src/main/java/com/vayunmathur/games/logicgate/domain/model/Circuit.kt@@ -135,7 +135,9 @@ data class Circuit(         val inIdx = nodes.filterIsInstance<LevelInput>().map { it.index }.toSet()         val outIdx = nodes.filterIsInstance<LevelOutput>().map { it.index }.toSet()         val fresh = forLevel(io)-        var next = nextId+        // A stale save can carry a nextId that is already taken by an existing node; fresh+        // level pins must be allocated above every id present, never on top of one.+        var next = maxOf(nextId, (nodes.maxOfOrNull { it.id.raw } ?: -1) + 1)         val missing = fresh.nodes.filter {             (it is LevelInput && it.index !in inIdx) || (it is LevelOutput && it.index !in outIdx)         }.map { n ->
games/logicgate/src/test/java/com/vayunmathur/games/logicgate/domain/VerifierTest.kt
diff --git a/games/logicgate/src/test/java/com/vayunmathur/games/logicgate/domain/VerifierTest.kt b/games/logicgate/src/test/java/com/vayunmathur/games/logicgate/domain/VerifierTest.ktindex 2e9528c144..4dcc254857 100644--- a/games/logicgate/src/test/java/com/vayunmathur/games/logicgate/domain/VerifierTest.kt+++ b/games/logicgate/src/test/java/com/vayunmathur/games/logicgate/domain/VerifierTest.kt@@ -6,6 +6,7 @@ import com.vayunmathur.games.logicgate.domain.model.Circuit import com.vayunmathur.games.logicgate.domain.model.ConnectCheck import com.vayunmathur.games.logicgate.domain.model.Gate import com.vayunmathur.games.logicgate.domain.model.LevelInput+import com.vayunmathur.games.logicgate.domain.model.NodeId import com.vayunmathur.games.logicgate.domain.model.Point import com.vayunmathur.games.logicgate.domain.model.Wire import com.vayunmathur.games.logicgate.domain.sim.Scoring@@ -156,6 +157,26 @@ class CircuitTest {         assertEquals(c, c.removeNode(input.id))     } +    @Test+    fun sanitizingAllocatesRestoredLevelPinsAboveExistingNodeIds() {+        // A stale save whose nextId was already taken by a gate: restoring the missing+        // level pins must not mint a second node with the gate's id (which would shadow+        // the gate in every id-keyed map downstream, simulator included).+        val stale = Circuit(+            nodes = listOf(+                LevelInput(NodeId(0), 0, Point(0f, 0f)),+                Gate(NodeId(1), Chips.NAND, Point(100f, 0f)),+            ),+            wires = emptyList(),+            nextId = 1,+        )+        val clean = stale.sanitized(level.io, lookup)+        assertEquals(clean.nodes.map { it.id }.toSet().size, clean.nodes.size)+        assertTrue(clean.nodes.any { it is Gate && it.id == NodeId(1) })+        assertEquals(level.inputs.size + level.outputs.size + 1, clean.nodes.size)+        assertTrue(clean.nextId > clean.nodes.maxOf { it.id.raw })+    }+     @Test     fun sanitizingDropsUnknownChipsAndRestoresMissingLevelPins() {         val (withJunk, _) = Circuit().addGate("NOPE", Point(0f, 0f))