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.
Files changed (157) 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 +178 -31
  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 +20 -1
  14. package/src/relay.mjs +1 -1
  15. package/src/render.mjs +12 -3
  16. package/src/runtime.mjs +23 -1
  17. package/src/sandbox.mjs +19 -3
  18. package/src/server.mjs +46 -7
  19. package/src/session.mjs +53 -10
  20. package/src/shared-data.mjs +1 -1
  21. package/src/tui/acp.mjs +121 -19
  22. package/src/tui/app.mjs +24 -5
  23. package/src/tui/chat.mjs +43 -16
  24. package/src/tui/cmd-config.mjs +3 -1
  25. package/src/tui/cmd-core.mjs +11 -7
  26. package/src/tui/cmd-extensibility.mjs +13 -2
  27. package/src/tui/commands.mjs +2 -1
  28. package/src/tui/extensions-local.mjs +2 -0
  29. package/src/tui/mcp-config.mjs +22 -4
  30. package/src/tui/review-cli.mjs +15 -4
  31. package/src/tui/snapshots.mjs +57 -16
  32. package/src/tui/statusline.mjs +2 -1
  33. package/src/two-words.mjs +14 -8
  34. package/src/view-sync.mjs +48 -0
  35. package/src/wakeups.mjs +2 -2
  36. package/src/winenv.mjs +1 -1
  37. package/src/workspace-api.mjs +21 -4
  38. package/vendor/client-sdk/index.cjs +5 -4
  39. package/vendor/cloud-cli/mlai.mjs +184 -28
  40. package/worker/architecture_worker.py +33 -4
  41. package/worker/browser_tool.py +19 -1
  42. package/worker/build_planner.py +75 -7
  43. package/worker/chat_fork.py +4 -1
  44. package/worker/checkpoints.py +71 -8
  45. package/worker/cloud_pty.py +91 -2
  46. package/worker/command_safety.py +80 -3
  47. package/worker/compaction_brief.py +186 -0
  48. package/worker/connected_turn.py +30 -0
  49. package/worker/context_manager.py +6 -0
  50. package/worker/conversation.py +139 -12
  51. package/worker/deck_render.js +11 -0
  52. package/worker/deck_tools.py +825 -0
  53. package/worker/git_sync.py +42 -17
  54. package/worker/goals.py +4 -0
  55. package/worker/image_generation.py +102 -23
  56. package/worker/kimi_bridge.py +580 -52
  57. package/worker/kimi_gates.py +74 -6
  58. package/worker/kimi_hook.py +213 -19
  59. package/worker/media_tools.py +182 -0
  60. package/worker/memory.py +2 -2
  61. package/worker/mlai_hooks.py +314 -5
  62. package/worker/mlai_mcp.py +49 -11
  63. package/worker/mlai_review/README.md +35 -0
  64. package/worker/mlai_review/__init__.py +1 -0
  65. package/worker/mlai_review/__main__.py +261 -0
  66. package/worker/mlai_review/_compat.py +29 -0
  67. package/worker/mlai_review/analysis/__init__.py +3 -0
  68. package/worker/mlai_review/analysis/detect.py +50 -0
  69. package/worker/mlai_review/analysis/main.py +128 -0
  70. package/worker/mlai_review/analysis/messages.py +8 -0
  71. package/worker/mlai_review/analysis/tools.py +194 -0
  72. package/worker/mlai_review/application/__init__.py +1 -0
  73. package/worker/mlai_review/application/asking.py +53 -0
  74. package/worker/mlai_review/application/challengers.py +142 -0
  75. package/worker/mlai_review/application/context_builder.py +194 -0
  76. package/worker/mlai_review/application/errors.py +17 -0
  77. package/worker/mlai_review/application/feedback.py +52 -0
  78. package/worker/mlai_review/application/matching.py +46 -0
  79. package/worker/mlai_review/application/messages.py +41 -0
  80. package/worker/mlai_review/application/ports.py +222 -0
  81. package/worker/mlai_review/application/prompts.py +342 -0
  82. package/worker/mlai_review/application/review_service.py +200 -0
  83. package/worker/mlai_review/application/reviewers.py +438 -0
  84. package/worker/mlai_review/application/streaming.py +91 -0
  85. package/worker/mlai_review/application/tools.py +138 -0
  86. package/worker/mlai_review/application/usage.py +145 -0
  87. package/worker/mlai_review/application/write_check.py +46 -0
  88. package/worker/mlai_review/contracts/__init__.py +5 -0
  89. package/worker/mlai_review/contracts/analysis.py +57 -0
  90. package/worker/mlai_review/contracts/review.py +42 -0
  91. package/worker/mlai_review/domain/__init__.py +4 -0
  92. package/worker/mlai_review/domain/changes.py +77 -0
  93. package/worker/mlai_review/domain/context.py +147 -0
  94. package/worker/mlai_review/domain/diagnostics.py +239 -0
  95. package/worker/mlai_review/domain/feedback.py +216 -0
  96. package/worker/mlai_review/domain/findings.py +505 -0
  97. package/worker/mlai_review/domain/lifecycle.py +85 -0
  98. package/worker/mlai_review/domain/partition.py +42 -0
  99. package/worker/mlai_review/domain/relations.py +166 -0
  100. package/worker/mlai_review/domain/relevance.py +202 -0
  101. package/worker/mlai_review/domain/report.py +99 -0
  102. package/worker/mlai_review/domain/structure.py +56 -0
  103. package/worker/mlai_review/domain/writes.py +86 -0
  104. package/worker/mlai_review/eval.py +221 -0
  105. package/worker/mlai_review/eval_cases/clean-rename.json +11 -0
  106. package/worker/mlai_review/eval_cases/missing-ownership-check.json +22 -0
  107. package/worker/mlai_review/eval_cases/off-by-one.json +22 -0
  108. package/worker/mlai_review/eval_cases/sql-injection.json +20 -0
  109. package/worker/mlai_review/feedback_report.py +60 -0
  110. package/worker/mlai_review/infrastructure/__init__.py +1 -0
  111. package/worker/mlai_review/infrastructure/analysis/__init__.py +1 -0
  112. package/worker/mlai_review/infrastructure/analysis/none.py +12 -0
  113. package/worker/mlai_review/infrastructure/analysis/translate.py +17 -0
  114. package/worker/mlai_review/infrastructure/github/__init__.py +0 -0
  115. package/worker/mlai_review/infrastructure/github/client.py +445 -0
  116. package/worker/mlai_review/infrastructure/github/feedback.py +212 -0
  117. package/worker/mlai_review/infrastructure/github/http.py +31 -0
  118. package/worker/mlai_review/infrastructure/github/rendering.py +398 -0
  119. package/worker/mlai_review/infrastructure/github/token.py +20 -0
  120. package/worker/mlai_review/infrastructure/local/__init__.py +1 -0
  121. package/worker/mlai_review/infrastructure/local/analyzer.py +48 -0
  122. package/worker/mlai_review/infrastructure/local/events.py +100 -0
  123. package/worker/mlai_review/infrastructure/local/git_reader.py +233 -0
  124. package/worker/mlai_review/infrastructure/local/publisher.py +138 -0
  125. package/worker/mlai_review/infrastructure/messages.py +54 -0
  126. package/worker/mlai_review/infrastructure/models/__init__.py +0 -0
  127. package/worker/mlai_review/infrastructure/models/openai_compatible.py +269 -0
  128. package/worker/mlai_review/infrastructure/structure/__init__.py +1 -0
  129. package/worker/mlai_review/infrastructure/structure/heuristic.py +186 -0
  130. package/worker/mlai_review/infrastructure/structure/tree_sitter.py +186 -0
  131. package/worker/mlai_review/infrastructure/workspace/__init__.py +1 -0
  132. package/worker/mlai_review/infrastructure/workspace/local.py +105 -0
  133. package/worker/mlai_review/infrastructure/workspace/memory.py +77 -0
  134. package/worker/mlai_review/infrastructure/workspace/paths.py +17 -0
  135. package/worker/mlai_review/pipeline.py +89 -0
  136. package/worker/mlai_review/runtime.py +194 -0
  137. package/worker/mlai_review/settings.py +139 -0
  138. package/worker/model_stream.py +21 -7
  139. package/worker/offline_guard.py +1 -1
  140. package/worker/preview.py +62 -4
  141. package/worker/product_facts.py +20 -0
  142. package/worker/project_context.py +33 -7
  143. package/worker/reply_language.py +92 -10
  144. package/worker/repo_policy.py +5 -0
  145. package/worker/roles.json +199 -37
  146. package/worker/roles.py +271 -9
  147. package/worker/runner.py +366 -53
  148. package/worker/runtime_control.py +64 -3
  149. package/worker/secret_scan.py +3 -2
  150. package/worker/server_memory.py +122 -0
  151. package/worker/site_check.py +279 -0
  152. package/worker/skill_library.py +15 -3
  153. package/worker/skill_pack.py +170 -16
  154. package/worker/subagents.py +6 -2
  155. package/worker/tool_catalog.py +4 -3
  156. package/worker/verification.py +156 -19
  157. package/worker/wakeups.py +56 -4
@@ -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
+ )