From 704e7a83fd4191cce913b53c3527b5eeaae1b2d0 Mon Sep 17 00:00:00 2001 From: Alexander Kucera Date: Tue, 10 Nov 2026 00:34:30 +0100 Subject: [PATCH] fix(session): drain a deferred nil scope as an explicit clear MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit pendingScopePieceTitle: String? conflated 'no pending request' with a pending request whose target is nil (the writer left the piece — New Post, unsaved draft, external guest, nothing open). Both deferral branches stored the target and the drain guarded on the optional, so a deferred nil target was silently dropped: at turn end (sendPrompt, runOneShot) or replay completion (resumeSession) the old piece's activeEntry survived under a nothing-open context, contrary to the scope contract (#176 class). Pending-ness now rides PendingScope (none | scope(String?)) — a nil target drains through scopeHostedSession like any titled target and clears activeEntry. Titled deferrals and the immediate path are unchanged. Verified: SessionScopeTests 4/4 green (both RED pins now green in 45 ms — the clear lands at the drain, not a deadline); full unit bundle 653/653 green; swiftformat 0 files; swiftlint exit 0 (two pre-existing warnings in MentorSessionModel.swift, unchanged kind). --- Museion/MentorSessionModel.swift | 23 ++++++++++++++++------- 1 file changed, 16 insertions(+), 7 deletions(-) diff --git a/Museion/MentorSessionModel.swift b/Museion/MentorSessionModel.swift index e844b28..bddabea 100644 --- a/Museion/MentorSessionModel.swift +++ b/Museion/MentorSessionModel.swift @@ -280,8 +280,15 @@ final class MentorSessionModel { /// Owner ruling (2026-11-07): an open-piece switch that arrived while a /// turn or pass streamed — fires at turn end /// (firePendingScopeIfRequested); an in-flight transcript is never - /// torn down mid-stream. - private var pendingScopePieceTitle: String? + /// torn down mid-stream. Pending-ness rides the enum, NEVER the + /// target's optionality: a nil target is an explicit clear (nothing + /// open hosts nothing), not "no pending request". + private enum PendingScope: Equatable { + case none + case scope(String?) + } + + private var pendingScope: PendingScope = .none /// #137: per-pass readiness — refreshed at pane entry (re-entry /// re-fires the pane's .task — the key-added-in-Settings re-enable /// path). #181: computed in the PassReadiness module over the provider @@ -589,7 +596,7 @@ final class MentorSessionModel { func scopeHostedSession(toOpenPieceTitle pieceTitle: String?) async { guard phase.isReady else { return } guard !isStreaming else { - pendingScopePieceTitle = pieceTitle + pendingScope = .scope(pieceTitle) return } // Finish-audit: a replay in flight defers the switch too — the @@ -597,10 +604,10 @@ final class MentorSessionModel { // "Resume refused" row into the transcript (the replay's // completion re-fires the pending scope). guard replayingSessionId == nil else { - pendingScopePieceTitle = pieceTitle + pendingScope = .scope(pieceTitle) return } - pendingScopePieceTitle = nil + pendingScope = .none let target = SessionRoster.hostedSessionId(forOpenPieceTitle: pieceTitle, in: roster) guard activeEntry?.sessionId != target else { return } guard let entry = roster.first(where: { $0.sessionId == target }) else { @@ -1064,9 +1071,11 @@ extension MentorSessionModel { /// Owner ruling (2026-11-07): a deferred open-piece switch fires at turn /// end — from BOTH turn-end defers (chat sendPrompt and the pass run), /// after `isStreaming` cleared, so the scope rail re-evaluates for real. + /// A deferred nil target drains too — it is an explicit clear, so the + /// old piece's hosting must not survive the drain. private func firePendingScopeIfRequested() { - guard let title = pendingScopePieceTitle else { return } - pendingScopePieceTitle = nil + guard case let .scope(title) = pendingScope else { return } + pendingScope = .none Task { await scopeHostedSession(toOpenPieceTitle: title) } }