Files
museion/Museion/PostsModelCards.swift
T
AlexKucera 012f91727f
CI / ci (push) Canceled after 0s
refactor(postsmodel): split domain extensions (#146)
PostsModel.swift had outgrown its lint ceilings — 1127 lines against the
1000 file-length error, class body 361 against 350; both pre-dated the
week and #143 deepened them while naming this split as the follow-up.
The six domain extensions after the class move to their own files,
domain-grouped: PostsModelOutline.swift (mixed-content guard #111 +
Convert to Post #114), PostsModelPasses.swift (pass orchestration #24 +
Blind Read #122), PostsModelCards.swift (stored cards #23 +
apply/skip/done #25 + promote #26, with their support types). The
watcher section (startWatching + rescan) moves to a file-scope
extension so the class body drops to 323 — #111's precedent: same-file
extensions keep private state file-scoped while type_body_length only
counts the class body.

Pure moves, mechanically verified line-for-line: the only edits are the
18 private->internal widenings the cross-file moves force (13
private(set) setters + rescan/dismissExternal/wireSession/
isGuardedOutline/openUnguarded, each doc-noted) and the two
wrapped-condition guards unwrapped per the #142 learning, clause order
preserved. The error ceilings and both opening_brace warnings are gone;
the residual default WARNING-tier notices (708/400 file, 323/250
type-body) match the repo's standing posture — the ticket's own
~700-line landing zone makes the 400 tier unreachable.

Full suite 574/574 by xcresult executed count (573 unit + 1 UI smoke).

Closes #146
2026-11-03 12:44:13 +01:00

156 lines
5.8 KiB
Swift

//
// PostsModelCards.swift
// Museion
//
// #146: the fix-card extensions of PostsModel — stored-card surfacing
// (#23), apply/skip/done (#25, S3.2/S3.3), and promote (#26, S4.1) —
// moved from PostsModel.swift (file-length ceiling), code unchanged.
//
import Foundation
// MARK: Stored fix cards (#23, M2 slice 3)
extension PostsModel {
/// #23 (M2, slice 3): re-derive `openPieceCards` from the OPEN piece's
/// `<piece>/cards/` (FixCardStore.scan — any MCP client's submissions,
/// newest-first; archive pieces resolve through folder(of:) too). The
/// nil-session rule clears: draft, external guest, held guard ask,
/// nothing open → []. Called at the rescan tail and the open
/// transitions — the piece-roots watcher already fires on cards/ writes.
/// #122: the same pass also derives `openPieceHasSnapshot` (the Blind
/// Read's base) — one detached read of the open piece's disk facts.
func refreshCards(home: URL) {
guard let id = session?.postID, let folder = PostStore.folder(of: id, home: home) else {
openPieceCards = []
openPieceHasSnapshot = false
return
}
// Plain disk reads re-run on every watcher burst (typing retriggers
// rescan) — keep the I/O off MainActor, same posture as rescan's
// Post.scan. The id re-check on hop-back drops a scan that raced a
// piece switch.
Task.detached { [weak self] in
let cards = FixCardStore.scan(pieceURL: folder)
let hasSnapshot = PostStore.latestPassSnapshot(id: id, home: home) != nil
await MainActor.run { [weak self] in
guard let self, session?.postID == id else { return }
openPieceCards = cards
openPieceHasSnapshot = hasSnapshot
}
}
}
/// The piece's display title (fallback: id) — runPass, the pass bar,
/// and the Fix Cards palette share this one lookup (review: three
/// hand-copies).
func title(of id: String) -> String {
posts.first(where: { $0.id == id })?.title ?? id
}
}
// MARK: Fix Card apply/skip/done (#25, S3.2/S3.3)
/// #25 (S3.2/S3.3): what Apply did — the edit landed, the anchor went
/// stale (no text touched, card flagged), or the operation refused with
/// a reason for the error strip.
enum FixCardApplyOutcome: Equatable {
case applied
case stale
case refused(String)
}
extension PostsModel {
/// #25 (S3.2/S3.3): Apply a mechanical card — the blocking ask already
/// happened in the UI (ask-before trust level); this lands the edit.
/// Dirty buffer lands first (Apply reads live text); an unanswered #67
/// conflict aborts here with the existing wording. A stale anchor
/// flags the card and touches no text. A failed close after a landed
/// edit is reported via lastError, never silent.
@discardableResult
func applyCard(_ card: FixCardStore.StoredFixCard, home: URL) -> FixCardApplyOutcome {
guard card.status == .open else { return .refused("card already closed") }
guard card.lane == .mechanical else { return .refused("demonstration cards close Done or Skip") }
guard let session, let postID = session.postID else {
return .refused("open the piece to work its cards")
}
guard flushOrAbort(postID) else { return .refused(lastError ?? "edits did not land") }
guard let newText = FixCardStore.appliedText(of: card, in: session.text) else {
// Stale anchor — the Post changed after the card was issued:
// persist the honest flag, select and change nothing. A failed
// flag write surfaces like the applied path's — never silent.
do {
_ = try FixCardStore.close(
pieceURL: session.passPieceURL, id: card.id, status: .open, anchorStatus: .stale,
)
} catch {
lastError = "card close failed: \(error.localizedDescription)"
}
refreshCards(home: home)
return .stale
}
guard session.adoptText(newText) else { return .refused(lastError ?? "write failed") }
closeCard(card, status: .applied, home: home)
return .applied
}
/// #25 (S3.3): Skip — not this instance. Closes the card recording
/// NOTHING beyond the status flip (CONTEXT.md).
func skipCard(_ card: FixCardStore.StoredFixCard, home: URL) {
closeCard(card, status: .skipped, home: home)
}
/// #25 (S3.3): Done — the writer rewrote the span themselves. Same
/// flip, different terminal.
func doneCard(_ card: FixCardStore.StoredFixCard, home: URL) {
closeCard(card, status: .done, home: home)
}
private func closeCard(
_ card: FixCardStore.StoredFixCard,
status: FixCardStore.StoredFixCard.Status,
home: URL,
) {
guard let session else {
lastError = "open the piece to work its cards"
return
}
do {
_ = try FixCardStore.close(pieceURL: session.passPieceURL, id: card.id, status: status)
} catch {
lastError = "card close failed: \(error.localizedDescription)"
}
refreshCards(home: home)
}
}
// MARK: Fix Card promote (#26, S4.1/S4.3)
/// #26 (S4.1): what Promote did — the Learning landed, or the operation
/// refused with a reason (the ask sheet shows it; lastError carries it).
enum FixCardPromoteOutcome: Equatable {
case promoted
case refused(String)
}
extension PostsModel {
/// #26 (S4.1/S4.3): Promote a card into ONE plain markdown Learning
/// under `<home>/learnings/<type>s/` — the writer's EXPLICIT
/// confirmation only (one confirm = one entry, never a repetition
/// counter; the store guarantees the one-stamp rule). Touches no text
/// and flushes nothing: this writes a Learning, not an edit.
@discardableResult
func promoteCard(_ card: FixCardStore.StoredFixCard, home: URL) -> FixCardPromoteOutcome {
guard card.promotedAt == nil else { return .refused("card already promoted") }
guard let session else { return .refused("open the piece to work its cards") }
do {
_ = try FixCardStore.promote(pieceURL: session.passPieceURL, card: card)
} catch {
lastError = "learning write failed: \(error.localizedDescription)"
return .refused("learning write failed: \(error.localizedDescription)")
}
refreshCards(home: home)
return .promoted
}
}