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,166 @@
1
+ """How a change relates to the rest of the repository: which definitions it touches, which
2
+ names it calls, and what a line of code must look like to be the definition of a name.
3
+ Pure text rules; finding the lines is the application's job."""
4
+
5
+ from __future__ import annotations
6
+
7
+ import re
8
+ from collections.abc import Iterable
9
+ from dataclasses import dataclass
10
+ from mlai_review._compat import StrEnum
11
+
12
+ from mlai_review.domain.structure import Outline
13
+
14
+ # Names too short or too common to be worth looking up.
15
+ MIN_NAME_LENGTH = 4
16
+ COMMON_NAMES = frozenset(
17
+ {
18
+ "self",
19
+ "this",
20
+ "super",
21
+ "print",
22
+ "len",
23
+ "str",
24
+ "int",
25
+ "float",
26
+ "bool",
27
+ "list",
28
+ "dict",
29
+ "set",
30
+ "tuple",
31
+ "range",
32
+ "enumerate",
33
+ "zip",
34
+ "map",
35
+ "filter",
36
+ "sorted",
37
+ "reversed",
38
+ "isinstance",
39
+ "hasattr",
40
+ "getattr",
41
+ "setattr",
42
+ "open",
43
+ "type",
44
+ "format",
45
+ "join",
46
+ "split",
47
+ "strip",
48
+ "append",
49
+ "extend",
50
+ "update",
51
+ "items",
52
+ "keys",
53
+ "values",
54
+ "get",
55
+ "pop",
56
+ "copy",
57
+ "push",
58
+ "then",
59
+ "catch",
60
+ "await",
61
+ "async",
62
+ "return",
63
+ "raise",
64
+ "throw",
65
+ "while",
66
+ "elif",
67
+ "else",
68
+ "with",
69
+ "lambda",
70
+ "yield",
71
+ "assert",
72
+ "except",
73
+ "function",
74
+ "require",
75
+ "import",
76
+ "export",
77
+ "console",
78
+ "Error",
79
+ "Exception",
80
+ "ValueError",
81
+ "TypeError",
82
+ "KeyError",
83
+ "Promise",
84
+ "Object",
85
+ "Array",
86
+ "String",
87
+ "Number",
88
+ "Boolean",
89
+ "JSON",
90
+ "Math",
91
+ "Date",
92
+ "logger",
93
+ "logging",
94
+ "test",
95
+ "describe",
96
+ "expect",
97
+ "pytest",
98
+ "fixture",
99
+ }
100
+ )
101
+
102
+ _CALL = re.compile(r"(?<![\w.])([A-Za-z_][A-Za-z0-9_]*)\s*\(")
103
+ _METHOD_CALL = re.compile(r"\.([A-Za-z_][A-Za-z0-9_]*)\s*\(")
104
+
105
+
106
+ class RelationKind(StrEnum):
107
+ DEFINITION = "definition" # where a name the change calls is defined
108
+ REFERENCE = "reference" # where a definition the change touches is used
109
+
110
+
111
+ @dataclass(frozen=True, slots=True)
112
+ class Relation:
113
+ kind: RelationKind
114
+ symbol: str
115
+ path: str
116
+ line: int
117
+ text: str
118
+
119
+
120
+ def worth_looking_up(name: str) -> bool:
121
+ return len(name) >= MIN_NAME_LENGTH and name not in COMMON_NAMES and not name.isupper()
122
+
123
+
124
+ def changed_definitions(outline: Outline, changed: Iterable[int]) -> tuple[str, ...]:
125
+ """Names of the functions and types whose body a change touches, innermost first."""
126
+ lines = set(changed)
127
+ touched = [d for d in outline.definitions if any(d.contains(n) for n in lines)]
128
+ names: list[str] = []
129
+ for definition in sorted(touched, key=lambda d: -d.depth):
130
+ if worth_looking_up(definition.name) and definition.name not in names:
131
+ names.append(definition.name)
132
+ return tuple(names)
133
+
134
+
135
+ def called_names(lines: Iterable[str], *, defined_here: Iterable[str] = ()) -> tuple[str, ...]:
136
+ """Names called in the given lines, in order of first use: plain calls and method calls,
137
+ minus what is defined in the same file and names not worth looking up."""
138
+ local = set(defined_here)
139
+ names: list[str] = []
140
+ for line in lines:
141
+ for match in (*_CALL.finditer(line), *_METHOD_CALL.finditer(line)):
142
+ name = match.group(1)
143
+ if worth_looking_up(name) and name not in local and name not in names:
144
+ names.append(name)
145
+ return tuple(names)
146
+
147
+
148
+ def definition_pattern(name: str) -> re.Pattern[str]:
149
+ """Matches a line that defines `name` in the common languages: a def/class/function/func/fn
150
+ keyword, a function bound to a const, an annotated method, or a class-body method."""
151
+ escaped = re.escape(name)
152
+ keyword = (
153
+ r"^\s*(?:export\s+)?(?:default\s+)?(?:pub(?:\([^)]*\))?\s+)?(?:async\s+)?"
154
+ r"(?:def|class|function\*?|func(?:\s*\([^)]*\))?|fn|struct|interface|trait|enum|type|"
155
+ rf"impl|module|object|record)\s+{escaped}\b"
156
+ )
157
+ bound = (
158
+ rf"^\s*(?:export\s+)?(?:const|let|var|val)\s+{escaped}\s*(?::[^=]+)?=\s*"
159
+ r"(?:async\s+)?(?:\(|function\b|[A-Za-z_]\w*\s*=>)"
160
+ )
161
+ annotated = (
162
+ r"^\s*(?:(?:public|private|protected|internal|static|final|override|abstract|virtual|"
163
+ rf"async|export)\s+)+[\w<>\[\],?.| ]*?\b{escaped}\s*\("
164
+ )
165
+ method = rf"^\s*(?:async\s+)?{escaped}\s*\([^)]*\)\s*(?::\s*[\w<>\[\], |?.]+)?\s*\{{\s*$"
166
+ return re.compile(f"(?:{keyword})|(?:{bound})|(?:{annotated})|(?:{method})")
@@ -0,0 +1,202 @@
1
+ """Which changed files deserve a reviewer's attention, and when a change is too large to review
2
+ at all. Pure rules over the change set; the numbers are defaults configuration may override."""
3
+
4
+ from __future__ import annotations
5
+
6
+ import fnmatch
7
+ import re
8
+ from dataclasses import dataclass
9
+
10
+ from mlai_review.domain.changes import ChangeSet
11
+
12
+ # Files no human reviewer reads either: lockfiles, vendored and generated code, build output,
13
+ # minified bundles, binaries and media. Each pattern matches the whole path or any suffix of it
14
+ # starting at a directory boundary, so "vendor/*" also covers "src/vendor/x.go".
15
+ NOISE_GLOBS: tuple[str, ...] = (
16
+ "*.lock",
17
+ "package-lock.json",
18
+ "yarn.lock",
19
+ "pnpm-lock.yaml",
20
+ "go.sum",
21
+ "node_modules/*",
22
+ "vendor/*",
23
+ "third_party/*",
24
+ "dist/*",
25
+ "__snapshots__/*",
26
+ "*.snap",
27
+ "*.min.js",
28
+ "*.min.css",
29
+ "*.map",
30
+ "*.bundle.js",
31
+ "*.generated.*",
32
+ "*_generated.*",
33
+ "*.pb.go",
34
+ "*_pb2.py",
35
+ "*_pb2_grpc.py",
36
+ "*.svg",
37
+ "*.png",
38
+ "*.jpg",
39
+ "*.jpeg",
40
+ "*.gif",
41
+ "*.ico",
42
+ "*.pdf",
43
+ "*.woff",
44
+ "*.woff2",
45
+ "*.ttf",
46
+ "*.zip",
47
+ "*.gz",
48
+ "*.jar",
49
+ "*.wasm",
50
+ "*.so",
51
+ "*.dylib",
52
+ "*.dll",
53
+ "*.exe",
54
+ "*.bin",
55
+ )
56
+
57
+
58
+ TEST_GLOBS: tuple[str, ...] = (
59
+ "tests/*",
60
+ "test/*",
61
+ "__tests__/*",
62
+ "spec/*",
63
+ "test_*.py",
64
+ "*_test.py",
65
+ "*_test.go",
66
+ "*.test.*",
67
+ "*.spec.*",
68
+ "*Test.java",
69
+ "*Tests.cs",
70
+ )
71
+
72
+
73
+ # Where code lives. Searches for related code look nowhere else: changelogs, CI config and
74
+ # documentation mention names without defining or calling them.
75
+ SOURCE_SUFFIXES: frozenset[str] = frozenset(
76
+ {
77
+ ".py",
78
+ ".pyi",
79
+ ".ts",
80
+ ".tsx",
81
+ ".js",
82
+ ".jsx",
83
+ ".mjs",
84
+ ".cjs",
85
+ ".go",
86
+ ".java",
87
+ ".kt",
88
+ ".kts",
89
+ ".rs",
90
+ ".rb",
91
+ ".cs",
92
+ ".php",
93
+ ".c",
94
+ ".h",
95
+ ".cc",
96
+ ".cpp",
97
+ ".hpp",
98
+ ".lua",
99
+ ".swift",
100
+ ".scala",
101
+ ".sql",
102
+ }
103
+ )
104
+
105
+
106
+ def is_source(path: str) -> bool:
107
+ dot = path.rfind(".")
108
+ return dot > path.rfind("/") and path[dot:].lower() in SOURCE_SUFFIXES
109
+
110
+
111
+ def is_test(path: str) -> bool:
112
+ return any(
113
+ fnmatch.fnmatchcase(path, glob) or fnmatch.fnmatchcase(path, f"*/{glob}")
114
+ for glob in TEST_GLOBS
115
+ )
116
+
117
+
118
+ def is_noise(path: str) -> bool:
119
+ return any(
120
+ fnmatch.fnmatchcase(path, glob) or fnmatch.fnmatchcase(path, f"*/{glob}")
121
+ for glob in NOISE_GLOBS
122
+ )
123
+
124
+
125
+ @dataclass(frozen=True, slots=True)
126
+ class Triage:
127
+ """The change set split into what gets reviewed and what was left out as noise."""
128
+
129
+ reviewable: ChangeSet
130
+ skipped: tuple[str, ...]
131
+
132
+
133
+ def triage(changes: ChangeSet) -> Triage:
134
+ kept = tuple(f for f in changes.files if not is_noise(f.path))
135
+ skipped = tuple(f.path for f in changes.files if is_noise(f.path))
136
+ return Triage(reviewable=ChangeSet(files=kept, since=changes.since), skipped=skipped)
137
+
138
+
139
+ @dataclass(frozen=True, slots=True)
140
+ class ReviewLimits:
141
+ """Above these, a change is not reviewed: a model cannot judge it well and the cost is
142
+ unbounded. The developer is told to split the pull request instead."""
143
+
144
+ max_files: int = 300
145
+ max_diff_lines: int = 20_000
146
+
147
+ def allows(self, changes: ChangeSet) -> bool:
148
+ return (
149
+ len(changes.files) <= self.max_files
150
+ and changes.additions + changes.deletions <= self.max_diff_lines
151
+ )
152
+
153
+
154
+ def _glob_regex(pattern: str) -> re.Pattern[str]:
155
+ out = []
156
+ i = 0
157
+ while i < len(pattern):
158
+ if pattern.startswith("**/", i):
159
+ out.append("(?:.*/)?")
160
+ i += 3
161
+ elif pattern.startswith("**", i):
162
+ out.append(".*")
163
+ i += 2
164
+ elif pattern[i] == "*":
165
+ out.append("[^/]*")
166
+ i += 1
167
+ elif pattern[i] == "?":
168
+ out.append("[^/]")
169
+ i += 1
170
+ else:
171
+ out.append(re.escape(pattern[i]))
172
+ i += 1
173
+ return re.compile("".join(out) + r"\Z")
174
+
175
+
176
+ def ignored(path: str, patterns: tuple[str, ...]) -> bool:
177
+ """Whether a repository's ignore list covers a path. `*` stays within a folder and `**`
178
+ crosses folders; a pattern without a slash matches the file name anywhere (like
179
+ .gitignore), and one ending in a slash covers everything under that folder."""
180
+ for raw in patterns:
181
+ pattern = raw.strip().lstrip("/")
182
+ if not pattern:
183
+ continue
184
+ if pattern.endswith("/"):
185
+ pattern += "**"
186
+ if "/" not in pattern:
187
+ if _glob_regex(pattern).match(path.rsplit("/", 1)[-1]):
188
+ return True
189
+ elif _glob_regex(pattern).match(path):
190
+ return True
191
+ return False
192
+
193
+
194
+ def without_ignored(result: Triage, patterns: tuple[str, ...]) -> tuple[Triage, int]:
195
+ """The triage with the repository's ignored paths set aside too, and how many there were."""
196
+ if not patterns:
197
+ return result, 0
198
+ files = result.reviewable.files
199
+ kept = tuple(f for f in files if not ignored(f.path, patterns))
200
+ dropped = tuple(f.path for f in files if ignored(f.path, patterns))
201
+ reviewable = ChangeSet(files=kept, since=result.reviewable.since)
202
+ return Triage(reviewable=reviewable, skipped=result.skipped), len(dropped)
@@ -0,0 +1,99 @@
1
+ """The outcome of reviewing a pull request, as data. How it is shown (a GitHub check, a comment,
2
+ a dashboard) is decided by whoever publishes it.
3
+
4
+ No `from __future__ import annotations` here: this type crosses the workflow boundary and
5
+ Temporal's sandbox must resolve its field types as real objects, not strings.
6
+ """
7
+
8
+ from dataclasses import dataclass, field
9
+ from mlai_review._compat import StrEnum
10
+ from mlai_review._compat import Self
11
+
12
+ from mlai_review.domain.changes import FileChange
13
+ from mlai_review.domain.findings import Finding
14
+ from mlai_review.domain.lifecycle import PriorFinding
15
+
16
+
17
+ class Verdict(StrEnum):
18
+ ISSUES_FOUND = "issues_found"
19
+ NO_ISSUES = "no_issues"
20
+ # The pipeline ran without judging the code (no reviewer configured).
21
+ NOT_REVIEWED = "not_reviewed"
22
+ # The change is above the review limits; the developer should split it.
23
+ TOO_LARGE = "too_large"
24
+ # The review failed before producing results.
25
+ INCOMPLETE = "incomplete"
26
+
27
+
28
+ @dataclass(frozen=True, slots=True)
29
+ class ReviewReport:
30
+ verdict: Verdict
31
+ # Findings the developer should see, most important first.
32
+ findings: tuple[Finding, ...] = field(default_factory=tuple)
33
+ files_reviewed: int = 0
34
+ # Changed files left out as noise (lockfiles, generated or vendored code).
35
+ files_skipped: int = 0
36
+ # Added plus deleted lines; reported when the change was too large.
37
+ diff_lines: int = 0
38
+ # Candidates held back by the publication policy.
39
+ suppressed: int = 0
40
+ # Candidates the refuter disproved.
41
+ refuted: int = 0
42
+ # True when the reviewer saw only diffs, not whole files.
43
+ diff_only: bool = False
44
+ # Set when only the changes since this commit were reviewed (an earlier push was).
45
+ since_commit: str | None = None
46
+ # Findings commented on by an earlier review and still reported: not commented again.
47
+ repeated: int = 0
48
+ # Earlier findings whose lines this push changed and which are no longer reported.
49
+ resolved: tuple[PriorFinding, ...] = field(default_factory=tuple)
50
+ # Findings left out because the team dismissed the same issue before.
51
+ dismissed: int = 0
52
+ # Not commented on but worth a look: listed in the summary, most severe first.
53
+ noted: tuple[Finding, ...] = field(default_factory=tuple)
54
+ # Changed files the repository's settings told the reviewer to skip (ignored paths).
55
+ files_ignored: int = 0
56
+ # The files this review looked at (the whole change, or only what changed since
57
+ # since_commit), for the summary comment's walkthrough.
58
+ changed: tuple[FileChange, ...] = field(default_factory=tuple)
59
+ # The pull request's title, for the walkthrough.
60
+ pr_title: str = ""
61
+ # Findings below the repository's minimum severity: listed in the summary, not commented.
62
+ below_minimum: int = 0
63
+
64
+ @classmethod
65
+ def of(
66
+ cls,
67
+ findings: tuple[Finding, ...],
68
+ *,
69
+ files_reviewed: int,
70
+ suppressed: int,
71
+ diff_only: bool,
72
+ files_skipped: int = 0,
73
+ since_commit: str | None = None,
74
+ refuted: int = 0,
75
+ ) -> Self:
76
+ return cls(
77
+ verdict=Verdict.ISSUES_FOUND if findings else Verdict.NO_ISSUES,
78
+ findings=findings,
79
+ files_reviewed=files_reviewed,
80
+ files_skipped=files_skipped,
81
+ suppressed=suppressed,
82
+ refuted=refuted,
83
+ diff_only=diff_only,
84
+ since_commit=since_commit,
85
+ )
86
+
87
+ @classmethod
88
+ def not_reviewed(cls, *, files_reviewed: int, files_skipped: int = 0) -> Self:
89
+ return cls(
90
+ verdict=Verdict.NOT_REVIEWED, files_reviewed=files_reviewed, files_skipped=files_skipped
91
+ )
92
+
93
+ @classmethod
94
+ def too_large(cls, *, files: int, diff_lines: int) -> Self:
95
+ return cls(verdict=Verdict.TOO_LARGE, files_reviewed=files, diff_lines=diff_lines)
96
+
97
+ @classmethod
98
+ def incomplete(cls) -> Self:
99
+ return cls(verdict=Verdict.INCOMPLETE)
@@ -0,0 +1,56 @@
1
+ """The shape of a source file: where its definitions and imports are. Language parsing is an
2
+ adapter's job; this is what the rest of the domain needs to know about the result."""
3
+
4
+ from __future__ import annotations
5
+
6
+ from dataclasses import dataclass
7
+ from mlai_review._compat import StrEnum
8
+
9
+
10
+ class DefinitionKind(StrEnum):
11
+ FUNCTION = "function" # functions, methods, constructors, lambdas bound to a name
12
+ TYPE = "type" # classes, structs, enums, traits, interfaces, modules
13
+
14
+
15
+ @dataclass(frozen=True, slots=True)
16
+ class Definition:
17
+ kind: DefinitionKind
18
+ name: str
19
+ start: int # first line, 1-based
20
+ end: int # last line, inclusive
21
+ depth: int # 0 at top level, +1 per enclosing definition
22
+
23
+ @property
24
+ def lines(self) -> int:
25
+ return self.end - self.start + 1
26
+
27
+ def contains(self, line: int) -> bool:
28
+ return self.start <= line <= self.end
29
+
30
+
31
+ @dataclass(frozen=True, slots=True)
32
+ class Outline:
33
+ # In source order; nested definitions follow their parent.
34
+ definitions: tuple[Definition, ...] = ()
35
+ # Lines holding import statements.
36
+ imports: tuple[int, ...] = ()
37
+
38
+ def enclosing_function(self, line: int) -> Definition | None:
39
+ """The innermost function or method containing the line, if any."""
40
+ inner = [
41
+ d for d in self.definitions if d.kind is DefinitionKind.FUNCTION and d.contains(line)
42
+ ]
43
+ return max(inner, key=lambda d: d.depth) if inner else None
44
+
45
+ def skeleton(self, *, max_lines: int) -> tuple[int, ...]:
46
+ """Lines that show the file's shape: imports and definition headers. When there are
47
+ more than fit, nested definitions go first, then the deepest of what remains."""
48
+ lines = sorted({*self.imports, *(d.start for d in self.definitions)})
49
+ if len(lines) <= max_lines:
50
+ return tuple(lines)
51
+ headers = {d.start: d.depth for d in self.definitions}
52
+ for depth in range(max(headers.values(), default=0), 0, -1):
53
+ lines = [n for n in lines if headers.get(n, 0) < depth]
54
+ if len(lines) <= max_lines:
55
+ break
56
+ return tuple(lines[:max_lines])
@@ -0,0 +1,86 @@
1
+ """The places where a change writes or removes stored data. Data loss and corruption happen at
2
+ these statements, and a general reading of a change tends to judge them by what they are meant
3
+ to do rather than by the rows they actually match; listing them lets each be checked on its own.
4
+
5
+ Found by their shape, in any language: SQL statements that change data or schema, data-access
6
+ calls that update, replace or delete, schema rules that delete or rewrite related rows, and file
7
+ removals. Test code is left out: it writes to fixtures, not to anyone's data.
8
+ """
9
+
10
+ from __future__ import annotations
11
+
12
+ import re
13
+ from dataclasses import dataclass
14
+
15
+ from mlai_review.domain.changes import ChangeSet, parse_patch
16
+ from mlai_review.domain.relevance import is_test
17
+
18
+ # More sites than this in one change are not listed one by one; the first ones are kept.
19
+ MAX_WRITE_SITES = 30
20
+
21
+ _SQL_HOME = re.compile(r"\.sql$|(^|/)migrations?/|(^|/)alembic/", re.IGNORECASE)
22
+ # SQL that changes data or schema, as it starts a line in SQL files and migrations.
23
+ _SQL_LINE = re.compile(
24
+ r"^\s*(UPDATE|DELETE|INSERT|MERGE|UPSERT|REPLACE|ALTER|DROP|TRUNCATE)\b", re.IGNORECASE
25
+ )
26
+ # The same written inside a string in application code.
27
+ _SQL_IN_STRING = re.compile(
28
+ r"[\"'`]\s*(UPDATE\s+\S+\s+SET|DELETE\s+FROM|INSERT\s+INTO|MERGE\s+INTO|TRUNCATE"
29
+ r"|ALTER\s+TABLE|DROP\s+TABLE)\b",
30
+ re.IGNORECASE,
31
+ )
32
+ _CODE = (
33
+ # Data-access calls that update, replace or delete (Mongoose, SQLAlchemy, Django, Prisma,
34
+ # Sequelize, TypeORM and the like).
35
+ re.compile(
36
+ r"\.(deleteMany|deleteOne|updateMany|updateOne|replaceOne|findOneAndUpdate"
37
+ r"|findOneAndReplace|findOneAndDelete|findOneAndRemove|findByIdAndUpdate"
38
+ r"|findByIdAndDelete|findByIdAndRemove|bulkWrite|destroy|truncate|remove|upsert"
39
+ r"|update|delete|drop)\s*\("
40
+ ),
41
+ # Update operators that set, clear or move whole fields.
42
+ re.compile(r"[\"']?\$(set|unset|pull|pullAll|rename|replaceRoot)\b"),
43
+ # Schema rules that delete or rewrite related rows.
44
+ re.compile(r"\bon_?delete\s*[=:]|\bon_?update\s*[=:]|\bON\s+DELETE\b|\bON\s+UPDATE\b", re.I),
45
+ # Migration operations that drop or rewrite.
46
+ re.compile(r"\bop\.(drop_\w+|alter_column|execute|bulk_insert)\b"),
47
+ # File and directory removal.
48
+ re.compile(
49
+ r"\b(rmSync|rmdirSync|unlinkSync|rimraf|rmtree|os\.remove|os\.unlink|os\.rmdir)\b"
50
+ r"|\bfs(\.promises)?\.(rm|rmdir|unlink)\s*\(|\.unlink\s*\("
51
+ ),
52
+ )
53
+ # HTTP route declarations (router.delete('/items/:id', ...)) read like deletes but write nothing.
54
+ _ROUTE = re.compile(r"\b\w*(router|app|api|route|server)\.(delete|put|patch)\s*\(\s*[\"'`/]")
55
+
56
+
57
+ @dataclass(frozen=True, slots=True)
58
+ class WriteSite:
59
+ """One added line that writes or removes stored data."""
60
+
61
+ path: str
62
+ line: int # in the new file
63
+ text: str
64
+
65
+
66
+ def writes_in(changes: ChangeSet) -> tuple[WriteSite, ...]:
67
+ """The added lines of a change that write or remove stored data, in file order."""
68
+ sites: list[WriteSite] = []
69
+ for change in changes.files:
70
+ if is_test(change.path):
71
+ continue
72
+ sql_home = bool(_SQL_HOME.search(change.path))
73
+ for diff in parse_patch(change.patch):
74
+ if diff.kind == "+" and _writes(diff.text, sql_home=sql_home):
75
+ sites.append(WriteSite(change.path, diff.new_line, diff.text.strip()))
76
+ if len(sites) == MAX_WRITE_SITES:
77
+ return tuple(sites)
78
+ return tuple(sites)
79
+
80
+
81
+ def _writes(text: str, *, sql_home: bool) -> bool:
82
+ if _ROUTE.search(text):
83
+ return False
84
+ if sql_home and _SQL_LINE.search(text):
85
+ return True
86
+ return bool(_SQL_IN_STRING.search(text)) or any(p.search(text) for p in _CODE)