fix(search): re-key sweep on Home and content revision
CI / ci (push) Canceled after 0s

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.
This commit is contained in:
AlexKucera committed 2026-11-11 22:20:14 +01:00
1 parent c0cd60b798
commit becad45883
2 files changed
+48 -11

No files matched your search

+15 -1
View File
@@ -721,6 +721,11 @@ extension PostsModel {
// spurious #67 conflict. // spurious #67 conflict.
let openSession = session let openSession = session
let scanMemo = scanCache // detached capture, not a property read 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 (scanned, openDiskText) = await Task.detached(priority: .userInitiated) {
let posts = Post.scan(home: home, cache: scanMemo) let posts = Post.scan(home: home, cache: scanMemo)
let disk = openPostID.flatMap { PostStore.readBody(id: $0, home: home) } let disk = openPostID.flatMap { PostStore.readBody(id: $0, home: home) }
@@ -729,9 +734,18 @@ extension PostsModel {
guard !Task.isCancelled else { return } guard !Task.isCancelled else { return }
// #143: @Observable invalidates on assignment regardless of equality — // #143: @Observable invalidates on assignment regardless of equality —
// skip the no-op so panes don't regroup on our own autosave echoes. // 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 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 // Stale-scan guard (#137 suite find): the snapshot can PREDATE the
// current selection — bind's initial rescan (detached scan over the // current selection — bind's initial rescan (detached scan over the
// empty tree) can land its continuation after a select its scan never // empty tree) can land its continuation after a select its scan never
+33 -10
View File
@@ -197,7 +197,11 @@ struct PostsLibraryColumn: View {
/// #144: the library search — live title+body filter over the /// #144: the library search — live title+body filter over the
/// non-archived pieces. nil = not searching (sections render). /// non-archived pieces. nil = not searching (sections render).
@State private var query = "" @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 /// 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 /// accent ring. r8 (argon#6, mercury#12 — root cause found): the r6/r7
/// onAppear-unfocus could never work — the launch focus is AppKit /// onAppear-unfocus could never work — the launch focus is AppKit
@@ -211,6 +215,16 @@ struct PostsLibraryColumn: View {
query.trimmingCharacters(in: .whitespacesAndNewlines) 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 { private var isSearching: Bool {
!trimmedQuery.isEmpty !trimmedQuery.isEmpty
} }
@@ -236,11 +250,15 @@ struct PostsLibraryColumn: View {
} }
searchField searchField
if isSearching { if isSearching {
if let matches { // #144 (mounted half): a landed sweep renders only while
if matches.isEmpty { // 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 searchEmptyState
} else { } else {
searchResults(matches) searchResults(snapshot.results)
} }
} else { } else {
ProgressView() ProgressView()
@@ -329,21 +347,26 @@ struct PostsLibraryColumn: View {
// nonisolated search() runs the disk reads off MainActor while // nonisolated search() runs the disk reads off MainActor while
// staying inside this task, so a superseding keystroke or clear // staying inside this task, so a superseding keystroke or clear
// cancels the sweep between pieces (the in-flight file read // cancels the sweep between pieces (the in-flight file read
// always finishes — readBody is synchronous). // always finishes — readBody is synchronous). Keyed to the full
.task(id: trimmedQuery) { // search key: a library write (revision bump) or a Home repoint
guard !trimmedQuery.isEmpty else { // restarts the sweep too. The snapshot lands under the key
matches = nil // 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 return
} }
try? await Task.sleep(for: .milliseconds(250)) try? await Task.sleep(for: .milliseconds(250))
guard !Task.isCancelled else { return } guard !Task.isCancelled else { return }
let found = await librarySearch( let found = await librarySearch(
candidates: model.posts.filter { !$0.isArchived }, candidates: model.posts.filter { !$0.isArchived },
matching: trimmedQuery, matching: key.query,
home: home, home: home,
) )
guard !Task.isCancelled else { return } guard !Task.isCancelled else { return }
matches = found snapshot = LibrarySearch.Snapshot(key: key, results: found)
} }
} }