Files
museion/docs/devlog/20261103-issue144-library-search-across-titles-and-bodies_log.md
T
AlexKucera 53c314d4e1
CI / ci (push) Canceled after 0s
feat(library): search by title and body (#144)
The library offered only project/pass-state browsing — no way to find a
piece by content ("which piece was it that talked about X", FEAT-004).
The library column gains a plain-text search field under the Posts
header: a 250 ms-debounced title+body filter over the non-archived
pieces, flat results with title matches first (both halves keep the
scan's Finder order), a mono match count, and the full row context menu
(Rename/Convert/Move/Archive/Trash) on result rows — extracted into one
postRow helper shared with the sections. Body matches go through
PostStore.readBody, so frontmatter never matches (machinery, not
prose). Clearing returns the project sections; state is view-local, so
both render sites (unfocused layout + focus-mode reveal) get the field.

The scan runs as a nonisolated static async helper awaited from
.task(id: trimmedQuery): off MainActor (a nonisolated async function
doesn't inherit the caller's actor) while staying INSIDE the structured
task, so a superseding keystroke or clear actually cancels the sweep
between pieces — the spec's Task.detached shape never received
cancellation (two-reviewer convergence in the finish-audit caught it,
plus the code comment claiming it did), and trimming the task id stops
whitespace-only edits from re-running identical scans.

Deliberately deferred: re-filter on a watcher rescan while a query sits
unchanged (any keystroke refreshes), and a PostScanCache-style body
memo (full-body re-read per sweep — same unbounded-read ceiling as
Post.scan; upgrade path noted in the helper's ponytail comment).

Known pre-existing (unchanged by this diff, proven by stash control):
repo-wide swiftlint exits 2 with 289 violations / 72 serious at HEAD;
this diff adds zero error-severity violations.

Suite 574/574 (xcresult executed count).

Closes #144
2026-11-03 12:04:09 +01:00

58 lines
5.8 KiB
Markdown
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
# Library search across titles and bodies (#144)
> **Date:** 2026-11-03
> **Type:** issue
> **Reference:** [museion-#144](https://git.babylondreams.de/AlexKucera/museion/issues/144) — [museion-FEAT-004] Library search across titles and bodies
## Goal
Ship FEAT-004 (Argus cycle-1 smallest-win, effort S): search across post titles and bodies over the whole library. Fix direction pinned in the ticket: in-memory title filter + body scan scoped to the library roots, debounce, no new persistence. Full spec (UI sketch, code, constraints) in `~/argus-audits/audit/2026-11-02/museion-features.md` §FEAT-004.
## What Was Done
- `PostsLibraryColumn` (Museion/PostsPaneView.swift) gains the search: `query`/`matches` view state below `collapsed`, `isSearching`/`trimmedQuery` computed, the plain-text `searchField` row under the Posts header (magnifyingglass + clear ✕, bottom hairline), `searchResults(_:)` (flat list, mono count line, title matches first) and `searchEmptyState`.
- The scan: `.task(id: trimmedQuery)` → 250 ms debounce → `await Self.search(...)` — a `nonisolated static func` running the disk reads off MainActor but *inside* the structured task, with a per-piece `Task.isCancelled` check.
- `postRow(_:)` extracted (`LibraryPostRow` + full context menu) — sections and search results render the same row; results carry Rename/Convert/Move/Archive/Trash too.
- DOX closeout: `Museion/AGENTS.md` PostsPaneView entry gains the `**#144:**` line (field, debounce, off-main posture, frontmatter exclusion, ordering, both render surfaces).
- Verification: build SUCCEEDED; suite 574/574 (xcresult executed count); swiftformat idempotent; swiftlint adds zero error-severity violations.
## Decisions & Rationale
| Decision | Rationale |
|----------|-----------|
| `nonisolated static` async helper instead of the spec's `Task.detached` | Finish-audit converged finding (security + general): `Task.detached` is unstructured — `.task(id:)` cancellation never reaches it, and the spec's comment claimed it did. A nonisolated async func awaited from the `.task` runs off MainActor (doesn't inherit the actor) while staying on the same *task*, so SwiftUI's cancellation actually lands; per-piece check stops superseded sweeps between files. Single pass also killed the double title-predicate. |
| `.task(id: trimmedQuery)` not `query` | Whitespace-only edits ("foo"→"foo ") must not restart the debounce+scan for identical trimmed input; the id now agrees with the render gate's source of truth. |
| `postRow(_:)` extraction | Search results lacked the context menu (Rename/Move/etc. unreachable from results); one row+menu source for both sites beats duplicating the menu block. |
| Stale-while-revalidate during debounce (`matches` nils only on clear) | Spec's exact code: later keystrokes keep showing prior results through the 250 ms window instead of a spinner flicker; ProgressView is the first-search state. |
| No `readBody` size gate | `Post.scan` already reads every post.md unbounded at startup — search adds no new exposure class; a gate would change shared-function behavior beyond this diff. Ceiling documented as a `ponytail:` note with the `PostScanCache`-style memo upgrade path (LearningsIndex posture). |
| No unit test for the filter | View-local state, no pure logic added — the spec's verification is manual smoke; the repo has no component-render harness (named unpinned delta in the audit report). |
| Two `PostsLibraryColumn` instances (unfocused layout + focus reveal) keep separate query state | Matches #108's documented per-view `collapsed` posture; only one is alive at a time; AGENTS.md row promises availability, not shared state. Reviewer confirmed fine. |
## Gotchas & Fixes
| Problem | Root Cause | Fix |
|---------|------------|-----|
| swiftlint `opening_brace` violation after formatting | swiftformat pushed a wrapped `else if` condition's brace to its own line (#142's known trap) | Hoisted the body read into a local (`let body = …`) so the condition never wraps — don't fight the formatter |
| Test-run exit 1 with all tests passing | My xcresult-summary one-liner ran while the bundle was still finalizing (#33's race, live again — a piped wait isn't enough) | Read the bundle in a separate later call: 574/574, 0 failed |
| swiftlint gate exits 2 repo-wide | Pre-existing: 289 violations / 72 serious at HEAD, identical with the diff; earlier chained runs piped through `tail` masked the exit code | Proven pre-existing via stash control (same 289/72); named here and in the commit; my file adds zero errors |
| GitNexus `detect-changes` said "No changes detected" despite a dirty tree | Stale index after an earlier incremental analyze | Re-ran `analyze` (full, 5,820 nodes) → detect-changes then reported the true scope (`PostsLibraryColumn` + docs) |
| Review flag: comment claimed `.task(id:)` cancels the in-flight scan | `Task.detached` never receives parent cancellation — the prose promised an invariant the code didn't have | Structured-concurrency fix above; the comment is now true |
## Files Changed
| File | Change Summary |
|------|---------------|
| `Museion/PostsPaneView.swift` | +164/−23: search state, field, results, empty state, structured scan, `postRow(_:)` extraction |
| `Museion/AGENTS.md` | `**#144:**` line on the PostsPaneView entry (DOX closeout) |
| `AGENTS.md` | write-log window refresh + gitnexus index-stats churn |
## Open Items & Next Steps
- [ ] Deferred: re-filter when a watcher rescan lands while a query sits unchanged (any keystroke refreshes; a later slice if it bites)
- [ ] Deferred: `folder(of:)` re-walk per piece per sweep — `PostScanCache`-style body memo if it ever measures hot (named in `search()`'s ponytail note; see the `local-store-caching` skill)
- [ ] Manual smoke: type a known word into Search posts → flat match list; clear → sections return; right-click a result → full menu
---
*Log written by write-log skill*