Skip to content

Commit 109bafb

Browse files
committed
feat(sleep): discover the versioned marketplace plugin layout
Cache discovery stopped at <cache>/<marketplace>/<plugin>/skills. Checked against a real Claude install: that layout does not occur there at all. Every installed plugin exposes skills at <cache>/<marketplace>/<plugin>/<version>/skills — 9 of 9 — so plugin-cache discovery returned nothing on a working machine, not merely "missed some". Supports both layouts, but deliberately contributes AT MOST ONE root per installed plugin: several versions of the same plugin can be present at once (the real install carries three of one plugin). Adding each version directory as a peer root would make an ordinary upgraded plugin resolve AMBIGUOUS, which would fire constantly for the most common case. The newest version wins, with the legacy unversioned directory as the fallback when no version directory carries skills. Version ordering is numeric per segment, so 1.10.0 outranks 1.9.0 rather than losing a string comparison; non-numeric segments (2.0.0-beta) sort below a bare number at the same position, and the directory name is the final tie-break so the order is total and deterministic. Tests add the sanitized real-install fixture that was requested: the versioned layout, a three-version plugin asserting newest-wins rather than AMBIGUOUS, numeric-vs-lexicographic ordering, two marketplaces each contributing a root, and the legacy layout still resolving. Verified against the actual cache: roots discovered went 1 -> 8, and chrome-devtools-mcp resolves to 1.6.0 only, never 1.1.1 or 1.5.0.
1 parent 1be51ce commit 109bafb

2 files changed

Lines changed: 126 additions & 4 deletions

File tree

‎skillopt_sleep/skill_resolver.py‎

Lines changed: 49 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
from __future__ import annotations
1313

1414
import os
15+
import re
1516
from dataclasses import dataclass
1617
from typing import List, Sequence, Tuple
1718

@@ -72,12 +73,54 @@ def _listdir(path: str) -> List[str]:
7273
return []
7374

7475

76+
def _version_sort_key(name: str) -> tuple:
77+
"""Order version directory names newest-last, numerically where possible.
78+
79+
``1.10.0`` must sort above ``1.9.0``, so numeric segments compare as ints.
80+
Non-numeric segments (``2.0.0-beta``) compare as strings and sort below a
81+
bare number at the same position, keeping prereleases under releases. The
82+
name is the final tie-break so the order is always total and deterministic.
83+
"""
84+
segments = []
85+
for part in re.split(r"[._\-+]", name):
86+
if part.isdigit():
87+
segments.append((1, int(part), ""))
88+
else:
89+
segments.append((0, 0, part))
90+
return (segments, name)
91+
92+
93+
def _plugin_skills_root(plugin_dir: str) -> str:
94+
"""The single skills root for one installed plugin, or "" if it has none.
95+
96+
Claude marketplace installs are versioned —
97+
``<plugin>/<version>/skills`` — and several versions of the same plugin can
98+
be present at once. Returning each of them as a peer root would make an
99+
ordinary upgraded plugin resolve AMBIGUOUS, so exactly one root is chosen:
100+
the newest version, falling back to the legacy unversioned
101+
``<plugin>/skills`` layout when no version directory carries skills.
102+
"""
103+
versioned = []
104+
for entry in _listdir(plugin_dir):
105+
candidate = os.path.join(plugin_dir, entry, "skills")
106+
if os.path.isdir(candidate):
107+
versioned.append((_version_sort_key(entry), candidate))
108+
if versioned:
109+
versioned.sort()
110+
return versioned[-1][1]
111+
112+
legacy = os.path.join(plugin_dir, "skills")
113+
return legacy if os.path.isdir(legacy) else ""
114+
115+
75116
def skill_search_roots(cfg: object) -> List[str]:
76117
"""Documented local skill roots for a config: user skills, then plugin cache.
77118
78-
``<claude_home>/skills`` holds hand-written skills; installed Claude Code
79-
plugins expose theirs under ``<claude_home>/plugins/cache/*/*/skills``.
80-
Only existing directories are returned, in that fixed precedence order.
119+
``<claude_home>/skills`` holds hand-written skills. Installed Claude Code
120+
plugins expose theirs under the plugin cache, in either the versioned
121+
marketplace layout ``plugins/cache/<marketplace>/<plugin>/<version>/skills``
122+
or the legacy ``plugins/cache/<marketplace>/<plugin>/skills``. At most one
123+
root per installed plugin is returned, in that fixed precedence order.
81124
"""
82125
configured = str(getattr(cfg, "claude_home", "") or "").strip()
83126
if not configured:
@@ -93,7 +136,9 @@ def skill_search_roots(cfg: object) -> List[str]:
93136
if not os.path.isdir(plugins_dir):
94137
continue
95138
for plugin in _listdir(plugins_dir):
96-
roots.append(os.path.join(plugins_dir, plugin, "skills"))
139+
root = _plugin_skills_root(os.path.join(plugins_dir, plugin))
140+
if root:
141+
roots.append(root)
97142
return [r for r in roots if os.path.isdir(r)]
98143

99144

‎tests/test_sleep_skill_resolver.py‎

Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -204,6 +204,83 @@ def test_resolution_through_config_roots_prefers_the_user_skill(self):
204204
self.assertEqual(res.status, FOUND)
205205
self.assertEqual(res.path, os.path.realpath(expected))
206206

207+
def test_versioned_marketplace_layout_is_discovered(self):
208+
# Sanitized mirror of a real marketplace install. Observed layout on a
209+
# working machine: every plugin skills dir sits at
210+
# <cache>/<marketplace>/<plugin>/<version>/skills — the unversioned
211+
# layout did not occur at all, so discovery must handle this one.
212+
with tempfile.TemporaryDirectory() as tmp:
213+
claude_home = os.path.join(tmp, ".claude")
214+
cache = os.path.join(claude_home, "plugins", "cache")
215+
expected = _write_skill(
216+
os.path.join(cache, "claude-plugins-official", "superpowers", "5.0.7", "skills"),
217+
"brainstorming",
218+
)
219+
cfg = load_config(claude_home=claude_home)
220+
self.assertIn(
221+
os.path.join(cache, "claude-plugins-official", "superpowers", "5.0.7", "skills"),
222+
skill_search_roots(cfg),
223+
)
224+
res = resolve_skill("brainstorming", skill_search_roots(cfg))
225+
self.assertEqual(res.status, FOUND)
226+
self.assertEqual(res.path, os.path.realpath(expected))
227+
228+
def test_multiple_installed_versions_resolve_to_the_newest_not_ambiguous(self):
229+
# A plugin upgraded in place keeps older version dirs alongside the new
230+
# one (observed: three versions of the same plugin). Treating each as a
231+
# peer root would make an ordinary upgrade resolve AMBIGUOUS.
232+
with tempfile.TemporaryDirectory() as tmp:
233+
claude_home = os.path.join(tmp, ".claude")
234+
plugin = os.path.join(claude_home, "plugins", "cache",
235+
"claude-plugins-official", "chrome-devtools-mcp")
236+
for version in ["1.1.1", "1.5.0", "1.6.0"]:
237+
_write_skill(os.path.join(plugin, version, "skills"), "chrome-devtools")
238+
newest = os.path.join(plugin, "1.6.0", "skills")
239+
240+
cfg = load_config(claude_home=claude_home)
241+
roots = skill_search_roots(cfg)
242+
self.assertEqual([r for r in roots if r.startswith(plugin)], [newest])
243+
244+
res = resolve_skill("chrome-devtools", roots)
245+
self.assertEqual(res.status, FOUND)
246+
self.assertEqual(res.path,
247+
os.path.realpath(os.path.join(newest, "chrome-devtools", "SKILL.md")))
248+
249+
def test_version_ordering_is_numeric_not_lexicographic(self):
250+
with tempfile.TemporaryDirectory() as tmp:
251+
claude_home = os.path.join(tmp, ".claude")
252+
plugin = os.path.join(claude_home, "plugins", "cache", "market", "plugin")
253+
for version in ["1.9.0", "1.10.0"]:
254+
_write_skill(os.path.join(plugin, version, "skills"), "example-skill")
255+
cfg = load_config(claude_home=claude_home)
256+
roots = [r for r in skill_search_roots(cfg) if r.startswith(plugin)]
257+
# "1.10.0" < "1.9.0" as strings; it must still win as a version.
258+
self.assertEqual(roots, [os.path.join(plugin, "1.10.0", "skills")])
259+
260+
def test_legacy_unversioned_layout_still_works(self):
261+
with tempfile.TemporaryDirectory() as tmp:
262+
claude_home = os.path.join(tmp, ".claude")
263+
plugin = os.path.join(claude_home, "plugins", "cache", "market", "plugin")
264+
expected = _write_skill(os.path.join(plugin, "skills"), "example-skill")
265+
cfg = load_config(claude_home=claude_home)
266+
self.assertIn(os.path.join(plugin, "skills"), skill_search_roots(cfg))
267+
res = resolve_skill("example-skill", skill_search_roots(cfg))
268+
self.assertEqual(res.status, FOUND)
269+
self.assertEqual(res.path, os.path.realpath(expected))
270+
271+
def test_two_marketplaces_each_contribute_a_root(self):
272+
with tempfile.TemporaryDirectory() as tmp:
273+
claude_home = os.path.join(tmp, ".claude")
274+
cache = os.path.join(claude_home, "plugins", "cache")
275+
official = os.path.join(cache, "claude-plugins-official", "sentry", "1.0.0", "skills")
276+
cognee = os.path.join(cache, "cognee", "cognee-memory", "1.0.0", "skills")
277+
_write_skill(official, "sentry-skill")
278+
_write_skill(cognee, "cognee-remember")
279+
cfg = load_config(claude_home=claude_home)
280+
roots = skill_search_roots(cfg)
281+
self.assertIn(official, roots)
282+
self.assertIn(cognee, roots)
283+
207284
def test_legacy_target_skill_path_behavior_is_untouched(self):
208285
with tempfile.TemporaryDirectory() as tmp:
209286
target = os.path.join(tmp, "repo", "SKILL.md")

0 commit comments

Comments
 (0)