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
@@ -5,14 +5,12 @@ to route changed files — text goes to the semantic parser, binary/image assets
5
5
  go to the perceptual asset diff — and to enrich diff metadata with the detected
6
6
  MIME type. Detection inspects the leading bytes of a file, not its extension.
7
7
 
8
- Falls back to a NUL-byte heuristic if the Rust core is unavailable, so callers
9
- never crash when the native extension is missing.
8
+ A missing or failing Rust engine raises; routing never uses a Python fallback.
10
9
  """
11
10
 
12
11
  from __future__ import annotations
13
12
 
14
- import json
15
- from typing import Any, TypedDict
13
+ from typing import TypedDict
16
14
 
17
15
  #: How many leading bytes to sniff. A few KB is plenty for magic-byte detection.
18
16
  HEAD_BYTES = 8192
@@ -25,33 +23,18 @@ class ContentType(TypedDict):
25
23
  is_text: bool
26
24
 
27
25
 
28
- def _fallback(head: bytes) -> ContentType:
29
- is_text = b"\x00" not in head[:HEAD_BYTES]
30
- return {
31
- "mime": "text/plain" if is_text else "application/octet-stream",
32
- "extension": "",
33
- "category": "text" if is_text else "binary",
34
- "is_text": is_text,
35
- }
36
-
37
-
38
26
  def detect_content_type(head: bytes) -> ContentType:
39
- """Return the detected content type for a file's leading bytes."""
40
- sample = bytes(head[:HEAD_BYTES])
41
- try:
42
- from intentumdiff.rust_core import _load_backend
43
-
44
- backend = _load_backend()
45
- raw = backend.detect_content_type_json(sample)
46
- parsed: dict[str, Any] = json.loads(raw)
47
- return {
48
- "mime": str(parsed.get("mime", "application/octet-stream")),
49
- "extension": str(parsed.get("extension", "")),
50
- "category": str(parsed.get("category", "binary")),
51
- "is_text": bool(parsed.get("is_text", False)),
52
- }
53
- except Exception: # noqa: BLE001 — detection must never break the diff pipeline.
54
- return _fallback(sample)
27
+ """Return Rust's detected content type; propagate required engine failures."""
28
+ from intentumdiff.rust_core import _required_engine_json
29
+
30
+ result = _required_engine_json(
31
+ "detect_content_type_json",
32
+ bytes(head[:HEAD_BYTES]),
33
+ result_type=dict,
34
+ )
35
+ if any(not isinstance(result.get(key), str) for key in ("mime", "extension", "category")) or type(result.get("is_text")) is not bool:
36
+ raise RuntimeError("Rust content-type result has invalid fields")
37
+ return ContentType(**{key: result[key] for key in ContentType.__annotations__})
55
38
 
56
39
 
57
40
  def is_text_bytes(head: bytes) -> bool:
@@ -36,7 +36,7 @@ import json
36
36
  import logging
37
37
  import os
38
38
  from dataclasses import dataclass
39
- from typing import TYPE_CHECKING, Iterator
39
+ from typing import TYPE_CHECKING, Callable, Iterator
40
40
 
41
41
  from intentumdiff.core.models import CommitDiff, CrossFileChange, DiffConfig, SemanticDiff
42
42
  from intentumdiff.differ import SemanticDiffer
@@ -45,6 +45,8 @@ from intentumdiff.core.index import SemanticIndex
45
45
  from intentumdiff.plugins.exceptions import PluginError, PluginNotFoundError
46
46
  from intentumdiff.sources.git_source import iter_changed_sources
47
47
  from intentumdiff.vcs.base import VcsBackend
48
+ from intentumdiff.vcs.git_cli import resolve_repo_root
49
+ from intentumdiff.vcs._safe_path import safe_policy_path
48
50
 
49
51
  if TYPE_CHECKING:
50
52
  from intentumdiff.core.models import SemanticNode
@@ -135,15 +137,9 @@ class CommitDiffer:
135
137
  if backend is not None:
136
138
  return self._diff_with_backend(backend, old_ref, new_ref)
137
139
 
138
- # Pre-pass: collect git-deletion paths and any new .gitignore content so
139
- # we can tag SemanticDiff objects whose deletion was triggered by a .gitignore
140
- # rule change rather than actual code removal.
141
- deleted_paths, gitignore_spec = self._collect_gitignore_state(
142
- repo_path, old_ref, new_ref
143
- )
144
-
145
140
  results, errors, changed_file_count = self._collect_file_diffs(
146
- iter_changed_sources(repo_path, old_ref, new_ref)
141
+ iter_changed_sources(repo_path, old_ref, new_ref),
142
+ policy_root=resolve_repo_root(repo_path),
147
143
  )
148
144
 
149
145
  return self._finalize_commit_diff(
@@ -152,8 +148,6 @@ class CommitDiffer:
152
148
  changed_file_count=changed_file_count,
153
149
  old_ref=old_ref,
154
150
  new_ref=new_ref,
155
- deleted_paths=deleted_paths,
156
- gitignore_spec=gitignore_spec,
157
151
  )
158
152
 
159
153
  def iter_file_diffs(
@@ -165,8 +159,7 @@ class CommitDiffer:
165
159
  """
166
160
  Yield per-file diffs one at a time for progressive/streaming review.
167
161
 
168
- Performs the gitignore pre-pass (so deletions are classifiable) and
169
- then yields each changed source's :class:`FileDiffResult` (on success)
162
+ Yields each changed source's :class:`FileDiffResult` (on success)
170
163
  or :class:`FileDiffError` (when no parser is available or the pipeline
171
164
  failed). Cross-file analysis is NOT performed here — callers that need
172
165
  a complete :class:`CommitDiff` should collect the results and pass them
@@ -175,31 +168,23 @@ class CommitDiffer:
175
168
  Always uses the git backend (the ``backend`` parameter of
176
169
  :meth:`diff_commit` is not supported by this streaming entry point).
177
170
  """
178
- def _looks_binary(text: str) -> bool:
179
- # Content is decoded with errors="replace"; a NUL byte (valid UTF-8
180
- # U+0000) survives and reliably marks binary/image assets, as does a
181
- # high ratio of U+FFFD replacement characters. Feeding such content to
182
- # a text parser (the generic catch-all) explodes the CST — e.g. a PNG
183
- # producing >100 MB of output that the plugin host then rejects.
184
- head = text[:8192]
185
- if not head:
186
- return False
187
- if "\x00" in head:
188
- return True
189
- return head.count("�") / len(head) > 0.1
190
-
191
- for source in iter_changed_sources(repo_path, old_ref, new_ref):
171
+ from intentumdiff.content_type import is_text_bytes
172
+
173
+ policy_root = resolve_repo_root(repo_path)
174
+ for source in iter_changed_sources(policy_root, old_ref, new_ref):
192
175
  old_content, new_content, old_path, new_path, staging_status = source
193
176
  # Backstop: primary content-based routing happens at the git read
194
- # boundary (magic-byte detection); this NUL-byte check catches any
177
+ # boundary (magic-byte detection); the same Rust detector catches any
195
178
  # binary that reaches the streaming path through another route,
196
179
  # before it explodes the text parser.
197
- if _looks_binary(new_content) or _looks_binary(old_content):
180
+ if not is_text_bytes(new_content.encode("utf-8")) or not is_text_bytes(old_content.encode("utf-8")):
198
181
  logger.debug("Skipping %r — binary/non-text asset", old_path)
199
182
  continue
200
183
  try:
201
184
  file_diff = self._differ._run_pipeline(
202
- old_content, new_content, old_path, None, new_filename=new_path
185
+ old_content, new_content, old_path, None, new_filename=new_path,
186
+ _policy_old_filename=safe_policy_path(policy_root, old_path),
187
+ _policy_source_filename=safe_policy_path(policy_root, new_path or old_path),
203
188
  )
204
189
  if staging_status is not None:
205
190
  file_diff = file_diff.model_copy(
@@ -241,17 +226,10 @@ class CommitDiffer:
241
226
  """
242
227
  Build a complete :class:`CommitDiff` from streamed per-file results.
243
228
 
244
- Applies the gitignore-deletion tagging (when *repo_path* is supplied so
245
- the pre-pass can run), runs cross-file analysis, and assembles the
229
+ Preserves engine-produced file evidence, runs cross-file analysis, and assembles the
246
230
  terminal ``CommitDiff``. This is the counterpart to
247
231
  :meth:`iter_file_diffs`.
248
232
  """
249
- deleted_paths: set[str] = set()
250
- gitignore_spec = None
251
- if repo_path is not None:
252
- deleted_paths, gitignore_spec = self._collect_gitignore_state(
253
- repo_path, old_ref, new_ref
254
- )
255
233
  changed_file_count = len(results) + len(errors)
256
234
  return self._finalize_commit_diff(
257
235
  results=results,
@@ -259,23 +237,27 @@ class CommitDiffer:
259
237
  changed_file_count=changed_file_count,
260
238
  old_ref=old_ref,
261
239
  new_ref=new_ref,
262
- deleted_paths=deleted_paths,
263
- gitignore_spec=gitignore_spec,
264
240
  )
265
241
 
266
242
  def _collect_file_diffs(
267
243
  self,
268
244
  sources: Iterator[tuple[str, str, str, str, object]],
245
+ *,
246
+ policy_root: str | os.PathLike[str],
247
+ policy_path_resolver: Callable[[str], str] | None = None,
269
248
  ) -> tuple[list[FileDiffResult], list[FileDiffError], int]:
270
249
  """Run the per-file pipeline over an iterator of changed sources."""
271
250
  results: list[FileDiffResult] = []
272
251
  errors: list[FileDiffError] = []
273
252
  changed_file_count = 0
253
+ policy_path = policy_path_resolver or (lambda path: safe_policy_path(policy_root, path))
274
254
  for old_content, new_content, old_path, new_path, staging_status in sources:
275
255
  changed_file_count += 1
276
256
  try:
277
257
  file_diff = self._differ._run_pipeline(
278
- old_content, new_content, old_path, None, new_filename=new_path
258
+ old_content, new_content, old_path, None, new_filename=new_path,
259
+ _policy_old_filename=policy_path(old_path),
260
+ _policy_source_filename=policy_path(new_path or old_path),
279
261
  )
280
262
  if staging_status is not None:
281
263
  file_diff = file_diff.model_copy(
@@ -320,8 +302,6 @@ class CommitDiffer:
320
302
  changed_file_count: int,
321
303
  old_ref: str,
322
304
  new_ref: str,
323
- deleted_paths: set[str] | None = None,
324
- gitignore_spec=None,
325
305
  ) -> CommitDiff:
326
306
  """Assemble the terminal CommitDiff from per-file results + errors."""
327
307
  file_diffs = [result.file_diff for result in results]
@@ -331,17 +311,17 @@ class CommitDiffer:
331
311
  if error.kind == "pipeline_error"
332
312
  ]
333
313
 
334
- self._raise_if_all_parsers_failed(changed_file_count, file_diffs, parse_errors)
335
-
336
- # Tag diffs whose file deletion was caused by a .gitignore rule addition.
337
- if gitignore_spec is not None and deleted_paths:
338
- file_diffs = [
339
- fd.model_copy(update={"gitignore_excluded": True})
340
- if fd.old_filename in deleted_paths
341
- and gitignore_spec.match_file(fd.old_filename)
342
- else fd
343
- for fd in file_diffs
344
- ]
314
+ self._raise_if_all_parsers_failed(
315
+ changed_file_count, file_diffs, parse_errors, file_errors=errors
316
+ )
317
+ if errors:
318
+ details = "; ".join(
319
+ f"{err.old_path}: {err.kind}: {err.reason}" for err in errors
320
+ )
321
+ raise RuntimeError(
322
+ f"Commit diff incomplete ({len(errors)} of {changed_file_count} text files failed): "
323
+ f"{details}"
324
+ )
345
325
 
346
326
  # Build semantic indexes for changed files only.
347
327
  old_file_contents = [
@@ -356,8 +336,14 @@ class CommitDiffer:
356
336
  if old_file_contents:
357
337
  old_index = self._build_index(old_file_contents)
358
338
  new_index = self._build_index(new_file_contents)
359
- if old_index is not None and new_index is not None:
360
- cross_file_changes = self._detect_cross_file(old_index, new_index)
339
+ # Both source sets are nonempty. An absent index means incomplete
340
+ # cross-file semantics, not an honest "no cross-file changes" result.
341
+ if old_index is None or new_index is None:
342
+ raise RuntimeError(
343
+ "Commit semantic index incomplete: missing old or new index "
344
+ "for changed text files"
345
+ )
346
+ cross_file_changes = self._detect_cross_file(old_index, new_index)
361
347
 
362
348
  return CommitDiff(
363
349
  old_ref=old_ref,
@@ -382,69 +368,30 @@ class CommitDiffer:
382
368
  old_ref: str,
383
369
  new_ref: str,
384
370
  ) -> CommitDiff:
385
- """
386
- Run a full semantic diff using an arbitrary :class:`VcsBackend`.
387
-
388
- Called by :meth:`diff_commit` when a *backend* is provided. The
389
- gitignore pre-pass is skipped because it is git-specific.
390
- """
391
- changed_files = backend.list_changed_files(old_ref, new_ref)
392
-
393
- file_diffs = []
394
- parse_errors: list[str] = []
395
- old_file_contents: list[tuple[str, str, str]] = []
396
- new_file_contents: list[tuple[str, str, str]] = []
397
- changed_file_count = 0
398
-
399
- for cf in changed_files:
400
- if cf.is_binary:
401
- continue
402
- changed_file_count += 1
403
-
404
- old_path = cf.old_path or cf.new_path
405
- new_path = cf.new_path or cf.old_path
406
-
407
- if old_path is None:
408
- continue
409
-
410
- old_content = backend.get_blob(old_path, old_ref) if cf.old_path else ""
411
- new_content = backend.get_blob(new_path, new_ref) if cf.new_path else "" # type: ignore[arg-type]
412
-
413
- try:
414
- file_diff = self._differ._run_pipeline(
415
- old_content, new_content, old_path, None, new_filename=new_path
416
- )
417
- file_diffs.append(file_diff)
418
- old_file_contents.append((old_path, file_diff.language, old_content))
419
- new_file_contents.append(
420
- (new_path or old_path, file_diff.language, new_content)
421
- )
422
- except PluginNotFoundError:
423
- logger.debug("Skipping %r — no parser available", old_path)
424
- except (PluginError, ValueError, RuntimeError) as exc:
425
- logger.warning("Failed to diff %r: %s", old_path, exc)
426
- parse_errors.append(f"{old_path}: {exc}")
427
-
428
- self._raise_if_all_parsers_failed(changed_file_count, file_diffs, parse_errors)
429
-
430
- cross_file_changes: list[CrossFileChange] = []
431
- if old_file_contents:
432
- old_index = self._build_index(old_file_contents)
433
- new_index = self._build_index(new_file_contents)
434
- if old_index is not None and new_index is not None:
435
- cross_file_changes = self._detect_cross_file(old_index, new_index)
371
+ """Apply the same fail-closed Rust-backed per-file route to every VCS."""
372
+
373
+ def changed_sources() -> Iterator[tuple[str, str, str, str, object]]:
374
+ for changed in backend.list_changed_files(old_ref, new_ref):
375
+ if changed.is_binary:
376
+ continue
377
+ path = changed.old_path or changed.new_path
378
+ if path is None:
379
+ raise ValueError("VCS backend returned a changed text file without a path")
380
+ new_path = changed.new_path or path
381
+ old = backend.get_blob(changed.old_path, old_ref) if changed.old_path else ""
382
+ new = backend.get_blob(changed.new_path, new_ref) if changed.new_path else ""
383
+ yield old, new, path, new_path, None
436
384
 
437
- return CommitDiff(
385
+ results, errors, changed_file_count = self._collect_file_diffs(
386
+ changed_sources(), policy_root=backend.resolve_root(),
387
+ policy_path_resolver=backend.guardrail_policy_path,
388
+ )
389
+ return self._finalize_commit_diff(
390
+ results=results,
391
+ errors=errors,
392
+ changed_file_count=changed_file_count,
438
393
  old_ref=old_ref,
439
394
  new_ref=new_ref,
440
- guardrail_violations=[
441
- violation
442
- for file_diff in file_diffs
443
- for violation in file_diff.guardrail_violations
444
- ],
445
- file_diffs=file_diffs,
446
- cross_file_changes=cross_file_changes,
447
- parse_errors=parse_errors,
448
395
  )
449
396
 
450
397
  def _raise_if_all_parsers_failed(
@@ -452,6 +399,8 @@ class CommitDiffer:
452
399
  changed_file_count: int,
453
400
  file_diffs: list,
454
401
  parse_errors: list[str],
402
+ *,
403
+ file_errors: list[FileDiffError] | None = None,
455
404
  ) -> None:
456
405
  """Fail loudly when changed files exist but no parser plugin loaded."""
457
406
  if changed_file_count <= 0 or file_diffs or parse_errors:
@@ -462,87 +411,16 @@ class CommitDiffer:
462
411
  summary = summary_fn()
463
412
  if summary:
464
413
  raise RuntimeError(summary)
465
-
466
- def _collect_gitignore_state(
467
- self,
468
- repo_path: "str | os.PathLike[str]",
469
- old_ref: str,
470
- new_ref: str,
471
- ) -> "tuple[set[str], object]":
472
- """
473
- Lightweight pre-scan of the commit diff.
474
-
475
- Returns a 2-tuple ``(deleted_paths, gitignore_spec)``:
476
-
477
- * ``deleted_paths`` — set of file paths that git marks as deleted (change
478
- type ``"D"``). Used later to distinguish genuine deletions from files
479
- evicted by a ``.gitignore`` rule change.
480
- * ``gitignore_spec`` — a compiled ``pathspec.PathSpec`` built from the
481
- *new* content of any ``.gitignore`` file modified in this commit, or
482
- ``None`` when no ``.gitignore`` changed. Only root-level and
483
- subdirectory ``.gitignore`` files that appear in the commit diff are
484
- considered; ``.git/info/exclude`` is outside the commit tree and is
485
- therefore ignored.
486
- """
487
- deleted_paths: set[str] = set()
488
- gitignore_spec = None
489
- try:
490
- import subprocess
491
-
492
- from intentumdiff.sources.git_source import _parse_name_status_z
493
- from intentumdiff.vcs.git_cli import resolve_repo_root, run_git_bytes
494
-
495
- root = resolve_repo_root(repo_path)
496
- # old -> new (or old -> working tree when new_ref is ""); a=old, b=new,
497
- # matching the former old_commit.diff(None|new_commit).
498
- args = ["diff", "--name-status", "-z", old_ref]
499
- if new_ref:
500
- args.append(new_ref)
501
- for code, a_path, b_path in _parse_name_status_z(run_git_bytes(root, args)):
502
- if code[:1] == "D":
503
- deleted_paths.add(a_path)
504
-
505
- # Capture the new .gitignore content for root or subdir gitignore files.
506
- if code[:1] in ("A", "M") and (
507
- b_path == ".gitignore" or b_path.endswith("/.gitignore")
508
- ):
509
- raw: str | None = None
510
- if new_ref: # commit-to-commit: read the new blob
511
- try:
512
- raw = run_git_bytes(
513
- root, ["cat-file", "blob", f"{new_ref}:{b_path}"]
514
- ).decode("utf-8", errors="replace")
515
- except subprocess.CalledProcessError:
516
- raw = None
517
- else: # working-tree mode: read from disk
518
- from pathlib import Path as _Path
519
- disk_gi = _Path(root) / b_path
520
- if disk_gi.exists():
521
- try:
522
- raw = disk_gi.read_text(encoding="utf-8", errors="replace")
523
- except OSError:
524
- pass
525
- if raw is not None:
526
- try:
527
- import pathspec as _pathspec # lazy import
528
- # For subdirectory gitignore files (e.g. src/.gitignore),
529
- # prepend the directory so patterns are matched against
530
- # full repo-relative paths.
531
- gitignore_dir = b_path[: -len(".gitignore")].rstrip("/")
532
- if gitignore_dir:
533
- lines = [
534
- f"{gitignore_dir}/{ln}" if ln and not ln.startswith("#")
535
- else ln
536
- for ln in raw.splitlines()
537
- ]
538
- else:
539
- lines = raw.splitlines()
540
- gitignore_spec = _pathspec.PathSpec.from_lines("gitignore", lines)
541
- except Exception as exc: # pragma: no cover
542
- logger.debug("Failed to parse .gitignore blob: %s", exc)
543
- except Exception as exc:
544
- logger.debug("gitignore pre-pass failed: %s", exc)
545
- return deleted_paths, gitignore_spec
414
+ # Preserve concrete failing paths when the registry cannot supply a richer
415
+ # parser-load diagnostic. A generic error hides the failed file and masks
416
+ # actionable evidence from the fail-closed commit boundary.
417
+ details = (
418
+ ": " + "; ".join(f"{error.old_path}: {error.reason}" for error in file_errors)
419
+ if file_errors else ""
420
+ )
421
+ raise RuntimeError(
422
+ "Commit diff incomplete: no parser succeeded for changed text files" + details
423
+ )
546
424
 
547
425
  def _build_index(
548
426
  self, file_contents: list[tuple[str, str, str]]
@@ -552,20 +430,27 @@ class CommitDiffer:
552
430
  triples.
553
431
 
554
432
  The symbol/reference tables are built by the Rust core
555
- (index-engine-lib) inside ``SemanticIndex.build()``. Returns ``None`` if
556
- no files could be parsed.
433
+ (index-engine-lib) inside ``SemanticIndex.build()``. Every changed text
434
+ file must contribute a tree; skipping one would silently remove its
435
+ cross-file evidence. Only an intentionally empty input list has no index.
557
436
  """
437
+ if not file_contents:
438
+ return None
439
+
558
440
  index = SemanticIndex()
559
441
  for filename, language, content in file_contents:
560
442
  try:
561
443
  tree = self._parse_to_tree(filename, language, content)
562
- if tree is not None:
563
- index.add_tree(filename, language, tree)
564
- except Exception as exc:
565
- logger.debug("Could not parse %r for index: %s", filename, exc)
566
-
567
- if not index._files: # type: ignore[attr-defined] # pylint: disable=protected-access
568
- return None
444
+ except (PluginNotFoundError, PluginError, ValueError, RuntimeError) as exc:
445
+ raise RuntimeError(
446
+ f"Commit semantic index incomplete for {filename!r}: {exc}"
447
+ ) from exc
448
+ if tree is None:
449
+ raise RuntimeError(
450
+ f"Commit semantic index incomplete for {filename!r}: "
451
+ "parser returned no semantic tree"
452
+ )
453
+ index.add_tree(filename, language, tree)
569
454
 
570
455
  index.build()
571
456
  return index
@@ -582,18 +467,18 @@ class CommitDiffer:
582
467
  """
583
468
  Parse *content* to a SemanticNode tree.
584
469
 
585
- Re-uses the ``SemanticDiffer`` parser pipeline so both FullParse and
586
- host-CST parser plugins can participate in commit-wide symbol indexing.
470
+ Re-uses the ``SemanticDiffer`` FullParse parser pipeline for
471
+ commit-wide symbol indexing. Missing parsers must propagate to the
472
+ caller so a complete commit review cannot silently omit a text file.
587
473
  """
588
- try:
589
- tree, _language = self._differ.parse(
590
- content,
591
- filename,
592
- language_hint=language,
593
- )
594
- return tree
595
- except PluginNotFoundError:
596
- return None
597
- except Exception as exc:
598
- logger.debug("parse_to_tree failed for %r: %s", filename, exc)
599
- return None
474
+ # Match the existing source-diff and native Rust empty-side contract. Some
475
+ # parsers reject empty input; that is not a missing symbol-table failure.
476
+ if not content:
477
+ from intentumdiff._differ_presentation import _empty_semantic_tree
478
+ return _empty_semantic_tree(language)
479
+ tree, _language = self._differ.parse(
480
+ content,
481
+ filename,
482
+ language_hint=language,
483
+ )
484
+ return tree
@@ -26,14 +26,9 @@ Examples::
26
26
 
27
27
  from __future__ import annotations
28
28
 
29
- import logging
30
29
  from pathlib import Path
31
- from typing import TYPE_CHECKING
32
-
33
- logger = logging.getLogger(__name__)
34
-
35
- if TYPE_CHECKING:
36
- import pathspec
30
+ import json
31
+ from collections.abc import Mapping
37
32
 
38
33
  #: Conventional filename — place in the repo / directory root.
39
34
  DIFFIGNORE_FILENAME = ".diffignore"
@@ -44,44 +39,43 @@ def load_diffignore(root: str | Path) -> "DiffIgnore | None":
44
39
  Look for a ``.diffignore`` file in *root* and return a :class:`DiffIgnore`
45
40
  instance if found, or ``None`` when the file does not exist.
46
41
 
47
- A warning is logged (but not raised) if the file exists but cannot be read.
42
+ A configured policy that cannot be read must fail closed; only a missing
43
+ policy is treated as no exclusions.
48
44
  """
49
45
  path = Path(root) / DIFFIGNORE_FILENAME
50
- if not path.is_file():
51
- return None
52
-
53
46
  try:
54
47
  text = path.read_text(encoding="utf-8")
55
- except OSError as exc:
56
- logger.warning("Could not read %s: %s", path, exc)
48
+ except FileNotFoundError as exc:
49
+ # A broken symlink is an explicitly configured but unusable policy,
50
+ # not evidence that no policy exists.
51
+ if path.is_symlink():
52
+ raise RuntimeError(f"Could not read configured .diffignore {path}: {exc}") from exc
57
53
  return None
54
+ except (OSError, UnicodeError) as exc:
55
+ raise RuntimeError(f"Could not read configured .diffignore {path}: {exc}") from exc
58
56
 
59
- import pathspec # lazy — keep import-time fast when diffignore is absent
60
-
61
- spec = pathspec.PathSpec.from_lines("gitignore", text.splitlines())
62
- logger.debug("Loaded .diffignore from %s (%d pattern(s))", path, len(spec.patterns))
63
- return DiffIgnore(spec)
57
+ return DiffIgnore(text)
64
58
 
65
59
 
66
60
  class DiffIgnore:
67
- """
68
- Wraps a ``pathspec.PathSpec`` to test whether relative file paths should
69
- be excluded from the diff / index pipeline.
61
+ """Thin adapter over Rust ignore rules; construct with raw file contents."""
70
62
 
71
- Instances are created by :func:`load_diffignore`; construct one directly
72
- only in tests.
73
- """
63
+ __slots__ = ("_files",)
74
64
 
75
- __slots__ = ("_spec",)
65
+ def __init__(self, patterns: str = "", *, directory_rules: Mapping[str, str] | None = None) -> None:
66
+ if not isinstance(patterns, str):
67
+ raise TypeError("DiffIgnore requires raw ignore-file text")
68
+ self._files = [{"directory": "", "content": patterns}]
69
+ self._files.extend({"directory": directory, "content": content} for directory, content in (directory_rules or {}).items())
70
+ self._matches([]) # validate rules through the engine eagerly
76
71
 
77
- def __init__(self, spec: "pathspec.PathSpec") -> None:
78
- self._spec = spec
72
+ def _matches(self, paths: list[dict]) -> list[bool]:
73
+ from intentumdiff.rust_core import _required_engine_json
74
+ result = _required_engine_json("match_ignore_rules", json.dumps({"files": self._files, "paths": paths}), result_type=list)
75
+ if len(result) != len(paths) or any(type(value) is not bool for value in result):
76
+ raise RuntimeError("Rust ignore result has invalid fields")
77
+ return result
79
78
 
80
79
  def is_ignored(self, rel_path: str) -> bool:
81
- """
82
- Return ``True`` if *rel_path* is matched by any pattern in the file.
83
-
84
- *rel_path* must use **forward slashes** (POSIX style), e.g.
85
- ``"src/generated/foo.py"`` — not ``"src\\generated\\foo.py"``.
86
- """
87
- return bool(self._spec.match_file(rel_path))
80
+ """Test a repository-relative POSIX file path using Rust semantics."""
81
+ return self._matches([{"path": rel_path}])[0]