intentumdiff-python 0.0.1__py3-none-win_amd64.whl → 0.0.3b1__py3-none-win_amd64.whl

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (150) hide show
  1. intentumdiff/__init__.py +9 -0
  2. intentumdiff/__main__.py +12 -0
  3. intentumdiff/_differ_gate.py +0 -136
  4. intentumdiff/_differ_presentation.py +17 -735
  5. intentumdiff/_differ_runtime.py +13 -64
  6. intentumdiff/analysis/compile_commands.py +26 -141
  7. intentumdiff/analysis/cross_file.py +23 -25
  8. intentumdiff/analysis/diagnostics.py +1 -164
  9. intentumdiff/analysis/guardrail_reports.py +31 -33
  10. intentumdiff/analysis/guardrails.py +111 -188
  11. intentumdiff/analysis/schema_resolver.py +72 -360
  12. intentumdiff/analysis/user_schemas.py +57 -254
  13. intentumdiff/api.py +21 -0
  14. intentumdiff/cache/sqlite_store.py +5 -23
  15. intentumdiff/cli/__init__.py +59 -0
  16. intentumdiff/cli/_commands.py +243 -137
  17. intentumdiff/cli/_parser.py +51 -56
  18. intentumdiff/cli/_shared.py +151 -116
  19. intentumdiff/content_type.py +13 -30
  20. intentumdiff/core/commit_differ.py +137 -286
  21. intentumdiff/core/diffignore.py +28 -34
  22. intentumdiff/core/index.py +16 -18
  23. intentumdiff/core/indexer.py +148 -169
  24. intentumdiff/core/models.py +279 -83
  25. intentumdiff/differ.py +366 -1000
  26. intentumdiff/github_app.py +40 -27
  27. intentumdiff/intentumdiff_rust_core/intentumdiff_rust_core.dll +0 -0
  28. intentumdiff/live_server.py +197 -34
  29. intentumdiff/lsp/client.py +49 -43
  30. intentumdiff/lsp/enricher.py +19 -119
  31. intentumdiff/lsp/exceptions.py +6 -7
  32. intentumdiff/lsp/servers.py +10 -11
  33. intentumdiff/lsp_server/_codelens.py +24 -57
  34. intentumdiff/lsp_server/_diagnostics.py +23 -51
  35. intentumdiff/lsp_server/_handlers.py +67 -9
  36. intentumdiff/lsp_server/server.py +28 -8
  37. intentumdiff/plugins/adapter.py +5 -0
  38. intentumdiff/plugins/builtins.py +7 -14
  39. intentumdiff/plugins/exceptions.py +28 -0
  40. intentumdiff/plugins/hub.py +39 -43
  41. intentumdiff/plugins/loader.py +229 -215
  42. intentumdiff/plugins/registry.py +259 -207
  43. intentumdiff/plugins/registry.schema.json +156 -15
  44. intentumdiff/plugins/registry_schema.py +47 -140
  45. intentumdiff/rust_core.py +623 -1326
  46. intentumdiff/serve/_app.py +26 -35
  47. intentumdiff/sources/base.py +24 -0
  48. intentumdiff/sources/file_source.py +15 -2
  49. intentumdiff/sources/git_source.py +64 -185
  50. intentumdiff/sources/live_buffer_source.py +45 -5
  51. intentumdiff/sources/patch_source.py +36 -109
  52. intentumdiff/vcs/_safe_path.py +7 -0
  53. intentumdiff/vcs/base.py +6 -0
  54. intentumdiff/vcs/git_backend.py +1 -1
  55. intentumdiff/vcs/git_cli.py +0 -27
  56. intentumdiff/vcs/hg_backend.py +1 -1
  57. intentumdiff/vcs/perforce_backend.py +7 -1
  58. intentumdiff/vcs/svn_backend.py +1 -1
  59. intentumdiff/wasm/abap_parser.wasm +0 -0
  60. intentumdiff/wasm/adf_parser.wasm +0 -0
  61. intentumdiff/wasm/asciidoc_parser.wasm +0 -0
  62. intentumdiff/wasm/asm_parser.wasm +0 -0
  63. intentumdiff/wasm/assemblyscript_parser.wasm +0 -0
  64. intentumdiff/wasm/astro_parser.wasm +0 -0
  65. intentumdiff/wasm/bash_parser.wasm +0 -0
  66. intentumdiff/wasm/clojure_parser.wasm +0 -0
  67. intentumdiff/wasm/cmake_parser.wasm +0 -0
  68. intentumdiff/wasm/cpp_parser.wasm +0 -0
  69. intentumdiff/wasm/csharp_parser.wasm +0 -0
  70. intentumdiff/wasm/css_parser.wasm +0 -0
  71. intentumdiff/wasm/dart_parser.wasm +0 -0
  72. intentumdiff/wasm/databricks_parser.wasm +0 -0
  73. intentumdiff/wasm/dax_parser.wasm +0 -0
  74. intentumdiff/wasm/delphi_parser.wasm +0 -0
  75. intentumdiff/wasm/dockerfile_parser.wasm +0 -0
  76. intentumdiff/wasm/elixir_parser.wasm +0 -0
  77. intentumdiff/wasm/freebasic_parser.wasm +0 -0
  78. intentumdiff/wasm/generic_parser.wasm +0 -0
  79. intentumdiff/wasm/gitignore_parser.wasm +0 -0
  80. intentumdiff/wasm/go_parser.wasm +0 -0
  81. intentumdiff/wasm/gomod_parser.wasm +0 -0
  82. intentumdiff/wasm/graphql_parser.wasm +0 -0
  83. intentumdiff/wasm/groovy_parser.wasm +0 -0
  84. intentumdiff/wasm/haskell_parser.wasm +0 -0
  85. intentumdiff/wasm/html_parser.wasm +0 -0
  86. intentumdiff/wasm/html_renderer.wasm +0 -0
  87. intentumdiff/wasm/ini_parser.wasm +0 -0
  88. intentumdiff/wasm/java_parser.wasm +0 -0
  89. intentumdiff/wasm/js_ts_parser.wasm +0 -0
  90. intentumdiff/wasm/json_parser.wasm +0 -0
  91. intentumdiff/wasm/kotlin_parser.wasm +0 -0
  92. intentumdiff/wasm/latex_parser.wasm +0 -0
  93. intentumdiff/wasm/llm_renderer.wasm +0 -0
  94. intentumdiff/wasm/lua_parser.wasm +0 -0
  95. intentumdiff/wasm/make_parser.wasm +0 -0
  96. intentumdiff/wasm/markdown_parser.wasm +0 -0
  97. intentumdiff/wasm/mdx_parser.wasm +0 -0
  98. intentumdiff/wasm/ocaml_parser.wasm +0 -0
  99. intentumdiff/wasm/odin_parser.wasm +0 -0
  100. intentumdiff/wasm/parser_manifest.json +81 -2
  101. intentumdiff/wasm/patch_renderer.wasm +0 -0
  102. intentumdiff/wasm/perl_parser.wasm +0 -0
  103. intentumdiff/wasm/php_parser.wasm +0 -0
  104. intentumdiff/wasm/plsql_parser.wasm +0 -0
  105. intentumdiff/wasm/po_parser.wasm +0 -0
  106. intentumdiff/wasm/postscript_parser.wasm +0 -0
  107. intentumdiff/wasm/powershell_parser.wasm +0 -0
  108. intentumdiff/wasm/proto_parser.wasm +0 -0
  109. intentumdiff/wasm/puppet_parser.wasm +0 -0
  110. intentumdiff/wasm/python_parser.wasm +0 -0
  111. intentumdiff/wasm/qsharp_parser.wasm +0 -0
  112. intentumdiff/wasm/r_parser.wasm +0 -0
  113. intentumdiff/wasm/reasonml_parser.wasm +0 -0
  114. intentumdiff/wasm/ruby_parser.wasm +0 -0
  115. intentumdiff/wasm/rust_parser.wasm +0 -0
  116. intentumdiff/wasm/sas_parser.wasm +0 -0
  117. intentumdiff/wasm/scala_parser.wasm +0 -0
  118. intentumdiff/wasm/scss_parser.wasm +0 -0
  119. intentumdiff/wasm/sql_parser.wasm +0 -0
  120. intentumdiff/wasm/squirrel_parser.wasm +0 -0
  121. intentumdiff/wasm/svelte_parser.wasm +0 -0
  122. intentumdiff/wasm/swift_parser.wasm +0 -0
  123. intentumdiff/wasm/terminal_renderer.wasm +0 -0
  124. intentumdiff/wasm/terraform_parser.wasm +0 -0
  125. intentumdiff/wasm/toml_parser.wasm +0 -0
  126. intentumdiff/wasm/tsql_parser.wasm +0 -0
  127. intentumdiff/wasm/vbnet_parser.wasm +0 -0
  128. intentumdiff/wasm/vue_parser.wasm +0 -0
  129. intentumdiff/wasm/wasm_provenance.json +884 -0
  130. intentumdiff/wasm/wat_parser.wasm +0 -0
  131. intentumdiff/wasm/xml_parser.wasm +0 -0
  132. intentumdiff/wasm/yaml_parser.wasm +0 -0
  133. intentumdiff/wasm/zig_parser.wasm +0 -0
  134. intentumdiff/watcher.py +15 -17
  135. intentumdiff_python-0.0.3b1.dist-info/METADATA +188 -0
  136. intentumdiff_python-0.0.3b1.dist-info/RECORD +164 -0
  137. {intentumdiff_python-0.0.1.dist-info → intentumdiff_python-0.0.3b1.dist-info}/WHEEL +1 -1
  138. {intentumdiff_python-0.0.1.dist-info → intentumdiff_python-0.0.3b1.dist-info}/sboms/intentumdiff-rust-core.cyclonedx.json +905 -379
  139. intentumdiff/analysis/invariances.py +0 -1207
  140. intentumdiff/analysis/keyed_profiles.py +0 -25
  141. intentumdiff/analysis/resource_profiles.py +0 -14
  142. intentumdiff/analysis/text_review.py +0 -46
  143. intentumdiff/core/cst_serializer.py +0 -101
  144. intentumdiff/invariances/__init__.py +0 -2
  145. intentumdiff/invariances/rules.schema.json +0 -283
  146. intentumdiff/invariances/rules.yaml +0 -349
  147. intentumdiff_python-0.0.1.dist-info/METADATA +0 -112
  148. intentumdiff_python-0.0.1.dist-info/RECORD +0 -165
  149. {intentumdiff_python-0.0.1.dist-info → intentumdiff_python-0.0.3b1.dist-info}/entry_points.txt +0 -0
  150. {intentumdiff_python-0.0.1.dist-info → intentumdiff_python-0.0.3b1.dist-info}/licenses/LICENSE +0 -0
@@ -14,6 +14,7 @@ GET /api/docs — OpenAPI / Swagger UI (auto-generated by FastAPI)
14
14
  from __future__ import annotations
15
15
 
16
16
  import asyncio
17
+ import json
17
18
  import re
18
19
  from pathlib import Path
19
20
  from typing import Annotated, Any
@@ -53,6 +54,19 @@ _MAX_CANDIDATES = 50
53
54
  _MAX_CANDIDATE_BYTES = 64
54
55
 
55
56
 
57
+ def _has_lone_surrogate(body: bytes) -> bool:
58
+ """An escaped lone surrogate (``"\\ud800"``) is valid JSON but not a Unicode string (#316)."""
59
+ if not re.search(rb"\\u[dD][89a-fA-F]", body):
60
+ return False
61
+ try:
62
+ json.dumps(json.loads(body), ensure_ascii=False).encode("utf-8")
63
+ except UnicodeEncodeError:
64
+ return True
65
+ except ValueError: # not JSON at all: FastAPI's own validation answers it
66
+ pass
67
+ return False
68
+
69
+
56
70
  def _request_body_too_large_response() -> Response:
57
71
  limit_mb = _MAX_REQUEST_BODY_BYTES // (1024 * 1024)
58
72
  return Response(
@@ -309,17 +323,13 @@ def create_app(
309
323
  return _request_body_too_large_response()
310
324
  chunks.append(chunk)
311
325
  body = b"".join(chunks)
312
- sent = False
313
-
314
- async def receive(): # type: ignore[no-untyped-def]
315
- nonlocal sent
316
- if sent:
317
- return {"type": "http.request", "body": b"", "more_body": False}
318
- sent = True
319
- return {"type": "http.request", "body": body, "more_body": False}
320
-
326
+ # BaseHTTPMiddleware replays the cached body through wrapped_receive.
327
+ # Preserve the original receive callable so its next event is the real
328
+ # upstream disconnect, rather than a second replayed request.
321
329
  request._body = body # type: ignore[attr-defined]
322
- request._receive = receive # type: ignore[attr-defined]
330
+ if _has_lone_surrogate(body): # rejected before validation, so no engine dispatch
331
+ return Response('{"detail": "strings must not contain lone surrogates"}',
332
+ status_code=422, media_type="application/json")
323
333
  return await call_next(request)
324
334
 
325
335
  if cors_origins is not None:
@@ -357,12 +367,12 @@ def create_app(
357
367
  @app.get("/languages", summary="List supported languages")
358
368
  async def languages() -> dict[str, list[str]]:
359
369
  """Return all language IDs accepted by the ``/diff`` endpoint."""
360
- return {"languages": _supported_languages(_differ)}
370
+ return {"languages": await asyncio.to_thread(_supported_languages, _differ)} # #320
361
371
 
362
372
  @app.get("/language-info", summary="List language and parser plugin metadata")
363
373
  async def language_info() -> dict[str, list[dict[str, Any]]]:
364
374
  """Return grouped language/plugin metadata for the playground UI."""
365
- return {"languages": _language_info(_differ)}
375
+ return {"languages": await asyncio.to_thread(_language_info, _differ)}
366
376
 
367
377
  @app.post("/diff", summary="Compute semantic diff")
368
378
  async def diff(req: DiffRequest) -> Response:
@@ -396,10 +406,7 @@ def create_app(
396
406
  detail=f"Unknown language: {exc}",
397
407
  ) from exc
398
408
  except GrammarNotInstalledError as exc:
399
- raise HTTPException(
400
- status_code=422,
401
- detail=str(exc),
402
- ) from exc
409
+ raise HTTPException(status_code=422, detail=str(exc)) from exc
403
410
  return Response(
404
411
  content=result.model_dump_json(),
405
412
  media_type="application/json",
@@ -415,26 +422,10 @@ def create_app(
415
422
  regex heuristics in Python. The response is stateless.
416
423
  """
417
424
  try:
425
+ args = (req.content, req.candidates, req.preferred_plugins, req.plugin_id)
426
+ count = 4 if req.plugin_id else 3 if req.preferred_plugins else 2
418
427
  async with _sem:
419
- if req.plugin_id:
420
- results = await asyncio.to_thread(
421
- _differ.detect_all,
422
- req.content,
423
- req.candidates,
424
- req.preferred_plugins,
425
- req.plugin_id,
426
- )
427
- elif req.preferred_plugins:
428
- results = await asyncio.to_thread(
429
- _differ.detect_all,
430
- req.content,
431
- req.candidates,
432
- req.preferred_plugins,
433
- )
434
- else:
435
- results = await asyncio.to_thread(
436
- _differ.detect_all, req.content, req.candidates
437
- )
428
+ results = await asyncio.to_thread(_differ.detect_all, *args[:count])
438
429
  except PluginNotFoundError as exc:
439
430
  raise HTTPException(
440
431
  status_code=422,
@@ -26,3 +26,27 @@ class Source(ABC):
26
26
  ``language_hint`` — e.g. "python", "sql". Pass None to auto-detect.
27
27
  """
28
28
  ...
29
+
30
+ def guardrail_policy_paths(self) -> tuple[str | None, str | None]:
31
+ """Return host filesystem paths for old/new guardrail policy discovery.
32
+
33
+ Source display labels are NOT physical policy identities. The default
34
+ has no filesystem context; adapters for real files/repos override it.
35
+ Rust alone interprets policy rules and evaluates semantic violations.
36
+ """
37
+ return None, None
38
+
39
+ def display_names(self) -> tuple[str, str] | None:
40
+ """
41
+ Distinct ``(old, new)`` names, when the two sides are named differently.
42
+
43
+ ``get_content`` returns ONE filename because that is what language detection
44
+ needs, and for most sources it is also the right thing to display: a git diff
45
+ compares one path at two revisions, so both sides share a name.
46
+
47
+ ``FileSource`` is the exception — it compares two separately named files — and
48
+ collapsing both sides onto the new name made the CLI report that it had diffed
49
+ a file with itself. Sources that know two names override this; ``None`` means
50
+ "one name applies to both", which stays the default.
51
+ """
52
+ return None
@@ -43,6 +43,19 @@ class FileSource(Source):
43
43
  self._filename = filename or self._new_path.name
44
44
 
45
45
  def get_content(self) -> tuple[str, str, str, str | None]:
46
- old_content = self._old_path.read_text(encoding="utf-8", errors="replace")
47
- new_content = self._new_path.read_text(encoding="utf-8", errors="replace")
46
+ old_content = self._old_path.read_text(encoding="utf-8", errors="strict")
47
+ new_content = self._new_path.read_text(encoding="utf-8", errors="strict")
48
48
  return old_content, new_content, self._filename, self._language_hint
49
+
50
+ def guardrail_policy_paths(self) -> tuple[str, str]:
51
+ """Preserve the two real file locations independently of display aliases."""
52
+ return str(self._old_path), str(self._new_path)
53
+
54
+ def display_names(self) -> tuple[str, str] | None:
55
+ # This source is the one that genuinely has two names. An explicit ``filename``
56
+ # overrides both, and identical basenames need no distinction.
57
+ if self._filename != self._new_path.name:
58
+ return None
59
+ if self._old_path.name == self._new_path.name:
60
+ return None
61
+ return self._old_path.name, self._new_path.name
@@ -13,14 +13,15 @@ path-traversal attacks before any I/O is performed.
13
13
  from __future__ import annotations
14
14
 
15
15
  import os
16
- import subprocess
17
16
  from collections.abc import Iterator
18
- from pathlib import Path
19
17
 
20
18
  from intentumdiff import content_type as _content_type
21
19
  from intentumdiff.sources.base import Source
22
- from intentumdiff.vcs._safe_path import safe_relative_path, safe_working_tree_path
23
- from intentumdiff.vcs.git_cli import resolve_repo_root, run_git_bytes
20
+ from intentumdiff.vcs._safe_path import safe_policy_path, safe_relative_path, safe_working_tree_path
21
+ from intentumdiff.vcs.git_cli import (
22
+ resolve_repo_root,
23
+ run_git_bytes,
24
+ )
24
25
 
25
26
 
26
27
  def _decode_text_or_none(data: bytes) -> str | None:
@@ -32,7 +33,7 @@ def _decode_text_or_none(data: bytes) -> str | None:
32
33
  """
33
34
  if not _content_type.is_text_bytes(data):
34
35
  return None
35
- return data.decode("utf-8", errors="replace")
36
+ return data.decode("utf-8", errors="strict")
36
37
 
37
38
 
38
39
  def _safe_file_path(file_path: str) -> str:
@@ -45,191 +46,11 @@ def _safe_file_path(file_path: str) -> str:
45
46
  return safe_relative_path(file_path)
46
47
 
47
48
 
48
- # Change-type codes that produce binary-safe text blobs on both sides.
49
- _TEXT_CHANGE_TYPES = frozenset({"A", "D", "M", "R", "C"})
50
-
51
-
52
49
  # Sentinel new_ref values recognised by iter_changed_sources.
53
50
  _REF_STAGED = ":staged" # compare HEAD to the git index (staged files only)
54
51
  _REF_UNPUSHED = ":unpushed" # compare remote tracking branch to HEAD (unpushed commits)
55
52
 
56
53
 
57
- _ChangedSource = tuple[str, str, str, str, str | None]
58
-
59
-
60
- def _is_python_path(path: str) -> bool:
61
- return path.lower().endswith((".py", ".pyi"))
62
-
63
-
64
- def _decode_git_path(path: bytes) -> str:
65
- return path.decode("utf-8", errors="replace")
66
-
67
-
68
- def _untracked_paths(repo_path: str) -> list[str]:
69
- data = run_git_bytes(repo_path, ["ls-files", "--others", "--exclude-standard", "-z"])
70
- return sorted(_decode_git_path(token) for token in data.split(b"\0") if token)
71
-
72
-
73
- def _read_working_tree_text(path: Path) -> str | None:
74
- try:
75
- data = path.read_bytes()
76
- except OSError:
77
- return None
78
- # Content-based routing: binary/image assets are skipped (reviewed via the
79
- # perceptual asset diff), never decoded and sent to the text engine.
80
- return _decode_text_or_none(data)
81
-
82
-
83
- def _parse_name_status_z(data: bytes) -> list[tuple[str, str, str]]:
84
- tokens = [token for token in data.split(b"\0") if token]
85
- entries: list[tuple[str, str, str]] = []
86
- idx = 0
87
- while idx < len(tokens):
88
- status = tokens[idx].decode("ascii", errors="replace")
89
- idx += 1
90
- code = status[:1]
91
- if code in {"R", "C"}:
92
- if idx + 1 >= len(tokens):
93
- break
94
- old_path = _decode_git_path(tokens[idx])
95
- new_path = _decode_git_path(tokens[idx + 1])
96
- idx += 2
97
- else:
98
- if idx >= len(tokens):
99
- break
100
- old_path = _decode_git_path(tokens[idx])
101
- new_path = old_path
102
- idx += 1
103
- entries.append((code, old_path, new_path))
104
- return entries
105
-
106
-
107
- def _read_commit_paths_batch(
108
- repo_path: str,
109
- rev: str,
110
- paths: list[str],
111
- ) -> dict[str, str]:
112
- unique_paths = list(dict.fromkeys(paths))
113
- if not unique_paths:
114
- return {}
115
- if any("\n" in path or "\r" in path for path in unique_paths):
116
- raise ValueError("git cat-file batch path contains a newline")
117
-
118
- request = "".join(f"{rev}:{path}\n" for path in unique_paths).encode("utf-8")
119
- output = run_git_bytes(repo_path, ["cat-file", "--batch"], input_bytes=request)
120
- result: dict[str, str] = {}
121
- pos = 0
122
- for path in unique_paths:
123
- header_end = output.find(b"\n", pos)
124
- if header_end < 0:
125
- raise ValueError("malformed git cat-file batch output")
126
- header = output[pos:header_end]
127
- pos = header_end + 1
128
- if header.endswith(b" missing"):
129
- result[path] = ""
130
- continue
131
- parts = header.split()
132
- if len(parts) < 3:
133
- raise ValueError("malformed git cat-file batch header")
134
- try:
135
- size = int(parts[2])
136
- except ValueError as exc:
137
- raise ValueError("malformed git cat-file batch size") from exc
138
- content = output[pos : pos + size]
139
- if len(content) != size:
140
- raise ValueError("truncated git cat-file batch content")
141
- pos += size
142
- if pos < len(output) and output[pos : pos + 1] == b"\n":
143
- pos += 1
144
- result[path] = content.decode("utf-8", errors="replace")
145
- return result
146
-
147
-
148
- def collect_working_tree_python_sources_fast(
149
- repo_path: "str | os.PathLike[str]",
150
- old_ref: str,
151
- ) -> "tuple[list[_ChangedSource], str | None] | None":
152
- """
153
- Fast working-tree collector for the certified Rust Python batch path.
154
-
155
- Returns ``None`` when the conservative Git CLI path cannot be used and the
156
- caller should fall back to ``iter_changed_sources``. A non-empty fallback
157
- reason means the diff contains a non-Python changed file, so the certified
158
- Python-only path must not run.
159
- """
160
- try:
161
- working_dir = resolve_repo_root(repo_path)
162
- # git resolves the ref on each call; using it directly (vs a pre-resolved
163
- # sha) is equivalent here and avoids an extra round-trip.
164
- old_rev = old_ref
165
- diff_output = run_git_bytes(working_dir, ["diff", "--name-status", "-z", old_rev])
166
- entries = [
167
- entry
168
- for entry in _parse_name_status_z(diff_output)
169
- if entry[0] in _TEXT_CHANGE_TYPES
170
- ]
171
- untracked_paths = _untracked_paths(working_dir)
172
- if not entries and not untracked_paths:
173
- return [], None
174
- for _code, old_path, new_path in entries:
175
- if not (_is_python_path(old_path) or _is_python_path(new_path)):
176
- return (
177
- [],
178
- "certified commit JSON requires all changed files to be Python",
179
- )
180
- for untracked_path in untracked_paths:
181
- if not _is_python_path(untracked_path):
182
- return (
183
- [],
184
- "certified commit JSON requires all changed files to be Python",
185
- )
186
-
187
- staged_output = run_git_bytes(
188
- working_dir,
189
- ["diff", "--cached", "--name-only", "-z", old_rev],
190
- )
191
- staged_paths = {
192
- _decode_git_path(token)
193
- for token in staged_output.split(b"\0")
194
- if token
195
- }
196
- old_paths = [old_path for code, old_path, _new_path in entries if code != "A"]
197
- old_contents = _read_commit_paths_batch(working_dir, old_rev, old_paths)
198
- sources: list[_ChangedSource] = []
199
- root = Path(working_dir)
200
- for code, old_path, new_path in entries:
201
- _safe_file_path(old_path)
202
- _safe_file_path(new_path)
203
- old_content = "" if code == "A" else old_contents.get(old_path, "")
204
- if code == "D":
205
- new_content = ""
206
- else:
207
- disk_path = safe_working_tree_path(root, new_path)
208
- if not disk_path.exists():
209
- new_content = ""
210
- else:
211
- new_content = disk_path.read_text(
212
- encoding="utf-8",
213
- errors="replace",
214
- )
215
- staging_status = (
216
- "staged"
217
- if old_path in staged_paths or new_path in staged_paths
218
- else "unstaged"
219
- )
220
- sources.append((old_content, new_content, old_path, new_path, staging_status))
221
- for untracked_path in untracked_paths:
222
- _safe_file_path(untracked_path)
223
- disk_path = safe_working_tree_path(root, untracked_path)
224
- new_content = _read_working_tree_text(disk_path)
225
- if new_content is None:
226
- continue
227
- sources.append(("", new_content, untracked_path, untracked_path, "untracked"))
228
- return sources, None
229
- except (OSError, subprocess.SubprocessError, ValueError):
230
- return None
231
-
232
-
233
54
  def iter_changed_sources(
234
55
  repo_path: "str | os.PathLike[str]",
235
56
  old_ref: str,
@@ -308,6 +129,17 @@ class GitSource(Source):
308
129
  )
309
130
  return old_content, new_content, self._file_path, self._language_hint
310
131
 
132
+ def guardrail_policy_paths(self) -> tuple[str, str]:
133
+ """Use repository policy context without inspecting a historical blob's live target.
134
+
135
+ Commit-to-commit Git data comes from object storage. A checkout may
136
+ meanwhile replace the tracked file with an external symlink; following
137
+ that unrelated final component must not invalidate historical review.
138
+ Resolve and validate only its parent (where policy discovery begins).
139
+ """
140
+ path = safe_policy_path(self._repo_path, self._file_path)
141
+ return path, path
142
+
311
143
  @classmethod
312
144
  def working_tree(
313
145
  cls,
@@ -331,6 +163,48 @@ class GitSource(Source):
331
163
  )
332
164
 
333
165
 
166
+ class StagedSource(GitSource):
167
+ """Select one file from Rust's index comparison, preserving rename paths."""
168
+
169
+ def __init__(self, repo_path: str | os.PathLike[str], file_path: str,
170
+ ref: str = "HEAD") -> None:
171
+ super().__init__(repo_path, file_path, old_ref=ref, new_ref=_REF_STAGED)
172
+ self._old_path = self._file_path
173
+ self._new_path = self._file_path
174
+
175
+ def get_content(self) -> tuple[str, str, str, str | None]:
176
+ from intentumdiff import rust_core
177
+
178
+ sources = list(iter_changed_sources(self._repo_path, self._old_ref, _REF_STAGED))
179
+ # Prefer the destination when a path is both a rename source and target.
180
+ selected = next((row for row in sources if row[3] == self._file_path), None)
181
+ if selected is None:
182
+ selected = next((row for row in sources if row[2] == self._file_path), None)
183
+ if selected is not None:
184
+ old, new, self._old_path, self._new_path, _status = selected
185
+ return old, new, self._new_path, self._language_hint
186
+ # The text inventory also omits binary entries. Read the actual index
187
+ # bytes before treating an absent row as an unchanged text source.
188
+ self._old_path = self._new_path = self._file_path
189
+ index_bytes = run_git_bytes(
190
+ self._repo_path, ["cat-file", "blob", f":{self._file_path}"]
191
+ )
192
+ new = _decode_text_or_none(index_bytes)
193
+ if new is None:
194
+ raise ValueError(f"Staged source {self._file_path!r} is not text")
195
+ old, _ = rust_core.git_source_content(
196
+ self._repo_path, self._file_path, self._old_ref, self._old_ref
197
+ )
198
+ return old, new, self._file_path, self._language_hint
199
+
200
+ def display_names(self) -> tuple[str, str]:
201
+ return self._old_path, self._new_path
202
+
203
+ def guardrail_policy_paths(self) -> tuple[str, str]:
204
+ return (safe_policy_path(self._repo_path, self._old_path),
205
+ safe_policy_path(self._repo_path, self._new_path))
206
+
207
+
334
208
  class WorkingTreeSource(Source):
335
209
  """
336
210
  Compare the committed version of a file against the current working tree.
@@ -373,3 +247,8 @@ class WorkingTreeSource(Source):
373
247
  self._repo_path, self._file_path, self._ref
374
248
  )
375
249
  return old_content, new_content, self._file_path, self._language_hint
250
+
251
+ def guardrail_policy_paths(self) -> tuple[str, str]:
252
+ """Locate working-tree guardrails in the actual repository root."""
253
+ path = str(safe_working_tree_path(self._repo_path, self._file_path))
254
+ return path, path
@@ -18,7 +18,7 @@ from pathlib import Path, PurePosixPath
18
18
  from typing import TYPE_CHECKING
19
19
 
20
20
  from intentumdiff.sources.base import Source
21
- from intentumdiff.vcs._safe_path import safe_relative_path
21
+ from intentumdiff.vcs._safe_path import safe_relative_path, safe_working_tree_path
22
22
  from intentumdiff.vcs.git_cli import resolve_repo_root, run_git_bytes
23
23
 
24
24
  if TYPE_CHECKING:
@@ -62,6 +62,7 @@ class LiveBufferSource(Source):
62
62
  ref: str = "HEAD",
63
63
  language_hint: str | None = None,
64
64
  edit_deltas: "list[EditDelta] | None" = None,
65
+ cache_snapshot: bool = False,
65
66
  ) -> None:
66
67
  # Validate the file path before storing it.
67
68
  safe = PurePosixPath(file_path)
@@ -77,6 +78,8 @@ class LiveBufferSource(Source):
77
78
  self._ref = ref
78
79
  self._language_hint = language_hint
79
80
  self.edit_deltas: list[EditDelta] | None = edit_deltas
81
+ self._cache_snapshot = cache_snapshot
82
+ self._cached_old_content: str | None = None
80
83
 
81
84
  # ------------------------------------------------------------------
82
85
  # Source interface
@@ -88,10 +91,21 @@ class LiveBufferSource(Source):
88
91
  *old_content* is read from the committed blob at ``self._ref``.
89
92
  *new_content* is the in-memory buffer ``live_content``.
90
93
  """
91
- old_content = self._read_committed()
94
+ if self._cache_snapshot:
95
+ if self._cached_old_content is None:
96
+ self._cached_old_content = self._read_committed()
97
+ old_content = self._cached_old_content
98
+ else:
99
+ old_content = self._read_committed()
92
100
  filename = Path(self._file_path).name
93
101
  return old_content, self._live_content, filename, self._language_hint
94
102
 
103
+ def guardrail_policy_paths(self) -> tuple[str, str]:
104
+ """Use the selected repository, not process cwd, for buffer policy I/O."""
105
+ root = resolve_repo_root(self._repo_path)
106
+ path = str(safe_working_tree_path(root, self._file_path))
107
+ return path, path
108
+
95
109
  # ------------------------------------------------------------------
96
110
  # Internal helpers
97
111
  # ------------------------------------------------------------------
@@ -107,7 +121,33 @@ class LiveBufferSource(Source):
107
121
  data = run_git_bytes(
108
122
  root, ["cat-file", "blob", f"{self._ref}:{self._file_path}"]
109
123
  )
110
- except subprocess.CalledProcessError:
111
- # File not yet tracked at this ref — treat old side as empty.
124
+ except subprocess.CalledProcessError as exc:
125
+ # A missing file at a valid ref is a legitimate addition. An invalid
126
+ # ref or a missing/corrupted tracked blob is not: returning an empty
127
+ # baseline for either would manufacture a successful semantic diff
128
+ # against the wrong source (#229). Check the Git tree independently.
129
+ #
130
+ # Use a literal pathspec and -z output: glob characters, spaces and
131
+ # non-ASCII path bytes must not turn a tracked file into an addition.
132
+ entries = run_git_bytes(
133
+ root,
134
+ [
135
+ "ls-tree", "-r", "-z", "--full-tree",
136
+ self._ref, "--", f":(literal){self._file_path}",
137
+ ],
138
+ )
139
+ expected_path = self._file_path.encode("utf-8", errors="surrogateescape")
140
+ tracked = any(
141
+ entry.partition(b"\t")[2] == expected_path
142
+ for entry in entries.split(b"\0")
143
+ if entry
144
+ )
145
+ if tracked:
146
+ raise RuntimeError(
147
+ f"Failed to read tracked Git blob {self._ref}:{self._file_path}"
148
+ ) from exc
112
149
  return ""
113
- return data.decode("utf-8", errors="replace")
150
+ # Git can return a tracked blob whose bytes are not valid UTF-8.
151
+ # Replacing invalid bytes would invent an old source and falsely
152
+ # certify a semantic review against content Git never stored (#231).
153
+ return data.decode("utf-8")