Skip to content

Keep Modex responsive while importing files into notes - #261

Merged
BunsDev merged 4 commits into
mainfrom
notes-import-async
Oct 16, 2026
Merged

BunsDev merged 4 commits into
mainfrom
notes-import-async

Conversation

@BunsDev

@BunsDev BunsDev commented Oct 16, 2026

Copy link
Copy Markdown
Contributor

Part of #241 (finding 4: "Import copies on the main thread").

What was wrong

NotesStore.importFiles copied each file with fs.copyFileSync on the Electron main process: up to 50 files of up to 100 MB each. A copy that is not an instant APFS clone (another disk, a network share, an iCloud placeholder that has to download) froze the whole window until every file had copied.

What changed

  • importFiles is now async. It awaits fs.promises.lstat and fs.promises.copyFile with the same COPYFILE_EXCL | COPYFILE_FICLONE flags. The 50-file and 100 MB limits are unchanged, and errors are still collected and reported per file.
  • The notes:import IPC handler in apps/desktop/src/main/index.ts awaits it. The renderer (SpaceView → bridge.invoke("notes:import")) already awaited the IPC result, so it needs no change.
  • Interleaving. The store had no lock or queue: it relied on every operation being synchronous. Once the copy yields, other notes actions can run while a file is still copying. To handle that the way the store already handles in-progress writes (a hidden temporary file, then a no-clobber link, as in writeFileDurably and moveNoClobber):
    • Each file is copied into a hidden staging file in .modex/, then hard-linked into the folder. A tree or page read never sees half a file. Without this, a half-copied .md file would be recorded as a page with a truncated first version.
    • The name is chosen when the file is linked, not before the copy. Two imports of Report.md into the same folder at the same time both land, as Report.md and Report 2.md. The link still never replaces anything.
    • If the folder is renamed or moved while a file copies, the file follows the folder (looked up by id).
    • The staging file is removed whether the copy succeeds or fails. .modex/ is skipped by the folder watcher, so a large copy does not make Space re-read the folder.
  • docs/status.md: one bullet under On main, unreleased.

The legacy space.json backup copyFileSync at startup is a different path and is unchanged.

One thing to know: if Modex crashes in the middle of a copy, a hidden import-*.tmp file can stay in .modex/. This is the same as any interrupted writeFileDurably write, and it never appears in the notes.

Regression test

apps/desktop/test/notes-tree.test.ts: "importing waits for each copy without blocking, and a file still copying never shows in the folder". The test replaces fs.promises.copyFile with a version that copies the bytes and then waits until the test releases it. This is deterministic: there are no large files and no timing. The test checks that:

  • the import is still waiting on its copy instead of having finished in one blocking call;
  • while it waits, the tree and the page list do not show the file;
  • the folder can be renamed, and a second file with the same name imported, while the first is still copying. Both files land in the renamed folder with distinct names and the right contents;
  • a copy that fails partway is reported for that file only, and no .tmp file is left anywhere under the notes folder.

On unmodified main, the test failed at its first assertion: 'imported' !== 'copying' ("the import waits for its copy instead of blocking until it is done"). The existing import test now awaits the call and uses assert.rejects for the target and 50-file checks. Its assertions are otherwise unchanged and it passes.

What I ran

All of these ran in this worktree on the branch rebased onto main at 5efafc4 (#248), with the Mac heavily loaded:

  • pnpm build: passed.
  • pnpm test: passed. Worktree scripts 107/107, core 16/16, desktop 1,422 passed and 16 skipped of 1,438 with 0 failures, desktop packaging and update scripts 21/21, site 10/10, store-desktop 16/16. No unrelated flakes. A run before the rebase also passed: desktop 1,421 passed and 16 skipped of 1,437.
  • node --test dist/test/notes-tree.test.js: 28/28 passed.
  • pnpm exec playwright test e2e/notes-files.spec.ts (Import files… from the Notes menu, including a second import of the same file): 1 passed.

Coven-Harness: Claude Code / claude-opus-5-5

Before, Import files copied each file with copyFileSync on the main
process: up to 50 files of 100 MB each. A real copy from another disk,
a network share or an iCloud placeholder froze the whole window until
it finished.

Now the import awaits fs.promises.copyFile (same EXCL and FICLONE
flags, same limits, errors still reported per file), and the IPC
handler awaits it. Because other notes actions can now run while a
copy is in flight, each file is copied into a hidden staging file in
.modex/ and then hard-linked into the folder under a name chosen at
that moment. A read never sees half a file, two imports of the same
name into one folder both land (Report.md, Report 2.md), a link still
never replaces anything, and a folder renamed or moved mid-import is
followed by id. The staging file is always removed.

A new test holds the copy open and checks that the import yields, that
the folder shows nothing until the copy is done, and that a concurrent
same-name import, a mid-copy rename and a failed copy all behave. It
failed on main at its first assertion.

Coven-Harness: Claude Code / claude-opus-5-5
@vercel

vercel Bot commented Oct 16, 2026 •

Copy link
Copy Markdown

The latest updates on your projects. Learn more about Vercel for GitHub.

Project Deployment Actions Updated
modex Ready Ready Preview Oct 16, 2026 10:38am UTC

Request Review

@github-code-quality

github-code-quality Bot commented Oct 16, 2026 •

Copy link
Copy Markdown

Code coverage overview

Languages: TypeScript

TypeScript / code-coverage-agent

main 9668924 notes-import-async 28f69bd +/-
61% 61% 0%

ℹ️ Calculations represent line coverage. Results may change as calculations are updated.


Updated October 16, 2026 10:56 UTC

@BunsDev
BunsDev merged commit a60efbb into main Oct 16, 2026
13 checks passed
@BunsDev
BunsDev deleted the notes-import-async branch October 16, 2026 10:58
BunsDev added a commit that referenced this pull request Oct 19, 2026
… and the routines spec (#310)

v0.2.0 was published on 2026-10-18 (23:14Z) from the signed tag on 01b1b8a. This
follows Val's status layout (#290):

- Shipped gains a v0.2.0 row linking its release and its review.
- The 13 "On main, unreleased" items move unchanged to a new "Status summary"
  at the end of the v0.2.0 review, along with the old In flight entry. Each one
  was introduced by a commit in the tag (#248, #253, #254, #257 to #261, #263,
  #264, #266, #267, #269). The CF slices and #283's Markdown parser came after
  the tag, so they stay.
- In flight keeps a short follow-ups entry for the gates no receipt records yet:
  the independent download checks, the banner upgrade on the release Mac, and
  the Companion 0.2.0 (7) TestFlight upload.
- The routines spec and the README now say Phase 1, and most of Phase 0, ship
  from v0.2.0.

Unchanged: the website's static fallback (apps/site/release.json) and the
release evidence, which stay with the release owner. Claimed on #290.

Coven-Harness: Claude Code / claude-opus-5-5

This branch was successfully deployed

1 active deployment
Preview — 28f69bde Deployed Oct 16, 2026 by vercel[bot]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant