fix(session): drain a deferred nil scope as an explicit clear
CI / ci (push) Canceled after 0s
CI / ci (pull_request) Canceled after 0s

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).
This commit is contained in:
AlexKucera committed 2026-11-10 00:34:30 +01:00
1 parent 8531d798bc
commit 836da5833d
1 file changed
+16 -7
+16 -7
View File
@@ -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 (refreshPassReadiness, same-file #137
/// extension) + the injectable read seam (tests). Stored in the class
/// body — extensions carry no stored state.
@@ -585,7 +592,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
@@ -593,10 +600,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 {
@@ -1103,9 +1110,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) }
}