CI / ci (pull_request) Canceled after 0s
CI / ci (push) Canceled after 0s
Three-reviewer fanout over PR #184's deferred nil-scope fix: - pin the titled drain's APPLY half (unpinned suite-wide) over a new hangOnPromptLoad rig mode (prompt parks, load answers); flip-verified against a nil-only drain guard - cut the dead Equatable on PendingScope (converged finding) - name the same-turn pendingRestart ceiling on the drain: respawn's .starting window drops the consumed scope; consume-at-apply is the upgrade path (follow-up ticket) - changelog + devlog for the PR; AGENTS.md rails refreshed Suite 654 executed / 0 failed; swiftformat clean; swiftlint unchanged (both warnings pre-exist on main).
250 lines
11 KiB
Swift
250 lines
11 KiB
Swift
//
|
|
// SessionScopeTests.swift
|
|
// MuseionTests
|
|
//
|
|
// Owner ruling (2026-11-07): the Mentor pane hosts the OPEN PIECE's
|
|
// session — the pure seam over the roster (exact pieceTitle match,
|
|
// newest-first) pinned here: match, no-match, and title-with-no-roster.
|
|
// #178: the defer pin runs IN-PROCESS over a ScriptedEngines rig at the
|
|
// EngineTransport seam — no child process.
|
|
// deferred-empty-piece-scope-lost: the DEFERRED nil-target pins — leaving
|
|
// the piece mid-stream or mid-replay must CLEAR the hosting at drain
|
|
// time (nil is an explicit clear target, not "no pending request").
|
|
//
|
|
|
|
import Foundation
|
|
@testable import Museion
|
|
import Testing
|
|
|
|
@Suite("SessionScopeTests")
|
|
struct SessionScopeTests {
|
|
/// The roster is newest-first (the model's own order), so the FIRST
|
|
/// exact pieceTitle match is the newest session for the piece. No
|
|
/// match — including near-misses, no open piece, or no roster at all —
|
|
/// hosts nothing: nil, the pane's honest empty presentation.
|
|
@Test func `hosted session id picks newest exact piece title match`() {
|
|
let older = SessionEntry(
|
|
sessionId: "s-old", name: "Old", pieceTitle: "Essay X",
|
|
createdAt: Date(timeIntervalSinceNow: -600),
|
|
)
|
|
let newer = SessionEntry(sessionId: "s-new", name: "New", pieceTitle: "Essay X", createdAt: Date())
|
|
let other = SessionEntry(sessionId: "s-other", name: "Other", pieceTitle: "Draft Y", createdAt: Date())
|
|
let roster = [newer, older, other] // newest-first, as the model keeps it
|
|
|
|
// Match — the newest session for the piece wins; a different piece
|
|
// finds its own.
|
|
#expect(SessionRoster.hostedSessionId(forOpenPieceTitle: "Essay X", in: roster) == "s-new")
|
|
#expect(SessionRoster.hostedSessionId(forOpenPieceTitle: "Draft Y", in: roster) == "s-other")
|
|
// No match — EXACT match only; near-misses never host.
|
|
#expect(SessionRoster.hostedSessionId(forOpenPieceTitle: "essay x", in: roster) == nil)
|
|
#expect(SessionRoster.hostedSessionId(forOpenPieceTitle: "Unwritten", in: roster) == nil)
|
|
// No open piece (draft, external guest, nothing) hosts nothing.
|
|
#expect(SessionRoster.hostedSessionId(forOpenPieceTitle: nil, in: roster) == nil)
|
|
// A title with no roster behind it hosts nothing.
|
|
#expect(SessionRoster.hostedSessionId(forOpenPieceTitle: "Essay X", in: []) == nil)
|
|
}
|
|
|
|
/// Finish-audit: the scope rail is an AUTOMATIC action — when the one
|
|
/// replay slot is busy (another session's session/load in flight), the
|
|
/// switch DEFERS like a stream (old behavior: resumeSession refused and
|
|
/// appended a user-visible "Resume refused" row for an action the writer
|
|
/// never took). Pinned over the scripted rig's `loadReplayHold` mode
|
|
/// parking the replay; teardown closes the parked engine, so the resume
|
|
/// task is deliberately never awaited.
|
|
@MainActor
|
|
@Test func `scope switch defers behind an in-flight replay without refusing`() async throws {
|
|
let rosterDir = acpTempDir("scope-defer")
|
|
let engines = ScriptedEngines(mode: "loadReplayHold")
|
|
let model = engines.makeMentor(rosterDirectory: rosterDir)
|
|
defer { model.shutdown() }
|
|
await model.openSession() // empty roster — creates the default session, completes
|
|
guard case .ready = model.phase else {
|
|
Issue.record("expected ready, got \(model.phase)")
|
|
return
|
|
}
|
|
// Two rostered pieces through the real flow; B's session is active.
|
|
guard await model.newSession(name: "On A", pieceTitle: "Piece A") != nil,
|
|
await model.newSession(name: "On B", pieceTitle: "Piece B") != nil
|
|
else {
|
|
Issue.record("session creation refused")
|
|
return
|
|
}
|
|
let entryA = try #require(model.roster.first { $0.pieceTitle == "Piece A" })
|
|
let activeBefore = model.activeEntry?.sessionId
|
|
// Park A's replay behind the hold — the load sits in flight, the
|
|
// slot stays taken, and B remains the pane's surface.
|
|
_ = Task { await model.resumeSession(entryA) }
|
|
var held = false
|
|
for _ in 0 ..< 250 where !held {
|
|
held = model.stderrTail.contains { $0.contains("hold-load") }
|
|
if !held {
|
|
try await Task.sleep(nanoseconds: 20_000_000)
|
|
}
|
|
}
|
|
#expect(held)
|
|
await model.scopeHostedSession(toOpenPieceTitle: "Piece A")
|
|
let refused = model.rows.contains { row in
|
|
if case let .text(text, _, _) = row.content {
|
|
return text.contains("Resume refused")
|
|
}
|
|
return false
|
|
}
|
|
#expect(!refused)
|
|
// The switch applied nowhere yet — deferred, not refused and not
|
|
// swapped behind the replay.
|
|
#expect(model.activeEntry?.sessionId == activeBefore)
|
|
}
|
|
|
|
/// deferred-empty-piece-scope-lost (a): a nil scope request (the writer
|
|
/// left the piece — New Post, unsaved draft, external guest, nothing
|
|
/// open) arriving while a turn STREAMS defers like any switch, and the
|
|
/// drain at turn end must CLEAR the hosting — the old piece's
|
|
/// conversation may not survive under a nothing-open context. Pinned
|
|
/// over the scripted rig's `hangOnPrompt` mode parking the turn;
|
|
/// `stopStreaming` ends it (the sendPrompt drain fires).
|
|
@MainActor
|
|
@Test func `deferred nil scope during a stream clears hosting at turn end`() async throws {
|
|
let rosterDir = acpTempDir("scope-nil-stream")
|
|
let engines = ScriptedEngines(mode: "hangOnPrompt")
|
|
let model = engines.makeMentor(rosterDirectory: rosterDir)
|
|
defer { model.shutdown() }
|
|
await model.openSession() // empty roster — creates the default session, completes
|
|
guard case .ready = model.phase else {
|
|
Issue.record("expected ready, got \(model.phase)")
|
|
return
|
|
}
|
|
guard await model.newSession(name: "On A", pieceTitle: "Piece A") != nil else {
|
|
Issue.record("session creation refused")
|
|
return
|
|
}
|
|
// The turn parks mid-stream (hangOnPrompt) — isStreaming holds.
|
|
_ = Task { await model.sendPrompt("hold this") }
|
|
var streaming = false
|
|
for _ in 0 ..< 250 where !streaming {
|
|
streaming = model.isStreaming
|
|
if !streaming {
|
|
try await Task.sleep(nanoseconds: 20_000_000)
|
|
}
|
|
}
|
|
#expect(streaming)
|
|
// The writer leaves the piece mid-stream — the clear defers to turn
|
|
// end; the in-flight transcript stays up.
|
|
await model.scopeHostedSession(toOpenPieceTitle: nil)
|
|
#expect(model.activeEntry?.pieceTitle == "Piece A")
|
|
// Cancel ends the turn — the deferred clear must land now.
|
|
model.stopStreaming()
|
|
var cleared = false
|
|
for _ in 0 ..< 250 where !cleared {
|
|
cleared = !model.isStreaming && model.activeEntry == nil
|
|
if !cleared {
|
|
try await Task.sleep(nanoseconds: 20_000_000)
|
|
}
|
|
}
|
|
#expect(cleared)
|
|
}
|
|
|
|
/// The titled twin of (a), pinning the drain's APPLY half — no test
|
|
/// anywhere drove a deferred NON-nil switch to its applied state (the
|
|
/// replay-deferral pin never releases its hold; the nil pins activate
|
|
/// nothing). Same rig shape over the `hangOnPromptLoad` mode (prompt
|
|
/// parks, load answers): stopStreaming ends the turn, the drain must
|
|
/// activate the target piece's session.
|
|
@MainActor
|
|
@Test func `deferred titled scope during a stream applies at turn end`() async throws {
|
|
let rosterDir = acpTempDir("scope-titled-stream")
|
|
let engines = ScriptedEngines(mode: "hangOnPromptLoad")
|
|
let model = engines.makeMentor(rosterDirectory: rosterDir)
|
|
defer { model.shutdown() }
|
|
await model.openSession() // empty roster — creates the default session, completes
|
|
guard case .ready = model.phase else {
|
|
Issue.record("expected ready, got \(model.phase)")
|
|
return
|
|
}
|
|
// B first, then A — A is the active surface (newest), B the target.
|
|
guard await model.newSession(name: "On B", pieceTitle: "Piece B") != nil,
|
|
await model.newSession(name: "On A", pieceTitle: "Piece A") != nil
|
|
else {
|
|
Issue.record("session creation refused")
|
|
return
|
|
}
|
|
#expect(model.activeEntry?.pieceTitle == "Piece A")
|
|
// The turn parks mid-stream (hangOnPrompt) — isStreaming holds.
|
|
_ = Task { await model.sendPrompt("hold this") }
|
|
var streaming = false
|
|
for _ in 0 ..< 250 where !streaming {
|
|
streaming = model.isStreaming
|
|
if !streaming {
|
|
try await Task.sleep(nanoseconds: 20_000_000)
|
|
}
|
|
}
|
|
#expect(streaming)
|
|
// The writer switches to B mid-stream — the switch defers to
|
|
// turn end; A stays the pane's surface.
|
|
await model.scopeHostedSession(toOpenPieceTitle: "Piece B")
|
|
#expect(model.activeEntry?.pieceTitle == "Piece A")
|
|
// Cancel ends the turn — the deferred switch must apply now.
|
|
model.stopStreaming()
|
|
var applied = false
|
|
for _ in 0 ..< 250 where !applied {
|
|
applied = !model.isStreaming && model.activeEntry?.pieceTitle == "Piece B"
|
|
if !applied {
|
|
try await Task.sleep(nanoseconds: 20_000_000)
|
|
}
|
|
}
|
|
#expect(applied)
|
|
}
|
|
|
|
/// deferred-empty-piece-scope-lost (b): the same nil-scope loss when the
|
|
/// request arrives while a session/load REPLAYS — the replay's
|
|
/// completion drains the pending scope; the explicit nil target must
|
|
/// clear the hosting the replay just activated, or the old piece's
|
|
/// conversation survives under a nothing-open context. Pinned over the
|
|
/// scripted rig's `loadReplayHold` mode parking the replay;
|
|
/// `releaseHeldLoad()` completes it (the resume drain fires).
|
|
@MainActor
|
|
@Test func `deferred nil scope behind a replay clears hosting at replay end`() async throws {
|
|
let rosterDir = acpTempDir("scope-nil-replay")
|
|
let engines = ScriptedEngines(mode: "loadReplayHold")
|
|
let model = engines.makeMentor(rosterDirectory: rosterDir)
|
|
defer { model.shutdown() }
|
|
await model.openSession() // empty roster — creates the default session, completes
|
|
guard case .ready = model.phase else {
|
|
Issue.record("expected ready, got \(model.phase)")
|
|
return
|
|
}
|
|
// Two rostered pieces through the real flow; B's session is active.
|
|
guard await model.newSession(name: "On A", pieceTitle: "Piece A") != nil,
|
|
await model.newSession(name: "On B", pieceTitle: "Piece B") != nil
|
|
else {
|
|
Issue.record("session creation refused")
|
|
return
|
|
}
|
|
let entryA = try #require(model.roster.first { $0.pieceTitle == "Piece A" })
|
|
// Park A's replay behind the hold — the load sits in flight, the
|
|
// slot stays taken, and B remains the pane's surface.
|
|
_ = Task { await model.resumeSession(entryA) }
|
|
var held = false
|
|
for _ in 0 ..< 250 where !held {
|
|
held = model.stderrTail.contains { $0.contains("hold-load") }
|
|
if !held {
|
|
try await Task.sleep(nanoseconds: 20_000_000)
|
|
}
|
|
}
|
|
#expect(held)
|
|
// The writer leaves the piece while the replay streams into A's
|
|
// store — the clear defers behind the replay.
|
|
await model.scopeHostedSession(toOpenPieceTitle: nil)
|
|
// Release: the replay completes and activates A, then the deferred
|
|
// nil scope must clear the hosting.
|
|
engines.releaseHeldLoad()
|
|
var cleared = false
|
|
for _ in 0 ..< 250 where !cleared {
|
|
cleared = model.activeEntry == nil
|
|
if !cleared {
|
|
try await Task.sleep(nanoseconds: 20_000_000)
|
|
}
|
|
}
|
|
#expect(cleared)
|
|
}
|
|
}
|