feat(library): projects & archive substrate — S1 (#107)
CI / ci (push) Canceled after 0s

Pieces re-root from posts/<id>/ to projects/<name>/<id>/ with an
archive/ twin root (ADR-0012): scan walks both levels, New Piece and
Import land in the selected project else the default posts, the slug
namespace is global across projects and archive (an archived slug is
never reused), and every piece carries type: post frontmatter —
stamped by New/Import, read leniently (absent or unknown reads as
post), the file stays post.md for every content type. Write paths
resolve the folder from the id alone (folder(of:)); sessions and
sidecars never thread (project, id).

Finish-audit hardening: duplicate ids on disk (Finder copy) dedupe in
scan to the same first-match folder(of:) writes to; folder(of:) skips
hidden/symlinked projects like scan does.

Dev posts moved by hand to projects/posts/ — no migration machinery,
the app never reads the old posts/ root (#103). Full suite green
(128 unit + UI), swiftlint clean on changed files.

Closes #107
This commit is contained in:
AlexKucera committed 2026-10-13 17:44:44 +02:00
1 parent 50eecc6dcb
commit 3ca9d4e601
17 files changed
+608 -197

No files matched your search

+71 -26
View File
@@ -3,13 +3,18 @@
// Museion
//
// #17 (S1.1): the Posts list mirrors what is on disk under the Museion Home.
// #107 (ADR-0012): pieces live under projects/<name>/<id>/ and
// archive/<name>/<id>/ — both roots are scanned.
//
import Foundation
/// One Post on disk: `<home>/posts/<id>/post.md` (ADR-0004 shape).
/// One Post on disk: `<home>/{projects|archive}/<project>/<id>/post.md`
/// (ADR-0012 shape; the folder name under either root is the project).
nonisolated struct Post: Identifiable, Equatable {
/// The post folder's name — the Post's durable identity (CONTEXT.md).
/// Unique across projects AND archive: the slug namespace is global
/// (ADR-0012), so the id alone resolves the folder.
let id: String
/// The Post's Title: first non-empty line after any frontmatter, heading markers stripped.
var title: String
@@ -17,6 +22,19 @@ nonisolated struct Post: Identifiable, Equatable {
var passState: PassState
/// Origin from frontmatter; writer when absent. Immutable once set (CONTEXT.md).
var origin: Origin
/// Content Type from frontmatter `type` (#107); post when absent.
var type: ContentType
/// The containing folder's name under projects/ or archive/ (#107).
var project: String
/// True when the piece lives under archive/ (#107).
var isArchived: Bool
/// New pieces land here when no project is selected (#107).
static let defaultProject = "posts"
/// The two roots that hold pieces, in resolution order (#107): projects/
/// is searched before archive/.
static let containerRoots = ["projects", "archive"]
nonisolated enum PassState: String, Equatable, CaseIterable {
case firstDraft = "First Draft"
@@ -31,33 +49,60 @@ nonisolated struct Post: Identifiable, Equatable {
case ai
}
/// Reads every `posts/<id>/post.md` into Posts (sorted by title, Finder order).
/// Read-only for #17 — create/rename/delete round-trips are #18. Symlinks and
/// hidden dirs are skipped; folders without `post.md` (lazy) aren't Posts;
/// a malformed folder can't crash the list.
static func scan(home: URL) -> [Post] {
let postsURL = home.appendingPathComponent("posts", isDirectory: true)
guard let entries = try? FileManager.default.contentsOfDirectory(
at: postsURL,
includingPropertiesForKeys: [.isDirectoryKey, .isSymbolicLinkKey],
) else { return [] }
nonisolated enum ContentType: String {
case post
case outline
}
/// Reads every `<root>/<project>/<id>/post.md` (roots: projects/, then
/// archive/ — ADR-0012) into Posts (sorted by title, Finder order).
/// Same skip rules at both levels: symlinks, hidden dirs, loose files;
/// folders without `post.md` (lazy) aren't Posts; a malformed folder
/// can't crash the list. Read-only for #17.
static func scan(home: URL) -> [Post] {
var posts: [Post] = []
for dir in entries {
let values = try? dir.resourceValues(forKeys: [.isDirectoryKey, .isSymbolicLinkKey])
guard values?.isDirectory ?? false, !(values?.isSymbolicLink ?? false),
!dir.lastPathComponent.hasPrefix(".") else { continue }
let fileURL = dir.appendingPathComponent("post.md")
guard let text = try? String(contentsOf: fileURL, encoding: .utf8) else { continue }
let frontmatter = frontmatter(of: text)
posts.append(
Post(
id: dir.lastPathComponent,
title: title(of: text),
passState: PassState(rawValue: frontmatter["pass_state"] ?? "") ?? .firstDraft,
origin: Origin(rawValue: frontmatter["origin"] ?? "") ?? .writer,
),
)
// #107 audit: duplicate ids can appear on disk (Finder copy across
// projects, partial restore) — first-wins dedupe, in the SAME
// projects-before-archive, name-sorted order folder(of:) resolves
// by, so the row shown is always the piece written. Later twins are
// invisible until renamed in Finder.
var seen: Set<String> = []
for root in containerRoots {
let isArchived = root == "archive"
let rootURL = home.appendingPathComponent(root, isDirectory: true)
guard let projects = try? FileManager.default.contentsOfDirectory(
at: rootURL,
includingPropertiesForKeys: [.isDirectoryKey, .isSymbolicLinkKey],
) else { continue }
for projectDir in projects.sorted(by: { $0.lastPathComponent < $1.lastPathComponent }) {
let values = try? projectDir.resourceValues(forKeys: [.isDirectoryKey, .isSymbolicLinkKey])
guard values?.isDirectory ?? false, !(values?.isSymbolicLink ?? false),
!projectDir.lastPathComponent.hasPrefix(".") else { continue }
guard let entries = try? FileManager.default.contentsOfDirectory(
at: projectDir,
includingPropertiesForKeys: [.isDirectoryKey, .isSymbolicLinkKey],
) else { continue }
for dir in entries.sorted(by: { $0.lastPathComponent < $1.lastPathComponent }) {
let pieceValues = try? dir.resourceValues(forKeys: [.isDirectoryKey, .isSymbolicLinkKey])
guard pieceValues?.isDirectory ?? false, !(pieceValues?.isSymbolicLink ?? false),
!dir.lastPathComponent.hasPrefix(".") else { continue }
let fileURL = dir.appendingPathComponent("post.md")
guard let text = try? String(contentsOf: fileURL, encoding: .utf8) else { continue }
guard seen.insert(dir.lastPathComponent).inserted else { continue }
let frontmatter = frontmatter(of: text)
posts.append(
Post(
id: dir.lastPathComponent,
title: title(of: text),
passState: PassState(rawValue: frontmatter["pass_state"] ?? "") ?? .firstDraft,
origin: Origin(rawValue: frontmatter["origin"] ?? "") ?? .writer,
type: ContentType(rawValue: frontmatter["type"] ?? "") ?? .post,
project: projectDir.lastPathComponent,
isArchived: isArchived,
),
)
}
}
}
return posts.sorted { $0.title.localizedStandardCompare($1.title) == .orderedAscending }
}