# Architecture

> The parts of screw-bugs, and what happens from an agent's request to a finished report.

Source: https://docs.screwbugs.dev/screw-bugs/architecture

`screw-bugs` is a CLI and an MCP server around a platform-free engine. It has no AI of its own: your coding agent writes the cases, and screw-bugs runs them in a real headless browser and decides every status in code.

## The parts

`src/` holds one folder per module. `schema`, `core` and `report` are their own package, `@screw-bugs/core`, which `screw-bugs` re-exports.

| Module               | Owns                                                                                                                 | Code                               |
| -------------------- | -------------------------------------------------------------------------------------------------------------------- | ---------------------------------- |
| `schema`             | Schemas of every file a run reads or writes: `JobSpec`, `CaseLedger`, `ReportBundle`, `Findings`                     | `packages/core/src/schema/job.ts`  |
| `core`               | The engine (`Engine`, `runCase`) and the services it runs on: `Browser`, `Storage`, `ReportRenderer`, `AccountBook`  | `packages/core/src/core/engine.ts` |
| `report`             | The renderers: `renderHtmlReport`, `renderWalkthrough`, `renderPrSummary`, `renderLivePage`                          | `packages/core/src/report/html.ts` |
| `browser-playwright` | The `Browser` service on Playwright: launches Chrome, Edge or Chromium, runs steps, takes screenshots, records video | `src/browser-playwright/launch.ts` |
| `store-local`        | The `Storage` and `AccountBook` services on disk, under `.screw-bugs/`                                               | `src/store-local/storage.ts`       |
| `cli`                | The CLI commands, the MCP server, the local report server and the sign-in live view                                  | `src/cli/mcp.ts`                   |

> **Note: Why the split**
>
> `@screw-bugs/core` depends only on `effect`: no Node, no Playwright. `pnpm lint` and `pnpm
>   check:workers` keep it that way, so it runs on Cloudflare Workers.

## The life of a run

1. **Plan.** The agent writes a job: a URL, a `summary`, and cases of structured steps.
2. **Start.** It calls the MCP tool `start_run` (or you run `screw-bugs run --spec job.json`). The reply is a job ID, right away.
3. **Record.** The engine saves `job.json` and starts `ledger.json` in `.screw-bugs/runs/<job-id>/`.
4. **Run.** Cases run in order, in one browser session. Each step acts, waits or checks; screenshots and one continuous video are saved as it goes.
5. **Judge.** Code sets each case to `passed`, `failed`, `blocked` or `not_verified`. A live page, `live.html`, follows the run.
6. **Report.** When the run ends it writes `evidence.json`, `report.html` and `walkthrough.html`.
7. **Review.** The agent reads the evidence pack from `run_status`, then sends its write-up with `submit_findings`.
8. **Re-render.** screw-bugs checks the write-up against the run, saves `findings.json`, draws `annotated/` screenshots and writes the report again.

```text
agent ──start_run──▶ MCP server ──▶ Engine ──▶ Browser (Playwright, headless)
                                      │              │
                                      ▼              ▼
                     .screw-bugs/runs/<job-id>/  evidence/ video/
                     job.json ledger.json evidence.json
                                      │
agent ◀──run_status── evidence pack ◀─┘
agent ──submit_findings──▶ findings.json ──▶ report.html + walkthrough.html
```

## How Cloud reuses it

screw-bugs Cloud runs the same engine from `@screw-bugs/core` on Cloudflare Browser Run, and renders shared reports with its renderers. See [Cloud runs](/cloud/cloud-runs.md).

**Next:** [Cases and steps](/screw-bugs/cases-and-steps.md)