Skip to content

feat(codegen-ts): run business rules in the generated service layer - #642

Merged
dmealing merged 4 commits into
mainfrom
fm/mo-130-lane-ts-2-b7
Nov 22, 2026
Merged

dmealing merged 4 commits into
mainfrom
fm/mo-130-lane-ts-2-b7

Conversation

@dmealing

@dmealing dmealing commented Nov 22, 2026 •

Copy link
Copy Markdown
Member

What Changed

  • The service generator (codegen-ts) now emits a command for each transition that derives one (rules.lifecycle.commands) and a shell for each declared command and query. Each shell runs ADR-0066 §6's checks in one transaction, in order: row (404), permission (403 forbidden), state (409 invalid_transition), hand-written body, guarded update (409 invalid_transition or transition_condition_failed), then the event hook. A changed lifecycle field on the generic update answers 409 state_managed. rules.lifecycle.create and rules.permissions.names: require take effect, and generation refuses a command with @transitions that addresses no row by key, a duplicate method and path, and a require point with no @permission. Policy, bodies, hooks and events reach the service through generated interfaces passed to create<Pkg>Service(deps), and any of them may refuse, which rolls the write back.
  • runtime-ts/service adds rules.ts, which holds the runtime helpers the shells call (checkPermission, checkState, makeTransition, checkStateManaged, checkCreateState, emitWrite, handlerContext, refuseBusiness). OpOptions, ServiceDeps, the service interface and the factory take an app-defined actor type A that defaults to unknown, so existing callers compile unchanged. The Fastify transport resolves the actor through an optional getActor(request).
  • Adds the shared API contract corpus fixtures/api-contract-conformance/rules/ (generated lane, TypeScript only) with three settings variants covering 403, the three 409 codes, a declared error, a derived and a declared command, a query, a public operation, and rollback when a hook or event hook refuses. The CLI rules-inert test gains a SERVICE_LAYER category so service generators may change files for rules vocabulary. The install-size budget is raised by ruling, recorded in scripts/install-size-budget.json. Docs and ADR-0066/0067 are updated to match, and the CHANGELOG records the change.
Validation

Risk Assessment

✅ Low: The change is bounded to the opt-in service generators, a new runtime module and a new shared corpus. The check order and the guarded update match ADR-0066 §6, every intent requirement is present, and I found no wrong-result path.

Testing

Baseline ci-local --affected passed at the target commit and was not re-run. Drove the generated TypeScript service live: the existing rules api-contract test boots the emitted service and transport over HTTP against real Postgres testcontainers, and all 34 runs (17 scenarios, 2 layouts) passed, covering lifecycle create under initial and any, state_managed, 403 forbidden, invalid_transition, transition_condition_failed, declared errors, derived and declared commands, the public query, and rollback on hook and event refusal. Also ran the worktree's codegen on disposable bad models: generation refuses a require-names point and a command with no row addressed by key, and generates cleanly for the control. Not driven live: the two-routes-on-one-method-and-path refusal. Overall result passes.

  • Live validation: ✅ go - 3 of 4 scenarios driven live against the product
Scenario Result Live Evidence
Lifecycle: a create outside the initial state is refused under rules.lifecycle.create initial (409 state_managed), and allowed under any (201 in the named state); a PATCH moving the lifecycle field is… ✅ pass live rules-api-contract.junit.xml: default settings create-starts-in-initial-state, state-managed-update; settings/create-any create-in-any-state (both layouts pass)
Commands and answers: 403 forbidden without the permission (derived, declared, and public-command-needs-no-actor), 409 invalid_transition, 409 transition_condition_failed, declared error note_required… ✅ pass live rules-api-contract.log (34 pass); rules-api-contract.junit.xml includes declared-permission-is-asked, derived-command-forbidden, public-command-needs-no-actor, invalid-transition, transition-condition…
Refusals roll back: a hook refusal (title_refused) and an event-hook refusal (audit_unavailable) leave the row and audit table unchanged; adversarial generation refusals fire for a require-names point… ✅ pass live rules-api-contract.junit.xml: hook-refusal-rolls-back, event-refusal-rolls-back, declared-error-rolls-back (default settings, both layouts); gen-refusals.log: control GENERATED ok, require REFUSED nam…
Two routes on one method and path (including derived CRUD routes) are refused at generation time ⏸️ untested no Not driven in this turn. Tried to reason about a duplicate method-and-path model variant; the scenario list was capped at three and the time went to the three scenarios above. Covered only by codegen-…
Evidence: Rules api-contract run log (34 pass)
bun test v1.3.14 (0d9b296a)

 34 pass
 0 fail
Ran 34 tests across 1 file. [22.83s]
Evidence: Rules api-contract junit report (scenario names, both layouts)
<?xml version="1.0" encoding="UTF-8"?>
<testsuites name="bun test" tests="34" assertions="0" failures="0" skipped="0" time="22.926207391">
  <testsuite name="test/api-contract-rules.test.ts" file="test/api-contract-rules.test.ts" tests="34" assertions="0" failures="0" skipped="0" time="0" hostname="pop-os-desktop">
    <testsuite name="api contract rules — GENERATED service lane [flat]" file="test/api-contract-rules.test.ts" line="28" tests="17" assertions="0" failures="0" skipped="0" time="19.967" hostname="pop-os-desktop">
      <testcase name="default settings: claimed-transition-has-no-command" classname="api contract rules — GENERATED service lane [flat]" time="1.70284" file="test/api-contract-rules.test.ts" line="32" assertions="0" />
      <testcase name="default settings: create-starts-in-initial-state" classname="api contract rules — GENERATED service lane [flat]" time="1.149994" file="test/api-contract-rules.test.ts" line="32" assertions="0" />
      <testcase name="default settings: declared-command-transitions" classname="api contract rules — GENERATED service lane [flat]" time="1.145077" file="test/api-contract-rules.test.ts" line="32" assertions="0" />
      <testcase name="default settings: declared-error-rolls-back" classname="api contract rules — GENERATED service lane [flat]" time="1.132143" file="test/api-contract-rules.test.ts" line="32" assertions="0" />
      <testcase name="default settings: declared-permission-is-asked" classname="api contract rules — GENERATED service lane [flat]" time="1.145074" file="test/api-contract-rules.test.ts" line="32" assertions="0" />
      <testcase name="default settings: derived-command-forbidden" classname="api contract rules — GENERATED service lane [flat]" time="1.120268" file="test/api-contract-rules.test.ts" line="32" assertions="0" />
      <testcase name="default settings: derived-command-moves-the-row" classname="api contract rules — GENERATED service lane [flat]" time="1.139562" file="test/api-contract-rules.test.ts" line="32" assertions="0" />
      <testcase name="default settings: event-refusal-rolls-back" classname="api contract rules — GENERATED service lane [flat]" time="1.119772" file="test/api-contract-rules.test.ts" line="32" assertions="0" />
      <testcase name="default settings: hook-refusal-rolls-back" classname="api contract rules — GENERATED service lane [flat]" time="1.147589" file="test/api-contract-rules.test.ts" line="32" assertions="0" />
      <testcase name="default settings: invalid-transition" classname="api contract rules — GENERATED service lane [flat]" time="1.167427" file="test/api-contract-rules.test.ts" line="32" assertions="0" />
      <testcase name="default settings: public-command-needs-no-actor" classname="api contract rules — GENERATED service lane [flat]" time="1.128608" file="test/api-contract-rules.test.ts" line="32" assertions="0" />
      <testcase name="default settings: public-query" classname="api contract rules — GENERATED service lane [flat]" time="1.158586" file="test/api-contract-rules.test.ts" line="32" assertions="0" />
      <testcase name="default settings: state-managed-update" classname="api contract rules — GENERATED service lane [flat]" time="1.139199" file="test/api-contract-rules.test.ts" line="32" assertions="0" />
      <testcase name="default settings: transition-condition-failed" classname="api contract rules — GENERATED service lane [flat]" time="1.116187" file="test/api-contract-rules.test.ts" line="32" assertions="0" />
      <testcase name="settings/commands-all: claimed-transition-has-a-command" classname="api contract rules — GENERATED service lane [flat]" time="1.136591" file="test/api-contract-rules.test.ts" line="32" assertions="0" />
      <testcase name="settings/commands-none: no-derived-commands" classname="api contract rules — GENERATED service lane [flat]" time="1.124465" file="test/api-contract-rules.test.ts" line="32" assertions="0" />
      <testcase name="settings/create-any: create-in-any-state" classname="api contract rules — GENERATED service lane [flat]" time="1.201657" file="test/api-contract-rules.test.ts" line="32" assertions="0" />
    </testsuite>
    <testsuite name="api contract rules — GENERATED service lane [package]" file="test/api-contract-rules.test.ts" line="28" tests="17" assertions="0" failures="0" skipped="0" time="1.7" hostname="pop-os-desktop">
      <testcase name="default settings: claimed-transition-has-no-command" classname="api contract rules — GENERATED service lane [package]" time="0.089743" file="test/api-contract-rules.test.ts" line="32" assertions="0" />
      <testcase name="default settings: create-starts-in-initial-state" classname="api contract rules — GENERATED service lane [package]" time="0.096833" file="test/api-contract-rules.test.ts" line="32" assertions="0" />
      <testcase name="default settings: declared-command-transitions" classname="api contract rules — GENERATED service lane [package]" time="0.096519" file="test/api-contract-rules.test.ts" line="32" assertions="0" />
      <testcase name="default settings: declared-error-rolls-back" classname="api contract rules — GENERATED service lane [package]" time="0.094064" file="test/api-contract-rules.test.ts" line="32" assertions="0" />
      <testcase name="default settings: declared-permission-is-asked" classname="api contract rules — GENERATED service lane [package]" time="0.090978" file="test/api-contract-rules.test.ts" line="32" assertions="0" />
      <testcase name="default settings: derived-command-forbidden" classname="api contract rules — GENERATED service lane [package]" time="0.10236" file="test/api-contract-rules.test.ts" line="32" assertions="0" />
      <testcase name="default settings: derived-command-moves-the-row" classname="api contract rules — GENERATED service lane [package]" time="0.105324" file="test/api-contract-rules.test.ts" line="32" assertions="0" />
      <testcase name="default settings: event-refusal-rolls-back" classname="api contract rules — GENERATED service lane [package]" time="0.114009" file="test/api-contract-rules.test.ts" line="32" assertions="0" />
      <testcase name="default settings: hook-refusal-rolls-back" classname="api contract rules — GENERATED service lane [package]" time="0.121545" file="test/api-contract-rules.test.ts" line="32" assertions="0" />
      <testcase name="default settings: invalid-transition" classname="api contract rules — GENERATED service lane [package]" time="0.108" file="test/api-contract-rules.test.ts" line="32" assertions="0" />
      <testcase name="default settings: public-command-needs-no-actor" classname="api contract rules — GENERATED service lane [package]" time="0.099881" file="test/api-contract-rules.test.ts" line="32" assertions="0" />
      <testcase name="default settings: public-query" classname="api contract rules — GENERATED service lane [package]" time="0.097029" file="test/api-contract-rules.test.ts" line="32" assertions="0" />
      <testcase name="default settings: state-managed-update" classname="api contract rules — GENERATED service lane [package]" time="0.088891" file="test/api-contract-rules.test.ts" line="32" assertions="0" />
      <testcase name="default settings: transition-condition-failed" classname="api contract rules — GENERATED service lane [package]" time="0.096139" file="test/api-contract-rules.test.ts" line="32" assertions="0" />
      <testcase name="settings/commands-all: claimed-transition-has-a-command" classname="api contract rules — GENERATED service lane [package]" time="0.090057" file="test/api-contract-rules.test.ts" line="32" assertions="0" />
      <testcase name="settings/commands-none: no-derived-commands" classname="api contract rules — GENERATED service lane [package]" time="0.108516" file="test/api-contract-rules.test.ts" line="32" assertions="0" />
      <testcase name="settings/create-any: create-in-any-state" classname="api contract rules — GENERATED service lane [package]" time="0.108602" file="test/api-contract-rules.test.ts" line="32" assertions="0" />
    </testsuite>
  </testsuite>
</testsuites>
Evidence: Generation refusal and control transcript
== control
GENERATED ok warnings=0
== require
REFUSED: [service] service: transition PurchaseRequest.status.submit declares no @permission and is not @public, and rules.permissions.names is "require"; declare its permission (it would derive "acme::purchasing.PurchaseRequest.submit")
== nokey
REFUSED: <scratch>/nokey/meta.json: metadata did not load cleanly: ERR_BINDING_PATH_MISMATCH: binding.rest on "Purchasing.decide" @path "/purchase-requests/{id}/decision" has placeholders ["id"]; "Purchasing.decide" addresses no row by key, so its path takes none.
== nokey2
REFUSED: [service] service: command decide lists @transitions but addresses no row by key, so it has no row to move

Gate and scope

  • Plan row L-TS-5 (service generator, part 2) of the 1.3.0 build plan. It also carries fff31a820, the install-size budget raise by Firstmate's ruling, ridden on this branch deliberately.
  • Gate runs: 1. Review, test, document and lint ran once. The Document step raised one ask-user finding (two agent-context audit-skill lines still said no generator emits from the rules vocabulary). Firstmate ruled fix, and the fix round regenerated the agent-context conformance goldens and the agent-context-shipped/ tree with their generators (no hand edits); the agent-context tests pass (130/130).
  • Not in scope (later rows): the Hono transport, an actor-resolver package, ERR_STORE_NO_INTERACTIVE_TX, sync SQLite and D1 (L-TS-6); the init flag, the verify advisory for a lifecycle with no service, gen --list (L-TS-7); moving reserved-name refusals to the generator (T6b). The other ports' services and their lanes of the new rules/ corpus are each port's own 1.3.0 work.
  • Overlap: triage notes this branch touches the same count and CHANGELOG text as open PRs feat(csharp): run the model authoring lints in dotnet meta verify #635, feat(metadata): move reporting and requirements into vocabulary packs #632 and docs(examples): add head-to-head demo of MetaObjects vs a DIY stack #516; whichever merges second resolves the counts.

Pipeline

Updates from git push no-mistakes

✅ **Intent** - passed

✅ No issues found.

✅ **Rebase** - passed

✅ No issues found.

⚠️ **Review** - 1 info
  • ℹ️ server/typescript/packages/codegen-ts/src/templates/service-file.ts:450 - A declared command or query with an @inputRef validates its input before the permission point. ADR-0066 §6 lists load row, permission, state, body, and does not mention input validation. Validation first is needed because the policy receives the validated input. The cost is that a caller the policy would refuse gets 400 validation detail before 403. The generated doc comment states the order, so this is a recorded choice, not a defect. Consider one line in ADR-0066 §6 saying validation precedes the permission point on operations that take an input.
✅ **Test** - passed

✅ No issues found.

  • Live validation: ✅ go - 3 of 4 scenarios driven live against the product
Scenario Result Live Evidence
Lifecycle: a create outside the initial state is refused under rules.lifecycle.create initial (409 state_managed), and allowed under any (201 in the named state); a PATCH moving the lifecycle field is… ✅ pass live rules-api-contract.junit.xml: default settings create-starts-in-initial-state, state-managed-update; settings/create-any create-in-any-state (both layouts pass)
Commands and answers: 403 forbidden without the permission (derived, declared, and public-command-needs-no-actor), 409 invalid_transition, 409 transition_condition_failed, declared error note_required… ✅ pass live rules-api-contract.log (34 pass); rules-api-contract.junit.xml includes declared-permission-is-asked, derived-command-forbidden, public-command-needs-no-actor, invalid-transition, transition-condition…
Refusals roll back: a hook refusal (title_refused) and an event-hook refusal (audit_unavailable) leave the row and audit table unchanged; adversarial generation refusals fire for a require-names point… ✅ pass live rules-api-contract.junit.xml: hook-refusal-rolls-back, event-refusal-rolls-back, declared-error-rolls-back (default settings, both layouts); gen-refusals.log: control GENERATED ok, require REFUSED nam…
Two routes on one method and path (including derived CRUD routes) are refused at generation time ⏸️ untested no Not driven in this turn. Tried to reason about a duplicate method-and-path model variant; the scenario list was capped at three and the time went to the three scenarios above. Covered only by codegen-…
  • scripts/ci-local.sh --affected --strict-toolchains
  • Baseline (configured): scripts/ci-local.sh --affected --strict-toolchains, passed at target commit 5d18b7ef7e (not re-run; no source changed in this turn)
  • cd server/typescript/packages/integration-tests && bun test test/api-contract-rules.test.ts (34 pass, 0 fail; junit list shows 17 scenarios x 2 output layouts incl. settings/create-any, settings/commands-all, settings/commands-none)
  • Scenario 1 and 2 evidence = the same run: create-starts-in-initial-state, create-in-any-state, state-managed-update, declared-permission-is-asked, derived-command-forbidden, public-command-needs-no-actor, invalid-transition, transition-condition-failed, declared-command-transitions, derived-command-moves-the-row, claimed-transition-has-no-command, claimed-transition-has-a-command, no-derived-commands, public-query
  • Scenario 3 evidence = declared-error-rolls-back, hook-refusal-rolls-back, event-refusal-rolls-back in the same run
  • Adversarial generation refusals: disposable driver under the scratch directory calling the worktree's codegen-ts runGen with serviceFile + serviceRoutesFile on 4 model variants: control (generates ok), rules.permissions.names=require (refused, names derived acme::purchasing.PurchaseRequest.submit), decide input without key borrow with path lacking placeholder (refused: addresses no row by key), decide with key-less input and placeholder path (refused by loader ERR_BINDING_PATH_MISMATCH). Transcript in gen-refusals.log
  • Cleanup: removed the scratch driver dir and temp pid file; git status clean in worktree; no background jobs of mine running (a bun test vocabulary-checks run belongs to another session, untouched); the four postgres and Ryuk containers started about 4 minutes ago are not verifiably mine and were not stopped
🔧 **Document** - 1 issue found → auto-fixed ✅
  • ⚠️ agent-context/skills/metaobjects-audit/references/calibration.md:37 - agent-context/skills/metaobjects-audit/references/calibration.md:37 and capability-checklist.md:369 still say the declared-API and lifecycle vocabulary has 'no generator emits from them yet'. False for TypeScript after this change. Not edited: the run intent says agent-context/*.md must not be touched. Fix is a one-line reword in each file, once Doug allows it.

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

✅ **Lint** - passed

✅ No issues found.

✅ **Push** - passed

✅ No issues found.

…its history

The install of @metaobjectsdev/cli measures 51.5 MB against a 51.0 MB
budget: feature growth since #622, measured late because hosted CI does
not run ts-slow. Ruling (Firstmate, 2026-11-22): raise to the measurement
plus 0.2 MB and keep the declaration maps.

check-install-size.mjs accepts a raise only when the newest history entry
records it (from main's budget, to the new one, with a reason and a
ruling), and it falls back to origin/main when there is no local main,
so the never-rises rule no longer skips such a checkout.
…rated service (L-TS-5)

The service generator now writes a command per transition that derives one and a shell per
declared command and query, each running ADR-0066 section 6's checks in one transaction: the
row (404), the permission point (403 forbidden), the state (409 invalid_transition), the
hand-written body, the guarded update (409 invalid_transition or transition_condition_failed)
and the event hook. The generic update refuses a changed lifecycle field (409 state_managed).

What an app writes attaches through generated interfaces passed to create<Pkg>Service(deps):
the policy, the operation bodies, optional hooks on the derived writes and the write-event hook.
Any of them may refuse, which rolls the write back. The actor is an app-defined type parameter,
passed as opts.actor and resolved by the Fastify transport's getActor(request).

rules.lifecycle.create, rules.lifecycle.commands and rules.permissions.names: require now act
on the generated side. New shared API tests (fixtures/api-contract-conformance/rules/, 17
scenarios with three settings variants) run on the TypeScript service lane in both layouts, and
the service compile gate builds the rules model in every dialect. ADR-0066's "no effect yet" is
removed, and ADR-0067, the business-rules and API-contract docs and the CHANGELOG say what the
service now does.
@dmealing
dmealing merged commit 9ca2653 into main Nov 22, 2026
10 checks passed
@dmealing
dmealing deleted the fm/mo-130-lane-ts-2-b7 branch November 22, 2026 10:33
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