Skip to content

Speakr is a personal, self-hosted web application designed for transcribing audio recordings

Python

Keep the next recording safe when the previous upload finishes (#407): clear only the crash-recovery session the uploaded recording was saved under, and do not switch to the uploaded (or incognito) recording while the recorder is in use

Murtaza Nasir86d68b3024f8
diff --git a/static/js/modules/composables/audio.js b/static/js/modules/composables/audio.js
index 74ab4ec2..ca96ce1d 100644
--- a/static/js/modules/composables/audio.js
+++ b/static/js/modules/composables/audio.js
@@ -144,6 +144,9 @@ export function useAudio(state, utils) {
//
// None of this is exposed on the shared state surface yet; the UI to
// monitor sync backlog lands in Phase C.
+ // startTime of the crash-recovery session this recording is saved under,
+ // handed to the upload so only that session is cleared afterwards (#407).
+ let currentRecordingSessionStartTime = null;
let serverSessionId = null;
let serverSessionUploader = null;
let serverSessionMimeType = 'audio/webm';
@@ -905,8 +908,9 @@ export function useAudio(state, utils) {
}
// Start IndexedDB recording session - convert Vue reactive objects to plain objects
+ currentRecordingSessionStartTime = null;
try {
- await RecordingDB.startRecordingSession({
+ const savedSession = await RecordingDB.startRecordingSession({
mode,
notes: recordingNotes.value || '',
tags: selectedTagIds.value ? [...selectedTagIds.value] : [], // Convert reactive array to plain array
@@ -921,6 +925,7 @@ export function useAudio(state, utils) {
mimeType,
incognito: !!(incognitoMode && incognitoMode.value)
});
+ currentRecordingSessionStartTime = savedSession ? savedSession.startTime : null;
} catch (dbError) {
console.warn('[Recording] IndexedDB persistence failed, continuing without persistence:', dbError);
}
@@ -1344,6 +1349,7 @@ export function useAudio(state, utils) {
recordingId: null,
clientId: queueClientId,
fromInProgressRecording: true, // marker: upload-success handler clears RecordingDB session
+ recordingSessionStartTime: currentRecordingSessionStartTime, // clear only this session (#407)
error: null,
willAutoSummarize: false // Server will tell us via SUMMARIZING status
});
@@ -1827,6 +1833,7 @@ export function useAudio(state, utils) {
// Restore chunks
audioChunks.value = recovered.chunks;
+ currentRecordingSessionStartTime = recovered.metadata.startTime || null;
// Create blob URL. Also restore the recording's mime type so the
// legacy upload path builds the File with the right container
diff --git a/static/js/modules/composables/upload.js b/static/js/modules/composables/upload.js
index dd0736a7..54c0a1b8 100644
--- a/static/js/modules/composables/upload.js
+++ b/static/js/modules/composables/upload.js
@@ -9,6 +9,7 @@ import * as RecordingDB from '../db/recording-persistence.js';
import { getUploadCsrfToken, isCsrfRejection } from '../csrf.js';
import { shouldSliceUpload, uploadFileInSlices } from '../db/sliced-file-upload.js';
import { computeUploadTimeout } from '../utils/upload-timeout.js';
+import { recorderInUse } from '../utils/recorder-state.js';
import {
sortQueuedForJoin, moveQueuedItem, placeQueuedItem, totalJoinDuration,
newJoinGroupId, isVideoFileName, MAX_JOIN_FILES,
@@ -59,7 +60,8 @@ export function useUpload(state, utils) {
// View state
currentView, showUploadModal,
// Upload disclaimer state
- uploadDisclaimer, showUploadDisclaimerModal
+ uploadDisclaimer, showUploadDisclaimerModal,
+ isRecording
} = state;
const { computed, nextTick, ref, markRaw } = Vue;
@@ -820,9 +822,13 @@ export function useUpload(state, utils) {
// #287(b)): the audio is on the server. Only do this for queue
// items that came from an in-app recording, to avoid touching the
// session for file-drag-drop uploads that never wrote one.
+ //
+ // Only the session this recording was saved under is cleared: the
+ // upload can finish after the user has started the next recording,
+ // whose crash-recovery copy must survive (#407).
if (fileItem.fromInProgressRecording) {
try {
- await RecordingDB.clearRecordingSession();
+ await RecordingDB.clearRecordingSessionIfStartedAt(fileItem.recordingSessionStartTime);
} catch (dbError) {
console.warn('[Upload] Failed to clear recording session after successful upload:', dbError);
}
@@ -858,7 +864,11 @@ export function useUpload(state, utils) {
// audio.js's recording-stop path) so bulk drag-drop
// uploads of many files still leave the user wherever
// they were.
- if (fileItem.fromInProgressRecording) {
+ //
+ // Not while the next recording is under way (#407): the upload can
+ // finish minutes after the user started recording again, and
+ // switching views then took them off the recorder.
+ if (fileItem.fromInProgressRecording && !recorderInUse(isRecording, currentView)) {
selectedRecording.value = data;
currentView.value = 'detail';
if (showUploadModal) showUploadModal.value = false;
@@ -1198,9 +1208,13 @@ export function useUpload(state, utils) {
processingProgress.value = 100;
processingMessage.value = t('incognito.recordingReady');
- // Auto-select the incognito recording and switch to detail view
- selectedRecording.value = incognitoData;
- currentView.value = 'detail';
+ // Auto-select the incognito recording and switch to detail view,
+ // unless the next recording is already under way (#407); it stays
+ // available from the incognito entry in the sidebar.
+ if (!recorderInUse(isRecording, currentView)) {
+ selectedRecording.value = incognitoData;
+ currentView.value = 'detail';
+ }
// Dismiss the upload modal if it was still open behind the
// success transition.
if (showUploadModal) showUploadModal.value = false;
diff --git a/static/js/modules/db/recording-persistence.js b/static/js/modules/db/recording-persistence.js
index d68f4b37..aba27c9e 100644
--- a/static/js/modules/db/recording-persistence.js
+++ b/static/js/modules/db/recording-persistence.js
@@ -286,6 +286,32 @@ export const recoverRecording = async () => {
}
};
+/**
+ * Clear the saved session only if it is still the one that started at
+ * `startTime` (#407). An upload can finish after the next recording has
+ * started; clearing unconditionally then deleted the new recording's
+ * crash-recovery copy. Returns true when a session was cleared.
+ */
+export const clearRecordingSessionIfStartedAt = (startTime) => serializeSessionWrite(async () => {
+ if (!startTime) return false;
+ try {
+ const db = await initDB();
+ const transaction = db.transaction([STORE_NAME], 'readwrite');
+ const objectStore = transaction.objectStore(STORE_NAME);
+ const session = await promisifyRequest(objectStore.get('current'));
+ if (!session || session.startTime !== startTime) {
+ console.log('[RecordingDB] Saved session belongs to a newer recording; kept');
+ return false;
+ }
+ await promisifyRequest(objectStore.delete('current'));
+ console.log('[RecordingDB] Recording session cleared');
+ return true;
+ } catch (error) {
+ console.error('[RecordingDB] Failed to clear session:', error);
+ return false;
+ }
+});
+
/**
* Clear recording session (after successful upload or discard)
*/
diff --git a/static/js/modules/db/recording-persistence.test.js b/static/js/modules/db/recording-persistence.test.js
index dd4f958b..d1ff2fba 100644
--- a/static/js/modules/db/recording-persistence.test.js
+++ b/static/js/modules/db/recording-persistence.test.js
@@ -408,4 +408,32 @@ describe('recording-persistence', () => {
expect(await mod.getDatabaseSize()).toBeNull();
});
});
+
+ describe('clearRecordingSessionIfStartedAt (#407)', () => {
+ it('clears the session that started at the given time', async () => {
+ const session = await mod.startRecordingSession({ mode: 'meeting' });
+ expect(await mod.clearRecordingSessionIfStartedAt(session.startTime)).toBe(true);
+ expect(mock.stores.get(STORE_NAME).get('current')).toBeUndefined();
+ });
+
+ it('keeps a newer recording that started after the uploaded one', async () => {
+ vi.useFakeTimers();
+ try {
+ vi.setSystemTime(new Date('2026-09-30T10:00:00Z'));
+ const first = await mod.startRecordingSession({ mode: 'meeting' });
+ vi.setSystemTime(new Date('2026-09-30T10:05:00Z'));
+ const second = await mod.startRecordingSession({ mode: 'meeting' });
+ expect(await mod.clearRecordingSessionIfStartedAt(first.startTime)).toBe(false);
+ expect(mock.stores.get(STORE_NAME).get('current').startTime).toBe(second.startTime);
+ } finally {
+ vi.useRealTimers();
+ }
+ });
+
+ it('does nothing without a start time', async () => {
+ await mod.startRecordingSession({ mode: 'meeting' });
+ expect(await mod.clearRecordingSessionIfStartedAt(null)).toBe(false);
+ expect(mock.stores.get(STORE_NAME).get('current')).toBeDefined();
+ });
+ });
});
diff --git a/static/js/modules/utils/recorder-state.js b/static/js/modules/utils/recorder-state.js
new file mode 100644
index 00000000..ba34e14c
--- /dev/null
+++ b/static/js/modules/utils/recorder-state.js
@@ -0,0 +1,9 @@
+/**
+ * Whether the recorder is in use: recording, paused, or on the recording
+ * screen. Work that finishes in the background (an upload of the previous
+ * recording, an incognito transcript) must not switch views then, or the
+ * user is taken off the recorder mid-recording (#407).
+ */
+export function recorderInUse(isRecording, currentView) {
+ return !!((isRecording && isRecording.value) || (currentView && currentView.value === 'recording'));
+}
diff --git a/static/js/modules/utils/recorder-state.test.js b/static/js/modules/utils/recorder-state.test.js
new file mode 100644
index 00000000..72e53fcc
--- /dev/null
+++ b/static/js/modules/utils/recorder-state.test.js
@@ -0,0 +1,18 @@
+import { describe, it, expect } from 'vitest';
+import { recorderInUse } from './recorder-state.js';
+
+const ref = (value) => ({ value });
+
+describe('recorderInUse', () => {
+ it('is true while recording, wherever the user is', () => {
+ expect(recorderInUse(ref(true), ref('detail'))).toBe(true);
+ });
+ it('is true on the recording screen even when paused or not yet started', () => {
+ expect(recorderInUse(ref(false), ref('recording'))).toBe(true);
+ });
+ it('is false otherwise, or when the refs are missing', () => {
+ expect(recorderInUse(ref(false), ref('detail'))).toBe(false);
+ expect(recorderInUse(ref(false), ref(null))).toBe(false);
+ expect(recorderInUse(undefined, undefined)).toBe(false);
+ });
+});