@metaobjectsdev/cli 0.21.6 → 0.22.0-rc.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,168 @@
1
+ // `meta verify` — the `@verifiedBy` check.
2
+ //
3
+ // A requirement's `@verifiedBy` names the tests that prove the behaviour. verify
4
+ // checks each name EXISTS and is NOT SKIPPED; it never runs them. Running them is
5
+ // the test runner's job, and a requirement gate that shelled out to one would be
6
+ // slow, ecosystem-specific, and wrong in CI.
7
+ //
8
+ // Until this existed, `@verifiedBy` was registered vocabulary that nothing read —
9
+ // the precise pattern ADR-0007 Amendment 2 and the `@role` shrink exist to punish.
10
+ // An attribute the loader accepts and no consumer dispatches on teaches authors
11
+ // that declaring is free and means nothing.
12
+ //
13
+ // PRECISION OVER RECALL, deliberately. The scan matches a name anywhere in the
14
+ // test corpus, as a whole word, in any language. That is the most generous
15
+ // possible reading, so a "missing" verdict means the name appears in NO test file
16
+ // at all — which is a broken claim in any ecosystem. The repo's standing rule for
17
+ // drift checks is to bias toward under-flagging, and a nagging gate gets disabled,
18
+ // which costs more than the misses.
19
+ //
20
+ // FAIL-OPEN ON INABILITY. If the project has no test files this scan can see, it
21
+ // says NOTHING rather than reporting every name missing. Absence of evidence is
22
+ // not evidence of absence, and a monorepo whose tests live outside `--cwd` must
23
+ // not be told its requirements are unverified.
24
+ import { readdirSync, readFileSync, statSync } from "node:fs";
25
+ import { join, relative, sep } from "node:path";
26
+ import { TYPE_REQUIREMENT, } from "@metaobjectsdev/metadata";
27
+ export const ERR_REQUIREMENT_TEST_MISSING = "ERR_REQUIREMENT_TEST_MISSING";
28
+ export const WARN_REQUIREMENT_TEST_SKIPPED = "WARN_REQUIREMENT_TEST_SKIPPED";
29
+ const IGNORE_SEGMENTS = new Set([
30
+ "node_modules", ".git", "dist", "build", "out", ".next", "coverage",
31
+ ".metaobjects", "generated", "target", "bin", "obj", "__pycache__", ".venv", "venv",
32
+ ]);
33
+ /** Test files across the five ecosystems this project ports to. */
34
+ const TEST_FILE = new RegExp([
35
+ "\\.(?:test|spec)\\.[cm]?[jt]sx?$", // bun / jest / vitest / mocha
36
+ "(?:^|[./_-])[Tt]est[^/]*\\.java$", // JUnit — TestFoo.java
37
+ "[A-Za-z0-9]Test(?:s)?\\.java$", // JUnit — FooTest.java / FooTests.java
38
+ "[A-Za-z0-9]Tests?\\.cs$", // xUnit / NUnit
39
+ "^test_[^/]*\\.py$", // pytest
40
+ "[^/]*_test\\.py$", // pytest, trailing convention
41
+ "[A-Za-z0-9]Test(?:s)?\\.kt$", // Kotlin
42
+ ].join("|"));
43
+ /** Markers that a test exists but is disabled, across the same ecosystems. */
44
+ const SKIP_MARKER = new RegExp([
45
+ "\\b(?:it|test|describe)\\.(?:skip|todo)\\b", // jest/vitest/bun
46
+ "\\bx(?:it|test|describe)\\b", // mocha/jasmine
47
+ "@Disabled\\b", // JUnit 5
48
+ "@Ignore\\b", // JUnit 4 / Kotlin
49
+ "@pytest\\.mark\\.skip", // pytest
50
+ "\\[Ignore[\\](]", // MSTest / NUnit
51
+ "\\bSkip\\s*=", // xUnit [Fact(Skip = "...")]
52
+ ].join("|"));
53
+ function walk(dir, root, acc, depth = 0) {
54
+ if (depth > 12)
55
+ return; // pathological trees; the scan is advisory, not exhaustive
56
+ let entries;
57
+ try {
58
+ entries = readdirSync(dir, { withFileTypes: true });
59
+ }
60
+ catch {
61
+ return;
62
+ }
63
+ for (const e of entries) {
64
+ if (e.isDirectory()) {
65
+ if (IGNORE_SEGMENTS.has(e.name) || e.name.startsWith("."))
66
+ continue;
67
+ walk(join(dir, e.name), root, acc, depth + 1);
68
+ continue;
69
+ }
70
+ if (!e.isFile() || !TEST_FILE.test(e.name))
71
+ continue;
72
+ const abs = join(dir, e.name);
73
+ try {
74
+ if (statSync(abs).size > 512 * 1024)
75
+ continue;
76
+ acc.byFile.set(relative(root, abs).split(sep).join("/"), readFileSync(abs, "utf8").split("\n"));
77
+ acc.files++;
78
+ }
79
+ catch {
80
+ /* unreadable file is not a finding */
81
+ }
82
+ }
83
+ }
84
+ /** Every `requirement.*` node in the tree, at any nesting depth. */
85
+ function collect(root) {
86
+ const out = [];
87
+ const rec = (n) => {
88
+ for (const c of n.children()) {
89
+ if (c.type === TYPE_REQUIREMENT)
90
+ out.push(c);
91
+ rec(c);
92
+ }
93
+ };
94
+ rec(root);
95
+ return out;
96
+ }
97
+ /**
98
+ * A whole-word match, so `OrderServiceTest` never satisfies a claim naming `Order`.
99
+ *
100
+ * `_` counts as a SEPARATOR, not a word character: pytest's `def test_OrderServiceTest`
101
+ * plainly is the test a claim naming `OrderServiceTest` means, and refusing it would
102
+ * emit the confident false error this scan is built to avoid. Camel-case boundaries
103
+ * stay strict, which is what actually prevents a short name matching a longer one.
104
+ */
105
+ function wordRx(name) {
106
+ return new RegExp(`(?:^|[^A-Za-z0-9])${name.replace(/[.*+?^${}()|[\]\\]/g, "\\$&")}(?![A-Za-z0-9])`);
107
+ }
108
+ /**
109
+ * Check every `@verifiedBy` name against the project's test corpus.
110
+ *
111
+ * Severity mirrors `@implementedBy`: a broken claim is an ERROR on `live`/`partial`
112
+ * and silent on `abandoned`/`superseded`, because a retired requirement naming a
113
+ * deleted test is the entry doing its job, not drift.
114
+ */
115
+ export function checkVerifiedBy(root, cwd) {
116
+ const reqs = collect(root).filter((r) => r.verifiedBy().length > 0);
117
+ if (reqs.length === 0)
118
+ return []; // opt-in by declaration
119
+ const corpus = { files: 0, byFile: new Map() };
120
+ walk(cwd, cwd, corpus);
121
+ if (corpus.files === 0)
122
+ return []; // fail open: nothing to judge against
123
+ const out = [];
124
+ for (const req of reqs) {
125
+ for (const test of req.verifiedBy()) {
126
+ const rx = wordRx(test);
127
+ let foundIn;
128
+ let skippedAt;
129
+ for (const [file, lines] of corpus.byFile) {
130
+ for (let i = 0; i < lines.length; i++) {
131
+ if (!rx.test(lines[i] ?? ""))
132
+ continue;
133
+ foundIn ??= file;
134
+ // a decorator/annotation sits above the declaration it disables
135
+ const window = lines.slice(Math.max(0, i - 3), i + 1).join("\n");
136
+ if (SKIP_MARKER.test(window))
137
+ skippedAt ??= `${file}:${i + 1}`;
138
+ }
139
+ if (foundIn !== undefined && skippedAt !== undefined)
140
+ break;
141
+ }
142
+ if (foundIn === undefined) {
143
+ if (req.requiresLiveNodes()) {
144
+ out.push({
145
+ severity: "error",
146
+ code: ERR_REQUIREMENT_TEST_MISSING,
147
+ name: req.name,
148
+ message: `'verifiedBy' names '${test}', which appears in none of the ` +
149
+ `${corpus.files} test file(s) found under this project. Either the test was ` +
150
+ `renamed or removed, or the claim was never true.`,
151
+ });
152
+ }
153
+ continue;
154
+ }
155
+ if (skippedAt !== undefined) {
156
+ out.push({
157
+ severity: "warn",
158
+ code: WARN_REQUIREMENT_TEST_SKIPPED,
159
+ name: req.name,
160
+ message: `'verifiedBy' names '${test}', but it is disabled at ${skippedAt}. ` +
161
+ `A skipped test proves nothing — the requirement reads as verified and is not.`,
162
+ });
163
+ }
164
+ }
165
+ }
166
+ return out;
167
+ }
168
+ //# sourceMappingURL=verified-by-scan.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"verified-by-scan.js","sourceRoot":"","sources":["../../../src/lib/verified-by-scan.ts"],"names":[],"mappings":"AAAA,2CAA2C;AAC3C,EAAE;AACF,iFAAiF;AACjF,kFAAkF;AAClF,iFAAiF;AACjF,6CAA6C;AAC7C,EAAE;AACF,kFAAkF;AAClF,mFAAmF;AACnF,gFAAgF;AAChF,4CAA4C;AAC5C,EAAE;AACF,+EAA+E;AAC/E,2EAA2E;AAC3E,kFAAkF;AAClF,kFAAkF;AAClF,mFAAmF;AACnF,oCAAoC;AACpC,EAAE;AACF,iFAAiF;AACjF,gFAAgF;AAChF,gFAAgF;AAChF,+CAA+C;AAE/C,OAAO,EAAE,WAAW,EAAE,YAAY,EAAE,QAAQ,EAAE,MAAM,SAAS,CAAC;AAC9D,OAAO,EAAE,IAAI,EAAE,QAAQ,EAAE,GAAG,E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EAAE,MAAM;oBAChB,IAAI,EAAE,6BAA6B;oBACnC,IAAI,EAAE,GAAG,CAAC,IAAI;oBACd,OAAO,EACL,uBAAuB,IAAI,4BAA4B,SAAS,IAAI;wBACpE,+EAA+E;iBAClF,CAAC,CAAC;YACL,CAAC;QACH,CAAC;IACH,CAAC;IACD,OAAO,GAAG,CAAC;AACb,CAAC"}
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@metaobjectsdev/cli",
3
- "version": "0.21.6",
3
+ "version": "0.22.0-rc.2",
4
4
  "description": "CLI for MetaObjects: scaffold, codegen, migrate, and drift-detection commands.",
5
5
  "type": "module",
6
6
  "main": "./dist/src/index.js",
@@ -49,15 +49,15 @@
49
49
  ],
50
50
  "dependencies": {
51
51
  "@libsql/kysely-libsql": "^0.4.0",
52
- "@metaobjectsdev/codegen-ts": "0.21.6",
53
- "@metaobjectsdev/codegen-ts-react": "0.21.6",
54
- "@metaobjectsdev/codegen-ts-tanstack": "0.21.6",
55
- "@metaobjectsdev/docs-site": "0.21.6",
56
- "@metaobjectsdev/metadata": "0.21.6",
57
- "@metaobjectsdev/migrate-ts": "0.21.6",
58
- "@metaobjectsdev/render": "0.21.6",
59
- "@metaobjectsdev/runtime-ts": "0.21.6",
60
- "@metaobjectsdev/sdk": "0.21.6",
52
+ "@metaobjectsdev/codegen-ts": "0.22.0-rc.2",
53
+ "@metaobjectsdev/codegen-ts-react": "0.22.0-rc.2",
54
+ "@metaobjectsdev/codegen-ts-tanstack": "0.22.0-rc.2",
55
+ "@metaobjectsdev/docs-site": "0.22.0-rc.2",
56
+ "@metaobjectsdev/metadata": "0.22.0-rc.2",
57
+ "@metaobjectsdev/migrate-ts": "0.22.0-rc.2",
58
+ "@metaobjectsdev/render": "0.22.0-rc.2",
59
+ "@metaobjectsdev/runtime-ts": "0.22.0-rc.2",
60
+ "@metaobjectsdev/sdk": "0.22.0-rc.2",
61
61
  "@toon-format/toon": "^2.3.0",
62
62
  "jiti": "^2.4.0"
63
63
  },
@@ -16,6 +16,8 @@ import { FileProvider } from "../lib/file-provider.js";
16
16
  import { derivePayloadFieldTree } from "../lib/payload-field-tree.js";
17
17
  import { loadMetaobjectsConfig } from "../lib/load-metaobjects-config.js";
18
18
  import { computeCodegenDrift } from "../lib/codegen-drift.js";
19
+ import { checkRequirements } from "../lib/requirement-check.js";
20
+ import { checkVerifiedBy } from "../lib/verified-by-scan.js";
19
21
  import { resolveD1Config } from "../lib/config.js";
20
22
  import {
21
23
  buildWranglerExecuteArgs,
@@ -158,6 +160,10 @@ export async function verifyCommand(
158
160
  const templateExit = runTemplates ? runTemplateVerify() : 0;
159
161
  const schemaExit = await runSchemaVerify();
160
162
  const codegenExit = runCodegen ? await runCodegenVerify() : 0;
163
+ // Requirements have no subverb: `requirement.*` nodes are metadata, so they
164
+ // are checked on every `meta verify`. Opt-in by DECLARATION — a model with no
165
+ // requirement nodes is silent, not in drift.
166
+ const requirementExit = runRequirementVerify();
161
167
 
162
168
  // Advisory verify-as-teacher pass: surface hand-rolled work the metadata could
163
169
  // model. Warnings ONLY — never changes the exit code (bias to under-flagging).
@@ -165,7 +171,28 @@ export async function verifyCommand(
165
171
  // noisy project (both opt-outs work on `meta verify` and `meta gen`).
166
172
  if (!flags.noAntipatterns && process.env.META_NO_ANTIPATTERNS !== "1") runAntiPatternAdvisory();
167
173
 
168
- return Math.max(templateExit, schemaExit, codegenExit);
174
+ return Math.max(templateExit, schemaExit, codegenExit, requirementExit);
175
+
176
+ // -- requirements (#290) ---------------------------------------------------
177
+ function runRequirementVerify(): number {
178
+ // `@verifiedBy` resolution needs the project on disk, so it is a separate
179
+ // scan; its diagnostics carry the same severities and share this reporter.
180
+ const diags = [...checkRequirements(root), ...checkVerifiedBy(root, cwd)];
181
+ if (diags.length === 0) return 0;
182
+ const errors = diags.filter((d) => d.severity === "error");
183
+ const warns = diags.filter((d) => d.severity === "warn");
184
+ const CAP = 20;
185
+ for (const d of errors) log.error(` ${d.code}${d.name !== undefined ? ` [${d.name}]` : ""}: ${d.message}`);
186
+ for (const d of warns.slice(0, CAP)) {
187
+ log.warn(` ${d.code}${d.name !== undefined ? ` [${d.name}]` : ""}: ${d.message}`);
188
+ }
189
+ if (warns.length > CAP) log.warn(` …and ${warns.length - CAP} more.`);
190
+ if (errors.length > 0) {
191
+ log.error(`meta verify — requirements: ${errors.length} error(s).`);
192
+ return 1;
193
+ }
194
+ return 0;
195
+ }
169
196
 
170
197
  // -- verify-as-teacher (advisory) ------------------------------------------
171
198
  function runAntiPatternAdvisory(): void {
@@ -1,7 +1,8 @@
1
1
  import { existsSync, readFileSync, readdirSync } from "node:fs";
2
+ import type { Dirent } from "node:fs";
2
3
  import { join } from "node:path";
3
4
  import {
4
- detectStack, makeStack,
5
+ detectStack, detectConcerns, makeStack,
5
6
  type ServerLang, type ClientFramework, type Stack, type ProjectProbe,
6
7
  SERVER_LANGS, CLIENT_FRAMEWORKS,
7
8
  } from "@metaobjectsdev/sdk/agent-context";
@@ -20,22 +21,64 @@ function depNames(cwd: string): Set<string> {
20
21
  return out;
21
22
  }
22
23
 
24
+ const METADATA_DIR = "metaobjects";
25
+ const METADATA_FILE_PATTERN = /\.(json|ya?ml)$/i;
26
+ // Cheap substring probe, not a metamodel load: matches both canonical JSON's
27
+ // quoted `"requirement.functional"` key and sigil-free YAML's bare
28
+ // `requirement.functional:` authoring form.
29
+ const REQUIREMENT_NODE_MARKER = "requirement.";
30
+
31
+ /** Recursively scans `metaobjects/` for any `.json`/`.yaml`/`.yml` file containing a
32
+ * `requirement.*` node marker. Defensive throughout: a missing/unreadable directory
33
+ * or file is treated as "not found", never thrown — this is a cheap heuristic, not
34
+ * a metamodel load. */
35
+ function hasRequirementNodes(cwd: string): boolean {
36
+ const root = join(cwd, METADATA_DIR);
37
+ if (!existsSync(root)) return false;
38
+ const pending: string[] = [root];
39
+ while (pending.length > 0) {
40
+ const dir = pending.pop()!;
41
+ let entries: Dirent[];
42
+ try {
43
+ entries = readdirSync(dir, { withFileTypes: true });
44
+ } catch {
45
+ continue; // unreadable directory — skip it, keep scanning siblings
46
+ }
47
+ for (const entry of entries) {
48
+ const full = join(dir, entry.name);
49
+ if (entry.isDirectory()) {
50
+ pending.push(full);
51
+ } else if (METADATA_FILE_PATTERN.test(entry.name)) {
52
+ try {
53
+ if (readFileSync(full, "utf8").includes(REQUIREMENT_NODE_MARKER)) return true;
54
+ } catch { /* unreadable file — treat as no match */ }
55
+ }
56
+ }
57
+ }
58
+ return false;
59
+ }
60
+
23
61
  function probe(cwd: string): ProjectProbe {
24
62
  const deps = depNames(cwd);
25
63
  const names = existsSync(cwd) ? readdirSync(cwd) : [];
26
64
  return {
27
65
  hasDep: (name) => deps.has(name),
28
66
  hasFileMatching: (re) => names.some((n) => re.test(n)),
67
+ hasRequirementNodes: () => hasRequirementNodes(cwd),
29
68
  };
30
69
  }
31
70
 
32
- /** Resolve the stack: explicit --server/--client overrides take precedence; otherwise detect. */
71
+ /** Resolve the stack: explicit --server/--client overrides take precedence; otherwise detect.
72
+ * Concern tokens (e.g. requirements) are always OBSERVED from project state, independent of
73
+ * any --server/--client override — a concern is not a stack axis. */
33
74
  export function resolveStack(cwd: string, overrides: { servers: string[]; clients: string[] }): Stack {
34
75
  const validServers = SERVER_LANGS as readonly string[];
35
76
  const validClients = CLIENT_FRAMEWORKS as readonly string[];
36
77
  const oServers = overrides.servers.filter((s): s is ServerLang => validServers.includes(s));
37
78
  const oClients = overrides.clients.filter((c): c is ClientFramework => validClients.includes(c));
38
- if (oServers.length > 0 || oClients.length > 0) return makeStack(oServers, oClients);
39
- const detected = detectStack(probe(cwd));
40
- return makeStack(detected.servers, detected.clients);
79
+ const p = probe(cwd);
80
+ const concerns = detectConcerns(p);
81
+ if (oServers.length > 0 || oClients.length > 0) return makeStack(oServers, oClients, concerns);
82
+ const detected = detectStack(p);
83
+ return makeStack(detected.servers, detected.clients, concerns);
41
84
  }
@@ -0,0 +1,272 @@
1
+ // `meta verify` — the requirement (capability) gate.
2
+ //
3
+ // Requirements are METADATA: `requirement.functional` / `requirement.architectural`
4
+ // are registered metamodel types, declared in `metaobjects/` beside the entities
5
+ // they describe. So this file parses NOTHING. It reads `requirement.*` nodes off
6
+ // the already-loaded model and checks the things the loader cannot.
7
+ //
8
+ // Division of labour, and the reason for it:
9
+ //
10
+ // LOADER (unconditional) the `@status` enum via `allowedValues`, required
11
+ // attrs, child rules, levels being integers.
12
+ // VERIFY (conditional) `@implementedBy` resolution, whose SEVERITY DEPENDS
13
+ // ON `@status`. A loader `references` descriptor
14
+ // always errors on an unresolved target, and an
15
+ // `abandoned` requirement exists precisely to name
16
+ // nodes that are gone — declaring it there would make
17
+ // the entries carrying the mechanism's only controlled
18
+ // evidence fail to load.
19
+ //
20
+ // Two kinds, opposite checks: `functional` fails when NOTHING implements it;
21
+ // `architectural` fails when something VIOLATES it (v1: an empty claim set on a
22
+ // live policy — claim-set arithmetic, deliberately not a predicate DSL).
23
+
24
+ import {
25
+ TYPE_OBJECT,
26
+ TYPE_REQUIREMENT,
27
+ OBJECT_SUBTYPE_ENTITY,
28
+ PACKAGE_SEPARATOR,
29
+ REQUIREMENT_SUBTYPE_ARCHITECTURAL,
30
+ REQUIREMENT_LINK_FLOOR_LEVEL,
31
+ REQUIREMENT_MIN_LEVEL,
32
+ REQUIREMENT_MAX_LEVEL,
33
+ REQUIREMENT_LEVEL_MEMBER,
34
+ REQUIREMENT_STATUSES_REQUIRING_LIVE_NODES,
35
+ resolveObjectRef,
36
+ didYouMeanHint,
37
+ type MetaData,
38
+ type MetaRequirement,
39
+ type RequirementStatus,
40
+ } from "@metaobjectsdev/metadata";
41
+
42
+ export type Severity = "error" | "warn";
43
+
44
+ export interface Diagnostic {
45
+ severity: Severity;
46
+ code: string;
47
+ /** The requirement node's name, when the diagnostic belongs to one. */
48
+ name?: string;
49
+ message: string;
50
+ }
51
+
52
+ export const ERR_REQUIREMENT_LINK_ABOVE_FLOOR = "ERR_REQUIREMENT_LINK_ABOVE_FLOOR";
53
+ export const ERR_REQUIREMENT_DANGLING_REF = "ERR_REQUIREMENT_DANGLING_REF";
54
+ export const ERR_REQUIREMENT_BAD_LEVEL = "ERR_REQUIREMENT_BAD_LEVEL";
55
+ export const ERR_REQUIREMENT_LEVEL_NESTING = "ERR_REQUIREMENT_LEVEL_NESTING";
56
+ export const ERR_REQUIREMENT_L4_NOT_OBJECT = "ERR_REQUIREMENT_L4_NOT_OBJECT";
57
+ export const ERR_REQUIREMENT_L5_NOT_MEMBER = "ERR_REQUIREMENT_L5_NOT_MEMBER";
58
+ export const ERR_REQUIREMENT_ARCH_NO_IMPLEMENTERS = "ERR_REQUIREMENT_ARCH_NO_IMPLEMENTERS";
59
+ export const WARN_REQUIREMENT_OBJECT_UNCLAIMED = "WARN_REQUIREMENT_OBJECT_UNCLAIMED";
60
+
61
+ /** Severity of the object-coverage gate. Promotion to `"error"` is a one-line
62
+ * flip here, which activates an already-written test rather than requiring new
63
+ * authoring under release pressure.
64
+ *
65
+ * It stays `"warn"`, and the reason is measured rather than cautious:
66
+ *
67
+ * - On a real 120-file estate carrying a SINGLE requirement, this gate reports
68
+ * 93 unclaimed entities — every entity in the repository. At `"error"` a
69
+ * project adopting requirements incrementally fails its first `verify` after
70
+ * authoring one entry, which teaches people to delete the entry.
71
+ * - The gate is satisfiable without being informative: `claimedObjects` below
72
+ * counts a claim from any requirement at any level and any status, so
73
+ * appending an FQN to an existing list clears it. Green proves an entity is
74
+ * NAMED, never that it is understood.
75
+ * - The experiment meant to settle whether forcing it yields real entries or
76
+ * padding stopped at its ceiling probe: at `"warn"` agents already authored
77
+ * proper L3/L4/L5 entries for what they added, so the arms could not differ.
78
+ * That is not evidence promotion is useless — it is evidence that instrument
79
+ * cannot see it.
80
+ *
81
+ * spec/design-docs/2026-08-11-prereg-duplication-and-levels.md, "Round E result". */
82
+ export const OBJECT_COVERAGE_SEVERITY: Severity = "warn";
83
+
84
+ /**
85
+ * Split a member reference into its owning object ref and the dotted member path.
86
+ * `::` qualifies the ROOT-level node only, so the object ref ends at the FIRST
87
+ * `.` after the last `::`.
88
+ * `acme::sales::Order.total.display` -> `["acme::sales::Order", ["total","display"]]`
89
+ */
90
+ export function splitMemberRef(ref: string): { owner: string; path: string[] } {
91
+ const pkgEnd = ref.lastIndexOf(PACKAGE_SEPARATOR);
92
+ const from = pkgEnd === -1 ? 0 : pkgEnd + PACKAGE_SEPARATOR.length;
93
+ const dot = ref.indexOf(".", from);
94
+ if (dot === -1) return { owner: ref, path: [] };
95
+ return { owner: ref.slice(0, dot), path: ref.slice(dot + 1).split(".") };
96
+ }
97
+
98
+ /** Walk dotted member segments by CHILD NAME from an object node. */
99
+ function resolveMember(obj: MetaData, path: string[]): MetaData | undefined {
100
+ let cur: MetaData | undefined = obj;
101
+ for (const seg of path) {
102
+ if (cur === undefined) return undefined;
103
+ cur = cur.children().find((c) => c.name === seg);
104
+ }
105
+ return cur;
106
+ }
107
+
108
+ /** Every `requirement.*` node in the tree, at any nesting depth. Hierarchy IS
109
+ * nesting — an L1 solution contains its L2 segments — so this is a walk, not a
110
+ * scan of a flat list keyed by a `parent` string. */
111
+ export function collectRequirements(root: MetaData): MetaRequirement[] {
112
+ const out: MetaRequirement[] = [];
113
+ const walk = (n: MetaData): void => {
114
+ for (const c of n.children()) {
115
+ if (c.type === TYPE_REQUIREMENT) out.push(c as MetaRequirement);
116
+ walk(c);
117
+ }
118
+ };
119
+ walk(root);
120
+ return out;
121
+ }
122
+
123
+ /**
124
+ * Check the requirement tree against the loaded model.
125
+ *
126
+ * What a clean run proves: referential integrity — links sit at or below the
127
+ * link floor, nesting agrees with levels, and references resolve. What it CANNOT
128
+ * prove: that a status is *true*, or that a node actually implements the
129
+ * requirement claiming it. No test can. That truth is the adopter's job.
130
+ */
131
+ export function checkRequirements(root: MetaData): Diagnostic[] {
132
+ const out: Diagnostic[] = [];
133
+ const reqs = collectRequirements(root);
134
+ if (reqs.length === 0) return out; // opt-in by declaration — no requirements, nothing to say
135
+
136
+ const claimedObjects = new Set<string>();
137
+
138
+ for (const req of reqs) {
139
+ const architectural = req.subType === REQUIREMENT_SUBTYPE_ARCHITECTURAL;
140
+ const level = req.level();
141
+ const refs = req.implementedBy();
142
+
143
+ if (!architectural) {
144
+ if (level === undefined || !Number.isInteger(level)
145
+ || level < REQUIREMENT_MIN_LEVEL || level > REQUIREMENT_MAX_LEVEL) {
146
+ out.push({
147
+ severity: "error", code: ERR_REQUIREMENT_BAD_LEVEL, name: req.name,
148
+ message: `level must be an integer ${REQUIREMENT_MIN_LEVEL}-${REQUIREMENT_MAX_LEVEL} (got ${String(level)}). ` +
149
+ `L1 solution, L2 segment (app/library), L3 service, L4 object, L5 member.`,
150
+ });
151
+ }
152
+ // Nesting IS the hierarchy, so a child must sit strictly below its parent.
153
+ const parent = req.parent;
154
+ if (parent !== undefined && parent.type === TYPE_REQUIREMENT) {
155
+ const pl = (parent as MetaRequirement).level();
156
+ if (pl !== undefined && level !== undefined && level <= pl) {
157
+ out.push({
158
+ severity: "error", code: ERR_REQUIREMENT_LEVEL_NESTING, name: req.name,
159
+ message: `nested under "${parent.name}" (level ${pl}) but declares level ${level}. ` +
160
+ `Nesting is the hierarchy — a child sits strictly below its parent.`,
161
+ });
162
+ }
163
+ }
164
+ }
165
+
166
+ // -- the link boundary ----------------------------------------------------
167
+ if (refs.length > 0 && !req.mayReferenceModel()) {
168
+ out.push({
169
+ severity: "error", code: ERR_REQUIREMENT_LINK_ABOVE_FLOOR, name: req.name,
170
+ message: `'implementedBy' is legal at L${REQUIREMENT_LINK_FLOOR_LEVEL} (object) and ` +
171
+ `L${REQUIREMENT_MAX_LEVEL} (member) only. L1-L3 are organisational and never reference ` +
172
+ `the model — move the links to a nested L${REQUIREMENT_LINK_FLOOR_LEVEL} child.`,
173
+ });
174
+ continue;
175
+ }
176
+
177
+ for (const ref of refs) {
178
+ const { owner, path } = splitMemberRef(ref);
179
+ // referrerPkg is the requirement's own effective package, so a bare ref
180
+ // binds package-locally under the ADR-0042 contract — the loader's own
181
+ // resolver, never a parallel name scan (#228).
182
+ const referrerPkg = req.package ?? req.fileDefaultPackage ?? "";
183
+ const { node } = resolveObjectRef(root, owner, referrerPkg);
184
+ const isObjectRef = path.length === 0;
185
+
186
+ if (node !== undefined && (isObjectRef || resolveMember(node, path) !== undefined)) {
187
+ claimedObjects.add(node.resolutionKey());
188
+ }
189
+
190
+ if (!architectural && level === REQUIREMENT_LINK_FLOOR_LEVEL && !isObjectRef) {
191
+ out.push({
192
+ severity: "error", code: ERR_REQUIREMENT_L4_NOT_OBJECT, name: req.name,
193
+ message: `L${REQUIREMENT_LINK_FLOOR_LEVEL} references an object; '${ref}' names a member. ` +
194
+ `Move it to a nested L${REQUIREMENT_LEVEL_MEMBER} child, or reference the object itself.`,
195
+ });
196
+ continue;
197
+ }
198
+ if (!architectural && level === REQUIREMENT_LEVEL_MEMBER && isObjectRef) {
199
+ out.push({
200
+ severity: "error", code: ERR_REQUIREMENT_L5_NOT_MEMBER, name: req.name,
201
+ message: `L${REQUIREMENT_LEVEL_MEMBER} references a member (field, view or identity); ` +
202
+ `'${ref}' names an object. Move it to its L${REQUIREMENT_LINK_FLOOR_LEVEL} parent.`,
203
+ });
204
+ continue;
205
+ }
206
+
207
+ const resolved = node !== undefined && (isObjectRef || resolveMember(node, path) !== undefined);
208
+ if (!resolved) {
209
+ // Severity is CONDITIONAL ON STATUS, and the asymmetry inverts as a pair.
210
+ // On abandoned/superseded the nodes are SUPPOSED to be gone — that is the
211
+ // entry doing its job, and the reason this check cannot live in the loader.
212
+ if (req.requiresLiveNodes()) {
213
+ out.push({
214
+ severity: "error", code: ERR_REQUIREMENT_DANGLING_REF, name: req.name,
215
+ message: `'${ref}' does not resolve in the loaded model (status '${String(req.status())}' — ` +
216
+ `the model moved and the requirement is stale).` + didYouMeanHint(root, owner),
217
+ });
218
+ }
219
+ }
220
+ }
221
+
222
+ // -- architectural universality, v1: claim-set arithmetic -----------------
223
+ // A live policy claimed by nothing is the audited-base case: declared and
224
+ // applied to nothing. Deliberately NOT a violation-predicate DSL — that
225
+ // would be the registration this design already argued its way to, arriving
226
+ // a second time through the back door.
227
+ const status = req.status();
228
+ const live = status !== undefined
229
+ && REQUIREMENT_STATUSES_REQUIRING_LIVE_NODES.includes(status as RequirementStatus);
230
+ if (architectural && live && refs.length === 0) {
231
+ out.push({
232
+ severity: "error", code: ERR_REQUIREMENT_ARCH_NO_IMPLEMENTERS, name: req.name,
233
+ message: `architectural requirement is '${String(status)}' but nothing implements it. ` +
234
+ `Its check is universality — a claim set of zero means the policy is declared and unapplied.`,
235
+ });
236
+ }
237
+ }
238
+
239
+ // -- object coverage: adding an entity forces a requirement -----------------
240
+ // Binary per entity, never a ratio: a "% claimed" number measures what the
241
+ // schema can express, is biased against the hardest rules, and invites
242
+ // optimising the number.
243
+ //
244
+ // SCOPE, stated because it is a decision and not an oversight:
245
+ //
246
+ // ENTITIES ONLY. `object.value` and `object.projection` are exempt. A value is a
247
+ // shape (a DTO, a payload, a message) and a projection is DERIVED from an entity
248
+ // that is itself claimable — requiring both to carry their own capability claim
249
+ // would multiply entries without adding information.
250
+ //
251
+ // OBJECT GRAIN ONLY. Fields, views, validators and identities are never required
252
+ // to be claimed. Member-grain coverage is the "thousands of meaningless links"
253
+ // failure `spec/capability-ledger.md` argues against: plumbing members are covered
254
+ // by ARCHITECTURAL requirements with high fan-out (one uuid-PK rule claims every
255
+ // entity), not by a per-member entry. L5 exists so a claim about a specific member
256
+ // CAN be made when it carries real meaning — never so that every member must.
257
+ //
258
+ // So a green run means "every entity is claimed by something", not "every node is
259
+ // described". The stronger reading would be false.
260
+ for (const ent of root.children()) {
261
+ if (ent.type !== TYPE_OBJECT || ent.subType !== OBJECT_SUBTYPE_ENTITY) continue;
262
+ const key = ent.resolutionKey();
263
+ if (!claimedObjects.has(key)) {
264
+ out.push({
265
+ severity: OBJECT_COVERAGE_SEVERITY, code: WARN_REQUIREMENT_OBJECT_UNCLAIMED,
266
+ message: `no requirement claims '${key}'. Add it to an L${REQUIREMENT_LINK_FLOOR_LEVEL} requirement's 'implementedBy'.`,
267
+ });
268
+ }
269
+ }
270
+
271
+ return out;
272
+ }