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,2370 @@
1
+ """Rust core host adapter.
2
+
3
+ Release wheels ship the first-party ``intentumdiff_rust_core`` PyO3 extension
4
+ inside the public ``intentumdiff`` package, and supported native paths use it by
5
+ default. The Python pipeline remains the fallback correctness path for
6
+ unsupported configs, missing native modules, and third-party/untrusted Wasm
7
+ plugin scenarios.
8
+ """
9
+
10
+ from __future__ import annotations
11
+
12
+ import importlib
13
+ import json
14
+ import logging
15
+ import os
16
+ import time
17
+ from dataclasses import dataclass, field
18
+ from pathlib import Path
19
+ from typing import Any
20
+
21
+ from pydantic import ValidationError
22
+
23
+ from intentumdiff.core.models import (
24
+ Change,
25
+ ChangeGroup,
26
+ ChangeType,
27
+ DiffConfig,
28
+ Match,
29
+ Matching,
30
+ SemanticDiff,
31
+ SemanticNode,
32
+ )
33
+
34
+ logger = logging.getLogger(__name__)
35
+
36
+ _COMPLETE_STATUS = "complete"
37
+ _BATCH_ENGINE = "rust_core_batch_v4"
38
+ _PYTHON_NATIVE_CERTIFICATION = "python_native_v4kb"
39
+ _CANONICAL_PYTHON_PLUGIN_ID = "intentumdiff:python:python"
40
+ _PYTHON_PLUGIN_IDS = frozenset(
41
+ {
42
+ "",
43
+ "python",
44
+ "python-parser",
45
+ "python_parser",
46
+ _CANONICAL_PYTHON_PLUGIN_ID,
47
+ }
48
+ )
49
+ _TREE_ENGINE = "rust_core_semantic_tree_v2_stage11"
50
+ # Language-agnostic engine label — emitted by ``diff_semantic_tree_json``
51
+ # (the migration-step-2 entrypoint that accepts any language label, not just
52
+ # Python). ``try_rust_core_tree_diff`` accepts either label so that
53
+ # pre-migration installs and post-migration installs both validate.
54
+ _TREE_ENGINE_V3 = "rust_core_semantic_tree_v3"
55
+ _SOURCE_ENGINE = "rust_core_sources_v3_stage11"
56
+
57
+
58
+ def _canonical_python_plugin_id(parser_plugin_id: str | None) -> str | None:
59
+ return parser_plugin_id
60
+
61
+
62
+ def _canonicalize_file_plugin_ids(files: list[dict[str, Any]]) -> list[dict[str, Any]]:
63
+ normalized: list[dict[str, Any]] = []
64
+ for file in files:
65
+ item = dict(file)
66
+ if "parser_plugin_id" in item:
67
+ item["parser_plugin_id"] = _canonical_python_plugin_id(item["parser_plugin_id"])
68
+ if "parserPluginId" in item:
69
+ item["parserPluginId"] = _canonical_python_plugin_id(item["parserPluginId"])
70
+ normalized.append(item)
71
+ return normalized
72
+
73
+
74
+ @dataclass(frozen=True)
75
+ class RustCoreAttempt:
76
+ diff: SemanticDiff | None = None
77
+ fallback_reason: str = ""
78
+ backend_version: str = ""
79
+
80
+ @property
81
+ def used(self) -> bool:
82
+ return self.diff is not None
83
+
84
+
85
+ @dataclass(frozen=True)
86
+ class RustCoreTreeAttempt:
87
+ changes: list[Change] | None = None
88
+ matching: Matching | None = None
89
+ metadata: dict[str, Any] | None = None
90
+ fallback_reason: str = ""
91
+ backend_version: str = ""
92
+
93
+ @property
94
+ def used(self) -> bool:
95
+ return self.changes is not None and self.matching is not None
96
+
97
+
98
+ @dataclass(frozen=True)
99
+ class RustCoreSourceAttempt:
100
+ old_tree: SemanticNode | None = None
101
+ new_tree: SemanticNode | None = None
102
+ changes: list[Change] | None = None
103
+ matching: Matching | None = None
104
+ adaptive_fuel: int = 0
105
+ metadata: dict[str, Any] | None = None
106
+ fallback_reason: str = ""
107
+ backend_version: str = ""
108
+
109
+ @property
110
+ def used(self) -> bool:
111
+ return (
112
+ self.old_tree is not None
113
+ and self.new_tree is not None
114
+ and self.changes is not None
115
+ and self.matching is not None
116
+ )
117
+
118
+
119
+ @dataclass(frozen=True)
120
+ class RustCoreBatchAttempt:
121
+ diff: SemanticDiff | None = None
122
+ fallback_reason: str = ""
123
+ backend_version: str = ""
124
+
125
+ @property
126
+ def used(self) -> bool:
127
+ return self.diff is not None
128
+
129
+
130
+ @dataclass(frozen=True)
131
+ class RustCoreBatchDiffsAttempt:
132
+ diffs: dict[int, SemanticDiff] = field(default_factory=dict)
133
+ fallback_reasons: dict[int, str] = field(default_factory=dict)
134
+ batch_metadata: dict[str, Any] = field(default_factory=dict)
135
+ adapter_phase_timings: list[dict[str, Any]] = field(default_factory=list)
136
+ backend_version: str = ""
137
+ fallback_reason: str = ""
138
+
139
+ @property
140
+ def used(self) -> bool:
141
+ return bool(self.diffs)
142
+
143
+
144
+ @dataclass(frozen=True)
145
+ class RustCoreCandidateBatchAttempt:
146
+ payload: dict[str, Any] | None = None
147
+ fallback_reason: str = ""
148
+ backend_version: str = ""
149
+
150
+ @property
151
+ def used(self) -> bool:
152
+ return self.payload is not None
153
+
154
+
155
+ @dataclass(frozen=True)
156
+ class RustCoreCommitJsonAttempt:
157
+ control: dict[str, Any] = field(default_factory=dict)
158
+ commit_diff_json: bytes = b""
159
+ adapter_phase_timings: list[dict[str, Any]] = field(default_factory=list)
160
+ backend_version: str = ""
161
+ fallback_reason: str = ""
162
+
163
+ @property
164
+ def used(self) -> bool:
165
+ return bool(self.commit_diff_json)
166
+
167
+ def to_model(self) -> Any:
168
+ """Lazily validate the certified JSON into the public CommitDiff model."""
169
+ from intentumdiff.core.models import CommitDiff
170
+
171
+ return CommitDiff.model_validate_json(self.commit_diff_json)
172
+
173
+
174
+ @dataclass(frozen=True)
175
+ class RustInvarianceResult:
176
+ changes: list[Change]
177
+ change_groups: list[ChangeGroup] = field(default_factory=list)
178
+ ignored_style_changes: list[dict[str, Any]] = field(default_factory=list)
179
+
180
+
181
+ def rust_core_enabled(config: DiffConfig) -> bool:
182
+ return bool(config.experimental_rust_core)
183
+
184
+
185
+ def try_rust_core_diff(
186
+ *,
187
+ old_content: str,
188
+ new_content: str,
189
+ old_filename: str,
190
+ new_filename: str,
191
+ language_hint: str | None,
192
+ parser_plugin_id: str | None,
193
+ config: DiffConfig,
194
+ ) -> RustCoreAttempt:
195
+ """Try the optional Rust core and return a fallback reason on failure."""
196
+ if not rust_core_enabled(config):
197
+ return RustCoreAttempt(fallback_reason="disabled")
198
+ parser_plugin_id = _canonical_python_plugin_id(parser_plugin_id)
199
+ if parser_plugin_id is not None and parser_plugin_id not in _PYTHON_PLUGIN_IDS:
200
+ return RustCoreAttempt(fallback_reason="unsupported parser plugin")
201
+ if language_hint not in (None, "", "python"):
202
+ return RustCoreAttempt(fallback_reason="unsupported language hint")
203
+ if not (old_filename.endswith((".py", ".pyi")) or new_filename.endswith((".py", ".pyi"))):
204
+ return RustCoreAttempt(fallback_reason="unsupported filename")
205
+
206
+ try:
207
+ backend = _load_backend()
208
+ except Exception as exc: # noqa: BLE001 - optional accelerator must never break diffing.
209
+ logger.debug("Rust core unavailable: %s", exc)
210
+ return RustCoreAttempt(fallback_reason=f"unavailable: {exc}")
211
+
212
+ try:
213
+ supports = getattr(backend, "supports_language", None)
214
+ if callable(supports) and not supports("python"):
215
+ return RustCoreAttempt(fallback_reason="backend does not support python")
216
+ backend_version = str(getattr(backend, "version", lambda: "")())
217
+ payload = backend.diff_python_json(
218
+ old_content,
219
+ new_content,
220
+ old_filename,
221
+ new_filename,
222
+ config.model_dump_json(),
223
+ )
224
+ diff = SemanticDiff.model_validate_json(payload)
225
+ metadata = dict(diff.metadata)
226
+ rust_metadata = dict(metadata.get("rust_core") or {})
227
+ status = str(rust_metadata.get("status") or "")
228
+ if status != _COMPLETE_STATUS:
229
+ return RustCoreAttempt(
230
+ fallback_reason=f"incomplete result status: {status or '<missing>'}",
231
+ backend_version=backend_version,
232
+ )
233
+ rust_metadata.update(
234
+ {
235
+ "used": True,
236
+ "backend_version": backend_version,
237
+ }
238
+ )
239
+ metadata["rust_core"] = rust_metadata
240
+ diff = diff.model_copy(update={"metadata": metadata})
241
+ return RustCoreAttempt(diff=diff, backend_version=backend_version)
242
+ except (ValidationError, ValueError, TypeError) as exc:
243
+ logger.debug("Rust core output rejected: %s", exc)
244
+ return RustCoreAttempt(fallback_reason=f"invalid output: {exc}")
245
+ except Exception as exc: # noqa: BLE001 - optional accelerator must fall back.
246
+ logger.debug("Rust core failed: %s", exc, exc_info=True)
247
+ return RustCoreAttempt(fallback_reason=f"failed: {exc}")
248
+
249
+
250
+ def _pyo3_extension_module_name() -> str | None:
251
+ """Return the importable name of the compiled PyO3 extension, or ``None``.
252
+
253
+ Uses ``find_spec`` (never ``import``) and requires the spec's ``origin`` to be a compiled
254
+ extension file (``.pyd``/``.so``), so it detects the real PyO3 build (dev / maturin) while
255
+ NOT executing anything in the PyO3-free cdylib wheel — where ``intentumdiff.intentumdiff_rust_core``
256
+ is a package (its ``__init__`` is the unused cffi wrapper), not an extension module.
257
+ """
258
+ import importlib.machinery # noqa: PLC0415
259
+ import importlib.util # noqa: PLC0415
260
+
261
+ ext_suffixes = tuple(importlib.machinery.EXTENSION_SUFFIXES)
262
+ # Cover both layouts: the .pyd bundled inside the `intentumdiff` package (release pyo3 wheel:
263
+ # `intentumdiff.intentumdiff_rust_core`) and the standalone maturin package whose extension is the
264
+ # like-named submodule (dev: `intentumdiff_rust_core.intentumdiff_rust_core`).
265
+ for module_name in (
266
+ "intentumdiff.intentumdiff_rust_core",
267
+ "intentumdiff_rust_core.intentumdiff_rust_core",
268
+ "intentumdiff_rust_core",
269
+ ):
270
+ try:
271
+ spec = importlib.util.find_spec(module_name)
272
+ except (ModuleNotFoundError, ValueError, ImportError):
273
+ continue
274
+ if spec is not None and spec.origin and spec.origin.endswith(ext_suffixes):
275
+ return module_name
276
+ return None
277
+
278
+
279
+ def _load_backend() -> Any:
280
+ # Backend selection (Phase B / #82). The C ABI (`intentumdiff_call`) is the language-agnostic
281
+ # boundary; the PyO3 extension is the transitional in-process fast path. Order:
282
+ # 1. explicit override — `INTENTUMDIFF_RUST_CORE_CTYPES=1` forces the pure-ctypes proxy,
283
+ # `INTENTUMDIFF_RUST_CORE_PYO3=1` forces PyO3.
284
+ # 2. auto — PyO3 when a compiled extension module is present (dev/maturin build; direct calls
285
+ # are faster); otherwise the pure-ctypes proxy over the cdylib (the PyO3-free wheel).
286
+ if os.getenv("INTENTUMDIFF_RUST_CORE_CTYPES", "").strip() == "1":
287
+ return _ctypes_backend()
288
+ force_pyo3 = os.getenv("INTENTUMDIFF_RUST_CORE_PYO3", "").strip() == "1"
289
+ module_name = _pyo3_extension_module_name()
290
+ if module_name is not None:
291
+ return importlib.import_module(module_name)
292
+ if force_pyo3:
293
+ raise ModuleNotFoundError("intentumdiff_rust_core PyO3 extension (INTENTUMDIFF_RUST_CORE_PYO3=1)")
294
+ return _ctypes_backend()
295
+
296
+
297
+ # ── Stateless C-ABI accessor (intentumdiff_call) ───────────────────────────────────────────────
298
+ # The compiled core exports the language-agnostic C ABI (`intentumdiff_call`/`intentumdiff_free`,
299
+ # ungated) alongside the PyO3 module, so the SAME artifact serves both. Handlers reachable ONLY
300
+ # over the C ABI (the stateless `cache_*` store surface — there is no PyO3 pyfunction for them)
301
+ # are driven through here. This is also the seam the full ctypes binding (B.5) grows from as the
302
+ # remaining stateless functions migrate off PyO3.
303
+ _C_ABI_LIB: Any = None
304
+
305
+
306
+ def _extension_path() -> str:
307
+ """Filesystem path to the compiled core that exports the C ABI (`intentumdiff_call`).
308
+
309
+ Handles both shapes: the PyO3 extension (dev / maturin build — a ``.pyd``/``.so`` module) and
310
+ the bare cdylib shipped as a data file in the PyO3-free wheel
311
+ (``intentumdiff/intentumdiff_rust_core/intentumdiff_rust_core.{dll,so,dylib}``). Uses ``find_spec``
312
+ throughout — never ``import`` — so the cdylib wheel's cffi wrapper ``__init__`` never runs.
313
+ """
314
+ import importlib.machinery # noqa: PLC0415
315
+ import importlib.util # noqa: PLC0415
316
+
317
+ ext_suffixes = tuple(importlib.machinery.EXTENSION_SUFFIXES)
318
+ lib_suffixes = (".dll", ".so", ".dylib")
319
+ # 1. The compiled PyO3 extension module — its origin IS the shared object.
320
+ name = _pyo3_extension_module_name()
321
+ if name is not None:
322
+ spec = importlib.util.find_spec(name)
323
+ if spec is not None and spec.origin:
324
+ return spec.origin
325
+ # 2. The bare cdylib: search the intentumdiff_rust_core subpackage dir (and the intentumdiff package
326
+ # dir) for `*intentumdiff_rust_core*.{dll,so,dylib}`, via find_spec locations.
327
+ search_dirs: list[str] = []
328
+ for package_name in ("intentumdiff.intentumdiff_rust_core", "intentumdiff_rust_core", "intentumdiff"):
329
+ try:
330
+ spec = importlib.util.find_spec(package_name)
331
+ except (ModuleNotFoundError, ValueError):
332
+ continue
333
+ if spec is None:
334
+ continue
335
+ locations = list(spec.submodule_search_locations or [])
336
+ if locations:
337
+ search_dirs.extend(locations)
338
+ elif spec.origin and spec.origin not in ("built-in", "namespace"):
339
+ search_dirs.append(os.path.dirname(spec.origin))
340
+ for directory in search_dirs:
341
+ try:
342
+ entries = sorted(os.listdir(directory))
343
+ except OSError:
344
+ continue
345
+ for entry in entries:
346
+ if "intentumdiff_rust_core" in entry and (
347
+ entry.endswith(lib_suffixes) or entry.endswith(ext_suffixes)
348
+ ):
349
+ return os.path.join(directory, entry)
350
+ raise RuntimeError("intentumdiff_rust_core compiled core not found for the C ABI")
351
+
352
+
353
+ def _c_abi_lib() -> Any:
354
+ global _C_ABI_LIB
355
+ if _C_ABI_LIB is None:
356
+ import ctypes # noqa: PLC0415
357
+
358
+ lib = ctypes.CDLL(_extension_path())
359
+ lib.intentumdiff_call.argtypes = [ctypes.c_char_p, ctypes.c_char_p]
360
+ lib.intentumdiff_call.restype = ctypes.c_void_p
361
+ lib.intentumdiff_free.argtypes = [ctypes.c_void_p]
362
+ lib.intentumdiff_free.restype = None
363
+ _C_ABI_LIB = lib
364
+ return _C_ABI_LIB
365
+
366
+
367
+ def _c_abi_call(name: str, *args: Any) -> Any:
368
+ """Invoke a C-ABI handler by name; return its ``result`` or re-raise the classified error.
369
+
370
+ `error_type` maps to the exception the PyO3 wrappers raised: ``not_found`` →
371
+ ``FileNotFoundError`` (the git readers' only non-``ValueError``), everything else →
372
+ ``ValueError``. Panics surface as ``RuntimeError``.
373
+ """
374
+ import ctypes # noqa: PLC0415
375
+
376
+ lib = _c_abi_lib()
377
+ # `bytes` args cross the ABI as a JSON array of byte values (the C ABI's `Vec<u8>` shape —
378
+ # e.g. detect_content_type's head slice), since raw bytes are not JSON-serialisable.
379
+ payload = [list(a) if isinstance(a, (bytes, bytearray)) else a for a in args]
380
+ ptr = lib.intentumdiff_call(
381
+ name.encode("utf-8"), json.dumps(payload).encode("utf-8")
382
+ )
383
+ if not ptr:
384
+ raise RuntimeError(f"intentumdiff_call({name!r}) returned NULL (allocation failure)")
385
+ try:
386
+ raw = ctypes.cast(ptr, ctypes.c_char_p).value
387
+ finally:
388
+ lib.intentumdiff_free(ptr)
389
+ envelope = json.loads((raw or b"").decode("utf-8"))
390
+ if envelope.get("ok"):
391
+ return envelope.get("result")
392
+ message = envelope.get("error", "engine error")
393
+ kind = envelope.get("error_type", "value_error")
394
+ if kind == "not_found":
395
+ raise FileNotFoundError(message)
396
+ if kind == "internal":
397
+ raise RuntimeError(message)
398
+ raise ValueError(message)
399
+
400
+
401
+ class _CtypesBackend:
402
+ """Pure-ctypes stand-in for the PyO3 module (#B.5): every attribute is a function that routes
403
+ through the C ABI. `getattr(backend, fn)` returns a callable — the code's ``getattr(..., None)``
404
+ guards always take the present-branch, which is correct since the C ABI covers the full
405
+ stateless pyfunction surface.
406
+
407
+ Naming: the C-ABI handler names dropped the Python-transport ``_json`` suffix (``finalize_review``
408
+ for the pyfunction ``finalize_review_json``), so a trailing ``_json`` is stripped — except the two
409
+ commit handlers, which keep the suffix AND return the ``(control_json, commit_bytes)`` binary tuple
410
+ the callers expect (the C ABI marshals it as a ``{control, commit_diff_json}`` envelope).
411
+
412
+ Result encoding: ``_c_abi_call`` returns the *parsed* ``result``. The PyO3 pyfunctions were
413
+ heterogeneous — some returned native values used directly (``supports_language`` -> bool,
414
+ ``validate_dep_hashes`` -> list, ``version`` -> str), others returned a JSON *string* the caller
415
+ ``json.loads``/``model_validate_json`` (``changed_sources_json``, ``diff_batch``, every ``*_json``
416
+ review/diff). The parsed value is already correct for the former; the latter are re-serialized
417
+ with ``json.dumps`` so the caller's parse still works. ``_RAW_HANDLERS`` is the (small) native set.
418
+ """
419
+
420
+ _COMMIT_TUPLE = frozenset({"diff_batch_commit_json", "diff_working_tree_python_commit_json"})
421
+ _RAW_HANDLERS = frozenset({
422
+ "version",
423
+ "supports_language",
424
+ "git_repo_toplevel",
425
+ "cache_make_key",
426
+ "cache_parse_key",
427
+ "cache_diff_key",
428
+ "cache_hover_map_key",
429
+ "validate_registry_ref",
430
+ "validate_dep_hashes",
431
+ "find_intentumdiff_config_path",
432
+ "vcs_backend_resolve_root",
433
+ "vcs_backend_merge_base",
434
+ })
435
+
436
+ def __getattr__(self, name: str) -> Any:
437
+ if name.startswith("__"):
438
+ raise AttributeError(name)
439
+ if name in self._COMMIT_TUPLE:
440
+ def _commit(request_json: str, _name: str = name) -> tuple[str, bytes | None]:
441
+ envelope = _c_abi_call(_name, request_json)
442
+ payload = envelope.get("commit_diff_json")
443
+ return (
444
+ json.dumps(envelope.get("control")),
445
+ payload.encode("utf-8") if payload is not None else None,
446
+ )
447
+
448
+ return _commit
449
+ handler = name[:-5] if name.endswith("_json") else name
450
+ if handler == "vcs_backend_get_blob":
451
+ # The C ABI wraps blob content in {"content": …} so JSON-valued content is not
452
+ # re-parsed by the envelope; the caller wants the bare string.
453
+ def _blob(*args: Any) -> str:
454
+ return _c_abi_call("vcs_backend_get_blob", *args)["content"]
455
+
456
+ return _blob
457
+ if handler == "register_user_xml_dialects":
458
+ # The pyfunction takes a JSON *string*; the C-ABI handler takes the parsed array.
459
+ def _register(dialects_json: str) -> Any:
460
+ return _c_abi_call("register_user_xml_dialects", json.loads(dialects_json))
461
+
462
+ return _register
463
+ raw = handler in self._RAW_HANDLERS
464
+
465
+ def _call(*args: Any, _handler: str = handler, _raw: bool = raw) -> Any:
466
+ result = _c_abi_call(_handler, *args)
467
+ return result if _raw else json.dumps(result)
468
+
469
+ return _call
470
+
471
+
472
+ _CTYPES_BACKEND: _CtypesBackend | None = None
473
+
474
+
475
+ def _ctypes_backend() -> _CtypesBackend:
476
+ global _CTYPES_BACKEND
477
+ if _CTYPES_BACKEND is None:
478
+ _CTYPES_BACKEND = _CtypesBackend()
479
+ return _CTYPES_BACKEND
480
+
481
+
482
+ def enrich_node_facts(tree: SemanticNode) -> SemanticNode:
483
+ """Fill language-agnostic NodeFacts (issue #70) on a Wasm-parsed tree via the Rust core.
484
+
485
+ Non-Python parsers emit ``facts: None``; this asks the Rust core to derive the same
486
+ privacy-safe structural facts (param_count, returns/return_kind, side_effects) from the
487
+ SemanticNode tree so every language gives the intent explainer structural signal. Idempotent —
488
+ nodes that already carry facts (the native Python path) are untouched. On any failure (backend
489
+ or function unavailable) the tree is returned unchanged: enrichment must never break a diff.
490
+ """
491
+ try:
492
+ backend = _load_backend()
493
+ enrich = getattr(backend, "enrich_node_facts_json", None)
494
+ if enrich is None:
495
+ return tree
496
+ return SemanticNode.model_validate_json(enrich(tree.model_dump_json()))
497
+ except Exception: # noqa: BLE001 — optional enrichment must degrade gracefully
498
+ return tree
499
+
500
+
501
+ def apply_invariances(
502
+ changes: list[Change],
503
+ *,
504
+ old_tree: SemanticNode,
505
+ new_tree: SemanticNode,
506
+ old_source: str,
507
+ new_source: str,
508
+ language: str,
509
+ ) -> RustInvarianceResult:
510
+ """Apply built-in invariance rules in the Rust engine."""
511
+
512
+ return _run_invariance_request(
513
+ {
514
+ "mode": "apply",
515
+ "changes": [change.model_dump(mode="json") for change in changes],
516
+ "old_tree": old_tree.model_dump(mode="json"),
517
+ "new_tree": new_tree.model_dump(mode="json"),
518
+ "old_source": old_source,
519
+ "new_source": new_source,
520
+ "language": language,
521
+ }
522
+ )
523
+
524
+
525
+ def build_style_only_evidence(
526
+ *,
527
+ old_source: str,
528
+ new_source: str,
529
+ language: str,
530
+ old_cst_json: str | None = None,
531
+ new_cst_json: str | None = None,
532
+ ) -> RustInvarianceResult:
533
+ """Build source-span style-only evidence in the Rust engine."""
534
+
535
+ return _run_invariance_request(
536
+ {
537
+ "mode": "style_only",
538
+ "old_source": old_source,
539
+ "new_source": new_source,
540
+ "language": language,
541
+ "old_cst_json": old_cst_json,
542
+ "new_cst_json": new_cst_json,
543
+ }
544
+ )
545
+
546
+
547
+ def build_zero_change_literal_evidence(
548
+ *,
549
+ old_tree: SemanticNode,
550
+ new_tree: SemanticNode,
551
+ old_source: str,
552
+ new_source: str,
553
+ language: str,
554
+ ) -> RustInvarianceResult:
555
+ """Build zero-change literal evidence in the Rust engine."""
556
+
557
+ return _run_invariance_request(
558
+ {
559
+ "mode": "zero_change_literal",
560
+ "old_tree": old_tree.model_dump(mode="json"),
561
+ "new_tree": new_tree.model_dump(mode="json"),
562
+ "old_source": old_source,
563
+ "new_source": new_source,
564
+ "language": language,
565
+ }
566
+ )
567
+
568
+
569
+ def build_scope_trails(
570
+ *,
571
+ old_tree: SemanticNode,
572
+ new_tree: SemanticNode,
573
+ changes: list[Change],
574
+ ) -> dict[str, Any]:
575
+ """Build hunk scope breadcrumb metadata in the Rust engine."""
576
+
577
+ try:
578
+ backend = _load_backend()
579
+ scope_fn = getattr(backend, "scope_trails_json", None)
580
+ if not callable(scope_fn):
581
+ raise RuntimeError("backend does not expose scope_trails_json")
582
+ payload = scope_fn(
583
+ json.dumps(
584
+ {
585
+ "old_tree": old_tree.model_dump(mode="json"),
586
+ "new_tree": new_tree.model_dump(mode="json"),
587
+ "changes": [change.model_dump(mode="json") for change in changes],
588
+ }
589
+ )
590
+ )
591
+ data = json.loads(payload)
592
+ except Exception as exc: # noqa: BLE001 - scope computation belongs to Rust.
593
+ raise RuntimeError(f"Rust scope trail engine unavailable: {exc}") from exc
594
+
595
+ scope_trails = data.get("scope_trails") if isinstance(data, dict) else None
596
+ return dict(scope_trails) if isinstance(scope_trails, dict) else {}
597
+
598
+
599
+ def try_rust_generic_text_review(
600
+ old_source: str,
601
+ new_source: str,
602
+ raw_change_count: int,
603
+ ) -> tuple[list[Change], list[ChangeGroup]] | None:
604
+ """Run the Rust generic-text review stage; None means fall back to Python.
605
+
606
+ Engine-side port of the generic text presentation (issue #35): line diff,
607
+ relocated-line netting, blank symmetry, inline char detail, and the
608
+ presentation.generic_text_diff suppression audit group.
609
+ """
610
+ try:
611
+ backend = _load_backend()
612
+ review_fn = getattr(backend, "generic_text_review_json", None)
613
+ if not callable(review_fn):
614
+ return None
615
+ data = json.loads(review_fn(old_source, new_source, raw_change_count))
616
+ if not isinstance(data, dict) or not data.get("used"):
617
+ return None
618
+ changes = [Change.model_validate(item) for item in data.get("changes") or []]
619
+ groups: list[ChangeGroup] = []
620
+ group = data.get("group")
621
+ if group:
622
+ groups.append(ChangeGroup.model_validate(group))
623
+ return changes, groups
624
+ except Exception as exc: # noqa: BLE001 - strangler boundary must fall back.
625
+ logger.debug("Rust generic-text review unavailable: %s", exc, exc_info=True)
626
+ return None
627
+
628
+
629
+ def try_rust_markdown_section_review(
630
+ old_source: str,
631
+ new_source: str,
632
+ ) -> dict[str, object] | None:
633
+ """Run the Rust markdown section review (moves + heading renames); None -> Python.
634
+
635
+ Engine-side port of the markdown post-presentation rules (issue #36): section
636
+ identity by content hash, LIS insertion-shift discrimination for moves, and
637
+ unique-body-hash heading renames.
638
+ """
639
+ try:
640
+ backend = _load_backend()
641
+ review_fn = getattr(backend, "markdown_section_review_json", None)
642
+ if not callable(review_fn):
643
+ return None
644
+ data = json.loads(review_fn(old_source, new_source))
645
+ if not isinstance(data, dict) or not data.get("used"):
646
+ return None
647
+ return {
648
+ "moves": [Change.model_validate(item) for item in data.get("moves") or []],
649
+ "moved_labels": set(data.get("moved_labels") or []),
650
+ "move_group": (
651
+ ChangeGroup.model_validate(data["move_group"]) if data.get("move_group") else None
652
+ ),
653
+ "renames": [Change.model_validate(item) for item in data.get("renames") or []],
654
+ "old_heading_lines": set(data.get("old_heading_lines") or []),
655
+ "new_heading_lines": set(data.get("new_heading_lines") or []),
656
+ "rename_group": (
657
+ ChangeGroup.model_validate(data["rename_group"])
658
+ if data.get("rename_group")
659
+ else None
660
+ ),
661
+ }
662
+ except Exception as exc: # noqa: BLE001 - strangler boundary must fall back.
663
+ logger.debug("Rust markdown section review unavailable: %s", exc, exc_info=True)
664
+ return None
665
+
666
+
667
+ def try_rust_collect_hover_targets(
668
+ root: SemanticNode,
669
+ ) -> list[tuple[str, int, int]] | None:
670
+ """Collect LSP hover targets in the Rust core (issue 100 S2); None -> Python walk.
671
+
672
+ Returns ``(result_node_id, line, col)`` triples mirroring
673
+ ``TypeEnricher._collect_hover_targets``: name-type leaves hover at their own
674
+ position; declaration/parameter nodes hover at their first name-leaf but store
675
+ under the declaration node's id.
676
+ """
677
+ try:
678
+ backend = _load_backend()
679
+ collect_fn = getattr(backend, "lsp_collect_hover_targets_json", None)
680
+ if not callable(collect_fn):
681
+ return None
682
+ data = json.loads(collect_fn(root.model_dump_json()))
683
+ if not isinstance(data, list):
684
+ return None
685
+ return [
686
+ (str(item["id"]), int(item["line"]), int(item["col"]))
687
+ for item in data
688
+ if isinstance(item, dict)
689
+ ]
690
+ except Exception as exc: # noqa: BLE001 - strangler boundary must fall back.
691
+ logger.debug("Rust hover-target collection unavailable: %s", exc, exc_info=True)
692
+ return None
693
+
694
+
695
+ def try_rust_finalize_review(
696
+ *,
697
+ old_tree: SemanticNode,
698
+ new_tree: SemanticNode,
699
+ old_source: str,
700
+ new_source: str,
701
+ language: str,
702
+ config: DiffConfig,
703
+ collect_trace: bool = False,
704
+ ) -> dict[str, Any] | None:
705
+ """Route the per-stage draft finalization through the Rust core (issue #57).
706
+
707
+ Runs the SAME refine+finalize pipeline as the certified batch from the provided
708
+ semantic trees. None -> caller stays on the Python transitional path.
709
+ """
710
+ try:
711
+ backend = _load_backend()
712
+ finalize_fn = getattr(backend, "finalize_review_json", None)
713
+ if not callable(finalize_fn):
714
+ return None
715
+ config_json = json.dumps(
716
+ {
717
+ "min_height": config.min_height,
718
+ "min_similarity": config.min_similarity,
719
+ "max_nodes": config.max_nodes,
720
+ "collect_trace": collect_trace,
721
+ }
722
+ )
723
+ data = json.loads(
724
+ finalize_fn(
725
+ old_tree.model_dump_json(),
726
+ new_tree.model_dump_json(),
727
+ old_source,
728
+ new_source,
729
+ language,
730
+ config_json,
731
+ )
732
+ )
733
+ if not isinstance(data, dict) or not data.get("used"):
734
+ return None
735
+ return {
736
+ "changes": [Change.model_validate(item) for item in data.get("changes") or []],
737
+ "change_groups": [
738
+ ChangeGroup.model_validate(item) for item in data.get("change_groups") or []
739
+ ],
740
+ "is_style_only": bool(data.get("is_style_only")),
741
+ "no_surviving_changes": bool(data.get("no_surviving_changes")),
742
+ "ignored_style_changes": [
743
+ dict(item)
744
+ for item in data.get("ignored_style_changes") or []
745
+ if isinstance(item, dict)
746
+ ],
747
+ "trace": [
748
+ dict(item)
749
+ for item in data.get("trace") or []
750
+ if isinstance(item, dict)
751
+ ],
752
+ }
753
+ except Exception as exc: # noqa: BLE001 - strangler boundary must fall back.
754
+ logger.debug("Rust finalize review unavailable: %s", exc, exc_info=True)
755
+ return None
756
+
757
+
758
+ def try_rust_profile_label_enrichment(
759
+ tree: SemanticNode,
760
+ source: str,
761
+ language: str,
762
+ identity_fields: tuple[str, ...] = (),
763
+ ) -> SemanticNode | None:
764
+ """Profile-label enrichment in the Rust core (issue #57 profile-enrichment port).
765
+
766
+ Returns the enriched tree, or None when the backend is unavailable (caller
767
+ falls back to the python profile enrichment until every family is ported).
768
+ """
769
+ try:
770
+ backend = _load_backend()
771
+ enrich_fn = getattr(backend, "enrich_profile_labels_json", None)
772
+ if not callable(enrich_fn):
773
+ return None
774
+ return SemanticNode.model_validate_json(
775
+ enrich_fn(tree.model_dump_json(), source, language, list(identity_fields))
776
+ )
777
+ except Exception as exc: # noqa: BLE001 - strangler boundary must fall back.
778
+ logger.debug("Rust profile enrichment unavailable: %s", exc, exc_info=True)
779
+ return None
780
+
781
+
782
+ def try_rust_build_symbol_table(files_json: str) -> str | None:
783
+ """Symbol-table extraction in the Rust core (index-engine-lib, #91).
784
+
785
+ *files_json* is a JSON array of ``{filename, language, tree}`` entries.
786
+ Returns the SymbolTable JSON, or None when the backend/entrypoint is
787
+ unavailable or the core reports an ``{"error": ...}`` envelope. This is the
788
+ Rust-authoritative replacement for the retired Python symbol extraction and
789
+ the index-engine Wasm-adapter round-trip.
790
+ """
791
+ try:
792
+ backend = _load_backend()
793
+ build_fn = getattr(backend, "build_symbol_table_json", None)
794
+ if not callable(build_fn):
795
+ return None
796
+ result = build_fn(files_json)
797
+ parsed = json.loads(result)
798
+ if isinstance(parsed, dict) and "error" in parsed:
799
+ logger.debug("Rust build_symbol_table error: %s", parsed["error"])
800
+ return None
801
+ return result
802
+ except Exception as exc: # noqa: BLE001 - engine boundary must degrade, not crash.
803
+ logger.debug("Rust build_symbol_table unavailable: %s", exc, exc_info=True)
804
+ return None
805
+
806
+
807
+ def try_rust_build_reference_table(files_json: str) -> str | None:
808
+ """Reference-table extraction in the Rust core (index-engine-lib, #91).
809
+
810
+ Returns the ReferenceTable JSON, or None when the backend is unavailable or
811
+ the core reports an error envelope.
812
+ """
813
+ try:
814
+ backend = _load_backend()
815
+ build_fn = getattr(backend, "build_reference_table_json", None)
816
+ if not callable(build_fn):
817
+ return None
818
+ result = build_fn(files_json)
819
+ parsed = json.loads(result)
820
+ if isinstance(parsed, dict) and "error" in parsed:
821
+ logger.debug("Rust build_reference_table error: %s", parsed["error"])
822
+ return None
823
+ return result
824
+ except Exception as exc: # noqa: BLE001 - engine boundary must degrade, not crash.
825
+ logger.debug("Rust build_reference_table unavailable: %s", exc, exc_info=True)
826
+ return None
827
+
828
+
829
+ def try_rust_diff_symbol_tables(
830
+ old_json: str, new_json: str
831
+ ) -> list[dict[str, Any]] | None:
832
+ """Cross-file diff in the Rust core (index-engine-lib, #91).
833
+
834
+ *old_json* / *new_json* are serialised symbol tables. Returns the parsed
835
+ list of cross-file change dicts (MOVE_TO_MODULE / SPLIT_MODULE /
836
+ CROSS_FILE_RENAME), or None when the backend is unavailable or the core
837
+ reports an error envelope.
838
+ """
839
+ try:
840
+ backend = _load_backend()
841
+ diff_fn = getattr(backend, "diff_symbol_tables_json", None)
842
+ if not callable(diff_fn):
843
+ return None
844
+ result = json.loads(diff_fn(old_json, new_json))
845
+ if isinstance(result, dict) and "error" in result:
846
+ logger.debug("Rust diff_symbol_tables error: %s", result["error"])
847
+ return None
848
+ return result
849
+ except Exception as exc: # noqa: BLE001 - engine boundary must degrade, not crash.
850
+ logger.debug("Rust diff_symbol_tables unavailable: %s", exc, exc_info=True)
851
+ return None
852
+
853
+
854
+ def try_rust_evaluate_guardrail_rules(
855
+ request: dict[str, Any],
856
+ ) -> list[dict[str, Any]] | None:
857
+ """Guardrail rule evaluation in the Rust core (#91 A1.3b).
858
+
859
+ *request* carries the diff language/filenames, the old/new trees, the diff's
860
+ changed-node ids, and the parsed policy rules. The core matches protected paths
861
+ against the changes and returns the list of violation dicts. Returns None when
862
+ the backend/entrypoint is unavailable or reports an error envelope.
863
+ """
864
+ try:
865
+ backend = _load_backend()
866
+ eval_fn = getattr(backend, "evaluate_guardrail_rules_json", None)
867
+ if not callable(eval_fn):
868
+ return None
869
+ result = json.loads(eval_fn(json.dumps(request)))
870
+ if isinstance(result, dict) and "error" in result:
871
+ logger.debug("Rust evaluate_guardrail_rules error: %s", result["error"])
872
+ return None
873
+ return result
874
+ except Exception as exc: # noqa: BLE001 - engine boundary must degrade, not crash.
875
+ logger.debug("Rust guardrail evaluation unavailable: %s", exc, exc_info=True)
876
+ return None
877
+
878
+
879
+ def load_project_diff_config_data(
880
+ start_path: str | None, explicit_path: str | None
881
+ ) -> dict[str, Any]:
882
+ """Resolve the ``intentumdiff.yaml`` ``config`` section in the Rust core (#99, A2.1).
883
+
884
+ Returns the validated config mapping (empty when there is no file/section).
885
+ Rust-authoritative: the file walk, YAML parse, and unsupported-key rejection run
886
+ in the core. A ``ValueError`` raised by the core (malformed file / unsupported
887
+ keys) propagates unchanged, matching the retired Python behaviour.
888
+ """
889
+ backend = _load_backend()
890
+ load_fn = getattr(backend, "load_project_diff_config_json", None)
891
+ if not callable(load_fn):
892
+ raise RuntimeError("backend does not expose load_project_diff_config_json")
893
+ return json.loads(load_fn(start_path, explicit_path))
894
+
895
+
896
+ def resolve_intentumdiff_config_path(
897
+ start_path: str | None, explicit_path: str | None
898
+ ) -> str | None:
899
+ """Find the nearest ``intentumdiff.yaml`` in the Rust core (#99, A2.1)."""
900
+ backend = _load_backend()
901
+ find_fn = getattr(backend, "find_intentumdiff_config_path", None)
902
+ if not callable(find_fn):
903
+ raise RuntimeError("backend does not expose find_intentumdiff_config_path")
904
+ return find_fn(start_path, explicit_path)
905
+
906
+
907
+ # ── Cache key generation (Rust-authoritative, #101, A2.2) ──────────────────
908
+ # Deterministic SHA-256 cache keys computed in the core so every binding agrees.
909
+
910
+
911
+ def cache_make_key(parts: list[str]) -> str:
912
+ """SHA-256 hex of length-prefixed parts (Rust ``store._make_key``)."""
913
+ return _load_backend().cache_make_key(list(parts))
914
+
915
+
916
+ def cache_parse_key(filtered_cst_or_content: str, grammar_id: str, wasm_hash: str) -> str:
917
+ """Cache key for a single-file parser result (Rust)."""
918
+ return _load_backend().cache_parse_key(filtered_cst_or_content, grammar_id, wasm_hash)
919
+
920
+
921
+ def cache_diff_key(
922
+ old_preprocessed: str, new_preprocessed: str, grammar_id: str, wasm_hash: str
923
+ ) -> str:
924
+ """Cache key for a full-pipeline SemanticDiff result (Rust)."""
925
+ return _load_backend().cache_diff_key(
926
+ old_preprocessed, new_preprocessed, grammar_id, wasm_hash
927
+ )
928
+
929
+
930
+ def cache_hover_map_key(content: str, language: str) -> str:
931
+ """Cache key for a file's LSP hover-type map (Rust)."""
932
+ return _load_backend().cache_hover_map_key(content, language)
933
+
934
+
935
+ class _RustCacheStore:
936
+ """Stateless proxy over the disk-cache C ABI (`cache_*` handlers, #B.5).
937
+
938
+ Holds only ``(path, ttl_days, max_mb)`` — no live object crosses the boundary. Each method
939
+ calls the corresponding `cache_*` handler, which resolves a warm ``SqliteStore`` from the
940
+ Rust-side registry keyed on that tuple, so the connection is reused across calls even though
941
+ the API is stateless. Return shapes match the retired PyO3 ``SqliteCacheStore`` pyclass
942
+ exactly, so the Python ``cache.sqlite_store.SqliteCacheStore`` delegator is unchanged: the
943
+ admin methods return JSON *strings* (the delegator ``json.loads`` them); ``get_*`` return the
944
+ stored string or ``None``.
945
+ """
946
+
947
+ __slots__ = ("_p", "_t", "_m")
948
+
949
+ def __init__(self, path: str, ttl_days: int, max_mb: int) -> None:
950
+ self._p, self._t, self._m = str(path), int(ttl_days), int(max_mb)
951
+ # Open-on-construct parity with the retired pyclass: creates the DB file + schema and
952
+ # runs eviction eagerly, so constructing a store persists an (empty) cache.
953
+ _c_abi_call("cache_open", self._p, self._t, self._m)
954
+
955
+ def get_parse(self, key: str) -> str | None:
956
+ return _c_abi_call("cache_get_parse", self._p, self._t, self._m, key)
957
+
958
+ def put_parse(self, key: str, value: str, grammar_id: str = "") -> None:
959
+ _c_abi_call("cache_put_parse", self._p, self._t, self._m, key, value, grammar_id)
960
+
961
+ def get_diff(self, key: str) -> str | None:
962
+ return _c_abi_call("cache_get_diff", self._p, self._t, self._m, key)
963
+
964
+ def put_diff(
965
+ self, key: str, value: str, language: str, old_filename: str, new_filename: str
966
+ ) -> None:
967
+ _c_abi_call(
968
+ "cache_put_diff", self._p, self._t, self._m, key, value, language, old_filename, new_filename
969
+ )
970
+
971
+ def get_symbol_index(self, cache_key: str) -> tuple[str, str] | None:
972
+ result = _c_abi_call("cache_get_symbol_index", self._p, self._t, self._m, cache_key)
973
+ return tuple(result) if result is not None else None
974
+
975
+ def put_symbol_index(
976
+ self, cache_key: str, symbols_json: str, refs_json: str, file_count: int
977
+ ) -> None:
978
+ _c_abi_call(
979
+ "cache_put_symbol_index", self._p, self._t, self._m, cache_key, symbols_json, refs_json, file_count
980
+ )
981
+
982
+ def get_hover_map(self, key: str) -> str | None:
983
+ return _c_abi_call("cache_get_hover_map", self._p, self._t, self._m, key)
984
+
985
+ def put_hover_map(self, key: str, value_json: str) -> None:
986
+ _c_abi_call("cache_put_hover_map", self._p, self._t, self._m, key, value_json)
987
+
988
+ def stats(self) -> str:
989
+ return json.dumps(_c_abi_call("cache_stats", self._p, self._t, self._m))
990
+
991
+ def metrics(self) -> str:
992
+ return json.dumps(_c_abi_call("cache_metrics", self._p, self._t, self._m))
993
+
994
+ def list_entries(
995
+ self,
996
+ table: str,
997
+ language: str | None,
998
+ since: int | None,
999
+ before: int | None,
1000
+ min_size: int | None,
1001
+ max_size: int | None,
1002
+ limit: int,
1003
+ with_glob: bool,
1004
+ ) -> str:
1005
+ return json.dumps(
1006
+ _c_abi_call(
1007
+ "cache_list_entries",
1008
+ self._p, self._t, self._m,
1009
+ table, language, since, before, min_size, max_size, limit, with_glob,
1010
+ )
1011
+ )
1012
+
1013
+ def get_entry_metadata(self, key: str, table: str) -> str | None:
1014
+ result = _c_abi_call("cache_get_entry_metadata", self._p, self._t, self._m, key, table)
1015
+ return json.dumps(result) if result is not None else None
1016
+
1017
+ def get_entry_payload(self, key: str, table: str) -> str | None:
1018
+ return _c_abi_call("cache_get_entry_payload", self._p, self._t, self._m, key, table)
1019
+
1020
+ def export_entries(self, table: str) -> str:
1021
+ return json.dumps(_c_abi_call("cache_export_entries", self._p, self._t, self._m, table))
1022
+
1023
+ def clear(self, parse: bool, diff: bool, index: bool, hover: bool) -> None:
1024
+ _c_abi_call("cache_clear", self._p, self._t, self._m, parse, diff, index, hover)
1025
+
1026
+ def close(self) -> None:
1027
+ _c_abi_call("cache_close", self._p, self._t, self._m)
1028
+
1029
+
1030
+ def sqlite_cache_store(path: str, ttl_days: int, max_mb: int) -> Any:
1031
+ """Return a stateless proxy over the native SQLite cache store (#101, A2.2; #B.5).
1032
+
1033
+ Rust-internal caching: the store logic + persistent connection live in the core (a warm
1034
+ `SqliteStore` in the process-global registry, keyed by ``(path, ttl_days, max_mb)``); this
1035
+ returns a stateless C-ABI proxy, so no PyO3 pyclass object crosses the boundary.
1036
+ """
1037
+ return _RustCacheStore(path, ttl_days, max_mb)
1038
+
1039
+
1040
+ class _RustAnalyticsStore:
1041
+ """Stateless proxy over the analytics C ABI (`analytics_*` handlers, #B.5).
1042
+
1043
+ Holds only the store ``path``; each method resolves a warm ``AnalyticsStore`` from the
1044
+ Rust-side registry (engine auto-selected at open). Return shapes match the retired PyO3
1045
+ ``AnalyticsStore`` pyclass exactly, so the Python ``cache.duckdb_store.DuckDBAnalyticsStore``
1046
+ delegator is unchanged: the query methods return JSON *strings* (the delegator ``json.loads``
1047
+ them), ``backend`` is a plain string, and ``record_diagnostics_run`` returns the run-id string.
1048
+ """
1049
+
1050
+ __slots__ = ("_p",)
1051
+
1052
+ def __init__(self, path: str) -> None:
1053
+ self._p = str(path)
1054
+ # Open-on-construct parity with the retired pyclass (creates the DB file + schema).
1055
+ _c_abi_call("analytics_open", self._p)
1056
+
1057
+ @property
1058
+ def backend(self) -> str:
1059
+ return _c_abi_call("analytics_backend", self._p)
1060
+
1061
+ def record_diff(self, diff_json: str) -> None:
1062
+ _c_abi_call("analytics_record_diff", self._p, diff_json)
1063
+
1064
+ def record_diagnostics_run(
1065
+ self,
1066
+ diffs_json: list[str],
1067
+ command: str,
1068
+ repo: str,
1069
+ argv_json: str,
1070
+ run_id: str | None,
1071
+ ) -> str:
1072
+ return _c_abi_call(
1073
+ "analytics_record_diagnostics_run", self._p, diffs_json, command, repo, argv_json, run_id
1074
+ )
1075
+
1076
+ def query(self, sql: str) -> str:
1077
+ return json.dumps(_c_abi_call("analytics_query", self._p, sql))
1078
+
1079
+ def query_readonly(self, sql: str) -> str:
1080
+ return json.dumps(_c_abi_call("analytics_query_readonly", self._p, sql))
1081
+
1082
+ def most_changed_files(self, limit: int) -> str:
1083
+ return json.dumps(_c_abi_call("analytics_most_changed_files", self._p, limit))
1084
+
1085
+ def changes_by_language(self) -> str:
1086
+ return json.dumps(_c_abi_call("analytics_changes_by_language", self._p))
1087
+
1088
+ def recent_diagnostic_runs(self, limit: int) -> str:
1089
+ return json.dumps(_c_abi_call("analytics_recent_diagnostic_runs", self._p, limit))
1090
+
1091
+ def fuel_by_language(self, limit: int) -> str:
1092
+ return json.dumps(_c_abi_call("analytics_fuel_by_language", self._p, limit))
1093
+
1094
+ def top_fuel_hotspots(self, limit: int) -> str:
1095
+ return json.dumps(_c_abi_call("analytics_top_fuel_hotspots", self._p, limit))
1096
+
1097
+ def close(self) -> None:
1098
+ _c_abi_call("analytics_close", self._p)
1099
+
1100
+
1101
+ def analytics_store(path: str) -> Any:
1102
+ """Return a stateless proxy over the native analytics store (#101, A2.2; #B.5).
1103
+
1104
+ Rust-internal: the store logic + connection live in the core (a warm ``AnalyticsStore`` in
1105
+ the process-global registry, keyed by path; engine — provided-DuckDB vs bundled-SQLite —
1106
+ auto-selected at open), so no PyO3 pyclass object crosses the boundary.
1107
+ """
1108
+ return _RustAnalyticsStore(path)
1109
+
1110
+
1111
+ # ── Registry-client security validators (Rust-authoritative, #97, A2.3) ────
1112
+
1113
+
1114
+ def validate_registry_ref(git_ref: str, strict: bool = False) -> None:
1115
+ """Validate a registry Git ref in the Rust core (path-traversal guard, #88/#97).
1116
+
1117
+ Raises ``ValueError`` when the ref is unsafe for URL-path interpolation (or, in
1118
+ ``strict`` mode, is not a full 40-char commit SHA).
1119
+ """
1120
+ backend = _load_backend()
1121
+ fn = getattr(backend, "validate_registry_ref", None)
1122
+ if not callable(fn):
1123
+ raise RuntimeError("backend does not expose validate_registry_ref")
1124
+ fn(git_ref, strict)
1125
+
1126
+
1127
+ def validate_dep_hashes(
1128
+ dep_hashes: list[tuple[str, str]],
1129
+ allowed_dependencies: list[str],
1130
+ package_name: str,
1131
+ install_target: str | None,
1132
+ ) -> list[str]:
1133
+ """Validate a plugin's ``dep_hashes`` (format + coverage) in the Rust core (#88/#97).
1134
+
1135
+ Returns human-readable error strings ([] = well-formed, hash-pinned install).
1136
+ """
1137
+ backend = _load_backend()
1138
+ fn = getattr(backend, "validate_dep_hashes", None)
1139
+ if not callable(fn):
1140
+ raise RuntimeError("backend does not expose validate_dep_hashes")
1141
+ return fn(dep_hashes, allowed_dependencies, package_name, install_target)
1142
+
1143
+
1144
+ # ── Git source-collection read ops (Rust-authoritative, #98, A2.4) ──────────
1145
+
1146
+
1147
+ def git_source_content(
1148
+ repo_path: str, file_path: str, old_ref: str, new_ref: str
1149
+ ) -> tuple[str, str]:
1150
+ """Read ``(old_content, new_content)`` of *file_path* at *old_ref*/*new_ref*.
1151
+
1152
+ Delegates the git object-model work (resolve ref, read blob) to the Rust core
1153
+ (shelling to the git CLI). Raises ``FileNotFoundError`` when the path is absent
1154
+ on either side, ``ValueError`` for an unresolvable ref.
1155
+ """
1156
+ backend = _load_backend()
1157
+ fn = getattr(backend, "git_source_content_json", None)
1158
+ if not callable(fn):
1159
+ raise RuntimeError("backend does not expose git_source_content_json")
1160
+ data = json.loads(fn(repo_path, file_path, old_ref, new_ref))
1161
+ return data["old_content"], data["new_content"]
1162
+
1163
+
1164
+ def working_tree_source_content(
1165
+ repo_path: str, file_path: str, ref: str
1166
+ ) -> tuple[str, str]:
1167
+ """Read ``(old_content, new_content)`` where old = *file_path* blob at *ref* and
1168
+ new = the working-tree file on disk. Raises ``FileNotFoundError`` for a missing
1169
+ blob or working-tree file, ``ValueError`` for a bare repo / unresolvable ref.
1170
+ """
1171
+ backend = _load_backend()
1172
+ fn = getattr(backend, "working_tree_source_content_json", None)
1173
+ if not callable(fn):
1174
+ raise RuntimeError("backend does not expose working_tree_source_content_json")
1175
+ data = json.loads(fn(repo_path, file_path, ref))
1176
+ return data["old_content"], data["new_content"]
1177
+
1178
+
1179
+ def git_repo_toplevel(repo_path: str) -> str:
1180
+ """Return the working-tree root for *repo_path*, raising ``ValueError`` when it is
1181
+ not inside a git work tree (replaces GitPython's InvalidGitRepositoryError)."""
1182
+ backend = _load_backend()
1183
+ fn = getattr(backend, "git_repo_toplevel", None)
1184
+ if not callable(fn):
1185
+ raise RuntimeError("backend does not expose git_repo_toplevel")
1186
+ return fn(repo_path)
1187
+
1188
+
1189
+ def changed_commit_sources(
1190
+ repo_path: str, old_ref: str, new_ref: str
1191
+ ) -> list[tuple[str, str, str, str, str | None]]:
1192
+ """Yield changed-source rows for a commit-to-commit diff via the Rust core.
1193
+
1194
+ Each row is ``(old_content, new_content, old_path, new_path, staging_status)`` with
1195
+ ``staging_status`` always ``None`` for commit-to-commit. Binary files are skipped.
1196
+ """
1197
+ backend = _load_backend()
1198
+ fn = getattr(backend, "changed_commit_sources_json", None)
1199
+ if not callable(fn):
1200
+ raise RuntimeError("backend does not expose changed_commit_sources_json")
1201
+ return [tuple(row) for row in json.loads(fn(repo_path, old_ref, new_ref))]
1202
+
1203
+
1204
+ def changed_sources(
1205
+ repo_path: str, old_ref: str, new_ref: str
1206
+ ) -> list[tuple[str, str, str, str, str | None]]:
1207
+ """Yield changed-source rows for any diff mode via the Rust core (the full
1208
+ ``iter_changed_sources`` dispatcher).
1209
+
1210
+ Each row is ``(old_content, new_content, old_path, new_path, staging_status)``.
1211
+ ``new_ref`` sentinels: ``""`` = working tree (per-file "staged"/"unstaged" +
1212
+ "untracked"), ``":staged"`` = the index, ``":unpushed"`` = commits not yet pushed;
1213
+ anything else is a commit-to-commit diff (``staging_status`` is ``None``).
1214
+ """
1215
+ backend = _load_backend()
1216
+ fn = getattr(backend, "changed_sources_json", None)
1217
+ if not callable(fn):
1218
+ raise RuntimeError("backend does not expose changed_sources_json")
1219
+ return [tuple(row) for row in json.loads(fn(repo_path, old_ref, new_ref))]
1220
+
1221
+
1222
+ # ── VCS backend ops (Rust-authoritative, #98, A2.4.4) ───────────────────────
1223
+ # `vcs` is one of "git" / "hg" / "svn" / "p4"; the core shells to that CLI.
1224
+
1225
+
1226
+ def vcs_backend_resolve_root(vcs: str, repo_path: str) -> str:
1227
+ """Work-tree root for *repo_path* under *vcs* (raises ValueError on failure)."""
1228
+ return _load_backend().vcs_backend_resolve_root(vcs, repo_path)
1229
+
1230
+
1231
+ def vcs_backend_get_blob(
1232
+ vcs: str, repo_path: str, path: str, ref: str, svn_repo_url: str | None = None
1233
+ ) -> str:
1234
+ """Content of *path* at *ref* ("" when absent).
1235
+
1236
+ *svn_repo_url* (svn only) targets a repository URL instead of the working copy.
1237
+ """
1238
+ return _load_backend().vcs_backend_get_blob(vcs, repo_path, path, ref, svn_repo_url)
1239
+
1240
+
1241
+ def vcs_backend_changed_files(
1242
+ vcs: str, repo_path: str, ref_a: str, ref_b: str, svn_repo_url: str | None = None
1243
+ ) -> list[dict[str, Any]]:
1244
+ """Changed files between two refs as dicts (old_path/new_path/change_type/is_binary).
1245
+
1246
+ *svn_repo_url* (svn only) targets a repository URL instead of the working copy.
1247
+ """
1248
+ return json.loads(
1249
+ _load_backend().vcs_backend_changed_files_json(
1250
+ vcs, repo_path, ref_a, ref_b, svn_repo_url
1251
+ )
1252
+ )
1253
+
1254
+
1255
+ def vcs_backend_working_tree_changes(
1256
+ vcs: str, repo_path: str, ref: str
1257
+ ) -> list[dict[str, Any]]:
1258
+ """Uncommitted working-tree changes vs *ref* (binaries skipped)."""
1259
+ return json.loads(
1260
+ _load_backend().vcs_backend_working_tree_changes_json(vcs, repo_path, ref)
1261
+ )
1262
+
1263
+
1264
+ def vcs_backend_merge_base(vcs: str, repo_path: str, ref_a: str, ref_b: str) -> str:
1265
+ """Common ancestor of two refs (raises ValueError when none)."""
1266
+ return _load_backend().vcs_backend_merge_base(vcs, repo_path, ref_a, ref_b)
1267
+
1268
+
1269
+ # --- Live-server protocol layer (#100, A2.5) -------------------------------
1270
+
1271
+
1272
+ def live_error_response(
1273
+ seq: int, code: str, message: str, op: str = "error"
1274
+ ) -> dict[str, Any]:
1275
+ """The protocol-v2 error envelope (`LiveServer._error_response`)."""
1276
+ return json.loads(_load_backend().live_error_response_json(seq, code, message, op))
1277
+
1278
+
1279
+ def live_limits() -> dict[str, Any]:
1280
+ """The wire limits advertised in ready/hello (`LiveServer._limits`)."""
1281
+ return json.loads(_load_backend().live_limits_json())
1282
+
1283
+
1284
+ def live_capabilities() -> dict[str, Any]:
1285
+ """The capability set advertised in ready/hello (`LiveServer._capabilities`)."""
1286
+ return json.loads(_load_backend().live_capabilities_json())
1287
+
1288
+
1289
+ def live_normalise_request_path(path: str) -> dict[str, Any]:
1290
+ """Repo-relative path guard: `{ok, path}` or `{ok: False, code, message}`."""
1291
+ return json.loads(_load_backend().live_normalise_request_path_json(path))
1292
+
1293
+
1294
+ def live_request_seq(request: dict[str, Any]) -> dict[str, Any]:
1295
+ """Validate the `seq` field: `{seq}` or `{error}` (`LiveServer._request_seq`)."""
1296
+ return json.loads(_load_backend().live_request_seq_json(json.dumps(request)))
1297
+
1298
+
1299
+ def live_parse_diff_request(request: dict[str, Any], default_ref: str) -> dict[str, Any]:
1300
+ """Validate a diff request: `{parsed}` or `{error}` (`LiveServer._parse_diff_request`)."""
1301
+ return json.loads(
1302
+ _load_backend().live_parse_diff_request_json(json.dumps(request), default_ref)
1303
+ )
1304
+
1305
+
1306
+ def live_parse_review_request(
1307
+ request: dict[str, Any], default_ref: str, seq: int
1308
+ ) -> dict[str, Any]:
1309
+ """Validate a review request's refs/stream: `{parsed}` or `{error}`."""
1310
+ return json.loads(
1311
+ _load_backend().live_parse_review_request_json(
1312
+ json.dumps(request), default_ref, seq
1313
+ )
1314
+ )
1315
+
1316
+
1317
+ def live_handle_diff(
1318
+ repo_path: str,
1319
+ path: str,
1320
+ ref: str,
1321
+ content: str,
1322
+ config_json: str,
1323
+ wasm_dir: str = "",
1324
+ ) -> dict[str, Any]:
1325
+ """Native single-file diff (Phase A1/R): `{diff}` (certified) or `{fallback: reason}`.
1326
+
1327
+ Serves any file the bundled-parser manifest in *wasm_dir* resolves (Python via the native
1328
+ tree-sitter path, other languages via their wasm parser); unresolved extensions and
1329
+ non-certified outcomes return a fallback marker for the Python differ path.
1330
+ """
1331
+ return json.loads(
1332
+ _load_backend().live_handle_diff_json(
1333
+ repo_path, path, ref, content, config_json, wasm_dir
1334
+ )
1335
+ )
1336
+
1337
+
1338
+ def live_handle_review(
1339
+ repo_path: str, old_ref: str, new_ref: str, config_json: str, wasm_dir: str = ""
1340
+ ) -> dict[str, Any]:
1341
+ """Native commit review (Phase A2 + d2-review): `{commit_diff}` or `{fallback: reason}`.
1342
+
1343
+ Each changed file resolves via the bundled-parser manifest in *wasm_dir*: an all-Python commit
1344
+ runs the certified batch commit; a commit with resolvable non-Python files is served per-file
1345
+ (Python via the certified batch item, other languages via the native single-file chain).
1346
+ Unresolvable extensions and non-served outcomes return a fallback marker for the Python
1347
+ CommitDiffer path. Older cores without the wasm_dir parameter degrade to the Python-only form.
1348
+ """
1349
+ backend = _load_backend()
1350
+ try:
1351
+ raw = backend.live_handle_review_json(repo_path, old_ref, new_ref, config_json, wasm_dir)
1352
+ except TypeError:
1353
+ # Older core: no wasm_dir parameter -> Python-only certified commit (non-Python falls back).
1354
+ raw = backend.live_handle_review_json(repo_path, old_ref, new_ref, config_json)
1355
+ return json.loads(raw)
1356
+
1357
+
1358
+ _registered_xml_dialects_fingerprint: str | None = None
1359
+
1360
+
1361
+ def try_register_user_xml_dialects(dialects: list[dict[str, Any]]) -> bool:
1362
+ """Marshal user XML dialect specs (issue #86) into the Rust registry.
1363
+
1364
+ Idempotent per payload; returns False when the backend or the entrypoint is
1365
+ unavailable (older core), in which case the diff proceeds without user XML
1366
+ dialects — the registration is additive, never load-bearing.
1367
+ """
1368
+ global _registered_xml_dialects_fingerprint
1369
+ payload = json.dumps(dialects, sort_keys=True)
1370
+ if payload == _registered_xml_dialects_fingerprint:
1371
+ return True
1372
+ try:
1373
+ backend = _load_backend()
1374
+ register_fn = getattr(backend, "register_user_xml_dialects_json", None)
1375
+ if not callable(register_fn):
1376
+ return False
1377
+ register_fn(payload)
1378
+ _registered_xml_dialects_fingerprint = payload
1379
+ return True
1380
+ except Exception as exc: # noqa: BLE001 - strangler boundary must fall back.
1381
+ logger.debug("Rust XML dialect registration unavailable: %s", exc, exc_info=True)
1382
+ return False
1383
+
1384
+
1385
+ def _run_invariance_request(request: dict[str, Any]) -> RustInvarianceResult:
1386
+ try:
1387
+ backend = _load_backend()
1388
+ apply_fn = getattr(backend, "apply_invariances_json", None)
1389
+ if not callable(apply_fn):
1390
+ raise RuntimeError("backend does not expose apply_invariances_json")
1391
+ payload = apply_fn(json.dumps(request))
1392
+ data = json.loads(payload)
1393
+ except Exception as exc: # noqa: BLE001 - public engine boundary requires Rust here.
1394
+ raise RuntimeError(f"Rust invariance engine unavailable: {exc}") from exc
1395
+
1396
+ if not isinstance(data, dict):
1397
+ raise RuntimeError("Rust invariance engine returned a non-object payload")
1398
+ return RustInvarianceResult(
1399
+ changes=[Change.model_validate(item) for item in data.get("changes") or []],
1400
+ change_groups=[
1401
+ ChangeGroup.model_validate(item) for item in data.get("change_groups") or []
1402
+ ],
1403
+ ignored_style_changes=[
1404
+ dict(item)
1405
+ for item in data.get("ignored_style_changes") or []
1406
+ if isinstance(item, dict)
1407
+ ],
1408
+ )
1409
+
1410
+
1411
+ def try_rust_core_batch_diff(
1412
+ *,
1413
+ old_source: str,
1414
+ new_source: str,
1415
+ old_filename: str,
1416
+ new_filename: str,
1417
+ parser_wasm_path: str,
1418
+ language: str,
1419
+ parser_plugin_id: str | None,
1420
+ config: DiffConfig,
1421
+ file_lifecycle: str | None = None,
1422
+ ) -> RustCoreBatchAttempt:
1423
+ """Try the planned Rust V4 batch boundary and fall back unless final output is ready."""
1424
+ if not rust_core_enabled(config):
1425
+ return RustCoreBatchAttempt(fallback_reason="disabled")
1426
+ if language != "python":
1427
+ return RustCoreBatchAttempt(fallback_reason="unsupported language")
1428
+ parser_plugin_id = _canonical_python_plugin_id(parser_plugin_id)
1429
+ if parser_plugin_id is not None and parser_plugin_id not in _PYTHON_PLUGIN_IDS:
1430
+ return RustCoreBatchAttempt(fallback_reason="unsupported parser plugin")
1431
+ if config.diagnostics:
1432
+ return RustCoreBatchAttempt(fallback_reason="diagnostics require Python pipeline")
1433
+
1434
+ batch = try_rust_core_batch_diffs(
1435
+ files=[
1436
+ {
1437
+ "old_source": old_source,
1438
+ "new_source": new_source,
1439
+ "old_filename": old_filename,
1440
+ "new_filename": new_filename,
1441
+ "language": language,
1442
+ "parser_plugin_id": parser_plugin_id or "",
1443
+ "parser_wasm_path": parser_wasm_path,
1444
+ "file_lifecycle": file_lifecycle or "",
1445
+ }
1446
+ ],
1447
+ config=config,
1448
+ )
1449
+ if batch.diffs:
1450
+ return RustCoreBatchAttempt(
1451
+ diff=batch.diffs[0],
1452
+ backend_version=batch.backend_version,
1453
+ )
1454
+ return RustCoreBatchAttempt(
1455
+ fallback_reason=batch.fallback_reasons.get(0)
1456
+ or batch.fallback_reason
1457
+ or "batch item is incomplete",
1458
+ backend_version=batch.backend_version,
1459
+ )
1460
+
1461
+
1462
+ def _normalize_builtin_change_type_enums(diff: SemanticDiff) -> SemanticDiff:
1463
+ """Normalize known Rust JSON change types to public ChangeType enums."""
1464
+
1465
+ normalized_changes: list[Change] = []
1466
+ changed = False
1467
+ for change in diff.changes:
1468
+ change_type = change.change_type
1469
+ if isinstance(change_type, str):
1470
+ try:
1471
+ enum_change_type = ChangeType(change_type)
1472
+ except ValueError:
1473
+ normalized_changes.append(change)
1474
+ continue
1475
+ normalized_changes.append(
1476
+ change.model_copy(update={"change_type": enum_change_type})
1477
+ )
1478
+ changed = True
1479
+ else:
1480
+ normalized_changes.append(change)
1481
+ if not changed:
1482
+ return diff
1483
+ return diff.model_copy(update={"changes": normalized_changes})
1484
+
1485
+
1486
+ def _stamp_certified_rust_diff(metadata: dict[str, Any]) -> dict[str, Any]:
1487
+ """Stamp the public ownership contract for accepted Rust-finalized diffs."""
1488
+
1489
+ stamped = dict(metadata)
1490
+ stamped.setdefault("engine_owner", "rust")
1491
+ stamped.setdefault("semantic_contract", "rust_finalized_v1")
1492
+ return stamped
1493
+
1494
+
1495
+ def _copy_diff_with_metadata(diff: SemanticDiff, metadata: dict[str, Any]) -> SemanticDiff:
1496
+ payload = diff.model_dump(mode="json")
1497
+ payload["metadata"] = metadata
1498
+ return SemanticDiff.model_validate(payload)
1499
+
1500
+
1501
+ def try_rust_core_batch_diffs(
1502
+ *,
1503
+ files: list[dict[str, Any]],
1504
+ config: DiffConfig,
1505
+ parallel: bool = False,
1506
+ max_workers: int | None = None,
1507
+ python_parser_backend: str = "native",
1508
+ ) -> RustCoreBatchDiffsAttempt:
1509
+ """Try the Rust V4 batch boundary for multiple certified Python file pairs."""
1510
+ if not rust_core_enabled(config):
1511
+ return RustCoreBatchDiffsAttempt(fallback_reason="disabled")
1512
+ if config.diagnostics:
1513
+ return RustCoreBatchDiffsAttempt(
1514
+ fallback_reason="diagnostics require Python pipeline"
1515
+ )
1516
+ if not files:
1517
+ return RustCoreBatchDiffsAttempt()
1518
+
1519
+ adapter_phase_timings: list[dict[str, Any]] = []
1520
+
1521
+ def _record_adapter_phase(
1522
+ name: str, started: float, *, summary_exclude: bool = False
1523
+ ) -> None:
1524
+ phase = {
1525
+ "name": name,
1526
+ "duration_ms": round((time.perf_counter() - started) * 1000, 3),
1527
+ "shared": True,
1528
+ }
1529
+ if summary_exclude:
1530
+ phase["summary_exclude"] = True
1531
+ adapter_phase_timings.append(phase)
1532
+
1533
+ try:
1534
+ backend = _load_backend()
1535
+ except Exception as exc: # noqa: BLE001 - optional accelerator must never break diffing.
1536
+ logger.debug("Rust core unavailable: %s", exc)
1537
+ return RustCoreBatchDiffsAttempt(fallback_reason=f"unavailable: {exc}")
1538
+
1539
+ try:
1540
+ supports = getattr(backend, "supports_language", None)
1541
+ if callable(supports) and not supports("python"):
1542
+ return RustCoreBatchDiffsAttempt(
1543
+ fallback_reason="backend does not support python"
1544
+ )
1545
+ backend_version = str(getattr(backend, "version", lambda: "")())
1546
+ diff_batch = getattr(backend, "diff_batch", None)
1547
+ if not callable(diff_batch):
1548
+ return RustCoreBatchDiffsAttempt(
1549
+ fallback_reason="backend does not expose diff_batch",
1550
+ backend_version=backend_version,
1551
+ )
1552
+ phase_started = time.perf_counter()
1553
+ normalized_files = _canonicalize_file_plugin_ids(files)
1554
+ request = {
1555
+ "schema_version": 1,
1556
+ "mode": "commit_batch",
1557
+ "python_parser_backend": python_parser_backend,
1558
+ "config": config.model_dump(mode="json"),
1559
+ "parallel": parallel,
1560
+ "max_workers": max_workers,
1561
+ "files": normalized_files,
1562
+ }
1563
+ _record_adapter_phase("rust_core_request_construction", phase_started)
1564
+ phase_started = time.perf_counter()
1565
+ request_json = json.dumps(request)
1566
+ _record_adapter_phase("rust_core_request_json_encode", phase_started)
1567
+ phase_started = time.perf_counter()
1568
+ raw_payload = diff_batch(request_json)
1569
+ _record_adapter_phase("rust_core_batch_execution", phase_started)
1570
+ phase_started = time.perf_counter()
1571
+ data = _load_tree_payload(raw_payload)
1572
+ _record_adapter_phase("rust_core_response_json_decode", phase_started)
1573
+ phase_started = time.perf_counter()
1574
+ engine = str(data.get("engine") or "")
1575
+ if engine != _BATCH_ENGINE:
1576
+ _record_adapter_phase("rust_core_response_validation", phase_started)
1577
+ return RustCoreBatchDiffsAttempt(
1578
+ fallback_reason=f"unexpected engine: {engine or '<missing>'}",
1579
+ adapter_phase_timings=adapter_phase_timings,
1580
+ backend_version=backend_version,
1581
+ )
1582
+ diff_items = data.get("diffs")
1583
+ if not isinstance(diff_items, list):
1584
+ reason = str(data.get("reason") or "batch response did not include a diff")
1585
+ _record_adapter_phase("rust_core_response_validation", phase_started)
1586
+ return RustCoreBatchDiffsAttempt(
1587
+ fallback_reason=reason,
1588
+ adapter_phase_timings=adapter_phase_timings,
1589
+ backend_version=backend_version,
1590
+ )
1591
+ batch_metadata = dict(data.get("metadata") or {})
1592
+ fallback_reasons: dict[int, str] = {}
1593
+ validated_diffs: list[tuple[int, str, SemanticDiff]] = []
1594
+ for fallback_index, item in enumerate(diff_items):
1595
+ if not isinstance(item, dict):
1596
+ fallback_reasons[fallback_index] = "batch diff item must be an object"
1597
+ continue
1598
+ index = int(item.get("index") or fallback_index)
1599
+ status = str(item.get("status") or "")
1600
+ if status != _COMPLETE_STATUS or "diff" not in item:
1601
+ fallback_reasons[index] = str(
1602
+ item.get("reason") or status or "batch item is incomplete"
1603
+ )
1604
+ continue
1605
+ diff = _normalize_builtin_change_type_enums(
1606
+ SemanticDiff.model_validate(item["diff"])
1607
+ )
1608
+ validated_diffs.append((index, status, diff))
1609
+ _record_adapter_phase("rust_core_response_validation", phase_started)
1610
+ phase_started = time.perf_counter()
1611
+ diffs: dict[int, SemanticDiff] = {}
1612
+ for index, status, diff in validated_diffs:
1613
+ metadata = dict(diff.metadata)
1614
+ rust_metadata = dict(metadata.get("rust_core") or {})
1615
+ rust_metadata.update(
1616
+ {
1617
+ "status": status,
1618
+ "used": True,
1619
+ "backend_version": backend_version,
1620
+ "engine": _BATCH_ENGINE,
1621
+ "stage": "batch_final_diff",
1622
+ "boundary": "source_batch_to_final_diff",
1623
+ "cache_policy": "python_diff_cache_skipped",
1624
+ }
1625
+ )
1626
+ metadata["rust_core"] = rust_metadata
1627
+ if batch_metadata:
1628
+ metadata["rust_core_batch"] = batch_metadata
1629
+ diffs[index] = _copy_diff_with_metadata(
1630
+ diff,
1631
+ _stamp_certified_rust_diff(metadata),
1632
+ )
1633
+ _record_adapter_phase("rust_core_result_assembly", phase_started)
1634
+ if adapter_phase_timings:
1635
+ batch_metadata = {
1636
+ **batch_metadata,
1637
+ "adapter_phase_timings": adapter_phase_timings,
1638
+ }
1639
+ for index, diff in list(diffs.items()):
1640
+ metadata = dict(diff.metadata)
1641
+ metadata["rust_core_batch"] = batch_metadata
1642
+ diffs[index] = _copy_diff_with_metadata(
1643
+ diff,
1644
+ _stamp_certified_rust_diff(metadata),
1645
+ )
1646
+ return RustCoreBatchDiffsAttempt(
1647
+ diffs=diffs,
1648
+ fallback_reasons=fallback_reasons,
1649
+ batch_metadata=batch_metadata,
1650
+ adapter_phase_timings=adapter_phase_timings,
1651
+ backend_version=backend_version,
1652
+ )
1653
+ except (ValidationError, ValueError, TypeError) as exc:
1654
+ logger.debug("Rust core batch output rejected: %s", exc)
1655
+ return RustCoreBatchDiffsAttempt(
1656
+ fallback_reason=f"invalid output: {exc}",
1657
+ adapter_phase_timings=adapter_phase_timings,
1658
+ )
1659
+ except Exception as exc: # noqa: BLE001 - optional accelerator must fall back.
1660
+ logger.debug("Rust core batch path failed: %s", exc, exc_info=True)
1661
+ return RustCoreBatchDiffsAttempt(
1662
+ fallback_reason=f"failed: {exc}",
1663
+ adapter_phase_timings=adapter_phase_timings,
1664
+ )
1665
+
1666
+
1667
+ def try_rust_core_commit_json(
1668
+ *,
1669
+ files: list[dict[str, Any]],
1670
+ old_ref: str,
1671
+ new_ref: str,
1672
+ config: DiffConfig,
1673
+ parallel: bool = False,
1674
+ max_workers: int | None = None,
1675
+ python_parser_backend: str = "native",
1676
+ ) -> RustCoreCommitJsonAttempt:
1677
+ """Return certified Rust CommitDiff JSON bytes without Pydantic hot-path validation."""
1678
+ if not rust_core_enabled(config):
1679
+ return RustCoreCommitJsonAttempt(fallback_reason="disabled")
1680
+ if config.diagnostics:
1681
+ return RustCoreCommitJsonAttempt(
1682
+ fallback_reason="diagnostics require Python pipeline"
1683
+ )
1684
+ if python_parser_backend != "native":
1685
+ return RustCoreCommitJsonAttempt(
1686
+ fallback_reason="certified commit JSON requires native Python backend"
1687
+ )
1688
+ if not files:
1689
+ return RustCoreCommitJsonAttempt()
1690
+
1691
+ adapter_phase_timings: list[dict[str, Any]] = []
1692
+
1693
+ def _record_adapter_phase(
1694
+ name: str, started: float, *, summary_exclude: bool = False
1695
+ ) -> None:
1696
+ phase = {
1697
+ "name": name,
1698
+ "duration_ms": round((time.perf_counter() - started) * 1000, 3),
1699
+ "shared": True,
1700
+ }
1701
+ if summary_exclude:
1702
+ phase["summary_exclude"] = True
1703
+ adapter_phase_timings.append(phase)
1704
+
1705
+ try:
1706
+ backend = _load_backend()
1707
+ except Exception as exc: # noqa: BLE001 - optional accelerator must never break diffing.
1708
+ logger.debug("Rust core unavailable: %s", exc)
1709
+ return RustCoreCommitJsonAttempt(fallback_reason=f"unavailable: {exc}")
1710
+
1711
+ try:
1712
+ supports = getattr(backend, "supports_language", None)
1713
+ if callable(supports) and not supports("python"):
1714
+ return RustCoreCommitJsonAttempt(
1715
+ fallback_reason="backend does not support python"
1716
+ )
1717
+ backend_version = str(getattr(backend, "version", lambda: "")())
1718
+ diff_batch_commit_json = getattr(backend, "diff_batch_commit_json", None)
1719
+ if not callable(diff_batch_commit_json):
1720
+ return RustCoreCommitJsonAttempt(
1721
+ fallback_reason="backend does not expose diff_batch_commit_json",
1722
+ backend_version=backend_version,
1723
+ )
1724
+ phase_started = time.perf_counter()
1725
+ normalized_files = _canonicalize_file_plugin_ids(files)
1726
+ request = {
1727
+ "schema_version": 1,
1728
+ "mode": "commit_json",
1729
+ "old_ref": old_ref,
1730
+ "new_ref": new_ref,
1731
+ "python_parser_backend": python_parser_backend,
1732
+ "config": config.model_dump(mode="json"),
1733
+ "parallel": parallel,
1734
+ "max_workers": max_workers,
1735
+ "files": normalized_files,
1736
+ }
1737
+ _record_adapter_phase("rust_core_commit_request_construction", phase_started)
1738
+ phase_started = time.perf_counter()
1739
+ request_json = json.dumps(request)
1740
+ _record_adapter_phase("rust_core_commit_request_json_encode", phase_started)
1741
+ phase_started = time.perf_counter()
1742
+ control_json, commit_payload = diff_batch_commit_json(request_json)
1743
+ _record_adapter_phase(
1744
+ "rust_core_commit_pyo3_call",
1745
+ phase_started,
1746
+ summary_exclude=True,
1747
+ )
1748
+ phase_started = time.perf_counter()
1749
+ control = json.loads(control_json)
1750
+ if not isinstance(control, dict):
1751
+ raise ValueError("certified commit control must be a JSON object")
1752
+ _record_adapter_phase("rust_core_commit_control_decode", phase_started)
1753
+ phase_started = time.perf_counter()
1754
+ status = str(control.get("status") or "")
1755
+ certification = str(control.get("certification") or "")
1756
+ if status != _COMPLETE_STATUS:
1757
+ reason = str(control.get("reason") or status or "commit JSON incomplete")
1758
+ _record_adapter_phase("rust_core_commit_control_validation", phase_started)
1759
+ return RustCoreCommitJsonAttempt(
1760
+ control=control,
1761
+ adapter_phase_timings=adapter_phase_timings,
1762
+ backend_version=backend_version,
1763
+ fallback_reason=reason,
1764
+ )
1765
+ if certification != _PYTHON_NATIVE_CERTIFICATION:
1766
+ _record_adapter_phase("rust_core_commit_control_validation", phase_started)
1767
+ return RustCoreCommitJsonAttempt(
1768
+ control=control,
1769
+ adapter_phase_timings=adapter_phase_timings,
1770
+ backend_version=backend_version,
1771
+ fallback_reason=(
1772
+ f"unexpected certification: {certification or '<missing>'}"
1773
+ ),
1774
+ )
1775
+ if commit_payload is None:
1776
+ _record_adapter_phase("rust_core_commit_control_validation", phase_started)
1777
+ return RustCoreCommitJsonAttempt(
1778
+ control=control,
1779
+ adapter_phase_timings=adapter_phase_timings,
1780
+ backend_version=backend_version,
1781
+ fallback_reason="commit JSON bytes missing",
1782
+ )
1783
+ commit_json = bytes(commit_payload)
1784
+ if not commit_json:
1785
+ _record_adapter_phase("rust_core_commit_control_validation", phase_started)
1786
+ return RustCoreCommitJsonAttempt(
1787
+ control=control,
1788
+ adapter_phase_timings=adapter_phase_timings,
1789
+ backend_version=backend_version,
1790
+ fallback_reason="commit JSON bytes empty",
1791
+ )
1792
+ declared_size = int(control.get("commit_diff_byte_size") or 0)
1793
+ if declared_size != len(commit_json):
1794
+ _record_adapter_phase("rust_core_commit_control_validation", phase_started)
1795
+ return RustCoreCommitJsonAttempt(
1796
+ control=control,
1797
+ adapter_phase_timings=adapter_phase_timings,
1798
+ backend_version=backend_version,
1799
+ fallback_reason=(
1800
+ "commit JSON byte size mismatch: "
1801
+ f"control={declared_size}, payload={len(commit_json)}"
1802
+ ),
1803
+ )
1804
+ _record_adapter_phase("rust_core_commit_control_validation", phase_started)
1805
+ if os.getenv("INTENTUMDIFF_RUST_CORE_VALIDATE_JSON", "").strip().lower() in {
1806
+ "1",
1807
+ "true",
1808
+ "yes",
1809
+ "on",
1810
+ }:
1811
+ phase_started = time.perf_counter()
1812
+ from intentumdiff.core.models import CommitDiff
1813
+
1814
+ CommitDiff.model_validate_json(commit_json)
1815
+ _record_adapter_phase("rust_core_commit_debug_pydantic_validation", phase_started)
1816
+ return RustCoreCommitJsonAttempt(
1817
+ control=control,
1818
+ commit_diff_json=commit_json,
1819
+ adapter_phase_timings=adapter_phase_timings,
1820
+ backend_version=backend_version,
1821
+ )
1822
+ except (ValidationError, ValueError, TypeError) as exc:
1823
+ logger.debug("Rust core certified commit JSON rejected: %s", exc)
1824
+ return RustCoreCommitJsonAttempt(
1825
+ fallback_reason=f"invalid output: {exc}",
1826
+ adapter_phase_timings=adapter_phase_timings,
1827
+ )
1828
+ except Exception as exc: # noqa: BLE001 - optional accelerator must fall back.
1829
+ logger.debug("Rust core certified commit JSON failed: %s", exc, exc_info=True)
1830
+ return RustCoreCommitJsonAttempt(
1831
+ fallback_reason=f"failed: {exc}",
1832
+ adapter_phase_timings=adapter_phase_timings,
1833
+ )
1834
+
1835
+
1836
+ def try_rust_core_working_tree_commit_json(
1837
+ *,
1838
+ repo_path: str | Path,
1839
+ old_ref: str,
1840
+ config: DiffConfig,
1841
+ parallel: bool = False,
1842
+ max_workers: int | None = None,
1843
+ python_parser_backend: str = "native",
1844
+ ) -> RustCoreCommitJsonAttempt:
1845
+ """Return Rust-certified CommitDiff JSON with Rust-side source collection."""
1846
+ if not rust_core_enabled(config):
1847
+ return RustCoreCommitJsonAttempt(fallback_reason="disabled")
1848
+ if config.diagnostics:
1849
+ return RustCoreCommitJsonAttempt(
1850
+ fallback_reason="diagnostics require Python pipeline"
1851
+ )
1852
+
1853
+ adapter_phase_timings: list[dict[str, Any]] = []
1854
+
1855
+ def _record_adapter_phase(
1856
+ name: str, started: float, *, summary_exclude: bool = False
1857
+ ) -> None:
1858
+ phase = {
1859
+ "name": name,
1860
+ "duration_ms": round((time.perf_counter() - started) * 1000, 3),
1861
+ "shared": True,
1862
+ }
1863
+ if summary_exclude:
1864
+ phase["summary_exclude"] = True
1865
+ adapter_phase_timings.append(phase)
1866
+
1867
+ try:
1868
+ backend = _load_backend()
1869
+ except Exception as exc: # noqa: BLE001 - optional accelerator must fall back.
1870
+ logger.debug("Rust core unavailable: %s", exc)
1871
+ return RustCoreCommitJsonAttempt(fallback_reason=f"unavailable: {exc}")
1872
+
1873
+ try:
1874
+ backend_version = str(getattr(backend, "version", lambda: "")())
1875
+ entrypoint = getattr(backend, "diff_working_tree_python_commit_json", None)
1876
+ if not callable(entrypoint):
1877
+ return RustCoreCommitJsonAttempt(
1878
+ fallback_reason="backend does not expose diff_working_tree_python_commit_json",
1879
+ backend_version=backend_version,
1880
+ )
1881
+
1882
+ phase_started = time.perf_counter()
1883
+ request = {
1884
+ "schema_version": 1,
1885
+ "mode": "working_tree_commit_json",
1886
+ "repo_path": str(repo_path),
1887
+ "old_ref": old_ref,
1888
+ "new_ref": "",
1889
+ "python_parser_backend": python_parser_backend,
1890
+ "config": config.model_dump(mode="json"),
1891
+ "parallel": parallel,
1892
+ "max_workers": max_workers,
1893
+ }
1894
+ _record_adapter_phase("rust_core_working_tree_request_construction", phase_started)
1895
+ phase_started = time.perf_counter()
1896
+ request_json = json.dumps(request)
1897
+ _record_adapter_phase("rust_core_working_tree_request_json_encode", phase_started)
1898
+ phase_started = time.perf_counter()
1899
+ control_json, commit_payload = entrypoint(request_json)
1900
+ _record_adapter_phase(
1901
+ "rust_core_working_tree_pyo3_call",
1902
+ phase_started,
1903
+ summary_exclude=True,
1904
+ )
1905
+ phase_started = time.perf_counter()
1906
+ control = json.loads(control_json)
1907
+ if not isinstance(control, dict):
1908
+ raise ValueError("certified working-tree control must be a JSON object")
1909
+ _record_adapter_phase("rust_core_working_tree_control_decode", phase_started)
1910
+
1911
+ phase_started = time.perf_counter()
1912
+ status = str(control.get("status") or "")
1913
+ certification = str(control.get("certification") or "")
1914
+ if status != _COMPLETE_STATUS:
1915
+ reason = str(control.get("reason") or status or "working-tree JSON incomplete")
1916
+ _record_adapter_phase("rust_core_working_tree_control_validation", phase_started)
1917
+ return RustCoreCommitJsonAttempt(
1918
+ control=control,
1919
+ adapter_phase_timings=adapter_phase_timings,
1920
+ backend_version=backend_version,
1921
+ fallback_reason=reason,
1922
+ )
1923
+ if certification != _PYTHON_NATIVE_CERTIFICATION:
1924
+ _record_adapter_phase("rust_core_working_tree_control_validation", phase_started)
1925
+ return RustCoreCommitJsonAttempt(
1926
+ control=control,
1927
+ adapter_phase_timings=adapter_phase_timings,
1928
+ backend_version=backend_version,
1929
+ fallback_reason=f"unexpected certification: {certification or '<missing>'}",
1930
+ )
1931
+ if commit_payload is None:
1932
+ _record_adapter_phase("rust_core_working_tree_control_validation", phase_started)
1933
+ return RustCoreCommitJsonAttempt(
1934
+ control=control,
1935
+ adapter_phase_timings=adapter_phase_timings,
1936
+ backend_version=backend_version,
1937
+ fallback_reason="commit JSON bytes missing",
1938
+ )
1939
+ commit_json = bytes(commit_payload)
1940
+ declared_size = int(control.get("commit_diff_byte_size") or 0)
1941
+ if declared_size != len(commit_json):
1942
+ _record_adapter_phase("rust_core_working_tree_control_validation", phase_started)
1943
+ return RustCoreCommitJsonAttempt(
1944
+ control=control,
1945
+ adapter_phase_timings=adapter_phase_timings,
1946
+ backend_version=backend_version,
1947
+ fallback_reason=(
1948
+ "commit JSON byte size mismatch: "
1949
+ f"control={declared_size}, payload={len(commit_json)}"
1950
+ ),
1951
+ )
1952
+ _record_adapter_phase("rust_core_working_tree_control_validation", phase_started)
1953
+ return RustCoreCommitJsonAttempt(
1954
+ control=control,
1955
+ commit_diff_json=commit_json,
1956
+ adapter_phase_timings=adapter_phase_timings,
1957
+ backend_version=backend_version,
1958
+ )
1959
+ except (ValidationError, ValueError, TypeError) as exc:
1960
+ logger.debug("Rust core working-tree commit JSON rejected: %s", exc)
1961
+ return RustCoreCommitJsonAttempt(
1962
+ fallback_reason=f"invalid output: {exc}",
1963
+ adapter_phase_timings=adapter_phase_timings,
1964
+ )
1965
+ except Exception as exc: # noqa: BLE001 - optional accelerator must fall back.
1966
+ logger.debug("Rust core working-tree commit JSON failed: %s", exc, exc_info=True)
1967
+ return RustCoreCommitJsonAttempt(
1968
+ fallback_reason=f"failed: {exc}",
1969
+ adapter_phase_timings=adapter_phase_timings,
1970
+ )
1971
+
1972
+
1973
+ def run_rust_core_candidate_batch(
1974
+ *,
1975
+ files: list[dict[str, Any]],
1976
+ config: DiffConfig,
1977
+ python_parser_backend: str = "wasm",
1978
+ ) -> RustCoreCandidateBatchAttempt:
1979
+ """Run the internal V4-A candidate lane without making it product-visible."""
1980
+ if not rust_core_enabled(config):
1981
+ return RustCoreCandidateBatchAttempt(fallback_reason="disabled")
1982
+
1983
+ try:
1984
+ backend = _load_backend()
1985
+ except Exception as exc: # noqa: BLE001 - optional benchmark lane records failures.
1986
+ logger.debug("Rust core unavailable for candidate batch: %s", exc)
1987
+ return RustCoreCandidateBatchAttempt(fallback_reason=f"unavailable: {exc}")
1988
+
1989
+ try:
1990
+ backend_version = str(getattr(backend, "version", lambda: "")())
1991
+ diff_batch = getattr(backend, "diff_batch", None)
1992
+ if not callable(diff_batch):
1993
+ return RustCoreCandidateBatchAttempt(
1994
+ fallback_reason="backend does not expose diff_batch",
1995
+ backend_version=backend_version,
1996
+ )
1997
+ request = {
1998
+ "schema_version": 1,
1999
+ "mode": "candidate_batch",
2000
+ "candidate": True,
2001
+ "python_parser_backend": python_parser_backend,
2002
+ "config": config.model_dump(mode="json"),
2003
+ "files": _canonicalize_file_plugin_ids(files),
2004
+ }
2005
+ data = _load_tree_payload(diff_batch(json.dumps(request)))
2006
+ engine = str(data.get("engine") or "")
2007
+ if engine != _BATCH_ENGINE:
2008
+ return RustCoreCandidateBatchAttempt(
2009
+ fallback_reason=f"unexpected engine: {engine or '<missing>'}",
2010
+ backend_version=backend_version,
2011
+ )
2012
+ for item in data.get("diffs") or []:
2013
+ if not isinstance(item, dict):
2014
+ continue
2015
+ candidate_diff = item.get("candidate_diff")
2016
+ if candidate_diff is not None:
2017
+ SemanticDiff.model_validate(candidate_diff)
2018
+ return RustCoreCandidateBatchAttempt(
2019
+ payload=data if isinstance(data, dict) else None,
2020
+ backend_version=backend_version,
2021
+ )
2022
+ except (ValidationError, ValueError, TypeError) as exc:
2023
+ logger.debug("Rust core candidate output rejected: %s", exc)
2024
+ return RustCoreCandidateBatchAttempt(
2025
+ fallback_reason=f"invalid output: {exc}",
2026
+ backend_version=locals().get("backend_version", ""),
2027
+ )
2028
+ except Exception as exc: # noqa: BLE001 - benchmark lane must report failures.
2029
+ logger.debug("Rust core candidate batch failed: %s", exc, exc_info=True)
2030
+ return RustCoreCandidateBatchAttempt(
2031
+ fallback_reason=f"failed: {exc}",
2032
+ backend_version=locals().get("backend_version", ""),
2033
+ )
2034
+
2035
+
2036
+ def try_rust_core_cst_diff(
2037
+ *,
2038
+ old_filtered_cst_json: str,
2039
+ new_filtered_cst_json: str,
2040
+ old_raw_source: str,
2041
+ new_raw_source: str,
2042
+ old_filename: str,
2043
+ new_filename: str,
2044
+ parser_wasm_path: str,
2045
+ language: str,
2046
+ parser_plugin_id: str | None,
2047
+ config: DiffConfig,
2048
+ ) -> RustCoreAttempt:
2049
+ """Try the Rust core after Python has produced filtered CST JSON."""
2050
+ if not rust_core_enabled(config):
2051
+ return RustCoreAttempt(fallback_reason="disabled")
2052
+ if language != "python":
2053
+ return RustCoreAttempt(fallback_reason="unsupported language")
2054
+ parser_plugin_id = _canonical_python_plugin_id(parser_plugin_id)
2055
+ if parser_plugin_id is not None and parser_plugin_id not in _PYTHON_PLUGIN_IDS:
2056
+ return RustCoreAttempt(fallback_reason="unsupported parser plugin")
2057
+ if config.diagnostics:
2058
+ return RustCoreAttempt(fallback_reason="diagnostics require Python pipeline")
2059
+
2060
+ try:
2061
+ backend = _load_backend()
2062
+ except Exception as exc: # noqa: BLE001 - optional accelerator must never break diffing.
2063
+ logger.debug("Rust core unavailable: %s", exc)
2064
+ return RustCoreAttempt(fallback_reason=f"unavailable: {exc}")
2065
+
2066
+ try:
2067
+ supports = getattr(backend, "supports_language", None)
2068
+ if callable(supports) and not supports("python"):
2069
+ return RustCoreAttempt(fallback_reason="backend does not support python")
2070
+ backend_version = str(getattr(backend, "version", lambda: "")())
2071
+ diff_fn = getattr(backend, "diff_python_cst_json", None)
2072
+ if not callable(diff_fn):
2073
+ return RustCoreAttempt(
2074
+ fallback_reason="backend does not expose diff_python_cst_json",
2075
+ backend_version=backend_version,
2076
+ )
2077
+ payload = diff_fn(
2078
+ old_filtered_cst_json,
2079
+ new_filtered_cst_json,
2080
+ old_raw_source,
2081
+ new_raw_source,
2082
+ old_filename,
2083
+ new_filename,
2084
+ parser_wasm_path,
2085
+ config.model_dump_json(),
2086
+ )
2087
+ diff = SemanticDiff.model_validate_json(payload)
2088
+ metadata = dict(diff.metadata)
2089
+ rust_metadata = dict(metadata.get("rust_core") or {})
2090
+ status = str(rust_metadata.get("status") or "")
2091
+ if status != _COMPLETE_STATUS:
2092
+ return RustCoreAttempt(
2093
+ fallback_reason=f"incomplete result status: {status or '<missing>'}",
2094
+ backend_version=backend_version,
2095
+ )
2096
+ rust_metadata.update(
2097
+ {
2098
+ "used": True,
2099
+ "backend_version": backend_version,
2100
+ }
2101
+ )
2102
+ metadata["rust_core"] = rust_metadata
2103
+ diff = diff.model_copy(update={"metadata": metadata})
2104
+ return RustCoreAttempt(diff=diff, backend_version=backend_version)
2105
+ except (ValidationError, ValueError, TypeError) as exc:
2106
+ logger.debug("Rust core CST output rejected: %s", exc)
2107
+ return RustCoreAttempt(fallback_reason=f"invalid output: {exc}")
2108
+ except Exception as exc: # noqa: BLE001 - optional accelerator must fall back.
2109
+ logger.debug("Rust core CST path failed: %s", exc, exc_info=True)
2110
+ return RustCoreAttempt(fallback_reason=f"failed: {exc}")
2111
+
2112
+
2113
+ def try_rust_core_tree_diff(
2114
+ *,
2115
+ old_tree: SemanticNode,
2116
+ new_tree: SemanticNode,
2117
+ old_filename: str,
2118
+ new_filename: str,
2119
+ language: str,
2120
+ config: DiffConfig,
2121
+ ) -> RustCoreTreeAttempt:
2122
+ """Try Rust stage 9-11 work after Python has built validated semantic trees.
2123
+
2124
+ Since migration step 2 (see ``docs/ENGINE_BOUNDARY_AUDIT.md``) the Rust
2125
+ matching/edit-script pipeline is language-agnostic — it operates on the
2126
+ ``SemanticNode`` shape, not on Python-specific AST facts. Any language
2127
+ label is accepted; the Python-only guard was a certification gate, not
2128
+ an algorithm limitation.
2129
+ """
2130
+ if not rust_core_enabled(config):
2131
+ return RustCoreTreeAttempt(fallback_reason="disabled")
2132
+ if config.diagnostics:
2133
+ return RustCoreTreeAttempt(fallback_reason="diagnostics require Python pipeline")
2134
+
2135
+ try:
2136
+ backend = _load_backend()
2137
+ except Exception as exc: # noqa: BLE001 - optional accelerator must never break diffing.
2138
+ logger.debug("Rust core unavailable: %s", exc)
2139
+ return RustCoreTreeAttempt(fallback_reason=f"unavailable: {exc}")
2140
+
2141
+ try:
2142
+ backend_version = str(getattr(backend, "version", lambda: "")())
2143
+ # Prefer the language-agnostic entrypoint added in migration step 2.
2144
+ # Fall back to the legacy Python-only entrypoint for older installs.
2145
+ diff_fn = getattr(backend, "diff_semantic_tree_json", None) or getattr(
2146
+ backend, "diff_python_semantic_tree_json", None
2147
+ )
2148
+ if not callable(diff_fn):
2149
+ return RustCoreTreeAttempt(
2150
+ fallback_reason="backend exposes no semantic-tree diff entrypoint",
2151
+ backend_version=backend_version,
2152
+ )
2153
+ # Only the legacy Python-only entrypoint requires the language guard.
2154
+ if (
2155
+ diff_fn.__name__ == "diff_python_semantic_tree_json"
2156
+ and language != "python"
2157
+ ):
2158
+ return RustCoreTreeAttempt(
2159
+ fallback_reason=(
2160
+ "installed backend only exposes the Python-only entrypoint; "
2161
+ "rebuild the Rust core to diff non-Python languages"
2162
+ ),
2163
+ backend_version=backend_version,
2164
+ )
2165
+ payload = diff_fn(
2166
+ old_tree.model_dump_json(),
2167
+ new_tree.model_dump_json(),
2168
+ old_filename,
2169
+ new_filename,
2170
+ language,
2171
+ config.model_dump_json(),
2172
+ )
2173
+ data = _load_tree_payload(payload)
2174
+ status = str(data.get("status") or "")
2175
+ if status != _COMPLETE_STATUS:
2176
+ return RustCoreTreeAttempt(
2177
+ fallback_reason=f"incomplete result status: {status or '<missing>'}",
2178
+ backend_version=backend_version,
2179
+ )
2180
+ engine = str(data.get("engine") or "")
2181
+ if engine not in {_TREE_ENGINE, _TREE_ENGINE_V3}:
2182
+ return RustCoreTreeAttempt(
2183
+ fallback_reason=f"unexpected engine: {engine or '<missing>'}",
2184
+ backend_version=backend_version,
2185
+ )
2186
+ old_lookup = _semantic_node_lookup(old_tree)
2187
+ new_lookup = _semantic_node_lookup(new_tree)
2188
+ matching = _matching_from_payload(data.get("matching_pairs"), old_lookup, new_lookup)
2189
+ changes = _changes_from_payload(data.get("changes"), old_lookup, new_lookup)
2190
+ metadata = dict(data.get("metadata") or {})
2191
+ metadata["rust_core"] = {
2192
+ "status": status,
2193
+ "used": True,
2194
+ "backend_version": backend_version,
2195
+ "engine": engine,
2196
+ "stage": "semantic_tree_stage9_11",
2197
+ }
2198
+ return RustCoreTreeAttempt(
2199
+ changes=changes,
2200
+ matching=matching,
2201
+ metadata=metadata,
2202
+ backend_version=backend_version,
2203
+ )
2204
+ except (ValidationError, ValueError, TypeError) as exc:
2205
+ logger.debug("Rust core semantic-tree output rejected: %s", exc)
2206
+ return RustCoreTreeAttempt(fallback_reason=f"invalid output: {exc}")
2207
+ except Exception as exc: # noqa: BLE001 - optional accelerator must fall back.
2208
+ logger.debug("Rust core semantic-tree path failed: %s", exc, exc_info=True)
2209
+ return RustCoreTreeAttempt(fallback_reason=f"failed: {exc}")
2210
+
2211
+
2212
+ def try_rust_core_sources_stage11(
2213
+ *,
2214
+ old_source: str,
2215
+ new_source: str,
2216
+ old_filename: str,
2217
+ new_filename: str,
2218
+ parser_wasm_path: str,
2219
+ language: str,
2220
+ parser_plugin_id: str | None,
2221
+ config: DiffConfig,
2222
+ ) -> RustCoreSourceAttempt:
2223
+ """Try Rust V3 from raw Python source through initial Stage 11 output."""
2224
+ if not rust_core_enabled(config):
2225
+ return RustCoreSourceAttempt(fallback_reason="disabled")
2226
+ if language != "python":
2227
+ return RustCoreSourceAttempt(fallback_reason="unsupported language")
2228
+ parser_plugin_id = _canonical_python_plugin_id(parser_plugin_id)
2229
+ if parser_plugin_id is not None and parser_plugin_id not in _PYTHON_PLUGIN_IDS:
2230
+ return RustCoreSourceAttempt(fallback_reason="unsupported parser plugin")
2231
+ if config.diagnostics:
2232
+ return RustCoreSourceAttempt(fallback_reason="diagnostics require Python pipeline")
2233
+
2234
+ try:
2235
+ backend = _load_backend()
2236
+ except Exception as exc: # noqa: BLE001 - optional accelerator must never break diffing.
2237
+ logger.debug("Rust core unavailable: %s", exc)
2238
+ return RustCoreSourceAttempt(fallback_reason=f"unavailable: {exc}")
2239
+
2240
+ try:
2241
+ supports = getattr(backend, "supports_language", None)
2242
+ if callable(supports) and not supports("python"):
2243
+ return RustCoreSourceAttempt(fallback_reason="backend does not support python")
2244
+ backend_version = str(getattr(backend, "version", lambda: "")())
2245
+ diff_fn = getattr(backend, "diff_python_sources_stage11_json", None)
2246
+ if not callable(diff_fn):
2247
+ return RustCoreSourceAttempt(
2248
+ fallback_reason="backend does not expose diff_python_sources_stage11_json",
2249
+ backend_version=backend_version,
2250
+ )
2251
+ payload = diff_fn(
2252
+ old_source,
2253
+ new_source,
2254
+ old_filename,
2255
+ new_filename,
2256
+ parser_wasm_path,
2257
+ config.model_dump_json(),
2258
+ )
2259
+ data = _load_tree_payload(payload)
2260
+ status = str(data.get("status") or "")
2261
+ if status != _COMPLETE_STATUS:
2262
+ reason = str(data.get("reason") or status or "<missing>")
2263
+ return RustCoreSourceAttempt(
2264
+ fallback_reason=f"incomplete result status: {reason}",
2265
+ backend_version=backend_version,
2266
+ )
2267
+ engine = str(data.get("engine") or "")
2268
+ if engine != _SOURCE_ENGINE:
2269
+ return RustCoreSourceAttempt(
2270
+ fallback_reason=f"unexpected engine: {engine or '<missing>'}",
2271
+ backend_version=backend_version,
2272
+ )
2273
+ old_tree = SemanticNode.model_validate(data.get("old_tree"))
2274
+ new_tree = SemanticNode.model_validate(data.get("new_tree"))
2275
+ old_lookup = _semantic_node_lookup(old_tree)
2276
+ new_lookup = _semantic_node_lookup(new_tree)
2277
+ matching = _matching_from_payload(data.get("matching_pairs"), old_lookup, new_lookup)
2278
+ changes = _changes_from_payload(data.get("changes"), old_lookup, new_lookup)
2279
+ adaptive_fuel = int(data.get("adaptive_fuel") or config.plugin_fuel)
2280
+ metadata = dict(data.get("metadata") or {})
2281
+ rust_phase_timings = metadata.pop("phase_timings", None)
2282
+ if rust_phase_timings is not None:
2283
+ metadata["rust_phase_timings"] = rust_phase_timings
2284
+ metadata["rust_core"] = {
2285
+ "status": status,
2286
+ "used": True,
2287
+ "backend_version": backend_version,
2288
+ "engine": engine,
2289
+ "stage": "sources_to_stage11",
2290
+ "wasm_boundary": metadata.get("wasm_boundary", "rust_wasmtime"),
2291
+ }
2292
+ return RustCoreSourceAttempt(
2293
+ old_tree=old_tree,
2294
+ new_tree=new_tree,
2295
+ changes=changes,
2296
+ matching=matching,
2297
+ adaptive_fuel=adaptive_fuel,
2298
+ metadata=metadata,
2299
+ backend_version=backend_version,
2300
+ )
2301
+ except (ValidationError, ValueError, TypeError) as exc:
2302
+ logger.debug("Rust core source-stage output rejected: %s", exc)
2303
+ return RustCoreSourceAttempt(fallback_reason=f"invalid output: {exc}")
2304
+ except Exception as exc: # noqa: BLE001 - optional accelerator must fall back.
2305
+ logger.debug("Rust core source-stage path failed: %s", exc, exc_info=True)
2306
+ return RustCoreSourceAttempt(fallback_reason=f"failed: {exc}")
2307
+
2308
+
2309
+ def _load_tree_payload(payload: Any) -> dict[str, Any]:
2310
+ if isinstance(payload, str):
2311
+ import json
2312
+
2313
+ data = json.loads(payload)
2314
+ else:
2315
+ data = payload
2316
+ if not isinstance(data, dict):
2317
+ raise ValueError("Rust core tree payload must be a JSON object")
2318
+ return data
2319
+
2320
+
2321
+ def _semantic_node_lookup(root: SemanticNode) -> dict[str, SemanticNode]:
2322
+ nodes = [root, *root.descendants()]
2323
+ return {node.id: node for node in nodes}
2324
+
2325
+
2326
+ def _matching_from_payload(
2327
+ raw_pairs: Any,
2328
+ old_lookup: dict[str, SemanticNode],
2329
+ new_lookup: dict[str, SemanticNode],
2330
+ ) -> Matching:
2331
+ if not isinstance(raw_pairs, list):
2332
+ raise ValueError("matching_pairs must be a list")
2333
+ matching: Matching = []
2334
+ for raw_pair in raw_pairs:
2335
+ if not isinstance(raw_pair, dict):
2336
+ raise ValueError("matching pair must be an object")
2337
+ old_id = str(raw_pair.get("old_id") or "")
2338
+ new_id = str(raw_pair.get("new_id") or "")
2339
+ try:
2340
+ old_node = old_lookup[old_id]
2341
+ new_node = new_lookup[new_id]
2342
+ except KeyError as exc:
2343
+ raise ValueError(f"matching pair references unknown node id: {exc}") from exc
2344
+ matching.append(Match(old_node, new_node))
2345
+ return matching
2346
+
2347
+
2348
+ def _changes_from_payload(
2349
+ raw_changes: Any,
2350
+ old_lookup: dict[str, SemanticNode],
2351
+ new_lookup: dict[str, SemanticNode],
2352
+ ) -> list[Change]:
2353
+ if not isinstance(raw_changes, list):
2354
+ raise ValueError("changes must be a list")
2355
+ changes: list[Change] = []
2356
+ for raw_change in raw_changes:
2357
+ change = Change.model_validate(raw_change)
2358
+ updates: dict[str, Any] = {}
2359
+ if change.old_node is not None:
2360
+ old_node = old_lookup.get(change.old_node.id)
2361
+ if old_node is None:
2362
+ raise ValueError(f"change references unknown old node id: {change.old_node.id}")
2363
+ updates["old_node"] = old_node
2364
+ if change.new_node is not None:
2365
+ new_node = new_lookup.get(change.new_node.id)
2366
+ if new_node is None:
2367
+ raise ValueError(f"change references unknown new node id: {change.new_node.id}")
2368
+ updates["new_node"] = new_node
2369
+ changes.append(change.model_copy(update=updates) if updates else change)
2370
+ return changes