// // 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, 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 home = boundHome, 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 /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 } // #179: the library-visible write of a completed pass — Pass State // advance — rides the write rail; the snapshot/stats/lint writes // above never do (they don't regroup the library). railWrite { _ = try PostStore.advancePassState(id: id, kind: kind, home: home) } } /// #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(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 home = boundHome, 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. } }