feat(tui): join outbox rows to chat rows by message id
All checks were successful
ci / test (pull_request) Successful in 4m12s

`chat_with_delivery` matched pending outbox rows to chat rows by body
because `store::Message` carried no id, so an older send that was still
queued showed a delivered mark whenever a newer send of the same text had
already crossed the circuit.

`store::Message` now carries its `messages.id`, and the TUI joins
`OutboxEntry::message_id` == `Message::id`. The body match and its
newest-first fallback are gone.

`list_messages()` keeps its signature; callers that do not need the id
are untouched.
This commit is contained in:
rust-dev 2026-09-12 14:34:37 -04:00 • committed by agent
commit 2cbe7d41e5
No known key found for this signature in database
4 changed files with 89 additions and 41 deletions

View file

@ -607,11 +607,10 @@ prefix, no server.
- The tag trails the body and wraps with it, so it is never clipped away. - The tag trails the body and wraps with it, so it is never clipped away.
Correlation: a delivered row is deleted from the outbox, so an outbound message Correlation: a delivered row is deleted from the outbox, so an outbound message
with no row is `Sent`; pending rows are matched to messages by body, newest send with no row is `Sent`; pending rows are joined to messages on the row id —
first (`chat_with_delivery`). The outbox holds no message id the UI can join on, `store::Message::id` equals `OutboxEntry::message_id` (`chat_with_delivery`).
so two identical bodies where only the newer one is still queued attach the Two identical bodies are distinct rows with distinct ids, so the marker always
marker to the newer send. An id-bearing accessor would remove that last lands on the row that actually owns the outbox entry.
ambiguity; see the follow-up card noted in the PR.
### Size floor / target ### Size floor / target

View file

@ -83,6 +83,9 @@ pub struct FriendProfile {
#[derive(Debug)] #[derive(Debug)]
pub struct Message { pub struct Message {
/// `messages.id` row id. The outbox row for a send carries the same
/// `message_id`, so the UI can join exactly instead of matching bodies.
pub id: i64,
pub dir: String, pub dir: String,
pub plaintext: Vec<u8>, pub plaintext: Vec<u8>,
} }
@ -858,7 +861,11 @@ impl Store {
let aad = message_aad(friend_id, &dir, id); let aad = message_aad(friend_id, &dir, id);
let plaintext = crate::backup::aead_decrypt(&self.msg_key, &blob, &aad) let plaintext = crate::backup::aead_decrypt(&self.msg_key, &blob, &aad)
.map_err(|_| Error("message decrypt failed".into()))?; .map_err(|_| Error("message decrypt failed".into()))?;
out.push(Message { dir, plaintext }); out.push(Message {
id,
dir,
plaintext,
});
} }
Ok(out) Ok(out)
} }

View file

@ -243,9 +243,10 @@ impl OutboxSummary {
/// Chat transcript with per-message delivery state, plus the footer summary. /// Chat transcript with per-message delivery state, plus the footer summary.
/// ///
/// Pending rows are matched to messages by body, newest send first: an /// Pending rows are joined to messages on `Message::id` /
/// outstanding retry is the most recent send of that text, and the outbox holds /// `OutboxEntry::message_id` — never on the body. Two identical bodies are
/// no message id the UI could join on. /// distinct rows with distinct ids, so the marker always lands on the row that
/// actually owns the outbox entry.
pub fn chat_with_delivery( pub fn chat_with_delivery(
msgs: &[Message], msgs: &[Message],
pending: &[OutboxEntry], pending: &[OutboxEntry],
@ -271,21 +272,18 @@ pub fn chat_with_delivery(
); );
} }
let mut by_body: HashMap<&[u8], Vec<&OutboxEntry>> = HashMap::new(); // One row per message in practice (a retry updates its row, not inserts a
for entry in pending { // second), and `pending` is ordered by row id, so a hypothetical duplicate
by_body // resolves to the newest — the same rule the store's own
.entry(entry.body.as_slice()) // `outbox_status_of_message` uses.
.or_default() let by_id: HashMap<i64, &OutboxEntry> =
.push(entry); pending.iter().map(|entry| (entry.message_id, entry)).collect();
}
let mut tags: Vec<Option<String>> = vec![None; msgs.len()]; let mut tags: Vec<Option<String>> = vec![None; msgs.len()];
for (i, msg) in msgs.iter().enumerate().rev() { for (i, msg) in msgs.iter().enumerate() {
if msg.dir != "out" { if msg.dir != "out" {
continue; continue;
} }
if let Some(rows) = by_body.get_mut(msg.plaintext.as_slice()) if let Some(entry) = by_id.get(&msg.id) {
&& let Some(entry) = rows.pop()
{
tags[i] = Some(delivery_tag( tags[i] = Some(delivery_tag(
&entry.status, &entry.status,
entry.attempts, entry.attempts,

View file

@ -388,21 +388,24 @@ fn ascii_switch_80x24_frame_is_pure_ascii() {
// prefix, no parallel state model. // prefix, no parallel state model.
// --------------------------------------------------------------------------- // ---------------------------------------------------------------------------
fn out_msg(body: &str) -> Message { fn out_msg(id: i64, body: &str) -> Message {
Message { Message {
id,
dir: "out".into(), dir: "out".into(),
plaintext: body.as_bytes().to_vec(), plaintext: body.as_bytes().to_vec(),
} }
} }
fn in_msg(body: &str) -> Message { fn in_msg(id: i64, body: &str) -> Message {
Message { Message {
id,
dir: "in".into(), dir: "in".into(),
plaintext: body.as_bytes().to_vec(), plaintext: body.as_bytes().to_vec(),
} }
} }
fn pending_row( fn pending_row(
message_id: i64,
body: &str, body: &str,
status: OutboxStatus, status: OutboxStatus,
attempts: u32, attempts: u32,
@ -412,7 +415,7 @@ fn pending_row(
OutboxEntry { OutboxEntry {
id: 1, id: 1,
friend_pk: vec![9u8; 32], friend_pk: vec![9u8; 32],
message_id: 1, message_id,
body: body.as_bytes().to_vec(), body: body.as_bytes().to_vec(),
attempts, attempts,
next_attempt_at: now + next_in, next_attempt_at: now + next_in,
@ -422,16 +425,23 @@ fn pending_row(
} }
} }
fn queued_row(body: &str, attempts: u32, next_in: i64, now: i64) -> OutboxEntry { fn queued_row(message_id: i64, body: &str, attempts: u32, next_in: i64, now: i64) -> OutboxEntry {
pending_row(body, OutboxStatus::Queued, attempts, next_in, now) pending_row(message_id, body, OutboxStatus::Queued, attempts, next_in, now)
} }
fn sending_row(body: &str, now: i64) -> OutboxEntry { fn sending_row(message_id: i64, body: &str, now: i64) -> OutboxEntry {
pending_row(body, OutboxStatus::Sending, 1, 0, now) pending_row(message_id, body, OutboxStatus::Sending, 1, 0, now)
} }
fn failed_row(body: &str, why: &str, now: i64) -> OutboxEntry { fn failed_row(message_id: i64, body: &str, why: &str, now: i64) -> OutboxEntry {
pending_row(body, OutboxStatus::Failed(why.into()), 8, 0, now) pending_row(
message_id,
body,
OutboxStatus::Failed(why.into()),
8,
0,
now,
)
} }
/// Marker is everything after the two-space separator the render helper uses. /// Marker is everything after the two-space separator the render helper uses.
@ -453,8 +463,8 @@ fn queued_and_delivered_messages_render_different_markers() {
force_unicode_mode(); force_unicode_mode();
let _guard = guard; let _guard = guard;
let now = 1_700_000_000; let now = 1_700_000_000;
let msgs = vec![out_msg("ping"), out_msg("pong")]; let msgs = vec![out_msg(1, "ping"), out_msg(2, "pong")];
let pending = vec![queued_row("pong", 2, 45, now)]; let pending = vec![queued_row(2, "pong", 2, 45, now)];
let (lines, summary) = chat_with_delivery(&msgs, &pending, now); let (lines, summary) = chat_with_delivery(&msgs, &pending, now);
@ -489,6 +499,40 @@ fn queued_and_delivered_messages_render_different_markers() {
assert_eq!(summary.failed, 0); assert_eq!(summary.failed, 0);
} }
/// The case the body match got wrong: the same text sent twice, only the
/// *older* send still queued. Joined by body, the (single) pending row lands on
/// the newest send and the actually-queued row shows `✓` — which is exactly
/// false delivery. The join is on `Message::id` / `OutboxEntry::message_id`.
#[test]
fn identical_bodies_join_by_message_id_not_body() {
let guard = ASCII_ENV.lock().expect("ascii env lock");
force_unicode_mode();
let _guard = guard;
let now = 1_700_000_000;
let msgs = vec![out_msg(7, "same text"), out_msg(8, "same text")];
let pending = vec![queued_row(7, "same text", 1, 30, now)];
let (lines, summary) = chat_with_delivery(&msgs, &pending, now);
assert!(
lines[0].contains('◷') && lines[0].contains("queued"),
"queued marker must land on the queued row (id 7): {:?}",
lines[0]
);
assert!(
!lines[0].contains('✓'),
"queued row must not claim delivery: {:?}",
lines[0]
);
assert_eq!(
marker_of(&lines[1]),
"✓",
"delivered row (id 8) must keep its mark: {:?}",
lines[1]
);
assert_eq!(summary.note().as_deref(), Some("1 queued"));
}
#[test] #[test]
fn delivery_markers_have_exercised_ascii_fallbacks() { fn delivery_markers_have_exercised_ascii_fallbacks() {
let _guard = ASCII_ENV.lock().expect("ascii env lock"); let _guard = ASCII_ENV.lock().expect("ascii env lock");
@ -498,16 +542,16 @@ fn delivery_markers_have_exercised_ascii_fallbacks() {
} }
let now = 500; let now = 500;
let msgs = vec![ let msgs = vec![
out_msg("alpha"), out_msg(1, "alpha"),
out_msg("beta"), out_msg(2, "beta"),
out_msg("gamma"), out_msg(3, "gamma"),
out_msg("delta"), out_msg(4, "delta"),
in_msg("epsilon"), in_msg(5, "epsilon"),
]; ];
let pending = vec![ let pending = vec![
failed_row("alpha", "connect peer.onion:80: connection timed out", now), failed_row(1, "alpha", "connect peer.onion:80: connection timed out", now),
queued_row("beta", 1, 30, now), queued_row(2, "beta", 1, 30, now),
sending_row("gamma", now), sending_row(3, "gamma", now),
]; ];
let (lines, summary) = chat_with_delivery(&msgs, &pending, now); let (lines, summary) = chat_with_delivery(&msgs, &pending, now);
@ -629,8 +673,8 @@ fn chat_and_footer_rows_fit_80x24_and_wrap_instead_of_clipping() {
let _guard = guard; let _guard = guard;
let now = 1_700_000_000; let now = 1_700_000_000;
let long = "this is a very long chat line that should wrap inside the chat pane instead of running off the right edge of an 80-column terminal and colliding with the roster border"; let long = "this is a very long chat line that should wrap inside the chat pane instead of running off the right edge of an 80-column terminal and colliding with the roster border";
let msgs = vec![out_msg("ok"), in_msg("pong"), out_msg(long)]; let msgs = vec![out_msg(1, "ok"), in_msg(2, "pong"), out_msg(3, long)];
let pending = vec![queued_row(long, 3, 45, now)]; let pending = vec![queued_row(3, long, 3, 45, now)];
let (lines, summary) = chat_with_delivery(&msgs, &pending, now); let (lines, summary) = chat_with_delivery(&msgs, &pending, now);
for line in lines.iter().take(2) { for line in lines.iter().take(2) {