Skip to content

feat(rust): add cargo meta gen run engine and entity generator - #608

Merged
dmealing merged 13 commits into
mainfrom
fm/mo-rust-run-engine-gen
Nov 19, 2026
Merged

dmealing merged 13 commits into
mainfrom
fm/mo-rust-run-engine-gen

Conversation

@dmealing

@dmealing dmealing commented Nov 19, 2026 •

Copy link
Copy Markdown
Member

What Changed

The Rust port (ADR-0065, preview, unpublished) can now generate code: a run engine, a cargo meta gen command and the entity generator.

  • metaobjects-codegen: the run engine (ADR-0064) and the entity generator. The engine writes a generated file only when its bytes changed, saves .metaobjects/.gen-state/.hashes.json once per run and only when a record changed, and skips the whole run (nothing rendered, nothing written, not even the manifest) when the model hash tree, project config, engine build, generator identities and flags match the stored key and every recorded output is intact. entity writes one crate per metadata package: serde structs for entities and value objects, enums, Box only where a by-value cycle needs it, path dependencies for cross-package references; the output is already rustfmt-clean.
  • metaobjects-cli: the cargo-meta binary, so cargo's external-subcommand rule makes cargo meta gen work (cargo-meta gen too). publish = false everywhere; no new crates.io dependency (SHA-256 is hand-rolled).
  • metaobjects crate: visibility only. The loader's @objectRef resolver is now pub and #[doc(hidden)], marked generator-internal and not a stable API, so the generator binds a reference exactly as the loader does and the 1.3.0 public surface does not grow. No logic change.
  • scripts/ci-local.sh runs a gen_bench step in the rust lane (record only). CHANGELOG entry under [Unreleased]. The pipeline's document step also touched README.md, docs/CONFORMANCE.md, docs/features/cli.md, the skip-corpus README and ADR-0065 to describe what this ships; PR 607 touches some of the same files, so expect a rebase conflict where they meet (keep both sides).

Where Rust deliberately differs from the other ports

Manifest and skip behaviour:

  • No three-way merge. A generated file edited by hand, or an existing file nobody recorded, is refused: the run finishes the other files and exits 1. Python behaves the same way. As a result the skip corpus case hand-edit-kept does not apply to Rust, and generated-file-edited means "the run is attempted and the file is refused".
  • The manifest file and its keys are shared and unchanged (.hashes.json, project-relative / paths, SHA-256 of the UTF-8 content, 2-space JSON with a trailing newline). Rust does require the output directory to be a sub-directory of the project (the project root is rejected), so every key is project-relative and the stale-file sweep can never touch another port's records.
  • The stored skip key is .fingerprint-rust.json (uncommitted, versioned by its own format field). It deliberately drops the TypeScript fingerprint's extras and untracked arrays: nothing in this port populates or reads them (generator extra inputs are folded straight into the key, and every output is inside the project). The file is internal to this port, so the shape difference is invisible to the other ports.
  • Engine identity is a build-time hash of the metaobjects-codegen and metaobjects sources, so a skipped run pays nothing for it.
  • Named runs (cargo meta gen Order) never skip, never store a key and sweep nothing. They emit every file of each crate that contains a selected object plus the transitive closure of the crates those depend on (unchanged bytes are never rewritten), so the result compiles. An entity name that matches two objects is an error; ::Name selects a package-less one.
  • Skip corpus: every case Rust can meet runs against the engine. Not applicable, each with a stated reason: template-edited, template-added (no templates), owned-generator-edited (no eject), hand-edit-kept (no merge), ci (no verify). engine-changed is exercised by altering the stored key, since the engine identity is a compile-time hash.
  • Not in this PR: verify, eject, templates, client or HTTP generators, relationships/projections/reports in the output.

Per-stage timing (gen_bench, 20x fitness project, 680 files in 20 crates, release build, median of 9; ms; record only, no thresholds)

stage first run no change (skips) one file changed forced, nothing changed
load 27.306 28.813 27.392 26.654
plan 12.819 13.196 13.290 13.300
skip 0.000 2.189 0.000 0.000
render 3.608 0.000 3.410 3.404
write 4.052 0.000 2.728 2.542
total 46.523 44.688 46.193 45.949

The skip saves render and write only; load and plan (hashing the model) are about 40 of the roughly 45 ms. There is no earlier generation benchmark to compare against, so the comparison is the first run against the no-change run. An unchanged project rewrites no file and no manifest.

Testing

cargo fmt --check, cargo clippy --locked --all-targets -- -D warnings and cargo test --locked pass in server/rust; all 526 shared metamodel fixtures still pass. New: a golden test of a two-package model's text; a test that generates that model and the fitness model and runs cargo check, rustfmt --check and a serde round trip on the crates (including a named first run and a three-deep dependency chain); run-engine tests (unchanged bytes not rewritten, one manifest save, skip, refusal, orphan sweep, named runs); the shared skip-conformance corpus; the loader-agreement test for @objectRef; and CLI tests.

Validation

no-mistakes took two runs on this branch. Run 1 passed its own steps (six review rounds; every finding fixed: reference resolution now reuses the loader's resolver, named runs emit dependency crates, ::Name selects a package-less object, a project-root output directory is rejected, the dead fingerprint fields were removed, help text corrected). The forge CI then failed on the rust lane: the generated-crate cargo check tests passed --offline, and a fresh runner has no cached serde (a dependency of the generated crates only), so they could not download it. Run 2 dropped --offline (proved with an empty cargo cache) and also fixed what its review and document steps found: two packages whose names map to one crate name could silently merge through a shared enum (now ERR_NAME_COLLISION, with edge tests), and a stale "does not exist" message in the cross-port bench script. All forge checks pass on the final head. Two document findings in run 1 were ignored on purpose (fit-assessment wording; ADR-0065's status, which PR 607 has since changed).

Pipeline

Updates from git push no-mistakes

✅ **Intent** - passed

✅ No issues found.

✅ **Rebase** - passed

✅ No issues found.

🔧 **Review** - 1 issue found → auto-fixed ✅
  • ⚠️ server/rust/metaobjects-codegen/src/entity.rs:417 - Two packages can silently merge into one crate when a shared enum is involved. Plan::build checks c.package != pkg (ERR_NAME_COLLISION) only for struct-bearing crates (lines 268-283). The shared-enum loop (lines 410-423) never compares packages. Trace: package a::b declares an entity, and package a_b declares an abstract shared field.enum that a field consumes. Both map to crate a_b via crate_name. The enum is pushed into the a_b crate planned for a::b, so output has no error, one crate, and a Cargo.toml/lib.rs describing a::b. The same hole exists between two packages that each own only shared enums, because c.package stays whichever came first. Fix: in the shared-enum loop, when the crate entry already has a non-empty package (or any struct/shared enum) and it differs from pkg, return the same ERR_NAME_COLLISION error. Siblings: none; this loop and the struct loop are the only two places a crate entry is created.

🔧 Fix applied.
✅ Re-checked - no issues remain.

✅ **Test** - passed

✅ No issues found.

  • Live validation: ✅ go - 3 of 3 scenarios driven live against the product
Scenario Result Live Evidence
Fresh full run on shop + shop::billing project writes crates that compile: cargo meta gen exits 0 with 11 files, and cargo check of generated shop_billing passes ✅ pass live rust-gen-live-transcript.txt S1; cargo meta gen --timings then cargo check --offline in generated/shop_billing (exit 0)
Rerun with unchanged inputs skips without touching files; editing one entity rewrites only that file and output still compiles ✅ pass live rust-gen-live-transcript.txt S2: 'gen: skipped, nothing changed', identical mtimes; edit run '1 updated, 10 unchanged'; recheck exit 0
Named run and ambiguity guard: cargo meta gen Invoice emits the cross-crate closure and compiles; a bare name shared by a package-less and a packaged object errors, while ::Name and app::Foo each se… ✅ pass live rust-gen-live-transcript.txt S3: Invoice run 11 files, cargo check exit 0; bare Foo -> ERR_NAME_COLLISION exit 1; ::Foo -> model crate only; app::Foo -> app crate only; both selected crates cargo chec…
  • scripts/ci-local.sh --affected --strict-toolchains
  • Baseline (not re-run): scripts/ci-local.sh --affected --strict-toolchains passed at e760a185d
  • cargo build -p metaobjects-cli --offline (up to date)
  • S1: cargo meta gen --timings on fresh project (metaobjects/meta.shop.json + meta.billing.json, package shop and shop::billing) -> exit 0, 11 files created
  • S1: cargo check --offline in generated/shop_billing -> exit 0 (shop path dep compiled)
  • S2: second cargo meta gen --timings unchanged -> gen: skipped, nothing changed exit 0, stat mtimes of all generated files identical before/after
  • S2: edit Category (added required field slug) -> 1 updated, 10 unchanged, only generated/shop/src/category.rs contains slug; cargo check shop_billing exit 0
  • S3: cargo meta gen Invoice on fresh project -> exit 0, 11 files (shop_billing + shop closure); cargo check shop_billing exit 0
  • S3: package-less Foo + app::Foo: cargo meta gen Foo -> exit 1 ERR_NAME_COLLISION with hint naming ::Name and pkg::Name; cargo meta gen ::Foo -> package-less model crate only; cargo meta gen app::Foo -> app crate only; cargo check model and app both exit 0
🔧 **Document** - 1 issue found → auto-fixed ✅
  • ℹ️ scripts/bench-load/stages.py:353 - Rust.prepare raises NotAvailable with the text "not available yet: cargo meta gen does not exist". The command now exists, so the text is stale. It is a runtime string, not a doc, so I left it. The fix is a one-line wording change to name the missing cargo-meta binary instead.

🔧 Fix applied.
✅ Re-checked - no issues remain.

✅ **Lint** - passed

✅ No issues found.

✅ **Push** - passed

✅ No issues found.

…ator

The Rust port can now generate code. Two new workspace crates:

- metaobjects-codegen: the run engine (ADR-0064) and the `entity` generator.
  The engine writes a generated file only when its bytes changed, saves the
  hash manifest once per run and only when a record changed, and skips the
  whole run when the model hash tree, project config, engine build,
  generator identities and flags match the stored key and every recorded
  output is intact. `entity` writes one crate per metadata package: serde
  structs for entities and value objects, enums, Box only where a by-value
  cycle needs it, path dependencies for cross-package references, already
  rustfmt-clean.
- metaobjects-cli: the `cargo-meta` binary, so `cargo meta gen` works.

Rust differs from the other ports on purpose: no three-way merge (a
hand-edited generated file is refused), outputs must be inside the project,
and there is no verify, eject or template tier yet.

Tests: golden text, `cargo check` + rustfmt + serde round trip on generated
crates, run-engine, the shared skip-conformance corpus, CLI. gen_bench
records time per stage with no thresholds; the rust lane prints it.
The compile tests passed --offline, so they only worked on a machine whose cargo
cache already held serde (a dependency of the generated crates alone). On a fresh
CI runner they failed to download it. The copied lockfile still pins serde, so
dropping --offline changes no version.
@dmealing
dmealing force-pushed the fm/mo-rust-run-engine-gen branch from b2908bc to b866cdc Compare November 19, 2026 05:54
@dmealing
dmealing merged commit bc21d01 into main Nov 19, 2026
10 checks passed
@dmealing
dmealing deleted the fm/mo-rust-run-engine-gen branch November 19, 2026 06:02
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant