From 01e33f5a6f25f523e2b128bdc1cd42488c498069 Mon Sep 17 00:00:00 2001 From: Kgothatso Ngako Date: Fri, 20 Nov 2026 01:21:12 +0200 Subject: [PATCH] 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 --- .../repository/DatabaseChatRepository.kt | 3 +++ .../mobi/giza/compose/managers/AdminFlags.kt | 27 +++++++++++++++++++ .../giza/compose/repository/ChatRepository.kt | 9 +++++++ .../ui/view/model/SynchronizationViewModel.kt | 7 +++++ .../managers/RobustGroupAdminsJvmTest.kt | 16 +++++++++++ 5 files changed, 62 insertions(+) diff --git a/composeApp/src/commonMain/kotlin/mobi/giza/compose/database/repository/DatabaseChatRepository.kt b/composeApp/src/commonMain/kotlin/mobi/giza/compose/database/repository/DatabaseChatRepository.kt index 0b28ef4f..55c42504 100644 --- a/composeApp/src/commonMain/kotlin/mobi/giza/compose/database/repository/DatabaseChatRepository.kt +++ b/composeApp/src/commonMain/kotlin/mobi/giza/compose/database/repository/DatabaseChatRepository.kt @@ -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) diff --git a/composeApp/src/commonMain/kotlin/mobi/giza/compose/managers/AdminFlags.kt b/composeApp/src/commonMain/kotlin/mobi/giza/compose/managers/AdminFlags.kt index cb121030..85a9156c 100644 --- a/composeApp/src/commonMain/kotlin/mobi/giza/compose/managers/AdminFlags.kt +++ b/composeApp/src/commonMain/kotlin/mobi/giza/compose/managers/AdminFlags.kt @@ -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 + } + } } diff --git a/composeApp/src/commonMain/kotlin/mobi/giza/compose/repository/ChatRepository.kt b/composeApp/src/commonMain/kotlin/mobi/giza/compose/repository/ChatRepository.kt index 894c444e..df13f39f 100644 --- a/composeApp/src/commonMain/kotlin/mobi/giza/compose/repository/ChatRepository.kt +++ b/composeApp/src/commonMain/kotlin/mobi/giza/compose/repository/ChatRepository.kt @@ -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, diff --git a/composeApp/src/commonMain/kotlin/mobi/giza/compose/ui/view/model/SynchronizationViewModel.kt b/composeApp/src/commonMain/kotlin/mobi/giza/compose/ui/view/model/SynchronizationViewModel.kt index c292ec61..8b4c5ac3 100644 --- a/composeApp/src/commonMain/kotlin/mobi/giza/compose/ui/view/model/SynchronizationViewModel.kt +++ b/composeApp/src/commonMain/kotlin/mobi/giza/compose/ui/view/model/SynchronizationViewModel.kt @@ -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 diff --git a/composeApp/src/jvmTest/kotlin/mobi/giza/compose/managers/RobustGroupAdminsJvmTest.kt b/composeApp/src/jvmTest/kotlin/mobi/giza/compose/managers/RobustGroupAdminsJvmTest.kt index cd660d8c..99d6a32b 100644 --- a/composeApp/src/jvmTest/kotlin/mobi/giza/compose/managers/RobustGroupAdminsJvmTest.kt +++ b/composeApp/src/jvmTest/kotlin/mobi/giza/compose/managers/RobustGroupAdminsJvmTest.kt @@ -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)