# Agent Bridge routes

Every route of the Bridge API (`/api/v1/bridge`), its body and its answer.
Rules, states and events: [Agent Bridge protocol](bridge-protocol.md). Schemas
live in `backend/app/schemas/bridge/`.

## Objects

**Participant** `{kind: "user" | "agent" | "provider" | "system", agent_id: "agt_…" | null}`

**Ref** `{kind, id, revision_id?}`: `kind` one of `vault`, `target`, `scan`,
`flow`, `ws_session`, `ws_frame`, `repeater_send`, `note`, `note_folder`,
`file`, `finding`, `report`, `report_section`, `payload_collection`,
`asset`, `comparison`, `repeater`, `repeater_tab`, `tag`, `view`, `attack`,
`conversation`, `message`, `task`, `skill`; `id` a number or a string of 1
to 64 characters. A `skill` reference (`skills.read`) is a skill the user
picked for the message; agents load it with `skill_get`.

**Conversation**

```json
{ "id": 3, "vault_id": 1, "title": "Login review", "agent_id": "agt_…", "provider_id": null,
  "pinned": false, "archived_at": null, "share_context": true,
  "created_by": {"kind": "user", "agent_id": null},
  "created_at": "…", "updated_at": "…", "last_seq": 12, "last_event_id": 4810, "last_message_at": "…" }
```

**Message**

```json
{ "id": 41, "conversation_id": 3, "vault_id": 1, "seq": 12,
  "sender": {"kind": "agent", "agent_id": "agt_…"}, "type": "text",
  "content": "Markdown", "refs": [{"kind": "flow", "id": 812}], "task_id": null,
  "delivery": "queued" | "delivered" | "failed", "failed_reason": null,
  "streaming": false, "attempts": 0,
  "created_at": "…", "delivered_at": null, "failed_at": null, "completed_at": "…" }
```

**Task**

```json
{ "id": 7, "vault_id": 1, "conversation_id": 3,
  "requested_by": {"kind": "user", "agent_id": null}, "assigned_agent_id": "agt_…",
  "action": "summarise traffic", "instructions": "", "context_refs": [],
  "state": "running", "state_reason": "started",
  "progress": {"percent": 40.0, "text": "reading"}, "result_text": null, "result_refs": [], "error": null,
  "attempts": 1, "max_attempts": 3, "lease_expires_at": "…", "deadline_at": null, "sensitive": false,
  "created_at": "…", "updated_at": "…", "started_at": "…", "completed_at": null, "cancel_requested_at": null }
```

The lease id appears only in the claim answer.

**Approval**

```json
{ "id": 2, "vault_id": 1, "conversation_id": 3, "task_id": 7, "message_id": 44,
  "requested_by": {"kind": "agent", "agent_id": "agt_…"},
  "operation": "Replay request #812 with a modified token", "resources": [{"kind": "flow", "id": 812}],
  "grant": "testing.execute", "reason": "…", "state": "pending" | "approved" | "denied" | "expired",
  "expires_at": "…", "decided_at": null, "decided_by": null, "note": null, "created_at": "…" }
```

**Event** `{id, type, vault_id, conversation_id, created_at, data}` where
`data` is the stored payload plus the attached object
([Agent Bridge protocol](bridge-protocol.md#events)).

## Handshake and conversations

| Route | Body / query | Answer |
|---|---|---|
| `POST /hello` | `{protocol, client: {name, version?}, capabilities: []}` | `200` hello ([Agent Bridge protocol](bridge-protocol.md#handshake)); `426` |
| `GET /conversations` | `vault_id` (required), `search`, `pinned`, `archived` (false), `offset` (0), `limit` (50, max 200) | `{items: [Conversation], next_offset}`: pinned first, then by last message |
| `POST /conversations` | `{vault_id, title?, agent_id?, provider_id?, share_context?}` | `201` Conversation. The user names the agent or provider (`share_context` default true); an agent's is with itself (default false) |
| `GET /conversations/{id}` | | Conversation |
| `PATCH /conversations/{id}` | `{title?, pinned?, archived?, share_context?}` (user only) | Conversation |
| `GET /conversations/{id}/messages` | `after_seq` (0), `limit` (50, max 200) | `{items: [Message], next_after_seq}`, in `seq` order; delivers the recipient agent's queued messages |
| `POST /conversations/{id}/messages` | header `Idempotency-Key`; `{type?, content, refs?, task_id?, streaming?}` | `201` Message, `200` for a repeated key; `409` archived |

## Messages

| Route | Body | Answer |
|---|---|---|
| `POST /messages/ack` | `{message_ids: [..]}` (agents) | `{delivered: [ids that changed]}` |
| `POST /messages/{id}/fail` | `{reason}` (recipient agent; `[a-z0-9_.-]`) | Message |
| `POST /messages/{id}/retry` | (user only, failed user messages) | Message, back to `queued` |
| `POST /messages/{id}/deltas` | `{text, offset?}` (sender of a streaming message) | `{message_id, offset, length}` |
| `POST /messages/{id}/finalize` | `{content?}` (sender) | Message with the whole text |

## Tasks

| Route | Body / query | Answer |
|---|---|---|
| `GET /tasks` | `vault_id`, `state` (repeatable), `conversation_id`, `limit` | `[Task]`, newest first |
| `POST /tasks` | `{vault_id, conversation_id?, assigned_agent_id?, action, instructions?, context_refs?, max_attempts? (1..10), deadline_at?, sensitive?}` | `201` Task (`queued`) |
| `GET /tasks/{id}` | | Task |
| `POST /tasks/{id}/claim` | `{lease_seconds?}` or no body (agents) | `{task, lease_id, lease_expires_at}`; `409` when claimed |
| `POST /tasks/{id}/progress` | `{lease_id, percent?, text?}` | Task |
| `POST /tasks/{id}/complete` | `{lease_id, result_text?, result_refs?}` | Task |
| `POST /tasks/{id}/fail` | `{lease_id, error: {code, message?}}` | Task |
| `POST /tasks/{id}/cancel` | (user or requesting agent) | Task |
| `POST /tasks/{id}/pause`, `/resume` | (user only) | Task |

## Approvals

| Route | Body / query | Answer |
|---|---|---|
| `GET /approvals` | `vault_id`, `state` (repeatable), `limit` | `[Approval]`, newest first; an agent sees its own |
| `POST /approvals` | `{conversation_id?, task_id?, lease_id?, operation, resources?, grant?, reason?, expires_in?}` (agents) | `201` Approval |
| `GET /approvals/{id}` | | Approval |
| `POST /approvals/{id}` | `{decision: "approve" | "deny", note?}` (user only) | Approval; `409` once decided |

## Events and context

| Route | Query / headers | Answer |
|---|---|---|
| `GET /events` | `after`, `Last-Event-ID` | `text/event-stream` |
| `GET /events/poll` | `after`, `wait` (0..25 s), `Last-Event-ID` | `{events: [Event], cursor, reset}` |
| `GET /context` | `window?`, `conversation_id?` | `{window, version, updated_at, shared, untrusted: true, redacted, notice, context}` ([Design: Workspace Context Service](../design/workspace-context.md)) |
