diff --git a/composeApp/src/commonMain/kotlin/to/curare/compose/database/dao/BroadcastNostrEventRequestDao.kt b/composeApp/src/commonMain/kotlin/to/curare/compose/database/dao/BroadcastNostrEventRequestDao.kt index 343ebcbd..3125cee6 100644 --- a/composeApp/src/commonMain/kotlin/to/curare/compose/database/dao/BroadcastNostrEventRequestDao.kt +++ b/composeApp/src/commonMain/kotlin/to/curare/compose/database/dao/BroadcastNostrEventRequestDao.kt @@ -17,6 +17,10 @@ interface BroadcastNostrEventRequestDao { @Query("SELECT * FROM BroadcastNostrEventRequest WHERE nostrEventId = :nostrEventId ORDER BY createdAt ASC") suspend fun getFirstBroadcastNostrEventRequestByNostrEventId(nostrEventId: String): BroadcastNostrEventRequest? + /** Every relay's request for one event: the obligation's scope, as rows. */ + @Query("SELECT * FROM BroadcastNostrEventRequest WHERE nostrEventId = :nostrEventId ORDER BY createdAt ASC, id ASC") + suspend fun getBroadcastNostrEventRequestsByNostrEventId(nostrEventId: String): List + // See NegentropySynchronizeRequestDao: the old `createdAt > :createdAt` bound a // once-evaluated `Clock.System.now()` for the life of the Flow. Because Instants are // persisted at second resolution, that hid every broadcast enqueued in the observer's @@ -33,7 +37,9 @@ interface BroadcastNostrEventRequestDao { * A request is flipped to "processing" before the publish is attempted, and the publish can * end in a timeout or a socket error that leaves it there. Nothing observes "processing" or * "failed", so those rows are dead weight. Requeue everything a previous process left behind - * when we start up, so an interrupted publish is retried instead of lost. + * when we start up, so an interrupted publish is retried instead of lost. Not "rejected": + * that is a relay's answer to these exact bytes, and it does not change on a retry -- see + * [to.curare.compose.database.model.BroadcastStatus]. */ @Query("UPDATE BroadcastNostrEventRequest SET status = 'pending' WHERE status IN ('processing', 'failed') AND createdAt <= :staleBefore") suspend fun requeueStaleBroadcastNostrEventRequests(staleBefore: Instant): Int diff --git a/composeApp/src/commonMain/kotlin/to/curare/compose/database/dao/MarmotOutboundDao.kt b/composeApp/src/commonMain/kotlin/to/curare/compose/database/dao/MarmotOutboundDao.kt index e0fd9b7f..24cd9cd1 100644 --- a/composeApp/src/commonMain/kotlin/to/curare/compose/database/dao/MarmotOutboundDao.kt +++ b/composeApp/src/commonMain/kotlin/to/curare/compose/database/dao/MarmotOutboundDao.kt @@ -522,6 +522,25 @@ abstract class MarmotOutboundDao( ) } + /** + * A change to the room -- a removal, a disband step -- was refused by every + * relay it was sent to, and so never happened; the line written when it was + * asked for would otherwise stand uncorrected. + */ + suspend fun announceChangeRejected( + chatRoomId: HexKey, + userPublicKey: HexKey, + reason: String?, + ) { + val why = reason?.takeIf { it.isNotBlank() }?.let { ": $it" } ?: "" + announceMembership( + chatRoomId = chatRoomId, + userPublicKey = userPublicKey, + messageType = ChatMessage.TYPE_CHANGE_REJECTED, + content = "The relays refused the change, so it was not made$why", + ) + } + private suspend fun inviteMember( chatRoomId: HexKey, mlsGroup: MlsGroup, diff --git a/composeApp/src/commonMain/kotlin/to/curare/compose/database/model/BroadcastStatus.kt b/composeApp/src/commonMain/kotlin/to/curare/compose/database/model/BroadcastStatus.kt new file mode 100644 index 00000000..0eaca341 --- /dev/null +++ b/composeApp/src/commonMain/kotlin/to/curare/compose/database/model/BroadcastStatus.kt @@ -0,0 +1,47 @@ +package to.curare.compose.database.model + +import to.curare.compose.exceptions.NostrPublishException + +/** + * What a `BroadcastNostrEventRequest.status` can be, and how a relay's answer + * becomes one. + * + * Four of these were string literals scattered over the sync pump, the + * navigation gate and the DAO's requeue; the fifth is new. [REJECTED] is a + * relay's `["OK", id, false, reason]` with a reason that will not change on a + * retry -- `blocked:`, `invalid:`, `pow:`, `restricted:` -- and is the one + * terminal status the requeue on launch leaves alone: the same bytes sent to + * the same relay get the same answer. A `rate-limited:` or `error:` refusal is + * [FAILED] like a timeout, because those do change. A `duplicate:` refusal is + * [PUBLISHED]: the relay is saying it already has the event, which is what + * published means, and it is the answer a byte-identical retry of a commit + * gets from a relay that took the first copy. + * + * The distinction matters most to publish-before-apply: a commit's obligation + * is discarded only when every relay in its scope has [REJECTED] it, and stays + * pending through any number of [FAILED]s. See docs/marmot-v2.md, Phase 6. + */ +object BroadcastStatus { + const val PENDING = "pending" + const val PROCESSING = "processing" + const val PUBLISHED = "published" + const val FAILED = "failed" + const val REJECTED = "rejected" + + /** NIP-01 `OK` prefixes a relay will keep answering with. */ + private val TERMINAL_PREFIXES = listOf("blocked:", "invalid:", "pow:", "restricted:") + + /** The status a publish attempt ends in, from what it threw or was answered. */ + fun of(error: Throwable?): String = when { + error == null -> PUBLISHED + error !is NostrPublishException -> FAILED + else -> { + val reason = error.message.orEmpty().trim() + when { + reason.startsWith("duplicate:") -> PUBLISHED + TERMINAL_PREFIXES.any { reason.startsWith(it) } -> REJECTED + else -> FAILED + } + } + } +} diff --git a/composeApp/src/commonMain/kotlin/to/curare/compose/database/model/ChatMessage.kt b/composeApp/src/commonMain/kotlin/to/curare/compose/database/model/ChatMessage.kt index 558cfc3f..050fad7b 100644 --- a/composeApp/src/commonMain/kotlin/to/curare/compose/database/model/ChatMessage.kt +++ b/composeApp/src/commonMain/kotlin/to/curare/compose/database/model/ChatMessage.kt @@ -541,6 +541,13 @@ data class ChatMessage( const val TYPE_MEMBER_LEFT = "memberLeft" const val TYPE_GROUP_DISBANDED = "groupDisbanded" + /** + * A change this device asked for that every relay refused, so the line + * written when it was asked for -- a removal, a disband -- did not come + * true. The invite's own version is [TYPE_MEMBER_INVITE_FAILED]. + */ + const val TYPE_CHANGE_REJECTED = "changeRejected" + /** * Every membership line, for the one check the transcript dispatches on. * @@ -554,6 +561,7 @@ data class ChatMessage( TYPE_MEMBER_ADDED, TYPE_MEMBER_LEFT, TYPE_GROUP_DISBANDED, + TYPE_CHANGE_REJECTED, ) const val TYPE_UNDECRYPTABLE_OUTER_LAYER = "undecryptableOuterLayer" diff --git a/composeApp/src/commonMain/kotlin/to/curare/compose/database/repository/DatabaseNostrRepository.kt b/composeApp/src/commonMain/kotlin/to/curare/compose/database/repository/DatabaseNostrRepository.kt index cfde64dc..a9104270 100644 --- a/composeApp/src/commonMain/kotlin/to/curare/compose/database/repository/DatabaseNostrRepository.kt +++ b/composeApp/src/commonMain/kotlin/to/curare/compose/database/repository/DatabaseNostrRepository.kt @@ -3,6 +3,7 @@ package to.curare.compose.database.repository import to.curare.compose.database.model.MarmotCommitResult import to.curare.compose.database.GENESIS_AT import to.curare.compose.database.model.BroadcastNostrEventRequest +import to.curare.compose.database.model.BroadcastStatus import to.curare.compose.database.model.NostrEvent import to.curare.compose.database.model.Profile import to.curare.compose.database.model.UnsignedNostrEvent @@ -473,7 +474,8 @@ class DatabaseNostrRepository( override suspend fun broadcastProcessed( broadcastNostrEventRequest: BroadcastNostrEventRequest, - status: String + status: String, + reason: String?, ) { database.broadcastNostrEventRequestDao().upsert( broadcastNostrEventRequest.copy( @@ -481,7 +483,11 @@ class DatabaseNostrRepository( ) ) - if (status == "published") { + if (status == BroadcastStatus.REJECTED) { + discardCommitRejectedEverywhere(broadcastNostrEventRequest.nostrEventId, reason) + } + + if (status == BroadcastStatus.PUBLISHED) { val broadcastNostrEventReceiptId = database.broadcastNostrEventReceiptDao().upsert( to.curare.compose.database.model.BroadcastNostrEventReceipt( nostrEventId = broadcastNostrEventRequest.nostrEventId, @@ -572,6 +578,47 @@ class DatabaseNostrRepository( } } + /** + * Publish before apply, the other half: a commit every relay in its scope has + * refused is discarded, and the room is free to make another. + * + * One relay's refusal decides nothing -- another may still accept, and a + * timeout on another is retried on the next launch with the same bytes -- so + * the obligation goes only when every request row for the event is + * "rejected". Then the gate drops the pending state, the live group having + * never advanced, and `canPrepareLocalCommit` is true again; the invite or + * the removal that was asked for is simply not done, and the room says so in + * the relay's words, because the screen that asked closed when the commit + * was made and the line is the only account the user gets. See + * docs/marmot-v2.md, Phase 6. + */ + private suspend fun discardCommitRejectedEverywhere(nostrEventId: HexKey, reason: String?) { + val marmotCommitResult = database.marmotCommitResultDao().getMarmotCommitRequestById(nostrEventId) ?: return + val obligationId = marmotCommitResult.obligationId ?: return + val requests = database.broadcastNostrEventRequestDao().getBroadcastNostrEventRequestsByNostrEventId(nostrEventId) + if (requests.any { it.status != BroadcastStatus.REJECTED }) return + + if (!database.marmotEngine.discardRejected(obligationId)) return + logger.w("Commit $nostrEventId rejected by every relay in scope; its obligation is discarded: $reason") + + if (marmotCommitResult.peerKeyPackageEventId != MarmotCommitResult.NO_INVITEE) { + database.marmotOutboundDao().announceInviteFailed( + chatRoomId = marmotCommitResult.chatRoomId, + userPublicKey = marmotCommitResult.userPublicKey, + peerPublicKey = database.marmotKeyPackageDao() + .getMarmotKeyPackageById(marmotCommitResult.peerKeyPackageEventId) + ?.publicKey, + reason = reason?.let { "the relays refused it ($it)" } ?: "the relays refused it", + ) + } else { + database.marmotOutboundDao().announceChangeRejected( + chatRoomId = marmotCommitResult.chatRoomId, + userPublicKey = marmotCommitResult.userPublicKey, + reason = reason, + ) + } + } + override suspend fun synchronizeNostrEventRequestProcessed(synchronizeNostrEventRequest: to.curare.compose.database.model.SynchronizeNostrEventRequest) { database.synchronizeNostrEventRequestDao().upsert( synchronizeNostrEventRequest.copy( diff --git a/composeApp/src/commonMain/kotlin/to/curare/compose/managers/MarmotEngine.kt b/composeApp/src/commonMain/kotlin/to/curare/compose/managers/MarmotEngine.kt index ba8b03b9..e51358bb 100644 --- a/composeApp/src/commonMain/kotlin/to/curare/compose/managers/MarmotEngine.kt +++ b/composeApp/src/commonMain/kotlin/to/curare/compose/managers/MarmotEngine.kt @@ -136,6 +136,23 @@ class MarmotEngine( database.marmotOutboundDao().continueDisband(localChatRoom) } + /** + * Every relay in scope refused [obligationId]: discard its pending state. + * + * The live group never advanced, so there is nothing to undo; the gate drops + * the clone, the lifecycle returns to `STABLE`, and the next commit may be + * prepared. Nothing is told to convergence -- a commit no relay carries is + * not a branch any member will ever see. Returns whether there was an + * obligation to discard. + */ + suspend fun discardRejected(obligationId: HexKey): Boolean { + ensureRestored() + val obligation = gate.allPending().firstOrNull { it.obligationId == obligationId } ?: return false + gate.resolve(obligationId, PublishOutcome.FAILED) + logger.w("Commit ${obligationId.take(8)}… discarded; ${obligation.groupId.take(8)}… stays at epoch ${obligation.priorState.groupContext.epoch}") + return true + } + /** * Wait for [routingId]'s convergence pass, if one is open, to settle. * diff --git a/composeApp/src/commonMain/kotlin/to/curare/compose/repository/NostrRepository.kt b/composeApp/src/commonMain/kotlin/to/curare/compose/repository/NostrRepository.kt index aa6fc7c1..bc61415f 100644 --- a/composeApp/src/commonMain/kotlin/to/curare/compose/repository/NostrRepository.kt +++ b/composeApp/src/commonMain/kotlin/to/curare/compose/repository/NostrRepository.kt @@ -111,7 +111,15 @@ interface NostrRepository { activeKeyPair: KeyPair ) - suspend fun broadcastProcessed(broadcastNostrEventRequest: BroadcastNostrEventRequest, status: String = "processing") + /** + * Record how a publish attempt ended. [reason] is the relay's own words when it + * refused, for the line that tells the user a change was rejected. + */ + suspend fun broadcastProcessed( + broadcastNostrEventRequest: BroadcastNostrEventRequest, + status: String = "processing", + reason: String? = null, + ) /** * Move broadcasts a previous process left in "processing"/"failed" back to "pending" so an @@ -325,7 +333,8 @@ interface NostrRepository { override suspend fun broadcastProcessed( broadcastNostrEventRequest: BroadcastNostrEventRequest, - status: String + status: String, + reason: String?, ) { TODO("Not yet implemented") } diff --git a/composeApp/src/commonMain/kotlin/to/curare/compose/ui/composable/widgets/chat/ChatTranscript.kt b/composeApp/src/commonMain/kotlin/to/curare/compose/ui/composable/widgets/chat/ChatTranscript.kt index bda4af15..b15c129f 100644 --- a/composeApp/src/commonMain/kotlin/to/curare/compose/ui/composable/widgets/chat/ChatTranscript.kt +++ b/composeApp/src/commonMain/kotlin/to/curare/compose/ui/composable/widgets/chat/ChatTranscript.kt @@ -834,6 +834,7 @@ private fun RitualNotice( ChatMessage.TYPE_MEMBER_INVITED -> Icons.Default.PersonAdd ChatMessage.TYPE_MEMBER_INVITE_SENT -> Icons.Default.Upload ChatMessage.TYPE_MEMBER_INVITE_FAILED -> Icons.Default.ErrorOutline + ChatMessage.TYPE_CHANGE_REJECTED -> Icons.Default.ErrorOutline else -> Icons.Default.PanTool } @@ -854,7 +855,8 @@ private fun RitualNotice( val tint = when { chatMessage.messageType == ChatMessage.TYPE_DKG_FAILED || chatMessage.messageType == ChatMessage.TYPE_FROST_FAILED || - chatMessage.messageType == ChatMessage.TYPE_MEMBER_INVITE_FAILED -> + chatMessage.messageType == ChatMessage.TYPE_MEMBER_INVITE_FAILED || + chatMessage.messageType == ChatMessage.TYPE_CHANGE_REJECTED -> MaterialTheme.colorScheme.error isRequest -> MaterialTheme.colorScheme.primary else -> MaterialTheme.colorScheme.onSurfaceVariant diff --git a/composeApp/src/commonMain/kotlin/to/curare/compose/ui/view/model/SynchronizationViewModel.kt b/composeApp/src/commonMain/kotlin/to/curare/compose/ui/view/model/SynchronizationViewModel.kt index 50a4b4f1..050c5098 100644 --- a/composeApp/src/commonMain/kotlin/to/curare/compose/ui/view/model/SynchronizationViewModel.kt +++ b/composeApp/src/commonMain/kotlin/to/curare/compose/ui/view/model/SynchronizationViewModel.kt @@ -4,6 +4,7 @@ import androidx.lifecycle.ViewModel import androidx.lifecycle.ViewModelProvider import androidx.lifecycle.viewmodel.initializer import androidx.lifecycle.viewmodel.viewModelFactory +import to.curare.compose.database.model.BroadcastStatus import to.curare.compose.database.model.BroadcastNostrEventRequest import to.curare.compose.database.model.SynchronizeNostrEventRequest import to.curare.compose.database.model.types.SynchronizationFilter @@ -830,19 +831,21 @@ class SynchronizationViewModel( "failed" ) }.collect { nostrPublishResult -> + // A relay's answer decides the status, not merely whether + // there was an error: a refusal it will keep giving is + // "rejected" and is not retried, a `duplicate:` is the + // event being there already. See BroadcastStatus. + val status = BroadcastStatus.of(nostrPublishResult.error) if (nostrPublishResult.error != null) { - logger.e("Error publishing note: $nostrPublishResult") - nostrRepository.broadcastProcessed( - localBroadcastNostrEventRequest.broadcastNostrEventRequest, - "failed" - ) + logger.e("Error publishing note ($status): $nostrPublishResult") } else { logger.d("Nostr Publish Result: $nostrPublishResult") - nostrRepository.broadcastProcessed( - localBroadcastNostrEventRequest.broadcastNostrEventRequest, - "published" - ) } + nostrRepository.broadcastProcessed( + localBroadcastNostrEventRequest.broadcastNostrEventRequest, + status, + reason = nostrPublishResult.error?.message, + ) } } } catch (e: CancellationException) { diff --git a/composeApp/src/jvmTest/kotlin/to/curare/compose/managers/PublishBeforeApplyJvmTest.kt b/composeApp/src/jvmTest/kotlin/to/curare/compose/managers/PublishBeforeApplyJvmTest.kt index 6768ac53..447ee0b3 100644 --- a/composeApp/src/jvmTest/kotlin/to/curare/compose/managers/PublishBeforeApplyJvmTest.kt +++ b/composeApp/src/jvmTest/kotlin/to/curare/compose/managers/PublishBeforeApplyJvmTest.kt @@ -11,6 +11,9 @@ import kotlinx.coroutines.Dispatchers import kotlinx.coroutines.runBlocking import to.curare.compose.database.CurareDatabase import to.curare.compose.database.builder.getRoomDatabase +import to.curare.compose.database.model.BroadcastNostrEventRequest +import to.curare.compose.database.model.BroadcastStatus +import to.curare.compose.database.model.ChatMessage import to.curare.compose.database.model.MarmotKeyPackage import to.curare.compose.database.model.NostrEvent import to.curare.compose.database.model.Profile @@ -150,4 +153,41 @@ class PublishBeforeApplyJvmTest { assertNull(db.marmotPublishObligationDao().getAll().firstOrNull()) assertTrue(rebuilt.gate.canPrepareLocalCommit(routingId)) } + + @Test + fun `a commit every relay refuses is discarded, and the room can invite again`() = runBlocking { + val routingId = establishedRoom() + val second = KeyPair() + chatRepository.inviteMember(db.chatRoomDao().findChatRoomById(roomId)!!, second.pubKey.toHexKey(), peerPackage(second)) + val (commitEvent, _) = commitEventFor(second) + // The default scope is one relay; a second row widens it to two, as a room + // whose relay list names two would have. + db.broadcastNostrEventRequestDao().insert(listOf(BroadcastNostrEventRequest(nostrEventId = commitEvent.id, relayURL = "wss://second.example"))) + val requests = db.broadcastNostrEventRequestDao().getBroadcastNostrEventRequestsByNostrEventId(commitEvent.id) + assertEquals(2, requests.size) + + // One relay's refusal decides nothing: the other may still take it. + nostrRepository.broadcastProcessed(requests[0], BroadcastStatus.REJECTED, reason = "blocked: no") + assertEquals(1, db.marmotPublishObligationDao().getAll().size) + assertTrue(!db.marmotEngine.gate.canPrepareLocalCommit(routingId)) + + // Nor does a timeout on the other: that is retried with the same bytes. + nostrRepository.broadcastProcessed(requests[1], BroadcastStatus.FAILED) + assertEquals(1, db.marmotPublishObligationDao().getAll().size) + + // The other relay refusing too is the end of it: the pending state is + // dropped, the room is where it was, and it says why. + nostrRepository.broadcastProcessed(requests[1], BroadcastStatus.REJECTED, reason = "blocked: no") + assertEquals(0, db.marmotPublishObligationDao().getAll().size) + assertEquals(1L, epochOnDisk()) + assertTrue(db.marmotEngine.gate.canPrepareLocalCommit(routingId)) + assertTrue(second.pubKey.toHexKey() !in db.marmotEngine.groupManager.getGroup(routingId)!!.currentMemberIdentities()) + val line = db.chatMessageDao().getChatMessagesByChatRoomId(roomId).map { it.chatMessage } + .last { it.messageType == ChatMessage.TYPE_MEMBER_INVITE_FAILED } + assertTrue("blocked: no" in line.content, line.content) + + // And the room is free: the same peer can be invited again. + chatRepository.inviteMember(db.chatRoomDao().findChatRoomById(roomId)!!, second.pubKey.toHexKey(), peerPackage(second)) + assertEquals(1, db.marmotPublishObligationDao().getAll().size) + } }