intentumdiff-python 0.0.1__py3-none-win_amd64.whl → 0.0.2b2__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 (139) hide show
  1. intentumdiff/__init__.py +2 -0
  2. intentumdiff/__main__.py +12 -0
  3. intentumdiff/_differ_presentation.py +35 -682
  4. intentumdiff/_differ_runtime.py +13 -47
  5. intentumdiff/analysis/compile_commands.py +26 -141
  6. intentumdiff/analysis/cross_file.py +2 -4
  7. intentumdiff/analysis/guardrail_reports.py +7 -5
  8. intentumdiff/analysis/guardrails.py +110 -122
  9. intentumdiff/analysis/invariances.py +4 -1003
  10. intentumdiff/analysis/schema_resolver.py +49 -273
  11. intentumdiff/analysis/text_review.py +4 -3
  12. intentumdiff/analysis/user_schemas.py +55 -237
  13. intentumdiff/api.py +21 -0
  14. intentumdiff/cache/sqlite_store.py +5 -23
  15. intentumdiff/cli/__init__.py +59 -0
  16. intentumdiff/cli/_commands.py +174 -67
  17. intentumdiff/cli/_parser.py +11 -1
  18. intentumdiff/cli/_shared.py +96 -112
  19. intentumdiff/content_type.py +13 -30
  20. intentumdiff/core/commit_differ.py +105 -220
  21. intentumdiff/core/diffignore.py +28 -34
  22. intentumdiff/core/index.py +16 -18
  23. intentumdiff/core/indexer.py +148 -168
  24. intentumdiff/core/models.py +53 -14
  25. intentumdiff/differ.py +278 -605
  26. intentumdiff/github_app.py +35 -19
  27. intentumdiff/intentumdiff_rust_core/intentumdiff_rust_core.dll +0 -0
  28. intentumdiff/live_server.py +180 -17
  29. intentumdiff/lsp/client.py +7 -1
  30. intentumdiff/lsp/enricher.py +19 -119
  31. intentumdiff/lsp/exceptions.py +6 -7
  32. intentumdiff/lsp_server/_codelens.py +24 -57
  33. intentumdiff/lsp_server/_diagnostics.py +23 -51
  34. intentumdiff/lsp_server/_handlers.py +66 -8
  35. intentumdiff/lsp_server/server.py +28 -7
  36. intentumdiff/plugins/adapter.py +5 -0
  37. intentumdiff/plugins/builtins.py +7 -14
  38. intentumdiff/plugins/exceptions.py +4 -0
  39. intentumdiff/plugins/hub.py +1 -2
  40. intentumdiff/plugins/loader.py +229 -215
  41. intentumdiff/plugins/registry.py +249 -197
  42. intentumdiff/rust_core.py +744 -784
  43. intentumdiff/serve/_app.py +3 -10
  44. intentumdiff/sources/base.py +24 -0
  45. intentumdiff/sources/file_source.py +15 -2
  46. intentumdiff/sources/git_source.py +120 -58
  47. intentumdiff/sources/live_buffer_source.py +45 -5
  48. intentumdiff/sources/patch_source.py +36 -109
  49. intentumdiff/vcs/_safe_path.py +7 -0
  50. intentumdiff/vcs/base.py +6 -0
  51. intentumdiff/vcs/git_backend.py +1 -1
  52. intentumdiff/vcs/git_cli.py +38 -18
  53. intentumdiff/vcs/hg_backend.py +1 -1
  54. intentumdiff/vcs/perforce_backend.py +7 -1
  55. intentumdiff/vcs/svn_backend.py +1 -1
  56. intentumdiff/wasm/abap_parser.wasm +0 -0
  57. intentumdiff/wasm/adf_parser.wasm +0 -0
  58. intentumdiff/wasm/asciidoc_parser.wasm +0 -0
  59. intentumdiff/wasm/asm_parser.wasm +0 -0
  60. intentumdiff/wasm/assemblyscript_parser.wasm +0 -0
  61. intentumdiff/wasm/astro_parser.wasm +0 -0
  62. intentumdiff/wasm/bash_parser.wasm +0 -0
  63. intentumdiff/wasm/clojure_parser.wasm +0 -0
  64. intentumdiff/wasm/cmake_parser.wasm +0 -0
  65. intentumdiff/wasm/cpp_parser.wasm +0 -0
  66. intentumdiff/wasm/csharp_parser.wasm +0 -0
  67. intentumdiff/wasm/css_parser.wasm +0 -0
  68. intentumdiff/wasm/dart_parser.wasm +0 -0
  69. intentumdiff/wasm/databricks_parser.wasm +0 -0
  70. intentumdiff/wasm/dax_parser.wasm +0 -0
  71. intentumdiff/wasm/delphi_parser.wasm +0 -0
  72. intentumdiff/wasm/dockerfile_parser.wasm +0 -0
  73. intentumdiff/wasm/elixir_parser.wasm +0 -0
  74. intentumdiff/wasm/freebasic_parser.wasm +0 -0
  75. intentumdiff/wasm/generic_parser.wasm +0 -0
  76. intentumdiff/wasm/gitignore_parser.wasm +0 -0
  77. intentumdiff/wasm/go_parser.wasm +0 -0
  78. intentumdiff/wasm/gomod_parser.wasm +0 -0
  79. intentumdiff/wasm/graphql_parser.wasm +0 -0
  80. intentumdiff/wasm/groovy_parser.wasm +0 -0
  81. intentumdiff/wasm/haskell_parser.wasm +0 -0
  82. intentumdiff/wasm/html_parser.wasm +0 -0
  83. intentumdiff/wasm/html_renderer.wasm +0 -0
  84. intentumdiff/wasm/ini_parser.wasm +0 -0
  85. intentumdiff/wasm/java_parser.wasm +0 -0
  86. intentumdiff/wasm/js_ts_parser.wasm +0 -0
  87. intentumdiff/wasm/json_parser.wasm +0 -0
  88. intentumdiff/wasm/kotlin_parser.wasm +0 -0
  89. intentumdiff/wasm/latex_parser.wasm +0 -0
  90. intentumdiff/wasm/llm_renderer.wasm +0 -0
  91. intentumdiff/wasm/lua_parser.wasm +0 -0
  92. intentumdiff/wasm/make_parser.wasm +0 -0
  93. intentumdiff/wasm/markdown_parser.wasm +0 -0
  94. intentumdiff/wasm/mdx_parser.wasm +0 -0
  95. intentumdiff/wasm/ocaml_parser.wasm +0 -0
  96. intentumdiff/wasm/odin_parser.wasm +0 -0
  97. intentumdiff/wasm/parser_manifest.json +81 -2
  98. intentumdiff/wasm/patch_renderer.wasm +0 -0
  99. intentumdiff/wasm/perl_parser.wasm +0 -0
  100. intentumdiff/wasm/php_parser.wasm +0 -0
  101. intentumdiff/wasm/plsql_parser.wasm +0 -0
  102. intentumdiff/wasm/po_parser.wasm +0 -0
  103. intentumdiff/wasm/postscript_parser.wasm +0 -0
  104. intentumdiff/wasm/powershell_parser.wasm +0 -0
  105. intentumdiff/wasm/proto_parser.wasm +0 -0
  106. intentumdiff/wasm/puppet_parser.wasm +0 -0
  107. intentumdiff/wasm/python_parser.wasm +0 -0
  108. intentumdiff/wasm/qsharp_parser.wasm +0 -0
  109. intentumdiff/wasm/r_parser.wasm +0 -0
  110. intentumdiff/wasm/reasonml_parser.wasm +0 -0
  111. intentumdiff/wasm/ruby_parser.wasm +0 -0
  112. intentumdiff/wasm/rust_parser.wasm +0 -0
  113. intentumdiff/wasm/sas_parser.wasm +0 -0
  114. intentumdiff/wasm/scala_parser.wasm +0 -0
  115. intentumdiff/wasm/scss_parser.wasm +0 -0
  116. intentumdiff/wasm/sql_parser.wasm +0 -0
  117. intentumdiff/wasm/squirrel_parser.wasm +0 -0
  118. intentumdiff/wasm/svelte_parser.wasm +0 -0
  119. intentumdiff/wasm/swift_parser.wasm +0 -0
  120. intentumdiff/wasm/terminal_renderer.wasm +0 -0
  121. intentumdiff/wasm/terraform_parser.wasm +0 -0
  122. intentumdiff/wasm/toml_parser.wasm +0 -0
  123. intentumdiff/wasm/tsql_parser.wasm +0 -0
  124. intentumdiff/wasm/vbnet_parser.wasm +0 -0
  125. intentumdiff/wasm/vue_parser.wasm +0 -0
  126. intentumdiff/wasm/wasm_provenance.json +884 -0
  127. intentumdiff/wasm/wat_parser.wasm +0 -0
  128. intentumdiff/wasm/xml_parser.wasm +0 -0
  129. intentumdiff/wasm/yaml_parser.wasm +0 -0
  130. intentumdiff/wasm/zig_parser.wasm +0 -0
  131. intentumdiff_python-0.0.2b2.dist-info/METADATA +156 -0
  132. intentumdiff_python-0.0.2b2.dist-info/RECORD +171 -0
  133. {intentumdiff_python-0.0.1.dist-info → intentumdiff_python-0.0.2b2.dist-info}/WHEEL +1 -1
  134. {intentumdiff_python-0.0.1.dist-info → intentumdiff_python-0.0.2b2.dist-info}/sboms/intentumdiff-rust-core.cyclonedx.json +905 -379
  135. intentumdiff/core/cst_serializer.py +0 -101
  136. intentumdiff_python-0.0.1.dist-info/METADATA +0 -112
  137. intentumdiff_python-0.0.1.dist-info/RECORD +0 -165
  138. {intentumdiff_python-0.0.1.dist-info → intentumdiff_python-0.0.2b2.dist-info}/entry_points.txt +0 -0
  139. {intentumdiff_python-0.0.1.dist-info → intentumdiff_python-0.0.2b2.dist-info}/licenses/LICENSE +0 -0
intentumdiff/rust_core.py CHANGED
@@ -1,10 +1,7 @@
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
@@ -25,8 +22,6 @@ from intentumdiff.core.models import (
25
22
  ChangeGroup,
26
23
  ChangeType,
27
24
  DiffConfig,
28
- Match,
29
- Matching,
30
25
  SemanticDiff,
31
26
  SemanticNode,
32
27
  )
@@ -46,16 +41,38 @@ _PYTHON_PLUGIN_IDS = frozenset(
46
41
  _CANONICAL_PYTHON_PLUGIN_ID,
47
42
  }
48
43
  )
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"
44
+ #: Distributions that ship the FIRST-PARTY Python parser. The plugin id is qualified by the
45
+ #: distribution name, and this package is published as ``intentumdiff-python`` while the import
46
+ #: package is ``intentumdiff`` — so the same certified parser is spelled two ways depending on
47
+ #: how it was catalogued. Deliberately an allowlist, not a prefix match: canonicalising an
48
+ #: arbitrary ``<dist>:python:python`` would let a third-party plugin claim the certified path.
49
+ _PYTHON_PLUGIN_DISTRIBUTIONS = frozenset(
50
+ {"intentumdiff", "intentumdiff-python", "intentumdiff_python"}
51
+ )
56
52
 
57
53
 
58
54
  def _canonical_python_plugin_id(parser_plugin_id: str | None) -> str | None:
55
+ """Normalise a first-party Python parser id to the certified spelling.
56
+
57
+ This was a no-op returning its argument, which meant the certified batch path rejected
58
+ the parser as an "unsupported parser plugin" and silently fell through to routed
59
+ finalize. The diff was still correct and still Rust — so no gate fired and nothing looked
60
+ broken — but the certified path was never taken, and the facts that only it derives went
61
+ missing.
62
+
63
+ Invisible until parser plugins could be catalogued at all: before that the id was absent
64
+ and the ``is not None`` guard let everything through.
65
+ """
66
+ if not parser_plugin_id:
67
+ return parser_plugin_id
68
+ parts = parser_plugin_id.split(":")
69
+ if (
70
+ len(parts) == 3
71
+ and parts[0] in _PYTHON_PLUGIN_DISTRIBUTIONS
72
+ and parts[1] == "python"
73
+ and parts[2] == "python"
74
+ ):
75
+ return _CANONICAL_PYTHON_PLUGIN_ID
59
76
  return parser_plugin_id
60
77
 
61
78
 
@@ -71,56 +88,12 @@ def _canonicalize_file_plugin_ids(files: list[dict[str, Any]]) -> list[dict[str,
71
88
  return normalized
72
89
 
73
90
 
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
91
  @dataclass(frozen=True)
120
92
  class RustCoreBatchAttempt:
121
93
  diff: SemanticDiff | None = None
122
94
  fallback_reason: str = ""
123
95
  backend_version: str = ""
96
+ invalid_output: bool = False # An invalid claimed-success Rust DTO is never a normal decline.
124
97
 
125
98
  @property
126
99
  def used(self) -> bool:
@@ -135,6 +108,7 @@ class RustCoreBatchDiffsAttempt:
135
108
  adapter_phase_timings: list[dict[str, Any]] = field(default_factory=list)
136
109
  backend_version: str = ""
137
110
  fallback_reason: str = ""
111
+ invalid_output: bool = False
138
112
 
139
113
  @property
140
114
  def used(self) -> bool:
@@ -182,71 +156,6 @@ def rust_core_enabled(config: DiffConfig) -> bool:
182
156
  return bool(config.experimental_rust_core)
183
157
 
184
158
 
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
159
  def _pyo3_extension_module_name() -> str | None:
251
160
  """Return the importable name of the compiled PyO3 extension, or ``None``.
252
161
 
@@ -277,23 +186,12 @@ def _pyo3_extension_module_name() -> str | None:
277
186
 
278
187
 
279
188
  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)")
189
+ # Maturin packages a plain C-ABI cdylib. A .so suffix does not make it
190
+ # a Python extension; importing it would require a nonexistent PyInit export.
294
191
  return _ctypes_backend()
295
192
 
296
193
 
194
+
297
195
  # ── Stateless C-ABI accessor (intentumdiff_call) ───────────────────────────────────────────────
298
196
  # The compiled core exports the language-agnostic C ABI (`intentumdiff_call`/`intentumdiff_free`,
299
197
  # ungated) alongside the PyO3 module, so the SAME artifact serves both. Handlers reachable ONLY
@@ -364,6 +262,31 @@ def _c_abi_lib() -> Any:
364
262
  return _C_ABI_LIB
365
263
 
366
264
 
265
+ class _InvalidNativeCAbiEnvelope(RuntimeError):
266
+ """Corrupted C ABI response: a different Rust route must not mask the failure."""
267
+
268
+
269
+ class _NativeCAbiOperationFailure(Exception):
270
+ """Rust returned an operation error, not a Rust-controlled fallback status.
271
+
272
+ This marker preserves each historical Python exception category via the
273
+ subclasses below while preventing the adapter from mistaking raised native
274
+ failures for an explicit status="fallback" result (#219).
275
+ """
276
+
277
+
278
+ class _NativeCAbiNotFoundError(FileNotFoundError, _NativeCAbiOperationFailure):
279
+ """Native not_found error with its existing FileNotFoundError contract."""
280
+
281
+
282
+ class _NativeCAbiValueError(ValueError, _NativeCAbiOperationFailure):
283
+ """Native input error with its existing ValueError contract."""
284
+
285
+
286
+ class _NativeCAbiInternalError(RuntimeError, _NativeCAbiOperationFailure):
287
+ """Native internal/panic error with its existing RuntimeError contract."""
288
+
289
+
367
290
  def _c_abi_call(name: str, *args: Any) -> Any:
368
291
  """Invoke a C-ABI handler by name; return its ``result`` or re-raise the classified error.
369
292
 
@@ -381,21 +304,47 @@ def _c_abi_call(name: str, *args: Any) -> Any:
381
304
  name.encode("utf-8"), json.dumps(payload).encode("utf-8")
382
305
  )
383
306
  if not ptr:
384
- raise RuntimeError(f"intentumdiff_call({name!r}) returned NULL (allocation failure)")
307
+ raise _InvalidNativeCAbiEnvelope(
308
+ f"intentumdiff_call({name!r}) returned NULL (allocation failure)"
309
+ )
385
310
  try:
386
311
  raw = ctypes.cast(ptr, ctypes.c_char_p).value
387
312
  finally:
388
313
  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")
314
+ try:
315
+ envelope = json.loads((raw or b"").decode("utf-8"))
316
+ except (UnicodeDecodeError, ValueError, TypeError) as exc:
317
+ raise _InvalidNativeCAbiEnvelope(
318
+ f"invalid C ABI envelope for {name!r}: malformed JSON: {exc}"
319
+ ) from exc
320
+ # Only the Rust ABI can certify success. A malformed envelope must not
321
+ # manufacture a None (or other truthy) result for a successful operation.
322
+ if not isinstance(envelope, dict) or type(envelope.get("ok")) is not bool:
323
+ raise _InvalidNativeCAbiEnvelope(
324
+ f"invalid C ABI envelope for {name!r}: expected a boolean 'ok'"
325
+ )
326
+ if envelope["ok"]:
327
+ if "result" not in envelope:
328
+ raise _InvalidNativeCAbiEnvelope(
329
+ f"invalid C ABI envelope for {name!r}: success missing 'result'"
330
+ )
331
+ return envelope["result"]
332
+ # The current Rust Core C ABI always emits both fields on a decline.
333
+ # Missing or incorrectly typed errors are corrupted transport, not an
334
+ # explicit decline that Python may route around.
335
+ message = envelope.get("error")
336
+ kind = envelope.get("error_type")
337
+ if not isinstance(message, str) or not isinstance(kind, str) or kind not in (
338
+ "not_found", "internal", "value_error", "bad_request"
339
+ ):
340
+ raise _InvalidNativeCAbiEnvelope(
341
+ f"invalid C ABI envelope for {name!r}: malformed error fields"
342
+ )
394
343
  if kind == "not_found":
395
- raise FileNotFoundError(message)
344
+ raise _NativeCAbiNotFoundError(message)
396
345
  if kind == "internal":
397
- raise RuntimeError(message)
398
- raise ValueError(message)
346
+ raise _NativeCAbiInternalError(message)
347
+ raise _NativeCAbiValueError(message)
399
348
 
400
349
 
401
350
  class _CtypesBackend:
@@ -439,9 +388,24 @@ class _CtypesBackend:
439
388
  if name in self._COMMIT_TUPLE:
440
389
  def _commit(request_json: str, _name: str = name) -> tuple[str, bytes | None]:
441
390
  envelope = _c_abi_call(_name, request_json)
391
+ # A malformed claimed-success C ABI result must not raise a
392
+ # generic AttributeError here: the commit adapters would treat
393
+ # it as a normal backend failure and silently reroute (#207).
394
+ # Check transport types before encoding; Rust owns the control
395
+ # status and CommitDiff semantics, which the callers validate.
396
+ if not isinstance(envelope, dict) or not isinstance(
397
+ envelope.get("control"), dict
398
+ ):
399
+ raise _InvalidNativeCommitDto(
400
+ f"Rust {_name} returned malformed C ABI commit envelope"
401
+ )
442
402
  payload = envelope.get("commit_diff_json")
403
+ if payload is not None and not isinstance(payload, str):
404
+ raise _InvalidNativeCommitDto(
405
+ f"Rust {_name} returned non-string commit_diff_json in C ABI envelope"
406
+ )
443
407
  return (
444
- json.dumps(envelope.get("control")),
408
+ json.dumps(envelope["control"]),
445
409
  payload.encode("utf-8") if payload is not None else None,
446
410
  )
447
411
 
@@ -480,22 +444,20 @@ def _ctypes_backend() -> _CtypesBackend:
480
444
 
481
445
 
482
446
  def enrich_node_facts(tree: SemanticNode) -> SemanticNode:
483
- """Fill language-agnostic NodeFacts (issue #70) on a Wasm-parsed tree via the Rust core.
447
+ """Request authoritative language-agnostic NodeFacts enrichment from Rust.
484
448
 
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.
449
+ Rust supplies structural facts for every shipped parser. Returning the input
450
+ tree after an engine error can silently lose intent evidence, so the 0.0.2
451
+ boundary rejects missing handlers and malformed responses instead of
452
+ manufacturing successful un-enriched output. A valid no-op is supported.
490
453
  """
491
454
  try:
492
- backend = _load_backend()
493
- enrich = getattr(backend, "enrich_node_facts_json", None)
494
- if enrich is None:
495
- return tree
455
+ enrich = getattr(_load_backend(), "enrich_node_facts_json", None)
456
+ if not callable(enrich):
457
+ raise RuntimeError("required Rust node facts handler is unavailable")
496
458
  return SemanticNode.model_validate_json(enrich(tree.model_dump_json()))
497
- except Exception: # noqa: BLE001 — optional enrichment must degrade gracefully
498
- return tree
459
+ except Exception as exc: # noqa: BLE001 - native failures must never become success.
460
+ raise RuntimeError(f"Rust node facts enrichment failed: {exc}") from exc
499
461
 
500
462
 
501
463
  def apply_invariances(
@@ -593,7 +555,17 @@ def build_scope_trails(
593
555
  raise RuntimeError(f"Rust scope trail engine unavailable: {exc}") from exc
594
556
 
595
557
  scope_trails = data.get("scope_trails") if isinstance(data, dict) else None
596
- return dict(scope_trails) if isinstance(scope_trails, dict) else {}
558
+ if not isinstance(scope_trails, dict):
559
+ raise RuntimeError("Rust scope trail response requires scope_trails object")
560
+ if any(not isinstance(scope_trails.get(side), list) for side in ("old", "new")):
561
+ raise RuntimeError("Rust scope trail response requires old/new arrays")
562
+ if any(
563
+ not isinstance(entry, dict)
564
+ for side in ("old", "new")
565
+ for entry in scope_trails[side]
566
+ ):
567
+ raise RuntimeError("Rust scope trail response contains non-object entry")
568
+ return dict(scope_trails)
597
569
 
598
570
 
599
571
  def try_rust_generic_text_review(
@@ -601,36 +573,46 @@ def try_rust_generic_text_review(
601
573
  new_source: str,
602
574
  raw_change_count: int,
603
575
  ) -> tuple[list[Change], list[ChangeGroup]] | None:
604
- """Run the Rust generic-text review stage; None means fall back to Python.
576
+ """Decode Rust-owned generic text review or an explicit Rust size-cap decline.
605
577
 
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.
578
+ Only Rust may decline this stage. Missing handlers, invalid envelopes and
579
+ engine failures cannot silently become successful Python pass-throughs.
609
580
  """
610
581
  try:
611
582
  backend = _load_backend()
612
583
  review_fn = getattr(backend, "generic_text_review_json", None)
613
584
  if not callable(review_fn):
614
- return None
585
+ raise RuntimeError("required Rust generic-text review handler is unavailable")
615
586
  data = json.loads(review_fn(old_source, new_source, raw_change_count))
616
- if not isinstance(data, dict) or not data.get("used"):
587
+ if not isinstance(data, dict):
588
+ raise ValueError("Rust generic-text review must return a JSON object")
589
+ used = data.get("used")
590
+ if used is False:
591
+ if data.get("reason") != "input too large for LCS table":
592
+ raise ValueError("uncertified Rust generic-text review decline")
617
593
  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))
594
+ if used is not True:
595
+ raise ValueError("Rust generic-text review missing explicit used=true")
596
+ if not isinstance(data.get("changes"), list):
597
+ raise ValueError("Rust generic-text review missing changes list")
598
+ if "group" not in data:
599
+ raise ValueError("Rust generic-text review missing group field")
600
+ group = data["group"]
601
+ if group is not None and not isinstance(group, dict):
602
+ raise ValueError("Rust generic-text review group must be an object or null")
603
+ changes = [Change.model_validate(item) for item in
604
+ _require_source_anchored_changes(data["changes"], "Rust generic-text review")]
605
+ groups = [ChangeGroup.model_validate(group)] if group is not None else []
623
606
  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
607
+ except Exception as exc: # noqa: BLE001 - fail closed on native/DTO failures.
608
+ raise RuntimeError(f"Rust generic-text review failed: {exc}") from exc
627
609
 
628
610
 
629
611
  def try_rust_markdown_section_review(
630
612
  old_source: str,
631
613
  new_source: str,
632
614
  ) -> dict[str, object] | None:
633
- """Run the Rust markdown section review (moves + heading renames); None -> Python.
615
+ """Run required Rust Markdown section review; malformed native data must fail closed.
634
616
 
635
617
  Engine-side port of the markdown post-presentation rules (issue #36): section
636
618
  identity by content hash, LIS insertion-shift discrimination for moves, and
@@ -640,56 +622,87 @@ def try_rust_markdown_section_review(
640
622
  backend = _load_backend()
641
623
  review_fn = getattr(backend, "markdown_section_review_json", None)
642
624
  if not callable(review_fn):
643
- return None
625
+ raise RuntimeError("required Rust markdown section review handler is unavailable")
644
626
  data = json.loads(review_fn(old_source, new_source))
645
- if not isinstance(data, dict) or not data.get("used"):
646
- return None
627
+ # The Rust markdown_section_review_impl always emits used=true and the
628
+ # complete DTO envelope, including real empty arrays for no-op reviews.
629
+ # Missing fields are engine-contract failures, never a Python decline.
630
+ if not isinstance(data, dict) or data.get("used") is not True:
631
+ raise ValueError("Rust markdown section review requires used=true")
632
+ for key in ("moves", "renames", "moved_labels", "old_heading_lines", "new_heading_lines"):
633
+ if not isinstance(data.get(key), list):
634
+ raise ValueError(f"Rust markdown section review requires {key} array")
635
+ if any(not isinstance(label, str) for label in data["moved_labels"]):
636
+ raise ValueError("Rust markdown section review has invalid moved_labels")
637
+ for key in ("old_heading_lines", "new_heading_lines"):
638
+ if any(type(line) is not int or line < 0 for line in data[key]):
639
+ raise ValueError(f"Rust markdown section review has invalid {key}")
640
+ for key in ("move_group", "rename_group"):
641
+ if key not in data or (data[key] is not None and not isinstance(data[key], dict)):
642
+ raise ValueError(f"Rust markdown section review requires {key} object or null")
647
643
  return {
648
- "moves": [Change.model_validate(item) for item in data.get("moves") or []],
649
- "moved_labels": set(data.get("moved_labels") or []),
644
+ "moves": [Change.model_validate(item) for item in
645
+ _require_source_anchored_changes(data["moves"], "Rust Markdown moves")],
646
+ "moved_labels": set(data["moved_labels"]),
650
647
  "move_group": (
651
- ChangeGroup.model_validate(data["move_group"]) if data.get("move_group") else None
648
+ ChangeGroup.model_validate(data["move_group"])
649
+ if data["move_group"] is not None else None
652
650
  ),
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 []),
651
+ "renames": [Change.model_validate(item) for item in
652
+ _require_source_anchored_changes(data["renames"], "Rust Markdown renames")],
653
+ "old_heading_lines": set(data["old_heading_lines"]),
654
+ "new_heading_lines": set(data["new_heading_lines"]),
656
655
  "rename_group": (
657
656
  ChangeGroup.model_validate(data["rename_group"])
658
- if data.get("rename_group")
659
- else None
657
+ if data["rename_group"] is not None else None
660
658
  ),
661
659
  }
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
660
+ except Exception as exc: # noqa: BLE001 - an engine failure is not an unsupported decline.
661
+ raise RuntimeError(f"Rust markdown section review failed: {exc}") from exc
665
662
 
666
663
 
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.
664
+ def try_rust_collect_hover_targets(root: SemanticNode) -> list[tuple[str, int, int]]:
665
+ """Decode Rust-owned hover targets; required engine failures propagate.
671
666
 
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.
667
+ The historical name is retained for compatibility. There is no Python walker.
676
668
  """
669
+ data = _required_engine_json(
670
+ "lsp_collect_hover_targets_json", root.model_dump_json(), result_type=list,
671
+ )
672
+ return _hover_target_dtos(data)
673
+
674
+
675
+ def collect_lsp_hover_targets(root: SemanticNode, source: str) -> list[tuple[str, int, int]]:
676
+ """Rust-owned selection and UTF-8 byte to UTF-16 protocol conversion."""
677
+ data = _required_engine_json(
678
+ "lsp_collect_utf16_hover_targets_json", root.model_dump_json(), source, result_type=list,
679
+ )
680
+ return _hover_target_dtos(data)
681
+
682
+
683
+ def attach_lsp_type_map(root: SemanticNode, type_map: dict[str, str]) -> SemanticNode:
684
+ """Attach host-observed LSP type DTOs using the Rust C ABI, never Python tree mutation."""
685
+ response = _required_engine_json(
686
+ "lsp_attach_type_map_json",
687
+ root.model_dump_json(),
688
+ json.dumps(type_map, sort_keys=True),
689
+ result_type=dict,
690
+ )
677
691
  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
692
+ return SemanticNode.model_validate(response)
693
+ except ValidationError as exc:
694
+ raise RuntimeError("Rust LSP type-map attachment returned invalid tree DTO") from exc
695
+
696
+
697
+ def _hover_target_dtos(data: list[Any]) -> list[tuple[str, int, int]]:
698
+ targets = []
699
+ for item in data:
700
+ if (not isinstance(item, dict) or not isinstance(item.get("id"), str)
701
+ or type(item.get("line")) is not int or type(item.get("col")) is not int
702
+ or item["line"] < 0 or item["col"] < 0):
703
+ raise RuntimeError("Rust hover-target response has an invalid target DTO")
704
+ targets.append((item["id"], item["line"], item["col"]))
705
+ return targets
693
706
 
694
707
 
695
708
  def try_rust_finalize_review(
@@ -705,19 +718,25 @@ def try_rust_finalize_review(
705
718
  """Route the per-stage draft finalization through the Rust core (issue #57).
706
719
 
707
720
  Runs the SAME refine+finalize pipeline as the certified batch from the provided
708
- semantic trees. None -> caller stays on the Python transitional path.
721
+ semantic trees. Only an explicit Rust tree-size guard may decline.
709
722
  """
710
723
  try:
711
724
  backend = _load_backend()
712
725
  finalize_fn = getattr(backend, "finalize_review_json", None)
713
726
  if not callable(finalize_fn):
714
- return None
727
+ raise RuntimeError("required Rust finalize_review_json handler is unavailable")
715
728
  config_json = json.dumps(
716
729
  {
717
730
  "min_height": config.min_height,
718
731
  "min_similarity": config.min_similarity,
719
732
  "max_nodes": config.max_nodes,
720
733
  "collect_trace": collect_trace,
734
+ "fallback_to_token_diff": config.fallback_to_token_diff,
735
+ "ignore_style": config.ignore_style,
736
+ "detect_refactorings": config.detect_refactorings,
737
+ "approx_move_threshold": config.approx_move_threshold,
738
+ "leaf_text_diff": config.leaf_text_diff,
739
+ "extra_trivia_types": config.extra_trivia_types,
721
740
  }
722
741
  )
723
742
  data = json.loads(
@@ -730,29 +749,117 @@ def try_rust_finalize_review(
730
749
  config_json,
731
750
  )
732
751
  )
733
- if not isinstance(data, dict) or not data.get("used"):
752
+ # The authoritative Rust finalize_review_impl emits explicit arrays and
753
+ # booleans on every successful response. Never turn a missing or malformed
754
+ # field into an empty/style-only result: that silently discards engine
755
+ # evidence and can misclassify meaningful edits.
756
+ if not isinstance(data, dict) or type(data.get("used")) is not bool:
757
+ raise ValueError("invalid Rust finalize response: 'used' must be a boolean")
758
+ if not data["used"]:
759
+ # The only explicit Rust decline is the max-nodes guard.
760
+ if data.get("reason") != "tree_too_large":
761
+ raise ValueError("invalid Rust finalize response: unexpected decline")
734
762
  return None
763
+
764
+ for key in ("changes", "change_groups", "ignored_style_changes"):
765
+ if not isinstance(data.get(key), list):
766
+ raise ValueError(f"invalid Rust finalize response: '{key}' must be an array")
767
+ for key in ("is_style_only", "no_surviving_changes"):
768
+ if type(data.get(key)) is not bool:
769
+ raise ValueError(f"invalid Rust finalize response: '{key}' must be a boolean")
770
+
771
+ ignored = data["ignored_style_changes"]
772
+ if any(not isinstance(item, dict) for item in ignored):
773
+ raise ValueError("invalid Rust finalize response: malformed ignored-style item")
774
+ # Trace is optional: Rust omits it for parse-error/source-lifecycle
775
+ # responses, but when present it must be a well-formed array.
776
+ trace = data.get("trace", [])
777
+ if not isinstance(trace, list) or any(not isinstance(item, dict) for item in trace):
778
+ raise ValueError("invalid Rust finalize response: malformed trace")
779
+ fallback_diff = data.get("fallback_diff")
780
+ if fallback_diff is not None and not isinstance(fallback_diff, dict):
781
+ raise ValueError("invalid Rust finalize response: malformed fallback_diff")
782
+
735
783
  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
- ],
784
+ "fallback_diff": (
785
+ SemanticDiff.model_validate(_require_complete_batch_diff_dto(fallback_diff))
786
+ if fallback_diff is not None else None
787
+ ),
788
+ "changes": [Change.model_validate(item) for item in
789
+ _require_source_anchored_changes(data["changes"], "Rust finalize review")],
790
+ "change_groups": [ChangeGroup.model_validate(item) for item in data["change_groups"]],
791
+ "is_style_only": data["is_style_only"],
792
+ "no_surviving_changes": data["no_surviving_changes"],
793
+ "ignored_style_changes": [dict(item) for item in ignored],
794
+ "trace": [dict(item) for item in trace],
752
795
  }
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
796
+ except Exception as exc: # noqa: BLE001 - an engine failure is not an unsupported decline.
797
+ raise RuntimeError(f"Rust finalize review failed: {exc}") from exc
798
+
799
+
800
+ def complete_routed_review(*, finalized: dict[str, Any], old_tree: SemanticNode,
801
+ new_tree: SemanticNode, old_source: str, new_source: str,
802
+ old_filename: str, new_filename: str, language: str,
803
+ schema_metadata: dict[str, Any] | None = None,
804
+ compile_metadata: dict[str, Any] | None = None) -> SemanticDiff:
805
+ """Marshal routed review inputs; the Rust engine owns final meaning and evidence."""
806
+ payload = dict(finalized)
807
+ for field in ("changes", "change_groups"):
808
+ payload[field] = [item.model_dump(mode="json") for item in finalized[field]]
809
+ # Rust owns the final inventories and semantic flags. Pydantic defaults
810
+ # must never turn a truncated claimed-complete Rust response into a
811
+ # successful no-change or style-only result (#208).
812
+ response = _c_abi_call("complete_routed_review", json.dumps({
813
+ "finalized": payload, "old_tree": old_tree.model_dump(mode="json"),
814
+ "new_tree": new_tree.model_dump(mode="json"), "old_source": old_source,
815
+ "new_source": new_source, "old_filename": old_filename, "new_filename": new_filename,
816
+ "language": language, "schema_metadata": schema_metadata, "compile_metadata": compile_metadata,
817
+ }))
818
+ return SemanticDiff.model_validate(_require_complete_batch_diff_dto(response))
819
+
820
+
821
+ def source_fallback_diff(old: str, new: str, old_filename: str, new_filename: str,
822
+ language: str, reason: str = "parse_errors") -> SemanticDiff:
823
+ """Shared source comparison; Python only marshals the C ABI."""
824
+ # Rust owns source fallback classification, inventories, style and spans.
825
+ # Decode before Pydantic to prevent its defaults from hiding a truncated
826
+ # claimed-complete Rust response (#210).
827
+ raw = _load_backend().source_fallback_diff_json(
828
+ old, new, old_filename, new_filename, language, reason
829
+ )
830
+ return SemanticDiff.model_validate(_require_complete_batch_diff_dto(json.loads(raw)))
831
+
832
+
833
+ def _decode_rust_boolean(payload: str, handler: str) -> bool:
834
+ """Validate Rust's boolean DTO without Python truthiness reinterpretation."""
835
+ decoded = json.loads(payload)
836
+ if type(decoded) is not bool:
837
+ raise ValueError(f"Rust {handler} must return a JSON boolean, got {type(decoded).__name__}")
838
+ return decoded
839
+
840
+
841
+ def parse_errors_present(source: str, tree_json: str, language: str) -> bool:
842
+ return _decode_rust_boolean(
843
+ _load_backend().parse_errors_present_json(source, tree_json, language),
844
+ "parse_errors_present",
845
+ )
846
+
847
+
848
+ def enrich_literal_labels(tree: SemanticNode, source: str) -> SemanticNode:
849
+ """Source-label enrichment owned by the shared Rust engine."""
850
+ return SemanticNode.model_validate_json(
851
+ _load_backend().enrich_literal_labels_json(tree.model_dump_json(), source)
852
+ )
853
+
854
+
855
+ def review_trees_equivalent(old_tree: SemanticNode, new_tree: SemanticNode) -> bool:
856
+ """Ask core for conservative review equivalence, including literal whitespace."""
857
+ return _decode_rust_boolean(
858
+ _load_backend().review_trees_equivalent_json(
859
+ old_tree.model_dump_json(), new_tree.model_dump_json()
860
+ ),
861
+ "review_trees_equivalent",
862
+ )
756
863
 
757
864
 
758
865
  def try_rust_profile_label_enrichment(
@@ -760,120 +867,68 @@ def try_rust_profile_label_enrichment(
760
867
  source: str,
761
868
  language: str,
762
869
  identity_fields: tuple[str, ...] = (),
763
- ) -> SemanticNode | None:
764
- """Profile-label enrichment in the Rust core (issue #57 profile-enrichment port).
870
+ ) -> SemanticNode:
871
+ """Request authoritative Rust profile-label enrichment for a supported parser.
765
872
 
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).
873
+ A missing backend handler is a contract failure, not an empty/no-op
874
+ enrichment: silently returning an un-enriched tree can alter matching and
875
+ suppress meaningful changes in the release candidate.
768
876
  """
769
877
  try:
770
878
  backend = _load_backend()
771
879
  enrich_fn = getattr(backend, "enrich_profile_labels_json", None)
772
880
  if not callable(enrich_fn):
773
- return None
881
+ raise RuntimeError("required Rust profile enrichment handler is unavailable")
774
882
  return SemanticNode.model_validate_json(
775
883
  enrich_fn(tree.model_dump_json(), source, language, list(identity_fields))
776
884
  )
777
- except Exception as exc: # noqa: BLE001 - strangler boundary must fall back.
778
- logger.debug("Rust profile enrichment unavailable: %s", exc, exc_info=True)
779
- return None
780
-
885
+ except Exception as exc: # noqa: BLE001 - an engine failure is not an unsupported decline.
886
+ raise RuntimeError(f"Rust profile enrichment failed: {exc}") from exc
781
887
 
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
888
 
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
- """
889
+ def _required_engine_json(handler: str, *args: Any, result_type: type) -> Any:
890
+ """Marshal a required engine operation; failures cannot become empty success."""
791
891
  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
892
+ operation = getattr(_load_backend(), handler, None)
893
+ if not callable(operation):
894
+ raise RuntimeError("required handler is unavailable")
895
+ result = json.loads(operation(*args))
896
+ if isinstance(result, dict) and "error" in result:
897
+ raise RuntimeError(str(result["error"]))
898
+ if not isinstance(result, result_type):
899
+ raise ValueError(f"expected {result_type.__name__} result")
801
900
  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
901
+ except Exception as exc:
902
+ raise RuntimeError(f"Rust engine operation {handler} failed: {exc}") from exc
903
+
904
+
905
+ def render_cli_review(diff: SemanticDiff, *, width: int, color: bool) -> str:
906
+ """Transport an authoritative DTO to Rust's rs-rich presentation renderer."""
907
+ request = {"diff": diff.model_dump(mode="json"), "width": width, "color": color}
908
+ return _required_engine_json("render_cli_review", json.dumps(request), result_type=str)
805
909
 
806
910
 
807
- def try_rust_build_reference_table(files_json: str) -> str | None:
808
- """Reference-table extraction in the Rust core (index-engine-lib, #91).
911
+ def try_rust_build_symbol_table(files_json: str) -> str:
912
+ """Extract a symbol table in Rust, raising on unavailable or invalid results.
809
913
 
810
- Returns the ReferenceTable JSON, or None when the backend is unavailable or
811
- the core reports an error envelope.
914
+ The historical function name is retained for compatibility; failure is never None.
812
915
  """
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
916
+ return json.dumps(_required_engine_json("build_symbol_table_json", files_json, result_type=dict))
827
917
 
828
918
 
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).
919
+ def try_rust_build_reference_table(files_json: str) -> str:
920
+ """Extract a reference table in Rust or raise an actionable engine error."""
921
+ return json.dumps(_required_engine_json("build_reference_table_json", files_json, result_type=dict))
833
922
 
834
- *old_json* / *new_json* are serialised symbol tables. Returns the parsed
835
- list of cross-file change dicts (MOVE_TO_MODULE / SPLIT_MODULE /
836
- CROSS_FILE_RENAME), or None when the backend is unavailable or the core
837
- reports an error envelope.
838
- """
839
- try:
840
- backend = _load_backend()
841
- diff_fn = getattr(backend, "diff_symbol_tables_json", None)
842
- if not callable(diff_fn):
843
- return None
844
- result = json.loads(diff_fn(old_json, new_json))
845
- if isinstance(result, dict) and "error" in result:
846
- logger.debug("Rust diff_symbol_tables error: %s", result["error"])
847
- return None
848
- return result
849
- except Exception as exc: # noqa: BLE001 - engine boundary must degrade, not crash.
850
- logger.debug("Rust diff_symbol_tables unavailable: %s", exc, exc_info=True)
851
- return None
852
923
 
924
+ def try_rust_diff_symbol_tables(old_json: str, new_json: str) -> list[dict[str, Any]]:
925
+ """Compare symbol tables in Rust; an empty list means successful no-change."""
926
+ return _required_engine_json("diff_symbol_tables_json", old_json, new_json, result_type=list)
853
927
 
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
928
 
859
- *request* carries the diff language/filenames, the old/new trees, the diff's
860
- changed-node ids, and the parsed policy rules. The core matches protected paths
861
- against the changes and returns the list of violation dicts. Returns None when
862
- the backend/entrypoint is unavailable or reports an error envelope.
863
- """
864
- try:
865
- backend = _load_backend()
866
- eval_fn = getattr(backend, "evaluate_guardrail_rules_json", None)
867
- if not callable(eval_fn):
868
- return None
869
- result = json.loads(eval_fn(json.dumps(request)))
870
- if isinstance(result, dict) and "error" in result:
871
- logger.debug("Rust evaluate_guardrail_rules error: %s", result["error"])
872
- return None
873
- return result
874
- except Exception as exc: # noqa: BLE001 - engine boundary must degrade, not crash.
875
- logger.debug("Rust guardrail evaluation unavailable: %s", exc, exc_info=True)
876
- return None
929
+ def try_rust_evaluate_guardrail_rules(request: dict[str, Any]) -> list[dict[str, Any]]:
930
+ """Evaluate in Rust; engine failure must never report a passed guardrail."""
931
+ return _required_engine_json("evaluate_guardrail_rules_json", json.dumps(request), result_type=list)
877
932
 
878
933
 
879
934
  def load_project_diff_config_data(
@@ -1010,6 +1065,25 @@ class _RustCacheStore:
1010
1065
  )
1011
1066
  )
1012
1067
 
1068
+ def list_entries_filtered(
1069
+ self,
1070
+ table: str,
1071
+ language: str | None,
1072
+ since: int | None,
1073
+ before: int | None,
1074
+ min_size: int | None,
1075
+ max_size: int | None,
1076
+ limit: int,
1077
+ file_glob: str | None,
1078
+ ) -> str:
1079
+ return json.dumps(
1080
+ _c_abi_call(
1081
+ "cache_list_entries_filtered",
1082
+ self._p, self._t, self._m,
1083
+ table, language, since, before, min_size, max_size, limit, file_glob,
1084
+ )
1085
+ )
1086
+
1013
1087
  def get_entry_metadata(self, key: str, table: str) -> str | None:
1014
1088
  result = _c_abi_call("cache_get_entry_metadata", self._p, self._t, self._m, key, table)
1015
1089
  return json.dumps(result) if result is not None else None
@@ -1228,6 +1302,11 @@ def vcs_backend_resolve_root(vcs: str, repo_path: str) -> str:
1228
1302
  return _load_backend().vcs_backend_resolve_root(vcs, repo_path)
1229
1303
 
1230
1304
 
1305
+ def p4_policy_path(client_root: str, depot_path: str) -> str:
1306
+ """Resolve a depot path's physical policy context in the shared Rust host."""
1307
+ return json.loads(_load_backend().p4_policy_path_json(client_root, depot_path))["path"]
1308
+
1309
+
1231
1310
  def vcs_backend_get_blob(
1232
1311
  vcs: str, repo_path: str, path: str, ref: str, svn_repo_url: str | None = None
1233
1312
  ) -> str:
@@ -1322,11 +1401,12 @@ def live_handle_diff(
1322
1401
  config_json: str,
1323
1402
  wasm_dir: str = "",
1324
1403
  ) -> dict[str, Any]:
1325
- """Native single-file diff (Phase A1/R): `{diff}` (certified) or `{fallback: reason}`.
1404
+ """Native single-file diff (Phase A1/R): certified DTO or decline.
1326
1405
 
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.
1406
+ The Rust handler uses bundled-parser manifests (native Python parser and
1407
+ other languages' Wasm parsers). A fallback marker is NOT a certified
1408
+ semantic diff: live transports must return a typed error, not recover via
1409
+ a second success path.
1330
1410
  """
1331
1411
  return json.loads(
1332
1412
  _load_backend().live_handle_diff_json(
@@ -1338,13 +1418,12 @@ def live_handle_diff(
1338
1418
  def live_handle_review(
1339
1419
  repo_path: str, old_ref: str, new_ref: str, config_json: str, wasm_dir: str = ""
1340
1420
  ) -> dict[str, Any]:
1341
- """Native commit review (Phase A2 + d2-review): `{commit_diff}` or `{fallback: reason}`.
1421
+ """Native commit review (Phase A2 + d2-review): certified DTO or decline.
1342
1422
 
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.
1423
+ Rust resolves changed files via the bundled-parser manifest in *wasm_dir*.
1424
+ Missing or non-certified results are declines, not permission for a live
1425
+ transport to manufacture a successful Python-path review. Older ABI
1426
+ signature compatibility does not relax result certification.
1348
1427
  """
1349
1428
  backend = _load_backend()
1350
1429
  try:
@@ -1355,31 +1434,44 @@ def live_handle_review(
1355
1434
  return json.loads(raw)
1356
1435
 
1357
1436
 
1437
+ def live_handle_asset_diff(
1438
+ repo_path: str, default_ref: str, request: dict[str, Any], seq: int
1439
+ ) -> dict[str, Any]:
1440
+ """Native perceptual asset diff: the whole protocol response for op ``asset_diff``.
1441
+
1442
+ Unlike the other ``live_*`` handlers this returns a finished response envelope — success or
1443
+ error — because a malformed request is an answer to send, not a call that failed. Request
1444
+ parsing, path containment and the artifact cache all live in the core, so the Python server
1445
+ and the native binary answer identically.
1446
+ """
1447
+ return json.loads(
1448
+ _load_backend().live_handle_asset_diff_json(
1449
+ repo_path, default_ref, json.dumps(request, separators=(",", ":")), int(seq)
1450
+ )
1451
+ )
1452
+
1453
+
1358
1454
  _registered_xml_dialects_fingerprint: str | None = None
1359
1455
 
1360
1456
 
1361
1457
  def try_register_user_xml_dialects(dialects: list[dict[str, Any]]) -> bool:
1362
1458
  """Marshal user XML dialect specs (issue #86) into the Rust registry.
1363
1459
 
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.
1460
+ Idempotent per successfully registered payload. Backend and registration
1461
+ failures propagate: configured matching rules must never be silently ignored.
1462
+ An empty payload clears the native registry.
1367
1463
  """
1368
1464
  global _registered_xml_dialects_fingerprint
1369
1465
  payload = json.dumps(dialects, sort_keys=True)
1370
1466
  if payload == _registered_xml_dialects_fingerprint:
1371
1467
  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
1468
+ backend = _load_backend()
1469
+ register_fn = getattr(backend, "register_user_xml_dialects_json", None)
1470
+ if not callable(register_fn):
1471
+ raise RuntimeError("backend does not expose register_user_xml_dialects_json")
1472
+ register_fn(payload)
1473
+ _registered_xml_dialects_fingerprint = payload
1474
+ return True
1383
1475
 
1384
1476
 
1385
1477
  def _run_invariance_request(request: dict[str, Any]) -> RustInvarianceResult:
@@ -1395,16 +1487,21 @@ def _run_invariance_request(request: dict[str, Any]) -> RustInvarianceResult:
1395
1487
 
1396
1488
  if not isinstance(data, dict):
1397
1489
  raise RuntimeError("Rust invariance engine returned a non-object payload")
1490
+ # Every Rust invariance mode returns all three arrays, even for genuinely
1491
+ # empty results. Missing/null/mistyped fields cannot certify a style-only
1492
+ # or no-change result and must not be silently replaced with empty lists.
1493
+ for field in ("changes", "change_groups", "ignored_style_changes"):
1494
+ if not isinstance(data.get(field), list):
1495
+ raise RuntimeError(f"invalid Rust invariance result: {field} must be an array")
1496
+ if any(not isinstance(item, dict) for item in data["ignored_style_changes"]):
1497
+ raise RuntimeError(
1498
+ "invalid Rust invariance result: ignored_style_changes entries must be objects"
1499
+ )
1398
1500
  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
- ],
1501
+ changes=[Change.model_validate(item) for item in
1502
+ _require_source_anchored_changes(data["changes"], "Rust invariance review")],
1503
+ change_groups=[ChangeGroup.model_validate(item) for item in data["change_groups"]],
1504
+ ignored_style_changes=[dict(item) for item in data["ignored_style_changes"]],
1408
1505
  )
1409
1506
 
1410
1507
 
@@ -1456,6 +1553,7 @@ def try_rust_core_batch_diff(
1456
1553
  or batch.fallback_reason
1457
1554
  or "batch item is incomplete",
1458
1555
  backend_version=batch.backend_version,
1556
+ invalid_output=batch.invalid_output,
1459
1557
  )
1460
1558
 
1461
1559
 
@@ -1483,10 +1581,65 @@ def _normalize_builtin_change_type_enums(diff: SemanticDiff) -> SemanticDiff:
1483
1581
  return diff.model_copy(update={"changes": normalized_changes})
1484
1582
 
1485
1583
 
1584
+ _REQUIRED_RUST_CHANGE_NODES: dict[str, tuple[str, ...]] = {
1585
+ # Structural DTO invariants only: never infer changes, spans or style in Python.
1586
+ "ADDITION": ("new_node",),
1587
+ "DELETION": ("old_node",),
1588
+ "MODIFICATION": ("old_node", "new_node"),
1589
+ "MOVE": ("old_node", "new_node"),
1590
+ "REORDER": ("old_node", "new_node"),
1591
+ }
1592
+
1593
+
1594
+ def _require_source_anchored_changes(changes: Any, context: str) -> list[dict[str, Any]]:
1595
+ """Reject truncated Rust change DTOs before Pydantic supplies nullable nodes.
1596
+
1597
+ Rust omits the *inapplicable* side of additions and deletions. The source
1598
+ side that proves the reported change is mandatory. STYLE_ONLY and other
1599
+ extension kinds retain their existing DTO contract; no semantic decision
1600
+ or source-span inference takes place in this adapter (#228).
1601
+ """
1602
+ if not isinstance(changes, list):
1603
+ raise ValueError(f"{context} requires changes array")
1604
+ for index, change in enumerate(changes):
1605
+ if not isinstance(change, dict):
1606
+ raise ValueError(f"{context} change[{index}] must be an object")
1607
+ for node_field in _REQUIRED_RUST_CHANGE_NODES.get(change.get("change_type"), ()):
1608
+ if not isinstance(change.get(node_field), dict):
1609
+ raise ValueError(
1610
+ f"{context} change[{index}] {change.get('change_type')!r} "
1611
+ f"requires {node_field} source-node object"
1612
+ )
1613
+ return changes
1614
+
1615
+
1616
+ def _require_complete_batch_diff_dto(payload: Any) -> dict[str, Any]:
1617
+ """Reject incomplete Rust success before Python DTO defaults can hide omissions.
1618
+
1619
+ Rust supplies the change inventory and semantic flags. This adapter checks
1620
+ their presence and types only; it never recomputes semantic meaning (#198).
1621
+ """
1622
+ if not isinstance(payload, dict):
1623
+ raise ValueError("Rust complete batch diff must be an object")
1624
+ for field in ("changes", "change_groups", "parse_errors", "guardrail_violations"):
1625
+ if not isinstance(payload.get(field), list):
1626
+ raise ValueError(f"Rust complete batch diff requires {field} array")
1627
+ _require_source_anchored_changes(payload["changes"], "Rust complete batch diff")
1628
+ for field in ("has_semantic_changes", "is_style_only", "is_fallback"):
1629
+ if type(payload.get(field)) is not bool:
1630
+ raise ValueError(f"Rust complete batch diff requires {field} boolean")
1631
+ if not isinstance(payload.get("metadata"), dict):
1632
+ raise ValueError("Rust complete batch diff requires metadata object")
1633
+ return payload
1634
+
1635
+
1486
1636
  def _stamp_certified_rust_diff(metadata: dict[str, Any]) -> dict[str, Any]:
1487
- """Stamp the public ownership contract for accepted Rust-finalized diffs."""
1637
+ """Stamp accepted Rust provenance, never overriding contradictory ownership."""
1488
1638
 
1489
1639
  stamped = dict(metadata)
1640
+ owner = stamped.get("engine_owner")
1641
+ if owner is not None and owner != "rust":
1642
+ raise ValueError(f"Rust complete batch diff contradicts engine ownership: {owner!r}")
1490
1643
  stamped.setdefault("engine_owner", "rust")
1491
1644
  stamped.setdefault("semantic_contract", "rust_finalized_v1")
1492
1645
  return stamped
@@ -1532,6 +1685,9 @@ def try_rust_core_batch_diffs(
1532
1685
 
1533
1686
  try:
1534
1687
  backend = _load_backend()
1688
+ except (RuntimeError, OSError):
1689
+ # Missing Rust runtime is not an explicit Rust decline.
1690
+ raise
1535
1691
  except Exception as exc: # noqa: BLE001 - optional accelerator must never break diffing.
1536
1692
  logger.debug("Rust core unavailable: %s", exc)
1537
1693
  return RustCoreBatchDiffsAttempt(fallback_reason=f"unavailable: {exc}")
@@ -1576,34 +1732,75 @@ def try_rust_core_batch_diffs(
1576
1732
  _record_adapter_phase("rust_core_response_validation", phase_started)
1577
1733
  return RustCoreBatchDiffsAttempt(
1578
1734
  fallback_reason=f"unexpected engine: {engine or '<missing>'}",
1735
+ invalid_output=True,
1579
1736
  adapter_phase_timings=adapter_phase_timings,
1580
1737
  backend_version=backend_version,
1581
1738
  )
1739
+ # Rust's batch contract has exactly four outer statuses. A malformed
1740
+ # envelope is not a legitimate Rust decline, even if a later Rust
1741
+ # finalizer could produce an otherwise valid result.
1742
+ batch_status = data.get("status")
1743
+ if type(batch_status) is not str or batch_status not in (
1744
+ _COMPLETE_STATUS, "candidate", "partial", "fallback"
1745
+ ):
1746
+ raise ValueError(f"Rust batch response status is invalid: {batch_status!r}")
1582
1747
  diff_items = data.get("diffs")
1583
1748
  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,
1749
+ # Core's C ABI emits an explicit item inventory for every declared
1750
+ # batch status, even when all files are Rust-declined. Omitting the
1751
+ # array is corrupted transport, not a valid fallback.
1752
+ raise ValueError("Rust batch response requires diffs array")
1753
+ # Rust always emits one indexed result per requested file, including
1754
+ # explicit candidate/fallback outcomes. A missing record is corrupted
1755
+ # transport, not an implicit Rust decline (or an empty success).
1756
+ if len(diff_items) != len(files):
1757
+ raise ValueError(
1758
+ f"Rust batch response has {len(diff_items)} items "
1759
+ f"for {len(files)} requested files"
1590
1760
  )
1591
1761
  batch_metadata = dict(data.get("metadata") or {})
1592
1762
  fallback_reasons: dict[int, str] = {}
1593
1763
  validated_diffs: list[tuple[int, str, SemanticDiff]] = []
1594
- for fallback_index, item in enumerate(diff_items):
1764
+ seen_indices: set[int] = set()
1765
+ for item in diff_items:
1595
1766
  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:
1767
+ # Rust never emits a non-object item; interpreting one as a
1768
+ # normal decline hides invalid native success behind rerouting.
1769
+ raise ValueError("Rust batch diff item must be an object")
1770
+ # Rust always sends an explicit file index, including on declines.
1771
+ # Inferring a missing index from response order can certify a diff
1772
+ # for the wrong requested file after transport damage.
1773
+ if "index" not in item:
1774
+ raise ValueError("Rust batch response item requires index")
1775
+ index = item["index"]
1776
+ if type(index) is not int or not 0 <= index < len(files):
1777
+ raise ValueError(f"Rust batch response index is invalid: {index!r}")
1778
+ if index in seen_indices:
1779
+ raise ValueError(f"Rust batch response repeats file index {index}")
1780
+ seen_indices.add(index)
1781
+ status = item.get("status")
1782
+ # Candidate is a real, explicitly uncertified Rust benchmark
1783
+ # outcome; both it and fallback may route to Rust finalization.
1784
+ if type(status) is not str or status not in (
1785
+ _COMPLETE_STATUS, "candidate", "fallback"
1786
+ ):
1787
+ raise ValueError(f"Rust batch item status is invalid: {status!r}")
1788
+ if (batch_status == _COMPLETE_STATUS and status != _COMPLETE_STATUS) or (
1789
+ batch_status == "fallback" and status != "fallback"
1790
+ ):
1791
+ raise ValueError(
1792
+ f"Rust batch envelope status {batch_status!r} "
1793
+ f"contradicts item status {status!r}"
1794
+ )
1795
+ if status == _COMPLETE_STATUS and "diff" not in item:
1796
+ raise ValueError("Rust complete batch item requires diff")
1797
+ if status != _COMPLETE_STATUS:
1601
1798
  fallback_reasons[index] = str(
1602
1799
  item.get("reason") or status or "batch item is incomplete"
1603
1800
  )
1604
1801
  continue
1605
1802
  diff = _normalize_builtin_change_type_enums(
1606
- SemanticDiff.model_validate(item["diff"])
1803
+ SemanticDiff.model_validate(_require_complete_batch_diff_dto(item["diff"]))
1607
1804
  )
1608
1805
  validated_diffs.append((index, status, diff))
1609
1806
  _record_adapter_phase("rust_core_response_validation", phase_started)
@@ -1650,12 +1847,33 @@ def try_rust_core_batch_diffs(
1650
1847
  adapter_phase_timings=adapter_phase_timings,
1651
1848
  backend_version=backend_version,
1652
1849
  )
1850
+ except _InvalidNativeCAbiEnvelope as exc:
1851
+ logger.debug("Rust core batch ABI integrity failure: %s", exc)
1852
+ return RustCoreBatchDiffsAttempt(
1853
+ fallback_reason=f"invalid native transport: {exc}",
1854
+ invalid_output=True,
1855
+ adapter_phase_timings=adapter_phase_timings,
1856
+ )
1857
+ except _NativeCAbiOperationFailure as exc:
1858
+ # A raised Rust error is not a Rust response with status="fallback".
1859
+ # The public batch and commit callers must reject this result rather
1860
+ # than executing another route and publishing manufactured success.
1861
+ logger.debug("Rust core batch operation failed: %s", exc)
1862
+ return RustCoreBatchDiffsAttempt(
1863
+ fallback_reason=f"native operation failed: {exc}",
1864
+ invalid_output=True,
1865
+ adapter_phase_timings=adapter_phase_timings,
1866
+ )
1653
1867
  except (ValidationError, ValueError, TypeError) as exc:
1654
1868
  logger.debug("Rust core batch output rejected: %s", exc)
1655
1869
  return RustCoreBatchDiffsAttempt(
1656
1870
  fallback_reason=f"invalid output: {exc}",
1871
+ invalid_output=True,
1657
1872
  adapter_phase_timings=adapter_phase_timings,
1658
1873
  )
1874
+ except (RuntimeError, OSError):
1875
+ # Rust failures cannot be treated as successful alternate routes.
1876
+ raise
1659
1877
  except Exception as exc: # noqa: BLE001 - optional accelerator must fall back.
1660
1878
  logger.debug("Rust core batch path failed: %s", exc, exc_info=True)
1661
1879
  return RustCoreBatchDiffsAttempt(
@@ -1664,6 +1882,125 @@ def try_rust_core_batch_diffs(
1664
1882
  )
1665
1883
 
1666
1884
 
1885
+ class _InvalidNativeCommitDto(RuntimeError):
1886
+ """Native claimed complete output but sent an invalid public commit DTO."""
1887
+
1888
+
1889
+ def _decode_native_commit_control(raw: Any, operation: str) -> dict[str, Any]:
1890
+ """Reject malformed Rust control transport; only an explicit decline can fall back."""
1891
+ try:
1892
+ control = json.loads(raw)
1893
+ except (TypeError, ValueError) as exc:
1894
+ raise _InvalidNativeCommitDto(
1895
+ f"Rust {operation} returned malformed control JSON: {exc}"
1896
+ ) from exc
1897
+ if not isinstance(control, dict) or not isinstance(control.get("status"), str):
1898
+ raise _InvalidNativeCommitDto(
1899
+ f"Rust {operation} requires a control object with status string"
1900
+ )
1901
+ if not control["status"]:
1902
+ raise _InvalidNativeCommitDto(f"Rust {operation} returned empty control status")
1903
+ # Rust commit_json_control emits only these two statuses. Unknown statuses
1904
+ # are malformed transport, never a valid explicit Rust decline.
1905
+ if control["status"] not in (_COMPLETE_STATUS, "fallback"):
1906
+ raise _InvalidNativeCommitDto(
1907
+ f"Rust {operation} returned unknown control status: {control['status']!r}"
1908
+ )
1909
+ return control
1910
+
1911
+
1912
+ def _require_claimed_complete_commit_bytes(
1913
+ control: dict[str, Any], payload: Any, operation: str
1914
+ ) -> bytes:
1915
+ """Do not reinterpret an invalid claimed-complete result as a Rust decline."""
1916
+ if control.get("certification") != _PYTHON_NATIVE_CERTIFICATION:
1917
+ raise _InvalidNativeCommitDto(
1918
+ f"Rust {operation} claimed complete with invalid certification: "
1919
+ f"{control.get('certification')!r}"
1920
+ )
1921
+ if not isinstance(payload, (bytes, bytearray, memoryview)):
1922
+ raise _InvalidNativeCommitDto(
1923
+ f"Rust {operation} claimed complete without valid commit JSON bytes"
1924
+ )
1925
+ commit_json = bytes(payload)
1926
+ if not commit_json:
1927
+ raise _InvalidNativeCommitDto(f"Rust {operation} claimed complete with empty commit JSON")
1928
+ declared_size = control.get("commit_diff_byte_size")
1929
+ if type(declared_size) is not int or declared_size != len(commit_json):
1930
+ raise _InvalidNativeCommitDto(
1931
+ f"Rust {operation} commit JSON byte size mismatch: "
1932
+ f"control={declared_size!r}, payload={len(commit_json)}"
1933
+ )
1934
+ return commit_json
1935
+
1936
+
1937
+ def _require_native_commit_diff_json(payload: bytes, operation: str) -> None:
1938
+ """Validate Rust-authored commit bytes before exposing a successful adapter attempt."""
1939
+ from intentumdiff.core.models import CommitDiff
1940
+
1941
+ try:
1942
+ # Reject malformed Rust-authored semantic flags *before* Pydantic's
1943
+ # non-strict bool fields can coerce strings or integers (#201).
1944
+ # Both certified commit adapters return the original JSON bytes, so
1945
+ # validating the normalized model alone cannot certify those bytes.
1946
+ raw = json.loads(payload)
1947
+ if not isinstance(raw, dict) or not isinstance(raw.get("file_diffs"), list):
1948
+ raise ValueError("Rust commit JSON requires a file_diffs array")
1949
+ for index, file_diff in enumerate(raw["file_diffs"]):
1950
+ if not isinstance(file_diff, dict):
1951
+ raise ValueError(f"Rust commit JSON file_diffs[{index}] must be an object")
1952
+ metadata = file_diff.get("metadata")
1953
+ if not isinstance(metadata, dict):
1954
+ raise ValueError(
1955
+ f"Rust commit JSON file_diffs[{index}] requires metadata object"
1956
+ )
1957
+ if metadata.get("engine_owner") != "rust":
1958
+ raise ValueError(
1959
+ f"Rust commit JSON file_diffs[{index}] requires Rust-owned metadata"
1960
+ )
1961
+ for flag in ("has_semantic_changes", "is_style_only", "is_fallback"):
1962
+ if type(file_diff.get(flag)) is not bool:
1963
+ raise ValueError(
1964
+ f"Rust commit JSON file_diffs[{index}] requires {flag} boolean"
1965
+ )
1966
+ # Reject truncated Rust change evidence before Pydantic normalises it.
1967
+ _require_source_anchored_changes(
1968
+ file_diff.get("changes"), f"Rust commit JSON file_diffs[{index}]"
1969
+ )
1970
+ result = CommitDiff.model_validate_json(payload)
1971
+ # A successful Rust commit must explicitly carry its file inventory.
1972
+ # Pydantic otherwise supplies [] for omitted arrays, manufacturing an
1973
+ # empty successful review from a truncated native response (#199).
1974
+ mandatory_arrays = {
1975
+ "file_diffs", "cross_file_changes", "parse_errors",
1976
+ "guardrail_violations",
1977
+ }
1978
+ omitted = mandatory_arrays - result.model_fields_set
1979
+ if omitted:
1980
+ raise ValueError(f"Rust commit JSON omitted required arrays: {sorted(omitted)}")
1981
+ # A valid top-level file_diffs array does not certify the nested files:
1982
+ # SemanticDiff has default empty arrays and false flags. Without this
1983
+ # check a truncated Rust per-file result becomes a successful no-change
1984
+ # diff when Pydantic fills those defaults (#200).
1985
+ mandatory_file_fields = {
1986
+ "changes", "change_groups", "parse_errors", "guardrail_violations",
1987
+ "has_semantic_changes", "is_style_only", "is_fallback", "metadata",
1988
+ }
1989
+ for index, file_diff in enumerate(result.file_diffs):
1990
+ omitted_file_fields = mandatory_file_fields - file_diff.model_fields_set
1991
+ if omitted_file_fields:
1992
+ raise ValueError(
1993
+ f"Rust commit JSON file_diffs[{index}] omitted required fields: "
1994
+ f"{sorted(omitted_file_fields)}"
1995
+ )
1996
+ if file_diff.metadata.get("engine_owner") != "rust":
1997
+ raise ValueError(
1998
+ f"Rust commit JSON file_diffs[{index}] lacks certified Rust ownership"
1999
+ )
2000
+ except (ValidationError, ValueError, TypeError) as exc:
2001
+ raise _InvalidNativeCommitDto(f"Rust {operation} returned malformed CommitDiff JSON: {exc}") from exc
2002
+
2003
+
1667
2004
  def try_rust_core_commit_json(
1668
2005
  *,
1669
2006
  files: list[dict[str, Any]],
@@ -1704,6 +2041,9 @@ def try_rust_core_commit_json(
1704
2041
 
1705
2042
  try:
1706
2043
  backend = _load_backend()
2044
+ except (RuntimeError, OSError):
2045
+ # Missing Rust runtime is not an explicit Rust decline.
2046
+ raise
1707
2047
  except Exception as exc: # noqa: BLE001 - optional accelerator must never break diffing.
1708
2048
  logger.debug("Rust core unavailable: %s", exc)
1709
2049
  return RustCoreCommitJsonAttempt(fallback_reason=f"unavailable: {exc}")
@@ -1746,13 +2086,10 @@ def try_rust_core_commit_json(
1746
2086
  summary_exclude=True,
1747
2087
  )
1748
2088
  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")
2089
+ control = _decode_native_commit_control(control_json, "certified commit")
1752
2090
  _record_adapter_phase("rust_core_commit_control_decode", phase_started)
1753
2091
  phase_started = time.perf_counter()
1754
2092
  status = str(control.get("status") or "")
1755
- certification = str(control.get("certification") or "")
1756
2093
  if status != _COMPLETE_STATUS:
1757
2094
  reason = str(control.get("reason") or status or "commit JSON incomplete")
1758
2095
  _record_adapter_phase("rust_core_commit_control_validation", phase_started)
@@ -1762,69 +2099,33 @@ def try_rust_core_commit_json(
1762
2099
  backend_version=backend_version,
1763
2100
  fallback_reason=reason,
1764
2101
  )
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
- )
2102
+ commit_json = _require_claimed_complete_commit_bytes(
2103
+ control, commit_payload, "certified commit"
2104
+ )
1804
2105
  _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)
2106
+ # The C ABI control status and matching byte count are not proof that
2107
+ # the bytes constitute a valid CommitDiff. Validate every successful
2108
+ # native response, even when the legacy debug env flag is unset.
2109
+ phase_started = time.perf_counter()
2110
+ _require_native_commit_diff_json(commit_json, "certified commit")
2111
+ _record_adapter_phase("rust_core_commit_pydantic_validation", phase_started)
1816
2112
  return RustCoreCommitJsonAttempt(
1817
2113
  control=control,
1818
2114
  commit_diff_json=commit_json,
1819
2115
  adapter_phase_timings=adapter_phase_timings,
1820
2116
  backend_version=backend_version,
1821
2117
  )
2118
+ except (_InvalidNativeCommitDto, _InvalidNativeCAbiEnvelope, _NativeCAbiOperationFailure):
2119
+ raise
1822
2120
  except (ValidationError, ValueError, TypeError) as exc:
1823
2121
  logger.debug("Rust core certified commit JSON rejected: %s", exc)
1824
2122
  return RustCoreCommitJsonAttempt(
1825
2123
  fallback_reason=f"invalid output: {exc}",
1826
2124
  adapter_phase_timings=adapter_phase_timings,
1827
2125
  )
2126
+ except (RuntimeError, OSError):
2127
+ # Rust failures cannot be treated as successful alternate routes.
2128
+ raise
1828
2129
  except Exception as exc: # noqa: BLE001 - optional accelerator must fall back.
1829
2130
  logger.debug("Rust core certified commit JSON failed: %s", exc, exc_info=True)
1830
2131
  return RustCoreCommitJsonAttempt(
@@ -1866,6 +2167,9 @@ def try_rust_core_working_tree_commit_json(
1866
2167
 
1867
2168
  try:
1868
2169
  backend = _load_backend()
2170
+ except (RuntimeError, OSError):
2171
+ # Missing Rust runtime is not an explicit Rust decline.
2172
+ raise
1869
2173
  except Exception as exc: # noqa: BLE001 - optional accelerator must fall back.
1870
2174
  logger.debug("Rust core unavailable: %s", exc)
1871
2175
  return RustCoreCommitJsonAttempt(fallback_reason=f"unavailable: {exc}")
@@ -1903,14 +2207,11 @@ def try_rust_core_working_tree_commit_json(
1903
2207
  summary_exclude=True,
1904
2208
  )
1905
2209
  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")
2210
+ control = _decode_native_commit_control(control_json, "working-tree commit")
1909
2211
  _record_adapter_phase("rust_core_working_tree_control_decode", phase_started)
1910
2212
 
1911
2213
  phase_started = time.perf_counter()
1912
2214
  status = str(control.get("status") or "")
1913
- certification = str(control.get("certification") or "")
1914
2215
  if status != _COMPLETE_STATUS:
1915
2216
  reason = str(control.get("reason") or status or "working-tree JSON incomplete")
1916
2217
  _record_adapter_phase("rust_core_working_tree_control_validation", phase_started)
@@ -1920,48 +2221,30 @@ def try_rust_core_working_tree_commit_json(
1920
2221
  backend_version=backend_version,
1921
2222
  fallback_reason=reason,
1922
2223
  )
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
- )
2224
+ commit_json = _require_claimed_complete_commit_bytes(
2225
+ control, commit_payload, "working-tree commit"
2226
+ )
1952
2227
  _record_adapter_phase("rust_core_working_tree_control_validation", phase_started)
2228
+ phase_started = time.perf_counter()
2229
+ _require_native_commit_diff_json(commit_json, "working-tree commit")
2230
+ _record_adapter_phase("rust_core_working_tree_pydantic_validation", phase_started)
1953
2231
  return RustCoreCommitJsonAttempt(
1954
2232
  control=control,
1955
2233
  commit_diff_json=commit_json,
1956
2234
  adapter_phase_timings=adapter_phase_timings,
1957
2235
  backend_version=backend_version,
1958
2236
  )
2237
+ except (_InvalidNativeCommitDto, _InvalidNativeCAbiEnvelope, _NativeCAbiOperationFailure):
2238
+ raise
1959
2239
  except (ValidationError, ValueError, TypeError) as exc:
1960
2240
  logger.debug("Rust core working-tree commit JSON rejected: %s", exc)
1961
2241
  return RustCoreCommitJsonAttempt(
1962
2242
  fallback_reason=f"invalid output: {exc}",
1963
2243
  adapter_phase_timings=adapter_phase_timings,
1964
2244
  )
2245
+ except (RuntimeError, OSError):
2246
+ # Rust failures cannot be treated as successful alternate routes.
2247
+ raise
1965
2248
  except Exception as exc: # noqa: BLE001 - optional accelerator must fall back.
1966
2249
  logger.debug("Rust core working-tree commit JSON failed: %s", exc, exc_info=True)
1967
2250
  return RustCoreCommitJsonAttempt(
@@ -2033,279 +2316,6 @@ def run_rust_core_candidate_batch(
2033
2316
  )
2034
2317
 
2035
2318
 
2036
- def try_rust_core_cst_diff(
2037
- *,
2038
- old_filtered_cst_json: str,
2039
- new_filtered_cst_json: str,
2040
- old_raw_source: str,
2041
- new_raw_source: str,
2042
- old_filename: str,
2043
- new_filename: str,
2044
- parser_wasm_path: str,
2045
- language: str,
2046
- parser_plugin_id: str | None,
2047
- config: DiffConfig,
2048
- ) -> RustCoreAttempt:
2049
- """Try the Rust core after Python has produced filtered CST JSON."""
2050
- if not rust_core_enabled(config):
2051
- return RustCoreAttempt(fallback_reason="disabled")
2052
- if language != "python":
2053
- return RustCoreAttempt(fallback_reason="unsupported language")
2054
- parser_plugin_id = _canonical_python_plugin_id(parser_plugin_id)
2055
- if parser_plugin_id is not None and parser_plugin_id not in _PYTHON_PLUGIN_IDS:
2056
- return RustCoreAttempt(fallback_reason="unsupported parser plugin")
2057
- if config.diagnostics:
2058
- return RustCoreAttempt(fallback_reason="diagnostics require Python pipeline")
2059
-
2060
- try:
2061
- backend = _load_backend()
2062
- except Exception as exc: # noqa: BLE001 - optional accelerator must never break diffing.
2063
- logger.debug("Rust core unavailable: %s", exc)
2064
- return RustCoreAttempt(fallback_reason=f"unavailable: {exc}")
2065
-
2066
- try:
2067
- supports = getattr(backend, "supports_language", None)
2068
- if callable(supports) and not supports("python"):
2069
- return RustCoreAttempt(fallback_reason="backend does not support python")
2070
- backend_version = str(getattr(backend, "version", lambda: "")())
2071
- diff_fn = getattr(backend, "diff_python_cst_json", None)
2072
- if not callable(diff_fn):
2073
- return RustCoreAttempt(
2074
- fallback_reason="backend does not expose diff_python_cst_json",
2075
- backend_version=backend_version,
2076
- )
2077
- payload = diff_fn(
2078
- old_filtered_cst_json,
2079
- new_filtered_cst_json,
2080
- old_raw_source,
2081
- new_raw_source,
2082
- old_filename,
2083
- new_filename,
2084
- parser_wasm_path,
2085
- config.model_dump_json(),
2086
- )
2087
- diff = SemanticDiff.model_validate_json(payload)
2088
- metadata = dict(diff.metadata)
2089
- rust_metadata = dict(metadata.get("rust_core") or {})
2090
- status = str(rust_metadata.get("status") or "")
2091
- if status != _COMPLETE_STATUS:
2092
- return RustCoreAttempt(
2093
- fallback_reason=f"incomplete result status: {status or '<missing>'}",
2094
- backend_version=backend_version,
2095
- )
2096
- rust_metadata.update(
2097
- {
2098
- "used": True,
2099
- "backend_version": backend_version,
2100
- }
2101
- )
2102
- metadata["rust_core"] = rust_metadata
2103
- diff = diff.model_copy(update={"metadata": metadata})
2104
- return RustCoreAttempt(diff=diff, backend_version=backend_version)
2105
- except (ValidationError, ValueError, TypeError) as exc:
2106
- logger.debug("Rust core CST output rejected: %s", exc)
2107
- return RustCoreAttempt(fallback_reason=f"invalid output: {exc}")
2108
- except Exception as exc: # noqa: BLE001 - optional accelerator must fall back.
2109
- logger.debug("Rust core CST path failed: %s", exc, exc_info=True)
2110
- return RustCoreAttempt(fallback_reason=f"failed: {exc}")
2111
-
2112
-
2113
- def try_rust_core_tree_diff(
2114
- *,
2115
- old_tree: SemanticNode,
2116
- new_tree: SemanticNode,
2117
- old_filename: str,
2118
- new_filename: str,
2119
- language: str,
2120
- config: DiffConfig,
2121
- ) -> RustCoreTreeAttempt:
2122
- """Try Rust stage 9-11 work after Python has built validated semantic trees.
2123
-
2124
- Since migration step 2 (see ``docs/ENGINE_BOUNDARY_AUDIT.md``) the Rust
2125
- matching/edit-script pipeline is language-agnostic — it operates on the
2126
- ``SemanticNode`` shape, not on Python-specific AST facts. Any language
2127
- label is accepted; the Python-only guard was a certification gate, not
2128
- an algorithm limitation.
2129
- """
2130
- if not rust_core_enabled(config):
2131
- return RustCoreTreeAttempt(fallback_reason="disabled")
2132
- if config.diagnostics:
2133
- return RustCoreTreeAttempt(fallback_reason="diagnostics require Python pipeline")
2134
-
2135
- try:
2136
- backend = _load_backend()
2137
- except Exception as exc: # noqa: BLE001 - optional accelerator must never break diffing.
2138
- logger.debug("Rust core unavailable: %s", exc)
2139
- return RustCoreTreeAttempt(fallback_reason=f"unavailable: {exc}")
2140
-
2141
- try:
2142
- backend_version = str(getattr(backend, "version", lambda: "")())
2143
- # Prefer the language-agnostic entrypoint added in migration step 2.
2144
- # Fall back to the legacy Python-only entrypoint for older installs.
2145
- diff_fn = getattr(backend, "diff_semantic_tree_json", None) or getattr(
2146
- backend, "diff_python_semantic_tree_json", None
2147
- )
2148
- if not callable(diff_fn):
2149
- return RustCoreTreeAttempt(
2150
- fallback_reason="backend exposes no semantic-tree diff entrypoint",
2151
- backend_version=backend_version,
2152
- )
2153
- # Only the legacy Python-only entrypoint requires the language guard.
2154
- if (
2155
- diff_fn.__name__ == "diff_python_semantic_tree_json"
2156
- and language != "python"
2157
- ):
2158
- return RustCoreTreeAttempt(
2159
- fallback_reason=(
2160
- "installed backend only exposes the Python-only entrypoint; "
2161
- "rebuild the Rust core to diff non-Python languages"
2162
- ),
2163
- backend_version=backend_version,
2164
- )
2165
- payload = diff_fn(
2166
- old_tree.model_dump_json(),
2167
- new_tree.model_dump_json(),
2168
- old_filename,
2169
- new_filename,
2170
- language,
2171
- config.model_dump_json(),
2172
- )
2173
- data = _load_tree_payload(payload)
2174
- status = str(data.get("status") or "")
2175
- if status != _COMPLETE_STATUS:
2176
- return RustCoreTreeAttempt(
2177
- fallback_reason=f"incomplete result status: {status or '<missing>'}",
2178
- backend_version=backend_version,
2179
- )
2180
- engine = str(data.get("engine") or "")
2181
- if engine not in {_TREE_ENGINE, _TREE_ENGINE_V3}:
2182
- return RustCoreTreeAttempt(
2183
- fallback_reason=f"unexpected engine: {engine or '<missing>'}",
2184
- backend_version=backend_version,
2185
- )
2186
- old_lookup = _semantic_node_lookup(old_tree)
2187
- new_lookup = _semantic_node_lookup(new_tree)
2188
- matching = _matching_from_payload(data.get("matching_pairs"), old_lookup, new_lookup)
2189
- changes = _changes_from_payload(data.get("changes"), old_lookup, new_lookup)
2190
- metadata = dict(data.get("metadata") or {})
2191
- metadata["rust_core"] = {
2192
- "status": status,
2193
- "used": True,
2194
- "backend_version": backend_version,
2195
- "engine": engine,
2196
- "stage": "semantic_tree_stage9_11",
2197
- }
2198
- return RustCoreTreeAttempt(
2199
- changes=changes,
2200
- matching=matching,
2201
- metadata=metadata,
2202
- backend_version=backend_version,
2203
- )
2204
- except (ValidationError, ValueError, TypeError) as exc:
2205
- logger.debug("Rust core semantic-tree output rejected: %s", exc)
2206
- return RustCoreTreeAttempt(fallback_reason=f"invalid output: {exc}")
2207
- except Exception as exc: # noqa: BLE001 - optional accelerator must fall back.
2208
- logger.debug("Rust core semantic-tree path failed: %s", exc, exc_info=True)
2209
- return RustCoreTreeAttempt(fallback_reason=f"failed: {exc}")
2210
-
2211
-
2212
- def try_rust_core_sources_stage11(
2213
- *,
2214
- old_source: str,
2215
- new_source: str,
2216
- old_filename: str,
2217
- new_filename: str,
2218
- parser_wasm_path: str,
2219
- language: str,
2220
- parser_plugin_id: str | None,
2221
- config: DiffConfig,
2222
- ) -> RustCoreSourceAttempt:
2223
- """Try Rust V3 from raw Python source through initial Stage 11 output."""
2224
- if not rust_core_enabled(config):
2225
- return RustCoreSourceAttempt(fallback_reason="disabled")
2226
- if language != "python":
2227
- return RustCoreSourceAttempt(fallback_reason="unsupported language")
2228
- parser_plugin_id = _canonical_python_plugin_id(parser_plugin_id)
2229
- if parser_plugin_id is not None and parser_plugin_id not in _PYTHON_PLUGIN_IDS:
2230
- return RustCoreSourceAttempt(fallback_reason="unsupported parser plugin")
2231
- if config.diagnostics:
2232
- return RustCoreSourceAttempt(fallback_reason="diagnostics require Python pipeline")
2233
-
2234
- try:
2235
- backend = _load_backend()
2236
- except Exception as exc: # noqa: BLE001 - optional accelerator must never break diffing.
2237
- logger.debug("Rust core unavailable: %s", exc)
2238
- return RustCoreSourceAttempt(fallback_reason=f"unavailable: {exc}")
2239
-
2240
- try:
2241
- supports = getattr(backend, "supports_language", None)
2242
- if callable(supports) and not supports("python"):
2243
- return RustCoreSourceAttempt(fallback_reason="backend does not support python")
2244
- backend_version = str(getattr(backend, "version", lambda: "")())
2245
- diff_fn = getattr(backend, "diff_python_sources_stage11_json", None)
2246
- if not callable(diff_fn):
2247
- return RustCoreSourceAttempt(
2248
- fallback_reason="backend does not expose diff_python_sources_stage11_json",
2249
- backend_version=backend_version,
2250
- )
2251
- payload = diff_fn(
2252
- old_source,
2253
- new_source,
2254
- old_filename,
2255
- new_filename,
2256
- parser_wasm_path,
2257
- config.model_dump_json(),
2258
- )
2259
- data = _load_tree_payload(payload)
2260
- status = str(data.get("status") or "")
2261
- if status != _COMPLETE_STATUS:
2262
- reason = str(data.get("reason") or status or "<missing>")
2263
- return RustCoreSourceAttempt(
2264
- fallback_reason=f"incomplete result status: {reason}",
2265
- backend_version=backend_version,
2266
- )
2267
- engine = str(data.get("engine") or "")
2268
- if engine != _SOURCE_ENGINE:
2269
- return RustCoreSourceAttempt(
2270
- fallback_reason=f"unexpected engine: {engine or '<missing>'}",
2271
- backend_version=backend_version,
2272
- )
2273
- old_tree = SemanticNode.model_validate(data.get("old_tree"))
2274
- new_tree = SemanticNode.model_validate(data.get("new_tree"))
2275
- old_lookup = _semantic_node_lookup(old_tree)
2276
- new_lookup = _semantic_node_lookup(new_tree)
2277
- matching = _matching_from_payload(data.get("matching_pairs"), old_lookup, new_lookup)
2278
- changes = _changes_from_payload(data.get("changes"), old_lookup, new_lookup)
2279
- adaptive_fuel = int(data.get("adaptive_fuel") or config.plugin_fuel)
2280
- metadata = dict(data.get("metadata") or {})
2281
- rust_phase_timings = metadata.pop("phase_timings", None)
2282
- if rust_phase_timings is not None:
2283
- metadata["rust_phase_timings"] = rust_phase_timings
2284
- metadata["rust_core"] = {
2285
- "status": status,
2286
- "used": True,
2287
- "backend_version": backend_version,
2288
- "engine": engine,
2289
- "stage": "sources_to_stage11",
2290
- "wasm_boundary": metadata.get("wasm_boundary", "rust_wasmtime"),
2291
- }
2292
- return RustCoreSourceAttempt(
2293
- old_tree=old_tree,
2294
- new_tree=new_tree,
2295
- changes=changes,
2296
- matching=matching,
2297
- adaptive_fuel=adaptive_fuel,
2298
- metadata=metadata,
2299
- backend_version=backend_version,
2300
- )
2301
- except (ValidationError, ValueError, TypeError) as exc:
2302
- logger.debug("Rust core source-stage output rejected: %s", exc)
2303
- return RustCoreSourceAttempt(fallback_reason=f"invalid output: {exc}")
2304
- except Exception as exc: # noqa: BLE001 - optional accelerator must fall back.
2305
- logger.debug("Rust core source-stage path failed: %s", exc, exc_info=True)
2306
- return RustCoreSourceAttempt(fallback_reason=f"failed: {exc}")
2307
-
2308
-
2309
2319
  def _load_tree_payload(payload: Any) -> dict[str, Any]:
2310
2320
  if isinstance(payload, str):
2311
2321
  import json
@@ -2318,53 +2328,3 @@ def _load_tree_payload(payload: Any) -> dict[str, Any]:
2318
2328
  return data
2319
2329
 
2320
2330
 
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