ark-runtime-kernel 1.0.0 → 1.1.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/README.md +121 -220
- package/bin/ark-check.mjs +142 -20
- package/bin/ark-mcp.mjs +0 -0
- package/dist/index.cjs +25 -0
- package/dist/index.cjs.map +1 -1
- package/dist/index.d.cts +4 -957
- package/dist/index.d.ts +4 -957
- package/dist/index.js +25 -0
- package/dist/index.js.map +1 -1
- package/dist/nestjs/index.cjs +2301 -0
- package/dist/nestjs/index.cjs.map +1 -0
- package/dist/nestjs/index.d.cts +22 -0
- package/dist/nestjs/index.d.ts +22 -0
- package/dist/nestjs/index.js +2298 -0
- package/dist/nestjs/index.js.map +1 -0
- package/dist/types-7K_KQCgS.d.cts +991 -0
- package/dist/types-7K_KQCgS.d.ts +991 -0
- package/package.json +43 -8
- package/server.json +31 -0
|
@@ -0,0 +1,2298 @@
|
|
|
1
|
+
import { Module, Inject } from '@nestjs/common';
|
|
2
|
+
|
|
3
|
+
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
|
|
4
|
+
var __decorateClass = (decorators, target, key, kind) => {
|
|
5
|
+
var result = kind > 1 ? void 0 : kind ? __getOwnPropDesc(target, key) : target;
|
|
6
|
+
for (var i = decorators.length - 1, decorator; i >= 0; i--)
|
|
7
|
+
if (decorator = decorators[i])
|
|
8
|
+
result = (decorator(result)) || result;
|
|
9
|
+
return result;
|
|
10
|
+
};
|
|
11
|
+
|
|
12
|
+
// src/kernel/audit/AuditTrail.ts
|
|
13
|
+
var auditSequence = 0;
|
|
14
|
+
function createAuditId() {
|
|
15
|
+
auditSequence += 1;
|
|
16
|
+
return `audit-${Date.now()}-${auditSequence}`;
|
|
17
|
+
}
|
|
18
|
+
function matchesQuery(record, query) {
|
|
19
|
+
if (query.type && record.type !== query.type) return false;
|
|
20
|
+
if (query.intent && record.intent !== query.intent) return false;
|
|
21
|
+
if (query.correlationId && record.correlationId !== query.correlationId) return false;
|
|
22
|
+
if (query.subject && record.subject !== query.subject) return false;
|
|
23
|
+
if (query.since && record.timestamp < query.since) return false;
|
|
24
|
+
if (query.until && record.timestamp > query.until) return false;
|
|
25
|
+
return true;
|
|
26
|
+
}
|
|
27
|
+
var InMemoryAuditStore = class {
|
|
28
|
+
constructor(maxRecords) {
|
|
29
|
+
this.maxRecords = maxRecords;
|
|
30
|
+
}
|
|
31
|
+
maxRecords;
|
|
32
|
+
records = [];
|
|
33
|
+
append(record) {
|
|
34
|
+
this.records.push(record);
|
|
35
|
+
if (this.maxRecords !== void 0 && this.records.length > this.maxRecords) {
|
|
36
|
+
this.records.splice(0, this.records.length - this.maxRecords);
|
|
37
|
+
}
|
|
38
|
+
}
|
|
39
|
+
query(query = {}) {
|
|
40
|
+
const records = this.records.filter((record) => matchesQuery(record, query));
|
|
41
|
+
return query.limit === void 0 ? [...records] : records.slice(-query.limit);
|
|
42
|
+
}
|
|
43
|
+
clear() {
|
|
44
|
+
this.records.length = 0;
|
|
45
|
+
}
|
|
46
|
+
};
|
|
47
|
+
var AuditTrailImpl = class {
|
|
48
|
+
constructor(store) {
|
|
49
|
+
this.store = store;
|
|
50
|
+
}
|
|
51
|
+
store;
|
|
52
|
+
async record(input) {
|
|
53
|
+
const record = {
|
|
54
|
+
id: createAuditId(),
|
|
55
|
+
timestamp: input.timestamp ?? (/* @__PURE__ */ new Date()).toISOString(),
|
|
56
|
+
type: input.type,
|
|
57
|
+
source: input.source,
|
|
58
|
+
actor: input.actor,
|
|
59
|
+
intent: input.intent,
|
|
60
|
+
correlationId: input.correlationId,
|
|
61
|
+
causationId: input.causationId,
|
|
62
|
+
subject: input.subject,
|
|
63
|
+
details: input.details
|
|
64
|
+
};
|
|
65
|
+
await this.store.append(record);
|
|
66
|
+
return record;
|
|
67
|
+
}
|
|
68
|
+
async query(query) {
|
|
69
|
+
return this.store.query(query);
|
|
70
|
+
}
|
|
71
|
+
async clear() {
|
|
72
|
+
await this.store.clear();
|
|
73
|
+
}
|
|
74
|
+
};
|
|
75
|
+
function createAuditTrail(options = {}) {
|
|
76
|
+
return new AuditTrailImpl(
|
|
77
|
+
options.store ?? new InMemoryAuditStore(options.maxRecords)
|
|
78
|
+
);
|
|
79
|
+
}
|
|
80
|
+
|
|
81
|
+
// src/kernel/intent/validateIntentName.ts
|
|
82
|
+
var ALLOWED_PREFIXES = [
|
|
83
|
+
"Domain.",
|
|
84
|
+
"Application.",
|
|
85
|
+
"Adapter.",
|
|
86
|
+
"Workflow.",
|
|
87
|
+
"Job.",
|
|
88
|
+
"Presentation.",
|
|
89
|
+
"Reporting.",
|
|
90
|
+
"Metadata.",
|
|
91
|
+
"Security.",
|
|
92
|
+
"Audit.",
|
|
93
|
+
"Observability.",
|
|
94
|
+
"Kernel."
|
|
95
|
+
];
|
|
96
|
+
function validateIntentName(name) {
|
|
97
|
+
if (!name || typeof name !== "string") {
|
|
98
|
+
return { valid: false, reason: "Intent name must be a non-empty string" };
|
|
99
|
+
}
|
|
100
|
+
if (!ALLOWED_PREFIXES.some((p) => name.startsWith(p))) {
|
|
101
|
+
return {
|
|
102
|
+
valid: false,
|
|
103
|
+
reason: `Intent "${name}" must start with one of: ${ALLOWED_PREFIXES.join(", ")}`
|
|
104
|
+
};
|
|
105
|
+
}
|
|
106
|
+
const rest = name.slice(name.indexOf(".") + 1);
|
|
107
|
+
if (!rest || !/^[A-Za-z][A-Za-z0-9_.]*$/.test(rest)) {
|
|
108
|
+
return {
|
|
109
|
+
valid: false,
|
|
110
|
+
reason: `Intent "${name}" has an invalid segment after the layer prefix`
|
|
111
|
+
};
|
|
112
|
+
}
|
|
113
|
+
return { valid: true };
|
|
114
|
+
}
|
|
115
|
+
|
|
116
|
+
// src/kernel/policy/PolicyViolationError.ts
|
|
117
|
+
var PolicyViolationError = class extends Error {
|
|
118
|
+
violations;
|
|
119
|
+
constructor(violations) {
|
|
120
|
+
const messages = violations.map((v) => `- ${v.policyName}: ${v.message}`).join("\n");
|
|
121
|
+
super(`Hard policy violation(s) detected:
|
|
122
|
+
${messages}`);
|
|
123
|
+
this.name = "PolicyViolationError";
|
|
124
|
+
this.violations = violations;
|
|
125
|
+
}
|
|
126
|
+
};
|
|
127
|
+
|
|
128
|
+
// src/kernel/policy/PolicyEngine.ts
|
|
129
|
+
var PolicyEngine = class {
|
|
130
|
+
policies = [];
|
|
131
|
+
constructor(initialPolicies = []) {
|
|
132
|
+
for (const policy of initialPolicies) {
|
|
133
|
+
this.add(policy);
|
|
134
|
+
}
|
|
135
|
+
}
|
|
136
|
+
/**
|
|
137
|
+
* Adds a policy to the engine.
|
|
138
|
+
*/
|
|
139
|
+
add(policy) {
|
|
140
|
+
if (this.policies.some((p) => p.name === policy.name)) {
|
|
141
|
+
throw new Error(`Policy "${policy.name}" is already registered in this engine.`);
|
|
142
|
+
}
|
|
143
|
+
this.policies.push(policy);
|
|
144
|
+
}
|
|
145
|
+
/**
|
|
146
|
+
* Returns all registered policies.
|
|
147
|
+
*/
|
|
148
|
+
getPolicies() {
|
|
149
|
+
return [...this.policies];
|
|
150
|
+
}
|
|
151
|
+
/**
|
|
152
|
+
* Evaluates all policies against the provided context.
|
|
153
|
+
*/
|
|
154
|
+
evaluate(context) {
|
|
155
|
+
const violations = [];
|
|
156
|
+
for (const policy of this.policies) {
|
|
157
|
+
const result = policy.check(context);
|
|
158
|
+
if (result === true) {
|
|
159
|
+
continue;
|
|
160
|
+
}
|
|
161
|
+
if (result === false) {
|
|
162
|
+
violations.push({
|
|
163
|
+
policyName: policy.name,
|
|
164
|
+
severity: policy.severity,
|
|
165
|
+
message: `Policy "${policy.name}" was violated.`
|
|
166
|
+
});
|
|
167
|
+
continue;
|
|
168
|
+
}
|
|
169
|
+
if (Array.isArray(result)) {
|
|
170
|
+
for (const v of result) {
|
|
171
|
+
violations.push({
|
|
172
|
+
policyName: policy.name,
|
|
173
|
+
severity: policy.severity,
|
|
174
|
+
message: v.message,
|
|
175
|
+
details: v.details
|
|
176
|
+
});
|
|
177
|
+
}
|
|
178
|
+
} else {
|
|
179
|
+
violations.push({
|
|
180
|
+
policyName: policy.name,
|
|
181
|
+
severity: policy.severity,
|
|
182
|
+
message: result.message,
|
|
183
|
+
details: result.details
|
|
184
|
+
});
|
|
185
|
+
}
|
|
186
|
+
}
|
|
187
|
+
const hardViolations = violations.filter((v) => v.severity === "hard");
|
|
188
|
+
const softViolations = violations.filter((v) => v.severity === "soft");
|
|
189
|
+
return {
|
|
190
|
+
passed: violations.length === 0,
|
|
191
|
+
violations,
|
|
192
|
+
hardViolations,
|
|
193
|
+
softViolations
|
|
194
|
+
};
|
|
195
|
+
}
|
|
196
|
+
/**
|
|
197
|
+
* Enforces all policies.
|
|
198
|
+
*
|
|
199
|
+
* - Soft violations are collected and can be observed (returned or logged).
|
|
200
|
+
* - Hard violations cause an error to be thrown (by default).
|
|
201
|
+
*
|
|
202
|
+
* @returns Evaluation result (including any soft violations)
|
|
203
|
+
* @throws Error if any hard policy is violated
|
|
204
|
+
*/
|
|
205
|
+
enforce(context) {
|
|
206
|
+
const result = this.evaluate(context);
|
|
207
|
+
if (result.hardViolations.length > 0) {
|
|
208
|
+
throw new PolicyViolationError(result.hardViolations);
|
|
209
|
+
}
|
|
210
|
+
return result;
|
|
211
|
+
}
|
|
212
|
+
/**
|
|
213
|
+
* Clears all registered policies.
|
|
214
|
+
*/
|
|
215
|
+
clear() {
|
|
216
|
+
this.policies.length = 0;
|
|
217
|
+
}
|
|
218
|
+
};
|
|
219
|
+
|
|
220
|
+
// src/kernel/policy/definePolicy.ts
|
|
221
|
+
function definePolicy(options) {
|
|
222
|
+
const severity = options.severity ?? "soft";
|
|
223
|
+
const policy = {
|
|
224
|
+
name: options.name,
|
|
225
|
+
severity,
|
|
226
|
+
tags: options.tags,
|
|
227
|
+
owner: options.owner,
|
|
228
|
+
version: options.version,
|
|
229
|
+
rationale: options.rationale,
|
|
230
|
+
enforcementMode: options.enforcementMode,
|
|
231
|
+
deprecated: options.deprecated,
|
|
232
|
+
replacedBy: options.replacedBy,
|
|
233
|
+
check: options.check
|
|
234
|
+
};
|
|
235
|
+
return policy;
|
|
236
|
+
}
|
|
237
|
+
|
|
238
|
+
// src/kernel/policy/builtins.ts
|
|
239
|
+
function defaultLayerOf(name) {
|
|
240
|
+
const dot = name.indexOf(".");
|
|
241
|
+
return dot >= 0 ? name.slice(0, dot) : name;
|
|
242
|
+
}
|
|
243
|
+
function resolveLayer(name, options) {
|
|
244
|
+
return options.resolveLayer?.(name) ?? defaultLayerOf(name);
|
|
245
|
+
}
|
|
246
|
+
function matchesRule(fromLayer, toLayer, rule) {
|
|
247
|
+
return fromLayer === rule.from && toLayer === rule.to;
|
|
248
|
+
}
|
|
249
|
+
function defineLayerPolicy(options) {
|
|
250
|
+
const name = options.name ?? "Layer isolation";
|
|
251
|
+
const severity = options.severity ?? "hard";
|
|
252
|
+
return definePolicy({
|
|
253
|
+
name,
|
|
254
|
+
severity,
|
|
255
|
+
tags: ["layer"],
|
|
256
|
+
check: (ctx) => {
|
|
257
|
+
const edges = [
|
|
258
|
+
...(ctx.edges ?? []).filter((e) => e.kind === "declared" || e.kind === "produces").map((e) => ({ from: e.from, to: e.to })),
|
|
259
|
+
...(ctx.relationships ?? []).filter((r) => r.kind === "dependsOn").map((r) => ({ from: r.from, to: r.to }))
|
|
260
|
+
];
|
|
261
|
+
const violations = [];
|
|
262
|
+
for (const edge of edges) {
|
|
263
|
+
const fromLayer = resolveLayer(edge.from, options);
|
|
264
|
+
const toLayer = resolveLayer(edge.to, options);
|
|
265
|
+
for (const rule of options.rules) {
|
|
266
|
+
if (!rule.allowed && matchesRule(fromLayer, toLayer, rule)) {
|
|
267
|
+
violations.push({
|
|
268
|
+
policyName: name,
|
|
269
|
+
severity,
|
|
270
|
+
message: rule.message ?? `Layer violation: ${edge.from} (${fromLayer}) must not relate to ${edge.to} (${toLayer})`
|
|
271
|
+
});
|
|
272
|
+
}
|
|
273
|
+
}
|
|
274
|
+
}
|
|
275
|
+
return violations.length > 0 ? violations : true;
|
|
276
|
+
}
|
|
277
|
+
});
|
|
278
|
+
}
|
|
279
|
+
function isLayerPolicy(policy) {
|
|
280
|
+
return policy.tags?.includes("layer") ?? false;
|
|
281
|
+
}
|
|
282
|
+
function defineArchitectureProfilePolicy(profile, options = {}) {
|
|
283
|
+
return defineLayerPolicy({
|
|
284
|
+
name: options.name ?? `${profile.name} layer policy`,
|
|
285
|
+
severity: options.severity ?? "hard",
|
|
286
|
+
rules: profile.rules,
|
|
287
|
+
resolveLayer: profile.resolveLayer
|
|
288
|
+
});
|
|
289
|
+
}
|
|
290
|
+
|
|
291
|
+
// src/kernel/event-bus/policyContext.ts
|
|
292
|
+
function buildPublishPolicyContext(options) {
|
|
293
|
+
return (event) => ({
|
|
294
|
+
event,
|
|
295
|
+
relationships: options.intentRegistry?.getAllRelationships(),
|
|
296
|
+
edges: options.dependencyGraph?.getEdges()
|
|
297
|
+
});
|
|
298
|
+
}
|
|
299
|
+
|
|
300
|
+
// src/kernel/event-bus/errors.ts
|
|
301
|
+
var UnregisteredIntentError = class extends Error {
|
|
302
|
+
intentName;
|
|
303
|
+
constructor(intentName) {
|
|
304
|
+
super(
|
|
305
|
+
`Intent "${intentName}" is not registered. Register it with IntentRegistry.define() before publish/subscribe, including metadata.source producer intents in strict mode.`
|
|
306
|
+
);
|
|
307
|
+
this.name = "UnregisteredIntentError";
|
|
308
|
+
this.intentName = intentName;
|
|
309
|
+
}
|
|
310
|
+
};
|
|
311
|
+
var InvalidIntentNameError = class extends Error {
|
|
312
|
+
intentName;
|
|
313
|
+
reason;
|
|
314
|
+
constructor(intentName, reason) {
|
|
315
|
+
super(`Invalid intent name "${intentName}": ${reason}`);
|
|
316
|
+
this.name = "InvalidIntentNameError";
|
|
317
|
+
this.intentName = intentName;
|
|
318
|
+
this.reason = reason;
|
|
319
|
+
}
|
|
320
|
+
};
|
|
321
|
+
var LayerPolicyContextError = class extends Error {
|
|
322
|
+
constructor() {
|
|
323
|
+
super(
|
|
324
|
+
"Layer/architecture policies require intentRegistry, dependencyGraph, or a custom getPolicyContext. Without graph/registry context, layer policies cannot inspect relationships."
|
|
325
|
+
);
|
|
326
|
+
this.name = "LayerPolicyContextError";
|
|
327
|
+
}
|
|
328
|
+
};
|
|
329
|
+
var EventContractViolationError = class extends Error {
|
|
330
|
+
intentName;
|
|
331
|
+
issues;
|
|
332
|
+
constructor(intentName, issues) {
|
|
333
|
+
super(
|
|
334
|
+
`Event contract violation for "${intentName}". Register a matching event contract/version or fix the payload before publishing.`
|
|
335
|
+
);
|
|
336
|
+
this.name = "EventContractViolationError";
|
|
337
|
+
this.intentName = intentName;
|
|
338
|
+
this.issues = issues;
|
|
339
|
+
}
|
|
340
|
+
};
|
|
341
|
+
var UnknownEventSourceError = class extends Error {
|
|
342
|
+
intentName;
|
|
343
|
+
source;
|
|
344
|
+
constructor(intentName, source) {
|
|
345
|
+
super(
|
|
346
|
+
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.`
|
|
347
|
+
);
|
|
348
|
+
this.name = "UnknownEventSourceError";
|
|
349
|
+
this.intentName = intentName;
|
|
350
|
+
this.source = source;
|
|
351
|
+
}
|
|
352
|
+
};
|
|
353
|
+
var SourceMetadataOverrideError = class extends Error {
|
|
354
|
+
boundSource;
|
|
355
|
+
attemptedSource;
|
|
356
|
+
constructor(boundSource, attemptedSource) {
|
|
357
|
+
super(
|
|
358
|
+
`Source-bound publisher for "${boundSource}" cannot publish with metadata.source "${attemptedSource}". Create a publisher for the intended source instead.`
|
|
359
|
+
);
|
|
360
|
+
this.name = "SourceMetadataOverrideError";
|
|
361
|
+
this.boundSource = boundSource;
|
|
362
|
+
this.attemptedSource = attemptedSource;
|
|
363
|
+
}
|
|
364
|
+
};
|
|
365
|
+
var ObservedLayerFlowViolationError = class extends Error {
|
|
366
|
+
source;
|
|
367
|
+
intentName;
|
|
368
|
+
fromLayer;
|
|
369
|
+
toLayer;
|
|
370
|
+
constructor(source, intentName, fromLayer, toLayer, message) {
|
|
371
|
+
super(
|
|
372
|
+
message ?? `Observed layer violation: "${source}" (${fromLayer}) must not produce "${intentName}" (${toLayer}). Route this through an allowed layer or adjust the architecture profile rule.`
|
|
373
|
+
);
|
|
374
|
+
this.name = "ObservedLayerFlowViolationError";
|
|
375
|
+
this.source = source;
|
|
376
|
+
this.intentName = intentName;
|
|
377
|
+
this.fromLayer = fromLayer;
|
|
378
|
+
this.toLayer = toLayer;
|
|
379
|
+
}
|
|
380
|
+
};
|
|
381
|
+
|
|
382
|
+
// src/kernel/event-bus/EventBus.ts
|
|
383
|
+
var interceptorSequence = 0;
|
|
384
|
+
function nextInterceptorRegistrationId() {
|
|
385
|
+
interceptorSequence += 1;
|
|
386
|
+
return `interceptor-${Date.now()}-${interceptorSequence}`;
|
|
387
|
+
}
|
|
388
|
+
function isPlainRecord(value) {
|
|
389
|
+
if (typeof value !== "object" || value === null || Array.isArray(value)) {
|
|
390
|
+
return false;
|
|
391
|
+
}
|
|
392
|
+
const proto = Object.getPrototypeOf(value);
|
|
393
|
+
return proto === Object.prototype || proto === null;
|
|
394
|
+
}
|
|
395
|
+
function clonePatchValue(value) {
|
|
396
|
+
if (Array.isArray(value)) return value.map(clonePatchValue);
|
|
397
|
+
if (isPlainRecord(value)) {
|
|
398
|
+
return Object.fromEntries(
|
|
399
|
+
Object.entries(value).map(([key, child]) => [key, clonePatchValue(child)])
|
|
400
|
+
);
|
|
401
|
+
}
|
|
402
|
+
return value;
|
|
403
|
+
}
|
|
404
|
+
function mergeRecordPatch(target, patch, path = "payload") {
|
|
405
|
+
const next = { ...target };
|
|
406
|
+
for (const [key, value] of Object.entries(patch)) {
|
|
407
|
+
const childPath = `${path}.${key}`;
|
|
408
|
+
if (!(key in next) || next[key] === void 0) {
|
|
409
|
+
next[key] = clonePatchValue(value);
|
|
410
|
+
continue;
|
|
411
|
+
}
|
|
412
|
+
if (isPlainRecord(next[key]) && isPlainRecord(value)) {
|
|
413
|
+
next[key] = mergeRecordPatch(next[key], value, childPath);
|
|
414
|
+
continue;
|
|
415
|
+
}
|
|
416
|
+
if (Array.isArray(next[key]) && Array.isArray(value)) {
|
|
417
|
+
next[key] = mergeArrayPatch(next[key], value, childPath);
|
|
418
|
+
continue;
|
|
419
|
+
}
|
|
420
|
+
throw new Error(`Interceptor patch cannot overwrite existing ${childPath}.`);
|
|
421
|
+
}
|
|
422
|
+
return next;
|
|
423
|
+
}
|
|
424
|
+
function mergeArrayPatch(target, patch, path = "payload") {
|
|
425
|
+
const next = [...target];
|
|
426
|
+
patch.forEach((value, index) => {
|
|
427
|
+
const childPath = `${path}[${index}]`;
|
|
428
|
+
if (index >= next.length || next[index] === void 0) {
|
|
429
|
+
next[index] = clonePatchValue(value);
|
|
430
|
+
return;
|
|
431
|
+
}
|
|
432
|
+
if (isPlainRecord(next[index]) && isPlainRecord(value)) {
|
|
433
|
+
next[index] = mergeRecordPatch(next[index], value, childPath);
|
|
434
|
+
return;
|
|
435
|
+
}
|
|
436
|
+
if (Array.isArray(next[index]) && Array.isArray(value)) {
|
|
437
|
+
next[index] = mergeArrayPatch(next[index], value, childPath);
|
|
438
|
+
return;
|
|
439
|
+
}
|
|
440
|
+
throw new Error(`Interceptor patch cannot overwrite existing ${childPath}.`);
|
|
441
|
+
});
|
|
442
|
+
return next;
|
|
443
|
+
}
|
|
444
|
+
function applyPayloadPatch(payload, patch) {
|
|
445
|
+
if (Array.isArray(patch)) {
|
|
446
|
+
if (payload === void 0) return clonePatchValue(patch);
|
|
447
|
+
if (!Array.isArray(payload)) {
|
|
448
|
+
throw new Error("Array interceptor patch requires an array payload.");
|
|
449
|
+
}
|
|
450
|
+
return mergeArrayPatch(payload, patch);
|
|
451
|
+
}
|
|
452
|
+
if (payload === void 0) return clonePatchValue(patch);
|
|
453
|
+
if (!isPlainRecord(payload)) {
|
|
454
|
+
throw new Error("Object interceptor patch requires an object payload.");
|
|
455
|
+
}
|
|
456
|
+
return mergeRecordPatch(payload, patch);
|
|
457
|
+
}
|
|
458
|
+
var EventBusImpl = class {
|
|
459
|
+
subscriptions = [];
|
|
460
|
+
subscriptionsByIntent = /* @__PURE__ */ new Map();
|
|
461
|
+
interceptors = [];
|
|
462
|
+
interceptorsByIntent = /* @__PURE__ */ new Map();
|
|
463
|
+
history = [];
|
|
464
|
+
trace = [];
|
|
465
|
+
onPublish;
|
|
466
|
+
onSoftViolation;
|
|
467
|
+
onHandlerError;
|
|
468
|
+
auditTrail;
|
|
469
|
+
eventContracts;
|
|
470
|
+
strictEventContracts;
|
|
471
|
+
requireKnownSource;
|
|
472
|
+
architectureProfile;
|
|
473
|
+
enforceObservedLayerFlowMode;
|
|
474
|
+
outbox;
|
|
475
|
+
instanceId;
|
|
476
|
+
traceSinks;
|
|
477
|
+
rethrowHandlerErrors;
|
|
478
|
+
policyEngine;
|
|
479
|
+
getPolicyContext;
|
|
480
|
+
maxHistorySize;
|
|
481
|
+
intentRegistry;
|
|
482
|
+
dependencyGraph;
|
|
483
|
+
strictRegistry;
|
|
484
|
+
validateIntentNaming;
|
|
485
|
+
constructor(options = {}) {
|
|
486
|
+
this.onPublish = options.onPublish;
|
|
487
|
+
this.onSoftViolation = options.onSoftViolation;
|
|
488
|
+
this.onHandlerError = options.onHandlerError;
|
|
489
|
+
this.auditTrail = options.auditTrail;
|
|
490
|
+
this.eventContracts = options.eventContracts;
|
|
491
|
+
this.strictEventContracts = options.strictEventContracts ?? false;
|
|
492
|
+
this.requireKnownSource = options.requireKnownSource ?? false;
|
|
493
|
+
this.architectureProfile = options.architectureProfile;
|
|
494
|
+
this.enforceObservedLayerFlowMode = options.enforceObservedLayerFlow ?? "off";
|
|
495
|
+
this.outbox = options.outbox;
|
|
496
|
+
this.instanceId = options.instanceId;
|
|
497
|
+
this.traceSinks = [...options.traceSinks ?? []];
|
|
498
|
+
this.rethrowHandlerErrors = options.rethrowHandlerErrors ?? false;
|
|
499
|
+
this.maxHistorySize = options.maxHistorySize;
|
|
500
|
+
this.intentRegistry = options.intentRegistry;
|
|
501
|
+
this.dependencyGraph = options.dependencyGraph;
|
|
502
|
+
this.strictRegistry = options.strictRegistry ?? options.intentRegistry !== void 0;
|
|
503
|
+
this.validateIntentNaming = options.validateIntentNaming ?? this.strictRegistry;
|
|
504
|
+
if (options.policyEngine) {
|
|
505
|
+
this.policyEngine = options.policyEngine;
|
|
506
|
+
} else if (options.policies && options.policies.length > 0) {
|
|
507
|
+
this.policyEngine = new PolicyEngine(options.policies);
|
|
508
|
+
}
|
|
509
|
+
const allPolicies = this.policyEngine?.getPolicies() ?? options.policies ?? [];
|
|
510
|
+
if (allPolicies.some(isLayerPolicy) && !options.intentRegistry && !options.dependencyGraph && !options.getPolicyContext) {
|
|
511
|
+
throw new LayerPolicyContextError();
|
|
512
|
+
}
|
|
513
|
+
if (options.getPolicyContext) {
|
|
514
|
+
this.getPolicyContext = options.getPolicyContext;
|
|
515
|
+
} else if (options.intentRegistry || options.dependencyGraph) {
|
|
516
|
+
this.getPolicyContext = buildPublishPolicyContext({
|
|
517
|
+
intentRegistry: options.intentRegistry,
|
|
518
|
+
dependencyGraph: options.dependencyGraph
|
|
519
|
+
});
|
|
520
|
+
} else {
|
|
521
|
+
this.getPolicyContext = (event) => ({ event });
|
|
522
|
+
}
|
|
523
|
+
}
|
|
524
|
+
async publish(eventOrCreator, payloadOrMeta, metadata) {
|
|
525
|
+
let event;
|
|
526
|
+
const rawPublish = typeof eventOrCreator !== "function";
|
|
527
|
+
if (!rawPublish) {
|
|
528
|
+
const creator = eventOrCreator;
|
|
529
|
+
const payload = payloadOrMeta;
|
|
530
|
+
const extraMeta = metadata ?? {};
|
|
531
|
+
const created = creator(payload);
|
|
532
|
+
event = {
|
|
533
|
+
...created,
|
|
534
|
+
metadata: this.enrichMetadata(created.metadata, extraMeta)
|
|
535
|
+
};
|
|
536
|
+
} else {
|
|
537
|
+
const rawEvent = eventOrCreator;
|
|
538
|
+
const extraMeta = metadata ?? payloadOrMeta ?? {};
|
|
539
|
+
event = {
|
|
540
|
+
...rawEvent,
|
|
541
|
+
metadata: this.enrichMetadata(rawEvent.metadata, extraMeta)
|
|
542
|
+
};
|
|
543
|
+
}
|
|
544
|
+
if (rawPublish && this.strictRegistry) {
|
|
545
|
+
await this.recordRawPublishDiagnostic(event);
|
|
546
|
+
}
|
|
547
|
+
this.assertIntentAllowed(event.intent);
|
|
548
|
+
this.assertSourceAllowed(event);
|
|
549
|
+
this.assertContractAllowed(event);
|
|
550
|
+
event = await this.applyInterceptors(event);
|
|
551
|
+
this.assertContractAllowed(event);
|
|
552
|
+
await this.assertObservedLayerFlowAllowed(event);
|
|
553
|
+
this.dependencyGraph?.registerEventFlow(event.metadata.source, event.intent);
|
|
554
|
+
const matching = [...this.subscriptionsByIntent.get(event.intent) ?? []];
|
|
555
|
+
if (this.policyEngine) {
|
|
556
|
+
const ctx = this.getPolicyContext(event);
|
|
557
|
+
let policyResult;
|
|
558
|
+
try {
|
|
559
|
+
policyResult = this.policyEngine.enforce(ctx);
|
|
560
|
+
} catch (err) {
|
|
561
|
+
if (err instanceof PolicyViolationError) {
|
|
562
|
+
this.appendTrace({
|
|
563
|
+
type: "policy.hardViolation",
|
|
564
|
+
timestamp: (/* @__PURE__ */ new Date()).toISOString(),
|
|
565
|
+
intent: event.intent,
|
|
566
|
+
correlationId: event.metadata.correlationId,
|
|
567
|
+
traceId: event.metadata.traceId,
|
|
568
|
+
spanId: event.metadata.spanId,
|
|
569
|
+
details: { violations: err.violations }
|
|
570
|
+
});
|
|
571
|
+
await this.recordAudit("policy.hardViolation", event, {
|
|
572
|
+
violations: err.violations
|
|
573
|
+
});
|
|
574
|
+
}
|
|
575
|
+
throw err;
|
|
576
|
+
}
|
|
577
|
+
if (policyResult.softViolations.length > 0) {
|
|
578
|
+
this.appendTrace({
|
|
579
|
+
type: "policy.softViolation",
|
|
580
|
+
timestamp: (/* @__PURE__ */ new Date()).toISOString(),
|
|
581
|
+
intent: event.intent,
|
|
582
|
+
correlationId: event.metadata.correlationId,
|
|
583
|
+
traceId: event.metadata.traceId,
|
|
584
|
+
spanId: event.metadata.spanId,
|
|
585
|
+
details: { violations: policyResult.softViolations }
|
|
586
|
+
});
|
|
587
|
+
await this.recordAudit("policy.softViolation", event, {
|
|
588
|
+
violations: policyResult.softViolations
|
|
589
|
+
});
|
|
590
|
+
if (this.onSoftViolation) {
|
|
591
|
+
await this.safeHook(
|
|
592
|
+
() => this.onSoftViolation(policyResult, event),
|
|
593
|
+
"onSoftViolation",
|
|
594
|
+
event
|
|
595
|
+
);
|
|
596
|
+
}
|
|
597
|
+
}
|
|
598
|
+
}
|
|
599
|
+
const record = {
|
|
600
|
+
event,
|
|
601
|
+
publishedAt: (/* @__PURE__ */ new Date()).toISOString(),
|
|
602
|
+
subscribersNotified: matching.length
|
|
603
|
+
};
|
|
604
|
+
this.appendHistory(record);
|
|
605
|
+
await this.outbox?.enqueue(event);
|
|
606
|
+
this.appendTrace({
|
|
607
|
+
type: "event.published",
|
|
608
|
+
timestamp: record.publishedAt,
|
|
609
|
+
intent: event.intent,
|
|
610
|
+
correlationId: event.metadata.correlationId,
|
|
611
|
+
traceId: event.metadata.traceId,
|
|
612
|
+
spanId: event.metadata.spanId,
|
|
613
|
+
details: { subscribersNotified: matching.length }
|
|
614
|
+
});
|
|
615
|
+
await this.recordAudit("event.published", event, {
|
|
616
|
+
subscribersNotified: matching.length
|
|
617
|
+
});
|
|
618
|
+
const notifications = matching.map(
|
|
619
|
+
(sub) => this.invokeHandler(sub, event)
|
|
620
|
+
);
|
|
621
|
+
await Promise.all(notifications);
|
|
622
|
+
if (this.onPublish) {
|
|
623
|
+
await this.safeHook(
|
|
624
|
+
() => this.onPublish(event),
|
|
625
|
+
"onPublish",
|
|
626
|
+
event
|
|
627
|
+
);
|
|
628
|
+
}
|
|
629
|
+
}
|
|
630
|
+
createPublisher(source) {
|
|
631
|
+
const sourceName = typeof source === "string" ? source : source.name;
|
|
632
|
+
this.assertIntentAllowed(sourceName);
|
|
633
|
+
return {
|
|
634
|
+
source: sourceName,
|
|
635
|
+
publish: async (intent, payload, metadata = {}) => {
|
|
636
|
+
if (metadata.source && metadata.source !== sourceName) {
|
|
637
|
+
throw new SourceMetadataOverrideError(sourceName, metadata.source);
|
|
638
|
+
}
|
|
639
|
+
await this.publish(intent, payload, {
|
|
640
|
+
...metadata,
|
|
641
|
+
source: sourceName
|
|
642
|
+
});
|
|
643
|
+
}
|
|
644
|
+
};
|
|
645
|
+
}
|
|
646
|
+
subscribe(intent, handler) {
|
|
647
|
+
const intentName = typeof intent === "string" ? intent : intent.name;
|
|
648
|
+
this.assertIntentAllowed(intentName);
|
|
649
|
+
const sub = {
|
|
650
|
+
intentName,
|
|
651
|
+
handler
|
|
652
|
+
};
|
|
653
|
+
this.subscriptions.push(sub);
|
|
654
|
+
const subscriptionsForIntent = this.subscriptionsByIntent.get(intentName) ?? [];
|
|
655
|
+
subscriptionsForIntent.push(sub);
|
|
656
|
+
this.subscriptionsByIntent.set(intentName, subscriptionsForIntent);
|
|
657
|
+
return () => {
|
|
658
|
+
const idx = this.subscriptions.indexOf(sub);
|
|
659
|
+
if (idx >= 0) this.subscriptions.splice(idx, 1);
|
|
660
|
+
const byIntent = this.subscriptionsByIntent.get(intentName);
|
|
661
|
+
if (!byIntent) return;
|
|
662
|
+
const intentIdx = byIntent.indexOf(sub);
|
|
663
|
+
if (intentIdx >= 0) byIntent.splice(intentIdx, 1);
|
|
664
|
+
if (byIntent.length === 0) this.subscriptionsByIntent.delete(intentName);
|
|
665
|
+
};
|
|
666
|
+
}
|
|
667
|
+
registerInterceptor(intent, interceptor, interceptorId) {
|
|
668
|
+
const intentName = typeof intent === "string" ? intent : intent.name;
|
|
669
|
+
this.assertIntentAllowed(intentName);
|
|
670
|
+
const registration = {
|
|
671
|
+
registrationId: nextInterceptorRegistrationId(),
|
|
672
|
+
interceptorId: interceptorId ?? intentName,
|
|
673
|
+
intentName,
|
|
674
|
+
interceptor,
|
|
675
|
+
createdAt: (/* @__PURE__ */ new Date()).toISOString()
|
|
676
|
+
};
|
|
677
|
+
this.interceptors.push(registration);
|
|
678
|
+
const interceptorsForIntent = this.interceptorsByIntent.get(intentName) ?? [];
|
|
679
|
+
interceptorsForIntent.push(registration);
|
|
680
|
+
this.interceptorsByIntent.set(intentName, interceptorsForIntent);
|
|
681
|
+
return registration.registrationId;
|
|
682
|
+
}
|
|
683
|
+
unregisterInterceptor(registrationId) {
|
|
684
|
+
const interceptor = this.interceptors.find(
|
|
685
|
+
(candidate) => candidate.registrationId === registrationId
|
|
686
|
+
);
|
|
687
|
+
if (!interceptor) return false;
|
|
688
|
+
const idx = this.interceptors.indexOf(interceptor);
|
|
689
|
+
if (idx >= 0) this.interceptors.splice(idx, 1);
|
|
690
|
+
const byIntent = this.interceptorsByIntent.get(interceptor.intentName);
|
|
691
|
+
if (byIntent) {
|
|
692
|
+
const intentIdx = byIntent.indexOf(interceptor);
|
|
693
|
+
if (intentIdx >= 0) byIntent.splice(intentIdx, 1);
|
|
694
|
+
if (byIntent.length === 0) this.interceptorsByIntent.delete(interceptor.intentName);
|
|
695
|
+
}
|
|
696
|
+
return true;
|
|
697
|
+
}
|
|
698
|
+
listInterceptors(intent) {
|
|
699
|
+
return this.interceptors.filter((interceptor) => !intent || interceptor.intentName === intent).map((interceptor) => ({
|
|
700
|
+
registrationId: interceptor.registrationId,
|
|
701
|
+
interceptorId: interceptor.interceptorId,
|
|
702
|
+
intent: interceptor.intentName,
|
|
703
|
+
createdAt: interceptor.createdAt,
|
|
704
|
+
lastInterceptedAt: interceptor.lastInterceptedAt
|
|
705
|
+
}));
|
|
706
|
+
}
|
|
707
|
+
getHistory() {
|
|
708
|
+
return [...this.history];
|
|
709
|
+
}
|
|
710
|
+
clearHistory() {
|
|
711
|
+
this.history.length = 0;
|
|
712
|
+
}
|
|
713
|
+
getTrace() {
|
|
714
|
+
return [...this.trace];
|
|
715
|
+
}
|
|
716
|
+
clearTrace() {
|
|
717
|
+
this.trace.length = 0;
|
|
718
|
+
}
|
|
719
|
+
assertIntentAllowed(intentName) {
|
|
720
|
+
if (!this.strictRegistry && !this.validateIntentNaming) {
|
|
721
|
+
return;
|
|
722
|
+
}
|
|
723
|
+
if (this.validateIntentNaming) {
|
|
724
|
+
const validation = validateIntentName(intentName);
|
|
725
|
+
if (!validation.valid) {
|
|
726
|
+
throw new InvalidIntentNameError(intentName, validation.reason);
|
|
727
|
+
}
|
|
728
|
+
}
|
|
729
|
+
if (this.strictRegistry && this.intentRegistry && !this.intentRegistry.has(intentName)) {
|
|
730
|
+
throw new UnregisteredIntentError(intentName);
|
|
731
|
+
}
|
|
732
|
+
}
|
|
733
|
+
assertSourceAllowed(event) {
|
|
734
|
+
if (!this.requireKnownSource) return;
|
|
735
|
+
if (!event.metadata.source || event.metadata.source === "unknown") {
|
|
736
|
+
throw new UnknownEventSourceError(event.intent);
|
|
737
|
+
}
|
|
738
|
+
if (this.intentRegistry && !this.intentRegistry.has(event.metadata.source)) {
|
|
739
|
+
throw new UnknownEventSourceError(event.intent, event.metadata.source);
|
|
740
|
+
}
|
|
741
|
+
}
|
|
742
|
+
assertContractAllowed(event) {
|
|
743
|
+
if (!this.eventContracts) return;
|
|
744
|
+
const result = this.eventContracts.validate(event);
|
|
745
|
+
if (!result.ok && (this.strictEventContracts || result.contract)) {
|
|
746
|
+
throw new EventContractViolationError(event.intent, result.issues);
|
|
747
|
+
}
|
|
748
|
+
}
|
|
749
|
+
/**
|
|
750
|
+
* Enforce the OBSERVED producer→event flow (metadata.source → intent) against the
|
|
751
|
+
* architecture profile's layer rules. This is the runtime counterpart to the
|
|
752
|
+
* declared-model layer policy: it checks what the system actually did, resolving both
|
|
753
|
+
* layers directly from the profile. It runs BEFORE the flow is recorded via
|
|
754
|
+
* registerEventFlow, so in hard mode a rejected flow leaves no edge in the graph.
|
|
755
|
+
*/
|
|
756
|
+
async assertObservedLayerFlowAllowed(event) {
|
|
757
|
+
if (this.enforceObservedLayerFlowMode === "off" || !this.architectureProfile) {
|
|
758
|
+
return;
|
|
759
|
+
}
|
|
760
|
+
const source = event.metadata.source;
|
|
761
|
+
if (!source || source === "unknown") return;
|
|
762
|
+
const profile = this.architectureProfile;
|
|
763
|
+
const fromLayer = profile.resolveLayer(source);
|
|
764
|
+
const toLayer = profile.resolveLayer(event.intent);
|
|
765
|
+
if (!fromLayer || !toLayer) return;
|
|
766
|
+
const blocked = profile.rules.find(
|
|
767
|
+
(rule) => !rule.allowed && rule.from === fromLayer && rule.to === toLayer
|
|
768
|
+
);
|
|
769
|
+
if (!blocked) return;
|
|
770
|
+
const severity = this.enforceObservedLayerFlowMode;
|
|
771
|
+
const message = blocked.message ?? `Observed layer violation: "${source}" (${fromLayer}) must not produce "${event.intent}" (${toLayer}).`;
|
|
772
|
+
const details = {
|
|
773
|
+
source,
|
|
774
|
+
intent: event.intent,
|
|
775
|
+
fromLayer,
|
|
776
|
+
toLayer,
|
|
777
|
+
severity,
|
|
778
|
+
message,
|
|
779
|
+
rule: blocked
|
|
780
|
+
};
|
|
781
|
+
this.appendTrace({
|
|
782
|
+
type: "layer.observedViolation",
|
|
783
|
+
timestamp: (/* @__PURE__ */ new Date()).toISOString(),
|
|
784
|
+
intent: event.intent,
|
|
785
|
+
correlationId: event.metadata.correlationId,
|
|
786
|
+
traceId: event.metadata.traceId,
|
|
787
|
+
spanId: event.metadata.spanId,
|
|
788
|
+
details
|
|
789
|
+
});
|
|
790
|
+
await this.recordAudit("layer.observedViolation", event, details);
|
|
791
|
+
if (severity === "hard") {
|
|
792
|
+
throw new ObservedLayerFlowViolationError(source, event.intent, fromLayer, toLayer, message);
|
|
793
|
+
}
|
|
794
|
+
}
|
|
795
|
+
async recordRawPublishDiagnostic(event) {
|
|
796
|
+
const details = {
|
|
797
|
+
intent: event.intent,
|
|
798
|
+
source: event.metadata.source,
|
|
799
|
+
suggestion: "Publish through a registered intent creator so strict registry, contracts, and agent tooling share one source of truth."
|
|
800
|
+
};
|
|
801
|
+
this.appendTrace({
|
|
802
|
+
type: "event.rawPublish",
|
|
803
|
+
timestamp: (/* @__PURE__ */ new Date()).toISOString(),
|
|
804
|
+
intent: event.intent,
|
|
805
|
+
correlationId: event.metadata.correlationId,
|
|
806
|
+
traceId: event.metadata.traceId,
|
|
807
|
+
spanId: event.metadata.spanId,
|
|
808
|
+
details
|
|
809
|
+
});
|
|
810
|
+
await this.recordAudit("event.rawPublish", event, details);
|
|
811
|
+
}
|
|
812
|
+
async applyInterceptors(event) {
|
|
813
|
+
if (event.metadata.allowInterception === false) {
|
|
814
|
+
return event;
|
|
815
|
+
}
|
|
816
|
+
const matching = [...this.interceptorsByIntent.get(event.intent) ?? []];
|
|
817
|
+
let current = event;
|
|
818
|
+
for (const registration of matching) {
|
|
819
|
+
const patches = [];
|
|
820
|
+
try {
|
|
821
|
+
await Promise.resolve(
|
|
822
|
+
registration.interceptor({
|
|
823
|
+
event: current,
|
|
824
|
+
intercept: (patch) => {
|
|
825
|
+
patches.push(patch);
|
|
826
|
+
}
|
|
827
|
+
})
|
|
828
|
+
);
|
|
829
|
+
if (patches.length === 0) {
|
|
830
|
+
continue;
|
|
831
|
+
}
|
|
832
|
+
const timestamp = (/* @__PURE__ */ new Date()).toISOString();
|
|
833
|
+
let candidate = {
|
|
834
|
+
...current,
|
|
835
|
+
metadata: {
|
|
836
|
+
...current.metadata,
|
|
837
|
+
interceptions: [
|
|
838
|
+
...current.metadata.interceptions ?? [],
|
|
839
|
+
{ interceptorId: registration.interceptorId, timestamp }
|
|
840
|
+
]
|
|
841
|
+
}
|
|
842
|
+
};
|
|
843
|
+
for (const patch of patches) {
|
|
844
|
+
candidate = {
|
|
845
|
+
...candidate,
|
|
846
|
+
payload: applyPayloadPatch(candidate.payload, patch),
|
|
847
|
+
metadata: { ...candidate.metadata }
|
|
848
|
+
};
|
|
849
|
+
}
|
|
850
|
+
this.assertContractAllowed(candidate);
|
|
851
|
+
current = candidate;
|
|
852
|
+
registration.lastInterceptedAt = timestamp;
|
|
853
|
+
this.appendTrace({
|
|
854
|
+
type: "event.intercepted",
|
|
855
|
+
timestamp,
|
|
856
|
+
intent: current.intent,
|
|
857
|
+
correlationId: current.metadata.correlationId,
|
|
858
|
+
traceId: current.metadata.traceId,
|
|
859
|
+
spanId: current.metadata.spanId,
|
|
860
|
+
details: {
|
|
861
|
+
registrationId: registration.registrationId,
|
|
862
|
+
interceptorId: registration.interceptorId,
|
|
863
|
+
patchesApplied: patches.length
|
|
864
|
+
}
|
|
865
|
+
});
|
|
866
|
+
await this.recordAudit("event.intercepted", current, {
|
|
867
|
+
registrationId: registration.registrationId,
|
|
868
|
+
interceptorId: registration.interceptorId,
|
|
869
|
+
patchesApplied: patches.length
|
|
870
|
+
});
|
|
871
|
+
} catch (err) {
|
|
872
|
+
await this.recordInterceptorError(registration, current, err);
|
|
873
|
+
}
|
|
874
|
+
}
|
|
875
|
+
return current;
|
|
876
|
+
}
|
|
877
|
+
async invokeHandler(sub, event) {
|
|
878
|
+
try {
|
|
879
|
+
await Promise.resolve(sub.handler(event));
|
|
880
|
+
} catch (err) {
|
|
881
|
+
this.appendTrace({
|
|
882
|
+
type: "handler.error",
|
|
883
|
+
timestamp: (/* @__PURE__ */ new Date()).toISOString(),
|
|
884
|
+
intent: event.intent,
|
|
885
|
+
correlationId: event.metadata.correlationId,
|
|
886
|
+
traceId: event.metadata.traceId,
|
|
887
|
+
spanId: event.metadata.spanId,
|
|
888
|
+
details: { error: err instanceof Error ? err.message : String(err) }
|
|
889
|
+
});
|
|
890
|
+
await this.recordAudit("handler.error", event, {
|
|
891
|
+
handlerIntent: sub.intentName,
|
|
892
|
+
error: err instanceof Error ? err.message : String(err)
|
|
893
|
+
});
|
|
894
|
+
if (this.onHandlerError) {
|
|
895
|
+
await this.safeHook(
|
|
896
|
+
() => this.onHandlerError(err, event, sub.intentName),
|
|
897
|
+
"onHandlerError",
|
|
898
|
+
event
|
|
899
|
+
);
|
|
900
|
+
}
|
|
901
|
+
if (this.rethrowHandlerErrors) {
|
|
902
|
+
throw err;
|
|
903
|
+
}
|
|
904
|
+
}
|
|
905
|
+
}
|
|
906
|
+
async safeHook(fn, hookName, event) {
|
|
907
|
+
try {
|
|
908
|
+
await Promise.resolve(fn());
|
|
909
|
+
} catch (err) {
|
|
910
|
+
this.appendTrace({
|
|
911
|
+
type: "hook.error",
|
|
912
|
+
timestamp: (/* @__PURE__ */ new Date()).toISOString(),
|
|
913
|
+
intent: event.intent,
|
|
914
|
+
correlationId: event.metadata.correlationId,
|
|
915
|
+
traceId: event.metadata.traceId,
|
|
916
|
+
spanId: event.metadata.spanId,
|
|
917
|
+
details: {
|
|
918
|
+
hook: hookName,
|
|
919
|
+
error: err instanceof Error ? err.message : String(err)
|
|
920
|
+
}
|
|
921
|
+
});
|
|
922
|
+
await this.recordAudit("hook.error", event, {
|
|
923
|
+
hook: hookName,
|
|
924
|
+
error: err instanceof Error ? err.message : String(err)
|
|
925
|
+
});
|
|
926
|
+
}
|
|
927
|
+
}
|
|
928
|
+
async recordInterceptorError(interceptor, event, error) {
|
|
929
|
+
const message = error instanceof Error ? error.message : String(error);
|
|
930
|
+
this.appendTrace({
|
|
931
|
+
type: "interceptor.error",
|
|
932
|
+
timestamp: (/* @__PURE__ */ new Date()).toISOString(),
|
|
933
|
+
intent: event.intent,
|
|
934
|
+
correlationId: event.metadata.correlationId,
|
|
935
|
+
traceId: event.metadata.traceId,
|
|
936
|
+
spanId: event.metadata.spanId,
|
|
937
|
+
details: {
|
|
938
|
+
registrationId: interceptor.registrationId,
|
|
939
|
+
interceptorId: interceptor.interceptorId,
|
|
940
|
+
error: message
|
|
941
|
+
}
|
|
942
|
+
});
|
|
943
|
+
await this.recordAudit("interceptor.error", event, {
|
|
944
|
+
registrationId: interceptor.registrationId,
|
|
945
|
+
interceptorId: interceptor.interceptorId,
|
|
946
|
+
error: message
|
|
947
|
+
});
|
|
948
|
+
}
|
|
949
|
+
appendHistory(record) {
|
|
950
|
+
this.history.push(record);
|
|
951
|
+
if (this.maxHistorySize !== void 0 && this.history.length > this.maxHistorySize) {
|
|
952
|
+
this.history.splice(0, this.history.length - this.maxHistorySize);
|
|
953
|
+
}
|
|
954
|
+
}
|
|
955
|
+
appendTrace(record) {
|
|
956
|
+
this.trace.push(record);
|
|
957
|
+
if (this.maxHistorySize !== void 0 && this.trace.length > this.maxHistorySize) {
|
|
958
|
+
this.trace.splice(0, this.trace.length - this.maxHistorySize);
|
|
959
|
+
}
|
|
960
|
+
for (const sink of this.traceSinks) {
|
|
961
|
+
try {
|
|
962
|
+
sink(record);
|
|
963
|
+
} catch {
|
|
964
|
+
}
|
|
965
|
+
}
|
|
966
|
+
}
|
|
967
|
+
async recordAudit(type, event, details) {
|
|
968
|
+
if (!this.auditTrail) return;
|
|
969
|
+
try {
|
|
970
|
+
await this.auditTrail.record({
|
|
971
|
+
type,
|
|
972
|
+
source: event.metadata.source,
|
|
973
|
+
intent: event.intent,
|
|
974
|
+
correlationId: event.metadata.correlationId,
|
|
975
|
+
causationId: event.metadata.causationId,
|
|
976
|
+
subject: event.intent,
|
|
977
|
+
details
|
|
978
|
+
});
|
|
979
|
+
} catch (err) {
|
|
980
|
+
this.appendTrace({
|
|
981
|
+
type: "hook.error",
|
|
982
|
+
timestamp: (/* @__PURE__ */ new Date()).toISOString(),
|
|
983
|
+
intent: event.intent,
|
|
984
|
+
correlationId: event.metadata.correlationId,
|
|
985
|
+
traceId: event.metadata.traceId,
|
|
986
|
+
spanId: event.metadata.spanId,
|
|
987
|
+
details: {
|
|
988
|
+
hook: "auditTrail",
|
|
989
|
+
error: err instanceof Error ? err.message : String(err)
|
|
990
|
+
}
|
|
991
|
+
});
|
|
992
|
+
}
|
|
993
|
+
}
|
|
994
|
+
enrichMetadata(base, extra) {
|
|
995
|
+
return {
|
|
996
|
+
...base,
|
|
997
|
+
...extra,
|
|
998
|
+
occurredAt: extra.occurredAt || base.occurredAt || (/* @__PURE__ */ new Date()).toISOString(),
|
|
999
|
+
source: extra.source || base.source || "unknown",
|
|
1000
|
+
kernelInstanceId: extra.kernelInstanceId ?? base.kernelInstanceId ?? this.instanceId,
|
|
1001
|
+
eventVersion: extra.eventVersion ?? base.eventVersion,
|
|
1002
|
+
schemaVersion: extra.schemaVersion ?? base.schemaVersion,
|
|
1003
|
+
allowInterception: extra.allowInterception ?? base.allowInterception,
|
|
1004
|
+
interceptions: extra.interceptions ?? base.interceptions,
|
|
1005
|
+
correlationId: extra.correlationId ?? base.correlationId,
|
|
1006
|
+
causationId: extra.causationId ?? base.causationId,
|
|
1007
|
+
traceId: extra.traceId ?? base.traceId,
|
|
1008
|
+
spanId: extra.spanId ?? base.spanId,
|
|
1009
|
+
parentSpanId: extra.parentSpanId ?? base.parentSpanId
|
|
1010
|
+
};
|
|
1011
|
+
}
|
|
1012
|
+
};
|
|
1013
|
+
function createEventBus(options) {
|
|
1014
|
+
return new EventBusImpl(options);
|
|
1015
|
+
}
|
|
1016
|
+
|
|
1017
|
+
// src/kernel/event-contracts/EventContractRegistry.ts
|
|
1018
|
+
function formatStandardSchemaPath(path) {
|
|
1019
|
+
if (!path || path.length === 0) return void 0;
|
|
1020
|
+
return path.map(
|
|
1021
|
+
(segment) => typeof segment === "object" && segment !== null && "key" in segment ? String(segment.key) : String(segment)
|
|
1022
|
+
).join(".");
|
|
1023
|
+
}
|
|
1024
|
+
function actualType(value) {
|
|
1025
|
+
if (Array.isArray(value)) return "array";
|
|
1026
|
+
if (value === null) return "object";
|
|
1027
|
+
const type = typeof value;
|
|
1028
|
+
if (type === "string" || type === "number" || type === "boolean" || type === "object") {
|
|
1029
|
+
return type;
|
|
1030
|
+
}
|
|
1031
|
+
return "unknown";
|
|
1032
|
+
}
|
|
1033
|
+
function formatPath(parent, field) {
|
|
1034
|
+
return parent ? `${parent}.${field}` : field;
|
|
1035
|
+
}
|
|
1036
|
+
function validateSchemaField(value, fieldSchema, path, contract, issues) {
|
|
1037
|
+
const valueType = actualType(value);
|
|
1038
|
+
if (fieldSchema.type !== "unknown" && valueType !== fieldSchema.type) {
|
|
1039
|
+
issues.push({
|
|
1040
|
+
intent: contract.intent,
|
|
1041
|
+
version: contract.version,
|
|
1042
|
+
field: path,
|
|
1043
|
+
message: `Expected ${fieldSchema.type}, received ${valueType}.`
|
|
1044
|
+
});
|
|
1045
|
+
return;
|
|
1046
|
+
}
|
|
1047
|
+
if (fieldSchema.enum && !fieldSchema.enum.some((allowed) => Object.is(allowed, value))) {
|
|
1048
|
+
issues.push({
|
|
1049
|
+
intent: contract.intent,
|
|
1050
|
+
version: contract.version,
|
|
1051
|
+
field: path,
|
|
1052
|
+
message: "Value is not allowed by event contract enum."
|
|
1053
|
+
});
|
|
1054
|
+
}
|
|
1055
|
+
if (fieldSchema.type === "object" && fieldSchema.fields) {
|
|
1056
|
+
if (value === null || typeof value !== "object" || Array.isArray(value)) {
|
|
1057
|
+
issues.push({
|
|
1058
|
+
intent: contract.intent,
|
|
1059
|
+
version: contract.version,
|
|
1060
|
+
field: path,
|
|
1061
|
+
message: "Nested object field must be a non-array object."
|
|
1062
|
+
});
|
|
1063
|
+
return;
|
|
1064
|
+
}
|
|
1065
|
+
validateObjectSchema(
|
|
1066
|
+
value,
|
|
1067
|
+
fieldSchema.fields,
|
|
1068
|
+
contract,
|
|
1069
|
+
issues,
|
|
1070
|
+
path
|
|
1071
|
+
);
|
|
1072
|
+
}
|
|
1073
|
+
if (fieldSchema.type === "array" && fieldSchema.items && Array.isArray(value)) {
|
|
1074
|
+
value.forEach((item, index) => {
|
|
1075
|
+
validateSchemaField(
|
|
1076
|
+
item,
|
|
1077
|
+
fieldSchema.items,
|
|
1078
|
+
`${path}[${index}]`,
|
|
1079
|
+
contract,
|
|
1080
|
+
issues
|
|
1081
|
+
);
|
|
1082
|
+
});
|
|
1083
|
+
}
|
|
1084
|
+
}
|
|
1085
|
+
function validateObjectSchema(payload, schema, contract, issues, parentPath) {
|
|
1086
|
+
for (const [field, fieldSchema] of Object.entries(schema)) {
|
|
1087
|
+
const path = formatPath(parentPath, field);
|
|
1088
|
+
const value = payload[field];
|
|
1089
|
+
if (value === void 0) {
|
|
1090
|
+
if (fieldSchema.required) {
|
|
1091
|
+
issues.push({
|
|
1092
|
+
intent: contract.intent,
|
|
1093
|
+
version: contract.version,
|
|
1094
|
+
field: path,
|
|
1095
|
+
message: "Required field is missing."
|
|
1096
|
+
});
|
|
1097
|
+
}
|
|
1098
|
+
continue;
|
|
1099
|
+
}
|
|
1100
|
+
validateSchemaField(value, fieldSchema, path, contract, issues);
|
|
1101
|
+
}
|
|
1102
|
+
}
|
|
1103
|
+
var EventContractRegistryImpl = class {
|
|
1104
|
+
contracts = /* @__PURE__ */ new Map();
|
|
1105
|
+
register(contract) {
|
|
1106
|
+
const key = this.key(contract.intent, contract.version);
|
|
1107
|
+
if (this.contracts.has(key)) {
|
|
1108
|
+
throw new Error(`Event contract "${key}" is already registered.`);
|
|
1109
|
+
}
|
|
1110
|
+
this.contracts.set(key, { allowAdditionalFields: true, ...contract });
|
|
1111
|
+
}
|
|
1112
|
+
get(intent, version2) {
|
|
1113
|
+
if (version2) return this.contracts.get(this.key(intent, version2));
|
|
1114
|
+
return this.list(intent).at(-1);
|
|
1115
|
+
}
|
|
1116
|
+
list(intent) {
|
|
1117
|
+
return Array.from(this.contracts.values()).filter(
|
|
1118
|
+
(contract) => !intent || contract.intent === intent
|
|
1119
|
+
);
|
|
1120
|
+
}
|
|
1121
|
+
validate(event) {
|
|
1122
|
+
const version2 = event.metadata.eventVersion;
|
|
1123
|
+
const contract = this.get(event.intent, version2);
|
|
1124
|
+
const issues = [];
|
|
1125
|
+
if (!contract) {
|
|
1126
|
+
return {
|
|
1127
|
+
ok: false,
|
|
1128
|
+
issues: [
|
|
1129
|
+
{
|
|
1130
|
+
intent: event.intent,
|
|
1131
|
+
version: version2,
|
|
1132
|
+
message: version2 ? `No event contract registered for version "${version2}".` : "No event contract registered for intent."
|
|
1133
|
+
}
|
|
1134
|
+
]
|
|
1135
|
+
};
|
|
1136
|
+
}
|
|
1137
|
+
if (contract.deprecated) {
|
|
1138
|
+
issues.push({
|
|
1139
|
+
intent: event.intent,
|
|
1140
|
+
version: contract.version,
|
|
1141
|
+
message: typeof contract.deprecated === "string" ? contract.deprecated : "Event contract is deprecated."
|
|
1142
|
+
});
|
|
1143
|
+
}
|
|
1144
|
+
const payload = event.payload != null && typeof event.payload === "object" ? event.payload : void 0;
|
|
1145
|
+
if (contract.schema) {
|
|
1146
|
+
if (!payload) {
|
|
1147
|
+
issues.push({
|
|
1148
|
+
intent: event.intent,
|
|
1149
|
+
version: contract.version,
|
|
1150
|
+
message: "Payload must be an object for schema validation."
|
|
1151
|
+
});
|
|
1152
|
+
} else {
|
|
1153
|
+
validateObjectSchema(payload, contract.schema, contract, issues);
|
|
1154
|
+
if (contract.allowAdditionalFields === false) {
|
|
1155
|
+
for (const field of Object.keys(payload)) {
|
|
1156
|
+
if (!contract.schema[field]) {
|
|
1157
|
+
issues.push({
|
|
1158
|
+
intent: event.intent,
|
|
1159
|
+
version: contract.version,
|
|
1160
|
+
field,
|
|
1161
|
+
message: "Additional field is not allowed by event contract."
|
|
1162
|
+
});
|
|
1163
|
+
}
|
|
1164
|
+
}
|
|
1165
|
+
}
|
|
1166
|
+
}
|
|
1167
|
+
}
|
|
1168
|
+
if (contract.standardSchema) {
|
|
1169
|
+
const result = contract.standardSchema["~standard"].validate(event.payload);
|
|
1170
|
+
if (result instanceof Promise) {
|
|
1171
|
+
issues.push({
|
|
1172
|
+
intent: event.intent,
|
|
1173
|
+
version: contract.version,
|
|
1174
|
+
message: "Standard Schema validator returned a Promise; event contract validation is synchronous."
|
|
1175
|
+
});
|
|
1176
|
+
} else if (result.issues) {
|
|
1177
|
+
for (const issue of result.issues) {
|
|
1178
|
+
issues.push({
|
|
1179
|
+
intent: event.intent,
|
|
1180
|
+
version: contract.version,
|
|
1181
|
+
field: formatStandardSchemaPath(issue.path),
|
|
1182
|
+
message: issue.message
|
|
1183
|
+
});
|
|
1184
|
+
}
|
|
1185
|
+
}
|
|
1186
|
+
}
|
|
1187
|
+
return { ok: issues.length === 0, contract, issues };
|
|
1188
|
+
}
|
|
1189
|
+
clear() {
|
|
1190
|
+
this.contracts.clear();
|
|
1191
|
+
}
|
|
1192
|
+
key(intent, version2) {
|
|
1193
|
+
return `${intent}@${version2}`;
|
|
1194
|
+
}
|
|
1195
|
+
};
|
|
1196
|
+
function createEventContractRegistry() {
|
|
1197
|
+
return new EventContractRegistryImpl();
|
|
1198
|
+
}
|
|
1199
|
+
|
|
1200
|
+
// src/kernel/graph/DependencyGraph.ts
|
|
1201
|
+
var DependencyGraphImpl = class {
|
|
1202
|
+
nodes = /* @__PURE__ */ new Map();
|
|
1203
|
+
edges = [];
|
|
1204
|
+
registerDependency(from, to, kind = "declared") {
|
|
1205
|
+
this.ensureNode(from);
|
|
1206
|
+
this.ensureNode(to);
|
|
1207
|
+
this.addEdge({ from, to, kind });
|
|
1208
|
+
}
|
|
1209
|
+
registerEventFlow(producer, consumer) {
|
|
1210
|
+
this.ensureNode(producer);
|
|
1211
|
+
this.ensureNode(consumer);
|
|
1212
|
+
this.addEdge({ from: producer, to: consumer, kind: "observed" });
|
|
1213
|
+
}
|
|
1214
|
+
getNodes() {
|
|
1215
|
+
return Array.from(this.nodes.values());
|
|
1216
|
+
}
|
|
1217
|
+
getEdges() {
|
|
1218
|
+
return [...this.edges];
|
|
1219
|
+
}
|
|
1220
|
+
toJSON() {
|
|
1221
|
+
return {
|
|
1222
|
+
nodes: this.getNodes(),
|
|
1223
|
+
edges: this.getEdges()
|
|
1224
|
+
};
|
|
1225
|
+
}
|
|
1226
|
+
toMermaid() {
|
|
1227
|
+
let out = "flowchart TD\n";
|
|
1228
|
+
for (const edge of this.edges) {
|
|
1229
|
+
const label = edge.kind ? `|${edge.kind}|` : "";
|
|
1230
|
+
out += ` ${this.safeId(edge.from)} -->${label} ${this.safeId(edge.to)}
|
|
1231
|
+
`;
|
|
1232
|
+
}
|
|
1233
|
+
const connected = new Set(
|
|
1234
|
+
this.edges.flatMap((e) => [e.from, e.to])
|
|
1235
|
+
);
|
|
1236
|
+
for (const node of this.nodes.keys()) {
|
|
1237
|
+
if (!connected.has(node)) {
|
|
1238
|
+
out += ` ${this.safeId(node)}
|
|
1239
|
+
`;
|
|
1240
|
+
}
|
|
1241
|
+
}
|
|
1242
|
+
return out.trim();
|
|
1243
|
+
}
|
|
1244
|
+
toLayerMermaid(profile) {
|
|
1245
|
+
const nodesByLayer = /* @__PURE__ */ new Map();
|
|
1246
|
+
for (const node of this.nodes.keys()) {
|
|
1247
|
+
const layer = profile.resolveLayer(node) ?? "Unclassified";
|
|
1248
|
+
const current = nodesByLayer.get(layer) ?? [];
|
|
1249
|
+
current.push(node);
|
|
1250
|
+
nodesByLayer.set(layer, current);
|
|
1251
|
+
}
|
|
1252
|
+
let out = "flowchart TD\n";
|
|
1253
|
+
for (const layer of profile.layers) {
|
|
1254
|
+
const nodes = nodesByLayer.get(layer.name) ?? [];
|
|
1255
|
+
if (nodes.length === 0) continue;
|
|
1256
|
+
out += ` subgraph ${this.safeId(layer.name)}[${layer.name}]
|
|
1257
|
+
`;
|
|
1258
|
+
for (const node of nodes) {
|
|
1259
|
+
out += ` ${this.safeId(node)}[${node}]
|
|
1260
|
+
`;
|
|
1261
|
+
}
|
|
1262
|
+
out += " end\n";
|
|
1263
|
+
}
|
|
1264
|
+
const unclassified = nodesByLayer.get("Unclassified") ?? [];
|
|
1265
|
+
if (unclassified.length > 0) {
|
|
1266
|
+
out += " subgraph Unclassified[Unclassified]\n";
|
|
1267
|
+
for (const node of unclassified) {
|
|
1268
|
+
out += ` ${this.safeId(node)}[${node}]
|
|
1269
|
+
`;
|
|
1270
|
+
}
|
|
1271
|
+
out += " end\n";
|
|
1272
|
+
}
|
|
1273
|
+
for (const edge of this.edges) {
|
|
1274
|
+
const label = edge.kind ? `|${edge.kind}|` : "";
|
|
1275
|
+
out += ` ${this.safeId(edge.from)} -->${label} ${this.safeId(edge.to)}
|
|
1276
|
+
`;
|
|
1277
|
+
}
|
|
1278
|
+
return out.trim();
|
|
1279
|
+
}
|
|
1280
|
+
detectViolations(rules = []) {
|
|
1281
|
+
const violations = [];
|
|
1282
|
+
const cycles = this.findSimpleCycles();
|
|
1283
|
+
if (cycles.length > 0) {
|
|
1284
|
+
violations.push(`Cycle detected: ${cycles.map((c) => c.join("->")).join(", ")}`);
|
|
1285
|
+
}
|
|
1286
|
+
for (const rule of rules) {
|
|
1287
|
+
try {
|
|
1288
|
+
const res = rule(this.edges);
|
|
1289
|
+
if (res && res.length) violations.push(...res);
|
|
1290
|
+
} catch (e) {
|
|
1291
|
+
violations.push(`Rule error: ${e.message}`);
|
|
1292
|
+
}
|
|
1293
|
+
}
|
|
1294
|
+
return violations;
|
|
1295
|
+
}
|
|
1296
|
+
ensureNode(id) {
|
|
1297
|
+
if (!this.nodes.has(id)) {
|
|
1298
|
+
this.nodes.set(id, { id });
|
|
1299
|
+
}
|
|
1300
|
+
}
|
|
1301
|
+
addEdge(edge) {
|
|
1302
|
+
const exists = this.edges.some(
|
|
1303
|
+
(e) => e.from === edge.from && e.to === edge.to && e.kind === edge.kind
|
|
1304
|
+
);
|
|
1305
|
+
if (!exists) {
|
|
1306
|
+
this.edges.push(edge);
|
|
1307
|
+
}
|
|
1308
|
+
}
|
|
1309
|
+
// Very naive cycle detection for small graphs
|
|
1310
|
+
findSimpleCycles() {
|
|
1311
|
+
const graph = /* @__PURE__ */ new Map();
|
|
1312
|
+
for (const e of this.edges) {
|
|
1313
|
+
if (!graph.has(e.from)) graph.set(e.from, []);
|
|
1314
|
+
graph.get(e.from).push(e.to);
|
|
1315
|
+
}
|
|
1316
|
+
const cycles = [];
|
|
1317
|
+
const visited = /* @__PURE__ */ new Set();
|
|
1318
|
+
const stack = [];
|
|
1319
|
+
const dfs = (node) => {
|
|
1320
|
+
visited.add(node);
|
|
1321
|
+
stack.push(node);
|
|
1322
|
+
for (const nei of graph.get(node) || []) {
|
|
1323
|
+
if (!visited.has(nei)) {
|
|
1324
|
+
dfs(nei);
|
|
1325
|
+
} else if (stack.includes(nei)) {
|
|
1326
|
+
const cycleStart = stack.indexOf(nei);
|
|
1327
|
+
const cycle = stack.slice(cycleStart).concat(nei);
|
|
1328
|
+
cycles.push(cycle);
|
|
1329
|
+
}
|
|
1330
|
+
}
|
|
1331
|
+
stack.pop();
|
|
1332
|
+
};
|
|
1333
|
+
for (const node of graph.keys()) {
|
|
1334
|
+
if (!visited.has(node)) dfs(node);
|
|
1335
|
+
}
|
|
1336
|
+
const unique = new Set(cycles.map((c) => c.join("->")));
|
|
1337
|
+
return Array.from(unique).map((s) => s.split("->"));
|
|
1338
|
+
}
|
|
1339
|
+
safeId(id) {
|
|
1340
|
+
return id.replace(/[^a-zA-Z0-9_]/g, "_");
|
|
1341
|
+
}
|
|
1342
|
+
};
|
|
1343
|
+
function createDependencyGraph() {
|
|
1344
|
+
return new DependencyGraphImpl();
|
|
1345
|
+
}
|
|
1346
|
+
|
|
1347
|
+
// src/kernel/graph/sync.ts
|
|
1348
|
+
function syncRegistryToGraph(registry, graph, options = {}) {
|
|
1349
|
+
const requireRegistered = options.requireRegisteredTargets ?? false;
|
|
1350
|
+
for (const rel of registry.getAllRelationships()) {
|
|
1351
|
+
if (requireRegistered && !registry.has(rel.to)) {
|
|
1352
|
+
continue;
|
|
1353
|
+
}
|
|
1354
|
+
if (rel.kind === "dependsOn") {
|
|
1355
|
+
graph.registerDependency(rel.from, rel.to, "declared");
|
|
1356
|
+
} else {
|
|
1357
|
+
graph.registerDependency(rel.from, rel.to, "produces");
|
|
1358
|
+
}
|
|
1359
|
+
}
|
|
1360
|
+
}
|
|
1361
|
+
|
|
1362
|
+
// src/kernel/intent/IntentRegistry.ts
|
|
1363
|
+
var IntentRegistry = class {
|
|
1364
|
+
intents = /* @__PURE__ */ new Map();
|
|
1365
|
+
dependencies = /* @__PURE__ */ new Map();
|
|
1366
|
+
productions = /* @__PURE__ */ new Map();
|
|
1367
|
+
/**
|
|
1368
|
+
* Define/register a new intent.
|
|
1369
|
+
*
|
|
1370
|
+
* @param name - Semantic intent name following the convention (Domain.*, Application.*, etc.)
|
|
1371
|
+
* @param options - Optional relationship declarations
|
|
1372
|
+
* @throws Error if an intent with the same name is already registered
|
|
1373
|
+
*/
|
|
1374
|
+
define(name, options) {
|
|
1375
|
+
if (this.intents.has(name)) {
|
|
1376
|
+
throw new Error(
|
|
1377
|
+
`Intent "${name}" is already registered. Intent names must be unique within a registry.`
|
|
1378
|
+
);
|
|
1379
|
+
}
|
|
1380
|
+
const fn = (payload) => ({
|
|
1381
|
+
intent: name,
|
|
1382
|
+
payload,
|
|
1383
|
+
metadata: {
|
|
1384
|
+
occurredAt: (/* @__PURE__ */ new Date()).toISOString(),
|
|
1385
|
+
source: "unknown"
|
|
1386
|
+
}
|
|
1387
|
+
});
|
|
1388
|
+
Object.defineProperty(fn, "name", {
|
|
1389
|
+
value: name,
|
|
1390
|
+
enumerable: true,
|
|
1391
|
+
configurable: false,
|
|
1392
|
+
writable: false
|
|
1393
|
+
});
|
|
1394
|
+
const creator = fn;
|
|
1395
|
+
this.intents.set(name, creator);
|
|
1396
|
+
if (options?.dependsOn) {
|
|
1397
|
+
for (const dep of options.dependsOn) {
|
|
1398
|
+
this.declareDependency(name, dep);
|
|
1399
|
+
}
|
|
1400
|
+
}
|
|
1401
|
+
if (options?.produces) {
|
|
1402
|
+
for (const prod of options.produces) {
|
|
1403
|
+
this.declareProduction(name, prod);
|
|
1404
|
+
}
|
|
1405
|
+
}
|
|
1406
|
+
return creator;
|
|
1407
|
+
}
|
|
1408
|
+
/**
|
|
1409
|
+
* Declare that one intent depends on / relates to another.
|
|
1410
|
+
* This information is used by the DependencyGraph (future iterations) and for policy checks.
|
|
1411
|
+
*
|
|
1412
|
+
* @param from - The source intent (e.g. an Application operation)
|
|
1413
|
+
* @param to - The target intent it depends on (e.g. a Domain event or concept)
|
|
1414
|
+
*/
|
|
1415
|
+
declareDependency(from, to) {
|
|
1416
|
+
if (!this.dependencies.has(from)) {
|
|
1417
|
+
this.dependencies.set(from, /* @__PURE__ */ new Set());
|
|
1418
|
+
}
|
|
1419
|
+
this.dependencies.get(from).add(to);
|
|
1420
|
+
}
|
|
1421
|
+
/**
|
|
1422
|
+
* Declare that one intent produces / emits another (e.g. use case → domain event).
|
|
1423
|
+
*/
|
|
1424
|
+
declareProduction(from, to) {
|
|
1425
|
+
if (!this.productions.has(from)) {
|
|
1426
|
+
this.productions.set(from, /* @__PURE__ */ new Set());
|
|
1427
|
+
}
|
|
1428
|
+
this.productions.get(from).add(to);
|
|
1429
|
+
}
|
|
1430
|
+
/**
|
|
1431
|
+
* Retrieve a previously defined intent creator by name.
|
|
1432
|
+
*/
|
|
1433
|
+
get(name) {
|
|
1434
|
+
return this.intents.get(name);
|
|
1435
|
+
}
|
|
1436
|
+
/**
|
|
1437
|
+
* List all registered intent creators.
|
|
1438
|
+
*/
|
|
1439
|
+
list() {
|
|
1440
|
+
return Array.from(this.intents.values());
|
|
1441
|
+
}
|
|
1442
|
+
/**
|
|
1443
|
+
* Get all declared dependencies for a given intent.
|
|
1444
|
+
*/
|
|
1445
|
+
getDependencies(intentName) {
|
|
1446
|
+
const deps = this.dependencies.get(intentName);
|
|
1447
|
+
return deps ? Array.from(deps) : [];
|
|
1448
|
+
}
|
|
1449
|
+
/**
|
|
1450
|
+
* Get all intents produced / emitted by a given intent.
|
|
1451
|
+
*/
|
|
1452
|
+
getProductions(intentName) {
|
|
1453
|
+
const prods = this.productions.get(intentName);
|
|
1454
|
+
return prods ? Array.from(prods) : [];
|
|
1455
|
+
}
|
|
1456
|
+
/**
|
|
1457
|
+
* Get all declared relationships (useful for graph generation).
|
|
1458
|
+
*/
|
|
1459
|
+
getAllRelationships() {
|
|
1460
|
+
const result = [];
|
|
1461
|
+
for (const [from, tos] of this.dependencies.entries()) {
|
|
1462
|
+
for (const to of tos) {
|
|
1463
|
+
result.push({ from, to, kind: "dependsOn" });
|
|
1464
|
+
}
|
|
1465
|
+
}
|
|
1466
|
+
for (const [from, tos] of this.productions.entries()) {
|
|
1467
|
+
for (const to of tos) {
|
|
1468
|
+
result.push({ from, to, kind: "produces" });
|
|
1469
|
+
}
|
|
1470
|
+
}
|
|
1471
|
+
return result;
|
|
1472
|
+
}
|
|
1473
|
+
/**
|
|
1474
|
+
* Check if an intent name has been registered.
|
|
1475
|
+
*/
|
|
1476
|
+
has(name) {
|
|
1477
|
+
return this.intents.has(name);
|
|
1478
|
+
}
|
|
1479
|
+
/**
|
|
1480
|
+
* Clear the registry. Primarily useful for tests.
|
|
1481
|
+
*/
|
|
1482
|
+
clear() {
|
|
1483
|
+
this.intents.clear();
|
|
1484
|
+
this.dependencies.clear();
|
|
1485
|
+
this.productions.clear();
|
|
1486
|
+
}
|
|
1487
|
+
};
|
|
1488
|
+
function createIntentRegistry() {
|
|
1489
|
+
return new IntentRegistry();
|
|
1490
|
+
}
|
|
1491
|
+
|
|
1492
|
+
// src/kernel/layers/ArchitectureProfile.ts
|
|
1493
|
+
function normalizePrefix(prefix) {
|
|
1494
|
+
return prefix.endsWith(".") ? prefix : `${prefix}.`;
|
|
1495
|
+
}
|
|
1496
|
+
function byLongestPrefix(a, b) {
|
|
1497
|
+
const maxA = Math.max(...a.prefixes.map((p) => p.length));
|
|
1498
|
+
const maxB = Math.max(...b.prefixes.map((p) => p.length));
|
|
1499
|
+
return maxB - maxA;
|
|
1500
|
+
}
|
|
1501
|
+
function flowKey(from, to) {
|
|
1502
|
+
return `${from}->${to}`;
|
|
1503
|
+
}
|
|
1504
|
+
function createStrictDenyRules(layers, allowedFlows) {
|
|
1505
|
+
const allowed = new Set(allowedFlows.map((flow) => flowKey(flow.from, flow.to)));
|
|
1506
|
+
const rules = [];
|
|
1507
|
+
for (const from of layers) {
|
|
1508
|
+
for (const to of layers) {
|
|
1509
|
+
if (from.name === to.name) continue;
|
|
1510
|
+
if (allowed.has(flowKey(from.name, to.name))) continue;
|
|
1511
|
+
rules.push({ from: from.name, to: to.name, allowed: false });
|
|
1512
|
+
}
|
|
1513
|
+
}
|
|
1514
|
+
return rules;
|
|
1515
|
+
}
|
|
1516
|
+
function createArchitectureProfile(options) {
|
|
1517
|
+
const layers = options.layers.map((layer) => ({
|
|
1518
|
+
...layer,
|
|
1519
|
+
prefixes: layer.prefixes.map(normalizePrefix)
|
|
1520
|
+
}));
|
|
1521
|
+
const sortedLayers = [...layers].sort(byLongestPrefix);
|
|
1522
|
+
const rules = [...options.rules ?? []];
|
|
1523
|
+
return {
|
|
1524
|
+
name: options.name,
|
|
1525
|
+
layers,
|
|
1526
|
+
rules,
|
|
1527
|
+
resolveLayer(name) {
|
|
1528
|
+
return sortedLayers.find(
|
|
1529
|
+
(layer) => layer.prefixes.some((prefix) => name.startsWith(prefix))
|
|
1530
|
+
)?.name;
|
|
1531
|
+
}
|
|
1532
|
+
};
|
|
1533
|
+
}
|
|
1534
|
+
var elevenLayerProfileLayers = [
|
|
1535
|
+
{
|
|
1536
|
+
name: "DomainModel",
|
|
1537
|
+
prefixes: ["Domain"],
|
|
1538
|
+
description: "Rich domain model, business rules, and domain events.",
|
|
1539
|
+
order: 1
|
|
1540
|
+
},
|
|
1541
|
+
{
|
|
1542
|
+
name: "ApplicationOrchestration",
|
|
1543
|
+
prefixes: ["Application"],
|
|
1544
|
+
description: "Use cases and command orchestration.",
|
|
1545
|
+
order: 2
|
|
1546
|
+
},
|
|
1547
|
+
{
|
|
1548
|
+
name: "PersistenceAdapters",
|
|
1549
|
+
prefixes: ["Adapter.Persistence", "Adapter.Repository"],
|
|
1550
|
+
description: "Database, repository, and storage adapters.",
|
|
1551
|
+
order: 3
|
|
1552
|
+
},
|
|
1553
|
+
{
|
|
1554
|
+
name: "IntegrationAdapters",
|
|
1555
|
+
prefixes: ["Adapter.Integration", "Adapter.External"],
|
|
1556
|
+
description: "External systems, APIs, and integration adapters.",
|
|
1557
|
+
order: 4
|
|
1558
|
+
},
|
|
1559
|
+
{
|
|
1560
|
+
name: "WorkflowSagaEngine",
|
|
1561
|
+
prefixes: ["Workflow"],
|
|
1562
|
+
description: "Sagas, workflows, and long-running processes.",
|
|
1563
|
+
order: 5
|
|
1564
|
+
},
|
|
1565
|
+
{
|
|
1566
|
+
name: "BackgroundJobsScheduling",
|
|
1567
|
+
prefixes: ["Job"],
|
|
1568
|
+
description: "Background jobs, scheduled work, and async processors.",
|
|
1569
|
+
order: 6
|
|
1570
|
+
},
|
|
1571
|
+
{
|
|
1572
|
+
name: "PresentationAdapters",
|
|
1573
|
+
prefixes: ["Presentation", "Adapter.Presentation", "Adapter.Api"],
|
|
1574
|
+
description: "API, UI, controller, and presentation adapters.",
|
|
1575
|
+
order: 7
|
|
1576
|
+
},
|
|
1577
|
+
{
|
|
1578
|
+
name: "ReportingReadModels",
|
|
1579
|
+
prefixes: ["Reporting"],
|
|
1580
|
+
description: "Read models, projections, and reporting surfaces.",
|
|
1581
|
+
order: 8
|
|
1582
|
+
},
|
|
1583
|
+
{
|
|
1584
|
+
name: "ExtensibilityMetadata",
|
|
1585
|
+
prefixes: ["Metadata"],
|
|
1586
|
+
description: "Metadata, extensions, and schema contracts.",
|
|
1587
|
+
order: 9
|
|
1588
|
+
},
|
|
1589
|
+
{
|
|
1590
|
+
name: "SecurityAuditObservability",
|
|
1591
|
+
prefixes: ["Security", "Audit", "Observability"],
|
|
1592
|
+
description: "Security, audit, and observability concerns.",
|
|
1593
|
+
order: 10
|
|
1594
|
+
},
|
|
1595
|
+
{
|
|
1596
|
+
name: "Kernel",
|
|
1597
|
+
prefixes: ["Kernel"],
|
|
1598
|
+
description: "Ark-owned governance and kernel signals.",
|
|
1599
|
+
order: 11
|
|
1600
|
+
}
|
|
1601
|
+
];
|
|
1602
|
+
var elevenLayerAllowedFlows = [
|
|
1603
|
+
{ from: "PresentationAdapters", to: "ApplicationOrchestration" },
|
|
1604
|
+
{ from: "ApplicationOrchestration", to: "DomainModel" },
|
|
1605
|
+
{ from: "WorkflowSagaEngine", to: "ApplicationOrchestration" },
|
|
1606
|
+
{ from: "WorkflowSagaEngine", to: "DomainModel" },
|
|
1607
|
+
{ from: "BackgroundJobsScheduling", to: "ApplicationOrchestration" }
|
|
1608
|
+
];
|
|
1609
|
+
var elevenLayerProfile = createArchitectureProfile({
|
|
1610
|
+
name: "Ark 11-layer Hexagonal Event-Driven Profile",
|
|
1611
|
+
layers: elevenLayerProfileLayers,
|
|
1612
|
+
rules: createStrictDenyRules(
|
|
1613
|
+
elevenLayerProfileLayers,
|
|
1614
|
+
elevenLayerAllowedFlows
|
|
1615
|
+
)
|
|
1616
|
+
});
|
|
1617
|
+
|
|
1618
|
+
// src/kernel/manifest/constants.ts
|
|
1619
|
+
var MANIFEST_SCHEMA_VERSION = "1.0";
|
|
1620
|
+
|
|
1621
|
+
// src/version.ts
|
|
1622
|
+
var version = "1.0.0";
|
|
1623
|
+
|
|
1624
|
+
// src/kernel/manifest/createArkManifest.ts
|
|
1625
|
+
function policyId(name) {
|
|
1626
|
+
return name.toLowerCase().replace(/[^a-z0-9]+/g, "-").replace(/^-|-$/g, "");
|
|
1627
|
+
}
|
|
1628
|
+
var ArkManifestImpl = class {
|
|
1629
|
+
constructor(data) {
|
|
1630
|
+
this.data = data;
|
|
1631
|
+
}
|
|
1632
|
+
data;
|
|
1633
|
+
toJSON() {
|
|
1634
|
+
return { ...this.data };
|
|
1635
|
+
}
|
|
1636
|
+
};
|
|
1637
|
+
function createArkManifest(options = {}) {
|
|
1638
|
+
const registry = options.registry;
|
|
1639
|
+
const policyEngine = options.policyEngine;
|
|
1640
|
+
const metadata = options.metadata;
|
|
1641
|
+
const graph = options.graph;
|
|
1642
|
+
const profile = options.profile;
|
|
1643
|
+
const projections = options.projections;
|
|
1644
|
+
const eventContracts = options.eventContracts;
|
|
1645
|
+
const observability = options.observability;
|
|
1646
|
+
const intents = registry ? registry.list().map((creator) => ({
|
|
1647
|
+
name: creator.name,
|
|
1648
|
+
dependencies: registry.getDependencies(creator.name),
|
|
1649
|
+
productions: registry.getProductions(creator.name)
|
|
1650
|
+
})) : [];
|
|
1651
|
+
const relationships = registry ? registry.getAllRelationships() : [];
|
|
1652
|
+
const policies = policyEngine ? policyEngine.getPolicies().map((p) => ({
|
|
1653
|
+
id: policyId(p.name),
|
|
1654
|
+
name: p.name,
|
|
1655
|
+
severity: p.severity,
|
|
1656
|
+
tags: p.tags ? [...p.tags] : void 0,
|
|
1657
|
+
owner: p.owner,
|
|
1658
|
+
version: p.version,
|
|
1659
|
+
rationale: p.rationale,
|
|
1660
|
+
enforcementMode: p.enforcementMode,
|
|
1661
|
+
deprecated: p.deprecated,
|
|
1662
|
+
replacedBy: p.replacedBy,
|
|
1663
|
+
description: p.tags?.includes("layer") ? "Enforces clean-architecture layer dependency rules on declared relationships." : void 0
|
|
1664
|
+
})) : [];
|
|
1665
|
+
const entities = metadata ? metadata.toJSON() : [];
|
|
1666
|
+
const graphData = graph ? graph.toJSON() : { nodes: [], edges: [] };
|
|
1667
|
+
const entityIntents = entities.filter((e) => (e.emits?.length ?? 0) > 0 || (e.consumes?.length ?? 0) > 0).map((e) => ({
|
|
1668
|
+
entity: e.name,
|
|
1669
|
+
emits: e.emits,
|
|
1670
|
+
consumes: e.consumes
|
|
1671
|
+
}));
|
|
1672
|
+
const projectionData = projections ? projections.list().map((projection) => ({
|
|
1673
|
+
name: projection.name,
|
|
1674
|
+
sourceIntents: projection.sourceIntents,
|
|
1675
|
+
checkpoint: projections.getCheckpoint(projection.name)
|
|
1676
|
+
})) : [];
|
|
1677
|
+
const data = {
|
|
1678
|
+
schemaVersion: MANIFEST_SCHEMA_VERSION,
|
|
1679
|
+
version,
|
|
1680
|
+
exportedAt: (/* @__PURE__ */ new Date()).toISOString(),
|
|
1681
|
+
intents,
|
|
1682
|
+
relationships,
|
|
1683
|
+
policies,
|
|
1684
|
+
entities,
|
|
1685
|
+
graph: graphData,
|
|
1686
|
+
architecture: profile ? {
|
|
1687
|
+
profile: profile.name,
|
|
1688
|
+
layers: profile.layers,
|
|
1689
|
+
rules: profile.rules
|
|
1690
|
+
} : void 0,
|
|
1691
|
+
projections: projectionData,
|
|
1692
|
+
eventContracts: eventContracts?.list() ?? [],
|
|
1693
|
+
observability: observability?.report(),
|
|
1694
|
+
links: { entityIntents }
|
|
1695
|
+
};
|
|
1696
|
+
return new ArkManifestImpl(data);
|
|
1697
|
+
}
|
|
1698
|
+
|
|
1699
|
+
// src/kernel/metadata/MetadataRegistry.ts
|
|
1700
|
+
var MetadataRegistryImpl = class {
|
|
1701
|
+
entities = /* @__PURE__ */ new Map();
|
|
1702
|
+
entity(name, meta, options = {}) {
|
|
1703
|
+
if (this.entities.has(name) && !options.allowOverwrite) {
|
|
1704
|
+
throw new Error(`Entity metadata "${name}" is already registered.`);
|
|
1705
|
+
}
|
|
1706
|
+
const full = { name, fields: {}, ...meta };
|
|
1707
|
+
this.entities.set(name, full);
|
|
1708
|
+
return full;
|
|
1709
|
+
}
|
|
1710
|
+
getEntity(name) {
|
|
1711
|
+
return this.entities.get(name);
|
|
1712
|
+
}
|
|
1713
|
+
listEntities() {
|
|
1714
|
+
return Array.from(this.entities.values());
|
|
1715
|
+
}
|
|
1716
|
+
findEntitiesByIntent(intentName) {
|
|
1717
|
+
return this.listEntities().filter(
|
|
1718
|
+
(entity) => entity.emits?.includes(intentName) || entity.consumes?.includes(intentName)
|
|
1719
|
+
);
|
|
1720
|
+
}
|
|
1721
|
+
validate() {
|
|
1722
|
+
const issues = [];
|
|
1723
|
+
for (const entity of this.entities.values()) {
|
|
1724
|
+
if (!entity.name.trim()) {
|
|
1725
|
+
issues.push({ entity: entity.name, message: "Entity name is required." });
|
|
1726
|
+
}
|
|
1727
|
+
for (const [fieldName, field] of Object.entries(entity.fields)) {
|
|
1728
|
+
if (!fieldName.trim()) {
|
|
1729
|
+
issues.push({ entity: entity.name, field: fieldName, message: "Field name is required." });
|
|
1730
|
+
}
|
|
1731
|
+
if (!field.type || typeof field.type !== "string") {
|
|
1732
|
+
issues.push({
|
|
1733
|
+
entity: entity.name,
|
|
1734
|
+
field: fieldName,
|
|
1735
|
+
message: "Field type must be a non-empty string."
|
|
1736
|
+
});
|
|
1737
|
+
}
|
|
1738
|
+
if (field.relation && !this.entities.has(field.relation.entity)) {
|
|
1739
|
+
issues.push({
|
|
1740
|
+
entity: entity.name,
|
|
1741
|
+
field: fieldName,
|
|
1742
|
+
message: `Related entity "${field.relation.entity}" is not registered.`
|
|
1743
|
+
});
|
|
1744
|
+
}
|
|
1745
|
+
}
|
|
1746
|
+
}
|
|
1747
|
+
return { ok: issues.length === 0, issues };
|
|
1748
|
+
}
|
|
1749
|
+
toJSON() {
|
|
1750
|
+
return this.listEntities();
|
|
1751
|
+
}
|
|
1752
|
+
};
|
|
1753
|
+
function createMetadataRegistry() {
|
|
1754
|
+
return new MetadataRegistryImpl();
|
|
1755
|
+
}
|
|
1756
|
+
|
|
1757
|
+
// src/kernel/observability/ObservabilityReporter.ts
|
|
1758
|
+
function flowKey2(flow) {
|
|
1759
|
+
return `${flow.from}->${flow.to}`;
|
|
1760
|
+
}
|
|
1761
|
+
function uniqueFlows(flows) {
|
|
1762
|
+
const seen = /* @__PURE__ */ new Set();
|
|
1763
|
+
const result = [];
|
|
1764
|
+
for (const flow of flows) {
|
|
1765
|
+
const key = flowKey2(flow);
|
|
1766
|
+
if (seen.has(key)) continue;
|
|
1767
|
+
seen.add(key);
|
|
1768
|
+
result.push(flow);
|
|
1769
|
+
}
|
|
1770
|
+
return result;
|
|
1771
|
+
}
|
|
1772
|
+
function difference(left, right) {
|
|
1773
|
+
const rightKeys = new Set(right.map(flowKey2));
|
|
1774
|
+
return left.filter((flow) => !rightKeys.has(flowKey2(flow)));
|
|
1775
|
+
}
|
|
1776
|
+
function createObservabilityReporter(options) {
|
|
1777
|
+
const registry = options.registry;
|
|
1778
|
+
const eventBus = options.eventBus;
|
|
1779
|
+
const graph = options.graph;
|
|
1780
|
+
return {
|
|
1781
|
+
report() {
|
|
1782
|
+
const declaredFromRegistry = registry ? registry.getAllRelationships().filter((relationship) => relationship.kind === "produces").map((relationship) => ({
|
|
1783
|
+
from: relationship.from,
|
|
1784
|
+
to: relationship.to
|
|
1785
|
+
})) : [];
|
|
1786
|
+
const declaredFromGraph = declaredFromRegistry.length > 0 || !graph ? [] : graph.getEdges().filter((edge) => edge.kind === "produces").map((edge) => ({ from: edge.from, to: edge.to }));
|
|
1787
|
+
const observedFromHistory = eventBus?.getHistory().map((record) => ({
|
|
1788
|
+
from: record.event.metadata.source,
|
|
1789
|
+
to: record.event.intent
|
|
1790
|
+
})) ?? [];
|
|
1791
|
+
const observedFromGraph = observedFromHistory.length > 0 || !graph ? [] : graph.getEdges().filter((edge) => edge.kind === "observed").map((edge) => ({ from: edge.from, to: edge.to }));
|
|
1792
|
+
const declaredProductions = uniqueFlows([
|
|
1793
|
+
...declaredFromRegistry,
|
|
1794
|
+
...declaredFromGraph
|
|
1795
|
+
]);
|
|
1796
|
+
const observedProductions = uniqueFlows([
|
|
1797
|
+
...observedFromHistory,
|
|
1798
|
+
...observedFromGraph
|
|
1799
|
+
]);
|
|
1800
|
+
const unknownSources = observedProductions.filter(
|
|
1801
|
+
(flow) => !flow.from || flow.from === "unknown"
|
|
1802
|
+
);
|
|
1803
|
+
const unregisteredObservedSources = registry ? Array.from(
|
|
1804
|
+
new Set(
|
|
1805
|
+
observedProductions.map((flow) => flow.from).filter(
|
|
1806
|
+
(source) => source && source !== "unknown" && !registry.has(source)
|
|
1807
|
+
)
|
|
1808
|
+
)
|
|
1809
|
+
) : [];
|
|
1810
|
+
const unregisteredObservedIntents = registry ? Array.from(
|
|
1811
|
+
new Set(
|
|
1812
|
+
observedProductions.map((flow) => flow.to).filter((intent) => !registry.has(intent))
|
|
1813
|
+
)
|
|
1814
|
+
) : [];
|
|
1815
|
+
const observedIntentNames = new Set(
|
|
1816
|
+
observedProductions.flatMap((flow) => [flow.from, flow.to])
|
|
1817
|
+
);
|
|
1818
|
+
const registeredButNeverObserved = registry ? registry.list().map((intent) => intent.name).filter((intent) => !observedIntentNames.has(intent)) : [];
|
|
1819
|
+
return {
|
|
1820
|
+
generatedAt: (/* @__PURE__ */ new Date()).toISOString(),
|
|
1821
|
+
declaredProductions,
|
|
1822
|
+
observedProductions,
|
|
1823
|
+
declaredButUnobserved: difference(declaredProductions, observedProductions),
|
|
1824
|
+
observedButUndeclared: difference(
|
|
1825
|
+
observedProductions.filter((flow) => flow.from !== "unknown"),
|
|
1826
|
+
declaredProductions
|
|
1827
|
+
),
|
|
1828
|
+
unknownSources,
|
|
1829
|
+
unregisteredObservedSources,
|
|
1830
|
+
unregisteredObservedIntents,
|
|
1831
|
+
registeredButNeverObserved
|
|
1832
|
+
};
|
|
1833
|
+
}
|
|
1834
|
+
};
|
|
1835
|
+
}
|
|
1836
|
+
|
|
1837
|
+
// src/kernel/outbox/InMemoryOutboxStore.ts
|
|
1838
|
+
var outboxSequence = 0;
|
|
1839
|
+
function nextOutboxId() {
|
|
1840
|
+
outboxSequence += 1;
|
|
1841
|
+
return `outbox-${Date.now()}-${outboxSequence}`;
|
|
1842
|
+
}
|
|
1843
|
+
function cloneRecord(record) {
|
|
1844
|
+
return {
|
|
1845
|
+
...record,
|
|
1846
|
+
event: {
|
|
1847
|
+
...record.event,
|
|
1848
|
+
metadata: { ...record.event.metadata }
|
|
1849
|
+
}
|
|
1850
|
+
};
|
|
1851
|
+
}
|
|
1852
|
+
var InMemoryOutboxStore = class {
|
|
1853
|
+
records = /* @__PURE__ */ new Map();
|
|
1854
|
+
async enqueue(event) {
|
|
1855
|
+
const now = (/* @__PURE__ */ new Date()).toISOString();
|
|
1856
|
+
const record = {
|
|
1857
|
+
id: nextOutboxId(),
|
|
1858
|
+
event,
|
|
1859
|
+
status: "pending",
|
|
1860
|
+
attempts: 0,
|
|
1861
|
+
createdAt: now,
|
|
1862
|
+
updatedAt: now
|
|
1863
|
+
};
|
|
1864
|
+
this.records.set(record.id, record);
|
|
1865
|
+
return cloneRecord(record);
|
|
1866
|
+
}
|
|
1867
|
+
async markDispatched(id) {
|
|
1868
|
+
const record = this.records.get(id);
|
|
1869
|
+
if (!record) return;
|
|
1870
|
+
record.status = "dispatched";
|
|
1871
|
+
record.updatedAt = (/* @__PURE__ */ new Date()).toISOString();
|
|
1872
|
+
}
|
|
1873
|
+
async markFailed(id, error) {
|
|
1874
|
+
const record = this.records.get(id);
|
|
1875
|
+
if (!record) return;
|
|
1876
|
+
record.status = "failed";
|
|
1877
|
+
record.attempts += 1;
|
|
1878
|
+
record.error = error instanceof Error ? error.message : String(error);
|
|
1879
|
+
record.updatedAt = (/* @__PURE__ */ new Date()).toISOString();
|
|
1880
|
+
}
|
|
1881
|
+
async list(status) {
|
|
1882
|
+
return Array.from(this.records.values()).filter((record) => !status || record.status === status).map(cloneRecord);
|
|
1883
|
+
}
|
|
1884
|
+
async clear() {
|
|
1885
|
+
this.records.clear();
|
|
1886
|
+
}
|
|
1887
|
+
};
|
|
1888
|
+
|
|
1889
|
+
// src/kernel/projections/ProjectionRegistry.ts
|
|
1890
|
+
var InMemoryReadModelStore = class {
|
|
1891
|
+
states = /* @__PURE__ */ new Map();
|
|
1892
|
+
load(name) {
|
|
1893
|
+
return this.states.get(name);
|
|
1894
|
+
}
|
|
1895
|
+
save(name, state) {
|
|
1896
|
+
this.states.set(name, state);
|
|
1897
|
+
}
|
|
1898
|
+
clear(name) {
|
|
1899
|
+
if (name) {
|
|
1900
|
+
this.states.delete(name);
|
|
1901
|
+
} else {
|
|
1902
|
+
this.states.clear();
|
|
1903
|
+
}
|
|
1904
|
+
}
|
|
1905
|
+
};
|
|
1906
|
+
function initialState(definition) {
|
|
1907
|
+
return typeof definition.initialState === "function" ? definition.initialState() : definition.initialState;
|
|
1908
|
+
}
|
|
1909
|
+
var ProjectionRegistryImpl = class {
|
|
1910
|
+
definitions = /* @__PURE__ */ new Map();
|
|
1911
|
+
checkpoints = /* @__PURE__ */ new Map();
|
|
1912
|
+
store;
|
|
1913
|
+
auditTrail;
|
|
1914
|
+
constructor(options = {}) {
|
|
1915
|
+
this.store = options.store ?? new InMemoryReadModelStore();
|
|
1916
|
+
this.auditTrail = options.auditTrail;
|
|
1917
|
+
}
|
|
1918
|
+
register(definition) {
|
|
1919
|
+
if (this.definitions.has(definition.name)) {
|
|
1920
|
+
throw new Error(`Projection "${definition.name}" is already registered.`);
|
|
1921
|
+
}
|
|
1922
|
+
this.definitions.set(definition.name, definition);
|
|
1923
|
+
this.checkpoints.set(definition.name, {
|
|
1924
|
+
projection: definition.name,
|
|
1925
|
+
appliedCount: 0
|
|
1926
|
+
});
|
|
1927
|
+
}
|
|
1928
|
+
list() {
|
|
1929
|
+
return Array.from(this.definitions.values());
|
|
1930
|
+
}
|
|
1931
|
+
async apply(event) {
|
|
1932
|
+
const applied = [];
|
|
1933
|
+
for (const definition of this.definitions.values()) {
|
|
1934
|
+
if (!definition.sourceIntents.includes(event.intent)) continue;
|
|
1935
|
+
const current = await this.store.load(definition.name) ?? initialState(definition);
|
|
1936
|
+
const next = await definition.project(event, current);
|
|
1937
|
+
await this.store.save(definition.name, next);
|
|
1938
|
+
const previous = this.checkpoints.get(definition.name);
|
|
1939
|
+
this.checkpoints.set(definition.name, {
|
|
1940
|
+
projection: definition.name,
|
|
1941
|
+
appliedCount: (previous?.appliedCount ?? 0) + 1,
|
|
1942
|
+
lastIntent: event.intent,
|
|
1943
|
+
lastCorrelationId: event.metadata.correlationId,
|
|
1944
|
+
updatedAt: (/* @__PURE__ */ new Date()).toISOString()
|
|
1945
|
+
});
|
|
1946
|
+
await this.auditTrail?.record({
|
|
1947
|
+
type: "projection.applied",
|
|
1948
|
+
source: "Kernel.ProjectionRegistry",
|
|
1949
|
+
intent: event.intent,
|
|
1950
|
+
correlationId: event.metadata.correlationId,
|
|
1951
|
+
causationId: event.metadata.causationId,
|
|
1952
|
+
subject: definition.name,
|
|
1953
|
+
details: { projection: definition.name }
|
|
1954
|
+
});
|
|
1955
|
+
applied.push(definition.name);
|
|
1956
|
+
}
|
|
1957
|
+
return applied;
|
|
1958
|
+
}
|
|
1959
|
+
async getState(name) {
|
|
1960
|
+
return this.store.load(name);
|
|
1961
|
+
}
|
|
1962
|
+
getCheckpoint(name) {
|
|
1963
|
+
return this.checkpoints.get(name);
|
|
1964
|
+
}
|
|
1965
|
+
getCheckpoints() {
|
|
1966
|
+
return Array.from(this.checkpoints.values());
|
|
1967
|
+
}
|
|
1968
|
+
async clear() {
|
|
1969
|
+
await this.store.clear();
|
|
1970
|
+
for (const definition of this.definitions.values()) {
|
|
1971
|
+
this.checkpoints.set(definition.name, {
|
|
1972
|
+
projection: definition.name,
|
|
1973
|
+
appliedCount: 0
|
|
1974
|
+
});
|
|
1975
|
+
}
|
|
1976
|
+
}
|
|
1977
|
+
};
|
|
1978
|
+
function createProjectionRegistry(options) {
|
|
1979
|
+
return new ProjectionRegistryImpl(options);
|
|
1980
|
+
}
|
|
1981
|
+
|
|
1982
|
+
// src/kernel/workflow/Saga.ts
|
|
1983
|
+
var workflowSequence = 0;
|
|
1984
|
+
function createWorkflowId(prefix) {
|
|
1985
|
+
workflowSequence += 1;
|
|
1986
|
+
return `${prefix}-${Date.now()}-${workflowSequence}`;
|
|
1987
|
+
}
|
|
1988
|
+
function errorMessage(error) {
|
|
1989
|
+
return error instanceof Error ? error.message : String(error);
|
|
1990
|
+
}
|
|
1991
|
+
function sleep(ms) {
|
|
1992
|
+
return new Promise((resolve) => {
|
|
1993
|
+
setTimeout(resolve, ms);
|
|
1994
|
+
});
|
|
1995
|
+
}
|
|
1996
|
+
async function withTimeout(operation, timeoutMs, stepName) {
|
|
1997
|
+
if (timeoutMs === void 0) return operation;
|
|
1998
|
+
let timeout;
|
|
1999
|
+
const timeoutPromise = new Promise((_, reject) => {
|
|
2000
|
+
timeout = setTimeout(() => {
|
|
2001
|
+
reject(new Error(`Workflow step "${stepName}" timed out after ${timeoutMs}ms.`));
|
|
2002
|
+
}, timeoutMs);
|
|
2003
|
+
});
|
|
2004
|
+
try {
|
|
2005
|
+
return await Promise.race([operation, timeoutPromise]);
|
|
2006
|
+
} finally {
|
|
2007
|
+
if (timeout) clearTimeout(timeout);
|
|
2008
|
+
}
|
|
2009
|
+
}
|
|
2010
|
+
var InMemoryWorkflowStore = class {
|
|
2011
|
+
snapshots = /* @__PURE__ */ new Map();
|
|
2012
|
+
save(snapshot) {
|
|
2013
|
+
this.snapshots.set(snapshot.id, { ...snapshot, context: { ...snapshot.context } });
|
|
2014
|
+
}
|
|
2015
|
+
get(id) {
|
|
2016
|
+
const snapshot = this.snapshots.get(id);
|
|
2017
|
+
return snapshot ? { ...snapshot, context: { ...snapshot.context } } : void 0;
|
|
2018
|
+
}
|
|
2019
|
+
list(workflowName) {
|
|
2020
|
+
return Array.from(this.snapshots.values()).filter((snapshot) => !workflowName || snapshot.workflowName === workflowName).map((snapshot) => ({ ...snapshot, context: { ...snapshot.context } }));
|
|
2021
|
+
}
|
|
2022
|
+
clear() {
|
|
2023
|
+
this.snapshots.clear();
|
|
2024
|
+
}
|
|
2025
|
+
};
|
|
2026
|
+
var WorkflowEngineImpl = class {
|
|
2027
|
+
constructor(bus, options = {}) {
|
|
2028
|
+
this.bus = bus;
|
|
2029
|
+
this.options = options;
|
|
2030
|
+
this.store = options.store ?? new InMemoryWorkflowStore();
|
|
2031
|
+
}
|
|
2032
|
+
bus;
|
|
2033
|
+
options;
|
|
2034
|
+
definitions = /* @__PURE__ */ new Map();
|
|
2035
|
+
store;
|
|
2036
|
+
register(definition) {
|
|
2037
|
+
if (this.definitions.has(definition.name)) {
|
|
2038
|
+
throw new Error(`Workflow "${definition.name}" is already registered.`);
|
|
2039
|
+
}
|
|
2040
|
+
this.definitions.set(definition.name, definition);
|
|
2041
|
+
if (definition.startOn) {
|
|
2042
|
+
const trigger = definition.startOn;
|
|
2043
|
+
this.bus.subscribe(trigger.intent, async (event) => {
|
|
2044
|
+
await this.start(definition.name, trigger.mapEventToPayload(event));
|
|
2045
|
+
});
|
|
2046
|
+
}
|
|
2047
|
+
}
|
|
2048
|
+
async start(workflowName, initialPayload, options = {}) {
|
|
2049
|
+
const definition = this.definitions.get(workflowName);
|
|
2050
|
+
if (!definition) {
|
|
2051
|
+
throw new Error(`Workflow "${workflowName}" is not registered.`);
|
|
2052
|
+
}
|
|
2053
|
+
const now = (/* @__PURE__ */ new Date()).toISOString();
|
|
2054
|
+
const snapshot = {
|
|
2055
|
+
id: options.id ?? createWorkflowId(workflowName),
|
|
2056
|
+
workflowName,
|
|
2057
|
+
status: "running",
|
|
2058
|
+
context: { ...initialPayload },
|
|
2059
|
+
completedSteps: [],
|
|
2060
|
+
attempts: {},
|
|
2061
|
+
startedAt: now,
|
|
2062
|
+
updatedAt: now
|
|
2063
|
+
};
|
|
2064
|
+
await this.store.save(snapshot);
|
|
2065
|
+
await this.audit("workflow.started", snapshot, { workflowName });
|
|
2066
|
+
try {
|
|
2067
|
+
for (const step of definition.steps) {
|
|
2068
|
+
await this.runStep(snapshot, step);
|
|
2069
|
+
}
|
|
2070
|
+
snapshot.status = "completed";
|
|
2071
|
+
snapshot.currentStep = void 0;
|
|
2072
|
+
snapshot.completedAt = (/* @__PURE__ */ new Date()).toISOString();
|
|
2073
|
+
snapshot.updatedAt = snapshot.completedAt;
|
|
2074
|
+
await this.store.save(snapshot);
|
|
2075
|
+
await this.audit("workflow.completed", snapshot, { workflowName });
|
|
2076
|
+
return { ...snapshot, context: { ...snapshot.context } };
|
|
2077
|
+
} catch (err) {
|
|
2078
|
+
await this.compensate(snapshot, definition.steps, err);
|
|
2079
|
+
snapshot.status = "failed";
|
|
2080
|
+
snapshot.error = errorMessage(err);
|
|
2081
|
+
snapshot.updatedAt = (/* @__PURE__ */ new Date()).toISOString();
|
|
2082
|
+
await this.store.save(snapshot);
|
|
2083
|
+
await this.audit("workflow.failed", snapshot, {
|
|
2084
|
+
workflowName,
|
|
2085
|
+
error: snapshot.error,
|
|
2086
|
+
failedStep: snapshot.failedStep
|
|
2087
|
+
});
|
|
2088
|
+
throw err;
|
|
2089
|
+
}
|
|
2090
|
+
}
|
|
2091
|
+
async get(id) {
|
|
2092
|
+
return this.store.get(id);
|
|
2093
|
+
}
|
|
2094
|
+
async list(workflowName) {
|
|
2095
|
+
return this.store.list(workflowName);
|
|
2096
|
+
}
|
|
2097
|
+
async runStep(snapshot, step) {
|
|
2098
|
+
const retry = step.retry ?? this.options.defaultRetry ?? { attempts: 1 };
|
|
2099
|
+
const maxAttempts = Math.max(1, retry.attempts);
|
|
2100
|
+
snapshot.currentStep = step.name;
|
|
2101
|
+
snapshot.updatedAt = (/* @__PURE__ */ new Date()).toISOString();
|
|
2102
|
+
await this.store.save(snapshot);
|
|
2103
|
+
for (let attempt = 1; attempt <= maxAttempts; attempt += 1) {
|
|
2104
|
+
snapshot.attempts[step.name] = attempt;
|
|
2105
|
+
await this.store.save(snapshot);
|
|
2106
|
+
try {
|
|
2107
|
+
const result = await withTimeout(
|
|
2108
|
+
Promise.resolve(step.execute(snapshot.context, this.bus)),
|
|
2109
|
+
step.timeoutMs,
|
|
2110
|
+
step.name
|
|
2111
|
+
);
|
|
2112
|
+
if (result) Object.assign(snapshot.context, result);
|
|
2113
|
+
snapshot.completedSteps.push(step.name);
|
|
2114
|
+
snapshot.currentStep = void 0;
|
|
2115
|
+
snapshot.updatedAt = (/* @__PURE__ */ new Date()).toISOString();
|
|
2116
|
+
await this.store.save(snapshot);
|
|
2117
|
+
await this.audit("workflow.step.completed", snapshot, {
|
|
2118
|
+
step: step.name,
|
|
2119
|
+
attempt
|
|
2120
|
+
});
|
|
2121
|
+
return;
|
|
2122
|
+
} catch (err) {
|
|
2123
|
+
if (attempt < maxAttempts) {
|
|
2124
|
+
if (retry.delayMs) await sleep(retry.delayMs);
|
|
2125
|
+
continue;
|
|
2126
|
+
}
|
|
2127
|
+
snapshot.failedStep = step.name;
|
|
2128
|
+
snapshot.updatedAt = (/* @__PURE__ */ new Date()).toISOString();
|
|
2129
|
+
await this.store.save(snapshot);
|
|
2130
|
+
await this.audit("workflow.step.failed", snapshot, {
|
|
2131
|
+
step: step.name,
|
|
2132
|
+
attempt,
|
|
2133
|
+
error: errorMessage(err)
|
|
2134
|
+
});
|
|
2135
|
+
throw err;
|
|
2136
|
+
}
|
|
2137
|
+
}
|
|
2138
|
+
throw new Error(`Workflow step "${step.name}" did not complete.`);
|
|
2139
|
+
}
|
|
2140
|
+
async compensate(snapshot, steps, error) {
|
|
2141
|
+
snapshot.status = "compensating";
|
|
2142
|
+
snapshot.updatedAt = (/* @__PURE__ */ new Date()).toISOString();
|
|
2143
|
+
await this.store.save(snapshot);
|
|
2144
|
+
const completed = steps.filter(
|
|
2145
|
+
(step) => snapshot.completedSteps.includes(step.name)
|
|
2146
|
+
);
|
|
2147
|
+
for (let index = completed.length - 1; index >= 0; index -= 1) {
|
|
2148
|
+
const step = completed[index];
|
|
2149
|
+
if (!step.compensate) continue;
|
|
2150
|
+
try {
|
|
2151
|
+
await Promise.resolve(step.compensate(snapshot.context, this.bus, error));
|
|
2152
|
+
await this.audit("workflow.compensation.completed", snapshot, {
|
|
2153
|
+
step: step.name
|
|
2154
|
+
});
|
|
2155
|
+
} catch (compensationError) {
|
|
2156
|
+
await this.audit("workflow.step.failed", snapshot, {
|
|
2157
|
+
step: step.name,
|
|
2158
|
+
compensation: true,
|
|
2159
|
+
error: errorMessage(compensationError)
|
|
2160
|
+
});
|
|
2161
|
+
}
|
|
2162
|
+
}
|
|
2163
|
+
}
|
|
2164
|
+
async audit(type, snapshot, details) {
|
|
2165
|
+
await this.options.auditTrail?.record({
|
|
2166
|
+
type,
|
|
2167
|
+
source: "Kernel.WorkflowEngine",
|
|
2168
|
+
subject: snapshot.id,
|
|
2169
|
+
details
|
|
2170
|
+
});
|
|
2171
|
+
}
|
|
2172
|
+
};
|
|
2173
|
+
function createWorkflowEngine(bus, options) {
|
|
2174
|
+
return new WorkflowEngineImpl(bus, options);
|
|
2175
|
+
}
|
|
2176
|
+
|
|
2177
|
+
// src/kernel/runtime/createArkKernel.ts
|
|
2178
|
+
var kernelSequence = 0;
|
|
2179
|
+
function nextKernelInstanceId() {
|
|
2180
|
+
kernelSequence += 1;
|
|
2181
|
+
return `ark-kernel-${Date.now()}-${kernelSequence}`;
|
|
2182
|
+
}
|
|
2183
|
+
function createArkKernel(options = {}) {
|
|
2184
|
+
const strict = options.strict ?? true;
|
|
2185
|
+
const instanceId = options.instanceId ?? nextKernelInstanceId();
|
|
2186
|
+
const profile = options.profile ?? elevenLayerProfile;
|
|
2187
|
+
const registry = createIntentRegistry();
|
|
2188
|
+
const graph = createDependencyGraph();
|
|
2189
|
+
const metadata = options.metadata ?? createMetadataRegistry();
|
|
2190
|
+
const auditTrail = options.auditTrail ?? createAuditTrail({ maxRecords: options.maxHistorySize });
|
|
2191
|
+
const eventContracts = options.eventContracts ?? createEventContractRegistry();
|
|
2192
|
+
const outbox = options.outbox ?? new InMemoryOutboxStore();
|
|
2193
|
+
const projections = options.projections ?? createProjectionRegistry({ auditTrail });
|
|
2194
|
+
const policyEngine = new PolicyEngine([
|
|
2195
|
+
defineArchitectureProfilePolicy(profile),
|
|
2196
|
+
...options.policies ?? []
|
|
2197
|
+
]);
|
|
2198
|
+
const syncGraph = () => {
|
|
2199
|
+
syncRegistryToGraph(registry, graph, { requireRegisteredTargets: true });
|
|
2200
|
+
};
|
|
2201
|
+
const eventBus = createEventBus({
|
|
2202
|
+
intentRegistry: registry,
|
|
2203
|
+
dependencyGraph: graph,
|
|
2204
|
+
policyEngine,
|
|
2205
|
+
strictRegistry: true,
|
|
2206
|
+
validateIntentNaming: true,
|
|
2207
|
+
auditTrail,
|
|
2208
|
+
eventContracts,
|
|
2209
|
+
strictEventContracts: options.strictEventContracts ?? strict,
|
|
2210
|
+
requireKnownSource: options.requireKnownSource ?? true,
|
|
2211
|
+
architectureProfile: profile,
|
|
2212
|
+
enforceObservedLayerFlow: options.enforceObservedLayerFlow ?? (strict ? "hard" : "off"),
|
|
2213
|
+
outbox,
|
|
2214
|
+
instanceId,
|
|
2215
|
+
maxHistorySize: options.maxHistorySize,
|
|
2216
|
+
onPublish: options.autoApplyProjections === false ? void 0 : async (event) => {
|
|
2217
|
+
await projections.apply(event);
|
|
2218
|
+
}
|
|
2219
|
+
});
|
|
2220
|
+
const workflowEngine = createWorkflowEngine(eventBus, { auditTrail });
|
|
2221
|
+
const observability = createObservabilityReporter({
|
|
2222
|
+
registry,
|
|
2223
|
+
eventBus,
|
|
2224
|
+
graph
|
|
2225
|
+
});
|
|
2226
|
+
return {
|
|
2227
|
+
instanceId,
|
|
2228
|
+
profile,
|
|
2229
|
+
registry,
|
|
2230
|
+
graph,
|
|
2231
|
+
metadata,
|
|
2232
|
+
auditTrail,
|
|
2233
|
+
eventContracts,
|
|
2234
|
+
outbox,
|
|
2235
|
+
projections,
|
|
2236
|
+
policyEngine,
|
|
2237
|
+
eventBus,
|
|
2238
|
+
workflowEngine,
|
|
2239
|
+
observability,
|
|
2240
|
+
publisher(source) {
|
|
2241
|
+
return eventBus.createPublisher(source);
|
|
2242
|
+
},
|
|
2243
|
+
syncGraph,
|
|
2244
|
+
manifest() {
|
|
2245
|
+
syncGraph();
|
|
2246
|
+
return createArkManifest({
|
|
2247
|
+
registry,
|
|
2248
|
+
policyEngine,
|
|
2249
|
+
metadata,
|
|
2250
|
+
graph,
|
|
2251
|
+
profile,
|
|
2252
|
+
projections,
|
|
2253
|
+
eventContracts,
|
|
2254
|
+
observability
|
|
2255
|
+
});
|
|
2256
|
+
}
|
|
2257
|
+
};
|
|
2258
|
+
}
|
|
2259
|
+
|
|
2260
|
+
// src/nestjs/index.ts
|
|
2261
|
+
var ARK_KERNEL = /* @__PURE__ */ Symbol("ARK_KERNEL");
|
|
2262
|
+
var InjectArk = () => Inject(ARK_KERNEL);
|
|
2263
|
+
var ArkModule = class {
|
|
2264
|
+
/**
|
|
2265
|
+
* Registers a global Ark kernel. Pass an existing kernel to share one across
|
|
2266
|
+
* processes/tests, or options to create a fresh strict kernel.
|
|
2267
|
+
*/
|
|
2268
|
+
static forRoot(kernelOrOptions) {
|
|
2269
|
+
const kernel = kernelOrOptions && "registry" in kernelOrOptions ? kernelOrOptions : createArkKernel(kernelOrOptions);
|
|
2270
|
+
return {
|
|
2271
|
+
module: ArkModule,
|
|
2272
|
+
global: true,
|
|
2273
|
+
providers: [{ provide: ARK_KERNEL, useValue: kernel }],
|
|
2274
|
+
exports: [ARK_KERNEL]
|
|
2275
|
+
};
|
|
2276
|
+
}
|
|
2277
|
+
static forRootAsync(options) {
|
|
2278
|
+
return {
|
|
2279
|
+
module: ArkModule,
|
|
2280
|
+
global: true,
|
|
2281
|
+
providers: [
|
|
2282
|
+
{
|
|
2283
|
+
provide: ARK_KERNEL,
|
|
2284
|
+
useFactory: options.useFactory,
|
|
2285
|
+
inject: options.inject ?? []
|
|
2286
|
+
}
|
|
2287
|
+
],
|
|
2288
|
+
exports: [ARK_KERNEL]
|
|
2289
|
+
};
|
|
2290
|
+
}
|
|
2291
|
+
};
|
|
2292
|
+
ArkModule = __decorateClass([
|
|
2293
|
+
Module({})
|
|
2294
|
+
], ArkModule);
|
|
2295
|
+
|
|
2296
|
+
export { ARK_KERNEL, ArkModule, InjectArk };
|
|
2297
|
+
//# sourceMappingURL=index.js.map
|
|
2298
|
+
//# sourceMappingURL=index.js.map
|