# Replaying a recording offline

The "replay" part of the loop. Work against the recording before touching the real site again. No daemon needed. Every option is in [commands.md](commands.md); `analyze deps` / `chain` are in [analysis.md](analysis.md).

- `tabcap replay serve <bundle> [--port 4500]` serves the recorded responses on 127.0.0.1 until Ctrl-C (method + path + sorted query + body sha256; `x-tabcap-replay: r-12;n=2` says which recording answered; no match → 404 listing the nearest recordings and what differs). A key recorded several times answers in recorded order, cycling: when the first attempt was a failure (a 403, then the 200 the scraper needs), pass `--prefer 2xx` (only the 2xx recordings of each key; all when a key has none) or `--prefer latest` (the last one); the header then ends in `;prefer=2xx`. The same `prefer` can sit in a rule (`"prefer": "2xx"`, beside `match` / `key` / `response`); `replay check --prefer` checks only the recordings that answer and lists the rest as `notPreferred`. A recorded CORS preflight answers an `OPTIONS` on its path like any recording; an unrecorded one is a 404.
- **A script's own requests** (a Python requests / httpx scraper, curl, node): `tabcap record -o <bundle> [--upstream-ca ca.pem] -- <command>` runs the command behind tabcap's proxy on 127.0.0.1 with a CA made for this run and deleted after it (both cases of `HTTP(S)_PROXY` set, `NO_PROXY` emptied, `SSL_CERT_FILE` / `REQUESTS_CA_BUNDLE` / `CURL_CA_BUNDLE` / `NODE_EXTRA_CA_CERTS` pointed at it) and writes its requests into a bundle in the order it sent them. `--upstream env` (default) goes out the way the command would have (its `https_proxy` …, minus `no_proxy`); `none` direct. HTTP/1.1 only; WebSocket frames are not recorded; aiohttp (`trust_env` off) and hard-coded `proxies=` are not caught (use `TABCAP_RECORD_PROXY` / `TABCAP_CA_FILE`). Exit 0; the command's code is `exitCode`. The site sees tabcap's TLS handshake, not the script's.
- **Order check**: `tabcap replay serve <bundle> --order strict -- <command>` runs the same command against the recording (https to any host lands on the replay server, nothing else is contacted) and, when it exits, reports the request order against the recorded one: `missing`, `extra`, `changed` (same method + path, other query / body), `moved`, `firstDivergence`; answers do not change. Exit code: the command's when not 0, else 1 when the order differs. Check a subset with `--order-include` / `--order-exclude` path globs or `"order": "skip"` in a rule; `--order-report <file>` keeps the report. Without a command, send the requests yourself; the report comes at Ctrl-C / `--once`.
- **Key rules** (`--rules replay-rules.json` on `replay serve`, `replay check` and `bundle export fixtures`; without the flag `config.json` `sites.<site>.replay`, then `replay`): when a scraper's requests differ from the recording only in volatile parts, write rules instead of a replay script. `{"schema": "tabcap-replay-rules/0", "rules": [{"match": {"host", "path" (glob), "method"}, "key": {"ignoreQuery": ["t", "nonce"], "body": "raw|form|json|decoded|none", "ignoreBodyFields": ["$.ts"], "decodeWith": "<decode plugin>", "caseInsensitiveQuery": true}, "response": {"compare": "status|status+body|status+decoded-body", "ignorePaths": ["$.serverTime"]}}]}`; the first matching rule wins, no match = the old key. `form` sorts the pairs, `json` compares canonical JSON, `decoded` keys on a decode plugin's plaintext (encrypted bodies). `ignoreBodyFields` / `ignorePaths` are [JSON paths](search.md) (or, without `$`, a form field / top-level name). `x-tabcap-replay: r-12;n=1;rule=rules[0]` names the rule, `x-tabcap-replay-ignored` lists ignored parts that differed, and a 404 says per candidate what differs (`query`, `body`, `ignored`). Every field: docs/04 §31.
- `tabcap bundle export fixtures <bundle> --rules replay-rules.json --out ./fixtures` writes one JSON fixture per recorded exchange (`key` as the rules read it, `request`, `response` with `decoded`, `meta`) plus `index.json`, redacted like a HAR export (`--no-redact` keeps the login body with its password). `--format <plugin>` hands each fixture to a fixture plugin (`"kind": "fixture"`) that writes the team's own test format. CORS preflights (`OPTIONS`) are left out (reason `preflight`) unless `--include-preflight`; the answer's `skipped` counts the left-out exchanges per reason (`{"websocket": 113, "preflight": 26}`) with up to 3 ids each in `skippedExamples`, and `index.json` lists every one; a rule's `prefer` decides which recordings of a key become fixtures (the ones `replay serve` answers with; the rest are listed in `notPreferred`). `bundle export har --rules` only drops the ignored query parameters.
- `tabcap replay check <bundle> --against http://127.0.0.1:4500` sends every recorded request again and compares status, headers and body field by field; exit 1 on any difference. The answer is JSON; `--text` prints one line per request that is not the same, then the totals. **Never use `--live` (a real site) without the user asking for it.** `--live` sends from the CLI, outside the daemon: it is never faster than the site's `fetch.minIntervalMs` (default one request per 3 s; a faster `--rate` is slowed down, with a warning), it is refused with `guard.stopped` while the daemon has the site's fetches stopped (resume as for `fetch`), and when no daemon runs a warning says stop rules were not checked; its answers are not recorded and no stop rule judges them.
- `tabcap replay session <bundle> -o storage-state.json` writes the recording's cookies as a Playwright storageState file (mode 0600): a live session — never print, commit or share it.
- `tabcap session export --for requests|httpx|playwright --out <file> [--origin <url|host>]` (daemon, the session's current tab) hands the logged-in session to a script: the tab's cookies, the headers the page sends to authenticate (sensitive or page-set headers of the latest recorded request to the origin's host: run `net start` before the page makes its requests, else `headers` is empty and `source.headers.request` is null) and its localStorage / sessionStorage (values over 64 KB skipped and listed). A single-page app often sends its `Authorization` to another origin (its API): `--origin https://api.example.com` (or just the host) takes the headers sent there from any tab and adds that origin's cookies to the page's, while the tab stays on the app. Without `--origin`, when the page's origin got no auth-looking header, the recorded host carrying the most of them is used and the answer says so (`source.headers: {request, origin, auto: true}`). `requests` = JSON (`tabcap-session-export/0`; `_tabcap_session.py` in an export-python package turns it into an httpx client), `httpx` = an importable module with `cookies`, `headers`, `storage` and `client(base_url)`, `playwright` = storageState (no headers, no sessionStorage). The file is 0600 and holds live credentials; the answer has the path, counts and header names, never a value. Never print, commit or share it; it expires with the session.
- `tabcap replay export-python <bundle> -o ./client` writes an httpx client, fixtures and a pytest suite that runs against `replay serve`. Requests with signature-like parameters or encrypted bodies become `NotImplementedError` TODOs: do not try to reproduce those values; that request stays in the real browser session. Each function sends only the request headers a browser does not add itself (content-type, authorization, a non-default accept, a cross-origin referer, custom `x-*` …); `--browser-headers` sends every recorded one (user-agent, accept-language, `sec-ch-ua*`, `sec-fetch-*` …).

## Record → deps → chain → export-python → verify

`analyze deps` says where every input of every request came from; `analyze chain` lists the requests needed for one request. When the graph links requests, `replay export-python` follows the chain to the same target `analyze chain` picks by default (the data-bearing request that started last, among the exported ones), or to `--request r-N` (as `analyze chain --request`; use it when the page's last request is a stats or tracking call): values from earlier replies become arguments (a `Bearer ` / quoted header gets its prefix back around the argument), `caller-input` values become plain parameters whose default is the recorded value, `computed-in-page` inputs become parameters defaulting to `TODO` (the comment names the hints and the co-varying inputs), and `client.py` gets `verify()` plus `verify_expected.json`. Run `uv run client.py verify --base-url <replay serve url>` first (uv creates the environment from the generated `pyproject.toml` on first run; the tests: `TABCAP_REPLAY=<url> uv run --group test pytest`); it must pass with the recorded values. Filling a TODO belongs to the user's own project, in `todo_inputs.py` next to `client.py` (never by editing `client.py`), and only when the user asks for it; a fill is right when `verify` still passes against `replay serve`. `verify` takes every input the chain resolves from an earlier reply from that reply, tokens and CSRF values included (a JSON path, the text around the value in HTML, or a decode plugin's plaintext through the installed plugin). Other secrets are never written out: `verify` reads `<secret:…>` arguments from `TABCAP_SECRET_*` environment variables, and `underived` in `verify_expected.json` says why such an input could not come from a reply.
