@trentapps/manager-protocol 1.3.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/LICENSE +21 -0
- package/README.md +639 -0
- package/dist/analyzers/ArchitectureDetector.d.ts +44 -0
- package/dist/analyzers/ArchitectureDetector.d.ts.map +1 -0
- package/dist/analyzers/ArchitectureDetector.js +218 -0
- package/dist/analyzers/ArchitectureDetector.js.map +1 -0
- package/dist/analyzers/CSSAnalyzer.d.ts +284 -0
- package/dist/analyzers/CSSAnalyzer.d.ts.map +1 -0
- package/dist/analyzers/CSSAnalyzer.js +1180 -0
- package/dist/analyzers/CSSAnalyzer.js.map +1 -0
- package/dist/analyzers/index.d.ts +5 -0
- package/dist/analyzers/index.d.ts.map +1 -0
- package/dist/analyzers/index.js +5 -0
- package/dist/analyzers/index.js.map +1 -0
- package/dist/cli.d.ts +8 -0
- package/dist/cli.d.ts.map +1 -0
- package/dist/cli.js +174 -0
- package/dist/cli.js.map +1 -0
- package/dist/design-system/index.d.ts +6 -0
- package/dist/design-system/index.d.ts.map +1 -0
- package/dist/design-system/index.js +6 -0
- package/dist/design-system/index.js.map +1 -0
- package/dist/design-system/tokens.d.ts +106 -0
- package/dist/design-system/tokens.d.ts.map +1 -0
- package/dist/design-system/tokens.js +554 -0
- package/dist/design-system/tokens.js.map +1 -0
- package/dist/engine/AuditLogger.d.ts +506 -0
- package/dist/engine/AuditLogger.d.ts.map +1 -0
- package/dist/engine/AuditLogger.js +1491 -0
- package/dist/engine/AuditLogger.js.map +1 -0
- package/dist/engine/GitHubApprovalManager.d.ts +123 -0
- package/dist/engine/GitHubApprovalManager.d.ts.map +1 -0
- package/dist/engine/GitHubApprovalManager.js +347 -0
- package/dist/engine/GitHubApprovalManager.js.map +1 -0
- package/dist/engine/GitHubClient.d.ts +183 -0
- package/dist/engine/GitHubClient.d.ts.map +1 -0
- package/dist/engine/GitHubClient.js +411 -0
- package/dist/engine/GitHubClient.js.map +1 -0
- package/dist/engine/RateLimiter.d.ts +81 -0
- package/dist/engine/RateLimiter.d.ts.map +1 -0
- package/dist/engine/RateLimiter.js +215 -0
- package/dist/engine/RateLimiter.js.map +1 -0
- package/dist/engine/RuleDependencyAnalyzer.d.ts +73 -0
- package/dist/engine/RuleDependencyAnalyzer.d.ts.map +1 -0
- package/dist/engine/RuleDependencyAnalyzer.js +475 -0
- package/dist/engine/RuleDependencyAnalyzer.js.map +1 -0
- package/dist/engine/RulesEngine.d.ts +176 -0
- package/dist/engine/RulesEngine.d.ts.map +1 -0
- package/dist/engine/RulesEngine.js +705 -0
- package/dist/engine/RulesEngine.js.map +1 -0
- package/dist/engine/TaskManager.d.ts +174 -0
- package/dist/engine/TaskManager.d.ts.map +1 -0
- package/dist/engine/TaskManager.js +663 -0
- package/dist/engine/TaskManager.js.map +1 -0
- package/dist/engine/index.d.ts +11 -0
- package/dist/engine/index.d.ts.map +1 -0
- package/dist/engine/index.js +13 -0
- package/dist/engine/index.js.map +1 -0
- package/dist/index.d.ts +21 -0
- package/dist/index.d.ts.map +1 -0
- package/dist/index.js +29 -0
- package/dist/index.js.map +1 -0
- package/dist/rules/architecture.d.ts +9 -0
- package/dist/rules/architecture.d.ts.map +1 -0
- package/dist/rules/architecture.js +322 -0
- package/dist/rules/architecture.js.map +1 -0
- package/dist/rules/azure.d.ts +7 -0
- package/dist/rules/azure.d.ts.map +1 -0
- package/dist/rules/azure.js +136 -0
- package/dist/rules/azure.js.map +1 -0
- package/dist/rules/compliance.d.ts +9 -0
- package/dist/rules/compliance.d.ts.map +1 -0
- package/dist/rules/compliance.js +286 -0
- package/dist/rules/compliance.js.map +1 -0
- package/dist/rules/condition-optimizer.d.ts +151 -0
- package/dist/rules/condition-optimizer.d.ts.map +1 -0
- package/dist/rules/condition-optimizer.js +479 -0
- package/dist/rules/condition-optimizer.js.map +1 -0
- package/dist/rules/css.d.ts +10 -0
- package/dist/rules/css.d.ts.map +1 -0
- package/dist/rules/css.js +1777 -0
- package/dist/rules/css.js.map +1 -0
- package/dist/rules/field-standards.d.ts +1172 -0
- package/dist/rules/field-standards.d.ts.map +1 -0
- package/dist/rules/field-standards.js +908 -0
- package/dist/rules/field-standards.js.map +1 -0
- package/dist/rules/flask.d.ts +7 -0
- package/dist/rules/flask.d.ts.map +1 -0
- package/dist/rules/flask.js +142 -0
- package/dist/rules/flask.js.map +1 -0
- package/dist/rules/index.d.ts +827 -0
- package/dist/rules/index.d.ts.map +1 -0
- package/dist/rules/index.js +556 -0
- package/dist/rules/index.js.map +1 -0
- package/dist/rules/ml-ai.d.ts +7 -0
- package/dist/rules/ml-ai.d.ts.map +1 -0
- package/dist/rules/ml-ai.js +148 -0
- package/dist/rules/ml-ai.js.map +1 -0
- package/dist/rules/operational.d.ts +9 -0
- package/dist/rules/operational.d.ts.map +1 -0
- package/dist/rules/operational.js +318 -0
- package/dist/rules/operational.js.map +1 -0
- package/dist/rules/patterns.d.ts +568 -0
- package/dist/rules/patterns.d.ts.map +1 -0
- package/dist/rules/patterns.js +1359 -0
- package/dist/rules/patterns.js.map +1 -0
- package/dist/rules/security.d.ts +9 -0
- package/dist/rules/security.d.ts.map +1 -0
- package/dist/rules/security.js +848 -0
- package/dist/rules/security.js.map +1 -0
- package/dist/rules/shared-patterns.d.ts +268 -0
- package/dist/rules/shared-patterns.d.ts.map +1 -0
- package/dist/rules/shared-patterns.js +556 -0
- package/dist/rules/shared-patterns.js.map +1 -0
- package/dist/rules/storage.d.ts +13 -0
- package/dist/rules/storage.d.ts.map +1 -0
- package/dist/rules/storage.js +672 -0
- package/dist/rules/storage.js.map +1 -0
- package/dist/rules/stripe.d.ts +7 -0
- package/dist/rules/stripe.d.ts.map +1 -0
- package/dist/rules/stripe.js +133 -0
- package/dist/rules/stripe.js.map +1 -0
- package/dist/rules/testing.d.ts +7 -0
- package/dist/rules/testing.d.ts.map +1 -0
- package/dist/rules/testing.js +135 -0
- package/dist/rules/testing.js.map +1 -0
- package/dist/rules/ux.d.ts +9 -0
- package/dist/rules/ux.d.ts.map +1 -0
- package/dist/rules/ux.js +280 -0
- package/dist/rules/ux.js.map +1 -0
- package/dist/rules/websocket.d.ts +7 -0
- package/dist/rules/websocket.d.ts.map +1 -0
- package/dist/rules/websocket.js +128 -0
- package/dist/rules/websocket.js.map +1 -0
- package/dist/server.d.ts +43 -0
- package/dist/server.d.ts.map +1 -0
- package/dist/server.js +1967 -0
- package/dist/server.js.map +1 -0
- package/dist/supervisor/AgentSupervisor.d.ts +195 -0
- package/dist/supervisor/AgentSupervisor.d.ts.map +1 -0
- package/dist/supervisor/AgentSupervisor.js +569 -0
- package/dist/supervisor/AgentSupervisor.js.map +1 -0
- package/dist/supervisor/ManagedServerRegistry.d.ts +185 -0
- package/dist/supervisor/ManagedServerRegistry.d.ts.map +1 -0
- package/dist/supervisor/ManagedServerRegistry.js +729 -0
- package/dist/supervisor/ManagedServerRegistry.js.map +1 -0
- package/dist/supervisor/ProjectTracker.d.ts +210 -0
- package/dist/supervisor/ProjectTracker.d.ts.map +1 -0
- package/dist/supervisor/ProjectTracker.js +709 -0
- package/dist/supervisor/ProjectTracker.js.map +1 -0
- package/dist/supervisor/index.d.ts +6 -0
- package/dist/supervisor/index.d.ts.map +1 -0
- package/dist/supervisor/index.js +6 -0
- package/dist/supervisor/index.js.map +1 -0
- package/dist/testing/index.d.ts +11 -0
- package/dist/testing/index.d.ts.map +1 -0
- package/dist/testing/index.js +12 -0
- package/dist/testing/index.js.map +1 -0
- package/dist/testing/rule-tester.d.ts +217 -0
- package/dist/testing/rule-tester.d.ts.map +1 -0
- package/dist/testing/rule-tester.examples.d.ts +57 -0
- package/dist/testing/rule-tester.examples.d.ts.map +1 -0
- package/dist/testing/rule-tester.examples.js +375 -0
- package/dist/testing/rule-tester.examples.js.map +1 -0
- package/dist/testing/rule-tester.js +381 -0
- package/dist/testing/rule-tester.js.map +1 -0
- package/dist/testing/rule-validator.d.ts +141 -0
- package/dist/testing/rule-validator.d.ts.map +1 -0
- package/dist/testing/rule-validator.js +640 -0
- package/dist/testing/rule-validator.js.map +1 -0
- package/dist/types/index.d.ts +1282 -0
- package/dist/types/index.d.ts.map +1 -0
- package/dist/types/index.js +386 -0
- package/dist/types/index.js.map +1 -0
- package/dist/utils/errors.d.ts +86 -0
- package/dist/utils/errors.d.ts.map +1 -0
- package/dist/utils/errors.js +171 -0
- package/dist/utils/errors.js.map +1 -0
- package/dist/utils/index.d.ts +7 -0
- package/dist/utils/index.d.ts.map +1 -0
- package/dist/utils/index.js +7 -0
- package/dist/utils/index.js.map +1 -0
- package/dist/utils/rate-limiting.d.ts +268 -0
- package/dist/utils/rate-limiting.d.ts.map +1 -0
- package/dist/utils/rate-limiting.js +403 -0
- package/dist/utils/rate-limiting.js.map +1 -0
- package/dist/utils/shared.d.ts +306 -0
- package/dist/utils/shared.d.ts.map +1 -0
- package/dist/utils/shared.js +464 -0
- package/dist/utils/shared.js.map +1 -0
- package/dist/utils/shell.d.ts +22 -0
- package/dist/utils/shell.d.ts.map +1 -0
- package/dist/utils/shell.js +29 -0
- package/dist/utils/shell.js.map +1 -0
- package/package.json +67 -0
|
@@ -0,0 +1,81 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Enterprise Agent Supervisor - Rate Limiter
|
|
3
|
+
*
|
|
4
|
+
* Token bucket rate limiting with multiple scopes and configurations.
|
|
5
|
+
* Uses extracted utilities from src/utils/rate-limiting.ts for
|
|
6
|
+
* bucket key building and sliding window logic.
|
|
7
|
+
*/
|
|
8
|
+
import type { RateLimitConfig, RateLimitState, ActionCategory } from '../types/index.js';
|
|
9
|
+
export declare class RateLimiter {
|
|
10
|
+
private configs;
|
|
11
|
+
private buckets;
|
|
12
|
+
private lastCleanup;
|
|
13
|
+
private cleanupIntervalMs;
|
|
14
|
+
/**
|
|
15
|
+
* Register a rate limit configuration
|
|
16
|
+
*/
|
|
17
|
+
registerLimit(config: RateLimitConfig): void;
|
|
18
|
+
/**
|
|
19
|
+
* Register multiple rate limit configurations
|
|
20
|
+
*/
|
|
21
|
+
registerLimits(configs: RateLimitConfig[]): void;
|
|
22
|
+
/**
|
|
23
|
+
* Remove a rate limit configuration
|
|
24
|
+
*/
|
|
25
|
+
removeLimit(configId: string): boolean;
|
|
26
|
+
/**
|
|
27
|
+
* Check if an action should be rate limited
|
|
28
|
+
*/
|
|
29
|
+
checkLimit(params: {
|
|
30
|
+
agentId?: string;
|
|
31
|
+
sessionId?: string;
|
|
32
|
+
userId?: string;
|
|
33
|
+
actionCategory?: ActionCategory;
|
|
34
|
+
actionType?: string;
|
|
35
|
+
}): {
|
|
36
|
+
allowed: boolean;
|
|
37
|
+
limitId?: string;
|
|
38
|
+
state?: RateLimitState;
|
|
39
|
+
};
|
|
40
|
+
/**
|
|
41
|
+
* Record a request (consume from bucket)
|
|
42
|
+
*/
|
|
43
|
+
recordRequest(params: {
|
|
44
|
+
agentId?: string;
|
|
45
|
+
sessionId?: string;
|
|
46
|
+
userId?: string;
|
|
47
|
+
actionCategory?: ActionCategory;
|
|
48
|
+
actionType?: string;
|
|
49
|
+
}): void;
|
|
50
|
+
/**
|
|
51
|
+
* Get current state for a specific limit
|
|
52
|
+
*/
|
|
53
|
+
getState(configId: string, params: {
|
|
54
|
+
agentId?: string;
|
|
55
|
+
sessionId?: string;
|
|
56
|
+
userId?: string;
|
|
57
|
+
}): RateLimitState | null;
|
|
58
|
+
/**
|
|
59
|
+
* Get all registered configurations
|
|
60
|
+
*/
|
|
61
|
+
getConfigs(): RateLimitConfig[];
|
|
62
|
+
/**
|
|
63
|
+
* Clear all buckets (useful for testing)
|
|
64
|
+
*/
|
|
65
|
+
clearBuckets(): void;
|
|
66
|
+
/**
|
|
67
|
+
* Convert params to BucketIdentifiers for the utility function
|
|
68
|
+
*/
|
|
69
|
+
private toIdentifiers;
|
|
70
|
+
/**
|
|
71
|
+
* Get or create a bucket with SlidingWindow
|
|
72
|
+
*/
|
|
73
|
+
private getOrCreateBucket;
|
|
74
|
+
/**
|
|
75
|
+
* Clean up expired buckets to prevent memory leaks
|
|
76
|
+
* Removes buckets that are older than their window duration
|
|
77
|
+
*/
|
|
78
|
+
private cleanupExpiredBuckets;
|
|
79
|
+
}
|
|
80
|
+
export declare const rateLimiter: RateLimiter;
|
|
81
|
+
//# sourceMappingURL=RateLimiter.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"RateLimiter.d.ts","sourceRoot":"","sources":["../../src/engine/RateLimiter.ts"],"names":[],"mappings":"AAAA;;;;;;GAMG;AAEH,OAAO,KAAK,EAAE,eAAe,EAAE,cAAc,EAAE,cAAc,EAAE,MAAM,mBAAmB,CAAC;AAWzF,qBAAa,WAAW;IACtB,OAAO,CAAC,OAAO,CAA2C;IAC1D,OAAO,CAAC,OAAO,CAA2C;IAC1D,OAAO,CAAC,WAAW,CAAsB;IACzC,OAAO,CAAC,iBAAiB,CAAyB;IAElD;;OAEG;IACH,aAAa,CAAC,MAAM,EAAE,eAAe,GAAG,IAAI;IAI5C;;OAEG;IACH,cAAc,CAAC,OAAO,EAAE,eAAe,EAAE,GAAG,IAAI;IAMhD;;OAEG;IACH,WAAW,CAAC,QAAQ,EAAE,MAAM,GAAG,OAAO;IAItC;;OAEG;IACH,UAAU,CAAC,MAAM,EAAE;QACjB,OAAO,CAAC,EAAE,MAAM,CAAC;QACjB,SAAS,CAAC,EAAE,MAAM,CAAC;QACnB,MAAM,CAAC,EAAE,MAAM,CAAC;QAChB,cAAc,CAAC,EAAE,cAAc,CAAC;QAChC,UAAU,CAAC,EAAE,MAAM,CAAC;KACrB,GAAG;QAAE,OAAO,EAAE,OAAO,CAAC;QAAC,OAAO,CAAC,EAAE,MAAM,CAAC;QAAC,KAAK,CAAC,EAAE,cAAc,CAAA;KAAE;IAyDlE;;OAEG;IACH,aAAa,CAAC,MAAM,EAAE;QACpB,OAAO,CAAC,EAAE,MAAM,CAAC;QACjB,SAAS,CAAC,EAAE,MAAM,CAAC;QACnB,MAAM,CAAC,EAAE,MAAM,CAAC;QAChB,cAAc,CAAC,EAAE,cAAc,CAAC;QAChC,UAAU,CAAC,EAAE,MAAM,CAAC;KACrB,GAAG,IAAI;IAqBR;;OAEG;IACH,QAAQ,CAAC,QAAQ,EAAE,MAAM,EAAE,MAAM,EAAE;QACjC,OAAO,CAAC,EAAE,MAAM,CAAC;QACjB,SAAS,CAAC,EAAE,MAAM,CAAC;QACnB,MAAM,CAAC,EAAE,MAAM,CAAC;KACjB,GAAG,cAAc,GAAG,IAAI;IA8BzB;;OAEG;IACH,UAAU,IAAI,eAAe,EAAE;IAI/B;;OAEG;IACH,YAAY,IAAI,IAAI;IAIpB;;OAEG;IACH,OAAO,CAAC,aAAa;IAcrB;;OAEG;IACH,OAAO,CAAC,iBAAiB;IAmBzB;;;OAGG;IACH,OAAO,CAAC,qBAAqB;CAmC9B;AAGD,eAAO,MAAM,WAAW,aAAoB,CAAC"}
|
|
@@ -0,0 +1,215 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Enterprise Agent Supervisor - Rate Limiter
|
|
3
|
+
*
|
|
4
|
+
* Token bucket rate limiting with multiple scopes and configurations.
|
|
5
|
+
* Uses extracted utilities from src/utils/rate-limiting.ts for
|
|
6
|
+
* bucket key building and sliding window logic.
|
|
7
|
+
*/
|
|
8
|
+
import { buildBucketKey, SlidingWindow } from '../utils/rate-limiting.js';
|
|
9
|
+
export class RateLimiter {
|
|
10
|
+
configs = new Map();
|
|
11
|
+
buckets = new Map();
|
|
12
|
+
lastCleanup = Date.now();
|
|
13
|
+
cleanupIntervalMs = 5 * 60 * 1000; // Cleanup every 5 minutes
|
|
14
|
+
/**
|
|
15
|
+
* Register a rate limit configuration
|
|
16
|
+
*/
|
|
17
|
+
registerLimit(config) {
|
|
18
|
+
this.configs.set(config.id, config);
|
|
19
|
+
}
|
|
20
|
+
/**
|
|
21
|
+
* Register multiple rate limit configurations
|
|
22
|
+
*/
|
|
23
|
+
registerLimits(configs) {
|
|
24
|
+
for (const config of configs) {
|
|
25
|
+
this.registerLimit(config);
|
|
26
|
+
}
|
|
27
|
+
}
|
|
28
|
+
/**
|
|
29
|
+
* Remove a rate limit configuration
|
|
30
|
+
*/
|
|
31
|
+
removeLimit(configId) {
|
|
32
|
+
return this.configs.delete(configId);
|
|
33
|
+
}
|
|
34
|
+
/**
|
|
35
|
+
* Check if an action should be rate limited
|
|
36
|
+
*/
|
|
37
|
+
checkLimit(params) {
|
|
38
|
+
const now = Date.now();
|
|
39
|
+
// Periodically clean up expired buckets to prevent memory leaks
|
|
40
|
+
this.cleanupExpiredBuckets(now);
|
|
41
|
+
for (const [configId, config] of this.configs) {
|
|
42
|
+
if (!config.enabled)
|
|
43
|
+
continue;
|
|
44
|
+
// Check if action category matches (if specified)
|
|
45
|
+
if (config.actionCategories && params.actionCategory) {
|
|
46
|
+
if (!config.actionCategories.includes(params.actionCategory)) {
|
|
47
|
+
continue;
|
|
48
|
+
}
|
|
49
|
+
}
|
|
50
|
+
// Build bucket key using extracted utility
|
|
51
|
+
const identifiers = this.toIdentifiers(params);
|
|
52
|
+
const bucketKey = buildBucketKey(config.scope, identifiers, configId);
|
|
53
|
+
const bucket = this.getOrCreateBucket(bucketKey, config);
|
|
54
|
+
// Check if window can accept using SlidingWindow
|
|
55
|
+
if (!bucket.window.canAccept(now)) {
|
|
56
|
+
const stats = bucket.window.getStats(now);
|
|
57
|
+
return {
|
|
58
|
+
allowed: false,
|
|
59
|
+
limitId: configId,
|
|
60
|
+
state: {
|
|
61
|
+
key: bucketKey,
|
|
62
|
+
count: stats.count,
|
|
63
|
+
windowStart: stats.windowStart,
|
|
64
|
+
remaining: 0,
|
|
65
|
+
resetAt: stats.resetAtISO
|
|
66
|
+
}
|
|
67
|
+
};
|
|
68
|
+
}
|
|
69
|
+
// Check burst limit if configured
|
|
70
|
+
if (config.burstLimit && !bucket.window.canAcceptBurst(now)) {
|
|
71
|
+
const stats = bucket.window.getStats(now);
|
|
72
|
+
return {
|
|
73
|
+
allowed: false,
|
|
74
|
+
limitId: configId,
|
|
75
|
+
state: {
|
|
76
|
+
key: bucketKey,
|
|
77
|
+
count: stats.count,
|
|
78
|
+
windowStart: stats.windowStart,
|
|
79
|
+
remaining: 0,
|
|
80
|
+
resetAt: stats.resetAtISO
|
|
81
|
+
}
|
|
82
|
+
};
|
|
83
|
+
}
|
|
84
|
+
}
|
|
85
|
+
return { allowed: true };
|
|
86
|
+
}
|
|
87
|
+
/**
|
|
88
|
+
* Record a request (consume from bucket)
|
|
89
|
+
*/
|
|
90
|
+
recordRequest(params) {
|
|
91
|
+
const now = Date.now();
|
|
92
|
+
for (const [configId, config] of this.configs) {
|
|
93
|
+
if (!config.enabled)
|
|
94
|
+
continue;
|
|
95
|
+
if (config.actionCategories && params.actionCategory) {
|
|
96
|
+
if (!config.actionCategories.includes(params.actionCategory)) {
|
|
97
|
+
continue;
|
|
98
|
+
}
|
|
99
|
+
}
|
|
100
|
+
const identifiers = this.toIdentifiers(params);
|
|
101
|
+
const bucketKey = buildBucketKey(config.scope, identifiers, configId);
|
|
102
|
+
const bucket = this.getOrCreateBucket(bucketKey, config);
|
|
103
|
+
// Record the request in the sliding window
|
|
104
|
+
bucket.window.record(now);
|
|
105
|
+
}
|
|
106
|
+
}
|
|
107
|
+
/**
|
|
108
|
+
* Get current state for a specific limit
|
|
109
|
+
*/
|
|
110
|
+
getState(configId, params) {
|
|
111
|
+
const config = this.configs.get(configId);
|
|
112
|
+
if (!config)
|
|
113
|
+
return null;
|
|
114
|
+
const now = Date.now();
|
|
115
|
+
const identifiers = this.toIdentifiers(params);
|
|
116
|
+
const bucketKey = buildBucketKey(config.scope, identifiers, configId);
|
|
117
|
+
const bucket = this.buckets.get(bucketKey);
|
|
118
|
+
if (!bucket) {
|
|
119
|
+
return {
|
|
120
|
+
key: bucketKey,
|
|
121
|
+
count: 0,
|
|
122
|
+
windowStart: now,
|
|
123
|
+
remaining: config.maxRequests,
|
|
124
|
+
resetAt: new Date(now + config.windowMs).toISOString()
|
|
125
|
+
};
|
|
126
|
+
}
|
|
127
|
+
const stats = bucket.window.getStats(now);
|
|
128
|
+
return {
|
|
129
|
+
key: bucketKey,
|
|
130
|
+
count: stats.count,
|
|
131
|
+
windowStart: stats.windowStart,
|
|
132
|
+
remaining: stats.remaining,
|
|
133
|
+
resetAt: stats.resetAtISO
|
|
134
|
+
};
|
|
135
|
+
}
|
|
136
|
+
/**
|
|
137
|
+
* Get all registered configurations
|
|
138
|
+
*/
|
|
139
|
+
getConfigs() {
|
|
140
|
+
return Array.from(this.configs.values());
|
|
141
|
+
}
|
|
142
|
+
/**
|
|
143
|
+
* Clear all buckets (useful for testing)
|
|
144
|
+
*/
|
|
145
|
+
clearBuckets() {
|
|
146
|
+
this.buckets.clear();
|
|
147
|
+
}
|
|
148
|
+
/**
|
|
149
|
+
* Convert params to BucketIdentifiers for the utility function
|
|
150
|
+
*/
|
|
151
|
+
toIdentifiers(params) {
|
|
152
|
+
return {
|
|
153
|
+
agentId: params.agentId,
|
|
154
|
+
sessionId: params.sessionId,
|
|
155
|
+
userId: params.userId,
|
|
156
|
+
actionType: params.actionType
|
|
157
|
+
};
|
|
158
|
+
}
|
|
159
|
+
/**
|
|
160
|
+
* Get or create a bucket with SlidingWindow
|
|
161
|
+
*/
|
|
162
|
+
getOrCreateBucket(key, config) {
|
|
163
|
+
let bucket = this.buckets.get(key);
|
|
164
|
+
if (!bucket) {
|
|
165
|
+
bucket = {
|
|
166
|
+
key,
|
|
167
|
+
window: new SlidingWindow({
|
|
168
|
+
windowMs: config.windowMs,
|
|
169
|
+
maxRequests: config.maxRequests,
|
|
170
|
+
burstLimit: config.burstLimit,
|
|
171
|
+
algorithm: 'fixed' // Use fixed window for consistent behavior with original implementation
|
|
172
|
+
})
|
|
173
|
+
};
|
|
174
|
+
this.buckets.set(key, bucket);
|
|
175
|
+
}
|
|
176
|
+
return bucket;
|
|
177
|
+
}
|
|
178
|
+
/**
|
|
179
|
+
* Clean up expired buckets to prevent memory leaks
|
|
180
|
+
* Removes buckets that are older than their window duration
|
|
181
|
+
*/
|
|
182
|
+
cleanupExpiredBuckets(now) {
|
|
183
|
+
// Only cleanup every cleanupIntervalMs to avoid excessive work
|
|
184
|
+
if (now - this.lastCleanup < this.cleanupIntervalMs) {
|
|
185
|
+
return;
|
|
186
|
+
}
|
|
187
|
+
this.lastCleanup = now;
|
|
188
|
+
// Find the maximum window duration across all configs
|
|
189
|
+
let maxWindowMs = 60000; // Default 1 minute
|
|
190
|
+
for (const config of this.configs.values()) {
|
|
191
|
+
if (config.windowMs > maxWindowMs) {
|
|
192
|
+
maxWindowMs = config.windowMs;
|
|
193
|
+
}
|
|
194
|
+
}
|
|
195
|
+
// Remove buckets that are older than the maximum window
|
|
196
|
+
// This uses >= for consistency with window expiration checks
|
|
197
|
+
// A bucket is expired when: now - windowStart >= 2*maxWindowMs (2x safety margin)
|
|
198
|
+
let removedCount = 0;
|
|
199
|
+
for (const [key, bucket] of this.buckets.entries()) {
|
|
200
|
+
const state = bucket.window.getState();
|
|
201
|
+
// Off-by-one fix: use consistent >= comparison for boundary calculation
|
|
202
|
+
// Window boundary semantics: now - windowStart >= threshold means expired
|
|
203
|
+
if (now - state.windowStart >= maxWindowMs * 2) {
|
|
204
|
+
this.buckets.delete(key);
|
|
205
|
+
removedCount++;
|
|
206
|
+
}
|
|
207
|
+
}
|
|
208
|
+
if (removedCount > 0) {
|
|
209
|
+
console.log(`[RateLimiter] Cleaned up ${removedCount} expired buckets`);
|
|
210
|
+
}
|
|
211
|
+
}
|
|
212
|
+
}
|
|
213
|
+
// Export singleton instance
|
|
214
|
+
export const rateLimiter = new RateLimiter();
|
|
215
|
+
//# sourceMappingURL=RateLimiter.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"RateLimiter.js","sourceRoot":"","sources":["../../src/engine/RateLimiter.ts"],"names":[],"mappings":"AAAA;;;;;;GAMG;AAGH,OAAO,EAAE,cAAc,EAAE,aAAa,EAA4C,MAAM,2BAA2B,CAAC;AAUpH,MAAM,OAAO,WAAW;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CAAC;QAC1E,CAAC;IACH,CAAC;CACF;AAED,4BAA4B;AAC5B,MAAM,CAAC,MAAM,WAAW,GAAG,IAAI,WAAW,EAAE,CAAC"}
|
|
@@ -0,0 +1,73 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Enterprise Agent Supervisor - Rule Dependency Analyzer (Task #37)
|
|
3
|
+
*
|
|
4
|
+
* Analyzes and validates rule interdependencies, detects circular dependencies,
|
|
5
|
+
* and provides topologically sorted execution order.
|
|
6
|
+
*/
|
|
7
|
+
import type { BusinessRule, RuleDependencyGraph, DependencyValidationResult } from '../types/index.js';
|
|
8
|
+
export interface DependencyAnalyzerOptions {
|
|
9
|
+
/**
|
|
10
|
+
* Maximum allowed depth for dependency chains before warning
|
|
11
|
+
*/
|
|
12
|
+
maxDepthWarning?: number;
|
|
13
|
+
/**
|
|
14
|
+
* Whether to include disabled rules in analysis
|
|
15
|
+
*/
|
|
16
|
+
includeDisabled?: boolean;
|
|
17
|
+
}
|
|
18
|
+
export declare class RuleDependencyAnalyzer {
|
|
19
|
+
private options;
|
|
20
|
+
constructor(options?: DependencyAnalyzerOptions);
|
|
21
|
+
/**
|
|
22
|
+
* Build a complete dependency graph from a set of rules
|
|
23
|
+
*/
|
|
24
|
+
analyzeDependencies(rules: BusinessRule[]): RuleDependencyGraph;
|
|
25
|
+
/**
|
|
26
|
+
* Find all circular dependencies in the rule set
|
|
27
|
+
*/
|
|
28
|
+
findCircularDependencies(rules: BusinessRule[]): string[][];
|
|
29
|
+
/**
|
|
30
|
+
* Get topologically sorted execution order for rules
|
|
31
|
+
* Rules with dependencies come after their dependencies
|
|
32
|
+
*/
|
|
33
|
+
getExecutionOrder(rules: BusinessRule[]): string[];
|
|
34
|
+
/**
|
|
35
|
+
* Validate all dependencies in the rule set
|
|
36
|
+
*/
|
|
37
|
+
validateDependencies(rules: BusinessRule[]): DependencyValidationResult;
|
|
38
|
+
/**
|
|
39
|
+
* Get rules that depend on a specific rule (direct dependents)
|
|
40
|
+
*/
|
|
41
|
+
getDependents(ruleId: string, rules: BusinessRule[]): BusinessRule[];
|
|
42
|
+
/**
|
|
43
|
+
* Get all rules that a specific rule depends on (direct dependencies)
|
|
44
|
+
*/
|
|
45
|
+
getDependencies(ruleId: string, rules: BusinessRule[]): BusinessRule[];
|
|
46
|
+
/**
|
|
47
|
+
* Get all transitive dependencies for a rule
|
|
48
|
+
*/
|
|
49
|
+
getTransitiveDependencies(ruleId: string, rules: BusinessRule[]): BusinessRule[];
|
|
50
|
+
/**
|
|
51
|
+
* Get all rules that would be affected if a rule is disabled
|
|
52
|
+
*/
|
|
53
|
+
getAffectedByDisabling(ruleId: string, rules: BusinessRule[]): BusinessRule[];
|
|
54
|
+
/**
|
|
55
|
+
* Get rules sorted by execution order respecting dependencies
|
|
56
|
+
* This integrates with the RulesEngine for proper evaluation order
|
|
57
|
+
*/
|
|
58
|
+
getSortedRulesForExecution(rules: BusinessRule[]): BusinessRule[];
|
|
59
|
+
/**
|
|
60
|
+
* Calculate depth for each node in the dependency graph
|
|
61
|
+
*/
|
|
62
|
+
private calculateDepths;
|
|
63
|
+
/**
|
|
64
|
+
* Detect conflicts between currently active rules
|
|
65
|
+
*/
|
|
66
|
+
private detectActiveConflicts;
|
|
67
|
+
/**
|
|
68
|
+
* Find dependencies that reference non-existent rules
|
|
69
|
+
*/
|
|
70
|
+
private findOrphanedDependencies;
|
|
71
|
+
}
|
|
72
|
+
export declare const ruleDependencyAnalyzer: RuleDependencyAnalyzer;
|
|
73
|
+
//# sourceMappingURL=RuleDependencyAnalyzer.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"RuleDependencyAnalyzer.d.ts","sourceRoot":"","sources":["../../src/engine/RuleDependencyAnalyzer.ts"],"names":[],"mappings":"AAAA;;;;;GAKG;AAEH,OAAO,KAAK,EACV,YAAY,EACZ,mBAAmB,EAEnB,0BAA0B,EAC3B,MAAM,mBAAmB,CAAC;AAE3B,MAAM,WAAW,yBAAyB;I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