mlai-code 0.3.3 → 0.3.5
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +1 -1
- package/package.json +1 -1
- package/src/checkpoints.mjs +217 -0
- package/src/cli.mjs +153 -26
- package/src/cloud.mjs +2 -0
- package/src/engine.mjs +1 -1
- package/src/headless.mjs +39 -11
- package/src/home.mjs +3 -2
- package/src/host.mjs +141 -13
- package/src/ide-quick.mjs +147 -0
- package/src/ide.mjs +182 -0
- package/src/netbox.mjs +1 -1
- package/src/parity.mjs +17 -0
- package/src/relay.mjs +1 -1
- package/src/render.mjs +16 -5
- package/src/runtime.mjs +2 -0
- package/src/server.mjs +46 -7
- package/src/session.mjs +31 -8
- package/src/tui/acp.mjs +121 -19
- package/src/tui/app.mjs +2 -1
- package/src/tui/chat.mjs +35 -10
- package/src/tui/cmd-config.mjs +3 -1
- package/src/tui/cmd-core.mjs +9 -5
- package/src/tui/cmd-extensibility.mjs +5 -0
- package/src/tui/extensions-local.mjs +2 -0
- package/src/tui/mcp-config.mjs +22 -4
- package/src/tui/review-cli.mjs +15 -4
- package/src/tui/snapshots.mjs +57 -16
- package/src/tui/transcript.mjs +13 -2
- package/src/two-words.mjs +14 -8
- package/src/view-sync.mjs +48 -0
- package/src/winenv.mjs +1 -1
- package/src/workspace-api.mjs +21 -4
- package/vendor/client-sdk/index.cjs +2 -1
- package/vendor/cloud-cli/mlai.mjs +23 -11
- package/worker/architecture_worker.py +33 -4
- package/worker/browser_tool.py +19 -1
- package/worker/chat_fork.py +4 -1
- package/worker/checkpoints.py +71 -8
- package/worker/command_safety.py +56 -3
- package/worker/connected_turn.py +30 -0
- package/worker/conversation.py +125 -13
- package/worker/git_sync.py +4 -4
- package/worker/image_generation.py +2 -2
- package/worker/kimi_bridge.py +251 -31
- package/worker/kimi_gates.py +25 -6
- package/worker/kimi_hook.py +154 -13
- package/worker/media_tools.py +4 -1
- package/worker/mlai_hooks.py +222 -3
- package/worker/mlai_mcp.py +26 -6
- package/worker/mlai_review/README.md +35 -0
- package/worker/mlai_review/__init__.py +1 -0
- package/worker/mlai_review/__main__.py +261 -0
- package/worker/mlai_review/_compat.py +29 -0
- package/worker/mlai_review/analysis/__init__.py +3 -0
- package/worker/mlai_review/analysis/detect.py +50 -0
- package/worker/mlai_review/analysis/main.py +128 -0
- package/worker/mlai_review/analysis/messages.py +8 -0
- package/worker/mlai_review/analysis/tools.py +194 -0
- package/worker/mlai_review/application/__init__.py +1 -0
- package/worker/mlai_review/application/asking.py +53 -0
- package/worker/mlai_review/application/challengers.py +142 -0
- package/worker/mlai_review/application/context_builder.py +194 -0
- package/worker/mlai_review/application/errors.py +17 -0
- package/worker/mlai_review/application/feedback.py +52 -0
- package/worker/mlai_review/application/matching.py +46 -0
- package/worker/mlai_review/application/messages.py +41 -0
- package/worker/mlai_review/application/ports.py +222 -0
- package/worker/mlai_review/application/prompts.py +342 -0
- package/worker/mlai_review/application/review_service.py +200 -0
- package/worker/mlai_review/application/reviewers.py +438 -0
- package/worker/mlai_review/application/streaming.py +91 -0
- package/worker/mlai_review/application/tools.py +138 -0
- package/worker/mlai_review/application/usage.py +145 -0
- package/worker/mlai_review/application/write_check.py +46 -0
- package/worker/mlai_review/contracts/__init__.py +5 -0
- package/worker/mlai_review/contracts/analysis.py +57 -0
- package/worker/mlai_review/contracts/review.py +42 -0
- package/worker/mlai_review/domain/__init__.py +4 -0
- package/worker/mlai_review/domain/changes.py +77 -0
- package/worker/mlai_review/domain/context.py +147 -0
- package/worker/mlai_review/domain/diagnostics.py +239 -0
- package/worker/mlai_review/domain/feedback.py +216 -0
- package/worker/mlai_review/domain/findings.py +505 -0
- package/worker/mlai_review/domain/lifecycle.py +85 -0
- package/worker/mlai_review/domain/partition.py +42 -0
- package/worker/mlai_review/domain/relations.py +166 -0
- package/worker/mlai_review/domain/relevance.py +202 -0
- package/worker/mlai_review/domain/report.py +99 -0
- package/worker/mlai_review/domain/structure.py +56 -0
- package/worker/mlai_review/domain/writes.py +86 -0
- package/worker/mlai_review/eval.py +221 -0
- package/worker/mlai_review/eval_cases/clean-rename.json +11 -0
- package/worker/mlai_review/eval_cases/missing-ownership-check.json +22 -0
- package/worker/mlai_review/eval_cases/off-by-one.json +22 -0
- package/worker/mlai_review/eval_cases/sql-injection.json +20 -0
- package/worker/mlai_review/feedback_report.py +60 -0
- package/worker/mlai_review/infrastructure/__init__.py +1 -0
- package/worker/mlai_review/infrastructure/analysis/__init__.py +1 -0
- package/worker/mlai_review/infrastructure/analysis/none.py +12 -0
- package/worker/mlai_review/infrastructure/analysis/translate.py +17 -0
- package/worker/mlai_review/infrastructure/github/__init__.py +0 -0
- package/worker/mlai_review/infrastructure/github/client.py +445 -0
- package/worker/mlai_review/infrastructure/github/feedback.py +212 -0
- package/worker/mlai_review/infrastructure/github/http.py +31 -0
- package/worker/mlai_review/infrastructure/github/rendering.py +398 -0
- package/worker/mlai_review/infrastructure/github/token.py +20 -0
- package/worker/mlai_review/infrastructure/local/__init__.py +1 -0
- package/worker/mlai_review/infrastructure/local/analyzer.py +48 -0
- package/worker/mlai_review/infrastructure/local/events.py +100 -0
- package/worker/mlai_review/infrastructure/local/git_reader.py +233 -0
- package/worker/mlai_review/infrastructure/local/publisher.py +138 -0
- package/worker/mlai_review/infrastructure/messages.py +54 -0
- package/worker/mlai_review/infrastructure/models/__init__.py +0 -0
- package/worker/mlai_review/infrastructure/models/openai_compatible.py +269 -0
- package/worker/mlai_review/infrastructure/structure/__init__.py +1 -0
- package/worker/mlai_review/infrastructure/structure/heuristic.py +186 -0
- package/worker/mlai_review/infrastructure/structure/tree_sitter.py +186 -0
- package/worker/mlai_review/infrastructure/workspace/__init__.py +1 -0
- package/worker/mlai_review/infrastructure/workspace/local.py +105 -0
- package/worker/mlai_review/infrastructure/workspace/memory.py +77 -0
- package/worker/mlai_review/infrastructure/workspace/paths.py +17 -0
- package/worker/mlai_review/pipeline.py +89 -0
- package/worker/mlai_review/runtime.py +194 -0
- package/worker/mlai_review/settings.py +139 -0
- package/worker/preview.py +15 -0
- package/worker/product_facts.py +3 -1
- package/worker/repo_policy.py +5 -0
- package/worker/roles.py +29 -2
- package/worker/runner.py +164 -23
- package/worker/runtime_control.py +95 -9
- package/worker/secret_scan.py +3 -2
- package/worker/server_memory.py +122 -0
- package/worker/subagents.py +6 -2
- package/worker/tool_catalog.py +5 -2
- package/worker/verification.py +113 -8
|
@@ -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 "/")
|
|
@@ -0,0 +1,145 @@
|
|
|
1
|
+
"""What one review costs in model tokens. A review opens a meter; every model call made while
|
|
2
|
+
it is open, from any reading, refuter or matcher task the review started, adds to it. Reviews
|
|
3
|
+
running side by side on one worker each have their own meter."""
|
|
4
|
+
|
|
5
|
+
from __future__ import annotations
|
|
6
|
+
|
|
7
|
+
import math
|
|
8
|
+
from collections.abc import Iterator
|
|
9
|
+
from contextlib import contextmanager
|
|
10
|
+
from contextvars import ContextVar
|
|
11
|
+
from dataclasses import dataclass, field
|
|
12
|
+
from typing import Any
|
|
13
|
+
|
|
14
|
+
from mlai_review.application.errors import PermanentReviewError
|
|
15
|
+
|
|
16
|
+
# Credit weights, kept in step with lib/plans.ts and worker/runner.py billable_tokens.
|
|
17
|
+
CACHED_INPUT_WEIGHT = 0.2
|
|
18
|
+
OUTPUT_WEIGHT = 3
|
|
19
|
+
RATE_KEYS = ("input", "cached", "cacheWrite", "output")
|
|
20
|
+
|
|
21
|
+
|
|
22
|
+
class CreditsExhausted(PermanentReviewError):
|
|
23
|
+
"""The account's credits for this review are used up; no further model calls are made."""
|
|
24
|
+
|
|
25
|
+
|
|
26
|
+
@dataclass(slots=True)
|
|
27
|
+
class TokenUsage:
|
|
28
|
+
"""Model calls and their tokens, as the provider reported them."""
|
|
29
|
+
|
|
30
|
+
calls: int = 0
|
|
31
|
+
prompt_tokens: int = 0
|
|
32
|
+
cached_tokens: int = 0 # of the prompt tokens, those served from the provider's cache
|
|
33
|
+
completion_tokens: int = 0
|
|
34
|
+
billable: int = 0 # credit-weighted tokens, as the app meters a run
|
|
35
|
+
rates: dict[str, float] | None = field(default=None, repr=False)
|
|
36
|
+
budget: int | None = None # billable tokens this review may use; None is no cap
|
|
37
|
+
|
|
38
|
+
def add(
|
|
39
|
+
self,
|
|
40
|
+
prompt_tokens: int,
|
|
41
|
+
cached_tokens: int,
|
|
42
|
+
completion_tokens: int,
|
|
43
|
+
raw: dict[str, Any] | None = None,
|
|
44
|
+
) -> None:
|
|
45
|
+
self.calls += 1
|
|
46
|
+
self.prompt_tokens += prompt_tokens
|
|
47
|
+
self.cached_tokens += cached_tokens
|
|
48
|
+
self.completion_tokens += completion_tokens
|
|
49
|
+
reported = raw or {
|
|
50
|
+
"prompt_tokens": prompt_tokens,
|
|
51
|
+
"completion_tokens": completion_tokens,
|
|
52
|
+
"prompt_tokens_details": {"cached_tokens": cached_tokens},
|
|
53
|
+
}
|
|
54
|
+
self.billable += billable_tokens(reported, self.rates)
|
|
55
|
+
|
|
56
|
+
@property
|
|
57
|
+
def exhausted(self) -> bool:
|
|
58
|
+
return self.budget is not None and self.billable >= self.budget
|
|
59
|
+
|
|
60
|
+
|
|
61
|
+
def credit_rates(value: object) -> dict[str, float] | None:
|
|
62
|
+
"""Per-model weights from lib/model-prices.ts, or None for the default weights."""
|
|
63
|
+
if not isinstance(value, dict):
|
|
64
|
+
return None
|
|
65
|
+
out: dict[str, float] = {}
|
|
66
|
+
for key in RATE_KEYS:
|
|
67
|
+
v = value.get(key)
|
|
68
|
+
if not isinstance(v, (int, float)) or isinstance(v, bool) or not math.isfinite(v) or v < 0:
|
|
69
|
+
return None
|
|
70
|
+
out[key] = float(v)
|
|
71
|
+
return out
|
|
72
|
+
|
|
73
|
+
|
|
74
|
+
def billable_tokens(usage: object, rates: dict[str, float] | None = None) -> int:
|
|
75
|
+
"""One call's usage in billable tokens, exactly as lib/laptop-run.ts usageTokens and the
|
|
76
|
+
worker meter it: fresh input in full, cached a fifth, output three times, or per-model rates."""
|
|
77
|
+
if not isinstance(usage, dict):
|
|
78
|
+
return 0
|
|
79
|
+
|
|
80
|
+
def num(v: object) -> int:
|
|
81
|
+
return max(0, int(v)) if isinstance(v, (int, float)) and not isinstance(v, bool) else 0
|
|
82
|
+
|
|
83
|
+
read = num(usage.get("cache_read_input_tokens"))
|
|
84
|
+
if usage.get("prompt_tokens") is not None:
|
|
85
|
+
prompt = num(usage["prompt_tokens"])
|
|
86
|
+
else:
|
|
87
|
+
prompt = num(usage.get("input_tokens")) + read + num(usage.get("cache_creation_input_tokens"))
|
|
88
|
+
if usage.get("completion_tokens") is not None:
|
|
89
|
+
output = num(usage["completion_tokens"])
|
|
90
|
+
else:
|
|
91
|
+
output = num(usage.get("output_tokens"))
|
|
92
|
+
details = usage.get("prompt_tokens_details")
|
|
93
|
+
details = details if isinstance(details, dict) else {}
|
|
94
|
+
cached = min(prompt, num(details.get("cached_tokens")) or num(usage.get("cached_tokens")) or read)
|
|
95
|
+
rates = credit_rates(rates)
|
|
96
|
+
if not prompt and not output:
|
|
97
|
+
total = num(usage.get("total_tokens"))
|
|
98
|
+
return int(math.ceil(total * rates["output"])) if rates else total
|
|
99
|
+
if rates:
|
|
100
|
+
written = min(
|
|
101
|
+
prompt - cached,
|
|
102
|
+
num(usage.get("cache_creation_input_tokens")) or num(details.get("cache_write_tokens")),
|
|
103
|
+
)
|
|
104
|
+
return int(
|
|
105
|
+
math.ceil(
|
|
106
|
+
(prompt - cached - written) * rates["input"]
|
|
107
|
+
+ cached * rates["cached"]
|
|
108
|
+
+ written * rates["cacheWrite"]
|
|
109
|
+
+ output * rates["output"]
|
|
110
|
+
)
|
|
111
|
+
)
|
|
112
|
+
return int(math.ceil(prompt - cached + cached * CACHED_INPUT_WEIGHT + output * OUTPUT_WEIGHT))
|
|
113
|
+
|
|
114
|
+
|
|
115
|
+
_meter: ContextVar[tuple[TokenUsage, ...]] = ContextVar("mlai_token_usage", default=())
|
|
116
|
+
|
|
117
|
+
|
|
118
|
+
@contextmanager
|
|
119
|
+
def metering(
|
|
120
|
+
rates: dict[str, float] | None = None, budget: int | None = None
|
|
121
|
+
) -> Iterator[TokenUsage]:
|
|
122
|
+
"""A fresh meter for the calls made inside the block (and the tasks it starts). Meters
|
|
123
|
+
nest: a call counts against every meter open around it, so a caller can meter a whole
|
|
124
|
+
run while the review service meters each step. Inner meters weigh tokens like the outer one."""
|
|
125
|
+
outer = _meter.get()
|
|
126
|
+
usage = TokenUsage(rates=credit_rates(rates) or (outer[-1].rates if outer else None), budget=budget)
|
|
127
|
+
token = _meter.set((*_meter.get(), usage))
|
|
128
|
+
try:
|
|
129
|
+
yield usage
|
|
130
|
+
finally:
|
|
131
|
+
_meter.reset(token)
|
|
132
|
+
|
|
133
|
+
|
|
134
|
+
def record(
|
|
135
|
+
prompt_tokens: int, cached_tokens: int, completion_tokens: int, raw: dict[str, Any] | None = None
|
|
136
|
+
) -> None:
|
|
137
|
+
"""Count one model call against the open meter; outside a review it is not counted."""
|
|
138
|
+
for usage in _meter.get():
|
|
139
|
+
usage.add(prompt_tokens, cached_tokens, completion_tokens, raw)
|
|
140
|
+
|
|
141
|
+
|
|
142
|
+
def check_budget() -> None:
|
|
143
|
+
"""Before a model call: stop once any open meter has used its budget."""
|
|
144
|
+
if any(usage.exhausted for usage in _meter.get()):
|
|
145
|
+
raise CreditsExhausted("credits used up")
|
|
@@ -0,0 +1,46 @@
|
|
|
1
|
+
"""A narrow look at every write a change adds. General readings judge a write by what it is
|
|
2
|
+
meant to do; asked about each write on its own, with its filter tested against example rows,
|
|
3
|
+
the model finds the ones that touch the wrong rows (see domain.writes)."""
|
|
4
|
+
|
|
5
|
+
from __future__ import annotations
|
|
6
|
+
|
|
7
|
+
from abc import ABC, abstractmethod
|
|
8
|
+
from typing import Any
|
|
9
|
+
|
|
10
|
+
from mlai_review.application import prompts
|
|
11
|
+
from mlai_review.application.asking import ask
|
|
12
|
+
from mlai_review.application.ports import LanguageModel
|
|
13
|
+
from mlai_review.domain.context import ReviewContext
|
|
14
|
+
from mlai_review.domain.writes import writes_in
|
|
15
|
+
|
|
16
|
+
|
|
17
|
+
class WriteCheck(ABC):
|
|
18
|
+
@abstractmethod
|
|
19
|
+
async def answer(self, context: ReviewContext) -> dict[str, Any] | None:
|
|
20
|
+
"""Findings about the change's writes, in the readings' format; None when there was
|
|
21
|
+
nothing to check."""
|
|
22
|
+
|
|
23
|
+
|
|
24
|
+
class NoWriteCheck(WriteCheck):
|
|
25
|
+
async def answer(self, context: ReviewContext) -> dict[str, Any] | None:
|
|
26
|
+
return None
|
|
27
|
+
|
|
28
|
+
|
|
29
|
+
class LlmWriteCheck(WriteCheck):
|
|
30
|
+
"""One model call per change (or part), listing every write it adds."""
|
|
31
|
+
|
|
32
|
+
def __init__(self, model: LanguageModel) -> None:
|
|
33
|
+
self._model = model
|
|
34
|
+
|
|
35
|
+
async def answer(self, context: ReviewContext) -> dict[str, Any] | None:
|
|
36
|
+
sites = writes_in(context.changes)
|
|
37
|
+
if not sites:
|
|
38
|
+
return None
|
|
39
|
+
return await ask(
|
|
40
|
+
self._model,
|
|
41
|
+
system=prompts.SHARED,
|
|
42
|
+
user=prompts.user_message(context) + prompts.write_check_task(sites),
|
|
43
|
+
tools=None,
|
|
44
|
+
budget=None,
|
|
45
|
+
who="writes",
|
|
46
|
+
)
|
|
@@ -0,0 +1,57 @@
|
|
|
1
|
+
"""The contract between the orchestrator and the static-analysis sandbox: what the sandbox is
|
|
2
|
+
asked to analyse, and the diagnostics it reports back."""
|
|
3
|
+
|
|
4
|
+
from __future__ import annotations
|
|
5
|
+
|
|
6
|
+
from dataclasses import asdict, dataclass, field
|
|
7
|
+
from typing import Any
|
|
8
|
+
|
|
9
|
+
ANALYSIS_RESULT_VERSION = 1
|
|
10
|
+
|
|
11
|
+
|
|
12
|
+
@dataclass(frozen=True, slots=True)
|
|
13
|
+
class Diagnostic:
|
|
14
|
+
"""One message from one tool, on one line of one file."""
|
|
15
|
+
|
|
16
|
+
tool: str # ruff, tsc, eslint, ...
|
|
17
|
+
path: str # repository-relative, POSIX style
|
|
18
|
+
line: int # 1-based
|
|
19
|
+
rule: str # the tool's code for the message, e.g. F821, TS2322, no-unused-vars
|
|
20
|
+
message: str
|
|
21
|
+
level: str # "error" or "warning"
|
|
22
|
+
|
|
23
|
+
|
|
24
|
+
@dataclass(frozen=True, slots=True)
|
|
25
|
+
class AnalysisResult:
|
|
26
|
+
version: int = ANALYSIS_RESULT_VERSION
|
|
27
|
+
tools_run: tuple[str, ...] = ()
|
|
28
|
+
diagnostics: tuple[Diagnostic, ...] = ()
|
|
29
|
+
# Tools that could not run, with the reason: shown to operators, never to the model.
|
|
30
|
+
errors: dict[str, str] = field(default_factory=dict)
|
|
31
|
+
|
|
32
|
+
def as_dict(self) -> dict[str, Any]:
|
|
33
|
+
return {
|
|
34
|
+
"version": self.version,
|
|
35
|
+
"tools_run": list(self.tools_run),
|
|
36
|
+
"diagnostics": [asdict(d) for d in self.diagnostics],
|
|
37
|
+
"errors": dict(self.errors),
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
@classmethod
|
|
41
|
+
def from_dict(cls, data: dict[str, Any]) -> AnalysisResult:
|
|
42
|
+
return cls(
|
|
43
|
+
version=int(data.get("version", ANALYSIS_RESULT_VERSION)),
|
|
44
|
+
tools_run=tuple(str(t) for t in data.get("tools_run", [])),
|
|
45
|
+
diagnostics=tuple(
|
|
46
|
+
Diagnostic(
|
|
47
|
+
tool=str(d["tool"]),
|
|
48
|
+
path=str(d["path"]),
|
|
49
|
+
line=int(d["line"]),
|
|
50
|
+
rule=str(d.get("rule", "")),
|
|
51
|
+
message=str(d.get("message", "")),
|
|
52
|
+
level=str(d.get("level", "error")),
|
|
53
|
+
)
|
|
54
|
+
for d in data.get("diagnostics", [])
|
|
55
|
+
),
|
|
56
|
+
errors={str(k): str(v) for k, v in (data.get("errors") or {}).items()},
|
|
57
|
+
)
|
|
@@ -0,0 +1,42 @@
|
|
|
1
|
+
"""The contract for asking the orchestrator to review a pull request."""
|
|
2
|
+
|
|
3
|
+
from __future__ import annotations
|
|
4
|
+
|
|
5
|
+
from dataclasses import dataclass
|
|
6
|
+
|
|
7
|
+
REVIEW_WORKFLOW = "ReviewPullRequest"
|
|
8
|
+
REVIEW_TASK_QUEUE = "reviews"
|
|
9
|
+
# The name of the check the orchestrator posts; the gateway recognises re-run requests by it.
|
|
10
|
+
CHECK_NAME = "ML AI Review"
|
|
11
|
+
|
|
12
|
+
|
|
13
|
+
@dataclass(frozen=True, slots=True)
|
|
14
|
+
class ReviewPullRequestInput:
|
|
15
|
+
"""Everything the orchestrator needs to review one pull request at one head commit."""
|
|
16
|
+
|
|
17
|
+
installation_id: int
|
|
18
|
+
repository_id: int
|
|
19
|
+
repository_full_name: str
|
|
20
|
+
pull_number: int
|
|
21
|
+
head_sha: str
|
|
22
|
+
base_sha: str
|
|
23
|
+
triggered_by_delivery: str
|
|
24
|
+
# The head commit before this push (GitHub's `before` on synchronize), when known. Lets
|
|
25
|
+
# the orchestrator review only what changed since, if that commit was reviewed.
|
|
26
|
+
previous_head_sha: str | None = None
|
|
27
|
+
# Set when someone asked for the review in a comment ("@ml-ai-review review"): the
|
|
28
|
+
# orchestrator acknowledges that comment. Such requests carry no commit (head_sha and
|
|
29
|
+
# base_sha are empty): the orchestrator reviews the pull request's head as it is then.
|
|
30
|
+
requested_in_comment: int | None = None
|
|
31
|
+
|
|
32
|
+
@property
|
|
33
|
+
def needs_head(self) -> bool:
|
|
34
|
+
return not self.head_sha
|
|
35
|
+
|
|
36
|
+
@property
|
|
37
|
+
def owner(self) -> str:
|
|
38
|
+
return self.repository_full_name.split("/", 1)[0]
|
|
39
|
+
|
|
40
|
+
@property
|
|
41
|
+
def repo(self) -> str:
|
|
42
|
+
return self.repository_full_name.split("/", 1)[1]
|
|
@@ -0,0 +1,77 @@
|
|
|
1
|
+
"""What a pull request changed, and which lines of the new files the diff shows."""
|
|
2
|
+
|
|
3
|
+
from __future__ import annotations
|
|
4
|
+
|
|
5
|
+
import re
|
|
6
|
+
from dataclasses import dataclass, field
|
|
7
|
+
|
|
8
|
+
_HUNK = re.compile(r"^@@ -\d+(?:,\d+)? \+(\d+)(?:,(\d+))? @@")
|
|
9
|
+
|
|
10
|
+
|
|
11
|
+
@dataclass(frozen=True, slots=True)
|
|
12
|
+
class FileChange:
|
|
13
|
+
path: str
|
|
14
|
+
status: str # added, modified, removed, renamed, ...
|
|
15
|
+
additions: int
|
|
16
|
+
deletions: int
|
|
17
|
+
# Unified diff for this file; absent for binary files or very large diffs.
|
|
18
|
+
patch: str | None = None
|
|
19
|
+
|
|
20
|
+
|
|
21
|
+
@dataclass(frozen=True, slots=True)
|
|
22
|
+
class ChangeSet:
|
|
23
|
+
files: tuple[FileChange, ...] = field(default_factory=tuple)
|
|
24
|
+
# When set, the files are only what changed since this commit, not the whole pull request.
|
|
25
|
+
since: str | None = None
|
|
26
|
+
|
|
27
|
+
@property
|
|
28
|
+
def additions(self) -> int:
|
|
29
|
+
return sum(f.additions for f in self.files)
|
|
30
|
+
|
|
31
|
+
@property
|
|
32
|
+
def deletions(self) -> int:
|
|
33
|
+
return sum(f.deletions for f in self.files)
|
|
34
|
+
|
|
35
|
+
def file(self, path: str) -> FileChange | None:
|
|
36
|
+
return next((f for f in self.files if f.path == path), None)
|
|
37
|
+
|
|
38
|
+
|
|
39
|
+
@dataclass(frozen=True, slots=True)
|
|
40
|
+
class DiffLine:
|
|
41
|
+
kind: str # "+" added, "-" removed, " " unchanged context
|
|
42
|
+
# For added and context lines, their number in the new file. For removed lines, the number
|
|
43
|
+
# of the new-file line they now precede (so the removal can be shown in place).
|
|
44
|
+
new_line: int
|
|
45
|
+
text: str
|
|
46
|
+
|
|
47
|
+
|
|
48
|
+
def parse_patch(patch: str | None) -> tuple[DiffLine, ...]:
|
|
49
|
+
if not patch:
|
|
50
|
+
return ()
|
|
51
|
+
lines: list[DiffLine] = []
|
|
52
|
+
current = 0
|
|
53
|
+
for raw in patch.splitlines():
|
|
54
|
+
header = _HUNK.match(raw)
|
|
55
|
+
if header:
|
|
56
|
+
current = int(header.group(1))
|
|
57
|
+
elif raw.startswith("\\") or not current:
|
|
58
|
+
continue # "", or text before the first hunk
|
|
59
|
+
elif raw.startswith("-"):
|
|
60
|
+
lines.append(DiffLine("-", current, raw[1:]))
|
|
61
|
+
else:
|
|
62
|
+
kind = "+" if raw.startswith("+") else " "
|
|
63
|
+
lines.append(DiffLine(kind, current, raw[1:] if raw else ""))
|
|
64
|
+
current += 1
|
|
65
|
+
return tuple(lines)
|
|
66
|
+
|
|
67
|
+
|
|
68
|
+
def lines_in_diff(patch: str | None) -> frozenset[int]:
|
|
69
|
+
"""Line numbers of the new file that the diff shows: added lines and the unchanged context
|
|
70
|
+
around them. A finding on one of these lines can be pointed at in the diff itself."""
|
|
71
|
+
return frozenset(d.new_line for d in parse_patch(patch) if d.kind != "-")
|
|
72
|
+
|
|
73
|
+
|
|
74
|
+
def changed_lines(patch: str | None) -> frozenset[int]:
|
|
75
|
+
"""New-file line numbers where something changed: added lines, and the line each removal
|
|
76
|
+
now precedes. The smallest set of places a reviewer must look at."""
|
|
77
|
+
return frozenset(d.new_line for d in parse_patch(patch) if d.kind != " ")
|