intentumdiff-python 0.0.1__py3-none-win_amd64.whl → 0.0.3b1__py3-none-win_amd64.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 (150) hide show
  1. intentumdiff/__init__.py +9 -0
  2. intentumdiff/__main__.py +12 -0
  3. intentumdiff/_differ_gate.py +0 -136
  4. intentumdiff/_differ_presentation.py +17 -735
  5. intentumdiff/_differ_runtime.py +13 -64
  6. intentumdiff/analysis/compile_commands.py +26 -141
  7. intentumdiff/analysis/cross_file.py +23 -25
  8. intentumdiff/analysis/diagnostics.py +1 -164
  9. intentumdiff/analysis/guardrail_reports.py +31 -33
  10. intentumdiff/analysis/guardrails.py +111 -188
  11. intentumdiff/analysis/schema_resolver.py +72 -360
  12. intentumdiff/analysis/user_schemas.py +57 -254
  13. intentumdiff/api.py +21 -0
  14. intentumdiff/cache/sqlite_store.py +5 -23
  15. intentumdiff/cli/__init__.py +59 -0
  16. intentumdiff/cli/_commands.py +243 -137
  17. intentumdiff/cli/_parser.py +51 -56
  18. intentumdiff/cli/_shared.py +151 -116
  19. intentumdiff/content_type.py +13 -30
  20. intentumdiff/core/commit_differ.py +137 -286
  21. intentumdiff/core/diffignore.py +28 -34
  22. intentumdiff/core/index.py +16 -18
  23. intentumdiff/core/indexer.py +148 -169
  24. intentumdiff/core/models.py +279 -83
  25. intentumdiff/differ.py +366 -1000
  26. intentumdiff/github_app.py +40 -27
  27. intentumdiff/intentumdiff_rust_core/intentumdiff_rust_core.dll +0 -0
  28. intentumdiff/live_server.py +197 -34
  29. intentumdiff/lsp/client.py +49 -43
  30. intentumdiff/lsp/enricher.py +19 -119
  31. intentumdiff/lsp/exceptions.py +6 -7
  32. intentumdiff/lsp/servers.py +10 -11
  33. intentumdiff/lsp_server/_codelens.py +24 -57
  34. intentumdiff/lsp_server/_diagnostics.py +23 -51
  35. intentumdiff/lsp_server/_handlers.py +67 -9
  36. intentumdiff/lsp_server/server.py +28 -8
  37. intentumdiff/plugins/adapter.py +5 -0
  38. intentumdiff/plugins/builtins.py +7 -14
  39. intentumdiff/plugins/exceptions.py +28 -0
  40. intentumdiff/plugins/hub.py +39 -43
  41. intentumdiff/plugins/loader.py +229 -215
  42. intentumdiff/plugins/registry.py +259 -207
  43. intentumdiff/plugins/registry.schema.json +156 -15
  44. intentumdiff/plugins/registry_schema.py +47 -140
  45. intentumdiff/rust_core.py +623 -1326
  46. intentumdiff/serve/_app.py +26 -35
  47. intentumdiff/sources/base.py +24 -0
  48. intentumdiff/sources/file_source.py +15 -2
  49. intentumdiff/sources/git_source.py +64 -185
  50. intentumdiff/sources/live_buffer_source.py +45 -5
  51. intentumdiff/sources/patch_source.py +36 -109
  52. intentumdiff/vcs/_safe_path.py +7 -0
  53. intentumdiff/vcs/base.py +6 -0
  54. intentumdiff/vcs/git_backend.py +1 -1
  55. intentumdiff/vcs/git_cli.py +0 -27
  56. intentumdiff/vcs/hg_backend.py +1 -1
  57. intentumdiff/vcs/perforce_backend.py +7 -1
  58. intentumdiff/vcs/svn_backend.py +1 -1
  59. intentumdiff/wasm/abap_parser.wasm +0 -0
  60. intentumdiff/wasm/adf_parser.wasm +0 -0
  61. intentumdiff/wasm/asciidoc_parser.wasm +0 -0
  62. intentumdiff/wasm/asm_parser.wasm +0 -0
  63. intentumdiff/wasm/assemblyscript_parser.wasm +0 -0
  64. intentumdiff/wasm/astro_parser.wasm +0 -0
  65. intentumdiff/wasm/bash_parser.wasm +0 -0
  66. intentumdiff/wasm/clojure_parser.wasm +0 -0
  67. intentumdiff/wasm/cmake_parser.wasm +0 -0
  68. intentumdiff/wasm/cpp_parser.wasm +0 -0
  69. intentumdiff/wasm/csharp_parser.wasm +0 -0
  70. intentumdiff/wasm/css_parser.wasm +0 -0
  71. intentumdiff/wasm/dart_parser.wasm +0 -0
  72. intentumdiff/wasm/databricks_parser.wasm +0 -0
  73. intentumdiff/wasm/dax_parser.wasm +0 -0
  74. intentumdiff/wasm/delphi_parser.wasm +0 -0
  75. intentumdiff/wasm/dockerfile_parser.wasm +0 -0
  76. intentumdiff/wasm/elixir_parser.wasm +0 -0
  77. intentumdiff/wasm/freebasic_parser.wasm +0 -0
  78. intentumdiff/wasm/generic_parser.wasm +0 -0
  79. intentumdiff/wasm/gitignore_parser.wasm +0 -0
  80. intentumdiff/wasm/go_parser.wasm +0 -0
  81. intentumdiff/wasm/gomod_parser.wasm +0 -0
  82. intentumdiff/wasm/graphql_parser.wasm +0 -0
  83. intentumdiff/wasm/groovy_parser.wasm +0 -0
  84. intentumdiff/wasm/haskell_parser.wasm +0 -0
  85. intentumdiff/wasm/html_parser.wasm +0 -0
  86. intentumdiff/wasm/html_renderer.wasm +0 -0
  87. intentumdiff/wasm/ini_parser.wasm +0 -0
  88. intentumdiff/wasm/java_parser.wasm +0 -0
  89. intentumdiff/wasm/js_ts_parser.wasm +0 -0
  90. intentumdiff/wasm/json_parser.wasm +0 -0
  91. intentumdiff/wasm/kotlin_parser.wasm +0 -0
  92. intentumdiff/wasm/latex_parser.wasm +0 -0
  93. intentumdiff/wasm/llm_renderer.wasm +0 -0
  94. intentumdiff/wasm/lua_parser.wasm +0 -0
  95. intentumdiff/wasm/make_parser.wasm +0 -0
  96. intentumdiff/wasm/markdown_parser.wasm +0 -0
  97. intentumdiff/wasm/mdx_parser.wasm +0 -0
  98. intentumdiff/wasm/ocaml_parser.wasm +0 -0
  99. intentumdiff/wasm/odin_parser.wasm +0 -0
  100. intentumdiff/wasm/parser_manifest.json +81 -2
  101. intentumdiff/wasm/patch_renderer.wasm +0 -0
  102. intentumdiff/wasm/perl_parser.wasm +0 -0
  103. intentumdiff/wasm/php_parser.wasm +0 -0
  104. intentumdiff/wasm/plsql_parser.wasm +0 -0
  105. intentumdiff/wasm/po_parser.wasm +0 -0
  106. intentumdiff/wasm/postscript_parser.wasm +0 -0
  107. intentumdiff/wasm/powershell_parser.wasm +0 -0
  108. intentumdiff/wasm/proto_parser.wasm +0 -0
  109. intentumdiff/wasm/puppet_parser.wasm +0 -0
  110. intentumdiff/wasm/python_parser.wasm +0 -0
  111. intentumdiff/wasm/qsharp_parser.wasm +0 -0
  112. intentumdiff/wasm/r_parser.wasm +0 -0
  113. intentumdiff/wasm/reasonml_parser.wasm +0 -0
  114. intentumdiff/wasm/ruby_parser.wasm +0 -0
  115. intentumdiff/wasm/rust_parser.wasm +0 -0
  116. intentumdiff/wasm/sas_parser.wasm +0 -0
  117. intentumdiff/wasm/scala_parser.wasm +0 -0
  118. intentumdiff/wasm/scss_parser.wasm +0 -0
  119. intentumdiff/wasm/sql_parser.wasm +0 -0
  120. intentumdiff/wasm/squirrel_parser.wasm +0 -0
  121. intentumdiff/wasm/svelte_parser.wasm +0 -0
  122. intentumdiff/wasm/swift_parser.wasm +0 -0
  123. intentumdiff/wasm/terminal_renderer.wasm +0 -0
  124. intentumdiff/wasm/terraform_parser.wasm +0 -0
  125. intentumdiff/wasm/toml_parser.wasm +0 -0
  126. intentumdiff/wasm/tsql_parser.wasm +0 -0
  127. intentumdiff/wasm/vbnet_parser.wasm +0 -0
  128. intentumdiff/wasm/vue_parser.wasm +0 -0
  129. intentumdiff/wasm/wasm_provenance.json +884 -0
  130. intentumdiff/wasm/wat_parser.wasm +0 -0
  131. intentumdiff/wasm/xml_parser.wasm +0 -0
  132. intentumdiff/wasm/yaml_parser.wasm +0 -0
  133. intentumdiff/wasm/zig_parser.wasm +0 -0
  134. intentumdiff/watcher.py +15 -17
  135. intentumdiff_python-0.0.3b1.dist-info/METADATA +188 -0
  136. intentumdiff_python-0.0.3b1.dist-info/RECORD +164 -0
  137. {intentumdiff_python-0.0.1.dist-info → intentumdiff_python-0.0.3b1.dist-info}/WHEEL +1 -1
  138. {intentumdiff_python-0.0.1.dist-info → intentumdiff_python-0.0.3b1.dist-info}/sboms/intentumdiff-rust-core.cyclonedx.json +905 -379
  139. intentumdiff/analysis/invariances.py +0 -1207
  140. intentumdiff/analysis/keyed_profiles.py +0 -25
  141. intentumdiff/analysis/resource_profiles.py +0 -14
  142. intentumdiff/analysis/text_review.py +0 -46
  143. intentumdiff/core/cst_serializer.py +0 -101
  144. intentumdiff/invariances/__init__.py +0 -2
  145. intentumdiff/invariances/rules.schema.json +0 -283
  146. intentumdiff/invariances/rules.yaml +0 -349
  147. intentumdiff_python-0.0.1.dist-info/METADATA +0 -112
  148. intentumdiff_python-0.0.1.dist-info/RECORD +0 -165
  149. {intentumdiff_python-0.0.1.dist-info → intentumdiff_python-0.0.3b1.dist-info}/entry_points.txt +0 -0
  150. {intentumdiff_python-0.0.1.dist-info → intentumdiff_python-0.0.3b1.dist-info}/licenses/LICENSE +0 -0
intentumdiff/rust_core.py CHANGED
@@ -1,32 +1,26 @@
1
- """Rust core host adapter.
1
+ """Thin transport adapter over the authoritative Rust engine's C ABI.
2
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.
3
+ Maturin packages the cffi wheel. Python marshals public DTOs and host inputs;
4
+ shared decisions belong to Rust, never a Python fallback correctness engine.
8
5
  """
9
6
 
10
7
  from __future__ import annotations
11
8
 
12
- import importlib
13
9
  import json
14
10
  import logging
15
11
  import os
16
12
  import time
17
13
  from dataclasses import dataclass, field
18
- from pathlib import Path
19
14
  from typing import Any
20
15
 
21
16
  from pydantic import ValidationError
22
17
 
23
18
  from intentumdiff.core.models import (
19
+ FUEL_UNLIMITED,
24
20
  Change,
25
21
  ChangeGroup,
26
22
  ChangeType,
27
23
  DiffConfig,
28
- Match,
29
- Matching,
30
24
  SemanticDiff,
31
25
  SemanticNode,
32
26
  )
@@ -35,92 +29,12 @@ logger = logging.getLogger(__name__)
35
29
 
36
30
  _COMPLETE_STATUS = "complete"
37
31
  _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
32
  @dataclass(frozen=True)
120
33
  class RustCoreBatchAttempt:
121
34
  diff: SemanticDiff | None = None
122
35
  fallback_reason: str = ""
123
36
  backend_version: str = ""
37
+ invalid_output: bool = False # An invalid claimed-success Rust DTO is never a normal decline.
124
38
 
125
39
  @property
126
40
  def used(self) -> bool:
@@ -135,118 +49,13 @@ class RustCoreBatchDiffsAttempt:
135
49
  adapter_phase_timings: list[dict[str, Any]] = field(default_factory=list)
136
50
  backend_version: str = ""
137
51
  fallback_reason: str = ""
52
+ invalid_output: bool = False
138
53
 
139
54
  @property
140
55
  def used(self) -> bool:
141
56
  return bool(self.diffs)
142
57
 
143
58
 
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
59
  def _pyo3_extension_module_name() -> str | None:
251
60
  """Return the importable name of the compiled PyO3 extension, or ``None``.
252
61
 
@@ -277,23 +86,12 @@ def _pyo3_extension_module_name() -> str | None:
277
86
 
278
87
 
279
88
  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)")
89
+ # Maturin packages a plain C-ABI cdylib. A .so suffix does not make it
90
+ # a Python extension; importing it would require a nonexistent PyInit export.
294
91
  return _ctypes_backend()
295
92
 
296
93
 
94
+
297
95
  # ── Stateless C-ABI accessor (intentumdiff_call) ───────────────────────────────────────────────
298
96
  # The compiled core exports the language-agnostic C ABI (`intentumdiff_call`/`intentumdiff_free`,
299
97
  # ungated) alongside the PyO3 module, so the SAME artifact serves both. Handlers reachable ONLY
@@ -364,12 +162,48 @@ def _c_abi_lib() -> Any:
364
162
  return _C_ABI_LIB
365
163
 
366
164
 
165
+ class _InvalidNativeCAbiEnvelope(RuntimeError):
166
+ """Corrupted C ABI response: a different Rust route must not mask the failure."""
167
+
168
+
169
+ class _NativeCAbiOperationFailure(Exception):
170
+ """Rust returned an operation error, not a Rust-controlled fallback status.
171
+
172
+ This marker preserves each historical Python exception category via the
173
+ subclasses below while preventing the adapter from mistaking raised native
174
+ failures for an explicit status="fallback" result (#219).
175
+ """
176
+
177
+
178
+ class _NativeCAbiNotFoundError(FileNotFoundError, _NativeCAbiOperationFailure):
179
+ """Native not_found error with its existing FileNotFoundError contract."""
180
+
181
+
182
+ class _NativeCAbiValueError(ValueError, _NativeCAbiOperationFailure):
183
+ """Native input error with its existing ValueError contract."""
184
+
185
+
186
+ class _NativeCAbiInternalError(RuntimeError, _NativeCAbiOperationFailure):
187
+ """Native internal/panic error with its existing RuntimeError contract."""
188
+
189
+
190
+ class CoreSizeLimitError(_NativeCAbiValueError):
191
+ """Core refused an input, tree or parser output over a configured byte limit.
192
+
193
+ Core reports these as ``error_type: "size_limit"`` and its message names the diff-config
194
+ setting that raises the limit (``max_cst_bytes`` / ``max_plugin_output_bytes``). It
195
+ subclasses the ``ValueError`` these errors raised before core #308, so existing
196
+ ``except ValueError`` callers keep working.
197
+ """
198
+
199
+
367
200
  def _c_abi_call(name: str, *args: Any) -> Any:
368
201
  """Invoke a C-ABI handler by name; return its ``result`` or re-raise the classified error.
369
202
 
370
203
  `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``.
204
+ ``FileNotFoundError`` (the git readers' only non-``ValueError``), ``size_limit`` →
205
+ ``CoreSizeLimitError`` (a ``ValueError``), everything else → ``ValueError``. Panics
206
+ surface as ``RuntimeError``.
373
207
  """
374
208
  import ctypes # noqa: PLC0415
375
209
 
@@ -381,21 +215,49 @@ def _c_abi_call(name: str, *args: Any) -> Any:
381
215
  name.encode("utf-8"), json.dumps(payload).encode("utf-8")
382
216
  )
383
217
  if not ptr:
384
- raise RuntimeError(f"intentumdiff_call({name!r}) returned NULL (allocation failure)")
218
+ raise _InvalidNativeCAbiEnvelope(
219
+ f"intentumdiff_call({name!r}) returned NULL (allocation failure)"
220
+ )
385
221
  try:
386
222
  raw = ctypes.cast(ptr, ctypes.c_char_p).value
387
223
  finally:
388
224
  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")
225
+ try:
226
+ envelope = json.loads((raw or b"").decode("utf-8"))
227
+ except (UnicodeDecodeError, ValueError, TypeError) as exc:
228
+ raise _InvalidNativeCAbiEnvelope(
229
+ f"invalid C ABI envelope for {name!r}: malformed JSON: {exc}"
230
+ ) from exc
231
+ # Only the Rust ABI can certify success. A malformed envelope must not
232
+ # manufacture a None (or other truthy) result for a successful operation.
233
+ if not isinstance(envelope, dict) or type(envelope.get("ok")) is not bool:
234
+ raise _InvalidNativeCAbiEnvelope(
235
+ f"invalid C ABI envelope for {name!r}: expected a boolean 'ok'"
236
+ )
237
+ if envelope["ok"]:
238
+ if "result" not in envelope:
239
+ raise _InvalidNativeCAbiEnvelope(
240
+ f"invalid C ABI envelope for {name!r}: success missing 'result'"
241
+ )
242
+ return envelope["result"]
243
+ # The current Rust Core C ABI always emits both fields on a decline.
244
+ # Missing or incorrectly typed errors are corrupted transport, not an
245
+ # explicit decline that Python may route around.
246
+ message = envelope.get("error")
247
+ kind = envelope.get("error_type")
248
+ if not isinstance(message, str) or not isinstance(kind, str) or kind not in (
249
+ "not_found", "internal", "value_error", "bad_request", "size_limit"
250
+ ):
251
+ raise _InvalidNativeCAbiEnvelope(
252
+ f"invalid C ABI envelope for {name!r}: malformed error fields"
253
+ )
394
254
  if kind == "not_found":
395
- raise FileNotFoundError(message)
255
+ raise _NativeCAbiNotFoundError(message)
396
256
  if kind == "internal":
397
- raise RuntimeError(message)
398
- raise ValueError(message)
257
+ raise _NativeCAbiInternalError(message)
258
+ if kind == "size_limit":
259
+ raise CoreSizeLimitError(message)
260
+ raise _NativeCAbiValueError(message)
399
261
 
400
262
 
401
263
  class _CtypesBackend:
@@ -405,9 +267,7 @@ class _CtypesBackend:
405
267
  stateless pyfunction surface.
406
268
 
407
269
  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).
270
+ for the pyfunction ``finalize_review_json``), so a trailing ``_json`` is stripped.
411
271
 
412
272
  Result encoding: ``_c_abi_call`` returns the *parsed* ``result``. The PyO3 pyfunctions were
413
273
  heterogeneous — some returned native values used directly (``supports_language`` -> bool,
@@ -417,7 +277,6 @@ class _CtypesBackend:
417
277
  with ``json.dumps`` so the caller's parse still works. ``_RAW_HANDLERS`` is the (small) native set.
418
278
  """
419
279
 
420
- _COMMIT_TUPLE = frozenset({"diff_batch_commit_json", "diff_working_tree_python_commit_json"})
421
280
  _RAW_HANDLERS = frozenset({
422
281
  "version",
423
282
  "supports_language",
@@ -436,16 +295,6 @@ class _CtypesBackend:
436
295
  def __getattr__(self, name: str) -> Any:
437
296
  if name.startswith("__"):
438
297
  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
298
  handler = name[:-5] if name.endswith("_json") else name
450
299
  if handler == "vcs_backend_get_blob":
451
300
  # The C ABI wraps blob content in {"content": …} so JSON-valued content is not
@@ -480,216 +329,53 @@ def _ctypes_backend() -> _CtypesBackend:
480
329
 
481
330
 
482
331
  def enrich_node_facts(tree: SemanticNode) -> SemanticNode:
483
- """Fill language-agnostic NodeFacts (issue #70) on a Wasm-parsed tree via the Rust core.
332
+ """Request authoritative language-agnostic NodeFacts enrichment from Rust.
484
333
 
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.
334
+ Rust supplies structural facts for every shipped parser. Returning the input
335
+ tree after an engine error can silently lose intent evidence, so the 0.0.2
336
+ boundary rejects missing handlers and malformed responses instead of
337
+ manufacturing successful un-enriched output. A valid no-op is supported.
490
338
  """
491
339
  try:
492
- backend = _load_backend()
493
- enrich = getattr(backend, "enrich_node_facts_json", None)
494
- if enrich is None:
495
- return tree
340
+ enrich = getattr(_load_backend(), "enrich_node_facts_json", None)
341
+ if not callable(enrich):
342
+ raise RuntimeError("required Rust node facts handler is unavailable")
496
343
  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
- )
344
+ except Exception as exc: # noqa: BLE001 - native failures must never become success.
345
+ raise RuntimeError(f"Rust node facts enrichment failed: {exc}") from exc
523
346
 
524
347
 
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
- }
348
+ def collect_lsp_hover_targets(root: SemanticNode, source: str) -> list[tuple[str, int, int]]:
349
+ """Rust-owned selection and UTF-8 byte to UTF-16 protocol conversion."""
350
+ data = _required_engine_json(
351
+ "lsp_collect_utf16_hover_targets_json", root.model_dump_json(), source, result_type=list,
544
352
  )
353
+ return _hover_target_dtos(data)
545
354
 
546
355
 
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
- }
356
+ def attach_lsp_type_map(root: SemanticNode, type_map: dict[str, str]) -> SemanticNode:
357
+ """Attach host-observed LSP type DTOs using the Rust C ABI, never Python tree mutation."""
358
+ response = _required_engine_json(
359
+ "lsp_attach_type_map_json",
360
+ root.model_dump_json(),
361
+ json.dumps(type_map, sort_keys=True),
362
+ result_type=dict,
566
363
  )
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
364
  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
-
365
+ return SemanticNode.model_validate(response)
366
+ except ValidationError as exc:
367
+ raise RuntimeError("Rust LSP type-map attachment returned invalid tree DTO") from exc
666
368
 
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
369
 
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
370
+ def _hover_target_dtos(data: list[Any]) -> list[tuple[str, int, int]]:
371
+ targets = []
372
+ for item in data:
373
+ if (not isinstance(item, dict) or not isinstance(item.get("id"), str)
374
+ or type(item.get("line")) is not int or type(item.get("col")) is not int
375
+ or item["line"] < 0 or item["col"] < 0):
376
+ raise RuntimeError("Rust hover-target response has an invalid target DTO")
377
+ targets.append((item["id"], item["line"], item["col"]))
378
+ return targets
693
379
 
694
380
 
695
381
  def try_rust_finalize_review(
@@ -705,19 +391,25 @@ def try_rust_finalize_review(
705
391
  """Route the per-stage draft finalization through the Rust core (issue #57).
706
392
 
707
393
  Runs the SAME refine+finalize pipeline as the certified batch from the provided
708
- semantic trees. None -> caller stays on the Python transitional path.
394
+ semantic trees. Only an explicit Rust tree-size guard may decline.
709
395
  """
710
396
  try:
711
397
  backend = _load_backend()
712
398
  finalize_fn = getattr(backend, "finalize_review_json", None)
713
399
  if not callable(finalize_fn):
714
- return None
400
+ raise RuntimeError("required Rust finalize_review_json handler is unavailable")
715
401
  config_json = json.dumps(
716
402
  {
717
403
  "min_height": config.min_height,
718
404
  "min_similarity": config.min_similarity,
719
405
  "max_nodes": config.max_nodes,
720
406
  "collect_trace": collect_trace,
407
+ "fallback_to_token_diff": config.fallback_to_token_diff,
408
+ "ignore_style": config.ignore_style,
409
+ "detect_refactorings": config.detect_refactorings,
410
+ "approx_move_threshold": config.approx_move_threshold,
411
+ "leaf_text_diff": config.leaf_text_diff,
412
+ "extra_trivia_types": config.extra_trivia_types,
721
413
  }
722
414
  )
723
415
  data = json.loads(
@@ -730,29 +422,127 @@ def try_rust_finalize_review(
730
422
  config_json,
731
423
  )
732
424
  )
733
- if not isinstance(data, dict) or not data.get("used"):
425
+ # The authoritative Rust finalize_review_impl emits explicit arrays and
426
+ # booleans on every successful response. Never turn a missing or malformed
427
+ # field into an empty/style-only result: that silently discards engine
428
+ # evidence and can misclassify meaningful edits.
429
+ if not isinstance(data, dict) or type(data.get("used")) is not bool:
430
+ raise ValueError("invalid Rust finalize response: 'used' must be a boolean")
431
+ if not data["used"]:
432
+ # The only explicit Rust decline is the max-nodes guard.
433
+ if data.get("reason") != "tree_too_large":
434
+ raise ValueError("invalid Rust finalize response: unexpected decline")
734
435
  return None
436
+
437
+ for key in ("changes", "change_groups", "ignored_style_changes"):
438
+ if not isinstance(data.get(key), list):
439
+ raise ValueError(f"invalid Rust finalize response: '{key}' must be an array")
440
+ for key in ("is_style_only", "no_surviving_changes"):
441
+ if type(data.get(key)) is not bool:
442
+ raise ValueError(f"invalid Rust finalize response: '{key}' must be a boolean")
443
+
444
+ ignored = data["ignored_style_changes"]
445
+ if any(not isinstance(item, dict) for item in ignored):
446
+ raise ValueError("invalid Rust finalize response: malformed ignored-style item")
447
+ # Trace is optional: Rust omits it for parse-error/source-lifecycle
448
+ # responses, but when present it must be a well-formed array.
449
+ trace = data.get("trace", [])
450
+ if not isinstance(trace, list) or any(not isinstance(item, dict) for item in trace):
451
+ raise ValueError("invalid Rust finalize response: malformed trace")
452
+ fallback_diff = data.get("fallback_diff")
453
+ if fallback_diff is not None and not isinstance(fallback_diff, dict):
454
+ raise ValueError("invalid Rust finalize response: malformed fallback_diff")
455
+ # Core #252: the proof rule and the suppression record go back to
456
+ # complete_routed_review verbatim, so core (not Python) states completeness.
457
+ proof = data.get("equivalence_proof")
458
+ if proof is not None and not isinstance(proof, str):
459
+ raise ValueError("invalid Rust finalize response: malformed equivalence_proof")
460
+ suppression_trace = data.get("suppression_trace")
461
+ if suppression_trace is not None and not isinstance(suppression_trace, list):
462
+ raise ValueError("invalid Rust finalize response: malformed suppression_trace")
463
+
735
464
  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
- ],
465
+ "fallback_diff": (
466
+ SemanticDiff.model_validate(_require_complete_batch_diff_dto(fallback_diff))
467
+ if fallback_diff is not None else None
468
+ ),
469
+ "changes": [Change.model_validate(item) for item in
470
+ _require_source_anchored_changes(data["changes"], "Rust finalize review")],
471
+ "change_groups": [ChangeGroup.model_validate(item) for item in data["change_groups"]],
472
+ "is_style_only": data["is_style_only"],
473
+ "no_surviving_changes": data["no_surviving_changes"],
474
+ "ignored_style_changes": [dict(item) for item in ignored],
475
+ "trace": [dict(item) for item in trace],
476
+ "equivalence_proof": proof,
477
+ "suppression_trace": suppression_trace,
752
478
  }
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
479
+ except Exception as exc: # noqa: BLE001 - an engine failure is not an unsupported decline.
480
+ raise RuntimeError(f"Rust finalize review failed: {exc}") from exc
481
+
482
+
483
+ def complete_routed_review(*, finalized: dict[str, Any], old_tree: SemanticNode,
484
+ new_tree: SemanticNode, old_source: str, new_source: str,
485
+ old_filename: str, new_filename: str, language: str,
486
+ schema_metadata: dict[str, Any] | None = None,
487
+ compile_metadata: dict[str, Any] | None = None) -> SemanticDiff:
488
+ """Marshal routed review inputs; the Rust engine owns final meaning and evidence."""
489
+ payload = dict(finalized)
490
+ for field in ("changes", "change_groups"):
491
+ payload[field] = [item.model_dump(mode="json") for item in finalized[field]]
492
+ # Rust owns the final inventories and semantic flags. Pydantic defaults
493
+ # must never turn a truncated claimed-complete Rust response into a
494
+ # successful no-change or style-only result (#208).
495
+ response = _c_abi_call("complete_routed_review", json.dumps({
496
+ "finalized": payload, "old_tree": old_tree.model_dump(mode="json"),
497
+ "new_tree": new_tree.model_dump(mode="json"), "old_source": old_source,
498
+ "new_source": new_source, "old_filename": old_filename, "new_filename": new_filename,
499
+ "language": language, "schema_metadata": schema_metadata, "compile_metadata": compile_metadata,
500
+ }))
501
+ return SemanticDiff.model_validate(_require_complete_batch_diff_dto(response))
502
+
503
+
504
+ def source_fallback_diff(old: str, new: str, old_filename: str, new_filename: str,
505
+ language: str, reason: str = "parse_errors") -> SemanticDiff:
506
+ """Shared source comparison; Python only marshals the C ABI."""
507
+ # Rust owns source fallback classification, inventories, style and spans.
508
+ # Decode before Pydantic to prevent its defaults from hiding a truncated
509
+ # claimed-complete Rust response (#210).
510
+ raw = _load_backend().source_fallback_diff_json(
511
+ old, new, old_filename, new_filename, language, reason
512
+ )
513
+ return SemanticDiff.model_validate(_require_complete_batch_diff_dto(json.loads(raw)))
514
+
515
+
516
+ def _decode_rust_boolean(payload: str, handler: str) -> bool:
517
+ """Validate Rust's boolean DTO without Python truthiness reinterpretation."""
518
+ decoded = json.loads(payload)
519
+ if type(decoded) is not bool:
520
+ raise ValueError(f"Rust {handler} must return a JSON boolean, got {type(decoded).__name__}")
521
+ return decoded
522
+
523
+
524
+ def parse_errors_present(source: str, tree_json: str, language: str) -> bool:
525
+ return _decode_rust_boolean(
526
+ _load_backend().parse_errors_present_json(source, tree_json, language),
527
+ "parse_errors_present",
528
+ )
529
+
530
+
531
+ def enrich_literal_labels(tree: SemanticNode, source: str) -> SemanticNode:
532
+ """Source-label enrichment owned by the shared Rust engine."""
533
+ return SemanticNode.model_validate_json(
534
+ _load_backend().enrich_literal_labels_json(tree.model_dump_json(), source)
535
+ )
536
+
537
+
538
+ def review_trees_equivalent(old_tree: SemanticNode, new_tree: SemanticNode) -> bool:
539
+ """Ask core for conservative review equivalence, including literal whitespace."""
540
+ return _decode_rust_boolean(
541
+ _load_backend().review_trees_equivalent_json(
542
+ old_tree.model_dump_json(), new_tree.model_dump_json()
543
+ ),
544
+ "review_trees_equivalent",
545
+ )
756
546
 
757
547
 
758
548
  def try_rust_profile_label_enrichment(
@@ -760,120 +550,91 @@ def try_rust_profile_label_enrichment(
760
550
  source: str,
761
551
  language: str,
762
552
  identity_fields: tuple[str, ...] = (),
763
- ) -> SemanticNode | None:
764
- """Profile-label enrichment in the Rust core (issue #57 profile-enrichment port).
553
+ ) -> SemanticNode:
554
+ """Request authoritative Rust profile-label enrichment for a supported parser.
765
555
 
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).
556
+ A missing backend handler is a contract failure, not an empty/no-op
557
+ enrichment: silently returning an un-enriched tree can alter matching and
558
+ suppress meaningful changes in the release candidate.
768
559
  """
769
560
  try:
770
561
  backend = _load_backend()
771
562
  enrich_fn = getattr(backend, "enrich_profile_labels_json", None)
772
563
  if not callable(enrich_fn):
773
- return None
564
+ raise RuntimeError("required Rust profile enrichment handler is unavailable")
774
565
  return SemanticNode.model_validate_json(
775
566
  enrich_fn(tree.model_dump_json(), source, language, list(identity_fields))
776
567
  )
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
568
+ except Exception as exc: # noqa: BLE001 - an engine failure is not an unsupported decline.
569
+ raise RuntimeError(f"Rust profile enrichment failed: {exc}") from exc
780
570
 
781
571
 
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
- """
572
+ def _required_engine_json(handler: str, *args: Any, result_type: type) -> Any:
573
+ """Marshal a required engine operation; failures cannot become empty success."""
791
574
  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
575
+ operation = getattr(_load_backend(), handler, None)
576
+ if not callable(operation):
577
+ raise RuntimeError("required handler is unavailable")
578
+ result = json.loads(operation(*args))
579
+ if isinstance(result, dict) and "error" in result:
580
+ raise RuntimeError(str(result["error"]))
581
+ if not isinstance(result, result_type):
582
+ raise ValueError(f"expected {result_type.__name__} result")
801
583
  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
584
+ except Exception as exc:
585
+ raise RuntimeError(f"Rust engine operation {handler} failed: {exc}") from exc
805
586
 
806
587
 
807
- def try_rust_build_reference_table(files_json: str) -> str | None:
808
- """Reference-table extraction in the Rust core (index-engine-lib, #91).
588
+ def render_cli_review(diff: SemanticDiff, *, width: int, color: bool) -> str:
589
+ """Transport an authoritative DTO to Rust's rs-rich presentation renderer."""
590
+ request = {"diff": diff.model_dump(mode="json"), "width": width, "color": color}
591
+ return _required_engine_json("render_cli_review", json.dumps(request), result_type=str)
809
592
 
810
- Returns the ReferenceTable JSON, or None when the backend is unavailable or
811
- the core reports an error envelope.
593
+
594
+ def fuel_budget(plugin_fuel: int, content_len: int) -> int:
595
+ """Transport core's content-scaled Wasm fuel policy (``fuel_budget``, #308).
596
+
597
+ Core answers ``{"fuel": u64}`` or ``{"fuel": null}`` (unlimited, a negative
598
+ ``plugin_fuel``); ``null`` is returned as the binding's ``FUEL_UNLIMITED`` (-1).
812
599
  """
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
600
+ config_json = json.dumps({"plugin_fuel": plugin_fuel})
601
+ result = _required_engine_json("fuel_budget", config_json, content_len, result_type=dict)
602
+ fuel = result.get("fuel", ...)
603
+ if fuel is None:
604
+ return FUEL_UNLIMITED
605
+ if type(fuel) is not int or fuel < 0:
606
+ raise RuntimeError(f"Rust engine operation fuel_budget returned invalid {result!r}")
607
+ return fuel
827
608
 
828
609
 
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).
610
+ def try_rust_build_symbol_table(files_json: str) -> str:
611
+ """Extract a symbol table in Rust, raising on unavailable or invalid results.
833
612
 
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.
613
+ The historical function name is retained for compatibility; failure is never None.
838
614
  """
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
615
+ return json.dumps(_required_engine_json("build_symbol_table_json", files_json, result_type=dict))
616
+
617
+
618
+ def try_rust_build_reference_table(files_json: str) -> str:
619
+ """Extract a reference table in Rust or raise an actionable engine error."""
620
+ return json.dumps(_required_engine_json("build_reference_table_json", files_json, result_type=dict))
621
+
852
622
 
623
+ def try_rust_diff_symbol_tables(old_json: str, new_json: str) -> list[dict[str, Any]]:
624
+ """Compare symbol tables in Rust; an empty list means successful no-change."""
625
+ return _required_engine_json("diff_symbol_tables_json", old_json, new_json, result_type=list)
853
626
 
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
627
 
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.
628
+ def commit_cross_file_index(files: list[dict[str, Any]]) -> dict[str, Any]:
629
+ """Core's commit-wide cross-file index over host-parsed trees (core #298).
630
+
631
+ Each file is ``{old_filename, new_filename, language, old_tree, new_tree, old_error,
632
+ new_error}``. Core decides which files are indexable, reports the rest in
633
+ ``index_errors`` and owns ``cross_file_index_status``.
863
634
  """
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
635
+ return _required_engine_json(
636
+ "commit_cross_file_index_json", json.dumps(files), result_type=dict
637
+ )
877
638
 
878
639
 
879
640
  def load_project_diff_config_data(
@@ -1010,6 +771,25 @@ class _RustCacheStore:
1010
771
  )
1011
772
  )
1012
773
 
774
+ def list_entries_filtered(
775
+ self,
776
+ table: str,
777
+ language: str | None,
778
+ since: int | None,
779
+ before: int | None,
780
+ min_size: int | None,
781
+ max_size: int | None,
782
+ limit: int,
783
+ file_glob: str | None,
784
+ ) -> str:
785
+ return json.dumps(
786
+ _c_abi_call(
787
+ "cache_list_entries_filtered",
788
+ self._p, self._t, self._m,
789
+ table, language, since, before, min_size, max_size, limit, file_glob,
790
+ )
791
+ )
792
+
1013
793
  def get_entry_metadata(self, key: str, table: str) -> str | None:
1014
794
  result = _c_abi_call("cache_get_entry_metadata", self._p, self._t, self._m, key, table)
1015
795
  return json.dumps(result) if result is not None else None
@@ -1141,6 +921,50 @@ def validate_dep_hashes(
1141
921
  return fn(dep_hashes, allowed_dependencies, package_name, install_target)
1142
922
 
1143
923
 
924
+ def validate_registry_manifest(manifest_json: str) -> list[dict[str, str]]:
925
+ """Transport core's registry-manifest validator (``validate_registry_manifest``, #329).
926
+
927
+ Returns core's errors unchanged (``[]`` = valid), each ``{"path", "message"}``. A response
928
+ whose ``ok`` disagrees with its errors, or any other shape, raises.
929
+ """
930
+ result = _required_engine_json(
931
+ "validate_registry_manifest", manifest_json, result_type=dict
932
+ )
933
+ errors = result.get("errors")
934
+ if (
935
+ type(result.get("ok")) is not bool
936
+ or not isinstance(errors, list)
937
+ or result["ok"] is not (not errors)
938
+ or not all(
939
+ isinstance(e, dict) and set(e) == {"path", "message"}
940
+ and all(isinstance(v, str) for v in e.values())
941
+ for e in errors
942
+ )
943
+ ):
944
+ raise RuntimeError(f"Rust engine operation validate_registry_manifest returned {result!r}")
945
+ return errors
946
+
947
+
948
+ def parser_allowlist_matches(
949
+ allowed_plugins: list[str] | None, identity: dict[str, Any]
950
+ ) -> dict[str, Any]:
951
+ """Transport core's parser-allowlist rule (``parser_allowlist_matches``, #311).
952
+
953
+ *identity* is ``{"plugin_id", "grammar_id", "languages", "aliases"}`` (all optional).
954
+ Returns core's ``{"matches": bool, "matched_by": str | None}`` unchanged; core's rule is
955
+ ``None`` = every parser, ``[]`` = none, else an exact grammar/plugin/alias/language id.
956
+ """
957
+ result = _required_engine_json(
958
+ "parser_allowlist_matches", allowed_plugins, json.dumps(identity), result_type=dict
959
+ )
960
+ matches, matched_by = result.get("matches"), result.get("matched_by", ...)
961
+ if type(matches) is not bool or not (
962
+ isinstance(matched_by, str) if matches else matched_by is None
963
+ ):
964
+ raise RuntimeError(f"Rust engine operation parser_allowlist_matches returned {result!r}")
965
+ return result
966
+
967
+
1144
968
  # ── Git source-collection read ops (Rust-authoritative, #98, A2.4) ──────────
1145
969
 
1146
970
 
@@ -1186,31 +1010,16 @@ def git_repo_toplevel(repo_path: str) -> str:
1186
1010
  return fn(repo_path)
1187
1011
 
1188
1012
 
1189
- def changed_commit_sources(
1013
+ def changed_sources(
1190
1014
  repo_path: str, old_ref: str, new_ref: str
1191
1015
  ) -> list[tuple[str, str, str, str, str | None]]:
1192
- """Yield changed-source rows for a commit-to-commit diff via the Rust core.
1016
+ """Yield changed-source rows for any diff mode via the Rust core (the full
1017
+ ``iter_changed_sources`` dispatcher).
1193
1018
 
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``).
1019
+ Each row is ``(old_content, new_content, old_path, new_path, staging_status)``.
1020
+ ``new_ref`` sentinels: ``""`` = working tree (per-file "staged"/"unstaged" +
1021
+ "untracked"), ``":staged"`` = the index, ``":unpushed"`` = commits not yet pushed;
1022
+ anything else is a commit-to-commit diff (``staging_status`` is ``None``).
1214
1023
  """
1215
1024
  backend = _load_backend()
1216
1025
  fn = getattr(backend, "changed_sources_json", None)
@@ -1228,6 +1037,11 @@ def vcs_backend_resolve_root(vcs: str, repo_path: str) -> str:
1228
1037
  return _load_backend().vcs_backend_resolve_root(vcs, repo_path)
1229
1038
 
1230
1039
 
1040
+ def p4_policy_path(client_root: str, depot_path: str) -> str:
1041
+ """Resolve a depot path's physical policy context in the shared Rust host."""
1042
+ return json.loads(_load_backend().p4_policy_path_json(client_root, depot_path))["path"]
1043
+
1044
+
1231
1045
  def vcs_backend_get_blob(
1232
1046
  vcs: str, repo_path: str, path: str, ref: str, svn_repo_url: str | None = None
1233
1047
  ) -> str:
@@ -1322,11 +1136,12 @@ def live_handle_diff(
1322
1136
  config_json: str,
1323
1137
  wasm_dir: str = "",
1324
1138
  ) -> dict[str, Any]:
1325
- """Native single-file diff (Phase A1/R): `{diff}` (certified) or `{fallback: reason}`.
1139
+ """Native single-file diff (Phase A1/R): certified DTO or decline.
1326
1140
 
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.
1141
+ The Rust handler uses bundled-parser manifests (native Python parser and
1142
+ other languages' Wasm parsers). A fallback marker is NOT a certified
1143
+ semantic diff: live transports must return a typed error, not recover via
1144
+ a second success path.
1330
1145
  """
1331
1146
  return json.loads(
1332
1147
  _load_backend().live_handle_diff_json(
@@ -1338,21 +1153,32 @@ def live_handle_diff(
1338
1153
  def live_handle_review(
1339
1154
  repo_path: str, old_ref: str, new_ref: str, config_json: str, wasm_dir: str = ""
1340
1155
  ) -> dict[str, Any]:
1341
- """Native commit review (Phase A2 + d2-review): `{commit_diff}` or `{fallback: reason}`.
1156
+ """Native commit review (Phase A2 + d2-review): certified DTO or decline.
1342
1157
 
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.
1158
+ Rust resolves changed files via the bundled-parser manifest in *wasm_dir*.
1159
+ Missing or non-certified results are declines, not permission for a live
1160
+ transport to manufacture a successful Python-path review.
1348
1161
  """
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)
1162
+ return json.loads(
1163
+ _load_backend().live_handle_review_json(repo_path, old_ref, new_ref, config_json, wasm_dir)
1164
+ )
1165
+
1166
+
1167
+ def live_handle_asset_diff(
1168
+ repo_path: str, default_ref: str, request: dict[str, Any], seq: int
1169
+ ) -> dict[str, Any]:
1170
+ """Native perceptual asset diff: the whole protocol response for op ``asset_diff``.
1171
+
1172
+ Unlike the other ``live_*`` handlers this returns a finished response envelope — success or
1173
+ error — because a malformed request is an answer to send, not a call that failed. Request
1174
+ parsing, path containment and the artifact cache all live in the core, so the Python server
1175
+ and the native binary answer identically.
1176
+ """
1177
+ return json.loads(
1178
+ _load_backend().live_handle_asset_diff_json(
1179
+ repo_path, default_ref, json.dumps(request, separators=(",", ":")), int(seq)
1180
+ )
1181
+ )
1356
1182
 
1357
1183
 
1358
1184
  _registered_xml_dialects_fingerprint: str | None = None
@@ -1361,51 +1187,21 @@ _registered_xml_dialects_fingerprint: str | None = None
1361
1187
  def try_register_user_xml_dialects(dialects: list[dict[str, Any]]) -> bool:
1362
1188
  """Marshal user XML dialect specs (issue #86) into the Rust registry.
1363
1189
 
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.
1190
+ Idempotent per successfully registered payload. Backend and registration
1191
+ failures propagate: configured matching rules must never be silently ignored.
1192
+ An empty payload clears the native registry.
1367
1193
  """
1368
1194
  global _registered_xml_dialects_fingerprint
1369
1195
  payload = json.dumps(dialects, sort_keys=True)
1370
1196
  if payload == _registered_xml_dialects_fingerprint:
1371
1197
  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
- )
1198
+ backend = _load_backend()
1199
+ register_fn = getattr(backend, "register_user_xml_dialects_json", None)
1200
+ if not callable(register_fn):
1201
+ raise RuntimeError("backend does not expose register_user_xml_dialects_json")
1202
+ register_fn(payload)
1203
+ _registered_xml_dialects_fingerprint = payload
1204
+ return True
1409
1205
 
1410
1206
 
1411
1207
  def try_rust_core_batch_diff(
@@ -1421,13 +1217,8 @@ def try_rust_core_batch_diff(
1421
1217
  file_lifecycle: str | None = None,
1422
1218
  ) -> RustCoreBatchAttempt:
1423
1219
  """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
1220
  if language != "python":
1427
1221
  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
1222
  if config.diagnostics:
1432
1223
  return RustCoreBatchAttempt(fallback_reason="diagnostics require Python pipeline")
1433
1224
 
@@ -1456,6 +1247,7 @@ def try_rust_core_batch_diff(
1456
1247
  or batch.fallback_reason
1457
1248
  or "batch item is incomplete",
1458
1249
  backend_version=batch.backend_version,
1250
+ invalid_output=batch.invalid_output,
1459
1251
  )
1460
1252
 
1461
1253
 
@@ -1483,10 +1275,74 @@ def _normalize_builtin_change_type_enums(diff: SemanticDiff) -> SemanticDiff:
1483
1275
  return diff.model_copy(update={"changes": normalized_changes})
1484
1276
 
1485
1277
 
1278
+ _REQUIRED_RUST_CHANGE_NODES: dict[str, tuple[str, ...]] = {
1279
+ # Structural DTO invariants only: never infer changes, spans or style in Python.
1280
+ "ADDITION": ("new_node",),
1281
+ "DELETION": ("old_node",),
1282
+ "MODIFICATION": ("old_node", "new_node"),
1283
+ "MOVE": ("old_node", "new_node"),
1284
+ "REORDER": ("old_node", "new_node"),
1285
+ }
1286
+
1287
+
1288
+ def _require_source_anchored_changes(changes: Any, context: str) -> list[dict[str, Any]]:
1289
+ """Reject truncated Rust change DTOs before Pydantic supplies nullable nodes.
1290
+
1291
+ Rust omits the *inapplicable* side of additions and deletions. The source
1292
+ side that proves the reported change is mandatory. STYLE_ONLY and other
1293
+ extension kinds retain their existing DTO contract; no semantic decision
1294
+ or source-span inference takes place in this adapter (#228).
1295
+ """
1296
+ if not isinstance(changes, list):
1297
+ raise ValueError(f"{context} requires changes array")
1298
+ for index, change in enumerate(changes):
1299
+ if not isinstance(change, dict):
1300
+ raise ValueError(f"{context} change[{index}] must be an object")
1301
+ for node_field in _REQUIRED_RUST_CHANGE_NODES.get(change.get("change_type"), ()):
1302
+ if not isinstance(change.get(node_field), dict):
1303
+ raise ValueError(
1304
+ f"{context} change[{index}] {change.get('change_type')!r} "
1305
+ f"requires {node_field} source-node object"
1306
+ )
1307
+ return changes
1308
+
1309
+
1310
+ def _require_complete_batch_diff_dto(payload: Any) -> dict[str, Any]:
1311
+ """Reject incomplete Rust success before Python DTO defaults can hide omissions.
1312
+
1313
+ Rust supplies the change inventory and semantic flags. This adapter checks
1314
+ their presence and types only; it never recomputes semantic meaning (#198).
1315
+ """
1316
+ if not isinstance(payload, dict):
1317
+ raise ValueError("Rust complete batch diff must be an object")
1318
+ for field in ("changes", "change_groups", "parse_errors", "guardrail_violations"):
1319
+ if not isinstance(payload.get(field), list):
1320
+ raise ValueError(f"Rust complete batch diff requires {field} array")
1321
+ _require_source_anchored_changes(payload["changes"], "Rust complete batch diff")
1322
+ for field in ("has_semantic_changes", "is_style_only", "is_fallback"):
1323
+ if type(payload.get(field)) is not bool:
1324
+ raise ValueError(f"Rust complete batch diff requires {field} boolean")
1325
+ if not isinstance(payload.get("metadata"), dict):
1326
+ raise ValueError("Rust complete batch diff requires metadata object")
1327
+ # Core #252: a stated completeness verdict travels with its `incomplete` field (object
1328
+ # or null); the typed DTO checks both. Echo ops given a DTO without a verdict (one not
1329
+ # served by core) return none, which stays None rather than being invented here.
1330
+ if payload.get("completeness") is not None:
1331
+ if payload["completeness"] not in ("complete", "equivalent", "incomplete"):
1332
+ raise ValueError("Rust complete batch diff requires completeness "
1333
+ "'complete', 'equivalent' or 'incomplete'")
1334
+ if "incomplete" not in payload:
1335
+ raise ValueError("Rust complete batch diff requires incomplete (object or null)")
1336
+ return payload
1337
+
1338
+
1486
1339
  def _stamp_certified_rust_diff(metadata: dict[str, Any]) -> dict[str, Any]:
1487
- """Stamp the public ownership contract for accepted Rust-finalized diffs."""
1340
+ """Stamp accepted Rust provenance, never overriding contradictory ownership."""
1488
1341
 
1489
1342
  stamped = dict(metadata)
1343
+ owner = stamped.get("engine_owner")
1344
+ if owner is not None and owner != "rust":
1345
+ raise ValueError(f"Rust complete batch diff contradicts engine ownership: {owner!r}")
1490
1346
  stamped.setdefault("engine_owner", "rust")
1491
1347
  stamped.setdefault("semantic_contract", "rust_finalized_v1")
1492
1348
  return stamped
@@ -1507,8 +1363,6 @@ def try_rust_core_batch_diffs(
1507
1363
  python_parser_backend: str = "native",
1508
1364
  ) -> RustCoreBatchDiffsAttempt:
1509
1365
  """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
1366
  if config.diagnostics:
1513
1367
  return RustCoreBatchDiffsAttempt(
1514
1368
  fallback_reason="diagnostics require Python pipeline"
@@ -1532,6 +1386,9 @@ def try_rust_core_batch_diffs(
1532
1386
 
1533
1387
  try:
1534
1388
  backend = _load_backend()
1389
+ except (RuntimeError, OSError):
1390
+ # Missing Rust runtime is not an explicit Rust decline.
1391
+ raise
1535
1392
  except Exception as exc: # noqa: BLE001 - optional accelerator must never break diffing.
1536
1393
  logger.debug("Rust core unavailable: %s", exc)
1537
1394
  return RustCoreBatchDiffsAttempt(fallback_reason=f"unavailable: {exc}")
@@ -1550,7 +1407,6 @@ def try_rust_core_batch_diffs(
1550
1407
  backend_version=backend_version,
1551
1408
  )
1552
1409
  phase_started = time.perf_counter()
1553
- normalized_files = _canonicalize_file_plugin_ids(files)
1554
1410
  request = {
1555
1411
  "schema_version": 1,
1556
1412
  "mode": "commit_batch",
@@ -1558,7 +1414,7 @@ def try_rust_core_batch_diffs(
1558
1414
  "config": config.model_dump(mode="json"),
1559
1415
  "parallel": parallel,
1560
1416
  "max_workers": max_workers,
1561
- "files": normalized_files,
1417
+ "files": files,
1562
1418
  }
1563
1419
  _record_adapter_phase("rust_core_request_construction", phase_started)
1564
1420
  phase_started = time.perf_counter()
@@ -1576,34 +1432,75 @@ def try_rust_core_batch_diffs(
1576
1432
  _record_adapter_phase("rust_core_response_validation", phase_started)
1577
1433
  return RustCoreBatchDiffsAttempt(
1578
1434
  fallback_reason=f"unexpected engine: {engine or '<missing>'}",
1435
+ invalid_output=True,
1579
1436
  adapter_phase_timings=adapter_phase_timings,
1580
1437
  backend_version=backend_version,
1581
1438
  )
1439
+ # Rust's batch contract has exactly four outer statuses. A malformed
1440
+ # envelope is not a legitimate Rust decline, even if a later Rust
1441
+ # finalizer could produce an otherwise valid result.
1442
+ batch_status = data.get("status")
1443
+ if type(batch_status) is not str or batch_status not in (
1444
+ _COMPLETE_STATUS, "candidate", "partial", "fallback"
1445
+ ):
1446
+ raise ValueError(f"Rust batch response status is invalid: {batch_status!r}")
1582
1447
  diff_items = data.get("diffs")
1583
1448
  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,
1449
+ # Core's C ABI emits an explicit item inventory for every declared
1450
+ # batch status, even when all files are Rust-declined. Omitting the
1451
+ # array is corrupted transport, not a valid fallback.
1452
+ raise ValueError("Rust batch response requires diffs array")
1453
+ # Rust always emits one indexed result per requested file, including
1454
+ # explicit candidate/fallback outcomes. A missing record is corrupted
1455
+ # transport, not an implicit Rust decline (or an empty success).
1456
+ if len(diff_items) != len(files):
1457
+ raise ValueError(
1458
+ f"Rust batch response has {len(diff_items)} items "
1459
+ f"for {len(files)} requested files"
1590
1460
  )
1591
1461
  batch_metadata = dict(data.get("metadata") or {})
1592
1462
  fallback_reasons: dict[int, str] = {}
1593
1463
  validated_diffs: list[tuple[int, str, SemanticDiff]] = []
1594
- for fallback_index, item in enumerate(diff_items):
1464
+ seen_indices: set[int] = set()
1465
+ for item in diff_items:
1595
1466
  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:
1467
+ # Rust never emits a non-object item; interpreting one as a
1468
+ # normal decline hides invalid native success behind rerouting.
1469
+ raise ValueError("Rust batch diff item must be an object")
1470
+ # Rust always sends an explicit file index, including on declines.
1471
+ # Inferring a missing index from response order can certify a diff
1472
+ # for the wrong requested file after transport damage.
1473
+ if "index" not in item:
1474
+ raise ValueError("Rust batch response item requires index")
1475
+ index = item["index"]
1476
+ if type(index) is not int or not 0 <= index < len(files):
1477
+ raise ValueError(f"Rust batch response index is invalid: {index!r}")
1478
+ if index in seen_indices:
1479
+ raise ValueError(f"Rust batch response repeats file index {index}")
1480
+ seen_indices.add(index)
1481
+ status = item.get("status")
1482
+ # Candidate is a real, explicitly uncertified Rust benchmark
1483
+ # outcome; both it and fallback may route to Rust finalization.
1484
+ if type(status) is not str or status not in (
1485
+ _COMPLETE_STATUS, "candidate", "fallback"
1486
+ ):
1487
+ raise ValueError(f"Rust batch item status is invalid: {status!r}")
1488
+ if (batch_status == _COMPLETE_STATUS and status != _COMPLETE_STATUS) or (
1489
+ batch_status == "fallback" and status != "fallback"
1490
+ ):
1491
+ raise ValueError(
1492
+ f"Rust batch envelope status {batch_status!r} "
1493
+ f"contradicts item status {status!r}"
1494
+ )
1495
+ if status == _COMPLETE_STATUS and "diff" not in item:
1496
+ raise ValueError("Rust complete batch item requires diff")
1497
+ if status != _COMPLETE_STATUS:
1601
1498
  fallback_reasons[index] = str(
1602
1499
  item.get("reason") or status or "batch item is incomplete"
1603
1500
  )
1604
1501
  continue
1605
1502
  diff = _normalize_builtin_change_type_enums(
1606
- SemanticDiff.model_validate(item["diff"])
1503
+ SemanticDiff.model_validate(_require_complete_batch_diff_dto(item["diff"]))
1607
1504
  )
1608
1505
  validated_diffs.append((index, status, diff))
1609
1506
  _record_adapter_phase("rust_core_response_validation", phase_started)
@@ -1650,660 +1547,110 @@ def try_rust_core_batch_diffs(
1650
1547
  adapter_phase_timings=adapter_phase_timings,
1651
1548
  backend_version=backend_version,
1652
1549
  )
1653
- except (ValidationError, ValueError, TypeError) as exc:
1654
- logger.debug("Rust core batch output rejected: %s", exc)
1550
+ except _InvalidNativeCAbiEnvelope as exc:
1551
+ logger.debug("Rust core batch ABI integrity failure: %s", exc)
1655
1552
  return RustCoreBatchDiffsAttempt(
1656
- fallback_reason=f"invalid output: {exc}",
1553
+ fallback_reason=f"invalid native transport: {exc}",
1554
+ invalid_output=True,
1657
1555
  adapter_phase_timings=adapter_phase_timings,
1658
1556
  )
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)
1557
+ except _NativeCAbiOperationFailure as exc:
1558
+ # A raised Rust error is not a Rust response with status="fallback".
1559
+ # The public batch and commit callers must reject this result rather
1560
+ # than executing another route and publishing manufactured success.
1561
+ logger.debug("Rust core batch operation failed: %s", exc)
1661
1562
  return RustCoreBatchDiffsAttempt(
1662
- fallback_reason=f"failed: {exc}",
1563
+ fallback_reason=f"native operation failed: {exc}",
1564
+ invalid_output=True,
1663
1565
  adapter_phase_timings=adapter_phase_timings,
1664
1566
  )
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
1567
  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(
1568
+ logger.debug("Rust core batch output rejected: %s", exc)
1569
+ return RustCoreBatchDiffsAttempt(
1962
1570
  fallback_reason=f"invalid output: {exc}",
1571
+ invalid_output=True,
1963
1572
  adapter_phase_timings=adapter_phase_timings,
1964
1573
  )
1574
+ except (RuntimeError, OSError):
1575
+ # Rust failures cannot be treated as successful alternate routes.
1576
+ raise
1965
1577
  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(
1578
+ logger.debug("Rust core batch path failed: %s", exc, exc_info=True)
1579
+ return RustCoreBatchDiffsAttempt(
1968
1580
  fallback_reason=f"failed: {exc}",
1969
1581
  adapter_phase_timings=adapter_phase_timings,
1970
1582
  )
1971
1583
 
1972
1584
 
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
-
1585
+ class _InvalidNativeCommitDto(RuntimeError):
1586
+ """Native claimed complete output but sent an invalid public commit DTO."""
2112
1587
 
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
1588
 
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}")
1589
+ def _require_native_commit_diff_json(payload: bytes, operation: str) -> None:
1590
+ """Validate Rust-authored commit bytes before exposing a successful adapter attempt."""
1591
+ from intentumdiff.core.models import CommitDiff
2140
1592
 
2141
1593
  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,
1594
+ # Reject malformed Rust-authored semantic flags *before* Pydantic's
1595
+ # non-strict bool fields can coerce strings or integers (#201).
1596
+ # Both certified commit adapters return the original JSON bytes, so
1597
+ # validating the normalized model alone cannot certify those bytes.
1598
+ raw = json.loads(payload)
1599
+ if not isinstance(raw, dict) or not isinstance(raw.get("file_diffs"), list):
1600
+ raise ValueError("Rust commit JSON requires a file_diffs array")
1601
+ for index, file_diff in enumerate(raw["file_diffs"]):
1602
+ if not isinstance(file_diff, dict):
1603
+ raise ValueError(f"Rust commit JSON file_diffs[{index}] must be an object")
1604
+ metadata = file_diff.get("metadata")
1605
+ if not isinstance(metadata, dict):
1606
+ raise ValueError(
1607
+ f"Rust commit JSON file_diffs[{index}] requires metadata object"
1608
+ )
1609
+ if metadata.get("engine_owner") != "rust":
1610
+ raise ValueError(
1611
+ f"Rust commit JSON file_diffs[{index}] requires Rust-owned metadata"
1612
+ )
1613
+ for flag in ("has_semantic_changes", "is_style_only", "is_fallback"):
1614
+ if type(file_diff.get(flag)) is not bool:
1615
+ raise ValueError(
1616
+ f"Rust commit JSON file_diffs[{index}] requires {flag} boolean"
1617
+ )
1618
+ # Reject truncated Rust change evidence before Pydantic normalises it.
1619
+ _require_source_anchored_changes(
1620
+ file_diff.get("changes"), f"Rust commit JSON file_diffs[{index}]"
2185
1621
  )
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",
1622
+ result = CommitDiff.model_validate_json(payload)
1623
+ # A successful Rust commit must explicitly carry its file inventory.
1624
+ # Pydantic otherwise supplies [] for omitted arrays, manufacturing an
1625
+ # empty successful review from a truncated native response (#199).
1626
+ mandatory_arrays = {
1627
+ "file_diffs", "cross_file_changes", "parse_errors",
1628
+ "guardrail_violations",
2197
1629
  }
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"),
1630
+ omitted = mandatory_arrays - result.model_fields_set
1631
+ if omitted:
1632
+ raise ValueError(f"Rust commit JSON omitted required arrays: {sorted(omitted)}")
1633
+ # A valid top-level file_diffs array does not certify the nested files:
1634
+ # SemanticDiff has default empty arrays and false flags. Without this
1635
+ # check a truncated Rust per-file result becomes a successful no-change
1636
+ # diff when Pydantic fills those defaults (#200).
1637
+ mandatory_file_fields = {
1638
+ "changes", "change_groups", "parse_errors", "guardrail_violations",
1639
+ "has_semantic_changes", "is_style_only", "is_fallback", "metadata",
2291
1640
  }
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
- )
1641
+ for index, file_diff in enumerate(result.file_diffs):
1642
+ omitted_file_fields = mandatory_file_fields - file_diff.model_fields_set
1643
+ if omitted_file_fields:
1644
+ raise ValueError(
1645
+ f"Rust commit JSON file_diffs[{index}] omitted required fields: "
1646
+ f"{sorted(omitted_file_fields)}"
1647
+ )
1648
+ if file_diff.metadata.get("engine_owner") != "rust":
1649
+ raise ValueError(
1650
+ f"Rust commit JSON file_diffs[{index}] lacks certified Rust ownership"
1651
+ )
2301
1652
  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}")
1653
+ raise _InvalidNativeCommitDto(f"Rust {operation} returned malformed CommitDiff JSON: {exc}") from exc
2307
1654
 
2308
1655
 
2309
1656
  def _load_tree_payload(payload: Any) -> dict[str, Any]:
@@ -2318,53 +1665,3 @@ def _load_tree_payload(payload: Any) -> dict[str, Any]:
2318
1665
  return data
2319
1666
 
2320
1667
 
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