intentumdiff-python 0.0.1__py3-none-win_amd64.whl → 0.0.3b1__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 (150) hide show
  1. intentumdiff/__init__.py +9 -0
  2. intentumdiff/__main__.py +12 -0
  3. intentumdiff/_differ_gate.py +0 -136
  4. intentumdiff/_differ_presentation.py +17 -735
  5. intentumdiff/_differ_runtime.py +13 -64
  6. intentumdiff/analysis/compile_commands.py +26 -141
  7. intentumdiff/analysis/cross_file.py +23 -25
  8. intentumdiff/analysis/diagnostics.py +1 -164
  9. intentumdiff/analysis/guardrail_reports.py +31 -33
  10. intentumdiff/analysis/guardrails.py +111 -188
  11. intentumdiff/analysis/schema_resolver.py +72 -360
  12. intentumdiff/analysis/user_schemas.py +57 -254
  13. intentumdiff/api.py +21 -0
  14. intentumdiff/cache/sqlite_store.py +5 -23
  15. intentumdiff/cli/__init__.py +59 -0
  16. intentumdiff/cli/_commands.py +243 -137
  17. intentumdiff/cli/_parser.py +51 -56
  18. intentumdiff/cli/_shared.py +151 -116
  19. intentumdiff/content_type.py +13 -30
  20. intentumdiff/core/commit_differ.py +137 -286
  21. intentumdiff/core/diffignore.py +28 -34
  22. intentumdiff/core/index.py +16 -18
  23. intentumdiff/core/indexer.py +148 -169
  24. intentumdiff/core/models.py +279 -83
  25. intentumdiff/differ.py +366 -1000
  26. intentumdiff/github_app.py +40 -27
  27. intentumdiff/intentumdiff_rust_core/intentumdiff_rust_core.dll +0 -0
  28. intentumdiff/live_server.py +197 -34
  29. intentumdiff/lsp/client.py +49 -43
  30. intentumdiff/lsp/enricher.py +19 -119
  31. intentumdiff/lsp/exceptions.py +6 -7
  32. intentumdiff/lsp/servers.py +10 -11
  33. intentumdiff/lsp_server/_codelens.py +24 -57
  34. intentumdiff/lsp_server/_diagnostics.py +23 -51
  35. intentumdiff/lsp_server/_handlers.py +67 -9
  36. intentumdiff/lsp_server/server.py +28 -8
  37. intentumdiff/plugins/adapter.py +5 -0
  38. intentumdiff/plugins/builtins.py +7 -14
  39. intentumdiff/plugins/exceptions.py +28 -0
  40. intentumdiff/plugins/hub.py +39 -43
  41. intentumdiff/plugins/loader.py +229 -215
  42. intentumdiff/plugins/registry.py +259 -207
  43. intentumdiff/plugins/registry.schema.json +156 -15
  44. intentumdiff/plugins/registry_schema.py +47 -140
  45. intentumdiff/rust_core.py +623 -1326
  46. intentumdiff/serve/_app.py +26 -35
  47. intentumdiff/sources/base.py +24 -0
  48. intentumdiff/sources/file_source.py +15 -2
  49. intentumdiff/sources/git_source.py +64 -185
  50. intentumdiff/sources/live_buffer_source.py +45 -5
  51. intentumdiff/sources/patch_source.py +36 -109
  52. intentumdiff/vcs/_safe_path.py +7 -0
  53. intentumdiff/vcs/base.py +6 -0
  54. intentumdiff/vcs/git_backend.py +1 -1
  55. intentumdiff/vcs/git_cli.py +0 -27
  56. intentumdiff/vcs/hg_backend.py +1 -1
  57. intentumdiff/vcs/perforce_backend.py +7 -1
  58. intentumdiff/vcs/svn_backend.py +1 -1
  59. intentumdiff/wasm/abap_parser.wasm +0 -0
  60. intentumdiff/wasm/adf_parser.wasm +0 -0
  61. intentumdiff/wasm/asciidoc_parser.wasm +0 -0
  62. intentumdiff/wasm/asm_parser.wasm +0 -0
  63. intentumdiff/wasm/assemblyscript_parser.wasm +0 -0
  64. intentumdiff/wasm/astro_parser.wasm +0 -0
  65. intentumdiff/wasm/bash_parser.wasm +0 -0
  66. intentumdiff/wasm/clojure_parser.wasm +0 -0
  67. intentumdiff/wasm/cmake_parser.wasm +0 -0
  68. intentumdiff/wasm/cpp_parser.wasm +0 -0
  69. intentumdiff/wasm/csharp_parser.wasm +0 -0
  70. intentumdiff/wasm/css_parser.wasm +0 -0
  71. intentumdiff/wasm/dart_parser.wasm +0 -0
  72. intentumdiff/wasm/databricks_parser.wasm +0 -0
  73. intentumdiff/wasm/dax_parser.wasm +0 -0
  74. intentumdiff/wasm/delphi_parser.wasm +0 -0
  75. intentumdiff/wasm/dockerfile_parser.wasm +0 -0
  76. intentumdiff/wasm/elixir_parser.wasm +0 -0
  77. intentumdiff/wasm/freebasic_parser.wasm +0 -0
  78. intentumdiff/wasm/generic_parser.wasm +0 -0
  79. intentumdiff/wasm/gitignore_parser.wasm +0 -0
  80. intentumdiff/wasm/go_parser.wasm +0 -0
  81. intentumdiff/wasm/gomod_parser.wasm +0 -0
  82. intentumdiff/wasm/graphql_parser.wasm +0 -0
  83. intentumdiff/wasm/groovy_parser.wasm +0 -0
  84. intentumdiff/wasm/haskell_parser.wasm +0 -0
  85. intentumdiff/wasm/html_parser.wasm +0 -0
  86. intentumdiff/wasm/html_renderer.wasm +0 -0
  87. intentumdiff/wasm/ini_parser.wasm +0 -0
  88. intentumdiff/wasm/java_parser.wasm +0 -0
  89. intentumdiff/wasm/js_ts_parser.wasm +0 -0
  90. intentumdiff/wasm/json_parser.wasm +0 -0
  91. intentumdiff/wasm/kotlin_parser.wasm +0 -0
  92. intentumdiff/wasm/latex_parser.wasm +0 -0
  93. intentumdiff/wasm/llm_renderer.wasm +0 -0
  94. intentumdiff/wasm/lua_parser.wasm +0 -0
  95. intentumdiff/wasm/make_parser.wasm +0 -0
  96. intentumdiff/wasm/markdown_parser.wasm +0 -0
  97. intentumdiff/wasm/mdx_parser.wasm +0 -0
  98. intentumdiff/wasm/ocaml_parser.wasm +0 -0
  99. intentumdiff/wasm/odin_parser.wasm +0 -0
  100. intentumdiff/wasm/parser_manifest.json +81 -2
  101. intentumdiff/wasm/patch_renderer.wasm +0 -0
  102. intentumdiff/wasm/perl_parser.wasm +0 -0
  103. intentumdiff/wasm/php_parser.wasm +0 -0
  104. intentumdiff/wasm/plsql_parser.wasm +0 -0
  105. intentumdiff/wasm/po_parser.wasm +0 -0
  106. intentumdiff/wasm/postscript_parser.wasm +0 -0
  107. intentumdiff/wasm/powershell_parser.wasm +0 -0
  108. intentumdiff/wasm/proto_parser.wasm +0 -0
  109. intentumdiff/wasm/puppet_parser.wasm +0 -0
  110. intentumdiff/wasm/python_parser.wasm +0 -0
  111. intentumdiff/wasm/qsharp_parser.wasm +0 -0
  112. intentumdiff/wasm/r_parser.wasm +0 -0
  113. intentumdiff/wasm/reasonml_parser.wasm +0 -0
  114. intentumdiff/wasm/ruby_parser.wasm +0 -0
  115. intentumdiff/wasm/rust_parser.wasm +0 -0
  116. intentumdiff/wasm/sas_parser.wasm +0 -0
  117. intentumdiff/wasm/scala_parser.wasm +0 -0
  118. intentumdiff/wasm/scss_parser.wasm +0 -0
  119. intentumdiff/wasm/sql_parser.wasm +0 -0
  120. intentumdiff/wasm/squirrel_parser.wasm +0 -0
  121. intentumdiff/wasm/svelte_parser.wasm +0 -0
  122. intentumdiff/wasm/swift_parser.wasm +0 -0
  123. intentumdiff/wasm/terminal_renderer.wasm +0 -0
  124. intentumdiff/wasm/terraform_parser.wasm +0 -0
  125. intentumdiff/wasm/toml_parser.wasm +0 -0
  126. intentumdiff/wasm/tsql_parser.wasm +0 -0
  127. intentumdiff/wasm/vbnet_parser.wasm +0 -0
  128. intentumdiff/wasm/vue_parser.wasm +0 -0
  129. intentumdiff/wasm/wasm_provenance.json +884 -0
  130. intentumdiff/wasm/wat_parser.wasm +0 -0
  131. intentumdiff/wasm/xml_parser.wasm +0 -0
  132. intentumdiff/wasm/yaml_parser.wasm +0 -0
  133. intentumdiff/wasm/zig_parser.wasm +0 -0
  134. intentumdiff/watcher.py +15 -17
  135. intentumdiff_python-0.0.3b1.dist-info/METADATA +188 -0
  136. intentumdiff_python-0.0.3b1.dist-info/RECORD +164 -0
  137. {intentumdiff_python-0.0.1.dist-info → intentumdiff_python-0.0.3b1.dist-info}/WHEEL +1 -1
  138. {intentumdiff_python-0.0.1.dist-info → intentumdiff_python-0.0.3b1.dist-info}/sboms/intentumdiff-rust-core.cyclonedx.json +905 -379
  139. intentumdiff/analysis/invariances.py +0 -1207
  140. intentumdiff/analysis/keyed_profiles.py +0 -25
  141. intentumdiff/analysis/resource_profiles.py +0 -14
  142. intentumdiff/analysis/text_review.py +0 -46
  143. intentumdiff/core/cst_serializer.py +0 -101
  144. intentumdiff/invariances/__init__.py +0 -2
  145. intentumdiff/invariances/rules.schema.json +0 -283
  146. intentumdiff/invariances/rules.yaml +0 -349
  147. intentumdiff_python-0.0.1.dist-info/METADATA +0 -112
  148. intentumdiff_python-0.0.1.dist-info/RECORD +0 -165
  149. {intentumdiff_python-0.0.1.dist-info → intentumdiff_python-0.0.3b1.dist-info}/entry_points.txt +0 -0
  150. {intentumdiff_python-0.0.1.dist-info → intentumdiff_python-0.0.3b1.dist-info}/licenses/LICENSE +0 -0
@@ -3,26 +3,21 @@
3
3
  from __future__ import annotations
4
4
 
5
5
  import json
6
- from collections.abc import Iterable
7
6
  from dataclasses import dataclass
8
7
  from pathlib import Path
9
- from typing import Any
10
8
 
11
- import yaml
12
9
 
13
10
  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
11
  from intentumdiff.core.models import (
17
12
  DiffConfig,
18
13
  GuardrailSeverity,
19
- GuardrailViolation,
20
14
  SemanticDiff,
21
15
  SemanticNode,
22
16
  )
23
17
 
24
18
  GUARDRAIL_POLICY_FILENAME = "intentumdiff.yaml"
25
- GUARDRAIL_CONFIG_LANGUAGES = KEYED_DATA_LANGUAGES | RESOURCE_PROFILE_LANGUAGES
19
+ # Which languages a protected rule may target is Rust policy: core's
20
+ # parse_guardrail_policy rejects unsupported languages (GUARDRAIL_RULE_LANGUAGES).
26
21
 
27
22
 
28
23
  @dataclass(frozen=True)
@@ -50,15 +45,18 @@ def guardrails_may_apply(
50
45
 
51
46
  if not config.guardrails_enabled:
52
47
  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
48
+ # Both sides of a move can have a policy. Checking only the destination
49
+ # silently releases protection on moves out of a guarded directory (#262).
50
+ # An explicit policy is mandatory even before a certified Rust batch return
51
+ # (#261); all YAML semantics and rule evaluation remain Rust-owned.
52
+ policies = _load_relevant_guardrail_policies(
53
+ filename, new_filename, config.guardrail_policy_path
54
+ )
55
+ return (
56
+ _is_policy_file(filename)
57
+ or _is_policy_file(new_filename)
58
+ or any(policy.rules for policy in policies)
59
+ )
62
60
 
63
61
 
64
62
  def load_guardrail_policy(
@@ -68,24 +66,59 @@ def load_guardrail_policy(
68
66
  ) -> GuardrailPolicy:
69
67
  policy_path = _find_policy_path(filename, explicit_path)
70
68
  if policy_path is None:
69
+ # An EXPLICIT --policy is the caller asserting that file exists. Returning an empty
70
+ # policy here is a fail-OPEN: the check then reports "Guardrail check passed" and
71
+ # exits 0 with nothing on stderr, so a typo in the path is indistinguishable from a
72
+ # clean run. For a gate whose documented purpose is stopping API keys changing
73
+ # unreviewed, that is the worst possible default.
74
+ #
75
+ # Auto-discovery finding nothing is different and stays permissive: the user never
76
+ # claimed a policy existed.
77
+ if explicit_path is not None:
78
+ raise FileNotFoundError(
79
+ f"Guardrail policy not found: {explicit_path}. "
80
+ "The check would otherwise pass with no rules loaded, which looks identical "
81
+ "to a clean run."
82
+ )
71
83
  return GuardrailPolicy(path=None, rules=())
72
84
 
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")
85
+ from intentumdiff.rust_core import _c_abi_call
76
86
 
77
- guardrails = raw.get("guardrails", raw)
78
- if not isinstance(guardrails, dict):
79
- raise ValueError(f"{policy_path} guardrails section must be a mapping")
87
+ try:
88
+ rules = _c_abi_call("parse_guardrail_policy", policy_path.read_text(encoding="utf-8"))
89
+ except ValueError as exc:
90
+ raise ValueError(f"{policy_path}: {exc}") from exc
91
+ return GuardrailPolicy(path=policy_path, rules=tuple(
92
+ ProtectedRule(rule_id=rule["rule_id"], severity=GuardrailSeverity(rule["severity"]),
93
+ language=rule["language"], path=rule["path"], message=rule["message"],
94
+ files=tuple(rule["files"])) for rule in rules))
80
95
 
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
96
 
87
- rules = tuple(_parse_rule(item, index, policy_path) for index, item in enumerate(protected))
88
- return GuardrailPolicy(path=policy_path, rules=rules)
97
+ def _load_relevant_guardrail_policies(
98
+ old_filename: str,
99
+ new_filename: str | None,
100
+ explicit_path: Path | None,
101
+ ) -> tuple[GuardrailPolicy, ...]:
102
+ """Discover distinct host policy files for both sides; Rust owns their meaning."""
103
+ if explicit_path is not None:
104
+ # An explicit policy overrides auto-discovery and is required to exist.
105
+ # load_guardrail_policy raises instead of manufacturing no rules (#261).
106
+ return (load_guardrail_policy(new_filename or old_filename,
107
+ explicit_path=explicit_path),)
108
+
109
+ found: list[GuardrailPolicy] = []
110
+ seen_paths: set[Path] = set()
111
+ for filename in (old_filename, new_filename):
112
+ if not filename:
113
+ continue
114
+ policy_path = _find_policy_path(filename, None)
115
+ if policy_path is None or policy_path in seen_paths:
116
+ continue
117
+ seen_paths.add(policy_path)
118
+ # Malformed, dangling or unreadable policies must fail closed even
119
+ # when the other side of the move contains a valid policy.
120
+ found.append(load_guardrail_policy(filename, explicit_path=policy_path))
121
+ return tuple(found)
89
122
 
90
123
 
91
124
  def apply_guardrails_to_diff(
@@ -97,36 +130,39 @@ def apply_guardrails_to_diff(
97
130
  new_source: str,
98
131
  config: DiffConfig,
99
132
  diagnostics: DiagnosticsRecorder | None = None,
133
+ policy_filename: str | None = None,
134
+ policy_old_filename: str | None = None,
100
135
  ) -> SemanticDiff:
101
136
  if not config.guardrails_enabled:
102
137
  return diff
103
138
 
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
- )
139
+ from dataclasses import asdict
140
+ from intentumdiff.rust_core import _c_abi_call, _require_complete_batch_diff_dto
120
141
 
121
- policy = load_guardrail_policy(
122
- diff.new_filename or diff.old_filename,
123
- explicit_path=config.guardrail_policy_path,
142
+ # FileSource transports the physical identities for both sides, preserving
143
+ # user-facing aliases. For cross-directory moves include both applicable
144
+ # policy files; only Rust interprets and matches their rules (#262).
145
+ policies = _load_relevant_guardrail_policies(
146
+ policy_old_filename or diff.old_filename,
147
+ policy_filename or diff.new_filename,
148
+ config.guardrail_policy_path,
124
149
  )
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
150
+ request = {
151
+ "diff": diff.model_dump(mode="json"),
152
+ "old_tree": old_tree.model_dump(mode="json") if old_tree else None,
153
+ "new_tree": new_tree.model_dump(mode="json") if new_tree else None,
154
+ "old_source": old_source, "new_source": new_source,
155
+ "rules": [asdict(rule) for policy in policies for rule in policy.rules],
156
+ }
157
+ # Pure host-I/O provenance: Rust selects filename-scoped rules and
158
+ # immutable policy-file warnings; public labels remain untouched (#260).
159
+ if policy_filename is not None:
160
+ request["guardrail_new_filename"] = policy_filename
161
+ if policy_old_filename is not None:
162
+ request["guardrail_old_filename"] = policy_old_filename
163
+ response = _c_abi_call("apply_guardrail_policy", json.dumps(request))
164
+ result = SemanticDiff.model_validate(_require_complete_batch_diff_dto(response))
165
+ violations = result.guardrail_violations[len(diff.guardrail_violations):]
130
166
 
131
167
  if diagnostics is not None and diagnostics.enabled:
132
168
  for violation in violations:
@@ -156,136 +192,7 @@ def apply_guardrails_to_diff(
156
192
  },
157
193
  )
158
194
 
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()
195
+ return result
289
196
 
290
197
 
291
198
  def _is_policy_file(filename: str | None) -> bool:
@@ -299,20 +206,36 @@ def _find_policy_path(filename: str, explicit_path: Path | None) -> Path | None:
299
206
  starts: list[Path] = []
300
207
  raw = Path(filename)
301
208
  if filename and not filename.startswith("<"):
302
- starts.append(raw if raw.is_dir() else raw.parent)
303
- starts.append(Path.cwd())
209
+ # The argument identifies a source *file*, even if its current checkout
210
+ # entry is a symlink to a directory. Do not follow that final component
211
+ # into unrelated external policy directories for a historical Git diff.
212
+ # For relative paths, the parent resolves against cwd already. An
213
+ # absolute path must never fall back to an unrelated process cwd.
214
+ starts.append(raw.parent)
215
+ else:
216
+ # Synthetic/in-memory sources have no physical policy-search origin.
217
+ starts.append(Path.cwd())
304
218
 
305
219
  seen: set[Path] = set()
306
220
  for start in starts:
307
221
  try:
308
222
  current = start.resolve()
309
- except OSError:
310
- current = Path.cwd()
223
+ except OSError as exc:
224
+ # A failed physical-path resolution is not permission to import
225
+ # a policy from another repository's process working directory.
226
+ # Report the host I/O failure instead of manufacturing a clean (or
227
+ # foreign-policy) guardrail result.
228
+ raise RuntimeError(
229
+ f"Unable to resolve guardrail policy search path {start}: {exc}"
230
+ ) from exc
311
231
  for directory in (current, *current.parents):
312
232
  if directory in seen:
313
233
  continue
314
234
  seen.add(directory)
315
235
  candidate = directory / GUARDRAIL_POLICY_FILENAME
316
- if candidate.exists():
236
+ # A dangling symlink is an explicitly present but unusable policy.
237
+ # Treating it as absent would silently disable protected-change checks.
238
+ # The subsequent strict read must report the missing target instead.
239
+ if candidate.exists() or candidate.is_symlink():
317
240
  return candidate
318
241
  return None