apcore-js 0.1.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (142) hide show
  1. package/.claude/settings.local.json +11 -0
  2. package/.gitmessage +60 -0
  3. package/.pre-commit-config.yaml +28 -0
  4. package/CHANGELOG.md +47 -0
  5. package/CLAUDE.md +68 -0
  6. package/README.md +131 -0
  7. package/apcore-logo.svg +79 -0
  8. package/package.json +37 -0
  9. package/planning/acl-system/overview.md +54 -0
  10. package/planning/acl-system/plan.md +92 -0
  11. package/planning/acl-system/state.json +76 -0
  12. package/planning/acl-system/tasks/acl-core.md +226 -0
  13. package/planning/acl-system/tasks/acl-rule.md +92 -0
  14. package/planning/acl-system/tasks/conditional-rules.md +259 -0
  15. package/planning/acl-system/tasks/pattern-matching.md +152 -0
  16. package/planning/acl-system/tasks/yaml-loading.md +271 -0
  17. package/planning/core-executor/overview.md +53 -0
  18. package/planning/core-executor/plan.md +88 -0
  19. package/planning/core-executor/state.json +76 -0
  20. package/planning/core-executor/tasks/async-support.md +106 -0
  21. package/planning/core-executor/tasks/execution-pipeline.md +113 -0
  22. package/planning/core-executor/tasks/redaction.md +85 -0
  23. package/planning/core-executor/tasks/safety-checks.md +65 -0
  24. package/planning/core-executor/tasks/setup.md +75 -0
  25. package/planning/decorator-bindings/overview.md +62 -0
  26. package/planning/decorator-bindings/plan.md +104 -0
  27. package/planning/decorator-bindings/state.json +87 -0
  28. package/planning/decorator-bindings/tasks/binding-directory.md +79 -0
  29. package/planning/decorator-bindings/tasks/binding-loader.md +148 -0
  30. package/planning/decorator-bindings/tasks/explicit-schemas.md +85 -0
  31. package/planning/decorator-bindings/tasks/function-module.md +127 -0
  32. package/planning/decorator-bindings/tasks/module-factory.md +89 -0
  33. package/planning/decorator-bindings/tasks/schema-modes.md +142 -0
  34. package/planning/middleware-system/overview.md +48 -0
  35. package/planning/middleware-system/plan.md +102 -0
  36. package/planning/middleware-system/state.json +65 -0
  37. package/planning/middleware-system/tasks/adapters.md +170 -0
  38. package/planning/middleware-system/tasks/base.md +115 -0
  39. package/planning/middleware-system/tasks/logging-middleware.md +304 -0
  40. package/planning/middleware-system/tasks/manager.md +313 -0
  41. package/planning/observability/overview.md +53 -0
  42. package/planning/observability/plan.md +119 -0
  43. package/planning/observability/state.json +98 -0
  44. package/planning/observability/tasks/context-logger.md +201 -0
  45. package/planning/observability/tasks/exporters.md +121 -0
  46. package/planning/observability/tasks/metrics-collector.md +162 -0
  47. package/planning/observability/tasks/metrics-middleware.md +141 -0
  48. package/planning/observability/tasks/obs-logging-middleware.md +179 -0
  49. package/planning/observability/tasks/span-model.md +120 -0
  50. package/planning/observability/tasks/tracing-middleware.md +179 -0
  51. package/planning/overview.md +81 -0
  52. package/planning/registry-system/overview.md +57 -0
  53. package/planning/registry-system/plan.md +114 -0
  54. package/planning/registry-system/state.json +109 -0
  55. package/planning/registry-system/tasks/dependencies.md +157 -0
  56. package/planning/registry-system/tasks/entry-point.md +148 -0
  57. package/planning/registry-system/tasks/metadata.md +198 -0
  58. package/planning/registry-system/tasks/registry-core.md +323 -0
  59. package/planning/registry-system/tasks/scanner.md +172 -0
  60. package/planning/registry-system/tasks/schema-export.md +261 -0
  61. package/planning/registry-system/tasks/types.md +124 -0
  62. package/planning/registry-system/tasks/validation.md +177 -0
  63. package/planning/schema-system/overview.md +56 -0
  64. package/planning/schema-system/plan.md +121 -0
  65. package/planning/schema-system/state.json +98 -0
  66. package/planning/schema-system/tasks/exporter.md +153 -0
  67. package/planning/schema-system/tasks/loader.md +106 -0
  68. package/planning/schema-system/tasks/ref-resolver.md +133 -0
  69. package/planning/schema-system/tasks/strict-mode.md +140 -0
  70. package/planning/schema-system/tasks/typebox-generation.md +133 -0
  71. package/planning/schema-system/tasks/types-and-annotations.md +160 -0
  72. package/planning/schema-system/tasks/validator.md +149 -0
  73. package/src/acl.ts +188 -0
  74. package/src/bindings.ts +208 -0
  75. package/src/config.ts +24 -0
  76. package/src/context.ts +75 -0
  77. package/src/decorator.ts +110 -0
  78. package/src/errors.ts +369 -0
  79. package/src/executor.ts +348 -0
  80. package/src/index.ts +81 -0
  81. package/src/middleware/adapters.ts +54 -0
  82. package/src/middleware/base.ts +33 -0
  83. package/src/middleware/index.ts +6 -0
  84. package/src/middleware/logging.ts +103 -0
  85. package/src/middleware/manager.ts +105 -0
  86. package/src/module.ts +41 -0
  87. package/src/observability/context-logger.ts +201 -0
  88. package/src/observability/index.ts +4 -0
  89. package/src/observability/metrics.ts +212 -0
  90. package/src/observability/tracing.ts +187 -0
  91. package/src/registry/dependencies.ts +99 -0
  92. package/src/registry/entry-point.ts +64 -0
  93. package/src/registry/index.ts +8 -0
  94. package/src/registry/metadata.ts +111 -0
  95. package/src/registry/registry.ts +314 -0
  96. package/src/registry/scanner.ts +150 -0
  97. package/src/registry/schema-export.ts +177 -0
  98. package/src/registry/types.ts +32 -0
  99. package/src/registry/validation.ts +38 -0
  100. package/src/schema/annotations.ts +67 -0
  101. package/src/schema/exporter.ts +93 -0
  102. package/src/schema/index.ts +14 -0
  103. package/src/schema/loader.ts +270 -0
  104. package/src/schema/ref-resolver.ts +235 -0
  105. package/src/schema/strict.ts +128 -0
  106. package/src/schema/types.ts +73 -0
  107. package/src/schema/validator.ts +82 -0
  108. package/src/utils/index.ts +1 -0
  109. package/src/utils/pattern.ts +30 -0
  110. package/tests/helpers.ts +30 -0
  111. package/tests/integration/test-acl-safety.test.ts +268 -0
  112. package/tests/integration/test-binding-executor.test.ts +194 -0
  113. package/tests/integration/test-e2e-flow.test.ts +117 -0
  114. package/tests/integration/test-error-propagation.test.ts +259 -0
  115. package/tests/integration/test-middleware-chain.test.ts +120 -0
  116. package/tests/integration/test-observability-integration.test.ts +438 -0
  117. package/tests/observability/test-context-logger.test.ts +123 -0
  118. package/tests/observability/test-metrics.test.ts +89 -0
  119. package/tests/observability/test-tracing.test.ts +131 -0
  120. package/tests/registry/test-dependencies.test.ts +70 -0
  121. package/tests/registry/test-entry-point.test.ts +133 -0
  122. package/tests/registry/test-metadata.test.ts +265 -0
  123. package/tests/registry/test-registry.test.ts +140 -0
  124. package/tests/registry/test-scanner.test.ts +257 -0
  125. package/tests/registry/test-schema-export.test.ts +224 -0
  126. package/tests/registry/test-validation.test.ts +75 -0
  127. package/tests/schema/test-loader.test.ts +97 -0
  128. package/tests/schema/test-ref-resolver.test.ts +105 -0
  129. package/tests/schema/test-strict.test.ts +139 -0
  130. package/tests/schema/test-validator.test.ts +64 -0
  131. package/tests/test-acl.test.ts +206 -0
  132. package/tests/test-bindings.test.ts +227 -0
  133. package/tests/test-config.test.ts +76 -0
  134. package/tests/test-context.test.ts +151 -0
  135. package/tests/test-decorator.test.ts +173 -0
  136. package/tests/test-errors.test.ts +204 -0
  137. package/tests/test-executor.test.ts +252 -0
  138. package/tests/test-middleware-manager.test.ts +185 -0
  139. package/tests/test-middleware.test.ts +86 -0
  140. package/tsconfig.build.json +8 -0
  141. package/tsconfig.json +20 -0
  142. package/vitest.config.ts +18 -0
package/src/errors.ts ADDED
@@ -0,0 +1,369 @@
1
+ /**
2
+ * Error hierarchy for the apcore framework.
3
+ */
4
+
5
+ export class ModuleError extends Error {
6
+ readonly code: string;
7
+ readonly details: Record<string, unknown>;
8
+ readonly cause?: Error;
9
+ readonly traceId?: string;
10
+ readonly timestamp: string;
11
+
12
+ constructor(
13
+ code: string,
14
+ message: string,
15
+ details?: Record<string, unknown>,
16
+ cause?: Error,
17
+ traceId?: string,
18
+ ) {
19
+ super(message);
20
+ this.name = 'ModuleError';
21
+ this.code = code;
22
+ this.details = details ?? {};
23
+ this.cause = cause;
24
+ this.traceId = traceId;
25
+ this.timestamp = new Date().toISOString();
26
+ }
27
+
28
+ override toString(): string {
29
+ return `[${this.code}] ${this.message}`;
30
+ }
31
+ }
32
+
33
+ export class ConfigNotFoundError extends ModuleError {
34
+ constructor(configPath: string, options?: { cause?: Error; traceId?: string }) {
35
+ super(
36
+ 'CONFIG_NOT_FOUND',
37
+ `Configuration file not found: ${configPath}`,
38
+ { configPath },
39
+ options?.cause,
40
+ options?.traceId,
41
+ );
42
+ this.name = 'ConfigNotFoundError';
43
+ }
44
+ }
45
+
46
+ export class ConfigError extends ModuleError {
47
+ constructor(message: string, options?: { cause?: Error; traceId?: string }) {
48
+ super('CONFIG_INVALID', message, {}, options?.cause, options?.traceId);
49
+ this.name = 'ConfigError';
50
+ }
51
+ }
52
+
53
+ export class ACLRuleError extends ModuleError {
54
+ constructor(message: string, options?: { cause?: Error; traceId?: string }) {
55
+ super('ACL_RULE_ERROR', message, {}, options?.cause, options?.traceId);
56
+ this.name = 'ACLRuleError';
57
+ }
58
+ }
59
+
60
+ export class ACLDeniedError extends ModuleError {
61
+ constructor(callerId: string | null, targetId: string, options?: { cause?: Error; traceId?: string }) {
62
+ super(
63
+ 'ACL_DENIED',
64
+ `Access denied: ${callerId} -> ${targetId}`,
65
+ { callerId, targetId },
66
+ options?.cause,
67
+ options?.traceId,
68
+ );
69
+ this.name = 'ACLDeniedError';
70
+ }
71
+
72
+ get callerId(): string | null {
73
+ return this.details['callerId'] as string | null;
74
+ }
75
+
76
+ get targetId(): string {
77
+ return this.details['targetId'] as string;
78
+ }
79
+ }
80
+
81
+ export class ModuleNotFoundError extends ModuleError {
82
+ constructor(moduleId: string, options?: { cause?: Error; traceId?: string }) {
83
+ super(
84
+ 'MODULE_NOT_FOUND',
85
+ `Module not found: ${moduleId}`,
86
+ { moduleId },
87
+ options?.cause,
88
+ options?.traceId,
89
+ );
90
+ this.name = 'ModuleNotFoundError';
91
+ }
92
+ }
93
+
94
+ export class ModuleTimeoutError extends ModuleError {
95
+ constructor(moduleId: string, timeoutMs: number, options?: { cause?: Error; traceId?: string }) {
96
+ super(
97
+ 'MODULE_TIMEOUT',
98
+ `Module ${moduleId} timed out after ${timeoutMs}ms`,
99
+ { moduleId, timeoutMs },
100
+ options?.cause,
101
+ options?.traceId,
102
+ );
103
+ this.name = 'ModuleTimeoutError';
104
+ }
105
+
106
+ get moduleId(): string {
107
+ return this.details['moduleId'] as string;
108
+ }
109
+
110
+ get timeoutMs(): number {
111
+ return this.details['timeoutMs'] as number;
112
+ }
113
+ }
114
+
115
+ export class SchemaValidationError extends ModuleError {
116
+ constructor(
117
+ message: string = 'Schema validation failed',
118
+ errors?: Array<Record<string, unknown>>,
119
+ options?: { cause?: Error; traceId?: string },
120
+ ) {
121
+ super(
122
+ 'SCHEMA_VALIDATION_ERROR',
123
+ message,
124
+ { errors: errors ?? [] },
125
+ options?.cause,
126
+ options?.traceId,
127
+ );
128
+ this.name = 'SchemaValidationError';
129
+ }
130
+ }
131
+
132
+ export class SchemaNotFoundError extends ModuleError {
133
+ constructor(schemaId: string, options?: { cause?: Error; traceId?: string }) {
134
+ super(
135
+ 'SCHEMA_NOT_FOUND',
136
+ `Schema not found: ${schemaId}`,
137
+ { schemaId },
138
+ options?.cause,
139
+ options?.traceId,
140
+ );
141
+ this.name = 'SchemaNotFoundError';
142
+ }
143
+ }
144
+
145
+ export class SchemaParseError extends ModuleError {
146
+ constructor(message: string, options?: { cause?: Error; traceId?: string }) {
147
+ super('SCHEMA_PARSE_ERROR', message, {}, options?.cause, options?.traceId);
148
+ this.name = 'SchemaParseError';
149
+ }
150
+ }
151
+
152
+ export class SchemaCircularRefError extends ModuleError {
153
+ constructor(refPath: string, options?: { cause?: Error; traceId?: string }) {
154
+ super(
155
+ 'SCHEMA_CIRCULAR_REF',
156
+ `Circular reference detected: ${refPath}`,
157
+ { refPath },
158
+ options?.cause,
159
+ options?.traceId,
160
+ );
161
+ this.name = 'SchemaCircularRefError';
162
+ }
163
+ }
164
+
165
+ export class CallDepthExceededError extends ModuleError {
166
+ constructor(depth: number, maxDepth: number, callChain: string[], options?: { cause?: Error; traceId?: string }) {
167
+ super(
168
+ 'CALL_DEPTH_EXCEEDED',
169
+ `Call depth ${depth} exceeds maximum ${maxDepth}`,
170
+ { depth, maxDepth, callChain },
171
+ options?.cause,
172
+ options?.traceId,
173
+ );
174
+ this.name = 'CallDepthExceededError';
175
+ }
176
+
177
+ get currentDepth(): number {
178
+ return this.details['depth'] as number;
179
+ }
180
+
181
+ get maxDepth(): number {
182
+ return this.details['maxDepth'] as number;
183
+ }
184
+ }
185
+
186
+ export class CircularCallError extends ModuleError {
187
+ constructor(moduleId: string, callChain: string[], options?: { cause?: Error; traceId?: string }) {
188
+ super(
189
+ 'CIRCULAR_CALL',
190
+ `Circular call detected for module ${moduleId}`,
191
+ { moduleId, callChain },
192
+ options?.cause,
193
+ options?.traceId,
194
+ );
195
+ this.name = 'CircularCallError';
196
+ }
197
+
198
+ get moduleId(): string {
199
+ return this.details['moduleId'] as string;
200
+ }
201
+ }
202
+
203
+ export class CallFrequencyExceededError extends ModuleError {
204
+ constructor(
205
+ moduleId: string,
206
+ count: number,
207
+ maxRepeat: number,
208
+ callChain: string[],
209
+ options?: { cause?: Error; traceId?: string },
210
+ ) {
211
+ super(
212
+ 'CALL_FREQUENCY_EXCEEDED',
213
+ `Module ${moduleId} called ${count} times, max is ${maxRepeat}`,
214
+ { moduleId, count, maxRepeat, callChain },
215
+ options?.cause,
216
+ options?.traceId,
217
+ );
218
+ this.name = 'CallFrequencyExceededError';
219
+ }
220
+
221
+ get moduleId(): string {
222
+ return this.details['moduleId'] as string;
223
+ }
224
+
225
+ get count(): number {
226
+ return this.details['count'] as number;
227
+ }
228
+
229
+ get maxRepeat(): number {
230
+ return this.details['maxRepeat'] as number;
231
+ }
232
+ }
233
+
234
+ export class InvalidInputError extends ModuleError {
235
+ constructor(message: string = 'Invalid input', options?: { cause?: Error; traceId?: string }) {
236
+ super('GENERAL_INVALID_INPUT', message, {}, options?.cause, options?.traceId);
237
+ this.name = 'InvalidInputError';
238
+ }
239
+ }
240
+
241
+ export class FuncMissingTypeHintError extends ModuleError {
242
+ constructor(functionName: string, parameterName: string, options?: { cause?: Error; traceId?: string }) {
243
+ super(
244
+ 'FUNC_MISSING_TYPE_HINT',
245
+ `Parameter '${parameterName}' in function '${functionName}' has no type annotation. Add a type hint like '${parameterName}: str'.`,
246
+ { functionName, parameterName },
247
+ options?.cause,
248
+ options?.traceId,
249
+ );
250
+ this.name = 'FuncMissingTypeHintError';
251
+ }
252
+ }
253
+
254
+ export class FuncMissingReturnTypeError extends ModuleError {
255
+ constructor(functionName: string, options?: { cause?: Error; traceId?: string }) {
256
+ super(
257
+ 'FUNC_MISSING_RETURN_TYPE',
258
+ `Function '${functionName}' has no return type annotation. Add a return type like '-> dict'.`,
259
+ { functionName },
260
+ options?.cause,
261
+ options?.traceId,
262
+ );
263
+ this.name = 'FuncMissingReturnTypeError';
264
+ }
265
+ }
266
+
267
+ export class BindingInvalidTargetError extends ModuleError {
268
+ constructor(target: string, options?: { cause?: Error; traceId?: string }) {
269
+ super(
270
+ 'BINDING_INVALID_TARGET',
271
+ `Invalid binding target '${target}'. Expected format: 'module.path:callable_name'.`,
272
+ { target },
273
+ options?.cause,
274
+ options?.traceId,
275
+ );
276
+ this.name = 'BindingInvalidTargetError';
277
+ }
278
+ }
279
+
280
+ export class BindingModuleNotFoundError extends ModuleError {
281
+ constructor(modulePath: string, options?: { cause?: Error; traceId?: string }) {
282
+ super(
283
+ 'BINDING_MODULE_NOT_FOUND',
284
+ `Cannot import module '${modulePath}'.`,
285
+ { modulePath },
286
+ options?.cause,
287
+ options?.traceId,
288
+ );
289
+ this.name = 'BindingModuleNotFoundError';
290
+ }
291
+ }
292
+
293
+ export class BindingCallableNotFoundError extends ModuleError {
294
+ constructor(callableName: string, modulePath: string, options?: { cause?: Error; traceId?: string }) {
295
+ super(
296
+ 'BINDING_CALLABLE_NOT_FOUND',
297
+ `Cannot find callable '${callableName}' in module '${modulePath}'.`,
298
+ { callableName, modulePath },
299
+ options?.cause,
300
+ options?.traceId,
301
+ );
302
+ this.name = 'BindingCallableNotFoundError';
303
+ }
304
+ }
305
+
306
+ export class BindingNotCallableError extends ModuleError {
307
+ constructor(target: string, options?: { cause?: Error; traceId?: string }) {
308
+ super(
309
+ 'BINDING_NOT_CALLABLE',
310
+ `Resolved target '${target}' is not callable.`,
311
+ { target },
312
+ options?.cause,
313
+ options?.traceId,
314
+ );
315
+ this.name = 'BindingNotCallableError';
316
+ }
317
+ }
318
+
319
+ export class BindingSchemaMissingError extends ModuleError {
320
+ constructor(target: string, options?: { cause?: Error; traceId?: string }) {
321
+ super(
322
+ 'BINDING_SCHEMA_MISSING',
323
+ `No schema available for target '${target}'. Add type hints or provide an explicit schema.`,
324
+ { target },
325
+ options?.cause,
326
+ options?.traceId,
327
+ );
328
+ this.name = 'BindingSchemaMissingError';
329
+ }
330
+ }
331
+
332
+ export class BindingFileInvalidError extends ModuleError {
333
+ constructor(filePath: string, reason: string, options?: { cause?: Error; traceId?: string }) {
334
+ super(
335
+ 'BINDING_FILE_INVALID',
336
+ `Invalid binding file '${filePath}': ${reason}`,
337
+ { filePath, reason },
338
+ options?.cause,
339
+ options?.traceId,
340
+ );
341
+ this.name = 'BindingFileInvalidError';
342
+ }
343
+ }
344
+
345
+ export class CircularDependencyError extends ModuleError {
346
+ constructor(cyclePath: string[], options?: { cause?: Error; traceId?: string }) {
347
+ super(
348
+ 'CIRCULAR_DEPENDENCY',
349
+ `Circular dependency detected: ${cyclePath.join(' -> ')}`,
350
+ { cyclePath },
351
+ options?.cause,
352
+ options?.traceId,
353
+ );
354
+ this.name = 'CircularDependencyError';
355
+ }
356
+ }
357
+
358
+ export class ModuleLoadError extends ModuleError {
359
+ constructor(moduleId: string, reason: string, options?: { cause?: Error; traceId?: string }) {
360
+ super(
361
+ 'MODULE_LOAD_ERROR',
362
+ `Failed to load module '${moduleId}': ${reason}`,
363
+ { moduleId, reason },
364
+ options?.cause,
365
+ options?.traceId,
366
+ );
367
+ this.name = 'ModuleLoadError';
368
+ }
369
+ }
@@ -0,0 +1,348 @@
1
+ /**
2
+ * Executor and related utilities for apcore.
3
+ *
4
+ * Async-only execution pipeline. Python's call() + call_async() merge into one async call().
5
+ * Timeout uses Promise.race instead of threading.
6
+ */
7
+
8
+ import type { TSchema } from '@sinclair/typebox';
9
+ import { Value } from '@sinclair/typebox/value';
10
+ import type { ACL } from './acl.js';
11
+ import type { Config } from './config.js';
12
+ import { Context } from './context.js';
13
+ import {
14
+ ACLDeniedError,
15
+ CallDepthExceededError,
16
+ CallFrequencyExceededError,
17
+ CircularCallError,
18
+ InvalidInputError,
19
+ ModuleNotFoundError,
20
+ ModuleTimeoutError,
21
+ SchemaValidationError,
22
+ } from './errors.js';
23
+ import { AfterMiddleware, BeforeMiddleware, Middleware } from './middleware/index.js';
24
+ import { MiddlewareChainError, MiddlewareManager } from './middleware/manager.js';
25
+ import type { ValidationResult } from './module.js';
26
+ import type { Registry } from './registry/registry.js';
27
+
28
+ export const REDACTED_VALUE: string = '***REDACTED***';
29
+
30
+ export function redactSensitive(
31
+ data: Record<string, unknown>,
32
+ schemaDict: Record<string, unknown>,
33
+ ): Record<string, unknown> {
34
+ const redacted = JSON.parse(JSON.stringify(data));
35
+ redactFields(redacted, schemaDict);
36
+ redactSecretPrefix(redacted);
37
+ return redacted;
38
+ }
39
+
40
+ function redactFields(data: Record<string, unknown>, schemaDict: Record<string, unknown>): void {
41
+ const properties = schemaDict['properties'] as Record<string, Record<string, unknown>> | undefined;
42
+ if (!properties) return;
43
+
44
+ for (const [fieldName, fieldSchema] of Object.entries(properties)) {
45
+ if (!(fieldName in data)) continue;
46
+ const value = data[fieldName];
47
+
48
+ if (fieldSchema['x-sensitive'] === true) {
49
+ if (value !== null && value !== undefined) {
50
+ data[fieldName] = REDACTED_VALUE;
51
+ }
52
+ continue;
53
+ }
54
+
55
+ if (fieldSchema['type'] === 'object' && 'properties' in fieldSchema && typeof value === 'object' && value !== null && !Array.isArray(value)) {
56
+ redactFields(value as Record<string, unknown>, fieldSchema);
57
+ continue;
58
+ }
59
+
60
+ if (fieldSchema['type'] === 'array' && 'items' in fieldSchema && Array.isArray(value)) {
61
+ const itemsSchema = fieldSchema['items'] as Record<string, unknown>;
62
+ if (itemsSchema['x-sensitive'] === true) {
63
+ for (let i = 0; i < value.length; i++) {
64
+ if (value[i] !== null && value[i] !== undefined) {
65
+ value[i] = REDACTED_VALUE;
66
+ }
67
+ }
68
+ } else if (itemsSchema['type'] === 'object' && 'properties' in itemsSchema) {
69
+ for (const item of value) {
70
+ if (typeof item === 'object' && item !== null) {
71
+ redactFields(item as Record<string, unknown>, itemsSchema);
72
+ }
73
+ }
74
+ }
75
+ }
76
+ }
77
+ }
78
+
79
+ function redactSecretPrefix(data: Record<string, unknown>): void {
80
+ for (const key of Object.keys(data)) {
81
+ if (key.startsWith('_secret_') && data[key] !== null && data[key] !== undefined) {
82
+ data[key] = REDACTED_VALUE;
83
+ }
84
+ }
85
+ }
86
+
87
+ export class Executor {
88
+ private _registry: Registry;
89
+ private _middlewareManager: MiddlewareManager;
90
+ private _acl: ACL | null;
91
+ private _config: Config | null;
92
+ private _defaultTimeout: number;
93
+ private _globalTimeout: number;
94
+ private _maxCallDepth: number;
95
+ private _maxModuleRepeat: number;
96
+
97
+ constructor(options: {
98
+ registry: Registry;
99
+ middlewares?: Middleware[] | null;
100
+ acl?: ACL | null;
101
+ config?: Config | null;
102
+ }) {
103
+ this._registry = options.registry;
104
+ this._middlewareManager = new MiddlewareManager();
105
+ this._acl = options.acl ?? null;
106
+ this._config = options.config ?? null;
107
+
108
+ if (options.middlewares) {
109
+ for (const mw of options.middlewares) {
110
+ this._middlewareManager.add(mw);
111
+ }
112
+ }
113
+
114
+ if (this._config !== null) {
115
+ this._defaultTimeout = (this._config.get('executor.default_timeout') as number) ?? 30000;
116
+ this._globalTimeout = (this._config.get('executor.global_timeout') as number) ?? 60000;
117
+ this._maxCallDepth = (this._config.get('executor.max_call_depth') as number) ?? 32;
118
+ this._maxModuleRepeat = (this._config.get('executor.max_module_repeat') as number) ?? 3;
119
+ } else {
120
+ this._defaultTimeout = 30000;
121
+ this._globalTimeout = 60000;
122
+ this._maxCallDepth = 32;
123
+ this._maxModuleRepeat = 3;
124
+ }
125
+ }
126
+
127
+ get registry(): Registry {
128
+ return this._registry;
129
+ }
130
+
131
+ get middlewares(): Middleware[] {
132
+ return this._middlewareManager.snapshot();
133
+ }
134
+
135
+ use(middleware: Middleware): Executor {
136
+ this._middlewareManager.add(middleware);
137
+ return this;
138
+ }
139
+
140
+ useBefore(callback: (moduleId: string, inputs: Record<string, unknown>, context: Context) => Record<string, unknown> | null): Executor {
141
+ this._middlewareManager.add(new BeforeMiddleware(callback));
142
+ return this;
143
+ }
144
+
145
+ useAfter(callback: (moduleId: string, inputs: Record<string, unknown>, output: Record<string, unknown>, context: Context) => Record<string, unknown> | null): Executor {
146
+ this._middlewareManager.add(new AfterMiddleware(callback));
147
+ return this;
148
+ }
149
+
150
+ remove(middleware: Middleware): boolean {
151
+ return this._middlewareManager.remove(middleware);
152
+ }
153
+
154
+ async call(
155
+ moduleId: string,
156
+ inputs?: Record<string, unknown> | null,
157
+ context?: Context | null,
158
+ ): Promise<Record<string, unknown>> {
159
+ let effectiveInputs = inputs ?? {};
160
+
161
+ // Step 1 -- Context
162
+ let ctx: Context;
163
+ if (context == null) {
164
+ ctx = Context.create(this);
165
+ ctx = ctx.child(moduleId);
166
+ } else {
167
+ ctx = context.child(moduleId);
168
+ }
169
+
170
+ // Step 2 -- Safety Checks
171
+ this._checkSafety(moduleId, ctx);
172
+
173
+ // Step 3 -- Lookup
174
+ const module = this._registry.get(moduleId);
175
+ if (module === null) {
176
+ throw new ModuleNotFoundError(moduleId);
177
+ }
178
+
179
+ const mod = module as Record<string, unknown>;
180
+
181
+ // Step 4 -- ACL
182
+ if (this._acl !== null) {
183
+ const allowed = this._acl.check(ctx.callerId, moduleId, ctx);
184
+ if (!allowed) {
185
+ throw new ACLDeniedError(ctx.callerId, moduleId);
186
+ }
187
+ }
188
+
189
+ // Step 5 -- Input Validation and Redaction
190
+ const inputSchema = mod['inputSchema'] as TSchema | undefined;
191
+ if (inputSchema != null) {
192
+ if (!Value.Check(inputSchema, effectiveInputs)) {
193
+ const errors: Array<Record<string, unknown>> = [];
194
+ for (const error of Value.Errors(inputSchema, effectiveInputs)) {
195
+ errors.push({
196
+ field: error.path || '/',
197
+ code: String(error.type),
198
+ message: error.message,
199
+ });
200
+ }
201
+ throw new SchemaValidationError('Input validation failed', errors);
202
+ }
203
+
204
+ ctx.redactedInputs = redactSensitive(
205
+ effectiveInputs,
206
+ inputSchema as unknown as Record<string, unknown>,
207
+ );
208
+ }
209
+
210
+ let executedMiddlewares: Middleware[] = [];
211
+
212
+ try {
213
+ // Step 6 -- Middleware Before
214
+ try {
215
+ [effectiveInputs, executedMiddlewares] = this._middlewareManager.executeBefore(moduleId, effectiveInputs, ctx);
216
+ } catch (e) {
217
+ if (e instanceof MiddlewareChainError) {
218
+ executedMiddlewares = e.executedMiddlewares;
219
+ const recovery = this._middlewareManager.executeOnError(
220
+ moduleId, effectiveInputs, e.original, ctx, executedMiddlewares,
221
+ );
222
+ if (recovery !== null) return recovery;
223
+ executedMiddlewares = [];
224
+ throw e.original;
225
+ }
226
+ throw e;
227
+ }
228
+
229
+ // Step 7 -- Execute with timeout
230
+ let output = await this._executeWithTimeout(mod, moduleId, effectiveInputs, ctx);
231
+
232
+ // Step 8 -- Output Validation
233
+ const outputSchema = mod['outputSchema'] as TSchema | undefined;
234
+ if (outputSchema != null) {
235
+ if (!Value.Check(outputSchema, output)) {
236
+ const errors: Array<Record<string, unknown>> = [];
237
+ for (const error of Value.Errors(outputSchema, output)) {
238
+ errors.push({
239
+ field: error.path || '/',
240
+ code: String(error.type),
241
+ message: error.message,
242
+ });
243
+ }
244
+ throw new SchemaValidationError('Output validation failed', errors);
245
+ }
246
+ }
247
+
248
+ // Step 9 -- Middleware After
249
+ output = this._middlewareManager.executeAfter(moduleId, effectiveInputs, output, ctx);
250
+
251
+ // Step 10 -- Return
252
+ return output;
253
+ } catch (exc) {
254
+ if (executedMiddlewares.length > 0) {
255
+ const recovery = this._middlewareManager.executeOnError(
256
+ moduleId, effectiveInputs, exc as Error, ctx, executedMiddlewares,
257
+ );
258
+ if (recovery !== null) return recovery;
259
+ }
260
+ throw exc;
261
+ }
262
+ }
263
+
264
+ validate(moduleId: string, inputs: Record<string, unknown>): ValidationResult {
265
+ const module = this._registry.get(moduleId);
266
+ if (module === null) {
267
+ throw new ModuleNotFoundError(moduleId);
268
+ }
269
+
270
+ const mod = module as Record<string, unknown>;
271
+ const inputSchema = mod['inputSchema'] as TSchema | undefined;
272
+
273
+ if (inputSchema == null) {
274
+ return { valid: true, errors: [] };
275
+ }
276
+
277
+ if (Value.Check(inputSchema, inputs)) {
278
+ return { valid: true, errors: [] };
279
+ }
280
+
281
+ const errors: Array<Record<string, string>> = [];
282
+ for (const error of Value.Errors(inputSchema, inputs)) {
283
+ errors.push({
284
+ field: error.path || '/',
285
+ code: String(error.type),
286
+ message: error.message,
287
+ });
288
+ }
289
+ return { valid: false, errors };
290
+ }
291
+
292
+ private _checkSafety(moduleId: string, ctx: Context): void {
293
+ const callChain = ctx.callChain;
294
+
295
+ // Depth check
296
+ if (callChain.length > this._maxCallDepth) {
297
+ throw new CallDepthExceededError(callChain.length, this._maxCallDepth, [...callChain]);
298
+ }
299
+
300
+ // Circular detection (strict cycles of length >= 2)
301
+ const priorChain = callChain.slice(0, -1);
302
+ const lastIdx = priorChain.lastIndexOf(moduleId);
303
+ if (lastIdx !== -1) {
304
+ const subsequence = priorChain.slice(lastIdx + 1);
305
+ if (subsequence.length > 0) {
306
+ throw new CircularCallError(moduleId, [...callChain]);
307
+ }
308
+ }
309
+
310
+ // Frequency check
311
+ const count = callChain.filter((id) => id === moduleId).length;
312
+ if (count > this._maxModuleRepeat) {
313
+ throw new CallFrequencyExceededError(moduleId, count, this._maxModuleRepeat, [...callChain]);
314
+ }
315
+ }
316
+
317
+ private async _executeWithTimeout(
318
+ mod: Record<string, unknown>,
319
+ moduleId: string,
320
+ inputs: Record<string, unknown>,
321
+ ctx: Context,
322
+ ): Promise<Record<string, unknown>> {
323
+ const timeoutMs = this._defaultTimeout;
324
+
325
+ if (timeoutMs < 0) {
326
+ throw new InvalidInputError(`Negative timeout: ${timeoutMs}ms`);
327
+ }
328
+
329
+ const executeFn = mod['execute'] as (
330
+ inputs: Record<string, unknown>,
331
+ context: Context,
332
+ ) => Promise<Record<string, unknown>> | Record<string, unknown>;
333
+
334
+ const executionPromise = Promise.resolve(executeFn.call(mod, inputs, ctx));
335
+
336
+ if (timeoutMs === 0) {
337
+ return executionPromise;
338
+ }
339
+
340
+ const timeoutPromise = new Promise<never>((_, reject) => {
341
+ setTimeout(() => {
342
+ reject(new ModuleTimeoutError(moduleId, timeoutMs));
343
+ }, timeoutMs);
344
+ });
345
+
346
+ return Promise.race([executionPromise, timeoutPromise]);
347
+ }
348
+ }