Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
353 changes: 353 additions & 0 deletions cli/src/bridge/v2/faults.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3146,3 +3146,356 @@ async fn stop_ends_background_work_left_running_after_its_turn() {
Some("Nothing is running.")
);
}

/// A voice message from the owner in the DM. `transcript` is the text the
/// server edits in; the audio URL is never fetched (not HTTPS).
fn voice_message(message_id: i64, duration_ms: u64, transcript: Option<&str>) -> MessageRecord {
MessageRecord {
chat_id: InlineId::new(9),
message_id: InlineId::new(message_id),
sender_id: InlineId::new(1),
timestamp: 1,
is_outgoing: false,
reply_to_message_id: None,
transaction: None,
content: inline_client::MessageContent::Media {
kind: inline_client::MediaKind::Voice,
file_id: format!("voice-{message_id}"),
url: Some(format!("http://127.0.0.1:9/voice-{message_id}.m4a")),
mime_type: Some("audio/mp4".into()),
file_name: Some("voice.m4a".into()),
caption: transcript.map(str::to_owned),
size_bytes: Some(2048),
width: None,
height: None,
duration_ms: Some(duration_ms),
},
metadata: inline_client::MessageMetadata {
sender_is_bot: Some(false),
edit_timestamp: transcript.map(|_| 2),
..Default::default()
},
}
}

fn received(harness: &FakeHarness) -> Vec<Input> {
harness.session.received.lock().unwrap().clone()
}

fn sends(harness: &FakeHarness) -> usize {
harness.session.sends.load(Ordering::SeqCst)
}

fn voice_holds(runtime: &Runtime) -> usize {
runtime.agent.ledger.voice_holds().unwrap().len()
}

/// Past every voice deadline.
fn after_deadlines() -> i64 {
super::voice::now_ms() + 100_000
}

#[tokio::test]
async fn a_voice_message_waits_for_its_transcript_and_goes_out_without_its_audio() {
let (mut runtime, harness, mut cleanup) = fixture(false);
runtime.agent.names.remember(1, "Owner".into()).await;
let id = input_id(2, 9, 300, "direct");
runtime
.on_message(&voice_message(300, 4_000, None))
.await
.unwrap();
assert_eq!(state(&runtime, &id), Some(InputState::Pending));
assert_eq!(voice_holds(&runtime), 1);
// The sender sees the agent working while it waits, repeated until the
// transcript comes.
assert_eq!(runtime.voice_working_sent, [(9, true)]);
runtime.voice_tick(super::voice::now_ms()).await;
assert_eq!(runtime.voice_working_sent.len(), 1, "not yet due again");
runtime.voice_working_due = Some(tokio::time::Instant::now());
runtime.voice_tick(super::voice::now_ms()).await;
assert_eq!(runtime.voice_working_sent, [(9, true), (9, true)]);
tokio::time::sleep(Duration::from_millis(50)).await;
assert_eq!(sends(&harness), 0, "held for the transcript");
runtime
.on_message(&voice_message(300, 4_000, Some("Ship the fix today.")))
.await
.unwrap();
until(|| sends(&harness) == 1).await;
let input = &received(&harness)[0];
assert_eq!(input.id.as_str(), id);
assert_eq!(
input.content.text,
"Voice message (0:04), transcribed by Inline:\n\nShip the fix today."
);
assert!(input.content.attachments.is_empty(), "no audio download");
assert_eq!(voice_holds(&runtime), 0);
// A replayed edit is the same message.
runtime
.on_message(&voice_message(300, 4_000, Some("Ship the fix today.")))
.await
.unwrap();
tokio::time::sleep(Duration::from_millis(50)).await;
assert_eq!(sends(&harness), 1);
cleanup.tasks.push(runtime.works.remove(&9).unwrap().task);
}

#[tokio::test]
async fn past_its_deadline_a_voice_message_goes_out_with_its_audio_and_a_late_transcript_follows() {
let (mut runtime, harness, mut cleanup) = fixture(false);
runtime.agent.names.remember(1, "Owner".into()).await;
let id = input_id(2, 9, 301, "direct");
runtime
.on_message(&voice_message(301, 42_000, None))
.await
.unwrap();
// Not yet due: nothing goes out.
runtime.voice_tick(super::voice::now_ms()).await;
tokio::time::sleep(Duration::from_millis(50)).await;
assert_eq!(sends(&harness), 0);
runtime.voice_tick(after_deadlines()).await;
until(|| sends(&harness) == 1).await;
let input = &received(&harness)[0];
assert_eq!(input.id.as_str(), id);
assert!(
input.content.text.starts_with(
"Review the attached voice message (0:42). Inline hasn't transcribed it yet"
),
"{}",
input.content.text
);
assert_eq!(input.content.attachments.len(), 1);
assert_eq!(
input.content.attachments[0].kind,
inline_agent_bridge::InputAttachmentKind::Audio
);
// The hold stays for a late transcript.
assert!(runtime.agent.ledger.voice_holds().unwrap()[0].delivered_audio);
runtime
.on_message(&voice_message(301, 42_000, Some("Late words.")))
.await
.unwrap();
until(|| sends(&harness) == 2).await;
let follow_up = &received(&harness)[1];
assert_eq!(
follow_up.id.as_str(),
input_id(2, 9, 301, "voice-transcript")
);
assert_eq!(
follow_up.content.text,
"Transcript of the voice message you received earlier (message 301), transcribed by Inline:\n\nLate words."
);
assert!(follow_up.content.attachments.is_empty());
assert_eq!(voice_holds(&runtime), 0);
runtime
.on_message(&voice_message(301, 42_000, Some("Late words.")))
.await
.unwrap();
tokio::time::sleep(Duration::from_millis(50)).await;
assert_eq!(sends(&harness), 2, "the follow-up goes out once");
cleanup.tasks.push(runtime.works.remove(&9).unwrap().task);
}

#[tokio::test]
async fn a_held_voice_message_survives_a_restart_and_is_never_reported_lost() {
let (mut runtime, harness, mut cleanup) = fixture(false);
runtime.agent.names.remember(1, "Owner".into()).await;
let found = input_id(2, 9, 302, "direct");
let waiting = input_id(2, 9, 303, "direct");
for message_id in [302, 303] {
runtime
.on_message(&voice_message(message_id, 8_000, None))
.await
.unwrap();
}
cleanup.tasks.push(runtime.works.remove(&9).unwrap().task);
drop(runtime);
let mut restarted = super::faults::runtime(&mut cleanup, harness.clone());
// Inline has the first transcript by now, not the second.
restarted.voice_lookups.insert(
(9, 302),
proto::Message {
id: 302,
message: Some("Written while you were away.".into()),
edit_date: Some(2),
..Default::default()
},
);
restarted.voice_lookups.insert(
(9, 303),
proto::Message {
id: 303,
..Default::default()
},
);
restarted.recover().await;
until(|| sends(&harness) == 1).await;
let input = &received(&harness)[0];
assert_eq!(input.id.as_str(), found);
assert_eq!(
input.content.text,
"Voice message (0:08), transcribed by Inline:\n\nWritten while you were away."
);
// The other keeps waiting for its original deadline: not "restarted
// before this ran".
assert_eq!(state(&restarted, &waiting), Some(InputState::Pending));
assert_eq!(voice_holds(&restarted), 1);
restarted.voice_tick(after_deadlines()).await;
until(|| sends(&harness) == 2).await;
assert_eq!(received(&harness)[1].id.as_str(), waiting);
assert_eq!(received(&harness)[1].content.attachments.len(), 1);
cleanup.tasks.push(restarted.works.remove(&9).unwrap().task);
}

#[tokio::test]
async fn a_restart_past_the_deadline_sends_the_audio_without_asking_again() {
let (mut runtime, harness, mut cleanup) = fixture(false);
runtime.agent.names.remember(1, "Owner".into()).await;
runtime
.on_message(&voice_message(304, 8_000, None))
.await
.unwrap();
// The deadline passed while the bridge was down.
{
let mut hold = runtime.agent.ledger.voice_holds().unwrap().remove(0);
runtime
.agent
.ledger
.remove_voice_hold(&hold.input_id)
.unwrap();
hold.deadline_at_ms = 1;
runtime.agent.ledger.hold_voice(&hold).unwrap();
}
cleanup.tasks.push(runtime.works.remove(&9).unwrap().task);
drop(runtime);
let mut restarted = super::faults::runtime(&mut cleanup, harness.clone());
restarted.recover().await;
until(|| sends(&harness) == 1).await;
let input = &received(&harness)[0];
assert_eq!(input.id.as_str(), input_id(2, 9, 304, "direct"));
assert_eq!(input.content.attachments.len(), 1);
assert!(
restarted.agent.ledger.voice_holds().unwrap()[0].delivered_audio,
"a late transcript still follows"
);
cleanup.tasks.push(restarted.works.remove(&9).unwrap().task);
}

#[tokio::test]
async fn voice_too_long_to_transcribe_goes_out_with_its_audio_at_once() {
let (mut runtime, harness, mut cleanup) = fixture(false);
runtime.agent.names.remember(1, "Owner".into()).await;
runtime
.on_message(&voice_message(305, 601_000, None))
.await
.unwrap();
until(|| sends(&harness) == 1).await;
let input = &received(&harness)[0];
assert_eq!(input.content.text, "Review the attached voice message.");
assert_eq!(input.content.attachments.len(), 1);
assert_eq!(voice_holds(&runtime), 0);
cleanup.tasks.push(runtime.works.remove(&9).unwrap().task);
}

#[tokio::test]
async fn voice_with_its_transcript_goes_out_at_once_without_its_audio() {
let (mut runtime, harness, mut cleanup) = fixture(false);
runtime.agent.names.remember(1, "Owner".into()).await;
runtime
.on_message(&voice_message(306, 12_000, Some("Already transcribed.")))
.await
.unwrap();
until(|| sends(&harness) == 1).await;
let input = &received(&harness)[0];
assert_eq!(
input.content.text,
"Voice message (0:12), transcribed by Inline:\n\nAlready transcribed."
);
assert!(input.content.attachments.is_empty());
assert_eq!(voice_holds(&runtime), 0);
cleanup.tasks.push(runtime.works.remove(&9).unwrap().task);
}

#[tokio::test]
async fn stop_drops_a_held_voice_message() {
let (mut runtime, harness, mut cleanup) = fixture(false);
runtime.agent.names.remember(1, "Owner".into()).await;
let id = input_id(2, 9, 307, "direct");
runtime
.on_message(&voice_message(307, 4_000, None))
.await
.unwrap();
assert_eq!(voice_holds(&runtime), 1);
let stop = MessageRecord {
chat_id: InlineId::new(9),
message_id: InlineId::new(308),
sender_id: InlineId::new(1),
timestamp: 1,
is_outgoing: false,
reply_to_message_id: None,
transaction: None,
content: inline_client::MessageContent::Text {
text: "/stop".into(),
},
metadata: inline_client::MessageMetadata {
sender_is_bot: Some(false),
entities: vec![inline_client::MessageEntityRecord {
kind: "TYPE_BOT_COMMAND".into(),
offset: 0,
length: 5,
user_id: None,
agent_id: None,
group_id: None,
chat_id: None,
value: None,
}],
..Default::default()
},
};
runtime.on_message(&stop).await.unwrap();
assert_eq!(state(&runtime, &id), Some(InputState::Discarded));
assert_eq!(voice_holds(&runtime), 0);
assert_eq!(runtime.voice_working_sent.last(), Some(&(9, false)));
// Neither the transcript nor the deadline brings it back.
runtime
.on_message(&voice_message(307, 4_000, Some("Never mind.")))
.await
.unwrap();
runtime.voice_tick(after_deadlines()).await;
tokio::time::sleep(Duration::from_millis(50)).await;
assert_eq!(sends(&harness), 0);
cleanup.tasks.push(runtime.works.remove(&9).unwrap().task);
}

#[tokio::test]
async fn the_apps_stop_drops_held_voice_only_for_someone_who_may_stop() {
let (mut runtime, _harness, mut cleanup) = fixture(false);
runtime.agent.names.remember(1, "Owner".into()).await;
runtime
.on_message(&voice_message(309, 4_000, None))
.await
.unwrap();
until(|| runtime.works[&9].agent_session_id() == Some(1)).await;
let stop = |actor: i64| ClientEvent::AgentSessionControlRequested {
interaction_id: InlineId::new(1),
chat_id: InlineId::new(9),
agent_session_id: InlineId::new(1),
actor_user_id: InlineId::new(actor),
op: proto::AgentSessionControl::Stop as i32,
};
// Someone without access to this agent.
tokio::time::timeout(Duration::from_secs(5), runtime.handle(&stop(5)))
.await
.expect("handled")
.unwrap();
assert_eq!(voice_holds(&runtime), 1);
tokio::time::timeout(Duration::from_secs(5), runtime.handle(&stop(1)))
.await
.expect("handled")
.unwrap();
assert_eq!(voice_holds(&runtime), 0);
assert_eq!(
state(&runtime, &input_id(2, 9, 309, "direct")),
Some(InputState::Discarded)
);
cleanup.tasks.push(runtime.works.remove(&9).unwrap().task);
}
Loading
Loading