Skip to content

Voice-to-text dictation app with local (Nvidia Parakeet/Whisper) and cloud models (BYOK). Privacy-first and available cross-platform.

feat(meta): stage durable spoken word activation capture (#2656)

Joshua70fa70cce9f9
diff --git a/main.js b/main.js
index 307751207..b4a22d88d 100644
--- a/main.js
+++ b/main.js
@@ -2122,7 +2122,13 @@ if (gotSingleInstanceLock) {
}
event.preventDefault();
performSyncTeardown();
- sidecarRegistry.shutdownAll().finally(() => app.exit(0));
+ Promise.allSettled([
+ sidecarRegistry.shutdownAll(),
+ Promise.race([
+ ipcHandlers?.metaActivation?.shutdown(),
+ new Promise((resolve) => setTimeout(resolve, 1500)),
+ ]),
+ ]).finally(() => app.exit(0));
});
}
diff --git a/preload.js b/preload.js
index cd4769d2f..e104b67b0 100644
--- a/preload.js
+++ b/preload.js
@@ -158,6 +158,11 @@ contextBridge.exposeInMainWorld("electronAPI", {
getTranscriptions: (limit, options) =>
ipcRenderer.invoke("db-get-transcriptions", limit, options),
recordAnalyticsEvent: (input) => ipcRenderer.invoke("analytics-record-event", input),
+ closeActivationStream: () => ipcRenderer.invoke("activation-close"),
+ beginActivationCapture: () => ipcRenderer.invoke("activation-begin"),
+ stopActivationCapture: (id) => ipcRenderer.invoke("activation-stop", id),
+ finishActivationCapture: (id, words) => ipcRenderer.invoke("activation-finish", id, words),
+ cancelActivationCapture: (id) => ipcRenderer.invoke("activation-cancel", id),
getAnalyticsSummary: () => ipcRenderer.invoke("analytics-get-summary"),
getPendingAnalyticsEvents: (limit, context) =>
ipcRenderer.invoke("analytics-get-pending", limit, context),
diff --git a/src/helpers/audioManager.js b/src/helpers/audioManager.js
index 9991b2e92..7adda5caf 100644
--- a/src/helpers/audioManager.js
+++ b/src/helpers/audioManager.js
@@ -1444,6 +1444,12 @@ registerProcessor("pcm-streaming-processor", PCMStreamingProcessor);
return false;
}
+ const activationGeneration = this._activationCaptureGeneration ?? 0;
+ if (
+ !(await this._beginActivationCapture()) ||
+ activationGeneration !== (this._activationCaptureGeneration ?? 0)
+ )
+ return false;
const startRequestedAt = performance.now();
prepared = forceDefaultMic ? null : await this.preparedMicCapture.take();
const constraints =
@@ -1602,6 +1608,7 @@ registerProcessor("pcm-streaming-processor", PCMStreamingProcessor);
return true;
} catch (error) {
+ this._cancelActivationCapture(this._takeActivationCapture());
// A prepared value the recording never adopted still owns a live stream
// (and possibly a pre-roll recorder); release it before any retry.
freshTap?.close();
@@ -1706,15 +1713,20 @@ registerProcessor("pcm-streaming-processor", PCMStreamingProcessor);
// Taken now, so a cancelled pipeline that settles late can't end the next
// recording's spool.
const spool = this._takeRecordingSpool();
+ const activationCapture = this._takeActivationCapture();
try {
- await this._finalizeBatchRecording(finalSegment);
+ if (activationCapture)
+ void window.electronAPI?.stopActivationCapture?.(activationCapture).catch(() => {});
+ await this._finalizeBatchRecording(finalSegment, activationCapture);
} finally {
+ this._cancelActivationCapture(activationCapture);
spool?.finish();
}
}
- async _finalizeBatchRecording(finalSegment) {
+ async _finalizeBatchRecording(finalSegment, activationCapture = null) {
const processingPipeline = this._startProcessingPipeline();
+ processingPipeline.activationCapture = activationCapture;
const wasCancelled = () => this._shouldAbandonProcessingPipeline(processingPipeline);
this.micRecovery.stop();
this.teardownSpeechGate();
@@ -1913,6 +1925,7 @@ registerProcessor("pcm-streaming-processor", PCMStreamingProcessor);
}
cancelRecording() {
+ this._cancelActivationCapture(this._takeActivationCapture());
this.micRecovery.stop();
if (this._rotatingBatchRecorder) {
this._cancelRequestedDuringMicRecovery = true;
@@ -2054,6 +2067,7 @@ registerProcessor("pcm-streaming-processor", PCMStreamingProcessor);
}
cancelProcessing() {
+ this._cancelActivationCapture(this._activeProcessingPipeline?.activationCapture);
if (this.isProcessing) {
this._processingCancellationGeneration = (this._processingCancellationGeneration ?? 0) + 1;
this._requestStreamingCancellation();
@@ -2195,6 +2209,8 @@ registerProcessor("pcm-streaming-processor", PCMStreamingProcessor);
// Settles once the result is delivered and saved; the recording's spool
// waits for it, so a main process that hangs on the paste or the save
// can't take the audio with it.
+ await this._completeActivationCapture(pipeline.activationCapture, result);
+ if (wasCancelled()) return;
pipeline.resultHandled = this.onTranscriptionComplete?.(result);
if (result?.source === "openwhispr") {
@@ -4261,6 +4277,46 @@ registerProcessor("pcm-streaming-processor", PCMStreamingProcessor);
}
}
+ async _beginActivationCapture() {
+ if (this._activationCapture || this.voiceAgentRequested) return true;
+ const generation = this._activationCaptureGeneration ?? 0;
+ try {
+ const id = await window.electronAPI?.beginActivationCapture?.();
+ if (generation !== (this._activationCaptureGeneration ?? 0)) {
+ this._cancelActivationCapture(id);
+ return false;
+ }
+ this._activationCapture = id;
+ } catch {
+ // Marketing capture must never interrupt dictation.
+ }
+ return generation === (this._activationCaptureGeneration ?? 0);
+ }
+
+ _takeActivationCapture() {
+ this._activationCaptureGeneration = (this._activationCaptureGeneration ?? 0) + 1;
+ const id = this._activationCapture;
+ this._activationCapture = null;
+ return id;
+ }
+
+ _cancelActivationCapture(id) {
+ if (id) void window.electronAPI?.cancelActivationCapture?.(id).catch(() => {});
+ }
+
+ async _completeActivationCapture(id, result) {
+ if (!id) return;
+ const words =
+ result?.success && !result.assistantConversation && !result.selectionEdit
+ ? countSpokenWords(result.rawText)
+ : 0;
+ try {
+ await window.electronAPI?.finishActivationCapture?.(id, words);
+ } catch {
+ // The main-process pending capture fences completeness after write failure.
+ }
+ }
+
async saveTranscription(
text,
rawText = null,
@@ -4850,6 +4906,14 @@ registerProcessor("pcm-streaming-processor", PCMStreamingProcessor);
}
this.stopRequestedDuringStreamingStart = false;
+ const activationGeneration = this._activationCaptureGeneration ?? 0;
+ if (
+ !(await this._beginActivationCapture()) ||
+ activationGeneration !== (this._activationCaptureGeneration ?? 0)
+ ) {
+ this._settleStreamingStart();
+ return false;
+ }
sessionId = (this._streamingSessionGeneration || 0) + 1;
this._streamingSessionGeneration = sessionId;
this.streamingFallbackReason = null;
@@ -5103,6 +5167,7 @@ registerProcessor("pcm-streaming-processor", PCMStreamingProcessor);
return true;
} catch (error) {
const stopRequested = this.stopRequestedDuringStreamingStart;
+ this._cancelActivationCapture(this._takeActivationCapture());
this._settleStreamingStart();
this.stopRequestedDuringStreamingStart = false;
@@ -5218,7 +5283,9 @@ registerProcessor("pcm-streaming-processor", PCMStreamingProcessor);
const sessionId = this._activeStreamingSessionId;
const spool = this._takeRecordingSpool();
- const delivery = {};
+ const delivery = { activationCapture: this._takeActivationCapture() };
+ if (delivery.activationCapture)
+ void window.electronAPI?.stopActivationCapture?.(delivery.activationCapture).catch(() => {});
const stopPromise = this._finalizeStreamingRecording(sessionId, delivery);
this._streamingStopPromise = stopPromise;
this._streamingStopMode = "finalize";
@@ -5226,6 +5293,7 @@ registerProcessor("pcm-streaming-processor", PCMStreamingProcessor);
return await stopPromise;
} finally {
// The stop settles at delivery; the spool waits until the result is saved.
+ this._cancelActivationCapture(delivery.activationCapture);
if (spool) void Promise.resolve(delivery.resultHandled).finally(() => spool.finish());
if (this._streamingStopPromise === stopPromise) {
this._streamingStopPromise = null;
@@ -5260,6 +5328,7 @@ registerProcessor("pcm-streaming-processor", PCMStreamingProcessor);
}
async cancelStreamingRecording() {
+ this._cancelActivationCapture(this._takeActivationCapture());
if (this._streamingStopPromise) {
if (this._streamingStopMode === "finalize") {
this._requestStreamingCancellation();
@@ -5868,6 +5937,13 @@ registerProcessor("pcm-streaming-processor", PCMStreamingProcessor);
model: batchFallbackResult ? null : streamingSttModel || null,
};
if (wasCancelled()) return true;
+ await this._completeActivationCapture(delivery.activationCapture, {
+ success: true,
+ rawText: batchFallbackResult?.rawText ?? rawStreamingText,
+ assistantConversation: this.pendingAssistantConversation,
+ selectionEdit: this.pendingSelectionEdit,
+ });
+ if (wasCancelled()) return true;
delivery.resultHandled = this.onTranscriptionComplete?.({
success: true,
text: finalText,
diff --git a/src/helpers/ipcHandlers.js b/src/helpers/ipcHandlers.js
index 491210419..b6d8693d8 100644
--- a/src/helpers/ipcHandlers.js
+++ b/src/helpers/ipcHandlers.js
@@ -18,6 +18,7 @@ const { BYOK_API_KEYS } = require("../config/secretKeys");
const tokenStore = require("./tokenStore");
const accountScopeBinding = require("./accountScopeBinding");
const { createCloudApiRequestHandler } = require("./cloudApiRequest");
+const { MetaActivation } = require("./metaActivation");
const { decodeLeaderboardPngDataUrl, leaderboardImageFilename } = require("./leaderboardImage");
const { withPolicyRequestHeaders } = require("./policyRequestHeaders");
const {
@@ -2455,6 +2456,7 @@ class IPCHandlers {
);
this.connectorManager?.accountChanged();
void this.connectorManager?.notifyStatusChanged();
+ void this.metaActivation?.flush();
return { success: true };
});
@@ -2490,6 +2492,7 @@ class IPCHandlers {
erasingDevice: options?.erasingDevice === true,
});
const result = this.databaseManager.deleteAccountData(accountId);
+ this.metaActivation?.purge(accountId);
this.notifyVectorChanges();
for (const noteId of result.deletedNoteIds) {
this._asyncMirrorDelete(noteId);
@@ -4246,6 +4249,16 @@ class IPCHandlers {
errors.push(`Connector revoke: ${errorCode ?? errorName}`);
}
+ try {
+ this.metaActivation?.dispose();
+ this.metaActivation = null;
+ const activationPath = path.join(app.getPath("userData"), "meta-activation.db");
+ for (const suffix of ["", "-wal", "-shm"])
+ fs.rmSync(activationPath + suffix, { force: true });
+ } catch (e) {
+ errors.push(`Activation data cleanup: ${e.message}`);
+ }
+
// Close DB connection before deleting the file
try {
this.databaseManager?.db?.close();
@@ -6299,6 +6312,10 @@ class IPCHandlers {
ipcMain.handle("auth-clear-session", async (event) => {
try {
+ await Promise.race([
+ this.metaActivation?.close(),
+ new Promise((resolve) => setTimeout(resolve, 1500)),
+ ]).catch(() => {});
const tokenState = tokenStore.clear();
const win = BrowserWindow.fromWebContents(event.sender);
if (win) {
@@ -6434,6 +6451,46 @@ class IPCHandlers {
tokenStore,
logger: debugLogger,
});
+ try {
+ const ActivationDatabase = require("better-sqlite3");
+ const activationDb = new ActivationDatabase(
+ path.join(app.getPath("userData"), "meta-activation.db")
+ );
+ activationDb.pragma("foreign_keys = ON");
+ this.metaActivation = new MetaActivation({
+ db: activationDb,
+ getScope: () =>
+ accountScopeBinding.resolveActiveAccountScope({
+ ...tokenStore.getState(),
+ binding: accountScopeBinding.read(),
+ }),
+ request: createCloudApiRequestHandler({
+ getApiUrl,
+ getAppVersion: () => app.getVersion(),
+ tokenStore,
+ proxyFetch: (url, init) =>
+ proxyFetch(url, { ...init, signal: AbortSignal.timeout(10_000) }),
+ }),
+ });
+ this.metaActivation.start();
+ } catch {
+ // Marketing bookkeeping must never prevent ordinary dictation/startup.
+ debugLogger.warn("Activation storage unavailable");
+ }
+ ipcMain.handle("activation-close", async () => {
+ await Promise.race([
+ this.metaActivation?.close(),
+ new Promise((resolve) => setTimeout(resolve, 1500)),
+ ]).catch(() => {});
+ });
+ ipcMain.handle("activation-begin", () => this.metaActivation?.begin() ?? null);
+ ipcMain.handle("activation-stop", (_event, id) => {
+ if (typeof id === "string") this.metaActivation?.stopCapture(id);
+ });
+ ipcMain.handle("activation-finish", (_event, id, words) =>
+ this.metaActivation?.finish(id, words)
+ );
+ ipcMain.handle("activation-cancel", (_event, id) => this.metaActivation?.cancel(id));
const workspacePolicyManager = createWorkspacePolicyManager({
cachePath: path.join(app.getPath("userData"), "workspace-policy.json"),
getApiUrl,
diff --git a/src/helpers/metaActivation.js b/src/helpers/metaActivation.js
new file mode 100644
index 000000000..163c16ac7
--- /dev/null
+++ b/src/helpers/metaActivation.js
@@ -0,0 +1,351 @@
+const { randomUUID } = require("node:crypto");
+
+// A small dedicated queue. It never reads transcription/Insights tables and
+// never stores text, audio, model names, titles or application context.
+class MetaActivation {
+ constructor({ db, getScope, request, monotonicNow = () => performance.now() }) {
+ this.db = db;
+ this.getScope = getScope;
+ this.request = request;
+ this.monotonicNow = monotonicNow;
+ this.anchor = null;
+ this.running = null;
+ this.failedCaptures = new Set();
+ this.db.exec(`
+ CREATE TABLE IF NOT EXISTS streams (
+ id TEXT PRIMARY KEY, account_id TEXT NOT NULL, state TEXT NOT NULL DEFAULT 'new',
+ registered_at TEXT, complete_through TEXT, last_seq INTEGER NOT NULL DEFAULT 0,
+ total_words INTEGER NOT NULL DEFAULT 0, gap INTEGER NOT NULL DEFAULT 0
+ );
+ CREATE TABLE IF NOT EXISTS captures (
+ id TEXT PRIMARY KEY, stream_id TEXT NOT NULL REFERENCES streams(id) ON DELETE CASCADE,
+ generation INTEGER NOT NULL, started_at TEXT NOT NULL, occurred_at TEXT,
+ seq INTEGER, words INTEGER
+ );
+ `);
+ // A process died before persisting a result. Do not certify that interval
+ // complete, even if a recovered recording later appears in ordinary history.
+ this.db
+ .exec(`UPDATE streams SET gap=1 WHERE id IN (SELECT stream_id FROM captures WHERE words IS NULL);
+ DELETE FROM captures WHERE words IS NULL;`);
+ }
+
+ start() {
+ this.timer = setInterval(() => void this.flush(true), 30_000);
+ this.timer.unref?.();
+ void this.flush();
+ }
+
+ current(scope = this.getScope()) {
+ if (!scope) return null;
+ return this.db
+ .prepare("SELECT * FROM streams WHERE account_id=? ORDER BY rowid DESC LIMIT 1")
+ .get(scope.accountId);
+ }
+
+ clock(scope) {
+ if (
+ !this.anchor ||
+ this.anchor.accountId !== scope?.accountId ||
+ this.anchor.generation !== scope?.authGeneration
+ )
+ return null;
+ return this.anchor.serverTime + (this.monotonicNow() - this.anchor.monotonicTime);
+ }
+
+ begin() {
+ const scope = this.getScope();
+ let stream = this.current(scope);
+ if (!scope) return null;
+ if (!stream || stream.state === "closed") {
+ this.db
+ .prepare("INSERT INTO streams(id,account_id) VALUES(?,?)")
+ .run(randomUUID(), scope.accountId);
+ stream = this.current(scope);
+ }
+ if (stream.state === "done") return null;
+ const time = this.clock(scope);
+ if (time === null || stream.state !== "open") {
+ // The cohort starts only after permitted registration. Earlier usage
+ // is intentionally untracked; a missing clock after enrollment is a gap.
+ if (stream.registered_at)
+ this.db.prepare("UPDATE streams SET gap=1 WHERE id=?").run(stream.id);
+ void this.flush();
+ return null;
+ }
+ if (stream.total_words >= 200) return null;
+ const id = randomUUID();
+ this.db
+ .prepare("INSERT INTO captures(id,stream_id,generation,started_at) VALUES(?,?,?,?)")
+ .run(id, stream.id, scope.authGeneration, new Date(Math.ceil(time)).toISOString());
+ return id;
+ }
+
+ stopCapture(id) {
+ const row = this.db
+ .prepare(
+ "SELECT c.*,s.account_id FROM captures c JOIN streams s ON s.id=c.stream_id WHERE c.id=?"
+ )
+ .get(id);
+ if (!row || row.occurred_at) return;
+ const scope = this.getScope();
+ const time = this.clock(scope);
+ if (
+ !scope ||
+ scope.accountId !== row.account_id ||
+ scope.authGeneration !== row.generation ||
+ time === null
+ ) {
+ this.db.prepare("UPDATE streams SET gap=1 WHERE id=?").run(row.stream_id);
+ return;
+ }
+ this.db
+ .prepare("UPDATE captures SET occurred_at=? WHERE id=?")
+ .run(new Date(Math.max(Math.ceil(time), Date.parse(row.started_at) + 1)).toISOString(), id);
+ }
+
+ finish(id, wordCount) {
+ if (typeof id !== "string" || !Number.isSafeInteger(wordCount) || wordCount < 0) return;
+ try {
+ this.db.transaction(() => {
+ const row = this.db
+ .prepare(
+ "SELECT c.*,s.account_id,s.total_words,s.last_seq FROM captures c JOIN streams s ON s.id=c.stream_id WHERE c.id=?"
+ )
+ .get(id);
+ if (!row || row.words !== null) return;
+ const scope = this.getScope();
+ if (
+ !scope ||
+ scope.accountId !== row.account_id ||
+ scope.authGeneration !== row.generation
+ ) {
+ this.db.prepare("UPDATE streams SET gap=1 WHERE id=?").run(row.stream_id);
+ this.db.prepare("DELETE FROM captures WHERE id=?").run(id);
+ return;
+ }
+ const words = Math.min(wordCount, 200 - row.total_words);
+ if (words === 0) {
+ this.db.prepare("DELETE FROM captures WHERE id=?").run(id);
+ return;
+ }
+ if (!row.occurred_at) {
+ this.db.prepare("UPDATE streams SET gap=1 WHERE id=?").run(row.stream_id);
+ this.db.prepare("DELETE FROM captures WHERE id=?").run(id);
+ return;
+ }
+ this.db
+ .prepare("UPDATE captures SET words=?,seq=? WHERE id=?")
+ .run(words, row.last_seq + 1, id);
+ this.db
+ .prepare("UPDATE streams SET last_seq=last_seq+1,total_words=total_words+? WHERE id=?")
+ .run(words, row.stream_id);
+ })();
+ } catch (error) {
+ // Keep the unresolved capture across renderer cleanup. On restart it
+ // becomes a durable coverage gap instead of silently losing spoken words.
+ this.failedCaptures.add(id);
+ throw error;
+ }
+ void this.flush();
+ }
+
+ cancel(id) {
+ if (typeof id !== "string" || this.failedCaptures.has(id)) return;
+ this.db.prepare("DELETE FROM captures WHERE id=? AND words IS NULL").run(id);
+ void this.flush();
+ }
+
+ async flush(scheduled = false) {
+ if (this.disposed) return;
+ if (this.running) return this.running;
+ this.running = this.runPass(scheduled)
+ .catch(() => {})
+ .finally(() => {
+ this.running = null;
+ });
+ return this.running;
+ }
+
+ async runPass(scheduled) {
+ const scope = this.getScope();
+ if (!scope) return;
+ const assertCurrent = () => {
+ const current = this.getScope();
+ if (this.disposed) throw new Error("Activation storage closed");
+ if (
+ !current ||
+ current.accountId !== scope.accountId ||
+ current.authGeneration !== scope.authGeneration
+ )
+ throw new Error("Account changed");
+ };
+ let stream = this.current(scope);
+ if (!stream || stream.state === "closed") {
+ const id = randomUUID();
+ this.db.prepare("INSERT INTO streams(id,account_id) VALUES(?,?)").run(id, scope.accountId);
+ stream = this.current(scope);
+ }
+ if (stream.state === "done") return;
+ // Once the server answers awaiting_permission or refuses (403, e.g. org
+ // policy) an unregistered stream, only activity posts again. A registered
+ // stream keeps certifying its watermark, and a lost answer may have
+ // committed a device, so the timer retries both.
+ if (scheduled && this.unpermittedStream === stream.id) return;
+ const post = async (path, body) => {
+ assertCurrent();
+ this.unpermittedStream = null;
+ const result = await this.request({
+ path,
+ method: "POST",
+ body,
+ expectedAuthGeneration: scope.authGeneration,
+ });
+ assertCurrent();
+ if (!result?.success) {
+ if (result?.status === 403 && !stream.registered_at) this.unpermittedStream = stream.id;
+ throw new Error("Activation sync unavailable");
+ }
+ return result.data;
+ };
+ if (this.clock(scope) === null || stream.state === "new") {
+ const response = await post("/api/activation/device", {
+ accountId: scope.accountId,
+ deviceId: stream.id,
+ });
+ if (response.status === "awaiting_permission") {
+ this.unpermittedStream = stream.id;
+ return;
+ }
+ if (response.status !== "collecting") {
+ this.retire(stream.id);
+ return;
+ }
+ if (!response.device || !Number.isFinite(Date.parse(response.serverTime)))
+ throw new Error("Invalid registration");
+ if (response.device.closed) {
+ this.db.prepare("UPDATE streams SET state='closed' WHERE id=?").run(stream.id);
+ return;
+ }
+ this.anchor = {
+ accountId: scope.accountId,
+ generation: scope.authGeneration,
+ serverTime: Date.parse(response.serverTime),
+ monotonicTime: this.monotonicNow(),
+ };
+ this.db
+ .prepare(
+ "UPDATE streams SET state=CASE WHEN state='closing' THEN state ELSE 'open' END,registered_at=?,complete_through=COALESCE(complete_through,?) WHERE id=?"
+ )
+ .run(
+ new Date(response.device.registered_at).toISOString(),
+ new Date(response.device.complete_through).toISOString(),
+ stream.id
+ );
+ stream = this.current(scope);
+ }
+ const pending = this.db
+ .prepare("SELECT * FROM captures WHERE stream_id=? ORDER BY seq")
+ .all(stream.id);
+ // Stop times belong to unfinished STT/cleanup work. Don't advance a device
+ // checkpoint while that work could still add an earlier contribution.
+ if (pending.some((row) => row.words === null) && !stream.gap) return;
+ const completeThrough = new Date(
+ Math.max(
+ Math.ceil(this.clock(scope)),
+ Date.parse(stream.complete_through),
+ ...pending.map((row) => Date.parse(row.occurred_at))
+ )
+ ).toISOString();
+ const events = pending
+ .filter((row) => row.words !== null)
+ .map((row) => ({
+ eventId: row.id,
+ seq: row.seq,
+ words: row.words,
+ occurredAt: row.occurred_at,
+ }));
+ const result = await post("/api/activation/batch", {
+ deviceId: stream.id,
+ events,
+ completeThrough,
+ close: stream.state === "closing",
+ gap: Boolean(stream.gap),
+ });
+ if (result.status !== "collecting") {
+ this.retire(stream.id);
+ return;
+ }
+ if (!Number.isSafeInteger(result.acceptedThrough) || result.acceptedThrough > stream.last_seq)
+ throw new Error("Invalid acknowledgement");
+ this.db.transaction(() => {
+ this.db
+ .prepare("DELETE FROM captures WHERE stream_id=? AND seq<=?")
+ .run(stream.id, result.acceptedThrough);
+ this.db
+ .prepare(
+ "UPDATE streams SET complete_through=?,state=CASE WHEN ? AND state='closing' THEN 'closed' ELSE state END WHERE id=?"
+ )
+ .run(completeThrough, stream.state === "closing" ? 1 : 0, stream.id);
+ })();
+ }
+
+ retire(id) {
+ for (const row of this.db.prepare("SELECT id FROM captures WHERE stream_id=?").all(id))
+ this.failedCaptures.delete(row.id);
+ this.db.transaction(() => {
+ this.db.prepare("UPDATE streams SET state='done' WHERE id=?").run(id);
+ this.db.prepare("DELETE FROM captures WHERE stream_id=?").run(id);
+ })();
+ }
+
+ async close() {
+ const stream = this.current();
+ if (
+ !stream ||
+ stream.state === "done" ||
+ stream.state === "closed" ||
+ this.unpermittedStream === stream.id
+ )
+ return;
+ const unresolved = this.db
+ .prepare("SELECT 1 FROM captures WHERE stream_id=? AND words IS NULL LIMIT 1")
+ .get(stream.id);
+ if (unresolved) {
+ this.db.prepare("UPDATE streams SET gap=1 WHERE id=?").run(stream.id);
+ }
+ this.db.prepare("DELETE FROM captures WHERE stream_id=? AND words IS NULL").run(stream.id);
+ this.db.prepare("UPDATE streams SET state='closing' WHERE id=?").run(stream.id);
+ await this.flush();
+ // An already-running pass may have read the pre-close state.
+ if (this.current()?.state === "closing" && this.unpermittedStream !== stream.id)
+ await this.flush();
+ this.anchor = null;
+ }
+
+ purge(accountId) {
+ const captures = this.db
+ .prepare(
+ "SELECT c.id FROM captures c JOIN streams s ON s.id=c.stream_id WHERE s.account_id=?"
+ )
+ .all(accountId);
+ for (const row of captures) this.failedCaptures.delete(row.id);
+ this.db.prepare("DELETE FROM streams WHERE account_id=?").run(accountId);
+ this.anchor = null;
+ }
+
+ dispose() {
+ this.disposed = true;
+ clearInterval(this.timer);
+ this.anchor = null;
+ this.failedCaptures.clear();
+ this.db.close();
+ }
+
+ async shutdown() {
+ clearInterval(this.timer);
+ await this.close();
+ }
+}
+
+module.exports = { MetaActivation };
diff --git a/src/lib/auth.ts b/src/lib/auth.ts
index b65f0ee62..d17dca5bb 100644
--- a/src/lib/auth.ts
+++ b/src/lib/auth.ts
@@ -133,6 +133,7 @@ export function getGracePeriodRemainingMs(): number {
export async function signOut(): Promise<void> {
credentialAccountCache = null;
+ await window.electronAPI?.closeActivationStream?.().catch(() => undefined);
try {
await authClient.signOut();
} catch {
diff --git a/src/types/electron.ts b/src/types/electron.ts
index ceea3b260..e5288e440 100644
--- a/src/types/electron.ts
+++ b/src/types/electron.ts
@@ -1733,6 +1733,11 @@ declare global {
accountId: string | null,
expectedAuthGeneration?: number
) => Promise<{ success: boolean; code?: string; error?: string }>;
+ closeActivationStream?: () => Promise<void>;
+ beginActivationCapture?: () => Promise<string | null>;
+ stopActivationCapture?: (id: string) => Promise<void>;
+ finishActivationCapture?: (id: string, words: number) => Promise<void>;
+ cancelActivationCapture?: (id: string) => Promise<void>;
getActiveAccountScope?: () => Promise<ActiveAccountScope | null>;
onActiveAccountScopeChanged?: (
callback: (scope: ActiveAccountScope | null) => void
diff --git a/test/components/signOutEndsMeetingRecording.test.js b/test/components/signOutEndsMeetingRecording.test.js
index 582296e4a..19daf6ba5 100644
--- a/test/components/signOutEndsMeetingRecording.test.js
+++ b/test/components/signOutEndsMeetingRecording.test.js
@@ -100,3 +100,23 @@ test("switching account from an invitation ends a live meeting before signing ou
"signed out",
]);
});
+
+test("auth sign-out closes the activation stream while its credential is still valid", async () => {
+ const calls = [];
+ const signOut = vm.runInNewContext(
+ `${extractFunction("../../src/lib/auth.ts", "signOut").replace(/^export /, "")}\nsignOut;`,
+ {
+ credentialAccountCache: {},
+ window: {
+ electronAPI: {
+ closeActivationStream: async () => calls.push("activation closed"),
+ authClearSession: async () => calls.push("local cleared"),
+ },
+ },
+ authClient: { signOut: async () => calls.push("remote revoked") },
+ markSignedOutState: () => calls.push("signed out"),
+ }
+ );
+ await signOut();
+ assert.deepEqual(calls, ["activation closed", "remote revoked", "local cleared", "signed out"]);
+});
diff --git a/test/helpers/accountScopeIpc.test.js b/test/helpers/accountScopeIpc.test.js
index cd35455a8..e9a6c693c 100644
--- a/test/helpers/accountScopeIpc.test.js
+++ b/test/helpers/accountScopeIpc.test.js
@@ -66,6 +66,10 @@ Module._load = function loadWithMocks(request, parent, isMain) {
return {
get: () => tokenState.token,
getState: () => ({ ...tokenState }),
+ clear: () => {
+ tokenState = { token: null, generation: tokenState.generation + 1 };
+ return { success: true, ...tokenState };
+ },
subscribe: () => () => {},
};
}
@@ -125,6 +129,7 @@ test.before(() => {
});
test.after(() => {
+ fakeThis.metaActivation?.dispose?.();
Module._load = originalLoad;
fs.rmSync(userDataDirectory, { recursive: true, force: true });
});
@@ -226,3 +231,21 @@ test("meeting prompt retirement precedes scope mutation and skips a true no-op",
assert.deepEqual(calls, ["old-account", "meeting-account", "meeting-account"]);
fakeThis.windowManager = originalManager;
});
+
+test("activation storage failure cannot prevent clearing the local bearer", async () => {
+ const manager = fakeThis.metaActivation;
+ fakeThis.metaActivation = {
+ close: async () => {
+ throw new Error("disk full");
+ },
+ };
+ tokenState = { token: "must-clear", generation: 30 };
+ try {
+ const result = await handlers.get("auth-clear-session")({ sender: {} });
+ assert.equal(result.success, true);
+ assert.equal(tokenState.token, null);
+ assert.equal(tokenState.generation, 31);
+ } finally {
+ fakeThis.metaActivation = manager;
+ }
+});
diff --git a/test/helpers/localServerSyncIpc.test.js b/test/helpers/localServerSyncIpc.test.js
index 4cfa6967c..b282ae67d 100644
--- a/test/helpers/localServerSyncIpc.test.js
+++ b/test/helpers/localServerSyncIpc.test.js
@@ -1,10 +1,14 @@
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");
const handlersModulePath = require.resolve("../../src/helpers/ipcHandlers");
const originalLoad = Module._load;
const handlers = new Map();
+const userDataDirectory = fs.mkdtempSync(path.join(os.tmpdir(), "local-server-sync-ipc-"));
const MODEL = "qwen3.5-4b-q4_k_m";
const modelManager = {
@@ -24,7 +28,7 @@ const modelManager = {
const electronStub = {
app: {
- getPath: () => "",
+ getPath: () => userDataDirectory,
getName: () => "test",
getVersion: () => "0.0.0",
isPackaged: false,
@@ -99,7 +103,9 @@ test.before(() => {
});
test.after(() => {
+ target.metaActivation?.dispose();
Module._load = originalLoad;
+ fs.rmSync(userDataDirectory, { recursive: true, force: true });
});
function sync({ loaded, signedIn = false, policySettled = true, ...scopes }) {
diff --git a/test/helpers/metaActivation.test.js b/test/helpers/metaActivation.test.js
new file mode 100644
index 000000000..0904a0665
--- /dev/null
+++ b/test/helpers/metaActivation.test.js
@@ -0,0 +1,382 @@
+const test = require("node:test");
+const assert = require("node:assert/strict");
+const Database = require("better-sqlite3");
+const { MetaActivation } = require("../../src/helpers/metaActivation");
+const { loadAudioManager } = require("./harness/audioManager");
+
+function rig(t) {
+ const db = new Database(":memory:");
+ db.pragma("foreign_keys = ON");
+ t.after(() => {
+ if (db.open) db.close();
+ });
+ let scope = { accountId: "a", authGeneration: 1 };
+ let clock = 0;
+ let offline = false;
+ let blocked = false;
+ let permitted = true;
+ const calls = [];
+ let seq = 0;
+ let total = 0;
+ const serverTime = "2026-10-14T10:00:00.000Z";
+ const request = async ({ path, body }) => {
+ calls.push({ path, body: structuredClone(body) });
+ if (offline) throw new Error("offline");
+ if (blocked) return { success: false, status: 403, code: "POLICY_CLOUD_BACKUP_BLOCKED" };
+ if (path.endsWith("/device") && !permitted)
+ return { success: true, data: { status: "awaiting_permission", serverTime, device: null } };
+ if (path.endsWith("/device"))
+ return {
+ success: true,
+ data: {
+ status: "collecting",
+ serverTime,
+ device: {
+ registered_at: serverTime,
+ complete_through: serverTime,
+ last_seq: seq,
+ closed: false,
+ },
+ },
+ };
+ for (const e of body.events)
+ if (e.seq > seq) {
+ seq = e.seq;
+ total += e.words;
+ }
+ return {
+ success: true,
+ data: { status: total >= 200 ? "pending" : "collecting", acceptedThrough: seq },
+ };
+ };
+ const create = () =>
+ new MetaActivation({ db, getScope: () => scope, request, monotonicNow: () => clock });
+ const manager = create();
+ return {
+ db,
+ manager,
+ create,
+ calls,
+ tick: (n = 1000) => (clock += n),
+ offline: (v) => (offline = v),
+ blocked: (v) => (blocked = v),
+ permitted: (v) => (permitted = v),
+ scope: (v) => (scope = v),
+ };
+}
+async function capture(r, words) {
+ r.tick();
+ const id = r.manager.begin();
+ r.tick();
+ r.manager.stopCapture(id);
+ r.manager.finish(id, words);
+ await r.manager.flush();
+ return id;
+}
+test("durable queue sends 199 then 1, caps expansion, and retires at the server milestone", async (t) => {
+ const r = rig(t);
+ await r.manager.flush();
+ await capture(r, 199);
+ await capture(r, 999);
+ assert.deepEqual(
+ r.calls
+ .filter((c) => c.path.endsWith("batch"))
+ .flatMap((c) => c.body.events)
+ .map((e) => e.words),
+ [199, 1]
+ );
+ assert.equal(r.manager.begin(), null);
+ assert.equal(r.db.prepare("SELECT count(*) n FROM captures").get().n, 0);
+});
+test("offline retry/restart retains original event id and occurrence time", async (t) => {
+ const r = rig(t);
+ await r.manager.flush();
+ r.offline(true);
+ await capture(r, 199);
+ const original = r.calls.at(-1).body.events[0];
+ const restarted = r.create();
+ r.offline(false);
+ await restarted.flush();
+ assert.deepEqual(r.calls.at(-1).body.events[0], original);
+});
+test("unresolved recognition fences watermarks and cancelled work adds no words", async (t) => {
+ const r = rig(t);
+ await r.manager.flush();
+ r.tick();
+ const id = r.manager.begin();
+ r.tick();
+ r.manager.stopCapture(id);
+ const before = r.calls.length;
+ await r.manager.flush();
+ assert.equal(r.calls.length, before);
+ r.manager.cancel(id);
+ await r.manager.flush();
+ assert.equal(r.calls.at(-1).body.events.length, 0);
+});
+test("account replacement cannot adopt a capture or acknowledge its old queue", async (t) => {
+ const r = rig(t);
+ await r.manager.flush();
+ r.tick();
+ const id = r.manager.begin();
+ r.tick();
+ r.manager.stopCapture(id);
+ r.scope({ accountId: "b", authGeneration: 2 });
+ r.manager.finish(id, 200);
+ await r.manager.flush();
+ assert.equal(
+ r.db.prepare("SELECT total_words,gap FROM streams WHERE account_id='a'").get().total_words,
+ 0
+ );
+ assert.equal(r.db.prepare("SELECT gap FROM streams WHERE account_id='a'").get().gap, 1);
+ assert.equal(r.calls.filter((c) => c.body.events).flatMap((c) => c.body.events).length, 0);
+});
+test("offline process restart cannot fabricate a source time from a stale monotonic clock", async (t) => {
+ const r = rig(t);
+ await r.manager.flush();
+ const restarted = r.create();
+ r.offline(true);
+ assert.equal(restarted.begin(), null);
+ assert.equal(r.db.prepare("SELECT gap FROM streams").get().gap, 1);
+});
+test("close proves stream complete; account erasure removes all dedicated local state", async (t) => {
+ const r = rig(t);
+ await r.manager.flush();
+ await capture(r, 3);
+ await r.manager.close();
+ assert.equal(r.calls.at(-1).body.close, true);
+ r.manager.purge("a");
+ assert.equal(r.db.prepare("SELECT count(*) n FROM streams").get().n, 0);
+});
+test("live capture counts original speech with history off, excluding assistant/output-only text", async (t) => {
+ const { window, createManager } = await loadAudioManager(t, {
+ cachePrefix: "activation-raw-",
+ settingsKey: "__activationSettings",
+ settings: { dataRetentionEnabled: false },
+ });
+ const counts = [];
+ window.electronAPI.finishActivationCapture = async (id, words) => counts.push({ id, words });
+ const manager = createManager();
+ await manager._completeActivationCapture("local", {
+ success: true,
+ rawText: "one",
+ text: "expanded ".repeat(300),
+ });
+ await manager._completeActivationCapture("cloud", {
+ success: true,
+ rawText: "one two",
+ text: "expanded ".repeat(300),
+ });
+ await manager._completeActivationCapture("assistant", {
+ success: true,
+ rawText: "one two",
+ assistantConversation: {},
+ });
+ await manager._completeActivationCapture("output-only", {
+ success: true,
+ text: "expanded ".repeat(300),
+ });
+ assert.deepEqual(counts, [
+ { id: "local", words: 1 },
+ { id: "cloud", words: 2 },
+ { id: "assistant", words: 0 },
+ { id: "output-only", words: 0 },
+ ]);
+});
+
+test("dictation before registration stays outside the prospective tracked count", async (t) => {
+ const r = rig(t);
+ assert.equal(r.manager.begin(), null);
+ await r.manager.flush();
+ assert.equal(r.calls.at(-1).body.gap, false);
+ assert.deepEqual(r.calls.at(-1).body.events, []);
+ await capture(r, 199);
+ assert.equal(r.manager.current().total_words, 199);
+});
+test("an unenrolled device registers on dictation, not on the 30 s timer", async (t) => {
+ t.mock.timers.enable({ apis: ["setInterval"] });
+ const r = rig(t);
+ r.permitted(false);
+ r.manager.start();
+ await r.manager.flush();
+ t.mock.timers.tick(90_000);
+ await r.manager.running;
+ await r.manager.close();
+ assert.equal(r.calls.length, 1);
+ r.permitted(true);
+ assert.equal(r.manager.begin(), null);
+ await r.manager.flush();
+ assert.equal(r.manager.current().state, "open");
+});
+test("the timer retries a registration whose answer was lost", async (t) => {
+ t.mock.timers.enable({ apis: ["setInterval"] });
+ const r = rig(t);
+ r.offline(true);
+ r.manager.start();
+ await r.manager.flush();
+ r.offline(false);
+ t.mock.timers.tick(30_000);
+ await r.manager.running;
+ assert.equal(r.manager.current().state, "open");
+});
+test("a 403 quiets the timer only for an unregistered stream", async (t) => {
+ t.mock.timers.enable({ apis: ["setInterval"] });
+ const r = rig(t);
+ const tick = async () => {
+ t.mock.timers.tick(30_000);
+ await r.manager.running;
+ };
+ r.blocked(true);
+ r.manager.start();
+ await r.manager.flush();
+ await tick();
+ assert.equal(r.calls.length, 1);
+ r.blocked(false);
+ await r.manager.flush();
+ r.blocked(true);
+ await r.manager.flush();
+ r.blocked(false);
+ await tick();
+ assert.deepEqual(
+ r.calls.slice(3).map((c) => c.path),
+ ["/api/activation/batch", "/api/activation/batch"]
+ );
+});
+test("storage failure cannot be erased by finalizer cancellation or clean shutdown", async (t) => {
+ const r = rig(t);
+ await r.manager.flush();
+ r.tick();
+ const id = r.manager.begin();
+ r.tick();
+ r.manager.stopCapture(id);
+ r.db.exec(
+ "CREATE TRIGGER fail_result BEFORE UPDATE OF words ON captures BEGIN SELECT RAISE(ABORT, 'disk full'); END;"
+ );
+ assert.throws(() => r.manager.finish(id, 200));
+ r.manager.cancel(id);
+ assert.equal(r.db.prepare("SELECT count(*) n FROM captures WHERE words IS NULL").get().n, 1);
+ await r.manager.close();
+ assert.equal(r.calls.at(-1).body.gap, true);
+});
+
+test("clean quit with unfinished recognition reports a coverage gap", async (t) => {
+ const r = rig(t);
+ await r.manager.flush();
+ r.tick();
+ const id = r.manager.begin();
+ r.tick();
+ r.manager.stopCapture(id);
+ await r.manager.close();
+ assert.equal(r.calls.at(-1).body.gap, true);
+});
+test("device erasure closes queue database and disables future sync", async (t) => {
+ const r = rig(t);
+ await r.manager.flush();
+ r.manager.dispose();
+ assert.equal(r.db.open, false);
+ const before = r.calls.length;
+ await r.manager.flush();
+ assert.equal(r.calls.length, before);
+});
+test("cancellation during activation IPC cannot restart streaming or retain its late capture", async (t) => {
+ const { window, createManager } = await loadAudioManager(t, {
+ cachePrefix: "activation-cancel-",
+ settingsKey: "__activationCancel",
+ settings: {},
+ });
+ let resolveBegin;
+ const cancelled = [];
+ window.electronAPI.beginActivationCapture = () =>
+ new Promise((resolve) => (resolveBegin = resolve));
+ window.electronAPI.cancelActivationCapture = async (id) => cancelled.push(id);
+ const manager = createManager({
+ micRecovery: { stop() {} },
+ cleanupStreamingAudio() {},
+ cleanupStreamingListeners() {},
+ cleanupPreview: async () => {},
+ getStreamingProvider: () => ({ stop: async () => {} }),
+ _streamingCancellationGeneration: 0,
+ _streamingStartSettlementWaiters: [],
+ });
+ manager.isRecordingAllowedByPolicy = () => true;
+ const starting = manager.startStreamingRecording();
+ await Promise.resolve();
+ const cancelling = manager.cancelStreamingRecording();
+ resolveBegin("late-capture");
+ assert.equal(await starting, false);
+ await cancelling;
+ assert.deepEqual(cancelled, ["late-capture"]);
+ assert.equal(manager._activationCapture ?? null, null);
+ assert.equal(manager.streamingStartInProgress, false);
+});
+
+test("close waits for a closing acknowledgement, not an earlier open batch", async (t) => {
+ const r = rig(t);
+ await r.manager.flush();
+ const original = r.manager.request;
+ let release;
+ let hold = true;
+ const flags = [];
+ r.manager.request = async (req) => {
+ if (req.path.endsWith("/batch")) {
+ flags.push(req.body.close);
+ if (hold) {
+ hold = false;
+ await new Promise((resolve) => (release = resolve));
+ }
+ }
+ return original(req);
+ };
+ const flushing = r.manager.flush();
+ const closing = r.manager.close();
+ release();
+ await Promise.all([flushing, closing]);
+ assert.deepEqual(flags, [false, true]);
+ assert.equal(r.manager.current().state, "closed");
+});
+test("erasing an account's failed capture cannot suppress another account", async (t) => {
+ const r = rig(t);
+ await r.manager.flush();
+ r.tick();
+ const id = r.manager.begin();
+ r.tick();
+ r.manager.stopCapture(id);
+ r.db.exec(
+ "CREATE TRIGGER fail_a BEFORE UPDATE OF words ON captures BEGIN SELECT RAISE(ABORT, 'disk full'); END;"
+ );
+ assert.throws(() => r.manager.finish(id, 10));
+ r.manager.purge("a");
+ r.db.exec("DROP TRIGGER fail_a;");
+ r.scope({ accountId: "b", authGeneration: 2 });
+ await r.manager.flush();
+ await r.manager.close();
+ assert.equal(r.calls.at(-1).body.gap, false);
+ assert.equal(r.manager.failedCaptures.size, 0);
+});
+
+test("excluded assistant capture still honors immediate startup cancellation", async (t) => {
+ const { createManager } = await loadAudioManager(t, {
+ cachePrefix: "activation-assistant-cancel-",
+ settingsKey: "__activationAssistantCancel",
+ settings: {},
+ });
+ const states = [];
+ const manager = createManager({
+ voiceAgentRequested: true,
+ micRecovery: { stop() {} },
+ cleanupStreamingAudio() {},
+ cleanupStreamingListeners() {},
+ cleanupPreview: async () => {},
+ getStreamingProvider: () => ({ stop: async () => {} }),
+ _streamingCancellationGeneration: 0,
+ _streamingStartSettlementWaiters: [],
+ isRecordingAllowedByPolicy: () => true,
+ onStateChange: (state) => states.push(state),
+ });
+ const start = manager.startStreamingRecording();
+ const cancel = manager.cancelStreamingRecording();
+ assert.deepEqual(await Promise.all([start, cancel]), [false, true]);
+ assert.equal(
+ states.some((state) => state.isRecording),
+ false
+ );
+});