ship-safe 6.1.1 → 6.3.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (49) hide show
  1. package/README.md +748 -641
  2. package/cli/agents/api-fuzzer.js +345 -345
  3. package/cli/agents/auth-bypass-agent.js +348 -348
  4. package/cli/agents/base-agent.js +272 -272
  5. package/cli/agents/cicd-scanner.js +236 -201
  6. package/cli/agents/config-auditor.js +521 -521
  7. package/cli/agents/deep-analyzer.js +6 -2
  8. package/cli/agents/git-history-scanner.js +170 -170
  9. package/cli/agents/html-reporter.js +568 -568
  10. package/cli/agents/index.js +85 -84
  11. package/cli/agents/injection-tester.js +500 -500
  12. package/cli/agents/legal-risk-agent.js +302 -0
  13. package/cli/agents/llm-redteam.js +251 -251
  14. package/cli/agents/mobile-scanner.js +231 -231
  15. package/cli/agents/orchestrator.js +322 -322
  16. package/cli/agents/pii-compliance-agent.js +301 -301
  17. package/cli/agents/scoring-engine.js +248 -248
  18. package/cli/agents/supabase-rls-agent.js +154 -154
  19. package/cli/agents/supply-chain-agent.js +650 -507
  20. package/cli/bin/ship-safe.js +464 -426
  21. package/cli/commands/agent.js +608 -608
  22. package/cli/commands/audit.js +1006 -980
  23. package/cli/commands/baseline.js +193 -193
  24. package/cli/commands/ci.js +342 -342
  25. package/cli/commands/deps.js +516 -516
  26. package/cli/commands/doctor.js +159 -159
  27. package/cli/commands/fix.js +218 -218
  28. package/cli/commands/hooks.js +268 -0
  29. package/cli/commands/init.js +407 -407
  30. package/cli/commands/legal.js +158 -0
  31. package/cli/commands/mcp.js +304 -304
  32. package/cli/commands/red-team.js +7 -1
  33. package/cli/commands/remediate.js +798 -798
  34. package/cli/commands/rotate.js +571 -571
  35. package/cli/commands/scan.js +569 -569
  36. package/cli/commands/score.js +449 -449
  37. package/cli/commands/watch.js +281 -281
  38. package/cli/hooks/patterns.js +313 -0
  39. package/cli/hooks/post-tool-use.js +140 -0
  40. package/cli/hooks/pre-tool-use.js +186 -0
  41. package/cli/index.js +73 -69
  42. package/cli/providers/llm-provider.js +397 -287
  43. package/cli/utils/autofix-rules.js +74 -74
  44. package/cli/utils/cache-manager.js +311 -311
  45. package/cli/utils/output.js +230 -230
  46. package/cli/utils/patterns.js +1121 -1121
  47. package/cli/utils/pdf-generator.js +94 -94
  48. package/package.json +69 -69
  49. package/configs/supabase/rls-templates.sql +0 -242
@@ -1,322 +1,322 @@
1
- /**
2
- * Agent Orchestrator
3
- * ==================
4
- *
5
- * Coordinates all security agents, deduplicates findings,
6
- * and produces a unified report.
7
- *
8
- * Features:
9
- * - Per-agent timeouts (default 30s, configurable via --timeout)
10
- * - Parallel execution with configurable concurrency (default 6)
11
- *
12
- * USAGE:
13
- * const orchestrator = new Orchestrator();
14
- * orchestrator.register(new InjectionTester());
15
- * const results = await orchestrator.runAll(rootPath, options);
16
- */
17
-
18
- import path from 'path';
19
- import ora from 'ora';
20
- import chalk from 'chalk';
21
- import { ReconAgent } from './recon-agent.js';
22
- import { VerifierAgent } from './verifier-agent.js';
23
- import { DeepAnalyzer } from './deep-analyzer.js';
24
-
25
- // =============================================================================
26
- // CONSTANTS
27
- // =============================================================================
28
-
29
- const DEFAULT_TIMEOUT = 30_000; // 30s per agent
30
- const DEFAULT_CONCURRENCY = 6;
31
-
32
- // =============================================================================
33
- // ORCHESTRATOR
34
- // =============================================================================
35
-
36
- export class Orchestrator {
37
- constructor() {
38
- /** @type {import('./base-agent.js').BaseAgent[]} */
39
- this.agents = [];
40
- this.reconAgent = new ReconAgent();
41
- this.verifierAgent = new VerifierAgent();
42
- }
43
-
44
- /**
45
- * Register an agent for execution.
46
- */
47
- register(agent) {
48
- this.agents.push(agent);
49
- return this;
50
- }
51
-
52
- /**
53
- * Register multiple agents at once.
54
- */
55
- registerAll(agents) {
56
- for (const agent of agents) {
57
- this.register(agent);
58
- }
59
- return this;
60
- }
61
-
62
- /**
63
- * Run a single agent with a timeout.
64
- */
65
- async runAgent(agent, context, timeout) {
66
- return Promise.race([
67
- agent.analyze(context),
68
- new Promise((_, reject) => {
69
- setTimeout(() => reject(new Error(`timed out after ${timeout / 1000}s`)), timeout);
70
- }),
71
- ]);
72
- }
73
-
74
- /**
75
- * Run all registered agents against the codebase.
76
- *
77
- * @param {string} rootPath — Absolute path to the project root
78
- * @param {object} options — { verbose, agents[], categories[], timeout, concurrency }
79
- * @returns {Promise<object>} — { recon, findings[], agentResults[] }
80
- */
81
- async runAll(rootPath, options = {}) {
82
- const absolutePath = path.resolve(rootPath);
83
- const timeout = options.timeout || DEFAULT_TIMEOUT;
84
- const concurrency = options.concurrency || DEFAULT_CONCURRENCY;
85
-
86
- // ── 1. Recon — map the attack surface ─────────────────────────────────────
87
- const quiet = options.quiet || false;
88
- const reconSpinner = quiet ? null : ora({ text: 'Mapping attack surface...', color: 'cyan' }).start();
89
- const recon = await this.reconAgent.analyze({ rootPath: absolutePath, options });
90
- if (reconSpinner) reconSpinner.succeed(chalk.green('Attack surface mapped'));
91
-
92
- // ── 2. Discover files once (shared across agents) ─────────────────────────
93
- const files = await this.reconAgent.discoverFiles(absolutePath);
94
-
95
- // ── 3. Filter agents if specific ones requested ───────────────────────────
96
- let agentsToRun = this.agents;
97
- if (options.agents && options.agents.length > 0) {
98
- const requested = options.agents.map(a => a.toLowerCase());
99
- agentsToRun = this.agents.filter(a => {
100
- const name = a.name.toLowerCase();
101
- const cat = a.category.toLowerCase();
102
- return requested.some(r => name === r || name.includes(r) || cat === r);
103
- });
104
- }
105
- if (options.categories && options.categories.length > 0) {
106
- const requested = new Set(options.categories.map(c => c.toLowerCase()));
107
- agentsToRun = agentsToRun.filter(a => requested.has(a.category.toLowerCase()));
108
- }
109
-
110
- // ── 4. Build shared context ─────────────────────────────────────────────
111
- // sharedFindings allows cross-agent awareness: later agents can see
112
- // what earlier agents found (e.g., secrets agent finds a key,
113
- // supply-chain agent can check if it's committed to a public repo).
114
- const sharedFindings = [];
115
- const context = { rootPath: absolutePath, files, recon, options, sharedFindings };
116
- if (options.changedFiles) {
117
- context.changedFiles = options.changedFiles;
118
- }
119
-
120
- // ── 5. Run agents in parallel (chunked by concurrency) ──────────────────
121
- const agentResults = [];
122
- let allFindings = [];
123
-
124
- const spinner = quiet ? null : ora({
125
- text: `Running ${agentsToRun.length} agents in parallel...`,
126
- color: 'cyan'
127
- }).start();
128
-
129
- // Filter agents by framework relevance (shouldRun check)
130
- const relevantAgents = agentsToRun.filter(a => {
131
- if (typeof a.shouldRun === 'function') {
132
- return a.shouldRun(recon);
133
- }
134
- return true;
135
- });
136
- const skippedAgents = agentsToRun.length - relevantAgents.length;
137
-
138
- for (let i = 0; i < relevantAgents.length; i += concurrency) {
139
- const chunk = relevantAgents.slice(i, i + concurrency);
140
- const settled = await Promise.allSettled(
141
- chunk.map(agent => this.runAgent(agent, context, timeout))
142
- );
143
-
144
- for (let j = 0; j < chunk.length; j++) {
145
- const agent = chunk[j];
146
- const result = settled[j];
147
-
148
- if (result.status === 'fulfilled') {
149
- const findings = result.value;
150
- agentResults.push({
151
- agent: agent.name,
152
- category: agent.category,
153
- findingCount: findings.length,
154
- success: true,
155
- });
156
- allFindings = allFindings.concat(findings);
157
- // Share findings with subsequent agents
158
- sharedFindings.push(...findings);
159
- } else {
160
- agentResults.push({
161
- agent: agent.name,
162
- category: agent.category,
163
- findingCount: 0,
164
- success: false,
165
- error: result.reason.message,
166
- });
167
- }
168
- }
169
- }
170
-
171
- // Show results summary
172
- if (spinner) {
173
- const succeeded = agentResults.filter(a => a.success).length;
174
- const failed = agentResults.filter(a => !a.success).length;
175
- const totalFindings = allFindings.length;
176
-
177
- const skipNote = skippedAgents > 0 ? `, ${skippedAgents} skipped (not relevant)` : '';
178
- if (failed > 0) {
179
- spinner.warn(chalk.yellow(
180
- `${succeeded}/${relevantAgents.length} agents completed, ${failed} failed, ${totalFindings} finding(s)${skipNote}`
181
- ));
182
- } else {
183
- spinner.succeed(
184
- totalFindings === 0
185
- ? chalk.green(`${succeeded} agents: clean${skipNote}`)
186
- : chalk.yellow(`${succeeded} agents: ${totalFindings} finding(s)${skipNote}`)
187
- );
188
- }
189
- }
190
-
191
- // Show per-agent results when not in quiet mode
192
- if (!quiet) {
193
- for (const r of agentResults) {
194
- if (r.success) {
195
- const icon = r.findingCount === 0 ? chalk.green(' ✔') : chalk.yellow(' ⚠');
196
- const msg = r.findingCount === 0
197
- ? chalk.green(`${r.agent}: clean`)
198
- : chalk.yellow(`${r.agent}: ${r.findingCount} finding(s)`);
199
- console.log(`${icon} ${msg}`);
200
- } else {
201
- console.log(chalk.red(` ✗ ${r.agent}: ${r.error}`));
202
- }
203
- }
204
- }
205
-
206
- // ── 6. Deduplicate ────────────────────────────────────────────────────────
207
- allFindings = this.deduplicate(allFindings);
208
-
209
- // ── 7. Second-pass verification (confirms or downgrades findings) ───────
210
- if (!options.skipVerifier) {
211
- const verifySpinner = quiet ? null : ora({ text: 'Verifying findings...', color: 'cyan' }).start();
212
- allFindings = this.verifierAgent.verify(allFindings, options);
213
- const verified = allFindings.filter(f => f.verified === true).length;
214
- const downgraded = allFindings.filter(f => f.verified === false).length;
215
- if (verifySpinner) {
216
- verifySpinner.succeed(chalk.green(
217
- `Verified: ${verified} confirmed, ${downgraded} downgraded`
218
- ));
219
- }
220
- }
221
-
222
- // ── 8. Deep LLM analysis (optional, --deep flag) ───────────────────────
223
- if (options.deep) {
224
- const analyzer = DeepAnalyzer.create(absolutePath, {
225
- local: options.local,
226
- model: options.model,
227
- budgetCents: options.budget || 50,
228
- verbose: options.verbose,
229
- });
230
-
231
- if (analyzer) {
232
- const deepSpinner = quiet ? null : ora({ text: `Deep analysis with ${analyzer.provider.name}...`, color: 'cyan' }).start();
233
- try {
234
- allFindings = await analyzer.analyze(allFindings, { rootPath: absolutePath, recon });
235
- const stats = analyzer.getStats();
236
- if (deepSpinner) {
237
- deepSpinner.succeed(chalk.green(
238
- `Deep analysis: ${stats.analyzedCount} findings analyzed (${stats.spentCents}c spent)`
239
- ));
240
- }
241
- } catch (err) {
242
- if (deepSpinner) deepSpinner.fail(chalk.yellow(`Deep analysis failed: ${err.message}`));
243
- }
244
- } else if (!quiet) {
245
- console.log(chalk.gray(' Deep analysis: no LLM provider found (set ANTHROPIC_API_KEY or use --local)'));
246
- }
247
- }
248
-
249
- // ── 9. Context-aware confidence tuning ──────────────────────────────────
250
- allFindings = this.tuneConfidence(allFindings);
251
-
252
- // ── 10. Sort by severity ──────────────────────────────────────────────────
253
- const sevOrder = { critical: 0, high: 1, medium: 2, low: 3 };
254
- allFindings.sort((a, b) =>
255
- (sevOrder[a.severity] ?? 4) - (sevOrder[b.severity] ?? 4)
256
- );
257
-
258
- return { recon, findings: allFindings, agentResults };
259
- }
260
-
261
- /**
262
- * Run only agents matching a specific category.
263
- */
264
- async runCategory(category, rootPath, options = {}) {
265
- return this.runAll(rootPath, { ...options, categories: [category] });
266
- }
267
-
268
- /**
269
- * Downgrade confidence for findings in test files, comments, docs, or examples.
270
- * Reduces false-positive noise since ScoringEngine applies confidence multipliers.
271
- */
272
- tuneConfidence(findings) {
273
- const TEST_PATH = /(?:__tests__|\.test\.|\.spec\.|\/test\/|\/tests\/|\/fixtures?\/)/i;
274
- const DOC_EXT = new Set(['.md', '.txt', '.rst', '.adoc', '.rdoc']);
275
- const EXAMPLE_PATH = /(?:\/examples?\/|\/samples?\/|\/demos?\/|\/fixtures?\/|\/mocks?\/)/i;
276
- const COMMENT_LINE = /^\s*(?:\/\/|#|\/?\*|<!--)/;
277
-
278
- for (const f of findings) {
279
- const ext = (f.file || '').match(/\.[^.]+$/)?.[0]?.toLowerCase() || '';
280
-
281
- // Findings in documentation files
282
- if (DOC_EXT.has(ext)) {
283
- f.confidence = 'low';
284
- continue;
285
- }
286
-
287
- // Findings in test files
288
- if (TEST_PATH.test(f.file || '')) {
289
- f.confidence = 'low';
290
- continue;
291
- }
292
-
293
- // Findings in example/sample/demo paths: high → medium
294
- if (EXAMPLE_PATH.test(f.file || '') && f.confidence === 'high') {
295
- f.confidence = 'medium';
296
- continue;
297
- }
298
-
299
- // Findings on comment lines
300
- if (f.matched && COMMENT_LINE.test(f.matched)) {
301
- f.confidence = 'low';
302
- }
303
- }
304
-
305
- return findings;
306
- }
307
-
308
- /**
309
- * Remove duplicate findings (same file + line + rule).
310
- */
311
- deduplicate(findings) {
312
- const seen = new Set();
313
- return findings.filter(f => {
314
- const key = `${f.file}:${f.line}:${f.rule}`;
315
- if (seen.has(key)) return false;
316
- seen.add(key);
317
- return true;
318
- });
319
- }
320
- }
321
-
322
- export default Orchestrator;
1
+ /**
2
+ * Agent Orchestrator
3
+ * ==================
4
+ *
5
+ * Coordinates all security agents, deduplicates findings,
6
+ * and produces a unified report.
7
+ *
8
+ * Features:
9
+ * - Per-agent timeouts (default 30s, configurable via --timeout)
10
+ * - Parallel execution with configurable concurrency (default 6)
11
+ *
12
+ * USAGE:
13
+ * const orchestrator = new Orchestrator();
14
+ * orchestrator.register(new InjectionTester());
15
+ * const results = await orchestrator.runAll(rootPath, options);
16
+ */
17
+
18
+ import path from 'path';
19
+ import ora from 'ora';
20
+ import chalk from 'chalk';
21
+ import { ReconAgent } from './recon-agent.js';
22
+ import { VerifierAgent } from './verifier-agent.js';
23
+ import { DeepAnalyzer } from './deep-analyzer.js';
24
+
25
+ // =============================================================================
26
+ // CONSTANTS
27
+ // =============================================================================
28
+
29
+ const DEFAULT_TIMEOUT = 30_000; // 30s per agent
30
+ const DEFAULT_CONCURRENCY = 6;
31
+
32
+ // =============================================================================
33
+ // ORCHESTRATOR
34
+ // =============================================================================
35
+
36
+ export class Orchestrator {
37
+ constructor() {
38
+ /** @type {import('./base-agent.js').BaseAgent[]} */
39
+ this.agents = [];
40
+ this.reconAgent = new ReconAgent();
41
+ this.verifierAgent = new VerifierAgent();
42
+ }
43
+
44
+ /**
45
+ * Register an agent for execution.
46
+ */
47
+ register(agent) {
48
+ this.agents.push(agent);
49
+ return this;
50
+ }
51
+
52
+ /**
53
+ * Register multiple agents at once.
54
+ */
55
+ registerAll(agents) {
56
+ for (const agent of agents) {
57
+ this.register(agent);
58
+ }
59
+ return this;
60
+ }
61
+
62
+ /**
63
+ * Run a single agent with a timeout.
64
+ */
65
+ async runAgent(agent, context, timeout) {
66
+ return Promise.race([
67
+ agent.analyze(context),
68
+ new Promise((_, reject) => {
69
+ setTimeout(() => reject(new Error(`timed out after ${timeout / 1000}s`)), timeout);
70
+ }),
71
+ ]);
72
+ }
73
+
74
+ /**
75
+ * Run all registered agents against the codebase.
76
+ *
77
+ * @param {string} rootPath — Absolute path to the project root
78
+ * @param {object} options — { verbose, agents[], categories[], timeout, concurrency }
79
+ * @returns {Promise<object>} — { recon, findings[], agentResults[] }
80
+ */
81
+ async runAll(rootPath, options = {}) {
82
+ const absolutePath = path.resolve(rootPath);
83
+ const timeout = options.timeout || DEFAULT_TIMEOUT;
84
+ const concurrency = options.concurrency || DEFAULT_CONCURRENCY;
85
+
86
+ // ── 1. Recon — map the attack surface ─────────────────────────────────────
87
+ const quiet = options.quiet || false;
88
+ const reconSpinner = quiet ? null : ora({ text: 'Mapping attack surface...', color: 'cyan' }).start();
89
+ const recon = await this.reconAgent.analyze({ rootPath: absolutePath, options });
90
+ if (reconSpinner) reconSpinner.succeed(chalk.green('Attack surface mapped'));
91
+
92
+ // ── 2. Discover files once (shared across agents) ─────────────────────────
93
+ const files = await this.reconAgent.discoverFiles(absolutePath);
94
+
95
+ // ── 3. Filter agents if specific ones requested ───────────────────────────
96
+ let agentsToRun = this.agents;
97
+ if (options.agents && options.agents.length > 0) {
98
+ const requested = options.agents.map(a => a.toLowerCase());
99
+ agentsToRun = this.agents.filter(a => {
100
+ const name = a.name.toLowerCase();
101
+ const cat = a.category.toLowerCase();
102
+ return requested.some(r => name === r || name.includes(r) || cat === r);
103
+ });
104
+ }
105
+ if (options.categories && options.categories.length > 0) {
106
+ const requested = new Set(options.categories.map(c => c.toLowerCase()));
107
+ agentsToRun = agentsToRun.filter(a => requested.has(a.category.toLowerCase()));
108
+ }
109
+
110
+ // ── 4. Build shared context ─────────────────────────────────────────────
111
+ // sharedFindings allows cross-agent awareness: later agents can see
112
+ // what earlier agents found (e.g., secrets agent finds a key,
113
+ // supply-chain agent can check if it's committed to a public repo).
114
+ const sharedFindings = [];
115
+ const context = { rootPath: absolutePath, files, recon, options, sharedFindings };
116
+ if (options.changedFiles) {
117
+ context.changedFiles = options.changedFiles;
118
+ }
119
+
120
+ // ── 5. Run agents in parallel (chunked by concurrency) ──────────────────
121
+ const agentResults = [];
122
+ let allFindings = [];
123
+
124
+ const spinner = quiet ? null : ora({
125
+ text: `Running ${agentsToRun.length} agents in parallel...`,
126
+ color: 'cyan'
127
+ }).start();
128
+
129
+ // Filter agents by framework relevance (shouldRun check)
130
+ const relevantAgents = agentsToRun.filter(a => {
131
+ if (typeof a.shouldRun === 'function') {
132
+ return a.shouldRun(recon);
133
+ }
134
+ return true;
135
+ });
136
+ const skippedAgents = agentsToRun.length - relevantAgents.length;
137
+
138
+ for (let i = 0; i < relevantAgents.length; i += concurrency) {
139
+ const chunk = relevantAgents.slice(i, i + concurrency);
140
+ const settled = await Promise.allSettled(
141
+ chunk.map(agent => this.runAgent(agent, context, timeout))
142
+ );
143
+
144
+ for (let j = 0; j < chunk.length; j++) {
145
+ const agent = chunk[j];
146
+ const result = settled[j];
147
+
148
+ if (result.status === 'fulfilled') {
149
+ const findings = result.value;
150
+ agentResults.push({
151
+ agent: agent.name,
152
+ category: agent.category,
153
+ findingCount: findings.length,
154
+ success: true,
155
+ });
156
+ allFindings = allFindings.concat(findings);
157
+ // Share findings with subsequent agents
158
+ sharedFindings.push(...findings);
159
+ } else {
160
+ agentResults.push({
161
+ agent: agent.name,
162
+ category: agent.category,
163
+ findingCount: 0,
164
+ success: false,
165
+ error: result.reason.message,
166
+ });
167
+ }
168
+ }
169
+ }
170
+
171
+ // Show results summary
172
+ if (spinner) {
173
+ const succeeded = agentResults.filter(a => a.success).length;
174
+ const failed = agentResults.filter(a => !a.success).length;
175
+ const totalFindings = allFindings.length;
176
+
177
+ const skipNote = skippedAgents > 0 ? `, ${skippedAgents} skipped (not relevant)` : '';
178
+ if (failed > 0) {
179
+ spinner.warn(chalk.yellow(
180
+ `${succeeded}/${relevantAgents.length} agents completed, ${failed} failed, ${totalFindings} finding(s)${skipNote}`
181
+ ));
182
+ } else {
183
+ spinner.succeed(
184
+ totalFindings === 0
185
+ ? chalk.green(`${succeeded} agents: clean${skipNote}`)
186
+ : chalk.yellow(`${succeeded} agents: ${totalFindings} finding(s)${skipNote}`)
187
+ );
188
+ }
189
+ }
190
+
191
+ // Show per-agent results when not in quiet mode
192
+ if (!quiet) {
193
+ for (const r of agentResults) {
194
+ if (r.success) {
195
+ const icon = r.findingCount === 0 ? chalk.green(' ✔') : chalk.yellow(' ⚠');
196
+ const msg = r.findingCount === 0
197
+ ? chalk.green(`${r.agent}: clean`)
198
+ : chalk.yellow(`${r.agent}: ${r.findingCount} finding(s)`);
199
+ console.log(`${icon} ${msg}`);
200
+ } else {
201
+ console.log(chalk.red(` ✗ ${r.agent}: ${r.error}`));
202
+ }
203
+ }
204
+ }
205
+
206
+ // ── 6. Deduplicate ────────────────────────────────────────────────────────
207
+ allFindings = this.deduplicate(allFindings);
208
+
209
+ // ── 7. Second-pass verification (confirms or downgrades findings) ───────
210
+ if (!options.skipVerifier) {
211
+ const verifySpinner = quiet ? null : ora({ text: 'Verifying findings...', color: 'cyan' }).start();
212
+ allFindings = this.verifierAgent.verify(allFindings, options);
213
+ const verified = allFindings.filter(f => f.verified === true).length;
214
+ const downgraded = allFindings.filter(f => f.verified === false).length;
215
+ if (verifySpinner) {
216
+ verifySpinner.succeed(chalk.green(
217
+ `Verified: ${verified} confirmed, ${downgraded} downgraded`
218
+ ));
219
+ }
220
+ }
221
+
222
+ // ── 8. Deep LLM analysis (optional, --deep flag) ───────────────────────
223
+ if (options.deep) {
224
+ const analyzer = DeepAnalyzer.create(absolutePath, {
225
+ local: options.local,
226
+ model: options.model,
227
+ budgetCents: options.budget || 50,
228
+ verbose: options.verbose,
229
+ });
230
+
231
+ if (analyzer) {
232
+ const deepSpinner = quiet ? null : ora({ text: `Deep analysis with ${analyzer.provider.name}...`, color: 'cyan' }).start();
233
+ try {
234
+ allFindings = await analyzer.analyze(allFindings, { rootPath: absolutePath, recon });
235
+ const stats = analyzer.getStats();
236
+ if (deepSpinner) {
237
+ deepSpinner.succeed(chalk.green(
238
+ `Deep analysis: ${stats.analyzedCount} findings analyzed (${stats.spentCents}c spent)`
239
+ ));
240
+ }
241
+ } catch (err) {
242
+ if (deepSpinner) deepSpinner.fail(chalk.yellow(`Deep analysis failed: ${err.message}`));
243
+ }
244
+ } else if (!quiet) {
245
+ console.log(chalk.gray(' Deep analysis: no LLM provider found (set ANTHROPIC_API_KEY or use --local)'));
246
+ }
247
+ }
248
+
249
+ // ── 9. Context-aware confidence tuning ──────────────────────────────────
250
+ allFindings = this.tuneConfidence(allFindings);
251
+
252
+ // ── 10. Sort by severity ──────────────────────────────────────────────────
253
+ const sevOrder = { critical: 0, high: 1, medium: 2, low: 3 };
254
+ allFindings.sort((a, b) =>
255
+ (sevOrder[a.severity] ?? 4) - (sevOrder[b.severity] ?? 4)
256
+ );
257
+
258
+ return { recon, findings: allFindings, agentResults };
259
+ }
260
+
261
+ /**
262
+ * Run only agents matching a specific category.
263
+ */
264
+ async runCategory(category, rootPath, options = {}) {
265
+ return this.runAll(rootPath, { ...options, categories: [category] });
266
+ }
267
+
268
+ /**
269
+ * Downgrade confidence for findings in test files, comments, docs, or examples.
270
+ * Reduces false-positive noise since ScoringEngine applies confidence multipliers.
271
+ */
272
+ tuneConfidence(findings) {
273
+ const TEST_PATH = /(?:__tests__|\.test\.|\.spec\.|\/test\/|\/tests\/|\/fixtures?\/)/i;
274
+ const DOC_EXT = new Set(['.md', '.txt', '.rst', '.adoc', '.rdoc']);
275
+ const EXAMPLE_PATH = /(?:\/examples?\/|\/samples?\/|\/demos?\/|\/fixtures?\/|\/mocks?\/)/i;
276
+ const COMMENT_LINE = /^\s*(?:\/\/|#|\/?\*|<!--)/;
277
+
278
+ for (const f of findings) {
279
+ const ext = (f.file || '').match(/\.[^.]+$/)?.[0]?.toLowerCase() || '';
280
+
281
+ // Findings in documentation files
282
+ if (DOC_EXT.has(ext)) {
283
+ f.confidence = 'low';
284
+ continue;
285
+ }
286
+
287
+ // Findings in test files
288
+ if (TEST_PATH.test(f.file || '')) {
289
+ f.confidence = 'low';
290
+ continue;
291
+ }
292
+
293
+ // Findings in example/sample/demo paths: high → medium
294
+ if (EXAMPLE_PATH.test(f.file || '') && f.confidence === 'high') {
295
+ f.confidence = 'medium';
296
+ continue;
297
+ }
298
+
299
+ // Findings on comment lines
300
+ if (f.matched && COMMENT_LINE.test(f.matched)) {
301
+ f.confidence = 'low';
302
+ }
303
+ }
304
+
305
+ return findings;
306
+ }
307
+
308
+ /**
309
+ * Remove duplicate findings (same file + line + rule).
310
+ */
311
+ deduplicate(findings) {
312
+ const seen = new Set();
313
+ return findings.filter(f => {
314
+ const key = `${f.file}:${f.line}:${f.rule}`;
315
+ if (seen.has(key)) return false;
316
+ seen.add(key);
317
+ return true;
318
+ });
319
+ }
320
+ }
321
+
322
+ export default Orchestrator;