Skip to content

fix(core): normalize file paths to NFC at the ingest boundary on macOS - #1765

Closed
sergi039 wants to merge 1 commit into
basicmachines-co:mainfrom
sergi039:fix/nfc-path-normalization
Closed

sergi039 wants to merge 1 commit into
basicmachines-co:mainfrom
sergi039:fix/nfc-path-normalization

Conversation

@sergi039

Copy link
Copy Markdown

Fixes #1275.

Problem

macOS filesystems resolve the NFC and NFD spellings of a name to one file but report whichever form is on disk, and Syncthing keeps NFD names on macOS. Paths are identity keys, so a name flipped to NFD rewrote an entity's key or minted a -1 twin; edit_note on such a note then fails with UNIQUE constraint failed: entity.permalink. On one vault (12.7k notes, macOS + Syncthing) this left 60 twin pairs.

Change

Following the layer @phernandez described in #1275 — the ingest boundary, not get_by_file_path:

  • utils.nfc_path() — NFC on macOS; byte-exact on Linux and Windows, whose filesystems store NFC and NFD names as distinct files.
  • Applied where relative paths enter the index: scanner (index/local_project.py), watcher (index/filesystem.py), /index-file (_canonical_file_path, which also matches entries across forms), title/directory → path in schemas/base.py (the directory is normalized before sanitize_for_directory, which otherwise drops combining marks from NFD input), move destination (runtime/note_move.py), document import, and generate_permalink.
  • Migration 0042 (macOS only) for existing indexes:
    • merges twins into the oldest row (it holds the original permalink);
    • keeps an accepted write that has not reached disk (note_content not synced) — two such writes for one file stop the upgrade with an explicit error instead of dropping one;
    • redirects relation.to_id, note_file_vacate.entity_id, accepted_project_note_change.entity_id; drops an edge that would duplicate one into the kept row (uix_relation_from_id_to_id);
    • merges NFC/NFD vacate markers to the one whose checksum matches the file on disk (ids are not a guide: a refreshed marker keeps its id);
    • clears the kept row's checksums so the next pass re-reads the file, and stores every path in NFC;
    • leaves embeddings of deleted chunks to the semantic index's orphan sweep, so the migration does not need sqlite-vec.

Tests

  • tests/index/test_unicode_path_normalization.py — each boundary; the scanner, watcher, move and title tests fail on current main and pass here.
  • tests/test_nfc_file_paths_migration.py — twin merge, unwritten content kept over a fresher synced copy, refusal on two unwritten writes, edge de-duplication, vacate marker selection by on-disk checksum.
  • Full unit suite on macOS: 8264 passed; 2 failures (test_search_with_date_filter, test_recent_activity_format_relative_time_and_truncate_helpers) also fail on unmodified main — they depend on the current date. ty clean apart from the pre-existing optional pymilvus imports. Not run: Postgres suite, integration tests, Windows.

Notes

  • This change was prepared with an AI coding assistant (Claude) and reviewed in two rounds by a second model (Codex); every finding from those reviews is addressed above. I run the same fix locally on top of v0.22.1.
  • I will sign the CLA when the bot asks.

@CLAassistant

CLAassistant commented Oct 14, 2026 •

Copy link
Copy Markdown

CLA assistant check
All committers have signed the CLA.

@chatgpt-codex-connector

chatgpt-codex-connector Bot commented Oct 14, 2026 •

Copy link
Copy Markdown

Codex Review Summary

This comment shows the latest Codex review activity on this pull request.

Review Status Commit Review trigger
📝 Code Review ✅ Completed 2026-10-14T11:39:02.475744Z 8041220 PR opened
ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review" or "@codex security review".

Codex reacts with 👀 while any review is running, comments if it has suggestions, and reacts with 👍 once all reviews finish with no findings.

@chatgpt-codex-connector chatgpt-codex-connector Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

💡 Codex Review

Here are some automated review suggestions for this pull request.

Reviewed commit: 80412204e8

ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".

sa.text("DELETE FROM note_content WHERE entity_id = :keeper"), {"keeper": keeper}
)
connection.execute(
sa.text("UPDATE note_content SET entity_id = :keeper WHERE entity_id = :owner"),

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

P1 Badge Re-key moved note content to the keeper's external ID

When _keep_unwritten_note_content selects a twin row (notably the pending-write preservation case), this update changes only entity_id; the row's external_id still names the entity deleted below, even though NoteContentRepository._align_identity_fields requires it to mirror the owning entity. /knowledge/entities/batch filters on NoteContent.external_id, so asking for the surviving keeper ID reports that canonical accepted write as missing until a later edit happens to realign it; update the mirrored external ID as part of the adoption.

AGENTS.md reference: AGENTS.md:L157-L161

Useful? React with 👍 / 👎.

@sergi039
sergi039 force-pushed the fix/nfc-path-normalization branch from 8041220 to 4ed3941 Compare October 14, 2026 11:45
macOS filesystems resolve the NFC and NFD spellings of a name to one file
but report whichever form is on disk, and Syncthing keeps NFD names on
macOS. Paths are identity keys, so a name flipped to NFD rewrote an
entity's key or minted a -1 twin, after which edit_note failed on the
permalink constraint (basicmachines-co#1275).

Normalize where relative paths enter the index: scanner, watcher,
/index-file, title and directory to path (before sanitizing, which drops
combining marks), move destination, document import, and
generate_permalink. Linux and Windows keep byte-exact paths, since their
filesystems store NFC and NFD names as distinct files.

Migration 0042 (macOS only) merges existing twins into the oldest row,
keeps an accepted write that has not reached disk, redirects references,
drops edges that would duplicate one into the kept row, keeps the vacate
marker whose checksum matches the file on disk, and stores every path in
NFC. Two unwritten writes for one file stop the upgrade instead of
dropping one.

Fixes basicmachines-co#1275

Signed-off-by: Sergey Sinyugin <[email protected]>
@phernandez

Copy link
Copy Markdown
Member

Thank you @sergi039, this is excellent work, and it follows the #1275 design closely. Fork PRs don't get our full CI matrix, so I've moved your commit unchanged (authorship and sign-off intact) to #1766. It will be rebase-merged so it lands on main as yours. Two follow-up commits there: the Codex P1 (the migration now re-keys note_content.external_id when it adopts a twin's unwritten write), and the macOS-only tests now pin sys.platform so Linux and Windows CI cover the NFC path and the migration. Closing in favor of #1766.

@phernandez phernandez closed this Oct 14, 2026
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.

file_path lookups are byte-wise, so NFC/NFD filename variants create duplicate entities (macOS + Syncthing)

3 participants