Architecture
One portable review engine, three hosts. The engine has no Cloudflare
imports; Cloudflare services are reached only through adapters. Decisions and
their alternatives are recorded in
openspec/changes/yomorai-v0-0-1/design.md (D1–D20).
flowchart LR subgraph hosts[Hosts] CLI[CLI / MCP server / Action local mode<br/>adapters-node] W[Cloudflare Worker<br/>adapters-cloudflare] end subgraph engine[packages/engine] P[runReview: twelve stages] end subgraph core[packages/core] C[Coordinator<br/>single writer of state] L[Budget ledger] G[Classification gate] SR[Stage runner] end A[analysis-visual / -dom / -perf / -runtime<br/>pure functions over evidence] B[browser<br/>executor + production guard] AI[ai<br/>projection, redaction, adapters] CLI --> P W --> P P --> SR --> C P --> L P --> G P --> A P --> B P --> AIPackages
Section titled “Packages”| Package | Holds | May depend on |
|---|---|---|
schemas |
zod schemas for configuration, findings, evidence, reviews, the API; error codes | zod |
core |
Protocols: lifecycle state machine, coordinator, budget ledger, stage runner, classification gate, ports (interfaces), configuration loader | schemas, security, telemetry only |
engine |
runReview(deps, input), the pipeline every host runs |
everything below |
intent |
Route map, impact analysis, intent record | |
scenarios |
Scenario planning, limits, denylist, preconditions | pure |
browser |
Playwright executor, production guard (guard.ts), storage-state restriction |
|
analysis-visual, analysis-dom, analysis-perf, analysis-runtime |
Deterministic comparisons | pure: no adapters, browsers or Lighthouse |
ai |
Allowlisted prompt projections, delimiting, redaction guard, OpenAI-compatible adapter, structured output | |
auth-profiles |
Encrypted credentials, scripted and human-assisted login | |
deployments |
Discovery providers, readiness, build-identifier check, origin allowlist | |
github |
App authentication, REST client, webhook parsing, manifest | |
reporting |
Check run, comment, text, HTML and JSON reports, with escaping | pure |
testgen |
Playwright test candidates from verified scenarios | |
storage |
SQL metadata store (D1 and SQLite), content-addressed blobs, migrations, retention, export | |
security |
Hashing, HMAC, encryption, redaction, escaping, globs | pure |
telemetry |
Logs, metrics, traces | |
api |
The HTTP API (Hono), shared by the Worker and yomorai serve |
|
adapters-node |
Local Chromium, SQLite, filesystem blobs, env secrets, createLocalEngine |
|
adapters-cloudflare |
Browser Run, D1, R2, Workers AI, secrets | the only package that may import Cloudflare modules (with apps/worker) |
The rules in the last column are enforced by ESLint no-restricted-imports
and scripts/check-boundaries.mjs (part of pnpm lint).
Applications: apps/worker (the deployment), apps/cli, apps/mcp,
apps/github-action, apps/perf-runner (Lighthouse service), apps/web
(dashboard), apps/landing (public site), apps/sample-app (test subject).
The twelve stages
Section titled “The twelve stages”Defined once in STAGES (packages/schemas/src/review.ts) and run in order
by packages/engine/src/pipeline.ts:
| # | Stage | Does |
|---|---|---|
| 1 | discover_deployments |
Find production and preview, check readiness, origin allowlist and build identifier |
| 2 | analyze_intent |
Route map, impact analysis, optional reasoning model |
| 3 | plan_scenarios |
Configured, changed-route and critical-route scenarios, within limits |
| 4 | execute_preview |
Run scenarios in preview; learn which steps would change state; derive further scenarios from the page |
| 5 | execute_production |
Run them in production, twice, guarded; stop before state-changing steps |
| 6 | compare |
Visual, DOM, accessibility, functional and runtime comparison into observations |
| 7 | measure_performance |
Lighthouse for routes marked performance |
| 8 | describe_visual |
Optional visual model describes visual observations |
| 9 | classify |
Optional reasoning model proposes; the gate decides each finding |
| 10 | generate_tests |
Test candidates from verified scenarios |
| 11 | report |
Consolidated report, conclusion, cost |
| 12 | publish |
Check run and comment, only if the commit is still current |
Each stage runs through runStage (packages/core/src/stage-runner.ts):
check the deadline, ask the coordinator to checkpoint (continue or stop),
record the start, run, store the result, record the finish. A stored result
means the stage is never run again (resuming after a restart). Retryable
errors are retried up to 3 attempts with jittered exponential backoff (1 s to
20 s).
Browser executions: one bounded pool
Section titled “Browser executions: one bounded pool”Stages 4 to 6 share one dependency-driven pool of browser executions
(packages/engine/src/pipeline.ts, pool.ts):
- At most N executions are in flight, each in a fresh context of one browser:
budgets.limits.maxConcurrentExecutions, else the host default (4 locally, 2 whenCIis set, 4 contexts per Browser Run browser).1is sequential. - A scenario’s production pass 1 starts as soon as its own preview has finished, pass 2 after pass 1, while previews of other scenarios fill the free slots. Each scenario is compared as soon as its executions have landed.
- Every finished execution and comparison is recorded as a part of its stage, so a restarted review repeats only unfinished work. The stages then collect the parts in plan order, never in completion order, and identifiers derive from stable keys (review, scenario, environment, pass): the report is the same whatever the pool’s scheduling.
- Before each execution the coordinator is consulted and the duration limit checked; at the limit, executions in flight are stopped and their contexts closed.
- Readiness after a navigation and after each step is event-driven: a
configured application signal or selector, otherwise network idle observed
from request events, with a bounded wait (
analysis.readiness).
On Cloudflare the same work is split into Workflow steps: one stage:<name>
step per stage, and exec:<wave>:<n> batch steps of four scenarios fanned
out in parallel, each holding one of the repository’s browser slots in its
RepoAgent and comparing screenshots inside the browser
(apps/worker/src/review-steps.ts).
Coordinator and ledger: single writers
Section titled “Coordinator and ledger: single writers”sequenceDiagram participant GH as GitHub participant W as Worker participant RA as RepoAgent (one per repository) participant WF as ReviewWorkflow (one per review) participant LG as LedgerObject (one per installation) GH->>W: pull_request webhook (HMAC verified first) W->>RA: handlePullRequestEvent (dedupe, supersede, create) GH->>W: /yomorai approve <sha> W->>RA: approve (live permission, exact head) RA->>WF: start loop every stage WF->>RA: checkpoint WF->>LG: reserve before spend / settle after end WF->>RA: authorizePublication (head still current?) WF->>GH: check run + commentCoordinator (packages/core/src/coordinator.ts) is the only code that
changes review state, approvals and publication permits. Every method is
synchronous and runs to completion, so a host that calls it from one place at
a time gets atomic transitions without locks. On Cloudflare that place is the
RepoAgent Durable Object; in the CLI and MCP server it is the process.
BudgetLedger (packages/core/src/budget.ts) works the same way, held by
LedgerObject per installation, with money in integer micro-dollars.
These two classes are what the TLA+ models describe and what the conformance
tests drive (Verification).
| Host | Browser | Store | Coordinator | Performance |
|---|---|---|---|---|
| CLI, MCP server, Action local mode | local Chromium (Playwright) | SQLite + evidence folder in .yomorai/data |
in-process | Lighthouse in-process |
| Cloudflare Worker | Browser Run | D1 + R2 | RepoAgent / LedgerObject Durable Objects; ReviewWorkflow runs the stages |
Lighthouse container (PerfRunner) or PERF_RUNNER_URL |
createLocalEngine (packages/adapters-node/src/local.ts) builds a complete
host in one process; the Worker builds the same EngineDeps from its
bindings. The CLI and the engine called directly produce equal findings for
equal inputs (verification/e2e/cli.e2e.test.ts, CLI-005).
Metadata tables (one migration set for D1 and SQLite, see
packages/storage/src/migrations.ts): reviews, stage results, evidence
(write-once, never deleted; only marked expired), deployments, scenarios,
evidence gaps, findings, intent records, reports, test candidates, usage
events, the audit log (append-only hash chain) and its head, configuration
versions, encrypted authentication profiles, API tokens, host state. Evidence
bytes live in the blob store under evidence/sha256/<hash>.