intentumdiff-python 0.0.1__py3-none-macosx_11_0_arm64.whl

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (165) hide show
  1. intentumdiff/__init__.py +134 -0
  2. intentumdiff/_differ_gate.py +210 -0
  3. intentumdiff/_differ_presentation.py +786 -0
  4. intentumdiff/_differ_runtime.py +320 -0
  5. intentumdiff/analysis/__init__.py +1 -0
  6. intentumdiff/analysis/compile_commands.py +151 -0
  7. intentumdiff/analysis/cross_file.py +71 -0
  8. intentumdiff/analysis/diagnostics.py +309 -0
  9. intentumdiff/analysis/guardrail_reports.py +254 -0
  10. intentumdiff/analysis/guardrails.py +318 -0
  11. intentumdiff/analysis/invariances.py +1207 -0
  12. intentumdiff/analysis/keyed_profiles.py +25 -0
  13. intentumdiff/analysis/resource_profiles.py +14 -0
  14. intentumdiff/analysis/schema_resolver.py +816 -0
  15. intentumdiff/analysis/text_review.py +46 -0
  16. intentumdiff/analysis/user_schemas.py +430 -0
  17. intentumdiff/assets.py +45 -0
  18. intentumdiff/cache/__init__.py +37 -0
  19. intentumdiff/cache/duckdb_store.py +98 -0
  20. intentumdiff/cache/sqlite_store.py +149 -0
  21. intentumdiff/cache/store.py +190 -0
  22. intentumdiff/cli/__init__.py +199 -0
  23. intentumdiff/cli/_commands.py +2141 -0
  24. intentumdiff/cli/_parser.py +1061 -0
  25. intentumdiff/cli/_shared.py +644 -0
  26. intentumdiff/content_type.py +59 -0
  27. intentumdiff/core/__init__.py +1 -0
  28. intentumdiff/core/commit_differ.py +599 -0
  29. intentumdiff/core/config.py +51 -0
  30. intentumdiff/core/cst_serializer.py +101 -0
  31. intentumdiff/core/diffignore.py +87 -0
  32. intentumdiff/core/index.py +189 -0
  33. intentumdiff/core/indexer.py +792 -0
  34. intentumdiff/core/models.py +837 -0
  35. intentumdiff/differ.py +2195 -0
  36. intentumdiff/github_app.py +287 -0
  37. intentumdiff/intentumdiff_rust_core/__init__.py +7 -0
  38. intentumdiff/intentumdiff_rust_core/ffi.py +8 -0
  39. intentumdiff/intentumdiff_rust_core/libintentumdiff_rust_core.dylib +0 -0
  40. intentumdiff/invariances/__init__.py +2 -0
  41. intentumdiff/invariances/rules.schema.json +283 -0
  42. intentumdiff/invariances/rules.yaml +349 -0
  43. intentumdiff/live_server.py +1314 -0
  44. intentumdiff/lsp/__init__.py +67 -0
  45. intentumdiff/lsp/client.py +544 -0
  46. intentumdiff/lsp/config.py +96 -0
  47. intentumdiff/lsp/enricher.py +227 -0
  48. intentumdiff/lsp/exceptions.py +27 -0
  49. intentumdiff/lsp/launcher.py +273 -0
  50. intentumdiff/lsp/servers.py +356 -0
  51. intentumdiff/lsp_server/__init__.py +14 -0
  52. intentumdiff/lsp_server/_codelens.py +67 -0
  53. intentumdiff/lsp_server/_diagnostics.py +62 -0
  54. intentumdiff/lsp_server/_handlers.py +175 -0
  55. intentumdiff/lsp_server/server.py +205 -0
  56. intentumdiff/plugins/__init__.py +1 -0
  57. intentumdiff/plugins/adapter.py +397 -0
  58. intentumdiff/plugins/builtins.py +323 -0
  59. intentumdiff/plugins/exceptions.py +93 -0
  60. intentumdiff/plugins/hub.py +1155 -0
  61. intentumdiff/plugins/language_metadata.py +250 -0
  62. intentumdiff/plugins/loader.py +1252 -0
  63. intentumdiff/plugins/registry.py +1079 -0
  64. intentumdiff/plugins/registry.schema.json +99 -0
  65. intentumdiff/plugins/registry_schema.py +280 -0
  66. intentumdiff/plugins/wit/plugin.wit +351 -0
  67. intentumdiff/rust_core.py +2370 -0
  68. intentumdiff/serve/__init__.py +19 -0
  69. intentumdiff/serve/_app.py +485 -0
  70. intentumdiff/serve/_static/index.html +326 -0
  71. intentumdiff/serve/_static/playground.js +567 -0
  72. intentumdiff/sources/__init__.py +1 -0
  73. intentumdiff/sources/base.py +28 -0
  74. intentumdiff/sources/file_source.py +48 -0
  75. intentumdiff/sources/git_source.py +375 -0
  76. intentumdiff/sources/live_buffer_source.py +113 -0
  77. intentumdiff/sources/patch_source.py +126 -0
  78. intentumdiff/sources/string_source.py +44 -0
  79. intentumdiff/testing.py +165 -0
  80. intentumdiff/vcs/__init__.py +37 -0
  81. intentumdiff/vcs/_safe_path.py +50 -0
  82. intentumdiff/vcs/base.py +136 -0
  83. intentumdiff/vcs/git_backend.py +97 -0
  84. intentumdiff/vcs/git_cli.py +100 -0
  85. intentumdiff/vcs/hg_backend.py +94 -0
  86. intentumdiff/vcs/perforce_backend.py +109 -0
  87. intentumdiff/vcs/svn_backend.py +105 -0
  88. intentumdiff/wasm/.gitkeep +2 -0
  89. intentumdiff/wasm/abap_parser.wasm +0 -0
  90. intentumdiff/wasm/adf_parser.wasm +0 -0
  91. intentumdiff/wasm/asciidoc_parser.wasm +0 -0
  92. intentumdiff/wasm/asm_parser.wasm +0 -0
  93. intentumdiff/wasm/assemblyscript_parser.wasm +0 -0
  94. intentumdiff/wasm/astro_parser.wasm +0 -0
  95. intentumdiff/wasm/bash_parser.wasm +0 -0
  96. intentumdiff/wasm/clojure_parser.wasm +0 -0
  97. intentumdiff/wasm/cmake_parser.wasm +0 -0
  98. intentumdiff/wasm/cpp_parser.wasm +0 -0
  99. intentumdiff/wasm/csharp_parser.wasm +0 -0
  100. intentumdiff/wasm/css_parser.wasm +0 -0
  101. intentumdiff/wasm/dart_parser.wasm +0 -0
  102. intentumdiff/wasm/databricks_parser.wasm +0 -0
  103. intentumdiff/wasm/dax_parser.wasm +0 -0
  104. intentumdiff/wasm/delphi_parser.wasm +0 -0
  105. intentumdiff/wasm/dockerfile_parser.wasm +0 -0
  106. intentumdiff/wasm/elixir_parser.wasm +0 -0
  107. intentumdiff/wasm/freebasic_parser.wasm +0 -0
  108. intentumdiff/wasm/generic_parser.wasm +0 -0
  109. intentumdiff/wasm/gitignore_parser.wasm +0 -0
  110. intentumdiff/wasm/go_parser.wasm +0 -0
  111. intentumdiff/wasm/gomod_parser.wasm +0 -0
  112. intentumdiff/wasm/graphql_parser.wasm +0 -0
  113. intentumdiff/wasm/groovy_parser.wasm +0 -0
  114. intentumdiff/wasm/haskell_parser.wasm +0 -0
  115. intentumdiff/wasm/html_parser.wasm +0 -0
  116. intentumdiff/wasm/ini_parser.wasm +0 -0
  117. intentumdiff/wasm/java_parser.wasm +0 -0
  118. intentumdiff/wasm/js_ts_parser.wasm +0 -0
  119. intentumdiff/wasm/json_parser.wasm +0 -0
  120. intentumdiff/wasm/kotlin_parser.wasm +0 -0
  121. intentumdiff/wasm/latex_parser.wasm +0 -0
  122. intentumdiff/wasm/lua_parser.wasm +0 -0
  123. intentumdiff/wasm/make_parser.wasm +0 -0
  124. intentumdiff/wasm/markdown_parser.wasm +0 -0
  125. intentumdiff/wasm/mdx_parser.wasm +0 -0
  126. intentumdiff/wasm/ocaml_parser.wasm +0 -0
  127. intentumdiff/wasm/odin_parser.wasm +0 -0
  128. intentumdiff/wasm/parser_manifest.json +590 -0
  129. intentumdiff/wasm/perl_parser.wasm +0 -0
  130. intentumdiff/wasm/php_parser.wasm +0 -0
  131. intentumdiff/wasm/plsql_parser.wasm +0 -0
  132. intentumdiff/wasm/po_parser.wasm +0 -0
  133. intentumdiff/wasm/postscript_parser.wasm +0 -0
  134. intentumdiff/wasm/powershell_parser.wasm +0 -0
  135. intentumdiff/wasm/proto_parser.wasm +0 -0
  136. intentumdiff/wasm/puppet_parser.wasm +0 -0
  137. intentumdiff/wasm/python_parser.wasm +0 -0
  138. intentumdiff/wasm/qsharp_parser.wasm +0 -0
  139. intentumdiff/wasm/r_parser.wasm +0 -0
  140. intentumdiff/wasm/reasonml_parser.wasm +0 -0
  141. intentumdiff/wasm/ruby_parser.wasm +0 -0
  142. intentumdiff/wasm/rust_parser.wasm +0 -0
  143. intentumdiff/wasm/sas_parser.wasm +0 -0
  144. intentumdiff/wasm/scala_parser.wasm +0 -0
  145. intentumdiff/wasm/scss_parser.wasm +0 -0
  146. intentumdiff/wasm/sql_parser.wasm +0 -0
  147. intentumdiff/wasm/squirrel_parser.wasm +0 -0
  148. intentumdiff/wasm/svelte_parser.wasm +0 -0
  149. intentumdiff/wasm/swift_parser.wasm +0 -0
  150. intentumdiff/wasm/terraform_parser.wasm +0 -0
  151. intentumdiff/wasm/toml_parser.wasm +0 -0
  152. intentumdiff/wasm/tsql_parser.wasm +0 -0
  153. intentumdiff/wasm/vbnet_parser.wasm +0 -0
  154. intentumdiff/wasm/vue_parser.wasm +0 -0
  155. intentumdiff/wasm/wat_parser.wasm +0 -0
  156. intentumdiff/wasm/xml_parser.wasm +0 -0
  157. intentumdiff/wasm/yaml_parser.wasm +0 -0
  158. intentumdiff/wasm/zig_parser.wasm +0 -0
  159. intentumdiff/watcher.py +259 -0
  160. intentumdiff_python-0.0.1.dist-info/METADATA +112 -0
  161. intentumdiff_python-0.0.1.dist-info/RECORD +165 -0
  162. intentumdiff_python-0.0.1.dist-info/WHEEL +4 -0
  163. intentumdiff_python-0.0.1.dist-info/entry_points.txt +80 -0
  164. intentumdiff_python-0.0.1.dist-info/licenses/LICENSE +21 -0
  165. intentumdiff_python-0.0.1.dist-info/sboms/intentumdiff-rust-core.cyclonedx.json +10968 -0
@@ -0,0 +1,320 @@
1
+ """File lifecycle, phase profiling, fuel and telemetry helpers for intentumdiff.differ."""
2
+
3
+ from __future__ import annotations
4
+
5
+ import logging
6
+ import time
7
+ from collections.abc import Iterator
8
+ from contextlib import contextmanager
9
+ from typing import Any
10
+
11
+ from intentumdiff.analysis.diagnostics import (
12
+ DiagnosticsRecorder,
13
+ )
14
+ from intentumdiff.core.models import (
15
+ ChangeGroupKind,
16
+ SemanticDiff,
17
+ )
18
+
19
+ logger = logging.getLogger(__name__)
20
+
21
+ _ADDED_FILE_STATUSES = frozenset({"added", "add", "a", "new", "untracked"})
22
+ _DELETED_FILE_STATUSES = frozenset({"deleted", "delete", "d", "removed", "remove"})
23
+
24
+
25
+ def _infer_file_lifecycle(
26
+ old_source: str,
27
+ new_source: str,
28
+ staging_status: str | None = None,
29
+ ) -> str:
30
+ """Infer the file lifecycle from source presence and git status facts.
31
+
32
+ This is shell-owned source metadata, not semantic classification. The engine
33
+ still owns the raw additions/deletions and review groups.
34
+ """
35
+
36
+ status = (staging_status or "").strip().lower()
37
+ if status in _ADDED_FILE_STATUSES:
38
+ return "added"
39
+ if status in _DELETED_FILE_STATUSES:
40
+ return "deleted"
41
+ if old_source == "" and new_source != "":
42
+ return "added"
43
+ if old_source != "" and new_source == "":
44
+ return "deleted"
45
+ return "modified"
46
+
47
+
48
+ def _apply_file_lifecycle_to_diff(
49
+ diff: SemanticDiff,
50
+ lifecycle: str,
51
+ ) -> SemanticDiff:
52
+ metadata = dict(diff.metadata)
53
+ metadata["file_lifecycle"] = lifecycle
54
+ if lifecycle == "modified":
55
+ return diff.model_copy(update={"metadata": metadata})
56
+
57
+ change_groups = [
58
+ group for group in diff.change_groups if group.kind != ChangeGroupKind.IGNORED_STYLE
59
+ ]
60
+ return diff.model_copy(
61
+ update={
62
+ "metadata": metadata,
63
+ "change_groups": change_groups,
64
+ "is_style_only": False,
65
+ "has_semantic_changes": bool(diff.changes),
66
+ }
67
+ )
68
+
69
+
70
+ class _PhaseProfiler:
71
+ """Tiny opt-in wall-clock phase timer used by benchmark/debug paths."""
72
+
73
+ def __init__(self, enabled: bool) -> None:
74
+ self.enabled = enabled
75
+ self._started = time.perf_counter()
76
+ self._phases: list[dict[str, Any]] = []
77
+
78
+ @contextmanager
79
+ def phase(self, name: str) -> Iterator[None]:
80
+ if not self.enabled:
81
+ yield
82
+ return
83
+ started = time.perf_counter()
84
+ try:
85
+ yield
86
+ finally:
87
+ self.record(name, (time.perf_counter() - started) * 1000)
88
+
89
+ def record(self, name: str, duration_ms: float, *, shared: bool = False) -> None:
90
+ if not self.enabled:
91
+ return
92
+ entry: dict[str, Any] = {
93
+ "name": name,
94
+ "duration_ms": round(duration_ms, 3),
95
+ }
96
+ if shared:
97
+ entry["shared"] = True
98
+ self._phases.append(entry)
99
+
100
+ def snapshot(self) -> dict[str, Any]:
101
+ total_ms = (time.perf_counter() - self._started) * 1000
102
+ return {
103
+ "schema_version": 1,
104
+ "total_ms": round(total_ms, 3),
105
+ "phases": list(self._phases),
106
+ }
107
+
108
+
109
+ _DEFAULT_PLUGIN_FUEL = 100_000_000
110
+ _EXPLICIT_FUEL_EXHAUSTION_TEST_CAP = 1_000_000
111
+ # Hotspot thresholds are calibrated to the measured worst legitimate parser plus ~30%
112
+ # headroom. Two effects moved the floor in 2026-07: literal-container text capture in the
113
+ # tree-sitter converters (issue #46 — a deliberate correctness cost), and whole-binary LTO
114
+ # (lto=true, codegen-units=1) letting ANY crate change reshuffle the tree-sitter parse
115
+ # loop's codegen by ±10-15% between rebuilds. Measured post-rebuild worst cases:
116
+ # powershell 30-func 16.4M/KB, typescript tiny-file 20.5M absolute. Thresholds exist to
117
+ # catch pathological (quadratic) parsers, not codegen jitter on the heaviest grammars.
118
+ _FUEL_HOTSPOT_ABSOLUTE = 30_000_000
119
+ _FUEL_HOTSPOT_PER_KB = 22_000_000.0
120
+ _FUEL_HOTSPOT_PER_LINE = 1_500_000.0
121
+
122
+
123
+ def _attach_content_type_metadata(
124
+ diff: SemanticDiff, old_content: str, new_content: str
125
+ ) -> SemanticDiff:
126
+ """Enrich a diff's metadata with the detected content type (mime + category).
127
+
128
+ Uses the Rust magic-byte detector on the changed content so downstream
129
+ surfaces can show what kind of file was diffed. Never raises — the detector
130
+ bridge falls back gracefully when the native core is unavailable.
131
+ """
132
+ from intentumdiff.content_type import detect_content_type
133
+
134
+ head = (new_content or old_content)[:8192].encode("utf-8", errors="replace")
135
+ metadata = dict(diff.metadata or {})
136
+ metadata["content_type"] = detect_content_type(head)
137
+ return diff.model_copy(update={"metadata": metadata})
138
+
139
+
140
+ def _fuel_budget(base: int, extra: int) -> int:
141
+ """Return an adaptive budget without hiding explicit low-fuel tests.
142
+
143
+ Real editor/CLI fuel settings are a floor, not a guarantee that ordinary
144
+ source files will fail once they cross that exact number. Tiny budgets are
145
+ still respected so fuel-pressure tests can prove runaway plugins fail
146
+ explicitly instead of being silently promoted to a larger cap.
147
+ """
148
+ if base == -1:
149
+ return base
150
+ if base < _EXPLICIT_FUEL_EXHAUSTION_TEST_CAP:
151
+ return base
152
+ return max(base, extra)
153
+
154
+
155
+ def _drain_plugin_telemetry(plugin_adapter: Any) -> list[dict[str, Any]]:
156
+ drain = getattr(plugin_adapter, "drain_telemetry", None)
157
+ if not callable(drain):
158
+ return []
159
+ records = drain()
160
+ return records if isinstance(records, list) else []
161
+
162
+
163
+ def _summarize_engine_telemetry(records: list[dict[str, Any]]) -> dict[str, Any]:
164
+ by_plugin: dict[tuple[str, str], dict[str, Any]] = {}
165
+ for record in records:
166
+ plugin = str(record.get("plugin") or "?")
167
+ function = str(record.get("function") or "?")
168
+ key = (plugin, function)
169
+ entry = by_plugin.setdefault(
170
+ key,
171
+ {
172
+ "plugin": plugin,
173
+ "function": function,
174
+ "engine_owner": record.get("engine_owner") or "unknown",
175
+ "engine": record.get("engine") or "unknown",
176
+ "provenance": record.get("provenance") or "unknown",
177
+ "call_count": 0,
178
+ "elapsed_ms": 0.0,
179
+ "fuel_budget": record.get("fuel_budget"),
180
+ "fuel_consumed": 0,
181
+ "total_fuel_consumed": 0,
182
+ "max_fuel_used_percent": None,
183
+ "language": record.get("language"),
184
+ "filename": record.get("filename"),
185
+ "input_bytes": 0,
186
+ "input_lines": 0,
187
+ "statuses": {},
188
+ "trusted": bool(record.get("trusted")),
189
+ },
190
+ )
191
+ entry["call_count"] += 1
192
+ entry["elapsed_ms"] = round(
193
+ float(entry["elapsed_ms"]) + float(record.get("elapsed_ms") or 0.0),
194
+ 3,
195
+ )
196
+ consumed = record.get("fuel_consumed")
197
+ if isinstance(consumed, int):
198
+ entry["fuel_consumed"] = max(entry["fuel_consumed"], consumed)
199
+ entry["total_fuel_consumed"] += consumed
200
+ input_bytes = record.get("input_bytes")
201
+ if isinstance(input_bytes, int):
202
+ entry["input_bytes"] += input_bytes
203
+ input_lines = record.get("input_lines")
204
+ if isinstance(input_lines, int):
205
+ entry["input_lines"] += input_lines
206
+ if entry.get("language") is None and record.get("language") is not None:
207
+ entry["language"] = record.get("language")
208
+ if entry.get("filename") is None and record.get("filename") is not None:
209
+ entry["filename"] = record.get("filename")
210
+ percent = record.get("fuel_used_percent")
211
+ if isinstance(percent, int | float):
212
+ current = entry["max_fuel_used_percent"]
213
+ entry["max_fuel_used_percent"] = percent if current is None else max(current, percent)
214
+ status = str(record.get("status") or "unknown")
215
+ statuses = entry["statuses"]
216
+ statuses[status] = statuses.get(status, 0) + 1
217
+ calls = sorted(
218
+ by_plugin.values(),
219
+ key=lambda item: (
220
+ item.get("fuel_consumed") or 0,
221
+ item.get("elapsed_ms") or 0.0,
222
+ ),
223
+ reverse=True,
224
+ )
225
+ hotspots = [_fuel_hotspot_for_call(call) for call in calls]
226
+ hotspots = [hotspot for hotspot in hotspots if hotspot is not None]
227
+ return {"schema_version": 1, "calls": calls, "fuel_hotspots": hotspots}
228
+
229
+
230
+ def _fuel_hotspot_for_call(call: dict[str, Any]) -> dict[str, Any] | None:
231
+ consumed = call.get("fuel_consumed")
232
+ if not isinstance(consumed, int) or consumed <= 0:
233
+ return None
234
+ input_bytes = call.get("input_bytes")
235
+ input_lines = call.get("input_lines")
236
+ kb = max((input_bytes or 0) / 1024.0, 1.0)
237
+ lines = max(input_lines or 0, 1)
238
+ fuel_per_kb = consumed / kb
239
+ fuel_per_line = consumed / lines
240
+ exceeded: list[str] = []
241
+ if consumed > _FUEL_HOTSPOT_ABSOLUTE:
242
+ exceeded.append("absolute")
243
+ if fuel_per_kb > _FUEL_HOTSPOT_PER_KB:
244
+ exceeded.append("per_kb")
245
+ if fuel_per_line > _FUEL_HOTSPOT_PER_LINE:
246
+ exceeded.append("per_line")
247
+ normalized_exceeded = any(threshold in exceeded for threshold in ("per_kb", "per_line"))
248
+ if not exceeded or ("absolute" in exceeded and not normalized_exceeded):
249
+ return None
250
+ return {
251
+ "plugin": call.get("plugin"),
252
+ "function": call.get("function"),
253
+ "language": call.get("language"),
254
+ "filename": call.get("filename"),
255
+ "fuel_consumed": consumed,
256
+ "fuel_budget": call.get("fuel_budget"),
257
+ "fuel_used_percent": call.get("max_fuel_used_percent"),
258
+ "input_bytes": input_bytes,
259
+ "input_lines": input_lines,
260
+ "fuel_per_kb": round(fuel_per_kb, 3),
261
+ "fuel_per_line": round(fuel_per_line, 3),
262
+ "thresholds_exceeded": exceeded,
263
+ }
264
+
265
+
266
+ def _attach_run_telemetry(
267
+ diff: SemanticDiff,
268
+ engine_telemetry: list[dict[str, Any]],
269
+ diagnostics: DiagnosticsRecorder,
270
+ ) -> SemanticDiff:
271
+ """Final metadata attach (engine_telemetry + diagnostics snapshot) shared by the
272
+ normal pipeline tail and the Rust finalize-routed return (issue #57) — the routed
273
+ short-circuit skipped it, dropping the wasm parser telemetry contract."""
274
+ if engine_telemetry:
275
+ telemetry_metadata = dict(diff.metadata)
276
+ telemetry_metadata["engine_telemetry"] = _summarize_engine_telemetry(
277
+ engine_telemetry
278
+ )
279
+ diff = diff.model_copy(update={"metadata": telemetry_metadata})
280
+ if diagnostics.enabled:
281
+ diagnostics_metadata = dict(diff.metadata)
282
+ diagnostics.summary.update(
283
+ {
284
+ "final_change_count": len(diff.changes),
285
+ "final_group_count": len(diff.change_groups),
286
+ "has_semantic_changes": diff.has_semantic_changes,
287
+ "is_style_only": diff.is_style_only,
288
+ }
289
+ )
290
+ diagnostics_metadata["diagnostics"] = diagnostics.snapshot()
291
+ diff = diff.model_copy(update={"metadata": diagnostics_metadata})
292
+ return diff
293
+
294
+
295
+ def _record_engine_telemetry(
296
+ diagnostics: DiagnosticsRecorder,
297
+ records: list[dict[str, Any]],
298
+ ) -> None:
299
+ if not records:
300
+ return
301
+ summary = _summarize_engine_telemetry(records)
302
+ diagnostics.summary["engine_telemetry"] = summary
303
+ diagnostics.record(
304
+ stage="engine.telemetry",
305
+ action="wasm_fuel_summary",
306
+ rule_id="engine.wasm_fuel_telemetry",
307
+ reason="Wasm plugin calls recorded fuel and duration telemetry",
308
+ metadata=summary,
309
+ )
310
+ hotspots = summary.get("fuel_hotspots")
311
+ if hotspots:
312
+ diagnostics.record(
313
+ stage="engine.telemetry",
314
+ action="wasm_fuel_hotspot",
315
+ rule_id="engine.wasm_fuel_hotspot",
316
+ reason="Wasm plugin fuel use exceeded the excessive-fuel policy",
317
+ metadata={"fuel_hotspots": hotspots},
318
+ )
319
+
320
+
@@ -0,0 +1 @@
1
+ # intentumdiff/analysis
@@ -0,0 +1,151 @@
1
+ """Compile database metadata for C/C++ review context."""
2
+
3
+ from __future__ import annotations
4
+
5
+ import hashlib
6
+ import json
7
+ import shlex
8
+ from pathlib import Path
9
+ from typing import Any
10
+
11
+ _CXX_LANGUAGES = frozenset({"c", "cpp", "c++", "cxx"})
12
+ _COMPILE_DATABASE = "compile_commands.json"
13
+
14
+
15
+ def compile_commands_metadata(
16
+ *,
17
+ filename: str,
18
+ language: str,
19
+ cwd: Path | None = None,
20
+ ) -> dict[str, Any] | None:
21
+ """Return normalized compile-command context for a C/C++ file if available."""
22
+
23
+ if language.lower() not in _CXX_LANGUAGES:
24
+ return None
25
+ logical_path = Path(filename)
26
+ if str(logical_path).startswith("<"):
27
+ return None
28
+ base_dir = cwd or Path.cwd()
29
+ file_path = logical_path if logical_path.is_absolute() else base_dir / logical_path
30
+ database = _find_compile_database(file_path)
31
+ if database is None:
32
+ return None
33
+ entry = _matching_entry(database, file_path)
34
+ if entry is None:
35
+ return None
36
+ arguments = _entry_arguments(entry)
37
+ if not arguments:
38
+ return None
39
+ metadata = {
40
+ "database": _repo_relative(database, base_dir),
41
+ "file": _repo_relative(_entry_file_path(entry, database.parent), base_dir),
42
+ "directory": _repo_relative(Path(str(entry.get("directory") or database.parent)), base_dir),
43
+ "arguments": arguments,
44
+ "defines": _flag_values(arguments, "-D"),
45
+ "include_dirs": _include_dirs(arguments),
46
+ "standard": _standard(arguments),
47
+ }
48
+ metadata["fingerprint"] = _fingerprint(metadata)
49
+ return metadata
50
+
51
+
52
+ def _find_compile_database(file_path: Path) -> Path | None:
53
+ for directory in [file_path.parent, *file_path.parents]:
54
+ candidate = directory / _COMPILE_DATABASE
55
+ if candidate.is_file():
56
+ return candidate
57
+ return None
58
+
59
+
60
+ def _matching_entry(database: Path, file_path: Path) -> dict[str, Any] | None:
61
+ try:
62
+ raw = json.loads(database.read_text(encoding="utf8"))
63
+ except (OSError, json.JSONDecodeError):
64
+ return None
65
+ if not isinstance(raw, list):
66
+ return None
67
+ target = _normalized_path(file_path)
68
+ basename = file_path.name.lower()
69
+ basename_match: dict[str, Any] | None = None
70
+ for item in raw:
71
+ if not isinstance(item, dict):
72
+ continue
73
+ entry_file = _entry_file_path(item, database.parent)
74
+ if _normalized_path(entry_file) == target:
75
+ return item
76
+ if entry_file.name.lower() == basename and basename_match is None:
77
+ basename_match = item
78
+ return basename_match
79
+
80
+
81
+ def _entry_file_path(entry: dict[str, Any], database_dir: Path) -> Path:
82
+ raw_file = Path(str(entry.get("file") or ""))
83
+ directory = Path(str(entry.get("directory") or database_dir))
84
+ return raw_file if raw_file.is_absolute() else directory / raw_file
85
+
86
+
87
+ def _entry_arguments(entry: dict[str, Any]) -> list[str]:
88
+ arguments = entry.get("arguments")
89
+ if isinstance(arguments, list):
90
+ return [str(arg) for arg in arguments if str(arg)]
91
+ command = entry.get("command")
92
+ if isinstance(command, str) and command.strip():
93
+ try:
94
+ return shlex.split(command, posix=False)
95
+ except ValueError:
96
+ return command.split()
97
+ return []
98
+
99
+
100
+ def _flag_values(arguments: list[str], prefix: str) -> list[str]:
101
+ values: list[str] = []
102
+ index = 0
103
+ while index < len(arguments):
104
+ arg = arguments[index]
105
+ if arg == prefix and index + 1 < len(arguments):
106
+ values.append(arguments[index + 1])
107
+ index += 2
108
+ continue
109
+ if arg.startswith(prefix) and len(arg) > len(prefix):
110
+ values.append(arg[len(prefix):])
111
+ index += 1
112
+ return values
113
+
114
+
115
+ def _include_dirs(arguments: list[str]) -> list[str]:
116
+ values = _flag_values(arguments, "-I")
117
+ values.extend(_flag_values(arguments, "/I"))
118
+ return values
119
+
120
+
121
+ def _standard(arguments: list[str]) -> str | None:
122
+ for arg in arguments:
123
+ if arg.startswith("-std="):
124
+ return arg.removeprefix("-std=")
125
+ if arg.startswith("/std:"):
126
+ return arg.removeprefix("/std:")
127
+ return None
128
+
129
+
130
+ def _fingerprint(metadata: dict[str, Any]) -> str:
131
+ payload = {
132
+ key: metadata[key]
133
+ for key in ("file", "arguments", "defines", "include_dirs", "standard")
134
+ if metadata.get(key)
135
+ }
136
+ encoded = json.dumps(payload, sort_keys=True, separators=(",", ":")).encode("utf8")
137
+ return hashlib.sha256(encoded).hexdigest()[:16]
138
+
139
+
140
+ def _normalized_path(path: Path) -> str:
141
+ try:
142
+ return str(path.resolve()).replace("\\", "/").lower()
143
+ except OSError:
144
+ return str(path.absolute()).replace("\\", "/").lower()
145
+
146
+
147
+ def _repo_relative(path: Path, base_dir: Path) -> str:
148
+ try:
149
+ return path.resolve().relative_to(base_dir.resolve()).as_posix()
150
+ except (OSError, ValueError):
151
+ return path.as_posix()
@@ -0,0 +1,71 @@
1
+ """
2
+ intentumdiff.analysis.cross_file
3
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
4
+
5
+ Detect cross-file semantic changes by comparing two ``SemanticIndex``
6
+ snapshots.
7
+
8
+ Rust-authoritative (#91): the detection algorithm — ``MOVE_TO_MODULE`` (a
9
+ definition appears in a different file under the same qualified name),
10
+ ``SPLIT_MODULE`` (one file's symbols fan out across several new files), and
11
+ ``CROSS_FILE_RENAME`` (a same-typed, same-scope definition reappears under a new
12
+ name) — runs in the Rust core (``index-engine-lib`` via ``diff_symbol_tables_json``).
13
+ The former pure-Python mirror was deleted; only the thin DTO-marshalling wrapper
14
+ survives here, so "if Python didn't exist, IntentumDiff still works".
15
+ """
16
+
17
+ from __future__ import annotations
18
+
19
+ import json
20
+
21
+ from intentumdiff.core.index import SemanticIndex
22
+ from intentumdiff.core.models import ChangeType, CrossFileChange
23
+
24
+
25
+ def detect_cross_file_changes(
26
+ old_index: SemanticIndex,
27
+ new_index: SemanticIndex,
28
+ ) -> list[CrossFileChange]:
29
+ """
30
+ Compare *old_index* and *new_index* via the Rust core and return the
31
+ resulting ``CrossFileChange`` instances.
32
+
33
+ Both indices must have had ``build()`` called first. The comparison itself
34
+ is performed by the Rust core over the flat symbol tables; this function only
35
+ marshals the tables in and the change list back out. Returns an empty list
36
+ when the core is unavailable.
37
+ """
38
+ from intentumdiff.rust_core import try_rust_diff_symbol_tables
39
+
40
+ old_json = json.dumps(
41
+ {k: [d.model_dump() for d in defs] for k, defs in old_index.symbols.items()}
42
+ )
43
+ new_json = json.dumps(
44
+ {k: [d.model_dump() for d in defs] for k, defs in new_index.symbols.items()}
45
+ )
46
+
47
+ result = try_rust_diff_symbol_tables(old_json, new_json)
48
+ if result is None:
49
+ return []
50
+
51
+ changes: list[CrossFileChange] = []
52
+ for item in result:
53
+ changes.append(
54
+ CrossFileChange(
55
+ change_type=ChangeType(item["change_type"]),
56
+ symbol_name=item["symbol_name"],
57
+ old_file=item["old_file"],
58
+ new_file=item["new_file"],
59
+ old_node_id=item.get("old_node_id"),
60
+ new_node_id=item.get("new_node_id"),
61
+ old_position=item.get("old_position"),
62
+ new_position=item.get("new_position"),
63
+ old_language=item.get("old_language", ""),
64
+ new_language=item.get("new_language", ""),
65
+ node_type=item.get("node_type", ""),
66
+ symbol_kind=item.get("symbol_kind", ""),
67
+ confidence=item.get("confidence", 1.0),
68
+ description=item.get("description", ""),
69
+ )
70
+ )
71
+ return changes