skill-lens-cli 0.1.0__py3-none-any.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.
- skill_lens/__init__.py +3 -0
- skill_lens/cli.py +326 -0
- skill_lens/core/__init__.py +1 -0
- skill_lens/core/compare.py +172 -0
- skill_lens/core/diff.py +503 -0
- skill_lens/core/discovery.py +562 -0
- skill_lens/core/doctor.py +464 -0
- skill_lens/core/exitcodes.py +151 -0
- skill_lens/core/hasher.py +117 -0
- skill_lens/core/parser.py +311 -0
- skill_lens/core/paths.py +130 -0
- skill_lens/core/resolver.py +607 -0
- skill_lens/core/scanner.py +230 -0
- skill_lens/core/system.py +96 -0
- skill_lens/models/__init__.py +73 -0
- skill_lens/models/compare.py +104 -0
- skill_lens/models/diff.py +231 -0
- skill_lens/models/doctor.py +98 -0
- skill_lens/models/enums.py +84 -0
- skill_lens/models/installation.py +74 -0
- skill_lens/models/parsing.py +105 -0
- skill_lens/models/resolution.py +108 -0
- skill_lens/py.typed +0 -0
- skill_lens/registry/__init__.py +14 -0
- skill_lens/registry/agents/antigravity.toml +50 -0
- skill_lens/registry/agents/claude.toml +34 -0
- skill_lens/registry/agents/codex.toml +64 -0
- skill_lens/registry/agents/dsh.toml +93 -0
- skill_lens/registry/agents/grok.toml +37 -0
- skill_lens/registry/agents/omp.toml +84 -0
- skill_lens/registry/agents/opencode.toml +48 -0
- skill_lens/registry/agents/pi.toml +48 -0
- skill_lens/registry/agents/qoder.toml +38 -0
- skill_lens/registry/agents/windsurf.toml +38 -0
- skill_lens/registry/loader.py +128 -0
- skill_lens/render.py +464 -0
- skill_lens_cli-0.1.0.dist-info/METADATA +296 -0
- skill_lens_cli-0.1.0.dist-info/RECORD +41 -0
- skill_lens_cli-0.1.0.dist-info/WHEEL +4 -0
- skill_lens_cli-0.1.0.dist-info/entry_points.txt +2 -0
- skill_lens_cli-0.1.0.dist-info/licenses/LICENSE +21 -0
|
@@ -0,0 +1,607 @@
|
|
|
1
|
+
"""The 3-axis resolution engine.
|
|
2
|
+
|
|
3
|
+
For one skill name, one agent and one working directory, this answers *why*
|
|
4
|
+
that name resolves the way it does. Every decision is derived from the agent
|
|
5
|
+
registry, so a reason is always traceable to a ``rule_id`` and an evidence tag
|
|
6
|
+
-- never invented.
|
|
7
|
+
|
|
8
|
+
The three axes:
|
|
9
|
+
|
|
10
|
+
* **Parse status** -- is the copy well-formed? (valid / malformed / ...)
|
|
11
|
+
* **Visibility** -- can this agent's search rules see it? (searched / hidden)
|
|
12
|
+
* **Collision** -- how does it relate to same-named copies? (winner / shadow)
|
|
13
|
+
"""
|
|
14
|
+
|
|
15
|
+
from __future__ import annotations
|
|
16
|
+
|
|
17
|
+
import json
|
|
18
|
+
from dataclasses import dataclass, replace
|
|
19
|
+
from pathlib import Path
|
|
20
|
+
|
|
21
|
+
from skill_lens.core.discovery import (
|
|
22
|
+
DiscoveredEntry,
|
|
23
|
+
DiscoveryIndex,
|
|
24
|
+
RootHit,
|
|
25
|
+
canonical_key,
|
|
26
|
+
discover,
|
|
27
|
+
labels_for_entries,
|
|
28
|
+
)
|
|
29
|
+
from skill_lens.models.enums import (
|
|
30
|
+
Collision,
|
|
31
|
+
Evidence,
|
|
32
|
+
HeadlineState,
|
|
33
|
+
ParseStatus,
|
|
34
|
+
Scope,
|
|
35
|
+
Visibility,
|
|
36
|
+
)
|
|
37
|
+
from skill_lens.models.installation import SkillInstallation
|
|
38
|
+
from skill_lens.models.resolution import CandidateResolution, ResolutionReport
|
|
39
|
+
from skill_lens.registry.loader import AgentDefinition, load_registry
|
|
40
|
+
|
|
41
|
+
_UNREADABLE_CANONICAL = "unreadable"
|
|
42
|
+
_CYCLE = "symlink_cycle"
|
|
43
|
+
_DANGLING = "dangling_symlink"
|
|
44
|
+
_DENIED = "permission_denied"
|
|
45
|
+
|
|
46
|
+
# Coexistence policies under which a lower-ranked copy is *not* suppressed.
|
|
47
|
+
# ``ambiguous``: ties / undocumented policy. ``merge``: every root is listed and
|
|
48
|
+
# no copy is documented to suppress the others (spec section 2, COEXISTS).
|
|
49
|
+
_COEXISTING_POLICIES = frozenset({"ambiguous", "merge"})
|
|
50
|
+
|
|
51
|
+
_FALLBACK_REASON = {
|
|
52
|
+
HeadlineState.ACTIVE: "Winning copy for this agent.",
|
|
53
|
+
HeadlineState.SHADOWED: "Suppressed by a higher-priority copy.",
|
|
54
|
+
HeadlineState.COEXISTS: "Coexists with another copy for this name.",
|
|
55
|
+
HeadlineState.DISABLED: "Explicitly turned off by agent configuration.",
|
|
56
|
+
HeadlineState.UNSEARCHED: "Not searched by this agent.",
|
|
57
|
+
HeadlineState.INVALID: "Rejected: invalid or unreadable skill.",
|
|
58
|
+
HeadlineState.AMBIGUOUS: "Tie or undocumented collision policy.",
|
|
59
|
+
}
|
|
60
|
+
|
|
61
|
+
_NOT_FOUND_NOTE = "No copy of this skill name was found in any search root."
|
|
62
|
+
|
|
63
|
+
|
|
64
|
+
@dataclass(frozen=True, slots=True)
|
|
65
|
+
class Candidate:
|
|
66
|
+
"""One entrypoint of a discovered copy, evaluated for a single agent.
|
|
67
|
+
|
|
68
|
+
A candidate is a *path*, not a canonical library: two entrypoints of the
|
|
69
|
+
same library are two candidates, because precedence is judged on the
|
|
70
|
+
entrypoint path (spec section 4). ``entrypoint_path`` therefore comes from
|
|
71
|
+
``hit`` -- the path this agent's root actually reaches the skill through.
|
|
72
|
+
"""
|
|
73
|
+
|
|
74
|
+
entry: DiscoveredEntry
|
|
75
|
+
hit: RootHit
|
|
76
|
+
searched: bool
|
|
77
|
+
overridden: bool
|
|
78
|
+
|
|
79
|
+
@property
|
|
80
|
+
def entrypoint_path(self) -> str:
|
|
81
|
+
"""The path this agent reaches the skill through."""
|
|
82
|
+
return self.hit.entrypoint_path or self.entry.entrypoint_path
|
|
83
|
+
|
|
84
|
+
@property
|
|
85
|
+
def rank(self) -> int:
|
|
86
|
+
return self.hit.rank if self.searched else -1
|
|
87
|
+
|
|
88
|
+
@property
|
|
89
|
+
def canonical_key(self) -> str:
|
|
90
|
+
return self.entry.canonical_path or f"@{self.entry.entrypoint_path}"
|
|
91
|
+
|
|
92
|
+
@property
|
|
93
|
+
def reason_fragment(self) -> str:
|
|
94
|
+
parse = self.entry.parse
|
|
95
|
+
if parse is not None and parse.description:
|
|
96
|
+
return parse.description
|
|
97
|
+
if self.hit.nested:
|
|
98
|
+
return "nested project copy"
|
|
99
|
+
return "same-named copy"
|
|
100
|
+
|
|
101
|
+
|
|
102
|
+
# --- Small predicates ------------------------------------------------------
|
|
103
|
+
|
|
104
|
+
|
|
105
|
+
def _is_valid(entry: DiscoveredEntry) -> bool:
|
|
106
|
+
return entry.parse_status == ParseStatus.VALID.value
|
|
107
|
+
|
|
108
|
+
|
|
109
|
+
def _searched_hits(agent: AgentDefinition, entry: DiscoveredEntry) -> list[RootHit]:
|
|
110
|
+
return [hit for hit in entry.hits if hit.agent_id == agent.id]
|
|
111
|
+
|
|
112
|
+
|
|
113
|
+
def _representative_hit(
|
|
114
|
+
agent: AgentDefinition, entry: DiscoveredEntry, fallback_root_id: str
|
|
115
|
+
) -> RootHit:
|
|
116
|
+
"""The highest-ranked searched root that reaches ``entry`` for this agent."""
|
|
117
|
+
hits = _searched_hits(agent, entry)
|
|
118
|
+
if hits:
|
|
119
|
+
return max(hits, key=lambda hit: (hit.rank, hit.specificity))
|
|
120
|
+
root = agent.root_by_id(fallback_root_id)
|
|
121
|
+
return RootHit(
|
|
122
|
+
agent_id=agent.id,
|
|
123
|
+
root_id=fallback_root_id,
|
|
124
|
+
rank=0,
|
|
125
|
+
rule_id=root.rule_id if root else agent.unsearched_rule_id,
|
|
126
|
+
evidence=root.evidence.value if root else agent.policy_evidence.value,
|
|
127
|
+
scope=root.scope if root else (entry.hits[0].scope if entry.hits else Scope.USER),
|
|
128
|
+
)
|
|
129
|
+
|
|
130
|
+
|
|
131
|
+
def _default_root_id(agent: AgentDefinition) -> str:
|
|
132
|
+
globals_ = [root for root in agent.roots if not root.is_project]
|
|
133
|
+
if globals_:
|
|
134
|
+
return max(globals_, key=lambda root: root.rank).id
|
|
135
|
+
return agent.roots[0].id if agent.roots else f"{agent.id}_roots"
|
|
136
|
+
|
|
137
|
+
|
|
138
|
+
def _identity_names(agent: AgentDefinition, entry: DiscoveredEntry) -> set[str]:
|
|
139
|
+
"""Every name this agent would use to look up ``entry``.
|
|
140
|
+
|
|
141
|
+
A skill that fails validation (for example a missing ``description``) is
|
|
142
|
+
still *findable*: the frontmatter name is populated for
|
|
143
|
+
``missing_description`` parses, so an agent that identifies skills by
|
|
144
|
+
frontmatter must be able to find it by that name rather than only by its
|
|
145
|
+
folder name.
|
|
146
|
+
"""
|
|
147
|
+
parse = entry.parse
|
|
148
|
+
if parse is None:
|
|
149
|
+
return {entry.name}
|
|
150
|
+
directory_name = parse.directory_name
|
|
151
|
+
frontmatter_name = parse.frontmatter_name or directory_name
|
|
152
|
+
if agent.identity_source == "directory_name":
|
|
153
|
+
return {directory_name}
|
|
154
|
+
if agent.identity_source == "frontmatter_name":
|
|
155
|
+
return {frontmatter_name}
|
|
156
|
+
return {directory_name, frontmatter_name}
|
|
157
|
+
|
|
158
|
+
|
|
159
|
+
def _matches(agent: AgentDefinition, entry: DiscoveredEntry, name: str) -> bool:
|
|
160
|
+
return name in _identity_names(agent, entry)
|
|
161
|
+
|
|
162
|
+
|
|
163
|
+
def _load_disabled_overrides(agent: AgentDefinition, home: Path) -> set[str]:
|
|
164
|
+
if not agent.disabled_settings or not agent.disabled_key:
|
|
165
|
+
return set()
|
|
166
|
+
try:
|
|
167
|
+
data = json.loads((home / agent.disabled_settings).read_text(encoding="utf-8"))
|
|
168
|
+
except (OSError, ValueError):
|
|
169
|
+
return set()
|
|
170
|
+
override = data.get(agent.disabled_key)
|
|
171
|
+
if not isinstance(override, dict):
|
|
172
|
+
return set()
|
|
173
|
+
return {str(name) for name, enabled in override.items() if enabled is False}
|
|
174
|
+
|
|
175
|
+
|
|
176
|
+
def _candidates_for(agent: AgentDefinition, index: DiscoveryIndex, name: str) -> list[Candidate]:
|
|
177
|
+
"""Build one candidate per entrypoint this agent reaches for ``name``.
|
|
178
|
+
|
|
179
|
+
A symlink farm reaches one canonical library through several paths. Each of
|
|
180
|
+
those paths is a genuine candidate for *this* agent, because precedence is
|
|
181
|
+
judged on the entrypoint path (spec section 4) -- a project shortcut into a
|
|
182
|
+
global library outranks the global root. Entries the agent never searches
|
|
183
|
+
contribute a single ``UNSEARCHED`` candidate.
|
|
184
|
+
"""
|
|
185
|
+
overrides = _load_disabled_overrides(agent, Path(index.home))
|
|
186
|
+
fallback_root_id = _default_root_id(agent)
|
|
187
|
+
candidates: list[Candidate] = []
|
|
188
|
+
for entry in index.entries:
|
|
189
|
+
if not _matches(agent, entry, name):
|
|
190
|
+
continue
|
|
191
|
+
overridden = entry.name in overrides
|
|
192
|
+
hits = _searched_hits(agent, entry)
|
|
193
|
+
if not hits:
|
|
194
|
+
candidates.append(
|
|
195
|
+
Candidate(
|
|
196
|
+
entry=entry,
|
|
197
|
+
hit=_representative_hit(agent, entry, fallback_root_id),
|
|
198
|
+
searched=False,
|
|
199
|
+
overridden=overridden,
|
|
200
|
+
)
|
|
201
|
+
)
|
|
202
|
+
continue
|
|
203
|
+
# One candidate per distinct entrypoint path; if two roots of the same
|
|
204
|
+
# agent reach the identical path, the higher-ranked rule represents it.
|
|
205
|
+
best: dict[str, RootHit] = {}
|
|
206
|
+
for hit in hits:
|
|
207
|
+
path = hit.entrypoint_path
|
|
208
|
+
current = best.get(path)
|
|
209
|
+
if current is None or hit.rank > current.rank:
|
|
210
|
+
best[path] = hit
|
|
211
|
+
for hit in best.values():
|
|
212
|
+
candidates.append(Candidate(entry=entry, hit=hit, searched=True, overridden=overridden))
|
|
213
|
+
return candidates
|
|
214
|
+
|
|
215
|
+
|
|
216
|
+
# --- Headline derivation ---------------------------------------------------
|
|
217
|
+
|
|
218
|
+
|
|
219
|
+
def _assign_states(
|
|
220
|
+
agent: AgentDefinition, candidates: list[Candidate]
|
|
221
|
+
) -> list[tuple[Candidate, HeadlineState]]:
|
|
222
|
+
"""Assign each candidate a state and return them alongside it."""
|
|
223
|
+
searched = [c for c in candidates if c.searched]
|
|
224
|
+
if not searched:
|
|
225
|
+
return [(c, HeadlineState.UNSEARCHED) for c in candidates]
|
|
226
|
+
|
|
227
|
+
valid = [c for c in searched if _is_valid(c.entry)]
|
|
228
|
+
if not valid:
|
|
229
|
+
return [
|
|
230
|
+
(c, HeadlineState.INVALID if c.searched else HeadlineState.UNSEARCHED)
|
|
231
|
+
for c in candidates
|
|
232
|
+
]
|
|
233
|
+
|
|
234
|
+
enabled = [c for c in valid if not c.overridden]
|
|
235
|
+
if not enabled:
|
|
236
|
+
return [(c, _fallback_state(c, HeadlineState.DISABLED)) for c in candidates]
|
|
237
|
+
|
|
238
|
+
roles = _active_roles(candidates, agent)
|
|
239
|
+
return [
|
|
240
|
+
(c, roles.get(index, _fallback_state(c, HeadlineState.ACTIVE)))
|
|
241
|
+
for index, c in enumerate(candidates)
|
|
242
|
+
]
|
|
243
|
+
|
|
244
|
+
|
|
245
|
+
# --- Per-candidate state assignment ----------------------------------------
|
|
246
|
+
|
|
247
|
+
|
|
248
|
+
def _active_roles(candidates: list[Candidate], agent: AgentDefinition) -> dict[int, HeadlineState]:
|
|
249
|
+
"""Map each candidate's position to its collision state.
|
|
250
|
+
|
|
251
|
+
The top-ranked searched, valid, enabled entrypoint wins. A nested
|
|
252
|
+
(qualified) copy coexists. Under an ``ambiguous`` or ``merge`` coexistence
|
|
253
|
+
policy any other searched valid copy coexists; otherwise it is shadowed.
|
|
254
|
+
|
|
255
|
+
States are keyed by candidate index, not by canonical target: two
|
|
256
|
+
entrypoints of the same library are distinct candidates with distinct
|
|
257
|
+
ranks, and collapsing them here is what used to lose a shadowed copy.
|
|
258
|
+
"""
|
|
259
|
+
searched_valid = [
|
|
260
|
+
c for c in candidates if c.searched and _is_valid(c.entry) and not c.overridden
|
|
261
|
+
]
|
|
262
|
+
if not searched_valid:
|
|
263
|
+
return {}
|
|
264
|
+
top_rank = max(c.rank for c in searched_valid)
|
|
265
|
+
winners = [c for c in searched_valid if c.rank == top_rank]
|
|
266
|
+
ambiguous = len(winners) > 1
|
|
267
|
+
multi = len(searched_valid) > 1
|
|
268
|
+
roles: dict[int, HeadlineState] = {}
|
|
269
|
+
for index, candidate in enumerate(candidates):
|
|
270
|
+
if ambiguous:
|
|
271
|
+
roles[index] = _fallback_state(candidate, HeadlineState.AMBIGUOUS)
|
|
272
|
+
continue
|
|
273
|
+
roles[index] = _role_for_active(agent, candidate, top_rank, multi)
|
|
274
|
+
return roles
|
|
275
|
+
|
|
276
|
+
|
|
277
|
+
def _role_for_active(
|
|
278
|
+
agent: AgentDefinition, candidate: Candidate, top_rank: int, multi: bool
|
|
279
|
+
) -> HeadlineState:
|
|
280
|
+
if not candidate.searched:
|
|
281
|
+
return HeadlineState.UNSEARCHED
|
|
282
|
+
if not _is_valid(candidate.entry):
|
|
283
|
+
return HeadlineState.INVALID
|
|
284
|
+
if candidate.overridden:
|
|
285
|
+
return HeadlineState.DISABLED
|
|
286
|
+
if candidate.rank == top_rank:
|
|
287
|
+
# A nested winner is qualified; it only "coexists" if another copy exists.
|
|
288
|
+
if candidate.hit.nested and multi:
|
|
289
|
+
return HeadlineState.COEXISTS
|
|
290
|
+
return HeadlineState.ACTIVE
|
|
291
|
+
if candidate.hit.nested:
|
|
292
|
+
return HeadlineState.COEXISTS
|
|
293
|
+
if agent.coexist_policy in _COEXISTING_POLICIES:
|
|
294
|
+
return HeadlineState.COEXISTS
|
|
295
|
+
return HeadlineState.SHADOWED
|
|
296
|
+
|
|
297
|
+
|
|
298
|
+
def _headline_from_states(states: list[HeadlineState]) -> HeadlineState:
|
|
299
|
+
"""Collapse per-candidate states into the single headline state."""
|
|
300
|
+
if not states:
|
|
301
|
+
return HeadlineState.UNSEARCHED
|
|
302
|
+
if HeadlineState.COEXISTS in states:
|
|
303
|
+
return HeadlineState.COEXISTS
|
|
304
|
+
if HeadlineState.ACTIVE in states:
|
|
305
|
+
return HeadlineState.ACTIVE
|
|
306
|
+
if HeadlineState.AMBIGUOUS in states:
|
|
307
|
+
return HeadlineState.AMBIGUOUS
|
|
308
|
+
if HeadlineState.DISABLED in states:
|
|
309
|
+
return HeadlineState.DISABLED
|
|
310
|
+
if HeadlineState.INVALID in states:
|
|
311
|
+
return HeadlineState.INVALID
|
|
312
|
+
return HeadlineState.UNSEARCHED
|
|
313
|
+
|
|
314
|
+
|
|
315
|
+
def _fallback_state(candidate: Candidate, headline: HeadlineState) -> HeadlineState:
|
|
316
|
+
"""Assign a candidate's state for a non-ACTIVE headline."""
|
|
317
|
+
if not candidate.searched:
|
|
318
|
+
return HeadlineState.UNSEARCHED
|
|
319
|
+
if headline is HeadlineState.UNSEARCHED:
|
|
320
|
+
return HeadlineState.UNSEARCHED
|
|
321
|
+
if headline is HeadlineState.INVALID:
|
|
322
|
+
return HeadlineState.INVALID
|
|
323
|
+
if headline is HeadlineState.AMBIGUOUS:
|
|
324
|
+
if candidate.overridden:
|
|
325
|
+
return HeadlineState.DISABLED
|
|
326
|
+
if not _is_valid(candidate.entry):
|
|
327
|
+
return HeadlineState.INVALID
|
|
328
|
+
return HeadlineState.AMBIGUOUS
|
|
329
|
+
if headline is HeadlineState.DISABLED:
|
|
330
|
+
return HeadlineState.DISABLED if candidate.overridden else HeadlineState.UNSEARCHED
|
|
331
|
+
if not _is_valid(candidate.entry):
|
|
332
|
+
return HeadlineState.INVALID
|
|
333
|
+
if candidate.overridden:
|
|
334
|
+
return HeadlineState.DISABLED
|
|
335
|
+
return headline
|
|
336
|
+
|
|
337
|
+
|
|
338
|
+
# --- Building the output ---------------------------------------------------
|
|
339
|
+
|
|
340
|
+
|
|
341
|
+
def _installation(candidate: Candidate) -> SkillInstallation:
|
|
342
|
+
entry = candidate.entry
|
|
343
|
+
parse = entry.parse
|
|
344
|
+
return SkillInstallation(
|
|
345
|
+
name=entry.name,
|
|
346
|
+
entrypoint_path=candidate.entrypoint_path,
|
|
347
|
+
canonical_path=entry.canonical_path or candidate.entrypoint_path,
|
|
348
|
+
scope=candidate.hit.scope,
|
|
349
|
+
parse_status=parse.status if parse else ParseStatus.UNREADABLE,
|
|
350
|
+
content_hash=entry.content_hash,
|
|
351
|
+
description=parse.description if parse else None,
|
|
352
|
+
frontmatter_name=parse.frontmatter_name if parse else None,
|
|
353
|
+
is_symlink=entry.is_symlink,
|
|
354
|
+
# Paths, not agent ids (Phase 0 contract): every entrypoint that reaches
|
|
355
|
+
# this canonical library, home-relative in display contexts.
|
|
356
|
+
agent_entrypoints=entry.entrypoint_paths or (candidate.entrypoint_path,),
|
|
357
|
+
variant_label=entry.variant_label,
|
|
358
|
+
)
|
|
359
|
+
|
|
360
|
+
|
|
361
|
+
def _winner_label(agent: AgentDefinition, winner: Candidate) -> str:
|
|
362
|
+
"""A short, human label for the root that won, e.g. ``the project root .grok/skills``.
|
|
363
|
+
|
|
364
|
+
Registry paths are home-relative for global roots and repository-relative
|
|
365
|
+
for project roots, so the two must be named differently -- otherwise
|
|
366
|
+
"``.agents/skills``" would read as the home copy when the project copy won.
|
|
367
|
+
"""
|
|
368
|
+
root = agent.root_by_id(winner.hit.root_id)
|
|
369
|
+
if root is None:
|
|
370
|
+
return f"the {winner.hit.root_id} root"
|
|
371
|
+
if root.is_absolute:
|
|
372
|
+
return f"the absolute root {root.path}"
|
|
373
|
+
if root.is_project:
|
|
374
|
+
return f"the project root {root.path}"
|
|
375
|
+
return f"the global root ~/{root.path}"
|
|
376
|
+
|
|
377
|
+
|
|
378
|
+
def _reason(
|
|
379
|
+
agent: AgentDefinition,
|
|
380
|
+
candidate: Candidate,
|
|
381
|
+
state: HeadlineState,
|
|
382
|
+
winner: Candidate | None = None,
|
|
383
|
+
) -> str:
|
|
384
|
+
entry = candidate.entry
|
|
385
|
+
if entry.error_code == _CYCLE:
|
|
386
|
+
return "Symlink cycle detected; traversal stopped safely."
|
|
387
|
+
if entry.error_code == _DANGLING:
|
|
388
|
+
return "Dangling symlink; no real target to resolve."
|
|
389
|
+
if entry.error_code == _DENIED or candidate.canonical_key == _UNREADABLE_CANONICAL:
|
|
390
|
+
return "Directory unreadable (permission denied)."
|
|
391
|
+
if state is HeadlineState.DISABLED:
|
|
392
|
+
return "Disabled by agent configuration override."
|
|
393
|
+
if state is HeadlineState.UNSEARCHED:
|
|
394
|
+
return f"Not searched by {agent.name} (found via another agent's root)."
|
|
395
|
+
if state is HeadlineState.AMBIGUOUS:
|
|
396
|
+
return f"Ties with another copy; {agent.name}'s collision policy is undocumented."
|
|
397
|
+
if state is HeadlineState.ACTIVE:
|
|
398
|
+
if candidate.hit.nested:
|
|
399
|
+
return f"Qualified nested copy ({candidate.reason_fragment}) wins for this directory."
|
|
400
|
+
if entry.is_symlink:
|
|
401
|
+
return "Canonical symlink entrypoint is the winning copy."
|
|
402
|
+
return f"Highest-priority copy ({candidate.reason_fragment})."
|
|
403
|
+
if state is HeadlineState.COEXISTS:
|
|
404
|
+
if candidate.hit.nested:
|
|
405
|
+
return "Qualified namespaced copy, accessible alongside the winning copy."
|
|
406
|
+
if agent.coexist_policy == "merge":
|
|
407
|
+
return (
|
|
408
|
+
f"Listed alongside a higher-priority copy; {agent.name} merges all roots "
|
|
409
|
+
"with no documented winner."
|
|
410
|
+
)
|
|
411
|
+
if agent.coexist_policy == "ambiguous":
|
|
412
|
+
return "Listed alongside another copy; collision policy is undocumented."
|
|
413
|
+
return "Coexists with another copy; both remain available."
|
|
414
|
+
if state is HeadlineState.SHADOWED:
|
|
415
|
+
# Name the *winning* copy's root, never this copy's own description.
|
|
416
|
+
if winner is not None:
|
|
417
|
+
return f"Suppressed by the higher-priority copy in {_winner_label(agent, winner)}."
|
|
418
|
+
return _FALLBACK_REASON[state]
|
|
419
|
+
return _FALLBACK_REASON[state]
|
|
420
|
+
|
|
421
|
+
|
|
422
|
+
_COLLISION_FOR_STATE = {
|
|
423
|
+
HeadlineState.ACTIVE: Collision.WINNING_ENTRY,
|
|
424
|
+
HeadlineState.SHADOWED: Collision.SUPPRESSED_SHADOW,
|
|
425
|
+
HeadlineState.COEXISTS: Collision.COEXISTING_MERGED,
|
|
426
|
+
HeadlineState.DISABLED: Collision.UNVERIFIED_POLICY,
|
|
427
|
+
HeadlineState.UNSEARCHED: Collision.UNVERIFIED_POLICY,
|
|
428
|
+
HeadlineState.INVALID: Collision.UNVERIFIED_POLICY,
|
|
429
|
+
HeadlineState.AMBIGUOUS: Collision.AMBIGUOUS_TIE,
|
|
430
|
+
}
|
|
431
|
+
|
|
432
|
+
_VISIBILITY_FOR_STATE = {
|
|
433
|
+
HeadlineState.ACTIVE: Visibility.ACTIVE_ROOT,
|
|
434
|
+
HeadlineState.SHADOWED: Visibility.ACTIVE_ROOT,
|
|
435
|
+
HeadlineState.COEXISTS: Visibility.ACTIVE_ROOT,
|
|
436
|
+
HeadlineState.DISABLED: Visibility.DISABLED,
|
|
437
|
+
HeadlineState.UNSEARCHED: Visibility.UNSEARCHED_ROOT,
|
|
438
|
+
HeadlineState.INVALID: Visibility.ACTIVE_ROOT,
|
|
439
|
+
HeadlineState.AMBIGUOUS: Visibility.ACTIVE_ROOT,
|
|
440
|
+
}
|
|
441
|
+
|
|
442
|
+
|
|
443
|
+
def _rule_and_evidence(
|
|
444
|
+
agent: AgentDefinition,
|
|
445
|
+
candidate: Candidate,
|
|
446
|
+
state: HeadlineState,
|
|
447
|
+
winner: Candidate | None = None,
|
|
448
|
+
) -> tuple[str, Evidence]:
|
|
449
|
+
entry = candidate.entry
|
|
450
|
+
if entry.error_code == _CYCLE:
|
|
451
|
+
return "symlink_cycle_guard", Evidence.EMPIRICAL
|
|
452
|
+
if not _is_valid(entry):
|
|
453
|
+
if candidate.hit.nested and candidate.hit.nested_rule_id:
|
|
454
|
+
return candidate.hit.nested_rule_id, Evidence(candidate.hit.evidence)
|
|
455
|
+
return "parser_requires_valid_frontmatter", Evidence.DOCUMENTED
|
|
456
|
+
if state is HeadlineState.AMBIGUOUS and agent.ambiguous_rule_id:
|
|
457
|
+
return agent.ambiguous_rule_id, Evidence(agent.policy_evidence)
|
|
458
|
+
if state is HeadlineState.DISABLED and agent.disabled_rule_id:
|
|
459
|
+
return agent.disabled_rule_id, Evidence.DOCUMENTED
|
|
460
|
+
if state is HeadlineState.UNSEARCHED:
|
|
461
|
+
return agent.unsearched_rule_id, Evidence(agent.policy_evidence)
|
|
462
|
+
if state is HeadlineState.SHADOWED and winner is not None:
|
|
463
|
+
# The rule that explains a shadow is the rule of the copy that beat it,
|
|
464
|
+
# so the reason is traceable to the actual cause, not to the loser.
|
|
465
|
+
return winner.hit.rule_for(entry.is_file), Evidence(winner.hit.evidence)
|
|
466
|
+
return candidate.hit.rule_for(entry.is_file), Evidence(candidate.hit.evidence)
|
|
467
|
+
|
|
468
|
+
|
|
469
|
+
def _resolution(
|
|
470
|
+
agent: AgentDefinition,
|
|
471
|
+
candidate: Candidate,
|
|
472
|
+
state: HeadlineState,
|
|
473
|
+
winner: Candidate | None = None,
|
|
474
|
+
) -> CandidateResolution:
|
|
475
|
+
rule_id, evidence = _rule_and_evidence(agent, candidate, state, winner)
|
|
476
|
+
collision = _COLLISION_FOR_STATE[state]
|
|
477
|
+
if state is HeadlineState.COEXISTS and candidate.hit.nested:
|
|
478
|
+
collision = Collision.QUALIFIED_NAMESPACE
|
|
479
|
+
return CandidateResolution(
|
|
480
|
+
state=state,
|
|
481
|
+
visibility=_VISIBILITY_FOR_STATE[state],
|
|
482
|
+
collision=collision,
|
|
483
|
+
rule_id=rule_id,
|
|
484
|
+
evidence=evidence,
|
|
485
|
+
reason=_reason(agent, candidate, state, winner),
|
|
486
|
+
rank=max(candidate.rank, 0),
|
|
487
|
+
installation=_installation(candidate),
|
|
488
|
+
)
|
|
489
|
+
|
|
490
|
+
|
|
491
|
+
_STATE_ORDER = {
|
|
492
|
+
HeadlineState.ACTIVE: 0,
|
|
493
|
+
HeadlineState.COEXISTS: 1,
|
|
494
|
+
HeadlineState.DISABLED: 2,
|
|
495
|
+
HeadlineState.SHADOWED: 3,
|
|
496
|
+
HeadlineState.AMBIGUOUS: 4,
|
|
497
|
+
HeadlineState.INVALID: 5,
|
|
498
|
+
HeadlineState.UNSEARCHED: 6,
|
|
499
|
+
}
|
|
500
|
+
|
|
501
|
+
|
|
502
|
+
def _assign_variant_labels(candidates: list[Candidate]) -> dict[int, str]:
|
|
503
|
+
"""Label differing copies of one name as Variant A / B / C.
|
|
504
|
+
|
|
505
|
+
Delegates to the shared rule in :mod:`skill_lens.core.discovery` so that
|
|
506
|
+
``why``, ``scan`` and ``diff`` can never disagree about a copy's label.
|
|
507
|
+
Ordering is by scope specificity (project before user), then discovery
|
|
508
|
+
order -- *not* by this agent's rank, because ``scan`` has no agent to rank
|
|
509
|
+
with. Which copy actually wins is reported by the candidate's ``state``, not
|
|
510
|
+
by its letter.
|
|
511
|
+
"""
|
|
512
|
+
labels_by_key = labels_for_entries(candidate.entry for candidate in candidates)
|
|
513
|
+
position_by_key: dict[str, int] = {}
|
|
514
|
+
for position, candidate in enumerate(candidates):
|
|
515
|
+
position_by_key.setdefault(canonical_key(candidate.entry), position)
|
|
516
|
+
return {
|
|
517
|
+
position_by_key[key]: label
|
|
518
|
+
for key, label in labels_by_key.items()
|
|
519
|
+
if key in position_by_key
|
|
520
|
+
}
|
|
521
|
+
|
|
522
|
+
|
|
523
|
+
def _sort_resolutions(resolutions: list[CandidateResolution]) -> list[CandidateResolution]:
|
|
524
|
+
deduped: list[CandidateResolution] = []
|
|
525
|
+
seen: set[tuple[str, str]] = set()
|
|
526
|
+
for resolution in resolutions:
|
|
527
|
+
key = (resolution.installation.entrypoint_path, resolution.state.value)
|
|
528
|
+
if key in seen:
|
|
529
|
+
continue
|
|
530
|
+
seen.add(key)
|
|
531
|
+
deduped.append(resolution)
|
|
532
|
+
deduped.sort(
|
|
533
|
+
key=lambda r: (_STATE_ORDER.get(r.state, 99), -r.rank, r.installation.entrypoint_path)
|
|
534
|
+
)
|
|
535
|
+
return deduped
|
|
536
|
+
|
|
537
|
+
|
|
538
|
+
# --- Public entry point ----------------------------------------------------
|
|
539
|
+
|
|
540
|
+
|
|
541
|
+
def resolve_skill(
|
|
542
|
+
name: str,
|
|
543
|
+
agent_id: str,
|
|
544
|
+
cwd: Path,
|
|
545
|
+
home: Path,
|
|
546
|
+
index: DiscoveryIndex | None = None,
|
|
547
|
+
registry: dict[str, AgentDefinition] | None = None,
|
|
548
|
+
) -> ResolutionReport:
|
|
549
|
+
"""Resolve ``name`` for ``agent_id`` from ``cwd`` and explain the result."""
|
|
550
|
+
agents = registry if registry is not None else load_registry()
|
|
551
|
+
if agent_id not in agents:
|
|
552
|
+
known = ", ".join(sorted(agents))
|
|
553
|
+
raise KeyError(f"unknown agent '{agent_id}'. Known agents: {known}")
|
|
554
|
+
agent = agents[agent_id]
|
|
555
|
+
discovery = index if index is not None else discover(home, cwd, agents)
|
|
556
|
+
|
|
557
|
+
candidates = _candidates_for(agent, discovery, name)
|
|
558
|
+
outcomes = _assign_states(agent, candidates)
|
|
559
|
+
headline = _headline_from_states([state for _candidate, state in outcomes])
|
|
560
|
+
|
|
561
|
+
# The winning copy explains every shadow, so it is resolved first and passed
|
|
562
|
+
# to the rest: their reason and rule then name the real cause.
|
|
563
|
+
winner = next(
|
|
564
|
+
(c for c, state in outcomes if state is HeadlineState.ACTIVE and c.searched), None
|
|
565
|
+
)
|
|
566
|
+
ordered = _sort_resolutions(
|
|
567
|
+
[_resolution(agent, candidate, state, winner) for candidate, state in outcomes]
|
|
568
|
+
)
|
|
569
|
+
|
|
570
|
+
labels = {
|
|
571
|
+
candidates[index].entrypoint_path: label
|
|
572
|
+
for index, label in _assign_variant_labels(candidates).items()
|
|
573
|
+
}
|
|
574
|
+
ordered = [_label_resolution(resolution, labels) for resolution in ordered]
|
|
575
|
+
|
|
576
|
+
notes: tuple[str, ...] = ()
|
|
577
|
+
if not candidates:
|
|
578
|
+
# F-10: UNSEARCHED means "on disk, outside this agent's search rules".
|
|
579
|
+
# Zero candidates is a different fact: the name is nowhere on disk.
|
|
580
|
+
notes = (_NOT_FOUND_NOTE,)
|
|
581
|
+
elif headline is HeadlineState.AMBIGUOUS:
|
|
582
|
+
notes = (f"{agent.name} has no documented winner for duplicate names.",)
|
|
583
|
+
return ResolutionReport(
|
|
584
|
+
skill_name=name,
|
|
585
|
+
agent=agent.id,
|
|
586
|
+
agent_name=agent.name,
|
|
587
|
+
cwd=str(cwd),
|
|
588
|
+
collision_policy=agent.collision_policy,
|
|
589
|
+
policy_evidence=agent.policy_evidence,
|
|
590
|
+
headline=headline,
|
|
591
|
+
candidates=tuple(ordered),
|
|
592
|
+
notes=notes,
|
|
593
|
+
found=bool(candidates),
|
|
594
|
+
)
|
|
595
|
+
|
|
596
|
+
|
|
597
|
+
def _label_resolution(
|
|
598
|
+
resolution: CandidateResolution, labels: dict[str, str]
|
|
599
|
+
) -> CandidateResolution:
|
|
600
|
+
"""Attach this entrypoint's variant label to its installation."""
|
|
601
|
+
label = labels.get(resolution.installation.entrypoint_path)
|
|
602
|
+
if label is None or resolution.installation.variant_label == label:
|
|
603
|
+
return resolution
|
|
604
|
+
return replace(resolution, installation=replace(resolution.installation, variant_label=label))
|
|
605
|
+
|
|
606
|
+
|
|
607
|
+
__all__ = ["Candidate", "resolve_skill"]
|