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,128 @@
|
|
|
1
|
+
"""Entry point of the sandbox job.
|
|
2
|
+
|
|
3
|
+
Environment:
|
|
4
|
+
MLAI_REPOSITORY owner/name
|
|
5
|
+
MLAI_HEAD_SHA the commit to analyse
|
|
6
|
+
MLAI_GITHUB_TOKEN a short-lived installation token with contents:read on that one repository
|
|
7
|
+
MLAI_RESULT_URL where to PUT the result (a write-only blob URL); omitted: print to stdout
|
|
8
|
+
MLAI_CLONE_BASE defaults to https://github.com
|
|
9
|
+
|
|
10
|
+
Local use: `mlai-analyse --root /path/to/checkout` skips the clone and prints the result.
|
|
11
|
+
"""
|
|
12
|
+
|
|
13
|
+
from __future__ import annotations
|
|
14
|
+
|
|
15
|
+
import argparse
|
|
16
|
+
import base64
|
|
17
|
+
import json
|
|
18
|
+
import os
|
|
19
|
+
import shutil
|
|
20
|
+
import subprocess
|
|
21
|
+
import sys
|
|
22
|
+
import tempfile
|
|
23
|
+
import urllib.request
|
|
24
|
+
from pathlib import Path
|
|
25
|
+
|
|
26
|
+
from mlai_review.analysis.detect import Tool, detect
|
|
27
|
+
from mlai_review.analysis.tools import ToolFailed, install_node_dependencies, run_tool
|
|
28
|
+
from mlai_review.contracts.analysis import AnalysisResult, Diagnostic
|
|
29
|
+
|
|
30
|
+
CLONE_TIMEOUT_SECONDS = 600
|
|
31
|
+
|
|
32
|
+
|
|
33
|
+
def analyse(
|
|
34
|
+
root: Path, *, allowed: tuple[Tool, ...] | None = None, install: bool = True
|
|
35
|
+
) -> AnalysisResult:
|
|
36
|
+
"""`allowed` limits the tools (eslint runs the repository's own config, which is code);
|
|
37
|
+
`install` False uses the dependencies already there, never touching a developer's
|
|
38
|
+
node_modules."""
|
|
39
|
+
tools = tuple(t for t in detect(root) if allowed is None or t in allowed)
|
|
40
|
+
diagnostics: list[Diagnostic] = []
|
|
41
|
+
errors: dict[str, str] = {}
|
|
42
|
+
ran: list[str] = []
|
|
43
|
+
node = Tool.TSC in tools or Tool.ESLINT in tools
|
|
44
|
+
if install and node and not (root / "node_modules").is_dir():
|
|
45
|
+
try:
|
|
46
|
+
install_node_dependencies(root)
|
|
47
|
+
except ToolFailed as exc:
|
|
48
|
+
errors["npm"] = str(exc)
|
|
49
|
+
for tool in tools:
|
|
50
|
+
try:
|
|
51
|
+
diagnostics += run_tool(tool, root)
|
|
52
|
+
ran.append(tool.value)
|
|
53
|
+
except ToolFailed as exc:
|
|
54
|
+
errors[tool.value] = str(exc)
|
|
55
|
+
return AnalysisResult(tools_run=tuple(ran), diagnostics=tuple(diagnostics), errors=errors)
|
|
56
|
+
|
|
57
|
+
|
|
58
|
+
def clone(repository: str, sha: str, token: str, base: str, into: Path) -> None:
|
|
59
|
+
credentials = base64.b64encode(f"x-access-token:{token}".encode()).decode()
|
|
60
|
+
env = {
|
|
61
|
+
**os.environ,
|
|
62
|
+
"GIT_TERMINAL_PROMPT": "0",
|
|
63
|
+
"GIT_CONFIG_COUNT": "1",
|
|
64
|
+
"GIT_CONFIG_KEY_0": "http.extraheader",
|
|
65
|
+
"GIT_CONFIG_VALUE_0": f"Authorization: Basic {credentials}",
|
|
66
|
+
}
|
|
67
|
+
url = f"{base.rstrip('/')}/{repository}.git"
|
|
68
|
+
for args in (
|
|
69
|
+
["init", "-q"],
|
|
70
|
+
["fetch", "-q", "--depth", "1", "--no-tags", url, sha],
|
|
71
|
+
["checkout", "-q", "--detach", "FETCH_HEAD"],
|
|
72
|
+
):
|
|
73
|
+
subprocess.run(
|
|
74
|
+
["git", "-C", str(into), *args], check=True, env=env, timeout=CLONE_TIMEOUT_SECONDS
|
|
75
|
+
)
|
|
76
|
+
|
|
77
|
+
|
|
78
|
+
def publish(result: AnalysisResult, url: str | None) -> None:
|
|
79
|
+
body = json.dumps(result.as_dict()).encode()
|
|
80
|
+
if not url:
|
|
81
|
+
sys.stdout.write(body.decode() + "\n")
|
|
82
|
+
return
|
|
83
|
+
request = urllib.request.Request(
|
|
84
|
+
url,
|
|
85
|
+
data=body,
|
|
86
|
+
method="PUT",
|
|
87
|
+
headers={"x-ms-blob-type": "BlockBlob", "Content-Type": "application/json"},
|
|
88
|
+
)
|
|
89
|
+
with urllib.request.urlopen(request, timeout=60):
|
|
90
|
+
pass
|
|
91
|
+
|
|
92
|
+
|
|
93
|
+
def main() -> int:
|
|
94
|
+
parser = argparse.ArgumentParser(description=__doc__)
|
|
95
|
+
parser.add_argument("--root", type=Path, help="analyse an existing checkout instead of cloning")
|
|
96
|
+
parser.add_argument("--out", type=Path, help="write the result here instead of stdout")
|
|
97
|
+
parser.add_argument("--no-eslint", action="store_true", help="skip eslint (runs repo code)")
|
|
98
|
+
args = parser.parse_args()
|
|
99
|
+
if args.root:
|
|
100
|
+
allowed = tuple(t for t in Tool if t is not Tool.ESLINT) if args.no_eslint else None
|
|
101
|
+
result = analyse(args.root, allowed=allowed)
|
|
102
|
+
if args.out:
|
|
103
|
+
args.out.write_text(json.dumps(result.as_dict()))
|
|
104
|
+
else:
|
|
105
|
+
publish(result, None)
|
|
106
|
+
return 0
|
|
107
|
+
repository = os.environ["MLAI_REPOSITORY"]
|
|
108
|
+
sha = os.environ["MLAI_HEAD_SHA"]
|
|
109
|
+
token = os.environ.pop("MLAI_GITHUB_TOKEN") # out of the environment before any tool runs
|
|
110
|
+
result_url = os.environ.get("MLAI_RESULT_URL")
|
|
111
|
+
base = os.environ.get("MLAI_CLONE_BASE", "https://github.com")
|
|
112
|
+
workdir = Path(tempfile.mkdtemp(prefix="mlai-analysis-"))
|
|
113
|
+
root = workdir / "repo"
|
|
114
|
+
root.mkdir()
|
|
115
|
+
try:
|
|
116
|
+
clone(repository, sha, token, base, root)
|
|
117
|
+
del token
|
|
118
|
+
result = analyse(root)
|
|
119
|
+
except subprocess.CalledProcessError as exc:
|
|
120
|
+
result = AnalysisResult(errors={"clone": f"git exited with {exc.returncode}"})
|
|
121
|
+
finally:
|
|
122
|
+
shutil.rmtree(workdir, ignore_errors=True)
|
|
123
|
+
publish(result, result_url)
|
|
124
|
+
return 0
|
|
125
|
+
|
|
126
|
+
|
|
127
|
+
if __name__ == "__main__":
|
|
128
|
+
sys.exit(main())
|
|
@@ -0,0 +1,8 @@
|
|
|
1
|
+
"""Wording for why a tool could not run. Reported to operators in the result, never to the
|
|
2
|
+
model."""
|
|
3
|
+
|
|
4
|
+
TOOL_NOT_AVAILABLE = "{name}: not available: {error}"
|
|
5
|
+
TOOL_TIMED_OUT = "{name}: took longer than {seconds:.0f}s"
|
|
6
|
+
TOOL_EXITED = "{name}: exited with {code}: {detail}"
|
|
7
|
+
TOOL_UNREADABLE_OUTPUT = "{name}: unreadable output: {error}"
|
|
8
|
+
ESLINT_MISSING = "eslint: not installed in the repository (node_modules/.bin/eslint)"
|
|
@@ -0,0 +1,194 @@
|
|
|
1
|
+
"""Running each tool and turning what it prints into diagnostics. Every tool is run with a
|
|
2
|
+
timeout and a clean environment; its exit code is not an error (tools exit non-zero when they
|
|
3
|
+
find something), only its output matters."""
|
|
4
|
+
|
|
5
|
+
from __future__ import annotations
|
|
6
|
+
|
|
7
|
+
import json
|
|
8
|
+
import os
|
|
9
|
+
import re
|
|
10
|
+
import subprocess
|
|
11
|
+
from collections.abc import Callable
|
|
12
|
+
from pathlib import Path
|
|
13
|
+
|
|
14
|
+
from mlai_review.analysis import messages
|
|
15
|
+
from mlai_review.analysis.detect import Tool
|
|
16
|
+
from mlai_review.contracts.analysis import Diagnostic
|
|
17
|
+
|
|
18
|
+
TOOL_TIMEOUT_SECONDS = 600
|
|
19
|
+
INSTALL_TIMEOUT_SECONDS = 900
|
|
20
|
+
MAX_DIAGNOSTICS = 2_000
|
|
21
|
+
|
|
22
|
+
_TSC_LINE = re.compile(
|
|
23
|
+
r"^(?P<path>[^(]+)\((?P<line>\d+),\d+\): "
|
|
24
|
+
r"(?P<level>error|warning) (?P<rule>TS\d+): (?P<message>.*)$"
|
|
25
|
+
)
|
|
26
|
+
# Ruff rules that mean the code is wrong, not merely untidy: undefined and misused names,
|
|
27
|
+
# syntax errors, invalid constructs. Everything else is reported as a warning.
|
|
28
|
+
_RUFF_ERRORS = frozenset(
|
|
29
|
+
{
|
|
30
|
+
"F821",
|
|
31
|
+
"F822",
|
|
32
|
+
"F823",
|
|
33
|
+
"F811",
|
|
34
|
+
"F632",
|
|
35
|
+
"F601",
|
|
36
|
+
"F602",
|
|
37
|
+
"F631",
|
|
38
|
+
"F633",
|
|
39
|
+
"F701",
|
|
40
|
+
"F702",
|
|
41
|
+
"F704",
|
|
42
|
+
"F706",
|
|
43
|
+
"F707",
|
|
44
|
+
}
|
|
45
|
+
)
|
|
46
|
+
|
|
47
|
+
|
|
48
|
+
class ToolFailed(Exception):
|
|
49
|
+
"""The tool could not run at all (missing, crashed, timed out)."""
|
|
50
|
+
|
|
51
|
+
|
|
52
|
+
def run_tool(tool: Tool, root: Path) -> list[Diagnostic]:
|
|
53
|
+
runner: Callable[[Path], list[Diagnostic]] = {
|
|
54
|
+
Tool.RUFF: _ruff,
|
|
55
|
+
Tool.TSC: _tsc,
|
|
56
|
+
Tool.ESLINT: _eslint,
|
|
57
|
+
}[tool]
|
|
58
|
+
return runner(root)[:MAX_DIAGNOSTICS]
|
|
59
|
+
|
|
60
|
+
|
|
61
|
+
def install_node_dependencies(root: Path) -> None:
|
|
62
|
+
"""Dependencies for tsc and eslint. Lifecycle scripts never run: they are arbitrary code
|
|
63
|
+
from the pull request, and the tools do not need them."""
|
|
64
|
+
if not (root / "package.json").is_file():
|
|
65
|
+
return
|
|
66
|
+
lockfile = (root / "package-lock.json").is_file() or (root / "npm-shrinkwrap.json").is_file()
|
|
67
|
+
command = [
|
|
68
|
+
"npm",
|
|
69
|
+
"ci" if lockfile else "install",
|
|
70
|
+
"--ignore-scripts",
|
|
71
|
+
"--no-audit",
|
|
72
|
+
"--no-fund",
|
|
73
|
+
]
|
|
74
|
+
_execute(command, root, INSTALL_TIMEOUT_SECONDS, name="npm")
|
|
75
|
+
|
|
76
|
+
|
|
77
|
+
def _ruff(root: Path) -> list[Diagnostic]:
|
|
78
|
+
output = _execute(
|
|
79
|
+
["ruff", "check", "--output-format", "json", "--exit-zero", "."],
|
|
80
|
+
root,
|
|
81
|
+
TOOL_TIMEOUT_SECONDS,
|
|
82
|
+
name="ruff",
|
|
83
|
+
)
|
|
84
|
+
try:
|
|
85
|
+
items = json.loads(output or "[]")
|
|
86
|
+
except json.JSONDecodeError as exc:
|
|
87
|
+
raise ToolFailed(messages.TOOL_UNREADABLE_OUTPUT.format(name="ruff", error=exc)) from exc
|
|
88
|
+
return [
|
|
89
|
+
Diagnostic(
|
|
90
|
+
tool="ruff",
|
|
91
|
+
path=_relative(root, item["filename"]),
|
|
92
|
+
line=int(item["location"]["row"]),
|
|
93
|
+
rule=str(item.get("code") or ""),
|
|
94
|
+
message=str(item.get("message", "")),
|
|
95
|
+
level="error" if _is_ruff_error(str(item.get("code") or "")) else "warning",
|
|
96
|
+
)
|
|
97
|
+
for item in items
|
|
98
|
+
]
|
|
99
|
+
|
|
100
|
+
|
|
101
|
+
def _tsc(root: Path) -> list[Diagnostic]:
|
|
102
|
+
binary = root / "node_modules" / ".bin" / "tsc"
|
|
103
|
+
command = [str(binary) if binary.is_file() else "tsc", "--noEmit", "--pretty", "false"]
|
|
104
|
+
output = _execute(command, root, TOOL_TIMEOUT_SECONDS, name="tsc", ok_codes=(0, 1, 2))
|
|
105
|
+
diagnostics = []
|
|
106
|
+
for line in output.splitlines():
|
|
107
|
+
match = _TSC_LINE.match(line.strip())
|
|
108
|
+
if match:
|
|
109
|
+
diagnostics.append(
|
|
110
|
+
Diagnostic(
|
|
111
|
+
tool="tsc",
|
|
112
|
+
path=_relative(root, match["path"]),
|
|
113
|
+
line=int(match["line"]),
|
|
114
|
+
rule=match["rule"],
|
|
115
|
+
message=match["message"],
|
|
116
|
+
level=match["level"],
|
|
117
|
+
)
|
|
118
|
+
)
|
|
119
|
+
return diagnostics
|
|
120
|
+
|
|
121
|
+
|
|
122
|
+
def _eslint(root: Path) -> list[Diagnostic]:
|
|
123
|
+
binary = root / "node_modules" / ".bin" / "eslint"
|
|
124
|
+
if not binary.is_file():
|
|
125
|
+
raise ToolFailed(messages.ESLINT_MISSING)
|
|
126
|
+
output = _execute(
|
|
127
|
+
[str(binary), ".", "--format", "json", "--no-error-on-unmatched-pattern"],
|
|
128
|
+
root,
|
|
129
|
+
TOOL_TIMEOUT_SECONDS,
|
|
130
|
+
name="eslint",
|
|
131
|
+
ok_codes=(0, 1),
|
|
132
|
+
)
|
|
133
|
+
try:
|
|
134
|
+
files = json.loads(output or "[]")
|
|
135
|
+
except json.JSONDecodeError as exc:
|
|
136
|
+
raise ToolFailed(messages.TOOL_UNREADABLE_OUTPUT.format(name="eslint", error=exc)) from exc
|
|
137
|
+
return [
|
|
138
|
+
Diagnostic(
|
|
139
|
+
tool="eslint",
|
|
140
|
+
path=_relative(root, file["filePath"]),
|
|
141
|
+
line=int(message.get("line") or 1),
|
|
142
|
+
rule=str(message.get("ruleId") or ""),
|
|
143
|
+
message=str(message.get("message", "")),
|
|
144
|
+
level="error" if message.get("severity") == 2 else "warning",
|
|
145
|
+
)
|
|
146
|
+
for file in files
|
|
147
|
+
for message in file.get("messages", [])
|
|
148
|
+
]
|
|
149
|
+
|
|
150
|
+
|
|
151
|
+
def _execute(
|
|
152
|
+
command: list[str],
|
|
153
|
+
root: Path,
|
|
154
|
+
timeout: float,
|
|
155
|
+
*,
|
|
156
|
+
name: str,
|
|
157
|
+
ok_codes: tuple[int, ...] = (0,),
|
|
158
|
+
) -> str:
|
|
159
|
+
env = {
|
|
160
|
+
"PATH": os.environ.get("PATH", "/usr/local/bin:/usr/bin:/bin"),
|
|
161
|
+
"HOME": str(root.parent),
|
|
162
|
+
"LANG": "C.UTF-8",
|
|
163
|
+
"CI": "1",
|
|
164
|
+
}
|
|
165
|
+
try:
|
|
166
|
+
done = subprocess.run(
|
|
167
|
+
command, cwd=root, env=env, capture_output=True, text=True, timeout=timeout
|
|
168
|
+
)
|
|
169
|
+
except FileNotFoundError as exc:
|
|
170
|
+
raise ToolFailed(messages.TOOL_NOT_AVAILABLE.format(name=name, error=exc)) from exc
|
|
171
|
+
except subprocess.TimeoutExpired as exc:
|
|
172
|
+
raise ToolFailed(messages.TOOL_TIMED_OUT.format(name=name, seconds=timeout)) from exc
|
|
173
|
+
if done.returncode not in ok_codes:
|
|
174
|
+
detail = (done.stderr or done.stdout).strip().splitlines()
|
|
175
|
+
raise ToolFailed(
|
|
176
|
+
messages.TOOL_EXITED.format(
|
|
177
|
+
name=name, code=done.returncode, detail=detail[-1] if detail else ""
|
|
178
|
+
)
|
|
179
|
+
)
|
|
180
|
+
return done.stdout
|
|
181
|
+
|
|
182
|
+
|
|
183
|
+
def _is_ruff_error(code: str) -> bool:
|
|
184
|
+
return code in _RUFF_ERRORS or code.startswith("E9")
|
|
185
|
+
|
|
186
|
+
|
|
187
|
+
def _relative(root: Path, path: str) -> str:
|
|
188
|
+
candidate = Path(path)
|
|
189
|
+
if candidate.is_absolute():
|
|
190
|
+
try:
|
|
191
|
+
return candidate.resolve().relative_to(root.resolve()).as_posix()
|
|
192
|
+
except ValueError:
|
|
193
|
+
return candidate.as_posix()
|
|
194
|
+
return candidate.as_posix()
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
"""Use cases and the ports they need. Depends on the domain and mlai_contracts only."""
|
|
@@ -0,0 +1,53 @@
|
|
|
1
|
+
"""One question to the model, with or without a lookup budget. Shared by every agent that
|
|
2
|
+
asks: specialists reviewing the change, and the refuter checking their claims."""
|
|
3
|
+
|
|
4
|
+
from __future__ import annotations
|
|
5
|
+
|
|
6
|
+
import logging
|
|
7
|
+
from typing import Any
|
|
8
|
+
|
|
9
|
+
from mlai_review.application import messages, prompts
|
|
10
|
+
from mlai_review.application.ports import LanguageModel, Message
|
|
11
|
+
from mlai_review.application.tools import LookupBudget, WorkspaceTools
|
|
12
|
+
|
|
13
|
+
_log = logging.getLogger("mlai.orchestrator.ask")
|
|
14
|
+
|
|
15
|
+
|
|
16
|
+
async def ask(
|
|
17
|
+
model: LanguageModel,
|
|
18
|
+
*,
|
|
19
|
+
system: str,
|
|
20
|
+
user: str,
|
|
21
|
+
tools: WorkspaceTools | None,
|
|
22
|
+
budget: LookupBudget | None,
|
|
23
|
+
who: str,
|
|
24
|
+
) -> dict[str, Any]:
|
|
25
|
+
"""The model's JSON answer. With tools and a budget, the model may look around first; the
|
|
26
|
+
budget bounds how far, after which it must answer without tools."""
|
|
27
|
+
if tools is None or budget is None:
|
|
28
|
+
return await model.complete_json(system=system, user=user)
|
|
29
|
+
conversation = [Message("system", system + prompts.LOOKUPS), Message("user", user)]
|
|
30
|
+
calls = spent = 0
|
|
31
|
+
nudge = prompts.LOOKUPS_EXHAUSTED
|
|
32
|
+
while calls < budget.max_calls and spent < budget.max_result_chars:
|
|
33
|
+
turn = await model.next_turn(tuple(conversation), tools.specs)
|
|
34
|
+
if turn.answer is not None:
|
|
35
|
+
_log.info(messages.LOOKUPS_USED, who, calls, spent)
|
|
36
|
+
return turn.answer
|
|
37
|
+
if not turn.tool_calls:
|
|
38
|
+
# It answered in prose instead of JSON: keep its words, ask for the object.
|
|
39
|
+
conversation.append(Message("assistant", turn.text))
|
|
40
|
+
nudge = prompts.ANSWER_AS_JSON
|
|
41
|
+
break
|
|
42
|
+
conversation.append(Message("assistant", tool_calls=turn.tool_calls))
|
|
43
|
+
for call in turn.tool_calls:
|
|
44
|
+
result = await tools.call(call)
|
|
45
|
+
calls += 1
|
|
46
|
+
spent += len(result)
|
|
47
|
+
conversation.append(Message("tool", result, tool_call_id=call.id))
|
|
48
|
+
# No more tools on offer, so the model must answer now, in JSON mode.
|
|
49
|
+
conversation.append(Message("user", nudge))
|
|
50
|
+
turn = await model.next_turn(tuple(conversation), ())
|
|
51
|
+
assert turn.answer is not None
|
|
52
|
+
_log.info(messages.LOOKUPS_USED, who, calls, spent)
|
|
53
|
+
return turn.answer
|
|
@@ -0,0 +1,142 @@
|
|
|
1
|
+
"""The refuter: a second opinion that tries to disprove each candidate finding before the
|
|
2
|
+
publication policy sees it. A claim the refuter breaks is not shown; one it cannot break is
|
|
3
|
+
shown at a lower confidence bar than an unchecked one."""
|
|
4
|
+
|
|
5
|
+
from __future__ import annotations
|
|
6
|
+
|
|
7
|
+
import asyncio
|
|
8
|
+
import logging
|
|
9
|
+
from abc import ABC, abstractmethod
|
|
10
|
+
from dataclasses import 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.errors import ReviewError
|
|
16
|
+
from mlai_review.application.ports import LanguageModel, Workspace
|
|
17
|
+
from mlai_review.application.tools import LookupBudget, WorkspaceTools
|
|
18
|
+
from mlai_review.domain.context import ReviewContext
|
|
19
|
+
from mlai_review.domain.findings import (
|
|
20
|
+
VALID_GROUNDS,
|
|
21
|
+
Challenge,
|
|
22
|
+
ChallengeOutcome,
|
|
23
|
+
Finding,
|
|
24
|
+
RefutationGround,
|
|
25
|
+
)
|
|
26
|
+
|
|
27
|
+
_log = logging.getLogger("mlai.orchestrator.refuter")
|
|
28
|
+
|
|
29
|
+
# A refutation the refuter itself doubts counts as "unsure", not as a refutation.
|
|
30
|
+
MIN_REFUTATION_CONFIDENCE = 0.5
|
|
31
|
+
|
|
32
|
+
|
|
33
|
+
class Challenger(ABC):
|
|
34
|
+
@abstractmethod
|
|
35
|
+
async def one(
|
|
36
|
+
self, context: ReviewContext, finding: Finding, workspace: Workspace | None
|
|
37
|
+
) -> Finding:
|
|
38
|
+
"""The finding, carrying the challenge's outcome where one was made."""
|
|
39
|
+
|
|
40
|
+
async def challenge(
|
|
41
|
+
self, context: ReviewContext, findings: list[Finding], workspace: Workspace | None
|
|
42
|
+
) -> list[Finding]:
|
|
43
|
+
return list(await asyncio.gather(*(self.one(context, f, workspace) for f in findings)))
|
|
44
|
+
|
|
45
|
+
|
|
46
|
+
class NoChallenger(Challenger):
|
|
47
|
+
async def one(
|
|
48
|
+
self, context: ReviewContext, finding: Finding, workspace: Workspace | None
|
|
49
|
+
) -> Finding:
|
|
50
|
+
return finding
|
|
51
|
+
|
|
52
|
+
|
|
53
|
+
class LlmChallenger(Challenger):
|
|
54
|
+
"""One model call per finding, all in parallel. A claim is about one place, so the refuter
|
|
55
|
+
sees the files it is about (its own, and any other changed file it names), not the whole
|
|
56
|
+
change: re-sending a 40,000-token change for every claim made the refuter most of a large
|
|
57
|
+
review's input. Claims about the same file share a prompt prefix, which providers cache. A
|
|
58
|
+
call that fails leaves its finding unchallenged: the policy then treats it as it would
|
|
59
|
+
without a refuter."""
|
|
60
|
+
|
|
61
|
+
def __init__(self, model: LanguageModel, lookups: LookupBudget | None = None) -> None:
|
|
62
|
+
self._model = model
|
|
63
|
+
self._lookups = lookups
|
|
64
|
+
|
|
65
|
+
async def one(
|
|
66
|
+
self, context: ReviewContext, finding: Finding, workspace: Workspace | None
|
|
67
|
+
) -> Finding:
|
|
68
|
+
tools = (
|
|
69
|
+
WorkspaceTools(workspace, self._lookups)
|
|
70
|
+
if workspace is not None and self._lookups is not None
|
|
71
|
+
else None
|
|
72
|
+
)
|
|
73
|
+
try:
|
|
74
|
+
scope = evidence_scope(context, finding)
|
|
75
|
+
answer = await self._challenge_one(prompts.user_message(scope), finding, tools)
|
|
76
|
+
except ReviewError as exc:
|
|
77
|
+
_log.warning(messages.CHALLENGE_FAILED, finding.title, exc)
|
|
78
|
+
return finding
|
|
79
|
+
challenge = _parse(answer)
|
|
80
|
+
_log.info(
|
|
81
|
+
messages.CHALLENGE_OUTCOME, challenge.outcome.value, finding.title, challenge.reason
|
|
82
|
+
)
|
|
83
|
+
return replace(finding, challenge=challenge)
|
|
84
|
+
|
|
85
|
+
async def _challenge_one(
|
|
86
|
+
self, user: str, finding: Finding, tools: WorkspaceTools | None
|
|
87
|
+
) -> dict[str, Any]:
|
|
88
|
+
return await ask(
|
|
89
|
+
self._model,
|
|
90
|
+
system=prompts.SHARED,
|
|
91
|
+
user=user + prompts.refute_task(finding),
|
|
92
|
+
tools=tools,
|
|
93
|
+
budget=self._lookups,
|
|
94
|
+
who="refuter",
|
|
95
|
+
)
|
|
96
|
+
|
|
97
|
+
|
|
98
|
+
# A change this small is sent whole: narrowing it saves little and loses context.
|
|
99
|
+
WHOLE_CHANGE_FILES = 3
|
|
100
|
+
MAX_SCOPE_FILES = 4
|
|
101
|
+
|
|
102
|
+
|
|
103
|
+
def evidence_scope(context: ReviewContext, finding: Finding) -> ReviewContext:
|
|
104
|
+
"""The part of the change a claim can be judged on: the file it points at, plus changed
|
|
105
|
+
files its words name (by file name), up to a few."""
|
|
106
|
+
files = context.changes.files
|
|
107
|
+
if len(files) <= WHOLE_CHANGE_FILES:
|
|
108
|
+
return context
|
|
109
|
+
told = f"{finding.title} {finding.explanation}".lower()
|
|
110
|
+
named = [
|
|
111
|
+
f.path for f in files if f.path != finding.path and _stem(f.path) and _stem(f.path) in told
|
|
112
|
+
]
|
|
113
|
+
paths = (finding.path, *named[: MAX_SCOPE_FILES - 1])
|
|
114
|
+
return context.part(paths)
|
|
115
|
+
|
|
116
|
+
|
|
117
|
+
def _stem(path: str) -> str:
|
|
118
|
+
""" "src/menu/menu.service.ts" -> "menu.service"; short stems ("a", "index") name nothing."""
|
|
119
|
+
name = path.rsplit("/", 1)[-1]
|
|
120
|
+
stem = name.rsplit(".", 1)[0].lower() if "." in name else name.lower()
|
|
121
|
+
return stem if len(stem) >= 6 and stem not in {"index", "module", "service", "utils"} else ""
|
|
122
|
+
|
|
123
|
+
|
|
124
|
+
def _parse(answer: dict[str, Any]) -> Challenge:
|
|
125
|
+
"""The refuter's verdict as typed values; anything unusable is 'unsure', never a
|
|
126
|
+
refutation, so a garbled answer cannot hide a real finding. A refutation stands only on
|
|
127
|
+
a valid ground: "the bug was already there" does not disprove a finding."""
|
|
128
|
+
try:
|
|
129
|
+
outcome = ChallengeOutcome(str(answer.get("verdict", "")).lower())
|
|
130
|
+
confidence = min(max(float(answer.get("confidence", 0.0)), 0.0), 1.0)
|
|
131
|
+
except (TypeError, ValueError):
|
|
132
|
+
return Challenge(ChallengeOutcome.UNSURE, "", 0.0)
|
|
133
|
+
reason = str(answer.get("reason", "")).strip()
|
|
134
|
+
ground: RefutationGround | None = None
|
|
135
|
+
if outcome is ChallengeOutcome.REFUTED:
|
|
136
|
+
try:
|
|
137
|
+
ground = RefutationGround(str(answer.get("ground", "other")).lower())
|
|
138
|
+
except ValueError:
|
|
139
|
+
ground = RefutationGround.OTHER
|
|
140
|
+
if confidence < MIN_REFUTATION_CONFIDENCE or ground not in VALID_GROUNDS:
|
|
141
|
+
outcome = ChallengeOutcome.UNSURE
|
|
142
|
+
return Challenge(outcome, reason, confidence, ground)
|