Voice-to-text dictation app with local (Nvidia Parakeet/Whisper) and cloud models (BYOK). Privacy-first and available cross-platform.
JavaScript aianthropiccross-platformgeminigroqlinuxmacosnvidiaopen-sourceopenaiparakeetspeech-to-texttranscribewhisperwindows
fix(meetings): keep a resumed note's speaker ids distinct per session (#2778)
Chad Piha37d5038fee9c
diff --git a/src/helpers/diarization.js b/src/helpers/diarization.jsindex 8718ddf5e..be72f7932 100644--- a/src/helpers/diarization.js+++ b/src/helpers/diarization.js@@ -10,6 +10,7 @@ const { getModelsDirForService } = require("./modelDirUtils");const { convertToWav } = require("./ffmpegUtils");const { getSafeTempDir } = require("./safeTempDir");const { applyConfirmedSpeaker } = require("./speakerAssignmentPolicy");+const { buildSpeakerRenumber } = require("./speakerIndexBase");const sidecarPidFile = require("./sidecarPidFile");const {transcriptsOverlap,@@ -443,21 +444,20 @@ class DiarizationManager {return segments.map((s) => (keep.has(s.speaker) ? s : { ...s, speaker: primary }));}- mergeWithTranscript(transcriptSegments, diarizationSegments, { diarizedSource = "system" } = {}) {+ mergeWithTranscript(+ transcriptSegments,+ diarizationSegments,+ { diarizedSource = "system", speakerIndexBase = 0 } = {}+ ) {if (!transcriptSegments || transcriptSegments.length === 0) return [];const deduped = dedupeMicAgainstSystem(transcriptSegments);if (!diarizationSegments || diarizationSegments.length === 0) {return deduped.map((seg) => ({ ...seg }));}- // Build speaker renumbering map (e.g., speaker_00 → speaker_0)+ // Renumber clusters (speaker_00 → speaker_0), offset past a resumed note's ids.const speakerSet = new Set(diarizationSegments.map((d) => d.speaker));- const speakerMap = new Map();- let idx = 0;- for (const sp of speakerSet) {- speakerMap.set(sp, `speaker_${idx}`);- idx++;- }+ const speakerMap = buildSpeakerRenumber(speakerSet, speakerIndexBase);// Mic-mode softening: a single cluster on a mic-only session is the user// talking alone (e.g. a call where the remote never spoke audibly), so itdiff --git a/src/helpers/ipcHandlers.js b/src/helpers/ipcHandlers.jsindex 87857d535..d0a35c91b 100644--- a/src/helpers/ipcHandlers.js+++ b/src/helpers/ipcHandlers.js@@ -175,6 +175,11 @@ const {isSpeakerLocked,} = require("./speakerAssignmentPolicy");const { normalizeStoredSpeakerCount } = require("./speakerCount");+const {+ buildSpeakerRenumber,+ normalizeSpeakerIndexBase,+ resolveSpeakerIndexBase,+} = require("./speakerIndexBase");const { downsample24kTo16k, pcm16ToWav } = require("../utils/audioUtils");const postMigrationDetector = require("./postMigrationDetector");const screenContextCapture = require("./screenContextCapture");@@ -708,6 +713,9 @@ class IPCHandlers {// off until the renderer syncs (see meetingDetectionPreferencePolicy.js).this.meetingProcessDetection = true;this.activeMeetingSpeakerConfig = null;+ // Diarization session id → { noteId, ceiling }: speaker ids a stopped session's+ // diarization may still write to its note (see _resolveMeetingSpeakerIndexBase).+ this._diarizationSpeakerReservations = new Map();this.whisperVadSettings = {dictationSileroEnabled: false,noteRecordingSileroEnabled: true,@@ -8055,6 +8063,8 @@ class IPCHandlers {let meetingOneOnOneAttendee = null;let meetingOneOnOneProfileBound = false;let meetingNoteId = null;+ // First speaker index this session may mint; past a resumed note's ids.+ let meetingSpeakerIndexBase = 0;const getLiveSpeakerProfiles = () => {const attendees = this._getNoteNonSelfParticipants(meetingNoteId);@@ -8368,6 +8378,7 @@ class IPCHandlers {getSpeakerProfiles: getLiveSpeakerProfiles,maxSpeakers: resolveSessionMaxSpeakers(),enabled: true,+ speakerIndexBase: meetingSpeakerIndexBase,}).catch((error) => {@@ -8627,6 +8638,7 @@ class IPCHandlers {meetingOneOnOneAttendee = null;meetingOneOnOneProfileBound = false;meetingNoteId = null;+ meetingSpeakerIndexBase = 0;this._activeMeetingNoteId = null;meetingLocalMode = false;meetingLocalBuffers = { mic: [], system: [] };@@ -9199,7 +9211,11 @@ class IPCHandlers {armMeetingSystemAudioSilenceTimer(meetingConnectionWin, result.systemAudioStrategy);startMeetingSystemAudioWatchdog(meetingConnectionWin, result.systemAudioStrategy);}- return { ...result, sessionId: recordingSessionId };+ return {+ ...result,+ sessionId: recordingSessionId,+ speakerIndexBase: meetingSpeakerIndexBase,+ };};try {@@ -9210,6 +9226,10 @@ class IPCHandlers {meetingOneOnOneAttendee = resolveOneOnOneAttendeeForNote(options.noteId);meetingOneOnOneProfileBound = false;meetingNoteId = options.noteId ?? null;+ meetingSpeakerIndexBase = this._resolveMeetingSpeakerIndexBase(+ meetingNoteId,+ options.speakerIndexBase+ );this._activeMeetingNoteId = meetingNoteId;// Seed the speaker cap from the note/calendar participants up front so live@@ -9633,6 +9653,7 @@ class IPCHandlers {buildOrderedTranscriptText(diarizationSegments) || meetingLocalTranscript;const sessionSpeakerConfigSnapshot = this.activeMeetingSpeakerConfig;const noteIdSnapshot = meetingNoteId;+ const speakerIndexBaseSnapshot = meetingSpeakerIndexBase;this.activeMeetingSpeakerConfig = null;resetMeetingLocalState();@@ -9646,7 +9667,8 @@ class IPCHandlers {liveSpeakerState,sessionSpeakerConfigSnapshot,noteIdSnapshot,- diarizedSource+ diarizedSource,+ speakerIndexBaseSnapshot);return { success: true, transcript, diarizationSessionId };@@ -9661,6 +9683,7 @@ class IPCHandlers {const sessionSpeakerConfigSnapshot = this.activeMeetingSpeakerConfig;const noteIdSnapshot = meetingNoteId;+ const speakerIndexBaseSnapshot = meetingSpeakerIndexBase;this.activeMeetingSpeakerConfig = null;// Fire-and-forget background diarization (or notify skip)@@ -9673,7 +9696,8 @@ class IPCHandlers {liveSpeakerState,sessionSpeakerConfigSnapshot,noteIdSnapshot,- diarizedSource+ diarizedSource,+ speakerIndexBaseSnapshot);return { success: true, transcript, diarizationSessionId };@@ -12944,6 +12968,34 @@ class IPCHandlers {return reconciledSpeakers;}+ // The renderer's base covers the transcript it resumes; mappings and embeddings+ // can also hold ids no row carries any more (merged clusters, a cluster that+ // owned no segment), and a reused one would still name the new voice.+ // A note recorded again before its last session's diarization lands has neither+ // that result's ids in the seed nor its embeddings stored yet, so the session's+ // reserved ceiling counts too.+ _resolveMeetingSpeakerIndexBase(noteId, requestedBase) {+ let base = normalizeSpeakerIndexBase(requestedBase);+ if (noteId == null) return base;+ for (const reservation of this._diarizationSpeakerReservations.values()) {+ if (reservation.noteId === noteId) base = Math.max(base, reservation.ceiling);+ }+ try {+ const storedIds = [+ ...this.databaseManager.getSpeakerMappings(noteId).map((m) => m.speaker_id),+ ...this.databaseManager.getNoteSpeakerEmbeddings(noteId).map((e) => e.speaker_id),+ ];+ return resolveSpeakerIndexBase(base, storedIds);+ } catch (error) {+ debugLogger.warn(+ "Could not read stored speaker ids for the meeting note",+ { noteId, error: error.message },+ "speaker"+ );+ return base;+ }+ }+_resolveSpeakerExpectation({ sessionConfig, noteId, observedSpeakerIds, diarizedSource }) {// Only a count the user set explicitly outranks the note: participants added// mid-meeting postdate the config snapshot taken at recording start.@@ -12991,7 +13043,8 @@ class IPCHandlers {liveSpeakerState = null,sessionConfig = null,noteId = null,- diarizedSource = "system"+ diarizedSource = "system",+ speakerIndexBase = 0) {const send = (payload) => {if (win && !win.isDestroyed()) {@@ -13013,31 +13066,40 @@ class IPCHandlers {const fs = require("fs");+ const observedSpeakerIds = new Set(+ transcriptSegments+ .filter((segment) => segment.source === "system" && segment.speaker)+ .map((segment) => segment.speaker)+ );+ for (const speakerId of Object.keys(liveSpeakerState || {})) {+ observedSpeakerIds.add(speakerId);+ }++ if (observedSpeakerIds.size > 10) {+ debugLogger.warn("Excessive speaker count from live identification", {+ observedSpeakers: observedSpeakerIds.size,+ });+ }++ const { numSpeakers, cap } = this._resolveSpeakerExpectation({+ sessionConfig,+ noteId,+ observedSpeakerIds,+ diarizedSource,+ });+ // Reserved before the async work so a "record more" started while this runs+ // numbers its speakers past every cluster the result can name.+ if (noteId != null) {+ this._diarizationSpeakerReservations.set(sessionId, {+ noteId,+ ceiling: speakerIndexBase + cap,+ });+ }+(async () => {let tmpWav = null;try {tmpWav = await this.diarizationManager.convertRawPcmToWav(rawPcmPath, 24000);- const observedSpeakerIds = new Set(- transcriptSegments- .filter((segment) => segment.source === "system" && segment.speaker)- .map((segment) => segment.speaker)- );- for (const speakerId of Object.keys(liveSpeakerState || {})) {- observedSpeakerIds.add(speakerId);- }-- if (observedSpeakerIds.size > 10) {- debugLogger.warn("Excessive speaker count from live identification", {- observedSpeakers: observedSpeakerIds.size,- });- }-- const { numSpeakers, cap } = this._resolveSpeakerExpectation({- sessionConfig,- noteId,- observedSpeakerIds,- diarizedSource,- });let diarizationSegments = await this.diarizationManager.diarize(tmpWav,numSpeakers > 0 ? { numSpeakers } : {}@@ -13068,15 +13130,18 @@ class IPCHandlers {const enrichedSegments = this.diarizationManager.mergeWithTranscript(normalized,diarizationSegments,- { diarizedSource }+ { diarizedSource, speakerIndexBase });const speakerSet = new Set(diarizationSegments.map((d) => d.speaker));- const speakerRenumber = new Map();- let sIdx = 0;- for (const sp of speakerSet) {- speakerRenumber.set(sp, `speaker_${sIdx}`);- sIdx++;+ const speakerRenumber = buildSpeakerRenumber(speakerSet, speakerIndexBase);+ // Narrowed to the ids the result actually names; kept until quit, since the+ // renderer persists them only after this send.+ if (noteId != null) {+ this._diarizationSpeakerReservations.set(sessionId, {+ noteId,+ ceiling: speakerIndexBase + speakerRenumber.size,+ });}// Mirrors the mic-mode single-cluster softening in mergeWithTranscript:@@ -13181,6 +13246,7 @@ class IPCHandlers {send({ segments: enrichedSegments, speakerEmbeddings: speakerEmbeddingsMap });} catch (err) {debugLogger.warn("Background diarization failed", { error: err.message });+ this._diarizationSpeakerReservations.delete(sessionId);send({ segments: [] });} finally {try {diff --git a/src/helpers/liveSpeakerIdentifier.js b/src/helpers/liveSpeakerIdentifier.jsindex 90da49fdc..e4ae521e6 100644--- a/src/helpers/liveSpeakerIdentifier.js+++ b/src/helpers/liveSpeakerIdentifier.js@@ -4,6 +4,7 @@ const speakerEmbeddings = require("./speakerEmbeddings");const { MAX_EMBEDDING_SECONDS } = speakerEmbeddings;const { downsample24kTo16k, pcm16ToFloat32 } = require("../utils/audioUtils");const { MAX_SPEAKER_COUNT } = require("../constants/speakerDetection.json");+const { normalizeSpeakerIndexBase } = require("./speakerIndexBase");function clampMaxSpeakers(value) {const n = Number(value);@@ -216,6 +217,7 @@ class LiveSpeakerIdentifier {getSpeakerProfiles = null,maxSpeakers = MAX_SPEAKER_COUNT,enabled = true,+ speakerIndexBase = 0,} = resolvedOptions;this.onSpeakerIdentified =@@ -224,6 +226,8 @@ class LiveSpeakerIdentifier {this.maxSpeakers = clampMaxSpeakers(maxSpeakers);this.enabled = enabled !== false;this._resetMeetingState();+ // A resumed note already holds earlier sessions' ids; see speakerIndexBase.js.+ this.nextLiveIndex = normalizeSpeakerIndexBase(speakerIndexBase);if (!this.isAvailable()) {debugLogger.warn("Live speaker identifier unavailable", {diff --git a/src/helpers/speakerIndexBase.js b/src/helpers/speakerIndexBase.jsnew file mode 100644index 000000000..970598606--- /dev/null+++ b/src/helpers/speakerIndexBase.js@@ -0,0 +1,42 @@+// Speaker ids (`speaker_N`) key a note's speaker mappings, embeddings and locks.+// A resumed recording ("record more") appends to a note that already holds an+// earlier session's ids, so its own ids start past every index the note uses —+// otherwise a new voice would inherit the earlier person's name and lock.++function speakerIndexOf(speakerId) {+ if (typeof speakerId !== "string") return null;+ const match = /^speaker_(\d+)$/.exec(speakerId);+ return match ? Number(match[1]) : null;+}++function normalizeSpeakerIndexBase(value) {+ const base = Number(value);+ return Number.isSafeInteger(base) && base > 0 ? base : 0;+}++// The first index free of both the requested base and every id already in use.+function resolveSpeakerIndexBase(requested, speakerIds) {+ let base = normalizeSpeakerIndexBase(requested);+ for (const speakerId of speakerIds) {+ const index = speakerIndexOf(speakerId);+ if (index != null && index + 1 > base) base = index + 1;+ }+ return base;+}++// Diarization clusters are renumbered in first-seen order from the session's base.+function buildSpeakerRenumber(clusterIds, speakerIndexBase) {+ const base = normalizeSpeakerIndexBase(speakerIndexBase);+ const renumber = new Map();+ for (const clusterId of clusterIds) {+ renumber.set(clusterId, `speaker_${base + renumber.size}`);+ }+ return renumber;+}++module.exports = {+ speakerIndexOf,+ normalizeSpeakerIndexBase,+ resolveSpeakerIndexBase,+ buildSpeakerRenumber,+};diff --git a/src/stores/meetingRecordingStore.ts b/src/stores/meetingRecordingStore.tsindex 5e95e4f1c..e8f595280 100644--- a/src/stores/meetingRecordingStore.ts+++ b/src/stores/meetingRecordingStore.ts@@ -137,10 +137,12 @@ const buildTranscriptText = (segments: TranscriptSegment[]) =>.join(" ").trim();+// Seeds come from synced transcript JSON, so an index past safe integers is ignored.const getSpeakerNumericIndex = (speakerId?: string): number | null => {if (!speakerId) return null;- const match = speakerId.match(/speaker_(\d+)/);- return match ? Number(match[1]) : null;+ const match = /^speaker_(\d+)$/.exec(speakerId);+ const index = match ? Number(match[1]) : null;+ return index != null && Number.isSafeInteger(index) ? index : null;};const isSegmentWithinIdentificationWindow = (@@ -430,6 +432,9 @@ let preparePromise: Promise<void> | null = null;let ipcCleanups: Array<() => void> = [];let speakerIdentifications: SpeakerIdentification[] = [];let nextPlaceholderSpeakerIndex = 0;+// First speaker index this session owns; a resumed note's earlier sessions keep the+// ids below it. Main may raise it (see _resolveMeetingSpeakerIndexBase).+let sessionSpeakerIndexBase = 0;let systemPartialSpeakerIdValue: string | null = null;let recentSystemSpeaker: RecentSystemSpeaker | null = null;let speakerLocks: Map<string, string> = new Map();@@ -605,12 +610,24 @@ function reserveSpeakerIndex(speakerId?: string) {nextPlaceholderSpeakerIndex = Math.max(nextPlaceholderSpeakerIndex, idx + 1);}+function isEarlierSessionSpeaker(speakerId?: string): boolean {+ const idx = getSpeakerNumericIndex(speakerId);+ return idx != null && idx < sessionSpeakerIndexBase;+}++function adoptSpeakerIndexBase(base: number | undefined): void {+ if (base == null || base <= sessionSpeakerIndexBase) return;+ sessionSpeakerIndexBase = base;+ nextPlaceholderSpeakerIndex = Math.max(nextPlaceholderSpeakerIndex, base);+}+// Other-speaker cap is expectedCount - 1 (the mic track is "you"); mirrors the-// backend cap so live labels can't climb past the count the user expects.+// backend cap so live labels can't climb past the count the user expects. The+// cap counts this session's speakers, so it starts at the session's base.function mintPlaceholderSpeakerId(): string {const expected = useMeetingRecordingStore.getState().sessionExpectedCount;const cap = Math.max(1, expected - 1);- const index = Math.min(nextPlaceholderSpeakerIndex, cap - 1);+ const index = Math.min(nextPlaceholderSpeakerIndex, sessionSpeakerIndexBase + cap - 1);nextPlaceholderSpeakerIndex = Math.max(nextPlaceholderSpeakerIndex, index + 1);return `speaker_${index}`;}@@ -641,15 +658,15 @@ function assignProvisionalSpeaker(segment: TranscriptSegment): TranscriptSegment});}- const previousSystemSegment = [...segmentsRefValue]- .reverse()- .find(- (candidate) =>- candidate.source === "system" &&- candidate.speaker &&- candidate.timestamp != null &&- nowMs - candidate.timestamp <= SYSTEM_SPEAKER_CARRY_FORWARD_MS- );+ const previousSystemSegment = [...segmentsRefValue].reverse().find(+ (candidate) =>+ candidate.source === "system" &&+ candidate.speaker &&+ // A seeded row from a session that just ended is not this session's voice.+ !isEarlierSessionSpeaker(candidate.speaker) &&+ candidate.timestamp != null &&+ nowMs - candidate.timestamp <= SYSTEM_SPEAKER_CARRY_FORWARD_MS+ );if (previousSystemSegment?.speaker) {reserveSpeakerIndex(previousSystemSegment.speaker);@@ -821,7 +838,8 @@ export async function startRecording(args: StartRecordingArgs): Promise<boolean>segmentsRefValue = seed;sessionSeedLength = seed.length;speakerIdentifications = [];- nextPlaceholderSpeakerIndex = maxSpeakerIndex + 1;+ sessionSpeakerIndexBase = maxSpeakerIndex + 1;+ nextPlaceholderSpeakerIndex = sessionSpeakerIndexBase;recentSystemSpeaker = null;speakerLocks = locks;systemPartialSpeakerIdValue = null;@@ -949,6 +967,7 @@ export async function startRecording(args: StartRecordingArgs): Promise<boolean>sessionId,autoEndEligible: args.autoEndEligible,aecEnabled: getSettings().meetingAecEnabled,+ speakerIndexBase: sessionSpeakerIndexBase,});const micCapturePromise = getMeetingMicConstraints().then(async (constraints) => {if (!isCurrentStart()) return null;@@ -1037,6 +1056,7 @@ export async function startRecording(args: StartRecordingArgs): Promise<boolean>releaseSession();return;}+ adoptSpeakerIndexBase(startResult.speakerIndexBase);const systemAudioMode = startResult.systemAudioMode || initialSystemAudioAccess.mode;const systemAudioStrategy = startResult.systemAudioStrategy || initialSystemAudioStrategy;systemCaptureResult = await ensureRendererSystemAudioCapture({@@ -1273,7 +1293,8 @@ export async function startRecording(args: StartRecordingArgs): Promise<boolean>if (startResult.oneOnOneAttendee) {const synthetic: SpeakerIdentification = {- speakerId: "speaker_0",+ // Main's live identifier mints the session's first cluster at the base too.+ speakerId: `speaker_${sessionSpeakerIndexBase}`,displayName: startResult.oneOnOneAttendee.displayName,startTime: 0,endTime: Number.MAX_SAFE_INTEGER,diff --git a/src/types/electron.ts b/src/types/electron.tsindex 8738cebae..53546c23b 100644--- a/src/types/electron.ts+++ b/src/types/electron.ts@@ -3273,6 +3273,7 @@ declare global {sessionId: string;autoEndEligible: boolean;aecEnabled?: boolean;+ speakerIndexBase?: number;}) => Promise<{success: boolean;@@ -3281,6 +3282,7 @@ declare global {systemAudioMode?: SystemAudioMode;systemAudioStrategy?: SystemAudioStrategy;oneOnOneAttendee?: { displayName: string; email: string | null } | null;+ speakerIndexBase?: number;} & PolicyFailureMetadata>;meetingTranscriptionSend?: (buffer: ArrayBuffer, source: "mic" | "system") => void;diff --git a/test/helpers/apiKeyUpdatesIpc.test.js b/test/helpers/apiKeyUpdatesIpc.test.jsindex a7c6f3ab9..863bd9a02 100644--- a/test/helpers/apiKeyUpdatesIpc.test.js+++ b/test/helpers/apiKeyUpdatesIpc.test.js@@ -163,6 +163,9 @@ test.after(() => {Module._load = originalLoad;});+// Loaded after the electron stub above, and shared with ipcHandlers so tests can mock it.+const liveSpeakerIdentifier = require("../../src/helpers/liveSpeakerIdentifier");+function anything() {return new Proxy(function () {}, {get: (_target, property) => {@@ -587,3 +590,33 @@ test("a prepare after a mid-meeting renewal leaves the live meeting alone", asyn});assertMeetingStreams(renewed);});++// Main's base (past the note's stored ids), not the renderer's, numbers the live+// identifier, reaches the renderer, and is what each stop path hands diarization.+for (const options of [{ provider: "local" }, { provider: "deepgram-realtime", mode: "byok" }]) {+ test(`${options.provider}: a meeting's speaker index base reaches live identification, the renderer and diarization`, async (t) => {+ resetMeeting(t);+ invoke("save-deepgram-key", "A");+ const requestedBases = [];+ const diarizedBases = [];+ target._resolveMeetingSpeakerIndexBase = (_noteId, requestedBase) => {+ requestedBases.push(requestedBase);+ return 6;+ };+ target._startOrSkipDiarization = (...args) => diarizedBases.push(args[9]);+ t.after(() => {+ delete target._resolveMeetingSpeakerIndexBase;+ delete target._startOrSkipDiarization;+ });+ t.mock.method(liveSpeakerIdentifier, "isAvailable", () => true);+ const liveStart = t.mock.method(liveSpeakerIdentifier, "start", async () => false);++ const result = await invoke("meeting-transcription-start", { ...options, speakerIndexBase: 3 });+ assert.equal(result.success, true);+ assert.equal((await invoke("meeting-transcription-stop")).success, true);+ assert.deepEqual(requestedBases, [3]);+ assert.equal(liveStart.mock.calls[0].arguments[1].speakerIndexBase, 6);+ assert.equal(result.speakerIndexBase, 6);+ assert.deepEqual(diarizedBases, [6]);+ });+}diff --git a/test/helpers/diarizationSpeakerMerge.test.js b/test/helpers/diarizationSpeakerMerge.test.jsindex dd57f4b8f..df45d3310 100644--- a/test/helpers/diarizationSpeakerMerge.test.js+++ b/test/helpers/diarizationSpeakerMerge.test.js@@ -163,3 +163,21 @@ test("mic mode still dedupes bleed-flagged mic echoes of system text first", ()assert.equal(merged.length, 1);assert.equal(merged[0].source, "system");});++test("a resumed session's clusters are numbered from its speaker index base", () => {+ const manager = new DiarizationManager();++ const merged = manager.mergeWithTranscript(+ [systemSegment(1, "picking this back up"), systemSegment(11, "sounds good")],+ [+ { start: 0, end: 5, speaker: "speaker_00" },+ { start: 10, end: 15, speaker: "speaker_01" },+ ],+ { speakerIndexBase: 3 }+ );++ assert.deepEqual(+ merged.map((segment) => segment.speaker),+ ["speaker_3", "speaker_4"]+ );+});diff --git a/test/helpers/liveSpeakerIdentifier.test.js b/test/helpers/liveSpeakerIdentifier.test.jsindex 61dfdfad6..95da926d0 100644--- a/test/helpers/liveSpeakerIdentifier.test.js+++ b/test/helpers/liveSpeakerIdentifier.test.js@@ -317,6 +317,27 @@ test("isAvailable() stays true when the binding loads", (t) => {assert.equal(Boolean(identifier.isAvailable()), true);});+// A resumed note already holds earlier sessions' speaker ids, so a session+// started with a base mints past them; the next plain start begins at 0 again.+test("start() mints cluster ids from the session's speaker index base", async (t) => {+ const { LiveSpeakerIdentifier, restore } = loadIdentifier({+ ort: () => ({+ InferenceSession: { create: async () => ({ inputNames: [], outputNames: [] }) },+ }),+ });+ t.after(restore);+ const identifier = new LiveSpeakerIdentifier();+ stubDownloadedModels(identifier);++ assert.equal(await identifier.start({ speakerIndexBase: 3 }), true);+ assert.equal(identifier._assignSpeakerId(voiceA), "speaker_3");+ assert.equal(identifier._assignSpeakerId(ambiguousVoice), "speaker_4");+ await identifier.stop();++ assert.equal(await identifier.start(() => {}, {}), true);+ assert.equal(identifier._assignSpeakerId(voiceA), "speaker_0");+});+// onnxruntime-node defaults intra-op threads to every core with spinning// workers — absurd for the tiny VAD graph running ~31x/sec, and a CPU// regression during every meeting recording.diff --git a/test/helpers/meetingSpeakerReconcile.test.js b/test/helpers/meetingSpeakerReconcile.test.jsindex 8408d91be..442b9fc79 100644--- a/test/helpers/meetingSpeakerReconcile.test.js+++ b/test/helpers/meetingSpeakerReconcile.test.js@@ -248,3 +248,127 @@ test("mic mode counts the user among the expected speakers", () => {"the no-signal fallback caps at the default expected total, user included");});++// A resumed note's mappings and embeddings can hold ids no transcript row+// carries any more; the session's base must clear them too, or a new voice+// would inherit that stored name.+test("a resumed meeting's speaker base clears the note's stored ids", () => {+ const { IPCHandlers } = loadHandlers();+ const resolveBase = IPCHandlers.prototype._resolveMeetingSpeakerIndexBase;+ const handlers = {+ _diarizationSpeakerReservations: new Map(),+ databaseManager: {+ getSpeakerMappings: () => [{ speaker_id: "speaker_0" }, { speaker_id: "speaker_3" }],+ getNoteSpeakerEmbeddings: () => [{ speaker_id: "speaker_1" }],+ },+ };++ assert.equal(resolveBase.call(handlers, NOTE_ID, 2), 4);+ assert.equal(resolveBase.call(handlers, NOTE_ID, 6), 6);+ assert.equal(resolveBase.call(handlers, null, 2), 2);++ const failing = {+ _diarizationSpeakerReservations: new Map(),+ databaseManager: {+ getSpeakerMappings: () => {+ throw new Error("db closed");+ },+ },+ };+ assert.equal(resolveBase.call(failing, NOTE_ID, 2), 2);+});++function loadDiarizationManager() {+ const diarizationPath = require.resolve("../../src/helpers/diarization");+ delete require.cache[diarizationPath];+ Module._load = function loadWithElectronStub(request, parent, isMain) {+ if (request === "electron") return electronStub;+ return originalLoad.call(this, request, parent, isMain);+ };+ try {+ return require(diarizationPath);+ } finally {+ Module._load = originalLoad;+ }+}++// "Record more" can start while the previous session's diarization still runs:+// its clusters are in neither the seeded transcript nor the stored embeddings+// yet, so the next session's base must clear every id the result can name.+test("a stopped session's diarization reserves its speaker ids until its result lands", async (t) => {+ const { IPCHandlers } = loadHandlers();+ const DiarizationManager = loadDiarizationManager();+ const speakerEmbeddings = require("../../src/helpers/speakerEmbeddings");+ t.mock.method(speakerEmbeddings, "isAvailable", () => true);+ t.mock.method(speakerEmbeddings, "extractEmbedding", async () => new Float32Array([1, 0]));+ t.mock.method(speakerEmbeddings, "computeCentroid", (embeddings) => embeddings[0]);++ let releaseDiarization;+ const diarizationGate = new Promise((resolve) => {+ releaseDiarization = resolve;+ });+ const diarizationManager = new DiarizationManager();+ Object.assign(diarizationManager, {+ isAvailable: () => true,+ convertRawPcmToWav: async () => "/nonexistent/ow-reserve-test.wav",+ diarize: async () => {+ await diarizationGate;+ return [+ { start: 0, end: 4, speaker: "speaker_00" },+ { start: 5, end: 9, speaker: "speaker_01" },+ ];+ },+ });++ const sent = [];+ const win = {+ isDestroyed: () => false,+ webContents: { send: (channel, payload) => sent.push({ channel, payload }) },+ };+ const handlers = Object.assign(Object.create(IPCHandlers.prototype), {+ _diarizationSpeakerReservations: new Map(),+ speakerDiarizationEnabled: true,+ diarizationManager,+ databaseManager: {+ // Four expected speakers: up to three remote clusters.+ getNote: () => ({ expected_speaker_count: 4 }),+ getSpeakerProfiles: () => [],+ getSpeakerMappings: () => [],+ getNoteSpeakerEmbeddings: () => [],+ },+ });++ handlers._startOrSkipDiarization(+ "diar-1",+ "/nonexistent/ow-reserve-test.pcm",+ 0,+ [+ { id: "a", source: "system", timestamp: 1, text: "first", speaker: "speaker_2" },+ { id: "b", source: "system", timestamp: 6, text: "second", speaker: "speaker_2" },+ ],+ win,+ null,+ null,+ NOTE_ID,+ "system",+ 2+ );++ // Pending: the base must clear speaker_2..speaker_4.+ assert.equal(handlers._resolveMeetingSpeakerIndexBase(NOTE_ID, 3), 5);+ assert.equal(handlers._resolveMeetingSpeakerIndexBase(NOTE_ID + 1, 0), 0);++ releaseDiarization();+ for (let i = 0; i < 50 && sent.length === 0; i += 1) {+ await new Promise((resolve) => setImmediate(resolve));+ }++ const { payload } = sent[0];+ assert.deepEqual(+ payload.segments.map((segment) => segment.speaker),+ ["speaker_2", "speaker_3"]+ );+ assert.deepEqual(Object.keys(payload.speakerEmbeddings).sort(), ["speaker_2", "speaker_3"]);+ // Landed: narrowed to the two ids the result actually names.+ assert.equal(handlers._resolveMeetingSpeakerIndexBase(NOTE_ID, 0), 4);+});diff --git a/test/helpers/speakerIndexBase.test.js b/test/helpers/speakerIndexBase.test.jsnew file mode 100644index 000000000..65246b62e--- /dev/null+++ b/test/helpers/speakerIndexBase.test.js@@ -0,0 +1,44 @@+const test = require("node:test");+const assert = require("node:assert/strict");+const {+ speakerIndexOf,+ resolveSpeakerIndexBase,+ buildSpeakerRenumber,+} = require("../../src/helpers/speakerIndexBase");++test("speakerIndexOf reads only exact speaker_N ids", () => {+ assert.equal(speakerIndexOf("speaker_0"), 0);+ assert.equal(speakerIndexOf("speaker_12"), 12);+ assert.equal(speakerIndexOf("you"), null);+ assert.equal(speakerIndexOf("speaker_1a"), null);+ assert.equal(speakerIndexOf(undefined), null);+});++test("a fresh meeting starts at 0", () => {+ assert.equal(resolveSpeakerIndexBase(undefined, []), 0);+ assert.equal(resolveSpeakerIndexBase(0, []), 0);+});++test("the base clears every stored id, not just the requested one", () => {+ assert.equal(resolveSpeakerIndexBase(2, ["speaker_0", "speaker_1"]), 2);+ // A mapping left on a merged-away cluster still names whoever reuses its id.+ assert.equal(resolveSpeakerIndexBase(2, ["speaker_0", "speaker_4", "you"]), 5);+});++test("an invalid requested base degrades to the stored ids", () => {+ for (const requested of [-3, 1.5, Number.NaN, null]) {+ assert.equal(resolveSpeakerIndexBase(requested, ["speaker_1"]), 2, String(requested));+ }+});++test("clusters are renumbered in first-seen order from the base", () => {+ const renumber = buildSpeakerRenumber(["speaker_01", "speaker_00"], 3);+ assert.deepEqual(+ [...renumber.entries()],+ [+ ["speaker_01", "speaker_3"],+ ["speaker_00", "speaker_4"],+ ]+ );+ assert.equal(buildSpeakerRenumber(["a"], 0).get("a"), "speaker_0");+});diff --git a/test/stores/meetingRecordingStoreResumeSpeakerIds.test.js b/test/stores/meetingRecordingStoreResumeSpeakerIds.test.jsnew file mode 100644index 000000000..cedaccd89--- /dev/null+++ b/test/stores/meetingRecordingStoreResumeSpeakerIds.test.js@@ -0,0 +1,217 @@+const test = require("node:test");+const assert = require("node:assert/strict");+const {+ createRendererServer,+ installBrowserGlobals,+ installMicCaptureGlobals,+} = require("../lib/rendererTestHarness");++// "Record more" seeds a recording with the note's earlier transcript. Speaker+// ids key the note's names, locks, mappings and embeddings, so the resumed+// session's speakers must never reuse an earlier session's id.++const NOTE_ID = 21;+const START_ARGS = {+ noteId: NOTE_ID,+ noteTitle: "Planning",+ folderId: null,+ autoEndEligible: true,+};++function createElectronAPI({ startResult = {} } = {}) {+ const noopListener = () => () => {};+ const state = { startOptions: [], segmentCallback: null };+ const api = {+ checkSystemAudioAccess: async () => ({+ granted: true,+ status: "granted",+ mode: "native",+ strategy: "native",+ }),+ meetingTranscriptionStart: async (options) => {+ state.startOptions.push(options);+ return {+ success: true,+ systemAudioMode: "native",+ systemAudioStrategy: "native",+ speakerIndexBase: options.speakerIndexBase,+ ...startResult,+ };+ },+ meetingTranscriptionSetSystemAudioAvailable: async () => ({ success: true }),+ meetingTranscriptionStop: async () => ({ success: true, diarizationSessionId: null }),+ meetingTranscriptionSend: () => {},+ getNote: async () => null,+ updateNote: async () => ({ success: true }),+ onMeetingDiarizationComplete: noopListener,+ onMeetingTranscriptionSegment: (callback) => {+ state.segmentCallback = callback;+ return () => {+ state.segmentCallback = null;+ };+ },+ onMeetingSpeakerIdentified: noopListener,+ onMeetingSpeakersMerged: noopListener,+ onMeetingSessionSpeakerConfigUpdated: noopListener,+ onMeetingTranscriptionError: noopListener,+ onMeetingTranscriptionFatalError: noopListener,+ onMeetingSystemAudioSilent: noopListener,+ };+ return { api, state };+}++async function loadStore(t, api) {+ installMicCaptureGlobals(t);+ installBrowserGlobals(t, {+ window: { electronAPI: api, setTimeout: (fn, ms) => setTimeout(fn, ms) },+ });+ const vite = await createRendererServer(t, {+ cachePrefix: "openwhispr-meeting-resume-speaker-ids-test-",+ });+ return vite.ssrLoadModule("/stores/meetingRecordingStore.ts");+}++// Seeded rows end "just now", inside the 8 s carry-forward window, so a+// resumed row would inherit the last seeded speaker if nothing stopped it.+function earlierSession() {+ const now = Date.now();+ return [+ {+ id: "s0",+ text: "let's start",+ source: "system",+ timestamp: now - 2_000,+ speaker: "speaker_0",+ speakerName: "Ada",+ speakerLocked: true,+ speakerIsPlaceholder: false,+ },+ {+ id: "s1",+ text: "agreed",+ source: "system",+ timestamp: now - 1_000,+ speaker: "speaker_1",+ speakerIsPlaceholder: true,+ },+ ];+}++function emitSystemFinal(state, text) {+ state.segmentCallback({ text, source: "system", type: "final", timestamp: Date.now() });+}++const segmentsOf = (store) => store.useMeetingRecordingStore.getState().segments;+const segmentWithText = (store, text) => segmentsOf(store).find((s) => s.text === text);++test("a fresh meeting numbers its speakers from speaker_0", async (t) => {+ const { api, state } = createElectronAPI();+ const store = await loadStore(t, api);++ assert.equal(await store.startRecording(START_ARGS), true);+ assert.equal(state.startOptions[0].speakerIndexBase, 0);++ emitSystemFinal(state, "hello");+ assert.equal(segmentWithText(store, "hello").speaker, "speaker_0");+ await store.stopRecording();+});++test("a resumed recording gives its speakers ids past the earlier session's", async (t) => {+ const { api, state } = createElectronAPI();+ const store = await loadStore(t, api);++ assert.equal(await store.startRecording({ ...START_ARGS, seedSegments: earlierSession() }), true);+ assert.equal(state.startOptions[0].speakerIndexBase, 2);++ emitSystemFinal(state, "picking back up");+ const resumed = segmentWithText(store, "picking back up");+ // Two expected speakers cap placeholders at one other voice; that cap counts+ // this session's speakers, so it must not clamp back to speaker_0.+ assert.equal(resumed.speaker, "speaker_2");+ assert.notEqual(resumed.speakerName, "Ada");+ assert.notEqual(resumed.speakerLocked, true);++ const seeded = segmentWithText(store, "let's start");+ assert.equal(seeded.speaker, "speaker_0");+ assert.equal(seeded.speakerName, "Ada");+ await store.stopRecording();+});++test("main can raise the base past ids only the note's stored mappings use", async (t) => {+ const { api, state } = createElectronAPI({ startResult: { speakerIndexBase: 5 } });+ const store = await loadStore(t, api);++ assert.equal(await store.startRecording({ ...START_ARGS, seedSegments: earlierSession() }), true);+ emitSystemFinal(state, "new voice");+ assert.equal(segmentWithText(store, "new voice").speaker, "speaker_5");+ await store.stopRecording();+});++test("a lower base from main never pulls the session back onto earlier ids", async (t) => {+ const { api, state } = createElectronAPI({ startResult: { speakerIndexBase: 0 } });+ const store = await loadStore(t, api);++ assert.equal(await store.startRecording({ ...START_ARGS, seedSegments: earlierSession() }), true);+ emitSystemFinal(state, "new voice");+ assert.equal(segmentWithText(store, "new voice").speaker, "speaker_2");+ await store.stopRecording();+});++test("a 1:1 attendee is bound to the resumed session's first speaker id", async (t) => {+ const { api, state } = createElectronAPI({+ startResult: { oneOnOneAttendee: { displayName: "Grace", email: null } },+ });+ const store = await loadStore(t, api);++ assert.equal(await store.startRecording({ ...START_ARGS, seedSegments: earlierSession() }), true);+ emitSystemFinal(state, "hi again");+ const resumed = segmentWithText(store, "hi again");+ assert.equal(resumed.speaker, "speaker_2");+ assert.equal(resumed.speakerName, "Grace");+ assert.equal(segmentWithText(store, "agreed").speakerName, undefined);+ await store.stopRecording();+});++test("resuming twice keeps every session's speaker ids distinct", async (t) => {+ const { api, state } = createElectronAPI();+ const store = await loadStore(t, api);++ assert.equal(await store.startRecording({ ...START_ARGS, seedSegments: earlierSession() }), true);+ emitSystemFinal(state, "second session");+ await store.stopRecording();+ const afterSecond = segmentsOf(store);+ assert.equal(segmentWithText(store, "second session").speaker, "speaker_2");++ assert.equal(await store.startRecording({ ...START_ARGS, seedSegments: afterSecond }), true);+ assert.equal(state.startOptions[1].speakerIndexBase, 3);+ emitSystemFinal(state, "third session");+ assert.equal(segmentWithText(store, "third session").speaker, "speaker_3");+ assert.equal(segmentWithText(store, "second session").speaker, "speaker_2");+ await store.stopRecording();+});++test("renaming a speaker during a resumed recording touches only that session's rows", async (t) => {+ const { api, state } = createElectronAPI();+ const store = await loadStore(t, api);++ assert.equal(await store.startRecording({ ...START_ARGS, seedSegments: earlierSession() }), true);+ emitSystemFinal(state, "new voice");+ const newSpeaker = segmentWithText(store, "new voice").speaker;++ store.lockSpeaker(newSpeaker, "Bob");+ assert.equal(segmentWithText(store, "new voice").speakerName, "Bob");+ assert.equal(segmentWithText(store, "let's start").speakerName, "Ada");+ assert.equal(segmentWithText(store, "agreed").speakerName, undefined);+ assert.notEqual(segmentWithText(store, "agreed").speakerLocked, true);++ // Renaming an earlier session's speaker leaves the new voice alone.+ store.lockSpeaker("speaker_1", "Carol");+ assert.equal(segmentWithText(store, "agreed").speakerName, "Carol");+ assert.equal(segmentWithText(store, "new voice").speakerName, "Bob");++ // Later rows from the renamed voice keep its lock.+ emitSystemFinal(state, "still me");+ assert.equal(segmentWithText(store, "still me").speaker, newSpeaker);+ assert.equal(segmentWithText(store, "still me").speakerName, "Bob");+ await store.stopRecording();+});