# Build S4: Convert to Post — in-place, transform choice at conversion > **Date:** 2026-10-16 > **Type:** issue > **Reference:** [#114](https://git.babylondreams.de/AlexKucera/Museion/issues/114) (map [#110](https://git.babylondreams.de/AlexKucera/Museion/issues/110)) ## Goal Land S4 of the Outliner II map: a Convert to Post command on outline pieces — id, folder, Pass State, origin kept; `type` frontmatter flips to `post`; the body transforms once, with the writer picking the transform at conversion (verbatim bullets vs headings ladder). Open session flushes first, then re-routes to the prose surface. One-way in v1. ## What Was Done - **`OutlineTree.headingsLadderBody(_:)`** — the pure ladder transform: roots → `##`, each deeper level steps one heading down to the `######` floor; deeper levels stay bullets, indent re-based flush under the floor; preamble (`# Title`) rides verbatim above. Reuses the private `scan`, so ladder depths and the outliner's depths can never disagree. - **`PostStore.convert(id:headingsLadder:home:)`** — the one-time in-place write: re-derives `pass_state`/`origin` from the file's own frontmatter (same as `overwrite`), stamps `type: post`, writes the (possibly transformed) body atomically. id and folder never move. Vanished piece throws, never resurrects. - **`PostsModel`** — `PendingConversion { id, lossy }` staged by `stageConversion(_:home:)` (outline pieces only; `lossy` = `OutlineTree.parsesWithLoss`, driving the sheet's warning), `cancelConversion()`, and `convertToPost(headingsLadder:home:)`: flush-first via the existing `flushOrAbort` (a failed write aborts with the strip message), convert, then — only when the piece was the open session — teardown-no-flush + `openUnguarded` reopen with `proseOverridePostID` set for the rescan lag window (literally the `lastDraftWasOutline` machinery reversed; the existing override slot, no new routing state). An unopened piece just converts on disk. - **UI** — `ConvertSheet` (house ask pattern, WorkspaceView): Verbatim Bullets (default action — lossless) · Headings · Cancel (dismissal = cancel, binding pattern); a warning paragraph when the ladder would drop prose lines. Row-menu `Convert to Post…` on outline pieces in the Posts library AND the Archive pane (archived piece stays archived — only type + body flip). ## Decisions & Rationale | Decision | Rationale | |----------|-----------| | Verbatim default, ladder opt-in | Verbatim is lossless on mixed content (body unchanged); the ladder is the one that drops prose lines — the default must never destroy. | | Ladder floor at `######`, deeper stay bullets | Markdown has six heading levels; depth > 4 outlines are rare — bullets under the floor keep their relative nesting without inventing `#######`. The simplest honest rule the ticket named. | | Reuse `proseOverridePostID` for the re-route | It already has `lastDraftWasOutline`'s exact lifecycle (dies on next open/draft/guest) and already flows through PostsPaneView's routing expression — the ticket's "flip the machinery the other way" is this slot. Zero new routing state. | | Flush-first via `flushOrAbort`, teardown-no-flush after | Convert reads disk; unflushed outliner edits must land before the transform. A failed flush aborts the convert (rename's pattern). After a good flush the teardown must NOT flush again — the file has moved on to converted content; a second flush would overwrite the conversion. | | Ask is id-scoped, not session-scoped | The ask survives background selects (multi-window) — conversion targets a piece, and `convertToPost` re-derives everything (flush, disk read) at answer time. No displacement wiring needed. | | Sheet, not inline picker | House pattern (OriginAsk / OutlineGuard / NewProject): model stages, WorkspaceView presents, answers call the model. | | Convert lives in the row menu | The menu is already per-piece and type-aware (✳ suffix); the editing surface stays untouched — routing flips it to prose the moment the answer lands. | ## Gotchas & Fixes | Problem | Root Cause | Fix | |---------|------------|-----| | `patch` fuzzy-match mangled `OutlineGuardSheet`'s closing braces (over-indented closers, ConvertSheet nested-looking) | old_string indentation didn't byte-match the file; fuzzy match accepted a wrong anchor | Region rewritten deterministically by line index (execute_code), verified by build | | Test append landed INSIDE the last `@Test` function ("Attribute 'Test' cannot be applied to a function within function") | `PostStoreTests.swift` uses file-scope `@Test` funcs — the file's final `}` closed the function, not a type; stripping it swallowed the pin into the body | Restored the function close before the pin; compiler confirmed | | swiftlint: 10 warnings across changed files | All pre-existing ceilings (file/type-length, the `up` param, the NFD comment line) — files already over the limits at HEAD | None owed; lint scoped to changed files only (no blanket run — #112 trap) | | Live report: verbatim convert "doesn't update the sidebar — row keeps ✳" (works with Headings) | Could not reproduce: model-level pin shows posts flips to `.post` and routing goes prose once the async rescan lands; headings vs verbatim share one code path | Read as rescan-lag/stale-instance on first look; user confirmed fixed on retest. Pinned as `verbatim convert flips the posts list after the rescan` | ## Files Changed | File | Change Summary | |------|---------------| | `Museion/OutlineTree.swift` | `headingsLadderBody(_:)` — the pure ladder transform | | `Museion/PostStore.swift` | `convert(id:headingsLadder:home:)` — the in-place type flip + one-time body transform | | `Museion/PostsModel.swift` | `PendingConversion`, `pendingConversion` slot, `stageConversion`/`cancelConversion`/`convertToPost` (#114 extension) | | `Museion/WorkspaceView.swift` | `ConvertSheet` + `convertAskBinding` (4th house ask); Archive row menu entry | | `Museion/PostsPaneView.swift` | Posts library row menu: `Convert to Post…` on outline pieces | | `MuseionTests/OutlineTreeTests.swift` | Ladder pin: ladder shape, bullet floor, preamble, mixed-content drop | | `MuseionTests/PostStoreTests.swift` | Convert round-trip: verbatim keeps body + flips type + scan reads post; ladder shape; vanished-piece throw | | `MuseionTests/PostsModelTests.swift` | Lifecycle pins: open piece reopens routed prose (override set, disk converted); unopened piece converts without touching the session | ## Verification - `xcodebuild build` — SUCCEEDED - `xcodebuild test` — SUCCEEDED (206 unit incl. 13 new pins + UI smoke, post-audit count) - `swiftformat` on changed files (2 reformatted), `swiftlint` — 0 new violations (10 pre-existing ceiling warnings, verified at HEAD) - GitNexus: impact before edits (PostsModel 59 / OutlineTree 57 dependants — all changes additive); `detect-changes` after: 8 files, risk critical by line-shift heuristic (`stateURL` flagged by position; body untouched) — discharged by full build + full suite green, #112 posture. ## Open Items & Next Steps - [ ] Commit + push pending authorization (house rule: never commit autonomously). - [ ] Map #110 continues: S5 prose-list shortcuts (#115) remains open on the frontier. - [ ] One-way in v1: Post → Outline is not built (per the ticket; reverse convert is a fresh decision if ever wanted). - [ ] Known ceiling: converting a piece open in ANOTHER window reopens the session there too (app-scoped model) — same-window convert is the tested path. ## Session audit (finish-audit pass) Three fresh-context reviewers (ponytail / security / general) over the working-tree diff. Accepted and fixed, each behind a pin (`MuseionTests/PostsModelTests.swift` audit section): - **Answer-time consent recheck (security MEDIUM, merged with general's stale-`lossy` finding)** — the sheet's warning covered the STAGED body; a foreign write behind the ask could make Headings drop unconsented prose. `convertToPost` now re-reads post-flush: `headingsLadder && nowLossy && !warned` aborts with "nothing was converted". Staging also derives `lossy` from `session.text` when the piece is open (disk lags the debounce). Pinned both ways (unwarned abort + warned-consent proceeds). - **Unanswered #67 conflict aborts the op (general MEDIUM, pre-existing class)** — `flushOrAbort` now treats `conflictAlert` like a failed flush: abort with the reload-question message, alert stays answerable. Fixes the class for rename/move/archive/restore too. Pinned. - **Spurious conflict after same-id reopen (general MEDIUM, pre-existing class)** — `rescan` captured the open post's ID; an in-flight rescan delivered the OLD session's stale disk text to the NEW session as a #67 conflict. Gate is now session identity (`openSession === session`). Fix landed un-pinned: the race window isn't harness-reproducible; the gate is an identity comparison. - **Held #111 guard ask cleared on convert (general LOW)** — multi-window: converting a piece whose guard ask is held now dismisses the ask as a cancel (`cancelOutlineGuard`). Pinned. - **`flushOrAbort` failure copy** — "— nothing was moved." → "— nothing was changed." (fits convert; no test pinned the old string). - **Ponytail round** — `PostStore.existingStamp` extracted (overwrite/convert shared 6 near-copy lines); `PendingConversion: Equatable` cut (dead); `ConvertSheet` two closures → one `(Bool) -> Void`; the test's hand-copy of PostsPaneView's routing expression cut (goes stale the moment #115 reshapes routing). Freebies taken: archived-piece convert pin, ladder identity edges (empty/prose-only), dirty-flush-before-convert pin, failed-flush-abort pin. - Pin discrimination verified: the three new guards flipped off once → their pins failed (3 for 3), restored, suite green. Deferred explicitly: security INFO-2 (unreadable body stages lossy-false + misleading missing-file error — house pattern, same shape as `overwrite`); disk-type re-derivation in `stageConversion` (the destructive half is covered by the answer-time recheck; verbatim re-stamp of an externally-flipped piece is harmless); `"## "` trailing space on empty ladder items (mirrors the outliner's own `"- "` serialization). --- *Log written by write-log skill*