rewrit-core
Parity engine for validating observable behavior during rewrites.
Install
Run this in your project folder. cargo add rewrit-core
Rewrit
Rewrit is a parity engine for software rewrites.
It compares the observable behavior of a reference implementation and a candidate implementation so teams can move code across languages, frameworks, services, and architectures without silently changing behavior.
Use Rewrit when "the tests pass" is not enough. It checks the values, errors, HTTP responses, side effects, contracts, and policy decisions that matter during a migration.
reference runtime candidate runtime
| |
v v
adapter adapter
| |
+------ observations -----+
|
v
normalize, compare, report
|
v
CI-friendly exit code
Why Rewrit?
Large rewrites usually fail in the gaps between test suites:
- a Laravel endpoint becomes an Encore service,
- a Django path moves to Rust,
- a PHP monolith function is extracted to Node,
- an HTTP API keeps the same shape but changes internals,
- a "harmless" refactor changes an error, header, decimal, queue message, or database side effect.
Rewrit gives those gaps names and evidence. Instead of flattening everything
into "test failed", it reports structured divergences such as
type_mismatch, schema_mismatch, missing_candidate_case,
side_effect_mismatch, timeout, and waiver_expired.
Current Status
Rewrit is pre-1.0. The MVP workspace is implemented and covered by tests, but the public protocol and SDK ergonomics may still change before a stable 1.0 release.
Today the repository includes:
- Rust CLI and engine,
- versioned NDJSON adapter protocol,
- JSON contract and report schemas,
- command and HTTP adapters,
- PHP, Node, Python, and Rust SDK surfaces,
- terminal, JSON, NDJSON, JUnit, SARIF, HTML, and Markdown report code,
- runnable migration examples.
Quickstart
Install Rust 1.80 or newer, then install the CLI from crates.io:
cargo install rewrit-cli
rewrit --help
Or build from source:
git clone https://github.com/leofmarciano/rewrit.git
cd rewrit
cargo build -p rewrit-cli --release
./target/release/rewrit --help
Run a passing Laravel-to-Encore shaped fixture:
cargo run -p rewrit-cli -- run --manifest examples/laravel-to-encore/rewrit.toml
Expected result:
Equivalent: 1
Blocking divergences: 0
Parity: 100.00%
Exit: 0
Run an intentionally failing command-to-command fixture:
cargo run -p rewrit-cli -- run --manifest examples/command-to-command/rewrit.toml
That fixture exits with 1 on purpose. It demonstrates how Rewrit reports a
candidate returning money as a number instead of a decimal string, plus a
missing candidate case.
Create a Project
Generate a runnable scaffold:
cargo build -p rewrit-cli
export PATH="$PWD/target/debug:$PATH"
rm -rf /tmp/rewrit-demo
mkdir /tmp/rewrit-demo
cd /tmp/rewrit-demo
rewrit init --template laravel-to-encore
rewrit run --mode mirror
rewrit capture --runtime legacy_laravel
rewrit verify --runtime encore_ts
rewrit audit
Available templates:
command-to-commandlaravel-to-encoredjango-to-rust
Core Ideas
Reference and Candidate
The reference is the implementation treated as the source of truth. The candidate is the implementation being validated.
Cases
A case is one stable behavior ID, such as
billing.invoice.create.success or auth.login.invalid_password.
Contracts
Contracts declare what must stay equivalent: inputs, outputs, status codes, schemas, headers, expected errors, side effects, tolerances, and policies.
Observations
Adapters translate framework-specific execution into canonical Rewrit observations. The Rust core does not need to understand Laravel, Django, Vitest, Pest, Pytest, Cargo test, or any other test runner.
Policies, Normalizers, and Waivers
Policies define what counts as a real difference. Normalizers remove accepted noise such as generated IDs or timestamps. Waivers are explicit, owned, and expiring exceptions that remain visible in reports.
Example Manifest
Rewrit projects are configured with rewrit.toml.
[project]
name = "billing-migration"
reference = "legacy_laravel"
candidate = "encore_ts"
contracts_dir = "contracts"
baselines_dir = ".rewrit/baselines"
reports_dir = ".rewrit/reports"
[runtimes.legacy_laravel]
adapter = "command"
cwd = "../legacy"
command = ["vendor/bin/pest", "--rewrit"]
timeout_ms = 30000
[runtimes.encore_ts]
adapter = "command"
cwd = "../candidate"
command = ["npm", "run", "test:rewrit"]
timeout_ms = 30000
[[reports]]
kind = "terminal"
[[reports]]
kind = "json"
path = ".rewrit/reports/latest.json"
Adapters emit versioned NDJSON events:
{"schema_version":"rewrit.event.v1","kind":"observation","case_id":"billing.invoice.create.success","runtime_id":"reference","status":"passed","value":{"kind":"json","value":{"status":"open","amount":"199.90"}}}
CLI
rewrit init --template laravel-to-encore
rewrit doctor
rewrit discover
rewrit run --mode mirror
rewrit capture --runtime legacy_laravel
rewrit verify --runtime encore_ts
rewrit audit
rewrit explain billing.invoice.create.success
rewrit schema export --kind all --out-dir dist/schemas
rewrit report open
rewrit completions --shell zsh
Common modes:
run --mode mirror: run reference and candidate in the same execution.capture --runtime <reference>: store a baseline reference observation.verify --runtime <candidate>: compare a candidate against the stored baseline or declared contracts.audit: check for missing required cases.
Examples
| Example | What it shows |
|---|---|
examples/command-to-command |
Generic command adapter and intentional divergences |
examples/http-to-http |
HTTP contract comparison and schema/type mismatches |
examples/laravel-to-encore |
PHP-shaped reference to Encore/Node-shaped candidate |
examples/django-to-rust |
Django/Python-shaped reference to Rust candidate |
examples/php-to-node-monolith |
Monolith function extraction with stable case IDs |
Documentation
Start with the documentation index.
Common entry points:
- New to Rewrit: Parity, contracts, and observations.
- Wiring a runner or framework: Adapter protocol v1 and the adapter guide for your stack.
- Using an existing SDK: PHP Pest, PHPUnit, Node Vitest, Node Jest, Node Encore, Python Pytest, Django, or Rust Cargo test.
- Planning a migration: Laravel to Encore, Laravel to Node, or Django to Rust.
- Reviewing design decisions: ADRs.
Repository Layout
crates/
rewrit-model canonical data model
rewrit-core normalization, comparison, policy, validation
rewrit-engine discovery, scheduling, runtime execution, baselines
rewrit-protocol adapter protocol types
rewrit-report terminal and machine-readable reports
rewrit-cli command-line interface
rewrit-adapter-* built-in adapter crates
sdks/
php/
node/
python/
rust/
docs/
concepts/
protocol/
adapters/
migrations/
adr/
examples/
command-to-command/
http-to-http/
laravel-to-encore/
django-to-rust/
php-to-node-monolith/
Development
Run the full Rust gate before opening a pull request:
cargo fmt --all -- --check
cargo clippy --workspace --all-targets --all-features -- -D warnings
cargo test --workspace --all-features
cargo doc --workspace --all-features --no-deps
Generate protocol and report schemas:
cargo run -p rewrit-cli -- schema export --kind all --out-dir dist/schemas
See CONTRIBUTING.md for contribution guidelines.
Security
Rewrit executes project code through adapters. Treat configured runtimes as trusted project code unless you explicitly run them inside a sandbox.
Supported guardrails include timeouts, stdout/stderr limits, secret redaction, temporary run directories, environment allowlists, and optional Docker or Podman sandboxing.
See SECURITY.md for details.
License
Rewrit is licensed under either of: