Files
museion/docs/devlog/20261003-issue19-editor-core_log.md
T
AlexKucera f2953ee07e
CI / ci (push) Canceled after 0s
feat(editor): land editor core — open, edit, autosave (S1.2)
Posts open into the TextKit2 surface (ADR-0010 mode B) lifted from
the proven spike: styled source, scaled headings, dimmed syntax,
native undo/IME. One EditorSession per open post drives a ~1 s
debounced autosave (flush on selection change, window close, app
quit; ⌘S immediate), the Saved/Saving/Failed dot, and the per-post
.museion-state.json cursor sidecar that dies with the folder.
External edits raise an explicit Reload/Keep-Local alert — the
debounce never answers it for the writer, and drafts keep the
lazy-folder first-⌘S rule (#62).

Reviewer fanout fixes folded in: session teardown at every lifecycle
exit (rename flushes before the folder move; trash cancels the
debounce), draft ⌘S continues in place, conflict holds the debounce,
rescan's disk read off-main, ~35 lines of dead code out.

Closes #19
2026-10-05 10:56:33 +02:00

8.0 KiB
Raw Blame History

Issue 19 — M1 editor core: open, edit, autosave (S1.2)

Date: 2026-10-03 Type: issue Reference: Wayfinder map #19 · decisions #63–#67

Goal

Implement the editor core per the #19 wayfinder map's five decisions: TextKit2 editing surface (ADR-0010 mode B, lifted from spike/textkit2/), ~1 s debounced autosave with flush on selection change / window close / app quit / ⌘S (#64), Saved→Saving…→Saved indicator with failure state (#65), cursor-state sidecar posts/<id>/.museion-state.json (#66), and the explicit external-change alert (#67). Replaces the #18 stub in place.

What Was Done

  • Museion/ProsePresenter.swift (new) — the spike's presenter lifted, verbatim modulo lint renames: line-kind → attribute map (Iowan Old Style 17 body / 26-20 bold headings / quote spacing / ***-rule), dimmed syntax (marker prefixes + inline marks, mono 13), attribute full-doc + reattribute per-paragraph. Anchor search NOT lifted — M2 (#25) per the map's out-of-scope.
  • Museion/EditorSession.swift (new) — @MainActor @Observable session per open post. edit re-arms a configurable ~1 s debounce; flush = PostStore.overwrite + sidecar write, tracks saveState; diskDidChange(diskText:) equality check (disk ≠ lastFlushed AND ≠ local) raises conflictAlert; reloadFromDisk/keepLocal implement the #67 Yes/No; failure → .failed + onError → lastError.
  • Museion/PostStore.swift — added CursorState (Codable: selectionLocation/selectionLength/scrollOffset) + readCursor/writeCursor on posts/<id>/.museion-state.json, atomic write; bodyURL helper.
  • Museion/PostsModel.swift — owns the session: select(_:home:) flushes the old session + opens a new one; newPost flushes first; rename/trash of the open post drops the session; shutdown() (app quit) flushes; rescan hands the session the open post's disk text (#67 check); onError routes into lastError.
  • Museion/PostEditorView.swift (new) — toolbar (crumb + SaveStateDot 6px dot, accent on failure; draft shows "⌘S saves"), ProseTextView NSViewRepresentable (plain store, presentation-only attributes, native undo/IME/find, 640pt measure, generation-keyed loads, clamped cursor restore, cursor push on selection change), window-close flush observer, the #67 alert (Reload / Keep Local).
  • Museion/PostsPaneView.swift — EditorStub deleted; editor column renders PostEditorView (session or draft); select call site passes home.
  • MuseionTests/EditorSessionTests.swift (new) — 4 tests: debounce→flush+sidecar round-trip, clean no-op + ⌘S immediate, #67 full matrix (echo suppressed / Yes / No→overwrite), write failure → failed + retry.

Decisions & Rationale

Decision Rationale
Lift the spike presenter verbatim #63: already written, perf-proven (≤1.1 ms on PERF-3k); a plain surface first would touch every editor file twice.
Session owns text; view loads keyed by loadGeneration The NSTextView is the live editor; re-loading per keystroke would fight the first responder. Generation bumps only at open and Yes-reload.
Watcher-echo suppression by text equality (no timer) diskText == lastFlushedText is exact — our own writes never alert. A suppression timer is state that can drift.
Sidecar write separate from content write, try? #65/#66: a failed cursor write must never read as a failed content save.
keepLocal = mark disk text as seen #67 No means "next autosave overwrites" — that requires treating the current disk text as baseline, else the alert re-fires on every watcher tick.
Drafts keep explicit ⌘S (no autosave, no sidecar) #64 exempts drafts; #62's lazy-folder rule stands — stray New Post clicks never litter posts/.
Esc on the conflict alert = Keep Local Same semantics as No; the alert binding's set-false path calls keepLocal().

Gotchas & Fixes

Problem Root Cause Fix
Build errors: AnyCancellable vs NSObjectProtocol, CGFloat?→Double?, ambiguous .greatestFiniteMagnitude, Self.cursorState Straight port slips Explicit types (AnyCancellable?, Double(...), CGFloat.greatestFiniteMagnitude, ProseTextView.cursorState)
Draft-mode reload on own keystroke updateDraft round-trips text through the model → updateNSView saw changed draft Echo guard: compare tv.string != draft (reload only on genuine divergence)
SwiftLint identifier_name/line_length on lifted spike code Spike package had no lint gate; app target does Renamed t→trimmedLine, p→style, ns→nsText, ch/c→codeUnit/scalar; extracted headingFont(size:weight:)
SwiftFormat mid-session reformats colliding with patches Format ran between write and patch Re-read → full-file rewrite where collided (ProsePresenter); verify with git status after every format run
Visual verification blocked A fullscreen game owns the display; window-scoped capture returns empty for offscreen windows Skipped pixels; verified by build + 77 unit tests + 1 UI smoke test + live launch staying alive
[audit] Rename/trash of the open post dropped the session unflushed; armed debounce then fired at the moved/dead path → edits lost + spurious "file doesn't exist" error No teardown path — select/newPost flushed, rename/trash/vanish didn't EditorSession.teardown(flushFirst:): flush before rename's disk read; cancel-only on trash/vanish; all four owners route through it
[audit] First ⌘S on a draft left the editor blank saveDraft set selectedPostID but no session; the editor column branches on model.session saveDraft now opens EditorSession(postID:) on success — typing continues in place
[audit] Debounce overwrote an external edit while its #67 alert was unanswered flush() had no conflict guard Debounce task checks conflictAlert before flushing; only explicit ⌘S overwrites mid-alert
[audit] Rescan's #67 disk read ran on the main actor readBody sat after the detached scan Folded into the same Task.detached — scan + open-post read return together
[audit] Dead code: saveSelected, bodyURL, unused home plumbing, write-only draftText, no-op removeObserver(AnyCancellable), unused .receive(on:), unreachable quote-spacing branch, writeCursor's throws Reviewer fanout (ponytail-review) All deleted/folded (~-35 lines, zero behavior change)

Files Changed

File Change Summary
Museion/ProsePresenter.swift new — lifted spike presenter (line-kind attributes, dimmed syntax)
Museion/EditorSession.swift new — debounce/flush/save-state/conflict session model
Museion/PostEditorView.swift new — TextKit2 surface + toolbar + widget + conflict alert
Museion/PostStore.swift + CursorState sidecar read/write (atomic, best-effort)
Museion/PostsModel.swift session ownership: select/newPost/rename/trash/shutdown/rescan wiring
Museion/PostsPaneView.swift EditorStub deleted; PostEditorView rendered; select passes home
MuseionTests/EditorSessionTests.swift new — 6 session-mechanics tests (debounce+sidecar, ⌘S, #67 matrix, failure retry, conflict-holds-debounce, teardown)
Museion/AGENTS.md, MuseionTests/AGENTS.md DOX pass: editor-core section replaces stub bullets

Open Items & Next Steps

  • #20 — reading toggle + iterative presentation polish (now narrowed: presenter landed here)
  • #21 — focus mode, centered measure (surface already uses a 640pt measure)
  • #25 (M2) — Fix-Card anchor highlight + stale-anchor alert in the real editor (spike proved the mechanics; anchorRange stays unlifted)
  • Visual pass on the editor once the machine's display is free (typography stance per ADR-010's "paper-grade polish" consequence)
  • Commit + push pending explicit authorization per repo policy

Log written by write-log skill