Skip to content

feat(rust): add cargo meta command line and client and commands generators - #611

Merged
dmealing merged 6 commits into
mainfrom
fm/mo-rust-phase2
Nov 20, 2026
Merged

dmealing merged 6 commits into
mainfrom
fm/mo-rust-phase2

Conversation

@dmealing

@dmealing dmealing commented Nov 20, 2026 •

Copy link
Copy Markdown
Member

What Changed

  • Adds the cargo meta command (metaobjects-cli crate, cargo-meta binary, unpublished). init scaffolds .metaobjects/config.json, ignore rules and a first metadata file. gen removes generated files nothing emits any more (hand-edited ones are refused by name) and reports created, updated and removed files. verify --codegen renders in memory and exits 1 on drift. --db, --templates and --deps exit 2 and say where those checks live. eject <generator> writes an owned codegen/ crate that gen and verify then build and run. eject --undo removes only copies still identical to the shipped source.
  • The entity generator now also emits <Name>Create, <Name>Patch and validate(), with exact decimals and durations in a generated meta_support crate, and honors wire.nullMembers. New client generator: a typed reqwest client per served entity with a query type checked at compile time. New commands generator: clap command groups over that client, emitted as a library. commands is Rust-only, recorded in fixtures/generator-registry-conformance/registry.json and ADR-0065 Amendment 1.
  • Adds gates for the new output: validation-conformance, the decimal and duration wire vectors, writer conformance, the skip-corpus cases for Rust, a compile gate that builds and formats generated output and drives the client over HTTP against a local stub, and a scale gate. Documents the command in docs/ports/rust.md, docs/features/cli.md and CHANGELOG.md. Rust stays preview; metaobjects-cli and the codegen crate are publish = false.

Gate runs

This PR took 2 no-mistakes runs. The first was started on a clean pre-run check and aborted about five minutes in, at its first review round and before anything was pushed: the business-rules PR (#610) merged four minutes after the run had rebased, and it touches four of the same doc files, so that run could only have ended in a branch that did not merge. main was merged in, the four files resolved keeping both sides' entries, the first run's two review findings (rows 19 and 20 below) fixed with tests, and the second run validated the result.

The merge also needed two repairs the text merge did not flag: the business-rules PR's Rust test (rules_inert.rs) did not compile against this branch's generator context and now covers all three shipped generators, and two of its doc sentences said Rust has no verify.

Design forks decided in this PR

Each is recorded in ADR-0065 Amendment 1. The first three are the owner's rulings; the rest are calls the work forced.

# Fork Chosen Why, and what was rejected
1 Is commands shared vocabulary? Rust-only for now (owner ruling). The generator registry lists client and commands with rust as the only port. A hidden difference would be found by an adopter; a listed one is checked by every port's registry test.
2 Generator plugin contract None (owner ruling). eject writes a codegen/ crate the project owns, and gen / verify build and run it. Rust cannot load a generator from source at run time. A plugin ABI would freeze the engine API while the port is in preview.
3 Publication Stays preview and unpublished, 0.x (owner ruling).
4 Shape of the generated crates Per package: <pkg> (entity), <pkg>_client, <pkg>_cli. Shared code is generated too: meta_support, meta_client, meta_cli. Nothing imports MetaObjects. Rejected: one crate with Cargo features (a model change rebuilds the client; a model-only consumer carries reqwest and clap); a published support crate (nothing is published, and it would be a runtime import); support code copied into every model crate, the first slice's shape (two crates would each have their own Decimal).
5 Decimals and wire.decimals Always the portable string, from an exact text-backed Decimal. Rust has no standard decimal type, so there is no "native" form for the setting to select. wire.nullMembers is honored.
6 Dates, times, uuid, uri String, with validate() checking the form. Picking a date-time crate for every adopter is not the generator's call.
7 validator.regex A small matcher in the support crate; a pattern outside it is refused at generation. Repeated groups are matched iteratively, so a long valid value matches. When the step budget or the pattern-nesting limit trips, validate() reports a distinct violation ("pattern could not be evaluated within limits"), never the ordinary no-match message. The repository bans a regex dependency, and a run-time refusal would be found by a user, not a build. The first version answered "no match" when it gave up, which the review caught: a give-up must not read as a wrong verdict.
8 Cross-member rules On <Name>Create only. A patch does not carry the members the rules compare.
9 Hand-edited generated file Refused by name; the orphan sweep refuses to delete one too. No three-way merge, as in every port but TypeScript.
10 verify surface Bare verify is --codegen. --db, --templates, --deps exit 2 with where the check lives. No fmt yet. Corrects ADR-0065 Decision 6, which listed fmt.
11 What the client is derived from The cross-port REST contract, not a Rust server. Every port serves the same routes for the same model, so the client needs no Rust route tier to exist.
12 Filter and sort typing One method per @filterable field and operator; an enum of @sortable fields; raw as the escape hatch. A request the server would refuse does not compile.
13 Keys the client can address get / update / delete only for a single key of text or a whole number. A composite key has no /{id} in the contract.
14 commands output A library crate: a clap::Subcommand and an async run, no main. The binary's name, the API address and how an answer prints belong to the application.
15 Bad input in commands Body is validated before sending, and an undeclared member is refused client-side. The mistake is reported where it was made.
16 Compile gate dependencies A committed lockfile; the gate fails if cargo needed a package it does not name. A dependency of generated code never arrives unreviewed.
17 Scale gate Structural budgets: linear growth in files and bytes, a byte budget and a longest-file budget for one copy, and it must build. Times are printed with loose ceilings. The plan asked for a time budget. On a shared machine a time budget measures the machine, which is the same reason load_bench is record-only.
18 Cross-port benchmark row stages.py runs Rust with --generators entity by default. cargo meta gen now runs three generators; the row would otherwise stop being comparable with its own history.
19 Exit code when an owned codegen/ crate breaks 1. Anything the owned crate answers other than 0, 1 or 2 (a failed build, a panic: cargo and Rust both say 101) is a failed run. The documented codes are 0, 1 and 2, and a script reading 1 as "failure or drift" must not meet a fourth. Found in review.
20 Which owned-crate files are skip-key inputs Every file under codegen/, except its build directory and Cargo.lock. The generator list is the project's file and may pull in any module beside it; naming only the files eject writes left an edit elsewhere unseen. Found in review.

Not in this PR

Rust migrations and database checks (roadmap phase 6; they stay with the Node meta tool), the sqlx/axum service tier, the prompt pillar, fmt, clients for projections and reports, publishing and version numbers.

Validation

Risk Assessment

✅ Low: I reviewed only the two fix-round commits (matcher rewrite, exit-code mapping and owned-crate skip key) and found no defect, but I did not read the other ~70 changed files of this phase, so the pass is partial.

Testing

Ran three live scenarios against the built cargo-meta binary in disposable scratch projects: JSON and YAML model gen/verify with drift refusal; owned codegen/ crate skip-key coverage of an extra file beside main.rs; broken owned crate and usage exit codes. All pass with exit codes and CLI output captured to evidence files. Broad regression came from the configured test command scripts/ci-local.sh --affected --strict-toolchains with its passing outcome, not rerun here. Not driven: a generator panic (exit 101 mapping) and Rust migrations (out of scope). Overall result: pass.

  • Live validation: ✅ go - 3 of 3 scenarios driven live against the product
Scenario Result Live Evidence
JSON model: init, gen, verify in sync, then hand-edit detected as drift and gen refuses to overwrite (cargo meta, scratch project) ✅ pass live scenario1-json-gen-verify-drift.txt: verify exit 0 'in sync'; after appending to generated/shop/src/order.rs, verify exit 1 'drifted: generated/shop/src/order.rs' and gen exit 1 refusing overwrite
YAML model gen works, and an owned codegen/ crate skip key covers every file in codegen/ (incl. a file beside main.rs) except target; gen --list shows the Rust-only commands generator ✅ pass live scenario2-owned-codegen-skip-key-and-exit-codes.txt: YAML gen exit 0; owned gen skips when unchanged; after editing codegen/extra.rs gen prints 'did not skip: an input changed'; gen --list shows comma…
Adversarial: broken owned codegen crate exits 1 (not cargo's 101) for gen and verify --codegen; usage error exits 2 ✅ pass live scenario2-owned-codegen-skip-key-and-exit-codes.txt: 'exit=1 (cargo build failure maps to 1, not 101)'; cargo meta gen --bogus exit=2
Evidence: Scenario 1: JSON model init/gen/verify, drift detection and gen refusal (transcript)
$ cargo meta init --project .
wrote .metaobjects/config.json
wrote .metaobjects/.gitignore
wrote metaobjects/meta.app.yaml
next: declare your objects in metaobjects/ (JSON or YAML), then run `cargo meta gen`.
Commit .metaobjects/.gen-state/.hashes.json with the generated crates.
Schema migrations are the Node `meta` command's (`meta migrate`), for every server language.
exit=0
$ cargo meta gen --project .  (JSON model)
gen: 24 files (24 created, 0 updated, 0 unchanged) in 12.8 ms; did not skip: no stored key
changed crates: meta_cli, meta_client, meta_support, shop, shop_cli, shop_client (cargo check -p meta_cli -p meta_client -p meta_support -p shop -p shop_cli -p shop_client)
exit=0
$ cargo meta verify --project .
cargo meta verify: in sync (the model and every recorded file are unchanged since the last gen; pass --full to render and compare).
exit=0
$ echo "// hand edit" >> generated/shop/src/order.rs
$ cargo meta verify --project .
error: generated code is out of sync with metadata.
  drifted: generated/shop/src/order.rs
regenerate (cargo meta gen) and commit the result.
exit=1
$ cargo meta gen --project .
cargo meta gen failed:
refused to overwrite 1 file(s) that differ from what the last run wrote (edited by hand, or not recorded in .metaobjects/.gen-state/.hashes.json): generated/shop/src/order.rs. Restore them, or delete them to generate them afresh.
exit=1
Evidence: Scenario 2 and 3: owned codegen skip key, gen --list, broken crate exit 1, usage exit 2 (transcript)
$ cargo meta gen --project .  (owned codegen crate, after eject entity)
gen: skipped, nothing changed since the last run (2.0 ms)
exit=0
$ cargo meta gen --project .  (again, unchanged)
gen: skipped, nothing changed since the last run (1.8 ms)
$ printf "pub const NOTE: &str = \"edited\";\n" > codegen/extra.rs   (file beside main.rs, not a named input)
$ cargo meta gen --project .
gen: 24 files (0 created, 0 updated, 24 unchanged) in 2.2 ms; did not skip: an input changed
$ cargo meta gen --project .  (again, unchanged after the edit)
gen: skipped, nothing changed since the last run (1.8 ms)
$ cargo meta gen --list --project .
entity - model crates, one per metadata package: serde structs, enums, value objects, create and patch inputs, validate() (layer: model) [owned: identical]
client - client crates, one per package with served entities: a typed HTTP client and list query per entity (needs entity) (layer: client)
commands - command crates, one per package with served entities: clap commands over the client (needs entity and client; Rust only) (layer: client)
$ printf "fn broken( {" >> codegen/main.rs ; cargo meta gen --project .
exit=1  (cargo build failure maps to 1, not 101)
$ cargo meta gen --bogus
cargo meta: unknown flag --bogus for `cargo meta gen`. Valid flags: --force, --generators, --list, --out, --project, --strict, --timings (also accepted everywhere: --help)
exit=2

Pipeline

Updates from git push no-mistakes

✅ **Intent** - passed

✅ No issues found.

✅ **Rebase** - passed

✅ No issues found.

🔧 **Review** - 3 issues found → auto-fixed ✅
  • ⚠️ server/rust/metaobjects-codegen/src/emitted/pattern.rs:27 - The matcher fails closed on depth, which turns a legitimate long input into a wrong validation verdict without any error. DEPTH_LIMIT is 1000 and each iteration of a repeated group (rep_more, then seq, then node) uses several levels. A valid value of a few hundred characters against a pattern such as ^(\w+\s?)*$ or ^(a)*$ hits the limit, full_match returns false, and the generated validate() reports "'field' does not match required pattern". tests/pattern.rs long_inputs_do_not_overflow_the_stack pins !full_match(&#34;(a)*&#34;, 200_000 &#39;a&#39;), so the false negative is deliberate. A diagnosable result (a distinct violation message, or raising the limit with an explicit heap stack) or an iterative matcher would change behaviour, so the remedy needs an owner decision, not a mechanical fix. Sibling sites: src/emitted/pattern.rs:25 (STEP_BUDGET gives up the same way), src/entity.rs member_checks (emits check::pattern, which cannot tell "gave up" from "no match"), tests/pattern.rs (long_inputs_do_not_overflow_the_stack).
  • ℹ️ server/rust/metaobjects-codegen/tests/generator_registry.rs:24 - depth_an_owned_copy_names_nothing_outside_the_engine_surface proves the registry's engine modules are exported by grepping src/lib.rs for lines equal to pub mod &lt;name&gt;; (LIB_RS via include_str!). A commented-out or cfg-gated line would pass. This is a source-text proxy for "the crate exports this module". The behavioural proof already exists in the ejected-alone build in metaobjects-cli/tests/cli.rs. Replace the grep with something executable, such as a generated use metaobjects_codegen::&lt;module&gt;; probe, or drop the LIB_RS check and rely on the build test. The scan over r.source (the text eject copies into a project) reads a persisted, shipped artifact, so it is a legitimate text contract.
  • ℹ️ docs/ports/rust.md:186 - The command examples do not match the model they describe. app order create --json &#39;{&#34;title&#34;:&#34;A bike&#34;}&#39; --total 12.50 and OrderQuery::new().title_like(...).total_gte(&amp;&#34;10.00&#34;.parse()?) use the shop Order, which has no title field. In that model total is field.currency, so the generated flag is Option&lt;i64&gt; (minor units) and --total 12.50 is rejected by clap. The total_gte argument would be an i64, not &amp;Decimal. A reader copying the example gets a compile or usage error. Use a field and value the shop golden actually has (for example --total 1250, status or createdAt).

🔧 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
JSON model: init, gen, verify in sync, then hand-edit detected as drift and gen refuses to overwrite (cargo meta, scratch project) ✅ pass live scenario1-json-gen-verify-drift.txt: verify exit 0 'in sync'; after appending to generated/shop/src/order.rs, verify exit 1 'drifted: generated/shop/src/order.rs' and gen exit 1 refusing overwrite
YAML model gen works, and an owned codegen/ crate skip key covers every file in codegen/ (incl. a file beside main.rs) except target; gen --list shows the Rust-only commands generator ✅ pass live scenario2-owned-codegen-skip-key-and-exit-codes.txt: YAML gen exit 0; owned gen skips when unchanged; after editing codegen/extra.rs gen prints 'did not skip: an input changed'; gen --list shows comma…
Adversarial: broken owned codegen crate exits 1 (not cargo's 101) for gen and verify --codegen; usage error exits 2 ✅ pass live scenario2-owned-codegen-skip-key-and-exit-codes.txt: 'exit=1 (cargo build failure maps to 1, not 101)'; cargo meta gen --bogus exit=2
  • scripts/ci-local.sh --affected --strict-toolchains
  • Built cargo build -p metaobjects-cli --bin cargo-meta (already up to date in server/rust/target)
  • Put target/debug on PATH and ran cargo meta init|gen|verify via cargo's external-subcommand dispatch on a JSON model in a scratch project
  • Hand-edit of generated/shop/src/order.rs, then cargo meta verify (expect exit 1, drifted) and cargo meta gen (expect refusal, exit 1)
  • YAML model (metaobjects/meta.shop.yaml) cargo meta gen (exit 0)
  • cargo meta eject entity, then owned-crate cargo meta gen builds codegen/ and runs it (exit 0), repeat gen skips with stored key
  • Edit codegen/extra.rs (a file beside main.rs, not a named input) then cargo meta gen must not skip; repeat skips again
  • cargo meta gen --list shows commands generator as Rust only, and entity marked owned: identical
  • Broken codegen/main.rs: cargo meta gen and cargo meta verify --codegen exit 1 (not 101)
  • cargo meta gen --bogus exits 2 with valid-flag list
  • Transcripts kept locally (scenario1-json-gen-verify-drift.txt and scenario2-owned-codegen-skip-key-and-exit-codes.txt); the excerpts above are from them
  • Removed the scratch projects; no background processes started; worktree git status clean
⚠️ **Document** - 1 info
  • ℹ️ ADR-0065 Status header says "Amends nothing" but the same record carries Amendment 1. Read as "amends no other ADR" it is accurate; left unchanged because the wording is the author's and the reading is a judgment call.
✅ **Lint** - passed

✅ No issues found.

✅ **Push** - passed

✅ No issues found.

…mands generators

Phase 2 of the Rust port (preview, ADR-0065 Amendment 1): a Rust developer
can run the whole codegen loop on a JSON or YAML model.

Run engine and CLI
- gen removes a generated file nothing emits any more (an edited one is
  refused by name) and prints a change report with the crates to re-check.
- cargo meta init scaffolds the config, the ignore rules and a first
  metadata file, writing only what is missing.
- cargo meta verify --codegen renders in memory, compares with disk and
  exits 1 on drift; --db, --templates and --deps exit 2 with the reason.
- cargo meta eject writes a codegen/ crate the project owns; gen and verify
  then build and run it. --undo takes back copies still identical.
- Exit codes follow the shared contract: 0, 1, 2.

Generators
- entity: <Name>Create and <Name>Patch bodies, validate(), exact decimals
  and signed durations in a generated meta_support crate, wire.nullMembers.
- client (new): a typed reqwest client per served entity, derived from the
  REST contract, with typed filters and sorts.
- commands (new, Rust-only by decision): clap command groups over the
  client, as a library with no main.

Gates
- validation-conformance through the generated validate(), the decimal and
  duration wire vectors, the core writer corpus, both columns of the skip
  corpus, and the generator registry with the eject contract.
- A compile gate builds and format-checks every generator's output and
  drives the generated client and commands over HTTP, from a committed
  lockfile. A scale gate holds twenty copies of the fitness model to linear
  growth and a size budget.

The generator registry records client and commands with rust as their only
port. Schema migrations and database checks stay with the Node meta tool.
Nothing is published.
…egen crates

Behaviour-preserving cleanup from the pre-gate simplification pass: no generated
output changes (goldens, compile gate and scale gate unchanged).
# Conflicts:
#	README.md
#	docs/CONFORMANCE.md
#	docs/ports/rust.md
#	spec/decisions/README.md
… crate into the skip key

A project's owned codegen crate that fails to build, or a generator in it that
panics, made cargo meta gen and verify exit 101; the documented codes are 0, 1
and 2, so anything else the owned crate answers is now a failed run, 1.

The skip key named three parts of the owned crate. The generator list is the
project's file and may pull in any module beside it, so an edit there left gen
reporting a skip and verify answering in sync over stale output. Every file
under codegen/ is now an input, except its build directory and lockfile.

Also brings the merged business-rules test and docs in line with this branch:
rules_inert.rs covers all three shipped generators, and the two pages that said
Rust has no verify now say it has the codegen gate only.
@dmealing
dmealing merged commit fea3151 into main Nov 20, 2026
10 checks passed
@dmealing
dmealing deleted the fm/mo-rust-phase2 branch November 20, 2026 03:12
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