# Markdown files and notes

Status: **available**. Phase E made Markdown files notes; Phase G gives
every note its file, adds `[[file:id]]` references and joins the two
names of one document in backlinks and the graph.

A Markdown document in Phantom is **one** object with two views: the
Research Notebook and the file workspace. There is never a second copy.

## The rule

- The note's body (`research_notes.body`) is the only content of a
  Markdown document.
- A Markdown file is a `vault_files` row with `note_id` set. It has no
  stored bytes and no file revisions of its own: reading, previewing or
  downloading it serves the note body, and its history is the note's
  revisions (`number` is the note revision id).
- The file's folder, bookmark and tags are the note's. Moving the file
  moves the note; tagging the file tags the note; moving or tagging the
  note in Notes shows in Files.
- Its size and SHA-256 follow the note body on every save, in the same
  transaction, wherever the save came from (Notes, Files, an agent, a
  revision restore), and `file.updated` (`note_changed`) is announced next
  to `note.updated`.

## Why the note keeps the content

Note bodies already drive search, link extraction on save, revisions,
backlinks, the graph, export and agent tools. Moving the body into file
storage would rebuild all of that and risk existing notes. Keeping the
body in the note and pointing the file at it keeps every note id, link,
revision and tag as it is.

## Which Markdown becomes a note

A `.md` (or `.markdown`) file becomes a note when it is created in place,
uploaded or duplicated, if its content is UTF-8 of at most 1 MiB (the
note limit); a byte order mark is dropped. Larger or non-UTF-8 Markdown is
stored as a plain file (`note_id: null`). Saving more than 1 MiB into a
note-backed file is refused.

## Names and titles

A file has a **name** (`Login bypass.md`); a note has a **title**. The
note starts with the name without its extension; after that they are
independent: renaming the file does not retitle the note and the other
way round. References use ids, so renames and moves never break them.

## Lifecycle

| In Files | Effect on the note |
|---|---|
| archive / unarchive | the note is archived / restored |
| delete (trash) | the note is archived |
| restore from the trash | the note is restored |
| purge | the note is deleted, with its revisions |

Archiving or restoring the note in Notes archives or restores the file
(and takes it out of the trash). Deleting the note deletes the file
(`file.deleted`).

## Concurrent edits

The file's `version` grows with every save of the note. A save through
Files sent with an older `expected_version` is refused with `409
conflict` and the current version, whether the other save came from
Notes, Files or an agent. Saves in Notes without a version keep their
current behaviour (last write wins) and are attributed.

## Permissions

A note-backed file is a note. On top of the `files` grant, agents need
`notes.read` to read it, `notes.create` to create one, and `notes.update`
to write, move, tag, archive or trash it. A files grant alone never
reaches notes.

## Every note has a file

A note made in Notes (or by an agent's note tool) gets its Markdown file
in the same transaction, in the note's folder. A note from before Phase G
gets one at startup: the backfill gives a file row to every note that
has none and never touches a note that has one, so running it again
changes nothing.

The file is named after the note's title: characters a file name cannot
hold become spaces, trailing dots and spaces go, a reserved device name
(`CON`, `NUL`, …) gets an underscore, and `.md` is added. When the folder
already has that name, the file is `<title> (2).md`, `<title> (3).md` and
so on. Its source is `note` ("Written in Notes" in the inspector). Note
ids, bodies, links, revisions, tags and folders are never changed by
giving a note its file.

A vault that already keeps the most files allowed (50,000) gets no new
file rows; its notes work as before and get their files on a later
start, once there is room.

The open note's header has **Show in Files**, which saves the note and
opens its file in Files; the file's **Open in Notes** goes the other way.

## References and the graph

`[[file:id]]` names a file of the vault. It resolves to the file's name,
or reads as missing for an id that is not a file of this vault. A
Markdown file that is a note and the note itself are one document:

- backlinks of the note include notes that wrote `[[file:id]]` of its
  file, and backlinks of the file include those that wrote
  `[[note:id]]`;
- the graph draws `[[file:id]]` of a note's file to the note's node, so
  there is never a second node for the same document;
- any other file is a graph node of its own, labelled with its name.

Clicking a file reference opens a note's file as that note in Notes, and
any other file in Files.
