refactor(session): extract the engine host
CI / ci (push) Canceled after 0s

Engine lifecycle policy — spawn, handshake, respawn-on-crash, teardown,
and the per-surface residency rules (resident chat engine; one-shot
pass engines spawned per run, torn down at the verdict, ADR-0014) —
moves out of MentorSessionModel into EngineHost behind the
EngineTransport seam. The model keeps session + turn coordination and
stays the only observable the panes see; ACPEngineClient becomes the
seam's production adapter, and a scripted in-process engine
(ScriptedEngine) becomes the second adapter — engine-behaviour tests
(streaming, crash, cancel, restart, per-surface spawn) now run without
child processes, retiring the zsh fake-server rigs from logic tests
(#30 flake family). Exactly one real-process wire smoke per LLM
surface survives (chat two-act + parameterized pass1/pass2): the
held-turn cancel contract, hung-child SIGTERM teardown, handshake,
streamed turn, crash, restart.

One-shot teardown is structural (withOneShot closes at body end, every
exit path); the one-shot slot is claimed before the first suspension so
the at-most-one invariant holds across the spawn/handshake awaits.

Suite: 652 executed / 652 passed / 0 failed (653 argument cases),
counted from the xcresult; swiftformat idempotent, swiftlint clean on
touched files.

Closes #178
This commit is contained in:
AlexKucera committed 2026-11-09 18:26:16 +01:00
1 parent d14cb40e73
commit 627ecf515e
18 files changed
+1663 -639

No files matched your search

+21 -38
View File
@@ -3,7 +3,9 @@
// MuseionTests
//
// #122: the Blind Read — a writer-triggered, whole-document A/B judgment
// (advisory). From user story #122 / decision #120.
// (advisory). From user story #122 / decision #120. #178: the advisory
// run rides the in-process ScriptedEngines rig — the rig's spawnedProfiles
// pin which engine judged, the engines' promptLog pins what it was asked.
//
// ## Scenario: the blind judge prompt
// Given a Pass-start snapshot and the current document
@@ -109,17 +111,6 @@ struct BlindReadTests {
// MARK: Scenario 3 — the advisory run (mentor rail)
/// makePassRig + the fake's directory (the spawn/prompt logs live
/// beside the fake — makePassRig doesn't expose it).
// swiftlint:disable:next large_tuple
private func makeBlindRig(mode: String = "full") throws -> (model: PostsModel, home: URL, mentor: MentorSessionModel, fakeDir: String) {
let (model, home) = try makeModelHome()
let dir = acpTempDir("blindread")
let fake = makeFakeACP(at: dir, mode: mode)
let mentor = MentorSessionModel(binaryPath: fake)
return (model, home, mentor, dir)
}
/// Open a piece with a planted snapshot and a differing buffer — the
/// blind read's given state. Returns the piece id.
@discardableResult
@@ -133,13 +124,14 @@ struct BlindReadTests {
return post.id
}
// swiftlint:disable:next large_tuple
private func spawnLog(_ rig: (model: PostsModel, home: URL, mentor: MentorSessionModel, fakeDir: String)) throws -> String {
try String(contentsOfFile: (rig.fakeDir as NSString).appendingPathComponent("hermes-acp-spawn-log"), encoding: .utf8)
/// How many `museion` (chat-profile) engines spawned — the fresh-judge
/// pin's countable (was: grep of the fake's spawn log).
private func museionSpawns(_ rig: ScriptedRig) -> Int {
rig.engines.spawnedProfiles.filter { $0 == "museion" }.count
}
@Test func `blind read judges on a fresh chat-profile engine never rostered`() async throws {
let rig = try makeBlindRig()
let rig = try makeScriptedRig()
defer { rig.mentor.shutdown() }
await rig.mentor.openSession()
let rosterBefore = rig.mentor.roster.count
@@ -159,18 +151,12 @@ struct BlindReadTests {
#expect(rig.mentor.roster.count == rosterBefore)
// A fresh CHAT-profile engine judged (the resident engine is the
// only other `museion` spawn; pass profiles never ran).
let spawns = try spawnLog(rig)
#expect(spawns.components(separatedBy: "\n").filter { $0.contains("acp -p museion") }.count == 2)
#expect(!containsPassProfile(spawns))
}
/// Non-`museion` (pass-profile) spawns — negation helper.
private func containsPassProfile(_ spawns: String) -> Bool {
spawns.components(separatedBy: "\n").contains { $0.contains("museion-pass") }
#expect(museionSpawns(rig) == 2)
#expect(!rig.engines.spawnedProfiles.contains { $0.contains("museion-pass") })
}
@Test func `cancelled blind read leaves no reveal`() async throws {
let rig = try makeBlindRig(mode: "cancelOnPrompt")
let rig = try makeScriptedRig(mode: "cancelOnPrompt")
defer { rig.mentor.shutdown() }
await rig.mentor.openSession()
_ = try await openJudgedPiece(on: rig.model, home: rig.home, snapshot: "Older prose.", current: "Newer prose.")
@@ -188,7 +174,7 @@ struct BlindReadTests {
// MARK: Audit pins (finish-audit: the refusal gate + the Cancel rail)
@Test func `refused blind read reveals nothing`() async throws {
let rig = try makeBlindRig(mode: "refuseOnPrompt")
let rig = try makeScriptedRig(mode: "refuseOnPrompt")
defer { rig.mentor.shutdown() }
await rig.mentor.openSession()
_ = try await openJudgedPiece(on: rig.model, home: rig.home, snapshot: "Older prose.", current: "Newer prose.")
@@ -208,7 +194,7 @@ struct BlindReadTests {
}
@Test func `cancel ends a held blind read and the rail is exclusive`() async throws {
let rig = try makeBlindRig(mode: "hangOnPrompt")
let rig = try makeScriptedRig(mode: "hangOnPrompt")
defer { rig.mentor.shutdown() }
await rig.mentor.openSession()
_ = try await openJudgedPiece(on: rig.model, home: rig.home, snapshot: "Older prose.", current: "Newer prose.")
@@ -243,7 +229,7 @@ struct BlindReadTests {
// MARK: Scenario 3 (model half) — the advisory orchestration
@Test func `blind read sends both bodies blind and records nothing`() async throws {
let rig = try makeBlindRig()
let rig = try makeScriptedRig()
defer { rig.mentor.shutdown() }
await rig.mentor.openSession()
_ = try await openJudgedPiece(
@@ -254,14 +240,11 @@ struct BlindReadTests {
await rig.model.blindRead(home: rig.home, mentor: rig.mentor)
// The prompt carried BOTH bodies inline — and no path: the judge
// cannot learn which file is which.
let promptLog = try String(
contentsOfFile: (rig.fakeDir as NSString).appendingPathComponent("hermes-acp-prompt-log"),
encoding: .utf8,
)
#expect(promptLog.contains("MARKER-the-older-text"))
#expect(promptLog.contains("MARKER-the-newer-text"))
#expect(!promptLog.contains(".md"))
// cannot learn which file is which (the engines' prompt log).
let asked = rig.engines.engines.flatMap(\.promptLog).map(\.text).joined()
#expect(asked.contains("MARKER-the-older-text"))
#expect(asked.contains("MARKER-the-newer-text"))
#expect(!asked.contains(".md"))
// The reveal landed below the verdict.
#expect(textRows(of: rig.mentor).contains { $0.contains("start snapshot") })
// Records nothing: Pass State untouched, rail idle, no error.
@@ -275,7 +258,7 @@ struct BlindReadTests {
}
@Test func `blind read refuses without a snapshot or without edits`() async throws {
let rig = try makeBlindRig()
let rig = try makeScriptedRig()
defer { rig.mentor.shutdown() }
await rig.mentor.openSession()
let post = try PostStore.writeNew(body: "# Fresh\n\nNo pass yet.", home: rig.home)
@@ -287,7 +270,7 @@ struct BlindReadTests {
// No pass ever ran — refuse, spawn nothing beyond the resident engine.
await rig.model.blindRead(home: rig.home, mentor: rig.mentor)
#expect(rig.model.lastError?.contains("run a Pass") == true)
#expect(try spawnLog(rig).components(separatedBy: "\n").filter { $0.contains("acp -p museion") }.count == 1)
#expect(museionSpawns(rig) == 1)
// Snapshot exists but the document is unchanged since — a vacuous
// read refuses honestly.