diff --git a/crates/audio/src/process_tree.rs b/crates/audio/src/process_tree.rs index 54daafa..799dc7a 100644 --- a/crates/audio/src/process_tree.rs +++ b/crates/audio/src/process_tree.rs @@ -5,7 +5,13 @@ //! process *tree*, so the recorder needs the top-most process of the app: //! walk up the parent chain while the parent runs the same executable. //! -//! The walk is a pure function over a process table so it can be tested +//! Some apps capture the mic in a helper with a different exe: new Teams +//! (since Jan 2026) in `ms-teams_modulehost.exe`, earlier in +//! `msedgewebview2.exe` children. [`attribute`] maps those helpers to their +//! Teams ancestor, and only those: no other exe boundary is ever crossed, so +//! e.g. an app launched from a browser is never attributed to the browser. +//! +//! The walks are pure functions over a process table so they can be tested //! without a live system; Windows builds the table from a ToolHelp snapshot. // Only the Windows session enumeration calls into this module. @@ -17,9 +23,64 @@ use std::collections::{HashMap, HashSet}; /// long chain) can never make the walk run away. const MAX_STEPS: usize = 64; +/// Helper processes that may hold the mic on behalf of Teams. +const TEAMS_HELPERS: [&str; 2] = ["ms-teams_modulehost.exe", "msedgewebview2.exe"]; +/// The Teams executables (new and classic) the helpers are attributed to. +const TEAMS_HOSTS: [&str; 2] = ["ms-teams.exe", "teams.exe"]; + /// pid -> (parent pid, executable file name). pub(crate) type ProcessTable = HashMap; +fn is_one_of(exe: &str, names: &[&str]) -> bool { + names.iter().any(|name| exe.eq_ignore_ascii_case(name)) +} + +/// The app a mic session belongs to: `(exe, root_pid)`. +/// +/// Normally that is the session process's own `exe` and its same-exe +/// [`root_pid`]. A Teams helper (`exe` in [`TEAMS_HELPERS`]) is attributed to +/// the nearest Teams ancestor instead, reached through helpers only, so +/// process loopback of that ancestor's tree covers the helper too. The walk +/// trusts a parent only if it was created strictly before its child +/// (`created` returns a process's creation time); otherwise the parent pid +/// was reused by an unrelated process, or the time is unknown, and the walk +/// stops. Without a Teams ancestor the session is reported as is. +pub(crate) fn attribute( + table: &ProcessTable, + pid: u32, + exe: &str, + created: impl Fn(u32) -> Option, +) -> (String, u32) { + let as_is = || (exe.to_string(), root_pid(table, pid)); + if !is_one_of(exe, &TEAMS_HELPERS) { + return as_is(); + } + let mut child = pid; + let mut seen = HashSet::from([pid]); + for _ in 0..MAX_STEPS { + let Some(&(parent, _)) = table.get(&child) else { + break; + }; + let Some((_, parent_exe)) = table.get(&parent) else { + break; + }; + let (Some(child_created), Some(parent_created)) = (created(child), created(parent)) else { + break; + }; + if parent_created >= child_created || !seen.insert(parent) { + break; + } + if is_one_of(parent_exe, &TEAMS_HOSTS) { + return (parent_exe.clone(), root_pid(table, parent)); + } + if !is_one_of(parent_exe, &TEAMS_HELPERS) { + break; + } + child = parent; + } + as_is() +} + /// The top-most ancestor of `pid` reachable through parents that run the /// same executable (compared case-insensitively). Returns `pid` itself if it /// is unknown or its parent runs something else. Cycles (possible after pid @@ -48,6 +109,154 @@ pub(crate) fn root_pid(table: &ProcessTable, pid: u32) -> u32 { mod tests { use super::*; + /// A process table plus creation times, in "pid starts" order: each + /// process is created at tick == its position in `entries`, unless + /// overridden. + struct World { + table: ProcessTable, + created: HashMap, + } + + impl World { + fn new(entries: &[(u32, u32, &str)]) -> Self { + Self { + table: table(entries), + created: entries + .iter() + .enumerate() + .map(|(tick, &(pid, _, _))| (pid, tick as u64)) + .collect(), + } + } + + fn attribute(&self, pid: u32) -> (String, u32) { + let exe = self.table[&pid].1.clone(); + attribute(&self.table, pid, &exe, |pid| { + self.created.get(&pid).copied() + }) + } + } + + #[test] + fn teams_modulehost_is_attributed_to_its_teams_root() { + let w = World::new(&[ + (4, 0, "explorer.exe"), + (100, 4, "ms-teams.exe"), + (110, 100, "ms-teams.exe"), + (200, 110, "ms-teams_modulehost.exe"), + ]); + assert_eq!(w.attribute(200), ("ms-teams.exe".to_string(), 100)); + } + + #[test] + fn webview_under_modulehost_under_teams_is_attributed_to_teams() { + let w = World::new(&[ + (4, 0, "explorer.exe"), + (100, 4, "ms-teams.exe"), + (200, 100, "ms-teams_modulehost.exe"), + (300, 200, "msedgewebview2.exe"), + (310, 300, "msedgewebview2.exe"), + ]); + assert_eq!(w.attribute(310), ("ms-teams.exe".to_string(), 100)); + } + + #[test] + fn classic_teams_webview_is_attributed_to_teams() { + let w = World::new(&[ + (4, 0, "explorer.exe"), + (100, 4, "Teams.exe"), + (300, 100, "msedgewebview2.exe"), + ]); + assert_eq!(w.attribute(300), ("Teams.exe".to_string(), 100)); + } + + #[test] + fn webview_under_outlook_is_unchanged() { + let w = World::new(&[ + (4, 0, "explorer.exe"), + (100, 4, "outlook.exe"), + (300, 100, "msedgewebview2.exe"), + (310, 300, "msedgewebview2.exe"), + ]); + assert_eq!(w.attribute(310), ("msedgewebview2.exe".to_string(), 300)); + } + + #[test] + fn a_parent_created_after_the_child_is_a_reused_pid() { + let mut w = World::new(&[ + (4, 0, "explorer.exe"), + (100, 4, "ms-teams.exe"), + (200, 100, "ms-teams_modulehost.exe"), + ]); + // The real parent exited and pid 100 went to a new Teams process. + w.created.insert(100, 50); + w.created.insert(200, 10); + assert_eq!( + w.attribute(200), + ("ms-teams_modulehost.exe".to_string(), 200) + ); + + // Same creation time is not "earlier" either. + w.created.insert(100, 10); + assert_eq!( + w.attribute(200), + ("ms-teams_modulehost.exe".to_string(), 200) + ); + } + + #[test] + fn an_unknown_creation_time_stops_the_walk() { + let mut w = World::new(&[ + (4, 0, "explorer.exe"), + (100, 4, "ms-teams.exe"), + (200, 100, "ms-teams_modulehost.exe"), + ]); + w.created.remove(&100); + assert_eq!( + w.attribute(200), + ("ms-teams_modulehost.exe".to_string(), 200) + ); + } + + #[test] + fn other_exes_never_cross_an_exe_boundary() { + let w = World::new(&[ + (4, 0, "explorer.exe"), + (100, 4, "chrome.exe"), + (200, 100, "zoom.exe"), + (300, 4, "ms-teams.exe"), + (400, 300, "microsip.exe"), + ]); + assert_eq!(w.attribute(200), ("zoom.exe".to_string(), 200)); + assert_eq!(w.attribute(400), ("microsip.exe".to_string(), 400)); + } + + #[test] + fn the_helper_walk_does_not_pass_through_other_exes() { + // A webview launched by some app that was itself started by Teams. + let w = World::new(&[ + (4, 0, "explorer.exe"), + (100, 4, "ms-teams.exe"), + (200, 100, "someapp.exe"), + (300, 200, "msedgewebview2.exe"), + ]); + assert_eq!(w.attribute(300), ("msedgewebview2.exe".to_string(), 300)); + } + + #[test] + fn the_helper_walk_survives_cycles() { + let w = World::new(&[ + (200, 300, "ms-teams_modulehost.exe"), + (300, 200, "msedgewebview2.exe"), + ]); + // Creation order makes 300 newer than 200, so the walk stops; with + // equal times it would still end on the cycle check. + assert_eq!( + w.attribute(200), + ("ms-teams_modulehost.exe".to_string(), 200) + ); + } + fn table(entries: &[(u32, u32, &str)]) -> ProcessTable { entries .iter() diff --git a/crates/audio/src/windows/sessions.rs b/crates/audio/src/windows/sessions.rs index f9e2127..56c8b50 100644 --- a/crates/audio/src/windows/sessions.rs +++ b/crates/audio/src/windows/sessions.rs @@ -5,23 +5,25 @@ //! process (`IAudioSessionControl2::GetProcessId`) and is active while one //! of its streams runs, i.e. while the app actually records the mic. The //! process's exe comes from `QueryFullProcessImageNameW`; its `root_pid` is -//! the top of its same-exe parent chain in a ToolHelp snapshot (see -//! [`crate::process_tree`]), which is the tree application loopback records. +//! the top of its same-exe parent chain in a ToolHelp snapshot, which is the +//! tree application loopback records. Teams helper processes are reported +//! as their Teams ancestor (see [`crate::process_tree`]). use std::path::Path; use recorder_core::detect::MicSession; use wasapi::{deinitialize, initialize_mta, DeviceEnumerator, Direction, SessionState}; use windows::core::PWSTR; -use windows::Win32::Foundation::{CloseHandle, HANDLE}; +use windows::Win32::Foundation::{CloseHandle, FILETIME, HANDLE}; use windows::Win32::System::Diagnostics::ToolHelp::{ CreateToolhelp32Snapshot, Process32FirstW, Process32NextW, PROCESSENTRY32W, TH32CS_SNAPPROCESS, }; use windows::Win32::System::Threading::{ - OpenProcess, QueryFullProcessImageNameW, PROCESS_NAME_WIN32, PROCESS_QUERY_LIMITED_INFORMATION, + GetProcessTimes, OpenProcess, QueryFullProcessImageNameW, PROCESS_NAME_WIN32, + PROCESS_QUERY_LIMITED_INFORMATION, }; -use crate::process_tree::{root_pid, ProcessTable}; +use crate::process_tree::{attribute, ProcessTable}; /// Sessions on every active capture endpoint, one entry per session. /// @@ -52,10 +54,11 @@ fn enumerate() -> Vec { raw.into_iter() .filter_map(|(pid, active)| { let exe = image_name(pid)?; + let (exe, root_pid) = attribute(&table, pid, &exe, creation_time); Some(MicSession { exe, pid, - root_pid: root_pid(&table, pid), + root_pid, active, }) }) @@ -121,6 +124,26 @@ fn image_name(pid: u32) -> Option { } } +/// When `pid` was created (FILETIME ticks), or `None` if the process cannot +/// be opened. +fn creation_time(pid: u32) -> Option { + // SAFETY: plain Win32 calls; the handle is closed below and the + // FILETIMEs outlive the call that fills them. + unsafe { + let handle = OpenProcess(PROCESS_QUERY_LIMITED_INFORMATION, false, pid).ok()?; + let (mut created, mut exited, mut kernel, mut user) = ( + FILETIME::default(), + FILETIME::default(), + FILETIME::default(), + FILETIME::default(), + ); + let result = GetProcessTimes(handle, &mut created, &mut exited, &mut kernel, &mut user); + close(handle); + result.ok()?; + Some((u64::from(created.dwHighDateTime) << 32) | u64::from(created.dwLowDateTime)) + } +} + /// pid -> (parent pid, exe) of every process, from one ToolHelp snapshot. /// Empty if the snapshot fails, which makes every session its own root. fn process_table() -> ProcessTable { diff --git a/src-tauri/src/commands.rs b/src-tauri/src/commands.rs index cd2194f..11125c8 100644 --- a/src-tauri/src/commands.rs +++ b/src-tauri/src/commands.rs @@ -16,7 +16,7 @@ use serde::Serialize; use tauri::{AppHandle, Emitter, Manager, State}; use tauri_plugin_autostart::ManagerExt; -use crate::state::{AppState, CallBrief, DeviceLevels, Ended, RecordingState}; +use crate::state::{AppState, CallBrief, DeviceLevels, Ended, PendingPrompt, RecordingState}; const LEVELS_INTERVAL: Duration = Duration::from_millis(100); @@ -106,7 +106,7 @@ pub async fn discard_current_recording(app: AppHandle) -> CmdResult<()> { /// The call the end-of-call prompt asks about, so the prompt can render even /// if it missed the `call-ended` event. #[tauri::command] -pub fn get_pending_prompt(state: State<'_, AppState>) -> Option { +pub fn get_pending_prompt(state: State<'_, AppState>) -> Option { state.pending_prompt() } @@ -115,12 +115,13 @@ pub fn get_call_brief(state: State<'_, AppState>, id: CallId) -> CmdResult CmdResult<()> { blocking(app, move |app| { - if app.state::().discard_call(id)? { + if app.state::().discard_call(id)? == Some(0) { set_window_visible(app, "prompt", false); } Ok(()) @@ -152,13 +153,13 @@ pub fn start_detected(app: &AppHandle, name: &str, root_pid: u32) -> CmdResult CmdResult<()> { let state = app.state::(); let ended = state.stop_recording(); - if ended.as_ref().is_some_and(|ended| ended.detected) { + if ended.is_some() { state.suppress_detected_call(); } after_stop(app, ended) @@ -173,12 +174,12 @@ pub fn end_detected_call(app: &AppHandle) { } } -/// Throws the current recording away. Never shows the prompt; a discarded -/// detected call is not detected again until the call app releases the mic. +/// Throws the current recording away. Never shows the prompt; detection +/// then leaves the ongoing call alone until the call app releases the mic. pub fn discard(app: &AppHandle) -> CmdResult<()> { let state = app.state::(); let ended = state.discard_current_recording(); - if ended.as_ref().is_some_and(|ended| ended.detected) { + if ended.is_some() { state.suppress_detected_call(); } sync_recorder_window(app); @@ -202,7 +203,7 @@ fn after_stop(app: &AppHandle, ended: Option) -> CmdResult<()> { }; ended.result?; let id = ended.call_id; - app.state::().set_pending_prompt(id); + app.state::().queue_prompt(id); show_and_focus(app, "prompt"); if let Err(e) = app.emit("call-ended", CallEnded { id }) { log::error!("could not emit call-ended: {e}"); diff --git a/src-tauri/src/detection.rs b/src-tauri/src/detection.rs index 842ea9f..3fab8e4 100644 --- a/src-tauri/src/detection.rs +++ b/src-tauri/src/detection.rs @@ -3,7 +3,9 @@ //! kept in [`AppState`]. A detected call is recorded until the detector //! sees it end; then the end-of-call prompt asks what to do with it. //! -//! Only Windows reports microphone sessions; elsewhere the loop runs but +//! Detection keeps running during a manual recording: a call it finds then +//! is already being recorded by hand, so it is only suppressed (see +//! [`decide`]). Only Windows reports microphone sessions; elsewhere the loop runs but //! never detects anything. use std::thread; @@ -46,27 +48,50 @@ pub fn spawn(app: AppHandle) { } } +/// What to do about one detector event. +#[derive(Debug, PartialEq, Eq)] +enum Action { + /// Record the detected call. + Start { + app: String, + root_pid: u32, + }, + /// A manual recording already covers the call: only keep the detector + /// from re-detecting it until the app releases the mic. + Suppress, + /// Stop the detected recording (a manual one keeps running). + EndDetected, + Nothing, +} + +fn decide(event: Option, manual_recording: bool) -> Action { + match event { + Some(DetectorEvent::CallStarted { .. }) if manual_recording => Action::Suppress, + Some(DetectorEvent::CallStarted { app, root_pid, .. }) => Action::Start { app, root_pid }, + Some(DetectorEvent::CallEnded) => Action::EndDetected, + None => Action::Nothing, + } +} + /// One detection step. `last_sessions` is the previous [`describe`] output, /// so the log shows each change of who holds the mic exactly once. fn poll(app: &AppHandle, last_sessions: &mut String) { let state = app.state::(); - // A recording started by hand owns the devices; detection waits. - if state.manual_recording_running() { - return; - } let sessions = recorder_audio::mic_sessions(); let described = describe(&sessions); if described != *last_sessions { log::info!("mic sessions: [{described}]"); *last_sessions = described; } - match state.detector_tick(Instant::now(), &sessions) { - Some(DetectorEvent::CallStarted { - exe, + let event = state.detector_tick(Instant::now(), &sessions); + if let Some(DetectorEvent::CallStarted { exe, root_pid, .. }) = &event { + log::info!("call detected: {exe} (process tree {root_pid})"); + } + match decide(event, state.manual_recording_running()) { + Action::Start { app: name, root_pid, - }) => { - log::info!("call detected: {exe} (process tree {root_pid})"); + } => { if let Err(e) = commands::start_detected(app, &name, root_pid) { log::error!("recording the detected call failed: {e}"); // Otherwise the detector would stay in the call and never @@ -74,11 +99,15 @@ fn poll(app: &AppHandle, last_sessions: &mut String) { state.suppress_detected_call(); } } - Some(DetectorEvent::CallEnded) => { + Action::Suppress => { + log::info!("call is already recorded manually"); + state.suppress_detected_call(); + } + Action::EndDetected => { log::info!("call ended"); commands::end_detected_call(app); } - None => {} + Action::Nothing => {} } } @@ -95,3 +124,92 @@ fn describe(sessions: &[MicSession]) -> String { parts.sort(); parts.join(", ") } + +#[cfg(test)] +mod tests { + use recorder_core::detect::{CallDetector, MicSession}; + + use super::*; + + fn started() -> DetectorEvent { + DetectorEvent::CallStarted { + exe: "ms-teams.exe".into(), + app: "Microsoft Teams".into(), + root_pid: 100, + } + } + + #[test] + fn a_call_found_during_a_manual_recording_is_only_suppressed() { + assert_eq!(decide(Some(started()), true), Action::Suppress); + assert_eq!( + decide(Some(started()), false), + Action::Start { + app: "Microsoft Teams".into(), + root_pid: 100 + } + ); + assert_eq!( + decide(Some(DetectorEvent::CallEnded), true), + Action::EndDetected + ); + assert_eq!(decide(None, true), Action::Nothing); + } + + /// One loop step against the real detector, without devices: returns + /// whether a detected recording would start. + fn step(detector: &mut CallDetector, now: Instant, teams_active: bool, manual: bool) -> bool { + let teams = MicSession { + exe: "ms-teams.exe".into(), + pid: 200, + root_pid: 100, + active: teams_active, + }; + match decide(detector.tick(now, &[teams]), manual) { + Action::Start { .. } => true, + Action::Suppress => { + detector.manual_stop(); + false + } + Action::EndDetected | Action::Nothing => false, + } + } + + /// A manual recording made during a call: started by hand while the + /// detector is still pending, stopped by hand mid-call. No automatic + /// recording may follow until Teams releases the mic. + #[test] + fn manual_stop_mid_call_does_not_start_an_automatic_recording() { + let mut detector = CallDetector::new(detector_config(&Settings::default())); + let t0 = Instant::now(); + let at = |secs| t0 + Duration::from_secs(secs); + let mut started = Vec::new(); + + if step(&mut detector, at(0), true, false) { + started.push(0); + } + // Recording started by hand while pending; detection keeps ticking. + for secs in 1..=10 { + if step(&mut detector, at(secs), true, true) { + started.push(secs); + } + } + // ■ Stopp on the manual recording. + detector.manual_stop(); + for secs in 11..=30 { + if step(&mut detector, at(secs), true, false) { + started.push(secs); + } + } + assert!(started.is_empty(), "auto recording started at {started:?}"); + + // Once Teams releases the mic, the next call is detected again. + step(&mut detector, at(31), false, false); + for secs in 32..=36 { + if step(&mut detector, at(secs), true, false) { + started.push(secs); + } + } + assert_eq!(started, vec![35]); + } +} diff --git a/src-tauri/src/state.rs b/src-tauri/src/state.rs index f6456f0..5808320 100644 --- a/src-tauri/src/state.rs +++ b/src-tauri/src/state.rs @@ -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>, detector: Mutex, - /// The last stopped call the end-of-call prompt asks about, until it is - /// decided (discarded or sent to transcription). - pending_prompt: Mutex>, + /// 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>, /// 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 { - 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 { + 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 { + 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 { 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 { + /// 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, 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 + /// (`-mic.wav`, `-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))); } } diff --git a/ui/src/lib/api.ts b/ui/src/lib/api.ts index ebd60ec..4946f21 100644 --- a/ui/src/lib/api.ts +++ b/ui/src/lib/api.ts @@ -49,6 +49,11 @@ export interface CallBrief { duration_secs: number } +/** The call the end-of-call prompt shows, and how many wait behind it. */ +export interface PendingPrompt extends CallBrief { + more_waiting: number +} + export interface Levels { mic: number out: number @@ -66,7 +71,7 @@ export const getRecordingState = () => invoke('get_recording_sta export const startManualRecording = () => invoke('start_manual_recording') export const stopRecording = () => invoke('stop_recording') export const discardCurrentRecording = () => invoke('discard_current_recording') -export const getPendingPrompt = () => invoke('get_pending_prompt') +export const getPendingPrompt = () => invoke('get_pending_prompt') export const getCallBrief = (id: CallId) => invoke('get_call_brief', { id }) export const transcribeCall = (id: CallId) => invoke('transcribe_call', { id }) export const discardCall = (id: CallId) => invoke('discard_call', { id }) diff --git a/ui/src/routes/Prompt.svelte b/ui/src/routes/Prompt.svelte index 83b214e..b4f3258 100644 --- a/ui/src/routes/Prompt.svelte +++ b/ui/src/routes/Prompt.svelte @@ -3,38 +3,33 @@ import { discardCall, errorText, - getCallBrief, getPendingPrompt, onCallEnded, transcribeCall, - type CallBrief, type CallId, + type PendingPrompt, } from '../lib/api' import { formatMinutes, formatTimeRange } from '../lib/format' - let brief = $state(null) + let brief = $state(null) let busy = $state(false) let error = $state('') - function show(next: CallBrief | null) { + function show(next: PendingPrompt | null) { if (next?.id !== brief?.id) error = '' brief = next } - // The window stays loaded while hidden, so it may have missed the event: - // ask the backend whenever it is shown again. + // The backend queues ended calls; this window always shows the oldest. + // It stays loaded while hidden and may have missed an event, so it also + // asks whenever it is shown again. const refresh = () => getPendingPrompt() .then(show) .catch((err) => (error = errorText(err))) - const load = (id: CallId) => - getCallBrief(id) - .then(show) - .catch((err) => (error = errorText(err))) - onMount(() => { - const unlisten = onCallEnded(load) + const unlisten = onCallEnded(refresh) refresh() window.addEventListener('focus', refresh) return () => { @@ -49,6 +44,9 @@ error = '' try { await action(brief.id) + // Decided: show the next queued call (the backend hides the window + // once none is left). + await refresh() } catch (err) { error = errorText(err) } finally { @@ -61,6 +59,9 @@ {#if brief}
{brief.app} · {formatMinutes(brief.duration_secs)}
{formatTimeRange(brief.started_at, brief.ended_at)}
+ {#if brief.more_waiting > 0} +
Weitere Gespräche warten: {brief.more_waiting}
+ {/if}