Skip to content

fix(mirror): give every page its own markdown mirror file - #13912

Open
yuxi-liu-wired wants to merge 5 commits into
logseq:masterfrom
yuxi-liu-wired:fix/mirror-one-file-per-page
Open

yuxi-liu-wired wants to merge 5 commits into
logseq:masterfrom
yuxi-liu-wired:fix/mirror-one-file-per-page

Conversation

@yuxi-liu-wired

@yuxi-liu-wired yuxi-liu-wired commented Oct 29, 2026 •

Copy link
Copy Markdown
Contributor

The markdown mirror (<graph>/mirror/markdown/pages/) can map two pages to one file, so a page silently goes missing from the mirror (one page's file holds another page's content, or a page has no file until it is edited again), and deleting one page can delete the file both share. Users who keep the mirror as a backup or in git lose a page from it. The DB is not affected.

How it happens

  • Case: pages Notes and notes (e.g. Work/Notes and Home/notes) are written to pages/Notes.md and pages/notes.md, one file on macOS (APFS default) and Windows.
  • Literal suffix: a page titled Foo (2) and the second page titled Foo both map to pages/Foo (2).md.
  • Index shift: pages Foo and Bar are mirrored; renaming Bar (uuid sorting first) to Foo gives it index 1 and it writes over the other Foo's file, which is not among the affected pages and is never rewritten.

Change

src/main/frontend/worker/markdown_mirror.cljs:

  • pages are grouped by the lower-cased file stem;
  • when appending (n), a suffixed stem that another page has as its own title is skipped;
  • each transaction also rewrites every page in the stem groups it changed (and in the groups whose suffix skips them), and a page's old file is not deleted when that path is now another page's file (the jobs run in parallel).

Test

frontend.worker.markdown-mirror-collision-test covers the three cases. On master it fails in all three (pages/notes.md twice by case, 2 distinct paths for 3 pages, foo body missing after the rename); with the change it passes. CI's unit suite passes.


Devin Review

Added: review follow-ups

  • A case-only takeover no longer deletes a page's file. On macOS and Windows, renaming Notes to Other moved notes from pages/notes (2).md to pages/notes.md, and the renamed page then deleted its old pages/Notes.md, which is the same file. A case-only rename (notes to Notes) deleted the page's own new file the same way. Old paths are now compared with every new path of the batch, the page's own included, by the lower-cased path, and an old path that is still an output of the batch is kept.
  • Files left by the previous naming are removed. A mirror written on Linux before this change can hold both pages/Notes.md and pages/notes.md; notes now writes pages/notes (2).md, and pages/notes.md stayed as a stale copy. Regenerating the mirror now deletes the .md files in pages/ and journals/ that no page owns, and after each write batch the path a written page had under the previous naming is deleted when it exists and no page owns it. A file that differs only in case from a page's path that is not listed itself is that page's file on a case-insensitive file system and is kept. The node platform gets :read-dir! to list a mirror directory; a platform without it skips the cleanup.
  • Tests. frontend.worker.markdown-mirror-collision-test adds the case-insensitive takeover and case-only rename cases, the upgrade case on a case-sensitive fake (regeneration and the first write), the regeneration case on a case-insensitive fake, and :read-dir! on the node platform; the rename test now asserts each page's content at its assigned path.

Added: pages that are not mirrored do not take a file name

  • What the user saw. A page named like a property up to case (the page Source next to the property source) was mirrored to pages/Source (2).md instead of pages/Source.md. Re-importing the mirror then gave a page titled Source (2), so the block refs and embeds on Source were no longer found under that page. This showed up when this PR is combined with fix(export): text, markdown, mirror and embeds #13869, whose page-mirror-writes-block-refs-embeds-and-node-values-by-block-id-test has exactly that page and property.
  • Cause. The stem index that hands out the (n) suffixes held every non-journal page, including properties and built-in or hidden pages, which are never written. With pages now grouped by the lower-cased stem, a page sharing a property's name up to case got pushed to a suffixed file to avoid a file that does not exist.
  • Change. Only pages that are mirrored (mirrorable-page?) enter the stem index and the legacy-path grouping.
  • Test. a-property-without-a-file-does-not-take-a-page-file-name-test in frontend.worker.markdown-mirror-collision-test (fails before the change with pages/Source (2).md). frontend.worker.markdown-mirror-test passes on this branch alone and on this branch merged with fix(export): text, markdown, mirror and embeds #13869.

@devin-ai-integration devin-ai-integration Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Devin Review found 3 potential issues.

Devin Review

Comment on lines +748 to +749
(if (and old-path (not= old-path new-path) (contains? new-paths old-path))
(assoc job :old-path nil)

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🔴 Case-only path takeover deletes another page

When Notes leaves a stem group, keep-old-paths-of-other-jobs fails to protect the surviving notes page. The old and new paths differ as strings but identify one file on macOS and Windows. Deleting the old path removes the surviving page's mirror.

Learn more

The stem index groups case variants together, so removing or renaming one page can give its old filename to another page. The protection compares paths with case-sensitive string equality, while a case-insensitive filesystem resolves both paths to the same file. <run-job! writes the surviving page and then deletes the departing page's old path. Its final file disappears regardless of which write completes first if the deletion runs last.

Example: On macOS, mirror Notes and notes; the latter uses pages/notes (2).md. Rename Notes to Other. The remaining page writes pages/notes.md, while the departing page deletes pages/Notes.md, removing that same file.

Recommended fix: Compare old and new paths using the filesystem-equivalent canonical path key, including the same page's new path. Suppress deletion whenever the old file is still an output of this batch; test case-only takeovers with a case-insensitive storage fake.

Devin Review


Was this helpful? React with 👍 or 👎 to provide feedback.

Comment on lines +177 to +178
0))]
(str "pages/" (duplicate-stem (stem-index db) stem index) ".md")))))))

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🟡 Legacy case-variant mirror files remain stale

On Linux, page-relative-path moves an existing notes mirror to notes (2).md when Notes also exists. Neither later edits nor full regeneration removes the old notes.md. The mirror retains a stale copy of the page.

Learn more

Existing mirrors on case-sensitive filesystems can have separate files for page titles differing only by case. The new path allocation moves one of those pages to a suffixed filename. page-job derives the old path from the current naming algorithm, not the path written by the earlier version; <mirror-repo! only writes files. The old filename persists with obsolete contents even when the user regenerates the mirror.

Example: Before upgrading on Linux, Notes and notes have pages/Notes.md and pages/notes.md. After upgrading, the latter writes pages/notes (2).md, but pages/notes.md still contains the previous copy forever.

Recommended fix: Add a migration or regeneration cleanup that identifies and removes obsolete mirror files without deleting files assigned to other pages. Cover upgrading an existing case-variant mirror on a case-sensitive filesystem.

Devin Review


Was this helpful? React with 👍 or 👎 to provide feedback.

Comment on lines +72 to +75
(is (has-body? "bar body") "the renamed page is mirrored")
(is (has-body? "foo body") "the page whose duplicate index shifted is still mirrored")
(is (not (contains? @files (str (markdown-mirror/repo-mirror-dir test-repo) "/pages/Bar.md")))
"the renamed page's old file is removed"))

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🔍 Rename test misses misplaced page contents

The body-presence checks pass even if the pages land in the wrong files. Assert each UUID's content at its assigned path after the rename.

Devin Review


Was this helpful? React with 👍 or 👎 to provide feedback.

@yuxi-liu-wired

Copy link
Copy Markdown
Contributor Author

Thanks, all three were right; fixed in 39349c8, 863cbc4 and cffec58.

Case-only path takeover (markdown_mirror.cljs:749). keep-old-paths-of-other-jobs now compares old paths with the batch's new paths by the lower-cased path (path-key, the same canonicalization stem-key uses for the stem groups), and the page's own new path is part of that set, so an old path that is still an output of the batch is never deleted: renaming Notes to Other no longer deletes the pages/notes.md that notes now writes, and renaming notes to Notes no longer deletes its own new file. a-departing-page-keeps-the-file-a-case-variant-takes-over-test and a-case-only-rename-keeps-the-page-file-test run against a case-insensitive storage fake (case variants resolve to one file, which keeps the name it was created with, and deletes land after the batch's writes); both failed before the change.

Stale legacy case-variant files (markdown_mirror.cljs:178). Regenerating the mirror now lists pages/ and journals/ (new node storage fn :read-dir!) and deletes the .md files no mirrored page owns. After each write batch, the path a written page had under the previous naming (exact-stem groups, computed by legacy-page-relative-path) and any old path kept by the check above are deleted when they exist and no page owns them, so the first write of notes after the upgrade removes the old pages/notes.md. A file is never deleted when a page owns it, and a file that differs only in case from a page's path that is not listed itself is treated as that page's file (a case-insensitive file system keeps the name the file was created with), so on macOS/Windows a page's own file is never removed. Tests: regenerating-removes-a-legacy-case-variant-file-test and writing-a-moved-page-removes-its-legacy-file-test start from a case-sensitive fake holding both pages/Notes.md and pages/notes.md (both failed before), regenerating-keeps-a-page-file-listed-under-another-case-test covers the case-insensitive side, and node-platform-reads-mirror-dir-file-names-test covers :read-dir! on the node platform.

Rename test (markdown_mirror_collision_test.cljs:75). The test now asserts the two pages' paths (pages/Foo.md for the renamed page, pages/Foo (2).md for the shifted one) and that each path holds its own page's body, so swapped contents fail it.

@yuxi-liu-wired
yuxi-liu-wired force-pushed the fix/mirror-one-file-per-page branch 2 times, most recently from 83122b8 to 2b8090f Compare November 3, 2026 12:02
@yuxi-liu-wired
yuxi-liu-wired force-pushed the fix/mirror-one-file-per-page branch 7 times, most recently from d3321a3 to da1d1f5 Compare November 18, 2026 14:39
@yuxi-liu-wired
yuxi-liu-wired force-pushed the fix/mirror-one-file-per-page branch from da1d1f5 to a2b9e87 Compare November 19, 2026 06:02
yuxi-liu-wired added a commit to yuxi-liu-wired/logseq that referenced this pull request Nov 19, 2026
…p/Down to reselect the moved rows

After Alt+Shift+Down moves selected blocks out of their parent, the selection holds the rows drawn before the move until the move's delta is flushed and the new rows are reselected. A Shift+Down pressed in that gap read the old row, which is no longer in the page, found no neighbour and did nothing: the selection stayed on the moved block. Master's e2e move-across-parents-keeps-selection-test catches it (CI on logseq#13912, 2026-11-18; 20 of 36 runs on master under 6 parallel lanes).

A second Alt+Shift+Up/Down already waits for the previous move through *move-up-down-tail. Shift+Up/Down (both bindings) and Up/Down on selected blocks now wait for it the same way (after-pending-move); with no move running they act at once.

Test: frontend.handler.editor-move-select-test: with a move pending no selection key acts, and each acts once the move settles; without a move Shift+Down acts at once. Fails without the change, passes with it. move-across-parents-keeps-selection-test under 6 lanes: 0 of 36 failed with the change.
@yuxi-liu-wired
yuxi-liu-wired force-pushed the fix/mirror-one-file-per-page branch from a2b9e87 to d653fc5 Compare November 19, 2026 07:41
yuxi-liu-wired added a commit to yuxi-liu-wired/logseq that referenced this pull request Nov 19, 2026
…p/Down to reselect the moved rows

After Alt+Shift+Down moves selected blocks out of their parent, the selection holds the rows drawn before the move until the move's delta is flushed and the new rows are reselected. A Shift+Down pressed in that gap read the old row, which is no longer in the page, found no neighbour and did nothing: the selection stayed on the moved block. Master's e2e move-across-parents-keeps-selection-test catches it (CI on logseq#13912, 2026-11-18; 20 of 36 runs on master under 6 parallel lanes).

A second Alt+Shift+Up/Down already waits for the previous move through *move-up-down-tail. Shift+Up/Down (both bindings) and Up/Down on selected blocks now wait for it the same way (after-pending-move); with no move running they act at once.

Test: frontend.handler.editor-move-select-test: with a move pending no selection key acts, and each acts once the move settles; without a move Shift+Down acts at once. Fails without the change, passes with it. move-across-parents-keeps-selection-test under 6 lanes: 0 of 36 failed with the change.
yuxi-liu-wired added a commit to yuxi-liu-wired/logseq that referenced this pull request Nov 19, 2026
…p/Down to reselect the moved rows

After Alt+Shift+Down moves selected blocks out of their parent, the selection holds the rows drawn before the move until the move's delta is flushed and the new rows are reselected. A Shift+Down pressed in that gap read the old row, which is no longer in the page, found no neighbour and did nothing: the selection stayed on the moved block. Master's e2e move-across-parents-keeps-selection-test catches it (CI on logseq#13912, 2026-11-18; 20 of 36 runs on master under 6 parallel lanes).

A second Alt+Shift+Up/Down already waits for the previous move through *move-up-down-tail. Shift+Up/Down (both bindings) and Up/Down on selected blocks now wait for it the same way (after-pending-move); with no move running they act at once.

Test: frontend.handler.editor-move-select-test: with a move pending no selection key acts, and each acts once the move settles; without a move Shift+Down acts at once. Fails without the change, passes with it. move-across-parents-keeps-selection-test under 6 lanes: 0 of 36 failed with the change.
What happened: in the markdown mirror, two pages could map to one file, so one page's content replaced the other's or was missing: titles differing only in case (`Notes`, `notes`) are one file on macOS and Windows; a page titled `Foo (2)` and the second page titled `Foo` both used `pages/Foo (2).md`; and renaming a page into an existing title, or deleting one, shifted the other pages' ` (n)` numbers without rewriting them, so the renamed page overwrote the other page's file.

Cause: `page-relative-path` grouped pages by their exact stem, appended ` (n)` without checking whether another page owns that name, and `<handle-tx-report!` only rewrote the pages the tx touched.

Change: pages are grouped by the lower-cased stem; a ` (n)` suffix that another page has as its own title is skipped; a tx also rewrites every page in the stem groups it changed (and the groups whose suffix skips them), and a page does not delete its old file when that path is now another page's file.

Test: `frontend.worker.markdown-mirror-collision-test` covers the case, literal-suffix and rename cases.
What happened: on macOS and Windows, mirroring `Notes` and `notes` puts `notes` at `pages/notes (2).md`; renaming `Notes` to `Other` moves `notes` to `pages/notes.md`, and the renamed page then deleted its old file `pages/Notes.md`, which is the same file, so `notes` had no mirror file. Renaming `notes` to `Notes` likewise wrote `pages/Notes.md` and then deleted `pages/notes.md`, the page's own new file.

Cause: `keep-old-paths-of-other-jobs` kept an old path only when it was equal, as a string, to another job's new path, while stem groups (and case-insensitive file systems) treat case variants as one name.

Change: old paths are compared with the batch's new paths by the lower-cased path, including the page's own new path; an old path that is still an output of the batch is not deleted.

Test: `a-departing-page-keeps-the-file-a-case-variant-takes-over-test` and `a-case-only-rename-keeps-the-page-file-test` in `frontend.worker.markdown-mirror-collision-test` run against a case-insensitive storage fake whose deletes land after the batch's writes.
What happened: a mirror written on Linux before this branch can hold both `pages/Notes.md` and `pages/notes.md`. With case variants grouped, `notes` moves to `pages/notes (2).md`, and the old `pages/notes.md` kept a stale copy of the page forever: page jobs derive the old path from the current naming, and regenerating the mirror only wrote files.

Change: regenerating the mirror lists `pages/` and `journals/` and deletes the `.md` files no mirrored page owns. After each write batch, the path a written page had under the previous naming (exact-stem groups), and an old path kept because it differs only in case from a path the batch wrote, are deleted when they exist and no page owns them. A file that differs only in case from a page's path that is not listed itself is that page's file on a case-insensitive file system and is kept. The node platform gets `:read-dir!` (file names in a mirror directory); a platform without it skips the cleanup.

Test: `regenerating-removes-a-legacy-case-variant-file-test` and `writing-a-moved-page-removes-its-legacy-file-test` (case-sensitive fake holding both legacy files), `regenerating-keeps-a-page-file-listed-under-another-case-test` (case-insensitive fake), and `node-platform-reads-mirror-dir-file-names-test`.
The rename test only checked that both page bodies exist somewhere in the mirror, so it passed if the two pages' contents were swapped. It now asserts the pages' paths (`pages/Foo.md`, `pages/Foo (2).md`) and that each path holds its own page's body.
A page whose title matches a property's name up to case (the page Source and the property source) was mirrored to pages/Source (2).md instead of pages/Source.md. The properties have no mirror file, so the suffix guarded against nothing, and a reader of the files saw the page under another name: re-importing the mirror gave a page titled "Source (2)", so its block refs and embeds were no longer found on the page named Source.

Cause: the stem index that assigns the ` (n)` suffixes held every non-journal page, including properties, built-in and hidden pages that are never written. Once pages are grouped by the lower-cased stem, any page named like a property shares a group with it.

Change: only pages that are mirrored (mirrorable-page?) enter the stem index and the legacy-path grouping; mirrorable-page? and contents-page? move up next to the stem code that now uses them.

Test: a-property-without-a-file-does-not-take-a-page-file-name-test in markdown_mirror_collision_test.cljs checks that the page Source keeps pages/Source.md next to the property source.
@yuxi-liu-wired
yuxi-liu-wired force-pushed the fix/mirror-one-file-per-page branch from d653fc5 to c236e2e Compare November 19, 2026 12:01
yuxi-liu-wired added a commit to yuxi-liu-wired/logseq that referenced this pull request Nov 19, 2026
…p/Down to reselect the moved rows

After Alt+Shift+Down moves selected blocks out of their parent, the selection holds the rows drawn before the move until the move's delta is flushed and the new rows are reselected. A Shift+Down pressed in that gap read the old row, which is no longer in the page, found no neighbour and did nothing: the selection stayed on the moved block. Master's e2e move-across-parents-keeps-selection-test catches it (CI on logseq#13912, 2026-11-18; 20 of 36 runs on master under 6 parallel lanes).

A second Alt+Shift+Up/Down already waits for the previous move through *move-up-down-tail. Shift+Up/Down (both bindings) and Up/Down on selected blocks now wait for it the same way (after-pending-move); with no move running they act at once.

Test: frontend.handler.editor-move-select-test: with a move pending no selection key acts, and each acts once the move settles; without a move Shift+Down acts at once. Fails without the change, passes with it. move-across-parents-keeps-selection-test under 6 lanes: 0 of 36 failed with the change.

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant