From 70dd47ed8c5348a2849d2db7df00346365cb64ef Mon Sep 17 00:00:00 2001 From: Alexander Kucera Date: Mon, 30 Nov 2026 22:17:13 +0100 Subject: [PATCH] fix(editor): normalize the guest watch's disk read MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit The external guest's watch read disk raw while the session's text and lastFlushedText came through readDocument's normalizing external branch, so diskTextChanged saw a difference on every BOM/CRLF file and raised conflictAlert on unchanged content — re-arming after each sibling directory event and after either alert answer, blocking departures and quit until the user's own flush rewrote the file normalized (finding museion/external-watch-compares-unnormalized-text, argus cycle-6; unticketed, brief-32). Extract the external branch (BOM strip + CRLF->LF) into EditorSession.readExternalDocument(at:) — one shared normalizer, two callers: the open read (readDocument) and the watch's onEvent. The #67 compare is now normalized-to-normalized; what readDocument loads is unchanged. --- Museion/EditorSession.swift | 23 +++++++++++++++++------ Museion/PostsModelImports.swift | 9 ++++++--- 2 files changed, 23 insertions(+), 9 deletions(-) diff --git a/Museion/EditorSession.swift b/Museion/EditorSession.swift index 8462562..f7793e5 100644 --- a/Museion/EditorSession.swift +++ b/Museion/EditorSession.swift @@ -157,16 +157,27 @@ final class EditorSession { private static func readDocument(_ target: SessionTarget) -> String? { switch target { case let .post(id, home): - return PostStore.readBody(id: id, home: home) + PostStore.readBody(id: id, home: home) case let .externalFile(url): - guard var text = try? String(contentsOf: url, encoding: .utf8) else { return nil } - if text.hasPrefix("\u{FEFF}") { - text.removeFirst() - } - return text.replacingOccurrences(of: "\r\n", with: "\n") + readExternalDocument(at: url) } } + /// #67/brief-32: the ONE external-target disk read — raw UTF-8, BOM + /// stripped, CRLF normalized to LF. Every reader of an external file's + /// bytes goes through here (the open read above, the guest watch in + /// PostsModelImports, Yes-reload, keep-local), so the #67 compare is + /// always normalized-to-normalized: the watch used to read raw and + /// raised a false conflict on every directory event over an unchanged + /// BOM/CRLF file. + static func readExternalDocument(at url: URL) -> String? { + guard var text = try? String(contentsOf: url, encoding: .utf8) else { return nil } + if text.hasPrefix("\u{FEFF}") { + text.removeFirst() + } + return text.replacingOccurrences(of: "\r\n", with: "\n") + } + /// Target-aware write: posts → `PostStore.overwrite` (+ sidecar); /// external → the whole text, in place, no sidecar (#93). private func writeDocument() throws { diff --git a/Museion/PostsModelImports.swift b/Museion/PostsModelImports.swift index 7de61cc..534e47d 100644 --- a/Museion/PostsModelImports.swift +++ b/Museion/PostsModelImports.swift @@ -62,11 +62,14 @@ extension PostsModel { } /// #93: watch the open external file — its rescan is the #67 disk-change - /// check, reused verbatim for foreign files. PostsWatcher fires on the - /// main actor already; no re-hop needed. + /// check, reused verbatim for foreign files. The disk read goes through + /// the shared external normalizer (brief-32): the session's text is + /// normalized on open, so a raw read raised a false conflict on every + /// directory event over an unchanged BOM/CRLF file. PostsWatcher fires + /// on the main actor already; no re-hop needed. private func startExternalWatch(url: URL) { externalWatcher.onEvent = { [weak self] in - guard let self, let session = externalSession, let disk = try? String(contentsOf: url, encoding: .utf8) else { return } + guard let self, let session = externalSession, let disk = EditorSession.readExternalDocument(at: url) else { return } session.diskDidChange(diskText: disk) } externalWatcher.start(urls: [url.deletingLastPathComponent()])