fix(marmot): surface real decrypt exception instead of stale epoch echo

`MlsGroupManager.decrypt` used to swallow the current-epoch exception
via `decryptOrNull`, try retained epochs, then retry `group.decrypt`.
After our inline-processCommit change this means a mid-commit throw
leaves the group half-advanced, and the retry then reports a
misleading "Message epoch N doesn't match current epoch N+1" —
masking the real cause (unable-to-decrypt path, path-mismatch, etc.).

Swap the order: call `group.decrypt` directly first, capture any
exception, fall through to retained epochs, and re-raise the original
exception if every epoch fails. Retains same semantics for messages
that DO decrypt on the first try.

https://claude.ai/code/session_1469d7f4-bb66-4ffa-a44d-1dfa4b526484
This commit is contained in:
Claude
2026-04-22 07:12:22 +00:00
parent 4e00df7344
commit 0e23f2cde5
@@ -333,19 +333,27 @@ class MlsGroupManager(
mutex.withLock {
val group = requireGroup(nostrGroupId)
// Try current epoch
val current = group.decryptOrNull(messageBytes)
if (current != null) return@withLock current
// Try current epoch. If we hit an exception here we MUST surface
// it — commits that throw mid-processCommit leave the in-memory
// group half-mutated, and a retry via `group.decrypt(...)` will
// just report a stale "epoch mismatch" from the partial advance,
// hiding the real bug. Capture the original throwable, try
// retained epochs as a fallback, and re-raise the captured one
// if nothing decrypts.
val currentFailure: Throwable? =
try {
return@withLock group.decrypt(messageBytes)
} catch (t: Throwable) {
t
}
// Try retained epochs
val retained = retainedEpochs[nostrGroupId] ?: emptyList()
for (epochSecrets in retained) {
val result = tryDecryptWithRetainedEpoch(messageBytes, epochSecrets)
if (result != null) return@withLock result
}
// No epoch could decrypt — rethrow from current epoch for diagnostics
group.decrypt(messageBytes)
throw currentFailure ?: IllegalStateException("Decrypt failed without captured cause")
}
/**