Collapse adjacent same-speaker segments after reconciliation

Fragments reabsorbed by smoothFragments (e.g. "I" then "need to switch it
back") were left as separate transcript lines. Add SpeakerReconciler.mergeAdjacent
to join consecutive same-speaker segments within 2s, concatenating their text.

Wire it into SessionController.finishBackend AFTER reconcile/LLM naming. The
collapse needs no LLM, so finishBackend no longer early-returns when the gateway
has no chat model — it runs the collapse and re-persists speakers.json
unconditionally, gating only the reconcile and recap passes on the model.
This commit is contained in:
Grant Gilliam
2026-06-08 13:19:05 -05:00
parent ab910cf742
commit a95f27ecd1
3 changed files with 70 additions and 8 deletions
@@ -82,6 +82,28 @@ enum SpeakerReconciler {
speakers: speakers, segments: result, models: file.models)
}
/// Collapse consecutive segments from the SAME speaker separated by `maxGap`
/// seconds into one, joining their text so fragments reabsorbed by smoothing
/// (e.g. "I" then "need to switch it back") read as a single clean line. Pure.
static func mergeAdjacent(_ file: SpeakersFile, maxGap: Double = 2.0) -> SpeakersFile {
let sorted = file.segments.sorted { $0.start < $1.start }
guard !sorted.isEmpty else { return file }
var out: [SpeakersFile.Segment] = []
for s in sorted {
if var last = out.last, last.speaker == s.speaker, s.start - last.end <= maxGap {
let joined = [last.text, s.text].compactMap { $0?.trimmingCharacters(in: .whitespaces) }
.filter { !$0.isEmpty }.joined(separator: " ")
last = .init(start: last.start, end: max(last.end, s.end), speaker: s.speaker,
text: joined.isEmpty ? nil : joined)
out[out.count - 1] = last
} else {
out.append(s)
}
}
return SpeakersFile(sessionId: file.sessionId, app: file.app, durationSec: file.durationSec,
speakers: file.speakers, segments: out, models: file.models)
}
// MARK: - Voiceprint merge (pure)
static func protectedNames(_ file: SpeakersFile, selfName: String) -> Set<String> {