# Data models

These pages list every SQLAlchemy model in `backend/app/models/`. Concrete
models inherit from `BaseModel`, which provides `id`, `created_at` and
`updated_at`. The schema is managed by Alembic (`backend/migrations/`), and
`migrations/env.py` imports `app.models` to collect the metadata.

Each model is marked with how it is exposed today:

- **API**: has REST endpoints under `/api/v1` (and the matching MCP tools)
  and is used by the UI.
- **Schema only, not exposed yet**: the table exists, but no router,
  service or UI reads or writes it.

| Model | Table | Exposure |
|---|---|---|
| [Vault](workspace.md#vault) | `vaults` | API: `/vaults` |
| [Target](workspace.md#target) | `targets` | API: `/vaults/{id}/targets` |
| [Scan](workspace.md#scan) | `scans` | API: `/vaults/{id}/scans`, `/engines` |
| [Finding](workspace.md#finding) | `findings` | API: `/findings` |
| [Signature](workspace.md#signature) | `signatures` | API: `/signatures` |
| [ProxyFlow](traffic.md#proxyflow) | `proxy_flows` | API: `/vaults/{id}/flows`, `/vaults/{id}/replay` |
| [WsSession, WsFrame](traffic.md#wssession-wsframe) | `ws_sessions`, `ws_frames` | API: `/vaults/{id}/flows/{flow_id}/frames` ([WebSocket sessions](../../guides/proxy/websocket.md)) |
| [AppSetting](traffic.md#appsetting) | `app_settings` | Indirect: through `/proxy/settings`, `/proxy/intercept` and `/proxy/intercept/rules` |
| [ResearchAsset, AssetObservation, AssetRelation, AssetEvent, AssetSyncState](research.md#researchasset-assetobservation-assetrelation-assetevent-assetsyncstate) | `research_assets`, `asset_observations`, `asset_relations`, `asset_events`, `asset_sync_states` | API: `/vaults/{id}/assets` ([Asset Intelligence](../../guides/assets.md)) |
| [Module](research.md#module) | `modules` | Schema only, not exposed yet |
| [Workflow](research.md#workflow) | `workflows` | Schema only, not exposed yet |
| [Note, NoteFolder, NoteRevision, NoteLink](research.md#note-notefolder-noterevision-notelink) | `research_notes`, `note_folders`, `note_revisions`, `note_links`, `note_tags` | API: `/vaults/{id}/notes` ([Research notes](../../guides/notes.md)) |
| [VaultFile, FileRevision, FileAttachment](research.md#vaultfile-filerevision) | `vault_files`, `file_revisions`, `file_tags`, `file_attachments` | API: `/vaults/{id}/files` ([Vault files](../../files/index.md)) |
| Comparison | `comparisons` | API: `/vaults/{id}/comparisons` ([Comparer](../../guides/comparer.md)) |
| [AgentToken, Agent, AgentAudit](agents.md#agenttoken-agent-agentaudit) | `agent_tokens`, `agent_token_vaults`, `agents`, `agent_audit` | API: `/connections/...` (user only; [Agent authentication](../../agents/authentication.md)) |
| [Agent Bridge models](agents.md#agent-bridge-models) | `agent_conversations`, `agent_messages`, `agent_tasks`, `agent_approvals`, `agent_events`, `workspace_contexts` | API: `/bridge/...` (agents with `bridge` grants and the user; [Agent Bridge protocol](../../agents/bridge-protocol.md)) |
| [SystemConfig](agents.md#systemconfig) | `system_configs` | Not in the schema: no migration creates the table |

## Pages

| Page | Models |
|---|---|
| [Vaults, targets, scans and findings](./workspace.md) | Vault, Target, Scan, Finding, Signature |
| [Traffic and settings](./traffic.md) | ProxyFlow, WsSession, WsFrame, AppSetting |
| [Research objects](./research.md) | ResearchAsset, AssetObservation, AssetRelation, AssetEvent, AssetSyncState, Module, Workflow, Note, NoteFolder, NoteRevision, NoteLink, VaultFile, FileRevision |
| [Agents and system](./agents.md) | AgentToken, Agent, AgentAudit, Agent Bridge models, SystemConfig |

## BaseModel

**File:** `base.py` · **Abstract:** yes

| Field | Type | Notes |
|---|---|---|
| `id` | Integer | Primary key, indexed |
| `created_at` | DateTime (tz) | `server_default=now()` on insert |
| `updated_at` | DateTime (tz) | Set to `now()` on update; null until the first update |

---

## Relationship map

```
Vault ──┬── Target ──┬── Scan ──── Finding
        │            ├── Finding
        │            └── (self: parent / children)
        ├── ProxyFlow
        ├── ResearchAsset ──┬── AssetObservation (source by id, no FK)
        │                   ├── AssetRelation (to asset FK; finding / target by id)
        │                   └── AssetEvent
        ├── AssetSyncState
        ├── Workflow
        ├── NoteFolder ── (self: parent)
        └── Note ──┬── NoteRevision
                   ├── NoteLink (kind + object id, no FK)
                   └── Tag (note_tags)

Module ──── Scan
Signature ── Finding
Finding ──── (self: duplicate_of_id, FK only)
AppSetting   (standalone key/value)
AgentToken ──┬── agent_token_vaults ── Vault
             ├── Agent
             └── AgentAudit (agent, token SET NULL; vault by id)
Vault ──┬── AgentConversation ──┬── AgentMessage (seq per conversation)
        │                       └── (agent SET NULL)
        ├── AgentTask ── (conversation, agents SET NULL)
        ├── AgentApproval ── (task, conversation CASCADE)
        └── WorkspaceContextRecord (one per vault)
AgentEvent   (vault, conversation, agent by id; ordered log)
SystemConfig (standalone, not migrated)
```

Delete behaviour: deleting a Vault removes its Targets, ProxyFlows,
Workflows, Notes and note folders (database `CASCADE`). Deleting a Vault also removes its
assets with their observations, relations, history and sync state, and its
Bridge conversations, messages, tasks, approvals and saved context (Bridge
events keep plain ids and age out with retention).
Deleting a Target removes its Scans and Findings (asset edges to them are
dropped at the next sync); notes that reference them keep the
reference, which reads as missing. Deleting a Scan keeps its Findings and
sets their `scan_id` to null.
