# Repeater

The Repeater is the manual request workspace: tabs that each hold one
request draft, send it on demand, and keep every revision that was sent.
Tabs are research objects of their vault, kept in the database, so they
survive reloads and restarts and are shared by the interface and agents.

## Model

**Tab** (`repeater_tabs`, cascades from the vault)

| Field | Notes |
|---|---|
| `name` | Optional; null while the interface derives a label from the request. |
| `position` | Order of the open tabs, dense from 0. |
| `closed_at` | Set while the tab is closed; closed tabs can be reopened. |
| `source_flow_id` | The flow the tab was opened from. ON DELETE SET NULL. |
| `raw` | The draft: raw HTTP/1.x text, up to 10,000,000 characters. |
| `scheme`, `host`, `port` | The target. `host` and `port` may be null (below). |
| `follow_redirects`, `timeout`, `update_content_length` | Per-tab settings: off, 30 s (1 to 300), on. |
| `note` | Up to 4,000 characters. |
| `actor` | `user` (REST) or `agent` (MCP), set by the backend. |

**Send** (`repeater_sends`, cascades from the tab): one row per send that
ended. It stores the exact revision sent (`raw`, `scheme`, `host`, `port`
and the settings as `options`), what it resolved to (`method`, `url`),
the outcome (`status`: `completed`, `failed` or `cancelled`;
`status_code`, `response_size`, `duration_ms`, `error`), an optional
`note`, `actor` and `sent_at`. `flow_id` is the replay flow recorded in
history (ON DELETE SET NULL); the response itself lives there.

Migration: `b2f6c8d4a1e7_repeater_tabs` (after `e4b7d1a9c3f6`).

The captured request is never changed. A tab opened from a flow copies
its request; each send stores the revision it sent, so the sends of a tab
are its revision history, and any of them can be restored as the draft.

## Drafts and targets

A draft is parsed like an Intruder template, without positions
(`app.core.repeater.draft`, shared parsing with `app.core.intruder.request`):
request line, header lines, blank line, body; CRLF or LF endings; body
byte for byte. The destination is:

1. the request target itself, when it is an absolute `http(s)://` URL;
2. otherwise the tab's `host` and `port` (port defaults to the scheme's);
3. otherwise the host and port of the Host header.

A tab opened from a flow gets the flow's request as HTTP/1.1 text
(pseudo-headers dropped, origin-form target) and the flow's scheme, host
and port. A flow whose request body was cut by the capture limit, or is
not UTF-8 text, cannot be opened (422).

Drafts are saved as written, valid or not; they are checked when sent.

## Sending

A send goes through the same exchange as `/replay`
(`app.runtime.replay`): the upstream TLS policy, UTF-8 header values on
the wire, the raw response body (content codings kept) up to the capture
body limit, and a flow recorded in history with `source = replay`.
Differences from `/replay`:

- No client headers are added (no User-Agent, Accept or Accept-Encoding);
  only Host when missing and Content-Length for a body.
- With `update_content_length` (default), Content-Length is replaced by
  the real body length; otherwise it is sent as written.
- With `follow_redirects`, up to 5 redirects are followed and each hop
  is checked against the scope (and, for agents, the workstation guard).
  A refused hop ends the send as `failed` with the reason.

Before anything is sent, the destination host and the Host header host
must be in the vault scope (422 `out_of_scope`), and an agent may not
target the workstation's own API or MCP endpoint on this machine
(403 `permission_denied`). A refused or malformed
send (422 `invalid_value`) records nothing.

Network, TLS and timeout errors are results: the send is `failed` with
the error and the flow records it too. A tab sends one request at a time
(409 while sending; the tab's `sending` flag shows it). `cancel` stops a
send in flight: it is recorded as `cancelled` with no flow. Closing or
deleting a tab cancels its send. Sends in flight are not persisted; a
restart ends them without a record.

## Limits

| Bound | Value |
|---|---|
| Open tabs per vault | 200 (create, duplicate and reopen refused past it) |
| Closed tabs kept per vault | 100; closing another deletes the oldest closed tab and its sends |
| Sends kept per tab | 500; older sends are pruned after each send, their flows stay in history |
| Draft size | 10,000,000 characters (the replay body bound) |
| Name / note | 255 / 4,000 characters |
| Timeout | 1 to 300 seconds |
| Send page | 1 to 200 (default 50) |

## History retention

Deleting flows (`/flows/delete`), pruning (`/flows/prune`, scheduled
retention) and clearing the history keep tabs and sends: only
`source_flow_id` and `flow_id` become null. A send keeps its request
revision, status code and response size; its response body is gone with
the flow. Deleting a tab deletes its sends, not its flows.

## REST

Under `/api/v1/vaults/{vault_id}/repeater/tabs`:

| Route | Does |
|---|---|
| `GET ?closed=&limit=` | Open tabs in order, or recently closed tabs (newest first, default 20, at most 100). `TabSummary[]`: no draft text; `request_line`, `sending`, `send_count`, `last_send`. |
| `POST` | Open a tab: `{name?, flow_id?, raw?, scheme?, host?, port?, settings?, note?}`. 201 `TabRead` (summary + `raw` + `settings`). |
| `POST /reorder` | `{ids}`: every open tab id once, in the new order. |
| `GET /{tab_id}` | `TabRead`. |
| `PATCH /{tab_id}` | `{name?, raw?, scheme?, host?, port?, settings?, note?, position?}`; null clears name, host, port, note; `position` moves an open tab. |
| `DELETE /{tab_id}` | 204; sends deleted, flows kept. |
| `POST /{tab_id}/duplicate` | 201: a copy right after the tab (draft, target, settings, note, source flow; no sends). |
| `POST /{tab_id}/close`, `/reopen` | Close (kept for reopening) or reopen at the end; idempotent. |
| `POST /{tab_id}/send` | Optional body `{raw?, scheme?, host?, port?}` saved first. `SendResult {send, flow, tab}`. |
| `POST /{tab_id}/cancel` | `{tab_id, cancelled}`. |
| `GET /{tab_id}/sends?before_id=&limit=` | `SendPage {tab_id, items, next_before_id}`, newest first. |
| `GET /{tab_id}/sends/{send_id}` | `SendRead`: the summary plus `raw`, target and `options`. |
| `PATCH /{tab_id}/sends/{send_id}` | `{note}`. |
| `POST /{tab_id}/sends/{send_id}/restore` | The send's request and target become the draft; settings stay. |

Every route is vault-isolated: another vault's tab or send is 404.

## Events

- `repeater.updated` `{action, tab}` where `tab` is a summary (no draft
  text) and `action` is `created`, `updated`, `duplicated`
  (`source_tab_id`), `closed` (`dropped`: ids of closed tabs deleted by
  the bound), `reopened`, `restored` (`send_id`), `send_updated` (`send`)
  or `sending`; `{action: "reordered", ids}`; `{action: "deleted", tab:
  {id, vault_id}}`.
- `repeater.sent` `{tab_id, send}`: a send ended (summary, no request
  text). A send that recorded a flow also announces `proxy.flow`.

## MCP

| Tool | Does |
|---|---|
| `repeater_tab_list` | Open or recently closed tabs with their latest send. |
| `repeater_tab_get` | One tab with its draft (redacted). |
| `repeater_tab_create` | Open a tab from a flow, raw text, or empty. |
| `repeater_tab_update` | Change draft, target, settings, name, note; `closed` closes or reopens. |
| `repeater_send` | Send the draft (optionally replacing it first); returns the send, the flow like `flow_get`, and the tab. |
| `repeater_sends_list`, `repeater_send_get` | The send history and one revision. |

Tabs and sends an agent creates record `actor: agent`. Drafts, sends and
flows go through the MCP redaction policy; a raw request is redacted
line by line in its head and by Content-Type in its body. An agent edits
the draft as returned by `repeater_tab_get`: head lines and a body it
left in their masked form are restored to the stored originals before
saving (with `flow_id`, against the flow's request), and a draft that
would still carry a placeholder is refused (`invalid_value`). Sends by
agents are refused for the workstation's own API and MCP endpoint, also
after resolving dot segments, percent-encoding and host names. Reorder, duplicate, delete, cancel, send
notes and restore are left to the user's interface.

## Interface

Proxy → Repeater (`frontend/src/components/proxy/repeater/`, state in
`frontend/src/stores/repeater.ts`):

- **Tabs** are the vault's open tabs in order; they survive reloads and
  follow `repeater.updated` from other windows and agents. Drag a tab to
  reorder it; double-click, F2 or the tab menu renames; the tab menu
  (right click, or Shift+F10 on the tab) also duplicates, copies the
  tab's link (`[[repeater:<id>]]`), attaches it to the agent conversation,
  closes it and reopens the last closed tab. Alt+T opens a tab, Alt+W closes the active one,
  Alt+Shift+T reopens the last closed one, and the clock-arrow menu lists
  recently closed tabs (the browser keeps Ctrl+T and Ctrl+W for itself).
- **Draft**: edits are kept locally and saved shortly after typing stops
  (the footer says Saving…, Saved or Not saved); an outside change
  reloads the draft only while it has no unsaved edit. History's "Send to
  Repeater" opens a tab from the flow (`flow_id`); a body the backend
  cannot copy (binary, cut by the capture limit) falls back to the text
  the interface has, with a note.
- **Raw and Fields**: the switch under the request edits it as text
  (Raw) or as rows (Fields): method, path, query parameters and headers,
  written back into the raw text on each change. The body stays in the
  Raw view and is never changed by Fields; unedited parameters keep
  their exact text and edited ones are percent-encoded; the line ending
  style is kept. An edit that would break the request (a line break in a
  value, an invalid header name, a space in the path) is shown and not
  written, so a field edit cannot inject a header or split the request.
  Alt+Left and Alt+Right move a focused tab.
- **Send**: Ctrl/⌘+Enter anywhere in the Repeater. While a send is in
  flight the button becomes Cancel (Esc). The response card shows the
  response, a refused send's message (out of scope, malformed), the
  failure of a send (TLS failure panel with its host exception action,
  network, timeout) or "Cancelled". After a reload each tab shows the
  response of its last send.
- **History**: the clock button lists the tab's sends (outcome, size,
  time, when) with a note per send. Opening one shows a banner with its
  outcome and note, its response or the exact request it sent, and
  actions: Restore (the request becomes the draft), compare with the
  previous or the latest send that has a response (Comparer), back.
- **Settings**: the gear opens follow redirects, update Content-Length,
  timeout and the tab note.

Pure helpers (labels, status, order, target resolution) are tested in
`frontend/src/utils/__tests__/repeater.spec.ts`, the Fields
serialization in `requestFields.spec.ts`.

## Not yet

- Deleting a tab outright is not offered in the interface; closed tabs
  are kept and the oldest are dropped past 100.
- Response comparison is done by loading two sends' flows (Comparer);
  there is no server-side diff.
- Sends store their full request text each time; identical revisions
  are not deduplicated.
