intentumdiff-python 0.0.1__py3-none-manylinux_2_39_x86_64.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.so +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,1079 @@
1
+ """
2
+ intentumdiff.plugins.registry
3
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
4
+
5
+ Plugin registry: discovery, caching, and dispatch.
6
+
7
+ Discovery
8
+ ─────────
9
+ 1. Built-in plugins are pre-registered via their entry-point callables
10
+ (see ``pyproject.toml`` ``[project.entry-points]``).
11
+ 2. Third-party plugins install themselves under the same entry-point groups.
12
+ 3. On first use, ``PluginRegistry`` scans ``importlib.metadata`` entry points
13
+ and loads each registered ``.wasm`` file.
14
+
15
+ Dispatch order (parsers)
16
+ ────────────────────────
17
+ For each file, ``detect_parser`` iterates registered parsers in descending
18
+ priority order and calls ``detect_language``. The first plugin that returns a
19
+ non-empty string wins.
20
+
21
+ Allowed-list filtering
22
+ ──────────────────────
23
+ If ``DiffConfig.allowed_plugins`` is set, only those grammar IDs are consulted.
24
+ If ``DiffConfig.strict_plugins`` is True, an error is raised for any language
25
+ not handled by the allowed list.
26
+ """
27
+
28
+ from __future__ import annotations
29
+
30
+ import importlib.metadata
31
+ import logging
32
+ import re
33
+ import threading
34
+ import time
35
+ from contextlib import contextmanager
36
+ from dataclasses import dataclass, field
37
+ from pathlib import Path
38
+ from typing import Callable, Iterator
39
+
40
+ from intentumdiff.core.models import DetectionResult, DiffConfig, LanguageInfoGroup
41
+ from intentumdiff.plugins.adapter import (
42
+ DiffAnalyzerAdapter,
43
+ EnricherAdapter,
44
+ ParserAdapter,
45
+ RendererAdapter,
46
+ )
47
+ from intentumdiff.plugins.exceptions import PluginFuelExhausted, PluginNotFoundError
48
+ from intentumdiff.plugins.language_metadata import fallback_language_info
49
+ from intentumdiff.plugins.loader import LoadedPlugin, load_plugin
50
+
51
+ logger = logging.getLogger(__name__)
52
+
53
+ _WASM_PATH_METADATA_FIELD = "IntentumDiff-Wasm-Path"
54
+
55
+ _PARSER_GROUP = "intentumdiff.parsers"
56
+ _RENDERER_GROUP = "intentumdiff.renderers"
57
+ _ENRICHER_GROUP = "intentumdiff.enrichers"
58
+ _DIFF_ANALYZER_GROUP = "intentumdiff.diff_analyzers"
59
+
60
+ # Entry-point callable type: returns the path to the .wasm file as str
61
+ EntryCallable = Callable[[], str]
62
+ PhaseRecorder = Callable[[str, float], None]
63
+
64
+ # First-party parser entry points that are intentionally not advertised until
65
+ # their parser contract is restored. Keeping this host-side guard makes editable
66
+ # installs safe even when stale entry-point metadata still lists the plugin.
67
+ _DISABLED_BUILTIN_PARSER_ENTRYPOINTS: frozenset[str] = frozenset({"freebasic"})
68
+
69
+ # Normalised names of first-party packages whose entry-point callables are
70
+ # trusted plugin metadata.
71
+ _TRUSTED_PLUGIN_PACKAGE_NAMES: frozenset[str] = frozenset(
72
+ {
73
+ "intentumdiff",
74
+ "intentumdiff-dbt",
75
+ "intentumdiff_dbt",
76
+ }
77
+ )
78
+
79
+ _ENTRYPOINT_LANGUAGE_ALIASES: dict[str, tuple[str, ...]] = {
80
+ "asciidoc": ("asciidoc", "adoc"),
81
+ "hcl": ("terraform", "hcl"),
82
+ "databricks-workflow": ("databricks-workflow", "databricks"),
83
+ "graphql": ("graphql", "gql"),
84
+ "latex": ("latex", "tex"),
85
+ "m": ("m", "dax"),
86
+ "dax": ("dax", "m"),
87
+ "ocaml": ("ocaml", "ml"),
88
+ "po": ("po", "pot", "gettext"),
89
+ "reasonml": ("reasonml", "re"),
90
+ "typescript": ("typescript",),
91
+ "tsx": ("tsx",),
92
+ "wat": ("wat", "wast"),
93
+ "yaml": ("yaml", "yml"),
94
+ }
95
+
96
+ _FIRST_PARTY_PARSER_ENTRYPOINT_FALLBACKS: dict[str, str] = {
97
+ # Source checkouts and long-lived editable environments can have stale
98
+ # entry-point metadata after package identity changes. Keep built-ins
99
+ # discoverable from the source tree instead of silently falling through to
100
+ # the generic text parser.
101
+ "python": "python_parser_entry",
102
+ "sql": "sql_parser_entry",
103
+ "generic": "generic_parser_entry",
104
+ "graphql": "graphql_parser_entry",
105
+ "gitignore": "gitignore_parser_entry",
106
+ "ocaml": "ocaml_parser_entry",
107
+ "reasonml": "reasonml_parser_entry",
108
+ "latex": "latex_parser_entry",
109
+ "asciidoc": "asciidoc_parser_entry",
110
+ "po": "po_parser_entry",
111
+ "javascript": "js_ts_parser_entry",
112
+ "typescript": "js_ts_parser_entry",
113
+ "tsx": "js_ts_parser_entry",
114
+ "java": "java_parser_entry",
115
+ "go": "go_parser_entry",
116
+ "rust": "rust_parser_entry",
117
+ "csharp": "csharp_parser_entry",
118
+ "ruby": "ruby_parser_entry",
119
+ "php": "php_parser_entry",
120
+ "kotlin": "kotlin_parser_entry",
121
+ "c": "cpp_parser_entry",
122
+ "cpp": "cpp_parser_entry",
123
+ "swift": "swift_parser_entry",
124
+ "bash": "bash_parser_entry",
125
+ "powershell": "powershell_parser_entry",
126
+ "elixir": "elixir_parser_entry",
127
+ "groovy": "groovy_parser_entry",
128
+ "dart": "dart_parser_entry",
129
+ "lua": "lua_parser_entry",
130
+ "xml": "xml_parser_entry",
131
+ "dockerfile": "dockerfile_parser_entry",
132
+ "vbnet": "vbnet_parser_entry",
133
+ "squirrel": "squirrel_parser_entry",
134
+ "puppet": "puppet_parser_entry",
135
+ "hcl": "terraform_parser_entry",
136
+ "delphi": "delphi_parser_entry",
137
+ "adf": "adf_parser_entry",
138
+ "databricks-workflow": "databricks_parser_entry",
139
+ "mdx": "mdx_parser_entry",
140
+ "markdown": "markdown_parser_entry",
141
+ "toml": "toml_parser_entry",
142
+ "ini": "ini_parser_entry",
143
+ "gomod": "gomod_parser_entry",
144
+ "make": "make_parser_entry",
145
+ "proto": "proto_parser_entry",
146
+ "cmake": "cmake_parser_entry",
147
+ "r": "r_parser_entry",
148
+ "haskell": "haskell_parser_entry",
149
+ "zig": "zig_parser_entry",
150
+ "scala": "scala_parser_entry",
151
+ "clojure": "clojure_parser_entry",
152
+ "perl": "perl_parser_entry",
153
+ "asm": "asm_parser_entry",
154
+ "assemblyscript": "assemblyscript_parser_entry",
155
+ "odin": "odin_parser_entry",
156
+ "wat": "wat_parser_entry",
157
+ "tsql": "tsql_parser_entry",
158
+ "plsql": "plsql_parser_entry",
159
+ "abap": "abap_parser_entry",
160
+ "dax": "dax_parser_entry",
161
+ "m": "dax_parser_entry",
162
+ "sas": "sas_parser_entry",
163
+ "qsharp": "qsharp_parser_entry",
164
+ "postscript": "postscript_parser_entry",
165
+ "css": "css_parser_entry",
166
+ "json": "json_parser_entry",
167
+ "yaml": "yaml_parser_entry",
168
+ "html": "html_parser_entry",
169
+ "scss": "scss_parser_entry",
170
+ "vue": "vue_parser_entry",
171
+ "svelte": "svelte_parser_entry",
172
+ "astro": "astro_parser_entry",
173
+ }
174
+
175
+
176
+ @dataclass
177
+ class _ParserCatalogEntry:
178
+ """Lightweight parser entry-point metadata.
179
+
180
+ The catalog intentionally avoids instantiating Wasm components. Normal
181
+ diffing only loads the entry selected by filename/language/plugin-id
182
+ shortlisting, while explicit inventory APIs can still load all parsers.
183
+ """
184
+
185
+ ep: importlib.metadata.EntryPoint
186
+ wasm_path: str
187
+ resolved_path: str
188
+ entry_names: list[str]
189
+ plugin_id: str
190
+ package_name: str
191
+ package_version: str
192
+ author: str
193
+ provenance: str
194
+ trusted: bool
195
+ adapter: ParserAdapter | None = None
196
+ load_error: str | None = None
197
+ lock: threading.Lock = field(default_factory=threading.Lock)
198
+
199
+ @property
200
+ def language_guesses(self) -> tuple[str, ...]:
201
+ guesses: list[str] = []
202
+ for name in self.entry_names:
203
+ guesses.extend(_ENTRYPOINT_LANGUAGE_ALIASES.get(name, (name,)))
204
+ return tuple(dict.fromkeys(guesses))
205
+
206
+
207
+ def _normalised_dist_name(dist: importlib.metadata.Distribution) -> str:
208
+ raw_name: str = (dist.metadata.get("Name") or dist.name or "").lower()
209
+ return raw_name.replace("-", "_")
210
+
211
+
212
+ def _is_trusted_entry_point(ep: importlib.metadata.EntryPoint) -> bool:
213
+ dist = ep.dist
214
+ if dist is None:
215
+ return False
216
+ trusted_names = {n.replace("-", "_") for n in _TRUSTED_PLUGIN_PACKAGE_NAMES}
217
+ return _normalised_dist_name(dist) in trusted_names
218
+
219
+
220
+ # ---------------------------------------------------------------------------
221
+ # Entry-point stubs (builtins.py — see plugins/builtins.py)
222
+ # ---------------------------------------------------------------------------
223
+
224
+
225
+ def _wasm_dir() -> Path:
226
+ """Return the directory containing built-in .wasm files."""
227
+ return Path(__file__).parent.parent / "wasm"
228
+
229
+
230
+ # ---------------------------------------------------------------------------
231
+ # Secure wasm-path resolution
232
+ # ---------------------------------------------------------------------------
233
+
234
+
235
+ def _wasm_path_from_ep(ep: importlib.metadata.EntryPoint) -> str:
236
+ """Resolve the ``.wasm`` path for a plugin entry point.
237
+
238
+ **First-party plugins** (the core package and repo-maintained plugin
239
+ packages) are trusted. Their entry-point callables are invoked to locate
240
+ the wasm file.
241
+
242
+ **Third-party plugins** must declare an ``IntentumDiff-Wasm-Path`` field in their
243
+ dist-info METADATA. This field holds a path relative to the installed
244
+ package root, resolvable via :func:`importlib.metadata.files` without
245
+ importing any Python module. If the field is absent, the plugin is
246
+ rejected — falling back to ``ep.load()`` would execute arbitrary Python
247
+ code before any Wasm sandboxing is in effect.
248
+
249
+ Raises
250
+ ------
251
+ ValueError
252
+ For third-party plugins that do not declare ``IntentumDiff-Wasm-Path``, or
253
+ when the declared path cannot be resolved on disk.
254
+ """
255
+ dist = ep.dist
256
+ if dist is None:
257
+ raise ValueError(
258
+ f"Entry point {ep.name!r} has no associated distribution metadata. "
259
+ "Cannot resolve wasm path safely."
260
+ )
261
+
262
+ # Trusted first-party plugins can use entry-point callables; untrusted
263
+ # packages stay on declarative metadata-only path resolution.
264
+ if _is_trusted_entry_point(ep):
265
+ fn: EntryCallable = ep.load()
266
+ return fn()
267
+
268
+ # Third-party plugins: read the wasm path from declarative metadata only.
269
+ wasm_rel: str | None = dist.metadata.get(_WASM_PATH_METADATA_FIELD)
270
+ if not wasm_rel:
271
+ raise ValueError(
272
+ f"Third-party plugin {ep.name!r} (package {dist.metadata.get('Name')!r}) "
273
+ f"does not declare an '{_WASM_PATH_METADATA_FIELD}' metadata field. "
274
+ "Importing the plugin's Python module is refused because it would "
275
+ "execute arbitrary code before Wasm sandboxing is in effect. "
276
+ f"The plugin author must add '{_WASM_PATH_METADATA_FIELD}: <relative/path.wasm>' "
277
+ "to their package metadata. "
278
+ "See: https://docs.intentumdiff.dev/plugins/metadata"
279
+ )
280
+
281
+ # Locate the file via importlib.metadata without importing anything.
282
+ for f in dist.files or []:
283
+ f_str = str(f).replace("\\", "/")
284
+ if f_str == wasm_rel.replace("\\", "/") or f.name == Path(wasm_rel).name:
285
+ full = Path(str(dist.locate_file(f)))
286
+ if full.exists():
287
+ return str(full)
288
+
289
+ raise ValueError(
290
+ f"Plugin {ep.name!r}: '{_WASM_PATH_METADATA_FIELD}' declares {wasm_rel!r} "
291
+ "but no matching file was found in the installed distribution."
292
+ )
293
+
294
+
295
+ # ---------------------------------------------------------------------------
296
+ # Registry
297
+ # ---------------------------------------------------------------------------
298
+
299
+
300
+ class PluginRegistry:
301
+ """
302
+ Thread-safe (at the loading stage) plugin registry.
303
+
304
+ Instantiate once and reuse — ``LoadedPlugin`` objects are cached.
305
+ """
306
+
307
+ def __init__(self, config: DiffConfig | None = None) -> None:
308
+ self._config = config or DiffConfig()
309
+ self._parsers: list[ParserAdapter] | None = None
310
+ self._parser_catalog: list[_ParserCatalogEntry] | None = None
311
+ self._parser_load_errors: list[str] = []
312
+ self._renderers: list[RendererAdapter] | None = None
313
+ self._enrichers: list[EnricherAdapter] | None = None
314
+ self._diff_analyzers: list[DiffAnalyzerAdapter] | None = None
315
+ self._lock = threading.Lock()
316
+
317
+ # ── Parser access ────────────────────────────────────────────────────────
318
+
319
+ @property
320
+ def parsers(self) -> list[ParserAdapter]:
321
+ if self._parsers is None:
322
+ with self._lock:
323
+ if self._parsers is None:
324
+ load_errors: list[str] = []
325
+ self._parsers = _load_parsers(
326
+ fuel=self._config.plugin_fuel,
327
+ load_errors=load_errors,
328
+ )
329
+ self._parser_load_errors = load_errors
330
+ return self._parsers
331
+
332
+ def parser_load_failure_summary(self) -> str | None:
333
+ """
334
+ Return a user-facing summary when parser discovery has completely failed.
335
+
336
+ Unsupported files should still be skipped quietly, but a repository
337
+ review with zero loaded parsers is almost always an environment or
338
+ dependency problem. Surfacing this state prevents VS Code from showing
339
+ a misleading "No semantic changes" result when every Wasm parser was
340
+ rejected during startup.
341
+ """
342
+ if self._parsers:
343
+ return None
344
+ if self._parser_catalog is not None and any(entry.adapter for entry in self._parser_catalog):
345
+ return None
346
+ if not self._parser_load_errors:
347
+ if self._parsers is None:
348
+ return None
349
+ return (
350
+ "No parser plugins are installed or discoverable. Semantic review "
351
+ "cannot run until IntentumDiff is installed with its parser "
352
+ "entry points."
353
+ )
354
+ sample = "; ".join(self._parser_load_errors[:5])
355
+ remaining = len(self._parser_load_errors) - 5
356
+ if remaining > 0:
357
+ sample = f"{sample}; and {remaining} more"
358
+ return (
359
+ "No parser plugins could be loaded for the reviewed files. Semantic review "
360
+ "cannot run until the parser environment is fixed. If this happened after "
361
+ "upgrading dependencies, run 'uv sync' or reinstall intentumdiff so "
362
+ "the runtime matches the lockfile. Parser load errors: "
363
+ f"{sample}"
364
+ )
365
+
366
+ def language_ids(self) -> list[str]:
367
+ """Return parser language IDs reported by loaded parser plugins."""
368
+ allowed = set(self._config.allowed_plugins or [])
369
+ ids: set[str] = set()
370
+ for parser in self.parsers:
371
+ if allowed and parser.grammar_id not in allowed:
372
+ continue
373
+ ids.update(parser.language_ids)
374
+ return sorted(ids)
375
+
376
+ def language_info(self) -> list[LanguageInfoGroup]:
377
+ """Return parser metadata grouped by language ID."""
378
+ allowed = set(self._config.allowed_plugins or [])
379
+ grouped: dict[str, list] = {}
380
+ for parser in self.parsers:
381
+ if allowed and parser.grammar_id not in allowed:
382
+ continue
383
+ for info in parser.language_info:
384
+ grouped.setdefault(info.language_id, []).append(info)
385
+
386
+ result: list[LanguageInfoGroup] = []
387
+ for language, plugins in grouped.items():
388
+ sorted_plugins = sorted(
389
+ plugins,
390
+ key=lambda info: (-info.priority, info.plugin_id),
391
+ )
392
+ result.append(
393
+ LanguageInfoGroup(
394
+ language=language,
395
+ selected_plugin_id=sorted_plugins[0].plugin_id,
396
+ plugins=sorted_plugins,
397
+ )
398
+ )
399
+ return sorted(result, key=lambda item: item.language)
400
+
401
+ def get_parser_by_id(
402
+ self,
403
+ plugin_id: str,
404
+ *,
405
+ language: str | None = None,
406
+ ) -> ParserAdapter:
407
+ """Return a parser by opaque plugin ID, optionally requiring language support."""
408
+ for parser in self.parsers:
409
+ if parser.plugin_id != plugin_id:
410
+ continue
411
+ if language is not None and language not in parser.language_ids:
412
+ break
413
+ return parser
414
+ raise PluginNotFoundError(language or plugin_id)
415
+
416
+ def _catalog(self, phase_recorder: PhaseRecorder | None = None) -> list[_ParserCatalogEntry]:
417
+ if self._parser_catalog is None:
418
+ with self._lock:
419
+ if self._parser_catalog is None:
420
+ load_errors: list[str] = []
421
+ with _record_phase(phase_recorder, "parser_entrypoint_discovery"):
422
+ self._parser_catalog = _discover_parser_catalog(load_errors=load_errors)
423
+ self._parser_load_errors.extend(load_errors)
424
+ return self._parser_catalog
425
+
426
+ def _candidate_entries(
427
+ self,
428
+ filename: str,
429
+ *,
430
+ language_hint: str | None = None,
431
+ plugin_id: str | None = None,
432
+ candidates: list[str] | None = None,
433
+ phase_recorder: PhaseRecorder | None = None,
434
+ ) -> list[_ParserCatalogEntry]:
435
+ with _record_phase(phase_recorder, "parser_candidate_shortlist"):
436
+ entries = self._catalog(phase_recorder)
437
+ language_filter = set(candidates or [])
438
+ if language_hint:
439
+ language_filter.add(language_hint)
440
+
441
+ result: list[_ParserCatalogEntry] = []
442
+ for entry in entries:
443
+ if plugin_id is not None:
444
+ if plugin_id == entry.plugin_id or plugin_id in entry.entry_names:
445
+ result.append(entry)
446
+ continue
447
+ if language_filter:
448
+ if language_filter.intersection(entry.language_guesses):
449
+ result.append(entry)
450
+ continue
451
+ if _entry_matches_filename(entry, filename):
452
+ result.append(entry)
453
+
454
+ if result or plugin_id is not None or language_filter:
455
+ # The generic parser is the designated FALLBACK: when a specific parser
456
+ # also matched the filename (CMakeLists.txt matches both generic's .txt
457
+ # and the cmake plugin), catalog order must not let generic claim the
458
+ # file first — it detect-claims everything.
459
+ result.sort(key=lambda e: "generic" in e.language_guesses)
460
+ return result
461
+ return list(entries)
462
+
463
+ def _load_catalog_entry(
464
+ self,
465
+ entry: _ParserCatalogEntry,
466
+ *,
467
+ phase_recorder: PhaseRecorder | None = None,
468
+ ) -> ParserAdapter:
469
+ if entry.adapter is not None:
470
+ return entry.adapter
471
+ with entry.lock:
472
+ if entry.adapter is not None:
473
+ return entry.adapter
474
+ with _record_phase(phase_recorder, "parser_plugin_instantiation"):
475
+ try:
476
+ plugin = load_plugin(
477
+ entry.wasm_path,
478
+ self._config.plugin_fuel,
479
+ trusted=entry.trusted,
480
+ )
481
+ adapter = ParserAdapter(plugin)
482
+ _assign_catalog_metadata(adapter, entry)
483
+ entry.adapter = adapter
484
+ entry.load_error = None
485
+ logger.debug(
486
+ "Loaded parser plugin: %s (%s)",
487
+ ",".join(entry.entry_names),
488
+ entry.wasm_path,
489
+ )
490
+ return adapter
491
+ except Exception as exc:
492
+ message = f"{entry.entry_names[0]}: {exc}"
493
+ entry.load_error = message
494
+ if message not in self._parser_load_errors:
495
+ self._parser_load_errors.append(message)
496
+ logger.warning(
497
+ "Failed to load parser plugin %r: %s",
498
+ entry.entry_names[0],
499
+ exc,
500
+ )
501
+ raise
502
+
503
+ def detect_parser(
504
+ self,
505
+ filename: str,
506
+ content: str,
507
+ language_hint: str | None = None,
508
+ plugin_id: str | None = None,
509
+ phase_recorder: PhaseRecorder | None = None,
510
+ ) -> tuple[ParserAdapter, str]:
511
+ """
512
+ Return ``(parser, language)`` for a given file.
513
+
514
+ If ``language_hint`` is supplied it overrides detection; we still
515
+ find the parser by grammar-id match.
516
+
517
+ Raises ``PluginNotFoundError`` if no parser can handle the file.
518
+ """
519
+ allowed = self._config.allowed_plugins
520
+
521
+ if plugin_id:
522
+ for entry in self._candidate_entries(
523
+ filename,
524
+ language_hint=language_hint,
525
+ plugin_id=plugin_id,
526
+ phase_recorder=phase_recorder,
527
+ ):
528
+ try:
529
+ parser = self._load_catalog_entry(entry, phase_recorder=phase_recorder)
530
+ if allowed is not None and parser.grammar_id not in allowed:
531
+ continue
532
+ if language_hint:
533
+ if language_hint in parser.language_ids:
534
+ return parser, language_hint
535
+ continue
536
+ with _record_phase(phase_recorder, "parser_plugin_language_detection"):
537
+ lang = parser.detect_language(filename, content[:2048])
538
+ if lang:
539
+ return parser, lang
540
+ except PluginFuelExhausted:
541
+ # Terminal, never a skip. Fuel exhaustion (now possible during
542
+ # instantiation since the wasmtime 47 binding) means THIS plugin hit
543
+ # the user's CPU cap — trying the next candidate would either
544
+ # mis-parse the file or dress the fuel problem up as
545
+ # PluginNotFoundError('unknown'). The whole point of the fuel
546
+ # contract is to fail explicitly rather than degrade silently.
547
+ raise
548
+ except Exception as exc:
549
+ logger.debug(
550
+ "Skipping parser candidate %s after load failure: %s",
551
+ ",".join(entry.entry_names),
552
+ exc,
553
+ )
554
+ continue
555
+ raise PluginNotFoundError(plugin_id, filename)
556
+
557
+ if language_hint:
558
+ for entry in self._candidate_entries(
559
+ filename,
560
+ language_hint=language_hint,
561
+ phase_recorder=phase_recorder,
562
+ ):
563
+ try:
564
+ parser = self._load_catalog_entry(entry, phase_recorder=phase_recorder)
565
+ except PluginFuelExhausted:
566
+ # Terminal, never a skip. Fuel exhaustion (now possible during
567
+ # instantiation since the wasmtime 47 binding) means THIS plugin hit
568
+ # the user's CPU cap — trying the next candidate would either
569
+ # mis-parse the file or dress the fuel problem up as
570
+ # PluginNotFoundError('unknown'). The whole point of the fuel
571
+ # contract is to fail explicitly rather than degrade silently.
572
+ raise
573
+ except Exception as exc:
574
+ logger.debug(
575
+ "Skipping parser candidate %s after load failure: %s",
576
+ ",".join(entry.entry_names),
577
+ exc,
578
+ )
579
+ continue
580
+ if allowed is not None and parser.grammar_id not in allowed:
581
+ continue
582
+ if language_hint in parser.language_ids:
583
+ return parser, language_hint
584
+ if language_hint in entry.language_guesses and "generic" in parser.language_ids:
585
+ return parser, language_hint
586
+ if self._config.strict_plugins:
587
+ raise PluginNotFoundError(language_hint, filename)
588
+
589
+ primary_entries = self._candidate_entries(filename, phase_recorder=phase_recorder)
590
+ matched_paths: set[str] = set()
591
+ for entries in (
592
+ primary_entries,
593
+ [entry for entry in self._catalog() if entry not in primary_entries],
594
+ ):
595
+ for entry in entries:
596
+ if entry.resolved_path in matched_paths:
597
+ continue
598
+ matched_paths.add(entry.resolved_path)
599
+ try:
600
+ parser = self._load_catalog_entry(entry, phase_recorder=phase_recorder)
601
+ except PluginFuelExhausted:
602
+ # Terminal, never a skip. Fuel exhaustion (now possible during
603
+ # instantiation since the wasmtime 47 binding) means THIS plugin hit
604
+ # the user's CPU cap — trying the next candidate would either
605
+ # mis-parse the file or dress the fuel problem up as
606
+ # PluginNotFoundError('unknown'). The whole point of the fuel
607
+ # contract is to fail explicitly rather than degrade silently.
608
+ raise
609
+ except Exception as exc:
610
+ logger.debug(
611
+ "Skipping parser candidate %s after load failure: %s",
612
+ ",".join(entry.entry_names),
613
+ exc,
614
+ )
615
+ continue
616
+ if allowed is not None and parser.grammar_id not in allowed:
617
+ continue
618
+ with _record_phase(phase_recorder, "parser_plugin_language_detection"):
619
+ lang = parser.detect_language(filename, content[:2048])
620
+ if lang:
621
+ return parser, lang
622
+ if entries is primary_entries and primary_entries:
623
+ break
624
+
625
+ raise PluginNotFoundError("unknown", filename)
626
+
627
+ def detect_by_content(
628
+ self,
629
+ content: str,
630
+ candidates: list[str] | None = None,
631
+ preferred_plugins: dict[str, str] | None = None,
632
+ plugin_id: str | None = None,
633
+ ) -> list[DetectionResult]:
634
+ """Return all parsers that claim to handle *content*, ranked by priority.
635
+
636
+ Parameters
637
+ ----------
638
+ content:
639
+ Source code to identify (first 4096 bytes are inspected).
640
+ candidates:
641
+ Optional pre-filter. When supplied, only parsers whose
642
+ ``language_ids`` overlap with *candidates* are consulted.
643
+ """
644
+ if plugin_id:
645
+ parser = self.get_parser_by_id(plugin_id)
646
+ allowed = self._config.allowed_plugins
647
+ if allowed is not None and parser.grammar_id not in allowed:
648
+ raise PluginNotFoundError(plugin_id)
649
+ if candidates and not any(c in parser.language_ids for c in candidates):
650
+ raise PluginNotFoundError(plugin_id)
651
+ lang = parser.detect_language("", content[:4096])
652
+ if not lang or (candidates and lang not in candidates):
653
+ raise PluginNotFoundError(plugin_id)
654
+ return [
655
+ DetectionResult(
656
+ language=lang,
657
+ grammar_id=parser.grammar_id,
658
+ confidence=1.0,
659
+ )
660
+ ]
661
+
662
+ ranked = sorted(self.parsers, key=lambda p: p.priority, reverse=True)
663
+ allowed = self._config.allowed_plugins
664
+ raw_results: list[tuple[str, str, int, bool]] = []
665
+ for parser in ranked:
666
+ if allowed is not None and parser.grammar_id not in allowed:
667
+ continue
668
+ if candidates and not any(c in parser.language_ids for c in candidates):
669
+ continue
670
+ lang = parser.detect_language("", content[:4096])
671
+ if lang:
672
+ preferred = bool(
673
+ preferred_plugins
674
+ and preferred_plugins.get(lang) == parser.plugin_id
675
+ )
676
+ raw_results.append((lang, parser.grammar_id, parser.priority, preferred))
677
+ raw_results.sort(key=lambda item: (not item[3], -item[2], item[0], item[1]))
678
+ results: list[DetectionResult] = []
679
+ for rank, (lang, grammar_id, _priority, _preferred) in enumerate(raw_results):
680
+ results.append(
681
+ DetectionResult(
682
+ language=lang,
683
+ grammar_id=grammar_id,
684
+ confidence=round(1.0 / (rank + 1), 3),
685
+ )
686
+ )
687
+ return results
688
+
689
+ def example_for(self, language: str, plugin_id: str | None = None) -> dict[str, str] | None:
690
+ """Return the ``{old, new}`` playground example from the parser that
691
+ owns *language*, or ``None`` if no parser claims it or the parser
692
+ has no example."""
693
+ if plugin_id:
694
+ parser = self.get_parser_by_id(plugin_id, language=language)
695
+ return parser.playground_example(language)
696
+ for parser in sorted(self.parsers, key=lambda p: p.priority, reverse=True):
697
+ if language in parser.language_ids:
698
+ return parser.playground_example(language)
699
+ return None
700
+
701
+ # ── Renderer access ──────────────────────────────────────────────────────
702
+
703
+ @property
704
+ def renderers(self) -> list[RendererAdapter]:
705
+ if self._renderers is None:
706
+ with self._lock:
707
+ if self._renderers is None:
708
+ self._renderers = _load_renderers(fuel=self._config.plugin_fuel)
709
+ return self._renderers
710
+
711
+ def get_renderer(self, format_name: str) -> RendererAdapter:
712
+ """Return the highest-priority renderer for ``format_name``."""
713
+ candidates = sorted(self.renderers, key=lambda r: r.priority, reverse=True)
714
+ for renderer in candidates:
715
+ if renderer.format_name == format_name:
716
+ return renderer
717
+ raise KeyError(
718
+ f"No renderer found for format {format_name!r}. "
719
+ f"Available: {[r.format_name for r in self.renderers]}"
720
+ )
721
+
722
+ # ── Enricher access ──────────────────────────────────────────────────────
723
+
724
+ @property
725
+ def enrichers(self) -> list[EnricherAdapter]:
726
+ if self._enrichers is None:
727
+ with self._lock:
728
+ if self._enrichers is None:
729
+ self._enrichers = _load_enrichers(fuel=self._config.plugin_fuel)
730
+ return self._enrichers
731
+
732
+ def get_enrichers(self, language: str) -> list[EnricherAdapter]:
733
+ """Return enrichers that handle ``language``, sorted by priority descending."""
734
+ return sorted(
735
+ [e for e in self.enrichers if language in e.language_ids],
736
+ key=lambda e: e.priority,
737
+ reverse=True,
738
+ )
739
+
740
+ # ── Diff-analyzer access ───────────────────────────────────────────────
741
+
742
+ @property
743
+ def diff_analyzers(self) -> list[DiffAnalyzerAdapter]:
744
+ if self._diff_analyzers is None:
745
+ with self._lock:
746
+ if self._diff_analyzers is None:
747
+ self._diff_analyzers = _load_diff_analyzers(
748
+ fuel=self._config.plugin_fuel
749
+ )
750
+ return self._diff_analyzers
751
+
752
+ def get_diff_analyzers(self, language: str) -> list[DiffAnalyzerAdapter]:
753
+ """Return diff analyzers that handle ``language``, sorted by priority descending."""
754
+ return sorted(
755
+ [a for a in self.diff_analyzers if language in a.language_ids],
756
+ key=lambda a: a.priority,
757
+ reverse=True,
758
+ )
759
+
760
+
761
+ # ---------------------------------------------------------------------------
762
+ # Internal loading helpers
763
+ # ---------------------------------------------------------------------------
764
+
765
+
766
+ def _provenance(ep: importlib.metadata.EntryPoint) -> str:
767
+ """Return a human-readable provenance string for an entry point."""
768
+ dist = ep.dist
769
+ if dist is None:
770
+ return ""
771
+ meta = dist.metadata
772
+ name = meta.get("Name") or dist.name
773
+ version = meta.get("Version") or "?"
774
+ author = meta.get("Author") or ""
775
+ if not author:
776
+ # RFC 822 Author-email header may contain "Name <email>" format
777
+ author_email = meta.get("Author-email") or ""
778
+ if "<" in author_email:
779
+ author = author_email.split("<")[0].strip().strip('"')
780
+ elif author_email:
781
+ author = author_email
782
+ if author:
783
+ return f"{name} {version} ({author})"
784
+ return f"{name} {version}"
785
+
786
+
787
+ def _distribution_metadata(ep: importlib.metadata.EntryPoint) -> dict[str, str]:
788
+ """Return package metadata used for host-owned plugin info fields."""
789
+ dist = ep.dist
790
+ if dist is None:
791
+ return {"name": "", "version": "", "author": ""}
792
+ meta = dist.metadata
793
+ name = meta.get("Name") or dist.name or ""
794
+ version = meta.get("Version") or ""
795
+ author = meta.get("Author") or ""
796
+ if not author:
797
+ author_email = meta.get("Author-email") or ""
798
+ if "<" in author_email:
799
+ author = author_email.split("<")[0].strip().strip('"')
800
+ elif author_email:
801
+ author = author_email
802
+ return {"name": name, "version": version, "author": author}
803
+
804
+
805
+ def _slug(value: str) -> str:
806
+ text = value.strip().lower().replace("_", "-")
807
+ return re.sub(r"[^a-z0-9.-]+", "-", text).strip("-") or "plugin"
808
+
809
+
810
+ def _make_plugin_id(
811
+ ep: importlib.metadata.EntryPoint,
812
+ plugin_key: str,
813
+ used_ids: set[str],
814
+ ) -> str:
815
+ meta = _distribution_metadata(ep)
816
+ base = f"{_slug(meta['name'] or 'unknown')}:{_slug(plugin_key)}:{_slug(ep.name)}"
817
+ plugin_id = base
818
+ suffix = 2
819
+ while plugin_id in used_ids:
820
+ plugin_id = f"{base}:{suffix}"
821
+ suffix += 1
822
+ used_ids.add(plugin_id)
823
+ return plugin_id
824
+
825
+
826
+ def _assign_parser_metadata(
827
+ adapter: ParserAdapter,
828
+ ep: importlib.metadata.EntryPoint,
829
+ used_ids: set[str],
830
+ ) -> None:
831
+ meta = _distribution_metadata(ep)
832
+ adapter.package_name = meta["name"]
833
+ adapter.package_version = meta["version"]
834
+ adapter.author = meta["author"]
835
+ adapter.provenance = _provenance(ep)
836
+ # Do not call guest exports during discovery. Very low fuel configurations
837
+ # intentionally exercise runtime exhaustion, and discovery should not drop
838
+ # every parser before the parse attempt gets a chance to fail clearly.
839
+ adapter.plugin_id = _make_plugin_id(ep, ep.name, used_ids)
840
+
841
+
842
+ @contextmanager
843
+ def _record_phase(
844
+ recorder: PhaseRecorder | None,
845
+ name: str,
846
+ ) -> Iterator[None]:
847
+ if recorder is None:
848
+ yield
849
+ return
850
+ started = time.perf_counter()
851
+ try:
852
+ yield
853
+ finally:
854
+ recorder(name, (time.perf_counter() - started) * 1000)
855
+
856
+
857
+ def _assign_catalog_metadata(adapter: ParserAdapter, entry: _ParserCatalogEntry) -> None:
858
+ adapter.package_name = entry.package_name
859
+ adapter.package_version = entry.package_version
860
+ adapter.author = entry.author
861
+ adapter.provenance = entry.provenance
862
+ adapter.plugin_id = entry.plugin_id
863
+
864
+
865
+ def _fallback_info_for_catalog(
866
+ entry: _ParserCatalogEntry,
867
+ language_id: str,
868
+ ):
869
+ return fallback_language_info(
870
+ language_id,
871
+ plugin_id=entry.plugin_id,
872
+ grammar_id=entry.entry_names[0],
873
+ priority=0,
874
+ is_trusted=entry.trusted,
875
+ provenance=entry.provenance,
876
+ author=entry.author,
877
+ plugin_version=entry.package_version,
878
+ )
879
+
880
+
881
+ def _entry_matches_filename(entry: _ParserCatalogEntry, filename: str) -> bool:
882
+ if not filename:
883
+ return False
884
+ name = Path(filename).name.lower()
885
+ full = filename.replace("\\", "/").lower()
886
+ for language_id in entry.language_guesses:
887
+ info = _fallback_info_for_catalog(entry, language_id)
888
+ if name == info.default_filename.lower() or full.endswith(
889
+ "/" + info.default_filename.lower()
890
+ ):
891
+ return True
892
+ for extension in info.language_file_extensions:
893
+ normalized = extension.lower()
894
+ if normalized.startswith("."):
895
+ if name.endswith(normalized):
896
+ return True
897
+ elif name == normalized:
898
+ return True
899
+ return False
900
+
901
+
902
+ def _discover_parser_catalog(
903
+ *,
904
+ load_errors: list[str] | None = None,
905
+ ) -> list[_ParserCatalogEntry]:
906
+ """Discover parser entry points without instantiating Wasm plugins."""
907
+ entries: list[_ParserCatalogEntry] = []
908
+ by_path: dict[str, _ParserCatalogEntry] = {}
909
+ used_ids: set[str] = set()
910
+ discovered = list(importlib.metadata.entry_points(group=_PARSER_GROUP))
911
+ for ep in discovered:
912
+ try:
913
+ if ep.name in _DISABLED_BUILTIN_PARSER_ENTRYPOINTS and _is_trusted_entry_point(ep):
914
+ logger.debug("Skipping disabled first-party parser entry point: %s", ep.name)
915
+ continue
916
+ wasm_path = _wasm_path_from_ep(ep)
917
+ resolved = str(Path(wasm_path).resolve())
918
+ existing = by_path.get(resolved)
919
+ if existing is not None:
920
+ if ep.name not in existing.entry_names:
921
+ existing.entry_names.append(ep.name)
922
+ logger.debug("Cataloged duplicate parser entry point: %s (%s)", ep.name, wasm_path)
923
+ continue
924
+
925
+ meta = _distribution_metadata(ep)
926
+ entry = _ParserCatalogEntry(
927
+ ep=ep,
928
+ wasm_path=wasm_path,
929
+ resolved_path=resolved,
930
+ entry_names=[ep.name],
931
+ plugin_id=_make_plugin_id(ep, ep.name, used_ids),
932
+ package_name=meta["name"],
933
+ package_version=meta["version"],
934
+ author=meta["author"],
935
+ provenance=_provenance(ep),
936
+ trusted=_is_trusted_entry_point(ep),
937
+ )
938
+ entries.append(entry)
939
+ by_path[resolved] = entry
940
+ logger.debug("Cataloged parser plugin: %s (%s)", ep.name, wasm_path)
941
+ except Exception as exc:
942
+ message = f"{ep.name}: {exc}"
943
+ if load_errors is not None:
944
+ load_errors.append(message)
945
+ logger.warning("Failed to catalog parser plugin %r: %s", ep.name, exc)
946
+ if all(isinstance(ep, importlib.metadata.EntryPoint) for ep in discovered):
947
+ _add_first_party_parser_entrypoint_fallbacks(entries, by_path, used_ids)
948
+ return entries
949
+
950
+
951
+ def _add_first_party_parser_entrypoint_fallbacks(
952
+ entries: list[_ParserCatalogEntry],
953
+ by_path: dict[str, _ParserCatalogEntry],
954
+ used_ids: set[str],
955
+ ) -> None:
956
+ existing_names = {name for entry in entries for name in entry.entry_names}
957
+ if set(_FIRST_PARTY_PARSER_ENTRYPOINT_FALLBACKS).issubset(existing_names):
958
+ return
959
+ from intentumdiff.plugins import builtins
960
+
961
+ for name, callable_name in _FIRST_PARTY_PARSER_ENTRYPOINT_FALLBACKS.items():
962
+ if name in existing_names:
963
+ continue
964
+ entry_callable = getattr(builtins, callable_name, None)
965
+ if entry_callable is None:
966
+ continue
967
+ wasm_path = entry_callable()
968
+ if not Path(wasm_path).exists():
969
+ continue
970
+ resolved = str(Path(wasm_path).resolve())
971
+ existing = by_path.get(resolved)
972
+ if existing is not None:
973
+ existing.entry_names.append(name)
974
+ continue
975
+ ep = importlib.metadata.EntryPoint(
976
+ name=name,
977
+ value=f"intentumdiff.plugins.builtins:{callable_name}",
978
+ group=_PARSER_GROUP,
979
+ )
980
+ plugin_id = f"intentumdiff:{name}:{name}"
981
+ suffix = 2
982
+ while plugin_id in used_ids:
983
+ plugin_id = f"intentumdiff:{name}:{name}:{suffix}"
984
+ suffix += 1
985
+ used_ids.add(plugin_id)
986
+ entry = _ParserCatalogEntry(
987
+ ep=ep,
988
+ wasm_path=wasm_path,
989
+ resolved_path=resolved,
990
+ entry_names=[name],
991
+ plugin_id=plugin_id,
992
+ package_name="intentumdiff",
993
+ package_version="",
994
+ author="",
995
+ provenance="IntentumDiff built-in fallback",
996
+ trusted=True,
997
+ )
998
+ entries.append(entry)
999
+ by_path[resolved] = entry
1000
+ logger.debug("Cataloged fallback parser plugin: %s (%s)", name, wasm_path)
1001
+
1002
+
1003
+ def _load_parsers(
1004
+ fuel: int = 10_000_000,
1005
+ *,
1006
+ load_errors: list[str] | None = None,
1007
+ ) -> list[ParserAdapter]:
1008
+ """Discover and instantiate all registered parser plugins.
1009
+
1010
+ Entry points for multi-language parsers (e.g. javascript/typescript/tsx)
1011
+ each point at the same wasm file. We deduplicate by resolved wasm path so
1012
+ that each physical plugin is loaded exactly once.
1013
+ """
1014
+ adapters: list[ParserAdapter] = []
1015
+ for entry in _discover_parser_catalog(load_errors=load_errors):
1016
+ try:
1017
+ plugin = load_plugin(entry.wasm_path, fuel, trusted=entry.trusted)
1018
+ adapter = ParserAdapter(plugin)
1019
+ _assign_catalog_metadata(adapter, entry)
1020
+ adapters.append(adapter)
1021
+ logger.debug(
1022
+ "Loaded parser plugin: %s (%s)",
1023
+ ",".join(entry.entry_names),
1024
+ entry.wasm_path,
1025
+ )
1026
+ except Exception as exc:
1027
+ message = f"{entry.entry_names[0]}: {exc}"
1028
+ if load_errors is not None:
1029
+ load_errors.append(message)
1030
+ logger.warning("Failed to load parser plugin %r: %s", entry.entry_names[0], exc)
1031
+ return adapters
1032
+
1033
+
1034
+ def _load_renderers(fuel: int = 10_000_000) -> list[RendererAdapter]:
1035
+ """Discover and instantiate all registered renderer plugins."""
1036
+ adapters: list[RendererAdapter] = []
1037
+ for ep in importlib.metadata.entry_points(group=_RENDERER_GROUP):
1038
+ try:
1039
+ wasm_path = _wasm_path_from_ep(ep)
1040
+ plugin = load_plugin(wasm_path, fuel, trusted=_is_trusted_entry_point(ep))
1041
+ adapter = RendererAdapter(plugin)
1042
+ adapter.provenance = _provenance(ep)
1043
+ adapters.append(adapter)
1044
+ logger.debug("Loaded renderer plugin: %s (%s)", ep.name, wasm_path)
1045
+ except Exception as exc:
1046
+ logger.warning("Failed to load renderer plugin %r: %s", ep.name, exc)
1047
+ return adapters
1048
+
1049
+
1050
+ def _load_enrichers(fuel: int = 10_000_000) -> list[EnricherAdapter]:
1051
+ """Discover and instantiate all registered enricher plugins."""
1052
+ adapters: list[EnricherAdapter] = []
1053
+ for ep in importlib.metadata.entry_points(group=_ENRICHER_GROUP):
1054
+ try:
1055
+ wasm_path = _wasm_path_from_ep(ep)
1056
+ plugin = load_plugin(wasm_path, fuel, trusted=_is_trusted_entry_point(ep))
1057
+ adapter = EnricherAdapter(plugin)
1058
+ adapter.provenance = _provenance(ep)
1059
+ adapters.append(adapter)
1060
+ logger.debug("Loaded enricher plugin: %s (%s)", ep.name, wasm_path)
1061
+ except Exception as exc:
1062
+ logger.warning("Failed to load enricher plugin %r: %s", ep.name, exc)
1063
+ return adapters
1064
+
1065
+
1066
+ def _load_diff_analyzers(fuel: int = 10_000_000) -> list[DiffAnalyzerAdapter]:
1067
+ """Discover and instantiate all registered diff-analyzer plugins."""
1068
+ adapters: list[DiffAnalyzerAdapter] = []
1069
+ for ep in importlib.metadata.entry_points(group=_DIFF_ANALYZER_GROUP):
1070
+ try:
1071
+ wasm_path = _wasm_path_from_ep(ep)
1072
+ plugin = load_plugin(wasm_path, fuel, trusted=_is_trusted_entry_point(ep))
1073
+ adapter = DiffAnalyzerAdapter(plugin)
1074
+ adapter.provenance = _provenance(ep)
1075
+ adapters.append(adapter)
1076
+ logger.debug("Loaded diff-analyzer plugin: %s (%s)", ep.name, wasm_path)
1077
+ except Exception as exc:
1078
+ logger.warning("Failed to load diff-analyzer plugin %r: %s", ep.name, exc)
1079
+ return adapters