feat(mentor): blind read A/B judgment (#122)
CI / ci (push) Canceled after 0s

A purposeful writer action on the open post — the "Blind Read" button
beside Run Pass — triggers a whole-document advisory judgment: the
current document against the Pass-start snapshot (the newest
passes/pass-N.md on disk), decided by a FRESH session of the same
museion profile over the existing one-shot ACP rail. Both versions
travel inline in one prompt under neutral A/B position labels with
randomized order — the judge never learns which is the rewrite — and
the A/B-to-origin reveal appends only after the verdict, so the mapping
cannot leak; cancelled, refused, and spawn-failed turns reveal nothing.

Advisory-only per decision #120: no Pass State advance, no cards, no
Learnings, the roster never grows — the verdict is display-only and
dies with the transient transcript. The writer stays judge of record;
demonstrator picks and Fix Card status are untouched. Honest refusals
for the degenerate states: no snapshot yet, an unchanged document, or
a busy rail (passes and reads refuse each other; the pass bar's one
Cancel ends either).

The pass engine's spawn/session/teardown block became one shared
runOneShot rail (failure rows speak the run's own name), the pass-bar
button gates on a derived openPieceHasSnapshot flag, the fake ACP
gained a prompt-log observable, and the GLOSSARY "Blind Read" term
landed with the issue.

Finish-audit (three reviewers): a refusal still ends .ended, so the
reveal now gates on the stopReason (flip-verified pin), plus the
Cancel-rail and pass/blind-read exclusivity pins. Suite 500; swiftformat
clean; swiftlint adds no violations over the HEAD baseline.

Closes #122
This commit is contained in:
AlexKucera committed 2026-10-30 17:31:00 +01:00
1 parent 4724e00b73
commit 97e86cf62d
15 files changed
+627 -32

No files matched your search

+306
View File
@@ -0,0 +1,306 @@
//
// BlindReadTests.swift
// MuseionTests
//
// #122: the Blind Read — a writer-triggered, whole-document A/B judgment
// (advisory). From user story #122 / decision #120.
//
// ## Scenario: the blind judge prompt
// Given a Pass-start snapshot and the current document
// When the writer triggers a Blind Read
// Then both versions travel inline in one prompt — origin-free labels,
// randomized order — so the judge cannot tell which is which.
//
// ## Scenario: snapshot selection
// Given a piece whose Passes left snapshots in `<piece>/passes/`
// When a Blind Read triggers
// Then the newest snapshot (either pass kind) is the comparison base;
// with none, the read refuses honestly.
//
// ## Scenario: the advisory run
// Given an open piece with a snapshot and edits since
// When the writer clicks Blind Read
// Then a fresh one-shot engine on the museion profile judges, the verdict
// and a reveal row land in the transient transcript, the roster stays
// untouched, and nothing on disk changes.
//
import Foundation
@testable import Museion
import Testing
@MainActor
struct BlindReadTests {
// MARK: Scenario 1 — the blind judge prompt
@Test func `prompt carries both bodies inline in the given order`() {
let prompt = BlindRead.prompt(
snapshot: "The snapshot prose.",
current: "The current prose.",
snapshotFirst: true,
)
#expect(prompt.contains("Version A:\nThe snapshot prose."))
#expect(prompt.contains("Version B:\nThe current prose."))
}
@Test func `prompt flip swaps which version leads`() {
let prompt = BlindRead.prompt(
snapshot: "The snapshot prose.",
current: "The current prose.",
snapshotFirst: false,
)
#expect(prompt.contains("Version A:\nThe current prose."))
#expect(prompt.contains("Version B:\nThe snapshot prose."))
}
@Test func `prompt never names an origin or a path`() {
let prompt = BlindRead.prompt(
snapshot: "One body.",
current: "Another body.",
snapshotFirst: true,
)
// Blindness: the judge must not learn which version is the
// rewrite — no origin words, no file paths.
#expect(!prompt.lowercased().contains("snapshot"))
#expect(!prompt.lowercased().contains("current"))
#expect(!prompt.lowercased().contains("original"))
#expect(!prompt.lowercased().contains("revised"))
}
@Test func `prompt asks for a pick and one line why`() {
let prompt = BlindRead.prompt(snapshot: "One body.", current: "Another body.", snapshotFirst: true)
#expect(prompt.contains(#""A" or "B""#))
#expect(prompt.lowercased().contains("one line"))
}
@Test func `reveal names the mapping per kind and flip`() {
#expect(
BlindRead.reveal(kind: .pass1, snapshotFirst: true)
== "A = Pass 1 start snapshot · B = current document",
)
#expect(
BlindRead.reveal(kind: .pass2, snapshotFirst: false)
== "A = current document · B = Pass 2 start snapshot",
)
}
// MARK: Scenario 2 — snapshot selection
@Test func `latest snapshot is the newest on disk either kind`() throws {
let home = try storeHome()
let post = try PostStore.writeNew(body: "# Judged\n\nThe base text.", home: home)
// Only pass-1's snapshot exists for the first read.
try PostStore.snapshotPass(id: post.id, kind: .pass1, body: "The pass-one start.", home: home)
#expect(PostStore.latestPassSnapshot(id: post.id, home: home)?.body == "The pass-one start.")
// pass-2's snapshot goes decisively newer on disk — it becomes the base.
try PostStore.snapshotPass(id: post.id, kind: .pass2, body: "The pass-two start.", home: home)
let piece = try #require(PostStore.folder(of: post.id, home: home))
let pass2 = piece.appendingPathComponent("passes/pass-2.md")
try FileManager.default.setAttributes(
[.modificationDate: Date().addingTimeInterval(60)],
ofItemAtPath: pass2.path,
)
let latest = PostStore.latestPassSnapshot(id: post.id, home: home)
#expect(latest?.kind == .pass2)
#expect(latest?.body == "The pass-two start.")
}
// 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
private func openJudgedPiece(on model: PostsModel, home: URL, snapshot: String, current: String) async throws -> String {
let post = try PostStore.writeNew(body: current, home: home)
try PostStore.snapshotPass(id: post.id, kind: .pass1, body: snapshot, home: home)
model.select(post.id, home: home)
for _ in 0 ..< 250 where !model.posts.contains(where: { $0.id == post.id }) {
try await Task.sleep(nanoseconds: 20_000_000)
}
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)
}
@Test func `blind read judges on a fresh chat-profile engine never rostered`() async throws {
let rig = try makeBlindRig()
defer { rig.mentor.shutdown() }
await rig.mentor.openSession()
let rosterBefore = rig.mentor.roster.count
_ = try await openJudgedPiece(on: rig.model, home: rig.home, snapshot: "Older prose.", current: "# Judged\n\nNewer prose.")
let outcome = await rig.mentor.runBlindRead(
prompt: BlindRead.prompt(snapshot: "Older prose.", current: "# Judged\n\nNewer prose.", snapshotFirst: true),
pieceTitle: "Judged",
reveal: BlindRead.reveal(kind: .pass1, snapshotFirst: true),
)
#expect(outcome == .ended(stopReason: "end_turn"))
// The verdict + reveal render in the transient transcript.
#expect(textRows(of: rig.mentor).contains("Blind Read · Judged"))
#expect(textRows(of: rig.mentor).contains("A = Pass 1 start snapshot · B = current document"))
// The roster never grew — records nothing (#129 posture).
#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") }
}
@Test func `cancelled blind read leaves no reveal`() async throws {
let rig = try makeBlindRig(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.")
let outcome = await rig.mentor.runBlindRead(
prompt: "prompt",
pieceTitle: "Judged",
reveal: BlindRead.reveal(kind: .pass1, snapshotFirst: true),
)
#expect(outcome == .cancelled)
#expect(!textRows(of: rig.mentor).contains { $0.contains("start snapshot") })
}
// 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")
defer { rig.mentor.shutdown() }
await rig.mentor.openSession()
_ = try await openJudgedPiece(on: rig.model, home: rig.home, snapshot: "Older prose.", current: "Newer prose.")
// A refusal still ENDS .ended — the reveal must gate on the
// stopReason (runPass's advance posture), never on the outcome
// alone. Realistic path: a 2×-document prompt can refuse on length.
let outcome = await rig.mentor.runBlindRead(
prompt: "prompt",
pieceTitle: "Judged",
reveal: BlindRead.reveal(kind: .pass1, snapshotFirst: true),
)
#expect(outcome == .ended(stopReason: "refusal"))
#expect(textRows(of: rig.mentor).contains { $0.contains("(refused the prompt)") })
#expect(!textRows(of: rig.mentor).contains { $0.contains("start snapshot") })
}
@Test func `cancel ends a held blind read and the rail is exclusive`() async throws {
let rig = try makeBlindRig(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.")
let run = Task { await rig.model.blindRead(home: rig.home, mentor: rig.mentor) }
for _ in 0 ..< 250 where !rig.model.runningBlindRead {
try await Task.sleep(nanoseconds: 20_000_000)
}
#expect(rig.model.runningBlindRead)
// Mutual exclusion — a pass refuses while the read owns the rail.
await rig.model.runPass(.pass1, home: rig.home, mentor: rig.mentor)
#expect(rig.model.lastError?.contains("already running") == true)
// The pass bar's Cancel rail ends the held read — no reveal,
// nothing on disk.
rig.model.cancelPass(mentor: rig.mentor)
for _ in 0 ..< 250 where rig.model.runningBlindRead {
try await Task.sleep(nanoseconds: 20_000_000)
}
#expect(!rig.model.runningBlindRead)
rig.mentor.shutdown() // frees the held turn if the rail regressed — bounds the await
_ = await run.value
#expect(!textRows(of: rig.mentor).contains { $0.contains("start snapshot") })
let onDisk = try String(
contentsOf: rig.home.appendingPathComponent("projects/posts/\(rig.model.selectedPostID!)/post.md"),
encoding: .utf8,
)
#expect(onDisk.contains("pass_state: First Draft"))
}
// 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()
defer { rig.mentor.shutdown() }
await rig.mentor.openSession()
_ = try await openJudgedPiece(
on: rig.model, home: rig.home,
snapshot: "MARKER-the-older-text", current: "# Judged\n\nMARKER-the-newer-text",
)
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"))
// 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.
let onDisk = try String(
contentsOf: rig.home.appendingPathComponent("projects/posts/\(rig.model.selectedPostID!)/post.md"),
encoding: .utf8,
)
#expect(onDisk.contains("pass_state: First Draft"))
#expect(rig.model.runningBlindRead == false)
#expect(rig.model.lastError == nil)
}
@Test func `blind read refuses without a snapshot or without edits`() async throws {
let rig = try makeBlindRig()
defer { rig.mentor.shutdown() }
await rig.mentor.openSession()
let post = try PostStore.writeNew(body: "# Fresh\n\nNo pass yet.", home: rig.home)
rig.model.select(post.id, home: rig.home)
for _ in 0 ..< 250 where !rig.model.posts.contains(where: { $0.id == post.id }) {
try await Task.sleep(nanoseconds: 20_000_000)
}
// 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)
// Snapshot exists but the document is unchanged since — a vacuous
// read refuses honestly.
try PostStore.snapshotPass(id: post.id, kind: .pass1, body: "# Fresh\n\nNo pass yet.", home: rig.home)
await rig.model.blindRead(home: rig.home, mentor: rig.mentor)
#expect(rig.model.lastError?.contains("No edits") == true)
}
/// A throwaway Home (PostLibraryTests' temp-dir pattern).
private func storeHome() throws -> URL {
let url = FileManager.default.temporaryDirectory
.appendingPathComponent("blindread-\(UUID().uuidString)", isDirectory: true)
try FileManager.default.createDirectory(at: url, withIntermediateDirectories: true)
return url
}
}