feat(mentor): per-surface engine lifecycle (#137)
CI / ci (push) Canceled after 0s

Profiles S4: passes run on their OWN one-shot engines —
`hermes acp -p museion-passN` spawns per pass run and tears down at
the verdict, so a model switch costs nothing (the next spawn reads
fresh config). The verdict streams into a TRANSIENT unrostered
transcript (#129's ruling: one Sessions surface over the chat
engine; a rostered pass session would be unresumable by the chat
engine). `beginPassSession` died with it — passes never mint
sessions; the #28 roster pins flipped to the transient posture.

The chat engine stays resident (adopted museion profile): a Chat
model change in Settings fires onChatSurfaceWrite after a
successful chat-surface write only, wired to requestRestart —
re-spawn in place when idle (resuming the pre-restart active
session), deferred to turn end while any turn streams (#129).

Missing-key posture (#132): refreshPassReadiness reads both pass
profiles' providers and their .env keys at pane entry; Run Pass
disables naming the provider with an Open Settings link, .unknown
never blocks, and the guard refuses before anything touches disk.

Finish-audit (ponytail/security/general fanout) applied: the
general reviewer's Medium — a session/new/load/fork completing
mid-pass stole the active surface and lost the Cancel to the wrong
engine — fixed with ONE claimActive gate across the post-await
activation writers, pinned deterministically via the fake's
loadReplayHold park + a mode-file flip (flip-verified: guard off
fails on the steal observables and the misrouted-cancel hang).
Also fixed: a spawn-window cancel loss in stopStreaming (keyed on
the transient entry, not the late-arriving wire id), and a
PRE-EXISTING rescan bug where a stale scan snapshot "vanished" a
just-opened piece and killed its session, cancelling the edit
debounce — the vanish-kill now re-checks disk and the watched
home. Ponytail cut ~59 lines of test duplication; the app target's
only force-unwrap is gone.

Suite 477 (was 472): EngineLifecycleTests new (own-engine
end-to-end, held-pass cancel, idle + deferred restart ordering,
readiness, steal pin), +3 Settings hook pins, one duplicate pin
deduplicated. swiftformat clean, swiftlint at HEAD baseline.

Closes #137
This commit is contained in:
AlexKucera committed 2026-10-28 17:26:40 +01:00
1 parent a4ea914690
commit 1c697440d8
19 files changed
+1034 -162

No files matched your search

+27 -25
View File
@@ -194,7 +194,19 @@ final class PostsModel {
}.value
guard !Task.isCancelled else { return }
posts = scanned
if let id = selectedPostID, !posts.contains(where: { $0.id == id }) {
if let id = selectedPostID, !posts.contains(where: { $0.id == id }),
// Stale-scan guard (#137 suite find): the snapshot can PREDATE the
// current selection — bind's initial rescan (detached scan over the
// empty tree) can land its continuation after a select its scan never
// saw, "vanishing" a piece that exists. Disk is the truth for
// vanished; a stale snapshot must not kill a live session (teardown
// cancels the debounce — unflushed edits would die with it).
// #137 audit (finish): a scan that outlived a Home repoint speaks
// for the OLD tree — never let it kill a session selected under
// the NEW home (the id is missing from the old snapshot by
// construction); the next rescan under the new home owns it.
watchedHome == home, PostStore.folder(of: id, home: home) == nil
{
selectedPostID = nil // the post vanished (deleted in Finder or here)
if let open = session, open.postID == id {
open.teardown(flushFirst: false) // post is gone; cancel the debounce
@@ -818,6 +830,13 @@ extension PostsModel {
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.
@@ -843,33 +862,16 @@ extension PostsModel {
// 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)
// #28 (S2.3): every pass auto-creates its own session — rostered
// 'Pass N · <title>' and activated, so the pass streams into ITS
// transcript. The existing ready-guard above stays the refusal pin.
let passSession = await mentor.beginPassSession(kind: kind, pieceTitle: title(of: id))
guard passSession != nil else {
lastError = "The mentor engine refused a session for the pass."
return
}
// Finish-audit: the beginPassSession await above is these guards'
// first suspension — a second Run Pass landing inside it re-passed
// every guard (both flags still unset) and minted a second pass
// session. Re-check ownership here, before the flag goes live; the
// sub-ms window where BOTH awaits are still in flight self-contains
// via sendPrompt's isStreaming refusal (one orphan roster entry,
// never a double stream — ponytail ceiling).
guard runningPass == nil else {
lastError = "A pass is already running."
return
}
// runningPass goes live only AFTER the session awaits are done: from
// here to sendPrompt's isStreaming there is no suspension, so anything
// observing runningPass (the pass bar, the #124 Cancel pin) can rely
// on Stop being able to cancel the live turn.
// #137 (S4): runningPass goes live BEFORE the mentor awaits —
// flush/snapshot/capture 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)
let outcome = await mentor.sendPrompt(stub, label: "\(kind.rawValue) · \(title(of: id))")
// #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
}