sentinelayer-cli 0.4.5 → 0.6.2
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/README.md +996 -998
- package/bin/create-sentinelayer.js +5 -5
- package/bin/sentinelayer-cli.js +4 -4
- package/bin/sl.js +5 -5
- package/package.json +63 -63
- package/src/agents/jules/config/definition.js +160 -209
- package/src/agents/jules/config/system-prompt.js +182 -175
- package/src/agents/jules/error-intake.js +51 -51
- package/src/agents/jules/fix-cycle.js +17 -377
- package/src/agents/jules/loop.js +450 -367
- package/src/agents/jules/pulse.js +10 -327
- package/src/agents/jules/stream.js +186 -186
- package/src/agents/jules/swarm/file-scanner.js +74 -74
- package/src/agents/jules/swarm/index.js +11 -11
- package/src/agents/jules/swarm/orchestrator.js +362 -362
- package/src/agents/jules/swarm/pattern-hunter.js +123 -123
- package/src/agents/jules/swarm/sub-agent.js +309 -308
- package/src/agents/jules/tools/aidenid-email.js +189 -0
- package/src/agents/jules/tools/auth-audit.js +1691 -557
- package/src/agents/jules/tools/dispatch.js +335 -327
- package/src/agents/jules/tools/file-edit.js +2 -180
- package/src/agents/jules/tools/file-read.js +2 -100
- package/src/agents/jules/tools/frontend-analyze.js +570 -570
- package/src/agents/jules/tools/glob.js +2 -168
- package/src/agents/jules/tools/grep.js +2 -228
- package/src/agents/jules/tools/index.js +29 -29
- package/src/agents/jules/tools/path-guards.js +2 -161
- package/src/agents/jules/tools/runtime-audit.js +507 -503
- package/src/agents/jules/tools/shell.js +2 -383
- package/src/agents/jules/tools/url-policy.js +100 -100
- package/src/agents/persona-visuals.js +61 -0
- package/src/agents/shared-tools/dispatch-core.js +315 -0
- package/src/agents/shared-tools/file-edit.js +180 -0
- package/src/agents/shared-tools/file-read.js +100 -0
- package/src/agents/shared-tools/glob.js +168 -0
- package/src/agents/shared-tools/grep.js +228 -0
- package/src/agents/shared-tools/index.js +46 -0
- package/src/agents/shared-tools/path-guards.js +161 -0
- package/src/agents/shared-tools/shell.js +383 -0
- package/src/ai/aidenid.js +1009 -972
- package/src/ai/client.js +553 -508
- package/src/ai/domain-target-store.js +268 -268
- package/src/ai/identity-store.js +270 -270
- package/src/ai/proxy.js +137 -0
- package/src/ai/site-store.js +145 -145
- package/src/audit/agents/architecture.js +180 -180
- package/src/audit/agents/compliance.js +179 -179
- package/src/audit/agents/documentation.js +165 -165
- package/src/audit/agents/performance.js +145 -145
- package/src/audit/agents/security.js +215 -215
- package/src/audit/agents/testing.js +172 -172
- package/src/audit/orchestrator.js +557 -557
- package/src/audit/package.js +204 -204
- package/src/audit/registry.js +284 -284
- package/src/audit/replay.js +103 -103
- package/src/auth/gate.js +371 -126
- package/src/auth/http.js +611 -270
- package/src/auth/service.js +1106 -891
- package/src/auth/session-store.js +813 -359
- package/src/cli.js +252 -252
- package/src/commands/ai/identity-lifecycle.js +1338 -1338
- package/src/commands/ai/provision-governance.js +1272 -1272
- package/src/commands/ai/shared.js +147 -147
- package/src/commands/ai.js +11 -11
- package/src/commands/apply.js +12 -12
- package/src/commands/audit.js +1166 -1166
- package/src/commands/auth.js +419 -375
- package/src/commands/chat.js +191 -191
- package/src/commands/config.js +184 -184
- package/src/commands/cost.js +311 -311
- package/src/commands/daemon/core.js +850 -850
- package/src/commands/daemon/extended.js +1048 -1048
- package/src/commands/daemon/shared.js +213 -213
- package/src/commands/daemon.js +11 -11
- package/src/commands/guide.js +174 -174
- package/src/commands/ingest.js +58 -58
- package/src/commands/init.js +55 -55
- package/src/commands/legacy-args.js +10 -10
- package/src/commands/mcp.js +461 -461
- package/src/commands/omargate.js +29 -21
- package/src/commands/persona.js +20 -20
- package/src/commands/plugin.js +260 -260
- package/src/commands/policy.js +132 -132
- package/src/commands/prompt.js +238 -238
- package/src/commands/review.js +704 -704
- package/src/commands/scan.js +872 -866
- package/src/commands/spec.js +716 -716
- package/src/commands/swarm.js +651 -651
- package/src/commands/telemetry.js +202 -202
- package/src/commands/watch.js +511 -510
- package/src/config/agent-dictionary.js +182 -182
- package/src/config/io.js +56 -56
- package/src/config/paths.js +18 -18
- package/src/config/schema.js +55 -55
- package/src/config/service.js +184 -184
- package/src/cost/budget.js +235 -235
- package/src/cost/history.js +188 -188
- package/src/cost/tracker.js +171 -171
- package/src/daemon/artifact-lineage.js +534 -534
- package/src/daemon/assignment-ledger.js +770 -770
- package/src/daemon/ast-parser-layer.js +258 -258
- package/src/daemon/budget-governor.js +633 -633
- package/src/daemon/callgraph-overlay.js +646 -646
- package/src/daemon/error-worker.js +626 -626
- package/src/daemon/fix-cycle.js +377 -0
- package/src/daemon/hybrid-mapper.js +929 -929
- package/src/daemon/jira-lifecycle.js +632 -632
- package/src/daemon/operator-control.js +657 -657
- package/src/daemon/pulse.js +327 -0
- package/src/daemon/reliability-lane.js +471 -471
- package/src/daemon/watchdog.js +971 -971
- package/src/guide/generator.js +316 -316
- package/src/ingest/engine.js +918 -918
- package/src/interactive/index.js +97 -95
- package/src/legacy-cli.js +2994 -2592
- package/src/mcp/registry.js +695 -695
- package/src/memory/blackboard.js +301 -301
- package/src/memory/retrieval.js +581 -581
- package/src/plugin/manifest.js +553 -553
- package/src/policy/packs.js +144 -144
- package/src/prompt/generator.js +118 -118
- package/src/review/ai-review.js +679 -669
- package/src/review/local-review.js +1305 -1295
- package/src/review/omargate-interactive.js +68 -0
- package/src/review/omargate-orchestrator.js +300 -0
- package/src/review/persona-prompts.js +296 -0
- package/src/review/replay.js +235 -235
- package/src/review/report.js +664 -664
- package/src/review/scan-modes.js +42 -0
- package/src/review/spec-binding.js +487 -487
- package/src/scaffold/generator.js +67 -67
- package/src/scaffold/templates.js +150 -150
- package/src/scan/generator.js +418 -418
- package/src/scan/gh-secrets.js +107 -107
- package/src/spec/generator.js +519 -519
- package/src/spec/regenerate.js +237 -237
- package/src/spec/templates.js +91 -91
- package/src/swarm/dashboard.js +247 -247
- package/src/swarm/factory.js +363 -363
- package/src/swarm/pentest.js +934 -934
- package/src/swarm/registry.js +419 -419
- package/src/swarm/report.js +158 -158
- package/src/swarm/runtime.js +576 -576
- package/src/swarm/scenario-dsl.js +272 -272
- package/src/telemetry/ledger.js +302 -302
- package/src/telemetry/session-tracker.js +234 -118
- package/src/telemetry/sync.js +203 -199
- package/src/ui/command-hints.js +13 -0
- package/src/ui/markdown.js +220 -220
package/src/review/ai-review.js
CHANGED
|
@@ -1,669 +1,679 @@
|
|
|
1
|
-
import fsp from "node:fs/promises";
|
|
2
|
-
import path from "node:path";
|
|
3
|
-
|
|
4
|
-
import {
|
|
5
|
-
createMultiProviderApiClient,
|
|
6
|
-
resolveModel,
|
|
7
|
-
resolveProvider,
|
|
8
|
-
} from "../ai/client.js";
|
|
9
|
-
import { loadConfig } from "../config/service.js";
|
|
10
|
-
import { evaluateBudget } from "../cost/budget.js";
|
|
11
|
-
import { appendCostEntry, summarizeCostHistory } from "../cost/history.js";
|
|
12
|
-
import { estimateModelCost } from "../cost/tracker.js";
|
|
13
|
-
import { appendRunEvent, deriveStopClassFromBudget } from "../telemetry/ledger.js";
|
|
14
|
-
|
|
15
|
-
const AI_SEVERITIES = new Set(["P0", "P1", "P2", "P3"]);
|
|
16
|
-
const DEFAULT_AI_MAX_FINDINGS = 20;
|
|
17
|
-
const DEFAULT_REVIEW_AI_MODEL = "gpt-5.3-codex";
|
|
18
|
-
|
|
19
|
-
function normalizeString(value) {
|
|
20
|
-
return String(value || "").trim();
|
|
21
|
-
}
|
|
22
|
-
|
|
23
|
-
function parseNonNegativeNumber(rawValue, field) {
|
|
24
|
-
const normalized = Number(rawValue || 0);
|
|
25
|
-
if (!Number.isFinite(normalized) || normalized < 0) {
|
|
26
|
-
throw new Error(`${field} must be a non-negative number.`);
|
|
27
|
-
}
|
|
28
|
-
return normalized;
|
|
29
|
-
}
|
|
30
|
-
|
|
31
|
-
function parsePercent(rawValue, field) {
|
|
32
|
-
const normalized = Number(rawValue || 0);
|
|
33
|
-
if (!Number.isFinite(normalized) || normalized < 0 || normalized > 100) {
|
|
34
|
-
throw new Error(`${field} must be between 0 and 100.`);
|
|
35
|
-
}
|
|
36
|
-
return normalized;
|
|
37
|
-
}
|
|
38
|
-
|
|
39
|
-
function estimateTokenCount(text) {
|
|
40
|
-
const normalized = String(text || "");
|
|
41
|
-
if (!normalized) {
|
|
42
|
-
return 0;
|
|
43
|
-
}
|
|
44
|
-
return Math.max(1, Math.ceil(normalized.length / 4));
|
|
45
|
-
}
|
|
46
|
-
|
|
47
|
-
function resolveConfiguredApiKey(provider, resolvedConfig = {}) {
|
|
48
|
-
const normalizedProvider = normalizeString(provider).toLowerCase();
|
|
49
|
-
if (normalizedProvider === "openai") {
|
|
50
|
-
return normalizeString(resolvedConfig.openaiApiKey);
|
|
51
|
-
}
|
|
52
|
-
if (normalizedProvider === "anthropic") {
|
|
53
|
-
return normalizeString(resolvedConfig.anthropicApiKey);
|
|
54
|
-
}
|
|
55
|
-
if (normalizedProvider === "google") {
|
|
56
|
-
return normalizeString(resolvedConfig.googleApiKey);
|
|
57
|
-
}
|
|
58
|
-
return "";
|
|
59
|
-
}
|
|
60
|
-
|
|
61
|
-
function sanitizeExcerpt(text) {
|
|
62
|
-
return String(text || "")
|
|
63
|
-
.trim()
|
|
64
|
-
.replace(/\s+/g, " ")
|
|
65
|
-
.slice(0, 180);
|
|
66
|
-
}
|
|
67
|
-
|
|
68
|
-
function extractJsonPayload(rawText) {
|
|
69
|
-
const text = String(rawText || "").trim();
|
|
70
|
-
if (!text) {
|
|
71
|
-
return null;
|
|
72
|
-
}
|
|
73
|
-
|
|
74
|
-
const candidates = [];
|
|
75
|
-
const fencedMatch = text.match(/```(?:json)?\s*([\s\S]*?)```/i);
|
|
76
|
-
if (fencedMatch && fencedMatch[1]) {
|
|
77
|
-
candidates.push(fencedMatch[1].trim());
|
|
78
|
-
}
|
|
79
|
-
candidates.push(text);
|
|
80
|
-
|
|
81
|
-
const objectStart = text.indexOf("{");
|
|
82
|
-
const objectEnd = text.lastIndexOf("}");
|
|
83
|
-
if (objectStart >= 0 && objectEnd > objectStart) {
|
|
84
|
-
candidates.push(text.slice(objectStart, objectEnd + 1));
|
|
85
|
-
}
|
|
86
|
-
|
|
87
|
-
for (const candidate of candidates) {
|
|
88
|
-
try {
|
|
89
|
-
const parsed = JSON.parse(candidate);
|
|
90
|
-
if (parsed && typeof parsed === "object" && !Array.isArray(parsed)) {
|
|
91
|
-
return parsed;
|
|
92
|
-
}
|
|
93
|
-
} catch {
|
|
94
|
-
continue;
|
|
95
|
-
}
|
|
96
|
-
}
|
|
97
|
-
|
|
98
|
-
return null;
|
|
99
|
-
}
|
|
100
|
-
|
|
101
|
-
function normalizeSeverity(value) {
|
|
102
|
-
const normalized = normalizeString(value).toUpperCase();
|
|
103
|
-
if (AI_SEVERITIES.has(normalized)) {
|
|
104
|
-
return normalized;
|
|
105
|
-
}
|
|
106
|
-
return "P2";
|
|
107
|
-
}
|
|
108
|
-
|
|
109
|
-
function normalizeLine(value) {
|
|
110
|
-
const normalized = Number(value || 1);
|
|
111
|
-
if (!Number.isFinite(normalized) || normalized < 1) {
|
|
112
|
-
return 1;
|
|
113
|
-
}
|
|
114
|
-
return Math.floor(normalized);
|
|
115
|
-
}
|
|
116
|
-
|
|
117
|
-
function normalizeConfidence(value) {
|
|
118
|
-
if (value === undefined || value === null || value === "") {
|
|
119
|
-
return null;
|
|
120
|
-
}
|
|
121
|
-
const normalized = Number(value);
|
|
122
|
-
if (!Number.isFinite(normalized)) {
|
|
123
|
-
return null;
|
|
124
|
-
}
|
|
125
|
-
return Math.max(0, Math.min(1, normalized));
|
|
126
|
-
}
|
|
127
|
-
|
|
128
|
-
function normalizeAiFinding(rawFinding, index) {
|
|
129
|
-
if (!rawFinding || typeof rawFinding !== "object" || Array.isArray(rawFinding)) {
|
|
130
|
-
return null;
|
|
131
|
-
}
|
|
132
|
-
|
|
133
|
-
const message = normalizeString(rawFinding.title || rawFinding.message);
|
|
134
|
-
const rationale = normalizeString(rawFinding.rationale || rawFinding.excerpt);
|
|
135
|
-
const suggestedFix = normalizeString(rawFinding.suggestedFix);
|
|
136
|
-
|
|
137
|
-
return {
|
|
138
|
-
severity: normalizeSeverity(rawFinding.severity),
|
|
139
|
-
file: normalizeString(rawFinding.file) || "unknown",
|
|
140
|
-
line: normalizeLine(rawFinding.line),
|
|
141
|
-
message: message || `AI finding ${index + 1}`,
|
|
142
|
-
rationale: rationale || "AI reviewer flagged a potential issue requiring validation.",
|
|
143
|
-
suggestedFix: suggestedFix || "Review and remediate this finding.",
|
|
144
|
-
confidence: normalizeConfidence(rawFinding.confidence),
|
|
145
|
-
};
|
|
146
|
-
}
|
|
147
|
-
|
|
148
|
-
function summarizeFindings(findings = []) {
|
|
149
|
-
const summary = {
|
|
150
|
-
P0: 0,
|
|
151
|
-
P1: 0,
|
|
152
|
-
P2: 0,
|
|
153
|
-
P3: 0,
|
|
154
|
-
};
|
|
155
|
-
for (const finding of findings) {
|
|
156
|
-
const severity = normalizeSeverity(finding.severity);
|
|
157
|
-
summary[severity] += 1;
|
|
158
|
-
}
|
|
159
|
-
return {
|
|
160
|
-
...summary,
|
|
161
|
-
blocking: summary.P0 > 0 || summary.P1 > 0,
|
|
162
|
-
};
|
|
163
|
-
}
|
|
164
|
-
|
|
165
|
-
export function parseAiReviewResponse({ text, maxFindings = DEFAULT_AI_MAX_FINDINGS } = {}) {
|
|
166
|
-
const parsed = extractJsonPayload(text);
|
|
167
|
-
const normalizedMaxFindings = Math.max(1, Math.floor(Number(maxFindings || DEFAULT_AI_MAX_FINDINGS)));
|
|
168
|
-
|
|
169
|
-
if (!parsed) {
|
|
170
|
-
return {
|
|
171
|
-
parser: "fallback_text",
|
|
172
|
-
summary:
|
|
173
|
-
sanitizeExcerpt(text) || "AI response could not be parsed as JSON; no structured findings extracted.",
|
|
174
|
-
findings: [],
|
|
175
|
-
};
|
|
176
|
-
}
|
|
177
|
-
|
|
178
|
-
const summary = normalizeString(
|
|
179
|
-
parsed.summary?.highLevel || parsed.summary?.risk || parsed.summary?.text || parsed.summary
|
|
180
|
-
);
|
|
181
|
-
const rawFindings = Array.isArray(parsed.findings) ? parsed.findings : [];
|
|
182
|
-
const findings = [];
|
|
183
|
-
for (let index = 0; index < rawFindings.length; index += 1) {
|
|
184
|
-
if (findings.length >= normalizedMaxFindings) {
|
|
185
|
-
break;
|
|
186
|
-
}
|
|
187
|
-
const normalized = normalizeAiFinding(rawFindings[index], index);
|
|
188
|
-
if (normalized) {
|
|
189
|
-
findings.push(normalized);
|
|
190
|
-
}
|
|
191
|
-
}
|
|
192
|
-
|
|
193
|
-
return {
|
|
194
|
-
parser: "json",
|
|
195
|
-
summary: summary || "Structured AI response parsed successfully.",
|
|
196
|
-
findings,
|
|
197
|
-
};
|
|
198
|
-
}
|
|
199
|
-
|
|
200
|
-
function formatDeterministicFindingLine(finding) {
|
|
201
|
-
return `- [${finding.severity}] ${finding.file}:${finding.line} ${finding.message}`;
|
|
202
|
-
}
|
|
203
|
-
|
|
204
|
-
function buildScopedFileSummary(scopedFiles = [], maxItems = 200) {
|
|
205
|
-
const normalized = Array.isArray(scopedFiles) ? scopedFiles : [];
|
|
206
|
-
const visible = normalized.slice(0, maxItems);
|
|
207
|
-
const omitted = Math.max(0, normalized.length - visible.length);
|
|
208
|
-
const lines = visible.map((item) => `- ${item}`);
|
|
209
|
-
if (omitted > 0) {
|
|
210
|
-
lines.push(`- ... ${omitted} more files omitted`);
|
|
211
|
-
}
|
|
212
|
-
return lines.join("\n") || "- none";
|
|
213
|
-
}
|
|
214
|
-
|
|
215
|
-
export function buildAiReviewPrompt({
|
|
216
|
-
targetPath,
|
|
217
|
-
mode,
|
|
218
|
-
deterministicSummary,
|
|
219
|
-
deterministicFindings = [],
|
|
220
|
-
scopedFiles = [],
|
|
221
|
-
specContext = null,
|
|
222
|
-
maxFindings = DEFAULT_AI_MAX_FINDINGS,
|
|
223
|
-
} = {}) {
|
|
224
|
-
const normalizedSummary = deterministicSummary || { P0: 0, P1: 0, P2: 0, P3: 0 };
|
|
225
|
-
const findingLines = deterministicFindings
|
|
226
|
-
.slice(0, 120)
|
|
227
|
-
.map((finding) => formatDeterministicFindingLine(finding))
|
|
228
|
-
.join("\n");
|
|
229
|
-
const normalizedMaxFindings = Math.max(1, Math.floor(Number(maxFindings || DEFAULT_AI_MAX_FINDINGS)));
|
|
230
|
-
const specPath = normalizeString(specContext?.specPath) || "none";
|
|
231
|
-
const specHash = normalizeString(specContext?.specHashSha256) || "unknown";
|
|
232
|
-
const specEndpointCount = Number(specContext?.endpointCount || 0);
|
|
233
|
-
const specAcceptanceCriteriaCount = Number(specContext?.acceptanceCriteriaCount || 0);
|
|
234
|
-
const specPreview = Array.isArray(specContext?.endpointsPreview) ? specContext.endpointsPreview : [];
|
|
235
|
-
|
|
236
|
-
return [
|
|
237
|
-
"You are Sentinelayer Omar reviewer layer 9.3.",
|
|
238
|
-
"Review the deterministic findings and scoped files. Add ONLY materially new findings.",
|
|
239
|
-
"Do not repeat deterministic findings unless you add new exploitability rationale.",
|
|
240
|
-
"Output STRICT JSON only. Do not wrap in markdown.",
|
|
241
|
-
"",
|
|
242
|
-
"JSON schema:",
|
|
243
|
-
"{",
|
|
244
|
-
' "summary": {"risk": "low|medium|high|critical", "highLevel": "short summary"},',
|
|
245
|
-
' "findings": [',
|
|
246
|
-
" {",
|
|
247
|
-
' "severity": "P0|P1|P2|P3",',
|
|
248
|
-
' "file": "relative/path",',
|
|
249
|
-
' "line": 1,',
|
|
250
|
-
' "title": "finding title",',
|
|
251
|
-
' "rationale": "why this matters",',
|
|
252
|
-
' "suggestedFix": "specific remediation",',
|
|
253
|
-
' "confidence": 0.0',
|
|
254
|
-
" }",
|
|
255
|
-
" ]",
|
|
256
|
-
"}",
|
|
257
|
-
"",
|
|
258
|
-
`Maximum findings: ${normalizedMaxFindings}`,
|
|
259
|
-
"",
|
|
260
|
-
`Target path: ${targetPath}`,
|
|
261
|
-
`Review mode: ${mode}`,
|
|
262
|
-
`Deterministic summary: P0=${normalizedSummary.P0} P1=${normalizedSummary.P1} P2=${normalizedSummary.P2} P3=${normalizedSummary.P3}`,
|
|
263
|
-
`Spec path: ${specPath}`,
|
|
264
|
-
`Spec sha256: ${specHash}`,
|
|
265
|
-
`Spec endpoints declared: ${specEndpointCount}`,
|
|
266
|
-
`Spec acceptance criteria count: ${specAcceptanceCriteriaCount}`,
|
|
267
|
-
`Spec endpoint preview: ${specPreview.length > 0 ? specPreview.join(", ") : "none"}`,
|
|
268
|
-
"",
|
|
269
|
-
"Scoped files:",
|
|
270
|
-
buildScopedFileSummary(scopedFiles),
|
|
271
|
-
"",
|
|
272
|
-
"Deterministic findings:",
|
|
273
|
-
findingLines || "- none",
|
|
274
|
-
].join("\n");
|
|
275
|
-
}
|
|
276
|
-
|
|
277
|
-
function maybeEstimateModelCost({ modelId, inputTokens, outputTokens }) {
|
|
278
|
-
try {
|
|
279
|
-
return {
|
|
280
|
-
costUsd: estimateModelCost({
|
|
281
|
-
modelId,
|
|
282
|
-
inputTokens,
|
|
283
|
-
outputTokens,
|
|
284
|
-
}),
|
|
285
|
-
pricingFound: true,
|
|
286
|
-
};
|
|
287
|
-
} catch {
|
|
288
|
-
return {
|
|
289
|
-
costUsd: 0,
|
|
290
|
-
pricingFound: false,
|
|
291
|
-
};
|
|
292
|
-
}
|
|
293
|
-
}
|
|
294
|
-
|
|
295
|
-
function composeAiReviewMarkdown({
|
|
296
|
-
generatedAt,
|
|
297
|
-
runId,
|
|
298
|
-
mode,
|
|
299
|
-
parser,
|
|
300
|
-
summary,
|
|
301
|
-
provider,
|
|
302
|
-
model,
|
|
303
|
-
dryRun,
|
|
304
|
-
findings = [],
|
|
305
|
-
usage,
|
|
306
|
-
combinedSummary,
|
|
307
|
-
} = {}) {
|
|
308
|
-
const findingLines =
|
|
309
|
-
findings.length > 0
|
|
310
|
-
? findings
|
|
311
|
-
.map(
|
|
312
|
-
(finding, index) =>
|
|
313
|
-
`${index + 1}. [${finding.severity}] ${finding.file}:${finding.line} ${finding.message}\n` +
|
|
314
|
-
` rationale: ${finding.rationale}\n` +
|
|
315
|
-
` suggested_fix: ${finding.suggestedFix}` +
|
|
316
|
-
(finding.confidence === null ? "" : `\n confidence: ${finding.confidence.toFixed(2)}`)
|
|
317
|
-
)
|
|
318
|
-
.join("\n")
|
|
319
|
-
: "- none";
|
|
320
|
-
|
|
321
|
-
return [
|
|
322
|
-
"# REVIEW_AI",
|
|
323
|
-
"",
|
|
324
|
-
`Generated: ${generatedAt}`,
|
|
325
|
-
`Run ID: ${runId}`,
|
|
326
|
-
`Mode: ${mode}`,
|
|
327
|
-
`Provider: ${provider}`,
|
|
328
|
-
`Model: ${model}`,
|
|
329
|
-
`Dry run: ${dryRun ? "yes" : "no"}`,
|
|
330
|
-
`Parser: ${parser}`,
|
|
331
|
-
"",
|
|
332
|
-
"Summary:",
|
|
333
|
-
`- ${summary || "No summary provided."}`,
|
|
334
|
-
`- Combined findings: P0=${combinedSummary.P0} P1=${combinedSummary.P1} P2=${combinedSummary.P2} P3=${combinedSummary.P3}`,
|
|
335
|
-
`- Blocking: ${combinedSummary.blocking ? "yes" : "no"}`,
|
|
336
|
-
`- Usage: input_tokens=${usage.inputTokens} output_tokens=${usage.outputTokens} cost_usd=${usage.costUsd.toFixed(6)} duration_ms=${usage.durationMs}`,
|
|
337
|
-
"",
|
|
338
|
-
"AI Findings:",
|
|
339
|
-
findingLines,
|
|
340
|
-
"",
|
|
341
|
-
].join("\n");
|
|
342
|
-
}
|
|
343
|
-
|
|
344
|
-
function toReviewFinding(aiFinding, index) {
|
|
345
|
-
return {
|
|
346
|
-
severity: aiFinding.severity,
|
|
347
|
-
file: aiFinding.file,
|
|
348
|
-
line: aiFinding.line,
|
|
349
|
-
message: aiFinding.message,
|
|
350
|
-
excerpt: sanitizeExcerpt(aiFinding.rationale),
|
|
351
|
-
ruleId: `SL-AI-${String(index + 1).padStart(3, "0")}`,
|
|
352
|
-
suggestedFix: aiFinding.suggestedFix,
|
|
353
|
-
layer: "ai_reasoning",
|
|
354
|
-
confidence: aiFinding.confidence,
|
|
355
|
-
};
|
|
356
|
-
}
|
|
357
|
-
|
|
358
|
-
function buildDryRunResponse({ deterministicSummary, maxFindings } = {}) {
|
|
359
|
-
const findingCount = Math.max(1, Math.min(2, Math.floor(Number(maxFindings || 1))));
|
|
360
|
-
const findings = [];
|
|
361
|
-
for (let index = 0; index < findingCount; index += 1) {
|
|
362
|
-
findings.push({
|
|
363
|
-
severity: index === 0 ? "P2" : "P3",
|
|
364
|
-
file: "src/example.js",
|
|
365
|
-
line: 1 + index,
|
|
366
|
-
title: `DRY_RUN finding ${index + 1}`,
|
|
367
|
-
rationale: `Synthetic AI rationale with deterministic context P1=${deterministicSummary.P1}.`,
|
|
368
|
-
suggestedFix: "Validate this path with targeted remediation.",
|
|
369
|
-
confidence: index === 0 ? 0.72 : 0.54,
|
|
370
|
-
});
|
|
371
|
-
}
|
|
372
|
-
return JSON.stringify(
|
|
373
|
-
{
|
|
374
|
-
summary: {
|
|
375
|
-
risk: "medium",
|
|
376
|
-
highLevel: "DRY_RUN_RESPONSE: synthetic AI review output.",
|
|
377
|
-
},
|
|
378
|
-
findings,
|
|
379
|
-
},
|
|
380
|
-
null,
|
|
381
|
-
2
|
|
382
|
-
);
|
|
383
|
-
}
|
|
384
|
-
|
|
385
|
-
export async function runAiReviewLayer({
|
|
386
|
-
targetPath,
|
|
387
|
-
mode,
|
|
388
|
-
runId,
|
|
389
|
-
runDirectory,
|
|
390
|
-
deterministic,
|
|
391
|
-
outputDir = "",
|
|
392
|
-
provider,
|
|
393
|
-
model,
|
|
394
|
-
apiKey,
|
|
395
|
-
sessionId,
|
|
396
|
-
maxFindings = DEFAULT_AI_MAX_FINDINGS,
|
|
397
|
-
maxCostUsd = 1.0,
|
|
398
|
-
maxOutputTokens = 0,
|
|
399
|
-
maxRuntimeMs = 0,
|
|
400
|
-
maxToolCalls = 0,
|
|
401
|
-
maxNoProgress = 3,
|
|
402
|
-
warningThresholdPercent = 80,
|
|
403
|
-
dryRun = false,
|
|
404
|
-
env = process.env,
|
|
405
|
-
} = {}) {
|
|
406
|
-
const normalizedTargetPath = path.resolve(String(targetPath || "."));
|
|
407
|
-
const normalizedRunDirectory = path.resolve(String(runDirectory || "."));
|
|
408
|
-
const normalizedMode = normalizeString(mode) || "full";
|
|
409
|
-
const normalizedMaxFindings = Math.max(
|
|
410
|
-
1,
|
|
411
|
-
Math.floor(Number(maxFindings || DEFAULT_AI_MAX_FINDINGS))
|
|
412
|
-
);
|
|
413
|
-
const normalizedRunId = normalizeString(runId) || "review-ai";
|
|
414
|
-
|
|
415
|
-
const config = await loadConfig({ cwd: normalizedTargetPath, env });
|
|
416
|
-
|
|
417
|
-
provider,
|
|
418
|
-
configProvider: config.resolved.defaultModelProvider,
|
|
419
|
-
env,
|
|
420
|
-
});
|
|
421
|
-
|
|
422
|
-
|
|
423
|
-
|
|
424
|
-
|
|
425
|
-
|
|
426
|
-
|
|
427
|
-
|
|
428
|
-
|
|
429
|
-
|
|
430
|
-
|
|
431
|
-
|
|
432
|
-
|
|
433
|
-
|
|
434
|
-
|
|
435
|
-
|
|
436
|
-
|
|
437
|
-
|
|
438
|
-
|
|
439
|
-
const
|
|
440
|
-
|
|
441
|
-
|
|
442
|
-
|
|
443
|
-
|
|
444
|
-
|
|
445
|
-
:
|
|
446
|
-
|
|
447
|
-
|
|
448
|
-
|
|
449
|
-
|
|
450
|
-
|
|
451
|
-
|
|
452
|
-
|
|
453
|
-
|
|
454
|
-
)
|
|
455
|
-
|
|
456
|
-
|
|
457
|
-
|
|
458
|
-
|
|
459
|
-
|
|
460
|
-
|
|
461
|
-
|
|
462
|
-
|
|
463
|
-
|
|
464
|
-
|
|
465
|
-
|
|
466
|
-
|
|
467
|
-
|
|
468
|
-
|
|
469
|
-
|
|
470
|
-
|
|
471
|
-
|
|
472
|
-
const
|
|
473
|
-
const
|
|
474
|
-
const
|
|
475
|
-
|
|
476
|
-
|
|
477
|
-
|
|
478
|
-
|
|
479
|
-
|
|
480
|
-
|
|
481
|
-
|
|
482
|
-
const
|
|
483
|
-
|
|
484
|
-
|
|
485
|
-
|
|
486
|
-
|
|
487
|
-
|
|
488
|
-
|
|
489
|
-
|
|
490
|
-
|
|
491
|
-
|
|
492
|
-
|
|
493
|
-
|
|
494
|
-
|
|
495
|
-
|
|
496
|
-
|
|
497
|
-
|
|
498
|
-
|
|
499
|
-
|
|
500
|
-
|
|
501
|
-
|
|
502
|
-
|
|
503
|
-
|
|
504
|
-
|
|
505
|
-
|
|
506
|
-
|
|
507
|
-
|
|
508
|
-
|
|
509
|
-
|
|
510
|
-
|
|
511
|
-
|
|
512
|
-
|
|
513
|
-
|
|
514
|
-
|
|
515
|
-
|
|
516
|
-
|
|
517
|
-
|
|
518
|
-
|
|
519
|
-
|
|
520
|
-
|
|
521
|
-
|
|
522
|
-
|
|
523
|
-
}
|
|
524
|
-
|
|
525
|
-
const
|
|
526
|
-
|
|
527
|
-
|
|
528
|
-
|
|
529
|
-
|
|
530
|
-
|
|
531
|
-
|
|
532
|
-
|
|
533
|
-
|
|
534
|
-
|
|
535
|
-
|
|
536
|
-
|
|
537
|
-
|
|
538
|
-
|
|
539
|
-
|
|
540
|
-
|
|
541
|
-
|
|
542
|
-
|
|
543
|
-
|
|
544
|
-
|
|
545
|
-
|
|
546
|
-
|
|
547
|
-
|
|
548
|
-
|
|
549
|
-
|
|
550
|
-
|
|
551
|
-
|
|
552
|
-
|
|
553
|
-
|
|
554
|
-
|
|
555
|
-
|
|
556
|
-
|
|
557
|
-
|
|
558
|
-
|
|
559
|
-
|
|
560
|
-
},
|
|
561
|
-
|
|
562
|
-
|
|
563
|
-
|
|
564
|
-
|
|
565
|
-
|
|
566
|
-
|
|
567
|
-
|
|
568
|
-
|
|
569
|
-
|
|
570
|
-
|
|
571
|
-
|
|
572
|
-
|
|
573
|
-
|
|
574
|
-
|
|
575
|
-
|
|
576
|
-
|
|
577
|
-
|
|
578
|
-
|
|
579
|
-
|
|
580
|
-
|
|
581
|
-
|
|
582
|
-
|
|
583
|
-
|
|
584
|
-
|
|
585
|
-
|
|
586
|
-
|
|
587
|
-
|
|
588
|
-
|
|
589
|
-
|
|
590
|
-
|
|
591
|
-
|
|
592
|
-
|
|
593
|
-
|
|
594
|
-
|
|
595
|
-
|
|
596
|
-
|
|
597
|
-
|
|
598
|
-
|
|
599
|
-
|
|
600
|
-
|
|
601
|
-
|
|
602
|
-
|
|
603
|
-
const
|
|
604
|
-
|
|
605
|
-
|
|
606
|
-
|
|
607
|
-
|
|
608
|
-
|
|
609
|
-
|
|
610
|
-
|
|
611
|
-
|
|
612
|
-
|
|
613
|
-
|
|
614
|
-
|
|
615
|
-
|
|
616
|
-
|
|
617
|
-
|
|
618
|
-
|
|
619
|
-
|
|
620
|
-
|
|
621
|
-
|
|
622
|
-
|
|
623
|
-
|
|
624
|
-
|
|
625
|
-
|
|
626
|
-
|
|
627
|
-
|
|
628
|
-
|
|
629
|
-
|
|
630
|
-
|
|
631
|
-
|
|
632
|
-
|
|
633
|
-
|
|
634
|
-
|
|
635
|
-
|
|
636
|
-
|
|
637
|
-
|
|
638
|
-
|
|
639
|
-
|
|
640
|
-
|
|
641
|
-
|
|
642
|
-
|
|
643
|
-
|
|
644
|
-
|
|
645
|
-
|
|
646
|
-
|
|
647
|
-
|
|
648
|
-
|
|
649
|
-
|
|
650
|
-
|
|
651
|
-
|
|
652
|
-
|
|
653
|
-
|
|
654
|
-
|
|
655
|
-
|
|
656
|
-
|
|
657
|
-
|
|
658
|
-
|
|
659
|
-
|
|
660
|
-
|
|
661
|
-
|
|
662
|
-
|
|
663
|
-
|
|
664
|
-
|
|
665
|
-
|
|
666
|
-
},
|
|
667
|
-
|
|
668
|
-
|
|
669
|
-
|
|
1
|
+
import fsp from "node:fs/promises";
|
|
2
|
+
import path from "node:path";
|
|
3
|
+
|
|
4
|
+
import {
|
|
5
|
+
createMultiProviderApiClient,
|
|
6
|
+
resolveModel,
|
|
7
|
+
resolveProvider,
|
|
8
|
+
} from "../ai/client.js";
|
|
9
|
+
import { loadConfig } from "../config/service.js";
|
|
10
|
+
import { evaluateBudget } from "../cost/budget.js";
|
|
11
|
+
import { appendCostEntry, summarizeCostHistory } from "../cost/history.js";
|
|
12
|
+
import { estimateModelCost } from "../cost/tracker.js";
|
|
13
|
+
import { appendRunEvent, deriveStopClassFromBudget } from "../telemetry/ledger.js";
|
|
14
|
+
|
|
15
|
+
const AI_SEVERITIES = new Set(["P0", "P1", "P2", "P3"]);
|
|
16
|
+
const DEFAULT_AI_MAX_FINDINGS = 20;
|
|
17
|
+
const DEFAULT_REVIEW_AI_MODEL = "gpt-5.3-codex";
|
|
18
|
+
|
|
19
|
+
function normalizeString(value) {
|
|
20
|
+
return String(value || "").trim();
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
function parseNonNegativeNumber(rawValue, field) {
|
|
24
|
+
const normalized = Number(rawValue || 0);
|
|
25
|
+
if (!Number.isFinite(normalized) || normalized < 0) {
|
|
26
|
+
throw new Error(`${field} must be a non-negative number.`);
|
|
27
|
+
}
|
|
28
|
+
return normalized;
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
function parsePercent(rawValue, field) {
|
|
32
|
+
const normalized = Number(rawValue || 0);
|
|
33
|
+
if (!Number.isFinite(normalized) || normalized < 0 || normalized > 100) {
|
|
34
|
+
throw new Error(`${field} must be between 0 and 100.`);
|
|
35
|
+
}
|
|
36
|
+
return normalized;
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
function estimateTokenCount(text) {
|
|
40
|
+
const normalized = String(text || "");
|
|
41
|
+
if (!normalized) {
|
|
42
|
+
return 0;
|
|
43
|
+
}
|
|
44
|
+
return Math.max(1, Math.ceil(normalized.length / 4));
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
function resolveConfiguredApiKey(provider, resolvedConfig = {}) {
|
|
48
|
+
const normalizedProvider = normalizeString(provider).toLowerCase();
|
|
49
|
+
if (normalizedProvider === "openai") {
|
|
50
|
+
return normalizeString(resolvedConfig.openaiApiKey);
|
|
51
|
+
}
|
|
52
|
+
if (normalizedProvider === "anthropic") {
|
|
53
|
+
return normalizeString(resolvedConfig.anthropicApiKey);
|
|
54
|
+
}
|
|
55
|
+
if (normalizedProvider === "google") {
|
|
56
|
+
return normalizeString(resolvedConfig.googleApiKey);
|
|
57
|
+
}
|
|
58
|
+
return "";
|
|
59
|
+
}
|
|
60
|
+
|
|
61
|
+
function sanitizeExcerpt(text) {
|
|
62
|
+
return String(text || "")
|
|
63
|
+
.trim()
|
|
64
|
+
.replace(/\s+/g, " ")
|
|
65
|
+
.slice(0, 180);
|
|
66
|
+
}
|
|
67
|
+
|
|
68
|
+
function extractJsonPayload(rawText) {
|
|
69
|
+
const text = String(rawText || "").trim();
|
|
70
|
+
if (!text) {
|
|
71
|
+
return null;
|
|
72
|
+
}
|
|
73
|
+
|
|
74
|
+
const candidates = [];
|
|
75
|
+
const fencedMatch = text.match(/```(?:json)?\s*([\s\S]*?)```/i);
|
|
76
|
+
if (fencedMatch && fencedMatch[1]) {
|
|
77
|
+
candidates.push(fencedMatch[1].trim());
|
|
78
|
+
}
|
|
79
|
+
candidates.push(text);
|
|
80
|
+
|
|
81
|
+
const objectStart = text.indexOf("{");
|
|
82
|
+
const objectEnd = text.lastIndexOf("}");
|
|
83
|
+
if (objectStart >= 0 && objectEnd > objectStart) {
|
|
84
|
+
candidates.push(text.slice(objectStart, objectEnd + 1));
|
|
85
|
+
}
|
|
86
|
+
|
|
87
|
+
for (const candidate of candidates) {
|
|
88
|
+
try {
|
|
89
|
+
const parsed = JSON.parse(candidate);
|
|
90
|
+
if (parsed && typeof parsed === "object" && !Array.isArray(parsed)) {
|
|
91
|
+
return parsed;
|
|
92
|
+
}
|
|
93
|
+
} catch {
|
|
94
|
+
continue;
|
|
95
|
+
}
|
|
96
|
+
}
|
|
97
|
+
|
|
98
|
+
return null;
|
|
99
|
+
}
|
|
100
|
+
|
|
101
|
+
function normalizeSeverity(value) {
|
|
102
|
+
const normalized = normalizeString(value).toUpperCase();
|
|
103
|
+
if (AI_SEVERITIES.has(normalized)) {
|
|
104
|
+
return normalized;
|
|
105
|
+
}
|
|
106
|
+
return "P2";
|
|
107
|
+
}
|
|
108
|
+
|
|
109
|
+
function normalizeLine(value) {
|
|
110
|
+
const normalized = Number(value || 1);
|
|
111
|
+
if (!Number.isFinite(normalized) || normalized < 1) {
|
|
112
|
+
return 1;
|
|
113
|
+
}
|
|
114
|
+
return Math.floor(normalized);
|
|
115
|
+
}
|
|
116
|
+
|
|
117
|
+
function normalizeConfidence(value) {
|
|
118
|
+
if (value === undefined || value === null || value === "") {
|
|
119
|
+
return null;
|
|
120
|
+
}
|
|
121
|
+
const normalized = Number(value);
|
|
122
|
+
if (!Number.isFinite(normalized)) {
|
|
123
|
+
return null;
|
|
124
|
+
}
|
|
125
|
+
return Math.max(0, Math.min(1, normalized));
|
|
126
|
+
}
|
|
127
|
+
|
|
128
|
+
function normalizeAiFinding(rawFinding, index) {
|
|
129
|
+
if (!rawFinding || typeof rawFinding !== "object" || Array.isArray(rawFinding)) {
|
|
130
|
+
return null;
|
|
131
|
+
}
|
|
132
|
+
|
|
133
|
+
const message = normalizeString(rawFinding.title || rawFinding.message);
|
|
134
|
+
const rationale = normalizeString(rawFinding.rationale || rawFinding.excerpt);
|
|
135
|
+
const suggestedFix = normalizeString(rawFinding.suggestedFix);
|
|
136
|
+
|
|
137
|
+
return {
|
|
138
|
+
severity: normalizeSeverity(rawFinding.severity),
|
|
139
|
+
file: normalizeString(rawFinding.file) || "unknown",
|
|
140
|
+
line: normalizeLine(rawFinding.line),
|
|
141
|
+
message: message || `AI finding ${index + 1}`,
|
|
142
|
+
rationale: rationale || "AI reviewer flagged a potential issue requiring validation.",
|
|
143
|
+
suggestedFix: suggestedFix || "Review and remediate this finding.",
|
|
144
|
+
confidence: normalizeConfidence(rawFinding.confidence),
|
|
145
|
+
};
|
|
146
|
+
}
|
|
147
|
+
|
|
148
|
+
function summarizeFindings(findings = []) {
|
|
149
|
+
const summary = {
|
|
150
|
+
P0: 0,
|
|
151
|
+
P1: 0,
|
|
152
|
+
P2: 0,
|
|
153
|
+
P3: 0,
|
|
154
|
+
};
|
|
155
|
+
for (const finding of findings) {
|
|
156
|
+
const severity = normalizeSeverity(finding.severity);
|
|
157
|
+
summary[severity] += 1;
|
|
158
|
+
}
|
|
159
|
+
return {
|
|
160
|
+
...summary,
|
|
161
|
+
blocking: summary.P0 > 0 || summary.P1 > 0,
|
|
162
|
+
};
|
|
163
|
+
}
|
|
164
|
+
|
|
165
|
+
export function parseAiReviewResponse({ text, maxFindings = DEFAULT_AI_MAX_FINDINGS } = {}) {
|
|
166
|
+
const parsed = extractJsonPayload(text);
|
|
167
|
+
const normalizedMaxFindings = Math.max(1, Math.floor(Number(maxFindings || DEFAULT_AI_MAX_FINDINGS)));
|
|
168
|
+
|
|
169
|
+
if (!parsed) {
|
|
170
|
+
return {
|
|
171
|
+
parser: "fallback_text",
|
|
172
|
+
summary:
|
|
173
|
+
sanitizeExcerpt(text) || "AI response could not be parsed as JSON; no structured findings extracted.",
|
|
174
|
+
findings: [],
|
|
175
|
+
};
|
|
176
|
+
}
|
|
177
|
+
|
|
178
|
+
const summary = normalizeString(
|
|
179
|
+
parsed.summary?.highLevel || parsed.summary?.risk || parsed.summary?.text || parsed.summary
|
|
180
|
+
);
|
|
181
|
+
const rawFindings = Array.isArray(parsed.findings) ? parsed.findings : [];
|
|
182
|
+
const findings = [];
|
|
183
|
+
for (let index = 0; index < rawFindings.length; index += 1) {
|
|
184
|
+
if (findings.length >= normalizedMaxFindings) {
|
|
185
|
+
break;
|
|
186
|
+
}
|
|
187
|
+
const normalized = normalizeAiFinding(rawFindings[index], index);
|
|
188
|
+
if (normalized) {
|
|
189
|
+
findings.push(normalized);
|
|
190
|
+
}
|
|
191
|
+
}
|
|
192
|
+
|
|
193
|
+
return {
|
|
194
|
+
parser: "json",
|
|
195
|
+
summary: summary || "Structured AI response parsed successfully.",
|
|
196
|
+
findings,
|
|
197
|
+
};
|
|
198
|
+
}
|
|
199
|
+
|
|
200
|
+
function formatDeterministicFindingLine(finding) {
|
|
201
|
+
return `- [${finding.severity}] ${finding.file}:${finding.line} ${finding.message}`;
|
|
202
|
+
}
|
|
203
|
+
|
|
204
|
+
function buildScopedFileSummary(scopedFiles = [], maxItems = 200) {
|
|
205
|
+
const normalized = Array.isArray(scopedFiles) ? scopedFiles : [];
|
|
206
|
+
const visible = normalized.slice(0, maxItems);
|
|
207
|
+
const omitted = Math.max(0, normalized.length - visible.length);
|
|
208
|
+
const lines = visible.map((item) => `- ${item}`);
|
|
209
|
+
if (omitted > 0) {
|
|
210
|
+
lines.push(`- ... ${omitted} more files omitted`);
|
|
211
|
+
}
|
|
212
|
+
return lines.join("\n") || "- none";
|
|
213
|
+
}
|
|
214
|
+
|
|
215
|
+
export function buildAiReviewPrompt({
|
|
216
|
+
targetPath,
|
|
217
|
+
mode,
|
|
218
|
+
deterministicSummary,
|
|
219
|
+
deterministicFindings = [],
|
|
220
|
+
scopedFiles = [],
|
|
221
|
+
specContext = null,
|
|
222
|
+
maxFindings = DEFAULT_AI_MAX_FINDINGS,
|
|
223
|
+
} = {}) {
|
|
224
|
+
const normalizedSummary = deterministicSummary || { P0: 0, P1: 0, P2: 0, P3: 0 };
|
|
225
|
+
const findingLines = deterministicFindings
|
|
226
|
+
.slice(0, 120)
|
|
227
|
+
.map((finding) => formatDeterministicFindingLine(finding))
|
|
228
|
+
.join("\n");
|
|
229
|
+
const normalizedMaxFindings = Math.max(1, Math.floor(Number(maxFindings || DEFAULT_AI_MAX_FINDINGS)));
|
|
230
|
+
const specPath = normalizeString(specContext?.specPath) || "none";
|
|
231
|
+
const specHash = normalizeString(specContext?.specHashSha256) || "unknown";
|
|
232
|
+
const specEndpointCount = Number(specContext?.endpointCount || 0);
|
|
233
|
+
const specAcceptanceCriteriaCount = Number(specContext?.acceptanceCriteriaCount || 0);
|
|
234
|
+
const specPreview = Array.isArray(specContext?.endpointsPreview) ? specContext.endpointsPreview : [];
|
|
235
|
+
|
|
236
|
+
return [
|
|
237
|
+
"You are Sentinelayer Omar reviewer layer 9.3.",
|
|
238
|
+
"Review the deterministic findings and scoped files. Add ONLY materially new findings.",
|
|
239
|
+
"Do not repeat deterministic findings unless you add new exploitability rationale.",
|
|
240
|
+
"Output STRICT JSON only. Do not wrap in markdown.",
|
|
241
|
+
"",
|
|
242
|
+
"JSON schema:",
|
|
243
|
+
"{",
|
|
244
|
+
' "summary": {"risk": "low|medium|high|critical", "highLevel": "short summary"},',
|
|
245
|
+
' "findings": [',
|
|
246
|
+
" {",
|
|
247
|
+
' "severity": "P0|P1|P2|P3",',
|
|
248
|
+
' "file": "relative/path",',
|
|
249
|
+
' "line": 1,',
|
|
250
|
+
' "title": "finding title",',
|
|
251
|
+
' "rationale": "why this matters",',
|
|
252
|
+
' "suggestedFix": "specific remediation",',
|
|
253
|
+
' "confidence": 0.0',
|
|
254
|
+
" }",
|
|
255
|
+
" ]",
|
|
256
|
+
"}",
|
|
257
|
+
"",
|
|
258
|
+
`Maximum findings: ${normalizedMaxFindings}`,
|
|
259
|
+
"",
|
|
260
|
+
`Target path: ${targetPath}`,
|
|
261
|
+
`Review mode: ${mode}`,
|
|
262
|
+
`Deterministic summary: P0=${normalizedSummary.P0} P1=${normalizedSummary.P1} P2=${normalizedSummary.P2} P3=${normalizedSummary.P3}`,
|
|
263
|
+
`Spec path: ${specPath}`,
|
|
264
|
+
`Spec sha256: ${specHash}`,
|
|
265
|
+
`Spec endpoints declared: ${specEndpointCount}`,
|
|
266
|
+
`Spec acceptance criteria count: ${specAcceptanceCriteriaCount}`,
|
|
267
|
+
`Spec endpoint preview: ${specPreview.length > 0 ? specPreview.join(", ") : "none"}`,
|
|
268
|
+
"",
|
|
269
|
+
"Scoped files:",
|
|
270
|
+
buildScopedFileSummary(scopedFiles),
|
|
271
|
+
"",
|
|
272
|
+
"Deterministic findings:",
|
|
273
|
+
findingLines || "- none",
|
|
274
|
+
].join("\n");
|
|
275
|
+
}
|
|
276
|
+
|
|
277
|
+
function maybeEstimateModelCost({ modelId, inputTokens, outputTokens }) {
|
|
278
|
+
try {
|
|
279
|
+
return {
|
|
280
|
+
costUsd: estimateModelCost({
|
|
281
|
+
modelId,
|
|
282
|
+
inputTokens,
|
|
283
|
+
outputTokens,
|
|
284
|
+
}),
|
|
285
|
+
pricingFound: true,
|
|
286
|
+
};
|
|
287
|
+
} catch {
|
|
288
|
+
return {
|
|
289
|
+
costUsd: 0,
|
|
290
|
+
pricingFound: false,
|
|
291
|
+
};
|
|
292
|
+
}
|
|
293
|
+
}
|
|
294
|
+
|
|
295
|
+
function composeAiReviewMarkdown({
|
|
296
|
+
generatedAt,
|
|
297
|
+
runId,
|
|
298
|
+
mode,
|
|
299
|
+
parser,
|
|
300
|
+
summary,
|
|
301
|
+
provider,
|
|
302
|
+
model,
|
|
303
|
+
dryRun,
|
|
304
|
+
findings = [],
|
|
305
|
+
usage,
|
|
306
|
+
combinedSummary,
|
|
307
|
+
} = {}) {
|
|
308
|
+
const findingLines =
|
|
309
|
+
findings.length > 0
|
|
310
|
+
? findings
|
|
311
|
+
.map(
|
|
312
|
+
(finding, index) =>
|
|
313
|
+
`${index + 1}. [${finding.severity}] ${finding.file}:${finding.line} ${finding.message}\n` +
|
|
314
|
+
` rationale: ${finding.rationale}\n` +
|
|
315
|
+
` suggested_fix: ${finding.suggestedFix}` +
|
|
316
|
+
(finding.confidence === null ? "" : `\n confidence: ${finding.confidence.toFixed(2)}`)
|
|
317
|
+
)
|
|
318
|
+
.join("\n")
|
|
319
|
+
: "- none";
|
|
320
|
+
|
|
321
|
+
return [
|
|
322
|
+
"# REVIEW_AI",
|
|
323
|
+
"",
|
|
324
|
+
`Generated: ${generatedAt}`,
|
|
325
|
+
`Run ID: ${runId}`,
|
|
326
|
+
`Mode: ${mode}`,
|
|
327
|
+
`Provider: ${provider}`,
|
|
328
|
+
`Model: ${model}`,
|
|
329
|
+
`Dry run: ${dryRun ? "yes" : "no"}`,
|
|
330
|
+
`Parser: ${parser}`,
|
|
331
|
+
"",
|
|
332
|
+
"Summary:",
|
|
333
|
+
`- ${summary || "No summary provided."}`,
|
|
334
|
+
`- Combined findings: P0=${combinedSummary.P0} P1=${combinedSummary.P1} P2=${combinedSummary.P2} P3=${combinedSummary.P3}`,
|
|
335
|
+
`- Blocking: ${combinedSummary.blocking ? "yes" : "no"}`,
|
|
336
|
+
`- Usage: input_tokens=${usage.inputTokens} output_tokens=${usage.outputTokens} cost_usd=${usage.costUsd.toFixed(6)} duration_ms=${usage.durationMs}`,
|
|
337
|
+
"",
|
|
338
|
+
"AI Findings:",
|
|
339
|
+
findingLines,
|
|
340
|
+
"",
|
|
341
|
+
].join("\n");
|
|
342
|
+
}
|
|
343
|
+
|
|
344
|
+
function toReviewFinding(aiFinding, index) {
|
|
345
|
+
return {
|
|
346
|
+
severity: aiFinding.severity,
|
|
347
|
+
file: aiFinding.file,
|
|
348
|
+
line: aiFinding.line,
|
|
349
|
+
message: aiFinding.message,
|
|
350
|
+
excerpt: sanitizeExcerpt(aiFinding.rationale),
|
|
351
|
+
ruleId: `SL-AI-${String(index + 1).padStart(3, "0")}`,
|
|
352
|
+
suggestedFix: aiFinding.suggestedFix,
|
|
353
|
+
layer: "ai_reasoning",
|
|
354
|
+
confidence: aiFinding.confidence,
|
|
355
|
+
};
|
|
356
|
+
}
|
|
357
|
+
|
|
358
|
+
function buildDryRunResponse({ deterministicSummary, maxFindings } = {}) {
|
|
359
|
+
const findingCount = Math.max(1, Math.min(2, Math.floor(Number(maxFindings || 1))));
|
|
360
|
+
const findings = [];
|
|
361
|
+
for (let index = 0; index < findingCount; index += 1) {
|
|
362
|
+
findings.push({
|
|
363
|
+
severity: index === 0 ? "P2" : "P3",
|
|
364
|
+
file: "src/example.js",
|
|
365
|
+
line: 1 + index,
|
|
366
|
+
title: `DRY_RUN finding ${index + 1}`,
|
|
367
|
+
rationale: `Synthetic AI rationale with deterministic context P1=${deterministicSummary.P1}.`,
|
|
368
|
+
suggestedFix: "Validate this path with targeted remediation.",
|
|
369
|
+
confidence: index === 0 ? 0.72 : 0.54,
|
|
370
|
+
});
|
|
371
|
+
}
|
|
372
|
+
return JSON.stringify(
|
|
373
|
+
{
|
|
374
|
+
summary: {
|
|
375
|
+
risk: "medium",
|
|
376
|
+
highLevel: "DRY_RUN_RESPONSE: synthetic AI review output.",
|
|
377
|
+
},
|
|
378
|
+
findings,
|
|
379
|
+
},
|
|
380
|
+
null,
|
|
381
|
+
2
|
|
382
|
+
);
|
|
383
|
+
}
|
|
384
|
+
|
|
385
|
+
export async function runAiReviewLayer({
|
|
386
|
+
targetPath,
|
|
387
|
+
mode,
|
|
388
|
+
runId,
|
|
389
|
+
runDirectory,
|
|
390
|
+
deterministic,
|
|
391
|
+
outputDir = "",
|
|
392
|
+
provider,
|
|
393
|
+
model,
|
|
394
|
+
apiKey,
|
|
395
|
+
sessionId,
|
|
396
|
+
maxFindings = DEFAULT_AI_MAX_FINDINGS,
|
|
397
|
+
maxCostUsd = 1.0,
|
|
398
|
+
maxOutputTokens = 0,
|
|
399
|
+
maxRuntimeMs = 0,
|
|
400
|
+
maxToolCalls = 0,
|
|
401
|
+
maxNoProgress = 3,
|
|
402
|
+
warningThresholdPercent = 80,
|
|
403
|
+
dryRun = false,
|
|
404
|
+
env = process.env,
|
|
405
|
+
} = {}) {
|
|
406
|
+
const normalizedTargetPath = path.resolve(String(targetPath || "."));
|
|
407
|
+
const normalizedRunDirectory = path.resolve(String(runDirectory || "."));
|
|
408
|
+
const normalizedMode = normalizeString(mode) || "full";
|
|
409
|
+
const normalizedMaxFindings = Math.max(
|
|
410
|
+
1,
|
|
411
|
+
Math.floor(Number(maxFindings || DEFAULT_AI_MAX_FINDINGS))
|
|
412
|
+
);
|
|
413
|
+
const normalizedRunId = normalizeString(runId) || "review-ai";
|
|
414
|
+
|
|
415
|
+
const config = await loadConfig({ cwd: normalizedTargetPath, env });
|
|
416
|
+
let resolvedProvider = resolveProvider({
|
|
417
|
+
provider,
|
|
418
|
+
configProvider: config.resolved.defaultModelProvider,
|
|
419
|
+
env,
|
|
420
|
+
});
|
|
421
|
+
// If no explicit provider and default fell through to openai,
|
|
422
|
+
// check for stored sentinelayer session (async fallback)
|
|
423
|
+
if (resolvedProvider === "openai" && !provider && !config.resolved.defaultModelProvider) {
|
|
424
|
+
try {
|
|
425
|
+
const { resolveProviderAsync } = await import("../ai/client.js");
|
|
426
|
+
resolvedProvider = await resolveProviderAsync({ env });
|
|
427
|
+
} catch {
|
|
428
|
+
// keep sync result
|
|
429
|
+
}
|
|
430
|
+
}
|
|
431
|
+
const resolvedModel = resolveModel({
|
|
432
|
+
provider: resolvedProvider,
|
|
433
|
+
model,
|
|
434
|
+
configModel: config.resolved.defaultModelId || DEFAULT_REVIEW_AI_MODEL,
|
|
435
|
+
});
|
|
436
|
+
const explicitApiKey = normalizeString(apiKey);
|
|
437
|
+
const configuredApiKey = resolveConfiguredApiKey(resolvedProvider, config.resolved);
|
|
438
|
+
|
|
439
|
+
const prompt = buildAiReviewPrompt({
|
|
440
|
+
targetPath: normalizedTargetPath,
|
|
441
|
+
mode: normalizedMode,
|
|
442
|
+
deterministicSummary: deterministic?.summary,
|
|
443
|
+
deterministicFindings: deterministic?.findings || [],
|
|
444
|
+
scopedFiles: deterministic?.scope?.scannedRelativeFiles || [],
|
|
445
|
+
specContext: deterministic?.layers?.specBinding || null,
|
|
446
|
+
maxFindings: normalizedMaxFindings,
|
|
447
|
+
});
|
|
448
|
+
|
|
449
|
+
const startedAt = Date.now();
|
|
450
|
+
const responseText = dryRun
|
|
451
|
+
? buildDryRunResponse({
|
|
452
|
+
deterministicSummary: deterministic?.summary || {},
|
|
453
|
+
maxFindings: normalizedMaxFindings,
|
|
454
|
+
})
|
|
455
|
+
: (
|
|
456
|
+
await createMultiProviderApiClient().invoke({
|
|
457
|
+
provider: resolvedProvider,
|
|
458
|
+
model: resolvedModel,
|
|
459
|
+
prompt,
|
|
460
|
+
apiKey: explicitApiKey || configuredApiKey,
|
|
461
|
+
env,
|
|
462
|
+
stream: false,
|
|
463
|
+
})
|
|
464
|
+
).text;
|
|
465
|
+
const durationMs = Math.max(0, Date.now() - startedAt);
|
|
466
|
+
|
|
467
|
+
const parsed = parseAiReviewResponse({
|
|
468
|
+
text: responseText,
|
|
469
|
+
maxFindings: normalizedMaxFindings,
|
|
470
|
+
});
|
|
471
|
+
const aiFindings = parsed.findings.map((finding, index) => toReviewFinding(finding, index));
|
|
472
|
+
const aiSummary = summarizeFindings(aiFindings);
|
|
473
|
+
const deterministicSummary = deterministic?.summary || { P0: 0, P1: 0, P2: 0, P3: 0 };
|
|
474
|
+
const combinedSummary = {
|
|
475
|
+
P0: deterministicSummary.P0 + aiSummary.P0,
|
|
476
|
+
P1: deterministicSummary.P1 + aiSummary.P1,
|
|
477
|
+
P2: deterministicSummary.P2 + aiSummary.P2,
|
|
478
|
+
P3: deterministicSummary.P3 + aiSummary.P3,
|
|
479
|
+
};
|
|
480
|
+
combinedSummary.blocking = combinedSummary.P0 > 0 || combinedSummary.P1 > 0;
|
|
481
|
+
|
|
482
|
+
const inputTokens = estimateTokenCount(prompt);
|
|
483
|
+
const outputTokens = estimateTokenCount(responseText);
|
|
484
|
+
const modelCost = maybeEstimateModelCost({
|
|
485
|
+
modelId: resolvedModel,
|
|
486
|
+
inputTokens,
|
|
487
|
+
outputTokens,
|
|
488
|
+
});
|
|
489
|
+
const normalizedSessionId =
|
|
490
|
+
normalizeString(sessionId) || `${normalizedRunId}-ai`;
|
|
491
|
+
|
|
492
|
+
const appendedCost = await appendCostEntry(
|
|
493
|
+
{
|
|
494
|
+
targetPath: normalizedTargetPath,
|
|
495
|
+
outputDirOverride: outputDir,
|
|
496
|
+
},
|
|
497
|
+
{
|
|
498
|
+
sessionId: normalizedSessionId,
|
|
499
|
+
provider: resolvedProvider,
|
|
500
|
+
model: resolvedModel,
|
|
501
|
+
inputTokens,
|
|
502
|
+
outputTokens,
|
|
503
|
+
cacheReadTokens: 0,
|
|
504
|
+
cacheWriteTokens: 0,
|
|
505
|
+
durationMs,
|
|
506
|
+
toolCalls: 1,
|
|
507
|
+
costUsd: modelCost.costUsd,
|
|
508
|
+
progressScore: aiFindings.length > 0 ? 1 : 0,
|
|
509
|
+
}
|
|
510
|
+
);
|
|
511
|
+
const costSummary = summarizeCostHistory(appendedCost.history);
|
|
512
|
+
const sessionSummary = costSummary.sessions.find((entry) => entry.sessionId === normalizedSessionId) || {
|
|
513
|
+
sessionId: normalizedSessionId,
|
|
514
|
+
invocationCount: 0,
|
|
515
|
+
inputTokens: 0,
|
|
516
|
+
outputTokens: 0,
|
|
517
|
+
cacheReadTokens: 0,
|
|
518
|
+
cacheWriteTokens: 0,
|
|
519
|
+
durationMs: 0,
|
|
520
|
+
toolCalls: 0,
|
|
521
|
+
costUsd: 0,
|
|
522
|
+
noProgressStreak: 0,
|
|
523
|
+
};
|
|
524
|
+
|
|
525
|
+
const budget = evaluateBudget({
|
|
526
|
+
sessionSummary,
|
|
527
|
+
maxCostUsd: parseNonNegativeNumber(maxCostUsd, "maxCostUsd"),
|
|
528
|
+
maxOutputTokens: parseNonNegativeNumber(maxOutputTokens, "maxOutputTokens"),
|
|
529
|
+
maxNoProgress: parseNonNegativeNumber(maxNoProgress, "maxNoProgress"),
|
|
530
|
+
maxRuntimeMs: parseNonNegativeNumber(maxRuntimeMs, "maxRuntimeMs"),
|
|
531
|
+
maxToolCalls: parseNonNegativeNumber(maxToolCalls, "maxToolCalls"),
|
|
532
|
+
warningThresholdPercent: parsePercent(warningThresholdPercent, "warningThresholdPercent"),
|
|
533
|
+
});
|
|
534
|
+
|
|
535
|
+
const usageTelemetry = await appendRunEvent(
|
|
536
|
+
{
|
|
537
|
+
targetPath: normalizedTargetPath,
|
|
538
|
+
outputDirOverride: outputDir,
|
|
539
|
+
},
|
|
540
|
+
{
|
|
541
|
+
sessionId: normalizedSessionId,
|
|
542
|
+
runId: normalizedRunId,
|
|
543
|
+
eventType: "usage",
|
|
544
|
+
usage: {
|
|
545
|
+
inputTokens,
|
|
546
|
+
outputTokens,
|
|
547
|
+
cacheReadTokens: 0,
|
|
548
|
+
cacheWriteTokens: 0,
|
|
549
|
+
costUsd: modelCost.costUsd,
|
|
550
|
+
durationMs,
|
|
551
|
+
toolCalls: 1,
|
|
552
|
+
},
|
|
553
|
+
metadata: {
|
|
554
|
+
sourceCommand: "review",
|
|
555
|
+
layer: "ai_reasoning",
|
|
556
|
+
provider: resolvedProvider,
|
|
557
|
+
model: resolvedModel,
|
|
558
|
+
invocationId: appendedCost.entry.invocationId,
|
|
559
|
+
dryRun: Boolean(dryRun),
|
|
560
|
+
},
|
|
561
|
+
}
|
|
562
|
+
);
|
|
563
|
+
|
|
564
|
+
let stopTelemetry = null;
|
|
565
|
+
if (budget.blocking) {
|
|
566
|
+
stopTelemetry = await appendRunEvent(
|
|
567
|
+
{
|
|
568
|
+
targetPath: normalizedTargetPath,
|
|
569
|
+
outputDirOverride: outputDir,
|
|
570
|
+
},
|
|
571
|
+
{
|
|
572
|
+
sessionId: normalizedSessionId,
|
|
573
|
+
runId: normalizedRunId,
|
|
574
|
+
eventType: "run_stop",
|
|
575
|
+
usage: {
|
|
576
|
+
inputTokens: sessionSummary.inputTokens,
|
|
577
|
+
outputTokens: sessionSummary.outputTokens,
|
|
578
|
+
cacheReadTokens: sessionSummary.cacheReadTokens,
|
|
579
|
+
cacheWriteTokens: sessionSummary.cacheWriteTokens,
|
|
580
|
+
costUsd: sessionSummary.costUsd,
|
|
581
|
+
durationMs: sessionSummary.durationMs,
|
|
582
|
+
toolCalls: sessionSummary.toolCalls,
|
|
583
|
+
},
|
|
584
|
+
stop: {
|
|
585
|
+
stopClass: deriveStopClassFromBudget(budget),
|
|
586
|
+
blocking: true,
|
|
587
|
+
reasonCodes: budget.reasons.map((reason) => reason.code),
|
|
588
|
+
},
|
|
589
|
+
metadata: {
|
|
590
|
+
sourceCommand: "review",
|
|
591
|
+
layer: "ai_reasoning",
|
|
592
|
+
provider: resolvedProvider,
|
|
593
|
+
model: resolvedModel,
|
|
594
|
+
invocationId: appendedCost.entry.invocationId,
|
|
595
|
+
dryRun: Boolean(dryRun),
|
|
596
|
+
},
|
|
597
|
+
}
|
|
598
|
+
);
|
|
599
|
+
}
|
|
600
|
+
|
|
601
|
+
await fsp.mkdir(normalizedRunDirectory, { recursive: true });
|
|
602
|
+
const promptPath = path.join(normalizedRunDirectory, "REVIEW_AI_PROMPT.txt");
|
|
603
|
+
const reportMarkdownPath = path.join(normalizedRunDirectory, "REVIEW_AI.md");
|
|
604
|
+
const reportJsonPath = path.join(normalizedRunDirectory, "REVIEW_AI.json");
|
|
605
|
+
const generatedAt = new Date().toISOString();
|
|
606
|
+
const usage = {
|
|
607
|
+
inputTokens,
|
|
608
|
+
outputTokens,
|
|
609
|
+
costUsd: modelCost.costUsd,
|
|
610
|
+
durationMs,
|
|
611
|
+
toolCalls: 1,
|
|
612
|
+
};
|
|
613
|
+
const reportPayload = {
|
|
614
|
+
schemaVersion: "1.0.0",
|
|
615
|
+
generatedAt,
|
|
616
|
+
runId: normalizedRunId,
|
|
617
|
+
mode: normalizedMode,
|
|
618
|
+
parser: parsed.parser,
|
|
619
|
+
summary: parsed.summary,
|
|
620
|
+
provider: resolvedProvider,
|
|
621
|
+
model: resolvedModel,
|
|
622
|
+
dryRun: Boolean(dryRun),
|
|
623
|
+
usage,
|
|
624
|
+
pricingFound: modelCost.pricingFound,
|
|
625
|
+
budget,
|
|
626
|
+
deterministicSummary,
|
|
627
|
+
aiSummary,
|
|
628
|
+
combinedSummary,
|
|
629
|
+
findings: aiFindings,
|
|
630
|
+
};
|
|
631
|
+
|
|
632
|
+
const reportMarkdown = composeAiReviewMarkdown({
|
|
633
|
+
generatedAt,
|
|
634
|
+
runId: normalizedRunId,
|
|
635
|
+
mode: normalizedMode,
|
|
636
|
+
parser: parsed.parser,
|
|
637
|
+
summary: parsed.summary,
|
|
638
|
+
provider: resolvedProvider,
|
|
639
|
+
model: resolvedModel,
|
|
640
|
+
dryRun: Boolean(dryRun),
|
|
641
|
+
findings: aiFindings,
|
|
642
|
+
usage,
|
|
643
|
+
combinedSummary,
|
|
644
|
+
});
|
|
645
|
+
|
|
646
|
+
await fsp.writeFile(promptPath, `${prompt}\n`, "utf-8");
|
|
647
|
+
await fsp.writeFile(reportMarkdownPath, `${reportMarkdown.trim()}\n`, "utf-8");
|
|
648
|
+
await fsp.writeFile(reportJsonPath, `${JSON.stringify(reportPayload, null, 2)}\n`, "utf-8");
|
|
649
|
+
|
|
650
|
+
return {
|
|
651
|
+
parser: parsed.parser,
|
|
652
|
+
summary: parsed.summary,
|
|
653
|
+
findings: aiFindings,
|
|
654
|
+
aiSummary,
|
|
655
|
+
combinedSummary,
|
|
656
|
+
provider: resolvedProvider,
|
|
657
|
+
model: resolvedModel,
|
|
658
|
+
dryRun: Boolean(dryRun),
|
|
659
|
+
usage,
|
|
660
|
+
pricingFound: modelCost.pricingFound,
|
|
661
|
+
budget,
|
|
662
|
+
artifacts: {
|
|
663
|
+
promptPath,
|
|
664
|
+
reportMarkdownPath,
|
|
665
|
+
reportJsonPath,
|
|
666
|
+
},
|
|
667
|
+
cost: {
|
|
668
|
+
filePath: appendedCost.filePath,
|
|
669
|
+
invocationId: appendedCost.entry.invocationId,
|
|
670
|
+
sessionId: normalizedSessionId,
|
|
671
|
+
},
|
|
672
|
+
telemetry: {
|
|
673
|
+
filePath: usageTelemetry.filePath,
|
|
674
|
+
usageEventId: usageTelemetry.event.eventId,
|
|
675
|
+
stopEventId: stopTelemetry?.event?.eventId || null,
|
|
676
|
+
},
|
|
677
|
+
};
|
|
678
|
+
}
|
|
679
|
+
|