Merge pull request 'fix(android): surface the outbox send state instead of denying it' (#52) from wt/t_7233051b into wt/t_9a29fd3c
All checks were successful
ci / test (pull_request) Successful in 3m54s
ci / sdk (pull_request) Successful in 58s

Reviewed-on: #52
This commit is contained in:
sirius 2026-09-12 23:05:40 +00:00
commit 6867a6c430
4 changed files with 271 additions and 19 deletions

View file

@ -21,6 +21,7 @@ import uniffi.onionwire_sdk.ChatMessage
import uniffi.onionwire_sdk.FriendInfo import uniffi.onionwire_sdk.FriendInfo
import uniffi.onionwire_sdk.InviteInfo import uniffi.onionwire_sdk.InviteInfo
import uniffi.onionwire_sdk.RotateOutcome import uniffi.onionwire_sdk.RotateOutcome
import uniffi.onionwire_sdk.SendState
import uniffi.onionwire_sdk.Wire import uniffi.onionwire_sdk.Wire
import uniffi.onionwire_sdk.decodeInvite import uniffi.onionwire_sdk.decodeInvite
import java.io.File import java.io.File
@ -361,7 +362,15 @@ class WireViewModel(app: Application) : AndroidViewModel(app) {
suspend fun messages(pubkeyHex: String): List<ChatMessage> = suspend fun messages(pubkeyHex: String): List<ChatMessage> =
requireWire().messages(pubkeyHex) requireWire().messages(pubkeyHex)
suspend fun send(pubkeyHex: String, body: String) = /**
* Hands the body to the node and reports what happened to it.
*
* `SENT` means the peer's onion took the frame during this call. `QUEUED`
* means it did not: the message is in the local outbox, local-only until
* the retry worker gets a circuit through. The caller must not render
* `QUEUED` as delivered.
*/
suspend fun send(pubkeyHex: String, body: String): SendState =
requireWire().send(pubkeyHex, body) requireWire().send(pubkeyHex, body)
suspend fun wipeMessages() { suspend fun wipeMessages() {

View file

@ -37,6 +37,12 @@ import androidx.compose.ui.text.font.FontFamily
import androidx.compose.ui.unit.dp import androidx.compose.ui.unit.dp
import kotlinx.coroutines.delay import kotlinx.coroutines.delay
import uniffi.onionwire_sdk.ChatMessage import uniffi.onionwire_sdk.ChatMessage
import uniffi.onionwire_sdk.SendState
/** Shown after a `Queued` send — the TUI's composer keeps `{n} queued`. */
private const val QUEUED_HINT =
"Queued: their onion is unreachable. The message is local-only in this " +
"device's outbox until the retry worker gets a circuit through."
@OptIn(ExperimentalMaterial3Api::class) @OptIn(ExperimentalMaterial3Api::class)
@Composable @Composable
@ -45,13 +51,14 @@ fun ChatScreen(
fingerprint: String, fingerprint: String,
onion: String, onion: String,
loadMessages: suspend () -> List<ChatMessage>, loadMessages: suspend () -> List<ChatMessage>,
onSend: suspend (String) -> Unit, onSend: suspend (String) -> SendState,
onBack: () -> Unit, onBack: () -> Unit,
) { ) {
var messages by remember { mutableStateOf<List<ChatMessage>?>(null) } var messages by remember { mutableStateOf<List<ChatMessage>?>(null) }
var draft by remember { mutableStateOf("") } var draft by remember { mutableStateOf("") }
var pending by remember { mutableStateOf<String?>(null) } var pending by remember { mutableStateOf<String?>(null) }
var error by remember { mutableStateOf<String?>(null) } var error by remember { mutableStateOf<String?>(null) }
var hint by remember { mutableStateOf<String?>(null) }
val listState = rememberLazyListState() val listState = rememberLazyListState()
// Poll: the node appends inbound messages in the background; there is no // Poll: the node appends inbound messages in the background; there is no
@ -65,16 +72,32 @@ fun ChatScreen(
} }
} }
// Fail-closed send. `send` retries the peer's onion for up to 3 minutes and // Fail closed — but not fail lost. `send` returns `Queued` when the peer's
// then errors — nothing is spooled, so a failure is a real failure. // onion is unreachable: the body is in this device's local outbox and the
// retry worker keeps trying. It only surfaces as a hard error when the
// node itself refuses the write.
LaunchedEffect(pending) { LaunchedEffect(pending) {
val body = pending ?: return@LaunchedEffect val body = pending ?: return@LaunchedEffect
error = null error = null
hint = null
runCatching { onSend(body) } runCatching { onSend(body) }
.onSuccess { state ->
// Never dress a queued send up as delivered.
hint = if (state == SendState.QUEUED) QUEUED_HINT else null
}
.onFailure { error = it.message ?: it.toString() } .onFailure { error = it.message ?: it.toString() }
pending = null pending = null
} }
// The hint is a toast, not a log line: the row marker and the `N queued`
// footer are what stay on screen.
LaunchedEffect(hint) {
if (hint != null) {
delay(6_000)
hint = null
}
}
LaunchedEffect(messages?.size) { LaunchedEffect(messages?.size) {
val n = messages?.size ?: 0 val n = messages?.size ?: 0
if (n > 0) listState.animateScrollToItem(n - 1) if (n > 0) listState.animateScrollToItem(n - 1)
@ -127,8 +150,13 @@ fun ChatScreen(
Text("No messages", style = MaterialTheme.typography.titleSmall) Text("No messages", style = MaterialTheme.typography.titleSmall)
Spacer(Modifier.height(6.dp)) Spacer(Modifier.height(6.dp))
Text( Text(
"Sends fail closed: if their onion is down the message is " + "Sends fail closed — nothing goes to a third party. If their " +
"not spooled anywhere. There is no outbox in v1.", "onion is unreachable the message is queued in this " +
"device's outbox instead of being dropped: it stays " +
"local-only until the retry worker gets a circuit " +
"through (8 attempts, backoff capped at 30 min), then it " +
"lands. \u2713 means delivered, \u25f7 queued, " +
"\u2717 failed.",
style = MaterialTheme.typography.bodySmall, style = MaterialTheme.typography.bodySmall,
color = MaterialTheme.colorScheme.onSurfaceVariant, color = MaterialTheme.colorScheme.onSurfaceVariant,
) )
@ -149,25 +177,77 @@ fun ChatScreen(
horizontalArrangement = horizontalArrangement =
if (mine) Arrangement.End else Arrangement.Start, if (mine) Arrangement.End else Arrangement.Start,
) { ) {
Text( Column(
m.body, horizontalAlignment =
style = MaterialTheme.typography.bodyMedium, if (mine) Alignment.End else Alignment.Start,
color = if (mine) MaterialTheme.colorScheme.onPrimary ) {
else MaterialTheme.colorScheme.onSurface, Text(
modifier = Modifier m.body,
.background( style = MaterialTheme.typography.bodyMedium,
if (mine) MaterialTheme.colorScheme.primary color = if (mine) MaterialTheme.colorScheme.onPrimary
else MaterialTheme.colorScheme.surfaceVariant, else MaterialTheme.colorScheme.onSurface,
RoundedCornerShape(10.dp), modifier = Modifier
) .background(
.padding(horizontal = 10.dp, vertical = 8.dp), if (mine) MaterialTheme.colorScheme.primary
) else MaterialTheme.colorScheme.surfaceVariant,
RoundedCornerShape(10.dp),
)
.padding(horizontal = 10.dp, vertical = 8.dp),
)
// Outgoing rows only: the SDK reports `null` for
// inbound, and an outgoing row with no outbox
// entry reads "sent".
if (mine) {
deliveryMarker(m.delivery)?.let { marker ->
Text(
marker.text,
style = MaterialTheme.typography.labelSmall,
fontFamily = FontFamily.Monospace,
color = when (marker.tone) {
DeliveryTone.SENT ->
MaterialTheme.colorScheme.onSurfaceVariant
DeliveryTone.PENDING ->
MaterialTheme.colorScheme.tertiary
DeliveryTone.FAILED ->
MaterialTheme.colorScheme.error
},
modifier = Modifier.padding(
top = 2.dp,
end = 2.dp,
),
)
}
}
}
} }
} }
} }
} }
} }
// TUI footer: `{n} queued` while the outbox still owns rows.
val queued = list?.count { it.direction == "out" && isOutboxPending(it.delivery) } ?: 0
if (queued > 0) {
Text(
"$queued queued",
style = MaterialTheme.typography.labelSmall,
fontFamily = FontFamily.Monospace,
color = MaterialTheme.colorScheme.tertiary,
modifier = Modifier.padding(horizontal = 12.dp, vertical = 4.dp),
)
}
hint?.let {
Text(
it,
style = MaterialTheme.typography.bodySmall,
color = MaterialTheme.colorScheme.tertiary,
modifier = Modifier.padding(horizontal = 12.dp, vertical = 4.dp),
)
}
error?.let { error?.let {
Text( Text(
it, it,

View file

@ -0,0 +1,78 @@
package com.siriusdevops.onionwire.ui
/**
* Per-row delivery markers for outgoing chat messages.
*
* The vocabulary is the TUI's (`docs/TUI.md` → "Delivery state (outbox)"), so
* both clients read the same:
*
* ```
* ✓ delivered — the peer's onion took the frame
* ◷ queued in this device's local outbox, waiting for a circuit
* » sending the retry worker has it in flight
* ✗ failed: … terminally failed (8 attempts, backoff capped at 30 min)
* ```
*
* The string comes from `ChatMessage.delivery` and is produced by the store's
* own outbox status — `"sent"` / `"queued"` / `"sending"` /
* `"failed: <reason>"` for outgoing rows, `null` for incoming ones. An outgoing
* row with no outbox row was accepted on the first attempt and reads `"sent"`.
*
* Nothing here guesses: a delivery string this build does not recognise renders
* no marker rather than a tick, because a missing marker never claims delivery
* while a wrong tick does.
*/
/** Which colour the marker is drawn in. */
internal enum class DeliveryTone {
/** Delivered to the peer's onion. */
SENT,
/** Still local-only: queued, or being retried right now. */
PENDING,
/** Out of attempts — the message never left this device. */
FAILED,
}
internal data class DeliveryMarker(val text: String, val tone: DeliveryTone)
/**
* Longest `failed: <reason>` tail we render before eliding. The TUI caps the
* same reason at 24 cells for its 80-column row; a phone bubble wraps, so this
* only exists to stop a pathological Tor error from filling the screen.
*/
private const val MAX_REASON_CHARS = 48
/** Marker for one outgoing row, or `null` when there is nothing to say. */
internal fun deliveryMarker(delivery: String?): DeliveryMarker? {
val state = delivery?.trim().orEmpty()
if (state.isEmpty()) return null
return when {
state == "sent" -> DeliveryMarker("\u2713", DeliveryTone.SENT)
state == "queued" -> DeliveryMarker("\u25f7 queued", DeliveryTone.PENDING)
state == "sending" -> DeliveryMarker("\u00bb sending", DeliveryTone.PENDING)
state.startsWith("failed") -> {
val reason = shortReason(state.substringAfter(':', "").trim())
DeliveryMarker(
if (reason.isEmpty()) "\u2717 failed" else "\u2717 failed: $reason",
DeliveryTone.FAILED,
)
}
// Unknown state: never render a tick for something we cannot read.
else -> null
}
}
/** True while the outbox still owns the row — the TUI's `{n} queued` footer. */
internal fun isOutboxPending(delivery: String?): Boolean {
val state = delivery?.trim().orEmpty()
return state == "queued" || state == "sending"
}
private fun shortReason(reason: String): String =
if (reason.length <= MAX_REASON_CHARS) {
reason
} else {
reason.take(MAX_REASON_CHARS - 1).trimEnd() + "\u2026"
}

View file

@ -0,0 +1,85 @@
package com.siriusdevops.onionwire.ui
import org.junit.Assert.assertEquals
import org.junit.Assert.assertFalse
import org.junit.Assert.assertNull
import org.junit.Assert.assertTrue
import org.junit.Test
/**
* The delivery vocabulary is shared with the TUI (`docs/TUI.md` → "Delivery
* state (outbox)"). These strings are the SDK's contract
* (`crates/onionwire-sdk/src/lib.rs`), so a drift here is a client that lies
* about a message having landed.
*/
class DeliveryMarkerTest {
@Test
fun delivered_row_gets_a_tick() {
val m = deliveryMarker("sent")
assertEquals(DeliveryMarker("\u2713", DeliveryTone.SENT), m)
}
@Test
fun queued_row_says_queued_not_delivered() {
val m = deliveryMarker("queued")
assertEquals(DeliveryMarker("\u25f7 queued", DeliveryTone.PENDING), m)
}
@Test
fun sending_row_is_pending() {
val m = deliveryMarker("sending")
assertEquals(DeliveryMarker("\u00bb sending", DeliveryTone.PENDING), m)
}
@Test
fun failed_row_carries_the_reason() {
val m = deliveryMarker("failed: connection timed out")
assertEquals(DeliveryMarker("\u2717 failed: connection timed out", DeliveryTone.FAILED), m)
}
@Test
fun failed_row_without_a_reason_still_reads_failed() {
assertEquals(
DeliveryMarker("\u2717 failed", DeliveryTone.FAILED),
deliveryMarker("failed"),
)
}
@Test
fun a_long_reason_is_elided() {
val m = deliveryMarker("failed: " + "x".repeat(200))
val text = m?.text.orEmpty()
assertTrue(text.startsWith("\u2717 failed: xxx"))
assertTrue(text.endsWith("\u2026"))
assertTrue("marker grew to ${text.length} chars", text.length <= 48 + "\u2717 failed: ".length)
}
@Test
fun incoming_row_has_no_marker() {
assertNull(deliveryMarker(null))
assertNull(deliveryMarker(""))
assertNull(deliveryMarker(" "))
}
@Test
fun unknown_state_never_claims_delivery() {
// Fail-safe: an unreadable state renders nothing rather than a tick.
assertNull(deliveryMarker("scheduled"))
assertNull(deliveryMarker("SENT"))
}
@Test
fun surrounding_whitespace_is_tolerated() {
assertEquals(DeliveryTone.SENT, deliveryMarker(" sent ")?.tone)
}
@Test
fun pending_counts_only_queued_and_sending() {
assertTrue(isOutboxPending("queued"))
assertTrue(isOutboxPending("sending"))
assertFalse(isOutboxPending("sent"))
assertFalse(isOutboxPending("failed: no route"))
assertFalse(isOutboxPending(null))
}
}