CI / ci (push) Canceled after 0s
Every FSEvents change — each 1 s autosave echo — ran Post.scan over the whole library: read + frontmatter parse + title per post.md, O(library bytes) per save cycle while writing, and the posts array was replaced unconditionally, so every observing pane regrouped and re-rendered even when nothing changed. Post.scan now takes an optional PostScanCache (path+mtime+size-keyed parse memo, nil default — every other caller unchanged): the readdir walk stays the truth, only changed files are read and re-parsed. The stamp is captured BEFORE the read and keys the store, so a mid-scan rewrite can never be pinned as fresh — a late overlapping store keys its own pre-read stamp and the next scan misses (review-caught race, pinned by a same-size-rewrite test). rescan skips the posts assignment when the scan equals the current list; @Observable invalidates on assignment regardless of equality, so panes stop re-rendering on the app's own autosave echoes. Deliberately skipped (measured cold): folder(of:) caching (stat-only walk) and refreshCards throttling (O(open piece), card surfacing depends on it). Closes #143
58 lines
6.2 KiB
Markdown
58 lines
6.2 KiB
Markdown
# #143 — scan parse cache: every save/watcher cycle re-reads the whole library
|
|
|
|
> **Date:** 2026-11-03
|
|
> **Type:** issue
|
|
> **Reference:** https://git.babylondreams.de/AlexKucera/Museion/issues/143 ([museion-OPT-F3], audit `~/argus-audits/audit/2026-11-02/museion-opt.md`)
|
|
|
|
## Goal
|
|
|
|
Kill the full-store rescan: every FSEvents change (each 1 s autosave echo) ran `Post.scan` over EVERY post.md in the library — read + frontmatter parse + title per file, O(library bytes) per save cycle forever while writing — and replaced the `posts` array unconditionally, re-rendering every observing pane even when nothing changed.
|
|
|
|
## What Was Done
|
|
|
|
- `Museion/PostLibrary.swift` — new `PostScanCache` (path+mtime+size-keyed parse memo, NSLock-guarded, `@unchecked Sendable`): `cachedPost(at:stamp:)` consults, `store(_:at:stamp:)` remembers, `stamp(at:)` captures the file's (mtime, size). The stamp is taken BEFORE the read and keys the store.
|
|
- `Post.scan(home:cache:)` — additive `cache` param, nil default (all ~60 existing callers unchanged); hit path reuses the prior parse, miss path reads + parses + stores. Per-file parse extracted to `Post.parse(text:id:project:isArchived:)` (pure function of text + location — the memoized unit; also kept `scan` under the 50-line function-body ceiling).
|
|
- `Museion/PostsModel.swift` — one app-scoped `scanCache` property, hoisted into the detached scan as a local (`scanMemo`); rescan skips the `posts` assignment when the scan equals the current list (`if scanned != posts { posts = scanned }` — @Observable invalidates on assignment regardless of equality).
|
|
- `MuseionTests/PostLibraryTests.swift` — 4 pins: cold scan misses once per file; warm scan all hits with identical output; a changed file re-parses while untouched files hit; a moved piece re-parses at its new path (path-keying); a late store carrying a stale pre-read stamp never pins stale content.
|
|
- DOX: `Museion/AGENTS.md` (PostLibrary + PostsModel entries), `MuseionTests/AGENTS.md` (PostLibraryTests entry); root `AGENTS.md` GitNexus counter auto-refreshed by analyze.
|
|
|
|
## Decisions & Rationale
|
|
|
|
| Decision | Rationale |
|
|
|----------|-----------|
|
|
| mtime+size+path-keyed memo, not FSEvents-path-driven patching | The ticket's own fix direction names the "mtime/offset-keyed cache". The readdir walk stays the truth (zero drift risk — any change moves mtime); only the read is skipped. Path-driven patching risks silent divergence between `posts` and disk. |
|
|
| Stamp captured BEFORE the read, keyed into `store` | Review-found race: a store that stats the file itself can key a pre-rewrite parse as the post-rewrite version when two rescans overlap (rescan re-enters at its `await`), pinning stale content until the next write. The pre-read stamp makes a mid-flight rewrite mismatch on the next scan — a conservative miss, self-healed. Pinned. |
|
|
| `folder(of:)` caching SKIPPED | Stat-only walks (readdir + N `fileExists`), microseconds at human project counts; the pre-existing ponytail note at `PostStore.swift:41-43` says exactly this. Caching it risks stale id→folder resolution across folder moves — a correctness trade for no measurable win. |
|
|
| `refreshCards` throttling SKIPPED | O(open piece), not O(library) — not the ticket's cost. Correctness-load-bearing: cards surface via the watcher-driven rescan tail (#23 path). |
|
|
| Cache entry linger (no pruning of vanished paths) | Entries for vanished paths are never consulted again (the walk doesn't visit them); bytes each. Ponytail-marked in the class doc. |
|
|
| `hits`/`misses` counters as plain internal observables | The honest test seam for "no re-read" (a black-box observable would need mtime-surgery fixtures); cheap and lock-guarded. |
|
|
|
|
## Gotchas & Fixes
|
|
|
|
| Problem | Root Cause | Fix |
|
|
|---------|------------|-----|
|
|
| Stat-at-store race (spec review finding) | `store` originally stat'ed the file after the read; overlapping detached rescans could store a pre-rewrite parse keyed as post-rewrite | Pre-read `Stamp` captured in `scan` before the consult, passed to consult AND store; pinned by `a late store carrying a stale stamp never pins stale content` (same-size rewrite — mtime axis alone) |
|
|
| Double `fstat` per file on miss paths (audit, 2 reviewers converged) | `cachedPost` stat'ed internally, `scan` stat'ed again for the store stamp | One stamp in `scan` serves both; `cachedPost(at:stamp:)` does no I/O (also closes the "I/O under the lock" nit) |
|
|
| swiftformat stripped a load-bearing `self.` | `let scanCache = self.scanCache` — self-remove rule made it self-referential | Renamed the local to `scanMemo` (no shadowing, nothing for the formatter to strip) — don't fight the formatter (#142 learning) |
|
|
| swiftformat fuzzy patch mangled a region twice | Loose `old_string` boundaries under wrapped-condition reflow | Re-read the region, restored, then anchored every later patch on exact current text |
|
|
| `PostScanCache`'s `// ponytail:` note between doc block and class tripped `orphaned_doc_comment` | A `//` block after `///` detaches the doc comment | Folded into the `///` block |
|
|
| `debounce flushes after idle` flaked in 2 of 3 full-suite runs | Known load-flake class (#30): the 5 s poll deadline starves under suite load; test shares no code path with the cache | Verified green in isolation (0.131 s) each time; not a regression |
|
|
|
|
## Files Changed
|
|
|
|
| File | Change Summary |
|
|
|------|---------------|
|
|
| `Museion/PostLibrary.swift` | `PostScanCache` + `scan(home:cache:)` + `parse(text:...)` helper |
|
|
| `Museion/PostsModel.swift` | app-scoped cache threaded through rescan; equality-gated `posts` assignment |
|
|
| `MuseionTests/PostLibraryTests.swift` | 4 cache pins |
|
|
| `Museion/AGENTS.md`, `MuseionTests/AGENTS.md`, `AGENTS.md` | DOX pass + GitNexus counter |
|
|
|
|
## Open Items & Next Steps
|
|
|
|
- [ ] PostsModel.swift lint debt: over `file_length` (1127/1000) and `type_body_length` (361/350) at HEAD already; this change adds +8 net lines. House-pattern fix = move the file-scope extensions (pass orchestration / blind read / cards / promote) to their own file. Say the word.
|
|
- [ ] Known ceilings (ponytail-marked in `PostScanCache`): same-size rewrite inside APFS timestamp granularity could serve a stale parse (upgrade: content hash); vanished-path entries linger (upgrade: prune to walked paths).
|
|
|
|
---
|
|
|
|
*Log written by write-log skill*
|