Consider it done. The open-source AI agent that works out of the box · 想到,就能做到。开源、开箱即用的 AI Agent。
TypeScript agentai-agentai-assistantandroidclaude-codecodexelectroniosllmmacosreact-nativetypescriptwindows
fix(voice): 校验桌面声纹模型缓存的内容哈希
DavidShen0c4856e96ab7
diff --git a/apps/desktop/src/main/voice-input/speaker/__tests__/speakerFilterService.test.ts b/apps/desktop/src/main/voice-input/speaker/__tests__/speakerFilterService.test.tsindex 3e645869da..a87f96bbc3 100644--- a/apps/desktop/src/main/voice-input/speaker/__tests__/speakerFilterService.test.ts+++ b/apps/desktop/src/main/voice-input/speaker/__tests__/speakerFilterService.test.ts@@ -2,6 +2,7 @@ import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest';import fs from 'node:fs';import os from 'node:os';import path from 'node:path';+import { createHash } from 'node:crypto';import { speakerModelAsset, speakerModelLocalName, SpeakerGate } from '@cindy/voice-input-core';@@ -9,6 +10,20 @@ let tempDir = '';let owner = 'owner-1';let generation = 1;+vi.mock('@cindy/voice-input-core', async (importOriginal) => {+ const actual = await importOriginal<typeof import('@cindy/voice-input-core')>();+ const assets = [+ { role: 'vad', fileName: 'vad.onnx', sha256: createHash('sha256').update(Buffer.alloc(8)).digest('hex'), bytes: 8 },+ { role: 'embedding', fileName: 'embed.onnx', sha256: createHash('sha256').update(Buffer.alloc(16)).digest('hex'), bytes: 16 },+ ] as const;+ return {+ ...actual,+ SPEAKER_MODEL_ASSETS: assets,+ SPEAKER_MODEL_TOTAL_BYTES: 24,+ speakerModelAsset: (role: string) => assets.find((asset) => asset.role === role),+ };+});+vi.mock('electron', () => ({app: { getPath: () => tempDir, once: vi.fn() },ipcMain: { handle: vi.fn() },@@ -57,10 +72,7 @@ function installModels(): void {fs.mkdirSync(dir, { recursive: true });for (const role of ['vad', 'embedding'] as const) {const asset = speakerModelAsset(role);- // Only the size is checked; a sparse file of the right length is enough.- const fd = fs.openSync(path.join(dir, speakerModelLocalName(asset)), 'w');- fs.ftruncateSync(fd, asset.bytes);- fs.closeSync(fd);+ fs.writeFileSync(path.join(dir, speakerModelLocalName(asset)), Buffer.alloc(asset.bytes));}}@@ -111,6 +123,21 @@ describe('speaker filter service', () => {// The worker is started lazily; warming it fails harmlessly in tests.expect(getSpeakerFilterService().createGate(on)).toBeInstanceOf(SpeakerGate);});++ it.runIf(getSpeakerFilterService().supported)('does not load a same-size corrupt model into the gate', () => {+ installModels();+ enroll();+ const service = getSpeakerFilterService();+ expect(service.getStatus().modelState).toBe('ready');+ const asset = speakerModelAsset('embedding');+ const file = path.join(tempDir, 'voice-models', speakerModelLocalName(asset));+ fs.writeFileSync(file, Buffer.alloc(asset.bytes, 1));+ fs.utimesSync(file, new Date(0), new Date(0));+ const warm = vi.spyOn(SpeakerEngineHost.prototype, 'warm');+ expect(service.getStatus().modelState).toBe('missing');+ expect(service.createGate(on)).toBeUndefined();+ expect(warm).not.toHaveBeenCalled();+ });});describe('parseEnrollmentClips', () => {diff --git a/apps/desktop/src/main/voice-input/speaker/__tests__/speakerModelStore.test.ts b/apps/desktop/src/main/voice-input/speaker/__tests__/speakerModelStore.test.tsindex aa4f8fd428..82e3ad205e 100644--- a/apps/desktop/src/main/voice-input/speaker/__tests__/speakerModelStore.test.ts+++ b/apps/desktop/src/main/voice-input/speaker/__tests__/speakerModelStore.test.ts@@ -2,13 +2,14 @@ import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest';import fs from 'node:fs';import os from 'node:os';import path from 'node:path';+import { createHash } from 'node:crypto';-// Tiny stand-ins for the real 28 MB models; the store only cares about size and name.+// Tiny models with real content hashes exercise the same cache validation as production.vi.mock('@cindy/voice-input-core', async (importOriginal) => {const actual = await importOriginal<typeof import('@cindy/voice-input-core')>();const assets = [- { role: 'vad', fileName: 'vad.onnx', sha256: 'a'.repeat(64), bytes: 8 },- { role: 'embedding', fileName: 'embed.onnx', sha256: 'b'.repeat(64), bytes: 16 },+ { role: 'vad', fileName: 'vad.onnx', sha256: createHash('sha256').update(Buffer.alloc(8)).digest('hex'), bytes: 8 },+ { role: 'embedding', fileName: 'embed.onnx', sha256: createHash('sha256').update(Buffer.alloc(16)).digest('hex'), bytes: 16 },] as const;return {...actual,@@ -19,7 +20,8 @@ vi.mock('@cindy/voice-input-core', async (importOriginal) => {});const { SpeakerModelStore } = await import('../speakerModelStore');-const { DownloadError } = await import('../../../downloader');+const { DownloadError, createDownloader } = await import('../../../downloader');+const { SPEAKER_MODEL_ASSETS, speakerModelLocalName, speakerModelCdnPath } = await import('@cindy/voice-input-core');let dir = '';@@ -28,6 +30,7 @@ beforeEach(() => {});afterEach(() => {+ vi.restoreAllMocks();fs.rmSync(dir, { recursive: true, force: true });});@@ -61,15 +64,14 @@ describe('SpeakerModelStore', () => {await store.ensureDownloaded();- expect(calls.map((call) => call.url)).toEqual([- `https://cdn.example.com/voice-models/${'a'.repeat(64)}.onnx`,- `https://cdn.example.com/voice-models/${'b'.repeat(64)}.onnx`,- ]);+ expect(calls.map((call) => call.url)).toEqual(+ SPEAKER_MODEL_ASSETS.map((asset) => `https://cdn.example.com/${speakerModelCdnPath(asset)}`),+ );expect(calls.every((call) => call.sha256 && call.expectedSize)).toBe(true);expect(store.getState()).toMatchObject({ modelState: 'ready', downloadedBytes: 24, totalBytes: 24 });expect(store.getModelPaths()).toEqual({- vadModel: path.join(dir, `${'a'.repeat(16)}-vad.onnx`),- embeddingModel: path.join(dir, `${'b'.repeat(16)}-embed.onnx`),+ vadModel: path.join(dir, speakerModelLocalName(SPEAKER_MODEL_ASSETS[0])),+ embeddingModel: path.join(dir, speakerModelLocalName(SPEAKER_MODEL_ASSETS[1])),});});@@ -81,7 +83,7 @@ describe('SpeakerModelStore', () => {});it('skips models already present and removes files of older versions', async () => {- fs.writeFileSync(path.join(dir, `${'a'.repeat(16)}-vad.onnx`), Buffer.alloc(8));+ fs.writeFileSync(path.join(dir, speakerModelLocalName(SPEAKER_MODEL_ASSETS[0])), Buffer.alloc(8));fs.writeFileSync(path.join(dir, 'old-model.onnx'), 'stale');const calls: DownloadCall[] = [];await createStore(fakeDownload(calls)).ensureDownloaded();@@ -89,6 +91,106 @@ describe('SpeakerModelStore', () => {expect(fs.existsSync(path.join(dir, 'old-model.onnx'))).toBe(false);});+ it('rejects same-size corrupt cached models and replaces them through the real downloader', async () => {+ for (const asset of SPEAKER_MODEL_ASSETS) {+ fs.writeFileSync(path.join(dir, speakerModelLocalName(asset)), Buffer.alloc(asset.bytes, 1));+ }+ const request = vi.fn(async (url: string) => {+ const asset = SPEAKER_MODEL_ASSETS.find((entry) => url.endsWith(speakerModelCdnPath(entry)))!;+ return { response: new Response(new Uint8Array(asset.bytes), {+ headers: { 'content-length': String(asset.bytes) },+ }) };+ });+ const download = createDownloader();+ const store = new SpeakerModelStore({+ dir: () => dir,+ cdnBaseUrl: () => 'https://cdn.example.com',+ download: (options) => download({ ...options, request }),+ });++ expect(store.getState().modelState).toBe('missing');+ expect(store.getModelPaths()).toBeNull();+ await store.ensureDownloaded();+ expect(request).toHaveBeenCalledTimes(2);+ expect(store.getState().modelState).toBe('ready');+ for (const asset of SPEAKER_MODEL_ASSETS) {+ expect(createHash('sha256').update(fs.readFileSync(path.join(dir, speakerModelLocalName(asset)))).digest('hex'))+ .toBe(asset.sha256);+ }+ });++ it('reuses hash checks until a cached file changes, then downloads only that model', async () => {+ const calls: DownloadCall[] = [];+ const store = createStore(fakeDownload(calls));+ await store.ensureDownloaded();+ expect(store.getState().modelState).toBe('ready');+ const read = vi.spyOn(fs, 'readFileSync');+ store.getState();+ store.getModelPaths();+ await store.ensureDownloaded();+ expect(read).not.toHaveBeenCalled();++ const asset = SPEAKER_MODEL_ASSETS[0];+ const file = path.join(dir, speakerModelLocalName(asset));+ fs.writeFileSync(file, Buffer.alloc(asset.bytes, 1));+ fs.utimesSync(file, new Date(0), new Date(0));+ expect(store.getState().modelState).toBe('missing');+ expect(store.getModelPaths()).toBeNull();+ expect(read).toHaveBeenCalledTimes(1);++ await store.ensureDownloaded();+ expect(calls).toHaveLength(3);+ expect(calls[2].targetPath).toBe(file);+ expect(store.getState().modelState).toBe('ready');+ });++ it('invalidates a replaced file even when its size and modification time are preserved', async () => {+ const store = createStore(fakeDownload([]));+ await store.ensureDownloaded();+ const asset = SPEAKER_MODEL_ASSETS[0];+ const file = path.join(dir, speakerModelLocalName(asset));+ const modified = new Date(1_000);+ fs.utimesSync(file, modified, modified);+ expect(store.getState().modelState).toBe('ready');++ const replacement = path.join(dir, 'replacement');+ fs.writeFileSync(replacement, Buffer.alloc(asset.bytes, 1));+ fs.utimesSync(replacement, modified, modified);+ fs.renameSync(replacement, file);+ expect(store.getState().modelState).toBe('missing');+ expect(store.getModelPaths()).toBeNull();+ });++ it('rejects a file changed during verification instead of caching the earlier bytes', async () => {+ const store = createStore(fakeDownload([]));+ await store.ensureDownloaded();+ const asset = SPEAKER_MODEL_ASSETS[0];+ const file = path.join(dir, speakerModelLocalName(asset));+ const readFile = fs.readFileSync.bind(fs);+ vi.spyOn(fs, 'readFileSync').mockImplementationOnce((...args) => {+ const data = readFile(...args);+ fs.writeFileSync(file, Buffer.alloc(asset.bytes, 1));+ fs.utimesSync(file, new Date(0), new Date(0));+ return data;+ });+ // A fresh store has no cached result and must validate the files on disk.+ const reopened = createStore(fakeDownload([]));+ expect(reopened.getState().modelState).toBe('missing');+ expect(reopened.getModelPaths()).toBeNull();+ });++ it('does not retain verification after a cached file disappears', async () => {+ const store = createStore(fakeDownload([]));+ await store.ensureDownloaded();+ expect(store.getState().modelState).toBe('ready');+ const asset = SPEAKER_MODEL_ASSETS[0];+ const file = path.join(dir, speakerModelLocalName(asset));+ fs.unlinkSync(file);+ expect(store.getState().modelState).toBe('missing');+ fs.writeFileSync(file, Buffer.alloc(asset.bytes, 1));+ expect(store.getModelPaths()).toBeNull();+ });+it('classifies a checksum failure and allows a retry', async () => {const calls: DownloadCall[] = [];let failOnce = true;diff --git a/apps/desktop/src/main/voice-input/speaker/speakerModelStore.ts b/apps/desktop/src/main/voice-input/speaker/speakerModelStore.tsindex be27672b0c..2c8b8e0da8 100644--- a/apps/desktop/src/main/voice-input/speaker/speakerModelStore.ts+++ b/apps/desktop/src/main/voice-input/speaker/speakerModelStore.ts@@ -1,5 +1,6 @@import fs from 'node:fs';import path from 'node:path';+import { createHash } from 'node:crypto';import {SPEAKER_MODEL_ASSETS,@@ -29,10 +30,11 @@ type SpeakerModelStoreOptions = {/*** Downloads the speaker-gate models on demand and reports where they are.*- * Files are fetched from the region CDN by content hash and only become- * visible after the downloader verified SHA-256, so a present file of the- * expected size is trusted. One download runs at a time; asking again while- * it runs joins it.+ * Files are fetched from the region CDN by content hash. Cached files must+ * also pass SHA-256 before use; size alone cannot detect local corruption.+ * Verification is reused while the file identity and timestamps are unchanged,+ * keeping status polls and audio chunks from repeatedly hashing the models.+ * One download runs at a time; asking again while it runs joins it.*/export class SpeakerModelStore {private readonly download: (options: DownloadOptions) => Promise<DownloadResult>;@@ -40,6 +42,7 @@ export class SpeakerModelStore {private abort: AbortController | null = null;private progress = new Map<string, number>();private lastError: VoiceSpeakerFilterStatus['downloadError'];+ private readonly checkedFiles = new Map<string, { file: string; stamp: string; valid: boolean }>();constructor(private readonly options: SpeakerModelStoreOptions) {this.download = options.download ?? createDownloader();@@ -146,14 +149,38 @@ export class SpeakerModelStore {}private isInstalled(asset: SpeakerModelAsset): boolean {+ const file = this.localPath(asset);try {- const stat = fs.statSync(this.localPath(asset));- return stat.isFile() && stat.size === asset.bytes;+ const stamp = this.fileStamp(file, asset.bytes);+ if (stamp === null) {+ this.checkedFiles.delete(asset.sha256);+ return false;+ }+ const checked = this.checkedFiles.get(asset.sha256);+ if (checked?.file === file && checked.stamp === stamp) return checked.valid;++ const valid = createHash('sha256').update(fs.readFileSync(file)).digest('hex') === asset.sha256;+ // Do not publish a result for a file replaced or written during the read.+ if (this.fileStamp(file, asset.bytes) !== stamp) {+ this.checkedFiles.delete(asset.sha256);+ return false;+ }+ // Remember mismatches too, so polling a corrupt model does not rehash it.+ this.checkedFiles.set(asset.sha256, { file, stamp, valid });+ return valid;} catch {+ this.checkedFiles.delete(asset.sha256);return false;}}+ private fileStamp(file: string, bytes: number): string | null {+ const stat = fs.statSync(file, { bigint: true });+ if (!stat.isFile() || stat.size !== BigInt(bytes)) return null;+ // ctime and file identity catch replacements even when mtime is preserved.+ return `${stat.dev}:${stat.ino}:${stat.size}:${stat.mtimeNs}:${stat.ctimeNs}`;+ }+/** Drops files from older model versions; keeps partial downloads. */private removeStaleFiles(dir: string): void {const keep = new Set(SPEAKER_MODEL_ASSETS.map(speakerModelLocalName));