feat(outline): mixed-content open guard — S7 (#111)
CI / ci (push) Canceled after 0s

Opening a type: outline piece whose body parses with dropped prose
(paragraph lines between bullets) now asks before the outliner mounts:
Open in Prose (default, nothing drops), Open as Outline (explicit
consent to save normalization), or Cancel. The detector shares the
parser's item definition, the ask presents over an empty editor so
nothing can normalize on save before the choice, and the prose answer
rides a routing override that dies with the next open.

Audit round pinned the lifecycle: reselect of the open piece is a
no-op (an armed debounce must never flush mid-ask), and a draft or
guest staged during the ask displaces it without being destroyed.

Closes #111

[Generated by AI]
This commit is contained in:
AlexKucera committed 2026-10-15 18:31:26 +02:00
1 parent c23ee0f62f
commit 0b172f73ef
12 files changed
+440 -17

No files matched your search

+108 -9
View File
@@ -122,6 +122,15 @@ final class PostsModel {
/// prose editor. True only between saveDraft and the next selection.
private(set) var lastDraftWasOutline = false
/// #111 (S7): a mixed-content outline piece whose open is held for the
/// guard ask. Non-nil = the ask is up (no session — the ask presents
/// over an empty editor); cleared by any answer.
private(set) var outlineGuardPieceID: String?
/// #111 (S7): the piece the guard's Prose answer opened in prose mode —
/// a routing override with lastDraftWasOutline's exact lifecycle (dies
/// on the next open/draft/guest; the rescan lag window is covered).
private(set) var proseOverridePostID: String?
/// Last failed-op message surfaced in the editor (nil = clean).
private(set) var lastError: String?
/// Home the watcher is bound to; also the rescan target.
@@ -218,6 +227,8 @@ final class PostsModel {
session = nil
draft = PostDraft(project: target)
lastDraftWasOutline = false // #109: the afterglow died with the draft
proseOverridePostID = nil // #111: same death, same clearing
outlineGuardPieceID = nil // #111: the draft displaces any held ask
selectedPostID = nil
}
@@ -231,6 +242,8 @@ final class PostsModel {
session = nil
draft = PostDraft(text: "- ", project: target, type: .outline)
lastDraftWasOutline = false // #109: a new draft resets the afterglow
proseOverridePostID = nil // #111: same death, same clearing
outlineGuardPieceID = nil // #111: the draft displaces any held ask
selectedPostID = nil
}
@@ -269,19 +282,37 @@ final class PostsModel {
/// selecting away). #19: opening a post ends the previous session
/// (flush-on-selection-change, #64) and starts a new one. #92: it also
/// dismisses the external guest (flush first).
/// #111 (S7): a `type: outline` piece that parses with dropped prose
/// fires the mixed-content guard — the open is held (routing stays
/// prose) until the writer picks. The old session still ends here:
/// its flush is prose-mode, so it can't drop anything.
func select(_ id: String, home: URL) {
dismissExternal()
if let session, session.postID != id {
// A reselect of the already-open piece is a no-op — INCLUDING a
// guarded one: the piece is open under a prior answer, and firing
// the ask again would nil its session without teardown (the armed
// debounce would then flush mid-ask, outside any owned session).
if let session, session.postID == id {
return
}
if let session {
session.teardown(flushFirst: true)
}
session = id == session?.postID ? session : EditorSession(postID: id, home: home)
draft = nil
// #108: abandoning the draft kills its explicit project target too —
// a later New Post must not silently land in the abandoned project.
pendingProject = nil
lastDraftWasOutline = false // #109: opening a post is a fresh context
selectedPostID = id
wireSession()
if isGuardedOutline(id, home: home) {
outlineGuardPieceID = id
proseOverridePostID = nil
session = nil
draft = nil
pendingProject = nil
lastDraftWasOutline = false
selectedPostID = id
return
}
// The prior session was torn down above; openUnguarded's guards
// are dead code on this path (session is nil or a different id) —
// kept: one open body answers select and the ask alike.
openUnguarded(id, home: home)
proseOverridePostID = nil // #111: the override died with the open it covered
}
/// Draft typing from the editor (private(set) barrier).
@@ -505,6 +536,8 @@ final class PostsModel {
draft = nil
pendingProject = nil // #108 audit: the draft died here too — its target dies with it
lastDraftWasOutline = false // #109: same death, same clearing
proseOverridePostID = nil // #111: same death, same clearing
outlineGuardPieceID = nil // #111: the guest displaces any held ask
externalSession = EditorSession(target: .externalFile(target))
if externalSession?.loadFailed == true {
lastError = "Couldn't read \(target.lastPathComponent) — not UTF-8 text or unreadable. Nothing was changed."
@@ -574,3 +607,69 @@ final class PostsModel {
externalSession?.onError = route
}
}
// MARK: Mixed-content outline guard (#111, S7)
extension PostsModel {
/// A `type: outline` piece that parses with dropped prose — the guard's
/// target. The old session already ended by the time this runs; its
/// flush was prose-mode, so it couldn't drop anything.
/// Type: the scan's word when the list knows it (archive pane opens,
/// app-start selection), the frontmatter's word during the rescan lag
/// (the same window lastDraftWasOutline covers) — ONE disk read derives
/// both. No watched home yet (pre-bind) = post: nothing to guard.
private func isGuardedOutline(_ id: String, home: URL) -> Bool {
guard let dir = PostStore.folder(of: id, home: home),
let text = try? String(contentsOf: dir.appendingPathComponent("post.md"), encoding: .utf8)
else { return false }
let type: Post.ContentType = if let known = posts.first(where: { $0.id == id })?.type {
known
} else {
Post.ContentType(rawValue: Post.frontmatter(of: text)["type"] ?? "") ?? .post
}
return type == .outline && OutlineTree.parsesWithLoss(PostStore.splitFrontmatter(text).body)
}
/// #111 (S7): guard answers. Prose = safe open in prose mode (the
/// outliner never mounts, nothing drops); Outline = explicit consent,
/// opens the outliner (saves normalize/drop as today). Cancel = back
/// out (no session); the piece stays guarded on the next open.
func openGuardedOutlineInProse(home: URL) {
guard let id = outlineGuardPieceID else { return }
outlineGuardPieceID = nil
proseOverridePostID = id // routing: prose until the next open
openUnguarded(id, home: home)
}
func openGuardedOutlineAsOutline(home: URL) {
guard let id = outlineGuardPieceID else { return }
outlineGuardPieceID = nil
openUnguarded(id, home: home)
}
/// #111 (S7): the ask's Cancel — the held open dies (no session opens,
/// nothing stays selected over the empty editor).
func cancelOutlineGuard() {
outlineGuardPieceID = nil
if session == nil {
selectedPostID = nil
}
}
/// #111 (S7): the open with the guard passed (either answer or a clean
/// parse) — the plain select body. No re-check: the answer is the
/// explicit choice the ruling requires.
private func openUnguarded(_ id: String, home: URL) {
dismissExternal()
if let session, session.postID != id {
session.teardown(flushFirst: true)
}
session = id == session?.postID ? session : EditorSession(postID: id, home: home)
draft = nil
pendingProject = nil
lastDraftWasOutline = false
outlineGuardPieceID = nil // the open displaces any held ask
selectedPostID = id
wireSession()
}
}