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: stop cloud sync from wiping locally-edited notes with stale empty cloud copies (#1291)
Joshuafa47857ab763
diff --git a/.github/workflows/build-and-notarize.yml b/.github/workflows/build-and-notarize.ymlindex 6ef1c6a58..df4e1c386 100644--- a/.github/workflows/build-and-notarize.yml+++ b/.github/workflows/build-and-notarize.yml@@ -25,6 +25,10 @@ jobs:node-version-file: .nvmrc- name: Install dependenciesrun: npm ci --ignore-scripts+ - name: Build better-sqlite3 for the runner's node+ # --ignore-scripts skips the native build; DB-backed tests silently+ # skip without it. This job is the only test gate on push to main.+ run: npm rebuild better-sqlite3- name: Run testsrun: npm testenv:diff --git a/.github/workflows/tests.yml b/.github/workflows/tests.ymlindex 25dcf1679..3c5e9d7f5 100644--- a/.github/workflows/tests.yml+++ b/.github/workflows/tests.yml@@ -13,6 +13,12 @@ jobs:node-version-file: .nvmrc- name: Install dependenciesrun: npm ci --ignore-scripts+ - name: Build better-sqlite3 for the runner's node+ # --ignore-scripts skips the native build, so every DB-backed test+ # (snippetsDatabase, noteCloudUpsertGuard, …) silently skips without this.+ run: npm rebuild better-sqlite3+ - name: Typecheck+ run: npm run typecheck- name: Run testsrun: npm testenv:diff --git a/src/helpers/cloudSyncGuards.js b/src/helpers/cloudSyncGuards.jsnew file mode 100644index 000000000..8dc5564ee--- /dev/null+++ b/src/helpers/cloudSyncGuards.js@@ -0,0 +1,45 @@+// Sync guards shared by SyncService and the node --test suite (#1290).++// SQLite `datetime('now')` yields "YYYY-MM-DD HH:MM:SS" (no T, no millis, no Z);+// the cloud sends ISO 8601 "YYYY-MM-DDTHH:MM:SS.sssZ". Normalize both to+// millis-precision ISO so the pull loop's lexical greater-than compares+// correctly — without the ".000" pad a whole-second local value sorts after a+// sub-second cloud value at the same instant ('Z' > '.').+export function normalizeTimestamp(value) {+ if (!value) return "";+ const iso = value.replace(" ", "T").replace(/Z$/, "");+ return (/\.\d+$/.test(iso) ? iso : `${iso}.000`) + "Z";+}++// Last-writer-wins gate for the pull loops. A raw compare hands every+// same-UTC-day conflict to the cloud copy ('T' > ' ' at index 10), which is+// how a staler empty cloud shell overwrote fresh local notes (#1290).+export function isCloudEntryNewer(cloudUpdatedAt, localUpdatedAt) {+ return normalizeTimestamp(cloudUpdatedAt) > normalizeTimestamp(localUpdatedAt);+}++// Full-content PATCH payload for an existing cloud note — a content-less+// PATCH still bumps the cloud row's updated_at, so the next pull elects a+// copy that never received the local edit (#1290). No client_note_id here:+// legacy cloud notes carry a different backfilled UUID per device, so+// PATCHing it on every push would fork duplicate notes; only the one-shot+// migration branch sends it.+export function buildNoteUpdatePayload(note, folderMap) {+ return {+ title: note.title,+ content: note.content,+ enhanced_content: note.enhanced_content,+ enhancement_prompt: note.enhancement_prompt,+ enhanced_at_content_hash: note.enhanced_at_content_hash,+ note_type: note.note_type,+ source_file: note.source_file,+ audio_duration_seconds: note.audio_duration_seconds,+ transcript: note.transcript,+ participants: note.participants,+ calendar_event_id: note.calendar_event_id,+ diarization_enabled: note.diarization_enabled,+ expected_speaker_count: note.expected_speaker_count,+ folder_id: note.folder_id ? (folderMap.get(note.folder_id) ?? null) : null,+ updated_at: note.updated_at,+ };+}diff --git a/src/helpers/database.js b/src/helpers/database.jsindex f5083a85b..e6c50bd64 100644--- a/src/helpers/database.js+++ b/src/helpers/database.js@@ -2714,6 +2714,9 @@ class DatabaseManager {upsertNoteFromCloud(cloudNote, localFolderId) {try {if (!this.db) throw new Error("Database not initialized");+ // Sync must never replace non-empty local content/enhanced_content/+ // transcript with an empty cloud value (#1290, the #938 invariant).+ // The enhancement prompt/hash travel with enhanced_content.const stmt = this.db.prepare(`INSERT INTO notes (client_note_id, cloud_id, title, content, enhanced_content,enhancement_prompt, enhanced_at_content_hash, note_type, source_file,@@ -2723,11 +2726,21 @@ class DatabaseManager {ON CONFLICT(client_note_id) DO UPDATE SETcloud_id = excluded.cloud_id,title = excluded.title,- content = excluded.content,- enhanced_content = excluded.enhanced_content,- enhancement_prompt = excluded.enhancement_prompt,- enhanced_at_content_hash = excluded.enhanced_at_content_hash,- transcript = excluded.transcript,+ content = CASE+ WHEN COALESCE(excluded.content, '') = '' AND COALESCE(content, '') <> ''+ THEN content ELSE excluded.content END,+ enhanced_content = CASE+ WHEN COALESCE(excluded.enhanced_content, '') = '' AND COALESCE(enhanced_content, '') <> ''+ THEN enhanced_content ELSE excluded.enhanced_content END,+ enhancement_prompt = CASE+ WHEN COALESCE(excluded.enhanced_content, '') = '' AND COALESCE(enhanced_content, '') <> ''+ THEN enhancement_prompt ELSE excluded.enhancement_prompt END,+ enhanced_at_content_hash = CASE+ WHEN COALESCE(excluded.enhanced_content, '') = '' AND COALESCE(enhanced_content, '') <> ''+ THEN enhanced_at_content_hash ELSE excluded.enhanced_at_content_hash END,+ transcript = CASE+ WHEN COALESCE(excluded.transcript, '') = '' AND COALESCE(transcript, '') <> ''+ THEN transcript ELSE excluded.transcript END,folder_id = excluded.folder_id,participants = COALESCE(excluded.participants, participants),calendar_event_id = COALESCE(excluded.calendar_event_id, calendar_event_id),diff --git a/src/services/SyncService.ts b/src/services/SyncService.tsindex 8c4a9f85d..965707d7d 100644--- a/src/services/SyncService.ts+++ b/src/services/SyncService.ts@@ -11,6 +11,11 @@ import { TranscriptionsService } from "./TranscriptionsService.js";import { DictionaryService } from "./DictionaryService.js";import { SnippetService, type CloudSnippetEntry } from "./SnippetService.js";import { CloudApiError } from "./cloudApi.js";+import {+ normalizeTimestamp,+ isCloudEntryNewer,+ buildNoteUpdatePayload,+} from "../helpers/cloudSyncGuards.js";function isHttpStatus(err: unknown, status: number): boolean {return err instanceof CloudApiError && err.status === status;@@ -32,17 +37,6 @@ const SYNC_ALL_LOCK = "openwhispr-sync-all";// may have just become possible (sign-in, subscription, backup enabled).const CAN_SYNC_KEYS = ["isSignedIn", "cloudBackupEnabled", "isSubscribed"];-// SQLite `datetime('now')` yields "YYYY-MM-DD HH:MM:SS" (no T, no millis, no Z);-// the cloud sends ISO 8601 "YYYY-MM-DDTHH:MM:SS.sssZ". Normalize both to-// millis-precision ISO so the pull loop's lexical greater-than compares-// correctly — without the ".000" pad a whole-second local value sorts after a-// sub-second cloud value at the same instant ('Z' > '.').-function normalizeTimestamp(value: string | null | undefined): string {- if (!value) return "";- const iso = value.replace(" ", "T").replace(/Z$/, "");- return (/\.\d+$/.test(iso) ? iso : `${iso}.000`) + "Z";-}-class SyncService {private syncing = false;private syncAllPending = false;@@ -241,23 +235,7 @@ class SyncService {const cloudFolderId = note.folder_id ? (folderMap.get(note.folder_id) ?? null) : null;if (note.cloud_id) {- await NotesService.update(note.cloud_id, {- title: note.title,- content: note.content,- enhanced_content: note.enhanced_content,- enhancement_prompt: note.enhancement_prompt,- enhanced_at_content_hash: note.enhanced_at_content_hash,- note_type: note.note_type,- source_file: note.source_file,- audio_duration_seconds: note.audio_duration_seconds,- transcript: note.transcript,- participants: note.participants,- calendar_event_id: note.calendar_event_id,- diarization_enabled: note.diarization_enabled,- expected_speaker_count: note.expected_speaker_count,- folder_id: cloudFolderId,- updated_at: note.updated_at,- });+ await NotesService.update(note.cloud_id, buildNoteUpdatePayload(note, folderMap));} else {const cloud = await NotesService.create({client_note_id: note.client_note_id,@@ -480,7 +458,7 @@ class SyncService {}if (local?.deleted_at) continue;- if (!local || cloudFolder.updated_at > local.updated_at) {+ if (!local || isCloudEntryNewer(cloudFolder.updated_at, local.updated_at)) {await window.electronAPI.upsertFolderFromCloud?.(cloudFolder as unknown as Record<string, unknown>);@@ -509,7 +487,13 @@ class SyncService {for (const note of migration) {try {- await NotesService.update(note.cloud_id!, { client_note_id: note.client_note_id });+ // Full content, not just { client_note_id }: a content-less PATCH bumps+ // the cloud row's updated_at without uploading the local edit, and the+ // pull in this same pass then elects the still-empty cloud copy (#1290).+ await NotesService.update(note.cloud_id!, {+ client_note_id: note.client_note_id,+ ...buildNoteUpdatePayload(note, folderMap),+ });await window.electronAPI.markNoteSynced?.(note.id, note.cloud_id!);} catch {await window.electronAPI.markNoteSyncError?.(note.id);@@ -583,7 +567,7 @@ class SyncService {continue;}- if (!local || cloudNote.updated_at > local.updated_at) {+ if (!local || isCloudEntryNewer(cloudNote.updated_at, local.updated_at)) {const localFolderId = cloudNote.folder_id? (cloudToLocal.get(cloudNote.folder_id) ?? defaultFolderId): defaultFolderId;@@ -689,7 +673,7 @@ class SyncService {continue;}- if (!local || cloudConv.updated_at > local.updated_at) {+ if (!local || isCloudEntryNewer(cloudConv.updated_at, local.updated_at)) {await window.electronAPI.upsertConversationFromCloud?.(cloudConv as unknown as Record<string, unknown>,(cloudConv.messages ?? []) as unknown as Array<Record<string, unknown>>diff --git a/test/helpers/cloudSyncGuards.test.js b/test/helpers/cloudSyncGuards.test.jsnew file mode 100644index 000000000..e05b80f3d--- /dev/null+++ b/test/helpers/cloudSyncGuards.test.js@@ -0,0 +1,109 @@+const test = require("node:test");+const assert = require("node:assert/strict");++const load = () => import("../../src/helpers/cloudSyncGuards.js");++// Regression for #1290: local rows carry SQLite format ("2026-07-22 08:47:00")+// while cloud rows are ISO ("2026-07-22T08:18:27.790Z"), so a raw lexical `>`+// let any same-UTC-day cloud copy beat any local edit and wipe it on pull.++test("a 29-minute-older cloud copy is not judged newer than a local edit", async () => {+ const { isCloudEntryNewer } = await load();+ // Raw lexical compare (the old gate) elects the older cloud copy:+ assert.equal("2026-07-22T08:18:00.000Z" > "2026-07-22 08:47:00", true);+ // The normalized gate must not:+ assert.equal(isCloudEntryNewer("2026-07-22T08:18:00.000Z", "2026-07-22 08:47:00"), false);+});++test("a whole-day-staler cloud copy is not judged newer", async () => {+ const { isCloudEntryNewer } = await load();+ assert.equal(isCloudEntryNewer("2026-07-22T00:00:00.000Z", "2026-07-22 23:59:59"), false);+});++test("a previous-day cloud copy still loses (control)", async () => {+ const { isCloudEntryNewer } = await load();+ assert.equal(isCloudEntryNewer("2026-07-21T23:59:59.000Z", "2026-07-22 08:47:00"), false);+});++test("a genuinely newer cloud copy still wins (last-writer-wins intact)", async () => {+ const { isCloudEntryNewer } = await load();+ assert.equal(isCloudEntryNewer("2026-07-22T08:47:01.000Z", "2026-07-22 08:47:00"), true);+ assert.equal(isCloudEntryNewer("2026-07-23T00:00:00.000Z", "2026-07-22 23:59:59"), true);+});++test("the same instant across formats is a tie, not a cloud win", async () => {+ const { isCloudEntryNewer } = await load();+ assert.equal(isCloudEntryNewer("2026-07-22T08:47:00.000Z", "2026-07-22 08:47:00"), false);+});++test("sub-second cloud precision beats a whole-second local value at the same second", async () => {+ const { isCloudEntryNewer } = await load();+ assert.equal(isCloudEntryNewer("2026-07-22T08:47:00.500Z", "2026-07-22 08:47:00"), true);+});++test("a missing local timestamp always yields to the cloud value", async () => {+ const { isCloudEntryNewer } = await load();+ assert.equal(isCloudEntryNewer("2026-07-22T08:47:00.000Z", ""), true);+ assert.equal(isCloudEntryNewer("2026-07-22T08:47:00.000Z", null), true);+});++// Regression for #1290's wipe engine: pushPendingNotes' migration branch+// PATCHed only { client_note_id }, bumping the cloud row's updated_at without+// uploading the local edit — the same pass then pulled the empty row back down.++const localNote = {+ id: 7,+ client_note_id: "client-uuid-1",+ cloud_id: "cloud-1",+ title: "Vision, Values, and Product Priorities",+ content: "REAL MEETING NOTES",+ enhanced_content: "ENHANCED NOTES",+ enhancement_prompt: "prompt",+ enhanced_at_content_hash: "hash-1",+ note_type: "meeting",+ source_file: null,+ audio_duration_seconds: 3130,+ transcript: '[{"text":"hello"}]',+ participants: '[{"email":"[email protected]"}]',+ calendar_event_id: "cal-ev-1",+ diarization_enabled: 1,+ expected_speaker_count: 2,+ folder_id: 3,+ sync_status: "pending",+ created_at: "2026-07-21 19:31:00",+ updated_at: "2026-07-22 08:47:00",+};++test("the note update payload carries the full content, not just identifiers", async () => {+ const { buildNoteUpdatePayload } = await load();+ const payload = buildNoteUpdatePayload(localNote, new Map([[3, "cloud-folder-3"]]));++ // Only the one-shot migration branch may send client_note_id+ // (see buildNoteUpdatePayload):+ assert.equal("client_note_id" in payload, false);+ assert.equal(payload.title, "Vision, Values, and Product Priorities");+ assert.equal(payload.content, "REAL MEETING NOTES");+ assert.equal(payload.enhanced_content, "ENHANCED NOTES");+ assert.equal(payload.transcript, '[{"text":"hello"}]');+ assert.equal(payload.enhancement_prompt, "prompt");+ assert.equal(payload.enhanced_at_content_hash, "hash-1");+ assert.equal(payload.note_type, "meeting");+ assert.equal(payload.audio_duration_seconds, 3130);+ assert.equal(payload.participants, '[{"email":"[email protected]"}]');+ assert.equal(payload.calendar_event_id, "cal-ev-1");+ assert.equal(payload.diarization_enabled, 1);+ assert.equal(payload.expected_speaker_count, 2);+ assert.equal(payload.updated_at, "2026-07-22 08:47:00");+});++test("the note update payload maps the local folder to its cloud id", async () => {+ const { buildNoteUpdatePayload } = await load();+ const mapped = buildNoteUpdatePayload(localNote, new Map([[3, "cloud-folder-3"]]));+ assert.equal(mapped.folder_id, "cloud-folder-3");++ const unmapped = buildNoteUpdatePayload(localNote, new Map());+ assert.equal(unmapped.folder_id, null);++ const folderless = buildNoteUpdatePayload({ ...localNote, folder_id: null }, new Map());+ assert.equal(folderless.folder_id, null);+});diff --git a/test/helpers/noteCloudUpsertGuard.test.js b/test/helpers/noteCloudUpsertGuard.test.jsnew file mode 100644index 000000000..7c5dd4a9c--- /dev/null+++ b/test/helpers/noteCloudUpsertGuard.test.js@@ -0,0 +1,230 @@+const test = require("node:test");+const assert = require("node:assert/strict");+const fs = require("node:fs");+const os = require("node:os");+const path = require("node:path");+const Module = require("node:module");++let userDataDir = fs.mkdtempSync(path.join(os.tmpdir(), "openwhispr-note-sync-db-"));+const originalLoad = Module._load;++Module._load = function patchedLoad(request, parent, isMain) {+ if (request === "electron") {+ return {+ app: {+ getPath: () => userDataDir,+ getAppPath: () => process.cwd(),+ isReady: () => false,+ },+ };+ }+ return originalLoad.call(this, request, parent, isMain);+};++process.env.NODE_ENV = "test";++const DatabaseManager = require("../../src/helpers/database.js");+const loadGuards = () => import("../../src/helpers/cloudSyncGuards.js");++function isNativeBindingUnavailable(error) {+ const message = String(error?.message || error);+ return (+ message.includes("NODE_MODULE_VERSION") ||+ message.includes("Could not locate the bindings file")+ );+}++function createDb(t) {+ userDataDir = fs.mkdtempSync(path.join(os.tmpdir(), "openwhispr-note-sync-db-"));+ try {+ const BetterSqlite = require("better-sqlite3");+ const probe = new BetterSqlite(path.join(userDataDir, "probe.db"));+ probe.close();+ fs.rmSync(path.join(userDataDir, "probe.db"), { force: true });+ } catch (error) {+ if (isNativeBindingUnavailable(error)) {+ t.skip("better-sqlite3 native binding is not available for this Node runtime");+ return null;+ }+ throw error;+ }++ try {+ return new DatabaseManager();+ } catch (error) {+ if (isNativeBindingUnavailable(error)) {+ t.skip("better-sqlite3 native binding is not available for this Node runtime");+ return null;+ }+ throw error;+ }+}++// Regression for #1290: a meeting note is created as an empty shell locally and+// in the cloud, the user's transcript and notes are saved locally, and a sync+// pull elects the still-empty cloud shell (its ISO timestamp lexically beats+// the local SQLite-format timestamp) and overwrites the local edit wholesale.++const TRANSCRIPT = JSON.stringify([{ text: "T".repeat(70000), source: "system", timestamp: 1 }]);++// The shell exactly as meetingDetectionEngine creates it, then the user's edit+// via the real save path. Returns the edited local row (sync_status pending,+// SQLite-format updated_at).+function seedEditedMeetingNote(db) {+ const saved = db.saveNote("Gabriel / Joshua", "", "meeting");+ const shell = saved.note || saved;+ db.updateNote(shell.id, {+ title: "Vision, Values, and Product Priorities",+ content: "REAL MEETING NOTES",+ enhanced_content: "ENHANCED NOTES",+ enhancement_prompt: "summarize the meeting",+ enhanced_at_content_hash: "hash-of-real-notes",+ transcript: TRANSCRIPT,+ cloud_id: "cloud-test-1",+ });+ return db.getNote(shell.id);+}++// The cloud's copy: the empty shell, stamped at the local edit's UTC midnight+// — older in real time (same instant at worst, if the edit lands exactly at+// 00:00:00) but with an ISO timestamp ('T' at index 10) that lexically beats+// the local SQLite format (' ' at index 10).+function emptyCloudShell(local) {+ const today = local.updated_at.slice(0, 10);+ return {+ id: "cloud-test-1",+ client_note_id: local.client_note_id,+ title: "Gabriel / Joshua",+ content: "",+ enhanced_content: null,+ transcript: null,+ note_type: "meeting",+ folder_id: null,+ source_file: null,+ audio_duration_seconds: null,+ enhancement_prompt: null,+ enhanced_at_content_hash: null,+ participants: JSON.stringify([{ email: "[email protected]" }]),+ calendar_event_id: "cal-ev-1",+ diarization_enabled: null,+ expected_speaker_count: null,+ deleted_at: null,+ created_at: `${today}T00:00:00.000Z`,+ updated_at: `${today}T00:00:00.000Z`,+ };+}++test("pull gate: an hours-older empty cloud shell is not elected over a fresh local edit", async (t) => {+ const db = createDb(t);+ if (!db) return;++ const local = seedEditedMeetingNote(db);+ assert.match(+ local.updated_at,+ /^\d{4}-\d\d-\d\d \d\d:\d\d:\d\d$/,+ "premise: local edits carry SQLite-format updated_at"+ );+ assert.equal(local.sync_status, "pending");++ const cloudNote = emptyCloudShell(local);+ assert.equal(+ cloudNote.updated_at > local.updated_at,+ true,+ "premise: the raw lexical compare would elect the hours-older cloud shell"+ );++ const { isCloudEntryNewer } = await loadGuards();+ assert.equal(+ isCloudEntryNewer(cloudNote.updated_at, local.updated_at),+ false,+ "the normalized pull gate must not elect the hours-older empty cloud shell"+ );+});++test("upsertNoteFromCloud: empty cloud values never destroy non-empty local content", (t) => {+ const db = createDb(t);+ if (!db) return;++ const before = seedEditedMeetingNote(db);+ db.upsertNoteFromCloud(emptyCloudShell(before), null);+ const after = db.getNote(before.id);++ assert.equal(after.content, "REAL MEETING NOTES", "content must survive an empty cloud copy");+ assert.equal(after.transcript, TRANSCRIPT, "transcript must survive an empty cloud copy");+ assert.equal(+ after.enhanced_content,+ "ENHANCED NOTES",+ "enhanced_content must survive an empty cloud copy"+ );+ assert.equal(+ after.enhancement_prompt,+ "summarize the meeting",+ "enhancement_prompt must travel with the preserved enhanced_content"+ );+ assert.equal(+ after.enhanced_at_content_hash,+ "hash-of-real-notes",+ "enhanced_at_content_hash must travel with the preserved enhanced_content (staleness detection breaks without it)"+ );+ assert.ok(after.participants, "participants still preserved (COALESCE)");+ assert.equal(after.calendar_event_id, "cal-ev-1", "calendar_event_id still preserved (COALESCE)");+});++test("upsertNoteFromCloud: non-empty cloud values still overwrite (last-writer-wins intact)", (t) => {+ const db = createDb(t);+ if (!db) return;++ const before = seedEditedMeetingNote(db);+ const cloudNote = {+ ...emptyCloudShell(before),+ title: "Cloud Title",+ content: "CLOUD EDIT",+ enhanced_content: "CLOUD ENHANCED",+ transcript: '[{"text":"cloud"}]',+ };+ db.upsertNoteFromCloud(cloudNote, null);+ const after = db.getNote(before.id);++ assert.equal(after.title, "Cloud Title");+ assert.equal(after.content, "CLOUD EDIT");+ assert.equal(after.enhanced_content, "CLOUD ENHANCED");+ assert.equal(after.transcript, '[{"text":"cloud"}]');+ assert.equal(+ after.enhancement_prompt,+ null,+ "a non-empty cloud enhancement brings its own prompt/hash (null here)"+ );+ assert.equal(after.sync_status, "synced");+});++test("upsertNoteFromCloud: a brand-new empty cloud note still inserts as-is", (t) => {+ const db = createDb(t);+ if (!db) return;++ const cloudNote = {+ id: "cloud-new-1",+ client_note_id: "client-new-1",+ title: "Fresh from cloud",+ content: "",+ enhanced_content: null,+ transcript: null,+ note_type: "personal",+ folder_id: null,+ source_file: null,+ audio_duration_seconds: null,+ enhancement_prompt: null,+ enhanced_at_content_hash: null,+ participants: null,+ calendar_event_id: null,+ diarization_enabled: null,+ expected_speaker_count: null,+ deleted_at: null,+ created_at: "2026-07-22T00:00:00.000Z",+ updated_at: "2026-07-22T00:00:00.000Z",+ };+ const row = db.upsertNoteFromCloud(cloudNote, null);++ assert.equal(row.title, "Fresh from cloud");+ assert.equal(row.content, "");+ assert.equal(row.sync_status, "synced");+});