atabey 0.0.13 → 0.0.14

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 (271) hide show
  1. package/ATABEY.md +1 -1
  2. package/README.md +252 -52
  3. package/dist/framework-mcp/src/index.js +914 -62
  4. package/dist/framework-mcp/src/index.js.map +1 -1
  5. package/dist/framework-mcp/src/tools/compliance/mask_pii.d.ts +17 -0
  6. package/dist/framework-mcp/src/tools/compliance/mask_pii.js +56 -0
  7. package/dist/framework-mcp/src/tools/compliance/mask_pii.js.map +1 -0
  8. package/dist/framework-mcp/src/tools/definitions.js +10 -9
  9. package/dist/framework-mcp/src/tools/definitions.js.map +1 -1
  10. package/dist/framework-mcp/src/tools/framework/run_tests.js +25 -0
  11. package/dist/framework-mcp/src/tools/framework/run_tests.js.map +1 -1
  12. package/dist/framework-mcp/src/tools/index.js +7 -8
  13. package/dist/framework-mcp/src/tools/index.js.map +1 -1
  14. package/dist/framework-mcp/src/tools/messaging/approve_operation.d.ts +22 -0
  15. package/dist/framework-mcp/src/tools/messaging/approve_operation.js +81 -0
  16. package/dist/framework-mcp/src/tools/messaging/approve_operation.js.map +1 -0
  17. package/dist/framework-mcp/src/tools/messaging/ask_human.d.ts +15 -0
  18. package/dist/framework-mcp/src/tools/messaging/ask_human.js +89 -20
  19. package/dist/framework-mcp/src/tools/messaging/ask_human.js.map +1 -1
  20. package/dist/framework-mcp/src/tools/observability/check_ports.js +3 -1
  21. package/dist/framework-mcp/src/tools/observability/check_ports.js.map +1 -1
  22. package/dist/framework-mcp/src/tools/quality/analyze_code_quality.js +13 -64
  23. package/dist/framework-mcp/src/tools/quality/analyze_code_quality.js.map +1 -1
  24. package/dist/framework-mcp/src/tools/schemas.d.ts +48 -53
  25. package/dist/framework-mcp/src/tools/schemas.js +18 -22
  26. package/dist/framework-mcp/src/tools/schemas.js.map +1 -1
  27. package/dist/framework-mcp/src/tools/shell/run_command.js +10 -0
  28. package/dist/framework-mcp/src/tools/shell/run_command.js.map +1 -1
  29. package/dist/framework-mcp/src/tools/types.d.ts +11 -105
  30. package/dist/framework-mcp/src/tools/types.js +1 -0
  31. package/dist/framework-mcp/src/tools/types.js.map +1 -1
  32. package/dist/framework-mcp/src/utils/auth.d.ts +36 -0
  33. package/dist/framework-mcp/src/utils/auth.js +79 -0
  34. package/dist/framework-mcp/src/utils/auth.js.map +1 -0
  35. package/dist/framework-mcp/src/utils/auto-rollback.d.ts +71 -0
  36. package/dist/framework-mcp/src/utils/auto-rollback.js +266 -0
  37. package/dist/framework-mcp/src/utils/auto-rollback.js.map +1 -0
  38. package/dist/framework-mcp/src/utils/compliance.js +12 -4
  39. package/dist/framework-mcp/src/utils/compliance.js.map +1 -1
  40. package/dist/framework-mcp/src/utils/context-optimizer.d.ts +69 -0
  41. package/dist/framework-mcp/src/utils/context-optimizer.js +157 -0
  42. package/dist/framework-mcp/src/utils/context-optimizer.js.map +1 -0
  43. package/dist/framework-mcp/src/utils/discipline.d.ts +68 -0
  44. package/dist/framework-mcp/src/utils/discipline.js +218 -0
  45. package/dist/framework-mcp/src/utils/discipline.js.map +1 -0
  46. package/dist/framework-mcp/src/utils/finops.d.ts +143 -0
  47. package/dist/framework-mcp/src/utils/finops.js +326 -0
  48. package/dist/framework-mcp/src/utils/finops.js.map +1 -0
  49. package/dist/framework-mcp/src/utils/human-in-loop.d.ts +95 -0
  50. package/dist/framework-mcp/src/utils/human-in-loop.js +216 -0
  51. package/dist/framework-mcp/src/utils/human-in-loop.js.map +1 -0
  52. package/dist/framework-mcp/src/utils/license-scanner.d.ts +71 -0
  53. package/dist/framework-mcp/src/utils/license-scanner.js +284 -0
  54. package/dist/framework-mcp/src/utils/license-scanner.js.map +1 -0
  55. package/dist/framework-mcp/src/utils/loop-detector.d.ts +105 -0
  56. package/dist/framework-mcp/src/utils/loop-detector.js +362 -0
  57. package/dist/framework-mcp/src/utils/loop-detector.js.map +1 -0
  58. package/dist/framework-mcp/src/utils/quality.d.ts +19 -0
  59. package/dist/framework-mcp/src/utils/quality.js +124 -0
  60. package/dist/framework-mcp/src/utils/quality.js.map +1 -0
  61. package/dist/framework-mcp/src/utils/rules-engine.d.ts +78 -0
  62. package/dist/framework-mcp/src/utils/rules-engine.js +232 -0
  63. package/dist/framework-mcp/src/utils/rules-engine.js.map +1 -0
  64. package/dist/framework-mcp/src/utils/silent-router.d.ts +47 -0
  65. package/dist/framework-mcp/src/utils/silent-router.js +129 -0
  66. package/dist/framework-mcp/src/utils/silent-router.js.map +1 -0
  67. package/dist/framework-mcp/src/utils/storage.d.ts +1 -0
  68. package/dist/framework-mcp/src/utils/storage.js +2 -2
  69. package/dist/framework-mcp/src/utils/storage.js.map +1 -1
  70. package/dist/framework-mcp/src/utils/telemetry-streamer.d.ts +133 -0
  71. package/dist/framework-mcp/src/utils/telemetry-streamer.js +397 -0
  72. package/dist/framework-mcp/src/utils/telemetry-streamer.js.map +1 -0
  73. package/dist/framework-mcp/tests/tools/file_system/file_system_tools.test.js +2 -2
  74. package/dist/framework-mcp/tests/tools/file_system/file_system_tools.test.js.map +1 -1
  75. package/dist/framework-mcp/tests/tools/framework/run_tests.test.d.ts +1 -0
  76. package/dist/framework-mcp/tests/tools/framework/run_tests.test.js +49 -0
  77. package/dist/framework-mcp/tests/tools/framework/run_tests.test.js.map +1 -0
  78. package/dist/framework-mcp/tests/tools/messaging/send_message.test.js +14 -5
  79. package/dist/framework-mcp/tests/tools/messaging/send_message.test.js.map +1 -1
  80. package/dist/framework-mcp/tests/tools/observability/check_ports.test.d.ts +1 -0
  81. package/dist/framework-mcp/tests/tools/observability/check_ports.test.js +30 -0
  82. package/dist/framework-mcp/tests/tools/observability/check_ports.test.js.map +1 -0
  83. package/dist/framework-mcp/tests/tools/shell/run_command.test.js +7 -0
  84. package/dist/framework-mcp/tests/tools/shell/run_command.test.js.map +1 -1
  85. package/dist/framework-mcp/tests/utils/auto-rollback.test.d.ts +7 -0
  86. package/dist/framework-mcp/tests/utils/auto-rollback.test.js +178 -0
  87. package/dist/framework-mcp/tests/utils/auto-rollback.test.js.map +1 -0
  88. package/dist/framework-mcp/tests/utils/context-optimizer.test.d.ts +1 -0
  89. package/dist/framework-mcp/tests/utils/context-optimizer.test.js +62 -0
  90. package/dist/framework-mcp/tests/utils/context-optimizer.test.js.map +1 -0
  91. package/dist/framework-mcp/tests/utils/finops.test.d.ts +7 -0
  92. package/dist/framework-mcp/tests/utils/finops.test.js +157 -0
  93. package/dist/framework-mcp/tests/utils/finops.test.js.map +1 -0
  94. package/dist/framework-mcp/tests/utils/human-in-loop.test.d.ts +1 -0
  95. package/dist/framework-mcp/tests/utils/human-in-loop.test.js +59 -0
  96. package/dist/framework-mcp/tests/utils/human-in-loop.test.js.map +1 -0
  97. package/dist/framework-mcp/tests/utils/license-scanner.test.d.ts +7 -0
  98. package/dist/framework-mcp/tests/utils/license-scanner.test.js +131 -0
  99. package/dist/framework-mcp/tests/utils/license-scanner.test.js.map +1 -0
  100. package/dist/framework-mcp/tests/utils/loop-detector.test.d.ts +7 -0
  101. package/dist/framework-mcp/tests/utils/loop-detector.test.js +204 -0
  102. package/dist/framework-mcp/tests/utils/loop-detector.test.js.map +1 -0
  103. package/dist/framework-mcp/tests/utils/rules-engine.test.d.ts +1 -0
  104. package/dist/framework-mcp/tests/utils/rules-engine.test.js +106 -0
  105. package/dist/framework-mcp/tests/utils/rules-engine.test.js.map +1 -0
  106. package/dist/framework-mcp/tests/utils/telemetry-streamer.test.d.ts +7 -0
  107. package/dist/framework-mcp/tests/utils/telemetry-streamer.test.js +201 -0
  108. package/dist/framework-mcp/tests/utils/telemetry-streamer.test.js.map +1 -0
  109. package/dist/src/cli/commands/check.js +16 -1
  110. package/dist/src/cli/commands/check.js.map +1 -1
  111. package/dist/src/cli/commands/dashboard.js +188 -164
  112. package/dist/src/cli/commands/dashboard.js.map +1 -1
  113. package/dist/src/cli/commands/gateway.js +2 -2
  114. package/dist/src/cli/commands/gateway.js.map +1 -1
  115. package/dist/src/cli/commands/init/scaffold-core.d.ts +2 -0
  116. package/dist/src/cli/commands/init/scaffold-core.js +16 -1
  117. package/dist/src/cli/commands/init/scaffold-core.js.map +1 -1
  118. package/dist/src/cli/commands/init/scaffold-ops.js +2 -2
  119. package/dist/src/cli/commands/init/scaffold-ops.js.map +1 -1
  120. package/dist/src/cli/commands/init/scaffold-standards.js +13 -1
  121. package/dist/src/cli/commands/init/scaffold-standards.js.map +1 -1
  122. package/dist/src/cli/commands/init.d.ts +4 -0
  123. package/dist/src/cli/commands/init.js +80 -24
  124. package/dist/src/cli/commands/init.js.map +1 -1
  125. package/dist/src/cli/commands/quickstart.d.ts +1 -1
  126. package/dist/src/cli/commands/quickstart.js +12 -24
  127. package/dist/src/cli/commands/quickstart.js.map +1 -1
  128. package/dist/src/cli/index.js +24 -23
  129. package/dist/src/cli/index.js.map +1 -1
  130. package/dist/src/cli/shims.js +7 -7
  131. package/dist/src/cli/utils/pkg.js +2 -3
  132. package/dist/src/cli/utils/pkg.js.map +1 -1
  133. package/dist/src/cli/utils/quality.d.ts +10 -0
  134. package/dist/src/cli/utils/quality.js +10 -0
  135. package/dist/src/cli/utils/quality.js.map +1 -0
  136. package/dist/src/modules/agents/definitions.js +45 -40
  137. package/dist/src/modules/agents/definitions.js.map +1 -1
  138. package/dist/src/modules/agents/registry/manager.js +6 -1
  139. package/dist/src/modules/agents/registry/manager.js.map +1 -1
  140. package/dist/src/modules/engines/agent-executor.d.ts +6 -10
  141. package/dist/src/modules/engines/agent-executor.js +53 -21
  142. package/dist/src/modules/engines/agent-executor.js.map +1 -1
  143. package/dist/src/modules/engines/agent-loop.d.ts +34 -0
  144. package/dist/src/modules/engines/agent-loop.js +102 -0
  145. package/dist/src/modules/engines/agent-loop.js.map +1 -0
  146. package/dist/src/modules/engines/evaluation-engine.js +2 -2
  147. package/dist/src/modules/engines/evaluation-engine.js.map +1 -1
  148. package/dist/src/modules/engines/quality-gate.d.ts +13 -5
  149. package/dist/src/modules/engines/quality-gate.js +39 -31
  150. package/dist/src/modules/engines/quality-gate.js.map +1 -1
  151. package/dist/src/modules/engines/routing-engine.d.ts +5 -3
  152. package/dist/src/modules/engines/routing-engine.js +16 -9
  153. package/dist/src/modules/engines/routing-engine.js.map +1 -1
  154. package/dist/src/modules/gateway/cost-tracker.d.ts +5 -0
  155. package/dist/src/modules/gateway/cost-tracker.js +71 -0
  156. package/dist/src/modules/gateway/cost-tracker.js.map +1 -1
  157. package/dist/src/modules/gateway/gateway.d.ts +4 -0
  158. package/dist/src/modules/gateway/gateway.js +8 -0
  159. package/dist/src/modules/gateway/gateway.js.map +1 -1
  160. package/dist/src/shared/audit.js +3 -2
  161. package/dist/src/shared/audit.js.map +1 -1
  162. package/dist/src/shared/pii.d.ts +41 -9
  163. package/dist/src/shared/pii.js +122 -31
  164. package/dist/src/shared/pii.js.map +1 -1
  165. package/dist/src/shared/storage.d.ts +19 -0
  166. package/dist/src/shared/storage.js +46 -6
  167. package/dist/src/shared/storage.js.map +1 -1
  168. package/dist/tests/embedding.test.d.ts +1 -0
  169. package/dist/tests/embedding.test.js +82 -0
  170. package/dist/tests/embedding.test.js.map +1 -0
  171. package/dist/tests/engines/evaluation-engine.test.d.ts +1 -0
  172. package/dist/tests/engines/evaluation-engine.test.js +71 -0
  173. package/dist/tests/engines/evaluation-engine.test.js.map +1 -0
  174. package/dist/tests/engines/planning-engine.test.d.ts +1 -0
  175. package/dist/tests/engines/planning-engine.test.js +100 -0
  176. package/dist/tests/engines/planning-engine.test.js.map +1 -0
  177. package/dist/tests/engines/quality-gate.test.d.ts +1 -0
  178. package/dist/tests/engines/quality-gate.test.js +59 -0
  179. package/dist/tests/engines/quality-gate.test.js.map +1 -0
  180. package/dist/tests/engines/risk-engine.test.d.ts +1 -0
  181. package/dist/tests/engines/risk-engine.test.js +80 -0
  182. package/dist/tests/engines/risk-engine.test.js.map +1 -0
  183. package/dist/tests/engines/routing-engine.test.d.ts +1 -0
  184. package/dist/tests/engines/routing-engine.test.js +78 -0
  185. package/dist/tests/engines/routing-engine.test.js.map +1 -0
  186. package/dist/tests/integration/hermes_locking.test.js +1 -0
  187. package/dist/tests/integration/hermes_locking.test.js.map +1 -1
  188. package/dist/tests/modules/gateway/cost-tracker.test.js +20 -1
  189. package/dist/tests/modules/gateway/cost-tracker.test.js.map +1 -1
  190. package/dist/tests/shared/lock.test.d.ts +1 -0
  191. package/dist/tests/shared/lock.test.js +95 -0
  192. package/dist/tests/shared/lock.test.js.map +1 -0
  193. package/dist/tests/shared/pii.test.d.ts +1 -0
  194. package/dist/tests/shared/pii.test.js +183 -0
  195. package/dist/tests/shared/pii.test.js.map +1 -0
  196. package/framework-mcp/README.md +307 -35
  197. package/framework-mcp/dist/dashboard/assets/index-B2mYld0c.js +1221 -0
  198. package/framework-mcp/dist/dashboard/index.html +1 -1
  199. package/framework-mcp/dist/framework-mcp/src/index.js +914 -62
  200. package/framework-mcp/dist/framework-mcp/src/tools/compliance/mask_pii.js +55 -0
  201. package/framework-mcp/dist/framework-mcp/src/tools/definitions.js +10 -9
  202. package/framework-mcp/dist/framework-mcp/src/tools/framework/run_tests.js +25 -0
  203. package/framework-mcp/dist/framework-mcp/src/tools/index.js +7 -8
  204. package/framework-mcp/dist/framework-mcp/src/tools/messaging/approve_operation.js +80 -0
  205. package/framework-mcp/dist/framework-mcp/src/tools/messaging/ask_human.js +89 -20
  206. package/framework-mcp/dist/framework-mcp/src/tools/observability/check_ports.js +3 -1
  207. package/framework-mcp/dist/framework-mcp/src/tools/quality/analyze_code_quality.js +13 -64
  208. package/framework-mcp/dist/framework-mcp/src/tools/schemas.js +18 -22
  209. package/framework-mcp/dist/framework-mcp/src/tools/shell/run_command.js +10 -0
  210. package/framework-mcp/dist/framework-mcp/src/tools/types.js +1 -0
  211. package/framework-mcp/dist/framework-mcp/src/utils/auth.js +78 -0
  212. package/framework-mcp/dist/framework-mcp/src/utils/auto-rollback.js +265 -0
  213. package/framework-mcp/dist/framework-mcp/src/utils/compliance.js +12 -4
  214. package/framework-mcp/dist/framework-mcp/src/utils/context-optimizer.js +156 -0
  215. package/framework-mcp/dist/framework-mcp/src/utils/discipline.js +217 -0
  216. package/framework-mcp/dist/framework-mcp/src/utils/finops.js +325 -0
  217. package/framework-mcp/dist/framework-mcp/src/utils/human-in-loop.js +215 -0
  218. package/framework-mcp/dist/framework-mcp/src/utils/license-scanner.js +283 -0
  219. package/framework-mcp/dist/framework-mcp/src/utils/loop-detector.js +361 -0
  220. package/framework-mcp/dist/framework-mcp/src/utils/quality.js +123 -0
  221. package/framework-mcp/dist/framework-mcp/src/utils/rules-engine.js +231 -0
  222. package/framework-mcp/dist/framework-mcp/src/utils/silent-router.js +128 -0
  223. package/framework-mcp/dist/framework-mcp/src/utils/storage.js +2 -2
  224. package/framework-mcp/dist/framework-mcp/src/utils/telemetry-streamer.js +396 -0
  225. package/framework-mcp/dist/src/cli/platforms/core.js +72 -0
  226. package/framework-mcp/dist/src/cli/platforms/index.js +5 -0
  227. package/framework-mcp/dist/src/cli/platforms/paths.js +101 -0
  228. package/framework-mcp/dist/src/cli/platforms/scaffold.js +72 -0
  229. package/framework-mcp/dist/src/cli/platforms/utils.js +75 -0
  230. package/framework-mcp/dist/src/cli/utils/claude.js +57 -0
  231. package/framework-mcp/dist/src/cli/utils/compliance.js +12 -0
  232. package/framework-mcp/dist/src/cli/utils/fs.js +138 -0
  233. package/framework-mcp/dist/src/cli/utils/pkg.js +282 -0
  234. package/framework-mcp/dist/src/cli/utils/quality.js +9 -0
  235. package/framework-mcp/dist/src/modules/agents/definitions.js +563 -0
  236. package/framework-mcp/dist/src/modules/agents/registry/analyst.js +39 -0
  237. package/framework-mcp/dist/src/modules/agents/registry/architect.js +42 -0
  238. package/framework-mcp/dist/src/modules/agents/registry/backend.js +50 -0
  239. package/framework-mcp/dist/src/modules/agents/registry/database.js +45 -0
  240. package/framework-mcp/dist/src/modules/agents/registry/devops.js +45 -0
  241. package/framework-mcp/dist/src/modules/agents/registry/explorer.js +37 -0
  242. package/framework-mcp/dist/src/modules/agents/registry/frontend.js +52 -0
  243. package/framework-mcp/dist/src/modules/agents/registry/git.js +36 -0
  244. package/framework-mcp/dist/src/modules/agents/registry/manager.js +64 -0
  245. package/framework-mcp/dist/src/modules/agents/registry/mobile.js +41 -0
  246. package/framework-mcp/dist/src/modules/agents/registry/native.js +41 -0
  247. package/framework-mcp/dist/src/modules/agents/registry/quality.js +43 -0
  248. package/framework-mcp/dist/src/modules/agents/registry/security.js +42 -0
  249. package/framework-mcp/dist/src/modules/agents/types.js +1 -0
  250. package/framework-mcp/dist/src/modules/engines/quality-gate.js +92 -0
  251. package/framework-mcp/dist/src/modules/engines/risk-engine.js +105 -0
  252. package/framework-mcp/dist/src/modules/engines/routing-engine.js +408 -0
  253. package/framework-mcp/dist/src/modules/engines/types.js +1 -0
  254. package/framework-mcp/dist/src/modules/providers/definitions.js +140 -0
  255. package/framework-mcp/dist/src/modules/providers/registry.js +18 -0
  256. package/framework-mcp/dist/src/modules/providers/shared.js +104 -0
  257. package/framework-mcp/dist/src/modules/providers/types.js +1 -0
  258. package/framework-mcp/dist/src/modules/skills/adapter-skills.js +449 -0
  259. package/framework-mcp/dist/src/modules/skills/definitions.js +70 -0
  260. package/framework-mcp/dist/src/shared/audit.js +220 -0
  261. package/framework-mcp/dist/src/shared/errors.js +68 -0
  262. package/framework-mcp/dist/src/shared/lock.js +159 -0
  263. package/framework-mcp/dist/src/shared/pii.js +122 -31
  264. package/framework-mcp/dist/src/shared/retention.js +170 -0
  265. package/framework-mcp/dist/src/shared/storage.js +46 -6
  266. package/framework-mcp/dist/src/shared/string.js +29 -0
  267. package/framework-mcp/package.json +8 -2
  268. package/mcp.json +11 -6
  269. package/package.json +13 -6
  270. package/templates/full/ATABEY_FULL.md +1 -1
  271. package/framework-mcp/dist/dashboard/assets/index-BAPGLiWE.js +0 -267
@@ -0,0 +1,215 @@
1
+ /**
2
+ * ─── HUMAN-IN-THE-LOOP (In-CLI Approval) ──────────────────────────
3
+ *
4
+ * Allows risk-gated approvals without the developer leaving their AI CLI chat.
5
+ *
6
+ * Key Innovation: Instead of forcing developers to switch to a web dashboard
7
+ * or another terminal, approval requests are sent as MCP notifications into
8
+ * the developer's existing chat interface.
9
+ *
10
+ * Flow:
11
+ * 1. Tool call triggers risk detection
12
+ * 2. If risk >= threshold, tool is blocked with a structured error
13
+ * 3. Error message includes the traceId and approval command
14
+ * 4. Developer types "atabey approve <traceId>" in the same chat
15
+ * 5. Hermes message status updates → retry succeeds
16
+ *
17
+ * Risk Levels:
18
+ * - LOW (0-30): Silent pass (no approval needed)
19
+ * - MEDIUM (31-59): Stealth warning to stderr only
20
+ * - HIGH (60-100): Blocked until approval
21
+ */
22
+ import { AtabeyStorage } from "../../../src/shared/storage.js";
23
+ import { asAgentID, asTraceID } from "../../../src/shared/types.js";
24
+ // ─── Configuration ────────────────────────────────────────────────
25
+ const CONFIG = {
26
+ /** Risk threshold for blocking (0-100) */
27
+ HIGH_RISK_THRESHOLD: parseInt(process.env.MCP_HIGH_RISK_THRESHOLD || "60", 10),
28
+ /** Risk threshold for medium warning */
29
+ MEDIUM_RISK_THRESHOLD: parseInt(process.env.MCP_MEDIUM_RISK_THRESHOLD || "30", 10),
30
+ /** Auto-approve low risk operations */
31
+ AUTO_APPROVE_LOW_RISK: process.env.MCP_AUTO_APPROVE_LOW_RISK !== "false",
32
+ /** Approval timeout in seconds */
33
+ APPROVAL_TIMEOUT_SECONDS: parseInt(process.env.MCP_APPROVAL_TIMEOUT || "300", 10), // 5 min
34
+ };
35
+ // ─── Active Approval Requests ─────────────────────────────────────
36
+ const activeApprovals = new Map();
37
+ /**
38
+ * Determine risk level from risk score.
39
+ */
40
+ export function getRiskLevel(score) {
41
+ if (score >= CONFIG.HIGH_RISK_THRESHOLD)
42
+ return "HIGH";
43
+ if (score >= CONFIG.MEDIUM_RISK_THRESHOLD)
44
+ return "MEDIUM";
45
+ return "LOW";
46
+ }
47
+ /**
48
+ * Create an approval request for a high-risk operation.
49
+ * Returns the approval request and a message for the AI/developer.
50
+ */
51
+ export function createApprovalRequest(traceId, toolName, agent, riskScore, reason, args) {
52
+ const now = Date.now();
53
+ const request = {
54
+ traceId,
55
+ toolName,
56
+ agent,
57
+ riskScore,
58
+ riskLevel: getRiskLevel(riskScore),
59
+ reason,
60
+ args,
61
+ createdAt: now,
62
+ expiresAt: now + CONFIG.APPROVAL_TIMEOUT_SECONDS * 1000,
63
+ status: "PENDING",
64
+ };
65
+ activeApprovals.set(traceId, request);
66
+ // Create a Hermes approval message
67
+ AtabeyStorage.saveMessage({
68
+ from: asAgentID(agent),
69
+ to: asAgentID("@manager"),
70
+ category: "ALERT",
71
+ content: JSON.stringify({
72
+ type: "APPROVAL_REQUIRED",
73
+ traceId,
74
+ tool: toolName,
75
+ riskScore,
76
+ reason,
77
+ }),
78
+ traceId: asTraceID(traceId),
79
+ timestamp: new Date().toISOString(),
80
+ status: "PENDING",
81
+ priority: "HIGH",
82
+ requiresApproval: true,
83
+ });
84
+ // Build the in-chat approval message
85
+ // This is what the developer sees in their AI CLI chat
86
+ const message = [
87
+ `[RISK GATE] Operation blocked: ${reason}`,
88
+ "",
89
+ ` Tool: ${toolName}`,
90
+ ` Agent: ${agent}`,
91
+ ` Risk Score: ${riskScore}/100 (${request.riskLevel})`,
92
+ ` Trace ID: ${traceId}`,
93
+ "",
94
+ "─── IN-CHAT APPROVAL ────────────────────────────────────────",
95
+ "Ask your AI assistant to call the approve_operation tool:",
96
+ "",
97
+ ` To approve: approve_operation { "action": "approve", "traceId": "${traceId}" }`,
98
+ ` To reject: approve_operation { "action": "reject", "traceId": "${traceId}" }`,
99
+ " To list: approve_operation { \"action\": \"list\" }",
100
+ "─────────────────────────────────────────────────────────────",
101
+ "",
102
+ `Alternative: atabey hitl answer "${traceId}:approve" in your terminal.`,
103
+ ].join("\n");
104
+ return { request, message };
105
+ }
106
+ /**
107
+ * Check if an operation should be blocked based on risk score.
108
+ * Returns:
109
+ * - null if allowed (LOW risk or auto-approved)
110
+ * - { blocked: true, message } if blocked (HIGH risk)
111
+ * - { blocked: false, warning } if warning only (MEDIUM risk)
112
+ */
113
+ export function checkRiskGate(traceId, toolName, agent, riskScore, reason, args) {
114
+ const level = getRiskLevel(riskScore);
115
+ // LOW risk: always pass
116
+ if (level === "LOW") {
117
+ return null;
118
+ }
119
+ // MEDIUM risk: warning only
120
+ if (level === "MEDIUM") {
121
+ return {
122
+ blocked: false,
123
+ message: null,
124
+ warning: `[RISK WARNING] ${reason} (risk: ${riskScore}/100). Monitor this operation.`,
125
+ };
126
+ }
127
+ // HIGH risk: block and create approval request
128
+ const { message } = createApprovalRequest(traceId, toolName, agent, riskScore, reason, args);
129
+ return {
130
+ blocked: true,
131
+ message,
132
+ };
133
+ }
134
+ /**
135
+ * Check if an approval request exists and is still valid.
136
+ */
137
+ export function getApprovalStatus(traceId) {
138
+ const request = activeApprovals.get(traceId);
139
+ if (!request)
140
+ return null;
141
+ // Check if expired
142
+ if (Date.now() > request.expiresAt) {
143
+ request.status = "EXPIRED";
144
+ return request;
145
+ }
146
+ return request;
147
+ }
148
+ /**
149
+ * Approve a pending operation.
150
+ */
151
+ export function approveOperation(traceId) {
152
+ const request = activeApprovals.get(traceId);
153
+ if (!request) {
154
+ return { success: false, message: `No pending approval found for trace: ${traceId}` };
155
+ }
156
+ if (request.status !== "PENDING") {
157
+ return { success: false, message: `Approval for ${traceId} is already ${request.status}` };
158
+ }
159
+ request.status = "APPROVED";
160
+ // Update Hermes message status
161
+ const messages = AtabeyStorage.getPendingMessages();
162
+ const pendingMsgs = messages.filter(m => m.traceId === traceId && m.category === "ALERT" && m.status === "PENDING");
163
+ for (const msg of pendingMsgs) {
164
+ if (msg.id !== undefined) {
165
+ AtabeyStorage.updateMessageStatus(msg.id, "APPROVED");
166
+ }
167
+ }
168
+ return { success: true, message: `Operation ${traceId} approved. You may retry.` };
169
+ }
170
+ /**
171
+ * Reject a pending operation.
172
+ */
173
+ export function rejectOperation(traceId) {
174
+ const request = activeApprovals.get(traceId);
175
+ if (!request) {
176
+ return { success: false, message: `No pending approval found for trace: ${traceId}` };
177
+ }
178
+ if (request.status !== "PENDING") {
179
+ return { success: false, message: `Approval for ${traceId} is already ${request.status}` };
180
+ }
181
+ request.status = "REJECTED";
182
+ // Update Hermes message status
183
+ const messages = AtabeyStorage.getPendingMessages();
184
+ const pendingMsgs = messages.filter(m => m.traceId === traceId && m.category === "ALERT" && m.status === "PENDING");
185
+ for (const msg of pendingMsgs) {
186
+ if (msg.id !== undefined) {
187
+ AtabeyStorage.updateMessageStatus(msg.id, "REJECTED");
188
+ }
189
+ }
190
+ return { success: true, message: `Operation ${traceId} rejected.` };
191
+ }
192
+ /**
193
+ * Get all pending approval requests.
194
+ */
195
+ export function getPendingApprovals() {
196
+ const now = Date.now();
197
+ const pending = [];
198
+ // Clean expired
199
+ for (const [traceId, request] of activeApprovals) {
200
+ if (request.status === "PENDING" && now > request.expiresAt) {
201
+ request.status = "EXPIRED";
202
+ }
203
+ if (request.status === "PENDING") {
204
+ pending.push(request);
205
+ }
206
+ }
207
+ return pending;
208
+ }
209
+ /**
210
+ * Get HITL config.
211
+ */
212
+ export function getHumanInLoopConfig() {
213
+ return { ...CONFIG };
214
+ }
215
+ export { CONFIG as HumanInLoopConfig };
@@ -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 };