@metaobjectsdev/sdk 0.23.2 → 0.24.1
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.
- package/agent-context/skills/metaobjects-audit/SKILL.md +20 -7
- package/agent-context/skills/metaobjects-audit/references/capability-checklist.md +65 -19
- package/agent-context/skills/metaobjects-audit/references/csharp.md +2 -1
- package/agent-context/skills/metaobjects-audit/references/java.md +3 -2
- package/agent-context/skills/metaobjects-audit/references/kotlin.md +2 -1
- package/agent-context/skills/metaobjects-audit/references/requirements.md +3 -4
- package/agent-context/skills/metaobjects-audit/references/typescript.md +1 -1
- package/agent-context/skills/metaobjects-authoring/SKILL.md +53 -10
- package/agent-context/skills/metaobjects-authoring/references/requirements.md +49 -23
- package/agent-context/skills/metaobjects-codegen/SKILL.md +4 -3
- package/agent-context/skills/metaobjects-codegen/references/csharp.md +12 -2
- package/agent-context/skills/metaobjects-codegen/references/java.md +3 -3
- package/agent-context/skills/metaobjects-codegen/references/kotlin.md +4 -4
- package/agent-context/skills/metaobjects-codegen/references/python.md +12 -2
- package/agent-context/skills/metaobjects-codegen/references/typescript.md +14 -3
- package/agent-context/skills/metaobjects-fit-assessment/SKILL.md +4 -4
- package/agent-context/skills/metaobjects-prompts/SKILL.md +53 -32
- package/agent-context/skills/metaobjects-prompts/references/csharp.md +26 -21
- package/agent-context/skills/metaobjects-prompts/references/java.md +24 -18
- package/agent-context/skills/metaobjects-prompts/references/kotlin.md +25 -20
- package/agent-context/skills/metaobjects-prompts/references/python.md +68 -26
- package/agent-context/skills/metaobjects-prompts/references/typescript.md +29 -23
- package/agent-context/skills/metaobjects-verify/references/migration.md +1 -1
- package/agent-context/skills/metaobjects-verify/references/requirements.md +36 -22
- package/agent-context/templates/always-on.md.mustache +3 -1
- package/dist/agent-docs/body.d.ts +1 -1
- package/dist/agent-docs/body.d.ts.map +1 -1
- package/dist/agent-docs/body.js +24 -2
- package/dist/agent-docs/body.js.map +1 -1
- package/dist/collection.d.ts +71 -0
- package/dist/collection.d.ts.map +1 -0
- package/dist/collection.js +203 -0
- package/dist/collection.js.map +1 -0
- package/dist/config.d.ts +51 -21
- package/dist/config.d.ts.map +1 -1
- package/dist/config.js +76 -10
- package/dist/config.js.map +1 -1
- package/dist/discovery.d.ts +43 -0
- package/dist/discovery.d.ts.map +1 -0
- package/dist/discovery.js +100 -0
- package/dist/discovery.js.map +1 -0
- package/dist/index.d.ts +10 -1
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +17 -4
- package/dist/index.js.map +1 -1
- package/dist/memory.d.ts +42 -19
- package/dist/memory.d.ts.map +1 -1
- package/dist/memory.js +38 -95
- package/dist/memory.js.map +1 -1
- package/dist/metadata-files.d.ts +67 -0
- package/dist/metadata-files.d.ts.map +1 -0
- package/dist/metadata-files.js +155 -0
- package/dist/metadata-files.js.map +1 -0
- package/dist/records/any.d.ts +8 -8
- package/dist/records/decision.d.ts +2 -2
- package/dist/records/glossary.d.ts +4 -4
- package/dist/records/principle.d.ts +2 -2
- package/dist/scope.d.ts +16 -0
- package/dist/scope.d.ts.map +1 -0
- package/dist/scope.js +75 -0
- package/dist/scope.js.map +1 -0
- package/dist/sources.d.ts +86 -0
- package/dist/sources.d.ts.map +1 -0
- package/dist/sources.js +130 -0
- package/dist/sources.js.map +1 -0
- package/package.json +2 -2
- package/src/agent-docs/body.ts +24 -2
- package/src/collection.ts +268 -0
- package/src/config.ts +80 -12
- package/src/discovery.ts +110 -0
- package/src/index.ts +26 -9
- package/src/memory.ts +65 -99
- package/src/metadata-files.ts +158 -0
- package/src/scope.ts +95 -0
- package/src/sources.ts +160 -0
package/src/discovery.ts
ADDED
|
@@ -0,0 +1,110 @@
|
|
|
1
|
+
// server/typescript/packages/sdk/src/discovery.ts
|
|
2
|
+
//
|
|
3
|
+
// Phase-1 metadata-source-resolution — nearest-ancestor collection discovery.
|
|
4
|
+
//
|
|
5
|
+
// Walks up from a starting directory to find the nearest directory that IS a
|
|
6
|
+
// project root. This is what makes a CLI *contextual*: run it inside an app in
|
|
7
|
+
// a monorepo and it finds that app's configuration rather than the repo root's.
|
|
8
|
+
//
|
|
9
|
+
// Three properties are load-bearing.
|
|
10
|
+
//
|
|
11
|
+
// 1. **One marker.** A directory is a project root when it carries
|
|
12
|
+
// `.metaobjects/config.json`, and on no other evidence. A directory that
|
|
13
|
+
// merely *holds* metadata is not a project boundary: where metadata lives is
|
|
14
|
+
// the `sources` key's answer, and `metaobjects/` is only that key's default
|
|
15
|
+
// value. Stopping on a bare `metaobjects/` directory would put a second
|
|
16
|
+
// definition of "where metadata lives" back into the walk — the exact
|
|
17
|
+
// duplication `resolveCollection` exists to be the only instance of — and it
|
|
18
|
+
// would ignore a project whose config points its `sources` somewhere else
|
|
19
|
+
// entirely. See design §4.6.1.
|
|
20
|
+
// 2. **Nearest wins** — the walk returns on the FIRST directory carrying the
|
|
21
|
+
// marker, so a config in a subdirectory beats one in an ancestor.
|
|
22
|
+
// 3. **The walk stops at a repository boundary** (`.git`), so a monorepo
|
|
23
|
+
// checkout can never silently adopt a *parent checkout's* configuration. The
|
|
24
|
+
// marker check runs BEFORE the `.git` check within each directory —
|
|
25
|
+
// reversed, a root-level project (where `.git` also lives) would be
|
|
26
|
+
// unreachable from any subdirectory, since the boundary would stop the walk
|
|
27
|
+
// one directory too early.
|
|
28
|
+
import { stat } from "node:fs/promises";
|
|
29
|
+
import { dirname, join, resolve } from "node:path";
|
|
30
|
+
import { CONFIG_FILE } from "./config.js";
|
|
31
|
+
import { DEFAULT_METAOBJECTS_DIR } from "./metadata-files.js";
|
|
32
|
+
|
|
33
|
+
const GIT_DIR = ".git";
|
|
34
|
+
|
|
35
|
+
/** Exported for reuse — `collection.ts` had its own byte-identical copy
|
|
36
|
+
* (`fileExists`); one definition, imported. */
|
|
37
|
+
export async function exists(path: string): Promise<boolean> {
|
|
38
|
+
try {
|
|
39
|
+
await stat(path);
|
|
40
|
+
return true;
|
|
41
|
+
} catch {
|
|
42
|
+
return false;
|
|
43
|
+
}
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
/** `exists`, narrowed to directories — `collection.ts`'s default-source probe
|
|
47
|
+
* needs that distinction to raise its friendlier `ERR_COLLECTION_NOT_FOUND`
|
|
48
|
+
* (a plain FILE where the default source directory should be is not a
|
|
49
|
+
* metadata home). Lives here beside `exists` so there is one filesystem
|
|
50
|
+
* predicate pair in the package rather than a copy per caller. */
|
|
51
|
+
export async function isDir(path: string): Promise<boolean> {
|
|
52
|
+
try {
|
|
53
|
+
return (await stat(path)).isDirectory();
|
|
54
|
+
} catch {
|
|
55
|
+
return false;
|
|
56
|
+
}
|
|
57
|
+
}
|
|
58
|
+
|
|
59
|
+
/** Where the discovery walk stopped, and what it found there. */
|
|
60
|
+
export interface DiscoveredRoot {
|
|
61
|
+
/** The project root the walk settled on. Always absolute; falls back to the
|
|
62
|
+
* resolved start directory when the walk found no marker at all. */
|
|
63
|
+
readonly dir: string;
|
|
64
|
+
/** Whether `dir` carries `.metaobjects/config.json`. False only on the
|
|
65
|
+
* no-marker fallback, where the DEFAULT sources apply. */
|
|
66
|
+
readonly hasConfig: boolean;
|
|
67
|
+
}
|
|
68
|
+
|
|
69
|
+
/**
|
|
70
|
+
* Walk up from `startDir` for the nearest project root — a directory holding
|
|
71
|
+
* `.metaobjects/config.json`, and nothing else (see the file header). The walk
|
|
72
|
+
* stops after examining a directory that contains `.git`, so a monorepo can
|
|
73
|
+
* never silently adopt a parent checkout's configuration.
|
|
74
|
+
*
|
|
75
|
+
* Never fails: with no config anywhere below the boundary it reports the
|
|
76
|
+
* resolved `startDir` with `hasConfig: false`, which is what
|
|
77
|
+
* `resolveCollection` turns into either the default source or
|
|
78
|
+
* `ERR_COLLECTION_NOT_FOUND`.
|
|
79
|
+
*/
|
|
80
|
+
export async function discoverCollectionRoot(startDir: string): Promise<DiscoveredRoot> {
|
|
81
|
+
const start = resolve(startDir);
|
|
82
|
+
let dir = start;
|
|
83
|
+
for (;;) {
|
|
84
|
+
if (await exists(join(dir, DEFAULT_METAOBJECTS_DIR, CONFIG_FILE))) {
|
|
85
|
+
return { dir, hasConfig: true };
|
|
86
|
+
}
|
|
87
|
+
// Boundary check AFTER the marker check: a repo-root project (sharing its
|
|
88
|
+
// directory with `.git`) is still findable from any subdirectory.
|
|
89
|
+
if (await exists(join(dir, GIT_DIR))) break;
|
|
90
|
+
const parent = dirname(dir);
|
|
91
|
+
if (parent === dir) break;
|
|
92
|
+
dir = parent;
|
|
93
|
+
}
|
|
94
|
+
return { dir: start, hasConfig: false };
|
|
95
|
+
}
|
|
96
|
+
|
|
97
|
+
/**
|
|
98
|
+
* The directory whose configuration governs a run started in `startDir` — the
|
|
99
|
+
* `dir` half of {@link discoverCollectionRoot}.
|
|
100
|
+
*
|
|
101
|
+
* Exists as its own export for the callers that must NOT require metadata to
|
|
102
|
+
* exist: `meta migrate apply-pending` and `--rollback` replay committed SQL and
|
|
103
|
+
* load no model at all, so they resolve their `.metaobjects/` directory through
|
|
104
|
+
* this rather than through `resolveCollection`. Sharing the walk is the point —
|
|
105
|
+
* a second "find the project root" implementation is how the migrations
|
|
106
|
+
* directory and the metadata directory come to disagree.
|
|
107
|
+
*/
|
|
108
|
+
export async function resolveConfigDir(startDir: string): Promise<string> {
|
|
109
|
+
return (await discoverCollectionRoot(startDir)).dir;
|
|
110
|
+
}
|
package/src/index.ts
CHANGED
|
@@ -76,17 +76,34 @@ export {
|
|
|
76
76
|
} from "./forge-types.js";
|
|
77
77
|
export type { ForgeType, ForgeAttr } from "./forge-types.js";
|
|
78
78
|
|
|
79
|
-
// Memory loader — read
|
|
80
|
-
//
|
|
81
|
-
//
|
|
82
|
-
export {
|
|
83
|
-
loadMemory,
|
|
84
|
-
defaultLoadMemoryProviders,
|
|
85
|
-
DEFAULT_METADATA_DIR,
|
|
86
|
-
DEFAULT_METAOBJECTS_DIR,
|
|
87
|
-
} from "./memory.js";
|
|
79
|
+
// Memory loader — read a project's resolved metadata into a MetaData tree.
|
|
80
|
+
// Where those files come from is `resolveCollection`'s decision (below), which
|
|
81
|
+
// `loadMemory` calls when the caller supplies no explicit file set.
|
|
82
|
+
export { loadMemory, defaultLoadMemoryProviders } from "./memory.js";
|
|
88
83
|
export type { LoadMemoryOptions } from "./memory.js";
|
|
89
84
|
|
|
85
|
+
// Default project layout — the DEFAULT value of `sources` (applied by
|
|
86
|
+
// `resolveCollection` alone) and the fixed directory holding the config that
|
|
87
|
+
// declares them. Exported for `meta init`, which SCAFFOLDS that layout.
|
|
88
|
+
export { DEFAULT_METADATA_DIR, DEFAULT_METAOBJECTS_DIR } from "./metadata-files.js";
|
|
89
|
+
|
|
90
|
+
// Scope — output filter over fully-qualified node names
|
|
91
|
+
export { compileScope, matchesScope } from "./scope.js";
|
|
92
|
+
export type { Scope, CompiledScope } from "./scope.js";
|
|
93
|
+
|
|
94
|
+
// Source resolution — a declared source SET to a canonically-sorted file list
|
|
95
|
+
export { resolveSources, resolveSpecPath, orderedPathSpecs, DEFAULT_SOURCES } from "./sources.js";
|
|
96
|
+
export type { SourceSpec, ResolvedSource } from "./sources.js";
|
|
97
|
+
|
|
98
|
+
// Discovery — nearest-ancestor project root (a `.metaobjects/config.json`,
|
|
99
|
+
// the only marker), bounded by the repo root
|
|
100
|
+
export { discoverCollectionRoot, resolveConfigDir } from "./discovery.js";
|
|
101
|
+
export type { DiscoveredRoot } from "./discovery.js";
|
|
102
|
+
|
|
103
|
+
// Collection — the single authority on where a project's metadata lives
|
|
104
|
+
export { resolveCollection } from "./collection.js";
|
|
105
|
+
export type { Collection } from "./collection.js";
|
|
106
|
+
|
|
90
107
|
// Workspace discovery — finds peer metadata packages in a monorepo
|
|
91
108
|
export { discoverWorkspace, resolveExtendsOrder, packageLabel } from "./workspace.js";
|
|
92
109
|
export type { Workspace, WorkspacePackage } from "./workspace.js";
|
package/src/memory.ts
CHANGED
|
@@ -1,5 +1,3 @@
|
|
|
1
|
-
import { join } from "node:path";
|
|
2
|
-
import { readdir, stat } from "node:fs/promises";
|
|
3
1
|
import {
|
|
4
2
|
composeRegistry,
|
|
5
3
|
coreProviders,
|
|
@@ -8,21 +6,8 @@ import {
|
|
|
8
6
|
type MetaRoot,
|
|
9
7
|
} from "@metaobjectsdev/metadata";
|
|
10
8
|
import { FileSource } from "@metaobjectsdev/metadata/core";
|
|
9
|
+
import { resolveCollection } from "./collection.js";
|
|
11
10
|
import { forgeTypesProvider } from "./forge-types.js";
|
|
12
|
-
import { discoverWorkspace, resolveExtendsOrder } from "./workspace.js";
|
|
13
|
-
|
|
14
|
-
/**
|
|
15
|
-
* Default directory name (relative to project root) where metadata JSON files
|
|
16
|
-
* are scanned. Scaffold via `meta init`; the directory is committed to git.
|
|
17
|
-
*/
|
|
18
|
-
export const DEFAULT_METADATA_DIR = "metaobjects";
|
|
19
|
-
|
|
20
|
-
/**
|
|
21
|
-
* Default directory name (relative to project root) for MetaObjects' own
|
|
22
|
-
* runtime state: config.json, .gen-state/, package.meta.json, agent docs.
|
|
23
|
-
* Scaffold via `meta init`; most contents are committed to git.
|
|
24
|
-
*/
|
|
25
|
-
export const DEFAULT_METAOBJECTS_DIR = ".metaobjects";
|
|
26
11
|
|
|
27
12
|
/**
|
|
28
13
|
* Options for {@link loadMemory}. Consumers can supply additional
|
|
@@ -50,6 +35,31 @@ export interface LoadMemoryOptions {
|
|
|
50
35
|
* the `meta verify` command opts in to `true` (strict-by-default, #96).
|
|
51
36
|
*/
|
|
52
37
|
strict?: boolean;
|
|
38
|
+
/**
|
|
39
|
+
* An already-resolved, absolute metadata file list — normally
|
|
40
|
+
* `resolveCollection(...).files`. When supplied, `loadMemory` loads exactly
|
|
41
|
+
* these files and resolves nothing itself.
|
|
42
|
+
*
|
|
43
|
+
* Omitting it is not a different WAY of finding metadata, only a different
|
|
44
|
+
* place the same resolution happens: `loadMemory` then calls
|
|
45
|
+
* `resolveCollection(repoRoot)` itself. Passing it saves the second
|
|
46
|
+
* resolution when the caller already holds a collection (every routed CLI
|
|
47
|
+
* command does) and lets a caller load a file set it computed some other
|
|
48
|
+
* way; it can no longer diverge from what the config declares.
|
|
49
|
+
*/
|
|
50
|
+
files?: readonly string[];
|
|
51
|
+
/**
|
|
52
|
+
* MetaObjects-shipped library packages to load ALONGSIDE the project's own files
|
|
53
|
+
* (e.g. `["ai"]` for `metaobjects::ai::LlmCallBase`). Prepended, so an
|
|
54
|
+
* `extends: "metaobjects::ai::LlmCallBase"` in project metadata resolves.
|
|
55
|
+
*
|
|
56
|
+
* Opt-in rather than always-on: a library package registers real top-level nodes, and
|
|
57
|
+
* a project that never references one should not have them appear in its model, its
|
|
58
|
+
* generated output or its docs. Without this the CLI could not load the metadata that
|
|
59
|
+
* shipped generators like `trace-helper` exist to consume, so the generator was
|
|
60
|
+
* reachable from the command line while its input was not (#333).
|
|
61
|
+
*/
|
|
62
|
+
libraries?: readonly string[];
|
|
53
63
|
}
|
|
54
64
|
|
|
55
65
|
/** Default provider bundle threaded by {@link loadMemory} when no options
|
|
@@ -62,10 +72,15 @@ export const defaultLoadMemoryProviders: readonly MetaDataTypeProvider[] = [
|
|
|
62
72
|
];
|
|
63
73
|
|
|
64
74
|
/**
|
|
65
|
-
* Load
|
|
66
|
-
*
|
|
67
|
-
*
|
|
68
|
-
*
|
|
75
|
+
* Load a project's metadata into a single MetaData tree.
|
|
76
|
+
*
|
|
77
|
+
* Which files those are is `resolveCollection`'s decision, never this
|
|
78
|
+
* function's: with no {@link LoadMemoryOptions.files} it calls
|
|
79
|
+
* `resolveCollection(repoRoot)` — nearest-ancestor `.metaobjects/config.json`,
|
|
80
|
+
* then that config's declared `sources`, falling back to the default source
|
|
81
|
+
* directory only when a project declares none. `loadMemory` names no directory
|
|
82
|
+
* of its own, so a caller cannot end up loading from somewhere the rest of the
|
|
83
|
+
* toolchain does not.
|
|
69
84
|
*
|
|
70
85
|
* Excludes `_pending/`. Registers metaobjects core types plus Meta Forge's
|
|
71
86
|
* descriptive top-level types (decision, principle, etc.) so mixed content
|
|
@@ -73,11 +88,15 @@ export const defaultLoadMemoryProviders: readonly MetaDataTypeProvider[] = [
|
|
|
73
88
|
* {@link LoadMemoryOptions.providers}) are composed AFTER the defaults so
|
|
74
89
|
* they may depend on core/forge ids.
|
|
75
90
|
*
|
|
76
|
-
* Throws
|
|
91
|
+
* Throws `ERR_COLLECTION_NOT_FOUND` when nothing resolves (callers should run
|
|
92
|
+
* `meta init`), unless `options.files` is supplied.
|
|
77
93
|
*
|
|
78
|
-
* @param repoRoot
|
|
79
|
-
* `
|
|
80
|
-
*
|
|
94
|
+
* @param repoRoot Where resolution STARTS — the working directory, typically
|
|
95
|
+
* `process.cwd()`. The walk goes up from here for the governing config, so
|
|
96
|
+
* this need not be the project root itself.
|
|
97
|
+
* **Ignored entirely when `options.files` is supplied**: that list is already
|
|
98
|
+
* resolved, so nothing reads this path. Every routed CLI command passes both,
|
|
99
|
+
* and the argument is inert at all of them.
|
|
81
100
|
* @param options Optional {@link LoadMemoryOptions} — supply additional
|
|
82
101
|
* providers or replace the default bundle entirely.
|
|
83
102
|
*/
|
|
@@ -99,16 +118,33 @@ export async function loadMemory(
|
|
|
99
118
|
}
|
|
100
119
|
const registry = composeRegistry(providers);
|
|
101
120
|
|
|
102
|
-
//
|
|
103
|
-
//
|
|
104
|
-
//
|
|
105
|
-
|
|
121
|
+
// Both arms are `resolveCollection`'s answer — one already computed by the
|
|
122
|
+
// caller, one computed here. There is no third way to find metadata, and
|
|
123
|
+
// that is the whole of this line's design: the previous no-`files` arm
|
|
124
|
+
// scanned `<repoRoot>/<default dir>` directly, so a caller that copied the
|
|
125
|
+
// routed shape but forgot `files` silently loaded from a directory the
|
|
126
|
+
// project's config may never have mentioned.
|
|
127
|
+
const paths = options?.files !== undefined
|
|
128
|
+
? [...options.files]
|
|
129
|
+
: [...(await resolveCollection(repoRoot)).files];
|
|
106
130
|
|
|
107
131
|
const loader = new MetaDataLoader({
|
|
108
132
|
registry,
|
|
109
133
|
...(options?.strict === true ? { strict: true } : {}),
|
|
110
134
|
});
|
|
111
|
-
|
|
135
|
+
|
|
136
|
+
// Library sources are imported lazily and only when asked for — the same reason
|
|
137
|
+
// `MetaDataLoader.fromDirectory` does it. `library-sources.ts` reads `node:fs`, so a
|
|
138
|
+
// static import from a root-reachable module drags Node built-ins into every consumer's
|
|
139
|
+
// graph; that is the #287 bundle defect, and the `./library` subpath exists for exactly
|
|
140
|
+
// the reason `./constants` does. Prepended, so a project's `extends` onto a
|
|
141
|
+
// library-shipped abstract base resolves — super resolution is order-independent, but
|
|
142
|
+
// prepending is the deterministic choice and matches `fromDirectory`.
|
|
143
|
+
const libSources =
|
|
144
|
+
options?.libraries !== undefined && options.libraries.length > 0
|
|
145
|
+
? (await import("@metaobjectsdev/metadata/library")).librarySources([...options.libraries])
|
|
146
|
+
: [];
|
|
147
|
+
const result = await loader.load([...libSources, ...paths.map((p) => new FileSource(p))]);
|
|
112
148
|
|
|
113
149
|
if (result.errors.length > 0) {
|
|
114
150
|
const first = result.errors[0]!;
|
|
@@ -117,73 +153,3 @@ export async function loadMemory(
|
|
|
117
153
|
|
|
118
154
|
return result.root;
|
|
119
155
|
}
|
|
120
|
-
|
|
121
|
-
// Dep packages' metaobjects/ files first (topological order), then current.
|
|
122
|
-
async function collectMetadataPaths(repoRoot: string): Promise<string[]> {
|
|
123
|
-
const currentMetaDir = join(repoRoot, ".meta");
|
|
124
|
-
const ws = await discoverWorkspace(repoRoot);
|
|
125
|
-
|
|
126
|
-
// Workspace path: walk extends, load dep metaobjects/ dirs first
|
|
127
|
-
if (ws !== undefined) {
|
|
128
|
-
const currentPkg = ws.packages.find((p) => p.metaDir === currentMetaDir);
|
|
129
|
-
if (currentPkg !== undefined && currentPkg.manifest.extends.length > 0) {
|
|
130
|
-
const ordered = resolveExtendsOrder(ws, currentMetaDir);
|
|
131
|
-
const paths: string[] = [];
|
|
132
|
-
for (const pkg of ordered) {
|
|
133
|
-
// Each workspace package's metadata lives alongside its .meta/ dir
|
|
134
|
-
const pkgRoot = join(pkg.metaDir, "..");
|
|
135
|
-
paths.push(...(await listMetadataFiles(join(pkgRoot, DEFAULT_METADATA_DIR))));
|
|
136
|
-
}
|
|
137
|
-
return paths;
|
|
138
|
-
}
|
|
139
|
-
}
|
|
140
|
-
|
|
141
|
-
// Single-package path: scan metaobjects/ at the project root
|
|
142
|
-
return listMetadataFiles(join(repoRoot, DEFAULT_METADATA_DIR));
|
|
143
|
-
}
|
|
144
|
-
|
|
145
|
-
/**
|
|
146
|
-
* Recursively list metadata files (*.json, *.yaml, *.yml) under a directory,
|
|
147
|
-
* excluding _pending/ at any level. Subdirectories (e.g. projections/) are
|
|
148
|
-
* walked depth-first. Files within a directory are sorted alphabetically for
|
|
149
|
-
* deterministic load order; subdirectories are visited after files at the
|
|
150
|
-
* same level.
|
|
151
|
-
*
|
|
152
|
-
* Format selection (parsing) happens downstream in `FileSource` from
|
|
153
|
-
* `@metaobjectsdev/metadata`, which infers the parser from file extension.
|
|
154
|
-
*/
|
|
155
|
-
async function listMetadataFiles(dir: string): Promise<string[]> {
|
|
156
|
-
let entries: string[];
|
|
157
|
-
try {
|
|
158
|
-
entries = await readdir(dir);
|
|
159
|
-
} catch (err) {
|
|
160
|
-
throw new Error(`loadMemory: cannot read ${dir}: ${(err as Error).message}`);
|
|
161
|
-
}
|
|
162
|
-
const paths: string[] = [];
|
|
163
|
-
const subdirs: string[] = [];
|
|
164
|
-
// #188: sort the raw `readdir` entries so file order is deterministic across
|
|
165
|
-
// runtimes/filesystems (Node vs Bun return different `readdir` orders), matching
|
|
166
|
-
// this function's docstring and the metadata package's own `DirectorySource`.
|
|
167
|
-
// (Resolution is now order-INDEPENDENT — super-resolve.ts #188 — so this is the
|
|
168
|
-
// deterministic-enumeration FLOOR, not the fix; it keeps every derived artifact
|
|
169
|
-
// that preserves declaration order, e.g. serialization, stable across runtimes.)
|
|
170
|
-
for (const entry of [...entries].sort()) {
|
|
171
|
-
if (entry === "_pending") continue;
|
|
172
|
-
const full = join(dir, entry);
|
|
173
|
-
const s = await stat(full);
|
|
174
|
-
if (s.isDirectory()) {
|
|
175
|
-
subdirs.push(full);
|
|
176
|
-
} else if (s.isFile() && isMetadataFile(entry)) {
|
|
177
|
-
paths.push(full);
|
|
178
|
-
}
|
|
179
|
-
}
|
|
180
|
-
// Recurse into subdirectories after collecting files at this level
|
|
181
|
-
for (const sub of subdirs.sort()) {
|
|
182
|
-
paths.push(...(await listMetadataFiles(sub)));
|
|
183
|
-
}
|
|
184
|
-
return paths;
|
|
185
|
-
}
|
|
186
|
-
|
|
187
|
-
function isMetadataFile(name: string): boolean {
|
|
188
|
-
return name.endsWith(".json") || name.endsWith(".yaml") || name.endsWith(".yml");
|
|
189
|
-
}
|
|
@@ -0,0 +1,158 @@
|
|
|
1
|
+
// server/typescript/packages/sdk/src/metadata-files.ts
|
|
2
|
+
//
|
|
3
|
+
// The project's default directory names, what counts as a metadata file, and
|
|
4
|
+
// the one walk that turns a directory into an ordered file list.
|
|
5
|
+
//
|
|
6
|
+
// **This module imports nothing from its siblings, and that is the point.**
|
|
7
|
+
// `resolveCollection` (`collection.ts`) is the single authority on where
|
|
8
|
+
// metadata lives, so `memory.ts`'s `loadMemory` must call it — while
|
|
9
|
+
// `collection.ts` and `sources.ts` need the constants and the walk below.
|
|
10
|
+
// Homing those in `memory.ts` closes an ESM cycle whose failure mode is not a
|
|
11
|
+
// warning but a crash: `DEFAULT_SOURCES` (`sources.ts`) reads
|
|
12
|
+
// `DEFAULT_METADATA_DIR` at module top level, so the cycle surfaces as
|
|
13
|
+
// `ReferenceError: Cannot access 'DEFAULT_METADATA_DIR' before initialization`
|
|
14
|
+
// on whichever module the entry point happens to reach first. A leaf both
|
|
15
|
+
// sides import is the fix; a lazy `await import()` inside `loadMemory` is not
|
|
16
|
+
// — that hides the cycle rather than removing it.
|
|
17
|
+
import { extname, join } from "node:path";
|
|
18
|
+
import { readdir, realpath, stat } from "node:fs/promises";
|
|
19
|
+
|
|
20
|
+
/**
|
|
21
|
+
* The DEFAULT value of `sources` — the directory scanned when
|
|
22
|
+
* `.metaobjects/config.json` declares no sources. Scaffold via `meta init`;
|
|
23
|
+
* the directory is committed to git.
|
|
24
|
+
*
|
|
25
|
+
* **A default, and nothing else.** No read path may assume a directory of this
|
|
26
|
+
* name exists or is where metadata lives: that question is answered by
|
|
27
|
+
* `resolveCollection`, which applies this constant exactly once (via
|
|
28
|
+
* `DEFAULT_SOURCES` in `sources.ts`) when a project declares nothing. A
|
|
29
|
+
* project that declares `sources` may put its metadata anywhere, and every
|
|
30
|
+
* command follows the config. `test/no-hardcoded-metadata-dir.test.ts` is the
|
|
31
|
+
* enforcer.
|
|
32
|
+
*/
|
|
33
|
+
export const DEFAULT_METADATA_DIR = "metaobjects";
|
|
34
|
+
|
|
35
|
+
/**
|
|
36
|
+
* Default directory name (relative to project root) for MetaObjects' own
|
|
37
|
+
* runtime state: config.json, .gen-state/, package.meta.json, agent docs.
|
|
38
|
+
* Scaffold via `meta init`; most contents are committed to git.
|
|
39
|
+
*
|
|
40
|
+
* Unlike {@link DEFAULT_METADATA_DIR} this one IS a fixed convention — it is
|
|
41
|
+
* where the config that answers "where is the metadata?" lives, so it cannot
|
|
42
|
+
* itself be configured.
|
|
43
|
+
*/
|
|
44
|
+
export const DEFAULT_METAOBJECTS_DIR = ".metaobjects";
|
|
45
|
+
|
|
46
|
+
/** Recognized metadata file extensions, matched case-insensitively — mirrors
|
|
47
|
+
* `DirectorySource` in `@metaobjectsdev/metadata`, which checks
|
|
48
|
+
* `extname().toLowerCase()`. The single definition every metadata-file
|
|
49
|
+
* walker in this package uses — and since `resolveSources` (`sources.ts`)
|
|
50
|
+
* calls {@link listMetadataFiles} outright rather than keeping a second
|
|
51
|
+
* recursive walk of its own, there is exactly one walker to keep honest. */
|
|
52
|
+
export const METADATA_EXTENSIONS = new Set([".json", ".yaml", ".yml"]);
|
|
53
|
+
|
|
54
|
+
export function isMetadataFile(name: string): boolean {
|
|
55
|
+
return METADATA_EXTENSIONS.has(extname(name).toLowerCase());
|
|
56
|
+
}
|
|
57
|
+
|
|
58
|
+
/** Directory excluded at every level of {@link listMetadataFiles} — drafts
|
|
59
|
+
* that are deliberately not part of the loaded model. */
|
|
60
|
+
const PENDING_DIR = "_pending";
|
|
61
|
+
|
|
62
|
+
/**
|
|
63
|
+
* Recursively list metadata files (*.json, *.yaml, *.yml, matched
|
|
64
|
+
* case-insensitively — see `isMetadataFile` above) under a directory,
|
|
65
|
+
* excluding _pending/ at any level. Subdirectories (e.g. projections/) are
|
|
66
|
+
* walked depth-first. Files within a directory are sorted alphabetically for
|
|
67
|
+
* deterministic load order; subdirectories are visited AFTER the files at the
|
|
68
|
+
* same level.
|
|
69
|
+
*
|
|
70
|
+
* That per-level rule is a contract, not an implementation detail. This is the
|
|
71
|
+
* order production has always handed the loader, and declaration order survives
|
|
72
|
+
* into generated output: `codegen-ts`'s barrel emits from `root.objects()`
|
|
73
|
+
* order, and so do the shared `enums.ts`, `meta docs` page ordering and `meta
|
|
74
|
+
* export`'s `canonicalSerialize` sibling order. A flat lexicographic sort of
|
|
75
|
+
* absolute paths is NOT the same list — it disagrees whenever a subdirectory
|
|
76
|
+
* name sorts before a sibling file (`common/` before `meta.users.json`) — so
|
|
77
|
+
* `resolveSources` calls this function rather than re-walking and re-sorting.
|
|
78
|
+
* Pinned by `test/source-order.test.ts`.
|
|
79
|
+
*
|
|
80
|
+
* Exported for that gate and for `sources.ts`; not re-exported from the package
|
|
81
|
+
* index — `resolveCollection` is the public door.
|
|
82
|
+
*
|
|
83
|
+
* An entry whose `stat` fails (a dangling symlink, a TOCTOU removal between
|
|
84
|
+
* `readdir` and `stat`, an EACCES entry) is SKIPPED, matching `DirectorySource`
|
|
85
|
+
* in `@metaobjectsdev/metadata`, which this walk otherwise mirrors. A failure to
|
|
86
|
+
* read the directory itself still throws — that is the "you have no metadata
|
|
87
|
+
* here" case callers report.
|
|
88
|
+
*
|
|
89
|
+
* A symlink CYCLE (e.g. `metaobjects/link -> ..`) is a loud error rather than
|
|
90
|
+
* unbounded recursion — see {@link listMetadataFilesGuarded}.
|
|
91
|
+
*
|
|
92
|
+
* Format selection (parsing) happens downstream in `FileSource` from
|
|
93
|
+
* `@metaobjectsdev/metadata`, which infers the parser from file extension.
|
|
94
|
+
*/
|
|
95
|
+
export async function listMetadataFiles(dir: string): Promise<string[]> {
|
|
96
|
+
return listMetadataFilesGuarded(dir, new Set());
|
|
97
|
+
}
|
|
98
|
+
|
|
99
|
+
/**
|
|
100
|
+
* {@link listMetadataFiles}'s recursive worker, carrying the REAL (symlink-
|
|
101
|
+
* resolved) ancestor directories already on this walk branch.
|
|
102
|
+
*
|
|
103
|
+
* This walk follows symlinked directories on purpose (`stat`, not `lstat`,
|
|
104
|
+
* below — matching `DirectorySource` in `@metaobjectsdev/metadata`), so an
|
|
105
|
+
* unguarded directory symlink that revisits an ancestor recurses forever:
|
|
106
|
+
* Java and Python both added this exact guard when this PR promoted
|
|
107
|
+
* symlink-following to a cross-port contract; the TypeScript reference itself
|
|
108
|
+
* did not, even though the corpus cites it as authoritative. `ancestors` is
|
|
109
|
+
* extended only on the recursive call (never mutated in place), so it
|
|
110
|
+
* reflects the current branch, not siblings visited earlier at the same
|
|
111
|
+
* level — a directory legitimately reachable via two different symlinked
|
|
112
|
+
* paths (not a cycle) is not falsely rejected.
|
|
113
|
+
*/
|
|
114
|
+
async function listMetadataFilesGuarded(dir: string, ancestors: ReadonlySet<string>): Promise<string[]> {
|
|
115
|
+
// `realpath` failing (e.g. `dir` vanished between being listed and now) is
|
|
116
|
+
// not this guard's problem — fall back to the given path and let `readdir`
|
|
117
|
+
// below raise its own coded error.
|
|
118
|
+
const real = await realpath(dir).catch(() => dir);
|
|
119
|
+
if (ancestors.has(real)) {
|
|
120
|
+
throw new Error(`symlink loop detected while expanding metadata directory: ${dir} revisits ${real}`);
|
|
121
|
+
}
|
|
122
|
+
const nextAncestors = new Set(ancestors);
|
|
123
|
+
nextAncestors.add(real);
|
|
124
|
+
|
|
125
|
+
let entries: string[];
|
|
126
|
+
try {
|
|
127
|
+
entries = await readdir(dir);
|
|
128
|
+
} catch (err) {
|
|
129
|
+
throw new Error(`cannot read metadata directory ${dir}: ${(err as Error).message}`);
|
|
130
|
+
}
|
|
131
|
+
const paths: string[] = [];
|
|
132
|
+
const subdirs: string[] = [];
|
|
133
|
+
// #188: sort the raw `readdir` entries so file order is deterministic across
|
|
134
|
+
// runtimes/filesystems (Node vs Bun return different `readdir` orders), matching
|
|
135
|
+
// this function's docstring and the metadata package's own `DirectorySource`.
|
|
136
|
+
// (Resolution is now order-INDEPENDENT — super-resolve.ts #188 — so this is the
|
|
137
|
+
// deterministic-enumeration FLOOR, not the fix; it keeps every derived artifact
|
|
138
|
+
// that preserves declaration order, e.g. serialization, stable across runtimes.)
|
|
139
|
+
for (const entry of [...entries].sort()) {
|
|
140
|
+
if (entry === PENDING_DIR) continue;
|
|
141
|
+
const full = join(dir, entry);
|
|
142
|
+
// `stat` (not `lstat`/`Dirent.isDirectory()`) so a symlinked subdirectory is
|
|
143
|
+
// traversed — `DirectorySource` has always followed symlinks this way.
|
|
144
|
+
const s = await stat(full).catch(() => undefined);
|
|
145
|
+
if (s === undefined) continue;
|
|
146
|
+
if (s.isDirectory()) {
|
|
147
|
+
subdirs.push(full);
|
|
148
|
+
} else if (s.isFile() && isMetadataFile(entry)) {
|
|
149
|
+
paths.push(full);
|
|
150
|
+
}
|
|
151
|
+
}
|
|
152
|
+
// Recurse into subdirectories after collecting files at this level.
|
|
153
|
+
// `subdirs` is already in sorted order (built from the sorted `entries` above).
|
|
154
|
+
for (const sub of subdirs) {
|
|
155
|
+
paths.push(...(await listMetadataFilesGuarded(sub, nextAncestors)));
|
|
156
|
+
}
|
|
157
|
+
return paths;
|
|
158
|
+
}
|
package/src/scope.ts
ADDED
|
@@ -0,0 +1,95 @@
|
|
|
1
|
+
// server/typescript/packages/sdk/src/scope.ts
|
|
2
|
+
//
|
|
3
|
+
// Phase-1 metadata-source-resolution — the scope pattern engine.
|
|
4
|
+
//
|
|
5
|
+
// A pure, no-I/O module deciding whether a fully-qualified node name falls
|
|
6
|
+
// inside a consumer's declared `include`/`exclude` scope. Source resolution
|
|
7
|
+
// and discovery (later phase-1 tasks) build on this; a cross-language
|
|
8
|
+
// conformance corpus pins its semantics, so exact pattern behavior matters.
|
|
9
|
+
|
|
10
|
+
// NOTE: PACKAGE_SEPARATOR is NOT re-exported from the browser-safe
|
|
11
|
+
// `@metaobjectsdev/metadata/constants` barrel (that barrel only re-exports
|
|
12
|
+
// the per-concern `*-constants.ts` modules; `PACKAGE_SEPARATOR` lives in
|
|
13
|
+
// `shared/structural.ts`, exported from the package root). This package
|
|
14
|
+
// (`@metaobjectsdev/sdk`) is server-side, not a `client/web/**` browser
|
|
15
|
+
// package, so importing metamodel values from the root — the same thing
|
|
16
|
+
// `memory.ts` and `forge-types.ts` in this package already do — is correct.
|
|
17
|
+
import { PACKAGE_SEPARATOR, ParseError, codeSource } from "@metaobjectsdev/metadata";
|
|
18
|
+
|
|
19
|
+
/** A consumer-side output filter over fully-qualified node names. */
|
|
20
|
+
export interface Scope {
|
|
21
|
+
/** Absent or empty means "everything". */
|
|
22
|
+
readonly include?: readonly string[];
|
|
23
|
+
/** Applied after `include`. */
|
|
24
|
+
readonly exclude?: readonly string[];
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
export interface CompiledScope {
|
|
28
|
+
readonly include: readonly RegExp[];
|
|
29
|
+
readonly exclude: readonly RegExp[];
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
/** One package segment: any run of characters containing no separator char. */
|
|
33
|
+
const SEGMENT = "[^:]+";
|
|
34
|
+
/** One or more segments, separator-joined — the `**` expansion. */
|
|
35
|
+
const SEGMENTS = `${SEGMENT}(?:${PACKAGE_SEPARATOR}${SEGMENT})*`;
|
|
36
|
+
|
|
37
|
+
function escapeLiteral(text: string): string {
|
|
38
|
+
return text.replace(/[.*+?^${}()|[\]\\]/g, "\\$&");
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
/** Compile one segment. `**` spans segments; `*` never crosses a separator. */
|
|
42
|
+
function compileSegment(segment: string, pattern: string): string {
|
|
43
|
+
if (segment.length === 0) {
|
|
44
|
+
throw new ParseError(`empty segment in scope pattern "${pattern}"`, {
|
|
45
|
+
code: "ERR_SCOPE_PATTERN_INVALID",
|
|
46
|
+
source: codeSource("compileSegment"),
|
|
47
|
+
});
|
|
48
|
+
}
|
|
49
|
+
// A segment surviving the split on the two-character PACKAGE_SEPARATOR
|
|
50
|
+
// ("::") can still contain a lone ":" when the pattern has an odd colon
|
|
51
|
+
// run — e.g. "acme:::Order".split("::") => ["acme", ":Order"]. SEGMENT
|
|
52
|
+
// ([^:]+) already excludes ":" from a well-formed segment, so a leftover
|
|
53
|
+
// ":" here means the separator was malformed, not that ":" is meant
|
|
54
|
+
// literally. Left unchecked, escapeLiteral treats it as a literal
|
|
55
|
+
// character and compiles a regex requiring three colons in a row — which
|
|
56
|
+
// no legal "::"-joined fully-qualified name can ever contain, so the
|
|
57
|
+
// pattern silently matches nothing instead of failing loud.
|
|
58
|
+
if (segment.includes(":")) {
|
|
59
|
+
throw new ParseError(
|
|
60
|
+
`scope pattern "${pattern}" has a malformed separator (an odd run of ":") — segments are joined by "::", never a single ":"`,
|
|
61
|
+
{ code: "ERR_SCOPE_PATTERN_INVALID", source: codeSource("compileSegment") },
|
|
62
|
+
);
|
|
63
|
+
}
|
|
64
|
+
if (segment === "**") return `(?:${SEGMENTS})`;
|
|
65
|
+
// `*` inside a segment matches any characters except the separator char.
|
|
66
|
+
return segment.split("*").map(escapeLiteral).join("[^:]*");
|
|
67
|
+
}
|
|
68
|
+
|
|
69
|
+
export function compilePattern(pattern: string): RegExp {
|
|
70
|
+
if (pattern.length === 0) {
|
|
71
|
+
throw new ParseError(`scope pattern must not be empty`, {
|
|
72
|
+
code: "ERR_SCOPE_PATTERN_INVALID",
|
|
73
|
+
source: codeSource("compilePattern"),
|
|
74
|
+
});
|
|
75
|
+
}
|
|
76
|
+
const body = pattern
|
|
77
|
+
.split(PACKAGE_SEPARATOR)
|
|
78
|
+
.map((segment) => compileSegment(segment, pattern))
|
|
79
|
+
.join(PACKAGE_SEPARATOR);
|
|
80
|
+
return new RegExp(`^${body}$`);
|
|
81
|
+
}
|
|
82
|
+
|
|
83
|
+
export function compileScope(scope: Scope): CompiledScope {
|
|
84
|
+
return {
|
|
85
|
+
include: (scope.include ?? []).map(compilePattern),
|
|
86
|
+
exclude: (scope.exclude ?? []).map(compilePattern),
|
|
87
|
+
};
|
|
88
|
+
}
|
|
89
|
+
|
|
90
|
+
/** True when `fqn` is inside the scope. An empty `include` means everything. */
|
|
91
|
+
export function matchesScope(fqn: string, compiled: CompiledScope): boolean {
|
|
92
|
+
const included = compiled.include.length === 0 || compiled.include.some((re) => re.test(fqn));
|
|
93
|
+
if (!included) return false;
|
|
94
|
+
return !compiled.exclude.some((re) => re.test(fqn));
|
|
95
|
+
}
|