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.
Files changed (136) 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 +39 -11
  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 +16 -5
  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/tui/transcript.mjs +13 -2
  30. package/src/two-words.mjs +14 -8
  31. package/src/view-sync.mjs +48 -0
  32. package/src/winenv.mjs +1 -1
  33. package/src/workspace-api.mjs +21 -4
  34. package/vendor/client-sdk/index.cjs +2 -1
  35. package/vendor/cloud-cli/mlai.mjs +23 -11
  36. package/worker/architecture_worker.py +33 -4
  37. package/worker/browser_tool.py +19 -1
  38. package/worker/chat_fork.py +4 -1
  39. package/worker/checkpoints.py +71 -8
  40. package/worker/command_safety.py +56 -3
  41. package/worker/connected_turn.py +30 -0
  42. package/worker/conversation.py +125 -13
  43. package/worker/git_sync.py +4 -4
  44. package/worker/image_generation.py +2 -2
  45. package/worker/kimi_bridge.py +251 -31
  46. package/worker/kimi_gates.py +25 -6
  47. package/worker/kimi_hook.py +154 -13
  48. package/worker/media_tools.py +4 -1
  49. package/worker/mlai_hooks.py +222 -3
  50. package/worker/mlai_mcp.py +26 -6
  51. package/worker/mlai_review/README.md +35 -0
  52. package/worker/mlai_review/__init__.py +1 -0
  53. package/worker/mlai_review/__main__.py +261 -0
  54. package/worker/mlai_review/_compat.py +29 -0
  55. package/worker/mlai_review/analysis/__init__.py +3 -0
  56. package/worker/mlai_review/analysis/detect.py +50 -0
  57. package/worker/mlai_review/analysis/main.py +128 -0
  58. package/worker/mlai_review/analysis/messages.py +8 -0
  59. package/worker/mlai_review/analysis/tools.py +194 -0
  60. package/worker/mlai_review/application/__init__.py +1 -0
  61. package/worker/mlai_review/application/asking.py +53 -0
  62. package/worker/mlai_review/application/challengers.py +142 -0
  63. package/worker/mlai_review/application/context_builder.py +194 -0
  64. package/worker/mlai_review/application/errors.py +17 -0
  65. package/worker/mlai_review/application/feedback.py +52 -0
  66. package/worker/mlai_review/application/matching.py +46 -0
  67. package/worker/mlai_review/application/messages.py +41 -0
  68. package/worker/mlai_review/application/ports.py +222 -0
  69. package/worker/mlai_review/application/prompts.py +342 -0
  70. package/worker/mlai_review/application/review_service.py +200 -0
  71. package/worker/mlai_review/application/reviewers.py +438 -0
  72. package/worker/mlai_review/application/streaming.py +91 -0
  73. package/worker/mlai_review/application/tools.py +138 -0
  74. package/worker/mlai_review/application/usage.py +145 -0
  75. package/worker/mlai_review/application/write_check.py +46 -0
  76. package/worker/mlai_review/contracts/__init__.py +5 -0
  77. package/worker/mlai_review/contracts/analysis.py +57 -0
  78. package/worker/mlai_review/contracts/review.py +42 -0
  79. package/worker/mlai_review/domain/__init__.py +4 -0
  80. package/worker/mlai_review/domain/changes.py +77 -0
  81. package/worker/mlai_review/domain/context.py +147 -0
  82. package/worker/mlai_review/domain/diagnostics.py +239 -0
  83. package/worker/mlai_review/domain/feedback.py +216 -0
  84. package/worker/mlai_review/domain/findings.py +505 -0
  85. package/worker/mlai_review/domain/lifecycle.py +85 -0
  86. package/worker/mlai_review/domain/partition.py +42 -0
  87. package/worker/mlai_review/domain/relations.py +166 -0
  88. package/worker/mlai_review/domain/relevance.py +202 -0
  89. package/worker/mlai_review/domain/report.py +99 -0
  90. package/worker/mlai_review/domain/structure.py +56 -0
  91. package/worker/mlai_review/domain/writes.py +86 -0
  92. package/worker/mlai_review/eval.py +221 -0
  93. package/worker/mlai_review/eval_cases/clean-rename.json +11 -0
  94. package/worker/mlai_review/eval_cases/missing-ownership-check.json +22 -0
  95. package/worker/mlai_review/eval_cases/off-by-one.json +22 -0
  96. package/worker/mlai_review/eval_cases/sql-injection.json +20 -0
  97. package/worker/mlai_review/feedback_report.py +60 -0
  98. package/worker/mlai_review/infrastructure/__init__.py +1 -0
  99. package/worker/mlai_review/infrastructure/analysis/__init__.py +1 -0
  100. package/worker/mlai_review/infrastructure/analysis/none.py +12 -0
  101. package/worker/mlai_review/infrastructure/analysis/translate.py +17 -0
  102. package/worker/mlai_review/infrastructure/github/__init__.py +0 -0
  103. package/worker/mlai_review/infrastructure/github/client.py +445 -0
  104. package/worker/mlai_review/infrastructure/github/feedback.py +212 -0
  105. package/worker/mlai_review/infrastructure/github/http.py +31 -0
  106. package/worker/mlai_review/infrastructure/github/rendering.py +398 -0
  107. package/worker/mlai_review/infrastructure/github/token.py +20 -0
  108. package/worker/mlai_review/infrastructure/local/__init__.py +1 -0
  109. package/worker/mlai_review/infrastructure/local/analyzer.py +48 -0
  110. package/worker/mlai_review/infrastructure/local/events.py +100 -0
  111. package/worker/mlai_review/infrastructure/local/git_reader.py +233 -0
  112. package/worker/mlai_review/infrastructure/local/publisher.py +138 -0
  113. package/worker/mlai_review/infrastructure/messages.py +54 -0
  114. package/worker/mlai_review/infrastructure/models/__init__.py +0 -0
  115. package/worker/mlai_review/infrastructure/models/openai_compatible.py +269 -0
  116. package/worker/mlai_review/infrastructure/structure/__init__.py +1 -0
  117. package/worker/mlai_review/infrastructure/structure/heuristic.py +186 -0
  118. package/worker/mlai_review/infrastructure/structure/tree_sitter.py +186 -0
  119. package/worker/mlai_review/infrastructure/workspace/__init__.py +1 -0
  120. package/worker/mlai_review/infrastructure/workspace/local.py +105 -0
  121. package/worker/mlai_review/infrastructure/workspace/memory.py +77 -0
  122. package/worker/mlai_review/infrastructure/workspace/paths.py +17 -0
  123. package/worker/mlai_review/pipeline.py +89 -0
  124. package/worker/mlai_review/runtime.py +194 -0
  125. package/worker/mlai_review/settings.py +139 -0
  126. package/worker/preview.py +15 -0
  127. package/worker/product_facts.py +3 -1
  128. package/worker/repo_policy.py +5 -0
  129. package/worker/roles.py +29 -2
  130. package/worker/runner.py +164 -23
  131. package/worker/runtime_control.py +95 -9
  132. package/worker/secret_scan.py +3 -2
  133. package/worker/server_memory.py +122 -0
  134. package/worker/subagents.py +6 -2
  135. package/worker/tool_catalog.py +5 -2
  136. package/worker/verification.py +113 -8
@@ -0,0 +1,200 @@
1
+ """The steps of reviewing one pull request. The workflow decides order, retries and timeouts;
2
+ each method here is one step and can run on any worker."""
3
+
4
+ from __future__ import annotations
5
+
6
+ import asyncio
7
+
8
+ from mlai_review._compat import TIMEOUTS
9
+ import logging
10
+ from contextlib import AsyncExitStack
11
+ from dataclasses import replace
12
+
13
+ from mlai_review.contracts.review import ReviewPullRequestInput
14
+ from mlai_review.application import messages
15
+ from mlai_review.application.context_builder import ContextBuilder
16
+ from mlai_review.application.errors import PermanentReviewError, ReviewError
17
+ from mlai_review.application.feedback import TeamFeedback
18
+ from mlai_review.application.ports import (
19
+ Repository,
20
+ ReviewPublisher,
21
+ StaticAnalyzer,
22
+ Workspace,
23
+ WorkspaceProvider,
24
+ )
25
+ from mlai_review.application.reviewers import Reviewer
26
+ from mlai_review.application.streaming import PublishingSink
27
+ from mlai_review.application.usage import metering
28
+ from mlai_review.domain.diagnostics import Diagnostic
29
+ from mlai_review.domain.feedback import Dismissal
30
+ from mlai_review.domain.lifecycle import reconcile
31
+ from mlai_review.domain.findings import Severity
32
+ from mlai_review.domain.relevance import ReviewLimits, without_ignored
33
+ from mlai_review.domain.report import ReviewReport
34
+
35
+ # How long a review waits for the static-analysis sandbox before going on without it.
36
+ ANALYSIS_WAIT_SECONDS = 180.0
37
+
38
+ _log = logging.getLogger("mlai.orchestrator.review")
39
+
40
+
41
+ class ReviewService:
42
+ def __init__(
43
+ self,
44
+ *,
45
+ context: ContextBuilder,
46
+ workspaces: WorkspaceProvider,
47
+ publisher: ReviewPublisher,
48
+ reviewer: Reviewer,
49
+ limits: ReviewLimits | None = None,
50
+ analyzer: StaticAnalyzer | None = None,
51
+ analysis_wait_seconds: float = ANALYSIS_WAIT_SECONDS,
52
+ feedback: TeamFeedback | None = None,
53
+ ignore: tuple[str, ...] = (),
54
+ min_severity: str = "low",
55
+ instructions: str = "",
56
+ ) -> None:
57
+ self._ignore = ignore
58
+ self._min_severity = Severity(min_severity) if min_severity in Severity._value2member_map_ else Severity.LOW
59
+ self._instructions = instructions
60
+ self._context = context
61
+ self._workspaces = workspaces
62
+ self._publisher = publisher
63
+ self._reviewer = reviewer
64
+ self._limits = limits or ReviewLimits()
65
+ self._analyzer = analyzer
66
+ self._analysis_wait = analysis_wait_seconds
67
+ self._feedback = feedback
68
+
69
+ async def resolve(self, target: ReviewPullRequestInput) -> ReviewPullRequestInput:
70
+ """A review asked for in a comment names no commit: review the head as it is now, and
71
+ acknowledge the comment."""
72
+ if not target.needs_head:
73
+ return target
74
+ head_sha, base_sha = await self._context.head(target)
75
+ resolved = replace(target, head_sha=head_sha, base_sha=base_sha)
76
+ await self._publisher.acknowledge(resolved)
77
+ return resolved
78
+
79
+ async def begin(self, target: ReviewPullRequestInput) -> int:
80
+ return await self._publisher.start(target)
81
+
82
+ async def analyse(self, target: ReviewPullRequestInput) -> ReviewReport:
83
+ """Gather the code and review it in one step, so file contents stay on this worker and
84
+ never travel through the workflow engine. Only the findings leave.
85
+
86
+ The checkout lives exactly as long as this step. If the repository cannot be read at
87
+ all, the review still happens, from the diffs alone.
88
+
89
+ What the step cost in model tokens is logged once per attempt, whether it succeeded or
90
+ not: the audit trail of spend per pull request.
91
+ """
92
+ with metering() as used:
93
+ try:
94
+ return await self._analyse(target)
95
+ finally:
96
+ _log.info(
97
+ messages.REVIEW_USAGE,
98
+ target.repository_full_name,
99
+ target.pull_number,
100
+ target.head_sha[:12],
101
+ used.calls,
102
+ used.prompt_tokens,
103
+ used.cached_tokens,
104
+ used.completion_tokens,
105
+ )
106
+
107
+ async def _analyse(self, target: ReviewPullRequestInput) -> ReviewReport:
108
+ since = await self._reviewed_before(target)
109
+ changes, ignored_count = without_ignored(
110
+ await self._context.changes(target, since=since), self._ignore
111
+ )
112
+ reviewable = changes.reviewable
113
+ extra = {"files_ignored": ignored_count, "changed": reviewable.files}
114
+ if not self._limits.allows(reviewable):
115
+ diff_lines = reviewable.additions + reviewable.deletions
116
+ _log.info(messages.CHANGE_TOO_LARGE, len(reviewable.files), diff_lines)
117
+ return replace(
118
+ ReviewReport.too_large(files=len(reviewable.files), diff_lines=diff_lines), **extra
119
+ )
120
+ # The sandbox clones and runs tools on its own; it starts now so its work overlaps the
121
+ # checkout and context building here.
122
+ analysis = self._start_analysis(target)
123
+ async with AsyncExitStack() as stack:
124
+ workspace: Workspace | None
125
+ try:
126
+ workspace = await stack.enter_async_context(self._workspaces.open(target))
127
+ except PermanentReviewError as exc:
128
+ _log.warning(messages.CONTENTS_UNAVAILABLE, exc)
129
+ workspace = None
130
+ diagnostics = await self._await_analysis(analysis)
131
+ context = await self._context.build(target, changes, workspace, diagnostics)
132
+ dismissed = await self._dismissed(target)
133
+ context = replace(context, dismissed=dismissed, instructions=self._instructions)
134
+ # Findings are commented on as they are confirmed. What is already on the pull
135
+ # request (an earlier review, or an earlier attempt of this step) is not repeated,
136
+ # and what the team dismissed is not raised at all.
137
+ prior = await self._publisher.prior_findings(target)
138
+ sink = PublishingSink(self._publisher, target, prior, dismissed, self._min_severity)
139
+ report = await self._reviewer.review(context, workspace, sink)
140
+ if report.dismissed:
141
+ _log.info(messages.DISMISSED_BY_FEEDBACK, report.dismissed)
142
+ resolved = reconcile(prior, tuple(sink.offered), context.changes).resolved
143
+ _log.info(messages.RECONCILED, len(report.findings), report.repeated, len(resolved))
144
+ return replace(
145
+ report, resolved=resolved, pr_title=context.pull_request.title, **extra
146
+ )
147
+
148
+ async def _dismissed(self, target: ReviewPullRequestInput) -> tuple[Dismissal, ...]:
149
+ if self._feedback is None:
150
+ return ()
151
+ repository = Repository(
152
+ target.repository_full_name, target.installation_id, target.repository_id
153
+ )
154
+ return await self._feedback.dismissed(repository)
155
+
156
+ def _start_analysis(
157
+ self, target: ReviewPullRequestInput
158
+ ) -> asyncio.Task[tuple[Diagnostic, ...]] | None:
159
+ if self._analyzer is None:
160
+ return None
161
+ return asyncio.create_task(self._analyzer.analyse(target))
162
+
163
+ async def _await_analysis(
164
+ self, analysis: asyncio.Task[tuple[Diagnostic, ...]] | None
165
+ ) -> tuple[Diagnostic, ...]:
166
+ """Tool output if it arrives in time; a review is never held hostage by the sandbox."""
167
+ if analysis is None:
168
+ return ()
169
+ try:
170
+ return await asyncio.wait_for(analysis, self._analysis_wait)
171
+ except TIMEOUTS:
172
+ _log.warning(messages.ANALYSIS_TIMED_OUT, self._analysis_wait)
173
+ except ReviewError as exc:
174
+ _log.warning(messages.ANALYSIS_FAILED, exc)
175
+ return ()
176
+
177
+ async def _reviewed_before(self, target: ReviewPullRequestInput) -> str | None:
178
+ """The previous head commit, if this push follows one we reviewed: then only what
179
+ changed since needs a look. Otherwise the whole pull request is reviewed."""
180
+ previous = target.previous_head_sha
181
+ if previous and previous != target.head_sha and await self._publisher.reviewed(target, previous):
182
+ _log.info(messages.INCREMENTAL_REVIEW, previous[:12])
183
+ return previous
184
+ # Several pushes since the last review (a skipped one, or a review asked for in a
185
+ # comment): the summary comment names the last commit that was actually reviewed. A
186
+ # commit no longer on the branch (a force push) falls back to the whole change.
187
+ last = await self._publisher.last_reviewed(target)
188
+ if last and last != target.head_sha:
189
+ _log.info(messages.INCREMENTAL_REVIEW, last[:12])
190
+ return last
191
+ return None
192
+
193
+ async def publish(
194
+ self, target: ReviewPullRequestInput, publication_id: int, report: ReviewReport
195
+ ) -> None:
196
+ await self._publisher.complete(target, publication_id, report)
197
+
198
+ async def abandon(self, target: ReviewPullRequestInput, publication_id: int) -> None:
199
+ """Close the in-progress publication so it never spins forever after a failed review."""
200
+ await self._publisher.complete(target, publication_id, ReviewReport.incomplete())
@@ -0,0 +1,438 @@
1
+ """Reviewers turn a review context into a report. New reviewers (security, data, tests) will
2
+ implement `Reviewer`; the workflow does not change when they arrive."""
3
+
4
+ from __future__ import annotations
5
+
6
+ import asyncio
7
+ import logging
8
+ from abc import ABC, abstractmethod
9
+ from collections import Counter
10
+ from dataclasses import dataclass, replace
11
+ from typing import Any
12
+
13
+ from mlai_review.application import messages, prompts
14
+ from mlai_review.application.asking import ask
15
+ from mlai_review.application.challengers import Challenger, NoChallenger
16
+ from mlai_review.application.matching import IssueMatcher, NoMatcher
17
+ from mlai_review.application.ports import LanguageModel, Workspace
18
+ from mlai_review.application.streaming import CollectingSink, FindingSink, Offer
19
+ from mlai_review.application.tools import LookupBudget, WorkspaceTools
20
+ from mlai_review.application.write_check import NoWriteCheck, WriteCheck
21
+ from mlai_review.domain.changes import lines_in_diff
22
+ from mlai_review.domain.context import ReviewContext
23
+ from mlai_review.domain.diagnostics import findings_from
24
+ from mlai_review.domain.findings import (
25
+ MAX_FIX_LINES,
26
+ Admission,
27
+ Finding,
28
+ Fix,
29
+ PublicationGate,
30
+ PublicationPolicy,
31
+ Severity,
32
+ counted,
33
+ issues_in,
34
+ join,
35
+ maybe_same,
36
+ )
37
+ from mlai_review.domain.partition import partition
38
+ from mlai_review.domain.report import ReviewReport
39
+
40
+ _log = logging.getLogger("mlai.orchestrator.reviewer")
41
+
42
+
43
+ class Reviewer(ABC):
44
+ @abstractmethod
45
+ async def review(
46
+ self,
47
+ context: ReviewContext,
48
+ workspace: Workspace | None = None,
49
+ sink: FindingSink | None = None,
50
+ ) -> ReviewReport:
51
+ """Judge the change. With a workspace, the reviewer may look around the checkout; each
52
+ finding goes to the sink as soon as it is confirmed."""
53
+
54
+
55
+ class PlaceholderReviewer(Reviewer):
56
+ """Proves the pipeline without judging the code."""
57
+
58
+ async def review(
59
+ self,
60
+ context: ReviewContext,
61
+ workspace: Workspace | None = None,
62
+ sink: FindingSink | None = None,
63
+ ) -> ReviewReport:
64
+ return ReviewReport.not_reviewed(
65
+ files_reviewed=len(context.changes.files), files_skipped=len(context.skipped)
66
+ )
67
+
68
+
69
+ @dataclass(frozen=True, slots=True)
70
+ class Specialist:
71
+ """One reviewing perspective: a name and the task that focuses the model on it."""
72
+
73
+ name: str
74
+ focus: str
75
+
76
+
77
+ GENERAL = Specialist("general", prompts.GENERAL)
78
+ SECURITY = Specialist("security", prompts.SECURITY)
79
+ SPECIALISTS = {s.name: s for s in (GENERAL, SECURITY)}
80
+
81
+
82
+ class LlmReviewer(Reviewer):
83
+ """Each specialist reviews the whole change in its own conversation, all in parallel, and
84
+ does so `passes` times independently: one reading of a large change notices a different
85
+ handful of issues each time, and the union of a few readings finds far more of them. With
86
+ a workspace and a lookup budget, a specialist may search and read the checkout before it
87
+ answers; the budget bounds how far it can wander. Each claim is held to the domain's rules
88
+ (a finding must point at a real line).
89
+
90
+ Once every reading is back, their claims are grouped by issue (by their words, then, for
91
+ nearby claims the words cannot settle, by asking the matcher), each marked with how many
92
+ readings reported it: agreement between independent readings is the best evidence the
93
+ reviewer has that an issue is real. Each issue is then challenged by the refuter once, and
94
+ put to the publication policy as soon as its challenge is back: the developer sees the first
95
+ finding while the rest are still being checked.
96
+
97
+ A change too large to read well at once (over `part_chars` rendered) is split into parts
98
+ of related files (see domain.partition), each reviewed on its own as above, all in parallel.
99
+
100
+ A reading that fails does not sink the review: the others still publish. Only when every
101
+ reading fails does the review fail, with the first error.
102
+ """
103
+
104
+ def __init__(
105
+ self,
106
+ model: LanguageModel,
107
+ policy: PublicationPolicy,
108
+ specialists: tuple[Specialist, ...] = (GENERAL,),
109
+ lookups: LookupBudget | None = None,
110
+ challenger: Challenger | None = None,
111
+ passes: int = 1,
112
+ matcher: IssueMatcher | None = None,
113
+ part_chars: int = 0,
114
+ write_check: WriteCheck | None = None,
115
+ ) -> None:
116
+ if passes < 1:
117
+ raise ValueError(messages.PASSES_AT_LEAST_ONE.format(passes=passes))
118
+ self._model = model
119
+ self._policy = policy
120
+ self._specialists = specialists
121
+ self._lookups = lookups
122
+ self._challenger = challenger or NoChallenger()
123
+ self._passes = passes
124
+ self._matcher = matcher or NoMatcher()
125
+ # Above this many characters of rendered change, the change is reviewed in parts; 0
126
+ # reviews it whole however large.
127
+ self._part_chars = part_chars
128
+ self._write_check = write_check or NoWriteCheck()
129
+
130
+ async def review(
131
+ self,
132
+ context: ReviewContext,
133
+ workspace: Workspace | None = None,
134
+ sink: FindingSink | None = None,
135
+ ) -> ReviewReport:
136
+ run = _Run(self._policy.gate(), sink or CollectingSink())
137
+ # Tool errors on changed lines are established facts: they go out first, except untidiness
138
+ # (low severity), which waits for the readings so it never takes a defect's place.
139
+ tool_findings = findings_from(context.diagnostics, context.changes)
140
+ for finding in tool_findings:
141
+ if finding.severity is not Severity.LOW:
142
+ await run.offer(finding)
143
+ tools = (
144
+ WorkspaceTools(workspace, self._lookups)
145
+ if workspace is not None and self._lookups is not None
146
+ else None
147
+ )
148
+ parts = self._parts(context)
149
+ if len(parts) > 1:
150
+ _log.info(messages.PARTS_LOG, len(parts), [len(p.changes.files) for p in parts])
151
+ outcomes = await asyncio.gather(
152
+ *(self._review_part(part, tools, workspace, run) for part in parts),
153
+ return_exceptions=True,
154
+ )
155
+ failures = [o for o in outcomes if isinstance(o, BaseException)]
156
+ if len(failures) == len(parts):
157
+ raise failures[0]
158
+ for failure in failures:
159
+ _log.warning(messages.PART_FAILED, failure)
160
+ for finding in tool_findings:
161
+ if finding.severity is Severity.LOW:
162
+ await run.offer(finding)
163
+ _log.info(
164
+ messages.REVIEW_OUTCOME,
165
+ run.claims,
166
+ run.rejected,
167
+ run.merged,
168
+ run.tally[Admission.REFUTED],
169
+ len(run.posted),
170
+ run.suppressed,
171
+ )
172
+ report = ReviewReport.of(
173
+ tuple(run.posted),
174
+ files_reviewed=len(context.changes.files),
175
+ suppressed=run.suppressed,
176
+ refuted=run.tally[Admission.REFUTED],
177
+ diff_only=not context.contents_available,
178
+ files_skipped=len(context.skipped),
179
+ since_commit=context.changes.since,
180
+ )
181
+ noted = sorted(run.noted, key=lambda f: (f.severity.rank, f.votes), reverse=True)
182
+ return replace(
183
+ report,
184
+ repeated=run.outcomes[Offer.REPEATED],
185
+ dismissed=run.outcomes[Offer.DISMISSED],
186
+ noted=tuple(noted),
187
+ below_minimum=run.outcomes[Offer.HELD],
188
+ )
189
+
190
+ def _parts(self, context: ReviewContext) -> list[ReviewContext]:
191
+ """The change as one part, or, when its rendering is over the budget, as parts of
192
+ related files reviewed separately."""
193
+ if self._part_chars <= 0 or len(prompts.user_message(context)) <= self._part_chars:
194
+ return [context]
195
+ sizes = {
196
+ f.path: len("\n".join(prompts.file_section(context, f))) for f in context.changes.files
197
+ }
198
+ groups = partition(sizes, self._part_chars)
199
+ if len(groups) == 1:
200
+ return [context] # every file fits in one part: nothing to gain by narrowing
201
+ return [context.part(paths) for paths in groups]
202
+
203
+ async def _review_part(
204
+ self,
205
+ part: ReviewContext,
206
+ tools: WorkspaceTools | None,
207
+ workspace: Workspace | None,
208
+ run: _Run,
209
+ ) -> None:
210
+ """Every reading of one part, its issues grouped and each challenged and offered."""
211
+ user = prompts.user_message(part)
212
+ readings = [(s, n) for n in range(1, self._passes + 1) for s in self._specialists]
213
+ outcomes, (writes,) = await asyncio.gather(
214
+ asyncio.gather(
215
+ *(self._read(s, n, user, tools, part, run) for s, n in readings),
216
+ return_exceptions=True,
217
+ ),
218
+ asyncio.gather(self._check_writes(part, run), return_exceptions=True),
219
+ )
220
+ failures = [o for o in outcomes if isinstance(o, BaseException)]
221
+ for (specialist, n), outcome in zip(readings, outcomes, strict=True):
222
+ if isinstance(outcome, BaseException):
223
+ _log.warning(messages.SPECIALIST_FAILED, _who(specialist, n), outcome)
224
+ if len(failures) == len(readings):
225
+ raise failures[0]
226
+ answered = [o for o in outcomes if not isinstance(o, BaseException)]
227
+ # The write check reads the change once more, looking only at its writes: what it
228
+ # reports counts like one more reading's claims.
229
+ if isinstance(writes, BaseException):
230
+ _log.warning(messages.WRITE_CHECK_FAILED, writes)
231
+ elif writes is not None:
232
+ answered.append(writes)
233
+ issues = await self._group(answered)
234
+ run.merged += sum(len(a) for a in answered) - len(issues)
235
+ votes = Counter(f.votes for f in issues)
236
+ _log.info(
237
+ messages.AGREEMENT_LOG,
238
+ len(answered),
239
+ " ".join(f"{v}:{votes[v]}" for v in sorted(votes, reverse=True)) or "-",
240
+ )
241
+
242
+ async def check(finding: Finding) -> None:
243
+ await run.offer(await self._challenger.one(part, finding, workspace))
244
+
245
+ await asyncio.gather(*(check(f) for f in issues))
246
+
247
+ async def _group(self, answered: list[list[Finding]]) -> list[Finding]:
248
+ """One finding per issue, marked with how many readings reported it."""
249
+ issues = issues_in(answered)
250
+ pairs = maybe_same(issues)
251
+ verdicts = await asyncio.gather(
252
+ *(self._matcher.same(issues[i].finding, issues[j].finding) for i, j in pairs)
253
+ )
254
+ same = [pair for pair, verdict in zip(pairs, verdicts, strict=True) if verdict]
255
+ if pairs:
256
+ _log.info(messages.MATCHED_LOG, len(pairs), len(same))
257
+ return counted(join(issues, same), len(answered))
258
+
259
+ async def _read(
260
+ self,
261
+ specialist: Specialist,
262
+ reading: int,
263
+ user: str,
264
+ tools: WorkspaceTools | None,
265
+ context: ReviewContext,
266
+ run: _Run,
267
+ ) -> list[Finding]:
268
+ """One independent reading: the claims that point at real lines."""
269
+ answer = await self._answer(specialist, reading, user, tools)
270
+ return self._claimed(answer, context, run)
271
+
272
+ async def _check_writes(self, context: ReviewContext, run: _Run) -> list[Finding] | None:
273
+ """The write check's claims that point at real lines; None when it had nothing to
274
+ check."""
275
+ answer = await self._write_check.answer(context)
276
+ return None if answer is None else self._claimed(answer, context, run)
277
+
278
+ def _claimed(self, answer: dict[str, Any], context: ReviewContext, run: _Run) -> list[Finding]:
279
+ claims = [c for raw in _findings_in(answer) if (c := _Claim.parse(raw)) is not None]
280
+ findings = [f for c in claims if (f := c.to_finding(context)) is not None]
281
+ run.claims += len(claims)
282
+ run.rejected += len(claims) - len(findings)
283
+ return findings
284
+
285
+ async def _answer(
286
+ self, specialist: Specialist, reading: int, user: str, tools: WorkspaceTools | None
287
+ ) -> dict[str, Any]:
288
+ return await ask(
289
+ self._model,
290
+ system=prompts.SHARED,
291
+ user=user + prompts.task(specialist.focus),
292
+ tools=tools,
293
+ budget=self._lookups,
294
+ who=_who(specialist, reading),
295
+ )
296
+
297
+
298
+ def _who(specialist: Specialist, reading: int) -> str:
299
+ return specialist.name if reading == 1 else f"{specialist.name}#{reading}"
300
+
301
+
302
+ class _Run:
303
+ """One review's progress: the gate decides, the sink publishes, one finding at a time so
304
+ duplicates and the budget are judged against what is really shown."""
305
+
306
+ def __init__(self, gate: PublicationGate, sink: FindingSink) -> None:
307
+ self._gate = gate
308
+ self._sink = sink
309
+ self._lock = asyncio.Lock()
310
+ self.tally: Counter[Admission] = Counter()
311
+ self.outcomes: Counter[Offer] = Counter()
312
+ self.posted: list[Finding] = []
313
+ self.noted: list[Finding] = []
314
+ self.claims = 0
315
+ self.rejected = 0
316
+ self.merged = 0
317
+
318
+ @property
319
+ def suppressed(self) -> int:
320
+ return sum(
321
+ self.tally[a] for a in (Admission.BELOW_BAR, Admission.DUPLICATE, Admission.OVER_BUDGET)
322
+ )
323
+
324
+ async def offer(self, finding: Finding) -> None:
325
+ async with self._lock:
326
+ admission = self._gate.decide(finding)
327
+ self.tally[admission] += 1
328
+ if admission is Admission.NOTE:
329
+ outcome = await self._sink.note(finding)
330
+ self.outcomes[outcome] += 1
331
+ if outcome is Offer.NOTED:
332
+ self._gate.note(finding)
333
+ self.noted.append(finding)
334
+ return
335
+ if admission is Admission.PUBLISH_TENTATIVE:
336
+ finding = replace(finding, tentative=True)
337
+ elif admission is not Admission.PUBLISH:
338
+ return
339
+ outcome = await self._sink.offer(finding)
340
+ self.outcomes[outcome] += 1
341
+ if outcome is not Offer.DISMISSED:
342
+ self._gate.admit(finding)
343
+ if outcome is Offer.POSTED:
344
+ self.posted.append(finding)
345
+ elif outcome is Offer.HELD:
346
+ self.noted.append(finding)
347
+
348
+
349
+ @dataclass(frozen=True, slots=True)
350
+ class _Claim:
351
+ """One finding as the model stated it, typed but not yet checked against the code."""
352
+
353
+ title: str
354
+ severity: Severity
355
+ category: str
356
+ path: str
357
+ line: int
358
+ explanation: str
359
+ suggestion: str
360
+ confidence: float
361
+ fix_lines: tuple[int, int] | None = None
362
+ fix_code: str = ""
363
+
364
+ @classmethod
365
+ def parse(cls, raw: Any) -> _Claim | None:
366
+ """Map the model's JSON (shaped by prompts.SHARED) to typed values; None if unusable."""
367
+ if not isinstance(raw, dict):
368
+ return None
369
+ try:
370
+ claim = cls(
371
+ title=str(raw["title"]).strip(),
372
+ severity=Severity(str(raw["severity"]).lower()),
373
+ category=str(raw.get("category", "other")),
374
+ path=str(raw["path"]).strip(),
375
+ line=int(raw["line"]),
376
+ explanation=str(raw.get("explanation", "")).strip(),
377
+ suggestion=str(raw.get("suggestion", "")).strip(),
378
+ confidence=min(max(float(raw.get("confidence", 0.0)), 0.0), 1.0),
379
+ **_fix_fields(raw.get("fix")),
380
+ )
381
+ except (KeyError, TypeError, ValueError):
382
+ return None
383
+ return claim if claim.title else None
384
+
385
+ def fix(self, context: ReviewContext) -> Fix | None:
386
+ """The claimed fix, if it names real lines of the file; otherwise nothing."""
387
+ if self.fix_lines is None or not self.fix_code.strip():
388
+ return None
389
+ start, end = self.fix_lines
390
+ if start > end or end - start + 1 > MAX_FIX_LINES:
391
+ return None
392
+ ends = (context.locate(self.path, start), context.locate(self.path, end))
393
+ if any(e is None for e in ends):
394
+ return None
395
+ change = context.changes.file(self.path)
396
+ shown = lines_in_diff(change.patch if change else None)
397
+ view = context.views.get(self.path)
398
+ text = view.text.splitlines() if view else []
399
+ return Fix(
400
+ start=start,
401
+ end=end,
402
+ code=self.fix_code.rstrip("\n"),
403
+ replaced=tuple(text[start - 1 : end]) if text else (),
404
+ suggestable=all(n in shown for n in range(start, end + 1)),
405
+ )
406
+
407
+ def to_finding(self, context: ReviewContext) -> Finding | None:
408
+ location = context.locate(self.path, self.line)
409
+ if location is None:
410
+ return None
411
+ return Finding(
412
+ title=self.title,
413
+ severity=self.severity,
414
+ category=self.category,
415
+ path=location.path,
416
+ line=location.line,
417
+ explanation=self.explanation,
418
+ suggestion=self.suggestion,
419
+ confidence=self.confidence,
420
+ in_diff=location.in_diff,
421
+ fix=self.fix(context),
422
+ )
423
+
424
+
425
+ def _fix_fields(raw: Any) -> dict[str, Any]:
426
+ """The fix as the model wrote it; a malformed one is dropped, not the whole finding."""
427
+ if not isinstance(raw, dict):
428
+ return {}
429
+ try:
430
+ first, last = (int(n) for n in raw["lines"])
431
+ return {"fix_lines": (first, last), "fix_code": str(raw.get("code", ""))}
432
+ except (KeyError, TypeError, ValueError):
433
+ return {}
434
+
435
+
436
+ def _findings_in(answer: dict[str, Any]) -> list[Any]:
437
+ findings = answer.get("findings")
438
+ return findings if isinstance(findings, list) else []