# Comparer

The Comparer diffs two captured exchanges: two requests or two responses,
as full messages or bodies only, side by side, in one unified column, or
as bytes. Comparisons are research objects of their vault, kept by the
backend: the one being filled survives reloads and restarts, and named
comparisons can be saved, pinned, annotated and linked from notes. The
backend stores which flows are compared and how; the diff itself is
computed in the interface from the two flows in history.

## Model

**Comparison** (`comparisons`, cascades from the vault)

| Field | Notes |
|---|---|
| `title` | Up to 200 characters; required for saved comparisons, null for the working one. |
| `saved` | False for the vault's working comparison (at most one), true for saved ones. |
| `pinned` | Saved comparisons only; pinned ones are listed first. |
| `left_flow_id`, `right_flow_id` | The flows shown as A and B, or null for an empty side. No foreign key (like note references). |
| `left_ref`, `right_ref` | What each side pointed at when it was placed: method, scheme, host, port, path, status, request and response size, capture time. |
| `options` | `part` (`request` / `response`), `view` (`split` / `unified` / `bytes`), `body_only`, `pretty` (format JSON), `ignore_whitespace`, `collapse` (fold unchanged), `ignored_headers` (up to 100 lower-case names). |
| `note` | Up to 4,000 characters. |
| `actor` | `user` (REST) or `agent` (MCP), set by the backend on every change. |

Migration: `a4d7e2c9f1b6_comparisons` (after `c8e2f4a6b9d1`), additive.

Deleting or pruning history never touches a comparison. A side whose flow
is gone reads as **missing**: the API returns the kept reference with
`missing: true`, so the interface still shows what was compared. A flow id
that now names a different flow (a reused id) is detected by its capture
time and also reads as missing.

## Working and saved comparisons

Every vault has one working comparison. The interface creates it the
first time a flow is placed (`PUT /comparisons/working`) and writes its
sides and options as they change; it is not listed with the saved ones.
"Save as…" copies what is open into a new saved comparison with a title
and an optional note, and opens it. A saved comparison can be changed in
place (swap, clear a side, options, title, note, pin); picking a flow
from History, Repeater or Intruder always fills the working comparison,
so a saved one is never overwritten by accident.

Intruder results that were not saved to history have no flow id: they
are compared for the browser session only, and their side is stored
empty. Save the result to history first to keep it.

## Limits

| Bound | Value |
|---|---|
| Saved comparisons per vault | 500 (the working one is not counted) |
| Title | 200 characters |
| Note | 4,000 characters |
| Ignored headers | 100 names of up to 128 characters, HTTP token characters |

Flows placed on a side must belong to the vault (404 otherwise). A side
set to the flow it already holds is accepted even when that flow is gone.
Unknown option fields, views and parts are refused (422).

## REST

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

| Method and path | Does |
|---|---|
| `GET /comparisons` | Saved comparisons, pinned first, then most recently changed (`limit` up to 500), with flow summaries. |
| `POST /comparisons` | Save a named comparison (`title`, sides, `options`, `note`, `pinned`). 201. |
| `GET /comparisons/working` | The working comparison, or `null` before one exists. |
| `PUT /comparisons/working` | Set its sides and options (omitted fields unchanged, null empties a side); creates it on first use. |
| `GET /comparisons/{id}` | One comparison. |
| `PATCH /comparisons/{id}` | Title, pin, note, sides, options (merged). Title and pin on the working comparison: 409 `invalid_state`. |
| `DELETE /comparisons/{id}` | Delete it; the flows stay in history. 204. |

Each side is returned as `{id, method, scheme, host, port, path,
status_code, request_size, response_size, captured_at, missing}`; bodies
are never part of a comparison.

Events: `comparison.updated {action: created|updated, comparison: {id,
vault_id, saved}}` and `comparison.deleted {id, vault_id, saved}`, with no
title, note or flow data.

## MCP

| Tool | Does |
|---|---|
| `comparison_list` | Saved comparisons of a vault. |
| `comparison_get` | One comparison: flow summaries and options. Agents read the exchanges with `flow_get`. |
| `comparison_create` | Save a comparison of two flows (default title "Flow A vs flow B", optional note and part), actor `agent`. |

Titles, notes and paths go through the MCP redaction policy. Changing and
deleting comparisons, and the user's working comparison, are left to the
interface.

## Notes

`[[comparison:id]]` references a comparison from a note. It resolves to
the saved title ("Working comparison" for the working one), opens it in
the Comparer, and reads as missing from another vault or after deletion.
The sidebar menu copies the reference.

## Findings

**Create finding from this comparison…** in a saved comparison's menu
opens a new finding for what the comparison shows: the target matching
the right-hand request's host, its URL, the comparison's title, and a
description that references the comparison and both requests
(`[[comparison:id]]`, `[[flow:id]]`). It is unavailable while either side
is empty or its request was deleted.

## Interface

Proxy → Comparer (`frontend/src/components/proxy/comparer/`, state in
`frontend/src/stores/comparer.ts` and `comparisons.ts`):

- **Header**: the open comparison (working, or a saved title with its pin
  and note), the view switch Split / Unified / Bytes, "Save as…", and for
  a saved comparison "Details…" and the way back to the working one.
- **Slots** A and B with method, masked URL and status; a missing side
  shows what it was, struck through, "deleted from history". Swap (⇄ or
  S), clear per side.
- **Options**: requests or responses, body only, format JSON, ignore
  whitespace, fold unchanged, ignored headers, reveal secrets. They are
  stored with the comparison and remembered (with the view) as the start
  of the next one.
- **Split**: side by side, paired changed lines, changed characters
  marked, long values folded.
- **Unified**: one column; in each change the `-` lines of A come before
  the `+` lines of B, with both line numbers and the same character
  marks; unchanged stretches fold into "N unchanged lines" with
  `@@ -a,b +c,d @@` hunk headers.
- **Bytes**: A and B side by side, each with its own offset, 16 bytes a
  row in hex and ASCII, virtualized up to 1 MiB per side. The sides are
  aligned: an inserted or removed byte shows as `--` on the other side and
  only that byte is highlighted, so the rest stays lined up. Messages too
  different to align quickly (many scattered changes in large bodies) are
  compared position by position, and the view says so. The message is the start line and kept headers with CRLF line
  endings, a blank line, then the body bytes as captured (binary bodies
  decoded from base64; text bodies as shown by the viewer).
- **Masking**: credentials are masked in every view until revealed; in
  the bytes view each byte of a credential shows as `••` / `•`. The diff
  always runs on the original data and the captured bytes are never
  changed.
- **Keys**: N / P or F7 / Shift+F7 move to the next / previous change
  (in every view), S swaps A and B.
- **Sidebar**: "Comparisons" lists the working comparison and the saved
  ones; right-click a saved one (or Shift+F10 on it) to open, rename in
  place (F2), pin, edit its note in the side panel, copy its note
  reference, attach it to the agent conversation or delete it (Delete;
  confirmed in place).

Pure helpers are tested in `frontend/src/utils/__tests__/comparer.spec.ts`
(text, folding, cells) and `comparerViews.spec.ts` (unified lines and
hunks, message bytes, byte masking, byte rows, option defaults and list
order).

## Not yet

- No server-side diff; agents compare the two flows themselves.
