@yagyeshvyas/vibeguard 1.3.0 → 1.11.1

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 (167) hide show
  1. package/.vibeguard-mcp.example.json +51 -0
  2. package/CHANGELOG.md +604 -216
  3. package/MCP-FIREWALL.md +296 -0
  4. package/README.md +355 -31
  5. package/bin/cli.js +355 -27
  6. package/bin/cmd-agent-monitor.js +45 -0
  7. package/bin/cmd-agent-risk.js +64 -0
  8. package/bin/cmd-gif.js +62 -0
  9. package/bin/cmd-git-scan.js +74 -0
  10. package/bin/cmd-llm-proxy.js +164 -0
  11. package/bin/cmd-mcp-firewall.js +191 -0
  12. package/bin/cmd-pentest.js +103 -0
  13. package/bin/cmd-platform.js +137 -0
  14. package/bin/cmd-pr-scan.js +135 -0
  15. package/bin/cmd-sbom-diff.js +38 -0
  16. package/bin/cmd-skill-scan.js +35 -0
  17. package/docs/AISVS.md +25 -0
  18. package/docs/ARCHITECTURE.md +81 -0
  19. package/docs/INSTALLATION.md +139 -0
  20. package/docs/WINDOWS.md +74 -0
  21. package/docs/security-policy.strict.example.json +134 -0
  22. package/docs/security-policy.strict.example.pub.pem +3 -0
  23. package/package.json +45 -7
  24. package/src/.vibeguard/cache/file-hashes.json +60 -0
  25. package/src/.vibeguard/latest-snapshot.json +36 -0
  26. package/src/.vibeguard/mcp-pins.json +1 -0
  27. package/src/.vibeguard/snapshots/2026-08-05T16-23-50-635Z/auto.js +428 -0
  28. package/src/.vibeguard/snapshots/2026-08-05T16-23-50-635Z/daemon.js +344 -0
  29. package/src/.vibeguard/snapshots/2026-08-05T16-23-50-635Z/dashboard.js +279 -0
  30. package/src/.vibeguard/snapshots/2026-08-05T16-23-50-635Z/engine.js +297 -0
  31. package/src/.vibeguard/snapshots/2026-08-05T16-23-50-635Z/hook.js +151 -0
  32. package/src/.vibeguard/snapshots/2026-08-05T16-23-50-635Z/install.js +299 -0
  33. package/src/.vibeguard/snapshots/2026-08-05T16-23-50-635Z/manifest.json +65 -0
  34. package/src/.vibeguard/snapshots/2026-08-05T16-23-50-635Z/mcp-server.js +2051 -0
  35. package/src/.vibeguard/snapshots/2026-08-05T16-23-50-635Z/plugin.js +168 -0
  36. package/src/.vibeguard/snapshots/2026-08-05T16-23-50-635Z/pre-deploy.js +362 -0
  37. package/src/.vibeguard/snapshots/2026-08-05T16-23-50-635Z/repl.js +150 -0
  38. package/src/.vibeguard/snapshots/2026-08-05T16-23-50-635Z/rules.js +1611 -0
  39. package/src/.vibeguard/snapshots/2026-08-05T16-23-50-635Z/sbom.js +164 -0
  40. package/src/.vibeguard/snapshots/2026-08-05T16-23-50-635Z/shell-guard.js +292 -0
  41. package/src/.vibeguard/snapshots/2026-08-05T16-23-50-635Z/slopsquat.js +144 -0
  42. package/src/.vibeguard/snapshots/2026-08-05T16-23-50-635Z/trace.js +220 -0
  43. package/src/.vibeguard/snapshots/2026-08-05T16-26-26-074Z/auto.js +428 -0
  44. package/src/.vibeguard/snapshots/2026-08-05T16-26-26-074Z/daemon.js +344 -0
  45. package/src/.vibeguard/snapshots/2026-08-05T16-26-26-074Z/engine.js +297 -0
  46. package/src/.vibeguard/snapshots/2026-08-05T16-26-26-074Z/install.js +299 -0
  47. package/src/.vibeguard/snapshots/2026-08-05T16-26-26-074Z/manifest.json +37 -0
  48. package/src/.vibeguard/snapshots/2026-08-05T16-26-26-074Z/mcp-server.js +2055 -0
  49. package/src/.vibeguard/snapshots/2026-08-05T16-26-26-074Z/plugin.js +168 -0
  50. package/src/.vibeguard/snapshots/2026-08-05T16-26-26-074Z/pre-deploy.js +362 -0
  51. package/src/.vibeguard/snapshots/2026-08-05T16-26-26-074Z/trace.js +220 -0
  52. package/src/.vibeguard/snapshots/2026-08-05T16-35-06-272Z/auto.js +428 -0
  53. package/src/.vibeguard/snapshots/2026-08-05T16-35-06-272Z/daemon.js +344 -0
  54. package/src/.vibeguard/snapshots/2026-08-05T16-35-06-272Z/dashboard.js +279 -0
  55. package/src/.vibeguard/snapshots/2026-08-05T16-35-06-272Z/engine.js +297 -0
  56. package/src/.vibeguard/snapshots/2026-08-05T16-35-06-272Z/hook.js +151 -0
  57. package/src/.vibeguard/snapshots/2026-08-05T16-35-06-272Z/install.js +299 -0
  58. package/src/.vibeguard/snapshots/2026-08-05T16-35-06-272Z/manifest.json +65 -0
  59. package/src/.vibeguard/snapshots/2026-08-05T16-35-06-272Z/mcp-server.js +2055 -0
  60. package/src/.vibeguard/snapshots/2026-08-05T16-35-06-272Z/plugin.js +168 -0
  61. package/src/.vibeguard/snapshots/2026-08-05T16-35-06-272Z/pre-deploy.js +362 -0
  62. package/src/.vibeguard/snapshots/2026-08-05T16-35-06-272Z/repl.js +150 -0
  63. package/src/.vibeguard/snapshots/2026-08-05T16-35-06-272Z/rules.js +1611 -0
  64. package/src/.vibeguard/snapshots/2026-08-05T16-35-06-272Z/sbom.js +164 -0
  65. package/src/.vibeguard/snapshots/2026-08-05T16-35-06-272Z/shell-guard.js +292 -0
  66. package/src/.vibeguard/snapshots/2026-08-05T16-35-06-272Z/slopsquat.js +144 -0
  67. package/src/.vibeguard/snapshots/2026-08-05T16-35-06-272Z/trace.js +220 -0
  68. package/src/.vibeguard/snapshots/2026-08-05T18-08-40-613Z/auto.js +428 -0
  69. package/src/.vibeguard/snapshots/2026-08-05T18-08-40-613Z/daemon.js +344 -0
  70. package/src/.vibeguard/snapshots/2026-08-05T18-08-40-613Z/dashboard.js +279 -0
  71. package/src/.vibeguard/snapshots/2026-08-05T18-08-40-613Z/engine.js +297 -0
  72. package/src/.vibeguard/snapshots/2026-08-05T18-08-40-613Z/hook.js +151 -0
  73. package/src/.vibeguard/snapshots/2026-08-05T18-08-40-613Z/install.js +299 -0
  74. package/src/.vibeguard/snapshots/2026-08-05T18-08-40-613Z/manifest.json +65 -0
  75. package/src/.vibeguard/snapshots/2026-08-05T18-08-40-613Z/mcp-server.js +2054 -0
  76. package/src/.vibeguard/snapshots/2026-08-05T18-08-40-613Z/plugin.js +168 -0
  77. package/src/.vibeguard/snapshots/2026-08-05T18-08-40-613Z/pre-deploy.js +362 -0
  78. package/src/.vibeguard/snapshots/2026-08-05T18-08-40-613Z/repl.js +150 -0
  79. package/src/.vibeguard/snapshots/2026-08-05T18-08-40-613Z/rules.js +1611 -0
  80. package/src/.vibeguard/snapshots/2026-08-05T18-08-40-613Z/sbom.js +164 -0
  81. package/src/.vibeguard/snapshots/2026-08-05T18-08-40-613Z/shell-guard.js +292 -0
  82. package/src/.vibeguard/snapshots/2026-08-05T18-08-40-613Z/slopsquat.js +144 -0
  83. package/src/.vibeguard/snapshots/2026-08-05T18-08-40-613Z/trace.js +220 -0
  84. package/src/.vibeguard/snapshots/2026-08-05T18-11-32-644Z/auto.js +428 -0
  85. package/src/.vibeguard/snapshots/2026-08-05T18-11-32-644Z/daemon.js +344 -0
  86. package/src/.vibeguard/snapshots/2026-08-05T18-11-32-644Z/engine.js +297 -0
  87. package/src/.vibeguard/snapshots/2026-08-05T18-11-32-644Z/install.js +299 -0
  88. package/src/.vibeguard/snapshots/2026-08-05T18-11-32-644Z/manifest.json +37 -0
  89. package/src/.vibeguard/snapshots/2026-08-05T18-11-32-644Z/mcp-server.js +2055 -0
  90. package/src/.vibeguard/snapshots/2026-08-05T18-11-32-644Z/plugin.js +168 -0
  91. package/src/.vibeguard/snapshots/2026-08-05T18-11-32-644Z/pre-deploy.js +362 -0
  92. package/src/.vibeguard/snapshots/2026-08-05T18-11-32-644Z/trace.js +220 -0
  93. package/src/.vibeguard/snapshots/2026-08-05T18-16-47-635Z/auto.js +428 -0
  94. package/src/.vibeguard/snapshots/2026-08-05T18-16-47-635Z/daemon.js +344 -0
  95. package/src/.vibeguard/snapshots/2026-08-05T18-16-47-635Z/engine.js +297 -0
  96. package/src/.vibeguard/snapshots/2026-08-05T18-16-47-635Z/manifest.json +33 -0
  97. package/src/.vibeguard/snapshots/2026-08-05T18-16-47-635Z/mcp-server.js +2055 -0
  98. package/src/.vibeguard/snapshots/2026-08-05T18-16-47-635Z/rules.js +1611 -0
  99. package/src/.vibeguard/snapshots/2026-08-05T18-16-47-635Z/shell-guard.js +292 -0
  100. package/src/.vibeguard/snapshots/2026-08-05T18-16-47-635Z/trace.js +220 -0
  101. package/src/.vibeguard-baseline.json +5226 -0
  102. package/src/action-guard.js +84 -6
  103. package/src/agent-monitor.js +262 -0
  104. package/src/agent-policy.js +642 -0
  105. package/src/agent-risk.js +271 -0
  106. package/src/agentic.js +1513 -0
  107. package/src/ast.js +165 -7
  108. package/src/audit-trail.js +212 -67
  109. package/src/auto.js +427 -427
  110. package/src/autofix.js +11 -4
  111. package/src/bundles.js +198 -0
  112. package/src/daemon.js +343 -343
  113. package/src/dashboard.js +278 -278
  114. package/src/engine.js +3 -0
  115. package/src/entropy.js +187 -0
  116. package/src/firewall.js +19 -3
  117. package/src/gif.js +134 -0
  118. package/src/gitscan.js +282 -0
  119. package/src/hook.js +151 -151
  120. package/src/injection-decode.js +136 -0
  121. package/src/install.js +298 -280
  122. package/src/integrity.js +16 -1
  123. package/src/integrity.json +43 -13
  124. package/src/interceptor.js +12 -9
  125. package/src/llm-proxy.js +986 -0
  126. package/src/mcp-audit.js +61 -5
  127. package/src/mcp-firewall.js +971 -0
  128. package/src/mcp-server.js +2500 -2047
  129. package/src/obfuscation.js +315 -0
  130. package/src/owasp-agentic.js +314 -0
  131. package/src/pentest.js +589 -0
  132. package/src/pii.js +4 -0
  133. package/src/platform/aisvs.js +38 -0
  134. package/src/platform/approval.js +187 -0
  135. package/src/platform/canonical.js +73 -0
  136. package/src/platform/contracts.js +158 -0
  137. package/src/platform/delegation.js +73 -0
  138. package/src/platform/http-gateway.js +177 -0
  139. package/src/platform/identity.js +253 -0
  140. package/src/platform/jwt.js +86 -0
  141. package/src/platform/mcp-http-bridge.js +199 -0
  142. package/src/platform/memory-guard.js +64 -0
  143. package/src/platform/os-sandbox.js +144 -0
  144. package/src/platform/policy.js +332 -0
  145. package/src/platform/sandbox-manager.js +92 -0
  146. package/src/platform/signed-audit.js +357 -0
  147. package/src/platform/ssrf.js +129 -0
  148. package/src/plugin.js +182 -168
  149. package/src/pre-deploy.js +170 -55
  150. package/src/privacy-audit.js +272 -272
  151. package/src/repl.js +150 -150
  152. package/src/report.js +7 -1
  153. package/src/routes.js +1210 -0
  154. package/src/rules-2026.js +1407 -0
  155. package/src/rules-pack.js +3068 -3019
  156. package/src/rules.js +376 -60
  157. package/src/sandbox.js +200 -10
  158. package/src/sbom.js +193 -164
  159. package/src/scanner.js +189 -33
  160. package/src/shell-guard.js +11 -0
  161. package/src/skill-scan.js +477 -0
  162. package/src/slopsquat.js +144 -144
  163. package/src/surface.js +408 -0
  164. package/src/taint-ast.js +204 -37
  165. package/src/taint.js +15 -3
  166. package/src/tokenlean.js +579 -0
  167. package/src/trace.js +220 -220
package/src/trace.js CHANGED
@@ -1,220 +1,220 @@
1
- 'use strict';
2
-
3
- /*
4
- * Prompt tracing — link findings back to the AI prompt that generated them.
5
- *
6
- * Reads Cursor (.cursor/), Claude Code, and Windsurf session history files.
7
- * Scans for security-relevant behavior patterns in AI agent transcripts.
8
- *
9
- * Two capabilities:
10
- * 1. Trace a finding back to the AI prompt that likely generated the code
11
- * 2. Behavior analysis: detect blind approvals, high delegation, risky patterns
12
- */
13
-
14
- const fs = require('fs');
15
- const path = require('path');
16
-
17
- const SESSION_DIRS = [
18
- '.cursor',
19
- '.cursor/projects',
20
- '.windsurf',
21
- '.claude',
22
- '.continue',
23
- ];
24
-
25
- function findSessionFiles(root) {
26
- const files = [];
27
- for (const dir of SESSION_DIRS) {
28
- const absDir = path.join(root, dir);
29
- if (!fs.existsSync(absDir)) continue;
30
- try {
31
- walkAndCollect(absDir, files, /\.(?:json|jsonl|md|txt)$/i);
32
- } catch {}
33
- }
34
- return files;
35
- }
36
-
37
- function walkAndCollect(dir, out, extRe) {
38
- let entries;
39
- try {
40
- entries = fs.readdirSync(dir, { withFileTypes: true });
41
- } catch { return; }
42
- for (const e of entries) {
43
- const full = path.join(dir, e.name);
44
- if (e.isDirectory()) walkAndCollect(full, out, extRe);
45
- else if (e.isFile() && extRe.test(e.name)) out.push(full);
46
- }
47
- }
48
-
49
- function parseSession(filepath) {
50
- const content = fs.readFileSync(filepath, 'utf8');
51
- const messages = [];
52
-
53
- // Try JSONL format (one JSON object per line)
54
- if (filepath.endsWith('.jsonl')) {
55
- for (const line of content.split('\n')) {
56
- try {
57
- const obj = JSON.parse(line);
58
- if (obj.role || obj.type || obj.message) messages.push(obj);
59
- } catch {}
60
- }
61
- }
62
-
63
- // Try JSON array
64
- if (messages.length === 0 && filepath.endsWith('.json')) {
65
- try {
66
- const arr = JSON.parse(content);
67
- if (Array.isArray(arr)) messages.push(...arr);
68
- else messages.push(arr);
69
- } catch {}
70
- }
71
-
72
- // Try markdown/text (split by user/assistant markers)
73
- if (messages.length === 0) {
74
- const parts = content.split(/^(?:User|Human|Assistant|AI|>>>)[:\s]/m);
75
- parts.forEach((p, i) => {
76
- if (p.trim()) messages.push({ role: i % 2 === 0 ? 'user' : 'assistant', content: p.trim() });
77
- });
78
- }
79
-
80
- return { file: filepath, messages };
81
- }
82
-
83
- const RISKY_BEHAVIORS = [
84
- {
85
- id: 'behavior.blind-approval',
86
- re: /(?:yes|ok|sure|go ahead|do it|approve|sounds good|perfect|great)\s*[!.?\s]*$/i,
87
- risk: 'medium',
88
- desc: 'Blind approval of AI suggestion without review',
89
- },
90
- {
91
- id: 'behavior.high-delegation',
92
- re: /(?:do\s+(?:all|everything|the\s+whole)|build\s+(?:the\s+entire|all)|write\s+(?:all|every|the\s+complete)|implement\s+(?:all|everything|the\s+entire))/i,
93
- risk: 'medium',
94
- desc: 'High delegation — asking AI to do everything at once reduces review quality',
95
- },
96
- {
97
- id: 'behavior.skip-tests',
98
- re: /(?:skip|don'?t\s+need|no\s+need\s+for)\s+(?:tests?|testing|validation)/i,
99
- risk: 'high',
100
- desc: 'Requesting code without tests',
101
- },
102
- {
103
- id: 'behavior.skip-auth',
104
- re: /(?:skip|don'?t\s+(?:need|worry\s+about)|no\s+need\s+for)\s+(?:auth|authentication|authorization|security)/i,
105
- risk: 'high',
106
- desc: 'Explicitly requesting to skip authentication/security',
107
- },
108
- {
109
- id: 'behavior.hardcode-secrets',
110
- re: /(?:just\s+)?(?:hardcode|hard-code|put\s+(?:the\s+)?(?:password|secret|token|key|api\s+key)\s+(?:directly\s+)?(?:in|here))/i,
111
- risk: 'high',
112
- desc: 'Asking AI to hardcode secrets',
113
- },
114
- {
115
- id: 'behavior.disable-security',
116
- re: /(?:disable|turn\s+off|remove|skip|bypass)\s+(?:security|validation|sanitization|escaping|CORS|helmet|CSRF)/i,
117
- risk: 'high',
118
- desc: 'Asking AI to disable security features',
119
- },
120
- {
121
- id: 'behavior.trust-input',
122
- re: /(?:trust\s+the\s+(?:user|client|input)|user\s+input\s+is\s+(?:safe|trusted|fine)|don'?t\s+validate)/i,
123
- risk: 'high',
124
- desc: 'Expressing trust in user/client input without validation',
125
- },
126
- {
127
- id: 'behavior.excessive-iter',
128
- re: /(?:again|redo|try\s+again|different\s+approach|start\s+over)/i,
129
- risk: 'low',
130
- desc: 'Excessive iteration on AI output — possible degradation of review quality',
131
- threshold: 10,
132
- },
133
- ];
134
-
135
- function analyzeBehavior(messages) {
136
- const behaviors = [];
137
- let userMessages = 0;
138
- let approvals = 0;
139
- let iterations = 0;
140
-
141
- for (const msg of messages) {
142
- const role = msg.role || msg.type || '';
143
- const content = msg.content || msg.message || msg.text || '';
144
- if (typeof content !== 'string') continue;
145
- if (!/user|human/i.test(role)) continue;
146
- userMessages++;
147
-
148
- for (const b of RISKY_BEHAVIORS) {
149
- if (b.re.test(content)) {
150
- if (b.id === 'behavior.blind-approval') approvals++;
151
- if (b.id === 'behavior.excessive-iter') iterations++;
152
- if (b.id === 'behavior.excessive-iter' && iterations < (b.threshold || 10)) continue;
153
- behaviors.push({
154
- id: b.id,
155
- risk: b.risk,
156
- message: b.desc,
157
- snippet: content.slice(0, 200),
158
- });
159
- }
160
- }
161
- }
162
-
163
- const delegationRatio = userMessages > 0 ? approvals / userMessages : 0;
164
- const risk = delegationRatio > 0.6 ? 'high' : delegationRatio > 0.3 ? 'medium' : 'low';
165
-
166
- return {
167
- totalUserMessages: userMessages,
168
- approvals,
169
- iterations,
170
- delegationRatio: Math.round(delegationRatio * 100) / 100,
171
- risk,
172
- behaviors,
173
- recommendation:
174
- risk === 'high'
175
- ? 'High blind-approval ratio — review AI suggestions more carefully before accepting'
176
- : risk === 'medium'
177
- ? 'Moderate delegation — ensure each AI suggestion is reviewed'
178
- : 'Good review discipline — keep validating AI output',
179
- };
180
- }
181
-
182
- function traceFindingsToPrompts(findings, sessions) {
183
- const traces = [];
184
- for (const f of findings) {
185
- const matching = [];
186
- for (const session of sessions) {
187
- for (const msg of session.messages) {
188
- const content = msg.content || msg.message || '';
189
- if (typeof content !== 'string') continue;
190
- const snippet = f.snippet || '';
191
- if (snippet && content.includes(snippet.slice(0, 30))) {
192
- matching.push({ file: session.file, role: msg.role, snippet: content.slice(0, 200) });
193
- }
194
- const titleWords = (f.title || '').split(/\s+/).filter((w) => w.length > 4);
195
- if (titleWords.length > 0 && titleWords.every((w) => content.toLowerCase().includes(w.toLowerCase()))) {
196
- matching.push({ file: session.file, role: msg.role, snippet: content.slice(0, 200) });
197
- }
198
- }
199
- }
200
- if (matching.length > 0) {
201
- traces.push({ finding: f.ruleId, file: f.file, line: f.line, matches: matching });
202
- }
203
- }
204
- return traces;
205
- }
206
-
207
- function trace(root, findings) {
208
- const sessionFiles = findSessionFiles(root);
209
- if (sessionFiles.length === 0) {
210
- return { sessions: 0, behavior: null, traces: [] };
211
- }
212
-
213
- const sessions = sessionFiles.map(parseSession);
214
- const behavior = analyzeBehavior(sessions.flatMap((s) => s.messages));
215
- const traces = traceFindingsToPrompts(findings, sessions);
216
-
217
- return { sessions: sessions.length, behavior, traces };
218
- }
219
-
220
- module.exports = { trace, analyzeBehavior, findSessionFiles, parseSession, RISKY_BEHAVIORS };
1
+ 'use strict';
2
+
3
+ /*
4
+ * Prompt tracing — link findings back to the AI prompt that generated them.
5
+ *
6
+ * Reads Cursor (.cursor/), Claude Code, and Windsurf session history files.
7
+ * Scans for security-relevant behavior patterns in AI agent transcripts.
8
+ *
9
+ * Two capabilities:
10
+ * 1. Trace a finding back to the AI prompt that likely generated the code
11
+ * 2. Behavior analysis: detect blind approvals, high delegation, risky patterns
12
+ */
13
+
14
+ const fs = require('fs');
15
+ const path = require('path');
16
+
17
+ const SESSION_DIRS = [
18
+ '.cursor',
19
+ '.cursor/projects',
20
+ '.windsurf',
21
+ '.claude',
22
+ '.continue',
23
+ ];
24
+
25
+ function findSessionFiles(root) {
26
+ const files = [];
27
+ for (const dir of SESSION_DIRS) {
28
+ const absDir = path.join(root, dir);
29
+ if (!fs.existsSync(absDir)) continue;
30
+ try {
31
+ walkAndCollect(absDir, files, /\.(?:json|jsonl|md|txt)$/i);
32
+ } catch {}
33
+ }
34
+ return files;
35
+ }
36
+
37
+ function walkAndCollect(dir, out, extRe) {
38
+ let entries;
39
+ try {
40
+ entries = fs.readdirSync(dir, { withFileTypes: true });
41
+ } catch { return; }
42
+ for (const e of entries) {
43
+ const full = path.join(dir, e.name);
44
+ if (e.isDirectory()) walkAndCollect(full, out, extRe);
45
+ else if (e.isFile() && extRe.test(e.name)) out.push(full);
46
+ }
47
+ }
48
+
49
+ function parseSession(filepath) {
50
+ const content = fs.readFileSync(filepath, 'utf8');
51
+ const messages = [];
52
+
53
+ // Try JSONL format (one JSON object per line)
54
+ if (filepath.endsWith('.jsonl')) {
55
+ for (const line of content.split('\n')) {
56
+ try {
57
+ const obj = JSON.parse(line);
58
+ if (obj.role || obj.type || obj.message) messages.push(obj);
59
+ } catch {}
60
+ }
61
+ }
62
+
63
+ // Try JSON array
64
+ if (messages.length === 0 && filepath.endsWith('.json')) {
65
+ try {
66
+ const arr = JSON.parse(content);
67
+ if (Array.isArray(arr)) messages.push(...arr);
68
+ else messages.push(arr);
69
+ } catch {}
70
+ }
71
+
72
+ // Try markdown/text (split by user/assistant markers)
73
+ if (messages.length === 0) {
74
+ const parts = content.split(/^(?:User|Human|Assistant|AI|>>>)[:\s]/m);
75
+ parts.forEach((p, i) => {
76
+ if (p.trim()) messages.push({ role: i % 2 === 0 ? 'user' : 'assistant', content: p.trim() });
77
+ });
78
+ }
79
+
80
+ return { file: filepath, messages };
81
+ }
82
+
83
+ const RISKY_BEHAVIORS = [
84
+ {
85
+ id: 'behavior.blind-approval',
86
+ re: /(?:yes|ok|sure|go ahead|do it|approve|sounds good|perfect|great)\s*[!.?\s]*$/i,
87
+ risk: 'medium',
88
+ desc: 'Blind approval of AI suggestion without review',
89
+ },
90
+ {
91
+ id: 'behavior.high-delegation',
92
+ re: /(?:do\s+(?:all|everything|the\s+whole)|build\s+(?:the\s+entire|all)|write\s+(?:all|every|the\s+complete)|implement\s+(?:all|everything|the\s+entire))/i,
93
+ risk: 'medium',
94
+ desc: 'High delegation — asking AI to do everything at once reduces review quality',
95
+ },
96
+ {
97
+ id: 'behavior.skip-tests',
98
+ re: /(?:skip|don'?t\s+need|no\s+need\s+for)\s+(?:tests?|testing|validation)/i,
99
+ risk: 'high',
100
+ desc: 'Requesting code without tests',
101
+ },
102
+ {
103
+ id: 'behavior.skip-auth',
104
+ re: /(?:skip|don'?t\s+(?:need|worry\s+about)|no\s+need\s+for)\s+(?:auth|authentication|authorization|security)/i,
105
+ risk: 'high',
106
+ desc: 'Explicitly requesting to skip authentication/security',
107
+ },
108
+ {
109
+ id: 'behavior.hardcode-secrets',
110
+ re: /(?:just\s+)?(?:hardcode|hard-code|put\s+(?:the\s+)?(?:password|secret|token|key|api\s+key)\s+(?:directly\s+)?(?:in|here))/i,
111
+ risk: 'high',
112
+ desc: 'Asking AI to hardcode secrets',
113
+ },
114
+ {
115
+ id: 'behavior.disable-security',
116
+ re: /(?:disable|turn\s+off|remove|skip|bypass)\s+(?:security|validation|sanitization|escaping|CORS|helmet|CSRF)/i,
117
+ risk: 'high',
118
+ desc: 'Asking AI to disable security features',
119
+ },
120
+ {
121
+ id: 'behavior.trust-input',
122
+ re: /(?:trust\s+the\s+(?:user|client|input)|user\s+input\s+is\s+(?:safe|trusted|fine)|don'?t\s+validate)/i,
123
+ risk: 'high',
124
+ desc: 'Expressing trust in user/client input without validation',
125
+ },
126
+ {
127
+ id: 'behavior.excessive-iter',
128
+ re: /(?:again|redo|try\s+again|different\s+approach|start\s+over)/i,
129
+ risk: 'low',
130
+ desc: 'Excessive iteration on AI output — possible degradation of review quality',
131
+ threshold: 10,
132
+ },
133
+ ];
134
+
135
+ function analyzeBehavior(messages) {
136
+ const behaviors = [];
137
+ let userMessages = 0;
138
+ let approvals = 0;
139
+ let iterations = 0;
140
+
141
+ for (const msg of messages) {
142
+ const role = msg.role || msg.type || '';
143
+ const content = msg.content || msg.message || msg.text || '';
144
+ if (typeof content !== 'string') continue;
145
+ if (!/user|human/i.test(role)) continue;
146
+ userMessages++;
147
+
148
+ for (const b of RISKY_BEHAVIORS) {
149
+ if (b.re.test(content)) {
150
+ if (b.id === 'behavior.blind-approval') approvals++;
151
+ if (b.id === 'behavior.excessive-iter') iterations++;
152
+ if (b.id === 'behavior.excessive-iter' && iterations < (b.threshold || 10)) continue;
153
+ behaviors.push({
154
+ id: b.id,
155
+ risk: b.risk,
156
+ message: b.desc,
157
+ snippet: content.slice(0, 200),
158
+ });
159
+ }
160
+ }
161
+ }
162
+
163
+ const delegationRatio = userMessages > 0 ? approvals / userMessages : 0;
164
+ const risk = delegationRatio > 0.6 ? 'high' : delegationRatio > 0.3 ? 'medium' : 'low';
165
+
166
+ return {
167
+ totalUserMessages: userMessages,
168
+ approvals,
169
+ iterations,
170
+ delegationRatio: Math.round(delegationRatio * 100) / 100,
171
+ risk,
172
+ behaviors,
173
+ recommendation:
174
+ risk === 'high'
175
+ ? 'High blind-approval ratio — review AI suggestions more carefully before accepting'
176
+ : risk === 'medium'
177
+ ? 'Moderate delegation — ensure each AI suggestion is reviewed'
178
+ : 'Good review discipline — keep validating AI output',
179
+ };
180
+ }
181
+
182
+ function traceFindingsToPrompts(findings, sessions) {
183
+ const traces = [];
184
+ for (const f of findings) {
185
+ const matching = [];
186
+ for (const session of sessions) {
187
+ for (const msg of session.messages) {
188
+ const content = msg.content || msg.message || '';
189
+ if (typeof content !== 'string') continue;
190
+ const snippet = f.snippet || '';
191
+ if (snippet && content.includes(snippet.slice(0, 30))) {
192
+ matching.push({ file: session.file, role: msg.role, snippet: content.slice(0, 200) });
193
+ }
194
+ const titleWords = (f.title || '').split(/\s+/).filter((w) => w.length > 4);
195
+ if (titleWords.length > 0 && titleWords.every((w) => content.toLowerCase().includes(w.toLowerCase()))) {
196
+ matching.push({ file: session.file, role: msg.role, snippet: content.slice(0, 200) });
197
+ }
198
+ }
199
+ }
200
+ if (matching.length > 0) {
201
+ traces.push({ finding: f.ruleId, file: f.file, line: f.line, matches: matching });
202
+ }
203
+ }
204
+ return traces;
205
+ }
206
+
207
+ function trace(root, findings) {
208
+ const sessionFiles = findSessionFiles(root);
209
+ if (sessionFiles.length === 0) {
210
+ return { sessions: 0, behavior: null, traces: [] };
211
+ }
212
+
213
+ const sessions = sessionFiles.map(parseSession);
214
+ const behavior = analyzeBehavior(sessions.flatMap((s) => s.messages));
215
+ const traces = traceFindingsToPrompts(findings, sessions);
216
+
217
+ return { sessions: sessions.length, behavior, traces };
218
+ }
219
+
220
+ module.exports = { trace, analyzeBehavior, findSessionFiles, parseSession, RISKY_BEHAVIORS };