intentumdiff-python 0.0.1__py3-none-win_arm64.whl → 0.0.2b2__py3-none-win_arm64.whl

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 (139) hide show
  1. intentumdiff/__init__.py +2 -0
  2. intentumdiff/__main__.py +12 -0
  3. intentumdiff/_differ_presentation.py +35 -682
  4. intentumdiff/_differ_runtime.py +13 -47
  5. intentumdiff/analysis/compile_commands.py +26 -141
  6. intentumdiff/analysis/cross_file.py +2 -4
  7. intentumdiff/analysis/guardrail_reports.py +7 -5
  8. intentumdiff/analysis/guardrails.py +110 -122
  9. intentumdiff/analysis/invariances.py +4 -1003
  10. intentumdiff/analysis/schema_resolver.py +49 -273
  11. intentumdiff/analysis/text_review.py +4 -3
  12. intentumdiff/analysis/user_schemas.py +55 -237
  13. intentumdiff/api.py +21 -0
  14. intentumdiff/cache/sqlite_store.py +5 -23
  15. intentumdiff/cli/__init__.py +59 -0
  16. intentumdiff/cli/_commands.py +174 -67
  17. intentumdiff/cli/_parser.py +11 -1
  18. intentumdiff/cli/_shared.py +96 -112
  19. intentumdiff/content_type.py +13 -30
  20. intentumdiff/core/commit_differ.py +105 -220
  21. intentumdiff/core/diffignore.py +28 -34
  22. intentumdiff/core/index.py +16 -18
  23. intentumdiff/core/indexer.py +148 -168
  24. intentumdiff/core/models.py +53 -14
  25. intentumdiff/differ.py +278 -605
  26. intentumdiff/github_app.py +35 -19
  27. intentumdiff/intentumdiff_rust_core/intentumdiff_rust_core.dll +0 -0
  28. intentumdiff/live_server.py +180 -17
  29. intentumdiff/lsp/client.py +7 -1
  30. intentumdiff/lsp/enricher.py +19 -119
  31. intentumdiff/lsp/exceptions.py +6 -7
  32. intentumdiff/lsp_server/_codelens.py +24 -57
  33. intentumdiff/lsp_server/_diagnostics.py +23 -51
  34. intentumdiff/lsp_server/_handlers.py +66 -8
  35. intentumdiff/lsp_server/server.py +28 -7
  36. intentumdiff/plugins/adapter.py +5 -0
  37. intentumdiff/plugins/builtins.py +7 -14
  38. intentumdiff/plugins/exceptions.py +4 -0
  39. intentumdiff/plugins/hub.py +1 -2
  40. intentumdiff/plugins/loader.py +229 -215
  41. intentumdiff/plugins/registry.py +249 -197
  42. intentumdiff/rust_core.py +744 -784
  43. intentumdiff/serve/_app.py +3 -10
  44. intentumdiff/sources/base.py +24 -0
  45. intentumdiff/sources/file_source.py +15 -2
  46. intentumdiff/sources/git_source.py +120 -58
  47. intentumdiff/sources/live_buffer_source.py +45 -5
  48. intentumdiff/sources/patch_source.py +36 -109
  49. intentumdiff/vcs/_safe_path.py +7 -0
  50. intentumdiff/vcs/base.py +6 -0
  51. intentumdiff/vcs/git_backend.py +1 -1
  52. intentumdiff/vcs/git_cli.py +38 -18
  53. intentumdiff/vcs/hg_backend.py +1 -1
  54. intentumdiff/vcs/perforce_backend.py +7 -1
  55. intentumdiff/vcs/svn_backend.py +1 -1
  56. intentumdiff/wasm/abap_parser.wasm +0 -0
  57. intentumdiff/wasm/adf_parser.wasm +0 -0
  58. intentumdiff/wasm/asciidoc_parser.wasm +0 -0
  59. intentumdiff/wasm/asm_parser.wasm +0 -0
  60. intentumdiff/wasm/assemblyscript_parser.wasm +0 -0
  61. intentumdiff/wasm/astro_parser.wasm +0 -0
  62. intentumdiff/wasm/bash_parser.wasm +0 -0
  63. intentumdiff/wasm/clojure_parser.wasm +0 -0
  64. intentumdiff/wasm/cmake_parser.wasm +0 -0
  65. intentumdiff/wasm/cpp_parser.wasm +0 -0
  66. intentumdiff/wasm/csharp_parser.wasm +0 -0
  67. intentumdiff/wasm/css_parser.wasm +0 -0
  68. intentumdiff/wasm/dart_parser.wasm +0 -0
  69. intentumdiff/wasm/databricks_parser.wasm +0 -0
  70. intentumdiff/wasm/dax_parser.wasm +0 -0
  71. intentumdiff/wasm/delphi_parser.wasm +0 -0
  72. intentumdiff/wasm/dockerfile_parser.wasm +0 -0
  73. intentumdiff/wasm/elixir_parser.wasm +0 -0
  74. intentumdiff/wasm/freebasic_parser.wasm +0 -0
  75. intentumdiff/wasm/generic_parser.wasm +0 -0
  76. intentumdiff/wasm/gitignore_parser.wasm +0 -0
  77. intentumdiff/wasm/go_parser.wasm +0 -0
  78. intentumdiff/wasm/gomod_parser.wasm +0 -0
  79. intentumdiff/wasm/graphql_parser.wasm +0 -0
  80. intentumdiff/wasm/groovy_parser.wasm +0 -0
  81. intentumdiff/wasm/haskell_parser.wasm +0 -0
  82. intentumdiff/wasm/html_parser.wasm +0 -0
  83. intentumdiff/wasm/html_renderer.wasm +0 -0
  84. intentumdiff/wasm/ini_parser.wasm +0 -0
  85. intentumdiff/wasm/java_parser.wasm +0 -0
  86. intentumdiff/wasm/js_ts_parser.wasm +0 -0
  87. intentumdiff/wasm/json_parser.wasm +0 -0
  88. intentumdiff/wasm/kotlin_parser.wasm +0 -0
  89. intentumdiff/wasm/latex_parser.wasm +0 -0
  90. intentumdiff/wasm/llm_renderer.wasm +0 -0
  91. intentumdiff/wasm/lua_parser.wasm +0 -0
  92. intentumdiff/wasm/make_parser.wasm +0 -0
  93. intentumdiff/wasm/markdown_parser.wasm +0 -0
  94. intentumdiff/wasm/mdx_parser.wasm +0 -0
  95. intentumdiff/wasm/ocaml_parser.wasm +0 -0
  96. intentumdiff/wasm/odin_parser.wasm +0 -0
  97. intentumdiff/wasm/parser_manifest.json +81 -2
  98. intentumdiff/wasm/patch_renderer.wasm +0 -0
  99. intentumdiff/wasm/perl_parser.wasm +0 -0
  100. intentumdiff/wasm/php_parser.wasm +0 -0
  101. intentumdiff/wasm/plsql_parser.wasm +0 -0
  102. intentumdiff/wasm/po_parser.wasm +0 -0
  103. intentumdiff/wasm/postscript_parser.wasm +0 -0
  104. intentumdiff/wasm/powershell_parser.wasm +0 -0
  105. intentumdiff/wasm/proto_parser.wasm +0 -0
  106. intentumdiff/wasm/puppet_parser.wasm +0 -0
  107. intentumdiff/wasm/python_parser.wasm +0 -0
  108. intentumdiff/wasm/qsharp_parser.wasm +0 -0
  109. intentumdiff/wasm/r_parser.wasm +0 -0
  110. intentumdiff/wasm/reasonml_parser.wasm +0 -0
  111. intentumdiff/wasm/ruby_parser.wasm +0 -0
  112. intentumdiff/wasm/rust_parser.wasm +0 -0
  113. intentumdiff/wasm/sas_parser.wasm +0 -0
  114. intentumdiff/wasm/scala_parser.wasm +0 -0
  115. intentumdiff/wasm/scss_parser.wasm +0 -0
  116. intentumdiff/wasm/sql_parser.wasm +0 -0
  117. intentumdiff/wasm/squirrel_parser.wasm +0 -0
  118. intentumdiff/wasm/svelte_parser.wasm +0 -0
  119. intentumdiff/wasm/swift_parser.wasm +0 -0
  120. intentumdiff/wasm/terminal_renderer.wasm +0 -0
  121. intentumdiff/wasm/terraform_parser.wasm +0 -0
  122. intentumdiff/wasm/toml_parser.wasm +0 -0
  123. intentumdiff/wasm/tsql_parser.wasm +0 -0
  124. intentumdiff/wasm/vbnet_parser.wasm +0 -0
  125. intentumdiff/wasm/vue_parser.wasm +0 -0
  126. intentumdiff/wasm/wasm_provenance.json +884 -0
  127. intentumdiff/wasm/wat_parser.wasm +0 -0
  128. intentumdiff/wasm/xml_parser.wasm +0 -0
  129. intentumdiff/wasm/yaml_parser.wasm +0 -0
  130. intentumdiff/wasm/zig_parser.wasm +0 -0
  131. intentumdiff_python-0.0.2b2.dist-info/METADATA +156 -0
  132. intentumdiff_python-0.0.2b2.dist-info/RECORD +171 -0
  133. {intentumdiff_python-0.0.1.dist-info → intentumdiff_python-0.0.2b2.dist-info}/WHEEL +1 -1
  134. {intentumdiff_python-0.0.1.dist-info → intentumdiff_python-0.0.2b2.dist-info}/sboms/intentumdiff-rust-core.cyclonedx.json +905 -379
  135. intentumdiff/core/cst_serializer.py +0 -101
  136. intentumdiff_python-0.0.1.dist-info/METADATA +0 -112
  137. intentumdiff_python-0.0.1.dist-info/RECORD +0 -165
  138. {intentumdiff_python-0.0.1.dist-info → intentumdiff_python-0.0.2b2.dist-info}/entry_points.txt +0 -0
  139. {intentumdiff_python-0.0.1.dist-info → intentumdiff_python-0.0.2b2.dist-info}/licenses/LICENSE +0 -0
@@ -2,11 +2,9 @@
2
2
 
3
3
  from __future__ import annotations
4
4
 
5
- import bisect
6
5
  import hashlib
7
6
  import json
8
7
  import logging
9
- from collections.abc import Iterator
10
8
  from typing import Any
11
9
 
12
10
  from intentumdiff.analysis.text_review import (
@@ -16,8 +14,6 @@ from intentumdiff.core.models import (
16
14
  Change,
17
15
  ChangeGroup,
18
16
  ChangeGroupKind,
19
- ChangeStreamEvent,
20
- ChangeStreamPhase,
21
17
  ChangeType,
22
18
  NodePosition,
23
19
  SemanticDiff,
@@ -84,465 +80,51 @@ def _count_semantic_nodes(root: SemanticNode) -> int:
84
80
 
85
81
 
86
82
  def _empty_semantic_tree(language: str) -> SemanticNode:
87
- digest = hashlib.sha256(f"intentumdiff-empty-tree:{language}".encode()).hexdigest()
88
- return SemanticNode(
89
- id="0",
90
- node_type="source_file",
91
- label="",
92
- position=NodePosition(start_line=0, start_col=0, end_line=0, end_col=0),
93
- structural_hash=digest,
94
- children=[],
95
- )
96
-
97
-
98
- def _is_markdown_filename(filename: str) -> bool:
99
- return filename.replace("\\", "/").lower().endswith(".md")
100
-
101
-
102
- def _markdown_sections(source: str, *, side: str) -> list[SemanticNode]:
103
- lines = source.splitlines()
104
- heading_indices = [
105
- idx
106
- for idx, line in enumerate(lines)
107
- if line.startswith("#") and line.lstrip("#").startswith(" ")
108
- ]
109
- sections: list[SemanticNode] = []
110
- for order, start in enumerate(heading_indices):
111
- end = heading_indices[order + 1] if order + 1 < len(heading_indices) else len(lines)
112
- text = "\n".join(lines[start:end]).strip()
113
- if not text:
114
- continue
115
- digest = hashlib.sha256(text.encode("utf-8")).hexdigest()
116
- sections.append(
117
- SemanticNode(
118
- id=f"markdown-{side}-{order}",
119
- node_type="markdown_section",
120
- label=lines[start].strip(),
121
- position=NodePosition(
122
- start_line=start,
123
- start_col=0,
124
- end_line=max(start, end - 1),
125
- end_col=len(lines[end - 1]) if end > start else len(lines[start]),
126
- ),
127
- structural_hash=digest,
128
- children=[],
129
- )
130
- )
131
- return sections
132
-
133
-
134
- def _markdown_section_body_hashes(source: str, *, side: str) -> dict[str, str]:
135
- lines = source.splitlines()
136
- heading_indices = [
137
- idx
138
- for idx, line in enumerate(lines)
139
- if line.startswith("#") and line.lstrip("#").startswith(" ")
140
- ]
141
- hashes: dict[str, str] = {}
142
- for order, start in enumerate(heading_indices):
143
- end = heading_indices[order + 1] if order + 1 < len(heading_indices) else len(lines)
144
- body = "\n".join(lines[start + 1 : end]).strip()
145
- hashes[f"markdown-{side}-{order}"] = hashlib.sha256(body.encode("utf-8")).hexdigest()
146
- return hashes
147
-
148
-
149
- def _markdown_section_move_presentation(
150
- presented: PresentationResult,
151
- *,
152
- old_source: str,
153
- new_source: str,
154
- old_filename: str,
155
- new_filename: str,
156
- ) -> PresentationResult:
157
- if not (_is_markdown_filename(old_filename) or _is_markdown_filename(new_filename)):
158
- return presented
159
-
160
- from intentumdiff.rust_core import try_rust_markdown_section_review
161
-
162
- rust_review = try_rust_markdown_section_review(old_source, new_source)
163
- if rust_review is not None:
164
- moves = list(rust_review["moves"])
165
- if not moves:
166
- return presented
167
- moved_labels = set(rust_review["moved_labels"])
168
- filtered_changes = [
169
- change
170
- for change in presented.changes
171
- if not (
172
- change.change_type in {ChangeType.ADDITION, ChangeType.DELETION}
173
- and (
174
- change.old_node is not None
175
- and change.old_node.label in moved_labels
176
- or change.new_node is not None
177
- and change.new_node.label in moved_labels
178
- )
179
- )
180
- ]
181
- groups = list(presented.change_groups)
182
- if rust_review["move_group"] is not None:
183
- groups.append(rust_review["move_group"])
184
- return PresentationResult(
185
- changes=[*filtered_changes, *moves],
186
- change_groups=groups,
187
- ignored_style_changes=presented.ignored_style_changes,
188
- )
189
-
190
- # Python fallback — mirror of the Rust stage.
191
- old_sections = _markdown_sections(old_source, side="old")
192
- new_sections = _markdown_sections(new_source, side="new")
193
- old_by_hash: dict[str, list[tuple[int, SemanticNode]]] = {}
194
- new_by_hash: dict[str, list[tuple[int, SemanticNode]]] = {}
195
- for idx, node in enumerate(old_sections):
196
- old_by_hash.setdefault(node.structural_hash, []).append((idx, node))
197
- for idx, node in enumerate(new_sections):
198
- new_by_hash.setdefault(node.structural_hash, []).append((idx, node))
199
-
200
- unique_common = [
201
- digest
202
- for digest, old_matches in old_by_hash.items()
203
- if len(old_matches) == 1 and len(new_by_hash.get(digest, [])) == 1
204
- ]
205
- old_relative_order = {
206
- digest: order
207
- for order, digest in enumerate(
208
- sorted(unique_common, key=lambda item: old_by_hash[item][0][0])
209
- )
210
- }
211
- new_relative_order = {
212
- digest: order
213
- for order, digest in enumerate(
214
- sorted(unique_common, key=lambda item: new_by_hash[item][0][0])
215
- )
216
- }
217
-
218
- # Insertion-shift discrimination (issue #15, mirroring the Rust LIS rule from issues
219
- # #12/#32): sections whose RELATIVE order is preserved (longest increasing subsequence of
220
- # new order walked in old order) are stationary anchors; only order-breaking sections
221
- # move. A swap of two sections is ONE move, not two.
222
- ordered_digests = sorted(unique_common, key=lambda item: old_relative_order[item])
223
- sequence = [new_relative_order[digest] for digest in ordered_digests]
224
- tails: list[int] = []
225
- predecessors: dict[int, int | None] = {}
226
- tail_indices: list[int] = []
227
- for idx, value in enumerate(sequence):
228
- pos = bisect.bisect_left([sequence[i] for i in tail_indices], value)
229
- predecessors[idx] = tail_indices[pos - 1] if pos > 0 else None
230
- if pos == len(tail_indices):
231
- tail_indices.append(idx)
232
- else:
233
- tail_indices[pos] = idx
234
- stationary: set[int] = set()
235
- cursor: int | None = tail_indices[-1] if tail_indices else None
236
- while cursor is not None:
237
- stationary.add(cursor)
238
- cursor = predecessors[cursor]
239
- stationary_digests = {ordered_digests[idx] for idx in stationary}
240
-
241
- moves: list[Change] = []
242
- moved_labels: set[str] = set()
243
- moved_old_ids: list[str] = []
244
- moved_new_ids: list[str] = []
245
- for digest in unique_common:
246
- old_matches = old_by_hash[digest]
247
- new_matches = new_by_hash[digest]
248
- _old_idx, old_node = old_matches[0]
249
- _new_idx, new_node = new_matches[0]
250
- if old_relative_order[digest] == new_relative_order[digest]:
251
- continue
252
- if digest in stationary_digests:
253
- continue
254
- moves.append(
255
- Change(
256
- change_type=ChangeType.MOVE,
257
- old_node=old_node,
258
- new_node=new_node,
259
- confidence=0.9,
260
- description=f"Move Markdown section {old_node.label!r}",
261
- )
262
- )
263
- moved_labels.add(old_node.label)
264
- moved_old_ids.append(old_node.id)
265
- moved_new_ids.append(new_node.id)
266
-
267
- if not moves:
268
- return presented
269
-
270
- filtered_changes = [
271
- change
272
- for change in presented.changes
273
- if not (
274
- change.change_type in {ChangeType.ADDITION, ChangeType.DELETION}
275
- and (
276
- change.old_node is not None
277
- and change.old_node.label in moved_labels
278
- or change.new_node is not None
279
- and change.new_node.label in moved_labels
280
- )
281
- )
282
- ]
283
- move_group = ChangeGroup(
284
- kind=ChangeGroupKind.MOVED_CODE,
285
- raw_change_indices=[],
286
- old_labels=sorted(moved_labels),
287
- new_labels=sorted(moved_labels),
288
- old_node_ids=moved_old_ids,
289
- new_node_ids=moved_new_ids,
290
- confidence=0.9,
291
- rule_id="presentation.markdown_section_move",
292
- metadata={"moved_section_count": len(moves)},
293
- )
83
+ from intentumdiff.rust_core import _c_abi_call
84
+ return SemanticNode.model_validate(_c_abi_call("empty_semantic_tree", language))
85
+
86
+
87
+ def _markdown_presentation(presented: PresentationResult, *, old_source: str, new_source: str,
88
+ old_filename: str, new_filename: str, phase: str) -> PresentationResult:
89
+ """DTO adapter for complete Rust reconciliation; core errors propagate."""
90
+ from intentumdiff.rust_core import _c_abi_call, _require_source_anchored_changes
91
+ result = _c_abi_call("reconcile_markdown", {
92
+ "changes": [change.model_dump(mode="json") for change in presented.changes],
93
+ "change_groups": [group.model_dump(mode="json") for group in presented.change_groups],
94
+ "ignored_style_changes": presented.ignored_style_changes,
95
+ }, old_source, new_source, old_filename, new_filename, phase)
294
96
  return PresentationResult(
295
- changes=[*filtered_changes, *moves],
296
- change_groups=[*presented.change_groups, move_group],
297
- ignored_style_changes=presented.ignored_style_changes,
97
+ changes=[Change.model_validate(value) for value in
98
+ _require_source_anchored_changes(result["changes"], "Rust Markdown reconciliation")],
99
+ change_groups=[ChangeGroup.model_validate(value) for value in result["change_groups"]],
100
+ ignored_style_changes=result["ignored_style_changes"],
298
101
  )
299
102
 
300
103
 
301
- def _markdown_section_heading_rename_presentation(
302
- presented: PresentationResult,
303
- *,
304
- old_source: str,
305
- new_source: str,
306
- old_filename: str,
307
- new_filename: str,
308
- ) -> PresentationResult:
309
- if not (_is_markdown_filename(old_filename) or _is_markdown_filename(new_filename)):
310
- return presented
311
-
312
- from intentumdiff.rust_core import try_rust_markdown_section_review
313
-
314
- rust_review = try_rust_markdown_section_review(old_source, new_source)
315
- if rust_review is not None:
316
- modifications = list(rust_review["renames"])
317
- if not modifications:
318
- return presented
319
- old_heading_lines = set(rust_review["old_heading_lines"])
320
- new_heading_lines = set(rust_review["new_heading_lines"])
321
- filtered_changes = [
322
- change
323
- for change in presented.changes
324
- if not (
325
- change.change_type
326
- in {ChangeType.ADDITION, ChangeType.DELETION, ChangeType.MODIFICATION}
327
- and (
328
- change.old_node is not None
329
- and change.old_node.position.start_line in old_heading_lines
330
- or change.new_node is not None
331
- and change.new_node.position.start_line in new_heading_lines
332
- )
333
- )
334
- ]
335
- groups = list(presented.change_groups)
336
- if rust_review["rename_group"] is not None:
337
- groups.append(rust_review["rename_group"])
338
- return PresentationResult(
339
- changes=[*filtered_changes, *modifications],
340
- change_groups=groups,
341
- ignored_style_changes=presented.ignored_style_changes,
342
- )
104
+ def _markdown_section_move_presentation(presented: PresentationResult, **kwargs: Any) -> PresentationResult:
105
+ return _markdown_presentation(presented, phase="moves", **kwargs)
343
106
 
344
- # Python fallback — mirror of the Rust stage.
345
- old_sections = _markdown_sections(old_source, side="old")
346
- new_sections = _markdown_sections(new_source, side="new")
347
- old_body_hashes = _markdown_section_body_hashes(old_source, side="old")
348
- new_body_hashes = _markdown_section_body_hashes(new_source, side="new")
349
- old_by_body: dict[str, list[SemanticNode]] = {}
350
- new_by_body: dict[str, list[SemanticNode]] = {}
351
- for node in old_sections:
352
- old_by_body.setdefault(old_body_hashes.get(node.id, ""), []).append(node)
353
- for node in new_sections:
354
- new_by_body.setdefault(new_body_hashes.get(node.id, ""), []).append(node)
355
-
356
- modifications: list[Change] = []
357
- old_heading_lines: set[int] = set()
358
- new_heading_lines: set[int] = set()
359
- old_labels: list[str] = []
360
- new_labels: list[str] = []
361
- old_ids: list[str] = []
362
- new_ids: list[str] = []
363
- for body_hash, old_matches in old_by_body.items():
364
- new_matches = new_by_body.get(body_hash, [])
365
- if not body_hash or len(old_matches) != 1 or len(new_matches) != 1:
366
- continue
367
- old_node = old_matches[0]
368
- new_node = new_matches[0]
369
- if old_node.label == new_node.label:
370
- continue
371
- modifications.append(
372
- Change(
373
- change_type=ChangeType.MODIFICATION,
374
- old_node=old_node,
375
- new_node=new_node,
376
- confidence=0.9,
377
- description=(f"Rename Markdown section {old_node.label!r} -> {new_node.label!r}"),
378
- )
379
- )
380
- old_heading_lines.add(old_node.position.start_line)
381
- new_heading_lines.add(new_node.position.start_line)
382
- old_labels.append(old_node.label)
383
- new_labels.append(new_node.label)
384
- old_ids.append(old_node.id)
385
- new_ids.append(new_node.id)
386
-
387
- if not modifications:
388
- return presented
389
-
390
- filtered_changes = [
391
- change
392
- for change in presented.changes
393
- if not (
394
- # A heading rename is now surfaced as one markdown_section MODIFICATION; drop the
395
- # overlapping line-level change on the same heading line (add/delete OR the
396
- # line-level modification the generic text diff produces) so it isn't duplicated.
397
- change.change_type
398
- in {ChangeType.ADDITION, ChangeType.DELETION, ChangeType.MODIFICATION}
399
- and (
400
- change.old_node is not None
401
- and change.old_node.position.start_line in old_heading_lines
402
- or change.new_node is not None
403
- and change.new_node.position.start_line in new_heading_lines
404
- )
405
- )
406
- ]
407
- group = ChangeGroup(
408
- kind=ChangeGroupKind.MEANINGFUL_CHANGE,
409
- raw_change_indices=[],
410
- old_labels=old_labels,
411
- new_labels=new_labels,
412
- old_node_ids=old_ids,
413
- new_node_ids=new_ids,
414
- confidence=0.9,
415
- rule_id="presentation.markdown_section_heading_rename",
416
- metadata={"renamed_section_count": len(modifications)},
417
- )
418
- return PresentationResult(
419
- changes=[*filtered_changes, *modifications],
420
- change_groups=[*presented.change_groups, group],
421
- ignored_style_changes=presented.ignored_style_changes,
422
- )
423
-
424
-
425
- def _all_semantic_nodes(root: SemanticNode) -> list[SemanticNode]:
426
- return [root, *root.descendants()]
427
107
 
428
-
429
- def _semantic_parent_map(root: SemanticNode) -> dict[str, SemanticNode]:
430
- parents: dict[str, SemanticNode] = {}
431
- for node in _all_semantic_nodes(root):
432
- for child in node.children:
433
- if child.id != node.id:
434
- parents[child.id] = node
435
- return parents
108
+ def _markdown_section_heading_rename_presentation(presented: PresentationResult, **kwargs: Any) -> PresentationResult:
109
+ return _markdown_presentation(presented, phase="renames", **kwargs)
436
110
 
437
111
 
438
112
  def _has_error_node(tree: SemanticNode) -> bool:
439
- """Return ``True`` when *tree* contains a tree-sitter ERROR node."""
440
- return tree.node_type == "ERROR" or any(n.node_type == "ERROR" for n in tree.descendants())
113
+ """Ask Rust whether a semantic tree contains a parse error."""
114
+ from intentumdiff.rust_core import parse_errors_present
115
+ return parse_errors_present("", tree.model_dump_json(), "")
441
116
 
442
117
 
443
118
  def _token_fallback_diff(
444
- old_content: str,
445
- new_content: str,
446
- old_filename: str,
447
- new_filename: str,
448
- language: str,
449
- *,
450
- metadata: dict[str, Any] | None = None,
119
+ old_content: str, new_content: str, old_filename: str, new_filename: str,
120
+ language: str, *, metadata: dict[str, Any] | None = None,
451
121
  ) -> SemanticDiff:
452
- """
453
- Coarse token-level diff used when the tree-sitter parse yields ERROR nodes.
454
-
455
- Splits both sides on whitespace and uses ``difflib.SequenceMatcher`` to
456
- produce ``ADDITION / DELETION / MODIFICATION`` changes with
457
- ``confidence=0.5``. The returned ``SemanticDiff`` has ``is_fallback=True``
458
- so callers can distinguish it from a full semantic diff.
459
- """
460
- import difflib
461
-
462
- old_tokens = old_content.split()
463
- new_tokens = new_content.split()
464
- changes: list[Change] = []
465
- matcher = difflib.SequenceMatcher(None, old_tokens, new_tokens, autojunk=False)
466
- for op, i1, i2, j1, j2 in matcher.get_opcodes():
467
- if op == "equal":
468
- continue
469
- if op == "insert":
470
- changes.append(
471
- Change(
472
- change_type=ChangeType.ADDITION,
473
- description=f"token-level fallback: inserted {j2 - j1} token(s)",
474
- confidence=0.5,
475
- )
476
- )
477
- elif op == "delete":
478
- changes.append(
479
- Change(
480
- change_type=ChangeType.DELETION,
481
- description=f"token-level fallback: deleted {i2 - i1} token(s)",
482
- confidence=0.5,
483
- )
484
- )
485
- elif op == "replace":
486
- changes.append(
487
- Change(
488
- change_type=ChangeType.MODIFICATION,
489
- description=f"token-level fallback: {i2 - i1} token(s) → {j2 - j1} token(s)",
490
- confidence=0.5,
491
- )
492
- )
493
- return SemanticDiff(
494
- changes=changes,
495
- old_filename=old_filename,
496
- new_filename=new_filename,
497
- language=language,
498
- has_semantic_changes=bool(changes),
499
- is_fallback=True,
500
- parse_errors=["tree-sitter reported parse errors; token-level fallback used"],
501
- metadata=metadata or {},
502
- )
503
-
504
-
505
- def _annotate_text_diffs(changes: list[Change]) -> list[Change]:
506
- """
507
- Annotate leaf-node MODIFICATION changes with an inline character-level diff.
508
-
509
- For each ``MODIFICATION`` change where both nodes are leaves with different
510
- labels, populates ``Change.text_diff`` with a compact ``[-old][+new]``
511
- annotation using ``difflib.SequenceMatcher``. Truncated to 200 characters.
512
- Already-annotated changes (``text_diff is not None``) are left unchanged.
513
- """
514
- import difflib
515
-
516
- result: list[Change] = []
517
- for change in changes:
518
- if (
519
- change.change_type == ChangeType.MODIFICATION
520
- and change.old_node is not None
521
- and change.new_node is not None
522
- and change.old_node.is_leaf()
523
- and change.new_node.is_leaf()
524
- and change.old_node.label != change.new_node.label
525
- and change.text_diff is None
526
- ):
527
- old_label = change.old_node.label
528
- new_label = change.new_node.label
529
- matcher = difflib.SequenceMatcher(None, old_label, new_label, autojunk=False)
530
- parts: list[str] = []
531
- for op, i1, i2, j1, j2 in matcher.get_opcodes():
532
- if op == "equal":
533
- parts.append(old_label[i1:i2])
534
- elif op == "replace":
535
- parts.append(f"[-{old_label[i1:i2]}][+{new_label[j1:j2]}]")
536
- elif op == "delete":
537
- parts.append(f"[-{old_label[i1:i2]}]")
538
- elif op == "insert":
539
- parts.append(f"[+{new_label[j1:j2]}]")
540
- text_diff: str = "".join(parts)
541
- if len(text_diff) > 200:
542
- text_diff = text_diff[:197] + "…"
543
- change = change.model_copy(update={"text_diff": text_diff})
544
- result.append(change)
545
- return result
122
+ """Compatibility adapter for Rust's source-preserving fallback."""
123
+ from intentumdiff.rust_core import source_fallback_diff
124
+ details = metadata or {}
125
+ result = source_fallback_diff(old_content, new_content, old_filename,
126
+ new_filename, language, str(details.get("fallback_reason", "parse_errors")))
127
+ return result.model_copy(update={"metadata": {**details, **dict(result.metadata)}})
546
128
 
547
129
 
548
130
  # ---------------------------------------------------------------------------
@@ -550,237 +132,8 @@ def _annotate_text_diffs(changes: list[Change]) -> list[Change]:
550
132
  # ---------------------------------------------------------------------------
551
133
 
552
134
 
553
- _GENERIC_STRING_LABELS = frozenset({"string", "string_literal"})
554
-
555
- # Entity node types whose labels should surface in review output even when
556
- # the matcher preserved the container (its body changed in place but the
557
- # container itself was matched by label). Broad coverage across all
558
- # supported parsers — tree-sitter grammars plus the lightweight hand-written
559
- # parsers (graphql, po, asciidoc, latex, ocaml, reasonml). See
560
- # ``the retired NOISE_SUPPRESSION_RETUNE doc (git history)`` Step A2.
561
- _NAMED_ENTITY_NODE_TYPES: frozenset[str] = frozenset(
562
- {
563
- # Tree-sitter common
564
- "function_definition", "async_function_def", "class_definition",
565
- "method_declaration", "function_declaration", "async_function_declaration",
566
- "constructor_declaration",
567
- "destructor_declaration", "property_declaration", "field_declaration",
568
- "variable_declaration", "lexical_declaration", "struct_definition",
569
- "interface_declaration", "enum_declaration", "enum_member_declaration",
570
- "operator_declaration", "event_declaration", "delegate_declaration",
571
- "record_declaration", "record_struct_declaration",
572
- # GraphQL
573
- "type_definition", "operation_definition", "fragment_definition",
574
- "field_definition", "directive_definition", "schema_definition",
575
- "enum_definition", "input_object_definition",
576
- # PO/gettext
577
- "message", "obsolete_message",
578
- # AsciiDoc
579
- "section_level_1", "section_level_2", "section_level_3",
580
- "section_level_4", "section_level_5", "section_level_6",
581
- # LaTeX
582
- "document_class", "package", "section", "environment",
583
- # OCaml / ReasonML
584
- "module_binding", "module", "value_definition",
585
- "recursive_value", "component", "signature_value", "class_type",
586
- "exception",
587
- }
588
- )
589
-
590
-
591
- def _is_named_entity_node(node: SemanticNode) -> bool:
592
- """Check whether a node is a named entity whose label should surface in review."""
593
- if not node.label or node.label == node.node_type:
594
- return False
595
- return (
596
- node.node_type in _NAMED_ENTITY_NODE_TYPES
597
- or node.node_type.endswith("_definition")
598
- or node.node_type.endswith("_declaration")
599
- )
600
-
601
-
602
- def _surface_changed_in_place_entities(
603
- *,
604
- matching: Any,
605
- changes: list[Change],
606
- ) -> list[ChangeGroup]:
607
- """Emit MEANINGFUL_CHANGE groups for named entities whose body changed in place.
608
-
609
- When the matcher preserves a named entity (e.g. ``type User``) whose body
610
- changed, the entity itself doesn't appear in the change list — only its
611
- changed descendants do. This function scans the matching for such pairs
612
- and emits change-groups carrying the entity's label, so review UIs and
613
- entity-label assertions can surface the container name.
614
-
615
- Guards:
616
- - Both endpoints must be named entities (``_is_named_entity_node``).
617
- - At least one descendant of the pair must appear in the change list
618
- (the container genuinely changed — detected via observable changes
619
- rather than structural_hash, which may be unreliable for some
620
- parser-produced trees).
621
- - The pair's old/new IDs must not already appear in any top-level
622
- change event (avoid duplicating already-surfaced entities).
623
- """
624
- if not matching:
625
- return []
626
-
627
- # Collect IDs that already surface in the change list.
628
- surfaced_old_ids: set[str] = set()
629
- surfaced_new_ids: set[str] = set()
630
- for change in changes:
631
- if change.old_node is not None:
632
- surfaced_old_ids.add(change.old_node.id)
633
- if change.new_node is not None:
634
- surfaced_new_ids.add(change.new_node.id)
635
-
636
- groups: list[ChangeGroup] = []
637
- for pair in matching:
638
- old_node = getattr(pair, "old_node", None)
639
- new_node = getattr(pair, "new_node", None)
640
- if old_node is None or new_node is None:
641
- continue
642
- if not _is_named_entity_node(old_node) or not _is_named_entity_node(new_node):
643
- continue
644
- # Skip if either endpoint already surfaces in a top-level change.
645
- if old_node.id in surfaced_old_ids or new_node.id in surfaced_new_ids:
646
- continue
647
- # Check if any descendant of this entity pair appears in the change
648
- # list. This is the reliable signal that the container changed in
649
- # place — structural_hash comparison alone is insufficient for some
650
- # parser-produced trees where the hash doesn't capture content changes.
651
- old_desc_ids = {d.id for d in old_node.descendants()}
652
- new_desc_ids = {d.id for d in new_node.descendants()}
653
- has_changed_descendant = bool(
654
- old_desc_ids & surfaced_old_ids or new_desc_ids & surfaced_new_ids
655
- )
656
- if not has_changed_descendant:
657
- continue
658
- groups.append(
659
- ChangeGroup(
660
- kind=ChangeGroupKind.MEANINGFUL_CHANGE,
661
- raw_change_indices=[],
662
- old_labels=[old_node.label],
663
- new_labels=[new_node.label],
664
- old_node_ids=[old_node.id],
665
- new_node_ids=[new_node.id],
666
- confidence=0.9,
667
- rule_id="presentation.surface_changed_in_place_entity",
668
- metadata={
669
- "entity_type": old_node.node_type,
670
- "old_label": old_node.label,
671
- "new_label": new_node.label,
672
- },
673
- )
674
- )
675
- return groups
676
-
677
-
678
- def _slice_source_text(source: str, position: NodePosition) -> str:
679
- lines = source.splitlines()
680
- if position.start_line < 0 or position.start_line >= len(lines):
681
- return ""
682
- if position.end_line < position.start_line:
683
- return ""
684
- if position.start_line == position.end_line:
685
- return lines[position.start_line][position.start_col : position.end_col]
686
-
687
- selected = [lines[position.start_line][position.start_col :]]
688
- for line_no in range(position.start_line + 1, min(position.end_line, len(lines))):
689
- selected.append(lines[line_no])
690
- if position.end_line < len(lines):
691
- selected.append(lines[position.end_line][: position.end_col])
692
- return "\n".join(selected)
693
-
694
-
695
- def _clean_string_literal_label(text: str) -> str:
696
- value = text.strip()
697
- while value and value[0] in "rRuUbBfF@$":
698
- value = value[1:]
699
- if len(value) >= 2 and value[0] == value[-1] and value[0] in {"'", '"', "`"}:
700
- value = value[1:-1]
701
- return value.strip()
702
-
703
-
704
135
  def _enrich_literal_labels(root: SemanticNode, source: str) -> SemanticNode:
705
- children = [_enrich_literal_labels(child, source) for child in root.children]
706
- label = root.label
707
- node_type = root.node_type.lower()
708
- if "string" in node_type and root.label in _GENERIC_STRING_LABELS:
709
- literal = _clean_string_literal_label(_slice_source_text(source, root.position))
710
- if literal:
711
- label = literal
712
- if node_type == "order_by_clause" and root.label in {root.node_type, node_type}:
713
- source_slice = _slice_source_text(source, root.position).lower()
714
- if "descending" in source_slice:
715
- label = f"{root.label} descending"
716
- if children == root.children and label == root.label:
717
- return root
718
- return root.model_copy(update={"label": label, "children": children})
719
-
720
-
721
- def _changes_to_stream_events(
722
- before: list[Change],
723
- after: list[Change],
724
- phase: ChangeStreamPhase,
725
- ) -> Iterator[ChangeStreamEvent]:
726
- """Diff two successive change lists and emit ``ChangeStreamEvent`` objects.
727
-
728
- Uses Python object identity to detect which changes were consumed
729
- (present in *before* but not *after*) and which were added (present in
730
- *after* but not *before*). ``Change`` is a frozen Pydantic model, so
731
- every logical change is a distinct Python object even when its content is
732
- the same as another change.
733
-
734
- For each new change in *after*:
735
-
736
- * If the new change's ``old_node.id`` or ``new_node.id`` matches a node ID
737
- from a consumed *before* change, the event is emitted as ``action="revise"``
738
- with ``replaced_ids`` listing the consumed node IDs.
739
- * Otherwise, ``action="add"`` is emitted (a brand-new change with no
740
- predecessor in *before*).
741
-
742
- Any change that was consumed but not referenced by a new change is emitted
743
- as ``action="remove"`` with ``replaced_ids=[node_id]``.
744
- """
745
- before_by_pyid = {id(c): c for c in before}
746
- after_by_pyid = {id(c): c for c in after}
747
-
748
- consumed = [c for pyid, c in before_by_pyid.items() if pyid not in after_by_pyid]
749
- added = [c for pyid, c in after_by_pyid.items() if pyid not in before_by_pyid]
750
-
751
- # Build node-id → consumed-change lookup.
752
- consumed_node_ids: dict[str, Change] = {}
753
- for c in consumed:
754
- if c.old_node:
755
- consumed_node_ids[c.old_node.id] = c
756
- if c.new_node:
757
- consumed_node_ids[c.new_node.id] = c
758
-
759
- mentioned: set[str] = set()
760
-
761
- for new_change in added:
762
- replaced_ids: list[str] = []
763
- if new_change.old_node and new_change.old_node.id in consumed_node_ids:
764
- replaced_ids.append(new_change.old_node.id)
765
- mentioned.add(new_change.old_node.id)
766
- if new_change.new_node and new_change.new_node.id in consumed_node_ids:
767
- replaced_ids.append(new_change.new_node.id)
768
- mentioned.add(new_change.new_node.id)
769
- yield ChangeStreamEvent(
770
- phase=phase,
771
- action="revise" if replaced_ids else "add",
772
- replaced_ids=replaced_ids,
773
- change=new_change,
774
- )
775
-
776
- # Emit "remove" for consumed changes not referenced by any new change.
777
- for c in consumed:
778
- node_id = c.old_node.id if c.old_node else c.new_node.id if c.new_node else None
779
- if node_id and node_id not in mentioned:
780
- yield ChangeStreamEvent(
781
- phase=phase,
782
- action="remove",
783
- replaced_ids=[node_id],
784
- )
785
-
136
+ """Delegate literal-label enrichment to Rust; never reconstruct semantics."""
137
+ from intentumdiff.rust_core import enrich_literal_labels
786
138
 
139
+ return enrich_literal_labels(root, source)