diff --git a/.github/workflows/mac-release.yml b/.github/workflows/mac-release.yml index dc060f73..c771889b 100644 --- a/.github/workflows/mac-release.yml +++ b/.github/workflows/mac-release.yml @@ -44,9 +44,11 @@ env: jobs: resolve-release-target: runs-on: ubuntu-latest + # The only output is the validated tag (v.., confirmed + # to exist). Every checkout below pins refs/tags/, so no job checks + # out a branch or any other caller-chosen ref. outputs: tag: ${{ steps.resolve.outputs.tag }} - ref: ${{ steps.resolve.outputs.ref }} steps: - name: Resolve and validate release target id: resolve @@ -63,7 +65,6 @@ jobs: return; } core.setOutput("tag", tag); - core.setOutput("ref", `refs/tags/${tag}`); return; } @@ -87,7 +88,6 @@ jobs: } core.setOutput("tag", requestedTag); - core.setOutput("ref", `refs/tags/${requestedTag}`); build-macos: needs: resolve-release-target @@ -111,7 +111,7 @@ jobs: - name: Checkout uses: actions/checkout@3d3c42e5aac5ba805825da76410c181273ba90b1 # v7.0.1 with: - ref: ${{ needs.resolve-release-target.outputs.ref }} + ref: refs/tags/${{ needs.resolve-release-target.outputs.tag }} fetch-depth: 0 - name: Verify package version matches release tag env: @@ -123,7 +123,26 @@ jobs: uses: actions/setup-node@949feb2413d6458794dcd2491c4babbbce0c15c1 # v7.1.0 with: node-version-file: '.nvmrc' - cache: npm + # Restore-only npm cache (next two steps): a release build reads the + # cache that ordinary CI writes on main but never saves one, so a + # release run cannot seed the default-branch cache scope. setup-node + # would save in its post step, so its own caching stays off. + package-manager-cache: false + + - name: Locate npm cache directory + id: npm-cache-dir + run: echo "dir=$(npm config get cache)" >> "$GITHUB_OUTPUT" + + - name: Restore npm cache (read-only) + # Same path and key format as setup-node's `cache: npm` (used by CI on + # main), so its entries match here. matrix.arch equals the runner's + # os.arch() on every leg of this job. + uses: actions/cache/restore@55cc8345863c7cc4c66a329aec7e433d2d1c52a9 # v6.1.0 + with: + path: ${{ steps.npm-cache-dir.outputs.dir }} + key: node-cache-${{ runner.os }}-${{ matrix.arch }}-npm-${{ hashFiles('package-lock.json') }} + restore-keys: | + node-cache-${{ runner.os }}-${{ matrix.arch }}-npm- - name: Verify macOS build assets run: node scripts/verify-build-assets.js --platform=mac @@ -383,7 +402,7 @@ jobs: - name: Checkout uses: actions/checkout@3d3c42e5aac5ba805825da76410c181273ba90b1 # v7.0.1 with: - ref: ${{ needs.resolve-release-target.outputs.ref }} + ref: refs/tags/${{ needs.resolve-release-target.outputs.tag }} fetch-depth: 0 - name: Free disk space on runner @@ -423,7 +442,26 @@ jobs: uses: actions/setup-node@949feb2413d6458794dcd2491c4babbbce0c15c1 # v7.1.0 with: node-version-file: '.nvmrc' - cache: npm + # Restore-only npm cache (next two steps): a release build reads the + # cache that ordinary CI writes on main but never saves one, so a + # release run cannot seed the default-branch cache scope. setup-node + # would save in its post step, so its own caching stays off. + package-manager-cache: false + + - name: Locate npm cache directory + id: npm-cache-dir + run: echo "dir=$(npm config get cache)" >> "$GITHUB_OUTPUT" + + - name: Restore npm cache (read-only) + # Same path and key format as setup-node's `cache: npm` (used by CI on + # main), so its entries match here. matrix.arch equals the runner's + # os.arch() on every leg of this job. + uses: actions/cache/restore@55cc8345863c7cc4c66a329aec7e433d2d1c52a9 # v6.1.0 + with: + path: ${{ steps.npm-cache-dir.outputs.dir }} + key: node-cache-${{ runner.os }}-${{ matrix.arch }}-npm-${{ hashFiles('package-lock.json') }} + restore-keys: | + node-cache-${{ runner.os }}-${{ matrix.arch }}-npm- - name: Install dependencies (with optional deps) run: npm ci --include=optional @@ -549,7 +587,7 @@ jobs: - name: Checkout uses: actions/checkout@3d3c42e5aac5ba805825da76410c181273ba90b1 # v7.0.1 with: - ref: ${{ needs.resolve-release-target.outputs.ref }} + ref: refs/tags/${{ needs.resolve-release-target.outputs.tag }} fetch-depth: 0 - name: Download macOS artifacts diff --git a/docs/MCP_SERVER.md b/docs/MCP_SERVER.md index f6f782db..f862945f 100644 --- a/docs/MCP_SERVER.md +++ b/docs/MCP_SERVER.md @@ -129,7 +129,7 @@ Search the indexed corpus by meaning, keyword, or both. ```json { - "query": "string (1..1000)", + "query": "string (2..1000)", "top_k": 10, "mode": "hybrid", "smart_folder_id": "optional-uuid" diff --git a/scripts/compose-walkthrough-media.js b/scripts/compose-walkthrough-media.js index ca42ea7e..cca1377f 100644 --- a/scripts/compose-walkthrough-media.js +++ b/scripts/compose-walkthrough-media.js @@ -29,8 +29,6 @@ const ROOT = path.resolve(__dirname, '..'); const OUT_DIR = path.join(ROOT, 'docs', 'walkthrough-assets'); const RAW_DIR = path.join(OUT_DIR, 'tour-raw'); const SHORT_DIR = path.join(OUT_DIR, 'shorts'); -const VIDEO = path.join(RAW_DIR, 'app.webm'); -const ACTIONS = path.join(RAW_DIR, 'actions.json'); const LOGO = path.join(ROOT, 'assets', 'stratosort-logo.png'); const FPS = 25; @@ -470,9 +468,13 @@ function probeVideo(file) { }; } -/** Paths inside a filter graph: forward slashes, and the drive colon escaped. */ +/** + * Paths inside a filter graph: forward slashes, and the drive colon escaped. + * One pass handles both characters, so every backslash in the input becomes + * '/' and the only backslashes in the result are the ones escaping ':'. + */ function ffPath(file) { - return file.replace(/\\/g, '/').replace(/:/g, '\\:'); + return file.replace(/[\\:]/g, (ch) => (ch === ':' ? '\\:' : '/')); } const even = (n) => Math.max(2, Math.floor(n / 2) * 2); diff --git a/scripts/verify-splash-csp-hash.js b/scripts/verify-splash-csp-hash.js index 28df0821..7c81d8fc 100644 --- a/scripts/verify-splash-csp-hash.js +++ b/scripts/verify-splash-csp-hash.js @@ -24,7 +24,9 @@ function main() { } const html = fs.readFileSync(indexPath, 'utf8'); const inlineBodies = []; - const re = /]*)>([\s\S]*?)<\/script>/gi; + // The end tag may carry whitespace or junk before '>' (""); the + // browser still ends the script there, so the hashed body must end there too. + const re = /]*)>([\s\S]*?)<\/script\b[^>]*>/gi; let m; while ((m = re.exec(html)) !== null) { const attrs = m[1]; diff --git a/src/main/analysis/contentSelector.js b/src/main/analysis/contentSelector.js index 66b672e7..5377393c 100644 --- a/src/main/analysis/contentSelector.js +++ b/src/main/analysis/contentSelector.js @@ -47,6 +47,25 @@ const MAX_HEADINGS = 25; // Internal helpers // --------------------------------------------------------------------------- +/** + * Remove HTML tags, repeating until nothing changes so no tag-like text can + * survive (for example "ipt>" never leaves "]+>/g, ''); + } while (result !== previous); + return result; +} + /** * Lightweight text cleaning that preserves paragraph structure. * @@ -165,8 +184,7 @@ function extractDocumentOutline(text, maxLength) { let heading = (match[1] || match[0]).trim(); // Strip residual HTML tags and Markdown # - heading = heading - .replace(/<[^>]+>/g, '') + heading = stripHtmlTags(heading) .replace(/^#+\s*/, '') .trim(); diff --git a/src/main/analysis/documentAnalysis.js b/src/main/analysis/documentAnalysis.js index 6ed4d877..bff7c04b 100644 --- a/src/main/analysis/documentAnalysis.js +++ b/src/main/analysis/documentAnalysis.js @@ -1016,7 +1016,7 @@ async function analyzeDocumentStages(filePath, smartFolders = [], options = {}, ? extractedText.slice(0, 8192) + '\0' + extractedText.length : extractedText; const contentHash = crypto - .createHash('md5') + .createHash('sha256') .update(hashInput) .update(safeModelName) .digest('hex'); @@ -1292,7 +1292,9 @@ async function extractCadOrDesignText(filePath, fileExtension) { return cad.extractTextFromEps(filePath); case '.ai': return cad.extractTextFromAi(filePath, { - extractTextFromPdf: (/** @type {string} */ p) => extractTextFromPdf(p, path.basename(p)) + // No OCR follows on this route, so nothing would release a pinned buffer. + extractTextFromPdf: (/** @type {string} */ p) => + extractTextFromPdf(p, path.basename(p), { pinForOcr: false }) }); default: return ''; diff --git a/src/main/analysis/documentExtractors.js b/src/main/analysis/documentExtractors.js index 55e3d82f..ed6c1830 100644 --- a/src/main/analysis/documentExtractors.js +++ b/src/main/analysis/documentExtractors.js @@ -244,9 +244,10 @@ const REGEX_PATTERNS = { rtfBraces: /[{}]/g, rtfControlWords: /\\[a-zA-Z]+-?\d* ?/g, - // HTML patterns - scriptTags: //gi, - styleTags: //gi, + // HTML patterns. End tags may carry whitespace or junk before '>' + // ("", ""); browsers still close the element there. + scriptTags: /]*>/gi, + styleTags: /]*>/gi, htmlTags: /<[^>]+>/g, // Whitespace patterns @@ -1100,12 +1101,16 @@ function csvRowCells(row) { * head of a CSV over the text size limit, like the other text formats. */ async function extractTextFromCsv(filePath, options = {}) { - // Treat CSV as structured text; enforce file size to avoid memory blowups - const fileSize = await checkFileSize(filePath, filePath); const maxBytes = options && Number.isFinite(options.maxBytes) && Number(options.maxBytes) > 0 ? Math.floor(Number(options.maxBytes)) : null; + // Treat CSV as structured text; enforce file size to avoid memory blowups. + // A capped read only loads maxBytes, so the document size limit does not + // apply (a finite override, since checkFileSize reads Infinity as "unset"). + const fileSize = maxBytes + ? await checkFileSize(filePath, filePath, Number.MAX_SAFE_INTEGER) + : await checkFileSize(filePath, filePath); let raw = await readTextFileWithEncoding(filePath, { maxBytes }); if (maxBytes && fileSize > maxBytes) { // The cap lands mid-row; a half row would parse as a short or broken @@ -1231,10 +1236,12 @@ function isEncryptedPdfError(/** @type {any} */ err) { /** * @param {any} filePath * @param {any} fileName - * @param {{ signal?: AbortSignal }} [options] + * @param {{ signal?: AbortSignal, pinForOcr?: boolean }} [options] pinForOcr: + * false for callers that never run ocrPdfIfNeeded (the .ai reader), so a + * text-less file is not left pinned in the buffer cache until the TTL sweep. */ async function extractTextFromPdf(filePath, fileName, options = {}) { - const { signal } = options; + const { signal, pinForOcr = true } = options; await checkFileSize(filePath, fileName); throwIfAborted(signal); @@ -1302,7 +1309,7 @@ async function extractTextFromPdf(filePath, fileName, options = {}) { // ocrPdfIfNeeded, which releases them in its finally. pdf.js may have // transferred the ArrayBuffer to its worker, leaving a detached // zero-length husk — never cache that. - if (rawBuffer.length > 0) _pdfBufferCache.set(filePath, rawBuffer); + if (pinForOcr && rawBuffer.length > 0) _pdfBufferCache.set(filePath, rawBuffer); if (thinTextLayer) { logger.info('[PDF] Text layer too thin to analyze; treating as a scan', { fileName, @@ -3493,7 +3500,7 @@ function chunkTextForAnalysis( * cannot outlive its consumer and pin its buffer in `_pdfBufferCache`. * @param {string} filePath - Path to PDF file * @param {string} fileName - Name of file for error messages - * @param {{ signal?: AbortSignal }} [options] + * @param {{ signal?: AbortSignal, pinForOcr?: boolean }} [options] * @returns {Promise} Extracted text */ async function extractTextFromPdfWithTimeout(filePath, fileName, options = {}) { diff --git a/src/main/analysis/documentLlm.js b/src/main/analysis/documentLlm.js index c5e919de..59b75dfb 100644 --- a/src/main/analysis/documentLlm.js +++ b/src/main/analysis/documentLlm.js @@ -674,11 +674,13 @@ ${truncated} return inference(); } - return globalDeduplicator.deduplicate( - dedupeKey, - inference, - { type: 'document', fileName: originalFileName } // Metadata for debugging cache hits - ); + return globalDeduplicator.deduplicate(dedupeKey, inference, { + type: 'document', + fileName: originalFileName, + // Lets a caller that joined this call re-run it when the + // starter's abort (not its own) ends the shared inference. + signal: abortController.signal + }); }, AppConfig.ai.textAnalysis.timeout, `Document analysis for ${originalFileName}`, diff --git a/src/main/analysis/embeddingQueue/EmbeddingQueueCore.js b/src/main/analysis/embeddingQueue/EmbeddingQueueCore.js index 171c6263..9074ebb9 100644 --- a/src/main/analysis/embeddingQueue/EmbeddingQueueCore.js +++ b/src/main/analysis/embeddingQueue/EmbeddingQueueCore.js @@ -33,6 +33,37 @@ const { createProgressTracker } = require('./progress'); const logger = createLogger('EmbeddingQueue'); // Shortest wait before the failed-item retry timer fires. const FAILED_RETRY_MIN_DELAY_MS = 1000; + +/** + * A model file name without its `.gguf` extension or quantization tag, so + * 'nomic-embed-text-v1.5-Q8_0.gguf' and 'nomic-embed-text-v1.5-Q4_K_M.gguf' + * compare equal. Quantizations of one model embed into the same vector space, + * which LlamaService's same-dimension fallback chain already relies on. + * @param {string} modelName + * @returns {string} + */ +function embeddingModelKey(modelName) { + return String(modelName) + .trim() + .toLowerCase() + .replace(/\.gguf$/, '') + .replace(/[-._](?:i?q\d\w*|bf16|f16|f32)$/, ''); +} + +/** + * Whether a queued item's vector came from a model other than `indexModel`. + * Producers stamp `item.model` with the model that computed the vector; an + * item without a stamp gives no answer (null). + * @param {any} item + * @param {string} indexModel + * @returns {boolean | null} + */ +function isFromOtherModel(item, indexModel) { + const model = item?.model; + if (typeof model !== 'string' || !model.trim()) return null; + return embeddingModelKey(model) !== embeddingModelKey(indexModel); +} + class EmbeddingQueue { /** * @param {{ persistenceFileName?: string, batchSize?: number, flushDelayMs?: number, parallelFlushConcurrency?: number, failedItemsPath?: string, deadLetterPath?: string }} [options] @@ -887,6 +918,14 @@ class EmbeddingQueue { this.retryCount = 0; this._offlineRetryCount = 0; + // Covers items restored from disk after a restart and items whose + // vector was computed just before a model change but enqueued after it. + const { removed: staleItems } = this._dropOtherModelItems( + typeof vectorDbService.getEmbeddingModel === 'function' + ? vectorDbService.getEmbeddingModel() + : null + ); + const fileItems = []; const folderItems = []; @@ -894,6 +933,7 @@ class EmbeddingQueue { // Also skip items that were marked for removal during flush for (const item of batch) { if (this._pendingRemovals?.has(item.id)) continue; + if (staleItems.has(item)) continue; if (item.id.startsWith('folder:')) { folderItems.push(item); } else { @@ -962,7 +1002,7 @@ class EmbeddingQueue { // Clear failed-item records for items that succeeded in this batch let clearedFailed = 0; for (const item of batch) { - if (!failedItemIds.has(item.id)) { + if (!failedItemIds.has(item.id) && !staleItems.has(item)) { if (this._failedItemHandler.failedItems.delete(item.id)) { clearedFailed += 1; } @@ -970,7 +1010,8 @@ class EmbeddingQueue { } const flushDuration = Date.now() - flushStartTime; - const successCount = batch.length - failedItemIds.size; + const staleInBatch = batch.filter((item) => staleItems.has(item)).length; + const successCount = batch.length - failedItemIds.size - staleInBatch; logger.info('[EmbeddingQueue] Successfully flushed batch', { success: successCount, @@ -1464,6 +1505,67 @@ class EmbeddingQueue { return totalQueueUpdated; } + /** + * Remove pending vectors that another embedding model produced from the + * queue, the failed-items map and the dead-letter queue, so none is written + * into an index built for `indexModel`. With a different dimension such a + * vector fails the write and churns into the dead-letter queue; with the + * same dimension it is written silently and mixes two vector spaces. + * The files are not lost: a model change resets the index and offers a + * rebuild, and the startup knowledge repair re-embeds every analyzed file + * that has no vector. + * @param {string | null} indexModel - Model the index holds; null skips the check + * @param {{ dropUnstamped?: boolean }} [options] - dropUnstamped: also drop + * items with no model stamp (safe only on a model change, see dropOtherModelEmbeddings) + * @returns {{ removed: Set, total: number }} Queue items removed, and the + * count across all three stores + * @private + */ + _dropOtherModelItems(indexModel, { dropUnstamped = false } = {}) { + /** @type {Set} */ + const removed = new Set(); + if (typeof indexModel !== 'string' || !indexModel.trim()) return { removed, total: 0 }; + + /** @param {any} item */ + const isStale = (item) => { + const other = isFromOtherModel(item, indexModel); + return other === null ? dropUnstamped : other; + }; + + // In place: a running flush filters its batch out of this same array. + for (let i = this.queue.length - 1; i >= 0; i--) { + if (isStale(this.queue[i])) removed.add(this.queue.splice(i, 1)[0]); + } + const { failed, deadLetter } = this._failedItemHandler.dropItems(isStale); + const total = removed.size + failed + deadLetter; + if (total > 0) { + logger.info(`[EmbeddingQueue] Dropped ${total} pending embeddings from another model`, { + queue: this._persistenceKeys.queue, + indexModel, + queued: removed.size, + failed, + deadLetter + }); + if (removed.size > 0) this._schedulePersist({ queue: true }); + } + return { removed, total }; + } + + /** + * Drop every pending vector not stamped with `newModel` after the user + * switches embedding models. Unstamped items go too: all of them were + * computed before the switch, and the switch resets the index and offers a + * rebuild that re-embeds every analyzed file from its stored analysis. + * A queue not yet restored from disk is skipped; flush applies the same + * check to stamped items once they load. + * @param {string} newModel + * @returns {number} Items dropped + */ + dropOtherModelEmbeddings(newModel) { + if (!this.initialized) return 0; + return this._dropOtherModelItems(newModel, { dropUnstamped: true }).total; + } + /** * Get queue statistics */ diff --git a/src/main/analysis/embeddingQueue/failedItemHandler.js b/src/main/analysis/embeddingQueue/failedItemHandler.js index 97a6532a..f7c8c0c8 100644 --- a/src/main/analysis/embeddingQueue/failedItemHandler.js +++ b/src/main/analysis/embeddingQueue/failedItemHandler.js @@ -145,10 +145,15 @@ function createFailedItemHandler(config) { const normalized = message.toLowerCase(); const isDimensionMismatch = normalized.includes('dimension_mismatch') || normalized.includes('dimension mismatch'); + // invalid_vector, invalid_vector_format, invalid_vector_values: a retry + // re-queues the same vector, skipping the enqueue checks, so it can only + // fail again or (before the store checked values) be stored as NaN. + const invalidVectorCode = normalized.match(/invalid_vector\w*/)?.[0] || null; return { message, requiresRebuild: isDimensionMismatch, - errorCode: isDimensionMismatch ? 'dimension_mismatch' : null + nonRetryable: isDimensionMismatch || invalidVectorCode !== null, + errorCode: isDimensionMismatch ? 'dimension_mismatch' : invalidVectorCode }; } @@ -164,9 +169,9 @@ function createFailedItemHandler(config) { // A dead-lettered item leaves the failed-items map too. Both writes must // be marked: writing only the dead-letter queue left the item in the // on-disk failed-items map, so after a crash it was retried again. - if (normalizedError.requiresRebuild) { + if (normalizedError.nonRetryable) { addToDeadLetterQueue(item, normalizedError.message, retryCount, { - requiresRebuild: true, + requiresRebuild: normalizedError.requiresRebuild, errorCode: normalizedError.errorCode }); if (failedItems.delete(item.id)) markFailedItemsChanged(); @@ -428,6 +433,31 @@ function createFailedItemHandler(config) { return count; } + /** + * Remove every failed-items and dead-letter entry whose item matches, so it + * is neither retried nor offered for a manual retry. Both writes join the + * owner's debounced persist. + * @param {(item: any) => boolean} predicate + * @returns {{ failed: number, deadLetter: number }} Entries removed + */ + function dropItems(predicate) { + let failed = 0; + for (const [id, data] of failedItems) { + if (predicate(data?.item)) { + failedItems.delete(id); + failed += 1; + } + } + const keptDeadLetter = deadLetterQueue.filter((entry) => !predicate(entry?.item)); + const deadLetter = deadLetterQueue.length - keptDeadLetter.length; + if (failed > 0) markFailedItemsChanged(); + if (deadLetter > 0) { + deadLetterQueue = keptDeadLetter; + markDeadLetterChanged(); + } + return { failed, deadLetter }; + } + /** * Set the dead letter queue (for initialization) * @param {any[]} items - Items to set @@ -471,6 +501,7 @@ function createFailedItemHandler(config) { clearDeadLetterQueue, retryDeadLetterItem, retryAllDeadLetterItems, + dropItems, setDeadLetterQueue, persistAll, persistPending, diff --git a/src/main/analysis/embeddingQueue/parallelProcessor.js b/src/main/analysis/embeddingQueue/parallelProcessor.js index 26c55777..842a19ab 100644 --- a/src/main/analysis/embeddingQueue/parallelProcessor.js +++ b/src/main/analysis/embeddingQueue/parallelProcessor.js @@ -134,7 +134,10 @@ async function processItemsInParallel({ `Batch ${type} upsert` ); if (isWritesBlockedResult(result)) throw writesBlockedError(type); - if (result && result.success === false) { + // Per-item failures come first: a batch where only some vectors had the + // wrong dimension also reports success:false, and the whole-batch + // branch below would fail (or re-upsert) the items that were written. + if (result && result.success === false && !(result.failed?.length > 0)) { const isDimensionMismatch = result.error === 'dimension_mismatch' || result.requiresRebuild === true; if (isDimensionMismatch) { diff --git a/src/main/analysis/embeddingQueue/queueManager.js b/src/main/analysis/embeddingQueue/queueManager.js index f9958001..a3b02dde 100644 --- a/src/main/analysis/embeddingQueue/queueManager.js +++ b/src/main/analysis/embeddingQueue/queueManager.js @@ -110,6 +110,18 @@ module.exports = { return total; }, + /** + * After an embedding-model switch, drop every pending vector (queued, + * retrying or dead-lettered) that is not from `newModel` in each staged queue. + * @param {string} newModel + * @returns {number} Items dropped + */ + dropOtherModelEmbeddings(newModel) { + let total = 0; + for (const q of queues) total += safeCallOptional(q, 'dropOtherModelEmbeddings', newModel) || 0; + return total; + }, + getStats() { return { analysis: analysisQueue?.getStats ? analysisQueue.getStats() : null, diff --git a/src/main/analysis/extractors/cadDesignExtractors.js b/src/main/analysis/extractors/cadDesignExtractors.js index 631b4f87..425c6d54 100644 --- a/src/main/analysis/extractors/cadDesignExtractors.js +++ b/src/main/analysis/extractors/cadDesignExtractors.js @@ -1216,8 +1216,17 @@ async function extractTextFromAi(filePath, options = {}) { const extractTextFromPdf = options ? options.extractTextFromPdf : undefined; const pdfCompatible = await isPdfCompatibleFile(filePath); if (pdfCompatible && typeof extractTextFromPdf === 'function') { - const text = await extractTextFromPdf(filePath); - return typeof text === 'string' ? text : ''; + try { + const text = await extractTextFromPdf(filePath); + if (typeof text === 'string' && text.trim()) return text; + } catch (/** @type {any} */ err) { + // Logos with outlined type have no or a thin text layer, and the PDF + // reader rejects them as scans. No OCR runs on this route, so keep the + // thin text it carries, else try the PostScript view below. + if (err?.code !== 'PDF_NO_TEXT_CONTENT') throw err; + const textLayer = err.metadata?.textLayer; + if (typeof textLayer === 'string' && textLayer.trim()) return textLayer; + } } const { lines } = await describeEpsFile(filePath); // An empty or structureless file gets '' like every other reader; the diff --git a/src/main/analysis/extractors/epubStructure.js b/src/main/analysis/extractors/epubStructure.js index ce774a1d..b96ad90a 100644 --- a/src/main/analysis/extractors/epubStructure.js +++ b/src/main/analysis/extractors/epubStructure.js @@ -291,8 +291,9 @@ function decodeEntities(text) { if (code >= 0xd800 && code <= 0xdfff) return ''; return String.fromCodePoint(code); } - const named = NAMED_ENTITIES[body.toLowerCase()]; - return named === undefined ? whole : named; + const lower = body.toLowerCase(); + // Own keys only, so &constructor; is not answered by Object.prototype. + return Object.hasOwn(NAMED_ENTITIES, lower) ? NAMED_ENTITIES[lower] : whole; } ); } diff --git a/src/main/analysis/extractors/htmlEntities.js b/src/main/analysis/extractors/htmlEntities.js index b2528887..75d3b796 100644 --- a/src/main/analysis/extractors/htmlEntities.js +++ b/src/main/analysis/extractors/htmlEntities.js @@ -195,11 +195,13 @@ function decodeHtmlEntities(text) { } } // Case matters for the letters (Aacute vs aacute); the XML five and - // nbsp are also accepted in any case, as browsers do for HTML. - const exact = NAMED_ENTITIES[body]; - if (exact !== undefined) return exact; - const folded = NAMED_ENTITIES[body.toLowerCase()]; - return folded !== undefined && /^(amp|lt|gt|quot|apos|nbsp)$/i.test(body) ? folded : match; + // nbsp are also accepted in any case, as browsers do for HTML. Own keys + // only, so &constructor; is not answered by Object.prototype. + if (Object.hasOwn(NAMED_ENTITIES, body)) return NAMED_ENTITIES[body]; + const lower = body.toLowerCase(); + return Object.hasOwn(NAMED_ENTITIES, lower) && /^(amp|lt|gt|quot|apos|nbsp)$/i.test(body) + ? NAMED_ENTITIES[lower] + : match; }); } diff --git a/src/main/analysis/extractors/rtfText.js b/src/main/analysis/extractors/rtfText.js index d34ad7fd..75f9ae3c 100644 --- a/src/main/analysis/extractors/rtfText.js +++ b/src/main/analysis/extractors/rtfText.js @@ -49,7 +49,8 @@ const SKIPPED_DESTINATIONS = new Set([ 'aftnsepc', 'bkmkstart', 'bkmkend', - 'field', + // Not 'field': its \fldrslt group is the text a reader sees (link text, + // page numbers); the \fldinst instructions are skipped on their own. 'fldinst', 'operator', 'panose', @@ -241,7 +242,7 @@ function rtfToText(rtf) { if (!/[a-zA-Z]/.test(next)) { i += 2; if (skipDepth === -1) { - const sym = SYMBOL_WORDS[next]; + const sym = Object.hasOwn(SYMBOL_WORDS, next) ? SYMBOL_WORDS[next] : undefined; if (sym !== undefined) { flushBytes(); out.push(sym); @@ -287,7 +288,8 @@ function rtfToText(rtf) { skipChars = uc; continue; } - const sym = SYMBOL_WORDS[word]; + // Own keys only: an unknown \constructor must not emit Object's source. + const sym = Object.hasOwn(SYMBOL_WORDS, word) ? SYMBOL_WORDS[word] : undefined; if (sym !== undefined) { flushBytes(); out.push(sym); diff --git a/src/main/analysis/fallbackUtils.js b/src/main/analysis/fallbackUtils.js index 34fbe8a4..21b835ff 100644 --- a/src/main/analysis/fallbackUtils.js +++ b/src/main/analysis/fallbackUtils.js @@ -14,6 +14,7 @@ const path = require('path'); const { isUncategorizedFolder } = require('../../shared/uncategorizedFolder'); +const { extractExtension } = require('../../shared/namingConventions'); // Use shared semantic extension mapping const { getSemanticExtensionScore, @@ -550,6 +551,18 @@ function getIntelligentKeywords(fileName, extension) { * @param {string} extension */ function safeSuggestedName(fileName, extension) { + // Callers pass path.extname, which is only '.gz' for logs.tar.gz. Widen it + // to the whole compound extension so the name keeps '.tar.gz' instead of + // folding '.tar' into the base as 'logs_tar.gz'. + const fullExtension = extractExtension(fileName); + if ( + extension && + fullExtension.length > extension.length && + fullExtension.toLowerCase().endsWith(extension.toLowerCase()) + ) { + extension = fullExtension; + } + // If the filename is already just the extension or empty, use a default topic if (!fileName || fileName === extension) { return `unnamed_file${extension}`; diff --git a/src/main/analysis/imageAnalysis.js b/src/main/analysis/imageAnalysis.js index 4f6a9c93..6ad5fa8f 100644 --- a/src/main/analysis/imageAnalysis.js +++ b/src/main/analysis/imageAnalysis.js @@ -546,7 +546,10 @@ Rules: } return globalDeduplicator.deduplicate(deduplicationKey, () => analysisFn(abortController), { type: 'image', - fileName: originalFileName + fileName: originalFileName, + // Lets a caller that joined this call re-run it when the starter's + // abort (not its own) ends the shared vision request. + signal: abortController.signal }); }, timeoutMs, diff --git a/src/main/analysis/modelAnalysisResult.js b/src/main/analysis/modelAnalysisResult.js index 63171410..82874a38 100644 --- a/src/main/analysis/modelAnalysisResult.js +++ b/src/main/analysis/modelAnalysisResult.js @@ -17,8 +17,17 @@ const path = require('path'); const { isPlausibleContentDate, toWrittenCalendarDay } = require('../../shared/contentDateSanity'); const { sanitizeSuggestedBaseName } = require('../../shared/pathSanitization'); +const { ALL_SUPPORTED_EXTENSIONS } = require('../../shared/constants'); const FolderMatchingService = require('../services/FolderMatchingService'); +// File types a model might append to its suggestion. Anything else after the +// last dot ("Dr._Smith_referral_letter", "Report_v2.1") is part of the name. +const KNOWN_FILE_EXTENSIONS = new Set( + (Array.isArray(ALL_SUPPORTED_EXTENSIONS) ? ALL_SUPPORTED_EXTENSIONS : []).map((ext) => + String(ext).toLowerCase() + ) +); + /** * @param {Record} parsedJson the parsed model output (mutated: date and confidence are normalized in place) * @param {{ @@ -187,9 +196,15 @@ function cleanSuggestedName(suggested, originalFileName, logger, source) { // The prompt says "no extension", but some models add one anyway — // if a wrong extension slips through (e.g. .docx for a .pdf file) // the renamed file becomes unopenable. + // Only a real file type counts as an added extension: path.extname would + // cut "St._Louis_trip_itinerary" down to "St". const originalExt = path.extname(originalFileName); const llmExt = path.extname(suggested); - const rawBase = llmExt ? suggested.slice(0, -llmExt.length) : suggested; + const lowerLlmExt = llmExt.toLowerCase(); + const isAddedExtension = + Boolean(llmExt) && + (lowerLlmExt === originalExt.toLowerCase() || KNOWN_FILE_EXTENSIONS.has(lowerLlmExt)); + const rawBase = isAddedExtension ? suggested.slice(0, -llmExt.length) : suggested; // Separators are rejected above; this covers the rest of the // Windows-illegal set (`<>:"|?*`, control chars), trailing dots // and reserved device names that would break the later rename. diff --git a/src/main/core/autoUpdater.js b/src/main/core/autoUpdater.js index 5dc9266a..af081262 100644 --- a/src/main/core/autoUpdater.js +++ b/src/main/core/autoUpdater.js @@ -40,6 +40,8 @@ let activeCleanup = null; let initPromise = null; let initialized = false; let hasDownloadedUpdate = false; +// Set when "Install now" is deferred to lifecycle's exit site (Windows NSIS). +let installOnExit = false; const UPDATER_DEGRADED_CODE = 'auto-updater.unavailable'; @@ -714,7 +716,26 @@ function quitAndInstall() { } try { - getAutoUpdater().quitAndInstall(); + const updater = getAutoUpdater(); + // electron-updater's quitAndInstall() spawns the NSIS installer before + // app.quit(), and in update mode the installer force-kills the running app + // about 2 s later, cutting off handleBeforeQuit (Orama final persist, + // hash-cache flush, lock release). Quit first instead; lifecycle starts + // the installer through runPendingInstallOnExit() once cleanup is done. + if (process.platform === 'win32' && typeof updater.install === 'function') { + if (!updater.installerPath) { + return { + success: false, + error: 'No downloaded update available yet. Check for updates and finish download first.' + }; + } + installOnExit = true; + logger.info('[UPDATER] Install requested; the installer starts after shutdown cleanup'); + // Deferred so the IPC reply reaches the renderer before shutdown starts. + setImmediate(() => app.quit()); + return { success: true }; + } + updater.quitAndInstall(); return { success: true }; } catch (error) { const message = error?.message || 'Failed to apply update'; @@ -729,12 +750,33 @@ function quitAndInstall() { } } +/** + * Start the installer that quitAndInstall() deferred. Called by lifecycle at + * its single exit site, after cleanup and right before app.exit(). install() + * is synchronous, spawns the installer detached and sets electron-updater's + * quitAndInstallCalled, so its own quit handler does not start a second, + * silent install. Logging is already closed when this runs. + * @returns {boolean} Whether an installer was started + */ +function runPendingInstallOnExit() { + if (!installOnExit) return false; + installOnExit = false; + try { + // Visible assisted install that relaunches the app, matching what + // electron-updater's quitAndInstall() would have run. + return getAutoUpdater().install(false, true) === true; + } catch { + return false; + } +} + module.exports = { initializeAutoUpdater, cleanupAutoUpdater, checkForUpdates, downloadUpdate, quitAndInstall, + runPendingInstallOnExit, hasPendingDownloadedUpdate: () => hasDownloadedUpdate, getUpdateTargetCapability, setAutoUpdateCheckEnabled, diff --git a/src/main/core/backgroundSetup.js b/src/main/core/backgroundSetup.js index b0ec328f..fa4edc69 100644 --- a/src/main/core/backgroundSetup.js +++ b/src/main/core/backgroundSetup.js @@ -331,6 +331,30 @@ async function downloadMissingModels() { stage: 'error', model: modelName }); + // Also send the terminal per-model event the download UIs wait on. + // Settings and the setup wizard defer to this run when their own + // request comes back `alreadyInProgress`, and they only stop waiting + // on model-download-complete / model-download-error, so without this + // a failed background download left them spinning forever. Skip it + // when this run lost a race to a UI-started download: that download + // is still running and reports its own outcome. + const errorMessage = downloadError?.message || 'Download failed'; + if (!errorMessage.includes('already in progress')) { + try { + const win = BrowserWindow.getAllWindows()[0]; + if (win && !win.isDestroyed()) { + safeSend(win.webContents, IPC_EVENTS.OPERATION_PROGRESS, { + type: 'model-download-error', + model: modelName, + error: errorMessage + }); + } + } catch (err) { + logger.debug('[BACKGROUND] Download error IPC skipped (window closing)', { + error: /** @type {any} */ (err)?.message || String(err) + }); + } + } } } // Setup "succeeded" only when the core (embedding + text) models are in diff --git a/src/main/core/ipcRegistry.js b/src/main/core/ipcRegistry.js index 939eb5f2..13e6734c 100644 --- a/src/main/core/ipcRegistry.js +++ b/src/main/core/ipcRegistry.js @@ -12,6 +12,7 @@ const { createLogger } = require('../../shared/logger'); const { withCorrelationId, generateCorrelationId } = require('../../shared/correlationId'); const { assertAllowedIpcSender } = require('../ipc/ipcSenderValidator'); const ipcAbortRegistry = require('./ipcAbortRegistry'); +const { IPC_CHANNELS } = require('../../shared/constants'); const TRACE_ID_PATTERN = /^[\w.:-]+$/; const MAX_TRACE_ID_LENGTH = 128; @@ -109,6 +110,14 @@ const registry = { let _isShuttingDown = false; const _inFlightOperations = new Set(); +/** + * Channels the shutdown gate lets through. lifecycle keeps `system:log` + * registered past its IPC cleanup so late renderer log lines still land; the + * gate rejecting them as APP_SHUTTING_DOWN made that exclusion pointless. + * Only side-effect-free channels that touch no service belong here. + */ +const SHUTDOWN_EXEMPT_CHANNELS = new Set([IPC_CHANNELS.SYSTEM.LOG]); + /** * Set shutdown state - called by lifecycle during app shutdown * @param {boolean} shutting - True if app is shutting down @@ -157,7 +166,7 @@ function registerHandler(ipcMain, channel, handler) { const wrappedHandler = async (/** @type {any} */ event, /** @type {any[]} */ ...rawArgs) => { const operationId = `${channel}-${++_opCounter}`; _inFlightOperations.add(operationId); - if (_isShuttingDown) { + if (_isShuttingDown && !SHUTDOWN_EXEMPT_CHANNELS.has(channel)) { _inFlightOperations.delete(operationId); logger.debug(`[REGISTRY] Rejecting IPC call during shutdown: ${channel}`); return { diff --git a/src/main/core/lifecycle.js b/src/main/core/lifecycle.js index 96740a46..f427118e 100644 --- a/src/main/core/lifecycle.js +++ b/src/main/core/lifecycle.js @@ -11,7 +11,7 @@ const electron = require('electron'); const { app, BrowserWindow, ipcMain } = electron; const { createLogger, shutdownLogging } = require('../../shared/logger'); -const { hasPendingDownloadedUpdate } = require('./autoUpdater'); +const { hasPendingDownloadedUpdate, runPendingInstallOnExit } = require('./autoUpdater'); const { attachUserDataLockPowerMonitor } = require('./userDataLock'); const { destroyTray, getTray, unregisterGlobalShortcuts } = require('./systemTray'); const { withoutJumpListTasks } = require('./jumpList'); @@ -144,6 +144,23 @@ function _shouldRelaunchAfterExit() { return Boolean(_relaunchRequest) && !_relaunchIssued && !hasPendingDownloadedUpdate(); } +/** + * Hide every visible window at the start of a shutdown. Windows already + * closed (autoUpdater.quitAndInstall on macOS closes them before before-quit) + * or destroyed are skipped; a failure never stops the shutdown. + */ +function _hideAllWindowsForShutdown() { + try { + for (const win of BrowserWindow.getAllWindows()) { + if (!win || win.isDestroyed?.()) continue; + if (typeof win.isVisible === 'function' && !win.isVisible()) continue; + win.hide(); + } + } catch (error) { + logger.debug('[SHUTDOWN] Could not hide windows:', error?.message || String(error)); + } +} + /** * Handle before-quit event - performs all cleanup operations * @returns {Promise} @@ -158,6 +175,12 @@ async function handleBeforeQuit(/** @type {any} */ event) { lifecycleConfig.setIsQuitting?.(true); + // From the IPC gate below onward every invoke answers APP_SHUTTING_DOWN, + // and cleanup can take many seconds: a window left on screen looks alive + // but no longer responds. Hide (not destroy) so the renderer can still + // send late log lines and app.exit() closes the windows at the end. + _hideAllWindowsForShutdown(); + // Stop a startup that is still running (quit within the first seconds) so // service integration does not initialise against services being shut down. try { @@ -328,14 +351,29 @@ async function handleBeforeQuit(/** @type {any} */ event) { // Batch analysis holds pending work that can race against vector/Llama shutdown if // left running past phase 3. await runPhase('ipcAndBatch', PHASE_BUDGETS.ipcAndBatch, async () => { + // The batch service's shutdown also shuts the embedding queues down; + // false means no batch service existed this session. + let embeddingQueuesShutDown = true; try { const { shutdownBatchAnalysisService } = require('../ipc/analysis'); - await shutdownBatchAnalysisService?.(); + embeddingQueuesShutDown = (await shutdownBatchAnalysisService?.()) !== false; logger.info('[CLEANUP] Batch analysis service shut down'); } catch (error) { logger.error('[CLEANUP] Failed to shut down batch analysis service:', error); } + // The embedding queues restore and flush on every launch, not only + // after a batch run, so their persist and SQLite close must not hinge + // on a batch service having been created. + if (!embeddingQueuesShutDown) { + try { + await require('../analysis/embeddingQueue/queueManager').shutdown(); + logger.info('[CLEANUP] Embedding queues shut down'); + } catch (error) { + logger.error('[CLEANUP] Failed to shut down embedding queues:', error); + } + } + try { // Keep `system:log` registered past this pass so the renderer can keep // sending log lines while the rest of the cleanup runs. Without this @@ -574,6 +612,14 @@ async function handleBeforeQuit(/** @type {any} */ event) { } } + // "Install now" starts the installer only here: it force-kills the running + // app shortly after launch, so starting it before cleanup lost unsaved work. + try { + runPendingInstallOnExit(); + } catch { + // Logging is closed; exiting is all that is left. + } + // Cleanup complete — now exit the app (bypasses before-quit to avoid re-entrance) app.exit(shutdownExitCode); } diff --git a/src/main/core/platformBehavior.js b/src/main/core/platformBehavior.js index 228c805a..45cf08f4 100644 --- a/src/main/core/platformBehavior.js +++ b/src/main/core/platformBehavior.js @@ -37,13 +37,15 @@ function bringWindowToForeground(win) { win.moveTop(); win.show(); win.setAlwaysOnTop(true); - const alwaysOnTopTimerId = setTimeout(() => { + // No 'closed' listener to cancel this: the callback already skips a + // destroyed window, and a once('closed') per call piles up on the + // long-lived main window (one per restore, never removed). + setTimeout(() => { if (win && !win.isDestroyed()) { win.setAlwaysOnTop(false); win.focus(); } }, WINDOW.ALWAYS_ON_TOP_DURATION_MS); - win.once('closed', () => clearTimeout(alwaysOnTopTimerId)); } win.focus(); diff --git a/src/main/core/systemTray.js b/src/main/core/systemTray.js index d576aa4f..9bb1061d 100644 --- a/src/main/core/systemTray.js +++ b/src/main/core/systemTray.js @@ -145,13 +145,14 @@ async function openSemanticSearch() { win.focus(); // Send message to renderer to open semantic search - // Small delay to ensure window is ready - const searchTimerId = setTimeout(() => { + // Small delay to ensure window is ready. The callback skips a destroyed + // window, so no once('closed') here: one per shortcut press would pile up + // on the long-lived main window. + setTimeout(() => { if (!win.isDestroyed()) { safeSend(win.webContents, IPC_EVENTS.OPEN_SEMANTIC_SEARCH); } }, 100); - win.once('closed', () => clearTimeout(searchTimerId)); } } diff --git a/src/main/core/windowState.js b/src/main/core/windowState.js index 787ae54f..15d2f771 100644 --- a/src/main/core/windowState.js +++ b/src/main/core/windowState.js @@ -138,7 +138,10 @@ function restoreMinimizedWindow(win) { // After restore, verify visibility and focus // Use a small delay to let Chromium settle - const settleTimerId = setTimeout(() => { + // No 'closed' listener to cancel this: the callback handles a destroyed + // window (and still resolves), and a once('closed') per restore piles up + // on the long-lived main window. + setTimeout(() => { if (!win || win.isDestroyed()) { resolve(); return; @@ -154,7 +157,6 @@ function restoreMinimizedWindow(win) { logger.debug('[WINDOW] Minimized window restored and focused'); resolve(); }, WINDOW.RESTORE_SETTLE_MS); - win.once('closed', () => clearTimeout(settleTimerId)); }; // Listen for the restore event diff --git a/src/main/errors/AnalysisError.js b/src/main/errors/AnalysisError.js index aebe5649..1e009c5b 100644 --- a/src/main/errors/AnalysisError.js +++ b/src/main/errors/AnalysisError.js @@ -65,15 +65,16 @@ class AnalysisError extends Error { PDF_PROCESSING_FAILURE: "This PDF file couldn't be processed. It may be corrupted or password-protected.", IMAGE_ANALYSIS_FAILURE: - "This image couldn't be analyzed. Please check the file format and try again.", - MODEL_NOT_INSTALLED: `Missing AI model: ${this.metadata.requiredModel}. Please install it to continue.`, - AI_ENGINE_CONNECTION_FAILURE: 'AI engine unavailable. Check Settings > Models and try again.', - DOCUMENT_ANALYSIS_FAILURE: 'Failed to analyze this document. Please check the file format.', + "This image couldn't be analyzed. Check the file format and try again.", + MODEL_NOT_INSTALLED: `Missing AI model: ${this.metadata.requiredModel}. Install it to continue.`, + AI_ENGINE_CONNECTION_FAILURE: + 'AI engine unavailable. Check Settings → Local AI engine and try again.', + DOCUMENT_ANALYSIS_FAILURE: 'Failed to analyze this document. Check the file format.', PDF_NO_TEXT_CONTENT: 'This PDF appears to be image-based. Try using image analysis instead.', - MODEL_VERIFICATION_FAILED: 'AI model verification failed. Please check your model downloads.', - DEPENDENCY_MISSING: `System component missing: ${this.metadata.dependency}. Please reinstall the application.`, + MODEL_VERIFICATION_FAILED: 'AI model verification failed. Check your model downloads.', + DEPENDENCY_MISSING: `System component missing: ${this.metadata.dependency}. Reinstall the application.`, FILE_TYPE_UNSUPPORTED: `File type "${this.metadata.fileType}" is not supported for AI analysis.`, - FILE_TOO_LARGE: 'File is too large for processing. Please use a smaller file.', + FILE_TOO_LARGE: 'File is too large to process. Use a smaller file.', FILE_EMPTY: 'This file is empty, so there was nothing to analyze.', FILE_READ_ERROR: "This file couldn't be read. It may be locked, moved, or you may not have permission to access it.", @@ -97,8 +98,8 @@ class AnalysisError extends Error { getActionableSteps() { /** @type {Record} */ const actions = { - MODEL_NOT_INSTALLED: ['Open Settings > Models and download the missing model'], - AI_ENGINE_CONNECTION_FAILURE: ['Check Settings > Models', 'Restart the app'], + MODEL_NOT_INSTALLED: ['Open Settings → Model management and download the missing model'], + AI_ENGINE_CONNECTION_FAILURE: ['Check Settings → Local AI engine', 'Restart the app'], DEPENDENCY_MISSING: [ `Check that ${this.metadata.dependency} is installed correctly`, 'Restart the application', @@ -133,7 +134,7 @@ class ModelMissingError extends AnalysisError { constructor(modelName) { super('MODEL_NOT_INSTALLED', { requiredModel: modelName, - installCommand: `Download model in Settings > Models: ${modelName}`, + installCommand: `Download the model in Settings → Model management: ${modelName}`, category: 'model' }); } diff --git a/src/main/errors/ErrorHandler.js b/src/main/errors/ErrorHandler.js index bbac4fb3..819e1134 100644 --- a/src/main/errors/ErrorHandler.js +++ b/src/main/errors/ErrorHandler.js @@ -225,7 +225,7 @@ class ErrorHandler { parseError(error) { const errorInfo = { type: ERROR_TYPES.UNKNOWN, - message: 'Something went wrong. Please try again.', + message: 'Something went wrong. Try again.', details: {}, stack: error?.stack }; @@ -286,11 +286,12 @@ class ErrorHandler { switch (resolvedCode) { case ERROR_CODES.LLAMA_MODEL_INCOMPATIBLE_CPU: errorInfo.message = - 'This build of the AI engine is not compatible with your CPU. Switch to the small Qwen 0.5B profile in Settings > Models.'; + 'This build of the AI engine is not compatible with your CPU. Switch to the small Qwen 0.5B profile in Settings → Default AI models.'; break; case ERROR_CODES.LLAMA_MODEL_NOT_FOUND: case ERROR_CODES.LLAMA_MODEL_PATH_MISMATCH: - errorInfo.message = 'AI model file is missing. Open Settings > Models to install it.'; + errorInfo.message = + 'AI model file is missing. Open Settings → Model management to install it.'; break; case ERROR_CODES.LLAMA_OOM: errorInfo.message = 'Out of memory. Try a smaller model or close other applications.'; @@ -301,14 +302,15 @@ class ErrorHandler { break; case ERROR_CODES.LLAMA_MODEL_LOAD_FAILED: errorInfo.message = - 'Could not load the AI model. Re-download it from Settings > Models.'; + 'Could not load the AI model. Re-download it from Settings → Model management.'; break; case ERROR_CODES.LLAMA_INFERENCE_FAILED: errorInfo.message = - 'AI inference failed. The prompt may be too long for this model — shorten the input or switch to a larger model.'; + 'AI inference failed. The prompt may be too long for this model. Shorten the input or switch to a larger model.'; break; default: - errorInfo.message = 'AI engine unavailable. Check Settings > Models and try again.'; + errorInfo.message = + 'AI engine unavailable. Check Settings → Local AI engine and try again.'; } } else if (isNetworkError(error)) { errorInfo.type = ERROR_TYPES.NETWORK_ERROR; @@ -316,7 +318,7 @@ class ErrorHandler { errorInfo.message = 'Request timed out. The AI model may be loading or your system is busy.'; } else { - errorInfo.message = 'A network-related error occurred. Please try again.'; + errorInfo.message = 'A network error occurred. Try again.'; } } else { // Keep original message if it's user-friendly, otherwise provide generic @@ -652,80 +654,106 @@ class ErrorHandler { * across every hard crash and occupy megabytes per incident, which is * especially painful on users who have set the app to launch at startup. * + * Crashpad keeps dumps in report subfolders, not the root: `reports/` on + * Windows and `new/`, `pending/`, `completed/` on macOS/Linux. The root + * `metadata` and `settings.dat` files are Crashpad's database and are + * never touched. + * * @param {string} crashDumpsDir - Absolute path to the crash-dumps directory. - * @param {number} [daysToKeep=30] - Delete files with mtime older than this. + * @param {number} [daysToKeep=30] - Delete dumps with mtime older than this. * @param {number} [maxFiles=20] - After age cleanup, keep at most this many - * (newest by mtime). Dumps are the only recovery signal for field - * issues, so the cap is intentionally higher than for verbose logs. + * dumps across all report folders (newest by mtime). Dumps are the + * only recovery signal for field issues, so the cap is intentionally + * higher than for verbose logs. */ async cleanupCrashDumps(crashDumpsDir, daysToKeep = 30, maxFiles = 20) { if (!crashDumpsDir) return; try { - let files; - try { - files = await fs.readdir(crashDumpsDir); - } catch (readErr) { - if (readErr?.code === 'ENOENT') return; - throw readErr; - } - - const cutoff = Date.now() - daysToKeep * 24 * 60 * 60 * 1000; - // Electron writes `.dmp` on Windows/Linux and `.ips`/`.dmp` on macOS; - // the CrashReporter also leaves `metadata` files alongside dumps. Keep - // the filter scoped to known crash-artifact extensions so we never - // clobber anything an admin placed in this directory. + // '' is the root, scanned only for stray dumps from older layouts. + const REPORT_FOLDERS = ['', 'reports', 'completed', 'pending', 'new']; + // Crashpad may still be writing a dump in new/ right after a crash. + const IN_PROGRESS_GRACE_MS = 5 * 60 * 1000; + const UUID_RE = /^[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}$/i; /** @param {string} name */ const isDumpFile = (name) => - name.endsWith('.dmp') || - name.endsWith('.ips') || - name.endsWith('.ips.synced') || - name === 'metadata' || - name === 'completed'; + name.endsWith('.dmp') || name.endsWith('.ips') || name.endsWith('.ips.synced'); - const candidates = files.filter(isDumpFile); + const now = Date.now(); + const cutoff = now - daysToKeep * 24 * 60 * 60 * 1000; - await Promise.all( - candidates.map(async (file) => { - const filePath = path.join(crashDumpsDir, file); + /** @type {Array<{ file: string, filePath: string, mtimeMs: number }>} */ + const dumps = []; + for (const folder of REPORT_FOLDERS) { + const dir = folder ? path.join(crashDumpsDir, folder) : crashDumpsDir; + let names; + try { + names = await fs.readdir(dir); + } catch (readErr) { + if (readErr?.code === 'ENOENT') continue; + throw readErr; + } + for (const name of names.filter(isDumpFile)) { + const filePath = path.join(dir, name); try { const stats = await fs.stat(filePath); - if (stats.isFile() && stats.mtimeMs < cutoff) { - await fs.unlink(filePath); - await this.log('info', `Cleaned up stale crash dump: ${file}`); - } - } catch (fileErr) { - logger.debug('Failed to cleanup crash dump entry:', { - file, - error: fileErr.message + if (!stats.isFile()) continue; + if (folder === 'new' && now - stats.mtimeMs < IN_PROGRESS_GRACE_MS) continue; + dumps.push({ + file: folder ? `${folder}/${name}` : name, + filePath, + mtimeMs: stats.mtimeMs }); + } catch { + // Ignore: entry may have been removed mid-sweep. } - }) - ); + } + } - // Secondary cap: even fresh dumps should be bounded. - const remaining = (await fs.readdir(crashDumpsDir)).filter(isDumpFile); - if (remaining.length <= maxFiles) return; + /** @param {{ filePath: string }} dump */ + const removeDump = async (dump) => { + await fs.unlink(dump.filePath); + if (!dump.filePath.endsWith('.dmp')) return; + const base = path.basename(dump.filePath, '.dmp'); + // Companion files: the .meta beside a macOS/Linux report and the + // attachments/ folder. Both are best-effort. + await fs.unlink(path.join(path.dirname(dump.filePath), `${base}.meta`)).catch(() => {}); + if (UUID_RE.test(base)) { + await fs + .rm(path.join(crashDumpsDir, 'attachments', base), { recursive: true, force: true }) + .catch(() => {}); + } + }; - const withStats = []; - for (const file of remaining) { - const filePath = path.join(crashDumpsDir, file); + /** @type {typeof dumps} */ + const kept = []; + for (const dump of dumps) { + if (dump.mtimeMs >= cutoff) { + kept.push(dump); + continue; + } try { - const stats = await fs.stat(filePath); - if (stats.isFile()) { - withStats.push({ file, filePath, mtimeMs: stats.mtimeMs }); - } - } catch { - // Ignore: entry may have been removed mid-sweep. + await removeDump(dump); + await this.log('info', `Cleaned up stale crash dump: ${dump.file}`); + } catch (fileErr) { + logger.debug('Failed to cleanup crash dump entry:', { + file: dump.file, + error: fileErr.message + }); } } - withStats.sort((a, b) => a.mtimeMs - b.mtimeMs); - let toRemove = withStats.length - maxFiles; - for (const item of withStats) { + + // Secondary cap across all folders: even fresh dumps should be bounded. + if (kept.length <= maxFiles) return; + kept.sort((a, b) => a.mtimeMs - b.mtimeMs); + let toRemove = kept.length - maxFiles; + for (const item of kept) { if (toRemove <= 0) break; try { - await fs.unlink(item.filePath); - await this.log('info', `Removed excess crash dump (retention cap): ${item.file}`); + await removeDump(item); + // Count the removal before logging so a logging failure cannot + // make the loop delete one dump too many. toRemove -= 1; + await this.log('info', `Removed excess crash dump (retention cap): ${item.file}`); } catch (fileErr) { logger.debug('Failed to remove excess crash dump:', { file: item.file, diff --git a/src/main/errors/FileSystemError.js b/src/main/errors/FileSystemError.js index 873c020d..fa26987a 100644 --- a/src/main/errors/FileSystemError.js +++ b/src/main/errors/FileSystemError.js @@ -213,7 +213,7 @@ class FileSystemError extends Error { [FILE_SYSTEM_ERROR_CODES.PERMISSION_DENIED]: 'You do not have permission to perform this action. Try running as administrator.', [FILE_SYSTEM_ERROR_CODES.WRITE_FAILED]: - 'Failed to save the file. Please check that the location is writable.', + 'Failed to save the file. Check that the location is writable.', [FILE_SYSTEM_ERROR_CODES.READ_FAILED]: 'Failed to read the file. It may be corrupted or inaccessible.', [FILE_SYSTEM_ERROR_CODES.DELETE_FAILED]: @@ -232,8 +232,7 @@ class FileSystemError extends Error { 'The folder is not empty. Remove its contents first.', [FILE_SYSTEM_ERROR_CODES.NOT_A_DIRECTORY]: 'Expected a folder but found a file.', [FILE_SYSTEM_ERROR_CODES.NOT_A_FILE]: 'Expected a file but found a folder.', - [FILE_SYSTEM_ERROR_CODES.DISK_FULL]: - 'Your disk is full. Please free up some space and try again.', + [FILE_SYSTEM_ERROR_CODES.DISK_FULL]: 'Your disk is full. Free up some space and try again.', [FILE_SYSTEM_ERROR_CODES.QUOTA_EXCEEDED]: 'Your storage quota has been exceeded.', [FILE_SYSTEM_ERROR_CODES.TOO_MANY_OPEN_FILES]: 'Too many files are open. Close some applications and try again.', @@ -252,8 +251,7 @@ class FileSystemError extends Error { 'File verification failed. The file may be corrupted.', [FILE_SYSTEM_ERROR_CODES.SIZE_MISMATCH]: 'File size verification failed. The copy may be incomplete.', - [FILE_SYSTEM_ERROR_CODES.PARTIAL_WRITE]: - 'Only part of the file was written. Please try again.', + [FILE_SYSTEM_ERROR_CODES.PARTIAL_WRITE]: 'Only part of the file was written. Try again.', [FILE_SYSTEM_ERROR_CODES.CORRUPTED_FILE]: 'The file appears to be corrupted and cannot be processed.', [FILE_SYSTEM_ERROR_CODES.WATCHER_FAILED]: 'Failed to monitor the folder for changes.', @@ -273,7 +271,7 @@ class FileSystemError extends Error { [FILE_SYSTEM_ERROR_CODES.UNKNOWN_ERROR]: 'An unexpected file system error occurred.' }; - return userMessages[this.code] || 'A file system error occurred. Please try again.'; + return userMessages[this.code] || 'A file system error occurred. Try again.'; } /** diff --git a/src/main/ipc/aiProvider.js b/src/main/ipc/aiProvider.js index 3915507f..905f39c5 100644 --- a/src/main/ipc/aiProvider.js +++ b/src/main/ipc/aiProvider.js @@ -66,7 +66,7 @@ const _SAFE_ERROR_MESSAGES = Object.freeze({ CONTENT_TOO_LONG: 'Request too large for the provider.', BUDGET_EXCEEDED: 'Monthly spend cap reached. Adjust the cap or wait until next month.', PROVIDER_DISABLED: 'This provider is not enabled.', - NO_PROVIDER_READY: 'No AI provider is ready. Configure one in Settings → AI.', + NO_PROVIDER_READY: 'No AI provider is ready. Configure one in Settings → AI Provider.', PROVIDER_FAILED: 'The provider returned an error. Try again or switch providers.', INVALID_RESPONSE: 'The provider returned an unexpected response.' }); @@ -368,7 +368,7 @@ function registerAiProviderIpc(servicesOrParams) { _router.on('folder-override-unavailable', (payload) => { const providerLabel = payload?.configuredProvider || 'cloud'; safeSendToMain(getMainWindow, IPC_EVENTS.NOTIFICATION, { - message: `Smart folder AI override (${providerLabel}) is unavailable — using local model.`, + message: `Smart folder AI override (${providerLabel}) is unavailable. Using the local model.`, severity: 'warning', duration: 8000, source: 'ai-provider', diff --git a/src/main/ipc/analysis.js b/src/main/ipc/analysis.js index 4a8ae57a..6ef70256 100644 --- a/src/main/ipc/analysis.js +++ b/src/main/ipc/analysis.js @@ -225,8 +225,14 @@ function getBatchAnalysisService() { return batchAnalysisService; } +/** + * Cancel active batch runs and shut the batch service (and with it the + * embedding queues) down. + * @returns {Promise} false when no batch service was ever created, + * so the embedding queues were not shut down here + */ async function shutdownBatchAnalysisService() { - if (!batchAnalysisService) return; + if (!batchAnalysisService) return false; const activeRuns = Array.from(activeBatchRuns.values()); for (const runState of activeRuns) { runState.cancelled = true; @@ -248,6 +254,7 @@ async function shutdownBatchAnalysisService() { const service = batchAnalysisService; batchAnalysisService = null; await service.shutdown(); + return true; } /** diff --git a/src/main/ipc/analysisHistory.js b/src/main/ipc/analysisHistory.js index 6241d1f8..bcb2478a 100644 --- a/src/main/ipc/analysisHistory.js +++ b/src/main/ipc/analysisHistory.js @@ -38,7 +38,7 @@ function _raiseAnalysisHistoryDegraded(detailContext) { severity: 'error', message: 'Analysis history is temporarily unavailable. Past results may not appear; ' + - 're-analyzing files will not help. Try restarting the app — if this persists, ' + + 're-analyzing files will not help. Try restarting the app. If this persists, ' + 'check the app log.', detail: detailContext }); @@ -237,7 +237,10 @@ function registerAnalysisHistoryIpc(servicesOrParams) { normalize: (payload) => { if (!payload || typeof payload !== 'object') return payload; return { - filePath: normalizeText(payload.filePath || '', { maxLength: 2000 }), + // The path is passed through exactly: the service matches entries by + // exact string, so collapsing a double space or cutting a long path + // would miss the entry (the Zod schema already requires a string). + filePath: typeof payload.filePath === 'string' ? payload.filePath : '', policy: normalizeText(payload.policy || '', { maxLength: 20 }) }; }, @@ -254,6 +257,16 @@ function registerAnalysisHistoryIpc(servicesOrParams) { } const result = await service.setEmbeddingPolicyByPath(filePath, policy); + // No entry matched, so nothing was stored: report it, or the renderer + // shows a policy that organize will not honor. + if (!result?.updated) { + return { + ...result, + success: false, + notFound: true, + error: 'No analysis history entry for this path' + }; + } return { success: true, ...result }; } catch (error) { logger.error('Failed to set embedding policy:', error); diff --git a/src/main/ipc/files/batchOrganizeHandler.js b/src/main/ipc/files/batchOrganizeHandler.js index c4d44282..d5da7e33 100644 --- a/src/main/ipc/files/batchOrganizeHandler.js +++ b/src/main/ipc/files/batchOrganizeHandler.js @@ -572,7 +572,7 @@ async function handleBatchOrganize(params) { log.warn('[FILE-OPS] Could not acquire batch lock - another batch operation is in progress'); return { success: false, - error: 'Another batch operation is already in progress. Please wait for it to complete.', + error: 'Another batch operation is already in progress. Wait for it to finish.', errorCode: ERROR_CODES.BATCH_LOCK_TIMEOUT }; } diff --git a/src/main/ipc/files/fileOperationHandlers.js b/src/main/ipc/files/fileOperationHandlers.js index c1feab69..7dfeccc3 100644 --- a/src/main/ipc/files/fileOperationHandlers.js +++ b/src/main/ipc/files/fileOperationHandlers.js @@ -1101,18 +1101,37 @@ function createPerformOperationHandler({ logger: log, getServiceIntegration, get }); continue; } - const single = await moveOneFile(op, { - log, - getServiceIntegration, - getMainWindow, - recordUndo: false, - destinationDecision: decisionForDestination( - batchMoveDecisions, - op?.destination, - process.platform, - op?.source - ) - }); + // moveOneFile rethrows most filesystem errors (EBUSY/EPERM on a + // locked file, a failed cross-device copy, EPATHCHANGED). Those + // all happen before or during the rename, so the file has not + // moved: record a failed row and keep going, so the files after + // it are still tried and the earlier moves get their undo entry. + let single; + try { + single = await moveOneFile(op, { + log, + getServiceIntegration, + getMainWindow, + recordUndo: false, + destinationDecision: decisionForDestination( + batchMoveDecisions, + op?.destination, + process.platform, + op?.source + ) + }); + } catch (moveErr) { + log.warn('[FILE-OPS] Batch move of one file failed', { + source: op?.source ? path.basename(op.source) : 'N/A', + error: moveErr?.message, + code: moveErr?.code + }); + single = { + success: false, + error: moveErr?.message || 'Move failed', + errorCode: moveErr?.code || 'MOVE_FAILED' + }; + } moveResults.push({ ...single, source: op?.source, destination: op?.destination }); if (single.success && !single.skipped && single.movedTo) { movedOps.push({ diff --git a/src/main/ipc/ipcWrappers.js b/src/main/ipc/ipcWrappers.js index 305ca334..0d9d715e 100644 --- a/src/main/ipc/ipcWrappers.js +++ b/src/main/ipc/ipcWrappers.js @@ -182,7 +182,7 @@ function withVectorDbInit({ ensureInit, isInitRef, handler }) { } catch (e) { return { success: false, - error: 'Vector DB is not available yet. Please try again in a moment.', + error: 'Vector DB is not available yet. Try again in a moment.', code: e?.code || 'VECTOR_DB_UNAVAILABLE', unavailable: true }; @@ -191,7 +191,7 @@ function withVectorDbInit({ ensureInit, isInitRef, handler }) { if (!isInitRef()) { return { success: false, - error: 'Vector DB initialization pending. Please try again in a few seconds.', + error: 'Vector DB is still starting. Try again in a few seconds.', code: 'VECTOR_DB_PENDING', pending: true }; diff --git a/src/main/ipc/semantic.js b/src/main/ipc/semantic.js index 6e876275..eb4f6b49 100644 --- a/src/main/ipc/semantic.js +++ b/src/main/ipc/semantic.js @@ -15,10 +15,17 @@ const getLlamaService = () => container.tryResolve(ServiceIds.LLAMA_SERVICE); const getUndoRedoService = () => container.tryResolve(ServiceIds.UNDO_REDO); const { getInstance: getAtlasService } = require('../services/AtlasProjectionService'); +/** @type {Record} */ +const SERVICE_LABELS = { + OramaVectorService: 'The search index', + ClusteringService: 'Clustering', + AtlasProjectionService: 'The Atlas view' +}; + /** @param {string} name */ const SERVICE_UNAVAILABLE = (name) => ({ success: false, - error: `${name} not available — service may still be initializing`, + error: `${SERVICE_LABELS[name] || 'This feature'} isn't ready yet. It may still be starting.`, errorCode: 'SERVICE_UNAVAILABLE' }); @@ -185,7 +192,7 @@ async function verifyEmbeddingModelAvailable(logger, preferredModel = null) { return { available: false, model: preferredModel || AI_DEFAULTS.EMBEDDING.MODEL, - error: 'LlamaService not available — AI engine may still be initializing' + error: "The AI engine isn't ready yet. It may still be starting." }; } const cfg = await llama.getConfig(); @@ -259,7 +266,7 @@ async function verifyEmbeddingModelAvailable(logger, preferredModel = null) { return { available: false, model, - error: `Embedding model "${model}" not downloaded. Download it in Settings > Models.`, + error: `Embedding model "${model}" not downloaded. Download it in Settings → Model management.`, availableModels: modelNames.slice(0, 10) }; } @@ -293,7 +300,7 @@ async function verifyReanalyzeModelsAvailable(logger) { textModel: AI_DEFAULTS.TEXT.MODEL, visionModel: AI_DEFAULTS.IMAGE.MODEL, embeddingModel: AI_DEFAULTS.EMBEDDING.MODEL, - error: 'LlamaService not available — AI engine may still be initializing' + error: "The AI engine isn't ready yet. It may still be starting." }; } const cfg = await llama.getConfig(); @@ -363,7 +370,7 @@ async function verifyReanalyzeModelsAvailable(logger) { available: false, model: textModel, modelType: 'text', - error: `Text model "${textModel}" not downloaded. Download it in Settings > Models.` + error: `Text model "${textModel}" not downloaded. Download it in Settings → Model management.` }; } } @@ -395,7 +402,7 @@ async function verifyReanalyzeModelsAvailable(logger) { available: false, model: visionModel, modelType: 'vision', - error: `Vision model "${visionModel}" not downloaded. Download it in Settings > Models.` + error: `Vision model "${visionModel}" not downloaded. Download it in Settings → Model management.` }; } } @@ -429,7 +436,7 @@ async function verifyReanalyzeModelsAvailable(logger) { available: false, model: embeddingModel, modelType: 'embedding', - error: `Embedding model "${embeddingModel}" not downloaded. Download it in Settings > Models.` + error: `Embedding model "${embeddingModel}" not downloaded. Download it in Settings → Model management.` }; } } @@ -786,6 +793,20 @@ async function handleTrashDuplicates({ logger }, event, { paths, summary, verify // Best effort; the staleness timer catches up otherwise. } } + + // Same contract as every other delete path: without this event the + // renderer kept the trashed files in Discover/Organize state. + try { + const trashedPaths = result.trashed.map((item) => item?.path).filter(Boolean); + if (trashedPaths.length > 0) { + safeSend(event?.sender, IPC_EVENTS.FILE_OPERATION_COMPLETE, { + operation: 'delete', + files: trashedPaths + }); + } + } catch { + // Best-effort notification; the files are already in the trash. + } } return { @@ -2409,16 +2430,53 @@ async function handleReanalyzeAll( } } +/** + * Whether the app has started shutting down (the IPC gate is closed). Read + * lazily: ipcRegistry is a core module and this file must stay loadable in + * tests that never touch it. + * @returns {boolean} + */ +function isAppShuttingDown() { + try { + return require('../core/ipcRegistry').isShuttingDown() === true; + } catch { + return false; + } +} + +/** + * Thrown inside handleFullRebuild when the invoke was aborted or the app began + * quitting mid-run; the catch below turns it into an interrupted result that + * leaves the rebuild-in-progress marker set. + */ +class FullRebuildInterruptedError extends Error { + constructor() { + super('Full rebuild was interrupted before it finished'); + this.name = 'FullRebuildInterruptedError'; + } +} + /** * @param {SemanticHandlerDeps} deps - * @param {IpcMainInvokeEvent} _event + * @param {IpcMainInvokeEvent} event * @param {Record} [options] */ async function handleFullRebuild( { safeEnsureInit, logger, getCustomFolders, getServiceIntegration, shouldBuildChunkEmbeddings }, - _event, + event, options = {} ) { + // Quitting mid-rebuild aborts this invoke (ipcAbortRegistry.abortAll) and + // stops the embedding and vector services underneath it. Without these + // checks every remaining embed failed fast, the upserts hit a null Orama + // service, and the marker below was still cleared: the next launch trusted + // an empty index. + const abortSignal = event?.abortSignal; + const throwIfInterrupted = () => { + if (abortSignal?.aborted || isAppShuttingDown()) { + throw new FullRebuildInterruptedError(); + } + }; const initErr = await safeEnsureInit(); if (initErr) return initErr; const lockResult = acquireRebuildLock('FULL_REBUILD'); @@ -2444,6 +2502,10 @@ async function handleFullRebuild( let previousEmbeddingModel = null; let overrideApplied = false; let rebuildSucceeded = false; + // False once any rebuilt row failed to reach the store (an upsert threw, + // was skipped for shutdown, or the vector service was gone): the store is + // then partial and the rebuild-in-progress marker must stay set. + let upsertsComplete = true; try { const embeddingService = getParallelEmbeddingService(); @@ -2506,7 +2568,7 @@ async function handleFullRebuild( if (!llamaSvc) { return { success: false, - error: 'AI engine is not available. Please wait for startup to complete and try again.', + error: 'AI engine is not available yet. Wait for startup to finish and try again.', errorCode: 'SERVICE_NOT_READY' }; } @@ -2564,6 +2626,8 @@ async function handleFullRebuild( } // Step 2: Clear all vector DB collections + // Last point where quitting leaves the old index intact on disk. + throwIfInterrupted(); logger.info('[EMBEDDINGS] Clearing all vector DB collections...'); // resetAll() does not reject on a failed save: the clear then lives in // memory only and the old index is still on disk (G2). @@ -2613,7 +2677,9 @@ async function handleFullRebuild( const validFolderPayloads = folderPayloads.filter((p) => p !== null); if (validFolderPayloads.length > 0) { - await getOramaService().batchUpsertFolders(validFolderPayloads); + throwIfInterrupted(); + const folderResult = await getOramaService().batchUpsertFolders(validFolderPayloads); + if (folderResult?.reason === 'shutdown') upsertsComplete = false; } } @@ -2633,6 +2699,9 @@ async function handleFullRebuild( const processedFileIds = new Set(); for (const entry of allEntries) { + // Outside the per-file try: an interruption must end the run, not + // count as one more failed file. + throwIfInterrupted(); // Keep the rebuild lock's inactivity heartbeat alive — each entry // can take seconds of CPU embedding, and a silent lock steal would // let a second rebuild interleave with this one. @@ -2756,6 +2825,7 @@ async function handleFullRebuild( }); for (const chunk of chunks) { + throwIfInterrupted(); touchRebuildLock(lockResult.token); try { const { vector: chunkVector, model: chunkModel } = @@ -2791,6 +2861,7 @@ async function handleFullRebuild( } } } catch (e) { + if (e instanceof FullRebuildInterruptedError) throw e; results.files.failed++; // Generic rebuild-loop exception — includes embedding-generation // failures, vector-DB upsert errors, etc. Always warn-level since @@ -2811,23 +2882,43 @@ async function handleFullRebuild( // Batch upsert files const BATCH_SIZE = BATCH.SEMANTIC_BATCH_SIZE; + // A batch that throws, is skipped for shutdown, or finds no vector + // service leaves the store partial: mark it so the marker stays set. for (let i = 0; i < filePayloads.length; i += BATCH_SIZE) { + throwIfInterrupted(); touchRebuildLock(lockResult.token); const batch = filePayloads.slice(i, i + BATCH_SIZE); try { - await getOramaService().batchUpsertFiles(batch); + const orama = getOramaService(); + if (!orama) throw new Error('Vector DB service is not available'); + const upsertResult = await orama.batchUpsertFiles(batch); + if (upsertResult?.skipped === true) { + upsertsComplete = false; + logger.warn('[EMBEDDINGS] File batch upsert skipped:', upsertResult.reason); + } } catch (e) { + upsertsComplete = false; logger.warn('[EMBEDDINGS] Failed to batch upsert files:', e.message); } } // Batch upsert chunks for (let i = 0; i < chunkPayloads.length; i += BATCH_SIZE) { + throwIfInterrupted(); touchRebuildLock(lockResult.token); const batch = chunkPayloads.slice(i, i + BATCH_SIZE); try { - await getOramaService().batchUpsertFileChunks(batch); + const orama = getOramaService(); + if (!orama) throw new Error('Vector DB service is not available'); + // Returns the inserted count; 0 for a non-empty batch is the + // shutdown/blocked-writes skip. + const inserted = await orama.batchUpsertFileChunks(batch); + if (inserted === 0) { + upsertsComplete = false; + logger.warn('[EMBEDDINGS] Chunk batch upsert wrote nothing'); + } } catch (e) { + upsertsComplete = false; logger.warn('[EMBEDDINGS] Failed to batch upsert chunks:', e.message); } } @@ -2835,6 +2926,7 @@ async function handleFullRebuild( } // Step 5: Rebuild BM25 index + throwIfInterrupted(); logger.info('[EMBEDDINGS] Rebuilding BM25 search index...'); try { const searchService = await getSearchService(); @@ -2884,6 +2976,17 @@ async function handleFullRebuild( : '[EMBEDDINGS] Rebuilt index is in memory only; saving it to disk failed' ); } + // A partial store is not a finished rebuild either: the final save would + // write it to disk as if complete, so keep the marker and raise the flag. + if (!upsertsComplete) { + persisted = false; + results.errors.push({ + type: 'persist', + error: 'Some rebuilt embeddings were not written to the index' + }); + } + // Quitting during the final save: the store may be saved partial. + throwIfInterrupted(); logger.info('[EMBEDDINGS] Full rebuild complete', { folders: results.folders, @@ -2962,6 +3065,22 @@ async function handleFullRebuild( message: `Full rebuild complete: ${results.folders.success} folders, ${results.files.success} files, ${results.chunks.success} chunks${persisted ? '' : ' (not saved to disk)'}` }; } catch (e) { + if (e instanceof FullRebuildInterruptedError) { + // The rebuild-in-progress marker stays set, so the next launch reports + // the torn index (embeddingRebuildInterrupted) instead of trusting it. + logger.warn('[EMBEDDINGS] Full rebuild interrupted; index left marked as rebuilding', { + reason: abortSignal?.aborted ? String(abortSignal.reason ?? 'aborted') : 'shutdown', + folders: results.folders, + files: results.files, + chunks: results.chunks + }); + return { + success: false, + error: e.message, + errorCode: 'FULL_REBUILD_INTERRUPTED', + partialResults: results + }; + } logger.error('[EMBEDDINGS] Full rebuild failed:', e); return { success: false, @@ -4124,7 +4243,7 @@ function registerEmbeddingsIpc(servicesOrParams) { } catch (e) { return { success: false, - error: 'Vector DB is not available yet. Please try again in a moment.', + error: 'Vector DB is not available yet. Try again in a moment.', code: e?.code || 'VECTOR_DB_UNAVAILABLE', unavailable: true }; diff --git a/src/main/ipc/settings.js b/src/main/ipc/settings.js index 2cbfc8b1..7571b0bd 100644 --- a/src/main/ipc/settings.js +++ b/src/main/ipc/settings.js @@ -887,7 +887,7 @@ function registerSettingsIpc(servicesOrParams) { const { bytesRead } = await fileHandle.read(buffer, 0, stats.size, 0); const statsAfter = await fileHandle.stat(); if (statsAfter.size > MAX_IMPORT_SIZE || bytesRead > MAX_IMPORT_SIZE) { - throw new Error('Import file size changed during read. Please try again.'); + throw new Error('Import file size changed during read. Try again.'); } // Read and parse import file diff --git a/src/main/ipc/smartFolders.js b/src/main/ipc/smartFolders.js index aaa9f27b..c2f13e31 100644 --- a/src/main/ipc/smartFolders.js +++ b/src/main/ipc/smartFolders.js @@ -1264,6 +1264,39 @@ Now generate a description for "${folderName}":`; } } +/** + * After a Smart Folder's directory was renamed on disk, point every other + * Smart Folder that lived inside it at its new location. Containment is by + * whole path segments, case-insensitive on Windows and macOS (the same rules + * the knowledge remap uses). Mutates `folders` in place. + * + * @param {{ folders: Record[], skipIndex: number, oldPath: string, + * newPath: string, logger: LoggerLike }} args + * @returns {number} how many folders were re-pointed + */ +function rebaseNestedSmartFolders({ folders, skipIndex, oldPath, newPath, logger }) { + let rebased = 0; + folders.forEach((folder, index) => { + if (index === skipIndex || typeof folder?.path !== 'string') return; + const [change] = buildFolderRenamePathChanges( + [{ originalPath: folder.path }], + oldPath, + newPath + ); + if (!change) return; + folders[index] = { ...folder, path: change.newPath, updatedAt: new Date().toISOString() }; + rebased++; + }); + if (rebased > 0) { + logger.info('[SMART-FOLDERS] Re-pointed nested Smart Folders after rename', { + oldPath, + newPath, + count: rebased + }); + } + return rebased; +} + /** * Tell the running watcher about a changed folder list right away and scan * any newly watched paths for files that already exist in them. Until this, @@ -1552,7 +1585,7 @@ async function handleSmartFoldersEdit( if (illegalChars.test(updatedFolder.name)) { return { success: false, - error: 'Folder name contains invalid characters. Please avoid: < > : " | ? *', + error: 'Folder name contains invalid characters: < > : " | ? *', errorCode: ERROR_CODES.INVALID_FOLDER_NAME_CHARS }; } @@ -1619,6 +1652,7 @@ async function handleSmartFoldersEdit( } const originalFolder = { ...customFolders[folderIndex] }; const originalFolders = customFolders.map((folder) => ({ ...folder })); + let renamedOnDisk = false; if (updatedFolder.path && !areEquivalentPaths(updatedFolder.path, originalFolder.path)) { try { const oldPath = originalFolder.path; @@ -1693,13 +1727,14 @@ async function handleSmartFoldersEdit( if (renameFailure?.code === 'EEXIST') { return { success: false, - error: 'Destination directory now occupied — refusing overwrite', + error: 'Destination already exists. Nothing was overwritten.', errorCode: ERROR_CODES.RENAME_FAILED, details: renameFailure.message }; } throw renameFailure; } + renamedOnDisk = true; logger.info(`[SMART-FOLDERS] Renamed directory "${oldPath}" -> "${newPath}"`); await remapKnowledgeUnderRenamedFolder({ oldPath, @@ -1746,6 +1781,18 @@ async function handleSmartFoldersEdit( ...updatedFolder, updatedAt: new Date().toISOString() }; + // The rename moved every Smart Folder nested under the old directory + // too. Left on the old path, an organize into one of them would + // recreate the old tree (mkdir recursive) beside the renamed one. + if (renamedOnDisk) { + rebaseNestedSmartFolders({ + folders: customFolders, + skipIndex: folderIndex, + oldPath: originalFolder.path, + newPath: customFolders[folderIndex].path, + logger + }); + } setCustomFolders(customFolders); await saveCustomFolders(customFolders); await syncSmartFolderEmbeddings({ @@ -1781,6 +1828,13 @@ async function handleSmartFoldersEdit( }; } catch (saveError) { customFolders[folderIndex] = originalFolder; + if (renamedOnDisk) { + originalFolders.forEach((folder, index) => { + if (index !== folderIndex && customFolders[index]?.id === folder.id) { + customFolders[index] = folder; + } + }); + } throw saveError; } } catch (error) { diff --git a/src/main/ipc/undoRedo.js b/src/main/ipc/undoRedo.js index 76b2fc4e..3a06f38f 100644 --- a/src/main/ipc/undoRedo.js +++ b/src/main/ipc/undoRedo.js @@ -53,7 +53,7 @@ function _raiseUndoRedoDegraded(detail) { subsystem: 'undo-redo', severity: 'error', message: - "Undo/redo isn't working — recent file moves can't be reversed from the toolbar. " + + "Undo/redo isn't working. Recent file moves can't be reversed from the toolbar. " + 'Avoid further destructive actions and restart the app to recover history.', detail }); diff --git a/src/main/ipc/validationSchemas.js b/src/main/ipc/validationSchemas.js index 42909207..de2af5bc 100644 --- a/src/main/ipc/validationSchemas.js +++ b/src/main/ipc/validationSchemas.js @@ -306,7 +306,7 @@ if (!z) { if (data === undefined || data === null) return { data }; const n = typeof data === 'number' ? data : Number(data); if (!Number.isFinite(n) || n < 1 || n > 500) { - return { error: 'history limit must be a number 1–500' }; + return { error: 'History limit must be a number from 1 to 500' }; } return { data: n }; }), @@ -314,7 +314,7 @@ if (!z) { if (data === undefined || data === null) return { data: 50 }; const n = typeof data === 'number' ? data : Number(data); if (!Number.isFinite(n) || n < 1 || n > 200) { - return { error: 'undo history limit must be a number 1–200' }; + return { error: 'Undo history limit must be a number from 1 to 200' }; } return { data: Math.floor(n) }; }), @@ -1188,11 +1188,15 @@ if (!z) { .min(1) .max(2048) .refine(isSafeAbsoluteFsPath, SAFE_ABSOLUTE_PATH_MESSAGE), + // A Smart Folder may live on a network share, and the renderer + // sends this for every action, so one share-hosted suggestion must + // not fail a whole Trash or Ignore request. Main checks it again + // (validateFileOperationPath and the session's own suggestion). suggestedFolderPath: z .string() .min(1) .max(2048) - .refine(isSafeAbsoluteFsPath, SAFE_ABSOLUTE_PATH_MESSAGE) + .refine(isSafeFileOpPath, SAFE_FILE_OP_PATH_MESSAGE) .optional(), size: z.number().min(0).optional() }) @@ -1272,11 +1276,14 @@ if (!z) { /** * Trash duplicates: user-initiated bulk delete via shell.trashItem, recorded - * as a single TRASH_BATCH undo entry. + * as a single TRASH_BATCH undo entry. A network-share path is accepted so a + * set that spans a share still trashes its local copies; TrashService leaves + * the share copy in place and reports it, since Windows has no Recycle Bin + * there. */ const trashDuplicatesSchema = z.object({ paths: z - .array(z.string().min(1).max(2048).refine(isSafeAbsoluteFsPath, SAFE_ABSOLUTE_PATH_MESSAGE)) + .array(z.string().min(1).max(2048).refine(isSafeFileOpPath, SAFE_FILE_OP_PATH_MESSAGE)) .min(1) .max(1000), summary: z @@ -1290,15 +1297,9 @@ if (!z) { verifyIdentical: z .array( z.object({ - keep: z - .string() - .min(1) - .max(2048) - .refine(isSafeAbsoluteFsPath, SAFE_ABSOLUTE_PATH_MESSAGE), + keep: z.string().min(1).max(2048).refine(isSafeFileOpPath, SAFE_FILE_OP_PATH_MESSAGE), paths: z - .array( - z.string().min(1).max(2048).refine(isSafeAbsoluteFsPath, SAFE_ABSOLUTE_PATH_MESSAGE) - ) + .array(z.string().min(1).max(2048).refine(isSafeFileOpPath, SAFE_FILE_OP_PATH_MESSAGE)) .max(1000) }) ) diff --git a/src/main/llamaUtils.js b/src/main/llamaUtils.js index fc3635a2..2687ac0b 100644 --- a/src/main/llamaUtils.js +++ b/src/main/llamaUtils.js @@ -31,8 +31,8 @@ function _raiseLlamaConfigStale(/** @type {any} */ detail) { subsystem: 'ai-engine', severity: 'warning', message: - "Couldn't read AI model settings; using last known values. Open Settings → AI to verify " + - 'the active models — changes you made may not be applied until the settings file is readable.', + "Couldn't read AI model settings; using last known values. Open Settings → Default AI models to verify " + + 'the active models. Changes you made may not apply until the settings file can be read.', detail }); } catch { @@ -285,7 +285,7 @@ function raiseTextModelSubstitutedFlag(configuredModel, fallbackModel) { code: TEXT_MODEL_SUBSTITUTED_FLAG, subsystem: 'llama', severity: 'warning', - message: `Configured text model "${configuredModel}" is not installed. Analysis is using "${fallbackModel}" instead; pick a model in Settings → Models to clear this notice.`, + message: `Configured text model "${configuredModel}" is not installed. Analysis is using "${fallbackModel}" instead; pick a model in Settings → Default AI models to clear this notice.`, detail: { errorCode: ERROR_CODES.LLAMA_MODEL_AUTO_FALLBACK, type: 'text', @@ -317,7 +317,7 @@ async function ensureTextModelReadyForAnalysis() { if (!service) { return { ok: false, - error: 'AI engine unavailable. Check Settings > Models and try again.' + error: 'AI engine unavailable. Check Settings → Local AI engine and try again.' }; } @@ -327,7 +327,7 @@ async function ensureTextModelReadyForAnalysis() { } catch (err) { return { ok: false, - error: `AI engine failed to initialize: ${err?.message || String(err)}. Check Settings > Models.` + error: `AI engine failed to initialize: ${err?.message || String(err)}. Check Settings → Local AI engine.` }; } } @@ -352,7 +352,7 @@ async function ensureTextModelReadyForAnalysis() { } catch (err) { return { ok: false, - error: `Could not verify AI models: ${err?.message || String(err)}. Check Settings > Models.` + error: `Could not verify AI models: ${err?.message || String(err)}. Check Settings → Model management.` }; } @@ -407,7 +407,7 @@ async function ensureTextModelReadyForAnalysis() { } else { return { ok: false, - error: `Text model "${textModel}" not downloaded. Download it in Settings > Models.` + error: `Text model "${textModel}" not downloaded. Download it in Settings → Model management.` }; } } diff --git a/src/main/services/BatchAnalysisService.js b/src/main/services/BatchAnalysisService.js index 0c03c955..58a05ec3 100644 --- a/src/main/services/BatchAnalysisService.js +++ b/src/main/services/BatchAnalysisService.js @@ -116,7 +116,6 @@ class BatchAnalysisService { this.backpressureLock = new Semaphore(1); this._embeddingProgressUnsubscribe = null; - this._backpressureWaitPromise = null; this._adaptiveRecommendation = null; this._adaptiveRecommendationAt = 0; this._adaptiveRecommendationTtlMs = 30000; @@ -606,6 +605,12 @@ class BatchAnalysisService { // Time file starts spent waiting for the embedding queue to drain. let backpressureWaitMs = 0; + // One drain wait shared by this run's concurrent file starts. It used to + // live on the service singleton, so a second run awaited a loop that + // checked the first run's cancel flag: cancelling one run made the other + // run's waiting files return null as if they too were cancelled. + /** @type {Promise | null} */ + let backpressureWaitPromise = null; const checkBackpressure = async () => { if (typeof isCancelled === 'function' && isCancelled()) { return false; @@ -629,8 +634,8 @@ class BatchAnalysisService { return true; } - if (!this._backpressureWaitPromise) { - this._backpressureWaitPromise = (async () => { + if (!backpressureWaitPromise) { + backpressureWaitPromise = (async () => { const backpressureStart = Date.now(); let backpressureDelay = BACKPRESSURE_INITIAL_DELAY_MS; let iterations = 0; @@ -711,12 +716,12 @@ class BatchAnalysisService { return true; // proceed anyway on error }) .finally(() => { - this._backpressureWaitPromise = null; + backpressureWaitPromise = null; }); } const waitStartedAt = Date.now(); - const canProceed = await this._backpressureWaitPromise; + const canProceed = await backpressureWaitPromise; backpressureWaitMs += Date.now() - waitStartedAt; return canProceed !== false; }; diff --git a/src/main/services/ChatHistoryStore.js b/src/main/services/ChatHistoryStore.js index 7f756104..0118ecfa 100644 --- a/src/main/services/ChatHistoryStore.js +++ b/src/main/services/ChatHistoryStore.js @@ -672,8 +672,8 @@ class ChatHistoryStore { * * 2. Decrypt-and-scan stage — for the most-recently-updated * conversations NOT already matched by stage 1, fetch their - * messages, decrypt the JSON payload columns in memory, - * stringify, and substring-match against the query. Bounded + * messages, decrypt the answer and follow-up columns in memory, + * and substring-match their text against the query. Bounded * at SEARCH_DECRYPT_SCAN_CAP candidate conversations so cost * stays sublinear in total history size for typical users. * @@ -709,8 +709,9 @@ class ChatHistoryStore { return { items: [], capped: false, capLimit: SEARCH_DECRYPT_SCAN_CAP, scannedCount: 0 }; } - // Escape LIKE special characters to prevent unintended wildcard matching - const escapedQuery = query.replace(/[%_]/g, '\\$&'); + // Escape LIKE special characters, and the ESCAPE character itself, so a + // query like "C:\Users" matches the backslash instead of escaping "U". + const escapedQuery = query.replace(/[\\%_]/g, '\\$&'); const pattern = `%${escapedQuery}%`; // Stage 1: fast SQL search on plain columns only. @@ -765,7 +766,7 @@ class ChatHistoryStore { const needle = query.toLowerCase(); const messageStmt = this._db.prepare( - `SELECT document_answer, model_answer, meta FROM messages WHERE conversation_id = ?` + `SELECT document_answer, model_answer, follow_ups FROM messages WHERE conversation_id = ?` ); let added = 0; @@ -796,24 +797,26 @@ class ChatHistoryStore { } /** - * Decrypt one message row's JSON columns and check whether the - * lowercase needle appears in any of them. Stops as soon as it - * finds a hit; the JSON.stringify pass over each payload only runs - * if the previous payloads didn't match. Encrypted-but-empty - * decrypts (cross-machine handoff, corrupt ciphertext) return '' - * which contains no needles, so the search just skips them. + * Decrypt one message row's answer columns and check whether the + * lowercase needle appears in the text a user can read: each answer + * item's `text` and each follow-up. The serialized JSON is never + * matched, so a key such as "text" or "mode" does not hit every + * conversation. Stops at the first hit. Encrypted-but-empty decrypts + * (cross-machine handoff, corrupt ciphertext) parse to [] and are + * skipped. * @private */ _messageMatchesNeedle(/** @type {any} */ row, /** @type {any} */ needle) { if (!row || !needle) return false; - const sources = [row.document_answer, row.model_answer, row.meta]; - for (const raw of sources) { - if (raw == null || raw === '') continue; - const plain = decryptFromStorage(raw); - if (!plain) continue; - if (plain.toLowerCase().includes(needle)) return true; + /** @param {any} value */ + const matches = (value) => typeof value === 'string' && value.toLowerCase().includes(needle); + for (const raw of [row.document_answer, row.model_answer]) { + const items = _decryptJsonPayload(raw, []); + if (!Array.isArray(items)) continue; + if (items.some((item) => matches(item?.text))) return true; } - return false; + const followUps = _decryptJsonPayload(row.follow_ups, []); + return Array.isArray(followUps) && followUps.some(matches); } /** diff --git a/src/main/services/ChatService.js b/src/main/services/ChatService.js index 05d2baa8..7f0bc580 100644 --- a/src/main/services/ChatService.js +++ b/src/main/services/ChatService.js @@ -62,7 +62,10 @@ const DEFAULTS = { mode: 'hybrid', chunkTopK: 6, chunkWeight: 0.2, - contextFileLimit: 200, + // Matches the 1000-entry cap the chat IPC schema and the renderer allow, so + // Strict scope searches every selected file. Scoring one is an in-memory + // getByID plus one cosine, so 1000 stays cheap. + contextFileLimit: 1000, memoryWindow: 6 }; const HOLISTIC_MIN_TOPK = 12; @@ -78,6 +81,19 @@ const MCP_SESSION_PREFIX = 'mcp:'; // MCP tool reports them as MODEL_NOT_READY instead of RETRIEVAL_FAILED. const MODEL_NOT_READY_CODES = new Set(['NO_MODEL', 'LLAMA_002', 'LLAMA_MODEL_NOT_FOUND']); +/** + * Whether a streaming failure is really a cancel. The local inference queue + * rejects an aborted request with a plain Error (code INFERENCE_SLOT_ABORTED) + * and cloud providers can wrap one as NETWORK_ERROR, so the stream's own + * signal decides, not only the error name. + * + * @param {any} error + * @param {AbortSignal} signal + */ +function isStreamCancel(error, signal) { + return error?.name === 'AbortError' || error?.code === 'INFERENCE_SLOT_ABORTED' || signal.aborted; +} + // Appended to the answer when a cloud provider stopped at its output token // cap. The streamed text is complete as far as it goes, so this is a notice, // not an error. @@ -684,7 +700,7 @@ Now respond as JSON:`; } catch (err) { logger.warn('[ChatService] Conversational streaming failed:', err); try { - const isAbort = err?.name === 'AbortError'; + const isAbort = isStreamCancel(err, streamController.signal); if (onEvent) onEvent({ type: 'error', @@ -721,6 +737,15 @@ Now respond as JSON:`; }); memory._documentScope = documentScopeItems || []; + // Retrieval does not watch the signal, so a Stop or a conversation + // delete during it lands here. Bail before anything is persisted; an + // abstention or error turn would otherwise recreate a deleted chat. + if (streamController.signal.aborted) { + const abortErr = new Error('Generation canceled'); + abortErr.name = 'AbortError'; + throw abortErr; + } + if (onEvent) { const sourceCount = Array.isArray(retrieval?.sources) ? retrieval.sources.length : 0; const droppedLowRelevance = Number( @@ -869,7 +894,7 @@ Now respond as JSON:`; } } catch (error) { logger.error('[ChatService] Streaming query failed:', error); - const isAbort = error?.name === 'AbortError'; + const isAbort = isStreamCancel(error, streamController.signal); if (!isAbort) { try { const memory = await this._getSessionMemory(sessionId); @@ -894,9 +919,7 @@ Now respond as JSON:`; this.chatHistoryStore.addMessage(memory.sessionId, { role: 'assistant', text: '', - modelAnswer: [ - { text: `Error: ${error.message || 'Response failed'}. Please try again.` } - ] + modelAnswer: [{ text: `Error: ${error.message || 'Response failed'}. Try again.` }] }); } } catch (saveErr) { @@ -946,7 +969,7 @@ Now respond as JSON:`; }) { if (!this.llamaService) { const err = new Error( - 'LLM model not loaded — open StratoSort and wait for the model to finish loading.' + "The AI model isn't loaded. Open StratoSort and wait for the model to finish loading." ); /** @type {any} */ (err).code = 'NO_MODEL'; throw err; diff --git a/src/main/services/ClusteringService.js b/src/main/services/ClusteringService.js index 1377f78e..a36724ec 100644 --- a/src/main/services/ClusteringService.js +++ b/src/main/services/ClusteringService.js @@ -19,6 +19,7 @@ const { } = require('../../shared/vectorMath'); const { TIMEOUTS } = require('../../shared/performanceConstants'); const { withAbortableTimeout } = require('../../shared/promiseUtils'); +const { atomicWriteFile } = require('../../shared/atomicFile'); const { getTextModel } = require('../llamaUtils'); const { AI_DEFAULTS } = require('../../shared/constants'); const { isInteractiveTextRequestActive } = require('./textModelActivity'); @@ -395,6 +396,25 @@ const DEFAULT_OPTIONS = { */ const MAX_USER_CLUSTERS = 100; +const USER_LABELS_FILE_NAME = 'cluster-user-labels.json'; +const USER_LABELS_FILE_VERSION = 2; +// A stored name re-attaches only to a cluster that still holds most of the +// files it was given to (Jaccard overlap of member ids). +const USER_LABEL_MIN_OVERLAP = 0.5; +// Names whose files were since regrouped stay stored in case the group comes +// back, but only this many, oldest unattached dropped first. +const MAX_STORED_USER_LABELS = 200; + +/** + * @typedef {{ label: string, memberIds: string[] }} UserLabelEntry + */ + +/** @param {any} cluster @returns {string[]} */ +const getClusterMemberIds = (cluster) => + (Array.isArray(cluster?.members) ? cluster.members : []) + .map((/** @type {any} */ m) => m?.id) + .filter((/** @type {any} */ id) => typeof id === 'string' && id.length > 0); + class ClusteringService { /** * Create a new ClusteringService instance @@ -416,8 +436,23 @@ class ClusteringService { this.clusterLabels = new Map(); this.lastComputedAt = null; - // User-supplied cluster labels (survives recomputation) - this.userLabels = new Map(); // clusterIdStr -> newLabel + // Names the user gave clusters. k-means renumbers clusters on every + // recompute, so each name keeps the member ids it was given to and follows + // whichever new cluster holds most of those files. + /** @type {UserLabelEntry[]} */ + this._userLabelEntries = []; + // Current cluster id -> attached entry, rebuilt after each recompute + /** @type {Map} */ + this._userLabelByClusterId = new Map(); + // Same attachment as "cluster:N" -> name, for getClustersForGraph + /** @type {Map} */ + this.userLabels = new Map(); + /** @type {Promise | null} */ + this._userLabelsLoadPromise = null; + /** @type {string | null} */ + this._userLabelsFilePath = null; + /** @type {Promise} */ + this._userLabelsWriteChain = Promise.resolve(); // Staleness threshold (30 minutes) this.STALE_MS = 30 * 60 * 1000; @@ -447,7 +482,8 @@ class ClusteringService { /** * Rename a cluster with a user-supplied label. - * Persists the label to disk so it survives recomputation. + * Persists the label with the cluster's member ids so it survives restarts + * and re-attaches to the same group of files after a recompute. * * @param {string} clusterIdStr - e.g. "cluster:0" * @param {string} newLabel - The new label to use @@ -455,74 +491,222 @@ class ClusteringService { */ async renameCluster(clusterIdStr, newLabel, labelsFilePath) { if (!clusterIdStr || !newLabel) return; - this.userLabels.set(clusterIdStr, newLabel); + if (labelsFilePath) this._userLabelsFilePath = labelsFilePath; + // Load first so this write does not replace the saved names with only this one + await this._ensureUserLabelsLoaded(labelsFilePath); - // Apply immediately to the in-memory cluster object const numericId = parseInt(String(clusterIdStr).replace(/^cluster:/, ''), 10); - if (!isNaN(numericId)) { - const cluster = this.clusters.find((c) => c.id === numericId); - if (cluster) { - cluster.label = newLabel; - cluster.labelConfidence = 'high'; - } - this.clusterLabels.set(numericId, newLabel); + const cluster = isNaN(numericId) ? null : this.clusters.find((c) => c.id === numericId); + if (!cluster) { + // Without its members the name could only be stored by index, which + // would land on an unrelated group after the next recompute. + throw new Error(`Cluster not found: ${clusterIdStr}`); } - // Persist to disk (best-effort) - if (labelsFilePath) { - try { - const obj = Object.fromEntries(this.userLabels); - await fsPromises.writeFile(labelsFilePath, JSON.stringify(obj, null, 2), 'utf8'); - logger.info('[ClusteringService] User labels persisted'); - } catch (e) { - logger.warn('[ClusteringService] Failed to persist user labels:', e.message); - } - } + /** @type {UserLabelEntry} */ + const entry = { label: newLabel, memberIds: getClusterMemberIds(cluster) }; + const previous = this._userLabelByClusterId.get(numericId); + this._userLabelEntries = this._userLabelEntries.filter((e) => e !== previous); + this._userLabelEntries.push(entry); + this._userLabelByClusterId.set(numericId, entry); + this.userLabels.set(`cluster:${numericId}`, newLabel); + this._trimUserLabelEntries(); + + // Apply immediately to the in-memory cluster object + cluster.label = newLabel; + cluster.labelConfidence = 'high'; + this.clusterLabels.set(numericId, newLabel); + + await this._persistUserLabels(); } /** * Load persisted user labels from disk into memory. * * @param {string} labelsFilePath - Path to the labels JSON file + * @returns {Promise} + */ + loadUserLabels(labelsFilePath) { + this._userLabelsFilePath = labelsFilePath; + this._userLabelsLoadPromise = this._readUserLabels(labelsFilePath); + return this._userLabelsLoadPromise; + } + + /** + * Load the saved names once, from the given path or the default userData file. + * @param {string} [labelsFilePath] + * @returns {Promise} + * @private + */ + _ensureUserLabelsLoaded(labelsFilePath) { + if (this._userLabelsLoadPromise) return this._userLabelsLoadPromise; + const filePath = labelsFilePath || this._userLabelsFilePath || this._defaultUserLabelsPath(); + if (!filePath) { + this._userLabelsLoadPromise = Promise.resolve(); + return this._userLabelsLoadPromise; + } + return this.loadUserLabels(filePath); + } + + /** + * @returns {string | null} + * @private + */ + _defaultUserLabelsPath() { + try { + const { app } = require('electron'); + const userData = app?.getPath?.('userData'); + return userData ? path.join(userData, USER_LABELS_FILE_NAME) : null; + } catch { + return null; + } + } + + /** + * @param {string} labelsFilePath + * @returns {Promise} + * @private */ - async loadUserLabels(labelsFilePath) { + async _readUserLabels(labelsFilePath) { + /** @type {UserLabelEntry[]} */ + let entries = []; try { const raw = await fsPromises.readFile(labelsFilePath, 'utf8'); // safeJsonTryParse both guards against __proto__ pollution and - // distinguishes corrupt content from missing-file errors. Label files - // live in userData and are written atomically, but a crash mid-write - // can leave truncated JSON — treat that as empty rather than throwing. + // distinguishes corrupt content from missing-file errors. A crash + // mid-write can leave truncated JSON; treat that as empty rather than throwing. const parseResult = safeJsonTryParse(raw); + const value = parseResult.success ? parseResult.value : null; if (!parseResult.success) { logger.warn('[ClusteringService] Discarding corrupt user labels file', { error: parseResult.error?.message }); - this.userLabels = new Map(); - return; + } else if (value && typeof value === 'object' && Array.isArray(value.labels)) { + entries = value.labels + .filter( + (/** @type {any} */ e) => + e && typeof e.label === 'string' && e.label && Array.isArray(e.memberIds) + ) + .map((/** @type {any} */ e) => ({ + label: e.label, + memberIds: e.memberIds.filter((/** @type {any} */ id) => typeof id === 'string') + })); + } else if (value && typeof value === 'object' && Object.keys(value).length > 0) { + // The older file keyed names by cluster index only. Those indices + // point at different files after any recompute, so they are dropped. + logger.info('[ClusteringService] Dropping index-keyed user labels from an older file', { + count: Object.keys(value).length + }); } - const obj = - parseResult.value && - typeof parseResult.value === 'object' && - !Array.isArray(parseResult.value) - ? parseResult.value - : {}; - this.userLabels = new Map(Object.entries(obj)); - // Apply to any already-loaded clusters - this.userLabels.forEach((label, idStr) => { - const numericId = parseInt(String(idStr).replace(/^cluster:/, ''), 10); - if (!isNaN(numericId)) { - const cluster = this.clusters.find((c) => c.id === numericId); - if (cluster) { - cluster.label = label; - cluster.labelConfidence = 'high'; - } - this.clusterLabels.set(numericId, label); - } - }); } catch { - // File doesn't exist yet — start with empty map - this.userLabels = new Map(); + // File doesn't exist yet: start with no saved names + } + this._userLabelEntries = entries.slice(-MAX_STORED_USER_LABELS); + this._reattachUserLabels(); + } + + /** + * Attach each saved name to the current cluster whose members overlap its + * saved members the most (at least USER_LABEL_MIN_OVERLAP), one name per + * cluster. Attached names take the cluster's current members, so a group + * that grows a little at a time keeps its name. + * + * @returns {boolean} Whether any saved member list changed + * @private + */ + _reattachUserLabels() { + /** @type {Map} */ + const clusterByMemberId = new Map(); + /** @type {Map} */ + const memberIdsByCluster = new Map(); + for (const cluster of this.clusters) { + const ids = getClusterMemberIds(cluster); + memberIdsByCluster.set(cluster.id, ids); + for (const id of ids) clusterByMemberId.set(id, cluster.id); } + + /** @type {Array<{ entryIdx: number, clusterId: number, score: number }>} */ + const candidates = []; + this._userLabelEntries.forEach((entry, entryIdx) => { + const saved = new Set(entry.memberIds); + /** @type {Map} */ + const shared = new Map(); + for (const id of saved) { + const clusterId = clusterByMemberId.get(id); + if (clusterId !== undefined) shared.set(clusterId, (shared.get(clusterId) || 0) + 1); + } + for (const [clusterId, count] of shared) { + const clusterSize = memberIdsByCluster.get(clusterId)?.length || 0; + const score = count / (saved.size + clusterSize - count); + if (score >= USER_LABEL_MIN_OVERLAP) candidates.push({ entryIdx, clusterId, score }); + } + }); + // Best overlap first; on a tie the newer rename wins + candidates.sort((a, b) => b.score - a.score || b.entryIdx - a.entryIdx); + + /** @type {Map} */ + const byClusterId = new Map(); + const usedEntries = new Set(); + let changed = false; + for (const { entryIdx, clusterId, score } of candidates) { + if (usedEntries.has(entryIdx) || byClusterId.has(clusterId)) continue; + usedEntries.add(entryIdx); + const entry = this._userLabelEntries[entryIdx]; + byClusterId.set(clusterId, entry); + if (score < 1) { + entry.memberIds = memberIdsByCluster.get(clusterId) || []; + changed = true; + } + } + + this._userLabelByClusterId = byClusterId; + this.userLabels = new Map( + Array.from(byClusterId, ([clusterId, entry]) => [`cluster:${clusterId}`, entry.label]) + ); + return changed; + } + + /** + * Keep at most MAX_STORED_USER_LABELS names, dropping the oldest ones that + * are not attached to a current cluster. + * @private + */ + _trimUserLabelEntries() { + let excess = this._userLabelEntries.length - MAX_STORED_USER_LABELS; + if (excess <= 0) return; + const attached = new Set(this._userLabelByClusterId.values()); + this._userLabelEntries = this._userLabelEntries.filter((entry) => { + if (excess > 0 && !attached.has(entry)) { + excess -= 1; + return false; + } + return true; + }); + } + + /** + * Write the saved names (best-effort). Writes run one at a time and each + * writes the latest names, so an older write never lands last. + * @returns {Promise} + * @private + */ + _persistUserLabels() { + const run = async () => { + const filePath = this._userLabelsFilePath; + if (!filePath) return; + try { + await atomicWriteFile( + filePath, + { version: USER_LABELS_FILE_VERSION, labels: this._userLabelEntries }, + { pretty: true } + ); + logger.info('[ClusteringService] User labels persisted'); + } catch (e) { + logger.warn('[ClusteringService] Failed to persist user labels:', e.message); + } + }; + this._userLabelsWriteChain = this._userLabelsWriteChain.then(run, run); + return this._userLabelsWriteChain; } /** @@ -943,6 +1127,10 @@ class ClusteringService { try { logger.info('[ClusteringService] Computing clusters...', { k }); + // Saved names must be in memory before the new clusters replace the old + // ones, so the re-attach below runs with no gap where they are missing. + await this._ensureUserLabelsLoaded(); + const files = await this.getAllFileEmbeddings(); if (files.length < 3) { @@ -977,6 +1165,10 @@ class ClusteringService { this.clusters = clusters; this.centroids = centroids; this.lastComputedAt = Date.now(); + // Labels from the previous run are keyed by its cluster indices, which + // now name different groups of files. + this.clusterLabels = new Map(); + if (this._reattachUserLabels()) await this._persistUserLabels(); logger.info('[ClusteringService] Clustering complete', { files: files.length, @@ -2063,6 +2255,9 @@ Examples of good names: "Q4 Financial Reports", "Employee Onboarding Materials", this.clusters = []; this.centroids = []; this.clusterLabels.clear(); + // The saved names stay; only their attachment to these cluster ids goes + this._userLabelByClusterId = new Map(); + this.userLabels = new Map(); this.lastComputedAt = null; logger.info('[ClusteringService] Clusters cleared'); } @@ -2586,6 +2781,8 @@ Examples of good names: "Q4 Financial Reports", "Employee Onboarding Materials", this.clusters = []; this.centroids = []; this.clusterLabels.clear(); + this._userLabelByClusterId = new Map(); + this.userLabels = new Map(); this.lastComputedAt = null; this._computePromise = null; this._duplicateScanCache = null; diff --git a/src/main/services/DegradationManager.js b/src/main/services/DegradationManager.js index 108f46ad..21cd5356 100644 --- a/src/main/services/DegradationManager.js +++ b/src/main/services/DegradationManager.js @@ -212,7 +212,7 @@ class DegradationManager { return { action: 'redownload_model', - message: 'Model file may be corrupted. Please re-download.', + message: 'Model file may be corrupted. Download it again.', shouldNotifyUser: true, modelType: context.modelType }; @@ -245,7 +245,7 @@ class DegradationManager { if (message.includes('enospc') || message.includes('no space left')) { return { action: 'cleanup_disk', - message: 'Disk full. Please free up space.', + message: 'Disk full. Free up some space.', shouldNotifyUser: true }; } @@ -281,7 +281,7 @@ class DegradationManager { return { canRecover: false, action: 'download_models', - message: 'Please download required models to continue.' + message: 'Download the required models to continue.' }; } diff --git a/src/main/services/DownloadWatcher.js b/src/main/services/DownloadWatcher.js index 3c5aacba..edb0018a 100644 --- a/src/main/services/DownloadWatcher.js +++ b/src/main/services/DownloadWatcher.js @@ -106,7 +106,8 @@ class DownloadWatcher { notificationService, analysisHistoryService, vectorDbService, - folderMatcher + folderMatcher, + filePathCoordinator }) { if (!settingsService) { throw new Error('DownloadWatcher requires settingsService'); @@ -124,6 +125,8 @@ class DownloadWatcher { this.analysisHistoryService = analysisHistoryService; this.vectorDbService = vectorDbService; this.folderMatcher = folderMatcher; + // Optional; resolved from the container when not injected (see _syncIndexesAfterMove). + this.filePathCoordinator = filePathCoordinator || null; this.watcher = null; /** The folder being watched, set by start(); the root the exclusion rules read against. */ this.downloadsPath = null; @@ -890,6 +893,7 @@ class DownloadWatcher { if (analysisResult) { let finalPath = filePath; let renamed = false; + let indexesMoved = false; // 1. Apply naming convention first so any later persistence uses the final path. const settings = presetSettings || (await this.settingsService.load()); @@ -938,6 +942,7 @@ class DownloadWatcher { finalPath = moveResult.destination; renamed = finalPath !== filePath; if (renamed) { + indexesMoved = await this._syncIndexesAfterMove(filePath, finalPath); await this._recordFallbackRenameUndo(filePath, finalPath); } } @@ -969,7 +974,7 @@ class DownloadWatcher { } // 3. Embed for search after persistence so policy overrides see the final path. - await this._embedAnalyzedFile(finalPath, analysisResult); + await this._embedAnalyzedFile(finalPath, analysisResult, { indexesMoved }); } } catch (error) { if (isNotFoundError(error)) { @@ -980,6 +985,113 @@ class DownloadWatcher { } } + /** + * Carry what the analysis pass indexed under the Downloads path over to + * where the file now lives. Analysis runs before the move, so under the + * default 'during_analysis' timing the file vector (queued or flushed) and + * its passages are keyed by the old path; nothing else re-keys them, since + * both watchers ignore their own tracked moves. Without this the old id + * stays behind and the organized file has no vector until the next + * launch's repair. + * @param {string} oldPath + * @param {string} newPath + * @returns {Promise} true when the vector rows were re-keyed + */ + async _syncIndexesAfterMove(oldPath, newPath) { + if (!oldPath || !newPath || oldPath === newPath) return false; + let coordinator = this.filePathCoordinator; + if (!coordinator) { + try { + const { container, ServiceIds } = require('./ServiceContainer'); + coordinator = container.tryResolve(ServiceIds.FILE_PATH_COORDINATOR); + } catch { + coordinator = null; + } + } + if (!coordinator || typeof coordinator.atomicPathUpdate !== 'function') { + logger.warn('[DOWNLOAD-WATCHER] FilePathCoordinator unavailable; index keeps old path', { + oldPath, + newPath + }); + return false; + } + + /** @type {boolean} */ + let indexesMoved; + try { + // Same folder fields batch organize sends, so the vector row and the + // history entry place the file in the folder it moved into. + const smartFolder = findContainingSmartFolder(newPath, this.getCustomFolders?.() || []); + const folderMeta = smartFolder + ? { newSmartFolder: smartFolder.name, newSmartFolderPath: smartFolder.path } + : undefined; + const outcome = await coordinator.atomicPathUpdate(oldPath, newPath, { + type: 'move', + folderMeta + }); + indexesMoved = outcome?.updated?.vectorDb === true; + if (outcome && outcome.success === false) { + logger.warn('[DOWNLOAD-WATCHER] Index path update after move was partial', { + oldPath, + newPath, + errors: outcome.errors + }); + } + } catch (error) { + logger.warn('[DOWNLOAD-WATCHER] Index path update after move failed', { + oldPath, + newPath, + error: error?.message + }); + return false; + } + + // Keyword search builds from paths too; SmartFolderWatcher does the same after a move. + try { + const { getSearchServiceInstance } = require('../ipc/semantic'); + getSearchServiceInstance?.()?.invalidateIndex?.({ + reason: 'download-watcher-move', + oldPath, + newPath + }); + } catch (indexErr) { + logger.debug('[DOWNLOAD-WATCHER] Could not invalidate search index after move:', { + error: indexErr?.message + }); + } + return indexesMoved; + } + + /** + * History state for a file whose analysis-stage vector followed it through + * a move. The queue may have flushed it before the move, when its 'done' + * landed on the old path where no history entry existed yet, so a row + * already at the new id is 'done'; otherwise the queued item now carries + * the new path and writes 'done' there when it flushes. + * @param {string} filePath + * @param {import('../analysis/embeddingHandoff').EmbeddingHandoff} handoff + */ + async _markMovedEmbeddingState(filePath, handoff) { + try { + const row = await this.vectorDbService?.getFile?.(getSemanticFileId(filePath)); + if (row) { + await this.analysisHistoryService?.updateEmbeddingStateByPath?.(filePath, { + status: 'done' + }); + return; + } + const current = await this.analysisHistoryService?.getAnalysisByPath?.(filePath); + if (current?.embedding?.status !== 'done') { + await this.analysisHistoryService?.updateEmbeddingStateByPath?.(filePath, { + status: 'pending', + model: handoff.model || null + }); + } + } catch { + // Non-fatal + } + } + /** * A fallback rename changes the name the user sees in Downloads, so it * gets the same undo entry every other watcher move gets. Auto-organize @@ -1247,6 +1359,8 @@ class DownloadWatcher { throw moveError; } + const indexesMoved = await this._syncIndexesAfterMove(filePath, result.destination); + // Record undo action AFTER the move succeeds to avoid phantom undo entries if (result.undoAction && this.autoOrganizeService?.undoRedo) { try { @@ -1321,7 +1435,7 @@ class DownloadWatcher { }); // This ensures DownloadWatcher-organized files are searchable - await this._embedAnalyzedFile(result.destination, result); + await this._embedAnalyzedFile(result.destination, result, { indexesMoved }); } catch (historyErr) { logger.debug('[DOWNLOAD-WATCHER] Failed to record history entry:', historyErr.message); } @@ -1694,8 +1808,10 @@ class DownloadWatcher { * @private * @param {string} filePath - Path to the file (destination after move) * @param {any} analysisResult - Result from auto-organize analysis + * @param {{ indexesMoved?: boolean }} [options] - indexesMoved: the analysis + * pass's vector rows were re-keyed from the pre-move path to filePath */ - async _embedAnalyzedFile(filePath, analysisResult) { + async _embedAnalyzedFile(filePath, analysisResult, options = {}) { // Skip if dependencies not available if (!this.folderMatcher || !this.vectorDbService) { logger.debug('[DOWNLOAD-WATCHER] Skipping embedding - services not available'); @@ -1723,6 +1839,19 @@ class DownloadWatcher { reason: gate.reason, filePath }); + // Refused only for timing: the analysis pass embedded it and the path + // update carried that vector here, so 'skipped' would be wrong. + const handoff = getEmbeddingHandoff(analysisResult); + if ( + options.indexesMoved && + handoff?.wasEnqueued && + gate.timing === 'during_analysis' && + gate.policy === 'embed' && + !gate.reason + ) { + await this._markMovedEmbeddingState(filePath, handoff); + return; + } try { await this.analysisHistoryService?.updateEmbeddingStateByPath?.(filePath, { status: 'skipped' diff --git a/src/main/services/FolderMatchingService.js b/src/main/services/FolderMatchingService.js index e8dcdde0..6a3a45c7 100644 --- a/src/main/services/FolderMatchingService.js +++ b/src/main/services/FolderMatchingService.js @@ -271,6 +271,21 @@ class FolderMatchingService { // Continue - the cache is still cleared, and users can manually rebuild } } + + // Vectors still waiting in the embedding queues (and their retry + // and dead-letter stores) came from the previous model; writing + // them would mix two vector spaces in the new index. The rebuild + // offered above re-embeds those files with the new model. + try { + require('../analysis/embeddingQueue/queueManager').dropOtherModelEmbeddings( + newModel + ); + } catch (queueError) { + logger.warn( + '[FolderMatchingService] Failed to drop queued embeddings from the previous model:', + queueError.message + ); + } } } ); @@ -1105,7 +1120,7 @@ class FolderMatchingService { }); throw new Error( `Embedding dimension mismatch: expected ${expectedDim}, got ${vector?.length}. ` + - `Model "${model}" may have changed. Please rebuild your embedding index.` + `Model "${model}" may have changed. Rebuild your embedding index.` ); } diff --git a/src/main/services/LlamaService.js b/src/main/services/LlamaService.js index 0911180c..aa407ecc 100644 --- a/src/main/services/LlamaService.js +++ b/src/main/services/LlamaService.js @@ -898,6 +898,12 @@ class LlamaService extends EventEmitter { } try { if (changedTypes.length > 0 && this._modelMemoryManager) { + // Drain in-flight work BEFORE taking the load locks. An op that + // already holds an inference slot (queued past the gate above) loads + // through acquireLoadLock, so waiting for it while holding the lock + // was a circular wait that only the timeout broke. The gate keeps new + // requests out, so this set drains on its own. + const drained = await this._waitForIdleOperations('config-change', 60000); // Acquire load locks before unloading to avoid crashing in-flight inference const releaseLocks = []; try { @@ -925,7 +931,11 @@ class LlamaService extends EventEmitter { ); } } - const safeToUnload = await this._waitForIdleOperations('config-change', 60000); + // Re-check under the locks. An op that took a slot after the drain + // is now blocked on our lock and cannot finish, so wait only + // briefly; if it remains, the stale marking below defers the swap. + const safeToUnload = + drained && (await this._waitForIdleOperations('config-change', 2000)); if (safeToUnload) { await this._modelMemoryManager.unloadAll(); } else { @@ -1438,7 +1448,7 @@ class LlamaService extends EventEmitter { if (settings.llamaGpuLayers === 0) { logger.warn( '[LlamaService] GPU layers set to 0 in settings — AI inference will use CPU only. ' + - 'Set to -1 (auto) in Settings > AI Models for GPU acceleration.' + 'Set to -1 (auto) in Settings → Default AI models for GPU acceleration.' ); } } @@ -1808,7 +1818,7 @@ class LlamaService extends EventEmitter { } if (resolution.action === 'cleanup_disk') { const diskError = /** @type {Error & { code?: string }} */ ( - new Error(`Disk full, cannot load ${modelType} model. Please free up space.`) + new Error(`Disk full, cannot load ${modelType} model. Free up some space.`) ); diskError.code = ERROR_CODES.LLAMA_MODEL_LOAD_FAILED; throw diskError; @@ -2714,7 +2724,7 @@ class LlamaService extends EventEmitter { code: `llama.auto-fallback.${type}`, subsystem: 'llama', severity: 'warning', - message: `Configured ${type} model is missing; using default "${defaultModel}" instead. Reinstall the preferred model from Settings → Models.`, + message: `Configured ${type} model is missing; using default "${defaultModel}" instead. Reinstall the preferred model from Settings → Model management.`, detail: { errorCode: ERROR_CODES.LLAMA_MODEL_AUTO_FALLBACK, type, diff --git a/src/main/services/ModelDownloadManager.js b/src/main/services/ModelDownloadManager.js index dff60c98..21c08979 100644 --- a/src/main/services/ModelDownloadManager.js +++ b/src/main/services/ModelDownloadManager.js @@ -239,70 +239,13 @@ class ModelDownloadManager { existing && (existing.status === 'downloading' || existing.status === 'redirecting' || - existing.status === 'retrying') && + existing.status === 'retrying' || + existing.status === 'installing') && !isRedirectContinuation && !isRetryContinuation ) { throw new Error(`Download already in progress: ${filename}`); } - const retryAttempt = Number.isInteger(options._attempt) ? options._attempt : 0; - const maxRetries = Number.isInteger(options._maxRetries) - ? Math.max(0, options._maxRetries) - : DEFAULT_DOWNLOAD_MAX_RETRIES; - const filePath = path.join(modelPath, filename); - const partialPath = filePath + '.partial'; - - // Check for existing partial download. Done before the space check so a - // resume only has to find room for the bytes still to come. - let startByte = 0; - try { - const partialStats = await fs.stat(partialPath); - if (partialStats.size >= modelInfo.size) { - // Stale/invalid partial (equal or larger than target size) can cause - // bad range requests and unrecoverable retry loops. Restart cleanly. - await this._cleanupPartialFile(partialPath); - logger.warn( - `[Download] Discarded stale partial for ${filename} (${partialStats.size} bytes), restarting` - ); - } else { - startByte = partialStats.size; - logger.info(`[Download] Resuming from byte ${startByte}`); - } - } catch { - // No partial file, start fresh - } - - // Check disk space — aggregated across every other in-flight download. - // Without the aggregate, N concurrent downloads each pass an individual - // check while jointly exceeding free space and failing mid-stream. - let pendingBytes = 0; - for (const [name, state] of this._downloads) { - if (name === filename) continue; - if ( - state.status === 'downloading' || - state.status === 'redirecting' || - state.status === 'retrying' - ) { - pendingBytes += Math.max(0, (state.totalBytes || 0) - (state.downloadedBytes || 0)); - } - } - // The bytes already in the partial are on disk; only the rest needs room. - const neededBytes = Math.max(0, modelInfo.size - startByte) + pendingBytes; - const spaceCheck = await this.checkDiskSpace(neededBytes); - if (!spaceCheck.sufficient) { - if (spaceCheck.checkFailed) { - throw new Error( - `Cannot verify free disk space (${spaceCheck.error || 'unknown error'}); ` + - `refusing to start a ${Math.round(modelInfo.size / 1024 / 1024 / 1024)}GB download` - ); - } - const neededGb = Math.round(neededBytes / 1024 / 1024 / 1024); - throw new Error( - `Insufficient disk space. Need ${neededGb}GB` + - (pendingBytes > 0 ? ' (including in-progress downloads)' : '') + - `, have ${Math.round(spaceCheck.available / 1024 / 1024 / 1024)}GB` - ); - } // Use redirect URL if provided (for following HTTP redirects), with a max redirect limit const downloadUrl = options._redirectUrl || modelInfo.url; @@ -313,18 +256,90 @@ class ModelDownloadManager { // Create an internal AbortController so cancelDownload() can work const internalAbortController = new AbortController(); - // Track download state + // Track download state. Claimed here, before the first await: the + // partial-file and disk-space checks below used to run between the guard + // and the set, so two near-simultaneous requests both passed the guard + // and wrote the same `.partial`. Every exit before the request opens + // releases the slot (identity-checked, like finalizeFailure). const downloadState = { filename, url: validatedDownloadUrl, totalBytes: modelInfo.size, - downloadedBytes: startByte, + downloadedBytes: 0, /** @type {Array<{ time: number, bytes: number }>} */ speedSamples: [], status: 'downloading', abortController: internalAbortController }; this._downloads.set(filename, downloadState); + const releaseSlot = () => { + if (this._downloads.get(filename) === downloadState) this._downloads.delete(filename); + }; + + const retryAttempt = Number.isInteger(options._attempt) ? options._attempt : 0; + const maxRetries = Number.isInteger(options._maxRetries) + ? Math.max(0, options._maxRetries) + : DEFAULT_DOWNLOAD_MAX_RETRIES; + const filePath = path.join(modelPath, filename); + const partialPath = filePath + '.partial'; + + // Check for existing partial download. Done before the space check so a + // resume only has to find room for the bytes still to come. + let startByte = 0; + try { + try { + const partialStats = await fs.stat(partialPath); + if (partialStats.size >= modelInfo.size) { + // Stale/invalid partial (equal or larger than target size) can cause + // bad range requests and unrecoverable retry loops. Restart cleanly. + await this._cleanupPartialFile(partialPath); + logger.warn( + `[Download] Discarded stale partial for ${filename} (${partialStats.size} bytes), restarting` + ); + } else { + startByte = partialStats.size; + logger.info(`[Download] Resuming from byte ${startByte}`); + } + } catch { + // No partial file, start fresh + } + downloadState.downloadedBytes = startByte; + + // Check disk space — aggregated across every other in-flight download. + // Without the aggregate, N concurrent downloads each pass an individual + // check while jointly exceeding free space and failing mid-stream. + let pendingBytes = 0; + for (const [name, state] of this._downloads) { + if (name === filename) continue; + if ( + state.status === 'downloading' || + state.status === 'redirecting' || + state.status === 'retrying' + ) { + pendingBytes += Math.max(0, (state.totalBytes || 0) - (state.downloadedBytes || 0)); + } + } + // The bytes already in the partial are on disk; only the rest needs room. + const neededBytes = Math.max(0, modelInfo.size - startByte) + pendingBytes; + const spaceCheck = await this.checkDiskSpace(neededBytes); + if (!spaceCheck.sufficient) { + if (spaceCheck.checkFailed) { + throw new Error( + `Cannot verify free disk space (${spaceCheck.error || 'unknown error'}); ` + + `refusing to start a ${Math.round(modelInfo.size / 1024 / 1024 / 1024)}GB download` + ); + } + const neededGb = Math.round(neededBytes / 1024 / 1024 / 1024); + throw new Error( + `Insufficient disk space. Need ${neededGb}GB` + + (pendingBytes > 0 ? ' (including in-progress downloads)' : '') + + `, have ${Math.round(spaceCheck.available / 1024 / 1024 / 1024)}GB` + ); + } + } catch (error) { + releaseSlot(); + throw error; + } return new Promise((resolve, reject) => { const url = new URL(validatedDownloadUrl); @@ -390,7 +405,9 @@ class ModelDownloadManager { // Keep the map slot occupied during the retry delay so a second // caller (or a second failure event) cannot start a parallel stream. downloadState.status = 'retrying'; - } else if (this._downloads.has(filename)) { + } else if (this._downloads.get(filename) === downloadState) { + // Identity check: after a cancel in the checksum phase a new + // download of the same file may already hold the slot. this._downloads.delete(filename); } @@ -596,9 +613,25 @@ class ModelDownloadManager { if (typeof writeStream.close === 'function') { writeStream.close(); } + // The abort listeners go once the body is written, but checking a + // multi-GB file and fetching a vision projector take a while. A + // cancel in that time must still stop the install, not just report + // "cancelled" while the model lands anyway. + const finishSignal = AbortSignal.any( + [internalAbortController.signal, signal].filter(Boolean) + ); + const stopIfCancelled = (/** @type {boolean} */ dropPartial) => { + if (!finishSignal.aborted && downloadState.status !== 'cancelled') return false; + // Leave the partial alone if a new download of this file already + // took the slot: it is that download's file now. + const ownsSlot = this._downloads.get(filename) === downloadState; + finalizeFailure(new Error('Download cancelled'), 'cancelled', dropPartial && ownsSlot); + return true; + }; try { // Verify file size against server-reported Content-Length (or catalog fallback). const stats = await fs.stat(partialPath); + if (stopIfCancelled(true)) return; if (stats.size !== expectedBytes) { finalizeFailure( new Error( @@ -611,7 +644,14 @@ class ModelDownloadManager { } // Verify checksum (required for all remote model artifacts). - const isValid = await this._verifyChecksum(partialPath, expectedChecksum); + let isValid; + try { + isValid = await this._verifyChecksum(partialPath, expectedChecksum, finishSignal); + } catch (verifyError) { + if (stopIfCancelled(true)) return; + throw verifyError; + } + if (stopIfCancelled(true)) return; if (!isValid) { finalizeFailure( new Error('Download corrupted - checksum mismatch'), @@ -621,10 +661,23 @@ class ModelDownloadManager { return; } - // Rename to final filename + // Rename to final filename. A rename cannot be called back, so + // cancelDownload refuses while it runs ('installing'). + downloadState.status = 'installing'; await fs.rename(partialPath, filePath); - downloadState.status = 'complete'; - if (this._downloads.has(filename)) this._downloads.delete(filename); + const needsCompanion = Boolean( + modelInfo.clipModel && modelInfo.clipModel.name && modelInfo.clipModel.url + ); + if (needsCompanion) { + // Keep the slot while the projector downloads so a cancel aimed + // at this model reaches it. No bytes left to count for space. + downloadState.status = 'downloading'; + downloadState.downloadedBytes = expectedBytes; + downloadState.totalBytes = expectedBytes; + } else { + downloadState.status = 'complete'; + if (this._downloads.get(filename) === downloadState) this._downloads.delete(filename); + } const finalProgress = { filename, @@ -641,7 +694,7 @@ class ModelDownloadManager { let companionDownload = null; // Auto-download clipModel companion (mmproj) for vision models - if (modelInfo.clipModel && modelInfo.clipModel.name && modelInfo.clipModel.url) { + if (needsCompanion) { const companionPath = path.join(modelPath, modelInfo.clipModel.name); const companionTotalBytes = Number(modelInfo.clipModel.size) || 0; try { @@ -659,7 +712,7 @@ class ModelDownloadManager { try { const companionResult = await this.downloadModel(modelInfo.clipModel.name, { onProgress, - signal + signal: finishSignal }); companionDownload = { name: modelInfo.clipModel.name, @@ -685,6 +738,14 @@ class ModelDownloadManager { } } + if (needsCompanion) { + // The model file is installed; a cancel during the projector + // still ends as cancelled, matching what the cancel call said. + if (stopIfCancelled(false)) return; + downloadState.status = 'complete'; + if (this._downloads.get(filename) === downloadState) this._downloads.delete(filename); + } + const companionDownloadedBytes = Number(companionDownload?.downloadedBytes) || 0; const companionTotalBytes = Number(companionDownload?.totalBytes) || 0; const degraded = Boolean(companionDownload) && companionDownload.success === false; @@ -827,7 +888,8 @@ class ModelDownloadManager { !!state && (state.status === 'downloading' || state.status === 'redirecting' || - state.status === 'retrying') + state.status === 'retrying' || + state.status === 'installing') ); } @@ -836,7 +898,8 @@ class ModelDownloadManager { */ cancelDownload(/** @type {any} */ filename) { const download = this._downloads.get(filename); - if (download && download.abortController) { + // 'installing': the file is being renamed into place and cannot be undone. + if (download && download.abortController && download.status !== 'installing') { download.status = 'cancelled'; download.abortController.abort(); return true; @@ -865,7 +928,7 @@ class ModelDownloadManager { return { success: false, error: inUse - ? 'Model file is in use — it may be loaded by the AI engine. Switch the active model or restart the app, then try again.' + ? 'Model file is in use. The AI engine may have it loaded. Switch the active model or restart the app, then try again.' : `Could not delete model file: ${error.message}`, errorCode: error?.code || 'DELETE_FAILED' }; @@ -916,7 +979,7 @@ class ModelDownloadManager { const code = error?.code; /** @type {Record} */ const messageMap = { - ENOSPC: `Disk full — not enough free space to download ${filename}`, + ENOSPC: `Disk full: not enough free space to download ${filename}`, EACCES: `Permission denied writing ${filename} (check models directory permissions)`, EPERM: `Permission denied writing ${filename} (check models directory permissions)`, ENOENT: `Models directory missing while writing ${filename}`, @@ -1016,10 +1079,29 @@ class ModelDownloadManager { return parsedUrl; } - async _verifyChecksum(/** @type {any} */ filePath, /** @type {any} */ expectedHash) { + async _verifyChecksum( + /** @type {any} */ filePath, + /** @type {any} */ expectedHash, + /** @type {AbortSignal|undefined} */ abortSignal = undefined + ) { return new Promise((resolve, reject) => { const hash = crypto.createHash('sha256'); const stream = require('fs').createReadStream(filePath); + // Hashing a multi-GB file takes tens of seconds; stop reading on cancel. + if (abortSignal) { + const onAbort = () => { + if (typeof stream.destroy === 'function') stream.destroy(); + reject(new Error('Download cancelled')); + }; + if (abortSignal.aborted) { + onAbort(); + return; + } + abortSignal.addEventListener('abort', onAbort, { once: true }); + const detach = () => abortSignal.removeEventListener('abort', onAbort); + stream.on('end', detach); + stream.on('error', detach); + } stream.on('data', (chunk) => hash.update(chunk)); stream.on('end', () => { diff --git a/src/main/services/NotificationService.js b/src/main/services/NotificationService.js index f4860f97..7e2dd76f 100644 --- a/src/main/services/NotificationService.js +++ b/src/main/services/NotificationService.js @@ -146,13 +146,12 @@ class NotificationService { return; } - const searchTimerId = setTimeout(() => { + // No 'closed' hook to clear this timer: _emitNotificationAction already + // skips destroyed windows, and a once('closed') per click piled up + // listeners on the long-lived main window. + setTimeout(() => { this._emitNotificationAction(activation); }, 100); - - for (const win of windows) { - win.once('closed', () => clearTimeout(searchTimerId)); - } } /** diff --git a/src/main/services/OramaVectorService.js b/src/main/services/OramaVectorService.js index 3484e49e..231ce139 100644 --- a/src/main/services/OramaVectorService.js +++ b/src/main/services/OramaVectorService.js @@ -373,6 +373,30 @@ async function serializeCollectionForDisk(db) { return { buffer, chars }; } +/** + * Every document of a collection, read straight from Orama's docs store (the + * same `data.docs.docs` that serializeCollectionForDisk saves). Shallow copies, + * so a caller cannot change a stored document in place. + * + * This used to call persist(db), whose default 'binary' format returns an + * encoded string, not an object: the shape check never matched, and every call + * encoded the whole collection (vector index included) on the main thread for + * nothing before falling back to paged search. + * + * @param {any} db + * @returns {any[] | null} null when the store has another shape; the caller + * then pages through search instead. + */ +function readStoredDocuments(db) { + const docsById = db?.data?.docs?.docs; + if (!docsById || typeof docsById !== 'object') return null; + const docs = []; + for (const doc of Object.values(docsById)) { + if (doc && typeof doc === 'object') docs.push({ ...doc }); + } + return docs; +} + const writeFileAtomic = async (/** @type {any} */ filePath, /** @type {any} */ data) => { const tempPath = `${filePath}.tmp.${require('crypto').randomUUID()}`; try { @@ -894,21 +918,9 @@ class OramaVectorService extends EventEmitter { async _getAllFileDocuments() { if (!this._databases?.files) return []; - // Prefer a persistence snapshot so repair/fallback logic is not capped by search limits. - try { - const snapshot = /** @type {any} */ (await persist(this._databases.files)); - const docsById = snapshot?.docs?.docs; - if (docsById && typeof docsById === 'object') { - return Object.values(docsById).filter((doc) => doc && typeof doc === 'object'); - } - } catch (error) { - logger.warn( - '[OramaVectorService] Failed to snapshot files DB; falling back to paged search', - { - error: error?.message - } - ); - } + // Read the docs store directly so repair/fallback logic is not capped by search limits. + const stored = readStoredDocuments(this._databases.files); + if (stored) return stored; const pageSize = 1000; const docs = []; @@ -950,20 +962,8 @@ class OramaVectorService extends EventEmitter { async _getAllFolderDocuments() { if (!this._databases?.folders) return []; - try { - const snapshot = /** @type {any} */ (await persist(this._databases.folders)); - const docsById = snapshot?.docs?.docs; - if (docsById && typeof docsById === 'object') { - return Object.values(docsById).filter((doc) => doc && typeof doc === 'object'); - } - } catch (error) { - logger.warn( - '[OramaVectorService] Failed to snapshot folders DB; falling back to paged search', - { - error: error?.message - } - ); - } + const stored = readStoredDocuments(this._databases.folders); + if (stored) return stored; const pageSize = 1000; const docs = []; @@ -1864,6 +1864,18 @@ class OramaVectorService extends EventEmitter { }; } + /** + * The embedding model this index holds vectors for, or null before the + * store has resolved it. The embedding queue drops pending vectors from any + * other model instead of writing them here. + * @returns {string | null} + */ + getEmbeddingModel() { + return typeof this._embeddingModel === 'string' && this._embeddingModel + ? this._embeddingModel + : null; + } + /** * Reconcile the vector store with a change of embedding model. If the new * model's dimension or identity differs, every collection is reset — @@ -4010,6 +4022,22 @@ class OramaVectorService extends EventEmitter { await this.initialize(); + // A NaN or Infinity anywhere makes the stored vector unsearchable while + // the document still looks embedded, so refuse it like folders do. + const valueValidation = this._validateEmbeddingValues(file.vector, 'files'); + if (!valueValidation.valid) { + this.emit('embedding-blocked', { + type: valueValidation.error, + fileId: file.id, + message: 'Embedding vector contains invalid values.' + }); + return { + success: false, + fileId: file.id, + error: valueValidation.error + }; + } + // Validate dimensions const dimValidation = await this.validateEmbeddingDimension(file.vector, 'files'); if (!dimValidation.valid) { @@ -4101,37 +4129,52 @@ class OramaVectorService extends EventEmitter { await this.initialize(); - // Validate batch dimensions before upserting any items to avoid partial writes + // Fail only the vectors of the wrong length. Rejecting the whole batch let + // one stale item (queued before an embedding-model switch) send every + // valid item beside it to the dead-letter queue as dimension_mismatch. const dimValidation = this._validateBatchDimensions(files, 'files'); + /** @type {any[]} */ + let toUpsert = files; + /** @type {Array<{ id: any, error: string, requiresRebuild?: boolean }>} */ + const failed = []; if (!dimValidation.valid && dimValidation.error === 'dimension_mismatch') { + toUpsert = []; + for (const f of files) { + const wrongLength = + Array.isArray(f?.vector) && + f.vector.length > 0 && + f.vector.length !== dimValidation.expectedDim; + if (wrongLength) { + failed.push({ id: f.id || f.path, error: 'dimension_mismatch', requiresRebuild: true }); + } else { + toUpsert.push(f); + } + } this.emit('embedding-blocked', { type: 'dimension_mismatch', - fileCount: files.length, + fileCount: failed.length, expectedDim: dimValidation.expectedDim, actualDim: dimValidation.actualDim, message: 'Embedding model changed. Run "Rebuild Embeddings" to fix.' }); - return { - success: false, - count: 0, - failed: files.map((/** @type {any} */ f) => ({ - id: f.id || f.path, + if (toUpsert.length === 0) { + return { + success: false, + count: 0, + failed, error: 'dimension_mismatch', requiresRebuild: true - })), - error: 'dimension_mismatch', - requiresRebuild: true - }; + }; + } } let successCount = 0; - const failed = []; - const upsertResults = await this._runBoundedBatch(files, (/** @type {any} */ file) => + const upsertResults = await this._runBoundedBatch(toUpsert, (/** @type {any} */ file) => this.upsertFile(file) ); for (let i = 0; i < upsertResults.length; i++) { const settled = upsertResults[i]; - const file = files[i]; + const file = toUpsert[i]; if (settled?.status === 'fulfilled') { const result = settled.value; if (result?.success !== false) { diff --git a/src/main/services/OrganizeResumeService.js b/src/main/services/OrganizeResumeService.js index bdc2df53..067b33e2 100644 --- a/src/main/services/OrganizeResumeService.js +++ b/src/main/services/OrganizeResumeService.js @@ -1,7 +1,7 @@ // @ts-check const path = require('path'); const fs = require('fs').promises; -const { atomicFileOps } = require('../../shared/atomicFileOperations'); +const { atomicFileOps, isCaseOnlyRenameOfSelf } = require('../../shared/atomicFileOperations'); const { IPC_EVENTS } = require('../../shared/constants'); const { safeSend } = require('../ipc/ipcWrappers'); const { computeFileChecksum, handleDuplicateMove } = require('../utils/fileDedup'); @@ -11,6 +11,10 @@ const { acquireBatchLock, releaseBatchLock } = require('../ipc/files/batchLockMa const { deferGhostCleanup, resumeGhostCleanup } = require('../../shared/ghostCleanupGuard'); const { migrateLegacyPersistedBatch } = require('./ProcessingStateService'); const { numberingBaseOf } = require('../ipc/files/collisionNames'); +const { + authorizeDestination, + buildStrictFileOpsAllowlist +} = require('../ipc/files/destinationPolicy'); /** * Resume runs replay operations from a persisted journal, so the source and @@ -43,6 +47,90 @@ async function validateResumeOperationPath(candidate, role, logger) { return validation.normalizedPath; } +/** + * Errors the live handler (or an earlier resume) journals when a path or + * destination rule refuses an op: a folder the user declined, a startup + * folder, a symlinked source. Those ops were refused on purpose, not + * interrupted, so resume must never replay them. + */ +const REFUSAL_ERROR_PREFIXES = ['Invalid source path', 'Invalid destination path', 'Unsafe ']; + +/** + * @param {any} op + * @returns {boolean} + */ +function isRefusedOp(op) { + return ( + op?.status === 'failed' && + typeof op.error === 'string' && + REFUSAL_ERROR_PREFIXES.some((prefix) => op.error.startsWith(prefix)) + ); +} + +/** + * Apply the live handler's move rules before a replayed move: a source + * that is a symlink to somewhere unsafe, the STRATOSORT_STRICT_FILE_OPS + * allowlist, and the destination policy. Folders approved in a dialog last + * session are not remembered, and nobody asked for this replay, so a + * destination that would need confirming is refused rather than prompted + * for. Throws with the live handler's message prefixes so the op is + * journaled as a refusal. + * @param {any} op + * @param {any} serviceIntegration + * @param {any} logger + * @returns {Promise} + */ +async function authorizeResumeMove(op, serviceIntegration, logger) { + const getServiceIntegration = () => serviceIntegration; + const strictFileOps = buildStrictFileOpsAllowlist(getServiceIntegration); + const strictPathOptions = strictFileOps.enabled + ? { allowedBasePaths: strictFileOps.allowlist } + : {}; + const sourceValidation = await validateFileOperationPath(op.source, { + checkSymlinks: true, + ...strictPathOptions + }); + if (!sourceValidation.valid) { + throw new Error(`Invalid source path: ${sourceValidation.error}`); + } + const destValidation = await validateFileOperationPath(op.destination, { + checkSymlinks: false, + ...strictPathOptions + }); + if (!destValidation.valid) { + throw new Error(`Invalid destination path: ${destValidation.error}`); + } + const decision = await authorizeDestination(destValidation.normalizedPath, { + sourcePath: sourceValidation.normalizedPath, + getServiceIntegration, + log: logger, + // No showMessageBox: a folder that needs the user's say is refused. + dialog: {} + }); + if (!decision.allowed) { + logger?.warn?.('[RESUME] Destination refused; not replaying op', { + destination: op.destination, + reason: decision.reason + }); + throw new Error(`Invalid destination path: ${decision.error}`); + } +} + +/** + * Whether the batch was closed (completed or rolled back) after the + * incomplete list was read. Checks the current journal entry too, in case + * the state object was replaced since. + * @param {any} batch + * @param {any} processingState + * @returns {boolean} + */ +function isBatchClosed(batch, processingState) { + const current = processingState?.state?.organize?.batches?.[batch.id]; + return Boolean( + batch.completedAt || batch.rolledBackAt || current?.completedAt || current?.rolledBackAt + ); +} + /** * The source size+mtime the live handler journaled with the started mark, * or null for legacy journal entries that carry none. @@ -269,6 +357,15 @@ async function resumeIncompleteBatches( logger.warn(`[RESUME] Resuming ${incomplete.length} incomplete organize batch(es)`); for (const batch of incomplete) { + // A batch this process started is a live run, not one a crash cut + // short: its handler (or the user's Resume) owns it. Replaying or + // finalizing it here would move files the user cancelled, or close a + // paused batch the renderer still offers to resume. + if (serviceIntegration.processingState.isSessionOrganizeBatch?.(batch.id)) { + logger?.info?.('[RESUME] Skipping batch started in this session', { batchId: batch.id }); + continue; + } + // A rolled-back batch is normally closed by the rollback path itself // and filtered out by getIncompleteOrganizeBatches; seeing one here // means the journal was written by an older build (or a crash landed @@ -306,6 +403,16 @@ async function resumeIncompleteBatches( }); continue; } + // The wait for the lock can outlast a live run that closed this entry + // (completed, cancelled or rolled back); replaying it then would move + // files the user cancelled. + if (isBatchClosed(batch, serviceIntegration.processingState)) { + logger?.info?.('[RESUME] Batch closed while waiting for the lock; skipping', { + batchId: batch.id + }); + releaseBatchLock(batch.id); + continue; + } deferGhostCleanup(); try { await resumeBatch(batch, serviceIntegration, logger, getMainWindow); @@ -320,8 +427,8 @@ async function resumeIncompleteBatches( } /** - * Replay one incomplete batch: skip done ops, re-run pending/failed ones, - * then persist and close the journal entry. Caller holds the batch lock. + * Replay one incomplete batch: skip done ops and ops a path or destination + * rule refused, re-run the other pending/failed ones, then persist and close the journal entry. Caller holds the batch lock. */ async function resumeBatch( /** @type {any} */ batch, @@ -351,10 +458,21 @@ async function resumeBatch( // Files this replay has still to move: the duplicate check must not count // one of them as a copy already in its destination folder. const pendingSources = batch.operations - .filter((/** @type {any} */ op) => op.status !== 'done' && typeof op.source === 'string') + .filter( + (/** @type {any} */ op) => + op.status !== 'done' && !isRefusedOp(op) && typeof op.source === 'string' + ) .map((/** @type {any} */ op) => op.source); for (let i = 0; i < total; i += 1) { const op = batch.operations[i]; + if (isRefusedOp(op)) { + logger?.info?.('[RESUME] Leaving refused op failed', { + batchId: batch.id, + index: i, + error: op.error + }); + continue; + } if (op.status === 'done') { const win = getMainWindow?.(); if (win && !win.isDestroyed()) { @@ -379,10 +497,15 @@ async function resumeBatch( await validateResumeOperationPath(op.source, 'source', logger); await validateResumeOperationPath(op.destination, 'destination', logger); - await serviceIntegration.processingState.markOrganizeOpStarted(batch.id, i); - const sourceExists = await pathExists(op.source, logger, 'source'); const destinationExists = await pathExists(op.destination, logger, 'destination'); + // Only a move still to make needs the live rules; a source already gone + // means the move happened and resume just records it. + if (sourceExists) { + await authorizeResumeMove(op, serviceIntegration, logger); + } + + await serviceIntegration.processingState.markOrganizeOpStarted(batch.id, i); if (!sourceExists) { const recorded = recordedSourceMetadata(op); @@ -523,8 +646,11 @@ async function resumeBatch( // Check destination collision and adjust. Resolve against the // local `finalDestination` first so we don't leave a - // half-updated `op.destination` visible to observers. - if (destinationExists) { + // half-updated `op.destination` visible to observers. A case-only + // rename (Invoice.pdf -> invoice.pdf) on a case-insensitive + // filesystem finds the source itself at the destination; that name is + // free, and numbering it would resume to `invoice_1.pdf`. + if (destinationExists && !(await isCaseOnlyRenameOfSelf(op.source, finalDestination))) { let counter = 1; let uniqueDestination; // Numbered as the live batch numbers it: from the name before an diff --git a/src/main/services/ProcessingStateService.js b/src/main/services/ProcessingStateService.js index 2a61df33..9185fbc3 100644 --- a/src/main/services/ProcessingStateService.js +++ b/src/main/services/ProcessingStateService.js @@ -96,6 +96,11 @@ class ProcessingStateService { /** @type {Promise} */ this._stateMutationLock = Promise.resolve(); + // Organize batches this process started. In memory only: startup resume + // replays crash-interrupted batches from an earlier run, never a live one. + /** @type {Set} */ + this._sessionOrganizeBatchIds = new Set(); + // Debounce persistence: coalesce rapid state transitions into a single // disk write. Callers that await saveState() wait up to one window, so // the window bounds organize throughput: five workers each awaiting the @@ -1272,6 +1277,7 @@ class ProcessingStateService { startedAt: now, completedAt: null }; + this._sessionOrganizeBatchIds.add(batchId); this.state.organize.lastUpdated = now; await this.saveState(); } @@ -1452,6 +1458,16 @@ class ProcessingStateService { return this._stateMutationLock; } + /** + * Whether this process created the organize batch (as opposed to loading + * it from an earlier run's journal). + * @param {string} batchId + * @returns {boolean} + */ + isSessionOrganizeBatch(batchId) { + return this._sessionOrganizeBatchIds.has(batchId); + } + getIncompleteOrganizeBatches() { if (!this.state) return []; // rolledBackAt without completedAt means a crash landed between the two diff --git a/src/main/services/QueryProcessor.js b/src/main/services/QueryProcessor.js index d7881cca..07000827 100644 --- a/src/main/services/QueryProcessor.js +++ b/src/main/services/QueryProcessor.js @@ -21,6 +21,13 @@ const { LRUCache } = require('../../shared/LRUCache'); const { getInstance: getCacheInvalidationBus } = require('../../shared/cacheInvalidation'); const logger = createLogger('QueryProcessor'); + +/** + * Whether a word occurs in the user's indexed files: true or false, or null + * when the index cannot answer. + * @typedef {(word: string) => boolean | null} IndexedTermLookup + */ + /** * QueryProcessor handles spell correction, synonym expansion, and query normalization */ @@ -29,6 +36,8 @@ class QueryProcessor { constructor(options = {}) { this._nlp = null; // Lazy loaded this._nlpUnavailable = false; + /** @type {any} Empty wink doc whose isOOV() answers lexicon lookups */ + this._lexiconDoc = null; // Common English stop words that should NEVER be spell-corrected // These are function words that provide grammatical structure @@ -330,12 +339,21 @@ class QueryProcessor { /** * Process a search query with spell correction and synonym expansion * + * `isIndexedTerm` lets the caller check a word against the user's indexed + * files: it returns true or false, or null when the index cannot answer + * (not built yet), in which case correction ignores it. + * * @param {string} query - Raw user query - * @param {{ expandSynonyms?: boolean, correctSpelling?: boolean, maxSynonymsPerWord?: number }} [options] + * @param {{ expandSynonyms?: boolean, correctSpelling?: boolean, maxSynonymsPerWord?: number, isIndexedTerm?: IndexedTermLookup }} [options] * @returns {Promise<{original: string, corrected: string, expanded: string, corrections: Array, synonymsAdded: Array}>} */ async processQuery(query, options = {}) { - const { expandSynonyms = true, correctSpelling = false, maxSynonymsPerWord = 3 } = options; + const { + expandSynonyms = true, + correctSpelling = false, + maxSynonymsPerWord = 3, + isIndexedTerm + } = options; if (!query || typeof query !== 'string') { return { @@ -366,10 +384,14 @@ class QueryProcessor { // Step 1: Spell correction (typos) if (correctSpelling) { - const corrected = this._correctSpelling(word); + const corrected = this._correctSpelling(word, isIndexedTerm); if (corrected !== word) { corrections.push({ original: word, corrected }); this.stats.correctionsApplied++; + // Keep the typed term in the keyword (BM25) query next to the + // correction: a word the vocabulary does not know can still be a + // real term in the user's files, and exact matches must keep hitting. + processed.push(word); } currentWord = corrected; } @@ -427,14 +449,19 @@ class QueryProcessor { * Strategy: * 1. NEVER correct stop words (common English function words) * 2. NEVER correct very short words (< 6 chars) - too risky - * 3. Only correct if edit distance is 1 (single typo) to avoid false matches - * 4. Only match against longer domain words (>= 5 chars) to avoid short word confusion - * 5. Use phonetic matching only for longer words (8+ chars) + * 3. NEVER correct a word the English lexicon knows ("audit", "protect") + * 4. NEVER correct a word that occurs in the user's indexed files (a + * surname, product code or project name) + * 5. Only correct if edit distance is 1 (single typo) to avoid false matches + * 6. Only match against longer domain words (>= 5 chars) to avoid short word confusion + * 7. Phonetic matching (longer words, 8+ chars) must also be within edit distance 2 + * 8. Among candidates, prefer one that occurs in the indexed files * * @param {string} word - Word to correct + * @param {IndexedTermLookup} [isIndexedTerm] - Index vocabulary check * @returns {string} Corrected word or original if no correction found */ - _correctSpelling(word) { + _correctSpelling(word, isIndexedTerm) { // Rule 1: NEVER correct stop words (they're valid English) // This check MUST happen first before any other logic if (this.stopWords.has(word.toLowerCase())) { @@ -453,6 +480,19 @@ class QueryProcessor { return word; } + // Rule 4: A real English word is not a typo, even when it sits one edit + // away from a domain word ("audit" vs "audio", "protect" vs "project"). + if (this._isKnownEnglishWord(word)) { + return word; + } + + // Rule 5: A word the user's files contain is a real term there, even + // when it is one edit from a domain word ("Budgen" vs "budget"). + const inIndex = (/** @type {string} */ term) => this._lookupIndexedTerm(isIndexedTerm, term); + if (inIndex(word) === true) { + return word; + } + // Find closest match using edit distance (Levenshtein) // ONLY consider corrections with 1 character difference (single typo) // ONLY match against longer domain words (>= 5 chars) to avoid contamination @@ -468,17 +508,24 @@ class QueryProcessor { const d = distance(word.toLowerCase(), known.toLowerCase()); if (d === 1) { - // Only accept single-character typos - bestMatch = known; - break; // Take first match to avoid over-correction + // Only accept single-character typos. Keep the first match, but a + // later match the indexed files contain wins, since only that one + // can add keyword hits. + if (bestMatch === word) bestMatch = known; + if (inIndex(known) !== false) { + bestMatch = known; + break; + } } } - // Rule 4: Only use phonetic matching for longer words where - // the user might have made more severe spelling mistakes + // Rule 6: Only use phonetic matching for longer words where the user + // might have made a worse mistake. A Soundex code alone is too loose + // ("personality" and "presentation" share one), so the match must also + // be within two edits. if (bestMatch === word && word.length >= 8) { const phoneticMatch = this._phoneticMatch(word); - if (phoneticMatch) { + if (phoneticMatch && distance(word.toLowerCase(), phoneticMatch.toLowerCase()) <= 2) { bestMatch = phoneticMatch; } } @@ -486,6 +533,45 @@ class QueryProcessor { return bestMatch; } + /** + * Ask the caller's index vocabulary about a term. Any failure counts as + * "cannot answer" so correction falls back to the index-free rules. + * + * @param {IndexedTermLookup | undefined} isIndexedTerm + * @param {string} term + * @returns {boolean|null} + */ + _lookupIndexedTerm(isIndexedTerm, term) { + if (typeof isIndexedTerm !== 'function') return null; + try { + const answer = isIndexedTerm(term.toLowerCase()); + return typeof answer === 'boolean' ? answer : null; + } catch (error) { + logger.debug('[QueryProcessor] Index vocabulary lookup failed for:', term, error.message); + return null; + } + } + + /** + * Whether the bundled English lexicon (wink-nlp model) knows this word. + * Words the model only learned from earlier input count as unknown. + * + * @param {string} word + * @returns {boolean} false when the lexicon is unavailable + */ + _isKnownEnglishWord(word) { + const nlp = this._getNlp(); + if (!nlp) return false; + try { + if (!this._lexiconDoc) this._lexiconDoc = nlp.readDoc(''); + if (typeof this._lexiconDoc?.isOOV !== 'function') return false; + return !this._lexiconDoc.isOOV(word.toLowerCase()); + } catch (error) { + logger.debug('[QueryProcessor] Lexicon lookup failed for:', word, error.message); + return false; + } + } + /** * Get lightweight "synonyms" using wink-nlp lemmas * @@ -714,6 +800,7 @@ class QueryProcessor { } this.synonymCache.clear(); this._nlp = null; + this._lexiconDoc = null; this._nlpUnavailable = false; logger.info('[QueryProcessor] Cleanup complete'); } diff --git a/src/main/services/RelationshipIndexService.js b/src/main/services/RelationshipIndexService.js index 72023225..9a125a3c 100644 --- a/src/main/services/RelationshipIndexService.js +++ b/src/main/services/RelationshipIndexService.js @@ -253,9 +253,12 @@ class RelationshipIndexService { } for (let i = 0; i < files.length; i += 1) { for (let j = i + 1; j < files.length; j += 1) { - const source = files[i]; - const target = files[j]; - const key = source < target ? `${source}|${target}` : `${target}|${source}`; + const [source, target] = + files[i] < files[j] ? [files[i], files[j]] : [files[j], files[i]]; + // Join on NUL, not '|': on macOS and Linux a file name may contain + // '|', and splitting the key on it cut such a path and dropped the + // edge. No path can contain NUL, so the split below is exact. + const key = `${source}\u0000${target}`; // Cap edge candidates to prevent unbounded memory growth if (edgeCounts.size >= DEFAULTS.maxEdgeCandidates && !edgeCounts.has(key)) continue; edgeCounts.set(key, (edgeCounts.get(key) || 0) + 1); @@ -279,7 +282,7 @@ class RelationshipIndexService { .sort((a, b) => b[1] - a[1]) .slice(0, DEFAULTS.maxEdges) .map(([key, weight]) => { - const [source, target] = key.split('|'); + const [source, target] = key.split('\u0000'); return { id: `rel:${source}->${target}`, source, diff --git a/src/main/services/SearchService.js b/src/main/services/SearchService.js index 6f11e44e..e35ba2fb 100644 --- a/src/main/services/SearchService.js +++ b/src/main/services/SearchService.js @@ -649,6 +649,37 @@ class SearchService { } } + /** + * Whether a word occurs in the indexed files, for spell correction. Reads + * the per-field token counts Orama keeps for BM25 scoring, after running + * the word through the index's own tokenizer (so "Kubernetes" matches the + * stored stem). Nothing is copied or cached: each call is a few hash + * lookups against whichever index is live, so a rebuild needs no + * invalidation here. + * + * @param {string} word + * @returns {boolean|null} null while no index is built (or its layout is + * not the one expected), so callers keep their index-free behavior + */ + isIndexedTerm(word) { + const index = /** @type {any} */ (this.bm25Index); + const occurrences = index?.data?.index?.tokenOccurrences; + const tokenizer = index?.tokenizer; + if (!occurrences || typeof tokenizer?.tokenize !== 'function') return null; + try { + const tokens = tokenizer.tokenize(String(word || ''), tokenizer.language); + if (!Array.isArray(tokens) || tokens.length === 0) return false; + // The id field holds file paths, not words the user wrote. + const fields = Object.keys(occurrences).filter((field) => field !== 'id'); + return tokens.every((/** @type {string} */ token) => + fields.some((field) => occurrences[field]?.[token] > 0) + ); + } catch (error) { + logger.debug('[SearchService] Index term lookup failed:', error.message); + return null; + } + } + /** * Truncate text to a maximum length for indexing * @@ -686,7 +717,7 @@ class SearchService { const errorMsg = `Embedding model changed. Your search index uses ${expectedDim}-dimension embeddings, ` + `but your current model produces ${vector.length}-dimension embeddings. ` + - `Please rebuild your search index to use the new model.`; + `Rebuild your search index to use the new model.`; logger.error('[SearchService] Embedding dimension mismatch - failing vector search', { collectionType, @@ -2217,7 +2248,10 @@ class SearchService { const processed = await queryProcessor.processQuery(query, { expandSynonyms, correctSpelling, - maxSynonymsPerWord: 3 + maxSynonymsPerWord: 3, + // Words the user's files contain are left alone; while no index + // is built this answers null and correction works as before. + isIndexedTerm: (/** @type {string} */ word) => this.isIndexedTerm(word) }); // Use corrected query for vector/chunk semantic retrieval. const correctedQuery = @@ -3355,7 +3389,7 @@ class SearchService { 'Failed to generate test embedding. The AI engine may be unavailable or the embedding model is missing.' }); diagnostics.recommendations.push( - 'Ensure the embedding model is downloaded and available in Settings > Models.' + 'Ensure the embedding model is downloaded and available in Settings → Model management.' ); } } catch (e) { @@ -3524,7 +3558,7 @@ class SearchService { message: `${label}: ${deadLetterCount} embeddings permanently failed (dead letter).` }); diagnostics.recommendations.push( - 'Review dead letter items in Settings > Advanced to identify recurring failures.' + 'Check Settings → Diagnostics → Troubleshooting logs to find what keeps failing.' ); } diff --git a/src/main/services/SettingsBackupService.js b/src/main/services/SettingsBackupService.js index fd004081..ea4a3d1b 100644 --- a/src/main/services/SettingsBackupService.js +++ b/src/main/services/SettingsBackupService.js @@ -58,7 +58,8 @@ class SettingsBackupService { * `settings-manual-.json`, never rotated, and goes only when the user * deletes it. Before the pools were separate, ten routine saves (a file * dialog counts as one) silently removed a backup the user had made. - * @param {{ kind?: 'automatic' | 'manual' }} [options] + * @param {{ kind?: 'automatic' | 'manual', keepPath?: string }} [options] keepPath + * is spared by the rotation (the backup a restore is reading from). * @returns {Promise<{success: boolean, path?: string, timestamp?: string, kind?: string, error?: string}>} */ async createBackup(options = {}) { @@ -112,7 +113,7 @@ class SettingsBackupService { } // Clean up old backups - await this.cleanupOldBackups(); + await this.cleanupOldBackups({ keepPath: options?.keepPath }); return { success: true, @@ -306,7 +307,9 @@ class SettingsBackupService { // known to go ahead: a rejected backup must not add a snapshot that // rotates out an older automatic backup. if (preRestoreBackup) { - await this.createBackup(); + // Spare the backup being restored: it can be the oldest automatic + // one, which this snapshot would otherwise rotate out. + await this.createBackup({ keepPath: backupPath }); } // Sanitize and merge with defaults @@ -355,8 +358,10 @@ class SettingsBackupService { /** * Rotate the automatic backups, keeping only the most recent ones. Manual * backups are never touched here. + * @param {{ keepPath?: string }} [options] */ - async cleanupOldBackups() { + async cleanupOldBackups(options = {}) { + const keepPath = options?.keepPath ? path.resolve(options.keepPath) : null; try { const cleanupStart = Date.now(); const backups = await this.listBackups({ includeParsedTime: true }); @@ -377,6 +382,13 @@ class SettingsBackupService { return true; }); + // The backup being restored still counts toward the limit but is never + // the one rotated out: move it to the front so the oldest other goes. + const keepIndex = keepPath + ? safeBackups.findIndex((backup) => path.resolve(backup.path) === keepPath) + : -1; + if (keepIndex > 0) safeBackups.unshift(...safeBackups.splice(keepIndex, 1)); + // Keep only the most recent backups if (safeBackups.length > this.maxBackups) { const backupsToDelete = safeBackups.slice(this.maxBackups); diff --git a/src/main/services/SettingsService.js b/src/main/services/SettingsService.js index b8989c67..01a87c33 100644 --- a/src/main/services/SettingsService.js +++ b/src/main/services/SettingsService.js @@ -512,9 +512,11 @@ class SettingsService { } this._isInternalChange = true; try { + // Same on-disk shape as save(): the user's values only, so + // recovery does not stamp every default into the file. const saveResult = await backupAndReplace( this.settingsPath, - JSON.stringify(merged, null, 2) + JSON.stringify(this._stripRuntimeDefaults(merged), null, 2) ); if (!saveResult.success) { throw new Error(saveResult.error || 'Failed to save restored settings'); @@ -1151,7 +1153,7 @@ class SettingsService { // Same on-disk shape as save(): the user's values only. const saveResult = await backupAndReplace( this.settingsPath, - JSON.stringify(stripDefaultValues(merged), null, 2) + JSON.stringify(this._stripRuntimeDefaults(merged), null, 2) ); if (!saveResult.success) { throw new Error(saveResult.error || 'Failed to save restored settings'); diff --git a/src/main/services/SmartFolderWatcher.js b/src/main/services/SmartFolderWatcher.js index 9c2931a5..29721697 100644 --- a/src/main/services/SmartFolderWatcher.js +++ b/src/main/services/SmartFolderWatcher.js @@ -315,6 +315,11 @@ class SmartFolderWatcher { this.isRunning = false; this.isStarting = false; this.watchedPaths = new Set(); + // Normalized folders unwatch() was called on in the current chokidar + // instance. It keeps ignoring their whole subtree, and add() lifts that + // only for the exact path added. + /** @type {Set} */ + this._unwatchedRoots = new Set(); this._startPromise = null; @@ -651,8 +656,7 @@ class SmartFolderWatcher { const folders = this.getSmartFolders(); if (!folders || folders.length === 0) { - const errorMsg = - 'No smart folders configured. Please add smart folders in the Setup phase first.'; + const errorMsg = 'No smart folders set up yet. Add them in the Setup phase first.'; logger.warn('[SMART-FOLDER-WATCHER] Start failed:', errorMsg); this._lastStartError = errorMsg; return false; @@ -661,7 +665,7 @@ class SmartFolderWatcher { const validPaths = await this._getValidFolderPaths(folders); if (validPaths.length === 0) { - const errorMsg = `All ${folders.length} smart folder path(s) are inaccessible. Please check that the folders exist and you have read permissions.`; + const errorMsg = `All ${folders.length} smart folder path(s) are inaccessible. Check that the folders exist and you have read permissions.`; logger.warn('[SMART-FOLDER-WATCHER] Start failed:', errorMsg); this._lastStartError = errorMsg; return false; @@ -702,9 +706,8 @@ class SmartFolderWatcher { } // Log the chosen depth so users debugging "my deeply-nested file - // wasn't picked up" can correlate without reading source. Default - // dropped from 10 → 5 in this release window; users with deeper - // trees can override via the STRATOSORT_WATCHER_DEPTH env var. + // wasn't picked up" can correlate without reading source. Users with + // deeper trees can override via the STRATOSORT_WATCHER_DEPTH env var. logger.info('[SMART-FOLDER-WATCHER] Watcher configuration', { depth: this.watcherDepth, polling: usePolling @@ -721,11 +724,9 @@ class SmartFolderWatcher { // does not increase for files we keep, and drops to zero for files we // reject up front. // - // `depth: 5` covers every real-world personal-document tree we've seen - // in user data (Downloads, Documents, Desktop trees top out at 3-4 - // nested folders). The old `depth: 10` was speculative and multiplied - // watch-handle cost on large folders without benefit. Users with - // deeper trees can adjust via the `watcherDepth` setting below. + // `depth` is `this.watcherDepth` (10 by default, STRATOSORT_WATCHER_DEPTH + // overrides it); _scanDirectory reads the same directories. + this._unwatchedRoots.clear(); this.watcher = chokidar.watch(validPaths, { ignoreInitial: true, // Don't process existing files on startup // The dotfile pattern already covers `.git/` directories; an @@ -1130,11 +1131,26 @@ class SmartFolderWatcher { } } + // Smart Folders can nest. chokidar's unwatch() closes and ignores the + // whole subtree, and add() lifts that ignore only for the exact path + // added, so a folder above or below one unwatched earlier, or a removed + // folder that contains a remaining one, needs a fresh chokidar instance. + let needsRestart = false; + const overlapsStrictly = (/** @type {string} */ a, /** @type {string} */ b) => + a !== b && (this._isUnderRoots(a, [b]) || this._isUnderRoots(b, [a])); + // Add paths present in desired but not already watched. let unparked = 0; for (const [key, addPath] of desiredByCanonical) { if (currentByCanonical.has(key)) continue; logger.info('[SMART-FOLDER-WATCHER] Adding watch path:', addPath); + const [addRoot] = this._normalizedRoots([addPath]); + if (addRoot) { + this._unwatchedRoots.delete(addRoot); + if ([...this._unwatchedRoots].some((root) => overlapsStrictly(root, addRoot))) { + needsRestart = true; + } + } this.watcher.add(addPath); this.watchedPaths.add(addPath); unparked += this._unparkItemsUnder(addPath); @@ -1157,18 +1173,45 @@ class SmartFolderWatcher { // smart folder, or the folder is simply unavailable right now. Only the // former should discard parked work — purging on a transient unmount is the // exact data loss parking exists to prevent. + // + // A folder a remaining root still covers is not unwatched at all. const configuredRoots = this._normalizedRoots( (Array.isArray(folders) ? folders : []).map((/** @type {any} */ f) => f?.path) ); + const desiredRoots = this._normalizedRoots(desiredByCanonical.values()); for (const [key, removePath] of currentByCanonical) { if (desiredByCanonical.has(key)) continue; + if (this._isUnderRoots(removePath, desiredRoots)) { + // Still inside a watched folder: its files keep their events and + // their queued work. + logger.info('[SMART-FOLDER-WATCHER] Removing watch path:', removePath, { + reason: 'covered-by-parent' + }); + this.watchedPaths.delete(removePath); + continue; + } const stillConfigured = this._isUnderRoots(removePath, configuredRoots); logger.info('[SMART-FOLDER-WATCHER] Removing watch path:', removePath, { reason: stillConfigured ? 'unavailable' : 'removed-from-config' }); - this.watcher.unwatch(removePath); + const [removeRoot] = this._normalizedRoots([removePath]); + if (removeRoot && desiredRoots.some((root) => overlapsStrictly(root, removeRoot))) { + needsRestart = true; + } else { + this.watcher.unwatch(removePath); + if (removeRoot) this._unwatchedRoots.add(removeRoot); + } this.watchedPaths.delete(removePath); - this._purgeQueuedWorkForRemovedFolder(removePath, { purgeParked: !stillConfigured }); + this._purgeQueuedWorkForRemovedFolder(removePath, { + purgeParked: !stillConfigured, + keepRoots: desiredRoots + }); + } + if (needsRestart) { + // stop() persists the queue it keeps (the purge above left the nested + // folders' work in it) and start() restores it. + logger.info('[SMART-FOLDER-WATCHER] Nested folder change needs a fresh watcher; restarting'); + await this.restart(); } } @@ -1206,18 +1249,22 @@ class SmartFolderWatcher { * for a folder that is no longer watched. Compares canonical forms so * `C:\Docs\` and `c:/docs` purge the same entries. * @param {string} removedFolderPath - * @param {{ purgeParked?: boolean }} [options] purgeParked drops the folder's - * parked backlog too. Only pass it when the folder left the smart-folder - * CONFIG — a folder that is merely unavailable keeps its parked work. + * @param {{ purgeParked?: boolean, keepRoots?: string[] }} [options] + * purgeParked drops the folder's parked backlog too. Only pass it when the + * folder left the smart-folder CONFIG — a folder that is merely + * unavailable keeps its parked work. keepRoots (normalized) spares work + * for files under a Smart Folder nested inside the removed one. * @private */ _purgeQueuedWorkForRemovedFolder(removedFolderPath, options = {}) { const normalizedFolder = normalizePathForIndex(removedFolderPath).replace(/\/+$/, ''); if (!normalizedFolder) return; const prefix = `${normalizedFolder}/`; + const keepRoots = Array.isArray(options.keepRoots) ? options.keepRoots : []; const matches = (/** @type {any} */ filePath) => { const p = normalizePathForIndex(filePath); - return p === normalizedFolder || p.startsWith(prefix); + if (p !== normalizedFolder && !p.startsWith(prefix)) return false; + return !this._isUnderRoots(filePath, keepRoots); }; for (const [key, pending] of this.pendingAnalysis.entries()) { @@ -2278,6 +2325,34 @@ class SmartFolderWatcher { } } + /** + * Whether the Smart Folder holding `filePath` can be reached, so a sweep + * may trust a "not found" for the file. A share or drive that dropped + * reports every file under it as not found, which must not wipe the + * analysis of files that still exist. A file under no Smart Folder has + * nothing to check. + * @private + * @param {string} filePath + * @param {Map} rootReachable per-sweep cache, keyed by + * folder path, so a sweep stats each Smart Folder once + * @returns {Promise} + */ + async _smartFolderReachableForSweep(filePath, rootReachable) { + const root = this._exclusionRootFor(filePath); + if (!root) return true; + let reachable = rootReachable.get(root); + if (reachable === undefined) { + try { + await fs.stat(root); + reachable = true; + } catch { + reachable = false; + } + rootReachable.set(root, reachable); + } + return reachable; + } + /** * Resolve the in-process AI service (overridable in tests). * @private @@ -4063,6 +4138,8 @@ class SmartFolderWatcher { const allEntries = Object.values(entries); let checked = 0; let removed = 0; + /** @type {Map} */ + const rootReachable = new Map(); for (const entry of allEntries) { if (this._isStopping) break; @@ -4076,9 +4153,12 @@ class SmartFolderWatcher { checked++; - try { - await fs.access(filePath); - } catch { + // Only a definite "not found" under a reachable folder is a deletion; + // EIO, EACCES or a dropped share leave the entry alone. + if ( + (await this._goneFromDisk(filePath)) && + (await this._smartFolderReachableForSweep(filePath, rootReachable)) + ) { // File no longer exists — clean up embeddings, chunks, and history try { await this._finalizeDeletion(filePath); @@ -4144,6 +4224,8 @@ class SmartFolderWatcher { return summary; } + /** @type {Map} */ + const rootReachable = new Map(); for (const doc of Array.isArray(docs) ? docs : []) { if (this._isStopping) break; const filePath = doc?.path; @@ -4151,16 +4233,16 @@ class SmartFolderWatcher { summary.skipped++; continue; } - let stillMissing = false; - try { - await fs.stat(filePath); - } catch { - stillMissing = true; - } - if (!stillMissing) { + if (!(await this._goneFromDisk(filePath))) { summary.kept++; continue; } + // Not found, but its Smart Folder is unreachable too: leave it for a + // sweep that can tell. + if (!(await this._smartFolderReachableForSweep(filePath, rootReachable))) { + summary.skipped++; + continue; + } try { await this._finalizeDeletion(filePath); summary.reaped++; @@ -4694,7 +4776,9 @@ class SmartFolderWatcher { * @returns {Promise} */ async _scanDirectory(/** @type {any} */ dirPath, maxDepth = this.watcherDepth, currentDepth = 0) { - if (currentDepth >= maxDepth) return []; + // chokidar reads a directory whose depth is <= `depth` (the root is 0), + // so depth 0 still sees the root's own files. + if (currentDepth > maxDepth) return []; /** @type {any[]} */ const files = []; diff --git a/src/main/services/TrashService.js b/src/main/services/TrashService.js index d24a98d3..46c34240 100644 --- a/src/main/services/TrashService.js +++ b/src/main/services/TrashService.js @@ -18,6 +18,32 @@ const { mapWithConcurrency } = require('../../shared/promiseUtils'); const logger = createLogger('TrashService'); +const NETWORK_NO_RECYCLE_BIN_MESSAGE = + 'This file is on a network drive, where Windows has no Recycle Bin, so it was left in place instead of being deleted permanently.'; + +/** + * True on Windows when the file lives on a network share (\\server\share) or + * a drive letter mapped to one. Windows keeps no Recycle Bin there, so a + * trash request must not reach shell.trashItem. realpath is the native + * resolver, which returns a mapped drive's \\server\share path. + * @param {string} safePath + * @returns {Promise} + */ +async function isWindowsNetworkPath(safePath) { + if (process.platform !== 'win32') return false; + let real = safePath; + try { + real = await fs.realpath(safePath); + } catch { + // Judge the path as given. + } + const text = String(real); + if (/^\\\\\?\\UNC\\/i.test(text)) return true; + // Two leading separators then a server name; \\?\ and \\.\ are local + // device namespaces, not shares. + return /^[\\/]{2}[^\\/?.]/.test(text); +} + class TrashService { /** @param {{ maxBatchSize?: number, concurrency?: number }} [options] */ constructor(options = {}) { @@ -63,6 +89,14 @@ class TrashService { code: 'NOT_FOUND' }; } + if (await isWindowsNetworkPath(safePath)) { + return { + success: false, + path: safePath, + error: NETWORK_NO_RECYCLE_BIN_MESSAGE, + code: 'NETWORK_NO_RECYCLE_BIN' + }; + } try { await shell.trashItem(safePath); return { success: true, path: safePath }; @@ -157,4 +191,4 @@ class TrashService { } } -module.exports = { TrashService }; +module.exports = { TrashService, isWindowsNetworkPath, NETWORK_NO_RECYCLE_BIN_MESSAGE }; diff --git a/src/main/services/WhisperService.js b/src/main/services/WhisperService.js index 74278b39..34d55e6a 100644 --- a/src/main/services/WhisperService.js +++ b/src/main/services/WhisperService.js @@ -200,7 +200,7 @@ function _ffmpegUnavailableMessage() { return `${base} Install ffmpeg via your package manager (e.g. \`apt install ffmpeg\` or \`dnf install ffmpeg\`). ${escapeHatch}`; } if (process.platform === 'darwin') { - return `${base} The bundled binary should ship with the app — reinstall, or install ffmpeg via Homebrew (\`brew install ffmpeg\`). ${escapeHatch}`; + return `${base} The app should include ffmpeg. Reinstall it, or install ffmpeg via Homebrew (\`brew install ffmpeg\`). ${escapeHatch}`; } return `${base} Run 'npm run setup:ffmpeg-runtime' (dev), reinstall the app, or install ffmpeg manually. ${escapeHatch}`; } diff --git a/src/main/services/ai/AIRouter.js b/src/main/services/ai/AIRouter.js index 02a3de3f..51c172f1 100644 --- a/src/main/services/ai/AIRouter.js +++ b/src/main/services/ai/AIRouter.js @@ -119,7 +119,7 @@ class AIRouter extends EventEmitter { // 5. Nothing works throw new AIProviderError( 'NO_PROVIDER_READY', - 'No AI provider is ready. Configure a provider in Settings → AI.' + 'No AI provider is ready. Configure a provider in Settings → AI Provider.' ); } @@ -132,7 +132,7 @@ class AIRouter extends EventEmitter { } throw new AIProviderError( 'NO_PROVIDER_READY', - 'No AI provider is ready. Configure a provider in Settings → AI.' + 'No AI provider is ready. Configure a provider in Settings → AI Provider.' ); } @@ -168,7 +168,7 @@ class AIRouter extends EventEmitter { if (!name) return 'no provider name configured'; if (!provider) return `provider "${name}" is not registered`; const cfg = settings.providers?.[name]; - if (!cfg?.enabled) return `provider "${name}" is disabled in Settings → AI`; + if (!cfg?.enabled) return `provider "${name}" is disabled in Settings → AI Provider`; if (!provider.ready()) { // The most common ready()=false cause for cloud providers is a missing // API key; surface that distinctly. diff --git a/src/main/services/analysisHistory/indexManager.js b/src/main/services/analysisHistory/indexManager.js index 37d3e40a..6892888f 100644 --- a/src/main/services/analysisHistory/indexManager.js +++ b/src/main/services/analysisHistory/indexManager.js @@ -102,6 +102,37 @@ function deletePathKeysOwnedBy(index, filePath, entryId) { } } +/** + * The id list stored under `key`, or null. Tag and category keys come from + * model keywords and user tags, so a key such as "constructor" or "toString" + * must not resolve to an inherited Object.prototype member: the index maps are + * plain objects (from JSON.parse or createEmptyIndex). + * @param {Record} map - Keyed index (tagIndex, categoryIndex) + * @param {string} key - Bucket key + * @returns {Array|null} + */ +function getIndexBucket(map, key) { + if (!map || !Object.prototype.hasOwnProperty.call(map, key)) return null; + const bucket = map[key]; + return Array.isArray(bucket) ? bucket : null; +} + +/** + * Store an id list under `key` as an own property. A plain assignment of + * "__proto__" would replace the map's prototype instead of adding a key. + * @param {Record} map - Keyed index + * @param {string} key - Bucket key + * @param {Array} bucket - Id list + */ +function setIndexBucket(map, key, bucket) { + Object.defineProperty(map, key, { + value: bucket, + writable: true, + enumerable: true, + configurable: true + }); +} + /** * Update all indexes for a new entry * @param {Record} index - Analysis index @@ -138,19 +169,18 @@ function updateIndexes(index, entry) { // Tag index if (entry.analysis.tags) { entry.analysis.tags.forEach((/** @type {any} */ tag) => { - if (!index.tagIndex[tag]) { - index.tagIndex[tag] = []; - } - index.tagIndex[tag].push(entry.id); + const bucket = getIndexBucket(index.tagIndex, tag); + if (bucket) bucket.push(entry.id); + else setIndexBucket(index.tagIndex, tag, [entry.id]); }); } // Category index if (entry.analysis.category) { - if (!index.categoryIndex[entry.analysis.category]) { - index.categoryIndex[entry.analysis.category] = []; - } - index.categoryIndex[entry.analysis.category].push(entry.id); + const category = entry.analysis.category; + const bucket = getIndexBucket(index.categoryIndex, category); + if (bucket) bucket.push(entry.id); + else setIndexBucket(index.categoryIndex, category, [entry.id]); } // Date index (by month) @@ -188,22 +218,22 @@ function removeFromIndexes(index, entry) { // Remove from tag index if (entry.analysis.tags) { entry.analysis.tags.forEach((/** @type {any} */ tag) => { - const tagEntries = index.tagIndex[tag] || []; - index.tagIndex[tag] = tagEntries.filter((/** @type {any} */ id) => id !== entry.id); - if (index.tagIndex[tag].length === 0) { - delete index.tagIndex[tag]; - } + const tagEntries = getIndexBucket(index.tagIndex, tag); + if (!tagEntries) return; + const remaining = tagEntries.filter((/** @type {any} */ id) => id !== entry.id); + if (remaining.length === 0) delete index.tagIndex[tag]; + else setIndexBucket(index.tagIndex, tag, remaining); }); } // Remove from category index if (entry.analysis.category) { - const categoryEntries = index.categoryIndex[entry.analysis.category] || []; - index.categoryIndex[entry.analysis.category] = categoryEntries.filter( - (/** @type {any} */ id) => id !== entry.id - ); - if (index.categoryIndex[entry.analysis.category].length === 0) { - delete index.categoryIndex[entry.analysis.category]; + const category = entry.analysis.category; + const categoryEntries = getIndexBucket(index.categoryIndex, category); + if (categoryEntries) { + const remaining = categoryEntries.filter((/** @type {any} */ id) => id !== entry.id); + if (remaining.length === 0) delete index.categoryIndex[category]; + else setIndexBucket(index.categoryIndex, category, remaining); } } @@ -270,6 +300,7 @@ module.exports = { createEmptyIndex, generateFileHash, getSizeRange, + getIndexBucket, updateIndexes, removeFromIndexes, updatePathIndexForMove, diff --git a/src/main/services/analysisHistory/queries.js b/src/main/services/analysisHistory/queries.js index c21fb0ba..f3106e08 100644 --- a/src/main/services/analysisHistory/queries.js +++ b/src/main/services/analysisHistory/queries.js @@ -9,6 +9,7 @@ */ const { maintainCacheSize } = require('./cacheManager'); +const { getIndexBucket } = require('./indexManager'); const { isPlausibleContentDate, isIsoDateOnly, @@ -168,7 +169,7 @@ function getAnalysisByCategory( cache.categoryResults.delete(cacheKey); } - const entryIds = analysisIndex.categoryIndex[category] || []; + const entryIds = getIndexBucket(analysisIndex.categoryIndex, category) || []; let entries = entryIds .map((/** @type {any} */ id) => analysisHistory.entries[id]) .filter(Boolean); @@ -221,7 +222,7 @@ function getAnalysisByTag(analysisHistory, analysisIndex, cache, cacheTTL, tag, cache.tagResults.delete(cacheKey); } - const entryIds = analysisIndex.tagIndex[tag] || []; + const entryIds = getIndexBucket(analysisIndex.tagIndex, tag) || []; let entries = entryIds .map((/** @type {any} */ id) => analysisHistory.entries[id]) .filter(Boolean); diff --git a/src/main/services/chat/RetrievalEngine.js b/src/main/services/chat/RetrievalEngine.js index 9fcce500..7be0c45d 100644 --- a/src/main/services/chat/RetrievalEngine.js +++ b/src/main/services/chat/RetrievalEngine.js @@ -459,9 +459,16 @@ class RetrievalEngine { ) { try { if (!this.embeddingService || !this.vectorDbService) return []; - const cleanIds = fileIds - .filter((/** @type {any} */ id) => typeof id === 'string' && id.length > 0) - .slice(0, this.DEFAULTS.contextFileLimit); + const validIds = fileIds.filter( + (/** @type {any} */ id) => typeof id === 'string' && id.length > 0 + ); + const cleanIds = validIds.slice(0, this.DEFAULTS.contextFileLimit); + if (cleanIds.length < validIds.length) { + logger.warn('[RetrievalEngine] Context files over the limit were not scored', { + requested: validIds.length, + limit: this.DEFAULTS.contextFileLimit + }); + } if (cleanIds.length === 0) return []; diff --git a/src/main/services/migration/DataMigrationService.js b/src/main/services/migration/DataMigrationService.js index 45708078..668af65c 100644 --- a/src/main/services/migration/DataMigrationService.js +++ b/src/main/services/migration/DataMigrationService.js @@ -44,6 +44,35 @@ class DataMigrationService { this._userDataPath = null; } + /** + * Legacy names still in use as the live vector folder. vectorDbPersistPath + * accepts any folder name, so a hand-edited 'vector_storage' would + * otherwise be wiped (and fully re-embedded) on every launch. + * @returns {Promise>} + */ + async _getInUseLegacyNames() { + const inUse = new Set(); + try { + const SettingsService = require('../SettingsService'); + const settings = await SettingsService.getInstance?.()?.load?.(); + const raw = settings?.vectorDbPersistPath; + if (typeof raw === 'string' && raw.trim()) { + const configured = raw + .trim() + .replace(/[\\/]+$/, '') + .toLowerCase(); + for (const name of LEGACY_PATHS) { + if (name.toLowerCase() === configured) inUse.add(name); + } + } + } catch (error) { + logger.debug('[DataMigrationService] Could not read vectorDbPersistPath', { + error: error?.message + }); + } + return inUse; + } + async _getUserDataPath() { if (this._userDataPath) return this._userDataPath; // Handle both main process and renderer/test environments if needed @@ -63,8 +92,10 @@ class DataMigrationService { async needsMigration() { try { const userData = await this._getUserDataPath(); + const inUse = await this._getInUseLegacyNames(); for (const legacyName of LEGACY_PATHS) { + if (inUse.has(legacyName)) continue; const legacyPath = path.join(userData, legacyName); try { await fs.access(legacyPath); @@ -117,6 +148,7 @@ class DataMigrationService { try { const userData = await this._getUserDataPath(); + const inUse = await this._getInUseLegacyNames(); let foundLegacy = false; const removedPaths = []; @@ -127,6 +159,10 @@ class DataMigrationService { for (const legacyName of LEGACY_PATHS) { const legacyPath = path.join(userData, legacyName); currentStep++; + if (inUse.has(legacyName)) { + logger.info(`[DataMigrationService] Keeping ${legacyName}: it is the live vector folder`); + continue; + } const progressPercent = Math.round((currentStep / totalSteps) * 100); try { diff --git a/src/main/services/organization/OrganizationSuggestionServiceCore.js b/src/main/services/organization/OrganizationSuggestionServiceCore.js index ba41d0f8..5390387f 100644 --- a/src/main/services/organization/OrganizationSuggestionServiceCore.js +++ b/src/main/services/organization/OrganizationSuggestionServiceCore.js @@ -1580,7 +1580,45 @@ class OrganizationSuggestionServiceCore { /** @type {any} */ accepted, /** @type {any} */ note ) { - // This prevents overwriting patterns that haven't been loaded yet + await this._ensurePatternsWritable(); + + this.patternMatcher.recordFeedback(file, suggestion, accepted); + if (accepted) { + await this._maybeStoreAutoFeedbackReason(file, suggestion, note); + } + if (suggestion?.note || suggestion?.feedbackNote) { + logger.debug( + '[OrganizationSuggestionService] Suggestion contains note field; use recordFeedbackNote.' + ); + } + return this._savePatterns(); + } + + /** + * Learn a file the startup scan found already in a smart folder. Counts as + * an accepted placement for the pattern, but writes no feedback-history + * row (that capped list is what undo retracts organize feedback from); + * `learnKey` is remembered so later scans skip the file. + * @param {any} file + * @param {any} suggestion + * @param {string} learnKey + * @returns {Promise} false when the file was already learned + */ + async recordScanFeedback(file, suggestion, learnKey) { + await this._ensurePatternsWritable(); + if (!this.patternMatcher.recordScanLearning(file, suggestion, learnKey)) return false; + await this._savePatterns(); + return true; + } + + /** + * Wait for the saved patterns; when they failed to load, back the file up + * and start empty so feedback can still be recorded. Writing before the + * load finished would overwrite patterns that were never read. + * @returns {Promise} + * @private + */ + async _ensurePatternsWritable() { await this._ensurePatternsLoaded(); if (!this._patternsLoadedSuccessfully) { @@ -1604,17 +1642,6 @@ class OrganizationSuggestionServiceCore { ); } } - - this.patternMatcher.recordFeedback(file, suggestion, accepted); - if (accepted) { - await this._maybeStoreAutoFeedbackReason(file, suggestion, note); - } - if (suggestion?.note || suggestion?.feedbackNote) { - logger.debug( - '[OrganizationSuggestionService] Suggestion contains note field; use recordFeedbackNote.' - ); - } - return this._savePatterns(); } async _maybeStoreAutoFeedbackReason( diff --git a/src/main/services/organization/learningFeedback.js b/src/main/services/organization/learningFeedback.js index 680b1379..1a9a4101 100644 --- a/src/main/services/organization/learningFeedback.js +++ b/src/main/services/organization/learningFeedback.js @@ -69,6 +69,52 @@ function isCatchAllFolder(folder) { ); } +/** + * Turn an analysis-history entry into the flat analysis buildFileMetadata + * reads. The entry nests category and tags under `entry.analysis`; passing + * the entry itself learned every file as `:unknown:`. + * Fallback and provisional results return null so their guesses are not + * learned (the same guard SmartFolderWatcher applies before learning). + * @param {Record|null|undefined} entry + * @returns {Record|null} + */ +function toLearnableAnalysis(entry) { + if (!entry || typeof entry !== 'object') return null; + const nested = entry.analysis && typeof entry.analysis === 'object' ? entry.analysis : null; + const analysis = nested || entry; + const extractionMethod = String(analysis.extractionMethod || '').toLowerCase(); + if ( + analysis._isFallback || + analysis.isFallback || + analysis.provisional === true || + analysis.fallbackReason || + extractionMethod === 'pdf_no_text' || + extractionMethod === 'failed' || + extractionMethod === 'filename' || + extractionMethod.includes('fallback') + ) { + return null; + } + if (!nested) return entry; + return { + ...nested, + keywords: nested.keywords || nested.tags || [], + smartFolder: nested.smartFolder || entry.organization?.smartFolder || null + }; +} + +/** + * Key naming a file in a folder, the same name + case-insensitive folder + * pairing retractFeedback uses. The startup scan stores these + * (patternMatcher.scanLearnedKeys) and derives them from feedback-history rows. + * @param {string} fileName + * @param {string} folder + * @returns {string} + */ +function scanLearnKey(fileName, folder) { + return `${fileName}\u0000${String(folder || '').toLowerCase()}`; +} + /** * Build a file metadata object for pattern learning * @param {string} filePath - Full path to the file @@ -260,8 +306,22 @@ class LearningFeedbackService { return false; } - // Record as positive feedback - user chose this folder for this file type - await this.suggestionService.recordFeedback(fileMetadata, suggestion, true); + if ( + source === FEEDBACK_SOURCES.STARTUP_SCAN && + typeof this.suggestionService.recordScanFeedback === 'function' + ) { + // A file found in place is learned without a feedback-history row: + // that list is capped, and undo retracts real organize feedback from it. + const learnedNow = await this.suggestionService.recordScanFeedback( + fileMetadata, + suggestion, + scanLearnKey(fileMetadata.name, suggestion.folder) + ); + if (!learnedNow) return false; + } else { + // Record as positive feedback - user chose this folder for this file type + await this.suggestionService.recordFeedback(fileMetadata, suggestion, true); + } // Update folder usage stats via pattern matcher if (this.suggestionService.patternMatcher) { @@ -496,6 +556,18 @@ class LearningFeedbackService { let scanned = 0; let learned = 0; + // The scan runs on every launch and the in-memory dedupe starts empty, so + // the persisted state says a file was already learned: the keys earlier + // scans stored, and accepted organize rows in the feedback history. + const matcher = await this._loadPatternMatcherForScan(); + /** @type {Set} */ + const alreadyLearned = new Set(matcher?.scanLearnedKeys || []); + for (const row of Array.isArray(matcher?.feedbackHistory) ? matcher.feedbackHistory : []) { + if (row?.accepted === true && row?.file?.name && row?.suggestion?.folder) { + alreadyLearned.add(scanLearnKey(row.file.name, row.suggestion.folder)); + } + } + logger.info('[LearningFeedback] Starting learning scan', { folderCount: smartFolders.length, maxFilesPerFolder @@ -506,15 +578,21 @@ class LearningFeedbackService { try { const files = await this._scanFolderFiles(folder.path, maxFilesPerFolder); + const folderLabel = buildFolderSuggestion(folder)?.folder || ''; for (const filePath of files) { scanned++; + const learnKey = scanLearnKey(path.basename(filePath), folderLabel); + if (alreadyLearned.has(learnKey)) continue; + // Get existing analysis if available let analysis = null; if (analysisHistoryService) { try { - analysis = await analysisHistoryService.getAnalysisByPath(filePath); + analysis = toLearnableAnalysis( + await analysisHistoryService.getAnalysisByPath(filePath) + ); } catch { // Continue without analysis } @@ -535,6 +613,7 @@ class LearningFeedbackService { if (success) { learned++; + alreadyLearned.add(learnKey); } } } catch (error) { @@ -549,6 +628,22 @@ class LearningFeedbackService { return { scanned, learned }; } + /** + * The pattern matcher with its persisted patterns loaded, or null. + * @returns {Promise} + * @private + */ + async _loadPatternMatcherForScan() { + try { + await this.suggestionService?._ensurePatternsLoaded?.(); + } catch (error) { + logger.debug('[LearningFeedback] Patterns not loaded before scan', { + error: error.message + }); + } + return this.suggestionService?.patternMatcher || null; + } + /** * Scan folder for files (non-recursive, limited) * @private diff --git a/src/main/services/organization/patternMatcher.js b/src/main/services/organization/patternMatcher.js index 376311f7..c7a1b449 100644 --- a/src/main/services/organization/patternMatcher.js +++ b/src/main/services/organization/patternMatcher.js @@ -51,6 +51,13 @@ class PatternMatcher { this.maxMemoryMB = config.maxMemoryMB || 50; this.patternSimilarityThreshold = config.patternSimilarityThreshold || 0.5; this.maxFeedbackHistory = config.maxFeedbackHistory || 1000; + // Files the startup scan already learned, as file name + folder keys + // (learningFeedback scanLearnKey; insertion order = age). Kept apart from feedbackHistory so scan + // learning neither fills the capped history undo retracts from nor stops + // once it is full. + /** @type {Set} */ + this.scanLearnedKeys = new Set(); + this.maxScanLearnedKeys = config.maxScanLearnedKeys || 5000; this.memoryCheckInterval = 100; this.patternCount = 0; } @@ -83,6 +90,14 @@ class PatternMatcher { this.feedbackHistory = stored.feedbackHistory; } + if (Array.isArray(stored.scanLearnedKeys)) { + this.scanLearnedKeys = new Set( + stored.scanLearnedKeys + .filter((/** @type {any} */ key) => typeof key === 'string') + .slice(-this.maxScanLearnedKeys) + ); + } + if (stored.folderUsageStats && Array.isArray(stored.folderUsageStats)) { try { this.folderUsageStats = new Map(stored.folderUsageStats); @@ -104,6 +119,7 @@ class PatternMatcher { return { patterns: Array.from(this.userPatterns.entries()), feedbackHistory: this.feedbackHistory.slice(-this.maxFeedbackHistory), + scanLearnedKeys: Array.from(this.scanLearnedKeys).slice(-this.maxScanLearnedKeys), folderUsageStats: Array.from(this.folderUsageStats.entries()) }; } @@ -187,6 +203,29 @@ class PatternMatcher { } } + /** + * Learn a file the startup scan found already sitting in a smart folder. + * The pattern counts as an accepted placement, but no feedback-history row + * is written: the history is capped and is what undo retracts organize + * feedback from. `learnKey` is remembered instead (newest + * maxScanLearnedKeys kept) so a later scan skips the file. + * @param {Record} file - Flat file metadata + * @param {Record} suggestion - Folder the file is in + * @param {string} learnKey - Key naming the file + folder + * @returns {boolean} false when the key was already learned + */ + recordScanLearning(file, suggestion, learnKey) { + if (!suggestion || !learnKey || this.scanLearnedKeys.has(learnKey)) return false; + this._updatePattern(this.extractPattern(file, suggestion), suggestion, Date.now()); + this.scanLearnedKeys.add(learnKey); + while (this.scanLearnedKeys.size > this.maxScanLearnedKeys) { + const oldest = this.scanLearnedKeys.values().next().value; + if (oldest === undefined) break; + this.scanLearnedKeys.delete(oldest); + } + return true; + } + /** * Update pattern from accepted suggestion * @private diff --git a/src/main/services/organization/persistence.js b/src/main/services/organization/persistence.js index d5fd317d..a88dc685 100644 --- a/src/main/services/organization/persistence.js +++ b/src/main/services/organization/persistence.js @@ -67,6 +67,8 @@ const feedbackEntrySchema = z.object({ const patternsSchema = z.object({ patterns: z.array(z.tuple([z.string(), patternEntrySchema])).optional(), feedbackHistory: z.array(feedbackEntrySchema).optional(), + // Files the startup scan learned; absent in files saved before it existed. + scanLearnedKeys: z.array(z.string()).optional(), folderUsageStats: z .array( z.tuple([ @@ -156,6 +158,7 @@ class PatternPersistence { return { patterns: validated.data.patterns || [], feedbackHistory: validated.data.feedbackHistory || [], + scanLearnedKeys: validated.data.scanLearnedKeys || [], folderUsageStats: validated.data.folderUsageStats || [], lastUpdated: validated.data.lastUpdated }; diff --git a/src/main/services/triage/DownloadsTriageService.js b/src/main/services/triage/DownloadsTriageService.js index 9c50e983..2fd44a3d 100644 --- a/src/main/services/triage/DownloadsTriageService.js +++ b/src/main/services/triage/DownloadsTriageService.js @@ -35,10 +35,9 @@ const { DEFAULT_EXCLUSION_GLOBS, sanitizeExclusionGlobs } = require('../../../shared/config/exclusionRulesSchema'); -const { THRESHOLDS, TIMEOUTS } = require('../../../shared/performanceConstants'); +const { THRESHOLDS } = require('../../../shared/performanceConstants'); const { ACTION_TYPES } = require('../../../shared/constants'); const { validateFileOperationPath } = require('../../../shared/pathSanitization'); -const { withTimeout } = require('../../../shared/promiseUtils'); const { isCaseInsensitiveFileSystem } = require('../../../shared/crossPlatformUtils'); const logger = createLogger('DownloadsTriageService'); @@ -862,7 +861,7 @@ class DownloadsTriageService { confidence: score, suggestedFolder: { id: top.id, name: top.name, path: top.path }, signal: 'deep-embedding', - why: `Possible match "${top.name || top.id}" (${Math.round(score * 100)}%) — review` + why: `Possible match "${top.name || top.id}" (${Math.round(score * 100)}%). Needs review.` }; } else { next = { @@ -1088,7 +1087,7 @@ class DownloadsTriageService { })); try { await undo.recordBatch(ACTION_TYPES.TRASH_BATCH, operations, { - summary: summary || { label: 'Downloads triage — Send to Trash' } + summary: summary || { label: 'Downloads triage: Send to Trash' } }); } catch (err) { undoUnavailable = true; @@ -1175,6 +1174,12 @@ class DownloadsTriageService { const failed = []; let totalBytes = 0; const undoOps = []; + // Real moves only. A dedup skip lands in `moved` for the renderer, + // but its source was removed, not moved, so it is no path change. + /** @type {Array<{ oldPath: string, newPath: string }>} */ + const pathChanges = []; + /** @type {string[]} */ + const dedupRemovedSources = []; // Fail closed on destination containment: suggestions only ever come // from this session's Smart Folder records, so a destination outside @@ -1265,23 +1270,32 @@ class DownloadsTriageService { await fs.mkdir(path.dirname(op.destination), { recursive: true }); - const moveResult = await withTimeout( - performFileMove(op, logger, computeFileChecksum, null, pendingSources), - TIMEOUTS.FILE_COPY || 120000, - `Triage organize ${path.basename(op.source)}` + // The move is awaited to its end, never raced against a timer: a + // timer cannot stop a copy, so a large cross-drive move would be + // reported failed while the file went on to move with no undo + // entry or path update. Triage has no Stop, so there is no abort + // signal to pass here. + const moveResult = await performFileMove( + op, + logger, + computeFileChecksum, + null, + pendingSources ); const finalDest = moveResult?.destination || op.destination; const size = Number(f.size) || 0; totalBytes += size; moved.push({ path: op.source, destination: finalDest, size }); - if (moveResult?.skipped) { + if (moveResult && 'skipped' in moveResult && moveResult.skipped) { // Dedup-skip: an identical file already sat at the destination, // so the SOURCE was removed and `finalDest` is the surviving // sibling this operation never touched. Recording a move undo // here made "undo" relocate that untouched sibling out of its // home. The source content is not restorable (it was // deduplicated away), so no undo op is recorded for this file. + dedupRemovedSources.push(op.source); } else { + pathChanges.push({ oldPath: op.source, newPath: finalDest }); undoOps.push({ type: 'move', originalPath: op.source, @@ -1308,7 +1322,7 @@ class DownloadsTriageService { if (undo && typeof undo.recordBatch === 'function') { try { await undo.recordBatch(ACTION_TYPES.MOVE_BATCH, undoOps, { - summary: summary || { label: 'Downloads triage — Organize' } + summary: summary || { label: 'Downloads triage: Organize' } }); } catch (err) { moveUndoUnavailable = true; @@ -1323,8 +1337,11 @@ class DownloadsTriageService { } const coordinator = tryResolveFilePathCoordinator(); - if (coordinator && typeof coordinator.batchPathUpdate === 'function') { - const pathChanges = moved.map((m) => ({ oldPath: m.path, newPath: m.destination })); + if ( + pathChanges.length > 0 && + coordinator && + typeof coordinator.batchPathUpdate === 'function' + ) { try { await coordinator.batchPathUpdate(pathChanges, { type: 'move' }); } catch (err) { @@ -1335,6 +1352,26 @@ class DownloadsTriageService { } } + // A dedup-skipped source is gone, so it is a deletion for the indexes. + // Re-pointing it at the kept sibling (as a move) made the sibling's + // path resolve to the removed file's history entry. This runs outside + // the undo guard because an all-skip request records no undo ops. + if (dedupRemovedSources.length > 0) { + const coordinator = tryResolveFilePathCoordinator(); + for (const source of dedupRemovedSources) { + try { + if (coordinator && typeof coordinator.handleFileDeletion === 'function') { + await coordinator.handleFileDeletion(source); + } + } catch (err) { + logger.debug('[DownloadsTriageService] dedup source index cleanup failed', { + path: source, + error: err?.message + }); + } + } + } + const movedKeys = new Set(moved.map((m) => triagePathKey(m.path)).filter(Boolean)); stripSessionPaths(session, movedKeys); diff --git a/src/main/services/triage/FastTriageClassifier.js b/src/main/services/triage/FastTriageClassifier.js index 7f448e85..d0ef4a62 100644 --- a/src/main/services/triage/FastTriageClassifier.js +++ b/src/main/services/triage/FastTriageClassifier.js @@ -195,7 +195,7 @@ class FastTriageClassifier { bucket: BUCKET.EXCLUDED, signal: SIGNAL.TOO_RECENT, confidence: 1, - why: `Too recent (${hoursOld}h old — wait before triaging)` + why: `Too recent (${hoursOld}h old). Wait before triaging.` }; } @@ -256,7 +256,7 @@ class FastTriageClassifier { signal: SIGNAL.CACHED_EMBEDDING, confidence: score, suggestedFolder: { id: top.id, name: top.name, path: top.path }, - why: `Cached embedding suggests "${top.name || top.id}" (${confidencePercent(score)}%) — below confidence threshold` + why: `Cached embedding suggests "${top.name || top.id}" (${confidencePercent(score)}%), below the confidence threshold` }; } } @@ -291,7 +291,7 @@ class FastTriageClassifier { signal: SIGNAL.LEXICAL_MATCH, confidence: top.score, suggestedFolder: { id: top.id, name: top.name, path: top.path }, - why: `Weak keyword match to "${top.name}" — needs deeper scan to confirm` + why: `Weak keyword match to "${top.name}". Needs a deeper scan to confirm.` }; } } catch (err) { diff --git a/src/main/simple-main.js b/src/main/simple-main.js index 0c8cbfeb..c1f909ee 100644 --- a/src/main/simple-main.js +++ b/src/main/simple-main.js @@ -408,6 +408,14 @@ const createMainWindow = require('./core/createWindow'); /** @type {Promise | null} */ let _windowCreationPromise = null; +/** + * Startup has registered IPC and reached its own window creation. Until then + * a second launch must not create the window: the renderer would start + * invoking before any handler exists. + */ +let _startupWindowCreationReached = false; +/** A second launch arrived before startup created the window. */ +let _windowRequestedDuringStartup = false; /** @type {any} Window whose restore waits for its first 'show'. */ let _restoreQueuedForWindow = null; @@ -442,6 +450,21 @@ function restoreMainWindow(win, source) { restore(); } +/** + * Startup's own window creation (normal and degraded paths). Runs after IPC + * registration and serves any launch that arrived before it. + */ +function createStartupWindow() { + _startupWindowCreationReached = true; + const creation = createWindow(); + if (_windowRequestedDuringStartup && mainWindow && !mainWindow.isDestroyed()) { + _windowRequestedDuringStartup = false; + // Queued for the window's first show, so the relaunch still brings it forward. + restoreMainWindow(mainWindow, 'SECOND-INSTANCE'); + } + return creation; +} + function createWindow() { if (_shutdownState.shouldClose) { logger.warn('[WINDOW] Ignoring createWindow() during shutdown'); @@ -469,61 +492,61 @@ function createWindow() { return Promise.resolve(); } - // runs synchronously and may set _windowCreationPromise = null before the return statement + // Take the mutex before any creation work runs, so a synchronous failure's + // release() cannot be followed by an assignment that leaves the mutex on a + // settled promise (every later createWindow() would then return it and + // never open a window). + /** @type {() => void} */ + let resolveCreation = () => {}; + /** @type {Promise} */ const promise = new Promise((resolve) => { - try { - // Double-check after acquiring mutex (another call might have created window) - if (mainWindow && !mainWindow.isDestroyed()) { - logger.debug('[WINDOW] Window created by concurrent call, using existing'); - _windowCreationPromise = null; - resolve(); - return; - } + resolveCreation = resolve; + }); + _windowCreationPromise = promise; + const release = () => { + if (_windowCreationPromise === promise) _windowCreationPromise = null; + resolveCreation(); + }; + + try { + _createWindowInternal(); - _createWindowInternal(); + // Wait for window to be ready before releasing mutex + if (mainWindow) { + /** @type {any} */ + let creationTimeout; - // Wait for window to be ready before releasing mutex - if (mainWindow) { - /** @type {any} */ - let creationTimeout; + const onReadyToShow = () => { + logger.debug('[WINDOW] Window ready, releasing creation mutex'); + if (creationTimeout) clearTrackedTimer(creationTimeout); + release(); + }; - const onReadyToShow = () => { - logger.debug('[WINDOW] Window ready, releasing creation mutex'); - if (creationTimeout) clearTrackedTimer(creationTimeout); - _windowCreationPromise = null; - resolve(); - }; + mainWindow.once('ready-to-show', onReadyToShow); - mainWindow.once('ready-to-show', onReadyToShow); - - creationTimeout = trackTimeout(() => { - if (_windowCreationPromise) { - logger.warn('[WINDOW] Window creation timeout, releasing mutex'); - // Remove the listener to avoid memory leak or late execution - if (mainWindow && !mainWindow.isDestroyed()) { - mainWindow.removeListener('ready-to-show', onReadyToShow); - // Last line of defence after createWindow.js's own show fallback: - // a window that is still hidden after a minute must not stay so. - if (!mainWindow.isVisible()) { - logger.warn('[WINDOW] Force-showing window after creation timeout'); - mainWindow.show(); - } + creationTimeout = trackTimeout(() => { + if (_windowCreationPromise === promise) { + logger.warn('[WINDOW] Window creation timeout, releasing mutex'); + // Remove the listener to avoid memory leak or late execution + if (mainWindow && !mainWindow.isDestroyed()) { + mainWindow.removeListener('ready-to-show', onReadyToShow); + // Last line of defence after createWindow.js's own show fallback: + // a window that is still hidden after a minute must not stay so. + if (!mainWindow.isVisible()) { + logger.warn('[WINDOW] Force-showing window after creation timeout'); + mainWindow.show(); } - _windowCreationPromise = null; - resolve(); } - }, TIMEOUTS.WINDOW_CREATION_READY); - } else { - _windowCreationPromise = null; - resolve(); - } - } catch (error) { - logger.error('[WINDOW] Error creating window:', error); - _windowCreationPromise = null; - resolve(); + release(); + } + }, TIMEOUTS.WINDOW_CREATION_READY); + } else { + release(); } - }); - _windowCreationPromise = promise; + } catch (error) { + logger.error('[WINDOW] Error creating window:', error); + release(); + } return promise; } @@ -982,7 +1005,7 @@ function raiseCoordinatedStartupFailure(/** @type {any} */ error) { subsystem: 'startup', severity: 'error', message: - 'AI services did not fully start — analysis and semantic search may be unavailable. Restart StratoSort to retry.', + 'AI services did not fully start. Analysis and semantic search may be unavailable. Restart StratoSort to retry.', detail: { error: error || null } }); } catch (trackerErr) { @@ -1013,7 +1036,7 @@ function raiseCoordinatedStartupNotice(startupResult) { subsystem: 'startup', severity: 'warning', message: - 'AI services are still starting — analysis and semantic search will be ready shortly. ' + + 'AI services are still starting. Analysis and semantic search will be ready shortly. ' + 'A large library can take a few minutes; StratoSort keeps loading in the background.', detail: { error: startupResult.error || null } }); @@ -1352,6 +1375,10 @@ if (gotTheLock || process.env.STRATOSORT_FORCE_LAUNCH === '1') { if (mainWindow && !mainWindow.isDestroyed()) { logger.debug('[SECOND-INSTANCE] Restoring window for second instance attempt'); restoreMainWindow(mainWindow, 'SECOND-INSTANCE'); + } else if (!_startupWindowCreationReached) { + // Startup creates the window once IPC is registered; it serves this launch. + logger.debug('[SECOND-INSTANCE] Startup still running; window will open when it is ready'); + _windowRequestedDuringStartup = true; } else { // No window exists, create one createWindow(); @@ -1815,7 +1842,7 @@ app uptimeS: Math.round(process.uptime()) }); perfMarks.mark('startup:main:before-window'); - createWindow(); + createStartupWindow(); perfMarks.mark('startup:main:after-window'); perfMarks.measureAndLog( 'startup:main:time-to-first-window', @@ -2201,7 +2228,7 @@ app } logger.warn('[STARTUP] Creating window in degraded mode due to startup errors'); - createWindow(); + createStartupWindow(); clearTrackedTimer(startupKeepalive); // A launch that keeps failing is exactly when logs and crash dumps pile // up, so the sweeps run here too. They wait one first-paint-gate length @@ -2246,6 +2273,7 @@ const { resumeIncompleteBatches } = require('./services/OrganizeResumeService'); // Exported for unit tests (test/main/simpleMain); the app never requires this module. module.exports = { + createStartupWindow, runCoordinatedStartupInBackground, abortCoordinatedStartup, completeDeferredStartup, diff --git a/src/main/transports/mcp/auth.js b/src/main/transports/mcp/auth.js index 25dc8734..4c606271 100644 --- a/src/main/transports/mcp/auth.js +++ b/src/main/transports/mcp/auth.js @@ -22,7 +22,9 @@ function createAuthMiddleware(/** @type {any} */ getToken) { ) { const header = req.headers?.authorization || ''; if (Buffer.byteLength(header, 'utf8') > MAX_AUTH_VALUE_BYTES) return _deny(res); - const bearerMatch = /^Bearer\s+(.+)$/.exec(header); + // `\S` starts the token, so the space run and the token never overlap + // and a header of spaces can't make the match backtrack. + const bearerMatch = String(header).match(/^Bearer\s+(\S.*)$/); if (!bearerMatch) return _deny(res); const provided = bearerMatch[1]; const expected = getToken(); diff --git a/src/main/transports/mcp/server.js b/src/main/transports/mcp/server.js index bca5a740..d1d74658 100644 --- a/src/main/transports/mcp/server.js +++ b/src/main/transports/mcp/server.js @@ -114,11 +114,6 @@ async function startMcpServer({ if (entry.lastActivityAt < cutoff) void closeSession(entry); } }; - const sweepTimer = setInterval( - sweepIdleSessions, - Math.min(SESSION_SWEEP_INTERVAL_MS, sessionIdleMs) - ); - sweepTimer.unref?.(); const auth = createAuthMiddleware(getToken); const origin = createOriginGuard(); @@ -186,6 +181,13 @@ async function startMcpServer({ }; const transport = await createTransport({ requestHandler, port, logger }); + // Started only once the port is bound: a failed bind throws above, and a + // sweep started earlier would keep running with nothing to clear it. + const sweepTimer = setInterval( + sweepIdleSessions, + Math.min(SESSION_SWEEP_INTERVAL_MS, sessionIdleMs) + ); + sweepTimer.unref?.(); return { port: transport.port, toolNames, diff --git a/src/main/transports/mcp/tools/chatWithDocuments.js b/src/main/transports/mcp/tools/chatWithDocuments.js index c2b65b80..dc9e7967 100644 --- a/src/main/transports/mcp/tools/chatWithDocuments.js +++ b/src/main/transports/mcp/tools/chatWithDocuments.js @@ -89,10 +89,16 @@ function registerChatWithDocuments(/** @type {any} */ mcp, /** @type {any} */ se async (/** @type {any} */ args) => { const query = (args?.query || '').trim(); if (!query) return _err('INVALID_QUERY', 'query is required.'); + // The chat service is null until LlamaService is registered (startup); + // calling into it would surface as a TypeError and RETRIEVAL_FAILED. + const chat = services.chat; + if (!chat || typeof chat.runOnce !== 'function') { + return _err('MODEL_NOT_READY', 'model not ready'); + } const sessionId = args?.session_id || `mcp-${crypto.randomUUID()}`; try { const result = await _withSessionMutex(sessionId, async () => - services.chat.runOnce({ + chat.runOnce({ query, sessionId, maxContextFiles: args?.max_context_files ?? 5, diff --git a/src/main/transports/mcp/tools/searchDocuments.js b/src/main/transports/mcp/tools/searchDocuments.js index 10f3eb4c..5295bf7c 100644 --- a/src/main/transports/mcp/tools/searchDocuments.js +++ b/src/main/transports/mcp/tools/searchDocuments.js @@ -4,6 +4,9 @@ const path = require('node:path'); const fs = require('node:fs'); const { z } = require('zod'); +// SearchService.hybridSearch rejects shorter queries. +const MIN_QUERY_LENGTH = 2; + // True when `child` is the same directory as `parent` or strictly inside it. // Plain string startsWith is wrong: '/home/user/Doc' would match // '/home/user/Documents'. We normalize both sides and require either equal @@ -62,7 +65,11 @@ function registerSearchDocuments(/** @type {any} */ mcp, /** @type {any} */ serv description: "Hybrid semantic + keyword search across the user's analyzed documents. Returns ranked file references with snippets and scores.", inputSchema: { - query: z.string().min(1).max(1000).describe('Natural-language or keyword search query'), + query: z + .string() + .min(MIN_QUERY_LENGTH) + .max(1000) + .describe('Natural-language or keyword search query (at least 2 characters)'), top_k: z .number() .int() @@ -85,6 +92,9 @@ function registerSearchDocuments(/** @type {any} */ mcp, /** @type {any} */ serv async (/** @type {any} */ args) => { const query = (args?.query || '').trim(); if (!query) return _err('INVALID_QUERY', 'query is required and must not be empty.'); + if (query.length < MIN_QUERY_LENGTH) { + return _err('INVALID_QUERY', `query must be at least ${MIN_QUERY_LENGTH} characters.`); + } const top_k = Math.min(args?.top_k || 10, 50); /** @type {Record} */ const modeMap = { hybrid: 'hybrid', semantic: 'vector', keyword: 'bm25' }; @@ -126,6 +136,13 @@ function registerSearchDocuments(/** @type {any} */ mcp, /** @type {any} */ serv topK: folderScope ? Math.max(top_k * 4, 50) : top_k, mode }); + // hybridSearch reports a broken index or a rejected query as + // { success: false } with an empty result list. Passing that on as + // "no matches" would tell the client the library has nothing. + if (r?.success === false) { + services.logger?.warn?.('[mcp.search] search returned failure', { error: r.error }); + return _err('SERVICE_UNAVAILABLE', 'search service failed'); + } const rawHits = Array.isArray(r?.results) ? r.results : []; const allHits = rawHits.map(_normalizeHit); // Path-prefix safety: '/home/user/Doc' must NOT match '/home/user/Documents'. diff --git a/src/main/transports/mcp/transport.js b/src/main/transports/mcp/transport.js index b1c4e5e5..8c731907 100644 --- a/src/main/transports/mcp/transport.js +++ b/src/main/transports/mcp/transport.js @@ -2,6 +2,9 @@ 'use strict'; const http = require('node:http'); +// Bind errors on the preferred port that an ephemeral port can work around. +const FALLBACK_BIND_CODES = new Set(['EADDRINUSE', 'EACCES']); + /** * @param {{ requestHandler: http.RequestListener, port?: number, logger?: any }} args */ @@ -9,22 +12,32 @@ async function createTransport({ requestHandler, port = 53827, logger = null }) const server = http.createServer(requestHandler); let preferredPortInUse = false; await new Promise((resolve, reject) => { + const onListening = () => { + server.removeListener('error', onError); + resolve(undefined); + }; const onError = (/** @type {any} */ err) => { - if (err.code === 'EADDRINUSE') { + server.removeListener('listening', onListening); + if (!preferredPortInUse && FALLBACK_BIND_CODES.has(err?.code)) { // Fall back to an OS-assigned ephemeral port. Log it so a user // diagnosing "my MCP client can't reach the server" can correlate the // configured port with the actual one. The lifecycle service emits // status-changed with the real port, but a startup log entry helps - // debugging without a renderer attached. + // debugging without a renderer attached. Windows reports a port inside + // an excluded range (Hyper-V/WSL reservations) as EACCES, not + // EADDRINUSE. The listeners stay attached, so a failed fallback rejects + // instead of raising an unhandled 'error' event. preferredPortInUse = true; - server.removeListener('error', onError); - server.listen(0, '127.0.0.1', () => resolve(undefined)); + server.once('error', onError); + server.once('listening', onListening); + server.listen(0, '127.0.0.1'); return; } reject(err); }; server.once('error', onError); - server.listen(port, '127.0.0.1', () => resolve(undefined)); + server.once('listening', onListening); + server.listen(port, '127.0.0.1'); }); const addr = server.address(); const actualPort = typeof addr === 'object' && addr ? addr.port : 0; diff --git a/src/main/utils/fileDedup.js b/src/main/utils/fileDedup.js index 9a438e0b..d8c26774 100644 --- a/src/main/utils/fileDedup.js +++ b/src/main/utils/fileDedup.js @@ -234,6 +234,10 @@ async function findDuplicateInDirectory({ const sourceStat = await statSafe(sourcePath); if (!isFileStat(sourceStat)) return null; if (typeof sourceStat.size !== 'number') return null; + // Every empty file has the same size and hash, so an empty file in the + // folder says nothing about the source. Treating it as a copy would trash + // an unrelated empty placeholder the user just organized. + if (sourceStat.size === 0) return null; let entries; try { @@ -338,6 +342,10 @@ async function findDuplicateForDestination({ const sourceStat = await statSafe(sourcePath); if (!isFileStat(sourceStat)) return null; + // Empty files all match each other on size and hash: that is no evidence + // of a duplicate, and removing the source would lose the file's name and + // place (see findDuplicateInDirectory). The move goes ahead instead. + if (sourceStat.size === 0) return null; const destStat = await statSafe(destinationPath); if ( diff --git a/src/main/utils/folderRenamePathChanges.js b/src/main/utils/folderRenamePathChanges.js index 6b38b8cc..25f5f653 100644 --- a/src/main/utils/folderRenamePathChanges.js +++ b/src/main/utils/folderRenamePathChanges.js @@ -79,6 +79,10 @@ function buildFolderRenamePathChanges(entries, oldFolder, newFolder, options = { const prefix = oldKey + mod.sep; const newRoot = normalizeRoot(newFolder, platform); + // The relative part is cut by segment count, not by prefix.length: + // toLowerCase() is not length-preserving ('İ' becomes two code units), so + // slicing the case-preserved path by the folded key's length loses characters. + const rootSegmentCount = normalizeRoot(oldFolder, platform).split(mod.sep).length; /** @type {Map} */ const byOld = new Map(); @@ -88,7 +92,7 @@ function buildFolderRenamePathChanges(entries, oldFolder, newFolder, options = { const key = compareKey(current, platform); if (!key.startsWith(prefix) || byOld.has(key)) continue; const oldPath = mod.normalize(current); - const relative = oldPath.slice(prefix.length); + const relative = oldPath.split(mod.sep).slice(rootSegmentCount).join(mod.sep); byOld.set(key, { oldPath, newPath: mod.join(newRoot, relative) }); } return [...byOld.values()]; diff --git a/src/main/utils/llmOptimization.js b/src/main/utils/llmOptimization.js index a83f3875..4dea0c08 100644 --- a/src/main/utils/llmOptimization.js +++ b/src/main/utils/llmOptimization.js @@ -55,7 +55,7 @@ class LLMRequestDeduplicator { * Generate a unique key for a request based on its inputs */ generateKey(/** @type {any} */ inputs) { - const hasher = crypto.createHash('sha1'); + const hasher = crypto.createHash('sha256'); // Handle different input types if (typeof inputs === 'string') { @@ -101,25 +101,41 @@ class LLMRequestDeduplicator { /** * @param {string} key * @param {() => any} fn - * @param {{ type?: string, fileName?: string }} [metadata] + * @param {{ type?: string, fileName?: string, signal?: AbortSignal | null }} [metadata] - + * `signal` is the abort signal `fn` runs under, so a caller that joins + * this request can tell its starter's abort from its own. */ async deduplicate(key, fn, metadata = {}) { - // If request is already in flight, return the existing promise - if (this.pendingRequests.has(key)) { - const entry = /** @type {any} */ (this.pendingRequests.get(key)); + const ownSignal = metadata.signal || null; + // If request is already in flight, join the existing promise + let entry = this.pendingRequests.get(key); + while (entry) { // Check if the cached entry is stale if (Date.now() - entry.createdAt > this.maxRequestAgeMs) { this.pendingRequests.delete(key); logger.warn('[LLM-DEDUP] Evicted stale in-flight request', { key: key.slice(0, 12) }); - } else { - logger.warn('[LLM-DEDUP] Cache hit - returning in-flight request', { - key: key.slice(0, 12), - type: metadata.type || 'unknown', - fileName: metadata.fileName || 'unknown', - pendingCount: this.pendingRequests.size + break; + } + logger.warn('[LLM-DEDUP] Cache hit - returning in-flight request', { + key: key.slice(0, 12), + type: metadata.type || 'unknown', + fileName: metadata.fileName || 'unknown', + pendingCount: this.pendingRequests.size + }); + try { + return await entry.promise; + } catch (error) { + // The shared call runs under the signal of the caller that started it + // (the watcher, say, which stops or times out). That caller stopping + // is not this one stopping: join whoever took the request over, or + // run it below. + if (!entry.signal?.aborted || ownSignal?.aborted) throw error; + logger.debug('[LLM-DEDUP] Joined request was aborted by its starter; retrying', { + key: key.slice(0, 12) }); - return entry.promise; } + const next = this.pendingRequests.get(key); + entry = next === entry ? undefined : next; } // Evict stale entries when approaching capacity @@ -151,7 +167,11 @@ class LLMRequestDeduplicator { placeholder.catch((/** @type {any} */ err) => { logger.debug('[LLM-DEDUP] Placeholder rejection suppressed:', err?.message); }); - this.pendingRequests.set(key, { promise: placeholder, createdAt: Date.now() }); + this.pendingRequests.set(key, { + promise: placeholder, + createdAt: Date.now(), + signal: ownSignal + }); try { const fnResult = fn(); diff --git a/src/main/utils/safeAccess.js b/src/main/utils/safeAccess.js index 9f506480..59a36d16 100644 --- a/src/main/utils/safeAccess.js +++ b/src/main/utils/safeAccess.js @@ -5,40 +5,61 @@ * @module main/utils/safeAccess */ +// eslint-disable-next-line no-control-regex +const CONTROL_CHARS = /[\x00-\x08\x0b\x0c\x0e-\x1f]/; +const TRAVERSAL = /(^|[\\/])\.\.($|[\\/])/; +const MAX_DECODE_PASSES = 5; + /** - * Safely access and validate file path + * True when the path, or any percent-decoded form of it (up to a few passes, + * so double-encoded `%252e%252e` is caught too), contains a `..` segment. + * @param {string} p + */ +function hasEncodedTraversal(p) { + let current = p; + for (let i = 0; i <= MAX_DECODE_PASSES; i++) { + if (TRAVERSAL.test(current)) return true; + let next; + try { + next = decodeURIComponent(current); + } catch { + // A malformed escape elsewhere (e.g. "%zz") makes decodeURIComponent + // throw, so decode just the dot and separator escapes one last time. + return TRAVERSAL.test( + current.replace(/%2e/gi, '.').replace(/%2f/gi, '/').replace(/%5c/gi, '\\') + ); + } + if (next === current) return false; + current = next; + } + return false; +} + +/** + * Validate a filesystem path received over IPC. The path is returned exactly + * as given: `%20` or surrounding spaces are legal in real file names, so + * decoding or trimming here would point the caller at a different file. + * Percent-decoding is used only to detect obfuscated traversal. * @param {string} filePath - Path to validate - * @returns {string|null} Valid path or null + * @returns {string|null} The unchanged path, or null when it is rejected */ function safeFilePath(filePath) { - if (!filePath || typeof filePath !== 'string') { + if (!filePath || typeof filePath !== 'string' || filePath.trim().length === 0) { return null; } - // Decode percent-encoded sequences first to catch encoded traversal attempts - // e.g., %2e%2e%2f -> ../ , %2e%2e%5c -> ..\ - let cleanPath = filePath; - try { - cleanPath = decodeURIComponent(cleanPath); - } catch { - // If decoding fails (malformed encoding), proceed with the raw string - } - - // Remove null bytes and control characters (ASCII 0x00-0x1f except tab/newline) - // eslint-disable-next-line no-control-regex - cleanPath = cleanPath.replace(/[\x00-\x08\x0b\x0c\x0e-\x1f]/g, '').trim(); - - if (!cleanPath || cleanPath.length === 0) { + // Reject rather than strip: stripping would also yield a different path. + if (CONTROL_CHARS.test(filePath)) { return null; } - // Reject path traversal sequences (both forward and backslash variants) - // Matches: ../ , ..\ , or bare .. at start/end of path - if (/(^|[\\/])\.\.($|[\\/])/.test(cleanPath)) { + // Reject path traversal sequences (both forward and backslash variants), + // including percent-encoded forms such as %2e%2e%2f. + if (hasEncodedTraversal(filePath)) { return null; } - return cleanPath; + return filePath; } module.exports = { diff --git a/src/main/workers/textExtractionWorker.js b/src/main/workers/textExtractionWorker.js index 4cb6e5b8..ea007ea1 100644 --- a/src/main/workers/textExtractionWorker.js +++ b/src/main/workers/textExtractionWorker.js @@ -7,8 +7,10 @@ const { rtfToText } = require('../analysis/extractors/rtfText'); const { decodeHtmlEntities } = require('../analysis/extractors/htmlEntities'); const REGEX_PATTERNS = { - scriptTags: //gi, - styleTags: //gi, + // End tags may carry whitespace or junk before '>' ("", + // ""); browsers still close the element there, so match it too. + scriptTags: /]*>/gi, + styleTags: /]*>/gi, htmlTags: /<[^>]+>/g, whitespace: /\s+/g }; diff --git a/src/preload/ipcSanitizer.js b/src/preload/ipcSanitizer.js index 937dcd8b..3bb1bf98 100644 --- a/src/preload/ipcSanitizer.js +++ b/src/preload/ipcSanitizer.js @@ -125,14 +125,17 @@ function createIpcSanitizer({ log }) { let cleaned = str; - // Remove dangerous executable tags (script, style, iframe) - cleaned = cleaned.replace(/)<[^<]*)*<\/script>/gi, ''); - cleaned = cleaned.replace(/]*>[\s\S]*$/gi, ''); - - cleaned = cleaned.replace(/)<[^<]*)*<\/style>/gi, ''); - cleaned = cleaned.replace(/]*>[\s\S]*$/gi, ''); - - cleaned = cleaned.replace(/)<[^<]*)*<\/iframe>/gi, ''); + // Remove dangerous executable tags (script, style, iframe). Closing tags + // may carry spaces or junk before ">" (``), as browsers accept. + // Repeat until nothing changes: removing one element can join the text + // around it into another (`ipt>`). + let previous; + do { + previous = cleaned; + cleaned = cleaned.replace(/<(script|style|iframe)\b[\s\S]*?<\/\1\b[^>]*>/gi, ''); + } while (cleaned !== previous); + // Then an unclosed script or style, to the end of the text. + cleaned = cleaned.replace(/<(script|style)\b[^>]*>[\s\S]*$/gi, ''); // IPC data is rendered through React JSX which auto-escapes, so entity encoding // is unnecessary. The previous code corrupted user text like "size < 10MB", @@ -160,8 +163,29 @@ function createIpcSanitizer({ log }) { // Prose that mentions a Windows path ("What is in D:\Docs?") matches the // path heuristic and used to be blanked before it left the renderer. Exact // names only; every other key keeps the heuristic, and a key containing - // "path" or "file" always wins. - const FREE_TEXT_KEYS = new Set(['query', 'description', 'text', 'message', 'note']); + // "path" or "file" always wins. Matched on key.toLowerCase(), so entries + // are lowercase. + // + // The settings prose fields (organization and persona guidance, persona + // label) were blanked when they named a path and also held `?`, `*` or + // `..`; an empty persona guidance then dropped the whole persona in main. + // Exclusion glob lists (settings `downloadsTriageExclusions`, triage + // `exclusionGlobs`) hold `\` and `*` by design; a blanked entry failed the + // settings schema (min length 1), so every later save failed. Main + // re-validates all of these (sanitizeCustomPersonas, sanitizeExclusionGlobs). + const FREE_TEXT_KEYS = new Set([ + 'query', + 'description', + 'text', + 'message', + 'note', + 'organizationcustomguidance', + 'chatpersonacustomguidance', + 'guidance', + 'label', + 'downloadstriageexclusions', + 'exclusionglobs' + ]); /** * @param {any} obj diff --git a/src/preload/ipcValidator.js b/src/preload/ipcValidator.js index d422c2ee..bd7ada45 100644 --- a/src/preload/ipcValidator.js +++ b/src/preload/ipcValidator.js @@ -16,6 +16,7 @@ function createIpcValidator({ log: _log } = {}) { const MAX_ARRAY_LENGTH = 50000; const MAX_OBJECT_KEYS = 5000; const MAX_STRING_LENGTH = 500000; // ~500KB per string payload + const PAYLOAD_TOO_LARGE = 'payload-too-large'; /** * Validate event source to prevent spoofing. @@ -136,7 +137,15 @@ function createIpcValidator({ log: _log } = {}) { case 'smart-folders:get-custom': return []; case 'analysis-history:get': - return []; + // Not []: pagination reads an empty page as the end of the + // history, so a page of long transcripts made the whole list look + // empty. The reason lets fetchAnalysisHistoryPages retry the same + // offset with a smaller page. + return { + success: false, + error: 'IPC response rejected: payload exceeded safety limits', + reason: PAYLOAD_TOO_LARGE + }; case 'undo-redo:get-history': return []; case 'undo-redo:get-state': diff --git a/src/renderer/components/AnalysisDetails.jsx b/src/renderer/components/AnalysisDetails.jsx index 543854aa..b4c0006c 100644 --- a/src/renderer/components/AnalysisDetails.jsx +++ b/src/renderer/components/AnalysisDetails.jsx @@ -148,7 +148,7 @@ const AnalysisDetails = memo(function AnalysisDetails({ if (result?.success) { setExportStatus( result?.truncated - ? `Saved ${format.toUpperCase()} — long recording: transcript was truncated at 1,000 segments` + ? `Saved ${format.toUpperCase()}. Long recording, so the transcript was cut off at 1,000 segments.` : `Saved ${format.toUpperCase()}` ); } else if (!result?.canceled) { diff --git a/src/renderer/components/AnalysisHistoryModal.jsx b/src/renderer/components/AnalysisHistoryModal.jsx index 134450e8..206b3cea 100644 --- a/src/renderer/components/AnalysisHistoryModal.jsx +++ b/src/renderer/components/AnalysisHistoryModal.jsx @@ -150,10 +150,10 @@ function AnalysisHistoryModal({ onClose, analysisStats, setAnalysisStats }) { if (actual && typeof actual === 'string') { const normalized = actual.replace(/\\+/g, '/'); const segments = normalized.split('/').filter(Boolean); + // organization.actual is the moved file's path, so the folder is its + // parent. Guessing by extension named LICENSE or Makefile as the folder. if (segments.length > 1) { - const last = segments[segments.length - 1]; - const isFile = /\.[A-Za-z0-9]+$/.test(last); - const folder = isFile ? segments[segments.length - 2] : last; + const folder = segments[segments.length - 2]; if (folder) return folder; } } diff --git a/src/renderer/components/ErrorBoundary.jsx b/src/renderer/components/ErrorBoundary.jsx index 33b50f39..d6b8eab9 100644 --- a/src/renderer/components/ErrorBoundary.jsx +++ b/src/renderer/components/ErrorBoundary.jsx @@ -27,6 +27,9 @@ class ErrorBoundaryCore extends React.Component { chunkReloadExhausted: false }; this.resetTimeoutId = null; + // Runs after an automatic reset; if the children stay up this long, the + // earlier failures are forgotten and errorCount starts over. + this.stableTimeoutId = null; this.handleReset = this.handleReset.bind(this); this.handleNavigateHome = this.handleNavigateHome.bind(this); this.handleReload = this.handleReload.bind(this); @@ -48,6 +51,8 @@ class ErrorBoundaryCore extends React.Component { timestamp: new Date().toISOString() }); + this.clearStableTimer(); + this.setState( (prevState) => ({ errorInfo, @@ -76,6 +81,14 @@ class ErrorBoundaryCore extends React.Component { clearTimeout(this.resetTimeoutId); this.resetTimeoutId = null; } + this.clearStableTimer(); + } + + clearStableTimer() { + if (this.stableTimeoutId) { + clearTimeout(this.stableTimeoutId); + this.stableTimeoutId = null; + } } handleChunkLoadError(error, context) { @@ -158,15 +171,42 @@ class ErrorBoundaryCore extends React.Component { clearTimeout(this.resetTimeoutId); } this.resetTimeoutId = setTimeout(() => { - this.handleReset(); + this.handleAutoReset(delay); }, delay); } + // Unlike a manual reset, keeps errorCount: an error that recurs right after + // the remount must count toward the retry cap and the "Multiple errors" + // hint, or a child that always throws is remounted forever. + handleAutoReset(delay) { + this.resetTimeoutId = null; + this.clearStableTimer(); + this.stableTimeoutId = setTimeout(() => { + this.stableTimeoutId = null; + if (!this.state.hasError) { + this.setState({ errorCount: 0 }); + } + }, delay); + + this.setState((prevState) => ({ + hasError: false, + error: null, + errorInfo: null, + resetKey: prevState.resetKey + 1, + chunkReloadExhausted: false + })); + + if (this.props.onReset) { + this.props.onReset(); + } + } + handleReset() { if (this.resetTimeoutId) { clearTimeout(this.resetTimeoutId); this.resetTimeoutId = null; } + this.clearStableTimer(); this.setState((prevState) => ({ hasError: false, @@ -183,6 +223,11 @@ class ErrorBoundaryCore extends React.Component { } handleNavigateHome() { + if (this.resetTimeoutId) { + clearTimeout(this.resetTimeoutId); + this.resetTimeoutId = null; + } + this.clearStableTimer(); this.setState((prevState) => ({ hasError: false, error: null, @@ -261,7 +306,7 @@ class ErrorBoundaryCore extends React.Component { {needsRestart - ? 'StratoSort was updated while it was running. Please quit and reopen the app to finish updating — reloading won’t resolve this.' + ? 'StratoSort was updated while it was running. Quit and reopen the app to finish updating. Reloading won’t fix this.' : isChunk ? 'App assets failed to load. This usually happens after an update.' : 'The application encountered an unexpected error.'} @@ -271,7 +316,7 @@ class ErrorBoundaryCore extends React.Component { {errorCount > 2 && !needsRestart && (
- Multiple errors detected. Consider reloading the application. + Multiple errors detected. Try reloading the app.
)} @@ -310,7 +355,7 @@ class ErrorBoundaryCore extends React.Component { - If this problem persists, please check your settings or contact support. + If this keeps happening, check your settings or contact support. @@ -344,7 +389,7 @@ class ErrorBoundaryCore extends React.Component { {isChunk && ( {needsRestart - ? 'StratoSort was updated while running. Please quit and reopen the app to finish updating — reloading won’t help.' + ? 'StratoSort was updated while running. Quit and reopen the app to finish updating. Reloading won’t help.' : 'This looks like an app asset mismatch. Reloading usually fixes it after an update.'} )} diff --git a/src/renderer/components/ModelSetupWizard.jsx b/src/renderer/components/ModelSetupWizard.jsx index fc047b5d..6a5400b0 100644 --- a/src/renderer/components/ModelSetupWizard.jsx +++ b/src/renderer/components/ModelSetupWizard.jsx @@ -38,13 +38,17 @@ const STEP_TITLES = { downloading: 'Download', complete: 'Finish' }; -const DOWNLOADS_STOPPED_MESSAGE = 'Required model downloads stopped. Please retry downloads.'; +const DOWNLOADS_STOPPED_MESSAGE = 'Required model downloads stopped. Retry the downloads.'; // Main says "started" before the download is in its status list (it checks // disk space and any partial file first), so for this long after a request an // empty list does not mean the download stopped. const DOWNLOAD_START_GRACE_MS = 3000; const PROJECTOR_MISSING_MESSAGE = 'The model downloaded, but its image projector file did not download. Images cannot be analyzed until it does. Retry to fetch it.'; +// The vision model is optional: when it does not install, setup can still +// finish on the required models, and this warning says what is missing. +const VISION_NOT_INSTALLED_MESSAGE = + 'The image analysis model is not installed, so images cannot be analyzed yet. Documents are analyzed as usual, and you can download it later from Settings.'; const CHECKING_STATUS_MESSAGES = [ 'Detecting available local models...', 'Checking active background downloads...', @@ -471,7 +475,7 @@ function ModelSetupWizardBody({ onComplete, onSkip, onDismiss, onStepChange }) { if (!hasLlamaApi) { if (!isMountedRef.current) return; applyFallbackSelection(); - setInitError('AI engine is still starting. Please try again in a moment.'); + setInitError('AI engine is still starting. Try again in a moment.'); setStep('select'); setIsRefreshing(false); return; @@ -653,7 +657,7 @@ function ModelSetupWizardBody({ onComplete, onSkip, onDismiss, onStepChange }) { if (step === 'checking' && !pendingEmbeddingSwitch) { const timer = setTimeout(() => { if (isMountedRef.current) { - setInitError('AI system check timed out. Please select models manually.'); + setInitError('AI system check timed out. Select models manually.'); setStep('select'); setIsRefreshing(false); } @@ -917,6 +921,38 @@ function ModelSetupWizardBody({ onComplete, onSkip, onDismiss, onStepChange }) { return; } + // Only the optional vision model is missing and nothing runs any more: + // its download stopped. A row still showing "downloading" would keep + // the step waiting forever with "Continue with limited AI" as the only + // exit (which skips the chosen profile), so mark it failed. The step + // then offers Continue (required models are in) next to Retry. + if ( + requiredMissing.length === 0 && + selectedMissing.length > 0 && + hasReliableDownloadStatus && + activeDownloads.length === 0 && + !downloadWasJustRequested() + ) { + setDownloadState((prev) => { + let changed = false; + const next = { ...prev }; + selectedMissing.forEach((name) => { + const status = prev[name]?.status; + if (status === 'failed' || status === 'cancelled') return; + changed = true; + next[name] = { + ...(prev[name] || {}), + status: 'failed', + speedBps: 0, + etaSeconds: 0, + error: prev[name]?.error || 'The download stopped before it finished.' + }; + }); + return changed ? next : prev; + }); + return; + } + if ( requiredMissing.length > 0 && hasReliableDownloadStatus && @@ -1072,6 +1108,9 @@ function ModelSetupWizardBody({ onComplete, onSkip, onDismiss, onStepChange }) { const selectedMissing = Object.values(selectedModels) .filter(Boolean) .filter((name) => !nextAvailable.has(name)); + const requiredMissing = [selectedModels.embedding, selectedModels.text] + .filter(Boolean) + .filter((name) => !nextAvailable.has(name)); let configApplied = true; if (selectedMissing.length === 0) { @@ -1084,11 +1123,15 @@ function ModelSetupWizardBody({ onComplete, onSkip, onDismiss, onStepChange }) { // An empty status list right after main acknowledged a request only // means the download is not registered yet; with a failed engine the // model list cannot say what is missing either. The poll takes over. + // With embedding + text installed, only the optional vision request + // failed: stay on the download step, where its row reads Failed and + // Continue / Retry are offered, instead of dropping back to Choose. const statusSaysNothingRuns = hasReliableDownloadStatus && activeDownloads.length === 0 && acknowledged === 0 && - !modelListError; + !modelListError && + requiredMissing.length > 0; setStep(statusSaysNothingRuns ? 'select' : 'downloading'); } } @@ -1429,8 +1472,20 @@ function ModelSetupWizardBody({ onComplete, onSkip, onDismiss, onStepChange }) { const status = downloadState[filename]?.status || 'downloading'; return !['ready', 'complete', 'failed', 'cancelled'].includes(status); }); + // The vision model is optional. With embedding + text ready and nothing + // still downloading, a failed or cancelled vision download is a warning, + // and "Continue" still saves the chosen profile and finishes setup. + const isReadyStatus = (filename) => + ['ready', 'complete'].includes(downloadState[filename]?.status); + const requiredComplete = [selectedModels.embedding, selectedModels.text] + .filter(Boolean) + .every(isReadyStatus); + const canContinue = allComplete || (requiredComplete && !hasActiveDownload); + const optionalVisionStopped = + Boolean(selectedModels.vision) && + ['failed', 'cancelled'].includes(downloadState[selectedModels.vision]?.status); const leaveKeepsDownloading = - !allComplete && hasActiveDownload && typeof onDismiss === 'function'; + !canContinue && hasActiveDownload && typeof onDismiss === 'function'; return ( @@ -1454,6 +1509,12 @@ function ModelSetupWizardBody({ onComplete, onSkip, onDismiss, onStepChange }) { )} + {requiredComplete && optionalVisionStopped && !hasActiveDownload && ( + + {VISION_NOT_INSTALLED_MESSAGE} + + )} +
{models.map(([_type, filename]) => { const progress = downloadState[filename] || { percent: 0 }; @@ -1543,7 +1604,7 @@ function ModelSetupWizardBody({ onComplete, onSkip, onDismiss, onStepChange }) {
diff --git a/src/renderer/components/NavigationBar.jsx b/src/renderer/components/NavigationBar.jsx index f8600cb7..59e69607 100644 --- a/src/renderer/components/NavigationBar.jsx +++ b/src/renderer/components/NavigationBar.jsx @@ -33,6 +33,8 @@ import { useUndoRedo } from './UndoRedoSystem'; import { hasDestinationFolder } from '../utils/smartFolderDestinations'; const logger = createLogger('NavigationBar'); +// How often the AI / vector DB health dots are re-probed while mounted. +const HEALTH_REPROBE_INTERVAL_MS = 60 * 1000; // ============================================================================= // Icon Components - Using Lucide React for premium icons // ============================================================================= @@ -717,7 +719,9 @@ function NavigationBar() { [dispatch] ); - const didProbeHealth = useRef(false); + // A probe still awaiting IPC; the focus listener and the interval skip + // instead of stacking a second llama:test-connection on top of it. + const healthProbeInFlight = useRef(false); const didFetchAiConfig = useRef(false); const AI_CONFIG_FETCH_MAX_ATTEMPTS = 3; @@ -752,11 +756,12 @@ function NavigationBar() { }; }, [dispatch, aiProviderLoaded]); + // AI health is re-probed while the bar is mounted (on window focus and + // every HEALTH_REPROBE_INTERVAL_MS), not only once at mount: a probe that + // ran while the engine was still starting left the dot offline for good, + // and a later failure or recovery never showed. useEffect(() => { - if (didProbeHealth.current) return; - didProbeHealth.current = true; - - const probeHealth = async () => { + const runProbe = async () => { // LlamaService is in-process - use testConnection to verify model loaded if (window.electronAPI?.llama?.testConnection) { try { @@ -808,7 +813,32 @@ function NavigationBar() { } }; - probeHealth(); + const probeHealth = async () => { + if (healthProbeInFlight.current) return; + healthProbeInFlight.current = true; + try { + await runProbe(); + } finally { + healthProbeInFlight.current = false; + } + }; + + void probeHealth(); + + const handleFocus = () => { + void probeHealth(); + }; + window.addEventListener('focus', handleFocus); + const intervalId = setInterval(() => { + // A hidden window re-probes on focus; skip the background ticks. + if (typeof document !== 'undefined' && document.visibilityState === 'hidden') return; + void probeHealth(); + }, HEALTH_REPROBE_INTERVAL_MS); + + return () => { + window.removeEventListener('focus', handleFocus); + clearInterval(intervalId); + }; }, [dispatch]); // Scroll effect for glass morphism - throttled to prevent excessive re-renders @@ -1057,7 +1087,7 @@ function NavigationBar() { ? executionStatus?.label ? `${executionStatus.label} Step navigation is paused until file moves finish.` : 'Step navigation is paused while undo/redo moves files.' - : 'Step navigation is paused. Please wait for the current operation to finish.'} + : 'Step navigation is paused until the current operation finishes.'}
)} diff --git a/src/renderer/components/PhaseErrorBoundary.jsx b/src/renderer/components/PhaseErrorBoundary.jsx index 13a7e9d4..de7b2a5a 100644 --- a/src/renderer/components/PhaseErrorBoundary.jsx +++ b/src/renderer/components/PhaseErrorBoundary.jsx @@ -6,6 +6,7 @@ import { resetUi } from '../store/slices/uiSlice'; import { resetFilesState } from '../store/slices/filesSlice'; import { resetAnalysisState, resetToSafeState } from '../store/slices/analysisSlice'; import { ErrorBoundaryCore } from './ErrorBoundary'; +import { useOptionalAnalysisRun } from '../phases/discover/AnalysisRunProvider'; const logger = createLogger('PhaseErrorBoundary'); /** @@ -18,8 +19,18 @@ const logger = createLogger('PhaseErrorBoundary'); */ function PhaseErrorBoundary({ children, phaseName }) { const dispatch = useAppDispatch(); + const analysisRun = useOptionalAnalysisRun(); const handleNavigateHome = () => { + // Clearing Redux alone leaves a running analysis alive with no progress + // card or Stop button, and the next files queue behind it. + try { + analysisRun?.resetRun?.('Go to Home', { discard: true }); + } catch (error) { + logger.warn('Could not stop the analysis run on Go to Home', { + error: error?.message + }); + } dispatch(resetUi()); dispatch(resetFilesState()); dispatch(resetAnalysisState()); diff --git a/src/renderer/components/SettingsPanel.jsx b/src/renderer/components/SettingsPanel.jsx index 7ec12437..4d77a366 100644 --- a/src/renderer/components/SettingsPanel.jsx +++ b/src/renderer/components/SettingsPanel.jsx @@ -1388,7 +1388,7 @@ const SettingsPanel = React.memo(function SettingsPanel() { title={`Save failed: ${lastSaveError}. Click to retry.`} >
- Electron API not available. Please restart the application. + Electron API not available. Restart the application. - Electron API not available. Please restart the application. + Electron API not available. Restart the application.
@@ -1636,8 +1636,8 @@ const SettingsPanel = React.memo(function SettingsPanel() {
{isSimpleMode - ? 'Simple mode hides the advanced configuration panels and uses a streamlined retrieval path, while still embedding your files on-device in the background — so nothing needs re-processing if you switch to Advanced later.' - : 'Advanced reveals every configuration panel — AI models, semantic search, chat-with-documents, and the knowledge graph — and enables graph-aware retrieval at search time.'} + ? 'Simple mode hides the advanced configuration panels and uses a simpler retrieval path. It still embeds your files on-device in the background, so nothing needs re-processing if you switch to Advanced later.' + : 'Advanced reveals every configuration panel (AI models, semantic search, chat-with-documents, and the knowledge graph) and enables graph-aware retrieval at search time.'} diff --git a/src/renderer/components/Toast.jsx b/src/renderer/components/Toast.jsx index 538c7e9d..e7cf4f60 100644 --- a/src/renderer/components/Toast.jsx +++ b/src/renderer/components/Toast.jsx @@ -79,12 +79,9 @@ const messageToText = (message) => { } return message == null ? '' : String(message); }; -const getHighestSeverity = (a, b) => { - const order = { error: 3, warning: 2, success: 1, info: 0 }; - const aScore = order[a] ?? 0; - const bScore = order[b] ?? 0; - return aScore >= bScore ? a : b; -}; +const SEVERITY_RANK = { error: 3, warning: 2, success: 1, info: 0 }; +const severityRank = (severity) => SEVERITY_RANK[severity] ?? 0; +const getHighestSeverity = (a, b) => (severityRank(a) >= severityRank(b) ? a : b); function Toast({ message, @@ -518,11 +515,23 @@ export const useToast = () => { existing.severity || 'info', severity || 'info' ); + // Show the text that carries the merged severity: a newer message at + // the same or a higher level replaces the old one (and its details + // and click action), so an escalated toast never shows the milder text. + const useIncoming = severityRank(severity || 'info') >= severityRank(existing.severity); + const incoming = useIncoming + ? { + message, + actionable: Boolean(meta?.actionable), + activationType: meta?.type || null, + activationData: meta?.data ?? null, + details: typeof meta?.details === 'string' && meta.details ? meta.details : null + } + : {}; const updated = { ...existing, + ...incoming, id: existing.id, // keep id stable for animation - message: existing.originalMessage || existing.message, - originalMessage: existing.originalMessage || existing.message, mergeCount, severity: mergedSeverity, duration: durationForSeverity(mergedSeverity, duration ?? existing.duration), diff --git a/src/renderer/components/UndoRedoSystem.jsx b/src/renderer/components/UndoRedoSystem.jsx index 8fabde7e..7485dc01 100644 --- a/src/renderer/components/UndoRedoSystem.jsx +++ b/src/renderer/components/UndoRedoSystem.jsx @@ -925,7 +925,7 @@ export function UndoRedoProvider({ children }) { const executeAction = useCallback( async (actionConfig) => { if (actionMutexRef.current) { - showInfo('Please wait for the current action to complete'); + showInfo('Wait for the current action to finish'); return null; } @@ -981,7 +981,7 @@ export function UndoRedoProvider({ children }) { const undo = useCallback(async () => { if (actionMutexRef.current) { - showInfo('Please wait for the current action to complete'); + showInfo('Wait for the current action to finish'); return; } @@ -1072,7 +1072,7 @@ export function UndoRedoProvider({ children }) { const redo = useCallback(async () => { if (actionMutexRef.current) { - showInfo('Please wait for the current action to complete'); + showInfo('Wait for the current action to finish'); return; } @@ -1159,7 +1159,7 @@ export function UndoRedoProvider({ children }) { const jumpToPoint = useCallback( async (targetIndex) => { if (actionMutexRef.current) { - showInfo('Please wait for the current action to complete'); + showInfo('Wait for the current action to finish'); return; } @@ -1179,7 +1179,7 @@ export function UndoRedoProvider({ children }) { beginExecution( 'jump', targetIndex < currentIndex ? 'Jumping back in history…' : 'Jumping forward in history…', - `${steps} step${steps === 1 ? '' : 's'} — files will move as each step runs` + `${steps} step${steps === 1 ? '' : 's'}: files will move as each step runs` ); try { diff --git a/src/renderer/components/common/AIProviderBadge.jsx b/src/renderer/components/common/AIProviderBadge.jsx index c6c3c66f..de7077ee 100644 --- a/src/renderer/components/common/AIProviderBadge.jsx +++ b/src/renderer/components/common/AIProviderBadge.jsx @@ -33,7 +33,7 @@ function AIProviderBadge() { const distinct = new Set(routes.map((route) => route.providerId)); const summary = distinct.size === 1 - ? `Cloud AI — ${labelFor(routes[0].providerId)}.` + ? `Cloud AI: ${labelFor(routes[0].providerId)}.` : `Cloud AI: ${routes .map((route) => { const where = diff --git a/src/renderer/components/common/AIProviderIcon.jsx b/src/renderer/components/common/AIProviderIcon.jsx index e28c645d..1a744a82 100644 --- a/src/renderer/components/common/AIProviderIcon.jsx +++ b/src/renderer/components/common/AIProviderIcon.jsx @@ -5,7 +5,7 @@ import { Cloud, Cpu } from 'lucide-react'; const PROVIDER_META = { local: { label: 'Local Llama', - title: 'Analyzed locally — no data left this machine', + title: 'Analyzed locally. No data left this machine.', Icon: Cpu, tone: 'text-system-gray-500' }, diff --git a/src/renderer/components/common/CloudAIConsentModal.jsx b/src/renderer/components/common/CloudAIConsentModal.jsx index 400e35a1..327b2e22 100644 --- a/src/renderer/components/common/CloudAIConsentModal.jsx +++ b/src/renderer/components/common/CloudAIConsentModal.jsx @@ -86,7 +86,7 @@ function CloudAIConsentModal({ provider, onAccept, onDecline }) { persistLocalAcknowledgment(); onAccept(); } catch (err) { - setError(err?.message || 'Could not record consent. Please try again.'); + setError(err?.message || 'Could not record consent. Try again.'); } finally { setSubmitting(false); } diff --git a/src/renderer/components/dashboard/DashboardView.jsx b/src/renderer/components/dashboard/DashboardView.jsx index 971a328a..dac52a1e 100644 --- a/src/renderer/components/dashboard/DashboardView.jsx +++ b/src/renderer/components/dashboard/DashboardView.jsx @@ -911,8 +911,8 @@ export default function DashboardView() { )} {stats.failed > 0 && ( - {stats.failed} {stats.failed === 1 ? 'file' : 'files'} couldn't be analyzed — - retry {stats.failed === 1 ? 'it' : 'them'} in Discover. + {stats.failed} {stats.failed === 1 ? 'file' : 'files'} couldn't be analyzed. + Retry {stats.failed === 1 ? 'it' : 'them'} in Discover. )} diff --git a/src/renderer/components/dashboard/KnowledgeStatsCard.jsx b/src/renderer/components/dashboard/KnowledgeStatsCard.jsx index 591619c2..7b2e1d8d 100644 --- a/src/renderer/components/dashboard/KnowledgeStatsCard.jsx +++ b/src/renderer/components/dashboard/KnowledgeStatsCard.jsx @@ -180,7 +180,7 @@ export default function KnowledgeStatsCard({ className = '' }) { if (hasYears) { tiles.push({ icon: CalendarRange, - value: `${startYear} – ${endYear}`, + value: `${startYear} to ${endYear}`, label: yearLabel }); } diff --git a/src/renderer/components/layout/DegradedStateBanner.jsx b/src/renderer/components/layout/DegradedStateBanner.jsx index 0db087a5..80639c21 100644 --- a/src/renderer/components/layout/DegradedStateBanner.jsx +++ b/src/renderer/components/layout/DegradedStateBanner.jsx @@ -179,7 +179,7 @@ const MIGRATION_FLAG_DEFINITIONS = [ code: 'state-migration.gap', severity: 'error', message: - "Your saved workspace state needs an upgrade step that this build is missing — it couldn't be loaded safely. Recent selections, file states, and analysis results were reset. Smart folders and settings should still be intact." + "Your saved workspace state needs an upgrade step that this build is missing, so it couldn't be loaded safely. Recent selections, file states, and analysis results were reset. Smart folders and settings should still be intact." }, { windowKey: '__STRATOSORT_STATE_SHAPE_RECOVERED__', @@ -445,7 +445,7 @@ function DegradedStateBanner() { code: 'renderer.smart-folders-load', subsystem: 'smart-folders', severity: 'error', - message: `Smart folders could not be loaded — organization suggestions may be incomplete. (${smartFoldersError})`, + message: `Smart folders could not be loaded, so organization suggestions may be incomplete. (${smartFoldersError})`, raisedAt: now, lastSeenAt: now, count: 1, @@ -456,7 +456,7 @@ function DegradedStateBanner() { code: 'renderer.settings-load', subsystem: 'settings', severity: 'warning', - message: `Settings could not be loaded — defaults are in effect. (${settingsError})`, + message: `Settings could not be loaded, so defaults are in effect. (${settingsError})`, raisedAt: now, lastSeenAt: now, count: 1, diff --git a/src/renderer/components/organize/DuplicateResolutionModal.jsx b/src/renderer/components/organize/DuplicateResolutionModal.jsx index 5994ebc0..4a55f9c9 100644 --- a/src/renderer/components/organize/DuplicateResolutionModal.jsx +++ b/src/renderer/components/organize/DuplicateResolutionModal.jsx @@ -37,7 +37,7 @@ const TIER_CONFIG = { related: { label: 'Related', reasonCopy: - 'Topically similar — may be drafts or versions. Do not bulk-delete without reviewing.', + 'Topically similar, so they may be drafts or versions. Do not bulk-delete without reviewing.', badgeClass: 'bg-system-gray-100 text-system-gray-600 border-system-gray-200' } }; @@ -505,7 +505,8 @@ export default function DuplicateResolutionModal({ const [showDismissed, setShowDismissed] = useState(false); const { openFile, revealFile } = useFileActions(); const redactPaths = useSelector(selectRedactPaths); - const prevIsOpenRef = useRef(false); + // The duplicateGroups array the default keepers were last computed for. + const initializedForRef = useRef(null); // Normalize incoming groups and attach a stable flatIndex used as selection key. const normalizedGroups = useMemo(() => { @@ -609,26 +610,41 @@ export default function DuplicateResolutionModal({ [normalizedGroups, relatedThreshold, dismissedSignatures] ); - // Initialize per-tier default selection on open. + // Initialize per-tier default selection once per scan result. Callers open + // the dialog and start the scan in the same handler, so the groups usually + // arrive after the dialog is already open; keying on the groups (not on the + // open transition) still pre-selects a keeper for them. useEffect(() => { - const wasOpen = prevIsOpenRef.current; - prevIsOpenRef.current = isOpen; - if (isOpen && !wasOpen && normalizedGroups.length > 0) { - const initial = {}; - for (const group of normalizedGroups) { - const keeper = pickDefaultKeeper(group); - if (keeper != null) { - initial[group.flatIndex] = keeper; - } + // Closed: re-initialize on the next open, but keep the choices for now so + // the footer does not change while the dialog animates out. + if (!isOpen) { + initializedForRef.current = null; + return; + } + // A rescan cleared the list: flatIndex keys would point at a different + // set of groups next time, so drop leftover choices. Bail out with the + // same object when already empty to avoid a render loop. + if (normalizedGroups.length === 0) { + initializedForRef.current = null; + setSelections((prev) => (Object.keys(prev).length === 0 ? prev : {})); + return; + } + if (initializedForRef.current === duplicateGroups) return; + initializedForRef.current = duplicateGroups; + const initial = {}; + for (const group of normalizedGroups) { + const keeper = pickDefaultKeeper(group); + if (keeper != null) { + initial[group.flatIndex] = keeper; } - setSelections(initial); - setCollapsedTiers({ - exact: false, - nearIdentical: false, - related: tierBuckets.related.length === 0 - }); } - }, [isOpen, normalizedGroups, tierBuckets.related.length]); + setSelections(initial); + setCollapsedTiers({ + exact: false, + nearIdentical: false, + related: tierBuckets.related.length === 0 + }); + }, [isOpen, duplicateGroups, normalizedGroups, tierBuckets.related.length]); const handleSelectKeep = useCallback((groupIndex, filePath) => { setSelections((prev) => ({ ...prev, [groupIndex]: filePath })); @@ -694,7 +710,7 @@ export default function DuplicateResolutionModal({ relatedHiddenByThreshold > 0 ? ` (${relatedHiddenByThreshold} related group${ relatedHiddenByThreshold === 1 ? '' : 's' - } below ${Math.round(relatedThreshold * 100)}% — lower the related slider to show)` + } below ${Math.round(relatedThreshold * 100)}%; lower the related slider to show them)` : '' }.${ dismissedCount > 0 diff --git a/src/renderer/components/organize/FeedbackMemoryPanel.jsx b/src/renderer/components/organize/FeedbackMemoryPanel.jsx index 52cc8204..b3073d4f 100644 --- a/src/renderer/components/organize/FeedbackMemoryPanel.jsx +++ b/src/renderer/components/organize/FeedbackMemoryPanel.jsx @@ -4,6 +4,28 @@ import { Trash2, Info, Pencil } from 'lucide-react'; import { Card, Button, IconButton, StateMessage, Textarea } from '../ui'; import { Heading, Text } from '../ui/Typography'; +// Matches feedbackMemoryAdd / feedbackMemoryUpdate in src/main/ipc/validationSchemas.js. +const MIN_MEMORY_LENGTH = 2; +const MAX_MEMORY_LENGTH = 500; + +/** + * Turn a failed IPC result (or a rejected invoke) into a short message. The + * handlers resolve `{ success: false, error }` rather than throwing, and + * `error` may be a string or an `{ code, message }` object. A schema failure + * arrives as `error.details.validationErrors` (Zod's flatten() output), whose + * bare message is just "Invalid input", so a bad `text` gets the length hint. + */ +function describeFailure(result, fallback) { + if (result?.error?.details?.validationErrors?.fieldErrors?.text) { + return `Notes must be ${MIN_MEMORY_LENGTH}-${MAX_MEMORY_LENGTH} characters.`; + } + const error = result?.error; + if (typeof error === 'string' && error) return error; + if (error && typeof error.message === 'string' && error.message) return error.message; + if (result instanceof Error && result.message) return result.message; + return fallback; +} + function FeedbackMemoryPanel({ className = '', refreshToken }) { const [memories, setMemories] = useState([]); const [newText, setNewText] = useState(''); @@ -11,6 +33,7 @@ function FeedbackMemoryPanel({ className = '', refreshToken }) { const [saving, setSaving] = useState(false); const [editingId, setEditingId] = useState(null); const [editingText, setEditingText] = useState(''); + const [errorMessage, setErrorMessage] = useState(null); const isMountedRef = React.useRef(true); useEffect(() => { @@ -42,6 +65,7 @@ function FeedbackMemoryPanel({ className = '', refreshToken }) { const trimmed = newText.trim(); if (!trimmed) return; setSaving(true); + setErrorMessage(null); try { const result = await window.electronAPI?.suggestions?.addFeedbackMemory?.(trimmed); if (isMountedRef.current) { @@ -49,9 +73,15 @@ function FeedbackMemoryPanel({ className = '', refreshToken }) { setMemories((prev) => [result.item, ...prev]); setNewText(''); } else { + // newText is kept so the user can fix it and try again. + setErrorMessage(describeFailure(result, 'Could not save this memory.')); await loadMemories(); } } + } catch (error) { + if (isMountedRef.current) { + setErrorMessage(describeFailure(error, 'Could not save this memory.')); + } } finally { if (isMountedRef.current) setSaving(false); } @@ -59,9 +89,21 @@ function FeedbackMemoryPanel({ className = '', refreshToken }) { const handleDelete = async (id) => { if (!id) return; - await window.electronAPI?.suggestions?.deleteFeedbackMemory?.(id); - if (isMountedRef.current) { - setMemories((prev) => prev.filter((entry) => entry.id !== id)); + setErrorMessage(null); + try { + const result = await window.electronAPI?.suggestions?.deleteFeedbackMemory?.(id); + if (!isMountedRef.current) return; + // `removed: false` means the store no longer had it, so dropping the + // row is still right. Any other failure leaves the row in place. + if (result?.success) { + setMemories((prev) => prev.filter((entry) => entry.id !== id)); + } else { + setErrorMessage(describeFailure(result, 'Could not delete this memory.')); + } + } catch (error) { + if (isMountedRef.current) { + setErrorMessage(describeFailure(error, 'Could not delete this memory.')); + } } }; @@ -73,22 +115,37 @@ function FeedbackMemoryPanel({ className = '', refreshToken }) { const cancelEditing = () => { setEditingId(null); setEditingText(''); + setErrorMessage(null); }; const handleUpdate = async () => { const trimmed = editingText.trim(); if (!editingId || !trimmed) return; - const result = await window.electronAPI?.suggestions?.updateFeedbackMemory?.( - editingId, - trimmed - ); - if (isMountedRef.current) { - if (result?.success && result.item) { - setMemories((prev) => prev.map((entry) => (entry.id === editingId ? result.item : entry))); + setErrorMessage(null); + try { + const result = await window.electronAPI?.suggestions?.updateFeedbackMemory?.( + editingId, + trimmed + ); + if (!isMountedRef.current) return; + if (result?.success) { + if (result.item) { + setMemories((prev) => + prev.map((entry) => (entry.id === editingId ? result.item : entry)) + ); + } else { + // Saved against an entry that no longer exists: show the real list. + await loadMemories(); + } + cancelEditing(); } else { - await loadMemories(); + // Keep the editor open with the user's text so the edit is not lost. + setErrorMessage(describeFailure(result, 'Could not save this change.')); + } + } catch (error) { + if (isMountedRef.current) { + setErrorMessage(describeFailure(error, 'Could not save this change.')); } - cancelEditing(); } }; @@ -117,18 +174,25 @@ function FeedbackMemoryPanel({ className = '', refreshToken }) { placeholder='e.g., "All .stl files go to 3D Prints"' className="w-full text-sm" rows={2} + maxLength={MAX_MEMORY_LENGTH} /> + {errorMessage && ( + + {errorMessage} + + )} + {loading ? ( Loading memories... @@ -157,9 +221,15 @@ function FeedbackMemoryPanel({ className = '', refreshToken }) { onChange={(event) => setEditingText(event.target.value)} className="w-full text-sm" rows={2} + maxLength={MAX_MEMORY_LENGTH} />
- @@ -95,7 +107,8 @@ VirtualizedFileRow.propTypes = { selectedFiles: PropTypes.instanceOf(Set).isRequired, handleToggleFile: PropTypes.func.isRequired, openFile: PropTypes.func.isRequired, - revealFile: PropTypes.func.isRequired + revealFile: PropTypes.func.isRequired, + redactPaths: PropTypes.bool.isRequired }).isRequired }; @@ -119,19 +132,25 @@ export default function TriageModal({ const [moveError, setMoveError] = useState(''); const { openFile, revealFile } = useFileActions(setMoveError); const prevIsOpenRef = useRef(false); - const containerRef = useRef(null); + const redactPaths = useSelector(selectRedactPaths); + // The container node lives in state (callback ref) rather than a ref read on + // [isOpen]: Modal mounts its children a render after isOpen flips and keeps + // them through the exit animation, so an isOpen-keyed effect saw a null node + // on open and observed the dying node on close, which then reported 0 height. + const [containerEl, setContainerEl] = useState(null); const [listHeight, setListHeight] = useState(400); useEffect(() => { - if (!containerRef.current) return; + if (!containerEl || typeof ResizeObserver === 'undefined') return undefined; const observer = new ResizeObserver((entries) => { for (const entry of entries) { - setListHeight(entry.contentRect.height); + // A detached node reports 0x0; keep the last real height instead. + if (entry.contentRect.height > 0) setListHeight(entry.contentRect.height); } }); - observer.observe(containerRef.current); + observer.observe(containerEl); return () => observer.disconnect(); - }, [isOpen]); + }, [containerEl]); useEffect(() => { const wasOpen = prevIsOpenRef.current; @@ -193,7 +212,7 @@ export default function TriageModal({ error: error?.message, stack: error?.stack }); - setMoveError(error?.message || 'Failed to move files. Please try again.'); + setMoveError(error?.message || 'Failed to move files. Try again.'); } finally { setIsMoving(false); } @@ -208,10 +227,11 @@ export default function TriageModal({ selectedFiles, handleToggleFile, openFile, - revealFile + revealFile, + redactPaths } }), - [files, selectedFiles, handleToggleFile, openFile, revealFile] + [files, selectedFiles, handleToggleFile, openFile, revealFile, redactPaths] ); return ( @@ -284,7 +304,7 @@ export default function TriageModal({
{/* File List */} -
+
{files.length > 0 ? ( {suggestion.displayName} {suggestion.earlierCount > 0 ? ` (${suggestion.earlierCount} analyzed earlier)` - : ''}{' '} - — want a Smart Folder for them? + : ''} + . Want a Smart Folder for them? ) : ( diff --git a/src/renderer/components/search/AtlasTabContent.jsx b/src/renderer/components/search/AtlasTabContent.jsx index 3126cc63..8680b11a 100644 --- a/src/renderer/components/search/AtlasTabContent.jsx +++ b/src/renderer/components/search/AtlasTabContent.jsx @@ -179,7 +179,7 @@ function AtlasTabContent({ isActive, openFile, revealFile, setError, simpleMode const api = /** @type {any} */ (window).electronAPI?.atlas; if (!api?.getProjection) { setStatus('error'); - setBothErrors('Atlas is unavailable — the projection service did not load.'); + setBothErrors('Atlas is unavailable. The projection service did not load.'); return; } // Generation token: a newer load (e.g. a forced rebuild racing the @@ -240,7 +240,7 @@ function AtlasTabContent({ isActive, openFile, revealFile, setError, simpleMode } else if (pageSignature !== fitSignature) { restarts += 1; if (restarts > 3) { - throw new Error('Atlas projection kept changing while loading — try again'); + throw new Error('Atlas projection kept changing while loading. Try again.'); } offset = 0; total = 0; @@ -1261,7 +1261,7 @@ function AtlasTabContent({ isActive, openFile, revealFile, setError, simpleMode {status === 'rebuilding' - ? 'Embeddings are rebuilding — the map will refresh shortly.' + ? 'Embeddings are rebuilding. The map will refresh shortly.' : 'Projecting your library…'}
diff --git a/src/renderer/components/search/BridgeAnalysisModal.jsx b/src/renderer/components/search/BridgeAnalysisModal.jsx index c65ef49d..d48e65c5 100644 --- a/src/renderer/components/search/BridgeAnalysisModal.jsx +++ b/src/renderer/components/search/BridgeAnalysisModal.jsx @@ -220,7 +220,7 @@ export default function BridgeAnalysisModal({ } title={ selectedBridge.centroidOnly - ? 'No concrete files to move — this bridge is a topical overlap only.' + ? 'No concrete files to move. This bridge is a topical overlap only.' : `Move ${selectedBridge.bridgeFiles.length} bridge file${selectedBridge.bridgeFiles.length === 1 ? '' : 's'} into a folder of your choice.` } onClick={() => diff --git a/src/renderer/components/search/ChatPanel.jsx b/src/renderer/components/search/ChatPanel.jsx index bb003091..b0b7e003 100644 --- a/src/renderer/components/search/ChatPanel.jsx +++ b/src/renderer/components/search/ChatPanel.jsx @@ -1064,8 +1064,7 @@ function ChatPanel({ > {messages.length === 0 ? ( - Ask me anything about your documents — search by meaning, summarize, or explore - connections. + Ask about your documents. You can search by meaning, summarize, or explore connections. ) : null} diff --git a/src/renderer/components/search/ClusterNode.jsx b/src/renderer/components/search/ClusterNode.jsx index 4516bc31..25ca66aa 100644 --- a/src/renderer/components/search/ClusterNode.jsx +++ b/src/renderer/components/search/ClusterNode.jsx @@ -316,10 +316,10 @@ const ClusterNode = memo(({ data, selected }) => { style={{ margin: '-6px' }} title={ confidence === 'high' - ? 'High confidence — AI generated a descriptive label from file content' + ? 'High confidence: AI generated a descriptive label from file content' : confidence === 'medium' - ? 'Medium confidence — label derived from shared tags or categories' - : 'Low confidence — label auto-generated from file count only' + ? 'Medium confidence: label derived from shared tags or categories' + : 'Low confidence: label auto-generated from file count only' } aria-label={`Cluster confidence: ${confidence}`} /> diff --git a/src/renderer/components/search/ComparisonTable.jsx b/src/renderer/components/search/ComparisonTable.jsx index d2316d45..2b115376 100644 --- a/src/renderer/components/search/ComparisonTable.jsx +++ b/src/renderer/components/search/ComparisonTable.jsx @@ -81,8 +81,8 @@ export default function ComparisonTable({ documentAnswer, sources, onOpenSource
{onlyDoc - ? `Only one document matched (${onlyDoc.name}). Comparison needs at least two — broaden the scope or try a less specific query.` - : 'No documents matched. Comparison needs at least two — broaden the scope or try a less specific query.'} + ? `Only one document matched (${onlyDoc.name}). Comparison needs at least two. Broaden the scope or try a less specific query.` + : 'No documents matched. Comparison needs at least two. Broaden the scope or try a less specific query.'}
diff --git a/src/renderer/components/search/EmptyEmbeddingsBanner.jsx b/src/renderer/components/search/EmptyEmbeddingsBanner.jsx index 08d635f5..e316b7da 100644 --- a/src/renderer/components/search/EmptyEmbeddingsBanner.jsx +++ b/src/renderer/components/search/EmptyEmbeddingsBanner.jsx @@ -57,7 +57,7 @@ function EmptyEmbeddingsBanner({ ? `${mismatchDetails} Rebuild the search index to restore reliable semantic search.` : `${mismatchDetails} Rebuild embeddings to restore reliable semantic search.` : needsFileEmbeddingRebuild - ? 'You already have analyzed files, but the embedding index is empty or incomplete. Run a file embedding rebuild from analysis history to restore full Knowledge OS coverage.' + ? 'You already have analyzed files, but the embedding index is empty or incomplete. Rebuild file embeddings from analysis history to restore full Knowledge OS coverage.' : isManual ? simpleMode ? 'Knowledge OS search needs a local search index, but your settings are set to Manual only. Turn automatic indexing back on (Settings -> Embedding behavior), then rebuild to populate the index.' diff --git a/src/renderer/components/search/EmptySearchState.jsx b/src/renderer/components/search/EmptySearchState.jsx index 6114cbf0..acd6670b 100644 --- a/src/renderer/components/search/EmptySearchState.jsx +++ b/src/renderer/components/search/EmptySearchState.jsx @@ -87,7 +87,7 @@ const EmptySearchState = memo(function EmptySearchState({ tone="error" size="lg" title="Search is offline" - description="The local AI engine or vector database is currently unavailable. Please check your model downloads in Settings or restart the app." + description="The local AI engine or vector database is currently unavailable. Check your model downloads in Settings or restart the app." action={ onOpenSettings && (
diff --git a/src/renderer/components/search/GapAnalysisCard.jsx b/src/renderer/components/search/GapAnalysisCard.jsx index 28fc8721..fbc634ca 100644 --- a/src/renderer/components/search/GapAnalysisCard.jsx +++ b/src/renderer/components/search/GapAnalysisCard.jsx @@ -170,8 +170,8 @@ export default function GapAnalysisCard({ sources, onSend }) {
- Topics with only 1 source may benefit from additional documents. Click any topic to - explore it further. + Topics with only 1 source could use more documents. Click any topic to explore it + further.
diff --git a/src/renderer/components/search/GraphTabContent.jsx b/src/renderer/components/search/GraphTabContent.jsx index a2fb3acb..ec9cdb43 100644 --- a/src/renderer/components/search/GraphTabContent.jsx +++ b/src/renderer/components/search/GraphTabContent.jsx @@ -244,7 +244,7 @@ export default function GraphTabContent({ tone="error" size="lg" title="Graph is offline" - description="The local AI engine or vector database is currently unavailable. Please check your model downloads in Settings or restart the app." + description="The local AI engine or vector database is currently unavailable. Check your model downloads in Settings or restart the app." action={ {' '} + instead )} diff --git a/src/renderer/components/search/SimilarityEdge.jsx b/src/renderer/components/search/SimilarityEdge.jsx index 9af169b9..152400c7 100644 --- a/src/renderer/components/search/SimilarityEdge.jsx +++ b/src/renderer/components/search/SimilarityEdge.jsx @@ -555,7 +555,7 @@ const SimilarityEdge = memo(
{data?.bridgeCount > 0 - ? 'These documents sit in both topic groups — handy for shared clients, projects, or cross-folder duplicates.' + ? 'These documents sit in both topic groups (handy for shared clients, projects, or cross-folder duplicates).' : 'These topic groups overlap by meaning, even when no single file spans both.'}
diff --git a/src/renderer/components/search/TimelineTabContent.jsx b/src/renderer/components/search/TimelineTabContent.jsx index f204e964..bef2d884 100644 --- a/src/renderer/components/search/TimelineTabContent.jsx +++ b/src/renderer/components/search/TimelineTabContent.jsx @@ -170,7 +170,7 @@ export default function TimelineTabContent({ {isEmpty && (
- No analyzed files yet — analyze files to build your timeline. + No analyzed files yet. Analyze files to build your timeline.
)} diff --git a/src/renderer/components/search/UnifiedSearchModal.jsx b/src/renderer/components/search/UnifiedSearchModal.jsx index ef583e10..3981f6e7 100644 --- a/src/renderer/components/search/UnifiedSearchModal.jsx +++ b/src/renderer/components/search/UnifiedSearchModal.jsx @@ -184,6 +184,7 @@ export default function UnifiedSearchModal({ focusedResultIndex, setFocusedResultIndex, viewMode, + navigableResults, selectedSearchResultPath, triggerSearchRefresh, resetSearchState @@ -1021,10 +1022,11 @@ export default function UnifiedSearchModal({ // Keyboard Shortcuts // ============================================================================ - // Reset focused index when search results change + // Reset focused index when search results change, or when the view mode + // changes, since the index points into the order on screen. useEffect(() => { setFocusedResultIndex(-1); - }, [searchResults, setFocusedResultIndex]); + }, [searchResults, viewMode, setFocusedResultIndex]); // Scroll focused result into view useEffect(() => { @@ -1135,13 +1137,15 @@ export default function UnifiedSearchModal({ const autocompleteOpen = document.querySelector('[role="listbox"]'); if (autocompleteOpen) return; // Let autocomplete handle arrow keys + // Step through the rows in on-screen order (grouped view reorders + // and caps them), not the flat result order. if (e.key === 'ArrowDown') { e.preventDefault(); setFocusedResultIndex((prev) => { - const next = Math.min(prev + 1, searchResults.length - 1); + const next = Math.min(prev + 1, navigableResults.length - 1); // Also select when navigating - if (searchResults[next]) { - setSelectedSearchId(searchResults[next].id); + if (navigableResults[next]) { + setSelectedSearchId(navigableResults[next].id); } return next; }); @@ -1151,8 +1155,8 @@ export default function UnifiedSearchModal({ setFocusedResultIndex((prev) => { const next = Math.max(prev - 1, -1); // Select result or clear selection when at -1 - if (next >= 0 && searchResults[next]) { - setSelectedSearchId(searchResults[next].id); + if (next >= 0 && navigableResults[next]) { + setSelectedSearchId(navigableResults[next].id); } else if (next === -1) { setSelectedSearchId(null); // Clear selection when defocusing } @@ -1161,7 +1165,7 @@ export default function UnifiedSearchModal({ } // Enter to open focused result if (e.key === 'Enter' && focusedResultIndex >= 0 && !e.ctrlKey && !e.metaKey) { - const result = searchResults[focusedResultIndex]; + const result = navigableResults[focusedResultIndex]; const resultPath = getMetadataPath(result, result?.id); if (resultPath) { e.preventDefault(); @@ -1221,6 +1225,7 @@ export default function UnifiedSearchModal({ selectedNodeId, nodes.length, searchResults, + navigableResults, focusedResultIndex, openFile, graphViewEnabled, @@ -1681,7 +1686,7 @@ export default function UnifiedSearchModal({ }} icon={Orbit} label="Atlas" - tooltip="See your whole library as a map — nearby files are similar" + tooltip="See your whole library as a map where nearby files are similar" /> )}
diff --git a/src/renderer/components/search/hooks/useChatTab.js b/src/renderer/components/search/hooks/useChatTab.js index f9e9a710..99c56b2e 100644 --- a/src/renderer/components/search/hooks/useChatTab.js +++ b/src/renderer/components/search/hooks/useChatTab.js @@ -160,7 +160,7 @@ export function useChatTab({ isOpen, searchResults, defaultTopK, openFile }) { const trimmed = typeof text === 'string' ? text.trim() : ''; if (!trimmed) return false; if (chatInFlightRef.current) { - setChatWarning('Please wait for the current response to finish.'); + setChatWarning('Wait for the current response to finish.'); return false; } @@ -279,7 +279,7 @@ export function useChatTab({ isOpen, searchResults, defaultTopK, openFile }) { streamingFlushIntervalRef.current = null; pendingStreamChunkRef.current = ''; logger.warn('[KnowledgeOS] Chat stream timed out — no activity from main process'); - setChatError('The response timed out. Please try again.'); + setChatError('The response timed out. Try again.'); setChatStatus(''); setChatWarning(''); setChatMessages((prev) => { @@ -514,7 +514,7 @@ export function useChatTab({ isOpen, searchResults, defaultTopK, openFile }) { logger.debug('[UnifiedSearchModal] Failed to save chat response mode', { error: err?.message || String(err) }); - setChatWarning('Failed to save chat response mode. Please try again.'); + setChatWarning('Failed to save chat response mode. Try again.'); }); }, []); @@ -528,7 +528,7 @@ export function useChatTab({ isOpen, searchResults, defaultTopK, openFile }) { logger.debug('[UnifiedSearchModal] Failed to save chat persona', { error: err?.message || String(err) }); - setChatWarning('Failed to save chat persona. Please try again.'); + setChatWarning('Failed to save chat persona. Try again.'); }); }, [savedCustomPersonas] @@ -663,7 +663,7 @@ export function useChatTab({ isOpen, searchResults, defaultTopK, openFile }) { } } catch (e) { logger.error('Failed to resolve scope files', e); - setChatWarning('Could not check those files against the index. Please try again.'); + setChatWarning('Could not check those files against the index. Try again.'); } }, []); diff --git a/src/renderer/components/search/hooks/useGraphTab.js b/src/renderer/components/search/hooks/useGraphTab.js index f11473e2..812bdd3b 100644 --- a/src/renderer/components/search/hooks/useGraphTab.js +++ b/src/renderer/components/search/hooks/useGraphTab.js @@ -1073,7 +1073,7 @@ export function useGraphTab( const handleClearFocus = useCallback(() => { setFocusNodeId(null); setSearchWithinScope(null); - setGraphStatus('Focus cleared - showing all nodes'); + setGraphStatus('Focus cleared. Showing all nodes.'); }, []); const handleExpandAllClusters = useCallback(async () => { @@ -2049,7 +2049,8 @@ export function useGraphTab( const end = cluster?.timeRange?.end; const startLabel = formatDateLabel(start); const endLabel = formatDateLabel(end); - if (startLabel && endLabel && startLabel !== endLabel) return `${startLabel} – ${endLabel}`; + if (startLabel && endLabel && startLabel !== endLabel) + return `${startLabel} to ${endLabel}`; if (endLabel) return endLabel; return fallback; } @@ -4495,10 +4496,21 @@ export function useGraphTab( logger.warn('Some duplicates could not be trashed', { failed: result.failed }); - setError( - `Trashed ${trashedPaths.length} of ${filesToDelete.length}. ` + - `${result.failed.length} failed — see logs for details.` - ); + const onShare = result.failed.filter( + (/** @type {any} */ f) => f?.code === 'NETWORK_NO_RECYCLE_BIN' + ).length; + const otherFailed = result.failed.length - onShare; + const parts = [`Trashed ${trashedPaths.length} of ${filesToDelete.length}.`]; + if (onShare > 0) { + parts.push( + `Left ${onShare} on a network drive, where Windows has no Recycle Bin. ` + + 'Delete those yourself if you are sure.' + ); + } + if (otherFailed > 0) { + parts.push(`${otherFailed} failed. See the logs for details.`); + } + setError(parts.join(' ')); } // Remove successfully-trashed file nodes from the live graph. diff --git a/src/renderer/components/search/hooks/useSearchTab.js b/src/renderer/components/search/hooks/useSearchTab.js index 46d0899a..dd9c4fb6 100644 --- a/src/renderer/components/search/hooks/useSearchTab.js +++ b/src/renderer/components/search/hooks/useSearchTab.js @@ -29,6 +29,9 @@ import { const logger = createLogger('useSearchTab'); +// Cap rows per category in grouped view (full counts still shown in header) +export const GROUPED_VIEW_MAX_PER_CATEGORY = 50; + const ENABLE_SEARCH_TRACE_LOGS = process.env.NODE_ENV === 'development'; /** Per-renderer sequence for the abortKey tagging each search-tab invoke. */ @@ -84,6 +87,17 @@ export function useSearchTab({ isOpen, activeTab, defaultTopK, error, setError } const [searchRefreshTrigger, setSearchRefreshTrigger] = useState(0); const [focusedResultIndex, setFocusedResultIndex] = useState(-1); const [viewMode, setViewMode] = useState('all'); // 'all' or 'grouped' + // Query the user chose to search exactly as typed (spelling correction + // off), with the corrected text it replaced so the notice can offer it back. + const [exactSearchState, setExactSearchState] = useState( + /** @type {{ query: string, corrected: string } | null} */ (null) + ); + + // Only the query it was chosen for is searched exactly; any other query + // gets spelling correction again. + const exactSearch = + exactSearchState && exactSearchState.query === debouncedQuery ? exactSearchState : null; + const exactQuery = exactSearch?.query ?? null; const lastSearchRef = useRef(0); const searchRequestCounterRef = useRef(0); @@ -170,7 +184,7 @@ export function useSearchTab({ isOpen, activeTab, defaultTopK, error, setError } const searchPromise = window.electronAPI?.embeddings?.search?.(q, { topK: defaultTopK, mode: 'hybrid', - correctSpelling: true, + correctSpelling: q !== exactQuery, expandSynonyms: true, abortKey }); @@ -199,9 +213,7 @@ export function useSearchTab({ isOpen, activeTab, defaultTopK, error, setError } if (invokeInFlight && !cancelled && lastSearchRef.current === requestId) { abortInFlightSearch(abortKey, 'search-timeout'); } - reject( - new Error('Search timed out. The index may still be loading — please try again.') - ); + reject(new Error('Search timed out. The index may still be loading. Try again.')); }, TIMEOUTS.SEMANTIC_QUERY); }) ]); @@ -276,7 +288,22 @@ export function useSearchTab({ isOpen, activeTab, defaultTopK, error, setError } }; // searchRefreshTrigger triggers re-search when files are moved/deleted // eslint-disable-next-line react-hooks/exhaustive-deps -- setError is a stable setState-backed callback - }, [debouncedQuery, isOpen, defaultTopK, activeTab, searchRefreshTrigger]); + }, [debouncedQuery, isOpen, defaultTopK, activeTab, searchRefreshTrigger, exactQuery]); + + // "Search for instead": re-run the current query without spelling + // correction. + const searchExactly = useCallback(() => { + if (!debouncedQuery || !queryMeta?.corrections?.length) return; + setExactSearchState({ + query: debouncedQuery, + corrected: typeof queryMeta.corrected === 'string' ? queryMeta.corrected : '' + }); + }, [debouncedQuery, queryMeta]); + + // Undo searchExactly: search the current query with correction again. + const searchWithCorrection = useCallback(() => { + setExactSearchState(null); + }, []); const searchStatusLabel = useMemo(() => { if (isSearching) return 'Searching...'; @@ -310,12 +337,24 @@ export function useSearchTab({ isOpen, activeTab, defaultTopK, error, setError } return sortedGroups; }, [searchResults]); - // Pre-compute ID-to-index map for O(1) lookup in grouped view + // Results in the order the list renders them. Grouped view shows them by + // category, each capped, so keyboard focus (focusedResultIndex) indexes this + // list: the highlight follows the rows on screen and never lands on a + // result the cap hides. + const navigableResults = useMemo(() => { + if (viewMode !== 'grouped') return searchResults; + return Object.values(groupedResults).flatMap((results) => + results.slice(0, GROUPED_VIEW_MAX_PER_CATEGORY) + ); + }, [viewMode, searchResults, groupedResults]); + + // Pre-compute ID-to-position map (into navigableResults) for O(1) lookup in + // grouped view const resultIdToIndex = useMemo(() => { const map = new Map(); - searchResults.forEach((r, i) => map.set(r.id, i)); + navigableResults.forEach((r, i) => map.set(r.id, i)); return map; - }, [searchResults]); + }, [navigableResults]); // Reset to a clean slate for a fresh open (called from the modal's // open-reset effect so the reset ordering stays exactly as before). @@ -328,6 +367,7 @@ export function useSearchTab({ isOpen, activeTab, defaultTopK, error, setError } setQueryMeta(null); setSearchMeta(null); setBulkSelectedIds(new Set()); + setExactSearchState(null); }, []); return { @@ -342,6 +382,9 @@ export function useSearchTab({ isOpen, activeTab, defaultTopK, error, setError } isSearching, queryMeta, searchMeta, + exactSearch, + searchExactly, + searchWithCorrection, bulkSelectedIds, setBulkSelectedIds, focusedResultIndex, @@ -354,6 +397,7 @@ export function useSearchTab({ isOpen, activeTab, defaultTopK, error, setError } selectedSearchResultName, searchStatusLabel, groupedResults, + navigableResults, resultIdToIndex, triggerSearchRefresh, resetSearchState diff --git a/src/renderer/components/search/utils/searchResultUtils.js b/src/renderer/components/search/utils/searchResultUtils.js index 1641f0d9..4b832463 100644 --- a/src/renderer/components/search/utils/searchResultUtils.js +++ b/src/renderer/components/search/utils/searchResultUtils.js @@ -35,11 +35,11 @@ export function getErrorMessage(/** @type {any} */ error, context = 'Operation') msg.includes('Vector DB') || msg.includes('not available yet') ) { - return `${context} failed: Knowledge Graph is initializing. Please wait a moment and try again.`; + return `${context} failed: Knowledge Graph is initializing. Wait a moment and try again.`; } if (msg.includes('Embedding model changed') || msg.includes('dimension mismatch')) { - return 'Embedding model changed. Please rebuild your index in Settings > Embeddings maintenance to use the new model.'; + return 'Embedding model changed. Rebuild your index in Settings → Embeddings maintenance to use the new model.'; } return msg || `${context} failed`; diff --git a/src/renderer/components/settings/AIProviderConfigModal.jsx b/src/renderer/components/settings/AIProviderConfigModal.jsx index b7418195..380aef9a 100644 --- a/src/renderer/components/settings/AIProviderConfigModal.jsx +++ b/src/renderer/components/settings/AIProviderConfigModal.jsx @@ -24,9 +24,9 @@ const PROVIDER_LABELS = { }; const ANTHROPIC_MODELS = [ - { value: 'claude-opus-5-5', label: 'Claude Opus 5.5 — best quality (~$4/M input)' }, - { value: 'claude-sonnet-5-5', label: 'Claude Sonnet 5.5 — balanced (~$2/M input)' }, - { value: 'claude-haiku-5-5', label: 'Claude Haiku 5.5 — fastest (~$0.10/M input)' } + { value: 'claude-opus-5-5', label: 'Claude Opus 5.5 (best quality, ~$4/M input)' }, + { value: 'claude-sonnet-5-5', label: 'Claude Sonnet 5.5 (balanced, ~$2/M input)' }, + { value: 'claude-haiku-5-5', label: 'Claude Haiku 5.5 (fastest, ~$0.10/M input)' } ]; /** @@ -38,7 +38,7 @@ const ANTHROPIC_MODELS = [ function anthropicModelOptions(current) { const saved = String(current ?? '').trim(); if (!saved || ANTHROPIC_MODELS.some((m) => m.value === saved)) return ANTHROPIC_MODELS; - return [{ value: saved, label: `${saved} — older model, currently in use` }, ...ANTHROPIC_MODELS]; + return [{ value: saved, label: `${saved} (older model, currently in use)` }, ...ANTHROPIC_MODELS]; } // Mirrors the Zod ceiling on spendCapMonthlyUsd (ipc/validationSchemas.js). @@ -218,7 +218,7 @@ function AIProviderConfigModal({ provider, onClose, addNotification }) { await dispatch(setProviderKey({ provider, key, allowPlaintext })).unwrap(); setKeyInput(''); addNotification?.( - allowPlaintext ? 'API key saved (unencrypted — see warning).' : 'API key saved.', + allowPlaintext ? 'API key saved (unencrypted, see warning).' : 'API key saved.', allowPlaintext ? 'warning' : 'success' ); }; @@ -386,7 +386,7 @@ function AIProviderConfigModal({ provider, onClose, addNotification }) { // Save again is now a plain config save on an off provider, which // offers the consent prompt and stores the whole form. addNotification?.( - 'Provider turned off. Your other changes need cloud AI to be acknowledged first — press Save again to store them.', + 'Provider turned off. Your other changes need cloud AI to be acknowledged first. Press Save again to store them.', 'warning' ); } else { diff --git a/src/renderer/components/settings/AIProviderSection.jsx b/src/renderer/components/settings/AIProviderSection.jsx index 31db8ebe..8423d480 100644 --- a/src/renderer/components/settings/AIProviderSection.jsx +++ b/src/renderer/components/settings/AIProviderSection.jsx @@ -70,7 +70,7 @@ function AIProviderSection({ addNotification }) { const formatError = (err) => { if (err?.code === 'CONSENT_REQUIRED' || err?.message === 'CONSENT_REQUIRED') { - return 'Cloud AI consent is required. Please accept the privacy notice to continue.'; + return 'Cloud AI consent is required. Accept the privacy notice to continue.'; } return err?.message || 'Something went wrong.'; }; diff --git a/src/renderer/components/settings/AutoOrganizeSection.jsx b/src/renderer/components/settings/AutoOrganizeSection.jsx index e3b14caa..c48758d0 100644 --- a/src/renderer/components/settings/AutoOrganizeSection.jsx +++ b/src/renderer/components/settings/AutoOrganizeSection.jsx @@ -86,11 +86,11 @@ function AutoOrganizeSection({ settings, setSettings }) { className="w-full" > - - - + + + {settings.smartFolderRoutingMode !== 'llm' && @@ -123,8 +123,8 @@ function AutoOrganizeSection({ settings, setSettings }) { {confidenceOutOfSliderRange && ( The saved value ({confidencePercent}%) is outside the slider's{' '} - {Math.round(CONFIDENCE_MIN * 100)}–{Math.round(CONFIDENCE_MAX * 100)}% range. It still - applies as saved; moving the slider will replace it. + {Math.round(CONFIDENCE_MIN * 100)} to {Math.round(CONFIDENCE_MAX * 100)}% range. It + still applies as saved; moving the slider will replace it. )}