sentinelayer-cli 0.6.2 → 0.8.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 (159) hide show
  1. package/README.md +996 -996
  2. package/bin/create-sentinelayer.js +5 -5
  3. package/bin/sentinelayer-cli.js +4 -4
  4. package/bin/sl.js +5 -5
  5. package/package.json +64 -63
  6. package/src/agents/jules/config/definition.js +160 -160
  7. package/src/agents/jules/config/system-prompt.js +182 -182
  8. package/src/agents/jules/error-intake.js +51 -51
  9. package/src/agents/jules/fix-cycle.js +17 -17
  10. package/src/agents/jules/loop.js +457 -450
  11. package/src/agents/jules/pulse.js +10 -10
  12. package/src/agents/jules/stream.js +187 -186
  13. package/src/agents/jules/swarm/file-scanner.js +74 -74
  14. package/src/agents/jules/swarm/index.js +11 -11
  15. package/src/agents/jules/swarm/orchestrator.js +362 -362
  16. package/src/agents/jules/swarm/pattern-hunter.js +123 -123
  17. package/src/agents/jules/swarm/sub-agent.js +311 -309
  18. package/src/agents/jules/tools/aidenid-email.js +189 -189
  19. package/src/agents/jules/tools/auth-audit.js +1699 -1691
  20. package/src/agents/jules/tools/dispatch.js +340 -335
  21. package/src/agents/jules/tools/file-edit.js +2 -2
  22. package/src/agents/jules/tools/file-read.js +2 -2
  23. package/src/agents/jules/tools/frontend-analyze.js +570 -570
  24. package/src/agents/jules/tools/glob.js +2 -2
  25. package/src/agents/jules/tools/grep.js +2 -2
  26. package/src/agents/jules/tools/index.js +29 -29
  27. package/src/agents/jules/tools/path-guards.js +2 -2
  28. package/src/agents/jules/tools/runtime-audit.js +507 -507
  29. package/src/agents/jules/tools/shell.js +2 -2
  30. package/src/agents/jules/tools/url-policy.js +100 -100
  31. package/src/agents/persona-visuals.js +64 -61
  32. package/src/agents/shared-tools/dispatch-core.js +320 -315
  33. package/src/agents/shared-tools/file-edit.js +180 -180
  34. package/src/agents/shared-tools/file-read.js +100 -100
  35. package/src/agents/shared-tools/glob.js +168 -168
  36. package/src/agents/shared-tools/grep.js +228 -228
  37. package/src/agents/shared-tools/index.js +46 -46
  38. package/src/agents/shared-tools/path-guards.js +161 -161
  39. package/src/agents/shared-tools/shell.js +383 -383
  40. package/src/ai/aidenid.js +1021 -1009
  41. package/src/ai/client.js +553 -553
  42. package/src/ai/domain-target-store.js +268 -268
  43. package/src/ai/identity-store.js +270 -270
  44. package/src/ai/proxy.js +137 -137
  45. package/src/ai/site-store.js +145 -145
  46. package/src/audit/agents/architecture.js +180 -180
  47. package/src/audit/agents/compliance.js +179 -179
  48. package/src/audit/agents/documentation.js +165 -165
  49. package/src/audit/agents/performance.js +145 -145
  50. package/src/audit/agents/security.js +215 -215
  51. package/src/audit/agents/testing.js +172 -172
  52. package/src/audit/orchestrator.js +557 -557
  53. package/src/audit/package.js +204 -204
  54. package/src/audit/registry.js +284 -284
  55. package/src/audit/replay.js +103 -103
  56. package/src/auth/gate.js +400 -371
  57. package/src/auth/http.js +681 -611
  58. package/src/auth/service.js +1106 -1106
  59. package/src/auth/session-store.js +813 -813
  60. package/src/cli.js +257 -252
  61. package/src/commands/ai/identity-lifecycle.js +1338 -1338
  62. package/src/commands/ai/provision-governance.js +1272 -1272
  63. package/src/commands/ai/shared.js +147 -147
  64. package/src/commands/ai.js +11 -11
  65. package/src/commands/apply.js +12 -12
  66. package/src/commands/audit.js +1171 -1166
  67. package/src/commands/auth.js +419 -419
  68. package/src/commands/chat.js +191 -191
  69. package/src/commands/config.js +184 -184
  70. package/src/commands/cost.js +311 -311
  71. package/src/commands/daemon/core.js +850 -850
  72. package/src/commands/daemon/extended.js +1048 -1048
  73. package/src/commands/daemon/shared.js +213 -213
  74. package/src/commands/daemon.js +11 -11
  75. package/src/commands/guide.js +174 -174
  76. package/src/commands/ingest.js +58 -58
  77. package/src/commands/init.js +55 -55
  78. package/src/commands/legacy-args.js +10 -10
  79. package/src/commands/mcp.js +461 -461
  80. package/src/commands/omargate.js +29 -29
  81. package/src/commands/persona.js +20 -20
  82. package/src/commands/plugin.js +260 -260
  83. package/src/commands/policy.js +132 -132
  84. package/src/commands/prompt.js +238 -238
  85. package/src/commands/review.js +704 -704
  86. package/src/commands/scan.js +872 -872
  87. package/src/commands/session.js +590 -0
  88. package/src/commands/spec.js +778 -716
  89. package/src/commands/swarm.js +651 -651
  90. package/src/commands/telemetry.js +202 -202
  91. package/src/commands/watch.js +511 -511
  92. package/src/config/agent-dictionary.js +182 -182
  93. package/src/config/io.js +56 -56
  94. package/src/config/paths.js +18 -18
  95. package/src/config/schema.js +55 -55
  96. package/src/config/service.js +184 -184
  97. package/src/cost/budget.js +235 -235
  98. package/src/cost/history.js +188 -188
  99. package/src/cost/tracker.js +171 -171
  100. package/src/daemon/artifact-lineage.js +534 -534
  101. package/src/daemon/assignment-ledger.js +966 -770
  102. package/src/daemon/ast-parser-layer.js +258 -258
  103. package/src/daemon/budget-governor.js +633 -633
  104. package/src/daemon/callgraph-overlay.js +646 -646
  105. package/src/daemon/error-worker.js +1209 -626
  106. package/src/daemon/fix-cycle.js +384 -377
  107. package/src/daemon/hybrid-mapper.js +929 -929
  108. package/src/daemon/ingest-refresh.js +10 -9
  109. package/src/daemon/jira-lifecycle.js +767 -632
  110. package/src/daemon/operator-control.js +657 -657
  111. package/src/daemon/pulse.js +327 -327
  112. package/src/daemon/reliability-lane.js +471 -471
  113. package/src/daemon/scope-engine.js +1068 -0
  114. package/src/daemon/watchdog.js +971 -971
  115. package/src/events/schema.js +190 -0
  116. package/src/guide/generator.js +316 -316
  117. package/src/ingest/engine.js +918 -918
  118. package/src/interactive/index.js +97 -97
  119. package/src/legacy-cli.js +3161 -2994
  120. package/src/mcp/registry.js +695 -695
  121. package/src/memory/blackboard.js +301 -301
  122. package/src/memory/retrieval.js +581 -581
  123. package/src/plugin/manifest.js +553 -553
  124. package/src/policy/packs.js +144 -144
  125. package/src/prompt/generator.js +136 -118
  126. package/src/review/ai-review.js +679 -679
  127. package/src/review/local-review.js +1351 -1305
  128. package/src/review/omargate-interactive.js +68 -68
  129. package/src/review/omargate-orchestrator.js +404 -300
  130. package/src/review/persona-prompts.js +296 -296
  131. package/src/review/replay.js +235 -235
  132. package/src/review/report.js +664 -664
  133. package/src/review/scan-modes.js +48 -42
  134. package/src/review/spec-binding.js +487 -487
  135. package/src/scaffold/generator.js +67 -67
  136. package/src/scaffold/templates.js +150 -150
  137. package/src/scan/generator.js +418 -418
  138. package/src/scan/gh-secrets.js +107 -107
  139. package/src/session/agent-registry.js +352 -0
  140. package/src/session/daemon.js +801 -0
  141. package/src/session/paths.js +33 -0
  142. package/src/session/runtime-bridge.js +739 -0
  143. package/src/session/store.js +388 -0
  144. package/src/session/stream.js +325 -0
  145. package/src/spec/generator.js +619 -519
  146. package/src/spec/regenerate.js +237 -237
  147. package/src/spec/templates.js +91 -91
  148. package/src/swarm/dashboard.js +247 -247
  149. package/src/swarm/factory.js +363 -363
  150. package/src/swarm/pentest.js +934 -934
  151. package/src/swarm/registry.js +419 -419
  152. package/src/swarm/report.js +158 -158
  153. package/src/swarm/runtime.js +576 -576
  154. package/src/swarm/scenario-dsl.js +272 -272
  155. package/src/telemetry/ledger.js +302 -302
  156. package/src/telemetry/session-tracker.js +234 -234
  157. package/src/telemetry/sync.js +203 -203
  158. package/src/ui/command-hints.js +13 -13
  159. package/src/ui/markdown.js +220 -220
@@ -1,679 +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
- 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
-
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
+