intentumdiff-python 0.0.1__py3-none-macosx_11_0_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 (165) hide show
  1. intentumdiff/__init__.py +134 -0
  2. intentumdiff/_differ_gate.py +210 -0
  3. intentumdiff/_differ_presentation.py +786 -0
  4. intentumdiff/_differ_runtime.py +320 -0
  5. intentumdiff/analysis/__init__.py +1 -0
  6. intentumdiff/analysis/compile_commands.py +151 -0
  7. intentumdiff/analysis/cross_file.py +71 -0
  8. intentumdiff/analysis/diagnostics.py +309 -0
  9. intentumdiff/analysis/guardrail_reports.py +254 -0
  10. intentumdiff/analysis/guardrails.py +318 -0
  11. intentumdiff/analysis/invariances.py +1207 -0
  12. intentumdiff/analysis/keyed_profiles.py +25 -0
  13. intentumdiff/analysis/resource_profiles.py +14 -0
  14. intentumdiff/analysis/schema_resolver.py +816 -0
  15. intentumdiff/analysis/text_review.py +46 -0
  16. intentumdiff/analysis/user_schemas.py +430 -0
  17. intentumdiff/assets.py +45 -0
  18. intentumdiff/cache/__init__.py +37 -0
  19. intentumdiff/cache/duckdb_store.py +98 -0
  20. intentumdiff/cache/sqlite_store.py +149 -0
  21. intentumdiff/cache/store.py +190 -0
  22. intentumdiff/cli/__init__.py +199 -0
  23. intentumdiff/cli/_commands.py +2141 -0
  24. intentumdiff/cli/_parser.py +1061 -0
  25. intentumdiff/cli/_shared.py +644 -0
  26. intentumdiff/content_type.py +59 -0
  27. intentumdiff/core/__init__.py +1 -0
  28. intentumdiff/core/commit_differ.py +599 -0
  29. intentumdiff/core/config.py +51 -0
  30. intentumdiff/core/cst_serializer.py +101 -0
  31. intentumdiff/core/diffignore.py +87 -0
  32. intentumdiff/core/index.py +189 -0
  33. intentumdiff/core/indexer.py +792 -0
  34. intentumdiff/core/models.py +837 -0
  35. intentumdiff/differ.py +2195 -0
  36. intentumdiff/github_app.py +287 -0
  37. intentumdiff/intentumdiff_rust_core/__init__.py +7 -0
  38. intentumdiff/intentumdiff_rust_core/ffi.py +8 -0
  39. intentumdiff/intentumdiff_rust_core/libintentumdiff_rust_core.dylib +0 -0
  40. intentumdiff/invariances/__init__.py +2 -0
  41. intentumdiff/invariances/rules.schema.json +283 -0
  42. intentumdiff/invariances/rules.yaml +349 -0
  43. intentumdiff/live_server.py +1314 -0
  44. intentumdiff/lsp/__init__.py +67 -0
  45. intentumdiff/lsp/client.py +544 -0
  46. intentumdiff/lsp/config.py +96 -0
  47. intentumdiff/lsp/enricher.py +227 -0
  48. intentumdiff/lsp/exceptions.py +27 -0
  49. intentumdiff/lsp/launcher.py +273 -0
  50. intentumdiff/lsp/servers.py +356 -0
  51. intentumdiff/lsp_server/__init__.py +14 -0
  52. intentumdiff/lsp_server/_codelens.py +67 -0
  53. intentumdiff/lsp_server/_diagnostics.py +62 -0
  54. intentumdiff/lsp_server/_handlers.py +175 -0
  55. intentumdiff/lsp_server/server.py +205 -0
  56. intentumdiff/plugins/__init__.py +1 -0
  57. intentumdiff/plugins/adapter.py +397 -0
  58. intentumdiff/plugins/builtins.py +323 -0
  59. intentumdiff/plugins/exceptions.py +93 -0
  60. intentumdiff/plugins/hub.py +1155 -0
  61. intentumdiff/plugins/language_metadata.py +250 -0
  62. intentumdiff/plugins/loader.py +1252 -0
  63. intentumdiff/plugins/registry.py +1079 -0
  64. intentumdiff/plugins/registry.schema.json +99 -0
  65. intentumdiff/plugins/registry_schema.py +280 -0
  66. intentumdiff/plugins/wit/plugin.wit +351 -0
  67. intentumdiff/rust_core.py +2370 -0
  68. intentumdiff/serve/__init__.py +19 -0
  69. intentumdiff/serve/_app.py +485 -0
  70. intentumdiff/serve/_static/index.html +326 -0
  71. intentumdiff/serve/_static/playground.js +567 -0
  72. intentumdiff/sources/__init__.py +1 -0
  73. intentumdiff/sources/base.py +28 -0
  74. intentumdiff/sources/file_source.py +48 -0
  75. intentumdiff/sources/git_source.py +375 -0
  76. intentumdiff/sources/live_buffer_source.py +113 -0
  77. intentumdiff/sources/patch_source.py +126 -0
  78. intentumdiff/sources/string_source.py +44 -0
  79. intentumdiff/testing.py +165 -0
  80. intentumdiff/vcs/__init__.py +37 -0
  81. intentumdiff/vcs/_safe_path.py +50 -0
  82. intentumdiff/vcs/base.py +136 -0
  83. intentumdiff/vcs/git_backend.py +97 -0
  84. intentumdiff/vcs/git_cli.py +100 -0
  85. intentumdiff/vcs/hg_backend.py +94 -0
  86. intentumdiff/vcs/perforce_backend.py +109 -0
  87. intentumdiff/vcs/svn_backend.py +105 -0
  88. intentumdiff/wasm/.gitkeep +2 -0
  89. intentumdiff/wasm/abap_parser.wasm +0 -0
  90. intentumdiff/wasm/adf_parser.wasm +0 -0
  91. intentumdiff/wasm/asciidoc_parser.wasm +0 -0
  92. intentumdiff/wasm/asm_parser.wasm +0 -0
  93. intentumdiff/wasm/assemblyscript_parser.wasm +0 -0
  94. intentumdiff/wasm/astro_parser.wasm +0 -0
  95. intentumdiff/wasm/bash_parser.wasm +0 -0
  96. intentumdiff/wasm/clojure_parser.wasm +0 -0
  97. intentumdiff/wasm/cmake_parser.wasm +0 -0
  98. intentumdiff/wasm/cpp_parser.wasm +0 -0
  99. intentumdiff/wasm/csharp_parser.wasm +0 -0
  100. intentumdiff/wasm/css_parser.wasm +0 -0
  101. intentumdiff/wasm/dart_parser.wasm +0 -0
  102. intentumdiff/wasm/databricks_parser.wasm +0 -0
  103. intentumdiff/wasm/dax_parser.wasm +0 -0
  104. intentumdiff/wasm/delphi_parser.wasm +0 -0
  105. intentumdiff/wasm/dockerfile_parser.wasm +0 -0
  106. intentumdiff/wasm/elixir_parser.wasm +0 -0
  107. intentumdiff/wasm/freebasic_parser.wasm +0 -0
  108. intentumdiff/wasm/generic_parser.wasm +0 -0
  109. intentumdiff/wasm/gitignore_parser.wasm +0 -0
  110. intentumdiff/wasm/go_parser.wasm +0 -0
  111. intentumdiff/wasm/gomod_parser.wasm +0 -0
  112. intentumdiff/wasm/graphql_parser.wasm +0 -0
  113. intentumdiff/wasm/groovy_parser.wasm +0 -0
  114. intentumdiff/wasm/haskell_parser.wasm +0 -0
  115. intentumdiff/wasm/html_parser.wasm +0 -0
  116. intentumdiff/wasm/ini_parser.wasm +0 -0
  117. intentumdiff/wasm/java_parser.wasm +0 -0
  118. intentumdiff/wasm/js_ts_parser.wasm +0 -0
  119. intentumdiff/wasm/json_parser.wasm +0 -0
  120. intentumdiff/wasm/kotlin_parser.wasm +0 -0
  121. intentumdiff/wasm/latex_parser.wasm +0 -0
  122. intentumdiff/wasm/lua_parser.wasm +0 -0
  123. intentumdiff/wasm/make_parser.wasm +0 -0
  124. intentumdiff/wasm/markdown_parser.wasm +0 -0
  125. intentumdiff/wasm/mdx_parser.wasm +0 -0
  126. intentumdiff/wasm/ocaml_parser.wasm +0 -0
  127. intentumdiff/wasm/odin_parser.wasm +0 -0
  128. intentumdiff/wasm/parser_manifest.json +590 -0
  129. intentumdiff/wasm/perl_parser.wasm +0 -0
  130. intentumdiff/wasm/php_parser.wasm +0 -0
  131. intentumdiff/wasm/plsql_parser.wasm +0 -0
  132. intentumdiff/wasm/po_parser.wasm +0 -0
  133. intentumdiff/wasm/postscript_parser.wasm +0 -0
  134. intentumdiff/wasm/powershell_parser.wasm +0 -0
  135. intentumdiff/wasm/proto_parser.wasm +0 -0
  136. intentumdiff/wasm/puppet_parser.wasm +0 -0
  137. intentumdiff/wasm/python_parser.wasm +0 -0
  138. intentumdiff/wasm/qsharp_parser.wasm +0 -0
  139. intentumdiff/wasm/r_parser.wasm +0 -0
  140. intentumdiff/wasm/reasonml_parser.wasm +0 -0
  141. intentumdiff/wasm/ruby_parser.wasm +0 -0
  142. intentumdiff/wasm/rust_parser.wasm +0 -0
  143. intentumdiff/wasm/sas_parser.wasm +0 -0
  144. intentumdiff/wasm/scala_parser.wasm +0 -0
  145. intentumdiff/wasm/scss_parser.wasm +0 -0
  146. intentumdiff/wasm/sql_parser.wasm +0 -0
  147. intentumdiff/wasm/squirrel_parser.wasm +0 -0
  148. intentumdiff/wasm/svelte_parser.wasm +0 -0
  149. intentumdiff/wasm/swift_parser.wasm +0 -0
  150. intentumdiff/wasm/terraform_parser.wasm +0 -0
  151. intentumdiff/wasm/toml_parser.wasm +0 -0
  152. intentumdiff/wasm/tsql_parser.wasm +0 -0
  153. intentumdiff/wasm/vbnet_parser.wasm +0 -0
  154. intentumdiff/wasm/vue_parser.wasm +0 -0
  155. intentumdiff/wasm/wat_parser.wasm +0 -0
  156. intentumdiff/wasm/xml_parser.wasm +0 -0
  157. intentumdiff/wasm/yaml_parser.wasm +0 -0
  158. intentumdiff/wasm/zig_parser.wasm +0 -0
  159. intentumdiff/watcher.py +259 -0
  160. intentumdiff_python-0.0.1.dist-info/METADATA +112 -0
  161. intentumdiff_python-0.0.1.dist-info/RECORD +165 -0
  162. intentumdiff_python-0.0.1.dist-info/WHEEL +4 -0
  163. intentumdiff_python-0.0.1.dist-info/entry_points.txt +80 -0
  164. intentumdiff_python-0.0.1.dist-info/licenses/LICENSE +21 -0
  165. intentumdiff_python-0.0.1.dist-info/sboms/intentumdiff-rust-core.cyclonedx.json +10968 -0
@@ -0,0 +1,786 @@
1
+ """Tree, markdown-presentation, fallback-diff and stream helpers for intentumdiff.differ."""
2
+
3
+ from __future__ import annotations
4
+
5
+ import bisect
6
+ import hashlib
7
+ import json
8
+ import logging
9
+ from collections.abc import Iterator
10
+ from typing import Any
11
+
12
+ from intentumdiff.analysis.text_review import (
13
+ PresentationResult,
14
+ )
15
+ from intentumdiff.core.models import (
16
+ Change,
17
+ ChangeGroup,
18
+ ChangeGroupKind,
19
+ ChangeStreamEvent,
20
+ ChangeStreamPhase,
21
+ ChangeType,
22
+ NodePosition,
23
+ SemanticDiff,
24
+ SemanticNode,
25
+ )
26
+
27
+ logger = logging.getLogger(__name__)
28
+
29
+ def _validate_tree_ids(root: SemanticNode, context: str) -> None:
30
+ """
31
+ Verify that every node in the tree has a unique ID.
32
+
33
+ Duplicate IDs within a single tree cause silent correctness bugs in the
34
+ matching phase (the second node with a given ID is silently skipped).
35
+ Raises ``ValueError`` when duplicates are detected, naming the offending
36
+ IDs so plugin authors can diagnose the issue quickly.
37
+ """
38
+ seen: set[str] = set()
39
+ duplicates: set[str] = set()
40
+ for node in [root] + root.descendants():
41
+ if node.id in seen:
42
+ duplicates.add(node.id)
43
+ seen.add(node.id)
44
+ if duplicates:
45
+ raise ValueError(
46
+ f"Plugin produced duplicate node IDs in {context!r}: "
47
+ f"{sorted(duplicates)}. Each node must have a unique 'id' field."
48
+ )
49
+
50
+
51
+ def _root_structural_hash(node: SemanticNode) -> str:
52
+ """Return the structural hash of a SemanticNode root (already computed)."""
53
+ return node.structural_hash
54
+
55
+
56
+ def _node_to_dict(node: SemanticNode) -> dict[str, Any]:
57
+ return json.loads(node.model_dump_json())
58
+
59
+
60
+ def _compute_structural_hash_for_tree(cst_json: str) -> str:
61
+ """
62
+ Compute the structural hash of the FILTERED (trivia-stripped) CST.
63
+ Used for the style-only shortcut before running the full diff algorithm.
64
+ """
65
+ from intentumdiff.plugins.loader import _structural_hash_impl
66
+
67
+ return _structural_hash_impl(cst_json)
68
+
69
+
70
+ def _count_cst_nodes(cst_json: str) -> int:
71
+ """Count total nodes in a CST JSON string (recursive depth-first)."""
72
+
73
+ def _count(node: Any) -> int:
74
+ return 1 + sum(_count(c) for c in node.get("children", ()))
75
+
76
+ try:
77
+ return _count(json.loads(cst_json))
78
+ except Exception:
79
+ return 0
80
+
81
+
82
+ def _count_semantic_nodes(root: SemanticNode) -> int:
83
+ return 1 + len(root.descendants())
84
+
85
+
86
+ 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
+ )
294
+ return PresentationResult(
295
+ changes=[*filtered_changes, *moves],
296
+ change_groups=[*presented.change_groups, move_group],
297
+ ignored_style_changes=presented.ignored_style_changes,
298
+ )
299
+
300
+
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
+ )
343
+
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
+
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
436
+
437
+
438
+ 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())
441
+
442
+
443
+ 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,
451
+ ) -> 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
546
+
547
+
548
+ # ---------------------------------------------------------------------------
549
+ # Streaming helpers
550
+ # ---------------------------------------------------------------------------
551
+
552
+
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
+ 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
+
786
+