mlai-code 0.3.2 → 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 +178 -31
- 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 +20 -1
- package/src/relay.mjs +1 -1
- package/src/render.mjs +12 -3
- package/src/runtime.mjs +23 -1
- package/src/sandbox.mjs +19 -3
- package/src/server.mjs +46 -7
- package/src/session.mjs +53 -10
- package/src/shared-data.mjs +1 -1
- package/src/tui/acp.mjs +121 -19
- package/src/tui/app.mjs +24 -5
- package/src/tui/chat.mjs +43 -16
- package/src/tui/cmd-config.mjs +3 -1
- package/src/tui/cmd-core.mjs +11 -7
- package/src/tui/cmd-extensibility.mjs +13 -2
- package/src/tui/commands.mjs +2 -1
- 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/tui/statusline.mjs +2 -1
- package/src/two-words.mjs +14 -8
- package/src/view-sync.mjs +48 -0
- package/src/wakeups.mjs +2 -2
- package/src/winenv.mjs +1 -1
- package/src/workspace-api.mjs +21 -4
- package/vendor/client-sdk/index.cjs +5 -4
- package/vendor/cloud-cli/mlai.mjs +184 -28
- package/worker/architecture_worker.py +33 -4
- package/worker/browser_tool.py +19 -1
- package/worker/build_planner.py +75 -7
- package/worker/chat_fork.py +4 -1
- package/worker/checkpoints.py +71 -8
- package/worker/cloud_pty.py +91 -2
- package/worker/command_safety.py +80 -3
- package/worker/compaction_brief.py +186 -0
- package/worker/connected_turn.py +30 -0
- package/worker/context_manager.py +6 -0
- package/worker/conversation.py +139 -12
- package/worker/deck_render.js +11 -0
- package/worker/deck_tools.py +825 -0
- package/worker/git_sync.py +42 -17
- package/worker/goals.py +4 -0
- package/worker/image_generation.py +102 -23
- package/worker/kimi_bridge.py +580 -52
- package/worker/kimi_gates.py +74 -6
- package/worker/kimi_hook.py +213 -19
- package/worker/media_tools.py +182 -0
- package/worker/memory.py +2 -2
- package/worker/mlai_hooks.py +314 -5
- package/worker/mlai_mcp.py +49 -11
- 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/model_stream.py +21 -7
- package/worker/offline_guard.py +1 -1
- package/worker/preview.py +62 -4
- package/worker/product_facts.py +20 -0
- package/worker/project_context.py +33 -7
- package/worker/reply_language.py +92 -10
- package/worker/repo_policy.py +5 -0
- package/worker/roles.json +199 -37
- package/worker/roles.py +271 -9
- package/worker/runner.py +366 -53
- package/worker/runtime_control.py +64 -3
- package/worker/secret_scan.py +3 -2
- package/worker/server_memory.py +122 -0
- package/worker/site_check.py +279 -0
- package/worker/skill_library.py +15 -3
- package/worker/skill_pack.py +170 -16
- package/worker/subagents.py +6 -2
- package/worker/tool_catalog.py +4 -3
- package/worker/verification.py +156 -19
- package/worker/wakeups.py +56 -4
|
@@ -0,0 +1,438 @@
|
|
|
1
|
+
"""Reviewers turn a review context into a report. New reviewers (security, data, tests) will
|
|
2
|
+
implement `Reviewer`; the workflow does not change when they arrive."""
|
|
3
|
+
|
|
4
|
+
from __future__ import annotations
|
|
5
|
+
|
|
6
|
+
import asyncio
|
|
7
|
+
import logging
|
|
8
|
+
from abc import ABC, abstractmethod
|
|
9
|
+
from collections import Counter
|
|
10
|
+
from dataclasses import dataclass, replace
|
|
11
|
+
from typing import Any
|
|
12
|
+
|
|
13
|
+
from mlai_review.application import messages, prompts
|
|
14
|
+
from mlai_review.application.asking import ask
|
|
15
|
+
from mlai_review.application.challengers import Challenger, NoChallenger
|
|
16
|
+
from mlai_review.application.matching import IssueMatcher, NoMatcher
|
|
17
|
+
from mlai_review.application.ports import LanguageModel, Workspace
|
|
18
|
+
from mlai_review.application.streaming import CollectingSink, FindingSink, Offer
|
|
19
|
+
from mlai_review.application.tools import LookupBudget, WorkspaceTools
|
|
20
|
+
from mlai_review.application.write_check import NoWriteCheck, WriteCheck
|
|
21
|
+
from mlai_review.domain.changes import lines_in_diff
|
|
22
|
+
from mlai_review.domain.context import ReviewContext
|
|
23
|
+
from mlai_review.domain.diagnostics import findings_from
|
|
24
|
+
from mlai_review.domain.findings import (
|
|
25
|
+
MAX_FIX_LINES,
|
|
26
|
+
Admission,
|
|
27
|
+
Finding,
|
|
28
|
+
Fix,
|
|
29
|
+
PublicationGate,
|
|
30
|
+
PublicationPolicy,
|
|
31
|
+
Severity,
|
|
32
|
+
counted,
|
|
33
|
+
issues_in,
|
|
34
|
+
join,
|
|
35
|
+
maybe_same,
|
|
36
|
+
)
|
|
37
|
+
from mlai_review.domain.partition import partition
|
|
38
|
+
from mlai_review.domain.report import ReviewReport
|
|
39
|
+
|
|
40
|
+
_log = logging.getLogger("mlai.orchestrator.reviewer")
|
|
41
|
+
|
|
42
|
+
|
|
43
|
+
class Reviewer(ABC):
|
|
44
|
+
@abstractmethod
|
|
45
|
+
async def review(
|
|
46
|
+
self,
|
|
47
|
+
context: ReviewContext,
|
|
48
|
+
workspace: Workspace | None = None,
|
|
49
|
+
sink: FindingSink | None = None,
|
|
50
|
+
) -> ReviewReport:
|
|
51
|
+
"""Judge the change. With a workspace, the reviewer may look around the checkout; each
|
|
52
|
+
finding goes to the sink as soon as it is confirmed."""
|
|
53
|
+
|
|
54
|
+
|
|
55
|
+
class PlaceholderReviewer(Reviewer):
|
|
56
|
+
"""Proves the pipeline without judging the code."""
|
|
57
|
+
|
|
58
|
+
async def review(
|
|
59
|
+
self,
|
|
60
|
+
context: ReviewContext,
|
|
61
|
+
workspace: Workspace | None = None,
|
|
62
|
+
sink: FindingSink | None = None,
|
|
63
|
+
) -> ReviewReport:
|
|
64
|
+
return ReviewReport.not_reviewed(
|
|
65
|
+
files_reviewed=len(context.changes.files), files_skipped=len(context.skipped)
|
|
66
|
+
)
|
|
67
|
+
|
|
68
|
+
|
|
69
|
+
@dataclass(frozen=True, slots=True)
|
|
70
|
+
class Specialist:
|
|
71
|
+
"""One reviewing perspective: a name and the task that focuses the model on it."""
|
|
72
|
+
|
|
73
|
+
name: str
|
|
74
|
+
focus: str
|
|
75
|
+
|
|
76
|
+
|
|
77
|
+
GENERAL = Specialist("general", prompts.GENERAL)
|
|
78
|
+
SECURITY = Specialist("security", prompts.SECURITY)
|
|
79
|
+
SPECIALISTS = {s.name: s for s in (GENERAL, SECURITY)}
|
|
80
|
+
|
|
81
|
+
|
|
82
|
+
class LlmReviewer(Reviewer):
|
|
83
|
+
"""Each specialist reviews the whole change in its own conversation, all in parallel, and
|
|
84
|
+
does so `passes` times independently: one reading of a large change notices a different
|
|
85
|
+
handful of issues each time, and the union of a few readings finds far more of them. With
|
|
86
|
+
a workspace and a lookup budget, a specialist may search and read the checkout before it
|
|
87
|
+
answers; the budget bounds how far it can wander. Each claim is held to the domain's rules
|
|
88
|
+
(a finding must point at a real line).
|
|
89
|
+
|
|
90
|
+
Once every reading is back, their claims are grouped by issue (by their words, then, for
|
|
91
|
+
nearby claims the words cannot settle, by asking the matcher), each marked with how many
|
|
92
|
+
readings reported it: agreement between independent readings is the best evidence the
|
|
93
|
+
reviewer has that an issue is real. Each issue is then challenged by the refuter once, and
|
|
94
|
+
put to the publication policy as soon as its challenge is back: the developer sees the first
|
|
95
|
+
finding while the rest are still being checked.
|
|
96
|
+
|
|
97
|
+
A change too large to read well at once (over `part_chars` rendered) is split into parts
|
|
98
|
+
of related files (see domain.partition), each reviewed on its own as above, all in parallel.
|
|
99
|
+
|
|
100
|
+
A reading that fails does not sink the review: the others still publish. Only when every
|
|
101
|
+
reading fails does the review fail, with the first error.
|
|
102
|
+
"""
|
|
103
|
+
|
|
104
|
+
def __init__(
|
|
105
|
+
self,
|
|
106
|
+
model: LanguageModel,
|
|
107
|
+
policy: PublicationPolicy,
|
|
108
|
+
specialists: tuple[Specialist, ...] = (GENERAL,),
|
|
109
|
+
lookups: LookupBudget | None = None,
|
|
110
|
+
challenger: Challenger | None = None,
|
|
111
|
+
passes: int = 1,
|
|
112
|
+
matcher: IssueMatcher | None = None,
|
|
113
|
+
part_chars: int = 0,
|
|
114
|
+
write_check: WriteCheck | None = None,
|
|
115
|
+
) -> None:
|
|
116
|
+
if passes < 1:
|
|
117
|
+
raise ValueError(messages.PASSES_AT_LEAST_ONE.format(passes=passes))
|
|
118
|
+
self._model = model
|
|
119
|
+
self._policy = policy
|
|
120
|
+
self._specialists = specialists
|
|
121
|
+
self._lookups = lookups
|
|
122
|
+
self._challenger = challenger or NoChallenger()
|
|
123
|
+
self._passes = passes
|
|
124
|
+
self._matcher = matcher or NoMatcher()
|
|
125
|
+
# Above this many characters of rendered change, the change is reviewed in parts; 0
|
|
126
|
+
# reviews it whole however large.
|
|
127
|
+
self._part_chars = part_chars
|
|
128
|
+
self._write_check = write_check or NoWriteCheck()
|
|
129
|
+
|
|
130
|
+
async def review(
|
|
131
|
+
self,
|
|
132
|
+
context: ReviewContext,
|
|
133
|
+
workspace: Workspace | None = None,
|
|
134
|
+
sink: FindingSink | None = None,
|
|
135
|
+
) -> ReviewReport:
|
|
136
|
+
run = _Run(self._policy.gate(), sink or CollectingSink())
|
|
137
|
+
# Tool errors on changed lines are established facts: they go out first, except untidiness
|
|
138
|
+
# (low severity), which waits for the readings so it never takes a defect's place.
|
|
139
|
+
tool_findings = findings_from(context.diagnostics, context.changes)
|
|
140
|
+
for finding in tool_findings:
|
|
141
|
+
if finding.severity is not Severity.LOW:
|
|
142
|
+
await run.offer(finding)
|
|
143
|
+
tools = (
|
|
144
|
+
WorkspaceTools(workspace, self._lookups)
|
|
145
|
+
if workspace is not None and self._lookups is not None
|
|
146
|
+
else None
|
|
147
|
+
)
|
|
148
|
+
parts = self._parts(context)
|
|
149
|
+
if len(parts) > 1:
|
|
150
|
+
_log.info(messages.PARTS_LOG, len(parts), [len(p.changes.files) for p in parts])
|
|
151
|
+
outcomes = await asyncio.gather(
|
|
152
|
+
*(self._review_part(part, tools, workspace, run) for part in parts),
|
|
153
|
+
return_exceptions=True,
|
|
154
|
+
)
|
|
155
|
+
failures = [o for o in outcomes if isinstance(o, BaseException)]
|
|
156
|
+
if len(failures) == len(parts):
|
|
157
|
+
raise failures[0]
|
|
158
|
+
for failure in failures:
|
|
159
|
+
_log.warning(messages.PART_FAILED, failure)
|
|
160
|
+
for finding in tool_findings:
|
|
161
|
+
if finding.severity is Severity.LOW:
|
|
162
|
+
await run.offer(finding)
|
|
163
|
+
_log.info(
|
|
164
|
+
messages.REVIEW_OUTCOME,
|
|
165
|
+
run.claims,
|
|
166
|
+
run.rejected,
|
|
167
|
+
run.merged,
|
|
168
|
+
run.tally[Admission.REFUTED],
|
|
169
|
+
len(run.posted),
|
|
170
|
+
run.suppressed,
|
|
171
|
+
)
|
|
172
|
+
report = ReviewReport.of(
|
|
173
|
+
tuple(run.posted),
|
|
174
|
+
files_reviewed=len(context.changes.files),
|
|
175
|
+
suppressed=run.suppressed,
|
|
176
|
+
refuted=run.tally[Admission.REFUTED],
|
|
177
|
+
diff_only=not context.contents_available,
|
|
178
|
+
files_skipped=len(context.skipped),
|
|
179
|
+
since_commit=context.changes.since,
|
|
180
|
+
)
|
|
181
|
+
noted = sorted(run.noted, key=lambda f: (f.severity.rank, f.votes), reverse=True)
|
|
182
|
+
return replace(
|
|
183
|
+
report,
|
|
184
|
+
repeated=run.outcomes[Offer.REPEATED],
|
|
185
|
+
dismissed=run.outcomes[Offer.DISMISSED],
|
|
186
|
+
noted=tuple(noted),
|
|
187
|
+
below_minimum=run.outcomes[Offer.HELD],
|
|
188
|
+
)
|
|
189
|
+
|
|
190
|
+
def _parts(self, context: ReviewContext) -> list[ReviewContext]:
|
|
191
|
+
"""The change as one part, or, when its rendering is over the budget, as parts of
|
|
192
|
+
related files reviewed separately."""
|
|
193
|
+
if self._part_chars <= 0 or len(prompts.user_message(context)) <= self._part_chars:
|
|
194
|
+
return [context]
|
|
195
|
+
sizes = {
|
|
196
|
+
f.path: len("\n".join(prompts.file_section(context, f))) for f in context.changes.files
|
|
197
|
+
}
|
|
198
|
+
groups = partition(sizes, self._part_chars)
|
|
199
|
+
if len(groups) == 1:
|
|
200
|
+
return [context] # every file fits in one part: nothing to gain by narrowing
|
|
201
|
+
return [context.part(paths) for paths in groups]
|
|
202
|
+
|
|
203
|
+
async def _review_part(
|
|
204
|
+
self,
|
|
205
|
+
part: ReviewContext,
|
|
206
|
+
tools: WorkspaceTools | None,
|
|
207
|
+
workspace: Workspace | None,
|
|
208
|
+
run: _Run,
|
|
209
|
+
) -> None:
|
|
210
|
+
"""Every reading of one part, its issues grouped and each challenged and offered."""
|
|
211
|
+
user = prompts.user_message(part)
|
|
212
|
+
readings = [(s, n) for n in range(1, self._passes + 1) for s in self._specialists]
|
|
213
|
+
outcomes, (writes,) = await asyncio.gather(
|
|
214
|
+
asyncio.gather(
|
|
215
|
+
*(self._read(s, n, user, tools, part, run) for s, n in readings),
|
|
216
|
+
return_exceptions=True,
|
|
217
|
+
),
|
|
218
|
+
asyncio.gather(self._check_writes(part, run), return_exceptions=True),
|
|
219
|
+
)
|
|
220
|
+
failures = [o for o in outcomes if isinstance(o, BaseException)]
|
|
221
|
+
for (specialist, n), outcome in zip(readings, outcomes, strict=True):
|
|
222
|
+
if isinstance(outcome, BaseException):
|
|
223
|
+
_log.warning(messages.SPECIALIST_FAILED, _who(specialist, n), outcome)
|
|
224
|
+
if len(failures) == len(readings):
|
|
225
|
+
raise failures[0]
|
|
226
|
+
answered = [o for o in outcomes if not isinstance(o, BaseException)]
|
|
227
|
+
# The write check reads the change once more, looking only at its writes: what it
|
|
228
|
+
# reports counts like one more reading's claims.
|
|
229
|
+
if isinstance(writes, BaseException):
|
|
230
|
+
_log.warning(messages.WRITE_CHECK_FAILED, writes)
|
|
231
|
+
elif writes is not None:
|
|
232
|
+
answered.append(writes)
|
|
233
|
+
issues = await self._group(answered)
|
|
234
|
+
run.merged += sum(len(a) for a in answered) - len(issues)
|
|
235
|
+
votes = Counter(f.votes for f in issues)
|
|
236
|
+
_log.info(
|
|
237
|
+
messages.AGREEMENT_LOG,
|
|
238
|
+
len(answered),
|
|
239
|
+
" ".join(f"{v}:{votes[v]}" for v in sorted(votes, reverse=True)) or "-",
|
|
240
|
+
)
|
|
241
|
+
|
|
242
|
+
async def check(finding: Finding) -> None:
|
|
243
|
+
await run.offer(await self._challenger.one(part, finding, workspace))
|
|
244
|
+
|
|
245
|
+
await asyncio.gather(*(check(f) for f in issues))
|
|
246
|
+
|
|
247
|
+
async def _group(self, answered: list[list[Finding]]) -> list[Finding]:
|
|
248
|
+
"""One finding per issue, marked with how many readings reported it."""
|
|
249
|
+
issues = issues_in(answered)
|
|
250
|
+
pairs = maybe_same(issues)
|
|
251
|
+
verdicts = await asyncio.gather(
|
|
252
|
+
*(self._matcher.same(issues[i].finding, issues[j].finding) for i, j in pairs)
|
|
253
|
+
)
|
|
254
|
+
same = [pair for pair, verdict in zip(pairs, verdicts, strict=True) if verdict]
|
|
255
|
+
if pairs:
|
|
256
|
+
_log.info(messages.MATCHED_LOG, len(pairs), len(same))
|
|
257
|
+
return counted(join(issues, same), len(answered))
|
|
258
|
+
|
|
259
|
+
async def _read(
|
|
260
|
+
self,
|
|
261
|
+
specialist: Specialist,
|
|
262
|
+
reading: int,
|
|
263
|
+
user: str,
|
|
264
|
+
tools: WorkspaceTools | None,
|
|
265
|
+
context: ReviewContext,
|
|
266
|
+
run: _Run,
|
|
267
|
+
) -> list[Finding]:
|
|
268
|
+
"""One independent reading: the claims that point at real lines."""
|
|
269
|
+
answer = await self._answer(specialist, reading, user, tools)
|
|
270
|
+
return self._claimed(answer, context, run)
|
|
271
|
+
|
|
272
|
+
async def _check_writes(self, context: ReviewContext, run: _Run) -> list[Finding] | None:
|
|
273
|
+
"""The write check's claims that point at real lines; None when it had nothing to
|
|
274
|
+
check."""
|
|
275
|
+
answer = await self._write_check.answer(context)
|
|
276
|
+
return None if answer is None else self._claimed(answer, context, run)
|
|
277
|
+
|
|
278
|
+
def _claimed(self, answer: dict[str, Any], context: ReviewContext, run: _Run) -> list[Finding]:
|
|
279
|
+
claims = [c for raw in _findings_in(answer) if (c := _Claim.parse(raw)) is not None]
|
|
280
|
+
findings = [f for c in claims if (f := c.to_finding(context)) is not None]
|
|
281
|
+
run.claims += len(claims)
|
|
282
|
+
run.rejected += len(claims) - len(findings)
|
|
283
|
+
return findings
|
|
284
|
+
|
|
285
|
+
async def _answer(
|
|
286
|
+
self, specialist: Specialist, reading: int, user: str, tools: WorkspaceTools | None
|
|
287
|
+
) -> dict[str, Any]:
|
|
288
|
+
return await ask(
|
|
289
|
+
self._model,
|
|
290
|
+
system=prompts.SHARED,
|
|
291
|
+
user=user + prompts.task(specialist.focus),
|
|
292
|
+
tools=tools,
|
|
293
|
+
budget=self._lookups,
|
|
294
|
+
who=_who(specialist, reading),
|
|
295
|
+
)
|
|
296
|
+
|
|
297
|
+
|
|
298
|
+
def _who(specialist: Specialist, reading: int) -> str:
|
|
299
|
+
return specialist.name if reading == 1 else f"{specialist.name}#{reading}"
|
|
300
|
+
|
|
301
|
+
|
|
302
|
+
class _Run:
|
|
303
|
+
"""One review's progress: the gate decides, the sink publishes, one finding at a time so
|
|
304
|
+
duplicates and the budget are judged against what is really shown."""
|
|
305
|
+
|
|
306
|
+
def __init__(self, gate: PublicationGate, sink: FindingSink) -> None:
|
|
307
|
+
self._gate = gate
|
|
308
|
+
self._sink = sink
|
|
309
|
+
self._lock = asyncio.Lock()
|
|
310
|
+
self.tally: Counter[Admission] = Counter()
|
|
311
|
+
self.outcomes: Counter[Offer] = Counter()
|
|
312
|
+
self.posted: list[Finding] = []
|
|
313
|
+
self.noted: list[Finding] = []
|
|
314
|
+
self.claims = 0
|
|
315
|
+
self.rejected = 0
|
|
316
|
+
self.merged = 0
|
|
317
|
+
|
|
318
|
+
@property
|
|
319
|
+
def suppressed(self) -> int:
|
|
320
|
+
return sum(
|
|
321
|
+
self.tally[a] for a in (Admission.BELOW_BAR, Admission.DUPLICATE, Admission.OVER_BUDGET)
|
|
322
|
+
)
|
|
323
|
+
|
|
324
|
+
async def offer(self, finding: Finding) -> None:
|
|
325
|
+
async with self._lock:
|
|
326
|
+
admission = self._gate.decide(finding)
|
|
327
|
+
self.tally[admission] += 1
|
|
328
|
+
if admission is Admission.NOTE:
|
|
329
|
+
outcome = await self._sink.note(finding)
|
|
330
|
+
self.outcomes[outcome] += 1
|
|
331
|
+
if outcome is Offer.NOTED:
|
|
332
|
+
self._gate.note(finding)
|
|
333
|
+
self.noted.append(finding)
|
|
334
|
+
return
|
|
335
|
+
if admission is Admission.PUBLISH_TENTATIVE:
|
|
336
|
+
finding = replace(finding, tentative=True)
|
|
337
|
+
elif admission is not Admission.PUBLISH:
|
|
338
|
+
return
|
|
339
|
+
outcome = await self._sink.offer(finding)
|
|
340
|
+
self.outcomes[outcome] += 1
|
|
341
|
+
if outcome is not Offer.DISMISSED:
|
|
342
|
+
self._gate.admit(finding)
|
|
343
|
+
if outcome is Offer.POSTED:
|
|
344
|
+
self.posted.append(finding)
|
|
345
|
+
elif outcome is Offer.HELD:
|
|
346
|
+
self.noted.append(finding)
|
|
347
|
+
|
|
348
|
+
|
|
349
|
+
@dataclass(frozen=True, slots=True)
|
|
350
|
+
class _Claim:
|
|
351
|
+
"""One finding as the model stated it, typed but not yet checked against the code."""
|
|
352
|
+
|
|
353
|
+
title: str
|
|
354
|
+
severity: Severity
|
|
355
|
+
category: str
|
|
356
|
+
path: str
|
|
357
|
+
line: int
|
|
358
|
+
explanation: str
|
|
359
|
+
suggestion: str
|
|
360
|
+
confidence: float
|
|
361
|
+
fix_lines: tuple[int, int] | None = None
|
|
362
|
+
fix_code: str = ""
|
|
363
|
+
|
|
364
|
+
@classmethod
|
|
365
|
+
def parse(cls, raw: Any) -> _Claim | None:
|
|
366
|
+
"""Map the model's JSON (shaped by prompts.SHARED) to typed values; None if unusable."""
|
|
367
|
+
if not isinstance(raw, dict):
|
|
368
|
+
return None
|
|
369
|
+
try:
|
|
370
|
+
claim = cls(
|
|
371
|
+
title=str(raw["title"]).strip(),
|
|
372
|
+
severity=Severity(str(raw["severity"]).lower()),
|
|
373
|
+
category=str(raw.get("category", "other")),
|
|
374
|
+
path=str(raw["path"]).strip(),
|
|
375
|
+
line=int(raw["line"]),
|
|
376
|
+
explanation=str(raw.get("explanation", "")).strip(),
|
|
377
|
+
suggestion=str(raw.get("suggestion", "")).strip(),
|
|
378
|
+
confidence=min(max(float(raw.get("confidence", 0.0)), 0.0), 1.0),
|
|
379
|
+
**_fix_fields(raw.get("fix")),
|
|
380
|
+
)
|
|
381
|
+
except (KeyError, TypeError, ValueError):
|
|
382
|
+
return None
|
|
383
|
+
return claim if claim.title else None
|
|
384
|
+
|
|
385
|
+
def fix(self, context: ReviewContext) -> Fix | None:
|
|
386
|
+
"""The claimed fix, if it names real lines of the file; otherwise nothing."""
|
|
387
|
+
if self.fix_lines is None or not self.fix_code.strip():
|
|
388
|
+
return None
|
|
389
|
+
start, end = self.fix_lines
|
|
390
|
+
if start > end or end - start + 1 > MAX_FIX_LINES:
|
|
391
|
+
return None
|
|
392
|
+
ends = (context.locate(self.path, start), context.locate(self.path, end))
|
|
393
|
+
if any(e is None for e in ends):
|
|
394
|
+
return None
|
|
395
|
+
change = context.changes.file(self.path)
|
|
396
|
+
shown = lines_in_diff(change.patch if change else None)
|
|
397
|
+
view = context.views.get(self.path)
|
|
398
|
+
text = view.text.splitlines() if view else []
|
|
399
|
+
return Fix(
|
|
400
|
+
start=start,
|
|
401
|
+
end=end,
|
|
402
|
+
code=self.fix_code.rstrip("\n"),
|
|
403
|
+
replaced=tuple(text[start - 1 : end]) if text else (),
|
|
404
|
+
suggestable=all(n in shown for n in range(start, end + 1)),
|
|
405
|
+
)
|
|
406
|
+
|
|
407
|
+
def to_finding(self, context: ReviewContext) -> Finding | None:
|
|
408
|
+
location = context.locate(self.path, self.line)
|
|
409
|
+
if location is None:
|
|
410
|
+
return None
|
|
411
|
+
return Finding(
|
|
412
|
+
title=self.title,
|
|
413
|
+
severity=self.severity,
|
|
414
|
+
category=self.category,
|
|
415
|
+
path=location.path,
|
|
416
|
+
line=location.line,
|
|
417
|
+
explanation=self.explanation,
|
|
418
|
+
suggestion=self.suggestion,
|
|
419
|
+
confidence=self.confidence,
|
|
420
|
+
in_diff=location.in_diff,
|
|
421
|
+
fix=self.fix(context),
|
|
422
|
+
)
|
|
423
|
+
|
|
424
|
+
|
|
425
|
+
def _fix_fields(raw: Any) -> dict[str, Any]:
|
|
426
|
+
"""The fix as the model wrote it; a malformed one is dropped, not the whole finding."""
|
|
427
|
+
if not isinstance(raw, dict):
|
|
428
|
+
return {}
|
|
429
|
+
try:
|
|
430
|
+
first, last = (int(n) for n in raw["lines"])
|
|
431
|
+
return {"fix_lines": (first, last), "fix_code": str(raw.get("code", ""))}
|
|
432
|
+
except (KeyError, TypeError, ValueError):
|
|
433
|
+
return {}
|
|
434
|
+
|
|
435
|
+
|
|
436
|
+
def _findings_in(answer: dict[str, Any]) -> list[Any]:
|
|
437
|
+
findings = answer.get("findings")
|
|
438
|
+
return findings if isinstance(findings, list) else []
|
|
@@ -0,0 +1,91 @@
|
|
|
1
|
+
"""Where a review's findings go as they are confirmed, one at a time, so the developer sees
|
|
2
|
+
each one as soon as it is ready rather than all of them at the end."""
|
|
3
|
+
|
|
4
|
+
from __future__ import annotations
|
|
5
|
+
|
|
6
|
+
from abc import ABC, abstractmethod
|
|
7
|
+
from mlai_review._compat import StrEnum
|
|
8
|
+
|
|
9
|
+
from mlai_review.contracts.review import ReviewPullRequestInput
|
|
10
|
+
from mlai_review.application.ports import ReviewPublisher
|
|
11
|
+
from mlai_review.domain.feedback import Dismissal
|
|
12
|
+
from mlai_review.domain.findings import Finding, Severity, fingerprint
|
|
13
|
+
from mlai_review.domain.lifecycle import PriorFinding
|
|
14
|
+
|
|
15
|
+
|
|
16
|
+
class Offer(StrEnum):
|
|
17
|
+
POSTED = "posted"
|
|
18
|
+
NOTED = "noted" # listed in the summary, not commented on
|
|
19
|
+
REPEATED = "repeated" # already commented on this pull request: left as it is
|
|
20
|
+
DISMISSED = "dismissed" # the team marked the same issue as not useful before
|
|
21
|
+
HELD = "held" # below the repository's minimum severity: listed in the summary only
|
|
22
|
+
|
|
23
|
+
|
|
24
|
+
class FindingSink(ABC):
|
|
25
|
+
@abstractmethod
|
|
26
|
+
async def offer(self, finding: Finding) -> Offer:
|
|
27
|
+
"""Take a finding the policy let through; say what became of it."""
|
|
28
|
+
|
|
29
|
+
async def note(self, finding: Finding) -> Offer:
|
|
30
|
+
"""Take a finding for the summary list: never commented on, so nothing is posted."""
|
|
31
|
+
return Offer.NOTED
|
|
32
|
+
|
|
33
|
+
|
|
34
|
+
class CollectingSink(FindingSink):
|
|
35
|
+
"""Keeps findings in memory: offline evaluation and tests."""
|
|
36
|
+
|
|
37
|
+
def __init__(self) -> None:
|
|
38
|
+
self.findings: list[Finding] = []
|
|
39
|
+
|
|
40
|
+
async def offer(self, finding: Finding) -> Offer:
|
|
41
|
+
self.findings.append(finding)
|
|
42
|
+
return Offer.POSTED
|
|
43
|
+
|
|
44
|
+
|
|
45
|
+
class PublishingSink(FindingSink):
|
|
46
|
+
"""Comments on the pull request as each finding arrives. An issue already commented on
|
|
47
|
+
(by an earlier review, or by an earlier attempt of this one) is not commented again, and
|
|
48
|
+
one the team dismissed is not raised at all."""
|
|
49
|
+
|
|
50
|
+
def __init__(
|
|
51
|
+
self,
|
|
52
|
+
publisher: ReviewPublisher,
|
|
53
|
+
target: ReviewPullRequestInput,
|
|
54
|
+
prior: tuple[PriorFinding, ...],
|
|
55
|
+
dismissed: tuple[Dismissal, ...],
|
|
56
|
+
min_severity: Severity = Severity.LOW,
|
|
57
|
+
) -> None:
|
|
58
|
+
self._min_rank = min_severity.rank
|
|
59
|
+
self._publisher = publisher
|
|
60
|
+
self._target = target
|
|
61
|
+
self._prior = prior
|
|
62
|
+
self._commented: set[str] = set() # by this review
|
|
63
|
+
self._dismissed = {d.fingerprint for d in dismissed}
|
|
64
|
+
# Everything the policy let through, whatever became of it: the issues this review
|
|
65
|
+
# still sees, which decides what earlier comments look resolved.
|
|
66
|
+
self.offered: list[Finding] = []
|
|
67
|
+
|
|
68
|
+
async def offer(self, finding: Finding) -> Offer:
|
|
69
|
+
self.offered.append(finding)
|
|
70
|
+
seen = self._seen(finding)
|
|
71
|
+
if seen is not None:
|
|
72
|
+
return seen
|
|
73
|
+
if finding.severity.rank < self._min_rank:
|
|
74
|
+
return Offer.HELD
|
|
75
|
+
await self._publisher.post_finding(self._target, finding)
|
|
76
|
+
self._commented.add(fingerprint(finding))
|
|
77
|
+
return Offer.POSTED
|
|
78
|
+
|
|
79
|
+
async def note(self, finding: Finding) -> Offer:
|
|
80
|
+
"""A noted issue still counts as seen (an earlier comment on it is not resolved), but
|
|
81
|
+
one already commented on or dismissed is not listed again."""
|
|
82
|
+
self.offered.append(finding)
|
|
83
|
+
return self._seen(finding) or Offer.NOTED
|
|
84
|
+
|
|
85
|
+
def _seen(self, finding: Finding) -> Offer | None:
|
|
86
|
+
key = fingerprint(finding)
|
|
87
|
+
if key in self._dismissed:
|
|
88
|
+
return Offer.DISMISSED
|
|
89
|
+
if key in self._commented or any(p.describes(finding) for p in self._prior):
|
|
90
|
+
return Offer.REPEATED
|
|
91
|
+
return None
|
|
@@ -0,0 +1,138 @@
|
|
|
1
|
+
"""What a reviewer may look up in the checkout while reviewing, and how much.
|
|
2
|
+
|
|
3
|
+
The model sees three read-only tools over the workspace. Every result is capped, every error
|
|
4
|
+
comes back as text the model can act on, and nothing here can write or run anything.
|
|
5
|
+
"""
|
|
6
|
+
|
|
7
|
+
from __future__ import annotations
|
|
8
|
+
|
|
9
|
+
import re
|
|
10
|
+
from dataclasses import dataclass
|
|
11
|
+
from typing import Any
|
|
12
|
+
|
|
13
|
+
from mlai_review.application import messages
|
|
14
|
+
from mlai_review.application.errors import ForbiddenPath
|
|
15
|
+
from mlai_review.application.ports import ToolCall, ToolSpec, Workspace
|
|
16
|
+
|
|
17
|
+
|
|
18
|
+
@dataclass(frozen=True, slots=True)
|
|
19
|
+
class LookupBudget:
|
|
20
|
+
"""How much one specialist may look around before it must answer."""
|
|
21
|
+
|
|
22
|
+
max_calls: int = 8
|
|
23
|
+
max_result_chars: int = 40_000
|
|
24
|
+
max_read_lines: int = 100
|
|
25
|
+
max_matches: int = 50
|
|
26
|
+
|
|
27
|
+
|
|
28
|
+
GREP = ToolSpec(
|
|
29
|
+
name="grep",
|
|
30
|
+
description=(
|
|
31
|
+
"Search the repository for lines matching a regular expression (Python syntax). "
|
|
32
|
+
"Returns up to a fixed number of matches as path:line: text. Use a specific pattern "
|
|
33
|
+
"such as a function or class name; narrow with glob when there are too many matches."
|
|
34
|
+
),
|
|
35
|
+
parameters={
|
|
36
|
+
"type": "object",
|
|
37
|
+
"properties": {
|
|
38
|
+
"pattern": {"type": "string", "description": "Regular expression to search for."},
|
|
39
|
+
"glob": {
|
|
40
|
+
"type": "string",
|
|
41
|
+
"description": "Optional path glob to restrict the search, e.g. src/api/*.py",
|
|
42
|
+
},
|
|
43
|
+
},
|
|
44
|
+
"required": ["pattern"],
|
|
45
|
+
},
|
|
46
|
+
)
|
|
47
|
+
READ = ToolSpec(
|
|
48
|
+
name="read",
|
|
49
|
+
description=(
|
|
50
|
+
"Read a range of lines from a file in the repository, numbered. At most a fixed number "
|
|
51
|
+
"of lines per call; ask for another range to see more."
|
|
52
|
+
),
|
|
53
|
+
parameters={
|
|
54
|
+
"type": "object",
|
|
55
|
+
"properties": {
|
|
56
|
+
"path": {"type": "string", "description": "Repository-relative file path."},
|
|
57
|
+
"start": {"type": "integer", "description": "First line to read (1-based)."},
|
|
58
|
+
"end": {"type": "integer", "description": "Last line to read, inclusive."},
|
|
59
|
+
},
|
|
60
|
+
"required": ["path", "start", "end"],
|
|
61
|
+
},
|
|
62
|
+
)
|
|
63
|
+
LIST_DIR = ToolSpec(
|
|
64
|
+
name="list_dir",
|
|
65
|
+
description="List the entries of a directory in the repository (directories end with /).",
|
|
66
|
+
parameters={
|
|
67
|
+
"type": "object",
|
|
68
|
+
"properties": {
|
|
69
|
+
"path": {
|
|
70
|
+
"type": "string",
|
|
71
|
+
"description": "Repository-relative directory; empty for root.",
|
|
72
|
+
}
|
|
73
|
+
},
|
|
74
|
+
"required": [],
|
|
75
|
+
},
|
|
76
|
+
)
|
|
77
|
+
|
|
78
|
+
|
|
79
|
+
class WorkspaceTools:
|
|
80
|
+
specs: tuple[ToolSpec, ...] = (GREP, READ, LIST_DIR)
|
|
81
|
+
|
|
82
|
+
def __init__(self, workspace: Workspace, budget: LookupBudget) -> None:
|
|
83
|
+
self._workspace = workspace
|
|
84
|
+
self._budget = budget
|
|
85
|
+
|
|
86
|
+
async def call(self, call: ToolCall) -> str:
|
|
87
|
+
"""The tool's result as text. Mistakes are answered, not raised: the model reads the
|
|
88
|
+
error and tries again or moves on."""
|
|
89
|
+
try:
|
|
90
|
+
match call.name:
|
|
91
|
+
case "grep":
|
|
92
|
+
return await self._grep(call.arguments)
|
|
93
|
+
case "read":
|
|
94
|
+
return await self._read(call.arguments)
|
|
95
|
+
case "list_dir":
|
|
96
|
+
return await self._list_dir(call.arguments)
|
|
97
|
+
return messages.TOOL_UNKNOWN.format(name=call.name)
|
|
98
|
+
except ForbiddenPath as exc:
|
|
99
|
+
return messages.TOOL_REFUSED.format(reason=exc)
|
|
100
|
+
except (KeyError, TypeError, ValueError) as exc:
|
|
101
|
+
return messages.TOOL_BAD_ARGUMENTS.format(name=call.name, reason=exc)
|
|
102
|
+
|
|
103
|
+
async def _grep(self, arguments: dict[str, Any]) -> str:
|
|
104
|
+
pattern = str(arguments["pattern"])
|
|
105
|
+
glob = arguments.get("glob")
|
|
106
|
+
try:
|
|
107
|
+
re.compile(pattern)
|
|
108
|
+
except re.error as exc:
|
|
109
|
+
return messages.TOOL_BAD_PATTERN.format(reason=exc)
|
|
110
|
+
matches = await self._workspace.grep(
|
|
111
|
+
pattern, glob=str(glob) if glob else None, limit=self._budget.max_matches
|
|
112
|
+
)
|
|
113
|
+
if not matches:
|
|
114
|
+
return messages.TOOL_NO_MATCHES
|
|
115
|
+
lines = [f"{m.path}:{m.line}: {m.text}" for m in matches]
|
|
116
|
+
if len(matches) >= self._budget.max_matches:
|
|
117
|
+
lines.append(messages.TOOL_MATCHES_CAPPED.format(limit=self._budget.max_matches))
|
|
118
|
+
return "\n".join(lines)
|
|
119
|
+
|
|
120
|
+
async def _read(self, arguments: dict[str, Any]) -> str:
|
|
121
|
+
path = str(arguments["path"])
|
|
122
|
+
start = max(int(arguments["start"]), 1)
|
|
123
|
+
end = max(int(arguments["end"]), start)
|
|
124
|
+
text = await self._workspace.read(path)
|
|
125
|
+
if text is None:
|
|
126
|
+
return messages.TOOL_FILE_UNREADABLE.format(path=path)
|
|
127
|
+
lines = text.splitlines()
|
|
128
|
+
if start > len(lines):
|
|
129
|
+
return messages.TOOL_PAST_END.format(path=path, total=len(lines))
|
|
130
|
+
end = min(end, len(lines), start + self._budget.max_read_lines - 1)
|
|
131
|
+
shown = [f"{n:>5}| {lines[n - 1]}" for n in range(start, end + 1)]
|
|
132
|
+
shown.append(messages.TOOL_READ_RANGE.format(start=start, end=end, total=len(lines)))
|
|
133
|
+
return "\n".join(shown)
|
|
134
|
+
|
|
135
|
+
async def _list_dir(self, arguments: dict[str, Any]) -> str:
|
|
136
|
+
path = str(arguments.get("path") or "")
|
|
137
|
+
entries = await self._workspace.list_dir(path)
|
|
138
|
+
return "\n".join(entries) if entries else messages.TOOL_EMPTY_DIR.format(path=path or "/")
|