From 640b5e3d106070002d442d6fc34cae9d8c7455e0 Mon Sep 17 00:00:00 2001 From: Yuri Karamian Date: Mon, 28 Sep 2026 07:50:46 +0200 Subject: [PATCH] Improve editor performance and harden note saving Replace the source editor with CodeMirror and reduce typing-time work. Virtualize note lists, apply targeted refreshes, and batch search indexing. Improve vault and in-note search, and bound graph layout work. Make saves revision-safe and flush pending changes before navigation or close. Preserve originating-vault ownership across async operations and detached windows. Harden sync, attachment writes, and notebook/tag appearance updates. Keep Trash previews read-only and skip restoring trashed notes. Update AI model presets and isolate streamed responses by request. Fix local image rendering and preserve portable image paths. Include regression tests for the changed behavior. --- .gitignore | 3 - package.json | 9 +- pnpm-lock.yaml | 225 +- src-tauri/Cargo.lock | 5 +- src-tauri/Cargo.toml | 3 + src-tauri/src/ai.rs | 124 +- src-tauri/src/commands.rs | 2487 +++++++++++++---- src-tauri/src/lib.rs | 59 +- src-tauri/src/search/mod.rs | 1518 ++++++++-- src-tauri/src/state.rs | 136 +- src-tauri/src/sync.rs | 524 +++- src-tauri/src/types.rs | 31 +- src-tauri/src/vault/operations.rs | 665 ++++- src-tauri/src/vault/watcher.rs | 328 ++- src/lib/api.ts | 170 +- src/lib/components/AppLayout.svelte | 489 ++-- src/lib/components/Editor.svelte | 1948 ++++++------- src/lib/components/GraphView.svelte | 145 +- src/lib/components/ImageViewer.svelte | 1 + src/lib/components/NoteList.svelte | 547 ++-- src/lib/components/NoteSwitcher.svelte | 4 +- src/lib/components/NoteWindow.svelte | 148 +- src/lib/components/SearchPanel.svelte | 321 ++- src/lib/components/SettingsPanel.svelte | 80 +- src/lib/components/Sidebar.svelte | 234 +- src/lib/components/SourceEditor.svelte | 43 + src/lib/components/TagLabel.svelte | 22 +- src/lib/components/TagStylePicker.svelte | 87 +- src/lib/components/TasksView.svelte | 80 +- src/lib/components/TitleBar.svelte | 11 +- src/lib/components/VaultPicker.svelte | 19 +- src/lib/editor/decorations.ts | 75 + .../extensions/cachedCodeBlockLowlight.ts | 130 + src/lib/editor/search.ts | 77 + src/lib/editor/source/controller.ts | 174 ++ src/lib/editor/source/selectionPairs.ts | 14 - src/lib/stores/app.ts | 72 +- src/lib/types.ts | 20 + src/lib/utils/appearance-mutations.test.mjs | 89 + src/lib/utils/appearance-mutations.ts | 67 + src/lib/utils/graph-layout.test.mjs | 110 + src/lib/utils/graph-layout.ts | 50 + src/lib/utils/note-list-updates.test.mjs | 62 + src/lib/utils/note-list-updates.ts | 43 + src/lib/utils/note-list-window.test.mjs | 61 + src/lib/utils/note-list-window.ts | 65 + src/lib/utils/note-navigation.test.mjs | 78 + src/lib/utils/note-navigation.ts | 15 + src/lib/utils/paths.ts | 16 + src/lib/utils/revision-save.test.mjs | 153 + src/lib/utils/revision-save.ts | 78 + src/lib/utils/tag-styles.test.mjs | 69 - src/lib/utils/tag-styles.ts | 33 - src/lib/utils/time.ts | 28 +- src/lib/utils/window.ts | 6 +- src/routes/+layout.svelte | 9 +- src/routes/+page.svelte | 24 +- tests/code-block-highlighting.test.mjs | 150 + tests/editor-decorations.test.mjs | 43 + tests/in-note-search.test.mjs | 77 + tests/note-link-paths.test.mjs | 26 + tests/source-editor.test.mjs | 102 + tests/title-visibility.test.mjs | 45 - 63 files changed, 9382 insertions(+), 3145 deletions(-) create mode 100644 src/lib/components/SourceEditor.svelte create mode 100644 src/lib/editor/decorations.ts create mode 100644 src/lib/editor/extensions/cachedCodeBlockLowlight.ts create mode 100644 src/lib/editor/search.ts create mode 100644 src/lib/editor/source/controller.ts delete mode 100644 src/lib/editor/source/selectionPairs.ts create mode 100644 src/lib/utils/appearance-mutations.test.mjs create mode 100644 src/lib/utils/appearance-mutations.ts create mode 100644 src/lib/utils/graph-layout.test.mjs create mode 100644 src/lib/utils/graph-layout.ts create mode 100644 src/lib/utils/note-list-updates.test.mjs create mode 100644 src/lib/utils/note-list-updates.ts create mode 100644 src/lib/utils/note-list-window.test.mjs create mode 100644 src/lib/utils/note-list-window.ts create mode 100644 src/lib/utils/note-navigation.test.mjs create mode 100644 src/lib/utils/note-navigation.ts create mode 100644 src/lib/utils/revision-save.test.mjs create mode 100644 src/lib/utils/revision-save.ts create mode 100644 tests/code-block-highlighting.test.mjs create mode 100644 tests/editor-decorations.test.mjs create mode 100644 tests/in-note-search.test.mjs create mode 100644 tests/source-editor.test.mjs diff --git a/.gitignore b/.gitignore index d73cd0a..a468692 100644 --- a/.gitignore +++ b/.gitignore @@ -13,9 +13,6 @@ bun.lockb /.svelte-kit /build -# Local tests -/tests/ - # Nix /result diff --git a/package.json b/package.json index fa52336..e464afa 100644 --- a/package.json +++ b/package.json @@ -35,18 +35,23 @@ "vite": "^7.3.6" }, "dependencies": { + "@codemirror/commands": "^6.11.1", + "@codemirror/lang-markdown": "^6.5.2", + "@codemirror/language": "^6.12.4", + "@codemirror/state": "^6.7.6", + "@codemirror/view": "^6.43.13", + "@lezer/highlight": "^1.2.4", "@tauri-apps/api": "^2.10.1", "@tauri-apps/plugin-dialog": "^2.6.0", "@tauri-apps/plugin-fs": "^2.4.5", "@tauri-apps/plugin-updater": "^2.10.0", "@tiptap/core": "^3.19.0", - "@tiptap/extension-code-block-lowlight": "^3.19.0", + "@tiptap/extension-code-block": "^3.19.0", "@tiptap/extension-color": "^3.19.0", "@tiptap/extension-details": "^3.19.0", "@tiptap/extension-highlight": "^3.19.0", "@tiptap/extension-image": "^3.19.0", "@tiptap/extension-link": "^3.19.0", - "@tiptap/extension-placeholder": "^3.19.0", "@tiptap/extension-subscript": "^3.19.0", "@tiptap/extension-superscript": "^3.19.0", "@tiptap/extension-table": "^3.19.0", diff --git a/pnpm-lock.yaml b/pnpm-lock.yaml index 51c69cb..07c1963 100644 --- a/pnpm-lock.yaml +++ b/pnpm-lock.yaml @@ -11,6 +11,24 @@ importers: .: dependencies: + '@codemirror/commands': + specifier: ^6.11.1 + version: 6.11.1 + '@codemirror/lang-markdown': + specifier: ^6.5.2 + version: 6.5.2 + '@codemirror/language': + specifier: ^6.12.4 + version: 6.12.4 + '@codemirror/state': + specifier: ^6.7.6 + version: 6.7.6 + '@codemirror/view': + specifier: ^6.43.13 + version: 6.43.13 + '@lezer/highlight': + specifier: ^1.2.4 + version: 1.2.4 '@tauri-apps/api': specifier: ^2.10.1 version: 2.10.1 @@ -26,9 +44,9 @@ importers: '@tiptap/core': specifier: ^3.19.0 version: 3.19.0(@tiptap/pm@3.19.0) - '@tiptap/extension-code-block-lowlight': + '@tiptap/extension-code-block': specifier: ^3.19.0 - version: 3.19.0(@tiptap/core@3.19.0(@tiptap/pm@3.19.0))(@tiptap/extension-code-block@3.19.0(@tiptap/core@3.19.0(@tiptap/pm@3.19.0))(@tiptap/pm@3.19.0))(@tiptap/pm@3.19.0)(highlight.js@11.11.1)(lowlight@3.3.0) + version: 3.19.0(@tiptap/core@3.19.0(@tiptap/pm@3.19.0))(@tiptap/pm@3.19.0) '@tiptap/extension-color': specifier: ^3.19.0 version: 3.19.0(@tiptap/extension-text-style@3.19.0(@tiptap/core@3.19.0(@tiptap/pm@3.19.0))) @@ -44,9 +62,6 @@ importers: '@tiptap/extension-link': specifier: ^3.19.0 version: 3.19.0(@tiptap/core@3.19.0(@tiptap/pm@3.19.0))(@tiptap/pm@3.19.0) - '@tiptap/extension-placeholder': - specifier: ^3.19.0 - version: 3.19.0(@tiptap/extensions@3.19.0(@tiptap/core@3.19.0(@tiptap/pm@3.19.0))(@tiptap/pm@3.19.0)) '@tiptap/extension-subscript': specifier: ^3.19.0 version: 3.19.0(@tiptap/core@3.19.0(@tiptap/pm@3.19.0))(@tiptap/pm@3.19.0) @@ -178,6 +193,36 @@ packages: '@chevrotain/types@11.1.2': resolution: {integrity: sha512-U+HFai5+zmJCkK86QsaJtoITlboZHBqrVketcO2ROv865xfCMSFpELQoz1GkX5GzME8pTa+3kbKrZHQtI0gdbw==} + '@codemirror/autocomplete@6.20.3': + resolution: {integrity: sha512-tlosUqb+3BbxCxZdu4tKeRghPFC+QM7q4X5YhKV2eCmPG+1r2F3f4AaSz5sCrFqUtX4Jh20VFTKecl16MgiV9g==} + + '@codemirror/commands@6.11.1': + resolution: {integrity: sha512-O/4hG3SC1YwcmQ0d2UVNDs+AsaNWd1iHVxbTeEBuqH+6bExAiPK3iS/BvpY6rZGURALv4ZD3sIgcCmRvw3ehBg==} + + '@codemirror/lang-css@6.3.1': + resolution: {integrity: sha512-kr5fwBGiGtmz6l0LSJIbno9QrifNMUusivHbnA1H6Dmqy4HZFte3UAICix1VuKo0lMPKQr2rqB+0BkKi/S3Ejg==} + + '@codemirror/lang-html@6.4.12': + resolution: {integrity: sha512-pw2ReWKUqSkbvh76RAT4NYxiogRu+PWkR2ukAwO9uOgrm8uipkzjtKKtNpyeAQwHOqxEeSvAXZ6vr3AfyB9y/w==} + + '@codemirror/lang-javascript@6.2.5': + resolution: {integrity: sha512-zD4e5mS+50htS7F+TYjBPsiIFGanfVqg4HyUz6WNFikgOPf2BgKlx+TQedI1w6n/IqRBVBbBWmGFdLB/7uxO4A==} + + '@codemirror/lang-markdown@6.5.2': + resolution: {integrity: sha512-AwBOdkWYuA//WcM0xO5PfHPUcmz/O2i5o0Nsg1U69SII/loCJlFI1Romd9xp2HYb1kYJRGZotyqRghuHH5n8Kw==} + + '@codemirror/language@6.12.4': + resolution: {integrity: sha512-1q4PaT+o6PbgpkJt4Q8Fv5XJxTy4FUZ4MWETtyiDw3J0Pyr9E2vqcKL+k9wcvjNTIsauxvE7OfmWj3FRPHQ76A==} + + '@codemirror/lint@6.9.7': + resolution: {integrity: sha512-28/+iWLYxKxsvGYhSYL7zaCZqLz5+FFFDq9tVsvGv9kv8RY4fFAchJ5WX9M3YrrRlTIsECjsXPqeNgnSmNP2dg==} + + '@codemirror/state@6.7.6': + resolution: {integrity: sha512-kAz+AncRtKuIknedxT1bq4XwXv4UowhbkHU1myPrtVb/jZtImWuV5BXzv5vK6i3kYACsdiZiQKFQQ5Mq7elW8w==} + + '@codemirror/view@6.43.13': + resolution: {integrity: sha512-sihaFrUzAsYBQsL9J2t69y8nfMQGwcYmggAZsk+kjPbjYZMyuf2hU8tUNTZ+P+isb6XRr8JE22TZlJxBoVdH1A==} + '@esbuild/aix-ppc64@0.28.2': resolution: {integrity: sha512-XExcO+dvLKvVtNTibSTBej1NCAbaGhWn9Ww1ZPx80qsahhPFe/8jgWP0IchNe0F3HwkU7n8ejhH8bjonqht8mQ==} engines: {node: '>=18'} @@ -356,6 +401,30 @@ packages: '@jridgewell/trace-mapping@0.3.31': resolution: {integrity: sha512-zzNR+SdQSDJzc8joaeP8QQoCQr8NuYx2dIIytl1QeBEZHJ9uW6hebsrYgbz8hJwUQao3TWCMtmfV8Nu1twOLAw==} + '@lezer/common@1.5.3': + resolution: {integrity: sha512-H0iErY4e43LpXbYDyBci5W4v/RwTgGV3YzYOlJPiGZ8RY8w51kVE+xLn1tg3Z6UmSfyETvtsUK3r7YsgDQEI7Q==} + + '@lezer/css@1.3.8': + resolution: {integrity: sha512-EJn1zcL9qoDptief6ipWKZKLiOpXkxSe0+t8CH9oiMVcZlq7NBWrjCqnc/41EIjeo/ITj1gFFiATdTkaJDL+Og==} + + '@lezer/highlight@1.2.4': + resolution: {integrity: sha512-4UeRVFO0lsBaM7GA32zOqbmqNPme9jedo8Ey+5c5656MU3MLitMLIA8aCZTYjUMEpEsuSeXEm78Uea6Iq3bvSg==} + + '@lezer/html@1.3.13': + resolution: {integrity: sha512-oI7n6NJml729m7pjm9lvLvmXbdoMoi2f+1pwSDJkl9d68zGr7a9Btz8NdHTGQZtW2DA25ybeuv/SyDb9D5tseg==} + + '@lezer/javascript@1.5.5': + resolution: {integrity: sha512-sWg4yX1J6XW67AaAynVt0iwF0M5c+np36TEu+P2ifAJ8haRYvHnWDV28r1jdwnJehWCwXECutAUy56K4RBZIyg==} + + '@lezer/lr@1.4.10': + resolution: {integrity: sha512-rnCpTIBafOx4mRp43xOxDJbFipJm/c0cia/V5TiGlhmMa+wsSdoGmUN3w5Bqrks/09Q/D4tNAmWaT8p6NRi77A==} + + '@lezer/markdown@1.7.2': + resolution: {integrity: sha512-iTkYvoVcKt3WkeL7qUDyXHONZEwLio4wj8KTNi2dnjQEXBZKMV63BpQrPqfsM+OkvuRbiSTAcycYAsQzLhRNoQ==} + + '@marijn/find-cluster-break@1.0.4': + resolution: {integrity: sha512-Wy0V7+SGUjnF9/TkiM1hKVDPj7jKXduPNboMVtHTA8dySMURWqfg/JZ9E2Sq8JgSJmkl7k7Qe9FLeMSrSraWmQ==} + '@mermaid-js/parser@1.2.0': resolution: {integrity: sha512-oYPyv8A4As1yH5Bx+04iQEQxXuIQDe0GKCNSRgao6z8AM9jixXIfP0vsppRLvGf+nKIOb9/LdpWA4YuJiVvESA==} @@ -764,15 +833,6 @@ packages: peerDependencies: '@tiptap/extension-list': ^3.19.0 - '@tiptap/extension-code-block-lowlight@3.19.0': - resolution: {integrity: sha512-P8O8i1J+XozEVA7bF/Ijwf/r1rVqrh1DBQ7dXxBcrUvLpIGyVjtxX228jBF/kD4kf2xOlphvjDhV2fLa8XOVsg==} - peerDependencies: - '@tiptap/core': ^3.19.0 - '@tiptap/extension-code-block': ^3.19.0 - '@tiptap/pm': ^3.19.0 - highlight.js: ^11 - lowlight: ^2 || ^3 - '@tiptap/extension-code-block@3.19.0': resolution: {integrity: sha512-b/2qR+tMn8MQb+eaFYgVk4qXnLNkkRYmwELQ8LEtEDQPxa5Vl7J3eu8+4OyoIFhZrNDZvvoEp80kHMCP8sI6rg==} peerDependencies: @@ -874,11 +934,6 @@ packages: peerDependencies: '@tiptap/core': ^3.19.0 - '@tiptap/extension-placeholder@3.19.0': - resolution: {integrity: sha512-i15OfgyI4IDCYAcYSKUMnuZkYuUInfanjf9zquH8J2BETiomf/jZldVCp/QycMJ8DOXZ38fXDc99wOygnSNySg==} - peerDependencies: - '@tiptap/extensions': ^3.19.0 - '@tiptap/extension-strike@3.19.0': resolution: {integrity: sha512-xYpabHsv7PccLUBQaP8AYiFCnYbx6P93RHPd0lgNwhdOjYFd931Zy38RyoxPHAgbYVmhf1iyx7lpuLtBnhS5dA==} peerDependencies: @@ -1680,6 +1735,9 @@ packages: resolution: {integrity: sha512-UXWMKhLOwVKb728IUtQPXxfYU+usdybtUrK/8uGE8CQMvrhOpwvzDBwj0QhSL7MQc7vIsISBG8VQ8+IDQxpfQA==} engines: {node: '>=0.10.0'} + style-mod@4.1.4: + resolution: {integrity: sha512-XXWIQt633/EpAFx8aZDOTjBzrCaGmhvEQlQo6MVPfa2OzO2cWo+4hV9h+6UkHYlXGfy+ODXKUdP7Pthmcu5ATw==} + stylis@4.4.0: resolution: {integrity: sha512-5Z9ZpRzfuH6l/UAvCPAPUo3665Nk2wLaZU3x+TLHKVzIz33+sbJqbtrYoC3KD4/uVOr2Zp+L0LySezP9OHV9yA==} @@ -1800,6 +1858,86 @@ snapshots: '@chevrotain/types@11.1.2': {} + '@codemirror/autocomplete@6.20.3': + dependencies: + '@codemirror/language': 6.12.4 + '@codemirror/state': 6.7.6 + '@codemirror/view': 6.43.13 + '@lezer/common': 1.5.3 + + '@codemirror/commands@6.11.1': + dependencies: + '@codemirror/language': 6.12.4 + '@codemirror/state': 6.7.6 + '@codemirror/view': 6.43.13 + '@lezer/common': 1.5.3 + + '@codemirror/lang-css@6.3.1': + dependencies: + '@codemirror/autocomplete': 6.20.3 + '@codemirror/language': 6.12.4 + '@codemirror/state': 6.7.6 + '@lezer/common': 1.5.3 + '@lezer/css': 1.3.8 + + '@codemirror/lang-html@6.4.12': + dependencies: + '@codemirror/autocomplete': 6.20.3 + '@codemirror/lang-css': 6.3.1 + '@codemirror/lang-javascript': 6.2.5 + '@codemirror/language': 6.12.4 + '@codemirror/state': 6.7.6 + '@codemirror/view': 6.43.13 + '@lezer/common': 1.5.3 + '@lezer/css': 1.3.8 + '@lezer/html': 1.3.13 + + '@codemirror/lang-javascript@6.2.5': + dependencies: + '@codemirror/autocomplete': 6.20.3 + '@codemirror/language': 6.12.4 + '@codemirror/lint': 6.9.7 + '@codemirror/state': 6.7.6 + '@codemirror/view': 6.43.13 + '@lezer/common': 1.5.3 + '@lezer/javascript': 1.5.5 + + '@codemirror/lang-markdown@6.5.2': + dependencies: + '@codemirror/autocomplete': 6.20.3 + '@codemirror/lang-html': 6.4.12 + '@codemirror/language': 6.12.4 + '@codemirror/state': 6.7.6 + '@codemirror/view': 6.43.13 + '@lezer/common': 1.5.3 + '@lezer/markdown': 1.7.2 + + '@codemirror/language@6.12.4': + dependencies: + '@codemirror/state': 6.7.6 + '@codemirror/view': 6.43.13 + '@lezer/common': 1.5.3 + '@lezer/highlight': 1.2.4 + '@lezer/lr': 1.4.10 + style-mod: 4.1.4 + + '@codemirror/lint@6.9.7': + dependencies: + '@codemirror/state': 6.7.6 + '@codemirror/view': 6.43.13 + crelt: 1.0.6 + + '@codemirror/state@6.7.6': + dependencies: + '@marijn/find-cluster-break': 1.0.4 + + '@codemirror/view@6.43.13': + dependencies: + '@codemirror/state': 6.7.6 + crelt: 1.0.6 + style-mod: 4.1.4 + w3c-keyname: 2.2.8 + '@esbuild/aix-ppc64@0.28.2': optional: true @@ -1905,6 +2043,41 @@ snapshots: '@jridgewell/resolve-uri': 3.1.2 '@jridgewell/sourcemap-codec': 1.5.5 + '@lezer/common@1.5.3': {} + + '@lezer/css@1.3.8': + dependencies: + '@lezer/common': 1.5.3 + '@lezer/highlight': 1.2.4 + '@lezer/lr': 1.4.10 + + '@lezer/highlight@1.2.4': + dependencies: + '@lezer/common': 1.5.3 + + '@lezer/html@1.3.13': + dependencies: + '@lezer/common': 1.5.3 + '@lezer/highlight': 1.2.4 + '@lezer/lr': 1.4.10 + + '@lezer/javascript@1.5.5': + dependencies: + '@lezer/common': 1.5.3 + '@lezer/highlight': 1.2.4 + '@lezer/lr': 1.4.10 + + '@lezer/lr@1.4.10': + dependencies: + '@lezer/common': 1.5.3 + + '@lezer/markdown@1.7.2': + dependencies: + '@lezer/common': 1.5.3 + '@lezer/highlight': 1.2.4 + + '@marijn/find-cluster-break@1.0.4': {} + '@mermaid-js/parser@1.2.0': dependencies: '@chevrotain/types': 11.1.2 @@ -2195,14 +2368,6 @@ snapshots: dependencies: '@tiptap/extension-list': 3.19.0(@tiptap/core@3.19.0(@tiptap/pm@3.19.0))(@tiptap/pm@3.19.0) - '@tiptap/extension-code-block-lowlight@3.19.0(@tiptap/core@3.19.0(@tiptap/pm@3.19.0))(@tiptap/extension-code-block@3.19.0(@tiptap/core@3.19.0(@tiptap/pm@3.19.0))(@tiptap/pm@3.19.0))(@tiptap/pm@3.19.0)(highlight.js@11.11.1)(lowlight@3.3.0)': - dependencies: - '@tiptap/core': 3.19.0(@tiptap/pm@3.19.0) - '@tiptap/extension-code-block': 3.19.0(@tiptap/core@3.19.0(@tiptap/pm@3.19.0))(@tiptap/pm@3.19.0) - '@tiptap/pm': 3.19.0 - highlight.js: 11.11.1 - lowlight: 3.3.0 - '@tiptap/extension-code-block@3.19.0(@tiptap/core@3.19.0(@tiptap/pm@3.19.0))(@tiptap/pm@3.19.0)': dependencies: '@tiptap/core': 3.19.0(@tiptap/pm@3.19.0) @@ -2286,10 +2451,6 @@ snapshots: dependencies: '@tiptap/core': 3.19.0(@tiptap/pm@3.19.0) - '@tiptap/extension-placeholder@3.19.0(@tiptap/extensions@3.19.0(@tiptap/core@3.19.0(@tiptap/pm@3.19.0))(@tiptap/pm@3.19.0))': - dependencies: - '@tiptap/extensions': 3.19.0(@tiptap/core@3.19.0(@tiptap/pm@3.19.0))(@tiptap/pm@3.19.0) - '@tiptap/extension-strike@3.19.0(@tiptap/core@3.19.0(@tiptap/pm@3.19.0))': dependencies: '@tiptap/core': 3.19.0(@tiptap/pm@3.19.0) @@ -3193,6 +3354,8 @@ snapshots: source-map-js@1.2.1: {} + style-mod@4.1.4: {} + stylis@4.4.0: {} svelte-check@4.3.6(picomatch@4.0.5)(svelte@5.56.9)(typescript@5.9.3): diff --git a/src-tauri/Cargo.lock b/src-tauri/Cargo.lock index cf3b9c0..ad0aaeb 100644 --- a/src-tauri/Cargo.lock +++ b/src-tauri/Cargo.lock @@ -1,6 +1,6 @@ # This file is automatically @generated by Cargo. # It is not intended for manual editing. -version = 3 +version = 4 [[package]] name = "adler2" @@ -1778,6 +1778,7 @@ checksum = "2304e00983f87ffb38b55b444b5e3b60a884b5d30c0fca7d82fe33449bbe55ea" name = "helixnotes" version = "1.3.5" dependencies = [ + "aho-corasick", "arboard", "chrono", "dirs", @@ -1787,6 +1788,7 @@ dependencies = [ "log", "notify", "objc2-app-kit", + "parking_lot", "png 0.17.16", "quick-xml", "rayon", @@ -1809,6 +1811,7 @@ dependencies = [ "tauri-plugin-single-instance", "tauri-plugin-updater", "tauri-plugin-window-state", + "tempfile", "tokio", "urlencoding", "uuid", diff --git a/src-tauri/Cargo.toml b/src-tauri/Cargo.toml index 23f85e0..0eeebac 100644 --- a/src-tauri/Cargo.toml +++ b/src-tauri/Cargo.toml @@ -32,7 +32,9 @@ serde_json = "1" serde_yaml = "0.9" log = "0.4" notify = "8" +parking_lot = "0.12" walkdir = "2" +tempfile = "3" chrono = { version = "0.4", features = ["serde", "unstable-locales"] } sys-locale = "0.3" uuid = { version = "1", features = ["v4"] } @@ -41,6 +43,7 @@ gray_matter = "0.2" tokio = { version = "1", features = ["full"] } dirs = "6" regex = "1" +aho-corasick = "1" zip = { version = "2", default-features = false, features = ["deflate"] } reqwest = { version = "0.12", default-features = false, features = ["json", "stream", "rustls-tls", "blocking"] } futures = "0.3" diff --git a/src-tauri/src/ai.rs b/src-tauri/src/ai.rs index d68472f..53437c4 100644 --- a/src-tauri/src/ai.rs +++ b/src-tauri/src/ai.rs @@ -89,6 +89,7 @@ pub fn ai_request( let _ = app.emit( "ai-stream", AiStreamEvent { + request_id: request_id.clone(), event_type: "error".to_string(), text: None, error: Some(e), @@ -109,13 +110,40 @@ fn normalize_openai_base(base: &str) -> String { .to_string() } +fn openai_request_body( + model: &str, + system_prompt: Option<&str>, + user_message: &str, + stream: bool, + max_tokens: u32, +) -> serde_json::Value { + // GPT-5/6 reject legacy output limits and sampling with their default reasoning. + let reasoning = model.starts_with("gpt-5") || model.starts_with("gpt-6"); + let token_key = if reasoning { + "max_completion_tokens" + } else { + "max_tokens" + }; + let mut messages = Vec::with_capacity(if system_prompt.is_some() { 2 } else { 1 }); + if let Some(system) = system_prompt { + messages.push(json!({ "role": "system", "content": system })); + } + messages.push(json!({ "role": "user", "content": user_message })); + let mut body = + json!({ "model": model, "stream": stream, token_key: max_tokens, "messages": messages }); + if stream && !reasoning { + body["temperature"] = json!(0.7); + } + body +} + async fn stream_anthropic( app: &AppHandle, api_key: &str, model: &str, system_prompt: &str, user_message: &str, - _request_id: &str, + request_id: &str, ) -> Result<(), String> { let client = Client::new(); @@ -168,6 +196,7 @@ async fn stream_anthropic( let _ = app.emit( "ai-stream", AiStreamEvent { + request_id: request_id.to_owned(), event_type: "done".to_string(), text: None, error: None, @@ -185,6 +214,7 @@ async fn stream_anthropic( let _ = app.emit( "ai-stream", AiStreamEvent { + request_id: request_id.to_owned(), event_type: "text".to_string(), text: Some(text.to_string()), error: None, @@ -196,6 +226,7 @@ async fn stream_anthropic( let _ = app.emit( "ai-stream", AiStreamEvent { + request_id: request_id.to_owned(), event_type: "done".to_string(), text: None, error: None, @@ -210,6 +241,7 @@ async fn stream_anthropic( let _ = app.emit( "ai-stream", AiStreamEvent { + request_id: request_id.to_owned(), event_type: "error".to_string(), text: None, error: Some(msg.to_string()), @@ -228,6 +260,7 @@ async fn stream_anthropic( let _ = app.emit( "ai-stream", AiStreamEvent { + request_id: request_id.to_owned(), event_type: "done".to_string(), text: None, error: None, @@ -244,37 +277,11 @@ async fn stream_openai( model: &str, system_prompt: &str, user_message: &str, - _request_id: &str, + request_id: &str, ) -> Result<(), String> { let client = Client::new(); - let is_gpt5 = model.starts_with("gpt-5"); - let token_key = if is_gpt5 { - "max_completion_tokens" - } else { - "max_tokens" - }; - - let mut body = json!({ - "model": model, - "stream": true, - token_key: 4096, - "messages": [ - { - "role": "system", - "content": system_prompt - }, - { - "role": "user", - "content": user_message - } - ] - }); - - // GPT-5 models don't support temperature - if !is_gpt5 { - body["temperature"] = json!(0.7); - } + let body = openai_request_body(model, Some(system_prompt), user_message, true, 4096); let mut req = client.post(url).header("content-type", "application/json"); @@ -312,6 +319,7 @@ async fn stream_openai( let _ = app.emit( "ai-stream", AiStreamEvent { + request_id: request_id.to_owned(), event_type: "done".to_string(), text: None, error: None, @@ -327,6 +335,7 @@ async fn stream_openai( let _ = app.emit( "ai-stream", AiStreamEvent { + request_id: request_id.to_owned(), event_type: "text".to_string(), text: Some(content.to_string()), error: None, @@ -341,6 +350,7 @@ async fn stream_openai( let _ = app.emit( "ai-stream", AiStreamEvent { + request_id: request_id.to_owned(), event_type: "done".to_string(), text: None, error: None, @@ -355,6 +365,7 @@ async fn stream_openai( let _ = app.emit( "ai-stream", AiStreamEvent { + request_id: request_id.to_owned(), event_type: "error".to_string(), text: None, error: Some(err.to_string()), @@ -371,6 +382,7 @@ async fn stream_openai( let _ = app.emit( "ai-stream", AiStreamEvent { + request_id: request_id.to_owned(), event_type: "done".to_string(), text: None, error: None, @@ -445,23 +457,7 @@ async fn test_anthropic(api_key: &str, model: &str) -> Result { async fn test_openai(url: &str, api_key: Option<&str>, model: &str) -> Result { let client = Client::new(); - let is_gpt5 = model.starts_with("gpt-5"); - let token_key = if is_gpt5 { - "max_completion_tokens" - } else { - "max_tokens" - }; - - let body = json!({ - "model": model, - token_key: 20, - "messages": [ - { - "role": "user", - "content": "Hi" - } - ] - }); + let body = openai_request_body(model, None, "Hi", false, 20); let mut req = client.post(url).header("content-type", "application/json"); @@ -483,3 +479,39 @@ async fn test_openai(url: &str, api_key: Option<&str>, model: &str) -> Result, path: &str) { - let search = state.search_index.lock().ok().and_then(|g| g.clone()); +fn index_note_bg(search: Option>, path: &str) { if let Some(search) = search { - let p = path.to_string(); - std::thread::spawn(move || { - let _ = search.index_note(&p); - }); + if let Err(error) = search.queue_index_note(path) { + log::error!("Failed to queue note indexing: {error}"); + } } } -fn remove_note_bg(state: &State<'_, AppState>, path: &str) { - let search = state.search_index.lock().ok().and_then(|g| g.clone()); +fn remove_note_bg(search: Option>, path: &str) { if let Some(search) = search { let p = path.to_string(); std::thread::spawn(move || { - let _ = search.remove_note(&p); + if let Err(error) = search.remove_note(&p) { + log::error!("Failed to remove note from search index: {error}"); + } }); } } -fn clear_vault_runtime(state: &State<'_, AppState>) -> Result<(), String> { - *state.watcher.lock().map_err(|error| error.to_string())? = None; - *state - .search_index - .lock() - .map_err(|error| error.to_string())? = None; - Ok(()) -} - // ── Vault Management ── #[cfg(target_os = "ios")] @@ -65,35 +56,11 @@ fn open_vault_path( state: &State<'_, AppState>, path: String, external: Option<(String, String)>, -) -> Result<(), String> { +) -> Result, String> { operations::ensure_vault_structure(&path)?; // Stage the search index and watcher before replacing the active runtime. - let search = std::sync::Arc::new(SearchIndex::new(&path)?); - #[cfg(target_os = "ios")] - { - if external.is_some() { - search.rebuild(&path)?; - } else { - let search_bg = search.clone(); - let vault = path.clone(); - std::thread::spawn(move || { - let _ = search_bg.rebuild(&vault); - log::info!("mobile: search index rebuild complete"); - }); - } - } - #[cfg(all(mobile, not(target_os = "ios")))] - { - let search_bg = search.clone(); - let vault = path.clone(); - std::thread::spawn(move || { - let _ = search_bg.rebuild(&vault); - log::info!("mobile: search index rebuild complete"); - }); - } - #[cfg(desktop)] - search.rebuild(&path)?; + let search = state.search_index_for_vault(&path)?; let new_watcher = watcher::start_watcher(app.clone(), path.clone())?; asset_scope::allow_vault_assets(&app, Path::new(&path))?; @@ -142,11 +109,18 @@ fn open_vault_path( next.active_vault = Some(path); next.active_bookmark_id = active_bookmark_id; save_app_config(&next)?; - *search_slot = Some(search); - *watcher_slot = Some(new_watcher); + let old_search = search_slot.replace(search.clone()); + let old_watcher = watcher_slot.replace(new_watcher); *config = next; + drop(config); + drop(watcher_slot); + drop(search_slot); + // Watcher shutdown and index disposal may block; never do either while a + // global state slot is locked. + drop(old_watcher); + drop(old_search); - Ok(()) + Ok(search) } #[tauri::command] @@ -156,10 +130,18 @@ pub async fn open_vault( path: String, ) -> Result<(), String> { let _transition = state.vault_transition.lock().await; - open_vault_path(app.clone(), &state, path, None)?; - #[cfg(target_os = "ios")] - app.ios_vault_access().release_active()?; - Ok(()) + tauri::async_runtime::spawn_blocking(move || { + let state = app.state::(); + let search = open_vault_path(app.clone(), &state, path.clone(), None)?; + #[cfg(target_os = "ios")] + app.ios_vault_access().release_active()?; + if let Err(error) = search.queue_vault_refresh(&path) { + log::error!("Failed to queue opened vault search refresh: {error}"); + } + Ok(()) + }) + .await + .map_err(|error| error.to_string())? } #[tauri::command] @@ -180,28 +162,44 @@ pub async fn choose_external_vault( return Ok(None); } let result = ExternalVaultResult::from(selection); - let bookmark_was_registered = state - .config - .lock() - .map_err(|error| error.to_string())? - .vaults - .iter() - .any(|vault| vault.bookmark_id.as_deref() == Some(result.bookmark_id.as_str())); - if let Err(error) = open_vault_path( - app.clone(), - &state, - result.path.clone(), - Some((result.bookmark_id.clone(), result.name.clone())), - ) { - if bookmark_was_registered { - let _ = app.ios_vault_access().rollback_staged(); - } else { - let _ = app.ios_vault_access().forget_bookmark(&result.bookmark_id); + tauri::async_runtime::spawn_blocking(move || { + let state = app.state::(); + let bookmark_was_registered = state + .config + .lock() + .map_err(|error| error.to_string())? + .vaults + .iter() + .any(|vault| vault.bookmark_id.as_deref() == Some(result.bookmark_id.as_str())); + let search = match open_vault_path( + app.clone(), + &state, + result.path.clone(), + Some((result.bookmark_id.clone(), result.name.clone())), + ) { + Ok(search) => search, + Err(error) => { + let rollback = if bookmark_was_registered { + app.ios_vault_access().rollback_staged() + } else { + app.ios_vault_access().forget_bookmark(&result.bookmark_id) + }; + return Err(match rollback { + Ok(()) => error, + Err(rollback_error) => { + format!("{error} Bookmark rollback also failed: {rollback_error}") + } + }); + } + }; + app.ios_vault_access().commit_staged()?; + if let Err(error) = search.queue_vault_refresh(&result.path) { + log::error!("Failed to queue opened vault search refresh: {error}"); } - return Err(error); - } - app.ios_vault_access().commit_staged()?; - Ok(Some(result)) + Ok(Some(result)) + }) + .await + .map_err(|error| error.to_string())? } #[cfg(not(target_os = "ios"))] { @@ -226,17 +224,32 @@ pub async fn restore_external_vault( .await .map_err(|error| error.to_string())??; let result = ExternalVaultResult::from(selection); - if let Err(error) = open_vault_path( - app.clone(), - &state, - result.path.clone(), - Some((result.bookmark_id.clone(), result.name.clone())), - ) { - let _ = app.ios_vault_access().rollback_staged(); - return Err(error); - } - app.ios_vault_access().commit_staged()?; - Ok(result) + tauri::async_runtime::spawn_blocking(move || { + let state = app.state::(); + let search = match open_vault_path( + app.clone(), + &state, + result.path.clone(), + Some((result.bookmark_id.clone(), result.name.clone())), + ) { + Ok(search) => search, + Err(error) => { + return Err(match app.ios_vault_access().rollback_staged() { + Ok(()) => error, + Err(rollback_error) => { + format!("{error} Bookmark rollback also failed: {rollback_error}") + } + }); + } + }; + app.ios_vault_access().commit_staged()?; + if let Err(error) = search.queue_vault_refresh(&result.path) { + log::error!("Failed to queue opened vault search refresh: {error}"); + } + Ok(result) + }) + .await + .map_err(|error| error.to_string())? } #[cfg(not(target_os = "ios"))] { @@ -253,6 +266,27 @@ pub async fn remove_vault( bookmark_id: Option, ) -> Result<(), String> { let _transition = state.vault_transition.lock().await; + tauri::async_runtime::spawn_blocking(move || { + let state = app.state::(); + let config_path = app_config_path()?; + remove_vault_path(&app, &state, path, bookmark_id, &config_path) + }) + .await + .map_err(|error| error.to_string())? +} + +fn remove_vault_path( + app: &AppHandle, + state: &AppState, + path: String, + bookmark_id: Option, + config_path: &Path, +) -> Result<(), String> { + let mut search_slot = state + .search_index + .lock() + .map_err(|error| error.to_string())?; + let mut watcher_slot = state.watcher.lock().map_err(|error| error.to_string())?; let mut config = state.config.lock().map_err(|error| error.to_string())?; let target = if let Some(bookmark_id) = bookmark_id.as_deref() { config @@ -292,12 +326,12 @@ pub async fn remove_vault( next.active_vault = None; next.active_bookmark_id = None; } - save_app_config(&next)?; + save_app_config_to(&next, config_path)?; #[cfg(target_os = "ios")] if let Some(id) = target_bookmark.as_deref() { if let Err(error) = app.ios_vault_access().forget_bookmark(id) { - return match save_app_config(&old) { + return match save_app_config_to(&old, config_path) { Ok(()) => Err(error), Err(rollback_error) => Err(format!( "{error} Configuration rollback also failed: {rollback_error}" @@ -306,12 +340,16 @@ pub async fn remove_vault( } } #[cfg(not(target_os = "ios"))] - let _ = (&app, &target_bookmark); + let _ = (app, &target_bookmark); - if is_active { - clear_vault_runtime(&state)?; - } + let old_search = if is_active { search_slot.take() } else { None }; + let old_watcher = if is_active { watcher_slot.take() } else { None }; *config = next; + drop(config); + drop(watcher_slot); + drop(search_slot); + drop(old_watcher); + drop(old_search); Ok(()) } @@ -328,6 +366,24 @@ pub fn get_pending_open_file(state: State<'_, AppState>) -> Result( + window: tauri::Window, + state: State<'_, AppState>, +) -> Result<(), String> { + #[cfg(desktop)] + if state.should_hide_to_tray( + window.label(), + window.app_handle().tray_by_id("helixnotes").is_some(), + )? { + return window.hide().map_err(|error| error.to_string()); + } + #[cfg(mobile)] + let _ = state; + window.destroy().map_err(|error| error.to_string()) +} + #[tauri::command] pub fn set_theme(state: State<'_, AppState>, theme: String) -> Result<(), String> { let mut config = state.config.lock().map_err(|e| e.to_string())?; @@ -525,59 +581,83 @@ pub fn set_content_width(state: State<'_, AppState>, width: Option) -> Resu // ── Notebooks ── #[tauri::command] -pub fn get_notebooks(state: State<'_, AppState>) -> Result, String> { - let config = state.config.lock().map_err(|e| e.to_string())?; - let vault_path = config.active_vault.as_ref().ok_or("No active vault")?; - operations::scan_notebooks(vault_path) +pub async fn get_notebooks(state: State<'_, AppState>) -> Result, String> { + let vault_path = state + .config + .lock() + .map_err(|error| error.to_string())? + .active_vault + .clone() + .ok_or("No active vault")?; + tauri::async_runtime::spawn_blocking(move || operations::scan_notebooks(&vault_path)) + .await + .map_err(|error| error.to_string())? } #[tauri::command] -pub fn count_root_notes(state: State<'_, AppState>) -> Result { - let config = state.config.lock().map_err(|e| e.to_string())?; - let vault_path = config.active_vault.as_ref().ok_or("No active vault")?; - operations::count_root_notes(vault_path) +pub async fn count_root_notes(state: State<'_, AppState>) -> Result { + let vault_path = state + .config + .lock() + .map_err(|error| error.to_string())? + .active_vault + .clone() + .ok_or("No active vault")?; + tauri::async_runtime::spawn_blocking(move || operations::count_root_notes(&vault_path)) + .await + .map_err(|error| error.to_string())? } #[tauri::command] -pub fn create_notebook( +pub async fn create_notebook( state: State<'_, AppState>, + vault_path: String, parent_relative: Option, name: String, ) -> Result { - let config = state.config.lock().map_err(|e| e.to_string())?; - let vault_path = config.active_vault.as_ref().ok_or("No active vault")?; - operations::create_notebook(vault_path, parent_relative.as_deref(), &name) + state.validate_vault(&vault_path)?; + let _write_guard = state.note_save.lock().await; + operations::create_notebook(&vault_path, parent_relative.as_deref(), &name) } #[tauri::command] -pub fn rename_notebook( +pub async fn rename_notebook( state: State<'_, AppState>, + vault_path: String, path: String, new_name: String, ) -> Result { - let config = state.config.lock().map_err(|error| error.to_string())?; - let vault_path = config.active_vault.as_ref().ok_or("No active vault")?; - operations::rename_notebook(vault_path, &path, &new_name) + let search = state.vault_snapshot(&vault_path)?; + let _write_guard = state.note_save.lock().await; + let renamed = operations::rename_notebook(&vault_path, &path, &new_name)?; + if let Some(search) = search { + if let Err(error) = search.queue_vault_refresh(&vault_path) { + log::error!("Failed to queue renamed notebook search refresh: {error}"); + } + } + Ok(renamed) } #[tauri::command] -pub fn move_notebook( +pub async fn move_notebook( state: State<'_, AppState>, + vault_path: String, notebook_path: String, dest_parent: String, ) -> Result { - let config = state.config.lock().map_err(|e| e.to_string())?; - let vault_path = config.active_vault.as_ref().ok_or("No active vault")?; + let search = state.vault_snapshot(&vault_path)?; + let _write_guard = state.note_save.lock().await; + let vault_path = vault_path.as_str(); let old_relative = Path::new(¬ebook_path) - .strip_prefix(vault_path.as_str()) + .strip_prefix(vault_path) .map(|p| p.to_string_lossy().to_string()) .unwrap_or_default(); let new_full_path = operations::move_notebook(vault_path, ¬ebook_path, &dest_parent)?; let new_relative = Path::new(&new_full_path) - .strip_prefix(vault_path.as_str()) + .strip_prefix(vault_path) .map(|p| p.to_string_lossy().to_string()) .unwrap_or_default(); @@ -596,37 +676,10 @@ pub fn move_notebook( } } - // Update notebook icon mappings - if let Ok(icons) = operations::load_notebook_icons(vault_path) { - let old_icon_key = operations::normalize_notebook_icon_key(&old_relative); - let new_icon_key = operations::normalize_notebook_icon_key(&new_relative); - let old_prefix = format!("{}/", old_icon_key); - let mut new_icons = std::collections::HashMap::new(); - let mut changed = false; - for (key, value) in &icons { - if *key == old_icon_key { - new_icons.insert(new_icon_key.clone(), value.clone()); - changed = true; - } else if key.starts_with(&old_prefix) { - let new_key = format!("{}/{}", new_icon_key, &key[old_prefix.len()..]); - new_icons.insert(new_key, value.clone()); - changed = true; - } else { - new_icons.insert(key.clone(), value.clone()); - } - } - if changed { - let icons_path = operations::helixnotes_dir(vault_path).join("notebook_icons.json"); - if let Ok(data) = serde_json::to_string_pretty(&new_icons) { - let _ = std::fs::write(&icons_path, data); - } - } - } - - // Rebuild search index (paths changed for all contained notes) - if let Ok(search_guard) = state.search_index.lock() { - if let Some(ref search) = *search_guard { - let _ = search.rebuild(vault_path); + // Reconcile renamed paths without blocking the move on a full rebuild. + if let Some(search) = search { + if let Err(error) = search.queue_vault_refresh(vault_path) { + log::error!("Failed to queue moved notebook search refresh: {error}"); } } @@ -634,153 +687,186 @@ pub fn move_notebook( } #[tauri::command] -pub fn delete_notebook(state: State<'_, AppState>, path: String) -> Result<(), String> { - let vault_path = { - let config = state.config.lock().map_err(|e| e.to_string())?; - config - .active_vault - .as_ref() - .ok_or("No active vault")? - .clone() - }; - operations::delete_notebook(&vault_path, &path) +pub async fn delete_notebook( + state: State<'_, AppState>, + vault_path: String, + path: String, +) -> Result<(), String> { + let search = state.vault_snapshot(&vault_path)?; + let _write_guard = state.note_save.lock().await; + operations::delete_notebook(&vault_path, &path)?; + if let Some(search) = search { + if let Err(error) = search.queue_vault_refresh(&vault_path) { + log::error!("Failed to queue deleted notebook search refresh: {error}"); + } + } + Ok(()) } // ── Notes ── #[tauri::command] -pub fn get_notes( +pub async fn get_notes( state: State<'_, AppState>, notebook_path: Option, ) -> Result, String> { - let config = state.config.lock().map_err(|e| e.to_string())?; - let vault_path = config.active_vault.as_ref().ok_or("No active vault")?; - operations::scan_notes(vault_path, notebook_path.as_deref()) + let vault_path = state + .config + .lock() + .map_err(|error| error.to_string())? + .active_vault + .clone() + .ok_or("No active vault")?; + tauri::async_runtime::spawn_blocking(move || { + operations::scan_notes(&vault_path, notebook_path.as_deref()) + }) + .await + .map_err(|error| error.to_string())? } #[tauri::command] -pub fn read_note(state: State<'_, AppState>, path: String) -> Result { - let config = state.config.lock().map_err(|error| error.to_string())?; - let vault_path = config.active_vault.as_ref().ok_or("No active vault")?; - operations::read_note(vault_path, &path) -} - -#[tauri::command] -pub fn save_note( +pub async fn read_note( state: State<'_, AppState>, + vault_path: String, + path: String, +) -> Result { + state.validate_vault(&vault_path)?; + tauri::async_runtime::spawn_blocking(move || operations::read_note(&vault_path, &path)) + .await + .map_err(|error| error.to_string())? +} + +#[tauri::command] +pub async fn save_note( + state: State<'_, AppState>, + vault_path: String, path: String, meta: NoteMeta, body: String, ) -> Result<(), String> { - let config = state.config.lock().map_err(|error| error.to_string())?; - let vault_path = config - .active_vault - .as_ref() - .ok_or("No active vault")? - .clone(); - let max_versions = config.max_versions_per_note; - let old_raw = operations::read_vault_note(&vault_path, &path)?.raw; - drop(config); + let search = state.vault_snapshot(&vault_path)?; + let max_versions = state + .config + .lock() + .map_err(|error| error.to_string())? + .max_versions_per_note; + // Capture the origin before queueing. Waiting must not retarget the write + // when the main window switches vaults or the calling window closes. + let save_guard = state.note_save.clone().lock_owned().await; - let note_id = meta.id.clone(); - let snapshot_vault = vault_path.clone(); - std::thread::spawn(move || { - crate::history::maybe_snapshot(&snapshot_vault, ¬e_id, &old_raw, max_versions); - }); - - operations::save_note(&vault_path, &path, &meta, &body)?; - - // Re-index note so search picks up changes (background to avoid blocking on FUSE fsync) - index_note_bg(&state, &path); - - Ok(()) + tauri::async_runtime::spawn_blocking(move || { + // Keep ordering even if the calling window closes while its save runs. + let _save_guard = save_guard; + if let Some(saved) = operations::save_note(&vault_path, &path, &meta, &body)? { + crate::history::maybe_snapshot( + &vault_path, + &saved.id, + &saved.previous_raw, + max_versions, + ); + index_note_bg(search, &path); + } + Ok(()) + }) + .await + .map_err(|error| error.to_string())? } #[tauri::command] -pub fn duplicate_note( +pub async fn duplicate_note( state: State<'_, AppState>, + vault_path: String, path: String, ) -> Result { - let config = state.config.lock().map_err(|e| e.to_string())?; - let vault = config - .active_vault - .as_ref() - .ok_or("No active vault")? - .clone(); - drop(config); - - let entry = operations::duplicate_note(&path, &vault)?; - index_note_bg(&state, &entry.path); + let search = state.vault_snapshot(&vault_path)?; + let _write_guard = state.note_save.lock().await; + let entry = operations::duplicate_note(&path, &vault_path)?; + index_note_bg(search, &entry.path); Ok(entry) } #[tauri::command] -pub fn create_note( +pub async fn create_note( state: State<'_, AppState>, + vault_path: String, notebook_relative: Option, title: String, ) -> Result { - let config = state.config.lock().map_err(|e| e.to_string())?; - let vault_path = config.active_vault.as_ref().ok_or("No active vault")?; - let entry = operations::create_note(vault_path, notebook_relative.as_deref(), &title)?; + let search = state.vault_snapshot(&vault_path)?; + let _write_guard = state.note_save.lock().await; + let entry = operations::create_note(&vault_path, notebook_relative.as_deref(), &title)?; // Index new note (background to avoid blocking on FUSE fsync) - index_note_bg(&state, &entry.path); + index_note_bg(search, &entry.path); Ok(entry) } #[tauri::command] -pub fn create_daily_note( +pub async fn create_daily_note( state: State<'_, AppState>, + vault_path: String, date: Option, ) -> Result { - let config = state.config.lock().map_err(|e| e.to_string())?; - let vault_path = config.active_vault.as_ref().ok_or("No active vault")?; - let entry = - operations::create_daily_note(vault_path, date.as_deref(), &config.daily_title_format)?; + let search = state.vault_snapshot(&vault_path)?; + let title_format = state + .config + .lock() + .map_err(|error| error.to_string())? + .daily_title_format + .clone(); + let _write_guard = state.note_save.lock().await; + let entry = operations::create_daily_note(&vault_path, date.as_deref(), &title_format)?; - index_note_bg(&state, &entry.path); + index_note_bg(search, &entry.path); Ok(entry) } #[tauri::command] -pub fn rename_note( +pub async fn rename_note( state: State<'_, AppState>, + vault_path: String, path: String, new_title: String, ) -> Result { - let config = state.config.lock().map_err(|e| e.to_string())?; - let vault_path = config - .active_vault - .as_ref() - .ok_or("No active vault")? - .clone(); - drop(config); - operations::rename_note(&path, &new_title, &vault_path) + let search = state.vault_snapshot(&vault_path)?; + let _write_guard = state.note_save.lock().await; + let renamed = operations::rename_note(&path, &new_title, &vault_path)?; + if let Some(search) = search { + if let Err(error) = search.queue_vault_refresh(&vault_path) { + log::error!("Failed to queue renamed note search refresh: {error}"); + } + } + Ok(renamed) } #[tauri::command] -pub fn delete_note(state: State<'_, AppState>, path: String) -> Result<(), String> { - let config = state.config.lock().map_err(|e| e.to_string())?; - let vault_path = config.active_vault.as_ref().ok_or("No active vault")?; - operations::delete_note(vault_path, &path)?; +pub async fn delete_note( + state: State<'_, AppState>, + vault_path: String, + path: String, +) -> Result<(), String> { + let search = state.vault_snapshot(&vault_path)?; + let _write_guard = state.note_save.lock().await; + operations::delete_note(&vault_path, &path)?; // Remove from index (background to avoid blocking on FUSE fsync) - remove_note_bg(&state, &path); + remove_note_bg(search, &path); Ok(()) } #[tauri::command] -pub fn move_note( +pub async fn move_note( state: State<'_, AppState>, + vault_path: String, note_path: String, dest_notebook: String, ) -> Result { - let config = state.config.lock().map_err(|e| e.to_string())?; - let vault_path = config.active_vault.as_ref().ok_or("No active vault")?; + let search = state.vault_snapshot(&vault_path)?; + let _write_guard = state.note_save.lock().await; + let vault_path = vault_path.as_str(); // Compute old relative path before move let old_relative = Path::new(¬e_path) @@ -805,6 +891,8 @@ pub fn move_note( } } } + index_note_bg(search.clone(), ¬e_path); + index_note_bg(search, &new_full_path); Ok(new_full_path) } @@ -812,18 +900,33 @@ pub fn move_note( // ── Tags ── #[tauri::command] -pub fn get_all_tags(state: State<'_, AppState>) -> Result, String> { - let config = state.config.lock().map_err(|e| e.to_string())?; - let vault_path = config.active_vault.as_ref().ok_or("No active vault")?; - operations::get_all_tags(vault_path) +pub async fn get_all_tags(state: State<'_, AppState>) -> Result, String> { + let vault_path = state + .config + .lock() + .map_err(|error| error.to_string())? + .active_vault + .clone() + .ok_or("No active vault")?; + tauri::async_runtime::spawn_blocking(move || operations::get_all_tags(&vault_path)) + .await + .map_err(|error| error.to_string())? } // ── Wiki-links ── #[tauri::command] -pub fn get_all_note_titles(state: State<'_, AppState>) -> Result, String> { - let config = state.config.lock().map_err(|e| e.to_string())?; - let vault_path = config.active_vault.as_ref().ok_or("No active vault")?; +pub async fn get_all_note_titles( + state: State<'_, AppState>, + vault_path: String, +) -> Result, String> { + state.validate_vault(&vault_path)?; + tauri::async_runtime::spawn_blocking(move || scan_all_note_titles(&vault_path)) + .await + .map_err(|error| error.to_string())? +} + +fn scan_all_note_titles(vault_path: &str) -> Result, String> { let vault = std::path::Path::new(vault_path); let hn_dir = operations::helixnotes_dir(vault_path); let mut entries = Vec::new(); @@ -893,11 +996,14 @@ pub async fn get_note_switcher_titles( // ── Graph ── #[tauri::command] -pub fn get_graph_data(state: State<'_, AppState>) -> Result { +pub fn get_graph_data( + state: State<'_, AppState>, + vault_path: String, +) -> Result { use std::collections::{HashMap, HashSet}; - let config = state.config.lock().map_err(|e| e.to_string())?; - let vault_path = config.active_vault.as_ref().ok_or("No active vault")?; + state.validate_vault(&vault_path)?; + let vault_path = vault_path.as_str(); let vault = std::path::Path::new(vault_path); // Pass 1: collect ALL notes as nodes (no title deduplication - every note must appear) @@ -1085,92 +1191,155 @@ fn extract_title_fast(raw: &str) -> Option { // ── Tasks ── #[tauri::command] -pub fn get_tasks(state: State<'_, AppState>) -> Result, String> { - use rayon::prelude::*; - let config = state.config.lock().map_err(|e| e.to_string())?; - let vault_path = config.active_vault.as_ref().ok_or("No active vault")?; - let vault = std::path::Path::new(vault_path); - let hn_dir = operations::helixnotes_dir(vault_path); - - let task_re = regex::Regex::new(r"^\s*[-*]\s\[([ xX])\]\s+(.+?)\s*$").unwrap(); - let due_re = regex::Regex::new(r"\bdue:(\d{4}-\d{2}-\d{2})\b").unwrap(); - let prio_re = regex::Regex::new(r"(?i)(?:^|\s)!(high|medium|med|low)\b").unwrap(); - - let paths: Vec = walkdir::WalkDir::new(vault) - .into_iter() - .filter_entry(|e| !operations::is_hidden(e.path()) && !e.path().starts_with(&hn_dir)) - .filter_map(|e| e.ok()) - .filter(|e| { - e.file_type().is_file() && e.path().extension().and_then(|x| x.to_str()) == Some("md") - }) - .map(|e| e.into_path()) - .collect(); - - // Read and parse each note in parallel; on Android every read_to_string is a FUSE round-trip. - let tasks: Vec = paths - .par_iter() - .flat_map(|path| { - let mut out: Vec = Vec::new(); - let raw = match std::fs::read_to_string(path) { - Ok(r) => r, - Err(_) => return out, - }; - let filename = path - .file_name() - .unwrap_or_default() - .to_string_lossy() - .to_string(); - let (meta, body) = crate::vault::frontmatter::parse_note(&raw, &filename); - let note_path = path.to_string_lossy().to_string(); - for (i, line) in body.lines().enumerate() { - let caps = match task_re.captures(line) { - Some(c) => c, - None => continue, - }; - let completed = !caps[1].trim().is_empty(); - let content = caps[2].to_string(); - let due = due_re.captures(&content).map(|c| c[1].to_string()); - let priority = prio_re.captures(&content).map(|c| { - let p = c[1].to_lowercase(); - if p == "medium" { - "med".to_string() - } else { - p - } - }); - let mut text = due_re.replace_all(&content, "").to_string(); - text = prio_re.replace_all(&text, " ").to_string(); - let text = text.split_whitespace().collect::>().join(" "); - out.push(crate::types::TaskItem { - note_path: note_path.clone(), - note_title: meta.title.clone(), - line: i, - raw_line: line.to_string(), - text, - completed, - due, - priority, - }); - } - out - }) - .collect(); - Ok(tasks) +pub async fn get_tasks( + state: State<'_, AppState>, + note_path: Option, +) -> Result, String> { + let vault_path = state + .config + .lock() + .map_err(|error| error.to_string())? + .active_vault + .clone() + .ok_or("No active vault")?; + tauri::async_runtime::spawn_blocking(move || scan_tasks(&vault_path, note_path.as_deref())) + .await + .map_err(|error| error.to_string())? } -fn read_task_note( - state: &State<'_, AppState>, - note_path: &str, -) -> Result<(String, NoteMeta, String), String> { - let config = state.config.lock().map_err(|error| error.to_string())?; - let vault_path = config - .active_vault - .as_ref() - .ok_or("No active vault")? - .clone(); - drop(config); - let note = operations::read_vault_note(&vault_path, note_path)?; - Ok((vault_path, note.meta, note.content)) +fn scan_tasks(vault_path: &str, note_path: Option<&str>) -> Result, String> { + use rayon::prelude::*; + + if let Some(path) = note_path { + let note = operations::read_vault_note(vault_path, path)?; + return Ok(parse_note_tasks(path, ¬e.meta.title, ¬e.content)); + } + + let vault = Path::new(vault_path); + let hn_dir = operations::helixnotes_dir(vault_path); + let paths: Vec = walkdir::WalkDir::new(vault) + .into_iter() + .filter_entry(|entry| { + !operations::is_hidden(entry.path()) && !entry.path().starts_with(&hn_dir) + }) + .filter_map(|entry| entry.ok()) + .filter(|entry| { + entry.file_type().is_file() + && entry + .path() + .extension() + .and_then(|extension| extension.to_str()) + == Some("md") + }) + .map(|entry| entry.into_path()) + .collect(); + + Ok(paths + .par_iter() + .flat_map(|path| { + let Ok(raw) = std::fs::read_to_string(path) else { + return Vec::new(); + }; + let filename = path.file_name().unwrap_or_default().to_string_lossy(); + let (meta, body) = crate::vault::frontmatter::parse_note(&raw, &filename); + parse_note_tasks(&path.to_string_lossy(), &meta.title, &body) + }) + .collect()) +} + +fn parse_note_tasks(note_path: &str, note_title: &str, body: &str) -> Vec { + use std::sync::LazyLock; + + static TASK_RE: LazyLock = + LazyLock::new(|| regex::Regex::new(r"^\s*[-*]\s\[([ xX])\]\s+(.+?)\s*$").unwrap()); + static DUE_RE: LazyLock = + LazyLock::new(|| regex::Regex::new(r"\bdue:(\d{4}-\d{2}-\d{2})\b").unwrap()); + static PRIORITY_RE: LazyLock = + LazyLock::new(|| regex::Regex::new(r"(?i)(?:^|\s)!(high|medium|med|low)\b").unwrap()); + + let mut tasks = Vec::new(); + for (line_number, line) in body.lines().enumerate() { + let Some(caps) = TASK_RE.captures(line) else { + continue; + }; + let completed = !caps[1].trim().is_empty(); + let content = &caps[2]; + let due = DUE_RE + .captures(content) + .map(|captures| captures[1].to_string()); + let priority = PRIORITY_RE.captures(content).map(|captures| { + let priority = captures[1].to_lowercase(); + if priority == "medium" { + "med".to_string() + } else { + priority + } + }); + let text = DUE_RE.replace_all(content, ""); + let text = PRIORITY_RE.replace_all(&text, " "); + tasks.push(TaskItem { + note_path: note_path.to_string(), + note_title: note_title.to_string(), + line: line_number, + raw_line: line.to_string(), + text: text.split_whitespace().collect::>().join(" "), + completed, + due, + priority, + }); + } + tasks +} + +#[cfg(test)] +mod task_tests { + use super::scan_tasks; + + #[test] + fn targeted_task_scan_limits_results_and_keeps_task_metadata() { + let vault = tempfile::tempdir().unwrap(); + let selected = vault.path().join("Selected.md"); + std::fs::write( + &selected, + "---\ntitle: Selected title\n---\n# Selected title\n\n- [x] Ship !medium due:2026-09-30\n", + ) + .unwrap(); + std::fs::write(vault.path().join("Other.md"), "- [ ] Unrelated task\n").unwrap(); + + let tasks = scan_tasks( + &vault.path().to_string_lossy(), + Some(&selected.to_string_lossy()), + ) + .unwrap(); + + assert_eq!(tasks.len(), 1); + assert_eq!(tasks[0].note_path, selected.to_string_lossy()); + assert_eq!(tasks[0].note_title, "Selected title"); + assert_eq!(tasks[0].line, 2); + assert_eq!(tasks[0].text, "Ship"); + assert_eq!(tasks[0].priority.as_deref(), Some("med")); + assert_eq!(tasks[0].due.as_deref(), Some("2026-09-30")); + assert!(tasks[0].completed); + + std::fs::write(&selected, "No tasks remain.\n").unwrap(); + assert!(scan_tasks( + &vault.path().to_string_lossy(), + Some(&selected.to_string_lossy()), + ) + .unwrap() + .is_empty()); + } + + #[test] + fn targeted_task_scan_rejects_notes_outside_the_active_vault() { + let root = tempfile::tempdir().unwrap(); + let vault = root.path().join("vault"); + std::fs::create_dir(&vault).unwrap(); + let outside = root.path().join("Outside.md"); + std::fs::write(&outside, "- [ ] Outside task\n").unwrap(); + + assert!(scan_tasks(&vault.to_string_lossy(), Some(&outside.to_string_lossy())).is_err()); + } } fn toggle_checkbox_line(line: &str, done: bool) -> String { @@ -1191,14 +1360,21 @@ fn toggle_checkbox_line(line: &str, done: bool) -> String { } #[tauri::command] -pub fn set_task_done( +pub async fn set_task_done( state: State<'_, AppState>, + vault_path: String, note_path: String, line: usize, raw_line: String, done: bool, ) -> Result<(), String> { - let (vault_path, meta, body) = read_task_note(&state, ¬e_path)?; + let search = state.vault_snapshot(&vault_path)?; + let _write_guard = state.note_save.lock().await; + let NoteContent { + meta, + content: body, + .. + } = operations::read_vault_note(&vault_path, ¬e_path)?; let mut lines: Vec = body.lines().map(|l| l.to_string()).collect(); // Verify the expected line; if the note drifted, fall back to the first exact match. let idx = if lines.get(line).map(|l| *l == raw_line).unwrap_or(false) { @@ -1219,9 +1395,9 @@ pub fn set_task_done( if body.ends_with('\n') && !new_body.ends_with('\n') { new_body.push('\n'); } - operations::save_note(&vault_path, ¬e_path, &meta, &new_body)?; - - index_note_bg(&state, ¬e_path); + if operations::save_note(&vault_path, ¬e_path, &meta, &new_body)?.is_some() { + index_note_bg(search, ¬e_path); + } Ok(()) } @@ -1236,8 +1412,9 @@ fn set_priority_on_line(line: &str, priority: Option<&str>) -> String { } #[tauri::command] -pub fn set_task_priority( +pub async fn set_task_priority( state: State<'_, AppState>, + vault_path: String, note_path: String, line: usize, raw_line: String, @@ -1252,7 +1429,13 @@ pub fn set_task_priority( Some(_) => return Err("Invalid priority".to_string()), }; - let (vault_path, meta, body) = read_task_note(&state, ¬e_path)?; + let search = state.vault_snapshot(&vault_path)?; + let _write_guard = state.note_save.lock().await; + let NoteContent { + meta, + content: body, + .. + } = operations::read_vault_note(&vault_path, ¬e_path)?; let mut lines: Vec = body.lines().map(|l| l.to_string()).collect(); let idx = if lines.get(line).map(|l| *l == raw_line).unwrap_or(false) { line @@ -1272,9 +1455,9 @@ pub fn set_task_priority( if body.ends_with('\n') && !new_body.ends_with('\n') { new_body.push('\n'); } - operations::save_note(&vault_path, ¬e_path, &meta, &new_body)?; - - index_note_bg(&state, ¬e_path); + if operations::save_note(&vault_path, ¬e_path, &meta, &new_body)?.is_some() { + index_note_bg(search, ¬e_path); + } Ok(()) } @@ -1289,8 +1472,9 @@ fn set_due_on_line(line: &str, due: Option<&str>) -> String { } #[tauri::command] -pub fn set_task_due( +pub async fn set_task_due( state: State<'_, AppState>, + vault_path: String, note_path: String, line: usize, raw_line: String, @@ -1304,7 +1488,13 @@ pub fn set_task_due( Some(_) => return Err("Invalid due date".to_string()), }; - let (vault_path, meta, body) = read_task_note(&state, ¬e_path)?; + let search = state.vault_snapshot(&vault_path)?; + let _write_guard = state.note_save.lock().await; + let NoteContent { + meta, + content: body, + .. + } = operations::read_vault_note(&vault_path, ¬e_path)?; let mut lines: Vec = body.lines().map(|l| l.to_string()).collect(); let idx = if lines.get(line).map(|l| *l == raw_line).unwrap_or(false) { line @@ -1324,111 +1514,229 @@ pub fn set_task_due( if body.ends_with('\n') && !new_body.ends_with('\n') { new_body.push('\n'); } - operations::save_note(&vault_path, ¬e_path, &meta, &new_body)?; - - index_note_bg(&state, ¬e_path); + if operations::save_note(&vault_path, ¬e_path, &meta, &new_body)?.is_some() { + index_note_bg(search, ¬e_path); + } Ok(()) } // ── Search ── #[tauri::command] -pub fn search_notes( +pub async fn search_notes( state: State<'_, AppState>, query: String, limit: Option, -) -> Result, String> { - let search_guard = state.search_index.lock().map_err(|e| e.to_string())?; - let search = search_guard - .as_ref() + offset: Option, +) -> Result { + let search = state + .search_index + .lock() + .map_err(|error| error.to_string())? + .clone() .ok_or("Search index not initialized")?; - search.search(&query, limit.unwrap_or(20)) + tauri::async_runtime::spawn_blocking(move || { + search.search(&query, limit.unwrap_or(20), offset.unwrap_or(0)) + }) + .await + .map_err(|error| error.to_string())? } #[tauri::command] -pub fn reindex(state: State<'_, AppState>) -> Result<(), String> { - let config = state.config.lock().map_err(|e| e.to_string())?; - let vault_path = config.active_vault.as_ref().ok_or("No active vault")?; - let search_guard = state.search_index.lock().map_err(|e| e.to_string())?; - let search = search_guard - .as_ref() - .ok_or("Search index not initialized")?; - search.rebuild(vault_path) +pub async fn reindex(state: State<'_, AppState>) -> Result<(), String> { + let (search, vault_path) = state.active_search()?; + tauri::async_runtime::spawn_blocking(move || search.rebuild(&vault_path)) + .await + .map_err(|error| error.to_string())? } // ── Trash ── #[tauri::command] -pub fn get_trash(state: State<'_, AppState>) -> Result { - let config = state.config.lock().map_err(|e| e.to_string())?; - let vault_path = config.active_vault.as_ref().ok_or("No active vault")?; - operations::get_trash_contents(vault_path) +pub async fn get_trash(state: State<'_, AppState>) -> Result { + let vault_path = state + .config + .lock() + .map_err(|error| error.to_string())? + .active_vault + .clone() + .ok_or("No active vault")?; + tauri::async_runtime::spawn_blocking(move || operations::get_trash_contents(&vault_path)) + .await + .map_err(|error| error.to_string())? } #[tauri::command] -pub fn restore_note( +pub async fn restore_note( state: State<'_, AppState>, + vault_path: String, trash_path: String, dest_notebook: Option, ) -> Result { - let vault_path = { - let config = state.config.lock().map_err(|e| e.to_string())?; - config - .active_vault - .as_ref() - .ok_or("No active vault")? - .clone() - }; - operations::restore_note(&vault_path, &trash_path, dest_notebook.as_deref()) + let search = state.vault_snapshot(&vault_path)?; + let _write_guard = state.note_save.lock().await; + let restored = operations::restore_note(&vault_path, &trash_path, dest_notebook.as_deref())?; + index_note_bg(search, &restored); + Ok(restored) } #[tauri::command] -pub fn restore_notebook(state: State<'_, AppState>, trash_path: String) -> Result { - let vault_path = { - let config = state.config.lock().map_err(|e| e.to_string())?; - config - .active_vault - .as_ref() - .ok_or("No active vault")? - .clone() - }; - operations::restore_notebook(&vault_path, &trash_path) +pub async fn restore_notebook( + state: State<'_, AppState>, + vault_path: String, + trash_path: String, +) -> Result { + let search = state.vault_snapshot(&vault_path)?; + let _write_guard = state.note_save.lock().await; + let restored = operations::restore_notebook(&vault_path, &trash_path)?; + if let Some(search) = search { + if let Err(error) = search.queue_vault_refresh(&vault_path) { + log::error!("Failed to queue restored notebook search refresh: {error}"); + } + } + Ok(restored) } #[tauri::command] -pub fn permanent_delete(state: State<'_, AppState>, path: String) -> Result<(), String> { - let vault_path = { - let config = state.config.lock().map_err(|e| e.to_string())?; - config - .active_vault - .as_ref() - .ok_or("No active vault")? - .clone() - }; +pub async fn permanent_delete( + state: State<'_, AppState>, + vault_path: String, + path: String, +) -> Result<(), String> { + state.validate_vault(&vault_path)?; + let _write_guard = state.note_save.lock().await; operations::permanent_delete(&vault_path, &path) } #[tauri::command] -pub fn empty_trash(state: State<'_, AppState>) -> Result<(), String> { - let config = state.config.lock().map_err(|e| e.to_string())?; - let vault_path = config.active_vault.as_ref().ok_or("No active vault")?; - operations::empty_trash(vault_path) +pub async fn empty_trash(state: State<'_, AppState>, vault_path: String) -> Result<(), String> { + state.validate_vault(&vault_path)?; + let _write_guard = state.note_save.lock().await; + operations::empty_trash(&vault_path) } // ── Vault State ── #[tauri::command] -pub fn load_vault_state(state: State<'_, AppState>) -> Result { - let config = state.config.lock().map_err(|e| e.to_string())?; - let vault_path = config.active_vault.as_ref().ok_or("No active vault")?; - operations::load_vault_state(vault_path) +pub async fn load_vault_state( + state: State<'_, AppState>, + vault_path: String, +) -> Result { + if state + .config + .lock() + .map_err(|error| error.to_string())? + .active_vault + .as_deref() + != Some(vault_path.as_str()) + { + return Err("Vault changed before loading its state".to_string()); + } + let read_guard = state.note_save.clone().lock_owned().await; + tauri::async_runtime::spawn_blocking(move || { + let _read_guard = read_guard; + operations::load_vault_state(&vault_path) + }) + .await + .map_err(|error| error.to_string())? } #[tauri::command] -pub fn save_vault_state(state: State<'_, AppState>, vault_state: VaultState) -> Result<(), String> { - let config = state.config.lock().map_err(|e| e.to_string())?; - let vault_path = config.active_vault.as_ref().ok_or("No active vault")?; - operations::save_vault_state(vault_path, &vault_state) +pub async fn save_vault_state( + state: State<'_, AppState>, + vault_path: String, + vault_state: VaultState, +) -> Result<(), String> { + if state + .config + .lock() + .map_err(|error| error.to_string())? + .active_vault + .as_deref() + != Some(vault_path.as_str()) + { + return Err("Vault changed before saving its state".to_string()); + } + // Serialize state writes with the existing vault write queue so concurrent + // navigation updates cannot finish out of order or race a state read. + let save_guard = state.note_save.clone().lock_owned().await; + tauri::async_runtime::spawn_blocking(move || { + let _save_guard = save_guard; + operations::save_vault_state(&vault_path, &vault_state) + }) + .await + .map_err(|error| error.to_string())? +} + +#[cfg(test)] +mod vault_state_scope_tests { + use super::*; + use futures::FutureExt; + + #[tokio::test] + async fn queued_state_write_keeps_its_originating_vault() -> Result<(), String> { + let first = tempfile::tempdir().unwrap(); + let second = tempfile::tempdir().unwrap(); + std::fs::create_dir(first.path().join(".helixnotes")).unwrap(); + std::fs::create_dir(second.path().join(".helixnotes")).unwrap(); + let first_path = first.path().to_string_lossy().into_owned(); + let second_path = second.path().to_string_lossy().into_owned(); + let first_state = VaultState { + sidebar_width: 240.0, + last_open_note: Some(first.path().join("A.md").to_string_lossy().into_owned()), + ..VaultState::default() + }; + let second_state = VaultState { + sidebar_width: 360.0, + ..VaultState::default() + }; + operations::save_vault_state(&second_path, &second_state).unwrap(); + let app = tauri::test::mock_builder() + .manage(AppState::new(AppConfig { + active_vault: Some(first_path.clone()), + ..AppConfig::default() + })) + .build(tauri::test::mock_context(tauri::test::noop_assets())) + .unwrap(); + let state = app.state::(); + let saving_note = state.note_save.lock().await; + let mut pending = Box::pin(save_vault_state( + app.state(), + first_path.clone(), + first_state.clone(), + )); + assert!(pending.as_mut().now_or_never().is_none()); + state + .config + .lock() + .map_err(|error| error.to_string())? + .active_vault = Some(second_path.clone()); + drop(saving_note); + pending.await?; + + let saved_first = operations::load_vault_state(&first_path).unwrap(); + assert_eq!(saved_first.sidebar_width, 240.0); + assert_eq!(saved_first.last_open_note, first_state.last_open_note); + assert_eq!( + operations::load_vault_state(&second_path) + .unwrap() + .sidebar_width, + 360.0 + ); + assert!( + save_vault_state(app.state(), first_path.clone(), first_state) + .await + .is_err() + ); + assert!(load_vault_state(app.state(), first_path).await.is_err()); + assert_eq!( + load_vault_state(app.state(), second_path) + .await? + .sidebar_width, + 360.0 + ); + Ok(()) + } } // ── Clipboard ── @@ -1454,160 +1762,236 @@ pub fn copy_text_to_clipboard(_text: String) -> Result<(), String> { /// Returns PNG bytes as Vec, or error if no image on clipboard. #[cfg(desktop)] #[tauri::command] -pub fn read_clipboard_image() -> Result, String> { - let mut clipboard = - arboard::Clipboard::new().map_err(|e| format!("Clipboard init failed: {}", e))?; - let img = clipboard - .get_image() - .map_err(|_| "No image on clipboard".to_string())?; - // Encode RGBA data to PNG - let mut buf: Vec = Vec::new(); - { - let mut encoder = png::Encoder::new( - std::io::Cursor::new(&mut buf), - img.width as u32, - img.height as u32, - ); - encoder.set_color(png::ColorType::Rgba); - encoder.set_depth(png::BitDepth::Eight); - let mut writer = encoder - .write_header() - .map_err(|e| format!("PNG header failed: {}", e))?; - writer - .write_image_data(&img.bytes) - .map_err(|e| format!("PNG encode failed: {}", e))?; - } - Ok(buf) +pub async fn read_clipboard_image() -> Result, String> { + tauri::async_runtime::spawn_blocking(move || { + let mut clipboard = + arboard::Clipboard::new().map_err(|e| format!("Clipboard init failed: {}", e))?; + let img = clipboard + .get_image() + .map_err(|_| "No image on clipboard".to_string())?; + // Encode RGBA data to PNG + let mut buf: Vec = Vec::new(); + { + let mut encoder = png::Encoder::new( + std::io::Cursor::new(&mut buf), + img.width as u32, + img.height as u32, + ); + encoder.set_color(png::ColorType::Rgba); + encoder.set_depth(png::BitDepth::Eight); + let mut writer = encoder + .write_header() + .map_err(|e| format!("PNG header failed: {}", e))?; + writer + .write_image_data(&img.bytes) + .map_err(|e| format!("PNG encode failed: {}", e))?; + } + Ok(buf) + }) + .await + .map_err(|error| error.to_string())? } #[cfg(mobile)] #[tauri::command] -pub fn read_clipboard_image() -> Result, String> { +pub async fn read_clipboard_image() -> Result, String> { Err("Clipboard image reading not supported on Android".to_string()) } /// Copy an image file to the system clipboard. #[cfg(desktop)] #[tauri::command] -pub fn copy_image_to_clipboard(app: AppHandle, path: String) -> Result<(), String> { +pub async fn copy_image_to_clipboard(app: AppHandle, path: String) -> Result<(), String> { ensure_readable_path(&app, Path::new(&path))?; - let data = std::fs::read(&path).map_err(|e| format!("Failed to read image: {}", e))?; - let img = - image::load_from_memory(&data).map_err(|e| format!("Failed to decode image: {}", e))?; - let rgba = img.to_rgba8(); - let (w, h) = rgba.dimensions(); - let img_data = arboard::ImageData { - width: w as usize, - height: h as usize, - bytes: std::borrow::Cow::Owned(rgba.into_raw()), - }; - let mut clipboard = - arboard::Clipboard::new().map_err(|e| format!("Clipboard init failed: {}", e))?; - clipboard - .set_image(img_data) - .map_err(|e| format!("Failed to set clipboard image: {}", e))?; - Ok(()) + tauri::async_runtime::spawn_blocking(move || { + let data = std::fs::read(&path).map_err(|e| format!("Failed to read image: {}", e))?; + let img = + image::load_from_memory(&data).map_err(|e| format!("Failed to decode image: {}", e))?; + let rgba = img.to_rgba8(); + let (w, h) = rgba.dimensions(); + let img_data = arboard::ImageData { + width: w as usize, + height: h as usize, + bytes: std::borrow::Cow::Owned(rgba.into_raw()), + }; + let mut clipboard = + arboard::Clipboard::new().map_err(|e| format!("Clipboard init failed: {}", e))?; + clipboard + .set_image(img_data) + .map_err(|e| format!("Failed to set clipboard image: {}", e))?; + Ok(()) + }) + .await + .map_err(|error| error.to_string())? } #[cfg(mobile)] #[tauri::command] -pub fn copy_image_to_clipboard(_app: AppHandle, _path: String) -> Result<(), String> { +pub async fn copy_image_to_clipboard(_app: AppHandle, _path: String) -> Result<(), String> { Err("Clipboard image copy not supported on Android".to_string()) } /// Copy PNG bytes directly to the system clipboard. #[cfg(desktop)] #[tauri::command] -pub fn copy_png_to_clipboard(data: Vec) -> Result<(), String> { - let img = - image::load_from_memory(&data).map_err(|e| format!("Failed to decode image: {}", e))?; - let rgba = img.to_rgba8(); - let (w, h) = rgba.dimensions(); - let img_data = arboard::ImageData { - width: w as usize, - height: h as usize, - bytes: std::borrow::Cow::Owned(rgba.into_raw()), - }; - let mut clipboard = - arboard::Clipboard::new().map_err(|e| format!("Clipboard init failed: {}", e))?; - clipboard - .set_image(img_data) - .map_err(|e| format!("Failed to set clipboard image: {}", e))?; - Ok(()) +pub async fn copy_png_to_clipboard(data: Vec) -> Result<(), String> { + tauri::async_runtime::spawn_blocking(move || { + let img = + image::load_from_memory(&data).map_err(|e| format!("Failed to decode image: {}", e))?; + let rgba = img.to_rgba8(); + let (w, h) = rgba.dimensions(); + let img_data = arboard::ImageData { + width: w as usize, + height: h as usize, + bytes: std::borrow::Cow::Owned(rgba.into_raw()), + }; + let mut clipboard = + arboard::Clipboard::new().map_err(|e| format!("Clipboard init failed: {}", e))?; + clipboard + .set_image(img_data) + .map_err(|e| format!("Failed to set clipboard image: {}", e))?; + Ok(()) + }) + .await + .map_err(|error| error.to_string())? } #[cfg(mobile)] #[tauri::command] -pub fn copy_png_to_clipboard(_data: Vec) -> Result<(), String> { +pub async fn copy_png_to_clipboard(_data: Vec) -> Result<(), String> { Err("Clipboard image copy not supported on Android".to_string()) } // ── Attachments ── #[tauri::command] -pub fn save_image( +pub async fn save_image( state: State<'_, AppState>, + vault_path: String, name: String, data: Vec, ) -> Result { - let config = state.config.lock().map_err(|e| e.to_string())?; - let vault_path = config.active_vault.as_ref().ok_or("No active vault")?; - operations::save_image(vault_path, &name, &data) + state.validate_vault(&vault_path)?; + let write_guard = state.note_save.clone().lock_owned().await; + tauri::async_runtime::spawn_blocking(move || { + let _write_guard = write_guard; + operations::save_image(&vault_path, &name, &data) + }) + .await + .map_err(|error| error.to_string())? } #[tauri::command] -pub fn save_attachment( +pub async fn save_attachment( state: State<'_, AppState>, + vault_path: String, name: String, data: Vec, ) -> Result { - let config = state.config.lock().map_err(|e| e.to_string())?; - let vault_path = config.active_vault.as_ref().ok_or("No active vault")?; - operations::save_attachment(vault_path, &name, &data) + state.validate_vault(&vault_path)?; + let write_guard = state.note_save.clone().lock_owned().await; + tauri::async_runtime::spawn_blocking(move || { + let _write_guard = write_guard; + operations::save_attachment(&vault_path, &name, &data) + }) + .await + .map_err(|error| error.to_string())? } // ── Notebook Icons ── #[tauri::command] -pub fn get_notebook_icons( +pub async fn get_notebook_icons( state: State<'_, AppState>, + vault_path: String, ) -> Result, String> { - let config = state.config.lock().map_err(|e| e.to_string())?; - let vault_path = config.active_vault.as_ref().ok_or("No active vault")?; - operations::load_notebook_icons(vault_path) + state.validate_vault(&vault_path)?; + tauri::async_runtime::spawn_blocking(move || operations::load_notebook_icons(&vault_path)) + .await + .map_err(|error| error.to_string())? } #[tauri::command] -pub fn set_notebook_icon( +pub async fn set_notebook_icon( state: State<'_, AppState>, + vault_path: String, notebook_relative: String, icon_relative: Option, ) -> Result<(), String> { - let config = state.config.lock().map_err(|e| e.to_string())?; - let vault_path = config.active_vault.as_ref().ok_or("No active vault")?; - operations::set_notebook_icon(vault_path, ¬ebook_relative, icon_relative.as_deref()) + state.validate_vault(&vault_path)?; + let write_guard = state.note_save.clone().lock_owned().await; + tauri::async_runtime::spawn_blocking(move || { + let _write_guard = write_guard; + operations::set_notebook_icon(&vault_path, ¬ebook_relative, icon_relative.as_deref()) + }) + .await + .map_err(|error| error.to_string())? +} + +#[tauri::command] +pub async fn import_notebook_icon( + state: State<'_, AppState>, + vault_path: String, + notebook_relative: String, + name: String, + data: Vec, +) -> Result { + state.validate_vault(&vault_path)?; + let write_guard = state.note_save.clone().lock_owned().await; + tauri::async_runtime::spawn_blocking(move || { + let _write_guard = write_guard; + operations::import_notebook_icon(&vault_path, ¬ebook_relative, &name, &data) + }) + .await + .map_err(|error| error.to_string())? } // ── Tag Styles ── #[tauri::command] -pub fn get_tag_styles( +pub async fn get_tag_styles( state: State<'_, AppState>, + vault_path: String, ) -> Result, String> { - let config = state.config.lock().map_err(|e| e.to_string())?; - let vault_path = config.active_vault.as_ref().ok_or("No active vault")?; - operations::load_tag_styles(vault_path) + state.validate_vault(&vault_path)?; + tauri::async_runtime::spawn_blocking(move || operations::load_tag_styles(&vault_path)) + .await + .map_err(|error| error.to_string())? } #[tauri::command] -pub fn set_tag_style( +pub async fn set_tag_style( state: State<'_, AppState>, + vault_path: String, tag: String, - style: Option, -) -> Result<(), String> { - let config = state.config.lock().map_err(|e| e.to_string())?; - let vault_path = config.active_vault.as_ref().ok_or("No active vault")?; - operations::set_tag_style(vault_path, &tag, style) + change: TagStyleChange, +) -> Result, String> { + state.validate_vault(&vault_path)?; + let write_guard = state.note_save.clone().lock_owned().await; + tauri::async_runtime::spawn_blocking(move || { + let _write_guard = write_guard; + operations::patch_tag_style(&vault_path, &tag, change) + }) + .await + .map_err(|error| error.to_string())? +} + +#[tauri::command] +pub async fn import_tag_icon( + state: State<'_, AppState>, + vault_path: String, + tag: String, + name: String, + data: Vec, +) -> Result { + state.validate_vault(&vault_path)?; + let write_guard = state.note_save.clone().lock_owned().await; + tauri::async_runtime::spawn_blocking(move || { + let _write_guard = write_guard; + operations::import_tag_icon(&vault_path, &tag, &name, &data) + }) + .await + .map_err(|error| error.to_string())? } // ── General Settings ── @@ -1677,42 +2061,56 @@ pub fn set_general_settings( // ── Quick Access ── #[tauri::command] -pub async fn get_quick_access(state: State<'_, AppState>) -> Result, String> { +pub async fn get_quick_access( + state: State<'_, AppState>, + vault_path: String, +) -> Result, String> { + state.validate_vault(&vault_path)?; + let read_guard = state.note_save.clone().lock_owned().await; + tauri::async_runtime::spawn_blocking(move || { + let _read_guard = read_guard; + operations::get_quick_access_notes(&vault_path) + }) + .await + .map_err(|error| error.to_string())? +} + +#[tauri::command] +pub async fn add_quick_access( + state: State<'_, AppState>, + vault_path: String, + note_relative: String, +) -> Result<(), String> { + state.validate_vault(&vault_path)?; + let _write_guard = state.note_save.lock().await; + operations::add_quick_access(&vault_path, ¬e_relative) +} + +#[tauri::command] +pub async fn remove_quick_access( + state: State<'_, AppState>, + vault_path: String, + note_relative: String, +) -> Result<(), String> { + state.validate_vault(&vault_path)?; + let _write_guard = state.note_save.lock().await; + operations::remove_quick_access(&vault_path, ¬e_relative) +} + +#[tauri::command] +pub async fn reorder_quick_access( + state: State<'_, AppState>, + paths: Vec, +) -> Result<(), String> { let vault_path = state .config .lock() .map_err(|error| error.to_string())? .active_vault .clone() - .ok_or_else(|| "No active vault".to_string())?; - - tauri::async_runtime::spawn_blocking(move || operations::get_quick_access_notes(&vault_path)) - .await - .map_err(|error| error.to_string())? -} - -#[tauri::command] -pub fn add_quick_access(state: State<'_, AppState>, note_relative: String) -> Result<(), String> { - let config = state.config.lock().map_err(|e| e.to_string())?; - let vault_path = config.active_vault.as_ref().ok_or("No active vault")?; - operations::add_quick_access(vault_path, ¬e_relative) -} - -#[tauri::command] -pub fn remove_quick_access( - state: State<'_, AppState>, - note_relative: String, -) -> Result<(), String> { - let config = state.config.lock().map_err(|e| e.to_string())?; - let vault_path = config.active_vault.as_ref().ok_or("No active vault")?; - operations::remove_quick_access(vault_path, ¬e_relative) -} - -#[tauri::command] -pub fn reorder_quick_access(state: State<'_, AppState>, paths: Vec) -> Result<(), String> { - let config = state.config.lock().map_err(|e| e.to_string())?; - let vault_path = config.active_vault.as_ref().ok_or("No active vault")?; - operations::save_quick_access(vault_path, &paths) + .ok_or("No active vault")?; + let _write_guard = state.note_save.lock().await; + operations::save_quick_access(&vault_path, &paths) } #[tauri::command] @@ -1802,6 +2200,8 @@ pub fn import_obsidian(app: AppHandle) -> Result<(), String> { fn do_import_obsidian(app: AppHandle, vault_path: &str) -> Result { let state = app.state::(); + let _write_guard = state.note_save.blocking_lock(); + ensure_cleanup_idle(&state)?; state .importing .store(true, std::sync::atomic::Ordering::Relaxed); @@ -1825,92 +2225,141 @@ pub struct OrphanAttachment { // Conservatively find files in .helixnotes/attachments not referenced by ANY note. Scans every // .md in the vault (including .helixnotes/trash, so a restorable trashed note keeps its files) -// plus notebook_icons.json and tag_styles.json, and matches each filename against both the raw text and a -// percent-decoded copy (so a URL-encoded path like `my%20file.png` still counts as a reference). +// plus notebook_icons.json and tag_styles.json. A single matcher finds all basename substrings +// in each source, first raw and then decoded in place (including URL-encoded paths). // When in doubt a file is KEPT: a leftover orphan is harmless, a wrong deletion is not. fn scan_orphaned_attachments(vault: &str) -> Result, String> { let attachments_dir = operations::helixnotes_dir(vault).join("attachments"); - if !attachments_dir.is_dir() { - return Ok(Vec::new()); - } + let entries = match std::fs::read_dir(&attachments_dir) { + Ok(entries) => entries, + Err(error) if error.kind() == std::io::ErrorKind::NotFound => return Ok(Vec::new()), + Err(error) => return Err(error.to_string()), + }; let mut files: Vec<(String, u64)> = Vec::new(); - for entry in std::fs::read_dir(&attachments_dir).map_err(|e| e.to_string())? { + for entry in entries { let entry = entry.map_err(|e| e.to_string())?; let p = entry.path(); - if p.is_file() { + let metadata = p.metadata().map_err(|error| error.to_string())?; + if metadata.is_file() { let name = p .file_name() .unwrap_or_default() .to_string_lossy() .to_string(); - let size = entry.metadata().map(|m| m.len()).unwrap_or(0); + let size = metadata.len(); files.push((name, size)); } } if files.is_empty() { return Ok(Vec::new()); } - let mut haystack = String::new(); - for entry in walkdir::WalkDir::new(vault) - .into_iter() - .filter_map(|e| e.ok()) - { + let matcher = aho_corasick::AhoCorasick::new(files.iter().map(|(name, _)| name.as_str())) + .map_err(|error| error.to_string())?; + let mut referenced = vec![false; files.len()]; + let mut scan_source = |content: String| { + // Overlapping matches retain shorter names contained in another attachment's name. + for found in matcher.find_overlapping_iter(content.as_bytes()) { + referenced[found.pattern().as_usize()] = true; + } + if !content.contains('%') { + return; + } + let decoded = percent_decode(content); + for found in matcher.find_overlapping_iter(&decoded) { + referenced[found.pattern().as_usize()] = true; + } + }; + for entry in walkdir::WalkDir::new(vault) { + let entry = entry.map_err(|error| error.to_string())?; let p = entry.path(); - if p.is_file() && p.extension().and_then(|x| x.to_str()) == Some("md") { - if let Ok(content) = std::fs::read_to_string(p) { - haystack.push_str(&content); - haystack.push('\n'); - } + if p.extension().and_then(|x| x.to_str()) == Some("md") + && p.metadata().map_err(|error| error.to_string())?.is_file() + { + let content = std::fs::read_to_string(p).map_err(|error| error.to_string())?; + scan_source(content); } } // Folder and tag icons live in attachments but are referenced here, not in notes. (#157) for mapping in ["notebook_icons.json", "tag_styles.json"] { let mapping_path = operations::helixnotes_dir(vault).join(mapping); - if let Ok(content) = std::fs::read_to_string(&mapping_path) { - haystack.push_str(&content); - haystack.push('\n'); + match std::fs::read_to_string(&mapping_path) { + Ok(content) => { + let references = operations::attachment_metadata_references(mapping, &content) + .map_err(|error| format!("Invalid {mapping}: {error}"))?; + scan_source(content); + // JSON escapes (including Unicode) are references too. Retain + // both the raw representation and decoded string values. + for reference in references { + scan_source(reference); + } + } + Err(error) if error.kind() == std::io::ErrorKind::NotFound => {} + Err(error) => return Err(error.to_string()), } } - let decoded = percent_decode(&haystack); let orphans = files .into_iter() - .filter(|(name, _)| !haystack.contains(name.as_str()) && !decoded.contains(name.as_str())) + .enumerate() + .filter_map(|(id, file)| (!referenced[id]).then_some(file)) .collect(); Ok(orphans) } #[tauri::command] -pub fn find_orphaned_attachments( +pub async fn find_orphaned_attachments( state: State<'_, AppState>, + vault_path: String, ) -> Result, String> { - let vault = { - let config = state.config.lock().map_err(|e| e.to_string())?; - config.active_vault.clone().ok_or("No active vault")? - }; - let orphans = scan_orphaned_attachments(&vault)?; - Ok(orphans - .into_iter() - .map(|(name, size)| OrphanAttachment { name, size }) - .collect()) + state.validate_vault(&vault_path)?; + let save_guard = state.note_save.clone().lock_owned().await; + ensure_cleanup_idle(&state)?; + tauri::async_runtime::spawn_blocking(move || { + let _save_guard = save_guard; + let orphans = scan_orphaned_attachments(&vault_path)?; + Ok(orphans + .into_iter() + .map(|(name, size)| OrphanAttachment { name, size }) + .collect()) + }) + .await + .map_err(|error| error.to_string())? } #[tauri::command] -pub fn trash_orphaned_attachments( +pub async fn trash_orphaned_attachments( state: State<'_, AppState>, + vault_path: String, names: Vec, ) -> Result { - let vault = { - let config = state.config.lock().map_err(|e| e.to_string())?; - config.active_vault.clone().ok_or("No active vault")? - }; + state.validate_vault(&vault_path)?; + let save_guard = state.note_save.clone().lock_owned().await; + ensure_cleanup_idle(&state)?; + tauri::async_runtime::spawn_blocking(move || { + let _save_guard = save_guard; + trash_orphaned_files(&vault_path, &names) + }) + .await + .map_err(|error| error.to_string())? +} + +fn ensure_cleanup_idle(state: &AppState) -> Result<(), String> { + use std::sync::atomic::Ordering; + if state.importing.load(Ordering::Acquire) || state.syncing.load(Ordering::Acquire) { + Err("Attachment cleanup is unavailable while import or sync is running".to_string()) + } else { + Ok(()) + } +} + +fn trash_orphaned_files(vault: &str, names: &[String]) -> Result { // Re-scan now and only move files that are STILL orphaned (guards against a reference added // between scan and confirm), intersected with the caller's selection. - let current: std::collections::HashSet = scan_orphaned_attachments(&vault)? + let current: std::collections::HashSet = scan_orphaned_attachments(vault)? .into_iter() .map(|(n, _)| n) .collect(); - let attachments_dir = operations::helixnotes_dir(&vault).join("attachments"); - let trash_dir = operations::helixnotes_dir(&vault).join("trash"); + let attachments_dir = operations::helixnotes_dir(vault).join("attachments"); + let trash_dir = operations::helixnotes_dir(vault).join("trash"); std::fs::create_dir_all(&trash_dir).map_err(|e| e.to_string())?; let stamp = chrono::Utc::now().format("%Y%m%d%H%M%S%3f").to_string(); let mut moved = 0u32; @@ -1928,36 +2377,287 @@ pub fn trash_orphaned_attachments( continue; } let src = attachments_dir.join(name); - if !src.is_file() { - continue; + match src.metadata() { + Ok(metadata) if metadata.is_file() => {} + Ok(_) => continue, + Err(error) if error.kind() == std::io::ErrorKind::NotFound => continue, + Err(error) => return Err(error.to_string()), } let dest = trash_dir.join(format!("{}_{}_attachment_{}", stamp, i, name)); - if std::fs::rename(&src, &dest).is_ok() { - moved += 1; - } + std::fs::rename(&src, &dest).map_err(|error| error.to_string())?; + moved += 1; } Ok(moved) } -fn percent_decode(s: &str) -> String { - let mut result = String::with_capacity(s.len()); - let mut chars = s.bytes(); - while let Some(b) = chars.next() { - if b == b'%' { - let hi = chars.next(); - let lo = chars.next(); - if let (Some(h), Some(l)) = (hi, lo) { - if let Ok(byte) = u8::from_str_radix(&format!("{}{}", h as char, l as char), 16) { - result.push(byte as char); - continue; - } - } - result.push('%'); - } else { - result.push(b as char); +#[cfg(test)] +mod orphan_attachment_tests { + use super::{scan_orphaned_attachments, trash_orphaned_files}; + use crate::vault::operations; + + #[test] + fn invalid_icon_metadata_aborts_scan_and_removal_without_losing_attachments() { + for (mapping, content) in [ + ("notebook_icons.json", "{"), + ("notebook_icons.json", r#"{"Notebook":42}"#), + ("tag_styles.json", r#"{"Work":"not a style"}"#), + ("tag_styles.json", r#"{"Work":{"icon":false}}"#), + ("tag_styles.json", r#"{"Work":{"color":"red"}}"#), + ("tag_styles.json", r#"{"Work":{"unexpected":"keep.png"}}"#), + ] { + let root = tempfile::tempdir().unwrap(); + let vault = root.path().to_str().unwrap(); + operations::ensure_vault_structure(vault).unwrap(); + let storage = operations::helixnotes_dir(vault); + let attachment = storage.join("attachments/keep.png"); + std::fs::write(&attachment, b"preserved").unwrap(); + std::fs::write(storage.join(mapping), content).unwrap(); + assert!( + scan_orphaned_attachments(vault).is_err(), + "{mapping}: {content}" + ); + assert!(trash_orphaned_files(vault, &["keep.png".into()]).is_err()); + assert_eq!(std::fs::read(&attachment).unwrap(), b"preserved"); } } - result + + #[test] + fn json_escaped_unicode_icon_references_survive_cleanup() { + let root = tempfile::tempdir().unwrap(); + let vault = root.path().to_str().unwrap(); + operations::ensure_vault_structure(vault).unwrap(); + let storage = operations::helixnotes_dir(vault); + std::fs::write(storage.join("attachments/猫.png"), b"notebook").unwrap(); + std::fs::write(storage.join("attachments/é.png"), b"tag").unwrap(); + std::fs::write( + storage.join("notebook_icons.json"), + r#"{"Book":"attachments/\u732b.png"}"#, + ) + .unwrap(); + std::fs::write( + storage.join("tag_styles.json"), + r#"{"Work":{"icon":"attachments/\u00e9.png"}}"#, + ) + .unwrap(); + assert_eq!( + trash_orphaned_files(vault, &["猫.png".into(), "é.png".into()]).unwrap(), + 0 + ); + assert_eq!( + std::fs::read(storage.join("attachments/猫.png")).unwrap(), + b"notebook" + ); + assert_eq!( + std::fs::read(storage.join("attachments/é.png")).unwrap(), + b"tag" + ); + } + + #[test] + fn cleanup_keeps_plain_overlapping_and_literal_percent_basenames() { + let root = tempfile::tempdir().unwrap(); + let vault = root.path().to_str().unwrap(); + operations::ensure_vault_structure(vault).unwrap(); + let attachments = operations::helixnotes_dir(vault).join("attachments"); + for name in [ + "plain.txt", + "icon.png", + "folder-icon.png", + "folder-icon.png.backup", + "literal%20space.png", + "literal space.png", + "orphan.png", + ] { + std::fs::write(attachments.join(name), b"image").unwrap(); + } + std::fs::write( + root.path().join("Mentions.md"), + "Not links: prefixplain.txtsuffix folder-icon.png.backup literal%20space.png", + ) + .unwrap(); + + assert_eq!( + scan_orphaned_attachments(vault).unwrap(), + vec![("orphan.png".into(), 5)] + ); + } + + #[test] + fn cleanup_keeps_unicode_and_encoded_references_from_every_source() { + let root = tempfile::tempdir().unwrap(); + let vault = root.path().to_str().unwrap(); + operations::ensure_vault_structure(vault).unwrap(); + let storage = operations::helixnotes_dir(vault); + let attachments = storage.join("attachments"); + for name in [ + "raw 猫.png", + "猫 spaced.png", + "café.png", + "%Q猫 image.png", + "%G1broken image.png", + "after-bad image.png", + "trailing%", + "trash ü.png", + "history ね.png", + "📁 icon.png", + "é tag.png", + "映像.png", + "orphan.png", + ] { + std::fs::write(attachments.join(name), b"image").unwrap(); + } + std::fs::write( + root.path().join("Unicode.md"), + "raw 猫.png 猫%20spaced.png %63%61%66%c3%a9.png\n\ + %Q猫%20image.png %G1broken%20image.png %FFafter-bad%20image.png trailing%", + ) + .unwrap(); + std::fs::write(storage.join("trash/old.md"), "![trash](trash%20%C3%BC.png)").unwrap(); + std::fs::create_dir_all(storage.join("history/Old")).unwrap(); + std::fs::write( + storage.join("history/Old/1.md"), + "![history](history%20%E3%81%AD.png)", + ) + .unwrap(); + std::fs::write( + storage.join("notebook_icons.json"), + r#"{"Notebook":"attachments/%F0%9F%93%81%20icon.png"}"#, + ) + .unwrap(); + std::fs::write( + storage.join("tag_styles.json"), + r#"{"encoded":{"icon":"attachments/%C3%A9%20tag.png"},"raw":{"icon":"attachments/映像.png"}}"#, + ) + .unwrap(); + + assert_eq!( + scan_orphaned_attachments(vault).unwrap(), + vec![("orphan.png".into(), 5)] + ); + } + + #[test] + fn unreadable_mapping_aborts_even_after_all_attachments_are_referenced() { + let root = tempfile::tempdir().unwrap(); + let vault = root.path().to_str().unwrap(); + operations::ensure_vault_structure(vault).unwrap(); + let storage = operations::helixnotes_dir(vault); + let attachment = storage.join("attachments/keep.png"); + std::fs::write(&attachment, b"image").unwrap(); + std::fs::write(root.path().join("Note.md"), "keep.png").unwrap(); + std::fs::write(storage.join("notebook_icons.json"), [0xff]).unwrap(); + + assert!(trash_orphaned_files(vault, &["keep.png".into()]).is_err()); + assert_eq!(std::fs::read(attachment).unwrap(), b"image"); + } + + #[test] + fn cleanup_revalidates_references_and_only_trashes_selected_orphans() { + let root = tempfile::tempdir().unwrap(); + let vault = root.path().to_str().unwrap(); + operations::ensure_vault_structure(vault).unwrap(); + let storage = operations::helixnotes_dir(vault); + let attachments = storage.join("attachments"); + for name in [ + "late.png", + "trashed image.png", + "icon.png", + "orphan.png", + "unselected.png", + ] { + std::fs::write(attachments.join(name), b"image").unwrap(); + } + std::fs::write( + storage.join("trash/old.md"), + "![kept](../attachments/trashed%20image.png)", + ) + .unwrap(); + std::fs::write( + storage.join("notebook_icons.json"), + r#"{"Notebook":"attachments/icon.png"}"#, + ) + .unwrap(); + let mut found: Vec<_> = scan_orphaned_attachments(vault) + .unwrap() + .into_iter() + .map(|(name, _)| name) + .collect(); + found.sort(); + assert_eq!(found, ["late.png", "orphan.png", "unselected.png"]); + + std::fs::write(root.path().join("New.md"), "![added](late.png)").unwrap(); + let selected = vec!["late.png".into(), "orphan.png".into(), "../New.md".into()]; + assert_eq!(trash_orphaned_files(vault, &selected).unwrap(), 1); + for name in [ + "late.png", + "trashed image.png", + "icon.png", + "unselected.png", + ] { + assert_eq!(std::fs::read(attachments.join(name)).unwrap(), b"image"); + } + assert!(!attachments.join("orphan.png").exists()); + assert_eq!( + std::fs::read_to_string(root.path().join("New.md")).unwrap(), + "![added](late.png)" + ); + let trashed = std::fs::read_dir(storage.join("trash")) + .unwrap() + .map(|entry| entry.unwrap().path()) + .find(|path| { + path.file_name() + .unwrap() + .to_string_lossy() + .ends_with("_attachment_orphan.png") + }) + .unwrap(); + assert_eq!(std::fs::read(trashed).unwrap(), b"image"); + } + + #[test] + fn unreadable_reference_sources_abort_cleanup_without_moving_files() { + let root = tempfile::tempdir().unwrap(); + let vault = root.path().to_str().unwrap(); + operations::ensure_vault_structure(vault).unwrap(); + let storage = operations::helixnotes_dir(vault); + let attachment = storage.join("attachments/keep.png"); + std::fs::write(&attachment, b"image").unwrap(); + let note = root.path().join("Unreadable.md"); + std::fs::write(¬e, [0xff, 0xfe]).unwrap(); + assert!(trash_orphaned_files(vault, &["keep.png".into()]).is_err()); + assert_eq!(std::fs::read(&attachment).unwrap(), b"image"); + + std::fs::remove_file(note).unwrap(); + std::fs::create_dir(storage.join("tag_styles.json")).unwrap(); + assert!(trash_orphaned_files(vault, &["keep.png".into()]).is_err()); + assert_eq!(std::fs::read(attachment).unwrap(), b"image"); + } +} + +fn percent_decode(s: String) -> Vec { + // Reuse the source buffer and match bytes, so Unicode is neither re-encoded nor made lossy. + // Invalid/truncated escapes remain literal; only a complete %HH consumes three bytes. + let mut bytes = s.into_bytes(); + let mut read = 0; + let mut write = 0; + while read < bytes.len() { + if bytes[read] == b'%' && read + 2 < bytes.len() { + if let (Some(hi), Some(lo)) = ( + (bytes[read + 1] as char).to_digit(16), + (bytes[read + 2] as char).to_digit(16), + ) { + bytes[write] = ((hi << 4) | lo) as u8; + read += 3; + write += 1; + continue; + } + } + bytes[write] = bytes[read]; + read += 1; + write += 1; + } + bytes.truncate(write); + bytes } // ── Open files and URLs with the system default handler ── @@ -2284,37 +2984,59 @@ pub fn set_backup_settings( // ── Version History ── #[tauri::command] -pub fn get_note_versions( +pub async fn get_note_versions( state: State<'_, AppState>, + vault_path: String, note_id: String, ) -> Result, String> { - let config = state.config.lock().map_err(|e| e.to_string())?; - let vault_path = config.active_vault.as_ref().ok_or("No active vault")?; - crate::history::list_versions(vault_path, ¬e_id) + state.validate_vault(&vault_path)?; + let read_guard = state.note_save.clone().lock_owned().await; + tauri::async_runtime::spawn_blocking(move || { + let _read_guard = read_guard; + crate::history::list_versions(&vault_path, ¬e_id) + }) + .await + .map_err(|error| error.to_string())? } #[tauri::command] -pub fn create_version( +pub async fn create_version( state: State<'_, AppState>, + vault_path: String, path: String, note_id: String, ) -> Result<(), String> { - let config = state.config.lock().map_err(|e| e.to_string())?; - let vault_path = config.active_vault.as_ref().ok_or("No active vault")?; - let max_versions = config.max_versions_per_note; - let raw = operations::read_vault_note(vault_path, &path)?.raw; - crate::history::force_snapshot(vault_path, ¬e_id, &raw, max_versions) + state.validate_vault(&vault_path)?; + let max_versions = state + .config + .lock() + .map_err(|error| error.to_string())? + .max_versions_per_note; + let save_guard = state.note_save.clone().lock_owned().await; + tauri::async_runtime::spawn_blocking(move || { + let _save_guard = save_guard; + let raw = operations::read_vault_note(&vault_path, &path)?.raw; + crate::history::force_snapshot(&vault_path, ¬e_id, &raw, max_versions) + }) + .await + .map_err(|error| error.to_string())? } #[tauri::command] -pub fn get_note_version_content( +pub async fn get_note_version_content( state: State<'_, AppState>, + vault_path: String, note_id: String, timestamp: String, ) -> Result { - let config = state.config.lock().map_err(|e| e.to_string())?; - let vault_path = config.active_vault.as_ref().ok_or("No active vault")?; - crate::history::get_version(vault_path, ¬e_id, ×tamp) + state.validate_vault(&vault_path)?; + let read_guard = state.note_save.clone().lock_owned().await; + tauri::async_runtime::spawn_blocking(move || { + let _read_guard = read_guard; + crate::history::get_version(&vault_path, ¬e_id, ×tamp) + }) + .await + .map_err(|error| error.to_string())? } // ── AI ── @@ -2796,8 +3518,12 @@ mod config_permission_tests { fn save_app_config(config: &AppConfig) -> Result<(), String> { let path = app_config_path()?; + save_app_config_to(config, &path) +} + +fn save_app_config_to(config: &AppConfig, path: &Path) -> Result<(), String> { let data = serde_json::to_string_pretty(config).map_err(|e| e.to_string())?; - write_private_file(&path, data.as_bytes())?; + write_private_file(path, data.as_bytes())?; Ok(()) } @@ -2851,3 +3577,594 @@ pub fn get_install_type() -> String { "native".to_string() } } + +#[cfg(test)] +mod vault_runtime_tests { + use super::*; + use std::sync::{Arc, TryLockError}; + use std::time::{Duration, Instant}; + + fn activate(state: &AppState, path: &str, index: Arc) { + let mut search = state.search_index.lock().unwrap(); + let mut config = state.config.lock().unwrap(); + *search = Some(index); + config.active_vault = Some(path.to_string()); + } + + fn wait_for_search_lock(state: &AppState) { + let deadline = Instant::now() + Duration::from_secs(5); + loop { + match state.search_index.try_lock() { + Err(TryLockError::WouldBlock) => return, + Err(TryLockError::Poisoned(error)) => panic!("{error}"), + Ok(_) => {} + } + assert!( + Instant::now() < deadline, + "runtime operation did not acquire search before config" + ); + std::thread::yield_now(); + } + } + + #[test] + fn reopening_a_vault_reuses_the_index_retained_by_background_work() { + let root = tempfile::tempdir().unwrap(); + let a = root.path().join("A").to_string_lossy().into_owned(); + let b = root.path().join("B").to_string_lossy().into_owned(); + operations::ensure_vault_structure(&a).unwrap(); + operations::ensure_vault_structure(&b).unwrap(); + let note = operations::create_note(&a, None, "Alpha").unwrap(); + let state = AppState::new(AppConfig::default()); + let first = state.search_index_for_vault(&a).unwrap(); + first.rebuild(&a).unwrap(); + activate(&state, &a, first.clone()); + + let background_work = first; + let second = state.search_index_for_vault(&b).unwrap(); + activate(&state, &b, second); + let reopened = state.search_index_for_vault(&a).unwrap(); + assert_eq!( + reopened.search("Alpha", 10, 0).unwrap().results[0].path, + note.path + ); + activate(&state, &a, reopened.clone()); + drop(background_work); + state.search_index.lock().unwrap().take(); + drop(reopened); + + // Expired weak entries do not retain a writer or prevent a later open. + let reopened_after_shutdown = state.search_index_for_vault(&a).unwrap(); + assert_eq!( + reopened_after_shutdown + .search("Alpha", 10, 0) + .unwrap() + .results[0] + .path, + note.path + ); + } + + #[test] + fn reindex_snapshot_cannot_mix_vaults_during_a_switch() { + let root = tempfile::tempdir().unwrap(); + let a = root.path().join("A").to_string_lossy().into_owned(); + let b = root.path().join("B").to_string_lossy().into_owned(); + operations::ensure_vault_structure(&a).unwrap(); + operations::ensure_vault_structure(&b).unwrap(); + let alpha = operations::create_note(&a, None, "Alpha").unwrap(); + let beta = operations::create_note(&b, None, "Beta").unwrap(); + let state = Arc::new(AppState::new(AppConfig::default())); + let first = state.search_index_for_vault(&a).unwrap(); + let second = state.search_index_for_vault(&b).unwrap(); + first.rebuild(&a).unwrap(); + second.rebuild(&b).unwrap(); + activate(&state, &a, first.clone()); + + // Stop the snapshot at its config read, then queue a switch behind it. + let config = state.config.lock().unwrap(); + let snapshot_state = state.clone(); + let snapshot = std::thread::spawn(move || snapshot_state.active_search().unwrap()); + wait_for_search_lock(&state); + let switch_state = state.clone(); + let switch_index = second.clone(); + let switch = std::thread::spawn(move || activate(&switch_state, &b, switch_index)); + drop(config); + let (index, vault_path) = snapshot.join().unwrap(); + switch.join().unwrap(); + index.rebuild(&vault_path).unwrap(); + + assert_eq!( + first.search("Alpha", 10, 0).unwrap().results[0].path, + alpha.path + ); + assert!(first.search("Beta", 10, 0).unwrap().results.is_empty()); + assert_eq!( + second.search("Beta", 10, 0).unwrap().results[0].path, + beta.path + ); + } + + #[test] + fn removing_a_vault_preserves_concurrent_settings_and_rolls_back_write_failure() { + let root = tempfile::tempdir().unwrap(); + let vault = root.path().join("vault").to_string_lossy().into_owned(); + operations::ensure_vault_structure(&vault).unwrap(); + let app = tauri::test::mock_builder() + .manage(AppState::new(AppConfig { + active_vault: Some(vault.clone()), + vaults: vec![VaultConfig { + path: vault.clone(), + name: "Vault".into(), + ..VaultConfig::default() + }], + ..AppConfig::default() + })) + .build(tauri::test::mock_context(tauri::test::noop_assets())) + .unwrap(); + let state = app.state::(); + let index = state.search_index_for_vault(&vault).unwrap(); + *state.search_index.lock().unwrap() = Some(index.clone()); + + assert!( + remove_vault_path(app.handle(), &state, vault.clone(), None, root.path(),).is_err() + ); + let (retained_index, retained_path) = state.active_search().unwrap(); + assert!(Arc::ptr_eq(&retained_index, &index)); + assert_eq!(retained_path, vault); + + let config_path = root.path().join("app-config.json"); + let saved_path = config_path.clone(); + let handle = app.handle().clone(); + let mut config = state.config.lock().unwrap(); + let removal = std::thread::spawn(move || { + let state = handle.state::(); + remove_vault_path(&handle, &state, vault, None, &config_path) + }); + wait_for_search_lock(&state); + config.theme = "nord".into(); + drop(config); + removal.join().unwrap().unwrap(); + + assert!(state.search_index.lock().unwrap().is_none()); + let saved: AppConfig = serde_json::from_slice(&std::fs::read(saved_path).unwrap()).unwrap(); + assert_eq!(saved.theme, "nord"); + assert!(saved.active_vault.is_none()); + assert!(saved.vaults.is_empty()); + assert_eq!(state.config.lock().unwrap().theme, "nord"); + } +} + +#[cfg(test)] +mod mutation_order_tests { + use super::*; + use futures::FutureExt; + + #[tokio::test] + async fn detached_history_quick_access_titles_and_graph_keep_their_origin() { + let first = tempfile::tempdir().unwrap(); + let second = tempfile::tempdir().unwrap(); + let a = first + .path() + .join("AlphaVault") + .to_string_lossy() + .into_owned(); + let b = second + .path() + .join("BetaVault") + .to_string_lossy() + .into_owned(); + operations::ensure_vault_structure(&a).unwrap(); + operations::ensure_vault_structure(&b).unwrap(); + let alpha = operations::create_note(&a, None, "Alpha").unwrap(); + let beta = operations::create_note(&b, None, "Beta").unwrap(); + let alpha_raw = operations::read_note(&a, &alpha.path).unwrap().raw; + operations::add_quick_access(&b, "Beta.md").unwrap(); + // The same ID may legitimately exist in a copied vault. Its history + // must still be looked up using the window's explicit vault origin. + crate::history::force_snapshot(&b, &alpha.meta.id, "Beta history", 20).unwrap(); + let app = tauri::test::mock_builder() + .manage(AppState::new(AppConfig { + active_vault: Some(a.clone()), + vaults: vec![ + VaultConfig { + path: a.clone(), + ..VaultConfig::default() + }, + VaultConfig { + path: b.clone(), + ..VaultConfig::default() + }, + ], + ..AppConfig::default() + })) + .build(tauri::test::mock_context(tauri::test::noop_assets())) + .unwrap(); + let state = app.state::(); + let held = state.note_save.lock().await; + let mut snapshot = Box::pin(create_version( + app.state(), + a.clone(), + alpha.path.clone(), + alpha.meta.id.clone(), + )); + let mut adding = Box::pin(add_quick_access(app.state(), a.clone(), "Alpha.md".into())); + assert!(snapshot.as_mut().now_or_never().is_none()); + assert!(adding.as_mut().now_or_never().is_none()); + state.config.lock().unwrap().active_vault = Some(b.clone()); + drop(held); + snapshot.await.unwrap(); + adding.await.unwrap(); + + let versions = get_note_versions(app.state(), a.clone(), alpha.meta.id.clone()) + .await + .unwrap(); + assert_eq!(versions.len(), 1); + let restored = get_note_version_content( + app.state(), + a.clone(), + alpha.meta.id.clone(), + versions[0].timestamp.clone(), + ) + .await + .unwrap(); + assert_eq!(restored, alpha_raw); + let beta_versions = get_note_versions(app.state(), b.clone(), alpha.meta.id.clone()) + .await + .unwrap(); + assert_eq!( + get_note_version_content( + app.state(), + b.clone(), + alpha.meta.id, + beta_versions[0].timestamp.clone(), + ) + .await + .unwrap(), + "Beta history" + ); + + let shortcuts = get_quick_access(app.state(), a.clone()).await.unwrap(); + assert_eq!( + shortcuts + .iter() + .map(|note| note.path.as_str()) + .collect::>(), + [alpha.path.as_str()] + ); + let titles = get_all_note_titles(app.state(), a.clone()).await.unwrap(); + assert_eq!( + titles + .iter() + .map(|note| note.title.as_str()) + .collect::>(), + ["Alpha"] + ); + let graph = get_graph_data(app.state(), a.clone()).unwrap(); + assert_eq!( + graph + .nodes + .iter() + .map(|note| note.path.as_str()) + .collect::>(), + [alpha.path.as_str()] + ); + remove_quick_access(app.state(), a.clone(), "Alpha.md".into()) + .await + .unwrap(); + assert!(get_quick_access(app.state(), a).await.unwrap().is_empty()); + let beta_shortcuts = get_quick_access(app.state(), b).await.unwrap(); + assert_eq!( + beta_shortcuts + .iter() + .map(|note| note.path.as_str()) + .collect::>(), + [beta.path.as_str()] + ); + } + + #[tokio::test] + async fn queued_writes_and_detached_saves_keep_the_authorized_origin() { + let first = tempfile::tempdir().unwrap(); + let second = tempfile::tempdir().unwrap(); + let outside = tempfile::tempdir().unwrap(); + let a = first.path().to_string_lossy().into_owned(); + let b = second.path().to_string_lossy().into_owned(); + operations::ensure_vault_structure(&a).unwrap(); + operations::ensure_vault_structure(&b).unwrap(); + let alpha = operations::create_note(&a, None, "Shared").unwrap(); + let beta = operations::create_note(&b, None, "Shared").unwrap(); + let beta_before = std::fs::read(&beta.path).unwrap(); + let app = tauri::test::mock_builder() + .manage(AppState::new(AppConfig { + active_vault: Some(a.clone()), + vaults: vec![ + VaultConfig { + path: a.clone(), + ..VaultConfig::default() + }, + VaultConfig { + path: b.clone(), + ..VaultConfig::default() + }, + ], + ..AppConfig::default() + })) + .build(tauri::test::mock_context(tauri::test::noop_assets())) + .unwrap(); + let state = app.state::(); + let held = state.note_save.lock().await; + let mut creating = Box::pin(create_note(app.state(), a.clone(), None, "Queued".into())); + let mut saving = Box::pin(save_note( + app.state(), + a.clone(), + alpha.path.clone(), + alpha.meta.clone(), + "# Shared\n\nQueued save".into(), + )); + assert!(creating.as_mut().now_or_never().is_none()); + assert!(saving.as_mut().now_or_never().is_none()); + state.config.lock().unwrap().active_vault = Some(b.clone()); + // Authorization fails before entering the write queue, not after disk access. + let rejected = create_note( + app.state(), + outside.path().to_string_lossy().into_owned(), + None, + "Forbidden".into(), + ) + .now_or_never() + .unwrap(); + assert!(rejected.is_err()); + drop(held); + let created = creating.await.unwrap(); + saving.await.unwrap(); + assert_eq!(Path::new(&created.path).parent(), Some(first.path())); + assert!(!second.path().join("Queued.md").exists()); + assert_eq!( + operations::read_note(&a, &alpha.path).unwrap().content, + "# Shared\n\nQueued save" + ); + assert_eq!(std::fs::read(&beta.path).unwrap(), beta_before); + + // A configured but inactive origin is valid for a later detached-window save. + save_note( + app.state(), + a.clone(), + alpha.path.clone(), + alpha.meta, + "# Shared\n\nDetached save".into(), + ) + .await + .unwrap(); + assert_eq!( + read_note(app.state(), a.clone(), alpha.path.clone()) + .await + .unwrap() + .content, + "# Shared\n\nDetached save" + ); + assert!(read_note(app.state(), b.clone(), alpha.path).await.is_err()); + assert_eq!(std::fs::read(&beta.path).unwrap(), beta_before); + } + + #[tokio::test] + async fn icon_imports_and_field_patches_serialize_with_cleanup_and_notebook_moves() { + let vault = tempfile::tempdir().unwrap(); + let path = vault.path().to_string_lossy().into_owned(); + operations::ensure_vault_structure(&path).unwrap(); + operations::create_notebook(&path, None, "Projects").unwrap(); + operations::create_notebook(&path, Some("Projects"), "Child").unwrap(); + operations::create_notebook(&path, None, "Archive").unwrap(); + let app = tauri::test::mock_builder() + .manage(AppState::new(AppConfig { + active_vault: Some(path.clone()), + ..AppConfig::default() + })) + .build(tauri::test::mock_context(tauri::test::noop_assets())) + .unwrap(); + let state = app.state::(); + let held = state.note_save.lock().await; + let mut color = Box::pin(set_tag_style( + app.state(), + path.clone(), + "Work".into(), + TagStyleChange::Color(Some("#AABBCC".into())), + )); + let mut tag_import = Box::pin(import_tag_icon( + app.state(), + path.clone(), + "work".into(), + "same.png".into(), + vec![1, 2, 3], + )); + let mut notebook_import = Box::pin(import_notebook_icon( + app.state(), + path.clone(), + "Projects/Child".into(), + "same.png".into(), + vec![4, 5, 6], + )); + let mut scan = Box::pin(find_orphaned_attachments(app.state(), path.clone())); + assert!(color.as_mut().now_or_never().is_none()); + assert!(tag_import.as_mut().now_or_never().is_none()); + assert!(notebook_import.as_mut().now_or_never().is_none()); + assert!(scan.as_mut().now_or_never().is_none()); + drop(held); + assert_eq!( + color.await.unwrap().unwrap().color.as_deref(), + Some("#aabbcc") + ); + let tag = tag_import.await.unwrap(); + let icon = notebook_import.await.unwrap(); + assert_eq!(tag.color.as_deref(), Some("#aabbcc")); + assert!(scan.await.unwrap().is_empty()); + let tag_icon = tag.icon.unwrap(); + assert_ne!(icon, tag_icon); + assert_eq!(std::fs::read(vault.path().join(&icon)).unwrap(), [4, 5, 6]); + assert_eq!( + std::fs::read(vault.path().join(&tag_icon)).unwrap(), + [1, 2, 3] + ); + let recolored = set_tag_style( + app.state(), + path.clone(), + "WORK".into(), + TagStyleChange::Color(Some("#112233".into())), + ) + .await + .unwrap() + .unwrap(); + assert_eq!(recolored.icon.as_deref(), Some(tag_icon.as_str())); + assert_eq!(recolored.color.as_deref(), Some("#112233")); + assert_eq!( + get_tag_styles(app.state(), path.clone()).await.unwrap()["Work"], + recolored + ); + + let held = state.note_save.lock().await; + let mut changing = Box::pin(set_notebook_icon( + app.state(), + path.clone(), + "Projects".into(), + Some("builtin:briefcase".into()), + )); + let mut renaming = Box::pin(rename_notebook( + app.state(), + path.clone(), + vault.path().join("Projects").to_string_lossy().into_owned(), + "Renamed".into(), + )); + assert!(changing.as_mut().now_or_never().is_none()); + assert!(renaming.as_mut().now_or_never().is_none()); + drop(held); + changing.await.unwrap(); + let renamed = renaming.await.unwrap(); + move_notebook( + app.state(), + path.clone(), + renamed, + vault.path().join("Archive").to_string_lossy().into_owned(), + ) + .await + .unwrap(); + let icons = get_notebook_icons(app.state(), path.clone()).await.unwrap(); + assert_eq!(icons.get("Archive/Renamed/Child"), Some(&icon)); + assert_eq!( + icons.get("Archive/Renamed").map(String::as_str), + Some("builtin:briefcase") + ); + assert!(!icons.contains_key("Projects/Child")); + let names = [&icon, &tag_icon] + .map(|relative| { + Path::new(relative) + .file_name() + .unwrap() + .to_string_lossy() + .into_owned() + }) + .to_vec(); + assert_eq!( + trash_orphaned_attachments(app.state(), path.clone(), names) + .await + .unwrap(), + 0 + ); + + state + .syncing + .store(true, std::sync::atomic::Ordering::Release); + assert!(find_orphaned_attachments(app.state(), path.clone()) + .await + .is_err()); + assert!(trash_orphaned_attachments(app.state(), path, Vec::new()) + .await + .is_err()); + state + .syncing + .store(false, std::sync::atomic::Ordering::Release); + } + + #[tokio::test] + async fn note_creation_and_save_work_without_an_initialized_search_index() { + let vault = tempfile::tempdir().unwrap(); + let vault_path = vault.path().to_string_lossy().into_owned(); + operations::ensure_vault_structure(&vault_path).unwrap(); + let app = tauri::test::mock_builder() + .manage(AppState::new(AppConfig { + active_vault: Some(vault_path.clone()), + ..AppConfig::default() + })) + .build(tauri::test::mock_context(tauri::test::noop_assets())) + .unwrap(); + let note = create_note(app.state(), vault_path.clone(), None, "Draft".into()) + .await + .unwrap(); + save_note( + app.state(), + vault_path.clone(), + note.path.clone(), + note.meta, + "# Draft\n\nSaved before search is ready.".into(), + ) + .await + .unwrap(); + let saved = operations::read_note(&vault_path, ¬e.path).unwrap(); + assert_eq!(saved.meta.title, "Draft"); + assert_eq!(saved.content, "# Draft\n\nSaved before search is ready."); + } + + #[tokio::test] + async fn move_and_trash_wait_for_pending_saves_and_keep_the_latest_content() { + let vault = tempfile::tempdir().unwrap(); + let vault_path = vault.path().to_string_lossy().into_owned(); + let archive = vault.path().join("Archive"); + std::fs::create_dir(&archive).unwrap(); + let entry = operations::create_note(&vault_path, None, "Note").unwrap(); + let app = tauri::test::mock_builder() + .manage(AppState::new(AppConfig { + active_vault: Some(vault_path.clone()), + ..AppConfig::default() + })) + .build(tauri::test::mock_context(tauri::test::noop_assets())) + .unwrap(); + let state = app.state::(); + + let saving = state.note_save.lock().await; + let mut moving = Box::pin(move_note( + app.state(), + vault_path.clone(), + entry.path.clone(), + archive.to_string_lossy().into_owned(), + )); + assert!(moving.as_mut().now_or_never().is_none()); + operations::save_note(&vault_path, &entry.path, &entry.meta, "Latest saved text").unwrap(); + drop(saving); + let moved_path = moving.await.unwrap(); + assert!(!Path::new(&entry.path).exists()); + let moved = operations::read_note(&vault_path, &moved_path).unwrap(); + assert_eq!(moved.content, "# Note\n\nLatest saved text"); + + let saving = state.note_save.lock().await; + let mut deleting = Box::pin(delete_note( + app.state(), + vault_path.clone(), + moved_path.clone(), + )); + assert!(deleting.as_mut().now_or_never().is_none()); + operations::save_note( + &vault_path, + &moved_path, + &moved.meta, + "Latest text before trash", + ) + .unwrap(); + drop(saving); + deleting.await.unwrap(); + assert!(!Path::new(&moved_path).exists()); + let trash = operations::get_trash_contents(&vault_path).unwrap(); + assert_eq!(trash.notes.len(), 1); + let raw = std::fs::read_to_string(&trash.notes[0].path).unwrap(); + let (_, content) = crate::vault::frontmatter::parse_note(&raw, "Note.md"); + assert_eq!(content, "# Note\n\nLatest text before trash"); + } +} diff --git a/src-tauri/src/lib.rs b/src-tauri/src/lib.rs index f0a7ca3..dfa14d1 100644 --- a/src-tauri/src/lib.rs +++ b/src-tauri/src/lib.rs @@ -37,8 +37,6 @@ pub fn run() { let config = commands::load_app_config(); #[cfg(desktop)] let show_tray = config.show_tray_icon; - #[cfg(desktop)] - let close_to_tray = config.close_to_tray && show_tray; let app_state = AppState::new(config); // Inject the compile-time platform so the frontend never sniffs the (sometimes @@ -164,6 +162,7 @@ pub fn run() { commands::restore_external_vault, commands::remove_vault, commands::get_app_config, + commands::complete_window_close, commands::set_theme, commands::set_system_themes, commands::set_accent_color, @@ -216,8 +215,10 @@ pub fn run() { commands::save_attachment, commands::get_notebook_icons, commands::set_notebook_icon, + commands::import_notebook_icon, commands::get_tag_styles, commands::set_tag_style, + commands::import_tag_icon, commands::set_general_settings, commands::get_quick_access, commands::add_quick_access, @@ -337,32 +338,44 @@ pub fn run() { queue_macos_traffic_light_fix(window); } - match event { - tauri::WindowEvent::CloseRequested { api, .. } => { - // Only hide to tray for the main window - if close_to_tray && window.label() == "main" { - api.prevent_close(); - let _ = window.hide(); + // Closing the main window also requests a save-aware close of note windows. + if matches!(event, tauri::WindowEvent::Destroyed) && window.label() == "main" { + for (label, win) in window.app_handle().webview_windows() { + if label.starts_with("note-") { + let _ = win.close(); } } - tauri::WindowEvent::Destroyed - // When main window is destroyed, close all note windows - if window.label() == "main" => { - let app = window.app_handle(); - for (label, win) in app.webview_windows() { - if label.starts_with("note-") { - let _ = win.close(); - } - } - } - _ => {} } }); } - builder - .run(tauri::generate_context!()) - .expect("error while running tauri application"); + let app = builder + .build(tauri::generate_context!()) + .expect("error while building tauri application"); + app.run(|app, event| { + #[cfg(desktop)] + if let tauri::RunEvent::ExitRequested { + code: Some(_), api, .. + } = event + { + let windows = app.webview_windows(); + if !windows.is_empty() { + // Menu/tray Quit must give each editor the same save opportunity + // as a window close. The app exits once the last window closes. + api.prevent_exit(); + app.state::() + .quitting + .store(true, std::sync::atomic::Ordering::Relaxed); + for window in windows.values() { + if let Err(error) = window.close() { + log::error!("Failed to close window during quit: {error}"); + } + } + } + } + #[cfg(mobile)] + let _ = (app, event); + }); } #[cfg(desktop)] @@ -371,7 +384,7 @@ fn setup_tray(app: &mut tauri::App) -> Result<(), Box> { let quit = MenuItemBuilder::with_id("quit", "Quit").build(app)?; let menu = MenuBuilder::new(app).items(&[&show, &quit]).build()?; - let _tray = TrayIconBuilder::new() + let _tray = TrayIconBuilder::with_id("helixnotes") .icon( Image::from_path("icons/32x32.png") .unwrap_or_else(|_| app.default_window_icon().cloned().unwrap()), diff --git a/src-tauri/src/search/mod.rs b/src-tauri/src/search/mod.rs index 6d0f6a9..d553f2c 100644 --- a/src-tauri/src/search/mod.rs +++ b/src-tauri/src/search/mod.rs @@ -1,24 +1,34 @@ -use crate::types::SearchResult; +use crate::types::{SearchMatch, SearchPage, SearchResult}; use crate::vault::frontmatter; use crate::vault::operations::helixnotes_dir; +use parking_lot::{Condvar, Mutex}; +use std::cmp::Reverse; +use std::collections::{HashMap, HashSet}; use std::fs; use std::path::Path; -use std::sync::Mutex; -use tantivy::collector::TopDocs; +use std::sync::Arc; +use std::time::{Duration, UNIX_EPOCH}; +use tantivy::collector::{Count, TopDocs}; #[cfg(desktop)] use tantivy::directory::MmapDirectory; #[cfg(mobile)] use tantivy::directory::RamDirectory; -use tantivy::query::{BooleanQuery, FuzzyTermQuery, Occur, PhrasePrefixQuery, Query, TermQuery}; +use tantivy::query::{BooleanQuery, BoostQuery, FuzzyTermQuery, Occur, Query, TermQuery}; use tantivy::schema::*; use tantivy::tokenizer::{LowerCaser, TextAnalyzer, Token, TokenStream, Tokenizer}; -use tantivy::{Index, IndexWriter, TantivyDocument, Term}; +use tantivy::{ + DocId, Index, IndexReader, IndexWriter, ReloadPolicy, SegmentReader, TantivyDocument, Term, +}; use walkdir::WalkDir; /// Bumped whenever the index schema or tokenizer changes, so the on-disk index is /// wiped and rebuilt once on the next vault open (the index is derived from the /// notes, so this never loses data). -const INDEX_SCHEMA_VERSION: &str = "2-cjk-bigram"; +const INDEX_SCHEMA_VERSION: &str = "4-stored-body-streaming-cjk"; +const INDEX_BATCH_DELAY: Duration = Duration::from_millis(300); +const MAX_PAGE_SIZE: usize = 100; +const SNIPPET_CHARS: usize = 240; +const SNIPPET_LEADING_CHARS: usize = 80; /// Per-vault search index dir in local app-data, kept out of the (possibly synced) vault. #[cfg(desktop)] @@ -48,107 +58,60 @@ fn is_cjk(c: char) -> bool { ) } -/// Tokenize `text` so that CJK runs become overlapping unigrams + bigrams (recall -/// over precision: a query for any substring of a CJK run will match), while runs -/// of other alphanumeric characters become a single word token (same as tantivy's -/// SimpleTokenizer, so Latin/English indexing is unchanged). Lowercasing is applied -/// by a LowerCaser filter in the analyzer, not here. -fn cjk_tokens(text: &str) -> Vec { - let mut tokens: Vec = Vec::new(); - let mut position: usize = 0; - let mut cjk_run: Vec<(usize, char)> = Vec::new(); - let mut word = String::new(); - let mut word_start: usize = 0; - - macro_rules! flush_word { - () => {{ - if !word.is_empty() { - let len = word.len(); - tokens.push(Token { - offset_from: word_start, - offset_to: word_start + len, - position, - text: std::mem::take(&mut word), - position_length: 1, - }); - position += 1; - } - }}; - } - macro_rules! flush_cjk { - () => {{ - let n = cjk_run.len(); - for i in 0..n { - let (off, ch) = cjk_run[i]; - tokens.push(Token { - offset_from: off, - offset_to: off + ch.len_utf8(), - position, - text: ch.to_string(), - position_length: 1, - }); - position += 1; - } - for i in 0..n.saturating_sub(1) { - let (off1, c1) = cjk_run[i]; - let (off2, c2) = cjk_run[i + 1]; - let mut s = String::with_capacity(c1.len_utf8() + c2.len_utf8()); - s.push(c1); - s.push(c2); - tokens.push(Token { - offset_from: off1, - offset_to: off2 + c2.len_utf8(), - position, - text: s, - position_length: 1, - }); - position += 1; - } - cjk_run.clear(); - }}; - } - - for (offset, c) in text.char_indices() { - if is_cjk(c) { - flush_word!(); - cjk_run.push((offset, c)); - } else if c.is_alphanumeric() { - flush_cjk!(); - if word.is_empty() { - word_start = offset; - } - word.push(c); - } else { - flush_cjk!(); - flush_word!(); - } - } - flush_cjk!(); - flush_word!(); - let _ = position; // the final flush increments position but nothing reads it after - tokens +/// Stream the same word / CJK uni-bigram tokens for indexing and highlighting. +/// Tokens stay in byte-offset order, without retaining a full document's tokens. +struct CjkTokenStream<'a> { + text: &'a str, + chars: std::iter::Peekable>, + token: Token, + bigram_end: Option, } -/// A pre-computed token stream (all tokens collected up front by `cjk_tokens`). -struct PreTokenizedStream { - tokens: Vec, - idx: usize, -} - -impl TokenStream for PreTokenizedStream { +impl TokenStream for CjkTokenStream<'_> { fn advance(&mut self) -> bool { - if self.idx < self.tokens.len() { - self.idx += 1; - true - } else { - false + self.token.position = self.token.position.wrapping_add(1); + self.token.text.clear(); + if let Some(end) = self.bigram_end.take() { + self.token.offset_to = end; + self.token + .text + .push_str(&self.text[self.token.offset_from..end]); + return true; } + while let Some((start, c)) = self.chars.next() { + if !c.is_alphanumeric() { + continue; + } + let mut end = start + c.len_utf8(); + if is_cjk(c) { + if let Some(&(offset, next)) = self.chars.peek() { + if next.is_alphanumeric() && is_cjk(next) { + self.bigram_end = Some(offset + next.len_utf8()); + } + } + } else { + while let Some(&(offset, next)) = self.chars.peek() { + if !next.is_alphanumeric() || is_cjk(next) { + break; + } + end = offset + next.len_utf8(); + self.chars.next(); + } + } + self.token.offset_from = start; + self.token.offset_to = end; + self.token.text.push_str(&self.text[start..end]); + return true; + } + false } + fn token(&self) -> &Token { - &self.tokens[self.idx - 1] + &self.token } + fn token_mut(&mut self) -> &mut Token { - &mut self.tokens[self.idx - 1] + &mut self.token } } @@ -156,27 +119,243 @@ impl TokenStream for PreTokenizedStream { struct CjkTokenizer; impl Tokenizer for CjkTokenizer { - type TokenStream<'a> = PreTokenizedStream; - fn token_stream<'a>(&'a mut self, text: &'a str) -> PreTokenizedStream { - PreTokenizedStream { - tokens: cjk_tokens(text), - idx: 0, + type TokenStream<'a> = CjkTokenStream<'a>; + + fn token_stream<'a>(&'a mut self, text: &'a str) -> CjkTokenStream<'a> { + CjkTokenStream { + text, + chars: text.char_indices().peekable(), + token: Token::default(), + bigram_end: None, + } + } +} + +struct SearchTerm { + text: String, + cjk: bool, +} + +impl SearchTerm { + fn matches(&self, token: &str) -> bool { + if self.cjk { + token == self.text + } else { + token.starts_with(&self.text) || within_one_edit(&self.text, token) + } + } +} + +/// Distance at most one, including one adjacent transposition, as in Tantivy's +/// fuzzy query. Only the suffix after the first differing character is examined. +fn within_one_edit(left: &str, right: &str) -> bool { + let mut left = left.chars(); + let mut right = right.chars(); + loop { + match (left.next(), right.next()) { + (Some(a), Some(b)) if a == b => continue, + (Some(a), Some(b)) => { + let left = left.as_str(); + let right = right.as_str(); + return left == right + || left.strip_prefix(b) == Some(right) + || right.strip_prefix(a) == Some(left) + || matches!( + (left.strip_prefix(b), right.strip_prefix(a)), + (Some(left), Some(right)) if left == right + ); + } + (None, None) => return true, + (Some(_), None) => return left.next().is_none(), + (None, Some(_)) => return right.next().is_none(), + } + } +} + +fn push_match(matches: &mut Vec, start: usize, end: usize) { + if let Some(last) = matches.last_mut() { + if start <= last.end { + last.end = last.end.max(end); + return; + } + } + matches.push(SearchMatch { start, end }); +} + +/// Convert sorted, merged byte offsets in one forward pass; never count from +/// the beginning of a long note for every match. +fn utf16_matches(text: &str, mut matches: Vec, prefix: usize) -> Vec { + let mut byte = 0; + let mut utf16 = prefix; + for range in &mut matches { + utf16 += text[byte..range.start].encode_utf16().count(); + byte = range.start; + range.start = utf16; + utf16 += text[byte..range.end].encode_utf16().count(); + byte = range.end; + range.end = utf16; + } + matches +} + +fn title_matches( + title: &str, + terms: &[SearchTerm], + analyzer: &mut TextAnalyzer, +) -> Vec { + let mut matches = Vec::new(); + let mut stream = analyzer.token_stream(title); + while stream.advance() { + let token = stream.token(); + if terms.iter().any(|term| term.matches(&token.text)) { + push_match(&mut matches, token.offset_from, token.offset_to); + } + } + utf16_matches(title, matches, 0) +} + +fn body_snippet( + body: &str, + terms: &[SearchTerm], + analyzer: &mut TextAnalyzer, +) -> (String, Vec) { + let mut stream = analyzer.token_stream(body); + let mut start = 0; + let mut end = body + .char_indices() + .nth(SNIPPET_CHARS) + .map_or(body.len(), |(offset, _)| offset); + let mut found = false; + let mut matches = Vec::new(); + while stream.advance() { + let token = stream.token(); + if found && token.offset_from >= end { + break; + } + if !terms.iter().any(|term| term.matches(&token.text)) { + continue; + } + if !found { + start = body[..token.offset_from] + .char_indices() + .rev() + .nth(SNIPPET_LEADING_CHARS - 1) + .map_or(0, |(offset, _)| offset); + end = body[start..] + .char_indices() + .nth(SNIPPET_CHARS) + .map_or(body.len(), |(offset, _)| start + offset); + found = true; + } + push_match( + &mut matches, + token.offset_from - start, + token.offset_to.min(end) - start, + ); + } + let context = &body[start..end]; + let prefix = usize::from(start > 0); + let matches = utf16_matches(context, matches, prefix); + let mut snippet = String::with_capacity(context.len() + 6); + if start > 0 { + snippet.push('…'); + } + snippet.push_str(context); + if end < body.len() { + snippet.push('…'); + } + (snippet, matches) +} + +#[derive(Clone, Copy, Debug, PartialEq, Eq)] +struct FileStamp { + seconds: i64, + nanos: i64, + size: u64, +} + +impl FileStamp { + fn read(path: &Path) -> std::io::Result { + let metadata = fs::metadata(path)?; + let modified = metadata.modified()?; + let (duration, sign) = match modified.duration_since(UNIX_EPOCH) { + Ok(duration) => (duration, 1), + Err(error) => (error.duration(), -1), + }; + Ok(Self { + seconds: duration.as_secs() as i64 * sign, + nanos: i64::from(duration.subsec_nanos()) * sign, + size: metadata.len(), + }) + } +} + +#[derive(Default)] +struct IndexQueue { + pending: HashSet, + refresh: Option, + running: bool, + initial_refresh_pending: bool, +} + +#[derive(Default)] +pub(crate) struct SearchIndexShutdown { + finished: Mutex, + ready: Condvar, + #[cfg(test)] + wait_started: std::sync::atomic::AtomicBool, +} + +impl SearchIndexShutdown { + pub(crate) fn is_finished(&self) -> bool { + self.finished.try_lock().is_some_and(|finished| *finished) + } + + pub(crate) fn wait(&self) { + #[cfg(test)] + self.wait_started + .store(true, std::sync::atomic::Ordering::Release); + let mut finished = self.finished.lock(); + while !*finished { + self.ready.wait(&mut finished); } } } pub struct SearchIndex { index: Index, + reader: IndexReader, writer: Mutex>, + queue: Mutex, + queue_ready: Condvar, + shutdown: Arc, #[allow(dead_code)] schema: Schema, path_field: Field, title_field: Field, body_field: Field, tags_field: Field, + modified_seconds_field: Field, + modified_nanos_field: Field, + size_field: Field, +} + +impl Drop for SearchIndex { + fn drop(&mut self) { + let mut finished = self.shutdown.finished.lock(); + // Arc's last strong reference disappears before Drop runs. Do not let + // a reopening vault create a writer until Tantivy releases its lock. + drop(self.writer.get_mut().take()); + *finished = true; + self.shutdown.ready.notify_all(); + } } impl SearchIndex { + pub(crate) fn shutdown_signal(&self) -> Arc { + self.shutdown.clone() + } + pub fn new(vault_path: &str) -> Result { let mut schema_builder = Schema::builder(); let path_field = schema_builder.add_text_field("path", STRING | STORED); @@ -188,8 +367,11 @@ impl SearchIndex { let cjk_text = TextOptions::default().set_indexing_options(cjk_indexing.clone()); let cjk_text_stored = cjk_text.clone().set_stored(); let title_field = schema_builder.add_text_field("title", cjk_text_stored.clone()); - let body_field = schema_builder.add_text_field("body", cjk_text); + let body_field = schema_builder.add_text_field("body", cjk_text_stored.clone()); let tags_field = schema_builder.add_text_field("tags", cjk_text_stored); + let modified_seconds_field = schema_builder.add_i64_field("modified_seconds", STORED); + let modified_nanos_field = schema_builder.add_i64_field("modified_nanos", STORED); + let size_field = schema_builder.add_u64_field("size", STORED); let schema = schema_builder.build(); // Mobile: use in-memory index (flock is unreliable on the sandboxed/FUSE filesystem) @@ -211,7 +393,7 @@ impl SearchIndex { let _ = fs::remove_dir_all(hn.join("search_index")); let _ = fs::remove_file(hn.join("search_index.version")); - // One-time wipe when the schema/tokenizer version changes; rebuild() repopulates. + // One-time wipe when the schema/tokenizer changes; refresh repopulates. let version_ok = fs::read_to_string(&version_path) .map(|v| v.trim() == INDEX_SCHEMA_VERSION) .unwrap_or(false); @@ -249,186 +431,1090 @@ impl SearchIndex { let heap_size = 50_000_000; let writer = index.writer(heap_size).map_err(|e| e.to_string())?; + let reader = index + .reader_builder() + .reload_policy(ReloadPolicy::Manual) + .try_into() + .map_err(|e: tantivy::TantivyError| e.to_string())?; + let initial_refresh_pending = index.load_metas().map_err(|e| e.to_string())?.opstamp == 0; Ok(Self { index, + reader, writer: Mutex::new(Some(writer)), + queue: Mutex::new(IndexQueue { + initial_refresh_pending, + ..IndexQueue::default() + }), + queue_ready: Condvar::new(), + shutdown: Arc::new(SearchIndexShutdown::default()), schema, path_field, title_field, body_field, tags_field, + modified_seconds_field, + modified_nanos_field, + size_field, }) } pub fn rebuild(&self, vault_path: &str) -> Result<(), String> { - let mut writer_guard = self.writer.lock().map_err(|e| e.to_string())?; + let mut writer_guard = self.writer.lock(); let writer = writer_guard.as_mut().ok_or("Writer not available")?; - writer.delete_all_documents().map_err(|e| e.to_string())?; - - let hn_dir = helixnotes_dir(vault_path); - - for entry in WalkDir::new(vault_path) - .into_iter() - .filter_entry(|e| { - let p = e.path(); - !p.starts_with(&hn_dir) - && !p - .file_name() - .and_then(|n| n.to_str()) - .map(|n| n.starts_with('.')) - .unwrap_or(false) - }) - .filter_map(|e| e.ok()) - .filter(|e| { - e.file_type().is_file() - && e.path().extension().and_then(|x| x.to_str()) == Some("md") - }) - { - if let Ok(raw) = fs::read_to_string(entry.path()) { - let filename = entry.file_name().to_string_lossy().to_string(); - let (meta, content) = frontmatter::parse_note(&raw, &filename); - let path_str = entry.path().to_string_lossy().to_string(); - - let mut doc = TantivyDocument::new(); - doc.add_text(self.path_field, &path_str); - doc.add_text(self.title_field, &meta.title); - doc.add_text(self.body_field, &content); - doc.add_text(self.tags_field, meta.tags.join(" ")); - let _ = writer.add_document(doc); + for entry in Self::vault_entries(vault_path) { + let entry = match entry { + Ok(entry) => entry, + Err(error) => { + let _ = writer.rollback(); + return Err(error.to_string()); + } + }; + if !Self::is_note(&entry) { + continue; + } + let path = entry.path().to_string_lossy(); + match self.note_document(&path) { + Ok(Some(doc)) => { + if let Err(error) = writer.add_document(doc) { + let _ = writer.rollback(); + return Err(error.to_string()); + } + } + Ok(None) => {} + Err(error) => { + let _ = writer.rollback(); + return Err(error); + } } } + self.commit(writer) + } - writer.commit().map_err(|e| e.to_string())?; + fn vault_entries(vault_path: &str) -> impl Iterator> { + let root = Path::new(vault_path).to_path_buf(); + WalkDir::new(vault_path) + .into_iter() + .filter_entry(move |entry| { + entry.path() == root + || !entry + .file_name() + .to_str() + .map(|name| name.starts_with('.')) + .unwrap_or(false) + }) + } + + fn is_note(entry: &walkdir::DirEntry) -> bool { + entry.file_type().is_file() + && entry.path().extension().and_then(|ext| ext.to_str()) == Some("md") + } + + /// Reconcile persistent entries on the same worker as note updates. Enqueuing + /// never traverses the vault or waits for the writer; the cached reader is live. + pub fn queue_vault_refresh(self: &Arc, vault_path: &str) -> Result<(), String> { + let mut queue = self.queue.lock(); + queue.refresh = Some(vault_path.to_owned()); + self.start_worker(&mut queue) + } + + /// Schedule a fixed-window batch, rather than delaying every time a note changes. + /// The worker owns the index only until its queue is empty. + pub fn queue_index_note(self: &Arc, path: &str) -> Result<(), String> { + let mut queue = self.queue.lock(); + if !queue.pending.contains(path) { + queue.pending.insert(path.to_owned()); + } + self.start_worker(&mut queue) + } + + fn start_worker(self: &Arc, queue: &mut IndexQueue) -> Result<(), String> { + if queue.running { + return Ok(()); + } + queue.running = true; + let search = Arc::clone(self); + if let Err(error) = std::thread::Builder::new() + .name("search-index".into()) + .spawn(move || search.drain_index_queue()) + { + queue.running = false; + self.queue_ready.notify_all(); + return Err(format!("Could not start search index worker: {error}")); + } Ok(()) } - pub fn index_note(&self, path: &str) -> Result<(), String> { - let p = Path::new(path); - let raw = fs::read_to_string(p).map_err(|e| e.to_string())?; - let filename = p - .file_name() - .unwrap_or_default() - .to_string_lossy() - .to_string(); - let (meta, content) = frontmatter::parse_note(&raw, &filename); + fn drain_index_queue(&self) { + let mut batch = HashSet::new(); + loop { + std::thread::sleep(INDEX_BATCH_DELAY); + let refresh = { + let mut queue = self.queue.lock(); + std::mem::swap(&mut batch, &mut queue.pending); + queue.refresh.take() + }; + if let Some(vault_path) = refresh { + if let Err(error) = self.refresh_vault(&vault_path) { + log::error!("Could not reconcile search index: {error}"); + } + self.finish_initial_refresh(); + } + if let Err(error) = self.index_batch(&batch) { + log::error!("Could not commit search index batch: {error}"); + } + batch.clear(); - let mut writer_guard = self.writer.lock().map_err(|e| e.to_string())?; + // Clearing `running` and checking for more work share the enqueue lock, + // so an edit arriving as the worker exits always starts another worker. + let mut queue = self.queue.lock(); + if queue.pending.is_empty() && queue.refresh.is_none() { + queue.running = false; + self.queue_ready.notify_all(); + return; + } + } + } + + fn finish_initial_refresh(&self) { + let mut queue = self.queue.lock(); + queue.initial_refresh_pending = false; + self.queue_ready.notify_all(); + } + + fn indexed_stamps(&self) -> Result, String> { + let searcher = self.reader.searcher(); + let mut stamps = HashMap::new(); + for (segment_ord, segment) in searcher.segment_readers().iter().enumerate() { + for doc_id in segment.doc_ids_alive() { + let doc: TantivyDocument = searcher + .doc(tantivy::DocAddress::new(segment_ord as u32, doc_id)) + .map_err(|e| e.to_string())?; + if let Some(path) = doc.get_first(self.path_field).and_then(|v| v.as_str()) { + stamps.insert( + path.to_owned(), + FileStamp { + seconds: doc + .get_first(self.modified_seconds_field) + .and_then(|v| v.as_i64()) + .unwrap_or_default(), + nanos: doc + .get_first(self.modified_nanos_field) + .and_then(|v| v.as_i64()) + .unwrap_or_default(), + size: doc + .get_first(self.size_field) + .and_then(|v| v.as_u64()) + .unwrap_or_default(), + }, + ); + } + } + } + Ok(stamps) + } + + fn refresh_vault(&self, vault_path: &str) -> Result<(), String> { + // Serialize reads and mutations with queued edits, removals and rebuilds. + // Never collect note bodies before taking this lock: a stale snapshot + // could otherwise replace an already-committed save. + let mut writer_guard = self.writer.lock(); let writer = writer_guard.as_mut().ok_or("Writer not available")?; + self.reader.reload().map_err(|e| e.to_string())?; + let mut indexed = self.indexed_stamps()?; + let mut changed = false; + let mut complete = true; + for entry in Self::vault_entries(vault_path) { + let entry = match entry { + Ok(entry) => entry, + Err(error) => { + log::error!("Could not inspect vault for search: {error}"); + complete = false; + continue; + } + }; + if !Self::is_note(&entry) { + continue; + } + let path = entry.path().to_string_lossy(); + let previous = indexed.remove(path.as_ref()); + match FileStamp::read(entry.path()) { + Ok(stamp) if previous == Some(stamp) => continue, + Ok(_) => {} + Err(error) => { + log::error!("Could not inspect note {path}: {error}"); + continue; + } + } + match self.note_document(&path) { + Ok(doc) => { + self.replace_document(writer, &path, doc)?; + changed = true; + } + Err(error) => log::error!("Could not index note {path}: {error}"), + } + } + if complete { + for path in indexed.keys() { + // Inaccessible directories and transient I/O failures must not + // turn a partial traversal into deletion of valid entries. + let removed = match fs::metadata(path) { + Ok(metadata) => !metadata.is_file(), + Err(error) => error.kind() == std::io::ErrorKind::NotFound, + }; + if removed { + writer.delete_term(Term::from_field_text(self.path_field, path)); + changed = true; + } + } + } + if changed { + self.commit(writer)?; + } + Ok(()) + } - // Delete old entry - let term = tantivy::Term::from_field_text(self.path_field, path); - writer.delete_term(term); + fn index_batch(&self, paths: &HashSet) -> Result<(), String> { + if paths.is_empty() { + return Ok(()); + } + let mut writer_guard = self.writer.lock(); + let writer = writer_guard.as_mut().ok_or("Writer not available")?; + let mut changed = false; + for path in paths { + match self.note_document(path) { + Ok(doc) => { + self.replace_document(writer, path, doc)?; + changed = true; + } + Err(error) => log::error!("Could not index note {path}: {error}"), + } + } + if changed { + self.commit(writer)?; + } + Ok(()) + } - // Add updated + fn note_document(&self, path: &str) -> Result, String> { + let p = Path::new(path); + let stamp = match FileStamp::read(p) { + Ok(stamp) => stamp, + Err(error) if error.kind() == std::io::ErrorKind::NotFound => return Ok(None), + Err(error) => return Err(error.to_string()), + }; + let raw = match fs::read_to_string(p) { + Ok(raw) => raw, + Err(error) if error.kind() == std::io::ErrorKind::NotFound => return Ok(None), + Err(error) => return Err(error.to_string()), + }; + if FileStamp::read(p).map_err(|e| e.to_string())? != stamp { + return Err("Note changed while being indexed".to_string()); + } + let filename = p.file_name().unwrap_or_default().to_string_lossy(); + let (meta, content) = frontmatter::parse_note(&raw, &filename); let mut doc = TantivyDocument::new(); doc.add_text(self.path_field, path); doc.add_text(self.title_field, &meta.title); doc.add_text(self.body_field, &content); doc.add_text(self.tags_field, meta.tags.join(" ")); - let _ = writer.add_document(doc); + doc.add_i64(self.modified_seconds_field, stamp.seconds); + doc.add_i64(self.modified_nanos_field, stamp.nanos); + doc.add_u64(self.size_field, stamp.size); + Ok(Some(doc)) + } - writer.commit().map_err(|e| e.to_string())?; + fn replace_document( + &self, + writer: &mut IndexWriter, + path: &str, + doc: Option, + ) -> Result<(), String> { + writer.delete_term(Term::from_field_text(self.path_field, path)); + if let Some(doc) = doc { + if let Err(error) = writer.add_document(doc) { + let _ = writer.rollback(); + return Err(error.to_string()); + } + } + Ok(()) + } + + fn commit(&self, writer: &mut IndexWriter) -> Result<(), String> { + if let Err(error) = writer.commit() { + let _ = writer.rollback(); + return Err(error.to_string()); + } + self.reader.reload().map_err(|e| e.to_string())?; + self.finish_initial_refresh(); Ok(()) } pub fn remove_note(&self, path: &str) -> Result<(), String> { - let mut writer_guard = self.writer.lock().map_err(|e| e.to_string())?; + let mut writer_guard = self.writer.lock(); let writer = writer_guard.as_mut().ok_or("Writer not available")?; - let term = tantivy::Term::from_field_text(self.path_field, path); - writer.delete_term(term); - writer.commit().map_err(|e| e.to_string())?; - Ok(()) + writer.delete_term(Term::from_field_text(self.path_field, path)); + self.commit(writer) } - pub fn search(&self, query_str: &str, limit: usize) -> Result, String> { - let reader = self.index.reader().map_err(|e| e.to_string())?; - let searcher = reader.searcher(); - - let fields = [self.title_field, self.body_field, self.tags_field]; - // Tokenize the query with the SAME CJK-aware analyzer used for indexing, so a - // Chinese/Japanese/Korean query becomes the same uni/bigram tokens as the docs. - // (For pure-ASCII queries this yields the same lowercased word tokens as before.) + pub fn search( + &self, + query_str: &str, + limit: usize, + offset: usize, + ) -> Result { let mut analyzer = self .index .tokenizers() .get("cjk") .ok_or("cjk tokenizer not registered")?; - let terms: Vec = { + let terms: Vec = { let mut out = Vec::new(); let mut stream = analyzer.token_stream(query_str); while stream.advance() { out.push(stream.token().text.clone()); } - out + out.sort_unstable(); + out.dedup(); + out.into_iter() + .map(|text| SearchTerm { + cjk: text.chars().any(is_cjk), + text, + }) + .collect() }; - - // For each term, OR queries across all fields, then AND all terms together. - // CJK tokens (uni/bigrams) already encode segmentation -> exact term match. - // Latin tokens keep prefix + fuzzy matching (unchanged English behaviour). + // A zero-clause BooleanQuery must never become a browse-all query. + if terms.is_empty() { + return Ok(SearchPage::default()); + } + { + let mut queue = self.queue.lock(); + // A cold/schema-migrated index has no useful cached answer. Search + // runs off-thread and waits for its first queued refresh, not for + // later maintenance of an already-committed persistent index. + while queue.initial_refresh_pending && queue.running { + self.queue_ready.wait(&mut queue); + } + } + let searcher = self.reader.searcher(); + // Native BM25 exact matches receive a 4x boost; title/tag/body weights + // are 4/2/1. Prefix matches add 1x and one-edit typos only 0.5x. + // Each distinct term is required, but can occur in any indexed field. + let fields = [ + (self.title_field, 4.0), + (self.tags_field, 2.0), + (self.body_field, 1.0), + ]; let term_queries: Vec<(Occur, Box)> = terms .iter() .map(|term| { - let is_cjk_term = term.chars().any(is_cjk); - let field_queries: Vec<(Occur, Box)> = fields - .iter() - .flat_map(|&field| { - if is_cjk_term { - let exact: Box = Box::new(TermQuery::new( - Term::from_field_text(field, term), + let mut field_queries: Vec<(Occur, Box)> = Vec::new(); + for &(field, field_boost) in &fields { + let indexed_term = Term::from_field_text(field, &term.text); + field_queries.push(( + Occur::Should, + Box::new(BoostQuery::new( + Box::new(TermQuery::new( + indexed_term.clone(), IndexRecordOption::WithFreqs, - )); - vec![(Occur::Should, exact)] - } else { - let prefix: Box = - Box::new(PhrasePrefixQuery::new(vec![Term::from_field_text( - field, term, - )])); - let fuzzy: Box = Box::new(FuzzyTermQuery::new( - Term::from_field_text(field, term), - 1, - true, - )); - vec![(Occur::Should, prefix), (Occur::Should, fuzzy)] - } - }) - .collect(); + )), + field_boost * 4.0, + )), + )); + if !term.cjk { + // A zero-edit prefix automaton has no expansion cap, + // unlike PhrasePrefixQuery, so short prefixes keep all hits. + field_queries.push(( + Occur::Should, + Box::new(BoostQuery::new( + Box::new(FuzzyTermQuery::new_prefix(indexed_term.clone(), 0, true)), + field_boost, + )), + )); + field_queries.push(( + Occur::Should, + Box::new(BoostQuery::new( + Box::new(FuzzyTermQuery::new(indexed_term, 1, true)), + field_boost * 0.5, + )), + )); + } + } let combined: Box = Box::new(BooleanQuery::new(field_queries)); (Occur::Must, combined) }) .collect(); - let query = BooleanQuery::new(term_queries); - - let top_docs = searcher - .search(&query, &TopDocs::with_limit(limit)) + let doc_count = usize::try_from(searcher.num_docs()).unwrap_or(usize::MAX); + if offset >= doc_count { + return Ok(SearchPage { + results: Vec::new(), + total: searcher.search(&query, &Count).map_err(|e| e.to_string())?, + }); + } + let limit = limit.clamp(1, MAX_PAGE_SIZE).min(doc_count - offset); + let segment_order: HashMap<_, _> = searcher + .segment_readers() + .iter() + .enumerate() + .map(|(ordinal, segment)| (segment.segment_id(), ordinal)) + .collect(); + // TopDocs alone does not promise stable ties. Include the complete + // document address without changing the reported relevance score. + let top_docs = TopDocs::with_limit(limit).and_offset(offset).tweak_score( + move |segment: &SegmentReader| { + let ordinal = segment_order[&segment.segment_id()]; + move |doc: DocId, score: f32| (score, Reverse(ordinal), Reverse(doc)) + }, + ); + let (top_docs, total) = searcher + .search(&query, &(top_docs, Count)) .map_err(|e| e.to_string())?; - - let mut results = Vec::new(); - for (score, doc_address) in top_docs { + let mut results = Vec::with_capacity(top_docs.len()); + for ((score, _, _), doc_address) in top_docs { let doc: TantivyDocument = searcher.doc(doc_address).map_err(|e| e.to_string())?; - let path = doc .get_first(self.path_field) .and_then(|v| v.as_str()) - .unwrap_or("") - .to_string(); - + .unwrap_or(""); let title = doc .get_first(self.title_field) .and_then(|v| v.as_str()) - .unwrap_or("") - .to_string(); - + .unwrap_or(""); + let body = doc + .get_first(self.body_field) + .and_then(|v| v.as_str()) + .unwrap_or(""); + let title_matches = title_matches(title, &terms, &mut analyzer); + let (snippet, snippet_matches) = body_snippet(body, &terms, &mut analyzer); results.push(SearchResult { - path, - title, - snippet: String::new(), + path: path.to_owned(), + title: title.to_owned(), + snippet, score, + title_matches, + snippet_matches, }); } + Ok(SearchPage { results, total }) + } +} - Ok(results) +#[cfg(test)] +mod tests { + use super::*; + use std::path::PathBuf; + + struct TestVault(PathBuf); + + impl TestVault { + fn new() -> Self { + let path = + std::env::temp_dir().join(format!("helixnotes-search-{}", uuid::Uuid::new_v4())); + fs::create_dir_all(&path).unwrap(); + Self(path) + } + + fn index(&self) -> Arc { + Arc::new(SearchIndex::new(self.0.to_str().unwrap()).unwrap()) + } + } + + impl Drop for TestVault { + fn drop(&mut self) { + let _ = fs::remove_dir_all(&self.0); + #[cfg(desktop)] + if let Some(base) = vault_index_base(self.0.to_str().unwrap()) { + let _ = fs::remove_dir_all(base); + } + } + } + + fn result_paths(index: &SearchIndex, query: &str) -> Vec { + let mut paths: Vec<_> = index + .search(query, 10, 0) + .unwrap() + .results + .into_iter() + .map(|result| result.path) + .collect(); + paths.sort(); + paths + } + + fn write_search_note(vault: &TestVault, name: &str, title: &str, body: &str) -> String { + let path = vault.0.join(name); + fs::write( + &path, + format!("---\ntitle: '{}'\n---\n{}", title.replace('\'', "''"), body), + ) + .unwrap(); + path.to_str().unwrap().to_owned() + } + + fn highlighted(text: &str, matches: &[SearchMatch]) -> Vec { + let utf16: Vec<_> = text.encode_utf16().collect(); + let mut previous_end = 0; + matches + .iter() + .map(|range| { + assert!(range.start >= previous_end); + assert!(range.start < range.end); + previous_end = range.end; + String::from_utf16(&utf16[range.start..range.end]).unwrap() + }) + .collect() + } + + #[test] + fn exact_and_title_matches_outrank_prefix_and_typo_body_matches() { + let vault = TestVault::new(); + let title = write_search_note(&vault, "title.md", "Watermelon", "summer harvest"); + let body = write_search_note(&vault, "body.md", "Kitchen", "watermelon"); + let fuzzy_title = + write_search_note(&vault, "fuzzy-title.md", "Watermelom", "summer harvest"); + let fuzzy_body = write_search_note(&vault, "fuzzy-body.md", "Workshop", "watermelom"); + let prefix = write_search_note(&vault, "prefix.md", "Garden", "watermelons"); + let index = vault.index(); + index.rebuild(vault.0.to_str().unwrap()).unwrap(); + let page = index.search("watermelon", 20, 0).unwrap(); + assert_eq!(page.total, 5); + let rank = |path: &str| { + page.results + .iter() + .position(|result| result.path == path) + .unwrap() + }; + assert!(rank(&title) < rank(&body)); + assert!(rank(&body) < rank(&fuzzy_title)); + assert!(rank(&body) < rank(&fuzzy_body)); + assert!(rank(&body) < rank(&prefix)); + assert!(rank(&fuzzy_title) < rank(&fuzzy_body)); + assert_eq!( + highlighted(&page.results[0].title, &page.results[0].title_matches), + vec!["Watermelon"] + ); + assert!(page + .results + .windows(2) + .all(|pair| pair[0].score >= pair[1].score)); + } + + #[test] + fn literal_terms_keep_prefix_typo_cjk_and_cross_field_and_matching() { + let vault = TestVault::new(); + let path = write_search_note( + &vault, + "archive.md", + "Archive", + "strawberry 東京大学 カタカナ 한국어 café", + ); + let index = vault.index(); + index.rebuild(vault.0.to_str().unwrap()).unwrap(); + for query in [ + "str", + "strawbery", + "strawberrry", + "strawberxy", + "strawberyr", + "cafe", + "東京", + "京大", + "タカ", + "한국", + "archive strawberyr 東京", + ] { + let page = index.search(query, 20, 0).unwrap(); + assert_eq!(page.total, 1, "{query}"); + assert_eq!(page.results[0].path, path, "{query}"); + assert!( + !highlighted(&page.results[0].snippet, &page.results[0].snippet_matches).is_empty(), + "{query}" + ); + } + for query in [ + "", + " ", + "!!! * & ()", + ",。!?", + "東京 missing", + "strawbrreyy", + ] { + let page = index.search(query, 20, 0).unwrap(); + assert_eq!(page.total, 0, "{query}"); + assert!(page.results.is_empty(), "{query}"); + } + } + + #[test] + fn short_prefix_pages_have_exact_totals_and_stable_ties_without_expansion_loss() { + let vault = TestVault::new(); + for i in 0..65 { + write_search_note( + &vault, + &format!("note-{i:03}.md"), + "Archive", + &format!("common{i:03}"), + ); + } + let index = vault.index(); + index.rebuild(vault.0.to_str().unwrap()).unwrap(); + let all = index.search("com", usize::MAX, 0).unwrap(); + assert_eq!(all.total, 65); + let expected: Vec<_> = all + .results + .iter() + .map(|result| result.path.clone()) + .collect(); + assert_eq!(expected.len(), 65); + let mut paged = Vec::new(); + for offset in [0, 20, 40, 60] { + let page = index.search("com", 20, offset).unwrap(); + assert_eq!(page.total, 65); + assert_eq!(page.results.len(), (65 - offset).min(20)); + paged.extend(page.results.into_iter().map(|result| result.path)); + } + assert_eq!(paged, expected); + assert_eq!(paged.iter().collect::>().len(), 65); + assert_eq!( + index + .search("com", 20, 20) + .unwrap() + .results + .into_iter() + .map(|result| result.path) + .collect::>(), + expected[20..40] + ); + let beyond = index.search("com", usize::MAX, usize::MAX).unwrap(); + assert_eq!(beyond.total, 65); + assert!(beyond.results.is_empty()); + let minimum = index.search("com", 0, 0).unwrap(); + assert_eq!(minimum.total, 65); + assert_eq!(minimum.results[0].path, expected[0]); + assert_eq!(minimum.results.len(), 1); + } + + #[test] + fn snippets_find_late_matches_and_return_literal_unicode_utf16_ranges() { + let vault = TestVault::new(); + let body = format!( + "{}🦊 Café, strawberry 東京大学 {}", + "quiet preamble. ".repeat(100), + "quiet ending. ".repeat(100), + ); + let path = write_search_note(&vault, "late.md", "🦊 Café 東京", &body); + let index = vault.index(); + index.rebuild(vault.0.to_str().unwrap()).unwrap(); + let result = index + .search("cafe strawberyr 東京", 20, 0) + .unwrap() + .results + .remove(0); + assert_eq!(result.path, path); + assert!(result.snippet.starts_with('…')); + assert!(result.snippet.ends_with('…')); + assert!(result.snippet.chars().count() <= SNIPPET_CHARS + 2); + assert!(result.snippet.contains("")); + assert_eq!( + highlighted(&result.snippet, &result.snippet_matches), + vec!["Café", "strawberry", "東京"] + ); + assert_eq!( + highlighted(&result.title, &result.title_matches), + vec!["Café", "東京"] + ); + assert_eq!(result.title_matches[0], SearchMatch { start: 3, end: 7 }); + // Prefixes highlight the actual matched indexed token, not an invented + // exact substring; overlapping CJK uni/bigrams become one range. + let prefix = index.search("straw", 20, 0).unwrap().results.remove(0); + assert_eq!( + highlighted(&prefix.snippet, &prefix.snippet_matches), + vec!["strawberry"] + ); + let cjk = index.search("東京大学", 20, 0).unwrap().results.remove(0); + assert_eq!( + highlighted(&cjk.snippet, &cjk.snippet_matches), + vec!["東京大学"] + ); + } + + #[test] + fn snippets_cover_case_expansion_clipped_tokens_and_title_or_tag_only_hits() { + let vault = TestVault::new(); + write_search_note(&vault, "unicode.md", "🦊 İSTANBUL", "🦊 İSTANBUL"); + let long_word = format!("prefix{}", "x".repeat(1000)); + write_search_note(&vault, "long.md", "Long token", &long_word); + let tagged = vault.0.join("tagged.md"); + fs::write( + &tagged, + "---\ntitle: Heading\ntags: [watermelon]\n---\nPlain body without the requested token.", + ) + .unwrap(); + let index = vault.index(); + index.rebuild(vault.0.to_str().unwrap()).unwrap(); + let unicode = index.search("İSTANBUL", 20, 0).unwrap().results.remove(0); + assert_eq!( + highlighted(&unicode.snippet, &unicode.snippet_matches), + vec!["İSTANBUL"] + ); + assert_eq!( + unicode.title_matches, + vec![SearchMatch { start: 3, end: 11 }] + ); + let long = index.search("prefix", 20, 0).unwrap().results.remove(0); + assert_eq!(long.snippet.chars().count(), SNIPPET_CHARS + 1); + assert_eq!( + highlighted(&long.snippet, &long.snippet_matches), + vec![long_word[..SNIPPET_CHARS].to_owned()] + ); + for query in ["heading", "watermelon"] { + let result = index.search(query, 20, 0).unwrap().results.remove(0); + assert_eq!(result.path, tagged.to_str().unwrap()); + assert_eq!(result.snippet, "Plain body without the requested token."); + assert!(result.snippet_matches.is_empty()); + } + } + + #[test] + fn stored_snippets_survive_reopen_and_refresh_without_reading_result_files() { + let vault = TestVault::new(); + let path = write_search_note(&vault, "cached.md", "Archive", "🦊 pineapple context"); + let index = vault.index(); + index.rebuild(vault.0.to_str().unwrap()).unwrap(); + #[cfg(desktop)] + let index = { + drop(index); + vault.index() + }; + // The cached hit and context remain usable even if the source has become + // temporarily unreadable. Refreshing later must replace both together. + fs::write(&path, [0xff]).unwrap(); + let cached = index.search("pineap", 20, 0).unwrap().results.remove(0); + assert_eq!(cached.snippet, "🦊 pineapple context"); + assert_eq!( + highlighted(&cached.snippet, &cached.snippet_matches), + vec!["pineapple"] + ); + write_search_note( + &vault, + "cached.md", + "Archive", + "🦊 watermelon changed context", + ); + index.refresh_vault(vault.0.to_str().unwrap()).unwrap(); + assert_eq!(index.search("pineap", 20, 0).unwrap().total, 0); + let refreshed = index.search("watermelno", 20, 0).unwrap().results.remove(0); + assert_eq!(refreshed.snippet, "🦊 watermelon changed context"); + assert_eq!( + highlighted(&refreshed.snippet, &refreshed.snippet_matches), + vec!["watermelon"] + ); + index.refresh_vault(vault.0.to_str().unwrap()).unwrap(); + assert_eq!(index.search("watermelno", 20, 0).unwrap().total, 1); + } + + #[cfg(desktop)] + #[test] + fn reopening_waits_for_writer_shutdown_after_the_last_arc_disappears() { + let vault = TestVault::new(); + let vault_path = vault.0.to_str().unwrap().to_owned(); + let path = vault.0.join("note.md").to_str().unwrap().to_owned(); + fs::write(&path, "watermelon").unwrap(); + let state = Arc::new(crate::state::AppState::new( + crate::types::AppConfig::default(), + )); + let index = state.search_index_for_vault(&vault_path).unwrap(); + index.rebuild(&vault_path).unwrap(); + let weak = Arc::downgrade(&index); + let shutdown = index.shutdown_signal(); + + // Hold Drop before it releases the writer, while allowing the final Arc + // count to reach zero. This is the window a Weak-only cache cannot cover. + let paused = shutdown.finished.lock(); + let dropping = std::thread::spawn(move || drop(index)); + let deadline = std::time::Instant::now() + Duration::from_secs(5); + while weak.strong_count() != 0 { + assert!( + std::time::Instant::now() < deadline, + "last Arc was not dropped" + ); + std::thread::yield_now(); + } + assert!(SearchIndex::new(&vault_path).is_err()); + assert!(!shutdown.is_finished()); + + let reopening = std::thread::spawn(move || state.search_index_for_vault(&vault_path)); + let deadline = std::time::Instant::now() + Duration::from_secs(5); + while !shutdown + .wait_started + .load(std::sync::atomic::Ordering::Acquire) + { + assert!( + std::time::Instant::now() < deadline, + "reopen did not wait for writer shutdown" + ); + std::thread::yield_now(); + } + drop(paused); + dropping.join().unwrap(); + let reopened = reopening.join().unwrap().unwrap(); + assert!(shutdown.is_finished()); + assert_eq!(result_paths(&reopened, "watermelon"), vec![path]); + } + + #[test] + fn coalesced_batch_reads_latest_note_contents() { + let vault = TestVault::new(); + let index = vault.index(); + let path = vault.0.join("note.md").to_str().unwrap().to_owned(); + + // Reserve the worker so the batch boundary is controlled by the test. + // Draining is synchronous: no assertion depends on a timing deadline. + index.queue.lock().running = true; + for content in ["pineapple", "strawberry", "watermelon"] { + fs::write(&path, content).unwrap(); + index.queue_index_note(&path).unwrap(); + } + assert_eq!(index.queue.lock().pending.len(), 1); + index.drain_index_queue(); + + assert_eq!(result_paths(&index, "watermelon"), vec![path]); + assert!(result_paths(&index, "pineapple").is_empty()); + assert!(result_paths(&index, "strawberry").is_empty()); + assert!(!index.queue.lock().running); + } + + #[test] + fn queued_moves_and_deletions_do_not_resurrect_old_paths() { + let vault = TestVault::new(); + let index = vault.index(); + let old_path = vault.0.join("old.md").to_str().unwrap().to_owned(); + let new_path = vault.0.join("new.md").to_str().unwrap().to_owned(); + let deleted_path = vault.0.join("deleted.md").to_str().unwrap().to_owned(); + for path in [&old_path, &deleted_path] { + fs::write(path, "watermelon").unwrap(); + } + index.rebuild(vault.0.to_str().unwrap()).unwrap(); + + index.queue.lock().running = true; + index.queue_index_note(&old_path).unwrap(); + index.queue_index_note(&deleted_path).unwrap(); + fs::rename(&old_path, &new_path).unwrap(); + fs::remove_file(&deleted_path).unwrap(); + index.remove_note(&old_path).unwrap(); + index.queue_index_note(&new_path).unwrap(); + index.drain_index_queue(); + + assert_eq!(result_paths(&index, "watermelon"), vec![new_path]); + } + + #[test] + fn unreadable_note_does_not_block_other_notes_or_later_edits() { + let vault = TestVault::new(); + let index = vault.index(); + let bad_path = vault.0.join("bad.md").to_str().unwrap().to_owned(); + let good_path = vault.0.join("good.md").to_str().unwrap().to_owned(); + fs::write(&bad_path, [0xff]).unwrap(); + fs::write(&good_path, "watermelon").unwrap(); + + index.queue.lock().running = true; + index.queue_index_note(&bad_path).unwrap(); + index.queue_index_note(&good_path).unwrap(); + index.drain_index_queue(); + assert_eq!(result_paths(&index, "watermelon"), vec![good_path.clone()]); + assert!(!index.queue.lock().running); + + fs::write(&bad_path, "pineapple").unwrap(); + fs::write(&good_path, "strawberry").unwrap(); + index.queue.lock().running = true; + index.queue_index_note(&bad_path).unwrap(); + index.queue_index_note(&good_path).unwrap(); + index.drain_index_queue(); + assert_eq!(result_paths(&index, "pineapple"), vec![bad_path]); + assert_eq!(result_paths(&index, "strawberry"), vec![good_path]); + assert!(result_paths(&index, "watermelon").is_empty()); + } + + #[test] + fn failed_batch_releases_worker_for_subsequent_updates() { + let vault = TestVault::new(); + let index = vault.index(); + let path = vault.0.join("note.md").to_str().unwrap().to_owned(); + fs::write(&path, "watermelon").unwrap(); + let writer = index.writer.lock().take(); + + index.queue.lock().running = true; + index.queue_index_note(&path).unwrap(); + index.drain_index_queue(); + assert!(!index.queue.lock().running); + assert!(result_paths(&index, "watermelon").is_empty()); + + *index.writer.lock() = writer; + index.queue.lock().running = true; + index.queue_index_note(&path).unwrap(); + index.drain_index_queue(); + assert_eq!(result_paths(&index, "watermelon"), vec![path]); + assert!(!index.queue.lock().running); + } + + #[test] + fn unchanged_refresh_preserves_commit_and_cached_results() { + let vault = TestVault::new(); + let path = vault.0.join("note.md").to_str().unwrap().to_owned(); + fs::write(&path, "watermelon").unwrap(); + let index = vault.index(); + index.rebuild(vault.0.to_str().unwrap()).unwrap(); + let committed = index.index.load_metas().unwrap().opstamp; + #[cfg(desktop)] + let index = { + drop(index); + vault.index() + }; + assert_eq!(result_paths(&index, "watermelon"), vec![path.clone()]); + + index.queue.lock().running = true; + index + .queue_vault_refresh(vault.0.to_str().unwrap()) + .unwrap(); + index + .queue_vault_refresh(vault.0.to_str().unwrap()) + .unwrap(); + // Cached search is available before reconciliation is allowed to run. + assert_eq!(result_paths(&index, "watermelon"), vec![path.clone()]); + index.drain_index_queue(); + assert_eq!(index.index.load_metas().unwrap().opstamp, committed); + assert_eq!(result_paths(&index, "watermelon"), vec![path]); + } + + fn set_modified(path: &str, nanos: u32) { + let modified = UNIX_EPOCH + Duration::new(1_700_000_000, nanos); + fs::File::options() + .write(true) + .open(path) + .unwrap() + .set_times(fs::FileTimes::new().set_modified(modified)) + .unwrap(); + } + + #[test] + fn refresh_detects_subsecond_edits_size_changes_new_notes_and_deletions() { + let vault = TestVault::new(); + let index = vault.index(); + let edited = vault.0.join("edited.md").to_str().unwrap().to_owned(); + let resized = vault.0.join("resized.md").to_str().unwrap().to_owned(); + let deleted = vault.0.join("deleted.md").to_str().unwrap().to_owned(); + let added = vault.0.join("added.md").to_str().unwrap().to_owned(); + for path in [&edited, &resized, &deleted] { + fs::write(path, "pineapple").unwrap(); + set_modified(path, 100_000_000); + } + index.rebuild(vault.0.to_str().unwrap()).unwrap(); + + // Same byte length and second; only subsecond precision detects this edit. + fs::write(&edited, "blueberry").unwrap(); + set_modified(&edited, 900_000_000); + fs::write(&resized, "strawberry").unwrap(); + set_modified(&resized, 100_000_000); + fs::remove_file(&deleted).unwrap(); + fs::write(&added, "watermelon").unwrap(); + index.refresh_vault(vault.0.to_str().unwrap()).unwrap(); + + assert_eq!(result_paths(&index, "blueberry"), vec![edited]); + assert_eq!(result_paths(&index, "strawberry"), vec![resized]); + assert_eq!(result_paths(&index, "watermelon"), vec![added]); + assert!(result_paths(&index, "pineapple").is_empty()); + let committed = index.index.load_metas().unwrap().opstamp; + index.refresh_vault(vault.0.to_str().unwrap()).unwrap(); + assert_eq!(index.index.load_metas().unwrap().opstamp, committed); + } + + #[test] + fn refresh_and_queued_edits_use_latest_disk_state() { + let vault = TestVault::new(); + let index = vault.index(); + let edited = vault.0.join("edited.md").to_str().unwrap().to_owned(); + let deleted = vault.0.join("deleted.md").to_str().unwrap().to_owned(); + for path in [&edited, &deleted] { + fs::write(path, "pineapple").unwrap(); + } + index.rebuild(vault.0.to_str().unwrap()).unwrap(); + index.queue.lock().running = true; + index + .queue_vault_refresh(vault.0.to_str().unwrap()) + .unwrap(); + index.queue_index_note(&edited).unwrap(); + index.queue_index_note(&deleted).unwrap(); + fs::write(&edited, "watermelon").unwrap(); + fs::remove_file(&deleted).unwrap(); + index.remove_note(&deleted).unwrap(); + index.drain_index_queue(); + + assert_eq!(result_paths(&index, "watermelon"), vec![edited]); + assert!(result_paths(&index, "pineapple").is_empty()); + assert!(!index.queue.lock().running); + } + + #[test] + fn failed_refresh_preserves_cached_notes_and_later_queue_work() { + let vault = TestVault::new(); + let index = vault.index(); + let path = vault.0.join("note.md").to_str().unwrap().to_owned(); + fs::write(&path, "pineapple").unwrap(); + index.rebuild(vault.0.to_str().unwrap()).unwrap(); + fs::write(&path, [0xff]).unwrap(); + let committed = index.index.load_metas().unwrap().opstamp; + index.refresh_vault(vault.0.to_str().unwrap()).unwrap(); + assert_eq!(index.index.load_metas().unwrap().opstamp, committed); + assert_eq!(result_paths(&index, "pineapple"), vec![path.clone()]); + + index.queue.lock().running = true; + index + .queue_vault_refresh(vault.0.join("missing").to_str().unwrap()) + .unwrap(); + fs::write(&path, "watermelon").unwrap(); + index.queue_index_note(&path).unwrap(); + index.drain_index_queue(); + assert_eq!(result_paths(&index, "watermelon"), vec![path]); + assert!(result_paths(&index, "pineapple").is_empty()); + } + + #[test] + fn first_cold_query_waits_for_initial_reconciliation() { + let vault = TestVault::new(); + let index = vault.index(); + let path = vault.0.join("note.md").to_str().unwrap().to_owned(); + fs::write(&path, "watermelon").unwrap(); + index.queue.lock().running = true; + index + .queue_vault_refresh(vault.0.to_str().unwrap()) + .unwrap(); + let worker = Arc::clone(&index); + let handle = std::thread::spawn(move || worker.drain_index_queue()); + assert_eq!(result_paths(&index, "watermelon"), vec![path]); + handle.join().unwrap(); + } + + #[test] + fn failed_initial_refresh_releases_query_and_allows_later_work() { + let vault = TestVault::new(); + let index = vault.index(); + let writer = index.writer.lock().take(); + index.queue.lock().running = true; + index + .queue_vault_refresh(vault.0.to_str().unwrap()) + .unwrap(); + let worker = Arc::clone(&index); + let handle = std::thread::spawn(move || worker.drain_index_queue()); + assert!(result_paths(&index, "watermelon").is_empty()); + handle.join().unwrap(); + *index.writer.lock() = writer; + let path = vault.0.join("note.md").to_str().unwrap().to_owned(); + fs::write(&path, "watermelon").unwrap(); + index.queue.lock().running = true; + index.queue_index_note(&path).unwrap(); + index.drain_index_queue(); + assert_eq!(result_paths(&index, "watermelon"), vec![path]); } } diff --git a/src-tauri/src/state.rs b/src-tauri/src/state.rs index 2ebbe4e..baf3f0a 100644 --- a/src-tauri/src/state.rs +++ b/src-tauri/src/state.rs @@ -1,30 +1,160 @@ -use crate::search::SearchIndex; +use crate::search::{SearchIndex, SearchIndexShutdown}; use crate::types::AppConfig; use crate::vault::watcher::VaultWatcher; +use std::collections::HashMap; use std::sync::atomic::AtomicBool; -use std::sync::Arc; -use std::sync::Mutex; +use std::sync::{Arc, Mutex, Weak}; + +struct CachedSearchIndex { + index: Weak, + shutdown: Arc, +} pub struct AppState { pub config: Mutex, pub search_index: Mutex>>, + search_indexes: Mutex>, pub watcher: Mutex>, pub vault_transition: tokio::sync::Mutex<()>, + pub note_save: Arc>, pub importing: AtomicBool, pub syncing: AtomicBool, + #[cfg(desktop)] + pub quitting: AtomicBool, pub pending_open_file: Mutex>, } impl AppState { + // A refresh worker may outlive a vault switch. Reuse its writer when opening + // that vault again instead of competing for Tantivy's exclusive writer lock. + pub fn search_index_for_vault(&self, vault_path: &str) -> Result, String> { + let mut indexes = self + .search_indexes + .lock() + .map_err(|error| error.to_string())?; + indexes + .retain(|_, cached| cached.index.strong_count() > 0 || !cached.shutdown.is_finished()); + if let Some(cached) = indexes.get(vault_path) { + if let Some(index) = cached.index.upgrade() { + return Ok(index); + } + // The last Arc is gone, but its writer may still be shutting down. + cached.shutdown.wait(); + } + let index = Arc::new(SearchIndex::new(vault_path)?); + indexes.insert( + vault_path.to_owned(), + CachedSearchIndex { + index: Arc::downgrade(&index), + shutdown: index.shutdown_signal(), + }, + ); + Ok(index) + } + + pub fn validate_vault(&self, vault_path: &str) -> Result<(), String> { + let config = self.config.lock().map_err(|error| error.to_string())?; + if config.active_vault.as_deref() == Some(vault_path) + || config.vaults.iter().any(|vault| vault.path == vault_path) + { + Ok(()) + } else { + Err("Unknown vault".to_string()) + } + } + + pub fn vault_snapshot(&self, vault_path: &str) -> Result>, String> { + let search = self + .search_index + .lock() + .map_err(|error| error.to_string())?; + let config = self.config.lock().map_err(|error| error.to_string())?; + if config.active_vault.as_deref() == Some(vault_path) { + return Ok(search.clone()); + } + if !config.vaults.iter().any(|vault| vault.path == vault_path) { + return Err("Unknown vault".to_string()); + } + drop(config); + drop(search); + // Detached windows retain access to configured vaults. Never pair their + // writes with the main window's index, nor hold config during disk I/O. + self.search_index_for_vault(vault_path).map(Some) + } + + pub fn active_vault_snapshot(&self) -> Result<(String, Option>), String> { + // Match vault transition and watcher lock order. The vault and its + // optional index must remain paired even if a switch follows the read. + let search = self + .search_index + .lock() + .map_err(|error| error.to_string())?; + let config = self.config.lock().map_err(|error| error.to_string())?; + Ok(( + config.active_vault.clone().ok_or("No active vault")?, + search.clone(), + )) + } + + pub fn active_search(&self) -> Result<(Arc, String), String> { + let (vault_path, search) = self.active_vault_snapshot()?; + Ok((search.ok_or("Search index not initialized")?, vault_path)) + } + + #[cfg(desktop)] + pub fn should_hide_to_tray(&self, label: &str, tray_available: bool) -> Result { + if label != "main" + || !tray_available + || self.quitting.load(std::sync::atomic::Ordering::Relaxed) + { + return Ok(false); + } + let config = self.config.lock().map_err(|error| error.to_string())?; + Ok(config.show_tray_icon && config.close_to_tray) + } + pub fn new(config: AppConfig) -> Self { Self { config: Mutex::new(config), search_index: Mutex::new(None), + search_indexes: Mutex::new(HashMap::new()), watcher: Mutex::new(None), vault_transition: tokio::sync::Mutex::new(()), + note_save: Arc::new(tokio::sync::Mutex::new(())), importing: AtomicBool::new(false), syncing: AtomicBool::new(false), + #[cfg(desktop)] + quitting: AtomicBool::new(false), pending_open_file: Mutex::new(None), } } } + +#[cfg(all(test, desktop))] +mod tests { + use super::*; + + #[test] + fn close_policy_respects_live_settings_window_type_and_explicit_quit() { + let mut state = AppState::new(AppConfig { + show_tray_icon: true, + close_to_tray: true, + ..AppConfig::default() + }); + assert!(state.should_hide_to_tray("main", true).unwrap()); + assert!(!state.should_hide_to_tray("note-123", true).unwrap()); + assert!(!state.should_hide_to_tray("main", false).unwrap()); + state + .quitting + .store(true, std::sync::atomic::Ordering::Relaxed); + assert!(!state.should_hide_to_tray("main", true).unwrap()); + state + .quitting + .store(false, std::sync::atomic::Ordering::Relaxed); + state.config.get_mut().unwrap().close_to_tray = false; + assert!(!state.should_hide_to_tray("main", true).unwrap()); + state.config.get_mut().unwrap().close_to_tray = true; + state.config.get_mut().unwrap().show_tray_icon = false; + assert!(!state.should_hide_to_tray("main", true).unwrap()); + } +} diff --git a/src-tauri/src/sync.rs b/src-tauri/src/sync.rs index 0dac192..9fd9e35 100644 --- a/src-tauri/src/sync.rs +++ b/src-tauri/src/sync.rs @@ -12,10 +12,12 @@ use crate::state::AppState; use crate::vault::operations::helixnotes_dir; +use crate::vault::watcher::changed_entry; use serde::{Deserialize, Serialize}; use sha2::{Digest, Sha256}; use std::collections::{HashMap, HashSet}; use std::fs; +use std::io::{self, Read}; use std::path::{Path, PathBuf}; use tauri::{Emitter, Manager}; use walkdir::WalkDir; @@ -69,15 +71,25 @@ fn save_manifest(vault: &str, m: &SyncManifest) { // ───────────────────────── helpers ───────────────────────── +const HASH_BUFFER_SIZE: usize = 64 * 1024; + fn sha256_hex(bytes: &[u8]) -> String { - let mut h = Sha256::new(); - h.update(bytes); - let digest = h.finalize(); - let mut s = String::with_capacity(64); - for b in digest { - s.push_str(&format!("{:02x}", b)); + format!("{:x}", Sha256::digest(bytes)) +} + +fn sha256_reader( + reader: &mut impl Read, + buffer: &mut [u8; HASH_BUFFER_SIZE], +) -> io::Result { + let mut hasher = Sha256::new(); + loop { + match reader.read(buffer) { + Ok(0) => return Ok(format!("{:x}", hasher.finalize())), + Ok(count) => hasher.update(&buffer[..count]), + Err(error) if error.kind() == io::ErrorKind::Interrupted => continue, + Err(error) => return Err(error), + } } - s } fn normalize_etag(s: &str) -> String { @@ -109,12 +121,63 @@ fn conflict_relpath(relpath: &str, stamp: &str) -> String { format!("{}{} (conflict {}){}", dir, stem, stamp, ext) } -fn write_local(vault: &Path, relpath: &str, bytes: &[u8]) -> Result<(), String> { +fn write_local( + vault: &Path, + relpath: &str, + bytes: &[u8], + changes: &mut HashMap, +) -> Result<(), String> { let target = vault.join(relpath); + if fs::read(&target).is_ok_and(|current| current == bytes) { + return Ok(()); + } if let Some(parent) = target.parent() { fs::create_dir_all(parent).map_err(|e| e.to_string())?; } - fs::write(&target, bytes).map_err(|e| e.to_string()) + if matches!( + relpath, + ".helixnotes/notebook_icons.json" | ".helixnotes/tag_styles.json" + ) { + let content = std::str::from_utf8(bytes).map_err(|error| error.to_string())?; + crate::vault::operations::attachment_metadata_references( + target.file_name().and_then(|name| name.to_str()).unwrap(), + content, + )?; + } + crate::vault::operations::replace_file_atomically(&target, bytes)?; + changes.insert(target, "modify"); + Ok(()) +} + +fn require_unchanged_local(path: &Path, expected_hash: Option<&str>) -> Result<(), String> { + let actual = match fs::File::open(path) { + Ok(mut file) => Some( + sha256_reader(&mut file, &mut [0; HASH_BUFFER_SIZE]) + .map_err(|error| error.to_string())?, + ), + Err(error) if error.kind() == io::ErrorKind::NotFound => None, + Err(error) => return Err(error.to_string()), + }; + if actual.as_deref() != expected_hash { + return Err(format!( + "Sync stopped because {} changed locally during sync", + path.display() + )); + } + Ok(()) +} + +fn write_local_if_unchanged( + write_guard: &tokio::sync::Mutex<()>, + vault: &Path, + relpath: &str, + bytes: &[u8], + expected_hash: Option<&str>, + changes: &mut HashMap, +) -> Result<(), String> { + let _guard = write_guard.blocking_lock(); + require_unchanged_local(&vault.join(relpath), expected_hash)?; + write_local(vault, relpath, bytes, changes) } struct LocalFile { @@ -138,6 +201,7 @@ fn is_synced_relpath(rel: &str) -> bool { fn scan_local(vault: &str) -> Result, String> { let vault_path = Path::new(vault); let mut map = HashMap::new(); + let mut buffer = [0; HASH_BUFFER_SIZE]; for entry in WalkDir::new(vault_path).into_iter().filter_map(|e| e.ok()) { let p = entry.path(); if !p.is_file() { @@ -150,14 +214,15 @@ fn scan_local(vault: &str) -> Result, String> { if !is_synced_relpath(&rel) { continue; } - let bytes = match fs::read(p) { - Ok(b) => b, - Err(_) => continue, - }; + let hash = + match fs::File::open(p).and_then(|mut file| sha256_reader(&mut file, &mut buffer)) { + Ok(hash) => hash, + Err(_) => continue, + }; map.insert( rel, LocalFile { - hash: sha256_hex(&bytes), + hash, path: p.to_path_buf(), }, ); @@ -511,6 +576,8 @@ fn apply_changes( local: &HashMap, remote: &HashMap, manifest: &SyncManifest, + changes: &mut HashMap, + write_guard: &tokio::sync::Mutex<()>, ) -> Result<(SyncManifest, SyncSummary), String> { let vault_path = Path::new(vault); let mut new_m = SyncManifest::default(); @@ -559,7 +626,14 @@ fn apply_changes( sum.uploaded += 1; } else if !local_changed && remote_changed { let bytes = client.get(key)?; - write_local(vault_path, key, &bytes)?; + write_local_if_unchanged( + write_guard, + vault_path, + key, + &bytes, + Some(&lf.hash), + changes, + )?; new_m.files.insert( key.clone(), ManifestEntry { @@ -582,8 +656,17 @@ fn apply_changes( ); } else { // keep-both: remote becomes canonical; local saved as a conflict copy. - let lbytes = fs::read(&lf.path).map_err(|e| e.to_string())?; - write_local(vault_path, key, &rbytes)?; + let ckey = conflict_relpath(key, &stamp); + let lbytes = { + let _guard = write_guard.blocking_lock(); + require_unchanged_local(&lf.path, Some(&lf.hash))?; + require_unchanged_local(&vault_path.join(&ckey), None)?; + let lbytes = fs::read(&lf.path).map_err(|e| e.to_string())?; + // Preserve the local bytes before replacing their canonical path. + write_local(vault_path, &ckey, &lbytes, changes)?; + write_local(vault_path, key, &rbytes, changes)?; + lbytes + }; new_m.files.insert( key.clone(), ManifestEntry { @@ -591,8 +674,6 @@ fn apply_changes( remote_etag: re.clone(), }, ); - let ckey = conflict_relpath(key, &stamp); - write_local(vault_path, &ckey, &lbytes)?; let cetag = client.upload(&ckey, &lbytes, &mut created)?; new_m.files.insert( ckey, @@ -634,7 +715,10 @@ fn apply_changes( sum.uploaded += 1; } else { // remote deleted, local unchanged -> delete local - let _ = fs::remove_file(&lf.path); + let _guard = write_guard.blocking_lock(); + require_unchanged_local(&lf.path, Some(&lf.hash))?; + fs::remove_file(&lf.path).map_err(|e| e.to_string())?; + changes.insert(lf.path.clone(), "remove"); sum.deleted_local += 1; } } @@ -643,7 +727,7 @@ fn apply_changes( None => { // new remote -> download let bytes = client.get(key)?; - write_local(vault_path, key, &bytes)?; + write_local_if_unchanged(write_guard, vault_path, key, &bytes, None, changes)?; new_m.files.insert( key.clone(), ManifestEntry { @@ -657,7 +741,14 @@ fn apply_changes( if &me.remote_etag != re { // local deleted but remote changed -> resurrect (download) let bytes = client.get(key)?; - write_local(vault_path, key, &bytes)?; + write_local_if_unchanged( + write_guard, + vault_path, + key, + &bytes, + None, + changes, + )?; new_m.files.insert( key.clone(), ManifestEntry { @@ -689,7 +780,7 @@ pub fn test_connection(cfg: WebdavConfig) -> Result { } /// Run a full sync. Mutes the file watcher while applying local writes, then -/// rebuilds the search index. Returns a summary of what changed. +/// queues only changed notes for search and publishes their final local state. pub fn run_sync( app: tauri::AppHandle, vault: String, @@ -698,6 +789,9 @@ pub fn run_sync( use std::sync::atomic::Ordering; let state = app.state::(); + // Keep the original vault's index even if the user switches vaults while + // WebDAV is working. No config lock is held during network or file I/O. + let search = state.vault_snapshot(&vault)?; let client = WebdavClient::new(cfg)?; let _ = app.emit("sync-progress", serde_json::json!({ "status": "listing" })); @@ -706,8 +800,10 @@ pub fn run_sync( .into_iter() .filter(|(k, _)| is_synced_relpath(k)) .collect(); - let local = scan_local(&vault)?; - let manifest = load_manifest(&vault); + let (local, manifest) = { + let _guard = state.note_save.blocking_lock(); + (scan_local(&vault)?, load_manifest(&vault)) + }; // Safety guard (issue #102): never treat an empty remote listing as "everything was // deleted remotely". If a prior sync exists (non-empty manifest) but the remote came back @@ -724,35 +820,241 @@ pub fn run_sync( } let _ = app.emit("sync-progress", serde_json::json!({ "status": "syncing" })); - // Mute the watcher across local writes so a pull doesn't storm the reindex path. - state.importing.store(true, Ordering::Relaxed); - let applied = apply_changes(&client, &vault, &local, &remote, &manifest); - state.importing.store(false, Ordering::Relaxed); + let mut changes = HashMap::new(); + // Mute the watcher across local writes; its deferred batch also preserves + // external edits that arrive while the network operation is in progress. + { + let _guard = state.note_save.blocking_lock(); + state.importing.store(true, Ordering::Release); + } + let applied = apply_changes( + &client, + &vault, + &local, + &remote, + &manifest, + &mut changes, + &state.note_save, + ); + state.importing.store(false, Ordering::Release); + + // Publish even a partial apply: a later network failure must not leave + // successfully downloaded/deleted notes absent from search or the UI. + let active = state + .config + .lock() + .ok() + .is_some_and(|config| config.active_vault.as_deref() == Some(vault.as_str())); + let metadata_root = helixnotes_dir(&vault); + for (path, event_type) in changes { + let is_note = path.extension().and_then(|extension| extension.to_str()) == Some("md") + && !path.starts_with(&metadata_root); + if is_note { + if let Some(search) = &search { + if let Err(error) = search.queue_index_note(&path.to_string_lossy()) { + log::error!("Could not queue synced note for search: {error}"); + } + } + } + if !active { + continue; + } + let event = if is_note { + changed_entry(path, event_type, Path::new(&vault)) + } else { + crate::types::FileEvent { + vault_path: vault.clone(), + event_type: event_type.to_string(), + path: path.to_string_lossy().to_string(), + note: None, + } + }; + let _ = app.emit("file-changed", event); + } let (new_manifest, summary) = applied?; save_manifest(&vault, &new_manifest); - - // Re-index after files changed (search is non-critical; ignore failures). - if let Ok(guard) = state.search_index.lock() { - if let Some(search) = guard.as_ref() { - let _ = search.rebuild(&vault); - } - } - - // The watcher was muted during apply, so nudge the UI to refresh its lists. - let _ = app.emit( - "file-changed", - crate::types::FileEvent { - event_type: "modify".to_string(), - path: vault.clone(), - }, - ); Ok(summary) } #[cfg(test)] mod tests { - use super::is_synced_relpath; + use super::*; + + #[test] + fn synced_metadata_waits_for_local_patches_and_never_overwrites_a_newer_style() { + use crate::types::TagStyleChange; + use crate::vault::operations; + let vault = tempfile::tempdir().unwrap(); + let root = vault.path().to_str().unwrap(); + operations::ensure_vault_structure(root).unwrap(); + operations::patch_tag_style(root, "Work", TagStyleChange::Color(Some("#123456".into()))) + .unwrap(); + let rel = ".helixnotes/tag_styles.json"; + let path = vault.path().join(rel); + let hash = sha256_hex(&fs::read(&path).unwrap()); + let guard = tokio::sync::Mutex::new(()); + let held = guard.blocking_lock(); + std::thread::scope(|scope| { + let pending = scope.spawn(|| { + let mut changes = HashMap::new(); + let result = write_local_if_unchanged( + &guard, + vault.path(), + rel, + br##"{"Work":{"color":"#ffffff"}}"##, + Some(&hash), + &mut changes, + ); + assert!(result.is_err()); + assert!(changes.is_empty()); + }); + operations::patch_tag_style( + root, + "Work", + TagStyleChange::Icon(Some("builtin:star".into())), + ) + .unwrap(); + drop(held); + pending.join().unwrap(); + }); + let persisted = operations::load_tag_styles(root).unwrap(); + assert_eq!(persisted["Work"].icon.as_deref(), Some("builtin:star")); + assert_eq!(persisted["Work"].color.as_deref(), Some("#123456")); + + let current = fs::read(&path).unwrap(); + let mut original = fs::File::open(&path).unwrap(); + let mut changes = HashMap::new(); + write_local_if_unchanged( + &guard, + vault.path(), + rel, + br##"{"Work":{"color":"#abcdef","icon":"builtin:star"}}"##, + Some(&sha256_hex(¤t)), + &mut changes, + ) + .unwrap(); + let mut old_bytes = Vec::new(); + original.read_to_end(&mut old_bytes).unwrap(); + assert_eq!(old_bytes, current); + assert_eq!( + operations::load_tag_styles(root).unwrap()["Work"] + .color + .as_deref(), + Some("#abcdef") + ); + + let before = fs::read(&path).unwrap(); + assert!(write_local_if_unchanged( + &guard, + vault.path(), + rel, + b"{broken", + Some(&sha256_hex(&before)), + &mut HashMap::new() + ) + .is_err()); + assert_eq!(fs::read(&path).unwrap(), before); + } + + #[test] + fn streaming_hash_matches_bytes_across_buffer_boundaries() { + let mut buffer = [0; HASH_BUFFER_SIZE]; + for len in [0, 256, HASH_BUFFER_SIZE, HASH_BUFFER_SIZE * 2 + 137] { + let bytes: Vec = (0..len).map(|i| (i % 256) as u8).collect(); + assert_eq!( + sha256_reader(&mut bytes.as_slice(), &mut buffer).unwrap(), + sha256_hex(&bytes), + "hash mismatch for {len} bytes" + ); + } + } + + #[test] + fn streaming_hash_retries_interruptions_and_accepts_short_reads() { + struct InterruptedShortReader<'a> { + remaining: &'a [u8], + interrupt_next: bool, + } + + impl Read for InterruptedShortReader<'_> { + fn read(&mut self, buffer: &mut [u8]) -> io::Result { + self.interrupt_next = !self.interrupt_next; + if self.interrupt_next { + return Err(io::ErrorKind::Interrupted.into()); + } + let len = buffer.len().min(7); + self.remaining.read(&mut buffer[..len]) + } + } + + let bytes: Vec = (0..513).map(|i| (i % 256) as u8).collect(); + let mut reader = InterruptedShortReader { + remaining: &bytes, + interrupt_next: false, + }; + let mut buffer = [0; HASH_BUFFER_SIZE]; + assert_eq!( + sha256_reader(&mut reader, &mut buffer).unwrap(), + sha256_hex(&bytes) + ); + } + + #[test] + fn streaming_hash_rejects_partial_content_after_read_error() { + struct FailingReader; + + impl Read for FailingReader { + fn read(&mut self, _: &mut [u8]) -> io::Result { + Err(io::ErrorKind::PermissionDenied.into()) + } + } + + let mut reader = b"partial content".as_slice().chain(FailingReader); + let mut buffer = [0; HASH_BUFFER_SIZE]; + assert_eq!( + sha256_reader(&mut reader, &mut buffer).unwrap_err().kind(), + io::ErrorKind::PermissionDenied + ); + } + + #[test] + fn local_scan_hashes_large_attachments_and_independent_files() { + let vault = tempfile::tempdir().unwrap(); + fs::create_dir_all(vault.path().join(".helixnotes/attachments")).unwrap(); + let attachment: Vec = (0..HASH_BUFFER_SIZE * 2 + 31) + .map(|i| (i % 256) as u8) + .collect(); + let files = [ + (".helixnotes/attachments/large.bin", attachment.as_slice()), + ( + ".helixnotes/attachments/small.bin", + b"\0\xff\x80".as_slice(), + ), + ("empty.md", b"".as_slice()), + ("note.md", b"note content".as_slice()), + ]; + for (relpath, bytes) in files { + fs::write(vault.path().join(relpath), bytes).unwrap(); + } + fs::write(vault.path().join("ignored.bin"), b"not synced").unwrap(); + + let local = scan_local(vault.path().to_str().unwrap()).unwrap(); + let actual: HashMap<_, _> = local + .into_iter() + .map(|(relpath, file)| (relpath, (file.hash, file.path))) + .collect(); + let expected: HashMap<_, _> = files + .into_iter() + .map(|(relpath, bytes)| { + ( + relpath.to_string(), + (sha256_hex(bytes), vault.path().join(relpath)), + ) + }) + .collect(); + assert_eq!(actual, expected); + } #[test] fn syncs_notebook_icon_mapping_and_assets_only() { @@ -775,4 +1077,134 @@ mod tests { assert!(!is_synced_relpath(path), "expected {path} to stay local"); } } + + #[test] + fn unchanged_sync_has_no_local_mutations() { + let vault = tempfile::tempdir().unwrap(); + let path = vault.path().join("note.md"); + fs::write(&path, "unchanged content").unwrap(); + let modified = fs::metadata(&path).unwrap().modified().unwrap(); + let vault_path = vault.path().to_str().unwrap(); + let local = scan_local(vault_path).unwrap(); + let remote = HashMap::from([("note.md".to_string(), "etag".to_string())]); + let manifest = SyncManifest { + files: HashMap::from([( + "note.md".to_string(), + ManifestEntry { + local_hash: local["note.md"].hash.clone(), + remote_etag: "etag".to_string(), + }, + )]), + }; + let client = WebdavClient::new(WebdavConfig { + url: "http://127.0.0.1:0/".to_string(), + username: String::new(), + password: String::new(), + }) + .unwrap(); + let mut changes = HashMap::new(); + let (next, summary) = apply_changes( + &client, + vault_path, + &local, + &remote, + &manifest, + &mut changes, + &tokio::sync::Mutex::new(()), + ) + .unwrap(); + assert!(changes.is_empty()); + assert_eq!(summary.uploaded, 0); + assert_eq!(summary.downloaded, 0); + assert_eq!(summary.deleted_local, 0); + assert_eq!(summary.deleted_remote, 0); + assert_eq!(summary.conflicts, 0); + assert_eq!(next.files["note.md"].local_hash, local["note.md"].hash); + assert_eq!(fs::metadata(&path).unwrap().modified().unwrap(), modified); + assert_eq!(fs::read_to_string(path).unwrap(), "unchanged content"); + } + + #[test] + fn remote_deletion_records_only_the_removed_local_note() { + let vault = tempfile::tempdir().unwrap(); + let path = vault.path().join("note.md"); + fs::write(&path, "unchanged content").unwrap(); + let vault_path = vault.path().to_str().unwrap(); + let local = scan_local(vault_path).unwrap(); + let manifest = SyncManifest { + files: HashMap::from([( + "note.md".to_string(), + ManifestEntry { + local_hash: local["note.md"].hash.clone(), + remote_etag: "etag".to_string(), + }, + )]), + }; + let client = WebdavClient::new(WebdavConfig { + url: "http://127.0.0.1:0/".to_string(), + username: String::new(), + password: String::new(), + }) + .unwrap(); + let mut changes = HashMap::new(); + let (next, summary) = apply_changes( + &client, + vault_path, + &local, + &HashMap::new(), + &manifest, + &mut changes, + &tokio::sync::Mutex::new(()), + ) + .unwrap(); + assert!(!path.exists()); + assert!(!next.files.contains_key("note.md")); + assert_eq!(summary.deleted_local, 1); + assert_eq!(changes, HashMap::from([(path, "remove")])); + } + + #[test] + fn local_download_journal_ignores_identical_bytes_and_preserves_partial_changes() { + let vault = tempfile::tempdir().unwrap(); + let path = vault.path().join("Notes/note.md"); + let mut changes = HashMap::new(); + write_local( + vault.path(), + "Notes/note.md", + b"first content", + &mut changes, + ) + .unwrap(); + assert_eq!(fs::read(&path).unwrap(), b"first content"); + changes.clear(); + let modified = fs::metadata(&path).unwrap().modified().unwrap(); + write_local( + vault.path(), + "Notes/note.md", + b"first content", + &mut changes, + ) + .unwrap(); + assert!(changes.is_empty()); + assert_eq!(fs::metadata(&path).unwrap().modified().unwrap(), modified); + + write_local( + vault.path(), + "Notes/note.md", + b"remote content", + &mut changes, + ) + .unwrap(); + fs::write(vault.path().join("blocked"), b"not a directory").unwrap(); + assert!(write_local( + vault.path(), + "blocked/note.md", + b"cannot write", + &mut changes + ) + .is_err()); + assert_eq!(changes, HashMap::from([(path.clone(), "modify")])); + let event = changed_entry(path, "modify", vault.path()); + assert_eq!(event.note.unwrap().preview, "remote content"); + } } diff --git a/src-tauri/src/types.rs b/src-tauri/src/types.rs index 497141f..fa42eab 100644 --- a/src-tauri/src/types.rs +++ b/src-tauri/src/types.rs @@ -43,6 +43,7 @@ pub struct NotebookEntry { } #[derive(Debug, Clone, Default, Serialize, Deserialize, PartialEq, Eq)] +#[serde(deny_unknown_fields)] pub struct TagStyle { #[serde(default, skip_serializing_if = "Option::is_none")] pub icon: Option, @@ -50,6 +51,14 @@ pub struct TagStyle { pub color: Option, } +#[derive(Debug, Clone, Serialize, Deserialize)] +#[serde(tag = "kind", content = "value", rename_all = "lowercase")] +pub enum TagStyleChange { + Icon(Option), + Color(Option), + Reset, +} + impl TagStyle { pub fn is_empty(&self) -> bool { self.icon @@ -298,7 +307,7 @@ fn default_max_versions() -> u32 { } fn default_ai_model() -> String { - "claude-sonnet-4-6".to_string() + "claude-sonnet-5".to_string() } fn default_system_light_theme() -> String { @@ -358,7 +367,7 @@ impl Default for AppConfig { ollama_api_key: None, openai_compatible_base_url: None, openai_compatible_api_key: None, - ai_model: "claude-sonnet-4-6".to_string(), + ai_model: default_ai_model(), ai_writing_style: None, default_view_mode: false, new_notes_in_source_mode: false, @@ -441,18 +450,35 @@ impl Default for VaultState { } } +/// Half-open UTF-16 offsets into the corresponding plain-text result field. +#[derive(Debug, Clone, Serialize, Deserialize, PartialEq, Eq)] +pub struct SearchMatch { + pub start: usize, + pub end: usize, +} + #[derive(Debug, Clone, Serialize, Deserialize)] pub struct SearchResult { pub path: String, pub title: String, pub snippet: String, pub score: f32, + pub title_matches: Vec, + pub snippet_matches: Vec, +} + +#[derive(Debug, Clone, Serialize, Deserialize, Default)] +pub struct SearchPage { + pub results: Vec, + pub total: usize, } #[derive(Debug, Clone, Serialize, Deserialize)] pub struct FileEvent { + pub vault_path: String, pub event_type: String, pub path: String, + pub note: Option, } #[derive(Debug, Clone, Serialize, Deserialize)] @@ -489,6 +515,7 @@ pub struct VersionEntry { #[derive(Debug, Clone, Serialize, Deserialize)] pub struct AiStreamEvent { + pub request_id: String, pub event_type: String, // "text", "done", "error" pub text: Option, pub error: Option, diff --git a/src-tauri/src/vault/operations.rs b/src-tauri/src/vault/operations.rs index e47a4e8..185cd75 100644 --- a/src-tauri/src/vault/operations.rs +++ b/src-tauri/src/vault/operations.rs @@ -1,6 +1,6 @@ use crate::types::{ - NoteContent, NoteEntry, NoteMeta, NoteTitleEntry, NotebookEntry, TagStyle, TrashContents, - TrashNotebookEntry, VaultState, + NoteContent, NoteEntry, NoteMeta, NoteTitleEntry, NotebookEntry, TagStyle, TagStyleChange, + TrashContents, TrashNotebookEntry, VaultState, }; use crate::vault::frontmatter; use chrono::{DateTime, Local, Locale, Utc}; @@ -8,7 +8,7 @@ use rayon::prelude::*; use std::cmp::Ordering; use std::collections::HashSet; use std::fs; -use std::io::Read; +use std::io::{Read, Write}; use std::path::{Component, Path, PathBuf}; use uuid::Uuid; use walkdir::WalkDir; @@ -440,9 +440,9 @@ fn read_note_entry(path: &Path, vault_root: &Path) -> Result read_note_entry_from_str(&raw, path, vault_root) } -/// Android-only: reads just the frontmatter (first 2KB) for tags/title/pinned, -/// uses filesystem timestamps for dates. No preview text. -#[cfg(mobile)] +/// Mobile listing reads the first 2KB and uses the filesystem modification time. +/// Persisted creation dates survive atomic file replacement. +#[cfg(any(mobile, test))] fn read_note_entry_metadata_only(path: &Path, vault_root: &Path) -> Result { // Read first 2KB - enough for frontmatter with tags, title, pinned let mut file = fs::File::open(path).map_err(|e| e.to_string())?; @@ -459,13 +459,15 @@ fn read_note_entry_metadata_only(path: &Path, vault_root: &Path) -> Result Result Result { +pub(crate) fn read_note_entry_fast(path: &Path, vault_root: &Path) -> Result { let mut file = fs::File::open(path).map_err(|e| e.to_string())?; let mut buf = vec![0u8; 2048]; let bytes_read = file.read(&mut buf).map_err(|e| e.to_string())?; @@ -588,25 +590,88 @@ fn read_note_content(validated: &Path, reported_path: &str) -> Result Result<(), String> { - let path = ensure_note_path(vault_path, Path::new(path))?; - let mut updated_meta = meta.clone(); - updated_meta.modified = Utc::now(); +pub struct SavedNote { + pub id: String, + pub previous_raw: String, +} - // Generate UUID on first save if note didn't have one - if updated_meta.id.is_empty() { +/// Returns the pre-save content only when the note actually changed. +pub fn save_note( + vault_path: &str, + path: &str, + meta: &NoteMeta, + body: &str, +) -> Result, String> { + let validated = ensure_note_path(vault_path, Path::new(path))?; + // Replace the actual file, not a symlink pointing to it. + let target = canonicalize_path(&validated, "note path")?; + let existing = read_note_content(&target, path)?; + let mut updated_meta = meta.clone(); + + // A stale editor may still have an empty ID after the first save. + if !existing.meta.id.is_empty() { + updated_meta.id.clone_from(&existing.meta.id); + } else if updated_meta.id.is_empty() { updated_meta.id = Uuid::new_v4().to_string(); } - // Read existing file to preserve unknown frontmatter fields - let existing = fs::read_to_string(&path).unwrap_or_default(); - let raw = if existing.is_empty() { - frontmatter::update_note_raw(&updated_meta, body) - } else { - frontmatter::merge_frontmatter(&existing, &updated_meta, body) - }; + // The editor appends one terminal LF; the note parser omits it. Do not + // trim spaces here: Markdown hard breaks and indentation are meaningful. + let comparable_body = body.strip_suffix('\n').unwrap_or(body); + if existing.content == comparable_body + && existing.meta.id == updated_meta.id + && existing.meta.title == updated_meta.title + && existing.meta.tags == updated_meta.tags + && existing.meta.pinned == updated_meta.pinned + && existing.meta.created == updated_meta.created + { + return Ok(None); + } - fs::write(path, raw).map_err(|e| e.to_string())?; + updated_meta.modified = Utc::now(); + let raw = frontmatter::merge_frontmatter(&existing.raw, &updated_meta, body); + replace_file_atomically(&target, raw.as_bytes())?; + + Ok(Some(SavedNote { + id: updated_meta.id, + previous_raw: existing.raw, + })) +} + +pub(crate) fn replace_file_atomically(path: &Path, content: &[u8]) -> Result<(), String> { + let permissions = match fs::metadata(path) { + Ok(metadata) => { + let permissions = metadata.permissions(); + if permissions.readonly() { + return Err("File is read-only".to_string()); + } + Some(permissions) + } + Err(error) if error.kind() == std::io::ErrorKind::NotFound => None, + Err(error) => return Err(error.to_string()), + }; + let parent = path.parent().ok_or("File has no parent directory")?; + let mut temporary = tempfile::Builder::new() + .prefix(".helixnotes-save-") + .tempfile_in(parent) + .map_err(|error| error.to_string())?; + temporary + .write_all(content) + .map_err(|error| error.to_string())?; + temporary.flush().map_err(|error| error.to_string())?; + if let Some(permissions) = permissions { + temporary + .as_file() + .set_permissions(permissions) + .map_err(|error| error.to_string())?; + } + temporary + .as_file() + .sync_all() + .map_err(|error| error.to_string())?; + // tempfile uses an atomic replacement on Unix and Windows, and owns cleanup + // on every failure before the replacement succeeds. + temporary.persist(path).map_err(|error| error.to_string())?; Ok(()) } @@ -1097,7 +1162,7 @@ pub fn rename_notebook(vault_path: &str, path: &str, new_name: &str) -> Result Result return Ok(format!(".helixnotes/attachments/{filename}")), + Err(error) if error.error.kind() == std::io::ErrorKind::AlreadyExists => { + temporary = error.file; + } + Err(error) => return Err(error.to_string()), + } + } } pub(crate) fn normalize_notebook_icon_key(path: &str) -> String { @@ -1414,11 +1492,13 @@ pub fn load_notebook_icons( vault_path: &str, ) -> Result, String> { let icons_path = helixnotes_dir(vault_path).join("notebook_icons.json"); - if !icons_path.exists() { - return Ok(std::collections::HashMap::new()); - } - - let data = fs::read_to_string(&icons_path).map_err(|e| e.to_string())?; + let data = match fs::read_to_string(&icons_path) { + Ok(data) => data, + Err(error) if error.kind() == std::io::ErrorKind::NotFound => { + return Ok(std::collections::HashMap::new()) + } + Err(error) => return Err(error.to_string()), + }; let icons: std::collections::HashMap = serde_json::from_str(&data).map_err(|e| e.to_string())?; Ok(icons @@ -1442,12 +1522,105 @@ pub fn set_notebook_icon( icons.remove(¬ebook_key); } } - let icons_path = helixnotes_dir(vault_path).join("notebook_icons.json"); - let data = serde_json::to_string_pretty(&icons).map_err(|e| e.to_string())?; - fs::write(&icons_path, data).map_err(|e| e.to_string())?; + save_notebook_icons(vault_path, &icons) +} + +fn save_notebook_icons( + vault_path: &str, + icons: &std::collections::HashMap, +) -> Result<(), String> { + let dir = helixnotes_dir(vault_path); + fs::create_dir_all(&dir).map_err(|error| error.to_string())?; + let data = serde_json::to_vec_pretty(icons).map_err(|error| error.to_string())?; + replace_file_atomically(&dir.join("notebook_icons.json"), &data) +} + +fn move_notebook_with_icons(vault_path: &str, src: &Path, dest: &Path) -> Result<(), String> { + let icons = load_notebook_icons(vault_path)?; + let old_key = normalize_notebook_icon_key( + &src.strip_prefix(vault_path) + .map_err(|error| error.to_string())? + .to_string_lossy(), + ); + let new_key = normalize_notebook_icon_key( + &dest + .strip_prefix(vault_path) + .map_err(|error| error.to_string())? + .to_string_lossy(), + ); + let prefix = format!("{old_key}/"); + let mut changed = false; + let migrated = icons + .into_iter() + .map(|(key, value)| { + let key = if key == old_key { + changed = true; + new_key.clone() + } else if let Some(suffix) = key.strip_prefix(&prefix) { + changed = true; + format!("{new_key}/{suffix}") + } else { + key + }; + (key, value) + }) + .collect(); + fs::rename(src, dest).map_err(|error| error.to_string())?; + if changed { + if let Err(error) = save_notebook_icons(vault_path, &migrated) { + return Err(match fs::rename(dest, src) { + Ok(()) => error, + Err(rollback) => format!("{error}; notebook move rollback failed: {rollback}"), + }); + } + } Ok(()) } +fn rollback_import( + vault_path: &str, + relative: &str, + result: Result, +) -> Result { + result.map_err( + |error| match fs::remove_file(Path::new(vault_path).join(relative)) { + Ok(()) => error, + Err(rollback) => format!("{error}; imported attachment rollback failed: {rollback}"), + }, + ) +} + +pub fn import_notebook_icon( + vault_path: &str, + notebook_relative: &str, + name: &str, + data: &[u8], +) -> Result { + let relative = save_attachment(vault_path, name, data)?; + rollback_import( + vault_path, + &relative, + set_notebook_icon(vault_path, notebook_relative, Some(&relative)), + )?; + Ok(relative) +} + +pub fn import_tag_icon( + vault_path: &str, + tag: &str, + name: &str, + data: &[u8], +) -> Result { + let relative = save_attachment(vault_path, name, data)?; + let result = patch_tag_style( + vault_path, + tag, + TagStyleChange::Icon(Some(relative.clone())), + ) + .and_then(|style| style.ok_or_else(|| "Imported tag icon was not registered".to_string())); + rollback_import(vault_path, &relative, result) +} + fn tag_styles_path(vault_path: &str) -> PathBuf { helixnotes_dir(vault_path).join("tag_styles.json") } @@ -1488,15 +1661,18 @@ pub fn load_tag_styles( vault_path: &str, ) -> Result, String> { let styles_path = tag_styles_path(vault_path); - if !styles_path.exists() { - return Ok(std::collections::HashMap::new()); - } - - let data = fs::read_to_string(&styles_path).map_err(|e| e.to_string())?; + let data = match fs::read_to_string(&styles_path) { + Ok(data) => data, + Err(error) if error.kind() == std::io::ErrorKind::NotFound => { + return Ok(std::collections::HashMap::new()) + } + Err(error) => return Err(error.to_string()), + }; let styles: std::collections::HashMap = serde_json::from_str(&data).map_err(|e| e.to_string())?; let mut collapsed = std::collections::HashMap::new(); for (tag, style) in styles { + normalize_tag_color(style.color.as_deref())?; let key = normalize_tag_style_key(&tag); if key.is_empty() || style.is_empty() { continue; @@ -1507,7 +1683,11 @@ pub fn load_tag_styles( Ok(collapsed) } -pub fn set_tag_style(vault_path: &str, tag: &str, style: Option) -> Result<(), String> { +pub fn patch_tag_style( + vault_path: &str, + tag: &str, + change: TagStyleChange, +) -> Result, String> { let key = normalize_tag_style_key(tag); if key.is_empty() { return Err("Tag name is required".to_string()); @@ -1518,28 +1698,45 @@ pub fn set_tag_style(vault_path: &str, tag: &str, style: Option) -> Re .keys() .find(|existing| tag_style_key_fold(existing) == tag_style_key_fold(&key)) .cloned() - .unwrap_or_else(|| key.clone()); - match style { - Some(style) => { - let next = TagStyle { - icon: normalize_tag_icon(style.icon.as_deref()), - color: normalize_tag_color(style.color.as_deref())?, - }; - remove_matching_tag_styles(&mut styles, &key); - if !next.is_empty() { - styles.insert(stored_key, next); - } - } - None => { - remove_matching_tag_styles(&mut styles, &key); - } + .unwrap_or(key); + let mut next = styles.remove(&stored_key).unwrap_or_default(); + match change { + TagStyleChange::Icon(value) => next.icon = normalize_tag_icon(value.as_deref()), + TagStyleChange::Color(value) => next.color = normalize_tag_color(value.as_deref())?, + TagStyleChange::Reset => next = TagStyle::default(), + } + let next = (!next.is_empty()).then_some(next); + if let Some(style) = &next { + styles.insert(stored_key, style.clone()); } let dir = helixnotes_dir(vault_path); fs::create_dir_all(&dir).map_err(|e| e.to_string())?; let data = serde_json::to_string_pretty(&styles).map_err(|e| e.to_string())?; - fs::write(tag_styles_path(vault_path), data).map_err(|e| e.to_string())?; - Ok(()) + replace_file_atomically(&tag_styles_path(vault_path), data.as_bytes())?; + Ok(next) +} + +pub(crate) fn attachment_metadata_references( + mapping: &str, + content: &str, +) -> Result, String> { + if mapping == "notebook_icons.json" { + let icons: std::collections::HashMap = + serde_json::from_str(content).map_err(|error| error.to_string())?; + Ok(icons.into_values().collect()) + } else { + let styles: std::collections::HashMap = + serde_json::from_str(content).map_err(|error| error.to_string())?; + let mut references = Vec::new(); + for style in styles.into_values() { + normalize_tag_color(style.color.as_deref())?; + if let Some(icon) = style.icon { + references.push(icon); + } + } + Ok(references) + } } pub fn load_quick_access(vault_path: &str) -> Result, String> { @@ -1660,11 +1857,140 @@ pub fn sanitize_filename(name: &str) -> String { mod tests { use super::{ compare_natural_names, create_notebook, duplicate_note, get_note_switcher_titles, - helixnotes_dir, load_notebook_icons, load_tag_styles, permanent_delete, read_note, - restore_notebook, scan_notebooks, set_notebook_icon, set_tag_style, + helixnotes_dir, load_notebook_icons, load_tag_styles, patch_tag_style, permanent_delete, + read_note, replace_file_atomically, restore_notebook, save_note, scan_notebooks, + set_notebook_icon, }; - use crate::types::TagStyle; + use crate::types::{TagStyle, TagStyleChange}; use std::fs; + use std::io::Read; + + #[test] + fn same_named_attachment_writes_never_replace_each_other() { + let vault = tempfile::tempdir().unwrap(); + let path = vault.path().to_str().unwrap(); + super::ensure_vault_structure(path).unwrap(); + let assets = std::thread::scope(|scope| { + let writes: Vec<_> = (0..16u8) + .map(|byte| { + scope.spawn(move || { + let relative = + super::save_attachment(path, "same.png", &[byte; 8]).unwrap(); + (relative, byte) + }) + }) + .collect(); + writes + .into_iter() + .map(|thread| thread.join().unwrap()) + .collect::>() + }); + let mut names = std::collections::HashSet::new(); + for (relative, byte) in assets { + assert!(names.insert(relative.clone())); + assert_eq!(fs::read(vault.path().join(relative)).unwrap(), [byte; 8]); + } + } + + #[test] + fn metadata_setters_atomically_replace_without_truncating_open_readers() { + let vault = tempfile::tempdir().unwrap(); + let path = vault.path().to_str().unwrap(); + super::ensure_vault_structure(path).unwrap(); + set_notebook_icon(path, "Book", Some("builtin:folder")).unwrap(); + patch_tag_style(path, "Work", TagStyleChange::Color(Some("#123456".into()))).unwrap(); + let icons_path = helixnotes_dir(path).join("notebook_icons.json"); + let styles_path = helixnotes_dir(path).join("tag_styles.json"); + let icons_before = fs::read(&icons_path).unwrap(); + let styles_before = fs::read(&styles_path).unwrap(); + let mut icons_reader = fs::File::open(&icons_path).unwrap(); + let mut styles_reader = fs::File::open(&styles_path).unwrap(); + set_notebook_icon(path, "Book", Some("builtin:star")).unwrap(); + patch_tag_style( + path, + "Work", + TagStyleChange::Icon(Some("builtin:briefcase".into())), + ) + .unwrap(); + let mut icons_old = Vec::new(); + let mut styles_old = Vec::new(); + icons_reader.read_to_end(&mut icons_old).unwrap(); + styles_reader.read_to_end(&mut styles_old).unwrap(); + assert_eq!(icons_old, icons_before); + assert_eq!(styles_old, styles_before); + assert_eq!(load_notebook_icons(path).unwrap()["Book"], "builtin:star"); + assert_eq!( + load_tag_styles(path).unwrap()["Work"], + TagStyle { + icon: Some("builtin:briefcase".into()), + color: Some("#123456".into()), + } + ); + } + + #[test] + fn failed_icon_registration_rolls_back_only_the_new_unique_attachment() { + for mapping in ["notebook_icons.json", "tag_styles.json"] { + let vault = tempfile::tempdir().unwrap(); + let path = vault.path().to_str().unwrap(); + super::ensure_vault_structure(path).unwrap(); + let original = super::save_attachment(path, "same.png", b"keep").unwrap(); + fs::write(helixnotes_dir(path).join(mapping), b"{broken").unwrap(); + let result = if mapping == "notebook_icons.json" { + super::import_notebook_icon(path, "Book", "same.png", b"reject") + } else { + super::import_tag_icon(path, "Work", "same.png", b"reject") + .map(|style| style.icon.unwrap()) + }; + assert!(result.is_err()); + assert_eq!(fs::read(vault.path().join(&original)).unwrap(), b"keep"); + let remaining: Vec<_> = fs::read_dir(helixnotes_dir(path).join("attachments")) + .unwrap() + .map(|entry| entry.unwrap().path()) + .collect(); + assert_eq!(remaining, vec![vault.path().join(original)]); + assert_eq!( + fs::read(helixnotes_dir(path).join(mapping)).unwrap(), + b"{broken" + ); + } + } + + #[test] + fn invalid_icon_mapping_prevents_notebook_move_before_renaming_files() { + let vault = tempfile::tempdir().unwrap(); + let path = vault.path().to_str().unwrap(); + super::ensure_vault_structure(path).unwrap(); + let notebook = create_notebook(path, None, "Book").unwrap(); + let note = super::create_note(path, Some("Book"), "Keep").unwrap(); + let before = fs::read(¬e.path).unwrap(); + let destination = create_notebook(path, None, "Archive").unwrap(); + fs::write(helixnotes_dir(path).join("notebook_icons.json"), b"{broken").unwrap(); + assert!(super::rename_notebook(path, ¬ebook.path, "Renamed").is_err()); + assert!(super::move_notebook(path, ¬ebook.path, &destination.path).is_err()); + assert_eq!(fs::read(¬e.path).unwrap(), before); + assert!(!vault.path().join("Renamed").exists()); + assert!(!vault.path().join("Archive/Book").exists()); + } + + #[cfg(unix)] + #[test] + fn failed_metadata_replacement_keeps_registration_and_rolls_back_notebook_move() { + use std::os::unix::fs::PermissionsExt; + let vault = tempfile::tempdir().unwrap(); + let path = vault.path().to_str().unwrap(); + super::ensure_vault_structure(path).unwrap(); + let notebook = create_notebook(path, None, "Book").unwrap(); + set_notebook_icon(path, "Book", Some("builtin:folder")).unwrap(); + let mapping = helixnotes_dir(path).join("notebook_icons.json"); + let before = fs::read(&mapping).unwrap(); + fs::set_permissions(&mapping, fs::Permissions::from_mode(0o440)).unwrap(); + assert!(set_notebook_icon(path, "Book", Some("builtin:star")).is_err()); + assert!(super::rename_notebook(path, ¬ebook.path, "Renamed").is_err()); + assert!(std::path::Path::new(¬ebook.path).is_dir()); + assert!(!vault.path().join("Renamed").exists()); + assert_eq!(fs::read(&mapping).unwrap(), before); + } use uuid::Uuid; #[test] @@ -1761,13 +2087,16 @@ mod tests { std::env::temp_dir().join(format!("helixnotes-tag-style-test-{}", Uuid::new_v4())); let vault_path = vault.to_string_lossy(); - set_tag_style( + patch_tag_style( &vault_path, " Work ", - Some(TagStyle { - icon: Some("builtin:briefcase".into()), - color: Some("#E11D48".into()), - }), + TagStyleChange::Icon(Some("builtin:briefcase".into())), + ) + .unwrap(); + patch_tag_style( + &vault_path, + "Work", + TagStyleChange::Color(Some("#E11D48".into())), ) .unwrap(); @@ -1783,7 +2112,7 @@ mod tests { assert_eq!(work.icon.as_deref(), Some("builtin:briefcase")); assert_eq!(work.color.as_deref(), Some("#e11d48")); - set_tag_style(&vault_path, "Work", None).unwrap(); + patch_tag_style(&vault_path, "Work", TagStyleChange::Reset).unwrap(); assert!(load_tag_styles(&vault_path).unwrap().is_empty()); fs::remove_dir_all(vault).unwrap(); @@ -1795,17 +2124,20 @@ mod tests { std::env::temp_dir().join(format!("helixnotes-tag-style-case-test-{}", Uuid::new_v4())); let vault_path = vault.to_string_lossy(); - set_tag_style( + patch_tag_style( &vault_path, "Work", - Some(TagStyle { - icon: Some("builtin:briefcase".into()), - color: Some("#e11d48".into()), - }), + TagStyleChange::Icon(Some("builtin:briefcase".into())), + ) + .unwrap(); + patch_tag_style( + &vault_path, + "Work", + TagStyleChange::Color(Some("#e11d48".into())), ) .unwrap(); - set_tag_style(&vault_path, "work", None).unwrap(); + patch_tag_style(&vault_path, "work", TagStyleChange::Reset).unwrap(); let stored: serde_json::Value = serde_json::from_str( &fs::read_to_string(helixnotes_dir(&vault_path).join("tag_styles.json")).unwrap(), @@ -1848,13 +2180,10 @@ mod tests { )); let vault_path = vault.to_string_lossy(); - let err = set_tag_style( + let err = patch_tag_style( &vault_path, "daily", - Some(TagStyle { - icon: None, - color: Some("red".into()), - }), + TagStyleChange::Color(Some("red".into())), ) .unwrap_err(); assert!(err.contains("hex")); @@ -1870,24 +2199,13 @@ mod tests { )); let vault_path = vault.to_string_lossy(); - set_tag_style( + patch_tag_style( &vault_path, "daily", - Some(TagStyle { - icon: Some("builtin:calendar".into()), - color: None, - }), - ) - .unwrap(); - set_tag_style( - &vault_path, - "daily", - Some(TagStyle { - icon: None, - color: None, - }), + TagStyleChange::Icon(Some("builtin:calendar".into())), ) .unwrap(); + patch_tag_style(&vault_path, "daily", TagStyleChange::Icon(None)).unwrap(); assert!(load_tag_styles(&vault_path).unwrap().is_empty()); fs::remove_dir_all(vault).unwrap(); @@ -2057,4 +2375,151 @@ mod tests { fs::remove_dir_all(vault).unwrap(); } + + #[test] + fn mobile_listing_preserves_creation_date_after_atomic_save() { + let vault = tempfile::tempdir().unwrap(); + let path = vault.path().join("Note.md"); + fs::write(&path, "---\nid: existing\ntitle: Note\ncreated: 2020-01-01T00:00:00Z\n---\n# Note\n\nOld body.\n").unwrap(); + let note = read_note(vault.path().to_str().unwrap(), path.to_str().unwrap()).unwrap(); + save_note( + vault.path().to_str().unwrap(), + path.to_str().unwrap(), + ¬e.meta, + "New body.", + ) + .unwrap(); + let mobile = super::read_note_entry_metadata_only(&path, vault.path()).unwrap(); + let desktop = super::read_note_entry_fast(&path, vault.path()).unwrap(); + assert_eq!(mobile.meta.created, note.meta.created); + assert_eq!(mobile.meta.created, desktop.meta.created); + assert_eq!(mobile.preview, "New body."); + } + + #[test] + fn unchanged_serialized_save_ignores_modified_without_rewriting_the_file() { + let vault = tempfile::tempdir().unwrap(); + let path = vault.path().join("Note.md"); + let original = "---\nid: existing\ntitle: Note\ncreated: 2020-01-01T00:00:00Z\nmodified: 2020-01-02T00:00:00Z\naliases: [Legacy]\n---\n# Note\n\nUnchanged body.\n"; + fs::write(&path, original).unwrap(); + let vault_path = vault.path().to_string_lossy(); + let note_path = path.to_string_lossy(); + let note = read_note(&vault_path, ¬e_path).unwrap(); + let before = fs::metadata(&path).unwrap().modified().unwrap(); + let mut meta = note.meta; + meta.modified = chrono::Utc::now(); + let serialized_body = format!("{}\n", note.content); + + assert!(save_note(&vault_path, ¬e_path, &meta, &serialized_body) + .unwrap() + .is_none()); + assert_eq!(fs::read_to_string(&path).unwrap(), original); + assert_eq!(fs::metadata(&path).unwrap().modified().unwrap(), before); + } + + #[test] + fn save_keeps_authored_terminal_spaces_before_the_serializer_newline() { + let vault = tempfile::tempdir().unwrap(); + let path = vault.path().join("Note.md"); + let original = "---\nid: existing\ntitle: Note\ncreated: 2020-01-01T00:00:00Z\n---\n# Note\n\nFinal line.\n"; + fs::write(&path, original).unwrap(); + let vault_path = vault.path().to_string_lossy(); + let note_path = path.to_string_lossy(); + let note = read_note(&vault_path, ¬e_path).unwrap(); + let body = format!("{} \n", note.content); + + assert!(save_note(&vault_path, ¬e_path, ¬e.meta, &body) + .unwrap() + .is_some()); + assert!(fs::read_to_string(&path) + .unwrap() + .ends_with("Final line. \n")); + } + + #[test] + fn saves_keep_first_identity_unknown_fields_and_previous_content() { + let vault = tempfile::tempdir().unwrap(); + let path = vault.path().join("Note.md"); + let original = "---\ntitle: Note\ncreated: 2020-01-01T00:00:00Z\naliases: [Legacy]\n---\nOriginal body.\n"; + fs::write(&path, original).unwrap(); + let vault_path = vault.path().to_string_lossy(); + let note_path = path.to_string_lossy(); + let meta = read_note(&vault_path, ¬e_path).unwrap().meta; + assert!(meta.id.is_empty()); + + let first = save_note(&vault_path, ¬e_path, &meta, "# Note\n\nFirst change.\n") + .unwrap() + .unwrap(); + Uuid::parse_str(&first.id).unwrap(); + assert_eq!(first.previous_raw, original); + let first_raw = fs::read_to_string(&path).unwrap(); + let second = save_note(&vault_path, ¬e_path, &meta, "# Note\n\nSecond change.\n") + .unwrap() + .unwrap(); + assert_eq!(second.id, first.id); + assert_eq!(second.previous_raw, first_raw); + + let saved = read_note(&vault_path, ¬e_path).unwrap(); + assert_eq!(saved.meta.id, first.id); + assert_eq!(saved.meta.created, meta.created); + assert!(saved.content.contains("Second change.")); + let parsed = gray_matter::Matter::::new().parse(&saved.raw); + let fields: serde_yaml::Value = parsed.data.unwrap().deserialize().unwrap(); + assert_eq!(fields["aliases"][0].as_str(), Some("Legacy")); + } + + #[test] + fn atomic_replacement_does_not_truncate_an_open_original() { + let vault = tempfile::tempdir().unwrap(); + let path = vault.path().join("Note.md"); + fs::write(&path, "original contents").unwrap(); + let mut original = fs::File::open(&path).unwrap(); + + replace_file_atomically(&path, b"replacement contents").unwrap(); + + let mut old_contents = String::new(); + original.read_to_string(&mut old_contents).unwrap(); + assert_eq!(old_contents, "original contents"); + assert_eq!(fs::read_to_string(&path).unwrap(), "replacement contents"); + } + + #[test] + fn failed_atomic_replacement_cleans_up_without_removing_destination() { + let vault = tempfile::tempdir().unwrap(); + let destination = vault.path().join("Note.md"); + fs::create_dir(&destination).unwrap(); + fs::write(destination.join("original"), "keep me").unwrap(); + + assert!(replace_file_atomically(&destination, b"replacement").is_err()); + + assert_eq!( + fs::read_to_string(destination.join("original")).unwrap(), + "keep me" + ); + let remaining: Vec<_> = fs::read_dir(vault.path()) + .unwrap() + .map(|entry| entry.unwrap().file_name()) + .collect(); + assert_eq!(remaining, vec![std::ffi::OsString::from("Note.md")]); + } + + #[cfg(unix)] + #[test] + fn atomic_save_preserves_permissions_and_refuses_read_only_notes() { + use std::os::unix::fs::PermissionsExt; + + let vault = tempfile::tempdir().unwrap(); + let path = vault.path().join("Note.md"); + fs::write(&path, "original").unwrap(); + fs::set_permissions(&path, fs::Permissions::from_mode(0o640)).unwrap(); + replace_file_atomically(&path, b"saved").unwrap(); + assert_eq!( + fs::metadata(&path).unwrap().permissions().mode() & 0o777, + 0o640 + ); + + fs::set_permissions(&path, fs::Permissions::from_mode(0o440)).unwrap(); + assert!(replace_file_atomically(&path, b"must not save").is_err()); + assert_eq!(fs::read_to_string(&path).unwrap(), "saved"); + } } diff --git a/src-tauri/src/vault/watcher.rs b/src-tauri/src/vault/watcher.rs index 660a4c2..cfef5e8 100644 --- a/src-tauri/src/vault/watcher.rs +++ b/src-tauri/src/vault/watcher.rs @@ -1,15 +1,18 @@ -use notify::{Config, EventKind, PollWatcher, RecommendedWatcher, RecursiveMode, Watcher}; -use std::path::Path; +use notify::event::{CreateKind, ModifyKind, RemoveKind, RenameMode}; +use notify::{Config, Event, EventKind, PollWatcher, RecommendedWatcher, RecursiveMode, Watcher}; +use std::collections::HashMap; +use std::path::{Component, Path, PathBuf}; use std::sync::mpsc; -use std::time::Duration; +use std::time::{Duration, Instant}; use tauri::{AppHandle, Emitter, Manager}; use crate::state::AppState; use crate::types::FileEvent; -use crate::vault::operations::helixnotes_dir; +use crate::vault::operations::{is_hidden, read_note_entry_fast}; const IOS_POLL_INTERVAL: Duration = Duration::from_secs(10); const NATIVE_POLL_INTERVAL: Duration = Duration::from_secs(1); +const EVENT_BATCH_INTERVAL: Duration = Duration::from_millis(50); #[derive(Debug, Clone, Copy, PartialEq, Eq)] enum WatcherBackend { @@ -46,6 +49,69 @@ impl VaultWatcher { } } +fn collect_changes(event: Event, vault_root: &Path, changes: &mut HashMap) { + let (default_type, may_be_directory) = match event.kind { + EventKind::Create(kind) => ("create", kind != CreateKind::File), + EventKind::Remove(kind) => ("remove", kind != RemoveKind::File), + EventKind::Modify(ModifyKind::Name(_)) => ("modify", true), + EventKind::Modify(_) => ("modify", false), + _ => return, + }; + for (index, path) in event.paths.into_iter().enumerate() { + let Ok(relative) = path.strip_prefix(vault_root) else { + continue; + }; + if !relative.components().all(|component| match component { + Component::Normal(name) => !is_hidden(Path::new(name)), + Component::CurDir => true, + _ => false, + }) { + continue; + } + // Removed/renamed directories no longer pass is_dir(). Retain unknown + // missing paths so external notebook changes still reach the UI. + if path.extension().and_then(|extension| extension.to_str()) != Some("md") + && !path.is_dir() + && !(may_be_directory && !path.exists()) + { + continue; + } + let event_type = match event.kind { + EventKind::Modify(ModifyKind::Name(RenameMode::From)) => "remove", + EventKind::Modify(ModifyKind::Name(RenameMode::Both)) if index == 0 => "remove", + _ => default_type, + }; + changes.insert(path, event_type); + } +} + +pub(crate) fn changed_entry(path: PathBuf, event_type: &str, vault_root: &Path) -> FileEvent { + let is_note = + path.extension().and_then(|extension| extension.to_str()) == Some("md") && path.is_file(); + let note = if is_note { + read_note_entry_fast(&path, vault_root).ok() + } else { + None + }; + // Atomic replacement may also report removal of the old destination. The + // final filesystem state, not the platform's rename spelling, is authoritative. + let event_type = if path.exists() { + if event_type == "remove" { + "modify" + } else { + event_type + } + } else { + "remove" + }; + FileEvent { + vault_path: vault_root.to_string_lossy().to_string(), + event_type: event_type.to_string(), + path: path.to_string_lossy().to_string(), + note, + } +} + pub fn start_watcher(app: AppHandle, vault_path: String) -> Result { let (tx, rx) = mpsc::channel(); @@ -64,58 +130,80 @@ pub fn start_watcher(app: AppHandle, vault_path: String) -> Result { - // Skip all file events while importing - let state = app.state::(); - if state.importing.load(std::sync::atomic::Ordering::Relaxed) { - continue; - } - // Skip .helixnotes directory events - let dominated_by_hn = event.paths.iter().all(|p| p.starts_with(&hn_dir)); - if dominated_by_hn { - continue; - } - - // Only care about .md file events - let has_md = event.paths.iter().any(|p| { - p.extension().and_then(|x| x.to_str()) == Some("md") || p.is_dir() - }); - - if !has_md { - continue; - } - - let event_type = match event.kind { - EventKind::Create(_) => "create", - EventKind::Modify(_) => "modify", - EventKind::Remove(_) => "remove", - _ => continue, - }; - - for path in &event.paths { - let fe = FileEvent { - event_type: event_type.to_string(), - path: path.to_string_lossy().to_string(), - }; - let _ = app.emit("file-changed", &fe); - } - - // On mobile, throttle event emission to prevent IPC flooding - // on FUSE filesystems where Syncthing/other apps generate - // constant file activity that blocks the Tauri command channel. - #[cfg(mobile)] - { - std::thread::sleep(Duration::from_secs(2)); - while rx.try_recv().is_ok() {} - } + let vault_root = Path::new(&vault_path); + let mut changes = HashMap::new(); + loop { + let first = if changes.is_empty() { + match rx.recv() { + Ok(event) => Some(event), + Err(_) => return, } - Err(e) => { - log::error!("File watcher error: {}", e); + } else { + match rx.recv_timeout(EVENT_BATCH_INTERVAL) { + Ok(event) => Some(event), + Err(mpsc::RecvTimeoutError::Timeout) => None, + Err(mpsc::RecvTimeoutError::Disconnected) => return, + } + }; + let deadline = Instant::now() + EVENT_BATCH_INTERVAL; + let mut next = first; + while let Some(result) = next { + match result { + Ok(event) => { + collect_changes(event, vault_root, &mut changes); + } + Err(error) => log::error!("File watcher error: {error}"), + } + let remaining = deadline.saturating_duration_since(Instant::now()); + if remaining.is_zero() { + break; + } + next = rx.recv_timeout(remaining).ok(); + } + let state = app.state::(); + // Keep the batch while muted, rather than losing concurrent external + // edits. Nothing is indexed or emitted until local imports finish. + if state.importing.load(std::sync::atomic::Ordering::Relaxed) { + continue; + } + let search = { + // Match the runtime commit's lock order so an old watcher cannot + // send its final batch into the newly selected vault's index. + let Ok(search) = state.search_index.lock() else { + continue; + }; + let active = state + .config + .lock() + .ok() + .is_some_and(|config| config.active_vault.as_deref() == Some(&vault_path)); + if !active { + continue; + } + search.clone() + }; + let mut refresh_vault = false; + for (path, event_type) in changes.drain() { + if path.extension().and_then(|extension| extension.to_str()) == Some("md") + && !path.is_dir() + { + if let Some(search) = &search { + if let Err(error) = search.queue_index_note(&path.to_string_lossy()) { + log::error!("Could not queue external note for search: {error}"); + } + } + } else { + refresh_vault = true; + } + let event = changed_entry(path, event_type, vault_root); + let _ = app.emit("file-changed", &event); + } + if refresh_vault { + if let Some(search) = &search { + if let Err(error) = search.queue_vault_refresh(&vault_path) { + log::error!("Could not queue notebook search refresh: {error}"); + } } } } @@ -152,4 +240,136 @@ mod tests { Duration::from_secs(1) ); } + + fn mapped_events(events: Vec, vault_root: &Path) -> Vec { + let mut changes = HashMap::new(); + for event in events { + collect_changes(event, vault_root, &mut changes); + } + changes + .into_iter() + .map(|(path, event_type)| changed_entry(path, event_type, vault_root)) + .collect() + } + + #[test] + fn atomic_replace_emits_only_enriched_destination() { + let vault = tempfile::tempdir().unwrap(); + let target = vault.path().join("Note.md"); + let temporary = vault.path().join(".helixnotes-save-123"); + std::fs::write(&target, "---\ntitle: New title\n---\nUpdated preview.").unwrap(); + let events = mapped_events( + vec![ + Event::new(EventKind::Modify(ModifyKind::Name(RenameMode::Both))) + .add_path(temporary) + .add_path(target.clone()), + Event::new(EventKind::Remove(RemoveKind::File)).add_path(target.clone()), + Event::new(EventKind::Create(CreateKind::File)) + .add_path(vault.path().join(".helixnotes/history/snapshot.md")), + Event::new(EventKind::Create(CreateKind::File)) + .add_path(vault.path().join(".hidden/secret.md")), + ], + vault.path(), + ); + + assert_eq!(events.len(), 1); + assert_eq!(events[0].path, target.to_string_lossy()); + assert_eq!(events[0].event_type, "modify"); + let note = events[0].note.as_ref().unwrap(); + assert_eq!(note.meta.title, "New title"); + assert_eq!(note.relative_path, "Note.md"); + assert_eq!(note.preview, "Updated preview."); + } + + #[test] + fn external_note_and_directory_renames_keep_both_sides() { + let vault = tempfile::tempdir().unwrap(); + let old_note = vault.path().join("Before.md"); + let new_note = vault.path().join("After.md"); + let old_directory = vault.path().join("Old.notebook"); + let new_directory = vault.path().join("New.notebook"); + std::fs::write(&new_note, "renamed note").unwrap(); + std::fs::create_dir(&new_directory).unwrap(); + let events = mapped_events( + vec![ + Event::new(EventKind::Modify(ModifyKind::Name(RenameMode::Both))) + .add_path(old_note.clone()) + .add_path(new_note.clone()), + Event::new(EventKind::Modify(ModifyKind::Name(RenameMode::Both))) + .add_path(old_directory.clone()) + .add_path(new_directory.clone()), + ], + vault.path(), + ); + + for removed in [old_note, old_directory] { + let event = events + .iter() + .find(|event| event.path == removed.to_string_lossy()) + .unwrap(); + assert_eq!(event.event_type, "remove"); + assert!(event.note.is_none()); + } + let note = events + .iter() + .find(|event| event.path == new_note.to_string_lossy()) + .unwrap(); + assert_eq!(note.event_type, "modify"); + assert!(note.note.is_some()); + let directory = events + .iter() + .find(|event| event.path == new_directory.to_string_lossy()) + .unwrap(); + assert_eq!(directory.event_type, "modify"); + assert!(directory.note.is_none()); + } + + #[test] + fn batching_keeps_distinct_external_changes_and_deleted_directories() { + let vault = tempfile::tempdir().unwrap(); + let first = vault.path().join("First.md"); + let second = vault.path().join("Second.md"); + let removed = vault.path().join("Removed.notebook"); + std::fs::write(&first, "first external change").unwrap(); + std::fs::write(&second, "second external change").unwrap(); + let events = mapped_events( + vec![ + Event::new(EventKind::Create(CreateKind::File)).add_path(first.clone()), + Event::new(EventKind::Modify(ModifyKind::Any)).add_path(first.clone()), + Event::new(EventKind::Create(CreateKind::File)).add_path(second.clone()), + Event::new(EventKind::Remove(RemoveKind::Folder)).add_path(removed.clone()), + ], + vault.path(), + ); + + let first_event = events + .iter() + .find(|event| event.path == first.to_string_lossy()) + .unwrap(); + assert_eq!( + first_event.note.as_ref().unwrap().preview, + "first external change" + ); + let second_event = events + .iter() + .find(|event| event.path == second.to_string_lossy()) + .unwrap(); + assert_eq!( + second_event.note.as_ref().unwrap().preview, + "second external change" + ); + let removed_event = events + .iter() + .find(|event| event.path == removed.to_string_lossy()) + .unwrap(); + assert_eq!(removed_event.event_type, "remove"); + assert!(removed_event.note.is_none()); + assert_eq!( + events + .iter() + .filter(|event| event.path == first.to_string_lossy()) + .count(), + 1 + ); + } } diff --git a/src/lib/api.ts b/src/lib/api.ts index 31c7831..3201328 100644 --- a/src/lib/api.ts +++ b/src/lib/api.ts @@ -7,11 +7,12 @@ import type { NoteMeta, NotebookEntry, NoteTitleEntry, - SearchResult, + SearchPage, TrashContents, VaultState, VaultStats, TagStyle, + TagStyleChange, ImportResult, BackupEntry, VersionEntry, @@ -40,6 +41,10 @@ export async function getAppConfig(): Promise { return invoke("get_app_config"); } +export async function completeWindowClose(): Promise { + return invoke("complete_window_close"); +} + export async function setTheme(theme: string): Promise { return invoke("set_theme", { theme }); } @@ -101,28 +106,31 @@ export async function countRootNotes(): Promise { } export async function createNotebook( + vaultPath: string, parentRelative: string | null, name: string, ): Promise { - return invoke("create_notebook", { parentRelative, name }); + return invoke("create_notebook", { vaultPath, parentRelative, name }); } export async function renameNotebook( + vaultPath: string, path: string, newName: string, ): Promise { - return invoke("rename_notebook", { path, newName }); + return invoke("rename_notebook", { vaultPath, path, newName }); } -export async function deleteNotebook(path: string): Promise { - return invoke("delete_notebook", { path }); +export async function deleteNotebook(vaultPath: string, path: string): Promise { + return invoke("delete_notebook", { vaultPath, path }); } export async function moveNotebook( + vaultPath: string, notebookPath: string, destParent: string, ): Promise { - return invoke("move_notebook", { notebookPath, destParent }); + return invoke("move_notebook", { vaultPath, notebookPath, destParent }); } export async function getNotes( @@ -131,57 +139,61 @@ export async function getNotes( return invoke("get_notes", { notebookPath }); } -export async function readNote(path: string): Promise { - return invoke("read_note", { path }); +export async function readNote(vaultPath: string, path: string): Promise { + return invoke("read_note", { vaultPath, path }); } export async function saveNote( + vaultPath: string, path: string, meta: NoteMeta, body: string, ): Promise { - return invoke("save_note", { path, meta, body }); + return invoke("save_note", { vaultPath, path, meta, body }); } export async function createNote( + vaultPath: string, notebookRelative: string | null, title: string, ): Promise { - return invoke("create_note", { notebookRelative, title }); + return invoke("create_note", { vaultPath, notebookRelative, title }); } -export async function duplicateNote(path: string): Promise { - return invoke("duplicate_note", { path }); +export async function duplicateNote(vaultPath: string, path: string): Promise { + return invoke("duplicate_note", { vaultPath, path }); } -export async function createDailyNote(date?: string): Promise { - return invoke("create_daily_note", { date: date ?? null }); +export async function createDailyNote(vaultPath: string, date?: string): Promise { + return invoke("create_daily_note", { vaultPath, date: date ?? null }); } export async function renameNote( + vaultPath: string, path: string, newTitle: string, ): Promise { - return invoke("rename_note", { path, newTitle }); + return invoke("rename_note", { vaultPath, path, newTitle }); } -export async function deleteNote(path: string): Promise { - return invoke("delete_note", { path }); +export async function deleteNote(vaultPath: string, path: string): Promise { + return invoke("delete_note", { vaultPath, path }); } export async function moveNote( + vaultPath: string, notePath: string, destNotebook: string, ): Promise { - return invoke("move_note", { notePath, destNotebook }); + return invoke("move_note", { vaultPath, notePath, destNotebook }); } export async function getAllTags(): Promise<[string, number][]> { return invoke("get_all_tags"); } -export async function getAllNoteTitles(): Promise { - return invoke("get_all_note_titles"); +export async function getAllNoteTitles(vaultPath: string): Promise { + return invoke("get_all_note_titles", { vaultPath }); } export async function getNoteSwitcherTitles( @@ -190,15 +202,16 @@ export async function getNoteSwitcherTitles( return invoke("get_note_switcher_titles", { recentPaths }); } -export async function getGraphData(): Promise<{ nodes: { title: string; path: string }[]; edges: { source: number; target: number; bidirectional: boolean }[] }> { - return invoke("get_graph_data"); +export async function getGraphData(vaultPath: string): Promise<{ nodes: { title: string; path: string }[]; edges: { source: number; target: number; bidirectional: boolean }[] }> { + return invoke("get_graph_data", { vaultPath }); } export async function searchNotes( query: string, limit?: number, -): Promise { - return invoke("search_notes", { query, limit }); + offset?: number, +): Promise { + return invoke("search_notes", { query, limit, offset }); } export async function reindex(): Promise { @@ -210,30 +223,31 @@ export async function getTrash(): Promise { } export async function restoreNote( + vaultPath: string, trashPath: string, destNotebook: string | null, ): Promise { - return invoke("restore_note", { trashPath, destNotebook }); + return invoke("restore_note", { vaultPath, trashPath, destNotebook }); } -export async function restoreNotebook(trashPath: string): Promise { - return invoke("restore_notebook", { trashPath }); +export async function restoreNotebook(vaultPath: string, trashPath: string): Promise { + return invoke("restore_notebook", { vaultPath, trashPath }); } -export async function permanentDelete(path: string): Promise { - return invoke("permanent_delete", { path }); +export async function permanentDelete(vaultPath: string, path: string): Promise { + return invoke("permanent_delete", { vaultPath, path }); } -export async function emptyTrash(): Promise { - return invoke("empty_trash"); +export async function emptyTrash(vaultPath: string): Promise { + return invoke("empty_trash", { vaultPath }); } -export async function loadVaultState(): Promise { - return invoke("load_vault_state"); +export async function loadVaultState(vaultPath: string): Promise { + return invoke("load_vault_state", { vaultPath }); } -export async function saveVaultState(vaultState: VaultState): Promise { - return invoke("save_vault_state", { vaultState }); +export async function saveVaultState(vaultPath: string, vaultState: VaultState): Promise { + return invoke("save_vault_state", { vaultPath, vaultState }); } export async function copyTextToClipboard(text: string): Promise { @@ -248,37 +262,58 @@ export async function copyImageToClipboard(path: string): Promise { return invoke("copy_image_to_clipboard", { path }); } -export async function saveImage(name: string, data: number[]): Promise { - return invoke("save_image", { name, data }); +export async function saveImage(vaultPath: string, name: string, data: number[]): Promise { + return invoke("save_image", { vaultPath, name, data }); } export async function saveAttachment( + vaultPath: string, name: string, data: number[], ): Promise { - return invoke("save_attachment", { name, data }); + return invoke("save_attachment", { vaultPath, name, data }); } -export async function getNotebookIcons(): Promise> { - return invoke("get_notebook_icons"); +export async function getNotebookIcons(vaultPath: string): Promise> { + return invoke("get_notebook_icons", { vaultPath }); } export async function setNotebookIcon( + vaultPath: string, notebookRelative: string, iconRelative: string | null, ): Promise { - return invoke("set_notebook_icon", { notebookRelative, iconRelative }); + return invoke("set_notebook_icon", { vaultPath, notebookRelative, iconRelative }); } -export async function getTagStyles(): Promise> { - return invoke("get_tag_styles"); +export async function getTagStyles(vaultPath: string): Promise> { + return invoke("get_tag_styles", { vaultPath }); } export async function setTagStyle( + vaultPath: string, tag: string, - style: TagStyle | null, -): Promise { - return invoke("set_tag_style", { tag, style }); + change: TagStyleChange, +): Promise { + return invoke("set_tag_style", { vaultPath, tag, change }); +} + +export async function importNotebookIcon( + vaultPath: string, + notebookRelative: string, + name: string, + data: number[], +): Promise { + return invoke("import_notebook_icon", { vaultPath, notebookRelative, name, data }); +} + +export async function importTagIcon( + vaultPath: string, + tag: string, + name: string, + data: number[], +): Promise { + return invoke("import_tag_icon", { vaultPath, tag, name, data }); } export async function setGeneralSettings( @@ -339,16 +374,16 @@ export async function setGeneralSettings( }); } -export async function getQuickAccess(): Promise { - return invoke("get_quick_access"); +export async function getQuickAccess(vaultPath: string): Promise { + return invoke("get_quick_access", { vaultPath }); } -export async function addQuickAccess(noteRelative: string): Promise { - return invoke("add_quick_access", { noteRelative }); +export async function addQuickAccess(vaultPath: string, noteRelative: string): Promise { + return invoke("add_quick_access", { vaultPath, noteRelative }); } -export async function removeQuickAccess(noteRelative: string): Promise { - return invoke("remove_quick_access", { noteRelative }); +export async function removeQuickAccess(vaultPath: string, noteRelative: string): Promise { + return invoke("remove_quick_access", { vaultPath, noteRelative }); } export async function reorderQuickAccess(paths: string[]): Promise { @@ -364,12 +399,12 @@ export interface OrphanAttachment { size: number; } -export async function findOrphanedAttachments(): Promise { - return invoke("find_orphaned_attachments"); +export async function findOrphanedAttachments(vaultPath: string): Promise { + return invoke("find_orphaned_attachments", { vaultPath }); } -export async function trashOrphanedAttachments(names: string[]): Promise { - return invoke("trash_orphaned_attachments", { names }); +export async function trashOrphanedAttachments(vaultPath: string, names: string[]): Promise { + return invoke("trash_orphaned_attachments", { vaultPath, names }); } export async function importObsidian(): Promise { @@ -442,35 +477,38 @@ export async function setBackupSettings( // ── Tasks ── -export async function getTasks(): Promise { - return invoke("get_tasks"); +export async function getTasks(notePath?: string): Promise { + return invoke("get_tasks", { notePath: notePath ?? null }); } export async function setTaskDone( + vaultPath: string, notePath: string, line: number, rawLine: string, done: boolean, ): Promise { - return invoke("set_task_done", { notePath, line, rawLine, done }); + return invoke("set_task_done", { vaultPath, notePath, line, rawLine, done }); } export async function setTaskPriority( + vaultPath: string, notePath: string, line: number, rawLine: string, priority: string | null, ): Promise { - return invoke("set_task_priority", { notePath, line, rawLine, priority }); + return invoke("set_task_priority", { vaultPath, notePath, line, rawLine, priority }); } export async function setTaskDue( + vaultPath: string, notePath: string, line: number, rawLine: string, due: string | null, ): Promise { - return invoke("set_task_due", { notePath, line, rawLine, due }); + return invoke("set_task_due", { vaultPath, notePath, line, rawLine, due }); } // ── Sync (WebDAV) ── @@ -497,22 +535,24 @@ export async function syncNow(): Promise { // ── Version History ── -export async function getNoteVersions(noteId: string): Promise { - return invoke("get_note_versions", { noteId }); +export async function getNoteVersions(vaultPath: string, noteId: string): Promise { + return invoke("get_note_versions", { vaultPath, noteId }); } export async function getNoteVersionContent( + vaultPath: string, noteId: string, timestamp: string, ): Promise { - return invoke("get_note_version_content", { noteId, timestamp }); + return invoke("get_note_version_content", { vaultPath, noteId, timestamp }); } export async function createVersion( + vaultPath: string, path: string, noteId: string, ): Promise { - return invoke("create_version", { path, noteId }); + return invoke("create_version", { vaultPath, path, noteId }); } // ── AI ── diff --git a/src/lib/components/AppLayout.svelte b/src/lib/components/AppLayout.svelte index e750231..24fcbe7 100644 --- a/src/lib/components/AppLayout.svelte +++ b/src/lib/components/AppLayout.svelte @@ -58,19 +58,23 @@ syncState, platformIsMobile, activeVaultConfig, + activateVaultData, } from '$lib/stores/app'; import { keybindings, matchAction } from '$lib/keybindings'; const appWindow = getCurrentWindow(); const isMac = navigator.platform.startsWith('Mac'); const isMobile = $derived($platformIsMobile); - import { loadVaultState, saveVaultState, readNote, deleteNote, createDailyNote, createBackup, getPendingOpenFile, addQuickAccess, removeQuickAccess, getQuickAccess, setTheme, syncNow, getAppConfig, setTaskDone, setTaskPriority, setTaskDue, findOrphanedAttachments, trashOrphanedAttachments } from '$lib/api'; + import { loadVaultState, saveVaultState, readNote, deleteNote, createDailyNote, createBackup, getPendingOpenFile, addQuickAccess, removeQuickAccess, getQuickAccess, setTheme, syncNow, getAppConfig, setTaskDone, setTaskPriority, setTaskDue } from '$lib/api'; import { darkThemes, isAndroid } from '$lib/platform'; - import { debounce } from '$lib/utils/debounce'; import { openNoteWindow } from '$lib/utils/window'; import { normalizeStartupView, resolveStartupTarget } from '$lib/utils/startup-view'; + import { isCurrentVaultEvent, sameNoteTags, updateNoteList } from '$lib/utils/note-list-updates'; + import { beginNoteNavigation, isCurrentNoteNavigation, cancelNoteNavigation } from '$lib/utils/note-navigation'; + import { isPathInDirectory, isTrashedNote } from '$lib/utils/paths'; import { get } from 'svelte/store'; - import type { VaultState, FileEvent, NotebookEntry, TaskItem } from '$lib/types'; + import type { VaultState, FileEvent, NotebookEntry, NoteEntry, TaskItem } from '$lib/types'; + import { completeWindowClose } from '$lib/api'; import type { StartupTarget } from '$lib/utils/startup-view'; function findNotebookByPath(list: NotebookEntry[], relPath: string): NotebookEntry | null { @@ -86,7 +90,75 @@ let noteList = $state(); let editor = $state(); let unlistenFileChange: (() => void) | null = null; + let unlistenClose: (() => void) | null = null; + let fileRefreshTimer: ReturnType | null = null; + const ownerVault = get(appConfig)?.active_vault ?? null; + activateVaultData(ownerVault); + let stateReady = false; + let restoringNote = false; + let disposed = false; + let navigationToken = 0; + let persistTimer: ReturnType | null = null; + let removePopstateListener: (() => void) | null = null; + const backupListeners = new Set<() => void>(); + const recentChanges = new Map(); + let initialRefreshPending = true; + let fileRefreshPending = false; + let refreshLists = false; + let fileRefreshRequest = 0; + + function ownerIsCurrent() { + return !disposed && (get(appConfig)?.active_vault ?? null) === ownerVault; + } + + function beginNavigation() { + return navigationToken = beginNoteNavigation(); + } + + function navigationIsCurrent(token: number) { + return ownerIsCurrent() && isCurrentNoteNavigation(token); + } + + function scheduleFileRefresh(lists: boolean) { + if (!ownerIsCurrent()) return; + fileRefreshPending = true; + refreshLists ||= lists; + // Let the initial reads finish; note updates are replayed by NoteList. + if (initialRefreshPending) return; + const request = ++fileRefreshRequest; + if (fileRefreshTimer) clearTimeout(fileRefreshTimer); + fileRefreshTimer = setTimeout(async () => { + fileRefreshTimer = null; + if (!ownerIsCurrent() || request !== fileRefreshRequest) return; + fileRefreshPending = false; + const lists = refreshLists; + refreshLists = false; + if (lists) { + await Promise.all([sidebar?.refresh(), noteList?.refresh(true)]); + if (!ownerIsCurrent() || request !== fileRefreshRequest) return; + if (isMobile) await sidebar?.refreshTags(); + } else { + await sidebar?.refreshTags(); + } + }, isMobile ? 1000 : 300); + } + + function handleTagsChanged(path: string, nextTags: string[]) { + if (!ownerIsCurrent()) return; + // Record successful saves before patching lists so their delayed watcher + // event does not schedule a second scan. + const previous = recentChanges.get(path) ?? get(notes).find((note) => note.path === path); + if (previous) { + const updated = { ...previous, meta: { ...previous.meta, tags: nextTags } }; + recentChanges.set(path, updated); + if (!sameNoteTags(previous, updated)) scheduleFileRefresh(false); + } else { + scheduleFileRefresh(false); + } + } + async function applyStartupTarget(target: StartupTarget): Promise { + if (!ownerIsCurrent()) return false; if (target.mode === 'notebook') { const vault = $appConfig?.active_vault; const notebook = target.notebookPath === '' && vault @@ -142,19 +214,23 @@ } async function checkScheduledBackup() { + if (!ownerIsCurrent()) return; const config = get(appConfig); if (!config?.backup_enabled) return; const interval = parseFrequencyMs(config.backup_frequency); const last = config.last_backup_time ? new Date(config.last_backup_time).getTime() : 0; if (Date.now() - last >= interval) { const unlisten = await listen('backup-done', (event: any) => { - if (event.payload?.success) { + if (ownerIsCurrent() && event.payload?.success) { const cur = get(appConfig); if (cur) appConfig.set({ ...cur, last_backup_time: new Date().toISOString() }); } unlisten(); + backupListeners.delete(unlisten); }); - try { await createBackup(); } catch (_) { unlisten(); } + if (!ownerIsCurrent()) { unlisten(); return; } + backupListeners.add(unlisten); + try { await createBackup(); } catch (_) { unlisten(); backupListeners.delete(unlisten); } } } @@ -164,6 +240,7 @@ // Auto-sync on a timer (only when configured and an interval is set). async function checkScheduledSync() { + if (!ownerIsCurrent()) return; const vc = activeVaultConfig(get(appConfig)); if (vc?.sync_provider !== 'webdav') return; const mins = vc.sync_interval_minutes ?? 0; @@ -175,6 +252,7 @@ } export async function triggerSyncNow() { + if (!ownerIsCurrent()) return; if (!syncConfigured() || get(syncState).running) return; try { await syncNow(); } catch (_) {} } @@ -185,30 +263,40 @@ if (path) navHistory.push(path); }); - function navigateHistory(direction: -1 | 1) { + async function navigateHistory(direction: -1 | 1) { + const token = beginNavigation(); + if (!navigationIsCurrent(token)) return; + if (editor && !(await editor.flushSave())) return; + if (!navigationIsCurrent(token)) return; const path = navHistory.go(direction); if (!path) return; - readNote(path).then((content) => { - editor?.flushSave(); + try { + const content = await readNote(ownerVault!, path); + if (!navigationIsCurrent(token)) return; + if (editor && !(await editor.flushSave())) return; + if (!navigationIsCurrent(token)) return; $activeNote = content; $activeNotePath = path; $editorDirty = false; editor?.loadNote(path, content.content); - }).catch(() => {}); + } catch (e) { + console.error('Failed to navigate history:', e); + } } - async function handleOpenFile(filePath: string) { + async function handleOpenFile(filePath: string, token = beginNavigation()) { + if (!navigationIsCurrent(token)) return; if (!filePath || !filePath.endsWith('.md')) return; - const config = get(appConfig); - const vaultRoot = config?.active_vault; - const isExternal = !vaultRoot || !filePath.startsWith(vaultRoot + '/'); + const isExternal = !isPathInDirectory(filePath, ownerVault); // External .md → viewer mode (skipped on Android) if (isExternal) { if (isAndroid) return; try { - const content = await readNote(filePath); - editor?.flushSave(); + const content = await readNote(ownerVault!, filePath); + if (!navigationIsCurrent(token)) return; + if (editor && !(await editor.flushSave())) return; + if (!navigationIsCurrent(token)) return; $viewerNote = { path: filePath, content: content.content }; $activeNote = content; $activeNotePath = filePath; @@ -224,8 +312,10 @@ // Vault file: normal flow try { - const content = await readNote(filePath); - editor?.flushSave(); + const content = await readNote(ownerVault!, filePath); + if (!navigationIsCurrent(token)) return; + if (editor && !(await editor.flushSave())) return; + if (!navigationIsCurrent(token)) return; $viewerNote = null; $activeNote = content; $activeNotePath = filePath; @@ -236,7 +326,33 @@ } } - const persistState = debounce(async () => { + async function restoreLastNote(path: string, token: number) { + const canRestore = () => navigationIsCurrent(token) && !$activeNotePath && !$editorDirty && !$viewerNote; + if (!canRestore() || isTrashedNote(path, ownerVault)) return; + const isExternal = !isPathInDirectory(path, ownerVault); + if (isExternal && isAndroid) return; + restoringNote = true; + try { + const content = await readNote(ownerVault!, path); + if (!canRestore()) return; + $activeNote = content; + $activeNotePath = path; + $editorDirty = false; + if (isExternal) { + $viewerNote = { path, content: content.content }; + $readOnly = true; + $focusMode = true; + } + editor?.loadNote(path, content.content); + } catch (_) { + } finally { + restoringNote = false; + if (ownerIsCurrent()) persistState(); + } + } + + async function persistVaultState() { + if (!ownerIsCurrent() || !ownerVault || !stateReady || (restoringNote && !$activeNotePath)) return; const state: VaultState = { last_open_note: $activeNotePath, sidebar_width: $sidebarWidth, @@ -259,9 +375,26 @@ tasks_sort: $tasksSort }; try { - await saveVaultState(state); + await saveVaultState(ownerVault, state); } catch (_) {} - }, 1000); + } + + function persistState() { + if (!ownerIsCurrent() || !stateReady) return; + if (persistTimer) clearTimeout(persistTimer); + persistTimer = setTimeout(() => { + persistTimer = null; + void persistVaultState(); + }, 1000); + } + + async function prepareVaultSwitch(): Promise { + const token = beginNavigation(); + if (editor && !(await editor.flushSave())) return false; + if (!navigationIsCurrent(token)) return false; + await persistVaultState(); + return navigationIsCurrent(token); + } function handleSidebarResize(delta: number) { $sidebarWidth = Math.max(160, Math.min(400, $sidebarWidth + delta)); @@ -275,14 +408,14 @@ function revealNoteList() { $notelistCollapsed = false; - tick().then(() => noteList?.refresh()); + tick().then(() => { if (ownerIsCurrent()) noteList?.refresh(); }); } function toggleNoteList() { const nextCollapsed = !$notelistCollapsed; $notelistCollapsed = nextCollapsed; if (!nextCollapsed) { - tick().then(() => noteList?.refresh()); + tick().then(() => { if (ownerIsCurrent()) noteList?.refresh(); }); } } @@ -290,6 +423,7 @@ let taskNoteOpened = $state(false); function handleNoteSelected(path: string, content: string, task?: TaskItem) { + if (!ownerIsCurrent()) return; // Selecting a real vault note exits viewer mode $viewerNote = null; taskNoteOpened = true; @@ -298,11 +432,17 @@ } async function selectNoteFromSwitcher(path: string): Promise { + const token = beginNavigation(); + if (!navigationIsCurrent(token)) return false; const currentPath = $activeNotePath; if (currentPath && currentPath.replace(/\\/g, '/') === path.replace(/\\/g, '/')) return true; - editor?.flushSave(); + if (editor && !(await editor.flushSave())) return false; + if (!navigationIsCurrent(token)) return false; try { - const content = await readNote(path); + const content = await readNote(ownerVault!, path); + if (!navigationIsCurrent(token)) return false; + if (editor && !(await editor.flushSave())) return false; + if (!navigationIsCurrent(token)) return false; $viewerNote = null; $activeNote = content; $activeNotePath = path; @@ -316,6 +456,8 @@ } function handleViewChanged() { + if (!ownerIsCurrent()) return; + beginNavigation(); taskNoteOpened = false; // Picking a notebook/tag/Tasks/etc. in the sidebar is a request to browse that view's // notes - so if the notes list was hidden, bring it back rather than navigating blindly @@ -328,10 +470,15 @@ if (isMobile) $mobileView = 'notelist'; } - async function createAndFocusNote() { - await noteList?.handleCreateNote(); - editor?.focusTitle(); - if (isMobile) $mobileView = 'editor'; + function createAndFocusNote() { + return noteList?.handleCreateNote(); + } + + async function updateActiveNoteMetadata(patch: Pick): Promise { + if (!ownerIsCurrent() || !$activeNote || !editor) return false; + $activeNote = { ...$activeNote, meta: { ...$activeNote.meta, ...patch } }; + editor.markEdited(); + return editor.forceSave(); } // Toggle a task done from the Tasks view. If the task's note is open in the editor, @@ -339,15 +486,18 @@ async function toggleTask(task: TaskItem) { const done = !task.completed; const isActive = task.note_path === $activeNotePath; - if (isActive) await editor?.forceSave(); + if (isActive && editor && !(await editor.forceSave())) return; + if (!ownerIsCurrent()) return; try { - await setTaskDone(task.note_path, task.line, task.raw_line, done); + await setTaskDone(ownerVault!, task.note_path, task.line, task.raw_line, done); } catch (e) { console.error('Failed to toggle task:', e); } - if (isActive) { + if (isActive && ownerIsCurrent() && $activeNotePath === task.note_path) { + const previous = $activeNote; try { - const content = await readNote(task.note_path); + const content = await readNote(ownerVault!, task.note_path); + if (!ownerIsCurrent() || $activeNotePath !== task.note_path || $activeNote !== previous || $editorDirty) return; $activeNote = content; $editorDirty = false; editor?.loadNote(task.note_path, content.content); @@ -357,15 +507,18 @@ async function changeTaskPriority(task: TaskItem, priority: string | null) { const isActive = task.note_path === $activeNotePath; - if (isActive) await editor?.forceSave(); + if (isActive && editor && !(await editor.forceSave())) return; + if (!ownerIsCurrent()) return; try { - await setTaskPriority(task.note_path, task.line, task.raw_line, priority); + await setTaskPriority(ownerVault!, task.note_path, task.line, task.raw_line, priority); } catch (e) { console.error('Failed to set task priority:', e); } - if (isActive) { + if (isActive && ownerIsCurrent() && $activeNotePath === task.note_path) { + const previous = $activeNote; try { - const content = await readNote(task.note_path); + const content = await readNote(ownerVault!, task.note_path); + if (!ownerIsCurrent() || $activeNotePath !== task.note_path || $activeNote !== previous || $editorDirty) return; $activeNote = content; $editorDirty = false; editor?.loadNote(task.note_path, content.content); @@ -375,15 +528,18 @@ async function changeTaskDue(task: TaskItem, due: string | null) { const isActive = task.note_path === $activeNotePath; - if (isActive) await editor?.forceSave(); + if (isActive && editor && !(await editor.forceSave())) return; + if (!ownerIsCurrent()) return; try { - await setTaskDue(task.note_path, task.line, task.raw_line, due); + await setTaskDue(ownerVault!, task.note_path, task.line, task.raw_line, due); } catch (e) { console.error('Failed to set task due date:', e); } - if (isActive) { + if (isActive && ownerIsCurrent() && $activeNotePath === task.note_path) { + const previous = $activeNote; try { - const content = await readNote(task.note_path); + const content = await readNote(ownerVault!, task.note_path); + if (!ownerIsCurrent() || $activeNotePath !== task.note_path || $activeNote !== previous || $editorDirty) return; $activeNote = content; $editorDirty = false; editor?.loadNote(task.note_path, content.content); @@ -392,10 +548,17 @@ } async function handleDailyNote() { + const token = beginNavigation(); + if (!navigationIsCurrent(token)) return; try { - const entry = await createDailyNote(); - const content = await readNote(entry.path); - editor?.flushSave(); + if (editor && !(await editor.flushSave())) return; + if (!navigationIsCurrent(token)) return; + const entry = await createDailyNote(ownerVault!); + if (!navigationIsCurrent(token)) return; + const content = await readNote(ownerVault!, entry.path); + if (!navigationIsCurrent(token)) return; + if (editor && !(await editor.flushSave())) return; + if (!navigationIsCurrent(token)) return; $activeNote = content; $activeNotePath = entry.path; $editorDirty = false; @@ -440,7 +603,8 @@ } }); - window.addEventListener('popstate', (e) => { + const onPopstate = (e: PopStateEvent) => { + if (!ownerIsCurrent()) return; if (navFromPopstate) { navFromPopstate = false; return; @@ -462,7 +626,9 @@ if (targetDepth === 0) $mobileView = 'sidebar'; else if (targetDepth === 1) $mobileView = 'notelist'; else $mobileView = 'editor'; - }); + }; + window.addEventListener('popstate', onPopstate); + removePopstateListener = () => window.removeEventListener('popstate', onPopstate); } function mobileBack() { @@ -473,7 +639,8 @@ async function trashOpenNote(path: string): Promise { if (path !== $activeNotePath || $viewerNote || $viewMode === 'trash') return false; try { - await deleteNote(path); + await deleteNote(ownerVault!, path); + if (!ownerIsCurrent()) return false; if (Object.hasOwn($noteOrder, path)) { const { [path]: _, ...rest } = $noteOrder; $noteOrder = rest; @@ -484,7 +651,7 @@ $activeNotePath = null; } noteList?.refresh(true).catch((error) => console.error('Failed to refresh notes after trashing:', error)); - if (isMobile) $mobileView = 'notelist'; + if (isMobile && !$activeNotePath) $mobileView = 'notelist'; return true; } catch (error) { console.error('Failed to move open note to Trash:', error); @@ -556,7 +723,7 @@ return; case 'open-new-window': if ($activeNotePath && $activeNote) { - openNoteWindow($activeNotePath, $activeNote.meta.title); + openNoteWindow(ownerVault!, $activeNotePath, $activeNote.meta.title); } return; case 'toggle-sidebar': @@ -638,12 +805,60 @@ }); onMount(async () => { + if (!ownerIsCurrent() || !ownerVault) return; + const startupToken = beginNavigation(); + unlistenClose = await appWindow.onCloseRequested(async (event) => { + // Always prevent Tauri's implicit JS destroy(), which bypasses the tray policy. + event.preventDefault(); + if (!ownerIsCurrent()) return; + try { + if (editor && !(await editor.flushSave())) return; + if (!ownerIsCurrent()) return; + await persistVaultState(); + if (!ownerIsCurrent()) return; + await completeWindowClose(); + } catch (error) { + console.error('Failed to close window:', error); + } + }); + if (!ownerIsCurrent()) { unlistenClose(); return; } + unlistenFileChange = await listen('file-changed', ({ payload }) => { + if (!ownerIsCurrent() || !isCurrentVaultEvent(payload, ownerVault)) return; + if (!payload.note) { + recentChanges.delete(payload.path); + scheduleFileRefresh(true); + return; + } + const note = payload.note; + const visiblePrevious = get(notes).find((entry) => entry.path === note.path); + const listPrevious = noteList?.updateNote(note) ?? visiblePrevious; + const previous = recentChanges.get(note.path) ?? listPrevious; + recentChanges.set(note.path, note); + if (!noteList) { + const key = $viewMode === 'notebook' && $activeNotebook ? `nb:${$activeNotebook.path}` : + $viewMode === 'tag' && $activeTag ? `tag:${$activeTag}` : $viewMode; + const updated = updateNoteList(get(notes), note, key, get(quickAccessPaths)); + if (updated !== get(notes)) notes.set(updated); + } + // Body/modified/preview changes do not change notebook counts or the tag index. + if (!previous) scheduleFileRefresh(true); + else if (!sameNoteTags(previous, note)) scheduleFileRefresh(false); + }); + if (!ownerIsCurrent()) { unlistenFileChange(); return; } + // Listen for file open events (from single-instance or OS file associations) + unlistenOpenFile = await listen('open-file', async (event) => { + if (!ownerIsCurrent()) return; + await handleOpenFile(event.payload); + }); + if (!ownerIsCurrent()) { unlistenOpenFile(); return; } + let lastNotePath: string | null = null; let lastViewMode = ''; let lastNotebook: string | null = null; let lastTag: string | null = null; try { - const state = await loadVaultState(); + const state = await loadVaultState(ownerVault); + if (!ownerIsCurrent()) return; $sidebarWidth = state.sidebar_width; $notelistWidth = state.notelist_width; if (typeof state.outline_width === 'number') $outlineWidth = state.outline_width; @@ -664,39 +879,30 @@ lastNotebook = state.last_notebook ?? null; lastTag = state.last_tag ?? null; } catch (_) {} + if (!ownerIsCurrent()) return; const restoreLastSession = $appConfig?.restore_last_session === true; - // On mobile, prefetch last-opened note so first tap is instant - let prefetchPromise: Promise | null = null; - if (isMobile && restoreLastSession && lastNotePath) { - prefetchPromise = readNote(lastNotePath).catch(() => null); - } + // Display the remembered note independently of the metadata scans. + if (restoreLastSession && lastNotePath) void restoreLastNote(lastNotePath, startupToken); - // Run sidebar and note list refresh in parallel - await Promise.all([sidebar?.refresh(), noteList?.refresh()]); + await Promise.allSettled([sidebar?.refresh(), noteList?.refresh()]); + if (!ownerIsCurrent()) return; - // Full-vault attachment scans cause navigation stalls on mobile storage. - // Mobile users can run the same cleanup explicitly from the Info panel. - if (!isMobile) { - setTimeout(async () => { - if (get(editorDirty)) return; // skip if user is actively editing - try { - const orphans = await findOrphanedAttachments(); - if (orphans.length > 0) { - if (get(editorDirty)) return; // re-check after async scan - const moved = await trashOrphanedAttachments(orphans.map((o) => o.name)); - if (moved > 0) { - const toast = document.createElement('div'); - toast.style.cssText = 'position:fixed;bottom:24px;left:50%;transform:translateX(-50%);background:var(--bg-secondary);color:var(--text-primary);padding:10px 18px;border-radius:8px;box-shadow:0 4px 12px rgba(0,0,0,.15);border:1px solid var(--border-color);font-size:13px;z-index:10000;opacity:0;transition:opacity .3s;pointer-events:none'; - toast.textContent = `Moved ${moved} orphaned attachment${moved > 1 ? 's' : ''} to trash`; - document.body.appendChild(toast); - requestAnimationFrame(() => (toast.style.opacity = '1')); - setTimeout(() => { toast.style.opacity = '0'; setTimeout(() => toast.remove(), 300); }, 4000); - } + // Only the all-notes list is a global tag source on mobile. + if (isMobile) { + if ($viewMode === 'all') { + const tagMap = new Map(); + for (const note of $notes) { + for (const tag of note.meta.tags) { + tagMap.set(tag, (tagMap.get(tag) ?? 0) + 1); } - } catch (_) {} - }, 3000); + } + $tags = Array.from(tagMap.entries()).sort((a, b) => a[0].localeCompare(b[0])); + } else { + await sidebar?.refreshTags(); + if (!ownerIsCurrent()) return; + } } const startupTarget = resolveStartupTarget({ @@ -706,100 +912,54 @@ lastNotebook, lastTag }); - if (!(await applyStartupTarget(startupTarget))) { - await applyStartupTarget({ mode: normalizeStartupView($appConfig?.startup_view) }); - } - - // Reopen the last note only when session restoration is enabled. - if (restoreLastSession && !isMobile && lastNotePath) { - try { - const content = await readNote(lastNotePath); - $activeNote = content; - $activeNotePath = lastNotePath; - $editorDirty = false; - editor?.loadNote(lastNotePath, content.content); - } catch (_) {} - } - - // On mobile, derive tags from the scanned notes (avoids a separate full-scan Rust call) - if (isMobile) { - const tagMap = new Map(); - for (const note of $notes) { - for (const tag of note.meta.tags) { - tagMap.set(tag, (tagMap.get(tag) ?? 0) + 1); - } + if (navigationIsCurrent(startupToken)) { + if (!(await applyStartupTarget(startupTarget)) && navigationIsCurrent(startupToken)) { + await applyStartupTarget({ mode: normalizeStartupView($appConfig?.startup_view) }); } - $tags = Array.from(tagMap.entries()).sort((a, b) => a[0].localeCompare(b[0])); } + if (!ownerIsCurrent()) return; - // On mobile, auto-load the last-opened note so it's ready when the user taps it - if (isMobile && prefetchPromise) { - prefetchPromise.then((noteContent) => { - if (noteContent && lastNotePath && !$activeNotePath) { - $activeNote = noteContent; - $activeNotePath = lastNotePath; - $editorDirty = false; - editor?.loadNote(lastNotePath, noteContent.content); - } - }); - } - - if (isMobile) { - // Mobile: debounce file-changed heavily and skip when actively editing - // (our own saves trigger the watcher, causing expensive refreshes on FUSE) - const debouncedRefresh = debounce(async () => { - if (get(editorDirty)) return; // user is still typing, skip - await Promise.all([sidebar?.refresh(), noteList?.refresh(true)]); - // Re-derive tags from refreshed notes - const tagMap = new Map(); - for (const note of get(notes)) { - for (const tag of note.meta.tags) { - tagMap.set(tag, (tagMap.get(tag) ?? 0) + 1); - } - } - tags.set(Array.from(tagMap.entries()).sort((a, b) => a[0].localeCompare(b[0]))); - }, 10000); - unlistenFileChange = await listen('file-changed', () => { - debouncedRefresh(); - }); - } else { - const debouncedDesktopRefresh = debounce(async () => { - await Promise.all([sidebar?.refresh(), noteList?.refresh(true)]); - }, 300); - unlistenFileChange = await listen('file-changed', () => { - debouncedDesktopRefresh(); - }); - } - - // Listen for file open events (from single-instance or OS file associations) - unlistenOpenFile = await listen('open-file', async (event) => { - await handleOpenFile(event.payload); - }); + initialRefreshPending = false; + if (fileRefreshPending) scheduleFileRefresh(refreshLists); // Check for pending file from first-launch CLI args try { const pending = await getPendingOpenFile(); - if (pending) await handleOpenFile(pending); + if (!ownerIsCurrent()) return; + if (pending && navigationIsCurrent(startupToken)) await handleOpenFile(pending); } catch (_) {} + if (!ownerIsCurrent()) return; // Scheduled backup: check on startup and every 5 minutes checkScheduledBackup(); backupInterval = setInterval(checkScheduledBackup, 5 * 60 * 1000); // ── WebDAV sync: global status + auto-sync triggers ── - unlistenSync.push(await listen('sync-progress', () => syncState.set({ running: true, error: null }))); + let syncConfigRequest = 0; + unlistenSync.push(await listen('sync-progress', () => { + if (ownerIsCurrent()) syncState.set({ running: true, error: null }); + })); + if (!ownerIsCurrent()) { unlistenSync.pop()?.(); return; } unlistenSync.push(await listen('sync-done', async () => { + if (!ownerIsCurrent()) return; + const request = ++syncConfigRequest; syncState.set({ running: false, error: null }); try { - appConfig.set(await getAppConfig()); + const config = await getAppConfig(); + if (ownerIsCurrent() && request === syncConfigRequest && config.active_vault === ownerVault) appConfig.set(config); } catch {} })); - unlistenSync.push(await listen('sync-error', (event: any) => syncState.set({ running: false, error: event.payload?.error ?? 'Sync failed' }))); + if (!ownerIsCurrent()) { unlistenSync.pop()?.(); return; } + unlistenSync.push(await listen('sync-error', (event: any) => { + if (ownerIsCurrent()) syncState.set({ running: false, error: event.payload?.error ?? 'Sync failed' }); + })); + if (!ownerIsCurrent()) { unlistenSync.pop()?.(); return; } // Sync when the vault opens (if enabled) if (syncConfigured() && activeVaultConfig(get(appConfig))?.sync_on_open) { try { await syncNow(); } catch (_) {} } + if (!ownerIsCurrent()) return; // Auto-sync interval: check on startup and every minute checkScheduledSync(); @@ -807,17 +967,28 @@ // Auto-sync on note change: a save flips editorDirty true -> false. Debounce a sync. unsubDirty = editorDirty.subscribe((d) => { + if (!ownerIsCurrent()) return; const vc = activeVaultConfig(get(appConfig)); if (prevDirty && !d && vc?.sync_provider === 'webdav' && vc?.sync_on_change) { if (onChangeSyncTimer) clearTimeout(onChangeSyncTimer); - onChangeSyncTimer = setTimeout(() => { if (!get(syncState).running) syncNow().catch(() => {}); }, 15000); + onChangeSyncTimer = setTimeout(() => { if (ownerIsCurrent() && !get(syncState).running) syncNow().catch(() => {}); }, 15000); } prevDirty = d; }); + stateReady = true; + persistState(); }); onDestroy(() => { + disposed = true; + cancelNoteNavigation(navigationToken); + if (persistTimer) clearTimeout(persistTimer); + removePopstateListener?.(); + backupListeners.forEach((unlisten) => unlisten()); + backupListeners.clear(); unlistenFileChange?.(); + unlistenClose?.(); + if (fileRefreshTimer) clearTimeout(fileRefreshTimer); unlistenOpenFile?.(); if (backupInterval) clearInterval(backupInterval); if (syncInterval) clearInterval(syncInterval); @@ -867,7 +1038,7 @@ {/if}
{#if $mobileView === 'editor'} - - - - + +
@@ -2340,6 +2377,9 @@ {/each} + {#if aiModel && !aiModels.some(model => model.value === aiModel)} +

Saved model: {aiModel}. It remains selected until you choose another model.

+ {/if} {/if} diff --git a/src/lib/components/Sidebar.svelte b/src/lib/components/Sidebar.svelte index ff4e697..d5e18c4 100644 --- a/src/lib/components/Sidebar.svelte +++ b/src/lib/components/Sidebar.svelte @@ -1,5 +1,6 @@ + +
+ + diff --git a/src/lib/components/TagLabel.svelte b/src/lib/components/TagLabel.svelte index ff6cbbc..8340046 100644 --- a/src/lib/components/TagLabel.svelte +++ b/src/lib/components/TagLabel.svelte @@ -40,15 +40,15 @@ style:--tag-color={color} title={`#${name}`} > - {#if builtinIcon} - + + {#if builtinIcon} - - {:else if imageSrc} - - {:else} - # - {/if} + {:else if imageSrc} + + {:else} + # + {/if} + {#if showName}{name}{/if} @@ -74,8 +74,10 @@ } .tag-icon { - display: grid; - place-items: center; + display: flex; + align-items: center; + justify-content: center; + line-height: 1; color: inherit; } diff --git a/src/lib/components/TagStylePicker.svelte b/src/lib/components/TagStylePicker.svelte index 7234289..dcb69dc 100644 --- a/src/lib/components/TagStylePicker.svelte +++ b/src/lib/components/TagStylePicker.svelte @@ -1,11 +1,12 @@ @@ -127,6 +152,7 @@ + {#if error}{/if}
{#each NOTEBOOK_ICON_OPTIONS as option (option.id)} {#each TAG_COLOR_PRESETS as preset (preset)} {/each}