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

+23 -19
View File
@@ -16,14 +16,15 @@ import Testing
@Suite("SessionLifecycleTests")
struct SessionLifecycleTests {
/// #28 AC2: the pass's session is rostered newest-first, active, and the
/// pass prompt streams into ITS transcript — the default freeform
/// session stays untouched while Pass State advances exactly as before.
@Test @MainActor func `pass auto-creates its rostered session`() async throws {
/// #137 (per #129): a pass streams into a TRANSIENT, unrostered
/// transcript — visible through the active surface while it runs, the
/// entry named by convention, never rostered, never persisted.
@Test @MainActor func `a pass streams into a transient unrostered transcript`() async throws {
let (model, home, mentor) = try makePassRig()
defer { mentor.shutdown() }
await mentor.openSession()
let defaultSession = try #require(mentor.activeEntry)
let rosterBefore = mentor.roster.count
let post = try PostStore.writeNew(body: "# Pass Piece\n\nThe body under pass.", home: home)
model.select(post.id, home: home)
for _ in 0 ..< 250 where !model.posts.contains(where: { $0.id == post.id }) {
@@ -32,25 +33,24 @@ struct SessionLifecycleTests {
await model.runPass(.pass1, home: home, mentor: mentor)
// Rostered newest-first, named by convention, and active.
#expect(mentor.roster.first?.name == "Pass 1 · \(post.title)")
#expect(mentor.activeEntry == mentor.roster.first)
// The prompt streamed into ITS transcript; the wire stub never lands.
// The roster never grew — the pass entry is transient.
#expect(mentor.roster.count == rosterBefore)
// The transient pass entry owns the active surface while it lives.
let entry = try #require(mentor.activeEntry)
#expect(texts(in: mentor.transcriptRows(of: entry)).contains("Pass 1 · \(post.title)"))
// The streamed verdict lands via a client notification Task that can
// settle just after runPass returns under load — bounded settle.
#expect(entry.name == "Pass 1 · \(post.title)")
#expect(entry.sessionId == MentorSessionModel.passTranscriptKey)
// The prompt streamed through the ACTIVE surface; the wire stub never lands.
#expect(texts(in: mentor.rows).contains("Pass 1 · \(post.title)"))
#expect(!texts(in: mentor.rows).contains { $0.contains("Critique the piece") })
var streamed = false
for _ in 0 ..< 250 {
streamed = texts(in: mentor.transcriptRows(of: entry)).contains(where: { $0.contains("Hello mentor.") })
streamed = texts(in: mentor.rows).contains { $0.contains("Hello mentor.") }
if streamed {
break
}
try await Task.sleep(nanoseconds: 20_000_000)
}
let rows = mentor.transcriptRows(of: entry)
#expect(streamed)
#expect(!texts(in: rows).contains { $0.contains("Critique the piece") })
// The pre-existing freeform session's transcript is untouched.
#expect(mentor.transcriptRows(of: defaultSession).isEmpty)
// Pass State advanced on the completed turn, exactly as before.
@@ -455,11 +455,15 @@ struct SessionLifecycleTests {
return false
})
}
}
// Scenario: draft candidate — from #30 (S2.7). Given a session
// transcript, when the sheet prefills, then the last mentor text row is
// the draft candidate; thought/card/research/raw rows never prefill.
@Test @MainActor func `draft candidate is the last mentor text row`() {
// Scenario: draft candidate — from #30 (S2.7). Given a session
// transcript, when the sheet prefills, then the last mentor text row is
// the draft candidate; thought/card/research/raw rows never prefill.
// File-scope extension — type_body_length posture; same suite, same file.
@MainActor
extension SessionLifecycleTests {
@Test func `draft candidate is the last mentor text row`() {
let rows: [SessionRow] = [
SessionRow(content: .text("You: help me draft this")),
SessionRow(content: .thought("thinking hard about angles")),
@@ -477,7 +481,7 @@ struct SessionLifecycleTests {
#expect(MentorSessionModel.draftCandidateText(in: rows) == "second and latest reply")
}
@Test @MainActor func `no mentor text yields no draft candidate`() {
@Test func `no mentor text yields no draft candidate`() {
// Empty transcript, user-only, and non-text lanes never prefill.
#expect(MentorSessionModel.draftCandidateText(in: []) == nil)
#expect(MentorSessionModel.draftCandidateText(in: [