intentumdiff-python 0.0.1__py3-none-win_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/intentumdiff_rust_core.dll +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,816 @@
1
+ """Runtime JSON/YAML schema discovery, safe fetch, and cached hints."""
2
+
3
+ from __future__ import annotations
4
+
5
+ import hashlib
6
+ import ipaddress
7
+ import json
8
+ import logging
9
+ import os
10
+ import re
11
+ import socket
12
+ import ssl
13
+ import subprocess
14
+ import time
15
+ import urllib.error
16
+ import urllib.parse
17
+ import urllib.request
18
+ from collections.abc import Callable, Mapping
19
+ from dataclasses import dataclass
20
+ from pathlib import Path
21
+ from typing import Any
22
+
23
+ from intentumdiff.analysis.user_schemas import (
24
+ load_user_schema_profiles,
25
+ match_user_profile,
26
+ )
27
+
28
+ logger = logging.getLogger(__name__)
29
+
30
+ DEFAULT_SCHEMA_TTL_SECONDS = 86_400
31
+ DEFAULT_TIMEOUT_SECONDS = 5.0
32
+ DEFAULT_MAX_BYTES = 5 * 1024 * 1024
33
+ IDENTITY_FIELD_CANDIDATES = frozenset(
34
+ {
35
+ "git",
36
+ "id",
37
+ "job_cluster_key",
38
+ "key",
39
+ "local",
40
+ "name",
41
+ "operationid",
42
+ "package",
43
+ "task",
44
+ "task_key",
45
+ }
46
+ )
47
+
48
+ DBT_SCHEMA_BASE = "https://raw.githubusercontent.com/dbt-labs/dbt-jsonschema/main/schemas/latest"
49
+ DBT_SCHEMA_URLS = {
50
+ "dbt_cloud": f"{DBT_SCHEMA_BASE}/dbt_cloud-latest.json",
51
+ "dbt_project": f"{DBT_SCHEMA_BASE}/dbt_project-latest.json",
52
+ "dbt_yml_files": f"{DBT_SCHEMA_BASE}/dbt_yml_files-latest.json",
53
+ "dependencies": f"{DBT_SCHEMA_BASE}/dependencies-latest.json",
54
+ "packages": f"{DBT_SCHEMA_BASE}/packages-latest.json",
55
+ "selectors": f"{DBT_SCHEMA_BASE}/selectors-latest.json",
56
+ }
57
+ ADVISORY_ADF_NO_RAW_SCHEMA_URL = (
58
+ "https://learn.microsoft.com/en-sg/answers/questions/2125264/"
59
+ "are-there-json-schemas-for-adf-source-files"
60
+ )
61
+ DATABRICKS_BUNDLE_SCHEMA_URL = (
62
+ "https://raw.githubusercontent.com/databricks/cli/main/bundle/schema/jsonschema.json"
63
+ )
64
+ OPENAPI_31_SCHEMA_URL = "https://spec.openapis.org/oas/3.1/schema/2022-10-07"
65
+ OPENAPI_30_SCHEMA_URL = "https://spec.openapis.org/oas/3.0/schema/2021-09-28"
66
+ KUBERNETES_SCHEMA_URL = "https://json.schemastore.org/kubernetes.json"
67
+ GITHUB_WORKFLOW_SCHEMA_URL = "https://json.schemastore.org/github-workflow.json"
68
+ AZURE_PIPELINES_SCHEMA_URL = "https://json.schemastore.org/azure-pipelines.json"
69
+ DBT_IDENTITY_FIELDS = frozenset(
70
+ {
71
+ "exposure",
72
+ "macro",
73
+ "metric",
74
+ "model",
75
+ "models",
76
+ "name",
77
+ "package_name",
78
+ "packages",
79
+ "project_name",
80
+ "resource_type",
81
+ "selector",
82
+ "selectors",
83
+ "seed",
84
+ "snapshot",
85
+ "snapshots",
86
+ "source",
87
+ "sources",
88
+ "test",
89
+ "tests",
90
+ "version",
91
+ }
92
+ )
93
+
94
+
95
+ @dataclass(frozen=True)
96
+ class FetchResponse:
97
+ body: bytes
98
+ final_url: str
99
+ etag: str | None = None
100
+ last_modified: str | None = None
101
+ status: int = 200
102
+
103
+
104
+ @dataclass(frozen=True)
105
+ class SchemaResolution:
106
+ provider_id: str
107
+ status: str
108
+ source_url: str | None = None
109
+ schema: dict[str, Any] | None = None
110
+ identity_fields: frozenset[str] = frozenset()
111
+ cache_fingerprint: str = "schema:none"
112
+ error: str | None = None
113
+
114
+ @property
115
+ def found(self) -> bool:
116
+ return self.schema is not None
117
+
118
+
119
+ @dataclass(frozen=True)
120
+ class SchemaCandidate:
121
+ provider_id: str
122
+ url: str | None
123
+ command: tuple[str, ...] = ()
124
+ status: str | None = None
125
+ advisory_url: str | None = None
126
+ identity_fields: frozenset[str] = frozenset()
127
+
128
+
129
+ FetchFunc = Callable[[str, Mapping[str, str]], FetchResponse]
130
+ CommandRunner = Callable[[tuple[str, ...]], str]
131
+
132
+
133
+ def schema_cache_dir(env: Mapping[str, str] | None = None) -> Path:
134
+ values = env or os.environ
135
+ if os.name == "nt":
136
+ base = Path(values.get("LOCALAPPDATA") or (Path.home() / "AppData" / "Local"))
137
+ else:
138
+ base = Path(values.get("XDG_CACHE_HOME") or (Path.home() / ".cache"))
139
+ return base / "intentumdiff" / "schemas"
140
+
141
+
142
+ def resolve_schema(
143
+ *,
144
+ content: str,
145
+ filename: str,
146
+ language: str | None,
147
+ env: Mapping[str, str] | None = None,
148
+ cache_dir: Path | None = None,
149
+ fetcher: FetchFunc | None = None,
150
+ command_runner: CommandRunner | None = None,
151
+ now: float | None = None,
152
+ ) -> SchemaResolution:
153
+ values = env or os.environ
154
+ mode = _schema_fetch_mode(values)
155
+ declared = discover_declared_schema(content, language)
156
+ # User-registered profiles (issue #63) outrank the embedded fetch and the
157
+ # built-in provider registry: a claimed $schema URL or a filename/marker
158
+ # match resolves OFFLINE from the local descriptor — no fetch, no logging
159
+ # of proprietary schema content.
160
+ user_profiles, _user_errors = load_user_schema_profiles(values)
161
+ if user_profiles:
162
+ profile = match_user_profile(
163
+ user_profiles,
164
+ filename=filename,
165
+ content=content,
166
+ declared_url=declared,
167
+ )
168
+ if profile is not None:
169
+ return SchemaResolution(
170
+ provider_id=profile.provider_id,
171
+ status="user-profile",
172
+ source_url=profile.schema_path,
173
+ schema=profile.schema,
174
+ identity_fields=profile.identity_fields,
175
+ cache_fingerprint=profile.fingerprint,
176
+ )
177
+ candidate = (
178
+ SchemaCandidate("embedded", declared)
179
+ if declared
180
+ else provider_schema_candidate(filename, language, content)
181
+ )
182
+ if candidate is None:
183
+ return SchemaResolution(provider_id="none", status="not_found")
184
+ if candidate.status:
185
+ return SchemaResolution(
186
+ provider_id=candidate.provider_id,
187
+ status=candidate.status,
188
+ source_url=candidate.advisory_url or candidate.url,
189
+ identity_fields=candidate.identity_fields,
190
+ )
191
+ if mode == "off":
192
+ return SchemaResolution(
193
+ provider_id=candidate.provider_id,
194
+ source_url=candidate.advisory_url or candidate.url,
195
+ status="disabled",
196
+ identity_fields=candidate.identity_fields,
197
+ )
198
+
199
+ ttl = _schema_ttl_seconds(values)
200
+ root = cache_dir or schema_cache_dir(values)
201
+ current_time = time.time() if now is None else now
202
+
203
+ if candidate.command:
204
+ if mode == "cache-only":
205
+ return _resolve_from_cache(candidate, root, ttl, current_time, allow_stale=True)
206
+ if _schema_commands_allowed(values):
207
+ try:
208
+ text = (command_runner or _run_command)(candidate.command)
209
+ schema = _parse_schema_text(text)
210
+ fingerprint = _fingerprint_text(candidate.provider_id, text)
211
+ return SchemaResolution(
212
+ provider_id=candidate.provider_id,
213
+ status="command",
214
+ schema=schema,
215
+ identity_fields=_identity_fields_for(candidate, schema),
216
+ cache_fingerprint=fingerprint,
217
+ )
218
+ except Exception as exc:
219
+ logger.debug("Schema command failed for %s", candidate.provider_id, exc_info=True)
220
+ cached = _resolve_from_cache(candidate, root, ttl, current_time, allow_stale=True)
221
+ if cached.found:
222
+ return cached
223
+ if candidate.url:
224
+ candidate = SchemaCandidate(
225
+ candidate.provider_id,
226
+ candidate.url,
227
+ identity_fields=candidate.identity_fields,
228
+ )
229
+ else:
230
+ return SchemaResolution(
231
+ provider_id=candidate.provider_id,
232
+ status="error",
233
+ error=str(exc),
234
+ )
235
+ elif candidate.url:
236
+ candidate = SchemaCandidate(
237
+ candidate.provider_id,
238
+ candidate.url,
239
+ identity_fields=candidate.identity_fields,
240
+ )
241
+ else:
242
+ cached = _resolve_from_cache(candidate, root, ttl, current_time, allow_stale=True)
243
+ if cached.found:
244
+ return cached
245
+ return SchemaResolution(
246
+ provider_id=candidate.provider_id,
247
+ status="command_disabled",
248
+ error=(
249
+ "schema provider command disabled; set "
250
+ "INTENTUMDIFF_SCHEMA_ALLOW_COMMANDS=1 to enable trusted "
251
+ "local schema helper commands"
252
+ ),
253
+ )
254
+
255
+ if not candidate.url:
256
+ return SchemaResolution(provider_id=candidate.provider_id, status="not_found")
257
+ if mode == "cache-only":
258
+ return _resolve_from_cache(candidate, root, ttl, current_time, allow_stale=True)
259
+ try:
260
+ return _fetch_or_cache(
261
+ candidate,
262
+ root,
263
+ ttl,
264
+ current_time,
265
+ values,
266
+ fetcher or _fetch_https,
267
+ )
268
+ except Exception as exc:
269
+ logger.debug("Schema fetch failed for %s", candidate.url, exc_info=True)
270
+ cached = _resolve_from_cache(candidate, root, ttl, current_time, allow_stale=True)
271
+ if cached.found:
272
+ return cached
273
+ return SchemaResolution(
274
+ provider_id=candidate.provider_id,
275
+ source_url=candidate.url,
276
+ status="error",
277
+ error=str(exc),
278
+ )
279
+
280
+
281
+ def discover_declared_schema(content: str, language: str | None = None) -> str | None:
282
+ language_id = (language or "").lower()
283
+ if language_id in {"json", "adf"} or _looks_like_json(content):
284
+ try:
285
+ parsed = json.loads(content)
286
+ except json.JSONDecodeError:
287
+ parsed = None
288
+ if isinstance(parsed, dict):
289
+ value = parsed.get("$schema")
290
+ if isinstance(value, str) and value.strip():
291
+ return value.strip()
292
+
293
+ for line in content.splitlines()[:5]:
294
+ match = re.match(r"\s*#\s*yaml-language-server:\s*\$schema=(\S+)\s*$", line)
295
+ if match:
296
+ return match.group(1).strip()
297
+ return None
298
+
299
+
300
+ def provider_schema_candidate(
301
+ filename: str,
302
+ language: str | None,
303
+ content: str = "",
304
+ ) -> SchemaCandidate | None:
305
+ normalized_path = filename.replace("\\", "/").lower()
306
+ basename = normalized_path.rsplit("/", 1)[-1]
307
+ language_id = (language or "").lower()
308
+
309
+ if url := _openapi_schema_url(basename, content):
310
+ version = "3.1" if url == OPENAPI_31_SCHEMA_URL else "3.0"
311
+ return SchemaCandidate(
312
+ f"openapi:{version}",
313
+ url,
314
+ identity_fields=frozenset({"operationId", "name"}),
315
+ )
316
+ if _looks_like_kubernetes_manifest(basename, content):
317
+ return SchemaCandidate(
318
+ "kubernetes:manifest",
319
+ KUBERNETES_SCHEMA_URL,
320
+ identity_fields=frozenset({"name"}),
321
+ )
322
+ if _looks_like_github_workflow(normalized_path, content):
323
+ return SchemaCandidate(
324
+ "github-actions:workflow",
325
+ GITHUB_WORKFLOW_SCHEMA_URL,
326
+ identity_fields=frozenset({"id", "name", "run", "runs-on", "uses"}),
327
+ )
328
+ if _looks_like_azure_pipeline(normalized_path, content):
329
+ return SchemaCandidate(
330
+ "azure-pipelines:pipeline",
331
+ AZURE_PIPELINES_SCHEMA_URL,
332
+ identity_fields=frozenset({"job", "stage", "task", "script", "displayName"}),
333
+ )
334
+
335
+ dbt_url = _dbt_schema_url(basename, language_id, content)
336
+ if dbt_url:
337
+ key = dbt_url.rsplit("/", 1)[-1].replace("-latest.json", "")
338
+ return SchemaCandidate(
339
+ f"dbt:{key}",
340
+ dbt_url,
341
+ identity_fields=DBT_IDENTITY_FIELDS,
342
+ )
343
+
344
+ if language_id in {"databricks", "databricks-workflow"} or basename in {
345
+ "databricks.yml",
346
+ "databricks.yaml",
347
+ }:
348
+ return SchemaCandidate(
349
+ "databricks:bundle",
350
+ DATABRICKS_BUNDLE_SCHEMA_URL,
351
+ command=("databricks", "bundle", "schema"),
352
+ identity_fields=frozenset(
353
+ {
354
+ "depends_on",
355
+ "job_cluster_key",
356
+ "library",
357
+ "libraries",
358
+ "task_key",
359
+ }
360
+ ),
361
+ )
362
+
363
+ if language_id in {"adf"} or _looks_like_adf_path(basename):
364
+ return SchemaCandidate(
365
+ "adf:no_raw_schema",
366
+ None,
367
+ status="no_raw_schema",
368
+ advisory_url=ADVISORY_ADF_NO_RAW_SCHEMA_URL,
369
+ )
370
+ if language_id in {"hcl", "terraform"} or basename.endswith((".tf", ".hcl")):
371
+ return None
372
+ return None
373
+
374
+
375
+ def derive_identity_fields(schema: dict[str, Any] | None) -> frozenset[str]:
376
+ if not schema:
377
+ return frozenset()
378
+ found: set[str] = set()
379
+ stack: list[Any] = [schema]
380
+ seen: set[int] = set()
381
+ while stack:
382
+ current = stack.pop()
383
+ current_id = id(current)
384
+ if current_id in seen:
385
+ continue
386
+ seen.add(current_id)
387
+ if isinstance(current, dict):
388
+ properties = current.get("properties")
389
+ if isinstance(properties, dict):
390
+ for key, value in properties.items():
391
+ normalized = _normalize_identity_field(key)
392
+ if normalized in IDENTITY_FIELD_CANDIDATES:
393
+ found.add(normalized)
394
+ stack.append(value)
395
+ for key in (
396
+ "items",
397
+ "anyOf",
398
+ "oneOf",
399
+ "allOf",
400
+ "$defs",
401
+ "definitions",
402
+ "patternProperties",
403
+ ):
404
+ value = current.get(key)
405
+ if value is not None:
406
+ stack.append(value)
407
+ elif isinstance(current, list):
408
+ stack.extend(current)
409
+ return frozenset(found)
410
+
411
+
412
+ def _identity_fields_for(
413
+ candidate: SchemaCandidate,
414
+ schema: dict[str, Any] | None,
415
+ ) -> frozenset[str]:
416
+ return frozenset({*candidate.identity_fields, *derive_identity_fields(schema)})
417
+
418
+
419
+ def schema_cache_fingerprint(resolution: SchemaResolution | None) -> str:
420
+ if resolution is None:
421
+ return "schema:none"
422
+ return resolution.cache_fingerprint
423
+
424
+
425
+ def schema_resolution_metadata(
426
+ resolution: SchemaResolution | None,
427
+ ) -> dict[str, Any] | None:
428
+ """Return public diff metadata for a detected schema/provider."""
429
+ if resolution is None or resolution.provider_id == "none":
430
+ return None
431
+ identity_fields = sorted(resolution.identity_fields)
432
+ metadata: dict[str, Any] = {
433
+ "provider_id": resolution.provider_id,
434
+ "status": resolution.status,
435
+ "source_url": resolution.source_url,
436
+ "identity_fields": identity_fields,
437
+ "detected": True,
438
+ "available": bool(resolution.found and identity_fields),
439
+ }
440
+ if resolution.error:
441
+ metadata["error"] = resolution.error
442
+ return metadata
443
+
444
+
445
+ def _fetch_or_cache(
446
+ candidate: SchemaCandidate,
447
+ root: Path,
448
+ ttl: int,
449
+ now: float,
450
+ env: Mapping[str, str],
451
+ fetcher: FetchFunc,
452
+ ) -> SchemaResolution:
453
+ url = candidate.url
454
+ if url is None:
455
+ raise ValueError("schema candidate has no URL to fetch")
456
+ cached = _read_cache(candidate, root)
457
+ if cached and now - cached["fetched_at"] < ttl:
458
+ return _resolution_from_cache(candidate, cached, "cache_hit")
459
+
460
+ _validate_schema_url(url, env)
461
+ headers: dict[str, str] = {
462
+ "Accept": "application/schema+json, application/json;q=0.9, */*;q=0.1"
463
+ }
464
+ if cached:
465
+ if isinstance(cached.get("etag"), str):
466
+ headers["If-None-Match"] = cached["etag"]
467
+ if isinstance(cached.get("last_modified"), str):
468
+ headers["If-Modified-Since"] = cached["last_modified"]
469
+
470
+ try:
471
+ response = fetcher(url, headers)
472
+ except urllib.error.HTTPError as exc:
473
+ if exc.code == 304 and cached:
474
+ return _resolution_from_cache(candidate, cached, "not_modified")
475
+ raise
476
+
477
+ _validate_schema_url(response.final_url, env)
478
+ if response.status == 304 and cached:
479
+ return _resolution_from_cache(candidate, cached, "not_modified")
480
+ if len(response.body) > DEFAULT_MAX_BYTES:
481
+ raise ValueError(f"schema exceeds {DEFAULT_MAX_BYTES} bytes")
482
+ text = response.body.decode("utf-8")
483
+ schema = _parse_schema_text(text)
484
+ content_hash = hashlib.sha256(response.body).hexdigest()
485
+ metadata = {
486
+ "provider_id": candidate.provider_id,
487
+ "source_url": url,
488
+ "final_url": response.final_url,
489
+ "fetched_at": now,
490
+ "etag": response.etag,
491
+ "last_modified": response.last_modified,
492
+ "content_hash": content_hash,
493
+ "byte_size": len(response.body),
494
+ "schema": schema,
495
+ }
496
+ _write_cache(candidate, root, metadata)
497
+ return _resolution_from_cache(candidate, metadata, "fetched")
498
+
499
+
500
+ def _resolve_from_cache(
501
+ candidate: SchemaCandidate,
502
+ root: Path,
503
+ ttl: int,
504
+ now: float,
505
+ *,
506
+ allow_stale: bool,
507
+ ) -> SchemaResolution:
508
+ cached = _read_cache(candidate, root)
509
+ if not cached:
510
+ return SchemaResolution(
511
+ provider_id=candidate.provider_id,
512
+ source_url=candidate.url,
513
+ status="cache_miss",
514
+ identity_fields=candidate.identity_fields,
515
+ )
516
+ if not allow_stale and now - cached["fetched_at"] >= ttl:
517
+ return SchemaResolution(
518
+ provider_id=candidate.provider_id,
519
+ source_url=candidate.url,
520
+ status="cache_stale",
521
+ identity_fields=candidate.identity_fields,
522
+ )
523
+ status = "cache_hit" if now - cached["fetched_at"] < ttl else "stale_cache"
524
+ return _resolution_from_cache(candidate, cached, status)
525
+
526
+
527
+ def _resolution_from_cache(
528
+ candidate: SchemaCandidate,
529
+ cached: Mapping[str, Any],
530
+ status: str,
531
+ ) -> SchemaResolution:
532
+ schema = cached.get("schema")
533
+ if not isinstance(schema, dict):
534
+ schema = None
535
+ content_hash = str(cached.get("content_hash") or "")
536
+ fingerprint = (
537
+ f"schema:{candidate.provider_id}:{content_hash}"
538
+ if content_hash
539
+ else f"schema:{candidate.provider_id}:unknown"
540
+ )
541
+ return SchemaResolution(
542
+ provider_id=candidate.provider_id,
543
+ source_url=str(cached.get("source_url") or candidate.url or ""),
544
+ status=status,
545
+ schema=schema,
546
+ identity_fields=_identity_fields_for(candidate, schema),
547
+ cache_fingerprint=fingerprint,
548
+ )
549
+
550
+
551
+ def _read_cache(candidate: SchemaCandidate, root: Path) -> dict[str, Any] | None:
552
+ path = _cache_file(candidate, root)
553
+ try:
554
+ data = json.loads(path.read_text(encoding="utf-8"))
555
+ if isinstance(data, dict):
556
+ return data
557
+ except Exception:
558
+ return None
559
+ return None
560
+
561
+
562
+ def _write_cache(candidate: SchemaCandidate, root: Path, data: Mapping[str, Any]) -> None:
563
+ path = _cache_file(candidate, root)
564
+ path.parent.mkdir(parents=True, exist_ok=True)
565
+ path.write_text(json.dumps(data, sort_keys=True, indent=2), encoding="utf-8")
566
+
567
+
568
+ def _cache_file(candidate: SchemaCandidate, root: Path) -> Path:
569
+ key = candidate.url or " ".join(candidate.command) or candidate.provider_id
570
+ digest = hashlib.sha256(key.encode("utf-8")).hexdigest()
571
+ return root / f"{digest}.json"
572
+
573
+
574
+ def _fetch_https(url: str, headers: Mapping[str, str]) -> FetchResponse:
575
+ request = urllib.request.Request(url, headers=dict(headers)) # noqa: S310
576
+ context = ssl.create_default_context()
577
+ opener = urllib.request.build_opener(
578
+ _HttpsOnlyRedirectHandler,
579
+ urllib.request.HTTPSHandler(context=context),
580
+ )
581
+ with opener.open( # noqa: S310 - URL and redirects are validated before fetch.
582
+ request,
583
+ timeout=DEFAULT_TIMEOUT_SECONDS,
584
+ ) as response:
585
+ body = response.read(DEFAULT_MAX_BYTES + 1)
586
+ return FetchResponse(
587
+ body=body,
588
+ final_url=response.geturl(),
589
+ etag=response.headers.get("ETag"),
590
+ last_modified=response.headers.get("Last-Modified"),
591
+ status=getattr(response, "status", 200),
592
+ )
593
+
594
+
595
+ class _HttpsOnlyRedirectHandler(urllib.request.HTTPRedirectHandler):
596
+ def redirect_request(
597
+ self,
598
+ req: urllib.request.Request,
599
+ fp: Any,
600
+ code: int,
601
+ msg: str,
602
+ headers: Mapping[str, str],
603
+ newurl: str,
604
+ ) -> urllib.request.Request | None:
605
+ if urllib.parse.urlparse(newurl).scheme.lower() != "https":
606
+ raise urllib.error.HTTPError(
607
+ req.full_url,
608
+ code,
609
+ "schema redirect target must use HTTPS",
610
+ dict(headers),
611
+ fp,
612
+ )
613
+ return super().redirect_request(req, fp, code, msg, headers, newurl)
614
+
615
+
616
+ def _run_command(command: tuple[str, ...]) -> str:
617
+ result = subprocess.run( # noqa: S603 - fixed argv from trusted provider registry.
618
+ command,
619
+ check=True,
620
+ capture_output=True,
621
+ text=True,
622
+ timeout=DEFAULT_TIMEOUT_SECONDS,
623
+ )
624
+ if len(result.stdout.encode("utf-8")) > DEFAULT_MAX_BYTES:
625
+ raise ValueError(f"schema exceeds {DEFAULT_MAX_BYTES} bytes")
626
+ return result.stdout
627
+
628
+
629
+ def _schema_fetch_mode(env: Mapping[str, str]) -> str:
630
+ value = env.get("INTENTUMDIFF_SCHEMA_FETCH", "cache-only").strip().lower()
631
+ return value if value in {"auto", "cache-only", "off"} else "cache-only"
632
+
633
+
634
+ def _schema_commands_allowed(env: Mapping[str, str]) -> bool:
635
+ return env.get("INTENTUMDIFF_SCHEMA_ALLOW_COMMANDS", "").strip().lower() in {
636
+ "1",
637
+ "true",
638
+ "yes",
639
+ }
640
+
641
+
642
+ def _schema_ttl_seconds(env: Mapping[str, str]) -> int:
643
+ raw = env.get("INTENTUMDIFF_SCHEMA_CACHE_TTL_SECONDS", "")
644
+ try:
645
+ return max(0, int(float(raw)))
646
+ except ValueError:
647
+ return DEFAULT_SCHEMA_TTL_SECONDS
648
+
649
+
650
+ def _parse_schema_text(text: str) -> dict[str, Any]:
651
+ parsed = json.loads(text)
652
+ if not isinstance(parsed, dict):
653
+ raise ValueError("schema root is not an object")
654
+ return parsed
655
+
656
+
657
+ def _fingerprint_text(provider_id: str, text: str) -> str:
658
+ digest = hashlib.sha256(text.encode("utf-8")).hexdigest()
659
+ return f"schema:{provider_id}:{digest}"
660
+
661
+
662
+ def _validate_schema_url(url: str, env: Mapping[str, str]) -> None:
663
+ parsed = urllib.parse.urlparse(url)
664
+ if parsed.scheme.lower() != "https":
665
+ raise ValueError("schema URL must use HTTPS")
666
+ if not parsed.hostname:
667
+ raise ValueError("schema URL must include a host")
668
+ if env.get("INTENTUMDIFF_SCHEMA_ALLOW_PRIVATE_HOSTS", "").strip().lower() in {"1", "true", "yes"}:
669
+ return
670
+ if _is_private_host(parsed.hostname):
671
+ raise ValueError("schema URL host resolves to a private or local address")
672
+
673
+
674
+ def _is_private_host(host: str) -> bool:
675
+ try:
676
+ addresses = {info[4][0] for info in socket.getaddrinfo(host, None)}
677
+ except OSError:
678
+ addresses = {host}
679
+ for address in addresses:
680
+ try:
681
+ ip = ipaddress.ip_address(address)
682
+ except ValueError:
683
+ continue
684
+ if (
685
+ ip.is_private
686
+ or ip.is_loopback
687
+ or ip.is_link_local
688
+ or ip.is_multicast
689
+ or ip.is_reserved
690
+ or ip.is_unspecified
691
+ ):
692
+ return True
693
+ return False
694
+
695
+
696
+ def _dbt_schema_url(basename: str, language_id: str, content: str) -> str | None:
697
+ if basename in {"dbt_project.yml", "dbt_project.yaml"}:
698
+ return DBT_SCHEMA_URLS["dbt_project"]
699
+ if basename in {"packages.yml", "packages.yaml"}:
700
+ return DBT_SCHEMA_URLS["packages"]
701
+ if basename in {"dependencies.yml", "dependencies.yaml"}:
702
+ return DBT_SCHEMA_URLS["dependencies"]
703
+ if basename in {"selectors.yml", "selectors.yaml"}:
704
+ return DBT_SCHEMA_URLS["selectors"]
705
+ if basename in {"dbt_cloud.yml", "dbt_cloud.yaml"}:
706
+ return DBT_SCHEMA_URLS["dbt_cloud"]
707
+ if language_id in {"dbt-yaml", "dbt-config", "dbt-packages"}:
708
+ if language_id == "dbt-config":
709
+ return DBT_SCHEMA_URLS["dbt_project"]
710
+ if language_id == "dbt-packages":
711
+ return DBT_SCHEMA_URLS["packages"]
712
+ return DBT_SCHEMA_URLS["dbt_yml_files"]
713
+ markers = ("version", "models", "sources", "exposures", "seeds", "snapshots")
714
+ if basename.endswith((".yml", ".yaml")) and any(
715
+ re.search(rf"(?m)^\s*{re.escape(marker)}\s*:", content) for marker in markers
716
+ ):
717
+ return DBT_SCHEMA_URLS["dbt_yml_files"]
718
+ return None
719
+
720
+
721
+ def _openapi_schema_url(basename: str, content: str) -> str | None:
722
+ if not basename.endswith((".json", ".yaml", ".yml")):
723
+ return None
724
+ lowered = basename.lower()
725
+ named_like_openapi = lowered in {
726
+ "openapi.json",
727
+ "openapi.yaml",
728
+ "openapi.yml",
729
+ "swagger.json",
730
+ "swagger.yaml",
731
+ "swagger.yml",
732
+ }
733
+ version = _top_level_scalar(content, "openapi")
734
+ if version:
735
+ if version.startswith("3.1"):
736
+ return OPENAPI_31_SCHEMA_URL
737
+ return OPENAPI_30_SCHEMA_URL
738
+ if _top_level_scalar(content, "swagger") or named_like_openapi:
739
+ return OPENAPI_30_SCHEMA_URL
740
+ return None
741
+
742
+
743
+ def _looks_like_kubernetes_manifest(basename: str, content: str) -> bool:
744
+ if not basename.endswith((".json", ".yaml", ".yml")):
745
+ return False
746
+ return bool(
747
+ _top_level_scalar(content, "apiVersion")
748
+ and _top_level_scalar(content, "kind")
749
+ and re.search(r"(?m)^\s*metadata\s*:", content)
750
+ )
751
+
752
+
753
+ def _looks_like_github_workflow(path: str, content: str) -> bool:
754
+ if not path.endswith((".yaml", ".yml")):
755
+ return False
756
+ if "/.github/workflows/" in f"/{path}":
757
+ return True
758
+ return bool(
759
+ re.search(r"(?m)^\s*on\s*:", content)
760
+ and re.search(r"(?m)^\s*jobs\s*:", content)
761
+ and (
762
+ re.search(r"(?m)^\s*uses\s*:", content)
763
+ or re.search(r"(?m)^\s*runs-on\s*:", content)
764
+ )
765
+ )
766
+
767
+
768
+ def _looks_like_azure_pipeline(path: str, content: str) -> bool:
769
+ basename = path.rsplit("/", 1)[-1]
770
+ if not path.endswith((".yaml", ".yml")):
771
+ return False
772
+ if basename in {"azure-pipelines.yml", "azure-pipelines.yaml"}:
773
+ return True
774
+ return bool(
775
+ re.search(r"(?m)^\s*(trigger|pr)\s*:", content)
776
+ and re.search(r"(?m)^\s*(stages|jobs|steps)\s*:", content)
777
+ and (
778
+ re.search(r"(?m)^\s*pool\s*:", content)
779
+ or re.search(r"(?m)^\s*vmImage\s*:", content)
780
+ or re.search(r"(?m)^\s*task\s*:", content)
781
+ )
782
+ )
783
+
784
+
785
+ def _top_level_scalar(content: str, key: str) -> str:
786
+ try:
787
+ parsed = json.loads(content)
788
+ except json.JSONDecodeError:
789
+ parsed = None
790
+ if isinstance(parsed, dict):
791
+ value = parsed.get(key)
792
+ return str(value).strip() if value is not None else ""
793
+ match = re.search(rf"(?m)^{re.escape(key)}\s*:\s*['\"]?([^'\"\n#]+)", content)
794
+ return match.group(1).strip() if match else ""
795
+
796
+
797
+ def _looks_like_adf_path(basename: str) -> bool:
798
+ direct_names = {"pipeline.json", "dataset.json", "linkedservice.json", "factory.json"}
799
+ return basename in direct_names or any(
800
+ basename.endswith(suffix)
801
+ for suffix in (
802
+ ".pipeline.json",
803
+ ".dataset.json",
804
+ ".linkedservice.json",
805
+ ".trigger.json",
806
+ )
807
+ )
808
+
809
+
810
+ def _normalize_identity_field(name: str) -> str:
811
+ return name.strip().lower().replace("-", "_")
812
+
813
+
814
+ def _looks_like_json(content: str) -> bool:
815
+ stripped = content.lstrip()
816
+ return stripped.startswith("{")