fix #14740: attribute teams helper processes, queue end-of-call prompts
Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
This commit is contained in:
+126
-49
@@ -5,6 +5,7 @@
|
||||
//! the end-of-call prompt. Everything here is independent of windows and
|
||||
//! events; the command layer turns results into UI updates.
|
||||
|
||||
use std::collections::VecDeque;
|
||||
use std::fs;
|
||||
use std::io::ErrorKind;
|
||||
use std::path::{Path, PathBuf};
|
||||
@@ -82,12 +83,18 @@ impl CallBrief {
|
||||
}
|
||||
}
|
||||
|
||||
/// The call the end-of-call prompt shows, and how many wait behind it.
|
||||
#[derive(Serialize, Clone, Debug, PartialEq)]
|
||||
pub struct PendingPrompt {
|
||||
#[serde(flatten)]
|
||||
pub brief: CallBrief,
|
||||
pub more_waiting: usize,
|
||||
}
|
||||
|
||||
/// A recording that was just stopped or discarded.
|
||||
#[derive(Debug)]
|
||||
pub struct Ended {
|
||||
pub call_id: CallId,
|
||||
/// Started by call detection rather than by hand.
|
||||
pub detected: bool,
|
||||
/// Whether stopping (finalizing the files and the row) or discarding
|
||||
/// worked.
|
||||
pub result: Result<(), String>,
|
||||
@@ -109,9 +116,10 @@ pub struct AppState {
|
||||
recordings_dir: PathBuf,
|
||||
current: Mutex<Option<CurrentRecording>>,
|
||||
detector: Mutex<CallDetector>,
|
||||
/// The last stopped call the end-of-call prompt asks about, until it is
|
||||
/// decided (discarded or sent to transcription).
|
||||
pending_prompt: Mutex<Option<CallId>>,
|
||||
/// Stopped calls awaiting the end-of-call prompt's decision, oldest
|
||||
/// first. A call leaves the queue when it is decided (discarded or sent
|
||||
/// to transcription).
|
||||
pending_prompts: Mutex<VecDeque<CallId>>,
|
||||
/// Set while a recording starts or the devices are tested, so neither
|
||||
/// runs twice and `current` is never locked for longer than a moment.
|
||||
devices_busy: AtomicBool,
|
||||
@@ -143,7 +151,7 @@ impl AppState {
|
||||
recordings_dir,
|
||||
current: Mutex::new(None),
|
||||
detector: Mutex::new(detector),
|
||||
pending_prompt: Mutex::new(None),
|
||||
pending_prompts: Mutex::new(VecDeque::new()),
|
||||
devices_busy: AtomicBool::new(false),
|
||||
}
|
||||
}
|
||||
@@ -303,7 +311,6 @@ impl AppState {
|
||||
});
|
||||
Some(Ended {
|
||||
call_id: id,
|
||||
detected: cur.detected,
|
||||
result,
|
||||
})
|
||||
}
|
||||
@@ -320,33 +327,51 @@ impl AppState {
|
||||
.map_err(|e| format!("Gespräch konnte nicht gelöscht werden: {e}"));
|
||||
Some(Ended {
|
||||
call_id: id,
|
||||
detected: cur.detected,
|
||||
result,
|
||||
})
|
||||
}
|
||||
|
||||
/// Remembers `id` as the call the end-of-call prompt asks about.
|
||||
pub fn set_pending_prompt(&self, id: CallId) {
|
||||
*lock(&self.pending_prompt) = Some(id);
|
||||
/// Queues `id` for the end-of-call prompt (once).
|
||||
pub fn queue_prompt(&self, id: CallId) {
|
||||
let mut queue = lock(&self.pending_prompts);
|
||||
if !queue.contains(&id) {
|
||||
queue.push_back(id);
|
||||
}
|
||||
}
|
||||
|
||||
/// The call the end-of-call prompt asks about, if it still exists.
|
||||
pub fn pending_prompt(&self) -> Option<CallBrief> {
|
||||
let mut pending = lock(&self.pending_prompt);
|
||||
let id = (*pending)?;
|
||||
match self.store.get_call(id) {
|
||||
Ok(Some(call)) => Some(CallBrief::of(&call)),
|
||||
Ok(None) => {
|
||||
*pending = None;
|
||||
None
|
||||
}
|
||||
Err(e) => {
|
||||
log::error!("could not load call {id} for the prompt: {e}");
|
||||
None
|
||||
/// The oldest queued call that still exists, with the number of calls
|
||||
/// queued behind it. Calls deleted elsewhere drop out of the queue.
|
||||
pub fn pending_prompt(&self) -> Option<PendingPrompt> {
|
||||
let mut queue = lock(&self.pending_prompts);
|
||||
loop {
|
||||
let id = *queue.front()?;
|
||||
match self.store.get_call(id) {
|
||||
Ok(Some(call)) => {
|
||||
return Some(PendingPrompt {
|
||||
brief: CallBrief::of(&call),
|
||||
more_waiting: queue.len() - 1,
|
||||
})
|
||||
}
|
||||
Ok(None) => {
|
||||
queue.pop_front();
|
||||
}
|
||||
Err(e) => {
|
||||
log::error!("could not load call {id} for the prompt: {e}");
|
||||
return None;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// Takes a decided call out of the prompt queue. Returns how many calls
|
||||
/// are still queued, or `None` if `id` was not queued.
|
||||
pub fn remove_queued_prompt(&self, id: CallId) -> Option<usize> {
|
||||
let mut queue = lock(&self.pending_prompts);
|
||||
let index = queue.iter().position(|&queued| queued == id)?;
|
||||
queue.remove(index);
|
||||
Some(queue.len())
|
||||
}
|
||||
|
||||
pub fn call_brief(&self, id: CallId) -> Result<CallBrief, String> {
|
||||
let call = self
|
||||
.store
|
||||
@@ -356,9 +381,10 @@ impl AppState {
|
||||
Ok(CallBrief::of(&call))
|
||||
}
|
||||
|
||||
/// Deletes a finished call: both audio files and the row. Returns
|
||||
/// whether it was the call the end-of-call prompt asked about.
|
||||
pub fn discard_call(&self, id: CallId) -> Result<bool, String> {
|
||||
/// Deletes a finished call: both audio files and the row, and takes it
|
||||
/// out of the prompt queue. Returns what
|
||||
/// [`AppState::remove_queued_prompt`] returns.
|
||||
pub fn discard_call(&self, id: CallId) -> Result<Option<usize>, String> {
|
||||
if lock(&self.current)
|
||||
.as_ref()
|
||||
.is_some_and(|cur| cur.call_id == id)
|
||||
@@ -385,12 +411,7 @@ impl AppState {
|
||||
self.store
|
||||
.delete_call(id)
|
||||
.map_err(|e| format!("Gespräch konnte nicht gelöscht werden: {e}"))?;
|
||||
let mut pending = lock(&self.pending_prompt);
|
||||
let was_pending = *pending == Some(id);
|
||||
if was_pending {
|
||||
*pending = None;
|
||||
}
|
||||
Ok(was_pending)
|
||||
Ok(self.remove_queued_prompt(id))
|
||||
}
|
||||
|
||||
/// Records both configured devices into a temporary directory for five
|
||||
@@ -486,10 +507,11 @@ mod tests {
|
||||
Local.with_ymd_and_hms(2026, 11, 5, h, m, s).unwrap()
|
||||
}
|
||||
|
||||
/// A finished call from 10:14:00 to 10:46:30 with both track files.
|
||||
fn finished_call(f: &Fixture) -> (CallId, PathBuf, PathBuf) {
|
||||
let mic = f.dir.path().join("mic.wav");
|
||||
let out = f.dir.path().join("out.wav");
|
||||
/// A finished call from 10:14:00 to 10:46:30 with both track files
|
||||
/// (`<name>-mic.wav`, `<name>-out.wav`).
|
||||
fn finished_call(f: &Fixture, name: &str) -> (CallId, PathBuf, PathBuf) {
|
||||
let mic = f.dir.path().join(format!("{name}-mic.wav"));
|
||||
let out = f.dir.path().join(format!("{name}-out.wav"));
|
||||
fs::write(&mic, b"mic").unwrap();
|
||||
fs::write(&out, b"out").unwrap();
|
||||
let store = &f.state.store;
|
||||
@@ -503,7 +525,7 @@ mod tests {
|
||||
#[test]
|
||||
fn call_brief_has_app_times_and_duration() {
|
||||
let f = fixture();
|
||||
let (id, _, _) = finished_call(&f);
|
||||
let (id, _, _) = finished_call(&f, "a");
|
||||
assert_eq!(
|
||||
f.state.call_brief(id).unwrap(),
|
||||
CallBrief {
|
||||
@@ -517,25 +539,80 @@ mod tests {
|
||||
assert!(f.state.call_brief(id + 1).is_err());
|
||||
}
|
||||
|
||||
fn queued(f: &Fixture) -> Option<(CallId, usize)> {
|
||||
f.state
|
||||
.pending_prompt()
|
||||
.map(|prompt| (prompt.brief.id, prompt.more_waiting))
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn pending_prompt_shows_the_call_until_it_is_gone() {
|
||||
let f = fixture();
|
||||
assert_eq!(f.state.pending_prompt(), None);
|
||||
let (id, _, _) = finished_call(&f);
|
||||
f.state.set_pending_prompt(id);
|
||||
assert_eq!(f.state.pending_prompt().map(|b| b.id), Some(id));
|
||||
let (id, _, _) = finished_call(&f, "a");
|
||||
f.state.queue_prompt(id);
|
||||
assert_eq!(queued(&f), Some((id, 0)));
|
||||
assert_eq!(
|
||||
f.state.pending_prompt().map(|p| p.brief),
|
||||
Some(f.state.call_brief(id).unwrap())
|
||||
);
|
||||
|
||||
f.state.store.delete_call(id).unwrap();
|
||||
assert_eq!(f.state.pending_prompt(), None);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn prompts_queue_up_in_order() {
|
||||
let f = fixture();
|
||||
let (first, _, _) = finished_call(&f, "a");
|
||||
let (second, _, _) = finished_call(&f, "b");
|
||||
f.state.queue_prompt(first);
|
||||
f.state.queue_prompt(second);
|
||||
f.state.queue_prompt(first); // queued once only
|
||||
assert_eq!(queued(&f), Some((first, 1)));
|
||||
|
||||
// Deciding the first shows the second.
|
||||
assert_eq!(f.state.discard_call(first), Ok(Some(1)));
|
||||
assert_eq!(queued(&f), Some((second, 0)));
|
||||
assert_eq!(f.state.discard_call(second), Ok(Some(0)));
|
||||
assert_eq!(queued(&f), None);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn a_queued_call_is_decided_by_id_not_by_position() {
|
||||
let f = fixture();
|
||||
let (a, _, _) = finished_call(&f, "a");
|
||||
let (b, _, _) = finished_call(&f, "b");
|
||||
let (c, _, _) = finished_call(&f, "c");
|
||||
for id in [a, b, c] {
|
||||
f.state.queue_prompt(id);
|
||||
}
|
||||
|
||||
assert_eq!(f.state.discard_call(b), Ok(Some(2)));
|
||||
assert_eq!(queued(&f), Some((a, 1)));
|
||||
assert_eq!(f.state.remove_queued_prompt(a), Some(1));
|
||||
assert_eq!(queued(&f), Some((c, 0)));
|
||||
assert_eq!(f.state.remove_queued_prompt(a), None);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn a_vanished_call_is_skipped() {
|
||||
let f = fixture();
|
||||
let (gone, _, _) = finished_call(&f, "a");
|
||||
let (next, _, _) = finished_call(&f, "b");
|
||||
f.state.queue_prompt(gone);
|
||||
f.state.queue_prompt(next);
|
||||
f.state.store.delete_call(gone).unwrap();
|
||||
assert_eq!(queued(&f), Some((next, 0)));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn discard_call_deletes_audio_and_row_and_clears_the_prompt() {
|
||||
let f = fixture();
|
||||
let (id, mic, out) = finished_call(&f);
|
||||
f.state.set_pending_prompt(id);
|
||||
let (id, mic, out) = finished_call(&f, "a");
|
||||
f.state.queue_prompt(id);
|
||||
|
||||
assert_eq!(f.state.discard_call(id), Ok(true));
|
||||
assert_eq!(f.state.discard_call(id), Ok(Some(0)));
|
||||
assert!(!mic.exists());
|
||||
assert!(!out.exists());
|
||||
assert_eq!(f.state.store.get_call(id).unwrap(), None);
|
||||
@@ -543,10 +620,10 @@ mod tests {
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn discard_call_of_another_call_keeps_the_prompt() {
|
||||
fn discard_call_of_an_unqueued_call_keeps_the_prompt() {
|
||||
let f = fixture();
|
||||
let (pending, _, _) = finished_call(&f);
|
||||
f.state.set_pending_prompt(pending);
|
||||
let (pending, _, _) = finished_call(&f, "a");
|
||||
f.state.queue_prompt(pending);
|
||||
let other = f
|
||||
.state
|
||||
.store
|
||||
@@ -554,8 +631,8 @@ mod tests {
|
||||
.unwrap();
|
||||
|
||||
// Missing track files are not an error.
|
||||
assert_eq!(f.state.discard_call(other), Ok(false));
|
||||
assert_eq!(f.state.discard_call(other), Ok(None));
|
||||
assert_eq!(f.state.store.get_call(other).unwrap(), None);
|
||||
assert_eq!(f.state.pending_prompt().map(|b| b.id), Some(pending));
|
||||
assert_eq!(queued(&f), Some((pending, 0)));
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user