From 76aba04ac9ac0b03ca9f8533be8a8cb150f840c4 Mon Sep 17 00:00:00 2001 From: Doug Mealing Date: Wed, 25 Nov 2026 02:02:00 -0500 Subject: [PATCH 1/6] feat(packs): systems and delivery vocabulary packs, plus deployment.railway (ADR-0069) Three independent packs, TypeScript port only, each 0.1.0 preview and unpublished: - systems: system.internal / system.external, app.service / app.client / app.job, resource.database / cache / bucket / keyvalue / queue / volume. An app's @needs and @calls resolve at load (ERR_SYSTEMS_UNRESOLVED_REF), addressed as System.child or pkg::System.child; @settings and @shell are names only. - delivery (requires systems): environment.standing / ephemeral / local, build.dockerfile / command / static (one per app), runtime.process, the abstract deployment.base a platform pack specialises, value.literal and value.ref. A deployment's @app / @environment and a value.ref's @from resolve at load (ERR_DELIVERY_UNRESOLVED_REF); @secrets and @setting are names only, since the config pack is not required. - railway (requires delivery): deployment.railway, vocabulary only, so the worked Railway example can load. Its fixture holds the app side and an estate repository's added deployment and overlay, with placeholder names. Core: applyProviderDefinition and its definition types are exported from @metaobjectsdev/metadata, with an optional validator map, so a pack registers its JSON vocabulary the way the bundled packs do. pack-conformance accepts the error codes of the packs a pack requires. No vocabulary, expected-registry.json, metamodelVersion or core version change. --- bun.lock | 43 ++ ...tadata-apply-provider-definition-export.md | 8 + docs/features/extending-with-providers.md | 8 + packs/README.md | 8 +- packs/delivery/CHANGELOG.md | 11 + packs/delivery/README.md | 68 +++ .../fixtures/conformance/ERROR-CODES.json | 6 + .../expected.json | 103 ++++ .../input/meta.shop.json | 103 ++++ .../providers.json | 1 + .../expected-errors.json | 15 + .../input/meta.shop.json | 48 ++ .../providers.json | 1 + .../expected-errors.json | 15 + .../input/meta.shop.json | 27 ++ .../error-restart-not-allowed/providers.json | 1 + .../error-two-builds/expected-errors.json | 15 + .../error-two-builds/input/meta.shop.json | 32 ++ .../error-two-builds/providers.json | 1 + .../fixtures/expected-vocabulary.json | 430 +++++++++++++++++ packs/delivery/metamodel/delivery.json | 121 +++++ packs/delivery/pack.json | 10 + packs/delivery/typescript/package.json | 23 + packs/delivery/typescript/src/constants.ts | 38 ++ packs/delivery/typescript/src/index.ts | 86 ++++ packs/delivery/typescript/tsconfig.json | 7 + packs/railway/CHANGELOG.md | 10 + packs/railway/README.md | 32 ++ .../expected-errors.json | 15 + .../input/meta.shop.json | 48 ++ .../providers.json | 1 + .../expected-errors.json | 25 + .../input/meta.shop.json | 49 ++ .../error-deployment-refs/providers.json | 1 + .../expected-errors.json | 25 + .../input/meta.shop.json | 67 +++ .../error-value-ref-unresolved/providers.json | 1 + .../railway-worked-example/expected.json | 152 ++++++ .../input/meta.app-system.json | 54 +++ .../input/meta.estate-overrides.json | 13 + .../input/meta.estate.json | 17 + .../railway-worked-example/providers.json | 1 + .../railway/fixtures/expected-vocabulary.json | 91 ++++ packs/railway/metamodel/railway.json | 16 + packs/railway/pack.json | 10 + packs/railway/typescript/package.json | 27 ++ packs/railway/typescript/src/index.ts | 26 ++ packs/railway/typescript/tsconfig.json | 7 + packs/systems/CHANGELOG.md | 10 + packs/systems/README.md | 69 +++ .../fixtures/conformance/ERROR-CODES.json | 6 + .../expected-errors.json | 15 + .../input/meta.shop.json | 12 + .../error-app-outside-system/providers.json | 1 + .../expected-errors.json | 15 + .../input/meta.shop.json | 27 ++ .../error-calls-wrong-kind/providers.json | 1 + .../expected-errors.json | 15 + .../input/meta.shop.json | 28 ++ .../error-needs-unresolved/providers.json | 1 + .../systems-apps-and-resources/expected.json | 123 +++++ .../input/meta.shop.json | 123 +++++ .../systems-apps-and-resources/providers.json | 1 + .../systems/fixtures/expected-vocabulary.json | 441 ++++++++++++++++++ packs/systems/metamodel/systems.json | 87 ++++ packs/systems/pack.json | 10 + packs/systems/typescript/package.json | 20 + packs/systems/typescript/src/constants.ts | 28 ++ packs/systems/typescript/src/index.ts | 67 +++ packs/systems/typescript/src/refs.ts | 72 +++ packs/systems/typescript/test/refs.test.ts | 57 +++ packs/systems/typescript/tsconfig.json | 7 + scripts/pack-conformance.ts | 19 +- .../typescript/packages/metadata/src/index.ts | 7 + .../packages/metadata/src/provider-data.ts | 14 +- .../metadata/test/provider-data.test.ts | 15 + 76 files changed, 3199 insertions(+), 9 deletions(-) create mode 100644 changelog.d/metadata-apply-provider-definition-export.md create mode 100644 packs/delivery/CHANGELOG.md create mode 100644 packs/delivery/README.md create mode 100644 packs/delivery/fixtures/conformance/ERROR-CODES.json create mode 100644 packs/delivery/fixtures/conformance/delivery-builds-runtime-environments/expected.json create mode 100644 packs/delivery/fixtures/conformance/delivery-builds-runtime-environments/input/meta.shop.json create mode 100644 packs/delivery/fixtures/conformance/delivery-builds-runtime-environments/providers.json create mode 100644 packs/delivery/fixtures/conformance/error-deployment-base-authored/expected-errors.json create mode 100644 packs/delivery/fixtures/conformance/error-deployment-base-authored/input/meta.shop.json create mode 100644 packs/delivery/fixtures/conformance/error-deployment-base-authored/providers.json create mode 100644 packs/delivery/fixtures/conformance/error-restart-not-allowed/expected-errors.json create mode 100644 packs/delivery/fixtures/conformance/error-restart-not-allowed/input/meta.shop.json create mode 100644 packs/delivery/fixtures/conformance/error-restart-not-allowed/providers.json create mode 100644 packs/delivery/fixtures/conformance/error-two-builds/expected-errors.json create mode 100644 packs/delivery/fixtures/conformance/error-two-builds/input/meta.shop.json create mode 100644 packs/delivery/fixtures/conformance/error-two-builds/providers.json create mode 100644 packs/delivery/fixtures/expected-vocabulary.json create mode 100644 packs/delivery/metamodel/delivery.json create mode 100644 packs/delivery/pack.json create mode 100644 packs/delivery/typescript/package.json create mode 100644 packs/delivery/typescript/src/constants.ts create mode 100644 packs/delivery/typescript/src/index.ts create mode 100644 packs/delivery/typescript/tsconfig.json create mode 100644 packs/railway/CHANGELOG.md create mode 100644 packs/railway/README.md create mode 100644 packs/railway/fixtures/conformance/error-deployment-missing-app/expected-errors.json create mode 100644 packs/railway/fixtures/conformance/error-deployment-missing-app/input/meta.shop.json create mode 100644 packs/railway/fixtures/conformance/error-deployment-missing-app/providers.json create mode 100644 packs/railway/fixtures/conformance/error-deployment-refs/expected-errors.json create mode 100644 packs/railway/fixtures/conformance/error-deployment-refs/input/meta.shop.json create mode 100644 packs/railway/fixtures/conformance/error-deployment-refs/providers.json create mode 100644 packs/railway/fixtures/conformance/error-value-ref-unresolved/expected-errors.json create mode 100644 packs/railway/fixtures/conformance/error-value-ref-unresolved/input/meta.shop.json create mode 100644 packs/railway/fixtures/conformance/error-value-ref-unresolved/providers.json create mode 100644 packs/railway/fixtures/conformance/railway-worked-example/expected.json create mode 100644 packs/railway/fixtures/conformance/railway-worked-example/input/meta.app-system.json create mode 100644 packs/railway/fixtures/conformance/railway-worked-example/input/meta.estate-overrides.json create mode 100644 packs/railway/fixtures/conformance/railway-worked-example/input/meta.estate.json create mode 100644 packs/railway/fixtures/conformance/railway-worked-example/providers.json create mode 100644 packs/railway/fixtures/expected-vocabulary.json create mode 100644 packs/railway/metamodel/railway.json create mode 100644 packs/railway/pack.json create mode 100644 packs/railway/typescript/package.json create mode 100644 packs/railway/typescript/src/index.ts create mode 100644 packs/railway/typescript/tsconfig.json create mode 100644 packs/systems/CHANGELOG.md create mode 100644 packs/systems/README.md create mode 100644 packs/systems/fixtures/conformance/ERROR-CODES.json create mode 100644 packs/systems/fixtures/conformance/error-app-outside-system/expected-errors.json create mode 100644 packs/systems/fixtures/conformance/error-app-outside-system/input/meta.shop.json create mode 100644 packs/systems/fixtures/conformance/error-app-outside-system/providers.json create mode 100644 packs/systems/fixtures/conformance/error-calls-wrong-kind/expected-errors.json create mode 100644 packs/systems/fixtures/conformance/error-calls-wrong-kind/input/meta.shop.json create mode 100644 packs/systems/fixtures/conformance/error-calls-wrong-kind/providers.json create mode 100644 packs/systems/fixtures/conformance/error-needs-unresolved/expected-errors.json create mode 100644 packs/systems/fixtures/conformance/error-needs-unresolved/input/meta.shop.json create mode 100644 packs/systems/fixtures/conformance/error-needs-unresolved/providers.json create mode 100644 packs/systems/fixtures/conformance/systems-apps-and-resources/expected.json create mode 100644 packs/systems/fixtures/conformance/systems-apps-and-resources/input/meta.shop.json create mode 100644 packs/systems/fixtures/conformance/systems-apps-and-resources/providers.json create mode 100644 packs/systems/fixtures/expected-vocabulary.json create mode 100644 packs/systems/metamodel/systems.json create mode 100644 packs/systems/pack.json create mode 100644 packs/systems/typescript/package.json create mode 100644 packs/systems/typescript/src/constants.ts create mode 100644 packs/systems/typescript/src/index.ts create mode 100644 packs/systems/typescript/src/refs.ts create mode 100644 packs/systems/typescript/test/refs.test.ts create mode 100644 packs/systems/typescript/tsconfig.json diff --git a/bun.lock b/bun.lock index fa652b6f2d..4c74c6a9ae 100644 --- a/bun.lock +++ b/bun.lock @@ -150,6 +150,43 @@ "typescript": "^5.6.0", }, }, + "packs/delivery/typescript": { + "name": "@metaobjectsdev/pack-delivery", + "version": "0.1.0", + "dependencies": { + "@metaobjectsdev/pack-systems": "workspace:*", + }, + "devDependencies": { + "@metaobjectsdev/metadata": "workspace:*", + }, + "peerDependencies": { + "@metaobjectsdev/metadata": ">=1.3.0 <2.0.0", + }, + }, + "packs/railway/typescript": { + "name": "@metaobjectsdev/pack-railway", + "version": "0.1.0", + "dependencies": { + "@metaobjectsdev/pack-delivery": "workspace:*", + "@metaobjectsdev/pack-systems": "workspace:*", + }, + "devDependencies": { + "@metaobjectsdev/metadata": "workspace:*", + }, + "peerDependencies": { + "@metaobjectsdev/metadata": ">=1.3.0 <2.0.0", + }, + }, + "packs/systems/typescript": { + "name": "@metaobjectsdev/pack-systems", + "version": "0.1.0", + "devDependencies": { + "@metaobjectsdev/metadata": "workspace:*", + }, + "peerDependencies": { + "@metaobjectsdev/metadata": ">=1.3.0 <2.0.0", + }, + }, "server/typescript/packages/ai-runtime": { "name": "@metaobjectsdev/ai-runtime", "version": "1.3.0", @@ -752,6 +789,12 @@ "@metaobjectsdev/migrate-ts": ["@metaobjectsdev/migrate-ts@workspace:server/typescript/packages/migrate-ts"], + "@metaobjectsdev/pack-delivery": ["@metaobjectsdev/pack-delivery@workspace:packs/delivery/typescript"], + + "@metaobjectsdev/pack-railway": ["@metaobjectsdev/pack-railway@workspace:packs/railway/typescript"], + + "@metaobjectsdev/pack-systems": ["@metaobjectsdev/pack-systems@workspace:packs/systems/typescript"], + "@metaobjectsdev/react": ["@metaobjectsdev/react@workspace:client/web/packages/react"], "@metaobjectsdev/render": ["@metaobjectsdev/render@workspace:server/typescript/packages/render"], diff --git a/changelog.d/metadata-apply-provider-definition-export.md b/changelog.d/metadata-apply-provider-definition-export.md new file mode 100644 index 0000000000..4776bb7da0 --- /dev/null +++ b/changelog.d/metadata-apply-provider-definition-export.md @@ -0,0 +1,8 @@ +- **metadata: a provider outside core can register vocabulary from a definition file.** + `applyProviderDefinition` and its types (`ProviderDefinition`, `TypeDef`, `ChildDef`, + `ExtendDef`, `FactoryMap`, `ValidatorMap`) are exported from `@metaobjectsdev/metadata`, with + the validation hook's types (`NodeValidator`, `ValidationContext`, `SymbolTable`, + `LoaderCode`). `applyProviderDefinition` takes an optional fourth argument, a + `"."` → validator map, so a type registered from data can carry a check the data + cannot express. The bundled packs already registered this way; the independent packs under + `packs/` (ADR-0069) now can too. No vocabulary or `metamodelVersion` change. diff --git a/docs/features/extending-with-providers.md b/docs/features/extending-with-providers.md index 9466e16c3e..9677b4b5af 100644 --- a/docs/features/extending-with-providers.md +++ b/docs/features/extending-with-providers.md @@ -160,6 +160,14 @@ but is not yet core vocabulary. The contract the provider is written against (`i `dependencies`, `registerTypes`, `register`, `extend`, `registerCommonAttrs` and the definition file format) is covered by core's package version (`docs/compatibility-policy.md`). +**From a definition file.** A provider can keep its vocabulary as data, in the +`spec/metamodel/*.json` format, and register it with `applyProviderDefinition(registry, definition, +factories, validators?)` from `@metaobjectsdev/metadata`. `factories` maps each +`"."` to the function that constructs its node; the optional `validators` map gives +a type a `NodeValidator`, for a rule the data cannot state (a reference to a nested node, say). +The bundled packs register this way, and so do the independent packs: `packs/systems/` is a +worked example. + ## Two registration modes ### `register` — declare a new `(type, subType)` diff --git a/packs/README.md b/packs/README.md index ca3ae1e3a4..6db90d81c4 100644 --- a/packs/README.md +++ b/packs/README.md @@ -2,7 +2,13 @@ A directory here is an **independent pack**: vocabulary, generators and runtime for a capability that is not part of core, built and versioned as if it were its own repository -([ADR-0069](../spec/decisions/ADR-0069-independent-packs.md)). None exists yet. +([ADR-0069](../spec/decisions/ADR-0069-independent-packs.md)). + +| Pack | What it declares | Requires | Ports | +|---|---|---|---| +| [`systems`](systems/README.md) | systems, apps (service, client, job), resources | core | TypeScript | +| [`delivery`](delivery/README.md) | environments, builds, runtime processes, deployments, value bindings | `systems` | TypeScript | +| [`railway`](railway/README.md) | `deployment.railway` (vocabulary only; the emitter is later work) | `delivery` | TypeScript | An independent pack is not in the default bundle. A project opts in by naming its provider. It is never in `fixtures/registry-conformance/expected-registry.json` and never moves diff --git a/packs/delivery/CHANGELOG.md b/packs/delivery/CHANGELOG.md new file mode 100644 index 0000000000..016a14a3c2 --- /dev/null +++ b/packs/delivery/CHANGELOG.md @@ -0,0 +1,11 @@ +# Changelog: delivery pack + +This pack's own changelog (ADR-0069). The root `CHANGELOG.md` records core only. + +## [0.1.0] - unreleased + +- **First version.** `environment.standing`, `.ephemeral`, `.local`; `build.dockerfile`, + `.command`, `.static` (at most one per app); `runtime.process`; the abstract `deployment.base` + a platform pack specialises; `value.literal` and `value.ref`. A deployment's `@app` and + `@environment` and a `value.ref`'s `@from` are resolved at load (`ERR_DELIVERY_UNRESOLVED_REF`); + `@secrets` and `@setting` are names only. TypeScript port. diff --git a/packs/delivery/README.md b/packs/delivery/README.md new file mode 100644 index 0000000000..4ac83ba325 --- /dev/null +++ b/packs/delivery/README.md @@ -0,0 +1,68 @@ +# delivery pack + +`metaobjects-delivery`, version 0.1.0, **preview**, unpublished. An independent pack +([ADR-0069](../../spec/decisions/ADR-0069-independent-packs.md)). Requires +[`systems`](../systems/README.md). + +It answers: **what runs where, and what differs per environment?** Facts true in every +environment sit under the app (how it is built, how its process runs). A deployment places one app +in one environment on one platform, and holds only what differs there. + +## Vocabulary + +| Node | Where | What it is | Attributes | +|---|---|---|---| +| `environment.standing`, `.ephemeral`, `.local` | document root | A named environment: production, a pull-request preview, a workstation | (documentation only) | +| `build.dockerfile` | under an `app.*`, at most one build | Built as an image from a Dockerfile | `@path`, `@context`, `@watchExclude` | +| `build.command` | under an `app.*` | Built by a command the platform's builder runs | `@command`, `@watchExclude` | +| `build.static` | under an `app.*` | A directory of files served as they are | `@directory`, `@command`, `@watchExclude` | +| `runtime.process` | under an `app.*` | How one of its processes runs | `@command`, `@port`, `@healthPath`, `@healthTimeoutSeconds`, `@drainSeconds`, `@restart` (`always`, `onFailure`, `never`) | +| `deployment.` | document root | An app in an environment on a platform. A **platform pack** registers the subtype (the [`railway`](../railway/README.md) pack registers `deployment.railway`) | `@app`, `@environment`, `@domains`, `@replicas`, `@region`, `@branch`, `@secrets`, plus the platform's own | +| `value.literal` | under a deployment | A setting's fixed value there | `@setting`, `@literal` | +| `value.ref` | under a deployment | A setting's value produced by another node at deploy time | `@setting`, `@from`, `@output` | + +`deployment.base` is the abstract anchor platform packs specialise (`extendsBase: true`); this pack +registers no concrete deployment, and an authored `deployment.base` fails to load +(`ERR_ABSTRACT_SUBTYPE_AUTHORED`). A secret needs no value node: the setting is marked secret in +the config pack and the deployment names its vault in `@secrets`. The full definition is +[`metamodel/delivery.json`](metamodel/delivery.json). + +## References + +- `@app` names an app as `System.app`, or `pkg::System.app` from another package. +- `@environment` names an `environment.*` node. +- `value.ref`'s `@from` names a `resource.*` or `app.*` the same way; `@output` says which of its + values (url, host, port, privateUrl, ...), for a platform generator to map. This is how an address + stays out of a repository: it is named, never written. + +Each fails the load with `ERR_DELIVERY_UNRESOLVED_REF` when it resolves to nothing or to the wrong +kind of node. A platform pack holds its deployments to the same rule by passing +`validateDeployment` as its subtype's validator. `@secrets` (a config pack `vault.*`) and +`@setting` (`Group.field` of an `object.settings`) are names only: this pack does not require the +config pack. + +## The estate pattern + +The app's repository declares its own deployments. A separate estate repository loads that model, +adds deployments the app never declared (a staging environment), and overrides the ones it did by +overlay, using the loader's existing merge: + +```json +{ "metadata.root": { "package": "acme::game", "children": [ + { "deployment.railway": { "name": "server-production", "overlay": true, "@replicas": 2 } } +] } } +``` + +The `railway` pack's `railway-worked-example` fixture is the whole example, both sides. + +## Ports + +TypeScript only (ADR-0069's port rule: elsewhere a model that uses it fails by name). + +## Checks + +``` +bun scripts/pack-vocabulary.ts delivery +bun scripts/pack-conformance.ts delivery +scripts/ci-local.sh --only packs +``` diff --git a/packs/delivery/fixtures/conformance/ERROR-CODES.json b/packs/delivery/fixtures/conformance/ERROR-CODES.json new file mode 100644 index 0000000000..e6a9c10be1 --- /dev/null +++ b/packs/delivery/fixtures/conformance/ERROR-CODES.json @@ -0,0 +1,6 @@ +{ + "version": 1, + "codes": { + "ERR_DELIVERY_UNRESOLVED_REF": "A deployment's @app or @environment, or a value.ref's @from, names no node, or names a node of the wrong kind (@app takes an app.*, @environment an environment.*, @from a resource.* or an app.*)." + } +} diff --git a/packs/delivery/fixtures/conformance/delivery-builds-runtime-environments/expected.json b/packs/delivery/fixtures/conformance/delivery-builds-runtime-environments/expected.json new file mode 100644 index 0000000000..eac0375b02 --- /dev/null +++ b/packs/delivery/fixtures/conformance/delivery-builds-runtime-environments/expected.json @@ -0,0 +1,103 @@ +{ + "metadata.root": { + "package": "acme::shop", + "children": [ + { + "system.internal": { + "name": "Shop", + "children": [ + { + "app.client": { + "name": "storefront", + "children": [ + { + "build.static": { + "name": "site", + "@command": "npm run build", + "@directory": "dist", + "@watchExclude": [ + "docs/**" + ] + } + } + ] + } + }, + { + "app.service": { + "name": "api", + "children": [ + { + "build.command": { + "name": "jar", + "@command": "./gradlew bootJar" + } + }, + { + "runtime.process": { + "name": "web", + "@command": "java -jar app.jar", + "@drainSeconds": 30, + "@healthPath": "/health", + "@healthTimeoutSeconds": 120, + "@port": 8080, + "@restart": "always" + } + }, + { + "runtime.process": { + "name": "worker", + "@command": "java -jar app.jar --worker", + "@restart": "onFailure" + } + } + ] + } + }, + { + "app.job": { + "name": "nightlyExport", + "@schedule": "0 3 * * *", + "children": [ + { + "build.dockerfile": { + "name": "image", + "@context": "jobs", + "@path": "jobs/Dockerfile" + } + }, + { + "runtime.process": { + "name": "run", + "@restart": "never" + } + } + ] + } + } + ] + } + }, + { + "environment.standing": { + "name": "production" + } + }, + { + "environment.standing": { + "name": "staging" + } + }, + { + "environment.ephemeral": { + "name": "preview" + } + }, + { + "environment.local": { + "name": "workstation" + } + } + ] + } +} diff --git a/packs/delivery/fixtures/conformance/delivery-builds-runtime-environments/input/meta.shop.json b/packs/delivery/fixtures/conformance/delivery-builds-runtime-environments/input/meta.shop.json new file mode 100644 index 0000000000..d13717b2ab --- /dev/null +++ b/packs/delivery/fixtures/conformance/delivery-builds-runtime-environments/input/meta.shop.json @@ -0,0 +1,103 @@ +{ + "metadata.root": { + "package": "acme::shop", + "children": [ + { + "system.internal": { + "name": "Shop", + "children": [ + { + "app.client": { + "name": "storefront", + "children": [ + { + "build.static": { + "name": "site", + "@directory": "dist", + "@command": "npm run build", + "@watchExclude": [ + "docs/**" + ] + } + } + ] + } + }, + { + "app.service": { + "name": "api", + "children": [ + { + "build.command": { + "name": "jar", + "@command": "./gradlew bootJar" + } + }, + { + "runtime.process": { + "name": "web", + "@command": "java -jar app.jar", + "@port": 8080, + "@healthPath": "/health", + "@healthTimeoutSeconds": 120, + "@drainSeconds": 30, + "@restart": "always" + } + }, + { + "runtime.process": { + "name": "worker", + "@command": "java -jar app.jar --worker", + "@restart": "onFailure" + } + } + ] + } + }, + { + "app.job": { + "name": "nightlyExport", + "@schedule": "0 3 * * *", + "children": [ + { + "build.dockerfile": { + "name": "image", + "@path": "jobs/Dockerfile", + "@context": "jobs" + } + }, + { + "runtime.process": { + "name": "run", + "@restart": "never" + } + } + ] + } + } + ] + } + }, + { + "environment.standing": { + "name": "production" + } + }, + { + "environment.standing": { + "name": "staging" + } + }, + { + "environment.ephemeral": { + "name": "preview" + } + }, + { + "environment.local": { + "name": "workstation" + } + } + ] + } +} diff --git a/packs/delivery/fixtures/conformance/delivery-builds-runtime-environments/providers.json b/packs/delivery/fixtures/conformance/delivery-builds-runtime-environments/providers.json new file mode 100644 index 0000000000..f6baea0511 --- /dev/null +++ b/packs/delivery/fixtures/conformance/delivery-builds-runtime-environments/providers.json @@ -0,0 +1 @@ +["metaobjects-core-types", "metaobjects-documentation", "metaobjects-systems", "metaobjects-delivery"] diff --git a/packs/delivery/fixtures/conformance/error-deployment-base-authored/expected-errors.json b/packs/delivery/fixtures/conformance/error-deployment-base-authored/expected-errors.json new file mode 100644 index 0000000000..e5c4c7a368 --- /dev/null +++ b/packs/delivery/fixtures/conformance/error-deployment-base-authored/expected-errors.json @@ -0,0 +1,15 @@ +{ + "errors": [ + { + "code": "ERR_ABSTRACT_SUBTYPE_AUTHORED", + "source": { + "format": "json", + "files": [ + "meta.shop.json" + ], + "jsonPath": "$['metadata.root'].children[2]['deployment.base']" + } + } + ], + "warnings": [] +} diff --git a/packs/delivery/fixtures/conformance/error-deployment-base-authored/input/meta.shop.json b/packs/delivery/fixtures/conformance/error-deployment-base-authored/input/meta.shop.json new file mode 100644 index 0000000000..7eb8e84a4e --- /dev/null +++ b/packs/delivery/fixtures/conformance/error-deployment-base-authored/input/meta.shop.json @@ -0,0 +1,48 @@ +{ + "metadata.root": { + "package": "acme::shop", + "children": [ + { + "system.internal": { + "name": "Shop", + "children": [ + { + "app.service": { + "name": "api", + "@needs": [ + "db" + ], + "@calls": [ + "Payments" + ] + } + }, + { + "resource.database": { + "name": "db", + "@engine": "postgres" + } + }, + { + "system.external": { + "name": "Payments" + } + } + ] + } + }, + { + "environment.standing": { + "name": "production" + } + }, + { + "deployment.base": { + "name": "api-production", + "@app": "Shop.api", + "@environment": "production" + } + } + ] + } +} diff --git a/packs/delivery/fixtures/conformance/error-deployment-base-authored/providers.json b/packs/delivery/fixtures/conformance/error-deployment-base-authored/providers.json new file mode 100644 index 0000000000..f6baea0511 --- /dev/null +++ b/packs/delivery/fixtures/conformance/error-deployment-base-authored/providers.json @@ -0,0 +1 @@ +["metaobjects-core-types", "metaobjects-documentation", "metaobjects-systems", "metaobjects-delivery"] diff --git a/packs/delivery/fixtures/conformance/error-restart-not-allowed/expected-errors.json b/packs/delivery/fixtures/conformance/error-restart-not-allowed/expected-errors.json new file mode 100644 index 0000000000..94b95548c6 --- /dev/null +++ b/packs/delivery/fixtures/conformance/error-restart-not-allowed/expected-errors.json @@ -0,0 +1,15 @@ +{ + "errors": [ + { + "code": "ERR_BAD_ATTR_VALUE", + "source": { + "format": "json", + "files": [ + "meta.shop.json" + ], + "jsonPath": "$['metadata.root'].children[0]['system.internal'].children[0]['app.service'].children[0]['runtime.process']" + } + } + ], + "warnings": [] +} diff --git a/packs/delivery/fixtures/conformance/error-restart-not-allowed/input/meta.shop.json b/packs/delivery/fixtures/conformance/error-restart-not-allowed/input/meta.shop.json new file mode 100644 index 0000000000..0c474a2f51 --- /dev/null +++ b/packs/delivery/fixtures/conformance/error-restart-not-allowed/input/meta.shop.json @@ -0,0 +1,27 @@ +{ + "metadata.root": { + "package": "acme::shop", + "children": [ + { + "system.internal": { + "name": "Shop", + "children": [ + { + "app.service": { + "name": "api", + "children": [ + { + "runtime.process": { + "name": "web", + "@restart": "sometimes" + } + } + ] + } + } + ] + } + } + ] + } +} diff --git a/packs/delivery/fixtures/conformance/error-restart-not-allowed/providers.json b/packs/delivery/fixtures/conformance/error-restart-not-allowed/providers.json new file mode 100644 index 0000000000..f6baea0511 --- /dev/null +++ b/packs/delivery/fixtures/conformance/error-restart-not-allowed/providers.json @@ -0,0 +1 @@ +["metaobjects-core-types", "metaobjects-documentation", "metaobjects-systems", "metaobjects-delivery"] diff --git a/packs/delivery/fixtures/conformance/error-two-builds/expected-errors.json b/packs/delivery/fixtures/conformance/error-two-builds/expected-errors.json new file mode 100644 index 0000000000..c7295ce6d5 --- /dev/null +++ b/packs/delivery/fixtures/conformance/error-two-builds/expected-errors.json @@ -0,0 +1,15 @@ +{ + "errors": [ + { + "code": "ERR_TOO_MANY_OCCURRENCES", + "source": { + "format": "json", + "files": [ + "meta.shop.json" + ], + "jsonPath": "$['metadata.root'].children[0]['system.internal'].children[0]['app.service'].children[1]['build.command']" + } + } + ], + "warnings": [] +} diff --git a/packs/delivery/fixtures/conformance/error-two-builds/input/meta.shop.json b/packs/delivery/fixtures/conformance/error-two-builds/input/meta.shop.json new file mode 100644 index 0000000000..d81daf6e92 --- /dev/null +++ b/packs/delivery/fixtures/conformance/error-two-builds/input/meta.shop.json @@ -0,0 +1,32 @@ +{ + "metadata.root": { + "package": "acme::shop", + "children": [ + { + "system.internal": { + "name": "Shop", + "children": [ + { + "app.service": { + "name": "api", + "children": [ + { + "build.dockerfile": { + "name": "image" + } + }, + { + "build.command": { + "name": "jar", + "@command": "make" + } + } + ] + } + } + ] + } + } + ] + } +} diff --git a/packs/delivery/fixtures/conformance/error-two-builds/providers.json b/packs/delivery/fixtures/conformance/error-two-builds/providers.json new file mode 100644 index 0000000000..f6baea0511 --- /dev/null +++ b/packs/delivery/fixtures/conformance/error-two-builds/providers.json @@ -0,0 +1 @@ +["metaobjects-core-types", "metaobjects-documentation", "metaobjects-systems", "metaobjects-delivery"] diff --git a/packs/delivery/fixtures/expected-vocabulary.json b/packs/delivery/fixtures/expected-vocabulary.json new file mode 100644 index 0000000000..c93cc8b60d --- /dev/null +++ b/packs/delivery/fixtures/expected-vocabulary.json @@ -0,0 +1,430 @@ +{ + "pack": "metaobjects-delivery", + "types": [ + { + "type": "build", + "subType": "base", + "description": "Abstract build base: the attributes every build.* shares. A build is a child of an app and says how its deployable is made, the same in every environment. Not authored directly: a `build.base` node fails to load (ERR_ABSTRACT_SUBTYPE_AUTHORED).", + "attrs": [ + { + "name": "watchExclude", + "valueType": "string", + "isArray": true, + "required": false, + "description": "Repository path globs whose change must not start a new build (`docs/**`, `*.md`). A platform that has no place for them in its own file gets them as a fact to set by hand, which its generator lists." + } + ], + "children": [] + }, + { + "type": "build", + "subType": "command", + "description": "The app is built by a command the platform's own builder runs, with no Dockerfile.", + "whenToUse": "A buildpack-style build: `npm run build`, `./gradlew bootJar`.", + "attrs": [ + { + "name": "command", + "valueType": "string", + "isArray": false, + "required": true, + "description": "The build command, run from the repository root." + }, + { + "name": "watchExclude", + "valueType": "string", + "isArray": true, + "required": false, + "description": "Repository path globs whose change must not start a new build (`docs/**`, `*.md`). A platform that has no place for them in its own file gets them as a fact to set by hand, which its generator lists." + } + ], + "children": [] + }, + { + "type": "build", + "subType": "dockerfile", + "description": "The app is built as a container image from a Dockerfile.", + "whenToUse": "Any app whose image is described by a Dockerfile in the repository.", + "attrs": [ + { + "name": "context", + "valueType": "string", + "isArray": false, + "required": false, + "description": "The build context directory from the repository root. Absent means the root." + }, + { + "name": "path", + "valueType": "string", + "isArray": false, + "required": false, + "description": "The Dockerfile's path from the repository root. Absent means `Dockerfile`." + }, + { + "name": "watchExclude", + "valueType": "string", + "isArray": true, + "required": false, + "description": "Repository path globs whose change must not start a new build (`docs/**`, `*.md`). A platform that has no place for them in its own file gets them as a fact to set by hand, which its generator lists." + } + ], + "children": [] + }, + { + "type": "build", + "subType": "static", + "description": "The app is a directory of files served as they are, optionally produced by a command first.", + "whenToUse": "A static site or a single-page client served by a CDN.", + "attrs": [ + { + "name": "command", + "valueType": "string", + "isArray": false, + "required": false, + "description": "The command that produces the directory. Absent means the directory is committed as it is." + }, + { + "name": "directory", + "valueType": "string", + "isArray": false, + "required": true, + "description": "The directory of files to serve, from the repository root (`dist`)." + }, + { + "name": "watchExclude", + "valueType": "string", + "isArray": true, + "required": false, + "description": "Repository path globs whose change must not start a new build (`docs/**`, `*.md`). A platform that has no place for them in its own file gets them as a fact to set by hand, which its generator lists." + } + ], + "children": [] + }, + { + "type": "deployment", + "subType": "base", + "description": "Abstract deployment base: an app placed in an environment on a platform. A platform pack registers the concrete subtype (`deployment.`), which takes these attributes and adds the platform's own. A deployment holds only what differs per environment; what is true everywhere sits under the app. An estate repository adds deployments the app never declared and overrides the ones it did by overlay. Not authored directly: a `deployment.base` node fails to load (ERR_ABSTRACT_SUBTYPE_AUTHORED).", + "attrs": [ + { + "name": "app", + "valueType": "string", + "isArray": false, + "required": true, + "description": "The app deployed: `System.app` (`Shop.web`), or `pkg::System.app` from another package. It must resolve to an app.* node (ERR_DELIVERY_UNRESOLVED_REF)." + }, + { + "name": "branch", + "valueType": "string", + "isArray": false, + "required": false, + "description": "The repository branch whose pushes deploy here." + }, + { + "name": "domains", + "valueType": "string", + "isArray": true, + "required": false, + "description": "The public host names that route to the app here." + }, + { + "name": "environment", + "valueType": "string", + "isArray": false, + "required": true, + "description": "The environment.* node it deploys to, by name (or `pkg::name`). It must resolve (ERR_DELIVERY_UNRESOLVED_REF)." + }, + { + "name": "region", + "valueType": "string", + "isArray": false, + "required": false, + "description": "The platform's region name." + }, + { + "name": "replicas", + "valueType": "int", + "isArray": false, + "required": false, + "description": "How many copies run. Absent means the platform's default." + }, + { + "name": "secrets", + "valueType": "string", + "isArray": false, + "required": false, + "description": "The vault the deployment's secret settings come from, by name (a vault.* of the config pack). Recorded by name only: this pack does not require the config pack." + } + ], + "children": [ + { + "childType": "value", + "childSubType": [ + "literal", + "ref" + ], + "childName": "*", + "min": 0, + "max": null + } + ] + }, + { + "type": "environment", + "subType": "base", + "description": "Abstract environment base. Not authored directly: an `environment.base` node fails to load (ERR_ABSTRACT_SUBTYPE_AUTHORED).", + "attrs": [], + "children": [] + }, + { + "type": "environment", + "subType": "ephemeral", + "description": "An environment created for one change and removed after it: a pull-request preview.", + "whenToUse": "Per-branch or per-pull-request previews.", + "attrs": [], + "children": [] + }, + { + "type": "environment", + "subType": "local", + "description": "A developer's own machine.", + "whenToUse": "The settings and resources a project needs to run on a workstation.", + "attrs": [], + "children": [] + }, + { + "type": "environment", + "subType": "standing", + "description": "A named environment that stays up: production, staging. Deployments name it with @environment; a settings vault maps its secrets to it by the same name.", + "whenToUse": "Every environment that exists whether or not anyone is working on it.", + "attrs": [], + "children": [] + }, + { + "type": "runtime", + "subType": "process", + "description": "How one of an app's processes runs, the same in every environment: its start command, port, health check, shutdown drain and restart policy. A platform generator writes what its platform's file can hold and lists the rest.", + "whenToUse": "Every app that runs as a process. A second runtime.process is a second process of the same app (a worker beside the web server).", + "attrs": [ + { + "name": "command", + "valueType": "string", + "isArray": false, + "required": false, + "description": "The start command. Absent means the image's or the builder's default." + }, + { + "name": "drainSeconds", + "valueType": "int", + "isArray": false, + "required": false, + "description": "How long the old process keeps running after the new one is healthy, so requests in flight finish." + }, + { + "name": "healthPath", + "valueType": "string", + "isArray": false, + "required": false, + "description": "The HTTP path that answers 2xx when the process is ready (`/health`)." + }, + { + "name": "healthTimeoutSeconds", + "valueType": "int", + "isArray": false, + "required": false, + "description": "How long a new deployment may take to pass its health check before it is failed." + }, + { + "name": "port", + "valueType": "int", + "isArray": false, + "required": false, + "description": "The port the process listens on." + }, + { + "name": "restart", + "valueType": "string", + "isArray": false, + "required": false, + "allowedValues": [ + "always", + "onFailure", + "never" + ], + "description": "When the platform restarts the process after it exits: always, onFailure (a non-zero exit) or never." + } + ], + "children": [] + }, + { + "type": "value", + "subType": "base", + "description": "Abstract value base: how one setting gets its value in one deployment. A child of a deployment. A secret needs no value node: the setting is marked secret and the deployment names its vault. Not authored directly: a `value.base` node fails to load (ERR_ABSTRACT_SUBTYPE_AUTHORED).", + "attrs": [ + { + "name": "setting", + "valueType": "string", + "isArray": false, + "required": true, + "description": "The setting this value is for, as `Group.field` (a field of an object.settings of the config pack). Recorded by name only: this pack does not require the config pack." + } + ], + "children": [] + }, + { + "type": "value", + "subType": "literal", + "description": "The setting takes a fixed value in this deployment.", + "whenToUse": "A non-secret value that differs per environment: a profile name, a feature flag, a log level.", + "attrs": [ + { + "name": "literal", + "valueType": "string", + "isArray": false, + "required": true, + "description": "The value, as a string (the setting's type parses it). Named @literal because `value` is a reserved structural key." + }, + { + "name": "setting", + "valueType": "string", + "isArray": false, + "required": true, + "description": "The setting this value is for, as `Group.field` (a field of an object.settings of the config pack). Recorded by name only: this pack does not require the config pack." + } + ], + "children": [] + }, + { + "type": "value", + "subType": "ref", + "description": "The setting takes a value another node produces at deploy time: a database's connection URL, another app's private address. This is how an address stays out of a repository: it is named, never written.", + "whenToUse": "A setting whose value only the platform knows once the resource or app exists.", + "attrs": [ + { + "name": "from", + "valueType": "string", + "isArray": false, + "required": true, + "description": "The node that produces the value: a resource.* or app.*, as `System.child` (`Shop.db`, or `pkg::System.child`). It must resolve (ERR_DELIVERY_UNRESOLVED_REF)." + }, + { + "name": "output", + "valueType": "string", + "isArray": false, + "required": true, + "description": "Which of its values: url, host, port, user, password, privateUrl, publicUrl. A platform generator maps the name to the platform's own reference and refuses one it cannot supply." + }, + { + "name": "setting", + "valueType": "string", + "isArray": false, + "required": true, + "description": "The setting this value is for, as `Group.field` (a field of an object.settings of the config pack). Recorded by name only: this pack does not require the config pack." + } + ], + "children": [] + } + ], + "extends": [ + { + "type": "app", + "subType": "client", + "attrs": [], + "children": [ + { + "childType": "build", + "childSubType": [ + "dockerfile", + "command", + "static" + ], + "childName": "*", + "min": 0, + "max": 1 + }, + { + "childType": "runtime", + "childSubType": "process", + "childName": "*", + "min": 0, + "max": null + } + ] + }, + { + "type": "app", + "subType": "job", + "attrs": [], + "children": [ + { + "childType": "build", + "childSubType": [ + "dockerfile", + "command", + "static" + ], + "childName": "*", + "min": 0, + "max": 1 + }, + { + "childType": "runtime", + "childSubType": "process", + "childName": "*", + "min": 0, + "max": null + } + ] + }, + { + "type": "app", + "subType": "service", + "attrs": [], + "children": [ + { + "childType": "build", + "childSubType": [ + "dockerfile", + "command", + "static" + ], + "childName": "*", + "min": 0, + "max": 1 + }, + { + "childType": "runtime", + "childSubType": "process", + "childName": "*", + "min": 0, + "max": null + } + ] + }, + { + "type": "metadata", + "subType": "root", + "attrs": [], + "children": [ + { + "childType": "deployment", + "childSubType": "*", + "childName": "*", + "min": 0, + "max": null + }, + { + "childType": "environment", + "childSubType": [ + "standing", + "ephemeral", + "local" + ], + "childName": "*", + "min": 0, + "max": null + } + ] + } + ], + "commonAttrs": [], + "defaultSubTypes": {} +} diff --git a/packs/delivery/metamodel/delivery.json b/packs/delivery/metamodel/delivery.json new file mode 100644 index 0000000000..4be04c0d9f --- /dev/null +++ b/packs/delivery/metamodel/delivery.json @@ -0,0 +1,121 @@ +{ + "provider": "metaobjects-delivery", + "types": [ + { + "type": "environment", + "subType": "base", + "description": "Abstract environment base. Not authored directly: an `environment.base` node fails to load (ERR_ABSTRACT_SUBTYPE_AUTHORED)." + }, + { "type": "environment", "subType": "standing", "extendsBase": true, "description": "A named environment that stays up: production, staging. Deployments name it with @environment; a settings vault maps its secrets to it by the same name.", "whenToUse": "Every environment that exists whether or not anyone is working on it." }, + { "type": "environment", "subType": "ephemeral", "extendsBase": true, "description": "An environment created for one change and removed after it: a pull-request preview.", "whenToUse": "Per-branch or per-pull-request previews." }, + { "type": "environment", "subType": "local", "extendsBase": true, "description": "A developer's own machine.", "whenToUse": "The settings and resources a project needs to run on a workstation." }, + { + "type": "build", + "subType": "base", + "description": "Abstract build base: the attributes every build.* shares. A build is a child of an app and says how its deployable is made, the same in every environment. Not authored directly: a `build.base` node fails to load (ERR_ABSTRACT_SUBTYPE_AUTHORED).", + "children": [ + { "type": "attr", "subType": "string", "name": "watchExclude", "isArray": true, "min": 0, "max": 1, "description": "Repository path globs whose change must not start a new build (`docs/**`, `*.md`). A platform that has no place for them in its own file gets them as a fact to set by hand, which its generator lists." } + ] + }, + { + "type": "build", + "subType": "dockerfile", + "extendsBase": true, + "description": "The app is built as a container image from a Dockerfile.", + "whenToUse": "Any app whose image is described by a Dockerfile in the repository.", + "children": [ + { "type": "attr", "subType": "string", "name": "path", "min": 0, "max": 1, "description": "The Dockerfile's path from the repository root. Absent means `Dockerfile`." }, + { "type": "attr", "subType": "string", "name": "context", "min": 0, "max": 1, "description": "The build context directory from the repository root. Absent means the root." } + ] + }, + { + "type": "build", + "subType": "command", + "extendsBase": true, + "description": "The app is built by a command the platform's own builder runs, with no Dockerfile.", + "whenToUse": "A buildpack-style build: `npm run build`, `./gradlew bootJar`.", + "children": [ + { "type": "attr", "subType": "string", "name": "command", "min": 1, "max": 1, "description": "The build command, run from the repository root." } + ] + }, + { + "type": "build", + "subType": "static", + "extendsBase": true, + "description": "The app is a directory of files served as they are, optionally produced by a command first.", + "whenToUse": "A static site or a single-page client served by a CDN.", + "children": [ + { "type": "attr", "subType": "string", "name": "directory", "min": 1, "max": 1, "description": "The directory of files to serve, from the repository root (`dist`)." }, + { "type": "attr", "subType": "string", "name": "command", "min": 0, "max": 1, "description": "The command that produces the directory. Absent means the directory is committed as it is." } + ] + }, + { + "type": "runtime", + "subType": "process", + "description": "How one of an app's processes runs, the same in every environment: its start command, port, health check, shutdown drain and restart policy. A platform generator writes what its platform's file can hold and lists the rest.", + "whenToUse": "Every app that runs as a process. A second runtime.process is a second process of the same app (a worker beside the web server).", + "children": [ + { "type": "attr", "subType": "string", "name": "command", "min": 0, "max": 1, "description": "The start command. Absent means the image's or the builder's default." }, + { "type": "attr", "subType": "int", "name": "port", "min": 0, "max": 1, "description": "The port the process listens on." }, + { "type": "attr", "subType": "string", "name": "healthPath", "min": 0, "max": 1, "description": "The HTTP path that answers 2xx when the process is ready (`/health`)." }, + { "type": "attr", "subType": "int", "name": "healthTimeoutSeconds", "min": 0, "max": 1, "description": "How long a new deployment may take to pass its health check before it is failed." }, + { "type": "attr", "subType": "int", "name": "drainSeconds", "min": 0, "max": 1, "description": "How long the old process keeps running after the new one is healthy, so requests in flight finish." }, + { "type": "attr", "subType": "string", "name": "restart", "min": 0, "max": 1, "allowedValues": ["always", "onFailure", "never"], "description": "When the platform restarts the process after it exits: always, onFailure (a non-zero exit) or never." } + ] + }, + { + "type": "deployment", + "subType": "base", + "description": "Abstract deployment base: an app placed in an environment on a platform. A platform pack registers the concrete subtype (`deployment.`), which takes these attributes and adds the platform's own. A deployment holds only what differs per environment; what is true everywhere sits under the app. An estate repository adds deployments the app never declared and overrides the ones it did by overlay. Not authored directly: a `deployment.base` node fails to load (ERR_ABSTRACT_SUBTYPE_AUTHORED).", + "children": [ + { "type": "attr", "subType": "string", "name": "app", "min": 1, "max": 1, "description": "The app deployed: `System.app` (`Shop.web`), or `pkg::System.app` from another package. It must resolve to an app.* node (ERR_DELIVERY_UNRESOLVED_REF)." }, + { "type": "attr", "subType": "string", "name": "environment", "min": 1, "max": 1, "description": "The environment.* node it deploys to, by name (or `pkg::name`). It must resolve (ERR_DELIVERY_UNRESOLVED_REF)." }, + { "type": "attr", "subType": "string", "name": "domains", "isArray": true, "min": 0, "max": 1, "description": "The public host names that route to the app here." }, + { "type": "attr", "subType": "int", "name": "replicas", "min": 0, "max": 1, "description": "How many copies run. Absent means the platform's default." }, + { "type": "attr", "subType": "string", "name": "region", "min": 0, "max": 1, "description": "The platform's region name." }, + { "type": "attr", "subType": "string", "name": "branch", "min": 0, "max": 1, "description": "The repository branch whose pushes deploy here." }, + { "type": "attr", "subType": "string", "name": "secrets", "min": 0, "max": 1, "description": "The vault the deployment's secret settings come from, by name (a vault.* of the config pack). Recorded by name only: this pack does not require the config pack." }, + { "type": "value", "subType": ["literal", "ref"], "name": "*", "min": 0, "max": null } + ] + }, + { + "type": "value", + "subType": "base", + "description": "Abstract value base: how one setting gets its value in one deployment. A child of a deployment. A secret needs no value node: the setting is marked secret and the deployment names its vault. Not authored directly: a `value.base` node fails to load (ERR_ABSTRACT_SUBTYPE_AUTHORED).", + "children": [ + { "type": "attr", "subType": "string", "name": "setting", "min": 1, "max": 1, "description": "The setting this value is for, as `Group.field` (a field of an object.settings of the config pack). Recorded by name only: this pack does not require the config pack." } + ] + }, + { + "type": "value", + "subType": "literal", + "extendsBase": true, + "description": "The setting takes a fixed value in this deployment.", + "whenToUse": "A non-secret value that differs per environment: a profile name, a feature flag, a log level.", + "children": [ + { "type": "attr", "subType": "string", "name": "literal", "min": 1, "max": 1, "description": "The value, as a string (the setting's type parses it). Named @literal because `value` is a reserved structural key." } + ] + }, + { + "type": "value", + "subType": "ref", + "extendsBase": true, + "description": "The setting takes a value another node produces at deploy time: a database's connection URL, another app's private address. This is how an address stays out of a repository: it is named, never written.", + "whenToUse": "A setting whose value only the platform knows once the resource or app exists.", + "children": [ + { "type": "attr", "subType": "string", "name": "from", "min": 1, "max": 1, "description": "The node that produces the value: a resource.* or app.*, as `System.child` (`Shop.db`, or `pkg::System.child`). It must resolve (ERR_DELIVERY_UNRESOLVED_REF)." }, + { "type": "attr", "subType": "string", "name": "output", "min": 1, "max": 1, "description": "Which of its values: url, host, port, user, password, privateUrl, publicUrl. A platform generator maps the name to the platform's own reference and refuses one it cannot supply." } + ] + } + ], + "extends": [ + { "type": "metadata", "subType": "root", "children": [ + { "type": "environment", "subType": ["standing", "ephemeral", "local"], "name": "*", "min": 0, "max": null }, + { "type": "deployment", "subType": "*", "name": "*", "min": 0, "max": null } + ] }, + { "type": "app", "subType": ["service", "client", "job"], "children": [ + { "type": "build", "subType": ["dockerfile", "command", "static"], "name": "*", "min": 0, "max": 1 }, + { "type": "runtime", "subType": "process", "name": "*", "min": 0, "max": null } + ] } + ] +} diff --git a/packs/delivery/pack.json b/packs/delivery/pack.json new file mode 100644 index 0000000000..229b07c576 --- /dev/null +++ b/packs/delivery/pack.json @@ -0,0 +1,10 @@ +{ + "id": "metaobjects-delivery", + "version": "0.1.0", + "stability": "preview", + "description": "What runs where, and what differs per environment: environments, builds, runtime processes, deployments and value bindings.", + "requires": { "metamodel": ">=1.3 <2", "packs": { "metaobjects-systems": ">=0.1.0 <0.2.0" } }, + "ports": { + "typescript": { "package": "@metaobjectsdev/pack-delivery", "core": ">=1.3.0 <2.0.0", "publish": false } + } +} diff --git a/packs/delivery/typescript/package.json b/packs/delivery/typescript/package.json new file mode 100644 index 0000000000..873bbda25e --- /dev/null +++ b/packs/delivery/typescript/package.json @@ -0,0 +1,23 @@ +{ + "name": "@metaobjectsdev/pack-delivery", + "version": "0.1.0", + "description": "The delivery pack: environments, builds, runtime, deployments and value bindings (ADR-0069 independent pack, preview).", + "private": true, + "type": "module", + "exports": { + ".": { "bun": "./src/index.ts", "default": "./src/index.ts" } + }, + "scripts": { + "typecheck": "tsc --noEmit -p ." + }, + "dependencies": { + "@metaobjectsdev/pack-systems": "workspace:*" + }, + "peerDependencies": { + "@metaobjectsdev/metadata": ">=1.3.0 <2.0.0" + }, + "devDependencies": { + "@metaobjectsdev/metadata": "workspace:*" + }, + "license": "Apache-2.0" +} diff --git a/packs/delivery/typescript/src/constants.ts b/packs/delivery/typescript/src/constants.ts new file mode 100644 index 0000000000..24aebb8c87 --- /dev/null +++ b/packs/delivery/typescript/src/constants.ts @@ -0,0 +1,38 @@ +// The delivery pack's vocabulary names, one constant each (metamodel/delivery.json is the +// definition; these are the spellings code compares against). + +export const DELIVERY_PROVIDER_ID = "metaobjects-delivery"; + +export const TYPE_ENVIRONMENT = "environment"; +export const ENVIRONMENT_SUBTYPE_STANDING = "standing"; +export const ENVIRONMENT_SUBTYPE_EPHEMERAL = "ephemeral"; +export const ENVIRONMENT_SUBTYPE_LOCAL = "local"; + +export const TYPE_BUILD = "build"; +export const BUILD_SUBTYPE_DOCKERFILE = "dockerfile"; +export const BUILD_SUBTYPE_COMMAND = "command"; +export const BUILD_SUBTYPE_STATIC = "static"; + +export const TYPE_RUNTIME = "runtime"; +export const RUNTIME_SUBTYPE_PROCESS = "process"; + +/** A platform pack registers the concrete `deployment.` subtype. */ +export const TYPE_DEPLOYMENT = "deployment"; +export const DEPLOYMENT_ATTR_APP = "app"; +export const DEPLOYMENT_ATTR_ENVIRONMENT = "environment"; +export const DEPLOYMENT_ATTR_DOMAINS = "domains"; +export const DEPLOYMENT_ATTR_REPLICAS = "replicas"; +export const DEPLOYMENT_ATTR_REGION = "region"; +export const DEPLOYMENT_ATTR_BRANCH = "branch"; +export const DEPLOYMENT_ATTR_SECRETS = "secrets"; + +export const TYPE_VALUE = "value"; +export const VALUE_SUBTYPE_LITERAL = "literal"; +export const VALUE_SUBTYPE_REF = "ref"; +export const VALUE_ATTR_SETTING = "setting"; +export const VALUE_ATTR_LITERAL = "literal"; +export const VALUE_ATTR_FROM = "from"; +export const VALUE_ATTR_OUTPUT = "output"; + +/** Raised when a deployment's @app / @environment or a value.ref's @from names nothing, or the wrong kind of node. */ +export const ERR_DELIVERY_UNRESOLVED_REF = "ERR_DELIVERY_UNRESOLVED_REF"; diff --git a/packs/delivery/typescript/src/index.ts b/packs/delivery/typescript/src/index.ts new file mode 100644 index 0000000000..c6442445cd --- /dev/null +++ b/packs/delivery/typescript/src/index.ts @@ -0,0 +1,86 @@ +// @metaobjectsdev/pack-delivery: what runs where, and what differs per environment +// (ADR-0069 independent pack). The vocabulary is metamodel/delivery.json; this module +// registers it and checks the references the data cannot: a deployment's @app and +// @environment, and a value.ref's @from. +// +// The pack registers `deployment.base` and no concrete deployment: the platform is the +// subtype, and a platform pack registers it (`deployment.`, `extendsBase: true`). +// Such a pack passes `validateDeployment` as that subtype's validator, so its +// deployments are held to the same references. + +import { + applyProviderDefinition, + type MetaDataTypeProvider, + type NodeValidator, + type ProviderDefinition, + type TypeRegistry, +} from "@metaobjectsdev/metadata"; +import { SYSTEMS_PROVIDER_ID, TYPE_APP, TYPE_RESOURCE, checkRefs, packTypeMaps } from "@metaobjectsdev/pack-systems"; +import definition from "../../metamodel/delivery.json" with { type: "json" }; +import { + DELIVERY_PROVIDER_ID, + DEPLOYMENT_ATTR_APP, + DEPLOYMENT_ATTR_ENVIRONMENT, + ERR_DELIVERY_UNRESOLVED_REF, + TYPE_BUILD, + TYPE_DEPLOYMENT, + TYPE_ENVIRONMENT, + TYPE_VALUE, + VALUE_ATTR_FROM, +} from "./constants.js"; + +export * from "./constants.js"; + +export const DELIVERY_DEFINITION = definition as ProviderDefinition; + +/** The validator a platform pack attaches to its `deployment.` subtype. */ +export const validateDeployment: NodeValidator = (node, ctx) => { + const error = ctx.error.bind(ctx); + checkRefs(node, DEPLOYMENT_ATTR_APP, [TYPE_APP], ERR_DELIVERY_UNRESOLVED_REF, error); + checkRefs(node, DEPLOYMENT_ATTR_ENVIRONMENT, [TYPE_ENVIRONMENT], ERR_DELIVERY_UNRESOLVED_REF, error); +}; + +// `deployment.base` is abstract, but the parser treats a `.base` as abstract only +// once a concrete sibling is registered, and none is until a platform pack is composed. +// Refuse it here too, with the parser's code, so it never loads either way. +const refuseAbstractDeployment: NodeValidator = (node, ctx) => { + ctx.error( + "ERR_ABSTRACT_SUBTYPE_AUTHORED", + node, + `deployment.base "${node.name}" is abstract: declare the platform's own subtype (deployment.), which a platform pack registers.`, + ); +}; + +// An app has at most one build. The registry enforces a child rule's upper bound only per +// subtype (`maxOccurs`), and a dockerfile beside a build command is two subtypes, so the +// bound across build.* is checked here, on every build after the first. +const validateBuild: NodeValidator = (node, ctx) => { + // ADR-0039: own: the bound is on what this app declares; an inherited build was counted where it was declared. + const builds = node.parent?.ownChildren().filter((c) => c.type === TYPE_BUILD) ?? []; + if (builds.length > 1 && builds[0] !== node) { + ctx.error( + "ERR_TOO_MANY_OCCURRENCES", + node, + `${node.type}.${node.subType} "${node.name}" is a second build of ${node.parent?.type}.${node.parent?.subType} "${node.parent?.name}": an app has at most one build.`, + ); + } +}; + +const validateValue: NodeValidator = (node, ctx) => { + checkRefs(node, VALUE_ATTR_FROM, [TYPE_RESOURCE, TYPE_APP], ERR_DELIVERY_UNRESOLVED_REF, ctx.error.bind(ctx)); +}; + +export const provider: MetaDataTypeProvider = { + id: DELIVERY_PROVIDER_ID, + dependencies: ["metaobjects-core-types", SYSTEMS_PROVIDER_ID], + description: + "Delivery: environment.*, build.*, runtime.process, deployment.base (a platform pack adds the concrete subtype) and value.literal / value.ref.", + registerTypes(registry: TypeRegistry): void { + const { factories, validators } = packTypeMaps(DELIVERY_DEFINITION, { + [TYPE_BUILD]: validateBuild, + [TYPE_VALUE]: validateValue, + [TYPE_DEPLOYMENT]: refuseAbstractDeployment, + }); + applyProviderDefinition(registry, DELIVERY_DEFINITION, factories, validators); + }, +}; diff --git a/packs/delivery/typescript/tsconfig.json b/packs/delivery/typescript/tsconfig.json new file mode 100644 index 0000000000..b33fae1d44 --- /dev/null +++ b/packs/delivery/typescript/tsconfig.json @@ -0,0 +1,7 @@ +{ + "extends": "../../../server/typescript/tsconfig.base.json", + "compilerOptions": { + "noEmit": true + }, + "include": ["src/**/*", "test/**/*"] +} diff --git a/packs/railway/CHANGELOG.md b/packs/railway/CHANGELOG.md new file mode 100644 index 0000000000..07c23ae5dd --- /dev/null +++ b/packs/railway/CHANGELOG.md @@ -0,0 +1,10 @@ +# Changelog: railway pack + +This pack's own changelog (ADR-0069). The root `CHANGELOG.md` records core only. + +## [0.1.0] - unreleased + +- **First version: vocabulary only.** `deployment.railway` (`@project`, `@service`) on the + delivery pack's `deployment.base`, held to its references. The worked example (an app's model + plus an estate repository's additions and overlay) is the `railway-worked-example` fixture. + TypeScript port. diff --git a/packs/railway/README.md b/packs/railway/README.md new file mode 100644 index 0000000000..e56ab311bb --- /dev/null +++ b/packs/railway/README.md @@ -0,0 +1,32 @@ +# railway pack + +`metaobjects-railway`, version 0.1.0, **preview**, unpublished. An independent pack +([ADR-0069](../../spec/decisions/ADR-0069-independent-packs.md)). Requires +[`delivery`](../delivery/README.md). + +Railway as a deployment platform. **Vocabulary only so far:** it registers `deployment.railway`, +so a model can place an app on Railway and be checked. The generators (the `.railway/railway.ts` +file, the list of facts that file cannot hold, the secret sync, deploy and doctor scripts) are +later work in this pack. + +## Vocabulary + +| Node | Attributes | +|---|---| +| `deployment.railway` | every `deployment.base` attribute (`@app`, `@environment`, `@domains`, `@replicas`, `@region`, `@branch`, `@secrets`, and `value.*` children), plus `@project` (required) and `@service` (absent: the app's name) | + +A `deployment.railway` is held to the delivery pack's references: `@app` must name an `app.*`, +`@environment` an `environment.*` (`ERR_DELIVERY_UNRESOLVED_REF`). + +## The worked example + +[`fixtures/conformance/railway-worked-example/`](fixtures/conformance/railway-worked-example/) is +an app's systems model (one service with its build and process facts, a database, a cache, an +outside bucket, two outside systems, a production deployment with a database URL by reference and a +literal profile) and an estate repository's additions (a staging environment and deployment it +declares itself, and an overlay that runs production on two replicas). Names, domains and project +names are placeholders. + +## Ports + +TypeScript only. diff --git a/packs/railway/fixtures/conformance/error-deployment-missing-app/expected-errors.json b/packs/railway/fixtures/conformance/error-deployment-missing-app/expected-errors.json new file mode 100644 index 0000000000..b411805ea3 --- /dev/null +++ b/packs/railway/fixtures/conformance/error-deployment-missing-app/expected-errors.json @@ -0,0 +1,15 @@ +{ + "errors": [ + { + "code": "ERR_MISSING_REQUIRED_ATTR", + "source": { + "format": "json", + "files": [ + "meta.shop.json" + ], + "jsonPath": "$['metadata.root'].children[2]['deployment.railway']" + } + } + ], + "warnings": [] +} diff --git a/packs/railway/fixtures/conformance/error-deployment-missing-app/input/meta.shop.json b/packs/railway/fixtures/conformance/error-deployment-missing-app/input/meta.shop.json new file mode 100644 index 0000000000..a3f246b3e1 --- /dev/null +++ b/packs/railway/fixtures/conformance/error-deployment-missing-app/input/meta.shop.json @@ -0,0 +1,48 @@ +{ + "metadata.root": { + "package": "acme::shop", + "children": [ + { + "system.internal": { + "name": "Shop", + "children": [ + { + "app.service": { + "name": "api", + "@needs": [ + "db" + ], + "@calls": [ + "Payments" + ] + } + }, + { + "resource.database": { + "name": "db", + "@engine": "postgres" + } + }, + { + "system.external": { + "name": "Payments" + } + } + ] + } + }, + { + "environment.standing": { + "name": "production" + } + }, + { + "deployment.railway": { + "name": "api-production", + "@environment": "production", + "@project": "shop" + } + } + ] + } +} diff --git a/packs/railway/fixtures/conformance/error-deployment-missing-app/providers.json b/packs/railway/fixtures/conformance/error-deployment-missing-app/providers.json new file mode 100644 index 0000000000..aac0d894bc --- /dev/null +++ b/packs/railway/fixtures/conformance/error-deployment-missing-app/providers.json @@ -0,0 +1 @@ +["metaobjects-core-types", "metaobjects-documentation", "metaobjects-systems", "metaobjects-delivery", "metaobjects-railway"] diff --git a/packs/railway/fixtures/conformance/error-deployment-refs/expected-errors.json b/packs/railway/fixtures/conformance/error-deployment-refs/expected-errors.json new file mode 100644 index 0000000000..d1470e3279 --- /dev/null +++ b/packs/railway/fixtures/conformance/error-deployment-refs/expected-errors.json @@ -0,0 +1,25 @@ +{ + "errors": [ + { + "code": "ERR_DELIVERY_UNRESOLVED_REF", + "source": { + "format": "json", + "files": [ + "meta.shop.json" + ], + "jsonPath": "$['metadata.root'].children[2]['deployment.railway']" + } + }, + { + "code": "ERR_DELIVERY_UNRESOLVED_REF", + "source": { + "format": "json", + "files": [ + "meta.shop.json" + ], + "jsonPath": "$['metadata.root'].children[2]['deployment.railway']" + } + } + ], + "warnings": [] +} diff --git a/packs/railway/fixtures/conformance/error-deployment-refs/input/meta.shop.json b/packs/railway/fixtures/conformance/error-deployment-refs/input/meta.shop.json new file mode 100644 index 0000000000..9a3292e5b1 --- /dev/null +++ b/packs/railway/fixtures/conformance/error-deployment-refs/input/meta.shop.json @@ -0,0 +1,49 @@ +{ + "metadata.root": { + "package": "acme::shop", + "children": [ + { + "system.internal": { + "name": "Shop", + "children": [ + { + "app.service": { + "name": "api", + "@needs": [ + "db" + ], + "@calls": [ + "Payments" + ] + } + }, + { + "resource.database": { + "name": "db", + "@engine": "postgres" + } + }, + { + "system.external": { + "name": "Payments" + } + } + ] + } + }, + { + "environment.standing": { + "name": "production" + } + }, + { + "deployment.railway": { + "name": "api-production", + "@app": "Shop.db", + "@environment": "prod", + "@project": "shop" + } + } + ] + } +} diff --git a/packs/railway/fixtures/conformance/error-deployment-refs/providers.json b/packs/railway/fixtures/conformance/error-deployment-refs/providers.json new file mode 100644 index 0000000000..aac0d894bc --- /dev/null +++ b/packs/railway/fixtures/conformance/error-deployment-refs/providers.json @@ -0,0 +1 @@ +["metaobjects-core-types", "metaobjects-documentation", "metaobjects-systems", "metaobjects-delivery", "metaobjects-railway"] diff --git a/packs/railway/fixtures/conformance/error-value-ref-unresolved/expected-errors.json b/packs/railway/fixtures/conformance/error-value-ref-unresolved/expected-errors.json new file mode 100644 index 0000000000..9916bf99e0 --- /dev/null +++ b/packs/railway/fixtures/conformance/error-value-ref-unresolved/expected-errors.json @@ -0,0 +1,25 @@ +{ + "errors": [ + { + "code": "ERR_DELIVERY_UNRESOLVED_REF", + "source": { + "format": "json", + "files": [ + "meta.shop.json" + ], + "jsonPath": "$['metadata.root'].children[2]['deployment.railway'].children[0]['value.ref']" + } + }, + { + "code": "ERR_DELIVERY_UNRESOLVED_REF", + "source": { + "format": "json", + "files": [ + "meta.shop.json" + ], + "jsonPath": "$['metadata.root'].children[2]['deployment.railway'].children[1]['value.ref']" + } + } + ], + "warnings": [] +} diff --git a/packs/railway/fixtures/conformance/error-value-ref-unresolved/input/meta.shop.json b/packs/railway/fixtures/conformance/error-value-ref-unresolved/input/meta.shop.json new file mode 100644 index 0000000000..56e7b012bf --- /dev/null +++ b/packs/railway/fixtures/conformance/error-value-ref-unresolved/input/meta.shop.json @@ -0,0 +1,67 @@ +{ + "metadata.root": { + "package": "acme::shop", + "children": [ + { + "system.internal": { + "name": "Shop", + "children": [ + { + "app.service": { + "name": "api", + "@needs": [ + "db" + ], + "@calls": [ + "Payments" + ] + } + }, + { + "resource.database": { + "name": "db", + "@engine": "postgres" + } + }, + { + "system.external": { + "name": "Payments" + } + } + ] + } + }, + { + "environment.standing": { + "name": "production" + } + }, + { + "deployment.railway": { + "name": "api-production", + "@app": "Shop.api", + "@environment": "production", + "@project": "shop", + "children": [ + { + "value.ref": { + "name": "dbUrl", + "@setting": "Database.url", + "@from": "Shop.database", + "@output": "url" + } + }, + { + "value.ref": { + "name": "payments", + "@setting": "Payments.url", + "@from": "Shop.Payments", + "@output": "url" + } + } + ] + } + } + ] + } +} diff --git a/packs/railway/fixtures/conformance/error-value-ref-unresolved/providers.json b/packs/railway/fixtures/conformance/error-value-ref-unresolved/providers.json new file mode 100644 index 0000000000..aac0d894bc --- /dev/null +++ b/packs/railway/fixtures/conformance/error-value-ref-unresolved/providers.json @@ -0,0 +1 @@ +["metaobjects-core-types", "metaobjects-documentation", "metaobjects-systems", "metaobjects-delivery", "metaobjects-railway"] diff --git a/packs/railway/fixtures/conformance/railway-worked-example/expected.json b/packs/railway/fixtures/conformance/railway-worked-example/expected.json new file mode 100644 index 0000000000..e63b42f7c1 --- /dev/null +++ b/packs/railway/fixtures/conformance/railway-worked-example/expected.json @@ -0,0 +1,152 @@ +{ + "metadata.root": { + "children": [ + { + "system.internal": { + "name": "Game", + "package": "acme::game", + "@description": "A multiplayer game: one Spring service, its database, cache and image bucket.", + "children": [ + { + "app.service": { + "name": "server", + "@calls": [ + "Chat", + "Payments" + ], + "@needs": [ + "db", + "cache", + "images" + ], + "@settings": [ + "Database", + "Redis", + "Llm", + "Chat", + "Payments" + ], + "children": [ + { + "build.dockerfile": { + "name": "image", + "@path": "Dockerfile", + "@watchExclude": [ + "docs/**", + "e2e-tests/**", + "scripts/**", + "assets/**", + "*.md" + ] + } + }, + { + "runtime.process": { + "name": "web", + "@drainSeconds": 180, + "@healthPath": "/actuator/health/container", + "@healthTimeoutSeconds": 300, + "@port": 8080, + "@restart": "onFailure" + } + } + ] + } + }, + { + "resource.database": { + "name": "db", + "@engine": "postgres", + "@version": "18" + } + }, + { + "resource.cache": { + "name": "cache", + "@engine": "redis" + } + }, + { + "resource.bucket": { + "name": "images", + "@engine": "r2", + "@managedBy": "external" + } + }, + { + "system.external": { + "name": "Chat" + } + }, + { + "system.external": { + "name": "Payments" + } + } + ] + } + }, + { + "environment.standing": { + "name": "production", + "package": "acme::game" + } + }, + { + "deployment.railway": { + "name": "server-production", + "package": "acme::game", + "@app": "Game.server", + "@branch": "main", + "@domains": [ + "app.example.com", + "play.example.com" + ], + "@environment": "production", + "@notes": "The estate overrides what differs: two replicas in production.", + "@project": "game", + "@replicas": 2, + "@secrets": "keys", + "@service": "game", + "children": [ + { + "value.ref": { + "name": "dbUrl", + "@from": "Game.db", + "@output": "url", + "@setting": "Database.url" + } + }, + { + "value.literal": { + "name": "profile", + "@literal": "prod", + "@setting": "Spring.profile" + } + } + ] + } + }, + { + "environment.standing": { + "name": "staging", + "package": "acme::estate" + } + }, + { + "deployment.railway": { + "name": "server-staging", + "package": "acme::estate", + "@app": "acme::game::Game.server", + "@description": "A deployment the app's own model never declared.", + "@domains": [ + "staging.example.com" + ], + "@environment": "staging", + "@project": "game", + "@service": "game" + } + } + ] + } +} diff --git a/packs/railway/fixtures/conformance/railway-worked-example/input/meta.app-system.json b/packs/railway/fixtures/conformance/railway-worked-example/input/meta.app-system.json new file mode 100644 index 0000000000..e9318f35f4 --- /dev/null +++ b/packs/railway/fixtures/conformance/railway-worked-example/input/meta.app-system.json @@ -0,0 +1,54 @@ +{ + "metadata.root": { + "package": "acme::game", + "children": [ + { "system.internal": { + "name": "Game", + "@description": "A multiplayer game: one Spring service, its database, cache and image bucket.", + "children": [ + { "app.service": { + "name": "server", + "@needs": ["db", "cache", "images"], + "@calls": ["Chat", "Payments"], + "@settings": ["Database", "Redis", "Llm", "Chat", "Payments"], + "children": [ + { "build.dockerfile": { + "name": "image", + "@path": "Dockerfile", + "@watchExclude": ["docs/**", "e2e-tests/**", "scripts/**", "assets/**", "*.md"] + } }, + { "runtime.process": { + "name": "web", + "@port": 8080, + "@healthPath": "/actuator/health/container", + "@healthTimeoutSeconds": 300, + "@drainSeconds": 180, + "@restart": "onFailure" + } } + ] + } }, + { "resource.database": { "name": "db", "@engine": "postgres", "@version": "18" } }, + { "resource.cache": { "name": "cache", "@engine": "redis" } }, + { "resource.bucket": { "name": "images", "@engine": "r2", "@managedBy": "external" } }, + { "system.external": { "name": "Chat" } }, + { "system.external": { "name": "Payments" } } + ] + } }, + { "environment.standing": { "name": "production" } }, + { "deployment.railway": { + "name": "server-production", + "@app": "Game.server", + "@environment": "production", + "@project": "game", + "@service": "game", + "@branch": "main", + "@domains": ["app.example.com", "play.example.com"], + "@secrets": "keys", + "children": [ + { "value.ref": { "name": "dbUrl", "@setting": "Database.url", "@from": "Game.db", "@output": "url" } }, + { "value.literal": { "name": "profile", "@setting": "Spring.profile", "@literal": "prod" } } + ] + } } + ] + } +} diff --git a/packs/railway/fixtures/conformance/railway-worked-example/input/meta.estate-overrides.json b/packs/railway/fixtures/conformance/railway-worked-example/input/meta.estate-overrides.json new file mode 100644 index 0000000000..c620400dfb --- /dev/null +++ b/packs/railway/fixtures/conformance/railway-worked-example/input/meta.estate-overrides.json @@ -0,0 +1,13 @@ +{ + "metadata.root": { + "package": "acme::game", + "children": [ + { "deployment.railway": { + "name": "server-production", + "overlay": true, + "@notes": "The estate overrides what differs: two replicas in production.", + "@replicas": 2 + } } + ] + } +} diff --git a/packs/railway/fixtures/conformance/railway-worked-example/input/meta.estate.json b/packs/railway/fixtures/conformance/railway-worked-example/input/meta.estate.json new file mode 100644 index 0000000000..fe1635d38c --- /dev/null +++ b/packs/railway/fixtures/conformance/railway-worked-example/input/meta.estate.json @@ -0,0 +1,17 @@ +{ + "metadata.root": { + "package": "acme::estate", + "children": [ + { "environment.standing": { "name": "staging" } }, + { "deployment.railway": { + "name": "server-staging", + "@description": "A deployment the app's own model never declared.", + "@app": "acme::game::Game.server", + "@environment": "staging", + "@project": "game", + "@service": "game", + "@domains": ["staging.example.com"] + } } + ] + } +} diff --git a/packs/railway/fixtures/conformance/railway-worked-example/providers.json b/packs/railway/fixtures/conformance/railway-worked-example/providers.json new file mode 100644 index 0000000000..aac0d894bc --- /dev/null +++ b/packs/railway/fixtures/conformance/railway-worked-example/providers.json @@ -0,0 +1 @@ +["metaobjects-core-types", "metaobjects-documentation", "metaobjects-systems", "metaobjects-delivery", "metaobjects-railway"] diff --git a/packs/railway/fixtures/expected-vocabulary.json b/packs/railway/fixtures/expected-vocabulary.json new file mode 100644 index 0000000000..3427e092ba --- /dev/null +++ b/packs/railway/fixtures/expected-vocabulary.json @@ -0,0 +1,91 @@ +{ + "pack": "metaobjects-railway", + "types": [ + { + "type": "deployment", + "subType": "railway", + "description": "An app deployed as one Railway service in one Railway environment. Takes every deployment.base attribute (@app, @environment, @domains, @replicas, @region, @branch, @secrets, and value.* children) and adds where it lives on Railway.", + "whenToUse": "Each (app, environment) pair that runs on Railway.", + "attrs": [ + { + "name": "app", + "valueType": "string", + "isArray": false, + "required": true, + "description": "The app deployed: `System.app` (`Shop.web`), or `pkg::System.app` from another package. It must resolve to an app.* node (ERR_DELIVERY_UNRESOLVED_REF)." + }, + { + "name": "branch", + "valueType": "string", + "isArray": false, + "required": false, + "description": "The repository branch whose pushes deploy here." + }, + { + "name": "domains", + "valueType": "string", + "isArray": true, + "required": false, + "description": "The public host names that route to the app here." + }, + { + "name": "environment", + "valueType": "string", + "isArray": false, + "required": true, + "description": "The environment.* node it deploys to, by name (or `pkg::name`). It must resolve (ERR_DELIVERY_UNRESOLVED_REF)." + }, + { + "name": "project", + "valueType": "string", + "isArray": false, + "required": true, + "description": "The Railway project's name." + }, + { + "name": "region", + "valueType": "string", + "isArray": false, + "required": false, + "description": "The platform's region name." + }, + { + "name": "replicas", + "valueType": "int", + "isArray": false, + "required": false, + "description": "How many copies run. Absent means the platform's default." + }, + { + "name": "secrets", + "valueType": "string", + "isArray": false, + "required": false, + "description": "The vault the deployment's secret settings come from, by name (a vault.* of the config pack). Recorded by name only: this pack does not require the config pack." + }, + { + "name": "service", + "valueType": "string", + "isArray": false, + "required": false, + "description": "The Railway service's name. Absent means the app's name." + } + ], + "children": [ + { + "childType": "value", + "childSubType": [ + "literal", + "ref" + ], + "childName": "*", + "min": 0, + "max": null + } + ] + } + ], + "extends": [], + "commonAttrs": [], + "defaultSubTypes": {} +} diff --git a/packs/railway/metamodel/railway.json b/packs/railway/metamodel/railway.json new file mode 100644 index 0000000000..715712f81d --- /dev/null +++ b/packs/railway/metamodel/railway.json @@ -0,0 +1,16 @@ +{ + "provider": "metaobjects-railway", + "types": [ + { + "type": "deployment", + "subType": "railway", + "extendsBase": true, + "description": "An app deployed as one Railway service in one Railway environment. Takes every deployment.base attribute (@app, @environment, @domains, @replicas, @region, @branch, @secrets, and value.* children) and adds where it lives on Railway.", + "whenToUse": "Each (app, environment) pair that runs on Railway.", + "children": [ + { "type": "attr", "subType": "string", "name": "project", "min": 1, "max": 1, "description": "The Railway project's name." }, + { "type": "attr", "subType": "string", "name": "service", "min": 0, "max": 1, "description": "The Railway service's name. Absent means the app's name." } + ] + } + ] +} diff --git a/packs/railway/pack.json b/packs/railway/pack.json new file mode 100644 index 0000000000..ae41bba084 --- /dev/null +++ b/packs/railway/pack.json @@ -0,0 +1,10 @@ +{ + "id": "metaobjects-railway", + "version": "0.1.0", + "stability": "preview", + "description": "Railway as a deployment platform: deployment.railway. Vocabulary only so far; the emitter comes later.", + "requires": { "metamodel": ">=1.3 <2", "packs": { "metaobjects-delivery": ">=0.1.0 <0.2.0" } }, + "ports": { + "typescript": { "package": "@metaobjectsdev/pack-railway", "core": ">=1.3.0 <2.0.0", "publish": false } + } +} diff --git a/packs/railway/typescript/package.json b/packs/railway/typescript/package.json new file mode 100644 index 0000000000..cfe321dc23 --- /dev/null +++ b/packs/railway/typescript/package.json @@ -0,0 +1,27 @@ +{ + "name": "@metaobjectsdev/pack-railway", + "version": "0.1.0", + "description": "The railway pack: deployment.railway (ADR-0069 independent pack, preview).", + "private": true, + "type": "module", + "exports": { + ".": { + "bun": "./src/index.ts", + "default": "./src/index.ts" + } + }, + "scripts": { + "typecheck": "tsc --noEmit -p ." + }, + "dependencies": { + "@metaobjectsdev/pack-delivery": "workspace:*", + "@metaobjectsdev/pack-systems": "workspace:*" + }, + "peerDependencies": { + "@metaobjectsdev/metadata": ">=1.3.0 <2.0.0" + }, + "devDependencies": { + "@metaobjectsdev/metadata": "workspace:*" + }, + "license": "Apache-2.0" +} diff --git a/packs/railway/typescript/src/index.ts b/packs/railway/typescript/src/index.ts new file mode 100644 index 0000000000..3d270fdd86 --- /dev/null +++ b/packs/railway/typescript/src/index.ts @@ -0,0 +1,26 @@ +// @metaobjectsdev/pack-railway: Railway as a deployment platform (ADR-0069 independent +// pack). It registers `deployment.railway` on the delivery pack's `deployment.base`, and +// holds it to the delivery pack's deployment references. Vocabulary only so far: the +// `.railway/railway.ts` emitter, the secret sync and the deploy scripts come later. + +import { applyProviderDefinition, type MetaDataTypeProvider, type ProviderDefinition, type TypeRegistry } from "@metaobjectsdev/metadata"; +import { DELIVERY_PROVIDER_ID, TYPE_DEPLOYMENT, validateDeployment } from "@metaobjectsdev/pack-delivery"; +import { packTypeMaps } from "@metaobjectsdev/pack-systems"; +import definition from "../../metamodel/railway.json" with { type: "json" }; + +export const RAILWAY_PROVIDER_ID = "metaobjects-railway"; +export const DEPLOYMENT_SUBTYPE_RAILWAY = "railway"; +export const RAILWAY_ATTR_PROJECT = "project"; +export const RAILWAY_ATTR_SERVICE = "service"; + +export const RAILWAY_DEFINITION = definition as ProviderDefinition; + +export const provider: MetaDataTypeProvider = { + id: RAILWAY_PROVIDER_ID, + dependencies: ["metaobjects-core-types", DELIVERY_PROVIDER_ID], + description: "Railway: deployment.railway.", + registerTypes(registry: TypeRegistry): void { + const { factories, validators } = packTypeMaps(RAILWAY_DEFINITION, { [TYPE_DEPLOYMENT]: validateDeployment }); + applyProviderDefinition(registry, RAILWAY_DEFINITION, factories, validators); + }, +}; diff --git a/packs/railway/typescript/tsconfig.json b/packs/railway/typescript/tsconfig.json new file mode 100644 index 0000000000..b33fae1d44 --- /dev/null +++ b/packs/railway/typescript/tsconfig.json @@ -0,0 +1,7 @@ +{ + "extends": "../../../server/typescript/tsconfig.base.json", + "compilerOptions": { + "noEmit": true + }, + "include": ["src/**/*", "test/**/*"] +} diff --git a/packs/systems/CHANGELOG.md b/packs/systems/CHANGELOG.md new file mode 100644 index 0000000000..5f1859a39e --- /dev/null +++ b/packs/systems/CHANGELOG.md @@ -0,0 +1,10 @@ +# Changelog: systems pack + +This pack's own changelog (ADR-0069). The root `CHANGELOG.md` records core only. + +## [0.1.0] - unreleased + +- **First version.** `system.internal`, `system.external`; `app.service`, `app.client`, + `app.job` (replacing the earlier design's `container.*`); `resource.database`, `.cache`, + `.bucket`, `.keyvalue`, `.queue`, `.volume`. An app's `@needs` and `@calls` are resolved at + load (`ERR_SYSTEMS_UNRESOLVED_REF`); `@settings` and `@shell` are names only. TypeScript port. diff --git a/packs/systems/README.md b/packs/systems/README.md new file mode 100644 index 0000000000..676f62d835 --- /dev/null +++ b/packs/systems/README.md @@ -0,0 +1,69 @@ +# systems pack + +`metaobjects-systems`, version 0.1.0, **preview**, unpublished. An independent pack +([ADR-0069](../../spec/decisions/ADR-0069-independent-packs.md)). + +It answers one question: **what exists, and what talks to what?** A system, the apps it is made +of, the resources they use and the outside systems they call. It holds no deploy facts: where an +app runs and what differs per environment is the [`delivery`](../delivery/README.md) pack's. A +documentation explorer can use this pack alone. + +## Vocabulary + +| Node | What it is | Attributes | +|---|---|---| +| `system.internal` | A system this team builds and runs. Holds `app.*`, `resource.*` and `system.external` | (documentation only) | +| `system.external` | A system someone else runs that an app calls. Inside a system, or at the document root when several share it | (documentation only) | +| `app.service` | A long-running process that answers requests | `@needs`, `@calls`, `@settings` | +| `app.client` | Code that runs on the user's device | the same, plus `@shell` | +| `app.job` | Work that runs to completion | the same, plus `@schedule` (cron, UTC) | +| `resource.database`, `.cache`, `.bucket`, `.keyvalue`, `.queue`, `.volume` | Something an app uses | `@engine`, `@version`, `@managedBy` (`platform` or `external`) | + +`app.base`, `resource.base` and `system.base` are abstract anchors and fail to load if authored. +The full definition, with every attribute's description, is [`metamodel/systems.json`](metamodel/systems.json). + +## References + +Apps and resources are children of a system, so they are addressed the way the loader addresses +any nested node (ADR-0059): `System.child` (`Shop.db`), or `pkg::System.child` from another +package. A bare name means a node of the referrer's own system, then a top-level node of that name +in the referrer's package. + +- `@needs` must name `resource.*` nodes, and `@calls` must name `app.*` or `system.external` nodes. + A name that resolves to nothing, or to the wrong kind of node, fails the load with + `ERR_SYSTEMS_UNRESOLVED_REF`. A rename therefore breaks the build rather than going stale. +- `@settings` (an `object.settings` of the config pack) and `@shell` (a UI pack's `nav.shell`) are + recorded by name only. This pack requires neither pack, so it does not resolve them. + +## Example + +```json +{ "metadata.root": { "package": "acme::shop", "children": [ + { "system.internal": { "name": "Shop", "children": [ + { "app.service": { "name": "api", "@needs": ["db"], "@calls": ["Payments"], "@settings": ["Database"] } }, + { "resource.database": { "name": "db", "@engine": "postgres", "@version": "18" } }, + { "system.external": { "name": "Payments" } } + ] } } +] } } +``` + +## Use it + +TypeScript: compose `provider` from `@metaobjectsdev/pack-systems` after the default bundle +(`composeRegistry([...coreProviders, provider])`), or name it in a project's provider list as any +provider of your own ([extending-with-providers](../../docs/features/extending-with-providers.md)). + +## Ports + +TypeScript only. In every other port the vocabulary is unknown, and a model that uses it fails +there by name (`ERR_UNKNOWN_TYPE`), per ADR-0069's port rule. Keep a systems model in a metadata +collection of its own, read by the Node `meta`, when the application's own port loads its +entities. + +## Checks + +``` +bun scripts/pack-vocabulary.ts systems # the pinned vocabulary, and that the pack leaves core alone +bun scripts/pack-conformance.ts systems # fixtures/conformance/ through the TypeScript port +scripts/ci-local.sh --only packs +``` diff --git a/packs/systems/fixtures/conformance/ERROR-CODES.json b/packs/systems/fixtures/conformance/ERROR-CODES.json new file mode 100644 index 0000000000..1c3fcf9ef4 --- /dev/null +++ b/packs/systems/fixtures/conformance/ERROR-CODES.json @@ -0,0 +1,6 @@ +{ + "version": 1, + "codes": { + "ERR_SYSTEMS_UNRESOLVED_REF": "An app's @needs or @calls entry names no node, or names a node of the wrong kind (@needs takes a resource.*, @calls an app.* or a system.external)." + } +} diff --git a/packs/systems/fixtures/conformance/error-app-outside-system/expected-errors.json b/packs/systems/fixtures/conformance/error-app-outside-system/expected-errors.json new file mode 100644 index 0000000000..8023121d5e --- /dev/null +++ b/packs/systems/fixtures/conformance/error-app-outside-system/expected-errors.json @@ -0,0 +1,15 @@ +{ + "errors": [ + { + "code": "ERR_CHILD_NOT_ALLOWED", + "source": { + "format": "json", + "files": [ + "meta.shop.json" + ], + "jsonPath": "$['metadata.root']" + } + } + ], + "warnings": [] +} diff --git a/packs/systems/fixtures/conformance/error-app-outside-system/input/meta.shop.json b/packs/systems/fixtures/conformance/error-app-outside-system/input/meta.shop.json new file mode 100644 index 0000000000..f471be7a25 --- /dev/null +++ b/packs/systems/fixtures/conformance/error-app-outside-system/input/meta.shop.json @@ -0,0 +1,12 @@ +{ + "metadata.root": { + "package": "acme::shop", + "children": [ + { + "app.service": { + "name": "api" + } + } + ] + } +} diff --git a/packs/systems/fixtures/conformance/error-app-outside-system/providers.json b/packs/systems/fixtures/conformance/error-app-outside-system/providers.json new file mode 100644 index 0000000000..8a10a47fbc --- /dev/null +++ b/packs/systems/fixtures/conformance/error-app-outside-system/providers.json @@ -0,0 +1 @@ +["metaobjects-core-types", "metaobjects-documentation", "metaobjects-systems"] diff --git a/packs/systems/fixtures/conformance/error-calls-wrong-kind/expected-errors.json b/packs/systems/fixtures/conformance/error-calls-wrong-kind/expected-errors.json new file mode 100644 index 0000000000..3b5dfc300a --- /dev/null +++ b/packs/systems/fixtures/conformance/error-calls-wrong-kind/expected-errors.json @@ -0,0 +1,15 @@ +{ + "errors": [ + { + "code": "ERR_SYSTEMS_UNRESOLVED_REF", + "source": { + "format": "json", + "files": [ + "meta.shop.json" + ], + "jsonPath": "$['metadata.root'].children[0]['system.internal'].children[0]['app.service']" + } + } + ], + "warnings": [] +} diff --git a/packs/systems/fixtures/conformance/error-calls-wrong-kind/input/meta.shop.json b/packs/systems/fixtures/conformance/error-calls-wrong-kind/input/meta.shop.json new file mode 100644 index 0000000000..70b03361a9 --- /dev/null +++ b/packs/systems/fixtures/conformance/error-calls-wrong-kind/input/meta.shop.json @@ -0,0 +1,27 @@ +{ + "metadata.root": { + "package": "acme::shop", + "children": [ + { + "system.internal": { + "name": "Shop", + "children": [ + { + "app.service": { + "name": "api", + "@calls": [ + "db" + ] + } + }, + { + "resource.database": { + "name": "db" + } + } + ] + } + } + ] + } +} diff --git a/packs/systems/fixtures/conformance/error-calls-wrong-kind/providers.json b/packs/systems/fixtures/conformance/error-calls-wrong-kind/providers.json new file mode 100644 index 0000000000..8a10a47fbc --- /dev/null +++ b/packs/systems/fixtures/conformance/error-calls-wrong-kind/providers.json @@ -0,0 +1 @@ +["metaobjects-core-types", "metaobjects-documentation", "metaobjects-systems"] diff --git a/packs/systems/fixtures/conformance/error-needs-unresolved/expected-errors.json b/packs/systems/fixtures/conformance/error-needs-unresolved/expected-errors.json new file mode 100644 index 0000000000..3b5dfc300a --- /dev/null +++ b/packs/systems/fixtures/conformance/error-needs-unresolved/expected-errors.json @@ -0,0 +1,15 @@ +{ + "errors": [ + { + "code": "ERR_SYSTEMS_UNRESOLVED_REF", + "source": { + "format": "json", + "files": [ + "meta.shop.json" + ], + "jsonPath": "$['metadata.root'].children[0]['system.internal'].children[0]['app.service']" + } + } + ], + "warnings": [] +} diff --git a/packs/systems/fixtures/conformance/error-needs-unresolved/input/meta.shop.json b/packs/systems/fixtures/conformance/error-needs-unresolved/input/meta.shop.json new file mode 100644 index 0000000000..60713d570e --- /dev/null +++ b/packs/systems/fixtures/conformance/error-needs-unresolved/input/meta.shop.json @@ -0,0 +1,28 @@ +{ + "metadata.root": { + "package": "acme::shop", + "children": [ + { + "system.internal": { + "name": "Shop", + "children": [ + { + "app.service": { + "name": "api", + "@needs": [ + "db", + "dbb" + ] + } + }, + { + "resource.database": { + "name": "db" + } + } + ] + } + } + ] + } +} diff --git a/packs/systems/fixtures/conformance/error-needs-unresolved/providers.json b/packs/systems/fixtures/conformance/error-needs-unresolved/providers.json new file mode 100644 index 0000000000..8a10a47fbc --- /dev/null +++ b/packs/systems/fixtures/conformance/error-needs-unresolved/providers.json @@ -0,0 +1 @@ +["metaobjects-core-types", "metaobjects-documentation", "metaobjects-systems"] diff --git a/packs/systems/fixtures/conformance/systems-apps-and-resources/expected.json b/packs/systems/fixtures/conformance/systems-apps-and-resources/expected.json new file mode 100644 index 0000000000..25b0489bf0 --- /dev/null +++ b/packs/systems/fixtures/conformance/systems-apps-and-resources/expected.json @@ -0,0 +1,123 @@ +{ + "metadata.root": { + "package": "acme::shop", + "children": [ + { + "system.internal": { + "name": "Shop", + "@description": "An online shop: a storefront, its API and a nightly job.", + "children": [ + { + "app.client": { + "name": "storefront", + "@calls": [ + "api" + ], + "@settings": [ + "Web" + ], + "@shell": "Storefront" + } + }, + { + "app.service": { + "name": "api", + "@calls": [ + "Payments", + "Mail", + "Billing.ledger" + ], + "@needs": [ + "db", + "sessions", + "uploads", + "jobs" + ], + "@settings": [ + "Database", + "Payments" + ] + } + }, + { + "app.job": { + "name": "nightlyExport", + "@needs": [ + "db", + "Shop.exports" + ], + "@schedule": "0 3 * * *" + } + }, + { + "resource.database": { + "name": "db", + "@engine": "postgres", + "@version": "18" + } + }, + { + "resource.cache": { + "name": "sessions", + "@engine": "redis" + } + }, + { + "resource.bucket": { + "name": "uploads", + "@managedBy": "external" + } + }, + { + "resource.keyvalue": { + "name": "flags" + } + }, + { + "resource.queue": { + "name": "jobs" + } + }, + { + "resource.volume": { + "name": "exports" + } + }, + { + "system.external": { + "name": "Payments" + } + } + ] + } + }, + { + "system.internal": { + "name": "Billing", + "children": [ + { + "app.service": { + "name": "ledger", + "@needs": [ + "ledgerDb" + ] + } + }, + { + "resource.database": { + "name": "ledgerDb", + "@engine": "postgres" + } + } + ] + } + }, + { + "system.external": { + "name": "Mail", + "@description": "An outside mail provider several systems share." + } + } + ] + } +} diff --git a/packs/systems/fixtures/conformance/systems-apps-and-resources/input/meta.shop.json b/packs/systems/fixtures/conformance/systems-apps-and-resources/input/meta.shop.json new file mode 100644 index 0000000000..d3defc6b9f --- /dev/null +++ b/packs/systems/fixtures/conformance/systems-apps-and-resources/input/meta.shop.json @@ -0,0 +1,123 @@ +{ + "metadata.root": { + "package": "acme::shop", + "children": [ + { + "system.internal": { + "name": "Shop", + "@description": "An online shop: a storefront, its API and a nightly job.", + "children": [ + { + "app.client": { + "name": "storefront", + "@calls": [ + "api" + ], + "@shell": "Storefront", + "@settings": [ + "Web" + ] + } + }, + { + "app.service": { + "name": "api", + "@needs": [ + "db", + "sessions", + "uploads", + "jobs" + ], + "@calls": [ + "Payments", + "Mail", + "Billing.ledger" + ], + "@settings": [ + "Database", + "Payments" + ] + } + }, + { + "app.job": { + "name": "nightlyExport", + "@schedule": "0 3 * * *", + "@needs": [ + "db", + "Shop.exports" + ] + } + }, + { + "resource.database": { + "name": "db", + "@engine": "postgres", + "@version": "18" + } + }, + { + "resource.cache": { + "name": "sessions", + "@engine": "redis" + } + }, + { + "resource.bucket": { + "name": "uploads", + "@managedBy": "external" + } + }, + { + "resource.keyvalue": { + "name": "flags" + } + }, + { + "resource.queue": { + "name": "jobs" + } + }, + { + "resource.volume": { + "name": "exports" + } + }, + { + "system.external": { + "name": "Payments" + } + } + ] + } + }, + { + "system.internal": { + "name": "Billing", + "children": [ + { + "app.service": { + "name": "ledger", + "@needs": [ + "ledgerDb" + ] + } + }, + { + "resource.database": { + "name": "ledgerDb", + "@engine": "postgres" + } + } + ] + } + }, + { + "system.external": { + "name": "Mail", + "@description": "An outside mail provider several systems share." + } + } + ] + } +} diff --git a/packs/systems/fixtures/conformance/systems-apps-and-resources/providers.json b/packs/systems/fixtures/conformance/systems-apps-and-resources/providers.json new file mode 100644 index 0000000000..8a10a47fbc --- /dev/null +++ b/packs/systems/fixtures/conformance/systems-apps-and-resources/providers.json @@ -0,0 +1 @@ +["metaobjects-core-types", "metaobjects-documentation", "metaobjects-systems"] diff --git a/packs/systems/fixtures/expected-vocabulary.json b/packs/systems/fixtures/expected-vocabulary.json new file mode 100644 index 0000000000..50130337fb --- /dev/null +++ b/packs/systems/fixtures/expected-vocabulary.json @@ -0,0 +1,441 @@ +{ + "pack": "metaobjects-systems", + "types": [ + { + "type": "app", + "subType": "base", + "description": "Abstract app base: the attributes every app.* shares. Not authored directly: an `app.base` node fails to load (ERR_ABSTRACT_SUBTYPE_AUTHORED).", + "attrs": [ + { + "name": "calls", + "valueType": "string", + "isArray": true, + "required": false, + "description": "The apps and outside systems this app calls. Each entry names an app.* or system.external node: a bare name is one in the same system (an outside system may also be declared at the document root); `System.child` names one in another system. A name that resolves to nothing, or to another kind of node, fails the load (ERR_SYSTEMS_UNRESOLVED_REF). Who may reach whom is derived from this list, so it is the list to keep current." + }, + { + "name": "needs", + "valueType": "string", + "isArray": true, + "required": false, + "description": "The resources this app uses. Each entry names a resource.* node: a bare name is a resource of the same system; `System.resource` (or `pkg::System.resource`) names one in another. A name that resolves to nothing, or to a node that is not a resource, fails the load (ERR_SYSTEMS_UNRESOLVED_REF)." + }, + { + "name": "settings", + "valueType": "string", + "isArray": true, + "required": false, + "description": "The settings groups this app reads, by name (an object.settings of the config pack). Recorded by name only: this pack does not require the config pack, so the name is not resolved here." + } + ], + "children": [] + }, + { + "type": "app", + "subType": "client", + "description": "Code that runs on the user's device: a single-page web app, a static site, a mobile app. It calls services; it serves nothing itself.", + "whenToUse": "A front end built and shipped as files, whatever serves them.", + "attrs": [ + { + "name": "calls", + "valueType": "string", + "isArray": true, + "required": false, + "description": "The apps and outside systems this app calls. Each entry names an app.* or system.external node: a bare name is one in the same system (an outside system may also be declared at the document root); `System.child` names one in another system. A name that resolves to nothing, or to another kind of node, fails the load (ERR_SYSTEMS_UNRESOLVED_REF). Who may reach whom is derived from this list, so it is the list to keep current." + }, + { + "name": "needs", + "valueType": "string", + "isArray": true, + "required": false, + "description": "The resources this app uses. Each entry names a resource.* node: a bare name is a resource of the same system; `System.resource` (or `pkg::System.resource`) names one in another. A name that resolves to nothing, or to a node that is not a resource, fails the load (ERR_SYSTEMS_UNRESOLVED_REF)." + }, + { + "name": "settings", + "valueType": "string", + "isArray": true, + "required": false, + "description": "The settings groups this app reads, by name (an object.settings of the config pack). Recorded by name only: this pack does not require the config pack, so the name is not resolved here." + }, + { + "name": "shell", + "valueType": "string", + "isArray": false, + "required": false, + "description": "The UI frame (a nav.shell of a UI pack) this client presents, by name. Recorded by name only: no UI pack is required, so the name is not resolved here." + } + ], + "children": [] + }, + { + "type": "app", + "subType": "job", + "description": "Work that runs to completion and exits: a scheduled task, a migration, a one-off batch. It may run on a schedule (@schedule) or when something triggers it.", + "whenToUse": "A cron job, a nightly import, a queue drain that is not a long-running service.", + "attrs": [ + { + "name": "calls", + "valueType": "string", + "isArray": true, + "required": false, + "description": "The apps and outside systems this app calls. Each entry names an app.* or system.external node: a bare name is one in the same system (an outside system may also be declared at the document root); `System.child` names one in another system. A name that resolves to nothing, or to another kind of node, fails the load (ERR_SYSTEMS_UNRESOLVED_REF). Who may reach whom is derived from this list, so it is the list to keep current." + }, + { + "name": "needs", + "valueType": "string", + "isArray": true, + "required": false, + "description": "The resources this app uses. Each entry names a resource.* node: a bare name is a resource of the same system; `System.resource` (or `pkg::System.resource`) names one in another. A name that resolves to nothing, or to a node that is not a resource, fails the load (ERR_SYSTEMS_UNRESOLVED_REF)." + }, + { + "name": "schedule", + "valueType": "string", + "isArray": false, + "required": false, + "description": "When the job runs, as a five-field cron expression in UTC (`0 3 * * *`). Absent means it runs only when triggered." + }, + { + "name": "settings", + "valueType": "string", + "isArray": true, + "required": false, + "description": "The settings groups this app reads, by name (an object.settings of the config pack). Recorded by name only: this pack does not require the config pack, so the name is not resolved here." + } + ], + "children": [] + }, + { + "type": "app", + "subType": "service", + "description": "A long-running process that answers requests: a web server, an API, a socket server. Where and how it runs (build, process, health check, deployments) is the delivery pack's, declared as children it admits.", + "whenToUse": "Anything that listens on a port and stays up.", + "attrs": [ + { + "name": "calls", + "valueType": "string", + "isArray": true, + "required": false, + "description": "The apps and outside systems this app calls. Each entry names an app.* or system.external node: a bare name is one in the same system (an outside system may also be declared at the document root); `System.child` names one in another system. A name that resolves to nothing, or to another kind of node, fails the load (ERR_SYSTEMS_UNRESOLVED_REF). Who may reach whom is derived from this list, so it is the list to keep current." + }, + { + "name": "needs", + "valueType": "string", + "isArray": true, + "required": false, + "description": "The resources this app uses. Each entry names a resource.* node: a bare name is a resource of the same system; `System.resource` (or `pkg::System.resource`) names one in another. A name that resolves to nothing, or to a node that is not a resource, fails the load (ERR_SYSTEMS_UNRESOLVED_REF)." + }, + { + "name": "settings", + "valueType": "string", + "isArray": true, + "required": false, + "description": "The settings groups this app reads, by name (an object.settings of the config pack). Recorded by name only: this pack does not require the config pack, so the name is not resolved here." + } + ], + "children": [] + }, + { + "type": "resource", + "subType": "base", + "description": "Abstract resource base: the attributes every resource.* shares. Not authored directly: a `resource.base` node fails to load (ERR_ABSTRACT_SUBTYPE_AUTHORED).", + "attrs": [ + { + "name": "engine", + "valueType": "string", + "isArray": false, + "required": false, + "description": "The product behind the resource, lower case: postgres, mysql, sqlite, redis, s3, r2. A platform generator reads it to pick the platform's own resource; this pack does not close the list." + }, + { + "name": "managedBy", + "valueType": "string", + "isArray": false, + "required": false, + "allowedValues": [ + "platform", + "external" + ], + "description": "Who provisions it. platform: the platform an app deploys to creates it beside the app (a platform database). external: it exists outside that platform and is reached by settings (a bucket at another provider). Absent means platform." + }, + { + "name": "version", + "valueType": "string", + "isArray": false, + "required": false, + "description": "The engine's major version, as a string (`18`)." + } + ], + "children": [] + }, + { + "type": "resource", + "subType": "bucket", + "description": "Object storage: files addressed by key.", + "whenToUse": "Uploaded images, exports, static assets kept outside the app.", + "attrs": [ + { + "name": "engine", + "valueType": "string", + "isArray": false, + "required": false, + "description": "The product behind the resource, lower case: postgres, mysql, sqlite, redis, s3, r2. A platform generator reads it to pick the platform's own resource; this pack does not close the list." + }, + { + "name": "managedBy", + "valueType": "string", + "isArray": false, + "required": false, + "allowedValues": [ + "platform", + "external" + ], + "description": "Who provisions it. platform: the platform an app deploys to creates it beside the app (a platform database). external: it exists outside that platform and is reached by settings (a bucket at another provider). Absent means platform." + }, + { + "name": "version", + "valueType": "string", + "isArray": false, + "required": false, + "description": "The engine's major version, as a string (`18`)." + } + ], + "children": [] + }, + { + "type": "resource", + "subType": "cache", + "description": "A cache whose contents can be lost without losing data.", + "whenToUse": "Redis or Memcached used for speed, sessions or rate limits.", + "attrs": [ + { + "name": "engine", + "valueType": "string", + "isArray": false, + "required": false, + "description": "The product behind the resource, lower case: postgres, mysql, sqlite, redis, s3, r2. A platform generator reads it to pick the platform's own resource; this pack does not close the list." + }, + { + "name": "managedBy", + "valueType": "string", + "isArray": false, + "required": false, + "allowedValues": [ + "platform", + "external" + ], + "description": "Who provisions it. platform: the platform an app deploys to creates it beside the app (a platform database). external: it exists outside that platform and is reached by settings (a bucket at another provider). Absent means platform." + }, + { + "name": "version", + "valueType": "string", + "isArray": false, + "required": false, + "description": "The engine's major version, as a string (`18`)." + } + ], + "children": [] + }, + { + "type": "resource", + "subType": "database", + "description": "A database an app stores records in.", + "whenToUse": "Postgres, MySQL, SQLite or any store an app queries.", + "attrs": [ + { + "name": "engine", + "valueType": "string", + "isArray": false, + "required": false, + "description": "The product behind the resource, lower case: postgres, mysql, sqlite, redis, s3, r2. A platform generator reads it to pick the platform's own resource; this pack does not close the list." + }, + { + "name": "managedBy", + "valueType": "string", + "isArray": false, + "required": false, + "allowedValues": [ + "platform", + "external" + ], + "description": "Who provisions it. platform: the platform an app deploys to creates it beside the app (a platform database). external: it exists outside that platform and is reached by settings (a bucket at another provider). Absent means platform." + }, + { + "name": "version", + "valueType": "string", + "isArray": false, + "required": false, + "description": "The engine's major version, as a string (`18`)." + } + ], + "children": [] + }, + { + "type": "resource", + "subType": "keyvalue", + "description": "A durable key-value store that is the source of its data, unlike a cache.", + "whenToUse": "A platform key-value namespace an app reads and writes as its store.", + "attrs": [ + { + "name": "engine", + "valueType": "string", + "isArray": false, + "required": false, + "description": "The product behind the resource, lower case: postgres, mysql, sqlite, redis, s3, r2. A platform generator reads it to pick the platform's own resource; this pack does not close the list." + }, + { + "name": "managedBy", + "valueType": "string", + "isArray": false, + "required": false, + "allowedValues": [ + "platform", + "external" + ], + "description": "Who provisions it. platform: the platform an app deploys to creates it beside the app (a platform database). external: it exists outside that platform and is reached by settings (a bucket at another provider). Absent means platform." + }, + { + "name": "version", + "valueType": "string", + "isArray": false, + "required": false, + "description": "The engine's major version, as a string (`18`)." + } + ], + "children": [] + }, + { + "type": "resource", + "subType": "queue", + "description": "A message queue apps send work through.", + "whenToUse": "Work handed from one app to another to run later.", + "attrs": [ + { + "name": "engine", + "valueType": "string", + "isArray": false, + "required": false, + "description": "The product behind the resource, lower case: postgres, mysql, sqlite, redis, s3, r2. A platform generator reads it to pick the platform's own resource; this pack does not close the list." + }, + { + "name": "managedBy", + "valueType": "string", + "isArray": false, + "required": false, + "allowedValues": [ + "platform", + "external" + ], + "description": "Who provisions it. platform: the platform an app deploys to creates it beside the app (a platform database). external: it exists outside that platform and is reached by settings (a bucket at another provider). Absent means platform." + }, + { + "name": "version", + "valueType": "string", + "isArray": false, + "required": false, + "description": "The engine's major version, as a string (`18`)." + } + ], + "children": [] + }, + { + "type": "resource", + "subType": "volume", + "description": "A disk attached to an app that survives a restart.", + "whenToUse": "A file system path an app writes to and must keep.", + "attrs": [ + { + "name": "engine", + "valueType": "string", + "isArray": false, + "required": false, + "description": "The product behind the resource, lower case: postgres, mysql, sqlite, redis, s3, r2. A platform generator reads it to pick the platform's own resource; this pack does not close the list." + }, + { + "name": "managedBy", + "valueType": "string", + "isArray": false, + "required": false, + "allowedValues": [ + "platform", + "external" + ], + "description": "Who provisions it. platform: the platform an app deploys to creates it beside the app (a platform database). external: it exists outside that platform and is reached by settings (a bucket at another provider). Absent means platform." + }, + { + "name": "version", + "valueType": "string", + "isArray": false, + "required": false, + "description": "The engine's major version, as a string (`18`)." + } + ], + "children": [] + }, + { + "type": "system", + "subType": "base", + "description": "Abstract system base: the shared root of system.internal and system.external. Not authored directly: a `system.base` node fails to load (ERR_ABSTRACT_SUBTYPE_AUTHORED).", + "attrs": [], + "children": [] + }, + { + "type": "system", + "subType": "external", + "description": "A system someone else runs that an app calls: a payment provider, a chat platform, a model API. Named so an app's @calls can resolve to it; its address and credentials are settings, never attributes here.", + "whenToUse": "Any third-party service an app depends on. Declare it inside the system that calls it, or at the document root when several systems share it.", + "attrs": [], + "children": [] + }, + { + "type": "system", + "subType": "internal", + "description": "A system this team builds and runs: the apps it is made of, the resources they use, and the outside systems they call. It answers what exists and what talks to what, and holds no deploy facts (those are the delivery pack's). Its apps and resources are addressed from outside it as `System.child` (`Shop.web`), or `pkg::System.child` from another package.", + "whenToUse": "Every product or service that has at least one app. One system per product; a system documentation explorer and every deploy generator start here.", + "attrs": [], + "children": [ + { + "childType": "app", + "childSubType": "*", + "childName": "*", + "min": 0, + "max": null + }, + { + "childType": "resource", + "childSubType": "*", + "childName": "*", + "min": 0, + "max": null + }, + { + "childType": "system", + "childSubType": "external", + "childName": "*", + "min": 0, + "max": null + } + ] + } + ], + "extends": [ + { + "type": "metadata", + "subType": "root", + "attrs": [], + "children": [ + { + "childType": "system", + "childSubType": [ + "internal", + "external" + ], + "childName": "*", + "min": 0, + "max": null + } + ] + } + ], + "commonAttrs": [], + "defaultSubTypes": {} +} diff --git a/packs/systems/metamodel/systems.json b/packs/systems/metamodel/systems.json new file mode 100644 index 0000000000..69c8562cad --- /dev/null +++ b/packs/systems/metamodel/systems.json @@ -0,0 +1,87 @@ +{ + "provider": "metaobjects-systems", + "types": [ + { + "type": "system", + "subType": "base", + "description": "Abstract system base: the shared root of system.internal and system.external. Not authored directly: a `system.base` node fails to load (ERR_ABSTRACT_SUBTYPE_AUTHORED)." + }, + { + "type": "system", + "subType": "internal", + "extendsBase": true, + "description": "A system this team builds and runs: the apps it is made of, the resources they use, and the outside systems they call. It answers what exists and what talks to what, and holds no deploy facts (those are the delivery pack's). Its apps and resources are addressed from outside it as `System.child` (`Shop.web`), or `pkg::System.child` from another package.", + "whenToUse": "Every product or service that has at least one app. One system per product; a system documentation explorer and every deploy generator start here.", + "children": [ + { "type": "app", "subType": "*", "name": "*", "min": 0, "max": null }, + { "type": "resource", "subType": "*", "name": "*", "min": 0, "max": null }, + { "type": "system", "subType": "external", "name": "*", "min": 0, "max": null } + ] + }, + { + "type": "system", + "subType": "external", + "extendsBase": true, + "description": "A system someone else runs that an app calls: a payment provider, a chat platform, a model API. Named so an app's @calls can resolve to it; its address and credentials are settings, never attributes here.", + "whenToUse": "Any third-party service an app depends on. Declare it inside the system that calls it, or at the document root when several systems share it." + }, + { + "type": "app", + "subType": "base", + "description": "Abstract app base: the attributes every app.* shares. Not authored directly: an `app.base` node fails to load (ERR_ABSTRACT_SUBTYPE_AUTHORED).", + "children": [ + { "type": "attr", "subType": "string", "name": "needs", "isArray": true, "min": 0, "max": 1, "description": "The resources this app uses. Each entry names a resource.* node: a bare name is a resource of the same system; `System.resource` (or `pkg::System.resource`) names one in another. A name that resolves to nothing, or to a node that is not a resource, fails the load (ERR_SYSTEMS_UNRESOLVED_REF)." }, + { "type": "attr", "subType": "string", "name": "calls", "isArray": true, "min": 0, "max": 1, "description": "The apps and outside systems this app calls. Each entry names an app.* or system.external node: a bare name is one in the same system (an outside system may also be declared at the document root); `System.child` names one in another system. A name that resolves to nothing, or to another kind of node, fails the load (ERR_SYSTEMS_UNRESOLVED_REF). Who may reach whom is derived from this list, so it is the list to keep current." }, + { "type": "attr", "subType": "string", "name": "settings", "isArray": true, "min": 0, "max": 1, "description": "The settings groups this app reads, by name (an object.settings of the config pack). Recorded by name only: this pack does not require the config pack, so the name is not resolved here." } + ] + }, + { + "type": "app", + "subType": "service", + "extendsBase": true, + "description": "A long-running process that answers requests: a web server, an API, a socket server. Where and how it runs (build, process, health check, deployments) is the delivery pack's, declared as children it admits.", + "whenToUse": "Anything that listens on a port and stays up." + }, + { + "type": "app", + "subType": "client", + "extendsBase": true, + "description": "Code that runs on the user's device: a single-page web app, a static site, a mobile app. It calls services; it serves nothing itself.", + "whenToUse": "A front end built and shipped as files, whatever serves them.", + "children": [ + { "type": "attr", "subType": "string", "name": "shell", "min": 0, "max": 1, "description": "The UI frame (a nav.shell of a UI pack) this client presents, by name. Recorded by name only: no UI pack is required, so the name is not resolved here." } + ] + }, + { + "type": "app", + "subType": "job", + "extendsBase": true, + "description": "Work that runs to completion and exits: a scheduled task, a migration, a one-off batch. It may run on a schedule (@schedule) or when something triggers it.", + "whenToUse": "A cron job, a nightly import, a queue drain that is not a long-running service.", + "children": [ + { "type": "attr", "subType": "string", "name": "schedule", "min": 0, "max": 1, "description": "When the job runs, as a five-field cron expression in UTC (`0 3 * * *`). Absent means it runs only when triggered." } + ] + }, + { + "type": "resource", + "subType": "base", + "description": "Abstract resource base: the attributes every resource.* shares. Not authored directly: a `resource.base` node fails to load (ERR_ABSTRACT_SUBTYPE_AUTHORED).", + "children": [ + { "type": "attr", "subType": "string", "name": "engine", "min": 0, "max": 1, "description": "The product behind the resource, lower case: postgres, mysql, sqlite, redis, s3, r2. A platform generator reads it to pick the platform's own resource; this pack does not close the list." }, + { "type": "attr", "subType": "string", "name": "version", "min": 0, "max": 1, "description": "The engine's major version, as a string (`18`)." }, + { "type": "attr", "subType": "string", "name": "managedBy", "min": 0, "max": 1, "allowedValues": ["platform", "external"], "description": "Who provisions it. platform: the platform an app deploys to creates it beside the app (a platform database). external: it exists outside that platform and is reached by settings (a bucket at another provider). Absent means platform." } + ] + }, + { "type": "resource", "subType": "database", "extendsBase": true, "description": "A database an app stores records in.", "whenToUse": "Postgres, MySQL, SQLite or any store an app queries." }, + { "type": "resource", "subType": "cache", "extendsBase": true, "description": "A cache whose contents can be lost without losing data.", "whenToUse": "Redis or Memcached used for speed, sessions or rate limits." }, + { "type": "resource", "subType": "bucket", "extendsBase": true, "description": "Object storage: files addressed by key.", "whenToUse": "Uploaded images, exports, static assets kept outside the app." }, + { "type": "resource", "subType": "keyvalue", "extendsBase": true, "description": "A durable key-value store that is the source of its data, unlike a cache.", "whenToUse": "A platform key-value namespace an app reads and writes as its store." }, + { "type": "resource", "subType": "queue", "extendsBase": true, "description": "A message queue apps send work through.", "whenToUse": "Work handed from one app to another to run later." }, + { "type": "resource", "subType": "volume", "extendsBase": true, "description": "A disk attached to an app that survives a restart.", "whenToUse": "A file system path an app writes to and must keep." } + ], + "extends": [ + { "type": "metadata", "subType": "root", "children": [ + { "type": "system", "subType": ["internal", "external"], "name": "*", "min": 0, "max": null } + ] } + ] +} diff --git a/packs/systems/pack.json b/packs/systems/pack.json new file mode 100644 index 0000000000..e18034ecea --- /dev/null +++ b/packs/systems/pack.json @@ -0,0 +1,10 @@ +{ + "id": "metaobjects-systems", + "version": "0.1.0", + "stability": "preview", + "description": "What exists and what talks to what: systems, apps (service, client, job) and the resources they use.", + "requires": { "metamodel": ">=1.3 <2", "packs": {} }, + "ports": { + "typescript": { "package": "@metaobjectsdev/pack-systems", "core": ">=1.3.0 <2.0.0", "publish": false } + } +} diff --git a/packs/systems/typescript/package.json b/packs/systems/typescript/package.json new file mode 100644 index 0000000000..06f59672f5 --- /dev/null +++ b/packs/systems/typescript/package.json @@ -0,0 +1,20 @@ +{ + "name": "@metaobjectsdev/pack-systems", + "version": "0.1.0", + "description": "The systems pack: systems, apps and resources (ADR-0069 independent pack, preview).", + "private": true, + "type": "module", + "exports": { + ".": { "bun": "./src/index.ts", "default": "./src/index.ts" } + }, + "scripts": { + "typecheck": "tsc --noEmit -p ." + }, + "peerDependencies": { + "@metaobjectsdev/metadata": ">=1.3.0 <2.0.0" + }, + "devDependencies": { + "@metaobjectsdev/metadata": "workspace:*" + }, + "license": "Apache-2.0" +} diff --git a/packs/systems/typescript/src/constants.ts b/packs/systems/typescript/src/constants.ts new file mode 100644 index 0000000000..2aaebd4d57 --- /dev/null +++ b/packs/systems/typescript/src/constants.ts @@ -0,0 +1,28 @@ +// The systems pack's vocabulary names, one constant each (metamodel/systems.json is the +// definition; these are the spellings code compares against). + +export const SYSTEMS_PROVIDER_ID = "metaobjects-systems"; + +export const TYPE_SYSTEM = "system"; +export const SYSTEM_SUBTYPE_INTERNAL = "internal"; +export const SYSTEM_SUBTYPE_EXTERNAL = "external"; + +export const TYPE_APP = "app"; +export const APP_SUBTYPE_SERVICE = "service"; +export const APP_SUBTYPE_CLIENT = "client"; +export const APP_SUBTYPE_JOB = "job"; + +export const TYPE_RESOURCE = "resource"; + +export const APP_ATTR_NEEDS = "needs"; +export const APP_ATTR_CALLS = "calls"; +export const APP_ATTR_SETTINGS = "settings"; +export const APP_ATTR_SHELL = "shell"; +export const APP_ATTR_SCHEDULE = "schedule"; + +export const RESOURCE_ATTR_ENGINE = "engine"; +export const RESOURCE_ATTR_VERSION = "version"; +export const RESOURCE_ATTR_MANAGED_BY = "managedBy"; + +/** Raised when an app's @needs or @calls names nothing, or the wrong kind of node. */ +export const ERR_SYSTEMS_UNRESOLVED_REF = "ERR_SYSTEMS_UNRESOLVED_REF"; diff --git a/packs/systems/typescript/src/index.ts b/packs/systems/typescript/src/index.ts new file mode 100644 index 0000000000..673282af22 --- /dev/null +++ b/packs/systems/typescript/src/index.ts @@ -0,0 +1,67 @@ +// @metaobjectsdev/pack-systems: what exists and what talks to what (ADR-0069 independent +// pack). The vocabulary is metamodel/systems.json; this module registers it and adds the +// one check the data cannot express, that an app's @needs and @calls name real nodes. + +import { + applyProviderDefinition, + MetaData, + type FactoryMap, + type MetaDataTypeProvider, + type NodeValidator, + type ProviderDefinition, + type TypeId, + type TypeRegistry, + type ValidatorMap, +} from "@metaobjectsdev/metadata"; +import definition from "../../metamodel/systems.json" with { type: "json" }; +import { + APP_ATTR_CALLS, + APP_ATTR_NEEDS, + ERR_SYSTEMS_UNRESOLVED_REF, + SYSTEMS_PROVIDER_ID, + SYSTEM_SUBTYPE_EXTERNAL, + TYPE_APP, + TYPE_RESOURCE, + TYPE_SYSTEM, +} from "./constants.js"; +import { checkRefs } from "./refs.js"; + +export * from "./constants.js"; +export { checkRefs, refList, resolveSystemRef } from "./refs.js"; + +/** A node of this pack's vocabulary. Its attributes are read with the resolving accessors. */ +export class MetaSystemNode extends MetaData {} + +export const SYSTEMS_DEFINITION = definition as ProviderDefinition; + +const validateApp: NodeValidator = (node, ctx) => { + const error = ctx.error.bind(ctx); + checkRefs(node, APP_ATTR_NEEDS, [TYPE_RESOURCE], ERR_SYSTEMS_UNRESOLVED_REF, error); + checkRefs(node, APP_ATTR_CALLS, [TYPE_APP, `${TYPE_SYSTEM}.${SYSTEM_SUBTYPE_EXTERNAL}`], ERR_SYSTEMS_UNRESOLVED_REF, error); +}; + +/** A factory for every type a definition declares, and a validator for each of `validated`'s types. */ +export function packTypeMaps( + data: ProviderDefinition, + validated: Readonly>, +): { factories: FactoryMap; validators: ValidatorMap } { + const factories: FactoryMap = {}; + const validators: ValidatorMap = {}; + for (const def of data.types ?? []) { + const key = `${def.type}.${def.subType}`; + factories[key] = (typeId: TypeId, name: string) => new MetaSystemNode(typeId, name); + const validate = validated[def.type]; + if (validate !== undefined) validators[key] = validate; + } + return { factories, validators }; +} + +export const provider: MetaDataTypeProvider = { + id: SYSTEMS_PROVIDER_ID, + dependencies: ["metaobjects-core-types"], + description: "Systems: system.internal / system.external, app.service / app.client / app.job and resource.*.", + registerTypes(registry: TypeRegistry): void { + const { factories, validators } = packTypeMaps(SYSTEMS_DEFINITION, { [TYPE_APP]: validateApp }); + applyProviderDefinition(registry, SYSTEMS_DEFINITION, factories, validators); + }, +}; diff --git a/packs/systems/typescript/src/refs.ts b/packs/systems/typescript/src/refs.ts new file mode 100644 index 0000000000..999a8e0db5 --- /dev/null +++ b/packs/systems/typescript/src/refs.ts @@ -0,0 +1,72 @@ +// Resolving a reference to a node inside a system. +// +// Apps and resources are children of a system.internal, so they are not top-level nodes +// and the loader's reference descriptors (which bind top-level nodes only) cannot reach +// them. They are addressed the house way instead (ADR-0059): `System.child` binds the +// system among the top-level nodes and walks to its child, and `pkg::System.child` +// does so from another package. A bare name is a convenience for the common case, a +// node in the referrer's own system, then a top-level node of that name (an outside +// system declared at the document root). + +import { + CHILD_REF_SEPARATOR, + PACKAGE_SEPARATOR, + effectivePackageOf, + resolveRef, + topLevelNodeOf, + type MetaData, +} from "@metaobjectsdev/metadata"; +import { TYPE_SYSTEM } from "./constants.js"; + +/** The nearest enclosing system of `node`, or undefined when it sits in none. */ +function enclosingSystem(node: MetaData): MetaData | undefined { + for (let p = node.parent; p !== undefined; p = p.parent) if (p.type === TYPE_SYSTEM) return p; + return undefined; +} + +/** + * The node `ref` names, seen from `from`, or undefined when it names nothing. The + * caller checks the kind: a reference to the wrong kind of node is the caller's error. + */ +export function resolveSystemRef(from: MetaData, ref: string): MetaData | undefined { + const top = topLevelNodeOf(from); + const root = top.parent; + if (root === undefined) return undefined; + const pkg = effectivePackageOf(top); + if (!ref.includes(CHILD_REF_SEPARATOR) && !ref.includes(PACKAGE_SEPARATOR)) { + // ADR-0039: resolving, so a system that `extends` another sees its inherited children. + const sibling = enclosingSystem(from)?.children().find((c) => c.name === ref); + if (sibling !== undefined) return sibling; + } + return resolveRef(root, ref, pkg).node; +} + +/** The string entries of a string or string-array attribute (absent: none). */ +export function refList(node: MetaData, attr: string): string[] { + const raw = node.attr(attr); + if (typeof raw === "string") return raw === "" ? [] : [raw]; + return Array.isArray(raw) ? raw.filter((v): v is string => typeof v === "string") : []; +} + +/** + * Check every reference in `attr` resolves to a node of one of `kinds` (a type, or a + * `type.subType`), reporting each one that does not under `code`. + */ +export function checkRefs( + node: MetaData, + attr: string, + kinds: readonly string[], + code: string, + error: (code: string, node: MetaData, message: string) => void, +): void { + for (const ref of refList(node, attr)) { + const target = resolveSystemRef(node, ref); + const label = `${node.type}.${node.subType} "${node.name}" @${attr} "${ref}"`; + const want = kinds.join(" or "); + if (target === undefined) { + error(code, node, `${label} does not resolve to a ${want}.`); + } else if (!kinds.some((k) => k === target.type || k === `${target.type}.${target.subType}`)) { + error(code, node, `${label} resolves to ${target.type}.${target.subType}, not a ${want}.`); + } + } +} diff --git a/packs/systems/typescript/test/refs.test.ts b/packs/systems/typescript/test/refs.test.ts new file mode 100644 index 0000000000..e13e36fe2d --- /dev/null +++ b/packs/systems/typescript/test/refs.test.ts @@ -0,0 +1,57 @@ +import { describe, expect, test } from "bun:test"; +import { InMemoryStringSource, MetaDataLoader, composeRegistry, coreProviders, type MetaData } from "@metaobjectsdev/metadata"; +import { provider, resolveSystemRef } from "../src/index.js"; + +const SHOP = { + "metadata.root": { + package: "acme::shop", + children: [ + { "system.internal": { name: "Shop", children: [ + { "app.service": { name: "api", "@needs": ["db"], "@calls": ["Mail", "Billing.ledger"] } }, + { "resource.database": { name: "db" } }, + ] } }, + { "system.internal": { name: "Billing", children: [{ "app.service": { name: "ledger" } }] } }, + { "system.external": { name: "Mail" } }, + ], + }, +}; +const ESTATE = { "metadata.root": { package: "acme::estate", children: [{ "system.external": { name: "Dns" } }] } }; + +async function load(): Promise { + const loader = new MetaDataLoader({ registry: composeRegistry([...coreProviders, provider]), strict: true }); + const { root } = await loader.load([ + new InMemoryStringSource(JSON.stringify(SHOP), { id: "meta.shop.json" }), + new InMemoryStringSource(JSON.stringify(ESTATE), { id: "meta.estate.json" }), + ]); + return root; +} + +function api(root: MetaData): MetaData { + const shop = root.children().find((c) => c.name === "Shop")!; + return shop.children().find((c) => c.name === "api")!; +} + +describe("resolveSystemRef", () => { + test("a bare name is a node of the referrer's own system", async () => { + const r = resolveSystemRef(api(await load()), "db"); + expect(`${r?.type}.${r?.subType}:${r?.name}`).toBe("resource.database:db"); + }); + + test("a bare name that is no sibling falls back to a top-level node of the referrer's package", async () => { + expect(resolveSystemRef(api(await load()), "Mail")?.subType).toBe("external"); + }); + + test("System.child walks into another system, and pkg::System.child into another package", async () => { + const root = await load(); + expect(resolveSystemRef(api(root), "Billing.ledger")?.name).toBe("ledger"); + expect(resolveSystemRef(api(root), "acme::shop::Billing.ledger")?.name).toBe("ledger"); + expect(resolveSystemRef(api(root), "acme::estate::Dns")?.name).toBe("Dns"); + }); + + test("a name that matches nothing resolves to nothing, as does a bare name from another package", async () => { + const root = await load(); + expect(resolveSystemRef(api(root), "nope")).toBeUndefined(); + expect(resolveSystemRef(api(root), "Billing.nope")).toBeUndefined(); + expect(resolveSystemRef(api(root), "Dns")).toBeUndefined(); + }); +}); diff --git a/packs/systems/typescript/tsconfig.json b/packs/systems/typescript/tsconfig.json new file mode 100644 index 0000000000..b33fae1d44 --- /dev/null +++ b/packs/systems/typescript/tsconfig.json @@ -0,0 +1,7 @@ +{ + "extends": "../../../server/typescript/tsconfig.base.json", + "compilerOptions": { + "noEmit": true + }, + "include": ["src/**/*", "test/**/*"] +} diff --git a/scripts/pack-conformance.ts b/scripts/pack-conformance.ts index 18f5d3e0dd..0d8d51e38d 100644 --- a/scripts/pack-conformance.ts +++ b/scripts/pack-conformance.ts @@ -11,8 +11,9 @@ * in its providers.json, because a scenario that loads only the default bundle tests * core, not the pack. * - * Error codes: the core list (fixtures/conformance/ERROR-CODES.json) plus the pack's own - * packs//fixtures/conformance/ERROR-CODES.json when it declares codes of its own. + * Error codes: the core list (fixtures/conformance/ERROR-CODES.json) plus + * packs//fixtures/conformance/ERROR-CODES.json of the pack and of every pack it + * requires, for each that declares codes of its own. * An optional packs//typescript/conformance-expected-failures.json is the port's * ledger, in the core ledger's format. * @@ -23,7 +24,7 @@ import { join } from "node:path"; import { runCorpus, loadErrorCodes, loadLedger, type CorpusResult } from "../server/typescript/packages/conformance/src/index.js"; import { createTsAdapter } from "../server/typescript/packages/metadata/test/conformance/adapter.js"; import type { MetaDataTypeProvider } from "../server/typescript/packages/metadata/src/index.js"; -import { PACK_CORPUS, REPO_ROOT, loadPackClosure, packDir, packNamesFromArgs, readPackManifest } from "./lib/packs.js"; +import { PACK_CORPUS, REPO_ROOT, loadPackClosure, packDir, packNamesFromArgs, packsById, readPackManifest } from "./lib/packs.js"; const ERROR_CODES = "ERROR-CODES.json"; @@ -34,10 +35,14 @@ export async function runPackConformance(name: string, root: string = REPO_ROOT) const closure = await loadPackClosure(name, root); const extra: Record = {}; for (const p of closure) extra[p.id] = p; - const errorCodes = [ - ...(await loadErrorCodes(join(root, "fixtures/conformance", ERROR_CODES))), - ...(await loadErrorCodes(join(corpusRoot, ERROR_CODES))), - ]; + // The codes a scenario may expect: core's, and those of this pack and every pack it + // requires (a required pack's check runs on this pack's models too). + const byId = packsById(root); + const errorCodes = [...(await loadErrorCodes(join(root, "fixtures/conformance", ERROR_CODES)))]; + for (const p of closure) { + // A pack with no codes of its own has no file; loadErrorCodes reads that as none. + errorCodes.push(...(await loadErrorCodes(join(packDir(byId.get(p.id) ?? name, root), PACK_CORPUS, ERROR_CODES)))); + } return runCorpus({ corpusRoot, adapter: createTsAdapter(extra), diff --git a/server/typescript/packages/metadata/src/index.ts b/server/typescript/packages/metadata/src/index.ts index ef6c0903c3..ef5df624b2 100644 --- a/server/typescript/packages/metadata/src/index.ts +++ b/server/typescript/packages/metadata/src/index.ts @@ -330,6 +330,13 @@ export { FIELD_ATTR_XML_TEXT } from "./template/template-constants.js"; // Type provider model export { composeRegistry } from "./provider.js"; export type { MetaDataTypeProvider } from "./provider.js"; +// FR-033 — register vocabulary from a provider definition file (the spec/metamodel/ +// format). The bundled packs use it; so may a provider outside core (ADR-0069 §5: +// the definition file format is part of the covered provider contract). +export { applyProviderDefinition } from "./provider-data.js"; +export type { ProviderDefinition, TypeDef, ChildDef, ExtendDef, FactoryMap, ValidatorMap } from "./provider-data.js"; +// The validation hook a provider's TypeDefinition may carry (`validate`). +export type { NodeValidator, ValidationContext, SymbolTable, LoaderCode } from "./validation-types.js"; // DataType classification export { diff --git a/server/typescript/packages/metadata/src/provider-data.ts b/server/typescript/packages/metadata/src/provider-data.ts index f94a73b78f..83ffb13db0 100644 --- a/server/typescript/packages/metadata/src/provider-data.ts +++ b/server/typescript/packages/metadata/src/provider-data.ts @@ -18,6 +18,7 @@ import type { AttrValue, MetaData } from "./shared/meta-data.js"; import type { DataType } from "./data-type.js"; import { CHILD_RULE_WILDCARD } from "./shared/structural.js"; import { SUBTYPE_BASE } from "./shared/base-types.js"; +import type { NodeValidator } from "./validation-types.js"; /** * A UNIFIED child requirement (spec §3.1). Every constraint on a type — both its @@ -175,6 +176,13 @@ export interface ProviderDefinition { */ export type FactoryMap = Record MetaData>; +/** + * Optional, also code: a `"."` → imperative validator map. A type the + * definition registers carries its validator on its `TypeDefinition` (`validate`), the + * extension point a provider outside core uses for checks the data cannot express. + */ +export type ValidatorMap = Record; + const ATTR_CHILD_TYPE = "attr"; /** Whether a `ChildDef` declares an attribute (vs a structural child). */ @@ -229,6 +237,7 @@ export function defineProviderFromData( data: ProviderDefinition, factories: FactoryMap, externalBase?: ExternalBaseLookup, + validators?: ValidatorMap, ): TypeDefinition[] { // Pre-compute each type's OWN lowered attrs + childRules, keyed by "type.subType", // so base-composition can fold a base's contributions into its subtypes. A @@ -263,6 +272,7 @@ export function defineProviderFromData( const self = lowered.get(key)!; const { attributes, childRules } = composeWithBase(t, self, lowered, externalBase); + const validate = validators?.[key]; return { typeId: new TypeId(t.type, t.subType), @@ -278,6 +288,7 @@ export function defineProviderFromData( ...(t.implementedIn !== undefined ? { implementedIn: t.implementedIn } : {}), ...(t.maxOccurs !== undefined ? { maxOccurs: t.maxOccurs } : {}), ...(t.defaultName !== undefined ? { defaultName: t.defaultName } : {}), + ...(validate !== undefined ? { validate } : {}), }; }); } @@ -293,6 +304,7 @@ export function applyProviderDefinition( registry: TypeRegistry, data: ProviderDefinition, factories: FactoryMap, + validators?: ValidatorMap, ): void { // A pack may add a subtype to a type another provider owns (the reporting pack's // object.report): its `extendsBase` folds in that type's `base` as already @@ -303,7 +315,7 @@ export function applyProviderDefinition( ? undefined : { attributes: [...base.attributes], childRules: [...base.childRules] }; }; - for (const def of defineProviderFromData(data, factories, externalBase)) { + for (const def of defineProviderFromData(data, factories, externalBase, validators)) { registry.register(def); } const commonAttrs = universalCommonAttrs(data); diff --git a/server/typescript/packages/metadata/test/provider-data.test.ts b/server/typescript/packages/metadata/test/provider-data.test.ts index 447e258a57..07a64b37f3 100644 --- a/server/typescript/packages/metadata/test/provider-data.test.ts +++ b/server/typescript/packages/metadata/test/provider-data.test.ts @@ -482,3 +482,18 @@ test("a type with no children → empty attributes and childRules", () => { expect(def.attributes).toEqual([]); expect(def.childRules).toEqual([]); }); + +test("a validator map puts each validator on its type's definition, and only there", () => { + const data: ProviderDefinition = { + provider: "t", + types: [ + { type: "field", subType: "string", description: "A text field." }, + { type: "field", subType: "int", description: "A whole number." }, + ], + }; + const reg = new TypeRegistry(); + const validate = () => {}; + applyProviderDefinition(reg, data, { "field.string": FIELD_FACTORY, "field.int": FIELD_FACTORY }, { "field.string": validate }); + expect(reg.find("field", "string")!.validate).toBe(validate); + expect(reg.find("field", "int")!.validate).toBeUndefined(); +}); From 80e0cc5e4cdf1438ec25cc99b7ba724f06c04f7d Mon Sep 17 00:00:00 2001 From: Doug Mealing Date: Wed, 25 Nov 2026 02:05:47 -0500 Subject: [PATCH 2/6] refactor(packs): reference checks take the validation context and resolve in the declaring package checkRefs takes the ValidationContext instead of a bound error callback, and resolves through referrerPackageFor (now exported from core, ADR-0059 R3), so an inherited reference means what it meant where it was written. pack-conformance throws on a required pack with no directory instead of falling back. --- ...metadata-apply-provider-definition-export.md | 6 ++++-- packs/delivery/typescript/src/index.ts | 7 +++---- packs/systems/typescript/src/index.ts | 5 ++--- packs/systems/typescript/src/refs.ts | 17 +++++++++++------ scripts/pack-conformance.ts | 4 +++- .../typescript/packages/metadata/src/index.ts | 1 + 6 files changed, 24 insertions(+), 16 deletions(-) diff --git a/changelog.d/metadata-apply-provider-definition-export.md b/changelog.d/metadata-apply-provider-definition-export.md index 4776bb7da0..ab6506ae0a 100644 --- a/changelog.d/metadata-apply-provider-definition-export.md +++ b/changelog.d/metadata-apply-provider-definition-export.md @@ -4,5 +4,7 @@ the validation hook's types (`NodeValidator`, `ValidationContext`, `SymbolTable`, `LoaderCode`). `applyProviderDefinition` takes an optional fourth argument, a `"."` → validator map, so a type registered from data can carry a check the data - cannot express. The bundled packs already registered this way; the independent packs under - `packs/` (ADR-0069) now can too. No vocabulary or `metamodelVersion` change. + cannot express. `referrerPackageFor` (ADR-0059 R3: the package a reference attribute resolves + in) is exported too, so a provider's own reference check resolves as the loader's does. The + bundled packs already registered this way; the independent packs under `packs/` (ADR-0069) now + can too. No vocabulary or `metamodelVersion` change. diff --git a/packs/delivery/typescript/src/index.ts b/packs/delivery/typescript/src/index.ts index c6442445cd..1a773a6b9f 100644 --- a/packs/delivery/typescript/src/index.ts +++ b/packs/delivery/typescript/src/index.ts @@ -35,9 +35,8 @@ export const DELIVERY_DEFINITION = definition as ProviderDefinition; /** The validator a platform pack attaches to its `deployment.` subtype. */ export const validateDeployment: NodeValidator = (node, ctx) => { - const error = ctx.error.bind(ctx); - checkRefs(node, DEPLOYMENT_ATTR_APP, [TYPE_APP], ERR_DELIVERY_UNRESOLVED_REF, error); - checkRefs(node, DEPLOYMENT_ATTR_ENVIRONMENT, [TYPE_ENVIRONMENT], ERR_DELIVERY_UNRESOLVED_REF, error); + checkRefs(node, DEPLOYMENT_ATTR_APP, [TYPE_APP], ERR_DELIVERY_UNRESOLVED_REF, ctx); + checkRefs(node, DEPLOYMENT_ATTR_ENVIRONMENT, [TYPE_ENVIRONMENT], ERR_DELIVERY_UNRESOLVED_REF, ctx); }; // `deployment.base` is abstract, but the parser treats a `.base` as abstract only @@ -67,7 +66,7 @@ const validateBuild: NodeValidator = (node, ctx) => { }; const validateValue: NodeValidator = (node, ctx) => { - checkRefs(node, VALUE_ATTR_FROM, [TYPE_RESOURCE, TYPE_APP], ERR_DELIVERY_UNRESOLVED_REF, ctx.error.bind(ctx)); + checkRefs(node, VALUE_ATTR_FROM, [TYPE_RESOURCE, TYPE_APP], ERR_DELIVERY_UNRESOLVED_REF, ctx); }; export const provider: MetaDataTypeProvider = { diff --git a/packs/systems/typescript/src/index.ts b/packs/systems/typescript/src/index.ts index 673282af22..1b93a3afde 100644 --- a/packs/systems/typescript/src/index.ts +++ b/packs/systems/typescript/src/index.ts @@ -35,9 +35,8 @@ export class MetaSystemNode extends MetaData {} export const SYSTEMS_DEFINITION = definition as ProviderDefinition; const validateApp: NodeValidator = (node, ctx) => { - const error = ctx.error.bind(ctx); - checkRefs(node, APP_ATTR_NEEDS, [TYPE_RESOURCE], ERR_SYSTEMS_UNRESOLVED_REF, error); - checkRefs(node, APP_ATTR_CALLS, [TYPE_APP, `${TYPE_SYSTEM}.${SYSTEM_SUBTYPE_EXTERNAL}`], ERR_SYSTEMS_UNRESOLVED_REF, error); + checkRefs(node, APP_ATTR_NEEDS, [TYPE_RESOURCE], ERR_SYSTEMS_UNRESOLVED_REF, ctx); + checkRefs(node, APP_ATTR_CALLS, [TYPE_APP, `${TYPE_SYSTEM}.${SYSTEM_SUBTYPE_EXTERNAL}`], ERR_SYSTEMS_UNRESOLVED_REF, ctx); }; /** A factory for every type a definition declares, and a validator for each of `validated`'s types. */ diff --git a/packs/systems/typescript/src/refs.ts b/packs/systems/typescript/src/refs.ts index 999a8e0db5..cff6f5c596 100644 --- a/packs/systems/typescript/src/refs.ts +++ b/packs/systems/typescript/src/refs.ts @@ -12,9 +12,11 @@ import { CHILD_REF_SEPARATOR, PACKAGE_SEPARATOR, effectivePackageOf, + referrerPackageFor, resolveRef, topLevelNodeOf, type MetaData, + type ValidationContext, } from "@metaobjectsdev/metadata"; import { TYPE_SYSTEM } from "./constants.js"; @@ -27,12 +29,15 @@ function enclosingSystem(node: MetaData): MetaData | undefined { /** * The node `ref` names, seen from `from`, or undefined when it names nothing. The * caller checks the kind: a reference to the wrong kind of node is the caller's error. + * Given the attribute the reference sits in, a qualified or top-level name resolves in + * the package of the node that wrote it (ADR-0059 R3), so an inherited reference means + * what it meant where it was written. */ -export function resolveSystemRef(from: MetaData, ref: string): MetaData | undefined { +export function resolveSystemRef(from: MetaData, ref: string, attr?: string): MetaData | undefined { const top = topLevelNodeOf(from); const root = top.parent; if (root === undefined) return undefined; - const pkg = effectivePackageOf(top); + const pkg = attr === undefined ? effectivePackageOf(top) : referrerPackageFor(from, attr); if (!ref.includes(CHILD_REF_SEPARATOR) && !ref.includes(PACKAGE_SEPARATOR)) { // ADR-0039: resolving, so a system that `extends` another sees its inherited children. const sibling = enclosingSystem(from)?.children().find((c) => c.name === ref); @@ -57,16 +62,16 @@ export function checkRefs( attr: string, kinds: readonly string[], code: string, - error: (code: string, node: MetaData, message: string) => void, + ctx: ValidationContext, ): void { for (const ref of refList(node, attr)) { - const target = resolveSystemRef(node, ref); + const target = resolveSystemRef(node, ref, attr); const label = `${node.type}.${node.subType} "${node.name}" @${attr} "${ref}"`; const want = kinds.join(" or "); if (target === undefined) { - error(code, node, `${label} does not resolve to a ${want}.`); + ctx.error(code, node, `${label} does not resolve to a ${want}.`); } else if (!kinds.some((k) => k === target.type || k === `${target.type}.${target.subType}`)) { - error(code, node, `${label} resolves to ${target.type}.${target.subType}, not a ${want}.`); + ctx.error(code, node, `${label} resolves to ${target.type}.${target.subType}, not a ${want}.`); } } } diff --git a/scripts/pack-conformance.ts b/scripts/pack-conformance.ts index 0d8d51e38d..c5636bd740 100644 --- a/scripts/pack-conformance.ts +++ b/scripts/pack-conformance.ts @@ -41,7 +41,9 @@ export async function runPackConformance(name: string, root: string = REPO_ROOT) const errorCodes = [...(await loadErrorCodes(join(root, "fixtures/conformance", ERROR_CODES)))]; for (const p of closure) { // A pack with no codes of its own has no file; loadErrorCodes reads that as none. - errorCodes.push(...(await loadErrorCodes(join(packDir(byId.get(p.id) ?? name, root), PACK_CORPUS, ERROR_CODES)))); + const dir = byId.get(p.id); + if (dir === undefined) throw new Error(`pack ${name}: required pack ${p.id} has no directory under packs/`); + errorCodes.push(...(await loadErrorCodes(join(packDir(dir, root), PACK_CORPUS, ERROR_CODES)))); } return runCorpus({ corpusRoot, diff --git a/server/typescript/packages/metadata/src/index.ts b/server/typescript/packages/metadata/src/index.ts index ef5df624b2..c7fdde7318 100644 --- a/server/typescript/packages/metadata/src/index.ts +++ b/server/typescript/packages/metadata/src/index.ts @@ -376,6 +376,7 @@ export { refMatchesObject, resolveObjectRef, resolveRef, + referrerPackageFor, lookupCanonical, refTarget, spellRef, From ae7a5aa5f78ec1ea06588c486e0e640fb0577cc6 Mon Sep 17 00:00:00 2001 From: Doug Mealing Date: Wed, 25 Nov 2026 02:07:25 -0500 Subject: [PATCH 3/6] fix(packs): kind-aware bare names, effective build count, closed @output, a hint for a bare name that misses - A bare reference prefers the sibling of the wanted kind when an app and a resource of the system share the name. - An app's build bound counts its effective builds, so one declared beside an inherited build is a second build. - value.ref @output is the closed set its description lists. - An unresolved bare name says a node inside a system is named System.child (a deployment sits in no system, so its @from always is). New fixtures: delivery error-build-beside-inherited, railway error-value-ref-bare-from-and-bad-output; a unit test for the same-name case. --- packs/delivery/README.md | 7 ++- .../expected-errors.json | 15 ++++++ .../input/meta.shop.json | 41 ++++++++++++++++ .../providers.json | 1 + .../fixtures/expected-vocabulary.json | 9 ++++ packs/delivery/metamodel/delivery.json | 2 +- packs/delivery/typescript/src/index.ts | 18 +++++-- .../expected-errors.json | 25 ++++++++++ .../input/meta.shop.json | 47 +++++++++++++++++++ .../providers.json | 1 + packs/systems/README.md | 5 +- packs/systems/typescript/src/refs.ts | 26 ++++++++-- packs/systems/typescript/test/refs.test.ts | 13 +++++ 13 files changed, 195 insertions(+), 15 deletions(-) create mode 100644 packs/delivery/fixtures/conformance/error-build-beside-inherited/expected-errors.json create mode 100644 packs/delivery/fixtures/conformance/error-build-beside-inherited/input/meta.shop.json create mode 100644 packs/delivery/fixtures/conformance/error-build-beside-inherited/providers.json create mode 100644 packs/railway/fixtures/conformance/error-value-ref-bare-from-and-bad-output/expected-errors.json create mode 100644 packs/railway/fixtures/conformance/error-value-ref-bare-from-and-bad-output/input/meta.shop.json create mode 100644 packs/railway/fixtures/conformance/error-value-ref-bare-from-and-bad-output/providers.json diff --git a/packs/delivery/README.md b/packs/delivery/README.md index 4ac83ba325..6207d243c0 100644 --- a/packs/delivery/README.md +++ b/packs/delivery/README.md @@ -31,8 +31,11 @@ the config pack and the deployment names its vault in `@secrets`. The full defin - `@app` names an app as `System.app`, or `pkg::System.app` from another package. - `@environment` names an `environment.*` node. -- `value.ref`'s `@from` names a `resource.*` or `app.*` the same way; `@output` says which of its - values (url, host, port, privateUrl, ...), for a platform generator to map. This is how an address +- `value.ref`'s `@from` names a `resource.*` or `app.*` the same way. A deployment sits at the + document root, in no system, so these are always written `System.child`: a bare name finds + only a top-level node. `@output` says which of its + values (`url`, `host`, `port`, `user`, `password`, `privateUrl`, `publicUrl`), for a platform + generator to map. This is how an address stays out of a repository: it is named, never written. Each fails the load with `ERR_DELIVERY_UNRESOLVED_REF` when it resolves to nothing or to the wrong diff --git a/packs/delivery/fixtures/conformance/error-build-beside-inherited/expected-errors.json b/packs/delivery/fixtures/conformance/error-build-beside-inherited/expected-errors.json new file mode 100644 index 0000000000..4cb2533a02 --- /dev/null +++ b/packs/delivery/fixtures/conformance/error-build-beside-inherited/expected-errors.json @@ -0,0 +1,15 @@ +{ + "errors": [ + { + "code": "ERR_TOO_MANY_OCCURRENCES", + "source": { + "format": "json", + "files": [ + "meta.shop.json" + ], + "jsonPath": "$['metadata.root'].children[0]['system.internal'].children[1]['app.service'].children[0]['build.static']" + } + } + ], + "warnings": [] +} diff --git a/packs/delivery/fixtures/conformance/error-build-beside-inherited/input/meta.shop.json b/packs/delivery/fixtures/conformance/error-build-beside-inherited/input/meta.shop.json new file mode 100644 index 0000000000..28e55a8af5 --- /dev/null +++ b/packs/delivery/fixtures/conformance/error-build-beside-inherited/input/meta.shop.json @@ -0,0 +1,41 @@ +{ + "metadata.root": { + "package": "acme::shop", + "children": [ + { + "system.internal": { + "name": "Shop", + "children": [ + { + "app.service": { + "name": "base", + "children": [ + { + "build.command": { + "name": "jar", + "@command": "./gradlew bootJar" + } + } + ] + } + }, + { + "app.service": { + "name": "api", + "extends": "Shop.base", + "children": [ + { + "build.static": { + "name": "site", + "@directory": "dist" + } + } + ] + } + } + ] + } + } + ] + } +} diff --git a/packs/delivery/fixtures/conformance/error-build-beside-inherited/providers.json b/packs/delivery/fixtures/conformance/error-build-beside-inherited/providers.json new file mode 100644 index 0000000000..f6baea0511 --- /dev/null +++ b/packs/delivery/fixtures/conformance/error-build-beside-inherited/providers.json @@ -0,0 +1 @@ +["metaobjects-core-types", "metaobjects-documentation", "metaobjects-systems", "metaobjects-delivery"] diff --git a/packs/delivery/fixtures/expected-vocabulary.json b/packs/delivery/fixtures/expected-vocabulary.json index c93cc8b60d..bdfc6b7e94 100644 --- a/packs/delivery/fixtures/expected-vocabulary.json +++ b/packs/delivery/fixtures/expected-vocabulary.json @@ -310,6 +310,15 @@ "valueType": "string", "isArray": false, "required": true, + "allowedValues": [ + "url", + "host", + "port", + "user", + "password", + "privateUrl", + "publicUrl" + ], "description": "Which of its values: url, host, port, user, password, privateUrl, publicUrl. A platform generator maps the name to the platform's own reference and refuses one it cannot supply." }, { diff --git a/packs/delivery/metamodel/delivery.json b/packs/delivery/metamodel/delivery.json index 4be04c0d9f..272f0bf27b 100644 --- a/packs/delivery/metamodel/delivery.json +++ b/packs/delivery/metamodel/delivery.json @@ -104,7 +104,7 @@ "whenToUse": "A setting whose value only the platform knows once the resource or app exists.", "children": [ { "type": "attr", "subType": "string", "name": "from", "min": 1, "max": 1, "description": "The node that produces the value: a resource.* or app.*, as `System.child` (`Shop.db`, or `pkg::System.child`). It must resolve (ERR_DELIVERY_UNRESOLVED_REF)." }, - { "type": "attr", "subType": "string", "name": "output", "min": 1, "max": 1, "description": "Which of its values: url, host, port, user, password, privateUrl, publicUrl. A platform generator maps the name to the platform's own reference and refuses one it cannot supply." } + { "type": "attr", "subType": "string", "name": "output", "min": 1, "max": 1, "allowedValues": ["url", "host", "port", "user", "password", "privateUrl", "publicUrl"], "description": "Which of its values: url, host, port, user, password, privateUrl, publicUrl. A platform generator maps the name to the platform's own reference and refuses one it cannot supply." } ] } ], diff --git a/packs/delivery/typescript/src/index.ts b/packs/delivery/typescript/src/index.ts index 1a773a6b9f..5bbe54a561 100644 --- a/packs/delivery/typescript/src/index.ts +++ b/packs/delivery/typescript/src/index.ts @@ -52,15 +52,23 @@ const refuseAbstractDeployment: NodeValidator = (node, ctx) => { // An app has at most one build. The registry enforces a child rule's upper bound only per // subtype (`maxOccurs`), and a dockerfile beside a build command is two subtypes, so the -// bound across build.* is checked here, on every build after the first. +// bound across build.* is checked here. The bound is on the app's effective builds: one it +// declares beside one it inherits through `extends` is two. The error lands on the declared +// build that makes the second: the later of two declared here, or one declared beside an +// inherited build. const validateBuild: NodeValidator = (node, ctx) => { - // ADR-0039: own: the bound is on what this app declares; an inherited build was counted where it was declared. - const builds = node.parent?.ownChildren().filter((c) => c.type === TYPE_BUILD) ?? []; - if (builds.length > 1 && builds[0] !== node) { + const app = node.parent; + if (app === undefined) return; + // ADR-0039: resolving, so a build inherited through `extends` counts. + const others = app.children().filter((c) => c.type === TYPE_BUILD && c !== node); + // ADR-0039: own: the builds this app declares, to find the first of them. + const declared = app.ownChildren().filter((c) => c.type === TYPE_BUILD); + const inherited = others.some((c) => !declared.includes(c)); + if (inherited || declared[0] !== node) { ctx.error( "ERR_TOO_MANY_OCCURRENCES", node, - `${node.type}.${node.subType} "${node.name}" is a second build of ${node.parent?.type}.${node.parent?.subType} "${node.parent?.name}": an app has at most one build.`, + `${node.type}.${node.subType} "${node.name}" is a second build of ${app.type}.${app.subType} "${app.name}": an app has at most one build.`, ); } }; diff --git a/packs/railway/fixtures/conformance/error-value-ref-bare-from-and-bad-output/expected-errors.json b/packs/railway/fixtures/conformance/error-value-ref-bare-from-and-bad-output/expected-errors.json new file mode 100644 index 0000000000..4df4b4ffeb --- /dev/null +++ b/packs/railway/fixtures/conformance/error-value-ref-bare-from-and-bad-output/expected-errors.json @@ -0,0 +1,25 @@ +{ + "errors": [ + { + "code": "ERR_DELIVERY_UNRESOLVED_REF", + "source": { + "format": "json", + "files": [ + "meta.shop.json" + ], + "jsonPath": "$['metadata.root'].children[2]['deployment.railway'].children[0]['value.ref']" + } + }, + { + "code": "ERR_BAD_ATTR_VALUE", + "source": { + "format": "json", + "files": [ + "meta.shop.json" + ], + "jsonPath": "$['metadata.root'].children[2]['deployment.railway'].children[0]['value.ref']" + } + } + ], + "warnings": [] +} diff --git a/packs/railway/fixtures/conformance/error-value-ref-bare-from-and-bad-output/input/meta.shop.json b/packs/railway/fixtures/conformance/error-value-ref-bare-from-and-bad-output/input/meta.shop.json new file mode 100644 index 0000000000..a6d2f2b307 --- /dev/null +++ b/packs/railway/fixtures/conformance/error-value-ref-bare-from-and-bad-output/input/meta.shop.json @@ -0,0 +1,47 @@ +{ + "metadata.root": { + "package": "acme::shop", + "children": [ + { + "system.internal": { + "name": "Shop", + "children": [ + { + "app.service": { + "name": "api" + } + }, + { + "resource.database": { + "name": "db" + } + } + ] + } + }, + { + "environment.standing": { + "name": "production" + } + }, + { + "deployment.railway": { + "name": "api-production", + "@app": "Shop.api", + "@environment": "production", + "@project": "shop", + "children": [ + { + "value.ref": { + "name": "dbUrl", + "@setting": "Database.url", + "@from": "db", + "@output": "connectionString" + } + } + ] + } + } + ] + } +} diff --git a/packs/railway/fixtures/conformance/error-value-ref-bare-from-and-bad-output/providers.json b/packs/railway/fixtures/conformance/error-value-ref-bare-from-and-bad-output/providers.json new file mode 100644 index 0000000000..aac0d894bc --- /dev/null +++ b/packs/railway/fixtures/conformance/error-value-ref-bare-from-and-bad-output/providers.json @@ -0,0 +1 @@ +["metaobjects-core-types", "metaobjects-documentation", "metaobjects-systems", "metaobjects-delivery", "metaobjects-railway"] diff --git a/packs/systems/README.md b/packs/systems/README.md index 676f62d835..44d327500f 100644 --- a/packs/systems/README.md +++ b/packs/systems/README.md @@ -26,8 +26,9 @@ The full definition, with every attribute's description, is [`metamodel/systems. Apps and resources are children of a system, so they are addressed the way the loader addresses any nested node (ADR-0059): `System.child` (`Shop.db`), or `pkg::System.child` from another -package. A bare name means a node of the referrer's own system, then a top-level node of that name -in the referrer's package. +package. A bare name means a node of the referrer's own system (of the kind the attribute wants, +when an app and a resource share the name), then a top-level node of that name in the +referrer's package. This bare-name rule is the systems packs' convention, not the loader's. - `@needs` must name `resource.*` nodes, and `@calls` must name `app.*` or `system.external` nodes. A name that resolves to nothing, or to the wrong kind of node, fails the load with diff --git a/packs/systems/typescript/src/refs.ts b/packs/systems/typescript/src/refs.ts index cff6f5c596..3a7269460f 100644 --- a/packs/systems/typescript/src/refs.ts +++ b/packs/systems/typescript/src/refs.ts @@ -33,19 +33,31 @@ function enclosingSystem(node: MetaData): MetaData | undefined { * the package of the node that wrote it (ADR-0059 R3), so an inherited reference means * what it meant where it was written. */ -export function resolveSystemRef(from: MetaData, ref: string, attr?: string): MetaData | undefined { +export function resolveSystemRef( + from: MetaData, + ref: string, + attr?: string, + kinds?: readonly string[], +): MetaData | undefined { const top = topLevelNodeOf(from); const root = top.parent; if (root === undefined) return undefined; const pkg = attr === undefined ? effectivePackageOf(top) : referrerPackageFor(from, attr); if (!ref.includes(CHILD_REF_SEPARATOR) && !ref.includes(PACKAGE_SEPARATOR)) { // ADR-0039: resolving, so a system that `extends` another sees its inherited children. - const sibling = enclosingSystem(from)?.children().find((c) => c.name === ref); + // An app and a resource may share a name; given the kinds wanted, prefer one of them. + const named = enclosingSystem(from)?.children().filter((c) => c.name === ref) ?? []; + const sibling = (kinds === undefined ? undefined : named.find((c) => isKind(c, kinds))) ?? named[0]; if (sibling !== undefined) return sibling; } return resolveRef(root, ref, pkg).node; } +/** Whether `node` is one of `kinds` (each a type, or a `type.subType`). */ +function isKind(node: MetaData, kinds: readonly string[]): boolean { + return kinds.some((k) => k === node.type || k === `${node.type}.${node.subType}`); +} + /** The string entries of a string or string-array attribute (absent: none). */ export function refList(node: MetaData, attr: string): string[] { const raw = node.attr(attr); @@ -65,12 +77,16 @@ export function checkRefs( ctx: ValidationContext, ): void { for (const ref of refList(node, attr)) { - const target = resolveSystemRef(node, ref, attr); + const target = resolveSystemRef(node, ref, attr, kinds); const label = `${node.type}.${node.subType} "${node.name}" @${attr} "${ref}"`; const want = kinds.join(" or "); if (target === undefined) { - ctx.error(code, node, `${label} does not resolve to a ${want}.`); - } else if (!kinds.some((k) => k === target.type || k === `${target.type}.${target.subType}`)) { + // A bare name finds a node of the referrer's own system or a top-level node; one + // inside another system, or seen from outside any system, needs its system's name. + const bare = !ref.includes(CHILD_REF_SEPARATOR) && !ref.includes(PACKAGE_SEPARATOR); + const hint = bare ? ` A node inside a system is named System.${ref}.` : ""; + ctx.error(code, node, `${label} does not resolve to a ${want}.${hint}`); + } else if (!isKind(target, kinds)) { ctx.error(code, node, `${label} resolves to ${target.type}.${target.subType}, not a ${want}.`); } } diff --git a/packs/systems/typescript/test/refs.test.ts b/packs/systems/typescript/test/refs.test.ts index e13e36fe2d..c6119ee245 100644 --- a/packs/systems/typescript/test/refs.test.ts +++ b/packs/systems/typescript/test/refs.test.ts @@ -54,4 +54,17 @@ describe("resolveSystemRef", () => { expect(resolveSystemRef(api(root), "Billing.nope")).toBeUndefined(); expect(resolveSystemRef(api(root), "Dns")).toBeUndefined(); }); + + test("given the kinds wanted, a bare name prefers the sibling of that kind when an app and a resource share it", async () => { + const SAME = { "metadata.root": { package: "acme::shop", children: [ + { "system.internal": { name: "Shop", children: [ + { "app.service": { name: "worker", "@needs": ["worker"] } }, + { "resource.queue": { name: "worker" } }, + ] } }, + ] } }; + const loader = new MetaDataLoader({ registry: composeRegistry([...coreProviders, provider]), strict: true }); + const { root } = await loader.load([new InMemoryStringSource(JSON.stringify(SAME), { id: "meta.shop.json" })]); + const app = root.children()[0]!.children().find((c) => c.type === "app")!; + expect(resolveSystemRef(app, "worker", "needs", ["resource"])?.type).toBe("resource"); + }); }); From be894f24ff00b0a07a021226eeb717cc205282b6 Mon Sep 17 00:00:00 2001 From: Doug Mealing Date: Wed, 25 Nov 2026 02:48:30 -0500 Subject: [PATCH 4/6] no-mistakes(review): Bind refs by kind, rename @producedBy, add uniqueness checks --- changelog.d/independent-packs-skeleton.md | 4 +- packs/delivery/CHANGELOG.md | 6 +- packs/delivery/README.md | 12 +++- .../fixtures/conformance/ERROR-CODES.json | 4 +- .../fixtures/expected-vocabulary.json | 14 ++-- packs/delivery/metamodel/delivery.json | 2 +- packs/delivery/typescript/src/constants.ts | 10 ++- packs/delivery/typescript/src/index.ts | 64 +++++++++++++++++-- .../expected-errors.json | 15 +++++ .../input/meta.shop.json | 46 +++++++++++++ .../providers.json | 0 .../expected-errors.json | 15 +++++ .../input/meta.shop.json | 54 ++++++++++++++++ .../error-duplicate-setting/providers.json | 1 + .../expected-errors.json | 0 .../input/meta.shop.json | 2 +- .../providers.json | 1 + .../input/meta.shop.json | 4 +- .../railway-worked-example/expected.json | 2 +- .../input/meta.app-system.json | 2 +- packs/systems/typescript/src/refs.ts | 26 ++++++-- packs/systems/typescript/test/refs.test.ts | 52 +++++++++++++++ 22 files changed, 305 insertions(+), 31 deletions(-) create mode 100644 packs/railway/fixtures/conformance/error-duplicate-deployment/expected-errors.json create mode 100644 packs/railway/fixtures/conformance/error-duplicate-deployment/input/meta.shop.json rename packs/railway/fixtures/conformance/{error-value-ref-bare-from-and-bad-output => error-duplicate-deployment}/providers.json (100%) create mode 100644 packs/railway/fixtures/conformance/error-duplicate-setting/expected-errors.json create mode 100644 packs/railway/fixtures/conformance/error-duplicate-setting/input/meta.shop.json create mode 100644 packs/railway/fixtures/conformance/error-duplicate-setting/providers.json rename packs/railway/fixtures/conformance/{error-value-ref-bare-from-and-bad-output => error-value-ref-bare-producedby-and-bad-output}/expected-errors.json (100%) rename packs/railway/fixtures/conformance/{error-value-ref-bare-from-and-bad-output => error-value-ref-bare-producedby-and-bad-output}/input/meta.shop.json (96%) create mode 100644 packs/railway/fixtures/conformance/error-value-ref-bare-producedby-and-bad-output/providers.json diff --git a/changelog.d/independent-packs-skeleton.md b/changelog.d/independent-packs-skeleton.md index dbb6b41c75..76530133c2 100644 --- a/changelog.d/independent-packs-skeleton.md +++ b/changelog.d/independent-packs-skeleton.md @@ -1,7 +1,7 @@ - **Independent packs: the `packs/` directory, its gates and its lane (ADR-0069).** A capability that is not core vocabulary can now be built in `packs//` with its own version, changelog - and port list, outside the default bundle and outside `metamodelVersion`. No pack exists yet, - and nothing published changes. `scripts/check-pack-boundary.ts` fails a change that makes core + and port list, outside the default bundle and outside `metamodelVersion`. The packs that + exist are listed in `packs/README.md`, and nothing published changes. `scripts/check-pack-boundary.ts` fails a change that makes core depend on a pack, puts a pack in the bundle, or lets core's release tooling see `packs/`. `scripts/check-pack-manifests.ts` holds each `pack.json` to its ports' native manifests. `scripts/pack-vocabulary.ts` pins the rows a pack adds and fails one that changes the bundle's, diff --git a/packs/delivery/CHANGELOG.md b/packs/delivery/CHANGELOG.md index 016a14a3c2..252b8a1933 100644 --- a/packs/delivery/CHANGELOG.md +++ b/packs/delivery/CHANGELOG.md @@ -7,5 +7,7 @@ This pack's own changelog (ADR-0069). The root `CHANGELOG.md` records core only. - **First version.** `environment.standing`, `.ephemeral`, `.local`; `build.dockerfile`, `.command`, `.static` (at most one per app); `runtime.process`; the abstract `deployment.base` a platform pack specialises; `value.literal` and `value.ref`. A deployment's `@app` and - `@environment` and a `value.ref`'s `@from` are resolved at load (`ERR_DELIVERY_UNRESOLVED_REF`); - `@secrets` and `@setting` are names only. TypeScript port. + `@environment` and a `value.ref`'s `@producedBy` are resolved at load (`ERR_DELIVERY_UNRESOLVED_REF`); + `@secrets` and `@setting` are names only. A deployment holds one value per `@setting` + (`ERR_DELIVERY_DUPLICATE_SETTING`) and a model one deployment per (`@app`, `@environment`) + (`ERR_DELIVERY_DUPLICATE_DEPLOYMENT`). TypeScript port. diff --git a/packs/delivery/README.md b/packs/delivery/README.md index 6207d243c0..b986b87d2c 100644 --- a/packs/delivery/README.md +++ b/packs/delivery/README.md @@ -19,7 +19,7 @@ in one environment on one platform, and holds only what differs there. | `runtime.process` | under an `app.*` | How one of its processes runs | `@command`, `@port`, `@healthPath`, `@healthTimeoutSeconds`, `@drainSeconds`, `@restart` (`always`, `onFailure`, `never`) | | `deployment.` | document root | An app in an environment on a platform. A **platform pack** registers the subtype (the [`railway`](../railway/README.md) pack registers `deployment.railway`) | `@app`, `@environment`, `@domains`, `@replicas`, `@region`, `@branch`, `@secrets`, plus the platform's own | | `value.literal` | under a deployment | A setting's fixed value there | `@setting`, `@literal` | -| `value.ref` | under a deployment | A setting's value produced by another node at deploy time | `@setting`, `@from`, `@output` | +| `value.ref` | under a deployment | A setting's value produced by another node at deploy time | `@setting`, `@producedBy`, `@output` | `deployment.base` is the abstract anchor platform packs specialise (`extendsBase: true`); this pack registers no concrete deployment, and an authored `deployment.base` fails to load @@ -31,13 +31,21 @@ the config pack and the deployment names its vault in `@secrets`. The full defin - `@app` names an app as `System.app`, or `pkg::System.app` from another package. - `@environment` names an `environment.*` node. -- `value.ref`'s `@from` names a `resource.*` or `app.*` the same way. A deployment sits at the +- `value.ref`'s `@producedBy` names a `resource.*` or `app.*` the same way. A deployment sits at the document root, in no system, so these are always written `System.child`: a bare name finds only a top-level node. `@output` says which of its values (`url`, `host`, `port`, `user`, `password`, `privateUrl`, `publicUrl`), for a platform generator to map. This is how an address stays out of a repository: it is named, never written. +## Uniqueness + +- A deployment holds at most one `value.*` per `@setting` (`ERR_DELIVERY_DUPLICATE_SETTING`). An estate + overlay that overrides a value has to reuse the value node's name so the overlay merges it; a + second, differently named value for the same setting is an error. +- A model holds at most one deployment per (`@app`, `@environment`), compared as resolved nodes, so + `Game.server` and `acme::game::Game.server` collide (`ERR_DELIVERY_DUPLICATE_DEPLOYMENT`). + Each fails the load with `ERR_DELIVERY_UNRESOLVED_REF` when it resolves to nothing or to the wrong kind of node. A platform pack holds its deployments to the same rule by passing `validateDeployment` as its subtype's validator. `@secrets` (a config pack `vault.*`) and diff --git a/packs/delivery/fixtures/conformance/ERROR-CODES.json b/packs/delivery/fixtures/conformance/ERROR-CODES.json index e6a9c10be1..1fc83ab605 100644 --- a/packs/delivery/fixtures/conformance/ERROR-CODES.json +++ b/packs/delivery/fixtures/conformance/ERROR-CODES.json @@ -1,6 +1,8 @@ { "version": 1, "codes": { - "ERR_DELIVERY_UNRESOLVED_REF": "A deployment's @app or @environment, or a value.ref's @from, names no node, or names a node of the wrong kind (@app takes an app.*, @environment an environment.*, @from a resource.* or an app.*)." + "ERR_DELIVERY_UNRESOLVED_REF": "A deployment's @app or @environment, or a value.ref's @producedBy, names no node, or names a node of the wrong kind (@app takes an app.*, @environment an environment.*, @producedBy a resource.* or an app.*).", + "ERR_DELIVERY_DUPLICATE_SETTING": "A deployment holds two value.* nodes for one @setting. An estate overlay that overrides a value must reuse the value node's name so it merges.", + "ERR_DELIVERY_DUPLICATE_DEPLOYMENT": "Two deployments target the same (@app, @environment), compared as resolved nodes." } } diff --git a/packs/delivery/fixtures/expected-vocabulary.json b/packs/delivery/fixtures/expected-vocabulary.json index bdfc6b7e94..465adba7b7 100644 --- a/packs/delivery/fixtures/expected-vocabulary.json +++ b/packs/delivery/fixtures/expected-vocabulary.json @@ -298,13 +298,6 @@ "description": "The setting takes a value another node produces at deploy time: a database's connection URL, another app's private address. This is how an address stays out of a repository: it is named, never written.", "whenToUse": "A setting whose value only the platform knows once the resource or app exists.", "attrs": [ - { - "name": "from", - "valueType": "string", - "isArray": false, - "required": true, - "description": "The node that produces the value: a resource.* or app.*, as `System.child` (`Shop.db`, or `pkg::System.child`). It must resolve (ERR_DELIVERY_UNRESOLVED_REF)." - }, { "name": "output", "valueType": "string", @@ -321,6 +314,13 @@ ], "description": "Which of its values: url, host, port, user, password, privateUrl, publicUrl. A platform generator maps the name to the platform's own reference and refuses one it cannot supply." }, + { + "name": "producedBy", + "valueType": "string", + "isArray": false, + "required": true, + "description": "The node that produces the value: a resource.* or app.*, as `System.child` (`Shop.db`, or `pkg::System.child`). It must resolve (ERR_DELIVERY_UNRESOLVED_REF)." + }, { "name": "setting", "valueType": "string", diff --git a/packs/delivery/metamodel/delivery.json b/packs/delivery/metamodel/delivery.json index 272f0bf27b..342498fc15 100644 --- a/packs/delivery/metamodel/delivery.json +++ b/packs/delivery/metamodel/delivery.json @@ -103,7 +103,7 @@ "description": "The setting takes a value another node produces at deploy time: a database's connection URL, another app's private address. This is how an address stays out of a repository: it is named, never written.", "whenToUse": "A setting whose value only the platform knows once the resource or app exists.", "children": [ - { "type": "attr", "subType": "string", "name": "from", "min": 1, "max": 1, "description": "The node that produces the value: a resource.* or app.*, as `System.child` (`Shop.db`, or `pkg::System.child`). It must resolve (ERR_DELIVERY_UNRESOLVED_REF)." }, + { "type": "attr", "subType": "string", "name": "producedBy", "min": 1, "max": 1, "description": "The node that produces the value: a resource.* or app.*, as `System.child` (`Shop.db`, or `pkg::System.child`). It must resolve (ERR_DELIVERY_UNRESOLVED_REF)." }, { "type": "attr", "subType": "string", "name": "output", "min": 1, "max": 1, "allowedValues": ["url", "host", "port", "user", "password", "privateUrl", "publicUrl"], "description": "Which of its values: url, host, port, user, password, privateUrl, publicUrl. A platform generator maps the name to the platform's own reference and refuses one it cannot supply." } ] } diff --git a/packs/delivery/typescript/src/constants.ts b/packs/delivery/typescript/src/constants.ts index 24aebb8c87..257f5b2188 100644 --- a/packs/delivery/typescript/src/constants.ts +++ b/packs/delivery/typescript/src/constants.ts @@ -31,8 +31,14 @@ export const VALUE_SUBTYPE_LITERAL = "literal"; export const VALUE_SUBTYPE_REF = "ref"; export const VALUE_ATTR_SETTING = "setting"; export const VALUE_ATTR_LITERAL = "literal"; -export const VALUE_ATTR_FROM = "from"; +export const VALUE_ATTR_PRODUCED_BY = "producedBy"; export const VALUE_ATTR_OUTPUT = "output"; -/** Raised when a deployment's @app / @environment or a value.ref's @from names nothing, or the wrong kind of node. */ +/** Raised when a deployment's @app / @environment or a value.ref's @producedBy names nothing, or the wrong kind of node. */ export const ERR_DELIVERY_UNRESOLVED_REF = "ERR_DELIVERY_UNRESOLVED_REF"; + +/** Raised when a deployment holds two values for one @setting. */ +export const ERR_DELIVERY_DUPLICATE_SETTING = "ERR_DELIVERY_DUPLICATE_SETTING"; + +/** Raised when two deployments target the same (@app, @environment). */ +export const ERR_DELIVERY_DUPLICATE_DEPLOYMENT = "ERR_DELIVERY_DUPLICATE_DEPLOYMENT"; diff --git a/packs/delivery/typescript/src/index.ts b/packs/delivery/typescript/src/index.ts index 5bbe54a561..1631503f10 100644 --- a/packs/delivery/typescript/src/index.ts +++ b/packs/delivery/typescript/src/index.ts @@ -1,7 +1,7 @@ // @metaobjectsdev/pack-delivery: what runs where, and what differs per environment // (ADR-0069 independent pack). The vocabulary is metamodel/delivery.json; this module // registers it and checks the references the data cannot: a deployment's @app and -// @environment, and a value.ref's @from. +// @environment, and a value.ref's @producedBy. // // The pack registers `deployment.base` and no concrete deployment: the platform is the // subtype, and a platform pack registers it (`deployment.`, `extendsBase: true`). @@ -10,33 +10,73 @@ import { applyProviderDefinition, + topLevelNodeOf, + type MetaData, type MetaDataTypeProvider, type NodeValidator, type ProviderDefinition, type TypeRegistry, + type ValidationContext, } from "@metaobjectsdev/metadata"; -import { SYSTEMS_PROVIDER_ID, TYPE_APP, TYPE_RESOURCE, checkRefs, packTypeMaps } from "@metaobjectsdev/pack-systems"; +import { SYSTEMS_PROVIDER_ID, TYPE_APP, TYPE_RESOURCE, checkRefs, packTypeMaps, refList, resolveSystemRef } from "@metaobjectsdev/pack-systems"; import definition from "../../metamodel/delivery.json" with { type: "json" }; import { DELIVERY_PROVIDER_ID, DEPLOYMENT_ATTR_APP, DEPLOYMENT_ATTR_ENVIRONMENT, + ERR_DELIVERY_DUPLICATE_DEPLOYMENT, + ERR_DELIVERY_DUPLICATE_SETTING, ERR_DELIVERY_UNRESOLVED_REF, TYPE_BUILD, TYPE_DEPLOYMENT, TYPE_ENVIRONMENT, TYPE_VALUE, - VALUE_ATTR_FROM, + VALUE_ATTR_PRODUCED_BY, + VALUE_ATTR_SETTING, } from "./constants.js"; export * from "./constants.js"; export const DELIVERY_DEFINITION = definition as ProviderDefinition; +// The (app, environment) a deployment targets, as resolved nodes, so `Game.server` and +// `acme::game::Game.server` are the same app. Undefined when either does not resolve (the +// reference check reports that). +function deploymentTarget(node: MetaData): [MetaData, MetaData] | undefined { + const app = refList(node, DEPLOYMENT_ATTR_APP)[0]; + const env = refList(node, DEPLOYMENT_ATTR_ENVIRONMENT)[0]; + if (app === undefined || env === undefined) return undefined; + const a = resolveSystemRef(node, app, DEPLOYMENT_ATTR_APP, [TYPE_APP]); + const e = resolveSystemRef(node, env, DEPLOYMENT_ATTR_ENVIRONMENT, [TYPE_ENVIRONMENT]); + return a === undefined || e === undefined ? undefined : [a, e]; +} + +// An app runs once per environment: two deployments for one (app, environment) leave a +// generator to pick by load order. The error lands on the later one. +function checkDeploymentUnique(node: MetaData, ctx: ValidationContext): void { + const mine = deploymentTarget(node); + const root = topLevelNodeOf(node).parent; + if (mine === undefined || root === undefined) return; + // ADR-0039: resolving, the deployments the model effectively holds. + const deployments = root.children().filter((c) => c.type === TYPE_DEPLOYMENT); + const first = deployments.find((d) => { + const t = deploymentTarget(d); + return t !== undefined && t[0] === mine[0] && t[1] === mine[1]; + }); + if (first !== undefined && first !== node) { + ctx.error( + ERR_DELIVERY_DUPLICATE_DEPLOYMENT, + node, + `${node.type}.${node.subType} "${node.name}" deploys the same app in the same environment as "${first.name}": a model has at most one deployment per (@app, @environment).`, + ); + } +} + /** The validator a platform pack attaches to its `deployment.` subtype. */ export const validateDeployment: NodeValidator = (node, ctx) => { checkRefs(node, DEPLOYMENT_ATTR_APP, [TYPE_APP], ERR_DELIVERY_UNRESOLVED_REF, ctx); checkRefs(node, DEPLOYMENT_ATTR_ENVIRONMENT, [TYPE_ENVIRONMENT], ERR_DELIVERY_UNRESOLVED_REF, ctx); + checkDeploymentUnique(node, ctx); }; // `deployment.base` is abstract, but the parser treats a `.base` as abstract only @@ -73,8 +113,24 @@ const validateBuild: NodeValidator = (node, ctx) => { } }; +// A deployment sets each setting once. An estate overlay that overrides a value must reuse +// the value node's name so the overlay merges into it; a second, differently named value +// for the same @setting would sit beside the first, so it is an error. It lands on the later. const validateValue: NodeValidator = (node, ctx) => { - checkRefs(node, VALUE_ATTR_FROM, [TYPE_RESOURCE, TYPE_APP], ERR_DELIVERY_UNRESOLVED_REF, ctx); + const setting = node.attr(VALUE_ATTR_SETTING); + const deployment = node.parent; + if (typeof setting === "string" && deployment !== undefined) { + // ADR-0039: resolving, so a value a deployment inherits counts. + const first = deployment.children().find((c) => c.type === TYPE_VALUE && c.attr(VALUE_ATTR_SETTING) === setting); + if (first !== undefined && first !== node) { + ctx.error( + ERR_DELIVERY_DUPLICATE_SETTING, + node, + `${node.type}.${node.subType} "${node.name}" sets "${setting}", which "${first.name}" already sets in ${deployment.type}.${deployment.subType} "${deployment.name}". An estate overlay overriding a value has to reuse the value node's name so the overlay merges it; a second, differently named value for the same @setting is an error.`, + ); + } + } + checkRefs(node, VALUE_ATTR_PRODUCED_BY, [TYPE_RESOURCE, TYPE_APP], ERR_DELIVERY_UNRESOLVED_REF, ctx); }; export const provider: MetaDataTypeProvider = { diff --git a/packs/railway/fixtures/conformance/error-duplicate-deployment/expected-errors.json b/packs/railway/fixtures/conformance/error-duplicate-deployment/expected-errors.json new file mode 100644 index 0000000000..eec28e55c9 --- /dev/null +++ b/packs/railway/fixtures/conformance/error-duplicate-deployment/expected-errors.json @@ -0,0 +1,15 @@ +{ + "errors": [ + { + "code": "ERR_DELIVERY_DUPLICATE_DEPLOYMENT", + "source": { + "format": "json", + "files": [ + "meta.shop.json" + ], + "jsonPath": "$['metadata.root'].children[3]['deployment.railway']" + } + } + ], + "warnings": [] +} diff --git a/packs/railway/fixtures/conformance/error-duplicate-deployment/input/meta.shop.json b/packs/railway/fixtures/conformance/error-duplicate-deployment/input/meta.shop.json new file mode 100644 index 0000000000..c55ef8a9a7 --- /dev/null +++ b/packs/railway/fixtures/conformance/error-duplicate-deployment/input/meta.shop.json @@ -0,0 +1,46 @@ +{ + "metadata.root": { + "package": "acme::shop", + "children": [ + { + "system.internal": { + "name": "Shop", + "children": [ + { + "app.service": { + "name": "api" + } + }, + { + "resource.database": { + "name": "db", + "@engine": "postgres" + } + } + ] + } + }, + { + "environment.standing": { + "name": "production" + } + }, + { + "deployment.railway": { + "name": "api-production", + "@app": "Shop.api", + "@environment": "production", + "@project": "shop" + } + }, + { + "deployment.railway": { + "name": "api-production-again", + "@app": "acme::shop::Shop.api", + "@environment": "production", + "@project": "shop" + } + } + ] + } +} diff --git a/packs/railway/fixtures/conformance/error-value-ref-bare-from-and-bad-output/providers.json b/packs/railway/fixtures/conformance/error-duplicate-deployment/providers.json similarity index 100% rename from packs/railway/fixtures/conformance/error-value-ref-bare-from-and-bad-output/providers.json rename to packs/railway/fixtures/conformance/error-duplicate-deployment/providers.json diff --git a/packs/railway/fixtures/conformance/error-duplicate-setting/expected-errors.json b/packs/railway/fixtures/conformance/error-duplicate-setting/expected-errors.json new file mode 100644 index 0000000000..36f2614d4a --- /dev/null +++ b/packs/railway/fixtures/conformance/error-duplicate-setting/expected-errors.json @@ -0,0 +1,15 @@ +{ + "errors": [ + { + "code": "ERR_DELIVERY_DUPLICATE_SETTING", + "source": { + "format": "json", + "files": [ + "meta.shop.json" + ], + "jsonPath": "$['metadata.root'].children[2]['deployment.railway'].children[1]['value.literal']" + } + } + ], + "warnings": [] +} diff --git a/packs/railway/fixtures/conformance/error-duplicate-setting/input/meta.shop.json b/packs/railway/fixtures/conformance/error-duplicate-setting/input/meta.shop.json new file mode 100644 index 0000000000..75bdf93a4e --- /dev/null +++ b/packs/railway/fixtures/conformance/error-duplicate-setting/input/meta.shop.json @@ -0,0 +1,54 @@ +{ + "metadata.root": { + "package": "acme::shop", + "children": [ + { + "system.internal": { + "name": "Shop", + "children": [ + { + "app.service": { + "name": "api" + } + }, + { + "resource.database": { + "name": "db", + "@engine": "postgres" + } + } + ] + } + }, + { + "environment.standing": { + "name": "production" + } + }, + { + "deployment.railway": { + "name": "api-production", + "@app": "Shop.api", + "@environment": "production", + "@project": "shop", + "children": [ + { + "value.literal": { + "name": "profile", + "@setting": "Spring.profile", + "@literal": "prod" + } + }, + { + "value.literal": { + "name": "activeProfile", + "@setting": "Spring.profile", + "@literal": "live" + } + } + ] + } + } + ] + } +} diff --git a/packs/railway/fixtures/conformance/error-duplicate-setting/providers.json b/packs/railway/fixtures/conformance/error-duplicate-setting/providers.json new file mode 100644 index 0000000000..aac0d894bc --- /dev/null +++ b/packs/railway/fixtures/conformance/error-duplicate-setting/providers.json @@ -0,0 +1 @@ +["metaobjects-core-types", "metaobjects-documentation", "metaobjects-systems", "metaobjects-delivery", "metaobjects-railway"] diff --git a/packs/railway/fixtures/conformance/error-value-ref-bare-from-and-bad-output/expected-errors.json b/packs/railway/fixtures/conformance/error-value-ref-bare-producedby-and-bad-output/expected-errors.json similarity index 100% rename from packs/railway/fixtures/conformance/error-value-ref-bare-from-and-bad-output/expected-errors.json rename to packs/railway/fixtures/conformance/error-value-ref-bare-producedby-and-bad-output/expected-errors.json diff --git a/packs/railway/fixtures/conformance/error-value-ref-bare-from-and-bad-output/input/meta.shop.json b/packs/railway/fixtures/conformance/error-value-ref-bare-producedby-and-bad-output/input/meta.shop.json similarity index 96% rename from packs/railway/fixtures/conformance/error-value-ref-bare-from-and-bad-output/input/meta.shop.json rename to packs/railway/fixtures/conformance/error-value-ref-bare-producedby-and-bad-output/input/meta.shop.json index a6d2f2b307..a6d7a2036b 100644 --- a/packs/railway/fixtures/conformance/error-value-ref-bare-from-and-bad-output/input/meta.shop.json +++ b/packs/railway/fixtures/conformance/error-value-ref-bare-producedby-and-bad-output/input/meta.shop.json @@ -35,7 +35,7 @@ "value.ref": { "name": "dbUrl", "@setting": "Database.url", - "@from": "db", + "@producedBy": "db", "@output": "connectionString" } } diff --git a/packs/railway/fixtures/conformance/error-value-ref-bare-producedby-and-bad-output/providers.json b/packs/railway/fixtures/conformance/error-value-ref-bare-producedby-and-bad-output/providers.json new file mode 100644 index 0000000000..aac0d894bc --- /dev/null +++ b/packs/railway/fixtures/conformance/error-value-ref-bare-producedby-and-bad-output/providers.json @@ -0,0 +1 @@ +["metaobjects-core-types", "metaobjects-documentation", "metaobjects-systems", "metaobjects-delivery", "metaobjects-railway"] diff --git a/packs/railway/fixtures/conformance/error-value-ref-unresolved/input/meta.shop.json b/packs/railway/fixtures/conformance/error-value-ref-unresolved/input/meta.shop.json index 56e7b012bf..b3c365411f 100644 --- a/packs/railway/fixtures/conformance/error-value-ref-unresolved/input/meta.shop.json +++ b/packs/railway/fixtures/conformance/error-value-ref-unresolved/input/meta.shop.json @@ -47,7 +47,7 @@ "value.ref": { "name": "dbUrl", "@setting": "Database.url", - "@from": "Shop.database", + "@producedBy": "Shop.database", "@output": "url" } }, @@ -55,7 +55,7 @@ "value.ref": { "name": "payments", "@setting": "Payments.url", - "@from": "Shop.Payments", + "@producedBy": "Shop.Payments", "@output": "url" } } diff --git a/packs/railway/fixtures/conformance/railway-worked-example/expected.json b/packs/railway/fixtures/conformance/railway-worked-example/expected.json index e63b42f7c1..b71900103a 100644 --- a/packs/railway/fixtures/conformance/railway-worked-example/expected.json +++ b/packs/railway/fixtures/conformance/railway-worked-example/expected.json @@ -112,8 +112,8 @@ { "value.ref": { "name": "dbUrl", - "@from": "Game.db", "@output": "url", + "@producedBy": "Game.db", "@setting": "Database.url" } }, diff --git a/packs/railway/fixtures/conformance/railway-worked-example/input/meta.app-system.json b/packs/railway/fixtures/conformance/railway-worked-example/input/meta.app-system.json index e9318f35f4..eb202b4338 100644 --- a/packs/railway/fixtures/conformance/railway-worked-example/input/meta.app-system.json +++ b/packs/railway/fixtures/conformance/railway-worked-example/input/meta.app-system.json @@ -45,7 +45,7 @@ "@domains": ["app.example.com", "play.example.com"], "@secrets": "keys", "children": [ - { "value.ref": { "name": "dbUrl", "@setting": "Database.url", "@from": "Game.db", "@output": "url" } }, + { "value.ref": { "name": "dbUrl", "@setting": "Database.url", "@producedBy": "Game.db", "@output": "url" } }, { "value.literal": { "name": "profile", "@setting": "Spring.profile", "@literal": "prod" } } ] } } diff --git a/packs/systems/typescript/src/refs.ts b/packs/systems/typescript/src/refs.ts index 3a7269460f..2415554d99 100644 --- a/packs/systems/typescript/src/refs.ts +++ b/packs/systems/typescript/src/refs.ts @@ -14,11 +14,12 @@ import { effectivePackageOf, referrerPackageFor, resolveRef, + splitChildTail, topLevelNodeOf, type MetaData, type ValidationContext, } from "@metaobjectsdev/metadata"; -import { TYPE_SYSTEM } from "./constants.js"; +import { TYPE_APP, TYPE_RESOURCE, TYPE_SYSTEM } from "./constants.js"; /** The nearest enclosing system of `node`, or undefined when it sits in none. */ function enclosingSystem(node: MetaData): MetaData | undefined { @@ -50,6 +51,20 @@ export function resolveSystemRef( const sibling = (kinds === undefined ? undefined : named.find((c) => isKind(c, kinds))) ?? named[0]; if (sibling !== undefined) return sibling; } + if (splitChildTail(ref).tail !== "") { + // `System.child`: the owner is a system, and among the owner's children of that name the + // wanted kind wins, as it does for a bare sibling. + const hit = resolveRef(root, ref, pkg, { ownerType: TYPE_SYSTEM }).node; + if (hit === undefined || kinds === undefined || isKind(hit, kinds)) return hit; + return hit.parent?.children().find((c) => c.name === hit.name && isKind(c, kinds)) ?? hit; + } + // A top-level name: a node of the wanted kinds wins over a same-named node of another type. + // None of those: take any node, so the caller reports the wrong kind it found. + const types = kinds === undefined ? [] : [...new Set(kinds.map((k) => k.split(".")[0]!))]; + for (const ownerType of types) { + const hit = resolveRef(root, ref, pkg, { ownerType }).node; + if (hit !== undefined && isKind(hit, kinds!)) return hit; + } return resolveRef(root, ref, pkg).node; } @@ -79,15 +94,16 @@ export function checkRefs( for (const ref of refList(node, attr)) { const target = resolveSystemRef(node, ref, attr, kinds); const label = `${node.type}.${node.subType} "${node.name}" @${attr} "${ref}"`; - const want = kinds.join(" or "); + const want = kinds.map((k) => `${/^[aeiou]/.test(k) ? "an" : "a"} ${k}`).join(" or "); if (target === undefined) { // A bare name finds a node of the referrer's own system or a top-level node; one // inside another system, or seen from outside any system, needs its system's name. const bare = !ref.includes(CHILD_REF_SEPARATOR) && !ref.includes(PACKAGE_SEPARATOR); - const hint = bare ? ` A node inside a system is named System.${ref}.` : ""; - ctx.error(code, node, `${label} does not resolve to a ${want}.${hint}`); + const inSystem = kinds.some((k) => k === TYPE_APP || k === TYPE_RESOURCE || k.startsWith(`${TYPE_APP}.`) || k.startsWith(`${TYPE_RESOURCE}.`)); + const hint = bare && inSystem ? ` A node inside a system is named System.${ref}.` : ""; + ctx.error(code, node, `${label} does not resolve to ${want}.${hint}`); } else if (!isKind(target, kinds)) { - ctx.error(code, node, `${label} resolves to ${target.type}.${target.subType}, not a ${want}.`); + ctx.error(code, node, `${label} resolves to ${target.type}.${target.subType}, not ${want}.`); } } } diff --git a/packs/systems/typescript/test/refs.test.ts b/packs/systems/typescript/test/refs.test.ts index c6119ee245..b9129faf25 100644 --- a/packs/systems/typescript/test/refs.test.ts +++ b/packs/systems/typescript/test/refs.test.ts @@ -68,3 +68,55 @@ describe("resolveSystemRef", () => { expect(resolveSystemRef(app, "worker", "needs", ["resource"])?.type).toBe("resource"); }); }); + +async function loadModel(children: unknown[]): Promise { + const loader = new MetaDataLoader({ registry: composeRegistry([...coreProviders, provider]), strict: true }); + const doc = { "metadata.root": { package: "acme::game", children } }; + const { root } = await loader.load([new InMemoryStringSource(JSON.stringify(doc), { id: "meta.game.json" })]); + return root; +} + +describe("resolveSystemRef binds by type and kind", () => { + test("a dotted ref binds the system, not a same-named entity declared first", async () => { + const root = await loadModel([ + { "object.entity": { name: "Game" } }, + { "system.internal": { name: "Game", children: [{ "app.service": { name: "server" } }] } }, + { "system.internal": { name: "Other", children: [{ "app.job": { name: "j", "@calls": ["Game.server"] } }] } }, + ]); + const job = root.children().find((c) => c.name === "Other")!.children()[0]!; + expect(resolveSystemRef(job, "Game.server", "calls", ["app", "system.external"])?.type).toBe("app"); + expect(resolveSystemRef(job, "acme::game::Game.server", "calls", ["app"])?.type).toBe("app"); + }); + + test("a top-level name prefers a node of the wanted kind over a same-named object declared first", async () => { + const root = await loadModel([ + { "object.entity": { name: "Mail" } }, + { "system.external": { name: "Mail" } }, + { "system.internal": { name: "Shop", children: [{ "app.service": { name: "api", "@calls": ["Mail"] } }] } }, + ]); + const api = root.children().find((c) => c.name === "Shop")!.children()[0]!; + expect(resolveSystemRef(api, "Mail", "calls", ["app", "system.external"])?.type).toBe("system"); + }); + + test("a top-level name of no wanted kind still resolves, so the caller can name the wrong kind", async () => { + const root = await loadModel([ + { "object.entity": { name: "Mail" } }, + { "system.internal": { name: "Shop", children: [{ "app.service": { name: "api" } }] } }, + ]); + const api = root.children().find((c) => c.name === "Shop")!.children()[0]!; + expect(resolveSystemRef(api, "Mail", "calls", ["app", "system.external"])?.type).toBe("object"); + }); + + test("a dotted ref prefers the wanted kind when a resource and an app share the name", async () => { + const root = await loadModel([ + { "system.internal": { name: "Shop", children: [ + { "resource.queue": { name: "worker" } }, + { "app.job": { name: "worker" } }, + ] } }, + { "system.internal": { name: "Other", children: [{ "app.service": { name: "api" } }] } }, + ]); + const api = root.children().find((c) => c.name === "Other")!.children()[0]!; + expect(resolveSystemRef(api, "Shop.worker", "calls", ["app"])?.type).toBe("app"); + expect(resolveSystemRef(api, "Shop.worker", "needs", ["resource"])?.type).toBe("resource"); + }); +}); From cd45f1ba74dbf7a58a74ea9eea53f87f3f2cd367 Mon Sep 17 00:00:00 2001 From: Doug Mealing Date: Wed, 25 Nov 2026 02:49:57 -0500 Subject: [PATCH 5/6] no-mistakes(review): Skip wrong-kind sibling before top-level lookup --- packs/systems/typescript/src/refs.ts | 14 +++++++++---- packs/systems/typescript/test/refs.test.ts | 23 ++++++++++++++++++++++ 2 files changed, 33 insertions(+), 4 deletions(-) diff --git a/packs/systems/typescript/src/refs.ts b/packs/systems/typescript/src/refs.ts index 2415554d99..a66eecf8d5 100644 --- a/packs/systems/typescript/src/refs.ts +++ b/packs/systems/typescript/src/refs.ts @@ -44,12 +44,18 @@ export function resolveSystemRef( const root = top.parent; if (root === undefined) return undefined; const pkg = attr === undefined ? effectivePackageOf(top) : referrerPackageFor(from, attr); + let wrongKindSibling: MetaData | undefined; if (!ref.includes(CHILD_REF_SEPARATOR) && !ref.includes(PACKAGE_SEPARATOR)) { // ADR-0039: resolving, so a system that `extends` another sees its inherited children. // An app and a resource may share a name; given the kinds wanted, prefer one of them. const named = enclosingSystem(from)?.children().filter((c) => c.name === ref) ?? []; - const sibling = (kinds === undefined ? undefined : named.find((c) => isKind(c, kinds))) ?? named[0]; - if (sibling !== undefined) return sibling; + if (kinds === undefined) { + if (named[0] !== undefined) return named[0]; + } else { + const sibling = named.find((c) => isKind(c, kinds)); + if (sibling !== undefined) return sibling; + wrongKindSibling = named[0]; + } } if (splitChildTail(ref).tail !== "") { // `System.child`: the owner is a system, and among the owner's children of that name the @@ -59,13 +65,13 @@ export function resolveSystemRef( return hit.parent?.children().find((c) => c.name === hit.name && isKind(c, kinds)) ?? hit; } // A top-level name: a node of the wanted kinds wins over a same-named node of another type. - // None of those: take any node, so the caller reports the wrong kind it found. + // None of those: take a sibling or node of another kind, so the caller reports the wrong kind it found. const types = kinds === undefined ? [] : [...new Set(kinds.map((k) => k.split(".")[0]!))]; for (const ownerType of types) { const hit = resolveRef(root, ref, pkg, { ownerType }).node; if (hit !== undefined && isKind(hit, kinds!)) return hit; } - return resolveRef(root, ref, pkg).node; + return wrongKindSibling ?? resolveRef(root, ref, pkg).node; } /** Whether `node` is one of `kinds` (each a type, or a `type.subType`). */ diff --git a/packs/systems/typescript/test/refs.test.ts b/packs/systems/typescript/test/refs.test.ts index b9129faf25..a3315b5b7a 100644 --- a/packs/systems/typescript/test/refs.test.ts +++ b/packs/systems/typescript/test/refs.test.ts @@ -119,4 +119,27 @@ describe("resolveSystemRef binds by type and kind", () => { expect(resolveSystemRef(api, "Shop.worker", "calls", ["app"])?.type).toBe("app"); expect(resolveSystemRef(api, "Shop.worker", "needs", ["resource"])?.type).toBe("resource"); }); + + test("a bare name skips a sibling of the wrong kind for a top-level node of the wanted kind", async () => { + const root = await loadModel([ + { "system.internal": { name: "Shop", children: [ + { "resource.cache": { name: "Mail" } }, + { "app.service": { name: "api", "@calls": ["Mail"] } }, + ] } }, + { "system.external": { name: "Mail" } }, + ]); + const api = root.children().find((c) => c.name === "Shop")!.children().find((c) => c.type === "app")!; + expect(resolveSystemRef(api, "Mail", "calls", ["app", "system.external"])?.type).toBe("system"); + }); + + test("a bare name with only a wrong-kind sibling still resolves to it, so the caller can name the wrong kind", async () => { + const root = await loadModel([ + { "system.internal": { name: "Shop", children: [ + { "resource.cache": { name: "Mail" } }, + { "app.service": { name: "api" } }, + ] } }, + ]); + const api = root.children().find((c) => c.name === "Shop")!.children().find((c) => c.type === "app")!; + expect(resolveSystemRef(api, "Mail", "calls", ["app", "system.external"])?.type).toBe("resource"); + }); }); From 3ef8be70977147fb1809f7bc4165d58b46513882 Mon Sep 17 00:00:00 2001 From: Doug Mealing Date: Wed, 25 Nov 2026 03:03:56 -0500 Subject: [PATCH 6/6] no-mistakes(document): Point README packs tree at packs/README.md --- README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/README.md b/README.md index b98068d97a..bbfad24768 100644 --- a/README.md +++ b/README.md @@ -324,7 +324,7 @@ metaobjects/ │ ├── ai/ # the LLM-call trace envelope (stable) │ └── iam/ # users, groups, roles, grants (preview) ├── packs/ # independent packs, outside the default bundle, each with its -│ # own version (ADR-0069; none shipped yet) +│ # own version (ADR-0069; listed in packs/README.md) ├── templates/ # canonical api/docs Mustache templates (the SSOT the │ # ports embed; byte-gated so copies cannot drift) ├── agent-context/ # the shared source the per-port AI-assistant context