archgraph-argo-beta 0.27.0-beta.1 → 0.27.0-beta.11

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/README.md CHANGED
@@ -88,6 +88,23 @@ After installing, open your project and start a coding agent. It will:
88
88
 
89
89
  The intent architecture graph — modelled in **ArchiMate 3.2** — is the single source of truth.
90
90
 
91
+ ## The federation model
92
+
93
+ ArchGraph is a **federated intent graph**, not one big distributed graph. Every project owns a
94
+ complete, **sovereign** canonical graph (`design/KG/SystemArchitecture.json`) and stays the single
95
+ source of truth for its own model — nothing is split, sharded, or merged into a central super-graph.
96
+
97
+ Projects **co-build** by federation: a member **self-registers** with the federation center, others
98
+ **discover** it, reads are **explicitly authorized and denied by default**, and a cross-project read
99
+ returns a **reference, not a copy**. The center holds only federation metadata — membership,
100
+ capabilities, interfaces, grants — plus a read-only mirror: it is a **registry + broker**, never the
101
+ content.
102
+
103
+ The pattern is **Registry–Broker Federation over sovereign knowledge graphs**: self-registration +
104
+ discovery + capability-based authorization + reference-not-copy, mediated by a broker. It is
105
+ *federated*, not "distributed" — a distributed model would mean one logical graph partitioned across
106
+ nodes, which is exactly what this design avoids.
107
+
91
108
  ## Community
92
109
 
93
110
  ArchGraph runs on open co-building. Join the community hub to share, browse and reuse **architecture
@@ -2,6 +2,8 @@
2
2
  "$schema": "https://json-schema.org/draft/2020-12/schema",
3
3
  "title": "ArchiMate 3.2 default schema bundle descriptor",
4
4
  "description": "Descriptor for the default ARGO schema bundle (ArchiMate 3.2 + ARGO extensions). A repository may override this bundle by placing its own SystemArchitecture.schema.json (plus an optional schema-bundle.config.json / schema-bundle.rules.json) under <workspace>/.argo/schema.",
5
+ "id": "default",
6
+ "aliases": ["archimate3.2", "archimate", "base"],
5
7
  "language": "ArchiMate 3.2",
6
8
  "elementTypeEnumKey": "archimateElementType",
7
9
  "relationshipTypeEnumKey": "archimateRelationshipType",
@@ -186,7 +186,7 @@ function diagnose(session, opts = {}) {
186
186
  const byQueryClass = {};
187
187
  const sigCount = {};
188
188
  const pathReads = {};
189
- let toolMs = 0; let humanWaitMs = 0; let errors = 0; let empty = 0;
189
+ let toolMs = 0; let humanWaitMs = 0; let errors = 0; let empty = 0; let emptyOrError = 0;
190
190
  for (const c of tc) {
191
191
  const isHuman = c.queryClass === 'human-wait' || HUMAN_WAIT_TOOLS.some(t => String(c.tool).includes(t));
192
192
  const bt = byTool[c.tool] || (byTool[c.tool] = { calls: 0, ms: 0, tokens: 0, errors: 0 });
@@ -200,6 +200,9 @@ function diagnose(session, opts = {}) {
200
200
  }
201
201
  if (c.ok === false) { bt.errors += 1; errors += 1; }
202
202
  if ((c.outputBytes || 0) < 2) empty += 1;
203
+ // A failed call with empty output is ONE unproductive call, not two — count the
204
+ // union so the hint does not overstate "empty/error" (issue #8 口径).
205
+ if (c.ok === false || (c.outputBytes || 0) < 2) emptyOrError += 1;
203
206
  byQueryClass[c.queryClass] = (byQueryClass[c.queryClass] || 0) + 1;
204
207
  sigCount[c.signature] = (sigCount[c.signature] || 0) + 1;
205
208
  if (c.queryClass === 'file-read' && c.path) pathReads[c.path] = (pathReads[c.path] || 0) + 1;
@@ -245,12 +248,12 @@ function diagnose(session, opts = {}) {
245
248
  overSearch: {
246
249
  duplicateCalls: duplicates,
247
250
  repeatedReads,
248
- emptyOrError: errors + empty,
251
+ emptyOrError,
249
252
  noProgressStreak: best,
250
253
  },
251
254
  topOffendersByTime: topByMs,
252
255
  topOffendersByTokens: topByTokens,
253
- hints: buildHints({ toolCalls: tc.length, duplicates: duplicates.length, emptyOrError: errors + empty, repeatedReads: repeatedReads.length, noProgressStreak: best, modelMs, mcpMs: byBackend.graph.ms, repoToolMs: byBackend.repo.ms, roundTrips: countRoundTrips(tc), blowups: blowups.length, blowupMax: blowups.length ? blowups[0].input : 0 }),
256
+ hints: buildHints({ toolCalls: tc.length, duplicates: duplicates.length, emptyOrError, repeatedReads: repeatedReads.length, noProgressStreak: best, modelMs, mcpMs: byBackend.graph.ms, repoToolMs: byBackend.repo.ms, roundTrips: countRoundTrips(tc), blowups: blowups.length, blowupMax: blowups.length ? blowups[0].input : 0 }),
254
257
  };
255
258
  }
256
259
 
@@ -149,12 +149,19 @@ const TOOLS = [
149
149
  },
150
150
  {
151
151
  name: 'addArchitectureElement',
152
- description: 'Use for one element. Creates a new element or adds an existing element to view_ids. view_ids is required so elements never exist outside views.',
152
+ description: 'Use for one element. Creates a new element or adds an existing element to view_ids. view_ids is required so elements never exist outside views. element.id is REQUIRED: a non-empty, caller-assigned, stable string — there is no auto-id (reuse an existing id, or allocate the next one from the graph).',
153
153
  inputSchema: {
154
154
  type: 'object',
155
155
  required: ['element', 'view_ids'],
156
156
  properties: {
157
- element: { type: 'object' },
157
+ element: {
158
+ type: 'object',
159
+ required: ['id'],
160
+ properties: {
161
+ id: { type: 'string', minLength: 1, description: 'Element id (non-empty, caller-assigned, stable). REQUIRED — the graph is written by id and there is NO auto-id: reuse an existing id (onConflict:"reuse" with the same (type,name)) or allocate the next one (e.g. MATCH (e:Element {graphKey:$graphKey}) RETURN max(toInteger(e.id))).' },
162
+ },
163
+ description: 'The element to create/attach. MUST include a non-empty `id`.',
164
+ },
158
165
  view_ids: { type: 'array', minItems: 1, items: { type: 'string' } },
159
166
  onConflict: { type: 'string', enum: ['reuse', 'allowDuplicate'], description: 'Dedup policy (default reuse). reuse: find-or-create — attach an existing exact (type, name) match; a same-type semantic near-duplicate also blocks creation. allowDuplicate: create anyway (even if a duplicate exists), requires a justification.' },
160
167
  justification: { type: 'string', description: 'Required when onConflict is allowDuplicate.' },
@@ -420,6 +427,13 @@ async function callTool(name, args = {}, progressToken = null, dependencies = un
420
427
  tool: name,
421
428
  args,
422
429
  });
430
+ // getSystemArchitecture declares an outputSchema, so its result MUST carry
431
+ // matching structuredContent even on the cross-project path (which bypasses
432
+ // the systemarchitecture module's own toolResult). A missing
433
+ // structuredContent makes the MCP client reject the call with -32600.
434
+ if (name === 'getSystemArchitecture') {
435
+ return toolResult(result, systemArchitectureMcp.buildGetSystemArchitectureStructuredContent(result));
436
+ }
423
437
  return toolResult(result);
424
438
  }
425
439
  if (name === 'initializeWorkspace') {
@@ -678,7 +692,7 @@ function normalizeRelativePath(value) {
678
692
  return String(value).replace(/\\/g, '/');
679
693
  }
680
694
 
681
- function toolResult(payload) {
695
+ function toolResult(payload, structuredContent = undefined) {
682
696
  return {
683
697
  content: [
684
698
  {
@@ -686,6 +700,7 @@ function toolResult(payload) {
686
700
  text: JSON.stringify(payload, null, 2),
687
701
  },
688
702
  ],
703
+ ...(structuredContent === undefined ? {} : { structuredContent }),
689
704
  isError: payload.status === 'failed',
690
705
  };
691
706
  }
@@ -259,8 +259,105 @@ function validateViewElementLimits(document, errors, options = {}) {
259
259
  }
260
260
  }
261
261
 
262
+ // Per-element-type attribute contract (issue #3): required attributes, a
263
+ // controlled vocabulary per attribute (enumByAttr) and uniqueness per attribute.
264
+ // Supplied by the active schema bundle's `attributesByElementType`; absent => no-op
265
+ // (fully backward compatible).
266
+ function attributeEntriesByName(element) {
267
+ const map = new Map();
268
+ const entries = Array.isArray(element && element.attributes) ? element.attributes : [];
269
+ for (const entry of entries) {
270
+ if (!entry || typeof entry !== 'object' || typeof entry.name !== 'string' || entry.name === '') {
271
+ continue;
272
+ }
273
+ if (!map.has(entry.name)) {
274
+ map.set(entry.name, []);
275
+ }
276
+ map.get(entry.name).push(entry);
277
+ }
278
+ return map;
279
+ }
280
+
281
+ function attributeValueOf(entry) {
282
+ if (entry.value !== undefined && entry.value !== null) {
283
+ return entry.value;
284
+ }
285
+ if (entry.content !== undefined && entry.content !== null) {
286
+ return entry.content;
287
+ }
288
+ return undefined;
289
+ }
290
+
291
+ function validateAttributeContracts(document, errors, ontology) {
292
+ const language = resolveOntology(ontology);
293
+ const contracts = language.attributesByElementType;
294
+ if (!contracts || typeof contracts !== 'object') {
295
+ return;
296
+ }
297
+ const uniqueTrackers = new Map();
298
+ for (const type of Object.keys(contracts)) {
299
+ uniqueTrackers.set(type, new Map());
300
+ }
301
+
302
+ for (const element of document.elements || []) {
303
+ if (!element || typeof element !== 'object') {
304
+ continue;
305
+ }
306
+ const contract = contracts[element.type];
307
+ if (!contract || typeof contract !== 'object') {
308
+ continue;
309
+ }
310
+ const byName = attributeEntriesByName(element);
311
+
312
+ for (const name of Array.isArray(contract.required) ? contract.required : []) {
313
+ const entries = byName.get(name);
314
+ const present = Array.isArray(entries) && entries.some((entry) => {
315
+ const value = attributeValueOf(entry);
316
+ return value !== undefined && value !== '';
317
+ });
318
+ if (!present) {
319
+ errors.push(`element '${element.id}' (type '${element.type}') is missing required attribute '${name}'`);
320
+ }
321
+ }
322
+
323
+ const enumByAttr = contract.enumByAttr && typeof contract.enumByAttr === 'object' ? contract.enumByAttr : {};
324
+ for (const [attr, allowed] of Object.entries(enumByAttr)) {
325
+ if (!Array.isArray(allowed)) {
326
+ continue;
327
+ }
328
+ for (const entry of byName.get(attr) || []) {
329
+ const value = attributeValueOf(entry);
330
+ if (value === undefined) {
331
+ continue;
332
+ }
333
+ const matches = allowed.some((option) => option === value || JSON.stringify(option) === JSON.stringify(value));
334
+ if (!matches) {
335
+ errors.push(`element '${element.id}' (type '${element.type}') attribute '${attr}' value ${JSON.stringify(value)} is not one of: ${allowed.map((option) => JSON.stringify(option)).join(', ')}`);
336
+ }
337
+ }
338
+ }
339
+
340
+ for (const attr of Array.isArray(contract.unique) ? contract.unique : []) {
341
+ const tracker = uniqueTrackers.get(element.type);
342
+ for (const entry of byName.get(attr) || []) {
343
+ const value = attributeValueOf(entry);
344
+ if (value === undefined) {
345
+ continue;
346
+ }
347
+ const key = JSON.stringify(value);
348
+ if (tracker.has(key) && tracker.get(key) !== element.id) {
349
+ errors.push(`element '${element.id}' (type '${element.type}') attribute '${attr}' value ${key} duplicates element '${tracker.get(key)}'`);
350
+ } else if (!tracker.has(key)) {
351
+ tracker.set(key, element.id);
352
+ }
353
+ }
354
+ }
355
+ }
356
+ }
357
+
262
358
  module.exports = {
263
359
  validateGraphSemantics,
264
360
  validateArchiMateEndpointMatrix,
265
361
  validateViewElementLimits,
362
+ validateAttributeContracts,
266
363
  };
@@ -99,7 +99,8 @@ function relativeLabel(workspaceRoot, absolutePath) {
99
99
 
100
100
  function buildBundle(kind, dir, workspaceRoot) {
101
101
  const schemaFile = path.join(dir, SCHEMA_BASENAME);
102
- const schema = readJsonFile(schemaFile);
102
+ const schemaExists = isFile(schemaFile);
103
+ const schema = schemaExists ? readJsonFile(schemaFile) : null;
103
104
 
104
105
  const configFile = path.join(dir, CONFIG_BASENAME);
105
106
  const fileConfig = isFile(configFile) ? readJsonFile(configFile) : {};
@@ -135,10 +136,9 @@ function buildBundle(kind, dir, workspaceRoot) {
135
136
  kind,
136
137
  dir,
137
138
  relativeDir: relativeLabel(workspaceRoot, dir),
138
- schema: {
139
- absolutePath: schemaFile,
140
- relativePath: relativeLabel(workspaceRoot, schemaFile),
141
- },
139
+ schema: schemaExists
140
+ ? { absolutePath: schemaFile, relativePath: relativeLabel(workspaceRoot, schemaFile) }
141
+ : null,
142
142
  schemaDocument: schema,
143
143
  config: {
144
144
  filePath: isFile(configFile) ? { absolutePath: configFile, relativePath: relativeLabel(workspaceRoot, configFile) } : null,
@@ -154,6 +154,262 @@ function buildBundle(kind, dir, workspaceRoot) {
154
154
  };
155
155
  }
156
156
 
157
+ // --- Bundle inheritance (issue #4) -----------------------------------------
158
+ // A bundle may declare `extends` to inherit a base bundle (the built-in default,
159
+ // or another bundle directory) and add/override on top of it instead of forking
160
+ // the whole rules file. `addElementTypes` / `addRelationships` / `overrideMatrix`
161
+ // are the delta; the effective element universe becomes base ∪ add (single
162
+ // source of truth), matrix/metadata merge by key.
163
+
164
+ function bundleConfig(dir) {
165
+ const configFile = path.join(dir, CONFIG_BASENAME);
166
+ if (!isFile(configFile)) {
167
+ return {};
168
+ }
169
+ try {
170
+ return readJsonFile(configFile);
171
+ } catch {
172
+ return {};
173
+ }
174
+ }
175
+
176
+ function hasExtendsConfig(dir) {
177
+ const config = bundleConfig(dir);
178
+ return typeof config.extends === 'string' && config.extends.trim() !== '';
179
+ }
180
+
181
+ // Symbolic names a bundle declares about itself (`id` + `aliases`) in its config.
182
+ // The framework resolves an `extends` value against these DECLARED names — never a
183
+ // name hardcoded in framework logic — so a bundle's identity/aliases are data.
184
+ function bundleSymbolicNames(dir) {
185
+ const config = bundleConfig(dir);
186
+ const names = new Set();
187
+ if (typeof config.id === 'string' && config.id.trim() !== '') {
188
+ names.add(config.id.trim());
189
+ }
190
+ if (Array.isArray(config.aliases)) {
191
+ for (const alias of config.aliases) {
192
+ if (typeof alias === 'string' && alias.trim() !== '') {
193
+ names.add(alias.trim());
194
+ }
195
+ }
196
+ }
197
+ return names;
198
+ }
199
+
200
+ function isPathLikeExt(value) {
201
+ return value.includes('/') || value.includes('\\') || value.startsWith('.') || value.startsWith('~')
202
+ || /^[a-zA-Z]:/.test(value);
203
+ }
204
+
205
+ // Bounded, general candidate list for symbolic bundle resolution: the built-in
206
+ // default bundle, any directory a bundle lists in its own `basePaths`, the bundle's
207
+ // own directory, and its immediate sub-directories. The framework matches a name
208
+ // against each candidate's SELF-DECLARED id/aliases (data) — it hardcodes no
209
+ // modeling-language name. A non-matching symbolic name falls back to a path, so
210
+ // bare relative directory references keep working.
211
+ function candidateBaseDirs(childDir, config) {
212
+ const dirs = [];
213
+ const seen = new Set();
214
+ const push = (dir) => {
215
+ if (!dir) {
216
+ return;
217
+ }
218
+ const resolved = path.resolve(dir);
219
+ const key = resolved.toLowerCase();
220
+ if (!seen.has(key)) {
221
+ seen.add(key);
222
+ dirs.push(resolved);
223
+ }
224
+ };
225
+ push(path.join(getArgoRoot(), 'schema'));
226
+ const basePaths = config && Array.isArray(config.basePaths) ? config.basePaths : [];
227
+ for (const basePath of basePaths) {
228
+ if (typeof basePath === 'string' && basePath.trim() !== '') {
229
+ push(path.resolve(childDir, basePath.trim()));
230
+ }
231
+ }
232
+ push(childDir);
233
+ let entries = [];
234
+ try {
235
+ entries = fs.readdirSync(childDir, { withFileTypes: true });
236
+ } catch {
237
+ entries = [];
238
+ }
239
+ for (const entry of entries) {
240
+ if (entry.isDirectory()) {
241
+ push(path.join(childDir, entry.name));
242
+ }
243
+ }
244
+ return dirs;
245
+ }
246
+
247
+ function resolveBaseBundleDir(ext, childDir, config) {
248
+ const value = String(ext).trim();
249
+ // `default` is the reserved, language-neutral name of the built-in bundle.
250
+ if (value === 'default') {
251
+ return path.join(getArgoRoot(), 'schema');
252
+ }
253
+ if (!isPathLikeExt(value)) {
254
+ for (const dir of candidateBaseDirs(childDir, config)) {
255
+ if (bundleSymbolicNames(dir).has(value)) {
256
+ return dir;
257
+ }
258
+ }
259
+ }
260
+ // Not a symbolic hit: treat as a path relative to the bundle directory.
261
+ return path.resolve(childDir, value);
262
+ }
263
+
264
+ function deepCloneJson(value) {
265
+ return value === undefined ? undefined : JSON.parse(JSON.stringify(value));
266
+ }
267
+
268
+ function mergeMatrixInto(target, source) {
269
+ if (!source || typeof source !== 'object') {
270
+ return target;
271
+ }
272
+ for (const [relationshipType, bySource] of Object.entries(source)) {
273
+ if (!bySource || typeof bySource !== 'object') {
274
+ target[relationshipType] = deepCloneJson(bySource);
275
+ continue;
276
+ }
277
+ if (!target[relationshipType] || typeof target[relationshipType] !== 'object') {
278
+ target[relationshipType] = {};
279
+ }
280
+ for (const [sourceType, targets] of Object.entries(bySource)) {
281
+ target[relationshipType][sourceType] = deepCloneJson(targets);
282
+ }
283
+ }
284
+ return target;
285
+ }
286
+
287
+ // Append added types to a JSON-Schema enum def (the element/relationship type
288
+ // universe), so a schema-less inheriting Profile's own added types are structurally
289
+ // valid — not just present at the ontology level.
290
+ function extendSchemaEnum(schemaDocument, enumKeys, additions) {
291
+ const defs = schemaDocument && typeof schemaDocument.$defs === 'object' ? schemaDocument.$defs : null;
292
+ if (!defs) {
293
+ return;
294
+ }
295
+ for (const key of enumKeys) {
296
+ const node = defs[key];
297
+ if (node && Array.isArray(node.enum)) {
298
+ for (const value of additions) {
299
+ if (typeof value === 'string' && value !== '' && !node.enum.includes(value)) {
300
+ node.enum.push(value);
301
+ }
302
+ }
303
+ return;
304
+ }
305
+ }
306
+ }
307
+
308
+ function mergeExtendsBundle(base, child) {
309
+ const baseRules = base.rules || {};
310
+ const childRules = child.rules || {};
311
+ const config = { ...(base.config || {}), ...(child.config || {}) };
312
+
313
+ const elementTypeMetadata = { ...(baseRules.elementTypeMetadata || {}), ...(childRules.elementTypeMetadata || {}) };
314
+ const archimateClassByElementType = { ...(baseRules.archimateClassByElementType || {}), ...(childRules.archimateClassByElementType || {}) };
315
+ const relationshipCategoryByType = { ...(baseRules.relationshipCategoryByType || {}), ...(childRules.relationshipCategoryByType || {}) };
316
+
317
+ const addElementTypes = config.addElementTypes && typeof config.addElementTypes === 'object' ? config.addElementTypes : {};
318
+ for (const [type, meta] of Object.entries(addElementTypes)) {
319
+ if (!elementTypeMetadata[type]) {
320
+ elementTypeMetadata[type] = { layer: (meta && meta.layer) || null, aspect: (meta && meta.aspect) || null };
321
+ }
322
+ if (meta && typeof meta.class === 'string' && meta.class !== '') {
323
+ archimateClassByElementType[type] = meta.class;
324
+ }
325
+ }
326
+ const addRelationships = config.addRelationships && typeof config.addRelationships === 'object' ? config.addRelationships : {};
327
+ for (const [type, category] of Object.entries(addRelationships)) {
328
+ relationshipCategoryByType[type] = category;
329
+ }
330
+
331
+ // Guarantee base ∪ add for the element/relationship universe regardless of the
332
+ // base dialect (class-matrix derives types from rules metadata, type-matrix from
333
+ // schema enums) — an inheriting Profile must never silently drop a base type.
334
+ const baseOntology = buildOntology(base);
335
+ for (const type of baseOntology.elementTypes) {
336
+ if (!elementTypeMetadata[type]) {
337
+ elementTypeMetadata[type] = { layer: null, aspect: null };
338
+ }
339
+ }
340
+ for (const type of baseOntology.relationshipTypes) {
341
+ if (!relationshipCategoryByType[type]) {
342
+ relationshipCategoryByType[type] = 'Custom';
343
+ }
344
+ }
345
+
346
+ const relationshipTargetMatrix = deepCloneJson(baseRules.relationshipTargetMatrix || {}) || {};
347
+ mergeMatrixInto(relationshipTargetMatrix, childRules.relationshipTargetMatrix || {});
348
+ mergeMatrixInto(relationshipTargetMatrix, config.overrideMatrix || {});
349
+
350
+ // Inherit the base dialect as-is; never default to a specific dialect NAME here
351
+ // (that would couple the generic merge to one modeling language). When both are
352
+ // absent the ontology builder decides from the bundle's declared rules.
353
+ const dialect = childRules.dialect || baseRules.dialect;
354
+ const rules = {
355
+ ...baseRules,
356
+ ...childRules,
357
+ dialect,
358
+ elementTypeMetadata,
359
+ archimateClassByElementType,
360
+ relationshipCategoryByType,
361
+ relationshipTargetMatrix,
362
+ };
363
+
364
+ // Structural schema: when the child has none, it inherits the base schema — but
365
+ // its added types must be valid against the base's $defs enum too, otherwise
366
+ // whole-graph validation (validateAgainstSchema) rejects elements of the added
367
+ // types. Extend the inherited schema enum in place (clone, never mutate base).
368
+ let schemaDocument = child.schemaDocument || base.schemaDocument;
369
+ if (!child.schemaDocument && base.schemaDocument) {
370
+ const extraElementTypes = Object.keys(addElementTypes).filter(Boolean);
371
+ const extraRelationshipTypes = Object.keys(addRelationships).filter(Boolean);
372
+ const extraFromConfigElements = Array.isArray(config.elementTypes) ? config.elementTypes : [];
373
+ const extraFromConfigRelationships = Array.isArray(config.relationshipTypes) ? config.relationshipTypes : [];
374
+ if (extraElementTypes.length || extraRelationshipTypes.length || extraFromConfigElements.length || extraFromConfigRelationships.length) {
375
+ schemaDocument = deepCloneJson(base.schemaDocument);
376
+ extendSchemaEnum(schemaDocument, ELEMENT_ENUM_KEYS, extraElementTypes.concat(extraFromConfigElements));
377
+ extendSchemaEnum(schemaDocument, RELATIONSHIP_ENUM_KEYS, extraRelationshipTypes.concat(extraFromConfigRelationships));
378
+ }
379
+ }
380
+
381
+ return {
382
+ ...child,
383
+ schema: child.schema || base.schema,
384
+ schemaDocument,
385
+ rules,
386
+ config,
387
+ inheritedFrom: base.dir,
388
+ chainDirs: Array.from(new Set([
389
+ ...(Array.isArray(base.chainDirs) && base.chainDirs.length > 0 ? base.chainDirs : [path.resolve(base.dir).toLowerCase()]),
390
+ path.resolve(child.dir).toLowerCase(),
391
+ ])),
392
+ };
393
+ }
394
+
395
+ function applyExtends(bundle, workspaceRoot, seen = new Set()) {
396
+ const ext = bundle && bundle.config && typeof bundle.config.extends === 'string' ? bundle.config.extends.trim() : '';
397
+ if (!ext) {
398
+ return bundle;
399
+ }
400
+ seen.add(path.resolve(bundle.dir).toLowerCase());
401
+ const baseDir = resolveBaseBundleDir(ext, bundle.dir, bundle.config);
402
+ const baseKey = path.resolve(baseDir).toLowerCase();
403
+ if (seen.has(baseKey)) {
404
+ throw new Error(`schema bundle extends cycle detected at '${baseDir}'`);
405
+ }
406
+ if (!isFile(path.join(baseDir, SCHEMA_BASENAME)) && !hasExtendsConfig(baseDir)) {
407
+ throw new Error(`schema bundle extends '${ext}' could not be resolved: no schema bundle at '${baseDir}'`);
408
+ }
409
+ const baseBundle = applyExtends(buildBundle('extends', baseDir, workspaceRoot), workspaceRoot, seen);
410
+ return mergeExtendsBundle(baseBundle, bundle);
411
+ }
412
+
157
413
  function resolveSchemaBundle(workspaceRoot, options = {}) {
158
414
  const root = path.resolve(workspaceRoot || process.cwd());
159
415
  const candidates = [];
@@ -172,8 +428,8 @@ function resolveSchemaBundle(workspaceRoot, options = {}) {
172
428
  continue;
173
429
  }
174
430
  seen.add(key);
175
- if (isFile(path.join(candidate.dir, SCHEMA_BASENAME))) {
176
- return buildBundle(candidate.kind, candidate.dir, root);
431
+ if (isFile(path.join(candidate.dir, SCHEMA_BASENAME)) || hasExtendsConfig(candidate.dir)) {
432
+ return applyExtends(buildBundle(candidate.kind, candidate.dir, root), root);
177
433
  }
178
434
  }
179
435
 
@@ -258,7 +514,77 @@ function resolveActorElementType(config) {
258
514
  return DEFAULT_ACTOR_ELEMENT_TYPE;
259
515
  }
260
516
 
261
- function validateBundle({ language, dialect, elementTypes, relationshipTypes, actorElementType, matrix, deliveryDependencies }) {
517
+ // Per-element-type attribute contract (issue #3): declare that an element type
518
+ // must carry certain attributes, that some attribute values are a controlled
519
+ // vocabulary, and that some attribute values are unique within the type.
520
+ // { "Rule": { "required": ["ruleId","normativity"], "unique": ["ruleId"],
521
+ // "enumByAttr": { "normativity": ["MUST","SHOULD","MAY","MUST_NOT"] } } }
522
+ // Absent => no per-type attribute contract (backward compatible).
523
+ function resolveAttributeContracts(config) {
524
+ const raw = config && typeof config.attributesByElementType === 'object' && config.attributesByElementType !== null
525
+ ? config.attributesByElementType
526
+ : null;
527
+ if (!raw) {
528
+ return undefined;
529
+ }
530
+ const contracts = {};
531
+ for (const [type, contract] of Object.entries(raw)) {
532
+ if (!contract || typeof contract !== 'object') {
533
+ contracts[type] = {};
534
+ continue;
535
+ }
536
+ contracts[type] = {
537
+ required: Array.isArray(contract.required) ? contract.required.slice() : undefined,
538
+ unique: Array.isArray(contract.unique) ? contract.unique.slice() : undefined,
539
+ enumByAttr: contract.enumByAttr && typeof contract.enumByAttr === 'object' ? { ...contract.enumByAttr } : undefined,
540
+ };
541
+ }
542
+ return contracts;
543
+ }
544
+
545
+ function validateAttributeContracts(language, elementTypeList, contracts) {
546
+ const errors = [];
547
+ if (!contracts || typeof contracts !== 'object') {
548
+ return errors;
549
+ }
550
+ for (const [type, contract] of Object.entries(contracts)) {
551
+ if (!elementTypeList.includes(type)) {
552
+ errors.push(`schema bundle '${language}' attributesByElementType references unknown element type '${type}'`);
553
+ }
554
+ if (!contract || typeof contract !== 'object') {
555
+ continue;
556
+ }
557
+ for (const key of ['required', 'unique']) {
558
+ const list = contract[key];
559
+ if (list === undefined) {
560
+ continue;
561
+ }
562
+ if (!Array.isArray(list)) {
563
+ errors.push(`schema bundle '${language}' attributesByElementType['${type}'].${key} must be an array`);
564
+ continue;
565
+ }
566
+ for (const name of list) {
567
+ if (typeof name !== 'string' || name === '') {
568
+ errors.push(`schema bundle '${language}' attributesByElementType['${type}'].${key} entries must be non-empty strings`);
569
+ }
570
+ }
571
+ }
572
+ if (contract.enumByAttr !== undefined) {
573
+ if (!contract.enumByAttr || typeof contract.enumByAttr !== 'object') {
574
+ errors.push(`schema bundle '${language}' attributesByElementType['${type}'].enumByAttr must be an object`);
575
+ } else {
576
+ for (const [attr, allowed] of Object.entries(contract.enumByAttr)) {
577
+ if (!Array.isArray(allowed) || allowed.length === 0) {
578
+ errors.push(`schema bundle '${language}' attributesByElementType['${type}'].enumByAttr['${attr}'] must be a non-empty array`);
579
+ }
580
+ }
581
+ }
582
+ }
583
+ }
584
+ return errors;
585
+ }
586
+
587
+ function validateBundle({ language, dialect, elementTypes, relationshipTypes, actorElementType, matrix, deliveryDependencies, attributesByElementType }) {
262
588
  const errors = [];
263
589
  const elementTypeList = Array.isArray(elementTypes) ? elementTypes : [];
264
590
  const relationshipTypeList = Array.isArray(relationshipTypes) ? relationshipTypes : [];
@@ -304,6 +630,7 @@ function validateBundle({ language, dialect, elementTypes, relationshipTypes, ac
304
630
  }
305
631
 
306
632
  errors.push(...validateDeliveryDependencies(language, relationshipTypeList, deliveryDependencies));
633
+ errors.push(...validateAttributeContracts(language, elementTypeList, attributesByElementType));
307
634
 
308
635
  return { status: errors.length === 0 ? 'passed' : 'failed', errors };
309
636
  }
@@ -386,6 +713,7 @@ function buildClassMatrixOntology(bundle) {
386
713
  const relationshipTypes = Array.from(relationshipCategoryByType.keys());
387
714
  const actorElementType = resolveActorElementType(bundle.config);
388
715
  const deliveryDependencies = resolveDeliveryDependencies(bundle.config, 'archimate-class-matrix');
716
+ const attributesByElementType = resolveAttributeContracts(bundle.config);
389
717
  return finalizeOntology({
390
718
  kind: bundle.kind,
391
719
  dialect: 'archimate-class-matrix',
@@ -397,7 +725,8 @@ function buildClassMatrixOntology(bundle) {
397
725
  relationshipTypes,
398
726
  actorElementType,
399
727
  deliveryDependencies,
400
- bundleValidation: validateBundle({ language, dialect: 'archimate-class-matrix', elementTypes, relationshipTypes, actorElementType, matrix: null, deliveryDependencies }),
728
+ attributesByElementType,
729
+ bundleValidation: validateBundle({ language, dialect: 'archimate-class-matrix', elementTypes, relationshipTypes, actorElementType, matrix: null, deliveryDependencies, attributesByElementType }),
401
730
  elementTypeMetadata,
402
731
  relationshipCategoryByType,
403
732
  isSupportedElementType,
@@ -456,6 +785,7 @@ function buildTypeMatrixOntology(bundle) {
456
785
  const elementTypes = Array.from(elementTypeMetadata.keys());
457
786
  const relationshipTypes = Array.from(relationshipCategoryByType.keys());
458
787
  const deliveryDependencies = resolveDeliveryDependencies(config, 'type-matrix');
788
+ const attributesByElementType = resolveAttributeContracts(config);
459
789
  const getArchiMateClass = (elementOrType) => {
460
790
  const type = typeof elementOrType === 'string' ? elementOrType : elementOrType && elementOrType.type;
461
791
  return type;
@@ -494,7 +824,8 @@ function buildTypeMatrixOntology(bundle) {
494
824
  relationshipTypes,
495
825
  actorElementType,
496
826
  deliveryDependencies,
497
- bundleValidation: validateBundle({ language, dialect: 'type-matrix', elementTypes, relationshipTypes, actorElementType, matrix, deliveryDependencies }),
827
+ attributesByElementType,
828
+ bundleValidation: validateBundle({ language, dialect: 'type-matrix', elementTypes, relationshipTypes, actorElementType, matrix, deliveryDependencies, attributesByElementType }),
498
829
  elementTypeMetadata,
499
830
  relationshipCategoryByType,
500
831
  isSupportedElementType: (type) => elementTypeMetadata.has(type),
@@ -552,9 +883,36 @@ function buildOntology(bundle) {
552
883
  return bundle.kind === 'default' ? buildClassMatrixOntology(bundle) : buildTypeMatrixOntology(bundle);
553
884
  }
554
885
 
886
+ // Cheap on-disk fingerprint of a bundle (and its extends chain) so a running MCP
887
+ // picks up edits to the bundle files without a restart: the ontology cache key
888
+ // changes when any key file's mtime/size changes.
889
+ function bundleFingerprint(bundle) {
890
+ const dirs = Array.isArray(bundle.chainDirs) && bundle.chainDirs.length > 0
891
+ ? bundle.chainDirs
892
+ : [path.resolve(bundle.dir).toLowerCase()];
893
+ const statFile = (absolutePath) => {
894
+ try {
895
+ const stat = fs.statSync(absolutePath);
896
+ return `${Math.round(stat.mtimeMs)}:${stat.size}`;
897
+ } catch {
898
+ return '-';
899
+ }
900
+ };
901
+ const parts = [];
902
+ for (const dir of dirs) {
903
+ for (const name of [SCHEMA_BASENAME, CONFIG_BASENAME, RULES_BASENAME]) {
904
+ parts.push(`${name}=${statFile(path.join(dir, name))}`);
905
+ }
906
+ }
907
+ if (bundle.config && typeof bundle.config.rules === 'string' && bundle.config.rules.trim() !== '') {
908
+ parts.push(`rules=${statFile(path.resolve(bundle.dir, bundle.config.rules))}`);
909
+ }
910
+ return parts.join('|');
911
+ }
912
+
555
913
  function loadSchemaBundleAndOntology(workspaceRoot, options = {}) {
556
914
  const bundle = resolveSchemaBundle(workspaceRoot, options);
557
- const cacheKey = `${bundle.kind}:${path.resolve(bundle.dir).toLowerCase()}`;
915
+ const cacheKey = `${bundle.kind}:${path.resolve(bundle.dir).toLowerCase()}:${bundleFingerprint(bundle)}`;
558
916
  if (ontologyCache.has(cacheKey)) {
559
917
  return { bundle, ontology: ontologyCache.get(cacheKey) };
560
918
  }