fix(editor): quit rides the departure gate

shutdown() flushed both sessions best-effort and ignored the
results — quitting with an unanswered #67 conflict wrote local
text over the foreign disk body (flush() holds for conflicts
only in the debounce), and a failed quit-time flush silently
dropped the only copy of the edits.

Quit now routes through the same departure gate as navigation:
documentsMayDepart runs both sessions through sessionMayDepart,
and the failed-flush-at-quit posture is BLOCK QUIT —
applicationShouldTerminate (quitReply(posts:), before the Cold
Turkey ask) refuses termination with one spoken reason via a
hosted-guarded alert. PostsModel.active (weak, registered by
AppGateState.init — the WritingLockState.active precedent) is
the delegate's reachability.

Finish-audit fixes folded in: sessionMayDepart refuses a
never-loaded session's retyped words (its flush is a vacuous
success — the same gap closed for navigation); shutdown stays
void (no production reader of a Bool).

+5 pins (conflict hold, clean flush, guest flush, never-loaded
refusal, delegate refusal incl. chmod-injected flush failure),
all flip-verified. Rail 708/708; format+lint clean.

Closes #200
This commit is contained in:
AlexKucera committed 2026-11-19 13:18:19 +01:00
1 parent 55be3d1a2f
commit 2334afc392
6 files changed
+198 -2

No files matched your search

@@ -12,6 +12,16 @@
// stage their temp file beside the destination), the permission-
// injection convention of FixCardApplyTests.
//
// #200 (gate quit-time flushes through the departure gate): quitting is
// the one departure that used to bypass the gate — shutdown() flushed
// both sessions best-effort. Pins: a quit-time shutdown with an
// unanswered #67 conflict leaves the foreign disk body untouched (the
// #114 harm); the open external guest's edits ride the shutdown flush;
// a never-loaded session with retyped words refuses (its flush would
// report a vacuous success and drop the only copy — finish-audit LOW).
// The block-quit posture itself (applicationShouldTerminate's refusal)
// is pinned in WritingLockTests at the delegate seam.
//
import Foundation
@testable import Museion
@@ -179,4 +189,76 @@ struct PostsModelDepartureTests {
#expect(model.lastError != nil)
#expect(PostStore.readBody(id: post.id, home: home) == "# A\n")
}
@Test func `quit-time shutdown leaves the foreign disk body untouched while the conflict alert is unanswered`() throws {
let (model, home) = try makeModelHome()
let post = try PostStore.writeNew(body: "# A\n", home: home)
model.select(post.id)
model.session?.edit("# A\n\nlocal paragraph")
// #67, for real this time: the foreign body is ON disk (quit's
// flush() never held for a conflict — only the debounce did, so
// shutdown used to write the local text over it, #114's harm).
try PostStore.overwrite(id: post.id, body: "foreign body from disk", home: home)
model.session?.diskDidChange(diskText: "foreign body from disk")
model.shutdown()
// AC #200: the foreign disk body survives the quit-time flush and
// the strip says why instead of pretending it saved.
#expect(PostStore.readBody(id: post.id, home: home) == "foreign body from disk")
#expect(model.lastError != nil)
}
@Test func `quit-time shutdown flushes a clean dirty session`() throws {
let (model, home) = try makeModelHome()
let post = try PostStore.writeNew(body: "# A\n", home: home)
model.select(post.id)
model.session?.edit("# A\n\nlast words before quitting")
model.shutdown()
// No conflict, no failure: the edits land and quit proceeds clean.
#expect(PostStore.readBody(id: post.id, home: home) == "# A\n\nlast words before quitting")
#expect(model.lastError == nil)
}
@Test func `quit-time shutdown flushes the open external guest`() throws {
let (model, _) = try makeModelHome()
let extDir = FileManager.default.temporaryDirectory
.appendingPathComponent("museion-departure-\(UUID().uuidString)", isDirectory: true)
try FileManager.default.createDirectory(at: extDir, withIntermediateDirectories: true)
defer { try? allowWrites(to: extDir) }
let extFile = extDir.appendingPathComponent("notes.md")
try "# Foreign\n".write(to: extFile, atomically: true, encoding: .utf8)
model.openExternal(extFile)
model.externalSession?.edit("# Foreign\n\nguest words that must land before quitting")
model.shutdown()
// The guest leg of the quit gate: its edits ride the shutdown
// flush, exactly as the old best-effort shutdown did.
#expect(try String(contentsOf: extFile, encoding: .utf8) == "# Foreign\n\nguest words that must land before quitting")
#expect(model.lastError == nil)
}
@Test func `quit-time shutdown refuses over a never-loaded session with retyped text`() throws {
let (model, home) = try makeModelHome()
let post = try PostStore.writeNew(body: "# A\n", home: home)
// The file turns unreadable between scan and open: the session
// opens empty (loadFailed), the writer retypes into it.
let file = try #require(PostStore.folder(of: post.id, home: home))
.appendingPathComponent("post.md")
try FileManager.default.setAttributes([.posixPermissions: 0o000], ofItemAtPath: file.path)
defer { try? FileManager.default.setAttributes([.posixPermissions: 0o644], ofItemAtPath: file.path) }
model.select(post.id)
#expect(model.session?.loadFailed == true)
model.session?.edit("# Retyped words over a file that never loaded")
model.shutdown()
// flush() reports a vacuous success for a never-loaded session —
// the gate must refuse instead of letting the retype die quietly.
#expect(model.lastError != nil)
#expect(model.session?.text == "# Retyped words over a file that never loaded")
}
}
+38
View File
@@ -253,6 +253,44 @@ struct WritingLockTests {
#expect(delegate.applicationShouldTerminate(NSApplication.shared) == .terminateNow)
}
// MARK: - #200: the quit-time departure gate
@Test @MainActor
func `quit refuses while an open piece cannot depart and proceeds once it can`() throws {
// The failed-flush-at-quit posture, pinned: block quit (this
// delegate's own refusal precedent) — never silently drop the
// only copy of the edits. quitReply is the pure decision seam;
// the refusal ALERT itself is hosted-guarded like the kiosk.
let delegate = WritingLockAppDelegate()
// No model alive → nothing open, nothing to lose.
#expect(delegate.quitReply(posts: nil) == .terminateNow)
// A dirty-but-clean-writable session departs: quit proceeds and
// the flush lands.
let (model, home) = try makeModelHome()
let post = try PostStore.writeNew(body: "# A\n", home: home)
model.select(post.id)
model.session?.edit("# A\n\nlast words before quitting")
#expect(delegate.quitReply(posts: model) == .terminateNow)
#expect(PostStore.readBody(id: post.id, home: home) == "# A\n\nlast words before quitting")
// An unanswered #67 conflict refuses termination — the foreign
// disk body must survive the quit attempt.
try PostStore.overwrite(id: post.id, body: "foreign body from disk", home: home)
model.session?.diskDidChange(diskText: "foreign body from disk")
#expect(delegate.quitReply(posts: model) == .terminateCancel)
#expect(PostStore.readBody(id: post.id, home: home) == "foreign body from disk")
// A failed flush refuses termination too — the pinned posture.
model.session?.keepLocal() // #67 No: the writer keeps their words
let pieceDir = try #require(PostStore.folder(of: post.id, home: home))
try FileManager.default.setAttributes([.posixPermissions: 0o555], ofItemAtPath: pieceDir.path)
defer { try? FileManager.default.setAttributes([.posixPermissions: 0o755], ofItemAtPath: pieceDir.path) }
#expect(delegate.quitReply(posts: model) == .terminateCancel)
#expect(model.lastError != nil)
}
/// Bounded poll for the watched scan to surface a piece (the
/// PostsModelTests poll pattern — the FSEvents latency window).
@MainActor