From f114a8ee688cdfc3373546929857cd61959f5a40 Mon Sep 17 00:00:00 2001 From: Ovi Trif Date: Wed, 11 Nov 2026 14:46:41 +0100 Subject: [PATCH 1/4] fix: save the profile without waiting for an unchanged copy A save with no tag change writes pubky.app and lets the Paykit copy write follow in the background, so Edit Profile Save takes one write again. --- .../java/to/bitkit/repositories/PubkyRepo.kt | 13 ++++++++++-- .../to/bitkit/repositories/PubkyRepoTest.kt | 21 +++++++++++++++++++ 2 files changed, 32 insertions(+), 2 deletions(-) diff --git a/app/src/main/java/to/bitkit/repositories/PubkyRepo.kt b/app/src/main/java/to/bitkit/repositories/PubkyRepo.kt index addc9a8c5b..a784b3ad81 100644 --- a/app/src/main/java/to/bitkit/repositories/PubkyRepo.kt +++ b/app/src/main/java/to/bitkit/repositories/PubkyRepo.kt @@ -1335,11 +1335,20 @@ class PubkyRepo @Inject constructor( } // Tags live only in the Paykit copy, so a changed tag that it does not take is reported. val copyData = profile.toProfileData().toPaykitProfile() - val copyError = keptTags?.let { + val writeCopy = suspend { runSuspendCatching { profileWrite(generation) { pubkyService.publishPaykitProfile(copyData, publicKey) } } .onSuccess { tagsUnavailable = false } .onFailure { Logger.warn("Failed to update Bitkit profile copy", it, context = TAG) } - .exceptionOrNull() + } + val copyError = keptTags?.let { + // With no tag change to report, the save does not wait for the copy. The write lock keeps it ahead of the + // next save's copy write, and a deletion refuses or waits for it. + if (it == baseTags) { + scope.launch { writeCopy() } + null + } else { + writeCopy().exceptionOrNull() + } } val saved = if (keptTags != null && copyError == null) profile else profile.copy(tags = baseTags) val tagsLost = (keptTags == null || copyError != null) && tags != baseTags diff --git a/app/src/test/java/to/bitkit/repositories/PubkyRepoTest.kt b/app/src/test/java/to/bitkit/repositories/PubkyRepoTest.kt index e4b8b77df3..8c695d431e 100644 --- a/app/src/test/java/to/bitkit/repositories/PubkyRepoTest.kt +++ b/app/src/test/java/to/bitkit/repositories/PubkyRepoTest.kt @@ -974,6 +974,27 @@ class PubkyRepoTest : BaseUnitTest() { assertEquals("New", sut.profile.value?.name) } + @Test + fun `saveProfile without a tag change does not wait for the profile copy`() = test { + authenticateForTesting(publicKey = VALID_SELF_KEY) + val finishCopy = CompletableDeferred() + whenever { pubkyService.publishPaykitProfile(any(), any()) }.doSuspendableAnswer { + finishCopy.await() + paykitRecord(it.getArgument(0)) + } + clearInvocations(pubkyService) + + val save = async { + sut.saveProfile(name = "Renamed", bio = "", links = emptyList(), tags = emptyList(), imageUrl = null) + } + runCurrent() + + assertTrue(save.isCompleted) + save.await().getOrThrow() + verifyBlocking(pubkyService) { publishPaykitProfile(any(), any()) } + finishCopy.complete(Unit) + } + @Test fun `saveProfile signs in again once when the session lacks the pubky app scope`() = test { val secretKey = "local_secret" From cd6aa23fe2feee1ac856223521946b735262a74c Mon Sep 17 00:00:00 2001 From: Ovi Trif Date: Wed, 11 Nov 2026 15:27:02 +0100 Subject: [PATCH 2/4] fix: order background copy writes and keep a load's unread tags A save waits for a copy write an earlier save left running before writing its own, and the background write no longer clears the unread flag a load may have set. --- .../java/to/bitkit/repositories/PubkyRepo.kt | 23 ++++++++++++------- .../to/bitkit/repositories/PubkyRepoTest.kt | 20 ++++++++++++++++ 2 files changed, 35 insertions(+), 8 deletions(-) diff --git a/app/src/main/java/to/bitkit/repositories/PubkyRepo.kt b/app/src/main/java/to/bitkit/repositories/PubkyRepo.kt index a784b3ad81..3ec3daf578 100644 --- a/app/src/main/java/to/bitkit/repositories/PubkyRepo.kt +++ b/app/src/main/java/to/bitkit/repositories/PubkyRepo.kt @@ -237,6 +237,10 @@ class PubkyRepo @Inject constructor( private val profileSaveEpoch = AtomicLong(0L) private val profileSavesRunning = AtomicInteger(0) + // Orders each save's Paykit copy write after the one an earlier save left running. + private val copyWriteOrder = Mutex() + private var pendingCopyWrite: Job? = null + /** Revision of the loaded pubky.app profile, which a save must still match; null when none exists. */ @Volatile private var ownProfileRevision: String? = null @@ -1337,17 +1341,20 @@ class PubkyRepo @Inject constructor( val copyData = profile.toProfileData().toPaykitProfile() val writeCopy = suspend { runSuspendCatching { profileWrite(generation) { pubkyService.publishPaykitProfile(copyData, publicKey) } } - .onSuccess { tagsUnavailable = false } .onFailure { Logger.warn("Failed to update Bitkit profile copy", it, context = TAG) } } val copyError = keptTags?.let { - // With no tag change to report, the save does not wait for the copy. The write lock keeps it ahead of the - // next save's copy write, and a deletion refuses or waits for it. - if (it == baseTags) { - scope.launch { writeCopy() } - null - } else { - writeCopy().exceptionOrNull() + copyWriteOrder.withLock { + // A copy write left running by an earlier save lands first, so it never overwrites this one. + pendingCopyWrite?.join() + if (it == baseTags) { + // With no tag change to report, the save does not wait for the copy; a deletion refuses or waits + // for it. It leaves the unread flag alone, which a load may have set since. + pendingCopyWrite = scope.launch { writeCopy() } + null + } else { + writeCopy().onSuccess { tagsUnavailable = false }.exceptionOrNull() + } } } val saved = if (keptTags != null && copyError == null) profile else profile.copy(tags = baseTags) diff --git a/app/src/test/java/to/bitkit/repositories/PubkyRepoTest.kt b/app/src/test/java/to/bitkit/repositories/PubkyRepoTest.kt index 8c695d431e..3c10454ea1 100644 --- a/app/src/test/java/to/bitkit/repositories/PubkyRepoTest.kt +++ b/app/src/test/java/to/bitkit/repositories/PubkyRepoTest.kt @@ -995,6 +995,26 @@ class PubkyRepoTest : BaseUnitTest() { finishCopy.complete(Unit) } + @Test + fun `a background copy write keeps a later load's unread tags`() = test { + authenticateForTesting(publicKey = VALID_SELF_KEY) + val finishCopy = CompletableDeferred() + whenever { pubkyService.publishPaykitProfile(any(), any()) }.doSuspendableAnswer { + finishCopy.await() + paykitRecord(it.getArgument(0)) + } + sut.saveProfile(name = "Renamed", bio = "", links = emptyList(), tags = emptyList(), imageUrl = null) + .getOrThrow() + runCurrent() + whenever(pubkyService.fetchPaykitProfile(VALID_SELF_KEY)).thenAnswer { throw TestAppError("offline") } + sut.loadProfile() + + finishCopy.complete(Unit) + runCurrent() + + assertTrue(sut.profileEditBase().tagsUnread) + } + @Test fun `saveProfile signs in again once when the session lacks the pubky app scope`() = test { val secretKey = "local_secret" From 9f3ecc7b6c72f565a9827c805e780a0fff083752 Mon Sep 17 00:00:00 2001 From: Ovi Trif Date: Wed, 11 Nov 2026 15:50:55 +0100 Subject: [PATCH 3/4] fix: clear the unread tags flag when a save reads the copy itself --- .../java/to/bitkit/repositories/PubkyRepo.kt | 12 ++++++-- .../to/bitkit/repositories/PubkyRepoTest.kt | 30 +++++++++++++++++++ 2 files changed, 40 insertions(+), 2 deletions(-) diff --git a/app/src/main/java/to/bitkit/repositories/PubkyRepo.kt b/app/src/main/java/to/bitkit/repositories/PubkyRepo.kt index 3ec3daf578..ffe79a62b1 100644 --- a/app/src/main/java/to/bitkit/repositories/PubkyRepo.kt +++ b/app/src/main/java/to/bitkit/repositories/PubkyRepo.kt @@ -249,6 +249,9 @@ class PubkyRepo @Inject constructor( @Volatile private var tagsUnavailable = false + // Moved on each time a load sets tagsUnavailable, so a save clears it only if no load set it since the save began. + private val tagsReadEpoch = AtomicLong(0L) + @Volatile private var restoreBase: RestoreBase? = null private val signInGeneration = AtomicLong(0L) @@ -948,6 +951,7 @@ class PubkyRepo @Inject constructor( restoreBase = RestoreBase(prefixedKey, it, revision = null) } } + tagsReadEpoch.incrementAndGet() tagsUnavailable = paykitRead.isFailure PubkyProfile.fromPubkyProfile(prefixedKey, snapshot.profile).copy(tags = paykitData?.tags.orEmpty()).also { restoreBase = RestoreBase(prefixedKey, it, snapshot.revision) @@ -1286,6 +1290,7 @@ class PubkyRepo @Inject constructor( tagsUnread: Boolean, ): SavedProfile { val generation = profileDeletionGeneration.get() + val readEpoch = tagsReadEpoch.get() // Unread when the edit began or now: either way the form's tags are not the stored ones. val copyUnread = tagsUnread || tagsUnavailable // A profile written without a base is fitted to pubky.app's limits, as a move is. @@ -1317,7 +1322,7 @@ class PubkyRepo @Inject constructor( } ownProfileRevision = snapshot.revision val baseTags = base?.tags.orEmpty() - val stored = storedCopy(publicKey, copyUnread) + val stored = storedCopy(publicKey, copyUnread, readEpoch) val wasPending = publicKey in pubkyStore.data.first().pubkyAppAvatarPending // The form could not show an avatar still to move while the copy was unread, so it stays pending. val keepPending = snapshot.profile.image == null && copyUnread && wasPending @@ -1370,9 +1375,11 @@ class PubkyRepo @Inject constructor( * What a save keeps from the Paykit copy after the last load could not read it, as the form then lacked its tags * and the avatar still to move: empty when that load read it, null while it still cannot be read. */ - private suspend fun storedCopy(publicKey: String, copyUnread: Boolean): StoredCopy? { + private suspend fun storedCopy(publicKey: String, copyUnread: Boolean, readEpoch: Long): StoredCopy? { if (!copyUnread) return StoredCopy(tags = null, image = null) val record = runSuspendCatching { pubkyService.fetchPaykitProfile(publicKey) }.getOrElse { return null } + // The save read the copy itself, so the tags are readable again, unless a load found otherwise meanwhile. + if (tagsReadEpoch.get() == readEpoch) tagsUnavailable = false val data = record?.let { PubkyProfileData.fromPaykitProfile(it.profile) } return StoredCopy(tags = data?.tags.orEmpty(), image = data?.image) } @@ -1395,6 +1402,7 @@ class PubkyRepo @Inject constructor( val saveEpoch = profileSaveEpoch.get() val paykitRead = runSuspendCatching { pubkyService.fetchPaykitProfile(publicKey) } .onFailure { Logger.warn("Failed to read Bitkit profile copy", it, context = TAG) } + tagsReadEpoch.incrementAndGet() tagsUnavailable = paykitRead.isFailure val paykitData = paykitRead.getOrNull()?.let { PubkyProfileData.fromPaykitProfile(it.profile) } val fetched = readRetryingOnce(publicKey) { pubkyService.fetchPubkyProfileWithRevision(publicKey) } diff --git a/app/src/test/java/to/bitkit/repositories/PubkyRepoTest.kt b/app/src/test/java/to/bitkit/repositories/PubkyRepoTest.kt index 3c10454ea1..fb824aaa76 100644 --- a/app/src/test/java/to/bitkit/repositories/PubkyRepoTest.kt +++ b/app/src/test/java/to/bitkit/repositories/PubkyRepoTest.kt @@ -1015,6 +1015,36 @@ class PubkyRepoTest : BaseUnitTest() { assertTrue(sut.profileEditBase().tagsUnread) } + @Test + fun `a save that reads the copy itself lets the next edit remove a tag`() = test { + authenticateForTesting(publicKey = VALID_SELF_KEY) + var copy = PubkyProfileData(name = "Test", bio = "", tags = listOf("a", "b")).toPaykitProfile() + whenever(pubkyService.fetchPaykitProfile(VALID_SELF_KEY)).thenAnswer { paykitRecord(copy) } + whenever(pubkyService.publishPaykitProfile(any(), any())).thenAnswer { + copy = it.getArgument(0) + paykitRecord(copy) + } + sut.loadProfile() + val editBase = sut.profileEditBase() + doAnswer { throw TestAppError("offline") }.whenever(pubkyService).fetchPaykitProfile(VALID_SELF_KEY) + sut.loadProfile() + doAnswer { paykitRecord(copy) }.whenever(pubkyService).fetchPaykitProfile(VALID_SELF_KEY) + + sut.saveProfile( + name = "Test", + bio = "", + links = emptyList(), + tags = listOf("a"), + imageUrl = null, + editBase = editBase + ).getOrThrow() + runCurrent() + sut.saveProfile(name = "Test", bio = "", links = emptyList(), tags = listOf("a"), imageUrl = null).getOrThrow() + runCurrent() + + assertEquals(listOf("a"), PubkyProfileData.fromPaykitProfile(copy).tags) + } + @Test fun `saveProfile signs in again once when the session lacks the pubky app scope`() = test { val secretKey = "local_secret" From e571a6a77d3266947a5e279e8d506d011cdcca70 Mon Sep 17 00:00:00 2001 From: Ovi Trif Date: Wed, 11 Nov 2026 16:28:28 +0100 Subject: [PATCH 4/4] fix: keep the unread tags flag when the copy write fails --- .../java/to/bitkit/repositories/PubkyRepo.kt | 18 +++++--- .../to/bitkit/repositories/PubkyRepoTest.kt | 42 +++++++++++++++++++ 2 files changed, 55 insertions(+), 5 deletions(-) diff --git a/app/src/main/java/to/bitkit/repositories/PubkyRepo.kt b/app/src/main/java/to/bitkit/repositories/PubkyRepo.kt index ffe79a62b1..fc4c021fb0 100644 --- a/app/src/main/java/to/bitkit/repositories/PubkyRepo.kt +++ b/app/src/main/java/to/bitkit/repositories/PubkyRepo.kt @@ -1322,7 +1322,7 @@ class PubkyRepo @Inject constructor( } ownProfileRevision = snapshot.revision val baseTags = base?.tags.orEmpty() - val stored = storedCopy(publicKey, copyUnread, readEpoch) + val stored = storedCopy(publicKey, copyUnread) val wasPending = publicKey in pubkyStore.data.first().pubkyAppAvatarPending // The form could not show an avatar still to move while the copy was unread, so it stays pending. val keepPending = snapshot.profile.image == null && copyUnread && wasPending @@ -1354,8 +1354,9 @@ class PubkyRepo @Inject constructor( pendingCopyWrite?.join() if (it == baseTags) { // With no tag change to report, the save does not wait for the copy; a deletion refuses or waits - // for it. It leaves the unread flag alone, which a load may have set since. + // for it. Its outcome leaves the unread flag alone, which a load may have set since. pendingCopyWrite = scope.launch { writeCopy() } + markTagsRead(stored, readEpoch) null } else { writeCopy().onSuccess { tagsUnavailable = false }.exceptionOrNull() @@ -1375,15 +1376,22 @@ class PubkyRepo @Inject constructor( * What a save keeps from the Paykit copy after the last load could not read it, as the form then lacked its tags * and the avatar still to move: empty when that load read it, null while it still cannot be read. */ - private suspend fun storedCopy(publicKey: String, copyUnread: Boolean, readEpoch: Long): StoredCopy? { + private suspend fun storedCopy(publicKey: String, copyUnread: Boolean): StoredCopy? { if (!copyUnread) return StoredCopy(tags = null, image = null) val record = runSuspendCatching { pubkyService.fetchPaykitProfile(publicKey) }.getOrElse { return null } - // The save read the copy itself, so the tags are readable again, unless a load found otherwise meanwhile. - if (tagsReadEpoch.get() == readEpoch) tagsUnavailable = false val data = record?.let { PubkyProfileData.fromPaykitProfile(it.profile) } return StoredCopy(tags = data?.tags.orEmpty(), image = data?.image) } + /** + * Clears the unread flag after a save read the copy itself and kept its tags in the profile it stores, unless a + * load found the copy unreadable since [readEpoch]. A save that writes the copy at once clears it only once that + * write succeeds, since a failed one stores the profile without the copy's tags. + */ + private fun markTagsRead(stored: StoredCopy?, readEpoch: Long) { + if (stored?.tags != null && tagsReadEpoch.get() == readEpoch) tagsUnavailable = false + } + /** [name], [bio] and [links] fitted to pubky.app's limits: one line, cut to length, at most five valid links. */ private fun fitted(name: String, bio: String, links: List) = FittedProfile( name = name.singleLine().takeCodePoints(PUBKY_APP_NAME_MAX_LENGTH), diff --git a/app/src/test/java/to/bitkit/repositories/PubkyRepoTest.kt b/app/src/test/java/to/bitkit/repositories/PubkyRepoTest.kt index fb824aaa76..00940b9097 100644 --- a/app/src/test/java/to/bitkit/repositories/PubkyRepoTest.kt +++ b/app/src/test/java/to/bitkit/repositories/PubkyRepoTest.kt @@ -1299,6 +1299,48 @@ class PubkyRepoTest : BaseUnitTest() { assertEquals(listOf("friend", "work", "vip"), PubkyProfileData.fromPaykitProfile(copy.lastValue).tags) } + @Test + fun `a tag save after a failed read and write keeps the stored tags without the error's base`() = test { + authenticateForTesting(publicKey = VALID_SELF_KEY) + whenever(pubkyService.fetchPaykitProfile(VALID_SELF_KEY)).thenAnswer { throw TestAppError("offline") } + sut.loadProfile() + doReturn( + paykitRecord(PubkyProfileData(name = "Test", bio = "", tags = listOf("friend", "work")).toPaykitProfile()) + ) + .whenever(pubkyService).fetchPaykitProfile(VALID_SELF_KEY) + whenever(pubkyService.publishPaykitProfile(any(), any())) + .thenAnswer { throw TestAppError("offline") } + .thenAnswer { paykitRecord(it.getArgument(0)) } + + sut.saveProfile(name = "Test", bio = "", links = emptyList(), tags = listOf("vip"), imageUrl = null) + sut.saveProfile(name = "Test", bio = "", links = emptyList(), tags = listOf("vip"), imageUrl = null) + + val copy = argumentCaptor() + verifyBlocking(pubkyService, times(2)) { publishPaykitProfile(copy.capture(), any()) } + assertEquals(listOf("friend", "work", "vip"), PubkyProfileData.fromPaykitProfile(copy.lastValue).tags) + } + + @Test + fun `a name save after a failed read and write keeps the stored tags`() = test { + authenticateForTesting(publicKey = VALID_SELF_KEY) + whenever(pubkyService.fetchPaykitProfile(VALID_SELF_KEY)).thenAnswer { throw TestAppError("offline") } + sut.loadProfile() + doReturn( + paykitRecord(PubkyProfileData(name = "Test", bio = "", tags = listOf("friend", "work")).toPaykitProfile()) + ) + .whenever(pubkyService).fetchPaykitProfile(VALID_SELF_KEY) + whenever(pubkyService.publishPaykitProfile(any(), any())) + .thenAnswer { throw TestAppError("offline") } + .thenAnswer { paykitRecord(it.getArgument(0)) } + + sut.saveProfile(name = "Renamed", bio = "", links = emptyList(), tags = emptyList(), imageUrl = null) + sut.saveProfile(name = "Renamed Again", bio = "", links = emptyList(), tags = emptyList(), imageUrl = null) + + val copy = argumentCaptor() + verifyBlocking(pubkyService, times(2)) { publishPaykitProfile(copy.capture(), any()) } + assertEquals(listOf("friend", "work"), PubkyProfileData.fromPaykitProfile(copy.lastValue).tags) + } + @Test fun `saveProfile keeps the stored tags when the edit began while they could not be read`() = test { authenticateForTesting(publicKey = VALID_SELF_KEY)