Bind received whispers and replies to the original sender identity

This commit is contained in:
omar committed 2026-11-27 16:33:15 +03:00
1 parent d1450439d5
commit 20240c1027
12 files changed
+205 -80

No files matched your search

+48 -35
View File
@@ -96,12 +96,12 @@ struct Line {
value: Value,
}
#[derive(Clone, Copy, PartialEq, Eq)]
#[derive(PartialEq, Eq)]
enum Seen {
Playback { from: u64, tone: Option<usize> },
RemoteStarted(u64),
RemoteStopped,
WhisperReceived(u64),
WhisperReceived(Value),
LocalStarted,
Muted,
Offline,
@@ -264,10 +264,7 @@ impl Peer {
Some(Seen::RemoteStopped)
}
}
Some("whisper_received") => {
let from = value["client_id"].as_u64().ok_or("whisper_received without client ID")?;
Some(Seen::WhisperReceived(from))
}
Some("whisper_received") => Some(Seen::WhisperReceived(value)),
Some("audio") => {
let from = value["speakers"].as_array().filter(|s| s.len() == 1)
.and_then(|s| s[0].as_u64()).ok_or("expected exactly one decoded speaker")?;
@@ -448,8 +445,8 @@ impl Duplex {
fn receipt(peer: &Peer, phase: &str, from: u64, expected: usize, since: Instant) -> Result<Receipt> {
let mut found: Option<Receipt> = None;
let mut remote_started = false;
for &(at, seen) in peer.seen.iter().filter(|(at, _)| *at >= since) {
match seen {
for &(at, ref seen) in peer.seen.iter().filter(|(at, _)| *at >= since) {
match *seen {
Seen::RemoteStarted(id) if id == from => remote_started = true,
Seen::RemoteStarted(_) => {
return Err(format!("{phase}: {}: talking start from the wrong peer; {}",
@@ -483,8 +480,8 @@ impl Duplex {
fn evidence(peer: &Peer, since: Instant) -> String {
let mut tones = [0usize; HZ.len()];
let (mut unknown, mut starts, mut stops, mut whispers, mut tx_starts, mut muted, mut offline) = (0, 0, 0, 0, 0, 0, 0);
for &(_, seen) in peer.seen.iter().filter(|(at, _)| *at >= since) {
match seen {
for &(_, ref seen) in peer.seen.iter().filter(|(at, _)| *at >= since) {
match *seen {
Seen::Playback { tone: Some(tone), .. } => tones[tone] += 1,
Seen::Playback { tone: None, .. } => unknown += 1,
Seen::RemoteStarted(_) => starts += 1,
@@ -503,8 +500,8 @@ impl Duplex {
let block_remote = !matches!(receive, ReceiveGate::Open);
let mut permitted_tail = 0;
let mut blocked_events = 0;
for &(at, seen) in peer.seen.iter().filter(|(at, _)| *at >= since) {
let forbidden = match seen {
for &(at, ref seen) in peer.seen.iter().filter(|(at, _)| *at >= since) {
let forbidden = match *seen {
Seen::LocalStarted => block_local,
Seen::RemoteStarted(_) | Seen::WhisperReceived(_) => block_remote,
Seen::Playback { from, tone } => match receive {
@@ -604,7 +601,7 @@ fn exercise(exe: &Path, root: &Path) -> Result<String> {
let mute_phase = "input mute (900 Hz blocked marker)";
Duplex::check_gate(&qa.a, mute_phase, muted.first, true, ReceiveGate::Open)?;
Duplex::check_gate(&qa.b, mute_phase, muted.first, false, ReceiveGate::Closed)?;
if !qa.a.seen.iter().any(|&(at, seen)| at >= muted.first && seen == Seen::Muted) {
if !qa.a.seen.iter().any(|(at, seen)| *at >= muted.first && *seen == Seen::Muted) {
return Err(format!("{mute_phase}: no core input rejection; {}", Duplex::evidence(&qa.a, muted.first)));
}
qa.mute_a(false)?;
@@ -636,7 +633,7 @@ fn exercise(exe: &Path, root: &Path) -> Result<String> {
let offline_phase = "old session (1450 Hz blocked marker)";
Duplex::check_gate(&qa.a, offline_phase, disconnected, true, ReceiveGate::Closed)?;
Duplex::check_gate(&qa.b, offline_phase, old, true, ReceiveGate::Closed)?;
if !qa.a.seen.iter().any(|&(at, seen)| at >= old && seen == Seen::Offline)
if !qa.a.seen.iter().any(|(at, seen)| *at >= old && *seen == Seen::Offline)
|| qa.a.state_changes != offline_changes
{
return Err(format!("{offline_phase}: no offline rejection or connection state changed; {}", Duplex::evidence(&qa.a, old)));
@@ -687,7 +684,7 @@ fn whisper_spurt(qa: &mut Duplex, target_id: u64, target_uid: &str, tone: usize)
let epoch = WHISPER_TAGS[tone].ok_or("whisper probe must carry an epoch")?;
start_whisper(qa, target_id, target_uid, epoch)?;
let burst = qa.burst(Some(tone), None, 42)?;
if !qa.a.transmitting || !qa.a.seen.iter().any(|&(at, seen)| at >= burst.first && seen == Seen::LocalStarted) {
if !qa.a.transmitting || !qa.a.seen.iter().any(|(at, seen)| *at >= burst.first && *seen == Seen::LocalStarted) {
return Err(format!("{} Hz whisper did not start transmitting; {}", HZ[tone], Duplex::evidence(&qa.a, burst.first)));
}
@@ -703,21 +700,30 @@ fn whisper_spurt(qa: &mut Duplex, target_id: u64, target_uid: &str, tone: usize)
Ok(burst)
}
fn check_whisper_event(peer: &Peer, phase: &str, since: Instant, expected: Option<u64>) -> Result<()> {
let mut received = peer.seen.iter().filter_map(|&(at, seen)| match seen {
Seen::WhisperReceived(from) if at >= since => Some(from),
fn check_whisper_event(peer: &Peer, phase: &str, since: Instant, expected: Option<&Value>) -> Result<()> {
let mut received = peer.seen.iter().filter_map(|(at, seen)| match seen {
Seen::WhisperReceived(event) if *at >= since => Some(event),
_ => None,
});
if received.next() != expected || received.next().is_some() {
return Err(format!("{phase}: unexpected whisper_received count or sender; {}", Duplex::evidence(peer, since)));
let matched = match (received.next(), expected) {
(Some(actual), Some(expected)) => ["client_id", "generation", "uid", "nickname"]
.into_iter().all(|key| actual[key] == expected[key]),
(None, None) => true,
_ => false,
};
if !matched || received.next().is_some() {
return Err(format!("{phase}: unexpected whisper_received count or sender identity; {}", Duplex::evidence(peer, since)));
}
Ok(())
}
fn roster_uid(peer: &Peer, client_id: u64, is_self: bool) -> Option<&str> {
peer.snapshot["clients"].as_array()?.iter().find(|client| {
fn roster_identity(peer: &Peer, client_id: u64, is_self: bool) -> Option<(&str, &str)> {
let client = peer.snapshot["clients"].as_array()?.iter().find(|client| {
client["id"].as_u64() == Some(client_id) && client["is_self"].as_bool() == Some(is_self)
})?["uid"].as_str().filter(|uid| !uid.is_empty())
})?;
let uid = client["uid"].as_str().filter(|uid| !uid.is_empty())?;
let nickname = client["nickname"].as_str().filter(|name| !name.is_empty())?;
Some((uid, nickname))
}
fn is_beta14_default(snapshot: &Value) -> bool {
@@ -740,21 +746,28 @@ fn exercise_whisper(exe: &Path, root: &Path) -> Result<String> {
}
}
// The server's live UID can differ from the local profile's key-derived UID.
// Match the receiver's own roster entry to the sender's view of that same client.
qa.wait_until("receiver UID in both rosters", CONTROL, |qa| {
roster_uid(&qa.b, b_id, true).is_some() && roster_uid(&qa.a, b_id, false).is_some()
// Use independent live rosters, not the profile's key-derived UID or the receipt itself.
qa.wait_until("peer identities in both rosters", CONTROL, |qa| {
roster_identity(&qa.a, a_id, true).is_some() && roster_identity(&qa.b, a_id, false).is_some()
&& roster_identity(&qa.b, b_id, true).is_some() && roster_identity(&qa.a, b_id, false).is_some()
})?;
let b_uid = roster_uid(&qa.b, b_id, true).ok_or("receiver self roster lacks a UID")?;
if roster_uid(&qa.a, b_id, false) != Some(b_uid) {
return Err("receiver UID does not match the sender's live roster".into());
let sender = roster_identity(&qa.a, a_id, true).ok_or("sender self roster lacks identity")?;
if roster_identity(&qa.b, a_id, false) != Some(sender) {
return Err("sender identity does not match the receiver's live roster".into());
}
let b_uid = b_uid.to_owned();
let receiver = roster_identity(&qa.b, b_id, true).ok_or("receiver self roster lacks identity")?;
if roster_identity(&qa.a, b_id, false) != Some(receiver) {
return Err("receiver identity does not match the sender's live roster".into());
}
let b_uid = receiver.0.to_owned();
let generation = qa.b.snapshot["generation"].as_u64().filter(|generation| *generation > 0)
.ok_or("receiver roster lacks a connection generation")?;
let sender_receipt = json!({ "client_id": a_id, "generation": generation, "uid": sender.0, "nickname": sender.1 });
set_whisper_receive(&mut qa, "allow_all")?;
let initial = whisper_spurt(&mut qa, b_id, &b_uid, A_TONE)?;
let first = Duplex::receipt(&qa.b, "direct whisper", a_id, A_TONE, initial.first)?;
check_whisper_event(&qa.b, "direct whisper", initial.first, Some(a_id))?;
check_whisper_event(&qa.b, "direct whisper", initial.first, Some(&sender_receipt))?;
let denied = Instant::now();
set_whisper_receive(&mut qa, "deny_all")?;
@@ -764,7 +777,7 @@ fn exercise_whisper(exe: &Path, root: &Path) -> Result<String> {
set_whisper_receive(&mut qa, "allow_all")?;
let restored = whisper_spurt(&mut qa, b_id, &b_uid, B_TONE)?;
let again = Duplex::receipt(&qa.b, "restored allow_all", a_id, B_TONE, restored.first)?;
check_whisper_event(&qa.b, "restored allow_all", restored.first, Some(a_id))?;
check_whisper_event(&qa.b, "restored allow_all", restored.first, Some(&sender_receipt))?;
// Both peers are still connected and receive policy is open. A fallback to either
// whisper or ordinary voice would be visible; the new tone cannot be the old audio tail.
@@ -780,7 +793,7 @@ fn exercise_whisper(exe: &Path, root: &Path) -> Result<String> {
start_whisper(&mut qa, b_id, &b_uid, held_epoch)?;
let held = qa.burst(Some(OLD_TONE), None, 42)?;
Duplex::receipt(&qa.b, "held whisper before disconnect", a_id, OLD_TONE, held.first)?;
check_whisper_event(&qa.b, "held whisper before disconnect", held.first, Some(a_id))?;
check_whisper_event(&qa.b, "held whisper before disconnect", held.first, Some(&sender_receipt))?;
if !qa.a.transmitting { return Err("sender was not transmitting at disconnect".into()); }
let held_confirmed = Duplex::disconnect(&mut qa.a, &qa.tones)?;
qa.wait_until("held whisper sender leaving roster", CONTROL, |qa| {
@@ -806,7 +819,7 @@ fn exercise_whisper(exe: &Path, root: &Path) -> Result<String> {
qa.a.shutdown(&qa.tones)?;
qa.b.shutdown(&qa.tones)?;
let confirmed = [held_confirmed, a_confirmed, b_confirmed].into_iter().filter(|confirmed| *confirmed).count();
Ok(format!("Whisper IPC passed: direct 650 Hz + whisper_received ({} frames); deny_all 900 Hz suppressed (no playback or whisper_received); restored allow_all 1150 Hz + whisper_received ({} frames); stale tag 1450 Hz dropped after stop and after reconnect during a live whisper; untagged 1750 Hz ordinary voice after reconnect ({} frames, no whisper_received). Server confirmed {confirmed}/3 disconnects. No device audio.",
Ok(format!("Whisper IPC passed: direct 650 Hz + roster-matched whisper_received ({} frames); deny_all 900 Hz suppressed (no playback or whisper_received); restored allow_all 1150 Hz + roster-matched whisper_received ({} frames); stale tag 1450 Hz dropped after stop and after reconnect during a live whisper; untagged 1750 Hz ordinary voice after reconnect ({} frames, no whisper_received). Server confirmed {confirmed}/3 disconnects. No device audio.",
first.frames, again.frames, voice.frames))
}