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