# Research annotations

Tags, bookmarks, notes and saved views: one mechanism for every research
object ([Concepts](../concepts.md#research-objects-and-references)), so one tag
can join a captured request, a target and a finding into one line of
inquiry. One backend service (`app/services/annotations/`) serves the
interface, REST and MCP. Design background: `design/research-annotations.md`.

Everything is vault-scoped. A tag, view or object of another vault answers
exactly like one that does not exist.

## Model

**Tag** (`tags`): `id`, `vault_id`, `name` (1-48 characters, no control
characters or commas, unique per vault case-insensitively, case kept),
`color`, `created_at`, `updated_at`. Reads add `count`, the number of
objects carrying the tag.

Palette (`TagColor`), fixed so tags stay legible on the Kuro theme:
`gray` (default), `red`, `orange`, `amber`, `green`, `teal`, `blue`,
`violet`, `pink`.

**Annotation** (`annotations`, `annotation_tags`): at most one per object.

| Field | Notes |
|---|---|
| `object_kind`, `object_id` | `flow`, `target`, `finding` or `asset` ([Asset Intelligence](assets.md); migration `b7e5c3a9d2f8`). Unique together. The service checks the object exists in the route's vault (SQL cannot enforce a polymorphic reference). |
| `bookmarked` | The star. |
| `note` | Markdown, up to 4,000 characters, trimmed; empty means none. |
| `tag_ids` | Tags of the same vault, returned ascending. |
| `actor` | Last writer: `user` (REST, interface) or `agent` (MCP). Set by the backend. |

An annotation with no bookmark, note or tag is deleted. Deleting an object
deletes its annotation in the same transaction:

| Delete path | Annotations removed |
|---|---|
| `DELETE /vaults/{id}/flows` (clear history) | every flow annotation of the vault |
| `DELETE /vaults/{id}/flows/{flow_id}`, `POST /vaults/{id}/flows/delete` | the deleted flows' |
| `DELETE /vaults/{id}/targets/{target_id}` | the target's, and those of its findings (deleted with it) |
| `DELETE /vaults/{id}/findings/{finding_id}` | the finding's |
| `DELETE /vaults/{id}` | all of the vault's tags, annotations and views (ON DELETE CASCADE) |

Deleting a tag removes it from every annotation (an annotation left with
nothing is deleted) and from every saved view query.

**Saved view** (`saved_views`): `id`, `vault_id`, `module` (`history`),
`name` (1-64, unique per vault and module, case-insensitive), `query`
(versioned, below), `columns` (optional JSON layout, stored as given, up to
16 KiB), `pinned`. Listed pinned first, then by name.

Migration: `f3c9a7e5b2d4_research_annotations` (after `d8a4f6c2e1b3`).

## Limits

Constants in `app/core/annotations/limits.py`, enforced for every
interface; going over fails with `invalid_value` and stores nothing.

| Limit | Value |
|---|---|
| Tags per vault | 500 |
| Tags per object | 32 |
| Note | 4,000 characters |
| Objects per batch change or batch read | 1,000 |
| Saved views per vault and module | 200 |
| View query: hosts / methods / tag ids | 100 / 16 / 32 |

## REST

Paths are under `/api/v1/vaults/{vault_id}`. Errors use the standard body
`{"error": {"code", "message", "details"}}`.

### Tags

| Route | Body | Response |
|---|---|---|
| `GET /tags` | | `[Tag]` by name |
| `POST /tags` | `{name, color?}` | `201 Tag`; `409` for a taken name |
| `PATCH /tags/{tag_id}` | `{name?, color?}` | `Tag` |
| `DELETE /tags/{tag_id}` | | `204` |

```json
{"id": 3, "vault_id": 1, "name": "idor-candidate", "color": "red", "count": 12,
 "created_at": "2026-11-27T09:12:40Z", "updated_at": null}
```

### Annotations

| Route | Body / query | Response |
|---|---|---|
| `GET /annotations` | `kind`, repeated `ids` (needs `kind`), repeated `tag`, `bookmarked`, `before_id`, `limit` (1-1000, default 500) | `[Annotation]`, newest first; objects without one are left out |
| `GET /annotations/{kind}/{object_id}` | | `Annotation` (the empty state when there is none) |
| `PUT /annotations/{kind}/{object_id}` | `{bookmarked?, note?, tag_ids?}` | `Annotation` |
| `POST /annotations/batch` | `{kind, ids, bookmarked?, add_tag_ids?, remove_tag_ids?}` | `{kind, affected, annotations}` |

`PUT` changes only the fields sent: `bookmarked: null` keeps the bookmark,
`note: null` or `""` removes the note, `tag_ids` replaces the tags (`null`
or `[]` removes them). A missing object is `404`; a tag of another vault is
`422` with `{"field": "tag_ids", "tag_ids": [..]}`.

```json
PUT /api/v1/vaults/1/annotations/flow/812
{"bookmarked": true, "note": "Session fixation? Cookie survives login.", "tag_ids": [3, 5]}
```

```json
{"id": 41, "vault_id": 1, "object_kind": "flow", "object_id": 812, "bookmarked": true,
 "note": "Session fixation? Cookie survives login.", "tag_ids": [3, 5], "actor": "user",
 "created_at": "2026-11-27T09:14:02Z", "updated_at": "2026-11-27T09:14:02Z"}
```

An empty state reads `{"id": null, ..., "bookmarked": false, "note": null,
"tag_ids": [], "actor": null, "created_at": null, "updated_at": null}`.

The batch is for multi-select and never changes notes. It needs at least
one change, cannot add and remove the same tag, and is all or nothing:
an id that is not an object of the vault (`{"field": "ids", "missing":
[..]}`), a foreign tag, or an object that would exceed 32 tags refuses the
whole batch. `affected` counts objects whose bookmark or tags changed;
`annotations` holds every object's state in request order.

```json
POST /api/v1/vaults/1/annotations/batch
{"kind": "flow", "ids": [812, 813, 820], "bookmarked": true, "add_tag_ids": [3]}
```

```json
{"kind": "flow", "affected": 3, "annotations": [{"object_id": 812, "bookmarked": true, "tag_ids": [3, 5], "...": "..."}]}
```

### Saved views

| Route | Body / query | Response |
|---|---|---|
| `GET /views` | `module?` | `[View]` |
| `POST /views` | `{module: "history", name, query, columns?, pinned?}` | `201 View` |
| `PATCH /views/{view_id}` | `{name?, query?, columns?, pinned?}` (`columns: null` removes the layout) | `View` |
| `DELETE /views/{view_id}` | | `204` |

History query, version 1 (every field but `v` optional):

```json
{"v": 1, "methods": ["POST"], "status_classes": [4, 5], "hosts": ["api.example.com"],
 "in_scope": true, "search": "login", "tag_ids": [3], "bookmarked": true}
```

The backend validates the query on every write and stores it normalised
(methods upper-case, hosts lower-case, repeats dropped, every field
present). A missing or unknown `v` is refused with `{"field": "query.v",
"supported": [1]}`, unknown fields and bad values with per-field details,
and tags of another vault with `{"field": "query.tag_ids"}`.

## History filters

`GET /vaults/{id}/flows` evaluates every filter in the database, in the
same keyset query, so `before_id` pages stay exact under any filter:

| Parameter | Meaning |
|---|---|
| `host`, `method`, `status_class` | Repeatable; any of the values (a single value works as before). |
| `tag` | Repeatable tag id; flows carrying any of them. |
| `bookmarked` | `true` only starred flows, `false` the others. |
| `in_scope`, `search` | As before (`search`: host or path substring). |
| `view_id` | The saved history view's query must also hold; a missing view is `404`. |

Different filters, and the view, must all hold; request filters narrow a
view. Tag ids of another vault match nothing.

`FlowSummary` carries `bookmarked` and `tag_ids` (one extra query per page,
never one per row), so the history table needs no second request.
`FlowDetail` adds `note`. Flows without an annotation read `false` / `[]` /
`null`.

## Events

Announced after the commit, routed by `vault_id`:

| Event | Data |
|---|---|
| `tag.updated` | `{action: created\|updated\|deleted, tag, annotations?}` (`annotations`: how many carried a deleted tag) |
| `annotation.updated` | `{kind, ids, annotations: [{object_id, bookmarked, tag_ids, has_note, actor, updated_at}]}` |
| `view.updated` | `{action: created\|updated\|deleted, view}` |

`annotation.updated` never carries note text: the bus reaches every
listener and a note may hold pasted text. `has_note` tells the interface to
read the note when it shows one. A batch announces once, listing only the
objects that changed. Deleting a tag sends `tag.updated` (clients drop the
id locally) and `view.updated` for each view that lost it. MCP clients get
`notifications/resources/updated` for `phantom://vault/{id}/flows/recent`
on flow annotation changes and tag deletions.

## MCP

`tag_list`, `tag_create`, `tag_update`, `tag_delete`; `annotation_get`,
`annotation_set`, `annotation_batch`; `view_list`, `view_create`,
`view_update`, `view_delete`; and `flow_list` takes `tag`, `bookmarked`,
`view_id` plus the lists `hosts`, `methods`, `status_classes`. The tools
call the same services as REST. Everything an agent writes records `actor:
agent`; agent notes are stored as given.

Redaction: notes are user-written knowledge, not captured data, but a user
may paste a token into one. Notes therefore go through the text redactor
under the MCP redaction policy, in `annotation_*` results and in
`flow_get`, and `annotation_get` accepts `reveal_secrets` when the policy
allows it. Because a redacted note copied back would overwrite the user's
text with placeholders, `annotation_set` refuses a note that contains a
placeholder (`invalid_value`, `{"field": "note"}`); omit `note` to change
only the bookmark or tags. Tag names and view queries are not redacted.

## Interface

Proxy > History (`stores/annotations.ts`, `stores/history.ts`,
`components/annotations/`):

- **Star column**: click the star, or press `B`, to bookmark the open
  request or every selected request. Starred rows keep a gold star.
- **Tags column**: coloured chips (up to three, then `+n`). `T` opens the
  tag popover: search, check or uncheck tags (a dash when only some
  selected rows carry one), or type a new name, pick a colour and press
  Enter to create and apply it.
- **Multi-select**: Ctrl/Cmd+click and Shift+click select several rows; a
  floating bar bookmarks or tags all of them in one batch request, `Esc`
  clears it. A right click on a selected row acts on the whole selection.
- **Row menu**: Bookmark, Tags (checkable submenu, New tag…), Note….
- **Request panel**: star, removable tag chips and a Markdown note under
  the summary; the note saves itself a moment after typing stops and shows
  Saving… / Saved.
- **Filters**: search, method, status, tags, host and the sidebar scope
  all run on the backend (`GET /flows` with keyset pages), so every match
  is found, not only loaded rows. "Failed requests" is the exception: a
  failure has no status code, so that status choice filters loaded rows.
  Search matches host and path.
- **Sidebar**: All traffic, In scope, Bookmarked, then pinned saved views;
  other views in their own group. "Save view" in the toolbar saves the
  current filters (on top of an open view) as a pinned view. Right-click
  a view to rename, pin or unpin, or delete it.
- **Live updates**: `annotation.updated`, `tag.updated` and `view.updated`
  update rows, tags and views from other windows and agents; a scope
  change (`proxy.rescoped`) reloads the rows with the current filters.

Targets and findings (`components/annotations/PObjectAnnotations.vue`,
`composables/useObjectAnnotation.ts`):

- **Target page**: a "Bookmark, tags and note" card on the overview and
  in the Notes section. **Finding inspector**: the same block on the
  overview tab, under triage.
- Star, removable tag chips, the same tag popover as History (search,
  check, create with a colour) and an autosaved Markdown note.
- A tag chip opens the tag's page (below); its tooltip shows how many
  objects in the vault carry it.
- The state is read with `GET /annotations?kind=&ids=` when the object
  changes and reloaded on `annotation.updated` events for it, so changes
  from agents or other windows show at once. Escape inside the tag
  popover closes only the popover, not the inspector.

Tag manager (`components/annotations/PTagManager.vue`, `PTagRow.vue`),
opened from "Manage tags…" at the foot of the tag popover or of the
History tag filter:

- Every tag of the vault with its count, most used first, with a filter
  once there are more than six.
- Click the name to rename it (Enter saves, Escape cancels), the dot to
  pick one of the nine palette colours.
- Delete asks in place and says how many objects carry the tag; deleting
  also removes it from saved views (backend). Changes reach every open
  view through `tag.updated`. The arrow on a row opens the tag's page.

Tag page (`/tags/<id>`, `views/TagView.vue`, `components/tags/`,
`composables/useTagPage.ts`): everything that carries one tag, so a
captured request, a target and a finding read as one line of inquiry.

- Reached from any tag chip (History table, request panel, target and
  finding annotations), the tag manager, and the command palette
  ("Tag: <name>").
- Header: the tag as a large chip (click the name to rename, the dot to
  recolour), figures for requests, targets, findings, bookmarked and
  noted objects, "Show in History" (History filtered to the tag), and
  delete with an in-place confirmation that returns to the previous page.
- Sections for requests, targets and findings with their bookmark and the
  first line of their note. A request opens History filtered to the tag
  with that request selected (`/proxy?tag=<id>&flow=<id>`); a target
  opens its page; a finding opens the inspector.
- Data: `GET /annotations?tag=<id>` (all kinds, keyset pages of 1,000,
  up to 5,000) joined with `GET /flows?tag=<id>` and the loaded targets
  and findings; an object that is gone keeps a "No longer available" row.
  Reloads on `annotation.updated` and `tag.updated`. Right-click an
  object to open it, copy its link (`[[kind:id]]`) or attach it to the
  agent conversation.
- The sidebar lists every tag of the vault with its count. Right-click a
  tag (or Shift+F10) to open it, show it in History, rename it in place
  (F2), recolour it, or delete it after a confirmation in the row that
  says how many objects carry it.

## Not yet

Moving payload collection tags ([Payload collections](payloads.md)) onto vault tags, asset
and scan annotations, tag hierarchies, shared tags and annotation
history.
