feat #14740: merge speakers and name calls

Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
This commit is contained in:
hauke
2026-11-06 11:17:40 +01:00
co-authored by Claude Opus 5.5
parent 5dfe1d9357
commit 345dd01147
12 changed files with 1213 additions and 29 deletions
+345 -13
View File
@@ -6,11 +6,15 @@ use std::path::{Path, PathBuf};
use std::sync::Mutex;
use chrono::{DateTime, Local};
use rusqlite::{params, Connection, Row};
use rusqlite::{params, Connection, Row, TransactionBehavior};
use thiserror::Error;
use crate::merge::{self, MergeError};
use crate::model::{Call, CallId, CallStatus, Segment, Speaker, Summary};
/// Longest call title, in characters.
const MAX_TITLE_CHARS: usize = 120;
/// Errors a [`Store`] operation can fail with.
#[derive(Debug, Error)]
pub enum StoreError {
@@ -22,6 +26,13 @@ pub enum StoreError {
/// speaker key or malformed summary JSON.
#[error("corrupt stored value: {0}")]
Corrupt(String),
/// A speaker merge that [`merge::check_merge`] refused.
#[error(transparent)]
Merge(#[from] MergeError),
#[error("Titel zu lang (max. 120 Zeichen)")]
TitleTooLong,
}
pub type Result<T> = std::result::Result<T, StoreError>;
@@ -40,7 +51,8 @@ pub struct RecoveryReport {
pub failed: Vec<CallId>,
}
const SCHEMA_SQL: &str = "
/// Version 1: the first release.
const SCHEMA_V1_SQL: &str = "
CREATE TABLE IF NOT EXISTS calls (
id INTEGER PRIMARY KEY AUTOINCREMENT,
app TEXT NOT NULL,
@@ -88,6 +100,22 @@ CREATE TABLE IF NOT EXISTS remote_jobs (
);
";
/// Version 2: call titles and reversible speaker merges.
const SCHEMA_V2_SQL: &str = "
ALTER TABLE calls ADD COLUMN title TEXT;
CREATE TABLE speaker_merges (
call_id INTEGER NOT NULL REFERENCES calls(id) ON DELETE CASCADE,
from_key TEXT NOT NULL,
into_key TEXT NOT NULL,
PRIMARY KEY (call_id, from_key)
);
";
/// Schema steps by `PRAGMA user_version`: entry `i` takes a database from
/// version `i` to `i + 1`. A new database runs all of them.
const MIGRATIONS: &[&str] = &[SCHEMA_V1_SQL, SCHEMA_V2_SQL];
/// SQLite-backed store for calls and everything derived from them.
pub struct Store {
conn: Mutex<Connection>,
@@ -106,15 +134,9 @@ impl Store {
Self::init(conn)
}
fn init(conn: Connection) -> Result<Store> {
fn init(mut conn: Connection) -> Result<Store> {
conn.execute_batch("PRAGMA foreign_keys = ON;")?;
let user_version: i64 = conn.query_row("PRAGMA user_version", [], |row| row.get(0))?;
if user_version < 1 {
conn.execute_batch(SCHEMA_SQL)?;
conn.execute_batch("PRAGMA user_version = 1;")?;
}
migrate(&mut conn)?;
Ok(Store {
conn: Mutex::new(conn),
})
@@ -309,6 +331,55 @@ impl Store {
Ok(names)
}
/// Merges remote speaker `from_key` into `into_key` for call `id`,
/// replacing an earlier merge of `from_key`. Refused (see
/// [`merge::check_merge`]) for "me", a speaker merged into itself, and a
/// merge that would make the call's merges loop.
pub fn set_speaker_merge(&self, id: CallId, from_key: &str, into_key: &str) -> Result<()> {
let mut conn = self.conn.lock().unwrap();
let tx = conn.transaction_with_behavior(TransactionBehavior::Immediate)?;
merge::check_merge(&merges_of(&tx, id)?, from_key, into_key)?;
tx.execute(
"INSERT INTO speaker_merges (call_id, from_key, into_key) VALUES (?1, ?2, ?3) \
ON CONFLICT(call_id, from_key) DO UPDATE SET into_key = excluded.into_key",
params![id, from_key, into_key],
)?;
tx.commit()?;
Ok(())
}
/// Undoes the merge of `from_key`. Not an error if it was not merged.
pub fn remove_speaker_merge(&self, id: CallId, from_key: &str) -> Result<()> {
let conn = self.conn.lock().unwrap();
conn.execute(
"DELETE FROM speaker_merges WHERE call_id = ?1 AND from_key = ?2",
params![id, from_key],
)?;
Ok(())
}
/// The call's speaker merges, source key → target key as stored (chains
/// are resolved by [`merge::resolve_merges`]).
pub fn speaker_merges(&self, id: CallId) -> Result<BTreeMap<String, String>> {
let conn = self.conn.lock().unwrap();
merges_of(&conn, id)
}
/// Names the call. The title is trimmed; an empty or missing one clears
/// it. Longer than 120 characters is refused.
pub fn set_title(&self, id: CallId, title: Option<&str>) -> Result<()> {
let title = title.map(str::trim).filter(|t| !t.is_empty());
if title.is_some_and(|t| t.chars().count() > MAX_TITLE_CHARS) {
return Err(StoreError::TitleTooLong);
}
let conn = self.conn.lock().unwrap();
conn.execute(
"UPDATE calls SET title = ?1 WHERE id = ?2",
params![title, id],
)?;
Ok(())
}
pub fn mark_transcribed(&self, id: CallId, at: DateTime<Local>) -> Result<()> {
let conn = self.conn.lock().unwrap();
conn.execute(
@@ -328,13 +399,14 @@ impl Store {
}
/// Deletes a call and every row derived from it (segments, summary,
/// speaker names, remote jobs).
/// speaker names and merges, remote jobs).
pub fn delete_call(&self, id: CallId) -> Result<()> {
let mut conn = self.conn.lock().unwrap();
let tx = conn.transaction()?;
tx.execute("DELETE FROM segments WHERE call_id = ?1", params![id])?;
tx.execute("DELETE FROM summaries WHERE call_id = ?1", params![id])?;
tx.execute("DELETE FROM speaker_names WHERE call_id = ?1", params![id])?;
tx.execute("DELETE FROM speaker_merges WHERE call_id = ?1", params![id])?;
tx.execute("DELETE FROM remote_jobs WHERE call_id = ?1", params![id])?;
tx.execute("DELETE FROM calls WHERE id = ?1", params![id])?;
tx.commit()?;
@@ -436,6 +508,34 @@ impl Store {
}
}
/// Brings the schema to the newest version in one transaction, so a failed
/// upgrade leaves the database as it was. Running it on an up-to-date
/// database changes nothing.
fn migrate(conn: &mut Connection) -> Result<()> {
let tx = conn.transaction_with_behavior(TransactionBehavior::Immediate)?;
let version: i64 = tx.query_row("PRAGMA user_version", [], |row| row.get(0))?;
let done = usize::try_from(version).unwrap_or(0);
if done < MIGRATIONS.len() {
for step in &MIGRATIONS[done..] {
tx.execute_batch(step)?;
}
tx.execute_batch(&format!("PRAGMA user_version = {};", MIGRATIONS.len()))?;
}
tx.commit()?;
Ok(())
}
fn merges_of(conn: &Connection, id: CallId) -> Result<BTreeMap<String, String>> {
let mut stmt =
conn.prepare("SELECT from_key, into_key FROM speaker_merges WHERE call_id = ?1")?;
let mut rows = stmt.query(params![id])?;
let mut merges = BTreeMap::new();
while let Some(row) = rows.next()? {
merges.insert(row.get(0)?, row.get(1)?);
}
Ok(merges)
}
/// Ids of calls currently in `status`, ordered by id (insertion order).
fn ids_with_status(conn: &rusqlite::Connection, status: CallStatus) -> Result<Vec<CallId>> {
let mut stmt = conn.prepare("SELECT id FROM calls WHERE status = ?1 ORDER BY id ASC")?;
@@ -447,9 +547,9 @@ fn ids_with_status(conn: &rusqlite::Connection, status: CallStatus) -> Result<Ve
Ok(ids)
}
const SELECT_ALL_CALLS_SQL: &str = "SELECT id, app, started_at, ended_at, status, interrupted, error, mic_path, out_path, transcribed_at, audio_deleted, account_id FROM calls";
const SELECT_ALL_CALLS_SQL: &str = "SELECT id, app, started_at, ended_at, status, interrupted, error, mic_path, out_path, transcribed_at, audio_deleted, account_id, title FROM calls";
const SELECT_CALL_COLUMNS_SQL: &str = "SELECT id, app, started_at, ended_at, status, interrupted, error, mic_path, out_path, transcribed_at, audio_deleted, account_id FROM calls WHERE id = ?1";
const SELECT_CALL_COLUMNS_SQL: &str = "SELECT id, app, started_at, ended_at, status, interrupted, error, mic_path, out_path, transcribed_at, audio_deleted, account_id, title FROM calls WHERE id = ?1";
fn row_to_call(row: &Row) -> Result<Call> {
let id: CallId = row.get(0)?;
@@ -464,6 +564,7 @@ fn row_to_call(row: &Row) -> Result<Call> {
let transcribed_at: Option<String> = row.get(9)?;
let audio_deleted: bool = row.get(10)?;
let account_id: Option<String> = row.get(11)?;
let title: Option<String> = row.get(12)?;
Ok(Call {
id,
@@ -478,6 +579,7 @@ fn row_to_call(row: &Row) -> Result<Call> {
transcribed_at: opt_dt_from_str(transcribed_at)?,
audio_deleted,
account_id,
title,
})
}
@@ -540,6 +642,234 @@ mod tests {
};
use crate::store::Store;
/// The schema of user_version 1, exactly as the first release created
/// it, so the upgrade runs against what users have on disk.
const RELEASED_V1_SCHEMA_SQL: &str = "
CREATE TABLE IF NOT EXISTS calls (
id INTEGER PRIMARY KEY AUTOINCREMENT,
app TEXT NOT NULL,
started_at TEXT NOT NULL,
ended_at TEXT,
status TEXT NOT NULL,
interrupted INTEGER NOT NULL,
error TEXT,
mic_path TEXT NOT NULL,
out_path TEXT NOT NULL,
transcribed_at TEXT,
audio_deleted INTEGER NOT NULL,
account_id TEXT
);
CREATE TABLE IF NOT EXISTS segments (
id INTEGER PRIMARY KEY AUTOINCREMENT,
call_id INTEGER NOT NULL REFERENCES calls(id) ON DELETE CASCADE,
seq INTEGER NOT NULL,
speaker TEXT NOT NULL,
start_ts REAL NOT NULL,
end_ts REAL NOT NULL,
text TEXT NOT NULL
);
CREATE INDEX IF NOT EXISTS segments_call_id_idx ON segments(call_id);
CREATE TABLE IF NOT EXISTS summaries (
call_id INTEGER PRIMARY KEY REFERENCES calls(id) ON DELETE CASCADE,
json TEXT NOT NULL,
edited INTEGER NOT NULL
);
CREATE TABLE IF NOT EXISTS speaker_names (
call_id INTEGER NOT NULL REFERENCES calls(id) ON DELETE CASCADE,
speaker_key TEXT NOT NULL,
name TEXT NOT NULL,
PRIMARY KEY (call_id, speaker_key)
);
CREATE TABLE IF NOT EXISTS remote_jobs (
call_id INTEGER NOT NULL REFERENCES calls(id) ON DELETE CASCADE,
track TEXT NOT NULL,
remote_id TEXT NOT NULL,
PRIMARY KEY (call_id, track, remote_id)
);
";
fn user_version(path: &Path) -> i64 {
rusqlite::Connection::open(path)
.unwrap()
.query_row("PRAGMA user_version", [], |row| row.get(0))
.unwrap()
}
fn new_call(store: &Store) -> CallId {
store
.create_call("teams.exe", Local::now(), Path::new(""), Path::new(""))
.unwrap()
}
fn merge_map(pairs: &[(&str, &str)]) -> BTreeMap<String, String> {
pairs
.iter()
.map(|(from, into)| (from.to_string(), into.to_string()))
.collect()
}
#[test]
fn a_new_database_starts_at_version_2() {
let dir = tempfile::tempdir().unwrap();
let path = dir.path().join("calls.sqlite");
let store = Store::open(&path).unwrap();
let id = new_call(&store);
store.set_title(id, Some("Angebot")).unwrap();
store.set_speaker_merge(id, "remote:2", "remote:1").unwrap();
drop(store);
assert_eq!(user_version(&path), 2);
}
#[test]
fn a_version_1_database_is_upgraded_keeping_its_data() {
let dir = tempfile::tempdir().unwrap();
let path = dir.path().join("calls.sqlite");
let started = "2026-11-05T10:14:00+01:00";
{
let conn = rusqlite::Connection::open(&path).unwrap();
conn.execute_batch(RELEASED_V1_SCHEMA_SQL).unwrap();
conn.execute_batch("PRAGMA user_version = 1;").unwrap();
conn.execute(
"INSERT INTO calls (app, started_at, ended_at, status, interrupted, error, mic_path, out_path, transcribed_at, audio_deleted, account_id) \
VALUES ('Microsoft Teams', ?1, ?1, 'done', 0, NULL, '/rec/1-mic.wav', '/rec/1-out.wav', ?1, 0, 'mock-2')",
rusqlite::params![started],
)
.unwrap();
conn.execute_batch(
"INSERT INTO segments (call_id, seq, speaker, start_ts, end_ts, text) VALUES \
(1, 0, 'me', 0.0, 1.5, 'Guten Tag.'), \
(1, 1, 'remote:2', 2.0, 3.0, 'Hallo.'); \
INSERT INTO speaker_names (call_id, speaker_key, name) VALUES (1, 'remote:2', 'Frau Probe');",
)
.unwrap();
}
let store = Store::open(&path).unwrap();
let call = store.get_call(1).unwrap().expect("call kept");
assert_eq!(call.app, "Microsoft Teams");
assert_eq!(call.status, CallStatus::Done);
assert_eq!(call.account_id.as_deref(), Some("mock-2"));
assert_eq!(call.mic_path, PathBuf::from("/rec/1-mic.wav"));
assert_eq!(call.title, None);
let texts: Vec<_> = store
.segments(1)
.unwrap()
.into_iter()
.map(|s| (s.speaker, s.text))
.collect();
assert_eq!(
texts,
[
(Speaker::Me, "Guten Tag.".to_string()),
(Speaker::Remote(2), "Hallo.".to_string())
]
);
assert_eq!(
store.speaker_names(1).unwrap(),
merge_map(&[("remote:2", "Frau Probe")])
);
assert_eq!(store.speaker_merges(1).unwrap(), BTreeMap::new());
// The new column and table work, and opening again changes nothing.
store.set_title(1, Some("Lieferung März")).unwrap();
store.set_speaker_merge(1, "remote:2", "remote:1").unwrap();
drop(store);
assert_eq!(user_version(&path), 2);
let store = Store::open(&path).unwrap();
let call = store.get_call(1).unwrap().unwrap();
assert_eq!(call.title.as_deref(), Some("Lieferung März"));
assert_eq!(
store.speaker_merges(1).unwrap(),
merge_map(&[("remote:2", "remote:1")])
);
assert_eq!(store.list_calls().unwrap().len(), 1);
}
#[test]
fn speaker_merges_are_set_replaced_and_removed() {
let store = Store::open_in_memory().unwrap();
let id = new_call(&store);
let other = new_call(&store);
assert_eq!(store.speaker_merges(id).unwrap(), BTreeMap::new());
store.set_speaker_merge(id, "remote:3", "remote:2").unwrap();
store.set_speaker_merge(id, "remote:2", "remote:1").unwrap();
store
.set_speaker_merge(other, "remote:1", "remote:2")
.unwrap();
assert_eq!(
store.speaker_merges(id).unwrap(),
merge_map(&[("remote:2", "remote:1"), ("remote:3", "remote:2")])
);
store.set_speaker_merge(id, "remote:3", "remote:1").unwrap();
store.remove_speaker_merge(id, "remote:2").unwrap();
store.remove_speaker_merge(id, "remote:9").unwrap(); // never merged
assert_eq!(
store.speaker_merges(id).unwrap(),
merge_map(&[("remote:3", "remote:1")])
);
assert_eq!(
store.speaker_merges(other).unwrap(),
merge_map(&[("remote:1", "remote:2")])
);
}
#[test]
fn a_merge_that_would_loop_or_involve_me_is_not_stored() {
let store = Store::open_in_memory().unwrap();
let id = new_call(&store);
store.set_speaker_merge(id, "remote:2", "remote:1").unwrap();
let err = store
.set_speaker_merge(id, "remote:1", "remote:2")
.unwrap_err();
assert_eq!(
err.to_string(),
"Diese Zuordnung würde eine Schleife ergeben"
);
assert!(store.set_speaker_merge(id, "me", "remote:1").is_err());
assert!(store.set_speaker_merge(id, "remote:1", "me").is_err());
assert_eq!(
store.speaker_merges(id).unwrap(),
merge_map(&[("remote:2", "remote:1")])
);
}
#[test]
fn the_title_is_trimmed_cleared_when_empty_and_limited_to_120_chars() {
let store = Store::open_in_memory().unwrap();
let id = new_call(&store);
let title = |store: &Store| store.get_call(id).unwrap().unwrap().title;
assert_eq!(title(&store), None);
store.set_title(id, Some(" Angebot Rioja ")).unwrap();
assert_eq!(title(&store).as_deref(), Some("Angebot Rioja"));
assert_eq!(
store.list_calls().unwrap()[0].title.as_deref(),
Some("Angebot Rioja")
);
store.set_title(id, Some(" ")).unwrap();
assert_eq!(title(&store), None);
store.set_title(id, Some("Angebot")).unwrap();
store.set_title(id, None).unwrap();
assert_eq!(title(&store), None);
let longest = "ä".repeat(120);
store.set_title(id, Some(&longest)).unwrap();
assert_eq!(title(&store), Some(longest.clone()));
let err = store.set_title(id, Some(&"ä".repeat(121))).unwrap_err();
assert_eq!(err.to_string(), "Titel zu lang (max. 120 Zeichen)");
assert_eq!(title(&store), Some(longest));
}
#[test]
fn create_and_finish_call() {
let store = Store::open_in_memory().unwrap();
@@ -853,6 +1183,7 @@ mod tests {
)
.unwrap();
store.set_speaker_name(id, "me", "Ich").unwrap();
store.set_speaker_merge(id, "remote:2", "remote:1").unwrap();
store
.record_remote_job(id, "deepscript", "job-123")
.unwrap();
@@ -864,6 +1195,7 @@ mod tests {
assert_eq!(store.summary(id).unwrap(), None);
assert_eq!(store.speaker_names(id).unwrap(), BTreeMap::new());
assert_eq!(store.remote_jobs(id).unwrap(), Vec::new());
assert_eq!(store.speaker_merges(id).unwrap(), BTreeMap::new());
}
#[test]