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,31 @@
|
|
|
1
|
+
"""What every GitHub call shares: headers, and how failures map to retryable or permanent."""
|
|
2
|
+
|
|
3
|
+
from __future__ import annotations
|
|
4
|
+
|
|
5
|
+
import httpx
|
|
6
|
+
|
|
7
|
+
from mlai_review.application.errors import AccessDenied, PermanentReviewError, ReviewError
|
|
8
|
+
from mlai_review.infrastructure import messages
|
|
9
|
+
|
|
10
|
+
API_VERSION = "2022-11-28"
|
|
11
|
+
|
|
12
|
+
|
|
13
|
+
def github_headers() -> dict[str, str]:
|
|
14
|
+
return {"Accept": "application/vnd.github+json", "X-GitHub-Api-Version": API_VERSION}
|
|
15
|
+
|
|
16
|
+
|
|
17
|
+
def raise_for_github_status(response: httpx.Response, operation: str) -> None:
|
|
18
|
+
"""Server errors and rate limits may pass on retry; anything else (missing permission, PR or
|
|
19
|
+
repository gone, invalid request) will not."""
|
|
20
|
+
if response.is_success:
|
|
21
|
+
return
|
|
22
|
+
status = response.status_code
|
|
23
|
+
detail = messages.GITHUB_CALL_FAILED.format(operation=operation, status=status)
|
|
24
|
+
retryable = (
|
|
25
|
+
status >= 500
|
|
26
|
+
or status == 429
|
|
27
|
+
or (status == 403 and response.headers.get("x-ratelimit-remaining") == "0")
|
|
28
|
+
)
|
|
29
|
+
if retryable:
|
|
30
|
+
raise ReviewError(detail)
|
|
31
|
+
raise (AccessDenied if status in (401, 403, 404) else PermanentReviewError)(detail)
|
|
@@ -0,0 +1,398 @@
|
|
|
1
|
+
"""How a review looks on GitHub: check-run titles and summaries, inline comments, and the
|
|
2
|
+
mapping from our verdicts to GitHub's check conclusions. The domain knows none of this."""
|
|
3
|
+
|
|
4
|
+
from __future__ import annotations
|
|
5
|
+
|
|
6
|
+
import re
|
|
7
|
+
|
|
8
|
+
from mlai_review.contracts.review import CHECK_NAME as CONTRACT_CHECK_NAME
|
|
9
|
+
from mlai_review.domain.findings import Finding, Fix, Severity, fingerprint
|
|
10
|
+
from mlai_review.domain.report import ReviewReport, Verdict
|
|
11
|
+
|
|
12
|
+
CHECK_NAME = CONTRACT_CHECK_NAME
|
|
13
|
+
IN_PROGRESS_TITLE = "Reviewing"
|
|
14
|
+
IN_PROGRESS_SUMMARY = "ML AI is reviewing this change."
|
|
15
|
+
MAX_SUMMARY_CHARS = 65_000 # GitHub's limit on a check-run summary.
|
|
16
|
+
INCOMPLETE_TITLE = "ML AI could not complete this review"
|
|
17
|
+
TENTATIVE_NOTE = (
|
|
18
|
+
"_ML AI is not sure about this one: it may be fine. A thumbs up or thumbs down reaction tells it whether such "
|
|
19
|
+
"comments help._"
|
|
20
|
+
)
|
|
21
|
+
_TENTATIVE_MARKER = "<!-- mlai-tentative -->"
|
|
22
|
+
RESOLVED_REPLY_PREFIX = "This looks resolved in "
|
|
23
|
+
|
|
24
|
+
# Every verdict is neutral until merge-gating is decided: a check that could fail a merge must
|
|
25
|
+
# be backed by verified findings, which do not exist yet.
|
|
26
|
+
_CONCLUSION = {
|
|
27
|
+
Verdict.ISSUES_FOUND: "neutral",
|
|
28
|
+
Verdict.NO_ISSUES: "neutral",
|
|
29
|
+
Verdict.NOT_REVIEWED: "neutral",
|
|
30
|
+
Verdict.TOO_LARGE: "neutral",
|
|
31
|
+
Verdict.INCOMPLETE: "neutral",
|
|
32
|
+
}
|
|
33
|
+
OVERVIEW_MARKER = "<!-- mlai-overview -->"
|
|
34
|
+
MAX_NOTE_CHARS = 220
|
|
35
|
+
_FINDING_PREFIX = "<!-- mlai-finding:"
|
|
36
|
+
|
|
37
|
+
|
|
38
|
+
# Conclusions that mean the commit's code was actually judged.
|
|
39
|
+
REVIEWED = frozenset({_CONCLUSION[Verdict.ISSUES_FOUND], _CONCLUSION[Verdict.NO_ISSUES]})
|
|
40
|
+
|
|
41
|
+
|
|
42
|
+
MAX_STATUS_CHARS = 140 # GitHub's limit on a commit status description.
|
|
43
|
+
|
|
44
|
+
|
|
45
|
+
def status_state(report: ReviewReport) -> str:
|
|
46
|
+
"""A commit status has no neutral: a judged or skipped change is success, never failure,
|
|
47
|
+
until merge-gating is decided; a review that broke is an error."""
|
|
48
|
+
return "error" if report.verdict is Verdict.INCOMPLETE else "success"
|
|
49
|
+
|
|
50
|
+
|
|
51
|
+
def status_reviewed(state: str, description: str) -> bool:
|
|
52
|
+
"""Whether a status of ours says the commit's code was actually judged."""
|
|
53
|
+
return state == "success" and (
|
|
54
|
+
description == "No issues found" or description.endswith(("issue found", "issues found"))
|
|
55
|
+
)
|
|
56
|
+
|
|
57
|
+
|
|
58
|
+
def conclusion(report: ReviewReport) -> str:
|
|
59
|
+
return _CONCLUSION[report.verdict]
|
|
60
|
+
|
|
61
|
+
|
|
62
|
+
def title(report: ReviewReport) -> str:
|
|
63
|
+
match report.verdict:
|
|
64
|
+
case Verdict.ISSUES_FOUND:
|
|
65
|
+
count = len(report.findings)
|
|
66
|
+
return f"{count} issue{'' if count == 1 else 's'} found"
|
|
67
|
+
case Verdict.NO_ISSUES:
|
|
68
|
+
return "No issues found"
|
|
69
|
+
case Verdict.NOT_REVIEWED:
|
|
70
|
+
return f"Pipeline check: {_files(report.files_reviewed)} received"
|
|
71
|
+
case Verdict.TOO_LARGE:
|
|
72
|
+
return "Change too large to review"
|
|
73
|
+
case Verdict.INCOMPLETE:
|
|
74
|
+
return INCOMPLETE_TITLE
|
|
75
|
+
|
|
76
|
+
|
|
77
|
+
def summary(report: ReviewReport) -> str:
|
|
78
|
+
lines: list[str]
|
|
79
|
+
match report.verdict:
|
|
80
|
+
case Verdict.INCOMPLETE:
|
|
81
|
+
lines = [
|
|
82
|
+
"The review failed before producing results. It will run again on the next "
|
|
83
|
+
"push; no findings were withheld."
|
|
84
|
+
]
|
|
85
|
+
case Verdict.TOO_LARGE:
|
|
86
|
+
lines = [
|
|
87
|
+
f"This pull request changes {_files(report.files_reviewed)} and "
|
|
88
|
+
f"{report.diff_lines} lines, more than ML AI reviews at once.",
|
|
89
|
+
"",
|
|
90
|
+
"Split it into smaller pull requests to get a review.",
|
|
91
|
+
]
|
|
92
|
+
case Verdict.NOT_REVIEWED:
|
|
93
|
+
lines = [
|
|
94
|
+
f"Received {_files(report.files_reviewed)}.",
|
|
95
|
+
"",
|
|
96
|
+
"Automated review is not enabled; this check confirms the pipeline ran.",
|
|
97
|
+
]
|
|
98
|
+
case Verdict.NO_ISSUES:
|
|
99
|
+
lines = [
|
|
100
|
+
f"Reviewed {_files(report.files_reviewed)}; nothing met the bar for reporting."
|
|
101
|
+
]
|
|
102
|
+
case Verdict.ISSUES_FOUND:
|
|
103
|
+
lines = [f"Reviewed {_files(report.files_reviewed)}.", ""]
|
|
104
|
+
for finding in report.findings:
|
|
105
|
+
lines += _finding_section(finding)
|
|
106
|
+
if report.noted:
|
|
107
|
+
lines += ["", "### Also worth a look", ""]
|
|
108
|
+
lines += [f"- {_note(f)}" for f in report.noted]
|
|
109
|
+
lines.append("")
|
|
110
|
+
if report.repeated:
|
|
111
|
+
lines.append(f"{report.repeated} finding(s) from earlier reviews are still open.")
|
|
112
|
+
if report.resolved:
|
|
113
|
+
lines.append(f"{len(report.resolved)} earlier finding(s) look resolved by this push.")
|
|
114
|
+
if report.dismissed:
|
|
115
|
+
lines.append(
|
|
116
|
+
f"{report.dismissed} finding(s) were left out: the team marked the same issue as "
|
|
117
|
+
"not useful before."
|
|
118
|
+
)
|
|
119
|
+
if report.refuted:
|
|
120
|
+
lines.append(f"{report.refuted} candidate finding(s) were disproved on a second look.")
|
|
121
|
+
tentative = sum(1 for f in report.findings if f.tentative)
|
|
122
|
+
if tentative:
|
|
123
|
+
lines.append(f"{tentative} of these are possibilities ML AI is not sure about.")
|
|
124
|
+
if report.suppressed:
|
|
125
|
+
lines.append(f"{report.suppressed} lower-confidence finding(s) were not shown.")
|
|
126
|
+
if report.since_commit:
|
|
127
|
+
lines.append(
|
|
128
|
+
f"Reviewed only what changed since commit {report.since_commit[:7]}, which was "
|
|
129
|
+
"reviewed earlier."
|
|
130
|
+
)
|
|
131
|
+
if report.files_skipped:
|
|
132
|
+
lines.append(
|
|
133
|
+
f"{report.files_skipped} generated, vendored or lock file(s) were not reviewed."
|
|
134
|
+
)
|
|
135
|
+
if report.diff_only:
|
|
136
|
+
lines.append("Reviewed from diffs only: file contents were not accessible.")
|
|
137
|
+
return "\n".join(lines)[:MAX_SUMMARY_CHARS]
|
|
138
|
+
|
|
139
|
+
|
|
140
|
+
def finding_marker(finding: Finding) -> str:
|
|
141
|
+
"""Identifies the issue a comment is about, so a later review can tell it was already
|
|
142
|
+
commented on and whether this push resolved it."""
|
|
143
|
+
return f"{_FINDING_PREFIX}{fingerprint(finding)} -->"
|
|
144
|
+
|
|
145
|
+
|
|
146
|
+
def fingerprint_in(body: str) -> str | None:
|
|
147
|
+
start = body.find(_FINDING_PREFIX)
|
|
148
|
+
if start < 0:
|
|
149
|
+
return None
|
|
150
|
+
end = body.find(" -->", start)
|
|
151
|
+
return body[start + len(_FINDING_PREFIX) : end] if end > start else None
|
|
152
|
+
|
|
153
|
+
|
|
154
|
+
def is_tentative(body: str) -> bool:
|
|
155
|
+
return _TENTATIVE_MARKER in body
|
|
156
|
+
|
|
157
|
+
|
|
158
|
+
def is_resolved_reply(body: str) -> bool:
|
|
159
|
+
return body.startswith(RESOLVED_REPLY_PREFIX)
|
|
160
|
+
|
|
161
|
+
|
|
162
|
+
def title_in(body: str) -> str:
|
|
163
|
+
"""The finding's title, read back from its comment's first line: **Severity: title**."""
|
|
164
|
+
first = body.strip().splitlines()[0] if body.strip() else ""
|
|
165
|
+
text = first.strip("*").strip()
|
|
166
|
+
return text.split(": ", 1)[1] if ": " in text else text
|
|
167
|
+
|
|
168
|
+
|
|
169
|
+
def explanation_in(body: str) -> str:
|
|
170
|
+
"""The finding's explanation, read back from its comment: the first paragraph after the
|
|
171
|
+
heading (and after the "Line N." of a file comment)."""
|
|
172
|
+
paragraphs = [p.strip() for p in body.strip().split("\n\n")[1:]]
|
|
173
|
+
for paragraph in paragraphs:
|
|
174
|
+
if paragraph and not _LINE_NOTE.fullmatch(paragraph) and not paragraph.startswith("<!--"):
|
|
175
|
+
return paragraph
|
|
176
|
+
return ""
|
|
177
|
+
|
|
178
|
+
|
|
179
|
+
def resolved_reply(head_sha: str) -> str:
|
|
180
|
+
return (
|
|
181
|
+
f"{RESOLVED_REPLY_PREFIX}{head_sha[:7]}: the lines changed and the issue is no longer "
|
|
182
|
+
"reported."
|
|
183
|
+
)
|
|
184
|
+
|
|
185
|
+
|
|
186
|
+
def inline_comment(finding: Finding) -> str:
|
|
187
|
+
"""Posted on the line itself. A fix within the diff becomes a GitHub suggestion the author
|
|
188
|
+
can commit with one click; otherwise it is shown as a diff. A tentative finding says it is
|
|
189
|
+
a possibility, in its heading and in a note."""
|
|
190
|
+
parts = [f"**{_label(finding)}: {finding.title}**", "", finding.explanation]
|
|
191
|
+
if finding.tentative:
|
|
192
|
+
parts += ["", TENTATIVE_NOTE]
|
|
193
|
+
if finding.fix is not None and finding.fix.suggestable:
|
|
194
|
+
parts += ["", "```suggestion", finding.fix.code, "```"]
|
|
195
|
+
elif finding.fix is not None:
|
|
196
|
+
parts += ["", *_details("Suggested fix", _diff_block(finding.fix))]
|
|
197
|
+
elif finding.suggestion:
|
|
198
|
+
parts += ["", *_details("Suggested fix", [finding.suggestion])]
|
|
199
|
+
parts += ["", finding_marker(finding)]
|
|
200
|
+
if finding.tentative:
|
|
201
|
+
parts.append(_TENTATIVE_MARKER)
|
|
202
|
+
return "\n".join(parts)
|
|
203
|
+
|
|
204
|
+
|
|
205
|
+
_LINE_NOTE = re.compile(r"Line \d+\.")
|
|
206
|
+
|
|
207
|
+
|
|
208
|
+
def file_comment(finding: Finding) -> str:
|
|
209
|
+
"""Posted on the file when the line is outside the diff (GitHub allows line comments only
|
|
210
|
+
on lines the diff shows), so it can still be discussed and reacted to."""
|
|
211
|
+
head, _, rest = inline_comment(finding).partition("\n")
|
|
212
|
+
return f"{head}\n\nLine {finding.line}.\n{rest}"
|
|
213
|
+
|
|
214
|
+
|
|
215
|
+
# The words GitHub readers see, the same as the Code review page; stored keys stay as they are.
|
|
216
|
+
SEVERITY_LABEL = {
|
|
217
|
+
Severity.CRITICAL: "Critical",
|
|
218
|
+
Severity.HIGH: "Major",
|
|
219
|
+
Severity.MEDIUM: "Minor",
|
|
220
|
+
Severity.LOW: "Nit",
|
|
221
|
+
}
|
|
222
|
+
|
|
223
|
+
|
|
224
|
+
def severity_label(severity: Severity) -> str:
|
|
225
|
+
return SEVERITY_LABEL[severity]
|
|
226
|
+
|
|
227
|
+
|
|
228
|
+
def _label(finding: Finding) -> str:
|
|
229
|
+
severity = severity_label(finding.severity)
|
|
230
|
+
return f"Possible issue ({severity})" if finding.tentative else severity
|
|
231
|
+
|
|
232
|
+
|
|
233
|
+
def _finding_section(finding: Finding) -> list[str]:
|
|
234
|
+
lines = [
|
|
235
|
+
f"### {_label(finding)}: {finding.title}",
|
|
236
|
+
f"`{finding.path}:{finding.line}`",
|
|
237
|
+
"",
|
|
238
|
+
finding.explanation,
|
|
239
|
+
"",
|
|
240
|
+
]
|
|
241
|
+
if finding.fix is not None:
|
|
242
|
+
lines += ["Suggested fix:", *_diff_block(finding.fix), ""]
|
|
243
|
+
elif finding.suggestion:
|
|
244
|
+
lines += [f"Suggested fix: {finding.suggestion}", ""]
|
|
245
|
+
return lines
|
|
246
|
+
|
|
247
|
+
|
|
248
|
+
def _details(summary: str, body: list[str]) -> list[str]:
|
|
249
|
+
"""A collapsed section: GitHub renders it closed, with the summary as its toggle."""
|
|
250
|
+
return ["<details>", f"<summary>{summary}</summary>", "", *body, "", "</details>"]
|
|
251
|
+
|
|
252
|
+
|
|
253
|
+
def _diff_block(fix: Fix) -> list[str]:
|
|
254
|
+
"""Removed lines in red, replacement in green, the way a human's suggestion reads."""
|
|
255
|
+
removed = [f"-{line}" for line in fix.replaced]
|
|
256
|
+
added = [f"+{line}" for line in fix.code.splitlines()]
|
|
257
|
+
return ["```diff", *removed, *added, "```"]
|
|
258
|
+
|
|
259
|
+
|
|
260
|
+
REVIEWED_PREFIX = "<!-- mlai-reviewed:"
|
|
261
|
+
CREDITS_START = "<!-- mlai-credits -->"
|
|
262
|
+
CREDITS_END = "<!-- /mlai-credits -->"
|
|
263
|
+
CREDITS_PENDING = "Credits used: counted when the review closes."
|
|
264
|
+
MAX_WALKTHROUGH_FILES = 15
|
|
265
|
+
MAX_TABLE_FINDINGS = 30
|
|
266
|
+
_SHA = re.compile(r"[0-9a-f]{40}")
|
|
267
|
+
|
|
268
|
+
|
|
269
|
+
def reviewed_in(body: str) -> str | None:
|
|
270
|
+
"""The head commit a summary comment says was reviewed, if any."""
|
|
271
|
+
start = body.find(REVIEWED_PREFIX)
|
|
272
|
+
if start < 0:
|
|
273
|
+
return None
|
|
274
|
+
sha = body[start + len(REVIEWED_PREFIX) :].split(" -->", 1)[0].strip()
|
|
275
|
+
return sha if _SHA.fullmatch(sha) else None
|
|
276
|
+
|
|
277
|
+
|
|
278
|
+
def _cell(text: str) -> str:
|
|
279
|
+
return text.replace("|", "\\|").replace("\n", " ").strip()
|
|
280
|
+
|
|
281
|
+
|
|
282
|
+
def overview(report: ReviewReport, *, head_sha: str, blob_base: str) -> str:
|
|
283
|
+
"""The one summary comment per pull request, rewritten by every review: a short walkthrough
|
|
284
|
+
of the change, the findings by severity with links to their lines, what was skipped and
|
|
285
|
+
why, and the credits used (filled in by the app once the review's run is closed). The
|
|
286
|
+
hidden marker records the reviewed commit, so the next push is reviewed incrementally."""
|
|
287
|
+
judged = report.verdict in (Verdict.ISSUES_FOUND, Verdict.NO_ISSUES)
|
|
288
|
+
lines = [OVERVIEW_MARKER]
|
|
289
|
+
if judged:
|
|
290
|
+
lines.append(f"{REVIEWED_PREFIX}{head_sha} -->")
|
|
291
|
+
shown = (*report.findings, *report.noted)
|
|
292
|
+
count = len(shown)
|
|
293
|
+
status = (
|
|
294
|
+
"change too large to review"
|
|
295
|
+
if report.verdict is Verdict.TOO_LARGE
|
|
296
|
+
else f"{count} finding{'' if count == 1 else 's'}"
|
|
297
|
+
if count
|
|
298
|
+
else "no issues found"
|
|
299
|
+
)
|
|
300
|
+
scope = (
|
|
301
|
+
f" (only what changed since `{report.since_commit[:7]}`)" if report.since_commit else ""
|
|
302
|
+
)
|
|
303
|
+
lines += ["## ML AI Review", "", f"Reviewed `{head_sha[:7]}`{scope} · {status}", ""]
|
|
304
|
+
if report.changed:
|
|
305
|
+
adds = sum(f.additions for f in report.changed)
|
|
306
|
+
dels = sum(f.deletions for f in report.changed)
|
|
307
|
+
files = len(report.changed)
|
|
308
|
+
lines += ["### Walkthrough", ""]
|
|
309
|
+
if report.pr_title:
|
|
310
|
+
lines.append(f"{_cell(report.pr_title)}.")
|
|
311
|
+
lines += [
|
|
312
|
+
f"{'This push changes' if report.since_commit else 'Changes'} "
|
|
313
|
+
f"{files} file{'' if files == 1 else 's'} (+{adds} -{dels}).",
|
|
314
|
+
"",
|
|
315
|
+
]
|
|
316
|
+
table = ["| File | Change |", "| --- | --- |"]
|
|
317
|
+
biggest = sorted(report.changed, key=lambda f: f.additions + f.deletions, reverse=True)
|
|
318
|
+
for f in biggest[:MAX_WALKTHROUGH_FILES]:
|
|
319
|
+
table.append(f"| `{_cell(f.path)}` | {f.status}, +{f.additions} -{f.deletions} |")
|
|
320
|
+
if files > MAX_WALKTHROUGH_FILES:
|
|
321
|
+
table.append(f"| and {files - MAX_WALKTHROUGH_FILES} more | |")
|
|
322
|
+
lines += [*_details("Changed files", table), ""]
|
|
323
|
+
if shown:
|
|
324
|
+
lines += ["### Findings", "", "| Severity | Finding | Where |", "| --- | --- | --- |"]
|
|
325
|
+
ordered = sorted(shown, key=lambda f: f.severity.rank, reverse=True)
|
|
326
|
+
inline = {id(f) for f in report.findings}
|
|
327
|
+
for f in ordered[:MAX_TABLE_FINDINGS]:
|
|
328
|
+
link = f"{blob_base}/{head_sha}/{f.path}#L{f.line}"
|
|
329
|
+
label = _label(f) + ("" if id(f) in inline else " (listed here only)")
|
|
330
|
+
lines.append(f"| {label} | {_cell(f.title)} | [`{_cell(f.path)}:{f.line}`]({link}) |")
|
|
331
|
+
if len(ordered) > MAX_TABLE_FINDINGS:
|
|
332
|
+
lines.append(f"| | and {len(ordered) - MAX_TABLE_FINDINGS} more | |")
|
|
333
|
+
lines.append("")
|
|
334
|
+
follow: list[str] = []
|
|
335
|
+
if report.resolved:
|
|
336
|
+
follow.append(f"{len(report.resolved)} earlier finding(s) look resolved by this push.")
|
|
337
|
+
if report.repeated:
|
|
338
|
+
follow.append(f"{report.repeated} finding(s) from earlier reviews are still open.")
|
|
339
|
+
if follow:
|
|
340
|
+
lines += [*follow, ""]
|
|
341
|
+
skipped: list[str] = []
|
|
342
|
+
if report.verdict is Verdict.TOO_LARGE:
|
|
343
|
+
skipped.append(
|
|
344
|
+
f"The whole change: {_files(report.files_reviewed)} and {report.diff_lines} changed "
|
|
345
|
+
"lines is over this repository's review limit. Split the pull request, or raise "
|
|
346
|
+
"maxFiles / maxLines in `.mlai/review.yml`."
|
|
347
|
+
)
|
|
348
|
+
if report.files_skipped:
|
|
349
|
+
skipped.append(f"{report.files_skipped} generated, vendored or lock file(s).")
|
|
350
|
+
if report.files_ignored:
|
|
351
|
+
skipped.append(
|
|
352
|
+
f"{report.files_ignored} file(s) matching this repository's ignored paths."
|
|
353
|
+
)
|
|
354
|
+
if report.below_minimum:
|
|
355
|
+
skipped.append(
|
|
356
|
+
f"Inline comments for {report.below_minimum} finding(s) below the minimum severity "
|
|
357
|
+
"(listed above only)."
|
|
358
|
+
)
|
|
359
|
+
if report.dismissed:
|
|
360
|
+
skipped.append(f"{report.dismissed} finding(s) the team marked as not useful before.")
|
|
361
|
+
if report.suppressed:
|
|
362
|
+
skipped.append(f"{report.suppressed} lower-confidence finding(s).")
|
|
363
|
+
if report.diff_only:
|
|
364
|
+
skipped.append("Whole-file context: file contents were not accessible, so diffs only.")
|
|
365
|
+
if skipped:
|
|
366
|
+
lines += ["### Skipped", "", *(f"- {item}" for item in skipped), ""]
|
|
367
|
+
lines += [
|
|
368
|
+
f"{CREDITS_START}{CREDITS_PENDING}{CREDITS_END}",
|
|
369
|
+
"",
|
|
370
|
+
"",
|
|
371
|
+
*_details(
|
|
372
|
+
"Commands",
|
|
373
|
+
[
|
|
374
|
+
"- `@mlai review` reviews again; `@mlai review deep` or `@mlai review security` "
|
|
375
|
+
"for one deeper or security-focused run.",
|
|
376
|
+
"- `@mlai resolve` marks the open findings resolved (in a finding's thread: that one).",
|
|
377
|
+
"- `@mlai fix` in reply to a finding pushes a fix to this branch.",
|
|
378
|
+
],
|
|
379
|
+
),
|
|
380
|
+
]
|
|
381
|
+
return "\n".join(lines)[:MAX_SUMMARY_CHARS]
|
|
382
|
+
|
|
383
|
+
|
|
384
|
+
def _note(finding: Finding, link: str | None = None) -> str:
|
|
385
|
+
where = f"`{finding.path}:{finding.line}`"
|
|
386
|
+
if link:
|
|
387
|
+
where = f"[{where}]({link})"
|
|
388
|
+
first = finding.explanation.strip().split(". ")[0].rstrip(".")
|
|
389
|
+
detail = f" {first[:MAX_NOTE_CHARS]}." if first else ""
|
|
390
|
+
return f"**{severity_label(finding.severity)}** {where} {finding.title}.{detail}"
|
|
391
|
+
|
|
392
|
+
|
|
393
|
+
def is_overview(body: str) -> bool:
|
|
394
|
+
return body.startswith(OVERVIEW_MARKER)
|
|
395
|
+
|
|
396
|
+
|
|
397
|
+
def _files(count: int) -> str:
|
|
398
|
+
return f"{count} changed file{'' if count == 1 else 's'}"
|
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
"""GitHub access with the token of the account that connected the repository. ML AI Code runs
|
|
2
|
+
the review where that token already lives (the cloud sandbox or this Mac), so there is no App
|
|
3
|
+
key to sign with and no installation to look up: one token serves every call."""
|
|
4
|
+
|
|
5
|
+
from __future__ import annotations
|
|
6
|
+
|
|
7
|
+
from typing import Any
|
|
8
|
+
|
|
9
|
+
|
|
10
|
+
class StaticTokenProvider:
|
|
11
|
+
"""Same interface the adapters used for App installation tokens: token() and locate()."""
|
|
12
|
+
|
|
13
|
+
def __init__(self, token: str) -> None:
|
|
14
|
+
self._token = token
|
|
15
|
+
|
|
16
|
+
async def token(self, installation_id: int, repository_id: int, permissions: Any = None) -> str:
|
|
17
|
+
return self._token
|
|
18
|
+
|
|
19
|
+
async def locate(self, owner: str, repo: str) -> tuple[int, int]:
|
|
20
|
+
return 0, 0
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
"""Adapters for reviewing a checkout on this machine and reporting to a file."""
|
|
@@ -0,0 +1,48 @@
|
|
|
1
|
+
"""The repository's own compiler and linters, run on this machine (the chat's sandbox or the
|
|
2
|
+
Mac the chat already runs commands on), or read from a result an earlier, secret-free step
|
|
3
|
+
wrote. In a pull request review the tools run before the review process starts and holds any
|
|
4
|
+
token, so code from the pull request never runs next to one."""
|
|
5
|
+
|
|
6
|
+
from __future__ import annotations
|
|
7
|
+
|
|
8
|
+
import asyncio
|
|
9
|
+
import json
|
|
10
|
+
from pathlib import Path
|
|
11
|
+
|
|
12
|
+
from mlai_review.analysis.detect import Tool
|
|
13
|
+
from mlai_review.application.errors import ReviewError
|
|
14
|
+
from mlai_review.application.ports import StaticAnalyzer
|
|
15
|
+
from mlai_review.contracts.analysis import AnalysisResult
|
|
16
|
+
from mlai_review.contracts.review import ReviewPullRequestInput
|
|
17
|
+
from mlai_review.domain.diagnostics import Diagnostic
|
|
18
|
+
from mlai_review.infrastructure.analysis.translate import diagnostics_from
|
|
19
|
+
|
|
20
|
+
|
|
21
|
+
class LocalAnalyzer(StaticAnalyzer):
|
|
22
|
+
def __init__(self, root: Path, *, tools: tuple[Tool, ...] | None = None) -> None:
|
|
23
|
+
self._root = root
|
|
24
|
+
self._tools = tools
|
|
25
|
+
|
|
26
|
+
async def analyse(self, target: ReviewPullRequestInput) -> tuple[Diagnostic, ...]:
|
|
27
|
+
result = await asyncio.to_thread(run_analysis, self._root, self._tools)
|
|
28
|
+
return diagnostics_from(result)
|
|
29
|
+
|
|
30
|
+
|
|
31
|
+
class StoredAnalysis(StaticAnalyzer):
|
|
32
|
+
def __init__(self, file: Path) -> None:
|
|
33
|
+
self._file = file
|
|
34
|
+
|
|
35
|
+
async def analyse(self, target: ReviewPullRequestInput) -> tuple[Diagnostic, ...]:
|
|
36
|
+
try:
|
|
37
|
+
data = json.loads(self._file.read_text(encoding="utf-8"))
|
|
38
|
+
except (OSError, json.JSONDecodeError) as exc:
|
|
39
|
+
raise ReviewError(f"analysis result unreadable: {exc}") from exc
|
|
40
|
+
if not data: # the secret-free step could not run the tools: review without them
|
|
41
|
+
return ()
|
|
42
|
+
return diagnostics_from(AnalysisResult.from_dict(data))
|
|
43
|
+
|
|
44
|
+
|
|
45
|
+
def run_analysis(root: Path, tools: tuple[Tool, ...] | None = None) -> AnalysisResult:
|
|
46
|
+
from mlai_review.analysis import main as analysis
|
|
47
|
+
|
|
48
|
+
return analysis.analyse(root, allowed=tools, install=False)
|
|
@@ -0,0 +1,100 @@
|
|
|
1
|
+
"""The review as a stream of JSON lines in a file: what the app shows while a review runs (each
|
|
2
|
+
finding inline in the diff as soon as it is confirmed) and the report when it ends. One line per
|
|
3
|
+
event, flushed at once, so a reader polling the file never sees half an event."""
|
|
4
|
+
|
|
5
|
+
from __future__ import annotations
|
|
6
|
+
|
|
7
|
+
import json
|
|
8
|
+
import time
|
|
9
|
+
from pathlib import Path
|
|
10
|
+
from typing import Any
|
|
11
|
+
|
|
12
|
+
from mlai_review.domain.findings import Finding, fingerprint
|
|
13
|
+
from mlai_review.domain.lifecycle import PriorFinding
|
|
14
|
+
from mlai_review.domain.report import ReviewReport
|
|
15
|
+
from mlai_review.infrastructure.github import rendering
|
|
16
|
+
|
|
17
|
+
|
|
18
|
+
class EventLog:
|
|
19
|
+
def __init__(self, path: Path | None) -> None:
|
|
20
|
+
self._path = path
|
|
21
|
+
if path is not None:
|
|
22
|
+
path.parent.mkdir(parents=True, exist_ok=True)
|
|
23
|
+
|
|
24
|
+
def emit(self, kind: str, **fields: Any) -> dict[str, Any]:
|
|
25
|
+
event = {"type": kind, "at": round(time.time(), 3), **fields}
|
|
26
|
+
if self._path is not None:
|
|
27
|
+
with self._path.open("a", encoding="utf-8") as out:
|
|
28
|
+
out.write(json.dumps(event, ensure_ascii=False, default=str) + "\n")
|
|
29
|
+
return event
|
|
30
|
+
|
|
31
|
+
|
|
32
|
+
def finding_json(finding: Finding, *, line_text: str | None = None) -> dict[str, Any]:
|
|
33
|
+
fix = finding.fix
|
|
34
|
+
out: dict[str, Any] = {
|
|
35
|
+
"fingerprint": fingerprint(finding),
|
|
36
|
+
"title": finding.title,
|
|
37
|
+
"severity": finding.severity.value,
|
|
38
|
+
"category": finding.category,
|
|
39
|
+
"path": finding.path,
|
|
40
|
+
"line": finding.line,
|
|
41
|
+
"explanation": finding.explanation,
|
|
42
|
+
"suggestion": finding.suggestion,
|
|
43
|
+
"confidence": round(finding.confidence, 3),
|
|
44
|
+
"inDiff": finding.in_diff,
|
|
45
|
+
"tentative": finding.tentative,
|
|
46
|
+
"votes": finding.votes,
|
|
47
|
+
"readings": finding.readings,
|
|
48
|
+
}
|
|
49
|
+
if finding.challenge is not None:
|
|
50
|
+
out["challenge"] = finding.challenge.outcome.value
|
|
51
|
+
if fix is not None:
|
|
52
|
+
out["fix"] = {
|
|
53
|
+
"start": fix.start,
|
|
54
|
+
"end": fix.end,
|
|
55
|
+
"code": fix.code,
|
|
56
|
+
"replaced": list(fix.replaced),
|
|
57
|
+
}
|
|
58
|
+
if line_text is not None:
|
|
59
|
+
out["lineText"] = line_text
|
|
60
|
+
return out
|
|
61
|
+
|
|
62
|
+
|
|
63
|
+
def prior_json(prior: PriorFinding) -> dict[str, Any]:
|
|
64
|
+
return {
|
|
65
|
+
"fingerprint": prior.fingerprint,
|
|
66
|
+
"path": prior.path,
|
|
67
|
+
"line": prior.line,
|
|
68
|
+
"title": prior.title,
|
|
69
|
+
"id": prior.comment_id,
|
|
70
|
+
}
|
|
71
|
+
|
|
72
|
+
|
|
73
|
+
def report_json(report: ReviewReport) -> dict[str, Any]:
|
|
74
|
+
return {
|
|
75
|
+
"verdict": report.verdict.value,
|
|
76
|
+
"title": rendering.title(report),
|
|
77
|
+
"summary": rendering.summary(report),
|
|
78
|
+
"filesReviewed": report.files_reviewed,
|
|
79
|
+
"filesSkipped": report.files_skipped,
|
|
80
|
+
"diffLines": report.diff_lines,
|
|
81
|
+
"suppressed": report.suppressed,
|
|
82
|
+
"refuted": report.refuted,
|
|
83
|
+
"repeated": report.repeated,
|
|
84
|
+
"dismissed": report.dismissed,
|
|
85
|
+
"diffOnly": report.diff_only,
|
|
86
|
+
"findings": [finding_json(f) for f in report.findings],
|
|
87
|
+
"noted": [finding_json(f) for f in report.noted],
|
|
88
|
+
"resolved": [prior_json(p) for p in report.resolved],
|
|
89
|
+
"severities": severity_counts(report),
|
|
90
|
+
"sinceCommit": report.since_commit,
|
|
91
|
+
"filesIgnored": report.files_ignored,
|
|
92
|
+
"belowMinimum": report.below_minimum,
|
|
93
|
+
}
|
|
94
|
+
|
|
95
|
+
|
|
96
|
+
def severity_counts(report: ReviewReport) -> dict[str, int]:
|
|
97
|
+
counts = {"critical": 0, "high": 0, "medium": 0, "low": 0}
|
|
98
|
+
for finding in (*report.findings, *report.noted):
|
|
99
|
+
counts[finding.severity.value] = counts.get(finding.severity.value, 0) + 1
|
|
100
|
+
return counts
|