couchloop-eq-mcp 1.0.3 → 1.0.5

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 (285) hide show
  1. package/README.md +24 -11
  2. package/dist/clients/shrinkChatClient.js +1 -1
  3. package/dist/clients/shrinkChatClient.js.map +1 -1
  4. package/dist/developer/analyzers/bloat-detector.d.ts +89 -0
  5. package/dist/developer/analyzers/bloat-detector.d.ts.map +1 -0
  6. package/dist/developer/analyzers/bloat-detector.js +483 -0
  7. package/dist/developer/analyzers/bloat-detector.js.map +1 -0
  8. package/dist/developer/backup/auto-backup.d.ts +96 -0
  9. package/dist/developer/backup/auto-backup.d.ts.map +1 -0
  10. package/dist/developer/backup/auto-backup.js +346 -0
  11. package/dist/developer/backup/auto-backup.js.map +1 -0
  12. package/dist/developer/blockers/package-blocker.d.ts +33 -0
  13. package/dist/developer/blockers/package-blocker.d.ts.map +1 -0
  14. package/dist/developer/blockers/package-blocker.js +224 -0
  15. package/dist/developer/blockers/package-blocker.js.map +1 -0
  16. package/dist/developer/evaluators/ai-error-preventer.d.ts +54 -0
  17. package/dist/developer/evaluators/ai-error-preventer.d.ts.map +1 -0
  18. package/dist/developer/evaluators/ai-error-preventer.js +270 -0
  19. package/dist/developer/evaluators/ai-error-preventer.js.map +1 -0
  20. package/dist/developer/evaluators/build-context-detector.d.ts +44 -0
  21. package/dist/developer/evaluators/build-context-detector.d.ts.map +1 -0
  22. package/dist/developer/evaluators/build-context-detector.js +258 -0
  23. package/dist/developer/evaluators/build-context-detector.js.map +1 -0
  24. package/dist/developer/evaluators/package-evaluator.d.ts +37 -0
  25. package/dist/developer/evaluators/package-evaluator.d.ts.map +1 -0
  26. package/dist/developer/evaluators/package-evaluator.js +278 -0
  27. package/dist/developer/evaluators/package-evaluator.js.map +1 -0
  28. package/dist/developer/guards/file-guardian.d.ts +79 -0
  29. package/dist/developer/guards/file-guardian.d.ts.map +1 -0
  30. package/dist/developer/guards/file-guardian.js +309 -0
  31. package/dist/developer/guards/file-guardian.js.map +1 -0
  32. package/dist/developer/managers/context-manager.d.ts +61 -0
  33. package/dist/developer/managers/context-manager.d.ts.map +1 -0
  34. package/dist/developer/managers/context-manager.js +302 -0
  35. package/dist/developer/managers/context-manager.js.map +1 -0
  36. package/dist/developer/metrics/complexity-calculator.d.ts +52 -0
  37. package/dist/developer/metrics/complexity-calculator.d.ts.map +1 -0
  38. package/dist/developer/metrics/complexity-calculator.js +259 -0
  39. package/dist/developer/metrics/complexity-calculator.js.map +1 -0
  40. package/dist/developer/reports/review-summary.d.ts +49 -0
  41. package/dist/developer/reports/review-summary.d.ts.map +1 -0
  42. package/dist/developer/reports/review-summary.js +249 -0
  43. package/dist/developer/reports/review-summary.js.map +1 -0
  44. package/dist/developer/scanners/review-assistant.d.ts +41 -0
  45. package/dist/developer/scanners/review-assistant.d.ts.map +1 -0
  46. package/dist/developer/scanners/review-assistant.js +374 -0
  47. package/dist/developer/scanners/review-assistant.js.map +1 -0
  48. package/dist/developer/scanners/secret-scanner.d.ts +66 -0
  49. package/dist/developer/scanners/secret-scanner.d.ts.map +1 -0
  50. package/dist/developer/scanners/secret-scanner.js +287 -0
  51. package/dist/developer/scanners/secret-scanner.js.map +1 -0
  52. package/dist/developer/scanners/sql-injection-detector.d.ts +54 -0
  53. package/dist/developer/scanners/sql-injection-detector.d.ts.map +1 -0
  54. package/dist/developer/scanners/sql-injection-detector.js +174 -0
  55. package/dist/developer/scanners/sql-injection-detector.js.map +1 -0
  56. package/dist/developer/scanners/xss-detector.d.ts +60 -0
  57. package/dist/developer/scanners/xss-detector.d.ts.map +1 -0
  58. package/dist/developer/scanners/xss-detector.js +229 -0
  59. package/dist/developer/scanners/xss-detector.js.map +1 -0
  60. package/dist/developer/types/ai-errors.d.ts +34 -0
  61. package/dist/developer/types/ai-errors.d.ts.map +1 -0
  62. package/dist/developer/types/ai-errors.js +271 -0
  63. package/dist/developer/types/ai-errors.js.map +1 -0
  64. package/dist/developer/types/package.d.ts +32 -0
  65. package/dist/developer/types/package.d.ts.map +1 -0
  66. package/dist/developer/types/package.js +5 -0
  67. package/dist/developer/types/package.js.map +1 -0
  68. package/dist/developer/updaters/dependency-updater.d.ts +102 -0
  69. package/dist/developer/updaters/dependency-updater.d.ts.map +1 -0
  70. package/dist/developer/updaters/dependency-updater.js +472 -0
  71. package/dist/developer/updaters/dependency-updater.js.map +1 -0
  72. package/dist/developer/validators/cargo.d.ts +14 -0
  73. package/dist/developer/validators/cargo.d.ts.map +1 -0
  74. package/dist/developer/validators/cargo.js +132 -0
  75. package/dist/developer/validators/cargo.js.map +1 -0
  76. package/dist/developer/validators/gem.d.ts +14 -0
  77. package/dist/developer/validators/gem.d.ts.map +1 -0
  78. package/dist/developer/validators/gem.js +85 -0
  79. package/dist/developer/validators/gem.js.map +1 -0
  80. package/dist/developer/validators/go.d.ts +14 -0
  81. package/dist/developer/validators/go.d.ts.map +1 -0
  82. package/dist/developer/validators/go.js +138 -0
  83. package/dist/developer/validators/go.js.map +1 -0
  84. package/dist/developer/validators/maven.d.ts +14 -0
  85. package/dist/developer/validators/maven.d.ts.map +1 -0
  86. package/dist/developer/validators/maven.js +99 -0
  87. package/dist/developer/validators/maven.js.map +1 -0
  88. package/dist/developer/validators/npm.d.ts +14 -0
  89. package/dist/developer/validators/npm.d.ts.map +1 -0
  90. package/dist/developer/validators/npm.js +96 -0
  91. package/dist/developer/validators/npm.js.map +1 -0
  92. package/dist/developer/validators/nuget.d.ts +15 -0
  93. package/dist/developer/validators/nuget.d.ts.map +1 -0
  94. package/dist/developer/validators/nuget.js +107 -0
  95. package/dist/developer/validators/nuget.js.map +1 -0
  96. package/dist/developer/validators/pypi.d.ts +14 -0
  97. package/dist/developer/validators/pypi.d.ts.map +1 -0
  98. package/dist/developer/validators/pypi.js +118 -0
  99. package/dist/developer/validators/pypi.js.map +1 -0
  100. package/dist/developer/validators/registry-manager.d.ts +37 -0
  101. package/dist/developer/validators/registry-manager.d.ts.map +1 -0
  102. package/dist/developer/validators/registry-manager.js +89 -0
  103. package/dist/developer/validators/registry-manager.js.map +1 -0
  104. package/dist/developer/validators/version-checker.d.ts +145 -0
  105. package/dist/developer/validators/version-checker.d.ts.map +1 -0
  106. package/dist/developer/validators/version-checker.js +529 -0
  107. package/dist/developer/validators/version-checker.js.map +1 -0
  108. package/dist/server/index.js.map +1 -1
  109. package/dist/server/middleware/auth.d.ts +7 -9
  110. package/dist/server/middleware/auth.d.ts.map +1 -1
  111. package/dist/server/middleware/auth.js.map +1 -1
  112. package/dist/tools/check-versions.d.ts +100 -0
  113. package/dist/tools/check-versions.d.ts.map +1 -0
  114. package/dist/tools/check-versions.js +328 -0
  115. package/dist/tools/check-versions.js.map +1 -0
  116. package/dist/tools/detect-code-smell.d.ts +9 -0
  117. package/dist/tools/detect-code-smell.d.ts.map +1 -0
  118. package/dist/tools/detect-code-smell.js +231 -0
  119. package/dist/tools/detect-code-smell.js.map +1 -0
  120. package/dist/tools/index.d.ts +471 -0
  121. package/dist/tools/index.d.ts.map +1 -1
  122. package/dist/tools/index.js +178 -0
  123. package/dist/tools/index.js.map +1 -1
  124. package/dist/tools/journey.js +1 -1
  125. package/dist/tools/journey.js.map +1 -1
  126. package/dist/tools/pre-review-code.d.ts +71 -0
  127. package/dist/tools/pre-review-code.d.ts.map +1 -0
  128. package/dist/tools/pre-review-code.js +159 -0
  129. package/dist/tools/pre-review-code.js.map +1 -0
  130. package/dist/tools/preserve-context.d.ts +27 -0
  131. package/dist/tools/preserve-context.d.ts.map +1 -0
  132. package/dist/tools/preserve-context.js +98 -0
  133. package/dist/tools/preserve-context.js.map +1 -0
  134. package/dist/tools/protect-files.d.ts +224 -0
  135. package/dist/tools/protect-files.d.ts.map +1 -0
  136. package/dist/tools/protect-files.js +286 -0
  137. package/dist/tools/protect-files.js.map +1 -0
  138. package/dist/tools/scan-security.d.ts +38 -0
  139. package/dist/tools/scan-security.d.ts.map +1 -0
  140. package/dist/tools/scan-security.js +237 -0
  141. package/dist/tools/scan-security.js.map +1 -0
  142. package/dist/tools/validate_packages.d.ts +8 -0
  143. package/dist/tools/validate_packages.d.ts.map +1 -0
  144. package/dist/tools/validate_packages.js +159 -0
  145. package/dist/tools/validate_packages.js.map +1 -0
  146. package/dist/types/auth.d.ts +18 -18
  147. package/dist/types/auth.d.ts.map +1 -1
  148. package/dist/types/auth.js +91 -36
  149. package/dist/types/auth.js.map +1 -1
  150. package/dist/types/context.d.ts +46 -0
  151. package/dist/types/context.d.ts.map +1 -0
  152. package/dist/types/context.js +17 -0
  153. package/dist/types/context.js.map +1 -0
  154. package/dist/types/file-protection.d.ts +50 -0
  155. package/dist/types/file-protection.d.ts.map +1 -0
  156. package/dist/types/file-protection.js +9 -0
  157. package/dist/types/file-protection.js.map +1 -0
  158. package/dist/utils/errorHandler.d.ts.map +1 -1
  159. package/dist/utils/errorHandler.js +2 -1
  160. package/dist/utils/errorHandler.js.map +1 -1
  161. package/package.json +23 -2
  162. package/dist/db/migrate.d.ts +0 -4
  163. package/dist/db/migrate.d.ts.map +0 -1
  164. package/dist/db/migrate.js +0 -34
  165. package/dist/db/migrate.js.map +0 -1
  166. package/dist/db/migrations/schema.d.ts +0 -1074
  167. package/dist/db/migrations/schema.d.ts.map +0 -1
  168. package/dist/db/migrations/schema.js +0 -160
  169. package/dist/db/migrations/schema.js.map +0 -1
  170. package/dist/db/schema.d.ts +0 -1576
  171. package/dist/db/schema.d.ts.map +0 -1
  172. package/dist/db/schema.js +0 -204
  173. package/dist/db/schema.js.map +0 -1
  174. package/dist/db/seed.d.ts +0 -4
  175. package/dist/db/seed.d.ts.map +0 -1
  176. package/dist/db/seed.js +0 -57
  177. package/dist/db/seed.js.map +0 -1
  178. package/dist/db/seedOAuth.d.ts +0 -4
  179. package/dist/db/seedOAuth.d.ts.map +0 -1
  180. package/dist/db/seedOAuth.js +0 -76
  181. package/dist/db/seedOAuth.js.map +0 -1
  182. package/dist/governance/config.d.ts +0 -66
  183. package/dist/governance/config.d.ts.map +0 -1
  184. package/dist/governance/config.js +0 -238
  185. package/dist/governance/config.js.map +0 -1
  186. package/dist/governance/detectors/hallucination.d.ts +0 -61
  187. package/dist/governance/detectors/hallucination.d.ts.map +0 -1
  188. package/dist/governance/detectors/hallucination.js +0 -338
  189. package/dist/governance/detectors/hallucination.js.map +0 -1
  190. package/dist/governance/detectors/inconsistency.d.ts +0 -99
  191. package/dist/governance/detectors/inconsistency.d.ts.map +0 -1
  192. package/dist/governance/detectors/inconsistency.js +0 -548
  193. package/dist/governance/detectors/inconsistency.js.map +0 -1
  194. package/dist/governance/detectors/toneDrift.d.ts +0 -63
  195. package/dist/governance/detectors/toneDrift.d.ts.map +0 -1
  196. package/dist/governance/detectors/toneDrift.js +0 -421
  197. package/dist/governance/detectors/toneDrift.js.map +0 -1
  198. package/dist/governance/detectors/unsafeReasoning.d.ts +0 -54
  199. package/dist/governance/detectors/unsafeReasoning.d.ts.map +0 -1
  200. package/dist/governance/detectors/unsafeReasoning.js +0 -473
  201. package/dist/governance/detectors/unsafeReasoning.js.map +0 -1
  202. package/dist/governance/evaluationEngine.d.ts +0 -112
  203. package/dist/governance/evaluationEngine.d.ts.map +0 -1
  204. package/dist/governance/evaluationEngine.js +0 -265
  205. package/dist/governance/evaluationEngine.js.map +0 -1
  206. package/dist/governance/intervention.d.ts +0 -81
  207. package/dist/governance/intervention.d.ts.map +0 -1
  208. package/dist/governance/intervention.js +0 -405
  209. package/dist/governance/intervention.js.map +0 -1
  210. package/dist/server/oauth/anomalyDetection.d.ts +0 -146
  211. package/dist/server/oauth/anomalyDetection.d.ts.map +0 -1
  212. package/dist/server/oauth/anomalyDetection.js +0 -405
  213. package/dist/server/oauth/anomalyDetection.js.map +0 -1
  214. package/dist/server/oauth/authServer.d.ts +0 -61
  215. package/dist/server/oauth/authServer.d.ts.map +0 -1
  216. package/dist/server/oauth/authServer.js +0 -283
  217. package/dist/server/oauth/authServer.js.map +0 -1
  218. package/dist/server/oauth/dpop.d.ts +0 -135
  219. package/dist/server/oauth/dpop.d.ts.map +0 -1
  220. package/dist/server/oauth/dpop.js +0 -338
  221. package/dist/server/oauth/dpop.js.map +0 -1
  222. package/dist/server/oauth/gdpr/consent.d.ts +0 -173
  223. package/dist/server/oauth/gdpr/consent.d.ts.map +0 -1
  224. package/dist/server/oauth/gdpr/consent.js +0 -388
  225. package/dist/server/oauth/gdpr/consent.js.map +0 -1
  226. package/dist/server/oauth/gdpr/dataPortability.d.ts +0 -214
  227. package/dist/server/oauth/gdpr/dataPortability.d.ts.map +0 -1
  228. package/dist/server/oauth/gdpr/dataPortability.js +0 -486
  229. package/dist/server/oauth/gdpr/dataPortability.js.map +0 -1
  230. package/dist/server/oauth/gdpr/index.d.ts +0 -103
  231. package/dist/server/oauth/gdpr/index.d.ts.map +0 -1
  232. package/dist/server/oauth/gdpr/index.js +0 -273
  233. package/dist/server/oauth/gdpr/index.js.map +0 -1
  234. package/dist/server/oauth/gdpr/rightToErasure.d.ts +0 -184
  235. package/dist/server/oauth/gdpr/rightToErasure.d.ts.map +0 -1
  236. package/dist/server/oauth/gdpr/rightToErasure.js +0 -527
  237. package/dist/server/oauth/gdpr/rightToErasure.js.map +0 -1
  238. package/dist/server/oauth/monitoring/securityMonitor.d.ts +0 -218
  239. package/dist/server/oauth/monitoring/securityMonitor.d.ts.map +0 -1
  240. package/dist/server/oauth/monitoring/securityMonitor.js +0 -615
  241. package/dist/server/oauth/monitoring/securityMonitor.js.map +0 -1
  242. package/dist/server/oauth/pkce.d.ts +0 -61
  243. package/dist/server/oauth/pkce.d.ts.map +0 -1
  244. package/dist/server/oauth/pkce.js +0 -157
  245. package/dist/server/oauth/pkce.js.map +0 -1
  246. package/dist/server/oauth/providers/base.d.ts +0 -147
  247. package/dist/server/oauth/providers/base.d.ts.map +0 -1
  248. package/dist/server/oauth/providers/base.js +0 -312
  249. package/dist/server/oauth/providers/base.js.map +0 -1
  250. package/dist/server/oauth/providers/github.d.ts +0 -55
  251. package/dist/server/oauth/providers/github.d.ts.map +0 -1
  252. package/dist/server/oauth/providers/github.js +0 -225
  253. package/dist/server/oauth/providers/github.js.map +0 -1
  254. package/dist/server/oauth/providers/google.d.ts +0 -49
  255. package/dist/server/oauth/providers/google.d.ts.map +0 -1
  256. package/dist/server/oauth/providers/google.js +0 -153
  257. package/dist/server/oauth/providers/google.js.map +0 -1
  258. package/dist/server/oauth/providers/index.d.ts +0 -9
  259. package/dist/server/oauth/providers/index.d.ts.map +0 -1
  260. package/dist/server/oauth/providers/index.js +0 -24
  261. package/dist/server/oauth/providers/index.js.map +0 -1
  262. package/dist/server/oauth/refreshTokenRotation.d.ts +0 -114
  263. package/dist/server/oauth/refreshTokenRotation.d.ts.map +0 -1
  264. package/dist/server/oauth/refreshTokenRotation.js +0 -344
  265. package/dist/server/oauth/refreshTokenRotation.js.map +0 -1
  266. package/dist/server/oauth/security.d.ts +0 -101
  267. package/dist/server/oauth/security.d.ts.map +0 -1
  268. package/dist/server/oauth/security.js +0 -268
  269. package/dist/server/oauth/security.js.map +0 -1
  270. package/dist/server/oauth/tokenEncryption.d.ts +0 -80
  271. package/dist/server/oauth/tokenEncryption.d.ts.map +0 -1
  272. package/dist/server/oauth/tokenEncryption.js +0 -218
  273. package/dist/server/oauth/tokenEncryption.js.map +0 -1
  274. package/dist/tools/sendMessage-complex-backup.d.ts +0 -6
  275. package/dist/tools/sendMessage-complex-backup.d.ts.map +0 -1
  276. package/dist/tools/sendMessage-complex-backup.js +0 -545
  277. package/dist/tools/sendMessage-complex-backup.js.map +0 -1
  278. package/dist/tools/sendMessage-revised.d.ts +0 -11
  279. package/dist/tools/sendMessage-revised.d.ts.map +0 -1
  280. package/dist/tools/sendMessage-revised.js +0 -429
  281. package/dist/tools/sendMessage-revised.js.map +0 -1
  282. package/dist/tools/sendMessage-truly-simple.d.ts +0 -8
  283. package/dist/tools/sendMessage-truly-simple.d.ts.map +0 -1
  284. package/dist/tools/sendMessage-truly-simple.js +0 -299
  285. package/dist/tools/sendMessage-truly-simple.js.map +0 -1
@@ -1,405 +0,0 @@
1
- /**
2
- * CouchLoop Behavioral Governance Layer - Intervention Engine
3
- *
4
- * Handles response blocking, modification, rewriting, and fallback responses
5
- * based on governance evaluation results
6
- */
7
- import { InterventionAction, RiskLevel } from './evaluationEngine.js';
8
- import { loadConfig } from './config.js';
9
- export class InterventionEngine {
10
- config;
11
- constructor(config) {
12
- this.config = config || loadConfig();
13
- }
14
- /**
15
- * Main intervention method - applies appropriate action based on evaluation
16
- */
17
- async intervene(action, originalResponse, evaluationResult) {
18
- switch (action) {
19
- case InterventionAction.APPROVE:
20
- return this.approve(originalResponse, evaluationResult);
21
- case InterventionAction.BLOCK:
22
- return this.block(originalResponse, evaluationResult);
23
- case InterventionAction.MODIFY:
24
- return this.modify(originalResponse, evaluationResult);
25
- case InterventionAction.FALLBACK:
26
- return this.fallback(originalResponse, evaluationResult);
27
- default:
28
- // Default to approval if unknown action
29
- return this.approve(originalResponse, evaluationResult);
30
- }
31
- }
32
- /**
33
- * Approve response without modification
34
- */
35
- approve(originalResponse, evaluation) {
36
- return {
37
- action: InterventionAction.APPROVE,
38
- originalResponse,
39
- finalResponse: originalResponse,
40
- modified: false,
41
- reason: 'Response approved - no governance issues detected',
42
- confidence: evaluation.confidence
43
- };
44
- }
45
- /**
46
- * Block response and provide safe alternative
47
- */
48
- block(originalResponse, evaluation) {
49
- let reason = 'Response blocked due to: ';
50
- const issues = [];
51
- // Compile blocking reasons
52
- if (evaluation.unsafeReasoning.detected) {
53
- issues.push('unsafe reasoning patterns');
54
- }
55
- if (evaluation.hallucination.detected) {
56
- issues.push('potential hallucination');
57
- }
58
- if (evaluation.inconsistency.detected) {
59
- issues.push('logical inconsistencies');
60
- }
61
- if (evaluation.toneDrift.detected) {
62
- issues.push('problematic tone drift');
63
- }
64
- reason += issues.join(', ');
65
- // Select appropriate fallback message
66
- let finalResponse;
67
- if (evaluation.overallRisk === RiskLevel.CRITICAL ||
68
- (evaluation.unsafeReasoning.detected && evaluation.unsafeReasoning.confidence > 0.8)) {
69
- // Crisis or high-risk situation
70
- finalResponse = this.config.fallbackResponses.crisis;
71
- }
72
- else {
73
- // General blocked response
74
- finalResponse = this.config.fallbackResponses.blocked;
75
- }
76
- return {
77
- action: InterventionAction.BLOCK,
78
- originalResponse,
79
- finalResponse,
80
- modified: true,
81
- reason,
82
- confidence: evaluation.confidence
83
- };
84
- }
85
- /**
86
- * Modify response to remove or soften problematic content
87
- */
88
- modify(originalResponse, evaluation) {
89
- let modifiedResponse = originalResponse;
90
- const modifications = [];
91
- // Apply modifications based on detected issues
92
- if (evaluation.hallucination.detected && evaluation.hallucination.patterns) {
93
- modifiedResponse = this.removeHallucinatedContent(modifiedResponse, evaluation.hallucination.patterns, modifications);
94
- }
95
- if (evaluation.unsafeReasoning.detected && evaluation.unsafeReasoning.patterns) {
96
- modifiedResponse = this.removeUnsafeContent(modifiedResponse, evaluation.unsafeReasoning.patterns, modifications);
97
- }
98
- if (evaluation.toneDrift.detected && evaluation.toneDrift.patterns) {
99
- modifiedResponse = this.neutralizeTone(modifiedResponse, modifications);
100
- }
101
- if (evaluation.inconsistency.detected && evaluation.inconsistency.patterns) {
102
- modifiedResponse = this.softenClaims(modifiedResponse, evaluation.inconsistency.patterns, modifications);
103
- }
104
- // If modifications were too extensive, use fallback
105
- if (this.tooMuchRemoved(originalResponse, modifiedResponse)) {
106
- return this.fallback(originalResponse, evaluation);
107
- }
108
- // Add modification prefix if configured
109
- if (modifiedResponse !== originalResponse && this.config.fallbackResponses.modified) {
110
- modifiedResponse = this.config.fallbackResponses.modified + '\n\n' + modifiedResponse;
111
- }
112
- return {
113
- action: InterventionAction.MODIFY,
114
- originalResponse,
115
- finalResponse: modifiedResponse,
116
- modified: true,
117
- reason: `Response modified to address: ${modifications.map(m => m.reason).join(', ')}`,
118
- modifications,
119
- confidence: evaluation.confidence
120
- };
121
- }
122
- /**
123
- * Replace with safe fallback response
124
- */
125
- fallback(originalResponse, evaluation) {
126
- // Select contextually appropriate fallback
127
- let finalResponse;
128
- let reason = 'Using fallback response due to: ';
129
- if (evaluation.overallRisk === RiskLevel.CRITICAL) {
130
- finalResponse = this.config.fallbackResponses.crisis;
131
- reason += 'critical safety concerns';
132
- }
133
- else if (evaluation.unsafeReasoning.detected) {
134
- finalResponse = this.config.fallbackResponses.crisis;
135
- reason += 'unsafe reasoning detected';
136
- }
137
- else if (evaluation.overallRisk === RiskLevel.HIGH) {
138
- finalResponse = this.config.fallbackResponses.blocked;
139
- reason += 'high risk content';
140
- }
141
- else {
142
- finalResponse = this.config.fallbackResponses.error;
143
- reason += 'multiple governance issues';
144
- }
145
- return {
146
- action: InterventionAction.FALLBACK,
147
- originalResponse,
148
- finalResponse,
149
- modified: true,
150
- reason,
151
- confidence: evaluation.confidence
152
- };
153
- }
154
- /**
155
- * Remove hallucinated content from response
156
- */
157
- removeHallucinatedContent(response, patterns, modifications) {
158
- let modified = response;
159
- for (const pattern of patterns) {
160
- // Extract the problematic phrase from the pattern description
161
- const match = pattern.match(/: "(.+)"/);
162
- if (match && match[1]) {
163
- const problematicPhrase = match[1];
164
- if (modified.includes(problematicPhrase)) {
165
- // Remove the sentence containing the problematic phrase
166
- const sentences = modified.split(/(?<=[.!?])\s+/);
167
- const filteredSentences = sentences.filter(s => !s.includes(problematicPhrase));
168
- if (filteredSentences.length < sentences.length) {
169
- modifications.push({
170
- type: 'removed',
171
- original: sentences.find(s => s.includes(problematicPhrase)) || problematicPhrase,
172
- modified: '',
173
- reason: 'hallucinated content'
174
- });
175
- modified = filteredSentences.join(' ');
176
- }
177
- }
178
- }
179
- }
180
- // Replace absolute certainty with hedged language
181
- const certaintyReplacements = [
182
- [/definitely will/gi, 'might'],
183
- [/absolutely certain/gi, 'possible'],
184
- [/guaranteed to/gi, 'may'],
185
- [/proven fact/gi, 'current understanding'],
186
- [/everyone knows/gi, 'it is commonly believed'],
187
- [/always works/gi, 'often helps'],
188
- [/never fails/gi, 'typically effective']
189
- ];
190
- for (const [pattern, replacement] of certaintyReplacements) {
191
- const before = modified;
192
- modified = modified.replace(pattern, replacement);
193
- if (before !== modified) {
194
- modifications.push({
195
- type: 'replaced',
196
- original: pattern.source,
197
- modified: replacement,
198
- reason: 'reduced false certainty'
199
- });
200
- }
201
- }
202
- return modified;
203
- }
204
- /**
205
- * Remove unsafe content from response
206
- */
207
- removeUnsafeContent(response, patterns, modifications) {
208
- let modified = response;
209
- for (const pattern of patterns) {
210
- // Extract the problematic phrase
211
- const match = pattern.match(/: "(.+)"/);
212
- if (match && match[1]) {
213
- const unsafePhrase = match[1];
214
- // Check if this is critical content that should be removed entirely
215
- if (pattern.includes('critical') || pattern.includes('Suicide') || pattern.includes('Self-harm')) {
216
- // Remove entire sentences containing critical unsafe content
217
- const sentences = modified.split(/(?<=[.!?])\s+/);
218
- const safeSentences = sentences.filter(s => !s.toLowerCase().includes(unsafePhrase.toLowerCase()));
219
- if (safeSentences.length < sentences.length) {
220
- modifications.push({
221
- type: 'removed',
222
- original: sentences.find(s => s.toLowerCase().includes(unsafePhrase.toLowerCase())) || unsafePhrase,
223
- modified: '',
224
- reason: 'critical safety concern'
225
- });
226
- modified = safeSentences.join(' ');
227
- }
228
- }
229
- else {
230
- // For non-critical unsafe content, try to soften or replace
231
- const safeAlternatives = {
232
- 'you should definitely': 'you might consider',
233
- 'you must': 'it could be helpful to',
234
- 'stop taking your medication': 'discuss your medication concerns with your doctor',
235
- "don't need therapy": 'therapy is a personal choice',
236
- 'ignore what they said': 'consider different perspectives',
237
- "don't tell anyone": 'sharing with trusted people can help'
238
- };
239
- for (const [unsafe, safe] of Object.entries(safeAlternatives)) {
240
- if (modified.toLowerCase().includes(unsafe)) {
241
- modified = modified.replace(new RegExp(unsafe, 'gi'), safe);
242
- modifications.push({
243
- type: 'replaced',
244
- original: unsafe,
245
- modified: safe,
246
- reason: 'unsafe advice neutralized'
247
- });
248
- }
249
- }
250
- }
251
- }
252
- }
253
- return modified;
254
- }
255
- /**
256
- * Neutralize problematic tone in response
257
- */
258
- neutralizeTone(response, modifications) {
259
- let modified = response;
260
- // Tone neutralization replacements
261
- const toneReplacements = [
262
- // Over-reassurance
263
- [/everything will be (fine|perfect|great)/gi, 'things may improve'],
264
- [/don't worry about anything/gi, "it's natural to have concerns"],
265
- [/absolutely no risk/gi, 'relatively low risk'],
266
- [/I promise/gi, 'I believe'],
267
- // Over-directive
268
- [/you must/gi, 'you might consider'],
269
- [/you have to/gi, 'it could help to'],
270
- [/you need to/gi, 'you may want to'],
271
- [/it's essential that you/gi, 'it could be beneficial to'],
272
- // Manipulative
273
- [/only I can help/gi, 'support is available'],
274
- [/you need me/gi, 'support can help'],
275
- [/trust me, not them/gi, 'consider various perspectives'],
276
- // Dependency-forming
277
- [/I'll always be here/gi, 'support is available'],
278
- [/come back to me anytime/gi, 'help is available when needed'],
279
- [/you can't do this without me/gi, 'support can be helpful']
280
- ];
281
- for (const [pattern, replacement] of toneReplacements) {
282
- const matches = modified.match(pattern);
283
- if (matches) {
284
- modified = modified.replace(pattern, replacement);
285
- modifications.push({
286
- type: 'neutralized',
287
- original: matches[0],
288
- modified: replacement,
289
- reason: 'tone neutralization'
290
- });
291
- }
292
- }
293
- // Remove excessive emotional language
294
- const emotionalWords = /\b(amazing|terrible|horrible|perfect|disaster|catastrophe|miracle)\b/gi;
295
- const emotionalMatches = modified.match(emotionalWords);
296
- if (emotionalMatches && emotionalMatches.length > 2) {
297
- // Replace with more neutral terms
298
- modified = modified
299
- .replace(/\bamazing\b/gi, 'positive')
300
- .replace(/\bterrible\b/gi, 'difficult')
301
- .replace(/\bhorrible\b/gi, 'challenging')
302
- .replace(/\bperfect\b/gi, 'good')
303
- .replace(/\bdisaster\b/gi, 'setback')
304
- .replace(/\bcatastrophe\b/gi, 'difficulty')
305
- .replace(/\bmiracle\b/gi, 'improvement');
306
- modifications.push({
307
- type: 'neutralized',
308
- original: 'excessive emotional language',
309
- modified: 'neutral terms',
310
- reason: 'emotional de-escalation'
311
- });
312
- }
313
- return modified;
314
- }
315
- /**
316
- * Soften claims to address inconsistencies
317
- */
318
- softenClaims(response, patterns, modifications) {
319
- let modified = response;
320
- // Add hedging language to strong claims
321
- const hedgeReplacements = [
322
- [/\bis\b/gi, 'may be'],
323
- [/\bare\b/gi, 'might be'],
324
- [/\bwill\b/gi, 'could'],
325
- [/\balways\b/gi, 'often'],
326
- [/\bnever\b/gi, 'rarely'],
327
- [/\bdefinitely\b/gi, 'probably'],
328
- [/\bcertainly\b/gi, 'likely']
329
- ];
330
- // Only apply hedging to sentences mentioned in contradiction patterns
331
- for (const pattern of patterns) {
332
- if (pattern.includes('Contradiction') || pattern.includes('Reversal')) {
333
- // Extract the current claim from the pattern
334
- const match = pattern.match(/Current: "(.+?)"/);
335
- if (match && match[1]) {
336
- const claim = match[1];
337
- // Find and soften this claim in the response
338
- if (modified.includes(claim)) {
339
- let softenedClaim = claim;
340
- for (const [original, hedged] of hedgeReplacements) {
341
- softenedClaim = softenedClaim.replace(original, hedged);
342
- }
343
- if (softenedClaim !== claim) {
344
- modified = modified.replace(claim, softenedClaim);
345
- modifications.push({
346
- type: 'softened',
347
- original: claim,
348
- modified: softenedClaim,
349
- reason: 'addressing inconsistency'
350
- });
351
- }
352
- }
353
- }
354
- }
355
- }
356
- return modified;
357
- }
358
- /**
359
- * Check if too much content was removed
360
- */
361
- tooMuchRemoved(original, modified) {
362
- const originalLength = original.length;
363
- const modifiedLength = modified.length;
364
- // If more than 60% was removed, use fallback instead
365
- const removalRatio = (originalLength - modifiedLength) / originalLength;
366
- return removalRatio > 0.6;
367
- }
368
- /**
369
- * Apply intervention based on evaluation without explicit action
370
- */
371
- async autoIntervene(originalResponse, evaluationResult) {
372
- // Use the recommended action from evaluation
373
- const action = evaluationResult.recommendedAction;
374
- return this.intervene(action, originalResponse, evaluationResult);
375
- }
376
- /**
377
- * Generate explanation for intervention
378
- */
379
- generateExplanation(result) {
380
- if (!result.modified) {
381
- return 'Response approved without modifications.';
382
- }
383
- let explanation = `Governance intervention applied (${result.action}): ${result.reason}\n`;
384
- if (result.modifications && result.modifications.length > 0) {
385
- explanation += '\nModifications made:\n';
386
- for (const mod of result.modifications) {
387
- explanation += `- ${mod.type}: ${mod.reason}\n`;
388
- if (mod.original && mod.modified) {
389
- explanation += ` From: "${mod.original}"\n`;
390
- explanation += ` To: "${mod.modified}"\n`;
391
- }
392
- }
393
- }
394
- explanation += `\nConfidence: ${(result.confidence * 100).toFixed(1)}%`;
395
- return explanation;
396
- }
397
- /**
398
- * Update configuration
399
- */
400
- updateConfig(config) {
401
- this.config = config;
402
- }
403
- }
404
- export default InterventionEngine;
405
- //# sourceMappingURL=intervention.js.map
@@ -1 +0,0 @@
1
- {"version":3,"file":"intervention.js","sourceRoot":"","sources":["../../src/governance/intervention.ts"],"names":[],"mappings":"AAAA;;;;;GAKG;AAEH,OAAO,EAEL,kBAAkB,EAClB,SAAS,EACV,MAAM,uBAAuB,CAAC;AAC/B,OAAO,EAAE,UAAU,EAAyB,MAAM,aAAa,CAAC;AAmBhE,MAAM,OAAO,kBAAkB;IACrB,MAAM,CAAmB;IAEjC,YAAY,MAAyB;QACnC,IAAI,CAAC,MAAM,GAAG,MAAM,IAAI,UAAU,EAAE,CAAC;IACvC,CAAC;IAED;;OAEG;IACH,KAAK,CAAC,SAAS,CACb,MAA0B,EAC1B,gBAAwB,EACxB,gBAAkC;QAElC,QAAQ,MAAM,EAAE,CAAC;YACf,KAAK,kBAAkB,CAAC,OAAO;gBAC7B,OAAO,IAAI,CAAC,OAAO,CAAC,gBAAgB,EAAE,gBAAgB,CAAC,CAAC;YAE1D,KAAK,kBAAkB,CAAC,KAAK;gBAC3B,OAAO,IAAI,CAAC,KAAK,CAAC,gBAAgB,EAAE,gBAAgB,CAAC,CAAC;YAExD,KAAK,kBAAkB,CAAC,MAAM;gBAC5B,OAAO,IAAI,CAAC,MAAM,CAAC,gBAAgB,EAAE,gBAAgB,CAAC,CAAC;YAEzD,KAAK,kBAAkB,CAAC,QAAQ;gBAC9B,OAAO,IAAI,CAAC,QAAQ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,MAAM,CAAC,MAAM,IAAI,CAAC;QAE3F,IAAI,MAAM,CAAC,aAAa,IAAI,MAAM,CAAC,aAAa,CAAC,MAAM,GAAG,CAAC,EAAE,CAAC;YAC5D,WAAW,IAAI,yBAAyB,CAAC;YACzC,KAAK,MAAM,GAAG,IAAI,MAAM,CAAC,aAAa,EAAE,CAAC;gBACvC,WAAW,IAAI,KAAK,GAAG,CAAC,IAAI,KAAK,GAAG,CAAC,MAAM,IAAI,CAAC;gBAChD,IAAI,GAAG,CAAC,QAAQ,IAAI,GAAG,CAAC,QAAQ,EAAE,CAAC;oBACjC,WAAW,IAAI,YAAY,GAAG,CAAC,QAAQ,KAAK,CAAC;oBAC7C,WAAW,IAAI,UAAU,GAAG,CAAC,QAAQ,KAAK,CAAC;gBAC7C,CAAC;YACH,CAAC;QACH,CAAC;QAED,WAAW,IAAI,iBAAiB,CAAC,MAAM,CAAC,UAAU,GAAG,GAAG,CAAC,CAAC,OAAO,CAAC,CAAC,CAAC,GAAG,CAAC;QAExE,OAAO,WAAW,CAAC;IACrB,CAAC;IAED;;OAEG;IACH,YAAY,CAAC,MAAwB;QACnC,IAAI,CAAC,MAAM,GAAG,MAAM,CAAC;IACvB,CAAC;CACF;AAED,eAAe,kBAAkB,CAAC"}
@@ -1,146 +0,0 @@
1
- /**
2
- * Authentication request context
3
- */
4
- export interface AuthRequest {
5
- userId?: string;
6
- clientId: string;
7
- ip: string;
8
- userAgent?: string;
9
- fingerprint?: string;
10
- timestamp: Date;
11
- method: 'login' | 'refresh' | 'logout' | 'register';
12
- success: boolean;
13
- metadata?: {
14
- country?: string;
15
- city?: string;
16
- asn?: string;
17
- isp?: string;
18
- deviceType?: string;
19
- browser?: string;
20
- os?: string;
21
- };
22
- }
23
- /**
24
- * Anomaly score and action
25
- */
26
- export interface AnomalyScore {
27
- composite: number;
28
- details: {
29
- ip: number;
30
- geo: number;
31
- device: number;
32
- time: number;
33
- velocity: number;
34
- pattern: number;
35
- };
36
- action: 'allow' | 'challenge' | 'deny';
37
- reasons: string[];
38
- }
39
- /**
40
- * Risk factors configuration
41
- */
42
- export interface RiskFactors {
43
- vpnWeight: number;
44
- torWeight: number;
45
- proxyWeight: number;
46
- newDeviceWeight: number;
47
- newLocationWeight: number;
48
- impossibleTravelWeight: number;
49
- bruteForceWeight: number;
50
- timeAnomalyWeight: number;
51
- }
52
- /**
53
- * Anomaly Detection System
54
- * Detects suspicious authentication patterns and potential attacks
55
- */
56
- export declare class AnomalyDetector {
57
- private userProfiles;
58
- private ipReputation;
59
- private readonly MAX_FAILED_ATTEMPTS;
60
- private readonly IMPOSSIBLE_TRAVEL_SPEED;
61
- private readonly TIME_WINDOW;
62
- private readonly riskFactors;
63
- /**
64
- * Analyze authentication request for anomalies
65
- */
66
- detectAnomalies(request: AuthRequest): Promise<AnomalyScore>;
67
- /**
68
- * Check IP reputation
69
- */
70
- private checkIPReputation;
71
- /**
72
- * Check geolocation anomaly
73
- */
74
- private checkGeoAnomaly;
75
- /**
76
- * Check device anomaly
77
- */
78
- private checkDeviceAnomaly;
79
- /**
80
- * Check time-based anomaly
81
- */
82
- private checkTimeAnomaly;
83
- /**
84
- * Check request velocity
85
- */
86
- private checkVelocity;
87
- /**
88
- * Check behavioral patterns
89
- */
90
- private checkBehavioralPattern;
91
- /**
92
- * Determine action based on score and context
93
- */
94
- private determineAction;
95
- /**
96
- * Query threat intelligence feeds
97
- */
98
- private queryThreatIntelligence;
99
- /**
100
- * Check if IP is in private range
101
- */
102
- private isPrivateIP;
103
- /**
104
- * Calculate distance between two coordinates (Haversine formula)
105
- */
106
- private calculateDistance;
107
- private toRad;
108
- /**
109
- * Calculate request entropy (randomness)
110
- */
111
- private calculateRequestEntropy;
112
- /**
113
- * Get or create user profile
114
- */
115
- private getUserProfile;
116
- /**
117
- * Update user profile with successful authentication
118
- */
119
- private updateUserProfile;
120
- /**
121
- * Count recent requests from IP
122
- */
123
- private countRecentRequests;
124
- /**
125
- * Log anomaly to database
126
- */
127
- private logAnomaly;
128
- /**
129
- * Get risk score for a user
130
- */
131
- getUserRiskScore(userId: string): Promise<number>;
132
- /**
133
- * Reset user profile (after password reset, etc.)
134
- */
135
- resetUserProfile(userId: string): void;
136
- /**
137
- * Get statistics
138
- */
139
- getStats(): {
140
- totalProfiles: number;
141
- totalIPs: number;
142
- highRiskUsers: number;
143
- };
144
- }
145
- export declare const anomalyDetector: AnomalyDetector;
146
- //# sourceMappingURL=anomalyDetection.d.ts.map
@@ -1 +0,0 @@
1
- {"version":3,"file":"anomalyDetection.d.ts","sourceRoot":"","sources":["../../../src/server/oauth/anomalyDetection.ts"],"names":[],"mappings":"AAIA;;GAEG;AACH,MAAM,WAAW,WAAW;IAC1B,MAAM,CAAC,EAAE,MAAM,CAAC;IAChB,QAAQ,EAAE,MAAM,CAAC;IACjB,EAAE,EAAE,MAAM,CAAC;IACX,SAAS,CAAC,EAAE,MAAM,CAAC;IACnB,WAAW,CAAC,EAAE,MAAM,CAAC;IACrB,SAAS,EAAE,IAAI,CAAC;IAChB,MAAM,EAAE,OAAO,GAAG,SAAS,GAAG,QAAQ,GAAG,UAAU,CAAC;IACpD,OAAO,EAAE,OAAO,CAAC;IACjB,QAAQ,CAAC,EAAE;QACT,OAAO,CAAC,EAAE,MAAM,CAAC;QACjB,IAAI,CAAC,EAAE,MAAM,CAAC;QACd,GAAG,CAAC,EAAE,MAAM,CAAC;QACb,GAAG,CAAC,EAAE,MAAM,CAAC;QACb,UAAU,CAAC,EAAE,MAAM,CAAC;QACpB,OAAO,CAAC,EAAE,MAAM,CAAC;QACjB,EAAE,CAAC,EAAE,MAAM,CAAC;KACb,CAAC;CACH;AAED;;GAEG;AACH,MAAM,WAAW,YAAY;IAC3B,SAAS,EAAE,MAAM,CAAC;IAClB,OAAO,EAAE;QACP,EAAE,EAAE,MAAM,CAAC;QACX,GAAG,EAAE,MAAM,CAAC;QACZ,MAAM,EAAE,MAAM,CAAC;QACf,IAAI,EAAE,MAAM,CAAC;QACb,QAAQ,EAAE,MA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