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:
hauke
2026-11-05 15:48:55 +01:00
co-authored by Claude Opus 5.5
parent d631252600
commit bac30205d6
7 changed files with 527 additions and 93 deletions
+210 -1
View File
@@ -5,7 +5,13 @@
//! process *tree*, so the recorder needs the top-most process of the app: //! 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. //! 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. //! without a live system; Windows builds the table from a ToolHelp snapshot.
// Only the Windows session enumeration calls into this module. // 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. /// long chain) can never make the walk run away.
const MAX_STEPS: usize = 64; 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). /// pid -> (parent pid, executable file name).
pub(crate) type ProcessTable = HashMap<u32, (u32, String)>; pub(crate) type ProcessTable = HashMap<u32, (u32, String)>;
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<u64>,
) -> (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 /// The top-most ancestor of `pid` reachable through parents that run the
/// same executable (compared case-insensitively). Returns `pid` itself if it /// same executable (compared case-insensitively). Returns `pid` itself if it
/// is unknown or its parent runs something else. Cycles (possible after pid /// 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 { mod tests {
use super::*; 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<u32, u64>,
}
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 { fn table(entries: &[(u32, u32, &str)]) -> ProcessTable {
entries entries
.iter() .iter()
+29 -6
View File
@@ -5,23 +5,25 @@
//! process (`IAudioSessionControl2::GetProcessId`) and is active while one //! process (`IAudioSessionControl2::GetProcessId`) and is active while one
//! of its streams runs, i.e. while the app actually records the mic. The //! of its streams runs, i.e. while the app actually records the mic. The
//! process's exe comes from `QueryFullProcessImageNameW`; its `root_pid` is //! process's exe comes from `QueryFullProcessImageNameW`; its `root_pid` is
//! the top of its same-exe parent chain in a ToolHelp snapshot (see //! the top of its same-exe parent chain in a ToolHelp snapshot, which is the
//! [`crate::process_tree`]), which is the tree application loopback records. //! tree application loopback records. Teams helper processes are reported
//! as their Teams ancestor (see [`crate::process_tree`]).
use std::path::Path; use std::path::Path;
use recorder_core::detect::MicSession; use recorder_core::detect::MicSession;
use wasapi::{deinitialize, initialize_mta, DeviceEnumerator, Direction, SessionState}; use wasapi::{deinitialize, initialize_mta, DeviceEnumerator, Direction, SessionState};
use windows::core::PWSTR; use windows::core::PWSTR;
use windows::Win32::Foundation::{CloseHandle, HANDLE}; use windows::Win32::Foundation::{CloseHandle, FILETIME, HANDLE};
use windows::Win32::System::Diagnostics::ToolHelp::{ use windows::Win32::System::Diagnostics::ToolHelp::{
CreateToolhelp32Snapshot, Process32FirstW, Process32NextW, PROCESSENTRY32W, TH32CS_SNAPPROCESS, CreateToolhelp32Snapshot, Process32FirstW, Process32NextW, PROCESSENTRY32W, TH32CS_SNAPPROCESS,
}; };
use windows::Win32::System::Threading::{ 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. /// Sessions on every active capture endpoint, one entry per session.
/// ///
@@ -52,10 +54,11 @@ fn enumerate() -> Vec<MicSession> {
raw.into_iter() raw.into_iter()
.filter_map(|(pid, active)| { .filter_map(|(pid, active)| {
let exe = image_name(pid)?; let exe = image_name(pid)?;
let (exe, root_pid) = attribute(&table, pid, &exe, creation_time);
Some(MicSession { Some(MicSession {
exe, exe,
pid, pid,
root_pid: root_pid(&table, pid), root_pid,
active, active,
}) })
}) })
@@ -121,6 +124,26 @@ fn image_name(pid: u32) -> Option<String> {
} }
} }
/// When `pid` was created (FILETIME ticks), or `None` if the process cannot
/// be opened.
fn creation_time(pid: u32) -> Option<u64> {
// 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. /// pid -> (parent pid, exe) of every process, from one ToolHelp snapshot.
/// Empty if the snapshot fails, which makes every session its own root. /// Empty if the snapshot fails, which makes every session its own root.
fn process_table() -> ProcessTable { fn process_table() -> ProcessTable {
+13 -12
View File
@@ -16,7 +16,7 @@ use serde::Serialize;
use tauri::{AppHandle, Emitter, Manager, State}; use tauri::{AppHandle, Emitter, Manager, State};
use tauri_plugin_autostart::ManagerExt; 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); 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 /// The call the end-of-call prompt asks about, so the prompt can render even
/// if it missed the `call-ended` event. /// if it missed the `call-ended` event.
#[tauri::command] #[tauri::command]
pub fn get_pending_prompt(state: State<'_, AppState>) -> Option<CallBrief> { pub fn get_pending_prompt(state: State<'_, AppState>) -> Option<PendingPrompt> {
state.pending_prompt() state.pending_prompt()
} }
@@ -115,12 +115,13 @@ pub fn get_call_brief(state: State<'_, AppState>, id: CallId) -> CmdResult<CallB
state.call_brief(id) state.call_brief(id)
} }
/// Deletes a finished call's audio and row; closes the prompt if it asked /// Deletes a finished call's audio and row. If that was the last call the
/// about this call. /// prompt was waiting on, closes the prompt; otherwise the prompt reloads
/// and shows the next one.
#[tauri::command] #[tauri::command]
pub async fn discard_call(app: AppHandle, id: CallId) -> CmdResult<()> { pub async fn discard_call(app: AppHandle, id: CallId) -> CmdResult<()> {
blocking(app, move |app| { blocking(app, move |app| {
if app.state::<AppState>().discard_call(id)? { if app.state::<AppState>().discard_call(id)? == Some(0) {
set_window_visible(app, "prompt", false); set_window_visible(app, "prompt", false);
} }
Ok(()) Ok(())
@@ -152,13 +153,13 @@ pub fn start_detected(app: &AppHandle, name: &str, root_pid: u32) -> CmdResult<C
Ok(call_id) Ok(call_id)
} }
/// Stops the current recording at the user's request (■ Stopp). Stopping /// Stops the current recording at the user's request (■ Stopp), manual or
/// a detected call also keeps detection from restarting it until the call /// detected. Detection then leaves the ongoing call alone until the call
/// app releases the mic. /// app releases the mic.
pub fn stop(app: &AppHandle) -> CmdResult<()> { pub fn stop(app: &AppHandle) -> CmdResult<()> {
let state = app.state::<AppState>(); let state = app.state::<AppState>();
let ended = state.stop_recording(); let ended = state.stop_recording();
if ended.as_ref().is_some_and(|ended| ended.detected) { if ended.is_some() {
state.suppress_detected_call(); state.suppress_detected_call();
} }
after_stop(app, ended) 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 /// Throws the current recording away. Never shows the prompt; detection
/// detected call is not detected again until the call app releases the mic. /// then leaves the ongoing call alone until the call app releases the mic.
pub fn discard(app: &AppHandle) -> CmdResult<()> { pub fn discard(app: &AppHandle) -> CmdResult<()> {
let state = app.state::<AppState>(); let state = app.state::<AppState>();
let ended = state.discard_current_recording(); let ended = state.discard_current_recording();
if ended.as_ref().is_some_and(|ended| ended.detected) { if ended.is_some() {
state.suppress_detected_call(); state.suppress_detected_call();
} }
sync_recorder_window(app); sync_recorder_window(app);
@@ -202,7 +203,7 @@ fn after_stop(app: &AppHandle, ended: Option<Ended>) -> CmdResult<()> {
}; };
ended.result?; ended.result?;
let id = ended.call_id; let id = ended.call_id;
app.state::<AppState>().set_pending_prompt(id); app.state::<AppState>().queue_prompt(id);
show_and_focus(app, "prompt"); show_and_focus(app, "prompt");
if let Err(e) = app.emit("call-ended", CallEnded { id }) { if let Err(e) = app.emit("call-ended", CallEnded { id }) {
log::error!("could not emit call-ended: {e}"); log::error!("could not emit call-ended: {e}");
+130 -12
View File
@@ -3,7 +3,9 @@
//! kept in [`AppState`]. A detected call is recorded until the detector //! 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. //! 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. //! never detects anything.
use std::thread; 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<DetectorEvent>, 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, /// One detection step. `last_sessions` is the previous [`describe`] output,
/// so the log shows each change of who holds the mic exactly once. /// so the log shows each change of who holds the mic exactly once.
fn poll(app: &AppHandle, last_sessions: &mut String) { fn poll(app: &AppHandle, last_sessions: &mut String) {
let state = app.state::<AppState>(); let state = app.state::<AppState>();
// A recording started by hand owns the devices; detection waits.
if state.manual_recording_running() {
return;
}
let sessions = recorder_audio::mic_sessions(); let sessions = recorder_audio::mic_sessions();
let described = describe(&sessions); let described = describe(&sessions);
if described != *last_sessions { if described != *last_sessions {
log::info!("mic sessions: [{described}]"); log::info!("mic sessions: [{described}]");
*last_sessions = described; *last_sessions = described;
} }
match state.detector_tick(Instant::now(), &sessions) { let event = state.detector_tick(Instant::now(), &sessions);
Some(DetectorEvent::CallStarted { if let Some(DetectorEvent::CallStarted { exe, root_pid, .. }) = &event {
exe, log::info!("call detected: {exe} (process tree {root_pid})");
}
match decide(event, state.manual_recording_running()) {
Action::Start {
app: name, app: name,
root_pid, root_pid,
}) => { } => {
log::info!("call detected: {exe} (process tree {root_pid})");
if let Err(e) = commands::start_detected(app, &name, root_pid) { if let Err(e) = commands::start_detected(app, &name, root_pid) {
log::error!("recording the detected call failed: {e}"); log::error!("recording the detected call failed: {e}");
// Otherwise the detector would stay in the call and never // 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(); 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"); log::info!("call ended");
commands::end_detected_call(app); commands::end_detected_call(app);
} }
None => {} Action::Nothing => {}
} }
} }
@@ -95,3 +124,92 @@ fn describe(sessions: &[MicSession]) -> String {
parts.sort(); parts.sort();
parts.join(", ") 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]);
}
}
+126 -49
View File
@@ -5,6 +5,7 @@
//! the end-of-call prompt. Everything here is independent of windows and //! the end-of-call prompt. Everything here is independent of windows and
//! events; the command layer turns results into UI updates. //! events; the command layer turns results into UI updates.
use std::collections::VecDeque;
use std::fs; use std::fs;
use std::io::ErrorKind; use std::io::ErrorKind;
use std::path::{Path, PathBuf}; 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. /// A recording that was just stopped or discarded.
#[derive(Debug)] #[derive(Debug)]
pub struct Ended { pub struct Ended {
pub call_id: CallId, 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 /// Whether stopping (finalizing the files and the row) or discarding
/// worked. /// worked.
pub result: Result<(), String>, pub result: Result<(), String>,
@@ -109,9 +116,10 @@ pub struct AppState {
recordings_dir: PathBuf, recordings_dir: PathBuf,
current: Mutex<Option<CurrentRecording>>, current: Mutex<Option<CurrentRecording>>,
detector: Mutex<CallDetector>, detector: Mutex<CallDetector>,
/// The last stopped call the end-of-call prompt asks about, until it is /// Stopped calls awaiting the end-of-call prompt's decision, oldest
/// decided (discarded or sent to transcription). /// first. A call leaves the queue when it is decided (discarded or sent
pending_prompt: Mutex<Option<CallId>>, /// to transcription).
pending_prompts: Mutex<VecDeque<CallId>>,
/// Set while a recording starts or the devices are tested, so neither /// Set while a recording starts or the devices are tested, so neither
/// runs twice and `current` is never locked for longer than a moment. /// runs twice and `current` is never locked for longer than a moment.
devices_busy: AtomicBool, devices_busy: AtomicBool,
@@ -143,7 +151,7 @@ impl AppState {
recordings_dir, recordings_dir,
current: Mutex::new(None), current: Mutex::new(None),
detector: Mutex::new(detector), detector: Mutex::new(detector),
pending_prompt: Mutex::new(None), pending_prompts: Mutex::new(VecDeque::new()),
devices_busy: AtomicBool::new(false), devices_busy: AtomicBool::new(false),
} }
} }
@@ -303,7 +311,6 @@ impl AppState {
}); });
Some(Ended { Some(Ended {
call_id: id, call_id: id,
detected: cur.detected,
result, result,
}) })
} }
@@ -320,33 +327,51 @@ impl AppState {
.map_err(|e| format!("Gespräch konnte nicht gelöscht werden: {e}")); .map_err(|e| format!("Gespräch konnte nicht gelöscht werden: {e}"));
Some(Ended { Some(Ended {
call_id: id, call_id: id,
detected: cur.detected,
result, result,
}) })
} }
/// Remembers `id` as the call the end-of-call prompt asks about. /// Queues `id` for the end-of-call prompt (once).
pub fn set_pending_prompt(&self, id: CallId) { pub fn queue_prompt(&self, id: CallId) {
*lock(&self.pending_prompt) = Some(id); 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. /// The oldest queued call that still exists, with the number of calls
pub fn pending_prompt(&self) -> Option<CallBrief> { /// queued behind it. Calls deleted elsewhere drop out of the queue.
let mut pending = lock(&self.pending_prompt); pub fn pending_prompt(&self) -> Option<PendingPrompt> {
let id = (*pending)?; let mut queue = lock(&self.pending_prompts);
match self.store.get_call(id) { loop {
Ok(Some(call)) => Some(CallBrief::of(&call)), let id = *queue.front()?;
Ok(None) => { match self.store.get_call(id) {
*pending = None; Ok(Some(call)) => {
None return Some(PendingPrompt {
} brief: CallBrief::of(&call),
Err(e) => { more_waiting: queue.len() - 1,
log::error!("could not load call {id} for the prompt: {e}"); })
None }
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> { pub fn call_brief(&self, id: CallId) -> Result<CallBrief, String> {
let call = self let call = self
.store .store
@@ -356,9 +381,10 @@ impl AppState {
Ok(CallBrief::of(&call)) Ok(CallBrief::of(&call))
} }
/// Deletes a finished call: both audio files and the row. Returns /// Deletes a finished call: both audio files and the row, and takes it
/// whether it was the call the end-of-call prompt asked about. /// out of the prompt queue. Returns what
pub fn discard_call(&self, id: CallId) -> Result<bool, String> { /// [`AppState::remove_queued_prompt`] returns.
pub fn discard_call(&self, id: CallId) -> Result<Option<usize>, String> {
if lock(&self.current) if lock(&self.current)
.as_ref() .as_ref()
.is_some_and(|cur| cur.call_id == id) .is_some_and(|cur| cur.call_id == id)
@@ -385,12 +411,7 @@ impl AppState {
self.store self.store
.delete_call(id) .delete_call(id)
.map_err(|e| format!("Gespräch konnte nicht gelöscht werden: {e}"))?; .map_err(|e| format!("Gespräch konnte nicht gelöscht werden: {e}"))?;
let mut pending = lock(&self.pending_prompt); Ok(self.remove_queued_prompt(id))
let was_pending = *pending == Some(id);
if was_pending {
*pending = None;
}
Ok(was_pending)
} }
/// Records both configured devices into a temporary directory for five /// 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() 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. /// A finished call from 10:14:00 to 10:46:30 with both track files
fn finished_call(f: &Fixture) -> (CallId, PathBuf, PathBuf) { /// (`<name>-mic.wav`, `<name>-out.wav`).
let mic = f.dir.path().join("mic.wav"); fn finished_call(f: &Fixture, name: &str) -> (CallId, PathBuf, PathBuf) {
let out = f.dir.path().join("out.wav"); 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(&mic, b"mic").unwrap();
fs::write(&out, b"out").unwrap(); fs::write(&out, b"out").unwrap();
let store = &f.state.store; let store = &f.state.store;
@@ -503,7 +525,7 @@ mod tests {
#[test] #[test]
fn call_brief_has_app_times_and_duration() { fn call_brief_has_app_times_and_duration() {
let f = fixture(); let f = fixture();
let (id, _, _) = finished_call(&f); let (id, _, _) = finished_call(&f, "a");
assert_eq!( assert_eq!(
f.state.call_brief(id).unwrap(), f.state.call_brief(id).unwrap(),
CallBrief { CallBrief {
@@ -517,25 +539,80 @@ mod tests {
assert!(f.state.call_brief(id + 1).is_err()); 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] #[test]
fn pending_prompt_shows_the_call_until_it_is_gone() { fn pending_prompt_shows_the_call_until_it_is_gone() {
let f = fixture(); let f = fixture();
assert_eq!(f.state.pending_prompt(), None); assert_eq!(f.state.pending_prompt(), None);
let (id, _, _) = finished_call(&f); let (id, _, _) = finished_call(&f, "a");
f.state.set_pending_prompt(id); f.state.queue_prompt(id);
assert_eq!(f.state.pending_prompt().map(|b| b.id), Some(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(); f.state.store.delete_call(id).unwrap();
assert_eq!(f.state.pending_prompt(), None); 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] #[test]
fn discard_call_deletes_audio_and_row_and_clears_the_prompt() { fn discard_call_deletes_audio_and_row_and_clears_the_prompt() {
let f = fixture(); let f = fixture();
let (id, mic, out) = finished_call(&f); let (id, mic, out) = finished_call(&f, "a");
f.state.set_pending_prompt(id); 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!(!mic.exists());
assert!(!out.exists()); assert!(!out.exists());
assert_eq!(f.state.store.get_call(id).unwrap(), None); assert_eq!(f.state.store.get_call(id).unwrap(), None);
@@ -543,10 +620,10 @@ mod tests {
} }
#[test] #[test]
fn discard_call_of_another_call_keeps_the_prompt() { fn discard_call_of_an_unqueued_call_keeps_the_prompt() {
let f = fixture(); let f = fixture();
let (pending, _, _) = finished_call(&f); let (pending, _, _) = finished_call(&f, "a");
f.state.set_pending_prompt(pending); f.state.queue_prompt(pending);
let other = f let other = f
.state .state
.store .store
@@ -554,8 +631,8 @@ mod tests {
.unwrap(); .unwrap();
// Missing track files are not an error. // 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.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)));
} }
} }
+6 -1
View File
@@ -49,6 +49,11 @@ export interface CallBrief {
duration_secs: number 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 { export interface Levels {
mic: number mic: number
out: number out: number
@@ -66,7 +71,7 @@ export const getRecordingState = () => invoke<RecordingState>('get_recording_sta
export const startManualRecording = () => invoke<CallId>('start_manual_recording') export const startManualRecording = () => invoke<CallId>('start_manual_recording')
export const stopRecording = () => invoke<void>('stop_recording') export const stopRecording = () => invoke<void>('stop_recording')
export const discardCurrentRecording = () => invoke<void>('discard_current_recording') export const discardCurrentRecording = () => invoke<void>('discard_current_recording')
export const getPendingPrompt = () => invoke<CallBrief | null>('get_pending_prompt') export const getPendingPrompt = () => invoke<PendingPrompt | null>('get_pending_prompt')
export const getCallBrief = (id: CallId) => invoke<CallBrief>('get_call_brief', { id }) export const getCallBrief = (id: CallId) => invoke<CallBrief>('get_call_brief', { id })
export const transcribeCall = (id: CallId) => invoke<void>('transcribe_call', { id }) export const transcribeCall = (id: CallId) => invoke<void>('transcribe_call', { id })
export const discardCall = (id: CallId) => invoke<void>('discard_call', { id }) export const discardCall = (id: CallId) => invoke<void>('discard_call', { id })
+13 -12
View File
@@ -3,38 +3,33 @@
import { import {
discardCall, discardCall,
errorText, errorText,
getCallBrief,
getPendingPrompt, getPendingPrompt,
onCallEnded, onCallEnded,
transcribeCall, transcribeCall,
type CallBrief,
type CallId, type CallId,
type PendingPrompt,
} from '../lib/api' } from '../lib/api'
import { formatMinutes, formatTimeRange } from '../lib/format' import { formatMinutes, formatTimeRange } from '../lib/format'
let brief = $state<CallBrief | null>(null) let brief = $state<PendingPrompt | null>(null)
let busy = $state(false) let busy = $state(false)
let error = $state('') let error = $state('')
function show(next: CallBrief | null) { function show(next: PendingPrompt | null) {
if (next?.id !== brief?.id) error = '' if (next?.id !== brief?.id) error = ''
brief = next brief = next
} }
// The window stays loaded while hidden, so it may have missed the event: // The backend queues ended calls; this window always shows the oldest.
// ask the backend whenever it is shown again. // It stays loaded while hidden and may have missed an event, so it also
// asks whenever it is shown again.
const refresh = () => const refresh = () =>
getPendingPrompt() getPendingPrompt()
.then(show) .then(show)
.catch((err) => (error = errorText(err))) .catch((err) => (error = errorText(err)))
const load = (id: CallId) =>
getCallBrief(id)
.then(show)
.catch((err) => (error = errorText(err)))
onMount(() => { onMount(() => {
const unlisten = onCallEnded(load) const unlisten = onCallEnded(refresh)
refresh() refresh()
window.addEventListener('focus', refresh) window.addEventListener('focus', refresh)
return () => { return () => {
@@ -49,6 +44,9 @@
error = '' error = ''
try { try {
await action(brief.id) await action(brief.id)
// Decided: show the next queued call (the backend hides the window
// once none is left).
await refresh()
} catch (err) { } catch (err) {
error = errorText(err) error = errorText(err)
} finally { } finally {
@@ -61,6 +59,9 @@
{#if brief} {#if brief}
<div class="title">{brief.app} · {formatMinutes(brief.duration_secs)}</div> <div class="title">{brief.app} · {formatMinutes(brief.duration_secs)}</div>
<div class="muted">{formatTimeRange(brief.started_at, brief.ended_at)}</div> <div class="muted">{formatTimeRange(brief.started_at, brief.ended_at)}</div>
{#if brief.more_waiting > 0}
<div class="muted">Weitere Gespräche warten: {brief.more_waiting}</div>
{/if}
<div class="actions"> <div class="actions">
<button class="btn primary" disabled={busy} onclick={() => run(transcribeCall)}> <button class="btn primary" disabled={busy} onclick={() => run(transcribeCall)}>
Transkribieren Transkribieren