feat: a founding invite that never went out is finished (robust-group-admins, Phase 4)

MarmotGroupCreation.finishFoundingInvite invites the founders a room's
founding invite never reached, in one commit, every key package first as
at creation. It acts only on a room still at epoch 0: the founding
invite is the commit that ends epoch 0, and leaving is a commit too, so
a founder without a leaf there is one nobody ever invited -- never one
who left. CeremonyFollowThrough takes it as step 4 once the room exists,
on the coordinator's device; the repository exposes it by room for the
detail screen, with the set it would invite.

Also fixes what Phase 0 found. MarmotGroupCreation invited the founders
through a room read before its routing id was recorded, so on the
current profile the engine adopted a second copy of the group under the
room id, and the copy at the real address still held only its creator:
every later invite took the founding path, with no commit and nobody
already in the room told. It now reads the room again first.

RobustGroupAdminsJvmTest: a room left without its invite is finished and
every founder joins as an admin, on both profiles; a room past its
founding invites nobody; a later member on the current profile now gets
a real commit. CeremonyFollowThroughJvmTest: step 4's outcomes.
jvmTest 1249/1249, compileDebugKotlinAndroid, audits.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
Kgothatso NgakoandClaude Opus 5.5 committed 2026-11-20 01:14:19 +02:00
1 parent 4671a86a6d
commit 1dc94ad423
7 files changed
+340 -12

No files matched your search

@@ -87,6 +87,33 @@ class DatabaseChatRepository(
parentChatRoomId = parentChatRoomId,
)
override suspend fun finishFoundingInvite(
ceremony: DkgSession,
ceremonyRoom: LocalChatRoom,
): MarmotGroupCreation.FoundingInvite = MarmotGroupCreation.finishFoundingInvite(
database = database,
chatRepository = this,
ceremony = ceremony,
ceremonyRoom = ceremonyRoom,
)
override suspend fun finishFoundingInvite(chatRoomId: String): MarmotGroupCreation.FoundingInvite {
val (ceremony, ceremonyRoom) = FoundingAdmins.ceremonyOf(database, chatRoomId)
?: return MarmotGroupCreation.FoundingInvite.NothingToDo
return finishFoundingInvite(ceremony, ceremonyRoom)
}
override suspend fun uninvitedFounders(localChatRoom: LocalChatRoom): Set<HexKey> = try {
FoundingAdmins.ceremonyOf(database, localChatRoom.chatRoom.id)
?.let { (ceremony, ceremonyRoom) ->
MarmotGroupCreation.uninvitedFounders(database, localChatRoom, ceremony, ceremonyRoom)
}
.orEmpty()
} catch (e: Throwable) {
logger.e("Error reading who ${localChatRoom.chatRoom.id}'s founding invite missed", e)
emptySet()
}
override suspend fun followThroughCeremonies(keyPair: KeyPair) =
CeremonyFollowThrough.observe(database = database, chatRepository = this, keyPair = keyPair)
@@ -30,7 +30,9 @@ import mobi.giza.compose.repository.ChatRepository
* 1. a subgroup's parent certifies the room the key derives -- proposed here, and
* signed by the parent's own quorum, which stays their decision;
* 2. the group signs what that room will sign with, its key state;
* 3. the room is created and the members welcomed into it.
* 3. the room is created and the members welcomed into it;
* 4. a room whose founding invite never went out has it finished
* (docs/robust-group-admins.md, Phase 4).
*
* None of the three proposals asks anybody anything: each is a request to the
* group, and the coordinator agreed to make it when they opened the ceremony. So
@@ -62,7 +64,7 @@ object CeremonyFollowThrough {
/** What one call to [takeNextStep] did. */
sealed interface Step {
/** Not this device's to do, or nothing left: not the coordinator, not finished, or the room exists. */
/** Not this device's to do, or nothing left: not the coordinator, not finished, or the room exists with its founders. */
data object NothingToDo : Step
/** Waiting on signatures somebody else gives. */
@@ -74,6 +76,12 @@ object CeremonyFollowThrough {
/** The room exists now. */
data class Created(val chatRoomId: String) : Step
/**
* The room existed without its founders, whose invite never went out, and
* now they have been invited; [notAdded] could not be.
*/
data class Invited(val chatRoomId: String, val notAdded: List<HexKey>) : Step
/** The room cannot be created until these members' devices publish a key package. */
data class BlockedOn(val missing: List<HexKey>) : Step
@@ -166,11 +174,24 @@ object CeremonyFollowThrough {
val thresholdPublicKey = current.thresholdPublicKey ?: return@withLock Step.NothingToDo
val roomId = SharedKeyDerivation.marmotGroupId(thresholdPublicKey)
if (database.chatRoomDao().findChatRoomById(roomId) != null) return@withLock Step.NothingToDo
val ceremonyRoom = database.chatRoomDao().findChatRoomById(current.chatRoomId)
?: return@withLock Step.Stuck("The room ceremony ${current.id} ran in is gone")
// 4. The room exists: finish its founding invite if it never went out.
// Only a room still at epoch 0 has anything to finish, so nobody who
// left is ever invited back -- see `MarmotGroupCreation.uninvitedFounders`.
if (database.chatRoomDao().findChatRoomById(roomId) != null) {
return@withLock when (val invite = chatRepository.finishFoundingInvite(current, ceremonyRoom)) {
MarmotGroupCreation.FoundingInvite.NothingToDo -> Step.NothingToDo
is MarmotGroupCreation.FoundingInvite.Sent -> {
logger.i("Ceremony ${current.id}: invited ${invite.invited.size} founder(s) to $roomId")
Step.Invited(roomId, invite.notAdded)
}
is MarmotGroupCreation.FoundingInvite.BlockedOn -> Step.BlockedOn(invite.missing)
is MarmotGroupCreation.FoundingInvite.Failed -> Step.Stuck(invite.reason)
}
}
val participants = ChillDkgRitualManager.participantsOf(current, ceremonyRoom)
val parent = current.parentChatRoomId
@@ -6,6 +6,7 @@ import com.vitorpamplona.quartz.nip01Core.core.HexKey
import mobi.giza.compose.database.GizaDatabase
import mobi.giza.compose.database.model.ChatMessage
import mobi.giza.compose.database.model.DkgSession
import mobi.giza.compose.database.model.intermdiate.LocalChatRoom
import mobi.giza.compose.nostr.subgroup.SubgroupBirthCertificateEvent
/**
@@ -94,6 +95,18 @@ object FoundingAdmins {
return check
}
/**
* The ceremony that made [chatRoomId]'s key, as this device holds it, and the
* room it ran in -- or null where this device holds no key state for the room,
* no record of that ceremony, or no longer the room it ran in.
*/
suspend fun ceremonyOf(database: GizaDatabase, chatRoomId: String): Pair<DkgSession, LocalChatRoom>? {
val state = database.groupKeyStateDao().getByChatRoomId(chatRoomId) ?: return null
val ceremony = database.dkgSessionDao().getSessionById(state.dkgSessionId) ?: return null
val ceremonyRoom = database.chatRoomDao().findChatRoomById(ceremony.chatRoomId) ?: return null
return ceremony to ceremonyRoom
}
/** The ceremony's participants, or null when this device holds no record of them. */
private suspend fun foundersOf(database: GizaDatabase, ceremony: DkgSession?): Set<HexKey>? {
ceremony ?: return null
@@ -4,6 +4,7 @@ import co.touchlab.kermit.Logger
import mobi.giza.compose.nostr.MarmotProfile
import com.vitorpamplona.quartz.nip01Core.core.HexKey
import com.vitorpamplona.quartz.nip01Core.crypto.KeyPair
import com.vitorpamplona.mls.group.MlsGroup
import kotlinx.coroutines.async
import kotlinx.coroutines.awaitAll
import kotlinx.coroutines.coroutineScope
@@ -11,6 +12,7 @@ import kotlinx.coroutines.flow.filterNotNull
import kotlinx.coroutines.flow.first
import kotlinx.coroutines.withTimeoutOrNull
import mobi.giza.compose.database.GizaDatabase
import mobi.giza.compose.database.model.Participant
import mobi.giza.compose.database.model.DkgSession
import mobi.giza.compose.database.model.MarmotKeyPackage
import mobi.giza.compose.database.model.intermdiate.LocalChatRoom
@@ -177,8 +179,16 @@ object MarmotGroupCreation {
// there is a row to file it against.
GroupKeyStateManager.adopt(database, groupId)
// Read again now the routing is on file. The row above predates it, and a
// room with no routing row is addressed by its own id -- so on the current
// profile the invite went to a second copy of the group the engine adopted
// under the room id, and the copy at the real address still held only its
// creator. Every later invite then took the founding path: no commit, and
// nobody already in the room told. docs/robust-group-admins.md, Phase 4.
val routedRoom = chatRepository.getChatRoomByIdentifier(groupId) ?: localChatRoom
val notAdded = runCatching {
chatRepository.addMembers(localChatRoom = localChatRoom, peers = addable)
chatRepository.addMembers(localChatRoom = routedRoom, peers = addable)
}.onFailure {
logger.e("Failed to add members to $groupId", it)
}.getOrElse { addable.map { (publicKey, _) -> publicKey } }
@@ -257,6 +267,96 @@ object MarmotGroupCreation {
)
}
/** What [finishFoundingInvite] did. */
sealed interface FoundingInvite {
/** Every founding member holds a leaf, or the room is past its founding. */
data object NothingToDo : FoundingInvite
/** The founders who were missing were invited; [notAdded] could not be. */
data class Sent(val chatRoomId: String, val invited: List<HexKey>, val notAdded: List<HexKey>) : FoundingInvite
/** Some founders have no published key package, so nobody was invited. */
data class BlockedOn(val missing: List<HexKey>) : FoundingInvite
data class Failed(val reason: String) : FoundingInvite
}
/**
* The founding members of [room] its founding invite never reached: listed as
* admins, holding no leaf, in a room nothing has been committed to since it was
* made.
*
* Only at epoch 0. A room is created at epoch 0 and its founding invite is the
* commit that leaves it -- so a room still there is one whose invite failed
* outright, and nobody in it has left or been removed, because either would
* have been a commit. Past epoch 0 a founder without a leaf might be one who
* left, and inviting them back is not this function's to decide
* (docs/robust-group-admins.md, Out of scope).
*
* A device holds a room at epoch 0 only if it made it: everybody else's copy
* comes from a Welcome, which is already past the founding commit.
*/
suspend fun uninvitedFounders(
database: GizaDatabase,
room: LocalChatRoom,
ceremony: DkgSession,
ceremonyRoom: LocalChatRoom,
): Set<HexKey> {
val group = liveGroupOf(database, room) ?: return emptySet()
if (group.epoch != 0L) return emptySet()
val holdingLeaves = Participant.fromMlsGroup(group, room.chatRoom.id, null).map { it.participantPublicKey }.toSet()
return ChillDkgRitualManager.participantsOf(ceremony, ceremonyRoom) - holdingLeaves
}
/**
* Invites the founding members [room]'s founding invite never reached -- see
* [uninvitedFounders] -- in one commit, as [create] would have.
*
* Every key package first, as at creation: a founder who cannot be invited yet
* holds the rest back rather than leaving the room to a second, partial
* founding. docs/robust-group-admins.md, Phase 4.
*/
suspend fun finishFoundingInvite(
database: GizaDatabase,
chatRepository: ChatRepository,
ceremony: DkgSession,
ceremonyRoom: LocalChatRoom,
keyPackageLookupTimeout: Long = KEY_PACKAGE_LOOKUP_TIMEOUT,
): FoundingInvite {
val thresholdPublicKey = ceremony.thresholdPublicKey ?: return FoundingInvite.NothingToDo
val groupId = SharedKeyDerivation.marmotGroupId(thresholdPublicKey)
val room = chatRepository.getChatRoomByIdentifier(groupId) ?: return FoundingInvite.NothingToDo
val uninvited = uninvitedFounders(database, room, ceremony, ceremonyRoom)
if (uninvited.isEmpty()) return FoundingInvite.NothingToDo
val keyPackages = keyPackagesFor(chatRepository, uninvited.toList(), keyPackageLookupTimeout, room.chatRoom.marmotProfile())
val missing = keyPackages.filter { it.second == null }.map { it.first }
if (missing.isNotEmpty()) {
logger.w("Not finishing $groupId's founding invite: no key package for $missing")
return FoundingInvite.BlockedOn(missing)
}
val addable = keyPackages.mapNotNull { (publicKey, keyPackage) -> keyPackage?.let { publicKey to it } }
val notAdded = runCatching {
chatRepository.addMembers(localChatRoom = room, peers = addable)
}.onFailure {
logger.e("Failed to finish $groupId's founding invite", it)
}.getOrElse { return FoundingInvite.Failed(it.message ?: "Couldn't invite them. Please try again.") }
flagAdmins(database, groupId)
logger.i("Finished $groupId's founding invite: ${addable.size - notAdded.size} invited, ${notAdded.size} not")
return FoundingInvite.Sent(groupId, addable.map { it.first }, notAdded)
}
/** The room's live group: the row's on the legacy profile, the engine's on the current one. */
private suspend fun liveGroupOf(database: GizaDatabase, room: LocalChatRoom): MlsGroup? =
when (room.chatRoom.marmotProfile()) {
MarmotProfile.LEGACY -> room.chatRoom.toMlsGroup()
MarmotProfile.CURRENT -> database.marmotEngine.groupFor(room.routingId)
}
/**
* Writes `adminAt` on the rows of the members this room was created with as
* admins.
@@ -224,6 +224,19 @@ interface ChatRepository {
parentChatRoomId: HexKey? = null,
): MarmotGroupCreation.Outcome
/**
* Invites the founding members [ceremony]'s room never reached, when its
* founding invite never went out. See `MarmotGroupCreation.finishFoundingInvite`
* and docs/robust-group-admins.md, Phase 4.
*/
suspend fun finishFoundingInvite(ceremony: DkgSession, ceremonyRoom: LocalChatRoom): MarmotGroupCreation.FoundingInvite
/** [finishFoundingInvite] for the room [chatRoomId], through the ceremony its key state names. */
suspend fun finishFoundingInvite(chatRoomId: String): MarmotGroupCreation.FoundingInvite
/** The founders [localChatRoom]'s founding invite never reached; empty when there are none to reach. */
suspend fun uninvitedFounders(localChatRoom: LocalChatRoom): Set<HexKey>
/**
* Takes every key ceremony [keyPair] coordinated from a key to a working room,
* until cancelled: asks the group to sign its key state (a subgroup's parent to
@@ -468,6 +481,14 @@ interface ChatRepository {
override suspend fun followThroughCeremonies(keyPair: KeyPair) = Unit
override suspend fun finishFoundingInvite(ceremony: DkgSession, ceremonyRoom: LocalChatRoom) =
MarmotGroupCreation.FoundingInvite.NothingToDo
override suspend fun finishFoundingInvite(chatRoomId: String) =
MarmotGroupCreation.FoundingInvite.NothingToDo
override suspend fun uninvitedFounders(localChatRoom: LocalChatRoom): Set<HexKey> = emptySet()
override suspend fun sendChatMessage(
text: String,