mlai-code 0.3.3 → 0.3.4
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 +1 -1
- package/package.json +1 -1
- package/src/checkpoints.mjs +217 -0
- package/src/cli.mjs +153 -26
- package/src/cloud.mjs +2 -0
- package/src/engine.mjs +1 -1
- package/src/headless.mjs +25 -9
- package/src/home.mjs +3 -2
- package/src/host.mjs +141 -13
- package/src/ide-quick.mjs +147 -0
- package/src/ide.mjs +182 -0
- package/src/netbox.mjs +1 -1
- package/src/parity.mjs +17 -0
- package/src/relay.mjs +1 -1
- package/src/render.mjs +9 -2
- package/src/runtime.mjs +2 -0
- package/src/server.mjs +46 -7
- package/src/session.mjs +31 -8
- package/src/tui/acp.mjs +121 -19
- package/src/tui/app.mjs +2 -1
- package/src/tui/chat.mjs +35 -10
- package/src/tui/cmd-config.mjs +3 -1
- package/src/tui/cmd-core.mjs +9 -5
- package/src/tui/cmd-extensibility.mjs +5 -0
- package/src/tui/extensions-local.mjs +2 -0
- package/src/tui/mcp-config.mjs +22 -4
- package/src/tui/review-cli.mjs +15 -4
- package/src/tui/snapshots.mjs +57 -16
- package/src/two-words.mjs +14 -8
- package/src/view-sync.mjs +48 -0
- package/src/winenv.mjs +1 -1
- package/src/workspace-api.mjs +21 -4
- package/vendor/client-sdk/index.cjs +2 -1
- package/vendor/cloud-cli/mlai.mjs +23 -11
- package/worker/architecture_worker.py +33 -4
- package/worker/browser_tool.py +19 -1
- package/worker/chat_fork.py +4 -1
- package/worker/checkpoints.py +71 -8
- package/worker/command_safety.py +56 -3
- package/worker/connected_turn.py +30 -0
- package/worker/conversation.py +81 -11
- package/worker/git_sync.py +4 -4
- package/worker/image_generation.py +2 -2
- package/worker/kimi_bridge.py +247 -27
- package/worker/kimi_gates.py +25 -6
- package/worker/kimi_hook.py +153 -12
- package/worker/media_tools.py +4 -1
- package/worker/mlai_hooks.py +222 -3
- package/worker/mlai_mcp.py +26 -6
- package/worker/mlai_review/README.md +35 -0
- package/worker/mlai_review/__init__.py +1 -0
- package/worker/mlai_review/__main__.py +261 -0
- package/worker/mlai_review/_compat.py +29 -0
- package/worker/mlai_review/analysis/__init__.py +3 -0
- package/worker/mlai_review/analysis/detect.py +50 -0
- package/worker/mlai_review/analysis/main.py +128 -0
- package/worker/mlai_review/analysis/messages.py +8 -0
- package/worker/mlai_review/analysis/tools.py +194 -0
- package/worker/mlai_review/application/__init__.py +1 -0
- package/worker/mlai_review/application/asking.py +53 -0
- package/worker/mlai_review/application/challengers.py +142 -0
- package/worker/mlai_review/application/context_builder.py +194 -0
- package/worker/mlai_review/application/errors.py +17 -0
- package/worker/mlai_review/application/feedback.py +52 -0
- package/worker/mlai_review/application/matching.py +46 -0
- package/worker/mlai_review/application/messages.py +41 -0
- package/worker/mlai_review/application/ports.py +222 -0
- package/worker/mlai_review/application/prompts.py +342 -0
- package/worker/mlai_review/application/review_service.py +200 -0
- package/worker/mlai_review/application/reviewers.py +438 -0
- package/worker/mlai_review/application/streaming.py +91 -0
- package/worker/mlai_review/application/tools.py +138 -0
- package/worker/mlai_review/application/usage.py +145 -0
- package/worker/mlai_review/application/write_check.py +46 -0
- package/worker/mlai_review/contracts/__init__.py +5 -0
- package/worker/mlai_review/contracts/analysis.py +57 -0
- package/worker/mlai_review/contracts/review.py +42 -0
- package/worker/mlai_review/domain/__init__.py +4 -0
- package/worker/mlai_review/domain/changes.py +77 -0
- package/worker/mlai_review/domain/context.py +147 -0
- package/worker/mlai_review/domain/diagnostics.py +239 -0
- package/worker/mlai_review/domain/feedback.py +216 -0
- package/worker/mlai_review/domain/findings.py +505 -0
- package/worker/mlai_review/domain/lifecycle.py +85 -0
- package/worker/mlai_review/domain/partition.py +42 -0
- package/worker/mlai_review/domain/relations.py +166 -0
- package/worker/mlai_review/domain/relevance.py +202 -0
- package/worker/mlai_review/domain/report.py +99 -0
- package/worker/mlai_review/domain/structure.py +56 -0
- package/worker/mlai_review/domain/writes.py +86 -0
- package/worker/mlai_review/eval.py +221 -0
- package/worker/mlai_review/eval_cases/clean-rename.json +11 -0
- package/worker/mlai_review/eval_cases/missing-ownership-check.json +22 -0
- package/worker/mlai_review/eval_cases/off-by-one.json +22 -0
- package/worker/mlai_review/eval_cases/sql-injection.json +20 -0
- package/worker/mlai_review/feedback_report.py +60 -0
- package/worker/mlai_review/infrastructure/__init__.py +1 -0
- package/worker/mlai_review/infrastructure/analysis/__init__.py +1 -0
- package/worker/mlai_review/infrastructure/analysis/none.py +12 -0
- package/worker/mlai_review/infrastructure/analysis/translate.py +17 -0
- package/worker/mlai_review/infrastructure/github/__init__.py +0 -0
- package/worker/mlai_review/infrastructure/github/client.py +445 -0
- package/worker/mlai_review/infrastructure/github/feedback.py +212 -0
- package/worker/mlai_review/infrastructure/github/http.py +31 -0
- package/worker/mlai_review/infrastructure/github/rendering.py +398 -0
- package/worker/mlai_review/infrastructure/github/token.py +20 -0
- package/worker/mlai_review/infrastructure/local/__init__.py +1 -0
- package/worker/mlai_review/infrastructure/local/analyzer.py +48 -0
- package/worker/mlai_review/infrastructure/local/events.py +100 -0
- package/worker/mlai_review/infrastructure/local/git_reader.py +233 -0
- package/worker/mlai_review/infrastructure/local/publisher.py +138 -0
- package/worker/mlai_review/infrastructure/messages.py +54 -0
- package/worker/mlai_review/infrastructure/models/__init__.py +0 -0
- package/worker/mlai_review/infrastructure/models/openai_compatible.py +269 -0
- package/worker/mlai_review/infrastructure/structure/__init__.py +1 -0
- package/worker/mlai_review/infrastructure/structure/heuristic.py +186 -0
- package/worker/mlai_review/infrastructure/structure/tree_sitter.py +186 -0
- package/worker/mlai_review/infrastructure/workspace/__init__.py +1 -0
- package/worker/mlai_review/infrastructure/workspace/local.py +105 -0
- package/worker/mlai_review/infrastructure/workspace/memory.py +77 -0
- package/worker/mlai_review/infrastructure/workspace/paths.py +17 -0
- package/worker/mlai_review/pipeline.py +89 -0
- package/worker/mlai_review/runtime.py +194 -0
- package/worker/mlai_review/settings.py +139 -0
- package/worker/preview.py +15 -0
- package/worker/product_facts.py +3 -1
- package/worker/repo_policy.py +5 -0
- package/worker/roles.py +29 -2
- package/worker/runner.py +126 -18
- package/worker/runtime_control.py +64 -3
- package/worker/secret_scan.py +3 -2
- package/worker/server_memory.py +122 -0
- package/worker/subagents.py +6 -2
- package/worker/tool_catalog.py +1 -1
- package/worker/verification.py +113 -8
|
@@ -0,0 +1,505 @@
|
|
|
1
|
+
"""Findings: what a reviewer believes is wrong, and the policy for which ones a developer sees.
|
|
2
|
+
|
|
3
|
+
No `from __future__ import annotations` here: these types cross the workflow boundary, and
|
|
4
|
+
Temporal's sandbox must resolve their field types as real objects, not strings.
|
|
5
|
+
"""
|
|
6
|
+
|
|
7
|
+
import hashlib
|
|
8
|
+
import re
|
|
9
|
+
from abc import ABC, abstractmethod
|
|
10
|
+
from dataclasses import dataclass, replace
|
|
11
|
+
from mlai_review._compat import StrEnum
|
|
12
|
+
from typing import Protocol
|
|
13
|
+
|
|
14
|
+
|
|
15
|
+
class Severity(StrEnum):
|
|
16
|
+
CRITICAL = "critical"
|
|
17
|
+
HIGH = "high"
|
|
18
|
+
MEDIUM = "medium"
|
|
19
|
+
LOW = "low"
|
|
20
|
+
|
|
21
|
+
@property
|
|
22
|
+
def rank(self) -> int:
|
|
23
|
+
return _RANK[self]
|
|
24
|
+
|
|
25
|
+
|
|
26
|
+
_RANK = {Severity.CRITICAL: 3, Severity.HIGH: 2, Severity.MEDIUM: 1, Severity.LOW: 0}
|
|
27
|
+
|
|
28
|
+
|
|
29
|
+
@dataclass(frozen=True, slots=True)
|
|
30
|
+
class Fix:
|
|
31
|
+
"""A concrete change: replace lines start..end (inclusive, new-file numbering) with code."""
|
|
32
|
+
|
|
33
|
+
start: int
|
|
34
|
+
end: int
|
|
35
|
+
code: str
|
|
36
|
+
# The lines being replaced, when the file text was available; shown as removed lines.
|
|
37
|
+
replaced: tuple[str, ...] = ()
|
|
38
|
+
# True when every replaced line is in the diff, so GitHub can offer it as a one-click
|
|
39
|
+
# suggestion on the pull request.
|
|
40
|
+
suggestable: bool = False
|
|
41
|
+
|
|
42
|
+
|
|
43
|
+
# A fix that rewrites more than this is a refactor, not a review suggestion.
|
|
44
|
+
MAX_FIX_LINES = 30
|
|
45
|
+
|
|
46
|
+
|
|
47
|
+
class ChallengeOutcome(StrEnum):
|
|
48
|
+
CONFIRMED = "confirmed" # the refuter tried to break the claim and could not
|
|
49
|
+
REFUTED = "refuted" # the refuter found concrete evidence the claim is wrong
|
|
50
|
+
UNSURE = "unsure" # depends on code the refuter could not see
|
|
51
|
+
|
|
52
|
+
|
|
53
|
+
class RefutationGround(StrEnum):
|
|
54
|
+
"""Why the refuter rejects a claim. Not every ground counts: a defect that predates the
|
|
55
|
+
change is still one the change exposes, so that ground only weakens the claim."""
|
|
56
|
+
|
|
57
|
+
CANNOT_OCCUR = "cannot_occur"
|
|
58
|
+
HANDLED_ELSEWHERE = "handled_elsewhere"
|
|
59
|
+
INTENTIONAL = "intentional"
|
|
60
|
+
MISREADS_CODE = "misreads_code"
|
|
61
|
+
NOT_A_DEFECT = "not_a_defect"
|
|
62
|
+
PRE_EXISTING = "pre_existing"
|
|
63
|
+
OTHER = "other"
|
|
64
|
+
|
|
65
|
+
|
|
66
|
+
# Grounds on which a refutation stands. Any other ground makes the outcome "unsure".
|
|
67
|
+
VALID_GROUNDS = frozenset(
|
|
68
|
+
{
|
|
69
|
+
RefutationGround.CANNOT_OCCUR,
|
|
70
|
+
RefutationGround.HANDLED_ELSEWHERE,
|
|
71
|
+
RefutationGround.INTENTIONAL,
|
|
72
|
+
RefutationGround.MISREADS_CODE,
|
|
73
|
+
RefutationGround.NOT_A_DEFECT,
|
|
74
|
+
}
|
|
75
|
+
)
|
|
76
|
+
|
|
77
|
+
|
|
78
|
+
@dataclass(frozen=True, slots=True)
|
|
79
|
+
class Challenge:
|
|
80
|
+
"""A second opinion on a finding: the refuter's verdict, its ground and its evidence."""
|
|
81
|
+
|
|
82
|
+
outcome: ChallengeOutcome
|
|
83
|
+
reason: str
|
|
84
|
+
confidence: float
|
|
85
|
+
ground: RefutationGround | None = None
|
|
86
|
+
|
|
87
|
+
|
|
88
|
+
@dataclass(frozen=True, slots=True)
|
|
89
|
+
class Finding:
|
|
90
|
+
title: str
|
|
91
|
+
severity: Severity
|
|
92
|
+
category: str
|
|
93
|
+
path: str
|
|
94
|
+
line: int
|
|
95
|
+
explanation: str
|
|
96
|
+
suggestion: str
|
|
97
|
+
confidence: float
|
|
98
|
+
# True when the diff shows `line`, so the finding can be pointed at in the diff itself.
|
|
99
|
+
in_diff: bool
|
|
100
|
+
fix: Fix | None = None
|
|
101
|
+
challenge: Challenge | None = None
|
|
102
|
+
# Shown as a possibility, not a verdict: it cleared the lower bar only.
|
|
103
|
+
tentative: bool = False
|
|
104
|
+
# How many of the review's independent readings reported this issue, out of how many. With
|
|
105
|
+
# a single reading there is no agreement to go by, and the model's confidence decides.
|
|
106
|
+
votes: int = 1
|
|
107
|
+
readings: int = 1
|
|
108
|
+
|
|
109
|
+
@property
|
|
110
|
+
def agreement_known(self) -> bool:
|
|
111
|
+
return self.readings > 1
|
|
112
|
+
|
|
113
|
+
@property
|
|
114
|
+
def refuted(self) -> bool:
|
|
115
|
+
return self.challenge is not None and self.challenge.outcome is ChallengeOutcome.REFUTED
|
|
116
|
+
|
|
117
|
+
@property
|
|
118
|
+
def confirmed(self) -> bool:
|
|
119
|
+
return self.challenge is not None and self.challenge.outcome is ChallengeOutcome.CONFIRMED
|
|
120
|
+
|
|
121
|
+
|
|
122
|
+
@dataclass(frozen=True, slots=True)
|
|
123
|
+
class RankedFindings:
|
|
124
|
+
published: tuple[Finding, ...]
|
|
125
|
+
suppressed: int
|
|
126
|
+
# Candidates the refuter disproved; never shown.
|
|
127
|
+
refuted: int = 0
|
|
128
|
+
|
|
129
|
+
|
|
130
|
+
_WORD = re.compile(r"[a-z0-9_]+")
|
|
131
|
+
_FILLER = frozenset(
|
|
132
|
+
{
|
|
133
|
+
"a",
|
|
134
|
+
"an",
|
|
135
|
+
"the",
|
|
136
|
+
"is",
|
|
137
|
+
"are",
|
|
138
|
+
"to",
|
|
139
|
+
"of",
|
|
140
|
+
"in",
|
|
141
|
+
"on",
|
|
142
|
+
"for",
|
|
143
|
+
"and",
|
|
144
|
+
"or",
|
|
145
|
+
"not",
|
|
146
|
+
"no",
|
|
147
|
+
"can",
|
|
148
|
+
"when",
|
|
149
|
+
"with",
|
|
150
|
+
"by",
|
|
151
|
+
"be",
|
|
152
|
+
"it",
|
|
153
|
+
"this",
|
|
154
|
+
"that",
|
|
155
|
+
}
|
|
156
|
+
)
|
|
157
|
+
_NEAR_LINES = 2
|
|
158
|
+
_SAME_WORDS = 0.4
|
|
159
|
+
# Two readings often word one issue differently in the title and alike in the explanation. On
|
|
160
|
+
# the benchmark, distinct issues within two lines of each other shared at most 0.15 of their
|
|
161
|
+
# title-and-explanation words; one issue retold by two readings shared 0.30.
|
|
162
|
+
_SAME_ACCOUNT = 0.2
|
|
163
|
+
# Below this many words an account is too short for its overlap to mean anything.
|
|
164
|
+
_MIN_ACCOUNT_WORDS = 12
|
|
165
|
+
TOOL_CATEGORY = "static-analysis"
|
|
166
|
+
|
|
167
|
+
|
|
168
|
+
def _words(text: str) -> frozenset[str]:
|
|
169
|
+
return frozenset(w for w in _WORD.findall(text.lower()) if w not in _FILLER)
|
|
170
|
+
|
|
171
|
+
|
|
172
|
+
class Reported(Protocol):
|
|
173
|
+
"""Anything that describes an issue at a place: a finding, or an earlier comment."""
|
|
174
|
+
|
|
175
|
+
@property
|
|
176
|
+
def path(self) -> str: ...
|
|
177
|
+
@property
|
|
178
|
+
def line(self) -> int: ...
|
|
179
|
+
@property
|
|
180
|
+
def title(self) -> str: ...
|
|
181
|
+
@property
|
|
182
|
+
def explanation(self) -> str: ...
|
|
183
|
+
@property
|
|
184
|
+
def category(self) -> str: ...
|
|
185
|
+
|
|
186
|
+
|
|
187
|
+
def fingerprint(finding: Finding) -> str:
|
|
188
|
+
"""Identity of an issue across pushes: the file and the words of the title, not the line
|
|
189
|
+
(lines shift as code around them changes)."""
|
|
190
|
+
words = " ".join(sorted(_words(finding.title)))
|
|
191
|
+
return hashlib.sha1(f"{finding.path}\n{words}".encode()).hexdigest()[:16]
|
|
192
|
+
|
|
193
|
+
|
|
194
|
+
def same_issue(a: Reported, b: Reported) -> bool:
|
|
195
|
+
"""Two reports describe one issue when they point at (nearly) the same place and their
|
|
196
|
+
titles mostly agree. Location alone is not enough: distinct bugs often sit lines apart,
|
|
197
|
+
except for a tool's message: a compiler error and a reviewer's finding on the very same
|
|
198
|
+
line are one issue, whatever the wording."""
|
|
199
|
+
if a.path != b.path or abs(a.line - b.line) > _NEAR_LINES:
|
|
200
|
+
return False
|
|
201
|
+
if a.line == b.line and TOOL_CATEGORY in (a.category, b.category):
|
|
202
|
+
return True
|
|
203
|
+
if _overlap(_words(a.title), _words(b.title)) >= _SAME_WORDS:
|
|
204
|
+
return True
|
|
205
|
+
ta, tb = _account(a), _account(b)
|
|
206
|
+
if min(len(ta), len(tb)) < _MIN_ACCOUNT_WORDS:
|
|
207
|
+
return False
|
|
208
|
+
return _overlap(ta, tb) >= _SAME_ACCOUNT
|
|
209
|
+
|
|
210
|
+
|
|
211
|
+
def _account(report: Reported) -> frozenset[str]:
|
|
212
|
+
return _words(f"{report.title} {report.explanation}")
|
|
213
|
+
|
|
214
|
+
|
|
215
|
+
def _overlap(a: frozenset[str], b: frozenset[str]) -> float:
|
|
216
|
+
return len(a & b) / len(a | b) if a and b else 0.0
|
|
217
|
+
|
|
218
|
+
|
|
219
|
+
def merge_duplicates(findings: list[Finding]) -> list[Finding]:
|
|
220
|
+
"""Collapse reports of the same issue (e.g. from two specialists) into the most confident
|
|
221
|
+
one, keeping the highest severity any of them gave."""
|
|
222
|
+
merged: list[Finding] = []
|
|
223
|
+
for finding in sorted(findings, key=lambda f: -f.confidence):
|
|
224
|
+
twin = next((i for i, kept in enumerate(merged) if same_issue(kept, finding)), None)
|
|
225
|
+
if twin is None:
|
|
226
|
+
merged.append(finding)
|
|
227
|
+
continue
|
|
228
|
+
kept = merged[twin]
|
|
229
|
+
if finding.severity.rank > kept.severity.rank:
|
|
230
|
+
kept = replace(kept, severity=finding.severity)
|
|
231
|
+
if kept.category == TOOL_CATEGORY and finding.category != TOOL_CATEGORY:
|
|
232
|
+
# The reviewer explains and suggests; the tool only confirms. Keep both.
|
|
233
|
+
kept = replace(
|
|
234
|
+
finding,
|
|
235
|
+
severity=kept.severity,
|
|
236
|
+
confidence=kept.confidence,
|
|
237
|
+
challenge=kept.challenge,
|
|
238
|
+
title=f"{finding.title} ({kept.title.split(':')[0]})",
|
|
239
|
+
)
|
|
240
|
+
merged[twin] = kept
|
|
241
|
+
return merged
|
|
242
|
+
|
|
243
|
+
|
|
244
|
+
class Admission(StrEnum):
|
|
245
|
+
PUBLISH = "publish"
|
|
246
|
+
PUBLISH_TENTATIVE = "publish_tentative" # shown, worded as a possibility
|
|
247
|
+
REFUTED = "refuted"
|
|
248
|
+
BELOW_BAR = "below_bar"
|
|
249
|
+
DUPLICATE = "duplicate" # the same issue as one already shown
|
|
250
|
+
OVER_BUDGET = "over_budget"
|
|
251
|
+
# Not commented on, but listed in the review's summary: below the bar for a comment, or
|
|
252
|
+
# over the comment budget, yet not disproved.
|
|
253
|
+
NOTE = "note"
|
|
254
|
+
|
|
255
|
+
|
|
256
|
+
class PublicationGate(ABC):
|
|
257
|
+
"""The policy applied one finding at a time, as findings arrive: what is already shown
|
|
258
|
+
decides whether the next one is a duplicate or over the budget."""
|
|
259
|
+
|
|
260
|
+
@abstractmethod
|
|
261
|
+
def decide(self, finding: Finding) -> Admission: ...
|
|
262
|
+
|
|
263
|
+
@abstractmethod
|
|
264
|
+
def note(self, finding: Finding) -> None:
|
|
265
|
+
"""Records a finding listed in the summary, so a later one can be told a duplicate."""
|
|
266
|
+
|
|
267
|
+
@abstractmethod
|
|
268
|
+
def admit(self, finding: Finding) -> None:
|
|
269
|
+
"""Count a finding as shown."""
|
|
270
|
+
|
|
271
|
+
|
|
272
|
+
@dataclass(frozen=True, slots=True)
|
|
273
|
+
class Issue:
|
|
274
|
+
"""One issue as the independent readings of a change reported it: the version first
|
|
275
|
+
reported, and which readings reported it."""
|
|
276
|
+
|
|
277
|
+
finding: Finding
|
|
278
|
+
readers: frozenset[int]
|
|
279
|
+
|
|
280
|
+
|
|
281
|
+
# Two issues this close in one file may be one issue in other words; a model decides (see
|
|
282
|
+
# maybe_same). On the benchmark, a second comment on an issue already commented sat 0-6 lines
|
|
283
|
+
# from the first and shared as few words with it as distinct issues do.
|
|
284
|
+
MAYBE_SAME_LINES = 10
|
|
285
|
+
|
|
286
|
+
|
|
287
|
+
def issues_in(readings: list[list[Finding]]) -> list[Issue]:
|
|
288
|
+
"""Claims grouped by issue where their words agree; a reading that repeats an issue in its
|
|
289
|
+
own answer counts once."""
|
|
290
|
+
found: list[tuple[Finding, set[int]]] = []
|
|
291
|
+
for index, reading in enumerate(readings):
|
|
292
|
+
for finding in reading:
|
|
293
|
+
known = next((issue for issue in found if same_issue(issue[0], finding)), None)
|
|
294
|
+
if known is None:
|
|
295
|
+
found.append((finding, {index}))
|
|
296
|
+
else:
|
|
297
|
+
known[1].add(index)
|
|
298
|
+
return [Issue(f, frozenset(who)) for f, who in found]
|
|
299
|
+
|
|
300
|
+
|
|
301
|
+
def maybe_same(issues: list[Issue]) -> list[tuple[int, int]]:
|
|
302
|
+
"""Pairs of issues the words could not tell apart from different ones, but which are close
|
|
303
|
+
enough in one file to be the same issue told differently."""
|
|
304
|
+
return [
|
|
305
|
+
(i, j)
|
|
306
|
+
for i, a in enumerate(issues)
|
|
307
|
+
for j in range(i + 1, len(issues))
|
|
308
|
+
if (b := issues[j]).finding.path == a.finding.path
|
|
309
|
+
and abs(b.finding.line - a.finding.line) <= MAYBE_SAME_LINES
|
|
310
|
+
]
|
|
311
|
+
|
|
312
|
+
|
|
313
|
+
def join(issues: list[Issue], same: list[tuple[int, int]]) -> list[Issue]:
|
|
314
|
+
"""Issues found to be one, joined: the first reported version is kept, at the highest
|
|
315
|
+
severity any of them had, and the readers of all of them count."""
|
|
316
|
+
root = list(range(len(issues)))
|
|
317
|
+
|
|
318
|
+
def find(i: int) -> int:
|
|
319
|
+
while root[i] != i:
|
|
320
|
+
i = root[i]
|
|
321
|
+
return i
|
|
322
|
+
|
|
323
|
+
for i, j in same:
|
|
324
|
+
a, b = find(i), find(j)
|
|
325
|
+
if a != b:
|
|
326
|
+
root[max(a, b)] = min(a, b)
|
|
327
|
+
groups: dict[int, list[Issue]] = {}
|
|
328
|
+
for index, issue in enumerate(issues):
|
|
329
|
+
groups.setdefault(find(index), []).append(issue)
|
|
330
|
+
joined = []
|
|
331
|
+
for members in groups.values():
|
|
332
|
+
kept = members[0].finding
|
|
333
|
+
worst = max((m.finding.severity for m in members), key=lambda s: s.rank)
|
|
334
|
+
readers = frozenset().union(*(m.readers for m in members))
|
|
335
|
+
joined.append(Issue(replace(kept, severity=worst), readers))
|
|
336
|
+
return joined
|
|
337
|
+
|
|
338
|
+
|
|
339
|
+
def counted(issues: list[Issue], readings: int) -> list[Finding]:
|
|
340
|
+
"""Each issue as a finding marked with how many of the readings reported it."""
|
|
341
|
+
return [replace(i.finding, votes=len(i.readers), readings=readings) for i in issues]
|
|
342
|
+
|
|
343
|
+
|
|
344
|
+
def agree(readings: list[list[Finding]]) -> list[Finding]:
|
|
345
|
+
"""One finding per issue across independent readings of the same change, by their words
|
|
346
|
+
alone."""
|
|
347
|
+
return counted(issues_in(readings), len(readings))
|
|
348
|
+
|
|
349
|
+
|
|
350
|
+
class PublicationPolicy(ABC):
|
|
351
|
+
"""Decides which candidate findings a developer sees. High recall inside, precision outside."""
|
|
352
|
+
|
|
353
|
+
@abstractmethod
|
|
354
|
+
def rank(self, candidates: list[Finding]) -> RankedFindings:
|
|
355
|
+
"""All at once, most important first (used to tune thresholds offline)."""
|
|
356
|
+
|
|
357
|
+
@abstractmethod
|
|
358
|
+
def gate(self) -> PublicationGate:
|
|
359
|
+
"""One at a time, as a review streams its findings out."""
|
|
360
|
+
|
|
361
|
+
|
|
362
|
+
class NoiseBudgetPolicy(PublicationPolicy):
|
|
363
|
+
"""The PRD's default thresholds and noise budget.
|
|
364
|
+
|
|
365
|
+
confidence > 0.90: publish; 0.75-0.90: publish if severity >= high; 0.50-0.75: internal only;
|
|
366
|
+
< 0.50: discard. At most `budget` findings are shown, but critical ones are never cut.
|
|
367
|
+
|
|
368
|
+
A finding the refuter disproved is never shown. One the refuter confirmed clears a lower
|
|
369
|
+
bar (`publish_confirmed_at`): a second opinion that failed to break it is worth more than
|
|
370
|
+
the reviewer's own confidence.
|
|
371
|
+
|
|
372
|
+
Below those bars, a medium-or-worse finding the refuter did not disprove may still be shown
|
|
373
|
+
as a possibility (`publish_tentative_at`), worded as such, up to `tentative_budget` per
|
|
374
|
+
review: the developer gets to judge it instead of it disappearing into a count.
|
|
375
|
+
|
|
376
|
+
When the change was read several times independently, agreement replaces confidence: the
|
|
377
|
+
model's own confidence barely separates real issues from false ones (on the benchmark, 44%
|
|
378
|
+
of findings at 0.9 were real, 32% at 0.7), while agreement does (33% of issues one reading
|
|
379
|
+
of three reported, 62% of those two did, 75% of those all three did). An issue at least
|
|
380
|
+
`publish_agreed_at` readings reported is stated; one only a single reading reported is a
|
|
381
|
+
possibility, and only when the refuter confirmed it at `publish_single_confirmed_at`.
|
|
382
|
+
"""
|
|
383
|
+
|
|
384
|
+
def __init__(
|
|
385
|
+
self,
|
|
386
|
+
*,
|
|
387
|
+
publish_at: float = 0.90,
|
|
388
|
+
publish_high_at: float = 0.75,
|
|
389
|
+
publish_confirmed_at: float = 0.75,
|
|
390
|
+
publish_tentative_at: float = 1.01, # off unless configured
|
|
391
|
+
budget: int = 5,
|
|
392
|
+
tentative_budget: int = 3,
|
|
393
|
+
note_budget: int = 0,
|
|
394
|
+
publish_agreed_at: int = 2,
|
|
395
|
+
publish_single_confirmed_at: float = 0.80,
|
|
396
|
+
) -> None:
|
|
397
|
+
self._publish_agreed_at = publish_agreed_at
|
|
398
|
+
self._publish_single_confirmed_at = publish_single_confirmed_at
|
|
399
|
+
self._publish_at = publish_at
|
|
400
|
+
self._publish_high_at = publish_high_at
|
|
401
|
+
self._publish_confirmed_at = publish_confirmed_at
|
|
402
|
+
self._publish_tentative_at = publish_tentative_at
|
|
403
|
+
self._budget = budget
|
|
404
|
+
self._tentative_budget = tentative_budget
|
|
405
|
+
# Findings listed in the summary instead of being dropped; 0 lists none.
|
|
406
|
+
self._note_budget = note_budget
|
|
407
|
+
|
|
408
|
+
def rank(self, candidates: list[Finding]) -> RankedFindings:
|
|
409
|
+
unique = merge_duplicates(candidates)
|
|
410
|
+
refuted = [f for f in unique if f.refuted]
|
|
411
|
+
eligible = sorted(
|
|
412
|
+
(f for f in unique if not f.refuted and self._eligible(f)),
|
|
413
|
+
key=lambda f: (f.severity.rank, f.confidence),
|
|
414
|
+
reverse=True,
|
|
415
|
+
)
|
|
416
|
+
shown = [
|
|
417
|
+
f for i, f in enumerate(eligible) if i < self._budget or f.severity is Severity.CRITICAL
|
|
418
|
+
]
|
|
419
|
+
return RankedFindings(
|
|
420
|
+
published=tuple(shown),
|
|
421
|
+
suppressed=len(candidates) - len(shown) - len(refuted),
|
|
422
|
+
refuted=len(refuted),
|
|
423
|
+
)
|
|
424
|
+
|
|
425
|
+
def gate(self) -> PublicationGate:
|
|
426
|
+
return _BudgetGate(self)
|
|
427
|
+
|
|
428
|
+
def eligible(self, finding: Finding) -> bool:
|
|
429
|
+
return self._eligible(finding)
|
|
430
|
+
|
|
431
|
+
def tentatively_eligible(self, finding: Finding) -> bool:
|
|
432
|
+
if finding.agreement_known:
|
|
433
|
+
return finding.confirmed and finding.confidence >= self._publish_single_confirmed_at
|
|
434
|
+
return (
|
|
435
|
+
finding.confidence >= self._publish_tentative_at
|
|
436
|
+
and finding.severity.rank >= Severity.MEDIUM.rank
|
|
437
|
+
)
|
|
438
|
+
|
|
439
|
+
@property
|
|
440
|
+
def budget(self) -> int:
|
|
441
|
+
return self._budget
|
|
442
|
+
|
|
443
|
+
@property
|
|
444
|
+
def note_budget(self) -> int:
|
|
445
|
+
return self._note_budget
|
|
446
|
+
|
|
447
|
+
@property
|
|
448
|
+
def tentative_budget(self) -> int:
|
|
449
|
+
return self._tentative_budget
|
|
450
|
+
|
|
451
|
+
def _eligible(self, finding: Finding) -> bool:
|
|
452
|
+
if finding.agreement_known:
|
|
453
|
+
return finding.votes >= self._publish_agreed_at
|
|
454
|
+
if finding.confidence > self._publish_at:
|
|
455
|
+
return True
|
|
456
|
+
if finding.confirmed and finding.confidence >= self._publish_confirmed_at:
|
|
457
|
+
return True
|
|
458
|
+
return (
|
|
459
|
+
finding.confidence >= self._publish_high_at
|
|
460
|
+
and finding.severity.rank >= Severity.HIGH.rank
|
|
461
|
+
)
|
|
462
|
+
|
|
463
|
+
|
|
464
|
+
class _BudgetGate(PublicationGate):
|
|
465
|
+
"""Refuted findings never pass; a finding that repeats one already shown or noted is a
|
|
466
|
+
duplicate; after `budget` non-critical findings only critical ones are commented on. What
|
|
467
|
+
is not disproved but does not earn a comment (below the bar, or over a budget) is noted,
|
|
468
|
+
up to `note_budget`."""
|
|
469
|
+
|
|
470
|
+
def __init__(self, policy: NoiseBudgetPolicy) -> None:
|
|
471
|
+
self._policy = policy
|
|
472
|
+
self._shown: list[Finding] = []
|
|
473
|
+
self._noted: list[Finding] = []
|
|
474
|
+
self._non_critical = 0
|
|
475
|
+
self._tentative = 0
|
|
476
|
+
|
|
477
|
+
def decide(self, finding: Finding) -> Admission:
|
|
478
|
+
if finding.refuted:
|
|
479
|
+
return Admission.REFUTED
|
|
480
|
+
if any(same_issue(seen, finding) for seen in (*self._shown, *self._noted)):
|
|
481
|
+
return Admission.DUPLICATE
|
|
482
|
+
sure = self._policy.eligible(finding)
|
|
483
|
+
if not sure and not self._policy.tentatively_eligible(finding):
|
|
484
|
+
return self._note_or(Admission.BELOW_BAR)
|
|
485
|
+
if not sure:
|
|
486
|
+
if self._tentative >= self._policy.tentative_budget:
|
|
487
|
+
return self._note_or(Admission.OVER_BUDGET)
|
|
488
|
+
return Admission.PUBLISH_TENTATIVE
|
|
489
|
+
critical = finding.severity is Severity.CRITICAL
|
|
490
|
+
if not critical and self._non_critical >= self._policy.budget:
|
|
491
|
+
return self._note_or(Admission.OVER_BUDGET)
|
|
492
|
+
return Admission.PUBLISH
|
|
493
|
+
|
|
494
|
+
def _note_or(self, otherwise: Admission) -> Admission:
|
|
495
|
+
return Admission.NOTE if len(self._noted) < self._policy.note_budget else otherwise
|
|
496
|
+
|
|
497
|
+
def admit(self, finding: Finding) -> None:
|
|
498
|
+
self._shown.append(finding)
|
|
499
|
+
if finding.tentative:
|
|
500
|
+
self._tentative += 1
|
|
501
|
+
elif finding.severity is not Severity.CRITICAL:
|
|
502
|
+
self._non_critical += 1
|
|
503
|
+
|
|
504
|
+
def note(self, finding: Finding) -> None:
|
|
505
|
+
self._noted.append(finding)
|
|
@@ -0,0 +1,85 @@
|
|
|
1
|
+
"""What becomes of findings across pushes to the same pull request.
|
|
2
|
+
|
|
3
|
+
A finding keeps its identity when the code around it shifts: it is the same issue in the same
|
|
4
|
+
file, not the same line number. A later reading that words the same issue differently, a line
|
|
5
|
+
or two away, is still the same issue. An issue already commented on is not commented on again; one
|
|
6
|
+
whose flagged line the developer changed, and which the new review no longer reports, is
|
|
7
|
+
treated as resolved.
|
|
8
|
+
"""
|
|
9
|
+
|
|
10
|
+
from __future__ import annotations
|
|
11
|
+
|
|
12
|
+
from dataclasses import dataclass
|
|
13
|
+
|
|
14
|
+
from mlai_review.domain.changes import ChangeSet
|
|
15
|
+
from mlai_review.domain.findings import Finding, fingerprint, same_issue
|
|
16
|
+
|
|
17
|
+
|
|
18
|
+
@dataclass(frozen=True, slots=True)
|
|
19
|
+
class PriorFinding:
|
|
20
|
+
"""A finding commented on the pull request by an earlier review."""
|
|
21
|
+
|
|
22
|
+
fingerprint: str
|
|
23
|
+
path: str
|
|
24
|
+
comment_id: int
|
|
25
|
+
# None when the host marks the comment outdated: the lines it sat on have changed since.
|
|
26
|
+
line: int | None
|
|
27
|
+
# Posted on the file rather than a line (the line was outside the diff): never outdated.
|
|
28
|
+
file_level: bool = False
|
|
29
|
+
# Read back from the comment, to recognise the issue retold in other words.
|
|
30
|
+
title: str = ""
|
|
31
|
+
explanation: str = ""
|
|
32
|
+
|
|
33
|
+
def describes(self, finding: Finding) -> bool:
|
|
34
|
+
"""Whether this comment is about the issue the finding reports."""
|
|
35
|
+
if self.fingerprint == fingerprint(finding):
|
|
36
|
+
return True
|
|
37
|
+
if self.line is None or not self.title:
|
|
38
|
+
return False # the comment's place is gone: only its identity can match
|
|
39
|
+
return same_issue(_Place(self.path, self.line, self.title, self.explanation), finding)
|
|
40
|
+
|
|
41
|
+
def changed_since(self, changes: ChangeSet) -> bool:
|
|
42
|
+
"""Whether the code it was about has changed. A line comment knows from the host; a
|
|
43
|
+
file comment only from an incremental review whose new commits touched its file."""
|
|
44
|
+
if self.file_level:
|
|
45
|
+
return changes.since is not None and changes.file(self.path) is not None
|
|
46
|
+
return self.line is None
|
|
47
|
+
|
|
48
|
+
|
|
49
|
+
@dataclass(frozen=True, slots=True)
|
|
50
|
+
class _Place:
|
|
51
|
+
path: str
|
|
52
|
+
line: int
|
|
53
|
+
title: str
|
|
54
|
+
explanation: str
|
|
55
|
+
category: str = ""
|
|
56
|
+
|
|
57
|
+
|
|
58
|
+
@dataclass(frozen=True, slots=True)
|
|
59
|
+
class Reconciliation:
|
|
60
|
+
new: tuple[Finding, ...] # to be commented now
|
|
61
|
+
repeated: tuple[Finding, ...] # already commented earlier, still reported
|
|
62
|
+
resolved: tuple[PriorFinding, ...] # commented earlier, lines changed, no longer reported
|
|
63
|
+
|
|
64
|
+
|
|
65
|
+
def reconcile(
|
|
66
|
+
prior: tuple[PriorFinding, ...], findings: tuple[Finding, ...], changes: ChangeSet
|
|
67
|
+
) -> Reconciliation:
|
|
68
|
+
"""Only files this review looked at can resolve an earlier finding: an incremental review
|
|
69
|
+
says nothing about files it did not touch."""
|
|
70
|
+
|
|
71
|
+
def known(f: Finding) -> bool:
|
|
72
|
+
return any(p.describes(f) for p in prior)
|
|
73
|
+
|
|
74
|
+
reviewed_paths = {c.path for c in changes.files}
|
|
75
|
+
return Reconciliation(
|
|
76
|
+
new=tuple(f for f in findings if not known(f)),
|
|
77
|
+
repeated=tuple(f for f in findings if known(f)),
|
|
78
|
+
resolved=tuple(
|
|
79
|
+
p
|
|
80
|
+
for p in prior
|
|
81
|
+
if not any(p.describes(f) for f in findings)
|
|
82
|
+
and p.path in reviewed_paths
|
|
83
|
+
and p.changed_since(changes)
|
|
84
|
+
),
|
|
85
|
+
)
|
|
@@ -0,0 +1,42 @@
|
|
|
1
|
+
"""Splitting a large change into parts that are reviewed separately.
|
|
2
|
+
|
|
3
|
+
One reading of a large change attends to a few issues and misses the rest: on the benchmark,
|
|
4
|
+
reviews whose prompt was above 15k tokens found 56% of known defects against 69% below, and
|
|
5
|
+
pull requests of 19-106 files about a third. Files that live together usually change together
|
|
6
|
+
and call each other, so a part is a run of whole directories, in path order, up to a size
|
|
7
|
+
budget; only a directory larger than the budget is cut, between its files.
|
|
8
|
+
"""
|
|
9
|
+
|
|
10
|
+
from __future__ import annotations
|
|
11
|
+
|
|
12
|
+
from pathlib import PurePosixPath
|
|
13
|
+
|
|
14
|
+
|
|
15
|
+
def partition(sizes: dict[str, int], budget: int) -> list[tuple[str, ...]]:
|
|
16
|
+
"""Paths grouped into parts of at most `budget` (by the sizes given, e.g. rendered
|
|
17
|
+
characters), keeping each directory's files together where they fit. A single file larger
|
|
18
|
+
than the budget is a part of its own."""
|
|
19
|
+
directories: dict[str, list[str]] = {}
|
|
20
|
+
for path in sorted(sizes):
|
|
21
|
+
directories.setdefault(str(PurePosixPath(path).parent), []).append(path)
|
|
22
|
+
parts: list[list[str]] = []
|
|
23
|
+
current: list[str] = []
|
|
24
|
+
used = 0
|
|
25
|
+
for files in directories.values():
|
|
26
|
+
size = sum(sizes[f] for f in files)
|
|
27
|
+
if current and used + size > budget:
|
|
28
|
+
parts.append(current)
|
|
29
|
+
current, used = [], 0
|
|
30
|
+
if size <= budget:
|
|
31
|
+
current += files
|
|
32
|
+
used += size
|
|
33
|
+
continue
|
|
34
|
+
for path in files: # a directory too large for one part: cut between its files
|
|
35
|
+
if current and used + sizes[path] > budget:
|
|
36
|
+
parts.append(current)
|
|
37
|
+
current, used = [], 0
|
|
38
|
+
current.append(path)
|
|
39
|
+
used += sizes[path]
|
|
40
|
+
if current:
|
|
41
|
+
parts.append(current)
|
|
42
|
+
return [tuple(p) for p in parts]
|