feat(outbox): local retry queue for chat sends
Queue a chat send locally when the peer's v3 onion is unreachable and retry
it with exponential backoff (30s doubling, capped at 30 min, 8 attempts then
terminal Failed). Still peer-to-peer: no server, no store-and-forward relay,
no new frame prefix — every attempt is a fresh Noise IK handshake.
- src/outbox.rs: Outbox + Deliverer + ArtiDeliverer, pure backoff_secs(),
is_permanent_send_error(), background worker (15s scan).
- store: `outbox` table (no body column — the body is read from the joined
messages row, under that row's existing AEAD + AAD), append_message_id(),
queue/due/pending/mark_* helpers, reset_sending_outbox(), wipe.
- Node::send() returns Result<OutboxStatus, String> (Sent/Queued) instead of
Err on transport failure; protocol errors still fail immediately and are
never queued.
- Retry triggers: timer, unlock/foreground (spawn_flush_all), and "that peer
was just seen" (handshake or connect_onion -> spawn_flush_friend).
- File transfer and payment/rcp frames stay fail-closed.
- docs/PROTOCOL.md + docs/THREAT_MODEL.md: replace the no-queue statements;
add the seizure/cadence delta to the threat model.
src/tui.rs gets one forced arm change (Ok(()) -> Ok(_)); UI surfacing is
child card t_d9a5b065.
2026-09-12 13:35:10 -04:00
|
|
|
//! Local outbox: queue a chat send when the peer's onion is unreachable, retry
|
|
|
|
|
//! with exponential backoff, expose a terminal status. All offline — the
|
|
|
|
|
//! transport is a fake deliverer, so these run without Tor.
|
|
|
|
|
|
|
|
|
|
use std::sync::atomic::{AtomicUsize, Ordering};
|
|
|
|
|
use std::sync::{Arc, Mutex};
|
|
|
|
|
|
fix(sdk): surface Sent/Queued on send(), and build the SDK in CI
`Node::send()` became `Result<OutboxStatus, String>` in the local-outbox work,
which broke `crates/onionwire-sdk` (E0308 at src/lib.rs:195) — and nothing
noticed, because the SDK is a separate cargo workspace that root CI never
compiles and android-release.yml only builds on a v*.*.* tag.
- `Wire::send()` now returns `WResult<SendState>` (`Sent` | `Queued`) instead
of swallowing the outcome as `()`. A queued send is local-only until the
retry worker gets a circuit through, so the Kotlin boundary must be able to
tell it apart from a delivered one.
- `ChatMessage` gains `delivery: Option<String>` ("sent" | "queued" |
"sending" | "failed: <reason>"), populated from the message's outbox row;
`None` for incoming messages. An outgoing row with no outbox row was accepted
on the first attempt, so it reads "sent".
- New `sdk` job in `.forgejo/workflows/ci.yml`: `cargo test --locked` +
`cargo clippy --locked --all-targets -- -D warnings` in `crates/onionwire-sdk`
in the same rust:1.91-bookworm container, with its own target volume (the SDK
resolves the root crate with rustls, so sharing the root job's target dir
would thrash feature-unified artifacts).
- Drift found while un-rotting it: the SDK `Cargo.lock` was stale (missing
`sha2`, added to the root crate by the zeroize work), so `--locked` failed;
and the SDK README still said "no outbox, no spool in v1" and "identity keys
in sqlite are plaintext", both untrue since the outbox and SQLCipher landed.
Tests: crates/onionwire-sdk 4 passed (2 new unit tests over the status
mapping), clippy clean; root `cargo test --locked` clean. The CI job body was
smoke-run locally in the same container image before being committed.
# Conflicts:
# crates/onionwire-sdk/src/lib.rs
2026-09-12 15:16:38 -04:00
|
|
|
use onionwire::Store;
|
feat(outbox): local retry queue for chat sends
Queue a chat send locally when the peer's v3 onion is unreachable and retry
it with exponential backoff (30s doubling, capped at 30 min, 8 attempts then
terminal Failed). Still peer-to-peer: no server, no store-and-forward relay,
no new frame prefix — every attempt is a fresh Noise IK handshake.
- src/outbox.rs: Outbox + Deliverer + ArtiDeliverer, pure backoff_secs(),
is_permanent_send_error(), background worker (15s scan).
- store: `outbox` table (no body column — the body is read from the joined
messages row, under that row's existing AEAD + AAD), append_message_id(),
queue/due/pending/mark_* helpers, reset_sending_outbox(), wipe.
- Node::send() returns Result<OutboxStatus, String> (Sent/Queued) instead of
Err on transport failure; protocol errors still fail immediately and are
never queued.
- Retry triggers: timer, unlock/foreground (spawn_flush_all), and "that peer
was just seen" (handshake or connect_onion -> spawn_flush_friend).
- File transfer and payment/rcp frames stay fail-closed.
- docs/PROTOCOL.md + docs/THREAT_MODEL.md: replace the no-queue statements;
add the seizure/cadence delta to the threat model.
src/tui.rs gets one forced arm change (Ok(()) -> Ok(_)); UI surfacing is
child card t_d9a5b065.
2026-09-12 13:35:10 -04:00
|
|
|
use onionwire::outbox::{
|
|
|
|
|
BACKOFF_CAP_SECS, BACKOFF_START_SECS, BoxFut, Deliverer, Outbox, OutboxStatus, backoff_secs,
|
|
|
|
|
is_permanent_send_error,
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
const PASS: &str = "outbox-pass";
|
|
|
|
|
|
|
|
|
|
fn pk(tag: u8) -> [u8; 32] {
|
|
|
|
|
let mut k = [0u8; 32];
|
|
|
|
|
k[0] = tag;
|
|
|
|
|
k
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// Fails `fail_times` dials with `err`, then succeeds and records the plaintext.
|
|
|
|
|
struct Flaky {
|
|
|
|
|
fail_times: AtomicUsize,
|
|
|
|
|
err: String,
|
|
|
|
|
calls: AtomicUsize,
|
|
|
|
|
delivered: Mutex<Vec<Vec<u8>>>,
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
impl Flaky {
|
|
|
|
|
fn new(fail_times: usize, err: &str) -> Arc<Self> {
|
|
|
|
|
Arc::new(Self {
|
|
|
|
|
fail_times: AtomicUsize::new(fail_times),
|
|
|
|
|
err: err.into(),
|
|
|
|
|
calls: AtomicUsize::new(0),
|
|
|
|
|
delivered: Mutex::new(Vec::new()),
|
|
|
|
|
})
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
fn calls(&self) -> usize {
|
|
|
|
|
self.calls.load(Ordering::SeqCst)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
fn delivered(&self) -> Vec<Vec<u8>> {
|
|
|
|
|
self.delivered.lock().unwrap().clone()
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
impl Deliverer for Flaky {
|
|
|
|
|
fn deliver<'a>(
|
|
|
|
|
&'a self,
|
|
|
|
|
_onion: &'a str,
|
|
|
|
|
_pinned_id: &'a [u8],
|
|
|
|
|
_prekey: &'a [u8],
|
|
|
|
|
plaintext: &'a [u8],
|
|
|
|
|
) -> BoxFut<'a, Result<(), String>> {
|
|
|
|
|
self.calls.fetch_add(1, Ordering::SeqCst);
|
|
|
|
|
let left = self.fail_times.load(Ordering::SeqCst);
|
|
|
|
|
if left > 0 {
|
|
|
|
|
self.fail_times.fetch_sub(1, Ordering::SeqCst);
|
|
|
|
|
let err = self.err.clone();
|
|
|
|
|
return Box::pin(async move { Err(err) });
|
|
|
|
|
}
|
|
|
|
|
self.delivered.lock().unwrap().push(plaintext.to_vec());
|
|
|
|
|
Box::pin(async move { Ok(()) })
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
fn store_and_friend(dir: &std::path::Path, onion: &str) -> Arc<Mutex<Store>> {
|
|
|
|
|
let store = Store::open_at_with_passphrase(dir, PASS).expect("open store");
|
|
|
|
|
store.upsert_friend(&pk(1), onion, None).unwrap();
|
|
|
|
|
store.set_friend_prekey(&pk(1), &[7u8; 32]).unwrap();
|
|
|
|
|
Arc::new(Mutex::new(store))
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
fn outbox(store: Arc<Mutex<Store>>, deliverer: Arc<dyn Deliverer>, max_attempts: u32) -> Outbox {
|
|
|
|
|
Outbox::new(store, deliverer).with_max_attempts(max_attempts)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
|
fn backoff_schedule_doubles_from_30s_and_caps_at_30m() {
|
|
|
|
|
assert_eq!(BACKOFF_START_SECS, 30);
|
|
|
|
|
assert_eq!(BACKOFF_CAP_SECS, 1800);
|
|
|
|
|
let got: Vec<i64> = (0..9).map(backoff_secs).collect();
|
|
|
|
|
assert_eq!(
|
|
|
|
|
got,
|
|
|
|
|
vec![30, 60, 120, 240, 480, 960, 1800, 1800, 1800],
|
|
|
|
|
"exponential backoff, capped"
|
|
|
|
|
);
|
|
|
|
|
assert_eq!(
|
|
|
|
|
backoff_secs(u32::MAX),
|
|
|
|
|
BACKOFF_CAP_SECS,
|
|
|
|
|
"no overflow at silly attempt counts"
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
|
fn transport_errors_are_retryable_protocol_errors_are_not() {
|
fix(sdk): surface Sent/Queued on send(), and build the SDK in CI
`Node::send()` became `Result<OutboxStatus, String>` in the local-outbox work,
which broke `crates/onionwire-sdk` (E0308 at src/lib.rs:195) — and nothing
noticed, because the SDK is a separate cargo workspace that root CI never
compiles and android-release.yml only builds on a v*.*.* tag.
- `Wire::send()` now returns `WResult<SendState>` (`Sent` | `Queued`) instead
of swallowing the outcome as `()`. A queued send is local-only until the
retry worker gets a circuit through, so the Kotlin boundary must be able to
tell it apart from a delivered one.
- `ChatMessage` gains `delivery: Option<String>` ("sent" | "queued" |
"sending" | "failed: <reason>"), populated from the message's outbox row;
`None` for incoming messages. An outgoing row with no outbox row was accepted
on the first attempt, so it reads "sent".
- New `sdk` job in `.forgejo/workflows/ci.yml`: `cargo test --locked` +
`cargo clippy --locked --all-targets -- -D warnings` in `crates/onionwire-sdk`
in the same rust:1.91-bookworm container, with its own target volume (the SDK
resolves the root crate with rustls, so sharing the root job's target dir
would thrash feature-unified artifacts).
- Drift found while un-rotting it: the SDK `Cargo.lock` was stale (missing
`sha2`, added to the root crate by the zeroize work), so `--locked` failed;
and the SDK README still said "no outbox, no spool in v1" and "identity keys
in sqlite are plaintext", both untrue since the outbox and SQLCipher landed.
Tests: crates/onionwire-sdk 4 passed (2 new unit tests over the status
mapping), clippy clean; root `cargo test --locked` clean. The CI job body was
smoke-run locally in the same container image before being committed.
# Conflicts:
# crates/onionwire-sdk/src/lib.rs
2026-09-12 15:16:38 -04:00
|
|
|
assert!(!is_permanent_send_error("connect abc.onion:80: timed out"));
|
feat(outbox): local retry queue for chat sends
Queue a chat send locally when the peer's v3 onion is unreachable and retry
it with exponential backoff (30s doubling, capped at 30 min, 8 attempts then
terminal Failed). Still peer-to-peer: no server, no store-and-forward relay,
no new frame prefix — every attempt is a fresh Noise IK handshake.
- src/outbox.rs: Outbox + Deliverer + ArtiDeliverer, pure backoff_secs(),
is_permanent_send_error(), background worker (15s scan).
- store: `outbox` table (no body column — the body is read from the joined
messages row, under that row's existing AEAD + AAD), append_message_id(),
queue/due/pending/mark_* helpers, reset_sending_outbox(), wipe.
- Node::send() returns Result<OutboxStatus, String> (Sent/Queued) instead of
Err on transport failure; protocol errors still fail immediately and are
never queued.
- Retry triggers: timer, unlock/foreground (spawn_flush_all), and "that peer
was just seen" (handshake or connect_onion -> spawn_flush_friend).
- File transfer and payment/rcp frames stay fail-closed.
- docs/PROTOCOL.md + docs/THREAT_MODEL.md: replace the no-queue statements;
add the seizure/cadence delta to the threat model.
src/tui.rs gets one forced arm change (Ok(()) -> Ok(_)); UI surfacing is
child card t_d9a5b065.
2026-09-12 13:35:10 -04:00
|
|
|
assert!(!is_permanent_send_error("connect abc.onion:80: refused"));
|
|
|
|
|
assert!(is_permanent_send_error("fingerprint mismatch"));
|
|
|
|
|
assert!(is_permanent_send_error("frame too large"));
|
|
|
|
|
assert!(is_permanent_send_error("bad version"));
|
|
|
|
|
assert!(is_permanent_send_error("bad identity proof"));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#[tokio::test]
|
|
|
|
|
async fn send_to_dead_onion_is_queued_and_row_persists() {
|
|
|
|
|
let dir = tempfile::tempdir().unwrap();
|
|
|
|
|
let store = store_and_friend(dir.path(), "dead.onion");
|
|
|
|
|
let dead = Flaky::new(usize::MAX, "connect dead.onion:80: timed out");
|
|
|
|
|
let ob = outbox(Arc::clone(&store), dead, 8);
|
|
|
|
|
|
|
|
|
|
let status = ob.send(&pk(1), b"hello dead peer").await.unwrap();
|
fix(sdk): surface Sent/Queued on send(), and build the SDK in CI
`Node::send()` became `Result<OutboxStatus, String>` in the local-outbox work,
which broke `crates/onionwire-sdk` (E0308 at src/lib.rs:195) — and nothing
noticed, because the SDK is a separate cargo workspace that root CI never
compiles and android-release.yml only builds on a v*.*.* tag.
- `Wire::send()` now returns `WResult<SendState>` (`Sent` | `Queued`) instead
of swallowing the outcome as `()`. A queued send is local-only until the
retry worker gets a circuit through, so the Kotlin boundary must be able to
tell it apart from a delivered one.
- `ChatMessage` gains `delivery: Option<String>` ("sent" | "queued" |
"sending" | "failed: <reason>"), populated from the message's outbox row;
`None` for incoming messages. An outgoing row with no outbox row was accepted
on the first attempt, so it reads "sent".
- New `sdk` job in `.forgejo/workflows/ci.yml`: `cargo test --locked` +
`cargo clippy --locked --all-targets -- -D warnings` in `crates/onionwire-sdk`
in the same rust:1.91-bookworm container, with its own target volume (the SDK
resolves the root crate with rustls, so sharing the root job's target dir
would thrash feature-unified artifacts).
- Drift found while un-rotting it: the SDK `Cargo.lock` was stale (missing
`sha2`, added to the root crate by the zeroize work), so `--locked` failed;
and the SDK README still said "no outbox, no spool in v1" and "identity keys
in sqlite are plaintext", both untrue since the outbox and SQLCipher landed.
Tests: crates/onionwire-sdk 4 passed (2 new unit tests over the status
mapping), clippy clean; root `cargo test --locked` clean. The CI job body was
smoke-run locally in the same container image before being committed.
# Conflicts:
# crates/onionwire-sdk/src/lib.rs
2026-09-12 15:16:38 -04:00
|
|
|
assert_eq!(
|
|
|
|
|
status,
|
|
|
|
|
OutboxStatus::Queued,
|
|
|
|
|
"dead onion must queue, not Err"
|
|
|
|
|
);
|
feat(outbox): local retry queue for chat sends
Queue a chat send locally when the peer's v3 onion is unreachable and retry
it with exponential backoff (30s doubling, capped at 30 min, 8 attempts then
terminal Failed). Still peer-to-peer: no server, no store-and-forward relay,
no new frame prefix — every attempt is a fresh Noise IK handshake.
- src/outbox.rs: Outbox + Deliverer + ArtiDeliverer, pure backoff_secs(),
is_permanent_send_error(), background worker (15s scan).
- store: `outbox` table (no body column — the body is read from the joined
messages row, under that row's existing AEAD + AAD), append_message_id(),
queue/due/pending/mark_* helpers, reset_sending_outbox(), wipe.
- Node::send() returns Result<OutboxStatus, String> (Sent/Queued) instead of
Err on transport failure; protocol errors still fail immediately and are
never queued.
- Retry triggers: timer, unlock/foreground (spawn_flush_all), and "that peer
was just seen" (handshake or connect_onion -> spawn_flush_friend).
- File transfer and payment/rcp frames stay fail-closed.
- docs/PROTOCOL.md + docs/THREAT_MODEL.md: replace the no-queue statements;
add the seizure/cadence delta to the threat model.
src/tui.rs gets one forced arm change (Ok(()) -> Ok(_)); UI surfacing is
child card t_d9a5b065.
2026-09-12 13:35:10 -04:00
|
|
|
assert!(!status.is_terminal());
|
|
|
|
|
|
|
|
|
|
let pending = ob.pending(&pk(1)).unwrap();
|
|
|
|
|
assert_eq!(pending.len(), 1, "one outbox row");
|
|
|
|
|
assert_eq!(pending[0].status, OutboxStatus::Queued);
|
|
|
|
|
assert_eq!(pending[0].attempts, 1, "the immediate attempt counted");
|
|
|
|
|
assert!(
|
fix(sdk): surface Sent/Queued on send(), and build the SDK in CI
`Node::send()` became `Result<OutboxStatus, String>` in the local-outbox work,
which broke `crates/onionwire-sdk` (E0308 at src/lib.rs:195) — and nothing
noticed, because the SDK is a separate cargo workspace that root CI never
compiles and android-release.yml only builds on a v*.*.* tag.
- `Wire::send()` now returns `WResult<SendState>` (`Sent` | `Queued`) instead
of swallowing the outcome as `()`. A queued send is local-only until the
retry worker gets a circuit through, so the Kotlin boundary must be able to
tell it apart from a delivered one.
- `ChatMessage` gains `delivery: Option<String>` ("sent" | "queued" |
"sending" | "failed: <reason>"), populated from the message's outbox row;
`None` for incoming messages. An outgoing row with no outbox row was accepted
on the first attempt, so it reads "sent".
- New `sdk` job in `.forgejo/workflows/ci.yml`: `cargo test --locked` +
`cargo clippy --locked --all-targets -- -D warnings` in `crates/onionwire-sdk`
in the same rust:1.91-bookworm container, with its own target volume (the SDK
resolves the root crate with rustls, so sharing the root job's target dir
would thrash feature-unified artifacts).
- Drift found while un-rotting it: the SDK `Cargo.lock` was stale (missing
`sha2`, added to the root crate by the zeroize work), so `--locked` failed;
and the SDK README still said "no outbox, no spool in v1" and "identity keys
in sqlite are plaintext", both untrue since the outbox and SQLCipher landed.
Tests: crates/onionwire-sdk 4 passed (2 new unit tests over the status
mapping), clippy clean; root `cargo test --locked` clean. The CI job body was
smoke-run locally in the same container image before being committed.
# Conflicts:
# crates/onionwire-sdk/src/lib.rs
2026-09-12 15:16:38 -04:00
|
|
|
ob.status_of_message(pending[0].message_id)
|
|
|
|
|
.unwrap()
|
|
|
|
|
.is_some(),
|
feat(outbox): local retry queue for chat sends
Queue a chat send locally when the peer's v3 onion is unreachable and retry
it with exponential backoff (30s doubling, capped at 30 min, 8 attempts then
terminal Failed). Still peer-to-peer: no server, no store-and-forward relay,
no new frame prefix — every attempt is a fresh Noise IK handshake.
- src/outbox.rs: Outbox + Deliverer + ArtiDeliverer, pure backoff_secs(),
is_permanent_send_error(), background worker (15s scan).
- store: `outbox` table (no body column — the body is read from the joined
messages row, under that row's existing AEAD + AAD), append_message_id(),
queue/due/pending/mark_* helpers, reset_sending_outbox(), wipe.
- Node::send() returns Result<OutboxStatus, String> (Sent/Queued) instead of
Err on transport failure; protocol errors still fail immediately and are
never queued.
- Retry triggers: timer, unlock/foreground (spawn_flush_all), and "that peer
was just seen" (handshake or connect_onion -> spawn_flush_friend).
- File transfer and payment/rcp frames stay fail-closed.
- docs/PROTOCOL.md + docs/THREAT_MODEL.md: replace the no-queue statements;
add the seizure/cadence delta to the threat model.
src/tui.rs gets one forced arm change (Ok(()) -> Ok(_)); UI surfacing is
child card t_d9a5b065.
2026-09-12 13:35:10 -04:00
|
|
|
"status is addressable by chat row id"
|
|
|
|
|
);
|
|
|
|
|
|
|
|
|
|
// The chat line is already in local history while it waits.
|
|
|
|
|
let msgs = store.lock().unwrap().list_messages(&pk(1)).unwrap();
|
|
|
|
|
assert_eq!(msgs.len(), 1);
|
|
|
|
|
assert_eq!(msgs[0].dir, "out");
|
|
|
|
|
assert_eq!(msgs[0].plaintext, b"hello dead peer");
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#[tokio::test]
|
|
|
|
|
async fn flaky_peer_is_flushed_by_the_worker_on_schedule() {
|
|
|
|
|
let dir = tempfile::tempdir().unwrap();
|
|
|
|
|
let store = store_and_friend(dir.path(), "flaky.onion");
|
|
|
|
|
// First dial fails (the immediate one), the rest succeed: "peer came up".
|
|
|
|
|
let flaky = Flaky::new(1, "connect flaky.onion:80: timed out");
|
|
|
|
|
let ob = outbox(Arc::clone(&store), flaky.clone(), 8);
|
|
|
|
|
|
|
|
|
|
let status = ob.send(&pk(1), b"late hello").await.unwrap();
|
|
|
|
|
assert_eq!(status, OutboxStatus::Queued);
|
|
|
|
|
let next = ob.pending(&pk(1)).unwrap()[0].next_attempt_at;
|
|
|
|
|
assert_eq!(
|
|
|
|
|
next - ob.pending(&pk(1)).unwrap()[0].created_at,
|
|
|
|
|
BACKOFF_START_SECS,
|
|
|
|
|
"first retry waits the start of the schedule"
|
|
|
|
|
);
|
|
|
|
|
|
|
|
|
|
// Too early: the worker must not dial out of schedule.
|
|
|
|
|
let t0 = next - BACKOFF_START_SECS;
|
|
|
|
|
let fired = ob.tick(t0 + BACKOFF_START_SECS - 1).await.unwrap();
|
|
|
|
|
assert!(fired.is_empty(), "not due yet");
|
|
|
|
|
|
|
|
|
|
let fired = ob.tick(t0 + BACKOFF_START_SECS).await.unwrap();
|
|
|
|
|
assert_eq!(fired.len(), 1);
|
|
|
|
|
assert_eq!(fired[0].1, OutboxStatus::Sent);
|
|
|
|
|
assert_eq!(flaky.delivered(), vec![b"late hello".to_vec()]);
|
|
|
|
|
assert!(
|
|
|
|
|
ob.pending(&pk(1)).unwrap().is_empty(),
|
|
|
|
|
"delivered rows leave the pending set"
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#[tokio::test]
|
|
|
|
|
async fn max_attempts_reaches_failed_and_stops_retrying() {
|
|
|
|
|
let dir = tempfile::tempdir().unwrap();
|
|
|
|
|
let store = store_and_friend(dir.path(), "gone.onion");
|
|
|
|
|
let dead = Flaky::new(usize::MAX, "connect gone.onion:80: timed out");
|
|
|
|
|
let ob = outbox(Arc::clone(&store), dead.clone(), 3);
|
|
|
|
|
|
|
|
|
|
assert_eq!(
|
|
|
|
|
ob.send(&pk(1), b"never lands").await.unwrap(),
|
|
|
|
|
OutboxStatus::Queued
|
|
|
|
|
);
|
|
|
|
|
assert_eq!(dead.calls(), 1, "immediate attempt");
|
|
|
|
|
|
|
|
|
|
// Walk the schedule forward; each tick is one more attempt.
|
|
|
|
|
let mut now = ob.pending(&pk(1)).unwrap()[0].next_attempt_at;
|
|
|
|
|
let mut last = OutboxStatus::Queued;
|
|
|
|
|
for _ in 0..6 {
|
|
|
|
|
let fired = ob.tick(now).await.unwrap();
|
|
|
|
|
if let Some((_, st)) = fired.first() {
|
|
|
|
|
last = st.clone();
|
|
|
|
|
}
|
|
|
|
|
if last == OutboxStatus::Failed("connect gone.onion:80: timed out".into()) {
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
now += BACKOFF_CAP_SECS;
|
|
|
|
|
}
|
|
|
|
|
assert_eq!(
|
|
|
|
|
last,
|
|
|
|
|
OutboxStatus::Failed("connect gone.onion:80: timed out".into()),
|
|
|
|
|
"gives up after max_attempts"
|
|
|
|
|
);
|
|
|
|
|
assert_eq!(dead.calls(), 3, "attempts stop at the cap");
|
|
|
|
|
|
|
|
|
|
let calls_at_cap = dead.calls();
|
|
|
|
|
for _ in 0..3 {
|
|
|
|
|
now += BACKOFF_CAP_SECS;
|
|
|
|
|
assert!(ob.tick(now).await.unwrap().is_empty(), "no more retries");
|
|
|
|
|
}
|
|
|
|
|
assert_eq!(dead.calls(), calls_at_cap);
|
|
|
|
|
|
|
|
|
|
let pending = ob.pending(&pk(1)).unwrap();
|
|
|
|
|
assert_eq!(pending.len(), 1);
|
|
|
|
|
assert!(matches!(pending[0].status, OutboxStatus::Failed(_)));
|
|
|
|
|
assert!(pending[0].status.is_terminal());
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#[tokio::test]
|
|
|
|
|
async fn queued_message_survives_reopen_and_flushes_after_unlock() {
|
|
|
|
|
let dir = tempfile::tempdir().unwrap();
|
|
|
|
|
let pk1 = pk(1);
|
|
|
|
|
let next_attempt_at = {
|
|
|
|
|
let store = store_and_friend(dir.path(), "later.onion");
|
|
|
|
|
let dead = Flaky::new(usize::MAX, "connect later.onion:80: timed out");
|
|
|
|
|
let ob = outbox(Arc::clone(&store), dead, 8);
|
|
|
|
|
assert_eq!(
|
|
|
|
|
ob.send(&pk1, b"survives restart").await.unwrap(),
|
|
|
|
|
OutboxStatus::Queued
|
|
|
|
|
);
|
|
|
|
|
ob.pending(&pk1).unwrap()[0].next_attempt_at
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
// Process exit: everything above is dropped and the store is reopened.
|
|
|
|
|
let store = Arc::new(Mutex::new(
|
|
|
|
|
Store::open_at_with_passphrase(dir.path(), PASS).unwrap(),
|
|
|
|
|
));
|
|
|
|
|
let up = Flaky::new(0, "");
|
|
|
|
|
let ob = outbox(Arc::clone(&store), up.clone(), 8);
|
|
|
|
|
let fired = ob.flush_all(next_attempt_at).await.unwrap();
|
|
|
|
|
assert_eq!(fired.len(), 1);
|
|
|
|
|
assert_eq!(fired[0].1, OutboxStatus::Sent);
|
|
|
|
|
assert_eq!(up.delivered(), vec![b"survives restart".to_vec()]);
|
|
|
|
|
assert!(ob.pending(&pk1).unwrap().is_empty());
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#[tokio::test]
|
|
|
|
|
async fn protocol_error_fails_immediately_and_is_not_queued() {
|
|
|
|
|
let dir = tempfile::tempdir().unwrap();
|
|
|
|
|
let store = store_and_friend(dir.path(), "liar.onion");
|
|
|
|
|
let liar = Flaky::new(usize::MAX, "fingerprint mismatch");
|
|
|
|
|
let ob = outbox(Arc::clone(&store), liar, 8);
|
|
|
|
|
|
|
|
|
|
let err = ob.send(&pk(1), b"hello liar").await.unwrap_err();
|
|
|
|
|
assert!(err.contains("fingerprint mismatch"), "got {err}");
|
|
|
|
|
assert!(
|
|
|
|
|
ob.pending(&pk(1)).unwrap().is_empty(),
|
|
|
|
|
"protocol errors are never queued"
|
|
|
|
|
);
|
|
|
|
|
assert!(
|
fix(sdk): surface Sent/Queued on send(), and build the SDK in CI
`Node::send()` became `Result<OutboxStatus, String>` in the local-outbox work,
which broke `crates/onionwire-sdk` (E0308 at src/lib.rs:195) — and nothing
noticed, because the SDK is a separate cargo workspace that root CI never
compiles and android-release.yml only builds on a v*.*.* tag.
- `Wire::send()` now returns `WResult<SendState>` (`Sent` | `Queued`) instead
of swallowing the outcome as `()`. A queued send is local-only until the
retry worker gets a circuit through, so the Kotlin boundary must be able to
tell it apart from a delivered one.
- `ChatMessage` gains `delivery: Option<String>` ("sent" | "queued" |
"sending" | "failed: <reason>"), populated from the message's outbox row;
`None` for incoming messages. An outgoing row with no outbox row was accepted
on the first attempt, so it reads "sent".
- New `sdk` job in `.forgejo/workflows/ci.yml`: `cargo test --locked` +
`cargo clippy --locked --all-targets -- -D warnings` in `crates/onionwire-sdk`
in the same rust:1.91-bookworm container, with its own target volume (the SDK
resolves the root crate with rustls, so sharing the root job's target dir
would thrash feature-unified artifacts).
- Drift found while un-rotting it: the SDK `Cargo.lock` was stale (missing
`sha2`, added to the root crate by the zeroize work), so `--locked` failed;
and the SDK README still said "no outbox, no spool in v1" and "identity keys
in sqlite are plaintext", both untrue since the outbox and SQLCipher landed.
Tests: crates/onionwire-sdk 4 passed (2 new unit tests over the status
mapping), clippy clean; root `cargo test --locked` clean. The CI job body was
smoke-run locally in the same container image before being committed.
# Conflicts:
# crates/onionwire-sdk/src/lib.rs
2026-09-12 15:16:38 -04:00
|
|
|
store
|
|
|
|
|
.lock()
|
|
|
|
|
.unwrap()
|
|
|
|
|
.list_messages(&pk(1))
|
|
|
|
|
.unwrap()
|
|
|
|
|
.is_empty(),
|
feat(outbox): local retry queue for chat sends
Queue a chat send locally when the peer's v3 onion is unreachable and retry
it with exponential backoff (30s doubling, capped at 30 min, 8 attempts then
terminal Failed). Still peer-to-peer: no server, no store-and-forward relay,
no new frame prefix — every attempt is a fresh Noise IK handshake.
- src/outbox.rs: Outbox + Deliverer + ArtiDeliverer, pure backoff_secs(),
is_permanent_send_error(), background worker (15s scan).
- store: `outbox` table (no body column — the body is read from the joined
messages row, under that row's existing AEAD + AAD), append_message_id(),
queue/due/pending/mark_* helpers, reset_sending_outbox(), wipe.
- Node::send() returns Result<OutboxStatus, String> (Sent/Queued) instead of
Err on transport failure; protocol errors still fail immediately and are
never queued.
- Retry triggers: timer, unlock/foreground (spawn_flush_all), and "that peer
was just seen" (handshake or connect_onion -> spawn_flush_friend).
- File transfer and payment/rcp frames stay fail-closed.
- docs/PROTOCOL.md + docs/THREAT_MODEL.md: replace the no-queue statements;
add the seizure/cadence delta to the threat model.
src/tui.rs gets one forced arm change (Ok(()) -> Ok(_)); UI surfacing is
child card t_d9a5b065.
2026-09-12 13:35:10 -04:00
|
|
|
"a failed protocol send is not written to history"
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#[tokio::test]
|
|
|
|
|
async fn flush_friend_ignores_the_schedule_when_that_peer_was_seen() {
|
|
|
|
|
let dir = tempfile::tempdir().unwrap();
|
|
|
|
|
let store = store_and_friend(dir.path(), "seen.onion");
|
|
|
|
|
let flaky = Flaky::new(1, "connect seen.onion:80: timed out");
|
|
|
|
|
let ob = outbox(Arc::clone(&store), flaky.clone(), 8);
|
|
|
|
|
|
|
|
|
|
assert_eq!(
|
|
|
|
|
ob.send(&pk(1), b"you are back").await.unwrap(),
|
|
|
|
|
OutboxStatus::Queued
|
|
|
|
|
);
|
|
|
|
|
let now = ob.pending(&pk(1)).unwrap()[0].created_at;
|
|
|
|
|
let fired = ob.flush_friend(&pk(1), now).await.unwrap();
|
|
|
|
|
assert_eq!(fired.len(), 1, "a live handshake flushes without waiting");
|
|
|
|
|
assert_eq!(fired[0].1, OutboxStatus::Sent);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#[tokio::test]
|
|
|
|
|
async fn wipe_clears_outbox_rows() {
|
|
|
|
|
let dir = tempfile::tempdir().unwrap();
|
|
|
|
|
let store = store_and_friend(dir.path(), "dead.onion");
|
|
|
|
|
let dead = Flaky::new(usize::MAX, "connect dead.onion:80: timed out");
|
|
|
|
|
let ob = outbox(Arc::clone(&store), dead, 8);
|
|
|
|
|
assert_eq!(
|
|
|
|
|
ob.send(&pk(1), b"wipe me").await.unwrap(),
|
|
|
|
|
OutboxStatus::Queued
|
|
|
|
|
);
|
|
|
|
|
assert_eq!(ob.pending(&pk(1)).unwrap().len(), 1);
|
|
|
|
|
|
|
|
|
|
store.lock().unwrap().wipe_messages().unwrap();
|
|
|
|
|
|
|
|
|
|
assert!(
|
|
|
|
|
ob.pending(&pk(1)).unwrap().is_empty(),
|
|
|
|
|
"wipe must drop queued sends, not only chat"
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#[tokio::test]
|
|
|
|
|
async fn missing_prekey_is_fatal_not_queued() {
|
|
|
|
|
let dir = tempfile::tempdir().unwrap();
|
|
|
|
|
let store = Arc::new(Mutex::new(
|
|
|
|
|
Store::open_at_with_passphrase(dir.path(), PASS).unwrap(),
|
|
|
|
|
));
|
|
|
|
|
{
|
|
|
|
|
let s = store.lock().unwrap();
|
|
|
|
|
s.upsert_friend(&pk(2), "noprekey.onion", None).unwrap();
|
|
|
|
|
}
|
|
|
|
|
let ob = outbox(Arc::clone(&store), Flaky::new(0, ""), 8);
|
|
|
|
|
let err = ob.send(&pk(2), b"hi").await.unwrap_err();
|
|
|
|
|
assert!(err.contains("prekey"), "got {err}");
|
|
|
|
|
assert!(ob.pending(&pk(2)).unwrap().is_empty());
|
|
|
|
|
}
|