@codepulse-oss/core 3.1.6 → 3.2.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.
- package/dist/__tests__/v3.1.7-intel-providers.test.d.ts +2 -0
- package/dist/__tests__/v3.1.7-intel-providers.test.d.ts.map +1 -0
- package/dist/__tests__/v3.1.7-intel-providers.test.js +179 -0
- package/dist/__tests__/v3.1.7-intel-providers.test.js.map +1 -0
- package/dist/__tests__/v320-engines.test.d.ts +2 -0
- package/dist/__tests__/v320-engines.test.d.ts.map +1 -0
- package/dist/__tests__/v320-engines.test.js +210 -0
- package/dist/__tests__/v320-engines.test.js.map +1 -0
- package/dist/ai/provider.d.ts +33 -6
- package/dist/ai/provider.d.ts.map +1 -1
- package/dist/ai/provider.js +237 -88
- package/dist/ai/provider.js.map +1 -1
- package/dist/audit/architecture-i18n-auditor.d.ts +39 -0
- package/dist/audit/architecture-i18n-auditor.d.ts.map +1 -0
- package/dist/audit/architecture-i18n-auditor.js +210 -0
- package/dist/audit/architecture-i18n-auditor.js.map +1 -0
- package/dist/audit/data-integrity-scanner.d.ts +13 -0
- package/dist/audit/data-integrity-scanner.d.ts.map +1 -0
- package/dist/audit/data-integrity-scanner.js +133 -0
- package/dist/audit/data-integrity-scanner.js.map +1 -0
- package/dist/audit/explain-simple.d.ts +3 -3
- package/dist/audit/explain-simple.d.ts.map +1 -1
- package/dist/audit/explain-simple.js +98 -46
- package/dist/audit/explain-simple.js.map +1 -1
- package/dist/audit/extended-detectors.d.ts.map +1 -1
- package/dist/audit/extended-detectors.js +30 -0
- package/dist/audit/extended-detectors.js.map +1 -1
- package/dist/audit/live-accessibility.d.ts +9 -0
- package/dist/audit/live-accessibility.d.ts.map +1 -1
- package/dist/audit/live-accessibility.js +65 -1
- package/dist/audit/live-accessibility.js.map +1 -1
- package/dist/audit/llm-security-scanner.d.ts +11 -0
- package/dist/audit/llm-security-scanner.d.ts.map +1 -0
- package/dist/audit/llm-security-scanner.js +130 -0
- package/dist/audit/llm-security-scanner.js.map +1 -0
- package/dist/audit/logic-race-idor-scanner.d.ts +11 -0
- package/dist/audit/logic-race-idor-scanner.d.ts.map +1 -0
- package/dist/audit/logic-race-idor-scanner.js +116 -0
- package/dist/audit/logic-race-idor-scanner.js.map +1 -0
- package/dist/audit/main-scanner.d.ts +4 -1
- package/dist/audit/main-scanner.d.ts.map +1 -1
- package/dist/audit/main-scanner.js +172 -84
- package/dist/audit/main-scanner.js.map +1 -1
- package/dist/audit/passive-dast.d.ts +31 -0
- package/dist/audit/passive-dast.d.ts.map +1 -0
- package/dist/audit/passive-dast.js +205 -0
- package/dist/audit/passive-dast.js.map +1 -0
- package/dist/audit/sql-rls-auditor.d.ts +13 -0
- package/dist/audit/sql-rls-auditor.d.ts.map +1 -0
- package/dist/audit/sql-rls-auditor.js +161 -0
- package/dist/audit/sql-rls-auditor.js.map +1 -0
- package/dist/audit/taint-tracker.d.ts +15 -0
- package/dist/audit/taint-tracker.d.ts.map +1 -0
- package/dist/audit/taint-tracker.js +243 -0
- package/dist/audit/taint-tracker.js.map +1 -0
- package/dist/audit/threat-intel.d.ts +75 -0
- package/dist/audit/threat-intel.d.ts.map +1 -0
- package/dist/audit/threat-intel.js +205 -0
- package/dist/audit/threat-intel.js.map +1 -0
- package/dist/audit/variant-hunter.d.ts +18 -0
- package/dist/audit/variant-hunter.d.ts.map +1 -0
- package/dist/audit/variant-hunter.js +138 -0
- package/dist/audit/variant-hunter.js.map +1 -0
- package/dist/audit/web-intel.d.ts +44 -0
- package/dist/audit/web-intel.d.ts.map +1 -0
- package/dist/audit/web-intel.js +144 -0
- package/dist/audit/web-intel.js.map +1 -0
- package/dist/autofix/worktree-autofix.d.ts +11 -0
- package/dist/autofix/worktree-autofix.d.ts.map +1 -1
- package/dist/autofix/worktree-autofix.js +33 -0
- package/dist/autofix/worktree-autofix.js.map +1 -1
- package/dist/contributors/contributors-registry.d.ts +37 -0
- package/dist/contributors/contributors-registry.d.ts.map +1 -0
- package/dist/contributors/contributors-registry.js +188 -0
- package/dist/contributors/contributors-registry.js.map +1 -0
- package/dist/db/database.d.ts +44 -0
- package/dist/db/database.d.ts.map +1 -1
- package/dist/db/database.js +166 -1
- package/dist/db/database.js.map +1 -1
- package/dist/db/schema.d.ts +1 -1
- package/dist/db/schema.d.ts.map +1 -1
- package/dist/db/schema.js +29 -0
- package/dist/db/schema.js.map +1 -1
- package/dist/index.d.ts +15 -0
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +15 -0
- package/dist/index.js.map +1 -1
- package/dist/output/html-report-writer.d.ts +1 -0
- package/dist/output/html-report-writer.d.ts.map +1 -1
- package/dist/output/html-report-writer.js +12 -3
- package/dist/output/html-report-writer.js.map +1 -1
- package/dist/output/markdown-writer.js +1 -1
- package/dist/output/markdown-writer.js.map +1 -1
- package/dist/output/pr-receipt.d.ts +35 -0
- package/dist/output/pr-receipt.d.ts.map +1 -0
- package/dist/output/pr-receipt.js +62 -0
- package/dist/output/pr-receipt.js.map +1 -0
- package/dist/output/sarif-writer.d.ts.map +1 -1
- package/dist/output/sarif-writer.js +4 -1
- package/dist/output/sarif-writer.js.map +1 -1
- package/dist/review/engine.d.ts +3 -1
- package/dist/review/engine.d.ts.map +1 -1
- package/dist/review/engine.js +44 -1
- package/dist/review/engine.js.map +1 -1
- package/dist/review/pipeline-stages.d.ts +52 -5
- package/dist/review/pipeline-stages.d.ts.map +1 -1
- package/dist/review/pipeline-stages.js +303 -185
- package/dist/review/pipeline-stages.js.map +1 -1
- package/dist/sast/ast-queries.d.ts.map +1 -1
- package/dist/sast/ast-queries.js +6 -2
- package/dist/sast/ast-queries.js.map +1 -1
- package/dist/sast/osv-scanner.d.ts +3 -0
- package/dist/sast/osv-scanner.d.ts.map +1 -1
- package/dist/sast/osv-scanner.js +35 -1
- package/dist/sast/osv-scanner.js.map +1 -1
- package/dist/sast/rule-packs.d.ts +2 -0
- package/dist/sast/rule-packs.d.ts.map +1 -1
- package/dist/sast/sbom-generator.js +2 -2
- package/dist/sast/secret-registry.d.ts.map +1 -1
- package/dist/sast/secret-registry.js +6 -3
- package/dist/sast/secret-registry.js.map +1 -1
- package/dist/telemetry/optin-telemetry.js +1 -1
- package/dist/testing/runtime-verifier.d.ts +33 -0
- package/dist/testing/runtime-verifier.d.ts.map +1 -0
- package/dist/testing/runtime-verifier.js +95 -0
- package/dist/testing/runtime-verifier.js.map +1 -0
- package/dist/testing/selective-retest.d.ts +28 -0
- package/dist/testing/selective-retest.d.ts.map +1 -0
- package/dist/testing/selective-retest.js +123 -0
- package/dist/testing/selective-retest.js.map +1 -0
- package/dist/testing/test-suite-runner.d.ts +54 -0
- package/dist/testing/test-suite-runner.d.ts.map +1 -0
- package/dist/testing/test-suite-runner.js +245 -0
- package/dist/testing/test-suite-runner.js.map +1 -0
- package/dist/types/review.d.ts +62 -1
- package/dist/types/review.d.ts.map +1 -1
- package/dist/types/review.js +33 -2
- package/dist/types/review.js.map +1 -1
- package/package.json +1 -1
|
@@ -5,6 +5,41 @@ import { renderRepoPageRankMap } from '../graph/pagerank-map.js';
|
|
|
5
5
|
import { mapSeverityToTier } from '../types/review.js';
|
|
6
6
|
import { maskSecrets, resolveSafeDirectory, safeResolveInside, toPosixPath } from '../utils/platform.js';
|
|
7
7
|
import { runCommand } from '../utils/process.js';
|
|
8
|
+
/**
|
|
9
|
+
* Split a full file cleanly into overlapping line windows (e.g., 250 lines with 20-line overlap)
|
|
10
|
+
* and explicit `[Lnnnn]` prefixes so no code past char 10,000 is ever truncated.
|
|
11
|
+
*/
|
|
12
|
+
export function chunkFileWithLineWindows(relPath, rawContent, options = {}) {
|
|
13
|
+
const posix = toPosixPath(relPath);
|
|
14
|
+
const windowLines = Math.max(40, options.windowLines ?? 250);
|
|
15
|
+
const overlapLines = Math.max(0, Math.min(windowLines - 10, options.overlapLines ?? 20));
|
|
16
|
+
const step = Math.max(20, windowLines - overlapLines);
|
|
17
|
+
const allLines = rawContent.split(/\r?\n/);
|
|
18
|
+
if (allLines.length === 0)
|
|
19
|
+
return [];
|
|
20
|
+
const chunks = [];
|
|
21
|
+
for (let startIdx = 0; startIdx < allLines.length; startIdx += step) {
|
|
22
|
+
const endIdx = Math.min(allLines.length, startIdx + windowLines);
|
|
23
|
+
const startLine = startIdx + 1;
|
|
24
|
+
const endLine = Math.max(startLine, endIdx);
|
|
25
|
+
const numbered = allLines
|
|
26
|
+
.slice(startIdx, endIdx)
|
|
27
|
+
.map((line, i) => `[L${String(startLine + i).padStart(4, '0')}] ${maskSecrets(line)}`)
|
|
28
|
+
.join('\n');
|
|
29
|
+
const snippet = `### ${posix} (lines ${startLine}-${endLine}/${allLines.length})\n${numbered}`;
|
|
30
|
+
chunks.push({
|
|
31
|
+
chunkKey: `${posix}:${startLine}-${endLine}`,
|
|
32
|
+
file: posix,
|
|
33
|
+
startLine,
|
|
34
|
+
endLine,
|
|
35
|
+
snippet,
|
|
36
|
+
tokens: Math.max(80, TokenBudgeter.estimateTokens(snippet))
|
|
37
|
+
});
|
|
38
|
+
if (endIdx >= allLines.length)
|
|
39
|
+
break;
|
|
40
|
+
}
|
|
41
|
+
return chunks;
|
|
42
|
+
}
|
|
8
43
|
const SKIP_AI_FILE_RE = /(?:^|\/)(?:node_modules|dist|build|coverage|\.git|\.codepulse|vendor|release|__tests__|tests?|e2e)\/|\.(?:test|spec|min|d)\.[^.]+$|\.(?:wasm|png|jpe?g|gif|webp|ico|woff2?|ttf|eot|pdf|zip|exe|dmg|AppImage|deb|rpm|lock)$|(?:package-lock\.json|yarn\.lock|pnpm-lock\.yaml)$/i;
|
|
9
44
|
export function isEligibleAiSourceFile(filePath) {
|
|
10
45
|
const posix = toPosixPath(filePath);
|
|
@@ -35,28 +70,37 @@ export function extractJsonObjectSafe(text) {
|
|
|
35
70
|
export function alignBudgetForScanMode(aiClient, options) {
|
|
36
71
|
if (!aiClient)
|
|
37
72
|
return;
|
|
73
|
+
const auditDepth = options.auditDepth ?? 'standard';
|
|
38
74
|
const est = estimateScanBudget({
|
|
39
75
|
filesCount: Math.max(1, options.filesCount),
|
|
40
|
-
totalBytes: Math.max(4096, options.filesCount * 4200),
|
|
76
|
+
totalBytes: Math.max(4096, options.filesCount * (auditDepth === 'exhaustive_fortress' ? 6800 : 4200)),
|
|
41
77
|
mode: options.mode,
|
|
42
78
|
provider: aiClient.config.provider,
|
|
43
79
|
model: aiClient.config.model,
|
|
44
|
-
fastModel: aiClient.config.fastModel
|
|
80
|
+
fastModel: aiClient.config.fastModel,
|
|
81
|
+
auditDepth
|
|
45
82
|
});
|
|
46
|
-
const isDeepMode =
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
83
|
+
const isDeepMode = auditDepth === 'exhaustive_fortress' ||
|
|
84
|
+
options.mode === 'comprehensive' ||
|
|
85
|
+
options.mode === 'ultrareview' ||
|
|
86
|
+
options.mode === 'deep';
|
|
87
|
+
const dynamicFloor = auditDepth === 'exhaustive_fortress'
|
|
88
|
+
? Math.max(1_000_000, Math.round(est.estimatedTotalTokens * 1.6))
|
|
89
|
+
: isDeepMode
|
|
90
|
+
? Math.max(350_000, Math.round(est.estimatedTotalTokens * 1.35))
|
|
91
|
+
: Math.max(120_000, Math.round(est.estimatedTotalTokens * 1.15));
|
|
50
92
|
aiClient.budgeter.adjustDynamicJobBudget(dynamicFloor, options.explicitUserCap);
|
|
51
93
|
}
|
|
52
94
|
/**
|
|
53
|
-
* Partition all eligible files
|
|
54
|
-
*
|
|
95
|
+
* Partition all eligible files cleanly into token-budgeted AI sub-batches using full-file
|
|
96
|
+
* overlapping line-window chunking (`[Lnnn]`), eliminating fixed character cutoffs.
|
|
55
97
|
*/
|
|
56
98
|
export async function partitionFilesIntoTokenBatches(repoPath, files, rankedEntries, options = {}) {
|
|
57
|
-
const
|
|
58
|
-
const
|
|
59
|
-
const
|
|
99
|
+
const auditDepth = options.auditDepth ?? 'standard';
|
|
100
|
+
const targetBatchTokens = options.targetBatchTokens ?? (auditDepth === 'exhaustive_fortress' ? 9_500 : 11_500);
|
|
101
|
+
const maxFilesPerBatch = options.maxFilesPerBatch ?? (auditDepth === 'exhaustive_fortress' ? 4 : 9);
|
|
102
|
+
const windowLines = options.windowLines ?? (auditDepth === 'exhaustive_fortress' ? 250 : 320);
|
|
103
|
+
const overlapLines = options.overlapLines ?? 20;
|
|
60
104
|
const rankScore = new Map(rankedEntries.map((r) => [toPosixPath(r.file), r.normalizedScore]));
|
|
61
105
|
const eligible = Array.from(new Set(files.map(toPosixPath)))
|
|
62
106
|
.filter(isEligibleAiSourceFile)
|
|
@@ -64,13 +108,14 @@ export async function partitionFilesIntoTokenBatches(repoPath, files, rankedEntr
|
|
|
64
108
|
const batches = [];
|
|
65
109
|
let currentFiles = [];
|
|
66
110
|
let currentTokens = 0;
|
|
111
|
+
let totalChunks = 0;
|
|
67
112
|
const safeRoot = resolveSafeDirectory(repoPath);
|
|
68
113
|
for (const rel of eligible) {
|
|
69
114
|
let raw = '';
|
|
70
115
|
try {
|
|
71
116
|
const abs = safeResolveInside(safeRoot, rel);
|
|
72
117
|
const st = await fs.stat(abs);
|
|
73
|
-
if (!st.isFile() || st.size >
|
|
118
|
+
if (!st.isFile() || st.size > 850_000)
|
|
74
119
|
continue;
|
|
75
120
|
raw = await fs.readFile(abs, 'utf8');
|
|
76
121
|
if (raw.includes('\0'))
|
|
@@ -79,21 +124,29 @@ export async function partitionFilesIntoTokenBatches(repoPath, files, rankedEntr
|
|
|
79
124
|
catch {
|
|
80
125
|
continue;
|
|
81
126
|
}
|
|
82
|
-
const
|
|
83
|
-
const
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
127
|
+
const fileChunks = chunkFileWithLineWindows(rel, raw, { windowLines, overlapLines });
|
|
128
|
+
for (const chunk of fileChunks) {
|
|
129
|
+
totalChunks++;
|
|
130
|
+
if (currentFiles.length > 0 &&
|
|
131
|
+
(currentFiles.length >= maxFilesPerBatch || currentTokens + chunk.tokens > targetBatchTokens)) {
|
|
132
|
+
batches.push({
|
|
133
|
+
batchIndex: batches.length + 1,
|
|
134
|
+
files: currentFiles,
|
|
135
|
+
totalTokens: currentTokens
|
|
136
|
+
});
|
|
137
|
+
currentFiles = [];
|
|
138
|
+
currentTokens = 0;
|
|
139
|
+
}
|
|
140
|
+
currentFiles.push({
|
|
141
|
+
file: chunk.file,
|
|
142
|
+
snippet: chunk.snippet,
|
|
143
|
+
tokens: chunk.tokens,
|
|
144
|
+
chunkKey: chunk.chunkKey,
|
|
145
|
+
startLine: chunk.startLine,
|
|
146
|
+
endLine: chunk.endLine
|
|
91
147
|
});
|
|
92
|
-
|
|
93
|
-
currentTokens = 0;
|
|
148
|
+
currentTokens += chunk.tokens;
|
|
94
149
|
}
|
|
95
|
-
currentFiles.push({ file: rel, snippet, tokens });
|
|
96
|
-
currentTokens += tokens;
|
|
97
150
|
}
|
|
98
151
|
if (currentFiles.length > 0) {
|
|
99
152
|
batches.push({
|
|
@@ -102,7 +155,7 @@ export async function partitionFilesIntoTokenBatches(repoPath, files, rankedEntr
|
|
|
102
155
|
totalTokens: currentTokens
|
|
103
156
|
});
|
|
104
157
|
}
|
|
105
|
-
return { batches, eligibleFilesCount: eligible.length };
|
|
158
|
+
return { batches, eligibleFilesCount: eligible.length, totalChunks };
|
|
106
159
|
}
|
|
107
160
|
/**
|
|
108
161
|
* Shared read-only AST, file-line, SQL schema, and git history tool executor for Persona A and Agent 2 Verifier.
|
|
@@ -179,14 +232,14 @@ export async function runGlobalCrossModuleSynthesisPass(options) {
|
|
|
179
232
|
const gitHistText = await executeAgentReadOnlyTool(repoPath, nodes, edges, { tool: 'get_git_history' });
|
|
180
233
|
const digestPayload = {
|
|
181
234
|
repository: repoName,
|
|
182
|
-
slices: sliceSummaries.
|
|
235
|
+
slices: sliceSummaries.map((s) => ({
|
|
183
236
|
domain: s.domain,
|
|
184
237
|
fileCount: s.fileCount,
|
|
185
238
|
riskScore: s.riskScore,
|
|
186
|
-
sampleFiles: s.files.slice(0,
|
|
239
|
+
sampleFiles: s.files.slice(0, 8)
|
|
187
240
|
})),
|
|
188
241
|
sqlTables,
|
|
189
|
-
existingFindingsDigest: candidates.slice(0,
|
|
242
|
+
existingFindingsDigest: candidates.slice(0, 120).map((f) => ({
|
|
190
243
|
severity: f.severity,
|
|
191
244
|
category: f.category,
|
|
192
245
|
file: f.file,
|
|
@@ -242,12 +295,83 @@ Return ONLY valid JSON: { "findings": [ { "severity": "critical|high|medium|low"
|
|
|
242
295
|
return { findings: [] };
|
|
243
296
|
}
|
|
244
297
|
}
|
|
298
|
+
/**
|
|
299
|
+
* Execute a Dual-Persona Stage-2 AI discovery pass (`2A` Security/SQL/Race/Taint or
|
|
300
|
+
* `2B` Data-Integrity/Cache/Swallowed-Error/Business-Logic) over a token-budgeted chunk batch
|
|
301
|
+
* with up to 4 read-only AST/SQL/git tool turns.
|
|
302
|
+
*/
|
|
303
|
+
export async function runPersonaDiscoveryBatch(options) {
|
|
304
|
+
const { aiClient, persona, sliceDomain, batch, totalBatches, repoPath, nodes, edges, useAgentTools, maxToolTurns = 4, blastLookup } = options;
|
|
305
|
+
const batchBody = batch.files.map((x) => x.snippet).join('\n\n');
|
|
306
|
+
const transcript = [];
|
|
307
|
+
const effectiveTurns = useAgentTools ? Math.max(2, Math.min(5, maxToolTurns)) : 1;
|
|
308
|
+
const focusDirective = persona === '2A_security_concurrency'
|
|
309
|
+
? 'Hunt ruthlessly for: SECURITY DEFINER RPC/SQL authorization gaps, cross-tenant RLS bypass, race conditions/TOCTOU, IDOR/BOLA, SQL/command/prompt injection, fail-open auth guards, SSRF, and cross-file tainted data flows.'
|
|
310
|
+
: 'Hunt ruthlessly for: swallowed `{ error }` SDK returns, empty Zod `z.object({})` request schemas, cache poisoning with partial/truncated lists, `.limit(N)` aggregation truncation, non-atomic multi-table writes, currency/negative-quantity precision errors, and migration drift.';
|
|
311
|
+
const findings = [];
|
|
312
|
+
const auditedFiles = new Set();
|
|
313
|
+
for (let turn = 0; turn < effectiveTurns; turn++) {
|
|
314
|
+
const sysPrompt = `You are Stage 2 Discovery Agent (${persona}) auditing the "${sliceDomain}" slice (batch ${batch.batchIndex}/${totalBatches}).
|
|
315
|
+
Line numbers are marked explicitly with [Lnnnn] prefixes on every line — use exact line numbers.
|
|
316
|
+
${focusDirective}
|
|
317
|
+
${useAgentTools ? `Inspect cross-file callers, symbol definitions, DB schemas, or git history first via {"tool_calls":[{"tool":"find_callers"|"get_symbol_definition"|"read_file_lines"|"inspect_db_schema"|"get_git_history",...}],"findings":[]} before returning final JSON.` : ''}
|
|
318
|
+
Return ONLY valid JSON: { "findings": [ { "severity": "critical|high|medium|low", "confidence": 0.0-1.0, "category": "security|auth|logic|finance|concurrency|migration-drift|architecture|reliability|llm-security", "title": "...", "body": "...", "file": "exact/path", "line_start": 1, "line_end": 1, "symbol": "optionalSymbol", "recommendation": "...", "evidence": "exact code snippet" } ] }`;
|
|
319
|
+
const aiRes = await aiClient.complete({
|
|
320
|
+
systemPrompt: sysPrompt,
|
|
321
|
+
userPrompt: transcript.length > 0 ? `${batchBody}\n\nTOOL_RESULTS:\n${transcript.join('\n')}` : batchBody,
|
|
322
|
+
jsonMode: true,
|
|
323
|
+
temperature: 0.1
|
|
324
|
+
});
|
|
325
|
+
const parsedTurn = extractJsonObjectSafe(aiRes.text);
|
|
326
|
+
if (useAgentTools && parsedTurn?.tool_calls && parsedTurn.tool_calls.length > 0 && turn < effectiveTurns - 1) {
|
|
327
|
+
for (const tc of parsedTurn.tool_calls.slice(0, 4)) {
|
|
328
|
+
const out = await executeAgentReadOnlyTool(repoPath, nodes, edges, tc);
|
|
329
|
+
transcript.push(`TOOL(${tc.tool} ${tc.file || tc.symbol || ''}):\n${out.slice(0, 3200)}`);
|
|
330
|
+
}
|
|
331
|
+
continue;
|
|
332
|
+
}
|
|
333
|
+
for (const f of parsedTurn?.findings ?? []) {
|
|
334
|
+
if (!f.file || !f.title)
|
|
335
|
+
continue;
|
|
336
|
+
const file = toPosixPath(f.file);
|
|
337
|
+
const blast = blastLookup(file);
|
|
338
|
+
findings.push({
|
|
339
|
+
id: `main-ai-${crypto.createHash('sha1').update(`${persona}:${file}:${f.line_start}:${f.title}`).digest('hex').slice(0, 10)}`,
|
|
340
|
+
severity: f.severity ?? 'medium',
|
|
341
|
+
tier: mapSeverityToTier(f.severity ?? 'medium'),
|
|
342
|
+
confidence: Number(f.confidence ?? 0.8),
|
|
343
|
+
category: f.category ?? 'logic',
|
|
344
|
+
title: f.title,
|
|
345
|
+
body: f.body ?? f.title,
|
|
346
|
+
file,
|
|
347
|
+
line_start: Math.max(1, Number(f.line_start ?? 1)),
|
|
348
|
+
line_end: Math.max(1, Number(f.line_end ?? f.line_start ?? 1)),
|
|
349
|
+
symbol: f.symbol,
|
|
350
|
+
source: 'ai',
|
|
351
|
+
blast_radius: {
|
|
352
|
+
risk_score: blast?.riskScore ?? 45,
|
|
353
|
+
direct_callers: blast?.directCallers.map((c) => `${c.file}:${c.line} (${c.name})`) ?? [],
|
|
354
|
+
affected_tests: blast?.affectedTests ?? [],
|
|
355
|
+
impacted_files: blast?.dependents ?? []
|
|
356
|
+
},
|
|
357
|
+
recommendation: f.recommendation ?? 'Review and fix.',
|
|
358
|
+
evidence: f.evidence,
|
|
359
|
+
status: 'open'
|
|
360
|
+
});
|
|
361
|
+
}
|
|
362
|
+
for (const fEntry of batch.files)
|
|
363
|
+
auditedFiles.add(fEntry.file);
|
|
364
|
+
break;
|
|
365
|
+
}
|
|
366
|
+
return { findings, auditedFiles: Array.from(auditedFiles) };
|
|
367
|
+
}
|
|
245
368
|
/**
|
|
246
369
|
* Stage 4: Independent Tool-Using Verifier Agent (Persona B Skeptic).
|
|
247
|
-
*
|
|
370
|
+
* Verifies ALL priority candidate findings in chunked batches of 25 (no 30-item truncation cap!),
|
|
371
|
+
* using real read-only AST, DB schema, and git tools.
|
|
248
372
|
*/
|
|
249
373
|
export async function runIndependentVerifierStage(options) {
|
|
250
|
-
const { aiClient, repoPath, nodes, edges, candidates, contextSummary = '' } = options;
|
|
374
|
+
const { aiClient, repoPath, nodes, edges, candidates, contextSummary = '', batchSize = 25 } = options;
|
|
251
375
|
if (!aiClient.isConfigured() || candidates.length === 0) {
|
|
252
376
|
return {
|
|
253
377
|
verifiedFindings: candidates,
|
|
@@ -256,7 +380,6 @@ export async function runIndependentVerifierStage(options) {
|
|
|
256
380
|
refutedCount: 0
|
|
257
381
|
};
|
|
258
382
|
}
|
|
259
|
-
// Review AI / synthesis / high-severity findings thoroughly (cap batch payload cleanly at top 30 priority candidates)
|
|
260
383
|
const sortedForVerify = [...candidates].sort((a, b) => {
|
|
261
384
|
const aiDiff = (b.source === 'ai' || b.source === 'debate' || b.source === 'ultrareview' ? 1 : 0) -
|
|
262
385
|
(a.source === 'ai' || a.source === 'debate' || a.source === 'ultrareview' ? 1 : 0);
|
|
@@ -265,8 +388,6 @@ export async function runIndependentVerifierStage(options) {
|
|
|
265
388
|
const rank = { critical: 4, high: 3, medium: 2, low: 1 };
|
|
266
389
|
return rank[b.severity] - rank[a.severity];
|
|
267
390
|
});
|
|
268
|
-
const toVerify = sortedForVerify.slice(0, 30);
|
|
269
|
-
const untouchedTail = sortedForVerify.slice(30);
|
|
270
391
|
const verifierSystem = `You are Stage 4 Independent Verifier Agent (Persona B Skeptic).
|
|
271
392
|
Your mission is to independently verify every candidate finding against real source code, AST callers, SQL schemas, and git history.
|
|
272
393
|
You have up to 3 read-only tool turns:
|
|
@@ -278,169 +399,166 @@ You have up to 3 read-only tool turns:
|
|
|
278
399
|
"refuted_ids": ["exact_finding_id_if_false_positive"],
|
|
279
400
|
"refuted_count": 0
|
|
280
401
|
}`;
|
|
281
|
-
const
|
|
282
|
-
|
|
283
|
-
|
|
284
|
-
|
|
285
|
-
|
|
286
|
-
|
|
287
|
-
|
|
288
|
-
|
|
289
|
-
|
|
290
|
-
|
|
291
|
-
|
|
292
|
-
|
|
293
|
-
|
|
294
|
-
|
|
295
|
-
|
|
296
|
-
|
|
297
|
-
|
|
298
|
-
|
|
299
|
-
|
|
300
|
-
|
|
301
|
-
|
|
302
|
-
|
|
303
|
-
|
|
304
|
-
|
|
305
|
-
|
|
306
|
-
|
|
307
|
-
|
|
308
|
-
|
|
309
|
-
|
|
310
|
-
|
|
311
|
-
|
|
312
|
-
|
|
313
|
-
|
|
314
|
-
|
|
315
|
-
|
|
316
|
-
|
|
317
|
-
|
|
318
|
-
|
|
402
|
+
const effectiveBatchSize = Math.max(5, Math.min(40, batchSize));
|
|
403
|
+
const reconciledAll = [];
|
|
404
|
+
let verifiedCount = 0;
|
|
405
|
+
let contestedCount = 0;
|
|
406
|
+
let refutedCount = 0;
|
|
407
|
+
let budgetWarning;
|
|
408
|
+
let stoppedEarly = false;
|
|
409
|
+
for (let offset = 0; offset < sortedForVerify.length; offset += effectiveBatchSize) {
|
|
410
|
+
const chunk = sortedForVerify.slice(offset, offset + effectiveBatchSize);
|
|
411
|
+
const compactCandidates = chunk.map((f) => ({
|
|
412
|
+
id: f.id,
|
|
413
|
+
severity: f.severity,
|
|
414
|
+
confidence: f.confidence,
|
|
415
|
+
category: f.category,
|
|
416
|
+
source: f.source,
|
|
417
|
+
file: f.file,
|
|
418
|
+
line_start: f.line_start,
|
|
419
|
+
line_end: f.line_end,
|
|
420
|
+
symbol: f.symbol,
|
|
421
|
+
title: f.title,
|
|
422
|
+
body: f.body.slice(0, 500),
|
|
423
|
+
recommendation: f.recommendation,
|
|
424
|
+
evidence: f.evidence?.slice(0, 300)
|
|
425
|
+
}));
|
|
426
|
+
const transcript = [];
|
|
427
|
+
let verdictObj = null;
|
|
428
|
+
try {
|
|
429
|
+
for (let turn = 0; turn < 3; turn++) {
|
|
430
|
+
const userPrompt = [
|
|
431
|
+
`CANDIDATE FINDINGS TO VERIFY (Batch ${Math.floor(offset / effectiveBatchSize) + 1}/${Math.ceil(sortedForVerify.length / effectiveBatchSize)}, ${compactCandidates.length} items):`,
|
|
432
|
+
JSON.stringify(compactCandidates, null, 2),
|
|
433
|
+
contextSummary ? `\nCONTEXT DIGEST:\n${contextSummary.slice(0, 6000)}` : '',
|
|
434
|
+
transcript.length > 0 ? `\nVERIFIER TOOL OBSERVATIONS:\n${transcript.join('\n\n')}` : ''
|
|
435
|
+
]
|
|
436
|
+
.filter(Boolean)
|
|
437
|
+
.join('\n');
|
|
438
|
+
const res = await aiClient.complete({
|
|
439
|
+
systemPrompt: `${verifierSystem}\n(Agent-2 Turn ${turn + 1}/3)`,
|
|
440
|
+
userPrompt,
|
|
441
|
+
temperature: 0.1,
|
|
442
|
+
jsonMode: true
|
|
443
|
+
});
|
|
444
|
+
const parsed = extractJsonObjectSafe(res.text);
|
|
445
|
+
if (parsed?.tool_calls && parsed.tool_calls.length > 0 && turn < 2) {
|
|
446
|
+
for (const tc of parsed.tool_calls.slice(0, 4)) {
|
|
447
|
+
const out = await executeAgentReadOnlyTool(repoPath, nodes, edges, tc);
|
|
448
|
+
transcript.push(`TOOL(${tc.tool} ${tc.file || tc.symbol || ''}):\n${out.slice(0, 3200)}`);
|
|
449
|
+
}
|
|
450
|
+
continue;
|
|
319
451
|
}
|
|
320
|
-
|
|
452
|
+
verdictObj = parsed;
|
|
453
|
+
break;
|
|
321
454
|
}
|
|
322
|
-
verdictObj = parsed;
|
|
323
|
-
break;
|
|
324
455
|
}
|
|
325
|
-
|
|
326
|
-
|
|
327
|
-
|
|
328
|
-
|
|
329
|
-
|
|
330
|
-
|
|
331
|
-
|
|
332
|
-
|
|
333
|
-
|
|
334
|
-
stoppedEarly: true
|
|
335
|
-
};
|
|
456
|
+
catch (err) {
|
|
457
|
+
if (err instanceof TokenBudgetExceededError) {
|
|
458
|
+
budgetWarning = err.message;
|
|
459
|
+
stoppedEarly = true;
|
|
460
|
+
reconciledAll.push(...sortedForVerify.slice(offset));
|
|
461
|
+
break;
|
|
462
|
+
}
|
|
463
|
+
reconciledAll.push(...chunk);
|
|
464
|
+
continue;
|
|
336
465
|
}
|
|
337
|
-
|
|
338
|
-
|
|
339
|
-
|
|
340
|
-
contestedCount: 0,
|
|
341
|
-
refutedCount: 0
|
|
342
|
-
};
|
|
343
|
-
}
|
|
344
|
-
if (!verdictObj) {
|
|
345
|
-
return {
|
|
346
|
-
verifiedFindings: candidates,
|
|
347
|
-
verifiedCount: 0,
|
|
348
|
-
contestedCount: 0,
|
|
349
|
-
refutedCount: 0
|
|
350
|
-
};
|
|
351
|
-
}
|
|
352
|
-
const byId = new Map(toVerify.map((f) => [f.id, f]));
|
|
353
|
-
const refutedSet = new Set(verdictObj.refuted_ids ?? []);
|
|
354
|
-
const handledIds = new Set(refutedSet);
|
|
355
|
-
const reconciled = [];
|
|
356
|
-
let verifiedCount = 0;
|
|
357
|
-
let contestedCount = 0;
|
|
358
|
-
const matchExisting = (item) => {
|
|
359
|
-
if (item.id && byId.has(item.id))
|
|
360
|
-
return byId.get(item.id);
|
|
361
|
-
if (item.file && item.title) {
|
|
362
|
-
const fNorm = toPosixPath(item.file);
|
|
363
|
-
return toVerify.find((x) => x.file === fNorm && x.title === item.title);
|
|
466
|
+
if (!verdictObj) {
|
|
467
|
+
reconciledAll.push(...chunk);
|
|
468
|
+
continue;
|
|
364
469
|
}
|
|
365
|
-
|
|
366
|
-
|
|
367
|
-
|
|
368
|
-
const
|
|
369
|
-
|
|
370
|
-
|
|
371
|
-
|
|
372
|
-
|
|
373
|
-
|
|
374
|
-
...(base ?? {
|
|
375
|
-
id: `v2-${crypto.createHash('sha1').update(`${file}:${item.line_start ?? 1}:${item.title ?? ''}`).digest('hex').slice(0, 10)}`,
|
|
376
|
-
severity: item.severity ?? 'medium',
|
|
377
|
-
tier: mapSeverityToTier(item.severity ?? 'medium'),
|
|
378
|
-
confidence: Number(item.confidence ?? 0.88),
|
|
379
|
-
category: item.category ?? 'logic',
|
|
380
|
-
title: String(item.title ?? 'Verified finding'),
|
|
381
|
-
body: String(item.body ?? item.title ?? 'Verified by Agent-2'),
|
|
382
|
-
file,
|
|
383
|
-
line_start: Math.max(1, Number(item.line_start ?? 1)),
|
|
384
|
-
line_end: Math.max(1, Number(item.line_end ?? item.line_start ?? 1)),
|
|
385
|
-
source: 'debate',
|
|
386
|
-
blast_radius: { risk_score: 40, direct_callers: [], affected_tests: [], impacted_files: [] },
|
|
387
|
-
recommendation: String(item.recommendation ?? 'Fix verified issue.'),
|
|
388
|
-
status: 'open'
|
|
389
|
-
}),
|
|
390
|
-
debate: {
|
|
391
|
-
status: 'agreed',
|
|
392
|
-
reviewer_argument: item.reviewer_argument ?? 'Flagged during Stage 2/3 deep discovery.',
|
|
393
|
-
skeptic_argument: item.skeptic_argument ?? 'Independently checked referenced code and callers.',
|
|
394
|
-
resolution_note: `✓ Verified by Agent-2: ${item.resolution_note ?? 'Confirmed grounded in repository code.'}`
|
|
470
|
+
const byId = new Map(chunk.map((f) => [f.id, f]));
|
|
471
|
+
const refutedSet = new Set(verdictObj.refuted_ids ?? []);
|
|
472
|
+
const handledIds = new Set(refutedSet);
|
|
473
|
+
const matchExisting = (item) => {
|
|
474
|
+
if (item.id && byId.has(item.id))
|
|
475
|
+
return byId.get(item.id);
|
|
476
|
+
if (item.file && item.title) {
|
|
477
|
+
const fNorm = toPosixPath(item.file);
|
|
478
|
+
return chunk.find((x) => x.file === fNorm && x.title === item.title);
|
|
395
479
|
}
|
|
396
|
-
|
|
397
|
-
|
|
398
|
-
|
|
399
|
-
|
|
400
|
-
|
|
401
|
-
|
|
402
|
-
|
|
403
|
-
|
|
404
|
-
|
|
405
|
-
|
|
406
|
-
|
|
407
|
-
|
|
408
|
-
|
|
409
|
-
|
|
410
|
-
|
|
411
|
-
|
|
412
|
-
|
|
413
|
-
|
|
414
|
-
|
|
415
|
-
|
|
416
|
-
|
|
417
|
-
|
|
418
|
-
|
|
419
|
-
|
|
420
|
-
|
|
421
|
-
|
|
422
|
-
|
|
423
|
-
|
|
424
|
-
|
|
425
|
-
|
|
480
|
+
return undefined;
|
|
481
|
+
};
|
|
482
|
+
for (const item of verdictObj.agreed ?? []) {
|
|
483
|
+
const base = matchExisting(item);
|
|
484
|
+
if (base)
|
|
485
|
+
handledIds.add(base.id);
|
|
486
|
+
verifiedCount++;
|
|
487
|
+
const file = toPosixPath(item.file ?? base?.file ?? 'unknown');
|
|
488
|
+
reconciledAll.push({
|
|
489
|
+
...(base ?? {
|
|
490
|
+
id: `v2-${crypto.createHash('sha1').update(`${file}:${item.line_start ?? 1}:${item.title ?? ''}`).digest('hex').slice(0, 10)}`,
|
|
491
|
+
severity: item.severity ?? 'medium',
|
|
492
|
+
tier: mapSeverityToTier(item.severity ?? 'medium'),
|
|
493
|
+
confidence: Number(item.confidence ?? 0.88),
|
|
494
|
+
category: item.category ?? 'logic',
|
|
495
|
+
title: String(item.title ?? 'Verified finding'),
|
|
496
|
+
body: String(item.body ?? item.title ?? 'Verified by Agent-2'),
|
|
497
|
+
file,
|
|
498
|
+
line_start: Math.max(1, Number(item.line_start ?? 1)),
|
|
499
|
+
line_end: Math.max(1, Number(item.line_end ?? item.line_start ?? 1)),
|
|
500
|
+
source: 'debate',
|
|
501
|
+
blast_radius: { risk_score: 40, direct_callers: [], affected_tests: [], impacted_files: [] },
|
|
502
|
+
recommendation: String(item.recommendation ?? 'Fix verified issue.'),
|
|
503
|
+
status: 'open'
|
|
504
|
+
}),
|
|
505
|
+
debate: {
|
|
506
|
+
status: 'agreed',
|
|
507
|
+
reviewer_argument: item.reviewer_argument ?? 'Flagged during Stage 2/3 deep discovery.',
|
|
508
|
+
skeptic_argument: item.skeptic_argument ?? 'Independently checked referenced code and callers.',
|
|
509
|
+
resolution_note: `✓ Verified by Agent-2: ${item.resolution_note ?? 'Confirmed grounded in repository code.'}`
|
|
510
|
+
}
|
|
511
|
+
});
|
|
512
|
+
}
|
|
513
|
+
for (const item of verdictObj.contested ?? []) {
|
|
514
|
+
const base = matchExisting(item);
|
|
515
|
+
if (base)
|
|
516
|
+
handledIds.add(base.id);
|
|
517
|
+
contestedCount++;
|
|
518
|
+
const file = toPosixPath(item.file ?? base?.file ?? 'unknown');
|
|
519
|
+
reconciledAll.push({
|
|
520
|
+
...(base ?? {
|
|
521
|
+
id: `c2-${crypto.createHash('sha1').update(`${file}:${item.line_start ?? 1}:${item.title ?? ''}`).digest('hex').slice(0, 10)}`,
|
|
522
|
+
severity: item.severity ?? 'medium',
|
|
523
|
+
tier: mapSeverityToTier(item.severity ?? 'medium'),
|
|
524
|
+
confidence: Number(item.confidence ?? 0.65),
|
|
525
|
+
category: item.category ?? 'logic',
|
|
526
|
+
title: String(item.title ?? 'Contested finding'),
|
|
527
|
+
body: String(item.body ?? item.title ?? 'Contested by Agent-2'),
|
|
528
|
+
file,
|
|
529
|
+
line_start: Math.max(1, Number(item.line_start ?? 1)),
|
|
530
|
+
line_end: Math.max(1, Number(item.line_end ?? item.line_start ?? 1)),
|
|
531
|
+
source: 'debate',
|
|
532
|
+
blast_radius: { risk_score: 30, direct_callers: [], affected_tests: [], impacted_files: [] },
|
|
533
|
+
recommendation: String(item.recommendation ?? 'Inspect runtime conditions.'),
|
|
534
|
+
status: 'open'
|
|
535
|
+
}),
|
|
536
|
+
debate: {
|
|
537
|
+
status: 'contested',
|
|
538
|
+
reviewer_argument: item.reviewer_argument ?? 'Candidate risk detected.',
|
|
539
|
+
skeptic_argument: item.skeptic_argument ?? 'Mitigation or precondition may exist upstream.',
|
|
540
|
+
resolution_note: item.resolution_note ?? 'Requires human confirmation.'
|
|
541
|
+
}
|
|
542
|
+
});
|
|
543
|
+
}
|
|
544
|
+
// Deterministic SAST/AST/OSV findings NOT explicitly refuted remain protected
|
|
545
|
+
for (const f of chunk) {
|
|
546
|
+
if (handledIds.has(f.id))
|
|
547
|
+
continue;
|
|
548
|
+
if (f.source === 'sast' || f.source === 'ast') {
|
|
549
|
+
reconciledAll.push(f);
|
|
426
550
|
}
|
|
427
|
-
});
|
|
428
|
-
}
|
|
429
|
-
// Deterministic SAST/AST/OSV findings NOT explicitly refuted remain protected
|
|
430
|
-
for (const f of toVerify) {
|
|
431
|
-
if (handledIds.has(f.id))
|
|
432
|
-
continue;
|
|
433
|
-
if (f.source === 'sast' || f.source === 'ast') {
|
|
434
|
-
reconciled.push(f);
|
|
435
551
|
}
|
|
552
|
+
const droppedUnmentionedAi = chunk.filter((f) => !handledIds.has(f.id) && f.source !== 'sast' && f.source !== 'ast').length;
|
|
553
|
+
refutedCount += Math.max(Number(verdictObj.refuted_count ?? 0), refutedSet.size + droppedUnmentionedAi);
|
|
436
554
|
}
|
|
437
|
-
const droppedUnmentionedAi = toVerify.filter((f) => !handledIds.has(f.id) && f.source !== 'sast' && f.source !== 'ast').length;
|
|
438
|
-
const refutedCount = Math.max(Number(verdictObj.refuted_count ?? 0), refutedSet.size + droppedUnmentionedAi);
|
|
439
555
|
return {
|
|
440
|
-
verifiedFindings:
|
|
556
|
+
verifiedFindings: reconciledAll,
|
|
441
557
|
verifiedCount,
|
|
442
558
|
contestedCount,
|
|
443
|
-
refutedCount
|
|
559
|
+
refutedCount,
|
|
560
|
+
...(budgetWarning ? { budgetWarning } : {}),
|
|
561
|
+
...(stoppedEarly ? { stoppedEarly } : {})
|
|
444
562
|
};
|
|
445
563
|
}
|
|
446
564
|
//# sourceMappingURL=pipeline-stages.js.map
|