diff --git a/docs/cultnet-transport-parity.md b/docs/cultnet-transport-parity.md index 2276b2cb..3cdf47f5 100644 --- a/docs/cultnet-transport-parity.md +++ b/docs/cultnet-transport-parity.md @@ -212,6 +212,10 @@ Current progress: connect, accept, single-packet sends, or fragmented sends enqueue anything; TCP-framed profiles do not publish the field because they do not own the RUDP pending-reliable queue. +- Kotlin now mirrors the C#/TypeScript/Python local authority lease gate: + peer cards may advertise `authorityLeaseId`, but `CultMeshAuthorityLeaseCatalog` + owns role/shard/time authorization so discovery contact cannot impersonate + trust. - The remaining parity work is to deepen Python's server-side use of its port and move each runtime's existing TCP/LiteNetLib/WebSocket bodies behind those ports, then broaden RUDP interop into reconnect policy and the wider diff --git a/packages/cultmesh-kotlin/README.md b/packages/cultmesh-kotlin/README.md index b1884eea..c71c23dc 100644 --- a/packages/cultmesh-kotlin/README.md +++ b/packages/cultmesh-kotlin/README.md @@ -212,6 +212,36 @@ val peerResponse = peers.createResponse( instances, so catalog discovery can ride the same MessagePack schema-v0 lane as RUDP schema traffic. +Peer cards are contact hints, not trust. Kotlin now mirrors the C#/TS/Python +lease gate so local clients can keep discovery and authority separate: + +```kotlin +val peer = CultMeshPeerCard( + peerId = "kotlin-peer", + verseId = "public", + endpoints = listOf("rudp://127.0.0.1:4100"), + roles = listOf("shard-primary"), + shardIds = listOf("players"), + authorityLeaseId = "lease:kotlin-peer", +) + +val leases = createAuthorityLeaseCatalog() +leases.upsert( + CultMeshAuthorityLease( + leaseId = "lease:kotlin-peer", + verseId = "public", + peerId = "kotlin-peer", + roles = listOf("shard-primary"), + shardIds = listOf("players"), + issuerRuntimeId = "odin", + validFrom = Instant.parse("2026-06-15T00:00:00Z"), + expiresAt = Instant.parse("2026-06-15T01:00:00Z"), + ), +) + +check(leases.isAuthorized(peer, "shard-primary", "players")) +``` + `EveMediaObservationDocument` carries byte-backed device streams such as camera luma frames and microphone PCM blocks. The document is observation transport, not synchronization authority: the device owns capture and local timestamps, diff --git a/packages/cultmesh-kotlin/src/main/kotlin/org/gamecult/cultmesh/CultMesh.kt b/packages/cultmesh-kotlin/src/main/kotlin/org/gamecult/cultmesh/CultMesh.kt index 282682f2..f73ddf69 100644 --- a/packages/cultmesh-kotlin/src/main/kotlin/org/gamecult/cultmesh/CultMesh.kt +++ b/packages/cultmesh-kotlin/src/main/kotlin/org/gamecult/cultmesh/CultMesh.kt @@ -715,6 +715,64 @@ data class CultMeshPeerCard( } } +data class CultMeshAuthorityLease( + val leaseId: String, + val verseId: String, + val peerId: String, + val roles: List, + val validFrom: Instant, + val expiresAt: Instant, + val shardIds: List = emptyList(), + val issuerRuntimeId: String, + val signature: String? = null, +) { + init { + requireNonBlank(leaseId, "leaseId") + requireNonBlank(verseId, "verseId") + requireNonBlank(peerId, "peerId") + requireNonBlank(issuerRuntimeId, "issuerRuntimeId") + if (!expiresAt.isAfter(validFrom)) throw IOException("CultMesh authority lease expiry must be after validFrom") + } + + fun isValidAt(at: Instant): Boolean = !at.isBefore(validFrom) && at.isBefore(expiresAt) + + fun covers(peer: CultMeshPeerCard, role: String, shardId: String? = null, at: Instant = Instant.now()): Boolean { + requireNonBlank(role, "role") + return isValidAt(at) && + verseId == peer.verseId && + peerId == peer.peerId && + leaseId == peer.authorityLeaseId && + role in roles && + peer.hasRole(role) && + (shardId.isNullOrBlank() || shardIds.isEmpty() || shardId in shardIds) + } +} + +class CultMeshAuthorityLeaseCatalog { + private val knownLeases = linkedMapOf() + + val leases: List + get() = knownLeases.toSortedMap().values.toList() + + fun upsert(lease: CultMeshAuthorityLease) { + knownLeases[lease.leaseId] = lease + } + + fun get(leaseId: String): CultMeshAuthorityLease? { + requireNonBlank(leaseId, "leaseId") + return knownLeases[leaseId] + } + + fun isAuthorized(peer: CultMeshPeerCard, role: String, shardId: String? = null, at: Instant = Instant.now()): Boolean { + requireNonBlank(role, "role") + val leaseId = peer.authorityLeaseId ?: return false + val lease = knownLeases[leaseId] ?: return false + return lease.covers(peer, role, shardId, at) + } +} + +fun createAuthorityLeaseCatalog(): CultMeshAuthorityLeaseCatalog = CultMeshAuthorityLeaseCatalog() + class CultMeshVerseCatalog { private val knownVerses = linkedMapOf() private val subscribers = mutableListOf<(CultMeshVerseDescriptor) -> Unit>() @@ -1981,6 +2039,7 @@ fun main(args: Array) { cultNetSchemaMessagesUseMessagePackMaps() cultNetSchemaCatalogsRoundTripDescriptors() cultMeshCatalogsRoundTripDiscoveryMessages() + cultMeshAuthorityLeasesGatePeerTrust() cultNetShardCatalogsAndLogsRoundTrip() rudpPacketCodecUsesDeterministicReliableOrderedFixture() rudpSessionPingsAndDetectsReceiveTimeout() @@ -2266,6 +2325,42 @@ private fun cultMeshCatalogsRoundTripDiscoveryMessages() { check(appliedPeerCatalog.get("peer-a")?.hasRole("schema") == true) } +private fun cultMeshAuthorityLeasesGatePeerTrust() { + val peer = CultMeshPeerCard( + peerId = "peer-authority", + verseId = "public", + endpoints = listOf("rudp://127.0.0.1:4100"), + roles = listOf("shard-primary", "schema"), + shardIds = listOf("players"), + authorityLeaseId = "lease:peer-authority", + ) + val leases = createAuthorityLeaseCatalog() + val validFrom = Instant.parse("2026-06-15T00:00:00Z") + val expiresAt = Instant.parse("2026-06-15T01:00:00Z") + val duringLease = Instant.parse("2026-06-15T00:30:00Z") + + check(!leases.isAuthorized(peer, "shard-primary", "players", duringLease)) + leases.upsert( + CultMeshAuthorityLease( + leaseId = "lease:peer-authority", + verseId = "public", + peerId = "peer-authority", + roles = listOf("shard-primary"), + shardIds = listOf("players"), + issuerRuntimeId = "kotlin-authority", + validFrom = validFrom, + expiresAt = expiresAt, + ), + ) + + check(leases.get("lease:peer-authority")?.issuerRuntimeId == "kotlin-authority") + check(leases.leases.map { it.leaseId } == listOf("lease:peer-authority")) + check(leases.isAuthorized(peer, "shard-primary", "players", duringLease)) + check(!leases.isAuthorized(peer, "schema", "players", duringLease)) + check(!leases.isAuthorized(peer, "shard-primary", "inventory", duringLease)) + check(!leases.isAuthorized(peer, "shard-primary", "players", expiresAt)) +} + private fun cultNetShardCatalogsAndLogsRoundTrip() { val notes = stringDocument("kotlin.note", "kotlin.note.v1") val cache = CultCache()