mlai-code 0.3.3 → 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.
Files changed (135) hide show
  1. package/README.md +1 -1
  2. package/package.json +1 -1
  3. package/src/checkpoints.mjs +217 -0
  4. package/src/cli.mjs +153 -26
  5. package/src/cloud.mjs +2 -0
  6. package/src/engine.mjs +1 -1
  7. package/src/headless.mjs +25 -9
  8. package/src/home.mjs +3 -2
  9. package/src/host.mjs +141 -13
  10. package/src/ide-quick.mjs +147 -0
  11. package/src/ide.mjs +182 -0
  12. package/src/netbox.mjs +1 -1
  13. package/src/parity.mjs +17 -0
  14. package/src/relay.mjs +1 -1
  15. package/src/render.mjs +9 -2
  16. package/src/runtime.mjs +2 -0
  17. package/src/server.mjs +46 -7
  18. package/src/session.mjs +31 -8
  19. package/src/tui/acp.mjs +121 -19
  20. package/src/tui/app.mjs +2 -1
  21. package/src/tui/chat.mjs +35 -10
  22. package/src/tui/cmd-config.mjs +3 -1
  23. package/src/tui/cmd-core.mjs +9 -5
  24. package/src/tui/cmd-extensibility.mjs +5 -0
  25. package/src/tui/extensions-local.mjs +2 -0
  26. package/src/tui/mcp-config.mjs +22 -4
  27. package/src/tui/review-cli.mjs +15 -4
  28. package/src/tui/snapshots.mjs +57 -16
  29. package/src/two-words.mjs +14 -8
  30. package/src/view-sync.mjs +48 -0
  31. package/src/winenv.mjs +1 -1
  32. package/src/workspace-api.mjs +21 -4
  33. package/vendor/client-sdk/index.cjs +2 -1
  34. package/vendor/cloud-cli/mlai.mjs +23 -11
  35. package/worker/architecture_worker.py +33 -4
  36. package/worker/browser_tool.py +19 -1
  37. package/worker/chat_fork.py +4 -1
  38. package/worker/checkpoints.py +71 -8
  39. package/worker/command_safety.py +56 -3
  40. package/worker/connected_turn.py +30 -0
  41. package/worker/conversation.py +81 -11
  42. package/worker/git_sync.py +4 -4
  43. package/worker/image_generation.py +2 -2
  44. package/worker/kimi_bridge.py +247 -27
  45. package/worker/kimi_gates.py +25 -6
  46. package/worker/kimi_hook.py +153 -12
  47. package/worker/media_tools.py +4 -1
  48. package/worker/mlai_hooks.py +222 -3
  49. package/worker/mlai_mcp.py +26 -6
  50. package/worker/mlai_review/README.md +35 -0
  51. package/worker/mlai_review/__init__.py +1 -0
  52. package/worker/mlai_review/__main__.py +261 -0
  53. package/worker/mlai_review/_compat.py +29 -0
  54. package/worker/mlai_review/analysis/__init__.py +3 -0
  55. package/worker/mlai_review/analysis/detect.py +50 -0
  56. package/worker/mlai_review/analysis/main.py +128 -0
  57. package/worker/mlai_review/analysis/messages.py +8 -0
  58. package/worker/mlai_review/analysis/tools.py +194 -0
  59. package/worker/mlai_review/application/__init__.py +1 -0
  60. package/worker/mlai_review/application/asking.py +53 -0
  61. package/worker/mlai_review/application/challengers.py +142 -0
  62. package/worker/mlai_review/application/context_builder.py +194 -0
  63. package/worker/mlai_review/application/errors.py +17 -0
  64. package/worker/mlai_review/application/feedback.py +52 -0
  65. package/worker/mlai_review/application/matching.py +46 -0
  66. package/worker/mlai_review/application/messages.py +41 -0
  67. package/worker/mlai_review/application/ports.py +222 -0
  68. package/worker/mlai_review/application/prompts.py +342 -0
  69. package/worker/mlai_review/application/review_service.py +200 -0
  70. package/worker/mlai_review/application/reviewers.py +438 -0
  71. package/worker/mlai_review/application/streaming.py +91 -0
  72. package/worker/mlai_review/application/tools.py +138 -0
  73. package/worker/mlai_review/application/usage.py +145 -0
  74. package/worker/mlai_review/application/write_check.py +46 -0
  75. package/worker/mlai_review/contracts/__init__.py +5 -0
  76. package/worker/mlai_review/contracts/analysis.py +57 -0
  77. package/worker/mlai_review/contracts/review.py +42 -0
  78. package/worker/mlai_review/domain/__init__.py +4 -0
  79. package/worker/mlai_review/domain/changes.py +77 -0
  80. package/worker/mlai_review/domain/context.py +147 -0
  81. package/worker/mlai_review/domain/diagnostics.py +239 -0
  82. package/worker/mlai_review/domain/feedback.py +216 -0
  83. package/worker/mlai_review/domain/findings.py +505 -0
  84. package/worker/mlai_review/domain/lifecycle.py +85 -0
  85. package/worker/mlai_review/domain/partition.py +42 -0
  86. package/worker/mlai_review/domain/relations.py +166 -0
  87. package/worker/mlai_review/domain/relevance.py +202 -0
  88. package/worker/mlai_review/domain/report.py +99 -0
  89. package/worker/mlai_review/domain/structure.py +56 -0
  90. package/worker/mlai_review/domain/writes.py +86 -0
  91. package/worker/mlai_review/eval.py +221 -0
  92. package/worker/mlai_review/eval_cases/clean-rename.json +11 -0
  93. package/worker/mlai_review/eval_cases/missing-ownership-check.json +22 -0
  94. package/worker/mlai_review/eval_cases/off-by-one.json +22 -0
  95. package/worker/mlai_review/eval_cases/sql-injection.json +20 -0
  96. package/worker/mlai_review/feedback_report.py +60 -0
  97. package/worker/mlai_review/infrastructure/__init__.py +1 -0
  98. package/worker/mlai_review/infrastructure/analysis/__init__.py +1 -0
  99. package/worker/mlai_review/infrastructure/analysis/none.py +12 -0
  100. package/worker/mlai_review/infrastructure/analysis/translate.py +17 -0
  101. package/worker/mlai_review/infrastructure/github/__init__.py +0 -0
  102. package/worker/mlai_review/infrastructure/github/client.py +445 -0
  103. package/worker/mlai_review/infrastructure/github/feedback.py +212 -0
  104. package/worker/mlai_review/infrastructure/github/http.py +31 -0
  105. package/worker/mlai_review/infrastructure/github/rendering.py +398 -0
  106. package/worker/mlai_review/infrastructure/github/token.py +20 -0
  107. package/worker/mlai_review/infrastructure/local/__init__.py +1 -0
  108. package/worker/mlai_review/infrastructure/local/analyzer.py +48 -0
  109. package/worker/mlai_review/infrastructure/local/events.py +100 -0
  110. package/worker/mlai_review/infrastructure/local/git_reader.py +233 -0
  111. package/worker/mlai_review/infrastructure/local/publisher.py +138 -0
  112. package/worker/mlai_review/infrastructure/messages.py +54 -0
  113. package/worker/mlai_review/infrastructure/models/__init__.py +0 -0
  114. package/worker/mlai_review/infrastructure/models/openai_compatible.py +269 -0
  115. package/worker/mlai_review/infrastructure/structure/__init__.py +1 -0
  116. package/worker/mlai_review/infrastructure/structure/heuristic.py +186 -0
  117. package/worker/mlai_review/infrastructure/structure/tree_sitter.py +186 -0
  118. package/worker/mlai_review/infrastructure/workspace/__init__.py +1 -0
  119. package/worker/mlai_review/infrastructure/workspace/local.py +105 -0
  120. package/worker/mlai_review/infrastructure/workspace/memory.py +77 -0
  121. package/worker/mlai_review/infrastructure/workspace/paths.py +17 -0
  122. package/worker/mlai_review/pipeline.py +89 -0
  123. package/worker/mlai_review/runtime.py +194 -0
  124. package/worker/mlai_review/settings.py +139 -0
  125. package/worker/preview.py +15 -0
  126. package/worker/product_facts.py +3 -1
  127. package/worker/repo_policy.py +5 -0
  128. package/worker/roles.py +29 -2
  129. package/worker/runner.py +126 -18
  130. package/worker/runtime_control.py +64 -3
  131. package/worker/secret_scan.py +3 -2
  132. package/worker/server_memory.py +122 -0
  133. package/worker/subagents.py +6 -2
  134. package/worker/tool_catalog.py +1 -1
  135. package/worker/verification.py +113 -8
@@ -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)
@@ -0,0 +1,239 @@
1
+ """What static-analysis tools said about the change, and how that becomes findings.
2
+
3
+ A tool's message on a line the change touches is evidence: deterministic, reproducible, and
4
+ already verified in the sense the PRD means. It is published as a finding of its own, marked
5
+ confirmed, and shown to the model so its own findings can build on it.
6
+
7
+ Not every message is worth a reviewer's comment. Formatting (Prettier, indentation, import
8
+ order) is the formatter's job and is dropped altogether. Untidiness (an unused variable, `let`
9
+ that could be `const`) is published as low severity, and a linter rule we do not know is
10
+ medium: only compilers and the curated Python checks speak with high severity.
11
+ """
12
+
13
+ from __future__ import annotations
14
+
15
+ from dataclasses import dataclass
16
+ from mlai_review._compat import StrEnum
17
+
18
+ from mlai_review.domain.changes import ChangeSet, changed_lines, lines_in_diff
19
+ from mlai_review.domain.findings import (
20
+ TOOL_CATEGORY,
21
+ Challenge,
22
+ ChallengeOutcome,
23
+ Finding,
24
+ Severity,
25
+ )
26
+
27
+ STATIC_ANALYSIS = TOOL_CATEGORY
28
+ # How many tool findings may be published per review on their own; the rest stay as context.
29
+ MAX_TOOL_FINDINGS = 5
30
+
31
+
32
+ class DiagnosticLevel(StrEnum):
33
+ ERROR = "error"
34
+ WARNING = "warning"
35
+
36
+
37
+ class DiagnosticKind(StrEnum):
38
+ FORMATTING = "formatting" # layout only: never shown, the formatter fixes it
39
+ HYGIENE = "hygiene" # untidy but working: low severity
40
+ DEFECT = "defect" # the code is wrong
41
+
42
+
43
+ # Tools whose errors are defects by construction: compilers, and ruff, whose errors the
44
+ # sandbox limits to undefined names, syntax errors and invalid constructs.
45
+ _CERTAIN_TOOLS = frozenset({"tsc", "ruff"})
46
+
47
+ # Rule names as linters report them, without a plugin prefix (`@typescript-eslint/indent` is
48
+ # `indent`). Formatting: how the code is laid out, never what it does.
49
+ _FORMATTING_RULES = frozenset(
50
+ {
51
+ "prettier",
52
+ "array-bracket-newline",
53
+ "array-bracket-spacing",
54
+ "array-element-newline",
55
+ "arrow-parens",
56
+ "arrow-spacing",
57
+ "block-spacing",
58
+ "brace-style",
59
+ "comma-dangle",
60
+ "comma-spacing",
61
+ "comma-style",
62
+ "computed-property-spacing",
63
+ "dot-location",
64
+ "eol-last",
65
+ "func-call-spacing",
66
+ "function-call-argument-newline",
67
+ "function-paren-newline",
68
+ "generator-star-spacing",
69
+ "implicit-arrow-linebreak",
70
+ "indent",
71
+ "jsx-quotes",
72
+ "key-spacing",
73
+ "keyword-spacing",
74
+ "linebreak-style",
75
+ "lines-around-comment",
76
+ "lines-between-class-members",
77
+ "max-len",
78
+ "max-statements-per-line",
79
+ "member-delimiter-style",
80
+ "multiline-ternary",
81
+ "new-parens",
82
+ "newline-per-chained-call",
83
+ "no-extra-parens",
84
+ "no-extra-semi",
85
+ "no-mixed-spaces-and-tabs",
86
+ "no-multi-spaces",
87
+ "no-multiple-empty-lines",
88
+ "no-tabs",
89
+ "no-trailing-spaces",
90
+ "no-whitespace-before-property",
91
+ "nonblock-statement-body-position",
92
+ "object-curly-newline",
93
+ "object-curly-spacing",
94
+ "object-property-newline",
95
+ "operator-linebreak",
96
+ "padded-blocks",
97
+ "padding-line-between-statements",
98
+ "quote-props",
99
+ "quotes",
100
+ "rest-spread-spacing",
101
+ "semi",
102
+ "semi-spacing",
103
+ "semi-style",
104
+ "space-before-blocks",
105
+ "space-before-function-paren",
106
+ "space-in-parens",
107
+ "space-infix-ops",
108
+ "space-unary-ops",
109
+ "spaced-comment",
110
+ "switch-colon-spacing",
111
+ "template-curly-spacing",
112
+ "type-annotation-spacing",
113
+ "wrap-regex",
114
+ "order", # import/order
115
+ "sort-imports",
116
+ }
117
+ )
118
+ # Plugins that only ever judge layout.
119
+ _FORMATTING_PLUGINS = frozenset({"prettier", "@stylistic", "simple-import-sort"})
120
+ # Untidy, not wrong.
121
+ _HYGIENE_RULES = frozenset(
122
+ {
123
+ "no-unused-vars",
124
+ "no-unused-expressions",
125
+ "unused-imports",
126
+ "no-unused-imports",
127
+ "no-unused-vars-experimental",
128
+ "prefer-const",
129
+ "no-var",
130
+ "no-console",
131
+ "no-debugger",
132
+ "no-explicit-any",
133
+ "no-empty",
134
+ "no-empty-function",
135
+ "no-empty-interface",
136
+ "no-inferrable-types",
137
+ "ban-ts-comment",
138
+ "no-useless-constructor",
139
+ "no-useless-escape",
140
+ "no-non-null-assertion",
141
+ "no-unnecessary-type-assertion",
142
+ # `any` flowing where a type was expected: weaker typing, not a bug by itself.
143
+ "no-unsafe-member-access",
144
+ "no-unsafe-assignment",
145
+ "no-unsafe-call",
146
+ "no-unsafe-return",
147
+ "no-unsafe-argument",
148
+ "consistent-type-imports",
149
+ "array-type",
150
+ "prefer-template",
151
+ "object-shorthand",
152
+ "F401", # Python: imported but unused
153
+ "F841", # Python: local variable assigned but never used
154
+ }
155
+ )
156
+ # TypeScript's "declared but never used" family, reported as errors under noUnused* options.
157
+ _TSC_UNUSED = frozenset({"TS6133", "TS6138", "TS6192", "TS6196", "TS6198", "TS6199", "TS6205"})
158
+ # Ruff (pycodestyle, isort, quotes, commas): layout codes by prefix.
159
+ _RUFF_FORMATTING_PREFIXES = ("E1", "E2", "E3", "E5", "W1", "W2", "W3", "W5", "I0", "Q0", "COM")
160
+
161
+
162
+ @dataclass(frozen=True, slots=True)
163
+ class Diagnostic:
164
+ tool: str
165
+ path: str
166
+ line: int
167
+ rule: str
168
+ message: str
169
+ level: DiagnosticLevel
170
+
171
+ @property
172
+ def title(self) -> str:
173
+ rule = f" ({self.rule})" if self.rule else ""
174
+ return f"{self.tool}{rule}: {self.message}"
175
+
176
+ @property
177
+ def kind(self) -> DiagnosticKind:
178
+ plugin, _, name = self.rule.rpartition("/")
179
+ if plugin.split("/")[0] in _FORMATTING_PLUGINS or name in _FORMATTING_RULES:
180
+ return DiagnosticKind.FORMATTING
181
+ if self.tool == "ruff" and self.rule.startswith(_RUFF_FORMATTING_PREFIXES):
182
+ return DiagnosticKind.FORMATTING
183
+ if name in _HYGIENE_RULES or plugin in _HYGIENE_RULES or self.rule in _TSC_UNUSED:
184
+ return DiagnosticKind.HYGIENE
185
+ return DiagnosticKind.DEFECT
186
+
187
+ @property
188
+ def severity(self) -> Severity:
189
+ if self.kind is not DiagnosticKind.DEFECT:
190
+ return Severity.LOW
191
+ return Severity.HIGH if self.tool in _CERTAIN_TOOLS else Severity.MEDIUM
192
+
193
+
194
+ def on_changed_lines(
195
+ diagnostics: tuple[Diagnostic, ...], changes: ChangeSet
196
+ ) -> tuple[Diagnostic, ...]:
197
+ """Only what the change is responsible for: messages on lines it added or touched. A
198
+ pre-existing warning three lines away is not this pull request's problem, and formatting
199
+ is nobody's review comment."""
200
+ kept = []
201
+ for change in changes.files:
202
+ touched = changed_lines(change.patch)
203
+ kept += [
204
+ d
205
+ for d in diagnostics
206
+ if d.path == change.path
207
+ and d.line in touched
208
+ and d.kind is not DiagnosticKind.FORMATTING
209
+ ]
210
+ return tuple(sorted(kept, key=lambda d: (d.path, d.line, d.tool)))
211
+
212
+
213
+ def findings_from(diagnostics: tuple[Diagnostic, ...], changes: ChangeSet) -> list[Finding]:
214
+ """Errors on changed lines, as findings the policy will publish, most severe first: a
215
+ compiler or linter error is a problem the change introduces, confirmed by the tool that
216
+ reported it, and as severe as what it is about."""
217
+ findings = []
218
+ for diagnostic in on_changed_lines(diagnostics, changes):
219
+ if diagnostic.level is not DiagnosticLevel.ERROR:
220
+ continue
221
+ change = changes.file(diagnostic.path)
222
+ findings.append(
223
+ Finding(
224
+ title=diagnostic.title,
225
+ severity=diagnostic.severity,
226
+ category=STATIC_ANALYSIS,
227
+ path=diagnostic.path,
228
+ line=diagnostic.line,
229
+ explanation=f"Reported by {diagnostic.tool} on this line.",
230
+ suggestion="",
231
+ confidence=1.0,
232
+ in_diff=diagnostic.line in lines_in_diff(change.patch if change else None),
233
+ challenge=Challenge(
234
+ ChallengeOutcome.CONFIRMED, f"reported by {diagnostic.tool}", 1.0
235
+ ),
236
+ )
237
+ )
238
+ findings.sort(key=lambda f: -f.severity.rank)
239
+ return findings[:MAX_TOOL_FINDINGS]
@@ -0,0 +1,216 @@
1
+ """What developers did with the comments a review posted, and what that says about the
2
+ reviewer.
3
+
4
+ Every signal is read back from the pull request itself: reactions, replies, whether the
5
+ thread was resolved, whether the flagged lines changed afterwards. Nothing is asked of the
6
+ developer beyond what they would do anyway, and nothing is stored that the host does not
7
+ already hold.
8
+ """
9
+
10
+ from __future__ import annotations
11
+
12
+ import re
13
+ import statistics
14
+ from dataclasses import dataclass, field
15
+ from datetime import datetime
16
+ from mlai_review._compat import StrEnum
17
+
18
+ # Replies that mean "this comment was wrong or not worth making".
19
+ _DISMISSAL = re.compile(
20
+ r"\b(intended|intentional(ly)?|by design|not a bug|false positive|won'?t fix|wontfix|"
21
+ r"expected behaviou?r|not an issue|not relevant|ignore (this|it))\b",
22
+ re.IGNORECASE,
23
+ )
24
+ MAX_REASON_CHARS = 200
25
+
26
+
27
+ class FeedbackVerdict(StrEnum):
28
+ USEFUL = "useful"
29
+ NOISE = "noise"
30
+ UNKNOWN = "unknown"
31
+
32
+
33
+ class FeedbackSignal(StrEnum):
34
+ THUMBS_UP = "thumbs_up"
35
+ THUMBS_DOWN = "thumbs_down"
36
+ DISMISSED_IN_REPLY = "dismissed_in_reply"
37
+ FIXED = "fixed" # the flagged lines changed afterwards and the issue was not reported again
38
+ RESOLVED_WITHOUT_CHANGE = "resolved_without_change" # ambiguous: agreed, or waved away
39
+ NONE = "none"
40
+
41
+
42
+ @dataclass(frozen=True, slots=True)
43
+ class PostedComment:
44
+ """One finding as it stands on the pull request now."""
45
+
46
+ comment_id: int
47
+ pull_number: int
48
+ path: str
49
+ fingerprint: str
50
+ title: str
51
+ created_at: datetime
52
+ thumbs_up: int = 0
53
+ thumbs_down: int = 0
54
+ # Replies written by people (not by ML AI), oldest first.
55
+ replies: tuple[str, ...] = ()
56
+ # The lines it sat on have changed since it was posted.
57
+ outdated: bool = False
58
+ # A later review said it looks resolved (lines changed, issue no longer reported).
59
+ fixed_by_push: bool = False
60
+ # None when the thread state could not be read.
61
+ thread_resolved: bool | None = None
62
+ # Posted as a possibility, not a verdict.
63
+ tentative: bool = False
64
+
65
+ def dismissal(self) -> str | None:
66
+ return next((r for r in self.replies if _DISMISSAL.search(r)), None)
67
+
68
+
69
+ @dataclass(frozen=True, slots=True)
70
+ class JudgedComment:
71
+ comment: PostedComment
72
+ verdict: FeedbackVerdict
73
+ signal: FeedbackSignal
74
+
75
+
76
+ def judge(comment: PostedComment) -> JudgedComment:
77
+ """Explicit votes decide when they are not tied; then what people wrote; then what
78
+ happened to the code. A resolved thread on unchanged code says nothing either way."""
79
+ c = comment
80
+ if c.thumbs_up != c.thumbs_down:
81
+ up = c.thumbs_up > c.thumbs_down
82
+ verdict = FeedbackVerdict.USEFUL if up else FeedbackVerdict.NOISE
83
+ signal = FeedbackSignal.THUMBS_UP if up else FeedbackSignal.THUMBS_DOWN
84
+ return JudgedComment(c, verdict, signal)
85
+ if c.dismissal() is not None:
86
+ return JudgedComment(c, FeedbackVerdict.NOISE, FeedbackSignal.DISMISSED_IN_REPLY)
87
+ if c.fixed_by_push or (c.thread_resolved and c.outdated):
88
+ return JudgedComment(c, FeedbackVerdict.USEFUL, FeedbackSignal.FIXED)
89
+ if c.thread_resolved:
90
+ return JudgedComment(c, FeedbackVerdict.UNKNOWN, FeedbackSignal.RESOLVED_WITHOUT_CHANGE)
91
+ return JudgedComment(c, FeedbackVerdict.UNKNOWN, FeedbackSignal.NONE)
92
+
93
+
94
+ @dataclass(frozen=True, slots=True)
95
+ class Dismissal:
96
+ """A finding the team said was not useful, and why: the reviewer should not raise the
97
+ same issue in the same file again, and should be wary of the same kind of issue."""
98
+
99
+ fingerprint: str
100
+ path: str
101
+ title: str
102
+ reason: str
103
+
104
+
105
+ def dismissals(comments: tuple[PostedComment, ...]) -> tuple[Dismissal, ...]:
106
+ """Every comment judged noise, newest first, once per issue."""
107
+ seen: set[str] = set()
108
+ out: list[Dismissal] = []
109
+ for c in sorted(comments, key=lambda c: c.created_at, reverse=True):
110
+ judged = judge(c)
111
+ if judged.verdict is not FeedbackVerdict.NOISE or c.fingerprint in seen:
112
+ continue
113
+ seen.add(c.fingerprint)
114
+ reason = c.dismissal() or "marked as not useful (thumbs down)"
115
+ out.append(Dismissal(c.fingerprint, c.path, c.title, reason[:MAX_REASON_CHARS]))
116
+ return tuple(out)
117
+
118
+
119
+ @dataclass(frozen=True, slots=True)
120
+ class ReviewRun:
121
+ pull_number: int
122
+ head_sha: str
123
+ started_at: datetime
124
+ completed_at: datetime
125
+ # False when the review could not complete.
126
+ completed: bool
127
+
128
+ @property
129
+ def seconds(self) -> float:
130
+ return (self.completed_at - self.started_at).total_seconds()
131
+
132
+
133
+ # What the internal trial agreed counts as good enough.
134
+ MIN_USEFUL_RATE = 0.70
135
+ MAX_NOISE_PER_PULL_REQUEST = 1.0
136
+ MAX_REVIEW_SECONDS = 600.0
137
+
138
+
139
+ @dataclass(frozen=True, slots=True)
140
+ class TrialCheck:
141
+ name: str
142
+ target: str
143
+ actual: str
144
+ met: bool | None # None: not enough signal yet
145
+
146
+
147
+ @dataclass(frozen=True, slots=True)
148
+ class FeedbackSummary:
149
+ comments: tuple[JudgedComment, ...] = ()
150
+ runs: tuple[ReviewRun, ...] = field(default_factory=tuple)
151
+
152
+ def count(self, verdict: FeedbackVerdict, *, tentative: bool | None = None) -> int:
153
+ return sum(
154
+ 1
155
+ for c in self.comments
156
+ if c.verdict is verdict and (tentative is None or c.comment.tentative is tentative)
157
+ )
158
+
159
+ def useful_rate_of(self, *, tentative: bool) -> float | None:
160
+ useful = self.count(FeedbackVerdict.USEFUL, tentative=tentative)
161
+ noise = self.count(FeedbackVerdict.NOISE, tentative=tentative)
162
+ return useful / (useful + noise) if useful + noise else None
163
+
164
+ @property
165
+ def pull_requests(self) -> int:
166
+ numbers = {r.pull_number for r in self.runs} | {
167
+ c.comment.pull_number for c in self.comments
168
+ }
169
+ return len(numbers)
170
+
171
+ @property
172
+ def useful_rate(self) -> float | None:
173
+ """Of the comments someone reacted to (or fixed), how many were useful."""
174
+ useful, noise = self.count(FeedbackVerdict.USEFUL), self.count(FeedbackVerdict.NOISE)
175
+ return useful / (useful + noise) if useful + noise else None
176
+
177
+ @property
178
+ def noise_per_pull_request(self) -> float | None:
179
+ prs = self.pull_requests
180
+ return self.count(FeedbackVerdict.NOISE) / prs if prs else None
181
+
182
+ @property
183
+ def failed_reviews(self) -> int:
184
+ return sum(1 for r in self.runs if not r.completed)
185
+
186
+ def review_seconds(self, quantile: float) -> float | None:
187
+ times = sorted(r.seconds for r in self.runs if r.completed)
188
+ if not times:
189
+ return None
190
+ if len(times) == 1:
191
+ return times[0]
192
+ return statistics.quantiles(times, n=100, method="inclusive")[int(quantile * 100) - 1]
193
+
194
+ def trial(self) -> tuple[TrialCheck, ...]:
195
+ rate, noise = self.useful_rate, self.noise_per_pull_request
196
+ slowest = max((r.seconds for r in self.runs if r.completed), default=None)
197
+ return (
198
+ TrialCheck(
199
+ "Comments rated useful",
200
+ f">= {MIN_USEFUL_RATE:.0%}",
201
+ "no ratings yet" if rate is None else f"{rate:.0%}",
202
+ None if rate is None else rate >= MIN_USEFUL_RATE,
203
+ ),
204
+ TrialCheck(
205
+ "Noise comments per pull request",
206
+ f"< {MAX_NOISE_PER_PULL_REQUEST:g}",
207
+ "no reviews yet" if noise is None else f"{noise:.2f}",
208
+ None if noise is None else noise < MAX_NOISE_PER_PULL_REQUEST,
209
+ ),
210
+ TrialCheck(
211
+ "Slowest review",
212
+ f"<= {MAX_REVIEW_SECONDS / 60:.0f} min",
213
+ "no reviews yet" if slowest is None else f"{slowest / 60:.1f} min",
214
+ None if slowest is None else slowest <= MAX_REVIEW_SECONDS,
215
+ ),
216
+ )