arkgate 2.13.0 → 3.0.0
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/CHANGELOG.md +39 -0
- package/README.md +37 -22
- package/bin/ark-check.mjs +62 -4
- package/bin/ark-mcp.mjs +108 -1
- package/bin/ark-shared.mjs +204 -149
- package/bin/ark.mjs +90 -25
- package/bin/lib/adapter-contract.mjs +93 -0
- package/bin/lib/agent-gates.mjs +1 -0
- package/bin/lib/analysis-engine.mjs +1171 -0
- package/bin/lib/architecture-scan.mjs +84 -135
- package/bin/lib/ci-and-commands.mjs +31 -0
- package/bin/lib/config-warnings.mjs +7 -205
- package/bin/lib/field-install.mjs +67 -10
- package/bin/lib/gate-files.mjs +42 -3
- package/bin/lib/graph-cycles.mjs +4 -54
- package/bin/lib/hook-templates.mjs +33 -1
- package/bin/lib/host-support-matrix.mjs +7 -1
- package/bin/lib/install-migrate.mjs +54 -16
- package/bin/lib/presets.mjs +42 -2
- package/bin/lib/safety-diagnostics.mjs +18 -17
- package/bin/lib/scan-files.mjs +12 -1
- package/bin/lib/skill-install.mjs +8 -1
- package/bin/lib/source-policy.mjs +36 -0
- package/bin/lib/start-preview.mjs +271 -0
- package/bin/lib/ts-resolve.mjs +11 -2
- package/bin/lib/write-path-capabilities.mjs +4 -0
- package/compat/nestjs.cjs +2 -0
- package/compat/nestjs.d.ts +2 -0
- package/compat/nestjs.js +1 -0
- package/compat/runtime.cjs +2 -0
- package/compat/runtime.d.ts +2 -0
- package/compat/runtime.js +1 -0
- package/dist/configContract-BxSIwVRo.d.cts +259 -0
- package/dist/configContract-BxSIwVRo.d.ts +259 -0
- package/dist/eslint/index.cjs +125 -48
- package/dist/eslint/index.d.cts +7 -1
- package/dist/eslint/index.d.ts +7 -1
- package/dist/eslint/index.js +125 -48
- package/dist/index.cjs +1248 -3302
- package/dist/index.d.cts +359 -483
- package/dist/index.d.ts +359 -483
- package/dist/index.js +1231 -3248
- package/docs/agent-guide.md +28 -16
- package/docs/ai-gates.md +30 -7
- package/docs/migrate-from-ark-runtime-kernel.md +2 -3
- package/docs/package-surface.md +8 -13
- package/docs/production-hardening.md +17 -4
- package/docs/typescript-support.md +27 -0
- package/package.json +33 -11
- package/schemas/ark.analysis-result.schema.json +91 -0
- package/server.json +2 -2
- package/templates/skills/ark-architect.md +3 -2
- package/dist/configContract-iBLxx5Tz.d.cts +0 -53
- package/dist/configContract-iBLxx5Tz.d.ts +0 -53
- package/dist/eslint/index.cjs.map +0 -1
- package/dist/eslint/index.js.map +0 -1
- package/dist/index.cjs.map +0 -1
- package/dist/index.js.map +0 -1
- package/dist/nestjs/index.cjs +0 -2606
- package/dist/nestjs/index.cjs.map +0 -1
- package/dist/nestjs/index.d.cts +0 -23
- package/dist/nestjs/index.d.ts +0 -23
- package/dist/nestjs/index.js +0 -2582
- package/dist/nestjs/index.js.map +0 -1
- package/dist/runtime/index.cjs +0 -4014
- package/dist/runtime/index.cjs.map +0 -1
- package/dist/runtime/index.d.cts +0 -3
- package/dist/runtime/index.d.ts +0 -3
- package/dist/runtime/index.js +0 -3925
- package/dist/runtime/index.js.map +0 -1
- package/dist/types-BxBwnBpC.d.cts +0 -1041
- package/dist/types-Wcs_l1_J.d.ts +0 -1041
package/dist/index.js
CHANGED
|
@@ -1,666 +1,90 @@
|
|
|
1
1
|
// src/version.ts
|
|
2
|
-
var version = "
|
|
2
|
+
var version = "3.0.0";
|
|
3
3
|
|
|
4
|
-
// src/
|
|
5
|
-
var
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
);
|
|
21
|
-
}
|
|
22
|
-
const fn = (payload) => ({
|
|
23
|
-
intent: name,
|
|
24
|
-
payload,
|
|
25
|
-
metadata: {
|
|
26
|
-
occurredAt: (/* @__PURE__ */ new Date()).toISOString(),
|
|
27
|
-
source: "unknown"
|
|
28
|
-
}
|
|
29
|
-
});
|
|
30
|
-
Object.defineProperty(fn, "name", {
|
|
31
|
-
value: name,
|
|
32
|
-
enumerable: true,
|
|
33
|
-
configurable: false,
|
|
34
|
-
writable: false
|
|
35
|
-
});
|
|
36
|
-
const creator = fn;
|
|
37
|
-
this.intents.set(name, creator);
|
|
38
|
-
if (options?.dependsOn) {
|
|
39
|
-
for (const dep of options.dependsOn) {
|
|
40
|
-
this.declareDependency(name, dep);
|
|
41
|
-
}
|
|
42
|
-
}
|
|
43
|
-
if (options?.produces) {
|
|
44
|
-
for (const prod of options.produces) {
|
|
45
|
-
this.declareProduction(name, prod);
|
|
46
|
-
}
|
|
47
|
-
}
|
|
48
|
-
return creator;
|
|
49
|
-
}
|
|
50
|
-
/**
|
|
51
|
-
* Declare that one intent depends on / relates to another.
|
|
52
|
-
* This information is used by the DependencyGraph (future iterations) and for policy checks.
|
|
53
|
-
*
|
|
54
|
-
* @param from - The source intent (e.g. an Application operation)
|
|
55
|
-
* @param to - The target intent it depends on (e.g. a Domain event or concept)
|
|
56
|
-
*/
|
|
57
|
-
declareDependency(from, to) {
|
|
58
|
-
if (!this.dependencies.has(from)) {
|
|
59
|
-
this.dependencies.set(from, /* @__PURE__ */ new Set());
|
|
60
|
-
}
|
|
61
|
-
this.dependencies.get(from).add(to);
|
|
62
|
-
}
|
|
63
|
-
/**
|
|
64
|
-
* Declare that one intent produces / emits another (e.g. use case → domain event).
|
|
65
|
-
*/
|
|
66
|
-
declareProduction(from, to) {
|
|
67
|
-
if (!this.productions.has(from)) {
|
|
68
|
-
this.productions.set(from, /* @__PURE__ */ new Set());
|
|
69
|
-
}
|
|
70
|
-
this.productions.get(from).add(to);
|
|
71
|
-
}
|
|
72
|
-
/**
|
|
73
|
-
* Retrieve a previously defined intent creator by name.
|
|
74
|
-
*/
|
|
75
|
-
get(name) {
|
|
76
|
-
return this.intents.get(name);
|
|
77
|
-
}
|
|
78
|
-
/**
|
|
79
|
-
* List all registered intent creators.
|
|
80
|
-
*/
|
|
81
|
-
list() {
|
|
82
|
-
return Array.from(this.intents.values());
|
|
83
|
-
}
|
|
84
|
-
/**
|
|
85
|
-
* Get all declared dependencies for a given intent.
|
|
86
|
-
*/
|
|
87
|
-
getDependencies(intentName) {
|
|
88
|
-
const deps = this.dependencies.get(intentName);
|
|
89
|
-
return deps ? Array.from(deps) : [];
|
|
90
|
-
}
|
|
91
|
-
/**
|
|
92
|
-
* Get all intents produced / emitted by a given intent.
|
|
93
|
-
*/
|
|
94
|
-
getProductions(intentName) {
|
|
95
|
-
const prods = this.productions.get(intentName);
|
|
96
|
-
return prods ? Array.from(prods) : [];
|
|
97
|
-
}
|
|
98
|
-
/**
|
|
99
|
-
* Get all declared relationships (useful for graph generation).
|
|
100
|
-
*/
|
|
101
|
-
getAllRelationships() {
|
|
102
|
-
const result = [];
|
|
103
|
-
for (const [from, tos] of this.dependencies.entries()) {
|
|
104
|
-
for (const to of tos) {
|
|
105
|
-
result.push({ from, to, kind: "dependsOn" });
|
|
106
|
-
}
|
|
107
|
-
}
|
|
108
|
-
for (const [from, tos] of this.productions.entries()) {
|
|
109
|
-
for (const to of tos) {
|
|
110
|
-
result.push({ from, to, kind: "produces" });
|
|
111
|
-
}
|
|
112
|
-
}
|
|
113
|
-
return result;
|
|
114
|
-
}
|
|
115
|
-
/**
|
|
116
|
-
* Check if an intent name has been registered.
|
|
117
|
-
*/
|
|
118
|
-
has(name) {
|
|
119
|
-
return this.intents.has(name);
|
|
120
|
-
}
|
|
121
|
-
/**
|
|
122
|
-
* Clear the registry. Primarily useful for tests.
|
|
123
|
-
*/
|
|
124
|
-
clear() {
|
|
125
|
-
this.intents.clear();
|
|
126
|
-
this.dependencies.clear();
|
|
127
|
-
this.productions.clear();
|
|
128
|
-
}
|
|
129
|
-
};
|
|
130
|
-
|
|
131
|
-
// src/kernel/intent/defineIntent.ts
|
|
132
|
-
var defaultRegistry = new IntentRegistry();
|
|
133
|
-
function defineIntent(name, options) {
|
|
134
|
-
return defaultRegistry.define(name, options);
|
|
135
|
-
}
|
|
136
|
-
function createIntentRegistry() {
|
|
137
|
-
return new IntentRegistry();
|
|
138
|
-
}
|
|
139
|
-
var defaultIntentRegistry = defaultRegistry;
|
|
140
|
-
|
|
141
|
-
// src/kernel/intent/validateIntentName.ts
|
|
142
|
-
var ALLOWED_PREFIXES = [
|
|
143
|
-
"Domain.",
|
|
144
|
-
"Application.",
|
|
145
|
-
"Adapter.",
|
|
146
|
-
"Workflow.",
|
|
147
|
-
"Job.",
|
|
148
|
-
"Presentation.",
|
|
149
|
-
"Reporting.",
|
|
150
|
-
"Metadata.",
|
|
151
|
-
"Security.",
|
|
152
|
-
"Audit.",
|
|
153
|
-
"Observability.",
|
|
154
|
-
"Kernel."
|
|
155
|
-
];
|
|
156
|
-
function validateIntentName(name) {
|
|
157
|
-
if (!name || typeof name !== "string") {
|
|
158
|
-
return { valid: false, reason: "Intent name must be a non-empty string" };
|
|
159
|
-
}
|
|
160
|
-
if (!ALLOWED_PREFIXES.some((p) => name.startsWith(p))) {
|
|
161
|
-
return {
|
|
162
|
-
valid: false,
|
|
163
|
-
reason: `Intent "${name}" must start with one of: ${ALLOWED_PREFIXES.join(", ")}`
|
|
164
|
-
};
|
|
165
|
-
}
|
|
166
|
-
const rest = name.slice(name.indexOf(".") + 1);
|
|
167
|
-
if (!rest || !/^[A-Za-z][A-Za-z0-9_.]*$/.test(rest)) {
|
|
168
|
-
return {
|
|
169
|
-
valid: false,
|
|
170
|
-
reason: `Intent "${name}" has an invalid segment after the layer prefix`
|
|
171
|
-
};
|
|
172
|
-
}
|
|
173
|
-
return { valid: true };
|
|
174
|
-
}
|
|
175
|
-
|
|
176
|
-
// src/kernel/policy/PolicyViolationError.ts
|
|
177
|
-
var PolicyViolationError = class extends Error {
|
|
178
|
-
violations;
|
|
179
|
-
constructor(violations) {
|
|
180
|
-
const messages = violations.map((v) => `- ${v.policyName}: ${v.message}`).join("\n");
|
|
181
|
-
super(`Hard policy violation(s) detected:
|
|
182
|
-
${messages}`);
|
|
183
|
-
this.name = "PolicyViolationError";
|
|
184
|
-
this.violations = violations;
|
|
185
|
-
}
|
|
186
|
-
};
|
|
187
|
-
|
|
188
|
-
// src/kernel/policy/PolicyEngine.ts
|
|
189
|
-
var PolicyEngine = class {
|
|
190
|
-
policies = [];
|
|
191
|
-
constructor(initialPolicies = []) {
|
|
192
|
-
for (const policy of initialPolicies) {
|
|
193
|
-
this.add(policy);
|
|
194
|
-
}
|
|
195
|
-
}
|
|
196
|
-
/**
|
|
197
|
-
* Adds a policy to the engine.
|
|
198
|
-
*/
|
|
199
|
-
add(policy) {
|
|
200
|
-
if (this.policies.some((p) => p.name === policy.name)) {
|
|
201
|
-
throw new Error(`Policy "${policy.name}" is already registered in this engine.`);
|
|
202
|
-
}
|
|
203
|
-
this.policies.push(policy);
|
|
204
|
-
}
|
|
205
|
-
/**
|
|
206
|
-
* Returns all registered policies.
|
|
207
|
-
*/
|
|
208
|
-
getPolicies() {
|
|
209
|
-
return [...this.policies];
|
|
210
|
-
}
|
|
211
|
-
/**
|
|
212
|
-
* Evaluates all policies against the provided context.
|
|
213
|
-
*/
|
|
214
|
-
evaluate(context) {
|
|
215
|
-
const violations = [];
|
|
216
|
-
for (const policy of this.policies) {
|
|
217
|
-
const result = policy.check(context);
|
|
218
|
-
if (result === true) {
|
|
219
|
-
continue;
|
|
220
|
-
}
|
|
221
|
-
if (result === false) {
|
|
222
|
-
violations.push({
|
|
223
|
-
policyName: policy.name,
|
|
224
|
-
severity: policy.severity,
|
|
225
|
-
message: `Policy "${policy.name}" was violated.`
|
|
226
|
-
});
|
|
227
|
-
continue;
|
|
228
|
-
}
|
|
229
|
-
if (Array.isArray(result)) {
|
|
230
|
-
for (const v of result) {
|
|
231
|
-
violations.push({
|
|
232
|
-
policyName: policy.name,
|
|
233
|
-
severity: policy.severity,
|
|
234
|
-
message: v.message,
|
|
235
|
-
details: v.details
|
|
236
|
-
});
|
|
237
|
-
}
|
|
238
|
-
} else {
|
|
239
|
-
violations.push({
|
|
240
|
-
policyName: policy.name,
|
|
241
|
-
severity: policy.severity,
|
|
242
|
-
message: result.message,
|
|
243
|
-
details: result.details
|
|
244
|
-
});
|
|
245
|
-
}
|
|
246
|
-
}
|
|
247
|
-
const hardViolations = violations.filter((v) => v.severity === "hard");
|
|
248
|
-
const softViolations = violations.filter((v) => v.severity === "soft");
|
|
249
|
-
return {
|
|
250
|
-
passed: violations.length === 0,
|
|
251
|
-
violations,
|
|
252
|
-
hardViolations,
|
|
253
|
-
softViolations
|
|
254
|
-
};
|
|
255
|
-
}
|
|
256
|
-
/**
|
|
257
|
-
* Enforces all policies.
|
|
258
|
-
*
|
|
259
|
-
* - Soft violations are collected and can be observed (returned or logged).
|
|
260
|
-
* - Hard violations cause an error to be thrown (by default).
|
|
261
|
-
*
|
|
262
|
-
* @returns Evaluation result (including any soft violations)
|
|
263
|
-
* @throws Error if any hard policy is violated
|
|
264
|
-
*/
|
|
265
|
-
enforce(context) {
|
|
266
|
-
const result = this.evaluate(context);
|
|
267
|
-
if (result.hardViolations.length > 0) {
|
|
268
|
-
throw new PolicyViolationError(result.hardViolations);
|
|
269
|
-
}
|
|
270
|
-
return result;
|
|
271
|
-
}
|
|
272
|
-
/**
|
|
273
|
-
* Clears all registered policies.
|
|
274
|
-
*/
|
|
275
|
-
clear() {
|
|
276
|
-
this.policies.length = 0;
|
|
277
|
-
}
|
|
278
|
-
};
|
|
279
|
-
|
|
280
|
-
// src/kernel/policy/definePolicy.ts
|
|
281
|
-
function definePolicy(options) {
|
|
282
|
-
const severity = options.severity ?? "soft";
|
|
283
|
-
const policy = {
|
|
284
|
-
name: options.name,
|
|
285
|
-
severity,
|
|
286
|
-
tags: options.tags,
|
|
287
|
-
owner: options.owner,
|
|
288
|
-
version: options.version,
|
|
289
|
-
rationale: options.rationale,
|
|
290
|
-
enforcementMode: options.enforcementMode,
|
|
291
|
-
deprecated: options.deprecated,
|
|
292
|
-
replacedBy: options.replacedBy,
|
|
293
|
-
check: options.check
|
|
4
|
+
// src/domain/adapterContract.ts
|
|
5
|
+
var ARK_ANALYSIS_RESULT_SCHEMA_VERSION = "1.0";
|
|
6
|
+
function text(value) {
|
|
7
|
+
return typeof value === "string" && value.length > 0 ? value : void 0;
|
|
8
|
+
}
|
|
9
|
+
function positiveInteger(value, fallback) {
|
|
10
|
+
return Number.isInteger(value) && Number(value) > 0 ? Number(value) : fallback;
|
|
11
|
+
}
|
|
12
|
+
function toAdapterDiagnostic(violation2, fallbackSeverity = "error") {
|
|
13
|
+
const ruleId = text(violation2.ruleId) ?? text(violation2.code) ?? "ARK_UNKNOWN";
|
|
14
|
+
const severity = violation2.severity === "warning" ? "warning" : fallbackSeverity;
|
|
15
|
+
const evidence = {
|
|
16
|
+
...text(violation2.target) ? { target: text(violation2.target) } : {},
|
|
17
|
+
...text(violation2.fromLayer) ? { fromLayer: text(violation2.fromLayer) } : {},
|
|
18
|
+
...text(violation2.toLayer) ? { toLayer: text(violation2.toLayer) } : {},
|
|
19
|
+
...typeof violation2.typeOnly === "boolean" ? { typeOnly: violation2.typeOnly } : {}
|
|
294
20
|
};
|
|
295
|
-
return
|
|
296
|
-
|
|
297
|
-
|
|
298
|
-
// src/kernel/policy/builtins.ts
|
|
299
|
-
function defaultLayerOf(name) {
|
|
300
|
-
const dot = name.indexOf(".");
|
|
301
|
-
return dot >= 0 ? name.slice(0, dot) : name;
|
|
302
|
-
}
|
|
303
|
-
function resolveLayer(name, options) {
|
|
304
|
-
return options.resolveLayer?.(name) ?? defaultLayerOf(name);
|
|
305
|
-
}
|
|
306
|
-
function matchesRule(fromLayer, toLayer, rule) {
|
|
307
|
-
return fromLayer === rule.from && toLayer === rule.to;
|
|
308
|
-
}
|
|
309
|
-
function defineLayerPolicy(options) {
|
|
310
|
-
const name = options.name ?? "Layer isolation";
|
|
311
|
-
const severity = options.severity ?? "hard";
|
|
312
|
-
return definePolicy({
|
|
313
|
-
name,
|
|
21
|
+
return {
|
|
22
|
+
ruleId,
|
|
314
23
|
severity,
|
|
315
|
-
|
|
316
|
-
|
|
317
|
-
|
|
318
|
-
|
|
319
|
-
|
|
320
|
-
|
|
321
|
-
|
|
322
|
-
|
|
323
|
-
const fromLayer = resolveLayer(edge.from, options);
|
|
324
|
-
const toLayer = resolveLayer(edge.to, options);
|
|
325
|
-
for (const rule of options.rules) {
|
|
326
|
-
if (!rule.allowed && matchesRule(fromLayer, toLayer, rule)) {
|
|
327
|
-
violations.push({
|
|
328
|
-
policyName: name,
|
|
329
|
-
severity,
|
|
330
|
-
message: rule.message ?? `Layer violation: ${edge.from} (${fromLayer}) must not relate to ${edge.to} (${toLayer})`
|
|
331
|
-
});
|
|
332
|
-
}
|
|
333
|
-
}
|
|
334
|
-
}
|
|
335
|
-
return violations.length > 0 ? violations : true;
|
|
336
|
-
}
|
|
337
|
-
});
|
|
338
|
-
}
|
|
339
|
-
function isLayerPolicy(policy) {
|
|
340
|
-
return policy.tags?.includes("layer") ?? false;
|
|
341
|
-
}
|
|
342
|
-
var architecturalPolicies = {
|
|
343
|
-
/**
|
|
344
|
-
* @deprecated Use cleanArchitectureMatrix() for full layer rules.
|
|
345
|
-
* Blocks Domain → Adapter declared dependencies only.
|
|
346
|
-
*/
|
|
347
|
-
layerIsolation() {
|
|
348
|
-
return architecturalPolicies.cleanArchitectureMatrix();
|
|
349
|
-
},
|
|
350
|
-
/**
|
|
351
|
-
* Clean-architecture dependency matrix (declared dependsOn / declared edges only).
|
|
352
|
-
* Does not block observed event flows (Domain events consumed by Application).
|
|
353
|
-
*/
|
|
354
|
-
cleanArchitectureMatrix() {
|
|
355
|
-
return defineLayerPolicy({
|
|
356
|
-
name: "Clean architecture matrix",
|
|
357
|
-
severity: "hard",
|
|
358
|
-
rules: [
|
|
359
|
-
{ from: "Domain", to: "Adapter", allowed: false },
|
|
360
|
-
{ from: "Domain", to: "Application", allowed: false },
|
|
361
|
-
{ from: "Adapter", to: "Application", allowed: false },
|
|
362
|
-
{ from: "Adapter", to: "Domain", allowed: false }
|
|
363
|
-
]
|
|
364
|
-
});
|
|
365
|
-
}
|
|
366
|
-
};
|
|
367
|
-
function defineArchitectureProfilePolicy(profile, options = {}) {
|
|
368
|
-
return defineLayerPolicy({
|
|
369
|
-
name: options.name ?? `${profile.name} layer policy`,
|
|
370
|
-
severity: options.severity ?? "hard",
|
|
371
|
-
rules: profile.rules,
|
|
372
|
-
resolveLayer: profile.resolveLayer
|
|
373
|
-
});
|
|
374
|
-
}
|
|
375
|
-
|
|
376
|
-
// src/kernel/event-bus/policyContext.ts
|
|
377
|
-
function buildPublishPolicyContext(options) {
|
|
378
|
-
return (event) => ({
|
|
379
|
-
event,
|
|
380
|
-
relationships: options.intentRegistry?.getAllRelationships(),
|
|
381
|
-
edges: options.dependencyGraph?.getEdges()
|
|
382
|
-
});
|
|
383
|
-
}
|
|
384
|
-
function definePublishPolicy(options) {
|
|
385
|
-
return definePolicy(options);
|
|
386
|
-
}
|
|
387
|
-
|
|
388
|
-
// src/kernel/event-bus/errors.ts
|
|
389
|
-
var UnregisteredIntentError = class extends Error {
|
|
390
|
-
intentName;
|
|
391
|
-
constructor(intentName) {
|
|
392
|
-
super(
|
|
393
|
-
`Intent "${intentName}" is not registered. Register it with IntentRegistry.define() before publish/subscribe, including metadata.source producer intents in strict mode.`
|
|
394
|
-
);
|
|
395
|
-
this.name = "UnregisteredIntentError";
|
|
396
|
-
this.intentName = intentName;
|
|
397
|
-
}
|
|
398
|
-
};
|
|
399
|
-
var InvalidIntentNameError = class extends Error {
|
|
400
|
-
intentName;
|
|
401
|
-
reason;
|
|
402
|
-
constructor(intentName, reason) {
|
|
403
|
-
super(`Invalid intent name "${intentName}": ${reason}`);
|
|
404
|
-
this.name = "InvalidIntentNameError";
|
|
405
|
-
this.intentName = intentName;
|
|
406
|
-
this.reason = reason;
|
|
407
|
-
}
|
|
408
|
-
};
|
|
409
|
-
var LayerPolicyContextError = class extends Error {
|
|
410
|
-
constructor() {
|
|
411
|
-
super(
|
|
412
|
-
"Layer/architecture policies require intentRegistry, dependencyGraph, or a custom getPolicyContext. Without graph/registry context, layer policies cannot inspect relationships."
|
|
413
|
-
);
|
|
414
|
-
this.name = "LayerPolicyContextError";
|
|
415
|
-
}
|
|
416
|
-
};
|
|
417
|
-
var EventContractViolationError = class extends Error {
|
|
418
|
-
intentName;
|
|
419
|
-
issues;
|
|
420
|
-
constructor(intentName, issues) {
|
|
421
|
-
super(
|
|
422
|
-
`Event contract violation for "${intentName}". Register a matching event contract/version or fix the payload before publishing.`
|
|
423
|
-
);
|
|
424
|
-
this.name = "EventContractViolationError";
|
|
425
|
-
this.intentName = intentName;
|
|
426
|
-
this.issues = issues;
|
|
427
|
-
}
|
|
428
|
-
};
|
|
429
|
-
var UnknownEventSourceError = class extends Error {
|
|
430
|
-
intentName;
|
|
431
|
-
source;
|
|
432
|
-
constructor(intentName, source) {
|
|
433
|
-
super(
|
|
434
|
-
source ? `Event "${intentName}" metadata.source "${source}" is not registered. Register the producer intent or publish from a known source.` : `Event "${intentName}" must include metadata.source. Strict Ark uses source to enforce observed layer flow.`
|
|
435
|
-
);
|
|
436
|
-
this.name = "UnknownEventSourceError";
|
|
437
|
-
this.intentName = intentName;
|
|
438
|
-
this.source = source;
|
|
439
|
-
}
|
|
440
|
-
};
|
|
441
|
-
var SourceMetadataOverrideError = class extends Error {
|
|
442
|
-
boundSource;
|
|
443
|
-
attemptedSource;
|
|
444
|
-
constructor(boundSource, attemptedSource) {
|
|
445
|
-
super(
|
|
446
|
-
`Source-bound publisher for "${boundSource}" cannot publish with metadata.source "${attemptedSource}". Create a publisher for the intended source instead.`
|
|
447
|
-
);
|
|
448
|
-
this.name = "SourceMetadataOverrideError";
|
|
449
|
-
this.boundSource = boundSource;
|
|
450
|
-
this.attemptedSource = attemptedSource;
|
|
451
|
-
}
|
|
452
|
-
};
|
|
453
|
-
var ObservedLayerFlowViolationError = class extends Error {
|
|
454
|
-
source;
|
|
455
|
-
intentName;
|
|
456
|
-
fromLayer;
|
|
457
|
-
toLayer;
|
|
458
|
-
constructor(source, intentName, fromLayer, toLayer, message) {
|
|
459
|
-
super(
|
|
460
|
-
message ?? `Observed layer violation: "${source}" (${fromLayer}) must not produce "${intentName}" (${toLayer}). Route this through an allowed layer or adjust the architecture profile rule.`
|
|
461
|
-
);
|
|
462
|
-
this.name = "ObservedLayerFlowViolationError";
|
|
463
|
-
this.source = source;
|
|
464
|
-
this.intentName = intentName;
|
|
465
|
-
this.fromLayer = fromLayer;
|
|
466
|
-
this.toLayer = toLayer;
|
|
467
|
-
}
|
|
468
|
-
};
|
|
469
|
-
|
|
470
|
-
// src/kernel/event-bus/publishGuards.ts
|
|
471
|
-
function assertIntentAllowed(intentName, options) {
|
|
472
|
-
if (!options.strictRegistry && !options.validateIntentNaming) {
|
|
473
|
-
return;
|
|
474
|
-
}
|
|
475
|
-
if (options.validateIntentNaming) {
|
|
476
|
-
const validation = validateIntentName(intentName);
|
|
477
|
-
if (!validation.valid) {
|
|
478
|
-
throw new InvalidIntentNameError(intentName, validation.reason);
|
|
479
|
-
}
|
|
480
|
-
}
|
|
481
|
-
if (options.strictRegistry && options.intentRegistry && !options.intentRegistry.has(intentName)) {
|
|
482
|
-
throw new UnregisteredIntentError(intentName);
|
|
483
|
-
}
|
|
484
|
-
}
|
|
485
|
-
function assertSourceAllowed(event, options) {
|
|
486
|
-
if (!options.requireKnownSource) return;
|
|
487
|
-
if (!event.metadata.source || event.metadata.source === "unknown") {
|
|
488
|
-
throw new UnknownEventSourceError(event.intent);
|
|
489
|
-
}
|
|
490
|
-
if (options.intentRegistry && !options.intentRegistry.has(event.metadata.source)) {
|
|
491
|
-
throw new UnknownEventSourceError(event.intent, event.metadata.source);
|
|
492
|
-
}
|
|
493
|
-
}
|
|
494
|
-
function assertContractAllowed(event, options) {
|
|
495
|
-
if (!options.eventContracts) return;
|
|
496
|
-
const result = options.eventContracts.validate(event);
|
|
497
|
-
if (!result.ok && (options.strictEventContracts || result.contract)) {
|
|
498
|
-
throw new EventContractViolationError(event.intent, result.issues);
|
|
499
|
-
}
|
|
500
|
-
}
|
|
501
|
-
|
|
502
|
-
// src/kernel/event-bus/payloadPatch.ts
|
|
503
|
-
function isPlainRecord(value) {
|
|
504
|
-
if (typeof value !== "object" || value === null || Array.isArray(value)) {
|
|
505
|
-
return false;
|
|
506
|
-
}
|
|
507
|
-
const proto = Object.getPrototypeOf(value);
|
|
508
|
-
return proto === Object.prototype || proto === null;
|
|
509
|
-
}
|
|
510
|
-
function clonePatchValue(value) {
|
|
511
|
-
if (Array.isArray(value)) return value.map(clonePatchValue);
|
|
512
|
-
if (isPlainRecord(value)) {
|
|
513
|
-
return Object.fromEntries(
|
|
514
|
-
Object.entries(value).map(([key, child]) => [key, clonePatchValue(child)])
|
|
515
|
-
);
|
|
516
|
-
}
|
|
517
|
-
return value;
|
|
518
|
-
}
|
|
519
|
-
function mergeRecordPatch(target, patch, path = "payload") {
|
|
520
|
-
const next = { ...target };
|
|
521
|
-
for (const [key, value] of Object.entries(patch)) {
|
|
522
|
-
const childPath = `${path}.${key}`;
|
|
523
|
-
if (!(key in next) || next[key] === void 0) {
|
|
524
|
-
next[key] = clonePatchValue(value);
|
|
525
|
-
continue;
|
|
526
|
-
}
|
|
527
|
-
if (isPlainRecord(next[key]) && isPlainRecord(value)) {
|
|
528
|
-
next[key] = mergeRecordPatch(next[key], value, childPath);
|
|
529
|
-
continue;
|
|
530
|
-
}
|
|
531
|
-
if (Array.isArray(next[key]) && Array.isArray(value)) {
|
|
532
|
-
next[key] = mergeArrayPatch(next[key], value, childPath);
|
|
533
|
-
continue;
|
|
534
|
-
}
|
|
535
|
-
throw new Error(`Interceptor patch cannot overwrite existing ${childPath}.`);
|
|
536
|
-
}
|
|
537
|
-
return next;
|
|
538
|
-
}
|
|
539
|
-
function mergeArrayPatch(target, patch, path = "payload") {
|
|
540
|
-
const next = [...target];
|
|
541
|
-
patch.forEach((value, index) => {
|
|
542
|
-
const childPath = `${path}[${index}]`;
|
|
543
|
-
if (index >= next.length || next[index] === void 0) {
|
|
544
|
-
next[index] = clonePatchValue(value);
|
|
545
|
-
return;
|
|
546
|
-
}
|
|
547
|
-
if (isPlainRecord(next[index]) && isPlainRecord(value)) {
|
|
548
|
-
next[index] = mergeRecordPatch(next[index], value, childPath);
|
|
549
|
-
return;
|
|
550
|
-
}
|
|
551
|
-
if (Array.isArray(next[index]) && Array.isArray(value)) {
|
|
552
|
-
next[index] = mergeArrayPatch(next[index], value, childPath);
|
|
553
|
-
return;
|
|
554
|
-
}
|
|
555
|
-
throw new Error(`Interceptor patch cannot overwrite existing ${childPath}.`);
|
|
556
|
-
});
|
|
557
|
-
return next;
|
|
24
|
+
message: text(violation2.message) ?? ruleId,
|
|
25
|
+
location: {
|
|
26
|
+
file: text(violation2.file) ?? "<unknown>",
|
|
27
|
+
line: positiveInteger(violation2.line, 1),
|
|
28
|
+
column: positiveInteger(violation2.column, 1)
|
|
29
|
+
},
|
|
30
|
+
evidence
|
|
31
|
+
};
|
|
558
32
|
}
|
|
559
|
-
function
|
|
560
|
-
|
|
561
|
-
|
|
562
|
-
|
|
563
|
-
|
|
564
|
-
|
|
565
|
-
|
|
566
|
-
|
|
567
|
-
|
|
568
|
-
if (!isPlainRecord(payload)) {
|
|
569
|
-
throw new Error("Object interceptor patch requires an object payload.");
|
|
570
|
-
}
|
|
571
|
-
return mergeRecordPatch(payload, patch);
|
|
33
|
+
function createAdapterResult(input) {
|
|
34
|
+
return {
|
|
35
|
+
schemaVersion: ARK_ANALYSIS_RESULT_SCHEMA_VERSION,
|
|
36
|
+
valid: input.valid,
|
|
37
|
+
diagnostics: [
|
|
38
|
+
...(input.violations ?? []).map((item) => toAdapterDiagnostic(item, "error")),
|
|
39
|
+
...(input.warnings ?? []).map((item) => toAdapterDiagnostic(item, "warning"))
|
|
40
|
+
]
|
|
41
|
+
};
|
|
572
42
|
}
|
|
573
|
-
|
|
574
|
-
|
|
575
|
-
|
|
576
|
-
|
|
577
|
-
|
|
578
|
-
|
|
579
|
-
|
|
580
|
-
|
|
581
|
-
|
|
582
|
-
|
|
583
|
-
|
|
584
|
-
|
|
585
|
-
|
|
586
|
-
|
|
587
|
-
|
|
588
|
-
|
|
43
|
+
var ARK_ANALYSIS_RESULT_SCHEMA = {
|
|
44
|
+
$schema: "https://json-schema.org/draft/2020-12/schema",
|
|
45
|
+
$id: "https://unpkg.com/arkgate@2/schemas/ark.analysis-result.schema.json",
|
|
46
|
+
title: "ArkGate analysis result",
|
|
47
|
+
type: "object",
|
|
48
|
+
additionalProperties: false,
|
|
49
|
+
required: ["schemaVersion", "valid", "diagnostics"],
|
|
50
|
+
properties: {
|
|
51
|
+
schemaVersion: { const: ARK_ANALYSIS_RESULT_SCHEMA_VERSION },
|
|
52
|
+
valid: { type: "boolean" },
|
|
53
|
+
diagnostics: {
|
|
54
|
+
type: "array",
|
|
55
|
+
items: {
|
|
56
|
+
type: "object",
|
|
57
|
+
additionalProperties: false,
|
|
58
|
+
required: ["ruleId", "severity", "message", "location", "evidence"],
|
|
59
|
+
properties: {
|
|
60
|
+
ruleId: { type: "string", minLength: 1 },
|
|
61
|
+
severity: { enum: ["error", "warning"] },
|
|
62
|
+
message: { type: "string", minLength: 1 },
|
|
63
|
+
location: {
|
|
64
|
+
type: "object",
|
|
65
|
+
additionalProperties: false,
|
|
66
|
+
required: ["file", "line", "column"],
|
|
67
|
+
properties: {
|
|
68
|
+
file: { type: "string", minLength: 1 },
|
|
69
|
+
line: { type: "integer", minimum: 1 },
|
|
70
|
+
column: { type: "integer", minimum: 1 }
|
|
71
|
+
}
|
|
72
|
+
},
|
|
73
|
+
evidence: {
|
|
74
|
+
type: "object",
|
|
75
|
+
additionalProperties: false,
|
|
76
|
+
properties: {
|
|
77
|
+
target: { type: "string" },
|
|
78
|
+
fromLayer: { type: "string" },
|
|
79
|
+
toLayer: { type: "string" },
|
|
80
|
+
typeOnly: { type: "boolean" }
|
|
81
|
+
}
|
|
589
82
|
}
|
|
590
|
-
})
|
|
591
|
-
);
|
|
592
|
-
if (patches.length === 0) {
|
|
593
|
-
continue;
|
|
594
|
-
}
|
|
595
|
-
const timestamp = (/* @__PURE__ */ new Date()).toISOString();
|
|
596
|
-
let candidate = {
|
|
597
|
-
...current,
|
|
598
|
-
metadata: {
|
|
599
|
-
...current.metadata,
|
|
600
|
-
interceptions: [
|
|
601
|
-
...current.metadata.interceptions ?? [],
|
|
602
|
-
{ interceptorId: registration.interceptorId, timestamp }
|
|
603
|
-
]
|
|
604
83
|
}
|
|
605
|
-
};
|
|
606
|
-
for (const patch of patches) {
|
|
607
|
-
candidate = {
|
|
608
|
-
...candidate,
|
|
609
|
-
payload: applyPayloadPatch(candidate.payload, patch),
|
|
610
|
-
metadata: { ...candidate.metadata }
|
|
611
|
-
};
|
|
612
84
|
}
|
|
613
|
-
assertContractAllowed(candidate, {
|
|
614
|
-
eventContracts: deps.eventContracts,
|
|
615
|
-
strictEventContracts: deps.strictEventContracts
|
|
616
|
-
});
|
|
617
|
-
current = candidate;
|
|
618
|
-
registration.lastInterceptedAt = timestamp;
|
|
619
|
-
deps.appendTrace({
|
|
620
|
-
type: "event.intercepted",
|
|
621
|
-
timestamp,
|
|
622
|
-
intent: current.intent,
|
|
623
|
-
correlationId: current.metadata.correlationId,
|
|
624
|
-
traceId: current.metadata.traceId,
|
|
625
|
-
spanId: current.metadata.spanId,
|
|
626
|
-
details: {
|
|
627
|
-
registrationId: registration.registrationId,
|
|
628
|
-
interceptorId: registration.interceptorId,
|
|
629
|
-
patchesApplied: patches.length
|
|
630
|
-
}
|
|
631
|
-
});
|
|
632
|
-
await deps.recordAudit("event.intercepted", current, {
|
|
633
|
-
registrationId: registration.registrationId,
|
|
634
|
-
interceptorId: registration.interceptorId,
|
|
635
|
-
patchesApplied: patches.length
|
|
636
|
-
});
|
|
637
|
-
} catch (err) {
|
|
638
|
-
await recordInterceptorError(registration, current, err, deps);
|
|
639
85
|
}
|
|
640
86
|
}
|
|
641
|
-
|
|
642
|
-
}
|
|
643
|
-
async function recordInterceptorError(interceptor, event, error, deps) {
|
|
644
|
-
const message = error instanceof Error ? error.message : String(error);
|
|
645
|
-
deps.appendTrace({
|
|
646
|
-
type: "interceptor.error",
|
|
647
|
-
timestamp: (/* @__PURE__ */ new Date()).toISOString(),
|
|
648
|
-
intent: event.intent,
|
|
649
|
-
correlationId: event.metadata.correlationId,
|
|
650
|
-
traceId: event.metadata.traceId,
|
|
651
|
-
spanId: event.metadata.spanId,
|
|
652
|
-
details: {
|
|
653
|
-
registrationId: interceptor.registrationId,
|
|
654
|
-
interceptorId: interceptor.interceptorId,
|
|
655
|
-
error: message
|
|
656
|
-
}
|
|
657
|
-
});
|
|
658
|
-
await deps.recordAudit("interceptor.error", event, {
|
|
659
|
-
registrationId: interceptor.registrationId,
|
|
660
|
-
interceptorId: interceptor.interceptorId,
|
|
661
|
-
error: message
|
|
662
|
-
});
|
|
663
|
-
}
|
|
87
|
+
};
|
|
664
88
|
|
|
665
89
|
// src/domain/layerMatch.ts
|
|
666
90
|
var regexpCache = /* @__PURE__ */ new Map();
|
|
@@ -827,1897 +251,353 @@ function findDeniedEdgeRule(rules, from, to, options) {
|
|
|
827
251
|
return void 0;
|
|
828
252
|
}
|
|
829
253
|
|
|
830
|
-
// src/
|
|
831
|
-
|
|
832
|
-
|
|
833
|
-
|
|
834
|
-
|
|
835
|
-
|
|
836
|
-
|
|
837
|
-
|
|
838
|
-
|
|
839
|
-
const toLayer = profile.resolveLayer(event.intent);
|
|
840
|
-
if (!fromLayer || !toLayer) return;
|
|
841
|
-
const blocked = findDeniedEdgeRule(profile.rules, fromLayer, toLayer);
|
|
842
|
-
if (!blocked) return;
|
|
843
|
-
const severity = deps.mode;
|
|
844
|
-
const message = blocked.message ?? `Observed layer violation: "${source}" (${fromLayer}) must not produce "${event.intent}" (${toLayer}).`;
|
|
845
|
-
const details = {
|
|
846
|
-
source,
|
|
847
|
-
intent: event.intent,
|
|
848
|
-
fromLayer,
|
|
849
|
-
toLayer,
|
|
850
|
-
severity,
|
|
851
|
-
message,
|
|
852
|
-
rule: blocked
|
|
853
|
-
};
|
|
854
|
-
deps.appendTrace({
|
|
855
|
-
type: "layer.observedViolation",
|
|
856
|
-
timestamp: (/* @__PURE__ */ new Date()).toISOString(),
|
|
857
|
-
intent: event.intent,
|
|
858
|
-
correlationId: event.metadata.correlationId,
|
|
859
|
-
traceId: event.metadata.traceId,
|
|
860
|
-
spanId: event.metadata.spanId,
|
|
861
|
-
details
|
|
862
|
-
});
|
|
863
|
-
await deps.recordAudit("layer.observedViolation", event, details);
|
|
864
|
-
if (severity === "hard") {
|
|
865
|
-
throw new ObservedLayerFlowViolationError(
|
|
866
|
-
source,
|
|
867
|
-
event.intent,
|
|
868
|
-
fromLayer,
|
|
869
|
-
toLayer,
|
|
870
|
-
message
|
|
871
|
-
);
|
|
872
|
-
}
|
|
254
|
+
// src/domain/sourcePolicy.ts
|
|
255
|
+
var SOURCE_POLICY_MESSAGES = {
|
|
256
|
+
RAW_EVENT_PUBLISH: "Publish through a registered intent creator; raw event objects or intent strings bypass Ark contracts and tooling.",
|
|
257
|
+
PUBLISH_MISSING_SOURCE: "Strict Ark publish calls must include metadata.source."
|
|
258
|
+
};
|
|
259
|
+
function looksLikeArkIntent(value) {
|
|
260
|
+
return /^(Domain|Application|Adapter|Workflow|Job|Presentation|Reporting|Metadata|Security|Audit|Observability|Kernel)\.[A-Za-z0-9_.]+$/.test(
|
|
261
|
+
value
|
|
262
|
+
);
|
|
873
263
|
}
|
|
874
|
-
|
|
875
|
-
|
|
876
|
-
|
|
877
|
-
|
|
878
|
-
|
|
879
|
-
|
|
880
|
-
|
|
881
|
-
} catch (err) {
|
|
882
|
-
if (err instanceof PolicyViolationError) {
|
|
883
|
-
deps.appendTrace({
|
|
884
|
-
type: "policy.hardViolation",
|
|
885
|
-
timestamp: (/* @__PURE__ */ new Date()).toISOString(),
|
|
886
|
-
intent: event.intent,
|
|
887
|
-
correlationId: event.metadata.correlationId,
|
|
888
|
-
traceId: event.metadata.traceId,
|
|
889
|
-
spanId: event.metadata.spanId,
|
|
890
|
-
details: { violations: err.violations }
|
|
891
|
-
});
|
|
892
|
-
await deps.recordAudit("policy.hardViolation", event, {
|
|
893
|
-
violations: err.violations
|
|
894
|
-
});
|
|
895
|
-
}
|
|
896
|
-
throw err;
|
|
897
|
-
}
|
|
898
|
-
if (policyResult.softViolations.length > 0) {
|
|
899
|
-
deps.appendTrace({
|
|
900
|
-
type: "policy.softViolation",
|
|
901
|
-
timestamp: (/* @__PURE__ */ new Date()).toISOString(),
|
|
902
|
-
intent: event.intent,
|
|
903
|
-
correlationId: event.metadata.correlationId,
|
|
904
|
-
traceId: event.metadata.traceId,
|
|
905
|
-
spanId: event.metadata.spanId,
|
|
906
|
-
details: { violations: policyResult.softViolations }
|
|
264
|
+
function classifyPublishFacts(facts) {
|
|
265
|
+
if (!facts.publishCall) return [];
|
|
266
|
+
const findings = [];
|
|
267
|
+
if (facts.rawIntentName !== void 0 && looksLikeArkIntent(facts.rawIntentName) || facts.objectHasIntent) {
|
|
268
|
+
findings.push({
|
|
269
|
+
ruleId: "RAW_EVENT_PUBLISH",
|
|
270
|
+
message: SOURCE_POLICY_MESSAGES.RAW_EVENT_PUBLISH
|
|
907
271
|
});
|
|
908
|
-
|
|
909
|
-
|
|
272
|
+
}
|
|
273
|
+
if (facts.arkPublishCandidate && !facts.hasSource) {
|
|
274
|
+
findings.push({
|
|
275
|
+
ruleId: "PUBLISH_MISSING_SOURCE",
|
|
276
|
+
message: SOURCE_POLICY_MESSAGES.PUBLISH_MISSING_SOURCE
|
|
910
277
|
});
|
|
911
|
-
if (deps.onSoftViolation) {
|
|
912
|
-
await deps.safeHook(
|
|
913
|
-
() => deps.onSoftViolation(policyResult, event),
|
|
914
|
-
"onSoftViolation",
|
|
915
|
-
event
|
|
916
|
-
);
|
|
917
|
-
}
|
|
918
278
|
}
|
|
279
|
+
return findings;
|
|
919
280
|
}
|
|
920
281
|
|
|
921
|
-
// src/kernel/
|
|
922
|
-
function
|
|
923
|
-
|
|
924
|
-
if (buffers.maxHistorySize !== void 0 && buffers.history.length > buffers.maxHistorySize) {
|
|
925
|
-
buffers.history.splice(0, buffers.history.length - buffers.maxHistorySize);
|
|
926
|
-
}
|
|
282
|
+
// src/kernel/semanticAnalysis.ts
|
|
283
|
+
function literalText(ts, node) {
|
|
284
|
+
return node && ts.isStringLiteralLike(node) ? node.text : void 0;
|
|
927
285
|
}
|
|
928
|
-
function
|
|
929
|
-
|
|
930
|
-
|
|
931
|
-
|
|
286
|
+
function lineOf(sourceFile, node) {
|
|
287
|
+
return sourceFile.getLineAndCharacterOfPosition(node.getStart(sourceFile)).line + 1;
|
|
288
|
+
}
|
|
289
|
+
function isTypeOnlyReference(ts, node) {
|
|
290
|
+
if (ts.isImportDeclaration(node)) {
|
|
291
|
+
const clause = node.importClause;
|
|
292
|
+
if (!clause) return false;
|
|
293
|
+
if (clause.isTypeOnly) return true;
|
|
294
|
+
const named = clause.namedBindings;
|
|
295
|
+
return Boolean(
|
|
296
|
+
named && ts.isNamedImports(named) && named.elements.length > 0 && named.elements.every((element) => element.isTypeOnly)
|
|
297
|
+
);
|
|
932
298
|
}
|
|
933
|
-
|
|
934
|
-
|
|
935
|
-
|
|
936
|
-
|
|
937
|
-
|
|
299
|
+
if (ts.isExportDeclaration(node)) {
|
|
300
|
+
if (node.isTypeOnly) return true;
|
|
301
|
+
const clause = node.exportClause;
|
|
302
|
+
return Boolean(
|
|
303
|
+
clause && ts.isNamedExports(clause) && clause.elements.length > 0 && clause.elements.every((element) => element.isTypeOnly)
|
|
304
|
+
);
|
|
938
305
|
}
|
|
306
|
+
return false;
|
|
939
307
|
}
|
|
940
|
-
|
|
941
|
-
|
|
308
|
+
function singleFileChecker(ts, sourceFile) {
|
|
309
|
+
const options = { noLib: true, noResolve: true, target: ts.ScriptTarget.Latest };
|
|
310
|
+
const host = ts.createCompilerHost(options, true);
|
|
311
|
+
host.getSourceFile = (fileName) => fileName === sourceFile.fileName ? sourceFile : void 0;
|
|
312
|
+
host.fileExists = (fileName) => fileName === sourceFile.fileName;
|
|
313
|
+
host.readFile = (fileName) => fileName === sourceFile.fileName ? sourceFile.text : void 0;
|
|
314
|
+
return ts.createProgram([sourceFile.fileName], options, host).getTypeChecker();
|
|
315
|
+
}
|
|
316
|
+
function symbolAt(checker, node) {
|
|
942
317
|
try {
|
|
943
|
-
|
|
944
|
-
|
|
945
|
-
|
|
946
|
-
intent: event.intent,
|
|
947
|
-
correlationId: event.metadata.correlationId,
|
|
948
|
-
causationId: event.metadata.causationId,
|
|
949
|
-
subject: event.intent,
|
|
950
|
-
details
|
|
951
|
-
});
|
|
952
|
-
} catch (err) {
|
|
953
|
-
appendTrace(buffers, {
|
|
954
|
-
type: "hook.error",
|
|
955
|
-
timestamp: (/* @__PURE__ */ new Date()).toISOString(),
|
|
956
|
-
intent: event.intent,
|
|
957
|
-
correlationId: event.metadata.correlationId,
|
|
958
|
-
traceId: event.metadata.traceId,
|
|
959
|
-
spanId: event.metadata.spanId,
|
|
960
|
-
details: {
|
|
961
|
-
hook: "auditTrail",
|
|
962
|
-
error: err instanceof Error ? err.message : String(err)
|
|
963
|
-
}
|
|
964
|
-
});
|
|
318
|
+
return checker.getSymbolAtLocation(node);
|
|
319
|
+
} catch {
|
|
320
|
+
return void 0;
|
|
965
321
|
}
|
|
966
322
|
}
|
|
967
|
-
|
|
968
|
-
const
|
|
969
|
-
|
|
970
|
-
|
|
971
|
-
|
|
972
|
-
}
|
|
973
|
-
|
|
974
|
-
|
|
975
|
-
|
|
976
|
-
|
|
977
|
-
|
|
978
|
-
|
|
979
|
-
|
|
980
|
-
details
|
|
981
|
-
});
|
|
982
|
-
await recordAudit(buffers, "event.rawPublish", event, details);
|
|
323
|
+
function localDeclaration(ts, checker, sourceFile, node) {
|
|
324
|
+
const shorthand = node.parent && ts.isShorthandPropertyAssignment(node.parent) && node.parent.name === node;
|
|
325
|
+
let symbol;
|
|
326
|
+
try {
|
|
327
|
+
symbol = shorthand ? checker.getShorthandAssignmentValueSymbol(node.parent) : symbolAt(checker, node);
|
|
328
|
+
} catch {
|
|
329
|
+
symbol = void 0;
|
|
330
|
+
}
|
|
331
|
+
return Boolean(
|
|
332
|
+
symbol?.declarations?.some(
|
|
333
|
+
(declaration) => declaration.getSourceFile().fileName === sourceFile.fileName
|
|
334
|
+
)
|
|
335
|
+
);
|
|
983
336
|
}
|
|
984
|
-
|
|
985
|
-
|
|
986
|
-
|
|
987
|
-
|
|
988
|
-
|
|
989
|
-
|
|
990
|
-
|
|
991
|
-
|
|
992
|
-
|
|
993
|
-
|
|
994
|
-
timestamp: record.publishedAt,
|
|
995
|
-
intent: event.intent,
|
|
996
|
-
correlationId: event.metadata.correlationId,
|
|
997
|
-
traceId: event.metadata.traceId,
|
|
998
|
-
spanId: event.metadata.spanId,
|
|
999
|
-
details: { subscribersNotified }
|
|
337
|
+
function extractSemanticDependencies(ts, sourceFile) {
|
|
338
|
+
let checker;
|
|
339
|
+
const dependencies = [];
|
|
340
|
+
const add = (node, kind, specifier, typeOnly = false) => dependencies.push({
|
|
341
|
+
specifier,
|
|
342
|
+
kind,
|
|
343
|
+
line: lineOf(sourceFile, node),
|
|
344
|
+
typeOnly,
|
|
345
|
+
unresolved: specifier === void 0,
|
|
346
|
+
node
|
|
1000
347
|
});
|
|
1001
|
-
|
|
1002
|
-
|
|
1003
|
-
|
|
1004
|
-
|
|
1005
|
-
|
|
1006
|
-
|
|
1007
|
-
|
|
1008
|
-
|
|
1009
|
-
|
|
1010
|
-
|
|
1011
|
-
|
|
1012
|
-
|
|
1013
|
-
|
|
1014
|
-
|
|
1015
|
-
|
|
1016
|
-
|
|
1017
|
-
|
|
1018
|
-
|
|
1019
|
-
|
|
1020
|
-
|
|
1021
|
-
|
|
348
|
+
const visit = (node) => {
|
|
349
|
+
if (ts.isImportDeclaration(node)) {
|
|
350
|
+
add(node, "import", literalText(ts, node.moduleSpecifier), isTypeOnlyReference(ts, node));
|
|
351
|
+
} else if (ts.isExportDeclaration(node) && node.moduleSpecifier) {
|
|
352
|
+
add(node, "export", literalText(ts, node.moduleSpecifier), isTypeOnlyReference(ts, node));
|
|
353
|
+
} else if (ts.isImportEqualsDeclaration(node) && ts.isExternalModuleReference(node.moduleReference)) {
|
|
354
|
+
add(node, "require", literalText(ts, node.moduleReference.expression));
|
|
355
|
+
} else if (ts.isCallExpression(node)) {
|
|
356
|
+
const dynamicImport = node.expression.kind === ts.SyntaxKind.ImportKeyword;
|
|
357
|
+
const requireCall = ts.isIdentifier(node.expression) && node.expression.text === "require";
|
|
358
|
+
const directRequire = requireCall && !localDeclaration(
|
|
359
|
+
ts,
|
|
360
|
+
checker ?? (checker = singleFileChecker(ts, sourceFile)),
|
|
361
|
+
sourceFile,
|
|
362
|
+
node.expression
|
|
363
|
+
);
|
|
364
|
+
if (dynamicImport || directRequire) {
|
|
365
|
+
add(node, directRequire ? "require" : "dynamic-import", literalText(ts, node.arguments[0]));
|
|
366
|
+
}
|
|
367
|
+
}
|
|
368
|
+
ts.forEachChild(node, visit);
|
|
1022
369
|
};
|
|
370
|
+
visit(sourceFile);
|
|
371
|
+
return dependencies;
|
|
1023
372
|
}
|
|
1024
|
-
|
|
1025
|
-
|
|
1026
|
-
|
|
1027
|
-
|
|
1028
|
-
|
|
1029
|
-
|
|
1030
|
-
|
|
1031
|
-
|
|
1032
|
-
|
|
1033
|
-
subscriptionsByIntent = /* @__PURE__ */ new Map();
|
|
1034
|
-
interceptors = [];
|
|
1035
|
-
interceptorsByIntent = /* @__PURE__ */ new Map();
|
|
1036
|
-
recording;
|
|
1037
|
-
onPublish;
|
|
1038
|
-
onSoftViolation;
|
|
1039
|
-
onHandlerError;
|
|
1040
|
-
eventContracts;
|
|
1041
|
-
strictEventContracts;
|
|
1042
|
-
requireKnownSource;
|
|
1043
|
-
architectureProfile;
|
|
1044
|
-
enforceObservedLayerFlowMode;
|
|
1045
|
-
rethrowHandlerErrors;
|
|
1046
|
-
policyEngine;
|
|
1047
|
-
getPolicyContext;
|
|
1048
|
-
intentRegistry;
|
|
1049
|
-
dependencyGraph;
|
|
1050
|
-
strictRegistry;
|
|
1051
|
-
validateIntentNaming;
|
|
1052
|
-
constructor(options = {}) {
|
|
1053
|
-
this.onPublish = options.onPublish;
|
|
1054
|
-
this.onSoftViolation = options.onSoftViolation;
|
|
1055
|
-
this.onHandlerError = options.onHandlerError;
|
|
1056
|
-
this.eventContracts = options.eventContracts;
|
|
1057
|
-
this.strictEventContracts = options.strictEventContracts ?? false;
|
|
1058
|
-
this.requireKnownSource = options.requireKnownSource ?? false;
|
|
1059
|
-
this.architectureProfile = options.architectureProfile;
|
|
1060
|
-
this.enforceObservedLayerFlowMode = options.enforceObservedLayerFlow ?? "off";
|
|
1061
|
-
this.rethrowHandlerErrors = options.rethrowHandlerErrors ?? false;
|
|
1062
|
-
this.intentRegistry = options.intentRegistry;
|
|
1063
|
-
this.dependencyGraph = options.dependencyGraph;
|
|
1064
|
-
this.strictRegistry = options.strictRegistry ?? options.intentRegistry !== void 0;
|
|
1065
|
-
this.validateIntentNaming = options.validateIntentNaming ?? this.strictRegistry;
|
|
1066
|
-
this.recording = {
|
|
1067
|
-
history: [],
|
|
1068
|
-
trace: [],
|
|
1069
|
-
maxHistorySize: options.maxHistorySize,
|
|
1070
|
-
traceSinks: [...options.traceSinks ?? []],
|
|
1071
|
-
auditTrail: options.auditTrail,
|
|
1072
|
-
outbox: options.outbox,
|
|
1073
|
-
instanceId: options.instanceId
|
|
1074
|
-
};
|
|
1075
|
-
if (options.policyEngine) {
|
|
1076
|
-
this.policyEngine = options.policyEngine;
|
|
1077
|
-
} else if (options.policies && options.policies.length > 0) {
|
|
1078
|
-
this.policyEngine = new PolicyEngine(options.policies);
|
|
1079
|
-
}
|
|
1080
|
-
const allPolicies = this.policyEngine?.getPolicies() ?? options.policies ?? [];
|
|
1081
|
-
if (allPolicies.some(isLayerPolicy) && !options.intentRegistry && !options.dependencyGraph && !options.getPolicyContext) {
|
|
1082
|
-
throw new LayerPolicyContextError();
|
|
1083
|
-
}
|
|
1084
|
-
if (options.getPolicyContext) {
|
|
1085
|
-
this.getPolicyContext = options.getPolicyContext;
|
|
1086
|
-
} else if (options.intentRegistry || options.dependencyGraph) {
|
|
1087
|
-
this.getPolicyContext = buildPublishPolicyContext({
|
|
1088
|
-
intentRegistry: options.intentRegistry,
|
|
1089
|
-
dependencyGraph: options.dependencyGraph
|
|
1090
|
-
});
|
|
1091
|
-
} else {
|
|
1092
|
-
this.getPolicyContext = (event) => ({ event });
|
|
373
|
+
function staticAccessPath(ts, node) {
|
|
374
|
+
const segments = [];
|
|
375
|
+
let current = node;
|
|
376
|
+
while (ts.isPropertyAccessExpression(current) || ts.isElementAccessExpression(current)) {
|
|
377
|
+
if (ts.isPropertyAccessExpression(current)) segments.unshift(current.name.text);
|
|
378
|
+
else {
|
|
379
|
+
const property = literalText(ts, current.argumentExpression);
|
|
380
|
+
if (property === void 0) return void 0;
|
|
381
|
+
segments.unshift(property);
|
|
1093
382
|
}
|
|
383
|
+
current = current.expression;
|
|
1094
384
|
}
|
|
1095
|
-
|
|
1096
|
-
|
|
1097
|
-
|
|
1098
|
-
|
|
1099
|
-
|
|
1100
|
-
|
|
1101
|
-
|
|
1102
|
-
|
|
1103
|
-
|
|
1104
|
-
|
|
1105
|
-
|
|
1106
|
-
|
|
1107
|
-
|
|
1108
|
-
|
|
1109
|
-
|
|
1110
|
-
|
|
1111
|
-
|
|
1112
|
-
|
|
1113
|
-
|
|
1114
|
-
|
|
1115
|
-
|
|
1116
|
-
|
|
1117
|
-
|
|
1118
|
-
|
|
1119
|
-
|
|
1120
|
-
|
|
1121
|
-
|
|
385
|
+
if (!ts.isIdentifier(current)) return void 0;
|
|
386
|
+
segments.unshift(current.text);
|
|
387
|
+
return { root: current, segments };
|
|
388
|
+
}
|
|
389
|
+
function runtimeIdentifierReference(ts, node) {
|
|
390
|
+
const parent = node.parent;
|
|
391
|
+
if (ts.isPropertyAccessExpression(parent) || ts.isElementAccessExpression(parent)) return false;
|
|
392
|
+
return ts.isExpressionNode(node) && !ts.isInTypeQuery(node) || ts.isShorthandPropertyAssignment(parent) && parent.name === node;
|
|
393
|
+
}
|
|
394
|
+
function bestForbiddenMatch(entries, segments) {
|
|
395
|
+
const normalized = segments[0] === "globalThis" ? segments.slice(1) : segments;
|
|
396
|
+
for (let length = normalized.length; length >= 1; length -= 1) {
|
|
397
|
+
const candidate = normalized.slice(0, length).join(".");
|
|
398
|
+
if (entries.has(candidate)) return candidate;
|
|
399
|
+
}
|
|
400
|
+
return void 0;
|
|
401
|
+
}
|
|
402
|
+
function collectForbiddenCapabilityUses(ts, sourceFile, forbidden) {
|
|
403
|
+
if (forbidden.length === 0) return [];
|
|
404
|
+
const entries = new Set(forbidden);
|
|
405
|
+
const checker = singleFileChecker(ts, sourceFile);
|
|
406
|
+
const aliases = /* @__PURE__ */ new Map();
|
|
407
|
+
const topLevelNames = /* @__PURE__ */ new Set();
|
|
408
|
+
for (const statement of sourceFile.statements) {
|
|
409
|
+
if (ts.isVariableStatement(statement)) {
|
|
410
|
+
for (const declaration of statement.declarationList.declarations) {
|
|
411
|
+
if (ts.isIdentifier(declaration.name)) topLevelNames.add(declaration.name.text);
|
|
412
|
+
}
|
|
1122
413
|
}
|
|
1123
|
-
|
|
1124
|
-
|
|
414
|
+
}
|
|
415
|
+
const resolvePath = (node) => {
|
|
416
|
+
const path = staticAccessPath(ts, node);
|
|
417
|
+
if (!path) return void 0;
|
|
418
|
+
const symbol = symbolAt(checker, path.root);
|
|
419
|
+
const alias = symbol ? aliases.get(symbol) : void 0;
|
|
420
|
+
if (alias) return [...alias, ...path.segments.slice(1)];
|
|
421
|
+
return localDeclaration(ts, checker, sourceFile, path.root) || topLevelNames.has(path.root.text) ? void 0 : path.segments;
|
|
422
|
+
};
|
|
423
|
+
for (const statement of sourceFile.statements) {
|
|
424
|
+
if (!ts.isVariableStatement(statement)) continue;
|
|
425
|
+
for (const declaration of statement.declarationList.declarations) {
|
|
426
|
+
if (!declaration.initializer || !ts.isIdentifier(declaration.name)) continue;
|
|
427
|
+
const path = resolvePath(declaration.initializer);
|
|
428
|
+
const symbol = symbolAt(checker, declaration.name);
|
|
429
|
+
if (!path || !symbol) continue;
|
|
430
|
+
aliases.set(symbol, path);
|
|
1125
431
|
}
|
|
1126
|
-
assertIntentAllowed(event.intent, {
|
|
1127
|
-
strictRegistry: this.strictRegistry,
|
|
1128
|
-
validateIntentNaming: this.validateIntentNaming,
|
|
1129
|
-
intentRegistry: this.intentRegistry
|
|
1130
|
-
});
|
|
1131
|
-
assertSourceAllowed(event, {
|
|
1132
|
-
requireKnownSource: this.requireKnownSource,
|
|
1133
|
-
intentRegistry: this.intentRegistry
|
|
1134
|
-
});
|
|
1135
|
-
assertContractAllowed(event, {
|
|
1136
|
-
eventContracts: this.eventContracts,
|
|
1137
|
-
strictEventContracts: this.strictEventContracts
|
|
1138
|
-
});
|
|
1139
|
-
event = await applyInterceptors(event, {
|
|
1140
|
-
interceptorsForIntent: (intent) => this.interceptorsByIntent.get(intent) ?? [],
|
|
1141
|
-
eventContracts: this.eventContracts,
|
|
1142
|
-
strictEventContracts: this.strictEventContracts,
|
|
1143
|
-
appendTrace: (r) => this.appendTrace(r),
|
|
1144
|
-
recordAudit: (type, e, details) => this.recordAudit(type, e, details)
|
|
1145
|
-
});
|
|
1146
|
-
assertContractAllowed(event, {
|
|
1147
|
-
eventContracts: this.eventContracts,
|
|
1148
|
-
strictEventContracts: this.strictEventContracts
|
|
1149
|
-
});
|
|
1150
|
-
await assertObservedLayerFlowAllowed(event, {
|
|
1151
|
-
mode: this.enforceObservedLayerFlowMode,
|
|
1152
|
-
architectureProfile: this.architectureProfile,
|
|
1153
|
-
appendTrace: (r) => this.appendTrace(r),
|
|
1154
|
-
recordAudit: (type, e, details) => this.recordAudit(type, e, details)
|
|
1155
|
-
});
|
|
1156
|
-
this.dependencyGraph?.registerEventFlow(event.metadata.source, event.intent);
|
|
1157
|
-
const matching = [...this.subscriptionsByIntent.get(event.intent) ?? []];
|
|
1158
|
-
if (this.policyEngine) {
|
|
1159
|
-
await enforcePublishPolicy(event, {
|
|
1160
|
-
policyEngine: this.policyEngine,
|
|
1161
|
-
getPolicyContext: this.getPolicyContext,
|
|
1162
|
-
appendTrace: (r) => this.appendTrace(r),
|
|
1163
|
-
recordAudit: (type, e, details) => this.recordAudit(type, e, details),
|
|
1164
|
-
onSoftViolation: this.onSoftViolation,
|
|
1165
|
-
safeHook: (fn, name, e) => this.safeHook(fn, name, e)
|
|
1166
|
-
});
|
|
1167
|
-
}
|
|
1168
|
-
await recordSuccessfulPublish(
|
|
1169
|
-
this.recording,
|
|
1170
|
-
event,
|
|
1171
|
-
matching.length
|
|
1172
|
-
);
|
|
1173
|
-
await Promise.all(
|
|
1174
|
-
matching.map((sub) => this.invokeHandler(sub, event))
|
|
1175
|
-
);
|
|
1176
|
-
if (this.onPublish) {
|
|
1177
|
-
await this.safeHook(
|
|
1178
|
-
() => this.onPublish(event),
|
|
1179
|
-
"onPublish",
|
|
1180
|
-
event
|
|
1181
|
-
);
|
|
1182
|
-
}
|
|
1183
|
-
}
|
|
1184
|
-
createPublisher(source) {
|
|
1185
|
-
const sourceName = typeof source === "string" ? source : source.name;
|
|
1186
|
-
assertIntentAllowed(sourceName, {
|
|
1187
|
-
strictRegistry: this.strictRegistry,
|
|
1188
|
-
validateIntentNaming: this.validateIntentNaming,
|
|
1189
|
-
intentRegistry: this.intentRegistry
|
|
1190
|
-
});
|
|
1191
|
-
return {
|
|
1192
|
-
source: sourceName,
|
|
1193
|
-
publish: async (intent, payload, metadata = {}) => {
|
|
1194
|
-
if (metadata.source && metadata.source !== sourceName) {
|
|
1195
|
-
throw new SourceMetadataOverrideError(sourceName, metadata.source);
|
|
1196
|
-
}
|
|
1197
|
-
await this.publish(intent, payload, {
|
|
1198
|
-
...metadata,
|
|
1199
|
-
source: sourceName
|
|
1200
|
-
});
|
|
1201
|
-
}
|
|
1202
|
-
};
|
|
1203
|
-
}
|
|
1204
|
-
subscribe(intent, handler) {
|
|
1205
|
-
const intentName = typeof intent === "string" ? intent : intent.name;
|
|
1206
|
-
assertIntentAllowed(intentName, {
|
|
1207
|
-
strictRegistry: this.strictRegistry,
|
|
1208
|
-
validateIntentNaming: this.validateIntentNaming,
|
|
1209
|
-
intentRegistry: this.intentRegistry
|
|
1210
|
-
});
|
|
1211
|
-
const sub = {
|
|
1212
|
-
intentName,
|
|
1213
|
-
handler
|
|
1214
|
-
};
|
|
1215
|
-
this.subscriptions.push(sub);
|
|
1216
|
-
const subscriptionsForIntent = this.subscriptionsByIntent.get(intentName) ?? [];
|
|
1217
|
-
subscriptionsForIntent.push(sub);
|
|
1218
|
-
this.subscriptionsByIntent.set(intentName, subscriptionsForIntent);
|
|
1219
|
-
return () => {
|
|
1220
|
-
const idx = this.subscriptions.indexOf(sub);
|
|
1221
|
-
if (idx >= 0) this.subscriptions.splice(idx, 1);
|
|
1222
|
-
const byIntent = this.subscriptionsByIntent.get(intentName);
|
|
1223
|
-
if (!byIntent) return;
|
|
1224
|
-
const intentIdx = byIntent.indexOf(sub);
|
|
1225
|
-
if (intentIdx >= 0) byIntent.splice(intentIdx, 1);
|
|
1226
|
-
if (byIntent.length === 0) this.subscriptionsByIntent.delete(intentName);
|
|
1227
|
-
};
|
|
1228
|
-
}
|
|
1229
|
-
registerInterceptor(intent, interceptor, interceptorId) {
|
|
1230
|
-
const intentName = typeof intent === "string" ? intent : intent.name;
|
|
1231
|
-
assertIntentAllowed(intentName, {
|
|
1232
|
-
strictRegistry: this.strictRegistry,
|
|
1233
|
-
validateIntentNaming: this.validateIntentNaming,
|
|
1234
|
-
intentRegistry: this.intentRegistry
|
|
1235
|
-
});
|
|
1236
|
-
const registration = {
|
|
1237
|
-
registrationId: nextInterceptorRegistrationId(),
|
|
1238
|
-
interceptorId: interceptorId ?? intentName,
|
|
1239
|
-
intentName,
|
|
1240
|
-
interceptor,
|
|
1241
|
-
createdAt: (/* @__PURE__ */ new Date()).toISOString()
|
|
1242
|
-
};
|
|
1243
|
-
this.interceptors.push(registration);
|
|
1244
|
-
const interceptorsForIntent = this.interceptorsByIntent.get(intentName) ?? [];
|
|
1245
|
-
interceptorsForIntent.push(registration);
|
|
1246
|
-
this.interceptorsByIntent.set(intentName, interceptorsForIntent);
|
|
1247
|
-
return registration.registrationId;
|
|
1248
|
-
}
|
|
1249
|
-
unregisterInterceptor(registrationId) {
|
|
1250
|
-
const interceptor = this.interceptors.find(
|
|
1251
|
-
(candidate) => candidate.registrationId === registrationId
|
|
1252
|
-
);
|
|
1253
|
-
if (!interceptor) return false;
|
|
1254
|
-
const idx = this.interceptors.indexOf(interceptor);
|
|
1255
|
-
if (idx >= 0) this.interceptors.splice(idx, 1);
|
|
1256
|
-
const byIntent = this.interceptorsByIntent.get(interceptor.intentName);
|
|
1257
|
-
if (byIntent) {
|
|
1258
|
-
const intentIdx = byIntent.indexOf(interceptor);
|
|
1259
|
-
if (intentIdx >= 0) byIntent.splice(intentIdx, 1);
|
|
1260
|
-
if (byIntent.length === 0) this.interceptorsByIntent.delete(interceptor.intentName);
|
|
1261
|
-
}
|
|
1262
|
-
return true;
|
|
1263
432
|
}
|
|
1264
|
-
|
|
1265
|
-
|
|
1266
|
-
|
|
1267
|
-
|
|
1268
|
-
|
|
1269
|
-
|
|
1270
|
-
|
|
1271
|
-
})
|
|
1272
|
-
}
|
|
1273
|
-
|
|
1274
|
-
|
|
1275
|
-
|
|
1276
|
-
|
|
1277
|
-
|
|
1278
|
-
|
|
1279
|
-
|
|
1280
|
-
|
|
1281
|
-
}
|
|
1282
|
-
clearTrace() {
|
|
1283
|
-
this.recording.trace.length = 0;
|
|
1284
|
-
}
|
|
1285
|
-
appendTrace(record) {
|
|
1286
|
-
appendTrace(this.recording, record);
|
|
1287
|
-
}
|
|
1288
|
-
async recordAudit(type, event, details) {
|
|
1289
|
-
await recordAudit(this.recording, type, event, details);
|
|
1290
|
-
}
|
|
1291
|
-
async invokeHandler(sub, event) {
|
|
1292
|
-
try {
|
|
1293
|
-
await Promise.resolve(sub.handler(event));
|
|
1294
|
-
} catch (err) {
|
|
1295
|
-
this.appendTrace({
|
|
1296
|
-
type: "handler.error",
|
|
1297
|
-
timestamp: (/* @__PURE__ */ new Date()).toISOString(),
|
|
1298
|
-
intent: event.intent,
|
|
1299
|
-
correlationId: event.metadata.correlationId,
|
|
1300
|
-
traceId: event.metadata.traceId,
|
|
1301
|
-
spanId: event.metadata.spanId,
|
|
1302
|
-
details: { error: err instanceof Error ? err.message : String(err) }
|
|
1303
|
-
});
|
|
1304
|
-
await this.recordAudit("handler.error", event, {
|
|
1305
|
-
handlerIntent: sub.intentName,
|
|
1306
|
-
error: err instanceof Error ? err.message : String(err)
|
|
1307
|
-
});
|
|
1308
|
-
if (this.onHandlerError) {
|
|
1309
|
-
await this.safeHook(
|
|
1310
|
-
() => this.onHandlerError(err, event, sub.intentName),
|
|
1311
|
-
"onHandlerError",
|
|
1312
|
-
event
|
|
1313
|
-
);
|
|
1314
|
-
}
|
|
1315
|
-
if (this.rethrowHandlerErrors) {
|
|
1316
|
-
throw err;
|
|
1317
|
-
}
|
|
433
|
+
const uses = [];
|
|
434
|
+
const seen = /* @__PURE__ */ new Set();
|
|
435
|
+
const flag = (name, node) => {
|
|
436
|
+
const line = lineOf(sourceFile, node);
|
|
437
|
+
const key = `${name}:${node.getStart(sourceFile)}`;
|
|
438
|
+
if (seen.has(key)) return;
|
|
439
|
+
seen.add(key);
|
|
440
|
+
uses.push({ name, line, node });
|
|
441
|
+
};
|
|
442
|
+
const visit = (node) => {
|
|
443
|
+
const parentContinuesPath = node.parent && (ts.isPropertyAccessExpression(node.parent) || ts.isElementAccessExpression(node.parent)) && node.parent.expression === node;
|
|
444
|
+
if ((ts.isPropertyAccessExpression(node) || ts.isElementAccessExpression(node)) && !parentContinuesPath) {
|
|
445
|
+
const path = resolvePath(node);
|
|
446
|
+
const match = path ? bestForbiddenMatch(entries, path) : void 0;
|
|
447
|
+
if (match) flag(match, node);
|
|
448
|
+
} else if (ts.isIdentifier(node) && entries.has(node.text) && runtimeIdentifierReference(ts, node) && !localDeclaration(ts, checker, sourceFile, node)) {
|
|
449
|
+
flag(node.text, node);
|
|
1318
450
|
}
|
|
1319
|
-
|
|
1320
|
-
|
|
1321
|
-
|
|
1322
|
-
|
|
1323
|
-
|
|
1324
|
-
|
|
1325
|
-
|
|
1326
|
-
|
|
1327
|
-
intent: event.intent,
|
|
1328
|
-
correlationId: event.metadata.correlationId,
|
|
1329
|
-
traceId: event.metadata.traceId,
|
|
1330
|
-
spanId: event.metadata.spanId,
|
|
1331
|
-
details: {
|
|
1332
|
-
hook: hookName,
|
|
1333
|
-
error: err instanceof Error ? err.message : String(err)
|
|
451
|
+
if (ts.isVariableDeclaration(node) && ts.isObjectBindingPattern(node.name) && node.initializer) {
|
|
452
|
+
const base = resolvePath(node.initializer);
|
|
453
|
+
if (base) {
|
|
454
|
+
for (const element of node.name.elements) {
|
|
455
|
+
if (!ts.isIdentifier(element.name)) continue;
|
|
456
|
+
const property = element.propertyName ? literalText(ts, element.propertyName) ?? element.propertyName.text : element.name.text;
|
|
457
|
+
const match = bestForbiddenMatch(entries, [...base, property]);
|
|
458
|
+
if (match) flag(match, node.initializer);
|
|
1334
459
|
}
|
|
1335
|
-
}
|
|
1336
|
-
await this.recordAudit("hook.error", event, {
|
|
1337
|
-
hook: hookName,
|
|
1338
|
-
error: err instanceof Error ? err.message : String(err)
|
|
1339
|
-
});
|
|
460
|
+
}
|
|
1340
461
|
}
|
|
1341
|
-
|
|
1342
|
-
};
|
|
1343
|
-
|
|
1344
|
-
return
|
|
462
|
+
ts.forEachChild(node, visit);
|
|
463
|
+
};
|
|
464
|
+
visit(sourceFile);
|
|
465
|
+
return uses;
|
|
1345
466
|
}
|
|
1346
467
|
|
|
1347
|
-
// src/kernel/
|
|
1348
|
-
function
|
|
1349
|
-
|
|
1350
|
-
return path.map(
|
|
1351
|
-
(segment) => typeof segment === "object" && segment !== null && "key" in segment ? String(segment.key) : String(segment)
|
|
1352
|
-
).join(".");
|
|
1353
|
-
}
|
|
1354
|
-
function actualType(value) {
|
|
1355
|
-
if (Array.isArray(value)) return "array";
|
|
1356
|
-
if (value === null) return "object";
|
|
1357
|
-
const type = typeof value;
|
|
1358
|
-
if (type === "string" || type === "number" || type === "boolean" || type === "object") {
|
|
1359
|
-
return type;
|
|
1360
|
-
}
|
|
1361
|
-
return "unknown";
|
|
1362
|
-
}
|
|
1363
|
-
function formatPath(parent, field) {
|
|
1364
|
-
return parent ? `${parent}.${field}` : field;
|
|
1365
|
-
}
|
|
1366
|
-
function validateSchemaField(value, fieldSchema, path, contract, issues) {
|
|
1367
|
-
const valueType2 = actualType(value);
|
|
1368
|
-
if (fieldSchema.type !== "unknown" && valueType2 !== fieldSchema.type) {
|
|
1369
|
-
issues.push({
|
|
1370
|
-
intent: contract.intent,
|
|
1371
|
-
version: contract.version,
|
|
1372
|
-
field: path,
|
|
1373
|
-
message: `Expected ${fieldSchema.type}, received ${valueType2}.`
|
|
1374
|
-
});
|
|
1375
|
-
return;
|
|
1376
|
-
}
|
|
1377
|
-
if (fieldSchema.enum && !fieldSchema.enum.some((allowed) => Object.is(allowed, value))) {
|
|
1378
|
-
issues.push({
|
|
1379
|
-
intent: contract.intent,
|
|
1380
|
-
version: contract.version,
|
|
1381
|
-
field: path,
|
|
1382
|
-
message: "Value is not allowed by event contract enum."
|
|
1383
|
-
});
|
|
1384
|
-
}
|
|
1385
|
-
if (fieldSchema.type === "object" && fieldSchema.fields) {
|
|
1386
|
-
if (value === null || typeof value !== "object" || Array.isArray(value)) {
|
|
1387
|
-
issues.push({
|
|
1388
|
-
intent: contract.intent,
|
|
1389
|
-
version: contract.version,
|
|
1390
|
-
field: path,
|
|
1391
|
-
message: "Nested object field must be a non-array object."
|
|
1392
|
-
});
|
|
1393
|
-
return;
|
|
1394
|
-
}
|
|
1395
|
-
validateObjectSchema(
|
|
1396
|
-
value,
|
|
1397
|
-
fieldSchema.fields,
|
|
1398
|
-
contract,
|
|
1399
|
-
issues,
|
|
1400
|
-
path
|
|
1401
|
-
);
|
|
1402
|
-
}
|
|
1403
|
-
if (fieldSchema.type === "array" && fieldSchema.items && Array.isArray(value)) {
|
|
1404
|
-
value.forEach((item, index) => {
|
|
1405
|
-
validateSchemaField(
|
|
1406
|
-
item,
|
|
1407
|
-
fieldSchema.items,
|
|
1408
|
-
`${path}[${index}]`,
|
|
1409
|
-
contract,
|
|
1410
|
-
issues
|
|
1411
|
-
);
|
|
1412
|
-
});
|
|
1413
|
-
}
|
|
468
|
+
// src/kernel/ai-gate/AICodeGate.ts
|
|
469
|
+
function violation(ruleId, message, extra) {
|
|
470
|
+
return { ruleId, code: ruleId, message, ...extra };
|
|
1414
471
|
}
|
|
1415
|
-
function
|
|
1416
|
-
|
|
1417
|
-
const path = formatPath(parentPath, field);
|
|
1418
|
-
const value = payload[field];
|
|
1419
|
-
if (value === void 0) {
|
|
1420
|
-
if (fieldSchema.required) {
|
|
1421
|
-
issues.push({
|
|
1422
|
-
intent: contract.intent,
|
|
1423
|
-
version: contract.version,
|
|
1424
|
-
field: path,
|
|
1425
|
-
message: "Required field is missing."
|
|
1426
|
-
});
|
|
1427
|
-
}
|
|
1428
|
-
continue;
|
|
1429
|
-
}
|
|
1430
|
-
validateSchemaField(value, fieldSchema, path, contract, issues);
|
|
1431
|
-
}
|
|
472
|
+
function lineOf2(source, index) {
|
|
473
|
+
return source.slice(0, index).split("\n").length;
|
|
1432
474
|
}
|
|
1433
|
-
|
|
1434
|
-
|
|
1435
|
-
|
|
1436
|
-
|
|
1437
|
-
|
|
1438
|
-
|
|
1439
|
-
}
|
|
1440
|
-
this.contracts.set(key, { allowAdditionalFields: true, ...contract });
|
|
1441
|
-
}
|
|
1442
|
-
get(intent, version2) {
|
|
1443
|
-
if (version2) return this.contracts.get(this.key(intent, version2));
|
|
1444
|
-
return this.list(intent).at(-1);
|
|
1445
|
-
}
|
|
1446
|
-
list(intent) {
|
|
1447
|
-
return Array.from(this.contracts.values()).filter(
|
|
1448
|
-
(contract) => !intent || contract.intent === intent
|
|
1449
|
-
);
|
|
475
|
+
function extractQuotedStrings(source) {
|
|
476
|
+
const matches = [];
|
|
477
|
+
const re = /['"`]([A-Za-z][A-Za-z0-9_.]*)['"`]/g;
|
|
478
|
+
let m;
|
|
479
|
+
while ((m = re.exec(source)) !== null) {
|
|
480
|
+
matches.push({ value: m[1], index: m.index });
|
|
1450
481
|
}
|
|
1451
|
-
|
|
1452
|
-
|
|
1453
|
-
|
|
1454
|
-
|
|
1455
|
-
|
|
1456
|
-
|
|
1457
|
-
|
|
1458
|
-
|
|
1459
|
-
|
|
1460
|
-
|
|
1461
|
-
|
|
1462
|
-
|
|
1463
|
-
|
|
1464
|
-
|
|
1465
|
-
|
|
1466
|
-
|
|
1467
|
-
|
|
1468
|
-
|
|
1469
|
-
|
|
1470
|
-
|
|
1471
|
-
message: typeof contract.deprecated === "string" ? contract.deprecated : "Event contract is deprecated."
|
|
1472
|
-
});
|
|
1473
|
-
}
|
|
1474
|
-
const payload = event.payload != null && typeof event.payload === "object" ? event.payload : void 0;
|
|
1475
|
-
if (contract.schema) {
|
|
1476
|
-
if (!payload) {
|
|
1477
|
-
issues.push({
|
|
1478
|
-
intent: event.intent,
|
|
1479
|
-
version: contract.version,
|
|
1480
|
-
message: "Payload must be an object for schema validation."
|
|
1481
|
-
});
|
|
1482
|
-
} else {
|
|
1483
|
-
validateObjectSchema(payload, contract.schema, contract, issues);
|
|
1484
|
-
if (contract.allowAdditionalFields === false) {
|
|
1485
|
-
for (const field of Object.keys(payload)) {
|
|
1486
|
-
if (!contract.schema[field]) {
|
|
1487
|
-
issues.push({
|
|
1488
|
-
intent: event.intent,
|
|
1489
|
-
version: contract.version,
|
|
1490
|
-
field,
|
|
1491
|
-
message: "Additional field is not allowed by event contract."
|
|
1492
|
-
});
|
|
1493
|
-
}
|
|
1494
|
-
}
|
|
1495
|
-
}
|
|
1496
|
-
}
|
|
482
|
+
return matches;
|
|
483
|
+
}
|
|
484
|
+
function extractModuleSpecifiers(source) {
|
|
485
|
+
const matches = [];
|
|
486
|
+
const patterns = [
|
|
487
|
+
{
|
|
488
|
+
kind: "import",
|
|
489
|
+
re: /\bimport\s+(?:type\s+)?(?:[^'"]*?\s+from\s*)?['"]([^'"]+)['"]/g
|
|
490
|
+
},
|
|
491
|
+
{
|
|
492
|
+
kind: "export",
|
|
493
|
+
re: /\bexport\s+(?:type\s+)?[^'"]*?\s+from\s*['"]([^'"]+)['"]/g
|
|
494
|
+
},
|
|
495
|
+
{
|
|
496
|
+
kind: "dynamic-import",
|
|
497
|
+
re: /\bimport\s*\(\s*['"]([^'"]+)['"]\s*\)/g
|
|
498
|
+
},
|
|
499
|
+
{
|
|
500
|
+
kind: "require",
|
|
501
|
+
re: /\brequire\s*\(\s*['"]([^'"]+)['"]\s*\)/g
|
|
1497
502
|
}
|
|
1498
|
-
|
|
1499
|
-
|
|
1500
|
-
|
|
1501
|
-
|
|
1502
|
-
|
|
1503
|
-
|
|
1504
|
-
|
|
1505
|
-
|
|
1506
|
-
} else if (result.issues) {
|
|
1507
|
-
for (const issue of result.issues) {
|
|
1508
|
-
issues.push({
|
|
1509
|
-
intent: event.intent,
|
|
1510
|
-
version: contract.version,
|
|
1511
|
-
field: formatStandardSchemaPath(issue.path),
|
|
1512
|
-
message: issue.message
|
|
1513
|
-
});
|
|
1514
|
-
}
|
|
1515
|
-
}
|
|
503
|
+
];
|
|
504
|
+
for (const pattern of patterns) {
|
|
505
|
+
let match;
|
|
506
|
+
while ((match = pattern.re.exec(source)) !== null) {
|
|
507
|
+
const index = match.index + match[0].indexOf(match[1]);
|
|
508
|
+
const raw = match[0];
|
|
509
|
+
const typeOnly = pattern.kind === "import" && /\bimport\s+type\b/.test(raw) || pattern.kind === "export" && /\bexport\s+type\b/.test(raw);
|
|
510
|
+
matches.push({ value: match[1], index, kind: pattern.kind, typeOnly });
|
|
1516
511
|
}
|
|
1517
|
-
return { ok: issues.length === 0, contract, issues };
|
|
1518
512
|
}
|
|
1519
|
-
|
|
1520
|
-
this.contracts.clear();
|
|
1521
|
-
}
|
|
1522
|
-
key(intent, version2) {
|
|
1523
|
-
return `${intent}@${version2}`;
|
|
1524
|
-
}
|
|
1525
|
-
};
|
|
1526
|
-
function createEventContractRegistry() {
|
|
1527
|
-
return new EventContractRegistryImpl();
|
|
1528
|
-
}
|
|
1529
|
-
|
|
1530
|
-
// src/kernel/outbox/InMemoryOutboxStore.ts
|
|
1531
|
-
var outboxSequence = 0;
|
|
1532
|
-
function nextOutboxId() {
|
|
1533
|
-
outboxSequence += 1;
|
|
1534
|
-
return `outbox-${Date.now()}-${outboxSequence}`;
|
|
513
|
+
return matches.sort((a, b) => a.index - b.index);
|
|
1535
514
|
}
|
|
1536
|
-
function
|
|
1537
|
-
|
|
1538
|
-
|
|
1539
|
-
|
|
1540
|
-
|
|
1541
|
-
|
|
515
|
+
function extractQuotedStringsAst(ts, source) {
|
|
516
|
+
const sourceFile = ts.createSourceFile("generated.ts", source, ts.ScriptTarget.Latest, true);
|
|
517
|
+
const matches = [];
|
|
518
|
+
const visit = (node) => {
|
|
519
|
+
if (ts.isStringLiteralLike(node)) {
|
|
520
|
+
matches.push({ value: node.text, index: node.getStart(sourceFile) });
|
|
1542
521
|
}
|
|
522
|
+
ts.forEachChild(node, visit);
|
|
1543
523
|
};
|
|
524
|
+
visit(sourceFile);
|
|
525
|
+
return matches;
|
|
1544
526
|
}
|
|
1545
|
-
|
|
1546
|
-
|
|
1547
|
-
|
|
1548
|
-
|
|
1549
|
-
|
|
1550
|
-
|
|
1551
|
-
|
|
1552
|
-
|
|
1553
|
-
|
|
1554
|
-
|
|
1555
|
-
|
|
1556
|
-
|
|
1557
|
-
|
|
1558
|
-
|
|
1559
|
-
}
|
|
1560
|
-
async markDispatched(id) {
|
|
1561
|
-
const record = this.records.get(id);
|
|
1562
|
-
if (!record) return;
|
|
1563
|
-
record.status = "dispatched";
|
|
1564
|
-
record.updatedAt = (/* @__PURE__ */ new Date()).toISOString();
|
|
1565
|
-
}
|
|
1566
|
-
async markFailed(id, error) {
|
|
1567
|
-
const record = this.records.get(id);
|
|
1568
|
-
if (!record) return;
|
|
1569
|
-
record.status = "failed";
|
|
1570
|
-
record.attempts += 1;
|
|
1571
|
-
record.error = error instanceof Error ? error.message : String(error);
|
|
1572
|
-
record.updatedAt = (/* @__PURE__ */ new Date()).toISOString();
|
|
1573
|
-
}
|
|
1574
|
-
async list(status) {
|
|
1575
|
-
return Array.from(this.records.values()).filter((record) => !status || record.status === status).map(cloneRecord);
|
|
1576
|
-
}
|
|
1577
|
-
async clear() {
|
|
1578
|
-
this.records.clear();
|
|
1579
|
-
}
|
|
1580
|
-
};
|
|
1581
|
-
|
|
1582
|
-
// src/kernel/observability/ObservabilityReporter.ts
|
|
1583
|
-
function flowKey(flow) {
|
|
1584
|
-
return `${flow.from}->${flow.to}`;
|
|
527
|
+
function hasInfrastructureToken(specifier) {
|
|
528
|
+
const tokens = specifier.toLowerCase().split(/[^a-z0-9]+/).filter(Boolean);
|
|
529
|
+
return [
|
|
530
|
+
"adapter",
|
|
531
|
+
"adapters",
|
|
532
|
+
"infra",
|
|
533
|
+
"infrastructure",
|
|
534
|
+
"persistence",
|
|
535
|
+
"repository",
|
|
536
|
+
"repositories",
|
|
537
|
+
"integration",
|
|
538
|
+
"database",
|
|
539
|
+
"db"
|
|
540
|
+
].some((token) => tokens.includes(token));
|
|
1585
541
|
}
|
|
1586
|
-
function
|
|
1587
|
-
const
|
|
1588
|
-
|
|
1589
|
-
|
|
1590
|
-
|
|
1591
|
-
if (seen.has(key)) continue;
|
|
1592
|
-
seen.add(key);
|
|
1593
|
-
result.push(flow);
|
|
1594
|
-
}
|
|
1595
|
-
return result;
|
|
542
|
+
function isKnownInfrastructurePackage(specifier) {
|
|
543
|
+
const normalized = specifier.toLowerCase();
|
|
544
|
+
return ["sequelize", "prisma", "typeorm", "mongoose", "knex"].some(
|
|
545
|
+
(name) => normalized === name || normalized.startsWith(`${name}/`)
|
|
546
|
+
);
|
|
1596
547
|
}
|
|
1597
|
-
function
|
|
1598
|
-
const
|
|
1599
|
-
return
|
|
548
|
+
function layerHasInfrastructureRole(layerName) {
|
|
549
|
+
const normalized = layerName.toLowerCase();
|
|
550
|
+
return [
|
|
551
|
+
"adapter",
|
|
552
|
+
"infra",
|
|
553
|
+
"persistence",
|
|
554
|
+
"repository",
|
|
555
|
+
"repositories",
|
|
556
|
+
"integration",
|
|
557
|
+
"database"
|
|
558
|
+
].some((token) => normalized.includes(token));
|
|
1600
559
|
}
|
|
1601
|
-
function
|
|
1602
|
-
|
|
1603
|
-
const eventBus = options.eventBus;
|
|
1604
|
-
const graph = options.graph;
|
|
1605
|
-
return {
|
|
1606
|
-
report() {
|
|
1607
|
-
const declaredFromRegistry = registry ? registry.getAllRelationships().filter((relationship) => relationship.kind === "produces").map((relationship) => ({
|
|
1608
|
-
from: relationship.from,
|
|
1609
|
-
to: relationship.to
|
|
1610
|
-
})) : [];
|
|
1611
|
-
const declaredFromGraph = declaredFromRegistry.length > 0 || !graph ? [] : graph.getEdges().filter((edge) => edge.kind === "produces").map((edge) => ({ from: edge.from, to: edge.to }));
|
|
1612
|
-
const observedFromHistory = eventBus?.getHistory().map((record) => ({
|
|
1613
|
-
from: record.event.metadata.source,
|
|
1614
|
-
to: record.event.intent
|
|
1615
|
-
})) ?? [];
|
|
1616
|
-
const observedFromGraph = observedFromHistory.length > 0 || !graph ? [] : graph.getEdges().filter((edge) => edge.kind === "observed").map((edge) => ({ from: edge.from, to: edge.to }));
|
|
1617
|
-
const declaredProductions = uniqueFlows([
|
|
1618
|
-
...declaredFromRegistry,
|
|
1619
|
-
...declaredFromGraph
|
|
1620
|
-
]);
|
|
1621
|
-
const observedProductions = uniqueFlows([
|
|
1622
|
-
...observedFromHistory,
|
|
1623
|
-
...observedFromGraph
|
|
1624
|
-
]);
|
|
1625
|
-
const unknownSources = observedProductions.filter(
|
|
1626
|
-
(flow) => !flow.from || flow.from === "unknown"
|
|
1627
|
-
);
|
|
1628
|
-
const unregisteredObservedSources = registry ? Array.from(
|
|
1629
|
-
new Set(
|
|
1630
|
-
observedProductions.map((flow) => flow.from).filter(
|
|
1631
|
-
(source) => source && source !== "unknown" && !registry.has(source)
|
|
1632
|
-
)
|
|
1633
|
-
)
|
|
1634
|
-
) : [];
|
|
1635
|
-
const unregisteredObservedIntents = registry ? Array.from(
|
|
1636
|
-
new Set(
|
|
1637
|
-
observedProductions.map((flow) => flow.to).filter((intent) => !registry.has(intent))
|
|
1638
|
-
)
|
|
1639
|
-
) : [];
|
|
1640
|
-
const observedIntentNames = new Set(
|
|
1641
|
-
observedProductions.flatMap((flow) => [flow.from, flow.to])
|
|
1642
|
-
);
|
|
1643
|
-
const registeredButNeverObserved = registry ? registry.list().map((intent) => intent.name).filter((intent) => !observedIntentNames.has(intent)) : [];
|
|
1644
|
-
return {
|
|
1645
|
-
generatedAt: (/* @__PURE__ */ new Date()).toISOString(),
|
|
1646
|
-
declaredProductions,
|
|
1647
|
-
observedProductions,
|
|
1648
|
-
declaredButUnobserved: difference(declaredProductions, observedProductions),
|
|
1649
|
-
observedButUndeclared: difference(
|
|
1650
|
-
observedProductions.filter((flow) => flow.from !== "unknown"),
|
|
1651
|
-
declaredProductions
|
|
1652
|
-
),
|
|
1653
|
-
unknownSources,
|
|
1654
|
-
unregisteredObservedSources,
|
|
1655
|
-
unregisteredObservedIntents,
|
|
1656
|
-
registeredButNeverObserved
|
|
1657
|
-
};
|
|
1658
|
-
}
|
|
1659
|
-
};
|
|
560
|
+
function tsStringLiteralText(ts, node) {
|
|
561
|
+
return node && ts.isStringLiteralLike(node) ? node.text : void 0;
|
|
1660
562
|
}
|
|
1661
|
-
|
|
1662
|
-
|
|
1663
|
-
|
|
1664
|
-
return
|
|
1665
|
-
events(intent) {
|
|
1666
|
-
return kernel.eventBus.getHistory().map((record) => record.event).filter((event) => !intent || event.intent === intent);
|
|
1667
|
-
},
|
|
1668
|
-
traces(type) {
|
|
1669
|
-
return kernel.eventBus.getTrace().filter((record) => !type || record.type === type);
|
|
1670
|
-
},
|
|
1671
|
-
audit(query) {
|
|
1672
|
-
return kernel.auditTrail.query(query);
|
|
1673
|
-
},
|
|
1674
|
-
outbox(status) {
|
|
1675
|
-
return kernel.outbox.list(status);
|
|
1676
|
-
},
|
|
1677
|
-
observability() {
|
|
1678
|
-
kernel.syncGraph();
|
|
1679
|
-
return kernel.observability.report();
|
|
1680
|
-
},
|
|
1681
|
-
async snapshot() {
|
|
1682
|
-
return {
|
|
1683
|
-
events: this.events(),
|
|
1684
|
-
traces: this.traces(),
|
|
1685
|
-
audit: await this.audit(),
|
|
1686
|
-
outbox: await this.outbox(),
|
|
1687
|
-
observability: this.observability()
|
|
1688
|
-
};
|
|
1689
|
-
},
|
|
1690
|
-
async clear() {
|
|
1691
|
-
kernel.eventBus.clearHistory();
|
|
1692
|
-
kernel.eventBus.clearTrace();
|
|
1693
|
-
await kernel.auditTrail.clear();
|
|
1694
|
-
await kernel.outbox.clear();
|
|
1695
|
-
}
|
|
1696
|
-
};
|
|
563
|
+
function tsPropertyName(ts, node) {
|
|
564
|
+
if (!node) return void 0;
|
|
565
|
+
if (ts.isIdentifier(node) || ts.isStringLiteralLike(node)) return node.text;
|
|
566
|
+
return void 0;
|
|
1697
567
|
}
|
|
1698
|
-
|
|
1699
|
-
|
|
1700
|
-
|
|
1701
|
-
|
|
1702
|
-
|
|
1703
|
-
return `audit-${Date.now()}-${auditSequence}`;
|
|
1704
|
-
}
|
|
1705
|
-
function matchesQuery(record, query) {
|
|
1706
|
-
if (query.type && record.type !== query.type) return false;
|
|
1707
|
-
if (query.intent && record.intent !== query.intent) return false;
|
|
1708
|
-
if (query.correlationId && record.correlationId !== query.correlationId) return false;
|
|
1709
|
-
if (query.subject && record.subject !== query.subject) return false;
|
|
1710
|
-
if (query.since && record.timestamp < query.since) return false;
|
|
1711
|
-
if (query.until && record.timestamp > query.until) return false;
|
|
1712
|
-
return true;
|
|
1713
|
-
}
|
|
1714
|
-
var InMemoryAuditStore = class {
|
|
1715
|
-
constructor(maxRecords) {
|
|
1716
|
-
this.maxRecords = maxRecords;
|
|
1717
|
-
}
|
|
1718
|
-
maxRecords;
|
|
1719
|
-
records = [];
|
|
1720
|
-
append(record) {
|
|
1721
|
-
this.records.push(record);
|
|
1722
|
-
if (this.maxRecords !== void 0 && this.records.length > this.maxRecords) {
|
|
1723
|
-
this.records.splice(0, this.records.length - this.maxRecords);
|
|
568
|
+
function tsObjectProperty(ts, node, name) {
|
|
569
|
+
if (!node || !ts.isObjectLiteralExpression(node)) return void 0;
|
|
570
|
+
return node.properties.find((property) => {
|
|
571
|
+
if (!ts.isPropertyAssignment(property) && !ts.isShorthandPropertyAssignment(property)) {
|
|
572
|
+
return false;
|
|
1724
573
|
}
|
|
1725
|
-
|
|
1726
|
-
|
|
1727
|
-
const records = this.records.filter((record) => matchesQuery(record, query));
|
|
1728
|
-
return query.limit === void 0 ? [...records] : records.slice(-query.limit);
|
|
1729
|
-
}
|
|
1730
|
-
clear() {
|
|
1731
|
-
this.records.length = 0;
|
|
1732
|
-
}
|
|
1733
|
-
};
|
|
1734
|
-
var AuditTrailImpl = class {
|
|
1735
|
-
constructor(store) {
|
|
1736
|
-
this.store = store;
|
|
1737
|
-
}
|
|
1738
|
-
store;
|
|
1739
|
-
async record(input) {
|
|
1740
|
-
const record = {
|
|
1741
|
-
id: createAuditId(),
|
|
1742
|
-
timestamp: input.timestamp ?? (/* @__PURE__ */ new Date()).toISOString(),
|
|
1743
|
-
type: input.type,
|
|
1744
|
-
source: input.source,
|
|
1745
|
-
actor: input.actor,
|
|
1746
|
-
intent: input.intent,
|
|
1747
|
-
correlationId: input.correlationId,
|
|
1748
|
-
causationId: input.causationId,
|
|
1749
|
-
subject: input.subject,
|
|
1750
|
-
details: input.details
|
|
1751
|
-
};
|
|
1752
|
-
await this.store.append(record);
|
|
1753
|
-
return record;
|
|
1754
|
-
}
|
|
1755
|
-
async query(query) {
|
|
1756
|
-
return this.store.query(query);
|
|
1757
|
-
}
|
|
1758
|
-
async clear() {
|
|
1759
|
-
await this.store.clear();
|
|
1760
|
-
}
|
|
1761
|
-
};
|
|
1762
|
-
function createAuditTrail(options = {}) {
|
|
1763
|
-
return new AuditTrailImpl(
|
|
1764
|
-
options.store ?? new InMemoryAuditStore(options.maxRecords)
|
|
1765
|
-
);
|
|
574
|
+
return tsPropertyName(ts, property.name) === name;
|
|
575
|
+
});
|
|
1766
576
|
}
|
|
1767
|
-
|
|
1768
|
-
|
|
1769
|
-
|
|
1770
|
-
|
|
1771
|
-
|
|
1772
|
-
|
|
1773
|
-
|
|
1774
|
-
|
|
1775
|
-
|
|
1776
|
-
|
|
1777
|
-
|
|
1778
|
-
|
|
1779
|
-
|
|
1780
|
-
|
|
1781
|
-
|
|
1782
|
-
|
|
1783
|
-
return Array.from(this.nodes.values());
|
|
1784
|
-
}
|
|
1785
|
-
getEdges() {
|
|
1786
|
-
return [...this.edges];
|
|
577
|
+
function tsObjectHasProperty(ts, node, name) {
|
|
578
|
+
return tsObjectProperty(ts, node, name) !== void 0;
|
|
579
|
+
}
|
|
580
|
+
function tsObjectPropertyValue(ts, node, name) {
|
|
581
|
+
const property = tsObjectProperty(ts, node, name);
|
|
582
|
+
return property && ts.isPropertyAssignment(property) ? property.initializer : void 0;
|
|
583
|
+
}
|
|
584
|
+
function tsObjectHasMetadataSource(ts, node) {
|
|
585
|
+
const metadata = tsObjectPropertyValue(ts, node, "metadata");
|
|
586
|
+
return tsObjectHasProperty(ts, metadata, "source");
|
|
587
|
+
}
|
|
588
|
+
function tsLooksLikeIntentCreatorExpression(ts, node) {
|
|
589
|
+
if (!node) return false;
|
|
590
|
+
if (ts.isIdentifier(node)) return /^[A-Z]/.test(node.text);
|
|
591
|
+
if (ts.isPropertyAccessExpression(node)) {
|
|
592
|
+
return tsLooksLikeIntentCreatorExpression(ts, node.name);
|
|
1787
593
|
}
|
|
1788
|
-
|
|
1789
|
-
|
|
1790
|
-
|
|
1791
|
-
|
|
1792
|
-
|
|
1793
|
-
|
|
1794
|
-
|
|
1795
|
-
let out = "flowchart TD\n";
|
|
1796
|
-
for (const edge of this.edges) {
|
|
1797
|
-
const label = edge.kind ? `|${edge.kind}|` : "";
|
|
1798
|
-
out += ` ${this.safeId(edge.from)} -->${label} ${this.safeId(edge.to)}
|
|
1799
|
-
`;
|
|
1800
|
-
}
|
|
1801
|
-
const connected = new Set(
|
|
1802
|
-
this.edges.flatMap((e) => [e.from, e.to])
|
|
1803
|
-
);
|
|
1804
|
-
for (const node of this.nodes.keys()) {
|
|
1805
|
-
if (!connected.has(node)) {
|
|
1806
|
-
out += ` ${this.safeId(node)}
|
|
1807
|
-
`;
|
|
1808
|
-
}
|
|
1809
|
-
}
|
|
1810
|
-
return out.trim();
|
|
1811
|
-
}
|
|
1812
|
-
toLayerMermaid(profile) {
|
|
1813
|
-
const nodesByLayer = /* @__PURE__ */ new Map();
|
|
1814
|
-
for (const node of this.nodes.keys()) {
|
|
1815
|
-
const layer = profile.resolveLayer(node) ?? "Unclassified";
|
|
1816
|
-
const current = nodesByLayer.get(layer) ?? [];
|
|
1817
|
-
current.push(node);
|
|
1818
|
-
nodesByLayer.set(layer, current);
|
|
1819
|
-
}
|
|
1820
|
-
let out = "flowchart TD\n";
|
|
1821
|
-
for (const layer of profile.layers) {
|
|
1822
|
-
const nodes = nodesByLayer.get(layer.name) ?? [];
|
|
1823
|
-
if (nodes.length === 0) continue;
|
|
1824
|
-
out += ` subgraph ${this.safeId(layer.name)}[${layer.name}]
|
|
1825
|
-
`;
|
|
1826
|
-
for (const node of nodes) {
|
|
1827
|
-
out += ` ${this.safeId(node)}[${node}]
|
|
1828
|
-
`;
|
|
1829
|
-
}
|
|
1830
|
-
out += " end\n";
|
|
1831
|
-
}
|
|
1832
|
-
const unclassified = nodesByLayer.get("Unclassified") ?? [];
|
|
1833
|
-
if (unclassified.length > 0) {
|
|
1834
|
-
out += " subgraph Unclassified[Unclassified]\n";
|
|
1835
|
-
for (const node of unclassified) {
|
|
1836
|
-
out += ` ${this.safeId(node)}[${node}]
|
|
1837
|
-
`;
|
|
1838
|
-
}
|
|
1839
|
-
out += " end\n";
|
|
1840
|
-
}
|
|
1841
|
-
for (const edge of this.edges) {
|
|
1842
|
-
const label = edge.kind ? `|${edge.kind}|` : "";
|
|
1843
|
-
out += ` ${this.safeId(edge.from)} -->${label} ${this.safeId(edge.to)}
|
|
1844
|
-
`;
|
|
1845
|
-
}
|
|
1846
|
-
return out.trim();
|
|
1847
|
-
}
|
|
1848
|
-
detectViolations(rules = []) {
|
|
1849
|
-
const violations = [];
|
|
1850
|
-
const cycles = this.findSimpleCycles();
|
|
1851
|
-
if (cycles.length > 0) {
|
|
1852
|
-
violations.push(`Cycle detected: ${cycles.map((c) => c.join("->")).join(", ")}`);
|
|
1853
|
-
}
|
|
1854
|
-
for (const rule of rules) {
|
|
1855
|
-
try {
|
|
1856
|
-
const res = rule(this.edges);
|
|
1857
|
-
if (res && res.length) violations.push(...res);
|
|
1858
|
-
} catch (e) {
|
|
1859
|
-
violations.push(`Rule error: ${e.message}`);
|
|
1860
|
-
}
|
|
1861
|
-
}
|
|
1862
|
-
return violations;
|
|
1863
|
-
}
|
|
1864
|
-
ensureNode(id) {
|
|
1865
|
-
if (!this.nodes.has(id)) {
|
|
1866
|
-
this.nodes.set(id, { id });
|
|
1867
|
-
}
|
|
1868
|
-
}
|
|
1869
|
-
addEdge(edge) {
|
|
1870
|
-
const exists = this.edges.some(
|
|
1871
|
-
(e) => e.from === edge.from && e.to === edge.to && e.kind === edge.kind
|
|
1872
|
-
);
|
|
1873
|
-
if (!exists) {
|
|
1874
|
-
this.edges.push(edge);
|
|
1875
|
-
}
|
|
1876
|
-
}
|
|
1877
|
-
// Very naive cycle detection for small graphs
|
|
1878
|
-
findSimpleCycles() {
|
|
1879
|
-
const graph = /* @__PURE__ */ new Map();
|
|
1880
|
-
for (const e of this.edges) {
|
|
1881
|
-
if (!graph.has(e.from)) graph.set(e.from, []);
|
|
1882
|
-
graph.get(e.from).push(e.to);
|
|
1883
|
-
}
|
|
1884
|
-
const cycles = [];
|
|
1885
|
-
const visited = /* @__PURE__ */ new Set();
|
|
1886
|
-
const stack = [];
|
|
1887
|
-
const dfs = (node) => {
|
|
1888
|
-
visited.add(node);
|
|
1889
|
-
stack.push(node);
|
|
1890
|
-
for (const nei of graph.get(node) || []) {
|
|
1891
|
-
if (!visited.has(nei)) {
|
|
1892
|
-
dfs(nei);
|
|
1893
|
-
} else if (stack.includes(nei)) {
|
|
1894
|
-
const cycleStart = stack.indexOf(nei);
|
|
1895
|
-
const cycle = stack.slice(cycleStart).concat(nei);
|
|
1896
|
-
cycles.push(cycle);
|
|
1897
|
-
}
|
|
1898
|
-
}
|
|
1899
|
-
stack.pop();
|
|
1900
|
-
};
|
|
1901
|
-
for (const node of graph.keys()) {
|
|
1902
|
-
if (!visited.has(node)) dfs(node);
|
|
1903
|
-
}
|
|
1904
|
-
const unique = new Set(cycles.map((c) => c.join("->")));
|
|
1905
|
-
return Array.from(unique).map((s) => s.split("->"));
|
|
1906
|
-
}
|
|
1907
|
-
safeId(id) {
|
|
1908
|
-
return id.replace(/[^a-zA-Z0-9_]/g, "_");
|
|
1909
|
-
}
|
|
1910
|
-
};
|
|
1911
|
-
function createDependencyGraph() {
|
|
1912
|
-
return new DependencyGraphImpl();
|
|
1913
|
-
}
|
|
1914
|
-
|
|
1915
|
-
// src/kernel/graph/sync.ts
|
|
1916
|
-
function syncRegistryToGraph(registry, graph, options = {}) {
|
|
1917
|
-
const requireRegistered = options.requireRegisteredTargets ?? false;
|
|
1918
|
-
for (const rel of registry.getAllRelationships()) {
|
|
1919
|
-
if (requireRegistered && !registry.has(rel.to)) {
|
|
1920
|
-
continue;
|
|
1921
|
-
}
|
|
1922
|
-
if (rel.kind === "dependsOn") {
|
|
1923
|
-
graph.registerDependency(rel.from, rel.to, "declared");
|
|
1924
|
-
} else {
|
|
1925
|
-
graph.registerDependency(rel.from, rel.to, "produces");
|
|
1926
|
-
}
|
|
1927
|
-
}
|
|
1928
|
-
}
|
|
1929
|
-
|
|
1930
|
-
// src/domain/configContract.ts
|
|
1931
|
-
var ARK_CONFIG_SCHEMA_VERSION = "1.0";
|
|
1932
|
-
var ARK_CONFIG_SCHEMA_URL = "https://unpkg.com/arkgate@2/schemas/ark.config.schema.json";
|
|
1933
|
-
var DEFAULT_LAYER_NAMES = [
|
|
1934
|
-
"DomainModel",
|
|
1935
|
-
"ApplicationOrchestration",
|
|
1936
|
-
"PersistenceAdapters",
|
|
1937
|
-
"IntegrationAdapters",
|
|
1938
|
-
"WorkflowSagaEngine",
|
|
1939
|
-
"BackgroundJobsScheduling",
|
|
1940
|
-
"PresentationAdapters",
|
|
1941
|
-
"ReportingReadModels",
|
|
1942
|
-
"ExtensibilityMetadata",
|
|
1943
|
-
"SecurityAuditObservability",
|
|
1944
|
-
"Kernel"
|
|
1945
|
-
];
|
|
1946
|
-
var DEFAULT_ALLOWED_FLOWS = /* @__PURE__ */ new Set([
|
|
1947
|
-
"PresentationAdapters->ApplicationOrchestration",
|
|
1948
|
-
"ApplicationOrchestration->DomainModel",
|
|
1949
|
-
"WorkflowSagaEngine->ApplicationOrchestration",
|
|
1950
|
-
"WorkflowSagaEngine->DomainModel",
|
|
1951
|
-
"BackgroundJobsScheduling->ApplicationOrchestration"
|
|
1952
|
-
]);
|
|
1953
|
-
function createDefaultRules() {
|
|
1954
|
-
const rules = [];
|
|
1955
|
-
for (const from of DEFAULT_LAYER_NAMES) {
|
|
1956
|
-
for (const to of DEFAULT_LAYER_NAMES) {
|
|
1957
|
-
if (from === to || DEFAULT_ALLOWED_FLOWS.has(`${from}->${to}`)) continue;
|
|
1958
|
-
rules.push({ from, to, allowed: false });
|
|
1959
|
-
}
|
|
1960
|
-
}
|
|
1961
|
-
return rules;
|
|
1962
|
-
}
|
|
1963
|
-
var DEFAULT_ARK_CONFIG_RULES = createDefaultRules();
|
|
1964
|
-
var stringArraySchema = {
|
|
1965
|
-
type: "array",
|
|
1966
|
-
items: { type: "string", minLength: 1 },
|
|
1967
|
-
uniqueItems: true
|
|
1968
|
-
};
|
|
1969
|
-
var ARK_CONFIG_SCHEMA = {
|
|
1970
|
-
$schema: "https://json-schema.org/draft/2020-12/schema",
|
|
1971
|
-
$id: ARK_CONFIG_SCHEMA_URL,
|
|
1972
|
-
title: "ArkGate architecture contract",
|
|
1973
|
-
description: "Versioned contract consumed identically by ArkGate CLI, MCP, and ESLint surfaces.",
|
|
1974
|
-
type: "object",
|
|
1975
|
-
additionalProperties: false,
|
|
1976
|
-
required: ["$schema", "schemaVersion", "include", "layers", "rules"],
|
|
1977
|
-
properties: {
|
|
1978
|
-
$schema: {
|
|
1979
|
-
type: "string",
|
|
1980
|
-
minLength: 1,
|
|
1981
|
-
default: ARK_CONFIG_SCHEMA_URL,
|
|
1982
|
-
description: "Editor-facing URL or local path for this JSON Schema."
|
|
1983
|
-
},
|
|
1984
|
-
schemaVersion: {
|
|
1985
|
-
type: "string",
|
|
1986
|
-
const: ARK_CONFIG_SCHEMA_VERSION,
|
|
1987
|
-
default: ARK_CONFIG_SCHEMA_VERSION
|
|
1988
|
-
},
|
|
1989
|
-
name: { type: "string", minLength: 1 },
|
|
1990
|
-
include: { ...stringArraySchema, minItems: 1, default: ["src"] },
|
|
1991
|
-
exclude: { ...stringArraySchema, default: [] },
|
|
1992
|
-
excludeGenerated: { type: "boolean", default: true },
|
|
1993
|
-
frameworkOverlay: { type: "string", minLength: 1 },
|
|
1994
|
-
layers: {
|
|
1995
|
-
type: "array",
|
|
1996
|
-
default: [],
|
|
1997
|
-
items: { $ref: "#/$defs/layer" }
|
|
1998
|
-
},
|
|
1999
|
-
rules: {
|
|
2000
|
-
type: "array",
|
|
2001
|
-
default: DEFAULT_ARK_CONFIG_RULES,
|
|
2002
|
-
items: { $ref: "#/$defs/rule" }
|
|
2003
|
-
},
|
|
2004
|
-
cyclePolicy: {
|
|
2005
|
-
type: "string",
|
|
2006
|
-
enum: ["strict", "soft", "framework-soft", "off"],
|
|
2007
|
-
default: "strict"
|
|
2008
|
-
},
|
|
2009
|
-
dynamicImportAllowlist: { ...stringArraySchema, default: [] },
|
|
2010
|
-
safety: {
|
|
2011
|
-
$ref: "#/$defs/safety",
|
|
2012
|
-
default: {
|
|
2013
|
-
maxTsSuppressions: 0,
|
|
2014
|
-
maxAnyCasts: 0,
|
|
2015
|
-
allowInMemory: false,
|
|
2016
|
-
allowDisabledPeerIsolation: false
|
|
2017
|
-
}
|
|
2018
|
-
}
|
|
2019
|
-
},
|
|
2020
|
-
$defs: {
|
|
2021
|
-
layer: {
|
|
2022
|
-
type: "object",
|
|
2023
|
-
additionalProperties: false,
|
|
2024
|
-
required: ["name", "patterns"],
|
|
2025
|
-
properties: {
|
|
2026
|
-
name: { type: "string", minLength: 1 },
|
|
2027
|
-
patterns: { ...stringArraySchema, minItems: 1 },
|
|
2028
|
-
exclude: stringArraySchema,
|
|
2029
|
-
intentPrefixes: stringArraySchema,
|
|
2030
|
-
description: { type: "string", minLength: 1 },
|
|
2031
|
-
forbiddenGlobals: stringArraySchema,
|
|
2032
|
-
mayImportInfrastructure: { type: "boolean" },
|
|
2033
|
-
optional: { type: "boolean" }
|
|
2034
|
-
}
|
|
2035
|
-
},
|
|
2036
|
-
rule: {
|
|
2037
|
-
type: "object",
|
|
2038
|
-
additionalProperties: false,
|
|
2039
|
-
required: ["from", "to", "allowed"],
|
|
2040
|
-
properties: {
|
|
2041
|
-
from: { type: "string", minLength: 1 },
|
|
2042
|
-
to: { type: "string", minLength: 1 },
|
|
2043
|
-
allowed: { type: "boolean" },
|
|
2044
|
-
message: { type: "string", minLength: 1 },
|
|
2045
|
-
peerIsolation: { type: "boolean" },
|
|
2046
|
-
sliceFolders: { ...stringArraySchema, minItems: 1 }
|
|
2047
|
-
}
|
|
2048
|
-
},
|
|
2049
|
-
safety: {
|
|
2050
|
-
type: "object",
|
|
2051
|
-
additionalProperties: false,
|
|
2052
|
-
properties: {
|
|
2053
|
-
maxTsSuppressions: { type: "integer", minimum: 0, default: 0 },
|
|
2054
|
-
maxAnyCasts: { type: "integer", minimum: 0, default: 0 },
|
|
2055
|
-
allowInMemory: { type: "boolean", default: false },
|
|
2056
|
-
allowDisabledPeerIsolation: { type: "boolean", default: false }
|
|
2057
|
-
}
|
|
2058
|
-
}
|
|
2059
|
-
}
|
|
2060
|
-
};
|
|
2061
|
-
var ArkConfigValidationError = class extends Error {
|
|
2062
|
-
issues;
|
|
2063
|
-
source;
|
|
2064
|
-
constructor(source, issues) {
|
|
2065
|
-
super(
|
|
2066
|
-
`Invalid ArkGate config (${source}):
|
|
2067
|
-
${issues.map((issue) => `- ${issue.path}: ${issue.message}`).join("\n")}`
|
|
2068
|
-
);
|
|
2069
|
-
this.name = "ArkConfigValidationError";
|
|
2070
|
-
this.source = source;
|
|
2071
|
-
this.issues = issues;
|
|
2072
|
-
}
|
|
2073
|
-
};
|
|
2074
|
-
function isObject(value) {
|
|
2075
|
-
return value !== null && typeof value === "object" && !Array.isArray(value);
|
|
2076
|
-
}
|
|
2077
|
-
function propertyPath(parent, key) {
|
|
2078
|
-
return /^[A-Za-z_$][A-Za-z0-9_$]*$/.test(key) ? `${parent}.${key}` : `${parent}[${JSON.stringify(key)}]`;
|
|
2079
|
-
}
|
|
2080
|
-
function valueType(value) {
|
|
2081
|
-
if (value === null) return "null";
|
|
2082
|
-
if (Array.isArray(value)) return "array";
|
|
2083
|
-
return typeof value;
|
|
2084
|
-
}
|
|
2085
|
-
function resolveSchemaRef(ref, root) {
|
|
2086
|
-
const prefix = "#/$defs/";
|
|
2087
|
-
if (!ref.startsWith(prefix)) return void 0;
|
|
2088
|
-
return root.$defs[ref.slice(prefix.length)];
|
|
2089
|
-
}
|
|
2090
|
-
function validateNode(value, schema, path, root, issues) {
|
|
2091
|
-
if (schema.$ref) {
|
|
2092
|
-
const referenced = resolveSchemaRef(schema.$ref, root);
|
|
2093
|
-
if (!referenced) {
|
|
2094
|
-
issues.push({ path, message: `schema reference ${schema.$ref} cannot be resolved` });
|
|
2095
|
-
return;
|
|
2096
|
-
}
|
|
2097
|
-
validateNode(value, referenced, path, root, issues);
|
|
2098
|
-
return;
|
|
2099
|
-
}
|
|
2100
|
-
if (schema.const !== void 0 && !Object.is(value, schema.const)) {
|
|
2101
|
-
issues.push({ path, message: `must equal ${JSON.stringify(schema.const)}` });
|
|
2102
|
-
return;
|
|
2103
|
-
}
|
|
2104
|
-
if (schema.enum && !schema.enum.some((candidate) => Object.is(candidate, value))) {
|
|
2105
|
-
issues.push({ path, message: `must be one of ${schema.enum.map(String).join(", ")}` });
|
|
2106
|
-
return;
|
|
2107
|
-
}
|
|
2108
|
-
if (schema.type === "object") {
|
|
2109
|
-
if (!isObject(value)) {
|
|
2110
|
-
issues.push({ path, message: `must be an object; received ${valueType(value)}` });
|
|
2111
|
-
return;
|
|
2112
|
-
}
|
|
2113
|
-
const properties = schema.properties ?? {};
|
|
2114
|
-
for (const key of schema.required ?? []) {
|
|
2115
|
-
if (value[key] === void 0) {
|
|
2116
|
-
issues.push({ path: propertyPath(path, key), message: "is required" });
|
|
2117
|
-
}
|
|
2118
|
-
}
|
|
2119
|
-
if (schema.additionalProperties === false) {
|
|
2120
|
-
for (const key of Object.keys(value)) {
|
|
2121
|
-
if (!(key in properties)) {
|
|
2122
|
-
issues.push({ path: propertyPath(path, key), message: "unknown field" });
|
|
2123
|
-
}
|
|
2124
|
-
}
|
|
2125
|
-
}
|
|
2126
|
-
for (const [key, childSchema] of Object.entries(properties)) {
|
|
2127
|
-
if (value[key] !== void 0) {
|
|
2128
|
-
validateNode(value[key], childSchema, propertyPath(path, key), root, issues);
|
|
2129
|
-
}
|
|
2130
|
-
}
|
|
2131
|
-
return;
|
|
2132
|
-
}
|
|
2133
|
-
if (schema.type === "array") {
|
|
2134
|
-
if (!Array.isArray(value)) {
|
|
2135
|
-
issues.push({ path, message: `must be an array; received ${valueType(value)}` });
|
|
2136
|
-
return;
|
|
2137
|
-
}
|
|
2138
|
-
if (schema.minItems !== void 0 && value.length < schema.minItems) {
|
|
2139
|
-
issues.push({ path, message: `must contain at least ${schema.minItems} item(s)` });
|
|
2140
|
-
}
|
|
2141
|
-
if (schema.uniqueItems) {
|
|
2142
|
-
const serialized = value.map((entry) => JSON.stringify(entry));
|
|
2143
|
-
if (new Set(serialized).size !== serialized.length) {
|
|
2144
|
-
issues.push({ path, message: "must not contain duplicate items" });
|
|
2145
|
-
}
|
|
2146
|
-
}
|
|
2147
|
-
if (schema.items) {
|
|
2148
|
-
value.forEach(
|
|
2149
|
-
(entry, index) => validateNode(entry, schema.items, `${path}[${index}]`, root, issues)
|
|
2150
|
-
);
|
|
2151
|
-
}
|
|
2152
|
-
return;
|
|
2153
|
-
}
|
|
2154
|
-
if (schema.type === "string") {
|
|
2155
|
-
if (typeof value !== "string") {
|
|
2156
|
-
issues.push({ path, message: `must be a string; received ${valueType(value)}` });
|
|
2157
|
-
return;
|
|
2158
|
-
}
|
|
2159
|
-
if (schema.minLength !== void 0 && value.length < schema.minLength) {
|
|
2160
|
-
issues.push({ path, message: `must contain at least ${schema.minLength} character(s)` });
|
|
2161
|
-
}
|
|
2162
|
-
return;
|
|
2163
|
-
}
|
|
2164
|
-
if (schema.type === "boolean") {
|
|
2165
|
-
if (typeof value !== "boolean") {
|
|
2166
|
-
issues.push({ path, message: `must be a boolean; received ${valueType(value)}` });
|
|
2167
|
-
}
|
|
2168
|
-
return;
|
|
2169
|
-
}
|
|
2170
|
-
if (schema.type === "integer") {
|
|
2171
|
-
if (!Number.isInteger(value)) {
|
|
2172
|
-
issues.push({ path, message: `must be an integer; received ${valueType(value)}` });
|
|
2173
|
-
return;
|
|
2174
|
-
}
|
|
2175
|
-
if (schema.minimum !== void 0 && value < schema.minimum) {
|
|
2176
|
-
issues.push({ path, message: `must be at least ${schema.minimum}` });
|
|
2177
|
-
}
|
|
2178
|
-
}
|
|
2179
|
-
}
|
|
2180
|
-
function defaultedConfig(input) {
|
|
2181
|
-
return {
|
|
2182
|
-
...input,
|
|
2183
|
-
$schema: input.$schema === void 0 ? ARK_CONFIG_SCHEMA_URL : input.$schema,
|
|
2184
|
-
schemaVersion: input.schemaVersion === void 0 ? ARK_CONFIG_SCHEMA_VERSION : input.schemaVersion,
|
|
2185
|
-
include: input.include === void 0 ? ["src"] : input.include,
|
|
2186
|
-
layers: input.layers === void 0 ? [] : input.layers,
|
|
2187
|
-
rules: input.rules === void 0 ? DEFAULT_ARK_CONFIG_RULES.map((rule) => ({ ...rule })) : input.rules
|
|
2188
|
-
};
|
|
2189
|
-
}
|
|
2190
|
-
function migrateArkConfig(input, source = "ark.config.json") {
|
|
2191
|
-
if (!isObject(input)) {
|
|
2192
|
-
throw new ArkConfigValidationError(source, [
|
|
2193
|
-
{ path: "$", message: `must be an object; received ${valueType(input)}` }
|
|
2194
|
-
]);
|
|
2195
|
-
}
|
|
2196
|
-
const migratedFrom = input.schemaVersion === void 0 ? "unversioned" : null;
|
|
2197
|
-
if (input.schemaVersion !== void 0 && input.schemaVersion !== ARK_CONFIG_SCHEMA_VERSION) {
|
|
2198
|
-
throw new ArkConfigValidationError(source, [
|
|
2199
|
-
{
|
|
2200
|
-
path: "$.schemaVersion",
|
|
2201
|
-
message: `unsupported version ${JSON.stringify(input.schemaVersion)}; expected ${ARK_CONFIG_SCHEMA_VERSION}`
|
|
2202
|
-
}
|
|
2203
|
-
]);
|
|
2204
|
-
}
|
|
2205
|
-
return { candidate: defaultedConfig(input), migratedFrom };
|
|
2206
|
-
}
|
|
2207
|
-
function loadArkConfigContract(input, source = "ark.config.json") {
|
|
2208
|
-
const { candidate, migratedFrom } = migrateArkConfig(input, source);
|
|
2209
|
-
const issues = [];
|
|
2210
|
-
validateNode(
|
|
2211
|
-
candidate,
|
|
2212
|
-
ARK_CONFIG_SCHEMA,
|
|
2213
|
-
"$",
|
|
2214
|
-
ARK_CONFIG_SCHEMA,
|
|
2215
|
-
issues
|
|
2216
|
-
);
|
|
2217
|
-
if (issues.length > 0) throw new ArkConfigValidationError(source, issues);
|
|
2218
|
-
return { config: candidate, migratedFrom };
|
|
2219
|
-
}
|
|
2220
|
-
function parseArkConfigJson(json, source = "ark.config.json") {
|
|
2221
|
-
let input;
|
|
2222
|
-
try {
|
|
2223
|
-
input = JSON.parse(json);
|
|
2224
|
-
} catch (error) {
|
|
2225
|
-
throw new ArkConfigValidationError(source, [
|
|
2226
|
-
{
|
|
2227
|
-
path: "$",
|
|
2228
|
-
message: `invalid JSON: ${error instanceof Error ? error.message : String(error)}`
|
|
2229
|
-
}
|
|
2230
|
-
]);
|
|
2231
|
-
}
|
|
2232
|
-
return loadArkConfigContract(input, source);
|
|
2233
|
-
}
|
|
2234
|
-
function withArkConfigMetadata(config) {
|
|
2235
|
-
const result = {
|
|
2236
|
-
$schema: typeof config.$schema === "string" && config.$schema.length > 0 ? config.$schema : ARK_CONFIG_SCHEMA_URL,
|
|
2237
|
-
schemaVersion: ARK_CONFIG_SCHEMA_VERSION
|
|
2238
|
-
};
|
|
2239
|
-
for (const [key, value] of Object.entries(config)) {
|
|
2240
|
-
if (key !== "$schema" && key !== "schemaVersion") result[key] = value;
|
|
2241
|
-
}
|
|
2242
|
-
return result;
|
|
2243
|
-
}
|
|
2244
|
-
|
|
2245
|
-
// src/kernel/layers/ArchitectureProfile.ts
|
|
2246
|
-
function normalizePrefix(prefix) {
|
|
2247
|
-
return prefix.endsWith(".") ? prefix : `${prefix}.`;
|
|
2248
|
-
}
|
|
2249
|
-
function byLongestPrefix(a, b) {
|
|
2250
|
-
const maxA = a.prefixes.length ? Math.max(...a.prefixes.map((p) => p.length)) : 0;
|
|
2251
|
-
const maxB = b.prefixes.length ? Math.max(...b.prefixes.map((p) => p.length)) : 0;
|
|
2252
|
-
return maxB - maxA;
|
|
2253
|
-
}
|
|
2254
|
-
function createArchitectureProfile(options) {
|
|
2255
|
-
const layers = options.layers.map((layer) => ({
|
|
2256
|
-
...layer,
|
|
2257
|
-
prefixes: layer.prefixes.map(normalizePrefix)
|
|
2258
|
-
}));
|
|
2259
|
-
const sortedLayers = [...layers].sort(byLongestPrefix);
|
|
2260
|
-
const rules = [...options.rules ?? []];
|
|
2261
|
-
return {
|
|
2262
|
-
name: options.name,
|
|
2263
|
-
layers,
|
|
2264
|
-
rules,
|
|
2265
|
-
resolveLayer(name) {
|
|
2266
|
-
return layers.find((layer) => layer.match?.(name))?.name ?? sortedLayers.find(
|
|
2267
|
-
(layer) => layer.prefixes.some((prefix) => name.startsWith(prefix))
|
|
2268
|
-
)?.name;
|
|
2269
|
-
}
|
|
2270
|
-
};
|
|
2271
|
-
}
|
|
2272
|
-
function createArchitectureProfileFromArkConfig(config, options = {}) {
|
|
2273
|
-
return createArchitectureProfile({
|
|
2274
|
-
name: options.name ?? config.name ?? "ark.config.json",
|
|
2275
|
-
layers: config.layers.map((layer, index) => ({
|
|
2276
|
-
name: layer.name,
|
|
2277
|
-
prefixes: layer.intentPrefixes ?? [],
|
|
2278
|
-
description: layer.description,
|
|
2279
|
-
order: index + 1
|
|
2280
|
-
})),
|
|
2281
|
-
rules: config.rules ?? []
|
|
2282
|
-
});
|
|
2283
|
-
}
|
|
2284
|
-
var elevenLayerProfileLayers = [
|
|
2285
|
-
{
|
|
2286
|
-
name: "DomainModel",
|
|
2287
|
-
prefixes: ["Domain"],
|
|
2288
|
-
description: "Rich domain model, business rules, and domain events.",
|
|
2289
|
-
order: 1
|
|
2290
|
-
},
|
|
2291
|
-
{
|
|
2292
|
-
name: "ApplicationOrchestration",
|
|
2293
|
-
prefixes: ["Application"],
|
|
2294
|
-
description: "Use cases and command orchestration.",
|
|
2295
|
-
order: 2
|
|
2296
|
-
},
|
|
2297
|
-
{
|
|
2298
|
-
name: "PersistenceAdapters",
|
|
2299
|
-
prefixes: ["Adapter.Persistence", "Adapter.Repository"],
|
|
2300
|
-
description: "Database, repository, and storage adapters.",
|
|
2301
|
-
order: 3
|
|
2302
|
-
},
|
|
2303
|
-
{
|
|
2304
|
-
name: "IntegrationAdapters",
|
|
2305
|
-
prefixes: ["Adapter.Integration", "Adapter.External"],
|
|
2306
|
-
description: "External systems, APIs, and integration adapters.",
|
|
2307
|
-
order: 4
|
|
2308
|
-
},
|
|
2309
|
-
{
|
|
2310
|
-
name: "WorkflowSagaEngine",
|
|
2311
|
-
prefixes: ["Workflow"],
|
|
2312
|
-
description: "Sagas, workflows, and long-running processes.",
|
|
2313
|
-
order: 5
|
|
2314
|
-
},
|
|
2315
|
-
{
|
|
2316
|
-
name: "BackgroundJobsScheduling",
|
|
2317
|
-
prefixes: ["Job"],
|
|
2318
|
-
description: "Background jobs, scheduled work, and async processors.",
|
|
2319
|
-
order: 6
|
|
2320
|
-
},
|
|
2321
|
-
{
|
|
2322
|
-
name: "PresentationAdapters",
|
|
2323
|
-
prefixes: ["Presentation", "Adapter.Presentation", "Adapter.Api"],
|
|
2324
|
-
description: "API, UI, controller, and presentation adapters.",
|
|
2325
|
-
order: 7
|
|
2326
|
-
},
|
|
2327
|
-
{
|
|
2328
|
-
name: "ReportingReadModels",
|
|
2329
|
-
prefixes: ["Reporting"],
|
|
2330
|
-
description: "Read models, projections, and reporting surfaces.",
|
|
2331
|
-
order: 8
|
|
2332
|
-
},
|
|
2333
|
-
{
|
|
2334
|
-
name: "ExtensibilityMetadata",
|
|
2335
|
-
prefixes: ["Metadata"],
|
|
2336
|
-
description: "Metadata, extensions, and schema contracts.",
|
|
2337
|
-
order: 9
|
|
2338
|
-
},
|
|
2339
|
-
{
|
|
2340
|
-
name: "SecurityAuditObservability",
|
|
2341
|
-
prefixes: ["Security", "Audit", "Observability"],
|
|
2342
|
-
description: "Security, audit, and observability concerns.",
|
|
2343
|
-
order: 10
|
|
2344
|
-
},
|
|
2345
|
-
{
|
|
2346
|
-
name: "Kernel",
|
|
2347
|
-
prefixes: ["Kernel"],
|
|
2348
|
-
description: "Ark-owned governance and kernel signals.",
|
|
2349
|
-
order: 11
|
|
2350
|
-
}
|
|
2351
|
-
];
|
|
2352
|
-
var elevenLayerProfile = createArchitectureProfile({
|
|
2353
|
-
name: "Ark 11-layer Hexagonal Event-Driven Profile",
|
|
2354
|
-
layers: elevenLayerProfileLayers,
|
|
2355
|
-
rules: DEFAULT_ARK_CONFIG_RULES.map((rule) => ({ ...rule }))
|
|
2356
|
-
});
|
|
2357
|
-
var defaultElevenLayerDirectories = {
|
|
2358
|
-
DomainModel: ["domain"],
|
|
2359
|
-
ApplicationOrchestration: ["application", "app"],
|
|
2360
|
-
PersistenceAdapters: [
|
|
2361
|
-
"adapters/persistence",
|
|
2362
|
-
"adapters/repository",
|
|
2363
|
-
"repositories",
|
|
2364
|
-
"infra/persistence"
|
|
2365
|
-
],
|
|
2366
|
-
IntegrationAdapters: ["adapters/integration", "adapters/external", "integrations"],
|
|
2367
|
-
WorkflowSagaEngine: ["workflows", "sagas"],
|
|
2368
|
-
BackgroundJobsScheduling: ["jobs", "schedules"],
|
|
2369
|
-
PresentationAdapters: ["presentation", "adapters/presentation", "adapters/api"],
|
|
2370
|
-
ReportingReadModels: ["reporting", "read-models", "projections"],
|
|
2371
|
-
ExtensibilityMetadata: ["metadata", "extensions"],
|
|
2372
|
-
SecurityAuditObservability: ["security", "audit", "observability"],
|
|
2373
|
-
Kernel: ["kernel"]
|
|
2374
|
-
};
|
|
2375
|
-
function createElevenLayerArkConfig(options = {}) {
|
|
2376
|
-
const rootDir = options.rootDir ?? "src";
|
|
2377
|
-
const optional = options.optionalLayers ?? true;
|
|
2378
|
-
const prefix = rootDir === "." ? "" : `${rootDir}/`;
|
|
2379
|
-
return withArkConfigMetadata({
|
|
2380
|
-
include: options.include ?? [rootDir],
|
|
2381
|
-
layers: elevenLayerProfile.layers.map((layer) => ({
|
|
2382
|
-
name: layer.name,
|
|
2383
|
-
patterns: (defaultElevenLayerDirectories[layer.name] ?? [layer.name]).map(
|
|
2384
|
-
(directory) => `${prefix}${directory}/**`
|
|
2385
|
-
),
|
|
2386
|
-
intentPrefixes: layer.prefixes,
|
|
2387
|
-
optional
|
|
2388
|
-
})),
|
|
2389
|
-
rules: [...elevenLayerProfile.rules]
|
|
2390
|
-
});
|
|
2391
|
-
}
|
|
2392
|
-
|
|
2393
|
-
// src/kernel/metadata/MetadataRegistry.ts
|
|
2394
|
-
var MetadataRegistryImpl = class {
|
|
2395
|
-
entities = /* @__PURE__ */ new Map();
|
|
2396
|
-
entity(name, meta, options = {}) {
|
|
2397
|
-
if (this.entities.has(name) && !options.allowOverwrite) {
|
|
2398
|
-
throw new Error(`Entity metadata "${name}" is already registered.`);
|
|
2399
|
-
}
|
|
2400
|
-
const full = { name, fields: {}, ...meta };
|
|
2401
|
-
this.entities.set(name, full);
|
|
2402
|
-
return full;
|
|
2403
|
-
}
|
|
2404
|
-
getEntity(name) {
|
|
2405
|
-
return this.entities.get(name);
|
|
2406
|
-
}
|
|
2407
|
-
listEntities() {
|
|
2408
|
-
return Array.from(this.entities.values());
|
|
2409
|
-
}
|
|
2410
|
-
findEntitiesByIntent(intentName) {
|
|
2411
|
-
return this.listEntities().filter(
|
|
2412
|
-
(entity) => entity.emits?.includes(intentName) || entity.consumes?.includes(intentName)
|
|
2413
|
-
);
|
|
2414
|
-
}
|
|
2415
|
-
validate() {
|
|
2416
|
-
const issues = [];
|
|
2417
|
-
for (const entity of this.entities.values()) {
|
|
2418
|
-
if (!entity.name.trim()) {
|
|
2419
|
-
issues.push({ entity: entity.name, message: "Entity name is required." });
|
|
2420
|
-
}
|
|
2421
|
-
for (const [fieldName, field] of Object.entries(entity.fields)) {
|
|
2422
|
-
if (!fieldName.trim()) {
|
|
2423
|
-
issues.push({ entity: entity.name, field: fieldName, message: "Field name is required." });
|
|
2424
|
-
}
|
|
2425
|
-
if (!field.type || typeof field.type !== "string") {
|
|
2426
|
-
issues.push({
|
|
2427
|
-
entity: entity.name,
|
|
2428
|
-
field: fieldName,
|
|
2429
|
-
message: "Field type must be a non-empty string."
|
|
2430
|
-
});
|
|
2431
|
-
}
|
|
2432
|
-
if (field.relation && !this.entities.has(field.relation.entity)) {
|
|
2433
|
-
issues.push({
|
|
2434
|
-
entity: entity.name,
|
|
2435
|
-
field: fieldName,
|
|
2436
|
-
message: `Related entity "${field.relation.entity}" is not registered.`
|
|
2437
|
-
});
|
|
2438
|
-
}
|
|
2439
|
-
}
|
|
2440
|
-
}
|
|
2441
|
-
return { ok: issues.length === 0, issues };
|
|
2442
|
-
}
|
|
2443
|
-
toJSON() {
|
|
2444
|
-
return this.listEntities();
|
|
2445
|
-
}
|
|
2446
|
-
};
|
|
2447
|
-
function createMetadataRegistry() {
|
|
2448
|
-
return new MetadataRegistryImpl();
|
|
2449
|
-
}
|
|
2450
|
-
|
|
2451
|
-
// src/kernel/adapters/ports.ts
|
|
2452
|
-
function definePort(name, options = {}) {
|
|
2453
|
-
return {
|
|
2454
|
-
name,
|
|
2455
|
-
ownerLayer: options.ownerLayer,
|
|
2456
|
-
intent: options.intent,
|
|
2457
|
-
allowedAdapters: options.allowedAdapters ? [...options.allowedAdapters] : void 0
|
|
2458
|
-
};
|
|
2459
|
-
}
|
|
2460
|
-
function createAdapter(port, impl, requiredKeysOrOptions, adapterOptions = {}) {
|
|
2461
|
-
const options = Array.isArray(requiredKeysOrOptions) ? { ...adapterOptions, requiredKeys: requiredKeysOrOptions } : { ...requiredKeysOrOptions ?? {} };
|
|
2462
|
-
const requiredKeys = options.requiredKeys;
|
|
2463
|
-
if (requiredKeys && requiredKeys.length > 0) {
|
|
2464
|
-
const missing = requiredKeys.filter((k) => !hasMember(impl, k));
|
|
2465
|
-
if (missing.length > 0) {
|
|
2466
|
-
throw new Error(
|
|
2467
|
-
`Adapter for port "${port.name}" is missing required members: ${missing.join(", ")}`
|
|
2468
|
-
);
|
|
2469
|
-
}
|
|
2470
|
-
}
|
|
2471
|
-
const adapter = {
|
|
2472
|
-
name: options.name,
|
|
2473
|
-
layer: options.layer,
|
|
2474
|
-
intent: options.intent,
|
|
2475
|
-
port,
|
|
2476
|
-
impl
|
|
2477
|
-
};
|
|
2478
|
-
const governance = checkAdapterGovernance(adapter);
|
|
2479
|
-
if (!governance.ok) {
|
|
2480
|
-
throw new Error(governance.issues.map((issue) => issue.message).join("\n"));
|
|
2481
|
-
}
|
|
2482
|
-
return adapter;
|
|
2483
|
-
}
|
|
2484
|
-
function checkContract(impl, requiredKeys = []) {
|
|
2485
|
-
const missing = requiredKeys.filter((k) => !hasMember(impl, k));
|
|
2486
|
-
return missing.length === 0 ? { ok: true } : { ok: false, missing };
|
|
2487
|
-
}
|
|
2488
|
-
function checkAdapterGovernance(adapter) {
|
|
2489
|
-
const allowed = adapter.port.allowedAdapters ?? [];
|
|
2490
|
-
if (allowed.length === 0) {
|
|
2491
|
-
return { ok: true, issues: [] };
|
|
2492
|
-
}
|
|
2493
|
-
const adapterNames = [adapter.name, adapter.intent].filter(
|
|
2494
|
-
(value) => typeof value === "string" && value.length > 0
|
|
2495
|
-
);
|
|
2496
|
-
const matched = adapterNames.some((name) => allowed.includes(name));
|
|
2497
|
-
if (matched) {
|
|
2498
|
-
return { ok: true, issues: [] };
|
|
2499
|
-
}
|
|
2500
|
-
return {
|
|
2501
|
-
ok: false,
|
|
2502
|
-
issues: [
|
|
2503
|
-
{
|
|
2504
|
-
ruleId: "ADAPTER_NOT_ALLOWED_FOR_PORT",
|
|
2505
|
-
port: adapter.port.name,
|
|
2506
|
-
adapter: adapter.name ?? adapter.intent,
|
|
2507
|
-
message: `Adapter "${adapter.name ?? adapter.intent ?? "unknown"}" is not allowed for port "${adapter.port.name}".`
|
|
2508
|
-
}
|
|
2509
|
-
]
|
|
2510
|
-
};
|
|
2511
|
-
}
|
|
2512
|
-
function hasMember(value, key) {
|
|
2513
|
-
return value != null && (typeof value === "object" || typeof value === "function") && key in value;
|
|
2514
|
-
}
|
|
2515
|
-
|
|
2516
|
-
// src/kernel/ai-gate/AICodeGate.ts
|
|
2517
|
-
function violation(ruleId, message, extra) {
|
|
2518
|
-
return { ruleId, code: ruleId, message, ...extra };
|
|
2519
|
-
}
|
|
2520
|
-
function lineOf(source, index) {
|
|
2521
|
-
return source.slice(0, index).split("\n").length;
|
|
2522
|
-
}
|
|
2523
|
-
function extractQuotedStrings(source) {
|
|
2524
|
-
const matches = [];
|
|
2525
|
-
const re = /['"`]([A-Za-z][A-Za-z0-9_.]*)['"`]/g;
|
|
2526
|
-
let m;
|
|
2527
|
-
while ((m = re.exec(source)) !== null) {
|
|
2528
|
-
matches.push({ value: m[1], index: m.index });
|
|
2529
|
-
}
|
|
2530
|
-
return matches;
|
|
2531
|
-
}
|
|
2532
|
-
function extractModuleSpecifiers(source) {
|
|
2533
|
-
const matches = [];
|
|
2534
|
-
const patterns = [
|
|
2535
|
-
{
|
|
2536
|
-
kind: "import",
|
|
2537
|
-
re: /\bimport\s+(?:type\s+)?(?:[^'"]*?\s+from\s*)?['"]([^'"]+)['"]/g
|
|
2538
|
-
},
|
|
2539
|
-
{
|
|
2540
|
-
kind: "export",
|
|
2541
|
-
re: /\bexport\s+(?:type\s+)?[^'"]*?\s+from\s*['"]([^'"]+)['"]/g
|
|
2542
|
-
},
|
|
2543
|
-
{
|
|
2544
|
-
kind: "dynamic-import",
|
|
2545
|
-
re: /\bimport\s*\(\s*['"]([^'"]+)['"]\s*\)/g
|
|
2546
|
-
},
|
|
2547
|
-
{
|
|
2548
|
-
kind: "require",
|
|
2549
|
-
re: /\brequire\s*\(\s*['"]([^'"]+)['"]\s*\)/g
|
|
2550
|
-
}
|
|
2551
|
-
];
|
|
2552
|
-
for (const pattern of patterns) {
|
|
2553
|
-
let match;
|
|
2554
|
-
while ((match = pattern.re.exec(source)) !== null) {
|
|
2555
|
-
const index = match.index + match[0].indexOf(match[1]);
|
|
2556
|
-
const raw = match[0];
|
|
2557
|
-
const typeOnly = pattern.kind === "import" && /\bimport\s+type\b/.test(raw) || pattern.kind === "export" && /\bexport\s+type\b/.test(raw);
|
|
2558
|
-
matches.push({ value: match[1], index, kind: pattern.kind, typeOnly });
|
|
2559
|
-
}
|
|
2560
|
-
}
|
|
2561
|
-
return matches.sort((a, b) => a.index - b.index);
|
|
2562
|
-
}
|
|
2563
|
-
function extractModuleSpecifiersAst(ts, source) {
|
|
2564
|
-
const sourceFile = ts.createSourceFile("generated.ts", source, ts.ScriptTarget.Latest, true);
|
|
2565
|
-
const matches = [];
|
|
2566
|
-
const push = (node, value, kind, typeOnly = false) => {
|
|
2567
|
-
matches.push({
|
|
2568
|
-
value,
|
|
2569
|
-
index: node.getStart(sourceFile),
|
|
2570
|
-
kind,
|
|
2571
|
-
typeOnly
|
|
2572
|
-
});
|
|
2573
|
-
};
|
|
2574
|
-
const visit = (node) => {
|
|
2575
|
-
if (ts.isImportDeclaration(node) && ts.isStringLiteralLike(node.moduleSpecifier)) {
|
|
2576
|
-
const clause = node.importClause;
|
|
2577
|
-
const namedBindings = clause?.namedBindings;
|
|
2578
|
-
const specifiersOnly = clause && !clause.name && namedBindings && ts.isNamedImports(namedBindings) && namedBindings.elements.length > 0 && namedBindings.elements.every((element) => element.isTypeOnly === true);
|
|
2579
|
-
push(
|
|
2580
|
-
node.moduleSpecifier,
|
|
2581
|
-
node.moduleSpecifier.text,
|
|
2582
|
-
"import",
|
|
2583
|
-
Boolean(clause?.isTypeOnly || specifiersOnly)
|
|
2584
|
-
);
|
|
2585
|
-
} else if (ts.isExportDeclaration(node) && ts.isStringLiteralLike(node.moduleSpecifier)) {
|
|
2586
|
-
const clause = node.exportClause;
|
|
2587
|
-
const specifiersOnly = clause && ts.isNamedExports(clause) && clause.elements.length > 0 && clause.elements.every((element) => element.isTypeOnly === true);
|
|
2588
|
-
push(
|
|
2589
|
-
node.moduleSpecifier,
|
|
2590
|
-
node.moduleSpecifier.text,
|
|
2591
|
-
"export",
|
|
2592
|
-
Boolean(node.isTypeOnly || specifiersOnly)
|
|
2593
|
-
);
|
|
2594
|
-
} else if (ts.isImportEqualsDeclaration(node) && ts.isExternalModuleReference(node.moduleReference)) {
|
|
2595
|
-
const argument = node.moduleReference.expression;
|
|
2596
|
-
const value = tsStringLiteralText(ts, argument);
|
|
2597
|
-
if (value !== void 0) push(argument, value, "require");
|
|
2598
|
-
} else if (ts.isCallExpression(node)) {
|
|
2599
|
-
const argument = node.arguments[0];
|
|
2600
|
-
const value = tsStringLiteralText(ts, argument);
|
|
2601
|
-
if (value !== void 0 && node.expression.kind === ts.SyntaxKind.ImportKeyword) {
|
|
2602
|
-
push(argument, value, "dynamic-import");
|
|
2603
|
-
} else if (value !== void 0 && ts.isIdentifier(node.expression) && node.expression.text === "require") {
|
|
2604
|
-
push(argument, value, "require");
|
|
2605
|
-
}
|
|
2606
|
-
}
|
|
2607
|
-
ts.forEachChild(node, visit);
|
|
2608
|
-
};
|
|
2609
|
-
visit(sourceFile);
|
|
2610
|
-
return matches.sort((a, b) => a.index - b.index);
|
|
2611
|
-
}
|
|
2612
|
-
function nonLiteralDynamicDependencies(ts, source) {
|
|
2613
|
-
const sourceFile = ts.createSourceFile("generated.ts", source, ts.ScriptTarget.Latest, true);
|
|
2614
|
-
const uses = [];
|
|
2615
|
-
const visit = (node) => {
|
|
2616
|
-
if (ts.isCallExpression(node)) {
|
|
2617
|
-
const dynamicImport = node.expression.kind === ts.SyntaxKind.ImportKeyword;
|
|
2618
|
-
const directRequire = ts.isIdentifier(node.expression) && node.expression.text === "require";
|
|
2619
|
-
const argument = node.arguments[0];
|
|
2620
|
-
if ((dynamicImport || directRequire) && (!argument || !ts.isStringLiteralLike(argument))) {
|
|
2621
|
-
uses.push({
|
|
2622
|
-
line: sourceFile.getLineAndCharacterOfPosition(node.getStart(sourceFile)).line + 1,
|
|
2623
|
-
kind: directRequire ? "require" : "import"
|
|
2624
|
-
});
|
|
2625
|
-
}
|
|
2626
|
-
}
|
|
2627
|
-
ts.forEachChild(node, visit);
|
|
2628
|
-
};
|
|
2629
|
-
visit(sourceFile);
|
|
2630
|
-
return uses;
|
|
2631
|
-
}
|
|
2632
|
-
function extractQuotedStringsAst(ts, source) {
|
|
2633
|
-
const sourceFile = ts.createSourceFile("generated.ts", source, ts.ScriptTarget.Latest, true);
|
|
2634
|
-
const matches = [];
|
|
2635
|
-
const visit = (node) => {
|
|
2636
|
-
if (ts.isStringLiteralLike(node)) {
|
|
2637
|
-
matches.push({ value: node.text, index: node.getStart(sourceFile) });
|
|
2638
|
-
}
|
|
2639
|
-
ts.forEachChild(node, visit);
|
|
2640
|
-
};
|
|
2641
|
-
visit(sourceFile);
|
|
2642
|
-
return matches;
|
|
2643
|
-
}
|
|
2644
|
-
function looksLikeIntentName(s) {
|
|
2645
|
-
return /^(Domain|Application|Adapter|Workflow|Job|Presentation|Reporting|Metadata|Security|Audit|Observability|Kernel)\.[A-Za-z0-9_.]+$/.test(s);
|
|
2646
|
-
}
|
|
2647
|
-
function hasInfrastructureToken(specifier) {
|
|
2648
|
-
const tokens = specifier.toLowerCase().split(/[^a-z0-9]+/).filter(Boolean);
|
|
2649
|
-
return [
|
|
2650
|
-
"adapter",
|
|
2651
|
-
"adapters",
|
|
2652
|
-
"infra",
|
|
2653
|
-
"infrastructure",
|
|
2654
|
-
"persistence",
|
|
2655
|
-
"repository",
|
|
2656
|
-
"repositories",
|
|
2657
|
-
"integration",
|
|
2658
|
-
"database",
|
|
2659
|
-
"db"
|
|
2660
|
-
].some((token) => tokens.includes(token));
|
|
2661
|
-
}
|
|
2662
|
-
function isKnownInfrastructurePackage(specifier) {
|
|
2663
|
-
const normalized = specifier.toLowerCase();
|
|
2664
|
-
return ["sequelize", "prisma", "typeorm", "mongoose", "knex"].some(
|
|
2665
|
-
(name) => normalized === name || normalized.startsWith(`${name}/`)
|
|
2666
|
-
);
|
|
2667
|
-
}
|
|
2668
|
-
function layerHasInfrastructureRole(layerName) {
|
|
2669
|
-
const normalized = layerName.toLowerCase();
|
|
2670
|
-
return [
|
|
2671
|
-
"adapter",
|
|
2672
|
-
"infra",
|
|
2673
|
-
"persistence",
|
|
2674
|
-
"repository",
|
|
2675
|
-
"repositories",
|
|
2676
|
-
"integration",
|
|
2677
|
-
"database"
|
|
2678
|
-
].some((token) => normalized.includes(token));
|
|
2679
|
-
}
|
|
2680
|
-
function tsStringLiteralText(ts, node) {
|
|
2681
|
-
return node && ts.isStringLiteralLike(node) ? node.text : void 0;
|
|
2682
|
-
}
|
|
2683
|
-
function tsPropertyName(ts, node) {
|
|
2684
|
-
if (!node) return void 0;
|
|
2685
|
-
if (ts.isIdentifier(node) || ts.isStringLiteralLike(node)) return node.text;
|
|
2686
|
-
return void 0;
|
|
2687
|
-
}
|
|
2688
|
-
function tsObjectProperty(ts, node, name) {
|
|
2689
|
-
if (!node || !ts.isObjectLiteralExpression(node)) return void 0;
|
|
2690
|
-
return node.properties.find((property) => {
|
|
2691
|
-
if (!ts.isPropertyAssignment(property) && !ts.isShorthandPropertyAssignment(property)) {
|
|
2692
|
-
return false;
|
|
2693
|
-
}
|
|
2694
|
-
return tsPropertyName(ts, property.name) === name;
|
|
2695
|
-
});
|
|
2696
|
-
}
|
|
2697
|
-
function tsObjectHasProperty(ts, node, name) {
|
|
2698
|
-
return tsObjectProperty(ts, node, name) !== void 0;
|
|
2699
|
-
}
|
|
2700
|
-
function tsObjectPropertyValue(ts, node, name) {
|
|
2701
|
-
const property = tsObjectProperty(ts, node, name);
|
|
2702
|
-
return property && ts.isPropertyAssignment(property) ? property.initializer : void 0;
|
|
2703
|
-
}
|
|
2704
|
-
function tsObjectHasMetadataSource(ts, node) {
|
|
2705
|
-
const metadata = tsObjectPropertyValue(ts, node, "metadata");
|
|
2706
|
-
return tsObjectHasProperty(ts, metadata, "source");
|
|
2707
|
-
}
|
|
2708
|
-
function tsLooksLikeIntentCreatorExpression(ts, node) {
|
|
2709
|
-
if (!node) return false;
|
|
2710
|
-
if (ts.isIdentifier(node)) return /^[A-Z]/.test(node.text);
|
|
2711
|
-
if (ts.isPropertyAccessExpression(node)) {
|
|
2712
|
-
return tsLooksLikeIntentCreatorExpression(ts, node.name);
|
|
2713
|
-
}
|
|
2714
|
-
return false;
|
|
2715
|
-
}
|
|
2716
|
-
function tsIsPublishCall(ts, node) {
|
|
2717
|
-
if (!ts.isCallExpression(node)) return false;
|
|
2718
|
-
const expression = node.expression;
|
|
2719
|
-
if (ts.isPropertyAccessExpression(expression)) {
|
|
2720
|
-
return expression.name.text === "publish";
|
|
594
|
+
return false;
|
|
595
|
+
}
|
|
596
|
+
function tsIsPublishCall(ts, node) {
|
|
597
|
+
if (!ts.isCallExpression(node)) return false;
|
|
598
|
+
const expression = node.expression;
|
|
599
|
+
if (ts.isPropertyAccessExpression(expression)) {
|
|
600
|
+
return expression.name.text === "publish";
|
|
2721
601
|
}
|
|
2722
602
|
return ts.isIdentifier(expression) && expression.text === "publish";
|
|
2723
603
|
}
|
|
@@ -2725,7 +605,7 @@ function tsIsArkPublishCandidate(ts, node) {
|
|
|
2725
605
|
if (!ts.isCallExpression(node)) return false;
|
|
2726
606
|
const firstArg = node.arguments[0];
|
|
2727
607
|
const rawIntent = tsStringLiteralText(ts, firstArg);
|
|
2728
|
-
return rawIntent !== void 0 &&
|
|
608
|
+
return rawIntent !== void 0 && looksLikeArkIntent(rawIntent) || tsObjectHasProperty(ts, firstArg, "intent") || tsLooksLikeIntentCreatorExpression(ts, firstArg);
|
|
2729
609
|
}
|
|
2730
610
|
function tsPublishHasSource(ts, node) {
|
|
2731
611
|
if (!ts.isCallExpression(node)) return false;
|
|
@@ -2738,80 +618,6 @@ function tsPublishSourceLiteral(ts, node) {
|
|
|
2738
618
|
const rawMetadata = tsObjectPropertyValue(ts, firstArg, "metadata");
|
|
2739
619
|
return tsStringLiteralText(ts, tsObjectPropertyValue(ts, rawMetadata, "source")) ?? tsStringLiteralText(ts, tsObjectPropertyValue(ts, secondArg, "source")) ?? tsStringLiteralText(ts, tsObjectPropertyValue(ts, thirdArg, "source"));
|
|
2740
620
|
}
|
|
2741
|
-
function singleFileTypeChecker(ts, sourceFile) {
|
|
2742
|
-
const options = {
|
|
2743
|
-
noLib: true,
|
|
2744
|
-
noResolve: true,
|
|
2745
|
-
target: ts.ScriptTarget.Latest
|
|
2746
|
-
};
|
|
2747
|
-
const host = ts.createCompilerHost(options, true);
|
|
2748
|
-
host.getSourceFile = (fileName) => fileName === sourceFile.fileName ? sourceFile : void 0;
|
|
2749
|
-
host.fileExists = (fileName) => fileName === sourceFile.fileName;
|
|
2750
|
-
host.readFile = (fileName) => fileName === sourceFile.fileName ? sourceFile.text : void 0;
|
|
2751
|
-
return ts.createProgram([sourceFile.fileName], options, host).getTypeChecker();
|
|
2752
|
-
}
|
|
2753
|
-
function propertyAccessPath(ts, node) {
|
|
2754
|
-
const segments = [];
|
|
2755
|
-
let current = node;
|
|
2756
|
-
while (ts.isPropertyAccessExpression(current)) {
|
|
2757
|
-
segments.unshift(current.name.text);
|
|
2758
|
-
current = current.expression;
|
|
2759
|
-
}
|
|
2760
|
-
if (!ts.isIdentifier(current)) return void 0;
|
|
2761
|
-
segments.unshift(current.text);
|
|
2762
|
-
return { root: current, segments };
|
|
2763
|
-
}
|
|
2764
|
-
function isRuntimeIdentifierReference(ts, node) {
|
|
2765
|
-
if (ts.isPropertyAccessExpression(node.parent) && node.parent.name === node) return false;
|
|
2766
|
-
return ts.isExpressionNode(node) && !ts.isInTypeQuery(node) || ts.isShorthandPropertyAssignment(node.parent) && node.parent.name === node;
|
|
2767
|
-
}
|
|
2768
|
-
function hasLocalDeclaration(ts, checker, sourceFile, node) {
|
|
2769
|
-
const shorthand = ts.isShorthandPropertyAssignment(node.parent) && node.parent.name === node;
|
|
2770
|
-
const symbol = shorthand ? checker.getShorthandAssignmentValueSymbol(node.parent) : checker.getSymbolAtLocation(node);
|
|
2771
|
-
return Boolean(
|
|
2772
|
-
symbol?.declarations?.some((declaration) => declaration.getSourceFile() === sourceFile)
|
|
2773
|
-
);
|
|
2774
|
-
}
|
|
2775
|
-
function analyzeForbiddenGlobals(ts, source, filePath, layer, forbidden) {
|
|
2776
|
-
const entries = new Set(forbidden);
|
|
2777
|
-
if (entries.size === 0) return [];
|
|
2778
|
-
const sourceFile = ts.createSourceFile("generated.ts", source, ts.ScriptTarget.Latest, true);
|
|
2779
|
-
const checker = singleFileTypeChecker(ts, sourceFile);
|
|
2780
|
-
const violations = [];
|
|
2781
|
-
const flag = (name, node) => violations.push(
|
|
2782
|
-
violation("FORBIDDEN_GLOBAL", `${layer} must not use the ambient global "${name}".`, {
|
|
2783
|
-
line: sourceFile.getLineAndCharacterOfPosition(node.getStart(sourceFile)).line + 1,
|
|
2784
|
-
filePath,
|
|
2785
|
-
target: name,
|
|
2786
|
-
fromLayer: layer,
|
|
2787
|
-
suggestion: "Inject the capability through a port (e.g. a Clock, IdGenerator, or HttpPort) instead of reaching for the ambient global."
|
|
2788
|
-
})
|
|
2789
|
-
);
|
|
2790
|
-
const visit = (node) => {
|
|
2791
|
-
const nestedPropertyAccess = ts.isPropertyAccessExpression(node) && ts.isPropertyAccessExpression(node.parent) && node.parent.expression === node;
|
|
2792
|
-
if (ts.isPropertyAccessExpression(node) && !nestedPropertyAccess) {
|
|
2793
|
-
const path = propertyAccessPath(ts, node);
|
|
2794
|
-
if (path && !hasLocalDeclaration(ts, checker, sourceFile, path.root)) {
|
|
2795
|
-
const explicitGlobalThis = path.segments[0] === "globalThis";
|
|
2796
|
-
const normalized = explicitGlobalThis ? path.segments.slice(1) : path.segments;
|
|
2797
|
-
let match;
|
|
2798
|
-
for (let length = normalized.length; length >= (explicitGlobalThis ? 1 : 2); length -= 1) {
|
|
2799
|
-
const candidate = normalized.slice(0, length).join(".");
|
|
2800
|
-
if (entries.has(candidate)) {
|
|
2801
|
-
match = candidate;
|
|
2802
|
-
break;
|
|
2803
|
-
}
|
|
2804
|
-
}
|
|
2805
|
-
if (match) flag(match, node);
|
|
2806
|
-
}
|
|
2807
|
-
} else if (ts.isIdentifier(node) && entries.has(node.text) && isRuntimeIdentifierReference(ts, node) && !hasLocalDeclaration(ts, checker, sourceFile, node)) {
|
|
2808
|
-
flag(node.text, node);
|
|
2809
|
-
}
|
|
2810
|
-
ts.forEachChild(node, visit);
|
|
2811
|
-
};
|
|
2812
|
-
visit(sourceFile);
|
|
2813
|
-
return violations;
|
|
2814
|
-
}
|
|
2815
621
|
function analyzePublishAst(ts, source, context, profile) {
|
|
2816
622
|
const sourceFile = ts.createSourceFile(
|
|
2817
623
|
"generated.ts",
|
|
@@ -2826,26 +632,21 @@ function analyzePublishAst(ts, source, context, profile) {
|
|
|
2826
632
|
const lineForNode = (node) => sourceFile.getLineAndCharacterOfPosition(node.getStart(sourceFile)).line + 1;
|
|
2827
633
|
const visit = (node) => {
|
|
2828
634
|
if (tsIsPublishCall(ts, node)) {
|
|
2829
|
-
const firstArg = node.arguments[0];
|
|
2830
|
-
const rawIntent = tsStringLiteralText(ts, firstArg);
|
|
2831
|
-
|
|
2832
|
-
|
|
2833
|
-
|
|
2834
|
-
|
|
2835
|
-
|
|
2836
|
-
|
|
2837
|
-
|
|
2838
|
-
}
|
|
2839
|
-
if (tsIsArkPublishCandidate(ts, node) && !tsPublishHasSource(ts, node)) {
|
|
635
|
+
const firstArg = node.arguments[0];
|
|
636
|
+
const rawIntent = tsStringLiteralText(ts, firstArg);
|
|
637
|
+
for (const finding of classifyPublishFacts({
|
|
638
|
+
publishCall: true,
|
|
639
|
+
rawIntentName: rawIntent,
|
|
640
|
+
objectHasIntent: tsObjectHasProperty(ts, firstArg, "intent"),
|
|
641
|
+
arkPublishCandidate: tsIsArkPublishCandidate(ts, node),
|
|
642
|
+
hasSource: tsPublishHasSource(ts, node)
|
|
643
|
+
})) {
|
|
2840
644
|
violations.push(
|
|
2841
|
-
violation(
|
|
2842
|
-
line: lineForNode(node),
|
|
2843
|
-
filePath
|
|
2844
|
-
})
|
|
645
|
+
violation(finding.ruleId, finding.message, { line: lineForNode(node), filePath })
|
|
2845
646
|
);
|
|
2846
647
|
}
|
|
2847
648
|
const sourceIntent = tsPublishSourceLiteral(ts, node);
|
|
2848
|
-
if (profile && contextLayer && sourceIntent &&
|
|
649
|
+
if (profile && contextLayer && sourceIntent && looksLikeArkIntent(sourceIntent)) {
|
|
2849
650
|
const sourceLayer = profile.resolveLayer(sourceIntent);
|
|
2850
651
|
if (sourceLayer && sourceLayer !== contextLayer) {
|
|
2851
652
|
violations.push(
|
|
@@ -2882,10 +683,23 @@ function createAICodeGate(options = {}) {
|
|
|
2882
683
|
const gateContext = context;
|
|
2883
684
|
const filePath = gateContext?.filePath;
|
|
2884
685
|
const contextLayer = gateContext?.layer;
|
|
2885
|
-
const
|
|
686
|
+
const semanticTypescript = options.typescript;
|
|
687
|
+
const semanticSourceFile = semanticTypescript ? semanticTypescript.createSourceFile(
|
|
688
|
+
filePath ?? "generated.ts",
|
|
689
|
+
source,
|
|
690
|
+
semanticTypescript.ScriptTarget.Latest,
|
|
691
|
+
true
|
|
692
|
+
) : void 0;
|
|
693
|
+
const semanticDependencies = semanticSourceFile ? extractSemanticDependencies(options.typescript, semanticSourceFile) : void 0;
|
|
694
|
+
const moduleSpecifiers2 = semanticDependencies ? semanticDependencies.filter((dependency) => dependency.specifier !== void 0).map((dependency) => ({
|
|
695
|
+
value: dependency.specifier,
|
|
696
|
+
index: dependency.node.getStart(semanticSourceFile),
|
|
697
|
+
kind: dependency.kind,
|
|
698
|
+
typeOnly: dependency.typeOnly
|
|
699
|
+
})) : extractModuleSpecifiers(source);
|
|
2886
700
|
const quotedStrings = options.typescript ? extractQuotedStringsAst(options.typescript, source) : extractQuotedStrings(source);
|
|
2887
701
|
if (options.typescript && !options.allowNonLiteralDynamicImport?.(filePath)) {
|
|
2888
|
-
for (const dependency of
|
|
702
|
+
for (const dependency of semanticDependencies?.filter(({ unresolved }) => unresolved) ?? []) {
|
|
2889
703
|
const isRequire = dependency.kind === "require";
|
|
2890
704
|
violations.push(
|
|
2891
705
|
violation(
|
|
@@ -2906,7 +720,7 @@ function createAICodeGate(options = {}) {
|
|
|
2906
720
|
if (match) {
|
|
2907
721
|
violations.push(
|
|
2908
722
|
violation("FORBIDDEN_PATTERN", `Forbidden pattern matched: ${pat}`, {
|
|
2909
|
-
line: match.index === void 0 ? void 0 :
|
|
723
|
+
line: match.index === void 0 ? void 0 : lineOf2(source, match.index),
|
|
2910
724
|
filePath,
|
|
2911
725
|
suggestion: "Remove infrastructure imports from domain/application layers." + infraLayerEscapeHatch
|
|
2912
726
|
})
|
|
@@ -2915,7 +729,7 @@ function createAICodeGate(options = {}) {
|
|
|
2915
729
|
} else if (source.includes(pat)) {
|
|
2916
730
|
violations.push(
|
|
2917
731
|
violation("FORBIDDEN_SUBSTRING", `Forbidden substring: ${pat}`, {
|
|
2918
|
-
line:
|
|
732
|
+
line: lineOf2(source, source.indexOf(pat)),
|
|
2919
733
|
filePath
|
|
2920
734
|
})
|
|
2921
735
|
);
|
|
@@ -2946,7 +760,7 @@ function createAICodeGate(options = {}) {
|
|
|
2946
760
|
"LAYER_IMPORT_VIOLATION",
|
|
2947
761
|
blocked.message ?? (peer ? `Layer "${contextLayer}" must not import across slices into "${targetLayer}".` : `Layer "${contextLayer}" must not import "${targetLayer}".`),
|
|
2948
762
|
{
|
|
2949
|
-
line:
|
|
763
|
+
line: lineOf2(source, specifier.index),
|
|
2950
764
|
source: specifier.value,
|
|
2951
765
|
target: specifier.value,
|
|
2952
766
|
filePath,
|
|
@@ -2976,7 +790,7 @@ function createAICodeGate(options = {}) {
|
|
|
2976
790
|
"FORBIDDEN_IMPORT",
|
|
2977
791
|
`Forbidden ${specifier.kind} target: "${specifier.value}".`,
|
|
2978
792
|
{
|
|
2979
|
-
line:
|
|
793
|
+
line: lineOf2(source, specifier.index),
|
|
2980
794
|
source: specifier.value,
|
|
2981
795
|
target: specifier.value,
|
|
2982
796
|
filePath,
|
|
@@ -3011,114 +825,587 @@ function createAICodeGate(options = {}) {
|
|
|
3011
825
|
}
|
|
3012
826
|
}
|
|
3013
827
|
}
|
|
3014
|
-
if (enforceAllowlist && intentNames.size > 0) {
|
|
3015
|
-
for (const literal of quotedStrings) {
|
|
3016
|
-
if (
|
|
3017
|
-
violations.push(
|
|
3018
|
-
violation(
|
|
3019
|
-
"UNKNOWN_INTENT",
|
|
3020
|
-
`Unknown intent reference: "${literal.value}"`,
|
|
3021
|
-
{
|
|
3022
|
-
line:
|
|
3023
|
-
filePath,
|
|
3024
|
-
target: literal.value,
|
|
3025
|
-
suggestion: `Register intent "${literal.value}" via defineIntent() or remove the reference.`
|
|
3026
|
-
}
|
|
3027
|
-
)
|
|
3028
|
-
);
|
|
3029
|
-
}
|
|
828
|
+
if (enforceAllowlist && intentNames.size > 0) {
|
|
829
|
+
for (const literal of quotedStrings) {
|
|
830
|
+
if (looksLikeArkIntent(literal.value) && !intentNames.has(literal.value)) {
|
|
831
|
+
violations.push(
|
|
832
|
+
violation(
|
|
833
|
+
"UNKNOWN_INTENT",
|
|
834
|
+
`Unknown intent reference: "${literal.value}"`,
|
|
835
|
+
{
|
|
836
|
+
line: lineOf2(source, literal.index),
|
|
837
|
+
filePath,
|
|
838
|
+
target: literal.value,
|
|
839
|
+
suggestion: `Register intent "${literal.value}" via defineIntent() or remove the reference.`
|
|
840
|
+
}
|
|
841
|
+
)
|
|
842
|
+
);
|
|
843
|
+
}
|
|
844
|
+
}
|
|
845
|
+
}
|
|
846
|
+
if (options.architectureProfile && contextLayer) {
|
|
847
|
+
for (const literal of quotedStrings) {
|
|
848
|
+
if (!looksLikeArkIntent(literal.value)) continue;
|
|
849
|
+
const targetLayer = options.architectureProfile.resolveLayer(literal.value);
|
|
850
|
+
if (!targetLayer) continue;
|
|
851
|
+
const blocked = findDeniedEdgeRule(
|
|
852
|
+
options.architectureProfile.rules,
|
|
853
|
+
contextLayer,
|
|
854
|
+
targetLayer
|
|
855
|
+
);
|
|
856
|
+
if (blocked) {
|
|
857
|
+
violations.push(
|
|
858
|
+
violation(
|
|
859
|
+
"LAYER_REFERENCE_VIOLATION",
|
|
860
|
+
blocked.message ?? `Layer "${contextLayer}" must not reference "${targetLayer}" through "${literal.value}".`,
|
|
861
|
+
{
|
|
862
|
+
line: lineOf2(source, literal.index),
|
|
863
|
+
filePath,
|
|
864
|
+
target: literal.value,
|
|
865
|
+
fromLayer: contextLayer,
|
|
866
|
+
toLayer: targetLayer,
|
|
867
|
+
suggestion: "Route the dependency through an allowed intent, port, or event.",
|
|
868
|
+
details: { rule: blocked }
|
|
869
|
+
}
|
|
870
|
+
)
|
|
871
|
+
);
|
|
872
|
+
}
|
|
873
|
+
}
|
|
874
|
+
}
|
|
875
|
+
if (options.extensions) {
|
|
876
|
+
for (const ext of options.extensions) {
|
|
877
|
+
try {
|
|
878
|
+
const extViolations = ext.analyze(source, context);
|
|
879
|
+
violations.push(...extViolations);
|
|
880
|
+
} catch (err) {
|
|
881
|
+
violations.push(
|
|
882
|
+
violation(
|
|
883
|
+
"EXTENSION_ERROR",
|
|
884
|
+
`Extension "${ext.name}" failed: ${err instanceof Error ? err.message : String(err)}`
|
|
885
|
+
)
|
|
886
|
+
);
|
|
887
|
+
}
|
|
888
|
+
}
|
|
889
|
+
}
|
|
890
|
+
if (options.typescript && semanticSourceFile && contextLayer && options.forbiddenGlobals?.[contextLayer]?.length) {
|
|
891
|
+
try {
|
|
892
|
+
violations.push(
|
|
893
|
+
...collectForbiddenCapabilityUses(
|
|
894
|
+
options.typescript,
|
|
895
|
+
semanticSourceFile,
|
|
896
|
+
options.forbiddenGlobals[contextLayer]
|
|
897
|
+
).map(
|
|
898
|
+
(use) => violation(
|
|
899
|
+
"FORBIDDEN_GLOBAL",
|
|
900
|
+
`${contextLayer} must not use the ambient global "${use.name}".`,
|
|
901
|
+
{
|
|
902
|
+
line: use.line,
|
|
903
|
+
filePath,
|
|
904
|
+
target: use.name,
|
|
905
|
+
fromLayer: contextLayer,
|
|
906
|
+
suggestion: "Inject the capability through a port (e.g. a Clock, IdGenerator, or HttpPort) instead of reaching for the ambient global."
|
|
907
|
+
}
|
|
908
|
+
)
|
|
909
|
+
)
|
|
910
|
+
);
|
|
911
|
+
} catch (err) {
|
|
912
|
+
violations.push(
|
|
913
|
+
violation(
|
|
914
|
+
"AST_ANALYZER_ERROR",
|
|
915
|
+
`TypeScript AST analyzer failed: ${err instanceof Error ? err.message : String(err)}`
|
|
916
|
+
)
|
|
917
|
+
);
|
|
918
|
+
}
|
|
919
|
+
}
|
|
920
|
+
if (options.typescript) {
|
|
921
|
+
try {
|
|
922
|
+
violations.push(
|
|
923
|
+
...analyzePublishAst(
|
|
924
|
+
options.typescript,
|
|
925
|
+
source,
|
|
926
|
+
context,
|
|
927
|
+
options.architectureProfile
|
|
928
|
+
)
|
|
929
|
+
);
|
|
930
|
+
} catch (err) {
|
|
931
|
+
violations.push(
|
|
932
|
+
violation(
|
|
933
|
+
"AST_ANALYZER_ERROR",
|
|
934
|
+
`TypeScript AST analyzer failed: ${err instanceof Error ? err.message : String(err)}`
|
|
935
|
+
)
|
|
936
|
+
);
|
|
937
|
+
}
|
|
938
|
+
}
|
|
939
|
+
return {
|
|
940
|
+
valid: violations.length === 0,
|
|
941
|
+
violations
|
|
942
|
+
};
|
|
943
|
+
}
|
|
944
|
+
};
|
|
945
|
+
}
|
|
946
|
+
|
|
947
|
+
// src/domain/configContract.ts
|
|
948
|
+
var ARK_CONFIG_SCHEMA_VERSION = "1.0";
|
|
949
|
+
var ARK_CONFIG_SCHEMA_URL = "https://unpkg.com/arkgate@2/schemas/ark.config.schema.json";
|
|
950
|
+
var DEFAULT_LAYER_NAMES = [
|
|
951
|
+
"DomainModel",
|
|
952
|
+
"ApplicationOrchestration",
|
|
953
|
+
"PersistenceAdapters",
|
|
954
|
+
"IntegrationAdapters",
|
|
955
|
+
"WorkflowSagaEngine",
|
|
956
|
+
"BackgroundJobsScheduling",
|
|
957
|
+
"PresentationAdapters",
|
|
958
|
+
"ReportingReadModels",
|
|
959
|
+
"ExtensibilityMetadata",
|
|
960
|
+
"SecurityAuditObservability",
|
|
961
|
+
"Kernel"
|
|
962
|
+
];
|
|
963
|
+
var DEFAULT_ALLOWED_FLOWS = /* @__PURE__ */ new Set([
|
|
964
|
+
"PresentationAdapters->ApplicationOrchestration",
|
|
965
|
+
"ApplicationOrchestration->DomainModel",
|
|
966
|
+
"WorkflowSagaEngine->ApplicationOrchestration",
|
|
967
|
+
"WorkflowSagaEngine->DomainModel",
|
|
968
|
+
"BackgroundJobsScheduling->ApplicationOrchestration"
|
|
969
|
+
]);
|
|
970
|
+
function createDefaultRules() {
|
|
971
|
+
const rules = [];
|
|
972
|
+
for (const from of DEFAULT_LAYER_NAMES) {
|
|
973
|
+
for (const to of DEFAULT_LAYER_NAMES) {
|
|
974
|
+
if (from === to || DEFAULT_ALLOWED_FLOWS.has(`${from}->${to}`)) continue;
|
|
975
|
+
rules.push({ from, to, allowed: false });
|
|
976
|
+
}
|
|
977
|
+
}
|
|
978
|
+
return rules;
|
|
979
|
+
}
|
|
980
|
+
var DEFAULT_ARK_CONFIG_RULES = createDefaultRules();
|
|
981
|
+
var stringArraySchema = {
|
|
982
|
+
type: "array",
|
|
983
|
+
items: { type: "string", minLength: 1 },
|
|
984
|
+
uniqueItems: true
|
|
985
|
+
};
|
|
986
|
+
var ARK_CONFIG_SCHEMA = {
|
|
987
|
+
$schema: "https://json-schema.org/draft/2020-12/schema",
|
|
988
|
+
$id: ARK_CONFIG_SCHEMA_URL,
|
|
989
|
+
title: "ArkGate architecture contract",
|
|
990
|
+
description: "Versioned contract consumed identically by ArkGate CLI, MCP, and ESLint surfaces.",
|
|
991
|
+
type: "object",
|
|
992
|
+
additionalProperties: false,
|
|
993
|
+
required: ["$schema", "schemaVersion", "include", "layers", "rules"],
|
|
994
|
+
properties: {
|
|
995
|
+
$schema: {
|
|
996
|
+
type: "string",
|
|
997
|
+
minLength: 1,
|
|
998
|
+
default: ARK_CONFIG_SCHEMA_URL,
|
|
999
|
+
description: "Editor-facing URL or local path for this JSON Schema."
|
|
1000
|
+
},
|
|
1001
|
+
schemaVersion: {
|
|
1002
|
+
type: "string",
|
|
1003
|
+
const: ARK_CONFIG_SCHEMA_VERSION,
|
|
1004
|
+
default: ARK_CONFIG_SCHEMA_VERSION
|
|
1005
|
+
},
|
|
1006
|
+
name: { type: "string", minLength: 1 },
|
|
1007
|
+
include: { ...stringArraySchema, minItems: 1, default: ["src"] },
|
|
1008
|
+
exclude: { ...stringArraySchema, default: [] },
|
|
1009
|
+
excludeGenerated: { type: "boolean", default: true },
|
|
1010
|
+
frameworkOverlay: { type: "string", minLength: 1 },
|
|
1011
|
+
layers: {
|
|
1012
|
+
type: "array",
|
|
1013
|
+
default: [],
|
|
1014
|
+
items: { $ref: "#/$defs/layer" }
|
|
1015
|
+
},
|
|
1016
|
+
rules: {
|
|
1017
|
+
type: "array",
|
|
1018
|
+
default: DEFAULT_ARK_CONFIG_RULES,
|
|
1019
|
+
items: { $ref: "#/$defs/rule" }
|
|
1020
|
+
},
|
|
1021
|
+
cyclePolicy: {
|
|
1022
|
+
type: "string",
|
|
1023
|
+
enum: ["strict", "soft", "framework-soft", "off"],
|
|
1024
|
+
default: "strict"
|
|
1025
|
+
},
|
|
1026
|
+
dynamicImportAllowlist: { ...stringArraySchema, default: [] },
|
|
1027
|
+
safety: {
|
|
1028
|
+
$ref: "#/$defs/safety",
|
|
1029
|
+
default: {
|
|
1030
|
+
maxTsSuppressions: 0,
|
|
1031
|
+
maxAnyCasts: 0,
|
|
1032
|
+
allowInMemory: false,
|
|
1033
|
+
allowDisabledPeerIsolation: false
|
|
1034
|
+
}
|
|
1035
|
+
}
|
|
1036
|
+
},
|
|
1037
|
+
$defs: {
|
|
1038
|
+
layer: {
|
|
1039
|
+
type: "object",
|
|
1040
|
+
additionalProperties: false,
|
|
1041
|
+
required: ["name", "patterns"],
|
|
1042
|
+
properties: {
|
|
1043
|
+
name: { type: "string", minLength: 1 },
|
|
1044
|
+
patterns: { ...stringArraySchema, minItems: 1 },
|
|
1045
|
+
exclude: stringArraySchema,
|
|
1046
|
+
intentPrefixes: stringArraySchema,
|
|
1047
|
+
description: { type: "string", minLength: 1 },
|
|
1048
|
+
forbiddenGlobals: stringArraySchema,
|
|
1049
|
+
mayImportInfrastructure: { type: "boolean" },
|
|
1050
|
+
optional: { type: "boolean" }
|
|
1051
|
+
}
|
|
1052
|
+
},
|
|
1053
|
+
rule: {
|
|
1054
|
+
type: "object",
|
|
1055
|
+
additionalProperties: false,
|
|
1056
|
+
required: ["from", "to", "allowed"],
|
|
1057
|
+
properties: {
|
|
1058
|
+
from: { type: "string", minLength: 1 },
|
|
1059
|
+
to: { type: "string", minLength: 1 },
|
|
1060
|
+
allowed: { type: "boolean" },
|
|
1061
|
+
message: { type: "string", minLength: 1 },
|
|
1062
|
+
peerIsolation: { type: "boolean" },
|
|
1063
|
+
sliceFolders: { ...stringArraySchema, minItems: 1 }
|
|
1064
|
+
}
|
|
1065
|
+
},
|
|
1066
|
+
safety: {
|
|
1067
|
+
type: "object",
|
|
1068
|
+
additionalProperties: false,
|
|
1069
|
+
properties: {
|
|
1070
|
+
maxTsSuppressions: { type: "integer", minimum: 0, default: 0 },
|
|
1071
|
+
maxAnyCasts: { type: "integer", minimum: 0, default: 0 },
|
|
1072
|
+
allowInMemory: { type: "boolean", default: false },
|
|
1073
|
+
allowDisabledPeerIsolation: { type: "boolean", default: false }
|
|
1074
|
+
}
|
|
1075
|
+
}
|
|
1076
|
+
}
|
|
1077
|
+
};
|
|
1078
|
+
var ArkConfigValidationError = class extends Error {
|
|
1079
|
+
issues;
|
|
1080
|
+
source;
|
|
1081
|
+
constructor(source, issues) {
|
|
1082
|
+
super(
|
|
1083
|
+
`Invalid ArkGate config (${source}):
|
|
1084
|
+
${issues.map((issue) => `- ${issue.path}: ${issue.message}`).join("\n")}`
|
|
1085
|
+
);
|
|
1086
|
+
this.name = "ArkConfigValidationError";
|
|
1087
|
+
this.source = source;
|
|
1088
|
+
this.issues = issues;
|
|
1089
|
+
}
|
|
1090
|
+
};
|
|
1091
|
+
function isObject(value) {
|
|
1092
|
+
return value !== null && typeof value === "object" && !Array.isArray(value);
|
|
1093
|
+
}
|
|
1094
|
+
function propertyPath(parent, key) {
|
|
1095
|
+
return /^[A-Za-z_$][A-Za-z0-9_$]*$/.test(key) ? `${parent}.${key}` : `${parent}[${JSON.stringify(key)}]`;
|
|
1096
|
+
}
|
|
1097
|
+
function valueType(value) {
|
|
1098
|
+
if (value === null) return "null";
|
|
1099
|
+
if (Array.isArray(value)) return "array";
|
|
1100
|
+
return typeof value;
|
|
1101
|
+
}
|
|
1102
|
+
function resolveSchemaRef(ref, root) {
|
|
1103
|
+
const prefix = "#/$defs/";
|
|
1104
|
+
if (!ref.startsWith(prefix)) return void 0;
|
|
1105
|
+
return root.$defs[ref.slice(prefix.length)];
|
|
1106
|
+
}
|
|
1107
|
+
function validateNode(value, schema, path, root, issues) {
|
|
1108
|
+
if (schema.$ref) {
|
|
1109
|
+
const referenced = resolveSchemaRef(schema.$ref, root);
|
|
1110
|
+
if (!referenced) {
|
|
1111
|
+
issues.push({ path, message: `schema reference ${schema.$ref} cannot be resolved` });
|
|
1112
|
+
return;
|
|
1113
|
+
}
|
|
1114
|
+
validateNode(value, referenced, path, root, issues);
|
|
1115
|
+
return;
|
|
1116
|
+
}
|
|
1117
|
+
if (schema.const !== void 0 && !Object.is(value, schema.const)) {
|
|
1118
|
+
issues.push({ path, message: `must equal ${JSON.stringify(schema.const)}` });
|
|
1119
|
+
return;
|
|
1120
|
+
}
|
|
1121
|
+
if (schema.enum && !schema.enum.some((candidate) => Object.is(candidate, value))) {
|
|
1122
|
+
issues.push({ path, message: `must be one of ${schema.enum.map(String).join(", ")}` });
|
|
1123
|
+
return;
|
|
1124
|
+
}
|
|
1125
|
+
if (schema.type === "object") {
|
|
1126
|
+
if (!isObject(value)) {
|
|
1127
|
+
issues.push({ path, message: `must be an object; received ${valueType(value)}` });
|
|
1128
|
+
return;
|
|
1129
|
+
}
|
|
1130
|
+
const properties = schema.properties ?? {};
|
|
1131
|
+
for (const key of schema.required ?? []) {
|
|
1132
|
+
if (value[key] === void 0) {
|
|
1133
|
+
issues.push({ path: propertyPath(path, key), message: "is required" });
|
|
1134
|
+
}
|
|
1135
|
+
}
|
|
1136
|
+
if (schema.additionalProperties === false) {
|
|
1137
|
+
for (const key of Object.keys(value)) {
|
|
1138
|
+
if (!(key in properties)) {
|
|
1139
|
+
issues.push({ path: propertyPath(path, key), message: "unknown field" });
|
|
3030
1140
|
}
|
|
3031
1141
|
}
|
|
3032
|
-
|
|
3033
|
-
|
|
3034
|
-
|
|
3035
|
-
|
|
3036
|
-
if (!targetLayer) continue;
|
|
3037
|
-
const blocked = findDeniedEdgeRule(
|
|
3038
|
-
options.architectureProfile.rules,
|
|
3039
|
-
contextLayer,
|
|
3040
|
-
targetLayer
|
|
3041
|
-
);
|
|
3042
|
-
if (blocked) {
|
|
3043
|
-
violations.push(
|
|
3044
|
-
violation(
|
|
3045
|
-
"LAYER_REFERENCE_VIOLATION",
|
|
3046
|
-
blocked.message ?? `Layer "${contextLayer}" must not reference "${targetLayer}" through "${literal.value}".`,
|
|
3047
|
-
{
|
|
3048
|
-
line: lineOf(source, literal.index),
|
|
3049
|
-
filePath,
|
|
3050
|
-
target: literal.value,
|
|
3051
|
-
fromLayer: contextLayer,
|
|
3052
|
-
toLayer: targetLayer,
|
|
3053
|
-
suggestion: "Route the dependency through an allowed intent, port, or event.",
|
|
3054
|
-
details: { rule: blocked }
|
|
3055
|
-
}
|
|
3056
|
-
)
|
|
3057
|
-
);
|
|
3058
|
-
}
|
|
3059
|
-
}
|
|
1142
|
+
}
|
|
1143
|
+
for (const [key, childSchema] of Object.entries(properties)) {
|
|
1144
|
+
if (value[key] !== void 0) {
|
|
1145
|
+
validateNode(value[key], childSchema, propertyPath(path, key), root, issues);
|
|
3060
1146
|
}
|
|
3061
|
-
|
|
3062
|
-
|
|
3063
|
-
|
|
3064
|
-
|
|
3065
|
-
|
|
3066
|
-
|
|
3067
|
-
|
|
3068
|
-
|
|
3069
|
-
|
|
3070
|
-
|
|
3071
|
-
|
|
3072
|
-
|
|
3073
|
-
|
|
3074
|
-
|
|
1147
|
+
}
|
|
1148
|
+
return;
|
|
1149
|
+
}
|
|
1150
|
+
if (schema.type === "array") {
|
|
1151
|
+
if (!Array.isArray(value)) {
|
|
1152
|
+
issues.push({ path, message: `must be an array; received ${valueType(value)}` });
|
|
1153
|
+
return;
|
|
1154
|
+
}
|
|
1155
|
+
if (schema.minItems !== void 0 && value.length < schema.minItems) {
|
|
1156
|
+
issues.push({ path, message: `must contain at least ${schema.minItems} item(s)` });
|
|
1157
|
+
}
|
|
1158
|
+
if (schema.uniqueItems) {
|
|
1159
|
+
const serialized = value.map((entry) => JSON.stringify(entry));
|
|
1160
|
+
if (new Set(serialized).size !== serialized.length) {
|
|
1161
|
+
issues.push({ path, message: "must not contain duplicate items" });
|
|
3075
1162
|
}
|
|
3076
|
-
|
|
3077
|
-
|
|
3078
|
-
|
|
3079
|
-
|
|
3080
|
-
|
|
3081
|
-
|
|
3082
|
-
|
|
3083
|
-
|
|
3084
|
-
|
|
3085
|
-
|
|
3086
|
-
|
|
3087
|
-
|
|
3088
|
-
|
|
3089
|
-
|
|
3090
|
-
|
|
3091
|
-
|
|
3092
|
-
|
|
3093
|
-
|
|
3094
|
-
|
|
1163
|
+
}
|
|
1164
|
+
if (schema.items) {
|
|
1165
|
+
value.forEach(
|
|
1166
|
+
(entry, index) => validateNode(entry, schema.items, `${path}[${index}]`, root, issues)
|
|
1167
|
+
);
|
|
1168
|
+
}
|
|
1169
|
+
return;
|
|
1170
|
+
}
|
|
1171
|
+
if (schema.type === "string") {
|
|
1172
|
+
if (typeof value !== "string") {
|
|
1173
|
+
issues.push({ path, message: `must be a string; received ${valueType(value)}` });
|
|
1174
|
+
return;
|
|
1175
|
+
}
|
|
1176
|
+
if (schema.minLength !== void 0 && value.length < schema.minLength) {
|
|
1177
|
+
issues.push({ path, message: `must contain at least ${schema.minLength} character(s)` });
|
|
1178
|
+
}
|
|
1179
|
+
return;
|
|
1180
|
+
}
|
|
1181
|
+
if (schema.type === "boolean") {
|
|
1182
|
+
if (typeof value !== "boolean") {
|
|
1183
|
+
issues.push({ path, message: `must be a boolean; received ${valueType(value)}` });
|
|
1184
|
+
}
|
|
1185
|
+
return;
|
|
1186
|
+
}
|
|
1187
|
+
if (schema.type === "integer") {
|
|
1188
|
+
if (!Number.isInteger(value)) {
|
|
1189
|
+
issues.push({ path, message: `must be an integer; received ${valueType(value)}` });
|
|
1190
|
+
return;
|
|
1191
|
+
}
|
|
1192
|
+
if (schema.minimum !== void 0 && value < schema.minimum) {
|
|
1193
|
+
issues.push({ path, message: `must be at least ${schema.minimum}` });
|
|
1194
|
+
}
|
|
1195
|
+
}
|
|
1196
|
+
}
|
|
1197
|
+
function defaultedConfig(input) {
|
|
1198
|
+
return {
|
|
1199
|
+
...input,
|
|
1200
|
+
$schema: input.$schema === void 0 ? ARK_CONFIG_SCHEMA_URL : input.$schema,
|
|
1201
|
+
schemaVersion: input.schemaVersion === void 0 ? ARK_CONFIG_SCHEMA_VERSION : input.schemaVersion,
|
|
1202
|
+
include: input.include === void 0 ? ["src"] : input.include,
|
|
1203
|
+
layers: input.layers === void 0 ? [] : input.layers,
|
|
1204
|
+
rules: input.rules === void 0 ? DEFAULT_ARK_CONFIG_RULES.map((rule) => ({ ...rule })) : input.rules
|
|
1205
|
+
};
|
|
1206
|
+
}
|
|
1207
|
+
function migrateArkConfig(input, source = "ark.config.json") {
|
|
1208
|
+
if (!isObject(input)) {
|
|
1209
|
+
throw new ArkConfigValidationError(source, [
|
|
1210
|
+
{ path: "$", message: `must be an object; received ${valueType(input)}` }
|
|
1211
|
+
]);
|
|
1212
|
+
}
|
|
1213
|
+
const migratedFrom = input.schemaVersion === void 0 ? "unversioned" : null;
|
|
1214
|
+
if (input.schemaVersion !== void 0 && input.schemaVersion !== ARK_CONFIG_SCHEMA_VERSION) {
|
|
1215
|
+
throw new ArkConfigValidationError(source, [
|
|
1216
|
+
{
|
|
1217
|
+
path: "$.schemaVersion",
|
|
1218
|
+
message: `unsupported version ${JSON.stringify(input.schemaVersion)}; expected ${ARK_CONFIG_SCHEMA_VERSION}`
|
|
3095
1219
|
}
|
|
3096
|
-
|
|
3097
|
-
|
|
3098
|
-
|
|
3099
|
-
|
|
3100
|
-
|
|
3101
|
-
|
|
3102
|
-
|
|
3103
|
-
|
|
3104
|
-
|
|
3105
|
-
|
|
3106
|
-
|
|
3107
|
-
|
|
3108
|
-
|
|
3109
|
-
|
|
3110
|
-
|
|
3111
|
-
|
|
3112
|
-
|
|
3113
|
-
|
|
1220
|
+
]);
|
|
1221
|
+
}
|
|
1222
|
+
return { candidate: defaultedConfig(input), migratedFrom };
|
|
1223
|
+
}
|
|
1224
|
+
function loadArkConfigContract(input, source = "ark.config.json") {
|
|
1225
|
+
const { candidate, migratedFrom } = migrateArkConfig(input, source);
|
|
1226
|
+
const issues = [];
|
|
1227
|
+
validateNode(
|
|
1228
|
+
candidate,
|
|
1229
|
+
ARK_CONFIG_SCHEMA,
|
|
1230
|
+
"$",
|
|
1231
|
+
ARK_CONFIG_SCHEMA,
|
|
1232
|
+
issues
|
|
1233
|
+
);
|
|
1234
|
+
if (issues.length > 0) throw new ArkConfigValidationError(source, issues);
|
|
1235
|
+
return { config: candidate, migratedFrom };
|
|
1236
|
+
}
|
|
1237
|
+
function parseArkConfigJson(json, source = "ark.config.json") {
|
|
1238
|
+
let input;
|
|
1239
|
+
try {
|
|
1240
|
+
input = JSON.parse(json);
|
|
1241
|
+
} catch (error) {
|
|
1242
|
+
throw new ArkConfigValidationError(source, [
|
|
1243
|
+
{
|
|
1244
|
+
path: "$",
|
|
1245
|
+
message: `invalid JSON: ${error instanceof Error ? error.message : String(error)}`
|
|
3114
1246
|
}
|
|
3115
|
-
|
|
3116
|
-
|
|
3117
|
-
|
|
3118
|
-
|
|
1247
|
+
]);
|
|
1248
|
+
}
|
|
1249
|
+
return loadArkConfigContract(input, source);
|
|
1250
|
+
}
|
|
1251
|
+
function withArkConfigMetadata(config) {
|
|
1252
|
+
const result = {
|
|
1253
|
+
$schema: typeof config.$schema === "string" && config.$schema.length > 0 ? config.$schema : ARK_CONFIG_SCHEMA_URL,
|
|
1254
|
+
schemaVersion: ARK_CONFIG_SCHEMA_VERSION
|
|
1255
|
+
};
|
|
1256
|
+
for (const [key, value] of Object.entries(config)) {
|
|
1257
|
+
if (key !== "$schema" && key !== "schemaVersion") result[key] = value;
|
|
1258
|
+
}
|
|
1259
|
+
return result;
|
|
1260
|
+
}
|
|
1261
|
+
|
|
1262
|
+
// src/kernel/layers/ArchitectureProfile.ts
|
|
1263
|
+
function normalizePrefix(prefix) {
|
|
1264
|
+
return prefix.endsWith(".") ? prefix : `${prefix}.`;
|
|
1265
|
+
}
|
|
1266
|
+
function byLongestPrefix(a, b) {
|
|
1267
|
+
const maxA = a.prefixes.length ? Math.max(...a.prefixes.map((p) => p.length)) : 0;
|
|
1268
|
+
const maxB = b.prefixes.length ? Math.max(...b.prefixes.map((p) => p.length)) : 0;
|
|
1269
|
+
return maxB - maxA;
|
|
1270
|
+
}
|
|
1271
|
+
function createArchitectureProfile(options) {
|
|
1272
|
+
const layers = options.layers.map((layer) => ({
|
|
1273
|
+
...layer,
|
|
1274
|
+
prefixes: layer.prefixes.map(normalizePrefix)
|
|
1275
|
+
}));
|
|
1276
|
+
const sortedLayers = [...layers].sort(byLongestPrefix);
|
|
1277
|
+
const rules = [...options.rules ?? []];
|
|
1278
|
+
return {
|
|
1279
|
+
name: options.name,
|
|
1280
|
+
layers,
|
|
1281
|
+
rules,
|
|
1282
|
+
resolveLayer(name) {
|
|
1283
|
+
return layers.find((layer) => layer.match?.(name))?.name ?? sortedLayers.find(
|
|
1284
|
+
(layer) => layer.prefixes.some((prefix) => name.startsWith(prefix))
|
|
1285
|
+
)?.name;
|
|
3119
1286
|
}
|
|
3120
1287
|
};
|
|
3121
1288
|
}
|
|
1289
|
+
function createArchitectureProfileFromArkConfig(config, options = {}) {
|
|
1290
|
+
return createArchitectureProfile({
|
|
1291
|
+
name: options.name ?? config.name ?? "ark.config.json",
|
|
1292
|
+
layers: config.layers.map((layer, index) => ({
|
|
1293
|
+
name: layer.name,
|
|
1294
|
+
prefixes: layer.intentPrefixes ?? [],
|
|
1295
|
+
description: layer.description,
|
|
1296
|
+
order: index + 1
|
|
1297
|
+
})),
|
|
1298
|
+
rules: config.rules ?? []
|
|
1299
|
+
});
|
|
1300
|
+
}
|
|
1301
|
+
var elevenLayerProfileLayers = [
|
|
1302
|
+
{
|
|
1303
|
+
name: "DomainModel",
|
|
1304
|
+
prefixes: ["Domain"],
|
|
1305
|
+
description: "Rich domain model, business rules, and domain events.",
|
|
1306
|
+
order: 1
|
|
1307
|
+
},
|
|
1308
|
+
{
|
|
1309
|
+
name: "ApplicationOrchestration",
|
|
1310
|
+
prefixes: ["Application"],
|
|
1311
|
+
description: "Use cases and command orchestration.",
|
|
1312
|
+
order: 2
|
|
1313
|
+
},
|
|
1314
|
+
{
|
|
1315
|
+
name: "PersistenceAdapters",
|
|
1316
|
+
prefixes: ["Adapter.Persistence", "Adapter.Repository"],
|
|
1317
|
+
description: "Database, repository, and storage adapters.",
|
|
1318
|
+
order: 3
|
|
1319
|
+
},
|
|
1320
|
+
{
|
|
1321
|
+
name: "IntegrationAdapters",
|
|
1322
|
+
prefixes: ["Adapter.Integration", "Adapter.External"],
|
|
1323
|
+
description: "External systems, APIs, and integration adapters.",
|
|
1324
|
+
order: 4
|
|
1325
|
+
},
|
|
1326
|
+
{
|
|
1327
|
+
name: "WorkflowSagaEngine",
|
|
1328
|
+
prefixes: ["Workflow"],
|
|
1329
|
+
description: "Sagas, workflows, and long-running processes.",
|
|
1330
|
+
order: 5
|
|
1331
|
+
},
|
|
1332
|
+
{
|
|
1333
|
+
name: "BackgroundJobsScheduling",
|
|
1334
|
+
prefixes: ["Job"],
|
|
1335
|
+
description: "Background jobs, scheduled work, and async processors.",
|
|
1336
|
+
order: 6
|
|
1337
|
+
},
|
|
1338
|
+
{
|
|
1339
|
+
name: "PresentationAdapters",
|
|
1340
|
+
prefixes: ["Presentation", "Adapter.Presentation", "Adapter.Api"],
|
|
1341
|
+
description: "API, UI, controller, and presentation adapters.",
|
|
1342
|
+
order: 7
|
|
1343
|
+
},
|
|
1344
|
+
{
|
|
1345
|
+
name: "ReportingReadModels",
|
|
1346
|
+
prefixes: ["Reporting"],
|
|
1347
|
+
description: "Read models, projections, and reporting surfaces.",
|
|
1348
|
+
order: 8
|
|
1349
|
+
},
|
|
1350
|
+
{
|
|
1351
|
+
name: "ExtensibilityMetadata",
|
|
1352
|
+
prefixes: ["Metadata"],
|
|
1353
|
+
description: "Metadata, extensions, and schema contracts.",
|
|
1354
|
+
order: 9
|
|
1355
|
+
},
|
|
1356
|
+
{
|
|
1357
|
+
name: "SecurityAuditObservability",
|
|
1358
|
+
prefixes: ["Security", "Audit", "Observability"],
|
|
1359
|
+
description: "Security, audit, and observability concerns.",
|
|
1360
|
+
order: 10
|
|
1361
|
+
},
|
|
1362
|
+
{
|
|
1363
|
+
name: "Kernel",
|
|
1364
|
+
prefixes: ["Kernel"],
|
|
1365
|
+
description: "Ark-owned governance and kernel signals.",
|
|
1366
|
+
order: 11
|
|
1367
|
+
}
|
|
1368
|
+
];
|
|
1369
|
+
var elevenLayerProfile = createArchitectureProfile({
|
|
1370
|
+
name: "Ark 11-layer Hexagonal Event-Driven Profile",
|
|
1371
|
+
layers: elevenLayerProfileLayers,
|
|
1372
|
+
rules: DEFAULT_ARK_CONFIG_RULES.map((rule) => ({ ...rule }))
|
|
1373
|
+
});
|
|
1374
|
+
var defaultElevenLayerDirectories = {
|
|
1375
|
+
DomainModel: ["domain"],
|
|
1376
|
+
ApplicationOrchestration: ["application", "app"],
|
|
1377
|
+
PersistenceAdapters: [
|
|
1378
|
+
"adapters/persistence",
|
|
1379
|
+
"adapters/repository",
|
|
1380
|
+
"repositories",
|
|
1381
|
+
"infra/persistence"
|
|
1382
|
+
],
|
|
1383
|
+
IntegrationAdapters: ["adapters/integration", "adapters/external", "integrations"],
|
|
1384
|
+
WorkflowSagaEngine: ["workflows", "sagas"],
|
|
1385
|
+
BackgroundJobsScheduling: ["jobs", "schedules"],
|
|
1386
|
+
PresentationAdapters: ["presentation", "adapters/presentation", "adapters/api"],
|
|
1387
|
+
ReportingReadModels: ["reporting", "read-models", "projections"],
|
|
1388
|
+
ExtensibilityMetadata: ["metadata", "extensions"],
|
|
1389
|
+
SecurityAuditObservability: ["security", "audit", "observability"],
|
|
1390
|
+
Kernel: ["kernel"]
|
|
1391
|
+
};
|
|
1392
|
+
function createElevenLayerArkConfig(options = {}) {
|
|
1393
|
+
const rootDir = options.rootDir ?? "src";
|
|
1394
|
+
const optional = options.optionalLayers ?? true;
|
|
1395
|
+
const prefix = rootDir === "." ? "" : `${rootDir}/`;
|
|
1396
|
+
return withArkConfigMetadata({
|
|
1397
|
+
include: options.include ?? [rootDir],
|
|
1398
|
+
layers: elevenLayerProfile.layers.map((layer) => ({
|
|
1399
|
+
name: layer.name,
|
|
1400
|
+
patterns: (defaultElevenLayerDirectories[layer.name] ?? [layer.name]).map(
|
|
1401
|
+
(directory) => `${prefix}${directory}/**`
|
|
1402
|
+
),
|
|
1403
|
+
intentPrefixes: layer.prefixes,
|
|
1404
|
+
optional
|
|
1405
|
+
})),
|
|
1406
|
+
rules: [...elevenLayerProfile.rules]
|
|
1407
|
+
});
|
|
1408
|
+
}
|
|
3122
1409
|
|
|
3123
1410
|
// src/domain/analysis.ts
|
|
3124
1411
|
var ANALYSIS_IR_SCHEMA_VERSION = "1.0";
|
|
@@ -3327,599 +1614,295 @@ function explainViolation(violation2) {
|
|
|
3327
1614
|
const target = violation2.edge.to ?? violation2.edge.specifier;
|
|
3328
1615
|
return `${violation2.ruleId} at ${location}: ${violation2.edge.from} imports ${target}. ${violation2.message}`;
|
|
3329
1616
|
}
|
|
3330
|
-
|
|
3331
|
-
|
|
3332
|
-
|
|
3333
|
-
|
|
3334
|
-
|
|
3335
|
-
|
|
3336
|
-
|
|
3337
|
-
|
|
3338
|
-
|
|
3339
|
-
|
|
3340
|
-
|
|
3341
|
-
|
|
3342
|
-
|
|
3343
|
-
|
|
3344
|
-
|
|
1617
|
+
function detectArchitectureCycles(graph) {
|
|
1618
|
+
let index = 0;
|
|
1619
|
+
const indices = /* @__PURE__ */ new Map();
|
|
1620
|
+
const low = /* @__PURE__ */ new Map();
|
|
1621
|
+
const onStack = /* @__PURE__ */ new Set();
|
|
1622
|
+
const stack = [];
|
|
1623
|
+
const components = [];
|
|
1624
|
+
const connect = (file) => {
|
|
1625
|
+
indices.set(file, index);
|
|
1626
|
+
low.set(file, index);
|
|
1627
|
+
index += 1;
|
|
1628
|
+
stack.push(file);
|
|
1629
|
+
onStack.add(file);
|
|
1630
|
+
for (const target of [...graph.get(file) ?? []].sort()) {
|
|
1631
|
+
if (!graph.has(target)) continue;
|
|
1632
|
+
if (!indices.has(target)) {
|
|
1633
|
+
connect(target);
|
|
1634
|
+
low.set(file, Math.min(low.get(file) ?? 0, low.get(target) ?? 0));
|
|
1635
|
+
} else if (onStack.has(target)) {
|
|
1636
|
+
low.set(file, Math.min(low.get(file) ?? 0, indices.get(target) ?? 0));
|
|
1637
|
+
}
|
|
3345
1638
|
}
|
|
3346
|
-
|
|
3347
|
-
|
|
3348
|
-
|
|
3349
|
-
|
|
3350
|
-
|
|
3351
|
-
|
|
3352
|
-
|
|
3353
|
-
|
|
3354
|
-
|
|
3355
|
-
|
|
3356
|
-
|
|
3357
|
-
|
|
3358
|
-
|
|
3359
|
-
}
|
|
3360
|
-
|
|
3361
|
-
|
|
3362
|
-
|
|
1639
|
+
if (low.get(file) !== indices.get(file)) return;
|
|
1640
|
+
const component = [];
|
|
1641
|
+
let member;
|
|
1642
|
+
do {
|
|
1643
|
+
member = stack.pop();
|
|
1644
|
+
if (member === void 0) break;
|
|
1645
|
+
onStack.delete(member);
|
|
1646
|
+
component.push(member);
|
|
1647
|
+
} while (member !== file);
|
|
1648
|
+
if (component.length > 1) components.push(component.sort());
|
|
1649
|
+
};
|
|
1650
|
+
for (const file of [...graph.keys()].sort()) {
|
|
1651
|
+
if (!indices.has(file)) connect(file);
|
|
1652
|
+
}
|
|
1653
|
+
return components.sort((left, right) => left[0].localeCompare(right[0])).map((members) => ({
|
|
1654
|
+
ruleId: "CIRCULAR_DEPENDENCY",
|
|
1655
|
+
file: members[0],
|
|
1656
|
+
line: 1,
|
|
1657
|
+
target: members.join(" \u2192 "),
|
|
1658
|
+
message: `Circular dependency among ${members.length} files: ${members.join(" \u2192 ")} \u2192 ${members[0]}.`,
|
|
1659
|
+
cycleKind: "value"
|
|
1660
|
+
}));
|
|
1661
|
+
}
|
|
1662
|
+
function evaluateArchitectureGraph(input) {
|
|
1663
|
+
const violations = input.contentViolations.map((violation2) => ({ ...violation2 }));
|
|
1664
|
+
const warnings = (input.warnings ?? []).map((warning) => ({ ...warning }));
|
|
1665
|
+
const graph = new Map(
|
|
1666
|
+
input.files.map((file) => [file, /* @__PURE__ */ new Set()])
|
|
1667
|
+
);
|
|
1668
|
+
for (const edge of input.edges) {
|
|
1669
|
+
if (edge.to && edge.to !== edge.from && !edge.typeOnly && graph.has(edge.from)) {
|
|
1670
|
+
graph.get(edge.from)?.add(edge.to);
|
|
3363
1671
|
}
|
|
3364
|
-
|
|
3365
|
-
|
|
3366
|
-
|
|
3367
|
-
|
|
1672
|
+
if (!edge.to || !edge.toLayer) continue;
|
|
1673
|
+
const rule = findDeniedEdgeRule(input.rules, edge.fromLayer, edge.toLayer, {
|
|
1674
|
+
fromPath: edge.from,
|
|
1675
|
+
toPath: edge.to,
|
|
1676
|
+
layers: input.config.layers
|
|
1677
|
+
});
|
|
1678
|
+
if (!rule) continue;
|
|
1679
|
+
const peerIsolation = Boolean(rule.peerIsolation);
|
|
1680
|
+
violations.push({
|
|
1681
|
+
ruleId: "LAYER_IMPORT_VIOLATION",
|
|
1682
|
+
file: edge.from,
|
|
1683
|
+
line: edge.line,
|
|
1684
|
+
fromLayer: edge.fromLayer,
|
|
1685
|
+
toLayer: edge.toLayer,
|
|
1686
|
+
target: edge.to,
|
|
1687
|
+
...edge.typeOnly ? { typeOnly: true } : {},
|
|
1688
|
+
...edge.targetTypeOnlyExports ? { targetTypeOnlyExports: true } : {},
|
|
1689
|
+
...edge.sourcePureTypeModule ? { sourcePureTypeModule: true } : {},
|
|
1690
|
+
...edge.namedBindingsTypeOnly ? { namedBindingsTypeOnly: true } : {},
|
|
1691
|
+
...!peerIsolation && edge.portProofEligible ? { portProofEligible: true } : {},
|
|
1692
|
+
...edge.kind ? { edgeKind: edge.kind } : {},
|
|
1693
|
+
...peerIsolation ? { peerIsolation: true } : {},
|
|
1694
|
+
message: rule.message ?? (peerIsolation ? `${edge.fromLayer} must not ${edge.kind} another slice of ${edge.toLayer} (${edge.from} \u2192 ${edge.to}). Extract shared code or use events/ports across slices.` : `${edge.fromLayer} must not ${edge.kind} ${edge.toLayer}.`)
|
|
3368
1695
|
});
|
|
3369
1696
|
}
|
|
3370
|
-
|
|
3371
|
-
|
|
3372
|
-
|
|
3373
|
-
|
|
3374
|
-
|
|
3375
|
-
|
|
3376
|
-
|
|
3377
|
-
|
|
3378
|
-
|
|
3379
|
-
|
|
3380
|
-
|
|
3381
|
-
|
|
3382
|
-
|
|
3383
|
-
appliedCount: (previous?.appliedCount ?? 0) + 1,
|
|
3384
|
-
lastIntent: event.intent,
|
|
3385
|
-
lastCorrelationId: event.metadata.correlationId,
|
|
3386
|
-
updatedAt: (/* @__PURE__ */ new Date()).toISOString()
|
|
3387
|
-
});
|
|
3388
|
-
await this.auditTrail?.record({
|
|
3389
|
-
type: "projection.applied",
|
|
3390
|
-
source: "Kernel.ProjectionRegistry",
|
|
3391
|
-
intent: event.intent,
|
|
3392
|
-
correlationId: event.metadata.correlationId,
|
|
3393
|
-
causationId: event.metadata.causationId,
|
|
3394
|
-
subject: definition.name,
|
|
3395
|
-
details: { projection: definition.name }
|
|
3396
|
-
});
|
|
3397
|
-
applied.push(definition.name);
|
|
3398
|
-
}
|
|
3399
|
-
return applied;
|
|
3400
|
-
}
|
|
3401
|
-
async getState(name) {
|
|
3402
|
-
return this.store.load(name);
|
|
3403
|
-
}
|
|
3404
|
-
getCheckpoint(name) {
|
|
3405
|
-
return this.checkpoints.get(name);
|
|
3406
|
-
}
|
|
3407
|
-
getCheckpoints() {
|
|
3408
|
-
return Array.from(this.checkpoints.values());
|
|
3409
|
-
}
|
|
3410
|
-
async clear() {
|
|
3411
|
-
await this.store.clear();
|
|
3412
|
-
for (const definition of this.definitions.values()) {
|
|
3413
|
-
this.checkpoints.set(definition.name, {
|
|
3414
|
-
projection: definition.name,
|
|
3415
|
-
appliedCount: 0
|
|
3416
|
-
});
|
|
1697
|
+
const cyclePolicy = String(input.config.cyclePolicy ?? "strict").toLowerCase();
|
|
1698
|
+
if (cyclePolicy !== "off") {
|
|
1699
|
+
const cycles = detectArchitectureCycles(graph);
|
|
1700
|
+
if (cyclePolicy === "soft" || cyclePolicy === "framework-soft") {
|
|
1701
|
+
warnings.push(
|
|
1702
|
+
...cycles.map((cycle) => ({
|
|
1703
|
+
...cycle,
|
|
1704
|
+
message: `${cycle.message} (soft cycle policy \u2014 advisory only; set cyclePolicy: "strict" to fail the check)`,
|
|
1705
|
+
failsStrict: false
|
|
1706
|
+
}))
|
|
1707
|
+
);
|
|
1708
|
+
} else {
|
|
1709
|
+
violations.push(...cycles);
|
|
3417
1710
|
}
|
|
3418
1711
|
}
|
|
3419
|
-
};
|
|
3420
|
-
function createProjectionRegistry(options) {
|
|
3421
|
-
return new ProjectionRegistryImpl(options);
|
|
3422
|
-
}
|
|
3423
|
-
|
|
3424
|
-
// src/kernel/manifest/constants.ts
|
|
3425
|
-
var MANIFEST_SCHEMA_VERSION = "1.0";
|
|
3426
|
-
|
|
3427
|
-
// src/kernel/manifest/createArkManifest.ts
|
|
3428
|
-
function policyId(name) {
|
|
3429
|
-
return name.toLowerCase().replace(/[^a-z0-9]+/g, "-").replace(/^-|-$/g, "");
|
|
3430
|
-
}
|
|
3431
|
-
var ArkManifestImpl = class {
|
|
3432
|
-
constructor(data) {
|
|
3433
|
-
this.data = data;
|
|
3434
|
-
}
|
|
3435
|
-
data;
|
|
3436
|
-
toJSON() {
|
|
3437
|
-
return { ...this.data };
|
|
3438
|
-
}
|
|
3439
|
-
};
|
|
3440
|
-
function createArkManifest(options = {}) {
|
|
3441
|
-
const registry = options.registry;
|
|
3442
|
-
const policyEngine = options.policyEngine;
|
|
3443
|
-
const metadata = options.metadata;
|
|
3444
|
-
const graph = options.graph;
|
|
3445
|
-
const profile = options.profile;
|
|
3446
|
-
const projections = options.projections;
|
|
3447
|
-
const eventContracts = options.eventContracts;
|
|
3448
|
-
const observability = options.observability;
|
|
3449
|
-
const intents = registry ? registry.list().map((creator) => ({
|
|
3450
|
-
name: creator.name,
|
|
3451
|
-
dependencies: registry.getDependencies(creator.name),
|
|
3452
|
-
productions: registry.getProductions(creator.name)
|
|
3453
|
-
})) : [];
|
|
3454
|
-
const relationships = registry ? registry.getAllRelationships() : [];
|
|
3455
|
-
const policies = policyEngine ? policyEngine.getPolicies().map((p) => ({
|
|
3456
|
-
id: policyId(p.name),
|
|
3457
|
-
name: p.name,
|
|
3458
|
-
severity: p.severity,
|
|
3459
|
-
tags: p.tags ? [...p.tags] : void 0,
|
|
3460
|
-
owner: p.owner,
|
|
3461
|
-
version: p.version,
|
|
3462
|
-
rationale: p.rationale,
|
|
3463
|
-
enforcementMode: p.enforcementMode,
|
|
3464
|
-
deprecated: p.deprecated,
|
|
3465
|
-
replacedBy: p.replacedBy,
|
|
3466
|
-
description: p.tags?.includes("layer") ? "Enforces clean-architecture layer dependency rules on declared relationships." : void 0
|
|
3467
|
-
})) : [];
|
|
3468
|
-
const entities = metadata ? metadata.toJSON() : [];
|
|
3469
|
-
const graphData = graph ? graph.toJSON() : { nodes: [], edges: [] };
|
|
3470
|
-
const entityIntents = entities.filter((e) => (e.emits?.length ?? 0) > 0 || (e.consumes?.length ?? 0) > 0).map((e) => ({
|
|
3471
|
-
entity: e.name,
|
|
3472
|
-
emits: e.emits,
|
|
3473
|
-
consumes: e.consumes
|
|
3474
|
-
}));
|
|
3475
|
-
const projectionData = projections ? projections.list().map((projection) => ({
|
|
3476
|
-
name: projection.name,
|
|
3477
|
-
sourceIntents: projection.sourceIntents,
|
|
3478
|
-
checkpoint: projections.getCheckpoint(projection.name)
|
|
3479
|
-
})) : [];
|
|
3480
|
-
const data = {
|
|
3481
|
-
schemaVersion: MANIFEST_SCHEMA_VERSION,
|
|
3482
|
-
version,
|
|
3483
|
-
exportedAt: (/* @__PURE__ */ new Date()).toISOString(),
|
|
3484
|
-
intents,
|
|
3485
|
-
relationships,
|
|
3486
|
-
policies,
|
|
3487
|
-
entities,
|
|
3488
|
-
graph: graphData,
|
|
3489
|
-
architecture: profile ? {
|
|
3490
|
-
profile: profile.name,
|
|
3491
|
-
layers: profile.layers,
|
|
3492
|
-
rules: profile.rules
|
|
3493
|
-
} : void 0,
|
|
3494
|
-
projections: projectionData,
|
|
3495
|
-
eventContracts: eventContracts?.list() ?? [],
|
|
3496
|
-
observability: observability?.report(),
|
|
3497
|
-
links: { entityIntents }
|
|
3498
|
-
};
|
|
3499
|
-
return new ArkManifestImpl(data);
|
|
3500
|
-
}
|
|
3501
|
-
|
|
3502
|
-
// src/kernel/workflow/Saga.ts
|
|
3503
|
-
var workflowSequence = 0;
|
|
3504
|
-
function createWorkflowId(prefix) {
|
|
3505
|
-
workflowSequence += 1;
|
|
3506
|
-
return `${prefix}-${Date.now()}-${workflowSequence}`;
|
|
3507
|
-
}
|
|
3508
|
-
function errorMessage(error) {
|
|
3509
|
-
return error instanceof Error ? error.message : String(error);
|
|
3510
|
-
}
|
|
3511
|
-
function sleep(ms) {
|
|
3512
|
-
return new Promise((resolve) => {
|
|
3513
|
-
setTimeout(resolve, ms);
|
|
3514
|
-
});
|
|
1712
|
+
return { violations, warnings, safety: input.safety };
|
|
3515
1713
|
}
|
|
3516
|
-
|
|
3517
|
-
|
|
3518
|
-
if (timeoutMs === void 0) return operation(controller.signal);
|
|
3519
|
-
let timeout;
|
|
3520
|
-
const timeoutPromise = new Promise((_, reject) => {
|
|
3521
|
-
timeout = setTimeout(() => {
|
|
3522
|
-
const error = new Error(`Workflow step "${stepName}" timed out after ${timeoutMs}ms.`);
|
|
3523
|
-
controller.abort(error);
|
|
3524
|
-
reject(error);
|
|
3525
|
-
}, timeoutMs);
|
|
3526
|
-
});
|
|
3527
|
-
try {
|
|
3528
|
-
return await Promise.race([operation(controller.signal), timeoutPromise]);
|
|
3529
|
-
} finally {
|
|
3530
|
-
if (timeout) clearTimeout(timeout);
|
|
3531
|
-
}
|
|
1714
|
+
function configWarning(ruleId, message, extra = {}) {
|
|
1715
|
+
return { ruleId, message, ...extra };
|
|
3532
1716
|
}
|
|
3533
|
-
|
|
3534
|
-
|
|
3535
|
-
|
|
3536
|
-
|
|
3537
|
-
|
|
3538
|
-
|
|
3539
|
-
|
|
3540
|
-
|
|
3541
|
-
|
|
3542
|
-
|
|
3543
|
-
return Array.from(this.snapshots.values()).filter((snapshot) => !workflowName || snapshot.workflowName === workflowName).map((snapshot) => ({ ...snapshot, context: { ...snapshot.context } }));
|
|
3544
|
-
}
|
|
3545
|
-
clear() {
|
|
3546
|
-
this.snapshots.clear();
|
|
3547
|
-
}
|
|
3548
|
-
};
|
|
3549
|
-
var WorkflowEngineImpl = class {
|
|
3550
|
-
constructor(bus, options = {}) {
|
|
3551
|
-
this.bus = bus;
|
|
3552
|
-
this.options = options;
|
|
3553
|
-
this.store = options.store ?? new InMemoryWorkflowStore();
|
|
1717
|
+
function collectAnalysisConfigWarnings(input) {
|
|
1718
|
+
const { config, rules, files, manifest } = input;
|
|
1719
|
+
const warnings = [];
|
|
1720
|
+
if (config.dynamicImportAllowlist !== void 0 && (!Array.isArray(config.dynamicImportAllowlist) || config.dynamicImportAllowlist.some((entry) => typeof entry !== "string"))) {
|
|
1721
|
+
warnings.push(
|
|
1722
|
+
configWarning(
|
|
1723
|
+
"CONFIG_INVALID_DYNAMIC_IMPORT_ALLOWLIST",
|
|
1724
|
+
"dynamicImportAllowlist must be an array of file globs."
|
|
1725
|
+
)
|
|
1726
|
+
);
|
|
3554
1727
|
}
|
|
3555
|
-
|
|
3556
|
-
|
|
3557
|
-
|
|
3558
|
-
|
|
3559
|
-
|
|
3560
|
-
|
|
3561
|
-
|
|
3562
|
-
|
|
3563
|
-
|
|
3564
|
-
|
|
3565
|
-
|
|
3566
|
-
throw new Error(
|
|
3567
|
-
`Workflow "${definition.name}" has duplicate step name "${step.name}".`
|
|
1728
|
+
if (config.safety !== void 0 && (config.safety === null || typeof config.safety !== "object" || Array.isArray(config.safety))) {
|
|
1729
|
+
warnings.push(configWarning("CONFIG_INVALID_SAFETY", "safety must be an object."));
|
|
1730
|
+
} else if (config.safety) {
|
|
1731
|
+
for (const key of ["maxTsSuppressions", "maxAnyCasts"]) {
|
|
1732
|
+
const value = config.safety[key];
|
|
1733
|
+
if (value !== void 0 && (!Number.isInteger(value) || value < 0)) {
|
|
1734
|
+
warnings.push(
|
|
1735
|
+
configWarning(
|
|
1736
|
+
"CONFIG_INVALID_SAFETY_THRESHOLD",
|
|
1737
|
+
`safety.${key} must be a non-negative integer.`
|
|
1738
|
+
)
|
|
3568
1739
|
);
|
|
3569
1740
|
}
|
|
3570
|
-
names.add(step.name);
|
|
3571
|
-
}
|
|
3572
|
-
this.definitions.set(definition.name, definition);
|
|
3573
|
-
if (definition.startOn) {
|
|
3574
|
-
const trigger = definition.startOn;
|
|
3575
|
-
this.bus.subscribe(trigger.intent, async (event) => {
|
|
3576
|
-
await this.start(definition.name, trigger.mapEventToPayload(event));
|
|
3577
|
-
});
|
|
3578
1741
|
}
|
|
3579
1742
|
}
|
|
3580
|
-
|
|
3581
|
-
|
|
3582
|
-
|
|
3583
|
-
|
|
1743
|
+
const layers = Array.isArray(config.layers) ? config.layers : [];
|
|
1744
|
+
const manifestLayers = Array.isArray(manifest?.architecture?.layers) ? manifest.architecture.layers : [];
|
|
1745
|
+
const knownLayers = /* @__PURE__ */ new Set([
|
|
1746
|
+
...layers.map((layer) => layer.name).filter(Boolean),
|
|
1747
|
+
...manifestLayers.map((layer) => layer.name).filter((name) => Boolean(name))
|
|
1748
|
+
]);
|
|
1749
|
+
if (layers.length === 0) {
|
|
1750
|
+
warnings.push(
|
|
1751
|
+
configWarning(
|
|
1752
|
+
"CONFIG_NO_LAYERS",
|
|
1753
|
+
"No file layers are configured; ark-check cannot classify files for import-boundary enforcement."
|
|
1754
|
+
)
|
|
1755
|
+
);
|
|
1756
|
+
}
|
|
1757
|
+
const seenLayers = /* @__PURE__ */ new Set();
|
|
1758
|
+
const duplicateLayers = /* @__PURE__ */ new Set();
|
|
1759
|
+
for (const layer of layers) {
|
|
1760
|
+
if (!layer.name) {
|
|
1761
|
+
warnings.push(configWarning("CONFIG_LAYER_WITHOUT_NAME", "A configured layer is missing a name."));
|
|
1762
|
+
continue;
|
|
3584
1763
|
}
|
|
3585
|
-
|
|
3586
|
-
|
|
3587
|
-
|
|
3588
|
-
|
|
3589
|
-
|
|
3590
|
-
|
|
3591
|
-
|
|
3592
|
-
|
|
3593
|
-
|
|
3594
|
-
|
|
3595
|
-
};
|
|
3596
|
-
await this.store.save(snapshot);
|
|
3597
|
-
await this.audit("workflow.started", snapshot, { workflowName });
|
|
3598
|
-
try {
|
|
3599
|
-
for (const step of definition.steps) {
|
|
3600
|
-
await this.runStep(snapshot, step);
|
|
3601
|
-
}
|
|
3602
|
-
snapshot.status = "completed";
|
|
3603
|
-
snapshot.currentStep = void 0;
|
|
3604
|
-
snapshot.completedAt = (/* @__PURE__ */ new Date()).toISOString();
|
|
3605
|
-
snapshot.updatedAt = snapshot.completedAt;
|
|
3606
|
-
await this.store.save(snapshot);
|
|
3607
|
-
await this.audit("workflow.completed", snapshot, { workflowName });
|
|
3608
|
-
return { ...snapshot, context: { ...snapshot.context } };
|
|
3609
|
-
} catch (err) {
|
|
3610
|
-
await this.compensate(snapshot, definition.steps, err);
|
|
3611
|
-
snapshot.status = "failed";
|
|
3612
|
-
snapshot.currentStep = void 0;
|
|
3613
|
-
snapshot.error = errorMessage(err);
|
|
3614
|
-
snapshot.updatedAt = (/* @__PURE__ */ new Date()).toISOString();
|
|
3615
|
-
await this.store.save(snapshot);
|
|
3616
|
-
await this.audit("workflow.failed", snapshot, {
|
|
3617
|
-
workflowName,
|
|
3618
|
-
error: snapshot.error,
|
|
3619
|
-
failedStep: snapshot.failedStep
|
|
3620
|
-
});
|
|
3621
|
-
throw err;
|
|
1764
|
+
if (seenLayers.has(layer.name)) duplicateLayers.add(layer.name);
|
|
1765
|
+
seenLayers.add(layer.name);
|
|
1766
|
+
if (layer.forbiddenGlobals !== void 0 && (!Array.isArray(layer.forbiddenGlobals) || layer.forbiddenGlobals.some((entry) => typeof entry !== "string"))) {
|
|
1767
|
+
warnings.push(
|
|
1768
|
+
configWarning(
|
|
1769
|
+
"CONFIG_INVALID_FORBIDDEN_GLOBALS",
|
|
1770
|
+
`Layer "${layer.name}" has an invalid forbiddenGlobals value; expected an array of strings (e.g. ["fetch", "Date.now"]). The entry is ignored.`,
|
|
1771
|
+
{ layer: layer.name }
|
|
1772
|
+
)
|
|
1773
|
+
);
|
|
3622
1774
|
}
|
|
3623
|
-
|
|
3624
|
-
|
|
3625
|
-
|
|
3626
|
-
|
|
3627
|
-
|
|
3628
|
-
|
|
3629
|
-
|
|
3630
|
-
|
|
3631
|
-
|
|
3632
|
-
|
|
3633
|
-
|
|
3634
|
-
|
|
3635
|
-
|
|
3636
|
-
for (let attempt = 1; attempt <= maxAttempts; attempt += 1) {
|
|
3637
|
-
snapshot.attempts[step.name] = attempt;
|
|
3638
|
-
await this.store.save(snapshot);
|
|
3639
|
-
let result;
|
|
1775
|
+
const patterns = Array.isArray(layer.patterns) ? layer.patterns : [];
|
|
1776
|
+
if (patterns.length === 0) {
|
|
1777
|
+
warnings.push(
|
|
1778
|
+
configWarning(
|
|
1779
|
+
"CONFIG_LAYER_WITHOUT_PATTERNS",
|
|
1780
|
+
`Layer "${layer.name}" has no file patterns and will never classify files.`,
|
|
1781
|
+
{ layer: layer.name }
|
|
1782
|
+
)
|
|
1783
|
+
);
|
|
1784
|
+
continue;
|
|
1785
|
+
}
|
|
1786
|
+
for (const pattern of patterns) {
|
|
1787
|
+
let expression;
|
|
3640
1788
|
try {
|
|
3641
|
-
|
|
3642
|
-
|
|
3643
|
-
|
|
3644
|
-
|
|
1789
|
+
expression = globToRegExp(pattern);
|
|
1790
|
+
} catch (error) {
|
|
1791
|
+
warnings.push(
|
|
1792
|
+
configWarning(
|
|
1793
|
+
"CONFIG_INVALID_LAYER_PATTERN",
|
|
1794
|
+
`Layer "${layer.name}" has an invalid pattern "${pattern}": ${error instanceof Error ? error.message : String(error)}`,
|
|
1795
|
+
{ layer: layer.name, pattern }
|
|
1796
|
+
)
|
|
1797
|
+
);
|
|
1798
|
+
continue;
|
|
1799
|
+
}
|
|
1800
|
+
if (!files.some((file) => expression.test(file)) && !layer.optional) {
|
|
1801
|
+
warnings.push(
|
|
1802
|
+
configWarning(
|
|
1803
|
+
"CONFIG_LAYER_PATTERN_NO_MATCHES",
|
|
1804
|
+
`Layer "${layer.name}" pattern "${pattern}" matched no included files.`,
|
|
1805
|
+
{ layer: layer.name, pattern, failsStrict: false }
|
|
1806
|
+
)
|
|
3645
1807
|
);
|
|
3646
|
-
} catch (err) {
|
|
3647
|
-
if (attempt < maxAttempts) {
|
|
3648
|
-
if (retry.delayMs) await sleep(retry.delayMs);
|
|
3649
|
-
continue;
|
|
3650
|
-
}
|
|
3651
|
-
snapshot.failedStep = step.name;
|
|
3652
|
-
snapshot.updatedAt = (/* @__PURE__ */ new Date()).toISOString();
|
|
3653
|
-
await this.store.save(snapshot);
|
|
3654
|
-
await this.audit("workflow.step.failed", snapshot, {
|
|
3655
|
-
step: step.name,
|
|
3656
|
-
attempt,
|
|
3657
|
-
error: errorMessage(err)
|
|
3658
|
-
});
|
|
3659
|
-
throw err;
|
|
3660
1808
|
}
|
|
3661
|
-
if (result) Object.assign(snapshot.context, result);
|
|
3662
|
-
snapshot.completedSteps.push(step.name);
|
|
3663
|
-
snapshot.currentStep = void 0;
|
|
3664
|
-
snapshot.updatedAt = (/* @__PURE__ */ new Date()).toISOString();
|
|
3665
|
-
await this.store.save(snapshot);
|
|
3666
|
-
await this.audit("workflow.step.completed", snapshot, {
|
|
3667
|
-
step: step.name,
|
|
3668
|
-
attempt
|
|
3669
|
-
});
|
|
3670
|
-
return;
|
|
3671
1809
|
}
|
|
3672
|
-
throw new Error(`Workflow step "${step.name}" did not complete.`);
|
|
3673
1810
|
}
|
|
3674
|
-
|
|
3675
|
-
|
|
3676
|
-
|
|
3677
|
-
|
|
3678
|
-
|
|
3679
|
-
(step) => snapshot.completedSteps.includes(step.name)
|
|
1811
|
+
for (const name of duplicateLayers) {
|
|
1812
|
+
warnings.push(
|
|
1813
|
+
configWarning("CONFIG_DUPLICATE_LAYER", `Layer "${name}" is configured more than once.`, {
|
|
1814
|
+
layer: name
|
|
1815
|
+
})
|
|
3680
1816
|
);
|
|
3681
|
-
|
|
3682
|
-
|
|
3683
|
-
|
|
3684
|
-
|
|
3685
|
-
|
|
3686
|
-
|
|
3687
|
-
|
|
3688
|
-
|
|
3689
|
-
|
|
3690
|
-
|
|
3691
|
-
|
|
3692
|
-
|
|
3693
|
-
|
|
3694
|
-
|
|
1817
|
+
}
|
|
1818
|
+
if (knownLayers.size > 0) {
|
|
1819
|
+
for (const rule of rules ?? []) {
|
|
1820
|
+
if (rule.from && !knownLayers.has(rule.from)) {
|
|
1821
|
+
warnings.push(
|
|
1822
|
+
configWarning(
|
|
1823
|
+
"CONFIG_RULE_UNKNOWN_FROM_LAYER",
|
|
1824
|
+
`Rule references unknown source layer "${rule.from}".`,
|
|
1825
|
+
{ fromLayer: rule.from, toLayer: rule.to }
|
|
1826
|
+
)
|
|
1827
|
+
);
|
|
1828
|
+
}
|
|
1829
|
+
if (rule.to && !knownLayers.has(rule.to)) {
|
|
1830
|
+
warnings.push(
|
|
1831
|
+
configWarning(
|
|
1832
|
+
"CONFIG_RULE_UNKNOWN_TO_LAYER",
|
|
1833
|
+
`Rule references unknown target layer "${rule.to}".`,
|
|
1834
|
+
{ fromLayer: rule.from, toLayer: rule.to }
|
|
1835
|
+
)
|
|
1836
|
+
);
|
|
3695
1837
|
}
|
|
3696
1838
|
}
|
|
3697
1839
|
}
|
|
3698
|
-
|
|
3699
|
-
|
|
3700
|
-
|
|
3701
|
-
|
|
3702
|
-
|
|
3703
|
-
|
|
3704
|
-
|
|
3705
|
-
|
|
3706
|
-
|
|
3707
|
-
|
|
3708
|
-
|
|
3709
|
-
|
|
3710
|
-
|
|
3711
|
-
|
|
3712
|
-
|
|
3713
|
-
|
|
3714
|
-
const sagaId = options.id ?? createWorkflowId(def.name);
|
|
3715
|
-
let status = "idle";
|
|
3716
|
-
let completedStepNames = [];
|
|
3717
|
-
return {
|
|
3718
|
-
id: sagaId,
|
|
3719
|
-
definition: def,
|
|
3720
|
-
get status() {
|
|
3721
|
-
return status;
|
|
3722
|
-
},
|
|
3723
|
-
get completedSteps() {
|
|
3724
|
-
return [...completedStepNames];
|
|
3725
|
-
},
|
|
3726
|
-
async run(initialPayload) {
|
|
3727
|
-
status = "running";
|
|
3728
|
-
completedStepNames = [];
|
|
3729
|
-
try {
|
|
3730
|
-
const snapshot = await engine.start(def.name, initialPayload, { id: sagaId });
|
|
3731
|
-
status = snapshot.status === "waiting" ? "idle" : snapshot.status;
|
|
3732
|
-
completedStepNames = [...snapshot.completedSteps];
|
|
3733
|
-
} catch (err) {
|
|
3734
|
-
const snapshot = await engine.get(sagaId);
|
|
3735
|
-
status = snapshot?.status === "waiting" ? "idle" : snapshot?.status ?? "failed";
|
|
3736
|
-
completedStepNames = snapshot?.completedSteps ?? [];
|
|
3737
|
-
throw err;
|
|
1840
|
+
const ambiguousPairs = /* @__PURE__ */ new Set();
|
|
1841
|
+
if (layers.length > 1) {
|
|
1842
|
+
for (const file of files) {
|
|
1843
|
+
let topScore = -1;
|
|
1844
|
+
let topLayers = [];
|
|
1845
|
+
for (const layer of layers) {
|
|
1846
|
+
for (const pattern of layer.patterns ?? []) {
|
|
1847
|
+
if (!globToRegExp(pattern).test(file)) continue;
|
|
1848
|
+
const score = patternSpecificity(pattern);
|
|
1849
|
+
if (score > topScore) {
|
|
1850
|
+
topScore = score;
|
|
1851
|
+
topLayers = [layer.name];
|
|
1852
|
+
} else if (score === topScore && !topLayers.includes(layer.name)) {
|
|
1853
|
+
topLayers.push(layer.name);
|
|
1854
|
+
}
|
|
1855
|
+
}
|
|
3738
1856
|
}
|
|
1857
|
+
if (topLayers.length > 1) ambiguousPairs.add([...topLayers].sort().join(" + "));
|
|
3739
1858
|
}
|
|
3740
|
-
}
|
|
3741
|
-
|
|
3742
|
-
|
|
3743
|
-
|
|
3744
|
-
|
|
3745
|
-
|
|
3746
|
-
|
|
3747
|
-
|
|
3748
|
-
|
|
3749
|
-
}
|
|
3750
|
-
|
|
3751
|
-
|
|
3752
|
-
|
|
3753
|
-
|
|
3754
|
-
|
|
3755
|
-
|
|
3756
|
-
|
|
3757
|
-
|
|
3758
|
-
|
|
3759
|
-
|
|
3760
|
-
|
|
3761
|
-
const projections = options.projections ?? createProjectionRegistry({ auditTrail });
|
|
3762
|
-
const policyEngine = new PolicyEngine([
|
|
3763
|
-
defineArchitectureProfilePolicy(profile),
|
|
3764
|
-
...options.policies ?? []
|
|
3765
|
-
]);
|
|
3766
|
-
const syncGraph = () => {
|
|
3767
|
-
syncRegistryToGraph(registry, graph, { requireRegisteredTargets: true });
|
|
3768
|
-
};
|
|
3769
|
-
const eventBus = createEventBus({
|
|
3770
|
-
intentRegistry: registry,
|
|
3771
|
-
dependencyGraph: graph,
|
|
3772
|
-
policyEngine,
|
|
3773
|
-
strictRegistry: true,
|
|
3774
|
-
validateIntentNaming: true,
|
|
3775
|
-
auditTrail,
|
|
3776
|
-
eventContracts,
|
|
3777
|
-
strictEventContracts: options.strictEventContracts ?? strict,
|
|
3778
|
-
requireKnownSource: options.requireKnownSource ?? true,
|
|
3779
|
-
architectureProfile: profile,
|
|
3780
|
-
enforceObservedLayerFlow: options.enforceObservedLayerFlow ?? (strict ? "hard" : "off"),
|
|
3781
|
-
outbox,
|
|
3782
|
-
instanceId,
|
|
3783
|
-
maxHistorySize,
|
|
3784
|
-
onPublish: options.autoApplyProjections === false ? void 0 : async (event) => {
|
|
3785
|
-
await projections.apply(event);
|
|
3786
|
-
}
|
|
3787
|
-
});
|
|
3788
|
-
const workflowEngine = createWorkflowEngine(eventBus, { auditTrail });
|
|
3789
|
-
const observability = createObservabilityReporter({
|
|
3790
|
-
registry,
|
|
3791
|
-
eventBus,
|
|
3792
|
-
graph
|
|
3793
|
-
});
|
|
3794
|
-
return {
|
|
3795
|
-
instanceId,
|
|
3796
|
-
profile,
|
|
3797
|
-
registry,
|
|
3798
|
-
graph,
|
|
3799
|
-
metadata,
|
|
3800
|
-
auditTrail,
|
|
3801
|
-
eventContracts,
|
|
3802
|
-
outbox,
|
|
3803
|
-
projections,
|
|
3804
|
-
policyEngine,
|
|
3805
|
-
eventBus,
|
|
3806
|
-
workflowEngine,
|
|
3807
|
-
observability,
|
|
3808
|
-
publisher(source) {
|
|
3809
|
-
return eventBus.createPublisher(source);
|
|
3810
|
-
},
|
|
3811
|
-
syncGraph,
|
|
3812
|
-
manifest() {
|
|
3813
|
-
syncGraph();
|
|
3814
|
-
return createArkManifest({
|
|
3815
|
-
registry,
|
|
3816
|
-
policyEngine,
|
|
3817
|
-
metadata,
|
|
3818
|
-
graph,
|
|
3819
|
-
profile,
|
|
3820
|
-
projections,
|
|
3821
|
-
eventContracts,
|
|
3822
|
-
observability
|
|
3823
|
-
});
|
|
3824
|
-
}
|
|
3825
|
-
};
|
|
3826
|
-
}
|
|
3827
|
-
function createStrictArkKernel(options = {}) {
|
|
3828
|
-
return createArkKernel({
|
|
3829
|
-
...options,
|
|
3830
|
-
strict: true,
|
|
3831
|
-
strictEventContracts: options.strictEventContracts ?? true,
|
|
3832
|
-
requireKnownSource: options.requireKnownSource ?? true,
|
|
3833
|
-
enforceObservedLayerFlow: options.enforceObservedLayerFlow ?? "hard"
|
|
3834
|
-
});
|
|
3835
|
-
}
|
|
3836
|
-
function createOptionsFromConfig(config, options = {}) {
|
|
3837
|
-
const { profileName, ...kernelOptions } = options;
|
|
3838
|
-
return {
|
|
3839
|
-
...kernelOptions,
|
|
3840
|
-
profile: createArchitectureProfileFromArkConfig(config, { name: profileName })
|
|
3841
|
-
};
|
|
3842
|
-
}
|
|
3843
|
-
function createArkKernelFromConfig(config, options = {}) {
|
|
3844
|
-
return createArkKernel(createOptionsFromConfig(config, options));
|
|
3845
|
-
}
|
|
3846
|
-
function createStrictArkKernelFromConfig(config, options = {}) {
|
|
3847
|
-
return createStrictArkKernel(createOptionsFromConfig(config, options));
|
|
3848
|
-
}
|
|
3849
|
-
function createLenientArkKernelFromConfig(config, options = {}) {
|
|
3850
|
-
return createLenientArkKernel(createOptionsFromConfig(config, options));
|
|
3851
|
-
}
|
|
3852
|
-
function createLenientArkKernel(options = {}) {
|
|
3853
|
-
return createArkKernel({
|
|
3854
|
-
...options,
|
|
3855
|
-
strict: false,
|
|
3856
|
-
strictEventContracts: options.strictEventContracts ?? false,
|
|
3857
|
-
enforceObservedLayerFlow: options.enforceObservedLayerFlow ?? "off"
|
|
3858
|
-
});
|
|
1859
|
+
}
|
|
1860
|
+
if (ambiguousPairs.size > 0) {
|
|
1861
|
+
warnings.push(
|
|
1862
|
+
configWarning(
|
|
1863
|
+
"CONFIG_AMBIGUOUS_LAYERS",
|
|
1864
|
+
`Some files match multiple layers at equal specificity; classification falls back to declaration order. Disambiguate the overlapping patterns: ${[...ambiguousPairs].join(", ")}.`,
|
|
1865
|
+
{ pairs: [...ambiguousPairs] }
|
|
1866
|
+
)
|
|
1867
|
+
);
|
|
1868
|
+
}
|
|
1869
|
+
const unclassified = files.filter((file) => !layerForRelativePath(file, layers));
|
|
1870
|
+
if (unclassified.length > 0) {
|
|
1871
|
+
warnings.push(
|
|
1872
|
+
configWarning(
|
|
1873
|
+
"CONFIG_UNCLASSIFIED_FILES",
|
|
1874
|
+
`${unclassified.length} included source file(s) are not matched by any configured layer; ark-check will not enforce import rules for those source files.`,
|
|
1875
|
+
{ count: unclassified.length, samples: unclassified.slice(0, 5) }
|
|
1876
|
+
)
|
|
1877
|
+
);
|
|
1878
|
+
}
|
|
1879
|
+
return warnings;
|
|
3859
1880
|
}
|
|
3860
1881
|
export {
|
|
3861
1882
|
ANALYSIS_IR_SCHEMA_VERSION,
|
|
3862
|
-
|
|
3863
|
-
|
|
3864
|
-
|
|
3865
|
-
|
|
3866
|
-
InMemoryOutboxStore,
|
|
3867
|
-
InMemoryReadModelStore,
|
|
3868
|
-
InMemoryWorkflowStore,
|
|
3869
|
-
IntentRegistry,
|
|
3870
|
-
InvalidIntentNameError,
|
|
3871
|
-
LayerPolicyContextError,
|
|
3872
|
-
MANIFEST_SCHEMA_VERSION,
|
|
3873
|
-
ObservedLayerFlowViolationError,
|
|
3874
|
-
PolicyEngine,
|
|
3875
|
-
PolicyViolationError,
|
|
3876
|
-
SourceMetadataOverrideError,
|
|
3877
|
-
UnknownEventSourceError,
|
|
3878
|
-
UnregisteredIntentError,
|
|
1883
|
+
ARK_ANALYSIS_RESULT_SCHEMA,
|
|
1884
|
+
ARK_ANALYSIS_RESULT_SCHEMA_VERSION,
|
|
1885
|
+
ARK_CONFIG_SCHEMA,
|
|
1886
|
+
ARK_CONFIG_SCHEMA_VERSION,
|
|
3879
1887
|
analyzeChange,
|
|
3880
1888
|
analyzeProject,
|
|
3881
|
-
|
|
3882
|
-
|
|
3883
|
-
checkAdapterGovernance,
|
|
3884
|
-
checkContract,
|
|
1889
|
+
collectAnalysisConfigWarnings,
|
|
1890
|
+
collectForbiddenCapabilityUses,
|
|
3885
1891
|
createAICodeGate,
|
|
3886
|
-
|
|
1892
|
+
createAdapterResult,
|
|
3887
1893
|
createArchitectureProfile,
|
|
3888
1894
|
createArchitectureProfileFromArkConfig,
|
|
3889
|
-
createArkKernel,
|
|
3890
|
-
createArkKernelFromConfig,
|
|
3891
|
-
createArkManifest,
|
|
3892
|
-
createArkTestHarness,
|
|
3893
|
-
createAuditTrail,
|
|
3894
|
-
createDependencyGraph,
|
|
3895
1895
|
createElevenLayerArkConfig,
|
|
3896
|
-
|
|
3897
|
-
createEventContractRegistry,
|
|
3898
|
-
createIntentRegistry,
|
|
3899
|
-
createLenientArkKernel,
|
|
3900
|
-
createLenientArkKernelFromConfig,
|
|
3901
|
-
createMetadataRegistry,
|
|
3902
|
-
createObservabilityReporter,
|
|
3903
|
-
createProjectionRegistry,
|
|
3904
|
-
createSaga,
|
|
3905
|
-
createStrictArkKernel,
|
|
3906
|
-
createStrictArkKernelFromConfig,
|
|
3907
|
-
createWorkflowEngine,
|
|
3908
|
-
defaultIntentRegistry,
|
|
3909
|
-
defineArchitectureProfilePolicy,
|
|
3910
|
-
defineIntent,
|
|
3911
|
-
defineLayerPolicy,
|
|
3912
|
-
definePolicy,
|
|
3913
|
-
definePort,
|
|
3914
|
-
definePublishPolicy,
|
|
1896
|
+
detectArchitectureCycles,
|
|
3915
1897
|
deterministicHash,
|
|
3916
1898
|
elevenLayerProfile,
|
|
1899
|
+
evaluateArchitectureGraph,
|
|
3917
1900
|
explainViolation,
|
|
3918
|
-
|
|
1901
|
+
extractSemanticDependencies,
|
|
1902
|
+
loadArkConfigContract,
|
|
3919
1903
|
loadContract,
|
|
1904
|
+
parseArkConfigJson,
|
|
3920
1905
|
stableSerialize,
|
|
3921
|
-
|
|
3922
|
-
validateIntentName,
|
|
1906
|
+
toAdapterDiagnostic,
|
|
3923
1907
|
version
|
|
3924
1908
|
};
|
|
3925
|
-
//# sourceMappingURL=index.js.map
|