fix: rooms joined before the Welcome wrote flags are caught up at start (robust-group-admins, Phase 6)

AdminFlags.reconcileAll runs AdminFlags.reconcile over every room this
device holds MLS state for, and the sync start calls it once, through
ChatRepository.reconcileAdminFlags, beside the ceremony resume. A room
joined before Phase 2 flags nobody on the joiner's device until some
commit arrives, and a robust group's admin room may never get one.

No stored "done" flag: a room whose rows already agree is a read and no
write, so a second start costs one decode per room. A room that will not
decode is logged and passed over.

RobustGroupAdminsJvmTest: a joiner's cleared flags are restored on both
profiles, and a second pass changes nothing. jvmTest 1250/1250,
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:21:12 +02:00
1 parent baf63b8d7a
commit 01e33f5a6f
5 files changed
+62

No files matched your search

@@ -7,6 +7,7 @@ import com.vitorpamplona.quartz.nip01Core.core.Kind
import com.vitorpamplona.quartz.nip01Core.core.hexToByteArray
import com.vitorpamplona.quartz.nip01Core.crypto.EventHasher
import com.vitorpamplona.quartz.nip01Core.crypto.KeyPair
import mobi.giza.compose.managers.AdminFlags
import mobi.giza.compose.managers.FoundingAdmins
import mobi.giza.compose.managers.SharedKeyDerivation
import mobi.giza.compose.managers.CeremonyFollowThrough
@@ -128,6 +129,8 @@ class DatabaseChatRepository(
null
}
override suspend fun reconcileAdminFlags(): Int = AdminFlags.reconcileAll(database)
override suspend fun followThroughCeremonies(keyPair: KeyPair) =
CeremonyFollowThrough.observe(database = database, chatRepository = this, keyPair = keyPair)
@@ -56,4 +56,31 @@ object AdminFlags {
}
return changed.size
}
/**
* [reconcile] for every room this device holds MLS state for and is still in.
*
* Run once per start, for the rooms made before the Welcome wrote any flags:
* a joiner's rows there flag nobody until some commit arrives, and in a
* robust group's admin room one may never arrive. No stored "done" flag -- a
* room whose rows already agree is a read and no write, so a second run costs
* one decode per room and a preference would only be one more thing to keep
* right. docs/robust-group-admins.md, Phase 6.
*
* One room that will not decode is logged and passed over, not fatal to the
* rest.
*
* @return how many rows changed, across every room.
*/
suspend fun reconcileAll(database: GizaDatabase): Int =
database.chatRoomDao().getChatRoomIdsWithMlsState().sumOf { chatRoomId ->
runCatching {
val group = database.chatRoomDao().findChatRoomById(chatRoomId)?.chatRoom?.toMlsGroup()
?: return@runCatching 0
reconcile(database, chatRoomId, group)
}.getOrElse {
logger.e("Could not bring $chatRoomId's admin flags in line", it)
0
}
}
}
@@ -245,6 +245,13 @@ interface ChatRepository {
*/
suspend fun adminRoomOf(chatRoomId: String): String?
/**
* Brings every Marmot room's admin flags in line with its group's policy, for
* the rooms joined before the Welcome wrote any. Once per start. See
* `AdminFlags.reconcileAll` and docs/robust-group-admins.md, Phase 6.
*/
suspend fun reconcileAdminFlags(): Int
/**
* 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
@@ -499,6 +506,8 @@ interface ChatRepository {
override suspend fun adminRoomOf(chatRoomId: String): String? = null
override suspend fun reconcileAdminFlags(): Int = 0
override suspend fun sendChatMessage(
text: String,
@@ -246,6 +246,13 @@ class SynchronizationViewModel(
// was waiting for. docs/automatic-key-ceremony.md, Phase 3.
runCatching { nostrRepository.resumeRunningCeremonies(keyPair) }
.onFailure { logger.e("Could not resume running key ceremonies", it) }
// The rooms joined before the Welcome wrote admin flags
// flag nobody until some commit arrives, and a robust
// group's admin room may never get one. Caught up here,
// once a start; a room already right is read, not
// written. docs/robust-group-admins.md, Phase 6.
runCatching { chatRepository.reconcileAdminFlags() }
.onFailure { logger.e("Could not bring admin flags in line", it) }
launch(Dispatchers.IO) { observePendingBroadcastNostrEventRequests(keyPair) }
// Runs until cancelled rather than draining a queue, but it
// belongs here for the same reason the pumps do: it needs the
@@ -227,6 +227,22 @@ class RobustGroupAdminsJvmTest {
}
}
/**
* A room joined before Phase 2, whose joiner's rows flag nobody, is caught up
* at the next start, once. docs/robust-group-admins.md, Phase 6.
*/
@Test
fun `a room joined before the Welcome wrote flags is caught up at start`() = onEveryProfile { profile, phones ->
val founders = phones.map { it.publicKey }.toSet()
val joiner = phones.last()
joiner.db.participantDao().upsert(joiner.room().localParticipants.map { it.participant.copy(adminAt = null) })
assertTrue(joiner.flaggedAdmins().isEmpty(), "the fixture did not clear the flags ($profile)")
assertEquals(founders.size, AdminFlags.reconcileAll(joiner.db), "($profile)")
assertEquals(founders, joiner.flaggedAdmins(), "($profile)")
assertEquals(0, AdminFlags.reconcileAll(joiner.db), "and a second start changes nothing ($profile)")
}
// ---- The founding invite: docs/robust-group-admins.md, Phase 4 -------------
private val ceremonyRoomId = "ce1e".repeat(16)