prism-code-review 2.0.0__py3-none-any.whl
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.
- prism/__init__.py +87 -0
- prism/__main__.py +13 -0
- prism/agents.py +191 -0
- prism/agents_policy.py +277 -0
- prism/agents_quality.py +584 -0
- prism/agents_security.py +243 -0
- prism/agents_test.py +145 -0
- prism/ai_architecture.py +65 -0
- prism/ai_common.py +32 -0
- prism/ai_findings.py +181 -0
- prism/ai_graph.py +224 -0
- prism/ai_performance.py +65 -0
- prism/ai_pipeline.py +447 -0
- prism/ai_policy.py +65 -0
- prism/ai_providers.py +514 -0
- prism/ai_quality.py +65 -0
- prism/ai_security.py +65 -0
- prism/ai_test.py +65 -0
- prism/autofix.py +360 -0
- prism/cli.py +248 -0
- prism/cli_commands.py +711 -0
- prism/cli_config.py +202 -0
- prism/cli_review.py +558 -0
- prism/codebase_cache.py +155 -0
- prism/commands_common.py +365 -0
- prism/commands_reports.py +212 -0
- prism/commands_setup.py +703 -0
- prism/feedback.py +254 -0
- prism/ignore.py +344 -0
- prism/langs.py +251 -0
- prism/logging_config.py +52 -0
- prism/lsp.py +194 -0
- prism/modes.py +94 -0
- prism/reports.py +277 -0
- prism/token_usage.py +93 -0
- prism_code_review-2.0.0.dist-info/METADATA +1392 -0
- prism_code_review-2.0.0.dist-info/RECORD +41 -0
- prism_code_review-2.0.0.dist-info/WHEEL +5 -0
- prism_code_review-2.0.0.dist-info/entry_points.txt +2 -0
- prism_code_review-2.0.0.dist-info/licenses/LICENSE +21 -0
- prism_code_review-2.0.0.dist-info/top_level.txt +1 -0
prism/cli_review.py
ADDED
|
@@ -0,0 +1,558 @@
|
|
|
1
|
+
import re
|
|
2
|
+
from pathlib import Path
|
|
3
|
+
|
|
4
|
+
from prism.agents import (
|
|
5
|
+
AGENT_REGISTRY, Finding, Severity, Blocker, AgentResult, run_pipeline,
|
|
6
|
+
risk_score, quality_gates_met,
|
|
7
|
+
)
|
|
8
|
+
from prism.ai_common import build_skeleton
|
|
9
|
+
from prism.feedback import scan_dependencies, should_suppress
|
|
10
|
+
from prism.modes import get_mode, select_agents
|
|
11
|
+
from prism import agents_policy, agents_quality
|
|
12
|
+
from prism import agents_security, agents_test
|
|
13
|
+
|
|
14
|
+
# The regex agents must be registered for any review path (CLI, LSP, library).
|
|
15
|
+
_REGEX_AGENT_MODULES = (agents_security, agents_test)
|
|
16
|
+
|
|
17
|
+
# Importing the AI agent modules runs their @register_agent decorators. Without
|
|
18
|
+
# this, the default agent list referenced ai_* names that were never in the
|
|
19
|
+
# registry, so run_pipeline logged "Agent not found" and skipped every AI
|
|
20
|
+
# review. The tuple keeps the imports referenced for linters.
|
|
21
|
+
from prism import (ai_security, ai_quality, ai_policy, ai_test,
|
|
22
|
+
ai_architecture, ai_performance)
|
|
23
|
+
|
|
24
|
+
_AI_AGENT_MODULES = (ai_security, ai_quality, ai_policy, ai_test,
|
|
25
|
+
ai_architecture, ai_performance)
|
|
26
|
+
|
|
27
|
+
from prism.cli_config import (
|
|
28
|
+
CONFIDENCE_THRESHOLD, load_deviation_policy, is_deviation, enabled_agents,
|
|
29
|
+
)
|
|
30
|
+
|
|
31
|
+
_SEVERITY_RANK = {"info": 0, "low": 1, "medium": 2, "high": 3, "critical": 4}
|
|
32
|
+
|
|
33
|
+
_STOPWORDS = {
|
|
34
|
+
"the", "and", "for", "with", "this", "that", "from", "into", "when",
|
|
35
|
+
"use", "using", "used", "has", "have", "are", "was", "were", "not",
|
|
36
|
+
"but", "can", "could", "should", "would", "will", "may", "might",
|
|
37
|
+
"there", "their", "them", "then", "than", "also", "only", "very",
|
|
38
|
+
"which", "while", "because", "been", "being", "does", "did", "each",
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
_CATEGORY_FAMILY = (
|
|
42
|
+
("secret", ("secret", "credential", "password", "token", "api key", "apikey")),
|
|
43
|
+
("injection", ("injection", "command", "eval", "exec", "deserial", "xss", "sql")),
|
|
44
|
+
("auth", ("auth", "permission", "access control", "authorization")),
|
|
45
|
+
("performance", ("performance", "n+1", "memory", "algorithm", "latency", "throughput")),
|
|
46
|
+
("architecture", ("architect", "coupling", "modular", "layer", "god", "design")),
|
|
47
|
+
("test", ("test", "coverage", "assert")),
|
|
48
|
+
("quality", ("complex", "duplicat", "maintain", "style", "docstring",
|
|
49
|
+
"naming", "unused", "readab")),
|
|
50
|
+
)
|
|
51
|
+
|
|
52
|
+
|
|
53
|
+
def _category_family(category):
|
|
54
|
+
lowered = (category or "").lower()
|
|
55
|
+
for family, needles in _CATEGORY_FAMILY:
|
|
56
|
+
if any(n in lowered for n in needles):
|
|
57
|
+
return family
|
|
58
|
+
return lowered
|
|
59
|
+
|
|
60
|
+
|
|
61
|
+
def _message_tokens(message):
|
|
62
|
+
words = re.findall(r"[a-z0-9_]{3,}", (message or "").lower())
|
|
63
|
+
return {w for w in words if w not in _STOPWORDS}
|
|
64
|
+
|
|
65
|
+
|
|
66
|
+
# Tokens that name *which* secret is exposed. Two findings about different
|
|
67
|
+
# subjects on the same line (an AWS key and a DB password) are distinct issues
|
|
68
|
+
# and must not be collapsed into one.
|
|
69
|
+
_SECRET_SUBJECTS = ("secret", "credential", "password", "passwd", "pwd",
|
|
70
|
+
"token", "key")
|
|
71
|
+
|
|
72
|
+
# Qualifier tokens distinguish two findings with the same subject: an AWS key
|
|
73
|
+
# and an SSH key on the same line are two different secrets. Generic qualifiers
|
|
74
|
+
# (api/access/private/public) describe the *kind* of key, not a provider, so
|
|
75
|
+
# they are treated as wildcards.
|
|
76
|
+
_SECRET_QUALIFIERS = (
|
|
77
|
+
"aws", "amazon", "gcp", "google", "azure", "ssh", "db", "database",
|
|
78
|
+
"postgres", "postgresql", "mysql", "mongo", "mongodb", "redis", "stripe",
|
|
79
|
+
"github", "gitlab", "slack", "jwt", "smtp", "ftp", "s3",
|
|
80
|
+
)
|
|
81
|
+
_GENERIC_QUALIFIERS = ("api", "access", "private", "public", "root", "admin")
|
|
82
|
+
|
|
83
|
+
# "credential" is what any secret is called. A rule prints it when it will not
|
|
84
|
+
# say which secret it matched, so it corroborates a finding rather than naming a
|
|
85
|
+
# second secret.
|
|
86
|
+
_UNNAMED_SECRET_NOUNS = frozenset({"credential"})
|
|
87
|
+
|
|
88
|
+
|
|
89
|
+
def _secret_subjects(tokens):
|
|
90
|
+
return {t for t in tokens if t in _SECRET_SUBJECTS}
|
|
91
|
+
|
|
92
|
+
|
|
93
|
+
def _secret_qualifiers(tokens):
|
|
94
|
+
return {t for t in tokens if t in _SECRET_QUALIFIERS}
|
|
95
|
+
|
|
96
|
+
|
|
97
|
+
def _views(finding):
|
|
98
|
+
"""The two views of a finding's subject: its message and its category.
|
|
99
|
+
|
|
100
|
+
Both are needed. A category can be a bare rule name that names nothing
|
|
101
|
+
("Banned Pattern"), and a message can leave out the family (a rule that
|
|
102
|
+
says only "command injection").
|
|
103
|
+
"""
|
|
104
|
+
return (_message_tokens(finding.message),
|
|
105
|
+
_message_tokens(finding.category))
|
|
106
|
+
|
|
107
|
+
|
|
108
|
+
def _all_tokens(finding):
|
|
109
|
+
"""Every word of a finding: its message and its category."""
|
|
110
|
+
message_tokens, category_tokens = _views(finding)
|
|
111
|
+
return message_tokens | category_tokens
|
|
112
|
+
|
|
113
|
+
|
|
114
|
+
def _kind_tokens(finding):
|
|
115
|
+
"""The kinds of problem this finding describes, read off its message.
|
|
116
|
+
|
|
117
|
+
The message is where the kind lives. A category is only a label, and for
|
|
118
|
+
the deterministic agents it is a *rule* name -- "Secret Exposure",
|
|
119
|
+
"Dangerous Pattern", "Banned Pattern" -- chosen from a closed set that
|
|
120
|
+
does not line up across agents: one rule files a hardcoded credential under
|
|
121
|
+
"Banned Pattern" and another under "Secret Exposure". Reading the kind off
|
|
122
|
+
the category would call those two findings different kinds of problem and
|
|
123
|
+
keep one secret reported twice.
|
|
124
|
+
"""
|
|
125
|
+
message_tokens, _category_tokens = _views(finding)
|
|
126
|
+
kinds = {family for family, needles in _CATEGORY_FAMILY
|
|
127
|
+
if any(n in message_tokens for n in needles)}
|
|
128
|
+
if kinds:
|
|
129
|
+
return kinds
|
|
130
|
+
# The message names no kind ("Hardcoded credential in source"), so the
|
|
131
|
+
# category is the only evidence left, and a bare rule name is weak: it is
|
|
132
|
+
# an assertion that this is *a* finding, not what kind.
|
|
133
|
+
return set()
|
|
134
|
+
|
|
135
|
+
|
|
136
|
+
def _about_secret(finding):
|
|
137
|
+
"""True when this finding is about a hardcoded secret.
|
|
138
|
+
|
|
139
|
+
Read off the nouns the finding actually names, so an inflected or reworded
|
|
140
|
+
one still counts: "Hardcoded API key detected" and "credential leakage" both
|
|
141
|
+
name a secret, where an exact word match only caught the singular
|
|
142
|
+
"credential" and the closed-up "apikey". Missing that, the copies the model
|
|
143
|
+
agents write for one secret were kept as separate findings.
|
|
144
|
+
"""
|
|
145
|
+
return bool(_secret_subjects(_message_tokens(finding.message))
|
|
146
|
+
or _secret_subjects(_message_tokens(finding.category)))
|
|
147
|
+
|
|
148
|
+
|
|
149
|
+
def _subject_of(finding):
|
|
150
|
+
"""The words naming *which* issue this is, not what kind.
|
|
151
|
+
|
|
152
|
+
Family words are dropped ("secret", "performance" say what kind of problem
|
|
153
|
+
it is and every finding of that kind carries them), and so are the secret
|
|
154
|
+
nouns and qualifiers, which ``_same_secret`` compares separately.
|
|
155
|
+
"""
|
|
156
|
+
tokens = _all_tokens(finding)
|
|
157
|
+
for _family, needles in _CATEGORY_FAMILY:
|
|
158
|
+
tokens -= set(needles)
|
|
159
|
+
return tokens - set(_SECRET_SUBJECTS) - set(_SECRET_QUALIFIERS)
|
|
160
|
+
|
|
161
|
+
|
|
162
|
+
def _same_secret(a, f):
|
|
163
|
+
"""True when two findings are about the same secret, not two secrets.
|
|
164
|
+
|
|
165
|
+
A secret is named by its nouns ("key", "password", "token") and, when the
|
|
166
|
+
finding is precise about it, by a qualifier ("aws", "ssh", "db").
|
|
167
|
+
|
|
168
|
+
The nouns are a claim about which secret this is. Two findings make the same
|
|
169
|
+
claim when they name the same set of nouns, and that is what lets them
|
|
170
|
+
collapse: "Possible api key detected" and "Hardcoded api key in source" are
|
|
171
|
+
the same finding, while "Possible api key detected" and "Possible auth token
|
|
172
|
+
detected" are two, and so are "Hardcoded secret found" and "Hardcoded
|
|
173
|
+
password found", where one names only the family and the other names a kind
|
|
174
|
+
of secret.
|
|
175
|
+
|
|
176
|
+
"credential" is the one noun that claims nothing: a rule prints it when it
|
|
177
|
+
will not say which secret it matched, so it is set aside on both sides. A
|
|
178
|
+
side left with no nouns is making no claim, and a claim with no claim is not
|
|
179
|
+
a contradiction -- "Possible api key detected" and "Banned Pattern: hardcoded
|
|
180
|
+
credential" are one hardcoded key.
|
|
181
|
+
|
|
182
|
+
When both sides name a qualifier, the qualifier decides: an AWS key and an
|
|
183
|
+
SSH key are both keys, and only the qualifier says which.
|
|
184
|
+
|
|
185
|
+
The nouns are read off the message alone. A category is a label from a
|
|
186
|
+
closed set, and the set files every secret under names that contain "secret"
|
|
187
|
+
("Secret Exposure"), so it would add the same family word to both sides and
|
|
188
|
+
hide the difference between the two findings being compared.
|
|
189
|
+
"""
|
|
190
|
+
nouns_a = _secret_subjects(_message_tokens(a.message)) - _UNNAMED_SECRET_NOUNS
|
|
191
|
+
nouns_b = _secret_subjects(_message_tokens(f.message)) - _UNNAMED_SECRET_NOUNS
|
|
192
|
+
|
|
193
|
+
# One side names no particular secret: it is agreeing that a secret is there,
|
|
194
|
+
# which is not a second secret. When both name one, the claims have to be
|
|
195
|
+
# compatible -- the same noun, or one naming a superset of the other ("api
|
|
196
|
+
# key" against "api key or secret string"), never two disjoint nouns.
|
|
197
|
+
if nouns_a and nouns_b and not (nouns_a <= nouns_b or nouns_b <= nouns_a):
|
|
198
|
+
return False
|
|
199
|
+
|
|
200
|
+
qa = _secret_qualifiers(_message_tokens(a.message))
|
|
201
|
+
qb = _secret_qualifiers(_message_tokens(f.message))
|
|
202
|
+
if qa and qb:
|
|
203
|
+
return bool(qa & qb)
|
|
204
|
+
return True
|
|
205
|
+
|
|
206
|
+
|
|
207
|
+
def _kind_conflicts(a, f):
|
|
208
|
+
"""True when both findings describe a kind of problem, and disjoint kinds.
|
|
209
|
+
|
|
210
|
+
Two findings that both say what kind of problem they are, and say different
|
|
211
|
+
ones, are different problems sharing a line: a secret and an O(n^2) loop.
|
|
212
|
+
|
|
213
|
+
Kinds come from the message only (``_kind_tokens``). A category is a label
|
|
214
|
+
an agent picked from a closed set of rule names, and for the deterministic
|
|
215
|
+
agents those names describe rules rather than problems, so they cannot
|
|
216
|
+
support a claim that two findings are unrelated.
|
|
217
|
+
"""
|
|
218
|
+
ka, kf = _kind_tokens(a), _kind_tokens(f)
|
|
219
|
+
return bool(ka and kf and not (ka & kf))
|
|
220
|
+
|
|
221
|
+
|
|
222
|
+
def _same_issue(a, f):
|
|
223
|
+
"""True when two findings on the same line describe the same issue.
|
|
224
|
+
|
|
225
|
+
Every agent answers the same question, so one problem arrives several times
|
|
226
|
+
and the copies must collapse, or the review lists one secret three times and
|
|
227
|
+
the blocking count is fiction.
|
|
228
|
+
|
|
229
|
+
Two findings are one issue when their kinds do not conflict and their
|
|
230
|
+
subjects agree. The kind check is what keeps a secret from collapsing into
|
|
231
|
+
the O(n^2) loop beside it on the same line; within that, secrets compare by
|
|
232
|
+
noun and qualifier, and everything else by subject-word overlap.
|
|
233
|
+
"""
|
|
234
|
+
if _kind_conflicts(a, f):
|
|
235
|
+
return False
|
|
236
|
+
|
|
237
|
+
if _about_secret(a) and _about_secret(f):
|
|
238
|
+
return _same_secret(a, f)
|
|
239
|
+
|
|
240
|
+
ma, mf = _message_tokens(a.message), _message_tokens(f.message)
|
|
241
|
+
# Identical messages are one finding reported twice, however the two agents
|
|
242
|
+
# labelled it: a TODO marker is "Incomplete Code" to one and "Invalid Issue
|
|
243
|
+
# Reference" to the other.
|
|
244
|
+
if ma and ma == mf:
|
|
245
|
+
return True
|
|
246
|
+
|
|
247
|
+
sa, sb = _subject_of(a), _subject_of(f)
|
|
248
|
+
if not sa or not sb:
|
|
249
|
+
return True
|
|
250
|
+
|
|
251
|
+
overlap = len(sa & sb) / len(sa | sb)
|
|
252
|
+
if overlap >= 0.6:
|
|
253
|
+
return True
|
|
254
|
+
if sa <= sb or sb <= sa:
|
|
255
|
+
return True
|
|
256
|
+
# The same kind of problem, and at least one subject word in common. Two
|
|
257
|
+
# agents describe one defect in different words -- "shell command" against
|
|
258
|
+
# "os/exec using unsanitized user input" -- and the shared noun is the thing
|
|
259
|
+
# they are both pointing at. The kind check keeps a command injection from
|
|
260
|
+
# collapsing into the SQL injection beside it.
|
|
261
|
+
ka, kf = _kind_tokens(a), _kind_tokens(f)
|
|
262
|
+
return bool(ka and ka == kf and (sa & sb))
|
|
263
|
+
|
|
264
|
+
|
|
265
|
+
def _rank(f):
|
|
266
|
+
"""Sort key for "which copy of a finding is worth keeping"."""
|
|
267
|
+
return (_SEVERITY_RANK.get(f.severity.value, 0), f.confidence)
|
|
268
|
+
|
|
269
|
+
|
|
270
|
+
def dedupe_results(results):
|
|
271
|
+
"""Collapse findings that describe one issue reported more than once.
|
|
272
|
+
|
|
273
|
+
Every agent answers the same question, so the same problem arrives several
|
|
274
|
+
times: two regex agents both flag the same hardcoded key, and an AI agent
|
|
275
|
+
then describes it again in its own words. The review listed each copy as a
|
|
276
|
+
separate finding, which inflated counts and buried the report.
|
|
277
|
+
|
|
278
|
+
Collapse happens per (file, line), and the surviving copy records who else
|
|
279
|
+
raised it (``corroborated_by``). Findings on different lines, or on the
|
|
280
|
+
same line about genuinely different issues (a secret *and* an O(n^2) loop),
|
|
281
|
+
are kept: that is disagreement between agents, not duplication.
|
|
282
|
+
"""
|
|
283
|
+
# (file, line) -> the finding kept as the representative for that location,
|
|
284
|
+
# and the list holding it. The list is needed because the representative can
|
|
285
|
+
# be replaced later by a stronger copy from another agent's result.
|
|
286
|
+
kept_by_spot = {}
|
|
287
|
+
for r in results:
|
|
288
|
+
kept = []
|
|
289
|
+
for f in r.findings:
|
|
290
|
+
spot = (f.file, f.line)
|
|
291
|
+
found = kept_by_spot.get(spot)
|
|
292
|
+
if found is None or not _same_issue(found[0], f):
|
|
293
|
+
kept_by_spot[spot] = (f, kept)
|
|
294
|
+
kept.append(f)
|
|
295
|
+
continue
|
|
296
|
+
|
|
297
|
+
duplicate, owner = found
|
|
298
|
+
if f.agent == duplicate.agent:
|
|
299
|
+
# The same agent raised the same issue twice: one copy, and no
|
|
300
|
+
# agent credits itself.
|
|
301
|
+
continue
|
|
302
|
+
|
|
303
|
+
if _rank(f) > _rank(duplicate):
|
|
304
|
+
# The stronger copy wins, wherever it came from: it replaces the
|
|
305
|
+
# weaker one in the result that was holding it.
|
|
306
|
+
f.corroborated_by = [duplicate.agent] + [
|
|
307
|
+
a for a in duplicate.corroborated_by if a != f.agent
|
|
308
|
+
]
|
|
309
|
+
if owner is kept:
|
|
310
|
+
_replace_in_place(kept, duplicate, f)
|
|
311
|
+
else:
|
|
312
|
+
owner[:] = [x for x in owner if x is not duplicate]
|
|
313
|
+
kept.append(f)
|
|
314
|
+
kept_by_spot[spot] = (f, kept)
|
|
315
|
+
elif f.agent not in duplicate.corroborated_by:
|
|
316
|
+
duplicate.corroborated_by.append(f.agent)
|
|
317
|
+
r.findings = kept
|
|
318
|
+
|
|
319
|
+
# A result left with nothing is a duplicate-only result: dropping it keeps
|
|
320
|
+
# the "who said what" structure honest (only corroborating agents appear in
|
|
321
|
+
# corroborated_by). Deterministic results are never dropped -- they are the
|
|
322
|
+
# record that the agent ran and found nothing, which is what the report and
|
|
323
|
+
# the enable/disable contract depend on.
|
|
324
|
+
return [r for r in results
|
|
325
|
+
if r.findings or not r.agent_name.startswith("ai_")]
|
|
326
|
+
|
|
327
|
+
|
|
328
|
+
def _replace_in_place(items, old, new):
|
|
329
|
+
"""Swap one finding for another in a list, by identity.
|
|
330
|
+
|
|
331
|
+
``Finding`` compares by value, so ``list.index`` would match a different
|
|
332
|
+
finding that happens to hold the same values.
|
|
333
|
+
"""
|
|
334
|
+
for i, x in enumerate(items):
|
|
335
|
+
if x is old:
|
|
336
|
+
items[i] = new
|
|
337
|
+
return True
|
|
338
|
+
return False
|
|
339
|
+
|
|
340
|
+
|
|
341
|
+
def review_file(file_path, config, effort="balanced", fast=False, diff=""):
|
|
342
|
+
"""Review a single file with the configured agents.
|
|
343
|
+
|
|
344
|
+
Returns (results, risk_score). `diff` (git diff hunks) is preferred over
|
|
345
|
+
full content when available. `--effort lite` runs only deterministic
|
|
346
|
+
regex agents (no API calls); `balanced`/`max` also run the AI agents.
|
|
347
|
+
|
|
348
|
+
The stored ``mode`` (``prism mode``) caps the selection: ``regex`` pins the
|
|
349
|
+
review to the deterministic agents and ``none`` skips agents entirely.
|
|
350
|
+
"""
|
|
351
|
+
path = Path(file_path)
|
|
352
|
+
if not path.exists():
|
|
353
|
+
return [], 0.0
|
|
354
|
+
content = path.read_text(errors="ignore")
|
|
355
|
+
# Binary files are not source; scanning them as text produced magic-number
|
|
356
|
+
# false positives (e.g. .bin, images, compiled artifacts).
|
|
357
|
+
if "\x00" in content:
|
|
358
|
+
return [], 0.0
|
|
359
|
+
skeleton = build_skeleton(file_path, content)
|
|
360
|
+
line_count = content.count("\n") + 1
|
|
361
|
+
agents = enabled_agents(config)
|
|
362
|
+
# `lite` effort is for IDE/LSP use: deterministic agents only, no API calls
|
|
363
|
+
# and no repo-wide coverage run.
|
|
364
|
+
if effort == "lite":
|
|
365
|
+
fast = True
|
|
366
|
+
agents = select_agents(agents, get_mode(config), effort)
|
|
367
|
+
# The policy agent's noisy rules (SPDX headers, print(), TODO refs) are
|
|
368
|
+
# opt-in per project rather than always on.
|
|
369
|
+
policy_cfg = config.get("policy", {}) or {}
|
|
370
|
+
agents_policy.HEADER_CHECKS_ENABLED = bool(policy_cfg.get("headers", False))
|
|
371
|
+
agents_policy.BANNED_CONSOLE_ENABLED = bool(
|
|
372
|
+
policy_cfg.get("banned_console", False))
|
|
373
|
+
agents_policy.CHECK_PRINT_ENABLED = bool(policy_cfg.get("check_print", False))
|
|
374
|
+
agents_policy.CHECK_TODO_REFS_ENABLED = bool(
|
|
375
|
+
policy_cfg.get("check_todo_refs", False))
|
|
376
|
+
if "policy" in AGENT_REGISTRY:
|
|
377
|
+
AGENT_REGISTRY["policy"]._policy = policy_cfg
|
|
378
|
+
# Quality rules that are style preferences are off unless opted in.
|
|
379
|
+
if "quality" in AGENT_REGISTRY:
|
|
380
|
+
AGENT_REGISTRY["quality"]._enabled = dict(
|
|
381
|
+
agents_quality.QUALITY_DEFAULTS,
|
|
382
|
+
**(config.get("quality", {}) or {}))
|
|
383
|
+
if not agents:
|
|
384
|
+
# `none` mode: nothing to review, but callers still expect a result set
|
|
385
|
+
# so the report/JSON/dashboard paths keep working.
|
|
386
|
+
return [], 0.0
|
|
387
|
+
results = run_pipeline(agents, file_path, content, diff, skeleton,
|
|
388
|
+
line_count=line_count, fast=fast)
|
|
389
|
+
|
|
390
|
+
# Run SCA dependency scan (a deterministic check, so `none` mode skips it)
|
|
391
|
+
sca_findings = [] if not agents else scan_dependencies(file_path, content)
|
|
392
|
+
if sca_findings:
|
|
393
|
+
sca_results = []
|
|
394
|
+
for item in sca_findings:
|
|
395
|
+
f = Finding(
|
|
396
|
+
agent="sca",
|
|
397
|
+
severity=Severity(item["severity"]),
|
|
398
|
+
category=item["category"],
|
|
399
|
+
message=item["message"],
|
|
400
|
+
file=item["file"],
|
|
401
|
+
suggestion=item.get("suggestion"),
|
|
402
|
+
blocker=Blocker.BLOCKS if item["severity"] in ("critical", "high") else Blocker.WARNS,
|
|
403
|
+
confidence=item.get("confidence", 0.95),
|
|
404
|
+
rule_id=item["rule_id"],
|
|
405
|
+
)
|
|
406
|
+
sca_results.append(f)
|
|
407
|
+
if sca_results:
|
|
408
|
+
results.append(AgentResult(
|
|
409
|
+
agent_name="sca",
|
|
410
|
+
findings=sca_results,
|
|
411
|
+
passed=not any(f.blocker == Blocker.BLOCKS for f in sca_results),
|
|
412
|
+
duration_ms=0.0,
|
|
413
|
+
))
|
|
414
|
+
|
|
415
|
+
results = dedupe_results(results)
|
|
416
|
+
findings = [f for r in results for f in r.findings]
|
|
417
|
+
score = risk_score(file_path, content, content, findings)
|
|
418
|
+
return results, score
|
|
419
|
+
|
|
420
|
+
|
|
421
|
+
def format_report(results, config, risk_scores=None, audit=False):
|
|
422
|
+
"""Format review results as a human-readable report string.
|
|
423
|
+
|
|
424
|
+
``audit`` is set by ``scan``: the same findings are reported, but the
|
|
425
|
+
verdict is advisory ("no commit gate") because a whole-project audit is
|
|
426
|
+
not a pre-commit check.
|
|
427
|
+
"""
|
|
428
|
+
lines = []
|
|
429
|
+
blocking_sev = set(config.get("block_on", ["critical", "high"]))
|
|
430
|
+
conf_threshold = config.get("confidence_threshold", CONFIDENCE_THRESHOLD)
|
|
431
|
+
policy = load_deviation_policy()
|
|
432
|
+
|
|
433
|
+
total = 0
|
|
434
|
+
blocking_count = 0
|
|
435
|
+
warn_count = 0
|
|
436
|
+
skipped = 0
|
|
437
|
+
|
|
438
|
+
for r in results:
|
|
439
|
+
if not r.findings:
|
|
440
|
+
continue
|
|
441
|
+
lines.append(" [{}] {} finding(s) ({}ms)".format(
|
|
442
|
+
r.agent_name, len(r.findings), int(r.duration_ms)))
|
|
443
|
+
for f in r.findings:
|
|
444
|
+
if f.confidence < conf_threshold:
|
|
445
|
+
skipped += 1
|
|
446
|
+
continue
|
|
447
|
+
if is_deviation(f, policy):
|
|
448
|
+
skipped += 1
|
|
449
|
+
continue
|
|
450
|
+
if f.rule_id and should_suppress(f.rule_id):
|
|
451
|
+
skipped += 1
|
|
452
|
+
continue
|
|
453
|
+
total += 1
|
|
454
|
+
is_blocking = (
|
|
455
|
+
f.severity.value in blocking_sev
|
|
456
|
+
and f.blocker == Blocker.BLOCKS
|
|
457
|
+
)
|
|
458
|
+
if is_blocking:
|
|
459
|
+
blocking_count += 1
|
|
460
|
+
else:
|
|
461
|
+
warn_count += 1
|
|
462
|
+
tag = "BLOCK" if is_blocking else "WARN"
|
|
463
|
+
lines.append(" [{}] {} {} [{}] conf={:.0f}%".format(
|
|
464
|
+
tag, f.severity.value.upper(), f.category,
|
|
465
|
+
f.rule_id or "N/A", f.confidence * 100))
|
|
466
|
+
lines.append(" Problem: {}".format(f.message))
|
|
467
|
+
if f.corroborated_by:
|
|
468
|
+
lines.append(" Also reported by: {}".format(
|
|
469
|
+
", ".join(f.corroborated_by)))
|
|
470
|
+
loc = "{}:{}".format(f.file, f.line) if f.line else f.file
|
|
471
|
+
lines.append(" at {}".format(loc))
|
|
472
|
+
if f.suggestion:
|
|
473
|
+
lines.append(" Suggestion: {}".format(f.suggestion))
|
|
474
|
+
if f.suggested_code:
|
|
475
|
+
lines.append(" Code fix:")
|
|
476
|
+
for code_line in f.suggested_code.split("\n"):
|
|
477
|
+
lines.append(" {}".format(code_line))
|
|
478
|
+
|
|
479
|
+
lines.append("")
|
|
480
|
+
lines.append("PRISM Review Report")
|
|
481
|
+
lines.append("")
|
|
482
|
+
lines.append("Total findings: {}".format(total))
|
|
483
|
+
lines.append(" Blocking: {}".format(blocking_count))
|
|
484
|
+
lines.append(" Warnings: {}".format(warn_count))
|
|
485
|
+
lines.append(" Skipped (low confidence / deviation): {}".format(skipped))
|
|
486
|
+
|
|
487
|
+
if risk_scores:
|
|
488
|
+
lines.append("")
|
|
489
|
+
lines.append("Risk Scores:")
|
|
490
|
+
for fname, score in risk_scores.items():
|
|
491
|
+
bar = "#" * int(score * 20) + "-" * (20 - int(score * 20))
|
|
492
|
+
lines.append(" {} [{}] {:.0%}".format(fname, bar, score))
|
|
493
|
+
|
|
494
|
+
gates_ok = quality_gates_met(results, config)
|
|
495
|
+
if not gates_ok:
|
|
496
|
+
lines.append("")
|
|
497
|
+
lines.append("QUALITY GATES: FAILED")
|
|
498
|
+
|
|
499
|
+
if blocking_count > 0 or not gates_ok:
|
|
500
|
+
lines.append("")
|
|
501
|
+
if audit:
|
|
502
|
+
lines.append(
|
|
503
|
+
"AUDIT: {} issue(s) found - review-only, no commit gate.".format(
|
|
504
|
+
blocking_count))
|
|
505
|
+
else:
|
|
506
|
+
lines.append("COMMIT BLOCKED - resolve {} issue(s) before pushing.".format(
|
|
507
|
+
blocking_count))
|
|
508
|
+
else:
|
|
509
|
+
lines.append("")
|
|
510
|
+
lines.append("All checks passed.")
|
|
511
|
+
|
|
512
|
+
return "\n".join(lines), blocking_count
|
|
513
|
+
|
|
514
|
+
|
|
515
|
+
def json_output(results, config, risk_scores, file_path):
|
|
516
|
+
"""Generate JSON output for IDE/CI integration."""
|
|
517
|
+
blocking_sev = set(config.get("block_on", ["critical", "high"]))
|
|
518
|
+
conf_threshold = config.get("confidence_threshold", CONFIDENCE_THRESHOLD)
|
|
519
|
+
policy = load_deviation_policy()
|
|
520
|
+
|
|
521
|
+
all_findings = []
|
|
522
|
+
for r in results:
|
|
523
|
+
for f in r.findings:
|
|
524
|
+
if f.confidence < conf_threshold:
|
|
525
|
+
continue
|
|
526
|
+
if is_deviation(f, policy):
|
|
527
|
+
continue
|
|
528
|
+
if f.rule_id and should_suppress(f.rule_id):
|
|
529
|
+
continue
|
|
530
|
+
all_findings.append({
|
|
531
|
+
"agent": f.agent,
|
|
532
|
+
"severity": f.severity.value,
|
|
533
|
+
"category": f.category,
|
|
534
|
+
"problem": f.message,
|
|
535
|
+
"file": f.file,
|
|
536
|
+
"line": f.line,
|
|
537
|
+
"suggestion": f.suggestion,
|
|
538
|
+
"suggested_code": f.suggested_code,
|
|
539
|
+
"confidence": f.confidence,
|
|
540
|
+
"rule_id": f.rule_id,
|
|
541
|
+
"blocking": (
|
|
542
|
+
f.severity.value in blocking_sev
|
|
543
|
+
and f.blocker == Blocker.BLOCKS
|
|
544
|
+
),
|
|
545
|
+
})
|
|
546
|
+
|
|
547
|
+
blocking = sum(1 for f in all_findings if f["blocking"])
|
|
548
|
+
gates_ok = quality_gates_met(results, config)
|
|
549
|
+
|
|
550
|
+
return {
|
|
551
|
+
"file": file_path,
|
|
552
|
+
"risk_score": risk_scores.get(file_path, 0.0),
|
|
553
|
+
"total_findings": len(all_findings),
|
|
554
|
+
"findings": all_findings,
|
|
555
|
+
"blocking": blocking,
|
|
556
|
+
"quality_gates_passed": gates_ok,
|
|
557
|
+
"commit_allowed": blocking == 0 and gates_ok,
|
|
558
|
+
}
|