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,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 != " ")
|
|
@@ -0,0 +1,147 @@
|
|
|
1
|
+
"""Everything a reviewer is shown about one pull request."""
|
|
2
|
+
|
|
3
|
+
from __future__ import annotations
|
|
4
|
+
|
|
5
|
+
from dataclasses import dataclass, field, replace
|
|
6
|
+
|
|
7
|
+
from mlai_review.domain.changes import ChangeSet, changed_lines, lines_in_diff
|
|
8
|
+
from mlai_review.domain.diagnostics import Diagnostic
|
|
9
|
+
from mlai_review.domain.feedback import Dismissal
|
|
10
|
+
from mlai_review.domain.relations import Relation
|
|
11
|
+
from mlai_review.domain.structure import Outline
|
|
12
|
+
|
|
13
|
+
# How a changed file is shown. A change inside a function shows the whole function; a function
|
|
14
|
+
# longer than this, or a change outside any function, shows a window around the change instead.
|
|
15
|
+
MAX_FUNCTION_LINES = 160
|
|
16
|
+
WINDOW = 8
|
|
17
|
+
# Regions this close together are shown as one.
|
|
18
|
+
MERGE_GAP = 4
|
|
19
|
+
# At most this many outline lines (imports, definition headers) per file.
|
|
20
|
+
MAX_SKELETON_LINES = 60
|
|
21
|
+
|
|
22
|
+
|
|
23
|
+
@dataclass(frozen=True, slots=True)
|
|
24
|
+
class PullRequestInfo:
|
|
25
|
+
title: str
|
|
26
|
+
body: str
|
|
27
|
+
|
|
28
|
+
|
|
29
|
+
@dataclass(frozen=True, slots=True)
|
|
30
|
+
class LineLocation:
|
|
31
|
+
path: str
|
|
32
|
+
line: int
|
|
33
|
+
# True when the diff shows this line, so a finding can be pointed at in the diff itself.
|
|
34
|
+
in_diff: bool
|
|
35
|
+
|
|
36
|
+
|
|
37
|
+
@dataclass(frozen=True, slots=True)
|
|
38
|
+
class Region:
|
|
39
|
+
start: int
|
|
40
|
+
end: int # inclusive
|
|
41
|
+
|
|
42
|
+
|
|
43
|
+
@dataclass(frozen=True, slots=True)
|
|
44
|
+
class FileView:
|
|
45
|
+
"""What the reviewer sees of one changed file: its outline, and the regions around the
|
|
46
|
+
change, instead of the whole text. The text itself stays here for rendering only."""
|
|
47
|
+
|
|
48
|
+
text: str
|
|
49
|
+
outline: Outline | None
|
|
50
|
+
regions: tuple[Region, ...]
|
|
51
|
+
skeleton: tuple[int, ...]
|
|
52
|
+
|
|
53
|
+
@property
|
|
54
|
+
def line_count(self) -> int:
|
|
55
|
+
return len(self.text.splitlines())
|
|
56
|
+
|
|
57
|
+
@property
|
|
58
|
+
def chars(self) -> int:
|
|
59
|
+
"""Size of what will be shown, for budgeting."""
|
|
60
|
+
lines = self.text.splitlines()
|
|
61
|
+
shown = {n for r in self.regions for n in range(r.start, r.end + 1)} | set(self.skeleton)
|
|
62
|
+
return sum(len(lines[n - 1]) + 8 for n in shown if n <= len(lines))
|
|
63
|
+
|
|
64
|
+
@classmethod
|
|
65
|
+
def focus(cls, text: str, patch: str | None, outline: Outline | None) -> FileView:
|
|
66
|
+
line_count = len(text.splitlines())
|
|
67
|
+
ranges: list[tuple[int, int]] = []
|
|
68
|
+
for line in sorted(changed_lines(patch)):
|
|
69
|
+
line = min(max(line, 1), max(line_count, 1))
|
|
70
|
+
function = outline.enclosing_function(line) if outline else None
|
|
71
|
+
if function is not None and function.lines <= MAX_FUNCTION_LINES:
|
|
72
|
+
ranges.append((function.start, function.end))
|
|
73
|
+
else:
|
|
74
|
+
ranges.append((max(1, line - WINDOW), min(line_count, line + WINDOW)))
|
|
75
|
+
skeleton = outline.skeleton(max_lines=MAX_SKELETON_LINES) if outline else ()
|
|
76
|
+
return cls(text, outline, _merge(ranges), skeleton)
|
|
77
|
+
|
|
78
|
+
|
|
79
|
+
def _merge(ranges: list[tuple[int, int]]) -> tuple[Region, ...]:
|
|
80
|
+
merged: list[Region] = []
|
|
81
|
+
for start, end in sorted(ranges):
|
|
82
|
+
if merged and start <= merged[-1].end + MERGE_GAP:
|
|
83
|
+
merged[-1] = Region(merged[-1].start, max(merged[-1].end, end))
|
|
84
|
+
else:
|
|
85
|
+
merged.append(Region(start, end))
|
|
86
|
+
return tuple(merged)
|
|
87
|
+
|
|
88
|
+
|
|
89
|
+
@dataclass(frozen=True, slots=True)
|
|
90
|
+
class ReviewContext:
|
|
91
|
+
pull_request: PullRequestInfo
|
|
92
|
+
# The files that get reviewed: the change set without noise.
|
|
93
|
+
changes: ChangeSet
|
|
94
|
+
# The reviewer's view of each changed file, keyed by path. May be partial: removed files,
|
|
95
|
+
# unreadable files and anything beyond the size budget are reviewed from their diff.
|
|
96
|
+
views: dict[str, FileView] = field(default_factory=dict)
|
|
97
|
+
# False when file contents could not be read at all (e.g. no Contents permission).
|
|
98
|
+
contents_available: bool = True
|
|
99
|
+
# Changed files left out as noise, for the record.
|
|
100
|
+
skipped: tuple[str, ...] = field(default_factory=tuple)
|
|
101
|
+
# Lines elsewhere in the repository that define what the change calls, or use what it
|
|
102
|
+
# changes. Found automatically; the reviewer can read further with its tools.
|
|
103
|
+
related: tuple[Relation, ...] = field(default_factory=tuple)
|
|
104
|
+
# What static-analysis tools reported on lines the change touches.
|
|
105
|
+
diagnostics: tuple[Diagnostic, ...] = field(default_factory=tuple)
|
|
106
|
+
# Earlier findings in this repository the team marked as not useful.
|
|
107
|
+
dismissed: tuple[Dismissal, ...] = field(default_factory=tuple)
|
|
108
|
+
# For one part of a change reviewed separately: the pull request's other changed files.
|
|
109
|
+
elsewhere: tuple[str, ...] = field(default_factory=tuple)
|
|
110
|
+
# The team's own guidance for reviews of this repository (its settings or .mlai/review.yml).
|
|
111
|
+
instructions: str = ""
|
|
112
|
+
|
|
113
|
+
def part(self, paths: tuple[str, ...]) -> ReviewContext:
|
|
114
|
+
"""The same review narrowed to some of its files: their changes, views and tool
|
|
115
|
+
messages, the related code that mentions what they mention, and the names of the
|
|
116
|
+
files left out."""
|
|
117
|
+
keep = set(paths)
|
|
118
|
+
files = tuple(f for f in self.changes.files if f.path in keep)
|
|
119
|
+
texts = [f.patch or "" for f in files] + [
|
|
120
|
+
self.views[f.path].text for f in files if f.path in self.views
|
|
121
|
+
]
|
|
122
|
+
return replace(
|
|
123
|
+
self,
|
|
124
|
+
changes=replace(self.changes, files=files),
|
|
125
|
+
views={p: v for p, v in self.views.items() if p in keep},
|
|
126
|
+
related=tuple(r for r in self.related if any(r.symbol in t for t in texts)),
|
|
127
|
+
diagnostics=tuple(d for d in self.diagnostics if d.path in keep),
|
|
128
|
+
elsewhere=tuple(f.path for f in self.changes.files if f.path not in keep),
|
|
129
|
+
)
|
|
130
|
+
|
|
131
|
+
def locate(self, path: str, line: int) -> LineLocation | None:
|
|
132
|
+
"""Where a claimed finding really is, or None if the claim points nowhere real.
|
|
133
|
+
|
|
134
|
+
A finding must name a file this change adds or modifies, and a line that exists in it.
|
|
135
|
+
When the file's text is unknown, only lines the diff shows can be checked, so only those
|
|
136
|
+
are accepted: an unverifiable location is never published.
|
|
137
|
+
"""
|
|
138
|
+
change = self.changes.file(path)
|
|
139
|
+
if change is None or change.status == "removed" or line < 1:
|
|
140
|
+
return None
|
|
141
|
+
in_diff = line in lines_in_diff(change.patch)
|
|
142
|
+
view = self.views.get(path)
|
|
143
|
+
if view is not None and line > view.line_count:
|
|
144
|
+
return None
|
|
145
|
+
if view is None and not in_diff:
|
|
146
|
+
return None
|
|
147
|
+
return LineLocation(path=path, line=line, in_diff=in_diff)
|