Files
museion/Museion/PostsModelPasses.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

167 lines
6.5 KiB
Swift

//
// PostsModelPasses.swift
// Museion
//
// #146: the engine-run extensions of PostsModel — pass orchestration
// (#24, S3.1) and the Blind Read (#122) — moved from PostsModel.swift
// (file-length ceiling), code unchanged.
//
import Foundation
// MARK: Pass orchestration (#24, S3.1)
extension PostsModel {
/// #24 (S3.1): run one structured critique pass over the OPEN piece.
/// The pre-pass snapshot lands before the engine sees the text; the
/// minimal stub (PassContext) crosses the bridge and the critique
/// streams into the mentor pane. Pass State advances ONLY when the
/// engine's turn ends normally — never on cancel, refusal, or crash,
/// and never when fix cards resolve. Passes run in any order
/// (advancePassState is monotonic).
func runPass(_ kind: PassKind, home: URL, mentor: MentorSessionModel) async {
guard runningPass == nil, !runningBlindRead else {
lastError = "A pass is already running."
return
}
guard !mentor.isStreaming else {
lastError = "The mentor is mid-reply — stop it or wait."
return
}
guard mentor.phase.isReady else {
lastError = "The mentor engine isn't ready — wait for it or restart it."
return
}
// #137 (S4): the pass profile's provider has no key — name it and
// point at Settings, BEFORE the open-piece guard so nothing writes
// to disk (not even the pre-pass snapshot).
if case let .missingKey(label) = mentor.readiness(for: kind) {
lastError = "\(kind.rawValue) needs a \(label) API key — add it in Settings → Provider Keys."
return
}
// The pass targets the open Piece: a never-saved draft has no
// folder, and the external guest and a held #111 guard ask are
// not Pieces.
guard let session,
let id = session.postID,
id == selectedPostID,
draft == nil,
externalSession == nil,
outlineGuardPieceID == nil
else {
lastError = "Open a saved post to run a pass."
return
}
guard flushOrAbort(id) else { return } // the engine reads disk
do {
try PostStore.snapshotPass(id: id, kind: kind, body: session.text, home: home)
} catch {
lastError = error.localizedDescription
return
}
// #31 (S5.1): unaided-first-draft stats — fires at the invocation,
// before the engine sees anything; eligibility (writer origin, First
// Draft, post type) is read from disk. A failed capture must never
// fail the pass (decay-clock posture), so the Bool is ignored.
PostStore.captureFirstDraftStats(id: id, body: session.text, home: home)
// #6 (S3.4): the native deslop lint — same invocation posture as the
// stats capture: pure Swift, no engine round-trip, a failure never
// blocks the pass. Cards land in <piece>/cards/ and the palette's
// watcher-driven rescan surfaces them (the #23 path).
DeslopLint.emit(body: session.text, pieceID: id, pieceURL: session.passPieceURL, kind: kind, home: home)
// #137 (S4): runningPass goes live BEFORE the mentor awaits —
// flush/snapshot/capture/lint above are synchronous, so nothing can slip
// between the guards and the flag: the #124 Cancel-visibility
// invariant now holds for the WHOLE run, pass-engine spawn included.
runningPass = kind
defer { runningPass = nil }
let stub = PassContext.stub(kind: kind, pieceURL: session.passPieceURL, hasResearch: session.hasResearch)
// #137: the pass runs on its OWN one-shot engine and streams into the
// transient pass transcript — never rostered (#129, ADR-0014).
let outcome = await mentor.runPass(kind: kind, stub: stub, pieceTitle: title(of: id))
guard case let .ended(stopReason) = outcome, stopReason != "refusal" else {
return // cancelled / refused / failed — the transcript tells the story
}
do {
_ = try PostStore.advancePassState(id: id, kind: kind, home: home)
lastError = nil
Task { await rescan() } // the library regroups by Pass State
} catch {
lastError = error.localizedDescription
}
}
/// #24 (S3.1): the pass bar's Cancel — the existing session/cancel
/// rail stops the agent; nothing advances (runPass reads .cancelled).
/// #122: the Blind Read shares the rail — Cancel ends either run.
func cancelPass(mentor: MentorSessionModel) {
guard runningPass != nil || runningBlindRead else { return }
mentor.stopStreaming()
}
}
// MARK: Blind Read (#122)
extension PostsModel {
/// #122: run one Blind Read over the OPEN piece — the current document
/// vs the newest Pass-start snapshot, judged blind by a fresh session
/// of the museion profile. Advisory-only (decision #120): no Pass State
/// advance, no cards, no Learnings — the verdict and the reveal live in
/// the transient transcript and die with it. The writer stays judge of
/// record.
func blindRead(home: URL, mentor: MentorSessionModel) async {
guard runningPass == nil else {
lastError = "A pass is already running."
return
}
guard !runningBlindRead else {
lastError = "A Blind Read is already running."
return
}
guard !mentor.isStreaming else {
lastError = "The mentor is mid-reply — stop it or wait."
return
}
guard mentor.phase.isReady else {
lastError = "The mentor engine isn't ready — wait for it or restart it."
return
}
// Same open-piece rule as a pass: the read judges ONE saved piece.
guard let session,
let id = session.postID,
id == selectedPostID,
draft == nil,
externalSession == nil,
outlineGuardPieceID == nil
else {
lastError = "Open a saved post to run a Blind Read."
return
}
guard flushOrAbort(id) else { return }
guard let base = PostStore.latestPassSnapshot(id: id, home: home) else {
lastError = "No Pass snapshot yet — run a Pass first."
return
}
let current = session.text
guard current != base.body else {
lastError = "No edits since the Pass started — the Blind Read judges the current document against the Pass-start snapshot."
return
}
// House posture (runPass's success path): the run is firing — a
// stale guard refusal no longer owns the error strip.
lastError = nil
runningBlindRead = true
defer { runningBlindRead = false }
// Randomized order kills position bias (#120's flagged default);
// the reveal names the mapping only after the verdict.
let snapshotFirst = Bool.random()
_ = await mentor.runBlindRead(
prompt: BlindRead.prompt(snapshot: base.body, current: current, snapshotFirst: snapshotFirst),
pieceTitle: title(of: id),
reveal: BlindRead.reveal(kind: base.kind, snapshotFirst: snapshotFirst),
)
// Advisory-only: nothing to advance or record — the outcome row in
// the transient transcript IS the deliverable.
}
}