intentumdiff-python 0.0.1__py3-none-win_amd64.whl

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (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,1207 @@
1
+ """
2
+ intentumdiff.analysis.invariances
3
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
4
+
5
+ Data-defined semantic invariance handling.
6
+
7
+ Rules are declared in packaged YAML and select a small allow-listed evaluator.
8
+ The YAML is data only: it never contains executable predicates, imports, or
9
+ arbitrary expressions.
10
+ """
11
+
12
+ from __future__ import annotations
13
+
14
+ import ast
15
+ import json
16
+ import math
17
+ import re
18
+ from dataclasses import dataclass, field
19
+ from difflib import SequenceMatcher
20
+ from functools import lru_cache
21
+ from importlib import resources
22
+ from typing import Any, Literal
23
+
24
+ import yaml
25
+ from pydantic import BaseModel, Field, field_validator, model_validator
26
+
27
+ from intentumdiff.core.models import (
28
+ Change,
29
+ ChangeGroup,
30
+ ChangeGroupKind,
31
+ ChangeType,
32
+ SemanticNode,
33
+ )
34
+
35
+ _ALLOWED_EVALUATORS = frozenset(
36
+ {
37
+ "css_color_equivalence",
38
+ "style_only_shortcut_evidence",
39
+ "integer_literal_equivalence",
40
+ "string_literal_equivalence",
41
+ "formatting_suppression_evidence",
42
+ "parser_normalization",
43
+ "number_literal_equivalence",
44
+ }
45
+ )
46
+ _KNOWN_LANGUAGE_IDS = frozenset(
47
+ {
48
+ "abap",
49
+ "adf",
50
+ "asciidoc",
51
+ "asm",
52
+ "assemblyscript",
53
+ "astro",
54
+ "bash",
55
+ "c",
56
+ "clojure",
57
+ "cpp",
58
+ "csharp",
59
+ "css",
60
+ "dart",
61
+ "databricks-workflow",
62
+ "dax",
63
+ "delphi",
64
+ "gitignore",
65
+ "dockerfile",
66
+ "elixir",
67
+ "freebasic",
68
+ "generic",
69
+ "go",
70
+ "graphql",
71
+ "groovy",
72
+ "haskell",
73
+ "hcl",
74
+ "html",
75
+ "java",
76
+ "javascript",
77
+ "json",
78
+ "kotlin",
79
+ "latex",
80
+ "lua",
81
+ "m",
82
+ "markdown",
83
+ "ini",
84
+ "gomod",
85
+ "make",
86
+ "cmake",
87
+ "proto",
88
+ "toml",
89
+ "mdx",
90
+ "ocaml",
91
+ "odin",
92
+ "perl",
93
+ "php",
94
+ "plsql",
95
+ "po",
96
+ "postscript",
97
+ "powershell",
98
+ "puppet",
99
+ "python",
100
+ "qsharp",
101
+ "r",
102
+ "reasonml",
103
+ "ruby",
104
+ "rust",
105
+ "sas",
106
+ "scala",
107
+ "scss",
108
+ "sql",
109
+ "squirrel",
110
+ "svelte",
111
+ "swift",
112
+ "tsx",
113
+ "tsql",
114
+ "typescript",
115
+ "vbnet",
116
+ "vue",
117
+ "wat",
118
+ "xml",
119
+ "yaml",
120
+ "zig",
121
+ }
122
+ )
123
+ _COLOR_TOKEN_RE = re.compile(
124
+ r"""
125
+ (?P<hex>\#[0-9a-fA-F]{3}(?:[0-9a-fA-F]{3})?)
126
+ |
127
+ (?P<rgb>\brgb\(\s*
128
+ (?P<r>\d{1,3})
129
+ (?:
130
+ \s*,\s*(?P<g_comma>\d{1,3})\s*,\s*(?P<b_comma>\d{1,3})
131
+ |
132
+ \s+(?P<g_space>\d{1,3})\s+(?P<b_space>\d{1,3})
133
+ )
134
+ \s*\))
135
+ |
136
+ (?P<name>\b[a-zA-Z]+\b)
137
+ """,
138
+ re.VERBOSE,
139
+ )
140
+
141
+ _CSS_NAMED_COLORS = {
142
+ "black": (0, 0, 0),
143
+ "blue": (0, 0, 255),
144
+ "cyan": (0, 255, 255),
145
+ "fuchsia": (255, 0, 255),
146
+ "gray": (128, 128, 128),
147
+ "green": (0, 128, 0),
148
+ "grey": (128, 128, 128),
149
+ "lime": (0, 255, 0),
150
+ "magenta": (255, 0, 255),
151
+ "red": (255, 0, 0),
152
+ "white": (255, 255, 255),
153
+ "yellow": (255, 255, 0),
154
+ }
155
+
156
+
157
+ class InvarianceRuleDefinition(BaseModel, frozen=True):
158
+ """Validated declaration for one packaged invariance rule."""
159
+
160
+ id: str = Field(min_length=1)
161
+ languages: tuple[str, ...] = Field(min_length=1)
162
+ status: Literal["implemented", "catalog"] = "implemented"
163
+ evaluator: str | None = None
164
+ node_types: tuple[str, ...] = Field(default_factory=tuple)
165
+ group_kind: ChangeGroupKind = ChangeGroupKind.IGNORED_STYLE
166
+ enabled: bool = True
167
+ risk: Literal["green", "amber", "red"] = "amber"
168
+ equivalence_kind: str = "canonical_value_equivalence"
169
+ explanation: str = Field(min_length=1)
170
+ examples: tuple[dict[str, Any], ...] = Field(default_factory=tuple)
171
+ stage: str | None = None
172
+ guarantee: str | None = None
173
+ guards: tuple[str, ...] = Field(default_factory=tuple)
174
+ source_refs: tuple[str, ...] = Field(default_factory=tuple)
175
+ notes: str = ""
176
+
177
+ @field_validator("evaluator")
178
+ @classmethod
179
+ def _known_evaluator(cls, value: str | None) -> str | None:
180
+ if value is not None and value not in _ALLOWED_EVALUATORS:
181
+ raise ValueError(f"unknown invariance evaluator: {value}")
182
+ return value
183
+
184
+ @field_validator("languages")
185
+ @classmethod
186
+ def _known_languages(cls, value: tuple[str, ...]) -> tuple[str, ...]:
187
+ unknown = sorted(set(value) - _KNOWN_LANGUAGE_IDS)
188
+ if unknown:
189
+ raise ValueError(f"unknown invariance languages: {unknown}")
190
+ return value
191
+
192
+ @field_validator("languages", "node_types", "guards", "source_refs", mode="before")
193
+ @classmethod
194
+ def _tuple_from_list(cls, value: Any) -> tuple[str, ...]:
195
+ if value is None:
196
+ return tuple()
197
+ return tuple(value)
198
+
199
+ @model_validator(mode="after")
200
+ def _status_consistency(self) -> InvarianceRuleDefinition:
201
+ if self.status == "implemented" and self.evaluator is None:
202
+ raise ValueError("implemented invariance rules require an evaluator")
203
+ if self.status == "catalog" and self.enabled:
204
+ raise ValueError("catalog invariance rules must be disabled")
205
+ return self
206
+
207
+
208
+ class InvarianceRuleSet(BaseModel, frozen=True):
209
+ """Top-level packaged rule file shape."""
210
+
211
+ version: int = Field(ge=1)
212
+ rules: tuple[InvarianceRuleDefinition, ...] = Field(default_factory=tuple)
213
+
214
+ @field_validator("rules", mode="before")
215
+ @classmethod
216
+ def _tuple_rules(cls, value: Any) -> tuple[Any, ...]:
217
+ return tuple(value or ())
218
+
219
+ @model_validator(mode="after")
220
+ def _unique_rule_ids(self) -> InvarianceRuleSet:
221
+ seen: set[str] = set()
222
+ duplicates: set[str] = set()
223
+ for rule in self.rules:
224
+ if rule.id in seen:
225
+ duplicates.add(rule.id)
226
+ seen.add(rule.id)
227
+ if duplicates:
228
+ raise ValueError(f"duplicate invariance rule ids: {sorted(duplicates)}")
229
+ return self
230
+
231
+
232
+ @dataclass(frozen=True)
233
+ class InvarianceResult:
234
+ changes: list[Change]
235
+ change_groups: list[ChangeGroup] = field(default_factory=list)
236
+ ignored_style_changes: list[dict[str, Any]] = field(default_factory=list)
237
+
238
+
239
+ @dataclass(frozen=True)
240
+ class _ColorEvidence:
241
+ old_label: str
242
+ new_label: str
243
+ canonical: str
244
+ old_span: tuple[int, int]
245
+ new_span: tuple[int, int]
246
+
247
+
248
+ @dataclass(frozen=True)
249
+ class _SourceEvidence:
250
+ old_label: str
251
+ new_label: str
252
+ old_span: tuple[int, int]
253
+ new_span: tuple[int, int]
254
+
255
+
256
+ @dataclass(frozen=True)
257
+ class _SourceLiteralEvidence(_SourceEvidence):
258
+ canonical: str
259
+
260
+
261
+ @lru_cache(maxsize=1)
262
+ def load_builtin_invariance_rules() -> tuple[InvarianceRuleDefinition, ...]:
263
+ """Load and validate packaged built-in invariance rules."""
264
+
265
+ rule_path = resources.files("intentumdiff.invariances").joinpath("rules.yaml")
266
+ raw = yaml.safe_load(rule_path.read_text(encoding="utf-8")) or {}
267
+ return InvarianceRuleSet.model_validate(raw).rules
268
+
269
+
270
+ def builtin_invariance_rule(rule_id: str) -> InvarianceRuleDefinition | None:
271
+ """Return a packaged invariance rule by id, if present."""
272
+
273
+ for rule in load_builtin_invariance_rules():
274
+ if rule.id == rule_id:
275
+ return rule
276
+ return None
277
+
278
+
279
+ def build_style_only_evidence(
280
+ *,
281
+ old_source: str,
282
+ new_source: str,
283
+ language: str,
284
+ old_cst_json: str | None = None,
285
+ new_cst_json: str | None = None,
286
+ rules: tuple[InvarianceRuleDefinition, ...] | None = None,
287
+ ) -> InvarianceResult:
288
+ """Build source-span evidence for the fast style-only shortcut."""
289
+
290
+ if old_source == new_source:
291
+ return InvarianceResult(changes=[])
292
+
293
+ active_rules = rules if rules is not None else load_builtin_invariance_rules()
294
+ groups: list[ChangeGroup] = []
295
+ ignored: list[dict[str, Any]] = []
296
+
297
+ string_rule = _active_rule(
298
+ active_rules,
299
+ language=language,
300
+ evaluator="string_literal_equivalence",
301
+ )
302
+ literal_spans: list[tuple[tuple[int, int], tuple[int, int]]] = []
303
+ if string_rule is not None:
304
+ literal_groups, literal_ignored = _source_string_literal_groups(
305
+ old_source=old_source,
306
+ new_source=new_source,
307
+ language=language,
308
+ rule=string_rule,
309
+ index_space="style_only_shortcut",
310
+ old_cst_json=old_cst_json,
311
+ new_cst_json=new_cst_json,
312
+ )
313
+ groups.extend(literal_groups)
314
+ ignored.extend(literal_ignored)
315
+ literal_spans.extend(
316
+ (
317
+ tuple(group.metadata["old_span"]), # type: ignore[arg-type]
318
+ tuple(group.metadata["new_span"]), # type: ignore[arg-type]
319
+ )
320
+ for group in literal_groups
321
+ )
322
+
323
+ shortcut_rule = _active_rule(
324
+ active_rules,
325
+ language=language,
326
+ evaluator="style_only_shortcut_evidence",
327
+ )
328
+ if shortcut_rule is not None:
329
+ generic_evidence = [
330
+ evidence
331
+ for evidence in _changed_source_evidence(old_source, new_source)
332
+ if not _covered_by_literal_evidence(evidence, literal_spans)
333
+ ]
334
+ for occurrence, evidence in enumerate(generic_evidence):
335
+ group, item = _source_group(
336
+ evidence,
337
+ rule=shortcut_rule,
338
+ language=language,
339
+ occurrence=occurrence,
340
+ index_space="style_only_shortcut",
341
+ confidence=0.75,
342
+ )
343
+ groups.append(group)
344
+ ignored.append(item)
345
+
346
+ return InvarianceResult(
347
+ changes=[],
348
+ change_groups=groups,
349
+ ignored_style_changes=ignored,
350
+ )
351
+
352
+
353
+ def build_zero_change_literal_evidence(
354
+ *,
355
+ old_tree: SemanticNode,
356
+ new_tree: SemanticNode,
357
+ old_source: str,
358
+ new_source: str,
359
+ language: str,
360
+ rules: tuple[InvarianceRuleDefinition, ...] | None = None,
361
+ ) -> InvarianceResult:
362
+ """Build literal equivalence evidence when refinement already removed all changes."""
363
+
364
+ if old_source == new_source:
365
+ return InvarianceResult(changes=[])
366
+
367
+ active_rules = rules if rules is not None else load_builtin_invariance_rules()
368
+ string_rule = _active_rule(
369
+ active_rules,
370
+ language=language,
371
+ evaluator="string_literal_equivalence",
372
+ )
373
+ if string_rule is None:
374
+ return InvarianceResult(changes=[])
375
+
376
+ groups, ignored = _source_string_literal_groups(
377
+ old_source=old_source,
378
+ new_source=new_source,
379
+ language=language,
380
+ rule=string_rule,
381
+ index_space="zero_change_source_equivalence",
382
+ old_tree=old_tree,
383
+ new_tree=new_tree,
384
+ )
385
+ return InvarianceResult(changes=[], change_groups=groups, ignored_style_changes=ignored)
386
+
387
+
388
+ def _active_rule(
389
+ rules: tuple[InvarianceRuleDefinition, ...],
390
+ *,
391
+ language: str,
392
+ evaluator: str,
393
+ ) -> InvarianceRuleDefinition | None:
394
+ for rule in rules:
395
+ if (
396
+ rule.status == "implemented"
397
+ and rule.enabled
398
+ and rule.evaluator == evaluator
399
+ and language in rule.languages
400
+ ):
401
+ return rule
402
+ return None
403
+
404
+
405
+ def apply_invariances(
406
+ changes: list[Change],
407
+ *,
408
+ old_tree: SemanticNode,
409
+ new_tree: SemanticNode,
410
+ old_source: str,
411
+ new_source: str,
412
+ language: str,
413
+ rules: tuple[InvarianceRuleDefinition, ...] | None = None,
414
+ ) -> InvarianceResult:
415
+ """Remove review-level changes that are proven equivalent by invariance rules."""
416
+
417
+ active_rules = rules if rules is not None else load_builtin_invariance_rules()
418
+ current = list(changes)
419
+ groups: list[ChangeGroup] = []
420
+ ignored: list[dict[str, Any]] = []
421
+
422
+ for rule in active_rules:
423
+ if rule.status != "implemented" or not rule.enabled or language not in rule.languages:
424
+ continue
425
+ if rule.evaluator == "css_color_equivalence":
426
+ current, rule_groups, rule_ignored = _apply_css_color_equivalence(
427
+ current,
428
+ old_tree=old_tree,
429
+ new_tree=new_tree,
430
+ old_source=old_source,
431
+ new_source=new_source,
432
+ rule=rule,
433
+ )
434
+ groups.extend(rule_groups)
435
+ ignored.extend(rule_ignored)
436
+ elif rule.evaluator == "integer_literal_equivalence":
437
+ current, rule_groups, rule_ignored = _apply_literal_equivalence(
438
+ current,
439
+ old_source=old_source,
440
+ new_source=new_source,
441
+ language=language,
442
+ rule=rule,
443
+ literal_kind="integer",
444
+ )
445
+ groups.extend(rule_groups)
446
+ ignored.extend(rule_ignored)
447
+ elif rule.evaluator == "string_literal_equivalence":
448
+ current, rule_groups, rule_ignored = _apply_literal_equivalence(
449
+ current,
450
+ old_source=old_source,
451
+ new_source=new_source,
452
+ language=language,
453
+ rule=rule,
454
+ literal_kind="string",
455
+ )
456
+ groups.extend(rule_groups)
457
+ ignored.extend(rule_ignored)
458
+ elif rule.evaluator == "number_literal_equivalence":
459
+ current, rule_groups, rule_ignored = _apply_number_literal_equivalence(
460
+ current,
461
+ old_source=old_source,
462
+ new_source=new_source,
463
+ language=language,
464
+ rule=rule,
465
+ )
466
+ groups.extend(rule_groups)
467
+ ignored.extend(rule_ignored)
468
+ elif rule.evaluator == "parser_normalization":
469
+ pass
470
+
471
+ return InvarianceResult(
472
+ changes=current,
473
+ change_groups=groups,
474
+ ignored_style_changes=ignored,
475
+ )
476
+
477
+
478
+ def _source_string_literal_groups(
479
+ *,
480
+ old_source: str,
481
+ new_source: str,
482
+ language: str,
483
+ rule: InvarianceRuleDefinition,
484
+ index_space: str,
485
+ old_tree: SemanticNode | None = None,
486
+ new_tree: SemanticNode | None = None,
487
+ old_cst_json: str | None = None,
488
+ new_cst_json: str | None = None,
489
+ ) -> tuple[list[ChangeGroup], list[dict[str, Any]]]:
490
+ evidence = _source_string_literal_evidence(
491
+ old_source=old_source,
492
+ new_source=new_source,
493
+ language=language,
494
+ old_tree=old_tree,
495
+ new_tree=new_tree,
496
+ old_cst_json=old_cst_json,
497
+ new_cst_json=new_cst_json,
498
+ )
499
+ groups: list[ChangeGroup] = []
500
+ ignored: list[dict[str, Any]] = []
501
+ for occurrence, item in enumerate(evidence):
502
+ group, ignored_item = _source_group(
503
+ item,
504
+ rule=rule,
505
+ language=language,
506
+ occurrence=occurrence,
507
+ index_space=index_space,
508
+ confidence=0.95,
509
+ canonical=item.canonical,
510
+ )
511
+ groups.append(group)
512
+ ignored.append(ignored_item)
513
+ return groups, ignored
514
+
515
+
516
+ def _source_string_literal_evidence(
517
+ *,
518
+ old_source: str,
519
+ new_source: str,
520
+ language: str,
521
+ old_tree: SemanticNode | None = None,
522
+ new_tree: SemanticNode | None = None,
523
+ old_cst_json: str | None = None,
524
+ new_cst_json: str | None = None,
525
+ ) -> list[_SourceLiteralEvidence]:
526
+ old_spans = _string_literal_spans(
527
+ old_source,
528
+ tree=old_tree,
529
+ cst_json=old_cst_json,
530
+ )
531
+ new_spans = _string_literal_spans(
532
+ new_source,
533
+ tree=new_tree,
534
+ cst_json=new_cst_json,
535
+ )
536
+ if not old_spans or len(old_spans) != len(new_spans):
537
+ return []
538
+
539
+ result: list[_SourceLiteralEvidence] = []
540
+ for old_text, old_span, new_text, new_span in zip(
541
+ [item[0] for item in old_spans],
542
+ [item[1] for item in old_spans],
543
+ [item[0] for item in new_spans],
544
+ [item[1] for item in new_spans],
545
+ strict=True,
546
+ ):
547
+ if old_text == new_text:
548
+ continue
549
+ old_value = _canonical_string_literal(old_text, language)
550
+ new_value = _canonical_string_literal(new_text, language)
551
+ if old_value is None or new_value is None or old_value != new_value:
552
+ continue
553
+ result.append(
554
+ _SourceLiteralEvidence(
555
+ old_label=old_text,
556
+ new_label=new_text,
557
+ old_span=old_span,
558
+ new_span=new_span,
559
+ canonical=f"string({old_value})",
560
+ )
561
+ )
562
+ return result
563
+
564
+
565
+ def _string_literal_spans(
566
+ source: str,
567
+ *,
568
+ tree: SemanticNode | None = None,
569
+ cst_json: str | None = None,
570
+ ) -> list[tuple[str, tuple[int, int]]]:
571
+ if tree is not None:
572
+ line_offsets = _line_offsets(source)
573
+ spans: list[tuple[str, tuple[int, int]]] = []
574
+ for node in _all_semantic_nodes(tree):
575
+ if not _is_string_node(node):
576
+ continue
577
+ start = _offset_from_line_col(
578
+ line_offsets,
579
+ node.position.start_line,
580
+ node.position.start_col,
581
+ )
582
+ end = _offset_from_line_col(line_offsets, node.position.end_line, node.position.end_col)
583
+ if start is None or end is None or start >= end:
584
+ continue
585
+ spans.append((source[start:end], (start, end)))
586
+ return sorted(set(spans), key=lambda item: item[1])
587
+
588
+ if cst_json is None:
589
+ return []
590
+ try:
591
+ root = json.loads(cst_json)
592
+ except (TypeError, ValueError):
593
+ return []
594
+ line_offsets = _line_offsets(source)
595
+ spans = []
596
+ for node in _walk_cst(root):
597
+ if not _is_cst_string_container_type(str(node.get("type", ""))):
598
+ continue
599
+ start = _offset_from_line_col(
600
+ line_offsets,
601
+ int(node.get("start_line", -1)),
602
+ int(node.get("start_col", -1)),
603
+ )
604
+ end = _offset_from_line_col(
605
+ line_offsets,
606
+ int(node.get("end_line", -1)),
607
+ int(node.get("end_col", -1)),
608
+ )
609
+ if start is None or end is None or start >= end:
610
+ continue
611
+ spans.append((source[start:end], (start, end)))
612
+ return sorted(set(spans), key=lambda item: item[1])
613
+
614
+
615
+ def _all_semantic_nodes(root: SemanticNode) -> list[SemanticNode]:
616
+ return [root, *root.descendants()]
617
+
618
+
619
+ def _walk_cst(node: dict[str, Any]) -> list[dict[str, Any]]:
620
+ nodes = [node]
621
+ for child in node.get("children", ()) or ():
622
+ if isinstance(child, dict):
623
+ nodes.extend(_walk_cst(child))
624
+ return nodes
625
+
626
+
627
+ def _line_offsets(source: str) -> list[int]:
628
+ offsets = [0]
629
+ for match in re.finditer(r"\n", source):
630
+ offsets.append(match.end())
631
+ return offsets
632
+
633
+
634
+ def _offset_from_line_col(offsets: list[int], line: int, col: int) -> int | None:
635
+ if line < 0 or col < 0 or line >= len(offsets):
636
+ return None
637
+ return offsets[line] + col
638
+
639
+
640
+ def _changed_source_evidence(old_source: str, new_source: str) -> list[_SourceEvidence]:
641
+ result: list[_SourceEvidence] = []
642
+ matcher = SequenceMatcher(None, old_source, new_source, autojunk=False)
643
+ for tag, old_start, old_end, new_start, new_end in matcher.get_opcodes():
644
+ if tag == "equal":
645
+ continue
646
+ result.append(
647
+ _SourceEvidence(
648
+ old_label=_source_label(old_source[old_start:old_end]),
649
+ new_label=_source_label(new_source[new_start:new_end]),
650
+ old_span=(old_start, old_end),
651
+ new_span=(new_start, new_end),
652
+ )
653
+ )
654
+ return result
655
+
656
+
657
+ def _source_label(value: str) -> str:
658
+ if value == "":
659
+ return "<empty>"
660
+ if value.strip() == "":
661
+ return "<whitespace>"
662
+ label = value.replace("\r", "\\r").replace("\n", "\\n").strip()
663
+ return label if len(label) <= 80 else f"{label[:77]}..."
664
+
665
+
666
+ def _covered_by_literal_evidence(
667
+ evidence: _SourceEvidence,
668
+ literal_spans: list[tuple[tuple[int, int], tuple[int, int]]],
669
+ ) -> bool:
670
+ for old_span, new_span in literal_spans:
671
+ if _span_within(evidence.old_span, old_span) and _span_within(
672
+ evidence.new_span,
673
+ new_span,
674
+ ):
675
+ return True
676
+ return False
677
+
678
+
679
+ def _span_within(inner: tuple[int, int], outer: tuple[int, int]) -> bool:
680
+ return outer[0] <= inner[0] <= inner[1] <= outer[1]
681
+
682
+
683
+ def _source_group(
684
+ evidence: _SourceEvidence,
685
+ *,
686
+ rule: InvarianceRuleDefinition,
687
+ language: str,
688
+ occurrence: int,
689
+ index_space: str,
690
+ confidence: float,
691
+ canonical: str | None = None,
692
+ ) -> tuple[ChangeGroup, dict[str, Any]]:
693
+ metadata: dict[str, Any] = {
694
+ "index_space": index_space,
695
+ "reason": rule.explanation,
696
+ "equivalence_kind": rule.equivalence_kind,
697
+ "old_label": evidence.old_label,
698
+ "new_label": evidence.new_label,
699
+ "risk": rule.risk,
700
+ "language": language,
701
+ "occurrence": occurrence,
702
+ "evidence_depth": "source_span",
703
+ "old_span": list(evidence.old_span),
704
+ "new_span": list(evidence.new_span),
705
+ }
706
+ if canonical is not None:
707
+ metadata["canonical_old"] = canonical
708
+ metadata["canonical_new"] = canonical
709
+
710
+ group = ChangeGroup(
711
+ kind=rule.group_kind,
712
+ raw_change_indices=[],
713
+ old_labels=[evidence.old_label],
714
+ new_labels=[evidence.new_label],
715
+ old_node_ids=[],
716
+ new_node_ids=[],
717
+ confidence=confidence,
718
+ rule_id=rule.id,
719
+ metadata=metadata,
720
+ )
721
+ return group, {"rule_id": rule.id, **metadata}
722
+
723
+
724
+ def _apply_css_color_equivalence(
725
+ changes: list[Change],
726
+ *,
727
+ old_tree: SemanticNode,
728
+ new_tree: SemanticNode,
729
+ old_source: str,
730
+ new_source: str,
731
+ rule: InvarianceRuleDefinition,
732
+ ) -> tuple[list[Change], list[ChangeGroup], list[dict[str, Any]]]:
733
+ if not changes:
734
+ return changes, [], []
735
+
736
+ old_normalized, old_tokens = _canonicalize_css_colors(old_source)
737
+ new_normalized, new_tokens = _canonicalize_css_colors(new_source)
738
+ if old_normalized != new_normalized:
739
+ return changes, [], []
740
+
741
+ evidence = _changed_color_evidence(old_tokens, new_tokens)
742
+ if not evidence:
743
+ return changes, [], []
744
+
745
+ indices = list(range(len(changes)))
746
+ old_labels = _labels_from_changes(changes, old=True)
747
+ new_labels = _labels_from_changes(changes, old=False)
748
+ old_node_ids = _node_ids_from_changes(changes, old=True) or _node_ids(old_tree)
749
+ new_node_ids = _node_ids_from_changes(changes, old=False) or _node_ids(new_tree)
750
+
751
+ groups: list[ChangeGroup] = []
752
+ ignored: list[dict[str, Any]] = []
753
+ for occurrence, item in enumerate(evidence):
754
+ metadata = {
755
+ "index_space": "invariance_input",
756
+ "reason": rule.explanation,
757
+ "equivalence_kind": rule.equivalence_kind,
758
+ "canonical_old": item.canonical,
759
+ "canonical_new": item.canonical,
760
+ "old_label": item.old_label,
761
+ "new_label": item.new_label,
762
+ "risk": rule.risk,
763
+ "language": "css",
764
+ "occurrence": occurrence,
765
+ "old_span": list(item.old_span),
766
+ "new_span": list(item.new_span),
767
+ }
768
+ groups.append(
769
+ ChangeGroup(
770
+ kind=rule.group_kind,
771
+ raw_change_indices=indices,
772
+ old_labels=list(dict.fromkeys([*old_labels, item.old_label])),
773
+ new_labels=list(dict.fromkeys([*new_labels, item.new_label])),
774
+ old_node_ids=old_node_ids,
775
+ new_node_ids=new_node_ids,
776
+ confidence=1.0,
777
+ rule_id=rule.id,
778
+ metadata=metadata,
779
+ )
780
+ )
781
+ ignored.append({"rule_id": rule.id, **metadata})
782
+
783
+ return [], groups, ignored
784
+
785
+
786
+ def _apply_literal_equivalence(
787
+ changes: list[Change],
788
+ *,
789
+ old_source: str,
790
+ new_source: str,
791
+ language: str,
792
+ rule: InvarianceRuleDefinition,
793
+ literal_kind: Literal["integer", "string"],
794
+ ) -> tuple[list[Change], list[ChangeGroup], list[dict[str, Any]]]:
795
+ if not changes:
796
+ return changes, [], []
797
+
798
+ kept: list[Change] = []
799
+ groups: list[ChangeGroup] = []
800
+ ignored: list[dict[str, Any]] = []
801
+
802
+ for idx, change in enumerate(changes):
803
+ evidence = _literal_equivalence_evidence(
804
+ change,
805
+ old_source=old_source,
806
+ new_source=new_source,
807
+ language=language,
808
+ literal_kind=literal_kind,
809
+ )
810
+ if evidence is None:
811
+ kept.append(change)
812
+ continue
813
+
814
+ old_node, new_node, old_label, new_label, canonical = evidence
815
+ metadata = {
816
+ "index_space": "invariance_input",
817
+ "reason": rule.explanation,
818
+ "equivalence_kind": rule.equivalence_kind,
819
+ "canonical_old": canonical,
820
+ "canonical_new": canonical,
821
+ "old_label": old_label,
822
+ "new_label": new_label,
823
+ "old_node_type": old_node.node_type,
824
+ "new_node_type": new_node.node_type,
825
+ "risk": rule.risk,
826
+ "language": language,
827
+ }
828
+ groups.append(
829
+ ChangeGroup(
830
+ kind=rule.group_kind,
831
+ raw_change_indices=[idx],
832
+ old_labels=[old_label],
833
+ new_labels=[new_label],
834
+ old_node_ids=[old_node.id],
835
+ new_node_ids=[new_node.id],
836
+ confidence=1.0 if literal_kind == "integer" else 0.95,
837
+ rule_id=rule.id,
838
+ metadata=metadata,
839
+ )
840
+ )
841
+ ignored.append({"rule_id": rule.id, **metadata})
842
+
843
+ return kept, groups, ignored
844
+
845
+
846
+ def _literal_equivalence_evidence(
847
+ change: Change,
848
+ *,
849
+ old_source: str,
850
+ new_source: str,
851
+ language: str,
852
+ literal_kind: Literal["integer", "string"],
853
+ ) -> tuple[SemanticNode, SemanticNode, str, str, str] | None:
854
+ if change.change_type != ChangeType.MODIFICATION:
855
+ return None
856
+ if change.old_node is None or change.new_node is None:
857
+ return None
858
+ old_node = change.old_node
859
+ new_node = change.new_node
860
+ if not (old_node.is_leaf() and new_node.is_leaf()):
861
+ return None
862
+
863
+ old_text = _node_source_text(old_source, old_node).strip()
864
+ new_text = _node_source_text(new_source, new_node).strip()
865
+ old_label = old_text or old_node.label
866
+ new_label = new_text or new_node.label
867
+ if old_label == new_label:
868
+ return None
869
+
870
+ if literal_kind == "integer":
871
+ if not (_is_integer_node(old_node) and _is_integer_node(new_node)):
872
+ return None
873
+ old_value = _canonical_integer_literal(old_label)
874
+ new_value = _canonical_integer_literal(new_label)
875
+ if old_value is None or new_value is None or old_value != new_value:
876
+ return None
877
+ return old_node, new_node, old_label, new_label, f"int({old_value})"
878
+
879
+ if not (_is_string_node(old_node) and _is_string_node(new_node)):
880
+ return None
881
+ old_value = _canonical_string_literal(old_label, language)
882
+ new_value = _canonical_string_literal(new_label, language)
883
+ if old_value is None or new_value is None or old_value != new_value:
884
+ return None
885
+ return old_node, new_node, old_label, new_label, f"string({old_value})"
886
+
887
+
888
+ def _node_source_text(source: str, node: SemanticNode) -> str:
889
+ lines = source.splitlines()
890
+ pos = node.position
891
+ if pos.start_line < 0 or pos.start_line >= len(lines):
892
+ return ""
893
+ if pos.end_line < pos.start_line:
894
+ return ""
895
+ if pos.start_line == pos.end_line:
896
+ return lines[pos.start_line][pos.start_col:pos.end_col]
897
+
898
+ selected = [lines[pos.start_line][pos.start_col:]]
899
+ for line_no in range(pos.start_line + 1, min(pos.end_line, len(lines))):
900
+ selected.append(lines[line_no])
901
+ if pos.end_line < len(lines):
902
+ selected.append(lines[pos.end_line][:pos.end_col])
903
+ return "\n".join(selected)
904
+
905
+
906
+ def _is_integer_node(node: SemanticNode) -> bool:
907
+ node_type = node.node_type.lower()
908
+ return (
909
+ "integer" in node_type
910
+ or node_type in {"int_literal", "numeric_literal", "number"}
911
+ )
912
+
913
+
914
+ def _is_string_node(node: SemanticNode) -> bool:
915
+ return _is_string_node_type(node.node_type)
916
+
917
+
918
+ def _is_string_node_type(node_type: str) -> bool:
919
+ node_type = node_type.lower()
920
+ return "string" in node_type or node_type in {"character_literal", "char_literal"}
921
+
922
+
923
+ def _is_cst_string_container_type(node_type: str) -> bool:
924
+ node_type = node_type.lower()
925
+ if node_type in {"string_start", "string_end", "string_content"}:
926
+ return False
927
+ return _is_string_node_type(node_type)
928
+
929
+
930
+ _INTEGER_LITERAL_RE = re.compile(
931
+ r"""
932
+ ^[+-]?
933
+ (?:
934
+ 0[xX][0-9a-fA-F_]+
935
+ | 0[oO][0-7_]+
936
+ | 0[bB][01_]+
937
+ | [0-9][0-9_]*
938
+ )$
939
+ """,
940
+ re.VERBOSE,
941
+ )
942
+
943
+
944
+ def _canonical_integer_literal(value: str) -> int | None:
945
+ stripped = value.strip()
946
+ if not _INTEGER_LITERAL_RE.fullmatch(stripped):
947
+ return None
948
+ try:
949
+ return int(stripped.replace("_", ""), 0)
950
+ except ValueError:
951
+ return None
952
+
953
+
954
+ def _apply_number_literal_equivalence(
955
+ changes: list[Change],
956
+ *,
957
+ old_source: str,
958
+ new_source: str,
959
+ language: str,
960
+ rule: InvarianceRuleDefinition,
961
+ ) -> tuple[list[Change], list[ChangeGroup], list[dict[str, Any]]]:
962
+ """Suppress changes where both number spellings parse to the same IEEE-754 double."""
963
+ if not changes:
964
+ return changes, [], []
965
+
966
+ kept: list[Change] = []
967
+ groups: list[ChangeGroup] = []
968
+ ignored: list[dict[str, Any]] = []
969
+
970
+ for idx, change in enumerate(changes):
971
+ if change.change_type != ChangeType.MODIFICATION:
972
+ kept.append(change)
973
+ continue
974
+ if change.old_node is None or change.new_node is None:
975
+ kept.append(change)
976
+ continue
977
+ old_node = change.old_node
978
+ new_node = change.new_node
979
+ if not (old_node.is_leaf() and new_node.is_leaf()):
980
+ kept.append(change)
981
+ continue
982
+
983
+ old_text = _node_source_text(old_source, old_node).strip()
984
+ new_text = _node_source_text(new_source, new_node).strip()
985
+ old_label = old_text or old_node.label
986
+ new_label = new_text or new_node.label
987
+ if old_label == new_label:
988
+ kept.append(change)
989
+ continue
990
+
991
+ old_bits = _canonical_number_bits(old_label)
992
+ new_bits = _canonical_number_bits(new_label)
993
+ if old_bits is None or new_bits is None or old_bits != new_bits:
994
+ kept.append(change)
995
+ continue
996
+
997
+ canonical = f"f64({old_label})"
998
+ metadata = {
999
+ "index_space": "invariance_input",
1000
+ "reason": rule.explanation,
1001
+ "equivalence_kind": rule.equivalence_kind,
1002
+ "canonical_old": canonical,
1003
+ "canonical_new": canonical,
1004
+ "old_label": old_label,
1005
+ "new_label": new_label,
1006
+ "old_node_type": old_node.node_type,
1007
+ "new_node_type": new_node.node_type,
1008
+ "risk": rule.risk,
1009
+ "language": language,
1010
+ }
1011
+ groups.append(
1012
+ ChangeGroup(
1013
+ kind=rule.group_kind,
1014
+ raw_change_indices=[idx],
1015
+ old_labels=[old_label],
1016
+ new_labels=[new_label],
1017
+ old_node_ids=[old_node.id],
1018
+ new_node_ids=[new_node.id],
1019
+ confidence=1.0,
1020
+ rule_id=rule.id,
1021
+ metadata=metadata,
1022
+ )
1023
+ )
1024
+ ignored.append({"rule_id": rule.id, **metadata})
1025
+
1026
+ return kept, groups, ignored
1027
+
1028
+
1029
+ def _canonical_number_bits(value: str) -> int | None:
1030
+ """Return the IEEE-754 bit pattern of a number literal, or None if unsafe.
1031
+
1032
+ Rejects NaN payloads, infinities, and signed-zero mismatches so that
1033
+ only exact bit-identical doubles are considered equivalent.
1034
+ """
1035
+ import struct
1036
+
1037
+ stripped = value.strip().replace("_", "")
1038
+ if not stripped or stripped in {"nan", "inf", "-inf", "infinity", "-infinity"}:
1039
+ return None
1040
+ try:
1041
+ f = float(stripped)
1042
+ except ValueError:
1043
+ return None
1044
+ # Reject NaN and infinities — their bit patterns are implementation-defined.
1045
+ if math.isnan(f) or math.isinf(f):
1046
+ return None
1047
+ bits = struct.unpack("<Q", struct.pack("<d", f))[0]
1048
+ # Treat +0.0 and -0.0 as distinct so sign changes are visible.
1049
+ return bits
1050
+
1051
+
1052
+ def _canonical_string_literal(value: str, language: str) -> str | None:
1053
+ stripped = value.strip()
1054
+ if not stripped:
1055
+ return None
1056
+ if "`" in stripped or "$" in stripped:
1057
+ return None
1058
+
1059
+ if language == "python":
1060
+ prefix = stripped[: max(0, stripped.find(stripped[-1]))].lower()
1061
+ if any(ch in prefix for ch in "fbr"):
1062
+ return None
1063
+ try:
1064
+ decoded = ast.literal_eval(stripped)
1065
+ except (SyntaxError, ValueError):
1066
+ return None
1067
+ return decoded if isinstance(decoded, str) else None
1068
+
1069
+ if language in {"javascript", "typescript", "tsx", "csharp"}:
1070
+ if stripped.startswith(("@", "$")):
1071
+ return None
1072
+ if len(stripped) < 2 or stripped[0] not in {"'", '"'} or stripped[-1] != stripped[0]:
1073
+ return None
1074
+ inner = stripped[1:-1]
1075
+ if stripped[0] == "'" and ("'" in inner or "\\" in inner):
1076
+ return None
1077
+ if stripped[0] == '"' and ('"' in inner):
1078
+ return None
1079
+ try:
1080
+ decoded = ast.literal_eval(stripped)
1081
+ except (SyntaxError, ValueError):
1082
+ if "\\" in inner:
1083
+ return None
1084
+ decoded = inner
1085
+ return decoded if isinstance(decoded, str) else None
1086
+
1087
+ return None
1088
+
1089
+
1090
+ def _canonicalize_css_colors(source: str) -> tuple[str, list[tuple[str, str, tuple[int, int]]]]:
1091
+ tokens: list[tuple[str, str, tuple[int, int]]] = []
1092
+ parts: list[str] = []
1093
+ last_end = 0
1094
+
1095
+ for match in _COLOR_TOKEN_RE.finditer(source):
1096
+ original = match.group(0)
1097
+ canonical = _canonical_css_color_match(match)
1098
+ if canonical is None:
1099
+ continue
1100
+ parts.append(source[last_end:match.start()])
1101
+ parts.append(canonical)
1102
+ tokens.append((original, canonical, (match.start(), match.end())))
1103
+ last_end = match.end()
1104
+
1105
+ parts.append(source[last_end:])
1106
+ return "".join(parts), tokens
1107
+
1108
+
1109
+ def _canonical_css_color_match(match: re.Match[str]) -> str | None:
1110
+ if match.group("hex"):
1111
+ return _canonical_hex_color(match.group("hex"))
1112
+ if match.group("rgb"):
1113
+ g = match.group("g_comma") or match.group("g_space")
1114
+ b = match.group("b_comma") or match.group("b_space")
1115
+ return _canonical_rgb_color(match.group("r"), g, b)
1116
+ name = match.group("name")
1117
+ if name is None:
1118
+ return None
1119
+ rgb = _CSS_NAMED_COLORS.get(name.lower())
1120
+ if rgb is None:
1121
+ return None
1122
+ return _format_srgb(*rgb)
1123
+
1124
+
1125
+ def _canonical_hex_color(value: str) -> str | None:
1126
+ raw = value[1:]
1127
+ if len(raw) == 3:
1128
+ raw = "".join(ch * 2 for ch in raw)
1129
+ if len(raw) != 6:
1130
+ return None
1131
+ try:
1132
+ red = int(raw[0:2], 16)
1133
+ green = int(raw[2:4], 16)
1134
+ blue = int(raw[4:6], 16)
1135
+ except ValueError:
1136
+ return None
1137
+ return _format_srgb(red, green, blue)
1138
+
1139
+
1140
+ def _canonical_rgb_color(red: str | None, green: str | None, blue: str | None) -> str | None:
1141
+ if red is None or green is None or blue is None:
1142
+ return None
1143
+ try:
1144
+ channels = (int(red), int(green), int(blue))
1145
+ except ValueError:
1146
+ return None
1147
+ if any(channel < 0 or channel > 255 for channel in channels):
1148
+ return None
1149
+ return _format_srgb(*channels)
1150
+
1151
+
1152
+ def _format_srgb(red: int, green: int, blue: int) -> str:
1153
+ return f"srgb({red},{green},{blue},1)"
1154
+
1155
+
1156
+ def _changed_color_evidence(
1157
+ old_tokens: list[tuple[str, str, tuple[int, int]]],
1158
+ new_tokens: list[tuple[str, str, tuple[int, int]]],
1159
+ ) -> list[_ColorEvidence]:
1160
+ if len(old_tokens) != len(new_tokens):
1161
+ return []
1162
+
1163
+ result: list[_ColorEvidence] = []
1164
+ for old, new in zip(old_tokens, new_tokens, strict=True):
1165
+ old_label, old_canonical, old_span = old
1166
+ new_label, new_canonical, new_span = new
1167
+ if old_canonical != new_canonical:
1168
+ return []
1169
+ if old_label != new_label:
1170
+ result.append(
1171
+ _ColorEvidence(
1172
+ old_label=old_label,
1173
+ new_label=new_label,
1174
+ canonical=old_canonical,
1175
+ old_span=old_span,
1176
+ new_span=new_span,
1177
+ )
1178
+ )
1179
+ return result
1180
+
1181
+
1182
+ def _labels_from_changes(changes: list[Change], *, old: bool) -> list[str]:
1183
+ labels: list[str] = []
1184
+ for change in changes:
1185
+ node = change.old_node if old else change.new_node
1186
+ labels.extend(_labels(node))
1187
+ return list(dict.fromkeys(labels))
1188
+
1189
+
1190
+ def _node_ids_from_changes(changes: list[Change], *, old: bool) -> list[str]:
1191
+ node_ids: list[str] = []
1192
+ for change in changes:
1193
+ node = change.old_node if old else change.new_node
1194
+ node_ids.extend(_node_ids(node))
1195
+ return list(dict.fromkeys(node_ids))
1196
+
1197
+
1198
+ def _labels(node: SemanticNode | None) -> list[str]:
1199
+ if node is None:
1200
+ return []
1201
+ return [item.label for item in [node, *node.descendants()] if item.label]
1202
+
1203
+
1204
+ def _node_ids(node: SemanticNode | None) -> list[str]:
1205
+ if node is None:
1206
+ return []
1207
+ return [item.id for item in [node, *node.descendants()]]