# Vault files

Status: **available** (backend, REST and MCP in Phase E; the Files page
in Phase F; every note's file, `[[file:id]]` references, graph nodes and
evidence attachments in Phase G).

Every vault has its own file workspace. Files are research objects: they
have identity, a folder, provenance, revisions, tags and an owner (the
vault). Nothing outside the vault can reach them, and nothing in a file
can widen what an agent may do.

## What a file is

| Field | Notes |
|---|---|
| `id` | stable; references use it, so renames and moves never break links |
| `vault_id` | owner; deleting the vault deletes its files and their storage |
| `folder_id` | the vault's folder tree (shared with Notes) or none |
| `note_id` | set when the file is a Markdown document that is a research note |
| `name`, `extension` | Unicode NFC; unique per folder ignoring case; safe names only |
| `mime`, `kind` | read from the bytes, not trusted from the name (see Safety) |
| `size`, `sha256` | of the current content |
| `revision`, `version` | current revision number; `version` grows on every change |
| `flags`, `quarantined` | what type detection noticed; quarantined files are never read as text |
| `source`, `origin` | `upload`, `created` or `duplicate`; the uploaded name or `file:<id>` |
| `actor`, `agent_id` | the last writer, from the authenticated principal |
| `creator`, `creator_agent_id` | the first writer |
| `pinned`, `tag_ids` | bookmark and the vault's research tags |
| `state` | `active`, `archived` or `trashed` |
| `editable` | text that takes new text (Markdown and text kinds, not quarantined) |

Kinds: `markdown`, `text`, `image`, `pdf`, `archive`, `executable`,
`binary`.

## Folders

Files and notes share **one folder tree per vault** (`note_folders`), so
a folder means the same thing in Notes and in Files. Folders are created,
renamed, moved and deleted with the note folder routes
(`/vaults/{id}/notes/folders`); deleting a folder moves its notes, files
and subfolders to its parent, and stops with `409` when a moved file's
name is taken there. `POST /vaults/{id}/files/starter-folders` creates
whichever of Research, Evidence, Payloads, Notes, Reports, Scripts,
Documents and Attachments the top level does not have yet. Nothing is
imposed.

A file is placed by `name` in a `folder_id`, or by a relative `path`
through existing folders (`Evidence/login/request.txt`, case-insensitive),
below `folder_id` when one is sent.

## REST

All under `/api/v1/vaults/{vault_id}/files`.

| Route | What it does |
|---|---|
| `GET ''` | page of files with `total`: `folder_id` (+ `recursive`), `unfiled`, `state`, repeated `kind` and `tag`, `pinned`, `quarantined`, `actor`, `search` (words in the name), `sort` (`name`, `updated`, `created`, `size`, `kind`), `order`, `offset`, `limit` |
| `POST ''` | create a text file from `text`, by `name` or `path` |
| `POST /upload` | upload one file; the body is the raw content, `name` or `path` and `folder_id` in the query |
| `GET /{id}` | metadata |
| `PATCH /{id}` | rename, move (`folder_id: null` for the top level), tags, bookmark |
| `POST /{id}/duplicate` | copy (`<name> copy`, `<name> copy 2`, …); stored content is shared |
| `POST /{id}/archive`, `/unarchive` | archive (out of default lists, still readable) or bring back |
| `GET /{id}/text` | text from `offset`, in characters, with `version` for the next save |
| `PUT /{id}/text` | new text as a new revision; `expected_version`, `message` |
| `GET /{id}/preview` | bounded preview: 1 MiB of text, or 64 KiB of base64 bytes for a hex view |
| `GET /{id}/content` | download (`inline=true` for display; `acknowledge_risk=true` for quarantined files) |
| `PUT /{id}/content` | replace the content with the raw body, as a new revision; `expected_version`, `message` |
| `GET /{id}/verify` | recompute the SHA-256 and compare it with the recorded one |
| `GET /{id}/revisions`, `/revisions/{n}`, `/revisions/{n}/content` | history, one revision, its bytes |
| `POST /{id}/revisions/{n}/restore` | make a revision current again, as a new revision |
| `DELETE /{id}` | move to the trash |
| `POST /{id}/restore` | bring back from the trash (`name (2)` when the name was taken meanwhile) |
| `POST /{id}/purge` | delete a trashed file for good |
| `DELETE /trash` | purge everything in the trash |
| `GET /stats` | storage statistics and limits |

Changes announce `file.updated` (`action`: created, updated, trashed,
restored, archived, unarchived, restored_revision, note_changed) and
purges `file.deleted` on the realtime channel of the vault.

Saves are guarded by the version: a save or restore sent with an
`expected_version` that is no longer current is refused with `409
conflict`, and the details carry the current `version` and `revision`,
so the editor can offer to keep yours, take theirs or compare.

## Revisions

Every change to a stored file's content adds an immutable revision:
number, size, checksum, type, actor, agent, time and an optional message.
Revisions reference content-addressed bytes, so restoring an old
revision or duplicating a file copies nothing. Retention keeps the newest
`FILES_REVISIONS_KEPT` (default 50) within `FILES_REVISION_BYTES`
(default 1 GiB) per file; the newest revision always stays, a revision
pinned as evidence by an attachment is never dropped (and does not count
toward the bounds), and bytes no revision references any more are removed
after the change commits.

A note-backed file's history is the note's revision history (see
[Markdown files and notes](markdown-notes.md)).

## Trash and purge

Deleting moves a file to the trash. A trashed file keeps its revisions,
cannot be changed, and is left out of every list but `state=trashed`.
Purging is only possible from the trash, and only for the user: rows go,
and stored bytes no other file or revision references are removed.

## Evidence attachments

A file can be attached to a finding or a report as evidence. Attaching
copies nothing: it pins the file's current revision and keeps what the
file was (name, size, SHA-256, type), with an optional note of up to 200
characters saying what it shows.

- Editing the file later never changes the evidence; the attachment shows
  **changed since**, and **Pin the current version** moves the pin.
- A pinned revision survives retention.
- Purging an attached file keeps the attachment, marked **file deleted**,
  with the name and SHA-256 that were evidence.
- Deleting the finding or report removes its attachments, never the file.
- A final (locked) report's files only change once it is reopened.
- A rendered report lists a finding's files under **Evidence files**:
  name, pinned revision, size, SHA-256 and the note, as they were
  pinned. A snapshot keeps them as they were when it was taken. A
  finding with evidence files no longer gets the `no_evidence` warning.
- File and object must be in the same vault. A trashed file cannot be
  attached.

| Method | Route | Does |
|---|---|---|
| GET | `/vaults/{id}/attachments?kind=finding&object_id=3` | Files attached to a finding or report |
| POST | `/vaults/{id}/attachments` | Attach `{file_id, kind, object_id, note?}`; 200 when re-pinned |
| PATCH | `/vaults/{id}/attachments/{attachment_id}` | `{note}` or `{repin: true}` |
| DELETE | `/vaults/{id}/attachments/{attachment_id}` | Detach; the file stays |
| GET | `/vaults/{id}/files/{file_id}/attachments` | Where a file is attached |

Changes are announced as `attachment.updated` and `attachment.deleted`.
In the interface, a finding's **Evidence** tab and the **Files** tab of
Report Studio list the attached files and attach one by search; the file
inspector shows **Attached to** and, with **Attach to…**, searches the
vault's findings and reports. **Attach as evidence…** in the file menu
opens that search.

## Safety

Files are untrusted research data.

- **Names.** NFC, trimmed, at most 255 UTF-8 bytes; no `/ \ < > : " | ? *`,
  no control, zero-width or direction override characters (they can
  disguise `exe.txt` as `txt.exe`), no trailing dot, no reserved device
  names (`CON`, `NUL`, `COM1`, …), not `.` or `..`. Paths are relative:
  a leading `/`, a drive letter, a backslash or an empty, `.` or `..`
  segment is refused before any lookup. Names never become storage paths.
- **Types.** The leading bytes decide: PNG, JPEG, GIF, WebP, PDF, ZIP,
  gzip, 7z, RAR, xz, tar, ELF, PE, Mach-O, WebAssembly, SQLite and others
  are recognised; UTF-8 without NUL bytes is text. A disagreement with the
  extension is flagged `mime_mismatch`.
- **Quarantine.** Native programs and installers (by bytes or extension)
  are `executable` and quarantined: listed with metadata and a hex
  preview, never read as text, never sent to an agent, downloaded only
  with `acknowledge_risk=true` and then as `application/octet-stream`.
- **Scripts** (`.sh`, `.ps1`, `.bat`, a `#!` line, …) are flagged `script`
  and stay readable text. Phantom never runs, unpacks or fetches anything
  from a file.
- **Active content.** HTML, SVG and XHTML are flagged `active_content`.
  They are served as `application/octet-stream` for downloads and as
  `text/plain` inline, never as markup.
- **Responses.** Every content response carries
  `X-Content-Type-Options: nosniff`, `Cache-Control: no-store`,
  `Cross-Origin-Resource-Policy: same-origin` and a content security policy
  that allows no scripts, requests or plugins (`sandbox` for everything but
  inline PDF), plus `X-Phantom-Sha256`. Inline display is limited to text,
  raster images and PDF proven by their bytes.
- **Integrity.** Whole-text reads check the SHA-256 first; changed bytes
  on disk are reported (`409`, reason `integrity`) instead of served.
  `GET /verify` checks any file.
- **Limits.** One file at most `FILES_MAX_BYTES` (default 100 MiB), one
  vault at most `FILES_VAULT_QUOTA` of stored contents (default 5 GiB),
  text saves through JSON at most 8 MiB, at most 50,000 files per vault.
  Over a limit: `413 too_large` with `limit` and `reason` (`file`,
  `quota`, `text`); nothing is kept.

## Agents

Agents use the MCP tools listed in [MCP](../agents/mcp.md#vault-files):
`file_list`, `file_get`, `file_read`, `file_create`, `file_update`,
`file_write`, `file_delete`, `file_revisions`, `file_folder_list`,
`file_folder_create`, `file_stats`, `file_attachments`, `file_attach`,
`file_detach`.

- Every tool needs a `files` grant; a note-backed Markdown file also needs
  the `notes` grant for the same action, so a files grant never reaches
  notes it was not given.
- Agents read text files only, at most 100,000 characters per call, through
  the MCP redaction policy. Binary and quarantined content is refused, never
  forwarded to a model provider.
- Text that still carries a redaction placeholder is refused on write, so a
  redacted read can never overwrite the stored credentials.
- Writes record `actor: agent` and the agent's id from its token.
- `file_delete` moves to the trash. Purging, emptying the trash, restoring,
  archiving, binary uploads and restoring revisions are user actions.
- Attaching or detaching needs `files.update` and `findings.update` or
  `reports.update`; listing attachments needs `files.read` and the
  object's read grant. Contents never travel with an attachment.
- File content is data, never instructions. It cannot grant or widen access.

## Storage statistics

`GET /stats` returns the file count, current sizes, archived and trashed
counts and sizes, note-backed and quarantined counts, revision count and
bytes, `stored_bytes` (each distinct content once, which is what counts
against the quota), `quota_left`, the ten largest files and the configured
limits.

## The Files page

**Files** in the rail (`G E`) is the vault's file workspace.

- **Sidebar.** Search by name, then the places: All files, Pinned,
  Recent (the fifty changed last), Unfiled, Archived and Trash. Below
  them is the folder tree, the same one Notes uses. Create a folder with
  the plus, rename it with a double click or F2, and drag files or
  folders onto a folder (or onto Unfiled) to move them. An empty tree
  offers the usual top-level folders. The footer counts files and bytes.
- **List.** A list or a grid. Click a column header to sort. Click
  selects a file, Ctrl or Cmd adds to the selection, Shift extends it,
  and Ctrl+A selects all. Double click or Enter opens a file. F2 renames
  in place and Delete moves to the trash. A selection of several files
  gets a bar with archive and trash (restore and delete for good in the
  trash). Every row has a right-click menu with the same actions plus
  pin, duplicate, download, and copying the reference (`file:<id>`) or
  the SHA-256. Drop files from your computer onto the list to upload
  them into the folder shown.
- **Open file.** Text opens in place with line numbers and optional
  wrapping. An editable file saves with Ctrl+S as a new revision. If
  someone else (another window or an agent) saved since you opened it,
  the save is refused and you can load their version. PNG, JPEG, GIF,
  WebP, BMP, ICO and AVIF images, and PDF, are shown from the sandboxed
  content route. Everything else, including SVG and quarantined files,
  gets a hex view of its first 64 KiB. A Markdown file that is a
  research note has **Open in Notes**.
- **Inspector.** Type, size, folder, source, who created and last
  changed the file, and its reference. **Verify** recomputes the
  SHA-256. The revisions list restores an older revision as a new one,
  after a confirmation in place.

`?file=<id>` on `/files` opens a file directly.

## Not yet

- In the Files page: tabs for several open files, replacing a file's
  content by upload, and viewing the bytes of an older revision.
- Portable export, content search, automatic trash expiry and storage
  adapters other than local storage.

See [File storage](storage.md) for storage internals and
`design/vault-files.md` for the interface.
