Hold an undelivered message and send it when they come back

Asked: how does the sender know to try again when the other person has no
registered device - especially since they are friends. They should not have to.
Marking it undelivered and stopping there moves the problem onto the person who
did nothing wrong: they would have to remember, notice their friend was back,
and retype it.

So the message goes into an outbox and is retried on every poll tick. Nothing
expires and nothing is discarded - a contact who reinstalls in a month gets it
in a month. Timing out would mean deleting something somebody wrote in order to
tidy up a label.

The queue has to live on the sender's phone, and that is forced by the
encryption rather than chosen: every envelope is sealed to a particular
device's keys, so somebody with no device has no keys to seal to, and the relay
could only queue plaintext. The consequence is that it goes out the next time
THIS app runs and polls, not the instant the recipient signs in - so the label
says "Undelivered - will send" rather than implying it is in flight.

Delivery is now a tri-state. PENDING is quiet and grey because it is the normal
thing that happens when a friend is signed out and there is nothing to do about
it; only FAILED is a warning, and the outbox never produces one.

OutboxTest covers it end to end, including delivery against a real second
identity rather than a hand-written bundle: queued while away, sent when they
register, and the bubble restated - a version that delivered the message but
left it reading "undelivered" would pass a test that only checked the queue.

Engine, client and app suites green.

Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
This commit is contained in:
Paul Hitt
2026-08-27 18:06:19 -04:00
co-authored by Claude Opus 5
parent f711dd279b
commit 94337cbc07
8 changed files with 395 additions and 29 deletions
@@ -1,5 +1,6 @@
package io.flamenet.e2e
import org.json.JSONArray
import org.json.JSONObject
import java.security.SecureRandom
import javax.crypto.Cipher
@@ -58,6 +59,7 @@ class Vault(private val storage: Storage, private val key: SecretKey) {
private const val STORIES = "stories"
private const val AVATARS = "avatars"
private const val MY_AVATAR = "my-avatar"
private const val OUTBOX = "outbox"
/** The two kinds of thread, named once so they cannot drift apart. */
fun threadForUser(userId: Long) = "user:$userId"
@@ -512,6 +514,28 @@ class Vault(private val storage: Storage, private val key: SecretKey) {
writeJson(historyName(threadId), JSONObject().put("messages", existing))
}
/**
* Messages typed but not yet delivered to anybody.
*
* 🔴 **This has to live here, on the sender's phone, and that is forced by
* the encryption rather than chosen.** A relay cannot hold a message for
* somebody with no registered device: every envelope is sealed to a
* specific device's keys, so with no device there is nothing to seal it to.
* The relay could only queue plaintext, which is the one thing this app
* exists not to do. So the sender keeps it and sends it when they can.
*
* ⚠️ In the vault, so it is encrypted at rest and survives being killed —
* an outbox that a task-switcher can empty is not an outbox.
*/
fun outbox(): JSONArray = readJson(OUTBOX)?.optJSONArray("pending") ?: JSONArray()
fun saveOutbox(pending: JSONArray) =
writeJson(OUTBOX, JSONObject().put("pending", pending))
/** Rewrite a thread wholesale, for restating a message's delivery state. */
fun replaceHistory(threadId: String, messages: JSONArray) =
writeJson(historyName(threadId), JSONObject().put("messages", messages))
/** Every thread this device holds, group or person. */
fun threads(): List<String> =
storage.names().filter { it.startsWith(HISTORY_PREFIX) }