# Capture: setup, heal & expand

> Explore a running app live via --record, write .pitafix/capture.json, CAPTURE.md, and a replay script (or pitafix.trace/1), heal failing captures, and cover dark files.

PitaFix maps a UI project in two phases: **interactive exploration** (drive the running app live via `--record` MCP tools) followed by **deterministic replay** (commit `.pitafix/capture.json`, `.pitafix/CAPTURE.md`, and a small replay script in the project's language).

## Choose the capture mode

| Target stack | `kind` | Recording path |
| --- | --- | --- |
| Web app (Next.js, React, Rails, Vue, static) | `"web"` | Localhost Chromium/CEF recorder (`POST /v1/sessions` or `--record` MCP) |
| Native iOS (Xcode 26+, iOS 26+, `swapify`) | `"mobile"` | Swapify iOS recorder (`platform: "ios"`; run `swapify init --no-agents` once) |
| Android, KMP, Flutter, SwiftUI without Swapify, desktop | `"mobile"` / `"native"` / `"trace"` | Write Level-0 `pitafix.trace/1` + `pitafix.tree/1` folders into `$PITAFIX_TRACES_DIR/<unique-run-id>/` |

## Phase 1: Explore live (`--record` MCP)

1. Run `pitafix doctor --project . --json` to verify local toolchains.
2. Boot the app on fresh fixture or local data (follow [Trust & boundaries](https://pitafix.app/wiki/agents/trust-and-boundaries.md#2-capture--secret-hygiene)).
3. Call `record_start` (`base_url` + `data_source: "fixture"` for web, or `platform: "ios"` for Swapify iOS).
4. Start each flow with `record_journey` using a stable `id`, `name`, and `section`.
5. Drive screens with `record_step`, inspecting live DOM/elements between actions via `record_inspect`. Pass `private: true` on sensitive web inputs and `route` to normalize dynamic URLs (`/items/123` → `/items/:id`).
6. Report unreached routes via `record_unreached`, then close with `record_finish`.

## Phase 2: Write `.pitafix/`

1. **`.pitafix/capture.json`** (full schema and byte limits in [Schemas](https://pitafix.app/wiki/agents/schemas.md#1-pitafixcapturejson-pitafixproject-capture1)):

```json
{
  "schema": "pitafix.project-capture/1",
  "kind": "web",
  "name": "Storefront",
  "command": ["node", ".pitafix/capture.mjs"],
  "diffScoped": true
}
```

2. **`.pitafix/capture.<ext>`** (`capture.mjs`, `capture.rb`, `capture.py`, `capture.go`, `capture.sh`):
   - Boots and tears down local fixture servers/DBs cleanly in `finally` / `ensure` / `defer`.
   - Calls `$PITAFIX_RECORDER_URL` with `Authorization: Bearer $PITAFIX_RECORDER_TOKEN` (endpoints and minimal Node, Swapify iOS, and Rails attribution examples in [Recorder API](https://pitafix.app/wiki/agents/recorder-api.md)).
   - Honors diff-scoped runs when `diffScoped: true`: when `PITAFIX_DIFF_SCOPED === "1"`, run only journey IDs in `PITAFIX_AFFECTED_JOURNEYS` (exit `0` cleanly when empty).
   - For non-web Level-0 captures, write `$PITAFIX_TRACES_DIR/<unique-run-id>/trace.ndjson` + `artifacts/*.png` + `artifacts/*.tree.json` (format in [Schemas](https://pitafix.app/wiki/agents/schemas.md#2-level-0-trace-pitafixtrace1)); PitaFix auto-ingests on exit (or run `pitafix ingest --project . <dir>`).
3. **`.pitafix/CAPTURE.md`**: concise playbook covering boot commands, fixture seeding, test login, journey inventory, and gotchas for future runs.

### Sidecar bundles for read-only repos

Place `capture.json`, `CAPTURE.md`, and the script in `<PITAFIX_DATA_DIR>/projects/<rootCommit>/capture/` (or run `pitafix record run --project <repo> --config /path/to/capture.json`). Resolve sibling fixtures from the script's own path, locate the repo via `$PITAFIX_PROJECT_ROOT` (fallback: cwd), export a throwaway copy with `git -C "$PITAFIX_PROJECT_ROOT" archive "${PITAFIX_COMMIT:-HEAD}" | tar -x -C "$tmp"`, and delete `$tmp` on exit.

## Heal a broken capture

1. Read `.pitafix/CAPTURE.md`, `.pitafix/capture.json`, the Capture Log failure report, and recent `git diff`.
2. Reproduce the failing step via `--record` MCP and inspect live roles/labels/`test_id`s with `record_inspect`.
3. Patch the script using resilient `role` + `name` or `testId` selectors while preserving existing journey `id`s so screen comments stay attached. Update `.pitafix/CAPTURE.md` if boot or seed steps changed.

| Symptom | Likely cause | Fix |
| --- | --- | --- |
| `proxyRefused` non-empty | External host blocked by loopback proxy | Mock locally (preferred) or add bare hostname to `allowedHosts` for human approval |
| `/step` returns HTTP `200` with `status: "failed"` | Missing selector, timeout, or blocking modal | Inspect `screenshotPath` and `elements` returned in the failed step or call `record_inspect` |
| `attributionsCount: 0` on web steps | Missing source maps or uninstrumented backend | Enable source maps on local build, or post backend files to `$PITAFIX_ATTRIBUTION_URL` via `X-Request-Id` |
| Capture hangs until timeout | Child server inherited stdio or ignored `SIGTERM` | Stop servers/DBs in cleanup hooks and close inherited stdio |
| Diff-scoped run fails on 0 matches | Script treated empty `PITAFIX_AFFECTED_JOURNEYS` as error | Exit `0` when `PITAFIX_DIFF_SCOPED === "1"` and `PITAFIX_AFFECTED_JOURNEYS` is empty |

## Expand coverage for new screens & dark files

1. Run `pitafix affected --project . --from HEAD~1 --json` (or MCP `list_dark_files` + `get_app_map`) to locate `NewDarkFiles` and `unreachedRoutes`.
2. Drive the new routes live with `--record` MCP using existing fixture/auth helpers from `.pitafix/CAPTURE.md`.
3. Add the new journeys/steps to `.pitafix/capture.<ext>` and drop reached routes from `unreached`.

## Completion criteria

1. `pitafix record run --project .` exits `0` with `failedSteps: 0`, ingesting screenshots, UI trees, and attributions.
2. Existing journey IDs stay intact unless a flow was removed from the app; newly targeted routes drop from `unreachedRoutes` / `list_dark_files`.
3. Zero dev servers, test databases, or temp directories remain after exit, and `.pitafix/CAPTURE.md` matches the script.
