ark-runtime-kernel 1.0.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/dist/index.cjs ADDED
@@ -0,0 +1,2853 @@
1
+ 'use strict';
2
+
3
+ // src/version.ts
4
+ var version = "1.0.0";
5
+
6
+ // src/kernel/intent/IntentRegistry.ts
7
+ var IntentRegistry = class {
8
+ intents = /* @__PURE__ */ new Map();
9
+ dependencies = /* @__PURE__ */ new Map();
10
+ productions = /* @__PURE__ */ new Map();
11
+ /**
12
+ * Define/register a new intent.
13
+ *
14
+ * @param name - Semantic intent name following the convention (Domain.*, Application.*, etc.)
15
+ * @param options - Optional relationship declarations
16
+ * @throws Error if an intent with the same name is already registered
17
+ */
18
+ define(name, options) {
19
+ if (this.intents.has(name)) {
20
+ throw new Error(
21
+ `Intent "${name}" is already registered. Intent names must be unique within a registry.`
22
+ );
23
+ }
24
+ const fn = (payload) => ({
25
+ intent: name,
26
+ payload,
27
+ metadata: {
28
+ occurredAt: (/* @__PURE__ */ new Date()).toISOString(),
29
+ source: "unknown"
30
+ }
31
+ });
32
+ Object.defineProperty(fn, "name", {
33
+ value: name,
34
+ enumerable: true,
35
+ configurable: false,
36
+ writable: false
37
+ });
38
+ const creator = fn;
39
+ this.intents.set(name, creator);
40
+ if (options?.dependsOn) {
41
+ for (const dep of options.dependsOn) {
42
+ this.declareDependency(name, dep);
43
+ }
44
+ }
45
+ if (options?.produces) {
46
+ for (const prod of options.produces) {
47
+ this.declareProduction(name, prod);
48
+ }
49
+ }
50
+ return creator;
51
+ }
52
+ /**
53
+ * Declare that one intent depends on / relates to another.
54
+ * This information is used by the DependencyGraph (future iterations) and for policy checks.
55
+ *
56
+ * @param from - The source intent (e.g. an Application operation)
57
+ * @param to - The target intent it depends on (e.g. a Domain event or concept)
58
+ */
59
+ declareDependency(from, to) {
60
+ if (!this.dependencies.has(from)) {
61
+ this.dependencies.set(from, /* @__PURE__ */ new Set());
62
+ }
63
+ this.dependencies.get(from).add(to);
64
+ }
65
+ /**
66
+ * Declare that one intent produces / emits another (e.g. use case → domain event).
67
+ */
68
+ declareProduction(from, to) {
69
+ if (!this.productions.has(from)) {
70
+ this.productions.set(from, /* @__PURE__ */ new Set());
71
+ }
72
+ this.productions.get(from).add(to);
73
+ }
74
+ /**
75
+ * Retrieve a previously defined intent creator by name.
76
+ */
77
+ get(name) {
78
+ return this.intents.get(name);
79
+ }
80
+ /**
81
+ * List all registered intent creators.
82
+ */
83
+ list() {
84
+ return Array.from(this.intents.values());
85
+ }
86
+ /**
87
+ * Get all declared dependencies for a given intent.
88
+ */
89
+ getDependencies(intentName) {
90
+ const deps = this.dependencies.get(intentName);
91
+ return deps ? Array.from(deps) : [];
92
+ }
93
+ /**
94
+ * Get all intents produced / emitted by a given intent.
95
+ */
96
+ getProductions(intentName) {
97
+ const prods = this.productions.get(intentName);
98
+ return prods ? Array.from(prods) : [];
99
+ }
100
+ /**
101
+ * Get all declared relationships (useful for graph generation).
102
+ */
103
+ getAllRelationships() {
104
+ const result = [];
105
+ for (const [from, tos] of this.dependencies.entries()) {
106
+ for (const to of tos) {
107
+ result.push({ from, to, kind: "dependsOn" });
108
+ }
109
+ }
110
+ for (const [from, tos] of this.productions.entries()) {
111
+ for (const to of tos) {
112
+ result.push({ from, to, kind: "produces" });
113
+ }
114
+ }
115
+ return result;
116
+ }
117
+ /**
118
+ * Check if an intent name has been registered.
119
+ */
120
+ has(name) {
121
+ return this.intents.has(name);
122
+ }
123
+ /**
124
+ * Clear the registry. Primarily useful for tests.
125
+ */
126
+ clear() {
127
+ this.intents.clear();
128
+ this.dependencies.clear();
129
+ this.productions.clear();
130
+ }
131
+ };
132
+
133
+ // src/kernel/intent/defineIntent.ts
134
+ var defaultRegistry = new IntentRegistry();
135
+ function defineIntent(name, options) {
136
+ return defaultRegistry.define(name, options);
137
+ }
138
+ function createIntentRegistry() {
139
+ return new IntentRegistry();
140
+ }
141
+ var defaultIntentRegistry = defaultRegistry;
142
+
143
+ // src/kernel/intent/validateIntentName.ts
144
+ var ALLOWED_PREFIXES = [
145
+ "Domain.",
146
+ "Application.",
147
+ "Adapter.",
148
+ "Workflow.",
149
+ "Job.",
150
+ "Presentation.",
151
+ "Reporting.",
152
+ "Metadata.",
153
+ "Security.",
154
+ "Audit.",
155
+ "Observability.",
156
+ "Kernel."
157
+ ];
158
+ function validateIntentName(name) {
159
+ if (!name || typeof name !== "string") {
160
+ return { valid: false, reason: "Intent name must be a non-empty string" };
161
+ }
162
+ if (!ALLOWED_PREFIXES.some((p) => name.startsWith(p))) {
163
+ return {
164
+ valid: false,
165
+ reason: `Intent "${name}" must start with one of: ${ALLOWED_PREFIXES.join(", ")}`
166
+ };
167
+ }
168
+ const rest = name.slice(name.indexOf(".") + 1);
169
+ if (!rest || !/^[A-Za-z][A-Za-z0-9_.]*$/.test(rest)) {
170
+ return {
171
+ valid: false,
172
+ reason: `Intent "${name}" has an invalid segment after the layer prefix`
173
+ };
174
+ }
175
+ return { valid: true };
176
+ }
177
+
178
+ // src/kernel/policy/PolicyViolationError.ts
179
+ var PolicyViolationError = class extends Error {
180
+ violations;
181
+ constructor(violations) {
182
+ const messages = violations.map((v) => `- ${v.policyName}: ${v.message}`).join("\n");
183
+ super(`Hard policy violation(s) detected:
184
+ ${messages}`);
185
+ this.name = "PolicyViolationError";
186
+ this.violations = violations;
187
+ }
188
+ };
189
+
190
+ // src/kernel/policy/PolicyEngine.ts
191
+ var PolicyEngine = class {
192
+ policies = [];
193
+ constructor(initialPolicies = []) {
194
+ for (const policy of initialPolicies) {
195
+ this.add(policy);
196
+ }
197
+ }
198
+ /**
199
+ * Adds a policy to the engine.
200
+ */
201
+ add(policy) {
202
+ if (this.policies.some((p) => p.name === policy.name)) {
203
+ throw new Error(`Policy "${policy.name}" is already registered in this engine.`);
204
+ }
205
+ this.policies.push(policy);
206
+ }
207
+ /**
208
+ * Returns all registered policies.
209
+ */
210
+ getPolicies() {
211
+ return [...this.policies];
212
+ }
213
+ /**
214
+ * Evaluates all policies against the provided context.
215
+ */
216
+ evaluate(context) {
217
+ const violations = [];
218
+ for (const policy of this.policies) {
219
+ const result = policy.check(context);
220
+ if (result === true) {
221
+ continue;
222
+ }
223
+ if (result === false) {
224
+ violations.push({
225
+ policyName: policy.name,
226
+ severity: policy.severity,
227
+ message: `Policy "${policy.name}" was violated.`
228
+ });
229
+ continue;
230
+ }
231
+ if (Array.isArray(result)) {
232
+ for (const v of result) {
233
+ violations.push({
234
+ policyName: policy.name,
235
+ severity: policy.severity,
236
+ message: v.message,
237
+ details: v.details
238
+ });
239
+ }
240
+ } else {
241
+ violations.push({
242
+ policyName: policy.name,
243
+ severity: policy.severity,
244
+ message: result.message,
245
+ details: result.details
246
+ });
247
+ }
248
+ }
249
+ const hardViolations = violations.filter((v) => v.severity === "hard");
250
+ const softViolations = violations.filter((v) => v.severity === "soft");
251
+ return {
252
+ passed: violations.length === 0,
253
+ violations,
254
+ hardViolations,
255
+ softViolations
256
+ };
257
+ }
258
+ /**
259
+ * Enforces all policies.
260
+ *
261
+ * - Soft violations are collected and can be observed (returned or logged).
262
+ * - Hard violations cause an error to be thrown (by default).
263
+ *
264
+ * @returns Evaluation result (including any soft violations)
265
+ * @throws Error if any hard policy is violated
266
+ */
267
+ enforce(context) {
268
+ const result = this.evaluate(context);
269
+ if (result.hardViolations.length > 0) {
270
+ throw new PolicyViolationError(result.hardViolations);
271
+ }
272
+ return result;
273
+ }
274
+ /**
275
+ * Clears all registered policies.
276
+ */
277
+ clear() {
278
+ this.policies.length = 0;
279
+ }
280
+ };
281
+
282
+ // src/kernel/policy/definePolicy.ts
283
+ function definePolicy(options) {
284
+ const severity = options.severity ?? "soft";
285
+ const policy = {
286
+ name: options.name,
287
+ severity,
288
+ tags: options.tags,
289
+ owner: options.owner,
290
+ version: options.version,
291
+ rationale: options.rationale,
292
+ enforcementMode: options.enforcementMode,
293
+ deprecated: options.deprecated,
294
+ replacedBy: options.replacedBy,
295
+ check: options.check
296
+ };
297
+ return policy;
298
+ }
299
+
300
+ // src/kernel/policy/builtins.ts
301
+ function defaultLayerOf(name) {
302
+ const dot = name.indexOf(".");
303
+ return dot >= 0 ? name.slice(0, dot) : name;
304
+ }
305
+ function resolveLayer(name, options) {
306
+ return options.resolveLayer?.(name) ?? defaultLayerOf(name);
307
+ }
308
+ function matchesRule(fromLayer, toLayer, rule) {
309
+ return fromLayer === rule.from && toLayer === rule.to;
310
+ }
311
+ function defineLayerPolicy(options) {
312
+ const name = options.name ?? "Layer isolation";
313
+ const severity = options.severity ?? "hard";
314
+ return definePolicy({
315
+ name,
316
+ severity,
317
+ tags: ["layer"],
318
+ check: (ctx) => {
319
+ const edges = [
320
+ ...(ctx.edges ?? []).filter((e) => e.kind === "declared" || e.kind === "produces").map((e) => ({ from: e.from, to: e.to })),
321
+ ...(ctx.relationships ?? []).filter((r) => r.kind === "dependsOn").map((r) => ({ from: r.from, to: r.to }))
322
+ ];
323
+ const violations = [];
324
+ for (const edge of edges) {
325
+ const fromLayer = resolveLayer(edge.from, options);
326
+ const toLayer = resolveLayer(edge.to, options);
327
+ for (const rule of options.rules) {
328
+ if (!rule.allowed && matchesRule(fromLayer, toLayer, rule)) {
329
+ violations.push({
330
+ policyName: name,
331
+ severity,
332
+ message: rule.message ?? `Layer violation: ${edge.from} (${fromLayer}) must not relate to ${edge.to} (${toLayer})`
333
+ });
334
+ }
335
+ }
336
+ }
337
+ return violations.length > 0 ? violations : true;
338
+ }
339
+ });
340
+ }
341
+ function isLayerPolicy(policy) {
342
+ return policy.tags?.includes("layer") ?? false;
343
+ }
344
+ var architecturalPolicies = {
345
+ /**
346
+ * @deprecated Use cleanArchitectureMatrix() for full layer rules.
347
+ * Blocks Domain → Adapter declared dependencies only.
348
+ */
349
+ layerIsolation() {
350
+ return architecturalPolicies.cleanArchitectureMatrix();
351
+ },
352
+ /**
353
+ * Clean-architecture dependency matrix (declared dependsOn / declared edges only).
354
+ * Does not block observed event flows (Domain events consumed by Application).
355
+ */
356
+ cleanArchitectureMatrix() {
357
+ return defineLayerPolicy({
358
+ name: "Clean architecture matrix",
359
+ severity: "hard",
360
+ rules: [
361
+ { from: "Domain", to: "Adapter", allowed: false },
362
+ { from: "Domain", to: "Application", allowed: false },
363
+ { from: "Adapter", to: "Application", allowed: false },
364
+ { from: "Adapter", to: "Domain", allowed: false }
365
+ ]
366
+ });
367
+ }
368
+ };
369
+ function defineArchitectureProfilePolicy(profile, options = {}) {
370
+ return defineLayerPolicy({
371
+ name: options.name ?? `${profile.name} layer policy`,
372
+ severity: options.severity ?? "hard",
373
+ rules: profile.rules,
374
+ resolveLayer: profile.resolveLayer
375
+ });
376
+ }
377
+
378
+ // src/kernel/event-bus/policyContext.ts
379
+ function buildPublishPolicyContext(options) {
380
+ return (event) => ({
381
+ event,
382
+ relationships: options.intentRegistry?.getAllRelationships(),
383
+ edges: options.dependencyGraph?.getEdges()
384
+ });
385
+ }
386
+ function definePublishPolicy(options) {
387
+ return definePolicy(options);
388
+ }
389
+
390
+ // src/kernel/event-bus/errors.ts
391
+ var UnregisteredIntentError = class extends Error {
392
+ intentName;
393
+ constructor(intentName) {
394
+ super(
395
+ `Intent "${intentName}" is not registered. Register it with IntentRegistry.define() before publish/subscribe, including metadata.source producer intents in strict mode.`
396
+ );
397
+ this.name = "UnregisteredIntentError";
398
+ this.intentName = intentName;
399
+ }
400
+ };
401
+ var InvalidIntentNameError = class extends Error {
402
+ intentName;
403
+ reason;
404
+ constructor(intentName, reason) {
405
+ super(`Invalid intent name "${intentName}": ${reason}`);
406
+ this.name = "InvalidIntentNameError";
407
+ this.intentName = intentName;
408
+ this.reason = reason;
409
+ }
410
+ };
411
+ var LayerPolicyContextError = class extends Error {
412
+ constructor() {
413
+ super(
414
+ "Layer/architecture policies require intentRegistry, dependencyGraph, or a custom getPolicyContext. Without graph/registry context, layer policies cannot inspect relationships."
415
+ );
416
+ this.name = "LayerPolicyContextError";
417
+ }
418
+ };
419
+ var EventContractViolationError = class extends Error {
420
+ intentName;
421
+ issues;
422
+ constructor(intentName, issues) {
423
+ super(
424
+ `Event contract violation for "${intentName}". Register a matching event contract/version or fix the payload before publishing.`
425
+ );
426
+ this.name = "EventContractViolationError";
427
+ this.intentName = intentName;
428
+ this.issues = issues;
429
+ }
430
+ };
431
+ var UnknownEventSourceError = class extends Error {
432
+ intentName;
433
+ source;
434
+ constructor(intentName, source) {
435
+ super(
436
+ 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.`
437
+ );
438
+ this.name = "UnknownEventSourceError";
439
+ this.intentName = intentName;
440
+ this.source = source;
441
+ }
442
+ };
443
+ var SourceMetadataOverrideError = class extends Error {
444
+ boundSource;
445
+ attemptedSource;
446
+ constructor(boundSource, attemptedSource) {
447
+ super(
448
+ `Source-bound publisher for "${boundSource}" cannot publish with metadata.source "${attemptedSource}". Create a publisher for the intended source instead.`
449
+ );
450
+ this.name = "SourceMetadataOverrideError";
451
+ this.boundSource = boundSource;
452
+ this.attemptedSource = attemptedSource;
453
+ }
454
+ };
455
+ var ObservedLayerFlowViolationError = class extends Error {
456
+ source;
457
+ intentName;
458
+ fromLayer;
459
+ toLayer;
460
+ constructor(source, intentName, fromLayer, toLayer, message) {
461
+ super(
462
+ message ?? `Observed layer violation: "${source}" (${fromLayer}) must not produce "${intentName}" (${toLayer}). Route this through an allowed layer or adjust the architecture profile rule.`
463
+ );
464
+ this.name = "ObservedLayerFlowViolationError";
465
+ this.source = source;
466
+ this.intentName = intentName;
467
+ this.fromLayer = fromLayer;
468
+ this.toLayer = toLayer;
469
+ }
470
+ };
471
+
472
+ // src/kernel/event-bus/EventBus.ts
473
+ var interceptorSequence = 0;
474
+ function nextInterceptorRegistrationId() {
475
+ interceptorSequence += 1;
476
+ return `interceptor-${Date.now()}-${interceptorSequence}`;
477
+ }
478
+ function isPlainRecord(value) {
479
+ if (typeof value !== "object" || value === null || Array.isArray(value)) {
480
+ return false;
481
+ }
482
+ const proto = Object.getPrototypeOf(value);
483
+ return proto === Object.prototype || proto === null;
484
+ }
485
+ function clonePatchValue(value) {
486
+ if (Array.isArray(value)) return value.map(clonePatchValue);
487
+ if (isPlainRecord(value)) {
488
+ return Object.fromEntries(
489
+ Object.entries(value).map(([key, child]) => [key, clonePatchValue(child)])
490
+ );
491
+ }
492
+ return value;
493
+ }
494
+ function mergeRecordPatch(target, patch, path = "payload") {
495
+ const next = { ...target };
496
+ for (const [key, value] of Object.entries(patch)) {
497
+ const childPath = `${path}.${key}`;
498
+ if (!(key in next) || next[key] === void 0) {
499
+ next[key] = clonePatchValue(value);
500
+ continue;
501
+ }
502
+ if (isPlainRecord(next[key]) && isPlainRecord(value)) {
503
+ next[key] = mergeRecordPatch(next[key], value, childPath);
504
+ continue;
505
+ }
506
+ if (Array.isArray(next[key]) && Array.isArray(value)) {
507
+ next[key] = mergeArrayPatch(next[key], value, childPath);
508
+ continue;
509
+ }
510
+ throw new Error(`Interceptor patch cannot overwrite existing ${childPath}.`);
511
+ }
512
+ return next;
513
+ }
514
+ function mergeArrayPatch(target, patch, path = "payload") {
515
+ const next = [...target];
516
+ patch.forEach((value, index) => {
517
+ const childPath = `${path}[${index}]`;
518
+ if (index >= next.length || next[index] === void 0) {
519
+ next[index] = clonePatchValue(value);
520
+ return;
521
+ }
522
+ if (isPlainRecord(next[index]) && isPlainRecord(value)) {
523
+ next[index] = mergeRecordPatch(next[index], value, childPath);
524
+ return;
525
+ }
526
+ if (Array.isArray(next[index]) && Array.isArray(value)) {
527
+ next[index] = mergeArrayPatch(next[index], value, childPath);
528
+ return;
529
+ }
530
+ throw new Error(`Interceptor patch cannot overwrite existing ${childPath}.`);
531
+ });
532
+ return next;
533
+ }
534
+ function applyPayloadPatch(payload, patch) {
535
+ if (Array.isArray(patch)) {
536
+ if (payload === void 0) return clonePatchValue(patch);
537
+ if (!Array.isArray(payload)) {
538
+ throw new Error("Array interceptor patch requires an array payload.");
539
+ }
540
+ return mergeArrayPatch(payload, patch);
541
+ }
542
+ if (payload === void 0) return clonePatchValue(patch);
543
+ if (!isPlainRecord(payload)) {
544
+ throw new Error("Object interceptor patch requires an object payload.");
545
+ }
546
+ return mergeRecordPatch(payload, patch);
547
+ }
548
+ var EventBusImpl = class {
549
+ subscriptions = [];
550
+ subscriptionsByIntent = /* @__PURE__ */ new Map();
551
+ interceptors = [];
552
+ interceptorsByIntent = /* @__PURE__ */ new Map();
553
+ history = [];
554
+ trace = [];
555
+ onPublish;
556
+ onSoftViolation;
557
+ onHandlerError;
558
+ auditTrail;
559
+ eventContracts;
560
+ strictEventContracts;
561
+ requireKnownSource;
562
+ architectureProfile;
563
+ enforceObservedLayerFlowMode;
564
+ outbox;
565
+ instanceId;
566
+ traceSinks;
567
+ rethrowHandlerErrors;
568
+ policyEngine;
569
+ getPolicyContext;
570
+ maxHistorySize;
571
+ intentRegistry;
572
+ dependencyGraph;
573
+ strictRegistry;
574
+ validateIntentNaming;
575
+ constructor(options = {}) {
576
+ this.onPublish = options.onPublish;
577
+ this.onSoftViolation = options.onSoftViolation;
578
+ this.onHandlerError = options.onHandlerError;
579
+ this.auditTrail = options.auditTrail;
580
+ this.eventContracts = options.eventContracts;
581
+ this.strictEventContracts = options.strictEventContracts ?? false;
582
+ this.requireKnownSource = options.requireKnownSource ?? false;
583
+ this.architectureProfile = options.architectureProfile;
584
+ this.enforceObservedLayerFlowMode = options.enforceObservedLayerFlow ?? "off";
585
+ this.outbox = options.outbox;
586
+ this.instanceId = options.instanceId;
587
+ this.traceSinks = [...options.traceSinks ?? []];
588
+ this.rethrowHandlerErrors = options.rethrowHandlerErrors ?? false;
589
+ this.maxHistorySize = options.maxHistorySize;
590
+ this.intentRegistry = options.intentRegistry;
591
+ this.dependencyGraph = options.dependencyGraph;
592
+ this.strictRegistry = options.strictRegistry ?? options.intentRegistry !== void 0;
593
+ this.validateIntentNaming = options.validateIntentNaming ?? this.strictRegistry;
594
+ if (options.policyEngine) {
595
+ this.policyEngine = options.policyEngine;
596
+ } else if (options.policies && options.policies.length > 0) {
597
+ this.policyEngine = new PolicyEngine(options.policies);
598
+ }
599
+ const allPolicies = this.policyEngine?.getPolicies() ?? options.policies ?? [];
600
+ if (allPolicies.some(isLayerPolicy) && !options.intentRegistry && !options.dependencyGraph && !options.getPolicyContext) {
601
+ throw new LayerPolicyContextError();
602
+ }
603
+ if (options.getPolicyContext) {
604
+ this.getPolicyContext = options.getPolicyContext;
605
+ } else if (options.intentRegistry || options.dependencyGraph) {
606
+ this.getPolicyContext = buildPublishPolicyContext({
607
+ intentRegistry: options.intentRegistry,
608
+ dependencyGraph: options.dependencyGraph
609
+ });
610
+ } else {
611
+ this.getPolicyContext = (event) => ({ event });
612
+ }
613
+ }
614
+ async publish(eventOrCreator, payloadOrMeta, metadata) {
615
+ let event;
616
+ const rawPublish = typeof eventOrCreator !== "function";
617
+ if (!rawPublish) {
618
+ const creator = eventOrCreator;
619
+ const payload = payloadOrMeta;
620
+ const extraMeta = metadata ?? {};
621
+ const created = creator(payload);
622
+ event = {
623
+ ...created,
624
+ metadata: this.enrichMetadata(created.metadata, extraMeta)
625
+ };
626
+ } else {
627
+ const rawEvent = eventOrCreator;
628
+ const extraMeta = metadata ?? payloadOrMeta ?? {};
629
+ event = {
630
+ ...rawEvent,
631
+ metadata: this.enrichMetadata(rawEvent.metadata, extraMeta)
632
+ };
633
+ }
634
+ if (rawPublish && this.strictRegistry) {
635
+ await this.recordRawPublishDiagnostic(event);
636
+ }
637
+ this.assertIntentAllowed(event.intent);
638
+ this.assertSourceAllowed(event);
639
+ this.assertContractAllowed(event);
640
+ event = await this.applyInterceptors(event);
641
+ this.assertContractAllowed(event);
642
+ await this.assertObservedLayerFlowAllowed(event);
643
+ this.dependencyGraph?.registerEventFlow(event.metadata.source, event.intent);
644
+ const matching = [...this.subscriptionsByIntent.get(event.intent) ?? []];
645
+ if (this.policyEngine) {
646
+ const ctx = this.getPolicyContext(event);
647
+ let policyResult;
648
+ try {
649
+ policyResult = this.policyEngine.enforce(ctx);
650
+ } catch (err) {
651
+ if (err instanceof PolicyViolationError) {
652
+ this.appendTrace({
653
+ type: "policy.hardViolation",
654
+ timestamp: (/* @__PURE__ */ new Date()).toISOString(),
655
+ intent: event.intent,
656
+ correlationId: event.metadata.correlationId,
657
+ traceId: event.metadata.traceId,
658
+ spanId: event.metadata.spanId,
659
+ details: { violations: err.violations }
660
+ });
661
+ await this.recordAudit("policy.hardViolation", event, {
662
+ violations: err.violations
663
+ });
664
+ }
665
+ throw err;
666
+ }
667
+ if (policyResult.softViolations.length > 0) {
668
+ this.appendTrace({
669
+ type: "policy.softViolation",
670
+ timestamp: (/* @__PURE__ */ new Date()).toISOString(),
671
+ intent: event.intent,
672
+ correlationId: event.metadata.correlationId,
673
+ traceId: event.metadata.traceId,
674
+ spanId: event.metadata.spanId,
675
+ details: { violations: policyResult.softViolations }
676
+ });
677
+ await this.recordAudit("policy.softViolation", event, {
678
+ violations: policyResult.softViolations
679
+ });
680
+ if (this.onSoftViolation) {
681
+ await this.safeHook(
682
+ () => this.onSoftViolation(policyResult, event),
683
+ "onSoftViolation",
684
+ event
685
+ );
686
+ }
687
+ }
688
+ }
689
+ const record = {
690
+ event,
691
+ publishedAt: (/* @__PURE__ */ new Date()).toISOString(),
692
+ subscribersNotified: matching.length
693
+ };
694
+ this.appendHistory(record);
695
+ await this.outbox?.enqueue(event);
696
+ this.appendTrace({
697
+ type: "event.published",
698
+ timestamp: record.publishedAt,
699
+ intent: event.intent,
700
+ correlationId: event.metadata.correlationId,
701
+ traceId: event.metadata.traceId,
702
+ spanId: event.metadata.spanId,
703
+ details: { subscribersNotified: matching.length }
704
+ });
705
+ await this.recordAudit("event.published", event, {
706
+ subscribersNotified: matching.length
707
+ });
708
+ const notifications = matching.map(
709
+ (sub) => this.invokeHandler(sub, event)
710
+ );
711
+ await Promise.all(notifications);
712
+ if (this.onPublish) {
713
+ await this.safeHook(
714
+ () => this.onPublish(event),
715
+ "onPublish",
716
+ event
717
+ );
718
+ }
719
+ }
720
+ createPublisher(source) {
721
+ const sourceName = typeof source === "string" ? source : source.name;
722
+ this.assertIntentAllowed(sourceName);
723
+ return {
724
+ source: sourceName,
725
+ publish: async (intent, payload, metadata = {}) => {
726
+ if (metadata.source && metadata.source !== sourceName) {
727
+ throw new SourceMetadataOverrideError(sourceName, metadata.source);
728
+ }
729
+ await this.publish(intent, payload, {
730
+ ...metadata,
731
+ source: sourceName
732
+ });
733
+ }
734
+ };
735
+ }
736
+ subscribe(intent, handler) {
737
+ const intentName = typeof intent === "string" ? intent : intent.name;
738
+ this.assertIntentAllowed(intentName);
739
+ const sub = {
740
+ intentName,
741
+ handler
742
+ };
743
+ this.subscriptions.push(sub);
744
+ const subscriptionsForIntent = this.subscriptionsByIntent.get(intentName) ?? [];
745
+ subscriptionsForIntent.push(sub);
746
+ this.subscriptionsByIntent.set(intentName, subscriptionsForIntent);
747
+ return () => {
748
+ const idx = this.subscriptions.indexOf(sub);
749
+ if (idx >= 0) this.subscriptions.splice(idx, 1);
750
+ const byIntent = this.subscriptionsByIntent.get(intentName);
751
+ if (!byIntent) return;
752
+ const intentIdx = byIntent.indexOf(sub);
753
+ if (intentIdx >= 0) byIntent.splice(intentIdx, 1);
754
+ if (byIntent.length === 0) this.subscriptionsByIntent.delete(intentName);
755
+ };
756
+ }
757
+ registerInterceptor(intent, interceptor, interceptorId) {
758
+ const intentName = typeof intent === "string" ? intent : intent.name;
759
+ this.assertIntentAllowed(intentName);
760
+ const registration = {
761
+ registrationId: nextInterceptorRegistrationId(),
762
+ interceptorId: interceptorId ?? intentName,
763
+ intentName,
764
+ interceptor,
765
+ createdAt: (/* @__PURE__ */ new Date()).toISOString()
766
+ };
767
+ this.interceptors.push(registration);
768
+ const interceptorsForIntent = this.interceptorsByIntent.get(intentName) ?? [];
769
+ interceptorsForIntent.push(registration);
770
+ this.interceptorsByIntent.set(intentName, interceptorsForIntent);
771
+ return registration.registrationId;
772
+ }
773
+ unregisterInterceptor(registrationId) {
774
+ const interceptor = this.interceptors.find(
775
+ (candidate) => candidate.registrationId === registrationId
776
+ );
777
+ if (!interceptor) return false;
778
+ const idx = this.interceptors.indexOf(interceptor);
779
+ if (idx >= 0) this.interceptors.splice(idx, 1);
780
+ const byIntent = this.interceptorsByIntent.get(interceptor.intentName);
781
+ if (byIntent) {
782
+ const intentIdx = byIntent.indexOf(interceptor);
783
+ if (intentIdx >= 0) byIntent.splice(intentIdx, 1);
784
+ if (byIntent.length === 0) this.interceptorsByIntent.delete(interceptor.intentName);
785
+ }
786
+ return true;
787
+ }
788
+ listInterceptors(intent) {
789
+ return this.interceptors.filter((interceptor) => !intent || interceptor.intentName === intent).map((interceptor) => ({
790
+ registrationId: interceptor.registrationId,
791
+ interceptorId: interceptor.interceptorId,
792
+ intent: interceptor.intentName,
793
+ createdAt: interceptor.createdAt,
794
+ lastInterceptedAt: interceptor.lastInterceptedAt
795
+ }));
796
+ }
797
+ getHistory() {
798
+ return [...this.history];
799
+ }
800
+ clearHistory() {
801
+ this.history.length = 0;
802
+ }
803
+ getTrace() {
804
+ return [...this.trace];
805
+ }
806
+ clearTrace() {
807
+ this.trace.length = 0;
808
+ }
809
+ assertIntentAllowed(intentName) {
810
+ if (!this.strictRegistry && !this.validateIntentNaming) {
811
+ return;
812
+ }
813
+ if (this.validateIntentNaming) {
814
+ const validation = validateIntentName(intentName);
815
+ if (!validation.valid) {
816
+ throw new InvalidIntentNameError(intentName, validation.reason);
817
+ }
818
+ }
819
+ if (this.strictRegistry && this.intentRegistry && !this.intentRegistry.has(intentName)) {
820
+ throw new UnregisteredIntentError(intentName);
821
+ }
822
+ }
823
+ assertSourceAllowed(event) {
824
+ if (!this.requireKnownSource) return;
825
+ if (!event.metadata.source || event.metadata.source === "unknown") {
826
+ throw new UnknownEventSourceError(event.intent);
827
+ }
828
+ if (this.intentRegistry && !this.intentRegistry.has(event.metadata.source)) {
829
+ throw new UnknownEventSourceError(event.intent, event.metadata.source);
830
+ }
831
+ }
832
+ assertContractAllowed(event) {
833
+ if (!this.eventContracts) return;
834
+ const result = this.eventContracts.validate(event);
835
+ if (!result.ok && (this.strictEventContracts || result.contract)) {
836
+ throw new EventContractViolationError(event.intent, result.issues);
837
+ }
838
+ }
839
+ /**
840
+ * Enforce the OBSERVED producer→event flow (metadata.source → intent) against the
841
+ * architecture profile's layer rules. This is the runtime counterpart to the
842
+ * declared-model layer policy: it checks what the system actually did, resolving both
843
+ * layers directly from the profile. It runs BEFORE the flow is recorded via
844
+ * registerEventFlow, so in hard mode a rejected flow leaves no edge in the graph.
845
+ */
846
+ async assertObservedLayerFlowAllowed(event) {
847
+ if (this.enforceObservedLayerFlowMode === "off" || !this.architectureProfile) {
848
+ return;
849
+ }
850
+ const source = event.metadata.source;
851
+ if (!source || source === "unknown") return;
852
+ const profile = this.architectureProfile;
853
+ const fromLayer = profile.resolveLayer(source);
854
+ const toLayer = profile.resolveLayer(event.intent);
855
+ if (!fromLayer || !toLayer) return;
856
+ const blocked = profile.rules.find(
857
+ (rule) => !rule.allowed && rule.from === fromLayer && rule.to === toLayer
858
+ );
859
+ if (!blocked) return;
860
+ const severity = this.enforceObservedLayerFlowMode;
861
+ const message = blocked.message ?? `Observed layer violation: "${source}" (${fromLayer}) must not produce "${event.intent}" (${toLayer}).`;
862
+ const details = {
863
+ source,
864
+ intent: event.intent,
865
+ fromLayer,
866
+ toLayer,
867
+ severity,
868
+ message,
869
+ rule: blocked
870
+ };
871
+ this.appendTrace({
872
+ type: "layer.observedViolation",
873
+ timestamp: (/* @__PURE__ */ new Date()).toISOString(),
874
+ intent: event.intent,
875
+ correlationId: event.metadata.correlationId,
876
+ traceId: event.metadata.traceId,
877
+ spanId: event.metadata.spanId,
878
+ details
879
+ });
880
+ await this.recordAudit("layer.observedViolation", event, details);
881
+ if (severity === "hard") {
882
+ throw new ObservedLayerFlowViolationError(source, event.intent, fromLayer, toLayer, message);
883
+ }
884
+ }
885
+ async recordRawPublishDiagnostic(event) {
886
+ const details = {
887
+ intent: event.intent,
888
+ source: event.metadata.source,
889
+ suggestion: "Publish through a registered intent creator so strict registry, contracts, and agent tooling share one source of truth."
890
+ };
891
+ this.appendTrace({
892
+ type: "event.rawPublish",
893
+ timestamp: (/* @__PURE__ */ new Date()).toISOString(),
894
+ intent: event.intent,
895
+ correlationId: event.metadata.correlationId,
896
+ traceId: event.metadata.traceId,
897
+ spanId: event.metadata.spanId,
898
+ details
899
+ });
900
+ await this.recordAudit("event.rawPublish", event, details);
901
+ }
902
+ async applyInterceptors(event) {
903
+ if (event.metadata.allowInterception === false) {
904
+ return event;
905
+ }
906
+ const matching = [...this.interceptorsByIntent.get(event.intent) ?? []];
907
+ let current = event;
908
+ for (const registration of matching) {
909
+ const patches = [];
910
+ try {
911
+ await Promise.resolve(
912
+ registration.interceptor({
913
+ event: current,
914
+ intercept: (patch) => {
915
+ patches.push(patch);
916
+ }
917
+ })
918
+ );
919
+ if (patches.length === 0) {
920
+ continue;
921
+ }
922
+ const timestamp = (/* @__PURE__ */ new Date()).toISOString();
923
+ let candidate = {
924
+ ...current,
925
+ metadata: {
926
+ ...current.metadata,
927
+ interceptions: [
928
+ ...current.metadata.interceptions ?? [],
929
+ { interceptorId: registration.interceptorId, timestamp }
930
+ ]
931
+ }
932
+ };
933
+ for (const patch of patches) {
934
+ candidate = {
935
+ ...candidate,
936
+ payload: applyPayloadPatch(candidate.payload, patch),
937
+ metadata: { ...candidate.metadata }
938
+ };
939
+ }
940
+ this.assertContractAllowed(candidate);
941
+ current = candidate;
942
+ registration.lastInterceptedAt = timestamp;
943
+ this.appendTrace({
944
+ type: "event.intercepted",
945
+ timestamp,
946
+ intent: current.intent,
947
+ correlationId: current.metadata.correlationId,
948
+ traceId: current.metadata.traceId,
949
+ spanId: current.metadata.spanId,
950
+ details: {
951
+ registrationId: registration.registrationId,
952
+ interceptorId: registration.interceptorId,
953
+ patchesApplied: patches.length
954
+ }
955
+ });
956
+ await this.recordAudit("event.intercepted", current, {
957
+ registrationId: registration.registrationId,
958
+ interceptorId: registration.interceptorId,
959
+ patchesApplied: patches.length
960
+ });
961
+ } catch (err) {
962
+ await this.recordInterceptorError(registration, current, err);
963
+ }
964
+ }
965
+ return current;
966
+ }
967
+ async invokeHandler(sub, event) {
968
+ try {
969
+ await Promise.resolve(sub.handler(event));
970
+ } catch (err) {
971
+ this.appendTrace({
972
+ type: "handler.error",
973
+ timestamp: (/* @__PURE__ */ new Date()).toISOString(),
974
+ intent: event.intent,
975
+ correlationId: event.metadata.correlationId,
976
+ traceId: event.metadata.traceId,
977
+ spanId: event.metadata.spanId,
978
+ details: { error: err instanceof Error ? err.message : String(err) }
979
+ });
980
+ await this.recordAudit("handler.error", event, {
981
+ handlerIntent: sub.intentName,
982
+ error: err instanceof Error ? err.message : String(err)
983
+ });
984
+ if (this.onHandlerError) {
985
+ await this.safeHook(
986
+ () => this.onHandlerError(err, event, sub.intentName),
987
+ "onHandlerError",
988
+ event
989
+ );
990
+ }
991
+ if (this.rethrowHandlerErrors) {
992
+ throw err;
993
+ }
994
+ }
995
+ }
996
+ async safeHook(fn, hookName, event) {
997
+ try {
998
+ await Promise.resolve(fn());
999
+ } catch (err) {
1000
+ this.appendTrace({
1001
+ type: "hook.error",
1002
+ timestamp: (/* @__PURE__ */ new Date()).toISOString(),
1003
+ intent: event.intent,
1004
+ correlationId: event.metadata.correlationId,
1005
+ traceId: event.metadata.traceId,
1006
+ spanId: event.metadata.spanId,
1007
+ details: {
1008
+ hook: hookName,
1009
+ error: err instanceof Error ? err.message : String(err)
1010
+ }
1011
+ });
1012
+ await this.recordAudit("hook.error", event, {
1013
+ hook: hookName,
1014
+ error: err instanceof Error ? err.message : String(err)
1015
+ });
1016
+ }
1017
+ }
1018
+ async recordInterceptorError(interceptor, event, error) {
1019
+ const message = error instanceof Error ? error.message : String(error);
1020
+ this.appendTrace({
1021
+ type: "interceptor.error",
1022
+ timestamp: (/* @__PURE__ */ new Date()).toISOString(),
1023
+ intent: event.intent,
1024
+ correlationId: event.metadata.correlationId,
1025
+ traceId: event.metadata.traceId,
1026
+ spanId: event.metadata.spanId,
1027
+ details: {
1028
+ registrationId: interceptor.registrationId,
1029
+ interceptorId: interceptor.interceptorId,
1030
+ error: message
1031
+ }
1032
+ });
1033
+ await this.recordAudit("interceptor.error", event, {
1034
+ registrationId: interceptor.registrationId,
1035
+ interceptorId: interceptor.interceptorId,
1036
+ error: message
1037
+ });
1038
+ }
1039
+ appendHistory(record) {
1040
+ this.history.push(record);
1041
+ if (this.maxHistorySize !== void 0 && this.history.length > this.maxHistorySize) {
1042
+ this.history.splice(0, this.history.length - this.maxHistorySize);
1043
+ }
1044
+ }
1045
+ appendTrace(record) {
1046
+ this.trace.push(record);
1047
+ if (this.maxHistorySize !== void 0 && this.trace.length > this.maxHistorySize) {
1048
+ this.trace.splice(0, this.trace.length - this.maxHistorySize);
1049
+ }
1050
+ for (const sink of this.traceSinks) {
1051
+ try {
1052
+ sink(record);
1053
+ } catch {
1054
+ }
1055
+ }
1056
+ }
1057
+ async recordAudit(type, event, details) {
1058
+ if (!this.auditTrail) return;
1059
+ try {
1060
+ await this.auditTrail.record({
1061
+ type,
1062
+ source: event.metadata.source,
1063
+ intent: event.intent,
1064
+ correlationId: event.metadata.correlationId,
1065
+ causationId: event.metadata.causationId,
1066
+ subject: event.intent,
1067
+ details
1068
+ });
1069
+ } catch (err) {
1070
+ this.appendTrace({
1071
+ type: "hook.error",
1072
+ timestamp: (/* @__PURE__ */ new Date()).toISOString(),
1073
+ intent: event.intent,
1074
+ correlationId: event.metadata.correlationId,
1075
+ traceId: event.metadata.traceId,
1076
+ spanId: event.metadata.spanId,
1077
+ details: {
1078
+ hook: "auditTrail",
1079
+ error: err instanceof Error ? err.message : String(err)
1080
+ }
1081
+ });
1082
+ }
1083
+ }
1084
+ enrichMetadata(base, extra) {
1085
+ return {
1086
+ ...base,
1087
+ ...extra,
1088
+ occurredAt: extra.occurredAt || base.occurredAt || (/* @__PURE__ */ new Date()).toISOString(),
1089
+ source: extra.source || base.source || "unknown",
1090
+ kernelInstanceId: extra.kernelInstanceId ?? base.kernelInstanceId ?? this.instanceId,
1091
+ eventVersion: extra.eventVersion ?? base.eventVersion,
1092
+ schemaVersion: extra.schemaVersion ?? base.schemaVersion,
1093
+ allowInterception: extra.allowInterception ?? base.allowInterception,
1094
+ interceptions: extra.interceptions ?? base.interceptions,
1095
+ correlationId: extra.correlationId ?? base.correlationId,
1096
+ causationId: extra.causationId ?? base.causationId,
1097
+ traceId: extra.traceId ?? base.traceId,
1098
+ spanId: extra.spanId ?? base.spanId,
1099
+ parentSpanId: extra.parentSpanId ?? base.parentSpanId
1100
+ };
1101
+ }
1102
+ };
1103
+ function createEventBus(options) {
1104
+ return new EventBusImpl(options);
1105
+ }
1106
+
1107
+ // src/kernel/event-contracts/EventContractRegistry.ts
1108
+ function actualType(value) {
1109
+ if (Array.isArray(value)) return "array";
1110
+ if (value === null) return "object";
1111
+ const type = typeof value;
1112
+ if (type === "string" || type === "number" || type === "boolean" || type === "object") {
1113
+ return type;
1114
+ }
1115
+ return "unknown";
1116
+ }
1117
+ function formatPath(parent, field) {
1118
+ return parent ? `${parent}.${field}` : field;
1119
+ }
1120
+ function validateSchemaField(value, fieldSchema, path, contract, issues) {
1121
+ const valueType = actualType(value);
1122
+ if (fieldSchema.type !== "unknown" && valueType !== fieldSchema.type) {
1123
+ issues.push({
1124
+ intent: contract.intent,
1125
+ version: contract.version,
1126
+ field: path,
1127
+ message: `Expected ${fieldSchema.type}, received ${valueType}.`
1128
+ });
1129
+ return;
1130
+ }
1131
+ if (fieldSchema.enum && !fieldSchema.enum.some((allowed) => Object.is(allowed, value))) {
1132
+ issues.push({
1133
+ intent: contract.intent,
1134
+ version: contract.version,
1135
+ field: path,
1136
+ message: "Value is not allowed by event contract enum."
1137
+ });
1138
+ }
1139
+ if (fieldSchema.type === "object" && fieldSchema.fields) {
1140
+ if (value === null || typeof value !== "object" || Array.isArray(value)) {
1141
+ issues.push({
1142
+ intent: contract.intent,
1143
+ version: contract.version,
1144
+ field: path,
1145
+ message: "Nested object field must be a non-array object."
1146
+ });
1147
+ return;
1148
+ }
1149
+ validateObjectSchema(
1150
+ value,
1151
+ fieldSchema.fields,
1152
+ contract,
1153
+ issues,
1154
+ path
1155
+ );
1156
+ }
1157
+ if (fieldSchema.type === "array" && fieldSchema.items && Array.isArray(value)) {
1158
+ value.forEach((item, index) => {
1159
+ validateSchemaField(
1160
+ item,
1161
+ fieldSchema.items,
1162
+ `${path}[${index}]`,
1163
+ contract,
1164
+ issues
1165
+ );
1166
+ });
1167
+ }
1168
+ }
1169
+ function validateObjectSchema(payload, schema, contract, issues, parentPath) {
1170
+ for (const [field, fieldSchema] of Object.entries(schema)) {
1171
+ const path = formatPath(parentPath, field);
1172
+ const value = payload[field];
1173
+ if (value === void 0) {
1174
+ if (fieldSchema.required) {
1175
+ issues.push({
1176
+ intent: contract.intent,
1177
+ version: contract.version,
1178
+ field: path,
1179
+ message: "Required field is missing."
1180
+ });
1181
+ }
1182
+ continue;
1183
+ }
1184
+ validateSchemaField(value, fieldSchema, path, contract, issues);
1185
+ }
1186
+ }
1187
+ var EventContractRegistryImpl = class {
1188
+ contracts = /* @__PURE__ */ new Map();
1189
+ register(contract) {
1190
+ const key = this.key(contract.intent, contract.version);
1191
+ if (this.contracts.has(key)) {
1192
+ throw new Error(`Event contract "${key}" is already registered.`);
1193
+ }
1194
+ this.contracts.set(key, { allowAdditionalFields: true, ...contract });
1195
+ }
1196
+ get(intent, version2) {
1197
+ if (version2) return this.contracts.get(this.key(intent, version2));
1198
+ return this.list(intent).at(-1);
1199
+ }
1200
+ list(intent) {
1201
+ return Array.from(this.contracts.values()).filter(
1202
+ (contract) => !intent || contract.intent === intent
1203
+ );
1204
+ }
1205
+ validate(event) {
1206
+ const version2 = event.metadata.eventVersion;
1207
+ const contract = this.get(event.intent, version2);
1208
+ const issues = [];
1209
+ if (!contract) {
1210
+ return {
1211
+ ok: false,
1212
+ issues: [
1213
+ {
1214
+ intent: event.intent,
1215
+ version: version2,
1216
+ message: version2 ? `No event contract registered for version "${version2}".` : "No event contract registered for intent."
1217
+ }
1218
+ ]
1219
+ };
1220
+ }
1221
+ if (contract.deprecated) {
1222
+ issues.push({
1223
+ intent: event.intent,
1224
+ version: contract.version,
1225
+ message: typeof contract.deprecated === "string" ? contract.deprecated : "Event contract is deprecated."
1226
+ });
1227
+ }
1228
+ const payload = event.payload != null && typeof event.payload === "object" ? event.payload : void 0;
1229
+ if (contract.schema) {
1230
+ if (!payload) {
1231
+ issues.push({
1232
+ intent: event.intent,
1233
+ version: contract.version,
1234
+ message: "Payload must be an object for schema validation."
1235
+ });
1236
+ } else {
1237
+ validateObjectSchema(payload, contract.schema, contract, issues);
1238
+ if (contract.allowAdditionalFields === false) {
1239
+ for (const field of Object.keys(payload)) {
1240
+ if (!contract.schema[field]) {
1241
+ issues.push({
1242
+ intent: event.intent,
1243
+ version: contract.version,
1244
+ field,
1245
+ message: "Additional field is not allowed by event contract."
1246
+ });
1247
+ }
1248
+ }
1249
+ }
1250
+ }
1251
+ }
1252
+ return { ok: issues.length === 0, contract, issues };
1253
+ }
1254
+ clear() {
1255
+ this.contracts.clear();
1256
+ }
1257
+ key(intent, version2) {
1258
+ return `${intent}@${version2}`;
1259
+ }
1260
+ };
1261
+ function createEventContractRegistry() {
1262
+ return new EventContractRegistryImpl();
1263
+ }
1264
+
1265
+ // src/kernel/outbox/InMemoryOutboxStore.ts
1266
+ var outboxSequence = 0;
1267
+ function nextOutboxId() {
1268
+ outboxSequence += 1;
1269
+ return `outbox-${Date.now()}-${outboxSequence}`;
1270
+ }
1271
+ function cloneRecord(record) {
1272
+ return {
1273
+ ...record,
1274
+ event: {
1275
+ ...record.event,
1276
+ metadata: { ...record.event.metadata }
1277
+ }
1278
+ };
1279
+ }
1280
+ var InMemoryOutboxStore = class {
1281
+ records = /* @__PURE__ */ new Map();
1282
+ async enqueue(event) {
1283
+ const now = (/* @__PURE__ */ new Date()).toISOString();
1284
+ const record = {
1285
+ id: nextOutboxId(),
1286
+ event,
1287
+ status: "pending",
1288
+ attempts: 0,
1289
+ createdAt: now,
1290
+ updatedAt: now
1291
+ };
1292
+ this.records.set(record.id, record);
1293
+ return cloneRecord(record);
1294
+ }
1295
+ async markDispatched(id) {
1296
+ const record = this.records.get(id);
1297
+ if (!record) return;
1298
+ record.status = "dispatched";
1299
+ record.updatedAt = (/* @__PURE__ */ new Date()).toISOString();
1300
+ }
1301
+ async markFailed(id, error) {
1302
+ const record = this.records.get(id);
1303
+ if (!record) return;
1304
+ record.status = "failed";
1305
+ record.attempts += 1;
1306
+ record.error = error instanceof Error ? error.message : String(error);
1307
+ record.updatedAt = (/* @__PURE__ */ new Date()).toISOString();
1308
+ }
1309
+ async list(status) {
1310
+ return Array.from(this.records.values()).filter((record) => !status || record.status === status).map(cloneRecord);
1311
+ }
1312
+ async clear() {
1313
+ this.records.clear();
1314
+ }
1315
+ };
1316
+
1317
+ // src/kernel/observability/ObservabilityReporter.ts
1318
+ function flowKey(flow) {
1319
+ return `${flow.from}->${flow.to}`;
1320
+ }
1321
+ function uniqueFlows(flows) {
1322
+ const seen = /* @__PURE__ */ new Set();
1323
+ const result = [];
1324
+ for (const flow of flows) {
1325
+ const key = flowKey(flow);
1326
+ if (seen.has(key)) continue;
1327
+ seen.add(key);
1328
+ result.push(flow);
1329
+ }
1330
+ return result;
1331
+ }
1332
+ function difference(left, right) {
1333
+ const rightKeys = new Set(right.map(flowKey));
1334
+ return left.filter((flow) => !rightKeys.has(flowKey(flow)));
1335
+ }
1336
+ function createObservabilityReporter(options) {
1337
+ const registry = options.registry;
1338
+ const eventBus = options.eventBus;
1339
+ const graph = options.graph;
1340
+ return {
1341
+ report() {
1342
+ const declaredFromRegistry = registry ? registry.getAllRelationships().filter((relationship) => relationship.kind === "produces").map((relationship) => ({
1343
+ from: relationship.from,
1344
+ to: relationship.to
1345
+ })) : [];
1346
+ const declaredFromGraph = declaredFromRegistry.length > 0 || !graph ? [] : graph.getEdges().filter((edge) => edge.kind === "produces").map((edge) => ({ from: edge.from, to: edge.to }));
1347
+ const observedFromHistory = eventBus?.getHistory().map((record) => ({
1348
+ from: record.event.metadata.source,
1349
+ to: record.event.intent
1350
+ })) ?? [];
1351
+ const observedFromGraph = observedFromHistory.length > 0 || !graph ? [] : graph.getEdges().filter((edge) => edge.kind === "observed").map((edge) => ({ from: edge.from, to: edge.to }));
1352
+ const declaredProductions = uniqueFlows([
1353
+ ...declaredFromRegistry,
1354
+ ...declaredFromGraph
1355
+ ]);
1356
+ const observedProductions = uniqueFlows([
1357
+ ...observedFromHistory,
1358
+ ...observedFromGraph
1359
+ ]);
1360
+ const unknownSources = observedProductions.filter(
1361
+ (flow) => !flow.from || flow.from === "unknown"
1362
+ );
1363
+ const unregisteredObservedSources = registry ? Array.from(
1364
+ new Set(
1365
+ observedProductions.map((flow) => flow.from).filter(
1366
+ (source) => source && source !== "unknown" && !registry.has(source)
1367
+ )
1368
+ )
1369
+ ) : [];
1370
+ const unregisteredObservedIntents = registry ? Array.from(
1371
+ new Set(
1372
+ observedProductions.map((flow) => flow.to).filter((intent) => !registry.has(intent))
1373
+ )
1374
+ ) : [];
1375
+ const observedIntentNames = new Set(
1376
+ observedProductions.flatMap((flow) => [flow.from, flow.to])
1377
+ );
1378
+ const registeredButNeverObserved = registry ? registry.list().map((intent) => intent.name).filter((intent) => !observedIntentNames.has(intent)) : [];
1379
+ return {
1380
+ generatedAt: (/* @__PURE__ */ new Date()).toISOString(),
1381
+ declaredProductions,
1382
+ observedProductions,
1383
+ declaredButUnobserved: difference(declaredProductions, observedProductions),
1384
+ observedButUndeclared: difference(
1385
+ observedProductions.filter((flow) => flow.from !== "unknown"),
1386
+ declaredProductions
1387
+ ),
1388
+ unknownSources,
1389
+ unregisteredObservedSources,
1390
+ unregisteredObservedIntents,
1391
+ registeredButNeverObserved
1392
+ };
1393
+ }
1394
+ };
1395
+ }
1396
+
1397
+ // src/kernel/testing/ArkTestHarness.ts
1398
+ function createArkTestHarness(kernel) {
1399
+ return {
1400
+ events(intent) {
1401
+ return kernel.eventBus.getHistory().map((record) => record.event).filter((event) => !intent || event.intent === intent);
1402
+ },
1403
+ traces(type) {
1404
+ return kernel.eventBus.getTrace().filter((record) => !type || record.type === type);
1405
+ },
1406
+ audit(query) {
1407
+ return kernel.auditTrail.query(query);
1408
+ },
1409
+ outbox(status) {
1410
+ return kernel.outbox.list(status);
1411
+ },
1412
+ observability() {
1413
+ kernel.syncGraph();
1414
+ return kernel.observability.report();
1415
+ },
1416
+ async snapshot() {
1417
+ return {
1418
+ events: this.events(),
1419
+ traces: this.traces(),
1420
+ audit: await this.audit(),
1421
+ outbox: await this.outbox(),
1422
+ observability: this.observability()
1423
+ };
1424
+ },
1425
+ async clear() {
1426
+ kernel.eventBus.clearHistory();
1427
+ kernel.eventBus.clearTrace();
1428
+ await kernel.auditTrail.clear();
1429
+ await kernel.outbox.clear();
1430
+ }
1431
+ };
1432
+ }
1433
+
1434
+ // src/kernel/audit/AuditTrail.ts
1435
+ var auditSequence = 0;
1436
+ function createAuditId() {
1437
+ auditSequence += 1;
1438
+ return `audit-${Date.now()}-${auditSequence}`;
1439
+ }
1440
+ function matchesQuery(record, query) {
1441
+ if (query.type && record.type !== query.type) return false;
1442
+ if (query.intent && record.intent !== query.intent) return false;
1443
+ if (query.correlationId && record.correlationId !== query.correlationId) return false;
1444
+ if (query.subject && record.subject !== query.subject) return false;
1445
+ if (query.since && record.timestamp < query.since) return false;
1446
+ if (query.until && record.timestamp > query.until) return false;
1447
+ return true;
1448
+ }
1449
+ var InMemoryAuditStore = class {
1450
+ constructor(maxRecords) {
1451
+ this.maxRecords = maxRecords;
1452
+ }
1453
+ maxRecords;
1454
+ records = [];
1455
+ append(record) {
1456
+ this.records.push(record);
1457
+ if (this.maxRecords !== void 0 && this.records.length > this.maxRecords) {
1458
+ this.records.splice(0, this.records.length - this.maxRecords);
1459
+ }
1460
+ }
1461
+ query(query = {}) {
1462
+ const records = this.records.filter((record) => matchesQuery(record, query));
1463
+ return query.limit === void 0 ? [...records] : records.slice(-query.limit);
1464
+ }
1465
+ clear() {
1466
+ this.records.length = 0;
1467
+ }
1468
+ };
1469
+ var AuditTrailImpl = class {
1470
+ constructor(store) {
1471
+ this.store = store;
1472
+ }
1473
+ store;
1474
+ async record(input) {
1475
+ const record = {
1476
+ id: createAuditId(),
1477
+ timestamp: input.timestamp ?? (/* @__PURE__ */ new Date()).toISOString(),
1478
+ type: input.type,
1479
+ source: input.source,
1480
+ actor: input.actor,
1481
+ intent: input.intent,
1482
+ correlationId: input.correlationId,
1483
+ causationId: input.causationId,
1484
+ subject: input.subject,
1485
+ details: input.details
1486
+ };
1487
+ await this.store.append(record);
1488
+ return record;
1489
+ }
1490
+ async query(query) {
1491
+ return this.store.query(query);
1492
+ }
1493
+ async clear() {
1494
+ await this.store.clear();
1495
+ }
1496
+ };
1497
+ function createAuditTrail(options = {}) {
1498
+ return new AuditTrailImpl(
1499
+ options.store ?? new InMemoryAuditStore(options.maxRecords)
1500
+ );
1501
+ }
1502
+
1503
+ // src/kernel/graph/DependencyGraph.ts
1504
+ var DependencyGraphImpl = class {
1505
+ nodes = /* @__PURE__ */ new Map();
1506
+ edges = [];
1507
+ registerDependency(from, to, kind = "declared") {
1508
+ this.ensureNode(from);
1509
+ this.ensureNode(to);
1510
+ this.addEdge({ from, to, kind });
1511
+ }
1512
+ registerEventFlow(producer, consumer) {
1513
+ this.ensureNode(producer);
1514
+ this.ensureNode(consumer);
1515
+ this.addEdge({ from: producer, to: consumer, kind: "observed" });
1516
+ }
1517
+ getNodes() {
1518
+ return Array.from(this.nodes.values());
1519
+ }
1520
+ getEdges() {
1521
+ return [...this.edges];
1522
+ }
1523
+ toJSON() {
1524
+ return {
1525
+ nodes: this.getNodes(),
1526
+ edges: this.getEdges()
1527
+ };
1528
+ }
1529
+ toMermaid() {
1530
+ let out = "flowchart TD\n";
1531
+ for (const edge of this.edges) {
1532
+ const label = edge.kind ? `|${edge.kind}|` : "";
1533
+ out += ` ${this.safeId(edge.from)} -->${label} ${this.safeId(edge.to)}
1534
+ `;
1535
+ }
1536
+ const connected = new Set(
1537
+ this.edges.flatMap((e) => [e.from, e.to])
1538
+ );
1539
+ for (const node of this.nodes.keys()) {
1540
+ if (!connected.has(node)) {
1541
+ out += ` ${this.safeId(node)}
1542
+ `;
1543
+ }
1544
+ }
1545
+ return out.trim();
1546
+ }
1547
+ toLayerMermaid(profile) {
1548
+ const nodesByLayer = /* @__PURE__ */ new Map();
1549
+ for (const node of this.nodes.keys()) {
1550
+ const layer = profile.resolveLayer(node) ?? "Unclassified";
1551
+ const current = nodesByLayer.get(layer) ?? [];
1552
+ current.push(node);
1553
+ nodesByLayer.set(layer, current);
1554
+ }
1555
+ let out = "flowchart TD\n";
1556
+ for (const layer of profile.layers) {
1557
+ const nodes = nodesByLayer.get(layer.name) ?? [];
1558
+ if (nodes.length === 0) continue;
1559
+ out += ` subgraph ${this.safeId(layer.name)}[${layer.name}]
1560
+ `;
1561
+ for (const node of nodes) {
1562
+ out += ` ${this.safeId(node)}[${node}]
1563
+ `;
1564
+ }
1565
+ out += " end\n";
1566
+ }
1567
+ const unclassified = nodesByLayer.get("Unclassified") ?? [];
1568
+ if (unclassified.length > 0) {
1569
+ out += " subgraph Unclassified[Unclassified]\n";
1570
+ for (const node of unclassified) {
1571
+ out += ` ${this.safeId(node)}[${node}]
1572
+ `;
1573
+ }
1574
+ out += " end\n";
1575
+ }
1576
+ for (const edge of this.edges) {
1577
+ const label = edge.kind ? `|${edge.kind}|` : "";
1578
+ out += ` ${this.safeId(edge.from)} -->${label} ${this.safeId(edge.to)}
1579
+ `;
1580
+ }
1581
+ return out.trim();
1582
+ }
1583
+ detectViolations(rules = []) {
1584
+ const violations = [];
1585
+ const cycles = this.findSimpleCycles();
1586
+ if (cycles.length > 0) {
1587
+ violations.push(`Cycle detected: ${cycles.map((c) => c.join("->")).join(", ")}`);
1588
+ }
1589
+ for (const rule of rules) {
1590
+ try {
1591
+ const res = rule(this.edges);
1592
+ if (res && res.length) violations.push(...res);
1593
+ } catch (e) {
1594
+ violations.push(`Rule error: ${e.message}`);
1595
+ }
1596
+ }
1597
+ return violations;
1598
+ }
1599
+ ensureNode(id) {
1600
+ if (!this.nodes.has(id)) {
1601
+ this.nodes.set(id, { id });
1602
+ }
1603
+ }
1604
+ addEdge(edge) {
1605
+ const exists = this.edges.some(
1606
+ (e) => e.from === edge.from && e.to === edge.to && e.kind === edge.kind
1607
+ );
1608
+ if (!exists) {
1609
+ this.edges.push(edge);
1610
+ }
1611
+ }
1612
+ // Very naive cycle detection for small graphs
1613
+ findSimpleCycles() {
1614
+ const graph = /* @__PURE__ */ new Map();
1615
+ for (const e of this.edges) {
1616
+ if (!graph.has(e.from)) graph.set(e.from, []);
1617
+ graph.get(e.from).push(e.to);
1618
+ }
1619
+ const cycles = [];
1620
+ const visited = /* @__PURE__ */ new Set();
1621
+ const stack = [];
1622
+ const dfs = (node) => {
1623
+ visited.add(node);
1624
+ stack.push(node);
1625
+ for (const nei of graph.get(node) || []) {
1626
+ if (!visited.has(nei)) {
1627
+ dfs(nei);
1628
+ } else if (stack.includes(nei)) {
1629
+ const cycleStart = stack.indexOf(nei);
1630
+ const cycle = stack.slice(cycleStart).concat(nei);
1631
+ cycles.push(cycle);
1632
+ }
1633
+ }
1634
+ stack.pop();
1635
+ };
1636
+ for (const node of graph.keys()) {
1637
+ if (!visited.has(node)) dfs(node);
1638
+ }
1639
+ const unique = new Set(cycles.map((c) => c.join("->")));
1640
+ return Array.from(unique).map((s) => s.split("->"));
1641
+ }
1642
+ safeId(id) {
1643
+ return id.replace(/[^a-zA-Z0-9_]/g, "_");
1644
+ }
1645
+ };
1646
+ function createDependencyGraph() {
1647
+ return new DependencyGraphImpl();
1648
+ }
1649
+
1650
+ // src/kernel/graph/sync.ts
1651
+ function syncRegistryToGraph(registry, graph, options = {}) {
1652
+ const requireRegistered = options.requireRegisteredTargets ?? false;
1653
+ for (const rel of registry.getAllRelationships()) {
1654
+ if (requireRegistered && !registry.has(rel.to)) {
1655
+ continue;
1656
+ }
1657
+ if (rel.kind === "dependsOn") {
1658
+ graph.registerDependency(rel.from, rel.to, "declared");
1659
+ } else {
1660
+ graph.registerDependency(rel.from, rel.to, "produces");
1661
+ }
1662
+ }
1663
+ }
1664
+
1665
+ // src/kernel/layers/ArchitectureProfile.ts
1666
+ function normalizePrefix(prefix) {
1667
+ return prefix.endsWith(".") ? prefix : `${prefix}.`;
1668
+ }
1669
+ function byLongestPrefix(a, b) {
1670
+ const maxA = Math.max(...a.prefixes.map((p) => p.length));
1671
+ const maxB = Math.max(...b.prefixes.map((p) => p.length));
1672
+ return maxB - maxA;
1673
+ }
1674
+ function flowKey2(from, to) {
1675
+ return `${from}->${to}`;
1676
+ }
1677
+ function createStrictDenyRules(layers, allowedFlows) {
1678
+ const allowed = new Set(allowedFlows.map((flow) => flowKey2(flow.from, flow.to)));
1679
+ const rules = [];
1680
+ for (const from of layers) {
1681
+ for (const to of layers) {
1682
+ if (from.name === to.name) continue;
1683
+ if (allowed.has(flowKey2(from.name, to.name))) continue;
1684
+ rules.push({ from: from.name, to: to.name, allowed: false });
1685
+ }
1686
+ }
1687
+ return rules;
1688
+ }
1689
+ function createArchitectureProfile(options) {
1690
+ const layers = options.layers.map((layer) => ({
1691
+ ...layer,
1692
+ prefixes: layer.prefixes.map(normalizePrefix)
1693
+ }));
1694
+ const sortedLayers = [...layers].sort(byLongestPrefix);
1695
+ const rules = [...options.rules ?? []];
1696
+ return {
1697
+ name: options.name,
1698
+ layers,
1699
+ rules,
1700
+ resolveLayer(name) {
1701
+ return sortedLayers.find(
1702
+ (layer) => layer.prefixes.some((prefix) => name.startsWith(prefix))
1703
+ )?.name;
1704
+ }
1705
+ };
1706
+ }
1707
+ var elevenLayerProfileLayers = [
1708
+ {
1709
+ name: "DomainModel",
1710
+ prefixes: ["Domain"],
1711
+ description: "Rich domain model, business rules, and domain events.",
1712
+ order: 1
1713
+ },
1714
+ {
1715
+ name: "ApplicationOrchestration",
1716
+ prefixes: ["Application"],
1717
+ description: "Use cases and command orchestration.",
1718
+ order: 2
1719
+ },
1720
+ {
1721
+ name: "PersistenceAdapters",
1722
+ prefixes: ["Adapter.Persistence", "Adapter.Repository"],
1723
+ description: "Database, repository, and storage adapters.",
1724
+ order: 3
1725
+ },
1726
+ {
1727
+ name: "IntegrationAdapters",
1728
+ prefixes: ["Adapter.Integration", "Adapter.External"],
1729
+ description: "External systems, APIs, and integration adapters.",
1730
+ order: 4
1731
+ },
1732
+ {
1733
+ name: "WorkflowSagaEngine",
1734
+ prefixes: ["Workflow"],
1735
+ description: "Sagas, workflows, and long-running processes.",
1736
+ order: 5
1737
+ },
1738
+ {
1739
+ name: "BackgroundJobsScheduling",
1740
+ prefixes: ["Job"],
1741
+ description: "Background jobs, scheduled work, and async processors.",
1742
+ order: 6
1743
+ },
1744
+ {
1745
+ name: "PresentationAdapters",
1746
+ prefixes: ["Presentation", "Adapter.Presentation", "Adapter.Api"],
1747
+ description: "API, UI, controller, and presentation adapters.",
1748
+ order: 7
1749
+ },
1750
+ {
1751
+ name: "ReportingReadModels",
1752
+ prefixes: ["Reporting"],
1753
+ description: "Read models, projections, and reporting surfaces.",
1754
+ order: 8
1755
+ },
1756
+ {
1757
+ name: "ExtensibilityMetadata",
1758
+ prefixes: ["Metadata"],
1759
+ description: "Metadata, extensions, and schema contracts.",
1760
+ order: 9
1761
+ },
1762
+ {
1763
+ name: "SecurityAuditObservability",
1764
+ prefixes: ["Security", "Audit", "Observability"],
1765
+ description: "Security, audit, and observability concerns.",
1766
+ order: 10
1767
+ },
1768
+ {
1769
+ name: "Kernel",
1770
+ prefixes: ["Kernel"],
1771
+ description: "Ark-owned governance and kernel signals.",
1772
+ order: 11
1773
+ }
1774
+ ];
1775
+ var elevenLayerAllowedFlows = [
1776
+ { from: "PresentationAdapters", to: "ApplicationOrchestration" },
1777
+ { from: "ApplicationOrchestration", to: "DomainModel" },
1778
+ { from: "WorkflowSagaEngine", to: "ApplicationOrchestration" },
1779
+ { from: "WorkflowSagaEngine", to: "DomainModel" },
1780
+ { from: "BackgroundJobsScheduling", to: "ApplicationOrchestration" }
1781
+ ];
1782
+ var elevenLayerProfile = createArchitectureProfile({
1783
+ name: "Ark 11-layer Hexagonal Event-Driven Profile",
1784
+ layers: elevenLayerProfileLayers,
1785
+ rules: createStrictDenyRules(
1786
+ elevenLayerProfileLayers,
1787
+ elevenLayerAllowedFlows
1788
+ )
1789
+ });
1790
+ var defaultElevenLayerDirectories = {
1791
+ DomainModel: ["domain"],
1792
+ ApplicationOrchestration: ["application", "app"],
1793
+ PersistenceAdapters: [
1794
+ "adapters/persistence",
1795
+ "adapters/repository",
1796
+ "repositories",
1797
+ "infra/persistence"
1798
+ ],
1799
+ IntegrationAdapters: ["adapters/integration", "adapters/external", "integrations"],
1800
+ WorkflowSagaEngine: ["workflows", "sagas"],
1801
+ BackgroundJobsScheduling: ["jobs", "schedules"],
1802
+ PresentationAdapters: ["presentation", "adapters/presentation", "adapters/api"],
1803
+ ReportingReadModels: ["reporting", "read-models", "projections"],
1804
+ ExtensibilityMetadata: ["metadata", "extensions"],
1805
+ SecurityAuditObservability: ["security", "audit", "observability"],
1806
+ Kernel: ["kernel"]
1807
+ };
1808
+ function createElevenLayerArkConfig(options = {}) {
1809
+ const rootDir = options.rootDir ?? "src";
1810
+ const optional = options.optionalLayers ?? true;
1811
+ return {
1812
+ include: options.include ?? [rootDir],
1813
+ layers: elevenLayerProfile.layers.map((layer) => ({
1814
+ name: layer.name,
1815
+ patterns: (defaultElevenLayerDirectories[layer.name] ?? [layer.name]).map(
1816
+ (directory) => `${rootDir}/${directory}/**`
1817
+ ),
1818
+ intentPrefixes: layer.prefixes,
1819
+ optional
1820
+ })),
1821
+ rules: [...elevenLayerProfile.rules]
1822
+ };
1823
+ }
1824
+
1825
+ // src/kernel/metadata/MetadataRegistry.ts
1826
+ var MetadataRegistryImpl = class {
1827
+ entities = /* @__PURE__ */ new Map();
1828
+ entity(name, meta, options = {}) {
1829
+ if (this.entities.has(name) && !options.allowOverwrite) {
1830
+ throw new Error(`Entity metadata "${name}" is already registered.`);
1831
+ }
1832
+ const full = { name, fields: {}, ...meta };
1833
+ this.entities.set(name, full);
1834
+ return full;
1835
+ }
1836
+ getEntity(name) {
1837
+ return this.entities.get(name);
1838
+ }
1839
+ listEntities() {
1840
+ return Array.from(this.entities.values());
1841
+ }
1842
+ findEntitiesByIntent(intentName) {
1843
+ return this.listEntities().filter(
1844
+ (entity) => entity.emits?.includes(intentName) || entity.consumes?.includes(intentName)
1845
+ );
1846
+ }
1847
+ validate() {
1848
+ const issues = [];
1849
+ for (const entity of this.entities.values()) {
1850
+ if (!entity.name.trim()) {
1851
+ issues.push({ entity: entity.name, message: "Entity name is required." });
1852
+ }
1853
+ for (const [fieldName, field] of Object.entries(entity.fields)) {
1854
+ if (!fieldName.trim()) {
1855
+ issues.push({ entity: entity.name, field: fieldName, message: "Field name is required." });
1856
+ }
1857
+ if (!field.type || typeof field.type !== "string") {
1858
+ issues.push({
1859
+ entity: entity.name,
1860
+ field: fieldName,
1861
+ message: "Field type must be a non-empty string."
1862
+ });
1863
+ }
1864
+ if (field.relation && !this.entities.has(field.relation.entity)) {
1865
+ issues.push({
1866
+ entity: entity.name,
1867
+ field: fieldName,
1868
+ message: `Related entity "${field.relation.entity}" is not registered.`
1869
+ });
1870
+ }
1871
+ }
1872
+ }
1873
+ return { ok: issues.length === 0, issues };
1874
+ }
1875
+ toJSON() {
1876
+ return this.listEntities();
1877
+ }
1878
+ };
1879
+ function createMetadataRegistry() {
1880
+ return new MetadataRegistryImpl();
1881
+ }
1882
+
1883
+ // src/kernel/adapters/ports.ts
1884
+ function definePort(name, options = {}) {
1885
+ return {
1886
+ name,
1887
+ ownerLayer: options.ownerLayer,
1888
+ intent: options.intent,
1889
+ allowedAdapters: options.allowedAdapters ? [...options.allowedAdapters] : void 0
1890
+ };
1891
+ }
1892
+ function createAdapter(port, impl, requiredKeysOrOptions, adapterOptions = {}) {
1893
+ const options = Array.isArray(requiredKeysOrOptions) ? { ...adapterOptions, requiredKeys: requiredKeysOrOptions } : { ...requiredKeysOrOptions ?? {} };
1894
+ const requiredKeys = options.requiredKeys;
1895
+ if (requiredKeys && requiredKeys.length > 0) {
1896
+ const missing = requiredKeys.filter((k) => !hasMember(impl, k));
1897
+ if (missing.length > 0) {
1898
+ throw new Error(
1899
+ `Adapter for port "${port.name}" is missing required members: ${missing.join(", ")}`
1900
+ );
1901
+ }
1902
+ }
1903
+ const adapter = {
1904
+ name: options.name,
1905
+ layer: options.layer,
1906
+ intent: options.intent,
1907
+ port,
1908
+ impl
1909
+ };
1910
+ const governance = checkAdapterGovernance(adapter);
1911
+ if (!governance.ok) {
1912
+ throw new Error(governance.issues.map((issue) => issue.message).join("\n"));
1913
+ }
1914
+ return adapter;
1915
+ }
1916
+ function checkContract(impl, requiredKeys = []) {
1917
+ const missing = requiredKeys.filter((k) => !hasMember(impl, k));
1918
+ return missing.length === 0 ? { ok: true } : { ok: false, missing };
1919
+ }
1920
+ function checkAdapterGovernance(adapter) {
1921
+ const allowed = adapter.port.allowedAdapters ?? [];
1922
+ if (allowed.length === 0) {
1923
+ return { ok: true, issues: [] };
1924
+ }
1925
+ const adapterNames = [adapter.name, adapter.intent].filter(
1926
+ (value) => typeof value === "string" && value.length > 0
1927
+ );
1928
+ const matched = adapterNames.some((name) => allowed.includes(name));
1929
+ if (matched) {
1930
+ return { ok: true, issues: [] };
1931
+ }
1932
+ return {
1933
+ ok: false,
1934
+ issues: [
1935
+ {
1936
+ ruleId: "ADAPTER_NOT_ALLOWED_FOR_PORT",
1937
+ port: adapter.port.name,
1938
+ adapter: adapter.name ?? adapter.intent,
1939
+ message: `Adapter "${adapter.name ?? adapter.intent ?? "unknown"}" is not allowed for port "${adapter.port.name}".`
1940
+ }
1941
+ ]
1942
+ };
1943
+ }
1944
+ function hasMember(value, key) {
1945
+ return value != null && (typeof value === "object" || typeof value === "function") && key in value;
1946
+ }
1947
+
1948
+ // src/kernel/ai-gate/AICodeGate.ts
1949
+ function violation(ruleId, message, extra) {
1950
+ return { ruleId, code: ruleId, message, ...extra };
1951
+ }
1952
+ function lineOf(source, index) {
1953
+ return source.slice(0, index).split("\n").length;
1954
+ }
1955
+ function extractQuotedStrings(source) {
1956
+ const matches = [];
1957
+ const re = /['"`]([A-Za-z][A-Za-z0-9_.]*)['"`]/g;
1958
+ let m;
1959
+ while ((m = re.exec(source)) !== null) {
1960
+ matches.push({ value: m[1], index: m.index });
1961
+ }
1962
+ return matches;
1963
+ }
1964
+ function extractModuleSpecifiers(source) {
1965
+ const matches = [];
1966
+ const patterns = [
1967
+ {
1968
+ kind: "import",
1969
+ re: /\bimport\s+(?:type\s+)?(?:[^'"]*?\s+from\s*)?['"]([^'"]+)['"]/g
1970
+ },
1971
+ {
1972
+ kind: "export",
1973
+ re: /\bexport\s+(?:type\s+)?[^'"]*?\s+from\s*['"]([^'"]+)['"]/g
1974
+ },
1975
+ {
1976
+ kind: "dynamic-import",
1977
+ re: /\bimport\s*\(\s*['"]([^'"]+)['"]\s*\)/g
1978
+ },
1979
+ {
1980
+ kind: "require",
1981
+ re: /\brequire\s*\(\s*['"]([^'"]+)['"]\s*\)/g
1982
+ }
1983
+ ];
1984
+ for (const pattern of patterns) {
1985
+ let match;
1986
+ while ((match = pattern.re.exec(source)) !== null) {
1987
+ const index = match.index + match[0].indexOf(match[1]);
1988
+ matches.push({ value: match[1], index, kind: pattern.kind });
1989
+ }
1990
+ }
1991
+ return matches.sort((a, b) => a.index - b.index);
1992
+ }
1993
+ function looksLikeIntentName(s) {
1994
+ return /^(Domain|Application|Adapter|Workflow|Job|Presentation|Reporting|Metadata|Security|Audit|Observability|Kernel)\.[A-Za-z0-9_.]+$/.test(s);
1995
+ }
1996
+ function hasInfrastructureToken(specifier) {
1997
+ const tokens = specifier.toLowerCase().split(/[^a-z0-9]+/).filter(Boolean);
1998
+ return [
1999
+ "adapter",
2000
+ "adapters",
2001
+ "infra",
2002
+ "infrastructure",
2003
+ "persistence",
2004
+ "repository",
2005
+ "repositories",
2006
+ "integration",
2007
+ "database",
2008
+ "db"
2009
+ ].some((token) => tokens.includes(token));
2010
+ }
2011
+ function isKnownInfrastructurePackage(specifier) {
2012
+ const normalized = specifier.toLowerCase();
2013
+ return ["sequelize", "prisma", "typeorm", "mongoose", "knex"].some(
2014
+ (name) => normalized === name || normalized.startsWith(`${name}/`)
2015
+ );
2016
+ }
2017
+ function tsStringLiteralText(ts, node) {
2018
+ return node && ts.isStringLiteralLike(node) ? node.text : void 0;
2019
+ }
2020
+ function tsPropertyName(ts, node) {
2021
+ if (!node) return void 0;
2022
+ if (ts.isIdentifier(node) || ts.isStringLiteralLike(node)) return node.text;
2023
+ return void 0;
2024
+ }
2025
+ function tsObjectProperty(ts, node, name) {
2026
+ if (!node || !ts.isObjectLiteralExpression(node)) return void 0;
2027
+ return node.properties.find((property) => {
2028
+ if (!ts.isPropertyAssignment(property) && !ts.isShorthandPropertyAssignment(property)) {
2029
+ return false;
2030
+ }
2031
+ return tsPropertyName(ts, property.name) === name;
2032
+ });
2033
+ }
2034
+ function tsObjectHasProperty(ts, node, name) {
2035
+ return tsObjectProperty(ts, node, name) !== void 0;
2036
+ }
2037
+ function tsObjectPropertyValue(ts, node, name) {
2038
+ const property = tsObjectProperty(ts, node, name);
2039
+ return property && ts.isPropertyAssignment(property) ? property.initializer : void 0;
2040
+ }
2041
+ function tsObjectHasMetadataSource(ts, node) {
2042
+ const metadata = tsObjectPropertyValue(ts, node, "metadata");
2043
+ return tsObjectHasProperty(ts, metadata, "source");
2044
+ }
2045
+ function tsLooksLikeIntentCreatorExpression(ts, node) {
2046
+ if (!node) return false;
2047
+ if (ts.isIdentifier(node)) return /^[A-Z]/.test(node.text);
2048
+ if (ts.isPropertyAccessExpression(node)) {
2049
+ return tsLooksLikeIntentCreatorExpression(ts, node.name);
2050
+ }
2051
+ return false;
2052
+ }
2053
+ function tsIsPublishCall(ts, node) {
2054
+ if (!ts.isCallExpression(node)) return false;
2055
+ const expression = node.expression;
2056
+ if (ts.isPropertyAccessExpression(expression)) {
2057
+ return expression.name.text === "publish";
2058
+ }
2059
+ return ts.isIdentifier(expression) && expression.text === "publish";
2060
+ }
2061
+ function tsIsArkPublishCandidate(ts, node) {
2062
+ if (!ts.isCallExpression(node)) return false;
2063
+ const firstArg = node.arguments[0];
2064
+ const rawIntent = tsStringLiteralText(ts, firstArg);
2065
+ return rawIntent !== void 0 && looksLikeIntentName(rawIntent) || tsObjectHasProperty(ts, firstArg, "intent") || tsLooksLikeIntentCreatorExpression(ts, firstArg);
2066
+ }
2067
+ function tsPublishHasSource(ts, node) {
2068
+ if (!ts.isCallExpression(node)) return false;
2069
+ const [firstArg, secondArg, thirdArg] = node.arguments;
2070
+ return tsObjectHasMetadataSource(ts, firstArg) || tsObjectHasProperty(ts, secondArg, "source") || tsObjectHasProperty(ts, thirdArg, "source");
2071
+ }
2072
+ function tsPublishSourceLiteral(ts, node) {
2073
+ if (!ts.isCallExpression(node)) return void 0;
2074
+ const [firstArg, secondArg, thirdArg] = node.arguments;
2075
+ const rawMetadata = tsObjectPropertyValue(ts, firstArg, "metadata");
2076
+ return tsStringLiteralText(ts, tsObjectPropertyValue(ts, rawMetadata, "source")) ?? tsStringLiteralText(ts, tsObjectPropertyValue(ts, secondArg, "source")) ?? tsStringLiteralText(ts, tsObjectPropertyValue(ts, thirdArg, "source"));
2077
+ }
2078
+ function analyzePublishAst(ts, source, context, profile) {
2079
+ const sourceFile = ts.createSourceFile(
2080
+ "generated.ts",
2081
+ source,
2082
+ ts.ScriptTarget.Latest,
2083
+ true
2084
+ );
2085
+ const gateContext = context;
2086
+ const filePath = gateContext?.filePath;
2087
+ const contextLayer = gateContext?.layer;
2088
+ const violations = [];
2089
+ const lineForNode = (node) => sourceFile.getLineAndCharacterOfPosition(node.getStart(sourceFile)).line + 1;
2090
+ const visit = (node) => {
2091
+ if (tsIsPublishCall(ts, node)) {
2092
+ const firstArg = node.arguments[0];
2093
+ const rawIntent = tsStringLiteralText(ts, firstArg);
2094
+ if (rawIntent && looksLikeIntentName(rawIntent) || tsObjectHasProperty(ts, firstArg, "intent")) {
2095
+ violations.push(
2096
+ violation("RAW_EVENT_PUBLISH", "Publish through a registered intent creator; raw event objects or intent strings bypass Ark contracts and tooling.", {
2097
+ line: lineForNode(node),
2098
+ filePath
2099
+ })
2100
+ );
2101
+ }
2102
+ if (tsIsArkPublishCandidate(ts, node) && !tsPublishHasSource(ts, node)) {
2103
+ violations.push(
2104
+ violation("PUBLISH_MISSING_SOURCE", "Strict Ark publish calls must include metadata.source.", {
2105
+ line: lineForNode(node),
2106
+ filePath
2107
+ })
2108
+ );
2109
+ }
2110
+ const sourceIntent = tsPublishSourceLiteral(ts, node);
2111
+ if (profile && contextLayer && sourceIntent && looksLikeIntentName(sourceIntent)) {
2112
+ const sourceLayer = profile.resolveLayer(sourceIntent);
2113
+ if (sourceLayer && sourceLayer !== contextLayer) {
2114
+ violations.push(
2115
+ violation(
2116
+ "PUBLISH_SOURCE_LAYER_MISMATCH",
2117
+ `Publish source "${sourceIntent}" resolves to ${sourceLayer}, but the target file is classified as ${contextLayer}.`,
2118
+ {
2119
+ line: lineForNode(node),
2120
+ filePath,
2121
+ target: sourceIntent,
2122
+ fromLayer: contextLayer,
2123
+ toLayer: sourceLayer
2124
+ }
2125
+ )
2126
+ );
2127
+ }
2128
+ }
2129
+ }
2130
+ ts.forEachChild(node, visit);
2131
+ };
2132
+ visit(sourceFile);
2133
+ return violations;
2134
+ }
2135
+ function createAICodeGate(options = {}) {
2136
+ const intentNames = new Set(
2137
+ (options.intents || []).map((i) => typeof i === "string" ? i : i.name)
2138
+ );
2139
+ const forbidden = [
2140
+ ...options.forbiddenPatterns || [],
2141
+ /from ['"].*\/(infra|adapters|persistence|db)/i,
2142
+ /import .* from ['"].*(sequelize|prisma|typeorm|mongoose|knex)/i
2143
+ ];
2144
+ const enforceAllowlist = options.enforceIntentAllowlist ?? intentNames.size > 0;
2145
+ return {
2146
+ validate(source, context) {
2147
+ const violations = [];
2148
+ const gateContext = context;
2149
+ const filePath = gateContext?.filePath;
2150
+ const contextLayer = gateContext?.layer;
2151
+ for (const pat of forbidden) {
2152
+ if (pat instanceof RegExp) {
2153
+ const match = source.match(pat);
2154
+ if (match) {
2155
+ violations.push(
2156
+ violation("FORBIDDEN_PATTERN", `Forbidden pattern matched: ${pat}`, {
2157
+ line: match.index === void 0 ? void 0 : lineOf(source, match.index),
2158
+ filePath,
2159
+ suggestion: "Remove infrastructure imports from domain/application layers."
2160
+ })
2161
+ );
2162
+ }
2163
+ } else if (source.includes(pat)) {
2164
+ violations.push(
2165
+ violation("FORBIDDEN_SUBSTRING", `Forbidden substring: ${pat}`, {
2166
+ line: lineOf(source, source.indexOf(pat)),
2167
+ filePath
2168
+ })
2169
+ );
2170
+ }
2171
+ }
2172
+ for (const specifier of extractModuleSpecifiers(source)) {
2173
+ if (!hasInfrastructureToken(specifier.value) && !isKnownInfrastructurePackage(specifier.value)) {
2174
+ continue;
2175
+ }
2176
+ violations.push(
2177
+ violation(
2178
+ "FORBIDDEN_IMPORT",
2179
+ `Forbidden ${specifier.kind} target: "${specifier.value}".`,
2180
+ {
2181
+ line: lineOf(source, specifier.index),
2182
+ source: specifier.value,
2183
+ target: specifier.value,
2184
+ filePath,
2185
+ suggestion: "Route infrastructure access through an allowed adapter or port boundary.",
2186
+ details: { importKind: specifier.kind }
2187
+ }
2188
+ )
2189
+ );
2190
+ }
2191
+ if (options.policies) {
2192
+ for (const policy of options.policies) {
2193
+ const res = policy.check({ source, context });
2194
+ if (res !== true) {
2195
+ if (Array.isArray(res)) {
2196
+ for (const v of res) {
2197
+ violations.push(
2198
+ violation("POLICY_VIOLATION", v.message, {
2199
+ filePath,
2200
+ suggestion: `Fix violation of policy "${policy.name}".`
2201
+ })
2202
+ );
2203
+ }
2204
+ } else if (res === false) {
2205
+ violations.push(
2206
+ violation("POLICY_VIOLATION", `Policy ${policy.name} failed on generated code`)
2207
+ );
2208
+ } else {
2209
+ violations.push(
2210
+ violation("POLICY_VIOLATION", res.message)
2211
+ );
2212
+ }
2213
+ }
2214
+ }
2215
+ }
2216
+ if (enforceAllowlist && intentNames.size > 0) {
2217
+ for (const literal of extractQuotedStrings(source)) {
2218
+ if (looksLikeIntentName(literal.value) && !intentNames.has(literal.value)) {
2219
+ violations.push(
2220
+ violation(
2221
+ "UNKNOWN_INTENT",
2222
+ `Unknown intent reference: "${literal.value}"`,
2223
+ {
2224
+ line: lineOf(source, literal.index),
2225
+ filePath,
2226
+ target: literal.value,
2227
+ suggestion: `Register intent "${literal.value}" via defineIntent() or remove the reference.`
2228
+ }
2229
+ )
2230
+ );
2231
+ }
2232
+ }
2233
+ }
2234
+ if (options.architectureProfile && contextLayer) {
2235
+ for (const literal of extractQuotedStrings(source)) {
2236
+ if (!looksLikeIntentName(literal.value)) continue;
2237
+ const targetLayer = options.architectureProfile.resolveLayer(literal.value);
2238
+ if (!targetLayer) continue;
2239
+ const blocked = options.architectureProfile.rules.find(
2240
+ (rule) => !rule.allowed && rule.from === contextLayer && rule.to === targetLayer
2241
+ );
2242
+ if (blocked) {
2243
+ violations.push(
2244
+ violation(
2245
+ "LAYER_REFERENCE_VIOLATION",
2246
+ blocked.message ?? `Layer "${contextLayer}" must not reference "${targetLayer}" through "${literal.value}".`,
2247
+ {
2248
+ line: lineOf(source, literal.index),
2249
+ filePath,
2250
+ target: literal.value,
2251
+ fromLayer: contextLayer,
2252
+ toLayer: targetLayer,
2253
+ suggestion: "Route the dependency through an allowed intent, port, or event.",
2254
+ details: { rule: blocked }
2255
+ }
2256
+ )
2257
+ );
2258
+ }
2259
+ }
2260
+ }
2261
+ if (options.extensions) {
2262
+ for (const ext of options.extensions) {
2263
+ try {
2264
+ const extViolations = ext.analyze(source, context);
2265
+ violations.push(...extViolations);
2266
+ } catch (err) {
2267
+ violations.push(
2268
+ violation(
2269
+ "EXTENSION_ERROR",
2270
+ `Extension "${ext.name}" failed: ${err instanceof Error ? err.message : String(err)}`
2271
+ )
2272
+ );
2273
+ }
2274
+ }
2275
+ }
2276
+ if (options.typescript) {
2277
+ try {
2278
+ violations.push(
2279
+ ...analyzePublishAst(
2280
+ options.typescript,
2281
+ source,
2282
+ context,
2283
+ options.architectureProfile
2284
+ )
2285
+ );
2286
+ } catch (err) {
2287
+ violations.push(
2288
+ violation(
2289
+ "AST_ANALYZER_ERROR",
2290
+ `TypeScript AST analyzer failed: ${err instanceof Error ? err.message : String(err)}`
2291
+ )
2292
+ );
2293
+ }
2294
+ }
2295
+ return {
2296
+ valid: violations.length === 0,
2297
+ violations
2298
+ };
2299
+ }
2300
+ };
2301
+ }
2302
+
2303
+ // src/kernel/projections/ProjectionRegistry.ts
2304
+ var InMemoryReadModelStore = class {
2305
+ states = /* @__PURE__ */ new Map();
2306
+ load(name) {
2307
+ return this.states.get(name);
2308
+ }
2309
+ save(name, state) {
2310
+ this.states.set(name, state);
2311
+ }
2312
+ clear(name) {
2313
+ if (name) {
2314
+ this.states.delete(name);
2315
+ } else {
2316
+ this.states.clear();
2317
+ }
2318
+ }
2319
+ };
2320
+ function initialState(definition) {
2321
+ return typeof definition.initialState === "function" ? definition.initialState() : definition.initialState;
2322
+ }
2323
+ var ProjectionRegistryImpl = class {
2324
+ definitions = /* @__PURE__ */ new Map();
2325
+ checkpoints = /* @__PURE__ */ new Map();
2326
+ store;
2327
+ auditTrail;
2328
+ constructor(options = {}) {
2329
+ this.store = options.store ?? new InMemoryReadModelStore();
2330
+ this.auditTrail = options.auditTrail;
2331
+ }
2332
+ register(definition) {
2333
+ if (this.definitions.has(definition.name)) {
2334
+ throw new Error(`Projection "${definition.name}" is already registered.`);
2335
+ }
2336
+ this.definitions.set(definition.name, definition);
2337
+ this.checkpoints.set(definition.name, {
2338
+ projection: definition.name,
2339
+ appliedCount: 0
2340
+ });
2341
+ }
2342
+ list() {
2343
+ return Array.from(this.definitions.values());
2344
+ }
2345
+ async apply(event) {
2346
+ const applied = [];
2347
+ for (const definition of this.definitions.values()) {
2348
+ if (!definition.sourceIntents.includes(event.intent)) continue;
2349
+ const current = await this.store.load(definition.name) ?? initialState(definition);
2350
+ const next = await definition.project(event, current);
2351
+ await this.store.save(definition.name, next);
2352
+ const previous = this.checkpoints.get(definition.name);
2353
+ this.checkpoints.set(definition.name, {
2354
+ projection: definition.name,
2355
+ appliedCount: (previous?.appliedCount ?? 0) + 1,
2356
+ lastIntent: event.intent,
2357
+ lastCorrelationId: event.metadata.correlationId,
2358
+ updatedAt: (/* @__PURE__ */ new Date()).toISOString()
2359
+ });
2360
+ await this.auditTrail?.record({
2361
+ type: "projection.applied",
2362
+ source: "Kernel.ProjectionRegistry",
2363
+ intent: event.intent,
2364
+ correlationId: event.metadata.correlationId,
2365
+ causationId: event.metadata.causationId,
2366
+ subject: definition.name,
2367
+ details: { projection: definition.name }
2368
+ });
2369
+ applied.push(definition.name);
2370
+ }
2371
+ return applied;
2372
+ }
2373
+ async getState(name) {
2374
+ return this.store.load(name);
2375
+ }
2376
+ getCheckpoint(name) {
2377
+ return this.checkpoints.get(name);
2378
+ }
2379
+ getCheckpoints() {
2380
+ return Array.from(this.checkpoints.values());
2381
+ }
2382
+ async clear() {
2383
+ await this.store.clear();
2384
+ for (const definition of this.definitions.values()) {
2385
+ this.checkpoints.set(definition.name, {
2386
+ projection: definition.name,
2387
+ appliedCount: 0
2388
+ });
2389
+ }
2390
+ }
2391
+ };
2392
+ function createProjectionRegistry(options) {
2393
+ return new ProjectionRegistryImpl(options);
2394
+ }
2395
+
2396
+ // src/kernel/manifest/constants.ts
2397
+ var MANIFEST_SCHEMA_VERSION = "1.0";
2398
+
2399
+ // src/kernel/manifest/createArkManifest.ts
2400
+ function policyId(name) {
2401
+ return name.toLowerCase().replace(/[^a-z0-9]+/g, "-").replace(/^-|-$/g, "");
2402
+ }
2403
+ var ArkManifestImpl = class {
2404
+ constructor(data) {
2405
+ this.data = data;
2406
+ }
2407
+ data;
2408
+ toJSON() {
2409
+ return { ...this.data };
2410
+ }
2411
+ };
2412
+ function createArkManifest(options = {}) {
2413
+ const registry = options.registry;
2414
+ const policyEngine = options.policyEngine;
2415
+ const metadata = options.metadata;
2416
+ const graph = options.graph;
2417
+ const profile = options.profile;
2418
+ const projections = options.projections;
2419
+ const eventContracts = options.eventContracts;
2420
+ const observability = options.observability;
2421
+ const intents = registry ? registry.list().map((creator) => ({
2422
+ name: creator.name,
2423
+ dependencies: registry.getDependencies(creator.name),
2424
+ productions: registry.getProductions(creator.name)
2425
+ })) : [];
2426
+ const relationships = registry ? registry.getAllRelationships() : [];
2427
+ const policies = policyEngine ? policyEngine.getPolicies().map((p) => ({
2428
+ id: policyId(p.name),
2429
+ name: p.name,
2430
+ severity: p.severity,
2431
+ tags: p.tags ? [...p.tags] : void 0,
2432
+ owner: p.owner,
2433
+ version: p.version,
2434
+ rationale: p.rationale,
2435
+ enforcementMode: p.enforcementMode,
2436
+ deprecated: p.deprecated,
2437
+ replacedBy: p.replacedBy,
2438
+ description: p.tags?.includes("layer") ? "Enforces clean-architecture layer dependency rules on declared relationships." : void 0
2439
+ })) : [];
2440
+ const entities = metadata ? metadata.toJSON() : [];
2441
+ const graphData = graph ? graph.toJSON() : { nodes: [], edges: [] };
2442
+ const entityIntents = entities.filter((e) => (e.emits?.length ?? 0) > 0 || (e.consumes?.length ?? 0) > 0).map((e) => ({
2443
+ entity: e.name,
2444
+ emits: e.emits,
2445
+ consumes: e.consumes
2446
+ }));
2447
+ const projectionData = projections ? projections.list().map((projection) => ({
2448
+ name: projection.name,
2449
+ sourceIntents: projection.sourceIntents,
2450
+ checkpoint: projections.getCheckpoint(projection.name)
2451
+ })) : [];
2452
+ const data = {
2453
+ schemaVersion: MANIFEST_SCHEMA_VERSION,
2454
+ version,
2455
+ exportedAt: (/* @__PURE__ */ new Date()).toISOString(),
2456
+ intents,
2457
+ relationships,
2458
+ policies,
2459
+ entities,
2460
+ graph: graphData,
2461
+ architecture: profile ? {
2462
+ profile: profile.name,
2463
+ layers: profile.layers,
2464
+ rules: profile.rules
2465
+ } : void 0,
2466
+ projections: projectionData,
2467
+ eventContracts: eventContracts?.list() ?? [],
2468
+ observability: observability?.report(),
2469
+ links: { entityIntents }
2470
+ };
2471
+ return new ArkManifestImpl(data);
2472
+ }
2473
+
2474
+ // src/kernel/workflow/Saga.ts
2475
+ var workflowSequence = 0;
2476
+ function createWorkflowId(prefix) {
2477
+ workflowSequence += 1;
2478
+ return `${prefix}-${Date.now()}-${workflowSequence}`;
2479
+ }
2480
+ function errorMessage(error) {
2481
+ return error instanceof Error ? error.message : String(error);
2482
+ }
2483
+ function sleep(ms) {
2484
+ return new Promise((resolve) => {
2485
+ setTimeout(resolve, ms);
2486
+ });
2487
+ }
2488
+ async function withTimeout(operation, timeoutMs, stepName) {
2489
+ if (timeoutMs === void 0) return operation;
2490
+ let timeout;
2491
+ const timeoutPromise = new Promise((_, reject) => {
2492
+ timeout = setTimeout(() => {
2493
+ reject(new Error(`Workflow step "${stepName}" timed out after ${timeoutMs}ms.`));
2494
+ }, timeoutMs);
2495
+ });
2496
+ try {
2497
+ return await Promise.race([operation, timeoutPromise]);
2498
+ } finally {
2499
+ if (timeout) clearTimeout(timeout);
2500
+ }
2501
+ }
2502
+ var InMemoryWorkflowStore = class {
2503
+ snapshots = /* @__PURE__ */ new Map();
2504
+ save(snapshot) {
2505
+ this.snapshots.set(snapshot.id, { ...snapshot, context: { ...snapshot.context } });
2506
+ }
2507
+ get(id) {
2508
+ const snapshot = this.snapshots.get(id);
2509
+ return snapshot ? { ...snapshot, context: { ...snapshot.context } } : void 0;
2510
+ }
2511
+ list(workflowName) {
2512
+ return Array.from(this.snapshots.values()).filter((snapshot) => !workflowName || snapshot.workflowName === workflowName).map((snapshot) => ({ ...snapshot, context: { ...snapshot.context } }));
2513
+ }
2514
+ clear() {
2515
+ this.snapshots.clear();
2516
+ }
2517
+ };
2518
+ var WorkflowEngineImpl = class {
2519
+ constructor(bus, options = {}) {
2520
+ this.bus = bus;
2521
+ this.options = options;
2522
+ this.store = options.store ?? new InMemoryWorkflowStore();
2523
+ }
2524
+ bus;
2525
+ options;
2526
+ definitions = /* @__PURE__ */ new Map();
2527
+ store;
2528
+ register(definition) {
2529
+ if (this.definitions.has(definition.name)) {
2530
+ throw new Error(`Workflow "${definition.name}" is already registered.`);
2531
+ }
2532
+ this.definitions.set(definition.name, definition);
2533
+ if (definition.startOn) {
2534
+ const trigger = definition.startOn;
2535
+ this.bus.subscribe(trigger.intent, async (event) => {
2536
+ await this.start(definition.name, trigger.mapEventToPayload(event));
2537
+ });
2538
+ }
2539
+ }
2540
+ async start(workflowName, initialPayload, options = {}) {
2541
+ const definition = this.definitions.get(workflowName);
2542
+ if (!definition) {
2543
+ throw new Error(`Workflow "${workflowName}" is not registered.`);
2544
+ }
2545
+ const now = (/* @__PURE__ */ new Date()).toISOString();
2546
+ const snapshot = {
2547
+ id: options.id ?? createWorkflowId(workflowName),
2548
+ workflowName,
2549
+ status: "running",
2550
+ context: { ...initialPayload },
2551
+ completedSteps: [],
2552
+ attempts: {},
2553
+ startedAt: now,
2554
+ updatedAt: now
2555
+ };
2556
+ await this.store.save(snapshot);
2557
+ await this.audit("workflow.started", snapshot, { workflowName });
2558
+ try {
2559
+ for (const step of definition.steps) {
2560
+ await this.runStep(snapshot, step);
2561
+ }
2562
+ snapshot.status = "completed";
2563
+ snapshot.currentStep = void 0;
2564
+ snapshot.completedAt = (/* @__PURE__ */ new Date()).toISOString();
2565
+ snapshot.updatedAt = snapshot.completedAt;
2566
+ await this.store.save(snapshot);
2567
+ await this.audit("workflow.completed", snapshot, { workflowName });
2568
+ return { ...snapshot, context: { ...snapshot.context } };
2569
+ } catch (err) {
2570
+ await this.compensate(snapshot, definition.steps, err);
2571
+ snapshot.status = "failed";
2572
+ snapshot.error = errorMessage(err);
2573
+ snapshot.updatedAt = (/* @__PURE__ */ new Date()).toISOString();
2574
+ await this.store.save(snapshot);
2575
+ await this.audit("workflow.failed", snapshot, {
2576
+ workflowName,
2577
+ error: snapshot.error,
2578
+ failedStep: snapshot.failedStep
2579
+ });
2580
+ throw err;
2581
+ }
2582
+ }
2583
+ async get(id) {
2584
+ return this.store.get(id);
2585
+ }
2586
+ async list(workflowName) {
2587
+ return this.store.list(workflowName);
2588
+ }
2589
+ async runStep(snapshot, step) {
2590
+ const retry = step.retry ?? this.options.defaultRetry ?? { attempts: 1 };
2591
+ const maxAttempts = Math.max(1, retry.attempts);
2592
+ snapshot.currentStep = step.name;
2593
+ snapshot.updatedAt = (/* @__PURE__ */ new Date()).toISOString();
2594
+ await this.store.save(snapshot);
2595
+ for (let attempt = 1; attempt <= maxAttempts; attempt += 1) {
2596
+ snapshot.attempts[step.name] = attempt;
2597
+ await this.store.save(snapshot);
2598
+ try {
2599
+ const result = await withTimeout(
2600
+ Promise.resolve(step.execute(snapshot.context, this.bus)),
2601
+ step.timeoutMs,
2602
+ step.name
2603
+ );
2604
+ if (result) Object.assign(snapshot.context, result);
2605
+ snapshot.completedSteps.push(step.name);
2606
+ snapshot.currentStep = void 0;
2607
+ snapshot.updatedAt = (/* @__PURE__ */ new Date()).toISOString();
2608
+ await this.store.save(snapshot);
2609
+ await this.audit("workflow.step.completed", snapshot, {
2610
+ step: step.name,
2611
+ attempt
2612
+ });
2613
+ return;
2614
+ } catch (err) {
2615
+ if (attempt < maxAttempts) {
2616
+ if (retry.delayMs) await sleep(retry.delayMs);
2617
+ continue;
2618
+ }
2619
+ snapshot.failedStep = step.name;
2620
+ snapshot.updatedAt = (/* @__PURE__ */ new Date()).toISOString();
2621
+ await this.store.save(snapshot);
2622
+ await this.audit("workflow.step.failed", snapshot, {
2623
+ step: step.name,
2624
+ attempt,
2625
+ error: errorMessage(err)
2626
+ });
2627
+ throw err;
2628
+ }
2629
+ }
2630
+ throw new Error(`Workflow step "${step.name}" did not complete.`);
2631
+ }
2632
+ async compensate(snapshot, steps, error) {
2633
+ snapshot.status = "compensating";
2634
+ snapshot.updatedAt = (/* @__PURE__ */ new Date()).toISOString();
2635
+ await this.store.save(snapshot);
2636
+ const completed = steps.filter(
2637
+ (step) => snapshot.completedSteps.includes(step.name)
2638
+ );
2639
+ for (let index = completed.length - 1; index >= 0; index -= 1) {
2640
+ const step = completed[index];
2641
+ if (!step.compensate) continue;
2642
+ try {
2643
+ await Promise.resolve(step.compensate(snapshot.context, this.bus, error));
2644
+ await this.audit("workflow.compensation.completed", snapshot, {
2645
+ step: step.name
2646
+ });
2647
+ } catch (compensationError) {
2648
+ await this.audit("workflow.step.failed", snapshot, {
2649
+ step: step.name,
2650
+ compensation: true,
2651
+ error: errorMessage(compensationError)
2652
+ });
2653
+ }
2654
+ }
2655
+ }
2656
+ async audit(type, snapshot, details) {
2657
+ await this.options.auditTrail?.record({
2658
+ type,
2659
+ source: "Kernel.WorkflowEngine",
2660
+ subject: snapshot.id,
2661
+ details
2662
+ });
2663
+ }
2664
+ };
2665
+ function createWorkflowEngine(bus, options) {
2666
+ return new WorkflowEngineImpl(bus, options);
2667
+ }
2668
+ function createSaga(def, bus, options = {}) {
2669
+ const engine = createWorkflowEngine(bus, options);
2670
+ const definition = { name: def.name, steps: def.steps };
2671
+ engine.register(definition);
2672
+ const sagaId = options.id ?? createWorkflowId(def.name);
2673
+ let status = "idle";
2674
+ let completedStepNames = [];
2675
+ return {
2676
+ id: sagaId,
2677
+ definition: def,
2678
+ get status() {
2679
+ return status;
2680
+ },
2681
+ get completedSteps() {
2682
+ return [...completedStepNames];
2683
+ },
2684
+ async run(initialPayload) {
2685
+ status = "running";
2686
+ completedStepNames = [];
2687
+ try {
2688
+ const snapshot = await engine.start(def.name, initialPayload, { id: sagaId });
2689
+ status = snapshot.status === "waiting" ? "idle" : snapshot.status;
2690
+ completedStepNames = [...snapshot.completedSteps];
2691
+ } catch (err) {
2692
+ const snapshot = await engine.get(sagaId);
2693
+ status = snapshot?.status === "waiting" ? "idle" : snapshot?.status ?? "failed";
2694
+ completedStepNames = snapshot?.completedSteps ?? [];
2695
+ throw err;
2696
+ }
2697
+ }
2698
+ };
2699
+ }
2700
+
2701
+ // src/kernel/runtime/createArkKernel.ts
2702
+ var kernelSequence = 0;
2703
+ function nextKernelInstanceId() {
2704
+ kernelSequence += 1;
2705
+ return `ark-kernel-${Date.now()}-${kernelSequence}`;
2706
+ }
2707
+ function createArkKernel(options = {}) {
2708
+ const strict = options.strict ?? true;
2709
+ const instanceId = options.instanceId ?? nextKernelInstanceId();
2710
+ const profile = options.profile ?? elevenLayerProfile;
2711
+ const registry = createIntentRegistry();
2712
+ const graph = createDependencyGraph();
2713
+ const metadata = options.metadata ?? createMetadataRegistry();
2714
+ const auditTrail = options.auditTrail ?? createAuditTrail({ maxRecords: options.maxHistorySize });
2715
+ const eventContracts = options.eventContracts ?? createEventContractRegistry();
2716
+ const outbox = options.outbox ?? new InMemoryOutboxStore();
2717
+ const projections = options.projections ?? createProjectionRegistry({ auditTrail });
2718
+ const policyEngine = new PolicyEngine([
2719
+ defineArchitectureProfilePolicy(profile),
2720
+ ...options.policies ?? []
2721
+ ]);
2722
+ const syncGraph = () => {
2723
+ syncRegistryToGraph(registry, graph, { requireRegisteredTargets: true });
2724
+ };
2725
+ const eventBus = createEventBus({
2726
+ intentRegistry: registry,
2727
+ dependencyGraph: graph,
2728
+ policyEngine,
2729
+ strictRegistry: true,
2730
+ validateIntentNaming: true,
2731
+ auditTrail,
2732
+ eventContracts,
2733
+ strictEventContracts: options.strictEventContracts ?? strict,
2734
+ requireKnownSource: options.requireKnownSource ?? true,
2735
+ architectureProfile: profile,
2736
+ enforceObservedLayerFlow: options.enforceObservedLayerFlow ?? (strict ? "hard" : "off"),
2737
+ outbox,
2738
+ instanceId,
2739
+ maxHistorySize: options.maxHistorySize,
2740
+ onPublish: options.autoApplyProjections === false ? void 0 : async (event) => {
2741
+ await projections.apply(event);
2742
+ }
2743
+ });
2744
+ const workflowEngine = createWorkflowEngine(eventBus, { auditTrail });
2745
+ const observability = createObservabilityReporter({
2746
+ registry,
2747
+ eventBus,
2748
+ graph
2749
+ });
2750
+ return {
2751
+ instanceId,
2752
+ profile,
2753
+ registry,
2754
+ graph,
2755
+ metadata,
2756
+ auditTrail,
2757
+ eventContracts,
2758
+ outbox,
2759
+ projections,
2760
+ policyEngine,
2761
+ eventBus,
2762
+ workflowEngine,
2763
+ observability,
2764
+ publisher(source) {
2765
+ return eventBus.createPublisher(source);
2766
+ },
2767
+ syncGraph,
2768
+ manifest() {
2769
+ syncGraph();
2770
+ return createArkManifest({
2771
+ registry,
2772
+ policyEngine,
2773
+ metadata,
2774
+ graph,
2775
+ profile,
2776
+ projections,
2777
+ eventContracts,
2778
+ observability
2779
+ });
2780
+ }
2781
+ };
2782
+ }
2783
+ function createStrictArkKernel(options = {}) {
2784
+ return createArkKernel({
2785
+ ...options,
2786
+ strict: true,
2787
+ strictEventContracts: options.strictEventContracts ?? true,
2788
+ requireKnownSource: options.requireKnownSource ?? true,
2789
+ enforceObservedLayerFlow: options.enforceObservedLayerFlow ?? "hard"
2790
+ });
2791
+ }
2792
+ function createLenientArkKernel(options = {}) {
2793
+ return createArkKernel({
2794
+ ...options,
2795
+ strict: false,
2796
+ strictEventContracts: options.strictEventContracts ?? false,
2797
+ enforceObservedLayerFlow: options.enforceObservedLayerFlow ?? "off"
2798
+ });
2799
+ }
2800
+
2801
+ exports.EventContractRegistryImpl = EventContractRegistryImpl;
2802
+ exports.EventContractViolationError = EventContractViolationError;
2803
+ exports.InMemoryAuditStore = InMemoryAuditStore;
2804
+ exports.InMemoryOutboxStore = InMemoryOutboxStore;
2805
+ exports.InMemoryReadModelStore = InMemoryReadModelStore;
2806
+ exports.InMemoryWorkflowStore = InMemoryWorkflowStore;
2807
+ exports.IntentRegistry = IntentRegistry;
2808
+ exports.InvalidIntentNameError = InvalidIntentNameError;
2809
+ exports.LayerPolicyContextError = LayerPolicyContextError;
2810
+ exports.MANIFEST_SCHEMA_VERSION = MANIFEST_SCHEMA_VERSION;
2811
+ exports.ObservedLayerFlowViolationError = ObservedLayerFlowViolationError;
2812
+ exports.PolicyEngine = PolicyEngine;
2813
+ exports.PolicyViolationError = PolicyViolationError;
2814
+ exports.SourceMetadataOverrideError = SourceMetadataOverrideError;
2815
+ exports.UnknownEventSourceError = UnknownEventSourceError;
2816
+ exports.UnregisteredIntentError = UnregisteredIntentError;
2817
+ exports.architecturalPolicies = architecturalPolicies;
2818
+ exports.buildPublishPolicyContext = buildPublishPolicyContext;
2819
+ exports.checkAdapterGovernance = checkAdapterGovernance;
2820
+ exports.checkContract = checkContract;
2821
+ exports.createAICodeGate = createAICodeGate;
2822
+ exports.createAdapter = createAdapter;
2823
+ exports.createArchitectureProfile = createArchitectureProfile;
2824
+ exports.createArkKernel = createArkKernel;
2825
+ exports.createArkManifest = createArkManifest;
2826
+ exports.createArkTestHarness = createArkTestHarness;
2827
+ exports.createAuditTrail = createAuditTrail;
2828
+ exports.createDependencyGraph = createDependencyGraph;
2829
+ exports.createElevenLayerArkConfig = createElevenLayerArkConfig;
2830
+ exports.createEventBus = createEventBus;
2831
+ exports.createEventContractRegistry = createEventContractRegistry;
2832
+ exports.createIntentRegistry = createIntentRegistry;
2833
+ exports.createLenientArkKernel = createLenientArkKernel;
2834
+ exports.createMetadataRegistry = createMetadataRegistry;
2835
+ exports.createObservabilityReporter = createObservabilityReporter;
2836
+ exports.createProjectionRegistry = createProjectionRegistry;
2837
+ exports.createSaga = createSaga;
2838
+ exports.createStrictArkKernel = createStrictArkKernel;
2839
+ exports.createWorkflowEngine = createWorkflowEngine;
2840
+ exports.defaultIntentRegistry = defaultIntentRegistry;
2841
+ exports.defineArchitectureProfilePolicy = defineArchitectureProfilePolicy;
2842
+ exports.defineIntent = defineIntent;
2843
+ exports.defineLayerPolicy = defineLayerPolicy;
2844
+ exports.definePolicy = definePolicy;
2845
+ exports.definePort = definePort;
2846
+ exports.definePublishPolicy = definePublishPolicy;
2847
+ exports.elevenLayerProfile = elevenLayerProfile;
2848
+ exports.isLayerPolicy = isLayerPolicy;
2849
+ exports.syncRegistryToGraph = syncRegistryToGraph;
2850
+ exports.validateIntentName = validateIntentName;
2851
+ exports.version = version;
2852
+ //# sourceMappingURL=index.cjs.map
2853
+ //# sourceMappingURL=index.cjs.map