Compare commits

..
4 changed files with 41 additions and 89 deletions

View file

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

View file

@ -83,9 +83,6 @@ 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>,
} }
@ -861,11 +858,7 @@ 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 { out.push(Message { dir, plaintext });
id,
dir,
plaintext,
});
} }
Ok(out) Ok(out)
} }

View file

@ -243,10 +243,9 @@ 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 joined to messages on `Message::id` / /// Pending rows are matched to messages by body, newest send first: an
/// `OutboxEntry::message_id` — never on the body. Two identical bodies are /// outstanding retry is the most recent send of that text, and the outbox holds
/// distinct rows with distinct ids, so the marker always lands on the row that /// no message id the UI could join on.
/// actually owns the outbox entry.
pub fn chat_with_delivery( pub fn chat_with_delivery(
msgs: &[Message], msgs: &[Message],
pending: &[OutboxEntry], pending: &[OutboxEntry],
@ -272,18 +271,21 @@ pub fn chat_with_delivery(
); );
} }
// One row per message in practice (a retry updates its row, not inserts a let mut by_body: HashMap<&[u8], Vec<&OutboxEntry>> = HashMap::new();
// second), and `pending` is ordered by row id, so a hypothetical duplicate for entry in pending {
// resolves to the newest — the same rule the store's own by_body
// `outbox_status_of_message` uses. .entry(entry.body.as_slice())
let by_id: HashMap<i64, &OutboxEntry> = .or_default()
pending.iter().map(|entry| (entry.message_id, entry)).collect(); .push(entry);
}
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() { for (i, msg) in msgs.iter().enumerate().rev() {
if msg.dir != "out" { if msg.dir != "out" {
continue; continue;
} }
if let Some(entry) = by_id.get(&msg.id) { if let Some(rows) = by_body.get_mut(msg.plaintext.as_slice())
&& 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,24 +388,21 @@ fn ascii_switch_80x24_frame_is_pure_ascii() {
// prefix, no parallel state model. // prefix, no parallel state model.
// --------------------------------------------------------------------------- // ---------------------------------------------------------------------------
fn out_msg(id: i64, body: &str) -> Message { fn out_msg(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(id: i64, body: &str) -> Message { fn in_msg(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,
@ -415,7 +412,7 @@ fn pending_row(
OutboxEntry { OutboxEntry {
id: 1, id: 1,
friend_pk: vec![9u8; 32], friend_pk: vec![9u8; 32],
message_id, message_id: 1,
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,
@ -425,23 +422,16 @@ fn pending_row(
} }
} }
fn queued_row(message_id: i64, body: &str, attempts: u32, next_in: i64, now: i64) -> OutboxEntry { fn queued_row(body: &str, attempts: u32, next_in: i64, now: i64) -> OutboxEntry {
pending_row(message_id, body, OutboxStatus::Queued, attempts, next_in, now) pending_row(body, OutboxStatus::Queued, attempts, next_in, now)
} }
fn sending_row(message_id: i64, body: &str, now: i64) -> OutboxEntry { fn sending_row(body: &str, now: i64) -> OutboxEntry {
pending_row(message_id, body, OutboxStatus::Sending, 1, 0, now) pending_row(body, OutboxStatus::Sending, 1, 0, now)
} }
fn failed_row(message_id: i64, body: &str, why: &str, now: i64) -> OutboxEntry { fn failed_row(body: &str, why: &str, now: i64) -> OutboxEntry {
pending_row( pending_row(body, OutboxStatus::Failed(why.into()), 8, 0, now)
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.
@ -463,8 +453,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(1, "ping"), out_msg(2, "pong")]; let msgs = vec![out_msg("ping"), out_msg("pong")];
let pending = vec![queued_row(2, "pong", 2, 45, now)]; let pending = vec![queued_row("pong", 2, 45, now)];
let (lines, summary) = chat_with_delivery(&msgs, &pending, now); let (lines, summary) = chat_with_delivery(&msgs, &pending, now);
@ -499,40 +489,6 @@ 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");
@ -542,16 +498,16 @@ fn delivery_markers_have_exercised_ascii_fallbacks() {
} }
let now = 500; let now = 500;
let msgs = vec![ let msgs = vec![
out_msg(1, "alpha"), out_msg("alpha"),
out_msg(2, "beta"), out_msg("beta"),
out_msg(3, "gamma"), out_msg("gamma"),
out_msg(4, "delta"), out_msg("delta"),
in_msg(5, "epsilon"), in_msg("epsilon"),
]; ];
let pending = vec![ let pending = vec![
failed_row(1, "alpha", "connect peer.onion:80: connection timed out", now), failed_row("alpha", "connect peer.onion:80: connection timed out", now),
queued_row(2, "beta", 1, 30, now), queued_row("beta", 1, 30, now),
sending_row(3, "gamma", now), sending_row("gamma", now),
]; ];
let (lines, summary) = chat_with_delivery(&msgs, &pending, now); let (lines, summary) = chat_with_delivery(&msgs, &pending, now);
@ -673,8 +629,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(1, "ok"), in_msg(2, "pong"), out_msg(3, long)]; let msgs = vec![out_msg("ok"), in_msg("pong"), out_msg(long)];
let pending = vec![queued_row(3, long, 3, 45, now)]; let pending = vec![queued_row(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) {