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

+64 -13
View File
@@ -67,11 +67,55 @@ extension PostsModel {
/// scan + start (or restart) the watcher. Called from WorkspaceView's
/// root `.task(id: museionHome)` — first frame and every Home repoint,
/// pane-independent.
/// brief-28: the dumb binder — it flips `watchedHome` unconditionally.
/// A REPOINT reaches it only after `repoint(to:)` passed its departure
/// gate (the Settings binding runs the gate before committing the
/// preference; the `.task` fires after that commit).
func bind(home: URL) {
startWatching(home: home)
Task { await rescan() }
}
/// brief-28 (home-repoint-retains-old-editor-target): a Home repoint is
/// a DOCUMENT DEPARTURE — the gate runs FIRST, the binding commits
/// after. The Settings path used to flip `watchedHome` under 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).
/// True = every open document landed through `sessionMayDepart` (the
/// three legs: unanswered #67, failed flush, never-loaded retype) and
/// the departed piece's state is cleared — the caller may bind the new
/// Home. False = `lastError` says why and NOTHING changed: the
/// preference stays A, `watchedHome` stays A, the session keeps its
/// (A, id) target and its buffer. No parallel gate — the same one
/// `select`/`newPost`/quit ride.
/// The in-memory draft survives: it has no Home until first save (#62)
/// and lands in the new library — the only alternative is dropping the
/// only copy. A running pass or Blind Read keeps its own completion
/// guard (#179: the advance refuses once `watchedHome` moved).
@discardableResult
func repoint(to newHome: URL) -> Bool {
guard newHome != watchedHome else { return true } // first bind / the same folder: nothing departs
guard departOpenDocuments() else { return false }
// The departed piece's state dies with it: a stale id under the new
// library is a ghost selection (a same-slug piece would read as
// open over an empty editor) and a stale ask would act on the new
// library's piece. Nothing auto-reopens — the writer picks from
// the new list (the reopen machinery stays where it is).
selectedPostID = nil
openPieceCards = []
openPieceHasSnapshot = false
outlineGuardPieceID = nil
proseOverridePostID = nil
pendingConversion = nil
lastDraftWasOutline = false
pendingProject = nil
return true
}
private func startWatching(home: URL) {
guard watchedHome != home else { return }
watchedHome = home
@@ -91,11 +135,16 @@ extension PostsModel {
guard let home = watchedHome else { return }
// Scan + the #67 disk read both run off-main (a synced large post.md
// must never stall the UI on a watcher burst).
let openPostID = session?.postID
// #114 audit: identity, not id — a same-id reopen (convert/restore/
// move) must not receive the OLD session's stale disk text as a
// spurious #67 conflict.
let openSession = session
// brief-28: the Home half of that identity — the open post's disk
// text is read from the session's OWN library. A session targeting
// another Home has no piece in this scan's tree; reading the
// current Home's same-slug file for it raised a spurious #67 on the
// old session.
let openPostID = openSession?.targets(home: home) == true ? openSession?.postID : nil
let scanMemo = scanCache // detached capture, not a property read
// The search's content signal: a re-read post.md (a cache miss)
// changed library content even when its Post compares equal. Read
@@ -108,6 +157,18 @@ extension PostsModel {
return (posts, disk)
}.value
guard !Task.isCancelled else { return }
// Stale-scan guard (#137 suite find / finish): the snapshot can
// PREDATE the current selection, and a scan that outlived a Home
// repoint speaks for the OLD tree — bind's initial rescan (detached
// scan over the empty tree) can land its continuation after a
// select its scan never saw, "vanishing" a piece that exists, and a
// pre-repoint scan would land the old library's list under the new
// binding. Disk is the truth for vanished; a stale snapshot must
// not kill a live session (teardown cancels the debounce —
// unflushed edits would die with it); the new Home's own rescan
// owns everything (brief-28: hoisted — the list, the revision and
// the #67 text ride the same verdict, not just the vanish check).
guard watchedHome == home else { return }
// #143: @Observable invalidates on assignment regardless of equality —
// skip the no-op so panes don't regroup on our own autosave echoes.
let listChanged = scanned != posts
@@ -122,19 +183,9 @@ extension PostsModel {
if listChanged || scanMemo.misses > missesBefore {
contentRevision += 1
}
// Stale-scan guard (#137 suite find): the snapshot can PREDATE the
// current selection — bind's initial rescan (detached scan over the
// empty tree) can land its continuation after a select its scan never
// saw, "vanishing" a piece that exists. Disk is the truth for
// vanished; a stale snapshot must not kill a live session (teardown
// cancels the debounce — unflushed edits would die with it).
// #137 audit (finish): a scan that outlived a Home repoint speaks
// for the OLD tree — never let it kill a session selected under
// the NEW home (the id is missing from the old snapshot by
// construction); the next rescan under the new home owns it.
if let id = selectedPostID, !posts.contains(where: { $0.id == id }), watchedHome == home, PostStore.folder(of: id, home: home) == nil {
if let id = selectedPostID, !posts.contains(where: { $0.id == id }), PostStore.folder(of: id, home: home) == nil {
selectedPostID = nil // the post vanished (deleted in Finder or here)
if let open = session, open.postID == id {
if let open = session, open.targets(postID: id, home: home) {
open.teardown(flushFirst: false) // post is gone; cancel the debounce
session = nil
}