# Build S1: storage substrate — projects/ + archive/, type frontmatter (#107) > **Date:** 2026-10-13 > **Type:** issue > **Reference:** https://git.babylondreams.de/AlexKucera/museion/issues/107 (map: #97, ADR-0012) ## Goal Re-root the piece storage substrate per #107 / ADR-0012: `posts//` → `projects///` + `archive///` (two-level scan), global slug namespace across both roots, `type: post` frontmatter stamped by New/Import and read leniently, every PostStore write path deriving its folder from the id alone. No UI change beyond what the paths force. ## What Was Done - `PostLibrary.swift` — `Post` gains `type` (`ContentType` post|outline), `project`, `isArchived`; `Post.scan` walks both roots (`projects`, `archive`) with the same skip guards at both levels; `type` read leniently (absent/unknown → post); audit fix: first-wins dedupe by id in the same deterministic order `folder(of:)` resolves (projects-before-archive, name-sorted). - `PostStore.swift` — new `folder(of:home:)` id→folder resolver (every write path derives its target from the id; nothing threads `(project, id)`); `availableSlug(for:home:)` over the global namespace (all entries under `projects/*/` + `archive/*/`; archived slug never reused); `writeNew(body:project:home:)` + `importFile(at:origin:project:home:)` land under the selected project else default `posts`; `stamped`/`mergedFrontmatter` carry `type: post` as the third our-key (first in the block, order pinned by tests); `overwrite`/`readBody`/cursor sidecar/`rename`/`trash` resolve via `folder(of:)`; audit fix: `folder(of:)` project loop applies scan's skip guards (dot-dirs, symlinks) so a hidden project can't shadow a visible piece; `mergedFrontmatter` key-rewrite folded into one ordered-pairs loop (complexity 11 → lint-clean). - `PostsModel.swift` — `PostDraft` carries `project`; shared `targetProject` (selected non-archived post's project, else default) feeds `newPost()` + `importFiles`; watcher roots → `projects/` + `archive/` (both created by `bind`); `PostsWatcher.start(urls:)` takes an array. - `EditorSession.swift` — `folderURL` is a stored property resolved once at init via `PostStore.folder(of:)` (fallback `projects/posts/` for a vanished piece), + test pinning the fallback. - `PostEditorView.swift` / `PostsPaneView.swift` / `SettingsPaneView.swift` — breadcrumb renders the real ` / / / post.md` path; empty-state + repoint copy updated. - Tests (4 files) — all fixtures re-rooted to `projects/posts//`; new cases: two-level scan across projects+archive, lenient `type`, archived-slug-never-reused, `writeNew(project:)` routing, rename-within-project + archive-stays-archived, trash-from-archive, external `type: outline` overwritten at import, unresolvable-id fallback, duplicate-id dedupe, hidden-project shadowing. - Manual step done: the 4 dev posts moved by hand to `~/Documents/Museion/projects/posts//`, old `posts/` root removed (no migration machinery — the app never reads it, #103). - DOX: Museion/AGENTS.md + MuseionTests/AGENTS.md truth-passed for the new layout. ## Decisions & Rationale | Decision | Rationale | |----------|-----------| | Write paths resolve folder from id (`folder(of:)`), never thread (project, id) | ADR-0012: Piece ID is the durable identity; keeps sessions/sidecars/callers unchanged | | Duplicate-id dedupe in `scan`, first-wins in `folder(of:)`'s exact order | Audit (security + general converged): a Finder copy across projects made every id-addressed write land on the sorted-first twin — silent wrong-piece edits/trash. One precedence, shared by row and write target | | `folder(of:)` skips dot-dirs/symlinks like `scan` | Same audit: `projects/.trash/` sorted before visible projects and captured all writes | | `type: post` stamped/normalized on save (unknown values → post) | Same our-keys-win rule #59 pinned for pass_state/origin; lenient-read + normalize-on-save | | Import lands in the selected project (not just New) | Consistency: both are "new piece lands somewhere"; one `targetProject` helper | | Deferred: per-piece write-refusal on ambiguous ids | Dedupe removes the wrong-write defect for S1; refusal UX belongs to S2's in-app moves slice | ## Gotchas & Fixes | Problem | Root Cause | Fix | |---------|------------|-----| | One `xcodebuild test` run printed `** TEST FAILED **` mid-verification | Every xcresult bundle in the window records 0 failing tests — runner-level flake (two parallel suite runs) | Re-ran; green ×3 since, incl. final full suite | | `swiftformat .` reformatted spike/, prototypes/, 3 unrelated app files | Ran tree-wide instead of scoped | `git checkout --` the strays; scoped format after (happened twice — child did it too) | | `folder(of:)` compile error: URL vs String | Converted listing to URL-based but still appended `project` directly | `project.lastPathComponent` | | Dedupe guard placed before the post.md read | A lazy folder would consume the id before its readable twin | Moved `seen.insert` after the read | ## Files Changed | File | Change Summary | |------|---------------| | `Museion/PostLibrary.swift` | two-level scan, ContentType/project/isArchived, duplicate-id dedupe | | `Museion/PostStore.swift` | folder(of:) resolver, global availableSlug, project-aware write paths, type stamping | | `Museion/PostsModel.swift` | PostDraft.project, targetProject, watcher on both roots | | `Museion/EditorSession.swift` | stored folderURL resolved at init | | `Museion/PostEditorView.swift` | real-path breadcrumb | | `Museion/PostsPaneView.swift` | header + empty-state copy | | `Museion/SettingsPaneView.swift` | repoint copy | | `MuseionTests/{PostLibrary,PostStore,EditorSession,ImportExternal}Tests.swift` | re-rooted fixtures + 10 new cases | | `Museion/AGENTS.md`, `MuseionTests/AGENTS.md`, `AGENTS.md` | DOX truth-pass + log window | ## Open Items & Next Steps - [ ] S2 of map #97: library UI (variant B project sections, Archive nav-rail) + in-app project commands — the substrate carries `project`/`isArchived` for it - [ ] Flag on #107 when closing: "selected project" reads as "selected non-archived post, else default" in the implementation - [ ] Per-piece duplicate-id write-refusal (or in-app resolution UX) if S2's moves make duplicates reachable from the app - [ ] Ceiling comments name the O(projects) walks (`folder(of:)`, `availableSlug`) — cache only if they ever measure hot --- *Log written by write-log skill*