fix(editor): Home repoint rides the departure gate; document identity is (Home, ID)
CI / ci (push) Canceled after 0s

Settings committed the new Home and bind flipped watchedHome without
departing the open session: the session's immutable target kept the
OLD Home, a same-slug piece in the new library made the ID-only
reselect return it, and autosave/Save/quit wrote the old library
while structural operations resolved against the new one. #198's
departure gate never saw this seam; a no-collision piece was torn
down by the vanish path with its dirty buffer dropped.

The gate runs BEFORE anything commits. PostsModel.repoint(to:) is a
document departure through the same sessionMayDepart (all three legs:
unanswered #67, failed flush, never-loaded retype — no parallel
gate); on refusal nothing changes (preference, watchedHome, the
session's (A, id) target, its buffer), on acceptance the departed
piece's state dies with it. AppGateState.repointMuseionHome(to:)
commits the preference and its persistence only after the gate;
WorkspaceView's Settings binding routes through it and the root
.task(id:) binds the model after that commit — the dumb binder. A
refusal speaks the #198 reason the way #200's quit refusal does
(hosted-guarded alert: no Posts strip is in sight from Settings).
Nothing auto-reopens after an accepted repoint — the writer picks
from the new list; the in-memory draft survives (no Home until first
save; dropping it would drop the only copy).

Document identity is (Home, ID) everywhere — EditorSession.targets
is the pair, PostsModel.openSessionTargets the model-side question;
every ID-only session comparison is retired: select's reselect
no-op, openUnguarded, the structural rail (rename/trash/move/archive/
restore/unarchive + flushOrAbort), conversion, the pass guard (its
answer now carries the Home it validated), Micropub's launch, the
lock's arm/body reads. Async continuations re-validate identity at
COMPLETION: refreshCards (launch and hop-back — the old ID-only guard
let a scan from the previous library land its cards on the new
library's same-slug piece) and rescan (the stale-Home verdict is
hoisted: a scan that outlived a repoint applies nothing, not just
the vanish check; the #67 disk read runs only for a session targeting
the scan's own Home). scanPieceCards is the per-model injectable
scanner the deterministic stale-scan pin needs.

Tests: PostsModelRepointTests (7) move to the gated entry and go
green (17/17 with PostsModelDepartureTests — the #198 matrix stays
green and composes); the stale-scan pin's RED is a mutation check
(completion guard reverted to ID-only → only that pin fails, cards
→ ["Hedging"]; restored byte-exact). The #179 mid-pass repoint pin
drives the real Settings path now (repoint + bind). swiftformat 0/152,
swiftlint 0.
This commit is contained in:
AlexKucera committed 2026-11-23 20:23:57 +01:00
1 parent bce6abb38b
commit 8b0d43aed8
12 files changed
+298 -72

No files matched your search

+88 -15
View File
@@ -17,18 +17,35 @@
// (flush AND quit), a no-collision repoint tears down cleanly (the old
// vanish path dropped the dirty buffer), a card scan from the old library
// never lands on the new library's piece (identity re-check at
// COMPLETION), and a pass started after the repoint runs against the new
// library.
// COMPLETION — the scan seam holds it past the repoint), and a pass
// started after the repoint runs against the new library.
//
// Two-Home rig: `projects/posts/shared/post.md` in BOTH libraries — the
// collision shape the finding names; differing bodies so a wrong-library
// read is observable.
// read is observable. The Settings path in these pins is the model's two
// steps — `repoint(to:)` (the gate) then `bind` (the commit) — exactly as
// WorkspaceView's binding and root `.task(id:)` drive them.
//
import Foundation
@testable import Museion
import Testing
/// brief-28 rig: a Sendable one-shot flag the stale card-scan pin's park
/// probe reads (the detached scanner sets it from off-main; pollUntil's
/// per-iteration probe must not consume anything).
private final class ParkedFlag: @unchecked Sendable {
private let lock = NSLock()
private var value = false
var isSet: Bool {
lock.withLock { value }
}
func set() {
lock.withLock { value = true }
}
}
@Suite("PostsModelRepointTests")
@MainActor
struct PostsModelRepointTests {
@@ -65,7 +82,17 @@ struct PostsModelRepointTests {
#expect(model.session?.text == "# Shared\n\nA body\n")
model.session?.edit("# Shared\n\nA body, typed in A\n")
model.bind(home: homeB) // the Settings repoint
// The Settings repoint: the gate first, the binding after.
#expect(model.repoint(to: homeB))
// Ordering: the departure landed on A and NOTHING has moved yet —
// watchedHome is still A until the binding commits, and no live
// session targets an A piece.
#expect(PostStore.readBody(id: "shared", home: homeA) == "# Shared\n\nA body, typed in A\n")
#expect(model.watchedHome == homeA)
#expect(model.session == nil)
#expect(model.selectedPostID == nil)
model.bind(home: homeB)
#expect(model.watchedHome == homeB)
model.select("shared")
// B's piece opened from B — not the retained A session (whose
@@ -91,7 +118,8 @@ struct PostsModelRepointTests {
try denyWrites(to: pieceDir)
defer { try? allowWrites(to: pieceDir) }
model.bind(home: homeB) // the Settings repoint
// The Settings repoint — refused: the gate fires before any change.
#expect(model.repoint(to: homeB) == false)
// ORDERING: the gate fired before ANY state changed — the binding,
// the selection, the session's (Home, ID) target and its buffer.
@@ -104,11 +132,13 @@ struct PostsModelRepointTests {
#expect(model.lastError != nil)
#expect(PostStore.readBody(id: "shared", home: homeB) == "# Shared\n\nB body\n")
// Writability restored → the edits land on A first, then the
// repoint goes through and departs the session.
// Writability restored → the edits land on A first (watchedHome
// still A until the binding commits), then the repoint departs.
try allowWrites(to: pieceDir)
model.bind(home: homeB)
#expect(model.repoint(to: homeB))
#expect(PostStore.readBody(id: "shared", home: homeA) == "# Shared\n\nA body, typed in A\n")
#expect(model.watchedHome == homeA)
model.bind(home: homeB)
#expect(model.session == nil)
#expect(model.selectedPostID == nil)
#expect(model.watchedHome == homeB)
@@ -121,7 +151,8 @@ struct PostsModelRepointTests {
// #67: a foreign disk body arrived — the alert is up, unanswered.
model.session?.diskDidChange(diskText: "foreign body from disk")
model.bind(home: homeB) // the Settings repoint
// The Settings repoint — refused.
#expect(model.repoint(to: homeB) == false)
#expect(model.watchedHome == homeA)
#expect(model.selectedPostID == "shared")
@@ -147,7 +178,8 @@ struct PostsModelRepointTests {
#expect(model.session?.loadFailed == true)
model.session?.edit("# Retyped words over a file that never loaded")
model.bind(home: homeB) // the Settings repoint
// The Settings repoint — refused: the retype is the only copy.
#expect(model.repoint(to: homeB) == false)
#expect(model.watchedHome == homeA)
#expect(model.selectedPostID == "shared")
@@ -161,13 +193,14 @@ struct PostsModelRepointTests {
model.select("unique")
model.session?.edit("# Unique\n\nA body, typed in A\n")
model.bind(home: homeB) // the Settings repoint
// The old session's edits landed on A BEFORE anything moved — the
// vanish path used to tear it down without a flush.
// The Settings repoint — accepted: the old session's edits landed
// on A BEFORE anything moved (the vanish path used to tear it down
// without a flush).
#expect(model.repoint(to: homeB))
#expect(PostStore.readBody(id: "unique", home: homeA) == "# Unique\n\nA body, typed in A\n")
#expect(model.session == nil)
#expect(model.selectedPostID == nil)
model.bind(home: homeB)
// No ghost: the new library's rescan leaves nothing selected, nothing open.
try await pollUntil { model.posts.contains { $0.id == "shared" } }
#expect(model.session == nil)
@@ -175,6 +208,44 @@ struct PostsModelRepointTests {
#expect(model.watchedHome == homeB)
}
@Test func `a card scan from the old library never lands on the new library's same-slug piece`() async throws {
let (model, homeA, homeB) = try makeTwoHomes(bodyA: "# Shared\n\nA body\n", bodyB: "# Shared\n\nB body\n")
let folderA = try #require(PostStore.folder(of: "shared", home: homeA))
let folderB = try #require(PostStore.folder(of: "shared", home: homeB))
_ = try FixCardStore.submit(piecePath: folderA.path, cards: [card(pattern: "Hedging", quote: "A body")])
_ = try FixCardStore.submit(piecePath: folderB.path, cards: [card(pattern: "Telling", quote: "B body")])
// HOLD the old library's scan past the repoint: the injected scanner
// parks on A's folder until the test releases it (a detached disk
// read is otherwise faster than any arrangement); B's scans run
// free. The park probe is a flag, never a consuming semaphore wait —
// pollUntil re-evaluates its condition per iteration.
let parked = ParkedFlag()
let release = DispatchSemaphore(value: 0)
model.scanPieceCards = { url in
if url == folderA {
parked.set()
_ = release.wait(timeout: .now() + 30) // bounded: a failed test must not leak a parked thread
}
return FixCardStore.scan(pieceURL: url)
}
model.select("shared") // the A scan parks inside the detached read
try await pollUntil { parked.isSet }
// The repoint, then B's same-slug piece opens and ITS scan lands.
#expect(model.repoint(to: homeB))
model.bind(home: homeB)
model.select("shared")
try await pollUntil { model.openPieceCards.map(\.pattern) == ["Telling"] }
#expect(model.openPieceCards.map(\.pattern) == ["Telling"])
// Release the stale A scan: its continuation completes AFTER the new
// library's piece is open — identity at completion, never the id.
release.signal()
release.signal() // a second A scan, if one ever queued, must not park the teardown
try await Task.sleep(nanoseconds: 200_000_000) // let the hop-back land
#expect(model.openPieceCards.map(\.pattern) == ["Telling"]) // never A's cards
}
@Test func `a pass started after the repoint runs against the new library`() async throws {
let (model, homeA, mentor) = try makePassRig()
defer { mentor.shutdown() }
@@ -187,7 +258,9 @@ struct PostsModelRepointTests {
model.select("shared")
#expect(model.session?.text == "# Shared\n\nA body\n")
model.bind(home: homeB) // the Settings repoint
// The Settings repoint: gate, then binding.
#expect(model.repoint(to: homeB))
model.bind(home: homeB)
model.select("shared")
#expect(model.session?.text == "# Shared\n\nB body\n")