Repository navigation
fix(membership): retry unconfirmed Favorites and Watchlist changes automatically #565
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from all commits
7e4dfda
cde1ec6
a91f256
acba18d
d5964cd
a4bca8d
894d227
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -11,7 +11,7 @@ import org.siloserver.silo.network.TokenManager | |
| import org.siloserver.silo.network.apiv2.MembershipV2Api | ||
|
|
||
| /** | ||
| * Storage foundation only. Both inline and worker sends must use [send] after activation. | ||
| * Storage foundation only. Both inline and worker sends must use [send] or [retry] after activation. | ||
| * [authority] is a persisted login identity, NOT a server/profile pair or process counter. | ||
| * The caller must verify its association with [scope] before constructing an Authority. | ||
| * No credentials are persisted in Room. A process owns one instance and calls [recover] | ||
|
|
@@ -52,12 +52,16 @@ class MembershipOutbox( | |
| )) | ||
| } | ||
|
|
||
| suspend fun send(id: Long, authority: Authority): Result { | ||
| suspend fun send(id: Long, authority: Authority): Result = | ||
| sendClaimed(id, authority) { claim -> dao.claimMembership(id, authority.key, claim, processOwner) } | ||
|
|
||
| private suspend fun sendClaimed(id: Long, authority: Authority, claimRow: suspend (String) -> Int): Result { | ||
| if (authority.scope != tokens.snapshotCurrentScope()) return Result(Resolution.NOT_CLAIMED) | ||
| val claim = UUID.randomUUID().toString() | ||
| var resolved = false | ||
| var refused = false | ||
| try { | ||
| if (dao.claimMembership(id, authority.key, claim, processOwner) != 1) return Result(Resolution.NOT_CLAIMED) | ||
| if (claimRow(claim) != 1) return Result(Resolution.NOT_CLAIMED) | ||
| val command = requireNotNull(dao.getById(id)) | ||
| require(command.serverId == authority.scope.serverId && command.profileId == authority.scope.profileId.orEmpty()) | ||
| val present = command.payloadJson.toBooleanStrict() | ||
|
|
@@ -73,7 +77,7 @@ class MembershipOutbox( | |
| if (resolved) return Result(Resolution.ACKNOWLEDGED, | ||
| dao.getLatestByCoalesceKey(command.coalesceKey) == null && authority.scope == tokens.snapshotCurrentScope()) | ||
| } | ||
| return Result(Resolution.NEEDS_RECONCILIATION) | ||
| refused = response is ApiResult.Error && refuses(response.code) | ||
| } finally { | ||
| // Claim commit can precede cancellation of its suspended return. Always attempt | ||
| // exact-token cleanup, even when admission never returned to this coroutine. | ||
|
|
@@ -82,9 +86,44 @@ class MembershipOutbox( | |
| dao.transitionMembership(id, claim, authority.key, SENDING, RECONCILE) | ||
| } | ||
| } | ||
| // Resending can't fix a refused write (an item that is gone or hidden). Read once instead: | ||
| // a removal of an item that is gone already matches; otherwise the command pauses. | ||
| return if (refused) reconcile(id, authority) else Result(Resolution.NEEDS_RECONCILIATION) | ||
|
Comment on lines
+89
to
+91
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Failed reconciliation causes definitively refused writes to be resent If the follow-up read fails after a definitive refusal, Prompt for LLMTalk to Kody by mentioning @kody Was this suggestion helpful? React with 👍 or 👎 to help Kody learn from this interaction. |
||
| } | ||
|
|
||
| /** | ||
| * Recovers an unconfirmed command. A read that already shows the desired state resolves it; | ||
| * otherwise the command is resent, but only while it is still the item's latest choice. | ||
| * PUT and DELETE are idempotent, so resending a write that did land is harmless. | ||
| */ | ||
| suspend fun retry(id: Long, authority: Authority): Result { | ||
|
Quick104 marked this conversation as resolved.
Resolved
|
||
| // Cancelled between record and send: nothing was sent yet, so send it through the usual claim. | ||
| if (dao.getById(id)?.state == READY) return send(id, authority) | ||
| val observed = reconcile(id, authority) | ||
| if (observed.resolution != Resolution.PAUSED) return observed | ||
| val command = dao.getById(id) ?: return Result(Resolution.NOT_CLAIMED) | ||
| val previous = command.membershipClaim ?: return Result(Resolution.NOT_CLAIMED) | ||
| if (dao.getLatestByCoalesceKey(command.coalesceKey)?.id != id) { | ||
| // A newer choice for the same item replaces this one. | ||
| dao.resolveMembership(id, previous, authority.key, PAUSED) | ||
| return Result(Resolution.NOT_CLAIMED) | ||
| } | ||
| return sendClaimed(id, authority) { claim -> dao.reclaimMembership(id, authority.key, previous, claim, processOwner) } | ||
|
Comment on lines
+106
to
+111
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Stale-command replay in MembershipOutbox.kt occurs because latest-choice admission checks only remaining dirty rows: an older retry whose GET finishes after a newer command is acknowledged and deleted can reclaim itself and resend the opposite value, silently reversing the confirmed Favorites or Watchlist change. Validate the durable projection's commandId in the reclaim transaction, or atomically retire superseded uncertain rows when recording the newer choice. Prompt for LLMTalk to Kody by mentioning @kody Was this suggestion helpful? React with 👍 or 👎 to help Kody learn from this interaction.
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Confirmed. I reproduced it with a unit test on this head. Command A (favorite) is unconfirmed, and its retry is waiting on the GET. Command B (unfavorite) is enqueued, sent, and acknowledged, and its row is deleted. A's GET then reports a mismatch, A becomes the latest remaining row, Repair: when a newer command is enqueued, I'm fixing this in a commit on this branch, so there's no need to change it yourself. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Thanks for reproducing it. Retiring the older That removes the stale command before the newer acknowledgement can erase the evidence of supersession. Keeping an in-flight send intact preserves the existing per-item serialization. Keep your reproducer as a regression test: acknowledge and delete B before releasing A’s GET, then assert that A sends no late PUT and the confirmed state remains unfavorited. I’ll leave the branch changes to you.
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Fixed in cde1ec6. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Thanks—based on the fix and regression results you reported, this addresses the original stale-command replay finding. Retiring older I have no further changes to request for this finding.
Quick104 marked this conversation as resolved.
Resolved
|
||
| } | ||
|
|
||
| /** Explicit read-only recovery; a mismatch pauses for a new user command, never resends. */ | ||
| /** | ||
| * The one automatic retry a command restored from an earlier run gets. Only a recent unconfirmed | ||
| * command is resent; a paused one (the viewer was told it wasn't saved, or the server refused it) | ||
| * or an older one is only read, so a stale choice never overwrites a newer change made elsewhere. | ||
| * [nowMs] is wall-clock time, compared with the command's [DirtyOperationEntity.createdAtMs]. | ||
| */ | ||
| suspend fun restore(id: Long, authority: Authority, nowMs: Long = System.currentTimeMillis()): Result { | ||
| val command = dao.getById(id) ?: return Result(Resolution.NOT_CLAIMED) | ||
| val recent = nowMs - command.createdAtMs in 0..RESTORE_RESEND_WINDOW_MS | ||
| return if (command.state == RECONCILE && recent) retry(id, authority) else reconcile(id, authority) | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Restored retries can overwrite a newer confirmed membership choice An automatic retry can replay an older choice after a newer one has been acknowledged and removed, because Kody rule violation: Order queued mutations per operation and acknowledge the exact instance Prompt for LLMTalk to Kody by mentioning @kody Was this suggestion helpful? React with 👍 or 👎 to help Kody learn from this interaction. |
||
| } | ||
|
|
||
| /** Read-only check; a mismatch pauses the command until [retry] or a new user command. */ | ||
| suspend fun reconcile(id: Long, authority: Authority): Result { | ||
| val command = dao.getById(id) ?: return Result(Resolution.NOT_CLAIMED) | ||
| if (command.membershipAuthority != authority.key || command.serverId != authority.scope.serverId || | ||
|
|
@@ -112,5 +151,14 @@ class MembershipOutbox( | |
| const val SENDING = "membership_sending" | ||
| const val RECONCILE = "membership_reconcile" | ||
| const val PAUSED = "membership_paused" | ||
|
|
||
| /** How long after a choice a restored command may still be resent without the viewer asking. */ | ||
| const val RESTORE_RESEND_WINDOW_MS = 10 * 60_000L | ||
|
|
||
| /** | ||
| * Responses a resend can't change. 401 is refreshed by the API; 403 can clear once the | ||
| * profile is verified again; 408 and 429 are transient. | ||
| */ | ||
| private fun refuses(code: Int) = code in setOf(400, 404, 410, 422) | ||
| } | ||
| } | ||
Uh oh!
There was an error while loading. Please reload this page.