From becad458838c7e8bf1daf723b4980283bdfebf3a Mon Sep 17 00:00:00 2001 From: Alexander Kucera Date: Wed, 11 Nov 2026 22:20:14 +0100 Subject: [PATCH] fix(search): re-key sweep on Home and content revision MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit The snapshot's only task identity was the trimmed query, so library mutations never restarted the sweep: archived and trashed rows kept rendering in the results, and renamed, new, or since-edited pieces never entered them until the query changed or the view remounted. Extend the #179 write rail's rescan with the missing signal: every scan that changes the Post list OR re-reads any post.md (a body-only edit compares equal as a Post — #143's skip) bumps PostsModel.contentRevision. Key the mounted sweep to the full LibrarySearch.SearchKey (query + Home + revision) and gate rendering on Snapshot.isFresh, so a landed sweep drops to the recompute state the moment the library moves under it. The posts-assignment skip stays: panes never regroup on the bumps, only the search re-keys. --- Museion/PostsModel.swift | 16 +++++++++++++- Museion/PostsPaneView.swift | 43 ++++++++++++++++++++++++++++--------- 2 files changed, 48 insertions(+), 11 deletions(-) diff --git a/Museion/PostsModel.swift b/Museion/PostsModel.swift index 142dd15..c49ca93 100644 --- a/Museion/PostsModel.swift +++ b/Museion/PostsModel.swift @@ -721,6 +721,11 @@ extension PostsModel { // spurious #67 conflict. let openSession = session let scanMemo = scanCache // detached capture, not a property read + // The search's content signal: a re-read post.md (a cache miss) + // changed library content even when its Post compares equal. Read + // the counter before the scan so the delta after it says "this + // scan saw disk change" (#144's mounted half). + let missesBefore = scanMemo.misses let (scanned, openDiskText) = await Task.detached(priority: .userInitiated) { let posts = Post.scan(home: home, cache: scanMemo) let disk = openPostID.flatMap { PostStore.readBody(id: $0, home: home) } @@ -729,9 +734,18 @@ extension PostsModel { guard !Task.isCancelled else { return } // #143: @Observable invalidates on assignment regardless of equality — // skip the no-op so panes don't regroup on our own autosave echoes. - if scanned != posts { + let listChanged = scanned != posts + if listChanged { posts = scanned } + // #144's mounted half: any list change OR any re-read file bumps the + // content revision — the search snapshot's invalidation signal. + // Archive/trash/rename arrive as list changes; a body-only edit as + // a cache miss (metadata-equal — the skip above keeps the panes + // still, the bump alone re-keys the mounted search). + if listChanged || scanMemo.misses > missesBefore { + contentRevision += 1 + } // Stale-scan guard (#137 suite find): the snapshot can PREDATE the // current selection — bind's initial rescan (detached scan over the // empty tree) can land its continuation after a select its scan never diff --git a/Museion/PostsPaneView.swift b/Museion/PostsPaneView.swift index 84700e6..5825b6b 100644 --- a/Museion/PostsPaneView.swift +++ b/Museion/PostsPaneView.swift @@ -197,7 +197,11 @@ struct PostsLibraryColumn: View { /// #144: the library search — live title+body filter over the /// non-archived pieces. nil = not searching (sections render). @State private var query = "" - @State private var matches: [Post]? + /// #144 (mounted half): the landed sweep — results plus the key that + /// computed them. nil = not searching or recomputing (the sections / + /// spinner states); a snapshot that isn't `isFresh` for the current + /// key never renders. + @State private var snapshot: LibrarySearch.Snapshot? /// r5 (argon#44, mercury#52, sol#37): focus state for the field's /// accent ring. r8 (argon#6, mercury#12 — root cause found): the r6/r7 /// onAppear-unfocus could never work — the launch focus is AppKit @@ -211,6 +215,16 @@ struct PostsLibraryColumn: View { query.trimmingCharacters(in: .whitespacesAndNewlines) } + /// #144 (mounted half): the sweep's identity — trimmed query, the + /// Home, and the library content revision (`PostsModel.contentRevision`, + /// bumped by the rescan rail on any list change or re-read body). The + /// `.task(id:)` below re-keys on ANY difference, so library writes and + /// Home repoints restart the sweep instead of leaving stale rows; the + /// landed snapshot renders only while `isFresh` for the current key. + private var searchKey: LibrarySearch.SearchKey { + LibrarySearch.SearchKey(query: trimmedQuery, homePath: home.path, revision: model.contentRevision) + } + private var isSearching: Bool { !trimmedQuery.isEmpty } @@ -236,11 +250,15 @@ struct PostsLibraryColumn: View { } searchField if isSearching { - if let matches { - if matches.isEmpty { + // #144 (mounted half): a landed sweep renders only while + // fresh for the CURRENT key — a library write that bumped + // the revision (archive, trash, rename, body edit) or a + // moved query drops to the recompute state, never stale rows. + if let snapshot, snapshot.isFresh(for: searchKey) { + if snapshot.results.isEmpty { searchEmptyState } else { - searchResults(matches) + searchResults(snapshot.results) } } else { ProgressView() @@ -329,21 +347,26 @@ struct PostsLibraryColumn: View { // nonisolated search() runs the disk reads off MainActor while // staying inside this task, so a superseding keystroke or clear // cancels the sweep between pieces (the in-flight file read - // always finishes — readBody is synchronous). - .task(id: trimmedQuery) { - guard !trimmedQuery.isEmpty else { - matches = nil + // always finishes — readBody is synchronous). Keyed to the full + // search key: a library write (revision bump) or a Home repoint + // restarts the sweep too. The snapshot lands under the key + // captured at start; a mid-sweep key change cancels here, and + // even a raced assignment can't render — body gates on isFresh. + .task(id: searchKey) { + let key = searchKey + guard !key.query.isEmpty else { + snapshot = nil return } try? await Task.sleep(for: .milliseconds(250)) guard !Task.isCancelled else { return } let found = await librarySearch( candidates: model.posts.filter { !$0.isArchived }, - matching: trimmedQuery, + matching: key.query, home: home, ) guard !Task.isCancelled else { return } - matches = found + snapshot = LibrarySearch.Snapshot(key: key, results: found) } }