refactor(posts): route every library write through one rail
CI / ci (push) Canceled after 0s
CI / ci (push) Canceled after 0s
#179: every library-reflecting store write (draft save, session-born post, rename, move to project, archive/restore, trash, convert, project creation, import batch, pass advance) now rides one internal seam — performWrite pairs the write with exactly one rescan and owns the error strip (cleared BEFORE the write so importFiles' per-file failure messages survive the success tail; a nil-Home refusal writes nothing). The model owns the Home: watchedHome is private(set), set only by bind (construction + the Settings repoint); no public method takes a Home parameter — 14 view call sites and ~115 test call sites swept, dead home plumbing cut. The editor's debounced flush, the card closes, the pass substrate and the micropub publish stay off the rail by design (own error semantics, no library regroup). A converged review finding is closed with a guard: the pass advance re-resolves watchedHome after the engine's minutes-long turn, so a mid-pass Settings repoint now refuses the advance with an honest strip message instead of silently stamping pass state into the repointed library (wrong-piece write on a slug collision). createProject failures now surface in the sheet AND the strip — the rail-owns-the- strip posture supersedes the #108 sheet-only rule. Five rail pins (PostsModelWriteRailTests, suite members): write → one library refresh + strip cleared, failed write owns the strip, nil-Home refusal, import batch keeps per-file messages, mid-pass repoint refuses the advance — the last two flip-verified against their guards. ScriptedEngine gains endHeldPrompt (release a held turn as ended). Verification: suite 657/657 the evening this landed; the next-morning re-run could not start — both test runners hung before establishing connection (#134 testmanagerd wedge; an untouched control suite hangs identically — machine state, not this diff; reboot pending). Rail pins 5/5 isolated after the last test-only edit; swiftlint at baseline parity; swiftformat clean. Closes #179 [Generated by AI]
This commit is contained in:
1 parent
627ecf515e
commit
5bf09552f6
27 files changed
+572
-345
No files matched your search
+100
-94
@@ -12,6 +12,11 @@
|
||||
// PostsModelOutline/PostsModelPasses/PostsModelCards.swift; startWatching
|
||||
// + rescan sit in the file-scope watcher extension below (type-body
|
||||
// ceiling).
|
||||
// #179: every library-reflecting store write rides `performWrite` — the
|
||||
// one write rail pairing each write with exactly one rescan and owning
|
||||
// the error strip (card closes and the pass substrate stay off it: they
|
||||
// never regroup the library); the model owns the Home (watchedHome), no
|
||||
// method takes one.
|
||||
//
|
||||
|
||||
import CoreServices
|
||||
@@ -171,8 +176,11 @@ final class PostsModel {
|
||||
/// Last failed-op message surfaced in the editor (nil = clean).
|
||||
/// #146: setter internal — the split-out domain extensions set it.
|
||||
var lastError: String?
|
||||
/// Home the watcher is bound to; also the rescan target.
|
||||
private var watchedHome: URL?
|
||||
/// #179: the bound Home — the MODEL owns it. Construction (`bind`) and
|
||||
/// the Settings repoint (bind again through `.task(id:)`) are the only
|
||||
/// writers; no public method takes a Home parameter, callers pass none.
|
||||
/// `private(set)` — the setter stays PostsModel.swift-file-private.
|
||||
private(set) var watchedHome: URL?
|
||||
private var watcher = PostsWatcher()
|
||||
/// #143 (OPT-F3): scan parse memo — a rescan re-parses only changed
|
||||
/// post.md files, not the library (see PostScanCache).
|
||||
@@ -196,6 +204,33 @@ final class PostsModel {
|
||||
// writes and same-volume moves; if a big Home ever stalls the UI, wrap
|
||||
// them in Task.detached like rescan.
|
||||
|
||||
/// #179: the write rail — ONE seam pairs every library store write with
|
||||
/// exactly one rescan and owns the error strip. A verb passes its
|
||||
/// PostStore write (plus any post-write state surgery); a throw lands
|
||||
/// its reason in the strip with nothing rescanned, a clean run rescans
|
||||
/// once. The strip clears BEFORE the write, so a batch write that sets
|
||||
/// its own per-file failure messages mid-closure (importFiles) keeps
|
||||
/// them. The editor's debounced flush stays OUTSIDE the rail — its
|
||||
/// error semantics are the session's own (EditorSession.onError).
|
||||
/// Returns nil when the write landed; the failure reason otherwise
|
||||
/// (createProject hands that to its sheet).
|
||||
@discardableResult
|
||||
func performWrite(_ write: (URL) throws -> Void) -> String? {
|
||||
guard let home = watchedHome else {
|
||||
lastError = "No library is open yet — nothing was changed."
|
||||
return lastError
|
||||
}
|
||||
lastError = nil
|
||||
do {
|
||||
try write(home)
|
||||
Task { await rescan() }
|
||||
return nil
|
||||
} catch {
|
||||
lastError = error.localizedDescription
|
||||
return lastError
|
||||
}
|
||||
}
|
||||
|
||||
/// #107: the project a NEW piece lands in — #108: an explicit pending
|
||||
/// target first, else the selected non-archived post's project, else the
|
||||
/// default. One home; newPost/importFiles must not drift.
|
||||
@@ -260,11 +295,10 @@ final class PostsModel {
|
||||
|
||||
/// First save of the draft (#62): lazy folder, slug, #59 stamp all fire
|
||||
/// here. The saved post opens straight into an editing session — the
|
||||
/// writer keeps typing where they were. Rescans immediately (the watcher
|
||||
/// would too, after its window).
|
||||
func saveDraft(home: URL) {
|
||||
/// writer keeps typing where they were. #179: the write rides the rail.
|
||||
func saveDraft() {
|
||||
guard let draft else { return }
|
||||
do {
|
||||
performWrite { home in
|
||||
let post = try PostStore.writeNew(body: draft.text, project: draft.project, type: draft.type, home: home)
|
||||
lastDraftWasOutline = draft.type == .outline // #109: covers the lagging rescan
|
||||
self.draft = nil
|
||||
@@ -272,10 +306,6 @@ final class PostsModel {
|
||||
selectedPostID = post.id
|
||||
session = EditorSession(postID: post.id, home: home)
|
||||
wireSession()
|
||||
lastError = nil
|
||||
Task { await rescan() }
|
||||
} catch {
|
||||
lastError = error.localizedDescription
|
||||
}
|
||||
}
|
||||
|
||||
@@ -283,18 +313,16 @@ final class PostsModel {
|
||||
/// draft stamped `origin: ai` (never stats-eligible) and open it in
|
||||
/// Posts (select's open path; the WorkspaceView selection onChange
|
||||
/// hops the pane). Nil + lastError on failure; Cancel never gets here.
|
||||
/// #179: the write rides the rail.
|
||||
@discardableResult
|
||||
func newPostFromSession(body: String, home: URL) -> Post? {
|
||||
do {
|
||||
func newPostFromSession(body: String) -> Post? {
|
||||
var written: Post?
|
||||
performWrite { home in
|
||||
let post = try PostStore.writeNew(body: body, origin: .ai, home: home)
|
||||
select(post.id, home: home)
|
||||
lastError = nil
|
||||
Task { await rescan() }
|
||||
return post
|
||||
} catch {
|
||||
lastError = error.localizedDescription
|
||||
return nil
|
||||
written = post
|
||||
select(post.id)
|
||||
}
|
||||
return written
|
||||
}
|
||||
|
||||
/// Select a Post; selecting anything abandons an unsaved draft (the
|
||||
@@ -306,7 +334,8 @@ final class PostsModel {
|
||||
/// fires the mixed-content guard — the open is held (routing stays
|
||||
/// prose) until the writer picks. The old session still ends here:
|
||||
/// its flush is prose-mode, so it can't drop anything.
|
||||
func select(_ id: String, home: URL) {
|
||||
func select(_ id: String) {
|
||||
guard watchedHome != nil else { return } // #179: no library, no open
|
||||
dismissExternal()
|
||||
// A reselect of the already-open piece is a no-op — INCLUDING a
|
||||
// guarded one: the piece is open under a prior answer, and firing
|
||||
@@ -318,7 +347,7 @@ final class PostsModel {
|
||||
if let session {
|
||||
session.teardown(flushFirst: true)
|
||||
}
|
||||
if isGuardedOutline(id, home: home) {
|
||||
if isGuardedOutline(id) {
|
||||
outlineGuardPieceID = id
|
||||
proseOverridePostID = nil
|
||||
session = nil
|
||||
@@ -326,13 +355,13 @@ final class PostsModel {
|
||||
pendingProject = nil
|
||||
lastDraftWasOutline = false
|
||||
selectedPostID = id
|
||||
refreshCards(home: home) // #23: nil session — the held ask shows no cards
|
||||
refreshCards() // #23: nil session — the held ask shows no cards
|
||||
return
|
||||
}
|
||||
// The prior session was torn down above; openUnguarded's guards
|
||||
// are dead code on this path (session is nil or a different id) —
|
||||
// kept: one open body answers select and the ask alike.
|
||||
openUnguarded(id, home: home)
|
||||
openUnguarded(id)
|
||||
proseOverridePostID = nil // #111: the override died with the open it covered
|
||||
}
|
||||
|
||||
@@ -379,14 +408,14 @@ final class PostsModel {
|
||||
|
||||
/// Explicit Rename = retitle + move (#60). Renaming the open post flushes
|
||||
/// first (rename reads the file from disk — unflushed edits must land
|
||||
/// before the move), then reopens a session at the new id. Sets lastError
|
||||
/// on failure.
|
||||
func rename(_ post: Post, to title: String, home: URL) {
|
||||
/// before the move), then reopens a session at the new id. #179: the
|
||||
/// write rides the rail.
|
||||
func rename(_ post: Post, to title: String) {
|
||||
// #108 audit 2: rename reads the file from disk — a failed write
|
||||
// aborts (moving the folder would strand unflushed edits), and the
|
||||
// session stays up, dirty and honest.
|
||||
guard flushOrAbort(post.id) else { return }
|
||||
do {
|
||||
performWrite { home in
|
||||
let moved = try PostStore.rename(post, to: title, home: home)
|
||||
if selectedPostID == post.id {
|
||||
selectedPostID = moved.id
|
||||
@@ -395,18 +424,15 @@ final class PostsModel {
|
||||
wireSession()
|
||||
}
|
||||
}
|
||||
lastError = nil
|
||||
Task { await rescan() }
|
||||
} catch {
|
||||
lastError = error.localizedDescription
|
||||
}
|
||||
}
|
||||
|
||||
/// Delete a Post (Trash, recoverable — #18 AC). Clears selection if it
|
||||
/// was the post; the session is torn down (cancel only — the folder is
|
||||
/// leaving, a flush would resurrect nothing but churn the watcher).
|
||||
func trash(_ post: Post, home: URL) {
|
||||
do {
|
||||
/// #179: the write rides the rail.
|
||||
func trash(_ post: Post) {
|
||||
performWrite { home in
|
||||
try PostStore.trash(post, home: home)
|
||||
if selectedPostID == post.id {
|
||||
selectedPostID = nil
|
||||
@@ -415,10 +441,6 @@ final class PostsModel {
|
||||
session = nil
|
||||
}
|
||||
}
|
||||
lastError = nil
|
||||
Task { await rescan() }
|
||||
} catch {
|
||||
lastError = error.localizedDescription
|
||||
}
|
||||
}
|
||||
|
||||
@@ -431,16 +453,12 @@ final class PostsModel {
|
||||
newPost()
|
||||
}
|
||||
|
||||
/// #108: raw create — the sheet composes on it. The failure reason on
|
||||
/// false (shown inline in the sheet; never routed into lastError — the
|
||||
/// strip behind the sheet must not double-message one error).
|
||||
func createProject(named name: String, home: URL) -> String? {
|
||||
do {
|
||||
try PostStore.createProject(named: name, home: home)
|
||||
return nil // success
|
||||
} catch {
|
||||
return error.localizedDescription
|
||||
}
|
||||
/// #108: raw create — the sheet composes on it. #179: rides the write
|
||||
/// rail (one rescan pairs the new folder); the failure reason is BOTH
|
||||
/// returned for the sheet's inline display AND owned by the strip —
|
||||
/// the rail posture replaces the old sheet-only rule.
|
||||
func createProject(named name: String) -> String? {
|
||||
performWrite { try PostStore.createProject(named: name, home: $0) }
|
||||
}
|
||||
|
||||
/// #108: File ▸ New Project… — stage the ask (WorkspaceView presents).
|
||||
@@ -457,7 +475,8 @@ final class PostsModel {
|
||||
/// projects/ plus the default (bind creates the roots, not posts/ — an
|
||||
/// empty Home must still offer it), localized-sorted to match the
|
||||
/// library's section order.
|
||||
func moveTargets(home: URL) -> [String] {
|
||||
func moveTargets() -> [String] {
|
||||
guard let home = watchedHome else { return [Post.defaultProject] }
|
||||
let root = home.appendingPathComponent("projects", isDirectory: true)
|
||||
let onDisk = (try? FileManager.default.contentsOfDirectory(atPath: root.path)) ?? []
|
||||
var names = Set(onDisk.filter { !$0.hasPrefix(".") })
|
||||
@@ -493,27 +512,25 @@ final class PostsModel {
|
||||
/// #108: Move to Project — flush first (move reads disk), then the
|
||||
/// folder moves under projects/. Id stays the same; the session is
|
||||
/// reopened anyway so folderURL re-resolves to the new parent.
|
||||
func moveToProject(_ post: Post, project: String, home: URL) {
|
||||
/// #179: the write rides the rail.
|
||||
func moveToProject(_ post: Post, project: String) {
|
||||
guard flushOrAbort(post.id) else { return }
|
||||
do {
|
||||
performWrite { home in
|
||||
try PostStore.move(post, to: project, home: home)
|
||||
if session?.postID == post.id {
|
||||
session = EditorSession(postID: post.id, home: home)
|
||||
wireSession()
|
||||
}
|
||||
lastError = nil
|
||||
Task { await rescan() }
|
||||
} catch {
|
||||
lastError = error.localizedDescription
|
||||
}
|
||||
}
|
||||
|
||||
/// #108: Move to Archive. Archiving the open piece clears it — the
|
||||
/// editor is empty until the next selection (archived pieces open from
|
||||
/// the Archive pane, where they're listed).
|
||||
func archivePost(_ post: Post, home: URL) {
|
||||
/// the Archive pane, where they're listed). #179: the write rides the
|
||||
/// rail.
|
||||
func archivePost(_ post: Post) {
|
||||
guard flushOrAbort(post.id) else { return }
|
||||
do {
|
||||
performWrite { home in
|
||||
try PostStore.archive(post, home: home)
|
||||
if selectedPostID == post.id {
|
||||
selectedPostID = nil
|
||||
@@ -522,10 +539,6 @@ final class PostsModel {
|
||||
open.teardown(flushFirst: false) // already flushed above
|
||||
session = nil
|
||||
}
|
||||
lastError = nil
|
||||
Task { await rescan() }
|
||||
} catch {
|
||||
lastError = error.localizedDescription
|
||||
}
|
||||
}
|
||||
|
||||
@@ -533,20 +546,17 @@ final class PostsModel {
|
||||
/// the writer stays in the piece they just revived. A session already
|
||||
/// open on the piece survives `select` — reopen it so folderURL points
|
||||
/// at projects/, not the archive path it was born with.
|
||||
func restorePost(_ post: Post, home: URL) {
|
||||
/// #179: the write rides the rail.
|
||||
func restorePost(_ post: Post) {
|
||||
guard flushOrAbort(post.id) else { return }
|
||||
do {
|
||||
performWrite { home in
|
||||
try PostStore.restore(post, home: home)
|
||||
if let open = session, open.postID == post.id {
|
||||
session = EditorSession(postID: post.id, home: home)
|
||||
wireSession()
|
||||
} else {
|
||||
select(post.id, home: home)
|
||||
select(post.id)
|
||||
}
|
||||
lastError = nil
|
||||
Task { await rescan() }
|
||||
} catch {
|
||||
lastError = error.localizedDescription
|
||||
}
|
||||
}
|
||||
|
||||
@@ -554,14 +564,16 @@ final class PostsModel {
|
||||
/// Flush-first like every folder move (a dirty open piece inside the
|
||||
/// project would otherwise race the move), and its session is reopened so
|
||||
/// folderURL re-resolves off archive/ (same reason as restorePost).
|
||||
func unarchiveProject(_ name: String, home: URL) {
|
||||
/// #108 audit 2: disk-derived membership — the posts snapshot is stale
|
||||
/// the moment a Finder move lands. Captured BEFORE the move: after it
|
||||
/// the piece resolves under projects/ and its archive membership is
|
||||
/// invisible. dir − 1 = project dir (…/archive/<name>), − 2 = root.
|
||||
/// #179: the write rides the rail.
|
||||
func unarchiveProject(_ name: String) {
|
||||
// #108 audit 2: disk-derived membership — the posts snapshot is stale
|
||||
// the moment a Finder move lands. Captured BEFORE the move: after it
|
||||
// the piece resolves under projects/ and its archive membership is
|
||||
// invisible. dir − 1 = project dir (…/archive/<name>), − 2 = root.
|
||||
guard let boundHome = watchedHome else { return }
|
||||
func openPieceInArchivedProject() -> String? {
|
||||
guard let id = session?.postID,
|
||||
let projectDir = PostStore.folder(of: id, home: home)?.deletingLastPathComponent(),
|
||||
let projectDir = PostStore.folder(of: id, home: boundHome)?.deletingLastPathComponent(),
|
||||
projectDir.lastPathComponent == name,
|
||||
projectDir.deletingLastPathComponent().lastPathComponent == "archive"
|
||||
else { return nil }
|
||||
@@ -572,16 +584,12 @@ final class PostsModel {
|
||||
// #108 audit 2: the only #108 folder move that skipped the gate.
|
||||
guard flushOrAbort(openID) else { return }
|
||||
}
|
||||
do {
|
||||
performWrite { home in
|
||||
try PostStore.unarchiveProject(name, home: home)
|
||||
if let openID {
|
||||
session = EditorSession(postID: openID, home: home)
|
||||
wireSession()
|
||||
}
|
||||
lastError = nil
|
||||
Task { await rescan() }
|
||||
} catch {
|
||||
lastError = error.localizedDescription
|
||||
}
|
||||
}
|
||||
|
||||
@@ -644,28 +652,26 @@ final class PostsModel {
|
||||
/// default, reversible; never an alert, never an abort). The last
|
||||
/// imported post opens; a failure anywhere keeps its message up even
|
||||
/// when other files landed. #107: the batch lands in the selected
|
||||
/// post's project (newPost's target rule).
|
||||
func importFiles(origin: Post.Origin, home: URL) {
|
||||
/// post's project (newPost's target rule). #179: the batch rides the
|
||||
/// rail — the strip was cleared up front, so the per-file failure
|
||||
/// messages the loop sets survive the rail's success tail.
|
||||
func importFiles(origin: Post.Origin) {
|
||||
guard let urls = pendingImportURLs else { return }
|
||||
pendingImportURLs = nil
|
||||
let project = targetProject
|
||||
var last: Post?
|
||||
var failures = 0
|
||||
for url in urls {
|
||||
do {
|
||||
last = try PostStore.importFile(at: url, origin: origin, project: project, home: home)
|
||||
} catch {
|
||||
failures += 1
|
||||
lastError = "Import failed — \(url.lastPathComponent): \(error.localizedDescription)"
|
||||
performWrite { home in
|
||||
for url in urls {
|
||||
do {
|
||||
last = try PostStore.importFile(at: url, origin: origin, project: project, home: home)
|
||||
} catch {
|
||||
lastError = "Import failed — \(url.lastPathComponent): \(error.localizedDescription)"
|
||||
}
|
||||
}
|
||||
}
|
||||
if let last {
|
||||
select(last.id, home: home)
|
||||
select(last.id)
|
||||
}
|
||||
if failures == 0 {
|
||||
lastError = nil
|
||||
}
|
||||
Task { await rescan() }
|
||||
}
|
||||
|
||||
/// #146: internal — the split-out domain extensions wire sessions too.
|
||||
@@ -739,6 +745,6 @@ extension PostsModel {
|
||||
}
|
||||
// #23: the open piece's stored fix cards ride the same rescan —
|
||||
// the watcher fires on <piece>/cards/ writes too.
|
||||
refreshCards(home: home)
|
||||
refreshCards()
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user