Modern Apps git

← Modern-Apps.git

voxels: join requests carry their kind discriminator on the wire

Commit
410f7d1a0647eb3ddc5b630134052039cd7195e5
Author
Vayun Mathur <[email protected]>
When
2026-12-01 10:38 UTC (5 days ago)
Parents
5fd4bb3
JoinRequest.kind defaulted to 'req', and kotlinx.serialization
omits default-valued properties, so sealed join requests
reached the relay with no kind field. pullInbox sorted
solely on that discriminator: the missing kind decoded as
'', took the invite branch, failed Invite parsing (no key),
and was silently dropped — re-dropped on every refresh since
the inbox is pulled from seq 0. World owners never saw join
requests in the menu or in-game; the existing round-trip
test decoded straight back to JoinRequest, where the default
refills 'req', so it could not see the mis-classification.

kind is now @EncodeDefault ('kind':'req' always written),
and parseInboxItem also classifies by requesterId presence
so blobs already sealed by broken builds sort as requests
(invites never carry one; garbage unchanged). Three new
tests pin the discriminator on the wire, new+legacy sorting,
and invite/garbage behaviour. Menu, inventory contracts and
settings were audited and cleared. Verified:
:games:voxels:testDevUnitTest — only the 6 pre-existing
environmental VoxelsCovX4 failures; :games:voxels:lint 0
errors.

Diff

games/voxels/src/main/java/com/vayunmathur/games/voxels/network/VoxelsSync.kt
diff --git a/games/voxels/src/main/java/com/vayunmathur/games/voxels/network/VoxelsSync.kt b/games/voxels/src/main/java/com/vayunmathur/games/voxels/network/VoxelsSync.ktindex 8b4a6d0ee3..a3052823c2 100644--- a/games/voxels/src/main/java/com/vayunmathur/games/voxels/network/VoxelsSync.kt+++ b/games/voxels/src/main/java/com/vayunmathur/games/voxels/network/VoxelsSync.kt@@ -23,6 +23,7 @@ import kotlinx.coroutines.isActive import kotlinx.coroutines.launch import kotlinx.coroutines.sync.Mutex import kotlinx.coroutines.sync.withLock+import kotlinx.serialization.EncodeDefault import kotlinx.serialization.Serializable import kotlinx.serialization.json.Json import kotlin.io.encoding.Base64@@ -164,17 +165,43 @@ object VoxelsSync {         val requests = mutableListOf<JoinRequest>()         for (b in p.actions) {             val plain = runCatching { identity.decrypt(Base64.decode(b)) }.getOrNull() ?: continue-            val text = plain.decodeToString()-            val kind = runCatching { json.decodeFromString<InboxEnvelope>(text).kind }.getOrNull().orEmpty()-            if (kind == "req") {-                runCatching { json.decodeFromString<JoinRequest>(text) }.getOrNull()?.let { requests.add(it) }-            } else {-                runCatching { json.decodeFromString<Invite>(text) }.getOrNull()?.let { invites.add(it) }+            when (val item = parseInboxItem(plain.decodeToString())) {+                is InboxItem.InviteItem -> invites.add(item.invite)+                is InboxItem.RequestItem -> requests.add(item.request)+                null -> Unit             }         }         return InboxResult(invites, requests, p.seq)     } +    /** One decrypted inbox blob, sorted into an invite or a join request. */+    internal sealed interface InboxItem {+        /** A sealed world invite. */+        data class InviteItem(val invite: Invite) : InboxItem++        /** A sealed request to join a world. */+        data class RequestItem(val request: JoinRequest) : InboxItem+    }++    /**+     * Sorts one decrypted inbox text into an invite or a join request; null for anything else.+     * A request is recognised by its `kind:"req"` discriminator, or — for blobs sealed by+     * builds that omitted the default-valued `kind` from the wire — by its `requesterId`,+     * which invites never carry. The inbox is always pulled from 0, so those legacy blobs+     * would otherwise be misread as invites and dropped on every refresh.+     */+    internal fun parseInboxItem(text: String): InboxItem? {+        val envelope = runCatching { json.decodeFromString<InboxEnvelope>(text) }.getOrNull()+        val isRequest = envelope?.kind == "req" || !envelope?.requesterId.isNullOrBlank()+        return if (isRequest) {+            runCatching { json.decodeFromString<JoinRequest>(text) }.getOrNull()+                ?.let { InboxItem.RequestItem(it) }+        } else {+            runCatching { json.decodeFromString<Invite>(text) }.getOrNull()+                ?.let { InboxItem.InviteItem(it) }+        }+    }+     // --- Owner-signed roster on the (world-key-encrypted) members channel ---      /** Canonical bytes an owner signs for a member record (binds id + role to the world). */@@ -359,14 +386,16 @@ object VoxelsSync {      /** A request to join a world, sealed to the owner. Public ids only — no key. */     @Serializable data class JoinRequest(-        val kind: String = "req",+        // Always written: the inbox sorts on this discriminator, and a default-valued+        // property would otherwise be omitted from the wire (see parseInboxItem).+        @EncodeDefault val kind: String = "req",         val worldId: String,         val requesterId: String,         val name: String = "",     )      /** Peeks the discriminator on an inbox blob so invites and requests can share the channel. */-    @Serializable private data class InboxEnvelope(val kind: String = "")+    @Serializable private data class InboxEnvelope(val kind: String = "", val requesterId: String = "")      /** An author-signed op/snapshot batch: author id, signature over [ops], and the ops JSON. */     @Serializable data class SignedOp(val author: String, val sig: String, val ops: String)
games/voxels/src/test/java/com/vayunmathur/games/voxels/network/VoxelsSyncInboxTest.kt
diff --git a/games/voxels/src/test/java/com/vayunmathur/games/voxels/network/VoxelsSyncInboxTest.kt b/games/voxels/src/test/java/com/vayunmathur/games/voxels/network/VoxelsSyncInboxTest.ktnew file mode 100644index 0000000000..a8aeac6ad3--- /dev/null+++ b/games/voxels/src/test/java/com/vayunmathur/games/voxels/network/VoxelsSyncInboxTest.kt@@ -0,0 +1,48 @@+package com.vayunmathur.games.voxels.network++import kotlinx.serialization.Serializable+import kotlinx.serialization.json.Json+import kotlin.test.Test+import kotlin.test.assertEquals+import kotlin.test.assertNull+import kotlin.test.assertTrue++class VoxelsSyncInboxTest {+    private val json = Json { ignoreUnknownKeys = true }++    @Serializable+    private data class EnvelopeProbe(val kind: String = "")++    @Test fun `join request carries req discriminator on the wire`() {+        val req = VoxelsSync.JoinRequest(worldId = "w1", requesterId = "dev9", name = "Bob")+        val wire = json.encodeToString(req)+        assertTrue(wire.contains("\"kind\":\"req\""), "kind discriminator missing from wire: $wire")+        assertEquals("req", json.decodeFromString<EnvelopeProbe>(wire).kind)+    }++    @Test fun `inbox sorts new and legacy join requests as requests, not invites`() {+        val req = VoxelsSync.JoinRequest(worldId = "w1", requesterId = "dev9", name = "Bob")+        val newWire = json.encodeToString(req)+        // Wire sealed by builds that omitted the default-valued kind (the inbox is+        // pulled from 0, so these blobs are re-read on every refresh).+        val legacyWire = """{"worldId":"w1","requesterId":"dev9","name":"Bob"}"""+        assertEquals(+            VoxelsSync.InboxItem.RequestItem(req),+            VoxelsSync.parseInboxItem(newWire),+        )+        assertEquals(+            VoxelsSync.InboxItem.RequestItem(req),+            VoxelsSync.parseInboxItem(legacyWire),+        )+    }++    @Test fun `inbox sorts invites as invites and drops garbage`() {+        val invite = VoxelsSync.Invite("w1", "a2V5", "World", 7, VoxelsRoles.EDITOR, "ownerKey", "dev0")+        assertEquals(+            VoxelsSync.InboxItem.InviteItem(invite),+            VoxelsSync.parseInboxItem(json.encodeToString(invite)),+        )+        assertNull(VoxelsSync.parseInboxItem("not json"))+        assertNull(VoxelsSync.parseInboxItem("""{"worldId":"w1"}"""))+    }+}