feat(tui): join outbox rows to chat rows by message id (drop the body match) #50

Merged
sirius merged 2 commits from feat/tui-msgid-join into main 2026-09-12 22:11:27 +00:00
4 changed files with 89 additions and 41 deletions
Showing only changes of commit 2cbe7d41e5 - Show all commits

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.
rust-dev 2026-09-12 14:34:37 -04:00 • committed by agent
No known key found for this signature in database

View file

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

View file

@ -83,6 +83,9 @@ pub struct FriendProfile {
#[derive(Debug)]
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 plaintext: Vec<u8>,
}
@ -858,7 +861,11 @@ impl Store {
let aad = message_aad(friend_id, &dir, id);
let plaintext = crate::backup::aead_decrypt(&self.msg_key, &blob, &aad)
.map_err(|_| Error("message decrypt failed".into()))?;
out.push(Message { dir, plaintext });
out.push(Message {
id,
dir,
plaintext,
});
}
Ok(out)
}

View file

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

View file

@ -388,21 +388,24 @@ fn ascii_switch_80x24_frame_is_pure_ascii() {
// prefix, no parallel state model.
// ---------------------------------------------------------------------------
fn out_msg(body: &str) -> Message {
fn out_msg(id: i64, body: &str) -> Message {
Message {
id,
dir: "out".into(),
plaintext: body.as_bytes().to_vec(),
}
}
fn in_msg(body: &str) -> Message {
fn in_msg(id: i64, body: &str) -> Message {
Message {
id,
dir: "in".into(),
plaintext: body.as_bytes().to_vec(),
}
}
fn pending_row(
message_id: i64,
body: &str,
status: OutboxStatus,
attempts: u32,
@ -412,7 +415,7 @@ fn pending_row(
OutboxEntry {
id: 1,
friend_pk: vec![9u8; 32],
message_id: 1,
message_id,
body: body.as_bytes().to_vec(),
attempts,
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 {
pending_row(body, OutboxStatus::Queued, attempts, next_in, now)
fn queued_row(message_id: i64, body: &str, attempts: u32, next_in: i64, now: i64) -> OutboxEntry {
pending_row(message_id, body, OutboxStatus::Queued, attempts, next_in, now)
}
fn sending_row(body: &str, now: i64) -> OutboxEntry {
pending_row(body, OutboxStatus::Sending, 1, 0, now)
fn sending_row(message_id: i64, body: &str, now: i64) -> OutboxEntry {
pending_row(message_id, body, OutboxStatus::Sending, 1, 0, now)
}
fn failed_row(body: &str, why: &str, now: i64) -> OutboxEntry {
pending_row(body, OutboxStatus::Failed(why.into()), 8, 0, now)
fn failed_row(message_id: i64, body: &str, why: &str, now: i64) -> OutboxEntry {
pending_row(
message_id,
body,
OutboxStatus::Failed(why.into()),
8,
0,
now,
)
}
/// 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();
let _guard = guard;
let now = 1_700_000_000;
let msgs = vec![out_msg("ping"), out_msg("pong")];
let pending = vec![queued_row("pong", 2, 45, now)];
let msgs = vec![out_msg(1, "ping"), out_msg(2, "pong")];
let pending = vec![queued_row(2, "pong", 2, 45, 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);
}
/// 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]
fn delivery_markers_have_exercised_ascii_fallbacks() {
let _guard = ASCII_ENV.lock().expect("ascii env lock");
@ -498,16 +542,16 @@ fn delivery_markers_have_exercised_ascii_fallbacks() {
}
let now = 500;
let msgs = vec![
out_msg("alpha"),
out_msg("beta"),
out_msg("gamma"),
out_msg("delta"),
in_msg("epsilon"),
out_msg(1, "alpha"),
out_msg(2, "beta"),
out_msg(3, "gamma"),
out_msg(4, "delta"),
in_msg(5, "epsilon"),
];
let pending = vec![
failed_row("alpha", "connect peer.onion:80: connection timed out", now),
queued_row("beta", 1, 30, now),
sending_row("gamma", now),
failed_row(1, "alpha", "connect peer.onion:80: connection timed out", now),
queued_row(2, "beta", 1, 30, now),
sending_row(3, "gamma", 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 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 msgs = vec![out_msg("ok"), in_msg("pong"), out_msg(long)];
let pending = vec![queued_row(long, 3, 45, now)];
let msgs = vec![out_msg(1, "ok"), in_msg(2, "pong"), out_msg(3, long)];
let pending = vec![queued_row(3, long, 3, 45, now)];
let (lines, summary) = chat_with_delivery(&msgs, &pending, now);
for line in lines.iter().take(2) {