refactor(workspace): finish-audit fixes — off-main scan, parser edges, dead code out

Reviewers' findings on 804b588: scan moved off MainActor (Task.detached),
PostLibrary struct folded into Post.scan, hidden dirs and symlinked dirs
now actually skipped (test de-vacuated), unterminated frontmatter no longer
leaks as the Title, quoted YAML values unquoted, leading blank line before
--- tolerated, #-only body reads untitled, persisted Home validated
isFileURL. Dead code deleted: radiusLarge, editorMinWidth, homeKey,
PaneHeader trailing slot, Settings ScrollView, PostRow fill dance.
Tests 56/56, lint 0.

[Generated by AI]
This commit is contained in:
AlexKucera committed 2026-10-01 13:43:25 +02:00
1 parent 804b5889f6
commit 765d32d4d4
10 files changed
+158 -101

No files matched your search

+29 -34
View File
@@ -8,12 +8,10 @@
import Foundation
/// One Post on disk: `<home>/posts/<id>/post.md` (ADR-0004 shape).
/// A Post directory with no post.md yet is skipped — folders are created lazily
/// on first write, so an empty folder is not a Post.
nonisolated struct Post: Identifiable, Equatable {
/// The post folder's name — the Post's durable identity (CONTEXT.md).
let id: String
/// The Post's Title: first non-empty line, heading markers stripped.
/// The Post's Title: first non-empty line after any frontmatter, heading markers stripped.
var title: String
/// Pass State from frontmatter; First Draft when absent (explicit, never inferred).
var passState: PassState
@@ -32,76 +30,73 @@ nonisolated struct Post: Identifiable, Equatable {
// swiftlint:disable:next identifier_name
case ai
}
}
/// Scans `<home>/posts/` and parses each Post's frontmatter leniently.
/// Read-only for #17 — create/rename/delete round-trips are #18.
/// Nonisolated: a pure synchronous disk scan, safe from any executor.
nonisolated struct PostLibrary: Equatable {
private(set) var posts: [Post] = []
/// Reads every `posts/<id>/post.md`. Frontmatter keys (`pass_state:`, `origin:`)
/// are read leniently — corpus posts ship bare, so absent or unknown values fall
/// back to First Draft / writer. Keys outside a frontmatter block are ignored;
/// 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) -> PostLibrary {
static func scan(home: URL) -> [Post] {
let postsURL = home.appendingPathComponent("posts", isDirectory: true)
guard let entries = try? FileManager.default.contentsOfDirectory(
at: postsURL,
includingPropertiesForKeys: [.isDirectoryKey],
) else { return PostLibrary() }
includingPropertiesForKeys: [.isDirectoryKey, .isSymbolicLinkKey],
) else { return [] }
var posts: [Post] = []
for dir in entries where (try? dir.resourceValues(forKeys: [.isDirectoryKey]))?.isDirectory == true {
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 = Self.frontmatter(of: text)
let frontmatter = frontmatter(of: text)
posts.append(
Post(
id: dir.lastPathComponent,
title: Self.title(of: text),
passState: Post.PassState(rawValue: frontmatter["pass_state"] ?? "") ?? .firstDraft,
origin: Post.Origin(rawValue: frontmatter["origin"] ?? "") ?? .writer,
title: title(of: text),
passState: PassState(rawValue: frontmatter["pass_state"] ?? "") ?? .firstDraft,
origin: Origin(rawValue: frontmatter["origin"] ?? "") ?? .writer,
),
)
}
return PostLibrary(posts: posts.sorted { $0.title.localizedStandardCompare($1.title) == .orderedAscending })
return posts.sorted { $0.title.localizedStandardCompare($1.title) == .orderedAscending }
}
/// The `---`-delimited frontmatter block if present; empty otherwise.
/// Quoted scalar values (`origin: "ai"`) are unquoted — lenient YAML.
static func frontmatter(of text: String) -> [String: String] {
let lines = text.split(separator: "\n", omittingEmptySubsequences: false)
guard lines.first?.trimmed() == "---" else { return [:] }
guard lines.first(where: { !$0.trimmed().isEmpty })?.trimmed() == "---" else { return [:] }
var map: [String: String] = [:]
for line in lines.dropFirst() {
for line in lines.drop(while: { $0.trimmed() != "---" }).dropFirst() {
let trimmed = line.trimmed()
guard trimmed != "---" else { break }
guard let colon = trimmed.firstIndex(of: ":") else { continue }
let key = trimmed[..<colon].trimmingCharacters(in: .whitespaces)
let value = trimmed[trimmed.index(after: colon)...].trimmingCharacters(in: .whitespaces)
let value = trimmed[trimmed.index(after: colon)...]
.trimmingCharacters(in: .whitespaces)
.trimmingCharacters(in: CharacterSet(charactersIn: "\"'"))
map[key] = value
}
return map
}
/// The Post's Title: first non-empty line after any frontmatter block,
/// `#` heading markers stripped. A body with no text at all reads as
/// `untitled` (the blank-draft id).
/// `#` heading markers stripped; `untitled` when nothing readable remains.
static func title(of text: String) -> String {
var lines = text.split(separator: "\n", omittingEmptySubsequences: false)[...]
if lines.first?.trimmed() == "---" {
if let close = lines.dropFirst().firstIndex(where: { $0.trimmed() == "---" }) {
if let open = lines.firstIndex(where: { !$0.trimmed().isEmpty }), lines[open].trimmed() == "---" {
if let close = lines[(open + 1)...].firstIndex(where: { $0.trimmed() == "---" }) {
lines = lines[(close + 1)...]
} else {
lines = lines.dropFirst() // unterminated block: no title inside it
return "untitled" // unterminated block: nothing trustworthy follows
}
}
for line in lines {
let trimmed = line.trimmed()
if trimmed.isEmpty {
continue
}
return trimmed.trimmingCharacters(in: CharacterSet(charactersIn: "#").union(.whitespaces))
guard !trimmed.isEmpty else { continue }
let stripped = trimmed.trimmingCharacters(in: CharacterSet(charactersIn: "#").union(.whitespaces))
return stripped.isEmpty ? "untitled" : stripped
}
return "untitled"
}