From e1b0033ac02eda0c20521e255792633015e2966f Mon Sep 17 00:00:00 2001 From: Virtue039 <77153652+virtue039@users.noreply.github.com> Date: Tue, 24 Nov 2026 20:17:25 +0800 Subject: [PATCH 1/2] Reject invalid transcription times before they become audio citations. Signed-off-by: Virtue039 <77153652+virtue039@users.noreply.github.com> --- crates/utopia-ingest/src/transcript.rs | 43 +++++++++++++++++++++++--- 1 file changed, 38 insertions(+), 5 deletions(-) diff --git a/crates/utopia-ingest/src/transcript.rs b/crates/utopia-ingest/src/transcript.rs index 1abfa424c..9af37d3c9 100644 --- a/crates/utopia-ingest/src/transcript.rs +++ b/crates/utopia-ingest/src/transcript.rs @@ -13,6 +13,7 @@ use crate::provenance::{Origin, Provenance, Segment}; use crate::reading::{Place, Reading, Region}; use crate::{NoSpeakers, Unreadable}; +use anyhow::Context; use serde_json::{json, Value}; /// 读转写结果。有一句带字却没有说话人,整份拒收 @@ -24,7 +25,7 @@ pub fn reading(response: &Value, model: &str) -> anyhow::Result { let mut text = String::new(); let mut regions: Vec = Vec::new(); let mut last_speaker: Option = None; - for seg in segments { + for (index, seg) in segments.iter().enumerate() { let said = crate::mineru::without_nul(seg["text"].as_str().unwrap_or_default()); let said = said.split_whitespace().collect::>().join(" "); if said.is_empty() { @@ -35,6 +36,15 @@ pub fn reading(response: &Value, model: &str) -> anyhow::Result { Value::Number(n) => n.to_string(), _ => return Err(NoSpeakers.into()), }; + let number = index + 1; + let start_ms = millis(&seg["start"]) + .with_context(|| format!("Transcription segment {number} has an invalid start time"))?; + let end_ms = millis(&seg["end"]) + .with_context(|| format!("Transcription segment {number} has an invalid end time"))?; + anyhow::ensure!( + end_ms >= start_ms, + "Transcription segment {number} ends before it starts" + ); let start = text.len(); if last_speaker.as_deref() == Some(speaker.as_str()) { text.push(' '); @@ -48,8 +58,8 @@ pub fn reading(response: &Value, model: &str) -> anyhow::Result { regions.push(Region { range: start..text.len(), place: Place::Time { - start_ms: millis(&seg["start"]), - end_ms: millis(&seg["end"]), + start_ms: start_ms.round() as u64, + end_ms: end_ms.round() as u64, speaker: speaker.clone(), }, }); @@ -92,8 +102,10 @@ pub fn reading(response: &Value, model: &str) -> anyhow::Result { }) } -fn millis(v: &Value) -> u64 { - (v.as_f64().unwrap_or(0.0).max(0.0) * 1000.0).round() as u64 +fn millis(v: &Value) -> Option { + let seconds = v.as_f64()?; + let millis = seconds * 1000.0; + (seconds >= 0.0 && millis.round() < u64::MAX as f64).then_some(millis) } #[cfg(test)] @@ -153,6 +165,27 @@ mod tests { ); } + #[test] + fn an_invalid_segment_time_cannot_become_a_citation() { + for (field, value, problem) in [ + ("start", Value::Null, "invalid start"), + ("end", json!(2.3999), "ends before"), + ("start", json!(-0.2), "invalid start"), + ("end", json!("5.1"), "invalid end"), + ] { + let mut input = meeting(); + let segment = &mut input["segments"][1]; + if value.is_null() { + segment.as_object_mut().unwrap().remove(field); + } else { + segment[field] = value; + } + let message = reading(&input, "model").unwrap_err().to_string(); + assert!(message.contains("segment 2"), "{message}"); + assert!(message.contains(problem), "{message}"); + } + } + #[test] fn a_transcript_without_speakers_is_refused() { let unlabelled = From 0c2a7159a85968ca7881daca661b3801aa241254 Mon Sep 17 00:00:00 2001 From: Virtue039 <77153652+virtue039@users.noreply.github.com> Date: Wed, 2 Dec 2026 22:18:27 +0800 Subject: [PATCH 2/2] Stop retrying transcriptions with unusable citation times. Signed-off-by: Virtue039 <77153652+virtue039@users.noreply.github.com> --- crates/utopia-ingest/src/transcript.rs | 21 +++++++++++---------- 1 file changed, 11 insertions(+), 10 deletions(-) diff --git a/crates/utopia-ingest/src/transcript.rs b/crates/utopia-ingest/src/transcript.rs index 9af37d3c9..d239ab078 100644 --- a/crates/utopia-ingest/src/transcript.rs +++ b/crates/utopia-ingest/src/transcript.rs @@ -13,7 +13,6 @@ use crate::provenance::{Origin, Provenance, Segment}; use crate::reading::{Place, Reading, Region}; use crate::{NoSpeakers, Unreadable}; -use anyhow::Context; use serde_json::{json, Value}; /// 读转写结果。有一句带字却没有说话人,整份拒收 @@ -37,14 +36,14 @@ pub fn reading(response: &Value, model: &str) -> anyhow::Result { _ => return Err(NoSpeakers.into()), }; let number = index + 1; - let start_ms = millis(&seg["start"]) - .with_context(|| format!("Transcription segment {number} has an invalid start time"))?; - let end_ms = millis(&seg["end"]) - .with_context(|| format!("Transcription segment {number} has an invalid end time"))?; - anyhow::ensure!( - end_ms >= start_ms, - "Transcription segment {number} ends before it starts" - ); + let invalid_time = + |reason: &str| Unreadable(format!("Transcription segment {number} {reason}")); + let start_ms = + millis(&seg["start"]).ok_or_else(|| invalid_time("has an invalid start time"))?; + let end_ms = millis(&seg["end"]).ok_or_else(|| invalid_time("has an invalid end time"))?; + if end_ms < start_ms { + return Err(invalid_time("ends before it starts").into()); + } let start = text.len(); if last_speaker.as_deref() == Some(speaker.as_str()) { text.push(' '); @@ -180,7 +179,9 @@ mod tests { } else { segment[field] = value; } - let message = reading(&input, "model").unwrap_err().to_string(); + let err = reading(&input, "model").unwrap_err(); + assert!(err.is::(), "{err}"); + let message = err.to_string(); assert!(message.contains("segment 2"), "{message}"); assert!(message.contains(problem), "{message}"); }