atabey 0.0.13 → 0.0.15
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/ATABEY.md +1 -1
- package/PRIVACY.md +8 -14
- package/README.md +405 -56
- package/bin/cli.js +72 -5
- package/dist/framework-mcp/src/index.js +988 -63
- package/dist/framework-mcp/src/index.js.map +1 -1
- package/dist/framework-mcp/src/tools/compliance/mask_pii.d.ts +17 -0
- package/dist/framework-mcp/src/tools/compliance/mask_pii.js +56 -0
- package/dist/framework-mcp/src/tools/compliance/mask_pii.js.map +1 -0
- package/dist/framework-mcp/src/tools/definitions.js +10 -9
- package/dist/framework-mcp/src/tools/definitions.js.map +1 -1
- package/dist/framework-mcp/src/tools/framework/run_tests.js +25 -0
- package/dist/framework-mcp/src/tools/framework/run_tests.js.map +1 -1
- package/dist/framework-mcp/src/tools/index.js +7 -8
- package/dist/framework-mcp/src/tools/index.js.map +1 -1
- package/dist/framework-mcp/src/tools/messaging/approve_operation.d.ts +22 -0
- package/dist/framework-mcp/src/tools/messaging/approve_operation.js +81 -0
- package/dist/framework-mcp/src/tools/messaging/approve_operation.js.map +1 -0
- package/dist/framework-mcp/src/tools/messaging/ask_human.d.ts +15 -0
- package/dist/framework-mcp/src/tools/messaging/ask_human.js +89 -20
- package/dist/framework-mcp/src/tools/messaging/ask_human.js.map +1 -1
- package/dist/framework-mcp/src/tools/observability/check_ports.d.ts +1 -0
- package/dist/framework-mcp/src/tools/observability/check_ports.js +24 -8
- package/dist/framework-mcp/src/tools/observability/check_ports.js.map +1 -1
- package/dist/framework-mcp/src/tools/quality/analyze_code_quality.js +13 -64
- package/dist/framework-mcp/src/tools/quality/analyze_code_quality.js.map +1 -1
- package/dist/framework-mcp/src/tools/schemas.d.ts +48 -53
- package/dist/framework-mcp/src/tools/schemas.js +18 -22
- package/dist/framework-mcp/src/tools/schemas.js.map +1 -1
- package/dist/framework-mcp/src/tools/shell/run_command.js +146 -90
- package/dist/framework-mcp/src/tools/shell/run_command.js.map +1 -1
- package/dist/framework-mcp/src/tools/types.d.ts +11 -105
- package/dist/framework-mcp/src/tools/types.js +1 -0
- package/dist/framework-mcp/src/tools/types.js.map +1 -1
- package/dist/framework-mcp/src/utils/auth.d.ts +36 -0
- package/dist/framework-mcp/src/utils/auth.js +79 -0
- package/dist/framework-mcp/src/utils/auth.js.map +1 -0
- package/dist/framework-mcp/src/utils/auto-rollback.d.ts +71 -0
- package/dist/framework-mcp/src/utils/auto-rollback.js +266 -0
- package/dist/framework-mcp/src/utils/auto-rollback.js.map +1 -0
- package/dist/framework-mcp/src/utils/compliance.js +12 -4
- package/dist/framework-mcp/src/utils/compliance.js.map +1 -1
- package/dist/framework-mcp/src/utils/context-optimizer.d.ts +69 -0
- package/dist/framework-mcp/src/utils/context-optimizer.js +157 -0
- package/dist/framework-mcp/src/utils/context-optimizer.js.map +1 -0
- package/dist/framework-mcp/src/utils/discipline.d.ts +68 -0
- package/dist/framework-mcp/src/utils/discipline.js +218 -0
- package/dist/framework-mcp/src/utils/discipline.js.map +1 -0
- package/dist/framework-mcp/src/utils/finops.d.ts +143 -0
- package/dist/framework-mcp/src/utils/finops.js +326 -0
- package/dist/framework-mcp/src/utils/finops.js.map +1 -0
- package/dist/framework-mcp/src/utils/human-in-loop.d.ts +95 -0
- package/dist/framework-mcp/src/utils/human-in-loop.js +216 -0
- package/dist/framework-mcp/src/utils/human-in-loop.js.map +1 -0
- package/dist/framework-mcp/src/utils/license-scanner.d.ts +71 -0
- package/dist/framework-mcp/src/utils/license-scanner.js +284 -0
- package/dist/framework-mcp/src/utils/license-scanner.js.map +1 -0
- package/dist/framework-mcp/src/utils/loop-detector.d.ts +105 -0
- package/dist/framework-mcp/src/utils/loop-detector.js +362 -0
- package/dist/framework-mcp/src/utils/loop-detector.js.map +1 -0
- package/dist/framework-mcp/src/utils/quality.d.ts +19 -0
- package/dist/framework-mcp/src/utils/quality.js +125 -0
- package/dist/framework-mcp/src/utils/quality.js.map +1 -0
- package/dist/framework-mcp/src/utils/rules-engine.d.ts +78 -0
- package/dist/framework-mcp/src/utils/rules-engine.js +232 -0
- package/dist/framework-mcp/src/utils/rules-engine.js.map +1 -0
- package/dist/framework-mcp/src/utils/silent-router.d.ts +49 -0
- package/dist/framework-mcp/src/utils/silent-router.js +155 -0
- package/dist/framework-mcp/src/utils/silent-router.js.map +1 -0
- package/dist/framework-mcp/src/utils/storage.d.ts +1 -0
- package/dist/framework-mcp/src/utils/storage.js +2 -2
- package/dist/framework-mcp/src/utils/storage.js.map +1 -1
- package/dist/framework-mcp/src/utils/telemetry-streamer.d.ts +133 -0
- package/dist/framework-mcp/src/utils/telemetry-streamer.js +397 -0
- package/dist/framework-mcp/src/utils/telemetry-streamer.js.map +1 -0
- package/dist/framework-mcp/tests/tools/file_system/file_system_tools.test.js +2 -2
- package/dist/framework-mcp/tests/tools/file_system/file_system_tools.test.js.map +1 -1
- package/dist/framework-mcp/tests/tools/framework/run_tests.test.d.ts +1 -0
- package/dist/framework-mcp/tests/tools/framework/run_tests.test.js +49 -0
- package/dist/framework-mcp/tests/tools/framework/run_tests.test.js.map +1 -0
- package/dist/framework-mcp/tests/tools/messaging/send_message.test.js +14 -5
- package/dist/framework-mcp/tests/tools/messaging/send_message.test.js.map +1 -1
- package/dist/framework-mcp/tests/tools/observability/check_ports.test.d.ts +1 -0
- package/dist/framework-mcp/tests/tools/observability/check_ports.test.js +44 -0
- package/dist/framework-mcp/tests/tools/observability/check_ports.test.js.map +1 -0
- package/dist/framework-mcp/tests/tools/shell/run_command.test.js +44 -14
- package/dist/framework-mcp/tests/tools/shell/run_command.test.js.map +1 -1
- package/dist/framework-mcp/tests/utils/auto-rollback.test.d.ts +7 -0
- package/dist/framework-mcp/tests/utils/auto-rollback.test.js +178 -0
- package/dist/framework-mcp/tests/utils/auto-rollback.test.js.map +1 -0
- package/dist/framework-mcp/tests/utils/context-optimizer.test.d.ts +1 -0
- package/dist/framework-mcp/tests/utils/context-optimizer.test.js +62 -0
- package/dist/framework-mcp/tests/utils/context-optimizer.test.js.map +1 -0
- package/dist/framework-mcp/tests/utils/finops.test.d.ts +7 -0
- package/dist/framework-mcp/tests/utils/finops.test.js +157 -0
- package/dist/framework-mcp/tests/utils/finops.test.js.map +1 -0
- package/dist/framework-mcp/tests/utils/human-in-loop.test.d.ts +1 -0
- package/dist/framework-mcp/tests/utils/human-in-loop.test.js +59 -0
- package/dist/framework-mcp/tests/utils/human-in-loop.test.js.map +1 -0
- package/dist/framework-mcp/tests/utils/license-scanner.test.d.ts +7 -0
- package/dist/framework-mcp/tests/utils/license-scanner.test.js +131 -0
- package/dist/framework-mcp/tests/utils/license-scanner.test.js.map +1 -0
- package/dist/framework-mcp/tests/utils/loop-detector.test.d.ts +7 -0
- package/dist/framework-mcp/tests/utils/loop-detector.test.js +204 -0
- package/dist/framework-mcp/tests/utils/loop-detector.test.js.map +1 -0
- package/dist/framework-mcp/tests/utils/rules-engine.test.d.ts +1 -0
- package/dist/framework-mcp/tests/utils/rules-engine.test.js +106 -0
- package/dist/framework-mcp/tests/utils/rules-engine.test.js.map +1 -0
- package/dist/framework-mcp/tests/utils/telemetry-streamer.test.d.ts +7 -0
- package/dist/framework-mcp/tests/utils/telemetry-streamer.test.js +201 -0
- package/dist/framework-mcp/tests/utils/telemetry-streamer.test.js.map +1 -0
- package/dist/src/cli/commands/check.js +16 -1
- package/dist/src/cli/commands/check.js.map +1 -1
- package/dist/src/cli/commands/dashboard.js +188 -164
- package/dist/src/cli/commands/dashboard.js.map +1 -1
- package/dist/src/cli/commands/gateway.js +2 -2
- package/dist/src/cli/commands/gateway.js.map +1 -1
- package/dist/src/cli/commands/init/scaffold-core.d.ts +2 -0
- package/dist/src/cli/commands/init/scaffold-core.js +16 -1
- package/dist/src/cli/commands/init/scaffold-core.js.map +1 -1
- package/dist/src/cli/commands/init/scaffold-ops.js +2 -2
- package/dist/src/cli/commands/init/scaffold-ops.js.map +1 -1
- package/dist/src/cli/commands/init/scaffold-standards.js +13 -1
- package/dist/src/cli/commands/init/scaffold-standards.js.map +1 -1
- package/dist/src/cli/commands/init.d.ts +4 -0
- package/dist/src/cli/commands/init.js +94 -31
- package/dist/src/cli/commands/init.js.map +1 -1
- package/dist/src/cli/commands/mcp.js +3 -1
- package/dist/src/cli/commands/mcp.js.map +1 -1
- package/dist/src/cli/commands/quickstart.d.ts +1 -1
- package/dist/src/cli/commands/quickstart.js +12 -24
- package/dist/src/cli/commands/quickstart.js.map +1 -1
- package/dist/src/cli/commands/script.js +8 -1
- package/dist/src/cli/commands/script.js.map +1 -1
- package/dist/src/cli/index.js +24 -23
- package/dist/src/cli/index.js.map +1 -1
- package/dist/src/cli/shims.js +7 -7
- package/dist/src/cli/utils/pkg.js +2 -3
- package/dist/src/cli/utils/pkg.js.map +1 -1
- package/dist/src/cli/utils/quality.d.ts +10 -0
- package/dist/src/cli/utils/quality.js +10 -0
- package/dist/src/cli/utils/quality.js.map +1 -0
- package/dist/src/modules/agents/definitions.js +45 -40
- package/dist/src/modules/agents/definitions.js.map +1 -1
- package/dist/src/modules/agents/registry/manager.js +6 -1
- package/dist/src/modules/agents/registry/manager.js.map +1 -1
- package/dist/src/modules/engines/agent-executor.d.ts +6 -10
- package/dist/src/modules/engines/agent-executor.js +63 -21
- package/dist/src/modules/engines/agent-executor.js.map +1 -1
- package/dist/src/modules/engines/agent-loop.d.ts +34 -0
- package/dist/src/modules/engines/agent-loop.js +102 -0
- package/dist/src/modules/engines/agent-loop.js.map +1 -0
- package/dist/src/modules/engines/evaluation-engine.d.ts +12 -2
- package/dist/src/modules/engines/evaluation-engine.js +78 -11
- package/dist/src/modules/engines/evaluation-engine.js.map +1 -1
- package/dist/src/modules/engines/quality-gate.d.ts +13 -5
- package/dist/src/modules/engines/quality-gate.js +39 -31
- package/dist/src/modules/engines/quality-gate.js.map +1 -1
- package/dist/src/modules/engines/routing-engine.d.ts +5 -3
- package/dist/src/modules/engines/routing-engine.js +16 -9
- package/dist/src/modules/engines/routing-engine.js.map +1 -1
- package/dist/src/modules/gateway/cost-tracker.d.ts +5 -0
- package/dist/src/modules/gateway/cost-tracker.js +71 -0
- package/dist/src/modules/gateway/cost-tracker.js.map +1 -1
- package/dist/src/modules/gateway/gateway.d.ts +4 -0
- package/dist/src/modules/gateway/gateway.js +8 -0
- package/dist/src/modules/gateway/gateway.js.map +1 -1
- package/dist/src/shared/audit.js +3 -2
- package/dist/src/shared/audit.js.map +1 -1
- package/dist/src/shared/pii.d.ts +41 -9
- package/dist/src/shared/pii.js +122 -31
- package/dist/src/shared/pii.js.map +1 -1
- package/dist/src/shared/storage.d.ts +62 -0
- package/dist/src/shared/storage.js +47 -7
- package/dist/src/shared/storage.js.map +1 -1
- package/dist/tests/embedding.test.d.ts +1 -0
- package/dist/tests/embedding.test.js +82 -0
- package/dist/tests/embedding.test.js.map +1 -0
- package/dist/tests/engines/evaluation-engine.test.d.ts +1 -0
- package/dist/tests/engines/evaluation-engine.test.js +71 -0
- package/dist/tests/engines/evaluation-engine.test.js.map +1 -0
- package/dist/tests/engines/planning-engine.test.d.ts +1 -0
- package/dist/tests/engines/planning-engine.test.js +100 -0
- package/dist/tests/engines/planning-engine.test.js.map +1 -0
- package/dist/tests/engines/quality-gate.test.d.ts +1 -0
- package/dist/tests/engines/quality-gate.test.js +59 -0
- package/dist/tests/engines/quality-gate.test.js.map +1 -0
- package/dist/tests/engines/risk-engine.test.d.ts +1 -0
- package/dist/tests/engines/risk-engine.test.js +80 -0
- package/dist/tests/engines/risk-engine.test.js.map +1 -0
- package/dist/tests/engines/routing-engine.test.d.ts +1 -0
- package/dist/tests/engines/routing-engine.test.js +78 -0
- package/dist/tests/engines/routing-engine.test.js.map +1 -0
- package/dist/tests/integration/hermes_locking.test.js +1 -0
- package/dist/tests/integration/hermes_locking.test.js.map +1 -1
- package/dist/tests/modules/engines/agent-executor.test.js +1 -1
- package/dist/tests/modules/engines/agent-executor.test.js.map +1 -1
- package/dist/tests/modules/gateway/cost-tracker.test.js +20 -1
- package/dist/tests/modules/gateway/cost-tracker.test.js.map +1 -1
- package/dist/tests/shared/lock.test.d.ts +1 -0
- package/dist/tests/shared/lock.test.js +95 -0
- package/dist/tests/shared/lock.test.js.map +1 -0
- package/dist/tests/shared/pii.test.d.ts +1 -0
- package/dist/tests/shared/pii.test.js +183 -0
- package/dist/tests/shared/pii.test.js.map +1 -0
- package/framework-mcp/README.md +307 -35
- package/framework-mcp/dist/dashboard/assets/index-B_rK57vi.js +1221 -0
- package/framework-mcp/dist/dashboard/index.html +1 -1
- package/framework-mcp/dist/framework-mcp/src/index.js +988 -63
- package/framework-mcp/dist/framework-mcp/src/tools/compliance/mask_pii.js +55 -0
- package/framework-mcp/dist/framework-mcp/src/tools/definitions.js +10 -9
- package/framework-mcp/dist/framework-mcp/src/tools/framework/run_tests.js +25 -0
- package/framework-mcp/dist/framework-mcp/src/tools/index.js +7 -8
- package/framework-mcp/dist/framework-mcp/src/tools/messaging/approve_operation.js +80 -0
- package/framework-mcp/dist/framework-mcp/src/tools/messaging/ask_human.js +89 -20
- package/framework-mcp/dist/framework-mcp/src/tools/observability/check_ports.js +24 -8
- package/framework-mcp/dist/framework-mcp/src/tools/quality/analyze_code_quality.js +13 -64
- package/framework-mcp/dist/framework-mcp/src/tools/schemas.js +18 -22
- package/framework-mcp/dist/framework-mcp/src/tools/shell/run_command.js +146 -90
- package/framework-mcp/dist/framework-mcp/src/tools/types.js +1 -0
- package/framework-mcp/dist/framework-mcp/src/utils/auth.js +78 -0
- package/framework-mcp/dist/framework-mcp/src/utils/auto-rollback.js +265 -0
- package/framework-mcp/dist/framework-mcp/src/utils/compliance.js +12 -4
- package/framework-mcp/dist/framework-mcp/src/utils/context-optimizer.js +156 -0
- package/framework-mcp/dist/framework-mcp/src/utils/discipline.js +217 -0
- package/framework-mcp/dist/framework-mcp/src/utils/finops.js +325 -0
- package/framework-mcp/dist/framework-mcp/src/utils/human-in-loop.js +215 -0
- package/framework-mcp/dist/framework-mcp/src/utils/license-scanner.js +283 -0
- package/framework-mcp/dist/framework-mcp/src/utils/loop-detector.js +361 -0
- package/framework-mcp/dist/framework-mcp/src/utils/quality.js +124 -0
- package/framework-mcp/dist/framework-mcp/src/utils/rules-engine.js +231 -0
- package/framework-mcp/dist/framework-mcp/src/utils/silent-router.js +154 -0
- package/framework-mcp/dist/framework-mcp/src/utils/storage.js +2 -2
- package/framework-mcp/dist/framework-mcp/src/utils/telemetry-streamer.js +396 -0
- package/framework-mcp/dist/src/cli/platforms/core.js +72 -0
- package/framework-mcp/dist/src/cli/platforms/index.js +5 -0
- package/framework-mcp/dist/src/cli/platforms/paths.js +101 -0
- package/framework-mcp/dist/src/cli/platforms/scaffold.js +72 -0
- package/framework-mcp/dist/src/cli/platforms/utils.js +75 -0
- package/framework-mcp/dist/src/cli/utils/claude.js +57 -0
- package/framework-mcp/dist/src/cli/utils/compliance.js +12 -0
- package/framework-mcp/dist/src/cli/utils/fs.js +138 -0
- package/framework-mcp/dist/src/cli/utils/pkg.js +282 -0
- package/framework-mcp/dist/src/cli/utils/quality.js +9 -0
- package/framework-mcp/dist/src/modules/agents/definitions.js +563 -0
- package/framework-mcp/dist/src/modules/agents/registry/analyst.js +39 -0
- package/framework-mcp/dist/src/modules/agents/registry/architect.js +42 -0
- package/framework-mcp/dist/src/modules/agents/registry/backend.js +50 -0
- package/framework-mcp/dist/src/modules/agents/registry/database.js +45 -0
- package/framework-mcp/dist/src/modules/agents/registry/devops.js +45 -0
- package/framework-mcp/dist/src/modules/agents/registry/explorer.js +37 -0
- package/framework-mcp/dist/src/modules/agents/registry/frontend.js +52 -0
- package/framework-mcp/dist/src/modules/agents/registry/git.js +36 -0
- package/framework-mcp/dist/src/modules/agents/registry/manager.js +64 -0
- package/framework-mcp/dist/src/modules/agents/registry/mobile.js +41 -0
- package/framework-mcp/dist/src/modules/agents/registry/native.js +41 -0
- package/framework-mcp/dist/src/modules/agents/registry/quality.js +43 -0
- package/framework-mcp/dist/src/modules/agents/registry/security.js +42 -0
- package/framework-mcp/dist/src/modules/agents/types.js +1 -0
- package/framework-mcp/dist/src/modules/engines/evaluation-engine.js +169 -0
- package/framework-mcp/dist/src/modules/engines/quality-gate.js +92 -0
- package/framework-mcp/dist/src/modules/engines/risk-engine.js +105 -0
- package/framework-mcp/dist/src/modules/engines/routing-engine.js +408 -0
- package/framework-mcp/dist/src/modules/engines/types.js +1 -0
- package/framework-mcp/dist/src/modules/providers/definitions.js +140 -0
- package/framework-mcp/dist/src/modules/providers/registry.js +18 -0
- package/framework-mcp/dist/src/modules/providers/shared.js +104 -0
- package/framework-mcp/dist/src/modules/providers/types.js +1 -0
- package/framework-mcp/dist/src/modules/skills/adapter-skills.js +449 -0
- package/framework-mcp/dist/src/modules/skills/definitions.js +70 -0
- package/framework-mcp/dist/src/shared/audit.js +220 -0
- package/framework-mcp/dist/src/shared/errors.js +68 -0
- package/framework-mcp/dist/src/shared/lock.js +159 -0
- package/framework-mcp/dist/src/shared/pii.js +122 -31
- package/framework-mcp/dist/src/shared/retention.js +170 -0
- package/framework-mcp/dist/src/shared/storage.js +47 -7
- package/framework-mcp/dist/src/shared/string.js +29 -0
- package/framework-mcp/package.json +8 -2
- package/mcp.json +12 -6
- package/package.json +11 -5
- package/templates/full/ATABEY_FULL.md +1 -1
- package/framework-mcp/dist/dashboard/assets/index-BAPGLiWE.js +0 -267
|
@@ -0,0 +1,283 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* ─── LICENSE / COPYRIGHT SCANNER ──────────────────────────────────
|
|
3
|
+
*
|
|
4
|
+
* Detects restrictive license blocks in AI-generated code before
|
|
5
|
+
* it reaches the developer's project. Prevents GPL, AGPL, and other
|
|
6
|
+
* copyleft license violations at the MCP middleware layer.
|
|
7
|
+
*
|
|
8
|
+
* Features:
|
|
9
|
+
* - Recognizes SPDX license identifiers
|
|
10
|
+
* - Detects known open-source license headers (GPL, AGPL, LGPL, MPL)
|
|
11
|
+
* - Checks against corporate allowlist/blocklist
|
|
12
|
+
* - Source code similarity detection for known OSS snippets
|
|
13
|
+
* - Warning vs. blocking based on license severity
|
|
14
|
+
*
|
|
15
|
+
* License Severity:
|
|
16
|
+
* CRITICAL: GPL-3.0, AGPL-3.0 (copyleft, automatic blocking)
|
|
17
|
+
* HIGH: GPL-2.0, LGPL-3.0, MPL-2.0 (file-level copyleft)
|
|
18
|
+
* MEDIUM: MIT, Apache-2.0, BSD (permissive, allowed with attribution)
|
|
19
|
+
* LOW: CC0, Unlicense (public domain, no restrictions)
|
|
20
|
+
*/
|
|
21
|
+
import path from "path";
|
|
22
|
+
// ─── Configuration ────────────────────────────────────────────────
|
|
23
|
+
const CONFIG = {
|
|
24
|
+
/** Enable license scanning */
|
|
25
|
+
ENABLED: process.env.ATABEY_LICENSE_SCAN !== "false",
|
|
26
|
+
/** Automatically block copyleft licenses */
|
|
27
|
+
BLOCK_COPYLEFT: process.env.ATABEY_BLOCK_COPYLEFT !== "false",
|
|
28
|
+
/** SPDX blocklist – these licenses are automatically blocked */
|
|
29
|
+
BLOCKLIST: (process.env.ATABEY_LICENSE_BLOCKLIST || "GPL-3.0,AGPL-3.0,GPL-2.0").split(",").map(l => l.trim()),
|
|
30
|
+
/** SPDX allowlist – these licenses are always allowed */
|
|
31
|
+
ALLOWLIST: (process.env.ATABEY_LICENSE_ALLOWLIST || "MIT,Apache-2.0,BSD-2-Clause,BSD-3-Clause,ISC,CC0-1.0,Unlicense").split(",").map(l => l.trim()),
|
|
32
|
+
/** Detect known OSS snippets by hash (requires snippet database) */
|
|
33
|
+
ENABLE_SNIPPET_DETECTION: process.env.ATABEY_LICENSE_SNIPPET_CHECK === "true",
|
|
34
|
+
/** Path to snippet hash database */
|
|
35
|
+
SNIPPET_DB_PATH: process.env.ATABEY_SNIPPET_DB_PATH || "",
|
|
36
|
+
};
|
|
37
|
+
const SPDX_LICENSES = {
|
|
38
|
+
// Critical – Copyleft
|
|
39
|
+
"GPL-3.0": { name: "GNU General Public License v3.0", severity: "CRITICAL", copyleft: true, requiresAttribution: true },
|
|
40
|
+
"GPL-3.0-only": { name: "GNU General Public License v3.0 only", severity: "CRITICAL", copyleft: true, requiresAttribution: true },
|
|
41
|
+
"GPL-3.0-or-later": { name: "GNU General Public License v3.0 or later", severity: "CRITICAL", copyleft: true, requiresAttribution: true },
|
|
42
|
+
"AGPL-3.0": { name: "GNU Affero General Public License v3.0", severity: "CRITICAL", copyleft: true, requiresAttribution: true },
|
|
43
|
+
"AGPL-3.0-only": { name: "GNU Affero General Public License v3.0 only", severity: "CRITICAL", copyleft: true, requiresAttribution: true },
|
|
44
|
+
"SSPL-1.0": { name: "Server Side Public License v1", severity: "CRITICAL", copyleft: true, requiresAttribution: true },
|
|
45
|
+
// High – File-level copyleft
|
|
46
|
+
"GPL-2.0": { name: "GNU General Public License v2.0", severity: "HIGH", copyleft: true, requiresAttribution: true },
|
|
47
|
+
"GPL-2.0-only": { name: "GNU General Public License v2.0 only", severity: "HIGH", copyleft: true, requiresAttribution: true },
|
|
48
|
+
"LGPL-3.0": { name: "GNU Lesser General Public License v3.0", severity: "HIGH", copyleft: true, requiresAttribution: true },
|
|
49
|
+
"LGPL-3.0-only": { name: "GNU Lesser General Public License v3.0 only", severity: "HIGH", copyleft: true, requiresAttribution: true },
|
|
50
|
+
"MPL-2.0": { name: "Mozilla Public License 2.0", severity: "HIGH", copyleft: true, requiresAttribution: true },
|
|
51
|
+
"EUPL-1.2": { name: "European Union Public License 1.2", severity: "HIGH", copyleft: true, requiresAttribution: true },
|
|
52
|
+
// Medium – Permissive
|
|
53
|
+
"MIT": { name: "MIT License", severity: "MEDIUM", copyleft: false, requiresAttribution: true },
|
|
54
|
+
"Apache-2.0": { name: "Apache License 2.0", severity: "MEDIUM", copyleft: false, requiresAttribution: true },
|
|
55
|
+
"BSD-2-Clause": { name: "BSD 2-Clause License", severity: "MEDIUM", copyleft: false, requiresAttribution: true },
|
|
56
|
+
"BSD-3-Clause": { name: "BSD 3-Clause License", severity: "MEDIUM", copyleft: false, requiresAttribution: true },
|
|
57
|
+
"ISC": { name: "ISC License", severity: "MEDIUM", copyleft: false, requiresAttribution: true },
|
|
58
|
+
// Low – Public domain
|
|
59
|
+
"CC0-1.0": { name: "Creative Commons Zero v1.0", severity: "LOW", copyleft: false, requiresAttribution: false },
|
|
60
|
+
"Unlicense": { name: "The Unlicense", severity: "LOW", copyleft: false, requiresAttribution: false },
|
|
61
|
+
};
|
|
62
|
+
// ─── License Header Patterns ──────────────────────────────────────
|
|
63
|
+
const LICENSE_HEADER_PATTERNS = [
|
|
64
|
+
{
|
|
65
|
+
name: "GNU General Public License v3.0",
|
|
66
|
+
spdxId: "GPL-3.0",
|
|
67
|
+
patterns: [
|
|
68
|
+
/GNU GENERAL PUBLIC LICENSE[\s\S]*Version 3/i,
|
|
69
|
+
/GNU General Public License v3/i,
|
|
70
|
+
/GPL-3\.0/,
|
|
71
|
+
],
|
|
72
|
+
},
|
|
73
|
+
{
|
|
74
|
+
name: "GNU Affero General Public License v3.0",
|
|
75
|
+
spdxId: "AGPL-3.0",
|
|
76
|
+
patterns: [
|
|
77
|
+
/GNU AFFERO GENERAL PUBLIC LICENSE/i,
|
|
78
|
+
/Affero General Public License/i,
|
|
79
|
+
/AGPL-3\.0/,
|
|
80
|
+
],
|
|
81
|
+
},
|
|
82
|
+
{
|
|
83
|
+
name: "GNU General Public License v2.0",
|
|
84
|
+
spdxId: "GPL-2.0",
|
|
85
|
+
patterns: [
|
|
86
|
+
/GNU GENERAL PUBLIC LICENSE[\s\S]*Version 2/i,
|
|
87
|
+
/GNU General Public License v2/i,
|
|
88
|
+
/GPL-2\.0/,
|
|
89
|
+
],
|
|
90
|
+
},
|
|
91
|
+
{
|
|
92
|
+
name: "MIT License",
|
|
93
|
+
spdxId: "MIT",
|
|
94
|
+
patterns: [
|
|
95
|
+
/MIT License/i,
|
|
96
|
+
/Permission is hereby granted, free of charge, to any person obtaining a copy/i,
|
|
97
|
+
],
|
|
98
|
+
},
|
|
99
|
+
{
|
|
100
|
+
name: "Apache License 2.0",
|
|
101
|
+
spdxId: "Apache-2.0",
|
|
102
|
+
patterns: [
|
|
103
|
+
/Apache License[\s\S]*Version 2\.0/i,
|
|
104
|
+
/Licensed under the Apache License, Version 2\.0/i,
|
|
105
|
+
/Apache-2\.0/,
|
|
106
|
+
],
|
|
107
|
+
},
|
|
108
|
+
{
|
|
109
|
+
name: "BSD 2-Clause License",
|
|
110
|
+
spdxId: "BSD-2-Clause",
|
|
111
|
+
patterns: [
|
|
112
|
+
/BSD 2-Clause/i,
|
|
113
|
+
/Redistributions of source code must retain the above copyright notice/i,
|
|
114
|
+
],
|
|
115
|
+
},
|
|
116
|
+
{
|
|
117
|
+
name: "BSD 3-Clause License",
|
|
118
|
+
spdxId: "BSD-3-Clause",
|
|
119
|
+
patterns: [
|
|
120
|
+
/BSD 3-Clause/i,
|
|
121
|
+
/BSD-3-Clause/,
|
|
122
|
+
/Neither the name of/i,
|
|
123
|
+
],
|
|
124
|
+
},
|
|
125
|
+
{
|
|
126
|
+
name: "Mozilla Public License 2.0",
|
|
127
|
+
spdxId: "MPL-2.0",
|
|
128
|
+
patterns: [
|
|
129
|
+
/Mozilla Public License/i,
|
|
130
|
+
/MPL-2\.0/,
|
|
131
|
+
],
|
|
132
|
+
},
|
|
133
|
+
];
|
|
134
|
+
// ─── Scanner Implementation ───────────────────────────────────────
|
|
135
|
+
/**
|
|
136
|
+
* Scan code content for license violations.
|
|
137
|
+
* Returns array of license matches found in the content.
|
|
138
|
+
*/
|
|
139
|
+
export function scanForLicenses(filePath, content) {
|
|
140
|
+
if (!CONFIG.ENABLED)
|
|
141
|
+
return [];
|
|
142
|
+
const ext = path.extname(filePath).toLowerCase();
|
|
143
|
+
// Only scan source files
|
|
144
|
+
if (![".ts", ".tsx", ".js", ".jsx", ".py", ".go", ".java", ".rb", ".php", ".c", ".cpp", ".h", ".hpp", ".rs", ".swift", ".kt"].includes(ext)) {
|
|
145
|
+
return [];
|
|
146
|
+
}
|
|
147
|
+
const matches = [];
|
|
148
|
+
const lines = content.split("\n");
|
|
149
|
+
// 1. Check for SPDX identifiers (e.g., `SPDX-License-Identifier: MIT`)
|
|
150
|
+
const spdxRegex = /SPDX-License-Identifier:\s*([^\s\n]+)/gi;
|
|
151
|
+
let spdxMatch;
|
|
152
|
+
while ((spdxMatch = spdxRegex.exec(content)) !== null) {
|
|
153
|
+
const spdxId = spdxMatch[1].trim();
|
|
154
|
+
const lineNo = getLineNumber(content, spdxMatch.index);
|
|
155
|
+
const licenseInfo = SPDX_LICENSES[spdxId];
|
|
156
|
+
if (licenseInfo) {
|
|
157
|
+
const blocked = CONFIG.BLOCKLIST.includes(spdxId) && CONFIG.BLOCK_COPYLEFT;
|
|
158
|
+
matches.push({
|
|
159
|
+
license: licenseInfo.name,
|
|
160
|
+
severity: licenseInfo.severity,
|
|
161
|
+
spdxId,
|
|
162
|
+
source: "spdx",
|
|
163
|
+
line: lineNo,
|
|
164
|
+
snippet: spdxMatch[0].substring(0, 80),
|
|
165
|
+
blocked,
|
|
166
|
+
requiresAttribution: licenseInfo.requiresAttribution,
|
|
167
|
+
});
|
|
168
|
+
}
|
|
169
|
+
else {
|
|
170
|
+
// Unknown SPDX identifier
|
|
171
|
+
matches.push({
|
|
172
|
+
license: `Unknown (${spdxId})`,
|
|
173
|
+
severity: "UNKNOWN",
|
|
174
|
+
spdxId,
|
|
175
|
+
source: "spdx",
|
|
176
|
+
line: lineNo,
|
|
177
|
+
snippet: spdxMatch[0].substring(0, 80),
|
|
178
|
+
blocked: CONFIG.BLOCK_COPYLEFT,
|
|
179
|
+
requiresAttribution: true,
|
|
180
|
+
});
|
|
181
|
+
}
|
|
182
|
+
}
|
|
183
|
+
// 2. Check for license header patterns in the first 50 lines
|
|
184
|
+
const headerRegion = lines.slice(0, 50).join("\n");
|
|
185
|
+
for (const header of LICENSE_HEADER_PATTERNS) {
|
|
186
|
+
for (const pattern of header.patterns) {
|
|
187
|
+
const headerMatch = headerRegion.match(pattern);
|
|
188
|
+
if (headerMatch) {
|
|
189
|
+
const lineNo = getLineNumber(headerRegion, headerMatch.index);
|
|
190
|
+
const licenseInfo = SPDX_LICENSES[header.spdxId] || {
|
|
191
|
+
name: header.name,
|
|
192
|
+
severity: "UNKNOWN",
|
|
193
|
+
copyleft: true,
|
|
194
|
+
requiresAttribution: true,
|
|
195
|
+
};
|
|
196
|
+
const blocked = CONFIG.BLOCKLIST.includes(header.spdxId) && CONFIG.BLOCK_COPYLEFT;
|
|
197
|
+
// Avoid duplicates from SPDX check
|
|
198
|
+
if (!matches.some(m => m.spdxId === header.spdxId && m.source === "header")) {
|
|
199
|
+
matches.push({
|
|
200
|
+
license: header.name,
|
|
201
|
+
severity: licenseInfo.severity,
|
|
202
|
+
spdxId: header.spdxId,
|
|
203
|
+
source: "header",
|
|
204
|
+
line: lineNo,
|
|
205
|
+
snippet: headerMatch[0].substring(0, 80),
|
|
206
|
+
blocked,
|
|
207
|
+
requiresAttribution: licenseInfo.requiresAttribution,
|
|
208
|
+
});
|
|
209
|
+
}
|
|
210
|
+
break;
|
|
211
|
+
}
|
|
212
|
+
}
|
|
213
|
+
}
|
|
214
|
+
return matches;
|
|
215
|
+
}
|
|
216
|
+
/**
|
|
217
|
+
* Validate content for license compliance before allowing file write.
|
|
218
|
+
* Returns error message if blocked, null if allowed.
|
|
219
|
+
*/
|
|
220
|
+
export function validateLicenseCompliance(filePath, content) {
|
|
221
|
+
const matches = scanForLicenses(filePath, content);
|
|
222
|
+
if (matches.length === 0)
|
|
223
|
+
return null;
|
|
224
|
+
// Check for blocked licenses
|
|
225
|
+
const blocked = matches.filter(m => m.blocked);
|
|
226
|
+
if (blocked.length > 0) {
|
|
227
|
+
const details = blocked.map(m => ` Line ${m.line}: ${m.license} (${m.spdxId}) [${m.severity}]`).join("\n");
|
|
228
|
+
return [
|
|
229
|
+
"[LICENSE] ⛔ Copyleft license detected – operation blocked",
|
|
230
|
+
"",
|
|
231
|
+
"The generated code contains copyleft-licensed content that",
|
|
232
|
+
"is incompatible with corporate IP policies:",
|
|
233
|
+
"",
|
|
234
|
+
details,
|
|
235
|
+
"",
|
|
236
|
+
`Blocked licenses: ${CONFIG.BLOCKLIST.join(", ")}`,
|
|
237
|
+
`Allowed licenses: ${CONFIG.ALLOWLIST.join(", ")}`,
|
|
238
|
+
"",
|
|
239
|
+
"Please rewrite this code without using copyleft-licensed sources.",
|
|
240
|
+
].join("\n");
|
|
241
|
+
}
|
|
242
|
+
// Check for licenses requiring attribution
|
|
243
|
+
const needsAttribution = matches.filter(m => m.requiresAttribution);
|
|
244
|
+
if (needsAttribution.length > 0) {
|
|
245
|
+
const details = needsAttribution.map(m => ` ${m.license} (${m.spdxId}) – found at line ${m.line}`).join("\n");
|
|
246
|
+
process.stderr.write(`[LICENSE] ⚠️ Attribution required for:\n${details}\n`);
|
|
247
|
+
// Don't block, just warn
|
|
248
|
+
}
|
|
249
|
+
return null;
|
|
250
|
+
}
|
|
251
|
+
/**
|
|
252
|
+
* Get the severity summary of a set of license matches.
|
|
253
|
+
*/
|
|
254
|
+
export function getLicenseSeveritySummary(matches) {
|
|
255
|
+
let highestSeverity = "LOW";
|
|
256
|
+
const severityOrder = ["CRITICAL", "HIGH", "MEDIUM", "LOW", "UNKNOWN"];
|
|
257
|
+
const uniqueLicenses = new Set();
|
|
258
|
+
for (const match of matches) {
|
|
259
|
+
uniqueLicenses.add(match.spdxId);
|
|
260
|
+
const currentIdx = severityOrder.indexOf(match.severity);
|
|
261
|
+
const highestIdx = severityOrder.indexOf(highestSeverity);
|
|
262
|
+
if (currentIdx < highestIdx || highestIdx === -1) {
|
|
263
|
+
highestSeverity = match.severity;
|
|
264
|
+
}
|
|
265
|
+
}
|
|
266
|
+
return {
|
|
267
|
+
hasBlocked: matches.some(m => m.blocked),
|
|
268
|
+
highestSeverity,
|
|
269
|
+
requiresAttribution: matches.some(m => m.requiresAttribution),
|
|
270
|
+
uniqueLicenses: Array.from(uniqueLicenses),
|
|
271
|
+
};
|
|
272
|
+
}
|
|
273
|
+
/**
|
|
274
|
+
* Get scanner config.
|
|
275
|
+
*/
|
|
276
|
+
export function getLicenseScannerConfig() {
|
|
277
|
+
return { ...CONFIG };
|
|
278
|
+
}
|
|
279
|
+
// ─── Helper ───────────────────────────────────────────────────────
|
|
280
|
+
function getLineNumber(content, index) {
|
|
281
|
+
return content.substring(0, index).split("\n").length;
|
|
282
|
+
}
|
|
283
|
+
export { CONFIG as LicenseScannerConfig };
|
|
@@ -0,0 +1,361 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* ─── LOOP DETECTOR ─────────────────────────────────────────────────
|
|
3
|
+
*
|
|
4
|
+
* Detects and prevents AI agents from entering infinite loops.
|
|
5
|
+
* Monitors call patterns across time, file paths, and tools.
|
|
6
|
+
*
|
|
7
|
+
* Detection Patterns:
|
|
8
|
+
* 1. **Consecutive Same Tool** – Same tool called N times in a row
|
|
9
|
+
* 2. **File Churn** – Same file written/read repeatedly
|
|
10
|
+
* 3. **Oscillation** – A → B → A → B alternating pattern
|
|
11
|
+
* 4. **Time-based Throttle** – Too many calls in a short window
|
|
12
|
+
* 5. **Semantic Similarity** – Similar arguments repeated (e.g., same error fix loop)
|
|
13
|
+
* 6. **Content Identity** – Same content being written repeatedly
|
|
14
|
+
*
|
|
15
|
+
* Action:
|
|
16
|
+
* - Cooldown: Block agent for N seconds
|
|
17
|
+
* - Escalate: Notify dashboard/human if loop persists
|
|
18
|
+
* - Kill: Force-stop the agent session
|
|
19
|
+
*
|
|
20
|
+
* Environment Variables:
|
|
21
|
+
* MCP_LOOP_MAX_CONSECUTIVE - Max same tool calls (default: 10)
|
|
22
|
+
* MCP_LOOP_MAX_FILE_CHURN - Max writes to same file (default: 5)
|
|
23
|
+
* MCP_LOOP_COOLDOWN_MS - Cooldown duration (default: 30000)
|
|
24
|
+
* MCP_LOOP_OSCILLATION_WINDOW - Oscillation detection window (default: 6)
|
|
25
|
+
*/
|
|
26
|
+
import path from "path";
|
|
27
|
+
// ─── Configuration ────────────────────────────────────────────────
|
|
28
|
+
const CONFIG = {
|
|
29
|
+
/** Max consecutive same tool calls before action */
|
|
30
|
+
MAX_CONSECUTIVE: parseInt(process.env.MCP_LOOP_MAX_CONSECUTIVE || "10", 10),
|
|
31
|
+
/** Max writes to the same file before action */
|
|
32
|
+
MAX_FILE_CHURN: parseInt(process.env.MCP_LOOP_MAX_FILE_CHURN || "5", 10),
|
|
33
|
+
/** Max reads of the same file before action */
|
|
34
|
+
MAX_FILE_READ_CHURN: parseInt(process.env.MCP_LOOP_MAX_FILE_READ_CHURN || "10", 10),
|
|
35
|
+
/** Oscillation pattern window (min calls to detect A→B→A→B) */
|
|
36
|
+
OSCILLATION_WINDOW: parseInt(process.env.MCP_LOOP_OSCILLATION_WINDOW || "6", 10),
|
|
37
|
+
/** Cooldown duration in ms */
|
|
38
|
+
COOLDOWN_MS: parseInt(process.env.MCP_LOOP_COOLDOWN_MS || "30000", 10),
|
|
39
|
+
/** Max calls to any tool in sliding window */
|
|
40
|
+
MAX_CALLS_IN_WINDOW: parseInt(process.env.MCP_LOOP_MAX_CALLS_IN_WINDOW || "50", 10),
|
|
41
|
+
/** Sliding window size in ms */
|
|
42
|
+
WINDOW_SIZE_MS: parseInt(process.env.MCP_LOOP_WINDOW_MS || "60000", 10),
|
|
43
|
+
/** Enable/disable loop detector */
|
|
44
|
+
ENABLED: process.env.MCP_LOOP_DETECTION !== "false",
|
|
45
|
+
};
|
|
46
|
+
const agentHistories = new Map();
|
|
47
|
+
// ─── Loop Detector ────────────────────────────────────────────────
|
|
48
|
+
/**
|
|
49
|
+
* Get or create agent history.
|
|
50
|
+
*/
|
|
51
|
+
function getOrCreateHistory(agent) {
|
|
52
|
+
if (!agentHistories.has(agent)) {
|
|
53
|
+
agentHistories.set(agent, {
|
|
54
|
+
calls: [],
|
|
55
|
+
consecutiveToolCalls: new Map(),
|
|
56
|
+
fileWriteCount: new Map(),
|
|
57
|
+
fileReadCount: new Map(),
|
|
58
|
+
lastContentHashes: [],
|
|
59
|
+
inCooldown: false,
|
|
60
|
+
cooldownUntil: 0,
|
|
61
|
+
cooldownCount: 0,
|
|
62
|
+
lastAlert: null,
|
|
63
|
+
});
|
|
64
|
+
}
|
|
65
|
+
return agentHistories.get(agent);
|
|
66
|
+
}
|
|
67
|
+
/**
|
|
68
|
+
* Prune old calls from history.
|
|
69
|
+
*/
|
|
70
|
+
function pruneOldCalls(history) {
|
|
71
|
+
const cutoff = Date.now() - CONFIG.WINDOW_SIZE_MS;
|
|
72
|
+
history.calls = history.calls.filter(c => c.timestamp > cutoff);
|
|
73
|
+
}
|
|
74
|
+
/**
|
|
75
|
+
* Extract file path from tool args if applicable.
|
|
76
|
+
*/
|
|
77
|
+
function extractFilePath(toolName, args) {
|
|
78
|
+
if (["write_file", "read_file", "replace_text", "patch_file", "batch_surgical_edit"].includes(toolName)) {
|
|
79
|
+
return args.path || args.filePath || undefined;
|
|
80
|
+
}
|
|
81
|
+
return undefined;
|
|
82
|
+
}
|
|
83
|
+
/**
|
|
84
|
+
* Compute a simple content hash for identity detection.
|
|
85
|
+
*/
|
|
86
|
+
function computeContentHash(args) {
|
|
87
|
+
if (args.content && typeof args.content === "string") {
|
|
88
|
+
// Use first 100 chars as hash (fast, good enough for identity)
|
|
89
|
+
return args.content.substring(0, 100);
|
|
90
|
+
}
|
|
91
|
+
return undefined;
|
|
92
|
+
}
|
|
93
|
+
/**
|
|
94
|
+
* Check for oscillation pattern (A → B → A → B).
|
|
95
|
+
*/
|
|
96
|
+
function detectOscillation(history, currentTool) {
|
|
97
|
+
const recent = history.calls.slice(-CONFIG.OSCILLATION_WINDOW);
|
|
98
|
+
if (recent.length < 4)
|
|
99
|
+
return false;
|
|
100
|
+
const recentToolNames = [...recent.map(c => c.toolName), currentTool];
|
|
101
|
+
// Check alternating pattern: tool[0] === tool[2] === tool[4] && tool[1] === tool[3] === tool[5]
|
|
102
|
+
if (recentToolNames.length >= 4) {
|
|
103
|
+
if (recentToolNames[0] === recentToolNames[2] &&
|
|
104
|
+
recentToolNames[1] === recentToolNames[3] &&
|
|
105
|
+
recentToolNames[0] !== recentToolNames[1]) {
|
|
106
|
+
// Check if pattern extends to 6
|
|
107
|
+
if (recentToolNames.length >= 6) {
|
|
108
|
+
return recentToolNames[0] === recentToolNames[4] && recentToolNames[1] === recentToolNames[5];
|
|
109
|
+
}
|
|
110
|
+
return true;
|
|
111
|
+
}
|
|
112
|
+
}
|
|
113
|
+
return false;
|
|
114
|
+
}
|
|
115
|
+
/**
|
|
116
|
+
* Detect if same content is being written repeatedly.
|
|
117
|
+
*/
|
|
118
|
+
function detectContentIdentity(history, contentHash) {
|
|
119
|
+
if (!contentHash)
|
|
120
|
+
return false;
|
|
121
|
+
const recentHashes = history.lastContentHashes.slice(-5);
|
|
122
|
+
if (recentHashes.length < 3)
|
|
123
|
+
return false;
|
|
124
|
+
// Check if this hash appears 3+ times in last 5
|
|
125
|
+
const hashCount = recentHashes.filter(h => h === contentHash).length;
|
|
126
|
+
if (contentHash === recentHashes[recentHashes.length - 1]) {
|
|
127
|
+
// Count including current
|
|
128
|
+
return hashCount + 1 >= 3;
|
|
129
|
+
}
|
|
130
|
+
return false;
|
|
131
|
+
}
|
|
132
|
+
/**
|
|
133
|
+
* Record a tool call and check for loop patterns.
|
|
134
|
+
* Returns LoopAlert if a loop pattern is detected, null otherwise.
|
|
135
|
+
*/
|
|
136
|
+
export function recordAndCheck(agent, toolName, args) {
|
|
137
|
+
if (!CONFIG.ENABLED)
|
|
138
|
+
return null;
|
|
139
|
+
const history = getOrCreateHistory(agent);
|
|
140
|
+
// Check cooldown
|
|
141
|
+
if (history.inCooldown) {
|
|
142
|
+
if (Date.now() < history.cooldownUntil) {
|
|
143
|
+
return {
|
|
144
|
+
type: "rate_limit",
|
|
145
|
+
severity: "critical",
|
|
146
|
+
agent,
|
|
147
|
+
detail: `Agent "${agent}" is in cooldown for ${Math.ceil((history.cooldownUntil - Date.now()) / 1000)}s. Loop detected earlier.`,
|
|
148
|
+
timestamp: Date.now(),
|
|
149
|
+
cooldownUntil: history.cooldownUntil,
|
|
150
|
+
};
|
|
151
|
+
}
|
|
152
|
+
else {
|
|
153
|
+
history.inCooldown = false;
|
|
154
|
+
}
|
|
155
|
+
}
|
|
156
|
+
const filePath = extractFilePath(toolName, args);
|
|
157
|
+
const contentHash = computeContentHash(args);
|
|
158
|
+
const record = {
|
|
159
|
+
agent,
|
|
160
|
+
toolName,
|
|
161
|
+
args,
|
|
162
|
+
timestamp: Date.now(),
|
|
163
|
+
filePath,
|
|
164
|
+
contentHash,
|
|
165
|
+
};
|
|
166
|
+
// Prune old calls
|
|
167
|
+
pruneOldCalls(history);
|
|
168
|
+
// Check 1: Consecutive same tool
|
|
169
|
+
const currentConsecutive = (history.consecutiveToolCalls.get(toolName) || 0) + 1;
|
|
170
|
+
history.consecutiveToolCalls.set(toolName, currentConsecutive);
|
|
171
|
+
// Reset other tools
|
|
172
|
+
for (const [tool] of history.consecutiveToolCalls) {
|
|
173
|
+
if (tool !== toolName) {
|
|
174
|
+
history.consecutiveToolCalls.set(tool, 0);
|
|
175
|
+
}
|
|
176
|
+
}
|
|
177
|
+
if (currentConsecutive >= CONFIG.MAX_CONSECUTIVE) {
|
|
178
|
+
const alert = {
|
|
179
|
+
type: "consecutive_same_tool",
|
|
180
|
+
severity: "critical",
|
|
181
|
+
agent,
|
|
182
|
+
detail: `Agent "${agent}" called "${toolName}" ${currentConsecutive} times consecutively. Possible infinite loop.`,
|
|
183
|
+
timestamp: Date.now(),
|
|
184
|
+
cooldownUntil: Date.now() + CONFIG.COOLDOWN_MS,
|
|
185
|
+
};
|
|
186
|
+
applyCooldown(history, alert, agent);
|
|
187
|
+
return alert;
|
|
188
|
+
}
|
|
189
|
+
// Check 2: File churn (for write operations)
|
|
190
|
+
if (filePath) {
|
|
191
|
+
if (["write_file", "replace_text", "patch_file", "batch_surgical_edit"].includes(toolName)) {
|
|
192
|
+
const writeCount = (history.fileWriteCount.get(filePath) || 0) + 1;
|
|
193
|
+
history.fileWriteCount.set(filePath, writeCount);
|
|
194
|
+
if (writeCount >= CONFIG.MAX_FILE_CHURN) {
|
|
195
|
+
const alert = {
|
|
196
|
+
type: "file_churn",
|
|
197
|
+
severity: "critical",
|
|
198
|
+
agent,
|
|
199
|
+
detail: `Agent "${agent}" wrote to "${path.relative(process.cwd(), filePath)}" ${writeCount} times. Possible file churn loop.`,
|
|
200
|
+
timestamp: Date.now(),
|
|
201
|
+
cooldownUntil: Date.now() + CONFIG.COOLDOWN_MS,
|
|
202
|
+
};
|
|
203
|
+
applyCooldown(history, alert, agent);
|
|
204
|
+
return alert;
|
|
205
|
+
}
|
|
206
|
+
}
|
|
207
|
+
if (toolName === "read_file") {
|
|
208
|
+
const readCount = (history.fileReadCount.get(filePath) || 0) + 1;
|
|
209
|
+
history.fileReadCount.set(filePath, readCount);
|
|
210
|
+
if (readCount >= CONFIG.MAX_FILE_READ_CHURN) {
|
|
211
|
+
const alert = {
|
|
212
|
+
type: "file_churn",
|
|
213
|
+
severity: "warning",
|
|
214
|
+
agent,
|
|
215
|
+
detail: `Agent "${agent}" read "${path.relative(process.cwd(), filePath)}" ${readCount} times. Possible read loop.`,
|
|
216
|
+
timestamp: Date.now(),
|
|
217
|
+
cooldownUntil: null,
|
|
218
|
+
};
|
|
219
|
+
history.lastAlert = alert;
|
|
220
|
+
return alert;
|
|
221
|
+
}
|
|
222
|
+
}
|
|
223
|
+
}
|
|
224
|
+
// Check 3: Oscillation pattern
|
|
225
|
+
if (detectOscillation(history, toolName)) {
|
|
226
|
+
const alert = {
|
|
227
|
+
type: "oscillation",
|
|
228
|
+
severity: "critical",
|
|
229
|
+
agent,
|
|
230
|
+
detail: `Agent "${agent}" is oscillating between tools. Pattern detected in last ${CONFIG.OSCILLATION_WINDOW} calls.`,
|
|
231
|
+
timestamp: Date.now(),
|
|
232
|
+
cooldownUntil: Date.now() + CONFIG.COOLDOWN_MS,
|
|
233
|
+
};
|
|
234
|
+
applyCooldown(history, alert, agent);
|
|
235
|
+
return alert;
|
|
236
|
+
}
|
|
237
|
+
// Check 4: Rate limit (calls in sliding window)
|
|
238
|
+
if (history.calls.length >= CONFIG.MAX_CALLS_IN_WINDOW) {
|
|
239
|
+
const alert = {
|
|
240
|
+
type: "rate_limit",
|
|
241
|
+
severity: "critical",
|
|
242
|
+
agent,
|
|
243
|
+
detail: `Agent "${agent}" made ${history.calls.length} calls in ${CONFIG.WINDOW_SIZE_MS / 1000}s window. Rate limit exceeded.`,
|
|
244
|
+
timestamp: Date.now(),
|
|
245
|
+
cooldownUntil: Date.now() + CONFIG.COOLDOWN_MS,
|
|
246
|
+
};
|
|
247
|
+
applyCooldown(history, alert, agent);
|
|
248
|
+
return alert;
|
|
249
|
+
}
|
|
250
|
+
// Check 5: Content identity (writing same content)
|
|
251
|
+
if (contentHash && detectContentIdentity(history, contentHash)) {
|
|
252
|
+
const alert = {
|
|
253
|
+
type: "content_identity",
|
|
254
|
+
severity: "critical",
|
|
255
|
+
agent,
|
|
256
|
+
detail: `Agent "${agent}" is writing the same content repeatedly. Possible fix-attempt loop.`,
|
|
257
|
+
timestamp: Date.now(),
|
|
258
|
+
cooldownUntil: Date.now() + CONFIG.COOLDOWN_MS,
|
|
259
|
+
};
|
|
260
|
+
applyCooldown(history, alert, agent);
|
|
261
|
+
return alert;
|
|
262
|
+
}
|
|
263
|
+
// If no detection, record the call
|
|
264
|
+
history.calls.push(record);
|
|
265
|
+
// Track content hashes for identity detection
|
|
266
|
+
if (contentHash) {
|
|
267
|
+
history.lastContentHashes.push(contentHash);
|
|
268
|
+
if (history.lastContentHashes.length > 10) {
|
|
269
|
+
history.lastContentHashes.shift();
|
|
270
|
+
}
|
|
271
|
+
}
|
|
272
|
+
return null;
|
|
273
|
+
}
|
|
274
|
+
/**
|
|
275
|
+
* Apply cooldown to an agent.
|
|
276
|
+
*/
|
|
277
|
+
function applyCooldown(history, alert, _agent) {
|
|
278
|
+
history.inCooldown = true;
|
|
279
|
+
history.cooldownUntil = alert.cooldownUntil || Date.now() + CONFIG.COOLDOWN_MS;
|
|
280
|
+
history.cooldownCount++;
|
|
281
|
+
history.lastAlert = alert;
|
|
282
|
+
process.stderr.write(`[LOOP DETECT] ${alert.type}: ${alert.detail}\n`);
|
|
283
|
+
}
|
|
284
|
+
/**
|
|
285
|
+
* Check if an agent is currently in cooldown.
|
|
286
|
+
*/
|
|
287
|
+
export function isInCooldown(agent) {
|
|
288
|
+
const history = agentHistories.get(agent);
|
|
289
|
+
if (!history || !history.inCooldown) {
|
|
290
|
+
return { inCooldown: false, remainingMs: 0, reason: null };
|
|
291
|
+
}
|
|
292
|
+
const remaining = history.cooldownUntil - Date.now();
|
|
293
|
+
if (remaining <= 0) {
|
|
294
|
+
history.inCooldown = false;
|
|
295
|
+
return { inCooldown: false, remainingMs: 0, reason: null };
|
|
296
|
+
}
|
|
297
|
+
return {
|
|
298
|
+
inCooldown: true,
|
|
299
|
+
remainingMs: remaining,
|
|
300
|
+
reason: history.lastAlert?.detail || null,
|
|
301
|
+
};
|
|
302
|
+
}
|
|
303
|
+
/**
|
|
304
|
+
* Manually clear cooldown for an agent.
|
|
305
|
+
*/
|
|
306
|
+
export function clearCooldown(agent) {
|
|
307
|
+
const history = agentHistories.get(agent);
|
|
308
|
+
if (history) {
|
|
309
|
+
history.inCooldown = false;
|
|
310
|
+
history.cooldownUntil = 0;
|
|
311
|
+
history.consecutiveToolCalls.clear();
|
|
312
|
+
return true;
|
|
313
|
+
}
|
|
314
|
+
return false;
|
|
315
|
+
}
|
|
316
|
+
/**
|
|
317
|
+
* Get loop detection stats for an agent.
|
|
318
|
+
*/
|
|
319
|
+
export function getLoopStats(agent) {
|
|
320
|
+
const history = agentHistories.get(agent);
|
|
321
|
+
if (!history) {
|
|
322
|
+
return {
|
|
323
|
+
totalCalls: 0,
|
|
324
|
+
recentCalls: 0,
|
|
325
|
+
inCooldown: false,
|
|
326
|
+
cooldownCount: 0,
|
|
327
|
+
consecutiveTools: {},
|
|
328
|
+
fileWrites: {},
|
|
329
|
+
fileReads: {},
|
|
330
|
+
lastAlert: null,
|
|
331
|
+
};
|
|
332
|
+
}
|
|
333
|
+
pruneOldCalls(history);
|
|
334
|
+
return {
|
|
335
|
+
totalCalls: history.calls.length + history.cooldownCount * CONFIG.MAX_CALLS_IN_WINDOW,
|
|
336
|
+
recentCalls: history.calls.length,
|
|
337
|
+
inCooldown: history.inCooldown && Date.now() < history.cooldownUntil,
|
|
338
|
+
cooldownCount: history.cooldownCount,
|
|
339
|
+
consecutiveTools: Object.fromEntries(history.consecutiveToolCalls),
|
|
340
|
+
fileWrites: Object.fromEntries(history.fileWriteCount),
|
|
341
|
+
fileReads: Object.fromEntries(history.fileReadCount),
|
|
342
|
+
lastAlert: history.lastAlert,
|
|
343
|
+
};
|
|
344
|
+
}
|
|
345
|
+
/**
|
|
346
|
+
* Get all agents' loop stats.
|
|
347
|
+
*/
|
|
348
|
+
export function getAllLoopStats() {
|
|
349
|
+
const stats = {};
|
|
350
|
+
for (const [agentName] of agentHistories) {
|
|
351
|
+
stats[agentName] = getLoopStats(agentName);
|
|
352
|
+
}
|
|
353
|
+
return stats;
|
|
354
|
+
}
|
|
355
|
+
/**
|
|
356
|
+
* Reset all agent histories.
|
|
357
|
+
*/
|
|
358
|
+
export function resetLoopDetection() {
|
|
359
|
+
agentHistories.clear();
|
|
360
|
+
}
|
|
361
|
+
export { CONFIG as LoopDetectorConfig };
|