diff --git a/CHANGELOG.md b/CHANGELOG.md index d733d56..4594135 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -36,6 +36,32 @@ adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html). - **"Watch smart folders" is now "Keep Smart Folders up to date".** Its description says it watches where files go, not where they come from, and names the ways files come in. +### Fixed + +- **Spreadsheets from Visio and .NET tools are read.** Some tools write a workbook's XML with a + prefix on every tag (``), and StratoSort read those workbooks as empty. They are now read + like any other. +- **Cloud analysis is no longer held to one file at a time.** With a cloud model, a batch still took + the concurrency the local graphics card recommends, which on a busy or small card was 1. Documents + analyzed in the cloud now run at the requested concurrency; images and media, which run on local + models, keep the hardware limit. +- **The search index Refresh button stays still during analysis.** Background refreshes of the index + counts, every 1.5 seconds during a batch, spun and disabled the button and faded the counts. They + now update the numbers quietly; a refresh you ask for still shows that it is loading. +- **Search and Chat name files as they are on disk.** A file not yet organized was listed under the + name StratoSort suggested for it, which did not exist yet. Search results and Chat sources now use + the file's current name. Rebuild the search index to update files analyzed earlier. +- **Cloud analysis no longer reloads the local text model after images.** With a cloud provider + doing the document analysis, StratoSort still loaded the local text model after every batch of + images. On a busy machine that pushed the embedding model out of memory, and it reloaded for the + next file. The text model now loads only when something local needs it. +- **The notice for a Smart Folder whose AI provider is not ready is accurate.** It said the local + model would be used; the folder actually falls back to your default provider, which the notice now + names, along with the reason (such as a missing API key). It appears once a minute per folder + instead of for every file. +- **Embedding problems say why.** The warning for a high embedding error rate now names the most + frequent errors. + ## [5.4.0] - 2026-11-16 ### Added diff --git a/src/main/analysis/documentExtractors.js b/src/main/analysis/documentExtractors.js index ed6c183..f26dc5f 100644 --- a/src/main/analysis/documentExtractors.js +++ b/src/main/analysis/documentExtractors.js @@ -2182,8 +2182,22 @@ async function extractTextFromDocx(filePath, options = {}) { throw mammothError; } +/** + * Spreadsheet XML with its element prefixes removed: "" becomes + * "". Excel writes the main namespace as the default one, but Visio and + * .NET (Open XML SDK) exports often bind it to a prefix, and the readers below + * match unprefixed tags only, so such a workbook read as empty. Attribute + * names ("r:id") and declarations (" 10000) { try { @@ -2224,6 +2238,7 @@ async function extractTextFromXlsxWorksheetXml( /** @type {any} */ rowBudget ) { if (!worksheetXml || typeof worksheetXml !== 'string') return { text: '', rowsUsed: 0 }; + worksheetXml = withoutElementPrefixes(worksheetXml); const pool = getTextExtractionPool(); if (pool && worksheetXml.length > 10000) { try { diff --git a/src/main/analysis/providerStamp.js b/src/main/analysis/providerStamp.js index 8a82f83..511f60f 100644 --- a/src/main/analysis/providerStamp.js +++ b/src/main/analysis/providerStamp.js @@ -42,7 +42,28 @@ function stampAnalysisProvider(/** @type {any} */ analysis, /** @type {any} */ p return analysis; } +/** + * True when document analysis goes to a cloud provider (the active router's + * side-effect-free preview for 'analyze' names one other than local). The + * local text model then does no document work, so it need not be loaded or + * limit concurrency. False when there is no router or on any doubt. + * @returns {boolean} + */ +function isDocumentAnalysisRemote() { + try { + // Lazy: aiProvider requires the services tree. + const aiProvider = require('../ipc/aiProvider'); + const router = aiProvider.getActiveAiRouter ? aiProvider.getActiveAiRouter() : null; + if (typeof router?.previewChoiceId !== 'function') return false; + const providerId = router.previewChoiceId({ operation: 'analyze' }); + return Boolean(providerId) && providerId !== 'local'; + } catch { + return false; + } +} + module.exports = { resolveProviderIdFromRouter, - stampAnalysisProvider + stampAnalysisProvider, + isDocumentAnalysisRemote }; diff --git a/src/main/core/systemAnalytics.js b/src/main/core/systemAnalytics.js index 87aca99..8c3ef5f 100644 --- a/src/main/core/systemAnalytics.js +++ b/src/main/core/systemAnalytics.js @@ -226,11 +226,26 @@ const systemAnalytics = { } bucket.lastDegradedAt = now; + // The reasons, most frequent first: a rate alone left a steady 25% + // embedding error rate with no cause in the log. + /** @type {Record} */ + const reasonCounts = {}; + for (const entry of bucket.recent) { + if (entry.outcome === 'primary') continue; + const reason = String(entry.reason || 'unknown').slice(0, 160); + reasonCounts[reason] = (reasonCounts[reason] || 0) + 1; + } + const topReasons = Object.entries(reasonCounts) + .sort((a, b) => b[1] - a[1]) + .slice(0, 5) + .map(([reason, count]) => ({ reason, count })); + logger.warn('[ANALYTICS] Pipeline degradation detected', { pipeline: pipelineName, recentTotal, fallbackRate: Number(fallbackRate.toFixed(3)), - errorRate: Number(errorRate.toFixed(3)) + errorRate: Number(errorRate.toFixed(3)), + topReasons }); }, diff --git a/src/main/ipc/aiProvider.js b/src/main/ipc/aiProvider.js index 905f39c..b2b6976 100644 --- a/src/main/ipc/aiProvider.js +++ b/src/main/ipc/aiProvider.js @@ -366,9 +366,23 @@ function registerAiProviderIpc(servicesOrParams) { safeSendToMain(getMainWindow, IPC_CHANNELS.AI_PROVIDER.STATUS_CHANGED, _routerStatus()); }); _router.on('folder-override-unavailable', (payload) => { - const providerLabel = payload?.configuredProvider || 'cloud'; + // Once a minute per folder: the router raises this for every file in + // the folder, and a reanalysis touches hundreds. + const key = `folder:${payload?.folderId}:${payload?.configuredProvider}`; + const now = Date.now(); + if (now - (_operationOverrideNoticeAt.get(key) || 0) < OVERRIDE_NOTICE_INTERVAL_MS) return; + _operationOverrideNoticeAt.set(key, now); + // The router falls through to the default provider (then local), not + // straight to local: name the one that actually runs. + const fallbackId = + _router?.previewChoiceId?.({ operation: payload?.operation || 'analyze' }) || 'local'; safeSendToMain(getMainWindow, IPC_EVENTS.NOTIFICATION, { - message: `Smart folder AI override (${providerLabel}) is unavailable. Using the local model.`, + message: folderOverrideNotice({ + configured: payload?.configuredProvider, + reason: payload?.reason, + fallbackId, + providers: _providers + }), severity: 'warning', duration: 8000, source: 'ai-provider', @@ -823,6 +837,24 @@ function getActiveAiRouter() { return _router; } +/** + * The notice for a Smart Folder whose AI provider is not ready. It names the + * provider that runs instead: the router falls through to the default + * provider, and the old text ("Using the local model") was wrong whenever the + * default was a cloud provider. + * @param {{ configured?: string, reason?: string, fallbackId?: string|null, providers?: Record|null }} p + * @returns {string} + */ +function folderOverrideNotice({ configured, reason, fallbackId, providers }) { + const label = (/** @type {string|null|undefined} */ id) => providers?.[id || '']?.displayName; + const configuredLabel = label(configured) || configured || 'the chosen provider'; + const fallbackLabel = + !fallbackId || fallbackId === 'local' ? 'the local model' : label(fallbackId) || fallbackId; + const why = reason ? ` (${reason})` : ''; + return `A Smart Folder is set to ${configuredLabel}, which is not ready${why}. Using ${fallbackLabel} instead. Change it in Settings → AI.`; +} + module.exports = registerAiProviderIpc; +module.exports.folderOverrideNotice = folderOverrideNotice; module.exports.disposeAiProvider = disposeAiProvider; module.exports.getActiveAiRouter = getActiveAiRouter; diff --git a/src/main/ipc/semantic.js b/src/main/ipc/semantic.js index def7adf..25feab7 100644 --- a/src/main/ipc/semantic.js +++ b/src/main/ipc/semantic.js @@ -2762,7 +2762,9 @@ async function handleFullRebuild( continue; } - const displayName = organization.newName || entry.fileName || path.basename(filePath); + // The name on disk: organization.newName is only a suggestion + // until the file is moved. + const displayName = path.basename(filePath) || entry.fileName; const ext = (path.extname(filePath) || '').toLowerCase(); const isImage = SUPPORTED_IMAGE_EXTENSIONS.includes(ext); const fileId = getFileEmbeddingId(filePath, isImage ? 'image' : 'file'); @@ -3511,8 +3513,9 @@ async function handleRebuildFiles( // Generate embedding const { vector, model } = await getFolderMatcher().embedText(summary); - // Use renamed name if available, otherwise fall back to original basename - const displayName = entry.organization?.newName || path.basename(filePath); + // The name on disk: organization.newName is only a suggestion until + // the file is moved. + const displayName = path.basename(filePath); // Extract rich metadata for graph visualization const tags = Array.isArray(entry.analysis?.tags) ? entry.analysis.tags : []; diff --git a/src/main/services/BatchAnalysisService.js b/src/main/services/BatchAnalysisService.js index 58a05ec..31b2153 100644 --- a/src/main/services/BatchAnalysisService.js +++ b/src/main/services/BatchAnalysisService.js @@ -81,6 +81,7 @@ const { getRecommendedConcurrency } = require('./PerformanceService'); const { getInstance: getModelAccessCoordinator } = require('./ModelAccessCoordinator'); const { getFileTypeSingular } = require('./autoOrganize/fileTypeUtils'); +const { isDocumentAnalysisRemote } = require('../analysis/providerStamp'); /** * BatchAnalysisService @@ -202,7 +203,28 @@ class BatchAnalysisService { } } - async _getAdaptiveConcurrency(/** @type {any} */ requestedConcurrency, autoConcurrency = true) { + /** + * True when document analysis goes to a cloud provider: the local model is + * not the one doing that work, so the local hardware cap does not apply to + * it. Reads the router's side-effect-free preview; false on any doubt. + * @returns {boolean} + */ + _isDocumentAnalysisRemote() { + return isDocumentAnalysisRemote(); + } + + /** + * @param {any} requestedConcurrency + * @param {boolean} [autoConcurrency] + * @param {{ remote?: boolean }} [options] remote: the work runs on a cloud + * provider, so the hardware (VRAM/RAM) cap is skipped; queue pressure on + * the local models still applies. + */ + async _getAdaptiveConcurrency( + /** @type {any} */ requestedConcurrency, + autoConcurrency = true, + { remote = false } = {} + ) { let capped = requestedConcurrency; let perfRecommendation = null; @@ -223,7 +245,9 @@ class BatchAnalysisService { // When the user has explicitly opted into manual concurrency, we honor // their value here; the queue-pressure reductions below (runtime // backpressure) still apply in both modes to protect against OOM. + // Work on a cloud provider does not run on this hardware at all. if ( + !remote && autoConcurrency !== false && Number.isFinite(perfRecommendation?.maxConcurrent) && perfRecommendation.maxConcurrent > 0 @@ -303,6 +327,15 @@ class BatchAnalysisService { const concurrency = disableAdaptiveConcurrency ? requestedConcurrency : await this._getAdaptiveConcurrency(requestedConcurrency, autoConcurrency); + // Documents analyzed by a cloud provider are not limited by local VRAM: + // a reanalysis through a cloud model ran one file at a time because the + // local hardware recommended 1. Images and media still run locally. + const documentConcurrency = + disableAdaptiveConcurrency || !this._isDocumentAnalysisRemote() + ? concurrency + : await this._getAdaptiveConcurrency(requestedConcurrency, autoConcurrency, { + remote: true + }); if (!Array.isArray(filePaths) || filePaths.length === 0) { return { @@ -322,6 +355,7 @@ class BatchAnalysisService { fileCount: filePaths.length, requestedConcurrency, concurrency, + documentConcurrency, sectionOrder: orderedSections, documentCount: partitioned.documents.length, imageCount: partitioned.images.length, @@ -402,7 +436,8 @@ class BatchAnalysisService { } const sectionFiles = partitioned[section]; const sectionIsImage = section === 'images'; - const sectionConcurrency = Math.max(1, Math.min(concurrency, sectionFiles.length || 1)); + const sectionCap = section === 'documents' ? documentConcurrency : concurrency; + const sectionConcurrency = Math.max(1, Math.min(sectionCap, sectionFiles.length || 1)); const sectionStart = Date.now(); // Vision batch mode is on by default in production. Tests can opt out // by passing { enableVisionBatchMode: false } in options. The previous diff --git a/src/main/services/LlamaService.js b/src/main/services/LlamaService.js index aa407ec..8372619 100644 --- a/src/main/services/LlamaService.js +++ b/src/main/services/LlamaService.js @@ -4413,6 +4413,17 @@ class LlamaService extends EventEmitter { // Rapid exit -> enter: batch mode was re-entered, or another batch is on // its way in, before we even start. if (this._visionBatchMode || this._visionBatchEntriesPending > 0) return; + const { resetLlamaCircuit } = require('./LlamaResilience'); + // Documents go to a cloud provider: nothing will use the local text + // model. Loading it after every image batch (a 7B model on a busy card) + // evicted the embedding model, which then reloaded for the next file. + // A local call that does need it still loads it on demand. + const { isDocumentAnalysisRemote } = require('../analysis/providerStamp'); + if (isDocumentAnalysisRemote()) { + for (const circuit of circuitsToReset) resetLlamaCircuit(circuit); + logger.debug('[LlamaService] Skipped text preload after vision: analysis is remote'); + return; + } let loaded; try { // speculative: this load does not hold the text lane, so the next @@ -4440,7 +4451,6 @@ class LlamaService extends EventEmitter { } return; } - const { resetLlamaCircuit } = require('./LlamaResilience'); for (const circuit of circuitsToReset) { resetLlamaCircuit(circuit); } diff --git a/src/main/services/SearchService.js b/src/main/services/SearchService.js index e35ba2f..e6f45ff 100644 --- a/src/main/services/SearchService.js +++ b/src/main/services/SearchService.js @@ -33,6 +33,7 @@ const { LRUCache } = require('../../shared/LRUCache'); const { normalizePathForIndex } = require('../../shared/pathSanitization'); const { validateEmbeddingDimensions, padOrTruncateVector } = require('../../shared/vectorMath'); const { getSemanticFileId } = require('../../shared/fileIdUtils'); +const { crossPlatformBasename } = require('../../shared/crossPlatformUtils'); const { createBackgroundWorkTracker } = require('../../shared/promiseUtils'); const { isGhostCleanupDeferred } = require('../../shared/ghostCleanupGuard'); const systemAnalytics = require('../core/systemAnalytics'); @@ -529,7 +530,11 @@ class SearchService { // If file was moved/renamed, use the actual destination path const currentPath = organization.actual || doc.originalPath; - const currentName = organization.newName || doc.fileName || ''; + // The name on disk. organization.newName is the suggested name from + // the moment a file is analyzed, before anything is renamed: search + // and chat cited files by names that did not exist yet. + const currentName = + (currentPath ? crossPlatformBasename(currentPath) : '') || doc.fileName || ''; // Use normalizePathForIndex for Windows case-insensitivity consistency // This ensures BM25 index keys match vector DB lookups diff --git a/src/renderer/components/search/hooks/useSearchModalStats.js b/src/renderer/components/search/hooks/useSearchModalStats.js index 056768a..c3c269b 100644 --- a/src/renderer/components/search/hooks/useSearchModalStats.js +++ b/src/renderer/components/search/hooks/useSearchModalStats.js @@ -82,10 +82,15 @@ export function useSearchModalStats({ // state written by a newer one (rapid open/close + rebuild races). const statsRequestCounterRef = useRef(0); + // `quiet`: a background refresh (batch progress, a file move) updates the + // counts without the loading state. Batch progress refreshed every 1.5 s, + // and each one spun and disabled the Refresh button and faded the counts: + // the button flashed for the whole run. const refreshStats = useCallback(async (/** @type {any} */ options = {}) => { const forceRefresh = options === true || options?.force === true; + const quiet = options?.quiet === true; const requestId = (statsRequestCounterRef.current += 1); - if (isMountedRef.current) setIsLoadingStats(true); + if (isMountedRef.current && !quiet) setIsLoadingStats(true); try { /** @type {any} */ let res = null; @@ -169,7 +174,7 @@ export function useSearchModalStats({ }); onFileOperation?.(data); embeddingsIpc.invalidateStatsCache(); - refreshStats(true); + refreshStats({ force: true, quiet: true }); } }; @@ -194,7 +199,7 @@ export function useSearchModalStats({ const elapsed = now - lastRefreshAt; const invoke = () => { lastRefreshAt = Date.now(); - refreshStats({ force }); + refreshStats({ force, quiet: true }); }; if (force || elapsed >= MIN_REFRESH_INTERVAL_MS) { diff --git a/test/BatchAnalysisService.remoteConcurrency.test.js b/test/BatchAnalysisService.remoteConcurrency.test.js new file mode 100644 index 0000000..e0c591c --- /dev/null +++ b/test/BatchAnalysisService.remoteConcurrency.test.js @@ -0,0 +1,129 @@ +/** + * Documents analyzed by a cloud provider are not limited by local VRAM: a + * reanalysis through a cloud model ran one file at a time because the local + * hardware recommended a concurrency of 1. Queue pressure on the local + * models (embeddings, vision) still applies. + */ + +jest.mock('../src/shared/logger', () => { + const logger = { + setContext: jest.fn(), + info: jest.fn(), + debug: jest.fn(), + warn: jest.fn(), + error: jest.fn() + }; + return { logger, createLogger: jest.fn(() => logger) }; +}); + +jest.mock('../src/shared/config/index', () => ({ + get: jest.fn((key, defaultValue) => defaultValue) +})); + +jest.mock('../src/main/utils/llmOptimization', () => ({ + globalBatchProcessor: { concurrencyLimit: 3, processBatch: jest.fn(), getStats: jest.fn() }, + BatchProcessor: jest.fn().mockImplementation(() => ({ + concurrencyLimit: 3, + processBatch: jest.fn(), + getStats: jest.fn() + })) +})); + +// The local hardware can take one model call at a time. +jest.mock('../src/main/services/PerformanceService', () => ({ + getRecommendedConcurrency: jest.fn().mockResolvedValue({ + maxConcurrent: 1, + reason: 'low VRAM', + vramMB: 4000 + }) +})); + +const mockQueueStats = { + text: { queued: 0, pending: 0 }, + vision: { queued: 0, pending: 0 }, + embedding: { queued: 0, pending: 0 } +}; +jest.mock('../src/main/services/ModelAccessCoordinator', () => ({ + getInstance: jest.fn(() => ({ getQueueStats: jest.fn(() => mockQueueStats) })) +})); + +jest.mock('../src/main/services/LlamaService', () => ({ + getInstance: jest.fn(() => ({ + enterVisionBatchMode: jest.fn(), + exitVisionBatchMode: jest.fn() + })) +})); +jest.mock('../src/main/analysis/documentAnalysis', () => ({ + analyzeDocumentFile: jest.fn(), + flushAllEmbeddings: jest.fn() +})); +jest.mock('../src/main/analysis/imageAnalysis', () => ({ + analyzeImageFile: jest.fn(), + flushAllEmbeddings: jest.fn() +})); +jest.mock('../src/main/services/ParallelEmbeddingService', () => ({ + getInstance: jest.fn(() => ({ + concurrencyLimit: 5, + getStats: jest.fn(() => ({})), + setConcurrencyLimit: jest.fn() + })) +})); +jest.mock('../src/main/analysis/embeddingQueue/stageQueues', () => ({ + analysisQueue: { + getStats: jest.fn(() => ({})), + onProgress: jest.fn(() => () => {}), + forceFlush: jest.fn(), + shutdown: jest.fn() + } +})); +jest.mock('../src/main/analysis/embeddingQueue/queueManager', () => ({ + forceFlush: jest.fn(), + shutdown: jest.fn() +})); + +const mockPreviewChoiceId = jest.fn(); +jest.mock('../src/main/ipc/aiProvider', () => ({ + getActiveAiRouter: jest.fn(() => ({ previewChoiceId: mockPreviewChoiceId })) +})); + +const BatchAnalysisService = require('../src/main/services/BatchAnalysisService'); + +describe('batch concurrency when documents go to a cloud provider', () => { + let service; + + beforeEach(() => { + mockPreviewChoiceId.mockReset(); + Object.assign(mockQueueStats.embedding, { queued: 0, pending: 0 }); + service = new BatchAnalysisService(); + }); + + test('the local hardware cap still applies to local work', async () => { + await expect(service._getAdaptiveConcurrency(4, true)).resolves.toBe(1); + }); + + test('cloud work keeps the requested concurrency', async () => { + await expect(service._getAdaptiveConcurrency(4, true, { remote: true })).resolves.toBe(4); + }); + + test('queue pressure on the local models still limits cloud work', async () => { + Object.assign(mockQueueStats.embedding, { queued: 3, pending: 1 }); + await expect(service._getAdaptiveConcurrency(4, true, { remote: true })).resolves.toBe(2); + }); + + test('document analysis counts as remote only when the router picks a cloud provider', () => { + mockPreviewChoiceId.mockReturnValue('anthropic'); + expect(service._isDocumentAnalysisRemote()).toBe(true); + expect(mockPreviewChoiceId).toHaveBeenCalledWith({ operation: 'analyze' }); + + mockPreviewChoiceId.mockReturnValue('local'); + expect(service._isDocumentAnalysisRemote()).toBe(false); + + mockPreviewChoiceId.mockReturnValue(null); + expect(service._isDocumentAnalysisRemote()).toBe(false); + + mockPreviewChoiceId.mockImplementation(() => { + throw new Error('router not ready'); + }); + expect(service._isDocumentAnalysisRemote()).toBe(false); + }); +}); diff --git a/test/LlamaService.visionBatchLane.test.js b/test/LlamaService.visionBatchLane.test.js index 8f9168f..9ba8715 100644 --- a/test/LlamaService.visionBatchLane.test.js +++ b/test/LlamaService.visionBatchLane.test.js @@ -68,6 +68,13 @@ jest.mock('../src/main/services/VisionService', () => ({ VISION_RUNTIME_MEMORY_TYPE: 'vision-runtime' })); +// Document analysis runs locally unless a test says otherwise. +const mockIsDocumentAnalysisRemote = jest.fn(() => false); +jest.mock('../src/main/analysis/providerStamp', () => ({ + ...jest.requireActual('../src/main/analysis/providerStamp'), + isDocumentAnalysisRemote: () => mockIsDocumentAnalysisRemote() +})); + const { LlamaService } = require('../src/main/services/LlamaService'); const { ModelMemoryManager } = require('../src/main/services/ModelMemoryManager'); @@ -430,6 +437,18 @@ describe('LlamaService: the text preload after a batch', () => { expect(manager.unloadModel).toHaveBeenCalledWith('text'); }); + test('skips the text model when documents are analyzed in the cloud', async () => { + // A reanalysis through Claude loaded a 7B local text model after every + // image batch; it evicted the embedding model, which reloaded each time. + mockIsDocumentAnalysisRemote.mockReturnValueOnce(true); + const { resetLlamaCircuit } = require('../src/main/services/LlamaResilience'); + + await service._preloadTextAfterVisionRelease(['embedding']); + + expect(manager.ensureModelLoaded).not.toHaveBeenCalled(); + expect(resetLlamaCircuit).toHaveBeenCalledWith('embedding'); + }); + test('gives way to a batch that is on its way in', async () => { service._visionBatchEntriesPending = 1; diff --git a/test/SearchService.extended.test.js b/test/SearchService.extended.test.js index d50176b..59e6d1e 100644 --- a/test/SearchService.extended.test.js +++ b/test/SearchService.extended.test.js @@ -124,6 +124,26 @@ describe('SearchService (extended)', () => { expect(oldMeta).toBeDefined(); }); + test('a file not yet organized is named as it is on disk, not by its suggested name', async () => { + // Analysis records the suggested name in organization.newName before + // anything is renamed; chat cited files by that name. + const entries = { + held: { + id: 'held', + timestamp: '2026-11-17T00:00:00.000Z', + originalPath: 'C:\\Inbox\\example.com-Performance-on-Search-2026-10.xlsx', + fileName: 'example.com-Performance-on-Search-2026-10.xlsx', + analysis: { subject: 'Search performance', tags: ['seo'] }, + organization: { newName: 'search-performance-2026-11-17.xlsx', renamed: true } + } + }; + const { service } = createService({ historyEntries: entries }); + await service.buildBM25Index(); + + const [meta] = Array.from(service.documentMap.values()); + expect(meta.name).toBe('example.com-Performance-on-Search-2026-10.xlsx'); + }); + test('returns indexed=0 and sets indexBuiltAt when there are no documents', async () => { const { service } = createService({ historyEntries: {} }); const result = await service.buildBM25Index(); diff --git a/test/documentExtractors.xlsxPrefixedNamespace.test.js b/test/documentExtractors.xlsxPrefixedNamespace.test.js new file mode 100644 index 0000000..c77690c --- /dev/null +++ b/test/documentExtractors.xlsxPrefixedNamespace.test.js @@ -0,0 +1,126 @@ +/** + * @jest-environment node + * + * A workbook whose spreadsheet XML binds the main namespace to a prefix + * ("", ""), as Visio and .NET (Open XML SDK) exports + * write it, used to read as empty: the readers matched unprefixed tags only. + * Real files in a temp dir; synthetic content only. + */ + +jest.unmock('fs'); +jest.unmock('fs/promises'); +jest.unmock('os'); + +const fsSync = require('fs'); +const path = require('path'); + +jest.mock('../src/shared/logger', () => { + const logger = { + info: jest.fn(), + warn: jest.fn(), + error: jest.fn(), + debug: jest.fn(), + setContext: jest.fn() + }; + return { createLogger: jest.fn(() => logger), logger }; +}); + +jest.mock('../src/main/analysis/analysisLimits', () => ({ + getAnalysisLimits: jest.fn().mockResolvedValue({ + maxFileSize: 100 * 1024 * 1024, + maxImageFileSize: 100 * 1024 * 1024, + maxDocumentFileSize: 100 * 1024 * 1024 + }), + getDefaultLimits: jest.fn().mockReturnValue({ + maxFileSize: 100 * 1024 * 1024, + maxImageFileSize: 100 * 1024 * 1024, + maxDocumentFileSize: 100 * 1024 * 1024 + }), + invalidateCache: jest.fn() +})); + +const AdmZip = require('adm-zip'); +const { extractTextFromXlsx } = require('../src/main/analysis/documentExtractors'); + +const MAIN_NS = 'http://schemas.openxmlformats.org/spreadsheetml/2006/main'; +const REL_NS = 'http://schemas.openxmlformats.org/officeDocument/2006/relationships'; + +/** A minimal workbook with every spreadsheet element written as "x:". */ +function prefixedWorkbook(file) { + const zip = new AdmZip(); + const add = (name, xml) => zip.addFile(name, Buffer.from(xml, 'utf8')); + add( + '[Content_Types].xml', + '' + + '' + + '' + + '' + + '' + + '' + + '' + + '' + ); + add( + '_rels/.rels', + '' + + '' + + '' + + '' + ); + add( + 'xl/_rels/workbook.xml.rels', + '' + + '' + + '' + + '' + + '' + ); + add( + 'xl/workbook.xml', + `` + + '' + ); + add( + 'xl/sharedStrings.xml', + `` + + 'Shape nameOwner' + + 'Loading dockFacilities team' + ); + add( + 'xl/worksheets/sheet1.xml', + `` + + '01' + + 'Area m2' + + '23' + + '412' + + '' + ); + zip.writeZip(file); +} + +describe('XLSX with a prefixed spreadsheet namespace', () => { + let tmpDir; + let xlsxPath; + + beforeAll(() => { + tmpDir = fsSync.mkdtempSync( + path.join(jest.requireActual('os').tmpdir(), 'stratosort-xlsx-prefix-') + ); + xlsxPath = path.join(tmpDir, 'floor-plan-shapes.xlsx'); + prefixedWorkbook(xlsxPath); + }); + + afterAll(() => { + fsSync.rmSync(tmpDir, { recursive: true, force: true }); + }); + + test('reads its shared strings, inline strings and numbers', async () => { + const text = await extractTextFromXlsx(xlsxPath); + expect(text).toContain('Shape name'); + expect(text).toContain('Owner'); + expect(text).toContain('Area m2'); + expect(text).toContain('Loading dock'); + expect(text).toContain('Facilities team'); + expect(text).toContain('412'); + }); +}); diff --git a/test/e2e/README.md b/test/e2e/README.md index 151dd94..4de4156 100644 --- a/test/e2e/README.md +++ b/test/e2e/README.md @@ -18,7 +18,7 @@ up every `*.spec.js` under `test/e2e/`, so a new spec only needs to be put in th | `settings/` | Settings panel: backup, models, AI providers, MCP | 6 | | `workflows/` | Journeys that cross several phases, run on seeded data (no model needed) | 2 | | `live-llm/` | `@live-llm` specs that need real local models | 3 | -| `manual/` | Opt-in runs that are never part of CI: soak, QA repro, local routing review | 3 | +| `manual/` | Opt-in runs that are never part of CI: soak, QA repro, routing, reanalysis | 4 | | `helpers/` | Launch helpers, page objects, fixtures, global setup/teardown (not specs) | - | ### Spec index @@ -111,13 +111,14 @@ All three are tagged `@live-llm` and run with `npm run test:e2e:live-llm`. Set #### `manual/` - opt-in, never in CI -All three skip themselves in a plain `npm run test:e2e` run when their requirement is missing. +All four skip themselves in a plain `npm run test:e2e` run when their requirement is missing. -| Spec | Purpose | CI smoke | Needs | -| ------------------------------ | ---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- | -------- | -------------------------------------------------------------------- | -| `soak.spec.js` | 10-15 minute stability loop with memory sampling | | `SOAK=1` (use `npm run test:soak`); optional `SOAK_MINUTES` | -| `qa-runtime-repro.spec.js` | QA repro session for backlog items (settings, chat streaming, graph edges) | | Windows, with the real models in `%APPDATA%\StratoSort Stack\models` | -| `local-routing-review.spec.js` | Small Search Console exports through the local model: folder, score, held, closest folder, for three folder descriptions; then a timed Chat question. Writes `test-results/local-routing-review/report.md` | | `STRATOSORT_E2E_ROUTING_REVIEW=1` and `STRATOSORT_E2E_MODELS_DIR` | +| Spec | Purpose | CI smoke | Needs | +| ------------------------------------ | ---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- | -------- | ----------------------------------------------------------------------- | +| `soak.spec.js` | 10-15 minute stability loop with memory sampling | | `SOAK=1` (use `npm run test:soak`); optional `SOAK_MINUTES` | +| `qa-runtime-repro.spec.js` | QA repro session for backlog items (settings, chat streaming, graph edges) | | Windows, with the real models in `%APPDATA%\StratoSort Stack\models` | +| `local-routing-review.spec.js` | Small Search Console exports through the local model: folder, score, held, closest folder, for three folder descriptions; then a timed Chat question. Writes `test-results/local-routing-review/report.md` | | `STRATOSORT_E2E_ROUTING_REVIEW=1` and `STRATOSORT_E2E_MODELS_DIR` | +| `reanalyze-embedding-errors.spec.js` | Reanalyze all on a Smart Folder of 3D models and notes; reports the embedding pipeline's outcomes and error reasons. Writes `test-results/reanalyze-embedding-errors/report.json` | | `STRATOSORT_E2E_REANALYZE_EMBEDDINGS=1` and `STRATOSORT_E2E_MODELS_DIR` | ## Prerequisites diff --git a/test/e2e/helpers/electronApp.js b/test/e2e/helpers/electronApp.js index 817231b..8c7d73d 100644 --- a/test/e2e/helpers/electronApp.js +++ b/test/e2e/helpers/electronApp.js @@ -1011,6 +1011,7 @@ module.exports = { closeApp, createIsolatedE2eUserDataDir, cleanupE2eUserDataDir, + linkLocalModels, killOrphanStratoSortElectronProcesses, focusAppForKeyboardShortcuts, safeWaitForTimeout, diff --git a/test/e2e/manual/local-routing-review.spec.js b/test/e2e/manual/local-routing-review.spec.js index 947ccc0..2c935fe 100644 --- a/test/e2e/manual/local-routing-review.spec.js +++ b/test/e2e/manual/local-routing-review.spec.js @@ -565,6 +565,12 @@ test.describe('Local routing review (real models)', () => { ? { query, ...(await askChat(window, query)) } : { query, ok: false, reason: 'exports did not reach the search index in 120 s' }; + // The embedding pipeline's outcomes over the run, with reasons for errors. + report.embeddingPipeline = await window.evaluate(async () => { + const metrics = await window.electronAPI?.system?.getMetrics?.(); + return metrics?.pipeline?.embedding || metrics?.data?.pipeline?.embedding || null; + }); + await fs.mkdir(REPORT_DIR, { recursive: true }); await fs.writeFile(path.join(REPORT_DIR, 'report.json'), JSON.stringify(report, null, 2)); await fs.writeFile(path.join(REPORT_DIR, 'report.md'), renderMarkdown(report)); diff --git a/test/e2e/manual/reanalyze-embedding-errors.spec.js b/test/e2e/manual/reanalyze-embedding-errors.spec.js new file mode 100644 index 0000000..68db1bf --- /dev/null +++ b/test/e2e/manual/reanalyze-embedding-errors.spec.js @@ -0,0 +1,222 @@ +/** + * "Reanalyze all" with the local models: does the embedding pipeline record + * errors, and why? + * + * A reanalysis of a full library logged a steady 25% embedding error rate + * from the moment "Reanalyze all" started, while analysis through Discover + * recorded none. This puts a few files inside a Smart Folder (3D models + * first, as in that run), runs Reanalyze all, waits for the Smart Folder + * watcher to finish and reports the embedding pipeline's outcomes and error + * reasons. Writes test-results/reanalyze-embedding-errors/report.json. + * + * Cloud analysis: set STRATOSORT_E2E_CLOUD_PROFILE to a real StratoSort + * profile folder. Its AI settings and encrypted provider keys ("ai-keys", + * with the "Local State" that holds the key for them) are copied into the + * fresh test profile, so analysis runs on that profile's cloud provider while + * embeddings stay local. The copy is deleted with the test profile; the real + * profile is only read. + * + * Run (Windows, from the repo root, after `npm run build`): + * $env:STRATOSORT_E2E_REANALYZE_EMBEDDINGS='1' + * $env:STRATOSORT_E2E_MODELS_DIR="$env:APPDATA\StratoSort Stack\models" + * npx playwright test test/e2e/manual/reanalyze-embedding-errors.spec.js --retries=0 + */ + +const { test, expect } = require('@playwright/test'); +const path = require('path'); +const fs = require('fs/promises'); +const fsSync = require('fs'); +const { + launchApp, + closeApp, + waitForAppReady, + createIsolatedE2eUserDataDir, + cleanupE2eUserDataDir, + linkLocalModels +} = require('../helpers/electronApp'); +const { createTempDir, cleanupTempDir } = require('../helpers/testFixtures'); +const { waitForLoadedTextModel } = require('../helpers/liveLlm'); + +const ENABLED = process.env.STRATOSORT_E2E_REANALYZE_EMBEDDINGS === '1'; +const CLOUD_PROFILE = process.env.STRATOSORT_E2E_CLOUD_PROFILE || ''; +// A prepared profile used as-is: its Smart Folders and index, nothing added. +// The caller owns it (it is not deleted here). Name it outside the +// "stratosort-e2e-*" pattern, which the global teardown deletes. Careful: +// Reanalyze all queues every file in the profile's analysis history, not only +// the files in its Smart Folders; with a cloud provider that is billed. +const USE_PROFILE = process.env.STRATOSORT_E2E_USE_PROFILE || ''; + +/** + * A fresh test profile with the local models, plus the cloud provider set-up + * of CLOUD_PROFILE: its "ai" settings, its encrypted keys and the Local State + * that decrypts them (same Windows user). Nothing else is copied. + * @returns {string} + */ +function cloudTestProfile() { + const dir = createIsolatedE2eUserDataDir(); + linkLocalModels(dir); + fsSync.copyFileSync(path.join(CLOUD_PROFILE, 'Local State'), path.join(dir, 'Local State')); + fsSync.cpSync(path.join(CLOUD_PROFILE, 'ai-keys'), path.join(dir, 'ai-keys'), { + recursive: true + }); + const real = JSON.parse(fsSync.readFileSync(path.join(CLOUD_PROFILE, 'settings.json'), 'utf8')); + const settingsPath = path.join(dir, 'settings.json'); + const seeded = JSON.parse(fsSync.readFileSync(settingsPath, 'utf8')); + fsSync.writeFileSync(settingsPath, JSON.stringify({ ...seeded, ai: real.ai }, null, 2)); + return dir; +} +const REPORT_DIR = path.join( + __dirname, + '..', + '..', + '..', + 'test-results', + 'reanalyze-embedding-errors' +); + +/** An ASCII STL: a few triangles and next to no words. */ +function asciiStl(name, facets) { + const lines = [`solid ${name}`]; + for (let i = 0; i < facets; i += 1) { + lines.push( + ' facet normal 0 0 1', + ' outer loop', + ` vertex ${i} 0 0`, + ` vertex ${i + 1} 0 0`, + ` vertex ${i} 1 0`, + ' endloop', + ' endfacet' + ); + } + lines.push(`endsolid ${name}`); + return lines.join('\n'); +} + +async function writeLibrary(dir) { + await fs.mkdir(dir, { recursive: true }); + const files = { + '17-round-2025-05-27.stl': asciiStl('round', 40), + '1mm-1-2025-05-30.stl': asciiStl('spacer', 12), + '2.stl': asciiStl('part', 4), + 'bracket-notes.txt': + 'Notes on the shelf bracket print: PETG, 0.2 mm layers, 30% infill, supports off.', + 'print-log.csv': + 'date,model,minutes,filament_g\n2026-11-01,bracket,95,41\n2026-11-03,spacer,12,3\n', + 'empty-notes.txt': '' + }; + for (const [name, content] of Object.entries(files)) { + await fs.writeFile(path.join(dir, name), content, 'utf8'); + } + return Object.keys(files); +} + +test.describe('Reanalyze all: embedding outcomes (real models)', () => { + test.skip(!ENABLED, 'Set STRATOSORT_E2E_REANALYZE_EMBEDDINGS=1 to run.'); + + let app; + let window; + let workDir; + let profileDir = null; + + test.afterAll(async () => { + // Stop the watcher's queue first: Reanalyze all keeps working in the + // background, and an app left running after the test kept analyzing + // (and, with a cloud provider, kept spending). + if (window) { + await window + .evaluate(async () => window.electronAPI.smartFolders.watcherStop()) + .catch(() => {}); + } + if (app) await closeApp(app); + if (workDir) await cleanupTempDir(workDir); + if (profileDir) cleanupE2eUserDataDir(profileDir); + }); + + test('records why embeddings fail during Reanalyze all', async () => { + test.setTimeout(30 * 60_000); + if (USE_PROFILE) { + ({ app, window } = await launchApp({ userDataDir: USE_PROFILE })); + } else if (CLOUD_PROFILE) { + profileDir = cloudTestProfile(); + ({ app, window } = await launchApp({ userDataDir: profileDir })); + } else { + ({ app, window } = await launchApp({ localModels: true })); + } + await waitForAppReady(window); + // A cloud provider does the analysis in the other modes; only a local + // run needs the text model. + if (!USE_PROFILE && !CLOUD_PROFILE) { + const modelReady = await waitForLoadedTextModel(window); + test.skip(!modelReady, 'No text model: set STRATOSORT_E2E_MODELS_DIR.'); + } + + let names = []; + if (!USE_PROFILE) { + workDir = await createTempDir('stratosort-reanalyze-'); + const folderPath = path.join(workDir, '3D print'); + names = await writeLibrary(folderPath); + const added = await window.evaluate( + async (p) => + window.electronAPI.smartFolders.add({ + name: '3D print', + path: p, + description: '3D printer models (STL) and print notes' + }), + folderPath + ); + expect(added?.success, JSON.stringify(added)).toBe(true); + } + + const metrics = async () => + window.evaluate(async () => (await window.electronAPI.system.getMetrics())?.pipeline); + const before = await metrics(); + + const started = Date.now(); + // A profile that already has Smart Folders may not have started the + // watcher yet; Reanalyze all needs it. + let reanalyze = await window.evaluate(async () => + window.electronAPI.embeddings.reanalyzeAll({}) + ); + if (reanalyze?.errorCode === 'WATCHER_NOT_AVAILABLE') { + const startedWatcher = await window.evaluate(async () => + window.electronAPI.smartFolders.watcherStart() + ); + console.log('[reanalyze] watcher start:', JSON.stringify(startedWatcher).slice(0, 200)); + reanalyze = await window.evaluate(async () => window.electronAPI.embeddings.reanalyzeAll({})); + } + console.log('[reanalyze] started:', JSON.stringify(reanalyze).slice(0, 300)); + + // Wait for the watcher to take the files and finish them. + let status = null; + let sawWork = false; + while (Date.now() - started < 20 * 60_000) { + await new Promise((r) => setTimeout(r, 5000)); + status = await window.evaluate(async () => window.electronAPI.smartFolders.watcherStatus()); + const s = status?.status || status; + const busy = (Number(s?.queueLength) || 0) + (Number(s?.processingCount) || 0); + if (busy > 0) sawWork = true; + if (sawWork && busy === 0) break; + if (!sawWork && Date.now() - started > 120_000) break; + } + const after = await metrics(); + + const report = { + cloud: Boolean(CLOUD_PROFILE), + provider: await window.evaluate(async () => { + const s = await window.electronAPI.settings.get(); + return s?.ai?.routing?.default || null; + }), + files: names, + reanalyze, + watcherStatus: status, + seconds: Math.round((Date.now() - started) / 1000), + embeddingBefore: before?.embedding || null, + embeddingAfter: after?.embedding || null, + documentAfter: after?.document || null + }; + await fs.mkdir(REPORT_DIR, { recursive: true }); + await fs.writeFile(path.join(REPORT_DIR, 'report.json'), JSON.stringify(report, null, 2)); + console.log('[reanalyze] embedding after:', JSON.stringify(after?.embedding, null, 1)); + expect(after?.embedding, 'embedding pipeline metrics').toBeTruthy(); + }); +}); diff --git a/test/renderer/components/search/useSearchModalStats.test.js b/test/renderer/components/search/useSearchModalStats.test.js index 26ae1d9..1761a4f 100644 --- a/test/renderer/components/search/useSearchModalStats.test.js +++ b/test/renderer/components/search/useSearchModalStats.test.js @@ -125,6 +125,55 @@ describe('useSearchModalStats', () => { await waitFor(() => expect(mockGetStatsCached).toHaveBeenCalled()); }); + test('a background refresh during a batch keeps the Refresh button still', async () => { + const { result } = mount(); + await waitFor(() => expect(result.current.hasLoadedStats).toBe(true)); + + // Hold the background refresh open so its in-flight state is visible. + let finish; + mockGetStatsCached.mockClear(); + mockGetStatsCached.mockImplementationOnce( + () => + new Promise((resolve) => { + finish = () => resolve({ success: true, files: 9, folders: 3, initialized: true }); + }) + ); + act(() => { + window.dispatchEvent( + new CustomEvent('operation-progress', { + detail: { type: 'batch_analyze', completed: 1, total: 4 } + }) + ); + }); + await waitFor(() => expect(mockGetStatsCached).toHaveBeenCalled()); + expect(result.current.isLoadingStats).toBe(false); + + await act(async () => { + finish(); + }); + expect(result.current.stats).toMatchObject({ files: 9 }); + }); + + test('a refresh the user asks for still shows that it is loading', async () => { + const { result } = mount(); + await waitFor(() => expect(result.current.hasLoadedStats).toBe(true)); + let finish; + mockGetStatsCached.mockImplementationOnce( + () => + new Promise((resolve) => { + finish = () => resolve({ success: true, files: 8, folders: 3, initialized: true }); + }) + ); + act(() => { + result.current.refreshStats(true); + }); + expect(result.current.isLoadingStats).toBe(true); + await act(async () => { + finish(); + }); + expect(result.current.isLoadingStats).toBe(false); + }); + test('stats refreshes read embedding settings from the store, not over IPC', async () => { mockUiSettings = { embeddingTiming: 'after_organize', defaultEmbeddingPolicy: 'skip' }; const { result } = mount(); diff --git a/test/systemAnalytics.metrics.test.js b/test/systemAnalytics.metrics.test.js index 8e31571..acb4247 100644 --- a/test/systemAnalytics.metrics.test.js +++ b/test/systemAnalytics.metrics.test.js @@ -11,13 +11,14 @@ * from the recent window (primary outcomes and null reasons excluded). */ +const mockLogger = { + info: jest.fn(), + warn: jest.fn(), + error: jest.fn(), + debug: jest.fn() +}; jest.mock('../src/shared/logger', () => ({ - createLogger: () => ({ - info: jest.fn(), - warn: jest.fn(), - error: jest.fn(), - debug: jest.fn() - }) + createLogger: () => mockLogger })); const systemAnalytics = require('../src/main/core/systemAnalytics'); @@ -120,3 +121,33 @@ describe('systemAnalytics pipeline fallbackReasons', () => { expect(summary.document.fallbackReasons).toEqual({ filename_fallback: 1 }); }); }); + +describe('systemAnalytics degradation warning', () => { + beforeEach(() => { + systemAnalytics.destroy(); + mockLogger.warn.mockClear(); + }); + + test('names the most frequent error reasons, not only the rate', () => { + for (let i = 0; i < 15; i += 1) { + systemAnalytics.recordPipelineOutcome('embedding', 'primary', { reason: 'ok' }); + } + for (let i = 0; i < 4; i += 1) { + systemAnalytics.recordPipelineOutcome('embedding', 'error', { reason: 'Input is too long' }); + } + systemAnalytics.recordPipelineOutcome('embedding', 'error', { reason: 'Context disposed' }); + + const call = mockLogger.warn.mock.calls.find( + ([msg]) => msg === '[ANALYTICS] Pipeline degradation detected' + ); + expect(call).toBeDefined(); + expect(call[1]).toMatchObject({ + pipeline: 'embedding', + errorRate: 0.25, + topReasons: [ + { reason: 'Input is too long', count: 4 }, + { reason: 'Context disposed', count: 1 } + ] + }); + }); +}); diff --git a/test/unit/ai/folderOverrideNotice.test.js b/test/unit/ai/folderOverrideNotice.test.js new file mode 100644 index 0000000..85dc11a --- /dev/null +++ b/test/unit/ai/folderOverrideNotice.test.js @@ -0,0 +1,36 @@ +/** + * The notice for a Smart Folder whose AI provider is not ready names the + * provider that actually runs: the router falls through to the default, and + * the old text said "Using the local model" even when that was Claude. + */ +const { folderOverrideNotice } = require('../../../src/main/ipc/aiProvider'); + +const providers = { + local: { displayName: 'Local Llama' }, + anthropic: { displayName: 'Anthropic Claude' }, + openaiCompatible: { displayName: 'OpenAI / Custom' } +}; + +describe('folderOverrideNotice', () => { + test('names the default provider that takes over', () => { + expect( + folderOverrideNotice({ + configured: 'openaiCompatible', + reason: 'API key is missing or invalid', + fallbackId: 'anthropic', + providers + }) + ).toBe( + 'A Smart Folder is set to OpenAI / Custom, which is not ready (API key is missing or invalid). Using Anthropic Claude instead. Change it in Settings → AI.' + ); + }); + + test('says the local model only when that is what runs', () => { + expect( + folderOverrideNotice({ configured: 'anthropic', fallbackId: 'local', providers }) + ).toContain('Using the local model instead.'); + expect( + folderOverrideNotice({ configured: 'anthropic', fallbackId: null, providers }) + ).toContain('Using the local model instead.'); + }); +});