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,318 @@
1
+ """Project-level protected semantic change guardrails."""
2
+
3
+ from __future__ import annotations
4
+
5
+ import json
6
+ from collections.abc import Iterable
7
+ from dataclasses import dataclass
8
+ from pathlib import Path
9
+ from typing import Any
10
+
11
+ import yaml
12
+
13
+ from intentumdiff.analysis.diagnostics import DiagnosticsRecorder
14
+ from intentumdiff.analysis.keyed_profiles import KEYED_DATA_LANGUAGES
15
+ from intentumdiff.analysis.resource_profiles import RESOURCE_PROFILE_LANGUAGES
16
+ from intentumdiff.core.models import (
17
+ DiffConfig,
18
+ GuardrailSeverity,
19
+ GuardrailViolation,
20
+ SemanticDiff,
21
+ SemanticNode,
22
+ )
23
+
24
+ GUARDRAIL_POLICY_FILENAME = "intentumdiff.yaml"
25
+ GUARDRAIL_CONFIG_LANGUAGES = KEYED_DATA_LANGUAGES | RESOURCE_PROFILE_LANGUAGES
26
+
27
+
28
+ @dataclass(frozen=True)
29
+ class ProtectedRule:
30
+ rule_id: str
31
+ severity: GuardrailSeverity
32
+ language: str
33
+ path: str
34
+ message: str
35
+ files: tuple[str, ...] = ()
36
+
37
+
38
+ @dataclass(frozen=True)
39
+ class GuardrailPolicy:
40
+ path: Path | None
41
+ rules: tuple[ProtectedRule, ...]
42
+
43
+
44
+ def guardrails_may_apply(
45
+ filename: str,
46
+ new_filename: str | None,
47
+ config: DiffConfig,
48
+ ) -> bool:
49
+ """Return true when guardrails may affect the final diff result."""
50
+
51
+ if not config.guardrails_enabled:
52
+ return False
53
+ if _is_policy_file(filename) or (new_filename is not None and _is_policy_file(new_filename)):
54
+ return True
55
+ policy_path = _find_policy_path(filename, config.guardrail_policy_path)
56
+ if policy_path is None:
57
+ return False
58
+ try:
59
+ return bool(load_guardrail_policy(filename, explicit_path=policy_path).rules)
60
+ except ValueError:
61
+ return True
62
+
63
+
64
+ def load_guardrail_policy(
65
+ filename: str,
66
+ *,
67
+ explicit_path: Path | None = None,
68
+ ) -> GuardrailPolicy:
69
+ policy_path = _find_policy_path(filename, explicit_path)
70
+ if policy_path is None:
71
+ return GuardrailPolicy(path=None, rules=())
72
+
73
+ raw = yaml.safe_load(policy_path.read_text(encoding="utf-8")) or {}
74
+ if not isinstance(raw, dict):
75
+ raise ValueError(f"{policy_path} must contain a YAML mapping")
76
+
77
+ guardrails = raw.get("guardrails", raw)
78
+ if not isinstance(guardrails, dict):
79
+ raise ValueError(f"{policy_path} guardrails section must be a mapping")
80
+
81
+ protected = guardrails.get("protected", [])
82
+ if protected is None:
83
+ protected = []
84
+ if not isinstance(protected, list):
85
+ raise ValueError(f"{policy_path} guardrails.protected must be a list")
86
+
87
+ rules = tuple(_parse_rule(item, index, policy_path) for index, item in enumerate(protected))
88
+ return GuardrailPolicy(path=policy_path, rules=rules)
89
+
90
+
91
+ def apply_guardrails_to_diff(
92
+ diff: SemanticDiff,
93
+ *,
94
+ old_tree: SemanticNode | None,
95
+ new_tree: SemanticNode | None,
96
+ old_source: str,
97
+ new_source: str,
98
+ config: DiffConfig,
99
+ diagnostics: DiagnosticsRecorder | None = None,
100
+ ) -> SemanticDiff:
101
+ if not config.guardrails_enabled:
102
+ return diff
103
+
104
+ violations: list[GuardrailViolation] = []
105
+ if (old_source != new_source) and (
106
+ _is_policy_file(diff.old_filename) or _is_policy_file(diff.new_filename)
107
+ ):
108
+ violations.append(
109
+ GuardrailViolation(
110
+ rule_id="intentumdiff.policy_file",
111
+ severity=GuardrailSeverity.IMMUTABLE,
112
+ file=diff.new_filename or diff.old_filename,
113
+ language=diff.language,
114
+ semantic_path=GUARDRAIL_POLICY_FILENAME,
115
+ old_value="project policy",
116
+ new_value="project policy",
117
+ message="Project guardrail policy changed",
118
+ )
119
+ )
120
+
121
+ policy = load_guardrail_policy(
122
+ diff.new_filename or diff.old_filename,
123
+ explicit_path=config.guardrail_policy_path,
124
+ )
125
+ if policy.rules and old_tree is not None and new_tree is not None:
126
+ violations.extend(_evaluate_policy_rules(diff, old_tree, new_tree, policy.rules))
127
+
128
+ if not violations:
129
+ return diff
130
+
131
+ if diagnostics is not None and diagnostics.enabled:
132
+ for violation in violations:
133
+ diagnostics.record(
134
+ stage="guardrails",
135
+ action="policy_violation",
136
+ rule_id=violation.rule_id,
137
+ reason=violation.message or "protected semantic path changed",
138
+ old_node_ids=[violation.old_node_id] if violation.old_node_id else [],
139
+ new_node_ids=[violation.new_node_id] if violation.new_node_id else [],
140
+ old_labels=[violation.old_value] if violation.old_value else [],
141
+ new_labels=[violation.new_value] if violation.new_value else [],
142
+ confidence=1.0,
143
+ metadata={
144
+ "severity": violation.severity.value,
145
+ "file": violation.file,
146
+ "language": violation.language,
147
+ "semantic_path": violation.semantic_path,
148
+ **(
149
+ {
150
+ "line": violation.position.start_line,
151
+ "column": violation.position.start_col,
152
+ }
153
+ if violation.position is not None
154
+ else {}
155
+ ),
156
+ },
157
+ )
158
+
159
+ metadata = dict(diff.metadata)
160
+ metadata["guardrails"] = {
161
+ "violation_count": len(violations),
162
+ "immutable_count": sum(
163
+ violation.severity == GuardrailSeverity.IMMUTABLE for violation in violations
164
+ ),
165
+ }
166
+ return diff.model_copy(
167
+ update={
168
+ "guardrail_violations": [*diff.guardrail_violations, *violations],
169
+ "metadata": metadata,
170
+ }
171
+ )
172
+
173
+
174
+ def _parse_rule(raw: Any, index: int, policy_path: Path) -> ProtectedRule:
175
+ if not isinstance(raw, dict):
176
+ raise ValueError(f"{policy_path} guardrails.protected[{index}] must be a mapping")
177
+
178
+ language = str(raw.get("language", "")).strip().lower()
179
+ if language not in GUARDRAIL_CONFIG_LANGUAGES:
180
+ raise ValueError(
181
+ f"{policy_path} guardrails.protected[{index}] targets unsupported "
182
+ f"language {language!r}; protected guardrails currently support "
183
+ f"{sorted(GUARDRAIL_CONFIG_LANGUAGES)}"
184
+ )
185
+
186
+ path = _normalise_path(raw.get("path", ""))
187
+ if not path:
188
+ raise ValueError(f"{policy_path} guardrails.protected[{index}] requires path")
189
+
190
+ severity_text = str(raw.get("severity", GuardrailSeverity.IMPORTANT.value)).lower()
191
+ try:
192
+ severity = GuardrailSeverity(severity_text)
193
+ except ValueError as exc:
194
+ raise ValueError(
195
+ f"{policy_path} guardrails.protected[{index}] has unsupported "
196
+ f"severity {severity_text!r}"
197
+ ) from exc
198
+
199
+ files = raw.get("files", ())
200
+ if isinstance(files, str):
201
+ file_patterns = (files,)
202
+ elif isinstance(files, list):
203
+ file_patterns = tuple(str(item) for item in files)
204
+ elif files in (None, ()):
205
+ file_patterns = ()
206
+ else:
207
+ raise ValueError(f"{policy_path} guardrails.protected[{index}].files is invalid")
208
+
209
+ return ProtectedRule(
210
+ rule_id=str(raw.get("id") or f"guardrail.{index + 1}"),
211
+ severity=severity,
212
+ language=language,
213
+ path=path,
214
+ message=str(raw.get("message") or f"Protected path {path} changed"),
215
+ files=file_patterns,
216
+ )
217
+
218
+
219
+ def _evaluate_policy_rules(
220
+ diff: SemanticDiff,
221
+ old_tree: SemanticNode,
222
+ new_tree: SemanticNode,
223
+ rules: Iterable[ProtectedRule],
224
+ ) -> list[GuardrailViolation]:
225
+ """Rust-authoritative (#91 A1.3b): rule matching + violation construction run in
226
+ the Rust core (``evaluate_guardrail_rules_json``), which reuses the same
227
+ semantic-path derivation. Python marshals the parsed policy + the diff's
228
+ changed-node ids in and rebuilds GuardrailViolation objects out; the Python
229
+ matching mirror (rule loop, ``_semantic_paths``, ``_node_value_summary``,
230
+ ``_file_matches``) was deleted. Returns ``[]`` when the core is unavailable.
231
+ """
232
+ from intentumdiff.rust_core import try_rust_evaluate_guardrail_rules
233
+
234
+ request = {
235
+ "language": diff.language,
236
+ "old_filename": diff.old_filename,
237
+ "new_filename": diff.new_filename,
238
+ "old_tree": json.loads(old_tree.model_dump_json()),
239
+ "new_tree": json.loads(new_tree.model_dump_json()),
240
+ "changes": [
241
+ {
242
+ "old_node_id": change.old_node.id if change.old_node is not None else None,
243
+ "new_node_id": change.new_node.id if change.new_node is not None else None,
244
+ }
245
+ for change in diff.changes
246
+ ],
247
+ "rules": [
248
+ {
249
+ "rule_id": rule.rule_id,
250
+ "severity": rule.severity.value,
251
+ "language": rule.language,
252
+ "path": rule.path,
253
+ "message": rule.message,
254
+ "files": list(rule.files),
255
+ }
256
+ for rule in rules
257
+ ],
258
+ }
259
+ violations = try_rust_evaluate_guardrail_rules(request)
260
+ if not violations:
261
+ return []
262
+ return [
263
+ GuardrailViolation(
264
+ rule_id=item["rule_id"],
265
+ severity=GuardrailSeverity(item["severity"]),
266
+ file=item["file"],
267
+ language=item["language"],
268
+ semantic_path=item["semantic_path"],
269
+ node_type=item.get("node_type", ""),
270
+ old_node_id=item.get("old_node_id"),
271
+ new_node_id=item.get("new_node_id"),
272
+ position=item.get("position"),
273
+ old_value=item.get("old_value", ""),
274
+ new_value=item.get("new_value", ""),
275
+ message=item.get("message", ""),
276
+ )
277
+ for item in violations
278
+ ]
279
+
280
+
281
+ def _normalise_path(value: Any) -> str:
282
+ text = str(value).strip().strip(".")
283
+ return ".".join(_clean_path_part(part) for part in text.split(".") if part)
284
+
285
+
286
+ def _clean_path_part(value: str) -> str:
287
+ text = str(value).strip().strip('"').strip("'")
288
+ return text.lower()
289
+
290
+
291
+ def _is_policy_file(filename: str | None) -> bool:
292
+ return Path(filename or "").name.lower() == GUARDRAIL_POLICY_FILENAME
293
+
294
+
295
+ def _find_policy_path(filename: str, explicit_path: Path | None) -> Path | None:
296
+ if explicit_path is not None:
297
+ return explicit_path if explicit_path.exists() else None
298
+
299
+ starts: list[Path] = []
300
+ raw = Path(filename)
301
+ if filename and not filename.startswith("<"):
302
+ starts.append(raw if raw.is_dir() else raw.parent)
303
+ starts.append(Path.cwd())
304
+
305
+ seen: set[Path] = set()
306
+ for start in starts:
307
+ try:
308
+ current = start.resolve()
309
+ except OSError:
310
+ current = Path.cwd()
311
+ for directory in (current, *current.parents):
312
+ if directory in seen:
313
+ continue
314
+ seen.add(directory)
315
+ candidate = directory / GUARDRAIL_POLICY_FILENAME
316
+ if candidate.exists():
317
+ return candidate
318
+ return None